xref: /openbmc/linux/fs/erofs/zdata.c (revision 387bab87)
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>
1047e4937aSGao Xiang 
1147e4937aSGao Xiang #include <trace/events/erofs.h>
1247e4937aSGao Xiang 
1347e4937aSGao Xiang /*
149f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
159f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
169f6cc76eSGao Xiang  */
179f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
189f6cc76eSGao Xiang 	struct kmem_cache *slab;
199f6cc76eSGao Xiang 	unsigned int maxpages;
209f6cc76eSGao Xiang 	char name[48];
219f6cc76eSGao Xiang };
229f6cc76eSGao Xiang 
239f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
249f6cc76eSGao Xiang 
259f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
269f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
279f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
289f6cc76eSGao Xiang };
299f6cc76eSGao Xiang 
30*387bab87SGao Xiang /* (obsoleted) page type for online pages */
31*387bab87SGao Xiang enum z_erofs_page_type {
32*387bab87SGao Xiang 	/* including Z_EROFS_VLE_PAGE_TAIL_EXCLUSIVE */
33*387bab87SGao Xiang 	Z_EROFS_PAGE_TYPE_EXCLUSIVE,
34*387bab87SGao Xiang 
35*387bab87SGao Xiang 	Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED,
36*387bab87SGao Xiang 
37*387bab87SGao Xiang 	Z_EROFS_VLE_PAGE_TYPE_HEAD,
38*387bab87SGao Xiang 	Z_EROFS_VLE_PAGE_TYPE_MAX
39*387bab87SGao Xiang };
40*387bab87SGao Xiang 
4106a304cdSGao Xiang struct z_erofs_bvec_iter {
4206a304cdSGao Xiang 	struct page *bvpage;
4306a304cdSGao Xiang 	struct z_erofs_bvset *bvset;
4406a304cdSGao Xiang 	unsigned int nr, cur;
4506a304cdSGao Xiang };
4606a304cdSGao Xiang 
4706a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
4806a304cdSGao Xiang {
4906a304cdSGao Xiang 	if (iter->bvpage)
5006a304cdSGao Xiang 		kunmap_local(iter->bvset);
5106a304cdSGao Xiang 	return iter->bvpage;
5206a304cdSGao Xiang }
5306a304cdSGao Xiang 
5406a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
5506a304cdSGao Xiang {
5606a304cdSGao Xiang 	unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
5706a304cdSGao Xiang 	/* have to access nextpage in advance, otherwise it will be unmapped */
5806a304cdSGao Xiang 	struct page *nextpage = iter->bvset->nextpage;
5906a304cdSGao Xiang 	struct page *oldpage;
6006a304cdSGao Xiang 
6106a304cdSGao Xiang 	DBG_BUGON(!nextpage);
6206a304cdSGao Xiang 	oldpage = z_erofs_bvec_iter_end(iter);
6306a304cdSGao Xiang 	iter->bvpage = nextpage;
6406a304cdSGao Xiang 	iter->bvset = kmap_local_page(nextpage);
6506a304cdSGao Xiang 	iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
6606a304cdSGao Xiang 	iter->cur = 0;
6706a304cdSGao Xiang 	return oldpage;
6806a304cdSGao Xiang }
6906a304cdSGao Xiang 
7006a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
7106a304cdSGao Xiang 				    struct z_erofs_bvset_inline *bvset,
7206a304cdSGao Xiang 				    unsigned int bootstrap_nr,
7306a304cdSGao Xiang 				    unsigned int cur)
7406a304cdSGao Xiang {
7506a304cdSGao Xiang 	*iter = (struct z_erofs_bvec_iter) {
7606a304cdSGao Xiang 		.nr = bootstrap_nr,
7706a304cdSGao Xiang 		.bvset = (struct z_erofs_bvset *)bvset,
7806a304cdSGao Xiang 	};
7906a304cdSGao Xiang 
8006a304cdSGao Xiang 	while (cur > iter->nr) {
8106a304cdSGao Xiang 		cur -= iter->nr;
8206a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
8306a304cdSGao Xiang 	}
8406a304cdSGao Xiang 	iter->cur = cur;
8506a304cdSGao Xiang }
8606a304cdSGao Xiang 
8706a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
8806a304cdSGao Xiang 				struct z_erofs_bvec *bvec,
8906a304cdSGao Xiang 				struct page **candidate_bvpage)
9006a304cdSGao Xiang {
9106a304cdSGao Xiang 	if (iter->cur == iter->nr) {
9206a304cdSGao Xiang 		if (!*candidate_bvpage)
9306a304cdSGao Xiang 			return -EAGAIN;
9406a304cdSGao Xiang 
9506a304cdSGao Xiang 		DBG_BUGON(iter->bvset->nextpage);
9606a304cdSGao Xiang 		iter->bvset->nextpage = *candidate_bvpage;
9706a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
9806a304cdSGao Xiang 
9906a304cdSGao Xiang 		iter->bvset->nextpage = NULL;
10006a304cdSGao Xiang 		*candidate_bvpage = NULL;
10106a304cdSGao Xiang 	}
10206a304cdSGao Xiang 	iter->bvset->bvec[iter->cur++] = *bvec;
10306a304cdSGao Xiang 	return 0;
10406a304cdSGao Xiang }
10506a304cdSGao Xiang 
10606a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
10706a304cdSGao Xiang 				 struct z_erofs_bvec *bvec,
10806a304cdSGao Xiang 				 struct page **old_bvpage)
10906a304cdSGao Xiang {
11006a304cdSGao Xiang 	if (iter->cur == iter->nr)
11106a304cdSGao Xiang 		*old_bvpage = z_erofs_bvset_flip(iter);
11206a304cdSGao Xiang 	else
11306a304cdSGao Xiang 		*old_bvpage = NULL;
11406a304cdSGao Xiang 	*bvec = iter->bvset->bvec[iter->cur++];
11506a304cdSGao Xiang }
11606a304cdSGao Xiang 
1179f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
1189f6cc76eSGao Xiang {
1199f6cc76eSGao Xiang 	int i;
1209f6cc76eSGao Xiang 
1219f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
1229f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
1239f6cc76eSGao Xiang 			continue;
1249f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
1259f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
1269f6cc76eSGao Xiang 	}
1279f6cc76eSGao Xiang }
1289f6cc76eSGao Xiang 
1299f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
1309f6cc76eSGao Xiang {
1319f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
1329f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
1339f6cc76eSGao Xiang 	unsigned int size;
1349f6cc76eSGao Xiang 
1359f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
1369f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
1379f6cc76eSGao Xiang 		size = struct_size(a, compressed_pages, pcs->maxpages);
1389f6cc76eSGao Xiang 
1399f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
1409f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
1419f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
1429f6cc76eSGao Xiang 		if (pcs->slab)
1439f6cc76eSGao Xiang 			continue;
1449f6cc76eSGao Xiang 
1459f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
1469f6cc76eSGao Xiang 		return -ENOMEM;
1479f6cc76eSGao Xiang 	}
1489f6cc76eSGao Xiang 	return 0;
1499f6cc76eSGao Xiang }
1509f6cc76eSGao Xiang 
1519f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
1529f6cc76eSGao Xiang {
1539f6cc76eSGao Xiang 	int i;
1549f6cc76eSGao Xiang 
1559f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
1569f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
1579f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
1589f6cc76eSGao Xiang 
1599f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
1609f6cc76eSGao Xiang 			continue;
1619f6cc76eSGao Xiang 
1629f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
1639f6cc76eSGao Xiang 		if (!pcl)
1649f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
1659f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
1669f6cc76eSGao Xiang 		return pcl;
1679f6cc76eSGao Xiang 	}
1689f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
1699f6cc76eSGao Xiang }
1709f6cc76eSGao Xiang 
1719f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
1729f6cc76eSGao Xiang {
173cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1749f6cc76eSGao Xiang 	int i;
1759f6cc76eSGao Xiang 
1769f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
1779f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
1789f6cc76eSGao Xiang 
179cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
1809f6cc76eSGao Xiang 			continue;
1819f6cc76eSGao Xiang 
1829f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
1839f6cc76eSGao Xiang 		return;
1849f6cc76eSGao Xiang 	}
1859f6cc76eSGao Xiang 	DBG_BUGON(1);
1869f6cc76eSGao Xiang }
1879f6cc76eSGao Xiang 
18847e4937aSGao Xiang /* how to allocate cached pages for a pcluster */
18947e4937aSGao Xiang enum z_erofs_cache_alloctype {
19047e4937aSGao Xiang 	DONTALLOC,	/* don't allocate any cached pages */
1911825c8d7SGao Xiang 	/*
1921825c8d7SGao Xiang 	 * try to use cached I/O if page allocation succeeds or fallback
1931825c8d7SGao Xiang 	 * to in-place I/O instead to avoid any direct reclaim.
1941825c8d7SGao Xiang 	 */
1951825c8d7SGao Xiang 	TRYALLOC,
19647e4937aSGao Xiang };
19747e4937aSGao Xiang 
19847e4937aSGao Xiang /*
19947e4937aSGao Xiang  * tagged pointer with 1-bit tag for all compressed pages
20047e4937aSGao Xiang  * tag 0 - the page is just found with an extra page reference
20147e4937aSGao Xiang  */
20247e4937aSGao Xiang typedef tagptr1_t compressed_page_t;
20347e4937aSGao Xiang 
20447e4937aSGao Xiang #define tag_compressed_page_justfound(page) \
20547e4937aSGao Xiang 	tagptr_fold(compressed_page_t, page, 1)
20647e4937aSGao Xiang 
20747e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
20847e4937aSGao Xiang 
20947e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
21047e4937aSGao Xiang {
21147e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
2129f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
21347e4937aSGao Xiang }
21447e4937aSGao Xiang 
21599634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
21647e4937aSGao Xiang {
21747e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
21847e4937aSGao Xiang 
21947e4937aSGao Xiang 	/*
22047e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
22147e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
22247e4937aSGao Xiang 	 */
2230e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
2240e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
22547e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
22647e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
22747e4937aSGao Xiang }
22847e4937aSGao Xiang 
22947e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
23047e4937aSGao Xiang {
2319f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
23247e4937aSGao Xiang 
2339f6cc76eSGao Xiang 	if (err)
2349f6cc76eSGao Xiang 		return err;
2359f6cc76eSGao Xiang 	err = z_erofs_init_workqueue();
2369f6cc76eSGao Xiang 	if (err)
2379f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
2389f6cc76eSGao Xiang 	return err;
23947e4937aSGao Xiang }
24047e4937aSGao Xiang 
24147e4937aSGao Xiang enum z_erofs_collectmode {
24247e4937aSGao Xiang 	COLLECT_SECONDARY,
24347e4937aSGao Xiang 	COLLECT_PRIMARY,
24447e4937aSGao Xiang 	/*
24547e4937aSGao Xiang 	 * The current collection was the tail of an exist chain, in addition
24647e4937aSGao Xiang 	 * that the previous processed chained collections are all decided to
24747e4937aSGao Xiang 	 * be hooked up to it.
24847e4937aSGao Xiang 	 * A new chain will be created for the remaining collections which are
24947e4937aSGao Xiang 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
25047e4937aSGao Xiang 	 * the next collection cannot reuse the whole page safely in
25147e4937aSGao Xiang 	 * the following scenario:
25247e4937aSGao Xiang 	 *  ________________________________________________________________
25347e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
25447e4937aSGao Xiang 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
25547e4937aSGao Xiang 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
25647e4937aSGao Xiang 	 */
25747e4937aSGao Xiang 	COLLECT_PRIMARY_HOOKED,
2580b964600SGao Xiang 	/*
2590b964600SGao Xiang 	 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
2600b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
2610b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
262*387bab87SGao Xiang 	 * bvpage) since it can be directly decoded without I/O submission.
2630b964600SGao Xiang 	 */
26447e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
26547e4937aSGao Xiang 	/*
26647e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
26747e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
26847e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
26947e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
27047e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
27147e4937aSGao Xiang 	 * illustrated below:
27247e4937aSGao Xiang 	 *  ________________________________________________________________
27347e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
27447e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
27547e4937aSGao Xiang 	 * |  PRIMARY_FOLLOWED or |                                        |
27647e4937aSGao Xiang 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
27747e4937aSGao Xiang 	 *
27847e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
27947e4937aSGao Xiang 	 */
28047e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED,
28147e4937aSGao Xiang };
28247e4937aSGao Xiang 
2835c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
2845c6dcc57SGao Xiang 	struct inode *const inode;
2855c6dcc57SGao Xiang 	struct erofs_map_blocks map;
28606a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
28747e4937aSGao Xiang 
28806a304cdSGao Xiang 	struct page *candidate_bvpage;
28947e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
29081382f5fSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
29181382f5fSGao Xiang 	struct page **icpage_ptr;
29247e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
29347e4937aSGao Xiang 
29447e4937aSGao Xiang 	enum z_erofs_collectmode mode;
29547e4937aSGao Xiang 
2966ea5aad3SGao Xiang 	bool readahead;
29747e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
29847e4937aSGao Xiang 	bool backmost;
29947e4937aSGao Xiang 	erofs_off_t headoffset;
30047e4937aSGao Xiang };
30147e4937aSGao Xiang 
30247e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
3035c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
3044398d3c3SWeizhao Ouyang 	.mode = COLLECT_PRIMARY_FOLLOWED, .backmost = true }
30547e4937aSGao Xiang 
30647e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
30747e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
30847e4937aSGao Xiang 
3096f39d1e1SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
3101825c8d7SGao Xiang 			       enum z_erofs_cache_alloctype type,
311eaa9172aSGao Xiang 			       struct page **pagepool)
31247e4937aSGao Xiang {
3136f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
3145c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
31547e4937aSGao Xiang 	bool standalone = true;
3166f39d1e1SGao Xiang 	/*
3176f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
3186f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
3196f39d1e1SGao Xiang 	 */
3201825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
3211825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
32281382f5fSGao Xiang 	struct page **pages;
32381382f5fSGao Xiang 	pgoff_t index;
32447e4937aSGao Xiang 
3255c6dcc57SGao Xiang 	if (fe->mode < COLLECT_PRIMARY_FOLLOWED)
32647e4937aSGao Xiang 		return;
32747e4937aSGao Xiang 
32881382f5fSGao Xiang 	pages = pcl->compressed_pages;
32981382f5fSGao Xiang 	index = pcl->obj.index;
33081382f5fSGao Xiang 	for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
33147e4937aSGao Xiang 		struct page *page;
33247e4937aSGao Xiang 		compressed_page_t t;
3331825c8d7SGao Xiang 		struct page *newpage = NULL;
33447e4937aSGao Xiang 
33547e4937aSGao Xiang 		/* the compressed page was loaded before */
33647e4937aSGao Xiang 		if (READ_ONCE(*pages))
33747e4937aSGao Xiang 			continue;
33847e4937aSGao Xiang 
33947e4937aSGao Xiang 		page = find_get_page(mc, index);
34047e4937aSGao Xiang 
34147e4937aSGao Xiang 		if (page) {
34247e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
3430b964600SGao Xiang 		} else {
3440b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
3450b964600SGao Xiang 			standalone = false;
3460b964600SGao Xiang 			switch (type) {
3470b964600SGao Xiang 			case TRYALLOC:
3481825c8d7SGao Xiang 				newpage = erofs_allocpage(pagepool, gfp);
3491825c8d7SGao Xiang 				if (!newpage)
35047e4937aSGao Xiang 					continue;
3510b964600SGao Xiang 				set_page_private(newpage,
3520b964600SGao Xiang 						 Z_EROFS_PREALLOCATED_PAGE);
3530b964600SGao Xiang 				t = tag_compressed_page_justfound(newpage);
3540b964600SGao Xiang 				break;
3550b964600SGao Xiang 			default:        /* DONTALLOC */
3560b964600SGao Xiang 				continue;
3570b964600SGao Xiang 			}
35847e4937aSGao Xiang 		}
35947e4937aSGao Xiang 
36047e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
36147e4937aSGao Xiang 			continue;
36247e4937aSGao Xiang 
363eaa9172aSGao Xiang 		if (page)
36447e4937aSGao Xiang 			put_page(page);
365eaa9172aSGao Xiang 		else if (newpage)
366eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
36747e4937aSGao Xiang 	}
36847e4937aSGao Xiang 
3690b964600SGao Xiang 	/*
3700b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
3710b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
3720b964600SGao Xiang 	 */
3730b964600SGao Xiang 	if (standalone)
3745c6dcc57SGao Xiang 		fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
37547e4937aSGao Xiang }
37647e4937aSGao Xiang 
37747e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
37847e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
37947e4937aSGao Xiang 				       struct erofs_workgroup *grp)
38047e4937aSGao Xiang {
38147e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
38247e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
38347e4937aSGao Xiang 	int i;
38447e4937aSGao Xiang 
385cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
38647e4937aSGao Xiang 	/*
38747e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
38847e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
38947e4937aSGao Xiang 	 */
3909f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
39147e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
39247e4937aSGao Xiang 
39347e4937aSGao Xiang 		if (!page)
39447e4937aSGao Xiang 			continue;
39547e4937aSGao Xiang 
39647e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
39747e4937aSGao Xiang 		if (!trylock_page(page))
39847e4937aSGao Xiang 			return -EBUSY;
39947e4937aSGao Xiang 
400f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
40147e4937aSGao Xiang 			continue;
40247e4937aSGao Xiang 
40347e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
40447e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
4056aaa7b06SGao Xiang 		detach_page_private(page);
40647e4937aSGao Xiang 		unlock_page(page);
40747e4937aSGao Xiang 	}
40847e4937aSGao Xiang 	return 0;
40947e4937aSGao Xiang }
41047e4937aSGao Xiang 
411d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
41247e4937aSGao Xiang {
41347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
41447e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
41547e4937aSGao Xiang 
41647e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
41747e4937aSGao Xiang 		unsigned int i;
41847e4937aSGao Xiang 
419cecf864dSYue Hu 		DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
4209f6cc76eSGao Xiang 		for (i = 0; i < pcl->pclusterpages; ++i) {
42147e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
42247e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
42347e4937aSGao Xiang 				ret = 1;
42447e4937aSGao Xiang 				break;
42547e4937aSGao Xiang 			}
42647e4937aSGao Xiang 		}
42747e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
42847e4937aSGao Xiang 
4296aaa7b06SGao Xiang 		if (ret)
4306aaa7b06SGao Xiang 			detach_page_private(page);
43147e4937aSGao Xiang 	}
43247e4937aSGao Xiang 	return ret;
43347e4937aSGao Xiang }
43447e4937aSGao Xiang 
43547e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
4365c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
43747e4937aSGao Xiang 				   struct page *page)
43847e4937aSGao Xiang {
4395c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
44047e4937aSGao Xiang 
4415c6dcc57SGao Xiang 	while (fe->icpage_ptr > pcl->compressed_pages)
4425c6dcc57SGao Xiang 		if (!cmpxchg(--fe->icpage_ptr, NULL, page))
44347e4937aSGao Xiang 			return true;
44447e4937aSGao Xiang 	return false;
44547e4937aSGao Xiang }
44647e4937aSGao Xiang 
44787ca34a7SGao Xiang /* callers must be with pcluster lock held */
4485c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
44906a304cdSGao Xiang 			       struct z_erofs_bvec *bvec,
45006a304cdSGao Xiang 			       enum z_erofs_page_type type)
45147e4937aSGao Xiang {
45247e4937aSGao Xiang 	int ret;
45347e4937aSGao Xiang 
4545c6dcc57SGao Xiang 	if (fe->mode >= COLLECT_PRIMARY &&
45506a304cdSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE) {
45606a304cdSGao Xiang 		/* give priority for inplaceio to use file pages first */
45706a304cdSGao Xiang 		if (z_erofs_try_inplace_io(fe, bvec->page))
45847e4937aSGao Xiang 			return 0;
45906a304cdSGao Xiang 		/* otherwise, check if it can be used as a bvpage */
46006a304cdSGao Xiang 		if (fe->mode >= COLLECT_PRIMARY_FOLLOWED &&
46106a304cdSGao Xiang 		    !fe->candidate_bvpage)
46206a304cdSGao Xiang 			fe->candidate_bvpage = bvec->page;
46306a304cdSGao Xiang 	}
46406a304cdSGao Xiang 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
46506a304cdSGao Xiang 	fe->pcl->vcnt += (ret >= 0);
46606a304cdSGao Xiang 	return ret;
46747e4937aSGao Xiang }
46847e4937aSGao Xiang 
4695c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
47047e4937aSGao Xiang {
4715c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
4725c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
47347e4937aSGao Xiang 
474473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
475473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
476473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
47747e4937aSGao Xiang 		*owned_head = &pcl->next;
478473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
4795c6dcc57SGao Xiang 		f->mode = COLLECT_PRIMARY_FOLLOWED;
480473e15b0SGao Xiang 		return;
481473e15b0SGao Xiang 	}
482473e15b0SGao Xiang 
48347e4937aSGao Xiang 	/*
484473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
485473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
48647e4937aSGao Xiang 	 */
48747e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
488473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
48947e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
4905c6dcc57SGao Xiang 		f->mode = COLLECT_PRIMARY_HOOKED;
4915c6dcc57SGao Xiang 		f->tailpcl = NULL;
492473e15b0SGao Xiang 		return;
49347e4937aSGao Xiang 	}
494473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
4955c6dcc57SGao Xiang 	f->mode = COLLECT_PRIMARY;
49647e4937aSGao Xiang }
49747e4937aSGao Xiang 
49883a386c0SGao Xiang static int z_erofs_lookup_pcluster(struct z_erofs_decompress_frontend *fe)
49947e4937aSGao Xiang {
50083a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
5015c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
50247e4937aSGao Xiang 	unsigned int length;
50347e4937aSGao Xiang 
50464094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
5055c6dcc57SGao Xiang 	if (fe->owned_head == &pcl->next || pcl == fe->tailpcl) {
50647e4937aSGao Xiang 		DBG_BUGON(1);
5079e579fc1SGao Xiang 		return -EFSCORRUPTED;
50847e4937aSGao Xiang 	}
50947e4937aSGao Xiang 
51087ca34a7SGao Xiang 	if (pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) {
51147e4937aSGao Xiang 		DBG_BUGON(1);
5129e579fc1SGao Xiang 		return -EFSCORRUPTED;
51347e4937aSGao Xiang 	}
51447e4937aSGao Xiang 
51547e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
51647e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
51747e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
51847e4937aSGao Xiang 			DBG_BUGON(1);
5199e579fc1SGao Xiang 			return -EFSCORRUPTED;
52047e4937aSGao Xiang 		}
52147e4937aSGao Xiang 	} else {
52247e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
52347e4937aSGao Xiang 
52447e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
52547e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
52647e4937aSGao Xiang 
52747e4937aSGao Xiang 		while (llen > length &&
52847e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
52947e4937aSGao Xiang 			cpu_relax();
53047e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
53147e4937aSGao Xiang 		}
53247e4937aSGao Xiang 	}
53387ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
53447e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
5355c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
5365c6dcc57SGao Xiang 		fe->tailpcl = pcl;
537473e15b0SGao Xiang 
5385c6dcc57SGao Xiang 	z_erofs_try_to_claim_pcluster(fe);
5399e579fc1SGao Xiang 	return 0;
54047e4937aSGao Xiang }
54147e4937aSGao Xiang 
54283a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
54347e4937aSGao Xiang {
54483a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
545cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
54647e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
54764094a04SGao Xiang 	struct erofs_workgroup *grp;
54847e4937aSGao Xiang 	int err;
54947e4937aSGao Xiang 
5508f899262SGao Xiang 	if (!(map->m_flags & EROFS_MAP_ENCODED)) {
5518f899262SGao Xiang 		DBG_BUGON(1);
5528f899262SGao Xiang 		return -EFSCORRUPTED;
5538f899262SGao Xiang 	}
5548f899262SGao Xiang 
5559f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
556cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
557cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
5589f6cc76eSGao Xiang 	if (IS_ERR(pcl))
5599f6cc76eSGao Xiang 		return PTR_ERR(pcl);
56047e4937aSGao Xiang 
56164094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
5628f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
56347e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
56447e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
56547e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
56647e4937aSGao Xiang 
56747e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
5685c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
56987ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
5705c6dcc57SGao Xiang 	fe->mode = COLLECT_PRIMARY_FOLLOWED;
57147e4937aSGao Xiang 
57247e4937aSGao Xiang 	/*
57347e4937aSGao Xiang 	 * lock all primary followed works before visible to others
57447e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
57547e4937aSGao Xiang 	 */
57687ca34a7SGao Xiang 	mutex_init(&pcl->lock);
57787ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
57847e4937aSGao Xiang 
579cecf864dSYue Hu 	if (ztailpacking) {
580cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
581cecf864dSYue Hu 		pcl->pageofs_in = erofs_blkoff(map->m_pa);
582cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
583cecf864dSYue Hu 	} else {
584cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
585cecf864dSYue Hu 
58683a386c0SGao Xiang 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
58764094a04SGao Xiang 		if (IS_ERR(grp)) {
58864094a04SGao Xiang 			err = PTR_ERR(grp);
58964094a04SGao Xiang 			goto err_out;
59064094a04SGao Xiang 		}
59164094a04SGao Xiang 
59264094a04SGao Xiang 		if (grp != &pcl->obj) {
5935c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
594cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
59564094a04SGao Xiang 			err = -EEXIST;
59664094a04SGao Xiang 			goto err_out;
59747e4937aSGao Xiang 		}
598cecf864dSYue Hu 	}
59947e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
6005c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
6015c6dcc57SGao Xiang 		fe->tailpcl = pcl;
6025c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
6035c6dcc57SGao Xiang 	fe->pcl = pcl;
6049e579fc1SGao Xiang 	return 0;
60564094a04SGao Xiang 
60664094a04SGao Xiang err_out:
60787ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
6089f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
60964094a04SGao Xiang 	return err;
61047e4937aSGao Xiang }
61147e4937aSGao Xiang 
61283a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
61347e4937aSGao Xiang {
61483a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
6150d823b42SGao Xiang 	struct erofs_workgroup *grp = NULL;
6169e579fc1SGao Xiang 	int ret;
61747e4937aSGao Xiang 
61887ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
61947e4937aSGao Xiang 
62087ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
6215c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
6225c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
62347e4937aSGao Xiang 
6240d823b42SGao Xiang 	if (!(map->m_flags & EROFS_MAP_META)) {
6250d823b42SGao Xiang 		grp = erofs_find_workgroup(fe->inode->i_sb,
6260d823b42SGao Xiang 					   map->m_pa >> PAGE_SHIFT);
6270d823b42SGao Xiang 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
62847e4937aSGao Xiang 		DBG_BUGON(1);
629cecf864dSYue Hu 		return -EFSCORRUPTED;
630cecf864dSYue Hu 	}
63147e4937aSGao Xiang 
63264094a04SGao Xiang 	if (grp) {
6335c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
6340d823b42SGao Xiang 		ret = -EEXIST;
63564094a04SGao Xiang 	} else {
63683a386c0SGao Xiang 		ret = z_erofs_register_pcluster(fe);
63764094a04SGao Xiang 	}
63847e4937aSGao Xiang 
6390d823b42SGao Xiang 	if (ret == -EEXIST) {
64083a386c0SGao Xiang 		ret = z_erofs_lookup_pcluster(fe);
64164094a04SGao Xiang 		if (ret) {
6425c6dcc57SGao Xiang 			erofs_workgroup_put(&fe->pcl->obj);
64364094a04SGao Xiang 			return ret;
64464094a04SGao Xiang 		}
6450d823b42SGao Xiang 	} else if (ret) {
6460d823b42SGao Xiang 		return ret;
6470d823b42SGao Xiang 	}
64806a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
649*387bab87SGao Xiang 				Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
65081382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
6515c6dcc57SGao Xiang 	fe->icpage_ptr = fe->pcl->compressed_pages +
6525c6dcc57SGao Xiang 			z_erofs_pclusterpages(fe->pcl);
65347e4937aSGao Xiang 	return 0;
65447e4937aSGao Xiang }
65547e4937aSGao Xiang 
65647e4937aSGao Xiang /*
65747e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
65847e4937aSGao Xiang  * only after a RCU grace period.
65947e4937aSGao Xiang  */
66047e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
66147e4937aSGao Xiang {
66287ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
66387ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
66447e4937aSGao Xiang }
66547e4937aSGao Xiang 
66647e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
66747e4937aSGao Xiang {
66847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
66947e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
67047e4937aSGao Xiang 
67187ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
67247e4937aSGao Xiang }
67347e4937aSGao Xiang 
6745c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
67547e4937aSGao Xiang {
67687ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
67747e4937aSGao Xiang 
67887ca34a7SGao Xiang 	if (!pcl)
67947e4937aSGao Xiang 		return false;
68047e4937aSGao Xiang 
68106a304cdSGao Xiang 	z_erofs_bvec_iter_end(&fe->biter);
68287ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
68347e4937aSGao Xiang 
68406a304cdSGao Xiang 	if (fe->candidate_bvpage) {
68506a304cdSGao Xiang 		DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
68606a304cdSGao Xiang 		fe->candidate_bvpage = NULL;
68706a304cdSGao Xiang 	}
68806a304cdSGao Xiang 
68947e4937aSGao Xiang 	/*
69047e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
69147e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
69247e4937aSGao Xiang 	 */
6935c6dcc57SGao Xiang 	if (fe->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
69487ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
69547e4937aSGao Xiang 
69687ca34a7SGao Xiang 	fe->pcl = NULL;
69747e4937aSGao Xiang 	return true;
69847e4937aSGao Xiang }
69947e4937aSGao Xiang 
70047e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
70147e4937aSGao Xiang 				       unsigned int cachestrategy,
70247e4937aSGao Xiang 				       erofs_off_t la)
70347e4937aSGao Xiang {
70447e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
70547e4937aSGao Xiang 		return false;
70647e4937aSGao Xiang 
70747e4937aSGao Xiang 	if (fe->backmost)
70847e4937aSGao Xiang 		return true;
70947e4937aSGao Xiang 
71047e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
71147e4937aSGao Xiang 		la < fe->headoffset;
71247e4937aSGao Xiang }
71347e4937aSGao Xiang 
71447e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
715eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
71647e4937aSGao Xiang {
71747e4937aSGao Xiang 	struct inode *const inode = fe->inode;
718bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
71947e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
72047e4937aSGao Xiang 	const loff_t offset = page_offset(page);
721dc76ea8cSGao Xiang 	bool tight = true;
72247e4937aSGao Xiang 
72347e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
72447e4937aSGao Xiang 	enum z_erofs_page_type page_type;
72547e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
72647e4937aSGao Xiang 	int err = 0;
72747e4937aSGao Xiang 
72847e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
72947e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
73047e4937aSGao Xiang 
73147e4937aSGao Xiang 	spiltted = 0;
73247e4937aSGao Xiang 	end = PAGE_SIZE;
73347e4937aSGao Xiang repeat:
73447e4937aSGao Xiang 	cur = end - 1;
73547e4937aSGao Xiang 
73639397a46SGao Xiang 	if (offset + cur < map->m_la ||
73739397a46SGao Xiang 	    offset + cur >= map->m_la + map->m_llen) {
73839397a46SGao Xiang 		erofs_dbg("out-of-range map @ pos %llu", offset + cur);
73947e4937aSGao Xiang 
7405c6dcc57SGao Xiang 		if (z_erofs_collector_end(fe))
74147e4937aSGao Xiang 			fe->backmost = false;
74247e4937aSGao Xiang 		map->m_la = offset + cur;
74347e4937aSGao Xiang 		map->m_llen = 0;
74447e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
7458d8a09b0SGao Xiang 		if (err)
74647e4937aSGao Xiang 			goto err_out;
74739397a46SGao Xiang 	} else {
74839397a46SGao Xiang 		if (fe->pcl)
74939397a46SGao Xiang 			goto hitted;
75039397a46SGao Xiang 		/* didn't get a valid pcluster previously (very rare) */
75139397a46SGao Xiang 	}
75247e4937aSGao Xiang 
7538d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
75447e4937aSGao Xiang 		goto hitted;
75547e4937aSGao Xiang 
75683a386c0SGao Xiang 	err = z_erofs_collector_begin(fe);
7578d8a09b0SGao Xiang 	if (err)
75847e4937aSGao Xiang 		goto err_out;
75947e4937aSGao Xiang 
7605c6dcc57SGao Xiang 	if (z_erofs_is_inline_pcluster(fe->pcl)) {
76109c54379SGao Xiang 		void *mp;
762cecf864dSYue Hu 
76309c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
76409c54379SGao Xiang 					erofs_blknr(map->m_pa), EROFS_NO_KMAP);
76509c54379SGao Xiang 		if (IS_ERR(mp)) {
76609c54379SGao Xiang 			err = PTR_ERR(mp);
767cecf864dSYue Hu 			erofs_err(inode->i_sb,
768cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
769cecf864dSYue Hu 			goto err_out;
770cecf864dSYue Hu 		}
77109c54379SGao Xiang 		get_page(fe->map.buf.page);
7725c6dcc57SGao Xiang 		WRITE_ONCE(fe->pcl->compressed_pages[0], fe->map.buf.page);
7735c6dcc57SGao Xiang 		fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
774cecf864dSYue Hu 	} else {
7756f39d1e1SGao Xiang 		/* bind cache first when cached decompression is preferred */
776cecf864dSYue Hu 		if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy,
777cecf864dSYue Hu 					       map->m_la))
7781825c8d7SGao Xiang 			cache_strategy = TRYALLOC;
77947e4937aSGao Xiang 		else
78047e4937aSGao Xiang 			cache_strategy = DONTALLOC;
78147e4937aSGao Xiang 
7826f39d1e1SGao Xiang 		z_erofs_bind_cache(fe, cache_strategy, pagepool);
783cecf864dSYue Hu 	}
78447e4937aSGao Xiang hitted:
785dc76ea8cSGao Xiang 	/*
786dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
787dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
788dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
789*387bab87SGao Xiang 	 * for inplace I/O or bvpage (should be processed in a strict order.)
790dc76ea8cSGao Xiang 	 */
7915c6dcc57SGao Xiang 	tight &= (fe->mode >= COLLECT_PRIMARY_HOOKED &&
7925c6dcc57SGao Xiang 		  fe->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
793dc76ea8cSGao Xiang 
79447e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
7958d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
79647e4937aSGao Xiang 		zero_user_segment(page, cur, end);
79747e4937aSGao Xiang 		goto next_part;
79847e4937aSGao Xiang 	}
79947e4937aSGao Xiang 
80047e4937aSGao Xiang 	/* let's derive page type */
80147e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
80247e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
80347e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
80447e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
80547e4937aSGao Xiang 
80647e4937aSGao Xiang 	if (cur)
8075c6dcc57SGao Xiang 		tight &= (fe->mode >= COLLECT_PRIMARY_FOLLOWED);
80847e4937aSGao Xiang 
80947e4937aSGao Xiang retry:
81006a304cdSGao Xiang 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
81106a304cdSGao Xiang 					.page = page,
81206a304cdSGao Xiang 					.offset = offset - map->m_la,
81306a304cdSGao Xiang 					.end = end,
81406a304cdSGao Xiang 				  }), page_type);
81506a304cdSGao Xiang 	/* should allocate an additional short-lived page for bvset */
81606a304cdSGao Xiang 	if (err == -EAGAIN && !fe->candidate_bvpage) {
81706a304cdSGao Xiang 		fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
81806a304cdSGao Xiang 		set_page_private(fe->candidate_bvpage,
81906a304cdSGao Xiang 				 Z_EROFS_SHORTLIVED_PAGE);
82047e4937aSGao Xiang 		goto retry;
82147e4937aSGao Xiang 	}
82247e4937aSGao Xiang 
82306a304cdSGao Xiang 	if (err) {
82406a304cdSGao Xiang 		DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
82547e4937aSGao Xiang 		goto err_out;
82606a304cdSGao Xiang 	}
82747e4937aSGao Xiang 
82847e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
82947e4937aSGao Xiang 
83047e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
83147e4937aSGao Xiang 
83247e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
83347e4937aSGao Xiang 	++spiltted;
83447e4937aSGao Xiang 	/* also update nr_pages */
83587ca34a7SGao Xiang 	fe->pcl->nr_pages = max_t(pgoff_t, fe->pcl->nr_pages, index + 1);
83647e4937aSGao Xiang next_part:
83747e4937aSGao Xiang 	/* can be used for verification */
83847e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
83947e4937aSGao Xiang 
84047e4937aSGao Xiang 	end = cur;
84147e4937aSGao Xiang 	if (end > 0)
84247e4937aSGao Xiang 		goto repeat;
84347e4937aSGao Xiang 
84447e4937aSGao Xiang out:
84547e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
84647e4937aSGao Xiang 
8474f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
84847e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
84947e4937aSGao Xiang 	return err;
85047e4937aSGao Xiang 
85147e4937aSGao Xiang 	/* if some error occurred while processing this page */
85247e4937aSGao Xiang err_out:
85347e4937aSGao Xiang 	SetPageError(page);
85447e4937aSGao Xiang 	goto out;
85547e4937aSGao Xiang }
85647e4937aSGao Xiang 
85740452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
85840452ffcSHuang Jianan 				       unsigned int readahead_pages)
85940452ffcSHuang Jianan {
860a2e20a25SMatthew Wilcox (Oracle) 	/* auto: enable for read_folio, disable for readahead */
86140452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
86240452ffcSHuang Jianan 	    !readahead_pages)
86340452ffcSHuang Jianan 		return true;
86440452ffcSHuang Jianan 
86540452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
86640452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
86740452ffcSHuang Jianan 		return true;
86840452ffcSHuang Jianan 
86940452ffcSHuang Jianan 	return false;
87040452ffcSHuang Jianan }
87140452ffcSHuang Jianan 
8726aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
8736aaa7b06SGao Xiang {
8746aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
8756aaa7b06SGao Xiang }
8766aaa7b06SGao Xiang 
87742fec235SGao Xiang static int z_erofs_parse_out_bvecs(struct z_erofs_pcluster *pcl,
87842fec235SGao Xiang 				   struct page **pages, struct page **pagepool)
87942fec235SGao Xiang {
88006a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
88106a304cdSGao Xiang 	struct page *old_bvpage;
88242fec235SGao Xiang 	int i, err = 0;
88342fec235SGao Xiang 
884*387bab87SGao Xiang 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
88542fec235SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
88606a304cdSGao Xiang 		struct z_erofs_bvec bvec;
88742fec235SGao Xiang 		unsigned int pagenr;
88842fec235SGao Xiang 
88906a304cdSGao Xiang 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
89042fec235SGao Xiang 
89106a304cdSGao Xiang 		if (old_bvpage)
89206a304cdSGao Xiang 			z_erofs_put_shortlivedpage(pagepool, old_bvpage);
89342fec235SGao Xiang 
89406a304cdSGao Xiang 		pagenr = (bvec.offset + pcl->pageofs_out) >> PAGE_SHIFT;
89542fec235SGao Xiang 		DBG_BUGON(pagenr >= pcl->nr_pages);
89606a304cdSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
89742fec235SGao Xiang 		/*
89842fec235SGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
89942fec235SGao Xiang 		 * so here erroring out one multiref page.
90042fec235SGao Xiang 		 */
90142fec235SGao Xiang 		if (pages[pagenr]) {
90242fec235SGao Xiang 			DBG_BUGON(1);
90342fec235SGao Xiang 			SetPageError(pages[pagenr]);
90442fec235SGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
90542fec235SGao Xiang 			err = -EFSCORRUPTED;
90642fec235SGao Xiang 		}
90706a304cdSGao Xiang 		pages[pagenr] = bvec.page;
90842fec235SGao Xiang 	}
90906a304cdSGao Xiang 
91006a304cdSGao Xiang 	old_bvpage = z_erofs_bvec_iter_end(&biter);
91106a304cdSGao Xiang 	if (old_bvpage)
91206a304cdSGao Xiang 		z_erofs_put_shortlivedpage(pagepool, old_bvpage);
91342fec235SGao Xiang 	return err;
91442fec235SGao Xiang }
91542fec235SGao Xiang 
91647e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
91747e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
918eaa9172aSGao Xiang 				       struct page **pagepool)
91947e4937aSGao Xiang {
92047e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
921cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
9229f6cc76eSGao Xiang 	unsigned int i, inputsize, outputsize, llen, nr_pages;
92347e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
92447e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
92547e4937aSGao Xiang 
92647e4937aSGao Xiang 	bool overlapped, partial;
92747e4937aSGao Xiang 	int err;
92847e4937aSGao Xiang 
92947e4937aSGao Xiang 	might_sleep();
93087ca34a7SGao Xiang 	DBG_BUGON(!READ_ONCE(pcl->nr_pages));
93147e4937aSGao Xiang 
93287ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
93387ca34a7SGao Xiang 	nr_pages = pcl->nr_pages;
93447e4937aSGao Xiang 
9358d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
93647e4937aSGao Xiang 		pages = pages_onstack;
93747e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
93847e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
93947e4937aSGao Xiang 		pages = z_pagemap_global;
94047e4937aSGao Xiang 	} else {
94147e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
94247e4937aSGao Xiang 
94347e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
94447e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
94547e4937aSGao Xiang 
94647e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
94747e4937aSGao Xiang 				       gfp_flags);
94847e4937aSGao Xiang 
94947e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
9508d8a09b0SGao Xiang 		if (!pages) {
95147e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
95247e4937aSGao Xiang 			pages = z_pagemap_global;
95347e4937aSGao Xiang 		}
95447e4937aSGao Xiang 	}
95547e4937aSGao Xiang 
95647e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
95747e4937aSGao Xiang 		pages[i] = NULL;
95847e4937aSGao Xiang 
95942fec235SGao Xiang 	err = z_erofs_parse_out_bvecs(pcl, pages, pagepool);
96047e4937aSGao Xiang 
96147e4937aSGao Xiang 	overlapped = false;
96247e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
96347e4937aSGao Xiang 
964cecf864dSYue Hu 	for (i = 0; i < pclusterpages; ++i) {
96547e4937aSGao Xiang 		unsigned int pagenr;
96647e4937aSGao Xiang 
96747e4937aSGao Xiang 		page = compressed_pages[i];
96847e4937aSGao Xiang 		/* all compressed pages ought to be valid */
96947e4937aSGao Xiang 		DBG_BUGON(!page);
97047e4937aSGao Xiang 
971cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
972cecf864dSYue Hu 			if (!PageUptodate(page))
973cecf864dSYue Hu 				err = -EIO;
974cecf864dSYue Hu 			continue;
975cecf864dSYue Hu 		}
976cecf864dSYue Hu 
977cecf864dSYue Hu 		DBG_BUGON(z_erofs_page_is_invalidated(page));
9786aaa7b06SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
97947e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
9808d8a09b0SGao Xiang 				if (!PageUptodate(page))
98147e4937aSGao Xiang 					err = -EIO;
98247e4937aSGao Xiang 				continue;
98347e4937aSGao Xiang 			}
98447e4937aSGao Xiang 
98547e4937aSGao Xiang 			/*
98647e4937aSGao Xiang 			 * only if non-head page can be selected
98747e4937aSGao Xiang 			 * for inplace decompression
98847e4937aSGao Xiang 			 */
98947e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
99047e4937aSGao Xiang 
99147e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
9928d8a09b0SGao Xiang 			if (pages[pagenr]) {
99347e4937aSGao Xiang 				DBG_BUGON(1);
99447e4937aSGao Xiang 				SetPageError(pages[pagenr]);
99547e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
99647e4937aSGao Xiang 				err = -EFSCORRUPTED;
99747e4937aSGao Xiang 			}
99847e4937aSGao Xiang 			pages[pagenr] = page;
99947e4937aSGao Xiang 
100047e4937aSGao Xiang 			overlapped = true;
100147e4937aSGao Xiang 		}
100247e4937aSGao Xiang 
10036aaa7b06SGao Xiang 		/* PG_error needs checking for all non-managed pages */
10048d8a09b0SGao Xiang 		if (PageError(page)) {
100547e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
100647e4937aSGao Xiang 			err = -EIO;
100747e4937aSGao Xiang 		}
100847e4937aSGao Xiang 	}
100947e4937aSGao Xiang 
10108d8a09b0SGao Xiang 	if (err)
101147e4937aSGao Xiang 		goto out;
101247e4937aSGao Xiang 
101347e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
101487ca34a7SGao Xiang 	if (nr_pages << PAGE_SHIFT >= pcl->pageofs_out + llen) {
101547e4937aSGao Xiang 		outputsize = llen;
101647e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
101747e4937aSGao Xiang 	} else {
101887ca34a7SGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - pcl->pageofs_out;
101947e4937aSGao Xiang 		partial = true;
102047e4937aSGao Xiang 	}
102147e4937aSGao Xiang 
1022cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1023cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1024cecf864dSYue Hu 	else
1025cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1026cecf864dSYue Hu 
102747e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
102847e4937aSGao Xiang 					.sb = sb,
102947e4937aSGao Xiang 					.in = compressed_pages,
103047e4937aSGao Xiang 					.out = pages,
1031cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
103287ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
10339f6cc76eSGao Xiang 					.inputsize = inputsize,
103447e4937aSGao Xiang 					.outputsize = outputsize,
103547e4937aSGao Xiang 					.alg = pcl->algorithmformat,
103647e4937aSGao Xiang 					.inplace_io = overlapped,
103747e4937aSGao Xiang 					.partial_decoding = partial
103847e4937aSGao Xiang 				 }, pagepool);
103947e4937aSGao Xiang 
104047e4937aSGao Xiang out:
1041cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1042cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1043cecf864dSYue Hu 		page = compressed_pages[0];
1044cecf864dSYue Hu 		WRITE_ONCE(compressed_pages[0], NULL);
1045cecf864dSYue Hu 		put_page(page);
1046cecf864dSYue Hu 	} else {
1047cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
104847e4937aSGao Xiang 			page = compressed_pages[i];
104947e4937aSGao Xiang 
105047e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
105147e4937aSGao Xiang 				continue;
105247e4937aSGao Xiang 
10536aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
10546aaa7b06SGao Xiang 			(void)z_erofs_put_shortlivedpage(pagepool, page);
105547e4937aSGao Xiang 			WRITE_ONCE(compressed_pages[i], NULL);
105647e4937aSGao Xiang 		}
1057cecf864dSYue Hu 	}
105847e4937aSGao Xiang 
105947e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
106047e4937aSGao Xiang 		page = pages[i];
106147e4937aSGao Xiang 		if (!page)
106247e4937aSGao Xiang 			continue;
106347e4937aSGao Xiang 
10646aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
106547e4937aSGao Xiang 
10666aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
10676aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
106847e4937aSGao Xiang 			continue;
106947e4937aSGao Xiang 
10708d8a09b0SGao Xiang 		if (err < 0)
107147e4937aSGao Xiang 			SetPageError(page);
107247e4937aSGao Xiang 
107347e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
107447e4937aSGao Xiang 	}
107547e4937aSGao Xiang 
107647e4937aSGao Xiang 	if (pages == z_pagemap_global)
107747e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
10788d8a09b0SGao Xiang 	else if (pages != pages_onstack)
107947e4937aSGao Xiang 		kvfree(pages);
108047e4937aSGao Xiang 
108187ca34a7SGao Xiang 	pcl->nr_pages = 0;
108206a304cdSGao Xiang 	pcl->bvset.nextpage = NULL;
108387ca34a7SGao Xiang 	pcl->vcnt = 0;
108447e4937aSGao Xiang 
108587ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
108647e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
108787ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
108847e4937aSGao Xiang 	return err;
108947e4937aSGao Xiang }
109047e4937aSGao Xiang 
10910c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1092eaa9172aSGao Xiang 				     struct page **pagepool)
109347e4937aSGao Xiang {
109447e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
109547e4937aSGao Xiang 
109647e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
109747e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
109847e4937aSGao Xiang 
109947e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
110047e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
110147e4937aSGao Xiang 
110247e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
110347e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
110447e4937aSGao Xiang 
110547e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
110647e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
110747e4937aSGao Xiang 
1108a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
110987ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
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 
11267865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
11277865827cSGao Xiang 				       bool sync, int bios)
11287865827cSGao Xiang {
11297865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
11307865827cSGao Xiang 
11317865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
11327865827cSGao Xiang 	if (sync) {
11337865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
113460b30050SHongyu Jin 			complete(&io->u.done);
113560b30050SHongyu Jin 
11367865827cSGao Xiang 		return;
11377865827cSGao Xiang 	}
11387865827cSGao Xiang 
11397865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
11407865827cSGao Xiang 		return;
11417865827cSGao Xiang 	/* Use workqueue and sync decompression for atomic contexts only */
11427865827cSGao Xiang 	if (in_atomic() || irqs_disabled()) {
11437865827cSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
11447865827cSGao Xiang 		/* enable sync decompression for readahead */
11457865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
11467865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
11477865827cSGao Xiang 		return;
11487865827cSGao Xiang 	}
11497865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
11507865827cSGao Xiang }
11517865827cSGao Xiang 
115247e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
115347e4937aSGao Xiang 					       unsigned int nr,
1154eaa9172aSGao Xiang 					       struct page **pagepool,
11559f2731d6SGao Xiang 					       struct address_space *mc)
115647e4937aSGao Xiang {
115747e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
11589f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
115947e4937aSGao Xiang 	bool tocache = false;
116047e4937aSGao Xiang 
116147e4937aSGao Xiang 	struct address_space *mapping;
116247e4937aSGao Xiang 	struct page *oldpage, *page;
116347e4937aSGao Xiang 
116447e4937aSGao Xiang 	compressed_page_t t;
116547e4937aSGao Xiang 	int justfound;
116647e4937aSGao Xiang 
116747e4937aSGao Xiang repeat:
116847e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
116947e4937aSGao Xiang 	oldpage = page;
117047e4937aSGao Xiang 
117147e4937aSGao Xiang 	if (!page)
117247e4937aSGao Xiang 		goto out_allocpage;
117347e4937aSGao Xiang 
117447e4937aSGao Xiang 	/* process the target tagged pointer */
117547e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
117647e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
117747e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
117847e4937aSGao Xiang 
11791825c8d7SGao Xiang 	/*
11801825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
11811825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
11821825c8d7SGao Xiang 	 */
11831825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
11841825c8d7SGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
11851825c8d7SGao Xiang 		set_page_private(page, 0);
11861825c8d7SGao Xiang 		tocache = true;
11871825c8d7SGao Xiang 		goto out_tocache;
11881825c8d7SGao Xiang 	}
118947e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
119047e4937aSGao Xiang 
119147e4937aSGao Xiang 	/*
11926aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
119347e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
119447e4937aSGao Xiang 	 */
119547e4937aSGao Xiang 	if (mapping && mapping != mc)
119647e4937aSGao Xiang 		/* ought to be unmanaged pages */
119747e4937aSGao Xiang 		goto out;
119847e4937aSGao Xiang 
11996aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
12006aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
12016aaa7b06SGao Xiang 		goto out;
12026aaa7b06SGao Xiang 
120347e4937aSGao Xiang 	lock_page(page);
120447e4937aSGao Xiang 
120547e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
120647e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
120747e4937aSGao Xiang 
120847e4937aSGao Xiang 	/* the page is still in manage cache */
120947e4937aSGao Xiang 	if (page->mapping == mc) {
121047e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
121147e4937aSGao Xiang 
121247e4937aSGao Xiang 		ClearPageError(page);
121347e4937aSGao Xiang 		if (!PagePrivate(page)) {
121447e4937aSGao Xiang 			/*
121547e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
121647e4937aSGao Xiang 			 * the current restriction as well if
121747e4937aSGao Xiang 			 * the page is already in compressed_pages[].
121847e4937aSGao Xiang 			 */
121947e4937aSGao Xiang 			DBG_BUGON(!justfound);
122047e4937aSGao Xiang 
122147e4937aSGao Xiang 			justfound = 0;
122247e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
122347e4937aSGao Xiang 			SetPagePrivate(page);
122447e4937aSGao Xiang 		}
122547e4937aSGao Xiang 
122647e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
122747e4937aSGao Xiang 		if (PageUptodate(page)) {
122847e4937aSGao Xiang 			unlock_page(page);
122947e4937aSGao Xiang 			page = NULL;
123047e4937aSGao Xiang 		}
123147e4937aSGao Xiang 		goto out;
123247e4937aSGao Xiang 	}
123347e4937aSGao Xiang 
123447e4937aSGao Xiang 	/*
123547e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
123647e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
123747e4937aSGao Xiang 	 */
123847e4937aSGao Xiang 	DBG_BUGON(page->mapping);
123947e4937aSGao Xiang 	DBG_BUGON(!justfound);
124047e4937aSGao Xiang 
124147e4937aSGao Xiang 	tocache = true;
124247e4937aSGao Xiang 	unlock_page(page);
124347e4937aSGao Xiang 	put_page(page);
124447e4937aSGao Xiang out_allocpage:
12455ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
12465ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1247eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
12485ddcee1fSGao Xiang 		cond_resched();
12495ddcee1fSGao Xiang 		goto repeat;
12505ddcee1fSGao Xiang 	}
12511825c8d7SGao Xiang out_tocache:
1252bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1253bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1254bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1255bf225074SGao Xiang 		goto out;
1256a30573b3SGao Xiang 	}
1257bf225074SGao Xiang 	attach_page_private(page, pcl);
1258bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1259bf225074SGao Xiang 	put_page(page);
1260bf225074SGao Xiang 
126147e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
126247e4937aSGao Xiang 	return page;
126347e4937aSGao Xiang }
126447e4937aSGao Xiang 
1265a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1266a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1267a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
126847e4937aSGao Xiang {
1269a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
127047e4937aSGao Xiang 
1271a4b1fab1SGao Xiang 	if (fg && !*fg) {
1272a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1273a4b1fab1SGao Xiang 		if (!q) {
1274a4b1fab1SGao Xiang 			*fg = true;
1275a4b1fab1SGao Xiang 			goto fg_out;
127647e4937aSGao Xiang 		}
12770c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1278a4b1fab1SGao Xiang 	} else {
1279a4b1fab1SGao Xiang fg_out:
1280a4b1fab1SGao Xiang 		q = fgq;
128160b30050SHongyu Jin 		init_completion(&fgq->u.done);
1282a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1283a4b1fab1SGao Xiang 	}
1284a4b1fab1SGao Xiang 	q->sb = sb;
1285a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1286a4b1fab1SGao Xiang 	return q;
128747e4937aSGao Xiang }
128847e4937aSGao Xiang 
128947e4937aSGao Xiang /* define decompression jobqueue types */
129047e4937aSGao Xiang enum {
129147e4937aSGao Xiang 	JQ_BYPASS,
129247e4937aSGao Xiang 	JQ_SUBMIT,
129347e4937aSGao Xiang 	NR_JOBQUEUES,
129447e4937aSGao Xiang };
129547e4937aSGao Xiang 
129647e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1297a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1298a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
129947e4937aSGao Xiang {
130047e4937aSGao Xiang 	/*
130147e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
130247e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
130347e4937aSGao Xiang 	 */
1304a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1305a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
130647e4937aSGao Xiang 
1307a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
130847e4937aSGao Xiang }
130947e4937aSGao Xiang 
131047e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
131147e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
131247e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
131347e4937aSGao Xiang {
131447e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
131547e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
131647e4937aSGao Xiang 
131747e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
131847e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
131947e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
132047e4937aSGao Xiang 
132147e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
132247e4937aSGao Xiang 
132347e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
132447e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
132547e4937aSGao Xiang 
132647e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
132747e4937aSGao Xiang }
132847e4937aSGao Xiang 
13297865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
13307865827cSGao Xiang {
13317865827cSGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
13327865827cSGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
13337865827cSGao Xiang 	blk_status_t err = bio->bi_status;
13347865827cSGao Xiang 	struct bio_vec *bvec;
13357865827cSGao Xiang 	struct bvec_iter_all iter_all;
13367865827cSGao Xiang 
13377865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
13387865827cSGao Xiang 		struct page *page = bvec->bv_page;
13397865827cSGao Xiang 
13407865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
13417865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
13427865827cSGao Xiang 
13437865827cSGao Xiang 		if (err)
13447865827cSGao Xiang 			SetPageError(page);
13457865827cSGao Xiang 
13467865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
13477865827cSGao Xiang 			if (!err)
13487865827cSGao Xiang 				SetPageUptodate(page);
13497865827cSGao Xiang 			unlock_page(page);
13507865827cSGao Xiang 		}
13517865827cSGao Xiang 	}
13527865827cSGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
13537865827cSGao Xiang 	bio_put(bio);
13547865827cSGao Xiang }
13557865827cSGao Xiang 
135683a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1357eaa9172aSGao Xiang 				 struct page **pagepool,
1358a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1359a4b1fab1SGao Xiang 				 bool *force_fg)
136047e4937aSGao Xiang {
136183a386c0SGao Xiang 	struct super_block *sb = f->inode->i_sb;
136283a386c0SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
136347e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1364a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
136547e4937aSGao Xiang 	void *bi_private;
13665c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1367dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
13683f649ab7SKees Cook 	pgoff_t last_index;
1369dfeab2e9SGao Xiang 	struct block_device *last_bdev;
13701e4a2955SGao Xiang 	unsigned int nr_bios = 0;
13711e4a2955SGao Xiang 	struct bio *bio = NULL;
137247e4937aSGao Xiang 
1373a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1374a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1375a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
137647e4937aSGao Xiang 
137747e4937aSGao Xiang 	/* by default, all need io submission */
137847e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
137947e4937aSGao Xiang 
138047e4937aSGao Xiang 	do {
1381dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
138247e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
13831e4a2955SGao Xiang 		pgoff_t cur, end;
13841e4a2955SGao Xiang 		unsigned int i = 0;
13851e4a2955SGao Xiang 		bool bypass = true;
138647e4937aSGao Xiang 
138747e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
138847e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
138947e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
139047e4937aSGao Xiang 
139147e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
139247e4937aSGao Xiang 
1393cecf864dSYue Hu 		/* close the main owned chain at first */
1394cecf864dSYue Hu 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1395cecf864dSYue Hu 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1396cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1397cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1398cecf864dSYue Hu 			continue;
1399cecf864dSYue Hu 		}
1400cecf864dSYue Hu 
1401dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1402dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
1403dfeab2e9SGao Xiang 			.m_pa = blknr_to_addr(pcl->obj.index),
1404dfeab2e9SGao Xiang 		};
1405dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1406dfeab2e9SGao Xiang 
1407dfeab2e9SGao Xiang 		cur = erofs_blknr(mdev.m_pa);
14089f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
140947e4937aSGao Xiang 
14101e4a2955SGao Xiang 		do {
14111e4a2955SGao Xiang 			struct page *page;
141247e4937aSGao Xiang 
14131e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
141483a386c0SGao Xiang 							  mc);
14151e4a2955SGao Xiang 			if (!page)
14161e4a2955SGao Xiang 				continue;
141747e4937aSGao Xiang 
1418dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1419dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
142047e4937aSGao Xiang submit_bio_retry:
142194e4e153SGao Xiang 				submit_bio(bio);
142247e4937aSGao Xiang 				bio = NULL;
142347e4937aSGao Xiang 			}
142447e4937aSGao Xiang 
142547e4937aSGao Xiang 			if (!bio) {
142607888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
142707888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
14280c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1429dfeab2e9SGao Xiang 
1430dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
14311e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1432a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1433a5c0b780SGao Xiang 				bio->bi_private = bi_private;
14346ea5aad3SGao Xiang 				if (f->readahead)
14356ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
143647e4937aSGao Xiang 				++nr_bios;
143747e4937aSGao Xiang 			}
143847e4937aSGao Xiang 
14396c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
144047e4937aSGao Xiang 				goto submit_bio_retry;
144147e4937aSGao Xiang 
14421e4a2955SGao Xiang 			last_index = cur;
14431e4a2955SGao Xiang 			bypass = false;
14441e4a2955SGao Xiang 		} while (++cur < end);
144547e4937aSGao Xiang 
14461e4a2955SGao Xiang 		if (!bypass)
144747e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
144847e4937aSGao Xiang 		else
144947e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
145047e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
145147e4937aSGao Xiang 
145247e4937aSGao Xiang 	if (bio)
145394e4e153SGao Xiang 		submit_bio(bio);
145447e4937aSGao Xiang 
1455587a67b7SGao Xiang 	/*
1456587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1457587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1458587a67b7SGao Xiang 	 */
1459587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1460587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
14611e4a2955SGao Xiang 		return;
1462587a67b7SGao Xiang 	}
1463a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
146447e4937aSGao Xiang }
146547e4937aSGao Xiang 
146683a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1467eaa9172aSGao Xiang 			     struct page **pagepool, bool force_fg)
146847e4937aSGao Xiang {
1469a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
147047e4937aSGao Xiang 
14715c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
147247e4937aSGao Xiang 		return;
147383a386c0SGao Xiang 	z_erofs_submit_queue(f, pagepool, io, &force_fg);
147447e4937aSGao Xiang 
14750c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
14760c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
147747e4937aSGao Xiang 
147847e4937aSGao Xiang 	if (!force_fg)
147947e4937aSGao Xiang 		return;
148047e4937aSGao Xiang 
148147e4937aSGao Xiang 	/* wait until all bios are completed */
148260b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
148347e4937aSGao Xiang 
14840c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
14850c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
148647e4937aSGao Xiang }
148747e4937aSGao Xiang 
148838629291SGao Xiang /*
148938629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
149038629291SGao Xiang  * approximate readmore strategies as a start.
149138629291SGao Xiang  */
149238629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
149338629291SGao Xiang 				      struct readahead_control *rac,
149438629291SGao Xiang 				      erofs_off_t end,
1495eaa9172aSGao Xiang 				      struct page **pagepool,
149638629291SGao Xiang 				      bool backmost)
149738629291SGao Xiang {
149838629291SGao Xiang 	struct inode *inode = f->inode;
149938629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
150038629291SGao Xiang 	erofs_off_t cur;
150138629291SGao Xiang 	int err;
150238629291SGao Xiang 
150338629291SGao Xiang 	if (backmost) {
150438629291SGao Xiang 		map->m_la = end;
1505622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1506622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
150738629291SGao Xiang 		if (err)
150838629291SGao Xiang 			return;
150938629291SGao Xiang 
151038629291SGao Xiang 		/* expend ra for the trailing edge if readahead */
151138629291SGao Xiang 		if (rac) {
151238629291SGao Xiang 			loff_t newstart = readahead_pos(rac);
151338629291SGao Xiang 
151438629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
151538629291SGao Xiang 			readahead_expand(rac, newstart, cur - newstart);
151638629291SGao Xiang 			return;
151738629291SGao Xiang 		}
151838629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
151938629291SGao Xiang 	} else {
152038629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
152138629291SGao Xiang 
152238629291SGao Xiang 		if (!map->m_llen)
152338629291SGao Xiang 			return;
152438629291SGao Xiang 	}
152538629291SGao Xiang 
152638629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
152738629291SGao Xiang 	while (cur >= end) {
152838629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
152938629291SGao Xiang 		struct page *page;
153038629291SGao Xiang 
153138629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1532aa793b46SGao Xiang 		if (page) {
153338629291SGao Xiang 			if (PageUptodate(page)) {
153438629291SGao Xiang 				unlock_page(page);
1535aa793b46SGao Xiang 			} else {
153638629291SGao Xiang 				err = z_erofs_do_read_page(f, page, pagepool);
153738629291SGao Xiang 				if (err)
153838629291SGao Xiang 					erofs_err(inode->i_sb,
153938629291SGao Xiang 						  "readmore error at page %lu @ nid %llu",
154038629291SGao Xiang 						  index, EROFS_I(inode)->nid);
1541aa793b46SGao Xiang 			}
154238629291SGao Xiang 			put_page(page);
1543aa793b46SGao Xiang 		}
1544aa793b46SGao Xiang 
154538629291SGao Xiang 		if (cur < PAGE_SIZE)
154638629291SGao Xiang 			break;
154738629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
154838629291SGao Xiang 	}
154938629291SGao Xiang }
155038629291SGao Xiang 
1551a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio)
155247e4937aSGao Xiang {
1553a2e20a25SMatthew Wilcox (Oracle) 	struct page *page = &folio->page;
155447e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
155540452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
155647e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1557eaa9172aSGao Xiang 	struct page *pagepool = NULL;
155847e4937aSGao Xiang 	int err;
155947e4937aSGao Xiang 
156047e4937aSGao Xiang 	trace_erofs_readpage(page, false);
156147e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
156247e4937aSGao Xiang 
156338629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
156438629291SGao Xiang 				  &pagepool, true);
15651825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
156638629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
156738629291SGao Xiang 
15685c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
156947e4937aSGao Xiang 
157047e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
157183a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
157240452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, 0));
157347e4937aSGao Xiang 
157447e4937aSGao Xiang 	if (err)
15754f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
157647e4937aSGao Xiang 
157709c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1578eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
157947e4937aSGao Xiang 	return err;
158047e4937aSGao Xiang }
158147e4937aSGao Xiang 
15820615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
158347e4937aSGao Xiang {
15840615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
158547e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
158647e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1587eaa9172aSGao Xiang 	struct page *pagepool = NULL, *head = NULL, *page;
158838629291SGao Xiang 	unsigned int nr_pages;
158947e4937aSGao Xiang 
15906ea5aad3SGao Xiang 	f.readahead = true;
15910615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
159247e4937aSGao Xiang 
159338629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, f.headoffset +
159438629291SGao Xiang 				  readahead_length(rac) - 1, &pagepool, true);
159538629291SGao Xiang 	nr_pages = readahead_count(rac);
159638629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
159738629291SGao Xiang 
15980615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
159947e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
160047e4937aSGao Xiang 		head = page;
160147e4937aSGao Xiang 	}
160247e4937aSGao Xiang 
160347e4937aSGao Xiang 	while (head) {
160447e4937aSGao Xiang 		struct page *page = head;
160547e4937aSGao Xiang 		int err;
160647e4937aSGao Xiang 
160747e4937aSGao Xiang 		/* traversal in reverse order */
160847e4937aSGao Xiang 		head = (void *)page_private(page);
160947e4937aSGao Xiang 
16101825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1611a5876e24SGao Xiang 		if (err)
16124f761fa2SGao Xiang 			erofs_err(inode->i_sb,
16134f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
16144f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
161547e4937aSGao Xiang 		put_page(page);
161647e4937aSGao Xiang 	}
161738629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
16185c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
161947e4937aSGao Xiang 
162083a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
162140452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
162209c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1623eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
162447e4937aSGao Xiang }
162547e4937aSGao Xiang 
16260c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
1627a2e20a25SMatthew Wilcox (Oracle) 	.read_folio = z_erofs_read_folio,
16280615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
162947e4937aSGao Xiang };
1630