xref: /openbmc/linux/fs/erofs/zdata.c (revision 1825c8d7)
147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only
247e4937aSGao Xiang /*
347e4937aSGao Xiang  * Copyright (C) 2018 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov  *             https://www.huawei.com/
547e4937aSGao Xiang  * Created by Gao Xiang <gaoxiang25@huawei.com>
647e4937aSGao Xiang  */
747e4937aSGao Xiang #include "zdata.h"
847e4937aSGao Xiang #include "compress.h"
947e4937aSGao Xiang #include <linux/prefetch.h>
1047e4937aSGao Xiang 
1147e4937aSGao Xiang #include <trace/events/erofs.h>
1247e4937aSGao Xiang 
1347e4937aSGao Xiang /*
1447e4937aSGao Xiang  * a compressed_pages[] placeholder in order to avoid
1547e4937aSGao Xiang  * being filled with file pages for in-place decompression.
1647e4937aSGao Xiang  */
1747e4937aSGao Xiang #define PAGE_UNALLOCATED     ((void *)0x5F0E4B1D)
1847e4937aSGao Xiang 
1947e4937aSGao Xiang /* how to allocate cached pages for a pcluster */
2047e4937aSGao Xiang enum z_erofs_cache_alloctype {
2147e4937aSGao Xiang 	DONTALLOC,	/* don't allocate any cached pages */
2247e4937aSGao Xiang 	DELAYEDALLOC,	/* delayed allocation (at the time of submitting io) */
23*1825c8d7SGao Xiang 	/*
24*1825c8d7SGao Xiang 	 * try to use cached I/O if page allocation succeeds or fallback
25*1825c8d7SGao Xiang 	 * to in-place I/O instead to avoid any direct reclaim.
26*1825c8d7SGao Xiang 	 */
27*1825c8d7SGao Xiang 	TRYALLOC,
2847e4937aSGao Xiang };
2947e4937aSGao Xiang 
3047e4937aSGao Xiang /*
3147e4937aSGao Xiang  * tagged pointer with 1-bit tag for all compressed pages
3247e4937aSGao Xiang  * tag 0 - the page is just found with an extra page reference
3347e4937aSGao Xiang  */
3447e4937aSGao Xiang typedef tagptr1_t compressed_page_t;
3547e4937aSGao Xiang 
3647e4937aSGao Xiang #define tag_compressed_page_justfound(page) \
3747e4937aSGao Xiang 	tagptr_fold(compressed_page_t, page, 1)
3847e4937aSGao Xiang 
3947e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
4047e4937aSGao Xiang static struct kmem_cache *pcluster_cachep __read_mostly;
4147e4937aSGao Xiang 
4247e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
4347e4937aSGao Xiang {
4447e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
4547e4937aSGao Xiang 	kmem_cache_destroy(pcluster_cachep);
4647e4937aSGao Xiang }
4747e4937aSGao Xiang 
4899634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
4947e4937aSGao Xiang {
5047e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
5147e4937aSGao Xiang 
5247e4937aSGao Xiang 	/*
5347e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
5447e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
5547e4937aSGao Xiang 	 */
560e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
570e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
5847e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
5947e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
6047e4937aSGao Xiang }
6147e4937aSGao Xiang 
6299634bf3SGao Xiang static void z_erofs_pcluster_init_once(void *ptr)
6347e4937aSGao Xiang {
6447e4937aSGao Xiang 	struct z_erofs_pcluster *pcl = ptr;
6547e4937aSGao Xiang 	struct z_erofs_collection *cl = z_erofs_primarycollection(pcl);
6647e4937aSGao Xiang 	unsigned int i;
6747e4937aSGao Xiang 
6847e4937aSGao Xiang 	mutex_init(&cl->lock);
6947e4937aSGao Xiang 	cl->nr_pages = 0;
7047e4937aSGao Xiang 	cl->vcnt = 0;
7147e4937aSGao Xiang 	for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i)
7247e4937aSGao Xiang 		pcl->compressed_pages[i] = NULL;
7347e4937aSGao Xiang }
7447e4937aSGao Xiang 
7547e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
7647e4937aSGao Xiang {
7747e4937aSGao Xiang 	pcluster_cachep = kmem_cache_create("erofs_compress",
7847e4937aSGao Xiang 					    Z_EROFS_WORKGROUP_SIZE, 0,
7999634bf3SGao Xiang 					    SLAB_RECLAIM_ACCOUNT,
8099634bf3SGao Xiang 					    z_erofs_pcluster_init_once);
8147e4937aSGao Xiang 	if (pcluster_cachep) {
8299634bf3SGao Xiang 		if (!z_erofs_init_workqueue())
8347e4937aSGao Xiang 			return 0;
8447e4937aSGao Xiang 
8547e4937aSGao Xiang 		kmem_cache_destroy(pcluster_cachep);
8647e4937aSGao Xiang 	}
8747e4937aSGao Xiang 	return -ENOMEM;
8847e4937aSGao Xiang }
8947e4937aSGao Xiang 
9047e4937aSGao Xiang enum z_erofs_collectmode {
9147e4937aSGao Xiang 	COLLECT_SECONDARY,
9247e4937aSGao Xiang 	COLLECT_PRIMARY,
9347e4937aSGao Xiang 	/*
9447e4937aSGao Xiang 	 * The current collection was the tail of an exist chain, in addition
9547e4937aSGao Xiang 	 * that the previous processed chained collections are all decided to
9647e4937aSGao Xiang 	 * be hooked up to it.
9747e4937aSGao Xiang 	 * A new chain will be created for the remaining collections which are
9847e4937aSGao Xiang 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
9947e4937aSGao Xiang 	 * the next collection cannot reuse the whole page safely in
10047e4937aSGao Xiang 	 * the following scenario:
10147e4937aSGao Xiang 	 *  ________________________________________________________________
10247e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
10347e4937aSGao Xiang 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
10447e4937aSGao Xiang 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
10547e4937aSGao Xiang 	 */
10647e4937aSGao Xiang 	COLLECT_PRIMARY_HOOKED,
10747e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
10847e4937aSGao Xiang 	/*
10947e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
11047e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
11147e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
11247e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
11347e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
11447e4937aSGao Xiang 	 * illustrated below:
11547e4937aSGao Xiang 	 *  ________________________________________________________________
11647e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
11747e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
11847e4937aSGao Xiang 	 * |  PRIMARY_FOLLOWED or |                                        |
11947e4937aSGao Xiang 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
12047e4937aSGao Xiang 	 *
12147e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
12247e4937aSGao Xiang 	 */
12347e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED,
12447e4937aSGao Xiang };
12547e4937aSGao Xiang 
12647e4937aSGao Xiang struct z_erofs_collector {
12747e4937aSGao Xiang 	struct z_erofs_pagevec_ctor vector;
12847e4937aSGao Xiang 
12947e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
13047e4937aSGao Xiang 	struct z_erofs_collection *cl;
13147e4937aSGao Xiang 	struct page **compressedpages;
13247e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
13347e4937aSGao Xiang 
13447e4937aSGao Xiang 	enum z_erofs_collectmode mode;
13547e4937aSGao Xiang };
13647e4937aSGao Xiang 
13747e4937aSGao Xiang struct z_erofs_decompress_frontend {
13847e4937aSGao Xiang 	struct inode *const inode;
13947e4937aSGao Xiang 
14047e4937aSGao Xiang 	struct z_erofs_collector clt;
14147e4937aSGao Xiang 	struct erofs_map_blocks map;
14247e4937aSGao Xiang 
1436ea5aad3SGao Xiang 	bool readahead;
14447e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
14547e4937aSGao Xiang 	bool backmost;
14647e4937aSGao Xiang 	erofs_off_t headoffset;
14747e4937aSGao Xiang };
14847e4937aSGao Xiang 
14947e4937aSGao Xiang #define COLLECTOR_INIT() { \
15047e4937aSGao Xiang 	.owned_head = Z_EROFS_PCLUSTER_TAIL, \
15147e4937aSGao Xiang 	.mode = COLLECT_PRIMARY_FOLLOWED }
15247e4937aSGao Xiang 
15347e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
15447e4937aSGao Xiang 	.inode = __i, .clt = COLLECTOR_INIT(), \
15547e4937aSGao Xiang 	.backmost = true, }
15647e4937aSGao Xiang 
15747e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
15847e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
15947e4937aSGao Xiang 
16047e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt,
16147e4937aSGao Xiang 				     struct address_space *mc,
162*1825c8d7SGao Xiang 				     enum z_erofs_cache_alloctype type,
163*1825c8d7SGao Xiang 				     struct list_head *pagepool)
16447e4937aSGao Xiang {
16547e4937aSGao Xiang 	const struct z_erofs_pcluster *pcl = clt->pcl;
16647e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
16747e4937aSGao Xiang 	struct page **pages = clt->compressedpages;
16847e4937aSGao Xiang 	pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
16947e4937aSGao Xiang 	bool standalone = true;
170*1825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
171*1825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
17247e4937aSGao Xiang 
17347e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
17447e4937aSGao Xiang 		return;
17547e4937aSGao Xiang 
17647e4937aSGao Xiang 	for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
17747e4937aSGao Xiang 		struct page *page;
17847e4937aSGao Xiang 		compressed_page_t t;
179*1825c8d7SGao Xiang 		struct page *newpage = NULL;
18047e4937aSGao Xiang 
18147e4937aSGao Xiang 		/* the compressed page was loaded before */
18247e4937aSGao Xiang 		if (READ_ONCE(*pages))
18347e4937aSGao Xiang 			continue;
18447e4937aSGao Xiang 
18547e4937aSGao Xiang 		page = find_get_page(mc, index);
18647e4937aSGao Xiang 
18747e4937aSGao Xiang 		if (page) {
18847e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
18947e4937aSGao Xiang 		} else if (type == DELAYEDALLOC) {
19047e4937aSGao Xiang 			t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
191*1825c8d7SGao Xiang 		} else if (type == TRYALLOC) {
192*1825c8d7SGao Xiang 			newpage = erofs_allocpage(pagepool, gfp);
193*1825c8d7SGao Xiang 			if (!newpage)
194*1825c8d7SGao Xiang 				goto dontalloc;
195*1825c8d7SGao Xiang 
196*1825c8d7SGao Xiang 			set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
197*1825c8d7SGao Xiang 			t = tag_compressed_page_justfound(newpage);
19847e4937aSGao Xiang 		} else {	/* DONTALLOC */
199*1825c8d7SGao Xiang dontalloc:
20047e4937aSGao Xiang 			if (standalone)
20147e4937aSGao Xiang 				clt->compressedpages = pages;
20247e4937aSGao Xiang 			standalone = false;
20347e4937aSGao Xiang 			continue;
20447e4937aSGao Xiang 		}
20547e4937aSGao Xiang 
20647e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
20747e4937aSGao Xiang 			continue;
20847e4937aSGao Xiang 
209*1825c8d7SGao Xiang 		if (page) {
21047e4937aSGao Xiang 			put_page(page);
211*1825c8d7SGao Xiang 		} else if (newpage) {
212*1825c8d7SGao Xiang 			set_page_private(newpage, 0);
213*1825c8d7SGao Xiang 			list_add(&newpage->lru, pagepool);
214*1825c8d7SGao Xiang 		}
21547e4937aSGao Xiang 	}
21647e4937aSGao Xiang 
21747e4937aSGao Xiang 	if (standalone)		/* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
21847e4937aSGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
21947e4937aSGao Xiang }
22047e4937aSGao Xiang 
22147e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
22247e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
22347e4937aSGao Xiang 				       struct erofs_workgroup *grp)
22447e4937aSGao Xiang {
22547e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
22647e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
22747e4937aSGao Xiang 	struct address_space *const mapping = MNGD_MAPPING(sbi);
22847e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
22947e4937aSGao Xiang 	int i;
23047e4937aSGao Xiang 
23147e4937aSGao Xiang 	/*
23247e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
23347e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
23447e4937aSGao Xiang 	 */
23547e4937aSGao Xiang 	for (i = 0; i < clusterpages; ++i) {
23647e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
23747e4937aSGao Xiang 
23847e4937aSGao Xiang 		if (!page)
23947e4937aSGao Xiang 			continue;
24047e4937aSGao Xiang 
24147e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
24247e4937aSGao Xiang 		if (!trylock_page(page))
24347e4937aSGao Xiang 			return -EBUSY;
24447e4937aSGao Xiang 
2458d8a09b0SGao Xiang 		if (page->mapping != mapping)
24647e4937aSGao Xiang 			continue;
24747e4937aSGao Xiang 
24847e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
24947e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
2506aaa7b06SGao Xiang 		detach_page_private(page);
25147e4937aSGao Xiang 		unlock_page(page);
25247e4937aSGao Xiang 	}
25347e4937aSGao Xiang 	return 0;
25447e4937aSGao Xiang }
25547e4937aSGao Xiang 
25647e4937aSGao Xiang int erofs_try_to_free_cached_page(struct address_space *mapping,
25747e4937aSGao Xiang 				  struct page *page)
25847e4937aSGao Xiang {
25947e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
26047e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
26147e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
26247e4937aSGao Xiang 
26347e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
26447e4937aSGao Xiang 		unsigned int i;
26547e4937aSGao Xiang 
26647e4937aSGao Xiang 		for (i = 0; i < clusterpages; ++i) {
26747e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
26847e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
26947e4937aSGao Xiang 				ret = 1;
27047e4937aSGao Xiang 				break;
27147e4937aSGao Xiang 			}
27247e4937aSGao Xiang 		}
27347e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
27447e4937aSGao Xiang 
2756aaa7b06SGao Xiang 		if (ret)
2766aaa7b06SGao Xiang 			detach_page_private(page);
27747e4937aSGao Xiang 	}
27847e4937aSGao Xiang 	return ret;
27947e4937aSGao Xiang }
28047e4937aSGao Xiang 
28147e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
28299634bf3SGao Xiang static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
28347e4937aSGao Xiang 					  struct page *page)
28447e4937aSGao Xiang {
28547e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = clt->pcl;
28647e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
28747e4937aSGao Xiang 
28847e4937aSGao Xiang 	while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
28947e4937aSGao Xiang 		if (!cmpxchg(clt->compressedpages++, NULL, page))
29047e4937aSGao Xiang 			return true;
29147e4937aSGao Xiang 	}
29247e4937aSGao Xiang 	return false;
29347e4937aSGao Xiang }
29447e4937aSGao Xiang 
29547e4937aSGao Xiang /* callers must be with collection lock held */
29647e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt,
29747e4937aSGao Xiang 			       struct page *page,
29847e4937aSGao Xiang 			       enum z_erofs_page_type type)
29947e4937aSGao Xiang {
30047e4937aSGao Xiang 	int ret;
30147e4937aSGao Xiang 	bool occupied;
30247e4937aSGao Xiang 
30347e4937aSGao Xiang 	/* give priority for inplaceio */
30447e4937aSGao Xiang 	if (clt->mode >= COLLECT_PRIMARY &&
30547e4937aSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
30699634bf3SGao Xiang 	    z_erofs_try_inplace_io(clt, page))
30747e4937aSGao Xiang 		return 0;
30847e4937aSGao Xiang 
30947e4937aSGao Xiang 	ret = z_erofs_pagevec_enqueue(&clt->vector,
31047e4937aSGao Xiang 				      page, type, &occupied);
31147e4937aSGao Xiang 	clt->cl->vcnt += (unsigned int)ret;
31247e4937aSGao Xiang 
31347e4937aSGao Xiang 	return ret ? 0 : -EAGAIN;
31447e4937aSGao Xiang }
31547e4937aSGao Xiang 
316473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
31747e4937aSGao Xiang {
318473e15b0SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
319473e15b0SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
32047e4937aSGao Xiang 
321473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
322473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
323473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
32447e4937aSGao Xiang 		*owned_head = &pcl->next;
325473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
326473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED;
327473e15b0SGao Xiang 		return;
328473e15b0SGao Xiang 	}
329473e15b0SGao Xiang 
33047e4937aSGao Xiang 	/*
331473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
332473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
33347e4937aSGao Xiang 	 */
33447e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
335473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
33647e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
337473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_HOOKED;
338473e15b0SGao Xiang 		clt->tailpcl = NULL;
339473e15b0SGao Xiang 		return;
34047e4937aSGao Xiang 	}
341473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
342473e15b0SGao Xiang 	clt->mode = COLLECT_PRIMARY;
34347e4937aSGao Xiang }
34447e4937aSGao Xiang 
3459e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
34647e4937aSGao Xiang 				     struct inode *inode,
34747e4937aSGao Xiang 				     struct erofs_map_blocks *map)
34847e4937aSGao Xiang {
34964094a04SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
35047e4937aSGao Xiang 	struct z_erofs_collection *cl;
35147e4937aSGao Xiang 	unsigned int length;
35247e4937aSGao Xiang 
35364094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
35447e4937aSGao Xiang 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
35547e4937aSGao Xiang 		DBG_BUGON(1);
3569e579fc1SGao Xiang 		return -EFSCORRUPTED;
35747e4937aSGao Xiang 	}
35847e4937aSGao Xiang 
35947e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
3608d8a09b0SGao Xiang 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
36147e4937aSGao Xiang 		DBG_BUGON(1);
3629e579fc1SGao Xiang 		return -EFSCORRUPTED;
36347e4937aSGao Xiang 	}
36447e4937aSGao Xiang 
36547e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
36647e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
36747e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
36847e4937aSGao Xiang 			DBG_BUGON(1);
3699e579fc1SGao Xiang 			return -EFSCORRUPTED;
37047e4937aSGao Xiang 		}
37147e4937aSGao Xiang 	} else {
37247e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
37347e4937aSGao Xiang 
37447e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
37547e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
37647e4937aSGao Xiang 
37747e4937aSGao Xiang 		while (llen > length &&
37847e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
37947e4937aSGao Xiang 			cpu_relax();
38047e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
38147e4937aSGao Xiang 		}
38247e4937aSGao Xiang 	}
38347e4937aSGao Xiang 	mutex_lock(&cl->lock);
38447e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
38547e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
38647e4937aSGao Xiang 		clt->tailpcl = pcl;
387473e15b0SGao Xiang 
388473e15b0SGao Xiang 	z_erofs_try_to_claim_pcluster(clt);
38947e4937aSGao Xiang 	clt->cl = cl;
3909e579fc1SGao Xiang 	return 0;
39147e4937aSGao Xiang }
39247e4937aSGao Xiang 
3939e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt,
39447e4937aSGao Xiang 				       struct inode *inode,
39547e4937aSGao Xiang 				       struct erofs_map_blocks *map)
39647e4937aSGao Xiang {
39747e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
39847e4937aSGao Xiang 	struct z_erofs_collection *cl;
39964094a04SGao Xiang 	struct erofs_workgroup *grp;
40047e4937aSGao Xiang 	int err;
40147e4937aSGao Xiang 
40247e4937aSGao Xiang 	/* no available workgroup, let's allocate one */
40347e4937aSGao Xiang 	pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
4048d8a09b0SGao Xiang 	if (!pcl)
4059e579fc1SGao Xiang 		return -ENOMEM;
40647e4937aSGao Xiang 
40764094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
40847e4937aSGao Xiang 	pcl->obj.index = map->m_pa >> PAGE_SHIFT;
40947e4937aSGao Xiang 
41047e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
41147e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
41247e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
41347e4937aSGao Xiang 
41447e4937aSGao Xiang 	if (map->m_flags & EROFS_MAP_ZIPPED)
41547e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
41647e4937aSGao Xiang 	else
41747e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
41847e4937aSGao Xiang 
419a5876e24SGao Xiang 	pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
42047e4937aSGao Xiang 	pcl->clusterbits -= PAGE_SHIFT;
42147e4937aSGao Xiang 
42247e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
42347e4937aSGao Xiang 	pcl->next = clt->owned_head;
42447e4937aSGao Xiang 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
42547e4937aSGao Xiang 
42647e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
42764094a04SGao Xiang 
42864094a04SGao Xiang 	/* must be cleaned before freeing to slab */
42964094a04SGao Xiang 	DBG_BUGON(cl->nr_pages);
43064094a04SGao Xiang 	DBG_BUGON(cl->vcnt);
43164094a04SGao Xiang 
43247e4937aSGao Xiang 	cl->pageofs = map->m_la & ~PAGE_MASK;
43347e4937aSGao Xiang 
43447e4937aSGao Xiang 	/*
43547e4937aSGao Xiang 	 * lock all primary followed works before visible to others
43647e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
43747e4937aSGao Xiang 	 */
43864094a04SGao Xiang 	DBG_BUGON(!mutex_trylock(&cl->lock));
43947e4937aSGao Xiang 
44064094a04SGao Xiang 	grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
44164094a04SGao Xiang 	if (IS_ERR(grp)) {
44264094a04SGao Xiang 		err = PTR_ERR(grp);
44364094a04SGao Xiang 		goto err_out;
44464094a04SGao Xiang 	}
44564094a04SGao Xiang 
44664094a04SGao Xiang 	if (grp != &pcl->obj) {
44764094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
44864094a04SGao Xiang 		err = -EEXIST;
44964094a04SGao Xiang 		goto err_out;
45047e4937aSGao Xiang 	}
45147e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
45247e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
45347e4937aSGao Xiang 		clt->tailpcl = pcl;
45447e4937aSGao Xiang 	clt->owned_head = &pcl->next;
45547e4937aSGao Xiang 	clt->pcl = pcl;
45647e4937aSGao Xiang 	clt->cl = cl;
4579e579fc1SGao Xiang 	return 0;
45864094a04SGao Xiang 
45964094a04SGao Xiang err_out:
46064094a04SGao Xiang 	mutex_unlock(&cl->lock);
46164094a04SGao Xiang 	kmem_cache_free(pcluster_cachep, pcl);
46264094a04SGao Xiang 	return err;
46347e4937aSGao Xiang }
46447e4937aSGao Xiang 
46547e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt,
46647e4937aSGao Xiang 				   struct inode *inode,
46747e4937aSGao Xiang 				   struct erofs_map_blocks *map)
46847e4937aSGao Xiang {
46964094a04SGao Xiang 	struct erofs_workgroup *grp;
4709e579fc1SGao Xiang 	int ret;
47147e4937aSGao Xiang 
47247e4937aSGao Xiang 	DBG_BUGON(clt->cl);
47347e4937aSGao Xiang 
47447e4937aSGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
47547e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
47647e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
47747e4937aSGao Xiang 
47847e4937aSGao Xiang 	if (!PAGE_ALIGNED(map->m_pa)) {
47947e4937aSGao Xiang 		DBG_BUGON(1);
48047e4937aSGao Xiang 		return -EINVAL;
48147e4937aSGao Xiang 	}
48247e4937aSGao Xiang 
48364094a04SGao Xiang 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
48464094a04SGao Xiang 	if (grp) {
48564094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
48664094a04SGao Xiang 	} else {
4879e579fc1SGao Xiang 		ret = z_erofs_register_collection(clt, inode, map);
48847e4937aSGao Xiang 
48964094a04SGao Xiang 		if (!ret)
49064094a04SGao Xiang 			goto out;
49164094a04SGao Xiang 		if (ret != -EEXIST)
4929e579fc1SGao Xiang 			return ret;
49364094a04SGao Xiang 	}
49447e4937aSGao Xiang 
49564094a04SGao Xiang 	ret = z_erofs_lookup_collection(clt, inode, map);
49664094a04SGao Xiang 	if (ret) {
49764094a04SGao Xiang 		erofs_workgroup_put(&clt->pcl->obj);
49864094a04SGao Xiang 		return ret;
49964094a04SGao Xiang 	}
50064094a04SGao Xiang 
50164094a04SGao Xiang out:
50247e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
5039e579fc1SGao Xiang 				  clt->cl->pagevec, clt->cl->vcnt);
50447e4937aSGao Xiang 
50547e4937aSGao Xiang 	clt->compressedpages = clt->pcl->compressed_pages;
50647e4937aSGao Xiang 	if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
50747e4937aSGao Xiang 		clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
50847e4937aSGao Xiang 	return 0;
50947e4937aSGao Xiang }
51047e4937aSGao Xiang 
51147e4937aSGao Xiang /*
51247e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
51347e4937aSGao Xiang  * only after a RCU grace period.
51447e4937aSGao Xiang  */
51547e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
51647e4937aSGao Xiang {
51747e4937aSGao Xiang 	struct z_erofs_collection *const cl =
51847e4937aSGao Xiang 		container_of(head, struct z_erofs_collection, rcu);
51947e4937aSGao Xiang 
52047e4937aSGao Xiang 	kmem_cache_free(pcluster_cachep,
52147e4937aSGao Xiang 			container_of(cl, struct z_erofs_pcluster,
52247e4937aSGao Xiang 				     primary_collection));
52347e4937aSGao Xiang }
52447e4937aSGao Xiang 
52547e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
52647e4937aSGao Xiang {
52747e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
52847e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
52947e4937aSGao Xiang 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
53047e4937aSGao Xiang 
53147e4937aSGao Xiang 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
53247e4937aSGao Xiang }
53347e4937aSGao Xiang 
53447e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl)
53547e4937aSGao Xiang {
53647e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
53747e4937aSGao Xiang 		container_of(cl, struct z_erofs_pcluster, primary_collection);
53847e4937aSGao Xiang 
53947e4937aSGao Xiang 	erofs_workgroup_put(&pcl->obj);
54047e4937aSGao Xiang }
54147e4937aSGao Xiang 
54247e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt)
54347e4937aSGao Xiang {
54447e4937aSGao Xiang 	struct z_erofs_collection *cl = clt->cl;
54547e4937aSGao Xiang 
54647e4937aSGao Xiang 	if (!cl)
54747e4937aSGao Xiang 		return false;
54847e4937aSGao Xiang 
54947e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
55047e4937aSGao Xiang 	mutex_unlock(&cl->lock);
55147e4937aSGao Xiang 
55247e4937aSGao Xiang 	/*
55347e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
55447e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
55547e4937aSGao Xiang 	 */
55647e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
55747e4937aSGao Xiang 		z_erofs_collection_put(cl);
55847e4937aSGao Xiang 
55947e4937aSGao Xiang 	clt->cl = NULL;
56047e4937aSGao Xiang 	return true;
56147e4937aSGao Xiang }
56247e4937aSGao Xiang 
56347e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
56447e4937aSGao Xiang 				       unsigned int cachestrategy,
56547e4937aSGao Xiang 				       erofs_off_t la)
56647e4937aSGao Xiang {
56747e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
56847e4937aSGao Xiang 		return false;
56947e4937aSGao Xiang 
57047e4937aSGao Xiang 	if (fe->backmost)
57147e4937aSGao Xiang 		return true;
57247e4937aSGao Xiang 
57347e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
57447e4937aSGao Xiang 		la < fe->headoffset;
57547e4937aSGao Xiang }
57647e4937aSGao Xiang 
57747e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
578*1825c8d7SGao Xiang 				struct page *page, struct list_head *pagepool)
57947e4937aSGao Xiang {
58047e4937aSGao Xiang 	struct inode *const inode = fe->inode;
581bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
58247e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
58347e4937aSGao Xiang 	struct z_erofs_collector *const clt = &fe->clt;
58447e4937aSGao Xiang 	const loff_t offset = page_offset(page);
585dc76ea8cSGao Xiang 	bool tight = true;
58647e4937aSGao Xiang 
58747e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
58847e4937aSGao Xiang 	enum z_erofs_page_type page_type;
58947e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
59047e4937aSGao Xiang 	int err = 0;
59147e4937aSGao Xiang 
59247e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
59347e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
59447e4937aSGao Xiang 
59547e4937aSGao Xiang 	spiltted = 0;
59647e4937aSGao Xiang 	end = PAGE_SIZE;
59747e4937aSGao Xiang repeat:
59847e4937aSGao Xiang 	cur = end - 1;
59947e4937aSGao Xiang 
60047e4937aSGao Xiang 	/* lucky, within the range of the current map_blocks */
60147e4937aSGao Xiang 	if (offset + cur >= map->m_la &&
60247e4937aSGao Xiang 	    offset + cur < map->m_la + map->m_llen) {
60347e4937aSGao Xiang 		/* didn't get a valid collection previously (very rare) */
60447e4937aSGao Xiang 		if (!clt->cl)
60547e4937aSGao Xiang 			goto restart_now;
60647e4937aSGao Xiang 		goto hitted;
60747e4937aSGao Xiang 	}
60847e4937aSGao Xiang 
60947e4937aSGao Xiang 	/* go ahead the next map_blocks */
6104f761fa2SGao Xiang 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
61147e4937aSGao Xiang 
61247e4937aSGao Xiang 	if (z_erofs_collector_end(clt))
61347e4937aSGao Xiang 		fe->backmost = false;
61447e4937aSGao Xiang 
61547e4937aSGao Xiang 	map->m_la = offset + cur;
61647e4937aSGao Xiang 	map->m_llen = 0;
61747e4937aSGao Xiang 	err = z_erofs_map_blocks_iter(inode, map, 0);
6188d8a09b0SGao Xiang 	if (err)
61947e4937aSGao Xiang 		goto err_out;
62047e4937aSGao Xiang 
62147e4937aSGao Xiang restart_now:
6228d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
62347e4937aSGao Xiang 		goto hitted;
62447e4937aSGao Xiang 
62547e4937aSGao Xiang 	err = z_erofs_collector_begin(clt, inode, map);
6268d8a09b0SGao Xiang 	if (err)
62747e4937aSGao Xiang 		goto err_out;
62847e4937aSGao Xiang 
62947e4937aSGao Xiang 	/* preload all compressed pages (maybe downgrade role if necessary) */
630f57a3fe4SChao Yu 	if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
631*1825c8d7SGao Xiang 		cache_strategy = TRYALLOC;
63247e4937aSGao Xiang 	else
63347e4937aSGao Xiang 		cache_strategy = DONTALLOC;
63447e4937aSGao Xiang 
635*1825c8d7SGao Xiang 	preload_compressed_pages(clt, MNGD_MAPPING(sbi),
636*1825c8d7SGao Xiang 				 cache_strategy, pagepool);
63747e4937aSGao Xiang 
63847e4937aSGao Xiang hitted:
639dc76ea8cSGao Xiang 	/*
640dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
641dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
642dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
643dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
644dc76ea8cSGao Xiang 	 */
645dc76ea8cSGao Xiang 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
646dc76ea8cSGao Xiang 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
647dc76ea8cSGao Xiang 
64847e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
6498d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
65047e4937aSGao Xiang 		zero_user_segment(page, cur, end);
65147e4937aSGao Xiang 		goto next_part;
65247e4937aSGao Xiang 	}
65347e4937aSGao Xiang 
65447e4937aSGao Xiang 	/* let's derive page type */
65547e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
65647e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
65747e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
65847e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
65947e4937aSGao Xiang 
66047e4937aSGao Xiang 	if (cur)
66147e4937aSGao Xiang 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
66247e4937aSGao Xiang 
66347e4937aSGao Xiang retry:
66447e4937aSGao Xiang 	err = z_erofs_attach_page(clt, page, page_type);
6656aaa7b06SGao Xiang 	/* should allocate an additional short-lived page for pagevec */
66647e4937aSGao Xiang 	if (err == -EAGAIN) {
66747e4937aSGao Xiang 		struct page *const newpage =
668e3f78d5eSChao Yu 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
66947e4937aSGao Xiang 
6706aaa7b06SGao Xiang 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
67147e4937aSGao Xiang 		err = z_erofs_attach_page(clt, newpage,
67247e4937aSGao Xiang 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
6738d8a09b0SGao Xiang 		if (!err)
67447e4937aSGao Xiang 			goto retry;
67547e4937aSGao Xiang 	}
67647e4937aSGao Xiang 
6778d8a09b0SGao Xiang 	if (err)
67847e4937aSGao Xiang 		goto err_out;
67947e4937aSGao Xiang 
68047e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
68147e4937aSGao Xiang 
68247e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
68347e4937aSGao Xiang 
68447e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
68547e4937aSGao Xiang 	++spiltted;
68647e4937aSGao Xiang 	/* also update nr_pages */
68747e4937aSGao Xiang 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
68847e4937aSGao Xiang next_part:
68947e4937aSGao Xiang 	/* can be used for verification */
69047e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
69147e4937aSGao Xiang 
69247e4937aSGao Xiang 	end = cur;
69347e4937aSGao Xiang 	if (end > 0)
69447e4937aSGao Xiang 		goto repeat;
69547e4937aSGao Xiang 
69647e4937aSGao Xiang out:
69747e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
69847e4937aSGao Xiang 
6994f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
70047e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
70147e4937aSGao Xiang 	return err;
70247e4937aSGao Xiang 
70347e4937aSGao Xiang 	/* if some error occurred while processing this page */
70447e4937aSGao Xiang err_out:
70547e4937aSGao Xiang 	SetPageError(page);
70647e4937aSGao Xiang 	goto out;
70747e4937aSGao Xiang }
70847e4937aSGao Xiang 
709a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
710a4b1fab1SGao Xiang 				       bool sync, int bios)
71147e4937aSGao Xiang {
712a4b1fab1SGao Xiang 	/* wake up the caller thread for sync decompression */
713a4b1fab1SGao Xiang 	if (sync) {
71447e4937aSGao Xiang 		unsigned long flags;
71547e4937aSGao Xiang 
71647e4937aSGao Xiang 		spin_lock_irqsave(&io->u.wait.lock, flags);
71747e4937aSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
71847e4937aSGao Xiang 			wake_up_locked(&io->u.wait);
71947e4937aSGao Xiang 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
72047e4937aSGao Xiang 		return;
72147e4937aSGao Xiang 	}
72247e4937aSGao Xiang 
72347e4937aSGao Xiang 	if (!atomic_add_return(bios, &io->pending_bios))
72447e4937aSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
72547e4937aSGao Xiang }
72647e4937aSGao Xiang 
7276aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
7286aaa7b06SGao Xiang {
7296aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
7306aaa7b06SGao Xiang }
7316aaa7b06SGao Xiang 
7320c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
73347e4937aSGao Xiang {
734a4b1fab1SGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
735a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
73647e4937aSGao Xiang 	blk_status_t err = bio->bi_status;
73747e4937aSGao Xiang 	struct bio_vec *bvec;
73847e4937aSGao Xiang 	struct bvec_iter_all iter_all;
73947e4937aSGao Xiang 
74047e4937aSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
74147e4937aSGao Xiang 		struct page *page = bvec->bv_page;
74247e4937aSGao Xiang 
74347e4937aSGao Xiang 		DBG_BUGON(PageUptodate(page));
7446aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
74547e4937aSGao Xiang 
7468d8a09b0SGao Xiang 		if (err)
74747e4937aSGao Xiang 			SetPageError(page);
74847e4937aSGao Xiang 
749a4b1fab1SGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
750a4b1fab1SGao Xiang 			if (!err)
751a4b1fab1SGao Xiang 				SetPageUptodate(page);
75247e4937aSGao Xiang 			unlock_page(page);
75347e4937aSGao Xiang 		}
754a4b1fab1SGao Xiang 	}
755a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
75647e4937aSGao Xiang 	bio_put(bio);
75747e4937aSGao Xiang }
75847e4937aSGao Xiang 
75947e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
76047e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
76147e4937aSGao Xiang 				       struct list_head *pagepool)
76247e4937aSGao Xiang {
76347e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
76447e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
76547e4937aSGao Xiang 	struct z_erofs_pagevec_ctor ctor;
76647e4937aSGao Xiang 	unsigned int i, outputsize, llen, nr_pages;
76747e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
76847e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
76947e4937aSGao Xiang 
77047e4937aSGao Xiang 	enum z_erofs_page_type page_type;
77147e4937aSGao Xiang 	bool overlapped, partial;
77247e4937aSGao Xiang 	struct z_erofs_collection *cl;
77347e4937aSGao Xiang 	int err;
77447e4937aSGao Xiang 
77547e4937aSGao Xiang 	might_sleep();
77647e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
77747e4937aSGao Xiang 	DBG_BUGON(!READ_ONCE(cl->nr_pages));
77847e4937aSGao Xiang 
77947e4937aSGao Xiang 	mutex_lock(&cl->lock);
78047e4937aSGao Xiang 	nr_pages = cl->nr_pages;
78147e4937aSGao Xiang 
7828d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
78347e4937aSGao Xiang 		pages = pages_onstack;
78447e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
78547e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
78647e4937aSGao Xiang 		pages = z_pagemap_global;
78747e4937aSGao Xiang 	} else {
78847e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
78947e4937aSGao Xiang 
79047e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
79147e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
79247e4937aSGao Xiang 
79347e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
79447e4937aSGao Xiang 				       gfp_flags);
79547e4937aSGao Xiang 
79647e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
7978d8a09b0SGao Xiang 		if (!pages) {
79847e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
79947e4937aSGao Xiang 			pages = z_pagemap_global;
80047e4937aSGao Xiang 		}
80147e4937aSGao Xiang 	}
80247e4937aSGao Xiang 
80347e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
80447e4937aSGao Xiang 		pages[i] = NULL;
80547e4937aSGao Xiang 
80647e4937aSGao Xiang 	err = 0;
80747e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
80847e4937aSGao Xiang 				  cl->pagevec, 0);
80947e4937aSGao Xiang 
81047e4937aSGao Xiang 	for (i = 0; i < cl->vcnt; ++i) {
81147e4937aSGao Xiang 		unsigned int pagenr;
81247e4937aSGao Xiang 
81347e4937aSGao Xiang 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
81447e4937aSGao Xiang 
81547e4937aSGao Xiang 		/* all pages in pagevec ought to be valid */
81647e4937aSGao Xiang 		DBG_BUGON(!page);
8176aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
81847e4937aSGao Xiang 
8196aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
82047e4937aSGao Xiang 			continue;
82147e4937aSGao Xiang 
82247e4937aSGao Xiang 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
82347e4937aSGao Xiang 			pagenr = 0;
82447e4937aSGao Xiang 		else
82547e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
82647e4937aSGao Xiang 
82747e4937aSGao Xiang 		DBG_BUGON(pagenr >= nr_pages);
82847e4937aSGao Xiang 
82947e4937aSGao Xiang 		/*
83047e4937aSGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
83147e4937aSGao Xiang 		 * so here erroring out one multiref page.
83247e4937aSGao Xiang 		 */
8338d8a09b0SGao Xiang 		if (pages[pagenr]) {
83447e4937aSGao Xiang 			DBG_BUGON(1);
83547e4937aSGao Xiang 			SetPageError(pages[pagenr]);
83647e4937aSGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
83747e4937aSGao Xiang 			err = -EFSCORRUPTED;
83847e4937aSGao Xiang 		}
83947e4937aSGao Xiang 		pages[pagenr] = page;
84047e4937aSGao Xiang 	}
84147e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&ctor, true);
84247e4937aSGao Xiang 
84347e4937aSGao Xiang 	overlapped = false;
84447e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
84547e4937aSGao Xiang 
84647e4937aSGao Xiang 	for (i = 0; i < clusterpages; ++i) {
84747e4937aSGao Xiang 		unsigned int pagenr;
84847e4937aSGao Xiang 
84947e4937aSGao Xiang 		page = compressed_pages[i];
85047e4937aSGao Xiang 
85147e4937aSGao Xiang 		/* all compressed pages ought to be valid */
85247e4937aSGao Xiang 		DBG_BUGON(!page);
8536aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
85447e4937aSGao Xiang 
8556aaa7b06SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
85647e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
8578d8a09b0SGao Xiang 				if (!PageUptodate(page))
85847e4937aSGao Xiang 					err = -EIO;
85947e4937aSGao Xiang 				continue;
86047e4937aSGao Xiang 			}
86147e4937aSGao Xiang 
86247e4937aSGao Xiang 			/*
86347e4937aSGao Xiang 			 * only if non-head page can be selected
86447e4937aSGao Xiang 			 * for inplace decompression
86547e4937aSGao Xiang 			 */
86647e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
86747e4937aSGao Xiang 
86847e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
8698d8a09b0SGao Xiang 			if (pages[pagenr]) {
87047e4937aSGao Xiang 				DBG_BUGON(1);
87147e4937aSGao Xiang 				SetPageError(pages[pagenr]);
87247e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
87347e4937aSGao Xiang 				err = -EFSCORRUPTED;
87447e4937aSGao Xiang 			}
87547e4937aSGao Xiang 			pages[pagenr] = page;
87647e4937aSGao Xiang 
87747e4937aSGao Xiang 			overlapped = true;
87847e4937aSGao Xiang 		}
87947e4937aSGao Xiang 
8806aaa7b06SGao Xiang 		/* PG_error needs checking for all non-managed pages */
8818d8a09b0SGao Xiang 		if (PageError(page)) {
88247e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
88347e4937aSGao Xiang 			err = -EIO;
88447e4937aSGao Xiang 		}
88547e4937aSGao Xiang 	}
88647e4937aSGao Xiang 
8878d8a09b0SGao Xiang 	if (err)
88847e4937aSGao Xiang 		goto out;
88947e4937aSGao Xiang 
89047e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
89147e4937aSGao Xiang 	if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
89247e4937aSGao Xiang 		outputsize = llen;
89347e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
89447e4937aSGao Xiang 	} else {
89547e4937aSGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
89647e4937aSGao Xiang 		partial = true;
89747e4937aSGao Xiang 	}
89847e4937aSGao Xiang 
89947e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
90047e4937aSGao Xiang 					.sb = sb,
90147e4937aSGao Xiang 					.in = compressed_pages,
90247e4937aSGao Xiang 					.out = pages,
90347e4937aSGao Xiang 					.pageofs_out = cl->pageofs,
90447e4937aSGao Xiang 					.inputsize = PAGE_SIZE,
90547e4937aSGao Xiang 					.outputsize = outputsize,
90647e4937aSGao Xiang 					.alg = pcl->algorithmformat,
90747e4937aSGao Xiang 					.inplace_io = overlapped,
90847e4937aSGao Xiang 					.partial_decoding = partial
90947e4937aSGao Xiang 				 }, pagepool);
91047e4937aSGao Xiang 
91147e4937aSGao Xiang out:
91247e4937aSGao Xiang 	/* must handle all compressed pages before endding pages */
91347e4937aSGao Xiang 	for (i = 0; i < clusterpages; ++i) {
91447e4937aSGao Xiang 		page = compressed_pages[i];
91547e4937aSGao Xiang 
91647e4937aSGao Xiang 		if (erofs_page_is_managed(sbi, page))
91747e4937aSGao Xiang 			continue;
91847e4937aSGao Xiang 
9196aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
9206aaa7b06SGao Xiang 		(void)z_erofs_put_shortlivedpage(pagepool, page);
92147e4937aSGao Xiang 
92247e4937aSGao Xiang 		WRITE_ONCE(compressed_pages[i], NULL);
92347e4937aSGao Xiang 	}
92447e4937aSGao Xiang 
92547e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
92647e4937aSGao Xiang 		page = pages[i];
92747e4937aSGao Xiang 		if (!page)
92847e4937aSGao Xiang 			continue;
92947e4937aSGao Xiang 
9306aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
93147e4937aSGao Xiang 
9326aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
9336aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
93447e4937aSGao Xiang 			continue;
93547e4937aSGao Xiang 
9368d8a09b0SGao Xiang 		if (err < 0)
93747e4937aSGao Xiang 			SetPageError(page);
93847e4937aSGao Xiang 
93947e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
94047e4937aSGao Xiang 	}
94147e4937aSGao Xiang 
94247e4937aSGao Xiang 	if (pages == z_pagemap_global)
94347e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
9448d8a09b0SGao Xiang 	else if (pages != pages_onstack)
94547e4937aSGao Xiang 		kvfree(pages);
94647e4937aSGao Xiang 
94747e4937aSGao Xiang 	cl->nr_pages = 0;
94847e4937aSGao Xiang 	cl->vcnt = 0;
94947e4937aSGao Xiang 
95047e4937aSGao Xiang 	/* all cl locks MUST be taken before the following line */
95147e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
95247e4937aSGao Xiang 
95347e4937aSGao Xiang 	/* all cl locks SHOULD be released right now */
95447e4937aSGao Xiang 	mutex_unlock(&cl->lock);
95547e4937aSGao Xiang 
95647e4937aSGao Xiang 	z_erofs_collection_put(cl);
95747e4937aSGao Xiang 	return err;
95847e4937aSGao Xiang }
95947e4937aSGao Xiang 
9600c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
96147e4937aSGao Xiang 				     struct list_head *pagepool)
96247e4937aSGao Xiang {
96347e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
96447e4937aSGao Xiang 
96547e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
96647e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
96747e4937aSGao Xiang 
96847e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
96947e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
97047e4937aSGao Xiang 
97147e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
97247e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
97347e4937aSGao Xiang 
97447e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
97547e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
97647e4937aSGao Xiang 
977a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
97847e4937aSGao Xiang 	}
97947e4937aSGao Xiang }
98047e4937aSGao Xiang 
9810c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
98247e4937aSGao Xiang {
983a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
984a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
98547e4937aSGao Xiang 	LIST_HEAD(pagepool);
98647e4937aSGao Xiang 
987a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
9880c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
98947e4937aSGao Xiang 
99047e4937aSGao Xiang 	put_pages_list(&pagepool);
991a4b1fab1SGao Xiang 	kvfree(bgq);
99247e4937aSGao Xiang }
99347e4937aSGao Xiang 
99447e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
99547e4937aSGao Xiang 					       unsigned int nr,
99647e4937aSGao Xiang 					       struct list_head *pagepool,
99747e4937aSGao Xiang 					       struct address_space *mc,
99847e4937aSGao Xiang 					       gfp_t gfp)
99947e4937aSGao Xiang {
100047e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
100147e4937aSGao Xiang 	bool tocache = false;
100247e4937aSGao Xiang 
100347e4937aSGao Xiang 	struct address_space *mapping;
100447e4937aSGao Xiang 	struct page *oldpage, *page;
100547e4937aSGao Xiang 
100647e4937aSGao Xiang 	compressed_page_t t;
100747e4937aSGao Xiang 	int justfound;
100847e4937aSGao Xiang 
100947e4937aSGao Xiang repeat:
101047e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
101147e4937aSGao Xiang 	oldpage = page;
101247e4937aSGao Xiang 
101347e4937aSGao Xiang 	if (!page)
101447e4937aSGao Xiang 		goto out_allocpage;
101547e4937aSGao Xiang 
101647e4937aSGao Xiang 	/*
101747e4937aSGao Xiang 	 * the cached page has not been allocated and
101847e4937aSGao Xiang 	 * an placeholder is out there, prepare it now.
101947e4937aSGao Xiang 	 */
1020bda17a45SGao Xiang 	if (page == PAGE_UNALLOCATED) {
102147e4937aSGao Xiang 		tocache = true;
102247e4937aSGao Xiang 		goto out_allocpage;
102347e4937aSGao Xiang 	}
102447e4937aSGao Xiang 
102547e4937aSGao Xiang 	/* process the target tagged pointer */
102647e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
102747e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
102847e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
102947e4937aSGao Xiang 
1030*1825c8d7SGao Xiang 	/*
1031*1825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
1032*1825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
1033*1825c8d7SGao Xiang 	 */
1034*1825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1035*1825c8d7SGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
1036*1825c8d7SGao Xiang 		set_page_private(page, 0);
1037*1825c8d7SGao Xiang 		tocache = true;
1038*1825c8d7SGao Xiang 		goto out_tocache;
1039*1825c8d7SGao Xiang 	}
104047e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
104147e4937aSGao Xiang 
104247e4937aSGao Xiang 	/*
10436aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
104447e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
104547e4937aSGao Xiang 	 */
104647e4937aSGao Xiang 	if (mapping && mapping != mc)
104747e4937aSGao Xiang 		/* ought to be unmanaged pages */
104847e4937aSGao Xiang 		goto out;
104947e4937aSGao Xiang 
10506aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
10516aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
10526aaa7b06SGao Xiang 		goto out;
10536aaa7b06SGao Xiang 
105447e4937aSGao Xiang 	lock_page(page);
105547e4937aSGao Xiang 
105647e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
105747e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
105847e4937aSGao Xiang 
105947e4937aSGao Xiang 	/* the page is still in manage cache */
106047e4937aSGao Xiang 	if (page->mapping == mc) {
106147e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
106247e4937aSGao Xiang 
106347e4937aSGao Xiang 		ClearPageError(page);
106447e4937aSGao Xiang 		if (!PagePrivate(page)) {
106547e4937aSGao Xiang 			/*
106647e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
106747e4937aSGao Xiang 			 * the current restriction as well if
106847e4937aSGao Xiang 			 * the page is already in compressed_pages[].
106947e4937aSGao Xiang 			 */
107047e4937aSGao Xiang 			DBG_BUGON(!justfound);
107147e4937aSGao Xiang 
107247e4937aSGao Xiang 			justfound = 0;
107347e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
107447e4937aSGao Xiang 			SetPagePrivate(page);
107547e4937aSGao Xiang 		}
107647e4937aSGao Xiang 
107747e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
107847e4937aSGao Xiang 		if (PageUptodate(page)) {
107947e4937aSGao Xiang 			unlock_page(page);
108047e4937aSGao Xiang 			page = NULL;
108147e4937aSGao Xiang 		}
108247e4937aSGao Xiang 		goto out;
108347e4937aSGao Xiang 	}
108447e4937aSGao Xiang 
108547e4937aSGao Xiang 	/*
108647e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
108747e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
108847e4937aSGao Xiang 	 */
108947e4937aSGao Xiang 	DBG_BUGON(page->mapping);
109047e4937aSGao Xiang 	DBG_BUGON(!justfound);
109147e4937aSGao Xiang 
109247e4937aSGao Xiang 	tocache = true;
109347e4937aSGao Xiang 	unlock_page(page);
109447e4937aSGao Xiang 	put_page(page);
109547e4937aSGao Xiang out_allocpage:
10965ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
10975ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
10985ddcee1fSGao Xiang 		list_add(&page->lru, pagepool);
10995ddcee1fSGao Xiang 		cond_resched();
11005ddcee1fSGao Xiang 		goto repeat;
11015ddcee1fSGao Xiang 	}
1102*1825c8d7SGao Xiang out_tocache:
1103bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1104bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1105bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1106bf225074SGao Xiang 		goto out;
1107a30573b3SGao Xiang 	}
1108bf225074SGao Xiang 	attach_page_private(page, pcl);
1109bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1110bf225074SGao Xiang 	put_page(page);
1111bf225074SGao Xiang 
111247e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
111347e4937aSGao Xiang 	return page;
111447e4937aSGao Xiang }
111547e4937aSGao Xiang 
1116a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1117a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1118a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
111947e4937aSGao Xiang {
1120a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
112147e4937aSGao Xiang 
1122a4b1fab1SGao Xiang 	if (fg && !*fg) {
1123a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1124a4b1fab1SGao Xiang 		if (!q) {
1125a4b1fab1SGao Xiang 			*fg = true;
1126a4b1fab1SGao Xiang 			goto fg_out;
112747e4937aSGao Xiang 		}
11280c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1129a4b1fab1SGao Xiang 	} else {
1130a4b1fab1SGao Xiang fg_out:
1131a4b1fab1SGao Xiang 		q = fgq;
1132a4b1fab1SGao Xiang 		init_waitqueue_head(&fgq->u.wait);
1133a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1134a4b1fab1SGao Xiang 	}
1135a4b1fab1SGao Xiang 	q->sb = sb;
1136a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1137a4b1fab1SGao Xiang 	return q;
113847e4937aSGao Xiang }
113947e4937aSGao Xiang 
114047e4937aSGao Xiang /* define decompression jobqueue types */
114147e4937aSGao Xiang enum {
114247e4937aSGao Xiang 	JQ_BYPASS,
114347e4937aSGao Xiang 	JQ_SUBMIT,
114447e4937aSGao Xiang 	NR_JOBQUEUES,
114547e4937aSGao Xiang };
114647e4937aSGao Xiang 
114747e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1148a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1149a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
115047e4937aSGao Xiang {
115147e4937aSGao Xiang 	/*
115247e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
115347e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
115447e4937aSGao Xiang 	 */
1155a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1156a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
115747e4937aSGao Xiang 
1158a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
115947e4937aSGao Xiang }
116047e4937aSGao Xiang 
116147e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
116247e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
116347e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
116447e4937aSGao Xiang {
116547e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
116647e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
116747e4937aSGao Xiang 
116847e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
116947e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
117047e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
117147e4937aSGao Xiang 
117247e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
117347e4937aSGao Xiang 
117447e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
117547e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
117647e4937aSGao Xiang 
117747e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
117847e4937aSGao Xiang }
117947e4937aSGao Xiang 
11801e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb,
11816ea5aad3SGao Xiang 				 struct z_erofs_decompress_frontend *f,
118247e4937aSGao Xiang 				 struct list_head *pagepool,
1183a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1184a4b1fab1SGao Xiang 				 bool *force_fg)
118547e4937aSGao Xiang {
1186bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
118747e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1188a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
118947e4937aSGao Xiang 	void *bi_private;
11906ea5aad3SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
119147e4937aSGao Xiang 	/* since bio will be NULL, no need to initialize last_index */
11923f649ab7SKees Cook 	pgoff_t last_index;
11931e4a2955SGao Xiang 	unsigned int nr_bios = 0;
11941e4a2955SGao Xiang 	struct bio *bio = NULL;
119547e4937aSGao Xiang 
1196a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1197a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1198a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
119947e4937aSGao Xiang 
120047e4937aSGao Xiang 	/* by default, all need io submission */
120147e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
120247e4937aSGao Xiang 
120347e4937aSGao Xiang 	do {
120447e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
12051e4a2955SGao Xiang 		pgoff_t cur, end;
12061e4a2955SGao Xiang 		unsigned int i = 0;
12071e4a2955SGao Xiang 		bool bypass = true;
120847e4937aSGao Xiang 
120947e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
121047e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
121147e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
121247e4937aSGao Xiang 
121347e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
121447e4937aSGao Xiang 
12151e4a2955SGao Xiang 		cur = pcl->obj.index;
12161e4a2955SGao Xiang 		end = cur + BIT(pcl->clusterbits);
121747e4937aSGao Xiang 
121847e4937aSGao Xiang 		/* close the main owned chain at first */
121947e4937aSGao Xiang 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
122047e4937aSGao Xiang 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
122147e4937aSGao Xiang 
12221e4a2955SGao Xiang 		do {
12231e4a2955SGao Xiang 			struct page *page;
122447e4937aSGao Xiang 
12251e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
122647e4937aSGao Xiang 							  MNGD_MAPPING(sbi),
122747e4937aSGao Xiang 							  GFP_NOFS);
12281e4a2955SGao Xiang 			if (!page)
12291e4a2955SGao Xiang 				continue;
123047e4937aSGao Xiang 
12311e4a2955SGao Xiang 			if (bio && cur != last_index + 1) {
123247e4937aSGao Xiang submit_bio_retry:
123394e4e153SGao Xiang 				submit_bio(bio);
123447e4937aSGao Xiang 				bio = NULL;
123547e4937aSGao Xiang 			}
123647e4937aSGao Xiang 
123747e4937aSGao Xiang 			if (!bio) {
1238a5c0b780SGao Xiang 				bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1239a5c0b780SGao Xiang 
12400c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1241a5c0b780SGao Xiang 				bio_set_dev(bio, sb->s_bdev);
12421e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1243a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1244a5c0b780SGao Xiang 				bio->bi_private = bi_private;
124594e4e153SGao Xiang 				bio->bi_opf = REQ_OP_READ;
12466ea5aad3SGao Xiang 				if (f->readahead)
12476ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
124847e4937aSGao Xiang 				++nr_bios;
124947e4937aSGao Xiang 			}
125047e4937aSGao Xiang 
12516c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
125247e4937aSGao Xiang 				goto submit_bio_retry;
125347e4937aSGao Xiang 
12541e4a2955SGao Xiang 			last_index = cur;
12551e4a2955SGao Xiang 			bypass = false;
12561e4a2955SGao Xiang 		} while (++cur < end);
125747e4937aSGao Xiang 
12581e4a2955SGao Xiang 		if (!bypass)
125947e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
126047e4937aSGao Xiang 		else
126147e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
126247e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
126347e4937aSGao Xiang 
126447e4937aSGao Xiang 	if (bio)
126594e4e153SGao Xiang 		submit_bio(bio);
126647e4937aSGao Xiang 
1267587a67b7SGao Xiang 	/*
1268587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1269587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1270587a67b7SGao Xiang 	 */
1271587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1272587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
12731e4a2955SGao Xiang 		return;
1274587a67b7SGao Xiang 	}
1275a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
127647e4937aSGao Xiang }
127747e4937aSGao Xiang 
12780c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb,
12796ea5aad3SGao Xiang 			     struct z_erofs_decompress_frontend *f,
12800c638f70SGao Xiang 			     struct list_head *pagepool, bool force_fg)
128147e4937aSGao Xiang {
1282a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
128347e4937aSGao Xiang 
12846ea5aad3SGao Xiang 	if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
128547e4937aSGao Xiang 		return;
12866ea5aad3SGao Xiang 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
128747e4937aSGao Xiang 
12880c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
12890c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
129047e4937aSGao Xiang 
129147e4937aSGao Xiang 	if (!force_fg)
129247e4937aSGao Xiang 		return;
129347e4937aSGao Xiang 
129447e4937aSGao Xiang 	/* wait until all bios are completed */
1295a93f8c36SGao Xiang 	io_wait_event(io[JQ_SUBMIT].u.wait,
129647e4937aSGao Xiang 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
129747e4937aSGao Xiang 
12980c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
12990c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
130047e4937aSGao Xiang }
130147e4937aSGao Xiang 
13020c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page)
130347e4937aSGao Xiang {
130447e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
130547e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
130647e4937aSGao Xiang 	int err;
130747e4937aSGao Xiang 	LIST_HEAD(pagepool);
130847e4937aSGao Xiang 
130947e4937aSGao Xiang 	trace_erofs_readpage(page, false);
131047e4937aSGao Xiang 
131147e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
131247e4937aSGao Xiang 
1313*1825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
131447e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
131547e4937aSGao Xiang 
131647e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
13176ea5aad3SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
131847e4937aSGao Xiang 
131947e4937aSGao Xiang 	if (err)
13204f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
132147e4937aSGao Xiang 
132247e4937aSGao Xiang 	if (f.map.mpage)
132347e4937aSGao Xiang 		put_page(f.map.mpage);
132447e4937aSGao Xiang 
132547e4937aSGao Xiang 	/* clean up the remaining free pages */
132647e4937aSGao Xiang 	put_pages_list(&pagepool);
132747e4937aSGao Xiang 	return err;
132847e4937aSGao Xiang }
132947e4937aSGao Xiang 
13300615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
133147e4937aSGao Xiang {
13320615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
133347e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
133447e4937aSGao Xiang 
1335bf9a123bSGao Xiang 	unsigned int nr_pages = readahead_count(rac);
1336bf9a123bSGao Xiang 	bool sync = (nr_pages <= sbi->ctx.max_sync_decompress_pages);
133747e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
13380615090cSMatthew Wilcox (Oracle) 	struct page *page, *head = NULL;
133947e4937aSGao Xiang 	LIST_HEAD(pagepool);
134047e4937aSGao Xiang 
1341bf9a123bSGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
134247e4937aSGao Xiang 
13436ea5aad3SGao Xiang 	f.readahead = true;
13440615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
134547e4937aSGao Xiang 
13460615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
134747e4937aSGao Xiang 		prefetchw(&page->flags);
134847e4937aSGao Xiang 
134947e4937aSGao Xiang 		/*
135047e4937aSGao Xiang 		 * A pure asynchronous readahead is indicated if
135147e4937aSGao Xiang 		 * a PG_readahead marked page is hitted at first.
135247e4937aSGao Xiang 		 * Let's also do asynchronous decompression for this case.
135347e4937aSGao Xiang 		 */
135447e4937aSGao Xiang 		sync &= !(PageReadahead(page) && !head);
135547e4937aSGao Xiang 
135647e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
135747e4937aSGao Xiang 		head = page;
135847e4937aSGao Xiang 	}
135947e4937aSGao Xiang 
136047e4937aSGao Xiang 	while (head) {
136147e4937aSGao Xiang 		struct page *page = head;
136247e4937aSGao Xiang 		int err;
136347e4937aSGao Xiang 
136447e4937aSGao Xiang 		/* traversal in reverse order */
136547e4937aSGao Xiang 		head = (void *)page_private(page);
136647e4937aSGao Xiang 
1367*1825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1368a5876e24SGao Xiang 		if (err)
13694f761fa2SGao Xiang 			erofs_err(inode->i_sb,
13704f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
13714f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
137247e4937aSGao Xiang 		put_page(page);
137347e4937aSGao Xiang 	}
137447e4937aSGao Xiang 
137547e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
137647e4937aSGao Xiang 
13776ea5aad3SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
137847e4937aSGao Xiang 
137947e4937aSGao Xiang 	if (f.map.mpage)
138047e4937aSGao Xiang 		put_page(f.map.mpage);
138147e4937aSGao Xiang 
138247e4937aSGao Xiang 	/* clean up the remaining free pages */
138347e4937aSGao Xiang 	put_pages_list(&pagepool);
138447e4937aSGao Xiang }
138547e4937aSGao Xiang 
13860c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
13870c638f70SGao Xiang 	.readpage = z_erofs_readpage,
13880615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
138947e4937aSGao Xiang };
139047e4937aSGao Xiang 
1391