xref: /openbmc/linux/fs/erofs/zdata.c (revision 07888c66)
147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only
247e4937aSGao Xiang /*
347e4937aSGao Xiang  * Copyright (C) 2018 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov  *             https://www.huawei.com/
547e4937aSGao Xiang  */
647e4937aSGao Xiang #include "zdata.h"
747e4937aSGao Xiang #include "compress.h"
847e4937aSGao Xiang #include <linux/prefetch.h>
947e4937aSGao Xiang 
1047e4937aSGao Xiang #include <trace/events/erofs.h>
1147e4937aSGao Xiang 
1247e4937aSGao Xiang /*
139f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
149f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
159f6cc76eSGao Xiang  */
169f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
179f6cc76eSGao Xiang 	struct kmem_cache *slab;
189f6cc76eSGao Xiang 	unsigned int maxpages;
199f6cc76eSGao Xiang 	char name[48];
209f6cc76eSGao Xiang };
219f6cc76eSGao Xiang 
229f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
239f6cc76eSGao Xiang 
249f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
259f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
269f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
279f6cc76eSGao Xiang };
289f6cc76eSGao Xiang 
299f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
309f6cc76eSGao Xiang {
319f6cc76eSGao Xiang 	int i;
329f6cc76eSGao Xiang 
339f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
349f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
359f6cc76eSGao Xiang 			continue;
369f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
379f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
389f6cc76eSGao Xiang 	}
399f6cc76eSGao Xiang }
409f6cc76eSGao Xiang 
419f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
429f6cc76eSGao Xiang {
439f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
449f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
459f6cc76eSGao Xiang 	unsigned int size;
469f6cc76eSGao Xiang 
479f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
489f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
499f6cc76eSGao Xiang 		size = struct_size(a, compressed_pages, pcs->maxpages);
509f6cc76eSGao Xiang 
519f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
529f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
539f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
549f6cc76eSGao Xiang 		if (pcs->slab)
559f6cc76eSGao Xiang 			continue;
569f6cc76eSGao Xiang 
579f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
589f6cc76eSGao Xiang 		return -ENOMEM;
599f6cc76eSGao Xiang 	}
609f6cc76eSGao Xiang 	return 0;
619f6cc76eSGao Xiang }
629f6cc76eSGao Xiang 
639f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
649f6cc76eSGao Xiang {
659f6cc76eSGao Xiang 	int i;
669f6cc76eSGao Xiang 
679f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
689f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
699f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
709f6cc76eSGao Xiang 
719f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
729f6cc76eSGao Xiang 			continue;
739f6cc76eSGao Xiang 
749f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
759f6cc76eSGao Xiang 		if (!pcl)
769f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
779f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
789f6cc76eSGao Xiang 		return pcl;
799f6cc76eSGao Xiang 	}
809f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
819f6cc76eSGao Xiang }
829f6cc76eSGao Xiang 
839f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
849f6cc76eSGao Xiang {
85cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
869f6cc76eSGao Xiang 	int i;
879f6cc76eSGao Xiang 
889f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
899f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
909f6cc76eSGao Xiang 
91cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
929f6cc76eSGao Xiang 			continue;
939f6cc76eSGao Xiang 
949f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
959f6cc76eSGao Xiang 		return;
969f6cc76eSGao Xiang 	}
979f6cc76eSGao Xiang 	DBG_BUGON(1);
989f6cc76eSGao Xiang }
999f6cc76eSGao Xiang 
10047e4937aSGao Xiang /* how to allocate cached pages for a pcluster */
10147e4937aSGao Xiang enum z_erofs_cache_alloctype {
10247e4937aSGao Xiang 	DONTALLOC,	/* don't allocate any cached pages */
1031825c8d7SGao Xiang 	/*
1041825c8d7SGao Xiang 	 * try to use cached I/O if page allocation succeeds or fallback
1051825c8d7SGao Xiang 	 * to in-place I/O instead to avoid any direct reclaim.
1061825c8d7SGao Xiang 	 */
1071825c8d7SGao Xiang 	TRYALLOC,
10847e4937aSGao Xiang };
10947e4937aSGao Xiang 
11047e4937aSGao Xiang /*
11147e4937aSGao Xiang  * tagged pointer with 1-bit tag for all compressed pages
11247e4937aSGao Xiang  * tag 0 - the page is just found with an extra page reference
11347e4937aSGao Xiang  */
11447e4937aSGao Xiang typedef tagptr1_t compressed_page_t;
11547e4937aSGao Xiang 
11647e4937aSGao Xiang #define tag_compressed_page_justfound(page) \
11747e4937aSGao Xiang 	tagptr_fold(compressed_page_t, page, 1)
11847e4937aSGao Xiang 
11947e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
12047e4937aSGao Xiang 
12147e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
12247e4937aSGao Xiang {
12347e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
1249f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
12547e4937aSGao Xiang }
12647e4937aSGao Xiang 
12799634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
12847e4937aSGao Xiang {
12947e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
13047e4937aSGao Xiang 
13147e4937aSGao Xiang 	/*
13247e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
13347e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
13447e4937aSGao Xiang 	 */
1350e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
1360e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
13747e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
13847e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
13947e4937aSGao Xiang }
14047e4937aSGao Xiang 
14147e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
14247e4937aSGao Xiang {
1439f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
14447e4937aSGao Xiang 
1459f6cc76eSGao Xiang 	if (err)
1469f6cc76eSGao Xiang 		return err;
1479f6cc76eSGao Xiang 	err = z_erofs_init_workqueue();
1489f6cc76eSGao Xiang 	if (err)
1499f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
1509f6cc76eSGao Xiang 	return err;
15147e4937aSGao Xiang }
15247e4937aSGao Xiang 
15347e4937aSGao Xiang enum z_erofs_collectmode {
15447e4937aSGao Xiang 	COLLECT_SECONDARY,
15547e4937aSGao Xiang 	COLLECT_PRIMARY,
15647e4937aSGao Xiang 	/*
15747e4937aSGao Xiang 	 * The current collection was the tail of an exist chain, in addition
15847e4937aSGao Xiang 	 * that the previous processed chained collections are all decided to
15947e4937aSGao Xiang 	 * be hooked up to it.
16047e4937aSGao Xiang 	 * A new chain will be created for the remaining collections which are
16147e4937aSGao Xiang 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
16247e4937aSGao Xiang 	 * the next collection cannot reuse the whole page safely in
16347e4937aSGao Xiang 	 * the following scenario:
16447e4937aSGao Xiang 	 *  ________________________________________________________________
16547e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
16647e4937aSGao Xiang 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
16747e4937aSGao Xiang 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
16847e4937aSGao Xiang 	 */
16947e4937aSGao Xiang 	COLLECT_PRIMARY_HOOKED,
1700b964600SGao Xiang 	/*
1710b964600SGao Xiang 	 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
1720b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
1730b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
1740b964600SGao Xiang 	 * pagevec) since it can be directly decoded without I/O submission.
1750b964600SGao Xiang 	 */
17647e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
17747e4937aSGao Xiang 	/*
17847e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
17947e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
18047e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
18147e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
18247e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
18347e4937aSGao Xiang 	 * illustrated below:
18447e4937aSGao Xiang 	 *  ________________________________________________________________
18547e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
18647e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
18747e4937aSGao Xiang 	 * |  PRIMARY_FOLLOWED or |                                        |
18847e4937aSGao Xiang 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
18947e4937aSGao Xiang 	 *
19047e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
19147e4937aSGao Xiang 	 */
19247e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED,
19347e4937aSGao Xiang };
19447e4937aSGao Xiang 
19547e4937aSGao Xiang struct z_erofs_collector {
19647e4937aSGao Xiang 	struct z_erofs_pagevec_ctor vector;
19747e4937aSGao Xiang 
19847e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
19947e4937aSGao Xiang 	struct z_erofs_collection *cl;
20081382f5fSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
20181382f5fSGao Xiang 	struct page **icpage_ptr;
20247e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
20347e4937aSGao Xiang 
20447e4937aSGao Xiang 	enum z_erofs_collectmode mode;
20547e4937aSGao Xiang };
20647e4937aSGao Xiang 
20747e4937aSGao Xiang struct z_erofs_decompress_frontend {
20847e4937aSGao Xiang 	struct inode *const inode;
20947e4937aSGao Xiang 
21047e4937aSGao Xiang 	struct z_erofs_collector clt;
21147e4937aSGao Xiang 	struct erofs_map_blocks map;
21247e4937aSGao Xiang 
2136ea5aad3SGao Xiang 	bool readahead;
21447e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
21547e4937aSGao Xiang 	bool backmost;
21647e4937aSGao Xiang 	erofs_off_t headoffset;
21747e4937aSGao Xiang };
21847e4937aSGao Xiang 
21947e4937aSGao Xiang #define COLLECTOR_INIT() { \
22047e4937aSGao Xiang 	.owned_head = Z_EROFS_PCLUSTER_TAIL, \
22147e4937aSGao Xiang 	.mode = COLLECT_PRIMARY_FOLLOWED }
22247e4937aSGao Xiang 
22347e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
22447e4937aSGao Xiang 	.inode = __i, .clt = COLLECTOR_INIT(), \
22547e4937aSGao Xiang 	.backmost = true, }
22647e4937aSGao Xiang 
22747e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
22847e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
22947e4937aSGao Xiang 
23047e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt,
23147e4937aSGao Xiang 				     struct address_space *mc,
2321825c8d7SGao Xiang 				     enum z_erofs_cache_alloctype type,
233eaa9172aSGao Xiang 				     struct page **pagepool)
23447e4937aSGao Xiang {
23581382f5fSGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
23647e4937aSGao Xiang 	bool standalone = true;
2371825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
2381825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
23981382f5fSGao Xiang 	struct page **pages;
24081382f5fSGao Xiang 	pgoff_t index;
24147e4937aSGao Xiang 
24247e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
24347e4937aSGao Xiang 		return;
24447e4937aSGao Xiang 
24581382f5fSGao Xiang 	pages = pcl->compressed_pages;
24681382f5fSGao Xiang 	index = pcl->obj.index;
24781382f5fSGao Xiang 	for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
24847e4937aSGao Xiang 		struct page *page;
24947e4937aSGao Xiang 		compressed_page_t t;
2501825c8d7SGao Xiang 		struct page *newpage = NULL;
25147e4937aSGao Xiang 
25247e4937aSGao Xiang 		/* the compressed page was loaded before */
25347e4937aSGao Xiang 		if (READ_ONCE(*pages))
25447e4937aSGao Xiang 			continue;
25547e4937aSGao Xiang 
25647e4937aSGao Xiang 		page = find_get_page(mc, index);
25747e4937aSGao Xiang 
25847e4937aSGao Xiang 		if (page) {
25947e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
2600b964600SGao Xiang 		} else {
2610b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
2620b964600SGao Xiang 			standalone = false;
2630b964600SGao Xiang 			switch (type) {
2640b964600SGao Xiang 			case TRYALLOC:
2651825c8d7SGao Xiang 				newpage = erofs_allocpage(pagepool, gfp);
2661825c8d7SGao Xiang 				if (!newpage)
26747e4937aSGao Xiang 					continue;
2680b964600SGao Xiang 				set_page_private(newpage,
2690b964600SGao Xiang 						 Z_EROFS_PREALLOCATED_PAGE);
2700b964600SGao Xiang 				t = tag_compressed_page_justfound(newpage);
2710b964600SGao Xiang 				break;
2720b964600SGao Xiang 			default:        /* DONTALLOC */
2730b964600SGao Xiang 				continue;
2740b964600SGao Xiang 			}
27547e4937aSGao Xiang 		}
27647e4937aSGao Xiang 
27747e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
27847e4937aSGao Xiang 			continue;
27947e4937aSGao Xiang 
280eaa9172aSGao Xiang 		if (page)
28147e4937aSGao Xiang 			put_page(page);
282eaa9172aSGao Xiang 		else if (newpage)
283eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
28447e4937aSGao Xiang 	}
28547e4937aSGao Xiang 
2860b964600SGao Xiang 	/*
2870b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
2880b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
2890b964600SGao Xiang 	 */
2900b964600SGao Xiang 	if (standalone)
29147e4937aSGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
29247e4937aSGao Xiang }
29347e4937aSGao Xiang 
29447e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
29547e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
29647e4937aSGao Xiang 				       struct erofs_workgroup *grp)
29747e4937aSGao Xiang {
29847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
29947e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
30047e4937aSGao Xiang 	int i;
30147e4937aSGao Xiang 
302cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
30347e4937aSGao Xiang 	/*
30447e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
30547e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
30647e4937aSGao Xiang 	 */
3079f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
30847e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
30947e4937aSGao Xiang 
31047e4937aSGao Xiang 		if (!page)
31147e4937aSGao Xiang 			continue;
31247e4937aSGao Xiang 
31347e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
31447e4937aSGao Xiang 		if (!trylock_page(page))
31547e4937aSGao Xiang 			return -EBUSY;
31647e4937aSGao Xiang 
317f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
31847e4937aSGao Xiang 			continue;
31947e4937aSGao Xiang 
32047e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
32147e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
3226aaa7b06SGao Xiang 		detach_page_private(page);
32347e4937aSGao Xiang 		unlock_page(page);
32447e4937aSGao Xiang 	}
32547e4937aSGao Xiang 	return 0;
32647e4937aSGao Xiang }
32747e4937aSGao Xiang 
328d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
32947e4937aSGao Xiang {
33047e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
33147e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
33247e4937aSGao Xiang 
33347e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
33447e4937aSGao Xiang 		unsigned int i;
33547e4937aSGao Xiang 
336cecf864dSYue Hu 		DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
3379f6cc76eSGao Xiang 		for (i = 0; i < pcl->pclusterpages; ++i) {
33847e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
33947e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
34047e4937aSGao Xiang 				ret = 1;
34147e4937aSGao Xiang 				break;
34247e4937aSGao Xiang 			}
34347e4937aSGao Xiang 		}
34447e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
34547e4937aSGao Xiang 
3466aaa7b06SGao Xiang 		if (ret)
3476aaa7b06SGao Xiang 			detach_page_private(page);
34847e4937aSGao Xiang 	}
34947e4937aSGao Xiang 	return ret;
35047e4937aSGao Xiang }
35147e4937aSGao Xiang 
35247e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
35381382f5fSGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
35447e4937aSGao Xiang 				   struct page *page)
35547e4937aSGao Xiang {
35647e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = clt->pcl;
35747e4937aSGao Xiang 
35881382f5fSGao Xiang 	while (clt->icpage_ptr > pcl->compressed_pages)
35981382f5fSGao Xiang 		if (!cmpxchg(--clt->icpage_ptr, NULL, page))
36047e4937aSGao Xiang 			return true;
36147e4937aSGao Xiang 	return false;
36247e4937aSGao Xiang }
36347e4937aSGao Xiang 
36447e4937aSGao Xiang /* callers must be with collection lock held */
36547e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt,
36686432a6dSGao Xiang 			       struct page *page, enum z_erofs_page_type type,
36786432a6dSGao Xiang 			       bool pvec_safereuse)
36847e4937aSGao Xiang {
36947e4937aSGao Xiang 	int ret;
37047e4937aSGao Xiang 
37147e4937aSGao Xiang 	/* give priority for inplaceio */
37247e4937aSGao Xiang 	if (clt->mode >= COLLECT_PRIMARY &&
37347e4937aSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
37499634bf3SGao Xiang 	    z_erofs_try_inplace_io(clt, page))
37547e4937aSGao Xiang 		return 0;
37647e4937aSGao Xiang 
37786432a6dSGao Xiang 	ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
37886432a6dSGao Xiang 				      pvec_safereuse);
37947e4937aSGao Xiang 	clt->cl->vcnt += (unsigned int)ret;
38047e4937aSGao Xiang 	return ret ? 0 : -EAGAIN;
38147e4937aSGao Xiang }
38247e4937aSGao Xiang 
383473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
38447e4937aSGao Xiang {
385473e15b0SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
386473e15b0SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
38747e4937aSGao Xiang 
388473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
389473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
390473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
39147e4937aSGao Xiang 		*owned_head = &pcl->next;
392473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
393473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED;
394473e15b0SGao Xiang 		return;
395473e15b0SGao Xiang 	}
396473e15b0SGao Xiang 
39747e4937aSGao Xiang 	/*
398473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
399473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
40047e4937aSGao Xiang 	 */
40147e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
402473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
40347e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
404473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_HOOKED;
405473e15b0SGao Xiang 		clt->tailpcl = NULL;
406473e15b0SGao Xiang 		return;
40747e4937aSGao Xiang 	}
408473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
409473e15b0SGao Xiang 	clt->mode = COLLECT_PRIMARY;
41047e4937aSGao Xiang }
41147e4937aSGao Xiang 
4129e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
41347e4937aSGao Xiang 				     struct inode *inode,
41447e4937aSGao Xiang 				     struct erofs_map_blocks *map)
41547e4937aSGao Xiang {
41664094a04SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
41747e4937aSGao Xiang 	struct z_erofs_collection *cl;
41847e4937aSGao Xiang 	unsigned int length;
41947e4937aSGao Xiang 
42064094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
42147e4937aSGao Xiang 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
42247e4937aSGao Xiang 		DBG_BUGON(1);
4239e579fc1SGao Xiang 		return -EFSCORRUPTED;
42447e4937aSGao Xiang 	}
42547e4937aSGao Xiang 
42647e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
4278d8a09b0SGao Xiang 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
42847e4937aSGao Xiang 		DBG_BUGON(1);
4299e579fc1SGao Xiang 		return -EFSCORRUPTED;
43047e4937aSGao Xiang 	}
43147e4937aSGao Xiang 
43247e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
43347e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
43447e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
43547e4937aSGao Xiang 			DBG_BUGON(1);
4369e579fc1SGao Xiang 			return -EFSCORRUPTED;
43747e4937aSGao Xiang 		}
43847e4937aSGao Xiang 	} else {
43947e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
44047e4937aSGao Xiang 
44147e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
44247e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
44347e4937aSGao Xiang 
44447e4937aSGao Xiang 		while (llen > length &&
44547e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
44647e4937aSGao Xiang 			cpu_relax();
44747e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
44847e4937aSGao Xiang 		}
44947e4937aSGao Xiang 	}
45047e4937aSGao Xiang 	mutex_lock(&cl->lock);
45147e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
45247e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
45347e4937aSGao Xiang 		clt->tailpcl = pcl;
454473e15b0SGao Xiang 
455473e15b0SGao Xiang 	z_erofs_try_to_claim_pcluster(clt);
45647e4937aSGao Xiang 	clt->cl = cl;
4579e579fc1SGao Xiang 	return 0;
45847e4937aSGao Xiang }
45947e4937aSGao Xiang 
4609e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt,
46147e4937aSGao Xiang 				       struct inode *inode,
46247e4937aSGao Xiang 				       struct erofs_map_blocks *map)
46347e4937aSGao Xiang {
464cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
46547e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
46647e4937aSGao Xiang 	struct z_erofs_collection *cl;
46764094a04SGao Xiang 	struct erofs_workgroup *grp;
46847e4937aSGao Xiang 	int err;
46947e4937aSGao Xiang 
4708f899262SGao Xiang 	if (!(map->m_flags & EROFS_MAP_ENCODED)) {
4718f899262SGao Xiang 		DBG_BUGON(1);
4728f899262SGao Xiang 		return -EFSCORRUPTED;
4738f899262SGao Xiang 	}
4748f899262SGao Xiang 
4759f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
476cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
477cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
4789f6cc76eSGao Xiang 	if (IS_ERR(pcl))
4799f6cc76eSGao Xiang 		return PTR_ERR(pcl);
48047e4937aSGao Xiang 
48164094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
4828f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
48347e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
48447e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
48547e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
48647e4937aSGao Xiang 
48747e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
48847e4937aSGao Xiang 	pcl->next = clt->owned_head;
48947e4937aSGao Xiang 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
49047e4937aSGao Xiang 
49147e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
49247e4937aSGao Xiang 	cl->pageofs = map->m_la & ~PAGE_MASK;
49347e4937aSGao Xiang 
49447e4937aSGao Xiang 	/*
49547e4937aSGao Xiang 	 * lock all primary followed works before visible to others
49647e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
49747e4937aSGao Xiang 	 */
4989f6cc76eSGao Xiang 	mutex_init(&cl->lock);
49964094a04SGao Xiang 	DBG_BUGON(!mutex_trylock(&cl->lock));
50047e4937aSGao Xiang 
501cecf864dSYue Hu 	if (ztailpacking) {
502cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
503cecf864dSYue Hu 		pcl->pageofs_in = erofs_blkoff(map->m_pa);
504cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
505cecf864dSYue Hu 	} else {
506cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
507cecf864dSYue Hu 
50864094a04SGao Xiang 		grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
50964094a04SGao Xiang 		if (IS_ERR(grp)) {
51064094a04SGao Xiang 			err = PTR_ERR(grp);
51164094a04SGao Xiang 			goto err_out;
51264094a04SGao Xiang 		}
51364094a04SGao Xiang 
51464094a04SGao Xiang 		if (grp != &pcl->obj) {
515cecf864dSYue Hu 			clt->pcl = container_of(grp,
516cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
51764094a04SGao Xiang 			err = -EEXIST;
51864094a04SGao Xiang 			goto err_out;
51947e4937aSGao Xiang 		}
520cecf864dSYue Hu 	}
52147e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
52247e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
52347e4937aSGao Xiang 		clt->tailpcl = pcl;
52447e4937aSGao Xiang 	clt->owned_head = &pcl->next;
52547e4937aSGao Xiang 	clt->pcl = pcl;
52647e4937aSGao Xiang 	clt->cl = cl;
5279e579fc1SGao Xiang 	return 0;
52864094a04SGao Xiang 
52964094a04SGao Xiang err_out:
53064094a04SGao Xiang 	mutex_unlock(&cl->lock);
5319f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
53264094a04SGao Xiang 	return err;
53347e4937aSGao Xiang }
53447e4937aSGao Xiang 
53547e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt,
53647e4937aSGao Xiang 				   struct inode *inode,
53747e4937aSGao Xiang 				   struct erofs_map_blocks *map)
53847e4937aSGao Xiang {
53964094a04SGao Xiang 	struct erofs_workgroup *grp;
5409e579fc1SGao Xiang 	int ret;
54147e4937aSGao Xiang 
54247e4937aSGao Xiang 	DBG_BUGON(clt->cl);
54347e4937aSGao Xiang 
54447e4937aSGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
54547e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
54647e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
54747e4937aSGao Xiang 
548cecf864dSYue Hu 	if (map->m_flags & EROFS_MAP_META) {
549cecf864dSYue Hu 		if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
55047e4937aSGao Xiang 			DBG_BUGON(1);
551cecf864dSYue Hu 			return -EFSCORRUPTED;
552cecf864dSYue Hu 		}
553cecf864dSYue Hu 		goto tailpacking;
55447e4937aSGao Xiang 	}
55547e4937aSGao Xiang 
55664094a04SGao Xiang 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
55764094a04SGao Xiang 	if (grp) {
55864094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
55964094a04SGao Xiang 	} else {
560cecf864dSYue Hu tailpacking:
5619e579fc1SGao Xiang 		ret = z_erofs_register_collection(clt, inode, map);
56264094a04SGao Xiang 		if (!ret)
56364094a04SGao Xiang 			goto out;
56464094a04SGao Xiang 		if (ret != -EEXIST)
5659e579fc1SGao Xiang 			return ret;
56664094a04SGao Xiang 	}
56747e4937aSGao Xiang 
56864094a04SGao Xiang 	ret = z_erofs_lookup_collection(clt, inode, map);
56964094a04SGao Xiang 	if (ret) {
57064094a04SGao Xiang 		erofs_workgroup_put(&clt->pcl->obj);
57164094a04SGao Xiang 		return ret;
57264094a04SGao Xiang 	}
57364094a04SGao Xiang 
57464094a04SGao Xiang out:
57547e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
5769e579fc1SGao Xiang 				  clt->cl->pagevec, clt->cl->vcnt);
57781382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
578cecf864dSYue Hu 	clt->icpage_ptr = clt->pcl->compressed_pages +
579cecf864dSYue Hu 			z_erofs_pclusterpages(clt->pcl);
58047e4937aSGao Xiang 	return 0;
58147e4937aSGao Xiang }
58247e4937aSGao Xiang 
58347e4937aSGao Xiang /*
58447e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
58547e4937aSGao Xiang  * only after a RCU grace period.
58647e4937aSGao Xiang  */
58747e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
58847e4937aSGao Xiang {
58947e4937aSGao Xiang 	struct z_erofs_collection *const cl =
59047e4937aSGao Xiang 		container_of(head, struct z_erofs_collection, rcu);
59147e4937aSGao Xiang 
5929f6cc76eSGao Xiang 	z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
59347e4937aSGao Xiang 					   primary_collection));
59447e4937aSGao Xiang }
59547e4937aSGao Xiang 
59647e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
59747e4937aSGao Xiang {
59847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
59947e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
60047e4937aSGao Xiang 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
60147e4937aSGao Xiang 
60247e4937aSGao Xiang 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
60347e4937aSGao Xiang }
60447e4937aSGao Xiang 
60547e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl)
60647e4937aSGao Xiang {
60747e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
60847e4937aSGao Xiang 		container_of(cl, struct z_erofs_pcluster, primary_collection);
60947e4937aSGao Xiang 
61047e4937aSGao Xiang 	erofs_workgroup_put(&pcl->obj);
61147e4937aSGao Xiang }
61247e4937aSGao Xiang 
61347e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt)
61447e4937aSGao Xiang {
61547e4937aSGao Xiang 	struct z_erofs_collection *cl = clt->cl;
61647e4937aSGao Xiang 
61747e4937aSGao Xiang 	if (!cl)
61847e4937aSGao Xiang 		return false;
61947e4937aSGao Xiang 
62047e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
62147e4937aSGao Xiang 	mutex_unlock(&cl->lock);
62247e4937aSGao Xiang 
62347e4937aSGao Xiang 	/*
62447e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
62547e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
62647e4937aSGao Xiang 	 */
62747e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
62847e4937aSGao Xiang 		z_erofs_collection_put(cl);
62947e4937aSGao Xiang 
63047e4937aSGao Xiang 	clt->cl = NULL;
63147e4937aSGao Xiang 	return true;
63247e4937aSGao Xiang }
63347e4937aSGao Xiang 
63447e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
63547e4937aSGao Xiang 				       unsigned int cachestrategy,
63647e4937aSGao Xiang 				       erofs_off_t la)
63747e4937aSGao Xiang {
63847e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
63947e4937aSGao Xiang 		return false;
64047e4937aSGao Xiang 
64147e4937aSGao Xiang 	if (fe->backmost)
64247e4937aSGao Xiang 		return true;
64347e4937aSGao Xiang 
64447e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
64547e4937aSGao Xiang 		la < fe->headoffset;
64647e4937aSGao Xiang }
64747e4937aSGao Xiang 
64847e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
649eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
65047e4937aSGao Xiang {
65147e4937aSGao Xiang 	struct inode *const inode = fe->inode;
652bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
65347e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
65447e4937aSGao Xiang 	struct z_erofs_collector *const clt = &fe->clt;
65547e4937aSGao Xiang 	const loff_t offset = page_offset(page);
656dc76ea8cSGao Xiang 	bool tight = true;
65747e4937aSGao Xiang 
65847e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
65947e4937aSGao Xiang 	enum z_erofs_page_type page_type;
66047e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
66147e4937aSGao Xiang 	int err = 0;
66247e4937aSGao Xiang 
66347e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
66447e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
66547e4937aSGao Xiang 
66647e4937aSGao Xiang 	spiltted = 0;
66747e4937aSGao Xiang 	end = PAGE_SIZE;
66847e4937aSGao Xiang repeat:
66947e4937aSGao Xiang 	cur = end - 1;
67047e4937aSGao Xiang 
67147e4937aSGao Xiang 	/* lucky, within the range of the current map_blocks */
67247e4937aSGao Xiang 	if (offset + cur >= map->m_la &&
67347e4937aSGao Xiang 	    offset + cur < map->m_la + map->m_llen) {
67447e4937aSGao Xiang 		/* didn't get a valid collection previously (very rare) */
67547e4937aSGao Xiang 		if (!clt->cl)
67647e4937aSGao Xiang 			goto restart_now;
67747e4937aSGao Xiang 		goto hitted;
67847e4937aSGao Xiang 	}
67947e4937aSGao Xiang 
68047e4937aSGao Xiang 	/* go ahead the next map_blocks */
6814f761fa2SGao Xiang 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
68247e4937aSGao Xiang 
68347e4937aSGao Xiang 	if (z_erofs_collector_end(clt))
68447e4937aSGao Xiang 		fe->backmost = false;
68547e4937aSGao Xiang 
68647e4937aSGao Xiang 	map->m_la = offset + cur;
68747e4937aSGao Xiang 	map->m_llen = 0;
68847e4937aSGao Xiang 	err = z_erofs_map_blocks_iter(inode, map, 0);
6898d8a09b0SGao Xiang 	if (err)
69047e4937aSGao Xiang 		goto err_out;
69147e4937aSGao Xiang 
69247e4937aSGao Xiang restart_now:
6938d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
69447e4937aSGao Xiang 		goto hitted;
69547e4937aSGao Xiang 
69647e4937aSGao Xiang 	err = z_erofs_collector_begin(clt, inode, map);
6978d8a09b0SGao Xiang 	if (err)
69847e4937aSGao Xiang 		goto err_out;
69947e4937aSGao Xiang 
700cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(clt->pcl)) {
70109c54379SGao Xiang 		void *mp;
702cecf864dSYue Hu 
70309c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
70409c54379SGao Xiang 					erofs_blknr(map->m_pa), EROFS_NO_KMAP);
70509c54379SGao Xiang 		if (IS_ERR(mp)) {
70609c54379SGao Xiang 			err = PTR_ERR(mp);
707cecf864dSYue Hu 			erofs_err(inode->i_sb,
708cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
709cecf864dSYue Hu 			goto err_out;
710cecf864dSYue Hu 		}
71109c54379SGao Xiang 		get_page(fe->map.buf.page);
71209c54379SGao Xiang 		WRITE_ONCE(clt->pcl->compressed_pages[0], fe->map.buf.page);
713cecf864dSYue Hu 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
714cecf864dSYue Hu 	} else {
715cecf864dSYue Hu 		/* preload all compressed pages (can change mode if needed) */
716cecf864dSYue Hu 		if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy,
717cecf864dSYue Hu 					       map->m_la))
7181825c8d7SGao Xiang 			cache_strategy = TRYALLOC;
71947e4937aSGao Xiang 		else
72047e4937aSGao Xiang 			cache_strategy = DONTALLOC;
72147e4937aSGao Xiang 
7221825c8d7SGao Xiang 		preload_compressed_pages(clt, MNGD_MAPPING(sbi),
7231825c8d7SGao Xiang 					 cache_strategy, pagepool);
724cecf864dSYue Hu 	}
72547e4937aSGao Xiang 
72647e4937aSGao Xiang hitted:
727dc76ea8cSGao Xiang 	/*
728dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
729dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
730dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
731dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
732dc76ea8cSGao Xiang 	 */
733dc76ea8cSGao Xiang 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
734dc76ea8cSGao Xiang 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
735dc76ea8cSGao Xiang 
73647e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
7378d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
73847e4937aSGao Xiang 		zero_user_segment(page, cur, end);
73947e4937aSGao Xiang 		goto next_part;
74047e4937aSGao Xiang 	}
74147e4937aSGao Xiang 
74247e4937aSGao Xiang 	/* let's derive page type */
74347e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
74447e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
74547e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
74647e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
74747e4937aSGao Xiang 
74847e4937aSGao Xiang 	if (cur)
74947e4937aSGao Xiang 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
75047e4937aSGao Xiang 
75147e4937aSGao Xiang retry:
75286432a6dSGao Xiang 	err = z_erofs_attach_page(clt, page, page_type,
75386432a6dSGao Xiang 				  clt->mode >= COLLECT_PRIMARY_FOLLOWED);
7546aaa7b06SGao Xiang 	/* should allocate an additional short-lived page for pagevec */
75547e4937aSGao Xiang 	if (err == -EAGAIN) {
75647e4937aSGao Xiang 		struct page *const newpage =
757e3f78d5eSChao Yu 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
75847e4937aSGao Xiang 
7596aaa7b06SGao Xiang 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
76047e4937aSGao Xiang 		err = z_erofs_attach_page(clt, newpage,
76186432a6dSGao Xiang 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
7628d8a09b0SGao Xiang 		if (!err)
76347e4937aSGao Xiang 			goto retry;
76447e4937aSGao Xiang 	}
76547e4937aSGao Xiang 
7668d8a09b0SGao Xiang 	if (err)
76747e4937aSGao Xiang 		goto err_out;
76847e4937aSGao Xiang 
76947e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
77047e4937aSGao Xiang 
77147e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
77247e4937aSGao Xiang 
77347e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
77447e4937aSGao Xiang 	++spiltted;
77547e4937aSGao Xiang 	/* also update nr_pages */
77647e4937aSGao Xiang 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
77747e4937aSGao Xiang next_part:
77847e4937aSGao Xiang 	/* can be used for verification */
77947e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
78047e4937aSGao Xiang 
78147e4937aSGao Xiang 	end = cur;
78247e4937aSGao Xiang 	if (end > 0)
78347e4937aSGao Xiang 		goto repeat;
78447e4937aSGao Xiang 
78547e4937aSGao Xiang out:
78647e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
78747e4937aSGao Xiang 
7884f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
78947e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
79047e4937aSGao Xiang 	return err;
79147e4937aSGao Xiang 
79247e4937aSGao Xiang 	/* if some error occurred while processing this page */
79347e4937aSGao Xiang err_out:
79447e4937aSGao Xiang 	SetPageError(page);
79547e4937aSGao Xiang 	goto out;
79647e4937aSGao Xiang }
79747e4937aSGao Xiang 
79840452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
79940452ffcSHuang Jianan 				       unsigned int readahead_pages)
80040452ffcSHuang Jianan {
80140452ffcSHuang Jianan 	/* auto: enable for readpage, disable for readahead */
80240452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
80340452ffcSHuang Jianan 	    !readahead_pages)
80440452ffcSHuang Jianan 		return true;
80540452ffcSHuang Jianan 
80640452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
80740452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
80840452ffcSHuang Jianan 		return true;
80940452ffcSHuang Jianan 
81040452ffcSHuang Jianan 	return false;
81140452ffcSHuang Jianan }
81240452ffcSHuang Jianan 
813648f2de0SHuang Jianan static void z_erofs_decompressqueue_work(struct work_struct *work);
814a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
815a4b1fab1SGao Xiang 				       bool sync, int bios)
81647e4937aSGao Xiang {
81730048cdaSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
81830048cdaSHuang Jianan 
819a4b1fab1SGao Xiang 	/* wake up the caller thread for sync decompression */
820a4b1fab1SGao Xiang 	if (sync) {
82147e4937aSGao Xiang 		unsigned long flags;
82247e4937aSGao Xiang 
82347e4937aSGao Xiang 		spin_lock_irqsave(&io->u.wait.lock, flags);
82447e4937aSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
82547e4937aSGao Xiang 			wake_up_locked(&io->u.wait);
82647e4937aSGao Xiang 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
82747e4937aSGao Xiang 		return;
82847e4937aSGao Xiang 	}
82947e4937aSGao Xiang 
830648f2de0SHuang Jianan 	if (atomic_add_return(bios, &io->pending_bios))
831648f2de0SHuang Jianan 		return;
83230048cdaSHuang Jianan 	/* Use workqueue and sync decompression for atomic contexts only */
833648f2de0SHuang Jianan 	if (in_atomic() || irqs_disabled()) {
83447e4937aSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
83540452ffcSHuang Jianan 		/* enable sync decompression for readahead */
83640452ffcSHuang Jianan 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
83740452ffcSHuang Jianan 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
838648f2de0SHuang Jianan 		return;
839648f2de0SHuang Jianan 	}
840648f2de0SHuang Jianan 	z_erofs_decompressqueue_work(&io->u.work);
84147e4937aSGao Xiang }
84247e4937aSGao Xiang 
8436aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
8446aaa7b06SGao Xiang {
8456aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
8466aaa7b06SGao Xiang }
8476aaa7b06SGao Xiang 
8480c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
84947e4937aSGao Xiang {
850a4b1fab1SGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
851a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
85247e4937aSGao Xiang 	blk_status_t err = bio->bi_status;
85347e4937aSGao Xiang 	struct bio_vec *bvec;
85447e4937aSGao Xiang 	struct bvec_iter_all iter_all;
85547e4937aSGao Xiang 
85647e4937aSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
85747e4937aSGao Xiang 		struct page *page = bvec->bv_page;
85847e4937aSGao Xiang 
85947e4937aSGao Xiang 		DBG_BUGON(PageUptodate(page));
8606aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
86147e4937aSGao Xiang 
8628d8a09b0SGao Xiang 		if (err)
86347e4937aSGao Xiang 			SetPageError(page);
86447e4937aSGao Xiang 
865a4b1fab1SGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
866a4b1fab1SGao Xiang 			if (!err)
867a4b1fab1SGao Xiang 				SetPageUptodate(page);
86847e4937aSGao Xiang 			unlock_page(page);
86947e4937aSGao Xiang 		}
870a4b1fab1SGao Xiang 	}
871a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
87247e4937aSGao Xiang 	bio_put(bio);
87347e4937aSGao Xiang }
87447e4937aSGao Xiang 
87547e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
87647e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
877eaa9172aSGao Xiang 				       struct page **pagepool)
87847e4937aSGao Xiang {
87947e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
880cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
88147e4937aSGao Xiang 	struct z_erofs_pagevec_ctor ctor;
8829f6cc76eSGao Xiang 	unsigned int i, inputsize, outputsize, llen, nr_pages;
88347e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
88447e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
88547e4937aSGao Xiang 
88647e4937aSGao Xiang 	enum z_erofs_page_type page_type;
88747e4937aSGao Xiang 	bool overlapped, partial;
88847e4937aSGao Xiang 	struct z_erofs_collection *cl;
88947e4937aSGao Xiang 	int err;
89047e4937aSGao Xiang 
89147e4937aSGao Xiang 	might_sleep();
89247e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
89347e4937aSGao Xiang 	DBG_BUGON(!READ_ONCE(cl->nr_pages));
89447e4937aSGao Xiang 
89547e4937aSGao Xiang 	mutex_lock(&cl->lock);
89647e4937aSGao Xiang 	nr_pages = cl->nr_pages;
89747e4937aSGao Xiang 
8988d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
89947e4937aSGao Xiang 		pages = pages_onstack;
90047e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
90147e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
90247e4937aSGao Xiang 		pages = z_pagemap_global;
90347e4937aSGao Xiang 	} else {
90447e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
90547e4937aSGao Xiang 
90647e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
90747e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
90847e4937aSGao Xiang 
90947e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
91047e4937aSGao Xiang 				       gfp_flags);
91147e4937aSGao Xiang 
91247e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
9138d8a09b0SGao Xiang 		if (!pages) {
91447e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
91547e4937aSGao Xiang 			pages = z_pagemap_global;
91647e4937aSGao Xiang 		}
91747e4937aSGao Xiang 	}
91847e4937aSGao Xiang 
91947e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
92047e4937aSGao Xiang 		pages[i] = NULL;
92147e4937aSGao Xiang 
92247e4937aSGao Xiang 	err = 0;
92347e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
92447e4937aSGao Xiang 				  cl->pagevec, 0);
92547e4937aSGao Xiang 
92647e4937aSGao Xiang 	for (i = 0; i < cl->vcnt; ++i) {
92747e4937aSGao Xiang 		unsigned int pagenr;
92847e4937aSGao Xiang 
92947e4937aSGao Xiang 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
93047e4937aSGao Xiang 
93147e4937aSGao Xiang 		/* all pages in pagevec ought to be valid */
93247e4937aSGao Xiang 		DBG_BUGON(!page);
9336aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
93447e4937aSGao Xiang 
9356aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
93647e4937aSGao Xiang 			continue;
93747e4937aSGao Xiang 
93847e4937aSGao Xiang 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
93947e4937aSGao Xiang 			pagenr = 0;
94047e4937aSGao Xiang 		else
94147e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
94247e4937aSGao Xiang 
94347e4937aSGao Xiang 		DBG_BUGON(pagenr >= nr_pages);
94447e4937aSGao Xiang 
94547e4937aSGao Xiang 		/*
94647e4937aSGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
94747e4937aSGao Xiang 		 * so here erroring out one multiref page.
94847e4937aSGao Xiang 		 */
9498d8a09b0SGao Xiang 		if (pages[pagenr]) {
95047e4937aSGao Xiang 			DBG_BUGON(1);
95147e4937aSGao Xiang 			SetPageError(pages[pagenr]);
95247e4937aSGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
95347e4937aSGao Xiang 			err = -EFSCORRUPTED;
95447e4937aSGao Xiang 		}
95547e4937aSGao Xiang 		pages[pagenr] = page;
95647e4937aSGao Xiang 	}
95747e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&ctor, true);
95847e4937aSGao Xiang 
95947e4937aSGao Xiang 	overlapped = false;
96047e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
96147e4937aSGao Xiang 
962cecf864dSYue Hu 	for (i = 0; i < pclusterpages; ++i) {
96347e4937aSGao Xiang 		unsigned int pagenr;
96447e4937aSGao Xiang 
96547e4937aSGao Xiang 		page = compressed_pages[i];
96647e4937aSGao Xiang 		/* all compressed pages ought to be valid */
96747e4937aSGao Xiang 		DBG_BUGON(!page);
96847e4937aSGao Xiang 
969cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
970cecf864dSYue Hu 			if (!PageUptodate(page))
971cecf864dSYue Hu 				err = -EIO;
972cecf864dSYue Hu 			continue;
973cecf864dSYue Hu 		}
974cecf864dSYue Hu 
975cecf864dSYue Hu 		DBG_BUGON(z_erofs_page_is_invalidated(page));
9766aaa7b06SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
97747e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
9788d8a09b0SGao Xiang 				if (!PageUptodate(page))
97947e4937aSGao Xiang 					err = -EIO;
98047e4937aSGao Xiang 				continue;
98147e4937aSGao Xiang 			}
98247e4937aSGao Xiang 
98347e4937aSGao Xiang 			/*
98447e4937aSGao Xiang 			 * only if non-head page can be selected
98547e4937aSGao Xiang 			 * for inplace decompression
98647e4937aSGao Xiang 			 */
98747e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
98847e4937aSGao Xiang 
98947e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
9908d8a09b0SGao Xiang 			if (pages[pagenr]) {
99147e4937aSGao Xiang 				DBG_BUGON(1);
99247e4937aSGao Xiang 				SetPageError(pages[pagenr]);
99347e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
99447e4937aSGao Xiang 				err = -EFSCORRUPTED;
99547e4937aSGao Xiang 			}
99647e4937aSGao Xiang 			pages[pagenr] = page;
99747e4937aSGao Xiang 
99847e4937aSGao Xiang 			overlapped = true;
99947e4937aSGao Xiang 		}
100047e4937aSGao Xiang 
10016aaa7b06SGao Xiang 		/* PG_error needs checking for all non-managed pages */
10028d8a09b0SGao Xiang 		if (PageError(page)) {
100347e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
100447e4937aSGao Xiang 			err = -EIO;
100547e4937aSGao Xiang 		}
100647e4937aSGao Xiang 	}
100747e4937aSGao Xiang 
10088d8a09b0SGao Xiang 	if (err)
100947e4937aSGao Xiang 		goto out;
101047e4937aSGao Xiang 
101147e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
101247e4937aSGao Xiang 	if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
101347e4937aSGao Xiang 		outputsize = llen;
101447e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
101547e4937aSGao Xiang 	} else {
101647e4937aSGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
101747e4937aSGao Xiang 		partial = true;
101847e4937aSGao Xiang 	}
101947e4937aSGao Xiang 
1020cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1021cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1022cecf864dSYue Hu 	else
1023cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1024cecf864dSYue Hu 
102547e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
102647e4937aSGao Xiang 					.sb = sb,
102747e4937aSGao Xiang 					.in = compressed_pages,
102847e4937aSGao Xiang 					.out = pages,
1029cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
103047e4937aSGao Xiang 					.pageofs_out = cl->pageofs,
10319f6cc76eSGao Xiang 					.inputsize = inputsize,
103247e4937aSGao Xiang 					.outputsize = outputsize,
103347e4937aSGao Xiang 					.alg = pcl->algorithmformat,
103447e4937aSGao Xiang 					.inplace_io = overlapped,
103547e4937aSGao Xiang 					.partial_decoding = partial
103647e4937aSGao Xiang 				 }, pagepool);
103747e4937aSGao Xiang 
103847e4937aSGao Xiang out:
1039cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1040cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1041cecf864dSYue Hu 		page = compressed_pages[0];
1042cecf864dSYue Hu 		WRITE_ONCE(compressed_pages[0], NULL);
1043cecf864dSYue Hu 		put_page(page);
1044cecf864dSYue Hu 	} else {
1045cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
104647e4937aSGao Xiang 			page = compressed_pages[i];
104747e4937aSGao Xiang 
104847e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
104947e4937aSGao Xiang 				continue;
105047e4937aSGao Xiang 
10516aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
10526aaa7b06SGao Xiang 			(void)z_erofs_put_shortlivedpage(pagepool, page);
105347e4937aSGao Xiang 			WRITE_ONCE(compressed_pages[i], NULL);
105447e4937aSGao Xiang 		}
1055cecf864dSYue Hu 	}
105647e4937aSGao Xiang 
105747e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
105847e4937aSGao Xiang 		page = pages[i];
105947e4937aSGao Xiang 		if (!page)
106047e4937aSGao Xiang 			continue;
106147e4937aSGao Xiang 
10626aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
106347e4937aSGao Xiang 
10646aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
10656aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
106647e4937aSGao Xiang 			continue;
106747e4937aSGao Xiang 
10688d8a09b0SGao Xiang 		if (err < 0)
106947e4937aSGao Xiang 			SetPageError(page);
107047e4937aSGao Xiang 
107147e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
107247e4937aSGao Xiang 	}
107347e4937aSGao Xiang 
107447e4937aSGao Xiang 	if (pages == z_pagemap_global)
107547e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
10768d8a09b0SGao Xiang 	else if (pages != pages_onstack)
107747e4937aSGao Xiang 		kvfree(pages);
107847e4937aSGao Xiang 
107947e4937aSGao Xiang 	cl->nr_pages = 0;
108047e4937aSGao Xiang 	cl->vcnt = 0;
108147e4937aSGao Xiang 
108247e4937aSGao Xiang 	/* all cl locks MUST be taken before the following line */
108347e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
108447e4937aSGao Xiang 
108547e4937aSGao Xiang 	/* all cl locks SHOULD be released right now */
108647e4937aSGao Xiang 	mutex_unlock(&cl->lock);
108747e4937aSGao Xiang 
108847e4937aSGao Xiang 	z_erofs_collection_put(cl);
108947e4937aSGao Xiang 	return err;
109047e4937aSGao Xiang }
109147e4937aSGao Xiang 
10920c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1093eaa9172aSGao Xiang 				     struct page **pagepool)
109447e4937aSGao Xiang {
109547e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
109647e4937aSGao Xiang 
109747e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
109847e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
109947e4937aSGao Xiang 
110047e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
110147e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
110247e4937aSGao Xiang 
110347e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
110447e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
110547e4937aSGao Xiang 
110647e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
110747e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
110847e4937aSGao Xiang 
1109a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
111047e4937aSGao Xiang 	}
111147e4937aSGao Xiang }
111247e4937aSGao Xiang 
11130c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
111447e4937aSGao Xiang {
1115a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1116a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1117eaa9172aSGao Xiang 	struct page *pagepool = NULL;
111847e4937aSGao Xiang 
1119a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
11200c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
112147e4937aSGao Xiang 
1122eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1123a4b1fab1SGao Xiang 	kvfree(bgq);
112447e4937aSGao Xiang }
112547e4937aSGao Xiang 
112647e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
112747e4937aSGao Xiang 					       unsigned int nr,
1128eaa9172aSGao Xiang 					       struct page **pagepool,
112947e4937aSGao Xiang 					       struct address_space *mc,
113047e4937aSGao Xiang 					       gfp_t gfp)
113147e4937aSGao Xiang {
113247e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
113347e4937aSGao Xiang 	bool tocache = false;
113447e4937aSGao Xiang 
113547e4937aSGao Xiang 	struct address_space *mapping;
113647e4937aSGao Xiang 	struct page *oldpage, *page;
113747e4937aSGao Xiang 
113847e4937aSGao Xiang 	compressed_page_t t;
113947e4937aSGao Xiang 	int justfound;
114047e4937aSGao Xiang 
114147e4937aSGao Xiang repeat:
114247e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
114347e4937aSGao Xiang 	oldpage = page;
114447e4937aSGao Xiang 
114547e4937aSGao Xiang 	if (!page)
114647e4937aSGao Xiang 		goto out_allocpage;
114747e4937aSGao Xiang 
114847e4937aSGao Xiang 	/* process the target tagged pointer */
114947e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
115047e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
115147e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
115247e4937aSGao Xiang 
11531825c8d7SGao Xiang 	/*
11541825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
11551825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
11561825c8d7SGao Xiang 	 */
11571825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
11581825c8d7SGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
11591825c8d7SGao Xiang 		set_page_private(page, 0);
11601825c8d7SGao Xiang 		tocache = true;
11611825c8d7SGao Xiang 		goto out_tocache;
11621825c8d7SGao Xiang 	}
116347e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
116447e4937aSGao Xiang 
116547e4937aSGao Xiang 	/*
11666aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
116747e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
116847e4937aSGao Xiang 	 */
116947e4937aSGao Xiang 	if (mapping && mapping != mc)
117047e4937aSGao Xiang 		/* ought to be unmanaged pages */
117147e4937aSGao Xiang 		goto out;
117247e4937aSGao Xiang 
11736aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
11746aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
11756aaa7b06SGao Xiang 		goto out;
11766aaa7b06SGao Xiang 
117747e4937aSGao Xiang 	lock_page(page);
117847e4937aSGao Xiang 
117947e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
118047e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
118147e4937aSGao Xiang 
118247e4937aSGao Xiang 	/* the page is still in manage cache */
118347e4937aSGao Xiang 	if (page->mapping == mc) {
118447e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
118547e4937aSGao Xiang 
118647e4937aSGao Xiang 		ClearPageError(page);
118747e4937aSGao Xiang 		if (!PagePrivate(page)) {
118847e4937aSGao Xiang 			/*
118947e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
119047e4937aSGao Xiang 			 * the current restriction as well if
119147e4937aSGao Xiang 			 * the page is already in compressed_pages[].
119247e4937aSGao Xiang 			 */
119347e4937aSGao Xiang 			DBG_BUGON(!justfound);
119447e4937aSGao Xiang 
119547e4937aSGao Xiang 			justfound = 0;
119647e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
119747e4937aSGao Xiang 			SetPagePrivate(page);
119847e4937aSGao Xiang 		}
119947e4937aSGao Xiang 
120047e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
120147e4937aSGao Xiang 		if (PageUptodate(page)) {
120247e4937aSGao Xiang 			unlock_page(page);
120347e4937aSGao Xiang 			page = NULL;
120447e4937aSGao Xiang 		}
120547e4937aSGao Xiang 		goto out;
120647e4937aSGao Xiang 	}
120747e4937aSGao Xiang 
120847e4937aSGao Xiang 	/*
120947e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
121047e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
121147e4937aSGao Xiang 	 */
121247e4937aSGao Xiang 	DBG_BUGON(page->mapping);
121347e4937aSGao Xiang 	DBG_BUGON(!justfound);
121447e4937aSGao Xiang 
121547e4937aSGao Xiang 	tocache = true;
121647e4937aSGao Xiang 	unlock_page(page);
121747e4937aSGao Xiang 	put_page(page);
121847e4937aSGao Xiang out_allocpage:
12195ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
12205ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1221eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
12225ddcee1fSGao Xiang 		cond_resched();
12235ddcee1fSGao Xiang 		goto repeat;
12245ddcee1fSGao Xiang 	}
12251825c8d7SGao Xiang out_tocache:
1226bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1227bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1228bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1229bf225074SGao Xiang 		goto out;
1230a30573b3SGao Xiang 	}
1231bf225074SGao Xiang 	attach_page_private(page, pcl);
1232bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1233bf225074SGao Xiang 	put_page(page);
1234bf225074SGao Xiang 
123547e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
123647e4937aSGao Xiang 	return page;
123747e4937aSGao Xiang }
123847e4937aSGao Xiang 
1239a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1240a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1241a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
124247e4937aSGao Xiang {
1243a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
124447e4937aSGao Xiang 
1245a4b1fab1SGao Xiang 	if (fg && !*fg) {
1246a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1247a4b1fab1SGao Xiang 		if (!q) {
1248a4b1fab1SGao Xiang 			*fg = true;
1249a4b1fab1SGao Xiang 			goto fg_out;
125047e4937aSGao Xiang 		}
12510c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1252a4b1fab1SGao Xiang 	} else {
1253a4b1fab1SGao Xiang fg_out:
1254a4b1fab1SGao Xiang 		q = fgq;
1255a4b1fab1SGao Xiang 		init_waitqueue_head(&fgq->u.wait);
1256a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1257a4b1fab1SGao Xiang 	}
1258a4b1fab1SGao Xiang 	q->sb = sb;
1259a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1260a4b1fab1SGao Xiang 	return q;
126147e4937aSGao Xiang }
126247e4937aSGao Xiang 
126347e4937aSGao Xiang /* define decompression jobqueue types */
126447e4937aSGao Xiang enum {
126547e4937aSGao Xiang 	JQ_BYPASS,
126647e4937aSGao Xiang 	JQ_SUBMIT,
126747e4937aSGao Xiang 	NR_JOBQUEUES,
126847e4937aSGao Xiang };
126947e4937aSGao Xiang 
127047e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1271a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1272a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
127347e4937aSGao Xiang {
127447e4937aSGao Xiang 	/*
127547e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
127647e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
127747e4937aSGao Xiang 	 */
1278a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1279a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
128047e4937aSGao Xiang 
1281a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
128247e4937aSGao Xiang }
128347e4937aSGao Xiang 
128447e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
128547e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
128647e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
128747e4937aSGao Xiang {
128847e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
128947e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
129047e4937aSGao Xiang 
129147e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
129247e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
129347e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
129447e4937aSGao Xiang 
129547e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
129647e4937aSGao Xiang 
129747e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
129847e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
129947e4937aSGao Xiang 
130047e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
130147e4937aSGao Xiang }
130247e4937aSGao Xiang 
13031e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb,
13046ea5aad3SGao Xiang 				 struct z_erofs_decompress_frontend *f,
1305eaa9172aSGao Xiang 				 struct page **pagepool,
1306a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1307a4b1fab1SGao Xiang 				 bool *force_fg)
130847e4937aSGao Xiang {
1309bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
131047e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1311a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
131247e4937aSGao Xiang 	void *bi_private;
13136ea5aad3SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1314dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
13153f649ab7SKees Cook 	pgoff_t last_index;
1316dfeab2e9SGao Xiang 	struct block_device *last_bdev;
13171e4a2955SGao Xiang 	unsigned int nr_bios = 0;
13181e4a2955SGao Xiang 	struct bio *bio = NULL;
131947e4937aSGao Xiang 
1320a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1321a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1322a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
132347e4937aSGao Xiang 
132447e4937aSGao Xiang 	/* by default, all need io submission */
132547e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
132647e4937aSGao Xiang 
132747e4937aSGao Xiang 	do {
1328dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
132947e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
13301e4a2955SGao Xiang 		pgoff_t cur, end;
13311e4a2955SGao Xiang 		unsigned int i = 0;
13321e4a2955SGao Xiang 		bool bypass = true;
133347e4937aSGao Xiang 
133447e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
133547e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
133647e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
133747e4937aSGao Xiang 
133847e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
133947e4937aSGao Xiang 
1340cecf864dSYue Hu 		/* close the main owned chain at first */
1341cecf864dSYue Hu 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1342cecf864dSYue Hu 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1343cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1344cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1345cecf864dSYue Hu 			continue;
1346cecf864dSYue Hu 		}
1347cecf864dSYue Hu 
1348dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1349dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
1350dfeab2e9SGao Xiang 			.m_pa = blknr_to_addr(pcl->obj.index),
1351dfeab2e9SGao Xiang 		};
1352dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1353dfeab2e9SGao Xiang 
1354dfeab2e9SGao Xiang 		cur = erofs_blknr(mdev.m_pa);
13559f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
135647e4937aSGao Xiang 
13571e4a2955SGao Xiang 		do {
13581e4a2955SGao Xiang 			struct page *page;
135947e4937aSGao Xiang 
13601e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
136147e4937aSGao Xiang 							  MNGD_MAPPING(sbi),
136247e4937aSGao Xiang 							  GFP_NOFS);
13631e4a2955SGao Xiang 			if (!page)
13641e4a2955SGao Xiang 				continue;
136547e4937aSGao Xiang 
1366dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1367dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
136847e4937aSGao Xiang submit_bio_retry:
136994e4e153SGao Xiang 				submit_bio(bio);
137047e4937aSGao Xiang 				bio = NULL;
137147e4937aSGao Xiang 			}
137247e4937aSGao Xiang 
137347e4937aSGao Xiang 			if (!bio) {
1374*07888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1375*07888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
13760c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1377dfeab2e9SGao Xiang 
1378dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
13791e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1380a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1381a5c0b780SGao Xiang 				bio->bi_private = bi_private;
13826ea5aad3SGao Xiang 				if (f->readahead)
13836ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
138447e4937aSGao Xiang 				++nr_bios;
138547e4937aSGao Xiang 			}
138647e4937aSGao Xiang 
13876c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
138847e4937aSGao Xiang 				goto submit_bio_retry;
138947e4937aSGao Xiang 
13901e4a2955SGao Xiang 			last_index = cur;
13911e4a2955SGao Xiang 			bypass = false;
13921e4a2955SGao Xiang 		} while (++cur < end);
139347e4937aSGao Xiang 
13941e4a2955SGao Xiang 		if (!bypass)
139547e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
139647e4937aSGao Xiang 		else
139747e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
139847e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
139947e4937aSGao Xiang 
140047e4937aSGao Xiang 	if (bio)
140194e4e153SGao Xiang 		submit_bio(bio);
140247e4937aSGao Xiang 
1403587a67b7SGao Xiang 	/*
1404587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1405587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1406587a67b7SGao Xiang 	 */
1407587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1408587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
14091e4a2955SGao Xiang 		return;
1410587a67b7SGao Xiang 	}
1411a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
141247e4937aSGao Xiang }
141347e4937aSGao Xiang 
14140c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb,
14156ea5aad3SGao Xiang 			     struct z_erofs_decompress_frontend *f,
1416eaa9172aSGao Xiang 			     struct page **pagepool, bool force_fg)
141747e4937aSGao Xiang {
1418a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
141947e4937aSGao Xiang 
14206ea5aad3SGao Xiang 	if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
142147e4937aSGao Xiang 		return;
14226ea5aad3SGao Xiang 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
142347e4937aSGao Xiang 
14240c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
14250c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
142647e4937aSGao Xiang 
142747e4937aSGao Xiang 	if (!force_fg)
142847e4937aSGao Xiang 		return;
142947e4937aSGao Xiang 
143047e4937aSGao Xiang 	/* wait until all bios are completed */
1431a93f8c36SGao Xiang 	io_wait_event(io[JQ_SUBMIT].u.wait,
143247e4937aSGao Xiang 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
143347e4937aSGao Xiang 
14340c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
14350c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
143647e4937aSGao Xiang }
143747e4937aSGao Xiang 
143838629291SGao Xiang /*
143938629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
144038629291SGao Xiang  * approximate readmore strategies as a start.
144138629291SGao Xiang  */
144238629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
144338629291SGao Xiang 				      struct readahead_control *rac,
144438629291SGao Xiang 				      erofs_off_t end,
1445eaa9172aSGao Xiang 				      struct page **pagepool,
144638629291SGao Xiang 				      bool backmost)
144738629291SGao Xiang {
144838629291SGao Xiang 	struct inode *inode = f->inode;
144938629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
145038629291SGao Xiang 	erofs_off_t cur;
145138629291SGao Xiang 	int err;
145238629291SGao Xiang 
145338629291SGao Xiang 	if (backmost) {
145438629291SGao Xiang 		map->m_la = end;
1455622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1456622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
145738629291SGao Xiang 		if (err)
145838629291SGao Xiang 			return;
145938629291SGao Xiang 
146038629291SGao Xiang 		/* expend ra for the trailing edge if readahead */
146138629291SGao Xiang 		if (rac) {
146238629291SGao Xiang 			loff_t newstart = readahead_pos(rac);
146338629291SGao Xiang 
146438629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
146538629291SGao Xiang 			readahead_expand(rac, newstart, cur - newstart);
146638629291SGao Xiang 			return;
146738629291SGao Xiang 		}
146838629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
146938629291SGao Xiang 	} else {
147038629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
147138629291SGao Xiang 
147238629291SGao Xiang 		if (!map->m_llen)
147338629291SGao Xiang 			return;
147438629291SGao Xiang 	}
147538629291SGao Xiang 
147638629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
147738629291SGao Xiang 	while (cur >= end) {
147838629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
147938629291SGao Xiang 		struct page *page;
148038629291SGao Xiang 
148138629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
148238629291SGao Xiang 		if (!page)
148338629291SGao Xiang 			goto skip;
148438629291SGao Xiang 
148538629291SGao Xiang 		if (PageUptodate(page)) {
148638629291SGao Xiang 			unlock_page(page);
148738629291SGao Xiang 			put_page(page);
148838629291SGao Xiang 			goto skip;
148938629291SGao Xiang 		}
149038629291SGao Xiang 
149138629291SGao Xiang 		err = z_erofs_do_read_page(f, page, pagepool);
149238629291SGao Xiang 		if (err)
149338629291SGao Xiang 			erofs_err(inode->i_sb,
149438629291SGao Xiang 				  "readmore error at page %lu @ nid %llu",
149538629291SGao Xiang 				  index, EROFS_I(inode)->nid);
149638629291SGao Xiang 		put_page(page);
149738629291SGao Xiang skip:
149838629291SGao Xiang 		if (cur < PAGE_SIZE)
149938629291SGao Xiang 			break;
150038629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
150138629291SGao Xiang 	}
150238629291SGao Xiang }
150338629291SGao Xiang 
15040c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page)
150547e4937aSGao Xiang {
150647e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
150740452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
150847e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1509eaa9172aSGao Xiang 	struct page *pagepool = NULL;
151047e4937aSGao Xiang 	int err;
151147e4937aSGao Xiang 
151247e4937aSGao Xiang 	trace_erofs_readpage(page, false);
151347e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
151447e4937aSGao Xiang 
151538629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
151638629291SGao Xiang 				  &pagepool, true);
15171825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
151838629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
151938629291SGao Xiang 
152047e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
152147e4937aSGao Xiang 
152247e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
152340452ffcSHuang Jianan 	z_erofs_runqueue(inode->i_sb, &f, &pagepool,
152440452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, 0));
152547e4937aSGao Xiang 
152647e4937aSGao Xiang 	if (err)
15274f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
152847e4937aSGao Xiang 
152909c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1530eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
153147e4937aSGao Xiang 	return err;
153247e4937aSGao Xiang }
153347e4937aSGao Xiang 
15340615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
153547e4937aSGao Xiang {
15360615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
153747e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
153847e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1539eaa9172aSGao Xiang 	struct page *pagepool = NULL, *head = NULL, *page;
154038629291SGao Xiang 	unsigned int nr_pages;
154147e4937aSGao Xiang 
15426ea5aad3SGao Xiang 	f.readahead = true;
15430615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
154447e4937aSGao Xiang 
154538629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, f.headoffset +
154638629291SGao Xiang 				  readahead_length(rac) - 1, &pagepool, true);
154738629291SGao Xiang 	nr_pages = readahead_count(rac);
154838629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
154938629291SGao Xiang 
15500615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
155147e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
155247e4937aSGao Xiang 		head = page;
155347e4937aSGao Xiang 	}
155447e4937aSGao Xiang 
155547e4937aSGao Xiang 	while (head) {
155647e4937aSGao Xiang 		struct page *page = head;
155747e4937aSGao Xiang 		int err;
155847e4937aSGao Xiang 
155947e4937aSGao Xiang 		/* traversal in reverse order */
156047e4937aSGao Xiang 		head = (void *)page_private(page);
156147e4937aSGao Xiang 
15621825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1563a5876e24SGao Xiang 		if (err)
15644f761fa2SGao Xiang 			erofs_err(inode->i_sb,
15654f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
15664f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
156747e4937aSGao Xiang 		put_page(page);
156847e4937aSGao Xiang 	}
156938629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
157047e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
157147e4937aSGao Xiang 
157238629291SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool,
157340452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
157409c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1575eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
157647e4937aSGao Xiang }
157747e4937aSGao Xiang 
15780c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
15790c638f70SGao Xiang 	.readpage = z_erofs_readpage,
15800615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
158147e4937aSGao Xiang };
1582