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