xref: /openbmc/linux/fs/erofs/zdata.c (revision b1ed220c)
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/
506a304cdSGao Xiang  * Copyright (C) 2022 Alibaba Cloud
647e4937aSGao Xiang  */
747e4937aSGao Xiang #include "zdata.h"
847e4937aSGao Xiang #include "compress.h"
947e4937aSGao Xiang #include <linux/prefetch.h>
1099486c51SChristoph Hellwig #include <linux/psi.h>
1147e4937aSGao Xiang 
1247e4937aSGao Xiang #include <trace/events/erofs.h>
1347e4937aSGao Xiang 
1447e4937aSGao Xiang /*
159f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
169f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
179f6cc76eSGao Xiang  */
189f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
199f6cc76eSGao Xiang 	struct kmem_cache *slab;
209f6cc76eSGao Xiang 	unsigned int maxpages;
219f6cc76eSGao Xiang 	char name[48];
229f6cc76eSGao Xiang };
239f6cc76eSGao Xiang 
249f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
259f6cc76eSGao Xiang 
269f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
279f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
289f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
299f6cc76eSGao Xiang };
309f6cc76eSGao Xiang 
3106a304cdSGao Xiang struct z_erofs_bvec_iter {
3206a304cdSGao Xiang 	struct page *bvpage;
3306a304cdSGao Xiang 	struct z_erofs_bvset *bvset;
3406a304cdSGao Xiang 	unsigned int nr, cur;
3506a304cdSGao Xiang };
3606a304cdSGao Xiang 
3706a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
3806a304cdSGao Xiang {
3906a304cdSGao Xiang 	if (iter->bvpage)
4006a304cdSGao Xiang 		kunmap_local(iter->bvset);
4106a304cdSGao Xiang 	return iter->bvpage;
4206a304cdSGao Xiang }
4306a304cdSGao Xiang 
4406a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
4506a304cdSGao Xiang {
4606a304cdSGao Xiang 	unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
4706a304cdSGao Xiang 	/* have to access nextpage in advance, otherwise it will be unmapped */
4806a304cdSGao Xiang 	struct page *nextpage = iter->bvset->nextpage;
4906a304cdSGao Xiang 	struct page *oldpage;
5006a304cdSGao Xiang 
5106a304cdSGao Xiang 	DBG_BUGON(!nextpage);
5206a304cdSGao Xiang 	oldpage = z_erofs_bvec_iter_end(iter);
5306a304cdSGao Xiang 	iter->bvpage = nextpage;
5406a304cdSGao Xiang 	iter->bvset = kmap_local_page(nextpage);
5506a304cdSGao Xiang 	iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
5606a304cdSGao Xiang 	iter->cur = 0;
5706a304cdSGao Xiang 	return oldpage;
5806a304cdSGao Xiang }
5906a304cdSGao Xiang 
6006a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
6106a304cdSGao Xiang 				    struct z_erofs_bvset_inline *bvset,
6206a304cdSGao Xiang 				    unsigned int bootstrap_nr,
6306a304cdSGao Xiang 				    unsigned int cur)
6406a304cdSGao Xiang {
6506a304cdSGao Xiang 	*iter = (struct z_erofs_bvec_iter) {
6606a304cdSGao Xiang 		.nr = bootstrap_nr,
6706a304cdSGao Xiang 		.bvset = (struct z_erofs_bvset *)bvset,
6806a304cdSGao Xiang 	};
6906a304cdSGao Xiang 
7006a304cdSGao Xiang 	while (cur > iter->nr) {
7106a304cdSGao Xiang 		cur -= iter->nr;
7206a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
7306a304cdSGao Xiang 	}
7406a304cdSGao Xiang 	iter->cur = cur;
7506a304cdSGao Xiang }
7606a304cdSGao Xiang 
7706a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
7806a304cdSGao Xiang 				struct z_erofs_bvec *bvec,
7906a304cdSGao Xiang 				struct page **candidate_bvpage)
8006a304cdSGao Xiang {
8106a304cdSGao Xiang 	if (iter->cur == iter->nr) {
8206a304cdSGao Xiang 		if (!*candidate_bvpage)
8306a304cdSGao Xiang 			return -EAGAIN;
8406a304cdSGao Xiang 
8506a304cdSGao Xiang 		DBG_BUGON(iter->bvset->nextpage);
8606a304cdSGao Xiang 		iter->bvset->nextpage = *candidate_bvpage;
8706a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
8806a304cdSGao Xiang 
8906a304cdSGao Xiang 		iter->bvset->nextpage = NULL;
9006a304cdSGao Xiang 		*candidate_bvpage = NULL;
9106a304cdSGao Xiang 	}
9206a304cdSGao Xiang 	iter->bvset->bvec[iter->cur++] = *bvec;
9306a304cdSGao Xiang 	return 0;
9406a304cdSGao Xiang }
9506a304cdSGao Xiang 
9606a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
9706a304cdSGao Xiang 				 struct z_erofs_bvec *bvec,
9806a304cdSGao Xiang 				 struct page **old_bvpage)
9906a304cdSGao Xiang {
10006a304cdSGao Xiang 	if (iter->cur == iter->nr)
10106a304cdSGao Xiang 		*old_bvpage = z_erofs_bvset_flip(iter);
10206a304cdSGao Xiang 	else
10306a304cdSGao Xiang 		*old_bvpage = NULL;
10406a304cdSGao Xiang 	*bvec = iter->bvset->bvec[iter->cur++];
10506a304cdSGao Xiang }
10606a304cdSGao Xiang 
1079f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
1089f6cc76eSGao Xiang {
1099f6cc76eSGao Xiang 	int i;
1109f6cc76eSGao Xiang 
1119f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
1129f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
1139f6cc76eSGao Xiang 			continue;
1149f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
1159f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
1169f6cc76eSGao Xiang 	}
1179f6cc76eSGao Xiang }
1189f6cc76eSGao Xiang 
1199f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
1209f6cc76eSGao Xiang {
1219f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
1229f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
1239f6cc76eSGao Xiang 	unsigned int size;
1249f6cc76eSGao Xiang 
1259f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
1269f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
127ed722fbcSGao Xiang 		size = struct_size(a, compressed_bvecs, pcs->maxpages);
1289f6cc76eSGao Xiang 
1299f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
1309f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
1319f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
1329f6cc76eSGao Xiang 		if (pcs->slab)
1339f6cc76eSGao Xiang 			continue;
1349f6cc76eSGao Xiang 
1359f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
1369f6cc76eSGao Xiang 		return -ENOMEM;
1379f6cc76eSGao Xiang 	}
1389f6cc76eSGao Xiang 	return 0;
1399f6cc76eSGao Xiang }
1409f6cc76eSGao Xiang 
1419f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
1429f6cc76eSGao Xiang {
1439f6cc76eSGao Xiang 	int i;
1449f6cc76eSGao Xiang 
1459f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
1469f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
1479f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
1489f6cc76eSGao Xiang 
1499f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
1509f6cc76eSGao Xiang 			continue;
1519f6cc76eSGao Xiang 
1529f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
1539f6cc76eSGao Xiang 		if (!pcl)
1549f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
1559f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
1569f6cc76eSGao Xiang 		return pcl;
1579f6cc76eSGao Xiang 	}
1589f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
1599f6cc76eSGao Xiang }
1609f6cc76eSGao Xiang 
1619f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
1629f6cc76eSGao Xiang {
163cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1649f6cc76eSGao Xiang 	int i;
1659f6cc76eSGao Xiang 
1669f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
1679f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
1689f6cc76eSGao Xiang 
169cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
1709f6cc76eSGao Xiang 			continue;
1719f6cc76eSGao Xiang 
1729f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
1739f6cc76eSGao Xiang 		return;
1749f6cc76eSGao Xiang 	}
1759f6cc76eSGao Xiang 	DBG_BUGON(1);
1769f6cc76eSGao Xiang }
1779f6cc76eSGao Xiang 
17847e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
17947e4937aSGao Xiang 
18047e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
18147e4937aSGao Xiang {
18247e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
1839f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
18447e4937aSGao Xiang }
18547e4937aSGao Xiang 
18699634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
18747e4937aSGao Xiang {
18847e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
18947e4937aSGao Xiang 
19047e4937aSGao Xiang 	/*
19147e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
19247e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
19347e4937aSGao Xiang 	 */
1940e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
1950e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
19647e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
19747e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
19847e4937aSGao Xiang }
19947e4937aSGao Xiang 
20047e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
20147e4937aSGao Xiang {
2029f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
20347e4937aSGao Xiang 
2049f6cc76eSGao Xiang 	if (err)
2059f6cc76eSGao Xiang 		return err;
2069f6cc76eSGao Xiang 	err = z_erofs_init_workqueue();
2079f6cc76eSGao Xiang 	if (err)
2089f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
2099f6cc76eSGao Xiang 	return err;
21047e4937aSGao Xiang }
21147e4937aSGao Xiang 
212db166fc2SGao Xiang enum z_erofs_pclustermode {
213db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_INFLIGHT,
21447e4937aSGao Xiang 	/*
215db166fc2SGao Xiang 	 * The current pclusters was the tail of an exist chain, in addition
216db166fc2SGao Xiang 	 * that the previous processed chained pclusters are all decided to
21747e4937aSGao Xiang 	 * be hooked up to it.
218db166fc2SGao Xiang 	 * A new chain will be created for the remaining pclusters which are
219db166fc2SGao Xiang 	 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
220db166fc2SGao Xiang 	 * the next pcluster cannot reuse the whole page safely for inplace I/O
221db166fc2SGao Xiang 	 * in the following scenario:
22247e4937aSGao Xiang 	 *  ________________________________________________________________
22347e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
224db166fc2SGao Xiang 	 * |   (belongs to the next pcl)  |   (belongs to the current pcl)  |
225db166fc2SGao Xiang 	 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
22647e4937aSGao Xiang 	 */
227db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_HOOKED,
2280b964600SGao Xiang 	/*
229db166fc2SGao Xiang 	 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
2300b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
2310b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
232387bab87SGao Xiang 	 * bvpage) since it can be directly decoded without I/O submission.
2330b964600SGao Xiang 	 */
234db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
23547e4937aSGao Xiang 	/*
23647e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
23747e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
23847e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
23947e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
24047e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
24147e4937aSGao Xiang 	 * illustrated below:
24247e4937aSGao Xiang 	 *  ________________________________________________________________
24347e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
24447e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
245db166fc2SGao Xiang 	 * | PCLUSTER_FOLLOWED or |                                        |
246db166fc2SGao Xiang 	 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
24747e4937aSGao Xiang 	 *
24847e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
24947e4937aSGao Xiang 	 */
250db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED,
25147e4937aSGao Xiang };
25247e4937aSGao Xiang 
2535c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
2545c6dcc57SGao Xiang 	struct inode *const inode;
2555c6dcc57SGao Xiang 	struct erofs_map_blocks map;
25606a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
25747e4937aSGao Xiang 
25806a304cdSGao Xiang 	struct page *candidate_bvpage;
25947e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
26047e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
261db166fc2SGao Xiang 	enum z_erofs_pclustermode mode;
26247e4937aSGao Xiang 
2636ea5aad3SGao Xiang 	bool readahead;
26447e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
26547e4937aSGao Xiang 	bool backmost;
26647e4937aSGao Xiang 	erofs_off_t headoffset;
267ed722fbcSGao Xiang 
268ed722fbcSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
269ed722fbcSGao Xiang 	unsigned int icur;
27047e4937aSGao Xiang };
27147e4937aSGao Xiang 
27247e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
2735c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
274db166fc2SGao Xiang 	.mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
27547e4937aSGao Xiang 
2761282dea3SGao Xiang static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
2771282dea3SGao Xiang {
2781282dea3SGao Xiang 	unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
2791282dea3SGao Xiang 
2801282dea3SGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
2811282dea3SGao Xiang 		return false;
2821282dea3SGao Xiang 
2831282dea3SGao Xiang 	if (fe->backmost)
2841282dea3SGao Xiang 		return true;
2851282dea3SGao Xiang 
2861282dea3SGao Xiang 	if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
2871282dea3SGao Xiang 	    fe->map.m_la < fe->headoffset)
2881282dea3SGao Xiang 		return true;
2891282dea3SGao Xiang 
2901282dea3SGao Xiang 	return false;
2911282dea3SGao Xiang }
2921282dea3SGao Xiang 
2936f39d1e1SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
294eaa9172aSGao Xiang 			       struct page **pagepool)
29547e4937aSGao Xiang {
2966f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
2975c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
2981282dea3SGao Xiang 	bool shouldalloc = z_erofs_should_alloc_cache(fe);
29947e4937aSGao Xiang 	bool standalone = true;
3006f39d1e1SGao Xiang 	/*
3016f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
3026f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
3036f39d1e1SGao Xiang 	 */
3041825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
3051825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
306ed722fbcSGao Xiang 	unsigned int i;
30747e4937aSGao Xiang 
308db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
30947e4937aSGao Xiang 		return;
31047e4937aSGao Xiang 
311ed722fbcSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
31247e4937aSGao Xiang 		struct page *page;
313*b1ed220cSGao Xiang 		void *t;	/* mark pages just found for debugging */
3141825c8d7SGao Xiang 		struct page *newpage = NULL;
31547e4937aSGao Xiang 
31647e4937aSGao Xiang 		/* the compressed page was loaded before */
317ed722fbcSGao Xiang 		if (READ_ONCE(pcl->compressed_bvecs[i].page))
31847e4937aSGao Xiang 			continue;
31947e4937aSGao Xiang 
320ed722fbcSGao Xiang 		page = find_get_page(mc, pcl->obj.index + i);
32147e4937aSGao Xiang 
32247e4937aSGao Xiang 		if (page) {
323*b1ed220cSGao Xiang 			t = (void *)((unsigned long)page | 1);
3240b964600SGao Xiang 		} else {
3250b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
3260b964600SGao Xiang 			standalone = false;
3271282dea3SGao Xiang 			if (!shouldalloc)
3281282dea3SGao Xiang 				continue;
3291282dea3SGao Xiang 
3301282dea3SGao Xiang 			/*
3311282dea3SGao Xiang 			 * try to use cached I/O if page allocation
3321282dea3SGao Xiang 			 * succeeds or fallback to in-place I/O instead
3331282dea3SGao Xiang 			 * to avoid any direct reclaim.
3341282dea3SGao Xiang 			 */
3351825c8d7SGao Xiang 			newpage = erofs_allocpage(pagepool, gfp);
3361825c8d7SGao Xiang 			if (!newpage)
33747e4937aSGao Xiang 				continue;
3381282dea3SGao Xiang 			set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
339*b1ed220cSGao Xiang 			t = (void *)((unsigned long)newpage | 1);
34047e4937aSGao Xiang 		}
34147e4937aSGao Xiang 
342*b1ed220cSGao Xiang 		if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
34347e4937aSGao Xiang 			continue;
34447e4937aSGao Xiang 
345eaa9172aSGao Xiang 		if (page)
34647e4937aSGao Xiang 			put_page(page);
347eaa9172aSGao Xiang 		else if (newpage)
348eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
34947e4937aSGao Xiang 	}
35047e4937aSGao Xiang 
3510b964600SGao Xiang 	/*
3520b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
3530b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
3540b964600SGao Xiang 	 */
3550b964600SGao Xiang 	if (standalone)
356db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
35747e4937aSGao Xiang }
35847e4937aSGao Xiang 
35947e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
36047e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
36147e4937aSGao Xiang 				       struct erofs_workgroup *grp)
36247e4937aSGao Xiang {
36347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
36447e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
36547e4937aSGao Xiang 	int i;
36647e4937aSGao Xiang 
367cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
36847e4937aSGao Xiang 	/*
36947e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
37047e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
37147e4937aSGao Xiang 	 */
3729f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
373ed722fbcSGao Xiang 		struct page *page = pcl->compressed_bvecs[i].page;
37447e4937aSGao Xiang 
37547e4937aSGao Xiang 		if (!page)
37647e4937aSGao Xiang 			continue;
37747e4937aSGao Xiang 
37847e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
37947e4937aSGao Xiang 		if (!trylock_page(page))
38047e4937aSGao Xiang 			return -EBUSY;
38147e4937aSGao Xiang 
382f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
38347e4937aSGao Xiang 			continue;
38447e4937aSGao Xiang 
38547e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
386ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
3876aaa7b06SGao Xiang 		detach_page_private(page);
38847e4937aSGao Xiang 		unlock_page(page);
38947e4937aSGao Xiang 	}
39047e4937aSGao Xiang 	return 0;
39147e4937aSGao Xiang }
39247e4937aSGao Xiang 
393d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
39447e4937aSGao Xiang {
39547e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
396ed722fbcSGao Xiang 	int ret, i;
39747e4937aSGao Xiang 
398ed722fbcSGao Xiang 	if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
399ed722fbcSGao Xiang 		return 0;
40047e4937aSGao Xiang 
401ed722fbcSGao Xiang 	ret = 0;
402cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
4039f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
404ed722fbcSGao Xiang 		if (pcl->compressed_bvecs[i].page == page) {
405ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
40647e4937aSGao Xiang 			ret = 1;
40747e4937aSGao Xiang 			break;
40847e4937aSGao Xiang 		}
40947e4937aSGao Xiang 	}
41047e4937aSGao Xiang 	erofs_workgroup_unfreeze(&pcl->obj, 1);
4116aaa7b06SGao Xiang 	if (ret)
4126aaa7b06SGao Xiang 		detach_page_private(page);
41347e4937aSGao Xiang 	return ret;
41447e4937aSGao Xiang }
41547e4937aSGao Xiang 
4165c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
417ed722fbcSGao Xiang 				   struct z_erofs_bvec *bvec)
41847e4937aSGao Xiang {
4195c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
42047e4937aSGao Xiang 
421ed722fbcSGao Xiang 	while (fe->icur > 0) {
422ed722fbcSGao Xiang 		if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
423ed722fbcSGao Xiang 			     NULL, bvec->page)) {
424ed722fbcSGao Xiang 			pcl->compressed_bvecs[fe->icur] = *bvec;
42547e4937aSGao Xiang 			return true;
426ed722fbcSGao Xiang 		}
427ed722fbcSGao Xiang 	}
42847e4937aSGao Xiang 	return false;
42947e4937aSGao Xiang }
43047e4937aSGao Xiang 
43187ca34a7SGao Xiang /* callers must be with pcluster lock held */
4325c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
4335b220b20SGao Xiang 			       struct z_erofs_bvec *bvec, bool exclusive)
43447e4937aSGao Xiang {
43547e4937aSGao Xiang 	int ret;
43647e4937aSGao Xiang 
437db166fc2SGao Xiang 	if (exclusive) {
43806a304cdSGao Xiang 		/* give priority for inplaceio to use file pages first */
439ed722fbcSGao Xiang 		if (z_erofs_try_inplace_io(fe, bvec))
44047e4937aSGao Xiang 			return 0;
44106a304cdSGao Xiang 		/* otherwise, check if it can be used as a bvpage */
442db166fc2SGao Xiang 		if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
44306a304cdSGao Xiang 		    !fe->candidate_bvpage)
44406a304cdSGao Xiang 			fe->candidate_bvpage = bvec->page;
44506a304cdSGao Xiang 	}
44606a304cdSGao Xiang 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
44706a304cdSGao Xiang 	fe->pcl->vcnt += (ret >= 0);
44806a304cdSGao Xiang 	return ret;
44947e4937aSGao Xiang }
45047e4937aSGao Xiang 
4515c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
45247e4937aSGao Xiang {
4535c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
4545c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
45547e4937aSGao Xiang 
456473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
457473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
458473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
45947e4937aSGao Xiang 		*owned_head = &pcl->next;
460473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
461db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
462473e15b0SGao Xiang 		return;
463473e15b0SGao Xiang 	}
464473e15b0SGao Xiang 
46547e4937aSGao Xiang 	/*
466473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
467473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
46847e4937aSGao Xiang 	 */
469267f2492SGao Xiang 	if (*owned_head != &pcl->next && pcl != f->tailpcl &&
470267f2492SGao Xiang 	    cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
471473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
47247e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
473db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_HOOKED;
4745c6dcc57SGao Xiang 		f->tailpcl = NULL;
475473e15b0SGao Xiang 		return;
47647e4937aSGao Xiang 	}
477473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
478db166fc2SGao Xiang 	f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
47947e4937aSGao Xiang }
48047e4937aSGao Xiang 
48183a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
48247e4937aSGao Xiang {
48383a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
484cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
48547e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
48664094a04SGao Xiang 	struct erofs_workgroup *grp;
48747e4937aSGao Xiang 	int err;
48847e4937aSGao Xiang 
489c42c0ffeSChen Zhongjin 	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
490c42c0ffeSChen Zhongjin 	    (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
4918f899262SGao Xiang 		DBG_BUGON(1);
4928f899262SGao Xiang 		return -EFSCORRUPTED;
4938f899262SGao Xiang 	}
4948f899262SGao Xiang 
4959f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
496cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
497cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
4989f6cc76eSGao Xiang 	if (IS_ERR(pcl))
4999f6cc76eSGao Xiang 		return PTR_ERR(pcl);
50047e4937aSGao Xiang 
50164094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
5028f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
5032bfab9c0SGao Xiang 	pcl->length = 0;
5042bfab9c0SGao Xiang 	pcl->partial = true;
50547e4937aSGao Xiang 
50647e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
5075c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
50887ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
509db166fc2SGao Xiang 	fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
51047e4937aSGao Xiang 
51147e4937aSGao Xiang 	/*
51247e4937aSGao Xiang 	 * lock all primary followed works before visible to others
51347e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
51447e4937aSGao Xiang 	 */
51587ca34a7SGao Xiang 	mutex_init(&pcl->lock);
51687ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
51747e4937aSGao Xiang 
518cecf864dSYue Hu 	if (ztailpacking) {
519cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
520cecf864dSYue Hu 		pcl->pageofs_in = erofs_blkoff(map->m_pa);
521cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
522cecf864dSYue Hu 	} else {
523cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
524cecf864dSYue Hu 
52583a386c0SGao Xiang 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
52664094a04SGao Xiang 		if (IS_ERR(grp)) {
52764094a04SGao Xiang 			err = PTR_ERR(grp);
52864094a04SGao Xiang 			goto err_out;
52964094a04SGao Xiang 		}
53064094a04SGao Xiang 
53164094a04SGao Xiang 		if (grp != &pcl->obj) {
5325c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
533cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
53464094a04SGao Xiang 			err = -EEXIST;
53564094a04SGao Xiang 			goto err_out;
53647e4937aSGao Xiang 		}
537cecf864dSYue Hu 	}
53847e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
5395c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
5405c6dcc57SGao Xiang 		fe->tailpcl = pcl;
5415c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
5425c6dcc57SGao Xiang 	fe->pcl = pcl;
5439e579fc1SGao Xiang 	return 0;
54464094a04SGao Xiang 
54564094a04SGao Xiang err_out:
54687ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
5479f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
54864094a04SGao Xiang 	return err;
54947e4937aSGao Xiang }
55047e4937aSGao Xiang 
55183a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
55247e4937aSGao Xiang {
55383a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
5540d823b42SGao Xiang 	struct erofs_workgroup *grp = NULL;
5559e579fc1SGao Xiang 	int ret;
55647e4937aSGao Xiang 
55787ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
55847e4937aSGao Xiang 
55987ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
5605c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
5615c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
56247e4937aSGao Xiang 
5630d823b42SGao Xiang 	if (!(map->m_flags & EROFS_MAP_META)) {
5640d823b42SGao Xiang 		grp = erofs_find_workgroup(fe->inode->i_sb,
5650d823b42SGao Xiang 					   map->m_pa >> PAGE_SHIFT);
5660d823b42SGao Xiang 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
56747e4937aSGao Xiang 		DBG_BUGON(1);
568cecf864dSYue Hu 		return -EFSCORRUPTED;
569cecf864dSYue Hu 	}
57047e4937aSGao Xiang 
57164094a04SGao Xiang 	if (grp) {
5725c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
5730d823b42SGao Xiang 		ret = -EEXIST;
57464094a04SGao Xiang 	} else {
57583a386c0SGao Xiang 		ret = z_erofs_register_pcluster(fe);
57664094a04SGao Xiang 	}
57747e4937aSGao Xiang 
5780d823b42SGao Xiang 	if (ret == -EEXIST) {
579267f2492SGao Xiang 		mutex_lock(&fe->pcl->lock);
580267f2492SGao Xiang 		/* used to check tail merging loop due to corrupted images */
581267f2492SGao Xiang 		if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
582267f2492SGao Xiang 			fe->tailpcl = fe->pcl;
583267f2492SGao Xiang 
584267f2492SGao Xiang 		z_erofs_try_to_claim_pcluster(fe);
5850d823b42SGao Xiang 	} else if (ret) {
5860d823b42SGao Xiang 		return ret;
5870d823b42SGao Xiang 	}
58806a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
589387bab87SGao Xiang 				Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
59081382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
591ed722fbcSGao Xiang 	fe->icur = z_erofs_pclusterpages(fe->pcl);
59247e4937aSGao Xiang 	return 0;
59347e4937aSGao Xiang }
59447e4937aSGao Xiang 
59547e4937aSGao Xiang /*
59647e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
59747e4937aSGao Xiang  * only after a RCU grace period.
59847e4937aSGao Xiang  */
59947e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
60047e4937aSGao Xiang {
60187ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
60287ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
60347e4937aSGao Xiang }
60447e4937aSGao Xiang 
60547e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
60647e4937aSGao Xiang {
60747e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
60847e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
60947e4937aSGao Xiang 
61087ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
61147e4937aSGao Xiang }
61247e4937aSGao Xiang 
6135c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
61447e4937aSGao Xiang {
61587ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
61647e4937aSGao Xiang 
61787ca34a7SGao Xiang 	if (!pcl)
61847e4937aSGao Xiang 		return false;
61947e4937aSGao Xiang 
62006a304cdSGao Xiang 	z_erofs_bvec_iter_end(&fe->biter);
62187ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
62247e4937aSGao Xiang 
62306a304cdSGao Xiang 	if (fe->candidate_bvpage) {
62406a304cdSGao Xiang 		DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
62506a304cdSGao Xiang 		fe->candidate_bvpage = NULL;
62606a304cdSGao Xiang 	}
62706a304cdSGao Xiang 
62847e4937aSGao Xiang 	/*
62947e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
63047e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
63147e4937aSGao Xiang 	 */
632db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
63387ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
63447e4937aSGao Xiang 
63587ca34a7SGao Xiang 	fe->pcl = NULL;
63647e4937aSGao Xiang 	return true;
63747e4937aSGao Xiang }
63847e4937aSGao Xiang 
639b15b2e30SYue Hu static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
640b15b2e30SYue Hu 				 struct page *page, unsigned int pageofs,
641b15b2e30SYue Hu 				 unsigned int len)
642b15b2e30SYue Hu {
643b15b2e30SYue Hu 	struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
644b15b2e30SYue Hu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
645b15b2e30SYue Hu 	u8 *src, *dst;
646b15b2e30SYue Hu 	unsigned int i, cnt;
647b15b2e30SYue Hu 
648e5126de1SYue Hu 	if (!packed_inode)
649e5126de1SYue Hu 		return -EFSCORRUPTED;
650e5126de1SYue Hu 
651b15b2e30SYue Hu 	pos += EROFS_I(inode)->z_fragmentoff;
652b15b2e30SYue Hu 	for (i = 0; i < len; i += cnt) {
653b15b2e30SYue Hu 		cnt = min_t(unsigned int, len - i,
654b15b2e30SYue Hu 			    EROFS_BLKSIZ - erofs_blkoff(pos));
655b15b2e30SYue Hu 		src = erofs_bread(&buf, packed_inode,
656b15b2e30SYue Hu 				  erofs_blknr(pos), EROFS_KMAP);
657b15b2e30SYue Hu 		if (IS_ERR(src)) {
658b15b2e30SYue Hu 			erofs_put_metabuf(&buf);
659b15b2e30SYue Hu 			return PTR_ERR(src);
660b15b2e30SYue Hu 		}
661b15b2e30SYue Hu 
662b15b2e30SYue Hu 		dst = kmap_local_page(page);
663b15b2e30SYue Hu 		memcpy(dst + pageofs + i, src + erofs_blkoff(pos), cnt);
664b15b2e30SYue Hu 		kunmap_local(dst);
665b15b2e30SYue Hu 		pos += cnt;
666b15b2e30SYue Hu 	}
667b15b2e30SYue Hu 	erofs_put_metabuf(&buf);
668b15b2e30SYue Hu 	return 0;
669b15b2e30SYue Hu }
670b15b2e30SYue Hu 
67147e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
672eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
67347e4937aSGao Xiang {
67447e4937aSGao Xiang 	struct inode *const inode = fe->inode;
67547e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
67647e4937aSGao Xiang 	const loff_t offset = page_offset(page);
6775b220b20SGao Xiang 	bool tight = true, exclusive;
6782bfab9c0SGao Xiang 	unsigned int cur, end, spiltted;
67947e4937aSGao Xiang 	int err = 0;
68047e4937aSGao Xiang 
68147e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
68247e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
68347e4937aSGao Xiang 
68447e4937aSGao Xiang 	spiltted = 0;
68547e4937aSGao Xiang 	end = PAGE_SIZE;
68647e4937aSGao Xiang repeat:
68747e4937aSGao Xiang 	cur = end - 1;
68847e4937aSGao Xiang 
68939397a46SGao Xiang 	if (offset + cur < map->m_la ||
69039397a46SGao Xiang 	    offset + cur >= map->m_la + map->m_llen) {
69139397a46SGao Xiang 		erofs_dbg("out-of-range map @ pos %llu", offset + cur);
69247e4937aSGao Xiang 
6935c6dcc57SGao Xiang 		if (z_erofs_collector_end(fe))
69447e4937aSGao Xiang 			fe->backmost = false;
69547e4937aSGao Xiang 		map->m_la = offset + cur;
69647e4937aSGao Xiang 		map->m_llen = 0;
69747e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
6988d8a09b0SGao Xiang 		if (err)
69967148551SGao Xiang 			goto out;
70039397a46SGao Xiang 	} else {
70139397a46SGao Xiang 		if (fe->pcl)
70239397a46SGao Xiang 			goto hitted;
70339397a46SGao Xiang 		/* didn't get a valid pcluster previously (very rare) */
70439397a46SGao Xiang 	}
70547e4937aSGao Xiang 
706b15b2e30SYue Hu 	if (!(map->m_flags & EROFS_MAP_MAPPED) ||
707b15b2e30SYue Hu 	    map->m_flags & EROFS_MAP_FRAGMENT)
70847e4937aSGao Xiang 		goto hitted;
70947e4937aSGao Xiang 
71083a386c0SGao Xiang 	err = z_erofs_collector_begin(fe);
7118d8a09b0SGao Xiang 	if (err)
71267148551SGao Xiang 		goto out;
71347e4937aSGao Xiang 
7145c6dcc57SGao Xiang 	if (z_erofs_is_inline_pcluster(fe->pcl)) {
71509c54379SGao Xiang 		void *mp;
716cecf864dSYue Hu 
71709c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
71809c54379SGao Xiang 					erofs_blknr(map->m_pa), EROFS_NO_KMAP);
71909c54379SGao Xiang 		if (IS_ERR(mp)) {
72009c54379SGao Xiang 			err = PTR_ERR(mp);
721cecf864dSYue Hu 			erofs_err(inode->i_sb,
722cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
72367148551SGao Xiang 			goto out;
724cecf864dSYue Hu 		}
72509c54379SGao Xiang 		get_page(fe->map.buf.page);
726ed722fbcSGao Xiang 		WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
727ed722fbcSGao Xiang 			   fe->map.buf.page);
728db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
729cecf864dSYue Hu 	} else {
7306f39d1e1SGao Xiang 		/* bind cache first when cached decompression is preferred */
7311282dea3SGao Xiang 		z_erofs_bind_cache(fe, pagepool);
732cecf864dSYue Hu 	}
73347e4937aSGao Xiang hitted:
734dc76ea8cSGao Xiang 	/*
735dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
736dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
737dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
738387bab87SGao Xiang 	 * for inplace I/O or bvpage (should be processed in a strict order.)
739dc76ea8cSGao Xiang 	 */
740db166fc2SGao Xiang 	tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
741db166fc2SGao Xiang 		  fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
742dc76ea8cSGao Xiang 
74347e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
7448d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
74547e4937aSGao Xiang 		zero_user_segment(page, cur, end);
74647e4937aSGao Xiang 		goto next_part;
74747e4937aSGao Xiang 	}
748b15b2e30SYue Hu 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
749b15b2e30SYue Hu 		unsigned int pageofs, skip, len;
750b15b2e30SYue Hu 
751b15b2e30SYue Hu 		if (offset > map->m_la) {
752b15b2e30SYue Hu 			pageofs = 0;
753b15b2e30SYue Hu 			skip = offset - map->m_la;
754b15b2e30SYue Hu 		} else {
755b15b2e30SYue Hu 			pageofs = map->m_la & ~PAGE_MASK;
756b15b2e30SYue Hu 			skip = 0;
757b15b2e30SYue Hu 		}
758b15b2e30SYue Hu 		len = min_t(unsigned int, map->m_llen - skip, end - cur);
759b15b2e30SYue Hu 		err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
760b15b2e30SYue Hu 		if (err)
761b15b2e30SYue Hu 			goto out;
762b15b2e30SYue Hu 		++spiltted;
763b15b2e30SYue Hu 		tight = false;
764b15b2e30SYue Hu 		goto next_part;
765b15b2e30SYue Hu 	}
76647e4937aSGao Xiang 
7675b220b20SGao Xiang 	exclusive = (!cur && (!spiltted || tight));
76847e4937aSGao Xiang 	if (cur)
769db166fc2SGao Xiang 		tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
77047e4937aSGao Xiang 
77147e4937aSGao Xiang retry:
77206a304cdSGao Xiang 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
77306a304cdSGao Xiang 					.page = page,
77406a304cdSGao Xiang 					.offset = offset - map->m_la,
77506a304cdSGao Xiang 					.end = end,
7765b220b20SGao Xiang 				  }), exclusive);
77706a304cdSGao Xiang 	/* should allocate an additional short-lived page for bvset */
77806a304cdSGao Xiang 	if (err == -EAGAIN && !fe->candidate_bvpage) {
77906a304cdSGao Xiang 		fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
78006a304cdSGao Xiang 		set_page_private(fe->candidate_bvpage,
78106a304cdSGao Xiang 				 Z_EROFS_SHORTLIVED_PAGE);
78247e4937aSGao Xiang 		goto retry;
78347e4937aSGao Xiang 	}
78447e4937aSGao Xiang 
78506a304cdSGao Xiang 	if (err) {
78606a304cdSGao Xiang 		DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
78767148551SGao Xiang 		goto out;
78806a304cdSGao Xiang 	}
78947e4937aSGao Xiang 
79067148551SGao Xiang 	z_erofs_onlinepage_split(page);
79147e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
79247e4937aSGao Xiang 	++spiltted;
793267f2492SGao Xiang 	if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
794267f2492SGao Xiang 		fe->pcl->multibases = true;
7952bfab9c0SGao Xiang 	if (fe->pcl->length < offset + end - map->m_la) {
7962bfab9c0SGao Xiang 		fe->pcl->length = offset + end - map->m_la;
7972bfab9c0SGao Xiang 		fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
7982bfab9c0SGao Xiang 	}
799e7933278SGao Xiang 	if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
800e7933278SGao Xiang 	    !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
801e7933278SGao Xiang 	    fe->pcl->length == map->m_llen)
802e7933278SGao Xiang 		fe->pcl->partial = false;
80347e4937aSGao Xiang next_part:
8042bfab9c0SGao Xiang 	/* shorten the remaining extent to update progress */
80547e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
8062bfab9c0SGao Xiang 	map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
80747e4937aSGao Xiang 
80847e4937aSGao Xiang 	end = cur;
80947e4937aSGao Xiang 	if (end > 0)
81047e4937aSGao Xiang 		goto repeat;
81147e4937aSGao Xiang 
81247e4937aSGao Xiang out:
81367148551SGao Xiang 	if (err)
81467148551SGao Xiang 		z_erofs_page_mark_eio(page);
81547e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
81647e4937aSGao Xiang 
8174f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
81847e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
81947e4937aSGao Xiang 	return err;
82047e4937aSGao Xiang }
82147e4937aSGao Xiang 
82240452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
82340452ffcSHuang Jianan 				       unsigned int readahead_pages)
82440452ffcSHuang Jianan {
825a2e20a25SMatthew Wilcox (Oracle) 	/* auto: enable for read_folio, disable for readahead */
82640452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
82740452ffcSHuang Jianan 	    !readahead_pages)
82840452ffcSHuang Jianan 		return true;
82940452ffcSHuang Jianan 
83040452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
83140452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
83240452ffcSHuang Jianan 		return true;
83340452ffcSHuang Jianan 
83440452ffcSHuang Jianan 	return false;
83540452ffcSHuang Jianan }
83640452ffcSHuang Jianan 
8376aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
8386aaa7b06SGao Xiang {
8396aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
8406aaa7b06SGao Xiang }
8416aaa7b06SGao Xiang 
8424f05687fSGao Xiang struct z_erofs_decompress_backend {
8434f05687fSGao Xiang 	struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
8444f05687fSGao Xiang 	struct super_block *sb;
8454f05687fSGao Xiang 	struct z_erofs_pcluster *pcl;
8464f05687fSGao Xiang 
8474f05687fSGao Xiang 	/* pages with the longest decompressed length for deduplication */
8484f05687fSGao Xiang 	struct page **decompressed_pages;
8494f05687fSGao Xiang 	/* pages to keep the compressed data */
8504f05687fSGao Xiang 	struct page **compressed_pages;
8514f05687fSGao Xiang 
852267f2492SGao Xiang 	struct list_head decompressed_secondary_bvecs;
8534f05687fSGao Xiang 	struct page **pagepool;
8542bfab9c0SGao Xiang 	unsigned int onstack_used, nr_pages;
8554f05687fSGao Xiang };
8564f05687fSGao Xiang 
857267f2492SGao Xiang struct z_erofs_bvec_item {
858267f2492SGao Xiang 	struct z_erofs_bvec bvec;
859267f2492SGao Xiang 	struct list_head list;
860267f2492SGao Xiang };
861267f2492SGao Xiang 
862267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
8633fe96ee0SGao Xiang 					 struct z_erofs_bvec *bvec)
8643fe96ee0SGao Xiang {
865267f2492SGao Xiang 	struct z_erofs_bvec_item *item;
866267f2492SGao Xiang 
867267f2492SGao Xiang 	if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
868267f2492SGao Xiang 		unsigned int pgnr;
8693fe96ee0SGao Xiang 
870267f2492SGao Xiang 		pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
8712bfab9c0SGao Xiang 		DBG_BUGON(pgnr >= be->nr_pages);
87263bbb856SGao Xiang 		if (!be->decompressed_pages[pgnr]) {
8733fe96ee0SGao Xiang 			be->decompressed_pages[pgnr] = bvec->page;
874267f2492SGao Xiang 			return;
8753fe96ee0SGao Xiang 		}
87663bbb856SGao Xiang 	}
8773fe96ee0SGao Xiang 
878267f2492SGao Xiang 	/* (cold path) one pcluster is requested multiple times */
879267f2492SGao Xiang 	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
880267f2492SGao Xiang 	item->bvec = *bvec;
881267f2492SGao Xiang 	list_add(&item->list, &be->decompressed_secondary_bvecs);
882267f2492SGao Xiang }
883267f2492SGao Xiang 
884267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
885267f2492SGao Xiang 				      int err)
886267f2492SGao Xiang {
887267f2492SGao Xiang 	unsigned int off0 = be->pcl->pageofs_out;
888267f2492SGao Xiang 	struct list_head *p, *n;
889267f2492SGao Xiang 
890267f2492SGao Xiang 	list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
891267f2492SGao Xiang 		struct z_erofs_bvec_item *bvi;
892267f2492SGao Xiang 		unsigned int end, cur;
893267f2492SGao Xiang 		void *dst, *src;
894267f2492SGao Xiang 
895267f2492SGao Xiang 		bvi = container_of(p, struct z_erofs_bvec_item, list);
896267f2492SGao Xiang 		cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
897267f2492SGao Xiang 		end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
898267f2492SGao Xiang 			    bvi->bvec.end);
899267f2492SGao Xiang 		dst = kmap_local_page(bvi->bvec.page);
900267f2492SGao Xiang 		while (cur < end) {
901267f2492SGao Xiang 			unsigned int pgnr, scur, len;
902267f2492SGao Xiang 
903267f2492SGao Xiang 			pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
904267f2492SGao Xiang 			DBG_BUGON(pgnr >= be->nr_pages);
905267f2492SGao Xiang 
906267f2492SGao Xiang 			scur = bvi->bvec.offset + cur -
907267f2492SGao Xiang 					((pgnr << PAGE_SHIFT) - off0);
908267f2492SGao Xiang 			len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
909267f2492SGao Xiang 			if (!be->decompressed_pages[pgnr]) {
910267f2492SGao Xiang 				err = -EFSCORRUPTED;
911267f2492SGao Xiang 				cur += len;
912267f2492SGao Xiang 				continue;
913267f2492SGao Xiang 			}
914267f2492SGao Xiang 			src = kmap_local_page(be->decompressed_pages[pgnr]);
915267f2492SGao Xiang 			memcpy(dst + cur, src + scur, len);
916267f2492SGao Xiang 			kunmap_local(src);
917267f2492SGao Xiang 			cur += len;
918267f2492SGao Xiang 		}
919267f2492SGao Xiang 		kunmap_local(dst);
920267f2492SGao Xiang 		if (err)
921267f2492SGao Xiang 			z_erofs_page_mark_eio(bvi->bvec.page);
922267f2492SGao Xiang 		z_erofs_onlinepage_endio(bvi->bvec.page);
923267f2492SGao Xiang 		list_del(p);
924267f2492SGao Xiang 		kfree(bvi);
925267f2492SGao Xiang 	}
926267f2492SGao Xiang }
927267f2492SGao Xiang 
928267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
92942fec235SGao Xiang {
9304f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
93106a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
93206a304cdSGao Xiang 	struct page *old_bvpage;
933267f2492SGao Xiang 	int i;
93442fec235SGao Xiang 
935387bab87SGao Xiang 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
93642fec235SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
93706a304cdSGao Xiang 		struct z_erofs_bvec bvec;
93842fec235SGao Xiang 
93906a304cdSGao Xiang 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
94042fec235SGao Xiang 
94106a304cdSGao Xiang 		if (old_bvpage)
9424f05687fSGao Xiang 			z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
94342fec235SGao Xiang 
94406a304cdSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
945267f2492SGao Xiang 		z_erofs_do_decompressed_bvec(be, &bvec);
94642fec235SGao Xiang 	}
94706a304cdSGao Xiang 
94806a304cdSGao Xiang 	old_bvpage = z_erofs_bvec_iter_end(&biter);
94906a304cdSGao Xiang 	if (old_bvpage)
9504f05687fSGao Xiang 		z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
95142fec235SGao Xiang }
95242fec235SGao Xiang 
9534f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
9544f05687fSGao Xiang 				  bool *overlapped)
95567139e36SGao Xiang {
9564f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
95767139e36SGao Xiang 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
95867139e36SGao Xiang 	int i, err = 0;
95967139e36SGao Xiang 
96067139e36SGao Xiang 	*overlapped = false;
96167139e36SGao Xiang 	for (i = 0; i < pclusterpages; ++i) {
962ed722fbcSGao Xiang 		struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
963ed722fbcSGao Xiang 		struct page *page = bvec->page;
96467139e36SGao Xiang 
96567139e36SGao Xiang 		/* compressed pages ought to be present before decompressing */
96667139e36SGao Xiang 		if (!page) {
96767139e36SGao Xiang 			DBG_BUGON(1);
96867139e36SGao Xiang 			continue;
96967139e36SGao Xiang 		}
970fe3e5914SGao Xiang 		be->compressed_pages[i] = page;
97167139e36SGao Xiang 
97267139e36SGao Xiang 		if (z_erofs_is_inline_pcluster(pcl)) {
97367139e36SGao Xiang 			if (!PageUptodate(page))
97467139e36SGao Xiang 				err = -EIO;
97567139e36SGao Xiang 			continue;
97667139e36SGao Xiang 		}
97767139e36SGao Xiang 
97867139e36SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
97967139e36SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
9804f05687fSGao Xiang 			if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
98167139e36SGao Xiang 				if (!PageUptodate(page))
98267139e36SGao Xiang 					err = -EIO;
98367139e36SGao Xiang 				continue;
98467139e36SGao Xiang 			}
985267f2492SGao Xiang 			z_erofs_do_decompressed_bvec(be, bvec);
98667139e36SGao Xiang 			*overlapped = true;
98767139e36SGao Xiang 		}
98867139e36SGao Xiang 	}
98967139e36SGao Xiang 
990fe3e5914SGao Xiang 	if (err)
9914f05687fSGao Xiang 		return err;
9924f05687fSGao Xiang 	return 0;
99367139e36SGao Xiang }
99467139e36SGao Xiang 
9954f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
9964f05687fSGao Xiang 				       int err)
99747e4937aSGao Xiang {
9984f05687fSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
9994f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
1000cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
10012bfab9c0SGao Xiang 	unsigned int i, inputsize;
100267148551SGao Xiang 	int err2;
10032bfab9c0SGao Xiang 	struct page *page;
10042bfab9c0SGao Xiang 	bool overlapped;
100547e4937aSGao Xiang 
100687ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
10072bfab9c0SGao Xiang 	be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
100847e4937aSGao Xiang 
1009fe3e5914SGao Xiang 	/* allocate (de)compressed page arrays if cannot be kept on stack */
1010fe3e5914SGao Xiang 	be->decompressed_pages = NULL;
1011fe3e5914SGao Xiang 	be->compressed_pages = NULL;
1012fe3e5914SGao Xiang 	be->onstack_used = 0;
10132bfab9c0SGao Xiang 	if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
10144f05687fSGao Xiang 		be->decompressed_pages = be->onstack_pages;
10152bfab9c0SGao Xiang 		be->onstack_used = be->nr_pages;
10164f05687fSGao Xiang 		memset(be->decompressed_pages, 0,
10172bfab9c0SGao Xiang 		       sizeof(struct page *) * be->nr_pages);
1018fe3e5914SGao Xiang 	}
1019fe3e5914SGao Xiang 
1020fe3e5914SGao Xiang 	if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1021fe3e5914SGao Xiang 		be->compressed_pages = be->onstack_pages + be->onstack_used;
1022fe3e5914SGao Xiang 
1023fe3e5914SGao Xiang 	if (!be->decompressed_pages)
10244f05687fSGao Xiang 		be->decompressed_pages =
102512724ba3SGao Xiang 			kcalloc(be->nr_pages, sizeof(struct page *),
1026e7368187SGao Xiang 				GFP_KERNEL | __GFP_NOFAIL);
1027fe3e5914SGao Xiang 	if (!be->compressed_pages)
1028fe3e5914SGao Xiang 		be->compressed_pages =
102912724ba3SGao Xiang 			kcalloc(pclusterpages, sizeof(struct page *),
1030fe3e5914SGao Xiang 				GFP_KERNEL | __GFP_NOFAIL);
103147e4937aSGao Xiang 
1032267f2492SGao Xiang 	z_erofs_parse_out_bvecs(be);
10334f05687fSGao Xiang 	err2 = z_erofs_parse_in_bvecs(be, &overlapped);
10344f05687fSGao Xiang 	if (err2)
10354f05687fSGao Xiang 		err = err2;
10368d8a09b0SGao Xiang 	if (err)
103747e4937aSGao Xiang 		goto out;
103847e4937aSGao Xiang 
1039cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1040cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1041cecf864dSYue Hu 	else
1042cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1043cecf864dSYue Hu 
104447e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
10454f05687fSGao Xiang 					.sb = be->sb,
10464f05687fSGao Xiang 					.in = be->compressed_pages,
10474f05687fSGao Xiang 					.out = be->decompressed_pages,
1048cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
104987ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
10509f6cc76eSGao Xiang 					.inputsize = inputsize,
10512bfab9c0SGao Xiang 					.outputsize = pcl->length,
105247e4937aSGao Xiang 					.alg = pcl->algorithmformat,
105347e4937aSGao Xiang 					.inplace_io = overlapped,
10542bfab9c0SGao Xiang 					.partial_decoding = pcl->partial,
1055267f2492SGao Xiang 					.fillgaps = pcl->multibases,
10564f05687fSGao Xiang 				 }, be->pagepool);
105747e4937aSGao Xiang 
105847e4937aSGao Xiang out:
1059cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1060cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1061ed722fbcSGao Xiang 		page = pcl->compressed_bvecs[0].page;
1062ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1063cecf864dSYue Hu 		put_page(page);
1064cecf864dSYue Hu 	} else {
1065cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
1066ed722fbcSGao Xiang 			page = pcl->compressed_bvecs[i].page;
106747e4937aSGao Xiang 
106847e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
106947e4937aSGao Xiang 				continue;
107047e4937aSGao Xiang 
10716aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
10724f05687fSGao Xiang 			(void)z_erofs_put_shortlivedpage(be->pagepool, page);
1073ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
107447e4937aSGao Xiang 		}
1075cecf864dSYue Hu 	}
1076fe3e5914SGao Xiang 	if (be->compressed_pages < be->onstack_pages ||
1077fe3e5914SGao Xiang 	    be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
107812724ba3SGao Xiang 		kfree(be->compressed_pages);
1079267f2492SGao Xiang 	z_erofs_fill_other_copies(be, err);
108047e4937aSGao Xiang 
10812bfab9c0SGao Xiang 	for (i = 0; i < be->nr_pages; ++i) {
10824f05687fSGao Xiang 		page = be->decompressed_pages[i];
108347e4937aSGao Xiang 		if (!page)
108447e4937aSGao Xiang 			continue;
108547e4937aSGao Xiang 
10866aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
108747e4937aSGao Xiang 
10886aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
10894f05687fSGao Xiang 		if (z_erofs_put_shortlivedpage(be->pagepool, page))
109047e4937aSGao Xiang 			continue;
109167148551SGao Xiang 		if (err)
109267148551SGao Xiang 			z_erofs_page_mark_eio(page);
109347e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
109447e4937aSGao Xiang 	}
109547e4937aSGao Xiang 
10964f05687fSGao Xiang 	if (be->decompressed_pages != be->onstack_pages)
109712724ba3SGao Xiang 		kfree(be->decompressed_pages);
109847e4937aSGao Xiang 
10992bfab9c0SGao Xiang 	pcl->length = 0;
11002bfab9c0SGao Xiang 	pcl->partial = true;
1101267f2492SGao Xiang 	pcl->multibases = false;
110206a304cdSGao Xiang 	pcl->bvset.nextpage = NULL;
110387ca34a7SGao Xiang 	pcl->vcnt = 0;
110447e4937aSGao Xiang 
110587ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
110647e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
110787ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
110847e4937aSGao Xiang 	return err;
110947e4937aSGao Xiang }
111047e4937aSGao Xiang 
11110c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1112eaa9172aSGao Xiang 				     struct page **pagepool)
111347e4937aSGao Xiang {
11144f05687fSGao Xiang 	struct z_erofs_decompress_backend be = {
11154f05687fSGao Xiang 		.sb = io->sb,
11164f05687fSGao Xiang 		.pagepool = pagepool,
1117267f2492SGao Xiang 		.decompressed_secondary_bvecs =
1118267f2492SGao Xiang 			LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
11194f05687fSGao Xiang 	};
112047e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
112147e4937aSGao Xiang 
112247e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
11234f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
112447e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
11254f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */
112647e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
112747e4937aSGao Xiang 
11284f05687fSGao Xiang 		be.pcl = container_of(owned, struct z_erofs_pcluster, next);
11294f05687fSGao Xiang 		owned = READ_ONCE(be.pcl->next);
113047e4937aSGao Xiang 
11314f05687fSGao Xiang 		z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
11324f05687fSGao Xiang 		erofs_workgroup_put(&be.pcl->obj);
113347e4937aSGao Xiang 	}
113447e4937aSGao Xiang }
113547e4937aSGao Xiang 
11360c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
113747e4937aSGao Xiang {
1138a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1139a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1140eaa9172aSGao Xiang 	struct page *pagepool = NULL;
114147e4937aSGao Xiang 
1142a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
11430c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
114447e4937aSGao Xiang 
1145eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1146a4b1fab1SGao Xiang 	kvfree(bgq);
114747e4937aSGao Xiang }
114847e4937aSGao Xiang 
11497865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1150cdba5506SGao Xiang 				       int bios)
11517865827cSGao Xiang {
11527865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
11537865827cSGao Xiang 
11547865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
1155cdba5506SGao Xiang 	if (io->sync) {
11567865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
115760b30050SHongyu Jin 			complete(&io->u.done);
11587865827cSGao Xiang 		return;
11597865827cSGao Xiang 	}
11607865827cSGao Xiang 
11617865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
11627865827cSGao Xiang 		return;
11637865827cSGao Xiang 	/* Use workqueue and sync decompression for atomic contexts only */
11647865827cSGao Xiang 	if (in_atomic() || irqs_disabled()) {
11657865827cSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
11667865827cSGao Xiang 		/* enable sync decompression for readahead */
11677865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
11687865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
11697865827cSGao Xiang 		return;
11707865827cSGao Xiang 	}
11717865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
11727865827cSGao Xiang }
11737865827cSGao Xiang 
117447e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
117547e4937aSGao Xiang 					       unsigned int nr,
1176eaa9172aSGao Xiang 					       struct page **pagepool,
11779f2731d6SGao Xiang 					       struct address_space *mc)
117847e4937aSGao Xiang {
117947e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
11809f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
118147e4937aSGao Xiang 	bool tocache = false;
118247e4937aSGao Xiang 
118347e4937aSGao Xiang 	struct address_space *mapping;
118447e4937aSGao Xiang 	struct page *oldpage, *page;
118547e4937aSGao Xiang 	int justfound;
118647e4937aSGao Xiang 
118747e4937aSGao Xiang repeat:
1188ed722fbcSGao Xiang 	page = READ_ONCE(pcl->compressed_bvecs[nr].page);
118947e4937aSGao Xiang 	oldpage = page;
119047e4937aSGao Xiang 
119147e4937aSGao Xiang 	if (!page)
119247e4937aSGao Xiang 		goto out_allocpage;
119347e4937aSGao Xiang 
1194*b1ed220cSGao Xiang 	justfound = (unsigned long)page & 1UL;
1195*b1ed220cSGao Xiang 	page = (struct page *)((unsigned long)page & ~1UL);
119647e4937aSGao Xiang 
11971825c8d7SGao Xiang 	/*
11981825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
11991825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
12001825c8d7SGao Xiang 	 */
12011825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1202ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
12031825c8d7SGao Xiang 		set_page_private(page, 0);
12041825c8d7SGao Xiang 		tocache = true;
12051825c8d7SGao Xiang 		goto out_tocache;
12061825c8d7SGao Xiang 	}
120747e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
120847e4937aSGao Xiang 
120947e4937aSGao Xiang 	/*
12106aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
121147e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
121247e4937aSGao Xiang 	 */
121347e4937aSGao Xiang 	if (mapping && mapping != mc)
121447e4937aSGao Xiang 		/* ought to be unmanaged pages */
121547e4937aSGao Xiang 		goto out;
121647e4937aSGao Xiang 
12176aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
12186aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
12196aaa7b06SGao Xiang 		goto out;
12206aaa7b06SGao Xiang 
122147e4937aSGao Xiang 	lock_page(page);
122247e4937aSGao Xiang 
122347e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
122447e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
122547e4937aSGao Xiang 
122647e4937aSGao Xiang 	/* the page is still in manage cache */
122747e4937aSGao Xiang 	if (page->mapping == mc) {
1228ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
122947e4937aSGao Xiang 
123047e4937aSGao Xiang 		if (!PagePrivate(page)) {
123147e4937aSGao Xiang 			/*
123247e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
123347e4937aSGao Xiang 			 * the current restriction as well if
1234ed722fbcSGao Xiang 			 * the page is already in compressed_bvecs[].
123547e4937aSGao Xiang 			 */
123647e4937aSGao Xiang 			DBG_BUGON(!justfound);
123747e4937aSGao Xiang 
123847e4937aSGao Xiang 			justfound = 0;
123947e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
124047e4937aSGao Xiang 			SetPagePrivate(page);
124147e4937aSGao Xiang 		}
124247e4937aSGao Xiang 
124347e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
124447e4937aSGao Xiang 		if (PageUptodate(page)) {
124547e4937aSGao Xiang 			unlock_page(page);
124647e4937aSGao Xiang 			page = NULL;
124747e4937aSGao Xiang 		}
124847e4937aSGao Xiang 		goto out;
124947e4937aSGao Xiang 	}
125047e4937aSGao Xiang 
125147e4937aSGao Xiang 	/*
125247e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
125347e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
125447e4937aSGao Xiang 	 */
125547e4937aSGao Xiang 	DBG_BUGON(page->mapping);
125647e4937aSGao Xiang 	DBG_BUGON(!justfound);
125747e4937aSGao Xiang 
125847e4937aSGao Xiang 	tocache = true;
125947e4937aSGao Xiang 	unlock_page(page);
126047e4937aSGao Xiang 	put_page(page);
126147e4937aSGao Xiang out_allocpage:
12625ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1263ed722fbcSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1264ed722fbcSGao Xiang 			       oldpage, page)) {
1265eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
12665ddcee1fSGao Xiang 		cond_resched();
12675ddcee1fSGao Xiang 		goto repeat;
12685ddcee1fSGao Xiang 	}
12691825c8d7SGao Xiang out_tocache:
1270bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1271bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1272bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1273bf225074SGao Xiang 		goto out;
1274a30573b3SGao Xiang 	}
1275bf225074SGao Xiang 	attach_page_private(page, pcl);
1276bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1277bf225074SGao Xiang 	put_page(page);
1278bf225074SGao Xiang 
127947e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
128047e4937aSGao Xiang 	return page;
128147e4937aSGao Xiang }
128247e4937aSGao Xiang 
1283cdba5506SGao Xiang static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1284a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
128547e4937aSGao Xiang {
1286a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
128747e4937aSGao Xiang 
1288a4b1fab1SGao Xiang 	if (fg && !*fg) {
1289a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1290a4b1fab1SGao Xiang 		if (!q) {
1291a4b1fab1SGao Xiang 			*fg = true;
1292a4b1fab1SGao Xiang 			goto fg_out;
129347e4937aSGao Xiang 		}
12940c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1295a4b1fab1SGao Xiang 	} else {
1296a4b1fab1SGao Xiang fg_out:
1297a4b1fab1SGao Xiang 		q = fgq;
129860b30050SHongyu Jin 		init_completion(&fgq->u.done);
1299a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
130067148551SGao Xiang 		q->eio = false;
1301cdba5506SGao Xiang 		q->sync = true;
1302a4b1fab1SGao Xiang 	}
1303a4b1fab1SGao Xiang 	q->sb = sb;
1304a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1305a4b1fab1SGao Xiang 	return q;
130647e4937aSGao Xiang }
130747e4937aSGao Xiang 
130847e4937aSGao Xiang /* define decompression jobqueue types */
130947e4937aSGao Xiang enum {
131047e4937aSGao Xiang 	JQ_BYPASS,
131147e4937aSGao Xiang 	JQ_SUBMIT,
131247e4937aSGao Xiang 	NR_JOBQUEUES,
131347e4937aSGao Xiang };
131447e4937aSGao Xiang 
131547e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
131647e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
131747e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
131847e4937aSGao Xiang {
131947e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
132047e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
132147e4937aSGao Xiang 
132247e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
132347e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
132447e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
132547e4937aSGao Xiang 
132647e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
132747e4937aSGao Xiang 
132847e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
132947e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
133047e4937aSGao Xiang 
133147e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
133247e4937aSGao Xiang }
133347e4937aSGao Xiang 
13347865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
13357865827cSGao Xiang {
1336cdba5506SGao Xiang 	struct z_erofs_decompressqueue *q = bio->bi_private;
13377865827cSGao Xiang 	blk_status_t err = bio->bi_status;
13387865827cSGao Xiang 	struct bio_vec *bvec;
13397865827cSGao Xiang 	struct bvec_iter_all iter_all;
13407865827cSGao Xiang 
13417865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
13427865827cSGao Xiang 		struct page *page = bvec->bv_page;
13437865827cSGao Xiang 
13447865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
13457865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
13467865827cSGao Xiang 
13477865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
13487865827cSGao Xiang 			if (!err)
13497865827cSGao Xiang 				SetPageUptodate(page);
13507865827cSGao Xiang 			unlock_page(page);
13517865827cSGao Xiang 		}
13527865827cSGao Xiang 	}
135367148551SGao Xiang 	if (err)
135467148551SGao Xiang 		q->eio = true;
1355cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q, -1);
13567865827cSGao Xiang 	bio_put(bio);
13577865827cSGao Xiang }
13587865827cSGao Xiang 
135983a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1360eaa9172aSGao Xiang 				 struct page **pagepool,
1361a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1362a4b1fab1SGao Xiang 				 bool *force_fg)
136347e4937aSGao Xiang {
136483a386c0SGao Xiang 	struct super_block *sb = f->inode->i_sb;
136583a386c0SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
136647e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1367a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
13685c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1369dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
13703f649ab7SKees Cook 	pgoff_t last_index;
1371dfeab2e9SGao Xiang 	struct block_device *last_bdev;
13721e4a2955SGao Xiang 	unsigned int nr_bios = 0;
13731e4a2955SGao Xiang 	struct bio *bio = NULL;
137482e60d00SJohannes Weiner 	unsigned long pflags;
137582e60d00SJohannes Weiner 	int memstall = 0;
137647e4937aSGao Xiang 
1377cdba5506SGao Xiang 	/*
1378cdba5506SGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
1379cdba5506SGao Xiang 	 * no need to read from device for all pclusters in this queue.
1380cdba5506SGao Xiang 	 */
1381cdba5506SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1382cdba5506SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1383cdba5506SGao Xiang 
1384a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1385a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
138647e4937aSGao Xiang 
138747e4937aSGao Xiang 	/* by default, all need io submission */
138847e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
138947e4937aSGao Xiang 
139047e4937aSGao Xiang 	do {
1391dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
139247e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
13931e4a2955SGao Xiang 		pgoff_t cur, end;
13941e4a2955SGao Xiang 		unsigned int i = 0;
13951e4a2955SGao Xiang 		bool bypass = true;
139647e4937aSGao Xiang 
139747e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
139847e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
139947e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
140047e4937aSGao Xiang 
140147e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
140247e4937aSGao Xiang 
1403cecf864dSYue Hu 		/* close the main owned chain at first */
1404cecf864dSYue Hu 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1405cecf864dSYue Hu 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1406cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1407cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1408cecf864dSYue Hu 			continue;
1409cecf864dSYue Hu 		}
1410cecf864dSYue Hu 
1411dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1412dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
1413dfeab2e9SGao Xiang 			.m_pa = blknr_to_addr(pcl->obj.index),
1414dfeab2e9SGao Xiang 		};
1415dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1416dfeab2e9SGao Xiang 
1417dfeab2e9SGao Xiang 		cur = erofs_blknr(mdev.m_pa);
14189f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
141947e4937aSGao Xiang 
14201e4a2955SGao Xiang 		do {
14211e4a2955SGao Xiang 			struct page *page;
142247e4937aSGao Xiang 
14231e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
142483a386c0SGao Xiang 							  mc);
14251e4a2955SGao Xiang 			if (!page)
14261e4a2955SGao Xiang 				continue;
142747e4937aSGao Xiang 
1428dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1429dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
143047e4937aSGao Xiang submit_bio_retry:
143194e4e153SGao Xiang 				submit_bio(bio);
143282e60d00SJohannes Weiner 				if (memstall) {
143382e60d00SJohannes Weiner 					psi_memstall_leave(&pflags);
143482e60d00SJohannes Weiner 					memstall = 0;
143582e60d00SJohannes Weiner 				}
143647e4937aSGao Xiang 				bio = NULL;
143747e4937aSGao Xiang 			}
143847e4937aSGao Xiang 
143982e60d00SJohannes Weiner 			if (unlikely(PageWorkingset(page)) && !memstall) {
144099486c51SChristoph Hellwig 				psi_memstall_enter(&pflags);
144182e60d00SJohannes Weiner 				memstall = 1;
144282e60d00SJohannes Weiner 			}
144399486c51SChristoph Hellwig 
144447e4937aSGao Xiang 			if (!bio) {
144507888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
144607888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
14470c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1448dfeab2e9SGao Xiang 
1449dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
14501e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1451a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1452cdba5506SGao Xiang 				bio->bi_private = q[JQ_SUBMIT];
14536ea5aad3SGao Xiang 				if (f->readahead)
14546ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
145547e4937aSGao Xiang 				++nr_bios;
145647e4937aSGao Xiang 			}
145747e4937aSGao Xiang 
14586c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
145947e4937aSGao Xiang 				goto submit_bio_retry;
146047e4937aSGao Xiang 
14611e4a2955SGao Xiang 			last_index = cur;
14621e4a2955SGao Xiang 			bypass = false;
14631e4a2955SGao Xiang 		} while (++cur < end);
146447e4937aSGao Xiang 
14651e4a2955SGao Xiang 		if (!bypass)
146647e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
146747e4937aSGao Xiang 		else
146847e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
146947e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
147047e4937aSGao Xiang 
147199486c51SChristoph Hellwig 	if (bio) {
147294e4e153SGao Xiang 		submit_bio(bio);
147382e60d00SJohannes Weiner 		if (memstall)
147482e60d00SJohannes Weiner 			psi_memstall_leave(&pflags);
147599486c51SChristoph Hellwig 	}
147647e4937aSGao Xiang 
1477587a67b7SGao Xiang 	/*
1478587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1479587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1480587a67b7SGao Xiang 	 */
1481587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1482587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
14831e4a2955SGao Xiang 		return;
1484587a67b7SGao Xiang 	}
1485cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
148647e4937aSGao Xiang }
148747e4937aSGao Xiang 
148883a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1489eaa9172aSGao Xiang 			     struct page **pagepool, bool force_fg)
149047e4937aSGao Xiang {
1491a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
149247e4937aSGao Xiang 
14935c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
149447e4937aSGao Xiang 		return;
149583a386c0SGao Xiang 	z_erofs_submit_queue(f, pagepool, io, &force_fg);
149647e4937aSGao Xiang 
14970c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
14980c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
149947e4937aSGao Xiang 
150047e4937aSGao Xiang 	if (!force_fg)
150147e4937aSGao Xiang 		return;
150247e4937aSGao Xiang 
150347e4937aSGao Xiang 	/* wait until all bios are completed */
150460b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
150547e4937aSGao Xiang 
15060c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
15070c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
150847e4937aSGao Xiang }
150947e4937aSGao Xiang 
151038629291SGao Xiang /*
151138629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
151238629291SGao Xiang  * approximate readmore strategies as a start.
151338629291SGao Xiang  */
151438629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
151538629291SGao Xiang 				      struct readahead_control *rac,
151638629291SGao Xiang 				      erofs_off_t end,
1517eaa9172aSGao Xiang 				      struct page **pagepool,
151838629291SGao Xiang 				      bool backmost)
151938629291SGao Xiang {
152038629291SGao Xiang 	struct inode *inode = f->inode;
152138629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
152238629291SGao Xiang 	erofs_off_t cur;
152338629291SGao Xiang 	int err;
152438629291SGao Xiang 
152538629291SGao Xiang 	if (backmost) {
152638629291SGao Xiang 		map->m_la = end;
1527622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1528622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
152938629291SGao Xiang 		if (err)
153038629291SGao Xiang 			return;
153138629291SGao Xiang 
153238629291SGao Xiang 		/* expend ra for the trailing edge if readahead */
153338629291SGao Xiang 		if (rac) {
153438629291SGao Xiang 			loff_t newstart = readahead_pos(rac);
153538629291SGao Xiang 
153638629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
153738629291SGao Xiang 			readahead_expand(rac, newstart, cur - newstart);
153838629291SGao Xiang 			return;
153938629291SGao Xiang 		}
154038629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
154138629291SGao Xiang 	} else {
154238629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
154338629291SGao Xiang 
154438629291SGao Xiang 		if (!map->m_llen)
154538629291SGao Xiang 			return;
154638629291SGao Xiang 	}
154738629291SGao Xiang 
154838629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
154938629291SGao Xiang 	while (cur >= end) {
155038629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
155138629291SGao Xiang 		struct page *page;
155238629291SGao Xiang 
155338629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1554aa793b46SGao Xiang 		if (page) {
155538629291SGao Xiang 			if (PageUptodate(page)) {
155638629291SGao Xiang 				unlock_page(page);
1557aa793b46SGao Xiang 			} else {
155838629291SGao Xiang 				err = z_erofs_do_read_page(f, page, pagepool);
155938629291SGao Xiang 				if (err)
156038629291SGao Xiang 					erofs_err(inode->i_sb,
156138629291SGao Xiang 						  "readmore error at page %lu @ nid %llu",
156238629291SGao Xiang 						  index, EROFS_I(inode)->nid);
1563aa793b46SGao Xiang 			}
156438629291SGao Xiang 			put_page(page);
1565aa793b46SGao Xiang 		}
1566aa793b46SGao Xiang 
156738629291SGao Xiang 		if (cur < PAGE_SIZE)
156838629291SGao Xiang 			break;
156938629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
157038629291SGao Xiang 	}
157138629291SGao Xiang }
157238629291SGao Xiang 
1573a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio)
157447e4937aSGao Xiang {
1575a2e20a25SMatthew Wilcox (Oracle) 	struct page *page = &folio->page;
157647e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
157740452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
157847e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1579eaa9172aSGao Xiang 	struct page *pagepool = NULL;
158047e4937aSGao Xiang 	int err;
158147e4937aSGao Xiang 
158247e4937aSGao Xiang 	trace_erofs_readpage(page, false);
158347e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
158447e4937aSGao Xiang 
158538629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
158638629291SGao Xiang 				  &pagepool, true);
15871825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
158838629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
158938629291SGao Xiang 
15905c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
159147e4937aSGao Xiang 
159247e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
159383a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
159440452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, 0));
159547e4937aSGao Xiang 
159647e4937aSGao Xiang 	if (err)
15974f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
159847e4937aSGao Xiang 
159909c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1600eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
160147e4937aSGao Xiang 	return err;
160247e4937aSGao Xiang }
160347e4937aSGao Xiang 
16040615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
160547e4937aSGao Xiang {
16060615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
160747e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
160847e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1609eaa9172aSGao Xiang 	struct page *pagepool = NULL, *head = NULL, *page;
161038629291SGao Xiang 	unsigned int nr_pages;
161147e4937aSGao Xiang 
16126ea5aad3SGao Xiang 	f.readahead = true;
16130615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
161447e4937aSGao Xiang 
161538629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, f.headoffset +
161638629291SGao Xiang 				  readahead_length(rac) - 1, &pagepool, true);
161738629291SGao Xiang 	nr_pages = readahead_count(rac);
161838629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
161938629291SGao Xiang 
16200615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
162147e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
162247e4937aSGao Xiang 		head = page;
162347e4937aSGao Xiang 	}
162447e4937aSGao Xiang 
162547e4937aSGao Xiang 	while (head) {
162647e4937aSGao Xiang 		struct page *page = head;
162747e4937aSGao Xiang 		int err;
162847e4937aSGao Xiang 
162947e4937aSGao Xiang 		/* traversal in reverse order */
163047e4937aSGao Xiang 		head = (void *)page_private(page);
163147e4937aSGao Xiang 
16321825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1633a5876e24SGao Xiang 		if (err)
16344f761fa2SGao Xiang 			erofs_err(inode->i_sb,
16354f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
16364f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
163747e4937aSGao Xiang 		put_page(page);
163847e4937aSGao Xiang 	}
163938629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
16405c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
164147e4937aSGao Xiang 
164283a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
164340452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
164409c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1645eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
164647e4937aSGao Xiang }
164747e4937aSGao Xiang 
16480c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
1649a2e20a25SMatthew Wilcox (Oracle) 	.read_folio = z_erofs_read_folio,
16500615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
165147e4937aSGao Xiang };
1652