xref: /openbmc/linux/fs/erofs/zdata.c (revision 9f6cc76e)
147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only
247e4937aSGao Xiang /*
347e4937aSGao Xiang  * Copyright (C) 2018 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov  *             https://www.huawei.com/
547e4937aSGao Xiang  * Created by Gao Xiang <gaoxiang25@huawei.com>
647e4937aSGao Xiang  */
747e4937aSGao Xiang #include "zdata.h"
847e4937aSGao Xiang #include "compress.h"
947e4937aSGao Xiang #include <linux/prefetch.h>
1047e4937aSGao Xiang 
1147e4937aSGao Xiang #include <trace/events/erofs.h>
1247e4937aSGao Xiang 
1347e4937aSGao Xiang /*
14*9f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
15*9f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
16*9f6cc76eSGao Xiang  */
17*9f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
18*9f6cc76eSGao Xiang 	struct kmem_cache *slab;
19*9f6cc76eSGao Xiang 	unsigned int maxpages;
20*9f6cc76eSGao Xiang 	char name[48];
21*9f6cc76eSGao Xiang };
22*9f6cc76eSGao Xiang 
23*9f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
24*9f6cc76eSGao Xiang 
25*9f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
26*9f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
27*9f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
28*9f6cc76eSGao Xiang };
29*9f6cc76eSGao Xiang 
30*9f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
31*9f6cc76eSGao Xiang {
32*9f6cc76eSGao Xiang 	int i;
33*9f6cc76eSGao Xiang 
34*9f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
35*9f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
36*9f6cc76eSGao Xiang 			continue;
37*9f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
38*9f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
39*9f6cc76eSGao Xiang 	}
40*9f6cc76eSGao Xiang }
41*9f6cc76eSGao Xiang 
42*9f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
43*9f6cc76eSGao Xiang {
44*9f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
45*9f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
46*9f6cc76eSGao Xiang 	unsigned int size;
47*9f6cc76eSGao Xiang 
48*9f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
49*9f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
50*9f6cc76eSGao Xiang 		size = struct_size(a, compressed_pages, pcs->maxpages);
51*9f6cc76eSGao Xiang 
52*9f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
53*9f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
54*9f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
55*9f6cc76eSGao Xiang 		if (pcs->slab)
56*9f6cc76eSGao Xiang 			continue;
57*9f6cc76eSGao Xiang 
58*9f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
59*9f6cc76eSGao Xiang 		return -ENOMEM;
60*9f6cc76eSGao Xiang 	}
61*9f6cc76eSGao Xiang 	return 0;
62*9f6cc76eSGao Xiang }
63*9f6cc76eSGao Xiang 
64*9f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
65*9f6cc76eSGao Xiang {
66*9f6cc76eSGao Xiang 	int i;
67*9f6cc76eSGao Xiang 
68*9f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
69*9f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
70*9f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
71*9f6cc76eSGao Xiang 
72*9f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
73*9f6cc76eSGao Xiang 			continue;
74*9f6cc76eSGao Xiang 
75*9f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
76*9f6cc76eSGao Xiang 		if (!pcl)
77*9f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
78*9f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
79*9f6cc76eSGao Xiang 		return pcl;
80*9f6cc76eSGao Xiang 	}
81*9f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
82*9f6cc76eSGao Xiang }
83*9f6cc76eSGao Xiang 
84*9f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
85*9f6cc76eSGao Xiang {
86*9f6cc76eSGao Xiang 	int i;
87*9f6cc76eSGao Xiang 
88*9f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
89*9f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
90*9f6cc76eSGao Xiang 
91*9f6cc76eSGao Xiang 		if (pcl->pclusterpages > pcs->maxpages)
92*9f6cc76eSGao Xiang 			continue;
93*9f6cc76eSGao Xiang 
94*9f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
95*9f6cc76eSGao Xiang 		return;
96*9f6cc76eSGao Xiang 	}
97*9f6cc76eSGao Xiang 	DBG_BUGON(1);
98*9f6cc76eSGao Xiang }
99*9f6cc76eSGao Xiang 
100*9f6cc76eSGao Xiang /*
10147e4937aSGao Xiang  * a compressed_pages[] placeholder in order to avoid
10247e4937aSGao Xiang  * being filled with file pages for in-place decompression.
10347e4937aSGao Xiang  */
10447e4937aSGao Xiang #define PAGE_UNALLOCATED     ((void *)0x5F0E4B1D)
10547e4937aSGao Xiang 
10647e4937aSGao Xiang /* how to allocate cached pages for a pcluster */
10747e4937aSGao Xiang enum z_erofs_cache_alloctype {
10847e4937aSGao Xiang 	DONTALLOC,	/* don't allocate any cached pages */
10947e4937aSGao Xiang 	DELAYEDALLOC,	/* delayed allocation (at the time of submitting io) */
1101825c8d7SGao Xiang 	/*
1111825c8d7SGao Xiang 	 * try to use cached I/O if page allocation succeeds or fallback
1121825c8d7SGao Xiang 	 * to in-place I/O instead to avoid any direct reclaim.
1131825c8d7SGao Xiang 	 */
1141825c8d7SGao Xiang 	TRYALLOC,
11547e4937aSGao Xiang };
11647e4937aSGao Xiang 
11747e4937aSGao Xiang /*
11847e4937aSGao Xiang  * tagged pointer with 1-bit tag for all compressed pages
11947e4937aSGao Xiang  * tag 0 - the page is just found with an extra page reference
12047e4937aSGao Xiang  */
12147e4937aSGao Xiang typedef tagptr1_t compressed_page_t;
12247e4937aSGao Xiang 
12347e4937aSGao Xiang #define tag_compressed_page_justfound(page) \
12447e4937aSGao Xiang 	tagptr_fold(compressed_page_t, page, 1)
12547e4937aSGao Xiang 
12647e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
12747e4937aSGao Xiang 
12847e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
12947e4937aSGao Xiang {
13047e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
131*9f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
13247e4937aSGao Xiang }
13347e4937aSGao Xiang 
13499634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
13547e4937aSGao Xiang {
13647e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
13747e4937aSGao Xiang 
13847e4937aSGao Xiang 	/*
13947e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
14047e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
14147e4937aSGao Xiang 	 */
1420e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
1430e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
14447e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
14547e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
14647e4937aSGao Xiang }
14747e4937aSGao Xiang 
14847e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
14947e4937aSGao Xiang {
150*9f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
15147e4937aSGao Xiang 
152*9f6cc76eSGao Xiang 	if (err)
153*9f6cc76eSGao Xiang 		return err;
154*9f6cc76eSGao Xiang 	err = z_erofs_init_workqueue();
155*9f6cc76eSGao Xiang 	if (err)
156*9f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
157*9f6cc76eSGao Xiang 	return err;
15847e4937aSGao Xiang }
15947e4937aSGao Xiang 
16047e4937aSGao Xiang enum z_erofs_collectmode {
16147e4937aSGao Xiang 	COLLECT_SECONDARY,
16247e4937aSGao Xiang 	COLLECT_PRIMARY,
16347e4937aSGao Xiang 	/*
16447e4937aSGao Xiang 	 * The current collection was the tail of an exist chain, in addition
16547e4937aSGao Xiang 	 * that the previous processed chained collections are all decided to
16647e4937aSGao Xiang 	 * be hooked up to it.
16747e4937aSGao Xiang 	 * A new chain will be created for the remaining collections which are
16847e4937aSGao Xiang 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
16947e4937aSGao Xiang 	 * the next collection cannot reuse the whole page safely in
17047e4937aSGao Xiang 	 * the following scenario:
17147e4937aSGao Xiang 	 *  ________________________________________________________________
17247e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
17347e4937aSGao Xiang 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
17447e4937aSGao Xiang 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
17547e4937aSGao Xiang 	 */
17647e4937aSGao Xiang 	COLLECT_PRIMARY_HOOKED,
1770b964600SGao Xiang 	/*
1780b964600SGao Xiang 	 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
1790b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
1800b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
1810b964600SGao Xiang 	 * pagevec) since it can be directly decoded without I/O submission.
1820b964600SGao Xiang 	 */
18347e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
18447e4937aSGao Xiang 	/*
18547e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
18647e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
18747e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
18847e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
18947e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
19047e4937aSGao Xiang 	 * illustrated below:
19147e4937aSGao Xiang 	 *  ________________________________________________________________
19247e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
19347e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
19447e4937aSGao Xiang 	 * |  PRIMARY_FOLLOWED or |                                        |
19547e4937aSGao Xiang 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
19647e4937aSGao Xiang 	 *
19747e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
19847e4937aSGao Xiang 	 */
19947e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED,
20047e4937aSGao Xiang };
20147e4937aSGao Xiang 
20247e4937aSGao Xiang struct z_erofs_collector {
20347e4937aSGao Xiang 	struct z_erofs_pagevec_ctor vector;
20447e4937aSGao Xiang 
20547e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
20647e4937aSGao Xiang 	struct z_erofs_collection *cl;
20747e4937aSGao Xiang 	struct page **compressedpages;
20847e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
20947e4937aSGao Xiang 
21047e4937aSGao Xiang 	enum z_erofs_collectmode mode;
21147e4937aSGao Xiang };
21247e4937aSGao Xiang 
21347e4937aSGao Xiang struct z_erofs_decompress_frontend {
21447e4937aSGao Xiang 	struct inode *const inode;
21547e4937aSGao Xiang 
21647e4937aSGao Xiang 	struct z_erofs_collector clt;
21747e4937aSGao Xiang 	struct erofs_map_blocks map;
21847e4937aSGao Xiang 
2196ea5aad3SGao Xiang 	bool readahead;
22047e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
22147e4937aSGao Xiang 	bool backmost;
22247e4937aSGao Xiang 	erofs_off_t headoffset;
22347e4937aSGao Xiang };
22447e4937aSGao Xiang 
22547e4937aSGao Xiang #define COLLECTOR_INIT() { \
22647e4937aSGao Xiang 	.owned_head = Z_EROFS_PCLUSTER_TAIL, \
22747e4937aSGao Xiang 	.mode = COLLECT_PRIMARY_FOLLOWED }
22847e4937aSGao Xiang 
22947e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
23047e4937aSGao Xiang 	.inode = __i, .clt = COLLECTOR_INIT(), \
23147e4937aSGao Xiang 	.backmost = true, }
23247e4937aSGao Xiang 
23347e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
23447e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
23547e4937aSGao Xiang 
23647e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt,
23747e4937aSGao Xiang 				     struct address_space *mc,
2381825c8d7SGao Xiang 				     enum z_erofs_cache_alloctype type,
2391825c8d7SGao Xiang 				     struct list_head *pagepool)
24047e4937aSGao Xiang {
24147e4937aSGao Xiang 	const struct z_erofs_pcluster *pcl = clt->pcl;
24247e4937aSGao Xiang 	struct page **pages = clt->compressedpages;
24347e4937aSGao Xiang 	pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
24447e4937aSGao Xiang 	bool standalone = true;
2451825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
2461825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
24747e4937aSGao Xiang 
24847e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
24947e4937aSGao Xiang 		return;
25047e4937aSGao Xiang 
251*9f6cc76eSGao Xiang 	for (; pages < pcl->compressed_pages + pcl->pclusterpages; ++pages) {
25247e4937aSGao Xiang 		struct page *page;
25347e4937aSGao Xiang 		compressed_page_t t;
2541825c8d7SGao Xiang 		struct page *newpage = NULL;
25547e4937aSGao Xiang 
25647e4937aSGao Xiang 		/* the compressed page was loaded before */
25747e4937aSGao Xiang 		if (READ_ONCE(*pages))
25847e4937aSGao Xiang 			continue;
25947e4937aSGao Xiang 
26047e4937aSGao Xiang 		page = find_get_page(mc, index);
26147e4937aSGao Xiang 
26247e4937aSGao Xiang 		if (page) {
26347e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
2640b964600SGao Xiang 		} else {
2650b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
2660b964600SGao Xiang 			standalone = false;
2670b964600SGao Xiang 			switch (type) {
2680b964600SGao Xiang 			case DELAYEDALLOC:
2690b964600SGao Xiang 				t = tagptr_init(compressed_page_t,
2700b964600SGao Xiang 						PAGE_UNALLOCATED);
2710b964600SGao Xiang 				break;
2720b964600SGao Xiang 			case TRYALLOC:
2731825c8d7SGao Xiang 				newpage = erofs_allocpage(pagepool, gfp);
2741825c8d7SGao Xiang 				if (!newpage)
27547e4937aSGao Xiang 					continue;
2760b964600SGao Xiang 				set_page_private(newpage,
2770b964600SGao Xiang 						 Z_EROFS_PREALLOCATED_PAGE);
2780b964600SGao Xiang 				t = tag_compressed_page_justfound(newpage);
2790b964600SGao Xiang 				break;
2800b964600SGao Xiang 			default:        /* DONTALLOC */
2810b964600SGao Xiang 				continue;
2820b964600SGao Xiang 			}
28347e4937aSGao Xiang 		}
28447e4937aSGao Xiang 
28547e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
28647e4937aSGao Xiang 			continue;
28747e4937aSGao Xiang 
2881825c8d7SGao Xiang 		if (page) {
28947e4937aSGao Xiang 			put_page(page);
2901825c8d7SGao Xiang 		} else if (newpage) {
2911825c8d7SGao Xiang 			set_page_private(newpage, 0);
2921825c8d7SGao Xiang 			list_add(&newpage->lru, pagepool);
2931825c8d7SGao Xiang 		}
29447e4937aSGao Xiang 	}
29547e4937aSGao Xiang 
2960b964600SGao Xiang 	/*
2970b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
2980b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
2990b964600SGao Xiang 	 */
3000b964600SGao Xiang 	if (standalone)
30147e4937aSGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
30247e4937aSGao Xiang }
30347e4937aSGao Xiang 
30447e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
30547e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
30647e4937aSGao Xiang 				       struct erofs_workgroup *grp)
30747e4937aSGao Xiang {
30847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
30947e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
31047e4937aSGao Xiang 	struct address_space *const mapping = MNGD_MAPPING(sbi);
31147e4937aSGao Xiang 	int i;
31247e4937aSGao Xiang 
31347e4937aSGao Xiang 	/*
31447e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
31547e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
31647e4937aSGao Xiang 	 */
317*9f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
31847e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
31947e4937aSGao Xiang 
32047e4937aSGao Xiang 		if (!page)
32147e4937aSGao Xiang 			continue;
32247e4937aSGao Xiang 
32347e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
32447e4937aSGao Xiang 		if (!trylock_page(page))
32547e4937aSGao Xiang 			return -EBUSY;
32647e4937aSGao Xiang 
3278d8a09b0SGao Xiang 		if (page->mapping != mapping)
32847e4937aSGao Xiang 			continue;
32947e4937aSGao Xiang 
33047e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
33147e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
3326aaa7b06SGao Xiang 		detach_page_private(page);
33347e4937aSGao Xiang 		unlock_page(page);
33447e4937aSGao Xiang 	}
33547e4937aSGao Xiang 	return 0;
33647e4937aSGao Xiang }
33747e4937aSGao Xiang 
33847e4937aSGao Xiang int erofs_try_to_free_cached_page(struct address_space *mapping,
33947e4937aSGao Xiang 				  struct page *page)
34047e4937aSGao Xiang {
34147e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
34247e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
34347e4937aSGao Xiang 
34447e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
34547e4937aSGao Xiang 		unsigned int i;
34647e4937aSGao Xiang 
347*9f6cc76eSGao Xiang 		for (i = 0; i < pcl->pclusterpages; ++i) {
34847e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
34947e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
35047e4937aSGao Xiang 				ret = 1;
35147e4937aSGao Xiang 				break;
35247e4937aSGao Xiang 			}
35347e4937aSGao Xiang 		}
35447e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
35547e4937aSGao Xiang 
3566aaa7b06SGao Xiang 		if (ret)
3576aaa7b06SGao Xiang 			detach_page_private(page);
35847e4937aSGao Xiang 	}
35947e4937aSGao Xiang 	return ret;
36047e4937aSGao Xiang }
36147e4937aSGao Xiang 
36247e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
36399634bf3SGao Xiang static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
36447e4937aSGao Xiang 					  struct page *page)
36547e4937aSGao Xiang {
36647e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = clt->pcl;
36747e4937aSGao Xiang 
368*9f6cc76eSGao Xiang 	while (clt->compressedpages <
369*9f6cc76eSGao Xiang 	       pcl->compressed_pages + pcl->pclusterpages) {
37047e4937aSGao Xiang 		if (!cmpxchg(clt->compressedpages++, NULL, page))
37147e4937aSGao Xiang 			return true;
37247e4937aSGao Xiang 	}
37347e4937aSGao Xiang 	return false;
37447e4937aSGao Xiang }
37547e4937aSGao Xiang 
37647e4937aSGao Xiang /* callers must be with collection lock held */
37747e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt,
37847e4937aSGao Xiang 			       struct page *page,
37947e4937aSGao Xiang 			       enum z_erofs_page_type type)
38047e4937aSGao Xiang {
38147e4937aSGao Xiang 	int ret;
38247e4937aSGao Xiang 	bool occupied;
38347e4937aSGao Xiang 
38447e4937aSGao Xiang 	/* give priority for inplaceio */
38547e4937aSGao Xiang 	if (clt->mode >= COLLECT_PRIMARY &&
38647e4937aSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
38799634bf3SGao Xiang 	    z_erofs_try_inplace_io(clt, page))
38847e4937aSGao Xiang 		return 0;
38947e4937aSGao Xiang 
39047e4937aSGao Xiang 	ret = z_erofs_pagevec_enqueue(&clt->vector,
39147e4937aSGao Xiang 				      page, type, &occupied);
39247e4937aSGao Xiang 	clt->cl->vcnt += (unsigned int)ret;
39347e4937aSGao Xiang 
39447e4937aSGao Xiang 	return ret ? 0 : -EAGAIN;
39547e4937aSGao Xiang }
39647e4937aSGao Xiang 
397473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
39847e4937aSGao Xiang {
399473e15b0SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
400473e15b0SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
40147e4937aSGao Xiang 
402473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
403473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
404473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
40547e4937aSGao Xiang 		*owned_head = &pcl->next;
406473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
407473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED;
408473e15b0SGao Xiang 		return;
409473e15b0SGao Xiang 	}
410473e15b0SGao Xiang 
41147e4937aSGao Xiang 	/*
412473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
413473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
41447e4937aSGao Xiang 	 */
41547e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
416473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
41747e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
418473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_HOOKED;
419473e15b0SGao Xiang 		clt->tailpcl = NULL;
420473e15b0SGao Xiang 		return;
42147e4937aSGao Xiang 	}
422473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
423473e15b0SGao Xiang 	clt->mode = COLLECT_PRIMARY;
42447e4937aSGao Xiang }
42547e4937aSGao Xiang 
4269e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
42747e4937aSGao Xiang 				     struct inode *inode,
42847e4937aSGao Xiang 				     struct erofs_map_blocks *map)
42947e4937aSGao Xiang {
43064094a04SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
43147e4937aSGao Xiang 	struct z_erofs_collection *cl;
43247e4937aSGao Xiang 	unsigned int length;
43347e4937aSGao Xiang 
43464094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
43547e4937aSGao Xiang 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
43647e4937aSGao Xiang 		DBG_BUGON(1);
4379e579fc1SGao Xiang 		return -EFSCORRUPTED;
43847e4937aSGao Xiang 	}
43947e4937aSGao Xiang 
44047e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
4418d8a09b0SGao Xiang 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
44247e4937aSGao Xiang 		DBG_BUGON(1);
4439e579fc1SGao Xiang 		return -EFSCORRUPTED;
44447e4937aSGao Xiang 	}
44547e4937aSGao Xiang 
44647e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
44747e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
44847e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
44947e4937aSGao Xiang 			DBG_BUGON(1);
4509e579fc1SGao Xiang 			return -EFSCORRUPTED;
45147e4937aSGao Xiang 		}
45247e4937aSGao Xiang 	} else {
45347e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
45447e4937aSGao Xiang 
45547e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
45647e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
45747e4937aSGao Xiang 
45847e4937aSGao Xiang 		while (llen > length &&
45947e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
46047e4937aSGao Xiang 			cpu_relax();
46147e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
46247e4937aSGao Xiang 		}
46347e4937aSGao Xiang 	}
46447e4937aSGao Xiang 	mutex_lock(&cl->lock);
46547e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
46647e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
46747e4937aSGao Xiang 		clt->tailpcl = pcl;
468473e15b0SGao Xiang 
469473e15b0SGao Xiang 	z_erofs_try_to_claim_pcluster(clt);
47047e4937aSGao Xiang 	clt->cl = cl;
4719e579fc1SGao Xiang 	return 0;
47247e4937aSGao Xiang }
47347e4937aSGao Xiang 
4749e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt,
47547e4937aSGao Xiang 				       struct inode *inode,
47647e4937aSGao Xiang 				       struct erofs_map_blocks *map)
47747e4937aSGao Xiang {
47847e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
47947e4937aSGao Xiang 	struct z_erofs_collection *cl;
48064094a04SGao Xiang 	struct erofs_workgroup *grp;
48147e4937aSGao Xiang 	int err;
48247e4937aSGao Xiang 
483*9f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
484*9f6cc76eSGao Xiang 	pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
485*9f6cc76eSGao Xiang 	if (IS_ERR(pcl))
486*9f6cc76eSGao Xiang 		return PTR_ERR(pcl);
48747e4937aSGao Xiang 
48864094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
48947e4937aSGao Xiang 	pcl->obj.index = map->m_pa >> PAGE_SHIFT;
49047e4937aSGao Xiang 
49147e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
49247e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
49347e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
49447e4937aSGao Xiang 
49547e4937aSGao Xiang 	if (map->m_flags & EROFS_MAP_ZIPPED)
49647e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
49747e4937aSGao Xiang 	else
49847e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
49947e4937aSGao Xiang 
50047e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
50147e4937aSGao Xiang 	pcl->next = clt->owned_head;
50247e4937aSGao Xiang 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
50347e4937aSGao Xiang 
50447e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
50547e4937aSGao Xiang 	cl->pageofs = map->m_la & ~PAGE_MASK;
50647e4937aSGao Xiang 
50747e4937aSGao Xiang 	/*
50847e4937aSGao Xiang 	 * lock all primary followed works before visible to others
50947e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
51047e4937aSGao Xiang 	 */
511*9f6cc76eSGao Xiang 	mutex_init(&cl->lock);
51264094a04SGao Xiang 	DBG_BUGON(!mutex_trylock(&cl->lock));
51347e4937aSGao Xiang 
51464094a04SGao Xiang 	grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
51564094a04SGao Xiang 	if (IS_ERR(grp)) {
51664094a04SGao Xiang 		err = PTR_ERR(grp);
51764094a04SGao Xiang 		goto err_out;
51864094a04SGao Xiang 	}
51964094a04SGao Xiang 
52064094a04SGao Xiang 	if (grp != &pcl->obj) {
52164094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
52264094a04SGao Xiang 		err = -EEXIST;
52364094a04SGao Xiang 		goto err_out;
52447e4937aSGao Xiang 	}
52547e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
52647e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
52747e4937aSGao Xiang 		clt->tailpcl = pcl;
52847e4937aSGao Xiang 	clt->owned_head = &pcl->next;
52947e4937aSGao Xiang 	clt->pcl = pcl;
53047e4937aSGao Xiang 	clt->cl = cl;
5319e579fc1SGao Xiang 	return 0;
53264094a04SGao Xiang 
53364094a04SGao Xiang err_out:
53464094a04SGao Xiang 	mutex_unlock(&cl->lock);
535*9f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
53664094a04SGao Xiang 	return err;
53747e4937aSGao Xiang }
53847e4937aSGao Xiang 
53947e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt,
54047e4937aSGao Xiang 				   struct inode *inode,
54147e4937aSGao Xiang 				   struct erofs_map_blocks *map)
54247e4937aSGao Xiang {
54364094a04SGao Xiang 	struct erofs_workgroup *grp;
5449e579fc1SGao Xiang 	int ret;
54547e4937aSGao Xiang 
54647e4937aSGao Xiang 	DBG_BUGON(clt->cl);
54747e4937aSGao Xiang 
54847e4937aSGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
54947e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
55047e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
55147e4937aSGao Xiang 
55247e4937aSGao Xiang 	if (!PAGE_ALIGNED(map->m_pa)) {
55347e4937aSGao Xiang 		DBG_BUGON(1);
55447e4937aSGao Xiang 		return -EINVAL;
55547e4937aSGao Xiang 	}
55647e4937aSGao Xiang 
55764094a04SGao Xiang 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
55864094a04SGao Xiang 	if (grp) {
55964094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
56064094a04SGao Xiang 	} else {
5619e579fc1SGao Xiang 		ret = z_erofs_register_collection(clt, inode, map);
56247e4937aSGao Xiang 
56364094a04SGao Xiang 		if (!ret)
56464094a04SGao Xiang 			goto out;
56564094a04SGao Xiang 		if (ret != -EEXIST)
5669e579fc1SGao Xiang 			return ret;
56764094a04SGao Xiang 	}
56847e4937aSGao Xiang 
56964094a04SGao Xiang 	ret = z_erofs_lookup_collection(clt, inode, map);
57064094a04SGao Xiang 	if (ret) {
57164094a04SGao Xiang 		erofs_workgroup_put(&clt->pcl->obj);
57264094a04SGao Xiang 		return ret;
57364094a04SGao Xiang 	}
57464094a04SGao Xiang 
57564094a04SGao Xiang out:
57647e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
5779e579fc1SGao Xiang 				  clt->cl->pagevec, clt->cl->vcnt);
57847e4937aSGao Xiang 
57947e4937aSGao Xiang 	clt->compressedpages = clt->pcl->compressed_pages;
58047e4937aSGao Xiang 	if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
581*9f6cc76eSGao Xiang 		clt->compressedpages += clt->pcl->pclusterpages;
58247e4937aSGao Xiang 	return 0;
58347e4937aSGao Xiang }
58447e4937aSGao Xiang 
58547e4937aSGao Xiang /*
58647e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
58747e4937aSGao Xiang  * only after a RCU grace period.
58847e4937aSGao Xiang  */
58947e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
59047e4937aSGao Xiang {
59147e4937aSGao Xiang 	struct z_erofs_collection *const cl =
59247e4937aSGao Xiang 		container_of(head, struct z_erofs_collection, rcu);
59347e4937aSGao Xiang 
594*9f6cc76eSGao Xiang 	z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
59547e4937aSGao Xiang 					   primary_collection));
59647e4937aSGao Xiang }
59747e4937aSGao Xiang 
59847e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
59947e4937aSGao Xiang {
60047e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
60147e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
60247e4937aSGao Xiang 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
60347e4937aSGao Xiang 
60447e4937aSGao Xiang 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
60547e4937aSGao Xiang }
60647e4937aSGao Xiang 
60747e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl)
60847e4937aSGao Xiang {
60947e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
61047e4937aSGao Xiang 		container_of(cl, struct z_erofs_pcluster, primary_collection);
61147e4937aSGao Xiang 
61247e4937aSGao Xiang 	erofs_workgroup_put(&pcl->obj);
61347e4937aSGao Xiang }
61447e4937aSGao Xiang 
61547e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt)
61647e4937aSGao Xiang {
61747e4937aSGao Xiang 	struct z_erofs_collection *cl = clt->cl;
61847e4937aSGao Xiang 
61947e4937aSGao Xiang 	if (!cl)
62047e4937aSGao Xiang 		return false;
62147e4937aSGao Xiang 
62247e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
62347e4937aSGao Xiang 	mutex_unlock(&cl->lock);
62447e4937aSGao Xiang 
62547e4937aSGao Xiang 	/*
62647e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
62747e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
62847e4937aSGao Xiang 	 */
62947e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
63047e4937aSGao Xiang 		z_erofs_collection_put(cl);
63147e4937aSGao Xiang 
63247e4937aSGao Xiang 	clt->cl = NULL;
63347e4937aSGao Xiang 	return true;
63447e4937aSGao Xiang }
63547e4937aSGao Xiang 
63647e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
63747e4937aSGao Xiang 				       unsigned int cachestrategy,
63847e4937aSGao Xiang 				       erofs_off_t la)
63947e4937aSGao Xiang {
64047e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
64147e4937aSGao Xiang 		return false;
64247e4937aSGao Xiang 
64347e4937aSGao Xiang 	if (fe->backmost)
64447e4937aSGao Xiang 		return true;
64547e4937aSGao Xiang 
64647e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
64747e4937aSGao Xiang 		la < fe->headoffset;
64847e4937aSGao Xiang }
64947e4937aSGao Xiang 
65047e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
6511825c8d7SGao Xiang 				struct page *page, struct list_head *pagepool)
65247e4937aSGao Xiang {
65347e4937aSGao Xiang 	struct inode *const inode = fe->inode;
654bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
65547e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
65647e4937aSGao Xiang 	struct z_erofs_collector *const clt = &fe->clt;
65747e4937aSGao Xiang 	const loff_t offset = page_offset(page);
658dc76ea8cSGao Xiang 	bool tight = true;
65947e4937aSGao Xiang 
66047e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
66147e4937aSGao Xiang 	enum z_erofs_page_type page_type;
66247e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
66347e4937aSGao Xiang 	int err = 0;
66447e4937aSGao Xiang 
66547e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
66647e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
66747e4937aSGao Xiang 
66847e4937aSGao Xiang 	spiltted = 0;
66947e4937aSGao Xiang 	end = PAGE_SIZE;
67047e4937aSGao Xiang repeat:
67147e4937aSGao Xiang 	cur = end - 1;
67247e4937aSGao Xiang 
67347e4937aSGao Xiang 	/* lucky, within the range of the current map_blocks */
67447e4937aSGao Xiang 	if (offset + cur >= map->m_la &&
67547e4937aSGao Xiang 	    offset + cur < map->m_la + map->m_llen) {
67647e4937aSGao Xiang 		/* didn't get a valid collection previously (very rare) */
67747e4937aSGao Xiang 		if (!clt->cl)
67847e4937aSGao Xiang 			goto restart_now;
67947e4937aSGao Xiang 		goto hitted;
68047e4937aSGao Xiang 	}
68147e4937aSGao Xiang 
68247e4937aSGao Xiang 	/* go ahead the next map_blocks */
6834f761fa2SGao Xiang 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
68447e4937aSGao Xiang 
68547e4937aSGao Xiang 	if (z_erofs_collector_end(clt))
68647e4937aSGao Xiang 		fe->backmost = false;
68747e4937aSGao Xiang 
68847e4937aSGao Xiang 	map->m_la = offset + cur;
68947e4937aSGao Xiang 	map->m_llen = 0;
69047e4937aSGao Xiang 	err = z_erofs_map_blocks_iter(inode, map, 0);
6918d8a09b0SGao Xiang 	if (err)
69247e4937aSGao Xiang 		goto err_out;
69347e4937aSGao Xiang 
69447e4937aSGao Xiang restart_now:
6958d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
69647e4937aSGao Xiang 		goto hitted;
69747e4937aSGao Xiang 
69847e4937aSGao Xiang 	err = z_erofs_collector_begin(clt, inode, map);
6998d8a09b0SGao Xiang 	if (err)
70047e4937aSGao Xiang 		goto err_out;
70147e4937aSGao Xiang 
70247e4937aSGao Xiang 	/* preload all compressed pages (maybe downgrade role if necessary) */
703f57a3fe4SChao Yu 	if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
7041825c8d7SGao Xiang 		cache_strategy = TRYALLOC;
70547e4937aSGao Xiang 	else
70647e4937aSGao Xiang 		cache_strategy = DONTALLOC;
70747e4937aSGao Xiang 
7081825c8d7SGao Xiang 	preload_compressed_pages(clt, MNGD_MAPPING(sbi),
7091825c8d7SGao Xiang 				 cache_strategy, pagepool);
71047e4937aSGao Xiang 
71147e4937aSGao Xiang hitted:
712dc76ea8cSGao Xiang 	/*
713dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
714dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
715dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
716dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
717dc76ea8cSGao Xiang 	 */
718dc76ea8cSGao Xiang 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
719dc76ea8cSGao Xiang 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
720dc76ea8cSGao Xiang 
72147e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
7228d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
72347e4937aSGao Xiang 		zero_user_segment(page, cur, end);
72447e4937aSGao Xiang 		goto next_part;
72547e4937aSGao Xiang 	}
72647e4937aSGao Xiang 
72747e4937aSGao Xiang 	/* let's derive page type */
72847e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
72947e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
73047e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
73147e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
73247e4937aSGao Xiang 
73347e4937aSGao Xiang 	if (cur)
73447e4937aSGao Xiang 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
73547e4937aSGao Xiang 
73647e4937aSGao Xiang retry:
73747e4937aSGao Xiang 	err = z_erofs_attach_page(clt, page, page_type);
7386aaa7b06SGao Xiang 	/* should allocate an additional short-lived page for pagevec */
73947e4937aSGao Xiang 	if (err == -EAGAIN) {
74047e4937aSGao Xiang 		struct page *const newpage =
741e3f78d5eSChao Yu 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
74247e4937aSGao Xiang 
7436aaa7b06SGao Xiang 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
74447e4937aSGao Xiang 		err = z_erofs_attach_page(clt, newpage,
74547e4937aSGao Xiang 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
7468d8a09b0SGao Xiang 		if (!err)
74747e4937aSGao Xiang 			goto retry;
74847e4937aSGao Xiang 	}
74947e4937aSGao Xiang 
7508d8a09b0SGao Xiang 	if (err)
75147e4937aSGao Xiang 		goto err_out;
75247e4937aSGao Xiang 
75347e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
75447e4937aSGao Xiang 
75547e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
75647e4937aSGao Xiang 
75747e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
75847e4937aSGao Xiang 	++spiltted;
75947e4937aSGao Xiang 	/* also update nr_pages */
76047e4937aSGao Xiang 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
76147e4937aSGao Xiang next_part:
76247e4937aSGao Xiang 	/* can be used for verification */
76347e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
76447e4937aSGao Xiang 
76547e4937aSGao Xiang 	end = cur;
76647e4937aSGao Xiang 	if (end > 0)
76747e4937aSGao Xiang 		goto repeat;
76847e4937aSGao Xiang 
76947e4937aSGao Xiang out:
77047e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
77147e4937aSGao Xiang 
7724f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
77347e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
77447e4937aSGao Xiang 	return err;
77547e4937aSGao Xiang 
77647e4937aSGao Xiang 	/* if some error occurred while processing this page */
77747e4937aSGao Xiang err_out:
77847e4937aSGao Xiang 	SetPageError(page);
77947e4937aSGao Xiang 	goto out;
78047e4937aSGao Xiang }
78147e4937aSGao Xiang 
782648f2de0SHuang Jianan static void z_erofs_decompressqueue_work(struct work_struct *work);
783a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
784a4b1fab1SGao Xiang 				       bool sync, int bios)
78547e4937aSGao Xiang {
78630048cdaSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
78730048cdaSHuang Jianan 
788a4b1fab1SGao Xiang 	/* wake up the caller thread for sync decompression */
789a4b1fab1SGao Xiang 	if (sync) {
79047e4937aSGao Xiang 		unsigned long flags;
79147e4937aSGao Xiang 
79247e4937aSGao Xiang 		spin_lock_irqsave(&io->u.wait.lock, flags);
79347e4937aSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
79447e4937aSGao Xiang 			wake_up_locked(&io->u.wait);
79547e4937aSGao Xiang 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
79647e4937aSGao Xiang 		return;
79747e4937aSGao Xiang 	}
79847e4937aSGao Xiang 
799648f2de0SHuang Jianan 	if (atomic_add_return(bios, &io->pending_bios))
800648f2de0SHuang Jianan 		return;
80130048cdaSHuang Jianan 	/* Use workqueue and sync decompression for atomic contexts only */
802648f2de0SHuang Jianan 	if (in_atomic() || irqs_disabled()) {
80347e4937aSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
80430048cdaSHuang Jianan 		sbi->ctx.readahead_sync_decompress = true;
805648f2de0SHuang Jianan 		return;
806648f2de0SHuang Jianan 	}
807648f2de0SHuang Jianan 	z_erofs_decompressqueue_work(&io->u.work);
80847e4937aSGao Xiang }
80947e4937aSGao Xiang 
8106aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
8116aaa7b06SGao Xiang {
8126aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
8136aaa7b06SGao Xiang }
8146aaa7b06SGao Xiang 
8150c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
81647e4937aSGao Xiang {
817a4b1fab1SGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
818a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
81947e4937aSGao Xiang 	blk_status_t err = bio->bi_status;
82047e4937aSGao Xiang 	struct bio_vec *bvec;
82147e4937aSGao Xiang 	struct bvec_iter_all iter_all;
82247e4937aSGao Xiang 
82347e4937aSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
82447e4937aSGao Xiang 		struct page *page = bvec->bv_page;
82547e4937aSGao Xiang 
82647e4937aSGao Xiang 		DBG_BUGON(PageUptodate(page));
8276aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
82847e4937aSGao Xiang 
8298d8a09b0SGao Xiang 		if (err)
83047e4937aSGao Xiang 			SetPageError(page);
83147e4937aSGao Xiang 
832a4b1fab1SGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
833a4b1fab1SGao Xiang 			if (!err)
834a4b1fab1SGao Xiang 				SetPageUptodate(page);
83547e4937aSGao Xiang 			unlock_page(page);
83647e4937aSGao Xiang 		}
837a4b1fab1SGao Xiang 	}
838a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
83947e4937aSGao Xiang 	bio_put(bio);
84047e4937aSGao Xiang }
84147e4937aSGao Xiang 
84247e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
84347e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
84447e4937aSGao Xiang 				       struct list_head *pagepool)
84547e4937aSGao Xiang {
84647e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
84747e4937aSGao Xiang 	struct z_erofs_pagevec_ctor ctor;
848*9f6cc76eSGao Xiang 	unsigned int i, inputsize, outputsize, llen, nr_pages;
84947e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
85047e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
85147e4937aSGao Xiang 
85247e4937aSGao Xiang 	enum z_erofs_page_type page_type;
85347e4937aSGao Xiang 	bool overlapped, partial;
85447e4937aSGao Xiang 	struct z_erofs_collection *cl;
85547e4937aSGao Xiang 	int err;
85647e4937aSGao Xiang 
85747e4937aSGao Xiang 	might_sleep();
85847e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
85947e4937aSGao Xiang 	DBG_BUGON(!READ_ONCE(cl->nr_pages));
86047e4937aSGao Xiang 
86147e4937aSGao Xiang 	mutex_lock(&cl->lock);
86247e4937aSGao Xiang 	nr_pages = cl->nr_pages;
86347e4937aSGao Xiang 
8648d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
86547e4937aSGao Xiang 		pages = pages_onstack;
86647e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
86747e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
86847e4937aSGao Xiang 		pages = z_pagemap_global;
86947e4937aSGao Xiang 	} else {
87047e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
87147e4937aSGao Xiang 
87247e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
87347e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
87447e4937aSGao Xiang 
87547e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
87647e4937aSGao Xiang 				       gfp_flags);
87747e4937aSGao Xiang 
87847e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
8798d8a09b0SGao Xiang 		if (!pages) {
88047e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
88147e4937aSGao Xiang 			pages = z_pagemap_global;
88247e4937aSGao Xiang 		}
88347e4937aSGao Xiang 	}
88447e4937aSGao Xiang 
88547e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
88647e4937aSGao Xiang 		pages[i] = NULL;
88747e4937aSGao Xiang 
88847e4937aSGao Xiang 	err = 0;
88947e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
89047e4937aSGao Xiang 				  cl->pagevec, 0);
89147e4937aSGao Xiang 
89247e4937aSGao Xiang 	for (i = 0; i < cl->vcnt; ++i) {
89347e4937aSGao Xiang 		unsigned int pagenr;
89447e4937aSGao Xiang 
89547e4937aSGao Xiang 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
89647e4937aSGao Xiang 
89747e4937aSGao Xiang 		/* all pages in pagevec ought to be valid */
89847e4937aSGao Xiang 		DBG_BUGON(!page);
8996aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
90047e4937aSGao Xiang 
9016aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
90247e4937aSGao Xiang 			continue;
90347e4937aSGao Xiang 
90447e4937aSGao Xiang 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
90547e4937aSGao Xiang 			pagenr = 0;
90647e4937aSGao Xiang 		else
90747e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
90847e4937aSGao Xiang 
90947e4937aSGao Xiang 		DBG_BUGON(pagenr >= nr_pages);
91047e4937aSGao Xiang 
91147e4937aSGao Xiang 		/*
91247e4937aSGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
91347e4937aSGao Xiang 		 * so here erroring out one multiref page.
91447e4937aSGao Xiang 		 */
9158d8a09b0SGao Xiang 		if (pages[pagenr]) {
91647e4937aSGao Xiang 			DBG_BUGON(1);
91747e4937aSGao Xiang 			SetPageError(pages[pagenr]);
91847e4937aSGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
91947e4937aSGao Xiang 			err = -EFSCORRUPTED;
92047e4937aSGao Xiang 		}
92147e4937aSGao Xiang 		pages[pagenr] = page;
92247e4937aSGao Xiang 	}
92347e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&ctor, true);
92447e4937aSGao Xiang 
92547e4937aSGao Xiang 	overlapped = false;
92647e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
92747e4937aSGao Xiang 
928*9f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
92947e4937aSGao Xiang 		unsigned int pagenr;
93047e4937aSGao Xiang 
93147e4937aSGao Xiang 		page = compressed_pages[i];
93247e4937aSGao Xiang 
93347e4937aSGao Xiang 		/* all compressed pages ought to be valid */
93447e4937aSGao Xiang 		DBG_BUGON(!page);
9356aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
93647e4937aSGao Xiang 
9376aaa7b06SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
93847e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
9398d8a09b0SGao Xiang 				if (!PageUptodate(page))
94047e4937aSGao Xiang 					err = -EIO;
94147e4937aSGao Xiang 				continue;
94247e4937aSGao Xiang 			}
94347e4937aSGao Xiang 
94447e4937aSGao Xiang 			/*
94547e4937aSGao Xiang 			 * only if non-head page can be selected
94647e4937aSGao Xiang 			 * for inplace decompression
94747e4937aSGao Xiang 			 */
94847e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
94947e4937aSGao Xiang 
95047e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
9518d8a09b0SGao Xiang 			if (pages[pagenr]) {
95247e4937aSGao Xiang 				DBG_BUGON(1);
95347e4937aSGao Xiang 				SetPageError(pages[pagenr]);
95447e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
95547e4937aSGao Xiang 				err = -EFSCORRUPTED;
95647e4937aSGao Xiang 			}
95747e4937aSGao Xiang 			pages[pagenr] = page;
95847e4937aSGao Xiang 
95947e4937aSGao Xiang 			overlapped = true;
96047e4937aSGao Xiang 		}
96147e4937aSGao Xiang 
9626aaa7b06SGao Xiang 		/* PG_error needs checking for all non-managed pages */
9638d8a09b0SGao Xiang 		if (PageError(page)) {
96447e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
96547e4937aSGao Xiang 			err = -EIO;
96647e4937aSGao Xiang 		}
96747e4937aSGao Xiang 	}
96847e4937aSGao Xiang 
9698d8a09b0SGao Xiang 	if (err)
97047e4937aSGao Xiang 		goto out;
97147e4937aSGao Xiang 
97247e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
97347e4937aSGao Xiang 	if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
97447e4937aSGao Xiang 		outputsize = llen;
97547e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
97647e4937aSGao Xiang 	} else {
97747e4937aSGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
97847e4937aSGao Xiang 		partial = true;
97947e4937aSGao Xiang 	}
98047e4937aSGao Xiang 
981*9f6cc76eSGao Xiang 	inputsize = pcl->pclusterpages * PAGE_SIZE;
98247e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
98347e4937aSGao Xiang 					.sb = sb,
98447e4937aSGao Xiang 					.in = compressed_pages,
98547e4937aSGao Xiang 					.out = pages,
98647e4937aSGao Xiang 					.pageofs_out = cl->pageofs,
987*9f6cc76eSGao Xiang 					.inputsize = inputsize,
98847e4937aSGao Xiang 					.outputsize = outputsize,
98947e4937aSGao Xiang 					.alg = pcl->algorithmformat,
99047e4937aSGao Xiang 					.inplace_io = overlapped,
99147e4937aSGao Xiang 					.partial_decoding = partial
99247e4937aSGao Xiang 				 }, pagepool);
99347e4937aSGao Xiang 
99447e4937aSGao Xiang out:
995fe6adcceSRuiqi Gong 	/* must handle all compressed pages before ending pages */
996*9f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
99747e4937aSGao Xiang 		page = compressed_pages[i];
99847e4937aSGao Xiang 
99947e4937aSGao Xiang 		if (erofs_page_is_managed(sbi, page))
100047e4937aSGao Xiang 			continue;
100147e4937aSGao Xiang 
10026aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
10036aaa7b06SGao Xiang 		(void)z_erofs_put_shortlivedpage(pagepool, page);
100447e4937aSGao Xiang 
100547e4937aSGao Xiang 		WRITE_ONCE(compressed_pages[i], NULL);
100647e4937aSGao Xiang 	}
100747e4937aSGao Xiang 
100847e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
100947e4937aSGao Xiang 		page = pages[i];
101047e4937aSGao Xiang 		if (!page)
101147e4937aSGao Xiang 			continue;
101247e4937aSGao Xiang 
10136aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
101447e4937aSGao Xiang 
10156aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
10166aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
101747e4937aSGao Xiang 			continue;
101847e4937aSGao Xiang 
10198d8a09b0SGao Xiang 		if (err < 0)
102047e4937aSGao Xiang 			SetPageError(page);
102147e4937aSGao Xiang 
102247e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
102347e4937aSGao Xiang 	}
102447e4937aSGao Xiang 
102547e4937aSGao Xiang 	if (pages == z_pagemap_global)
102647e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
10278d8a09b0SGao Xiang 	else if (pages != pages_onstack)
102847e4937aSGao Xiang 		kvfree(pages);
102947e4937aSGao Xiang 
103047e4937aSGao Xiang 	cl->nr_pages = 0;
103147e4937aSGao Xiang 	cl->vcnt = 0;
103247e4937aSGao Xiang 
103347e4937aSGao Xiang 	/* all cl locks MUST be taken before the following line */
103447e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
103547e4937aSGao Xiang 
103647e4937aSGao Xiang 	/* all cl locks SHOULD be released right now */
103747e4937aSGao Xiang 	mutex_unlock(&cl->lock);
103847e4937aSGao Xiang 
103947e4937aSGao Xiang 	z_erofs_collection_put(cl);
104047e4937aSGao Xiang 	return err;
104147e4937aSGao Xiang }
104247e4937aSGao Xiang 
10430c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
104447e4937aSGao Xiang 				     struct list_head *pagepool)
104547e4937aSGao Xiang {
104647e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
104747e4937aSGao Xiang 
104847e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
104947e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
105047e4937aSGao Xiang 
105147e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
105247e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
105347e4937aSGao Xiang 
105447e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
105547e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
105647e4937aSGao Xiang 
105747e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
105847e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
105947e4937aSGao Xiang 
1060a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
106147e4937aSGao Xiang 	}
106247e4937aSGao Xiang }
106347e4937aSGao Xiang 
10640c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
106547e4937aSGao Xiang {
1066a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1067a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
106847e4937aSGao Xiang 	LIST_HEAD(pagepool);
106947e4937aSGao Xiang 
1070a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
10710c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
107247e4937aSGao Xiang 
107347e4937aSGao Xiang 	put_pages_list(&pagepool);
1074a4b1fab1SGao Xiang 	kvfree(bgq);
107547e4937aSGao Xiang }
107647e4937aSGao Xiang 
107747e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
107847e4937aSGao Xiang 					       unsigned int nr,
107947e4937aSGao Xiang 					       struct list_head *pagepool,
108047e4937aSGao Xiang 					       struct address_space *mc,
108147e4937aSGao Xiang 					       gfp_t gfp)
108247e4937aSGao Xiang {
108347e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
108447e4937aSGao Xiang 	bool tocache = false;
108547e4937aSGao Xiang 
108647e4937aSGao Xiang 	struct address_space *mapping;
108747e4937aSGao Xiang 	struct page *oldpage, *page;
108847e4937aSGao Xiang 
108947e4937aSGao Xiang 	compressed_page_t t;
109047e4937aSGao Xiang 	int justfound;
109147e4937aSGao Xiang 
109247e4937aSGao Xiang repeat:
109347e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
109447e4937aSGao Xiang 	oldpage = page;
109547e4937aSGao Xiang 
109647e4937aSGao Xiang 	if (!page)
109747e4937aSGao Xiang 		goto out_allocpage;
109847e4937aSGao Xiang 
109947e4937aSGao Xiang 	/*
110047e4937aSGao Xiang 	 * the cached page has not been allocated and
110147e4937aSGao Xiang 	 * an placeholder is out there, prepare it now.
110247e4937aSGao Xiang 	 */
1103bda17a45SGao Xiang 	if (page == PAGE_UNALLOCATED) {
110447e4937aSGao Xiang 		tocache = true;
110547e4937aSGao Xiang 		goto out_allocpage;
110647e4937aSGao Xiang 	}
110747e4937aSGao Xiang 
110847e4937aSGao Xiang 	/* process the target tagged pointer */
110947e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
111047e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
111147e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
111247e4937aSGao Xiang 
11131825c8d7SGao Xiang 	/*
11141825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
11151825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
11161825c8d7SGao Xiang 	 */
11171825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
11181825c8d7SGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
11191825c8d7SGao Xiang 		set_page_private(page, 0);
11201825c8d7SGao Xiang 		tocache = true;
11211825c8d7SGao Xiang 		goto out_tocache;
11221825c8d7SGao Xiang 	}
112347e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
112447e4937aSGao Xiang 
112547e4937aSGao Xiang 	/*
11266aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
112747e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
112847e4937aSGao Xiang 	 */
112947e4937aSGao Xiang 	if (mapping && mapping != mc)
113047e4937aSGao Xiang 		/* ought to be unmanaged pages */
113147e4937aSGao Xiang 		goto out;
113247e4937aSGao Xiang 
11336aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
11346aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
11356aaa7b06SGao Xiang 		goto out;
11366aaa7b06SGao Xiang 
113747e4937aSGao Xiang 	lock_page(page);
113847e4937aSGao Xiang 
113947e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
114047e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
114147e4937aSGao Xiang 
114247e4937aSGao Xiang 	/* the page is still in manage cache */
114347e4937aSGao Xiang 	if (page->mapping == mc) {
114447e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
114547e4937aSGao Xiang 
114647e4937aSGao Xiang 		ClearPageError(page);
114747e4937aSGao Xiang 		if (!PagePrivate(page)) {
114847e4937aSGao Xiang 			/*
114947e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
115047e4937aSGao Xiang 			 * the current restriction as well if
115147e4937aSGao Xiang 			 * the page is already in compressed_pages[].
115247e4937aSGao Xiang 			 */
115347e4937aSGao Xiang 			DBG_BUGON(!justfound);
115447e4937aSGao Xiang 
115547e4937aSGao Xiang 			justfound = 0;
115647e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
115747e4937aSGao Xiang 			SetPagePrivate(page);
115847e4937aSGao Xiang 		}
115947e4937aSGao Xiang 
116047e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
116147e4937aSGao Xiang 		if (PageUptodate(page)) {
116247e4937aSGao Xiang 			unlock_page(page);
116347e4937aSGao Xiang 			page = NULL;
116447e4937aSGao Xiang 		}
116547e4937aSGao Xiang 		goto out;
116647e4937aSGao Xiang 	}
116747e4937aSGao Xiang 
116847e4937aSGao Xiang 	/*
116947e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
117047e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
117147e4937aSGao Xiang 	 */
117247e4937aSGao Xiang 	DBG_BUGON(page->mapping);
117347e4937aSGao Xiang 	DBG_BUGON(!justfound);
117447e4937aSGao Xiang 
117547e4937aSGao Xiang 	tocache = true;
117647e4937aSGao Xiang 	unlock_page(page);
117747e4937aSGao Xiang 	put_page(page);
117847e4937aSGao Xiang out_allocpage:
11795ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
11805ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
11815ddcee1fSGao Xiang 		list_add(&page->lru, pagepool);
11825ddcee1fSGao Xiang 		cond_resched();
11835ddcee1fSGao Xiang 		goto repeat;
11845ddcee1fSGao Xiang 	}
11851825c8d7SGao Xiang out_tocache:
1186bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1187bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1188bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1189bf225074SGao Xiang 		goto out;
1190a30573b3SGao Xiang 	}
1191bf225074SGao Xiang 	attach_page_private(page, pcl);
1192bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1193bf225074SGao Xiang 	put_page(page);
1194bf225074SGao Xiang 
119547e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
119647e4937aSGao Xiang 	return page;
119747e4937aSGao Xiang }
119847e4937aSGao Xiang 
1199a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1200a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1201a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
120247e4937aSGao Xiang {
1203a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
120447e4937aSGao Xiang 
1205a4b1fab1SGao Xiang 	if (fg && !*fg) {
1206a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1207a4b1fab1SGao Xiang 		if (!q) {
1208a4b1fab1SGao Xiang 			*fg = true;
1209a4b1fab1SGao Xiang 			goto fg_out;
121047e4937aSGao Xiang 		}
12110c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1212a4b1fab1SGao Xiang 	} else {
1213a4b1fab1SGao Xiang fg_out:
1214a4b1fab1SGao Xiang 		q = fgq;
1215a4b1fab1SGao Xiang 		init_waitqueue_head(&fgq->u.wait);
1216a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1217a4b1fab1SGao Xiang 	}
1218a4b1fab1SGao Xiang 	q->sb = sb;
1219a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1220a4b1fab1SGao Xiang 	return q;
122147e4937aSGao Xiang }
122247e4937aSGao Xiang 
122347e4937aSGao Xiang /* define decompression jobqueue types */
122447e4937aSGao Xiang enum {
122547e4937aSGao Xiang 	JQ_BYPASS,
122647e4937aSGao Xiang 	JQ_SUBMIT,
122747e4937aSGao Xiang 	NR_JOBQUEUES,
122847e4937aSGao Xiang };
122947e4937aSGao Xiang 
123047e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1231a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1232a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
123347e4937aSGao Xiang {
123447e4937aSGao Xiang 	/*
123547e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
123647e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
123747e4937aSGao Xiang 	 */
1238a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1239a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
124047e4937aSGao Xiang 
1241a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
124247e4937aSGao Xiang }
124347e4937aSGao Xiang 
124447e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
124547e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
124647e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
124747e4937aSGao Xiang {
124847e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
124947e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
125047e4937aSGao Xiang 
125147e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
125247e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
125347e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
125447e4937aSGao Xiang 
125547e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
125647e4937aSGao Xiang 
125747e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
125847e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
125947e4937aSGao Xiang 
126047e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
126147e4937aSGao Xiang }
126247e4937aSGao Xiang 
12631e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb,
12646ea5aad3SGao Xiang 				 struct z_erofs_decompress_frontend *f,
126547e4937aSGao Xiang 				 struct list_head *pagepool,
1266a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1267a4b1fab1SGao Xiang 				 bool *force_fg)
126847e4937aSGao Xiang {
1269bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
127047e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1271a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
127247e4937aSGao Xiang 	void *bi_private;
12736ea5aad3SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
127447e4937aSGao Xiang 	/* since bio will be NULL, no need to initialize last_index */
12753f649ab7SKees Cook 	pgoff_t last_index;
12761e4a2955SGao Xiang 	unsigned int nr_bios = 0;
12771e4a2955SGao Xiang 	struct bio *bio = NULL;
127847e4937aSGao Xiang 
1279a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1280a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1281a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
128247e4937aSGao Xiang 
128347e4937aSGao Xiang 	/* by default, all need io submission */
128447e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
128547e4937aSGao Xiang 
128647e4937aSGao Xiang 	do {
128747e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
12881e4a2955SGao Xiang 		pgoff_t cur, end;
12891e4a2955SGao Xiang 		unsigned int i = 0;
12901e4a2955SGao Xiang 		bool bypass = true;
129147e4937aSGao Xiang 
129247e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
129347e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
129447e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
129547e4937aSGao Xiang 
129647e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
129747e4937aSGao Xiang 
12981e4a2955SGao Xiang 		cur = pcl->obj.index;
1299*9f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
130047e4937aSGao Xiang 
130147e4937aSGao Xiang 		/* close the main owned chain at first */
130247e4937aSGao Xiang 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
130347e4937aSGao Xiang 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
130447e4937aSGao Xiang 
13051e4a2955SGao Xiang 		do {
13061e4a2955SGao Xiang 			struct page *page;
130747e4937aSGao Xiang 
13081e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
130947e4937aSGao Xiang 							  MNGD_MAPPING(sbi),
131047e4937aSGao Xiang 							  GFP_NOFS);
13111e4a2955SGao Xiang 			if (!page)
13121e4a2955SGao Xiang 				continue;
131347e4937aSGao Xiang 
13141e4a2955SGao Xiang 			if (bio && cur != last_index + 1) {
131547e4937aSGao Xiang submit_bio_retry:
131694e4e153SGao Xiang 				submit_bio(bio);
131747e4937aSGao Xiang 				bio = NULL;
131847e4937aSGao Xiang 			}
131947e4937aSGao Xiang 
132047e4937aSGao Xiang 			if (!bio) {
1321a8affc03SChristoph Hellwig 				bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS);
1322a5c0b780SGao Xiang 
13230c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1324a5c0b780SGao Xiang 				bio_set_dev(bio, sb->s_bdev);
13251e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1326a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1327a5c0b780SGao Xiang 				bio->bi_private = bi_private;
132894e4e153SGao Xiang 				bio->bi_opf = REQ_OP_READ;
13296ea5aad3SGao Xiang 				if (f->readahead)
13306ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
133147e4937aSGao Xiang 				++nr_bios;
133247e4937aSGao Xiang 			}
133347e4937aSGao Xiang 
13346c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
133547e4937aSGao Xiang 				goto submit_bio_retry;
133647e4937aSGao Xiang 
13371e4a2955SGao Xiang 			last_index = cur;
13381e4a2955SGao Xiang 			bypass = false;
13391e4a2955SGao Xiang 		} while (++cur < end);
134047e4937aSGao Xiang 
13411e4a2955SGao Xiang 		if (!bypass)
134247e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
134347e4937aSGao Xiang 		else
134447e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
134547e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
134647e4937aSGao Xiang 
134747e4937aSGao Xiang 	if (bio)
134894e4e153SGao Xiang 		submit_bio(bio);
134947e4937aSGao Xiang 
1350587a67b7SGao Xiang 	/*
1351587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1352587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1353587a67b7SGao Xiang 	 */
1354587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1355587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
13561e4a2955SGao Xiang 		return;
1357587a67b7SGao Xiang 	}
1358a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
135947e4937aSGao Xiang }
136047e4937aSGao Xiang 
13610c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb,
13626ea5aad3SGao Xiang 			     struct z_erofs_decompress_frontend *f,
13630c638f70SGao Xiang 			     struct list_head *pagepool, bool force_fg)
136447e4937aSGao Xiang {
1365a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
136647e4937aSGao Xiang 
13676ea5aad3SGao Xiang 	if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
136847e4937aSGao Xiang 		return;
13696ea5aad3SGao Xiang 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
137047e4937aSGao Xiang 
13710c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
13720c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
137347e4937aSGao Xiang 
137447e4937aSGao Xiang 	if (!force_fg)
137547e4937aSGao Xiang 		return;
137647e4937aSGao Xiang 
137747e4937aSGao Xiang 	/* wait until all bios are completed */
1378a93f8c36SGao Xiang 	io_wait_event(io[JQ_SUBMIT].u.wait,
137947e4937aSGao Xiang 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
138047e4937aSGao Xiang 
13810c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
13820c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
138347e4937aSGao Xiang }
138447e4937aSGao Xiang 
13850c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page)
138647e4937aSGao Xiang {
138747e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
138847e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
138947e4937aSGao Xiang 	int err;
139047e4937aSGao Xiang 	LIST_HEAD(pagepool);
139147e4937aSGao Xiang 
139247e4937aSGao Xiang 	trace_erofs_readpage(page, false);
139347e4937aSGao Xiang 
139447e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
139547e4937aSGao Xiang 
13961825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
139747e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
139847e4937aSGao Xiang 
139947e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
14006ea5aad3SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
140147e4937aSGao Xiang 
140247e4937aSGao Xiang 	if (err)
14034f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
140447e4937aSGao Xiang 
140547e4937aSGao Xiang 	if (f.map.mpage)
140647e4937aSGao Xiang 		put_page(f.map.mpage);
140747e4937aSGao Xiang 
140847e4937aSGao Xiang 	/* clean up the remaining free pages */
140947e4937aSGao Xiang 	put_pages_list(&pagepool);
141047e4937aSGao Xiang 	return err;
141147e4937aSGao Xiang }
141247e4937aSGao Xiang 
14130615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
141447e4937aSGao Xiang {
14150615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
141647e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
141747e4937aSGao Xiang 
1418bf9a123bSGao Xiang 	unsigned int nr_pages = readahead_count(rac);
141930048cdaSHuang Jianan 	bool sync = (sbi->ctx.readahead_sync_decompress &&
142030048cdaSHuang Jianan 			nr_pages <= sbi->ctx.max_sync_decompress_pages);
142147e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
14220615090cSMatthew Wilcox (Oracle) 	struct page *page, *head = NULL;
142347e4937aSGao Xiang 	LIST_HEAD(pagepool);
142447e4937aSGao Xiang 
1425bf9a123bSGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
142647e4937aSGao Xiang 
14276ea5aad3SGao Xiang 	f.readahead = true;
14280615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
142947e4937aSGao Xiang 
14300615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
143147e4937aSGao Xiang 		prefetchw(&page->flags);
143247e4937aSGao Xiang 
143347e4937aSGao Xiang 		/*
143447e4937aSGao Xiang 		 * A pure asynchronous readahead is indicated if
143547e4937aSGao Xiang 		 * a PG_readahead marked page is hitted at first.
143647e4937aSGao Xiang 		 * Let's also do asynchronous decompression for this case.
143747e4937aSGao Xiang 		 */
143847e4937aSGao Xiang 		sync &= !(PageReadahead(page) && !head);
143947e4937aSGao Xiang 
144047e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
144147e4937aSGao Xiang 		head = page;
144247e4937aSGao Xiang 	}
144347e4937aSGao Xiang 
144447e4937aSGao Xiang 	while (head) {
144547e4937aSGao Xiang 		struct page *page = head;
144647e4937aSGao Xiang 		int err;
144747e4937aSGao Xiang 
144847e4937aSGao Xiang 		/* traversal in reverse order */
144947e4937aSGao Xiang 		head = (void *)page_private(page);
145047e4937aSGao Xiang 
14511825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1452a5876e24SGao Xiang 		if (err)
14534f761fa2SGao Xiang 			erofs_err(inode->i_sb,
14544f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
14554f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
145647e4937aSGao Xiang 		put_page(page);
145747e4937aSGao Xiang 	}
145847e4937aSGao Xiang 
145947e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
146047e4937aSGao Xiang 
14616ea5aad3SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
146247e4937aSGao Xiang 
146347e4937aSGao Xiang 	if (f.map.mpage)
146447e4937aSGao Xiang 		put_page(f.map.mpage);
146547e4937aSGao Xiang 
146647e4937aSGao Xiang 	/* clean up the remaining free pages */
146747e4937aSGao Xiang 	put_pages_list(&pagepool);
146847e4937aSGao Xiang }
146947e4937aSGao Xiang 
14700c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
14710c638f70SGao Xiang 	.readpage = z_erofs_readpage,
14720615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
147347e4937aSGao Xiang };
147447e4937aSGao Xiang 
1475