xref: /openbmc/linux/fs/erofs/zdata.c (revision 7dea3de7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Created by Gao Xiang <gaoxiang25@huawei.com>
6  */
7 #include "zdata.h"
8 #include "compress.h"
9 #include <linux/prefetch.h>
10 
11 #include <trace/events/erofs.h>
12 
13 /*
14  * since pclustersize is variable for big pcluster feature, introduce slab
15  * pools implementation for different pcluster sizes.
16  */
17 struct z_erofs_pcluster_slab {
18 	struct kmem_cache *slab;
19 	unsigned int maxpages;
20 	char name[48];
21 };
22 
23 #define _PCLP(n) { .maxpages = n }
24 
25 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
26 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
27 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
28 };
29 
30 static void z_erofs_destroy_pcluster_pool(void)
31 {
32 	int i;
33 
34 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
35 		if (!pcluster_pool[i].slab)
36 			continue;
37 		kmem_cache_destroy(pcluster_pool[i].slab);
38 		pcluster_pool[i].slab = NULL;
39 	}
40 }
41 
42 static int z_erofs_create_pcluster_pool(void)
43 {
44 	struct z_erofs_pcluster_slab *pcs;
45 	struct z_erofs_pcluster *a;
46 	unsigned int size;
47 
48 	for (pcs = pcluster_pool;
49 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
50 		size = struct_size(a, compressed_pages, pcs->maxpages);
51 
52 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
53 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
54 					      SLAB_RECLAIM_ACCOUNT, NULL);
55 		if (pcs->slab)
56 			continue;
57 
58 		z_erofs_destroy_pcluster_pool();
59 		return -ENOMEM;
60 	}
61 	return 0;
62 }
63 
64 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
65 {
66 	int i;
67 
68 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
69 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
70 		struct z_erofs_pcluster *pcl;
71 
72 		if (nrpages > pcs->maxpages)
73 			continue;
74 
75 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
76 		if (!pcl)
77 			return ERR_PTR(-ENOMEM);
78 		pcl->pclusterpages = nrpages;
79 		return pcl;
80 	}
81 	return ERR_PTR(-EINVAL);
82 }
83 
84 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
85 {
86 	int i;
87 
88 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
89 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
90 
91 		if (pcl->pclusterpages > pcs->maxpages)
92 			continue;
93 
94 		kmem_cache_free(pcs->slab, pcl);
95 		return;
96 	}
97 	DBG_BUGON(1);
98 }
99 
100 /*
101  * a compressed_pages[] placeholder in order to avoid
102  * being filled with file pages for in-place decompression.
103  */
104 #define PAGE_UNALLOCATED     ((void *)0x5F0E4B1D)
105 
106 /* how to allocate cached pages for a pcluster */
107 enum z_erofs_cache_alloctype {
108 	DONTALLOC,	/* don't allocate any cached pages */
109 	DELAYEDALLOC,	/* delayed allocation (at the time of submitting io) */
110 	/*
111 	 * try to use cached I/O if page allocation succeeds or fallback
112 	 * to in-place I/O instead to avoid any direct reclaim.
113 	 */
114 	TRYALLOC,
115 };
116 
117 /*
118  * tagged pointer with 1-bit tag for all compressed pages
119  * tag 0 - the page is just found with an extra page reference
120  */
121 typedef tagptr1_t compressed_page_t;
122 
123 #define tag_compressed_page_justfound(page) \
124 	tagptr_fold(compressed_page_t, page, 1)
125 
126 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
127 
128 void z_erofs_exit_zip_subsystem(void)
129 {
130 	destroy_workqueue(z_erofs_workqueue);
131 	z_erofs_destroy_pcluster_pool();
132 }
133 
134 static inline int z_erofs_init_workqueue(void)
135 {
136 	const unsigned int onlinecpus = num_possible_cpus();
137 
138 	/*
139 	 * no need to spawn too many threads, limiting threads could minimum
140 	 * scheduling overhead, perhaps per-CPU threads should be better?
141 	 */
142 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
143 					    WQ_UNBOUND | WQ_HIGHPRI,
144 					    onlinecpus + onlinecpus / 4);
145 	return z_erofs_workqueue ? 0 : -ENOMEM;
146 }
147 
148 int __init z_erofs_init_zip_subsystem(void)
149 {
150 	int err = z_erofs_create_pcluster_pool();
151 
152 	if (err)
153 		return err;
154 	err = z_erofs_init_workqueue();
155 	if (err)
156 		z_erofs_destroy_pcluster_pool();
157 	return err;
158 }
159 
160 enum z_erofs_collectmode {
161 	COLLECT_SECONDARY,
162 	COLLECT_PRIMARY,
163 	/*
164 	 * The current collection was the tail of an exist chain, in addition
165 	 * that the previous processed chained collections are all decided to
166 	 * be hooked up to it.
167 	 * A new chain will be created for the remaining collections which are
168 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
169 	 * the next collection cannot reuse the whole page safely in
170 	 * the following scenario:
171 	 *  ________________________________________________________________
172 	 * |      tail (partial) page     |       head (partial) page       |
173 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
174 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
175 	 */
176 	COLLECT_PRIMARY_HOOKED,
177 	/*
178 	 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
179 	 * could be dispatched into bypass queue later due to uptodated managed
180 	 * pages. All related online pages cannot be reused for inplace I/O (or
181 	 * pagevec) since it can be directly decoded without I/O submission.
182 	 */
183 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
184 	/*
185 	 * The current collection has been linked with the owned chain, and
186 	 * could also be linked with the remaining collections, which means
187 	 * if the processing page is the tail page of the collection, thus
188 	 * the current collection can safely use the whole page (since
189 	 * the previous collection is under control) for in-place I/O, as
190 	 * illustrated below:
191 	 *  ________________________________________________________________
192 	 * |  tail (partial) page |          head (partial) page           |
193 	 * |  (of the current cl) |      (of the previous collection)      |
194 	 * |  PRIMARY_FOLLOWED or |                                        |
195 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
196 	 *
197 	 * [  (*) the above page can be used as inplace I/O.               ]
198 	 */
199 	COLLECT_PRIMARY_FOLLOWED,
200 };
201 
202 struct z_erofs_collector {
203 	struct z_erofs_pagevec_ctor vector;
204 
205 	struct z_erofs_pcluster *pcl, *tailpcl;
206 	struct z_erofs_collection *cl;
207 	/* a pointer used to pick up inplace I/O pages */
208 	struct page **icpage_ptr;
209 	z_erofs_next_pcluster_t owned_head;
210 
211 	enum z_erofs_collectmode mode;
212 };
213 
214 struct z_erofs_decompress_frontend {
215 	struct inode *const inode;
216 
217 	struct z_erofs_collector clt;
218 	struct erofs_map_blocks map;
219 
220 	bool readahead;
221 	/* used for applying cache strategy on the fly */
222 	bool backmost;
223 	erofs_off_t headoffset;
224 };
225 
226 #define COLLECTOR_INIT() { \
227 	.owned_head = Z_EROFS_PCLUSTER_TAIL, \
228 	.mode = COLLECT_PRIMARY_FOLLOWED }
229 
230 #define DECOMPRESS_FRONTEND_INIT(__i) { \
231 	.inode = __i, .clt = COLLECTOR_INIT(), \
232 	.backmost = true, }
233 
234 static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
235 static DEFINE_MUTEX(z_pagemap_global_lock);
236 
237 static void preload_compressed_pages(struct z_erofs_collector *clt,
238 				     struct address_space *mc,
239 				     enum z_erofs_cache_alloctype type,
240 				     struct list_head *pagepool)
241 {
242 	struct z_erofs_pcluster *pcl = clt->pcl;
243 	bool standalone = true;
244 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
245 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
246 	struct page **pages;
247 	pgoff_t index;
248 
249 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
250 		return;
251 
252 	pages = pcl->compressed_pages;
253 	index = pcl->obj.index;
254 	for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
255 		struct page *page;
256 		compressed_page_t t;
257 		struct page *newpage = NULL;
258 
259 		/* the compressed page was loaded before */
260 		if (READ_ONCE(*pages))
261 			continue;
262 
263 		page = find_get_page(mc, index);
264 
265 		if (page) {
266 			t = tag_compressed_page_justfound(page);
267 		} else {
268 			/* I/O is needed, no possible to decompress directly */
269 			standalone = false;
270 			switch (type) {
271 			case DELAYEDALLOC:
272 				t = tagptr_init(compressed_page_t,
273 						PAGE_UNALLOCATED);
274 				break;
275 			case TRYALLOC:
276 				newpage = erofs_allocpage(pagepool, gfp);
277 				if (!newpage)
278 					continue;
279 				set_page_private(newpage,
280 						 Z_EROFS_PREALLOCATED_PAGE);
281 				t = tag_compressed_page_justfound(newpage);
282 				break;
283 			default:        /* DONTALLOC */
284 				continue;
285 			}
286 		}
287 
288 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
289 			continue;
290 
291 		if (page) {
292 			put_page(page);
293 		} else if (newpage) {
294 			set_page_private(newpage, 0);
295 			list_add(&newpage->lru, pagepool);
296 		}
297 	}
298 
299 	/*
300 	 * don't do inplace I/O if all compressed pages are available in
301 	 * managed cache since it can be moved to the bypass queue instead.
302 	 */
303 	if (standalone)
304 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
305 }
306 
307 /* called by erofs_shrinker to get rid of all compressed_pages */
308 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
309 				       struct erofs_workgroup *grp)
310 {
311 	struct z_erofs_pcluster *const pcl =
312 		container_of(grp, struct z_erofs_pcluster, obj);
313 	struct address_space *const mapping = MNGD_MAPPING(sbi);
314 	int i;
315 
316 	/*
317 	 * refcount of workgroup is now freezed as 1,
318 	 * therefore no need to worry about available decompression users.
319 	 */
320 	for (i = 0; i < pcl->pclusterpages; ++i) {
321 		struct page *page = pcl->compressed_pages[i];
322 
323 		if (!page)
324 			continue;
325 
326 		/* block other users from reclaiming or migrating the page */
327 		if (!trylock_page(page))
328 			return -EBUSY;
329 
330 		if (page->mapping != mapping)
331 			continue;
332 
333 		/* barrier is implied in the following 'unlock_page' */
334 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
335 		detach_page_private(page);
336 		unlock_page(page);
337 	}
338 	return 0;
339 }
340 
341 int erofs_try_to_free_cached_page(struct address_space *mapping,
342 				  struct page *page)
343 {
344 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
345 	int ret = 0;	/* 0 - busy */
346 
347 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
348 		unsigned int i;
349 
350 		for (i = 0; i < pcl->pclusterpages; ++i) {
351 			if (pcl->compressed_pages[i] == page) {
352 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
353 				ret = 1;
354 				break;
355 			}
356 		}
357 		erofs_workgroup_unfreeze(&pcl->obj, 1);
358 
359 		if (ret)
360 			detach_page_private(page);
361 	}
362 	return ret;
363 }
364 
365 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
366 static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
367 				   struct page *page)
368 {
369 	struct z_erofs_pcluster *const pcl = clt->pcl;
370 
371 	while (clt->icpage_ptr > pcl->compressed_pages)
372 		if (!cmpxchg(--clt->icpage_ptr, NULL, page))
373 			return true;
374 	return false;
375 }
376 
377 /* callers must be with collection lock held */
378 static int z_erofs_attach_page(struct z_erofs_collector *clt,
379 			       struct page *page,
380 			       enum z_erofs_page_type type)
381 {
382 	int ret;
383 
384 	/* give priority for inplaceio */
385 	if (clt->mode >= COLLECT_PRIMARY &&
386 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
387 	    z_erofs_try_inplace_io(clt, page))
388 		return 0;
389 
390 	ret = z_erofs_pagevec_enqueue(&clt->vector, page, type);
391 	clt->cl->vcnt += (unsigned int)ret;
392 
393 	return ret ? 0 : -EAGAIN;
394 }
395 
396 static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
397 {
398 	struct z_erofs_pcluster *pcl = clt->pcl;
399 	z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
400 
401 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
402 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
403 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
404 		*owned_head = &pcl->next;
405 		/* so we can attach this pcluster to our submission chain. */
406 		clt->mode = COLLECT_PRIMARY_FOLLOWED;
407 		return;
408 	}
409 
410 	/*
411 	 * type 2, link to the end of an existing open chain, be careful
412 	 * that its submission is controlled by the original attached chain.
413 	 */
414 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
415 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
416 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
417 		clt->mode = COLLECT_PRIMARY_HOOKED;
418 		clt->tailpcl = NULL;
419 		return;
420 	}
421 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
422 	clt->mode = COLLECT_PRIMARY;
423 }
424 
425 static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
426 				     struct inode *inode,
427 				     struct erofs_map_blocks *map)
428 {
429 	struct z_erofs_pcluster *pcl = clt->pcl;
430 	struct z_erofs_collection *cl;
431 	unsigned int length;
432 
433 	/* to avoid unexpected loop formed by corrupted images */
434 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
435 		DBG_BUGON(1);
436 		return -EFSCORRUPTED;
437 	}
438 
439 	cl = z_erofs_primarycollection(pcl);
440 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
441 		DBG_BUGON(1);
442 		return -EFSCORRUPTED;
443 	}
444 
445 	length = READ_ONCE(pcl->length);
446 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
447 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
448 			DBG_BUGON(1);
449 			return -EFSCORRUPTED;
450 		}
451 	} else {
452 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
453 
454 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
455 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
456 
457 		while (llen > length &&
458 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
459 			cpu_relax();
460 			length = READ_ONCE(pcl->length);
461 		}
462 	}
463 	mutex_lock(&cl->lock);
464 	/* used to check tail merging loop due to corrupted images */
465 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
466 		clt->tailpcl = pcl;
467 
468 	z_erofs_try_to_claim_pcluster(clt);
469 	clt->cl = cl;
470 	return 0;
471 }
472 
473 static int z_erofs_register_collection(struct z_erofs_collector *clt,
474 				       struct inode *inode,
475 				       struct erofs_map_blocks *map)
476 {
477 	struct z_erofs_pcluster *pcl;
478 	struct z_erofs_collection *cl;
479 	struct erofs_workgroup *grp;
480 	int err;
481 
482 	/* no available pcluster, let's allocate one */
483 	pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
484 	if (IS_ERR(pcl))
485 		return PTR_ERR(pcl);
486 
487 	atomic_set(&pcl->obj.refcount, 1);
488 	pcl->obj.index = map->m_pa >> PAGE_SHIFT;
489 
490 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
491 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
492 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
493 
494 	if (map->m_flags & EROFS_MAP_ZIPPED)
495 		pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
496 	else
497 		pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
498 
499 	/* new pclusters should be claimed as type 1, primary and followed */
500 	pcl->next = clt->owned_head;
501 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
502 
503 	cl = z_erofs_primarycollection(pcl);
504 	cl->pageofs = map->m_la & ~PAGE_MASK;
505 
506 	/*
507 	 * lock all primary followed works before visible to others
508 	 * and mutex_trylock *never* fails for a new pcluster.
509 	 */
510 	mutex_init(&cl->lock);
511 	DBG_BUGON(!mutex_trylock(&cl->lock));
512 
513 	grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
514 	if (IS_ERR(grp)) {
515 		err = PTR_ERR(grp);
516 		goto err_out;
517 	}
518 
519 	if (grp != &pcl->obj) {
520 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
521 		err = -EEXIST;
522 		goto err_out;
523 	}
524 	/* used to check tail merging loop due to corrupted images */
525 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
526 		clt->tailpcl = pcl;
527 	clt->owned_head = &pcl->next;
528 	clt->pcl = pcl;
529 	clt->cl = cl;
530 	return 0;
531 
532 err_out:
533 	mutex_unlock(&cl->lock);
534 	z_erofs_free_pcluster(pcl);
535 	return err;
536 }
537 
538 static int z_erofs_collector_begin(struct z_erofs_collector *clt,
539 				   struct inode *inode,
540 				   struct erofs_map_blocks *map)
541 {
542 	struct erofs_workgroup *grp;
543 	int ret;
544 
545 	DBG_BUGON(clt->cl);
546 
547 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
548 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
549 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
550 
551 	if (!PAGE_ALIGNED(map->m_pa)) {
552 		DBG_BUGON(1);
553 		return -EINVAL;
554 	}
555 
556 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
557 	if (grp) {
558 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
559 	} else {
560 		ret = z_erofs_register_collection(clt, inode, map);
561 
562 		if (!ret)
563 			goto out;
564 		if (ret != -EEXIST)
565 			return ret;
566 	}
567 
568 	ret = z_erofs_lookup_collection(clt, inode, map);
569 	if (ret) {
570 		erofs_workgroup_put(&clt->pcl->obj);
571 		return ret;
572 	}
573 
574 out:
575 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
576 				  clt->cl->pagevec, clt->cl->vcnt);
577 
578 	/* since file-backed online pages are traversed in reverse order */
579 	clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages;
580 	return 0;
581 }
582 
583 /*
584  * keep in mind that no referenced pclusters will be freed
585  * only after a RCU grace period.
586  */
587 static void z_erofs_rcu_callback(struct rcu_head *head)
588 {
589 	struct z_erofs_collection *const cl =
590 		container_of(head, struct z_erofs_collection, rcu);
591 
592 	z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
593 					   primary_collection));
594 }
595 
596 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
597 {
598 	struct z_erofs_pcluster *const pcl =
599 		container_of(grp, struct z_erofs_pcluster, obj);
600 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
601 
602 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
603 }
604 
605 static void z_erofs_collection_put(struct z_erofs_collection *cl)
606 {
607 	struct z_erofs_pcluster *const pcl =
608 		container_of(cl, struct z_erofs_pcluster, primary_collection);
609 
610 	erofs_workgroup_put(&pcl->obj);
611 }
612 
613 static bool z_erofs_collector_end(struct z_erofs_collector *clt)
614 {
615 	struct z_erofs_collection *cl = clt->cl;
616 
617 	if (!cl)
618 		return false;
619 
620 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
621 	mutex_unlock(&cl->lock);
622 
623 	/*
624 	 * if all pending pages are added, don't hold its reference
625 	 * any longer if the pcluster isn't hosted by ourselves.
626 	 */
627 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
628 		z_erofs_collection_put(cl);
629 
630 	clt->cl = NULL;
631 	return true;
632 }
633 
634 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
635 				       unsigned int cachestrategy,
636 				       erofs_off_t la)
637 {
638 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
639 		return false;
640 
641 	if (fe->backmost)
642 		return true;
643 
644 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
645 		la < fe->headoffset;
646 }
647 
648 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
649 				struct page *page, struct list_head *pagepool)
650 {
651 	struct inode *const inode = fe->inode;
652 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
653 	struct erofs_map_blocks *const map = &fe->map;
654 	struct z_erofs_collector *const clt = &fe->clt;
655 	const loff_t offset = page_offset(page);
656 	bool tight = true;
657 
658 	enum z_erofs_cache_alloctype cache_strategy;
659 	enum z_erofs_page_type page_type;
660 	unsigned int cur, end, spiltted, index;
661 	int err = 0;
662 
663 	/* register locked file pages as online pages in pack */
664 	z_erofs_onlinepage_init(page);
665 
666 	spiltted = 0;
667 	end = PAGE_SIZE;
668 repeat:
669 	cur = end - 1;
670 
671 	/* lucky, within the range of the current map_blocks */
672 	if (offset + cur >= map->m_la &&
673 	    offset + cur < map->m_la + map->m_llen) {
674 		/* didn't get a valid collection previously (very rare) */
675 		if (!clt->cl)
676 			goto restart_now;
677 		goto hitted;
678 	}
679 
680 	/* go ahead the next map_blocks */
681 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
682 
683 	if (z_erofs_collector_end(clt))
684 		fe->backmost = false;
685 
686 	map->m_la = offset + cur;
687 	map->m_llen = 0;
688 	err = z_erofs_map_blocks_iter(inode, map, 0);
689 	if (err)
690 		goto err_out;
691 
692 restart_now:
693 	if (!(map->m_flags & EROFS_MAP_MAPPED))
694 		goto hitted;
695 
696 	err = z_erofs_collector_begin(clt, inode, map);
697 	if (err)
698 		goto err_out;
699 
700 	/* preload all compressed pages (maybe downgrade role if necessary) */
701 	if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
702 		cache_strategy = TRYALLOC;
703 	else
704 		cache_strategy = DONTALLOC;
705 
706 	preload_compressed_pages(clt, MNGD_MAPPING(sbi),
707 				 cache_strategy, pagepool);
708 
709 hitted:
710 	/*
711 	 * Ensure the current partial page belongs to this submit chain rather
712 	 * than other concurrent submit chains or the noio(bypass) chain since
713 	 * those chains are handled asynchronously thus the page cannot be used
714 	 * for inplace I/O or pagevec (should be processed in strict order.)
715 	 */
716 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
717 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
718 
719 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
720 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
721 		zero_user_segment(page, cur, end);
722 		goto next_part;
723 	}
724 
725 	/* let's derive page type */
726 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
727 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
728 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
729 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
730 
731 	if (cur)
732 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
733 
734 retry:
735 	err = z_erofs_attach_page(clt, page, page_type);
736 	/* should allocate an additional short-lived page for pagevec */
737 	if (err == -EAGAIN) {
738 		struct page *const newpage =
739 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
740 
741 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
742 		err = z_erofs_attach_page(clt, newpage,
743 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
744 		if (!err)
745 			goto retry;
746 	}
747 
748 	if (err)
749 		goto err_out;
750 
751 	index = page->index - (map->m_la >> PAGE_SHIFT);
752 
753 	z_erofs_onlinepage_fixup(page, index, true);
754 
755 	/* bump up the number of spiltted parts of a page */
756 	++spiltted;
757 	/* also update nr_pages */
758 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
759 next_part:
760 	/* can be used for verification */
761 	map->m_llen = offset + cur - map->m_la;
762 
763 	end = cur;
764 	if (end > 0)
765 		goto repeat;
766 
767 out:
768 	z_erofs_onlinepage_endio(page);
769 
770 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
771 		  __func__, page, spiltted, map->m_llen);
772 	return err;
773 
774 	/* if some error occurred while processing this page */
775 err_out:
776 	SetPageError(page);
777 	goto out;
778 }
779 
780 static void z_erofs_decompressqueue_work(struct work_struct *work);
781 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
782 				       bool sync, int bios)
783 {
784 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
785 
786 	/* wake up the caller thread for sync decompression */
787 	if (sync) {
788 		unsigned long flags;
789 
790 		spin_lock_irqsave(&io->u.wait.lock, flags);
791 		if (!atomic_add_return(bios, &io->pending_bios))
792 			wake_up_locked(&io->u.wait);
793 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
794 		return;
795 	}
796 
797 	if (atomic_add_return(bios, &io->pending_bios))
798 		return;
799 	/* Use workqueue and sync decompression for atomic contexts only */
800 	if (in_atomic() || irqs_disabled()) {
801 		queue_work(z_erofs_workqueue, &io->u.work);
802 		sbi->ctx.readahead_sync_decompress = true;
803 		return;
804 	}
805 	z_erofs_decompressqueue_work(&io->u.work);
806 }
807 
808 static bool z_erofs_page_is_invalidated(struct page *page)
809 {
810 	return !page->mapping && !z_erofs_is_shortlived_page(page);
811 }
812 
813 static void z_erofs_decompressqueue_endio(struct bio *bio)
814 {
815 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
816 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
817 	blk_status_t err = bio->bi_status;
818 	struct bio_vec *bvec;
819 	struct bvec_iter_all iter_all;
820 
821 	bio_for_each_segment_all(bvec, bio, iter_all) {
822 		struct page *page = bvec->bv_page;
823 
824 		DBG_BUGON(PageUptodate(page));
825 		DBG_BUGON(z_erofs_page_is_invalidated(page));
826 
827 		if (err)
828 			SetPageError(page);
829 
830 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
831 			if (!err)
832 				SetPageUptodate(page);
833 			unlock_page(page);
834 		}
835 	}
836 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
837 	bio_put(bio);
838 }
839 
840 static int z_erofs_decompress_pcluster(struct super_block *sb,
841 				       struct z_erofs_pcluster *pcl,
842 				       struct list_head *pagepool)
843 {
844 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
845 	struct z_erofs_pagevec_ctor ctor;
846 	unsigned int i, inputsize, outputsize, llen, nr_pages;
847 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
848 	struct page **pages, **compressed_pages, *page;
849 
850 	enum z_erofs_page_type page_type;
851 	bool overlapped, partial;
852 	struct z_erofs_collection *cl;
853 	int err;
854 
855 	might_sleep();
856 	cl = z_erofs_primarycollection(pcl);
857 	DBG_BUGON(!READ_ONCE(cl->nr_pages));
858 
859 	mutex_lock(&cl->lock);
860 	nr_pages = cl->nr_pages;
861 
862 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
863 		pages = pages_onstack;
864 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
865 		   mutex_trylock(&z_pagemap_global_lock)) {
866 		pages = z_pagemap_global;
867 	} else {
868 		gfp_t gfp_flags = GFP_KERNEL;
869 
870 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
871 			gfp_flags |= __GFP_NOFAIL;
872 
873 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
874 				       gfp_flags);
875 
876 		/* fallback to global pagemap for the lowmem scenario */
877 		if (!pages) {
878 			mutex_lock(&z_pagemap_global_lock);
879 			pages = z_pagemap_global;
880 		}
881 	}
882 
883 	for (i = 0; i < nr_pages; ++i)
884 		pages[i] = NULL;
885 
886 	err = 0;
887 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
888 				  cl->pagevec, 0);
889 
890 	for (i = 0; i < cl->vcnt; ++i) {
891 		unsigned int pagenr;
892 
893 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
894 
895 		/* all pages in pagevec ought to be valid */
896 		DBG_BUGON(!page);
897 		DBG_BUGON(z_erofs_page_is_invalidated(page));
898 
899 		if (z_erofs_put_shortlivedpage(pagepool, page))
900 			continue;
901 
902 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
903 			pagenr = 0;
904 		else
905 			pagenr = z_erofs_onlinepage_index(page);
906 
907 		DBG_BUGON(pagenr >= nr_pages);
908 
909 		/*
910 		 * currently EROFS doesn't support multiref(dedup),
911 		 * so here erroring out one multiref page.
912 		 */
913 		if (pages[pagenr]) {
914 			DBG_BUGON(1);
915 			SetPageError(pages[pagenr]);
916 			z_erofs_onlinepage_endio(pages[pagenr]);
917 			err = -EFSCORRUPTED;
918 		}
919 		pages[pagenr] = page;
920 	}
921 	z_erofs_pagevec_ctor_exit(&ctor, true);
922 
923 	overlapped = false;
924 	compressed_pages = pcl->compressed_pages;
925 
926 	for (i = 0; i < pcl->pclusterpages; ++i) {
927 		unsigned int pagenr;
928 
929 		page = compressed_pages[i];
930 
931 		/* all compressed pages ought to be valid */
932 		DBG_BUGON(!page);
933 		DBG_BUGON(z_erofs_page_is_invalidated(page));
934 
935 		if (!z_erofs_is_shortlived_page(page)) {
936 			if (erofs_page_is_managed(sbi, page)) {
937 				if (!PageUptodate(page))
938 					err = -EIO;
939 				continue;
940 			}
941 
942 			/*
943 			 * only if non-head page can be selected
944 			 * for inplace decompression
945 			 */
946 			pagenr = z_erofs_onlinepage_index(page);
947 
948 			DBG_BUGON(pagenr >= nr_pages);
949 			if (pages[pagenr]) {
950 				DBG_BUGON(1);
951 				SetPageError(pages[pagenr]);
952 				z_erofs_onlinepage_endio(pages[pagenr]);
953 				err = -EFSCORRUPTED;
954 			}
955 			pages[pagenr] = page;
956 
957 			overlapped = true;
958 		}
959 
960 		/* PG_error needs checking for all non-managed pages */
961 		if (PageError(page)) {
962 			DBG_BUGON(PageUptodate(page));
963 			err = -EIO;
964 		}
965 	}
966 
967 	if (err)
968 		goto out;
969 
970 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
971 	if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
972 		outputsize = llen;
973 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
974 	} else {
975 		outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
976 		partial = true;
977 	}
978 
979 	inputsize = pcl->pclusterpages * PAGE_SIZE;
980 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
981 					.sb = sb,
982 					.in = compressed_pages,
983 					.out = pages,
984 					.pageofs_out = cl->pageofs,
985 					.inputsize = inputsize,
986 					.outputsize = outputsize,
987 					.alg = pcl->algorithmformat,
988 					.inplace_io = overlapped,
989 					.partial_decoding = partial
990 				 }, pagepool);
991 
992 out:
993 	/* must handle all compressed pages before ending pages */
994 	for (i = 0; i < pcl->pclusterpages; ++i) {
995 		page = compressed_pages[i];
996 
997 		if (erofs_page_is_managed(sbi, page))
998 			continue;
999 
1000 		/* recycle all individual short-lived pages */
1001 		(void)z_erofs_put_shortlivedpage(pagepool, page);
1002 
1003 		WRITE_ONCE(compressed_pages[i], NULL);
1004 	}
1005 
1006 	for (i = 0; i < nr_pages; ++i) {
1007 		page = pages[i];
1008 		if (!page)
1009 			continue;
1010 
1011 		DBG_BUGON(z_erofs_page_is_invalidated(page));
1012 
1013 		/* recycle all individual short-lived pages */
1014 		if (z_erofs_put_shortlivedpage(pagepool, page))
1015 			continue;
1016 
1017 		if (err < 0)
1018 			SetPageError(page);
1019 
1020 		z_erofs_onlinepage_endio(page);
1021 	}
1022 
1023 	if (pages == z_pagemap_global)
1024 		mutex_unlock(&z_pagemap_global_lock);
1025 	else if (pages != pages_onstack)
1026 		kvfree(pages);
1027 
1028 	cl->nr_pages = 0;
1029 	cl->vcnt = 0;
1030 
1031 	/* all cl locks MUST be taken before the following line */
1032 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1033 
1034 	/* all cl locks SHOULD be released right now */
1035 	mutex_unlock(&cl->lock);
1036 
1037 	z_erofs_collection_put(cl);
1038 	return err;
1039 }
1040 
1041 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1042 				     struct list_head *pagepool)
1043 {
1044 	z_erofs_next_pcluster_t owned = io->head;
1045 
1046 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1047 		struct z_erofs_pcluster *pcl;
1048 
1049 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1050 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
1051 
1052 		/* no possible that 'owned' equals NULL */
1053 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1054 
1055 		pcl = container_of(owned, struct z_erofs_pcluster, next);
1056 		owned = READ_ONCE(pcl->next);
1057 
1058 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
1059 	}
1060 }
1061 
1062 static void z_erofs_decompressqueue_work(struct work_struct *work)
1063 {
1064 	struct z_erofs_decompressqueue *bgq =
1065 		container_of(work, struct z_erofs_decompressqueue, u.work);
1066 	LIST_HEAD(pagepool);
1067 
1068 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1069 	z_erofs_decompress_queue(bgq, &pagepool);
1070 
1071 	put_pages_list(&pagepool);
1072 	kvfree(bgq);
1073 }
1074 
1075 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1076 					       unsigned int nr,
1077 					       struct list_head *pagepool,
1078 					       struct address_space *mc,
1079 					       gfp_t gfp)
1080 {
1081 	const pgoff_t index = pcl->obj.index;
1082 	bool tocache = false;
1083 
1084 	struct address_space *mapping;
1085 	struct page *oldpage, *page;
1086 
1087 	compressed_page_t t;
1088 	int justfound;
1089 
1090 repeat:
1091 	page = READ_ONCE(pcl->compressed_pages[nr]);
1092 	oldpage = page;
1093 
1094 	if (!page)
1095 		goto out_allocpage;
1096 
1097 	/*
1098 	 * the cached page has not been allocated and
1099 	 * an placeholder is out there, prepare it now.
1100 	 */
1101 	if (page == PAGE_UNALLOCATED) {
1102 		tocache = true;
1103 		goto out_allocpage;
1104 	}
1105 
1106 	/* process the target tagged pointer */
1107 	t = tagptr_init(compressed_page_t, page);
1108 	justfound = tagptr_unfold_tags(t);
1109 	page = tagptr_unfold_ptr(t);
1110 
1111 	/*
1112 	 * preallocated cached pages, which is used to avoid direct reclaim
1113 	 * otherwise, it will go inplace I/O path instead.
1114 	 */
1115 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1116 		WRITE_ONCE(pcl->compressed_pages[nr], page);
1117 		set_page_private(page, 0);
1118 		tocache = true;
1119 		goto out_tocache;
1120 	}
1121 	mapping = READ_ONCE(page->mapping);
1122 
1123 	/*
1124 	 * file-backed online pages in plcuster are all locked steady,
1125 	 * therefore it is impossible for `mapping' to be NULL.
1126 	 */
1127 	if (mapping && mapping != mc)
1128 		/* ought to be unmanaged pages */
1129 		goto out;
1130 
1131 	/* directly return for shortlived page as well */
1132 	if (z_erofs_is_shortlived_page(page))
1133 		goto out;
1134 
1135 	lock_page(page);
1136 
1137 	/* only true if page reclaim goes wrong, should never happen */
1138 	DBG_BUGON(justfound && PagePrivate(page));
1139 
1140 	/* the page is still in manage cache */
1141 	if (page->mapping == mc) {
1142 		WRITE_ONCE(pcl->compressed_pages[nr], page);
1143 
1144 		ClearPageError(page);
1145 		if (!PagePrivate(page)) {
1146 			/*
1147 			 * impossible to be !PagePrivate(page) for
1148 			 * the current restriction as well if
1149 			 * the page is already in compressed_pages[].
1150 			 */
1151 			DBG_BUGON(!justfound);
1152 
1153 			justfound = 0;
1154 			set_page_private(page, (unsigned long)pcl);
1155 			SetPagePrivate(page);
1156 		}
1157 
1158 		/* no need to submit io if it is already up-to-date */
1159 		if (PageUptodate(page)) {
1160 			unlock_page(page);
1161 			page = NULL;
1162 		}
1163 		goto out;
1164 	}
1165 
1166 	/*
1167 	 * the managed page has been truncated, it's unsafe to
1168 	 * reuse this one, let's allocate a new cache-managed page.
1169 	 */
1170 	DBG_BUGON(page->mapping);
1171 	DBG_BUGON(!justfound);
1172 
1173 	tocache = true;
1174 	unlock_page(page);
1175 	put_page(page);
1176 out_allocpage:
1177 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1178 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1179 		list_add(&page->lru, pagepool);
1180 		cond_resched();
1181 		goto repeat;
1182 	}
1183 out_tocache:
1184 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1185 		/* turn into temporary page if fails (1 ref) */
1186 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1187 		goto out;
1188 	}
1189 	attach_page_private(page, pcl);
1190 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1191 	put_page(page);
1192 
1193 out:	/* the only exit (for tracing and debugging) */
1194 	return page;
1195 }
1196 
1197 static struct z_erofs_decompressqueue *
1198 jobqueue_init(struct super_block *sb,
1199 	      struct z_erofs_decompressqueue *fgq, bool *fg)
1200 {
1201 	struct z_erofs_decompressqueue *q;
1202 
1203 	if (fg && !*fg) {
1204 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1205 		if (!q) {
1206 			*fg = true;
1207 			goto fg_out;
1208 		}
1209 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1210 	} else {
1211 fg_out:
1212 		q = fgq;
1213 		init_waitqueue_head(&fgq->u.wait);
1214 		atomic_set(&fgq->pending_bios, 0);
1215 	}
1216 	q->sb = sb;
1217 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1218 	return q;
1219 }
1220 
1221 /* define decompression jobqueue types */
1222 enum {
1223 	JQ_BYPASS,
1224 	JQ_SUBMIT,
1225 	NR_JOBQUEUES,
1226 };
1227 
1228 static void *jobqueueset_init(struct super_block *sb,
1229 			      struct z_erofs_decompressqueue *q[],
1230 			      struct z_erofs_decompressqueue *fgq, bool *fg)
1231 {
1232 	/*
1233 	 * if managed cache is enabled, bypass jobqueue is needed,
1234 	 * no need to read from device for all pclusters in this queue.
1235 	 */
1236 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1237 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1238 
1239 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1240 }
1241 
1242 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1243 				    z_erofs_next_pcluster_t qtail[],
1244 				    z_erofs_next_pcluster_t owned_head)
1245 {
1246 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1247 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1248 
1249 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1250 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1251 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1252 
1253 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1254 
1255 	WRITE_ONCE(*submit_qtail, owned_head);
1256 	WRITE_ONCE(*bypass_qtail, &pcl->next);
1257 
1258 	qtail[JQ_BYPASS] = &pcl->next;
1259 }
1260 
1261 static void z_erofs_submit_queue(struct super_block *sb,
1262 				 struct z_erofs_decompress_frontend *f,
1263 				 struct list_head *pagepool,
1264 				 struct z_erofs_decompressqueue *fgq,
1265 				 bool *force_fg)
1266 {
1267 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
1268 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1269 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1270 	void *bi_private;
1271 	z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1272 	/* since bio will be NULL, no need to initialize last_index */
1273 	pgoff_t last_index;
1274 	unsigned int nr_bios = 0;
1275 	struct bio *bio = NULL;
1276 
1277 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1278 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1279 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1280 
1281 	/* by default, all need io submission */
1282 	q[JQ_SUBMIT]->head = owned_head;
1283 
1284 	do {
1285 		struct z_erofs_pcluster *pcl;
1286 		pgoff_t cur, end;
1287 		unsigned int i = 0;
1288 		bool bypass = true;
1289 
1290 		/* no possible 'owned_head' equals the following */
1291 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1292 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1293 
1294 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1295 
1296 		cur = pcl->obj.index;
1297 		end = cur + pcl->pclusterpages;
1298 
1299 		/* close the main owned chain at first */
1300 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1301 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1302 
1303 		do {
1304 			struct page *page;
1305 
1306 			page = pickup_page_for_submission(pcl, i++, pagepool,
1307 							  MNGD_MAPPING(sbi),
1308 							  GFP_NOFS);
1309 			if (!page)
1310 				continue;
1311 
1312 			if (bio && cur != last_index + 1) {
1313 submit_bio_retry:
1314 				submit_bio(bio);
1315 				bio = NULL;
1316 			}
1317 
1318 			if (!bio) {
1319 				bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS);
1320 
1321 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1322 				bio_set_dev(bio, sb->s_bdev);
1323 				bio->bi_iter.bi_sector = (sector_t)cur <<
1324 					LOG_SECTORS_PER_BLOCK;
1325 				bio->bi_private = bi_private;
1326 				bio->bi_opf = REQ_OP_READ;
1327 				if (f->readahead)
1328 					bio->bi_opf |= REQ_RAHEAD;
1329 				++nr_bios;
1330 			}
1331 
1332 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1333 				goto submit_bio_retry;
1334 
1335 			last_index = cur;
1336 			bypass = false;
1337 		} while (++cur < end);
1338 
1339 		if (!bypass)
1340 			qtail[JQ_SUBMIT] = &pcl->next;
1341 		else
1342 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1343 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1344 
1345 	if (bio)
1346 		submit_bio(bio);
1347 
1348 	/*
1349 	 * although background is preferred, no one is pending for submission.
1350 	 * don't issue workqueue for decompression but drop it directly instead.
1351 	 */
1352 	if (!*force_fg && !nr_bios) {
1353 		kvfree(q[JQ_SUBMIT]);
1354 		return;
1355 	}
1356 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1357 }
1358 
1359 static void z_erofs_runqueue(struct super_block *sb,
1360 			     struct z_erofs_decompress_frontend *f,
1361 			     struct list_head *pagepool, bool force_fg)
1362 {
1363 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1364 
1365 	if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
1366 		return;
1367 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
1368 
1369 	/* handle bypass queue (no i/o pclusters) immediately */
1370 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1371 
1372 	if (!force_fg)
1373 		return;
1374 
1375 	/* wait until all bios are completed */
1376 	io_wait_event(io[JQ_SUBMIT].u.wait,
1377 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
1378 
1379 	/* handle synchronous decompress queue in the caller context */
1380 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1381 }
1382 
1383 static int z_erofs_readpage(struct file *file, struct page *page)
1384 {
1385 	struct inode *const inode = page->mapping->host;
1386 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1387 	int err;
1388 	LIST_HEAD(pagepool);
1389 
1390 	trace_erofs_readpage(page, false);
1391 
1392 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1393 
1394 	err = z_erofs_do_read_page(&f, page, &pagepool);
1395 	(void)z_erofs_collector_end(&f.clt);
1396 
1397 	/* if some compressed cluster ready, need submit them anyway */
1398 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
1399 
1400 	if (err)
1401 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1402 
1403 	if (f.map.mpage)
1404 		put_page(f.map.mpage);
1405 
1406 	/* clean up the remaining free pages */
1407 	put_pages_list(&pagepool);
1408 	return err;
1409 }
1410 
1411 static void z_erofs_readahead(struct readahead_control *rac)
1412 {
1413 	struct inode *const inode = rac->mapping->host;
1414 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1415 
1416 	unsigned int nr_pages = readahead_count(rac);
1417 	bool sync = (sbi->ctx.readahead_sync_decompress &&
1418 			nr_pages <= sbi->ctx.max_sync_decompress_pages);
1419 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1420 	struct page *page, *head = NULL;
1421 	LIST_HEAD(pagepool);
1422 
1423 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1424 
1425 	f.readahead = true;
1426 	f.headoffset = readahead_pos(rac);
1427 
1428 	while ((page = readahead_page(rac))) {
1429 		prefetchw(&page->flags);
1430 
1431 		/*
1432 		 * A pure asynchronous readahead is indicated if
1433 		 * a PG_readahead marked page is hitted at first.
1434 		 * Let's also do asynchronous decompression for this case.
1435 		 */
1436 		sync &= !(PageReadahead(page) && !head);
1437 
1438 		set_page_private(page, (unsigned long)head);
1439 		head = page;
1440 	}
1441 
1442 	while (head) {
1443 		struct page *page = head;
1444 		int err;
1445 
1446 		/* traversal in reverse order */
1447 		head = (void *)page_private(page);
1448 
1449 		err = z_erofs_do_read_page(&f, page, &pagepool);
1450 		if (err)
1451 			erofs_err(inode->i_sb,
1452 				  "readahead error at page %lu @ nid %llu",
1453 				  page->index, EROFS_I(inode)->nid);
1454 		put_page(page);
1455 	}
1456 
1457 	(void)z_erofs_collector_end(&f.clt);
1458 
1459 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
1460 
1461 	if (f.map.mpage)
1462 		put_page(f.map.mpage);
1463 
1464 	/* clean up the remaining free pages */
1465 	put_pages_list(&pagepool);
1466 }
1467 
1468 const struct address_space_operations z_erofs_aops = {
1469 	.readpage = z_erofs_readpage,
1470 	.readahead = z_erofs_readahead,
1471 };
1472 
1473