xref: /openbmc/linux/fs/erofs/zdata.c (revision 39397a46)
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  */
647e4937aSGao Xiang #include "zdata.h"
747e4937aSGao Xiang #include "compress.h"
847e4937aSGao Xiang #include <linux/prefetch.h>
947e4937aSGao Xiang 
1047e4937aSGao Xiang #include <trace/events/erofs.h>
1147e4937aSGao Xiang 
1247e4937aSGao Xiang /*
139f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
149f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
159f6cc76eSGao Xiang  */
169f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
179f6cc76eSGao Xiang 	struct kmem_cache *slab;
189f6cc76eSGao Xiang 	unsigned int maxpages;
199f6cc76eSGao Xiang 	char name[48];
209f6cc76eSGao Xiang };
219f6cc76eSGao Xiang 
229f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
239f6cc76eSGao Xiang 
249f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
259f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
269f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
279f6cc76eSGao Xiang };
289f6cc76eSGao Xiang 
299f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
309f6cc76eSGao Xiang {
319f6cc76eSGao Xiang 	int i;
329f6cc76eSGao Xiang 
339f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
349f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
359f6cc76eSGao Xiang 			continue;
369f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
379f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
389f6cc76eSGao Xiang 	}
399f6cc76eSGao Xiang }
409f6cc76eSGao Xiang 
419f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
429f6cc76eSGao Xiang {
439f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
449f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
459f6cc76eSGao Xiang 	unsigned int size;
469f6cc76eSGao Xiang 
479f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
489f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
499f6cc76eSGao Xiang 		size = struct_size(a, compressed_pages, pcs->maxpages);
509f6cc76eSGao Xiang 
519f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
529f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
539f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
549f6cc76eSGao Xiang 		if (pcs->slab)
559f6cc76eSGao Xiang 			continue;
569f6cc76eSGao Xiang 
579f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
589f6cc76eSGao Xiang 		return -ENOMEM;
599f6cc76eSGao Xiang 	}
609f6cc76eSGao Xiang 	return 0;
619f6cc76eSGao Xiang }
629f6cc76eSGao Xiang 
639f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
649f6cc76eSGao Xiang {
659f6cc76eSGao Xiang 	int i;
669f6cc76eSGao Xiang 
679f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
689f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
699f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
709f6cc76eSGao Xiang 
719f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
729f6cc76eSGao Xiang 			continue;
739f6cc76eSGao Xiang 
749f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
759f6cc76eSGao Xiang 		if (!pcl)
769f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
779f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
789f6cc76eSGao Xiang 		return pcl;
799f6cc76eSGao Xiang 	}
809f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
819f6cc76eSGao Xiang }
829f6cc76eSGao Xiang 
839f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
849f6cc76eSGao Xiang {
85cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
869f6cc76eSGao Xiang 	int i;
879f6cc76eSGao Xiang 
889f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
899f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
909f6cc76eSGao Xiang 
91cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
929f6cc76eSGao Xiang 			continue;
939f6cc76eSGao Xiang 
949f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
959f6cc76eSGao Xiang 		return;
969f6cc76eSGao Xiang 	}
979f6cc76eSGao Xiang 	DBG_BUGON(1);
989f6cc76eSGao Xiang }
999f6cc76eSGao Xiang 
10047e4937aSGao Xiang /* how to allocate cached pages for a pcluster */
10147e4937aSGao Xiang enum z_erofs_cache_alloctype {
10247e4937aSGao Xiang 	DONTALLOC,	/* don't allocate any cached pages */
1031825c8d7SGao Xiang 	/*
1041825c8d7SGao Xiang 	 * try to use cached I/O if page allocation succeeds or fallback
1051825c8d7SGao Xiang 	 * to in-place I/O instead to avoid any direct reclaim.
1061825c8d7SGao Xiang 	 */
1071825c8d7SGao Xiang 	TRYALLOC,
10847e4937aSGao Xiang };
10947e4937aSGao Xiang 
11047e4937aSGao Xiang /*
11147e4937aSGao Xiang  * tagged pointer with 1-bit tag for all compressed pages
11247e4937aSGao Xiang  * tag 0 - the page is just found with an extra page reference
11347e4937aSGao Xiang  */
11447e4937aSGao Xiang typedef tagptr1_t compressed_page_t;
11547e4937aSGao Xiang 
11647e4937aSGao Xiang #define tag_compressed_page_justfound(page) \
11747e4937aSGao Xiang 	tagptr_fold(compressed_page_t, page, 1)
11847e4937aSGao Xiang 
11947e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
12047e4937aSGao Xiang 
12147e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
12247e4937aSGao Xiang {
12347e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
1249f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
12547e4937aSGao Xiang }
12647e4937aSGao Xiang 
12799634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
12847e4937aSGao Xiang {
12947e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
13047e4937aSGao Xiang 
13147e4937aSGao Xiang 	/*
13247e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
13347e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
13447e4937aSGao Xiang 	 */
1350e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
1360e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
13747e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
13847e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
13947e4937aSGao Xiang }
14047e4937aSGao Xiang 
14147e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
14247e4937aSGao Xiang {
1439f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
14447e4937aSGao Xiang 
1459f6cc76eSGao Xiang 	if (err)
1469f6cc76eSGao Xiang 		return err;
1479f6cc76eSGao Xiang 	err = z_erofs_init_workqueue();
1489f6cc76eSGao Xiang 	if (err)
1499f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
1509f6cc76eSGao Xiang 	return err;
15147e4937aSGao Xiang }
15247e4937aSGao Xiang 
15347e4937aSGao Xiang enum z_erofs_collectmode {
15447e4937aSGao Xiang 	COLLECT_SECONDARY,
15547e4937aSGao Xiang 	COLLECT_PRIMARY,
15647e4937aSGao Xiang 	/*
15747e4937aSGao Xiang 	 * The current collection was the tail of an exist chain, in addition
15847e4937aSGao Xiang 	 * that the previous processed chained collections are all decided to
15947e4937aSGao Xiang 	 * be hooked up to it.
16047e4937aSGao Xiang 	 * A new chain will be created for the remaining collections which are
16147e4937aSGao Xiang 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
16247e4937aSGao Xiang 	 * the next collection cannot reuse the whole page safely in
16347e4937aSGao Xiang 	 * the following scenario:
16447e4937aSGao Xiang 	 *  ________________________________________________________________
16547e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
16647e4937aSGao Xiang 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
16747e4937aSGao Xiang 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
16847e4937aSGao Xiang 	 */
16947e4937aSGao Xiang 	COLLECT_PRIMARY_HOOKED,
1700b964600SGao Xiang 	/*
1710b964600SGao Xiang 	 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
1720b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
1730b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
1740b964600SGao Xiang 	 * pagevec) since it can be directly decoded without I/O submission.
1750b964600SGao Xiang 	 */
17647e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
17747e4937aSGao Xiang 	/*
17847e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
17947e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
18047e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
18147e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
18247e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
18347e4937aSGao Xiang 	 * illustrated below:
18447e4937aSGao Xiang 	 *  ________________________________________________________________
18547e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
18647e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
18747e4937aSGao Xiang 	 * |  PRIMARY_FOLLOWED or |                                        |
18847e4937aSGao Xiang 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
18947e4937aSGao Xiang 	 *
19047e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
19147e4937aSGao Xiang 	 */
19247e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED,
19347e4937aSGao Xiang };
19447e4937aSGao Xiang 
1955c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
1965c6dcc57SGao Xiang 	struct inode *const inode;
1975c6dcc57SGao Xiang 	struct erofs_map_blocks map;
1985c6dcc57SGao Xiang 
19947e4937aSGao Xiang 	struct z_erofs_pagevec_ctor vector;
20047e4937aSGao Xiang 
20147e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
20281382f5fSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
20381382f5fSGao Xiang 	struct page **icpage_ptr;
20447e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
20547e4937aSGao Xiang 
20647e4937aSGao Xiang 	enum z_erofs_collectmode mode;
20747e4937aSGao Xiang 
2086ea5aad3SGao Xiang 	bool readahead;
20947e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
21047e4937aSGao Xiang 	bool backmost;
21147e4937aSGao Xiang 	erofs_off_t headoffset;
21247e4937aSGao Xiang };
21347e4937aSGao Xiang 
21447e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
2155c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
2165c6dcc57SGao Xiang 	.mode = COLLECT_PRIMARY_FOLLOWED }
21747e4937aSGao Xiang 
21847e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
21947e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
22047e4937aSGao Xiang 
2216f39d1e1SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
2221825c8d7SGao Xiang 			       enum z_erofs_cache_alloctype type,
223eaa9172aSGao Xiang 			       struct page **pagepool)
22447e4937aSGao Xiang {
2256f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
2265c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
22747e4937aSGao Xiang 	bool standalone = true;
2286f39d1e1SGao Xiang 	/*
2296f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
2306f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
2316f39d1e1SGao Xiang 	 */
2321825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
2331825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
23481382f5fSGao Xiang 	struct page **pages;
23581382f5fSGao Xiang 	pgoff_t index;
23647e4937aSGao Xiang 
2375c6dcc57SGao Xiang 	if (fe->mode < COLLECT_PRIMARY_FOLLOWED)
23847e4937aSGao Xiang 		return;
23947e4937aSGao Xiang 
24081382f5fSGao Xiang 	pages = pcl->compressed_pages;
24181382f5fSGao Xiang 	index = pcl->obj.index;
24281382f5fSGao Xiang 	for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
24347e4937aSGao Xiang 		struct page *page;
24447e4937aSGao Xiang 		compressed_page_t t;
2451825c8d7SGao Xiang 		struct page *newpage = NULL;
24647e4937aSGao Xiang 
24747e4937aSGao Xiang 		/* the compressed page was loaded before */
24847e4937aSGao Xiang 		if (READ_ONCE(*pages))
24947e4937aSGao Xiang 			continue;
25047e4937aSGao Xiang 
25147e4937aSGao Xiang 		page = find_get_page(mc, index);
25247e4937aSGao Xiang 
25347e4937aSGao Xiang 		if (page) {
25447e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
2550b964600SGao Xiang 		} else {
2560b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
2570b964600SGao Xiang 			standalone = false;
2580b964600SGao Xiang 			switch (type) {
2590b964600SGao Xiang 			case TRYALLOC:
2601825c8d7SGao Xiang 				newpage = erofs_allocpage(pagepool, gfp);
2611825c8d7SGao Xiang 				if (!newpage)
26247e4937aSGao Xiang 					continue;
2630b964600SGao Xiang 				set_page_private(newpage,
2640b964600SGao Xiang 						 Z_EROFS_PREALLOCATED_PAGE);
2650b964600SGao Xiang 				t = tag_compressed_page_justfound(newpage);
2660b964600SGao Xiang 				break;
2670b964600SGao Xiang 			default:        /* DONTALLOC */
2680b964600SGao Xiang 				continue;
2690b964600SGao Xiang 			}
27047e4937aSGao Xiang 		}
27147e4937aSGao Xiang 
27247e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
27347e4937aSGao Xiang 			continue;
27447e4937aSGao Xiang 
275eaa9172aSGao Xiang 		if (page)
27647e4937aSGao Xiang 			put_page(page);
277eaa9172aSGao Xiang 		else if (newpage)
278eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
27947e4937aSGao Xiang 	}
28047e4937aSGao Xiang 
2810b964600SGao Xiang 	/*
2820b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
2830b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
2840b964600SGao Xiang 	 */
2850b964600SGao Xiang 	if (standalone)
2865c6dcc57SGao Xiang 		fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
28747e4937aSGao Xiang }
28847e4937aSGao Xiang 
28947e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
29047e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
29147e4937aSGao Xiang 				       struct erofs_workgroup *grp)
29247e4937aSGao Xiang {
29347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
29447e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
29547e4937aSGao Xiang 	int i;
29647e4937aSGao Xiang 
297cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
29847e4937aSGao Xiang 	/*
29947e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
30047e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
30147e4937aSGao Xiang 	 */
3029f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
30347e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
30447e4937aSGao Xiang 
30547e4937aSGao Xiang 		if (!page)
30647e4937aSGao Xiang 			continue;
30747e4937aSGao Xiang 
30847e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
30947e4937aSGao Xiang 		if (!trylock_page(page))
31047e4937aSGao Xiang 			return -EBUSY;
31147e4937aSGao Xiang 
312f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
31347e4937aSGao Xiang 			continue;
31447e4937aSGao Xiang 
31547e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
31647e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
3176aaa7b06SGao Xiang 		detach_page_private(page);
31847e4937aSGao Xiang 		unlock_page(page);
31947e4937aSGao Xiang 	}
32047e4937aSGao Xiang 	return 0;
32147e4937aSGao Xiang }
32247e4937aSGao Xiang 
323d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
32447e4937aSGao Xiang {
32547e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
32647e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
32747e4937aSGao Xiang 
32847e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
32947e4937aSGao Xiang 		unsigned int i;
33047e4937aSGao Xiang 
331cecf864dSYue Hu 		DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
3329f6cc76eSGao Xiang 		for (i = 0; i < pcl->pclusterpages; ++i) {
33347e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
33447e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
33547e4937aSGao Xiang 				ret = 1;
33647e4937aSGao Xiang 				break;
33747e4937aSGao Xiang 			}
33847e4937aSGao Xiang 		}
33947e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
34047e4937aSGao Xiang 
3416aaa7b06SGao Xiang 		if (ret)
3426aaa7b06SGao Xiang 			detach_page_private(page);
34347e4937aSGao Xiang 	}
34447e4937aSGao Xiang 	return ret;
34547e4937aSGao Xiang }
34647e4937aSGao Xiang 
34747e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
3485c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
34947e4937aSGao Xiang 				   struct page *page)
35047e4937aSGao Xiang {
3515c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
35247e4937aSGao Xiang 
3535c6dcc57SGao Xiang 	while (fe->icpage_ptr > pcl->compressed_pages)
3545c6dcc57SGao Xiang 		if (!cmpxchg(--fe->icpage_ptr, NULL, page))
35547e4937aSGao Xiang 			return true;
35647e4937aSGao Xiang 	return false;
35747e4937aSGao Xiang }
35847e4937aSGao Xiang 
35987ca34a7SGao Xiang /* callers must be with pcluster lock held */
3605c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
36186432a6dSGao Xiang 			       struct page *page, enum z_erofs_page_type type,
36286432a6dSGao Xiang 			       bool pvec_safereuse)
36347e4937aSGao Xiang {
36447e4937aSGao Xiang 	int ret;
36547e4937aSGao Xiang 
36647e4937aSGao Xiang 	/* give priority for inplaceio */
3675c6dcc57SGao Xiang 	if (fe->mode >= COLLECT_PRIMARY &&
36847e4937aSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
3695c6dcc57SGao Xiang 	    z_erofs_try_inplace_io(fe, page))
37047e4937aSGao Xiang 		return 0;
37147e4937aSGao Xiang 
3725c6dcc57SGao Xiang 	ret = z_erofs_pagevec_enqueue(&fe->vector, page, type,
37386432a6dSGao Xiang 				      pvec_safereuse);
37487ca34a7SGao Xiang 	fe->pcl->vcnt += (unsigned int)ret;
37547e4937aSGao Xiang 	return ret ? 0 : -EAGAIN;
37647e4937aSGao Xiang }
37747e4937aSGao Xiang 
3785c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
37947e4937aSGao Xiang {
3805c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
3815c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
38247e4937aSGao Xiang 
383473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
384473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
385473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
38647e4937aSGao Xiang 		*owned_head = &pcl->next;
387473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
3885c6dcc57SGao Xiang 		f->mode = COLLECT_PRIMARY_FOLLOWED;
389473e15b0SGao Xiang 		return;
390473e15b0SGao Xiang 	}
391473e15b0SGao Xiang 
39247e4937aSGao Xiang 	/*
393473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
394473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
39547e4937aSGao Xiang 	 */
39647e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
397473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
39847e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
3995c6dcc57SGao Xiang 		f->mode = COLLECT_PRIMARY_HOOKED;
4005c6dcc57SGao Xiang 		f->tailpcl = NULL;
401473e15b0SGao Xiang 		return;
40247e4937aSGao Xiang 	}
403473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
4045c6dcc57SGao Xiang 	f->mode = COLLECT_PRIMARY;
40547e4937aSGao Xiang }
40647e4937aSGao Xiang 
40787ca34a7SGao Xiang static int z_erofs_lookup_pcluster(struct z_erofs_decompress_frontend *fe,
40847e4937aSGao Xiang 				   struct inode *inode,
40947e4937aSGao Xiang 				   struct erofs_map_blocks *map)
41047e4937aSGao Xiang {
4115c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
41247e4937aSGao Xiang 	unsigned int length;
41347e4937aSGao Xiang 
41464094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
4155c6dcc57SGao Xiang 	if (fe->owned_head == &pcl->next || pcl == fe->tailpcl) {
41647e4937aSGao Xiang 		DBG_BUGON(1);
4179e579fc1SGao Xiang 		return -EFSCORRUPTED;
41847e4937aSGao Xiang 	}
41947e4937aSGao Xiang 
42087ca34a7SGao Xiang 	if (pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) {
42147e4937aSGao Xiang 		DBG_BUGON(1);
4229e579fc1SGao Xiang 		return -EFSCORRUPTED;
42347e4937aSGao Xiang 	}
42447e4937aSGao Xiang 
42547e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
42647e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
42747e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
42847e4937aSGao Xiang 			DBG_BUGON(1);
4299e579fc1SGao Xiang 			return -EFSCORRUPTED;
43047e4937aSGao Xiang 		}
43147e4937aSGao Xiang 	} else {
43247e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
43347e4937aSGao Xiang 
43447e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
43547e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
43647e4937aSGao Xiang 
43747e4937aSGao Xiang 		while (llen > length &&
43847e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
43947e4937aSGao Xiang 			cpu_relax();
44047e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
44147e4937aSGao Xiang 		}
44247e4937aSGao Xiang 	}
44387ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
44447e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
4455c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
4465c6dcc57SGao Xiang 		fe->tailpcl = pcl;
447473e15b0SGao Xiang 
4485c6dcc57SGao Xiang 	z_erofs_try_to_claim_pcluster(fe);
4499e579fc1SGao Xiang 	return 0;
45047e4937aSGao Xiang }
45147e4937aSGao Xiang 
45287ca34a7SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe,
45347e4937aSGao Xiang 				     struct inode *inode,
45447e4937aSGao Xiang 				     struct erofs_map_blocks *map)
45547e4937aSGao Xiang {
456cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
45747e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
45864094a04SGao Xiang 	struct erofs_workgroup *grp;
45947e4937aSGao Xiang 	int err;
46047e4937aSGao Xiang 
4618f899262SGao Xiang 	if (!(map->m_flags & EROFS_MAP_ENCODED)) {
4628f899262SGao Xiang 		DBG_BUGON(1);
4638f899262SGao Xiang 		return -EFSCORRUPTED;
4648f899262SGao Xiang 	}
4658f899262SGao Xiang 
4669f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
467cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
468cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
4699f6cc76eSGao Xiang 	if (IS_ERR(pcl))
4709f6cc76eSGao Xiang 		return PTR_ERR(pcl);
47147e4937aSGao Xiang 
47264094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
4738f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
47447e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
47547e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
47647e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
47747e4937aSGao Xiang 
47847e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
4795c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
48087ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
4815c6dcc57SGao Xiang 	fe->mode = COLLECT_PRIMARY_FOLLOWED;
48247e4937aSGao Xiang 
48347e4937aSGao Xiang 	/*
48447e4937aSGao Xiang 	 * lock all primary followed works before visible to others
48547e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
48647e4937aSGao Xiang 	 */
48787ca34a7SGao Xiang 	mutex_init(&pcl->lock);
48887ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
48947e4937aSGao Xiang 
490cecf864dSYue Hu 	if (ztailpacking) {
491cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
492cecf864dSYue Hu 		pcl->pageofs_in = erofs_blkoff(map->m_pa);
493cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
494cecf864dSYue Hu 	} else {
495cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
496cecf864dSYue Hu 
49764094a04SGao Xiang 		grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
49864094a04SGao Xiang 		if (IS_ERR(grp)) {
49964094a04SGao Xiang 			err = PTR_ERR(grp);
50064094a04SGao Xiang 			goto err_out;
50164094a04SGao Xiang 		}
50264094a04SGao Xiang 
50364094a04SGao Xiang 		if (grp != &pcl->obj) {
5045c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
505cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
50664094a04SGao Xiang 			err = -EEXIST;
50764094a04SGao Xiang 			goto err_out;
50847e4937aSGao Xiang 		}
509cecf864dSYue Hu 	}
51047e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
5115c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
5125c6dcc57SGao Xiang 		fe->tailpcl = pcl;
5135c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
5145c6dcc57SGao Xiang 	fe->pcl = pcl;
5159e579fc1SGao Xiang 	return 0;
51664094a04SGao Xiang 
51764094a04SGao Xiang err_out:
51887ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
5199f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
52064094a04SGao Xiang 	return err;
52147e4937aSGao Xiang }
52247e4937aSGao Xiang 
5235c6dcc57SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe,
52447e4937aSGao Xiang 				   struct inode *inode,
52547e4937aSGao Xiang 				   struct erofs_map_blocks *map)
52647e4937aSGao Xiang {
52764094a04SGao Xiang 	struct erofs_workgroup *grp;
5289e579fc1SGao Xiang 	int ret;
52947e4937aSGao Xiang 
53087ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
53147e4937aSGao Xiang 
53287ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
5335c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
5345c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
53547e4937aSGao Xiang 
536cecf864dSYue Hu 	if (map->m_flags & EROFS_MAP_META) {
537cecf864dSYue Hu 		if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
53847e4937aSGao Xiang 			DBG_BUGON(1);
539cecf864dSYue Hu 			return -EFSCORRUPTED;
540cecf864dSYue Hu 		}
541cecf864dSYue Hu 		goto tailpacking;
54247e4937aSGao Xiang 	}
54347e4937aSGao Xiang 
54464094a04SGao Xiang 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
54564094a04SGao Xiang 	if (grp) {
5465c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
54764094a04SGao Xiang 	} else {
548cecf864dSYue Hu tailpacking:
54987ca34a7SGao Xiang 		ret = z_erofs_register_pcluster(fe, inode, map);
55064094a04SGao Xiang 		if (!ret)
55164094a04SGao Xiang 			goto out;
55264094a04SGao Xiang 		if (ret != -EEXIST)
5539e579fc1SGao Xiang 			return ret;
55464094a04SGao Xiang 	}
55547e4937aSGao Xiang 
55687ca34a7SGao Xiang 	ret = z_erofs_lookup_pcluster(fe, inode, map);
55764094a04SGao Xiang 	if (ret) {
5585c6dcc57SGao Xiang 		erofs_workgroup_put(&fe->pcl->obj);
55964094a04SGao Xiang 		return ret;
56064094a04SGao Xiang 	}
56164094a04SGao Xiang 
56264094a04SGao Xiang out:
5635c6dcc57SGao Xiang 	z_erofs_pagevec_ctor_init(&fe->vector, Z_EROFS_NR_INLINE_PAGEVECS,
56487ca34a7SGao Xiang 				  fe->pcl->pagevec, fe->pcl->vcnt);
56581382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
5665c6dcc57SGao Xiang 	fe->icpage_ptr = fe->pcl->compressed_pages +
5675c6dcc57SGao Xiang 			z_erofs_pclusterpages(fe->pcl);
56847e4937aSGao Xiang 	return 0;
56947e4937aSGao Xiang }
57047e4937aSGao Xiang 
57147e4937aSGao Xiang /*
57247e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
57347e4937aSGao Xiang  * only after a RCU grace period.
57447e4937aSGao Xiang  */
57547e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
57647e4937aSGao Xiang {
57787ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
57887ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
57947e4937aSGao Xiang }
58047e4937aSGao Xiang 
58147e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
58247e4937aSGao Xiang {
58347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
58447e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
58547e4937aSGao Xiang 
58687ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
58747e4937aSGao Xiang }
58847e4937aSGao Xiang 
5895c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
59047e4937aSGao Xiang {
59187ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
59247e4937aSGao Xiang 
59387ca34a7SGao Xiang 	if (!pcl)
59447e4937aSGao Xiang 		return false;
59547e4937aSGao Xiang 
5965c6dcc57SGao Xiang 	z_erofs_pagevec_ctor_exit(&fe->vector, false);
59787ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
59847e4937aSGao Xiang 
59947e4937aSGao Xiang 	/*
60047e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
60147e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
60247e4937aSGao Xiang 	 */
6035c6dcc57SGao Xiang 	if (fe->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
60487ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
60547e4937aSGao Xiang 
60687ca34a7SGao Xiang 	fe->pcl = NULL;
60747e4937aSGao Xiang 	return true;
60847e4937aSGao Xiang }
60947e4937aSGao Xiang 
61047e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
61147e4937aSGao Xiang 				       unsigned int cachestrategy,
61247e4937aSGao Xiang 				       erofs_off_t la)
61347e4937aSGao Xiang {
61447e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
61547e4937aSGao Xiang 		return false;
61647e4937aSGao Xiang 
61747e4937aSGao Xiang 	if (fe->backmost)
61847e4937aSGao Xiang 		return true;
61947e4937aSGao Xiang 
62047e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
62147e4937aSGao Xiang 		la < fe->headoffset;
62247e4937aSGao Xiang }
62347e4937aSGao Xiang 
62447e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
625eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
62647e4937aSGao Xiang {
62747e4937aSGao Xiang 	struct inode *const inode = fe->inode;
628bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
62947e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
63047e4937aSGao Xiang 	const loff_t offset = page_offset(page);
631dc76ea8cSGao Xiang 	bool tight = true;
63247e4937aSGao Xiang 
63347e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
63447e4937aSGao Xiang 	enum z_erofs_page_type page_type;
63547e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
63647e4937aSGao Xiang 	int err = 0;
63747e4937aSGao Xiang 
63847e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
63947e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
64047e4937aSGao Xiang 
64147e4937aSGao Xiang 	spiltted = 0;
64247e4937aSGao Xiang 	end = PAGE_SIZE;
64347e4937aSGao Xiang repeat:
64447e4937aSGao Xiang 	cur = end - 1;
64547e4937aSGao Xiang 
646*39397a46SGao Xiang 	if (offset + cur < map->m_la ||
647*39397a46SGao Xiang 	    offset + cur >= map->m_la + map->m_llen) {
648*39397a46SGao Xiang 		erofs_dbg("out-of-range map @ pos %llu", offset + cur);
64947e4937aSGao Xiang 
6505c6dcc57SGao Xiang 		if (z_erofs_collector_end(fe))
65147e4937aSGao Xiang 			fe->backmost = false;
65247e4937aSGao Xiang 		map->m_la = offset + cur;
65347e4937aSGao Xiang 		map->m_llen = 0;
65447e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
6558d8a09b0SGao Xiang 		if (err)
65647e4937aSGao Xiang 			goto err_out;
657*39397a46SGao Xiang 	} else {
658*39397a46SGao Xiang 		if (fe->pcl)
659*39397a46SGao Xiang 			goto hitted;
660*39397a46SGao Xiang 		/* didn't get a valid pcluster previously (very rare) */
661*39397a46SGao Xiang 	}
66247e4937aSGao Xiang 
6638d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
66447e4937aSGao Xiang 		goto hitted;
66547e4937aSGao Xiang 
6665c6dcc57SGao Xiang 	err = z_erofs_collector_begin(fe, inode, map);
6678d8a09b0SGao Xiang 	if (err)
66847e4937aSGao Xiang 		goto err_out;
66947e4937aSGao Xiang 
6705c6dcc57SGao Xiang 	if (z_erofs_is_inline_pcluster(fe->pcl)) {
67109c54379SGao Xiang 		void *mp;
672cecf864dSYue Hu 
67309c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
67409c54379SGao Xiang 					erofs_blknr(map->m_pa), EROFS_NO_KMAP);
67509c54379SGao Xiang 		if (IS_ERR(mp)) {
67609c54379SGao Xiang 			err = PTR_ERR(mp);
677cecf864dSYue Hu 			erofs_err(inode->i_sb,
678cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
679cecf864dSYue Hu 			goto err_out;
680cecf864dSYue Hu 		}
68109c54379SGao Xiang 		get_page(fe->map.buf.page);
6825c6dcc57SGao Xiang 		WRITE_ONCE(fe->pcl->compressed_pages[0], fe->map.buf.page);
6835c6dcc57SGao Xiang 		fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
684cecf864dSYue Hu 	} else {
6856f39d1e1SGao Xiang 		/* bind cache first when cached decompression is preferred */
686cecf864dSYue Hu 		if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy,
687cecf864dSYue Hu 					       map->m_la))
6881825c8d7SGao Xiang 			cache_strategy = TRYALLOC;
68947e4937aSGao Xiang 		else
69047e4937aSGao Xiang 			cache_strategy = DONTALLOC;
69147e4937aSGao Xiang 
6926f39d1e1SGao Xiang 		z_erofs_bind_cache(fe, cache_strategy, pagepool);
693cecf864dSYue Hu 	}
69447e4937aSGao Xiang hitted:
695dc76ea8cSGao Xiang 	/*
696dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
697dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
698dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
699dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
700dc76ea8cSGao Xiang 	 */
7015c6dcc57SGao Xiang 	tight &= (fe->mode >= COLLECT_PRIMARY_HOOKED &&
7025c6dcc57SGao Xiang 		  fe->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
703dc76ea8cSGao Xiang 
70447e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
7058d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
70647e4937aSGao Xiang 		zero_user_segment(page, cur, end);
70747e4937aSGao Xiang 		goto next_part;
70847e4937aSGao Xiang 	}
70947e4937aSGao Xiang 
71047e4937aSGao Xiang 	/* let's derive page type */
71147e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
71247e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
71347e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
71447e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
71547e4937aSGao Xiang 
71647e4937aSGao Xiang 	if (cur)
7175c6dcc57SGao Xiang 		tight &= (fe->mode >= COLLECT_PRIMARY_FOLLOWED);
71847e4937aSGao Xiang 
71947e4937aSGao Xiang retry:
7205c6dcc57SGao Xiang 	err = z_erofs_attach_page(fe, page, page_type,
7215c6dcc57SGao Xiang 				  fe->mode >= COLLECT_PRIMARY_FOLLOWED);
7226aaa7b06SGao Xiang 	/* should allocate an additional short-lived page for pagevec */
72347e4937aSGao Xiang 	if (err == -EAGAIN) {
72447e4937aSGao Xiang 		struct page *const newpage =
725e3f78d5eSChao Yu 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
72647e4937aSGao Xiang 
7276aaa7b06SGao Xiang 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
7285c6dcc57SGao Xiang 		err = z_erofs_attach_page(fe, newpage,
72986432a6dSGao Xiang 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
7308d8a09b0SGao Xiang 		if (!err)
73147e4937aSGao Xiang 			goto retry;
73247e4937aSGao Xiang 	}
73347e4937aSGao Xiang 
7348d8a09b0SGao Xiang 	if (err)
73547e4937aSGao Xiang 		goto err_out;
73647e4937aSGao Xiang 
73747e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
73847e4937aSGao Xiang 
73947e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
74047e4937aSGao Xiang 
74147e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
74247e4937aSGao Xiang 	++spiltted;
74347e4937aSGao Xiang 	/* also update nr_pages */
74487ca34a7SGao Xiang 	fe->pcl->nr_pages = max_t(pgoff_t, fe->pcl->nr_pages, index + 1);
74547e4937aSGao Xiang next_part:
74647e4937aSGao Xiang 	/* can be used for verification */
74747e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
74847e4937aSGao Xiang 
74947e4937aSGao Xiang 	end = cur;
75047e4937aSGao Xiang 	if (end > 0)
75147e4937aSGao Xiang 		goto repeat;
75247e4937aSGao Xiang 
75347e4937aSGao Xiang out:
75447e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
75547e4937aSGao Xiang 
7564f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
75747e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
75847e4937aSGao Xiang 	return err;
75947e4937aSGao Xiang 
76047e4937aSGao Xiang 	/* if some error occurred while processing this page */
76147e4937aSGao Xiang err_out:
76247e4937aSGao Xiang 	SetPageError(page);
76347e4937aSGao Xiang 	goto out;
76447e4937aSGao Xiang }
76547e4937aSGao Xiang 
76640452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
76740452ffcSHuang Jianan 				       unsigned int readahead_pages)
76840452ffcSHuang Jianan {
76940452ffcSHuang Jianan 	/* auto: enable for readpage, disable for readahead */
77040452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
77140452ffcSHuang Jianan 	    !readahead_pages)
77240452ffcSHuang Jianan 		return true;
77340452ffcSHuang Jianan 
77440452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
77540452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
77640452ffcSHuang Jianan 		return true;
77740452ffcSHuang Jianan 
77840452ffcSHuang Jianan 	return false;
77940452ffcSHuang Jianan }
78040452ffcSHuang Jianan 
7816aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
7826aaa7b06SGao Xiang {
7836aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
7846aaa7b06SGao Xiang }
7856aaa7b06SGao Xiang 
78647e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
78747e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
788eaa9172aSGao Xiang 				       struct page **pagepool)
78947e4937aSGao Xiang {
79047e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
791cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
79247e4937aSGao Xiang 	struct z_erofs_pagevec_ctor ctor;
7939f6cc76eSGao Xiang 	unsigned int i, inputsize, outputsize, llen, nr_pages;
79447e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
79547e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
79647e4937aSGao Xiang 
79747e4937aSGao Xiang 	enum z_erofs_page_type page_type;
79847e4937aSGao Xiang 	bool overlapped, partial;
79947e4937aSGao Xiang 	int err;
80047e4937aSGao Xiang 
80147e4937aSGao Xiang 	might_sleep();
80287ca34a7SGao Xiang 	DBG_BUGON(!READ_ONCE(pcl->nr_pages));
80347e4937aSGao Xiang 
80487ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
80587ca34a7SGao Xiang 	nr_pages = pcl->nr_pages;
80647e4937aSGao Xiang 
8078d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
80847e4937aSGao Xiang 		pages = pages_onstack;
80947e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
81047e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
81147e4937aSGao Xiang 		pages = z_pagemap_global;
81247e4937aSGao Xiang 	} else {
81347e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
81447e4937aSGao Xiang 
81547e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
81647e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
81747e4937aSGao Xiang 
81847e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
81947e4937aSGao Xiang 				       gfp_flags);
82047e4937aSGao Xiang 
82147e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
8228d8a09b0SGao Xiang 		if (!pages) {
82347e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
82447e4937aSGao Xiang 			pages = z_pagemap_global;
82547e4937aSGao Xiang 		}
82647e4937aSGao Xiang 	}
82747e4937aSGao Xiang 
82847e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
82947e4937aSGao Xiang 		pages[i] = NULL;
83047e4937aSGao Xiang 
83147e4937aSGao Xiang 	err = 0;
83247e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
83387ca34a7SGao Xiang 				  pcl->pagevec, 0);
83447e4937aSGao Xiang 
83587ca34a7SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
83647e4937aSGao Xiang 		unsigned int pagenr;
83747e4937aSGao Xiang 
83847e4937aSGao Xiang 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
83947e4937aSGao Xiang 
84047e4937aSGao Xiang 		/* all pages in pagevec ought to be valid */
84147e4937aSGao Xiang 		DBG_BUGON(!page);
8426aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
84347e4937aSGao Xiang 
8446aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
84547e4937aSGao Xiang 			continue;
84647e4937aSGao Xiang 
84747e4937aSGao Xiang 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
84847e4937aSGao Xiang 			pagenr = 0;
84947e4937aSGao Xiang 		else
85047e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
85147e4937aSGao Xiang 
85247e4937aSGao Xiang 		DBG_BUGON(pagenr >= nr_pages);
85347e4937aSGao Xiang 
85447e4937aSGao Xiang 		/*
85547e4937aSGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
85647e4937aSGao Xiang 		 * so here erroring out one multiref page.
85747e4937aSGao Xiang 		 */
8588d8a09b0SGao Xiang 		if (pages[pagenr]) {
85947e4937aSGao Xiang 			DBG_BUGON(1);
86047e4937aSGao Xiang 			SetPageError(pages[pagenr]);
86147e4937aSGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
86247e4937aSGao Xiang 			err = -EFSCORRUPTED;
86347e4937aSGao Xiang 		}
86447e4937aSGao Xiang 		pages[pagenr] = page;
86547e4937aSGao Xiang 	}
86647e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&ctor, true);
86747e4937aSGao Xiang 
86847e4937aSGao Xiang 	overlapped = false;
86947e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
87047e4937aSGao Xiang 
871cecf864dSYue Hu 	for (i = 0; i < pclusterpages; ++i) {
87247e4937aSGao Xiang 		unsigned int pagenr;
87347e4937aSGao Xiang 
87447e4937aSGao Xiang 		page = compressed_pages[i];
87547e4937aSGao Xiang 		/* all compressed pages ought to be valid */
87647e4937aSGao Xiang 		DBG_BUGON(!page);
87747e4937aSGao Xiang 
878cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
879cecf864dSYue Hu 			if (!PageUptodate(page))
880cecf864dSYue Hu 				err = -EIO;
881cecf864dSYue Hu 			continue;
882cecf864dSYue Hu 		}
883cecf864dSYue Hu 
884cecf864dSYue Hu 		DBG_BUGON(z_erofs_page_is_invalidated(page));
8856aaa7b06SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
88647e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
8878d8a09b0SGao Xiang 				if (!PageUptodate(page))
88847e4937aSGao Xiang 					err = -EIO;
88947e4937aSGao Xiang 				continue;
89047e4937aSGao Xiang 			}
89147e4937aSGao Xiang 
89247e4937aSGao Xiang 			/*
89347e4937aSGao Xiang 			 * only if non-head page can be selected
89447e4937aSGao Xiang 			 * for inplace decompression
89547e4937aSGao Xiang 			 */
89647e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
89747e4937aSGao Xiang 
89847e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
8998d8a09b0SGao Xiang 			if (pages[pagenr]) {
90047e4937aSGao Xiang 				DBG_BUGON(1);
90147e4937aSGao Xiang 				SetPageError(pages[pagenr]);
90247e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
90347e4937aSGao Xiang 				err = -EFSCORRUPTED;
90447e4937aSGao Xiang 			}
90547e4937aSGao Xiang 			pages[pagenr] = page;
90647e4937aSGao Xiang 
90747e4937aSGao Xiang 			overlapped = true;
90847e4937aSGao Xiang 		}
90947e4937aSGao Xiang 
9106aaa7b06SGao Xiang 		/* PG_error needs checking for all non-managed pages */
9118d8a09b0SGao Xiang 		if (PageError(page)) {
91247e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
91347e4937aSGao Xiang 			err = -EIO;
91447e4937aSGao Xiang 		}
91547e4937aSGao Xiang 	}
91647e4937aSGao Xiang 
9178d8a09b0SGao Xiang 	if (err)
91847e4937aSGao Xiang 		goto out;
91947e4937aSGao Xiang 
92047e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
92187ca34a7SGao Xiang 	if (nr_pages << PAGE_SHIFT >= pcl->pageofs_out + llen) {
92247e4937aSGao Xiang 		outputsize = llen;
92347e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
92447e4937aSGao Xiang 	} else {
92587ca34a7SGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - pcl->pageofs_out;
92647e4937aSGao Xiang 		partial = true;
92747e4937aSGao Xiang 	}
92847e4937aSGao Xiang 
929cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
930cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
931cecf864dSYue Hu 	else
932cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
933cecf864dSYue Hu 
93447e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
93547e4937aSGao Xiang 					.sb = sb,
93647e4937aSGao Xiang 					.in = compressed_pages,
93747e4937aSGao Xiang 					.out = pages,
938cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
93987ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
9409f6cc76eSGao Xiang 					.inputsize = inputsize,
94147e4937aSGao Xiang 					.outputsize = outputsize,
94247e4937aSGao Xiang 					.alg = pcl->algorithmformat,
94347e4937aSGao Xiang 					.inplace_io = overlapped,
94447e4937aSGao Xiang 					.partial_decoding = partial
94547e4937aSGao Xiang 				 }, pagepool);
94647e4937aSGao Xiang 
94747e4937aSGao Xiang out:
948cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
949cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
950cecf864dSYue Hu 		page = compressed_pages[0];
951cecf864dSYue Hu 		WRITE_ONCE(compressed_pages[0], NULL);
952cecf864dSYue Hu 		put_page(page);
953cecf864dSYue Hu 	} else {
954cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
95547e4937aSGao Xiang 			page = compressed_pages[i];
95647e4937aSGao Xiang 
95747e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
95847e4937aSGao Xiang 				continue;
95947e4937aSGao Xiang 
9606aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
9616aaa7b06SGao Xiang 			(void)z_erofs_put_shortlivedpage(pagepool, page);
96247e4937aSGao Xiang 			WRITE_ONCE(compressed_pages[i], NULL);
96347e4937aSGao Xiang 		}
964cecf864dSYue Hu 	}
96547e4937aSGao Xiang 
96647e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
96747e4937aSGao Xiang 		page = pages[i];
96847e4937aSGao Xiang 		if (!page)
96947e4937aSGao Xiang 			continue;
97047e4937aSGao Xiang 
9716aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
97247e4937aSGao Xiang 
9736aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
9746aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
97547e4937aSGao Xiang 			continue;
97647e4937aSGao Xiang 
9778d8a09b0SGao Xiang 		if (err < 0)
97847e4937aSGao Xiang 			SetPageError(page);
97947e4937aSGao Xiang 
98047e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
98147e4937aSGao Xiang 	}
98247e4937aSGao Xiang 
98347e4937aSGao Xiang 	if (pages == z_pagemap_global)
98447e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
9858d8a09b0SGao Xiang 	else if (pages != pages_onstack)
98647e4937aSGao Xiang 		kvfree(pages);
98747e4937aSGao Xiang 
98887ca34a7SGao Xiang 	pcl->nr_pages = 0;
98987ca34a7SGao Xiang 	pcl->vcnt = 0;
99047e4937aSGao Xiang 
99187ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
99247e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
99387ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
99447e4937aSGao Xiang 	return err;
99547e4937aSGao Xiang }
99647e4937aSGao Xiang 
9970c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
998eaa9172aSGao Xiang 				     struct page **pagepool)
99947e4937aSGao Xiang {
100047e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
100147e4937aSGao Xiang 
100247e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
100347e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
100447e4937aSGao Xiang 
100547e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
100647e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
100747e4937aSGao Xiang 
100847e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
100947e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
101047e4937aSGao Xiang 
101147e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
101247e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
101347e4937aSGao Xiang 
1014a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
101587ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
101647e4937aSGao Xiang 	}
101747e4937aSGao Xiang }
101847e4937aSGao Xiang 
10190c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
102047e4937aSGao Xiang {
1021a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1022a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1023eaa9172aSGao Xiang 	struct page *pagepool = NULL;
102447e4937aSGao Xiang 
1025a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
10260c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
102747e4937aSGao Xiang 
1028eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1029a4b1fab1SGao Xiang 	kvfree(bgq);
103047e4937aSGao Xiang }
103147e4937aSGao Xiang 
10327865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
10337865827cSGao Xiang 				       bool sync, int bios)
10347865827cSGao Xiang {
10357865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
10367865827cSGao Xiang 
10377865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
10387865827cSGao Xiang 	if (sync) {
10397865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
104060b30050SHongyu Jin 			complete(&io->u.done);
104160b30050SHongyu Jin 
10427865827cSGao Xiang 		return;
10437865827cSGao Xiang 	}
10447865827cSGao Xiang 
10457865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
10467865827cSGao Xiang 		return;
10477865827cSGao Xiang 	/* Use workqueue and sync decompression for atomic contexts only */
10487865827cSGao Xiang 	if (in_atomic() || irqs_disabled()) {
10497865827cSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
10507865827cSGao Xiang 		/* enable sync decompression for readahead */
10517865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
10527865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
10537865827cSGao Xiang 		return;
10547865827cSGao Xiang 	}
10557865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
10567865827cSGao Xiang }
10577865827cSGao Xiang 
105847e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
105947e4937aSGao Xiang 					       unsigned int nr,
1060eaa9172aSGao Xiang 					       struct page **pagepool,
10619f2731d6SGao Xiang 					       struct address_space *mc)
106247e4937aSGao Xiang {
106347e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
10649f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
106547e4937aSGao Xiang 	bool tocache = false;
106647e4937aSGao Xiang 
106747e4937aSGao Xiang 	struct address_space *mapping;
106847e4937aSGao Xiang 	struct page *oldpage, *page;
106947e4937aSGao Xiang 
107047e4937aSGao Xiang 	compressed_page_t t;
107147e4937aSGao Xiang 	int justfound;
107247e4937aSGao Xiang 
107347e4937aSGao Xiang repeat:
107447e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
107547e4937aSGao Xiang 	oldpage = page;
107647e4937aSGao Xiang 
107747e4937aSGao Xiang 	if (!page)
107847e4937aSGao Xiang 		goto out_allocpage;
107947e4937aSGao Xiang 
108047e4937aSGao Xiang 	/* process the target tagged pointer */
108147e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
108247e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
108347e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
108447e4937aSGao Xiang 
10851825c8d7SGao Xiang 	/*
10861825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
10871825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
10881825c8d7SGao Xiang 	 */
10891825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
10901825c8d7SGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
10911825c8d7SGao Xiang 		set_page_private(page, 0);
10921825c8d7SGao Xiang 		tocache = true;
10931825c8d7SGao Xiang 		goto out_tocache;
10941825c8d7SGao Xiang 	}
109547e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
109647e4937aSGao Xiang 
109747e4937aSGao Xiang 	/*
10986aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
109947e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
110047e4937aSGao Xiang 	 */
110147e4937aSGao Xiang 	if (mapping && mapping != mc)
110247e4937aSGao Xiang 		/* ought to be unmanaged pages */
110347e4937aSGao Xiang 		goto out;
110447e4937aSGao Xiang 
11056aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
11066aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
11076aaa7b06SGao Xiang 		goto out;
11086aaa7b06SGao Xiang 
110947e4937aSGao Xiang 	lock_page(page);
111047e4937aSGao Xiang 
111147e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
111247e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
111347e4937aSGao Xiang 
111447e4937aSGao Xiang 	/* the page is still in manage cache */
111547e4937aSGao Xiang 	if (page->mapping == mc) {
111647e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
111747e4937aSGao Xiang 
111847e4937aSGao Xiang 		ClearPageError(page);
111947e4937aSGao Xiang 		if (!PagePrivate(page)) {
112047e4937aSGao Xiang 			/*
112147e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
112247e4937aSGao Xiang 			 * the current restriction as well if
112347e4937aSGao Xiang 			 * the page is already in compressed_pages[].
112447e4937aSGao Xiang 			 */
112547e4937aSGao Xiang 			DBG_BUGON(!justfound);
112647e4937aSGao Xiang 
112747e4937aSGao Xiang 			justfound = 0;
112847e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
112947e4937aSGao Xiang 			SetPagePrivate(page);
113047e4937aSGao Xiang 		}
113147e4937aSGao Xiang 
113247e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
113347e4937aSGao Xiang 		if (PageUptodate(page)) {
113447e4937aSGao Xiang 			unlock_page(page);
113547e4937aSGao Xiang 			page = NULL;
113647e4937aSGao Xiang 		}
113747e4937aSGao Xiang 		goto out;
113847e4937aSGao Xiang 	}
113947e4937aSGao Xiang 
114047e4937aSGao Xiang 	/*
114147e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
114247e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
114347e4937aSGao Xiang 	 */
114447e4937aSGao Xiang 	DBG_BUGON(page->mapping);
114547e4937aSGao Xiang 	DBG_BUGON(!justfound);
114647e4937aSGao Xiang 
114747e4937aSGao Xiang 	tocache = true;
114847e4937aSGao Xiang 	unlock_page(page);
114947e4937aSGao Xiang 	put_page(page);
115047e4937aSGao Xiang out_allocpage:
11515ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
11525ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1153eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
11545ddcee1fSGao Xiang 		cond_resched();
11555ddcee1fSGao Xiang 		goto repeat;
11565ddcee1fSGao Xiang 	}
11571825c8d7SGao Xiang out_tocache:
1158bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1159bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1160bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1161bf225074SGao Xiang 		goto out;
1162a30573b3SGao Xiang 	}
1163bf225074SGao Xiang 	attach_page_private(page, pcl);
1164bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1165bf225074SGao Xiang 	put_page(page);
1166bf225074SGao Xiang 
116747e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
116847e4937aSGao Xiang 	return page;
116947e4937aSGao Xiang }
117047e4937aSGao Xiang 
1171a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1172a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1173a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
117447e4937aSGao Xiang {
1175a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
117647e4937aSGao Xiang 
1177a4b1fab1SGao Xiang 	if (fg && !*fg) {
1178a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1179a4b1fab1SGao Xiang 		if (!q) {
1180a4b1fab1SGao Xiang 			*fg = true;
1181a4b1fab1SGao Xiang 			goto fg_out;
118247e4937aSGao Xiang 		}
11830c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1184a4b1fab1SGao Xiang 	} else {
1185a4b1fab1SGao Xiang fg_out:
1186a4b1fab1SGao Xiang 		q = fgq;
118760b30050SHongyu Jin 		init_completion(&fgq->u.done);
1188a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1189a4b1fab1SGao Xiang 	}
1190a4b1fab1SGao Xiang 	q->sb = sb;
1191a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1192a4b1fab1SGao Xiang 	return q;
119347e4937aSGao Xiang }
119447e4937aSGao Xiang 
119547e4937aSGao Xiang /* define decompression jobqueue types */
119647e4937aSGao Xiang enum {
119747e4937aSGao Xiang 	JQ_BYPASS,
119847e4937aSGao Xiang 	JQ_SUBMIT,
119947e4937aSGao Xiang 	NR_JOBQUEUES,
120047e4937aSGao Xiang };
120147e4937aSGao Xiang 
120247e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1203a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1204a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
120547e4937aSGao Xiang {
120647e4937aSGao Xiang 	/*
120747e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
120847e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
120947e4937aSGao Xiang 	 */
1210a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1211a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
121247e4937aSGao Xiang 
1213a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
121447e4937aSGao Xiang }
121547e4937aSGao Xiang 
121647e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
121747e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
121847e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
121947e4937aSGao Xiang {
122047e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
122147e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
122247e4937aSGao Xiang 
122347e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
122447e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
122547e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
122647e4937aSGao Xiang 
122747e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
122847e4937aSGao Xiang 
122947e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
123047e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
123147e4937aSGao Xiang 
123247e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
123347e4937aSGao Xiang }
123447e4937aSGao Xiang 
12357865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
12367865827cSGao Xiang {
12377865827cSGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
12387865827cSGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
12397865827cSGao Xiang 	blk_status_t err = bio->bi_status;
12407865827cSGao Xiang 	struct bio_vec *bvec;
12417865827cSGao Xiang 	struct bvec_iter_all iter_all;
12427865827cSGao Xiang 
12437865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
12447865827cSGao Xiang 		struct page *page = bvec->bv_page;
12457865827cSGao Xiang 
12467865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
12477865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
12487865827cSGao Xiang 
12497865827cSGao Xiang 		if (err)
12507865827cSGao Xiang 			SetPageError(page);
12517865827cSGao Xiang 
12527865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
12537865827cSGao Xiang 			if (!err)
12547865827cSGao Xiang 				SetPageUptodate(page);
12557865827cSGao Xiang 			unlock_page(page);
12567865827cSGao Xiang 		}
12577865827cSGao Xiang 	}
12587865827cSGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
12597865827cSGao Xiang 	bio_put(bio);
12607865827cSGao Xiang }
12617865827cSGao Xiang 
12621e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb,
12636ea5aad3SGao Xiang 				 struct z_erofs_decompress_frontend *f,
1264eaa9172aSGao Xiang 				 struct page **pagepool,
1265a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1266a4b1fab1SGao Xiang 				 bool *force_fg)
126747e4937aSGao Xiang {
1268bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
126947e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1270a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
127147e4937aSGao Xiang 	void *bi_private;
12725c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1273dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
12743f649ab7SKees Cook 	pgoff_t last_index;
1275dfeab2e9SGao Xiang 	struct block_device *last_bdev;
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 {
1287dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
128847e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
12891e4a2955SGao Xiang 		pgoff_t cur, end;
12901e4a2955SGao Xiang 		unsigned int i = 0;
12911e4a2955SGao Xiang 		bool bypass = true;
129247e4937aSGao Xiang 
129347e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
129447e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
129547e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
129647e4937aSGao Xiang 
129747e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
129847e4937aSGao Xiang 
1299cecf864dSYue Hu 		/* close the main owned chain at first */
1300cecf864dSYue Hu 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1301cecf864dSYue Hu 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1302cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1303cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1304cecf864dSYue Hu 			continue;
1305cecf864dSYue Hu 		}
1306cecf864dSYue Hu 
1307dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1308dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
1309dfeab2e9SGao Xiang 			.m_pa = blknr_to_addr(pcl->obj.index),
1310dfeab2e9SGao Xiang 		};
1311dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1312dfeab2e9SGao Xiang 
1313dfeab2e9SGao Xiang 		cur = erofs_blknr(mdev.m_pa);
13149f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
131547e4937aSGao Xiang 
13161e4a2955SGao Xiang 		do {
13171e4a2955SGao Xiang 			struct page *page;
131847e4937aSGao Xiang 
13191e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
13209f2731d6SGao Xiang 							  MNGD_MAPPING(sbi));
13211e4a2955SGao Xiang 			if (!page)
13221e4a2955SGao Xiang 				continue;
132347e4937aSGao Xiang 
1324dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1325dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
132647e4937aSGao Xiang submit_bio_retry:
132794e4e153SGao Xiang 				submit_bio(bio);
132847e4937aSGao Xiang 				bio = NULL;
132947e4937aSGao Xiang 			}
133047e4937aSGao Xiang 
133147e4937aSGao Xiang 			if (!bio) {
133207888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
133307888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
13340c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1335dfeab2e9SGao Xiang 
1336dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
13371e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1338a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1339a5c0b780SGao Xiang 				bio->bi_private = bi_private;
13406ea5aad3SGao Xiang 				if (f->readahead)
13416ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
134247e4937aSGao Xiang 				++nr_bios;
134347e4937aSGao Xiang 			}
134447e4937aSGao Xiang 
13456c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
134647e4937aSGao Xiang 				goto submit_bio_retry;
134747e4937aSGao Xiang 
13481e4a2955SGao Xiang 			last_index = cur;
13491e4a2955SGao Xiang 			bypass = false;
13501e4a2955SGao Xiang 		} while (++cur < end);
135147e4937aSGao Xiang 
13521e4a2955SGao Xiang 		if (!bypass)
135347e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
135447e4937aSGao Xiang 		else
135547e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
135647e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
135747e4937aSGao Xiang 
135847e4937aSGao Xiang 	if (bio)
135994e4e153SGao Xiang 		submit_bio(bio);
136047e4937aSGao Xiang 
1361587a67b7SGao Xiang 	/*
1362587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1363587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1364587a67b7SGao Xiang 	 */
1365587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1366587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
13671e4a2955SGao Xiang 		return;
1368587a67b7SGao Xiang 	}
1369a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
137047e4937aSGao Xiang }
137147e4937aSGao Xiang 
13720c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb,
13736ea5aad3SGao Xiang 			     struct z_erofs_decompress_frontend *f,
1374eaa9172aSGao Xiang 			     struct page **pagepool, bool force_fg)
137547e4937aSGao Xiang {
1376a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
137747e4937aSGao Xiang 
13785c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
137947e4937aSGao Xiang 		return;
13806ea5aad3SGao Xiang 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
138147e4937aSGao Xiang 
13820c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
13830c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
138447e4937aSGao Xiang 
138547e4937aSGao Xiang 	if (!force_fg)
138647e4937aSGao Xiang 		return;
138747e4937aSGao Xiang 
138847e4937aSGao Xiang 	/* wait until all bios are completed */
138960b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
139047e4937aSGao Xiang 
13910c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
13920c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
139347e4937aSGao Xiang }
139447e4937aSGao Xiang 
139538629291SGao Xiang /*
139638629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
139738629291SGao Xiang  * approximate readmore strategies as a start.
139838629291SGao Xiang  */
139938629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
140038629291SGao Xiang 				      struct readahead_control *rac,
140138629291SGao Xiang 				      erofs_off_t end,
1402eaa9172aSGao Xiang 				      struct page **pagepool,
140338629291SGao Xiang 				      bool backmost)
140438629291SGao Xiang {
140538629291SGao Xiang 	struct inode *inode = f->inode;
140638629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
140738629291SGao Xiang 	erofs_off_t cur;
140838629291SGao Xiang 	int err;
140938629291SGao Xiang 
141038629291SGao Xiang 	if (backmost) {
141138629291SGao Xiang 		map->m_la = end;
1412622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1413622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
141438629291SGao Xiang 		if (err)
141538629291SGao Xiang 			return;
141638629291SGao Xiang 
141738629291SGao Xiang 		/* expend ra for the trailing edge if readahead */
141838629291SGao Xiang 		if (rac) {
141938629291SGao Xiang 			loff_t newstart = readahead_pos(rac);
142038629291SGao Xiang 
142138629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
142238629291SGao Xiang 			readahead_expand(rac, newstart, cur - newstart);
142338629291SGao Xiang 			return;
142438629291SGao Xiang 		}
142538629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
142638629291SGao Xiang 	} else {
142738629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
142838629291SGao Xiang 
142938629291SGao Xiang 		if (!map->m_llen)
143038629291SGao Xiang 			return;
143138629291SGao Xiang 	}
143238629291SGao Xiang 
143338629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
143438629291SGao Xiang 	while (cur >= end) {
143538629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
143638629291SGao Xiang 		struct page *page;
143738629291SGao Xiang 
143838629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
143938629291SGao Xiang 		if (!page)
144038629291SGao Xiang 			goto skip;
144138629291SGao Xiang 
144238629291SGao Xiang 		if (PageUptodate(page)) {
144338629291SGao Xiang 			unlock_page(page);
144438629291SGao Xiang 			put_page(page);
144538629291SGao Xiang 			goto skip;
144638629291SGao Xiang 		}
144738629291SGao Xiang 
144838629291SGao Xiang 		err = z_erofs_do_read_page(f, page, pagepool);
144938629291SGao Xiang 		if (err)
145038629291SGao Xiang 			erofs_err(inode->i_sb,
145138629291SGao Xiang 				  "readmore error at page %lu @ nid %llu",
145238629291SGao Xiang 				  index, EROFS_I(inode)->nid);
145338629291SGao Xiang 		put_page(page);
145438629291SGao Xiang skip:
145538629291SGao Xiang 		if (cur < PAGE_SIZE)
145638629291SGao Xiang 			break;
145738629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
145838629291SGao Xiang 	}
145938629291SGao Xiang }
146038629291SGao Xiang 
14610c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page)
146247e4937aSGao Xiang {
146347e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
146440452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
146547e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1466eaa9172aSGao Xiang 	struct page *pagepool = NULL;
146747e4937aSGao Xiang 	int err;
146847e4937aSGao Xiang 
146947e4937aSGao Xiang 	trace_erofs_readpage(page, false);
147047e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
147147e4937aSGao Xiang 
147238629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
147338629291SGao Xiang 				  &pagepool, true);
14741825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
147538629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
147638629291SGao Xiang 
14775c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
147847e4937aSGao Xiang 
147947e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
148040452ffcSHuang Jianan 	z_erofs_runqueue(inode->i_sb, &f, &pagepool,
148140452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, 0));
148247e4937aSGao Xiang 
148347e4937aSGao Xiang 	if (err)
14844f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
148547e4937aSGao Xiang 
148609c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1487eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
148847e4937aSGao Xiang 	return err;
148947e4937aSGao Xiang }
149047e4937aSGao Xiang 
14910615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
149247e4937aSGao Xiang {
14930615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
149447e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
149547e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1496eaa9172aSGao Xiang 	struct page *pagepool = NULL, *head = NULL, *page;
149738629291SGao Xiang 	unsigned int nr_pages;
149847e4937aSGao Xiang 
14996ea5aad3SGao Xiang 	f.readahead = true;
15000615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
150147e4937aSGao Xiang 
150238629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, f.headoffset +
150338629291SGao Xiang 				  readahead_length(rac) - 1, &pagepool, true);
150438629291SGao Xiang 	nr_pages = readahead_count(rac);
150538629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
150638629291SGao Xiang 
15070615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
150847e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
150947e4937aSGao Xiang 		head = page;
151047e4937aSGao Xiang 	}
151147e4937aSGao Xiang 
151247e4937aSGao Xiang 	while (head) {
151347e4937aSGao Xiang 		struct page *page = head;
151447e4937aSGao Xiang 		int err;
151547e4937aSGao Xiang 
151647e4937aSGao Xiang 		/* traversal in reverse order */
151747e4937aSGao Xiang 		head = (void *)page_private(page);
151847e4937aSGao Xiang 
15191825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1520a5876e24SGao Xiang 		if (err)
15214f761fa2SGao Xiang 			erofs_err(inode->i_sb,
15224f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
15234f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
152447e4937aSGao Xiang 		put_page(page);
152547e4937aSGao Xiang 	}
152638629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
15275c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
152847e4937aSGao Xiang 
152938629291SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool,
153040452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
153109c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1532eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
153347e4937aSGao Xiang }
153447e4937aSGao Xiang 
15350c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
15360c638f70SGao Xiang 	.readpage = z_erofs_readpage,
15370615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
153847e4937aSGao Xiang };
1539