xref: /openbmc/linux/fs/erofs/zdata.c (revision 473e15b0)
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) */
2347e4937aSGao Xiang };
2447e4937aSGao Xiang 
2547e4937aSGao Xiang /*
2647e4937aSGao Xiang  * tagged pointer with 1-bit tag for all compressed pages
2747e4937aSGao Xiang  * tag 0 - the page is just found with an extra page reference
2847e4937aSGao Xiang  */
2947e4937aSGao Xiang typedef tagptr1_t compressed_page_t;
3047e4937aSGao Xiang 
3147e4937aSGao Xiang #define tag_compressed_page_justfound(page) \
3247e4937aSGao Xiang 	tagptr_fold(compressed_page_t, page, 1)
3347e4937aSGao Xiang 
3447e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
3547e4937aSGao Xiang static struct kmem_cache *pcluster_cachep __read_mostly;
3647e4937aSGao Xiang 
3747e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void)
3847e4937aSGao Xiang {
3947e4937aSGao Xiang 	destroy_workqueue(z_erofs_workqueue);
4047e4937aSGao Xiang 	kmem_cache_destroy(pcluster_cachep);
4147e4937aSGao Xiang }
4247e4937aSGao Xiang 
4399634bf3SGao Xiang static inline int z_erofs_init_workqueue(void)
4447e4937aSGao Xiang {
4547e4937aSGao Xiang 	const unsigned int onlinecpus = num_possible_cpus();
4647e4937aSGao Xiang 
4747e4937aSGao Xiang 	/*
4847e4937aSGao Xiang 	 * no need to spawn too many threads, limiting threads could minimum
4947e4937aSGao Xiang 	 * scheduling overhead, perhaps per-CPU threads should be better?
5047e4937aSGao Xiang 	 */
510e62ea33SGao Xiang 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
520e62ea33SGao Xiang 					    WQ_UNBOUND | WQ_HIGHPRI,
5347e4937aSGao Xiang 					    onlinecpus + onlinecpus / 4);
5447e4937aSGao Xiang 	return z_erofs_workqueue ? 0 : -ENOMEM;
5547e4937aSGao Xiang }
5647e4937aSGao Xiang 
5799634bf3SGao Xiang static void z_erofs_pcluster_init_once(void *ptr)
5847e4937aSGao Xiang {
5947e4937aSGao Xiang 	struct z_erofs_pcluster *pcl = ptr;
6047e4937aSGao Xiang 	struct z_erofs_collection *cl = z_erofs_primarycollection(pcl);
6147e4937aSGao Xiang 	unsigned int i;
6247e4937aSGao Xiang 
6347e4937aSGao Xiang 	mutex_init(&cl->lock);
6447e4937aSGao Xiang 	cl->nr_pages = 0;
6547e4937aSGao Xiang 	cl->vcnt = 0;
6647e4937aSGao Xiang 	for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i)
6747e4937aSGao Xiang 		pcl->compressed_pages[i] = NULL;
6847e4937aSGao Xiang }
6947e4937aSGao Xiang 
7047e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
7147e4937aSGao Xiang {
7247e4937aSGao Xiang 	pcluster_cachep = kmem_cache_create("erofs_compress",
7347e4937aSGao Xiang 					    Z_EROFS_WORKGROUP_SIZE, 0,
7499634bf3SGao Xiang 					    SLAB_RECLAIM_ACCOUNT,
7599634bf3SGao Xiang 					    z_erofs_pcluster_init_once);
7647e4937aSGao Xiang 	if (pcluster_cachep) {
7799634bf3SGao Xiang 		if (!z_erofs_init_workqueue())
7847e4937aSGao Xiang 			return 0;
7947e4937aSGao Xiang 
8047e4937aSGao Xiang 		kmem_cache_destroy(pcluster_cachep);
8147e4937aSGao Xiang 	}
8247e4937aSGao Xiang 	return -ENOMEM;
8347e4937aSGao Xiang }
8447e4937aSGao Xiang 
8547e4937aSGao Xiang enum z_erofs_collectmode {
8647e4937aSGao Xiang 	COLLECT_SECONDARY,
8747e4937aSGao Xiang 	COLLECT_PRIMARY,
8847e4937aSGao Xiang 	/*
8947e4937aSGao Xiang 	 * The current collection was the tail of an exist chain, in addition
9047e4937aSGao Xiang 	 * that the previous processed chained collections are all decided to
9147e4937aSGao Xiang 	 * be hooked up to it.
9247e4937aSGao Xiang 	 * A new chain will be created for the remaining collections which are
9347e4937aSGao Xiang 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
9447e4937aSGao Xiang 	 * the next collection cannot reuse the whole page safely in
9547e4937aSGao Xiang 	 * the following scenario:
9647e4937aSGao Xiang 	 *  ________________________________________________________________
9747e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
9847e4937aSGao Xiang 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
9947e4937aSGao Xiang 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
10047e4937aSGao Xiang 	 */
10147e4937aSGao Xiang 	COLLECT_PRIMARY_HOOKED,
10247e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
10347e4937aSGao Xiang 	/*
10447e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
10547e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
10647e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
10747e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
10847e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
10947e4937aSGao Xiang 	 * illustrated below:
11047e4937aSGao Xiang 	 *  ________________________________________________________________
11147e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
11247e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
11347e4937aSGao Xiang 	 * |  PRIMARY_FOLLOWED or |                                        |
11447e4937aSGao Xiang 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
11547e4937aSGao Xiang 	 *
11647e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
11747e4937aSGao Xiang 	 */
11847e4937aSGao Xiang 	COLLECT_PRIMARY_FOLLOWED,
11947e4937aSGao Xiang };
12047e4937aSGao Xiang 
12147e4937aSGao Xiang struct z_erofs_collector {
12247e4937aSGao Xiang 	struct z_erofs_pagevec_ctor vector;
12347e4937aSGao Xiang 
12447e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
12547e4937aSGao Xiang 	struct z_erofs_collection *cl;
12647e4937aSGao Xiang 	struct page **compressedpages;
12747e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
12847e4937aSGao Xiang 
12947e4937aSGao Xiang 	enum z_erofs_collectmode mode;
13047e4937aSGao Xiang };
13147e4937aSGao Xiang 
13247e4937aSGao Xiang struct z_erofs_decompress_frontend {
13347e4937aSGao Xiang 	struct inode *const inode;
13447e4937aSGao Xiang 
13547e4937aSGao Xiang 	struct z_erofs_collector clt;
13647e4937aSGao Xiang 	struct erofs_map_blocks map;
13747e4937aSGao Xiang 
1386ea5aad3SGao Xiang 	bool readahead;
13947e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
14047e4937aSGao Xiang 	bool backmost;
14147e4937aSGao Xiang 	erofs_off_t headoffset;
14247e4937aSGao Xiang };
14347e4937aSGao Xiang 
14447e4937aSGao Xiang #define COLLECTOR_INIT() { \
14547e4937aSGao Xiang 	.owned_head = Z_EROFS_PCLUSTER_TAIL, \
14647e4937aSGao Xiang 	.mode = COLLECT_PRIMARY_FOLLOWED }
14747e4937aSGao Xiang 
14847e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
14947e4937aSGao Xiang 	.inode = __i, .clt = COLLECTOR_INIT(), \
15047e4937aSGao Xiang 	.backmost = true, }
15147e4937aSGao Xiang 
15247e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
15347e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
15447e4937aSGao Xiang 
15547e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt,
15647e4937aSGao Xiang 				     struct address_space *mc,
157e3f78d5eSChao Yu 				     enum z_erofs_cache_alloctype type)
15847e4937aSGao Xiang {
15947e4937aSGao Xiang 	const struct z_erofs_pcluster *pcl = clt->pcl;
16047e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
16147e4937aSGao Xiang 	struct page **pages = clt->compressedpages;
16247e4937aSGao Xiang 	pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
16347e4937aSGao Xiang 	bool standalone = true;
16447e4937aSGao Xiang 
16547e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
16647e4937aSGao Xiang 		return;
16747e4937aSGao Xiang 
16847e4937aSGao Xiang 	for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
16947e4937aSGao Xiang 		struct page *page;
17047e4937aSGao Xiang 		compressed_page_t t;
17147e4937aSGao Xiang 
17247e4937aSGao Xiang 		/* the compressed page was loaded before */
17347e4937aSGao Xiang 		if (READ_ONCE(*pages))
17447e4937aSGao Xiang 			continue;
17547e4937aSGao Xiang 
17647e4937aSGao Xiang 		page = find_get_page(mc, index);
17747e4937aSGao Xiang 
17847e4937aSGao Xiang 		if (page) {
17947e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
18047e4937aSGao Xiang 		} else if (type == DELAYEDALLOC) {
18147e4937aSGao Xiang 			t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
18247e4937aSGao Xiang 		} else {	/* DONTALLOC */
18347e4937aSGao Xiang 			if (standalone)
18447e4937aSGao Xiang 				clt->compressedpages = pages;
18547e4937aSGao Xiang 			standalone = false;
18647e4937aSGao Xiang 			continue;
18747e4937aSGao Xiang 		}
18847e4937aSGao Xiang 
18947e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
19047e4937aSGao Xiang 			continue;
19147e4937aSGao Xiang 
19247e4937aSGao Xiang 		if (page)
19347e4937aSGao Xiang 			put_page(page);
19447e4937aSGao Xiang 	}
19547e4937aSGao Xiang 
19647e4937aSGao Xiang 	if (standalone)		/* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
19747e4937aSGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
19847e4937aSGao Xiang }
19947e4937aSGao Xiang 
20047e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
20147e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
20247e4937aSGao Xiang 				       struct erofs_workgroup *grp)
20347e4937aSGao Xiang {
20447e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
20547e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
20647e4937aSGao Xiang 	struct address_space *const mapping = MNGD_MAPPING(sbi);
20747e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
20847e4937aSGao Xiang 	int i;
20947e4937aSGao Xiang 
21047e4937aSGao Xiang 	/*
21147e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
21247e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
21347e4937aSGao Xiang 	 */
21447e4937aSGao Xiang 	for (i = 0; i < clusterpages; ++i) {
21547e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
21647e4937aSGao Xiang 
21747e4937aSGao Xiang 		if (!page)
21847e4937aSGao Xiang 			continue;
21947e4937aSGao Xiang 
22047e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
22147e4937aSGao Xiang 		if (!trylock_page(page))
22247e4937aSGao Xiang 			return -EBUSY;
22347e4937aSGao Xiang 
2248d8a09b0SGao Xiang 		if (page->mapping != mapping)
22547e4937aSGao Xiang 			continue;
22647e4937aSGao Xiang 
22747e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
22847e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
2296aaa7b06SGao Xiang 		detach_page_private(page);
23047e4937aSGao Xiang 		unlock_page(page);
23147e4937aSGao Xiang 	}
23247e4937aSGao Xiang 	return 0;
23347e4937aSGao Xiang }
23447e4937aSGao Xiang 
23547e4937aSGao Xiang int erofs_try_to_free_cached_page(struct address_space *mapping,
23647e4937aSGao Xiang 				  struct page *page)
23747e4937aSGao Xiang {
23847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
23947e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
24047e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
24147e4937aSGao Xiang 
24247e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
24347e4937aSGao Xiang 		unsigned int i;
24447e4937aSGao Xiang 
24547e4937aSGao Xiang 		for (i = 0; i < clusterpages; ++i) {
24647e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
24747e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
24847e4937aSGao Xiang 				ret = 1;
24947e4937aSGao Xiang 				break;
25047e4937aSGao Xiang 			}
25147e4937aSGao Xiang 		}
25247e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
25347e4937aSGao Xiang 
2546aaa7b06SGao Xiang 		if (ret)
2556aaa7b06SGao Xiang 			detach_page_private(page);
25647e4937aSGao Xiang 	}
25747e4937aSGao Xiang 	return ret;
25847e4937aSGao Xiang }
25947e4937aSGao Xiang 
26047e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
26199634bf3SGao Xiang static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
26247e4937aSGao Xiang 					  struct page *page)
26347e4937aSGao Xiang {
26447e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = clt->pcl;
26547e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
26647e4937aSGao Xiang 
26747e4937aSGao Xiang 	while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
26847e4937aSGao Xiang 		if (!cmpxchg(clt->compressedpages++, NULL, page))
26947e4937aSGao Xiang 			return true;
27047e4937aSGao Xiang 	}
27147e4937aSGao Xiang 	return false;
27247e4937aSGao Xiang }
27347e4937aSGao Xiang 
27447e4937aSGao Xiang /* callers must be with collection lock held */
27547e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt,
27647e4937aSGao Xiang 			       struct page *page,
27747e4937aSGao Xiang 			       enum z_erofs_page_type type)
27847e4937aSGao Xiang {
27947e4937aSGao Xiang 	int ret;
28047e4937aSGao Xiang 	bool occupied;
28147e4937aSGao Xiang 
28247e4937aSGao Xiang 	/* give priority for inplaceio */
28347e4937aSGao Xiang 	if (clt->mode >= COLLECT_PRIMARY &&
28447e4937aSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
28599634bf3SGao Xiang 	    z_erofs_try_inplace_io(clt, page))
28647e4937aSGao Xiang 		return 0;
28747e4937aSGao Xiang 
28847e4937aSGao Xiang 	ret = z_erofs_pagevec_enqueue(&clt->vector,
28947e4937aSGao Xiang 				      page, type, &occupied);
29047e4937aSGao Xiang 	clt->cl->vcnt += (unsigned int)ret;
29147e4937aSGao Xiang 
29247e4937aSGao Xiang 	return ret ? 0 : -EAGAIN;
29347e4937aSGao Xiang }
29447e4937aSGao Xiang 
295*473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
29647e4937aSGao Xiang {
297*473e15b0SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
298*473e15b0SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
29947e4937aSGao Xiang 
300*473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
301*473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
302*473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
30347e4937aSGao Xiang 		*owned_head = &pcl->next;
304*473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
305*473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED;
306*473e15b0SGao Xiang 		return;
307*473e15b0SGao Xiang 	}
308*473e15b0SGao Xiang 
30947e4937aSGao Xiang 	/*
310*473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
311*473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
31247e4937aSGao Xiang 	 */
31347e4937aSGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
314*473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
31547e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
316*473e15b0SGao Xiang 		clt->mode = COLLECT_PRIMARY_HOOKED;
317*473e15b0SGao Xiang 		clt->tailpcl = NULL;
318*473e15b0SGao Xiang 		return;
31947e4937aSGao Xiang 	}
320*473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
321*473e15b0SGao Xiang 	clt->mode = COLLECT_PRIMARY;
32247e4937aSGao Xiang }
32347e4937aSGao Xiang 
3249e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
32547e4937aSGao Xiang 				     struct inode *inode,
32647e4937aSGao Xiang 				     struct erofs_map_blocks *map)
32747e4937aSGao Xiang {
32864094a04SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
32947e4937aSGao Xiang 	struct z_erofs_collection *cl;
33047e4937aSGao Xiang 	unsigned int length;
33147e4937aSGao Xiang 
33264094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
33347e4937aSGao Xiang 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
33447e4937aSGao Xiang 		DBG_BUGON(1);
3359e579fc1SGao Xiang 		return -EFSCORRUPTED;
33647e4937aSGao Xiang 	}
33747e4937aSGao Xiang 
33847e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
3398d8a09b0SGao Xiang 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
34047e4937aSGao Xiang 		DBG_BUGON(1);
3419e579fc1SGao Xiang 		return -EFSCORRUPTED;
34247e4937aSGao Xiang 	}
34347e4937aSGao Xiang 
34447e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
34547e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
34647e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
34747e4937aSGao Xiang 			DBG_BUGON(1);
3489e579fc1SGao Xiang 			return -EFSCORRUPTED;
34947e4937aSGao Xiang 		}
35047e4937aSGao Xiang 	} else {
35147e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
35247e4937aSGao Xiang 
35347e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
35447e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
35547e4937aSGao Xiang 
35647e4937aSGao Xiang 		while (llen > length &&
35747e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
35847e4937aSGao Xiang 			cpu_relax();
35947e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
36047e4937aSGao Xiang 		}
36147e4937aSGao Xiang 	}
36247e4937aSGao Xiang 	mutex_lock(&cl->lock);
36347e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
36447e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
36547e4937aSGao Xiang 		clt->tailpcl = pcl;
366*473e15b0SGao Xiang 
367*473e15b0SGao Xiang 	z_erofs_try_to_claim_pcluster(clt);
36847e4937aSGao Xiang 	clt->cl = cl;
3699e579fc1SGao Xiang 	return 0;
37047e4937aSGao Xiang }
37147e4937aSGao Xiang 
3729e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt,
37347e4937aSGao Xiang 				       struct inode *inode,
37447e4937aSGao Xiang 				       struct erofs_map_blocks *map)
37547e4937aSGao Xiang {
37647e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
37747e4937aSGao Xiang 	struct z_erofs_collection *cl;
37864094a04SGao Xiang 	struct erofs_workgroup *grp;
37947e4937aSGao Xiang 	int err;
38047e4937aSGao Xiang 
38147e4937aSGao Xiang 	/* no available workgroup, let's allocate one */
38247e4937aSGao Xiang 	pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
3838d8a09b0SGao Xiang 	if (!pcl)
3849e579fc1SGao Xiang 		return -ENOMEM;
38547e4937aSGao Xiang 
38664094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
38747e4937aSGao Xiang 	pcl->obj.index = map->m_pa >> PAGE_SHIFT;
38847e4937aSGao Xiang 
38947e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
39047e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
39147e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
39247e4937aSGao Xiang 
39347e4937aSGao Xiang 	if (map->m_flags & EROFS_MAP_ZIPPED)
39447e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
39547e4937aSGao Xiang 	else
39647e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
39747e4937aSGao Xiang 
398a5876e24SGao Xiang 	pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
39947e4937aSGao Xiang 	pcl->clusterbits -= PAGE_SHIFT;
40047e4937aSGao Xiang 
40147e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
40247e4937aSGao Xiang 	pcl->next = clt->owned_head;
40347e4937aSGao Xiang 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
40447e4937aSGao Xiang 
40547e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
40664094a04SGao Xiang 
40764094a04SGao Xiang 	/* must be cleaned before freeing to slab */
40864094a04SGao Xiang 	DBG_BUGON(cl->nr_pages);
40964094a04SGao Xiang 	DBG_BUGON(cl->vcnt);
41064094a04SGao Xiang 
41147e4937aSGao Xiang 	cl->pageofs = map->m_la & ~PAGE_MASK;
41247e4937aSGao Xiang 
41347e4937aSGao Xiang 	/*
41447e4937aSGao Xiang 	 * lock all primary followed works before visible to others
41547e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
41647e4937aSGao Xiang 	 */
41764094a04SGao Xiang 	DBG_BUGON(!mutex_trylock(&cl->lock));
41847e4937aSGao Xiang 
41964094a04SGao Xiang 	grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
42064094a04SGao Xiang 	if (IS_ERR(grp)) {
42164094a04SGao Xiang 		err = PTR_ERR(grp);
42264094a04SGao Xiang 		goto err_out;
42364094a04SGao Xiang 	}
42464094a04SGao Xiang 
42564094a04SGao Xiang 	if (grp != &pcl->obj) {
42664094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
42764094a04SGao Xiang 		err = -EEXIST;
42864094a04SGao Xiang 		goto err_out;
42947e4937aSGao Xiang 	}
43047e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
43147e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
43247e4937aSGao Xiang 		clt->tailpcl = pcl;
43347e4937aSGao Xiang 	clt->owned_head = &pcl->next;
43447e4937aSGao Xiang 	clt->pcl = pcl;
43547e4937aSGao Xiang 	clt->cl = cl;
4369e579fc1SGao Xiang 	return 0;
43764094a04SGao Xiang 
43864094a04SGao Xiang err_out:
43964094a04SGao Xiang 	mutex_unlock(&cl->lock);
44064094a04SGao Xiang 	kmem_cache_free(pcluster_cachep, pcl);
44164094a04SGao Xiang 	return err;
44247e4937aSGao Xiang }
44347e4937aSGao Xiang 
44447e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt,
44547e4937aSGao Xiang 				   struct inode *inode,
44647e4937aSGao Xiang 				   struct erofs_map_blocks *map)
44747e4937aSGao Xiang {
44864094a04SGao Xiang 	struct erofs_workgroup *grp;
4499e579fc1SGao Xiang 	int ret;
45047e4937aSGao Xiang 
45147e4937aSGao Xiang 	DBG_BUGON(clt->cl);
45247e4937aSGao Xiang 
45347e4937aSGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
45447e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
45547e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
45647e4937aSGao Xiang 
45747e4937aSGao Xiang 	if (!PAGE_ALIGNED(map->m_pa)) {
45847e4937aSGao Xiang 		DBG_BUGON(1);
45947e4937aSGao Xiang 		return -EINVAL;
46047e4937aSGao Xiang 	}
46147e4937aSGao Xiang 
46264094a04SGao Xiang 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
46364094a04SGao Xiang 	if (grp) {
46464094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
46564094a04SGao Xiang 	} else {
4669e579fc1SGao Xiang 		ret = z_erofs_register_collection(clt, inode, map);
46747e4937aSGao Xiang 
46864094a04SGao Xiang 		if (!ret)
46964094a04SGao Xiang 			goto out;
47064094a04SGao Xiang 		if (ret != -EEXIST)
4719e579fc1SGao Xiang 			return ret;
47264094a04SGao Xiang 	}
47347e4937aSGao Xiang 
47464094a04SGao Xiang 	ret = z_erofs_lookup_collection(clt, inode, map);
47564094a04SGao Xiang 	if (ret) {
47664094a04SGao Xiang 		erofs_workgroup_put(&clt->pcl->obj);
47764094a04SGao Xiang 		return ret;
47864094a04SGao Xiang 	}
47964094a04SGao Xiang 
48064094a04SGao Xiang out:
48147e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
4829e579fc1SGao Xiang 				  clt->cl->pagevec, clt->cl->vcnt);
48347e4937aSGao Xiang 
48447e4937aSGao Xiang 	clt->compressedpages = clt->pcl->compressed_pages;
48547e4937aSGao Xiang 	if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
48647e4937aSGao Xiang 		clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
48747e4937aSGao Xiang 	return 0;
48847e4937aSGao Xiang }
48947e4937aSGao Xiang 
49047e4937aSGao Xiang /*
49147e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
49247e4937aSGao Xiang  * only after a RCU grace period.
49347e4937aSGao Xiang  */
49447e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
49547e4937aSGao Xiang {
49647e4937aSGao Xiang 	struct z_erofs_collection *const cl =
49747e4937aSGao Xiang 		container_of(head, struct z_erofs_collection, rcu);
49847e4937aSGao Xiang 
49947e4937aSGao Xiang 	kmem_cache_free(pcluster_cachep,
50047e4937aSGao Xiang 			container_of(cl, struct z_erofs_pcluster,
50147e4937aSGao Xiang 				     primary_collection));
50247e4937aSGao Xiang }
50347e4937aSGao Xiang 
50447e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
50547e4937aSGao Xiang {
50647e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
50747e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
50847e4937aSGao Xiang 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
50947e4937aSGao Xiang 
51047e4937aSGao Xiang 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
51147e4937aSGao Xiang }
51247e4937aSGao Xiang 
51347e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl)
51447e4937aSGao Xiang {
51547e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
51647e4937aSGao Xiang 		container_of(cl, struct z_erofs_pcluster, primary_collection);
51747e4937aSGao Xiang 
51847e4937aSGao Xiang 	erofs_workgroup_put(&pcl->obj);
51947e4937aSGao Xiang }
52047e4937aSGao Xiang 
52147e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt)
52247e4937aSGao Xiang {
52347e4937aSGao Xiang 	struct z_erofs_collection *cl = clt->cl;
52447e4937aSGao Xiang 
52547e4937aSGao Xiang 	if (!cl)
52647e4937aSGao Xiang 		return false;
52747e4937aSGao Xiang 
52847e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
52947e4937aSGao Xiang 	mutex_unlock(&cl->lock);
53047e4937aSGao Xiang 
53147e4937aSGao Xiang 	/*
53247e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
53347e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
53447e4937aSGao Xiang 	 */
53547e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
53647e4937aSGao Xiang 		z_erofs_collection_put(cl);
53747e4937aSGao Xiang 
53847e4937aSGao Xiang 	clt->cl = NULL;
53947e4937aSGao Xiang 	return true;
54047e4937aSGao Xiang }
54147e4937aSGao Xiang 
54247e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
54347e4937aSGao Xiang 				       unsigned int cachestrategy,
54447e4937aSGao Xiang 				       erofs_off_t la)
54547e4937aSGao Xiang {
54647e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
54747e4937aSGao Xiang 		return false;
54847e4937aSGao Xiang 
54947e4937aSGao Xiang 	if (fe->backmost)
55047e4937aSGao Xiang 		return true;
55147e4937aSGao Xiang 
55247e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
55347e4937aSGao Xiang 		la < fe->headoffset;
55447e4937aSGao Xiang }
55547e4937aSGao Xiang 
55647e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
557e3f78d5eSChao Yu 				struct page *page)
55847e4937aSGao Xiang {
55947e4937aSGao Xiang 	struct inode *const inode = fe->inode;
560bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
56147e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
56247e4937aSGao Xiang 	struct z_erofs_collector *const clt = &fe->clt;
56347e4937aSGao Xiang 	const loff_t offset = page_offset(page);
564dc76ea8cSGao Xiang 	bool tight = true;
56547e4937aSGao Xiang 
56647e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
56747e4937aSGao Xiang 	enum z_erofs_page_type page_type;
56847e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
56947e4937aSGao Xiang 	int err = 0;
57047e4937aSGao Xiang 
57147e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
57247e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
57347e4937aSGao Xiang 
57447e4937aSGao Xiang 	spiltted = 0;
57547e4937aSGao Xiang 	end = PAGE_SIZE;
57647e4937aSGao Xiang repeat:
57747e4937aSGao Xiang 	cur = end - 1;
57847e4937aSGao Xiang 
57947e4937aSGao Xiang 	/* lucky, within the range of the current map_blocks */
58047e4937aSGao Xiang 	if (offset + cur >= map->m_la &&
58147e4937aSGao Xiang 	    offset + cur < map->m_la + map->m_llen) {
58247e4937aSGao Xiang 		/* didn't get a valid collection previously (very rare) */
58347e4937aSGao Xiang 		if (!clt->cl)
58447e4937aSGao Xiang 			goto restart_now;
58547e4937aSGao Xiang 		goto hitted;
58647e4937aSGao Xiang 	}
58747e4937aSGao Xiang 
58847e4937aSGao Xiang 	/* go ahead the next map_blocks */
5894f761fa2SGao Xiang 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
59047e4937aSGao Xiang 
59147e4937aSGao Xiang 	if (z_erofs_collector_end(clt))
59247e4937aSGao Xiang 		fe->backmost = false;
59347e4937aSGao Xiang 
59447e4937aSGao Xiang 	map->m_la = offset + cur;
59547e4937aSGao Xiang 	map->m_llen = 0;
59647e4937aSGao Xiang 	err = z_erofs_map_blocks_iter(inode, map, 0);
5978d8a09b0SGao Xiang 	if (err)
59847e4937aSGao Xiang 		goto err_out;
59947e4937aSGao Xiang 
60047e4937aSGao Xiang restart_now:
6018d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
60247e4937aSGao Xiang 		goto hitted;
60347e4937aSGao Xiang 
60447e4937aSGao Xiang 	err = z_erofs_collector_begin(clt, inode, map);
6058d8a09b0SGao Xiang 	if (err)
60647e4937aSGao Xiang 		goto err_out;
60747e4937aSGao Xiang 
60847e4937aSGao Xiang 	/* preload all compressed pages (maybe downgrade role if necessary) */
609f57a3fe4SChao Yu 	if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
61047e4937aSGao Xiang 		cache_strategy = DELAYEDALLOC;
61147e4937aSGao Xiang 	else
61247e4937aSGao Xiang 		cache_strategy = DONTALLOC;
61347e4937aSGao Xiang 
614e3f78d5eSChao Yu 	preload_compressed_pages(clt, MNGD_MAPPING(sbi), cache_strategy);
61547e4937aSGao Xiang 
61647e4937aSGao Xiang hitted:
617dc76ea8cSGao Xiang 	/*
618dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
619dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
620dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
621dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
622dc76ea8cSGao Xiang 	 */
623dc76ea8cSGao Xiang 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
624dc76ea8cSGao Xiang 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
625dc76ea8cSGao Xiang 
62647e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
6278d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
62847e4937aSGao Xiang 		zero_user_segment(page, cur, end);
62947e4937aSGao Xiang 		goto next_part;
63047e4937aSGao Xiang 	}
63147e4937aSGao Xiang 
63247e4937aSGao Xiang 	/* let's derive page type */
63347e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
63447e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
63547e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
63647e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
63747e4937aSGao Xiang 
63847e4937aSGao Xiang 	if (cur)
63947e4937aSGao Xiang 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
64047e4937aSGao Xiang 
64147e4937aSGao Xiang retry:
64247e4937aSGao Xiang 	err = z_erofs_attach_page(clt, page, page_type);
6436aaa7b06SGao Xiang 	/* should allocate an additional short-lived page for pagevec */
64447e4937aSGao Xiang 	if (err == -EAGAIN) {
64547e4937aSGao Xiang 		struct page *const newpage =
646e3f78d5eSChao Yu 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
64747e4937aSGao Xiang 
6486aaa7b06SGao Xiang 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
64947e4937aSGao Xiang 		err = z_erofs_attach_page(clt, newpage,
65047e4937aSGao Xiang 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
6518d8a09b0SGao Xiang 		if (!err)
65247e4937aSGao Xiang 			goto retry;
65347e4937aSGao Xiang 	}
65447e4937aSGao Xiang 
6558d8a09b0SGao Xiang 	if (err)
65647e4937aSGao Xiang 		goto err_out;
65747e4937aSGao Xiang 
65847e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
65947e4937aSGao Xiang 
66047e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
66147e4937aSGao Xiang 
66247e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
66347e4937aSGao Xiang 	++spiltted;
66447e4937aSGao Xiang 	/* also update nr_pages */
66547e4937aSGao Xiang 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
66647e4937aSGao Xiang next_part:
66747e4937aSGao Xiang 	/* can be used for verification */
66847e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
66947e4937aSGao Xiang 
67047e4937aSGao Xiang 	end = cur;
67147e4937aSGao Xiang 	if (end > 0)
67247e4937aSGao Xiang 		goto repeat;
67347e4937aSGao Xiang 
67447e4937aSGao Xiang out:
67547e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
67647e4937aSGao Xiang 
6774f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
67847e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
67947e4937aSGao Xiang 	return err;
68047e4937aSGao Xiang 
68147e4937aSGao Xiang 	/* if some error occurred while processing this page */
68247e4937aSGao Xiang err_out:
68347e4937aSGao Xiang 	SetPageError(page);
68447e4937aSGao Xiang 	goto out;
68547e4937aSGao Xiang }
68647e4937aSGao Xiang 
687a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
688a4b1fab1SGao Xiang 				       bool sync, int bios)
68947e4937aSGao Xiang {
690a4b1fab1SGao Xiang 	/* wake up the caller thread for sync decompression */
691a4b1fab1SGao Xiang 	if (sync) {
69247e4937aSGao Xiang 		unsigned long flags;
69347e4937aSGao Xiang 
69447e4937aSGao Xiang 		spin_lock_irqsave(&io->u.wait.lock, flags);
69547e4937aSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
69647e4937aSGao Xiang 			wake_up_locked(&io->u.wait);
69747e4937aSGao Xiang 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
69847e4937aSGao Xiang 		return;
69947e4937aSGao Xiang 	}
70047e4937aSGao Xiang 
70147e4937aSGao Xiang 	if (!atomic_add_return(bios, &io->pending_bios))
70247e4937aSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
70347e4937aSGao Xiang }
70447e4937aSGao Xiang 
7056aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
7066aaa7b06SGao Xiang {
7076aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
7086aaa7b06SGao Xiang }
7096aaa7b06SGao Xiang 
7100c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
71147e4937aSGao Xiang {
712a4b1fab1SGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
713a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
71447e4937aSGao Xiang 	blk_status_t err = bio->bi_status;
71547e4937aSGao Xiang 	struct bio_vec *bvec;
71647e4937aSGao Xiang 	struct bvec_iter_all iter_all;
71747e4937aSGao Xiang 
71847e4937aSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
71947e4937aSGao Xiang 		struct page *page = bvec->bv_page;
72047e4937aSGao Xiang 
72147e4937aSGao Xiang 		DBG_BUGON(PageUptodate(page));
7226aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
72347e4937aSGao Xiang 
7248d8a09b0SGao Xiang 		if (err)
72547e4937aSGao Xiang 			SetPageError(page);
72647e4937aSGao Xiang 
727a4b1fab1SGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
728a4b1fab1SGao Xiang 			if (!err)
729a4b1fab1SGao Xiang 				SetPageUptodate(page);
73047e4937aSGao Xiang 			unlock_page(page);
73147e4937aSGao Xiang 		}
732a4b1fab1SGao Xiang 	}
733a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
73447e4937aSGao Xiang 	bio_put(bio);
73547e4937aSGao Xiang }
73647e4937aSGao Xiang 
73747e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
73847e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
73947e4937aSGao Xiang 				       struct list_head *pagepool)
74047e4937aSGao Xiang {
74147e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
74247e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
74347e4937aSGao Xiang 	struct z_erofs_pagevec_ctor ctor;
74447e4937aSGao Xiang 	unsigned int i, outputsize, llen, nr_pages;
74547e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
74647e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
74747e4937aSGao Xiang 
74847e4937aSGao Xiang 	enum z_erofs_page_type page_type;
74947e4937aSGao Xiang 	bool overlapped, partial;
75047e4937aSGao Xiang 	struct z_erofs_collection *cl;
75147e4937aSGao Xiang 	int err;
75247e4937aSGao Xiang 
75347e4937aSGao Xiang 	might_sleep();
75447e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
75547e4937aSGao Xiang 	DBG_BUGON(!READ_ONCE(cl->nr_pages));
75647e4937aSGao Xiang 
75747e4937aSGao Xiang 	mutex_lock(&cl->lock);
75847e4937aSGao Xiang 	nr_pages = cl->nr_pages;
75947e4937aSGao Xiang 
7608d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
76147e4937aSGao Xiang 		pages = pages_onstack;
76247e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
76347e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
76447e4937aSGao Xiang 		pages = z_pagemap_global;
76547e4937aSGao Xiang 	} else {
76647e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
76747e4937aSGao Xiang 
76847e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
76947e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
77047e4937aSGao Xiang 
77147e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
77247e4937aSGao Xiang 				       gfp_flags);
77347e4937aSGao Xiang 
77447e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
7758d8a09b0SGao Xiang 		if (!pages) {
77647e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
77747e4937aSGao Xiang 			pages = z_pagemap_global;
77847e4937aSGao Xiang 		}
77947e4937aSGao Xiang 	}
78047e4937aSGao Xiang 
78147e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
78247e4937aSGao Xiang 		pages[i] = NULL;
78347e4937aSGao Xiang 
78447e4937aSGao Xiang 	err = 0;
78547e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
78647e4937aSGao Xiang 				  cl->pagevec, 0);
78747e4937aSGao Xiang 
78847e4937aSGao Xiang 	for (i = 0; i < cl->vcnt; ++i) {
78947e4937aSGao Xiang 		unsigned int pagenr;
79047e4937aSGao Xiang 
79147e4937aSGao Xiang 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
79247e4937aSGao Xiang 
79347e4937aSGao Xiang 		/* all pages in pagevec ought to be valid */
79447e4937aSGao Xiang 		DBG_BUGON(!page);
7956aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
79647e4937aSGao Xiang 
7976aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
79847e4937aSGao Xiang 			continue;
79947e4937aSGao Xiang 
80047e4937aSGao Xiang 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
80147e4937aSGao Xiang 			pagenr = 0;
80247e4937aSGao Xiang 		else
80347e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
80447e4937aSGao Xiang 
80547e4937aSGao Xiang 		DBG_BUGON(pagenr >= nr_pages);
80647e4937aSGao Xiang 
80747e4937aSGao Xiang 		/*
80847e4937aSGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
80947e4937aSGao Xiang 		 * so here erroring out one multiref page.
81047e4937aSGao Xiang 		 */
8118d8a09b0SGao Xiang 		if (pages[pagenr]) {
81247e4937aSGao Xiang 			DBG_BUGON(1);
81347e4937aSGao Xiang 			SetPageError(pages[pagenr]);
81447e4937aSGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
81547e4937aSGao Xiang 			err = -EFSCORRUPTED;
81647e4937aSGao Xiang 		}
81747e4937aSGao Xiang 		pages[pagenr] = page;
81847e4937aSGao Xiang 	}
81947e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&ctor, true);
82047e4937aSGao Xiang 
82147e4937aSGao Xiang 	overlapped = false;
82247e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
82347e4937aSGao Xiang 
82447e4937aSGao Xiang 	for (i = 0; i < clusterpages; ++i) {
82547e4937aSGao Xiang 		unsigned int pagenr;
82647e4937aSGao Xiang 
82747e4937aSGao Xiang 		page = compressed_pages[i];
82847e4937aSGao Xiang 
82947e4937aSGao Xiang 		/* all compressed pages ought to be valid */
83047e4937aSGao Xiang 		DBG_BUGON(!page);
8316aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
83247e4937aSGao Xiang 
8336aaa7b06SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
83447e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
8358d8a09b0SGao Xiang 				if (!PageUptodate(page))
83647e4937aSGao Xiang 					err = -EIO;
83747e4937aSGao Xiang 				continue;
83847e4937aSGao Xiang 			}
83947e4937aSGao Xiang 
84047e4937aSGao Xiang 			/*
84147e4937aSGao Xiang 			 * only if non-head page can be selected
84247e4937aSGao Xiang 			 * for inplace decompression
84347e4937aSGao Xiang 			 */
84447e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
84547e4937aSGao Xiang 
84647e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
8478d8a09b0SGao Xiang 			if (pages[pagenr]) {
84847e4937aSGao Xiang 				DBG_BUGON(1);
84947e4937aSGao Xiang 				SetPageError(pages[pagenr]);
85047e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
85147e4937aSGao Xiang 				err = -EFSCORRUPTED;
85247e4937aSGao Xiang 			}
85347e4937aSGao Xiang 			pages[pagenr] = page;
85447e4937aSGao Xiang 
85547e4937aSGao Xiang 			overlapped = true;
85647e4937aSGao Xiang 		}
85747e4937aSGao Xiang 
8586aaa7b06SGao Xiang 		/* PG_error needs checking for all non-managed pages */
8598d8a09b0SGao Xiang 		if (PageError(page)) {
86047e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
86147e4937aSGao Xiang 			err = -EIO;
86247e4937aSGao Xiang 		}
86347e4937aSGao Xiang 	}
86447e4937aSGao Xiang 
8658d8a09b0SGao Xiang 	if (err)
86647e4937aSGao Xiang 		goto out;
86747e4937aSGao Xiang 
86847e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
86947e4937aSGao Xiang 	if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
87047e4937aSGao Xiang 		outputsize = llen;
87147e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
87247e4937aSGao Xiang 	} else {
87347e4937aSGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
87447e4937aSGao Xiang 		partial = true;
87547e4937aSGao Xiang 	}
87647e4937aSGao Xiang 
87747e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
87847e4937aSGao Xiang 					.sb = sb,
87947e4937aSGao Xiang 					.in = compressed_pages,
88047e4937aSGao Xiang 					.out = pages,
88147e4937aSGao Xiang 					.pageofs_out = cl->pageofs,
88247e4937aSGao Xiang 					.inputsize = PAGE_SIZE,
88347e4937aSGao Xiang 					.outputsize = outputsize,
88447e4937aSGao Xiang 					.alg = pcl->algorithmformat,
88547e4937aSGao Xiang 					.inplace_io = overlapped,
88647e4937aSGao Xiang 					.partial_decoding = partial
88747e4937aSGao Xiang 				 }, pagepool);
88847e4937aSGao Xiang 
88947e4937aSGao Xiang out:
89047e4937aSGao Xiang 	/* must handle all compressed pages before endding pages */
89147e4937aSGao Xiang 	for (i = 0; i < clusterpages; ++i) {
89247e4937aSGao Xiang 		page = compressed_pages[i];
89347e4937aSGao Xiang 
89447e4937aSGao Xiang 		if (erofs_page_is_managed(sbi, page))
89547e4937aSGao Xiang 			continue;
89647e4937aSGao Xiang 
8976aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
8986aaa7b06SGao Xiang 		(void)z_erofs_put_shortlivedpage(pagepool, page);
89947e4937aSGao Xiang 
90047e4937aSGao Xiang 		WRITE_ONCE(compressed_pages[i], NULL);
90147e4937aSGao Xiang 	}
90247e4937aSGao Xiang 
90347e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
90447e4937aSGao Xiang 		page = pages[i];
90547e4937aSGao Xiang 		if (!page)
90647e4937aSGao Xiang 			continue;
90747e4937aSGao Xiang 
9086aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
90947e4937aSGao Xiang 
9106aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
9116aaa7b06SGao Xiang 		if (z_erofs_put_shortlivedpage(pagepool, page))
91247e4937aSGao Xiang 			continue;
91347e4937aSGao Xiang 
9148d8a09b0SGao Xiang 		if (err < 0)
91547e4937aSGao Xiang 			SetPageError(page);
91647e4937aSGao Xiang 
91747e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
91847e4937aSGao Xiang 	}
91947e4937aSGao Xiang 
92047e4937aSGao Xiang 	if (pages == z_pagemap_global)
92147e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
9228d8a09b0SGao Xiang 	else if (pages != pages_onstack)
92347e4937aSGao Xiang 		kvfree(pages);
92447e4937aSGao Xiang 
92547e4937aSGao Xiang 	cl->nr_pages = 0;
92647e4937aSGao Xiang 	cl->vcnt = 0;
92747e4937aSGao Xiang 
92847e4937aSGao Xiang 	/* all cl locks MUST be taken before the following line */
92947e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
93047e4937aSGao Xiang 
93147e4937aSGao Xiang 	/* all cl locks SHOULD be released right now */
93247e4937aSGao Xiang 	mutex_unlock(&cl->lock);
93347e4937aSGao Xiang 
93447e4937aSGao Xiang 	z_erofs_collection_put(cl);
93547e4937aSGao Xiang 	return err;
93647e4937aSGao Xiang }
93747e4937aSGao Xiang 
9380c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
93947e4937aSGao Xiang 				     struct list_head *pagepool)
94047e4937aSGao Xiang {
94147e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
94247e4937aSGao Xiang 
94347e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
94447e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
94547e4937aSGao Xiang 
94647e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
94747e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
94847e4937aSGao Xiang 
94947e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
95047e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
95147e4937aSGao Xiang 
95247e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
95347e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
95447e4937aSGao Xiang 
955a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
95647e4937aSGao Xiang 	}
95747e4937aSGao Xiang }
95847e4937aSGao Xiang 
9590c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
96047e4937aSGao Xiang {
961a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
962a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
96347e4937aSGao Xiang 	LIST_HEAD(pagepool);
96447e4937aSGao Xiang 
965a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
9660c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
96747e4937aSGao Xiang 
96847e4937aSGao Xiang 	put_pages_list(&pagepool);
969a4b1fab1SGao Xiang 	kvfree(bgq);
97047e4937aSGao Xiang }
97147e4937aSGao Xiang 
97247e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
97347e4937aSGao Xiang 					       unsigned int nr,
97447e4937aSGao Xiang 					       struct list_head *pagepool,
97547e4937aSGao Xiang 					       struct address_space *mc,
97647e4937aSGao Xiang 					       gfp_t gfp)
97747e4937aSGao Xiang {
97847e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
97947e4937aSGao Xiang 	bool tocache = false;
98047e4937aSGao Xiang 
98147e4937aSGao Xiang 	struct address_space *mapping;
98247e4937aSGao Xiang 	struct page *oldpage, *page;
98347e4937aSGao Xiang 
98447e4937aSGao Xiang 	compressed_page_t t;
98547e4937aSGao Xiang 	int justfound;
98647e4937aSGao Xiang 
98747e4937aSGao Xiang repeat:
98847e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
98947e4937aSGao Xiang 	oldpage = page;
99047e4937aSGao Xiang 
99147e4937aSGao Xiang 	if (!page)
99247e4937aSGao Xiang 		goto out_allocpage;
99347e4937aSGao Xiang 
99447e4937aSGao Xiang 	/*
99547e4937aSGao Xiang 	 * the cached page has not been allocated and
99647e4937aSGao Xiang 	 * an placeholder is out there, prepare it now.
99747e4937aSGao Xiang 	 */
998bda17a45SGao Xiang 	if (page == PAGE_UNALLOCATED) {
99947e4937aSGao Xiang 		tocache = true;
100047e4937aSGao Xiang 		goto out_allocpage;
100147e4937aSGao Xiang 	}
100247e4937aSGao Xiang 
100347e4937aSGao Xiang 	/* process the target tagged pointer */
100447e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
100547e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
100647e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
100747e4937aSGao Xiang 
100847e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
100947e4937aSGao Xiang 
101047e4937aSGao Xiang 	/*
10116aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
101247e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
101347e4937aSGao Xiang 	 */
101447e4937aSGao Xiang 	if (mapping && mapping != mc)
101547e4937aSGao Xiang 		/* ought to be unmanaged pages */
101647e4937aSGao Xiang 		goto out;
101747e4937aSGao Xiang 
10186aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
10196aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
10206aaa7b06SGao Xiang 		goto out;
10216aaa7b06SGao Xiang 
102247e4937aSGao Xiang 	lock_page(page);
102347e4937aSGao Xiang 
102447e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
102547e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
102647e4937aSGao Xiang 
102747e4937aSGao Xiang 	/* the page is still in manage cache */
102847e4937aSGao Xiang 	if (page->mapping == mc) {
102947e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
103047e4937aSGao Xiang 
103147e4937aSGao Xiang 		ClearPageError(page);
103247e4937aSGao Xiang 		if (!PagePrivate(page)) {
103347e4937aSGao Xiang 			/*
103447e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
103547e4937aSGao Xiang 			 * the current restriction as well if
103647e4937aSGao Xiang 			 * the page is already in compressed_pages[].
103747e4937aSGao Xiang 			 */
103847e4937aSGao Xiang 			DBG_BUGON(!justfound);
103947e4937aSGao Xiang 
104047e4937aSGao Xiang 			justfound = 0;
104147e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
104247e4937aSGao Xiang 			SetPagePrivate(page);
104347e4937aSGao Xiang 		}
104447e4937aSGao Xiang 
104547e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
104647e4937aSGao Xiang 		if (PageUptodate(page)) {
104747e4937aSGao Xiang 			unlock_page(page);
104847e4937aSGao Xiang 			page = NULL;
104947e4937aSGao Xiang 		}
105047e4937aSGao Xiang 		goto out;
105147e4937aSGao Xiang 	}
105247e4937aSGao Xiang 
105347e4937aSGao Xiang 	/*
105447e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
105547e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
105647e4937aSGao Xiang 	 */
105747e4937aSGao Xiang 	DBG_BUGON(page->mapping);
105847e4937aSGao Xiang 	DBG_BUGON(!justfound);
105947e4937aSGao Xiang 
106047e4937aSGao Xiang 	tocache = true;
106147e4937aSGao Xiang 	unlock_page(page);
106247e4937aSGao Xiang 	put_page(page);
106347e4937aSGao Xiang out_allocpage:
10645ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
10655ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
10665ddcee1fSGao Xiang 		list_add(&page->lru, pagepool);
10675ddcee1fSGao Xiang 		cond_resched();
10685ddcee1fSGao Xiang 		goto repeat;
10695ddcee1fSGao Xiang 	}
1070a30573b3SGao Xiang 
1071bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1072bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1073bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1074bf225074SGao Xiang 		goto out;
1075a30573b3SGao Xiang 	}
1076bf225074SGao Xiang 	attach_page_private(page, pcl);
1077bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1078bf225074SGao Xiang 	put_page(page);
1079bf225074SGao Xiang 
108047e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
108147e4937aSGao Xiang 	return page;
108247e4937aSGao Xiang }
108347e4937aSGao Xiang 
1084a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1085a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1086a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
108747e4937aSGao Xiang {
1088a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
108947e4937aSGao Xiang 
1090a4b1fab1SGao Xiang 	if (fg && !*fg) {
1091a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1092a4b1fab1SGao Xiang 		if (!q) {
1093a4b1fab1SGao Xiang 			*fg = true;
1094a4b1fab1SGao Xiang 			goto fg_out;
109547e4937aSGao Xiang 		}
10960c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1097a4b1fab1SGao Xiang 	} else {
1098a4b1fab1SGao Xiang fg_out:
1099a4b1fab1SGao Xiang 		q = fgq;
1100a4b1fab1SGao Xiang 		init_waitqueue_head(&fgq->u.wait);
1101a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1102a4b1fab1SGao Xiang 	}
1103a4b1fab1SGao Xiang 	q->sb = sb;
1104a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1105a4b1fab1SGao Xiang 	return q;
110647e4937aSGao Xiang }
110747e4937aSGao Xiang 
110847e4937aSGao Xiang /* define decompression jobqueue types */
110947e4937aSGao Xiang enum {
111047e4937aSGao Xiang 	JQ_BYPASS,
111147e4937aSGao Xiang 	JQ_SUBMIT,
111247e4937aSGao Xiang 	NR_JOBQUEUES,
111347e4937aSGao Xiang };
111447e4937aSGao Xiang 
111547e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1116a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1117a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
111847e4937aSGao Xiang {
111947e4937aSGao Xiang 	/*
112047e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
112147e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
112247e4937aSGao Xiang 	 */
1123a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1124a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
112547e4937aSGao Xiang 
1126a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
112747e4937aSGao Xiang }
112847e4937aSGao Xiang 
112947e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
113047e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
113147e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
113247e4937aSGao Xiang {
113347e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
113447e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
113547e4937aSGao Xiang 
113647e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
113747e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
113847e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
113947e4937aSGao Xiang 
114047e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
114147e4937aSGao Xiang 
114247e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
114347e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
114447e4937aSGao Xiang 
114547e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
114647e4937aSGao Xiang }
114747e4937aSGao Xiang 
11481e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb,
11496ea5aad3SGao Xiang 				 struct z_erofs_decompress_frontend *f,
115047e4937aSGao Xiang 				 struct list_head *pagepool,
1151a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1152a4b1fab1SGao Xiang 				 bool *force_fg)
115347e4937aSGao Xiang {
1154bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
115547e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1156a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
115747e4937aSGao Xiang 	void *bi_private;
11586ea5aad3SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
115947e4937aSGao Xiang 	/* since bio will be NULL, no need to initialize last_index */
11603f649ab7SKees Cook 	pgoff_t last_index;
11611e4a2955SGao Xiang 	unsigned int nr_bios = 0;
11621e4a2955SGao Xiang 	struct bio *bio = NULL;
116347e4937aSGao Xiang 
1164a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1165a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1166a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
116747e4937aSGao Xiang 
116847e4937aSGao Xiang 	/* by default, all need io submission */
116947e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
117047e4937aSGao Xiang 
117147e4937aSGao Xiang 	do {
117247e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
11731e4a2955SGao Xiang 		pgoff_t cur, end;
11741e4a2955SGao Xiang 		unsigned int i = 0;
11751e4a2955SGao Xiang 		bool bypass = true;
117647e4937aSGao Xiang 
117747e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
117847e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
117947e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
118047e4937aSGao Xiang 
118147e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
118247e4937aSGao Xiang 
11831e4a2955SGao Xiang 		cur = pcl->obj.index;
11841e4a2955SGao Xiang 		end = cur + BIT(pcl->clusterbits);
118547e4937aSGao Xiang 
118647e4937aSGao Xiang 		/* close the main owned chain at first */
118747e4937aSGao Xiang 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
118847e4937aSGao Xiang 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
118947e4937aSGao Xiang 
11901e4a2955SGao Xiang 		do {
11911e4a2955SGao Xiang 			struct page *page;
119247e4937aSGao Xiang 
11931e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
119447e4937aSGao Xiang 							  MNGD_MAPPING(sbi),
119547e4937aSGao Xiang 							  GFP_NOFS);
11961e4a2955SGao Xiang 			if (!page)
11971e4a2955SGao Xiang 				continue;
119847e4937aSGao Xiang 
11991e4a2955SGao Xiang 			if (bio && cur != last_index + 1) {
120047e4937aSGao Xiang submit_bio_retry:
120194e4e153SGao Xiang 				submit_bio(bio);
120247e4937aSGao Xiang 				bio = NULL;
120347e4937aSGao Xiang 			}
120447e4937aSGao Xiang 
120547e4937aSGao Xiang 			if (!bio) {
1206a5c0b780SGao Xiang 				bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1207a5c0b780SGao Xiang 
12080c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1209a5c0b780SGao Xiang 				bio_set_dev(bio, sb->s_bdev);
12101e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1211a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1212a5c0b780SGao Xiang 				bio->bi_private = bi_private;
121394e4e153SGao Xiang 				bio->bi_opf = REQ_OP_READ;
12146ea5aad3SGao Xiang 				if (f->readahead)
12156ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
121647e4937aSGao Xiang 				++nr_bios;
121747e4937aSGao Xiang 			}
121847e4937aSGao Xiang 
12196c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
122047e4937aSGao Xiang 				goto submit_bio_retry;
122147e4937aSGao Xiang 
12221e4a2955SGao Xiang 			last_index = cur;
12231e4a2955SGao Xiang 			bypass = false;
12241e4a2955SGao Xiang 		} while (++cur < end);
122547e4937aSGao Xiang 
12261e4a2955SGao Xiang 		if (!bypass)
122747e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
122847e4937aSGao Xiang 		else
122947e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
123047e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
123147e4937aSGao Xiang 
123247e4937aSGao Xiang 	if (bio)
123394e4e153SGao Xiang 		submit_bio(bio);
123447e4937aSGao Xiang 
1235587a67b7SGao Xiang 	/*
1236587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1237587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1238587a67b7SGao Xiang 	 */
1239587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1240587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
12411e4a2955SGao Xiang 		return;
1242587a67b7SGao Xiang 	}
1243a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
124447e4937aSGao Xiang }
124547e4937aSGao Xiang 
12460c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb,
12476ea5aad3SGao Xiang 			     struct z_erofs_decompress_frontend *f,
12480c638f70SGao Xiang 			     struct list_head *pagepool, bool force_fg)
124947e4937aSGao Xiang {
1250a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
125147e4937aSGao Xiang 
12526ea5aad3SGao Xiang 	if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
125347e4937aSGao Xiang 		return;
12546ea5aad3SGao Xiang 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
125547e4937aSGao Xiang 
12560c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
12570c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
125847e4937aSGao Xiang 
125947e4937aSGao Xiang 	if (!force_fg)
126047e4937aSGao Xiang 		return;
126147e4937aSGao Xiang 
126247e4937aSGao Xiang 	/* wait until all bios are completed */
1263a93f8c36SGao Xiang 	io_wait_event(io[JQ_SUBMIT].u.wait,
126447e4937aSGao Xiang 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
126547e4937aSGao Xiang 
12660c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
12670c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
126847e4937aSGao Xiang }
126947e4937aSGao Xiang 
12700c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page)
127147e4937aSGao Xiang {
127247e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
127347e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
127447e4937aSGao Xiang 	int err;
127547e4937aSGao Xiang 	LIST_HEAD(pagepool);
127647e4937aSGao Xiang 
127747e4937aSGao Xiang 	trace_erofs_readpage(page, false);
127847e4937aSGao Xiang 
127947e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
128047e4937aSGao Xiang 
1281e3f78d5eSChao Yu 	err = z_erofs_do_read_page(&f, page);
128247e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
128347e4937aSGao Xiang 
128447e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
12856ea5aad3SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
128647e4937aSGao Xiang 
128747e4937aSGao Xiang 	if (err)
12884f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
128947e4937aSGao Xiang 
129047e4937aSGao Xiang 	if (f.map.mpage)
129147e4937aSGao Xiang 		put_page(f.map.mpage);
129247e4937aSGao Xiang 
129347e4937aSGao Xiang 	/* clean up the remaining free pages */
129447e4937aSGao Xiang 	put_pages_list(&pagepool);
129547e4937aSGao Xiang 	return err;
129647e4937aSGao Xiang }
129747e4937aSGao Xiang 
12980615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
129947e4937aSGao Xiang {
13000615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
130147e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
130247e4937aSGao Xiang 
1303bf9a123bSGao Xiang 	unsigned int nr_pages = readahead_count(rac);
1304bf9a123bSGao Xiang 	bool sync = (nr_pages <= sbi->ctx.max_sync_decompress_pages);
130547e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
13060615090cSMatthew Wilcox (Oracle) 	struct page *page, *head = NULL;
130747e4937aSGao Xiang 	LIST_HEAD(pagepool);
130847e4937aSGao Xiang 
1309bf9a123bSGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
131047e4937aSGao Xiang 
13116ea5aad3SGao Xiang 	f.readahead = true;
13120615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
131347e4937aSGao Xiang 
13140615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
131547e4937aSGao Xiang 		prefetchw(&page->flags);
131647e4937aSGao Xiang 
131747e4937aSGao Xiang 		/*
131847e4937aSGao Xiang 		 * A pure asynchronous readahead is indicated if
131947e4937aSGao Xiang 		 * a PG_readahead marked page is hitted at first.
132047e4937aSGao Xiang 		 * Let's also do asynchronous decompression for this case.
132147e4937aSGao Xiang 		 */
132247e4937aSGao Xiang 		sync &= !(PageReadahead(page) && !head);
132347e4937aSGao Xiang 
132447e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
132547e4937aSGao Xiang 		head = page;
132647e4937aSGao Xiang 	}
132747e4937aSGao Xiang 
132847e4937aSGao Xiang 	while (head) {
132947e4937aSGao Xiang 		struct page *page = head;
133047e4937aSGao Xiang 		int err;
133147e4937aSGao Xiang 
133247e4937aSGao Xiang 		/* traversal in reverse order */
133347e4937aSGao Xiang 		head = (void *)page_private(page);
133447e4937aSGao Xiang 
1335e3f78d5eSChao Yu 		err = z_erofs_do_read_page(&f, page);
1336a5876e24SGao Xiang 		if (err)
13374f761fa2SGao Xiang 			erofs_err(inode->i_sb,
13384f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
13394f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
134047e4937aSGao Xiang 		put_page(page);
134147e4937aSGao Xiang 	}
134247e4937aSGao Xiang 
134347e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
134447e4937aSGao Xiang 
13456ea5aad3SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
134647e4937aSGao Xiang 
134747e4937aSGao Xiang 	if (f.map.mpage)
134847e4937aSGao Xiang 		put_page(f.map.mpage);
134947e4937aSGao Xiang 
135047e4937aSGao Xiang 	/* clean up the remaining free pages */
135147e4937aSGao Xiang 	put_pages_list(&pagepool);
135247e4937aSGao Xiang }
135347e4937aSGao Xiang 
13540c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
13550c638f70SGao Xiang 	.readpage = z_erofs_readpage,
13560615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
135747e4937aSGao Xiang };
135847e4937aSGao Xiang 
1359