xref: /openbmc/linux/fs/erofs/zdata.c (revision 40452ffc)
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 {
859f6cc76eSGao Xiang 	int i;
869f6cc76eSGao Xiang 
879f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
889f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
899f6cc76eSGao Xiang 
909f6cc76eSGao Xiang 		if (pcl->pclusterpages > pcs->maxpages)
919f6cc76eSGao Xiang 			continue;
929f6cc76eSGao Xiang 
939f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
949f6cc76eSGao Xiang 		return;
959f6cc76eSGao Xiang 	}
969f6cc76eSGao Xiang 	DBG_BUGON(1);
979f6cc76eSGao Xiang }
989f6cc76eSGao Xiang 
9947e4937aSGao Xiang /* how to allocate cached pages for a pcluster */
10047e4937aSGao Xiang enum z_erofs_cache_alloctype {
10147e4937aSGao Xiang 	DONTALLOC,	/* don't allocate any cached pages */
1021825c8d7SGao Xiang 	/*
1031825c8d7SGao Xiang 	 * try to use cached I/O if page allocation succeeds or fallback
1041825c8d7SGao Xiang 	 * to in-place I/O instead to avoid any direct reclaim.
1051825c8d7SGao Xiang 	 */
1061825c8d7SGao Xiang 	TRYALLOC,
10747e4937aSGao Xiang };
10847e4937aSGao Xiang 
10947e4937aSGao Xiang /*
11047e4937aSGao Xiang  * tagged pointer with 1-bit tag for all compressed pages
11147e4937aSGao Xiang  * tag 0 - the page is just found with an extra page reference
11247e4937aSGao Xiang  */
11347e4937aSGao Xiang typedef tagptr1_t compressed_page_t;
11447e4937aSGao Xiang 
11547e4937aSGao Xiang #define tag_compressed_page_justfound(page) \
11647e4937aSGao Xiang 	tagptr_fold(compressed_page_t, page, 1)
11747e4937aSGao Xiang 
11847e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
11947e4937aSGao Xiang 
12047e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
12147e4937aSGao Xiang {
12247e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
1239f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
12447e4937aSGao Xiang }
12547e4937aSGao Xiang 
12699634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
12747e4937aSGao Xiang {
12847e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
12947e4937aSGao Xiang 
13047e4937aSGao Xiang 	/*
13147e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
13247e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
13347e4937aSGao Xiang 	 */
1340e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
1350e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
13647e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
13747e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
13847e4937aSGao Xiang }
13947e4937aSGao Xiang 
14047e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
14147e4937aSGao Xiang {
1429f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
14347e4937aSGao Xiang 
1449f6cc76eSGao Xiang 	if (err)
1459f6cc76eSGao Xiang 		return err;
1469f6cc76eSGao Xiang 	err = z_erofs_init_workqueue();
1479f6cc76eSGao Xiang 	if (err)
1489f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
1499f6cc76eSGao Xiang 	return err;
15047e4937aSGao Xiang }
15147e4937aSGao Xiang 
15247e4937aSGao Xiang enum z_erofs_collectmode {
15347e4937aSGao Xiang 	COLLECT_SECONDARY,
15447e4937aSGao Xiang 	COLLECT_PRIMARY,
15547e4937aSGao Xiang 	/*
15647e4937aSGao Xiang 	 * The current collection was the tail of an exist chain, in addition
15747e4937aSGao Xiang 	 * that the previous processed chained collections are all decided to
15847e4937aSGao Xiang 	 * be hooked up to it.
15947e4937aSGao Xiang 	 * A new chain will be created for the remaining collections which are
16047e4937aSGao Xiang 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
16147e4937aSGao Xiang 	 * the next collection cannot reuse the whole page safely in
16247e4937aSGao Xiang 	 * the following scenario:
16347e4937aSGao Xiang 	 *  ________________________________________________________________
16447e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
16547e4937aSGao Xiang 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
16647e4937aSGao Xiang 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
16747e4937aSGao Xiang 	 */
16847e4937aSGao Xiang 	COLLECT_PRIMARY_HOOKED,
1690b964600SGao Xiang 	/*
1700b964600SGao Xiang 	 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
1710b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
1720b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
1730b964600SGao Xiang 	 * pagevec) since it can be directly decoded without I/O submission.
1740b964600SGao Xiang 	 */
17547e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
17647e4937aSGao Xiang 	/*
17747e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
17847e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
17947e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
18047e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
18147e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
18247e4937aSGao Xiang 	 * illustrated below:
18347e4937aSGao Xiang 	 *  ________________________________________________________________
18447e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
18547e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
18647e4937aSGao Xiang 	 * |  PRIMARY_FOLLOWED or |                                        |
18747e4937aSGao Xiang 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
18847e4937aSGao Xiang 	 *
18947e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
19047e4937aSGao Xiang 	 */
19147e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED,
19247e4937aSGao Xiang };
19347e4937aSGao Xiang 
19447e4937aSGao Xiang struct z_erofs_collector {
19547e4937aSGao Xiang 	struct z_erofs_pagevec_ctor vector;
19647e4937aSGao Xiang 
19747e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
19847e4937aSGao Xiang 	struct z_erofs_collection *cl;
19981382f5fSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
20081382f5fSGao Xiang 	struct page **icpage_ptr;
20147e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
20247e4937aSGao Xiang 
20347e4937aSGao Xiang 	enum z_erofs_collectmode mode;
20447e4937aSGao Xiang };
20547e4937aSGao Xiang 
20647e4937aSGao Xiang struct z_erofs_decompress_frontend {
20747e4937aSGao Xiang 	struct inode *const inode;
20847e4937aSGao Xiang 
20947e4937aSGao Xiang 	struct z_erofs_collector clt;
21047e4937aSGao Xiang 	struct erofs_map_blocks map;
21147e4937aSGao Xiang 
2126ea5aad3SGao Xiang 	bool readahead;
21347e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
21447e4937aSGao Xiang 	bool backmost;
21547e4937aSGao Xiang 	erofs_off_t headoffset;
21647e4937aSGao Xiang };
21747e4937aSGao Xiang 
21847e4937aSGao Xiang #define COLLECTOR_INIT() { \
21947e4937aSGao Xiang 	.owned_head = Z_EROFS_PCLUSTER_TAIL, \
22047e4937aSGao Xiang 	.mode = COLLECT_PRIMARY_FOLLOWED }
22147e4937aSGao Xiang 
22247e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
22347e4937aSGao Xiang 	.inode = __i, .clt = COLLECTOR_INIT(), \
22447e4937aSGao Xiang 	.backmost = true, }
22547e4937aSGao Xiang 
22647e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
22747e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
22847e4937aSGao Xiang 
22947e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt,
23047e4937aSGao Xiang 				     struct address_space *mc,
2311825c8d7SGao Xiang 				     enum z_erofs_cache_alloctype type,
232eaa9172aSGao Xiang 				     struct page **pagepool)
23347e4937aSGao Xiang {
23481382f5fSGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
23547e4937aSGao Xiang 	bool standalone = true;
2361825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
2371825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
23881382f5fSGao Xiang 	struct page **pages;
23981382f5fSGao Xiang 	pgoff_t index;
24047e4937aSGao Xiang 
24147e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
24247e4937aSGao Xiang 		return;
24347e4937aSGao Xiang 
24481382f5fSGao Xiang 	pages = pcl->compressed_pages;
24581382f5fSGao Xiang 	index = pcl->obj.index;
24681382f5fSGao Xiang 	for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
24747e4937aSGao Xiang 		struct page *page;
24847e4937aSGao Xiang 		compressed_page_t t;
2491825c8d7SGao Xiang 		struct page *newpage = NULL;
25047e4937aSGao Xiang 
25147e4937aSGao Xiang 		/* the compressed page was loaded before */
25247e4937aSGao Xiang 		if (READ_ONCE(*pages))
25347e4937aSGao Xiang 			continue;
25447e4937aSGao Xiang 
25547e4937aSGao Xiang 		page = find_get_page(mc, index);
25647e4937aSGao Xiang 
25747e4937aSGao Xiang 		if (page) {
25847e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
2590b964600SGao Xiang 		} else {
2600b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
2610b964600SGao Xiang 			standalone = false;
2620b964600SGao Xiang 			switch (type) {
2630b964600SGao Xiang 			case TRYALLOC:
2641825c8d7SGao Xiang 				newpage = erofs_allocpage(pagepool, gfp);
2651825c8d7SGao Xiang 				if (!newpage)
26647e4937aSGao Xiang 					continue;
2670b964600SGao Xiang 				set_page_private(newpage,
2680b964600SGao Xiang 						 Z_EROFS_PREALLOCATED_PAGE);
2690b964600SGao Xiang 				t = tag_compressed_page_justfound(newpage);
2700b964600SGao Xiang 				break;
2710b964600SGao Xiang 			default:        /* DONTALLOC */
2720b964600SGao Xiang 				continue;
2730b964600SGao Xiang 			}
27447e4937aSGao Xiang 		}
27547e4937aSGao Xiang 
27647e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
27747e4937aSGao Xiang 			continue;
27847e4937aSGao Xiang 
279eaa9172aSGao Xiang 		if (page)
28047e4937aSGao Xiang 			put_page(page);
281eaa9172aSGao Xiang 		else if (newpage)
282eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
28347e4937aSGao Xiang 	}
28447e4937aSGao Xiang 
2850b964600SGao Xiang 	/*
2860b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
2870b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
2880b964600SGao Xiang 	 */
2890b964600SGao Xiang 	if (standalone)
29047e4937aSGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
29147e4937aSGao Xiang }
29247e4937aSGao Xiang 
29347e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
29447e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
29547e4937aSGao Xiang 				       struct erofs_workgroup *grp)
29647e4937aSGao Xiang {
29747e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
29847e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
29947e4937aSGao Xiang 	int i;
30047e4937aSGao Xiang 
30147e4937aSGao Xiang 	/*
30247e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
30347e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
30447e4937aSGao Xiang 	 */
3059f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
30647e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
30747e4937aSGao Xiang 
30847e4937aSGao Xiang 		if (!page)
30947e4937aSGao Xiang 			continue;
31047e4937aSGao Xiang 
31147e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
31247e4937aSGao Xiang 		if (!trylock_page(page))
31347e4937aSGao Xiang 			return -EBUSY;
31447e4937aSGao Xiang 
315f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
31647e4937aSGao Xiang 			continue;
31747e4937aSGao Xiang 
31847e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
31947e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
3206aaa7b06SGao Xiang 		detach_page_private(page);
32147e4937aSGao Xiang 		unlock_page(page);
32247e4937aSGao Xiang 	}
32347e4937aSGao Xiang 	return 0;
32447e4937aSGao Xiang }
32547e4937aSGao Xiang 
326d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
32747e4937aSGao Xiang {
32847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
32947e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
33047e4937aSGao Xiang 
33147e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
33247e4937aSGao Xiang 		unsigned int i;
33347e4937aSGao Xiang 
3349f6cc76eSGao Xiang 		for (i = 0; i < pcl->pclusterpages; ++i) {
33547e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
33647e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
33747e4937aSGao Xiang 				ret = 1;
33847e4937aSGao Xiang 				break;
33947e4937aSGao Xiang 			}
34047e4937aSGao Xiang 		}
34147e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
34247e4937aSGao Xiang 
3436aaa7b06SGao Xiang 		if (ret)
3446aaa7b06SGao Xiang 			detach_page_private(page);
34547e4937aSGao Xiang 	}
34647e4937aSGao Xiang 	return ret;
34747e4937aSGao Xiang }
34847e4937aSGao Xiang 
34947e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
35081382f5fSGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
35147e4937aSGao Xiang 				   struct page *page)
35247e4937aSGao Xiang {
35347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = clt->pcl;
35447e4937aSGao Xiang 
35581382f5fSGao Xiang 	while (clt->icpage_ptr > pcl->compressed_pages)
35681382f5fSGao Xiang 		if (!cmpxchg(--clt->icpage_ptr, NULL, page))
35747e4937aSGao Xiang 			return true;
35847e4937aSGao Xiang 	return false;
35947e4937aSGao Xiang }
36047e4937aSGao Xiang 
36147e4937aSGao Xiang /* callers must be with collection lock held */
36247e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt,
36386432a6dSGao Xiang 			       struct page *page, enum z_erofs_page_type type,
36486432a6dSGao Xiang 			       bool pvec_safereuse)
36547e4937aSGao Xiang {
36647e4937aSGao Xiang 	int ret;
36747e4937aSGao Xiang 
36847e4937aSGao Xiang 	/* give priority for inplaceio */
36947e4937aSGao Xiang 	if (clt->mode >= COLLECT_PRIMARY &&
37047e4937aSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
37199634bf3SGao Xiang 	    z_erofs_try_inplace_io(clt, page))
37247e4937aSGao Xiang 		return 0;
37347e4937aSGao Xiang 
37486432a6dSGao Xiang 	ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
37586432a6dSGao Xiang 				      pvec_safereuse);
37647e4937aSGao Xiang 	clt->cl->vcnt += (unsigned int)ret;
37747e4937aSGao Xiang 	return ret ? 0 : -EAGAIN;
37847e4937aSGao Xiang }
37947e4937aSGao Xiang 
380473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
38147e4937aSGao Xiang {
382473e15b0SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
383473e15b0SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
38447e4937aSGao Xiang 
385473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
386473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
387473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
38847e4937aSGao Xiang 		*owned_head = &pcl->next;
389473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
390473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED;
391473e15b0SGao Xiang 		return;
392473e15b0SGao Xiang 	}
393473e15b0SGao Xiang 
39447e4937aSGao Xiang 	/*
395473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
396473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
39747e4937aSGao Xiang 	 */
39847e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
399473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
40047e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
401473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_HOOKED;
402473e15b0SGao Xiang 		clt->tailpcl = NULL;
403473e15b0SGao Xiang 		return;
40447e4937aSGao Xiang 	}
405473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
406473e15b0SGao Xiang 	clt->mode = COLLECT_PRIMARY;
40747e4937aSGao Xiang }
40847e4937aSGao Xiang 
4099e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
41047e4937aSGao Xiang 				     struct inode *inode,
41147e4937aSGao Xiang 				     struct erofs_map_blocks *map)
41247e4937aSGao Xiang {
41364094a04SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
41447e4937aSGao Xiang 	struct z_erofs_collection *cl;
41547e4937aSGao Xiang 	unsigned int length;
41647e4937aSGao Xiang 
41764094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
41847e4937aSGao Xiang 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
41947e4937aSGao Xiang 		DBG_BUGON(1);
4209e579fc1SGao Xiang 		return -EFSCORRUPTED;
42147e4937aSGao Xiang 	}
42247e4937aSGao Xiang 
42347e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
4248d8a09b0SGao Xiang 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
42547e4937aSGao Xiang 		DBG_BUGON(1);
4269e579fc1SGao Xiang 		return -EFSCORRUPTED;
42747e4937aSGao Xiang 	}
42847e4937aSGao Xiang 
42947e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
43047e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
43147e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
43247e4937aSGao Xiang 			DBG_BUGON(1);
4339e579fc1SGao Xiang 			return -EFSCORRUPTED;
43447e4937aSGao Xiang 		}
43547e4937aSGao Xiang 	} else {
43647e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
43747e4937aSGao Xiang 
43847e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
43947e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
44047e4937aSGao Xiang 
44147e4937aSGao Xiang 		while (llen > length &&
44247e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
44347e4937aSGao Xiang 			cpu_relax();
44447e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
44547e4937aSGao Xiang 		}
44647e4937aSGao Xiang 	}
44747e4937aSGao Xiang 	mutex_lock(&cl->lock);
44847e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
44947e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
45047e4937aSGao Xiang 		clt->tailpcl = pcl;
451473e15b0SGao Xiang 
452473e15b0SGao Xiang 	z_erofs_try_to_claim_pcluster(clt);
45347e4937aSGao Xiang 	clt->cl = cl;
4549e579fc1SGao Xiang 	return 0;
45547e4937aSGao Xiang }
45647e4937aSGao Xiang 
4579e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt,
45847e4937aSGao Xiang 				       struct inode *inode,
45947e4937aSGao Xiang 				       struct erofs_map_blocks *map)
46047e4937aSGao Xiang {
46147e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
46247e4937aSGao Xiang 	struct z_erofs_collection *cl;
46364094a04SGao Xiang 	struct erofs_workgroup *grp;
46447e4937aSGao Xiang 	int err;
46547e4937aSGao Xiang 
4668f899262SGao Xiang 	if (!(map->m_flags & EROFS_MAP_ENCODED)) {
4678f899262SGao Xiang 		DBG_BUGON(1);
4688f899262SGao Xiang 		return -EFSCORRUPTED;
4698f899262SGao Xiang 	}
4708f899262SGao Xiang 
4719f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
4729f6cc76eSGao Xiang 	pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
4739f6cc76eSGao Xiang 	if (IS_ERR(pcl))
4749f6cc76eSGao Xiang 		return PTR_ERR(pcl);
47547e4937aSGao Xiang 
47664094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
47747e4937aSGao Xiang 	pcl->obj.index = map->m_pa >> PAGE_SHIFT;
4788f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
47947e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
48047e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
48147e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
48247e4937aSGao Xiang 
48347e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
48447e4937aSGao Xiang 	pcl->next = clt->owned_head;
48547e4937aSGao Xiang 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
48647e4937aSGao Xiang 
48747e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
48847e4937aSGao Xiang 	cl->pageofs = map->m_la & ~PAGE_MASK;
48947e4937aSGao Xiang 
49047e4937aSGao Xiang 	/*
49147e4937aSGao Xiang 	 * lock all primary followed works before visible to others
49247e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
49347e4937aSGao Xiang 	 */
4949f6cc76eSGao Xiang 	mutex_init(&cl->lock);
49564094a04SGao Xiang 	DBG_BUGON(!mutex_trylock(&cl->lock));
49647e4937aSGao Xiang 
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) {
50464094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
50564094a04SGao Xiang 		err = -EEXIST;
50664094a04SGao Xiang 		goto err_out;
50747e4937aSGao Xiang 	}
50847e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
50947e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
51047e4937aSGao Xiang 		clt->tailpcl = pcl;
51147e4937aSGao Xiang 	clt->owned_head = &pcl->next;
51247e4937aSGao Xiang 	clt->pcl = pcl;
51347e4937aSGao Xiang 	clt->cl = cl;
5149e579fc1SGao Xiang 	return 0;
51564094a04SGao Xiang 
51664094a04SGao Xiang err_out:
51764094a04SGao Xiang 	mutex_unlock(&cl->lock);
5189f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
51964094a04SGao Xiang 	return err;
52047e4937aSGao Xiang }
52147e4937aSGao Xiang 
52247e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt,
52347e4937aSGao Xiang 				   struct inode *inode,
52447e4937aSGao Xiang 				   struct erofs_map_blocks *map)
52547e4937aSGao Xiang {
52664094a04SGao Xiang 	struct erofs_workgroup *grp;
5279e579fc1SGao Xiang 	int ret;
52847e4937aSGao Xiang 
52947e4937aSGao Xiang 	DBG_BUGON(clt->cl);
53047e4937aSGao Xiang 
53147e4937aSGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
53247e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
53347e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
53447e4937aSGao Xiang 
53547e4937aSGao Xiang 	if (!PAGE_ALIGNED(map->m_pa)) {
53647e4937aSGao Xiang 		DBG_BUGON(1);
53747e4937aSGao Xiang 		return -EINVAL;
53847e4937aSGao Xiang 	}
53947e4937aSGao Xiang 
54064094a04SGao Xiang 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
54164094a04SGao Xiang 	if (grp) {
54264094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
54364094a04SGao Xiang 	} else {
5449e579fc1SGao Xiang 		ret = z_erofs_register_collection(clt, inode, map);
54547e4937aSGao Xiang 
54664094a04SGao Xiang 		if (!ret)
54764094a04SGao Xiang 			goto out;
54864094a04SGao Xiang 		if (ret != -EEXIST)
5499e579fc1SGao Xiang 			return ret;
55064094a04SGao Xiang 	}
55147e4937aSGao Xiang 
55264094a04SGao Xiang 	ret = z_erofs_lookup_collection(clt, inode, map);
55364094a04SGao Xiang 	if (ret) {
55464094a04SGao Xiang 		erofs_workgroup_put(&clt->pcl->obj);
55564094a04SGao Xiang 		return ret;
55664094a04SGao Xiang 	}
55764094a04SGao Xiang 
55864094a04SGao Xiang out:
55947e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
5609e579fc1SGao Xiang 				  clt->cl->pagevec, clt->cl->vcnt);
56147e4937aSGao Xiang 
56281382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
56381382f5fSGao Xiang 	clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages;
56447e4937aSGao Xiang 	return 0;
56547e4937aSGao Xiang }
56647e4937aSGao Xiang 
56747e4937aSGao Xiang /*
56847e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
56947e4937aSGao Xiang  * only after a RCU grace period.
57047e4937aSGao Xiang  */
57147e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
57247e4937aSGao Xiang {
57347e4937aSGao Xiang 	struct z_erofs_collection *const cl =
57447e4937aSGao Xiang 		container_of(head, struct z_erofs_collection, rcu);
57547e4937aSGao Xiang 
5769f6cc76eSGao Xiang 	z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
57747e4937aSGao Xiang 					   primary_collection));
57847e4937aSGao Xiang }
57947e4937aSGao Xiang 
58047e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
58147e4937aSGao Xiang {
58247e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
58347e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
58447e4937aSGao Xiang 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
58547e4937aSGao Xiang 
58647e4937aSGao Xiang 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
58747e4937aSGao Xiang }
58847e4937aSGao Xiang 
58947e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl)
59047e4937aSGao Xiang {
59147e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
59247e4937aSGao Xiang 		container_of(cl, struct z_erofs_pcluster, primary_collection);
59347e4937aSGao Xiang 
59447e4937aSGao Xiang 	erofs_workgroup_put(&pcl->obj);
59547e4937aSGao Xiang }
59647e4937aSGao Xiang 
59747e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt)
59847e4937aSGao Xiang {
59947e4937aSGao Xiang 	struct z_erofs_collection *cl = clt->cl;
60047e4937aSGao Xiang 
60147e4937aSGao Xiang 	if (!cl)
60247e4937aSGao Xiang 		return false;
60347e4937aSGao Xiang 
60447e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
60547e4937aSGao Xiang 	mutex_unlock(&cl->lock);
60647e4937aSGao Xiang 
60747e4937aSGao Xiang 	/*
60847e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
60947e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
61047e4937aSGao Xiang 	 */
61147e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
61247e4937aSGao Xiang 		z_erofs_collection_put(cl);
61347e4937aSGao Xiang 
61447e4937aSGao Xiang 	clt->cl = NULL;
61547e4937aSGao Xiang 	return true;
61647e4937aSGao Xiang }
61747e4937aSGao Xiang 
61847e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
61947e4937aSGao Xiang 				       unsigned int cachestrategy,
62047e4937aSGao Xiang 				       erofs_off_t la)
62147e4937aSGao Xiang {
62247e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
62347e4937aSGao Xiang 		return false;
62447e4937aSGao Xiang 
62547e4937aSGao Xiang 	if (fe->backmost)
62647e4937aSGao Xiang 		return true;
62747e4937aSGao Xiang 
62847e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
62947e4937aSGao Xiang 		la < fe->headoffset;
63047e4937aSGao Xiang }
63147e4937aSGao Xiang 
63247e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
633eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
63447e4937aSGao Xiang {
63547e4937aSGao Xiang 	struct inode *const inode = fe->inode;
636bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
63747e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
63847e4937aSGao Xiang 	struct z_erofs_collector *const clt = &fe->clt;
63947e4937aSGao Xiang 	const loff_t offset = page_offset(page);
640dc76ea8cSGao Xiang 	bool tight = true;
64147e4937aSGao Xiang 
64247e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
64347e4937aSGao Xiang 	enum z_erofs_page_type page_type;
64447e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
64547e4937aSGao Xiang 	int err = 0;
64647e4937aSGao Xiang 
64747e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
64847e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
64947e4937aSGao Xiang 
65047e4937aSGao Xiang 	spiltted = 0;
65147e4937aSGao Xiang 	end = PAGE_SIZE;
65247e4937aSGao Xiang repeat:
65347e4937aSGao Xiang 	cur = end - 1;
65447e4937aSGao Xiang 
65547e4937aSGao Xiang 	/* lucky, within the range of the current map_blocks */
65647e4937aSGao Xiang 	if (offset + cur >= map->m_la &&
65747e4937aSGao Xiang 	    offset + cur < map->m_la + map->m_llen) {
65847e4937aSGao Xiang 		/* didn't get a valid collection previously (very rare) */
65947e4937aSGao Xiang 		if (!clt->cl)
66047e4937aSGao Xiang 			goto restart_now;
66147e4937aSGao Xiang 		goto hitted;
66247e4937aSGao Xiang 	}
66347e4937aSGao Xiang 
66447e4937aSGao Xiang 	/* go ahead the next map_blocks */
6654f761fa2SGao Xiang 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
66647e4937aSGao Xiang 
66747e4937aSGao Xiang 	if (z_erofs_collector_end(clt))
66847e4937aSGao Xiang 		fe->backmost = false;
66947e4937aSGao Xiang 
67047e4937aSGao Xiang 	map->m_la = offset + cur;
67147e4937aSGao Xiang 	map->m_llen = 0;
67247e4937aSGao Xiang 	err = z_erofs_map_blocks_iter(inode, map, 0);
6738d8a09b0SGao Xiang 	if (err)
67447e4937aSGao Xiang 		goto err_out;
67547e4937aSGao Xiang 
67647e4937aSGao Xiang restart_now:
6778d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
67847e4937aSGao Xiang 		goto hitted;
67947e4937aSGao Xiang 
68047e4937aSGao Xiang 	err = z_erofs_collector_begin(clt, inode, map);
6818d8a09b0SGao Xiang 	if (err)
68247e4937aSGao Xiang 		goto err_out;
68347e4937aSGao Xiang 
68447e4937aSGao Xiang 	/* preload all compressed pages (maybe downgrade role if necessary) */
685e6242465SGao Xiang 	if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy, map->m_la))
6861825c8d7SGao Xiang 		cache_strategy = TRYALLOC;
68747e4937aSGao Xiang 	else
68847e4937aSGao Xiang 		cache_strategy = DONTALLOC;
68947e4937aSGao Xiang 
6901825c8d7SGao Xiang 	preload_compressed_pages(clt, MNGD_MAPPING(sbi),
6911825c8d7SGao Xiang 				 cache_strategy, pagepool);
69247e4937aSGao Xiang 
69347e4937aSGao Xiang hitted:
694dc76ea8cSGao Xiang 	/*
695dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
696dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
697dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
698dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
699dc76ea8cSGao Xiang 	 */
700dc76ea8cSGao Xiang 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
701dc76ea8cSGao Xiang 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
702dc76ea8cSGao Xiang 
70347e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
7048d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
70547e4937aSGao Xiang 		zero_user_segment(page, cur, end);
70647e4937aSGao Xiang 		goto next_part;
70747e4937aSGao Xiang 	}
70847e4937aSGao Xiang 
70947e4937aSGao Xiang 	/* let's derive page type */
71047e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
71147e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
71247e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
71347e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
71447e4937aSGao Xiang 
71547e4937aSGao Xiang 	if (cur)
71647e4937aSGao Xiang 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
71747e4937aSGao Xiang 
71847e4937aSGao Xiang retry:
71986432a6dSGao Xiang 	err = z_erofs_attach_page(clt, page, page_type,
72086432a6dSGao Xiang 				  clt->mode >= COLLECT_PRIMARY_FOLLOWED);
7216aaa7b06SGao Xiang 	/* should allocate an additional short-lived page for pagevec */
72247e4937aSGao Xiang 	if (err == -EAGAIN) {
72347e4937aSGao Xiang 		struct page *const newpage =
724e3f78d5eSChao Yu 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
72547e4937aSGao Xiang 
7266aaa7b06SGao Xiang 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
72747e4937aSGao Xiang 		err = z_erofs_attach_page(clt, newpage,
72886432a6dSGao Xiang 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
7298d8a09b0SGao Xiang 		if (!err)
73047e4937aSGao Xiang 			goto retry;
73147e4937aSGao Xiang 	}
73247e4937aSGao Xiang 
7338d8a09b0SGao Xiang 	if (err)
73447e4937aSGao Xiang 		goto err_out;
73547e4937aSGao Xiang 
73647e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
73747e4937aSGao Xiang 
73847e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
73947e4937aSGao Xiang 
74047e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
74147e4937aSGao Xiang 	++spiltted;
74247e4937aSGao Xiang 	/* also update nr_pages */
74347e4937aSGao Xiang 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
74447e4937aSGao Xiang next_part:
74547e4937aSGao Xiang 	/* can be used for verification */
74647e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
74747e4937aSGao Xiang 
74847e4937aSGao Xiang 	end = cur;
74947e4937aSGao Xiang 	if (end > 0)
75047e4937aSGao Xiang 		goto repeat;
75147e4937aSGao Xiang 
75247e4937aSGao Xiang out:
75347e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
75447e4937aSGao Xiang 
7554f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
75647e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
75747e4937aSGao Xiang 	return err;
75847e4937aSGao Xiang 
75947e4937aSGao Xiang 	/* if some error occurred while processing this page */
76047e4937aSGao Xiang err_out:
76147e4937aSGao Xiang 	SetPageError(page);
76247e4937aSGao Xiang 	goto out;
76347e4937aSGao Xiang }
76447e4937aSGao Xiang 
765*40452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
766*40452ffcSHuang Jianan 				       unsigned int readahead_pages)
767*40452ffcSHuang Jianan {
768*40452ffcSHuang Jianan 	/* auto: enable for readpage, disable for readahead */
769*40452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
770*40452ffcSHuang Jianan 	    !readahead_pages)
771*40452ffcSHuang Jianan 		return true;
772*40452ffcSHuang Jianan 
773*40452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
774*40452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
775*40452ffcSHuang Jianan 		return true;
776*40452ffcSHuang Jianan 
777*40452ffcSHuang Jianan 	return false;
778*40452ffcSHuang Jianan }
779*40452ffcSHuang Jianan 
780648f2de0SHuang Jianan static void z_erofs_decompressqueue_work(struct work_struct *work);
781a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
782a4b1fab1SGao Xiang 				       bool sync, int bios)
78347e4937aSGao Xiang {
78430048cdaSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
78530048cdaSHuang Jianan 
786a4b1fab1SGao Xiang 	/* wake up the caller thread for sync decompression */
787a4b1fab1SGao Xiang 	if (sync) {
78847e4937aSGao Xiang 		unsigned long flags;
78947e4937aSGao Xiang 
79047e4937aSGao Xiang 		spin_lock_irqsave(&io->u.wait.lock, flags);
79147e4937aSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
79247e4937aSGao Xiang 			wake_up_locked(&io->u.wait);
79347e4937aSGao Xiang 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
79447e4937aSGao Xiang 		return;
79547e4937aSGao Xiang 	}
79647e4937aSGao Xiang 
797648f2de0SHuang Jianan 	if (atomic_add_return(bios, &io->pending_bios))
798648f2de0SHuang Jianan 		return;
79930048cdaSHuang Jianan 	/* Use workqueue and sync decompression for atomic contexts only */
800648f2de0SHuang Jianan 	if (in_atomic() || irqs_disabled()) {
80147e4937aSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
802*40452ffcSHuang Jianan 		/* enable sync decompression for readahead */
803*40452ffcSHuang Jianan 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
804*40452ffcSHuang Jianan 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
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,
844eaa9172aSGao Xiang 				       struct page **pagepool)
84547e4937aSGao Xiang {
84647e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
84747e4937aSGao Xiang 	struct z_erofs_pagevec_ctor ctor;
8489f6cc76eSGao 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 
9289f6cc76eSGao 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 
9819f6cc76eSGao 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,
9879f6cc76eSGao 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 */
9969f6cc76eSGao 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,
1044eaa9172aSGao Xiang 				     struct page **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);
1068eaa9172aSGao Xiang 	struct page *pagepool = NULL;
106947e4937aSGao Xiang 
1070a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
10710c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
107247e4937aSGao Xiang 
1073eaa9172aSGao Xiang 	erofs_release_pages(&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,
1079eaa9172aSGao Xiang 					       struct page **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 	/* process the target tagged pointer */
110047e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
110147e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
110247e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
110347e4937aSGao Xiang 
11041825c8d7SGao Xiang 	/*
11051825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
11061825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
11071825c8d7SGao Xiang 	 */
11081825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
11091825c8d7SGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
11101825c8d7SGao Xiang 		set_page_private(page, 0);
11111825c8d7SGao Xiang 		tocache = true;
11121825c8d7SGao Xiang 		goto out_tocache;
11131825c8d7SGao Xiang 	}
111447e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
111547e4937aSGao Xiang 
111647e4937aSGao Xiang 	/*
11176aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
111847e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
111947e4937aSGao Xiang 	 */
112047e4937aSGao Xiang 	if (mapping && mapping != mc)
112147e4937aSGao Xiang 		/* ought to be unmanaged pages */
112247e4937aSGao Xiang 		goto out;
112347e4937aSGao Xiang 
11246aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
11256aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
11266aaa7b06SGao Xiang 		goto out;
11276aaa7b06SGao Xiang 
112847e4937aSGao Xiang 	lock_page(page);
112947e4937aSGao Xiang 
113047e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
113147e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
113247e4937aSGao Xiang 
113347e4937aSGao Xiang 	/* the page is still in manage cache */
113447e4937aSGao Xiang 	if (page->mapping == mc) {
113547e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
113647e4937aSGao Xiang 
113747e4937aSGao Xiang 		ClearPageError(page);
113847e4937aSGao Xiang 		if (!PagePrivate(page)) {
113947e4937aSGao Xiang 			/*
114047e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
114147e4937aSGao Xiang 			 * the current restriction as well if
114247e4937aSGao Xiang 			 * the page is already in compressed_pages[].
114347e4937aSGao Xiang 			 */
114447e4937aSGao Xiang 			DBG_BUGON(!justfound);
114547e4937aSGao Xiang 
114647e4937aSGao Xiang 			justfound = 0;
114747e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
114847e4937aSGao Xiang 			SetPagePrivate(page);
114947e4937aSGao Xiang 		}
115047e4937aSGao Xiang 
115147e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
115247e4937aSGao Xiang 		if (PageUptodate(page)) {
115347e4937aSGao Xiang 			unlock_page(page);
115447e4937aSGao Xiang 			page = NULL;
115547e4937aSGao Xiang 		}
115647e4937aSGao Xiang 		goto out;
115747e4937aSGao Xiang 	}
115847e4937aSGao Xiang 
115947e4937aSGao Xiang 	/*
116047e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
116147e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
116247e4937aSGao Xiang 	 */
116347e4937aSGao Xiang 	DBG_BUGON(page->mapping);
116447e4937aSGao Xiang 	DBG_BUGON(!justfound);
116547e4937aSGao Xiang 
116647e4937aSGao Xiang 	tocache = true;
116747e4937aSGao Xiang 	unlock_page(page);
116847e4937aSGao Xiang 	put_page(page);
116947e4937aSGao Xiang out_allocpage:
11705ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
11715ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1172eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
11735ddcee1fSGao Xiang 		cond_resched();
11745ddcee1fSGao Xiang 		goto repeat;
11755ddcee1fSGao Xiang 	}
11761825c8d7SGao Xiang out_tocache:
1177bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1178bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1179bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1180bf225074SGao Xiang 		goto out;
1181a30573b3SGao Xiang 	}
1182bf225074SGao Xiang 	attach_page_private(page, pcl);
1183bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1184bf225074SGao Xiang 	put_page(page);
1185bf225074SGao Xiang 
118647e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
118747e4937aSGao Xiang 	return page;
118847e4937aSGao Xiang }
118947e4937aSGao Xiang 
1190a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1191a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1192a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
119347e4937aSGao Xiang {
1194a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
119547e4937aSGao Xiang 
1196a4b1fab1SGao Xiang 	if (fg && !*fg) {
1197a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1198a4b1fab1SGao Xiang 		if (!q) {
1199a4b1fab1SGao Xiang 			*fg = true;
1200a4b1fab1SGao Xiang 			goto fg_out;
120147e4937aSGao Xiang 		}
12020c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1203a4b1fab1SGao Xiang 	} else {
1204a4b1fab1SGao Xiang fg_out:
1205a4b1fab1SGao Xiang 		q = fgq;
1206a4b1fab1SGao Xiang 		init_waitqueue_head(&fgq->u.wait);
1207a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1208a4b1fab1SGao Xiang 	}
1209a4b1fab1SGao Xiang 	q->sb = sb;
1210a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1211a4b1fab1SGao Xiang 	return q;
121247e4937aSGao Xiang }
121347e4937aSGao Xiang 
121447e4937aSGao Xiang /* define decompression jobqueue types */
121547e4937aSGao Xiang enum {
121647e4937aSGao Xiang 	JQ_BYPASS,
121747e4937aSGao Xiang 	JQ_SUBMIT,
121847e4937aSGao Xiang 	NR_JOBQUEUES,
121947e4937aSGao Xiang };
122047e4937aSGao Xiang 
122147e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1222a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1223a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
122447e4937aSGao Xiang {
122547e4937aSGao Xiang 	/*
122647e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
122747e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
122847e4937aSGao Xiang 	 */
1229a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1230a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
123147e4937aSGao Xiang 
1232a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
123347e4937aSGao Xiang }
123447e4937aSGao Xiang 
123547e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
123647e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
123747e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
123847e4937aSGao Xiang {
123947e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
124047e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
124147e4937aSGao Xiang 
124247e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
124347e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
124447e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
124547e4937aSGao Xiang 
124647e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
124747e4937aSGao Xiang 
124847e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
124947e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
125047e4937aSGao Xiang 
125147e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
125247e4937aSGao Xiang }
125347e4937aSGao Xiang 
12541e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb,
12556ea5aad3SGao Xiang 				 struct z_erofs_decompress_frontend *f,
1256eaa9172aSGao Xiang 				 struct page **pagepool,
1257a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1258a4b1fab1SGao Xiang 				 bool *force_fg)
125947e4937aSGao Xiang {
1260bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
126147e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1262a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
126347e4937aSGao Xiang 	void *bi_private;
12646ea5aad3SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1265dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
12663f649ab7SKees Cook 	pgoff_t last_index;
1267dfeab2e9SGao Xiang 	struct block_device *last_bdev;
12681e4a2955SGao Xiang 	unsigned int nr_bios = 0;
12691e4a2955SGao Xiang 	struct bio *bio = NULL;
127047e4937aSGao Xiang 
1271a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1272a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1273a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
127447e4937aSGao Xiang 
127547e4937aSGao Xiang 	/* by default, all need io submission */
127647e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
127747e4937aSGao Xiang 
127847e4937aSGao Xiang 	do {
1279dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
128047e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
12811e4a2955SGao Xiang 		pgoff_t cur, end;
12821e4a2955SGao Xiang 		unsigned int i = 0;
12831e4a2955SGao Xiang 		bool bypass = true;
128447e4937aSGao Xiang 
128547e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
128647e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
128747e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
128847e4937aSGao Xiang 
128947e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
129047e4937aSGao Xiang 
1291dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1292dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
1293dfeab2e9SGao Xiang 			.m_pa = blknr_to_addr(pcl->obj.index),
1294dfeab2e9SGao Xiang 		};
1295dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1296dfeab2e9SGao Xiang 
1297dfeab2e9SGao Xiang 		cur = erofs_blknr(mdev.m_pa);
12989f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
129947e4937aSGao Xiang 
130047e4937aSGao Xiang 		/* close the main owned chain at first */
130147e4937aSGao Xiang 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
130247e4937aSGao Xiang 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
130347e4937aSGao Xiang 
13041e4a2955SGao Xiang 		do {
13051e4a2955SGao Xiang 			struct page *page;
130647e4937aSGao Xiang 
13071e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
130847e4937aSGao Xiang 							  MNGD_MAPPING(sbi),
130947e4937aSGao Xiang 							  GFP_NOFS);
13101e4a2955SGao Xiang 			if (!page)
13111e4a2955SGao Xiang 				continue;
131247e4937aSGao Xiang 
1313dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1314dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
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);
13220c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1323dfeab2e9SGao Xiang 
1324dfeab2e9SGao Xiang 				bio_set_dev(bio, mdev.m_bdev);
1325dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
13261e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1327a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1328a5c0b780SGao Xiang 				bio->bi_private = bi_private;
132994e4e153SGao Xiang 				bio->bi_opf = REQ_OP_READ;
13306ea5aad3SGao Xiang 				if (f->readahead)
13316ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
133247e4937aSGao Xiang 				++nr_bios;
133347e4937aSGao Xiang 			}
133447e4937aSGao Xiang 
13356c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
133647e4937aSGao Xiang 				goto submit_bio_retry;
133747e4937aSGao Xiang 
13381e4a2955SGao Xiang 			last_index = cur;
13391e4a2955SGao Xiang 			bypass = false;
13401e4a2955SGao Xiang 		} while (++cur < end);
134147e4937aSGao Xiang 
13421e4a2955SGao Xiang 		if (!bypass)
134347e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
134447e4937aSGao Xiang 		else
134547e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
134647e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
134747e4937aSGao Xiang 
134847e4937aSGao Xiang 	if (bio)
134994e4e153SGao Xiang 		submit_bio(bio);
135047e4937aSGao Xiang 
1351587a67b7SGao Xiang 	/*
1352587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1353587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1354587a67b7SGao Xiang 	 */
1355587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1356587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
13571e4a2955SGao Xiang 		return;
1358587a67b7SGao Xiang 	}
1359a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
136047e4937aSGao Xiang }
136147e4937aSGao Xiang 
13620c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb,
13636ea5aad3SGao Xiang 			     struct z_erofs_decompress_frontend *f,
1364eaa9172aSGao Xiang 			     struct page **pagepool, bool force_fg)
136547e4937aSGao Xiang {
1366a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
136747e4937aSGao Xiang 
13686ea5aad3SGao Xiang 	if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
136947e4937aSGao Xiang 		return;
13706ea5aad3SGao Xiang 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
137147e4937aSGao Xiang 
13720c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
13730c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
137447e4937aSGao Xiang 
137547e4937aSGao Xiang 	if (!force_fg)
137647e4937aSGao Xiang 		return;
137747e4937aSGao Xiang 
137847e4937aSGao Xiang 	/* wait until all bios are completed */
1379a93f8c36SGao Xiang 	io_wait_event(io[JQ_SUBMIT].u.wait,
138047e4937aSGao Xiang 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
138147e4937aSGao Xiang 
13820c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
13830c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
138447e4937aSGao Xiang }
138547e4937aSGao Xiang 
138638629291SGao Xiang /*
138738629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
138838629291SGao Xiang  * approximate readmore strategies as a start.
138938629291SGao Xiang  */
139038629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
139138629291SGao Xiang 				      struct readahead_control *rac,
139238629291SGao Xiang 				      erofs_off_t end,
1393eaa9172aSGao Xiang 				      struct page **pagepool,
139438629291SGao Xiang 				      bool backmost)
139538629291SGao Xiang {
139638629291SGao Xiang 	struct inode *inode = f->inode;
139738629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
139838629291SGao Xiang 	erofs_off_t cur;
139938629291SGao Xiang 	int err;
140038629291SGao Xiang 
140138629291SGao Xiang 	if (backmost) {
140238629291SGao Xiang 		map->m_la = end;
1403622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1404622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
140538629291SGao Xiang 		if (err)
140638629291SGao Xiang 			return;
140738629291SGao Xiang 
140838629291SGao Xiang 		/* expend ra for the trailing edge if readahead */
140938629291SGao Xiang 		if (rac) {
141038629291SGao Xiang 			loff_t newstart = readahead_pos(rac);
141138629291SGao Xiang 
141238629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
141338629291SGao Xiang 			readahead_expand(rac, newstart, cur - newstart);
141438629291SGao Xiang 			return;
141538629291SGao Xiang 		}
141638629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
141738629291SGao Xiang 	} else {
141838629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
141938629291SGao Xiang 
142038629291SGao Xiang 		if (!map->m_llen)
142138629291SGao Xiang 			return;
142238629291SGao Xiang 	}
142338629291SGao Xiang 
142438629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
142538629291SGao Xiang 	while (cur >= end) {
142638629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
142738629291SGao Xiang 		struct page *page;
142838629291SGao Xiang 
142938629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
143038629291SGao Xiang 		if (!page)
143138629291SGao Xiang 			goto skip;
143238629291SGao Xiang 
143338629291SGao Xiang 		if (PageUptodate(page)) {
143438629291SGao Xiang 			unlock_page(page);
143538629291SGao Xiang 			put_page(page);
143638629291SGao Xiang 			goto skip;
143738629291SGao Xiang 		}
143838629291SGao Xiang 
143938629291SGao Xiang 		err = z_erofs_do_read_page(f, page, pagepool);
144038629291SGao Xiang 		if (err)
144138629291SGao Xiang 			erofs_err(inode->i_sb,
144238629291SGao Xiang 				  "readmore error at page %lu @ nid %llu",
144338629291SGao Xiang 				  index, EROFS_I(inode)->nid);
144438629291SGao Xiang 		put_page(page);
144538629291SGao Xiang skip:
144638629291SGao Xiang 		if (cur < PAGE_SIZE)
144738629291SGao Xiang 			break;
144838629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
144938629291SGao Xiang 	}
145038629291SGao Xiang }
145138629291SGao Xiang 
14520c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page)
145347e4937aSGao Xiang {
145447e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
1455*40452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
145647e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1457eaa9172aSGao Xiang 	struct page *pagepool = NULL;
145847e4937aSGao Xiang 	int err;
145947e4937aSGao Xiang 
146047e4937aSGao Xiang 	trace_erofs_readpage(page, false);
146147e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
146247e4937aSGao Xiang 
146338629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
146438629291SGao Xiang 				  &pagepool, true);
14651825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
146638629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
146738629291SGao Xiang 
146847e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
146947e4937aSGao Xiang 
147047e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
1471*40452ffcSHuang Jianan 	z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1472*40452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, 0));
147347e4937aSGao Xiang 
147447e4937aSGao Xiang 	if (err)
14754f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
147647e4937aSGao Xiang 
147747e4937aSGao Xiang 	if (f.map.mpage)
147847e4937aSGao Xiang 		put_page(f.map.mpage);
147947e4937aSGao Xiang 
1480eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
148147e4937aSGao Xiang 	return err;
148247e4937aSGao Xiang }
148347e4937aSGao Xiang 
14840615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
148547e4937aSGao Xiang {
14860615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
148747e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
148847e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1489eaa9172aSGao Xiang 	struct page *pagepool = NULL, *head = NULL, *page;
149038629291SGao Xiang 	unsigned int nr_pages;
149147e4937aSGao Xiang 
14926ea5aad3SGao Xiang 	f.readahead = true;
14930615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
149447e4937aSGao Xiang 
149538629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, f.headoffset +
149638629291SGao Xiang 				  readahead_length(rac) - 1, &pagepool, true);
149738629291SGao Xiang 	nr_pages = readahead_count(rac);
149838629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
149938629291SGao Xiang 
15000615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
150147e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
150247e4937aSGao Xiang 		head = page;
150347e4937aSGao Xiang 	}
150447e4937aSGao Xiang 
150547e4937aSGao Xiang 	while (head) {
150647e4937aSGao Xiang 		struct page *page = head;
150747e4937aSGao Xiang 		int err;
150847e4937aSGao Xiang 
150947e4937aSGao Xiang 		/* traversal in reverse order */
151047e4937aSGao Xiang 		head = (void *)page_private(page);
151147e4937aSGao Xiang 
15121825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1513a5876e24SGao Xiang 		if (err)
15144f761fa2SGao Xiang 			erofs_err(inode->i_sb,
15154f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
15164f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
151747e4937aSGao Xiang 		put_page(page);
151847e4937aSGao Xiang 	}
151938629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
152047e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
152147e4937aSGao Xiang 
152238629291SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1523*40452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
152447e4937aSGao Xiang 	if (f.map.mpage)
152547e4937aSGao Xiang 		put_page(f.map.mpage);
1526eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
152747e4937aSGao Xiang }
152847e4937aSGao Xiang 
15290c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
15300c638f70SGao Xiang 	.readpage = z_erofs_readpage,
15310615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
153247e4937aSGao Xiang };
1533