xref: /openbmc/linux/fs/erofs/zdata.c (revision 06a304cd)
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/
5*06a304cdSGao 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*06a304cdSGao Xiang struct z_erofs_bvec_iter {
31*06a304cdSGao Xiang 	struct page *bvpage;
32*06a304cdSGao Xiang 	struct z_erofs_bvset *bvset;
33*06a304cdSGao Xiang 	unsigned int nr, cur;
34*06a304cdSGao Xiang };
35*06a304cdSGao Xiang 
36*06a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
37*06a304cdSGao Xiang {
38*06a304cdSGao Xiang 	if (iter->bvpage)
39*06a304cdSGao Xiang 		kunmap_local(iter->bvset);
40*06a304cdSGao Xiang 	return iter->bvpage;
41*06a304cdSGao Xiang }
42*06a304cdSGao Xiang 
43*06a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
44*06a304cdSGao Xiang {
45*06a304cdSGao Xiang 	unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
46*06a304cdSGao Xiang 	/* have to access nextpage in advance, otherwise it will be unmapped */
47*06a304cdSGao Xiang 	struct page *nextpage = iter->bvset->nextpage;
48*06a304cdSGao Xiang 	struct page *oldpage;
49*06a304cdSGao Xiang 
50*06a304cdSGao Xiang 	DBG_BUGON(!nextpage);
51*06a304cdSGao Xiang 	oldpage = z_erofs_bvec_iter_end(iter);
52*06a304cdSGao Xiang 	iter->bvpage = nextpage;
53*06a304cdSGao Xiang 	iter->bvset = kmap_local_page(nextpage);
54*06a304cdSGao Xiang 	iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
55*06a304cdSGao Xiang 	iter->cur = 0;
56*06a304cdSGao Xiang 	return oldpage;
57*06a304cdSGao Xiang }
58*06a304cdSGao Xiang 
59*06a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
60*06a304cdSGao Xiang 				    struct z_erofs_bvset_inline *bvset,
61*06a304cdSGao Xiang 				    unsigned int bootstrap_nr,
62*06a304cdSGao Xiang 				    unsigned int cur)
63*06a304cdSGao Xiang {
64*06a304cdSGao Xiang 	*iter = (struct z_erofs_bvec_iter) {
65*06a304cdSGao Xiang 		.nr = bootstrap_nr,
66*06a304cdSGao Xiang 		.bvset = (struct z_erofs_bvset *)bvset,
67*06a304cdSGao Xiang 	};
68*06a304cdSGao Xiang 
69*06a304cdSGao Xiang 	while (cur > iter->nr) {
70*06a304cdSGao Xiang 		cur -= iter->nr;
71*06a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
72*06a304cdSGao Xiang 	}
73*06a304cdSGao Xiang 	iter->cur = cur;
74*06a304cdSGao Xiang }
75*06a304cdSGao Xiang 
76*06a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
77*06a304cdSGao Xiang 				struct z_erofs_bvec *bvec,
78*06a304cdSGao Xiang 				struct page **candidate_bvpage)
79*06a304cdSGao Xiang {
80*06a304cdSGao Xiang 	if (iter->cur == iter->nr) {
81*06a304cdSGao Xiang 		if (!*candidate_bvpage)
82*06a304cdSGao Xiang 			return -EAGAIN;
83*06a304cdSGao Xiang 
84*06a304cdSGao Xiang 		DBG_BUGON(iter->bvset->nextpage);
85*06a304cdSGao Xiang 		iter->bvset->nextpage = *candidate_bvpage;
86*06a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
87*06a304cdSGao Xiang 
88*06a304cdSGao Xiang 		iter->bvset->nextpage = NULL;
89*06a304cdSGao Xiang 		*candidate_bvpage = NULL;
90*06a304cdSGao Xiang 	}
91*06a304cdSGao Xiang 	iter->bvset->bvec[iter->cur++] = *bvec;
92*06a304cdSGao Xiang 	return 0;
93*06a304cdSGao Xiang }
94*06a304cdSGao Xiang 
95*06a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
96*06a304cdSGao Xiang 				 struct z_erofs_bvec *bvec,
97*06a304cdSGao Xiang 				 struct page **old_bvpage)
98*06a304cdSGao Xiang {
99*06a304cdSGao Xiang 	if (iter->cur == iter->nr)
100*06a304cdSGao Xiang 		*old_bvpage = z_erofs_bvset_flip(iter);
101*06a304cdSGao Xiang 	else
102*06a304cdSGao Xiang 		*old_bvpage = NULL;
103*06a304cdSGao Xiang 	*bvec = iter->bvset->bvec[iter->cur++];
104*06a304cdSGao Xiang }
105*06a304cdSGao Xiang 
1069f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
1079f6cc76eSGao Xiang {
1089f6cc76eSGao Xiang 	int i;
1099f6cc76eSGao Xiang 
1109f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
1119f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
1129f6cc76eSGao Xiang 			continue;
1139f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
1149f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
1159f6cc76eSGao Xiang 	}
1169f6cc76eSGao Xiang }
1179f6cc76eSGao Xiang 
1189f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
1199f6cc76eSGao Xiang {
1209f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
1219f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
1229f6cc76eSGao Xiang 	unsigned int size;
1239f6cc76eSGao Xiang 
1249f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
1259f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
1269f6cc76eSGao Xiang 		size = struct_size(a, compressed_pages, pcs->maxpages);
1279f6cc76eSGao Xiang 
1289f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
1299f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
1309f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
1319f6cc76eSGao Xiang 		if (pcs->slab)
1329f6cc76eSGao Xiang 			continue;
1339f6cc76eSGao Xiang 
1349f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
1359f6cc76eSGao Xiang 		return -ENOMEM;
1369f6cc76eSGao Xiang 	}
1379f6cc76eSGao Xiang 	return 0;
1389f6cc76eSGao Xiang }
1399f6cc76eSGao Xiang 
1409f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
1419f6cc76eSGao Xiang {
1429f6cc76eSGao Xiang 	int i;
1439f6cc76eSGao Xiang 
1449f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
1459f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
1469f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
1479f6cc76eSGao Xiang 
1489f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
1499f6cc76eSGao Xiang 			continue;
1509f6cc76eSGao Xiang 
1519f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
1529f6cc76eSGao Xiang 		if (!pcl)
1539f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
1549f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
1559f6cc76eSGao Xiang 		return pcl;
1569f6cc76eSGao Xiang 	}
1579f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
1589f6cc76eSGao Xiang }
1599f6cc76eSGao Xiang 
1609f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
1619f6cc76eSGao Xiang {
162cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1639f6cc76eSGao Xiang 	int i;
1649f6cc76eSGao Xiang 
1659f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
1669f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
1679f6cc76eSGao Xiang 
168cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
1699f6cc76eSGao Xiang 			continue;
1709f6cc76eSGao Xiang 
1719f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
1729f6cc76eSGao Xiang 		return;
1739f6cc76eSGao Xiang 	}
1749f6cc76eSGao Xiang 	DBG_BUGON(1);
1759f6cc76eSGao Xiang }
1769f6cc76eSGao Xiang 
17747e4937aSGao Xiang /* how to allocate cached pages for a pcluster */
17847e4937aSGao Xiang enum z_erofs_cache_alloctype {
17947e4937aSGao Xiang 	DONTALLOC,	/* don't allocate any cached pages */
1801825c8d7SGao Xiang 	/*
1811825c8d7SGao Xiang 	 * try to use cached I/O if page allocation succeeds or fallback
1821825c8d7SGao Xiang 	 * to in-place I/O instead to avoid any direct reclaim.
1831825c8d7SGao Xiang 	 */
1841825c8d7SGao Xiang 	TRYALLOC,
18547e4937aSGao Xiang };
18647e4937aSGao Xiang 
18747e4937aSGao Xiang /*
18847e4937aSGao Xiang  * tagged pointer with 1-bit tag for all compressed pages
18947e4937aSGao Xiang  * tag 0 - the page is just found with an extra page reference
19047e4937aSGao Xiang  */
19147e4937aSGao Xiang typedef tagptr1_t compressed_page_t;
19247e4937aSGao Xiang 
19347e4937aSGao Xiang #define tag_compressed_page_justfound(page) \
19447e4937aSGao Xiang 	tagptr_fold(compressed_page_t, page, 1)
19547e4937aSGao Xiang 
19647e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
19747e4937aSGao Xiang 
19847e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
19947e4937aSGao Xiang {
20047e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
2019f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
20247e4937aSGao Xiang }
20347e4937aSGao Xiang 
20499634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
20547e4937aSGao Xiang {
20647e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
20747e4937aSGao Xiang 
20847e4937aSGao Xiang 	/*
20947e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
21047e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
21147e4937aSGao Xiang 	 */
2120e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
2130e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
21447e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
21547e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
21647e4937aSGao Xiang }
21747e4937aSGao Xiang 
21847e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
21947e4937aSGao Xiang {
2209f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
22147e4937aSGao Xiang 
2229f6cc76eSGao Xiang 	if (err)
2239f6cc76eSGao Xiang 		return err;
2249f6cc76eSGao Xiang 	err = z_erofs_init_workqueue();
2259f6cc76eSGao Xiang 	if (err)
2269f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
2279f6cc76eSGao Xiang 	return err;
22847e4937aSGao Xiang }
22947e4937aSGao Xiang 
23047e4937aSGao Xiang enum z_erofs_collectmode {
23147e4937aSGao Xiang 	COLLECT_SECONDARY,
23247e4937aSGao Xiang 	COLLECT_PRIMARY,
23347e4937aSGao Xiang 	/*
23447e4937aSGao Xiang 	 * The current collection was the tail of an exist chain, in addition
23547e4937aSGao Xiang 	 * that the previous processed chained collections are all decided to
23647e4937aSGao Xiang 	 * be hooked up to it.
23747e4937aSGao Xiang 	 * A new chain will be created for the remaining collections which are
23847e4937aSGao Xiang 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
23947e4937aSGao Xiang 	 * the next collection cannot reuse the whole page safely in
24047e4937aSGao Xiang 	 * the following scenario:
24147e4937aSGao Xiang 	 *  ________________________________________________________________
24247e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
24347e4937aSGao Xiang 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
24447e4937aSGao Xiang 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
24547e4937aSGao Xiang 	 */
24647e4937aSGao Xiang 	COLLECT_PRIMARY_HOOKED,
2470b964600SGao Xiang 	/*
2480b964600SGao Xiang 	 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
2490b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
2500b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
2510b964600SGao Xiang 	 * pagevec) since it can be directly decoded without I/O submission.
2520b964600SGao Xiang 	 */
25347e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
25447e4937aSGao Xiang 	/*
25547e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
25647e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
25747e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
25847e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
25947e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
26047e4937aSGao Xiang 	 * illustrated below:
26147e4937aSGao Xiang 	 *  ________________________________________________________________
26247e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
26347e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
26447e4937aSGao Xiang 	 * |  PRIMARY_FOLLOWED or |                                        |
26547e4937aSGao Xiang 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
26647e4937aSGao Xiang 	 *
26747e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
26847e4937aSGao Xiang 	 */
26947e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED,
27047e4937aSGao Xiang };
27147e4937aSGao Xiang 
2725c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
2735c6dcc57SGao Xiang 	struct inode *const inode;
2745c6dcc57SGao Xiang 	struct erofs_map_blocks map;
275*06a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
27647e4937aSGao Xiang 	struct z_erofs_pagevec_ctor vector;
27747e4937aSGao Xiang 
278*06a304cdSGao Xiang 	struct page *candidate_bvpage;
27947e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
28081382f5fSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
28181382f5fSGao Xiang 	struct page **icpage_ptr;
28247e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
28347e4937aSGao Xiang 
28447e4937aSGao Xiang 	enum z_erofs_collectmode mode;
28547e4937aSGao Xiang 
2866ea5aad3SGao Xiang 	bool readahead;
28747e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
28847e4937aSGao Xiang 	bool backmost;
28947e4937aSGao Xiang 	erofs_off_t headoffset;
29047e4937aSGao Xiang };
29147e4937aSGao Xiang 
29247e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
2935c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
2944398d3c3SWeizhao Ouyang 	.mode = COLLECT_PRIMARY_FOLLOWED, .backmost = true }
29547e4937aSGao Xiang 
29647e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
29747e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
29847e4937aSGao Xiang 
2996f39d1e1SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
3001825c8d7SGao Xiang 			       enum z_erofs_cache_alloctype type,
301eaa9172aSGao Xiang 			       struct page **pagepool)
30247e4937aSGao Xiang {
3036f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
3045c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
30547e4937aSGao Xiang 	bool standalone = true;
3066f39d1e1SGao Xiang 	/*
3076f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
3086f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
3096f39d1e1SGao Xiang 	 */
3101825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
3111825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
31281382f5fSGao Xiang 	struct page **pages;
31381382f5fSGao Xiang 	pgoff_t index;
31447e4937aSGao Xiang 
3155c6dcc57SGao Xiang 	if (fe->mode < COLLECT_PRIMARY_FOLLOWED)
31647e4937aSGao Xiang 		return;
31747e4937aSGao Xiang 
31881382f5fSGao Xiang 	pages = pcl->compressed_pages;
31981382f5fSGao Xiang 	index = pcl->obj.index;
32081382f5fSGao Xiang 	for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
32147e4937aSGao Xiang 		struct page *page;
32247e4937aSGao Xiang 		compressed_page_t t;
3231825c8d7SGao Xiang 		struct page *newpage = NULL;
32447e4937aSGao Xiang 
32547e4937aSGao Xiang 		/* the compressed page was loaded before */
32647e4937aSGao Xiang 		if (READ_ONCE(*pages))
32747e4937aSGao Xiang 			continue;
32847e4937aSGao Xiang 
32947e4937aSGao Xiang 		page = find_get_page(mc, index);
33047e4937aSGao Xiang 
33147e4937aSGao Xiang 		if (page) {
33247e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
3330b964600SGao Xiang 		} else {
3340b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
3350b964600SGao Xiang 			standalone = false;
3360b964600SGao Xiang 			switch (type) {
3370b964600SGao Xiang 			case TRYALLOC:
3381825c8d7SGao Xiang 				newpage = erofs_allocpage(pagepool, gfp);
3391825c8d7SGao Xiang 				if (!newpage)
34047e4937aSGao Xiang 					continue;
3410b964600SGao Xiang 				set_page_private(newpage,
3420b964600SGao Xiang 						 Z_EROFS_PREALLOCATED_PAGE);
3430b964600SGao Xiang 				t = tag_compressed_page_justfound(newpage);
3440b964600SGao Xiang 				break;
3450b964600SGao Xiang 			default:        /* DONTALLOC */
3460b964600SGao Xiang 				continue;
3470b964600SGao Xiang 			}
34847e4937aSGao Xiang 		}
34947e4937aSGao Xiang 
35047e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
35147e4937aSGao Xiang 			continue;
35247e4937aSGao Xiang 
353eaa9172aSGao Xiang 		if (page)
35447e4937aSGao Xiang 			put_page(page);
355eaa9172aSGao Xiang 		else if (newpage)
356eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
35747e4937aSGao Xiang 	}
35847e4937aSGao Xiang 
3590b964600SGao Xiang 	/*
3600b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
3610b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
3620b964600SGao Xiang 	 */
3630b964600SGao Xiang 	if (standalone)
3645c6dcc57SGao Xiang 		fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
36547e4937aSGao Xiang }
36647e4937aSGao Xiang 
36747e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
36847e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
36947e4937aSGao Xiang 				       struct erofs_workgroup *grp)
37047e4937aSGao Xiang {
37147e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
37247e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
37347e4937aSGao Xiang 	int i;
37447e4937aSGao Xiang 
375cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
37647e4937aSGao Xiang 	/*
37747e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
37847e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
37947e4937aSGao Xiang 	 */
3809f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
38147e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
38247e4937aSGao Xiang 
38347e4937aSGao Xiang 		if (!page)
38447e4937aSGao Xiang 			continue;
38547e4937aSGao Xiang 
38647e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
38747e4937aSGao Xiang 		if (!trylock_page(page))
38847e4937aSGao Xiang 			return -EBUSY;
38947e4937aSGao Xiang 
390f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
39147e4937aSGao Xiang 			continue;
39247e4937aSGao Xiang 
39347e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
39447e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
3956aaa7b06SGao Xiang 		detach_page_private(page);
39647e4937aSGao Xiang 		unlock_page(page);
39747e4937aSGao Xiang 	}
39847e4937aSGao Xiang 	return 0;
39947e4937aSGao Xiang }
40047e4937aSGao Xiang 
401d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
40247e4937aSGao Xiang {
40347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
40447e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
40547e4937aSGao Xiang 
40647e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
40747e4937aSGao Xiang 		unsigned int i;
40847e4937aSGao Xiang 
409cecf864dSYue Hu 		DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
4109f6cc76eSGao Xiang 		for (i = 0; i < pcl->pclusterpages; ++i) {
41147e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
41247e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
41347e4937aSGao Xiang 				ret = 1;
41447e4937aSGao Xiang 				break;
41547e4937aSGao Xiang 			}
41647e4937aSGao Xiang 		}
41747e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
41847e4937aSGao Xiang 
4196aaa7b06SGao Xiang 		if (ret)
4206aaa7b06SGao Xiang 			detach_page_private(page);
42147e4937aSGao Xiang 	}
42247e4937aSGao Xiang 	return ret;
42347e4937aSGao Xiang }
42447e4937aSGao Xiang 
42547e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
4265c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
42747e4937aSGao Xiang 				   struct page *page)
42847e4937aSGao Xiang {
4295c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
43047e4937aSGao Xiang 
4315c6dcc57SGao Xiang 	while (fe->icpage_ptr > pcl->compressed_pages)
4325c6dcc57SGao Xiang 		if (!cmpxchg(--fe->icpage_ptr, NULL, page))
43347e4937aSGao Xiang 			return true;
43447e4937aSGao Xiang 	return false;
43547e4937aSGao Xiang }
43647e4937aSGao Xiang 
43787ca34a7SGao Xiang /* callers must be with pcluster lock held */
4385c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
439*06a304cdSGao Xiang 			       struct z_erofs_bvec *bvec,
440*06a304cdSGao Xiang 			       enum z_erofs_page_type type)
44147e4937aSGao Xiang {
44247e4937aSGao Xiang 	int ret;
44347e4937aSGao Xiang 
4445c6dcc57SGao Xiang 	if (fe->mode >= COLLECT_PRIMARY &&
445*06a304cdSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE) {
446*06a304cdSGao Xiang 		/* give priority for inplaceio to use file pages first */
447*06a304cdSGao Xiang 		if (z_erofs_try_inplace_io(fe, bvec->page))
44847e4937aSGao Xiang 			return 0;
449*06a304cdSGao Xiang 		/* otherwise, check if it can be used as a bvpage */
450*06a304cdSGao Xiang 		if (fe->mode >= COLLECT_PRIMARY_FOLLOWED &&
451*06a304cdSGao Xiang 		    !fe->candidate_bvpage)
452*06a304cdSGao Xiang 			fe->candidate_bvpage = bvec->page;
453*06a304cdSGao Xiang 	}
454*06a304cdSGao Xiang 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
455*06a304cdSGao Xiang 	fe->pcl->vcnt += (ret >= 0);
456*06a304cdSGao Xiang 	return ret;
45747e4937aSGao Xiang }
45847e4937aSGao Xiang 
4595c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
46047e4937aSGao Xiang {
4615c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
4625c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
46347e4937aSGao Xiang 
464473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
465473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
466473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
46747e4937aSGao Xiang 		*owned_head = &pcl->next;
468473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
4695c6dcc57SGao Xiang 		f->mode = COLLECT_PRIMARY_FOLLOWED;
470473e15b0SGao Xiang 		return;
471473e15b0SGao Xiang 	}
472473e15b0SGao Xiang 
47347e4937aSGao Xiang 	/*
474473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
475473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
47647e4937aSGao Xiang 	 */
47747e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
478473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
47947e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
4805c6dcc57SGao Xiang 		f->mode = COLLECT_PRIMARY_HOOKED;
4815c6dcc57SGao Xiang 		f->tailpcl = NULL;
482473e15b0SGao Xiang 		return;
48347e4937aSGao Xiang 	}
484473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
4855c6dcc57SGao Xiang 	f->mode = COLLECT_PRIMARY;
48647e4937aSGao Xiang }
48747e4937aSGao Xiang 
48883a386c0SGao Xiang static int z_erofs_lookup_pcluster(struct z_erofs_decompress_frontend *fe)
48947e4937aSGao Xiang {
49083a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
4915c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
49247e4937aSGao Xiang 	unsigned int length;
49347e4937aSGao Xiang 
49464094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
4955c6dcc57SGao Xiang 	if (fe->owned_head == &pcl->next || pcl == fe->tailpcl) {
49647e4937aSGao Xiang 		DBG_BUGON(1);
4979e579fc1SGao Xiang 		return -EFSCORRUPTED;
49847e4937aSGao Xiang 	}
49947e4937aSGao Xiang 
50087ca34a7SGao Xiang 	if (pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) {
50147e4937aSGao Xiang 		DBG_BUGON(1);
5029e579fc1SGao Xiang 		return -EFSCORRUPTED;
50347e4937aSGao Xiang 	}
50447e4937aSGao Xiang 
50547e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
50647e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
50747e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
50847e4937aSGao Xiang 			DBG_BUGON(1);
5099e579fc1SGao Xiang 			return -EFSCORRUPTED;
51047e4937aSGao Xiang 		}
51147e4937aSGao Xiang 	} else {
51247e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
51347e4937aSGao Xiang 
51447e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
51547e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
51647e4937aSGao Xiang 
51747e4937aSGao Xiang 		while (llen > length &&
51847e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
51947e4937aSGao Xiang 			cpu_relax();
52047e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
52147e4937aSGao Xiang 		}
52247e4937aSGao Xiang 	}
52387ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
52447e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
5255c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
5265c6dcc57SGao Xiang 		fe->tailpcl = pcl;
527473e15b0SGao Xiang 
5285c6dcc57SGao Xiang 	z_erofs_try_to_claim_pcluster(fe);
5299e579fc1SGao Xiang 	return 0;
53047e4937aSGao Xiang }
53147e4937aSGao Xiang 
53283a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
53347e4937aSGao Xiang {
53483a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
535cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
53647e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
53764094a04SGao Xiang 	struct erofs_workgroup *grp;
53847e4937aSGao Xiang 	int err;
53947e4937aSGao Xiang 
5408f899262SGao Xiang 	if (!(map->m_flags & EROFS_MAP_ENCODED)) {
5418f899262SGao Xiang 		DBG_BUGON(1);
5428f899262SGao Xiang 		return -EFSCORRUPTED;
5438f899262SGao Xiang 	}
5448f899262SGao Xiang 
5459f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
546cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
547cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
5489f6cc76eSGao Xiang 	if (IS_ERR(pcl))
5499f6cc76eSGao Xiang 		return PTR_ERR(pcl);
55047e4937aSGao Xiang 
55164094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
5528f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
55347e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
55447e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
55547e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
55647e4937aSGao Xiang 
55747e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
5585c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
55987ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
5605c6dcc57SGao Xiang 	fe->mode = COLLECT_PRIMARY_FOLLOWED;
56147e4937aSGao Xiang 
56247e4937aSGao Xiang 	/*
56347e4937aSGao Xiang 	 * lock all primary followed works before visible to others
56447e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
56547e4937aSGao Xiang 	 */
56687ca34a7SGao Xiang 	mutex_init(&pcl->lock);
56787ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
56847e4937aSGao Xiang 
569cecf864dSYue Hu 	if (ztailpacking) {
570cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
571cecf864dSYue Hu 		pcl->pageofs_in = erofs_blkoff(map->m_pa);
572cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
573cecf864dSYue Hu 	} else {
574cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
575cecf864dSYue Hu 
57683a386c0SGao Xiang 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
57764094a04SGao Xiang 		if (IS_ERR(grp)) {
57864094a04SGao Xiang 			err = PTR_ERR(grp);
57964094a04SGao Xiang 			goto err_out;
58064094a04SGao Xiang 		}
58164094a04SGao Xiang 
58264094a04SGao Xiang 		if (grp != &pcl->obj) {
5835c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
584cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
58564094a04SGao Xiang 			err = -EEXIST;
58664094a04SGao Xiang 			goto err_out;
58747e4937aSGao Xiang 		}
588cecf864dSYue Hu 	}
58947e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
5905c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
5915c6dcc57SGao Xiang 		fe->tailpcl = pcl;
5925c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
5935c6dcc57SGao Xiang 	fe->pcl = pcl;
5949e579fc1SGao Xiang 	return 0;
59564094a04SGao Xiang 
59664094a04SGao Xiang err_out:
59787ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
5989f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
59964094a04SGao Xiang 	return err;
60047e4937aSGao Xiang }
60147e4937aSGao Xiang 
60283a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
60347e4937aSGao Xiang {
60483a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
6050d823b42SGao Xiang 	struct erofs_workgroup *grp = NULL;
6069e579fc1SGao Xiang 	int ret;
60747e4937aSGao Xiang 
60887ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
60947e4937aSGao Xiang 
61087ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
6115c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
6125c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
61347e4937aSGao Xiang 
6140d823b42SGao Xiang 	if (!(map->m_flags & EROFS_MAP_META)) {
6150d823b42SGao Xiang 		grp = erofs_find_workgroup(fe->inode->i_sb,
6160d823b42SGao Xiang 					   map->m_pa >> PAGE_SHIFT);
6170d823b42SGao Xiang 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
61847e4937aSGao Xiang 		DBG_BUGON(1);
619cecf864dSYue Hu 		return -EFSCORRUPTED;
620cecf864dSYue Hu 	}
62147e4937aSGao Xiang 
62264094a04SGao Xiang 	if (grp) {
6235c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
6240d823b42SGao Xiang 		ret = -EEXIST;
62564094a04SGao Xiang 	} else {
62683a386c0SGao Xiang 		ret = z_erofs_register_pcluster(fe);
62764094a04SGao Xiang 	}
62847e4937aSGao Xiang 
6290d823b42SGao Xiang 	if (ret == -EEXIST) {
63083a386c0SGao Xiang 		ret = z_erofs_lookup_pcluster(fe);
63164094a04SGao Xiang 		if (ret) {
6325c6dcc57SGao Xiang 			erofs_workgroup_put(&fe->pcl->obj);
63364094a04SGao Xiang 			return ret;
63464094a04SGao Xiang 		}
6350d823b42SGao Xiang 	} else if (ret) {
6360d823b42SGao Xiang 		return ret;
6370d823b42SGao Xiang 	}
638*06a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
639*06a304cdSGao Xiang 				Z_EROFS_NR_INLINE_PAGEVECS, fe->pcl->vcnt);
64081382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
6415c6dcc57SGao Xiang 	fe->icpage_ptr = fe->pcl->compressed_pages +
6425c6dcc57SGao Xiang 			z_erofs_pclusterpages(fe->pcl);
64347e4937aSGao Xiang 	return 0;
64447e4937aSGao Xiang }
64547e4937aSGao Xiang 
64647e4937aSGao Xiang /*
64747e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
64847e4937aSGao Xiang  * only after a RCU grace period.
64947e4937aSGao Xiang  */
65047e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
65147e4937aSGao Xiang {
65287ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
65387ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
65447e4937aSGao Xiang }
65547e4937aSGao Xiang 
65647e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
65747e4937aSGao Xiang {
65847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
65947e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
66047e4937aSGao Xiang 
66187ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
66247e4937aSGao Xiang }
66347e4937aSGao Xiang 
6645c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
66547e4937aSGao Xiang {
66687ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
66747e4937aSGao Xiang 
66887ca34a7SGao Xiang 	if (!pcl)
66947e4937aSGao Xiang 		return false;
67047e4937aSGao Xiang 
671*06a304cdSGao Xiang 	z_erofs_bvec_iter_end(&fe->biter);
67287ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
67347e4937aSGao Xiang 
674*06a304cdSGao Xiang 	if (fe->candidate_bvpage) {
675*06a304cdSGao Xiang 		DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
676*06a304cdSGao Xiang 		fe->candidate_bvpage = NULL;
677*06a304cdSGao Xiang 	}
678*06a304cdSGao Xiang 
67947e4937aSGao Xiang 	/*
68047e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
68147e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
68247e4937aSGao Xiang 	 */
6835c6dcc57SGao Xiang 	if (fe->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
68487ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
68547e4937aSGao Xiang 
68687ca34a7SGao Xiang 	fe->pcl = NULL;
68747e4937aSGao Xiang 	return true;
68847e4937aSGao Xiang }
68947e4937aSGao Xiang 
69047e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
69147e4937aSGao Xiang 				       unsigned int cachestrategy,
69247e4937aSGao Xiang 				       erofs_off_t la)
69347e4937aSGao Xiang {
69447e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
69547e4937aSGao Xiang 		return false;
69647e4937aSGao Xiang 
69747e4937aSGao Xiang 	if (fe->backmost)
69847e4937aSGao Xiang 		return true;
69947e4937aSGao Xiang 
70047e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
70147e4937aSGao Xiang 		la < fe->headoffset;
70247e4937aSGao Xiang }
70347e4937aSGao Xiang 
70447e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
705eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
70647e4937aSGao Xiang {
70747e4937aSGao Xiang 	struct inode *const inode = fe->inode;
708bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
70947e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
71047e4937aSGao Xiang 	const loff_t offset = page_offset(page);
711dc76ea8cSGao Xiang 	bool tight = true;
71247e4937aSGao Xiang 
71347e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
71447e4937aSGao Xiang 	enum z_erofs_page_type page_type;
71547e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
71647e4937aSGao Xiang 	int err = 0;
71747e4937aSGao Xiang 
71847e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
71947e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
72047e4937aSGao Xiang 
72147e4937aSGao Xiang 	spiltted = 0;
72247e4937aSGao Xiang 	end = PAGE_SIZE;
72347e4937aSGao Xiang repeat:
72447e4937aSGao Xiang 	cur = end - 1;
72547e4937aSGao Xiang 
72639397a46SGao Xiang 	if (offset + cur < map->m_la ||
72739397a46SGao Xiang 	    offset + cur >= map->m_la + map->m_llen) {
72839397a46SGao Xiang 		erofs_dbg("out-of-range map @ pos %llu", offset + cur);
72947e4937aSGao Xiang 
7305c6dcc57SGao Xiang 		if (z_erofs_collector_end(fe))
73147e4937aSGao Xiang 			fe->backmost = false;
73247e4937aSGao Xiang 		map->m_la = offset + cur;
73347e4937aSGao Xiang 		map->m_llen = 0;
73447e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
7358d8a09b0SGao Xiang 		if (err)
73647e4937aSGao Xiang 			goto err_out;
73739397a46SGao Xiang 	} else {
73839397a46SGao Xiang 		if (fe->pcl)
73939397a46SGao Xiang 			goto hitted;
74039397a46SGao Xiang 		/* didn't get a valid pcluster previously (very rare) */
74139397a46SGao Xiang 	}
74247e4937aSGao Xiang 
7438d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
74447e4937aSGao Xiang 		goto hitted;
74547e4937aSGao Xiang 
74683a386c0SGao Xiang 	err = z_erofs_collector_begin(fe);
7478d8a09b0SGao Xiang 	if (err)
74847e4937aSGao Xiang 		goto err_out;
74947e4937aSGao Xiang 
7505c6dcc57SGao Xiang 	if (z_erofs_is_inline_pcluster(fe->pcl)) {
75109c54379SGao Xiang 		void *mp;
752cecf864dSYue Hu 
75309c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
75409c54379SGao Xiang 					erofs_blknr(map->m_pa), EROFS_NO_KMAP);
75509c54379SGao Xiang 		if (IS_ERR(mp)) {
75609c54379SGao Xiang 			err = PTR_ERR(mp);
757cecf864dSYue Hu 			erofs_err(inode->i_sb,
758cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
759cecf864dSYue Hu 			goto err_out;
760cecf864dSYue Hu 		}
76109c54379SGao Xiang 		get_page(fe->map.buf.page);
7625c6dcc57SGao Xiang 		WRITE_ONCE(fe->pcl->compressed_pages[0], fe->map.buf.page);
7635c6dcc57SGao Xiang 		fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
764cecf864dSYue Hu 	} else {
7656f39d1e1SGao Xiang 		/* bind cache first when cached decompression is preferred */
766cecf864dSYue Hu 		if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy,
767cecf864dSYue Hu 					       map->m_la))
7681825c8d7SGao Xiang 			cache_strategy = TRYALLOC;
76947e4937aSGao Xiang 		else
77047e4937aSGao Xiang 			cache_strategy = DONTALLOC;
77147e4937aSGao Xiang 
7726f39d1e1SGao Xiang 		z_erofs_bind_cache(fe, cache_strategy, pagepool);
773cecf864dSYue Hu 	}
77447e4937aSGao Xiang hitted:
775dc76ea8cSGao Xiang 	/*
776dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
777dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
778dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
779dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
780dc76ea8cSGao Xiang 	 */
7815c6dcc57SGao Xiang 	tight &= (fe->mode >= COLLECT_PRIMARY_HOOKED &&
7825c6dcc57SGao Xiang 		  fe->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
783dc76ea8cSGao Xiang 
78447e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
7858d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
78647e4937aSGao Xiang 		zero_user_segment(page, cur, end);
78747e4937aSGao Xiang 		goto next_part;
78847e4937aSGao Xiang 	}
78947e4937aSGao Xiang 
79047e4937aSGao Xiang 	/* let's derive page type */
79147e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
79247e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
79347e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
79447e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
79547e4937aSGao Xiang 
79647e4937aSGao Xiang 	if (cur)
7975c6dcc57SGao Xiang 		tight &= (fe->mode >= COLLECT_PRIMARY_FOLLOWED);
79847e4937aSGao Xiang 
79947e4937aSGao Xiang retry:
800*06a304cdSGao Xiang 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
801*06a304cdSGao Xiang 					.page = page,
802*06a304cdSGao Xiang 					.offset = offset - map->m_la,
803*06a304cdSGao Xiang 					.end = end,
804*06a304cdSGao Xiang 				  }), page_type);
805*06a304cdSGao Xiang 	/* should allocate an additional short-lived page for bvset */
806*06a304cdSGao Xiang 	if (err == -EAGAIN && !fe->candidate_bvpage) {
807*06a304cdSGao Xiang 		fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
808*06a304cdSGao Xiang 		set_page_private(fe->candidate_bvpage,
809*06a304cdSGao Xiang 				 Z_EROFS_SHORTLIVED_PAGE);
81047e4937aSGao Xiang 		goto retry;
81147e4937aSGao Xiang 	}
81247e4937aSGao Xiang 
813*06a304cdSGao Xiang 	if (err) {
814*06a304cdSGao Xiang 		DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
81547e4937aSGao Xiang 		goto err_out;
816*06a304cdSGao Xiang 	}
81747e4937aSGao Xiang 
81847e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
81947e4937aSGao Xiang 
82047e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
82147e4937aSGao Xiang 
82247e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
82347e4937aSGao Xiang 	++spiltted;
82447e4937aSGao Xiang 	/* also update nr_pages */
82587ca34a7SGao Xiang 	fe->pcl->nr_pages = max_t(pgoff_t, fe->pcl->nr_pages, index + 1);
82647e4937aSGao Xiang next_part:
82747e4937aSGao Xiang 	/* can be used for verification */
82847e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
82947e4937aSGao Xiang 
83047e4937aSGao Xiang 	end = cur;
83147e4937aSGao Xiang 	if (end > 0)
83247e4937aSGao Xiang 		goto repeat;
83347e4937aSGao Xiang 
83447e4937aSGao Xiang out:
83547e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
83647e4937aSGao Xiang 
8374f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
83847e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
83947e4937aSGao Xiang 	return err;
84047e4937aSGao Xiang 
84147e4937aSGao Xiang 	/* if some error occurred while processing this page */
84247e4937aSGao Xiang err_out:
84347e4937aSGao Xiang 	SetPageError(page);
84447e4937aSGao Xiang 	goto out;
84547e4937aSGao Xiang }
84647e4937aSGao Xiang 
84740452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
84840452ffcSHuang Jianan 				       unsigned int readahead_pages)
84940452ffcSHuang Jianan {
850a2e20a25SMatthew Wilcox (Oracle) 	/* auto: enable for read_folio, disable for readahead */
85140452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
85240452ffcSHuang Jianan 	    !readahead_pages)
85340452ffcSHuang Jianan 		return true;
85440452ffcSHuang Jianan 
85540452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
85640452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
85740452ffcSHuang Jianan 		return true;
85840452ffcSHuang Jianan 
85940452ffcSHuang Jianan 	return false;
86040452ffcSHuang Jianan }
86140452ffcSHuang Jianan 
8626aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
8636aaa7b06SGao Xiang {
8646aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
8656aaa7b06SGao Xiang }
8666aaa7b06SGao Xiang 
86742fec235SGao Xiang static int z_erofs_parse_out_bvecs(struct z_erofs_pcluster *pcl,
86842fec235SGao Xiang 				   struct page **pages, struct page **pagepool)
86942fec235SGao Xiang {
870*06a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
871*06a304cdSGao Xiang 	struct page *old_bvpage;
87242fec235SGao Xiang 	int i, err = 0;
87342fec235SGao Xiang 
874*06a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset,
875*06a304cdSGao Xiang 				Z_EROFS_NR_INLINE_PAGEVECS, 0);
87642fec235SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
877*06a304cdSGao Xiang 		struct z_erofs_bvec bvec;
87842fec235SGao Xiang 		unsigned int pagenr;
87942fec235SGao Xiang 
880*06a304cdSGao Xiang 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
88142fec235SGao Xiang 
882*06a304cdSGao Xiang 		if (old_bvpage)
883*06a304cdSGao Xiang 			z_erofs_put_shortlivedpage(pagepool, old_bvpage);
88442fec235SGao Xiang 
885*06a304cdSGao Xiang 		pagenr = (bvec.offset + pcl->pageofs_out) >> PAGE_SHIFT;
88642fec235SGao Xiang 		DBG_BUGON(pagenr >= pcl->nr_pages);
887*06a304cdSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
88842fec235SGao Xiang 		/*
88942fec235SGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
89042fec235SGao Xiang 		 * so here erroring out one multiref page.
89142fec235SGao Xiang 		 */
89242fec235SGao Xiang 		if (pages[pagenr]) {
89342fec235SGao Xiang 			DBG_BUGON(1);
89442fec235SGao Xiang 			SetPageError(pages[pagenr]);
89542fec235SGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
89642fec235SGao Xiang 			err = -EFSCORRUPTED;
89742fec235SGao Xiang 		}
898*06a304cdSGao Xiang 		pages[pagenr] = bvec.page;
89942fec235SGao Xiang 	}
900*06a304cdSGao Xiang 
901*06a304cdSGao Xiang 	old_bvpage = z_erofs_bvec_iter_end(&biter);
902*06a304cdSGao Xiang 	if (old_bvpage)
903*06a304cdSGao Xiang 		z_erofs_put_shortlivedpage(pagepool, old_bvpage);
90442fec235SGao Xiang 	return err;
90542fec235SGao Xiang }
90642fec235SGao Xiang 
90747e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
90847e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
909eaa9172aSGao Xiang 				       struct page **pagepool)
91047e4937aSGao Xiang {
91147e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
912cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
9139f6cc76eSGao Xiang 	unsigned int i, inputsize, outputsize, llen, nr_pages;
91447e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
91547e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
91647e4937aSGao Xiang 
91747e4937aSGao Xiang 	bool overlapped, partial;
91847e4937aSGao Xiang 	int err;
91947e4937aSGao Xiang 
92047e4937aSGao Xiang 	might_sleep();
92187ca34a7SGao Xiang 	DBG_BUGON(!READ_ONCE(pcl->nr_pages));
92247e4937aSGao Xiang 
92387ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
92487ca34a7SGao Xiang 	nr_pages = pcl->nr_pages;
92547e4937aSGao Xiang 
9268d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
92747e4937aSGao Xiang 		pages = pages_onstack;
92847e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
92947e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
93047e4937aSGao Xiang 		pages = z_pagemap_global;
93147e4937aSGao Xiang 	} else {
93247e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
93347e4937aSGao Xiang 
93447e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
93547e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
93647e4937aSGao Xiang 
93747e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
93847e4937aSGao Xiang 				       gfp_flags);
93947e4937aSGao Xiang 
94047e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
9418d8a09b0SGao Xiang 		if (!pages) {
94247e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
94347e4937aSGao Xiang 			pages = z_pagemap_global;
94447e4937aSGao Xiang 		}
94547e4937aSGao Xiang 	}
94647e4937aSGao Xiang 
94747e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
94847e4937aSGao Xiang 		pages[i] = NULL;
94947e4937aSGao Xiang 
95042fec235SGao Xiang 	err = z_erofs_parse_out_bvecs(pcl, pages, pagepool);
95147e4937aSGao Xiang 
95247e4937aSGao Xiang 	overlapped = false;
95347e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
95447e4937aSGao Xiang 
955cecf864dSYue Hu 	for (i = 0; i < pclusterpages; ++i) {
95647e4937aSGao Xiang 		unsigned int pagenr;
95747e4937aSGao Xiang 
95847e4937aSGao Xiang 		page = compressed_pages[i];
95947e4937aSGao Xiang 		/* all compressed pages ought to be valid */
96047e4937aSGao Xiang 		DBG_BUGON(!page);
96147e4937aSGao Xiang 
962cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
963cecf864dSYue Hu 			if (!PageUptodate(page))
964cecf864dSYue Hu 				err = -EIO;
965cecf864dSYue Hu 			continue;
966cecf864dSYue Hu 		}
967cecf864dSYue Hu 
968cecf864dSYue Hu 		DBG_BUGON(z_erofs_page_is_invalidated(page));
9696aaa7b06SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
97047e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
9718d8a09b0SGao Xiang 				if (!PageUptodate(page))
97247e4937aSGao Xiang 					err = -EIO;
97347e4937aSGao Xiang 				continue;
97447e4937aSGao Xiang 			}
97547e4937aSGao Xiang 
97647e4937aSGao Xiang 			/*
97747e4937aSGao Xiang 			 * only if non-head page can be selected
97847e4937aSGao Xiang 			 * for inplace decompression
97947e4937aSGao Xiang 			 */
98047e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
98147e4937aSGao Xiang 
98247e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
9838d8a09b0SGao Xiang 			if (pages[pagenr]) {
98447e4937aSGao Xiang 				DBG_BUGON(1);
98547e4937aSGao Xiang 				SetPageError(pages[pagenr]);
98647e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
98747e4937aSGao Xiang 				err = -EFSCORRUPTED;
98847e4937aSGao Xiang 			}
98947e4937aSGao Xiang 			pages[pagenr] = page;
99047e4937aSGao Xiang 
99147e4937aSGao Xiang 			overlapped = true;
99247e4937aSGao Xiang 		}
99347e4937aSGao Xiang 
9946aaa7b06SGao Xiang 		/* PG_error needs checking for all non-managed pages */
9958d8a09b0SGao Xiang 		if (PageError(page)) {
99647e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
99747e4937aSGao Xiang 			err = -EIO;
99847e4937aSGao Xiang 		}
99947e4937aSGao Xiang 	}
100047e4937aSGao Xiang 
10018d8a09b0SGao Xiang 	if (err)
100247e4937aSGao Xiang 		goto out;
100347e4937aSGao Xiang 
100447e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
100587ca34a7SGao Xiang 	if (nr_pages << PAGE_SHIFT >= pcl->pageofs_out + llen) {
100647e4937aSGao Xiang 		outputsize = llen;
100747e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
100847e4937aSGao Xiang 	} else {
100987ca34a7SGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - pcl->pageofs_out;
101047e4937aSGao Xiang 		partial = true;
101147e4937aSGao Xiang 	}
101247e4937aSGao Xiang 
1013cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1014cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1015cecf864dSYue Hu 	else
1016cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1017cecf864dSYue Hu 
101847e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
101947e4937aSGao Xiang 					.sb = sb,
102047e4937aSGao Xiang 					.in = compressed_pages,
102147e4937aSGao Xiang 					.out = pages,
1022cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
102387ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
10249f6cc76eSGao Xiang 					.inputsize = inputsize,
102547e4937aSGao Xiang 					.outputsize = outputsize,
102647e4937aSGao Xiang 					.alg = pcl->algorithmformat,
102747e4937aSGao Xiang 					.inplace_io = overlapped,
102847e4937aSGao Xiang 					.partial_decoding = partial
102947e4937aSGao Xiang 				 }, pagepool);
103047e4937aSGao Xiang 
103147e4937aSGao Xiang out:
1032cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1033cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1034cecf864dSYue Hu 		page = compressed_pages[0];
1035cecf864dSYue Hu 		WRITE_ONCE(compressed_pages[0], NULL);
1036cecf864dSYue Hu 		put_page(page);
1037cecf864dSYue Hu 	} else {
1038cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
103947e4937aSGao Xiang 			page = compressed_pages[i];
104047e4937aSGao Xiang 
104147e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
104247e4937aSGao Xiang 				continue;
104347e4937aSGao Xiang 
10446aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
10456aaa7b06SGao Xiang 			(void)z_erofs_put_shortlivedpage(pagepool, page);
104647e4937aSGao Xiang 			WRITE_ONCE(compressed_pages[i], NULL);
104747e4937aSGao Xiang 		}
1048cecf864dSYue Hu 	}
104947e4937aSGao Xiang 
105047e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
105147e4937aSGao Xiang 		page = pages[i];
105247e4937aSGao Xiang 		if (!page)
105347e4937aSGao Xiang 			continue;
105447e4937aSGao Xiang 
10556aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
105647e4937aSGao Xiang 
10576aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
10586aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
105947e4937aSGao Xiang 			continue;
106047e4937aSGao Xiang 
10618d8a09b0SGao Xiang 		if (err < 0)
106247e4937aSGao Xiang 			SetPageError(page);
106347e4937aSGao Xiang 
106447e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
106547e4937aSGao Xiang 	}
106647e4937aSGao Xiang 
106747e4937aSGao Xiang 	if (pages == z_pagemap_global)
106847e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
10698d8a09b0SGao Xiang 	else if (pages != pages_onstack)
107047e4937aSGao Xiang 		kvfree(pages);
107147e4937aSGao Xiang 
107287ca34a7SGao Xiang 	pcl->nr_pages = 0;
1073*06a304cdSGao Xiang 	pcl->bvset.nextpage = NULL;
107487ca34a7SGao Xiang 	pcl->vcnt = 0;
107547e4937aSGao Xiang 
107687ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
107747e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
107887ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
107947e4937aSGao Xiang 	return err;
108047e4937aSGao Xiang }
108147e4937aSGao Xiang 
10820c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1083eaa9172aSGao Xiang 				     struct page **pagepool)
108447e4937aSGao Xiang {
108547e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
108647e4937aSGao Xiang 
108747e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
108847e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
108947e4937aSGao Xiang 
109047e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
109147e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
109247e4937aSGao Xiang 
109347e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
109447e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
109547e4937aSGao Xiang 
109647e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
109747e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
109847e4937aSGao Xiang 
1099a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
110087ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
110147e4937aSGao Xiang 	}
110247e4937aSGao Xiang }
110347e4937aSGao Xiang 
11040c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
110547e4937aSGao Xiang {
1106a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1107a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1108eaa9172aSGao Xiang 	struct page *pagepool = NULL;
110947e4937aSGao Xiang 
1110a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
11110c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
111247e4937aSGao Xiang 
1113eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1114a4b1fab1SGao Xiang 	kvfree(bgq);
111547e4937aSGao Xiang }
111647e4937aSGao Xiang 
11177865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
11187865827cSGao Xiang 				       bool sync, int bios)
11197865827cSGao Xiang {
11207865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
11217865827cSGao Xiang 
11227865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
11237865827cSGao Xiang 	if (sync) {
11247865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
112560b30050SHongyu Jin 			complete(&io->u.done);
112660b30050SHongyu Jin 
11277865827cSGao Xiang 		return;
11287865827cSGao Xiang 	}
11297865827cSGao Xiang 
11307865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
11317865827cSGao Xiang 		return;
11327865827cSGao Xiang 	/* Use workqueue and sync decompression for atomic contexts only */
11337865827cSGao Xiang 	if (in_atomic() || irqs_disabled()) {
11347865827cSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
11357865827cSGao Xiang 		/* enable sync decompression for readahead */
11367865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
11377865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
11387865827cSGao Xiang 		return;
11397865827cSGao Xiang 	}
11407865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
11417865827cSGao Xiang }
11427865827cSGao Xiang 
114347e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
114447e4937aSGao Xiang 					       unsigned int nr,
1145eaa9172aSGao Xiang 					       struct page **pagepool,
11469f2731d6SGao Xiang 					       struct address_space *mc)
114747e4937aSGao Xiang {
114847e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
11499f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
115047e4937aSGao Xiang 	bool tocache = false;
115147e4937aSGao Xiang 
115247e4937aSGao Xiang 	struct address_space *mapping;
115347e4937aSGao Xiang 	struct page *oldpage, *page;
115447e4937aSGao Xiang 
115547e4937aSGao Xiang 	compressed_page_t t;
115647e4937aSGao Xiang 	int justfound;
115747e4937aSGao Xiang 
115847e4937aSGao Xiang repeat:
115947e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
116047e4937aSGao Xiang 	oldpage = page;
116147e4937aSGao Xiang 
116247e4937aSGao Xiang 	if (!page)
116347e4937aSGao Xiang 		goto out_allocpage;
116447e4937aSGao Xiang 
116547e4937aSGao Xiang 	/* process the target tagged pointer */
116647e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
116747e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
116847e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
116947e4937aSGao Xiang 
11701825c8d7SGao Xiang 	/*
11711825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
11721825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
11731825c8d7SGao Xiang 	 */
11741825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
11751825c8d7SGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
11761825c8d7SGao Xiang 		set_page_private(page, 0);
11771825c8d7SGao Xiang 		tocache = true;
11781825c8d7SGao Xiang 		goto out_tocache;
11791825c8d7SGao Xiang 	}
118047e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
118147e4937aSGao Xiang 
118247e4937aSGao Xiang 	/*
11836aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
118447e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
118547e4937aSGao Xiang 	 */
118647e4937aSGao Xiang 	if (mapping && mapping != mc)
118747e4937aSGao Xiang 		/* ought to be unmanaged pages */
118847e4937aSGao Xiang 		goto out;
118947e4937aSGao Xiang 
11906aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
11916aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
11926aaa7b06SGao Xiang 		goto out;
11936aaa7b06SGao Xiang 
119447e4937aSGao Xiang 	lock_page(page);
119547e4937aSGao Xiang 
119647e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
119747e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
119847e4937aSGao Xiang 
119947e4937aSGao Xiang 	/* the page is still in manage cache */
120047e4937aSGao Xiang 	if (page->mapping == mc) {
120147e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
120247e4937aSGao Xiang 
120347e4937aSGao Xiang 		ClearPageError(page);
120447e4937aSGao Xiang 		if (!PagePrivate(page)) {
120547e4937aSGao Xiang 			/*
120647e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
120747e4937aSGao Xiang 			 * the current restriction as well if
120847e4937aSGao Xiang 			 * the page is already in compressed_pages[].
120947e4937aSGao Xiang 			 */
121047e4937aSGao Xiang 			DBG_BUGON(!justfound);
121147e4937aSGao Xiang 
121247e4937aSGao Xiang 			justfound = 0;
121347e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
121447e4937aSGao Xiang 			SetPagePrivate(page);
121547e4937aSGao Xiang 		}
121647e4937aSGao Xiang 
121747e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
121847e4937aSGao Xiang 		if (PageUptodate(page)) {
121947e4937aSGao Xiang 			unlock_page(page);
122047e4937aSGao Xiang 			page = NULL;
122147e4937aSGao Xiang 		}
122247e4937aSGao Xiang 		goto out;
122347e4937aSGao Xiang 	}
122447e4937aSGao Xiang 
122547e4937aSGao Xiang 	/*
122647e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
122747e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
122847e4937aSGao Xiang 	 */
122947e4937aSGao Xiang 	DBG_BUGON(page->mapping);
123047e4937aSGao Xiang 	DBG_BUGON(!justfound);
123147e4937aSGao Xiang 
123247e4937aSGao Xiang 	tocache = true;
123347e4937aSGao Xiang 	unlock_page(page);
123447e4937aSGao Xiang 	put_page(page);
123547e4937aSGao Xiang out_allocpage:
12365ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
12375ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1238eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
12395ddcee1fSGao Xiang 		cond_resched();
12405ddcee1fSGao Xiang 		goto repeat;
12415ddcee1fSGao Xiang 	}
12421825c8d7SGao Xiang out_tocache:
1243bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1244bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1245bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1246bf225074SGao Xiang 		goto out;
1247a30573b3SGao Xiang 	}
1248bf225074SGao Xiang 	attach_page_private(page, pcl);
1249bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1250bf225074SGao Xiang 	put_page(page);
1251bf225074SGao Xiang 
125247e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
125347e4937aSGao Xiang 	return page;
125447e4937aSGao Xiang }
125547e4937aSGao Xiang 
1256a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1257a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1258a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
125947e4937aSGao Xiang {
1260a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
126147e4937aSGao Xiang 
1262a4b1fab1SGao Xiang 	if (fg && !*fg) {
1263a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1264a4b1fab1SGao Xiang 		if (!q) {
1265a4b1fab1SGao Xiang 			*fg = true;
1266a4b1fab1SGao Xiang 			goto fg_out;
126747e4937aSGao Xiang 		}
12680c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1269a4b1fab1SGao Xiang 	} else {
1270a4b1fab1SGao Xiang fg_out:
1271a4b1fab1SGao Xiang 		q = fgq;
127260b30050SHongyu Jin 		init_completion(&fgq->u.done);
1273a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1274a4b1fab1SGao Xiang 	}
1275a4b1fab1SGao Xiang 	q->sb = sb;
1276a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1277a4b1fab1SGao Xiang 	return q;
127847e4937aSGao Xiang }
127947e4937aSGao Xiang 
128047e4937aSGao Xiang /* define decompression jobqueue types */
128147e4937aSGao Xiang enum {
128247e4937aSGao Xiang 	JQ_BYPASS,
128347e4937aSGao Xiang 	JQ_SUBMIT,
128447e4937aSGao Xiang 	NR_JOBQUEUES,
128547e4937aSGao Xiang };
128647e4937aSGao Xiang 
128747e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1288a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1289a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
129047e4937aSGao Xiang {
129147e4937aSGao Xiang 	/*
129247e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
129347e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
129447e4937aSGao Xiang 	 */
1295a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1296a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
129747e4937aSGao Xiang 
1298a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
129947e4937aSGao Xiang }
130047e4937aSGao Xiang 
130147e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
130247e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
130347e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
130447e4937aSGao Xiang {
130547e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
130647e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
130747e4937aSGao Xiang 
130847e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
130947e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
131047e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
131147e4937aSGao Xiang 
131247e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
131347e4937aSGao Xiang 
131447e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
131547e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
131647e4937aSGao Xiang 
131747e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
131847e4937aSGao Xiang }
131947e4937aSGao Xiang 
13207865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
13217865827cSGao Xiang {
13227865827cSGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
13237865827cSGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
13247865827cSGao Xiang 	blk_status_t err = bio->bi_status;
13257865827cSGao Xiang 	struct bio_vec *bvec;
13267865827cSGao Xiang 	struct bvec_iter_all iter_all;
13277865827cSGao Xiang 
13287865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
13297865827cSGao Xiang 		struct page *page = bvec->bv_page;
13307865827cSGao Xiang 
13317865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
13327865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
13337865827cSGao Xiang 
13347865827cSGao Xiang 		if (err)
13357865827cSGao Xiang 			SetPageError(page);
13367865827cSGao Xiang 
13377865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
13387865827cSGao Xiang 			if (!err)
13397865827cSGao Xiang 				SetPageUptodate(page);
13407865827cSGao Xiang 			unlock_page(page);
13417865827cSGao Xiang 		}
13427865827cSGao Xiang 	}
13437865827cSGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
13447865827cSGao Xiang 	bio_put(bio);
13457865827cSGao Xiang }
13467865827cSGao Xiang 
134783a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1348eaa9172aSGao Xiang 				 struct page **pagepool,
1349a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1350a4b1fab1SGao Xiang 				 bool *force_fg)
135147e4937aSGao Xiang {
135283a386c0SGao Xiang 	struct super_block *sb = f->inode->i_sb;
135383a386c0SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
135447e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1355a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
135647e4937aSGao Xiang 	void *bi_private;
13575c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1358dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
13593f649ab7SKees Cook 	pgoff_t last_index;
1360dfeab2e9SGao Xiang 	struct block_device *last_bdev;
13611e4a2955SGao Xiang 	unsigned int nr_bios = 0;
13621e4a2955SGao Xiang 	struct bio *bio = NULL;
136347e4937aSGao Xiang 
1364a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1365a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1366a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
136747e4937aSGao Xiang 
136847e4937aSGao Xiang 	/* by default, all need io submission */
136947e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
137047e4937aSGao Xiang 
137147e4937aSGao Xiang 	do {
1372dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
137347e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
13741e4a2955SGao Xiang 		pgoff_t cur, end;
13751e4a2955SGao Xiang 		unsigned int i = 0;
13761e4a2955SGao Xiang 		bool bypass = true;
137747e4937aSGao Xiang 
137847e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
137947e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
138047e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
138147e4937aSGao Xiang 
138247e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
138347e4937aSGao Xiang 
1384cecf864dSYue Hu 		/* close the main owned chain at first */
1385cecf864dSYue Hu 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1386cecf864dSYue Hu 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1387cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1388cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1389cecf864dSYue Hu 			continue;
1390cecf864dSYue Hu 		}
1391cecf864dSYue Hu 
1392dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1393dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
1394dfeab2e9SGao Xiang 			.m_pa = blknr_to_addr(pcl->obj.index),
1395dfeab2e9SGao Xiang 		};
1396dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1397dfeab2e9SGao Xiang 
1398dfeab2e9SGao Xiang 		cur = erofs_blknr(mdev.m_pa);
13999f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
140047e4937aSGao Xiang 
14011e4a2955SGao Xiang 		do {
14021e4a2955SGao Xiang 			struct page *page;
140347e4937aSGao Xiang 
14041e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
140583a386c0SGao Xiang 							  mc);
14061e4a2955SGao Xiang 			if (!page)
14071e4a2955SGao Xiang 				continue;
140847e4937aSGao Xiang 
1409dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1410dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
141147e4937aSGao Xiang submit_bio_retry:
141294e4e153SGao Xiang 				submit_bio(bio);
141347e4937aSGao Xiang 				bio = NULL;
141447e4937aSGao Xiang 			}
141547e4937aSGao Xiang 
141647e4937aSGao Xiang 			if (!bio) {
141707888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
141807888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
14190c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1420dfeab2e9SGao Xiang 
1421dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
14221e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1423a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1424a5c0b780SGao Xiang 				bio->bi_private = bi_private;
14256ea5aad3SGao Xiang 				if (f->readahead)
14266ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
142747e4937aSGao Xiang 				++nr_bios;
142847e4937aSGao Xiang 			}
142947e4937aSGao Xiang 
14306c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
143147e4937aSGao Xiang 				goto submit_bio_retry;
143247e4937aSGao Xiang 
14331e4a2955SGao Xiang 			last_index = cur;
14341e4a2955SGao Xiang 			bypass = false;
14351e4a2955SGao Xiang 		} while (++cur < end);
143647e4937aSGao Xiang 
14371e4a2955SGao Xiang 		if (!bypass)
143847e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
143947e4937aSGao Xiang 		else
144047e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
144147e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
144247e4937aSGao Xiang 
144347e4937aSGao Xiang 	if (bio)
144494e4e153SGao Xiang 		submit_bio(bio);
144547e4937aSGao Xiang 
1446587a67b7SGao Xiang 	/*
1447587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1448587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1449587a67b7SGao Xiang 	 */
1450587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1451587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
14521e4a2955SGao Xiang 		return;
1453587a67b7SGao Xiang 	}
1454a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
145547e4937aSGao Xiang }
145647e4937aSGao Xiang 
145783a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1458eaa9172aSGao Xiang 			     struct page **pagepool, bool force_fg)
145947e4937aSGao Xiang {
1460a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
146147e4937aSGao Xiang 
14625c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
146347e4937aSGao Xiang 		return;
146483a386c0SGao Xiang 	z_erofs_submit_queue(f, pagepool, io, &force_fg);
146547e4937aSGao Xiang 
14660c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
14670c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
146847e4937aSGao Xiang 
146947e4937aSGao Xiang 	if (!force_fg)
147047e4937aSGao Xiang 		return;
147147e4937aSGao Xiang 
147247e4937aSGao Xiang 	/* wait until all bios are completed */
147360b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
147447e4937aSGao Xiang 
14750c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
14760c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
147747e4937aSGao Xiang }
147847e4937aSGao Xiang 
147938629291SGao Xiang /*
148038629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
148138629291SGao Xiang  * approximate readmore strategies as a start.
148238629291SGao Xiang  */
148338629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
148438629291SGao Xiang 				      struct readahead_control *rac,
148538629291SGao Xiang 				      erofs_off_t end,
1486eaa9172aSGao Xiang 				      struct page **pagepool,
148738629291SGao Xiang 				      bool backmost)
148838629291SGao Xiang {
148938629291SGao Xiang 	struct inode *inode = f->inode;
149038629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
149138629291SGao Xiang 	erofs_off_t cur;
149238629291SGao Xiang 	int err;
149338629291SGao Xiang 
149438629291SGao Xiang 	if (backmost) {
149538629291SGao Xiang 		map->m_la = end;
1496622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1497622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
149838629291SGao Xiang 		if (err)
149938629291SGao Xiang 			return;
150038629291SGao Xiang 
150138629291SGao Xiang 		/* expend ra for the trailing edge if readahead */
150238629291SGao Xiang 		if (rac) {
150338629291SGao Xiang 			loff_t newstart = readahead_pos(rac);
150438629291SGao Xiang 
150538629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
150638629291SGao Xiang 			readahead_expand(rac, newstart, cur - newstart);
150738629291SGao Xiang 			return;
150838629291SGao Xiang 		}
150938629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
151038629291SGao Xiang 	} else {
151138629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
151238629291SGao Xiang 
151338629291SGao Xiang 		if (!map->m_llen)
151438629291SGao Xiang 			return;
151538629291SGao Xiang 	}
151638629291SGao Xiang 
151738629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
151838629291SGao Xiang 	while (cur >= end) {
151938629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
152038629291SGao Xiang 		struct page *page;
152138629291SGao Xiang 
152238629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1523aa793b46SGao Xiang 		if (page) {
152438629291SGao Xiang 			if (PageUptodate(page)) {
152538629291SGao Xiang 				unlock_page(page);
1526aa793b46SGao Xiang 			} else {
152738629291SGao Xiang 				err = z_erofs_do_read_page(f, page, pagepool);
152838629291SGao Xiang 				if (err)
152938629291SGao Xiang 					erofs_err(inode->i_sb,
153038629291SGao Xiang 						  "readmore error at page %lu @ nid %llu",
153138629291SGao Xiang 						  index, EROFS_I(inode)->nid);
1532aa793b46SGao Xiang 			}
153338629291SGao Xiang 			put_page(page);
1534aa793b46SGao Xiang 		}
1535aa793b46SGao Xiang 
153638629291SGao Xiang 		if (cur < PAGE_SIZE)
153738629291SGao Xiang 			break;
153838629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
153938629291SGao Xiang 	}
154038629291SGao Xiang }
154138629291SGao Xiang 
1542a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio)
154347e4937aSGao Xiang {
1544a2e20a25SMatthew Wilcox (Oracle) 	struct page *page = &folio->page;
154547e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
154640452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
154747e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1548eaa9172aSGao Xiang 	struct page *pagepool = NULL;
154947e4937aSGao Xiang 	int err;
155047e4937aSGao Xiang 
155147e4937aSGao Xiang 	trace_erofs_readpage(page, false);
155247e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
155347e4937aSGao Xiang 
155438629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
155538629291SGao Xiang 				  &pagepool, true);
15561825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
155738629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
155838629291SGao Xiang 
15595c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
156047e4937aSGao Xiang 
156147e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
156283a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
156340452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, 0));
156447e4937aSGao Xiang 
156547e4937aSGao Xiang 	if (err)
15664f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
156747e4937aSGao Xiang 
156809c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1569eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
157047e4937aSGao Xiang 	return err;
157147e4937aSGao Xiang }
157247e4937aSGao Xiang 
15730615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
157447e4937aSGao Xiang {
15750615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
157647e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
157747e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1578eaa9172aSGao Xiang 	struct page *pagepool = NULL, *head = NULL, *page;
157938629291SGao Xiang 	unsigned int nr_pages;
158047e4937aSGao Xiang 
15816ea5aad3SGao Xiang 	f.readahead = true;
15820615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
158347e4937aSGao Xiang 
158438629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, f.headoffset +
158538629291SGao Xiang 				  readahead_length(rac) - 1, &pagepool, true);
158638629291SGao Xiang 	nr_pages = readahead_count(rac);
158738629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
158838629291SGao Xiang 
15890615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
159047e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
159147e4937aSGao Xiang 		head = page;
159247e4937aSGao Xiang 	}
159347e4937aSGao Xiang 
159447e4937aSGao Xiang 	while (head) {
159547e4937aSGao Xiang 		struct page *page = head;
159647e4937aSGao Xiang 		int err;
159747e4937aSGao Xiang 
159847e4937aSGao Xiang 		/* traversal in reverse order */
159947e4937aSGao Xiang 		head = (void *)page_private(page);
160047e4937aSGao Xiang 
16011825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1602a5876e24SGao Xiang 		if (err)
16034f761fa2SGao Xiang 			erofs_err(inode->i_sb,
16044f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
16054f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
160647e4937aSGao Xiang 		put_page(page);
160747e4937aSGao Xiang 	}
160838629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
16095c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
161047e4937aSGao Xiang 
161183a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
161240452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
161309c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1614eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
161547e4937aSGao Xiang }
161647e4937aSGao Xiang 
16170c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
1618a2e20a25SMatthew Wilcox (Oracle) 	.read_folio = z_erofs_read_folio,
16190615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
162047e4937aSGao Xiang };
1621