xref: /openbmc/linux/fs/erofs/zdata.c (revision d252ff3d)
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,
2391825c8d7SGao Xiang 				     struct list_head *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 
2901825c8d7SGao Xiang 		if (page) {
29147e4937aSGao Xiang 			put_page(page);
2921825c8d7SGao Xiang 		} else if (newpage) {
2931825c8d7SGao Xiang 			set_page_private(newpage, 0);
2941825c8d7SGao Xiang 			list_add(&newpage->lru, pagepool);
2951825c8d7SGao Xiang 		}
29647e4937aSGao Xiang 	}
29747e4937aSGao Xiang 
2980b964600SGao Xiang 	/*
2990b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
3000b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
3010b964600SGao Xiang 	 */
3020b964600SGao Xiang 	if (standalone)
30347e4937aSGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
30447e4937aSGao Xiang }
30547e4937aSGao Xiang 
30647e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
30747e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
30847e4937aSGao Xiang 				       struct erofs_workgroup *grp)
30947e4937aSGao Xiang {
31047e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
31147e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
31247e4937aSGao Xiang 	int i;
31347e4937aSGao Xiang 
31447e4937aSGao Xiang 	/*
31547e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
31647e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
31747e4937aSGao Xiang 	 */
3189f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
31947e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
32047e4937aSGao Xiang 
32147e4937aSGao Xiang 		if (!page)
32247e4937aSGao Xiang 			continue;
32347e4937aSGao Xiang 
32447e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
32547e4937aSGao Xiang 		if (!trylock_page(page))
32647e4937aSGao Xiang 			return -EBUSY;
32747e4937aSGao Xiang 
328f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
32947e4937aSGao Xiang 			continue;
33047e4937aSGao Xiang 
33147e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
33247e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
3336aaa7b06SGao Xiang 		detach_page_private(page);
33447e4937aSGao Xiang 		unlock_page(page);
33547e4937aSGao Xiang 	}
33647e4937aSGao Xiang 	return 0;
33747e4937aSGao Xiang }
33847e4937aSGao Xiang 
339*d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
34047e4937aSGao Xiang {
34147e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
34247e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
34347e4937aSGao Xiang 
34447e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
34547e4937aSGao Xiang 		unsigned int i;
34647e4937aSGao Xiang 
3479f6cc76eSGao Xiang 		for (i = 0; i < pcl->pclusterpages; ++i) {
34847e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
34947e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
35047e4937aSGao Xiang 				ret = 1;
35147e4937aSGao Xiang 				break;
35247e4937aSGao Xiang 			}
35347e4937aSGao Xiang 		}
35447e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
35547e4937aSGao Xiang 
3566aaa7b06SGao Xiang 		if (ret)
3576aaa7b06SGao Xiang 			detach_page_private(page);
35847e4937aSGao Xiang 	}
35947e4937aSGao Xiang 	return ret;
36047e4937aSGao Xiang }
36147e4937aSGao Xiang 
36247e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
36381382f5fSGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
36447e4937aSGao Xiang 				   struct page *page)
36547e4937aSGao Xiang {
36647e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = clt->pcl;
36747e4937aSGao Xiang 
36881382f5fSGao Xiang 	while (clt->icpage_ptr > pcl->compressed_pages)
36981382f5fSGao Xiang 		if (!cmpxchg(--clt->icpage_ptr, NULL, page))
37047e4937aSGao Xiang 			return true;
37147e4937aSGao Xiang 	return false;
37247e4937aSGao Xiang }
37347e4937aSGao Xiang 
37447e4937aSGao Xiang /* callers must be with collection lock held */
37547e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt,
37647e4937aSGao Xiang 			       struct page *page,
37747e4937aSGao Xiang 			       enum z_erofs_page_type type)
37847e4937aSGao Xiang {
37947e4937aSGao Xiang 	int ret;
38047e4937aSGao Xiang 
38147e4937aSGao Xiang 	/* give priority for inplaceio */
38247e4937aSGao Xiang 	if (clt->mode >= COLLECT_PRIMARY &&
38347e4937aSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
38499634bf3SGao Xiang 	    z_erofs_try_inplace_io(clt, page))
38547e4937aSGao Xiang 		return 0;
38647e4937aSGao Xiang 
3877dea3de7SYue Hu 	ret = z_erofs_pagevec_enqueue(&clt->vector, page, type);
38847e4937aSGao Xiang 	clt->cl->vcnt += (unsigned int)ret;
38947e4937aSGao Xiang 
39047e4937aSGao Xiang 	return ret ? 0 : -EAGAIN;
39147e4937aSGao Xiang }
39247e4937aSGao Xiang 
393473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
39447e4937aSGao Xiang {
395473e15b0SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
396473e15b0SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
39747e4937aSGao Xiang 
398473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
399473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
400473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
40147e4937aSGao Xiang 		*owned_head = &pcl->next;
402473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
403473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED;
404473e15b0SGao Xiang 		return;
405473e15b0SGao Xiang 	}
406473e15b0SGao Xiang 
40747e4937aSGao Xiang 	/*
408473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
409473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
41047e4937aSGao Xiang 	 */
41147e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
412473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
41347e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
414473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_HOOKED;
415473e15b0SGao Xiang 		clt->tailpcl = NULL;
416473e15b0SGao Xiang 		return;
41747e4937aSGao Xiang 	}
418473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
419473e15b0SGao Xiang 	clt->mode = COLLECT_PRIMARY;
42047e4937aSGao Xiang }
42147e4937aSGao Xiang 
4229e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
42347e4937aSGao Xiang 				     struct inode *inode,
42447e4937aSGao Xiang 				     struct erofs_map_blocks *map)
42547e4937aSGao Xiang {
42664094a04SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
42747e4937aSGao Xiang 	struct z_erofs_collection *cl;
42847e4937aSGao Xiang 	unsigned int length;
42947e4937aSGao Xiang 
43064094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
43147e4937aSGao Xiang 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
43247e4937aSGao Xiang 		DBG_BUGON(1);
4339e579fc1SGao Xiang 		return -EFSCORRUPTED;
43447e4937aSGao Xiang 	}
43547e4937aSGao Xiang 
43647e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
4378d8a09b0SGao Xiang 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
43847e4937aSGao Xiang 		DBG_BUGON(1);
4399e579fc1SGao Xiang 		return -EFSCORRUPTED;
44047e4937aSGao Xiang 	}
44147e4937aSGao Xiang 
44247e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
44347e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
44447e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
44547e4937aSGao Xiang 			DBG_BUGON(1);
4469e579fc1SGao Xiang 			return -EFSCORRUPTED;
44747e4937aSGao Xiang 		}
44847e4937aSGao Xiang 	} else {
44947e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
45047e4937aSGao Xiang 
45147e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
45247e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
45347e4937aSGao Xiang 
45447e4937aSGao Xiang 		while (llen > length &&
45547e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
45647e4937aSGao Xiang 			cpu_relax();
45747e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
45847e4937aSGao Xiang 		}
45947e4937aSGao Xiang 	}
46047e4937aSGao Xiang 	mutex_lock(&cl->lock);
46147e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
46247e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
46347e4937aSGao Xiang 		clt->tailpcl = pcl;
464473e15b0SGao Xiang 
465473e15b0SGao Xiang 	z_erofs_try_to_claim_pcluster(clt);
46647e4937aSGao Xiang 	clt->cl = cl;
4679e579fc1SGao Xiang 	return 0;
46847e4937aSGao Xiang }
46947e4937aSGao Xiang 
4709e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt,
47147e4937aSGao Xiang 				       struct inode *inode,
47247e4937aSGao Xiang 				       struct erofs_map_blocks *map)
47347e4937aSGao Xiang {
47447e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
47547e4937aSGao Xiang 	struct z_erofs_collection *cl;
47664094a04SGao Xiang 	struct erofs_workgroup *grp;
47747e4937aSGao Xiang 	int err;
47847e4937aSGao Xiang 
4799f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
4809f6cc76eSGao Xiang 	pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
4819f6cc76eSGao Xiang 	if (IS_ERR(pcl))
4829f6cc76eSGao Xiang 		return PTR_ERR(pcl);
48347e4937aSGao Xiang 
48464094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
48547e4937aSGao Xiang 	pcl->obj.index = map->m_pa >> PAGE_SHIFT;
48647e4937aSGao Xiang 
48747e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
48847e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
48947e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
49047e4937aSGao Xiang 
49147e4937aSGao Xiang 	if (map->m_flags & EROFS_MAP_ZIPPED)
49247e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
49347e4937aSGao Xiang 	else
49447e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
49547e4937aSGao Xiang 
49647e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
49747e4937aSGao Xiang 	pcl->next = clt->owned_head;
49847e4937aSGao Xiang 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
49947e4937aSGao Xiang 
50047e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
50147e4937aSGao Xiang 	cl->pageofs = map->m_la & ~PAGE_MASK;
50247e4937aSGao Xiang 
50347e4937aSGao Xiang 	/*
50447e4937aSGao Xiang 	 * lock all primary followed works before visible to others
50547e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
50647e4937aSGao Xiang 	 */
5079f6cc76eSGao Xiang 	mutex_init(&cl->lock);
50864094a04SGao Xiang 	DBG_BUGON(!mutex_trylock(&cl->lock));
50947e4937aSGao Xiang 
51064094a04SGao Xiang 	grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
51164094a04SGao Xiang 	if (IS_ERR(grp)) {
51264094a04SGao Xiang 		err = PTR_ERR(grp);
51364094a04SGao Xiang 		goto err_out;
51464094a04SGao Xiang 	}
51564094a04SGao Xiang 
51664094a04SGao Xiang 	if (grp != &pcl->obj) {
51764094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
51864094a04SGao Xiang 		err = -EEXIST;
51964094a04SGao Xiang 		goto err_out;
52047e4937aSGao Xiang 	}
52147e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
52247e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
52347e4937aSGao Xiang 		clt->tailpcl = pcl;
52447e4937aSGao Xiang 	clt->owned_head = &pcl->next;
52547e4937aSGao Xiang 	clt->pcl = pcl;
52647e4937aSGao Xiang 	clt->cl = cl;
5279e579fc1SGao Xiang 	return 0;
52864094a04SGao Xiang 
52964094a04SGao Xiang err_out:
53064094a04SGao Xiang 	mutex_unlock(&cl->lock);
5319f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
53264094a04SGao Xiang 	return err;
53347e4937aSGao Xiang }
53447e4937aSGao Xiang 
53547e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt,
53647e4937aSGao Xiang 				   struct inode *inode,
53747e4937aSGao Xiang 				   struct erofs_map_blocks *map)
53847e4937aSGao Xiang {
53964094a04SGao Xiang 	struct erofs_workgroup *grp;
5409e579fc1SGao Xiang 	int ret;
54147e4937aSGao Xiang 
54247e4937aSGao Xiang 	DBG_BUGON(clt->cl);
54347e4937aSGao Xiang 
54447e4937aSGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
54547e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
54647e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
54747e4937aSGao Xiang 
54847e4937aSGao Xiang 	if (!PAGE_ALIGNED(map->m_pa)) {
54947e4937aSGao Xiang 		DBG_BUGON(1);
55047e4937aSGao Xiang 		return -EINVAL;
55147e4937aSGao Xiang 	}
55247e4937aSGao Xiang 
55364094a04SGao Xiang 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
55464094a04SGao Xiang 	if (grp) {
55564094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
55664094a04SGao Xiang 	} else {
5579e579fc1SGao Xiang 		ret = z_erofs_register_collection(clt, inode, map);
55847e4937aSGao Xiang 
55964094a04SGao Xiang 		if (!ret)
56064094a04SGao Xiang 			goto out;
56164094a04SGao Xiang 		if (ret != -EEXIST)
5629e579fc1SGao Xiang 			return ret;
56364094a04SGao Xiang 	}
56447e4937aSGao Xiang 
56564094a04SGao Xiang 	ret = z_erofs_lookup_collection(clt, inode, map);
56664094a04SGao Xiang 	if (ret) {
56764094a04SGao Xiang 		erofs_workgroup_put(&clt->pcl->obj);
56864094a04SGao Xiang 		return ret;
56964094a04SGao Xiang 	}
57064094a04SGao Xiang 
57164094a04SGao Xiang out:
57247e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
5739e579fc1SGao Xiang 				  clt->cl->pagevec, clt->cl->vcnt);
57447e4937aSGao Xiang 
57581382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
57681382f5fSGao Xiang 	clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages;
57747e4937aSGao Xiang 	return 0;
57847e4937aSGao Xiang }
57947e4937aSGao Xiang 
58047e4937aSGao Xiang /*
58147e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
58247e4937aSGao Xiang  * only after a RCU grace period.
58347e4937aSGao Xiang  */
58447e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
58547e4937aSGao Xiang {
58647e4937aSGao Xiang 	struct z_erofs_collection *const cl =
58747e4937aSGao Xiang 		container_of(head, struct z_erofs_collection, rcu);
58847e4937aSGao Xiang 
5899f6cc76eSGao Xiang 	z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
59047e4937aSGao Xiang 					   primary_collection));
59147e4937aSGao Xiang }
59247e4937aSGao Xiang 
59347e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
59447e4937aSGao Xiang {
59547e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
59647e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
59747e4937aSGao Xiang 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
59847e4937aSGao Xiang 
59947e4937aSGao Xiang 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
60047e4937aSGao Xiang }
60147e4937aSGao Xiang 
60247e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl)
60347e4937aSGao Xiang {
60447e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
60547e4937aSGao Xiang 		container_of(cl, struct z_erofs_pcluster, primary_collection);
60647e4937aSGao Xiang 
60747e4937aSGao Xiang 	erofs_workgroup_put(&pcl->obj);
60847e4937aSGao Xiang }
60947e4937aSGao Xiang 
61047e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt)
61147e4937aSGao Xiang {
61247e4937aSGao Xiang 	struct z_erofs_collection *cl = clt->cl;
61347e4937aSGao Xiang 
61447e4937aSGao Xiang 	if (!cl)
61547e4937aSGao Xiang 		return false;
61647e4937aSGao Xiang 
61747e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
61847e4937aSGao Xiang 	mutex_unlock(&cl->lock);
61947e4937aSGao Xiang 
62047e4937aSGao Xiang 	/*
62147e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
62247e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
62347e4937aSGao Xiang 	 */
62447e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
62547e4937aSGao Xiang 		z_erofs_collection_put(cl);
62647e4937aSGao Xiang 
62747e4937aSGao Xiang 	clt->cl = NULL;
62847e4937aSGao Xiang 	return true;
62947e4937aSGao Xiang }
63047e4937aSGao Xiang 
63147e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
63247e4937aSGao Xiang 				       unsigned int cachestrategy,
63347e4937aSGao Xiang 				       erofs_off_t la)
63447e4937aSGao Xiang {
63547e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
63647e4937aSGao Xiang 		return false;
63747e4937aSGao Xiang 
63847e4937aSGao Xiang 	if (fe->backmost)
63947e4937aSGao Xiang 		return true;
64047e4937aSGao Xiang 
64147e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
64247e4937aSGao Xiang 		la < fe->headoffset;
64347e4937aSGao Xiang }
64447e4937aSGao Xiang 
64547e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
6461825c8d7SGao Xiang 				struct page *page, struct list_head *pagepool)
64747e4937aSGao Xiang {
64847e4937aSGao Xiang 	struct inode *const inode = fe->inode;
649bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
65047e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
65147e4937aSGao Xiang 	struct z_erofs_collector *const clt = &fe->clt;
65247e4937aSGao Xiang 	const loff_t offset = page_offset(page);
653dc76ea8cSGao Xiang 	bool tight = true;
65447e4937aSGao Xiang 
65547e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
65647e4937aSGao Xiang 	enum z_erofs_page_type page_type;
65747e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
65847e4937aSGao Xiang 	int err = 0;
65947e4937aSGao Xiang 
66047e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
66147e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
66247e4937aSGao Xiang 
66347e4937aSGao Xiang 	spiltted = 0;
66447e4937aSGao Xiang 	end = PAGE_SIZE;
66547e4937aSGao Xiang repeat:
66647e4937aSGao Xiang 	cur = end - 1;
66747e4937aSGao Xiang 
66847e4937aSGao Xiang 	/* lucky, within the range of the current map_blocks */
66947e4937aSGao Xiang 	if (offset + cur >= map->m_la &&
67047e4937aSGao Xiang 	    offset + cur < map->m_la + map->m_llen) {
67147e4937aSGao Xiang 		/* didn't get a valid collection previously (very rare) */
67247e4937aSGao Xiang 		if (!clt->cl)
67347e4937aSGao Xiang 			goto restart_now;
67447e4937aSGao Xiang 		goto hitted;
67547e4937aSGao Xiang 	}
67647e4937aSGao Xiang 
67747e4937aSGao Xiang 	/* go ahead the next map_blocks */
6784f761fa2SGao Xiang 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
67947e4937aSGao Xiang 
68047e4937aSGao Xiang 	if (z_erofs_collector_end(clt))
68147e4937aSGao Xiang 		fe->backmost = false;
68247e4937aSGao Xiang 
68347e4937aSGao Xiang 	map->m_la = offset + cur;
68447e4937aSGao Xiang 	map->m_llen = 0;
68547e4937aSGao Xiang 	err = z_erofs_map_blocks_iter(inode, map, 0);
6868d8a09b0SGao Xiang 	if (err)
68747e4937aSGao Xiang 		goto err_out;
68847e4937aSGao Xiang 
68947e4937aSGao Xiang restart_now:
6908d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
69147e4937aSGao Xiang 		goto hitted;
69247e4937aSGao Xiang 
69347e4937aSGao Xiang 	err = z_erofs_collector_begin(clt, inode, map);
6948d8a09b0SGao Xiang 	if (err)
69547e4937aSGao Xiang 		goto err_out;
69647e4937aSGao Xiang 
69747e4937aSGao Xiang 	/* preload all compressed pages (maybe downgrade role if necessary) */
698f57a3fe4SChao Yu 	if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
6991825c8d7SGao Xiang 		cache_strategy = TRYALLOC;
70047e4937aSGao Xiang 	else
70147e4937aSGao Xiang 		cache_strategy = DONTALLOC;
70247e4937aSGao Xiang 
7031825c8d7SGao Xiang 	preload_compressed_pages(clt, MNGD_MAPPING(sbi),
7041825c8d7SGao Xiang 				 cache_strategy, pagepool);
70547e4937aSGao Xiang 
70647e4937aSGao Xiang hitted:
707dc76ea8cSGao Xiang 	/*
708dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
709dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
710dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
711dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
712dc76ea8cSGao Xiang 	 */
713dc76ea8cSGao Xiang 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
714dc76ea8cSGao Xiang 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
715dc76ea8cSGao Xiang 
71647e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
7178d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
71847e4937aSGao Xiang 		zero_user_segment(page, cur, end);
71947e4937aSGao Xiang 		goto next_part;
72047e4937aSGao Xiang 	}
72147e4937aSGao Xiang 
72247e4937aSGao Xiang 	/* let's derive page type */
72347e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
72447e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
72547e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
72647e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
72747e4937aSGao Xiang 
72847e4937aSGao Xiang 	if (cur)
72947e4937aSGao Xiang 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
73047e4937aSGao Xiang 
73147e4937aSGao Xiang retry:
73247e4937aSGao Xiang 	err = z_erofs_attach_page(clt, page, page_type);
7336aaa7b06SGao Xiang 	/* should allocate an additional short-lived page for pagevec */
73447e4937aSGao Xiang 	if (err == -EAGAIN) {
73547e4937aSGao Xiang 		struct page *const newpage =
736e3f78d5eSChao Yu 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
73747e4937aSGao Xiang 
7386aaa7b06SGao Xiang 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
73947e4937aSGao Xiang 		err = z_erofs_attach_page(clt, newpage,
74047e4937aSGao Xiang 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
7418d8a09b0SGao Xiang 		if (!err)
74247e4937aSGao Xiang 			goto retry;
74347e4937aSGao Xiang 	}
74447e4937aSGao Xiang 
7458d8a09b0SGao Xiang 	if (err)
74647e4937aSGao Xiang 		goto err_out;
74747e4937aSGao Xiang 
74847e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
74947e4937aSGao Xiang 
75047e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
75147e4937aSGao Xiang 
75247e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
75347e4937aSGao Xiang 	++spiltted;
75447e4937aSGao Xiang 	/* also update nr_pages */
75547e4937aSGao Xiang 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
75647e4937aSGao Xiang next_part:
75747e4937aSGao Xiang 	/* can be used for verification */
75847e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
75947e4937aSGao Xiang 
76047e4937aSGao Xiang 	end = cur;
76147e4937aSGao Xiang 	if (end > 0)
76247e4937aSGao Xiang 		goto repeat;
76347e4937aSGao Xiang 
76447e4937aSGao Xiang out:
76547e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
76647e4937aSGao Xiang 
7674f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
76847e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
76947e4937aSGao Xiang 	return err;
77047e4937aSGao Xiang 
77147e4937aSGao Xiang 	/* if some error occurred while processing this page */
77247e4937aSGao Xiang err_out:
77347e4937aSGao Xiang 	SetPageError(page);
77447e4937aSGao Xiang 	goto out;
77547e4937aSGao Xiang }
77647e4937aSGao Xiang 
777648f2de0SHuang Jianan static void z_erofs_decompressqueue_work(struct work_struct *work);
778a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
779a4b1fab1SGao Xiang 				       bool sync, int bios)
78047e4937aSGao Xiang {
78130048cdaSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
78230048cdaSHuang Jianan 
783a4b1fab1SGao Xiang 	/* wake up the caller thread for sync decompression */
784a4b1fab1SGao Xiang 	if (sync) {
78547e4937aSGao Xiang 		unsigned long flags;
78647e4937aSGao Xiang 
78747e4937aSGao Xiang 		spin_lock_irqsave(&io->u.wait.lock, flags);
78847e4937aSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
78947e4937aSGao Xiang 			wake_up_locked(&io->u.wait);
79047e4937aSGao Xiang 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
79147e4937aSGao Xiang 		return;
79247e4937aSGao Xiang 	}
79347e4937aSGao Xiang 
794648f2de0SHuang Jianan 	if (atomic_add_return(bios, &io->pending_bios))
795648f2de0SHuang Jianan 		return;
79630048cdaSHuang Jianan 	/* Use workqueue and sync decompression for atomic contexts only */
797648f2de0SHuang Jianan 	if (in_atomic() || irqs_disabled()) {
79847e4937aSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
79930048cdaSHuang Jianan 		sbi->ctx.readahead_sync_decompress = true;
800648f2de0SHuang Jianan 		return;
801648f2de0SHuang Jianan 	}
802648f2de0SHuang Jianan 	z_erofs_decompressqueue_work(&io->u.work);
80347e4937aSGao Xiang }
80447e4937aSGao Xiang 
8056aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
8066aaa7b06SGao Xiang {
8076aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
8086aaa7b06SGao Xiang }
8096aaa7b06SGao Xiang 
8100c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
81147e4937aSGao Xiang {
812a4b1fab1SGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
813a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
81447e4937aSGao Xiang 	blk_status_t err = bio->bi_status;
81547e4937aSGao Xiang 	struct bio_vec *bvec;
81647e4937aSGao Xiang 	struct bvec_iter_all iter_all;
81747e4937aSGao Xiang 
81847e4937aSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
81947e4937aSGao Xiang 		struct page *page = bvec->bv_page;
82047e4937aSGao Xiang 
82147e4937aSGao Xiang 		DBG_BUGON(PageUptodate(page));
8226aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
82347e4937aSGao Xiang 
8248d8a09b0SGao Xiang 		if (err)
82547e4937aSGao Xiang 			SetPageError(page);
82647e4937aSGao Xiang 
827a4b1fab1SGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
828a4b1fab1SGao Xiang 			if (!err)
829a4b1fab1SGao Xiang 				SetPageUptodate(page);
83047e4937aSGao Xiang 			unlock_page(page);
83147e4937aSGao Xiang 		}
832a4b1fab1SGao Xiang 	}
833a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
83447e4937aSGao Xiang 	bio_put(bio);
83547e4937aSGao Xiang }
83647e4937aSGao Xiang 
83747e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
83847e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
83947e4937aSGao Xiang 				       struct list_head *pagepool)
84047e4937aSGao Xiang {
84147e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
84247e4937aSGao Xiang 	struct z_erofs_pagevec_ctor ctor;
8439f6cc76eSGao Xiang 	unsigned int i, inputsize, outputsize, llen, nr_pages;
84447e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
84547e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
84647e4937aSGao Xiang 
84747e4937aSGao Xiang 	enum z_erofs_page_type page_type;
84847e4937aSGao Xiang 	bool overlapped, partial;
84947e4937aSGao Xiang 	struct z_erofs_collection *cl;
85047e4937aSGao Xiang 	int err;
85147e4937aSGao Xiang 
85247e4937aSGao Xiang 	might_sleep();
85347e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
85447e4937aSGao Xiang 	DBG_BUGON(!READ_ONCE(cl->nr_pages));
85547e4937aSGao Xiang 
85647e4937aSGao Xiang 	mutex_lock(&cl->lock);
85747e4937aSGao Xiang 	nr_pages = cl->nr_pages;
85847e4937aSGao Xiang 
8598d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
86047e4937aSGao Xiang 		pages = pages_onstack;
86147e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
86247e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
86347e4937aSGao Xiang 		pages = z_pagemap_global;
86447e4937aSGao Xiang 	} else {
86547e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
86647e4937aSGao Xiang 
86747e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
86847e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
86947e4937aSGao Xiang 
87047e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
87147e4937aSGao Xiang 				       gfp_flags);
87247e4937aSGao Xiang 
87347e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
8748d8a09b0SGao Xiang 		if (!pages) {
87547e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
87647e4937aSGao Xiang 			pages = z_pagemap_global;
87747e4937aSGao Xiang 		}
87847e4937aSGao Xiang 	}
87947e4937aSGao Xiang 
88047e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
88147e4937aSGao Xiang 		pages[i] = NULL;
88247e4937aSGao Xiang 
88347e4937aSGao Xiang 	err = 0;
88447e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
88547e4937aSGao Xiang 				  cl->pagevec, 0);
88647e4937aSGao Xiang 
88747e4937aSGao Xiang 	for (i = 0; i < cl->vcnt; ++i) {
88847e4937aSGao Xiang 		unsigned int pagenr;
88947e4937aSGao Xiang 
89047e4937aSGao Xiang 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
89147e4937aSGao Xiang 
89247e4937aSGao Xiang 		/* all pages in pagevec ought to be valid */
89347e4937aSGao Xiang 		DBG_BUGON(!page);
8946aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
89547e4937aSGao Xiang 
8966aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
89747e4937aSGao Xiang 			continue;
89847e4937aSGao Xiang 
89947e4937aSGao Xiang 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
90047e4937aSGao Xiang 			pagenr = 0;
90147e4937aSGao Xiang 		else
90247e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
90347e4937aSGao Xiang 
90447e4937aSGao Xiang 		DBG_BUGON(pagenr >= nr_pages);
90547e4937aSGao Xiang 
90647e4937aSGao Xiang 		/*
90747e4937aSGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
90847e4937aSGao Xiang 		 * so here erroring out one multiref page.
90947e4937aSGao Xiang 		 */
9108d8a09b0SGao Xiang 		if (pages[pagenr]) {
91147e4937aSGao Xiang 			DBG_BUGON(1);
91247e4937aSGao Xiang 			SetPageError(pages[pagenr]);
91347e4937aSGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
91447e4937aSGao Xiang 			err = -EFSCORRUPTED;
91547e4937aSGao Xiang 		}
91647e4937aSGao Xiang 		pages[pagenr] = page;
91747e4937aSGao Xiang 	}
91847e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&ctor, true);
91947e4937aSGao Xiang 
92047e4937aSGao Xiang 	overlapped = false;
92147e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
92247e4937aSGao Xiang 
9239f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
92447e4937aSGao Xiang 		unsigned int pagenr;
92547e4937aSGao Xiang 
92647e4937aSGao Xiang 		page = compressed_pages[i];
92747e4937aSGao Xiang 
92847e4937aSGao Xiang 		/* all compressed pages ought to be valid */
92947e4937aSGao Xiang 		DBG_BUGON(!page);
9306aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
93147e4937aSGao Xiang 
9326aaa7b06SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
93347e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
9348d8a09b0SGao Xiang 				if (!PageUptodate(page))
93547e4937aSGao Xiang 					err = -EIO;
93647e4937aSGao Xiang 				continue;
93747e4937aSGao Xiang 			}
93847e4937aSGao Xiang 
93947e4937aSGao Xiang 			/*
94047e4937aSGao Xiang 			 * only if non-head page can be selected
94147e4937aSGao Xiang 			 * for inplace decompression
94247e4937aSGao Xiang 			 */
94347e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
94447e4937aSGao Xiang 
94547e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
9468d8a09b0SGao Xiang 			if (pages[pagenr]) {
94747e4937aSGao Xiang 				DBG_BUGON(1);
94847e4937aSGao Xiang 				SetPageError(pages[pagenr]);
94947e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
95047e4937aSGao Xiang 				err = -EFSCORRUPTED;
95147e4937aSGao Xiang 			}
95247e4937aSGao Xiang 			pages[pagenr] = page;
95347e4937aSGao Xiang 
95447e4937aSGao Xiang 			overlapped = true;
95547e4937aSGao Xiang 		}
95647e4937aSGao Xiang 
9576aaa7b06SGao Xiang 		/* PG_error needs checking for all non-managed pages */
9588d8a09b0SGao Xiang 		if (PageError(page)) {
95947e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
96047e4937aSGao Xiang 			err = -EIO;
96147e4937aSGao Xiang 		}
96247e4937aSGao Xiang 	}
96347e4937aSGao Xiang 
9648d8a09b0SGao Xiang 	if (err)
96547e4937aSGao Xiang 		goto out;
96647e4937aSGao Xiang 
96747e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
96847e4937aSGao Xiang 	if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
96947e4937aSGao Xiang 		outputsize = llen;
97047e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
97147e4937aSGao Xiang 	} else {
97247e4937aSGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
97347e4937aSGao Xiang 		partial = true;
97447e4937aSGao Xiang 	}
97547e4937aSGao Xiang 
9769f6cc76eSGao Xiang 	inputsize = pcl->pclusterpages * PAGE_SIZE;
97747e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
97847e4937aSGao Xiang 					.sb = sb,
97947e4937aSGao Xiang 					.in = compressed_pages,
98047e4937aSGao Xiang 					.out = pages,
98147e4937aSGao Xiang 					.pageofs_out = cl->pageofs,
9829f6cc76eSGao Xiang 					.inputsize = inputsize,
98347e4937aSGao Xiang 					.outputsize = outputsize,
98447e4937aSGao Xiang 					.alg = pcl->algorithmformat,
98547e4937aSGao Xiang 					.inplace_io = overlapped,
98647e4937aSGao Xiang 					.partial_decoding = partial
98747e4937aSGao Xiang 				 }, pagepool);
98847e4937aSGao Xiang 
98947e4937aSGao Xiang out:
990fe6adcceSRuiqi Gong 	/* must handle all compressed pages before ending pages */
9919f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
99247e4937aSGao Xiang 		page = compressed_pages[i];
99347e4937aSGao Xiang 
99447e4937aSGao Xiang 		if (erofs_page_is_managed(sbi, page))
99547e4937aSGao Xiang 			continue;
99647e4937aSGao Xiang 
9976aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
9986aaa7b06SGao Xiang 		(void)z_erofs_put_shortlivedpage(pagepool, page);
99947e4937aSGao Xiang 
100047e4937aSGao Xiang 		WRITE_ONCE(compressed_pages[i], NULL);
100147e4937aSGao Xiang 	}
100247e4937aSGao Xiang 
100347e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
100447e4937aSGao Xiang 		page = pages[i];
100547e4937aSGao Xiang 		if (!page)
100647e4937aSGao Xiang 			continue;
100747e4937aSGao Xiang 
10086aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
100947e4937aSGao Xiang 
10106aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
10116aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
101247e4937aSGao Xiang 			continue;
101347e4937aSGao Xiang 
10148d8a09b0SGao Xiang 		if (err < 0)
101547e4937aSGao Xiang 			SetPageError(page);
101647e4937aSGao Xiang 
101747e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
101847e4937aSGao Xiang 	}
101947e4937aSGao Xiang 
102047e4937aSGao Xiang 	if (pages == z_pagemap_global)
102147e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
10228d8a09b0SGao Xiang 	else if (pages != pages_onstack)
102347e4937aSGao Xiang 		kvfree(pages);
102447e4937aSGao Xiang 
102547e4937aSGao Xiang 	cl->nr_pages = 0;
102647e4937aSGao Xiang 	cl->vcnt = 0;
102747e4937aSGao Xiang 
102847e4937aSGao Xiang 	/* all cl locks MUST be taken before the following line */
102947e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
103047e4937aSGao Xiang 
103147e4937aSGao Xiang 	/* all cl locks SHOULD be released right now */
103247e4937aSGao Xiang 	mutex_unlock(&cl->lock);
103347e4937aSGao Xiang 
103447e4937aSGao Xiang 	z_erofs_collection_put(cl);
103547e4937aSGao Xiang 	return err;
103647e4937aSGao Xiang }
103747e4937aSGao Xiang 
10380c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
103947e4937aSGao Xiang 				     struct list_head *pagepool)
104047e4937aSGao Xiang {
104147e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
104247e4937aSGao Xiang 
104347e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
104447e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
104547e4937aSGao Xiang 
104647e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
104747e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
104847e4937aSGao Xiang 
104947e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
105047e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
105147e4937aSGao Xiang 
105247e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
105347e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
105447e4937aSGao Xiang 
1055a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
105647e4937aSGao Xiang 	}
105747e4937aSGao Xiang }
105847e4937aSGao Xiang 
10590c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
106047e4937aSGao Xiang {
1061a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1062a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
106347e4937aSGao Xiang 	LIST_HEAD(pagepool);
106447e4937aSGao Xiang 
1065a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
10660c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
106747e4937aSGao Xiang 
106847e4937aSGao Xiang 	put_pages_list(&pagepool);
1069a4b1fab1SGao Xiang 	kvfree(bgq);
107047e4937aSGao Xiang }
107147e4937aSGao Xiang 
107247e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
107347e4937aSGao Xiang 					       unsigned int nr,
107447e4937aSGao Xiang 					       struct list_head *pagepool,
107547e4937aSGao Xiang 					       struct address_space *mc,
107647e4937aSGao Xiang 					       gfp_t gfp)
107747e4937aSGao Xiang {
107847e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
107947e4937aSGao Xiang 	bool tocache = false;
108047e4937aSGao Xiang 
108147e4937aSGao Xiang 	struct address_space *mapping;
108247e4937aSGao Xiang 	struct page *oldpage, *page;
108347e4937aSGao Xiang 
108447e4937aSGao Xiang 	compressed_page_t t;
108547e4937aSGao Xiang 	int justfound;
108647e4937aSGao Xiang 
108747e4937aSGao Xiang repeat:
108847e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
108947e4937aSGao Xiang 	oldpage = page;
109047e4937aSGao Xiang 
109147e4937aSGao Xiang 	if (!page)
109247e4937aSGao Xiang 		goto out_allocpage;
109347e4937aSGao Xiang 
109447e4937aSGao Xiang 	/*
109547e4937aSGao Xiang 	 * the cached page has not been allocated and
109647e4937aSGao Xiang 	 * an placeholder is out there, prepare it now.
109747e4937aSGao Xiang 	 */
1098bda17a45SGao Xiang 	if (page == PAGE_UNALLOCATED) {
109947e4937aSGao Xiang 		tocache = true;
110047e4937aSGao Xiang 		goto out_allocpage;
110147e4937aSGao Xiang 	}
110247e4937aSGao Xiang 
110347e4937aSGao Xiang 	/* process the target tagged pointer */
110447e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
110547e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
110647e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
110747e4937aSGao Xiang 
11081825c8d7SGao Xiang 	/*
11091825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
11101825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
11111825c8d7SGao Xiang 	 */
11121825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
11131825c8d7SGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
11141825c8d7SGao Xiang 		set_page_private(page, 0);
11151825c8d7SGao Xiang 		tocache = true;
11161825c8d7SGao Xiang 		goto out_tocache;
11171825c8d7SGao Xiang 	}
111847e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
111947e4937aSGao Xiang 
112047e4937aSGao Xiang 	/*
11216aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
112247e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
112347e4937aSGao Xiang 	 */
112447e4937aSGao Xiang 	if (mapping && mapping != mc)
112547e4937aSGao Xiang 		/* ought to be unmanaged pages */
112647e4937aSGao Xiang 		goto out;
112747e4937aSGao Xiang 
11286aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
11296aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
11306aaa7b06SGao Xiang 		goto out;
11316aaa7b06SGao Xiang 
113247e4937aSGao Xiang 	lock_page(page);
113347e4937aSGao Xiang 
113447e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
113547e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
113647e4937aSGao Xiang 
113747e4937aSGao Xiang 	/* the page is still in manage cache */
113847e4937aSGao Xiang 	if (page->mapping == mc) {
113947e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
114047e4937aSGao Xiang 
114147e4937aSGao Xiang 		ClearPageError(page);
114247e4937aSGao Xiang 		if (!PagePrivate(page)) {
114347e4937aSGao Xiang 			/*
114447e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
114547e4937aSGao Xiang 			 * the current restriction as well if
114647e4937aSGao Xiang 			 * the page is already in compressed_pages[].
114747e4937aSGao Xiang 			 */
114847e4937aSGao Xiang 			DBG_BUGON(!justfound);
114947e4937aSGao Xiang 
115047e4937aSGao Xiang 			justfound = 0;
115147e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
115247e4937aSGao Xiang 			SetPagePrivate(page);
115347e4937aSGao Xiang 		}
115447e4937aSGao Xiang 
115547e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
115647e4937aSGao Xiang 		if (PageUptodate(page)) {
115747e4937aSGao Xiang 			unlock_page(page);
115847e4937aSGao Xiang 			page = NULL;
115947e4937aSGao Xiang 		}
116047e4937aSGao Xiang 		goto out;
116147e4937aSGao Xiang 	}
116247e4937aSGao Xiang 
116347e4937aSGao Xiang 	/*
116447e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
116547e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
116647e4937aSGao Xiang 	 */
116747e4937aSGao Xiang 	DBG_BUGON(page->mapping);
116847e4937aSGao Xiang 	DBG_BUGON(!justfound);
116947e4937aSGao Xiang 
117047e4937aSGao Xiang 	tocache = true;
117147e4937aSGao Xiang 	unlock_page(page);
117247e4937aSGao Xiang 	put_page(page);
117347e4937aSGao Xiang out_allocpage:
11745ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
11755ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
11765ddcee1fSGao Xiang 		list_add(&page->lru, pagepool);
11775ddcee1fSGao Xiang 		cond_resched();
11785ddcee1fSGao Xiang 		goto repeat;
11795ddcee1fSGao Xiang 	}
11801825c8d7SGao Xiang out_tocache:
1181bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1182bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1183bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1184bf225074SGao Xiang 		goto out;
1185a30573b3SGao Xiang 	}
1186bf225074SGao Xiang 	attach_page_private(page, pcl);
1187bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1188bf225074SGao Xiang 	put_page(page);
1189bf225074SGao Xiang 
119047e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
119147e4937aSGao Xiang 	return page;
119247e4937aSGao Xiang }
119347e4937aSGao Xiang 
1194a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1195a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1196a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
119747e4937aSGao Xiang {
1198a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
119947e4937aSGao Xiang 
1200a4b1fab1SGao Xiang 	if (fg && !*fg) {
1201a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1202a4b1fab1SGao Xiang 		if (!q) {
1203a4b1fab1SGao Xiang 			*fg = true;
1204a4b1fab1SGao Xiang 			goto fg_out;
120547e4937aSGao Xiang 		}
12060c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1207a4b1fab1SGao Xiang 	} else {
1208a4b1fab1SGao Xiang fg_out:
1209a4b1fab1SGao Xiang 		q = fgq;
1210a4b1fab1SGao Xiang 		init_waitqueue_head(&fgq->u.wait);
1211a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1212a4b1fab1SGao Xiang 	}
1213a4b1fab1SGao Xiang 	q->sb = sb;
1214a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1215a4b1fab1SGao Xiang 	return q;
121647e4937aSGao Xiang }
121747e4937aSGao Xiang 
121847e4937aSGao Xiang /* define decompression jobqueue types */
121947e4937aSGao Xiang enum {
122047e4937aSGao Xiang 	JQ_BYPASS,
122147e4937aSGao Xiang 	JQ_SUBMIT,
122247e4937aSGao Xiang 	NR_JOBQUEUES,
122347e4937aSGao Xiang };
122447e4937aSGao Xiang 
122547e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1226a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1227a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
122847e4937aSGao Xiang {
122947e4937aSGao Xiang 	/*
123047e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
123147e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
123247e4937aSGao Xiang 	 */
1233a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1234a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
123547e4937aSGao Xiang 
1236a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
123747e4937aSGao Xiang }
123847e4937aSGao Xiang 
123947e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
124047e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
124147e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
124247e4937aSGao Xiang {
124347e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
124447e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
124547e4937aSGao Xiang 
124647e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
124747e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
124847e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
124947e4937aSGao Xiang 
125047e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
125147e4937aSGao Xiang 
125247e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
125347e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
125447e4937aSGao Xiang 
125547e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
125647e4937aSGao Xiang }
125747e4937aSGao Xiang 
12581e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb,
12596ea5aad3SGao Xiang 				 struct z_erofs_decompress_frontend *f,
126047e4937aSGao Xiang 				 struct list_head *pagepool,
1261a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1262a4b1fab1SGao Xiang 				 bool *force_fg)
126347e4937aSGao Xiang {
1264bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
126547e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1266a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
126747e4937aSGao Xiang 	void *bi_private;
12686ea5aad3SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
126947e4937aSGao Xiang 	/* since bio will be NULL, no need to initialize last_index */
12703f649ab7SKees Cook 	pgoff_t last_index;
12711e4a2955SGao Xiang 	unsigned int nr_bios = 0;
12721e4a2955SGao Xiang 	struct bio *bio = NULL;
127347e4937aSGao Xiang 
1274a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1275a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1276a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
127747e4937aSGao Xiang 
127847e4937aSGao Xiang 	/* by default, all need io submission */
127947e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
128047e4937aSGao Xiang 
128147e4937aSGao Xiang 	do {
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 
12931e4a2955SGao Xiang 		cur = pcl->obj.index;
12949f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
129547e4937aSGao Xiang 
129647e4937aSGao Xiang 		/* close the main owned chain at first */
129747e4937aSGao Xiang 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
129847e4937aSGao Xiang 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
129947e4937aSGao Xiang 
13001e4a2955SGao Xiang 		do {
13011e4a2955SGao Xiang 			struct page *page;
130247e4937aSGao Xiang 
13031e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
130447e4937aSGao Xiang 							  MNGD_MAPPING(sbi),
130547e4937aSGao Xiang 							  GFP_NOFS);
13061e4a2955SGao Xiang 			if (!page)
13071e4a2955SGao Xiang 				continue;
130847e4937aSGao Xiang 
13091e4a2955SGao Xiang 			if (bio && cur != last_index + 1) {
131047e4937aSGao Xiang submit_bio_retry:
131194e4e153SGao Xiang 				submit_bio(bio);
131247e4937aSGao Xiang 				bio = NULL;
131347e4937aSGao Xiang 			}
131447e4937aSGao Xiang 
131547e4937aSGao Xiang 			if (!bio) {
1316a8affc03SChristoph Hellwig 				bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS);
1317a5c0b780SGao Xiang 
13180c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1319a5c0b780SGao Xiang 				bio_set_dev(bio, sb->s_bdev);
13201e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1321a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1322a5c0b780SGao Xiang 				bio->bi_private = bi_private;
132394e4e153SGao Xiang 				bio->bi_opf = REQ_OP_READ;
13246ea5aad3SGao Xiang 				if (f->readahead)
13256ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
132647e4937aSGao Xiang 				++nr_bios;
132747e4937aSGao Xiang 			}
132847e4937aSGao Xiang 
13296c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
133047e4937aSGao Xiang 				goto submit_bio_retry;
133147e4937aSGao Xiang 
13321e4a2955SGao Xiang 			last_index = cur;
13331e4a2955SGao Xiang 			bypass = false;
13341e4a2955SGao Xiang 		} while (++cur < end);
133547e4937aSGao Xiang 
13361e4a2955SGao Xiang 		if (!bypass)
133747e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
133847e4937aSGao Xiang 		else
133947e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
134047e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
134147e4937aSGao Xiang 
134247e4937aSGao Xiang 	if (bio)
134394e4e153SGao Xiang 		submit_bio(bio);
134447e4937aSGao Xiang 
1345587a67b7SGao Xiang 	/*
1346587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1347587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1348587a67b7SGao Xiang 	 */
1349587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1350587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
13511e4a2955SGao Xiang 		return;
1352587a67b7SGao Xiang 	}
1353a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
135447e4937aSGao Xiang }
135547e4937aSGao Xiang 
13560c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb,
13576ea5aad3SGao Xiang 			     struct z_erofs_decompress_frontend *f,
13580c638f70SGao Xiang 			     struct list_head *pagepool, bool force_fg)
135947e4937aSGao Xiang {
1360a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
136147e4937aSGao Xiang 
13626ea5aad3SGao Xiang 	if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
136347e4937aSGao Xiang 		return;
13646ea5aad3SGao Xiang 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
136547e4937aSGao Xiang 
13660c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
13670c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
136847e4937aSGao Xiang 
136947e4937aSGao Xiang 	if (!force_fg)
137047e4937aSGao Xiang 		return;
137147e4937aSGao Xiang 
137247e4937aSGao Xiang 	/* wait until all bios are completed */
1373a93f8c36SGao Xiang 	io_wait_event(io[JQ_SUBMIT].u.wait,
137447e4937aSGao Xiang 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
137547e4937aSGao Xiang 
13760c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
13770c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
137847e4937aSGao Xiang }
137947e4937aSGao Xiang 
13800c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page)
138147e4937aSGao Xiang {
138247e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
138347e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
138447e4937aSGao Xiang 	int err;
138547e4937aSGao Xiang 	LIST_HEAD(pagepool);
138647e4937aSGao Xiang 
138747e4937aSGao Xiang 	trace_erofs_readpage(page, false);
138847e4937aSGao Xiang 
138947e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
139047e4937aSGao Xiang 
13911825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
139247e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
139347e4937aSGao Xiang 
139447e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
13956ea5aad3SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
139647e4937aSGao Xiang 
139747e4937aSGao Xiang 	if (err)
13984f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
139947e4937aSGao Xiang 
140047e4937aSGao Xiang 	if (f.map.mpage)
140147e4937aSGao Xiang 		put_page(f.map.mpage);
140247e4937aSGao Xiang 
140347e4937aSGao Xiang 	/* clean up the remaining free pages */
140447e4937aSGao Xiang 	put_pages_list(&pagepool);
140547e4937aSGao Xiang 	return err;
140647e4937aSGao Xiang }
140747e4937aSGao Xiang 
14080615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
140947e4937aSGao Xiang {
14100615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
141147e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
141247e4937aSGao Xiang 
1413bf9a123bSGao Xiang 	unsigned int nr_pages = readahead_count(rac);
141430048cdaSHuang Jianan 	bool sync = (sbi->ctx.readahead_sync_decompress &&
141530048cdaSHuang Jianan 			nr_pages <= sbi->ctx.max_sync_decompress_pages);
141647e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
14170615090cSMatthew Wilcox (Oracle) 	struct page *page, *head = NULL;
141847e4937aSGao Xiang 	LIST_HEAD(pagepool);
141947e4937aSGao Xiang 
1420bf9a123bSGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
142147e4937aSGao Xiang 
14226ea5aad3SGao Xiang 	f.readahead = true;
14230615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
142447e4937aSGao Xiang 
14250615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
142647e4937aSGao Xiang 		prefetchw(&page->flags);
142747e4937aSGao Xiang 
142847e4937aSGao Xiang 		/*
142947e4937aSGao Xiang 		 * A pure asynchronous readahead is indicated if
143047e4937aSGao Xiang 		 * a PG_readahead marked page is hitted at first.
143147e4937aSGao Xiang 		 * Let's also do asynchronous decompression for this case.
143247e4937aSGao Xiang 		 */
143347e4937aSGao Xiang 		sync &= !(PageReadahead(page) && !head);
143447e4937aSGao Xiang 
143547e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
143647e4937aSGao Xiang 		head = page;
143747e4937aSGao Xiang 	}
143847e4937aSGao Xiang 
143947e4937aSGao Xiang 	while (head) {
144047e4937aSGao Xiang 		struct page *page = head;
144147e4937aSGao Xiang 		int err;
144247e4937aSGao Xiang 
144347e4937aSGao Xiang 		/* traversal in reverse order */
144447e4937aSGao Xiang 		head = (void *)page_private(page);
144547e4937aSGao Xiang 
14461825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1447a5876e24SGao Xiang 		if (err)
14484f761fa2SGao Xiang 			erofs_err(inode->i_sb,
14494f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
14504f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
145147e4937aSGao Xiang 		put_page(page);
145247e4937aSGao Xiang 	}
145347e4937aSGao Xiang 
145447e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
145547e4937aSGao Xiang 
14566ea5aad3SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
145747e4937aSGao Xiang 
145847e4937aSGao Xiang 	if (f.map.mpage)
145947e4937aSGao Xiang 		put_page(f.map.mpage);
146047e4937aSGao Xiang 
146147e4937aSGao Xiang 	/* clean up the remaining free pages */
146247e4937aSGao Xiang 	put_pages_list(&pagepool);
146347e4937aSGao Xiang }
146447e4937aSGao Xiang 
14650c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
14660c638f70SGao Xiang 	.readpage = z_erofs_readpage,
14670615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
146847e4937aSGao Xiang };
1469