xref: /openbmc/linux/fs/erofs/zdata.c (revision a9a94d93)
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 "compress.h"
847e4937aSGao Xiang #include <linux/prefetch.h>
999486c51SChristoph Hellwig #include <linux/psi.h>
1047e4937aSGao Xiang 
1147e4937aSGao Xiang #include <trace/events/erofs.h>
1247e4937aSGao Xiang 
13*a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_MAX_PAGES	(Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
14*a9a94d93SGao Xiang #define Z_EROFS_INLINE_BVECS		2
15*a9a94d93SGao Xiang 
16*a9a94d93SGao Xiang /*
17*a9a94d93SGao Xiang  * let's leave a type here in case of introducing
18*a9a94d93SGao Xiang  * another tagged pointer later.
19*a9a94d93SGao Xiang  */
20*a9a94d93SGao Xiang typedef void *z_erofs_next_pcluster_t;
21*a9a94d93SGao Xiang 
22*a9a94d93SGao Xiang struct z_erofs_bvec {
23*a9a94d93SGao Xiang 	struct page *page;
24*a9a94d93SGao Xiang 	int offset;
25*a9a94d93SGao Xiang 	unsigned int end;
26*a9a94d93SGao Xiang };
27*a9a94d93SGao Xiang 
28*a9a94d93SGao Xiang #define __Z_EROFS_BVSET(name, total) \
29*a9a94d93SGao Xiang struct name { \
30*a9a94d93SGao Xiang 	/* point to the next page which contains the following bvecs */ \
31*a9a94d93SGao Xiang 	struct page *nextpage; \
32*a9a94d93SGao Xiang 	struct z_erofs_bvec bvec[total]; \
33*a9a94d93SGao Xiang }
34*a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset,);
35*a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
36*a9a94d93SGao Xiang 
37*a9a94d93SGao Xiang /*
38*a9a94d93SGao Xiang  * Structure fields follow one of the following exclusion rules.
39*a9a94d93SGao Xiang  *
40*a9a94d93SGao Xiang  * I: Modifiable by initialization/destruction paths and read-only
41*a9a94d93SGao Xiang  *    for everyone else;
42*a9a94d93SGao Xiang  *
43*a9a94d93SGao Xiang  * L: Field should be protected by the pcluster lock;
44*a9a94d93SGao Xiang  *
45*a9a94d93SGao Xiang  * A: Field should be accessed / updated in atomic for parallelized code.
46*a9a94d93SGao Xiang  */
47*a9a94d93SGao Xiang struct z_erofs_pcluster {
48*a9a94d93SGao Xiang 	struct erofs_workgroup obj;
49*a9a94d93SGao Xiang 	struct mutex lock;
50*a9a94d93SGao Xiang 
51*a9a94d93SGao Xiang 	/* A: point to next chained pcluster or TAILs */
52*a9a94d93SGao Xiang 	z_erofs_next_pcluster_t next;
53*a9a94d93SGao Xiang 
54*a9a94d93SGao Xiang 	/* L: the maximum decompression size of this round */
55*a9a94d93SGao Xiang 	unsigned int length;
56*a9a94d93SGao Xiang 
57*a9a94d93SGao Xiang 	/* L: total number of bvecs */
58*a9a94d93SGao Xiang 	unsigned int vcnt;
59*a9a94d93SGao Xiang 
60*a9a94d93SGao Xiang 	/* I: page offset of start position of decompression */
61*a9a94d93SGao Xiang 	unsigned short pageofs_out;
62*a9a94d93SGao Xiang 
63*a9a94d93SGao Xiang 	/* I: page offset of inline compressed data */
64*a9a94d93SGao Xiang 	unsigned short pageofs_in;
65*a9a94d93SGao Xiang 
66*a9a94d93SGao Xiang 	union {
67*a9a94d93SGao Xiang 		/* L: inline a certain number of bvec for bootstrap */
68*a9a94d93SGao Xiang 		struct z_erofs_bvset_inline bvset;
69*a9a94d93SGao Xiang 
70*a9a94d93SGao Xiang 		/* I: can be used to free the pcluster by RCU. */
71*a9a94d93SGao Xiang 		struct rcu_head rcu;
72*a9a94d93SGao Xiang 	};
73*a9a94d93SGao Xiang 
74*a9a94d93SGao Xiang 	union {
75*a9a94d93SGao Xiang 		/* I: physical cluster size in pages */
76*a9a94d93SGao Xiang 		unsigned short pclusterpages;
77*a9a94d93SGao Xiang 
78*a9a94d93SGao Xiang 		/* I: tailpacking inline compressed size */
79*a9a94d93SGao Xiang 		unsigned short tailpacking_size;
80*a9a94d93SGao Xiang 	};
81*a9a94d93SGao Xiang 
82*a9a94d93SGao Xiang 	/* I: compression algorithm format */
83*a9a94d93SGao Xiang 	unsigned char algorithmformat;
84*a9a94d93SGao Xiang 
85*a9a94d93SGao Xiang 	/* L: whether partial decompression or not */
86*a9a94d93SGao Xiang 	bool partial;
87*a9a94d93SGao Xiang 
88*a9a94d93SGao Xiang 	/* L: indicate several pageofs_outs or not */
89*a9a94d93SGao Xiang 	bool multibases;
90*a9a94d93SGao Xiang 
91*a9a94d93SGao Xiang 	/* A: compressed bvecs (can be cached or inplaced pages) */
92*a9a94d93SGao Xiang 	struct z_erofs_bvec compressed_bvecs[];
93*a9a94d93SGao Xiang };
94*a9a94d93SGao Xiang 
95*a9a94d93SGao Xiang /* let's avoid the valid 32-bit kernel addresses */
96*a9a94d93SGao Xiang 
97*a9a94d93SGao Xiang /* the chained workgroup has't submitted io (still open) */
98*a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_TAIL           ((void *)0x5F0ECAFE)
99*a9a94d93SGao Xiang /* the chained workgroup has already submitted io */
100*a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_TAIL_CLOSED    ((void *)0x5F0EDEAD)
101*a9a94d93SGao Xiang 
102*a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_NIL            (NULL)
103*a9a94d93SGao Xiang 
104*a9a94d93SGao Xiang struct z_erofs_decompressqueue {
105*a9a94d93SGao Xiang 	struct super_block *sb;
106*a9a94d93SGao Xiang 	atomic_t pending_bios;
107*a9a94d93SGao Xiang 	z_erofs_next_pcluster_t head;
108*a9a94d93SGao Xiang 
109*a9a94d93SGao Xiang 	union {
110*a9a94d93SGao Xiang 		struct completion done;
111*a9a94d93SGao Xiang 		struct work_struct work;
112*a9a94d93SGao Xiang 	} u;
113*a9a94d93SGao Xiang 	bool eio, sync;
114*a9a94d93SGao Xiang };
115*a9a94d93SGao Xiang 
116*a9a94d93SGao Xiang static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
117*a9a94d93SGao Xiang {
118*a9a94d93SGao Xiang 	return !pcl->obj.index;
119*a9a94d93SGao Xiang }
120*a9a94d93SGao Xiang 
121*a9a94d93SGao Xiang static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
122*a9a94d93SGao Xiang {
123*a9a94d93SGao Xiang 	if (z_erofs_is_inline_pcluster(pcl))
124*a9a94d93SGao Xiang 		return 1;
125*a9a94d93SGao Xiang 	return pcl->pclusterpages;
126*a9a94d93SGao Xiang }
127*a9a94d93SGao Xiang 
128*a9a94d93SGao Xiang /*
129*a9a94d93SGao Xiang  * bit 30: I/O error occurred on this page
130*a9a94d93SGao Xiang  * bit 0 - 29: remaining parts to complete this page
131*a9a94d93SGao Xiang  */
132*a9a94d93SGao Xiang #define Z_EROFS_PAGE_EIO			(1 << 30)
133*a9a94d93SGao Xiang 
134*a9a94d93SGao Xiang static inline void z_erofs_onlinepage_init(struct page *page)
135*a9a94d93SGao Xiang {
136*a9a94d93SGao Xiang 	union {
137*a9a94d93SGao Xiang 		atomic_t o;
138*a9a94d93SGao Xiang 		unsigned long v;
139*a9a94d93SGao Xiang 	} u = { .o = ATOMIC_INIT(1) };
140*a9a94d93SGao Xiang 
141*a9a94d93SGao Xiang 	set_page_private(page, u.v);
142*a9a94d93SGao Xiang 	smp_wmb();
143*a9a94d93SGao Xiang 	SetPagePrivate(page);
144*a9a94d93SGao Xiang }
145*a9a94d93SGao Xiang 
146*a9a94d93SGao Xiang static inline void z_erofs_onlinepage_split(struct page *page)
147*a9a94d93SGao Xiang {
148*a9a94d93SGao Xiang 	atomic_inc((atomic_t *)&page->private);
149*a9a94d93SGao Xiang }
150*a9a94d93SGao Xiang 
151*a9a94d93SGao Xiang static inline void z_erofs_page_mark_eio(struct page *page)
152*a9a94d93SGao Xiang {
153*a9a94d93SGao Xiang 	int orig;
154*a9a94d93SGao Xiang 
155*a9a94d93SGao Xiang 	do {
156*a9a94d93SGao Xiang 		orig = atomic_read((atomic_t *)&page->private);
157*a9a94d93SGao Xiang 	} while (atomic_cmpxchg((atomic_t *)&page->private, orig,
158*a9a94d93SGao Xiang 				orig | Z_EROFS_PAGE_EIO) != orig);
159*a9a94d93SGao Xiang }
160*a9a94d93SGao Xiang 
161*a9a94d93SGao Xiang static inline void z_erofs_onlinepage_endio(struct page *page)
162*a9a94d93SGao Xiang {
163*a9a94d93SGao Xiang 	unsigned int v;
164*a9a94d93SGao Xiang 
165*a9a94d93SGao Xiang 	DBG_BUGON(!PagePrivate(page));
166*a9a94d93SGao Xiang 	v = atomic_dec_return((atomic_t *)&page->private);
167*a9a94d93SGao Xiang 	if (!(v & ~Z_EROFS_PAGE_EIO)) {
168*a9a94d93SGao Xiang 		set_page_private(page, 0);
169*a9a94d93SGao Xiang 		ClearPagePrivate(page);
170*a9a94d93SGao Xiang 		if (!(v & Z_EROFS_PAGE_EIO))
171*a9a94d93SGao Xiang 			SetPageUptodate(page);
172*a9a94d93SGao Xiang 		unlock_page(page);
173*a9a94d93SGao Xiang 	}
174*a9a94d93SGao Xiang }
175*a9a94d93SGao Xiang 
176*a9a94d93SGao Xiang #define Z_EROFS_ONSTACK_PAGES		32
177*a9a94d93SGao Xiang 
17847e4937aSGao Xiang /*
1799f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
1809f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
1819f6cc76eSGao Xiang  */
1829f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
1839f6cc76eSGao Xiang 	struct kmem_cache *slab;
1849f6cc76eSGao Xiang 	unsigned int maxpages;
1859f6cc76eSGao Xiang 	char name[48];
1869f6cc76eSGao Xiang };
1879f6cc76eSGao Xiang 
1889f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
1899f6cc76eSGao Xiang 
1909f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
1919f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
1929f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
1939f6cc76eSGao Xiang };
1949f6cc76eSGao Xiang 
19506a304cdSGao Xiang struct z_erofs_bvec_iter {
19606a304cdSGao Xiang 	struct page *bvpage;
19706a304cdSGao Xiang 	struct z_erofs_bvset *bvset;
19806a304cdSGao Xiang 	unsigned int nr, cur;
19906a304cdSGao Xiang };
20006a304cdSGao Xiang 
20106a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
20206a304cdSGao Xiang {
20306a304cdSGao Xiang 	if (iter->bvpage)
20406a304cdSGao Xiang 		kunmap_local(iter->bvset);
20506a304cdSGao Xiang 	return iter->bvpage;
20606a304cdSGao Xiang }
20706a304cdSGao Xiang 
20806a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
20906a304cdSGao Xiang {
21006a304cdSGao Xiang 	unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
21106a304cdSGao Xiang 	/* have to access nextpage in advance, otherwise it will be unmapped */
21206a304cdSGao Xiang 	struct page *nextpage = iter->bvset->nextpage;
21306a304cdSGao Xiang 	struct page *oldpage;
21406a304cdSGao Xiang 
21506a304cdSGao Xiang 	DBG_BUGON(!nextpage);
21606a304cdSGao Xiang 	oldpage = z_erofs_bvec_iter_end(iter);
21706a304cdSGao Xiang 	iter->bvpage = nextpage;
21806a304cdSGao Xiang 	iter->bvset = kmap_local_page(nextpage);
21906a304cdSGao Xiang 	iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
22006a304cdSGao Xiang 	iter->cur = 0;
22106a304cdSGao Xiang 	return oldpage;
22206a304cdSGao Xiang }
22306a304cdSGao Xiang 
22406a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
22506a304cdSGao Xiang 				    struct z_erofs_bvset_inline *bvset,
22606a304cdSGao Xiang 				    unsigned int bootstrap_nr,
22706a304cdSGao Xiang 				    unsigned int cur)
22806a304cdSGao Xiang {
22906a304cdSGao Xiang 	*iter = (struct z_erofs_bvec_iter) {
23006a304cdSGao Xiang 		.nr = bootstrap_nr,
23106a304cdSGao Xiang 		.bvset = (struct z_erofs_bvset *)bvset,
23206a304cdSGao Xiang 	};
23306a304cdSGao Xiang 
23406a304cdSGao Xiang 	while (cur > iter->nr) {
23506a304cdSGao Xiang 		cur -= iter->nr;
23606a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
23706a304cdSGao Xiang 	}
23806a304cdSGao Xiang 	iter->cur = cur;
23906a304cdSGao Xiang }
24006a304cdSGao Xiang 
24106a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
24206a304cdSGao Xiang 				struct z_erofs_bvec *bvec,
24306a304cdSGao Xiang 				struct page **candidate_bvpage)
24406a304cdSGao Xiang {
24506a304cdSGao Xiang 	if (iter->cur == iter->nr) {
24606a304cdSGao Xiang 		if (!*candidate_bvpage)
24706a304cdSGao Xiang 			return -EAGAIN;
24806a304cdSGao Xiang 
24906a304cdSGao Xiang 		DBG_BUGON(iter->bvset->nextpage);
25006a304cdSGao Xiang 		iter->bvset->nextpage = *candidate_bvpage;
25106a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
25206a304cdSGao Xiang 
25306a304cdSGao Xiang 		iter->bvset->nextpage = NULL;
25406a304cdSGao Xiang 		*candidate_bvpage = NULL;
25506a304cdSGao Xiang 	}
25606a304cdSGao Xiang 	iter->bvset->bvec[iter->cur++] = *bvec;
25706a304cdSGao Xiang 	return 0;
25806a304cdSGao Xiang }
25906a304cdSGao Xiang 
26006a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
26106a304cdSGao Xiang 				 struct z_erofs_bvec *bvec,
26206a304cdSGao Xiang 				 struct page **old_bvpage)
26306a304cdSGao Xiang {
26406a304cdSGao Xiang 	if (iter->cur == iter->nr)
26506a304cdSGao Xiang 		*old_bvpage = z_erofs_bvset_flip(iter);
26606a304cdSGao Xiang 	else
26706a304cdSGao Xiang 		*old_bvpage = NULL;
26806a304cdSGao Xiang 	*bvec = iter->bvset->bvec[iter->cur++];
26906a304cdSGao Xiang }
27006a304cdSGao Xiang 
2719f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
2729f6cc76eSGao Xiang {
2739f6cc76eSGao Xiang 	int i;
2749f6cc76eSGao Xiang 
2759f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
2769f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
2779f6cc76eSGao Xiang 			continue;
2789f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
2799f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
2809f6cc76eSGao Xiang 	}
2819f6cc76eSGao Xiang }
2829f6cc76eSGao Xiang 
2839f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
2849f6cc76eSGao Xiang {
2859f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
2869f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
2879f6cc76eSGao Xiang 	unsigned int size;
2889f6cc76eSGao Xiang 
2899f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
2909f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
291ed722fbcSGao Xiang 		size = struct_size(a, compressed_bvecs, pcs->maxpages);
2929f6cc76eSGao Xiang 
2939f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
2949f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
2959f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
2969f6cc76eSGao Xiang 		if (pcs->slab)
2979f6cc76eSGao Xiang 			continue;
2989f6cc76eSGao Xiang 
2999f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
3009f6cc76eSGao Xiang 		return -ENOMEM;
3019f6cc76eSGao Xiang 	}
3029f6cc76eSGao Xiang 	return 0;
3039f6cc76eSGao Xiang }
3049f6cc76eSGao Xiang 
3059f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
3069f6cc76eSGao Xiang {
3079f6cc76eSGao Xiang 	int i;
3089f6cc76eSGao Xiang 
3099f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3109f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3119f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
3129f6cc76eSGao Xiang 
3139f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
3149f6cc76eSGao Xiang 			continue;
3159f6cc76eSGao Xiang 
3169f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
3179f6cc76eSGao Xiang 		if (!pcl)
3189f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
3199f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
3209f6cc76eSGao Xiang 		return pcl;
3219f6cc76eSGao Xiang 	}
3229f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
3239f6cc76eSGao Xiang }
3249f6cc76eSGao Xiang 
3259f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
3269f6cc76eSGao Xiang {
327cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
3289f6cc76eSGao Xiang 	int i;
3299f6cc76eSGao Xiang 
3309f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3319f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3329f6cc76eSGao Xiang 
333cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
3349f6cc76eSGao Xiang 			continue;
3359f6cc76eSGao Xiang 
3369f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
3379f6cc76eSGao Xiang 		return;
3389f6cc76eSGao Xiang 	}
3399f6cc76eSGao Xiang 	DBG_BUGON(1);
3409f6cc76eSGao Xiang }
3419f6cc76eSGao Xiang 
34247e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
34347e4937aSGao Xiang 
34447e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
34547e4937aSGao Xiang {
34647e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
3479f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
34847e4937aSGao Xiang }
34947e4937aSGao Xiang 
35099634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
35147e4937aSGao Xiang {
35247e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
35347e4937aSGao Xiang 
35447e4937aSGao Xiang 	/*
35547e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
35647e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
35747e4937aSGao Xiang 	 */
3580e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
3590e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
36047e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
36147e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
36247e4937aSGao Xiang }
36347e4937aSGao Xiang 
36447e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
36547e4937aSGao Xiang {
3669f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
36747e4937aSGao Xiang 
3689f6cc76eSGao Xiang 	if (err)
3699f6cc76eSGao Xiang 		return err;
3709f6cc76eSGao Xiang 	err = z_erofs_init_workqueue();
3719f6cc76eSGao Xiang 	if (err)
3729f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
3739f6cc76eSGao Xiang 	return err;
37447e4937aSGao Xiang }
37547e4937aSGao Xiang 
376db166fc2SGao Xiang enum z_erofs_pclustermode {
377db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_INFLIGHT,
37847e4937aSGao Xiang 	/*
379db166fc2SGao Xiang 	 * The current pclusters was the tail of an exist chain, in addition
380db166fc2SGao Xiang 	 * that the previous processed chained pclusters are all decided to
38147e4937aSGao Xiang 	 * be hooked up to it.
382db166fc2SGao Xiang 	 * A new chain will be created for the remaining pclusters which are
383db166fc2SGao Xiang 	 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
384db166fc2SGao Xiang 	 * the next pcluster cannot reuse the whole page safely for inplace I/O
385db166fc2SGao Xiang 	 * in the following scenario:
38647e4937aSGao Xiang 	 *  ________________________________________________________________
38747e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
388db166fc2SGao Xiang 	 * |   (belongs to the next pcl)  |   (belongs to the current pcl)  |
389db166fc2SGao Xiang 	 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
39047e4937aSGao Xiang 	 */
391db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_HOOKED,
3920b964600SGao Xiang 	/*
393db166fc2SGao Xiang 	 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
3940b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
3950b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
396387bab87SGao Xiang 	 * bvpage) since it can be directly decoded without I/O submission.
3970b964600SGao Xiang 	 */
398db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
39947e4937aSGao Xiang 	/*
40047e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
40147e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
40247e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
40347e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
40447e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
40547e4937aSGao Xiang 	 * illustrated below:
40647e4937aSGao Xiang 	 *  ________________________________________________________________
40747e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
40847e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
409db166fc2SGao Xiang 	 * | PCLUSTER_FOLLOWED or |                                        |
410db166fc2SGao Xiang 	 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
41147e4937aSGao Xiang 	 *
41247e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
41347e4937aSGao Xiang 	 */
414db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED,
41547e4937aSGao Xiang };
41647e4937aSGao Xiang 
4175c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
4185c6dcc57SGao Xiang 	struct inode *const inode;
4195c6dcc57SGao Xiang 	struct erofs_map_blocks map;
42006a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
42147e4937aSGao Xiang 
42206a304cdSGao Xiang 	struct page *candidate_bvpage;
42347e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
42447e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
425db166fc2SGao Xiang 	enum z_erofs_pclustermode mode;
42647e4937aSGao Xiang 
4276ea5aad3SGao Xiang 	bool readahead;
42847e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
42947e4937aSGao Xiang 	bool backmost;
43047e4937aSGao Xiang 	erofs_off_t headoffset;
431ed722fbcSGao Xiang 
432ed722fbcSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
433ed722fbcSGao Xiang 	unsigned int icur;
43447e4937aSGao Xiang };
43547e4937aSGao Xiang 
43647e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
4375c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
438db166fc2SGao Xiang 	.mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
43947e4937aSGao Xiang 
4401282dea3SGao Xiang static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
4411282dea3SGao Xiang {
4421282dea3SGao Xiang 	unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
4431282dea3SGao Xiang 
4441282dea3SGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
4451282dea3SGao Xiang 		return false;
4461282dea3SGao Xiang 
4471282dea3SGao Xiang 	if (fe->backmost)
4481282dea3SGao Xiang 		return true;
4491282dea3SGao Xiang 
4501282dea3SGao Xiang 	if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
4511282dea3SGao Xiang 	    fe->map.m_la < fe->headoffset)
4521282dea3SGao Xiang 		return true;
4531282dea3SGao Xiang 
4541282dea3SGao Xiang 	return false;
4551282dea3SGao Xiang }
4561282dea3SGao Xiang 
4576f39d1e1SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
458eaa9172aSGao Xiang 			       struct page **pagepool)
45947e4937aSGao Xiang {
4606f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
4615c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
4621282dea3SGao Xiang 	bool shouldalloc = z_erofs_should_alloc_cache(fe);
46347e4937aSGao Xiang 	bool standalone = true;
4646f39d1e1SGao Xiang 	/*
4656f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
4666f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
4676f39d1e1SGao Xiang 	 */
4681825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
4691825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
470ed722fbcSGao Xiang 	unsigned int i;
47147e4937aSGao Xiang 
472db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
47347e4937aSGao Xiang 		return;
47447e4937aSGao Xiang 
475ed722fbcSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
47647e4937aSGao Xiang 		struct page *page;
477b1ed220cSGao Xiang 		void *t;	/* mark pages just found for debugging */
4781825c8d7SGao Xiang 		struct page *newpage = NULL;
47947e4937aSGao Xiang 
48047e4937aSGao Xiang 		/* the compressed page was loaded before */
481ed722fbcSGao Xiang 		if (READ_ONCE(pcl->compressed_bvecs[i].page))
48247e4937aSGao Xiang 			continue;
48347e4937aSGao Xiang 
484ed722fbcSGao Xiang 		page = find_get_page(mc, pcl->obj.index + i);
48547e4937aSGao Xiang 
48647e4937aSGao Xiang 		if (page) {
487b1ed220cSGao Xiang 			t = (void *)((unsigned long)page | 1);
4880b964600SGao Xiang 		} else {
4890b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
4900b964600SGao Xiang 			standalone = false;
4911282dea3SGao Xiang 			if (!shouldalloc)
4921282dea3SGao Xiang 				continue;
4931282dea3SGao Xiang 
4941282dea3SGao Xiang 			/*
4951282dea3SGao Xiang 			 * try to use cached I/O if page allocation
4961282dea3SGao Xiang 			 * succeeds or fallback to in-place I/O instead
4971282dea3SGao Xiang 			 * to avoid any direct reclaim.
4981282dea3SGao Xiang 			 */
4991825c8d7SGao Xiang 			newpage = erofs_allocpage(pagepool, gfp);
5001825c8d7SGao Xiang 			if (!newpage)
50147e4937aSGao Xiang 				continue;
5021282dea3SGao Xiang 			set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
503b1ed220cSGao Xiang 			t = (void *)((unsigned long)newpage | 1);
50447e4937aSGao Xiang 		}
50547e4937aSGao Xiang 
506b1ed220cSGao Xiang 		if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
50747e4937aSGao Xiang 			continue;
50847e4937aSGao Xiang 
509eaa9172aSGao Xiang 		if (page)
51047e4937aSGao Xiang 			put_page(page);
511eaa9172aSGao Xiang 		else if (newpage)
512eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
51347e4937aSGao Xiang 	}
51447e4937aSGao Xiang 
5150b964600SGao Xiang 	/*
5160b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
5170b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
5180b964600SGao Xiang 	 */
5190b964600SGao Xiang 	if (standalone)
520db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
52147e4937aSGao Xiang }
52247e4937aSGao Xiang 
52347e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
52447e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
52547e4937aSGao Xiang 				       struct erofs_workgroup *grp)
52647e4937aSGao Xiang {
52747e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
52847e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
52947e4937aSGao Xiang 	int i;
53047e4937aSGao Xiang 
531cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
53247e4937aSGao Xiang 	/*
53347e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
53447e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
53547e4937aSGao Xiang 	 */
5369f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
537ed722fbcSGao Xiang 		struct page *page = pcl->compressed_bvecs[i].page;
53847e4937aSGao Xiang 
53947e4937aSGao Xiang 		if (!page)
54047e4937aSGao Xiang 			continue;
54147e4937aSGao Xiang 
54247e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
54347e4937aSGao Xiang 		if (!trylock_page(page))
54447e4937aSGao Xiang 			return -EBUSY;
54547e4937aSGao Xiang 
546f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
54747e4937aSGao Xiang 			continue;
54847e4937aSGao Xiang 
54947e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
550ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
5516aaa7b06SGao Xiang 		detach_page_private(page);
55247e4937aSGao Xiang 		unlock_page(page);
55347e4937aSGao Xiang 	}
55447e4937aSGao Xiang 	return 0;
55547e4937aSGao Xiang }
55647e4937aSGao Xiang 
557d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
55847e4937aSGao Xiang {
55947e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
560ed722fbcSGao Xiang 	int ret, i;
56147e4937aSGao Xiang 
562ed722fbcSGao Xiang 	if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
563ed722fbcSGao Xiang 		return 0;
56447e4937aSGao Xiang 
565ed722fbcSGao Xiang 	ret = 0;
566cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
5679f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
568ed722fbcSGao Xiang 		if (pcl->compressed_bvecs[i].page == page) {
569ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
57047e4937aSGao Xiang 			ret = 1;
57147e4937aSGao Xiang 			break;
57247e4937aSGao Xiang 		}
57347e4937aSGao Xiang 	}
57447e4937aSGao Xiang 	erofs_workgroup_unfreeze(&pcl->obj, 1);
5756aaa7b06SGao Xiang 	if (ret)
5766aaa7b06SGao Xiang 		detach_page_private(page);
57747e4937aSGao Xiang 	return ret;
57847e4937aSGao Xiang }
57947e4937aSGao Xiang 
5805c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
581ed722fbcSGao Xiang 				   struct z_erofs_bvec *bvec)
58247e4937aSGao Xiang {
5835c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
58447e4937aSGao Xiang 
585ed722fbcSGao Xiang 	while (fe->icur > 0) {
586ed722fbcSGao Xiang 		if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
587ed722fbcSGao Xiang 			     NULL, bvec->page)) {
588ed722fbcSGao Xiang 			pcl->compressed_bvecs[fe->icur] = *bvec;
58947e4937aSGao Xiang 			return true;
590ed722fbcSGao Xiang 		}
591ed722fbcSGao Xiang 	}
59247e4937aSGao Xiang 	return false;
59347e4937aSGao Xiang }
59447e4937aSGao Xiang 
59587ca34a7SGao Xiang /* callers must be with pcluster lock held */
5965c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
5975b220b20SGao Xiang 			       struct z_erofs_bvec *bvec, bool exclusive)
59847e4937aSGao Xiang {
59947e4937aSGao Xiang 	int ret;
60047e4937aSGao Xiang 
601db166fc2SGao Xiang 	if (exclusive) {
60206a304cdSGao Xiang 		/* give priority for inplaceio to use file pages first */
603ed722fbcSGao Xiang 		if (z_erofs_try_inplace_io(fe, bvec))
60447e4937aSGao Xiang 			return 0;
60506a304cdSGao Xiang 		/* otherwise, check if it can be used as a bvpage */
606db166fc2SGao Xiang 		if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
60706a304cdSGao Xiang 		    !fe->candidate_bvpage)
60806a304cdSGao Xiang 			fe->candidate_bvpage = bvec->page;
60906a304cdSGao Xiang 	}
61006a304cdSGao Xiang 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
61106a304cdSGao Xiang 	fe->pcl->vcnt += (ret >= 0);
61206a304cdSGao Xiang 	return ret;
61347e4937aSGao Xiang }
61447e4937aSGao Xiang 
6155c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
61647e4937aSGao Xiang {
6175c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
6185c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
61947e4937aSGao Xiang 
620473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
621473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
622473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
62347e4937aSGao Xiang 		*owned_head = &pcl->next;
624473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
625db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
626473e15b0SGao Xiang 		return;
627473e15b0SGao Xiang 	}
628473e15b0SGao Xiang 
62947e4937aSGao Xiang 	/*
630473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
631473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
63247e4937aSGao Xiang 	 */
633267f2492SGao Xiang 	if (*owned_head != &pcl->next && pcl != f->tailpcl &&
634267f2492SGao Xiang 	    cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
635473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
63647e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
637db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_HOOKED;
6385c6dcc57SGao Xiang 		f->tailpcl = NULL;
639473e15b0SGao Xiang 		return;
64047e4937aSGao Xiang 	}
641473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
642db166fc2SGao Xiang 	f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
64347e4937aSGao Xiang }
64447e4937aSGao Xiang 
64583a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
64647e4937aSGao Xiang {
64783a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
648cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
64947e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
65064094a04SGao Xiang 	struct erofs_workgroup *grp;
65147e4937aSGao Xiang 	int err;
65247e4937aSGao Xiang 
653c42c0ffeSChen Zhongjin 	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
654c42c0ffeSChen Zhongjin 	    (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
6558f899262SGao Xiang 		DBG_BUGON(1);
6568f899262SGao Xiang 		return -EFSCORRUPTED;
6578f899262SGao Xiang 	}
6588f899262SGao Xiang 
6599f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
660cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
661cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
6629f6cc76eSGao Xiang 	if (IS_ERR(pcl))
6639f6cc76eSGao Xiang 		return PTR_ERR(pcl);
66447e4937aSGao Xiang 
66564094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
6668f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
6672bfab9c0SGao Xiang 	pcl->length = 0;
6682bfab9c0SGao Xiang 	pcl->partial = true;
66947e4937aSGao Xiang 
67047e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
6715c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
67287ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
673db166fc2SGao Xiang 	fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
67447e4937aSGao Xiang 
67547e4937aSGao Xiang 	/*
67647e4937aSGao Xiang 	 * lock all primary followed works before visible to others
67747e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
67847e4937aSGao Xiang 	 */
67987ca34a7SGao Xiang 	mutex_init(&pcl->lock);
68087ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
68147e4937aSGao Xiang 
682cecf864dSYue Hu 	if (ztailpacking) {
683cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
684cecf864dSYue Hu 		pcl->pageofs_in = erofs_blkoff(map->m_pa);
685cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
686cecf864dSYue Hu 	} else {
687cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
688cecf864dSYue Hu 
68983a386c0SGao Xiang 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
69064094a04SGao Xiang 		if (IS_ERR(grp)) {
69164094a04SGao Xiang 			err = PTR_ERR(grp);
69264094a04SGao Xiang 			goto err_out;
69364094a04SGao Xiang 		}
69464094a04SGao Xiang 
69564094a04SGao Xiang 		if (grp != &pcl->obj) {
6965c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
697cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
69864094a04SGao Xiang 			err = -EEXIST;
69964094a04SGao Xiang 			goto err_out;
70047e4937aSGao Xiang 		}
701cecf864dSYue Hu 	}
70247e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
7035c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
7045c6dcc57SGao Xiang 		fe->tailpcl = pcl;
7055c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
7065c6dcc57SGao Xiang 	fe->pcl = pcl;
7079e579fc1SGao Xiang 	return 0;
70864094a04SGao Xiang 
70964094a04SGao Xiang err_out:
71087ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
7119f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
71264094a04SGao Xiang 	return err;
71347e4937aSGao Xiang }
71447e4937aSGao Xiang 
71583a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
71647e4937aSGao Xiang {
71783a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
7180d823b42SGao Xiang 	struct erofs_workgroup *grp = NULL;
7199e579fc1SGao Xiang 	int ret;
72047e4937aSGao Xiang 
72187ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
72247e4937aSGao Xiang 
72387ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
7245c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
7255c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
72647e4937aSGao Xiang 
7270d823b42SGao Xiang 	if (!(map->m_flags & EROFS_MAP_META)) {
7280d823b42SGao Xiang 		grp = erofs_find_workgroup(fe->inode->i_sb,
7290d823b42SGao Xiang 					   map->m_pa >> PAGE_SHIFT);
7300d823b42SGao Xiang 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
73147e4937aSGao Xiang 		DBG_BUGON(1);
732cecf864dSYue Hu 		return -EFSCORRUPTED;
733cecf864dSYue Hu 	}
73447e4937aSGao Xiang 
73564094a04SGao Xiang 	if (grp) {
7365c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
7370d823b42SGao Xiang 		ret = -EEXIST;
73864094a04SGao Xiang 	} else {
73983a386c0SGao Xiang 		ret = z_erofs_register_pcluster(fe);
74064094a04SGao Xiang 	}
74147e4937aSGao Xiang 
7420d823b42SGao Xiang 	if (ret == -EEXIST) {
743267f2492SGao Xiang 		mutex_lock(&fe->pcl->lock);
744267f2492SGao Xiang 		/* used to check tail merging loop due to corrupted images */
745267f2492SGao Xiang 		if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
746267f2492SGao Xiang 			fe->tailpcl = fe->pcl;
747267f2492SGao Xiang 
748267f2492SGao Xiang 		z_erofs_try_to_claim_pcluster(fe);
7490d823b42SGao Xiang 	} else if (ret) {
7500d823b42SGao Xiang 		return ret;
7510d823b42SGao Xiang 	}
75206a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
753387bab87SGao Xiang 				Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
75481382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
755ed722fbcSGao Xiang 	fe->icur = z_erofs_pclusterpages(fe->pcl);
75647e4937aSGao Xiang 	return 0;
75747e4937aSGao Xiang }
75847e4937aSGao Xiang 
75947e4937aSGao Xiang /*
76047e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
76147e4937aSGao Xiang  * only after a RCU grace period.
76247e4937aSGao Xiang  */
76347e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
76447e4937aSGao Xiang {
76587ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
76687ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
76747e4937aSGao Xiang }
76847e4937aSGao Xiang 
76947e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
77047e4937aSGao Xiang {
77147e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
77247e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
77347e4937aSGao Xiang 
77487ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
77547e4937aSGao Xiang }
77647e4937aSGao Xiang 
7775c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
77847e4937aSGao Xiang {
77987ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
78047e4937aSGao Xiang 
78187ca34a7SGao Xiang 	if (!pcl)
78247e4937aSGao Xiang 		return false;
78347e4937aSGao Xiang 
78406a304cdSGao Xiang 	z_erofs_bvec_iter_end(&fe->biter);
78587ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
78647e4937aSGao Xiang 
78706a304cdSGao Xiang 	if (fe->candidate_bvpage) {
78806a304cdSGao Xiang 		DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
78906a304cdSGao Xiang 		fe->candidate_bvpage = NULL;
79006a304cdSGao Xiang 	}
79106a304cdSGao Xiang 
79247e4937aSGao Xiang 	/*
79347e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
79447e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
79547e4937aSGao Xiang 	 */
796db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
79787ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
79847e4937aSGao Xiang 
79987ca34a7SGao Xiang 	fe->pcl = NULL;
80047e4937aSGao Xiang 	return true;
80147e4937aSGao Xiang }
80247e4937aSGao Xiang 
803b15b2e30SYue Hu static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
804b15b2e30SYue Hu 				 struct page *page, unsigned int pageofs,
805b15b2e30SYue Hu 				 unsigned int len)
806b15b2e30SYue Hu {
807b15b2e30SYue Hu 	struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
808b15b2e30SYue Hu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
809b15b2e30SYue Hu 	u8 *src, *dst;
810b15b2e30SYue Hu 	unsigned int i, cnt;
811b15b2e30SYue Hu 
812e5126de1SYue Hu 	if (!packed_inode)
813e5126de1SYue Hu 		return -EFSCORRUPTED;
814e5126de1SYue Hu 
815b15b2e30SYue Hu 	pos += EROFS_I(inode)->z_fragmentoff;
816b15b2e30SYue Hu 	for (i = 0; i < len; i += cnt) {
817b15b2e30SYue Hu 		cnt = min_t(unsigned int, len - i,
818b15b2e30SYue Hu 			    EROFS_BLKSIZ - erofs_blkoff(pos));
819b15b2e30SYue Hu 		src = erofs_bread(&buf, packed_inode,
820b15b2e30SYue Hu 				  erofs_blknr(pos), EROFS_KMAP);
821b15b2e30SYue Hu 		if (IS_ERR(src)) {
822b15b2e30SYue Hu 			erofs_put_metabuf(&buf);
823b15b2e30SYue Hu 			return PTR_ERR(src);
824b15b2e30SYue Hu 		}
825b15b2e30SYue Hu 
826b15b2e30SYue Hu 		dst = kmap_local_page(page);
827b15b2e30SYue Hu 		memcpy(dst + pageofs + i, src + erofs_blkoff(pos), cnt);
828b15b2e30SYue Hu 		kunmap_local(dst);
829b15b2e30SYue Hu 		pos += cnt;
830b15b2e30SYue Hu 	}
831b15b2e30SYue Hu 	erofs_put_metabuf(&buf);
832b15b2e30SYue Hu 	return 0;
833b15b2e30SYue Hu }
834b15b2e30SYue Hu 
83547e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
836eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
83747e4937aSGao Xiang {
83847e4937aSGao Xiang 	struct inode *const inode = fe->inode;
83947e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
84047e4937aSGao Xiang 	const loff_t offset = page_offset(page);
8415b220b20SGao Xiang 	bool tight = true, exclusive;
8422bfab9c0SGao Xiang 	unsigned int cur, end, spiltted;
84347e4937aSGao Xiang 	int err = 0;
84447e4937aSGao Xiang 
84547e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
84647e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
84747e4937aSGao Xiang 
84847e4937aSGao Xiang 	spiltted = 0;
84947e4937aSGao Xiang 	end = PAGE_SIZE;
85047e4937aSGao Xiang repeat:
85147e4937aSGao Xiang 	cur = end - 1;
85247e4937aSGao Xiang 
85339397a46SGao Xiang 	if (offset + cur < map->m_la ||
85439397a46SGao Xiang 	    offset + cur >= map->m_la + map->m_llen) {
85539397a46SGao Xiang 		erofs_dbg("out-of-range map @ pos %llu", offset + cur);
85647e4937aSGao Xiang 
8575c6dcc57SGao Xiang 		if (z_erofs_collector_end(fe))
85847e4937aSGao Xiang 			fe->backmost = false;
85947e4937aSGao Xiang 		map->m_la = offset + cur;
86047e4937aSGao Xiang 		map->m_llen = 0;
86147e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
8628d8a09b0SGao Xiang 		if (err)
86367148551SGao Xiang 			goto out;
86439397a46SGao Xiang 	} else {
86539397a46SGao Xiang 		if (fe->pcl)
86639397a46SGao Xiang 			goto hitted;
86739397a46SGao Xiang 		/* didn't get a valid pcluster previously (very rare) */
86839397a46SGao Xiang 	}
86947e4937aSGao Xiang 
870b15b2e30SYue Hu 	if (!(map->m_flags & EROFS_MAP_MAPPED) ||
871b15b2e30SYue Hu 	    map->m_flags & EROFS_MAP_FRAGMENT)
87247e4937aSGao Xiang 		goto hitted;
87347e4937aSGao Xiang 
87483a386c0SGao Xiang 	err = z_erofs_collector_begin(fe);
8758d8a09b0SGao Xiang 	if (err)
87667148551SGao Xiang 		goto out;
87747e4937aSGao Xiang 
8785c6dcc57SGao Xiang 	if (z_erofs_is_inline_pcluster(fe->pcl)) {
87909c54379SGao Xiang 		void *mp;
880cecf864dSYue Hu 
88109c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
88209c54379SGao Xiang 					erofs_blknr(map->m_pa), EROFS_NO_KMAP);
88309c54379SGao Xiang 		if (IS_ERR(mp)) {
88409c54379SGao Xiang 			err = PTR_ERR(mp);
885cecf864dSYue Hu 			erofs_err(inode->i_sb,
886cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
88767148551SGao Xiang 			goto out;
888cecf864dSYue Hu 		}
88909c54379SGao Xiang 		get_page(fe->map.buf.page);
890ed722fbcSGao Xiang 		WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
891ed722fbcSGao Xiang 			   fe->map.buf.page);
892db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
893cecf864dSYue Hu 	} else {
8946f39d1e1SGao Xiang 		/* bind cache first when cached decompression is preferred */
8951282dea3SGao Xiang 		z_erofs_bind_cache(fe, pagepool);
896cecf864dSYue Hu 	}
89747e4937aSGao Xiang hitted:
898dc76ea8cSGao Xiang 	/*
899dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
900dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
901dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
902387bab87SGao Xiang 	 * for inplace I/O or bvpage (should be processed in a strict order.)
903dc76ea8cSGao Xiang 	 */
904db166fc2SGao Xiang 	tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
905db166fc2SGao Xiang 		  fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
906dc76ea8cSGao Xiang 
90747e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
9088d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
90947e4937aSGao Xiang 		zero_user_segment(page, cur, end);
91047e4937aSGao Xiang 		goto next_part;
91147e4937aSGao Xiang 	}
912b15b2e30SYue Hu 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
913b15b2e30SYue Hu 		unsigned int pageofs, skip, len;
914b15b2e30SYue Hu 
915b15b2e30SYue Hu 		if (offset > map->m_la) {
916b15b2e30SYue Hu 			pageofs = 0;
917b15b2e30SYue Hu 			skip = offset - map->m_la;
918b15b2e30SYue Hu 		} else {
919b15b2e30SYue Hu 			pageofs = map->m_la & ~PAGE_MASK;
920b15b2e30SYue Hu 			skip = 0;
921b15b2e30SYue Hu 		}
922b15b2e30SYue Hu 		len = min_t(unsigned int, map->m_llen - skip, end - cur);
923b15b2e30SYue Hu 		err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
924b15b2e30SYue Hu 		if (err)
925b15b2e30SYue Hu 			goto out;
926b15b2e30SYue Hu 		++spiltted;
927b15b2e30SYue Hu 		tight = false;
928b15b2e30SYue Hu 		goto next_part;
929b15b2e30SYue Hu 	}
93047e4937aSGao Xiang 
9315b220b20SGao Xiang 	exclusive = (!cur && (!spiltted || tight));
93247e4937aSGao Xiang 	if (cur)
933db166fc2SGao Xiang 		tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
93447e4937aSGao Xiang 
93547e4937aSGao Xiang retry:
93606a304cdSGao Xiang 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
93706a304cdSGao Xiang 					.page = page,
93806a304cdSGao Xiang 					.offset = offset - map->m_la,
93906a304cdSGao Xiang 					.end = end,
9405b220b20SGao Xiang 				  }), exclusive);
94106a304cdSGao Xiang 	/* should allocate an additional short-lived page for bvset */
94206a304cdSGao Xiang 	if (err == -EAGAIN && !fe->candidate_bvpage) {
94306a304cdSGao Xiang 		fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
94406a304cdSGao Xiang 		set_page_private(fe->candidate_bvpage,
94506a304cdSGao Xiang 				 Z_EROFS_SHORTLIVED_PAGE);
94647e4937aSGao Xiang 		goto retry;
94747e4937aSGao Xiang 	}
94847e4937aSGao Xiang 
94906a304cdSGao Xiang 	if (err) {
95006a304cdSGao Xiang 		DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
95167148551SGao Xiang 		goto out;
95206a304cdSGao Xiang 	}
95347e4937aSGao Xiang 
95467148551SGao Xiang 	z_erofs_onlinepage_split(page);
95547e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
95647e4937aSGao Xiang 	++spiltted;
957267f2492SGao Xiang 	if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
958267f2492SGao Xiang 		fe->pcl->multibases = true;
9592bfab9c0SGao Xiang 	if (fe->pcl->length < offset + end - map->m_la) {
9602bfab9c0SGao Xiang 		fe->pcl->length = offset + end - map->m_la;
9612bfab9c0SGao Xiang 		fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
9622bfab9c0SGao Xiang 	}
963e7933278SGao Xiang 	if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
964e7933278SGao Xiang 	    !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
965e7933278SGao Xiang 	    fe->pcl->length == map->m_llen)
966e7933278SGao Xiang 		fe->pcl->partial = false;
96747e4937aSGao Xiang next_part:
9682bfab9c0SGao Xiang 	/* shorten the remaining extent to update progress */
96947e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
9702bfab9c0SGao Xiang 	map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
97147e4937aSGao Xiang 
97247e4937aSGao Xiang 	end = cur;
97347e4937aSGao Xiang 	if (end > 0)
97447e4937aSGao Xiang 		goto repeat;
97547e4937aSGao Xiang 
97647e4937aSGao Xiang out:
97767148551SGao Xiang 	if (err)
97867148551SGao Xiang 		z_erofs_page_mark_eio(page);
97947e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
98047e4937aSGao Xiang 
9814f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
98247e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
98347e4937aSGao Xiang 	return err;
98447e4937aSGao Xiang }
98547e4937aSGao Xiang 
98640452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
98740452ffcSHuang Jianan 				       unsigned int readahead_pages)
98840452ffcSHuang Jianan {
989a2e20a25SMatthew Wilcox (Oracle) 	/* auto: enable for read_folio, disable for readahead */
99040452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
99140452ffcSHuang Jianan 	    !readahead_pages)
99240452ffcSHuang Jianan 		return true;
99340452ffcSHuang Jianan 
99440452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
99540452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
99640452ffcSHuang Jianan 		return true;
99740452ffcSHuang Jianan 
99840452ffcSHuang Jianan 	return false;
99940452ffcSHuang Jianan }
100040452ffcSHuang Jianan 
10016aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
10026aaa7b06SGao Xiang {
10036aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
10046aaa7b06SGao Xiang }
10056aaa7b06SGao Xiang 
10064f05687fSGao Xiang struct z_erofs_decompress_backend {
10074f05687fSGao Xiang 	struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
10084f05687fSGao Xiang 	struct super_block *sb;
10094f05687fSGao Xiang 	struct z_erofs_pcluster *pcl;
10104f05687fSGao Xiang 
10114f05687fSGao Xiang 	/* pages with the longest decompressed length for deduplication */
10124f05687fSGao Xiang 	struct page **decompressed_pages;
10134f05687fSGao Xiang 	/* pages to keep the compressed data */
10144f05687fSGao Xiang 	struct page **compressed_pages;
10154f05687fSGao Xiang 
1016267f2492SGao Xiang 	struct list_head decompressed_secondary_bvecs;
10174f05687fSGao Xiang 	struct page **pagepool;
10182bfab9c0SGao Xiang 	unsigned int onstack_used, nr_pages;
10194f05687fSGao Xiang };
10204f05687fSGao Xiang 
1021267f2492SGao Xiang struct z_erofs_bvec_item {
1022267f2492SGao Xiang 	struct z_erofs_bvec bvec;
1023267f2492SGao Xiang 	struct list_head list;
1024267f2492SGao Xiang };
1025267f2492SGao Xiang 
1026267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
10273fe96ee0SGao Xiang 					 struct z_erofs_bvec *bvec)
10283fe96ee0SGao Xiang {
1029267f2492SGao Xiang 	struct z_erofs_bvec_item *item;
1030267f2492SGao Xiang 
1031267f2492SGao Xiang 	if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1032267f2492SGao Xiang 		unsigned int pgnr;
10333fe96ee0SGao Xiang 
1034267f2492SGao Xiang 		pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
10352bfab9c0SGao Xiang 		DBG_BUGON(pgnr >= be->nr_pages);
103663bbb856SGao Xiang 		if (!be->decompressed_pages[pgnr]) {
10373fe96ee0SGao Xiang 			be->decompressed_pages[pgnr] = bvec->page;
1038267f2492SGao Xiang 			return;
10393fe96ee0SGao Xiang 		}
104063bbb856SGao Xiang 	}
10413fe96ee0SGao Xiang 
1042267f2492SGao Xiang 	/* (cold path) one pcluster is requested multiple times */
1043267f2492SGao Xiang 	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1044267f2492SGao Xiang 	item->bvec = *bvec;
1045267f2492SGao Xiang 	list_add(&item->list, &be->decompressed_secondary_bvecs);
1046267f2492SGao Xiang }
1047267f2492SGao Xiang 
1048267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1049267f2492SGao Xiang 				      int err)
1050267f2492SGao Xiang {
1051267f2492SGao Xiang 	unsigned int off0 = be->pcl->pageofs_out;
1052267f2492SGao Xiang 	struct list_head *p, *n;
1053267f2492SGao Xiang 
1054267f2492SGao Xiang 	list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1055267f2492SGao Xiang 		struct z_erofs_bvec_item *bvi;
1056267f2492SGao Xiang 		unsigned int end, cur;
1057267f2492SGao Xiang 		void *dst, *src;
1058267f2492SGao Xiang 
1059267f2492SGao Xiang 		bvi = container_of(p, struct z_erofs_bvec_item, list);
1060267f2492SGao Xiang 		cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1061267f2492SGao Xiang 		end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1062267f2492SGao Xiang 			    bvi->bvec.end);
1063267f2492SGao Xiang 		dst = kmap_local_page(bvi->bvec.page);
1064267f2492SGao Xiang 		while (cur < end) {
1065267f2492SGao Xiang 			unsigned int pgnr, scur, len;
1066267f2492SGao Xiang 
1067267f2492SGao Xiang 			pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1068267f2492SGao Xiang 			DBG_BUGON(pgnr >= be->nr_pages);
1069267f2492SGao Xiang 
1070267f2492SGao Xiang 			scur = bvi->bvec.offset + cur -
1071267f2492SGao Xiang 					((pgnr << PAGE_SHIFT) - off0);
1072267f2492SGao Xiang 			len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1073267f2492SGao Xiang 			if (!be->decompressed_pages[pgnr]) {
1074267f2492SGao Xiang 				err = -EFSCORRUPTED;
1075267f2492SGao Xiang 				cur += len;
1076267f2492SGao Xiang 				continue;
1077267f2492SGao Xiang 			}
1078267f2492SGao Xiang 			src = kmap_local_page(be->decompressed_pages[pgnr]);
1079267f2492SGao Xiang 			memcpy(dst + cur, src + scur, len);
1080267f2492SGao Xiang 			kunmap_local(src);
1081267f2492SGao Xiang 			cur += len;
1082267f2492SGao Xiang 		}
1083267f2492SGao Xiang 		kunmap_local(dst);
1084267f2492SGao Xiang 		if (err)
1085267f2492SGao Xiang 			z_erofs_page_mark_eio(bvi->bvec.page);
1086267f2492SGao Xiang 		z_erofs_onlinepage_endio(bvi->bvec.page);
1087267f2492SGao Xiang 		list_del(p);
1088267f2492SGao Xiang 		kfree(bvi);
1089267f2492SGao Xiang 	}
1090267f2492SGao Xiang }
1091267f2492SGao Xiang 
1092267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
109342fec235SGao Xiang {
10944f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
109506a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
109606a304cdSGao Xiang 	struct page *old_bvpage;
1097267f2492SGao Xiang 	int i;
109842fec235SGao Xiang 
1099387bab87SGao Xiang 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
110042fec235SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
110106a304cdSGao Xiang 		struct z_erofs_bvec bvec;
110242fec235SGao Xiang 
110306a304cdSGao Xiang 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
110442fec235SGao Xiang 
110506a304cdSGao Xiang 		if (old_bvpage)
11064f05687fSGao Xiang 			z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
110742fec235SGao Xiang 
110806a304cdSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1109267f2492SGao Xiang 		z_erofs_do_decompressed_bvec(be, &bvec);
111042fec235SGao Xiang 	}
111106a304cdSGao Xiang 
111206a304cdSGao Xiang 	old_bvpage = z_erofs_bvec_iter_end(&biter);
111306a304cdSGao Xiang 	if (old_bvpage)
11144f05687fSGao Xiang 		z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
111542fec235SGao Xiang }
111642fec235SGao Xiang 
11174f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
11184f05687fSGao Xiang 				  bool *overlapped)
111967139e36SGao Xiang {
11204f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
112167139e36SGao Xiang 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
112267139e36SGao Xiang 	int i, err = 0;
112367139e36SGao Xiang 
112467139e36SGao Xiang 	*overlapped = false;
112567139e36SGao Xiang 	for (i = 0; i < pclusterpages; ++i) {
1126ed722fbcSGao Xiang 		struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1127ed722fbcSGao Xiang 		struct page *page = bvec->page;
112867139e36SGao Xiang 
112967139e36SGao Xiang 		/* compressed pages ought to be present before decompressing */
113067139e36SGao Xiang 		if (!page) {
113167139e36SGao Xiang 			DBG_BUGON(1);
113267139e36SGao Xiang 			continue;
113367139e36SGao Xiang 		}
1134fe3e5914SGao Xiang 		be->compressed_pages[i] = page;
113567139e36SGao Xiang 
113667139e36SGao Xiang 		if (z_erofs_is_inline_pcluster(pcl)) {
113767139e36SGao Xiang 			if (!PageUptodate(page))
113867139e36SGao Xiang 				err = -EIO;
113967139e36SGao Xiang 			continue;
114067139e36SGao Xiang 		}
114167139e36SGao Xiang 
114267139e36SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
114367139e36SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
11444f05687fSGao Xiang 			if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
114567139e36SGao Xiang 				if (!PageUptodate(page))
114667139e36SGao Xiang 					err = -EIO;
114767139e36SGao Xiang 				continue;
114867139e36SGao Xiang 			}
1149267f2492SGao Xiang 			z_erofs_do_decompressed_bvec(be, bvec);
115067139e36SGao Xiang 			*overlapped = true;
115167139e36SGao Xiang 		}
115267139e36SGao Xiang 	}
115367139e36SGao Xiang 
1154fe3e5914SGao Xiang 	if (err)
11554f05687fSGao Xiang 		return err;
11564f05687fSGao Xiang 	return 0;
115767139e36SGao Xiang }
115867139e36SGao Xiang 
11594f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
11604f05687fSGao Xiang 				       int err)
116147e4937aSGao Xiang {
11624f05687fSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
11634f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
1164cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
11652bfab9c0SGao Xiang 	unsigned int i, inputsize;
116667148551SGao Xiang 	int err2;
11672bfab9c0SGao Xiang 	struct page *page;
11682bfab9c0SGao Xiang 	bool overlapped;
116947e4937aSGao Xiang 
117087ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
11712bfab9c0SGao Xiang 	be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
117247e4937aSGao Xiang 
1173fe3e5914SGao Xiang 	/* allocate (de)compressed page arrays if cannot be kept on stack */
1174fe3e5914SGao Xiang 	be->decompressed_pages = NULL;
1175fe3e5914SGao Xiang 	be->compressed_pages = NULL;
1176fe3e5914SGao Xiang 	be->onstack_used = 0;
11772bfab9c0SGao Xiang 	if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
11784f05687fSGao Xiang 		be->decompressed_pages = be->onstack_pages;
11792bfab9c0SGao Xiang 		be->onstack_used = be->nr_pages;
11804f05687fSGao Xiang 		memset(be->decompressed_pages, 0,
11812bfab9c0SGao Xiang 		       sizeof(struct page *) * be->nr_pages);
1182fe3e5914SGao Xiang 	}
1183fe3e5914SGao Xiang 
1184fe3e5914SGao Xiang 	if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1185fe3e5914SGao Xiang 		be->compressed_pages = be->onstack_pages + be->onstack_used;
1186fe3e5914SGao Xiang 
1187fe3e5914SGao Xiang 	if (!be->decompressed_pages)
11884f05687fSGao Xiang 		be->decompressed_pages =
118912724ba3SGao Xiang 			kcalloc(be->nr_pages, sizeof(struct page *),
1190e7368187SGao Xiang 				GFP_KERNEL | __GFP_NOFAIL);
1191fe3e5914SGao Xiang 	if (!be->compressed_pages)
1192fe3e5914SGao Xiang 		be->compressed_pages =
119312724ba3SGao Xiang 			kcalloc(pclusterpages, sizeof(struct page *),
1194fe3e5914SGao Xiang 				GFP_KERNEL | __GFP_NOFAIL);
119547e4937aSGao Xiang 
1196267f2492SGao Xiang 	z_erofs_parse_out_bvecs(be);
11974f05687fSGao Xiang 	err2 = z_erofs_parse_in_bvecs(be, &overlapped);
11984f05687fSGao Xiang 	if (err2)
11994f05687fSGao Xiang 		err = err2;
12008d8a09b0SGao Xiang 	if (err)
120147e4937aSGao Xiang 		goto out;
120247e4937aSGao Xiang 
1203cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1204cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1205cecf864dSYue Hu 	else
1206cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1207cecf864dSYue Hu 
120847e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
12094f05687fSGao Xiang 					.sb = be->sb,
12104f05687fSGao Xiang 					.in = be->compressed_pages,
12114f05687fSGao Xiang 					.out = be->decompressed_pages,
1212cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
121387ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
12149f6cc76eSGao Xiang 					.inputsize = inputsize,
12152bfab9c0SGao Xiang 					.outputsize = pcl->length,
121647e4937aSGao Xiang 					.alg = pcl->algorithmformat,
121747e4937aSGao Xiang 					.inplace_io = overlapped,
12182bfab9c0SGao Xiang 					.partial_decoding = pcl->partial,
1219267f2492SGao Xiang 					.fillgaps = pcl->multibases,
12204f05687fSGao Xiang 				 }, be->pagepool);
122147e4937aSGao Xiang 
122247e4937aSGao Xiang out:
1223cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1224cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1225ed722fbcSGao Xiang 		page = pcl->compressed_bvecs[0].page;
1226ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1227cecf864dSYue Hu 		put_page(page);
1228cecf864dSYue Hu 	} else {
1229cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
1230ed722fbcSGao Xiang 			page = pcl->compressed_bvecs[i].page;
123147e4937aSGao Xiang 
123247e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
123347e4937aSGao Xiang 				continue;
123447e4937aSGao Xiang 
12356aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
12364f05687fSGao Xiang 			(void)z_erofs_put_shortlivedpage(be->pagepool, page);
1237ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
123847e4937aSGao Xiang 		}
1239cecf864dSYue Hu 	}
1240fe3e5914SGao Xiang 	if (be->compressed_pages < be->onstack_pages ||
1241fe3e5914SGao Xiang 	    be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
124212724ba3SGao Xiang 		kfree(be->compressed_pages);
1243267f2492SGao Xiang 	z_erofs_fill_other_copies(be, err);
124447e4937aSGao Xiang 
12452bfab9c0SGao Xiang 	for (i = 0; i < be->nr_pages; ++i) {
12464f05687fSGao Xiang 		page = be->decompressed_pages[i];
124747e4937aSGao Xiang 		if (!page)
124847e4937aSGao Xiang 			continue;
124947e4937aSGao Xiang 
12506aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
125147e4937aSGao Xiang 
12526aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
12534f05687fSGao Xiang 		if (z_erofs_put_shortlivedpage(be->pagepool, page))
125447e4937aSGao Xiang 			continue;
125567148551SGao Xiang 		if (err)
125667148551SGao Xiang 			z_erofs_page_mark_eio(page);
125747e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
125847e4937aSGao Xiang 	}
125947e4937aSGao Xiang 
12604f05687fSGao Xiang 	if (be->decompressed_pages != be->onstack_pages)
126112724ba3SGao Xiang 		kfree(be->decompressed_pages);
126247e4937aSGao Xiang 
12632bfab9c0SGao Xiang 	pcl->length = 0;
12642bfab9c0SGao Xiang 	pcl->partial = true;
1265267f2492SGao Xiang 	pcl->multibases = false;
126606a304cdSGao Xiang 	pcl->bvset.nextpage = NULL;
126787ca34a7SGao Xiang 	pcl->vcnt = 0;
126847e4937aSGao Xiang 
126987ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
127047e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
127187ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
127247e4937aSGao Xiang 	return err;
127347e4937aSGao Xiang }
127447e4937aSGao Xiang 
12750c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1276eaa9172aSGao Xiang 				     struct page **pagepool)
127747e4937aSGao Xiang {
12784f05687fSGao Xiang 	struct z_erofs_decompress_backend be = {
12794f05687fSGao Xiang 		.sb = io->sb,
12804f05687fSGao Xiang 		.pagepool = pagepool,
1281267f2492SGao Xiang 		.decompressed_secondary_bvecs =
1282267f2492SGao Xiang 			LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
12834f05687fSGao Xiang 	};
128447e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
128547e4937aSGao Xiang 
128647e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
12874f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
128847e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
12894f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */
129047e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
129147e4937aSGao Xiang 
12924f05687fSGao Xiang 		be.pcl = container_of(owned, struct z_erofs_pcluster, next);
12934f05687fSGao Xiang 		owned = READ_ONCE(be.pcl->next);
129447e4937aSGao Xiang 
12954f05687fSGao Xiang 		z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
12964f05687fSGao Xiang 		erofs_workgroup_put(&be.pcl->obj);
129747e4937aSGao Xiang 	}
129847e4937aSGao Xiang }
129947e4937aSGao Xiang 
13000c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
130147e4937aSGao Xiang {
1302a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1303a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1304eaa9172aSGao Xiang 	struct page *pagepool = NULL;
130547e4937aSGao Xiang 
1306a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
13070c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
130847e4937aSGao Xiang 
1309eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1310a4b1fab1SGao Xiang 	kvfree(bgq);
131147e4937aSGao Xiang }
131247e4937aSGao Xiang 
13137865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1314cdba5506SGao Xiang 				       int bios)
13157865827cSGao Xiang {
13167865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
13177865827cSGao Xiang 
13187865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
1319cdba5506SGao Xiang 	if (io->sync) {
13207865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
132160b30050SHongyu Jin 			complete(&io->u.done);
13227865827cSGao Xiang 		return;
13237865827cSGao Xiang 	}
13247865827cSGao Xiang 
13257865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
13267865827cSGao Xiang 		return;
13277865827cSGao Xiang 	/* Use workqueue and sync decompression for atomic contexts only */
13287865827cSGao Xiang 	if (in_atomic() || irqs_disabled()) {
13297865827cSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
13307865827cSGao Xiang 		/* enable sync decompression for readahead */
13317865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
13327865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
13337865827cSGao Xiang 		return;
13347865827cSGao Xiang 	}
13357865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
13367865827cSGao Xiang }
13377865827cSGao Xiang 
133847e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
133947e4937aSGao Xiang 					       unsigned int nr,
1340eaa9172aSGao Xiang 					       struct page **pagepool,
13419f2731d6SGao Xiang 					       struct address_space *mc)
134247e4937aSGao Xiang {
134347e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
13449f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
134547e4937aSGao Xiang 	bool tocache = false;
134647e4937aSGao Xiang 
134747e4937aSGao Xiang 	struct address_space *mapping;
134847e4937aSGao Xiang 	struct page *oldpage, *page;
134947e4937aSGao Xiang 	int justfound;
135047e4937aSGao Xiang 
135147e4937aSGao Xiang repeat:
1352ed722fbcSGao Xiang 	page = READ_ONCE(pcl->compressed_bvecs[nr].page);
135347e4937aSGao Xiang 	oldpage = page;
135447e4937aSGao Xiang 
135547e4937aSGao Xiang 	if (!page)
135647e4937aSGao Xiang 		goto out_allocpage;
135747e4937aSGao Xiang 
1358b1ed220cSGao Xiang 	justfound = (unsigned long)page & 1UL;
1359b1ed220cSGao Xiang 	page = (struct page *)((unsigned long)page & ~1UL);
136047e4937aSGao Xiang 
13611825c8d7SGao Xiang 	/*
13621825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
13631825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
13641825c8d7SGao Xiang 	 */
13651825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1366ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
13671825c8d7SGao Xiang 		set_page_private(page, 0);
13681825c8d7SGao Xiang 		tocache = true;
13691825c8d7SGao Xiang 		goto out_tocache;
13701825c8d7SGao Xiang 	}
137147e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
137247e4937aSGao Xiang 
137347e4937aSGao Xiang 	/*
13746aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
137547e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
137647e4937aSGao Xiang 	 */
137747e4937aSGao Xiang 	if (mapping && mapping != mc)
137847e4937aSGao Xiang 		/* ought to be unmanaged pages */
137947e4937aSGao Xiang 		goto out;
138047e4937aSGao Xiang 
13816aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
13826aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
13836aaa7b06SGao Xiang 		goto out;
13846aaa7b06SGao Xiang 
138547e4937aSGao Xiang 	lock_page(page);
138647e4937aSGao Xiang 
138747e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
138847e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
138947e4937aSGao Xiang 
139047e4937aSGao Xiang 	/* the page is still in manage cache */
139147e4937aSGao Xiang 	if (page->mapping == mc) {
1392ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
139347e4937aSGao Xiang 
139447e4937aSGao Xiang 		if (!PagePrivate(page)) {
139547e4937aSGao Xiang 			/*
139647e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
139747e4937aSGao Xiang 			 * the current restriction as well if
1398ed722fbcSGao Xiang 			 * the page is already in compressed_bvecs[].
139947e4937aSGao Xiang 			 */
140047e4937aSGao Xiang 			DBG_BUGON(!justfound);
140147e4937aSGao Xiang 
140247e4937aSGao Xiang 			justfound = 0;
140347e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
140447e4937aSGao Xiang 			SetPagePrivate(page);
140547e4937aSGao Xiang 		}
140647e4937aSGao Xiang 
140747e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
140847e4937aSGao Xiang 		if (PageUptodate(page)) {
140947e4937aSGao Xiang 			unlock_page(page);
141047e4937aSGao Xiang 			page = NULL;
141147e4937aSGao Xiang 		}
141247e4937aSGao Xiang 		goto out;
141347e4937aSGao Xiang 	}
141447e4937aSGao Xiang 
141547e4937aSGao Xiang 	/*
141647e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
141747e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
141847e4937aSGao Xiang 	 */
141947e4937aSGao Xiang 	DBG_BUGON(page->mapping);
142047e4937aSGao Xiang 	DBG_BUGON(!justfound);
142147e4937aSGao Xiang 
142247e4937aSGao Xiang 	tocache = true;
142347e4937aSGao Xiang 	unlock_page(page);
142447e4937aSGao Xiang 	put_page(page);
142547e4937aSGao Xiang out_allocpage:
14265ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1427ed722fbcSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1428ed722fbcSGao Xiang 			       oldpage, page)) {
1429eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
14305ddcee1fSGao Xiang 		cond_resched();
14315ddcee1fSGao Xiang 		goto repeat;
14325ddcee1fSGao Xiang 	}
14331825c8d7SGao Xiang out_tocache:
1434bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1435bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1436bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1437bf225074SGao Xiang 		goto out;
1438a30573b3SGao Xiang 	}
1439bf225074SGao Xiang 	attach_page_private(page, pcl);
1440bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1441bf225074SGao Xiang 	put_page(page);
1442bf225074SGao Xiang 
144347e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
144447e4937aSGao Xiang 	return page;
144547e4937aSGao Xiang }
144647e4937aSGao Xiang 
1447cdba5506SGao Xiang static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1448a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
144947e4937aSGao Xiang {
1450a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
145147e4937aSGao Xiang 
1452a4b1fab1SGao Xiang 	if (fg && !*fg) {
1453a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1454a4b1fab1SGao Xiang 		if (!q) {
1455a4b1fab1SGao Xiang 			*fg = true;
1456a4b1fab1SGao Xiang 			goto fg_out;
145747e4937aSGao Xiang 		}
14580c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1459a4b1fab1SGao Xiang 	} else {
1460a4b1fab1SGao Xiang fg_out:
1461a4b1fab1SGao Xiang 		q = fgq;
146260b30050SHongyu Jin 		init_completion(&fgq->u.done);
1463a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
146467148551SGao Xiang 		q->eio = false;
1465cdba5506SGao Xiang 		q->sync = true;
1466a4b1fab1SGao Xiang 	}
1467a4b1fab1SGao Xiang 	q->sb = sb;
1468a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1469a4b1fab1SGao Xiang 	return q;
147047e4937aSGao Xiang }
147147e4937aSGao Xiang 
147247e4937aSGao Xiang /* define decompression jobqueue types */
147347e4937aSGao Xiang enum {
147447e4937aSGao Xiang 	JQ_BYPASS,
147547e4937aSGao Xiang 	JQ_SUBMIT,
147647e4937aSGao Xiang 	NR_JOBQUEUES,
147747e4937aSGao Xiang };
147847e4937aSGao Xiang 
147947e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
148047e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
148147e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
148247e4937aSGao Xiang {
148347e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
148447e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
148547e4937aSGao Xiang 
148647e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
148747e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
148847e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
148947e4937aSGao Xiang 
149047e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
149147e4937aSGao Xiang 
149247e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
149347e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
149447e4937aSGao Xiang 
149547e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
149647e4937aSGao Xiang }
149747e4937aSGao Xiang 
14987865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
14997865827cSGao Xiang {
1500cdba5506SGao Xiang 	struct z_erofs_decompressqueue *q = bio->bi_private;
15017865827cSGao Xiang 	blk_status_t err = bio->bi_status;
15027865827cSGao Xiang 	struct bio_vec *bvec;
15037865827cSGao Xiang 	struct bvec_iter_all iter_all;
15047865827cSGao Xiang 
15057865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
15067865827cSGao Xiang 		struct page *page = bvec->bv_page;
15077865827cSGao Xiang 
15087865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
15097865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
15107865827cSGao Xiang 
15117865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
15127865827cSGao Xiang 			if (!err)
15137865827cSGao Xiang 				SetPageUptodate(page);
15147865827cSGao Xiang 			unlock_page(page);
15157865827cSGao Xiang 		}
15167865827cSGao Xiang 	}
151767148551SGao Xiang 	if (err)
151867148551SGao Xiang 		q->eio = true;
1519cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q, -1);
15207865827cSGao Xiang 	bio_put(bio);
15217865827cSGao Xiang }
15227865827cSGao Xiang 
152383a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1524eaa9172aSGao Xiang 				 struct page **pagepool,
1525a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1526a4b1fab1SGao Xiang 				 bool *force_fg)
152747e4937aSGao Xiang {
152883a386c0SGao Xiang 	struct super_block *sb = f->inode->i_sb;
152983a386c0SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
153047e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1531a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
15325c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1533dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
15343f649ab7SKees Cook 	pgoff_t last_index;
1535dfeab2e9SGao Xiang 	struct block_device *last_bdev;
15361e4a2955SGao Xiang 	unsigned int nr_bios = 0;
15371e4a2955SGao Xiang 	struct bio *bio = NULL;
153882e60d00SJohannes Weiner 	unsigned long pflags;
153982e60d00SJohannes Weiner 	int memstall = 0;
154047e4937aSGao Xiang 
1541cdba5506SGao Xiang 	/*
1542cdba5506SGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
1543cdba5506SGao Xiang 	 * no need to read from device for all pclusters in this queue.
1544cdba5506SGao Xiang 	 */
1545cdba5506SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1546cdba5506SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1547cdba5506SGao Xiang 
1548a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1549a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
155047e4937aSGao Xiang 
155147e4937aSGao Xiang 	/* by default, all need io submission */
155247e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
155347e4937aSGao Xiang 
155447e4937aSGao Xiang 	do {
1555dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
155647e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
15571e4a2955SGao Xiang 		pgoff_t cur, end;
15581e4a2955SGao Xiang 		unsigned int i = 0;
15591e4a2955SGao Xiang 		bool bypass = true;
156047e4937aSGao Xiang 
156147e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
156247e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
156347e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
156447e4937aSGao Xiang 
156547e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
156647e4937aSGao Xiang 
1567cecf864dSYue Hu 		/* close the main owned chain at first */
1568cecf864dSYue Hu 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1569cecf864dSYue Hu 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1570cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1571cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1572cecf864dSYue Hu 			continue;
1573cecf864dSYue Hu 		}
1574cecf864dSYue Hu 
1575dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1576dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
1577dfeab2e9SGao Xiang 			.m_pa = blknr_to_addr(pcl->obj.index),
1578dfeab2e9SGao Xiang 		};
1579dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1580dfeab2e9SGao Xiang 
1581dfeab2e9SGao Xiang 		cur = erofs_blknr(mdev.m_pa);
15829f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
158347e4937aSGao Xiang 
15841e4a2955SGao Xiang 		do {
15851e4a2955SGao Xiang 			struct page *page;
158647e4937aSGao Xiang 
15871e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
158883a386c0SGao Xiang 							  mc);
15891e4a2955SGao Xiang 			if (!page)
15901e4a2955SGao Xiang 				continue;
159147e4937aSGao Xiang 
1592dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1593dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
159447e4937aSGao Xiang submit_bio_retry:
159594e4e153SGao Xiang 				submit_bio(bio);
159682e60d00SJohannes Weiner 				if (memstall) {
159782e60d00SJohannes Weiner 					psi_memstall_leave(&pflags);
159882e60d00SJohannes Weiner 					memstall = 0;
159982e60d00SJohannes Weiner 				}
160047e4937aSGao Xiang 				bio = NULL;
160147e4937aSGao Xiang 			}
160247e4937aSGao Xiang 
160382e60d00SJohannes Weiner 			if (unlikely(PageWorkingset(page)) && !memstall) {
160499486c51SChristoph Hellwig 				psi_memstall_enter(&pflags);
160582e60d00SJohannes Weiner 				memstall = 1;
160682e60d00SJohannes Weiner 			}
160799486c51SChristoph Hellwig 
160847e4937aSGao Xiang 			if (!bio) {
160907888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
161007888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
16110c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1612dfeab2e9SGao Xiang 
1613dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
16141e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1615a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1616cdba5506SGao Xiang 				bio->bi_private = q[JQ_SUBMIT];
16176ea5aad3SGao Xiang 				if (f->readahead)
16186ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
161947e4937aSGao Xiang 				++nr_bios;
162047e4937aSGao Xiang 			}
162147e4937aSGao Xiang 
16226c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
162347e4937aSGao Xiang 				goto submit_bio_retry;
162447e4937aSGao Xiang 
16251e4a2955SGao Xiang 			last_index = cur;
16261e4a2955SGao Xiang 			bypass = false;
16271e4a2955SGao Xiang 		} while (++cur < end);
162847e4937aSGao Xiang 
16291e4a2955SGao Xiang 		if (!bypass)
163047e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
163147e4937aSGao Xiang 		else
163247e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
163347e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
163447e4937aSGao Xiang 
163599486c51SChristoph Hellwig 	if (bio) {
163694e4e153SGao Xiang 		submit_bio(bio);
163782e60d00SJohannes Weiner 		if (memstall)
163882e60d00SJohannes Weiner 			psi_memstall_leave(&pflags);
163999486c51SChristoph Hellwig 	}
164047e4937aSGao Xiang 
1641587a67b7SGao Xiang 	/*
1642587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1643587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1644587a67b7SGao Xiang 	 */
1645587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1646587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
16471e4a2955SGao Xiang 		return;
1648587a67b7SGao Xiang 	}
1649cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
165047e4937aSGao Xiang }
165147e4937aSGao Xiang 
165283a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1653eaa9172aSGao Xiang 			     struct page **pagepool, bool force_fg)
165447e4937aSGao Xiang {
1655a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
165647e4937aSGao Xiang 
16575c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
165847e4937aSGao Xiang 		return;
165983a386c0SGao Xiang 	z_erofs_submit_queue(f, pagepool, io, &force_fg);
166047e4937aSGao Xiang 
16610c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
16620c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
166347e4937aSGao Xiang 
166447e4937aSGao Xiang 	if (!force_fg)
166547e4937aSGao Xiang 		return;
166647e4937aSGao Xiang 
166747e4937aSGao Xiang 	/* wait until all bios are completed */
166860b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
166947e4937aSGao Xiang 
16700c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
16710c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
167247e4937aSGao Xiang }
167347e4937aSGao Xiang 
167438629291SGao Xiang /*
167538629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
167638629291SGao Xiang  * approximate readmore strategies as a start.
167738629291SGao Xiang  */
167838629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
167938629291SGao Xiang 				      struct readahead_control *rac,
168038629291SGao Xiang 				      erofs_off_t end,
1681eaa9172aSGao Xiang 				      struct page **pagepool,
168238629291SGao Xiang 				      bool backmost)
168338629291SGao Xiang {
168438629291SGao Xiang 	struct inode *inode = f->inode;
168538629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
168638629291SGao Xiang 	erofs_off_t cur;
168738629291SGao Xiang 	int err;
168838629291SGao Xiang 
168938629291SGao Xiang 	if (backmost) {
169038629291SGao Xiang 		map->m_la = end;
1691622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1692622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
169338629291SGao Xiang 		if (err)
169438629291SGao Xiang 			return;
169538629291SGao Xiang 
169638629291SGao Xiang 		/* expend ra for the trailing edge if readahead */
169738629291SGao Xiang 		if (rac) {
169838629291SGao Xiang 			loff_t newstart = readahead_pos(rac);
169938629291SGao Xiang 
170038629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
170138629291SGao Xiang 			readahead_expand(rac, newstart, cur - newstart);
170238629291SGao Xiang 			return;
170338629291SGao Xiang 		}
170438629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
170538629291SGao Xiang 	} else {
170638629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
170738629291SGao Xiang 
170838629291SGao Xiang 		if (!map->m_llen)
170938629291SGao Xiang 			return;
171038629291SGao Xiang 	}
171138629291SGao Xiang 
171238629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
171338629291SGao Xiang 	while (cur >= end) {
171438629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
171538629291SGao Xiang 		struct page *page;
171638629291SGao Xiang 
171738629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1718aa793b46SGao Xiang 		if (page) {
171938629291SGao Xiang 			if (PageUptodate(page)) {
172038629291SGao Xiang 				unlock_page(page);
1721aa793b46SGao Xiang 			} else {
172238629291SGao Xiang 				err = z_erofs_do_read_page(f, page, pagepool);
172338629291SGao Xiang 				if (err)
172438629291SGao Xiang 					erofs_err(inode->i_sb,
172538629291SGao Xiang 						  "readmore error at page %lu @ nid %llu",
172638629291SGao Xiang 						  index, EROFS_I(inode)->nid);
1727aa793b46SGao Xiang 			}
172838629291SGao Xiang 			put_page(page);
1729aa793b46SGao Xiang 		}
1730aa793b46SGao Xiang 
173138629291SGao Xiang 		if (cur < PAGE_SIZE)
173238629291SGao Xiang 			break;
173338629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
173438629291SGao Xiang 	}
173538629291SGao Xiang }
173638629291SGao Xiang 
1737a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio)
173847e4937aSGao Xiang {
1739a2e20a25SMatthew Wilcox (Oracle) 	struct page *page = &folio->page;
174047e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
174140452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
174247e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1743eaa9172aSGao Xiang 	struct page *pagepool = NULL;
174447e4937aSGao Xiang 	int err;
174547e4937aSGao Xiang 
174647e4937aSGao Xiang 	trace_erofs_readpage(page, false);
174747e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
174847e4937aSGao Xiang 
174938629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
175038629291SGao Xiang 				  &pagepool, true);
17511825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
175238629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
175338629291SGao Xiang 
17545c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
175547e4937aSGao Xiang 
175647e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
175783a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
175840452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, 0));
175947e4937aSGao Xiang 
176047e4937aSGao Xiang 	if (err)
17614f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
176247e4937aSGao Xiang 
176309c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1764eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
176547e4937aSGao Xiang 	return err;
176647e4937aSGao Xiang }
176747e4937aSGao Xiang 
17680615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
176947e4937aSGao Xiang {
17700615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
177147e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
177247e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1773eaa9172aSGao Xiang 	struct page *pagepool = NULL, *head = NULL, *page;
177438629291SGao Xiang 	unsigned int nr_pages;
177547e4937aSGao Xiang 
17766ea5aad3SGao Xiang 	f.readahead = true;
17770615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
177847e4937aSGao Xiang 
177938629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, f.headoffset +
178038629291SGao Xiang 				  readahead_length(rac) - 1, &pagepool, true);
178138629291SGao Xiang 	nr_pages = readahead_count(rac);
178238629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
178338629291SGao Xiang 
17840615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
178547e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
178647e4937aSGao Xiang 		head = page;
178747e4937aSGao Xiang 	}
178847e4937aSGao Xiang 
178947e4937aSGao Xiang 	while (head) {
179047e4937aSGao Xiang 		struct page *page = head;
179147e4937aSGao Xiang 		int err;
179247e4937aSGao Xiang 
179347e4937aSGao Xiang 		/* traversal in reverse order */
179447e4937aSGao Xiang 		head = (void *)page_private(page);
179547e4937aSGao Xiang 
17961825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1797a5876e24SGao Xiang 		if (err)
17984f761fa2SGao Xiang 			erofs_err(inode->i_sb,
17994f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
18004f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
180147e4937aSGao Xiang 		put_page(page);
180247e4937aSGao Xiang 	}
180338629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
18045c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
180547e4937aSGao Xiang 
180683a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
180740452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
180809c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1809eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
181047e4937aSGao Xiang }
181147e4937aSGao Xiang 
18120c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
1813a2e20a25SMatthew Wilcox (Oracle) 	.read_folio = z_erofs_read_folio,
18140615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
181547e4937aSGao Xiang };
1816