xref: /openbmc/linux/fs/erofs/zdata.c (revision eaa9172a)
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 
999f6cc76eSGao Xiang /*
10047e4937aSGao Xiang  * a compressed_pages[] placeholder in order to avoid
10147e4937aSGao Xiang  * being filled with file pages for in-place decompression.
10247e4937aSGao Xiang  */
10347e4937aSGao Xiang #define PAGE_UNALLOCATED     ((void *)0x5F0E4B1D)
10447e4937aSGao Xiang 
10547e4937aSGao Xiang /* how to allocate cached pages for a pcluster */
10647e4937aSGao Xiang enum z_erofs_cache_alloctype {
10747e4937aSGao Xiang 	DONTALLOC,	/* don't allocate any cached pages */
10847e4937aSGao Xiang 	DELAYEDALLOC,	/* delayed allocation (at the time of submitting io) */
1091825c8d7SGao Xiang 	/*
1101825c8d7SGao Xiang 	 * try to use cached I/O if page allocation succeeds or fallback
1111825c8d7SGao Xiang 	 * to in-place I/O instead to avoid any direct reclaim.
1121825c8d7SGao Xiang 	 */
1131825c8d7SGao Xiang 	TRYALLOC,
11447e4937aSGao Xiang };
11547e4937aSGao Xiang 
11647e4937aSGao Xiang /*
11747e4937aSGao Xiang  * tagged pointer with 1-bit tag for all compressed pages
11847e4937aSGao Xiang  * tag 0 - the page is just found with an extra page reference
11947e4937aSGao Xiang  */
12047e4937aSGao Xiang typedef tagptr1_t compressed_page_t;
12147e4937aSGao Xiang 
12247e4937aSGao Xiang #define tag_compressed_page_justfound(page) \
12347e4937aSGao Xiang 	tagptr_fold(compressed_page_t, page, 1)
12447e4937aSGao Xiang 
12547e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
12647e4937aSGao Xiang 
12747e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
12847e4937aSGao Xiang {
12947e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
1309f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
13147e4937aSGao Xiang }
13247e4937aSGao Xiang 
13399634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
13447e4937aSGao Xiang {
13547e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
13647e4937aSGao Xiang 
13747e4937aSGao Xiang 	/*
13847e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
13947e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
14047e4937aSGao Xiang 	 */
1410e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
1420e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
14347e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
14447e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
14547e4937aSGao Xiang }
14647e4937aSGao Xiang 
14747e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
14847e4937aSGao Xiang {
1499f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
15047e4937aSGao Xiang 
1519f6cc76eSGao Xiang 	if (err)
1529f6cc76eSGao Xiang 		return err;
1539f6cc76eSGao Xiang 	err = z_erofs_init_workqueue();
1549f6cc76eSGao Xiang 	if (err)
1559f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
1569f6cc76eSGao Xiang 	return err;
15747e4937aSGao Xiang }
15847e4937aSGao Xiang 
15947e4937aSGao Xiang enum z_erofs_collectmode {
16047e4937aSGao Xiang 	COLLECT_SECONDARY,
16147e4937aSGao Xiang 	COLLECT_PRIMARY,
16247e4937aSGao Xiang 	/*
16347e4937aSGao Xiang 	 * The current collection was the tail of an exist chain, in addition
16447e4937aSGao Xiang 	 * that the previous processed chained collections are all decided to
16547e4937aSGao Xiang 	 * be hooked up to it.
16647e4937aSGao Xiang 	 * A new chain will be created for the remaining collections which are
16747e4937aSGao Xiang 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
16847e4937aSGao Xiang 	 * the next collection cannot reuse the whole page safely in
16947e4937aSGao Xiang 	 * the following scenario:
17047e4937aSGao Xiang 	 *  ________________________________________________________________
17147e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
17247e4937aSGao Xiang 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
17347e4937aSGao Xiang 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
17447e4937aSGao Xiang 	 */
17547e4937aSGao Xiang 	COLLECT_PRIMARY_HOOKED,
1760b964600SGao Xiang 	/*
1770b964600SGao Xiang 	 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
1780b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
1790b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
1800b964600SGao Xiang 	 * pagevec) since it can be directly decoded without I/O submission.
1810b964600SGao Xiang 	 */
18247e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
18347e4937aSGao Xiang 	/*
18447e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
18547e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
18647e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
18747e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
18847e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
18947e4937aSGao Xiang 	 * illustrated below:
19047e4937aSGao Xiang 	 *  ________________________________________________________________
19147e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
19247e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
19347e4937aSGao Xiang 	 * |  PRIMARY_FOLLOWED or |                                        |
19447e4937aSGao Xiang 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
19547e4937aSGao Xiang 	 *
19647e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
19747e4937aSGao Xiang 	 */
19847e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED,
19947e4937aSGao Xiang };
20047e4937aSGao Xiang 
20147e4937aSGao Xiang struct z_erofs_collector {
20247e4937aSGao Xiang 	struct z_erofs_pagevec_ctor vector;
20347e4937aSGao Xiang 
20447e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
20547e4937aSGao Xiang 	struct z_erofs_collection *cl;
20681382f5fSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
20781382f5fSGao Xiang 	struct page **icpage_ptr;
20847e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
20947e4937aSGao Xiang 
21047e4937aSGao Xiang 	enum z_erofs_collectmode mode;
21147e4937aSGao Xiang };
21247e4937aSGao Xiang 
21347e4937aSGao Xiang struct z_erofs_decompress_frontend {
21447e4937aSGao Xiang 	struct inode *const inode;
21547e4937aSGao Xiang 
21647e4937aSGao Xiang 	struct z_erofs_collector clt;
21747e4937aSGao Xiang 	struct erofs_map_blocks map;
21847e4937aSGao Xiang 
2196ea5aad3SGao Xiang 	bool readahead;
22047e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
22147e4937aSGao Xiang 	bool backmost;
22247e4937aSGao Xiang 	erofs_off_t headoffset;
22347e4937aSGao Xiang };
22447e4937aSGao Xiang 
22547e4937aSGao Xiang #define COLLECTOR_INIT() { \
22647e4937aSGao Xiang 	.owned_head = Z_EROFS_PCLUSTER_TAIL, \
22747e4937aSGao Xiang 	.mode = COLLECT_PRIMARY_FOLLOWED }
22847e4937aSGao Xiang 
22947e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
23047e4937aSGao Xiang 	.inode = __i, .clt = COLLECTOR_INIT(), \
23147e4937aSGao Xiang 	.backmost = true, }
23247e4937aSGao Xiang 
23347e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
23447e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
23547e4937aSGao Xiang 
23647e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt,
23747e4937aSGao Xiang 				     struct address_space *mc,
2381825c8d7SGao Xiang 				     enum z_erofs_cache_alloctype type,
239*eaa9172aSGao Xiang 				     struct page **pagepool)
24047e4937aSGao Xiang {
24181382f5fSGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
24247e4937aSGao Xiang 	bool standalone = true;
2431825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
2441825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
24581382f5fSGao Xiang 	struct page **pages;
24681382f5fSGao Xiang 	pgoff_t index;
24747e4937aSGao Xiang 
24847e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
24947e4937aSGao Xiang 		return;
25047e4937aSGao Xiang 
25181382f5fSGao Xiang 	pages = pcl->compressed_pages;
25281382f5fSGao Xiang 	index = pcl->obj.index;
25381382f5fSGao Xiang 	for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
25447e4937aSGao Xiang 		struct page *page;
25547e4937aSGao Xiang 		compressed_page_t t;
2561825c8d7SGao Xiang 		struct page *newpage = NULL;
25747e4937aSGao Xiang 
25847e4937aSGao Xiang 		/* the compressed page was loaded before */
25947e4937aSGao Xiang 		if (READ_ONCE(*pages))
26047e4937aSGao Xiang 			continue;
26147e4937aSGao Xiang 
26247e4937aSGao Xiang 		page = find_get_page(mc, index);
26347e4937aSGao Xiang 
26447e4937aSGao Xiang 		if (page) {
26547e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
2660b964600SGao Xiang 		} else {
2670b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
2680b964600SGao Xiang 			standalone = false;
2690b964600SGao Xiang 			switch (type) {
2700b964600SGao Xiang 			case DELAYEDALLOC:
2710b964600SGao Xiang 				t = tagptr_init(compressed_page_t,
2720b964600SGao Xiang 						PAGE_UNALLOCATED);
2730b964600SGao Xiang 				break;
2740b964600SGao Xiang 			case TRYALLOC:
2751825c8d7SGao Xiang 				newpage = erofs_allocpage(pagepool, gfp);
2761825c8d7SGao Xiang 				if (!newpage)
27747e4937aSGao Xiang 					continue;
2780b964600SGao Xiang 				set_page_private(newpage,
2790b964600SGao Xiang 						 Z_EROFS_PREALLOCATED_PAGE);
2800b964600SGao Xiang 				t = tag_compressed_page_justfound(newpage);
2810b964600SGao Xiang 				break;
2820b964600SGao Xiang 			default:        /* DONTALLOC */
2830b964600SGao Xiang 				continue;
2840b964600SGao Xiang 			}
28547e4937aSGao Xiang 		}
28647e4937aSGao Xiang 
28747e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
28847e4937aSGao Xiang 			continue;
28947e4937aSGao Xiang 
290*eaa9172aSGao Xiang 		if (page)
29147e4937aSGao Xiang 			put_page(page);
292*eaa9172aSGao Xiang 		else if (newpage)
293*eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
29447e4937aSGao Xiang 	}
29547e4937aSGao Xiang 
2960b964600SGao Xiang 	/*
2970b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
2980b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
2990b964600SGao Xiang 	 */
3000b964600SGao Xiang 	if (standalone)
30147e4937aSGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
30247e4937aSGao Xiang }
30347e4937aSGao Xiang 
30447e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
30547e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
30647e4937aSGao Xiang 				       struct erofs_workgroup *grp)
30747e4937aSGao Xiang {
30847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
30947e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
31047e4937aSGao Xiang 	int i;
31147e4937aSGao Xiang 
31247e4937aSGao Xiang 	/*
31347e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
31447e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
31547e4937aSGao Xiang 	 */
3169f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
31747e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
31847e4937aSGao Xiang 
31947e4937aSGao Xiang 		if (!page)
32047e4937aSGao Xiang 			continue;
32147e4937aSGao Xiang 
32247e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
32347e4937aSGao Xiang 		if (!trylock_page(page))
32447e4937aSGao Xiang 			return -EBUSY;
32547e4937aSGao Xiang 
326f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
32747e4937aSGao Xiang 			continue;
32847e4937aSGao Xiang 
32947e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
33047e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
3316aaa7b06SGao Xiang 		detach_page_private(page);
33247e4937aSGao Xiang 		unlock_page(page);
33347e4937aSGao Xiang 	}
33447e4937aSGao Xiang 	return 0;
33547e4937aSGao Xiang }
33647e4937aSGao Xiang 
337d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
33847e4937aSGao Xiang {
33947e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
34047e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
34147e4937aSGao Xiang 
34247e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
34347e4937aSGao Xiang 		unsigned int i;
34447e4937aSGao Xiang 
3459f6cc76eSGao Xiang 		for (i = 0; i < pcl->pclusterpages; ++i) {
34647e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
34747e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
34847e4937aSGao Xiang 				ret = 1;
34947e4937aSGao Xiang 				break;
35047e4937aSGao Xiang 			}
35147e4937aSGao Xiang 		}
35247e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
35347e4937aSGao Xiang 
3546aaa7b06SGao Xiang 		if (ret)
3556aaa7b06SGao Xiang 			detach_page_private(page);
35647e4937aSGao Xiang 	}
35747e4937aSGao Xiang 	return ret;
35847e4937aSGao Xiang }
35947e4937aSGao Xiang 
36047e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
36181382f5fSGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
36247e4937aSGao Xiang 				   struct page *page)
36347e4937aSGao Xiang {
36447e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = clt->pcl;
36547e4937aSGao Xiang 
36681382f5fSGao Xiang 	while (clt->icpage_ptr > pcl->compressed_pages)
36781382f5fSGao Xiang 		if (!cmpxchg(--clt->icpage_ptr, NULL, page))
36847e4937aSGao Xiang 			return true;
36947e4937aSGao Xiang 	return false;
37047e4937aSGao Xiang }
37147e4937aSGao Xiang 
37247e4937aSGao Xiang /* callers must be with collection lock held */
37347e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt,
37447e4937aSGao Xiang 			       struct page *page,
37547e4937aSGao Xiang 			       enum z_erofs_page_type type)
37647e4937aSGao Xiang {
37747e4937aSGao Xiang 	int ret;
37847e4937aSGao Xiang 
37947e4937aSGao Xiang 	/* give priority for inplaceio */
38047e4937aSGao Xiang 	if (clt->mode >= COLLECT_PRIMARY &&
38147e4937aSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
38299634bf3SGao Xiang 	    z_erofs_try_inplace_io(clt, page))
38347e4937aSGao Xiang 		return 0;
38447e4937aSGao Xiang 
3857dea3de7SYue Hu 	ret = z_erofs_pagevec_enqueue(&clt->vector, page, type);
38647e4937aSGao Xiang 	clt->cl->vcnt += (unsigned int)ret;
38747e4937aSGao Xiang 
38847e4937aSGao Xiang 	return ret ? 0 : -EAGAIN;
38947e4937aSGao Xiang }
39047e4937aSGao Xiang 
391473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
39247e4937aSGao Xiang {
393473e15b0SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
394473e15b0SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
39547e4937aSGao Xiang 
396473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
397473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
398473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
39947e4937aSGao Xiang 		*owned_head = &pcl->next;
400473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
401473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED;
402473e15b0SGao Xiang 		return;
403473e15b0SGao Xiang 	}
404473e15b0SGao Xiang 
40547e4937aSGao Xiang 	/*
406473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
407473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
40847e4937aSGao Xiang 	 */
40947e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
410473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
41147e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
412473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_HOOKED;
413473e15b0SGao Xiang 		clt->tailpcl = NULL;
414473e15b0SGao Xiang 		return;
41547e4937aSGao Xiang 	}
416473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
417473e15b0SGao Xiang 	clt->mode = COLLECT_PRIMARY;
41847e4937aSGao Xiang }
41947e4937aSGao Xiang 
4209e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
42147e4937aSGao Xiang 				     struct inode *inode,
42247e4937aSGao Xiang 				     struct erofs_map_blocks *map)
42347e4937aSGao Xiang {
42464094a04SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
42547e4937aSGao Xiang 	struct z_erofs_collection *cl;
42647e4937aSGao Xiang 	unsigned int length;
42747e4937aSGao Xiang 
42864094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
42947e4937aSGao Xiang 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
43047e4937aSGao Xiang 		DBG_BUGON(1);
4319e579fc1SGao Xiang 		return -EFSCORRUPTED;
43247e4937aSGao Xiang 	}
43347e4937aSGao Xiang 
43447e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
4358d8a09b0SGao Xiang 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
43647e4937aSGao Xiang 		DBG_BUGON(1);
4379e579fc1SGao Xiang 		return -EFSCORRUPTED;
43847e4937aSGao Xiang 	}
43947e4937aSGao Xiang 
44047e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
44147e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
44247e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
44347e4937aSGao Xiang 			DBG_BUGON(1);
4449e579fc1SGao Xiang 			return -EFSCORRUPTED;
44547e4937aSGao Xiang 		}
44647e4937aSGao Xiang 	} else {
44747e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
44847e4937aSGao Xiang 
44947e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
45047e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
45147e4937aSGao Xiang 
45247e4937aSGao Xiang 		while (llen > length &&
45347e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
45447e4937aSGao Xiang 			cpu_relax();
45547e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
45647e4937aSGao Xiang 		}
45747e4937aSGao Xiang 	}
45847e4937aSGao Xiang 	mutex_lock(&cl->lock);
45947e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
46047e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
46147e4937aSGao Xiang 		clt->tailpcl = pcl;
462473e15b0SGao Xiang 
463473e15b0SGao Xiang 	z_erofs_try_to_claim_pcluster(clt);
46447e4937aSGao Xiang 	clt->cl = cl;
4659e579fc1SGao Xiang 	return 0;
46647e4937aSGao Xiang }
46747e4937aSGao Xiang 
4689e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt,
46947e4937aSGao Xiang 				       struct inode *inode,
47047e4937aSGao Xiang 				       struct erofs_map_blocks *map)
47147e4937aSGao Xiang {
47247e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
47347e4937aSGao Xiang 	struct z_erofs_collection *cl;
47464094a04SGao Xiang 	struct erofs_workgroup *grp;
47547e4937aSGao Xiang 	int err;
47647e4937aSGao Xiang 
4778f899262SGao Xiang 	if (!(map->m_flags & EROFS_MAP_ENCODED)) {
4788f899262SGao Xiang 		DBG_BUGON(1);
4798f899262SGao Xiang 		return -EFSCORRUPTED;
4808f899262SGao Xiang 	}
4818f899262SGao Xiang 
4829f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
4839f6cc76eSGao Xiang 	pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
4849f6cc76eSGao Xiang 	if (IS_ERR(pcl))
4859f6cc76eSGao Xiang 		return PTR_ERR(pcl);
48647e4937aSGao Xiang 
48764094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
48847e4937aSGao Xiang 	pcl->obj.index = map->m_pa >> PAGE_SHIFT;
4898f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
49047e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
49147e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
49247e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
49347e4937aSGao Xiang 
49447e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
49547e4937aSGao Xiang 	pcl->next = clt->owned_head;
49647e4937aSGao Xiang 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
49747e4937aSGao Xiang 
49847e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
49947e4937aSGao Xiang 	cl->pageofs = map->m_la & ~PAGE_MASK;
50047e4937aSGao Xiang 
50147e4937aSGao Xiang 	/*
50247e4937aSGao Xiang 	 * lock all primary followed works before visible to others
50347e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
50447e4937aSGao Xiang 	 */
5059f6cc76eSGao Xiang 	mutex_init(&cl->lock);
50664094a04SGao Xiang 	DBG_BUGON(!mutex_trylock(&cl->lock));
50747e4937aSGao Xiang 
50864094a04SGao Xiang 	grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
50964094a04SGao Xiang 	if (IS_ERR(grp)) {
51064094a04SGao Xiang 		err = PTR_ERR(grp);
51164094a04SGao Xiang 		goto err_out;
51264094a04SGao Xiang 	}
51364094a04SGao Xiang 
51464094a04SGao Xiang 	if (grp != &pcl->obj) {
51564094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
51664094a04SGao Xiang 		err = -EEXIST;
51764094a04SGao Xiang 		goto err_out;
51847e4937aSGao Xiang 	}
51947e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
52047e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
52147e4937aSGao Xiang 		clt->tailpcl = pcl;
52247e4937aSGao Xiang 	clt->owned_head = &pcl->next;
52347e4937aSGao Xiang 	clt->pcl = pcl;
52447e4937aSGao Xiang 	clt->cl = cl;
5259e579fc1SGao Xiang 	return 0;
52664094a04SGao Xiang 
52764094a04SGao Xiang err_out:
52864094a04SGao Xiang 	mutex_unlock(&cl->lock);
5299f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
53064094a04SGao Xiang 	return err;
53147e4937aSGao Xiang }
53247e4937aSGao Xiang 
53347e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt,
53447e4937aSGao Xiang 				   struct inode *inode,
53547e4937aSGao Xiang 				   struct erofs_map_blocks *map)
53647e4937aSGao Xiang {
53764094a04SGao Xiang 	struct erofs_workgroup *grp;
5389e579fc1SGao Xiang 	int ret;
53947e4937aSGao Xiang 
54047e4937aSGao Xiang 	DBG_BUGON(clt->cl);
54147e4937aSGao Xiang 
54247e4937aSGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
54347e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
54447e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
54547e4937aSGao Xiang 
54647e4937aSGao Xiang 	if (!PAGE_ALIGNED(map->m_pa)) {
54747e4937aSGao Xiang 		DBG_BUGON(1);
54847e4937aSGao Xiang 		return -EINVAL;
54947e4937aSGao Xiang 	}
55047e4937aSGao Xiang 
55164094a04SGao Xiang 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
55264094a04SGao Xiang 	if (grp) {
55364094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
55464094a04SGao Xiang 	} else {
5559e579fc1SGao Xiang 		ret = z_erofs_register_collection(clt, inode, map);
55647e4937aSGao Xiang 
55764094a04SGao Xiang 		if (!ret)
55864094a04SGao Xiang 			goto out;
55964094a04SGao Xiang 		if (ret != -EEXIST)
5609e579fc1SGao Xiang 			return ret;
56164094a04SGao Xiang 	}
56247e4937aSGao Xiang 
56364094a04SGao Xiang 	ret = z_erofs_lookup_collection(clt, inode, map);
56464094a04SGao Xiang 	if (ret) {
56564094a04SGao Xiang 		erofs_workgroup_put(&clt->pcl->obj);
56664094a04SGao Xiang 		return ret;
56764094a04SGao Xiang 	}
56864094a04SGao Xiang 
56964094a04SGao Xiang out:
57047e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
5719e579fc1SGao Xiang 				  clt->cl->pagevec, clt->cl->vcnt);
57247e4937aSGao Xiang 
57381382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
57481382f5fSGao Xiang 	clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages;
57547e4937aSGao Xiang 	return 0;
57647e4937aSGao Xiang }
57747e4937aSGao Xiang 
57847e4937aSGao Xiang /*
57947e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
58047e4937aSGao Xiang  * only after a RCU grace period.
58147e4937aSGao Xiang  */
58247e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
58347e4937aSGao Xiang {
58447e4937aSGao Xiang 	struct z_erofs_collection *const cl =
58547e4937aSGao Xiang 		container_of(head, struct z_erofs_collection, rcu);
58647e4937aSGao Xiang 
5879f6cc76eSGao Xiang 	z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
58847e4937aSGao Xiang 					   primary_collection));
58947e4937aSGao Xiang }
59047e4937aSGao Xiang 
59147e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
59247e4937aSGao Xiang {
59347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
59447e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
59547e4937aSGao Xiang 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
59647e4937aSGao Xiang 
59747e4937aSGao Xiang 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
59847e4937aSGao Xiang }
59947e4937aSGao Xiang 
60047e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl)
60147e4937aSGao Xiang {
60247e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
60347e4937aSGao Xiang 		container_of(cl, struct z_erofs_pcluster, primary_collection);
60447e4937aSGao Xiang 
60547e4937aSGao Xiang 	erofs_workgroup_put(&pcl->obj);
60647e4937aSGao Xiang }
60747e4937aSGao Xiang 
60847e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt)
60947e4937aSGao Xiang {
61047e4937aSGao Xiang 	struct z_erofs_collection *cl = clt->cl;
61147e4937aSGao Xiang 
61247e4937aSGao Xiang 	if (!cl)
61347e4937aSGao Xiang 		return false;
61447e4937aSGao Xiang 
61547e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
61647e4937aSGao Xiang 	mutex_unlock(&cl->lock);
61747e4937aSGao Xiang 
61847e4937aSGao Xiang 	/*
61947e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
62047e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
62147e4937aSGao Xiang 	 */
62247e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
62347e4937aSGao Xiang 		z_erofs_collection_put(cl);
62447e4937aSGao Xiang 
62547e4937aSGao Xiang 	clt->cl = NULL;
62647e4937aSGao Xiang 	return true;
62747e4937aSGao Xiang }
62847e4937aSGao Xiang 
62947e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
63047e4937aSGao Xiang 				       unsigned int cachestrategy,
63147e4937aSGao Xiang 				       erofs_off_t la)
63247e4937aSGao Xiang {
63347e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
63447e4937aSGao Xiang 		return false;
63547e4937aSGao Xiang 
63647e4937aSGao Xiang 	if (fe->backmost)
63747e4937aSGao Xiang 		return true;
63847e4937aSGao Xiang 
63947e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
64047e4937aSGao Xiang 		la < fe->headoffset;
64147e4937aSGao Xiang }
64247e4937aSGao Xiang 
64347e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
644*eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
64547e4937aSGao Xiang {
64647e4937aSGao Xiang 	struct inode *const inode = fe->inode;
647bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
64847e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
64947e4937aSGao Xiang 	struct z_erofs_collector *const clt = &fe->clt;
65047e4937aSGao Xiang 	const loff_t offset = page_offset(page);
651dc76ea8cSGao Xiang 	bool tight = true;
65247e4937aSGao Xiang 
65347e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
65447e4937aSGao Xiang 	enum z_erofs_page_type page_type;
65547e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
65647e4937aSGao Xiang 	int err = 0;
65747e4937aSGao Xiang 
65847e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
65947e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
66047e4937aSGao Xiang 
66147e4937aSGao Xiang 	spiltted = 0;
66247e4937aSGao Xiang 	end = PAGE_SIZE;
66347e4937aSGao Xiang repeat:
66447e4937aSGao Xiang 	cur = end - 1;
66547e4937aSGao Xiang 
66647e4937aSGao Xiang 	/* lucky, within the range of the current map_blocks */
66747e4937aSGao Xiang 	if (offset + cur >= map->m_la &&
66847e4937aSGao Xiang 	    offset + cur < map->m_la + map->m_llen) {
66947e4937aSGao Xiang 		/* didn't get a valid collection previously (very rare) */
67047e4937aSGao Xiang 		if (!clt->cl)
67147e4937aSGao Xiang 			goto restart_now;
67247e4937aSGao Xiang 		goto hitted;
67347e4937aSGao Xiang 	}
67447e4937aSGao Xiang 
67547e4937aSGao Xiang 	/* go ahead the next map_blocks */
6764f761fa2SGao Xiang 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
67747e4937aSGao Xiang 
67847e4937aSGao Xiang 	if (z_erofs_collector_end(clt))
67947e4937aSGao Xiang 		fe->backmost = false;
68047e4937aSGao Xiang 
68147e4937aSGao Xiang 	map->m_la = offset + cur;
68247e4937aSGao Xiang 	map->m_llen = 0;
68347e4937aSGao Xiang 	err = z_erofs_map_blocks_iter(inode, map, 0);
6848d8a09b0SGao Xiang 	if (err)
68547e4937aSGao Xiang 		goto err_out;
68647e4937aSGao Xiang 
68747e4937aSGao Xiang restart_now:
6888d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
68947e4937aSGao Xiang 		goto hitted;
69047e4937aSGao Xiang 
69147e4937aSGao Xiang 	err = z_erofs_collector_begin(clt, inode, map);
6928d8a09b0SGao Xiang 	if (err)
69347e4937aSGao Xiang 		goto err_out;
69447e4937aSGao Xiang 
69547e4937aSGao Xiang 	/* preload all compressed pages (maybe downgrade role if necessary) */
696e6242465SGao Xiang 	if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy, map->m_la))
6971825c8d7SGao Xiang 		cache_strategy = TRYALLOC;
69847e4937aSGao Xiang 	else
69947e4937aSGao Xiang 		cache_strategy = DONTALLOC;
70047e4937aSGao Xiang 
7011825c8d7SGao Xiang 	preload_compressed_pages(clt, MNGD_MAPPING(sbi),
7021825c8d7SGao Xiang 				 cache_strategy, pagepool);
70347e4937aSGao Xiang 
70447e4937aSGao Xiang hitted:
705dc76ea8cSGao Xiang 	/*
706dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
707dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
708dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
709dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
710dc76ea8cSGao Xiang 	 */
711dc76ea8cSGao Xiang 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
712dc76ea8cSGao Xiang 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
713dc76ea8cSGao Xiang 
71447e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
7158d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
71647e4937aSGao Xiang 		zero_user_segment(page, cur, end);
71747e4937aSGao Xiang 		goto next_part;
71847e4937aSGao Xiang 	}
71947e4937aSGao Xiang 
72047e4937aSGao Xiang 	/* let's derive page type */
72147e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
72247e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
72347e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
72447e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
72547e4937aSGao Xiang 
72647e4937aSGao Xiang 	if (cur)
72747e4937aSGao Xiang 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
72847e4937aSGao Xiang 
72947e4937aSGao Xiang retry:
73047e4937aSGao Xiang 	err = z_erofs_attach_page(clt, page, page_type);
7316aaa7b06SGao Xiang 	/* should allocate an additional short-lived page for pagevec */
73247e4937aSGao Xiang 	if (err == -EAGAIN) {
73347e4937aSGao Xiang 		struct page *const newpage =
734e3f78d5eSChao Yu 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
73547e4937aSGao Xiang 
7366aaa7b06SGao Xiang 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
73747e4937aSGao Xiang 		err = z_erofs_attach_page(clt, newpage,
73847e4937aSGao Xiang 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
7398d8a09b0SGao Xiang 		if (!err)
74047e4937aSGao Xiang 			goto retry;
74147e4937aSGao Xiang 	}
74247e4937aSGao Xiang 
7438d8a09b0SGao Xiang 	if (err)
74447e4937aSGao Xiang 		goto err_out;
74547e4937aSGao Xiang 
74647e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
74747e4937aSGao Xiang 
74847e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
74947e4937aSGao Xiang 
75047e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
75147e4937aSGao Xiang 	++spiltted;
75247e4937aSGao Xiang 	/* also update nr_pages */
75347e4937aSGao Xiang 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
75447e4937aSGao Xiang next_part:
75547e4937aSGao Xiang 	/* can be used for verification */
75647e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
75747e4937aSGao Xiang 
75847e4937aSGao Xiang 	end = cur;
75947e4937aSGao Xiang 	if (end > 0)
76047e4937aSGao Xiang 		goto repeat;
76147e4937aSGao Xiang 
76247e4937aSGao Xiang out:
76347e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
76447e4937aSGao Xiang 
7654f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
76647e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
76747e4937aSGao Xiang 	return err;
76847e4937aSGao Xiang 
76947e4937aSGao Xiang 	/* if some error occurred while processing this page */
77047e4937aSGao Xiang err_out:
77147e4937aSGao Xiang 	SetPageError(page);
77247e4937aSGao Xiang 	goto out;
77347e4937aSGao Xiang }
77447e4937aSGao Xiang 
775648f2de0SHuang Jianan static void z_erofs_decompressqueue_work(struct work_struct *work);
776a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
777a4b1fab1SGao Xiang 				       bool sync, int bios)
77847e4937aSGao Xiang {
77930048cdaSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
78030048cdaSHuang Jianan 
781a4b1fab1SGao Xiang 	/* wake up the caller thread for sync decompression */
782a4b1fab1SGao Xiang 	if (sync) {
78347e4937aSGao Xiang 		unsigned long flags;
78447e4937aSGao Xiang 
78547e4937aSGao Xiang 		spin_lock_irqsave(&io->u.wait.lock, flags);
78647e4937aSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
78747e4937aSGao Xiang 			wake_up_locked(&io->u.wait);
78847e4937aSGao Xiang 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
78947e4937aSGao Xiang 		return;
79047e4937aSGao Xiang 	}
79147e4937aSGao Xiang 
792648f2de0SHuang Jianan 	if (atomic_add_return(bios, &io->pending_bios))
793648f2de0SHuang Jianan 		return;
79430048cdaSHuang Jianan 	/* Use workqueue and sync decompression for atomic contexts only */
795648f2de0SHuang Jianan 	if (in_atomic() || irqs_disabled()) {
79647e4937aSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
797e6242465SGao Xiang 		sbi->opt.readahead_sync_decompress = true;
798648f2de0SHuang Jianan 		return;
799648f2de0SHuang Jianan 	}
800648f2de0SHuang Jianan 	z_erofs_decompressqueue_work(&io->u.work);
80147e4937aSGao Xiang }
80247e4937aSGao Xiang 
8036aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
8046aaa7b06SGao Xiang {
8056aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
8066aaa7b06SGao Xiang }
8076aaa7b06SGao Xiang 
8080c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
80947e4937aSGao Xiang {
810a4b1fab1SGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
811a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
81247e4937aSGao Xiang 	blk_status_t err = bio->bi_status;
81347e4937aSGao Xiang 	struct bio_vec *bvec;
81447e4937aSGao Xiang 	struct bvec_iter_all iter_all;
81547e4937aSGao Xiang 
81647e4937aSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
81747e4937aSGao Xiang 		struct page *page = bvec->bv_page;
81847e4937aSGao Xiang 
81947e4937aSGao Xiang 		DBG_BUGON(PageUptodate(page));
8206aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
82147e4937aSGao Xiang 
8228d8a09b0SGao Xiang 		if (err)
82347e4937aSGao Xiang 			SetPageError(page);
82447e4937aSGao Xiang 
825a4b1fab1SGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
826a4b1fab1SGao Xiang 			if (!err)
827a4b1fab1SGao Xiang 				SetPageUptodate(page);
82847e4937aSGao Xiang 			unlock_page(page);
82947e4937aSGao Xiang 		}
830a4b1fab1SGao Xiang 	}
831a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
83247e4937aSGao Xiang 	bio_put(bio);
83347e4937aSGao Xiang }
83447e4937aSGao Xiang 
83547e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
83647e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
837*eaa9172aSGao Xiang 				       struct page **pagepool)
83847e4937aSGao Xiang {
83947e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
84047e4937aSGao Xiang 	struct z_erofs_pagevec_ctor ctor;
8419f6cc76eSGao Xiang 	unsigned int i, inputsize, outputsize, llen, nr_pages;
84247e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
84347e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
84447e4937aSGao Xiang 
84547e4937aSGao Xiang 	enum z_erofs_page_type page_type;
84647e4937aSGao Xiang 	bool overlapped, partial;
84747e4937aSGao Xiang 	struct z_erofs_collection *cl;
84847e4937aSGao Xiang 	int err;
84947e4937aSGao Xiang 
85047e4937aSGao Xiang 	might_sleep();
85147e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
85247e4937aSGao Xiang 	DBG_BUGON(!READ_ONCE(cl->nr_pages));
85347e4937aSGao Xiang 
85447e4937aSGao Xiang 	mutex_lock(&cl->lock);
85547e4937aSGao Xiang 	nr_pages = cl->nr_pages;
85647e4937aSGao Xiang 
8578d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
85847e4937aSGao Xiang 		pages = pages_onstack;
85947e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
86047e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
86147e4937aSGao Xiang 		pages = z_pagemap_global;
86247e4937aSGao Xiang 	} else {
86347e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
86447e4937aSGao Xiang 
86547e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
86647e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
86747e4937aSGao Xiang 
86847e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
86947e4937aSGao Xiang 				       gfp_flags);
87047e4937aSGao Xiang 
87147e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
8728d8a09b0SGao Xiang 		if (!pages) {
87347e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
87447e4937aSGao Xiang 			pages = z_pagemap_global;
87547e4937aSGao Xiang 		}
87647e4937aSGao Xiang 	}
87747e4937aSGao Xiang 
87847e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
87947e4937aSGao Xiang 		pages[i] = NULL;
88047e4937aSGao Xiang 
88147e4937aSGao Xiang 	err = 0;
88247e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
88347e4937aSGao Xiang 				  cl->pagevec, 0);
88447e4937aSGao Xiang 
88547e4937aSGao Xiang 	for (i = 0; i < cl->vcnt; ++i) {
88647e4937aSGao Xiang 		unsigned int pagenr;
88747e4937aSGao Xiang 
88847e4937aSGao Xiang 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
88947e4937aSGao Xiang 
89047e4937aSGao Xiang 		/* all pages in pagevec ought to be valid */
89147e4937aSGao Xiang 		DBG_BUGON(!page);
8926aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
89347e4937aSGao Xiang 
8946aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
89547e4937aSGao Xiang 			continue;
89647e4937aSGao Xiang 
89747e4937aSGao Xiang 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
89847e4937aSGao Xiang 			pagenr = 0;
89947e4937aSGao Xiang 		else
90047e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
90147e4937aSGao Xiang 
90247e4937aSGao Xiang 		DBG_BUGON(pagenr >= nr_pages);
90347e4937aSGao Xiang 
90447e4937aSGao Xiang 		/*
90547e4937aSGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
90647e4937aSGao Xiang 		 * so here erroring out one multiref page.
90747e4937aSGao Xiang 		 */
9088d8a09b0SGao Xiang 		if (pages[pagenr]) {
90947e4937aSGao Xiang 			DBG_BUGON(1);
91047e4937aSGao Xiang 			SetPageError(pages[pagenr]);
91147e4937aSGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
91247e4937aSGao Xiang 			err = -EFSCORRUPTED;
91347e4937aSGao Xiang 		}
91447e4937aSGao Xiang 		pages[pagenr] = page;
91547e4937aSGao Xiang 	}
91647e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&ctor, true);
91747e4937aSGao Xiang 
91847e4937aSGao Xiang 	overlapped = false;
91947e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
92047e4937aSGao Xiang 
9219f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
92247e4937aSGao Xiang 		unsigned int pagenr;
92347e4937aSGao Xiang 
92447e4937aSGao Xiang 		page = compressed_pages[i];
92547e4937aSGao Xiang 
92647e4937aSGao Xiang 		/* all compressed pages ought to be valid */
92747e4937aSGao Xiang 		DBG_BUGON(!page);
9286aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
92947e4937aSGao Xiang 
9306aaa7b06SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
93147e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
9328d8a09b0SGao Xiang 				if (!PageUptodate(page))
93347e4937aSGao Xiang 					err = -EIO;
93447e4937aSGao Xiang 				continue;
93547e4937aSGao Xiang 			}
93647e4937aSGao Xiang 
93747e4937aSGao Xiang 			/*
93847e4937aSGao Xiang 			 * only if non-head page can be selected
93947e4937aSGao Xiang 			 * for inplace decompression
94047e4937aSGao Xiang 			 */
94147e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
94247e4937aSGao Xiang 
94347e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
9448d8a09b0SGao Xiang 			if (pages[pagenr]) {
94547e4937aSGao Xiang 				DBG_BUGON(1);
94647e4937aSGao Xiang 				SetPageError(pages[pagenr]);
94747e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
94847e4937aSGao Xiang 				err = -EFSCORRUPTED;
94947e4937aSGao Xiang 			}
95047e4937aSGao Xiang 			pages[pagenr] = page;
95147e4937aSGao Xiang 
95247e4937aSGao Xiang 			overlapped = true;
95347e4937aSGao Xiang 		}
95447e4937aSGao Xiang 
9556aaa7b06SGao Xiang 		/* PG_error needs checking for all non-managed pages */
9568d8a09b0SGao Xiang 		if (PageError(page)) {
95747e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
95847e4937aSGao Xiang 			err = -EIO;
95947e4937aSGao Xiang 		}
96047e4937aSGao Xiang 	}
96147e4937aSGao Xiang 
9628d8a09b0SGao Xiang 	if (err)
96347e4937aSGao Xiang 		goto out;
96447e4937aSGao Xiang 
96547e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
96647e4937aSGao Xiang 	if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
96747e4937aSGao Xiang 		outputsize = llen;
96847e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
96947e4937aSGao Xiang 	} else {
97047e4937aSGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
97147e4937aSGao Xiang 		partial = true;
97247e4937aSGao Xiang 	}
97347e4937aSGao Xiang 
9749f6cc76eSGao Xiang 	inputsize = pcl->pclusterpages * PAGE_SIZE;
97547e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
97647e4937aSGao Xiang 					.sb = sb,
97747e4937aSGao Xiang 					.in = compressed_pages,
97847e4937aSGao Xiang 					.out = pages,
97947e4937aSGao Xiang 					.pageofs_out = cl->pageofs,
9809f6cc76eSGao Xiang 					.inputsize = inputsize,
98147e4937aSGao Xiang 					.outputsize = outputsize,
98247e4937aSGao Xiang 					.alg = pcl->algorithmformat,
98347e4937aSGao Xiang 					.inplace_io = overlapped,
98447e4937aSGao Xiang 					.partial_decoding = partial
98547e4937aSGao Xiang 				 }, pagepool);
98647e4937aSGao Xiang 
98747e4937aSGao Xiang out:
988fe6adcceSRuiqi Gong 	/* must handle all compressed pages before ending pages */
9899f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
99047e4937aSGao Xiang 		page = compressed_pages[i];
99147e4937aSGao Xiang 
99247e4937aSGao Xiang 		if (erofs_page_is_managed(sbi, page))
99347e4937aSGao Xiang 			continue;
99447e4937aSGao Xiang 
9956aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
9966aaa7b06SGao Xiang 		(void)z_erofs_put_shortlivedpage(pagepool, page);
99747e4937aSGao Xiang 
99847e4937aSGao Xiang 		WRITE_ONCE(compressed_pages[i], NULL);
99947e4937aSGao Xiang 	}
100047e4937aSGao Xiang 
100147e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
100247e4937aSGao Xiang 		page = pages[i];
100347e4937aSGao Xiang 		if (!page)
100447e4937aSGao Xiang 			continue;
100547e4937aSGao Xiang 
10066aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
100747e4937aSGao Xiang 
10086aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
10096aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
101047e4937aSGao Xiang 			continue;
101147e4937aSGao Xiang 
10128d8a09b0SGao Xiang 		if (err < 0)
101347e4937aSGao Xiang 			SetPageError(page);
101447e4937aSGao Xiang 
101547e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
101647e4937aSGao Xiang 	}
101747e4937aSGao Xiang 
101847e4937aSGao Xiang 	if (pages == z_pagemap_global)
101947e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
10208d8a09b0SGao Xiang 	else if (pages != pages_onstack)
102147e4937aSGao Xiang 		kvfree(pages);
102247e4937aSGao Xiang 
102347e4937aSGao Xiang 	cl->nr_pages = 0;
102447e4937aSGao Xiang 	cl->vcnt = 0;
102547e4937aSGao Xiang 
102647e4937aSGao Xiang 	/* all cl locks MUST be taken before the following line */
102747e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
102847e4937aSGao Xiang 
102947e4937aSGao Xiang 	/* all cl locks SHOULD be released right now */
103047e4937aSGao Xiang 	mutex_unlock(&cl->lock);
103147e4937aSGao Xiang 
103247e4937aSGao Xiang 	z_erofs_collection_put(cl);
103347e4937aSGao Xiang 	return err;
103447e4937aSGao Xiang }
103547e4937aSGao Xiang 
10360c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1037*eaa9172aSGao Xiang 				     struct page **pagepool)
103847e4937aSGao Xiang {
103947e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
104047e4937aSGao Xiang 
104147e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
104247e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
104347e4937aSGao Xiang 
104447e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
104547e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
104647e4937aSGao Xiang 
104747e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
104847e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
104947e4937aSGao Xiang 
105047e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
105147e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
105247e4937aSGao Xiang 
1053a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
105447e4937aSGao Xiang 	}
105547e4937aSGao Xiang }
105647e4937aSGao Xiang 
10570c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
105847e4937aSGao Xiang {
1059a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1060a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1061*eaa9172aSGao Xiang 	struct page *pagepool = NULL;
106247e4937aSGao Xiang 
1063a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
10640c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
106547e4937aSGao Xiang 
1066*eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1067a4b1fab1SGao Xiang 	kvfree(bgq);
106847e4937aSGao Xiang }
106947e4937aSGao Xiang 
107047e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
107147e4937aSGao Xiang 					       unsigned int nr,
1072*eaa9172aSGao Xiang 					       struct page **pagepool,
107347e4937aSGao Xiang 					       struct address_space *mc,
107447e4937aSGao Xiang 					       gfp_t gfp)
107547e4937aSGao Xiang {
107647e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
107747e4937aSGao Xiang 	bool tocache = false;
107847e4937aSGao Xiang 
107947e4937aSGao Xiang 	struct address_space *mapping;
108047e4937aSGao Xiang 	struct page *oldpage, *page;
108147e4937aSGao Xiang 
108247e4937aSGao Xiang 	compressed_page_t t;
108347e4937aSGao Xiang 	int justfound;
108447e4937aSGao Xiang 
108547e4937aSGao Xiang repeat:
108647e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
108747e4937aSGao Xiang 	oldpage = page;
108847e4937aSGao Xiang 
108947e4937aSGao Xiang 	if (!page)
109047e4937aSGao Xiang 		goto out_allocpage;
109147e4937aSGao Xiang 
109247e4937aSGao Xiang 	/*
109347e4937aSGao Xiang 	 * the cached page has not been allocated and
109447e4937aSGao Xiang 	 * an placeholder is out there, prepare it now.
109547e4937aSGao Xiang 	 */
1096bda17a45SGao Xiang 	if (page == PAGE_UNALLOCATED) {
109747e4937aSGao Xiang 		tocache = true;
109847e4937aSGao Xiang 		goto out_allocpage;
109947e4937aSGao Xiang 	}
110047e4937aSGao Xiang 
110147e4937aSGao Xiang 	/* process the target tagged pointer */
110247e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
110347e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
110447e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
110547e4937aSGao Xiang 
11061825c8d7SGao Xiang 	/*
11071825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
11081825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
11091825c8d7SGao Xiang 	 */
11101825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
11111825c8d7SGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
11121825c8d7SGao Xiang 		set_page_private(page, 0);
11131825c8d7SGao Xiang 		tocache = true;
11141825c8d7SGao Xiang 		goto out_tocache;
11151825c8d7SGao Xiang 	}
111647e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
111747e4937aSGao Xiang 
111847e4937aSGao Xiang 	/*
11196aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
112047e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
112147e4937aSGao Xiang 	 */
112247e4937aSGao Xiang 	if (mapping && mapping != mc)
112347e4937aSGao Xiang 		/* ought to be unmanaged pages */
112447e4937aSGao Xiang 		goto out;
112547e4937aSGao Xiang 
11266aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
11276aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
11286aaa7b06SGao Xiang 		goto out;
11296aaa7b06SGao Xiang 
113047e4937aSGao Xiang 	lock_page(page);
113147e4937aSGao Xiang 
113247e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
113347e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
113447e4937aSGao Xiang 
113547e4937aSGao Xiang 	/* the page is still in manage cache */
113647e4937aSGao Xiang 	if (page->mapping == mc) {
113747e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
113847e4937aSGao Xiang 
113947e4937aSGao Xiang 		ClearPageError(page);
114047e4937aSGao Xiang 		if (!PagePrivate(page)) {
114147e4937aSGao Xiang 			/*
114247e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
114347e4937aSGao Xiang 			 * the current restriction as well if
114447e4937aSGao Xiang 			 * the page is already in compressed_pages[].
114547e4937aSGao Xiang 			 */
114647e4937aSGao Xiang 			DBG_BUGON(!justfound);
114747e4937aSGao Xiang 
114847e4937aSGao Xiang 			justfound = 0;
114947e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
115047e4937aSGao Xiang 			SetPagePrivate(page);
115147e4937aSGao Xiang 		}
115247e4937aSGao Xiang 
115347e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
115447e4937aSGao Xiang 		if (PageUptodate(page)) {
115547e4937aSGao Xiang 			unlock_page(page);
115647e4937aSGao Xiang 			page = NULL;
115747e4937aSGao Xiang 		}
115847e4937aSGao Xiang 		goto out;
115947e4937aSGao Xiang 	}
116047e4937aSGao Xiang 
116147e4937aSGao Xiang 	/*
116247e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
116347e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
116447e4937aSGao Xiang 	 */
116547e4937aSGao Xiang 	DBG_BUGON(page->mapping);
116647e4937aSGao Xiang 	DBG_BUGON(!justfound);
116747e4937aSGao Xiang 
116847e4937aSGao Xiang 	tocache = true;
116947e4937aSGao Xiang 	unlock_page(page);
117047e4937aSGao Xiang 	put_page(page);
117147e4937aSGao Xiang out_allocpage:
11725ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
11735ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1174*eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
11755ddcee1fSGao Xiang 		cond_resched();
11765ddcee1fSGao Xiang 		goto repeat;
11775ddcee1fSGao Xiang 	}
11781825c8d7SGao Xiang out_tocache:
1179bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1180bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1181bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1182bf225074SGao Xiang 		goto out;
1183a30573b3SGao Xiang 	}
1184bf225074SGao Xiang 	attach_page_private(page, pcl);
1185bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1186bf225074SGao Xiang 	put_page(page);
1187bf225074SGao Xiang 
118847e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
118947e4937aSGao Xiang 	return page;
119047e4937aSGao Xiang }
119147e4937aSGao Xiang 
1192a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1193a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1194a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
119547e4937aSGao Xiang {
1196a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
119747e4937aSGao Xiang 
1198a4b1fab1SGao Xiang 	if (fg && !*fg) {
1199a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1200a4b1fab1SGao Xiang 		if (!q) {
1201a4b1fab1SGao Xiang 			*fg = true;
1202a4b1fab1SGao Xiang 			goto fg_out;
120347e4937aSGao Xiang 		}
12040c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1205a4b1fab1SGao Xiang 	} else {
1206a4b1fab1SGao Xiang fg_out:
1207a4b1fab1SGao Xiang 		q = fgq;
1208a4b1fab1SGao Xiang 		init_waitqueue_head(&fgq->u.wait);
1209a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1210a4b1fab1SGao Xiang 	}
1211a4b1fab1SGao Xiang 	q->sb = sb;
1212a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1213a4b1fab1SGao Xiang 	return q;
121447e4937aSGao Xiang }
121547e4937aSGao Xiang 
121647e4937aSGao Xiang /* define decompression jobqueue types */
121747e4937aSGao Xiang enum {
121847e4937aSGao Xiang 	JQ_BYPASS,
121947e4937aSGao Xiang 	JQ_SUBMIT,
122047e4937aSGao Xiang 	NR_JOBQUEUES,
122147e4937aSGao Xiang };
122247e4937aSGao Xiang 
122347e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1224a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1225a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
122647e4937aSGao Xiang {
122747e4937aSGao Xiang 	/*
122847e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
122947e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
123047e4937aSGao Xiang 	 */
1231a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1232a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
123347e4937aSGao Xiang 
1234a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
123547e4937aSGao Xiang }
123647e4937aSGao Xiang 
123747e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
123847e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
123947e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
124047e4937aSGao Xiang {
124147e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
124247e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
124347e4937aSGao Xiang 
124447e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
124547e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
124647e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
124747e4937aSGao Xiang 
124847e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
124947e4937aSGao Xiang 
125047e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
125147e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
125247e4937aSGao Xiang 
125347e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
125447e4937aSGao Xiang }
125547e4937aSGao Xiang 
12561e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb,
12576ea5aad3SGao Xiang 				 struct z_erofs_decompress_frontend *f,
1258*eaa9172aSGao Xiang 				 struct page **pagepool,
1259a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1260a4b1fab1SGao Xiang 				 bool *force_fg)
126147e4937aSGao Xiang {
1262bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
126347e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1264a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
126547e4937aSGao Xiang 	void *bi_private;
12666ea5aad3SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1267dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
12683f649ab7SKees Cook 	pgoff_t last_index;
1269dfeab2e9SGao Xiang 	struct block_device *last_bdev;
12701e4a2955SGao Xiang 	unsigned int nr_bios = 0;
12711e4a2955SGao Xiang 	struct bio *bio = NULL;
127247e4937aSGao Xiang 
1273a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1274a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1275a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
127647e4937aSGao Xiang 
127747e4937aSGao Xiang 	/* by default, all need io submission */
127847e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
127947e4937aSGao Xiang 
128047e4937aSGao Xiang 	do {
1281dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
128247e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
12831e4a2955SGao Xiang 		pgoff_t cur, end;
12841e4a2955SGao Xiang 		unsigned int i = 0;
12851e4a2955SGao Xiang 		bool bypass = true;
128647e4937aSGao Xiang 
128747e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
128847e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
128947e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
129047e4937aSGao Xiang 
129147e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
129247e4937aSGao Xiang 
1293dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1294dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
1295dfeab2e9SGao Xiang 			.m_pa = blknr_to_addr(pcl->obj.index),
1296dfeab2e9SGao Xiang 		};
1297dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1298dfeab2e9SGao Xiang 
1299dfeab2e9SGao Xiang 		cur = erofs_blknr(mdev.m_pa);
13009f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
130147e4937aSGao Xiang 
130247e4937aSGao Xiang 		/* close the main owned chain at first */
130347e4937aSGao Xiang 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
130447e4937aSGao Xiang 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
130547e4937aSGao Xiang 
13061e4a2955SGao Xiang 		do {
13071e4a2955SGao Xiang 			struct page *page;
130847e4937aSGao Xiang 
13091e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
131047e4937aSGao Xiang 							  MNGD_MAPPING(sbi),
131147e4937aSGao Xiang 							  GFP_NOFS);
13121e4a2955SGao Xiang 			if (!page)
13131e4a2955SGao Xiang 				continue;
131447e4937aSGao Xiang 
1315dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1316dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
131747e4937aSGao Xiang submit_bio_retry:
131894e4e153SGao Xiang 				submit_bio(bio);
131947e4937aSGao Xiang 				bio = NULL;
132047e4937aSGao Xiang 			}
132147e4937aSGao Xiang 
132247e4937aSGao Xiang 			if (!bio) {
1323a8affc03SChristoph Hellwig 				bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS);
13240c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1325dfeab2e9SGao Xiang 
1326dfeab2e9SGao Xiang 				bio_set_dev(bio, mdev.m_bdev);
1327dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
13281e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1329a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1330a5c0b780SGao Xiang 				bio->bi_private = bi_private;
133194e4e153SGao Xiang 				bio->bi_opf = REQ_OP_READ;
13326ea5aad3SGao Xiang 				if (f->readahead)
13336ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
133447e4937aSGao Xiang 				++nr_bios;
133547e4937aSGao Xiang 			}
133647e4937aSGao Xiang 
13376c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
133847e4937aSGao Xiang 				goto submit_bio_retry;
133947e4937aSGao Xiang 
13401e4a2955SGao Xiang 			last_index = cur;
13411e4a2955SGao Xiang 			bypass = false;
13421e4a2955SGao Xiang 		} while (++cur < end);
134347e4937aSGao Xiang 
13441e4a2955SGao Xiang 		if (!bypass)
134547e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
134647e4937aSGao Xiang 		else
134747e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
134847e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
134947e4937aSGao Xiang 
135047e4937aSGao Xiang 	if (bio)
135194e4e153SGao Xiang 		submit_bio(bio);
135247e4937aSGao Xiang 
1353587a67b7SGao Xiang 	/*
1354587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1355587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1356587a67b7SGao Xiang 	 */
1357587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1358587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
13591e4a2955SGao Xiang 		return;
1360587a67b7SGao Xiang 	}
1361a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
136247e4937aSGao Xiang }
136347e4937aSGao Xiang 
13640c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb,
13656ea5aad3SGao Xiang 			     struct z_erofs_decompress_frontend *f,
1366*eaa9172aSGao Xiang 			     struct page **pagepool, bool force_fg)
136747e4937aSGao Xiang {
1368a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
136947e4937aSGao Xiang 
13706ea5aad3SGao Xiang 	if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
137147e4937aSGao Xiang 		return;
13726ea5aad3SGao Xiang 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
137347e4937aSGao Xiang 
13740c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
13750c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
137647e4937aSGao Xiang 
137747e4937aSGao Xiang 	if (!force_fg)
137847e4937aSGao Xiang 		return;
137947e4937aSGao Xiang 
138047e4937aSGao Xiang 	/* wait until all bios are completed */
1381a93f8c36SGao Xiang 	io_wait_event(io[JQ_SUBMIT].u.wait,
138247e4937aSGao Xiang 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
138347e4937aSGao Xiang 
13840c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
13850c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
138647e4937aSGao Xiang }
138747e4937aSGao Xiang 
138838629291SGao Xiang /*
138938629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
139038629291SGao Xiang  * approximate readmore strategies as a start.
139138629291SGao Xiang  */
139238629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
139338629291SGao Xiang 				      struct readahead_control *rac,
139438629291SGao Xiang 				      erofs_off_t end,
1395*eaa9172aSGao Xiang 				      struct page **pagepool,
139638629291SGao Xiang 				      bool backmost)
139738629291SGao Xiang {
139838629291SGao Xiang 	struct inode *inode = f->inode;
139938629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
140038629291SGao Xiang 	erofs_off_t cur;
140138629291SGao Xiang 	int err;
140238629291SGao Xiang 
140338629291SGao Xiang 	if (backmost) {
140438629291SGao Xiang 		map->m_la = end;
1405622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1406622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
140738629291SGao Xiang 		if (err)
140838629291SGao Xiang 			return;
140938629291SGao Xiang 
141038629291SGao Xiang 		/* expend ra for the trailing edge if readahead */
141138629291SGao Xiang 		if (rac) {
141238629291SGao Xiang 			loff_t newstart = readahead_pos(rac);
141338629291SGao Xiang 
141438629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
141538629291SGao Xiang 			readahead_expand(rac, newstart, cur - newstart);
141638629291SGao Xiang 			return;
141738629291SGao Xiang 		}
141838629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
141938629291SGao Xiang 	} else {
142038629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
142138629291SGao Xiang 
142238629291SGao Xiang 		if (!map->m_llen)
142338629291SGao Xiang 			return;
142438629291SGao Xiang 	}
142538629291SGao Xiang 
142638629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
142738629291SGao Xiang 	while (cur >= end) {
142838629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
142938629291SGao Xiang 		struct page *page;
143038629291SGao Xiang 
143138629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
143238629291SGao Xiang 		if (!page)
143338629291SGao Xiang 			goto skip;
143438629291SGao Xiang 
143538629291SGao Xiang 		if (PageUptodate(page)) {
143638629291SGao Xiang 			unlock_page(page);
143738629291SGao Xiang 			put_page(page);
143838629291SGao Xiang 			goto skip;
143938629291SGao Xiang 		}
144038629291SGao Xiang 
144138629291SGao Xiang 		err = z_erofs_do_read_page(f, page, pagepool);
144238629291SGao Xiang 		if (err)
144338629291SGao Xiang 			erofs_err(inode->i_sb,
144438629291SGao Xiang 				  "readmore error at page %lu @ nid %llu",
144538629291SGao Xiang 				  index, EROFS_I(inode)->nid);
144638629291SGao Xiang 		put_page(page);
144738629291SGao Xiang skip:
144838629291SGao Xiang 		if (cur < PAGE_SIZE)
144938629291SGao Xiang 			break;
145038629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
145138629291SGao Xiang 	}
145238629291SGao Xiang }
145338629291SGao Xiang 
14540c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page)
145547e4937aSGao Xiang {
145647e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
145747e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1458*eaa9172aSGao Xiang 	struct page *pagepool = NULL;
145947e4937aSGao Xiang 	int err;
146047e4937aSGao Xiang 
146147e4937aSGao Xiang 	trace_erofs_readpage(page, false);
146247e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
146347e4937aSGao Xiang 
146438629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
146538629291SGao Xiang 				  &pagepool, true);
14661825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
146738629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
146838629291SGao Xiang 
146947e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
147047e4937aSGao Xiang 
147147e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
14726ea5aad3SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
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 
1480*eaa9172aSGao 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);
1489*eaa9172aSGao 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,
152338629291SGao Xiang 			 sbi->opt.readahead_sync_decompress &&
152438629291SGao Xiang 			 nr_pages <= sbi->opt.max_sync_decompress_pages);
152547e4937aSGao Xiang 	if (f.map.mpage)
152647e4937aSGao Xiang 		put_page(f.map.mpage);
1527*eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
152847e4937aSGao Xiang }
152947e4937aSGao Xiang 
15300c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
15310c638f70SGao Xiang 	.readpage = z_erofs_readpage,
15320615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
153347e4937aSGao Xiang };
1534