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