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