xref: /linux/fs/erofs/zdata.c (revision b1a54551dd9ed5ef1763b97b35a0999ca002b95c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Copyright (C) 2022 Alibaba Cloud
6  */
7 #include "compress.h"
8 #include <linux/psi.h>
9 #include <linux/cpuhotplug.h>
10 #include <trace/events/erofs.h>
11 
12 #define Z_EROFS_PCLUSTER_MAX_PAGES	(Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
13 #define Z_EROFS_INLINE_BVECS		2
14 
15 /*
16  * let's leave a type here in case of introducing
17  * another tagged pointer later.
18  */
19 typedef void *z_erofs_next_pcluster_t;
20 
21 struct z_erofs_bvec {
22 	struct page *page;
23 	int offset;
24 	unsigned int end;
25 };
26 
27 #define __Z_EROFS_BVSET(name, total) \
28 struct name { \
29 	/* point to the next page which contains the following bvecs */ \
30 	struct page *nextpage; \
31 	struct z_erofs_bvec bvec[total]; \
32 }
33 __Z_EROFS_BVSET(z_erofs_bvset,);
34 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
35 
36 /*
37  * Structure fields follow one of the following exclusion rules.
38  *
39  * I: Modifiable by initialization/destruction paths and read-only
40  *    for everyone else;
41  *
42  * L: Field should be protected by the pcluster lock;
43  *
44  * A: Field should be accessed / updated in atomic for parallelized code.
45  */
46 struct z_erofs_pcluster {
47 	struct erofs_workgroup obj;
48 	struct mutex lock;
49 
50 	/* A: point to next chained pcluster or TAILs */
51 	z_erofs_next_pcluster_t next;
52 
53 	/* L: the maximum decompression size of this round */
54 	unsigned int length;
55 
56 	/* L: total number of bvecs */
57 	unsigned int vcnt;
58 
59 	/* I: pcluster size (compressed size) in bytes */
60 	unsigned int pclustersize;
61 
62 	/* I: page offset of start position of decompression */
63 	unsigned short pageofs_out;
64 
65 	/* I: page offset of inline compressed data */
66 	unsigned short pageofs_in;
67 
68 	union {
69 		/* L: inline a certain number of bvec for bootstrap */
70 		struct z_erofs_bvset_inline bvset;
71 
72 		/* I: can be used to free the pcluster by RCU. */
73 		struct rcu_head rcu;
74 	};
75 
76 	/* I: compression algorithm format */
77 	unsigned char algorithmformat;
78 
79 	/* L: whether partial decompression or not */
80 	bool partial;
81 
82 	/* L: indicate several pageofs_outs or not */
83 	bool multibases;
84 
85 	/* A: compressed bvecs (can be cached or inplaced pages) */
86 	struct z_erofs_bvec compressed_bvecs[];
87 };
88 
89 /* the end of a chain of pclusters */
90 #define Z_EROFS_PCLUSTER_TAIL           ((void *) 0x700 + POISON_POINTER_DELTA)
91 #define Z_EROFS_PCLUSTER_NIL            (NULL)
92 
93 struct z_erofs_decompressqueue {
94 	struct super_block *sb;
95 	atomic_t pending_bios;
96 	z_erofs_next_pcluster_t head;
97 
98 	union {
99 		struct completion done;
100 		struct work_struct work;
101 		struct kthread_work kthread_work;
102 	} u;
103 	bool eio, sync;
104 };
105 
106 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
107 {
108 	return !pcl->obj.index;
109 }
110 
111 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
112 {
113 	return PAGE_ALIGN(pcl->pclustersize) >> PAGE_SHIFT;
114 }
115 
116 /*
117  * bit 30: I/O error occurred on this page
118  * bit 0 - 29: remaining parts to complete this page
119  */
120 #define Z_EROFS_PAGE_EIO			(1 << 30)
121 
122 static inline void z_erofs_onlinepage_init(struct page *page)
123 {
124 	union {
125 		atomic_t o;
126 		unsigned long v;
127 	} u = { .o = ATOMIC_INIT(1) };
128 
129 	set_page_private(page, u.v);
130 	smp_wmb();
131 	SetPagePrivate(page);
132 }
133 
134 static inline void z_erofs_onlinepage_split(struct page *page)
135 {
136 	atomic_inc((atomic_t *)&page->private);
137 }
138 
139 static void z_erofs_onlinepage_endio(struct page *page, int err)
140 {
141 	int orig, v;
142 
143 	DBG_BUGON(!PagePrivate(page));
144 
145 	do {
146 		orig = atomic_read((atomic_t *)&page->private);
147 		v = (orig - 1) | (err ? Z_EROFS_PAGE_EIO : 0);
148 	} while (atomic_cmpxchg((atomic_t *)&page->private, orig, v) != orig);
149 
150 	if (!(v & ~Z_EROFS_PAGE_EIO)) {
151 		set_page_private(page, 0);
152 		ClearPagePrivate(page);
153 		if (!(v & Z_EROFS_PAGE_EIO))
154 			SetPageUptodate(page);
155 		unlock_page(page);
156 	}
157 }
158 
159 #define Z_EROFS_ONSTACK_PAGES		32
160 
161 /*
162  * since pclustersize is variable for big pcluster feature, introduce slab
163  * pools implementation for different pcluster sizes.
164  */
165 struct z_erofs_pcluster_slab {
166 	struct kmem_cache *slab;
167 	unsigned int maxpages;
168 	char name[48];
169 };
170 
171 #define _PCLP(n) { .maxpages = n }
172 
173 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
174 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
175 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
176 };
177 
178 struct z_erofs_bvec_iter {
179 	struct page *bvpage;
180 	struct z_erofs_bvset *bvset;
181 	unsigned int nr, cur;
182 };
183 
184 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
185 {
186 	if (iter->bvpage)
187 		kunmap_local(iter->bvset);
188 	return iter->bvpage;
189 }
190 
191 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
192 {
193 	unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
194 	/* have to access nextpage in advance, otherwise it will be unmapped */
195 	struct page *nextpage = iter->bvset->nextpage;
196 	struct page *oldpage;
197 
198 	DBG_BUGON(!nextpage);
199 	oldpage = z_erofs_bvec_iter_end(iter);
200 	iter->bvpage = nextpage;
201 	iter->bvset = kmap_local_page(nextpage);
202 	iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
203 	iter->cur = 0;
204 	return oldpage;
205 }
206 
207 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
208 				    struct z_erofs_bvset_inline *bvset,
209 				    unsigned int bootstrap_nr,
210 				    unsigned int cur)
211 {
212 	*iter = (struct z_erofs_bvec_iter) {
213 		.nr = bootstrap_nr,
214 		.bvset = (struct z_erofs_bvset *)bvset,
215 	};
216 
217 	while (cur > iter->nr) {
218 		cur -= iter->nr;
219 		z_erofs_bvset_flip(iter);
220 	}
221 	iter->cur = cur;
222 }
223 
224 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
225 				struct z_erofs_bvec *bvec,
226 				struct page **candidate_bvpage,
227 				struct page **pagepool)
228 {
229 	if (iter->cur >= iter->nr) {
230 		struct page *nextpage = *candidate_bvpage;
231 
232 		if (!nextpage) {
233 			nextpage = erofs_allocpage(pagepool, GFP_NOFS);
234 			if (!nextpage)
235 				return -ENOMEM;
236 			set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
237 		}
238 		DBG_BUGON(iter->bvset->nextpage);
239 		iter->bvset->nextpage = nextpage;
240 		z_erofs_bvset_flip(iter);
241 
242 		iter->bvset->nextpage = NULL;
243 		*candidate_bvpage = NULL;
244 	}
245 	iter->bvset->bvec[iter->cur++] = *bvec;
246 	return 0;
247 }
248 
249 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
250 				 struct z_erofs_bvec *bvec,
251 				 struct page **old_bvpage)
252 {
253 	if (iter->cur == iter->nr)
254 		*old_bvpage = z_erofs_bvset_flip(iter);
255 	else
256 		*old_bvpage = NULL;
257 	*bvec = iter->bvset->bvec[iter->cur++];
258 }
259 
260 static void z_erofs_destroy_pcluster_pool(void)
261 {
262 	int i;
263 
264 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
265 		if (!pcluster_pool[i].slab)
266 			continue;
267 		kmem_cache_destroy(pcluster_pool[i].slab);
268 		pcluster_pool[i].slab = NULL;
269 	}
270 }
271 
272 static int z_erofs_create_pcluster_pool(void)
273 {
274 	struct z_erofs_pcluster_slab *pcs;
275 	struct z_erofs_pcluster *a;
276 	unsigned int size;
277 
278 	for (pcs = pcluster_pool;
279 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
280 		size = struct_size(a, compressed_bvecs, pcs->maxpages);
281 
282 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
283 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
284 					      SLAB_RECLAIM_ACCOUNT, NULL);
285 		if (pcs->slab)
286 			continue;
287 
288 		z_erofs_destroy_pcluster_pool();
289 		return -ENOMEM;
290 	}
291 	return 0;
292 }
293 
294 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int size)
295 {
296 	unsigned int nrpages = PAGE_ALIGN(size) >> PAGE_SHIFT;
297 	struct z_erofs_pcluster_slab *pcs = pcluster_pool;
298 
299 	for (; pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
300 		struct z_erofs_pcluster *pcl;
301 
302 		if (nrpages > pcs->maxpages)
303 			continue;
304 
305 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
306 		if (!pcl)
307 			return ERR_PTR(-ENOMEM);
308 		pcl->pclustersize = size;
309 		return pcl;
310 	}
311 	return ERR_PTR(-EINVAL);
312 }
313 
314 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
315 {
316 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
317 	int i;
318 
319 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
320 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
321 
322 		if (pclusterpages > pcs->maxpages)
323 			continue;
324 
325 		kmem_cache_free(pcs->slab, pcl);
326 		return;
327 	}
328 	DBG_BUGON(1);
329 }
330 
331 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
332 
333 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
334 static struct kthread_worker __rcu **z_erofs_pcpu_workers;
335 
336 static void erofs_destroy_percpu_workers(void)
337 {
338 	struct kthread_worker *worker;
339 	unsigned int cpu;
340 
341 	for_each_possible_cpu(cpu) {
342 		worker = rcu_dereference_protected(
343 					z_erofs_pcpu_workers[cpu], 1);
344 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
345 		if (worker)
346 			kthread_destroy_worker(worker);
347 	}
348 	kfree(z_erofs_pcpu_workers);
349 }
350 
351 static struct kthread_worker *erofs_init_percpu_worker(int cpu)
352 {
353 	struct kthread_worker *worker =
354 		kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
355 
356 	if (IS_ERR(worker))
357 		return worker;
358 	if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
359 		sched_set_fifo_low(worker->task);
360 	return worker;
361 }
362 
363 static int erofs_init_percpu_workers(void)
364 {
365 	struct kthread_worker *worker;
366 	unsigned int cpu;
367 
368 	z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
369 			sizeof(struct kthread_worker *), GFP_ATOMIC);
370 	if (!z_erofs_pcpu_workers)
371 		return -ENOMEM;
372 
373 	for_each_online_cpu(cpu) {	/* could miss cpu{off,on}line? */
374 		worker = erofs_init_percpu_worker(cpu);
375 		if (!IS_ERR(worker))
376 			rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
377 	}
378 	return 0;
379 }
380 #else
381 static inline void erofs_destroy_percpu_workers(void) {}
382 static inline int erofs_init_percpu_workers(void) { return 0; }
383 #endif
384 
385 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
386 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
387 static enum cpuhp_state erofs_cpuhp_state;
388 
389 static int erofs_cpu_online(unsigned int cpu)
390 {
391 	struct kthread_worker *worker, *old;
392 
393 	worker = erofs_init_percpu_worker(cpu);
394 	if (IS_ERR(worker))
395 		return PTR_ERR(worker);
396 
397 	spin_lock(&z_erofs_pcpu_worker_lock);
398 	old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
399 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
400 	if (!old)
401 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
402 	spin_unlock(&z_erofs_pcpu_worker_lock);
403 	if (old)
404 		kthread_destroy_worker(worker);
405 	return 0;
406 }
407 
408 static int erofs_cpu_offline(unsigned int cpu)
409 {
410 	struct kthread_worker *worker;
411 
412 	spin_lock(&z_erofs_pcpu_worker_lock);
413 	worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
414 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
415 	rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
416 	spin_unlock(&z_erofs_pcpu_worker_lock);
417 
418 	synchronize_rcu();
419 	if (worker)
420 		kthread_destroy_worker(worker);
421 	return 0;
422 }
423 
424 static int erofs_cpu_hotplug_init(void)
425 {
426 	int state;
427 
428 	state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
429 			"fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
430 	if (state < 0)
431 		return state;
432 
433 	erofs_cpuhp_state = state;
434 	return 0;
435 }
436 
437 static void erofs_cpu_hotplug_destroy(void)
438 {
439 	if (erofs_cpuhp_state)
440 		cpuhp_remove_state_nocalls(erofs_cpuhp_state);
441 }
442 #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
443 static inline int erofs_cpu_hotplug_init(void) { return 0; }
444 static inline void erofs_cpu_hotplug_destroy(void) {}
445 #endif
446 
447 void z_erofs_exit_zip_subsystem(void)
448 {
449 	erofs_cpu_hotplug_destroy();
450 	erofs_destroy_percpu_workers();
451 	destroy_workqueue(z_erofs_workqueue);
452 	z_erofs_destroy_pcluster_pool();
453 }
454 
455 int __init z_erofs_init_zip_subsystem(void)
456 {
457 	int err = z_erofs_create_pcluster_pool();
458 
459 	if (err)
460 		goto out_error_pcluster_pool;
461 
462 	z_erofs_workqueue = alloc_workqueue("erofs_worker",
463 			WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
464 	if (!z_erofs_workqueue) {
465 		err = -ENOMEM;
466 		goto out_error_workqueue_init;
467 	}
468 
469 	err = erofs_init_percpu_workers();
470 	if (err)
471 		goto out_error_pcpu_worker;
472 
473 	err = erofs_cpu_hotplug_init();
474 	if (err < 0)
475 		goto out_error_cpuhp_init;
476 	return err;
477 
478 out_error_cpuhp_init:
479 	erofs_destroy_percpu_workers();
480 out_error_pcpu_worker:
481 	destroy_workqueue(z_erofs_workqueue);
482 out_error_workqueue_init:
483 	z_erofs_destroy_pcluster_pool();
484 out_error_pcluster_pool:
485 	return err;
486 }
487 
488 enum z_erofs_pclustermode {
489 	Z_EROFS_PCLUSTER_INFLIGHT,
490 	/*
491 	 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
492 	 * could be dispatched into bypass queue later due to uptodated managed
493 	 * pages. All related online pages cannot be reused for inplace I/O (or
494 	 * bvpage) since it can be directly decoded without I/O submission.
495 	 */
496 	Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
497 	/*
498 	 * The pcluster was just linked to a decompression chain by us.  It can
499 	 * also be linked with the remaining pclusters, which means if the
500 	 * processing page is the tail page of a pcluster, this pcluster can
501 	 * safely use the whole page (since the previous pcluster is within the
502 	 * same chain) for in-place I/O, as illustrated below:
503 	 *  ___________________________________________________
504 	 * |  tail (partial) page  |    head (partial) page    |
505 	 * |  (of the current pcl) |   (of the previous pcl)   |
506 	 * |___PCLUSTER_FOLLOWED___|_____PCLUSTER_FOLLOWED_____|
507 	 *
508 	 * [  (*) the page above can be used as inplace I/O.   ]
509 	 */
510 	Z_EROFS_PCLUSTER_FOLLOWED,
511 };
512 
513 struct z_erofs_decompress_frontend {
514 	struct inode *const inode;
515 	struct erofs_map_blocks map;
516 	struct z_erofs_bvec_iter biter;
517 
518 	struct page *pagepool;
519 	struct page *candidate_bvpage;
520 	struct z_erofs_pcluster *pcl;
521 	z_erofs_next_pcluster_t owned_head;
522 	enum z_erofs_pclustermode mode;
523 
524 	erofs_off_t headoffset;
525 
526 	/* a pointer used to pick up inplace I/O pages */
527 	unsigned int icur;
528 };
529 
530 #define DECOMPRESS_FRONTEND_INIT(__i) { \
531 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
532 	.mode = Z_EROFS_PCLUSTER_FOLLOWED }
533 
534 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
535 {
536 	unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
537 
538 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
539 		return false;
540 
541 	if (!(fe->map.m_flags & EROFS_MAP_FULL_MAPPED))
542 		return true;
543 
544 	if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
545 	    fe->map.m_la < fe->headoffset)
546 		return true;
547 
548 	return false;
549 }
550 
551 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe)
552 {
553 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
554 	struct z_erofs_pcluster *pcl = fe->pcl;
555 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
556 	bool shouldalloc = z_erofs_should_alloc_cache(fe);
557 	bool standalone = true;
558 	/*
559 	 * optimistic allocation without direct reclaim since inplace I/O
560 	 * can be used if low memory otherwise.
561 	 */
562 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
563 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
564 	unsigned int i;
565 
566 	if (i_blocksize(fe->inode) != PAGE_SIZE)
567 		return;
568 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
569 		return;
570 
571 	for (i = 0; i < pclusterpages; ++i) {
572 		struct page *page, *newpage;
573 		void *t;	/* mark pages just found for debugging */
574 
575 		/* the compressed page was loaded before */
576 		if (READ_ONCE(pcl->compressed_bvecs[i].page))
577 			continue;
578 
579 		page = find_get_page(mc, pcl->obj.index + i);
580 
581 		if (page) {
582 			t = (void *)((unsigned long)page | 1);
583 			newpage = NULL;
584 		} else {
585 			/* I/O is needed, no possible to decompress directly */
586 			standalone = false;
587 			if (!shouldalloc)
588 				continue;
589 
590 			/*
591 			 * Try cached I/O if allocation succeeds or fallback to
592 			 * in-place I/O instead to avoid any direct reclaim.
593 			 */
594 			newpage = erofs_allocpage(&fe->pagepool, gfp);
595 			if (!newpage)
596 				continue;
597 			set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
598 			t = (void *)((unsigned long)newpage | 1);
599 		}
600 
601 		if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
602 			continue;
603 
604 		if (page)
605 			put_page(page);
606 		else if (newpage)
607 			erofs_pagepool_add(&fe->pagepool, newpage);
608 	}
609 
610 	/*
611 	 * don't do inplace I/O if all compressed pages are available in
612 	 * managed cache since it can be moved to the bypass queue instead.
613 	 */
614 	if (standalone)
615 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
616 }
617 
618 /* called by erofs_shrinker to get rid of all compressed_pages */
619 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
620 				       struct erofs_workgroup *grp)
621 {
622 	struct z_erofs_pcluster *const pcl =
623 		container_of(grp, struct z_erofs_pcluster, obj);
624 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
625 	int i;
626 
627 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
628 	/*
629 	 * refcount of workgroup is now freezed as 0,
630 	 * therefore no need to worry about available decompression users.
631 	 */
632 	for (i = 0; i < pclusterpages; ++i) {
633 		struct page *page = pcl->compressed_bvecs[i].page;
634 
635 		if (!page)
636 			continue;
637 
638 		/* block other users from reclaiming or migrating the page */
639 		if (!trylock_page(page))
640 			return -EBUSY;
641 
642 		if (!erofs_page_is_managed(sbi, page))
643 			continue;
644 
645 		/* barrier is implied in the following 'unlock_page' */
646 		WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
647 		detach_page_private(page);
648 		unlock_page(page);
649 	}
650 	return 0;
651 }
652 
653 static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)
654 {
655 	struct z_erofs_pcluster *pcl = folio_get_private(folio);
656 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
657 	bool ret;
658 	int i;
659 
660 	if (!folio_test_private(folio))
661 		return true;
662 
663 	ret = false;
664 	spin_lock(&pcl->obj.lockref.lock);
665 	if (pcl->obj.lockref.count > 0)
666 		goto out;
667 
668 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
669 	for (i = 0; i < pclusterpages; ++i) {
670 		if (pcl->compressed_bvecs[i].page == &folio->page) {
671 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
672 			ret = true;
673 			break;
674 		}
675 	}
676 	if (ret)
677 		folio_detach_private(folio);
678 out:
679 	spin_unlock(&pcl->obj.lockref.lock);
680 	return ret;
681 }
682 
683 /*
684  * It will be called only on inode eviction. In case that there are still some
685  * decompression requests in progress, wait with rescheduling for a bit here.
686  * An extra lock could be introduced instead but it seems unnecessary.
687  */
688 static void z_erofs_cache_invalidate_folio(struct folio *folio,
689 					   size_t offset, size_t length)
690 {
691 	const size_t stop = length + offset;
692 
693 	/* Check for potential overflow in debug mode */
694 	DBG_BUGON(stop > folio_size(folio) || stop < length);
695 
696 	if (offset == 0 && stop == folio_size(folio))
697 		while (!z_erofs_cache_release_folio(folio, GFP_NOFS))
698 			cond_resched();
699 }
700 
701 static const struct address_space_operations z_erofs_cache_aops = {
702 	.release_folio = z_erofs_cache_release_folio,
703 	.invalidate_folio = z_erofs_cache_invalidate_folio,
704 };
705 
706 int erofs_init_managed_cache(struct super_block *sb)
707 {
708 	struct inode *const inode = new_inode(sb);
709 
710 	if (!inode)
711 		return -ENOMEM;
712 
713 	set_nlink(inode, 1);
714 	inode->i_size = OFFSET_MAX;
715 	inode->i_mapping->a_ops = &z_erofs_cache_aops;
716 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
717 	EROFS_SB(sb)->managed_cache = inode;
718 	return 0;
719 }
720 
721 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
722 				   struct z_erofs_bvec *bvec)
723 {
724 	struct z_erofs_pcluster *const pcl = fe->pcl;
725 
726 	while (fe->icur > 0) {
727 		if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
728 			     NULL, bvec->page)) {
729 			pcl->compressed_bvecs[fe->icur] = *bvec;
730 			return true;
731 		}
732 	}
733 	return false;
734 }
735 
736 /* callers must be with pcluster lock held */
737 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
738 			       struct z_erofs_bvec *bvec, bool exclusive)
739 {
740 	int ret;
741 
742 	if (exclusive) {
743 		/* give priority for inplaceio to use file pages first */
744 		if (z_erofs_try_inplace_io(fe, bvec))
745 			return 0;
746 		/* otherwise, check if it can be used as a bvpage */
747 		if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
748 		    !fe->candidate_bvpage)
749 			fe->candidate_bvpage = bvec->page;
750 	}
751 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
752 				   &fe->pagepool);
753 	fe->pcl->vcnt += (ret >= 0);
754 	return ret;
755 }
756 
757 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
758 {
759 	struct z_erofs_pcluster *pcl = f->pcl;
760 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
761 
762 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
763 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
764 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
765 		*owned_head = &pcl->next;
766 		/* so we can attach this pcluster to our submission chain. */
767 		f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
768 		return;
769 	}
770 
771 	/* type 2, it belongs to an ongoing chain */
772 	f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
773 }
774 
775 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
776 {
777 	struct erofs_map_blocks *map = &fe->map;
778 	struct super_block *sb = fe->inode->i_sb;
779 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
780 	struct z_erofs_pcluster *pcl;
781 	struct erofs_workgroup *grp;
782 	int err;
783 
784 	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
785 	    (!ztailpacking && !erofs_blknr(sb, map->m_pa))) {
786 		DBG_BUGON(1);
787 		return -EFSCORRUPTED;
788 	}
789 
790 	/* no available pcluster, let's allocate one */
791 	pcl = z_erofs_alloc_pcluster(map->m_plen);
792 	if (IS_ERR(pcl))
793 		return PTR_ERR(pcl);
794 
795 	spin_lock_init(&pcl->obj.lockref.lock);
796 	pcl->obj.lockref.count = 1;	/* one ref for this request */
797 	pcl->algorithmformat = map->m_algorithmformat;
798 	pcl->length = 0;
799 	pcl->partial = true;
800 
801 	/* new pclusters should be claimed as type 1, primary and followed */
802 	pcl->next = fe->owned_head;
803 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
804 	fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
805 
806 	/*
807 	 * lock all primary followed works before visible to others
808 	 * and mutex_trylock *never* fails for a new pcluster.
809 	 */
810 	mutex_init(&pcl->lock);
811 	DBG_BUGON(!mutex_trylock(&pcl->lock));
812 
813 	if (ztailpacking) {
814 		pcl->obj.index = 0;	/* which indicates ztailpacking */
815 	} else {
816 		pcl->obj.index = erofs_blknr(sb, map->m_pa);
817 
818 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
819 		if (IS_ERR(grp)) {
820 			err = PTR_ERR(grp);
821 			goto err_out;
822 		}
823 
824 		if (grp != &pcl->obj) {
825 			fe->pcl = container_of(grp,
826 					struct z_erofs_pcluster, obj);
827 			err = -EEXIST;
828 			goto err_out;
829 		}
830 	}
831 	fe->owned_head = &pcl->next;
832 	fe->pcl = pcl;
833 	return 0;
834 
835 err_out:
836 	mutex_unlock(&pcl->lock);
837 	z_erofs_free_pcluster(pcl);
838 	return err;
839 }
840 
841 static int z_erofs_pcluster_begin(struct z_erofs_decompress_frontend *fe)
842 {
843 	struct erofs_map_blocks *map = &fe->map;
844 	struct super_block *sb = fe->inode->i_sb;
845 	erofs_blk_t blknr = erofs_blknr(sb, map->m_pa);
846 	struct erofs_workgroup *grp = NULL;
847 	int ret;
848 
849 	DBG_BUGON(fe->pcl);
850 
851 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
852 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
853 
854 	if (!(map->m_flags & EROFS_MAP_META)) {
855 		grp = erofs_find_workgroup(sb, blknr);
856 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
857 		DBG_BUGON(1);
858 		return -EFSCORRUPTED;
859 	}
860 
861 	if (grp) {
862 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
863 		ret = -EEXIST;
864 	} else {
865 		ret = z_erofs_register_pcluster(fe);
866 	}
867 
868 	if (ret == -EEXIST) {
869 		mutex_lock(&fe->pcl->lock);
870 		z_erofs_try_to_claim_pcluster(fe);
871 	} else if (ret) {
872 		return ret;
873 	}
874 
875 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
876 				Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
877 	if (!z_erofs_is_inline_pcluster(fe->pcl)) {
878 		/* bind cache first when cached decompression is preferred */
879 		z_erofs_bind_cache(fe);
880 	} else {
881 		void *mptr;
882 
883 		mptr = erofs_read_metabuf(&map->buf, sb, blknr, EROFS_NO_KMAP);
884 		if (IS_ERR(mptr)) {
885 			ret = PTR_ERR(mptr);
886 			erofs_err(sb, "failed to get inline data %d", ret);
887 			return ret;
888 		}
889 		get_page(map->buf.page);
890 		WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, map->buf.page);
891 		fe->pcl->pageofs_in = map->m_pa & ~PAGE_MASK;
892 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
893 	}
894 	/* file-backed inplace I/O pages are traversed in reverse order */
895 	fe->icur = z_erofs_pclusterpages(fe->pcl);
896 	return 0;
897 }
898 
899 /*
900  * keep in mind that no referenced pclusters will be freed
901  * only after a RCU grace period.
902  */
903 static void z_erofs_rcu_callback(struct rcu_head *head)
904 {
905 	z_erofs_free_pcluster(container_of(head,
906 			struct z_erofs_pcluster, rcu));
907 }
908 
909 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
910 {
911 	struct z_erofs_pcluster *const pcl =
912 		container_of(grp, struct z_erofs_pcluster, obj);
913 
914 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
915 }
916 
917 static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe)
918 {
919 	struct z_erofs_pcluster *pcl = fe->pcl;
920 
921 	if (!pcl)
922 		return;
923 
924 	z_erofs_bvec_iter_end(&fe->biter);
925 	mutex_unlock(&pcl->lock);
926 
927 	if (fe->candidate_bvpage)
928 		fe->candidate_bvpage = NULL;
929 
930 	/*
931 	 * if all pending pages are added, don't hold its reference
932 	 * any longer if the pcluster isn't hosted by ourselves.
933 	 */
934 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
935 		erofs_workgroup_put(&pcl->obj);
936 
937 	fe->pcl = NULL;
938 }
939 
940 static int z_erofs_read_fragment(struct super_block *sb, struct page *page,
941 			unsigned int cur, unsigned int end, erofs_off_t pos)
942 {
943 	struct inode *packed_inode = EROFS_SB(sb)->packed_inode;
944 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
945 	unsigned int cnt;
946 	u8 *src;
947 
948 	if (!packed_inode)
949 		return -EFSCORRUPTED;
950 
951 	buf.inode = packed_inode;
952 	for (; cur < end; cur += cnt, pos += cnt) {
953 		cnt = min_t(unsigned int, end - cur,
954 			    sb->s_blocksize - erofs_blkoff(sb, pos));
955 		src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
956 		if (IS_ERR(src)) {
957 			erofs_put_metabuf(&buf);
958 			return PTR_ERR(src);
959 		}
960 		memcpy_to_page(page, cur, src + erofs_blkoff(sb, pos), cnt);
961 	}
962 	erofs_put_metabuf(&buf);
963 	return 0;
964 }
965 
966 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
967 				struct page *page)
968 {
969 	struct inode *const inode = fe->inode;
970 	struct erofs_map_blocks *const map = &fe->map;
971 	const loff_t offset = page_offset(page);
972 	const unsigned int bs = i_blocksize(inode);
973 	bool tight = true, exclusive;
974 	unsigned int cur, end, len, split;
975 	int err = 0;
976 
977 	z_erofs_onlinepage_init(page);
978 	split = 0;
979 	end = PAGE_SIZE;
980 repeat:
981 	if (offset + end - 1 < map->m_la ||
982 	    offset + end - 1 >= map->m_la + map->m_llen) {
983 		z_erofs_pcluster_end(fe);
984 		map->m_la = offset + end - 1;
985 		map->m_llen = 0;
986 		err = z_erofs_map_blocks_iter(inode, map, 0);
987 		if (err)
988 			goto out;
989 	}
990 
991 	cur = offset > map->m_la ? 0 : map->m_la - offset;
992 	/* bump split parts first to avoid several separate cases */
993 	++split;
994 
995 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
996 		zero_user_segment(page, cur, end);
997 		tight = false;
998 		goto next_part;
999 	}
1000 
1001 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
1002 		erofs_off_t fpos = offset + cur - map->m_la;
1003 
1004 		len = min_t(unsigned int, map->m_llen - fpos, end - cur);
1005 		err = z_erofs_read_fragment(inode->i_sb, page, cur, cur + len,
1006 				EROFS_I(inode)->z_fragmentoff + fpos);
1007 		if (err)
1008 			goto out;
1009 		tight = false;
1010 		goto next_part;
1011 	}
1012 
1013 	if (!fe->pcl) {
1014 		err = z_erofs_pcluster_begin(fe);
1015 		if (err)
1016 			goto out;
1017 	}
1018 
1019 	/*
1020 	 * Ensure the current partial page belongs to this submit chain rather
1021 	 * than other concurrent submit chains or the noio(bypass) chain since
1022 	 * those chains are handled asynchronously thus the page cannot be used
1023 	 * for inplace I/O or bvpage (should be processed in a strict order.)
1024 	 */
1025 	tight &= (fe->mode > Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1026 	exclusive = (!cur && ((split <= 1) || (tight && bs == PAGE_SIZE)));
1027 	if (cur)
1028 		tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1029 
1030 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
1031 					.page = page,
1032 					.offset = offset - map->m_la,
1033 					.end = end,
1034 				  }), exclusive);
1035 	if (err)
1036 		goto out;
1037 
1038 	z_erofs_onlinepage_split(page);
1039 	if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1040 		fe->pcl->multibases = true;
1041 	if (fe->pcl->length < offset + end - map->m_la) {
1042 		fe->pcl->length = offset + end - map->m_la;
1043 		fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1044 	}
1045 	if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1046 	    !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1047 	    fe->pcl->length == map->m_llen)
1048 		fe->pcl->partial = false;
1049 next_part:
1050 	/* shorten the remaining extent to update progress */
1051 	map->m_llen = offset + cur - map->m_la;
1052 	map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
1053 
1054 	end = cur;
1055 	if (end > 0)
1056 		goto repeat;
1057 
1058 out:
1059 	z_erofs_onlinepage_endio(page, err);
1060 	return err;
1061 }
1062 
1063 static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
1064 				       unsigned int readahead_pages)
1065 {
1066 	/* auto: enable for read_folio, disable for readahead */
1067 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
1068 	    !readahead_pages)
1069 		return true;
1070 
1071 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
1072 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
1073 		return true;
1074 
1075 	return false;
1076 }
1077 
1078 static bool z_erofs_page_is_invalidated(struct page *page)
1079 {
1080 	return !page->mapping && !z_erofs_is_shortlived_page(page);
1081 }
1082 
1083 struct z_erofs_decompress_backend {
1084 	struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1085 	struct super_block *sb;
1086 	struct z_erofs_pcluster *pcl;
1087 
1088 	/* pages with the longest decompressed length for deduplication */
1089 	struct page **decompressed_pages;
1090 	/* pages to keep the compressed data */
1091 	struct page **compressed_pages;
1092 
1093 	struct list_head decompressed_secondary_bvecs;
1094 	struct page **pagepool;
1095 	unsigned int onstack_used, nr_pages;
1096 };
1097 
1098 struct z_erofs_bvec_item {
1099 	struct z_erofs_bvec bvec;
1100 	struct list_head list;
1101 };
1102 
1103 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
1104 					 struct z_erofs_bvec *bvec)
1105 {
1106 	struct z_erofs_bvec_item *item;
1107 	unsigned int pgnr;
1108 
1109 	if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK) &&
1110 	    (bvec->end == PAGE_SIZE ||
1111 	     bvec->offset + bvec->end == be->pcl->length)) {
1112 		pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
1113 		DBG_BUGON(pgnr >= be->nr_pages);
1114 		if (!be->decompressed_pages[pgnr]) {
1115 			be->decompressed_pages[pgnr] = bvec->page;
1116 			return;
1117 		}
1118 	}
1119 
1120 	/* (cold path) one pcluster is requested multiple times */
1121 	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1122 	item->bvec = *bvec;
1123 	list_add(&item->list, &be->decompressed_secondary_bvecs);
1124 }
1125 
1126 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1127 				      int err)
1128 {
1129 	unsigned int off0 = be->pcl->pageofs_out;
1130 	struct list_head *p, *n;
1131 
1132 	list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1133 		struct z_erofs_bvec_item *bvi;
1134 		unsigned int end, cur;
1135 		void *dst, *src;
1136 
1137 		bvi = container_of(p, struct z_erofs_bvec_item, list);
1138 		cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1139 		end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1140 			    bvi->bvec.end);
1141 		dst = kmap_local_page(bvi->bvec.page);
1142 		while (cur < end) {
1143 			unsigned int pgnr, scur, len;
1144 
1145 			pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1146 			DBG_BUGON(pgnr >= be->nr_pages);
1147 
1148 			scur = bvi->bvec.offset + cur -
1149 					((pgnr << PAGE_SHIFT) - off0);
1150 			len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1151 			if (!be->decompressed_pages[pgnr]) {
1152 				err = -EFSCORRUPTED;
1153 				cur += len;
1154 				continue;
1155 			}
1156 			src = kmap_local_page(be->decompressed_pages[pgnr]);
1157 			memcpy(dst + cur, src + scur, len);
1158 			kunmap_local(src);
1159 			cur += len;
1160 		}
1161 		kunmap_local(dst);
1162 		z_erofs_onlinepage_endio(bvi->bvec.page, err);
1163 		list_del(p);
1164 		kfree(bvi);
1165 	}
1166 }
1167 
1168 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
1169 {
1170 	struct z_erofs_pcluster *pcl = be->pcl;
1171 	struct z_erofs_bvec_iter biter;
1172 	struct page *old_bvpage;
1173 	int i;
1174 
1175 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1176 	for (i = 0; i < pcl->vcnt; ++i) {
1177 		struct z_erofs_bvec bvec;
1178 
1179 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1180 
1181 		if (old_bvpage)
1182 			z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1183 
1184 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1185 		z_erofs_do_decompressed_bvec(be, &bvec);
1186 	}
1187 
1188 	old_bvpage = z_erofs_bvec_iter_end(&biter);
1189 	if (old_bvpage)
1190 		z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1191 }
1192 
1193 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
1194 				  bool *overlapped)
1195 {
1196 	struct z_erofs_pcluster *pcl = be->pcl;
1197 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1198 	int i, err = 0;
1199 
1200 	*overlapped = false;
1201 	for (i = 0; i < pclusterpages; ++i) {
1202 		struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1203 		struct page *page = bvec->page;
1204 
1205 		/* compressed data ought to be valid before decompressing */
1206 		if (!page) {
1207 			err = -EIO;
1208 			continue;
1209 		}
1210 		be->compressed_pages[i] = page;
1211 
1212 		if (z_erofs_is_inline_pcluster(pcl) ||
1213 		    erofs_page_is_managed(EROFS_SB(be->sb), page)) {
1214 			if (!PageUptodate(page))
1215 				err = -EIO;
1216 			continue;
1217 		}
1218 
1219 		DBG_BUGON(z_erofs_page_is_invalidated(page));
1220 		if (z_erofs_is_shortlived_page(page))
1221 			continue;
1222 		z_erofs_do_decompressed_bvec(be, bvec);
1223 		*overlapped = true;
1224 	}
1225 	return err;
1226 }
1227 
1228 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
1229 				       int err)
1230 {
1231 	struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1232 	struct z_erofs_pcluster *pcl = be->pcl;
1233 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1234 	const struct z_erofs_decompressor *decomp =
1235 				&erofs_decompressors[pcl->algorithmformat];
1236 	int i, err2;
1237 	struct page *page;
1238 	bool overlapped;
1239 
1240 	mutex_lock(&pcl->lock);
1241 	be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1242 
1243 	/* allocate (de)compressed page arrays if cannot be kept on stack */
1244 	be->decompressed_pages = NULL;
1245 	be->compressed_pages = NULL;
1246 	be->onstack_used = 0;
1247 	if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1248 		be->decompressed_pages = be->onstack_pages;
1249 		be->onstack_used = be->nr_pages;
1250 		memset(be->decompressed_pages, 0,
1251 		       sizeof(struct page *) * be->nr_pages);
1252 	}
1253 
1254 	if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1255 		be->compressed_pages = be->onstack_pages + be->onstack_used;
1256 
1257 	if (!be->decompressed_pages)
1258 		be->decompressed_pages =
1259 			kvcalloc(be->nr_pages, sizeof(struct page *),
1260 				 GFP_KERNEL | __GFP_NOFAIL);
1261 	if (!be->compressed_pages)
1262 		be->compressed_pages =
1263 			kvcalloc(pclusterpages, sizeof(struct page *),
1264 				 GFP_KERNEL | __GFP_NOFAIL);
1265 
1266 	z_erofs_parse_out_bvecs(be);
1267 	err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1268 	if (err2)
1269 		err = err2;
1270 	if (!err)
1271 		err = decomp->decompress(&(struct z_erofs_decompress_req) {
1272 					.sb = be->sb,
1273 					.in = be->compressed_pages,
1274 					.out = be->decompressed_pages,
1275 					.pageofs_in = pcl->pageofs_in,
1276 					.pageofs_out = pcl->pageofs_out,
1277 					.inputsize = pcl->pclustersize,
1278 					.outputsize = pcl->length,
1279 					.alg = pcl->algorithmformat,
1280 					.inplace_io = overlapped,
1281 					.partial_decoding = pcl->partial,
1282 					.fillgaps = pcl->multibases,
1283 				 }, be->pagepool);
1284 
1285 	/* must handle all compressed pages before actual file pages */
1286 	if (z_erofs_is_inline_pcluster(pcl)) {
1287 		page = pcl->compressed_bvecs[0].page;
1288 		WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1289 		put_page(page);
1290 	} else {
1291 		for (i = 0; i < pclusterpages; ++i) {
1292 			/* consider shortlived pages added when decompressing */
1293 			page = be->compressed_pages[i];
1294 
1295 			if (!page || erofs_page_is_managed(sbi, page))
1296 				continue;
1297 			(void)z_erofs_put_shortlivedpage(be->pagepool, page);
1298 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1299 		}
1300 	}
1301 	if (be->compressed_pages < be->onstack_pages ||
1302 	    be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1303 		kvfree(be->compressed_pages);
1304 	z_erofs_fill_other_copies(be, err);
1305 
1306 	for (i = 0; i < be->nr_pages; ++i) {
1307 		page = be->decompressed_pages[i];
1308 		if (!page)
1309 			continue;
1310 
1311 		DBG_BUGON(z_erofs_page_is_invalidated(page));
1312 
1313 		/* recycle all individual short-lived pages */
1314 		if (z_erofs_put_shortlivedpage(be->pagepool, page))
1315 			continue;
1316 		z_erofs_onlinepage_endio(page, err);
1317 	}
1318 
1319 	if (be->decompressed_pages != be->onstack_pages)
1320 		kvfree(be->decompressed_pages);
1321 
1322 	pcl->length = 0;
1323 	pcl->partial = true;
1324 	pcl->multibases = false;
1325 	pcl->bvset.nextpage = NULL;
1326 	pcl->vcnt = 0;
1327 
1328 	/* pcluster lock MUST be taken before the following line */
1329 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1330 	mutex_unlock(&pcl->lock);
1331 	return err;
1332 }
1333 
1334 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1335 				     struct page **pagepool)
1336 {
1337 	struct z_erofs_decompress_backend be = {
1338 		.sb = io->sb,
1339 		.pagepool = pagepool,
1340 		.decompressed_secondary_bvecs =
1341 			LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1342 	};
1343 	z_erofs_next_pcluster_t owned = io->head;
1344 
1345 	while (owned != Z_EROFS_PCLUSTER_TAIL) {
1346 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1347 
1348 		be.pcl = container_of(owned, struct z_erofs_pcluster, next);
1349 		owned = READ_ONCE(be.pcl->next);
1350 
1351 		z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
1352 		if (z_erofs_is_inline_pcluster(be.pcl))
1353 			z_erofs_free_pcluster(be.pcl);
1354 		else
1355 			erofs_workgroup_put(&be.pcl->obj);
1356 	}
1357 }
1358 
1359 static void z_erofs_decompressqueue_work(struct work_struct *work)
1360 {
1361 	struct z_erofs_decompressqueue *bgq =
1362 		container_of(work, struct z_erofs_decompressqueue, u.work);
1363 	struct page *pagepool = NULL;
1364 
1365 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
1366 	z_erofs_decompress_queue(bgq, &pagepool);
1367 	erofs_release_pages(&pagepool);
1368 	kvfree(bgq);
1369 }
1370 
1371 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1372 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1373 {
1374 	z_erofs_decompressqueue_work((struct work_struct *)work);
1375 }
1376 #endif
1377 
1378 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1379 				       int bios)
1380 {
1381 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1382 
1383 	/* wake up the caller thread for sync decompression */
1384 	if (io->sync) {
1385 		if (!atomic_add_return(bios, &io->pending_bios))
1386 			complete(&io->u.done);
1387 		return;
1388 	}
1389 
1390 	if (atomic_add_return(bios, &io->pending_bios))
1391 		return;
1392 	/* Use (kthread_)work and sync decompression for atomic contexts only */
1393 	if (!in_task() || irqs_disabled() || rcu_read_lock_any_held()) {
1394 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1395 		struct kthread_worker *worker;
1396 
1397 		rcu_read_lock();
1398 		worker = rcu_dereference(
1399 				z_erofs_pcpu_workers[raw_smp_processor_id()]);
1400 		if (!worker) {
1401 			INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1402 			queue_work(z_erofs_workqueue, &io->u.work);
1403 		} else {
1404 			kthread_queue_work(worker, &io->u.kthread_work);
1405 		}
1406 		rcu_read_unlock();
1407 #else
1408 		queue_work(z_erofs_workqueue, &io->u.work);
1409 #endif
1410 		/* enable sync decompression for readahead */
1411 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1412 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1413 		return;
1414 	}
1415 	z_erofs_decompressqueue_work(&io->u.work);
1416 }
1417 
1418 static void z_erofs_fill_bio_vec(struct bio_vec *bvec,
1419 				 struct z_erofs_decompress_frontend *f,
1420 				 struct z_erofs_pcluster *pcl,
1421 				 unsigned int nr,
1422 				 struct address_space *mc)
1423 {
1424 	gfp_t gfp = mapping_gfp_mask(mc);
1425 	bool tocache = false;
1426 	struct z_erofs_bvec *zbv = pcl->compressed_bvecs + nr;
1427 	struct address_space *mapping;
1428 	struct page *page, *oldpage;
1429 	int justfound, bs = i_blocksize(f->inode);
1430 
1431 	/* Except for inplace pages, the entire page can be used for I/Os */
1432 	bvec->bv_offset = 0;
1433 	bvec->bv_len = PAGE_SIZE;
1434 repeat:
1435 	oldpage = READ_ONCE(zbv->page);
1436 	if (!oldpage)
1437 		goto out_allocpage;
1438 
1439 	justfound = (unsigned long)oldpage & 1UL;
1440 	page = (struct page *)((unsigned long)oldpage & ~1UL);
1441 	bvec->bv_page = page;
1442 
1443 	DBG_BUGON(z_erofs_is_shortlived_page(page));
1444 	/*
1445 	 * Handle preallocated cached pages.  We tried to allocate such pages
1446 	 * without triggering direct reclaim.  If allocation failed, inplace
1447 	 * file-backed pages will be used instead.
1448 	 */
1449 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1450 		set_page_private(page, 0);
1451 		WRITE_ONCE(zbv->page, page);
1452 		tocache = true;
1453 		goto out_tocache;
1454 	}
1455 
1456 	mapping = READ_ONCE(page->mapping);
1457 	/*
1458 	 * File-backed pages for inplace I/Os are all locked steady,
1459 	 * therefore it is impossible for `mapping` to be NULL.
1460 	 */
1461 	if (mapping && mapping != mc) {
1462 		if (zbv->offset < 0)
1463 			bvec->bv_offset = round_up(-zbv->offset, bs);
1464 		bvec->bv_len = round_up(zbv->end, bs) - bvec->bv_offset;
1465 		return;
1466 	}
1467 
1468 	lock_page(page);
1469 	/* only true if page reclaim goes wrong, should never happen */
1470 	DBG_BUGON(justfound && PagePrivate(page));
1471 
1472 	/* the cached page is still in managed cache */
1473 	if (page->mapping == mc) {
1474 		WRITE_ONCE(zbv->page, page);
1475 		/*
1476 		 * The cached page is still available but without a valid
1477 		 * `->private` pcluster hint.  Let's reconnect them.
1478 		 */
1479 		if (!PagePrivate(page)) {
1480 			DBG_BUGON(!justfound);
1481 			/* compressed_bvecs[] already takes a ref */
1482 			attach_page_private(page, pcl);
1483 			put_page(page);
1484 		}
1485 
1486 		/* no need to submit if it is already up-to-date */
1487 		if (PageUptodate(page)) {
1488 			unlock_page(page);
1489 			bvec->bv_page = NULL;
1490 		}
1491 		return;
1492 	}
1493 
1494 	/*
1495 	 * It has been truncated, so it's unsafe to reuse this one. Let's
1496 	 * allocate a new page for compressed data.
1497 	 */
1498 	DBG_BUGON(page->mapping);
1499 	DBG_BUGON(!justfound);
1500 
1501 	tocache = true;
1502 	unlock_page(page);
1503 	put_page(page);
1504 out_allocpage:
1505 	page = erofs_allocpage(&f->pagepool, gfp | __GFP_NOFAIL);
1506 	if (oldpage != cmpxchg(&zbv->page, oldpage, page)) {
1507 		erofs_pagepool_add(&f->pagepool, page);
1508 		cond_resched();
1509 		goto repeat;
1510 	}
1511 	bvec->bv_page = page;
1512 out_tocache:
1513 	if (!tocache || bs != PAGE_SIZE ||
1514 	    add_to_page_cache_lru(page, mc, pcl->obj.index + nr, gfp)) {
1515 		/* turn into a temporary shortlived page (1 ref) */
1516 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1517 		return;
1518 	}
1519 	attach_page_private(page, pcl);
1520 	/* drop a refcount added by allocpage (then 2 refs in total here) */
1521 	put_page(page);
1522 }
1523 
1524 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1525 			      struct z_erofs_decompressqueue *fgq, bool *fg)
1526 {
1527 	struct z_erofs_decompressqueue *q;
1528 
1529 	if (fg && !*fg) {
1530 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1531 		if (!q) {
1532 			*fg = true;
1533 			goto fg_out;
1534 		}
1535 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1536 		kthread_init_work(&q->u.kthread_work,
1537 				  z_erofs_decompressqueue_kthread_work);
1538 #else
1539 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1540 #endif
1541 	} else {
1542 fg_out:
1543 		q = fgq;
1544 		init_completion(&fgq->u.done);
1545 		atomic_set(&fgq->pending_bios, 0);
1546 		q->eio = false;
1547 		q->sync = true;
1548 	}
1549 	q->sb = sb;
1550 	q->head = Z_EROFS_PCLUSTER_TAIL;
1551 	return q;
1552 }
1553 
1554 /* define decompression jobqueue types */
1555 enum {
1556 	JQ_BYPASS,
1557 	JQ_SUBMIT,
1558 	NR_JOBQUEUES,
1559 };
1560 
1561 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1562 				    z_erofs_next_pcluster_t qtail[],
1563 				    z_erofs_next_pcluster_t owned_head)
1564 {
1565 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1566 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1567 
1568 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
1569 
1570 	WRITE_ONCE(*submit_qtail, owned_head);
1571 	WRITE_ONCE(*bypass_qtail, &pcl->next);
1572 
1573 	qtail[JQ_BYPASS] = &pcl->next;
1574 }
1575 
1576 static void z_erofs_submissionqueue_endio(struct bio *bio)
1577 {
1578 	struct z_erofs_decompressqueue *q = bio->bi_private;
1579 	blk_status_t err = bio->bi_status;
1580 	struct bio_vec *bvec;
1581 	struct bvec_iter_all iter_all;
1582 
1583 	bio_for_each_segment_all(bvec, bio, iter_all) {
1584 		struct page *page = bvec->bv_page;
1585 
1586 		DBG_BUGON(PageUptodate(page));
1587 		DBG_BUGON(z_erofs_page_is_invalidated(page));
1588 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1589 			if (!err)
1590 				SetPageUptodate(page);
1591 			unlock_page(page);
1592 		}
1593 	}
1594 	if (err)
1595 		q->eio = true;
1596 	z_erofs_decompress_kickoff(q, -1);
1597 	bio_put(bio);
1598 }
1599 
1600 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1601 				 struct z_erofs_decompressqueue *fgq,
1602 				 bool *force_fg, bool readahead)
1603 {
1604 	struct super_block *sb = f->inode->i_sb;
1605 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1606 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1607 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1608 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1609 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
1610 	erofs_off_t last_pa;
1611 	struct block_device *last_bdev;
1612 	unsigned int nr_bios = 0;
1613 	struct bio *bio = NULL;
1614 	unsigned long pflags;
1615 	int memstall = 0;
1616 
1617 	/* No need to read from device for pclusters in the bypass queue. */
1618 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1619 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1620 
1621 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1622 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1623 
1624 	/* by default, all need io submission */
1625 	q[JQ_SUBMIT]->head = owned_head;
1626 
1627 	do {
1628 		struct erofs_map_dev mdev;
1629 		struct z_erofs_pcluster *pcl;
1630 		erofs_off_t cur, end;
1631 		struct bio_vec bvec;
1632 		unsigned int i = 0;
1633 		bool bypass = true;
1634 
1635 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1636 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1637 		owned_head = READ_ONCE(pcl->next);
1638 
1639 		if (z_erofs_is_inline_pcluster(pcl)) {
1640 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1641 			continue;
1642 		}
1643 
1644 		/* no device id here, thus it will always succeed */
1645 		mdev = (struct erofs_map_dev) {
1646 			.m_pa = erofs_pos(sb, pcl->obj.index),
1647 		};
1648 		(void)erofs_map_dev(sb, &mdev);
1649 
1650 		cur = mdev.m_pa;
1651 		end = cur + pcl->pclustersize;
1652 		do {
1653 			z_erofs_fill_bio_vec(&bvec, f, pcl, i++, mc);
1654 			if (!bvec.bv_page)
1655 				continue;
1656 
1657 			if (bio && (cur != last_pa ||
1658 				    last_bdev != mdev.m_bdev)) {
1659 submit_bio_retry:
1660 				submit_bio(bio);
1661 				if (memstall) {
1662 					psi_memstall_leave(&pflags);
1663 					memstall = 0;
1664 				}
1665 				bio = NULL;
1666 			}
1667 
1668 			if (unlikely(PageWorkingset(bvec.bv_page)) &&
1669 			    !memstall) {
1670 				psi_memstall_enter(&pflags);
1671 				memstall = 1;
1672 			}
1673 
1674 			if (!bio) {
1675 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1676 						REQ_OP_READ, GFP_NOIO);
1677 				bio->bi_end_io = z_erofs_submissionqueue_endio;
1678 				bio->bi_iter.bi_sector = cur >> 9;
1679 				bio->bi_private = q[JQ_SUBMIT];
1680 				if (readahead)
1681 					bio->bi_opf |= REQ_RAHEAD;
1682 				++nr_bios;
1683 				last_bdev = mdev.m_bdev;
1684 			}
1685 
1686 			if (cur + bvec.bv_len > end)
1687 				bvec.bv_len = end - cur;
1688 			if (!bio_add_page(bio, bvec.bv_page, bvec.bv_len,
1689 					  bvec.bv_offset))
1690 				goto submit_bio_retry;
1691 
1692 			last_pa = cur + bvec.bv_len;
1693 			bypass = false;
1694 		} while ((cur += bvec.bv_len) < end);
1695 
1696 		if (!bypass)
1697 			qtail[JQ_SUBMIT] = &pcl->next;
1698 		else
1699 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1700 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1701 
1702 	if (bio) {
1703 		submit_bio(bio);
1704 		if (memstall)
1705 			psi_memstall_leave(&pflags);
1706 	}
1707 
1708 	/*
1709 	 * although background is preferred, no one is pending for submission.
1710 	 * don't issue decompression but drop it directly instead.
1711 	 */
1712 	if (!*force_fg && !nr_bios) {
1713 		kvfree(q[JQ_SUBMIT]);
1714 		return;
1715 	}
1716 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1717 }
1718 
1719 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1720 			     bool force_fg, bool ra)
1721 {
1722 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1723 
1724 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1725 		return;
1726 	z_erofs_submit_queue(f, io, &force_fg, ra);
1727 
1728 	/* handle bypass queue (no i/o pclusters) immediately */
1729 	z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
1730 
1731 	if (!force_fg)
1732 		return;
1733 
1734 	/* wait until all bios are completed */
1735 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1736 
1737 	/* handle synchronous decompress queue in the caller context */
1738 	z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool);
1739 }
1740 
1741 /*
1742  * Since partial uptodate is still unimplemented for now, we have to use
1743  * approximate readmore strategies as a start.
1744  */
1745 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1746 		struct readahead_control *rac, bool backmost)
1747 {
1748 	struct inode *inode = f->inode;
1749 	struct erofs_map_blocks *map = &f->map;
1750 	erofs_off_t cur, end, headoffset = f->headoffset;
1751 	int err;
1752 
1753 	if (backmost) {
1754 		if (rac)
1755 			end = headoffset + readahead_length(rac) - 1;
1756 		else
1757 			end = headoffset + PAGE_SIZE - 1;
1758 		map->m_la = end;
1759 		err = z_erofs_map_blocks_iter(inode, map,
1760 					      EROFS_GET_BLOCKS_READMORE);
1761 		if (err)
1762 			return;
1763 
1764 		/* expand ra for the trailing edge if readahead */
1765 		if (rac) {
1766 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1767 			readahead_expand(rac, headoffset, cur - headoffset);
1768 			return;
1769 		}
1770 		end = round_up(end, PAGE_SIZE);
1771 	} else {
1772 		end = round_up(map->m_la, PAGE_SIZE);
1773 
1774 		if (!map->m_llen)
1775 			return;
1776 	}
1777 
1778 	cur = map->m_la + map->m_llen - 1;
1779 	while ((cur >= end) && (cur < i_size_read(inode))) {
1780 		pgoff_t index = cur >> PAGE_SHIFT;
1781 		struct page *page;
1782 
1783 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1784 		if (page) {
1785 			if (PageUptodate(page))
1786 				unlock_page(page);
1787 			else
1788 				(void)z_erofs_do_read_page(f, page);
1789 			put_page(page);
1790 		}
1791 
1792 		if (cur < PAGE_SIZE)
1793 			break;
1794 		cur = (index << PAGE_SHIFT) - 1;
1795 	}
1796 }
1797 
1798 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1799 {
1800 	struct inode *const inode = folio->mapping->host;
1801 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1802 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1803 	int err;
1804 
1805 	trace_erofs_read_folio(folio, false);
1806 	f.headoffset = (erofs_off_t)folio->index << PAGE_SHIFT;
1807 
1808 	z_erofs_pcluster_readmore(&f, NULL, true);
1809 	err = z_erofs_do_read_page(&f, &folio->page);
1810 	z_erofs_pcluster_readmore(&f, NULL, false);
1811 	z_erofs_pcluster_end(&f);
1812 
1813 	/* if some compressed cluster ready, need submit them anyway */
1814 	z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, 0), false);
1815 
1816 	if (err && err != -EINTR)
1817 		erofs_err(inode->i_sb, "read error %d @ %lu of nid %llu",
1818 			  err, folio->index, EROFS_I(inode)->nid);
1819 
1820 	erofs_put_metabuf(&f.map.buf);
1821 	erofs_release_pages(&f.pagepool);
1822 	return err;
1823 }
1824 
1825 static void z_erofs_readahead(struct readahead_control *rac)
1826 {
1827 	struct inode *const inode = rac->mapping->host;
1828 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1829 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1830 	struct folio *head = NULL, *folio;
1831 	unsigned int nr_folios;
1832 	int err;
1833 
1834 	f.headoffset = readahead_pos(rac);
1835 
1836 	z_erofs_pcluster_readmore(&f, rac, true);
1837 	nr_folios = readahead_count(rac);
1838 	trace_erofs_readpages(inode, readahead_index(rac), nr_folios, false);
1839 
1840 	while ((folio = readahead_folio(rac))) {
1841 		folio->private = head;
1842 		head = folio;
1843 	}
1844 
1845 	/* traverse in reverse order for best metadata I/O performance */
1846 	while (head) {
1847 		folio = head;
1848 		head = folio_get_private(folio);
1849 
1850 		err = z_erofs_do_read_page(&f, &folio->page);
1851 		if (err && err != -EINTR)
1852 			erofs_err(inode->i_sb, "readahead error at folio %lu @ nid %llu",
1853 				  folio->index, EROFS_I(inode)->nid);
1854 	}
1855 	z_erofs_pcluster_readmore(&f, rac, false);
1856 	z_erofs_pcluster_end(&f);
1857 
1858 	z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, nr_folios), true);
1859 	erofs_put_metabuf(&f.map.buf);
1860 	erofs_release_pages(&f.pagepool);
1861 }
1862 
1863 const struct address_space_operations z_erofs_aops = {
1864 	.read_folio = z_erofs_read_folio,
1865 	.readahead = z_erofs_readahead,
1866 };
1867