xref: /openbmc/linux/fs/erofs/zdata.c (revision 6c3e485e)
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 
13847e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
13947e4937aSGao Xiang 	bool backmost;
14047e4937aSGao Xiang 	erofs_off_t headoffset;
14147e4937aSGao Xiang };
14247e4937aSGao Xiang 
14347e4937aSGao Xiang #define COLLECTOR_INIT() { \
14447e4937aSGao Xiang 	.owned_head = Z_EROFS_PCLUSTER_TAIL, \
14547e4937aSGao Xiang 	.mode = COLLECT_PRIMARY_FOLLOWED }
14647e4937aSGao Xiang 
14747e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
14847e4937aSGao Xiang 	.inode = __i, .clt = COLLECTOR_INIT(), \
14947e4937aSGao Xiang 	.backmost = true, }
15047e4937aSGao Xiang 
15147e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
15247e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock);
15347e4937aSGao Xiang 
15447e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt,
15547e4937aSGao Xiang 				     struct address_space *mc,
156e3f78d5eSChao Yu 				     enum z_erofs_cache_alloctype type)
15747e4937aSGao Xiang {
15847e4937aSGao Xiang 	const struct z_erofs_pcluster *pcl = clt->pcl;
15947e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
16047e4937aSGao Xiang 	struct page **pages = clt->compressedpages;
16147e4937aSGao Xiang 	pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
16247e4937aSGao Xiang 	bool standalone = true;
16347e4937aSGao Xiang 
16447e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
16547e4937aSGao Xiang 		return;
16647e4937aSGao Xiang 
16747e4937aSGao Xiang 	for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
16847e4937aSGao Xiang 		struct page *page;
16947e4937aSGao Xiang 		compressed_page_t t;
17047e4937aSGao Xiang 
17147e4937aSGao Xiang 		/* the compressed page was loaded before */
17247e4937aSGao Xiang 		if (READ_ONCE(*pages))
17347e4937aSGao Xiang 			continue;
17447e4937aSGao Xiang 
17547e4937aSGao Xiang 		page = find_get_page(mc, index);
17647e4937aSGao Xiang 
17747e4937aSGao Xiang 		if (page) {
17847e4937aSGao Xiang 			t = tag_compressed_page_justfound(page);
17947e4937aSGao Xiang 		} else if (type == DELAYEDALLOC) {
18047e4937aSGao Xiang 			t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
18147e4937aSGao Xiang 		} else {	/* DONTALLOC */
18247e4937aSGao Xiang 			if (standalone)
18347e4937aSGao Xiang 				clt->compressedpages = pages;
18447e4937aSGao Xiang 			standalone = false;
18547e4937aSGao Xiang 			continue;
18647e4937aSGao Xiang 		}
18747e4937aSGao Xiang 
18847e4937aSGao Xiang 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
18947e4937aSGao Xiang 			continue;
19047e4937aSGao Xiang 
19147e4937aSGao Xiang 		if (page)
19247e4937aSGao Xiang 			put_page(page);
19347e4937aSGao Xiang 	}
19447e4937aSGao Xiang 
19547e4937aSGao Xiang 	if (standalone)		/* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
19647e4937aSGao Xiang 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
19747e4937aSGao Xiang }
19847e4937aSGao Xiang 
19947e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
20047e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
20147e4937aSGao Xiang 				       struct erofs_workgroup *grp)
20247e4937aSGao Xiang {
20347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
20447e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
20547e4937aSGao Xiang 	struct address_space *const mapping = MNGD_MAPPING(sbi);
20647e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
20747e4937aSGao Xiang 	int i;
20847e4937aSGao Xiang 
20947e4937aSGao Xiang 	/*
21047e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
21147e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
21247e4937aSGao Xiang 	 */
21347e4937aSGao Xiang 	for (i = 0; i < clusterpages; ++i) {
21447e4937aSGao Xiang 		struct page *page = pcl->compressed_pages[i];
21547e4937aSGao Xiang 
21647e4937aSGao Xiang 		if (!page)
21747e4937aSGao Xiang 			continue;
21847e4937aSGao Xiang 
21947e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
22047e4937aSGao Xiang 		if (!trylock_page(page))
22147e4937aSGao Xiang 			return -EBUSY;
22247e4937aSGao Xiang 
2238d8a09b0SGao Xiang 		if (page->mapping != mapping)
22447e4937aSGao Xiang 			continue;
22547e4937aSGao Xiang 
22647e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
22747e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
22847e4937aSGao Xiang 		set_page_private(page, 0);
22947e4937aSGao Xiang 		ClearPagePrivate(page);
23047e4937aSGao Xiang 
23147e4937aSGao Xiang 		unlock_page(page);
23247e4937aSGao Xiang 		put_page(page);
23347e4937aSGao Xiang 	}
23447e4937aSGao Xiang 	return 0;
23547e4937aSGao Xiang }
23647e4937aSGao Xiang 
23747e4937aSGao Xiang int erofs_try_to_free_cached_page(struct address_space *mapping,
23847e4937aSGao Xiang 				  struct page *page)
23947e4937aSGao Xiang {
24047e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
24147e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
24247e4937aSGao Xiang 	int ret = 0;	/* 0 - busy */
24347e4937aSGao Xiang 
24447e4937aSGao Xiang 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
24547e4937aSGao Xiang 		unsigned int i;
24647e4937aSGao Xiang 
24747e4937aSGao Xiang 		for (i = 0; i < clusterpages; ++i) {
24847e4937aSGao Xiang 			if (pcl->compressed_pages[i] == page) {
24947e4937aSGao Xiang 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
25047e4937aSGao Xiang 				ret = 1;
25147e4937aSGao Xiang 				break;
25247e4937aSGao Xiang 			}
25347e4937aSGao Xiang 		}
25447e4937aSGao Xiang 		erofs_workgroup_unfreeze(&pcl->obj, 1);
25547e4937aSGao Xiang 
25647e4937aSGao Xiang 		if (ret) {
25747e4937aSGao Xiang 			ClearPagePrivate(page);
25847e4937aSGao Xiang 			put_page(page);
25947e4937aSGao Xiang 		}
26047e4937aSGao Xiang 	}
26147e4937aSGao Xiang 	return ret;
26247e4937aSGao Xiang }
26347e4937aSGao Xiang 
26447e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
26599634bf3SGao Xiang static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
26647e4937aSGao Xiang 					  struct page *page)
26747e4937aSGao Xiang {
26847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = clt->pcl;
26947e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
27047e4937aSGao Xiang 
27147e4937aSGao Xiang 	while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
27247e4937aSGao Xiang 		if (!cmpxchg(clt->compressedpages++, NULL, page))
27347e4937aSGao Xiang 			return true;
27447e4937aSGao Xiang 	}
27547e4937aSGao Xiang 	return false;
27647e4937aSGao Xiang }
27747e4937aSGao Xiang 
27847e4937aSGao Xiang /* callers must be with collection lock held */
27947e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt,
28047e4937aSGao Xiang 			       struct page *page,
28147e4937aSGao Xiang 			       enum z_erofs_page_type type)
28247e4937aSGao Xiang {
28347e4937aSGao Xiang 	int ret;
28447e4937aSGao Xiang 	bool occupied;
28547e4937aSGao Xiang 
28647e4937aSGao Xiang 	/* give priority for inplaceio */
28747e4937aSGao Xiang 	if (clt->mode >= COLLECT_PRIMARY &&
28847e4937aSGao Xiang 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
28999634bf3SGao Xiang 	    z_erofs_try_inplace_io(clt, page))
29047e4937aSGao Xiang 		return 0;
29147e4937aSGao Xiang 
29247e4937aSGao Xiang 	ret = z_erofs_pagevec_enqueue(&clt->vector,
29347e4937aSGao Xiang 				      page, type, &occupied);
29447e4937aSGao Xiang 	clt->cl->vcnt += (unsigned int)ret;
29547e4937aSGao Xiang 
29647e4937aSGao Xiang 	return ret ? 0 : -EAGAIN;
29747e4937aSGao Xiang }
29847e4937aSGao Xiang 
29947e4937aSGao Xiang static enum z_erofs_collectmode
30047e4937aSGao Xiang try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
30147e4937aSGao Xiang 		      z_erofs_next_pcluster_t *owned_head)
30247e4937aSGao Xiang {
30347e4937aSGao Xiang 	/* let's claim these following types of pclusters */
30447e4937aSGao Xiang retry:
30547e4937aSGao Xiang 	if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
30647e4937aSGao Xiang 		/* type 1, nil pcluster */
30747e4937aSGao Xiang 		if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
30847e4937aSGao Xiang 			    *owned_head) != Z_EROFS_PCLUSTER_NIL)
30947e4937aSGao Xiang 			goto retry;
31047e4937aSGao Xiang 
31147e4937aSGao Xiang 		*owned_head = &pcl->next;
31247e4937aSGao Xiang 		/* lucky, I am the followee :) */
31347e4937aSGao Xiang 		return COLLECT_PRIMARY_FOLLOWED;
31447e4937aSGao Xiang 	} else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
31547e4937aSGao Xiang 		/*
31647e4937aSGao Xiang 		 * type 2, link to the end of a existing open chain,
31747e4937aSGao Xiang 		 * be careful that its submission itself is governed
31847e4937aSGao Xiang 		 * by the original owned chain.
31947e4937aSGao Xiang 		 */
32047e4937aSGao Xiang 		if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
32147e4937aSGao Xiang 			    *owned_head) != Z_EROFS_PCLUSTER_TAIL)
32247e4937aSGao Xiang 			goto retry;
32347e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
32447e4937aSGao Xiang 		return COLLECT_PRIMARY_HOOKED;
32547e4937aSGao Xiang 	}
32647e4937aSGao Xiang 	return COLLECT_PRIMARY;	/* :( better luck next time */
32747e4937aSGao Xiang }
32847e4937aSGao Xiang 
3299e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
33047e4937aSGao Xiang 				     struct inode *inode,
33147e4937aSGao Xiang 				     struct erofs_map_blocks *map)
33247e4937aSGao Xiang {
33364094a04SGao Xiang 	struct z_erofs_pcluster *pcl = clt->pcl;
33447e4937aSGao Xiang 	struct z_erofs_collection *cl;
33547e4937aSGao Xiang 	unsigned int length;
33647e4937aSGao Xiang 
33764094a04SGao Xiang 	/* to avoid unexpected loop formed by corrupted images */
33847e4937aSGao Xiang 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
33947e4937aSGao Xiang 		DBG_BUGON(1);
3409e579fc1SGao Xiang 		return -EFSCORRUPTED;
34147e4937aSGao Xiang 	}
34247e4937aSGao Xiang 
34347e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
3448d8a09b0SGao Xiang 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
34547e4937aSGao Xiang 		DBG_BUGON(1);
3469e579fc1SGao Xiang 		return -EFSCORRUPTED;
34747e4937aSGao Xiang 	}
34847e4937aSGao Xiang 
34947e4937aSGao Xiang 	length = READ_ONCE(pcl->length);
35047e4937aSGao Xiang 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
35147e4937aSGao Xiang 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
35247e4937aSGao Xiang 			DBG_BUGON(1);
3539e579fc1SGao Xiang 			return -EFSCORRUPTED;
35447e4937aSGao Xiang 		}
35547e4937aSGao Xiang 	} else {
35647e4937aSGao Xiang 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
35747e4937aSGao Xiang 
35847e4937aSGao Xiang 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
35947e4937aSGao Xiang 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
36047e4937aSGao Xiang 
36147e4937aSGao Xiang 		while (llen > length &&
36247e4937aSGao Xiang 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
36347e4937aSGao Xiang 			cpu_relax();
36447e4937aSGao Xiang 			length = READ_ONCE(pcl->length);
36547e4937aSGao Xiang 		}
36647e4937aSGao Xiang 	}
36747e4937aSGao Xiang 	mutex_lock(&cl->lock);
36847e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
36947e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
37047e4937aSGao Xiang 		clt->tailpcl = pcl;
37147e4937aSGao Xiang 	clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
37247e4937aSGao Xiang 	/* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
37347e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
37447e4937aSGao Xiang 		clt->tailpcl = NULL;
37547e4937aSGao Xiang 	clt->cl = cl;
3769e579fc1SGao Xiang 	return 0;
37747e4937aSGao Xiang }
37847e4937aSGao Xiang 
3799e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt,
38047e4937aSGao Xiang 				       struct inode *inode,
38147e4937aSGao Xiang 				       struct erofs_map_blocks *map)
38247e4937aSGao Xiang {
38347e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
38447e4937aSGao Xiang 	struct z_erofs_collection *cl;
38564094a04SGao Xiang 	struct erofs_workgroup *grp;
38647e4937aSGao Xiang 	int err;
38747e4937aSGao Xiang 
38847e4937aSGao Xiang 	/* no available workgroup, let's allocate one */
38947e4937aSGao Xiang 	pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
3908d8a09b0SGao Xiang 	if (!pcl)
3919e579fc1SGao Xiang 		return -ENOMEM;
39247e4937aSGao Xiang 
39364094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
39447e4937aSGao Xiang 	pcl->obj.index = map->m_pa >> PAGE_SHIFT;
39547e4937aSGao Xiang 
39647e4937aSGao Xiang 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
39747e4937aSGao Xiang 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
39847e4937aSGao Xiang 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
39947e4937aSGao Xiang 
40047e4937aSGao Xiang 	if (map->m_flags & EROFS_MAP_ZIPPED)
40147e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
40247e4937aSGao Xiang 	else
40347e4937aSGao Xiang 		pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
40447e4937aSGao Xiang 
405a5876e24SGao Xiang 	pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
40647e4937aSGao Xiang 	pcl->clusterbits -= PAGE_SHIFT;
40747e4937aSGao Xiang 
40847e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
40947e4937aSGao Xiang 	pcl->next = clt->owned_head;
41047e4937aSGao Xiang 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
41147e4937aSGao Xiang 
41247e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
41364094a04SGao Xiang 
41464094a04SGao Xiang 	/* must be cleaned before freeing to slab */
41564094a04SGao Xiang 	DBG_BUGON(cl->nr_pages);
41664094a04SGao Xiang 	DBG_BUGON(cl->vcnt);
41764094a04SGao Xiang 
41847e4937aSGao Xiang 	cl->pageofs = map->m_la & ~PAGE_MASK;
41947e4937aSGao Xiang 
42047e4937aSGao Xiang 	/*
42147e4937aSGao Xiang 	 * lock all primary followed works before visible to others
42247e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
42347e4937aSGao Xiang 	 */
42464094a04SGao Xiang 	DBG_BUGON(!mutex_trylock(&cl->lock));
42547e4937aSGao Xiang 
42664094a04SGao Xiang 	grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
42764094a04SGao Xiang 	if (IS_ERR(grp)) {
42864094a04SGao Xiang 		err = PTR_ERR(grp);
42964094a04SGao Xiang 		goto err_out;
43064094a04SGao Xiang 	}
43164094a04SGao Xiang 
43264094a04SGao Xiang 	if (grp != &pcl->obj) {
43364094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
43464094a04SGao Xiang 		err = -EEXIST;
43564094a04SGao Xiang 		goto err_out;
43647e4937aSGao Xiang 	}
43747e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
43847e4937aSGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
43947e4937aSGao Xiang 		clt->tailpcl = pcl;
44047e4937aSGao Xiang 	clt->owned_head = &pcl->next;
44147e4937aSGao Xiang 	clt->pcl = pcl;
44247e4937aSGao Xiang 	clt->cl = cl;
4439e579fc1SGao Xiang 	return 0;
44464094a04SGao Xiang 
44564094a04SGao Xiang err_out:
44664094a04SGao Xiang 	mutex_unlock(&cl->lock);
44764094a04SGao Xiang 	kmem_cache_free(pcluster_cachep, pcl);
44864094a04SGao Xiang 	return err;
44947e4937aSGao Xiang }
45047e4937aSGao Xiang 
45147e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt,
45247e4937aSGao Xiang 				   struct inode *inode,
45347e4937aSGao Xiang 				   struct erofs_map_blocks *map)
45447e4937aSGao Xiang {
45564094a04SGao Xiang 	struct erofs_workgroup *grp;
4569e579fc1SGao Xiang 	int ret;
45747e4937aSGao Xiang 
45847e4937aSGao Xiang 	DBG_BUGON(clt->cl);
45947e4937aSGao Xiang 
46047e4937aSGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
46147e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
46247e4937aSGao Xiang 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
46347e4937aSGao Xiang 
46447e4937aSGao Xiang 	if (!PAGE_ALIGNED(map->m_pa)) {
46547e4937aSGao Xiang 		DBG_BUGON(1);
46647e4937aSGao Xiang 		return -EINVAL;
46747e4937aSGao Xiang 	}
46847e4937aSGao Xiang 
46964094a04SGao Xiang 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
47064094a04SGao Xiang 	if (grp) {
47164094a04SGao Xiang 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
47264094a04SGao Xiang 	} else {
4739e579fc1SGao Xiang 		ret = z_erofs_register_collection(clt, inode, map);
47447e4937aSGao Xiang 
47564094a04SGao Xiang 		if (!ret)
47664094a04SGao Xiang 			goto out;
47764094a04SGao Xiang 		if (ret != -EEXIST)
4789e579fc1SGao Xiang 			return ret;
47964094a04SGao Xiang 	}
48047e4937aSGao Xiang 
48164094a04SGao Xiang 	ret = z_erofs_lookup_collection(clt, inode, map);
48264094a04SGao Xiang 	if (ret) {
48364094a04SGao Xiang 		erofs_workgroup_put(&clt->pcl->obj);
48464094a04SGao Xiang 		return ret;
48564094a04SGao Xiang 	}
48664094a04SGao Xiang 
48764094a04SGao Xiang out:
48847e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
4899e579fc1SGao Xiang 				  clt->cl->pagevec, clt->cl->vcnt);
49047e4937aSGao Xiang 
49147e4937aSGao Xiang 	clt->compressedpages = clt->pcl->compressed_pages;
49247e4937aSGao Xiang 	if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
49347e4937aSGao Xiang 		clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
49447e4937aSGao Xiang 	return 0;
49547e4937aSGao Xiang }
49647e4937aSGao Xiang 
49747e4937aSGao Xiang /*
49847e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
49947e4937aSGao Xiang  * only after a RCU grace period.
50047e4937aSGao Xiang  */
50147e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
50247e4937aSGao Xiang {
50347e4937aSGao Xiang 	struct z_erofs_collection *const cl =
50447e4937aSGao Xiang 		container_of(head, struct z_erofs_collection, rcu);
50547e4937aSGao Xiang 
50647e4937aSGao Xiang 	kmem_cache_free(pcluster_cachep,
50747e4937aSGao Xiang 			container_of(cl, struct z_erofs_pcluster,
50847e4937aSGao Xiang 				     primary_collection));
50947e4937aSGao Xiang }
51047e4937aSGao Xiang 
51147e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
51247e4937aSGao Xiang {
51347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
51447e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
51547e4937aSGao Xiang 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
51647e4937aSGao Xiang 
51747e4937aSGao Xiang 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
51847e4937aSGao Xiang }
51947e4937aSGao Xiang 
52047e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl)
52147e4937aSGao Xiang {
52247e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
52347e4937aSGao Xiang 		container_of(cl, struct z_erofs_pcluster, primary_collection);
52447e4937aSGao Xiang 
52547e4937aSGao Xiang 	erofs_workgroup_put(&pcl->obj);
52647e4937aSGao Xiang }
52747e4937aSGao Xiang 
52847e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt)
52947e4937aSGao Xiang {
53047e4937aSGao Xiang 	struct z_erofs_collection *cl = clt->cl;
53147e4937aSGao Xiang 
53247e4937aSGao Xiang 	if (!cl)
53347e4937aSGao Xiang 		return false;
53447e4937aSGao Xiang 
53547e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
53647e4937aSGao Xiang 	mutex_unlock(&cl->lock);
53747e4937aSGao Xiang 
53847e4937aSGao Xiang 	/*
53947e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
54047e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
54147e4937aSGao Xiang 	 */
54247e4937aSGao Xiang 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
54347e4937aSGao Xiang 		z_erofs_collection_put(cl);
54447e4937aSGao Xiang 
54547e4937aSGao Xiang 	clt->cl = NULL;
54647e4937aSGao Xiang 	return true;
54747e4937aSGao Xiang }
54847e4937aSGao Xiang 
54947e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
55047e4937aSGao Xiang 				       unsigned int cachestrategy,
55147e4937aSGao Xiang 				       erofs_off_t la)
55247e4937aSGao Xiang {
55347e4937aSGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
55447e4937aSGao Xiang 		return false;
55547e4937aSGao Xiang 
55647e4937aSGao Xiang 	if (fe->backmost)
55747e4937aSGao Xiang 		return true;
55847e4937aSGao Xiang 
55947e4937aSGao Xiang 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
56047e4937aSGao Xiang 		la < fe->headoffset;
56147e4937aSGao Xiang }
56247e4937aSGao Xiang 
56347e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
564e3f78d5eSChao Yu 				struct page *page)
56547e4937aSGao Xiang {
56647e4937aSGao Xiang 	struct inode *const inode = fe->inode;
567bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
56847e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
56947e4937aSGao Xiang 	struct z_erofs_collector *const clt = &fe->clt;
57047e4937aSGao Xiang 	const loff_t offset = page_offset(page);
571dc76ea8cSGao Xiang 	bool tight = true;
57247e4937aSGao Xiang 
57347e4937aSGao Xiang 	enum z_erofs_cache_alloctype cache_strategy;
57447e4937aSGao Xiang 	enum z_erofs_page_type page_type;
57547e4937aSGao Xiang 	unsigned int cur, end, spiltted, index;
57647e4937aSGao Xiang 	int err = 0;
57747e4937aSGao Xiang 
57847e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
57947e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
58047e4937aSGao Xiang 
58147e4937aSGao Xiang 	spiltted = 0;
58247e4937aSGao Xiang 	end = PAGE_SIZE;
58347e4937aSGao Xiang repeat:
58447e4937aSGao Xiang 	cur = end - 1;
58547e4937aSGao Xiang 
58647e4937aSGao Xiang 	/* lucky, within the range of the current map_blocks */
58747e4937aSGao Xiang 	if (offset + cur >= map->m_la &&
58847e4937aSGao Xiang 	    offset + cur < map->m_la + map->m_llen) {
58947e4937aSGao Xiang 		/* didn't get a valid collection previously (very rare) */
59047e4937aSGao Xiang 		if (!clt->cl)
59147e4937aSGao Xiang 			goto restart_now;
59247e4937aSGao Xiang 		goto hitted;
59347e4937aSGao Xiang 	}
59447e4937aSGao Xiang 
59547e4937aSGao Xiang 	/* go ahead the next map_blocks */
5964f761fa2SGao Xiang 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
59747e4937aSGao Xiang 
59847e4937aSGao Xiang 	if (z_erofs_collector_end(clt))
59947e4937aSGao Xiang 		fe->backmost = false;
60047e4937aSGao Xiang 
60147e4937aSGao Xiang 	map->m_la = offset + cur;
60247e4937aSGao Xiang 	map->m_llen = 0;
60347e4937aSGao Xiang 	err = z_erofs_map_blocks_iter(inode, map, 0);
6048d8a09b0SGao Xiang 	if (err)
60547e4937aSGao Xiang 		goto err_out;
60647e4937aSGao Xiang 
60747e4937aSGao Xiang restart_now:
6088d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED))
60947e4937aSGao Xiang 		goto hitted;
61047e4937aSGao Xiang 
61147e4937aSGao Xiang 	err = z_erofs_collector_begin(clt, inode, map);
6128d8a09b0SGao Xiang 	if (err)
61347e4937aSGao Xiang 		goto err_out;
61447e4937aSGao Xiang 
61547e4937aSGao Xiang 	/* preload all compressed pages (maybe downgrade role if necessary) */
616f57a3fe4SChao Yu 	if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
61747e4937aSGao Xiang 		cache_strategy = DELAYEDALLOC;
61847e4937aSGao Xiang 	else
61947e4937aSGao Xiang 		cache_strategy = DONTALLOC;
62047e4937aSGao Xiang 
621e3f78d5eSChao Yu 	preload_compressed_pages(clt, MNGD_MAPPING(sbi), cache_strategy);
62247e4937aSGao Xiang 
62347e4937aSGao Xiang hitted:
624dc76ea8cSGao Xiang 	/*
625dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
626dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
627dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
628dc76ea8cSGao Xiang 	 * for inplace I/O or pagevec (should be processed in strict order.)
629dc76ea8cSGao Xiang 	 */
630dc76ea8cSGao Xiang 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
631dc76ea8cSGao Xiang 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
632dc76ea8cSGao Xiang 
63347e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
6348d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
63547e4937aSGao Xiang 		zero_user_segment(page, cur, end);
63647e4937aSGao Xiang 		goto next_part;
63747e4937aSGao Xiang 	}
63847e4937aSGao Xiang 
63947e4937aSGao Xiang 	/* let's derive page type */
64047e4937aSGao Xiang 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
64147e4937aSGao Xiang 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
64247e4937aSGao Xiang 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
64347e4937aSGao Xiang 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
64447e4937aSGao Xiang 
64547e4937aSGao Xiang 	if (cur)
64647e4937aSGao Xiang 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
64747e4937aSGao Xiang 
64847e4937aSGao Xiang retry:
64947e4937aSGao Xiang 	err = z_erofs_attach_page(clt, page, page_type);
65047e4937aSGao Xiang 	/* should allocate an additional staging page for pagevec */
65147e4937aSGao Xiang 	if (err == -EAGAIN) {
65247e4937aSGao Xiang 		struct page *const newpage =
653e3f78d5eSChao Yu 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
65447e4937aSGao Xiang 
6555ddcee1fSGao Xiang 		newpage->mapping = Z_EROFS_MAPPING_STAGING;
65647e4937aSGao Xiang 		err = z_erofs_attach_page(clt, newpage,
65747e4937aSGao Xiang 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
6588d8a09b0SGao Xiang 		if (!err)
65947e4937aSGao Xiang 			goto retry;
66047e4937aSGao Xiang 	}
66147e4937aSGao Xiang 
6628d8a09b0SGao Xiang 	if (err)
66347e4937aSGao Xiang 		goto err_out;
66447e4937aSGao Xiang 
66547e4937aSGao Xiang 	index = page->index - (map->m_la >> PAGE_SHIFT);
66647e4937aSGao Xiang 
66747e4937aSGao Xiang 	z_erofs_onlinepage_fixup(page, index, true);
66847e4937aSGao Xiang 
66947e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
67047e4937aSGao Xiang 	++spiltted;
67147e4937aSGao Xiang 	/* also update nr_pages */
67247e4937aSGao Xiang 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
67347e4937aSGao Xiang next_part:
67447e4937aSGao Xiang 	/* can be used for verification */
67547e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
67647e4937aSGao Xiang 
67747e4937aSGao Xiang 	end = cur;
67847e4937aSGao Xiang 	if (end > 0)
67947e4937aSGao Xiang 		goto repeat;
68047e4937aSGao Xiang 
68147e4937aSGao Xiang out:
68247e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
68347e4937aSGao Xiang 
6844f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
68547e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
68647e4937aSGao Xiang 	return err;
68747e4937aSGao Xiang 
68847e4937aSGao Xiang 	/* if some error occurred while processing this page */
68947e4937aSGao Xiang err_out:
69047e4937aSGao Xiang 	SetPageError(page);
69147e4937aSGao Xiang 	goto out;
69247e4937aSGao Xiang }
69347e4937aSGao Xiang 
694a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
695a4b1fab1SGao Xiang 				       bool sync, int bios)
69647e4937aSGao Xiang {
697a4b1fab1SGao Xiang 	/* wake up the caller thread for sync decompression */
698a4b1fab1SGao Xiang 	if (sync) {
69947e4937aSGao Xiang 		unsigned long flags;
70047e4937aSGao Xiang 
70147e4937aSGao Xiang 		spin_lock_irqsave(&io->u.wait.lock, flags);
70247e4937aSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
70347e4937aSGao Xiang 			wake_up_locked(&io->u.wait);
70447e4937aSGao Xiang 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
70547e4937aSGao Xiang 		return;
70647e4937aSGao Xiang 	}
70747e4937aSGao Xiang 
70847e4937aSGao Xiang 	if (!atomic_add_return(bios, &io->pending_bios))
70947e4937aSGao Xiang 		queue_work(z_erofs_workqueue, &io->u.work);
71047e4937aSGao Xiang }
71147e4937aSGao Xiang 
7120c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
71347e4937aSGao Xiang {
714a4b1fab1SGao Xiang 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
715a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
71647e4937aSGao Xiang 	blk_status_t err = bio->bi_status;
71747e4937aSGao Xiang 	struct bio_vec *bvec;
71847e4937aSGao Xiang 	struct bvec_iter_all iter_all;
71947e4937aSGao Xiang 
72047e4937aSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
72147e4937aSGao Xiang 		struct page *page = bvec->bv_page;
72247e4937aSGao Xiang 
72347e4937aSGao Xiang 		DBG_BUGON(PageUptodate(page));
72447e4937aSGao Xiang 		DBG_BUGON(!page->mapping);
72547e4937aSGao Xiang 
7268d8a09b0SGao Xiang 		if (err)
72747e4937aSGao Xiang 			SetPageError(page);
72847e4937aSGao Xiang 
729a4b1fab1SGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
730a4b1fab1SGao Xiang 			if (!err)
731a4b1fab1SGao Xiang 				SetPageUptodate(page);
73247e4937aSGao Xiang 			unlock_page(page);
73347e4937aSGao Xiang 		}
734a4b1fab1SGao Xiang 	}
735a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
73647e4937aSGao Xiang 	bio_put(bio);
73747e4937aSGao Xiang }
73847e4937aSGao Xiang 
73947e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb,
74047e4937aSGao Xiang 				       struct z_erofs_pcluster *pcl,
74147e4937aSGao Xiang 				       struct list_head *pagepool)
74247e4937aSGao Xiang {
74347e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
74447e4937aSGao Xiang 	const unsigned int clusterpages = BIT(pcl->clusterbits);
74547e4937aSGao Xiang 	struct z_erofs_pagevec_ctor ctor;
74647e4937aSGao Xiang 	unsigned int i, outputsize, llen, nr_pages;
74747e4937aSGao Xiang 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
74847e4937aSGao Xiang 	struct page **pages, **compressed_pages, *page;
74947e4937aSGao Xiang 
75047e4937aSGao Xiang 	enum z_erofs_page_type page_type;
75147e4937aSGao Xiang 	bool overlapped, partial;
75247e4937aSGao Xiang 	struct z_erofs_collection *cl;
75347e4937aSGao Xiang 	int err;
75447e4937aSGao Xiang 
75547e4937aSGao Xiang 	might_sleep();
75647e4937aSGao Xiang 	cl = z_erofs_primarycollection(pcl);
75747e4937aSGao Xiang 	DBG_BUGON(!READ_ONCE(cl->nr_pages));
75847e4937aSGao Xiang 
75947e4937aSGao Xiang 	mutex_lock(&cl->lock);
76047e4937aSGao Xiang 	nr_pages = cl->nr_pages;
76147e4937aSGao Xiang 
7628d8a09b0SGao Xiang 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
76347e4937aSGao Xiang 		pages = pages_onstack;
76447e4937aSGao Xiang 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
76547e4937aSGao Xiang 		   mutex_trylock(&z_pagemap_global_lock)) {
76647e4937aSGao Xiang 		pages = z_pagemap_global;
76747e4937aSGao Xiang 	} else {
76847e4937aSGao Xiang 		gfp_t gfp_flags = GFP_KERNEL;
76947e4937aSGao Xiang 
77047e4937aSGao Xiang 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
77147e4937aSGao Xiang 			gfp_flags |= __GFP_NOFAIL;
77247e4937aSGao Xiang 
77347e4937aSGao Xiang 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
77447e4937aSGao Xiang 				       gfp_flags);
77547e4937aSGao Xiang 
77647e4937aSGao Xiang 		/* fallback to global pagemap for the lowmem scenario */
7778d8a09b0SGao Xiang 		if (!pages) {
77847e4937aSGao Xiang 			mutex_lock(&z_pagemap_global_lock);
77947e4937aSGao Xiang 			pages = z_pagemap_global;
78047e4937aSGao Xiang 		}
78147e4937aSGao Xiang 	}
78247e4937aSGao Xiang 
78347e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i)
78447e4937aSGao Xiang 		pages[i] = NULL;
78547e4937aSGao Xiang 
78647e4937aSGao Xiang 	err = 0;
78747e4937aSGao Xiang 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
78847e4937aSGao Xiang 				  cl->pagevec, 0);
78947e4937aSGao Xiang 
79047e4937aSGao Xiang 	for (i = 0; i < cl->vcnt; ++i) {
79147e4937aSGao Xiang 		unsigned int pagenr;
79247e4937aSGao Xiang 
79347e4937aSGao Xiang 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
79447e4937aSGao Xiang 
79547e4937aSGao Xiang 		/* all pages in pagevec ought to be valid */
79647e4937aSGao Xiang 		DBG_BUGON(!page);
79747e4937aSGao Xiang 		DBG_BUGON(!page->mapping);
79847e4937aSGao Xiang 
79947e4937aSGao Xiang 		if (z_erofs_put_stagingpage(pagepool, page))
80047e4937aSGao Xiang 			continue;
80147e4937aSGao Xiang 
80247e4937aSGao Xiang 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
80347e4937aSGao Xiang 			pagenr = 0;
80447e4937aSGao Xiang 		else
80547e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
80647e4937aSGao Xiang 
80747e4937aSGao Xiang 		DBG_BUGON(pagenr >= nr_pages);
80847e4937aSGao Xiang 
80947e4937aSGao Xiang 		/*
81047e4937aSGao Xiang 		 * currently EROFS doesn't support multiref(dedup),
81147e4937aSGao Xiang 		 * so here erroring out one multiref page.
81247e4937aSGao Xiang 		 */
8138d8a09b0SGao Xiang 		if (pages[pagenr]) {
81447e4937aSGao Xiang 			DBG_BUGON(1);
81547e4937aSGao Xiang 			SetPageError(pages[pagenr]);
81647e4937aSGao Xiang 			z_erofs_onlinepage_endio(pages[pagenr]);
81747e4937aSGao Xiang 			err = -EFSCORRUPTED;
81847e4937aSGao Xiang 		}
81947e4937aSGao Xiang 		pages[pagenr] = page;
82047e4937aSGao Xiang 	}
82147e4937aSGao Xiang 	z_erofs_pagevec_ctor_exit(&ctor, true);
82247e4937aSGao Xiang 
82347e4937aSGao Xiang 	overlapped = false;
82447e4937aSGao Xiang 	compressed_pages = pcl->compressed_pages;
82547e4937aSGao Xiang 
82647e4937aSGao Xiang 	for (i = 0; i < clusterpages; ++i) {
82747e4937aSGao Xiang 		unsigned int pagenr;
82847e4937aSGao Xiang 
82947e4937aSGao Xiang 		page = compressed_pages[i];
83047e4937aSGao Xiang 
83147e4937aSGao Xiang 		/* all compressed pages ought to be valid */
83247e4937aSGao Xiang 		DBG_BUGON(!page);
83347e4937aSGao Xiang 		DBG_BUGON(!page->mapping);
83447e4937aSGao Xiang 
83547e4937aSGao Xiang 		if (!z_erofs_page_is_staging(page)) {
83647e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page)) {
8378d8a09b0SGao Xiang 				if (!PageUptodate(page))
83847e4937aSGao Xiang 					err = -EIO;
83947e4937aSGao Xiang 				continue;
84047e4937aSGao Xiang 			}
84147e4937aSGao Xiang 
84247e4937aSGao Xiang 			/*
84347e4937aSGao Xiang 			 * only if non-head page can be selected
84447e4937aSGao Xiang 			 * for inplace decompression
84547e4937aSGao Xiang 			 */
84647e4937aSGao Xiang 			pagenr = z_erofs_onlinepage_index(page);
84747e4937aSGao Xiang 
84847e4937aSGao Xiang 			DBG_BUGON(pagenr >= nr_pages);
8498d8a09b0SGao Xiang 			if (pages[pagenr]) {
85047e4937aSGao Xiang 				DBG_BUGON(1);
85147e4937aSGao Xiang 				SetPageError(pages[pagenr]);
85247e4937aSGao Xiang 				z_erofs_onlinepage_endio(pages[pagenr]);
85347e4937aSGao Xiang 				err = -EFSCORRUPTED;
85447e4937aSGao Xiang 			}
85547e4937aSGao Xiang 			pages[pagenr] = page;
85647e4937aSGao Xiang 
85747e4937aSGao Xiang 			overlapped = true;
85847e4937aSGao Xiang 		}
85947e4937aSGao Xiang 
86047e4937aSGao Xiang 		/* PG_error needs checking for inplaced and staging pages */
8618d8a09b0SGao Xiang 		if (PageError(page)) {
86247e4937aSGao Xiang 			DBG_BUGON(PageUptodate(page));
86347e4937aSGao Xiang 			err = -EIO;
86447e4937aSGao Xiang 		}
86547e4937aSGao Xiang 	}
86647e4937aSGao Xiang 
8678d8a09b0SGao Xiang 	if (err)
86847e4937aSGao Xiang 		goto out;
86947e4937aSGao Xiang 
87047e4937aSGao Xiang 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
87147e4937aSGao Xiang 	if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
87247e4937aSGao Xiang 		outputsize = llen;
87347e4937aSGao Xiang 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
87447e4937aSGao Xiang 	} else {
87547e4937aSGao Xiang 		outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
87647e4937aSGao Xiang 		partial = true;
87747e4937aSGao Xiang 	}
87847e4937aSGao Xiang 
87947e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
88047e4937aSGao Xiang 					.sb = sb,
88147e4937aSGao Xiang 					.in = compressed_pages,
88247e4937aSGao Xiang 					.out = pages,
88347e4937aSGao Xiang 					.pageofs_out = cl->pageofs,
88447e4937aSGao Xiang 					.inputsize = PAGE_SIZE,
88547e4937aSGao Xiang 					.outputsize = outputsize,
88647e4937aSGao Xiang 					.alg = pcl->algorithmformat,
88747e4937aSGao Xiang 					.inplace_io = overlapped,
88847e4937aSGao Xiang 					.partial_decoding = partial
88947e4937aSGao Xiang 				 }, pagepool);
89047e4937aSGao Xiang 
89147e4937aSGao Xiang out:
89247e4937aSGao Xiang 	/* must handle all compressed pages before endding pages */
89347e4937aSGao Xiang 	for (i = 0; i < clusterpages; ++i) {
89447e4937aSGao Xiang 		page = compressed_pages[i];
89547e4937aSGao Xiang 
89647e4937aSGao Xiang 		if (erofs_page_is_managed(sbi, page))
89747e4937aSGao Xiang 			continue;
89847e4937aSGao Xiang 
89947e4937aSGao Xiang 		/* recycle all individual staging pages */
90047e4937aSGao Xiang 		(void)z_erofs_put_stagingpage(pagepool, page);
90147e4937aSGao Xiang 
90247e4937aSGao Xiang 		WRITE_ONCE(compressed_pages[i], NULL);
90347e4937aSGao Xiang 	}
90447e4937aSGao Xiang 
90547e4937aSGao Xiang 	for (i = 0; i < nr_pages; ++i) {
90647e4937aSGao Xiang 		page = pages[i];
90747e4937aSGao Xiang 		if (!page)
90847e4937aSGao Xiang 			continue;
90947e4937aSGao Xiang 
91047e4937aSGao Xiang 		DBG_BUGON(!page->mapping);
91147e4937aSGao Xiang 
91247e4937aSGao Xiang 		/* recycle all individual staging pages */
91347e4937aSGao Xiang 		if (z_erofs_put_stagingpage(pagepool, page))
91447e4937aSGao Xiang 			continue;
91547e4937aSGao Xiang 
9168d8a09b0SGao Xiang 		if (err < 0)
91747e4937aSGao Xiang 			SetPageError(page);
91847e4937aSGao Xiang 
91947e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
92047e4937aSGao Xiang 	}
92147e4937aSGao Xiang 
92247e4937aSGao Xiang 	if (pages == z_pagemap_global)
92347e4937aSGao Xiang 		mutex_unlock(&z_pagemap_global_lock);
9248d8a09b0SGao Xiang 	else if (pages != pages_onstack)
92547e4937aSGao Xiang 		kvfree(pages);
92647e4937aSGao Xiang 
92747e4937aSGao Xiang 	cl->nr_pages = 0;
92847e4937aSGao Xiang 	cl->vcnt = 0;
92947e4937aSGao Xiang 
93047e4937aSGao Xiang 	/* all cl locks MUST be taken before the following line */
93147e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
93247e4937aSGao Xiang 
93347e4937aSGao Xiang 	/* all cl locks SHOULD be released right now */
93447e4937aSGao Xiang 	mutex_unlock(&cl->lock);
93547e4937aSGao Xiang 
93647e4937aSGao Xiang 	z_erofs_collection_put(cl);
93747e4937aSGao Xiang 	return err;
93847e4937aSGao Xiang }
93947e4937aSGao Xiang 
9400c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
94147e4937aSGao Xiang 				     struct list_head *pagepool)
94247e4937aSGao Xiang {
94347e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
94447e4937aSGao Xiang 
94547e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
94647e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
94747e4937aSGao Xiang 
94847e4937aSGao Xiang 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
94947e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
95047e4937aSGao Xiang 
95147e4937aSGao Xiang 		/* no possible that 'owned' equals NULL */
95247e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
95347e4937aSGao Xiang 
95447e4937aSGao Xiang 		pcl = container_of(owned, struct z_erofs_pcluster, next);
95547e4937aSGao Xiang 		owned = READ_ONCE(pcl->next);
95647e4937aSGao Xiang 
957a4b1fab1SGao Xiang 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
95847e4937aSGao Xiang 	}
95947e4937aSGao Xiang }
96047e4937aSGao Xiang 
9610c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
96247e4937aSGao Xiang {
963a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
964a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
96547e4937aSGao Xiang 	LIST_HEAD(pagepool);
96647e4937aSGao Xiang 
967a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
9680c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
96947e4937aSGao Xiang 
97047e4937aSGao Xiang 	put_pages_list(&pagepool);
971a4b1fab1SGao Xiang 	kvfree(bgq);
97247e4937aSGao Xiang }
97347e4937aSGao Xiang 
97447e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
97547e4937aSGao Xiang 					       unsigned int nr,
97647e4937aSGao Xiang 					       struct list_head *pagepool,
97747e4937aSGao Xiang 					       struct address_space *mc,
97847e4937aSGao Xiang 					       gfp_t gfp)
97947e4937aSGao Xiang {
98047e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
98147e4937aSGao Xiang 	bool tocache = false;
98247e4937aSGao Xiang 
98347e4937aSGao Xiang 	struct address_space *mapping;
98447e4937aSGao Xiang 	struct page *oldpage, *page;
98547e4937aSGao Xiang 
98647e4937aSGao Xiang 	compressed_page_t t;
98747e4937aSGao Xiang 	int justfound;
98847e4937aSGao Xiang 
98947e4937aSGao Xiang repeat:
99047e4937aSGao Xiang 	page = READ_ONCE(pcl->compressed_pages[nr]);
99147e4937aSGao Xiang 	oldpage = page;
99247e4937aSGao Xiang 
99347e4937aSGao Xiang 	if (!page)
99447e4937aSGao Xiang 		goto out_allocpage;
99547e4937aSGao Xiang 
99647e4937aSGao Xiang 	/*
99747e4937aSGao Xiang 	 * the cached page has not been allocated and
99847e4937aSGao Xiang 	 * an placeholder is out there, prepare it now.
99947e4937aSGao Xiang 	 */
1000bda17a45SGao Xiang 	if (page == PAGE_UNALLOCATED) {
100147e4937aSGao Xiang 		tocache = true;
100247e4937aSGao Xiang 		goto out_allocpage;
100347e4937aSGao Xiang 	}
100447e4937aSGao Xiang 
100547e4937aSGao Xiang 	/* process the target tagged pointer */
100647e4937aSGao Xiang 	t = tagptr_init(compressed_page_t, page);
100747e4937aSGao Xiang 	justfound = tagptr_unfold_tags(t);
100847e4937aSGao Xiang 	page = tagptr_unfold_ptr(t);
100947e4937aSGao Xiang 
101047e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
101147e4937aSGao Xiang 
101247e4937aSGao Xiang 	/*
101347e4937aSGao Xiang 	 * unmanaged (file) pages are all locked solidly,
101447e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
101547e4937aSGao Xiang 	 */
101647e4937aSGao Xiang 	if (mapping && mapping != mc)
101747e4937aSGao Xiang 		/* ought to be unmanaged pages */
101847e4937aSGao Xiang 		goto out;
101947e4937aSGao Xiang 
102047e4937aSGao Xiang 	lock_page(page);
102147e4937aSGao Xiang 
102247e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
102347e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
102447e4937aSGao Xiang 
102547e4937aSGao Xiang 	/* the page is still in manage cache */
102647e4937aSGao Xiang 	if (page->mapping == mc) {
102747e4937aSGao Xiang 		WRITE_ONCE(pcl->compressed_pages[nr], page);
102847e4937aSGao Xiang 
102947e4937aSGao Xiang 		ClearPageError(page);
103047e4937aSGao Xiang 		if (!PagePrivate(page)) {
103147e4937aSGao Xiang 			/*
103247e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
103347e4937aSGao Xiang 			 * the current restriction as well if
103447e4937aSGao Xiang 			 * the page is already in compressed_pages[].
103547e4937aSGao Xiang 			 */
103647e4937aSGao Xiang 			DBG_BUGON(!justfound);
103747e4937aSGao Xiang 
103847e4937aSGao Xiang 			justfound = 0;
103947e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
104047e4937aSGao Xiang 			SetPagePrivate(page);
104147e4937aSGao Xiang 		}
104247e4937aSGao Xiang 
104347e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
104447e4937aSGao Xiang 		if (PageUptodate(page)) {
104547e4937aSGao Xiang 			unlock_page(page);
104647e4937aSGao Xiang 			page = NULL;
104747e4937aSGao Xiang 		}
104847e4937aSGao Xiang 		goto out;
104947e4937aSGao Xiang 	}
105047e4937aSGao Xiang 
105147e4937aSGao Xiang 	/*
105247e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
105347e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
105447e4937aSGao Xiang 	 */
105547e4937aSGao Xiang 	DBG_BUGON(page->mapping);
105647e4937aSGao Xiang 	DBG_BUGON(!justfound);
105747e4937aSGao Xiang 
105847e4937aSGao Xiang 	tocache = true;
105947e4937aSGao Xiang 	unlock_page(page);
106047e4937aSGao Xiang 	put_page(page);
106147e4937aSGao Xiang out_allocpage:
10625ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
10635ddcee1fSGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
10645ddcee1fSGao Xiang 		/* non-LRU / non-movable temporary page is needed */
106547e4937aSGao Xiang 		page->mapping = Z_EROFS_MAPPING_STAGING;
10665ddcee1fSGao Xiang 		tocache = false;
106747e4937aSGao Xiang 	}
106847e4937aSGao Xiang 
10695ddcee1fSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
10705ddcee1fSGao Xiang 		if (tocache) {
10715ddcee1fSGao Xiang 			/* since it added to managed cache successfully */
10725ddcee1fSGao Xiang 			unlock_page(page);
10735ddcee1fSGao Xiang 			put_page(page);
10745ddcee1fSGao Xiang 		} else {
10755ddcee1fSGao Xiang 			list_add(&page->lru, pagepool);
10765ddcee1fSGao Xiang 		}
10775ddcee1fSGao Xiang 		cond_resched();
10785ddcee1fSGao Xiang 		goto repeat;
10795ddcee1fSGao Xiang 	}
108047e4937aSGao Xiang 	set_page_private(page, (unsigned long)pcl);
108147e4937aSGao Xiang 	SetPagePrivate(page);
108247e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
108347e4937aSGao Xiang 	return page;
108447e4937aSGao Xiang }
108547e4937aSGao Xiang 
1086a4b1fab1SGao Xiang static struct z_erofs_decompressqueue *
1087a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb,
1088a4b1fab1SGao Xiang 	      struct z_erofs_decompressqueue *fgq, bool *fg)
108947e4937aSGao Xiang {
1090a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
109147e4937aSGao Xiang 
1092a4b1fab1SGao Xiang 	if (fg && !*fg) {
1093a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1094a4b1fab1SGao Xiang 		if (!q) {
1095a4b1fab1SGao Xiang 			*fg = true;
1096a4b1fab1SGao Xiang 			goto fg_out;
109747e4937aSGao Xiang 		}
10980c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1099a4b1fab1SGao Xiang 	} else {
1100a4b1fab1SGao Xiang fg_out:
1101a4b1fab1SGao Xiang 		q = fgq;
1102a4b1fab1SGao Xiang 		init_waitqueue_head(&fgq->u.wait);
1103a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
1104a4b1fab1SGao Xiang 	}
1105a4b1fab1SGao Xiang 	q->sb = sb;
1106a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1107a4b1fab1SGao Xiang 	return q;
110847e4937aSGao Xiang }
110947e4937aSGao Xiang 
111047e4937aSGao Xiang /* define decompression jobqueue types */
111147e4937aSGao Xiang enum {
111247e4937aSGao Xiang 	JQ_BYPASS,
111347e4937aSGao Xiang 	JQ_SUBMIT,
111447e4937aSGao Xiang 	NR_JOBQUEUES,
111547e4937aSGao Xiang };
111647e4937aSGao Xiang 
111747e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb,
1118a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *q[],
1119a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
112047e4937aSGao Xiang {
112147e4937aSGao Xiang 	/*
112247e4937aSGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
112347e4937aSGao Xiang 	 * no need to read from device for all pclusters in this queue.
112447e4937aSGao Xiang 	 */
1125a4b1fab1SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1126a4b1fab1SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
112747e4937aSGao Xiang 
1128a4b1fab1SGao Xiang 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
112947e4937aSGao Xiang }
113047e4937aSGao Xiang 
113147e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
113247e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
113347e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
113447e4937aSGao Xiang {
113547e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
113647e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
113747e4937aSGao Xiang 
113847e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
113947e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
114047e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
114147e4937aSGao Xiang 
114247e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
114347e4937aSGao Xiang 
114447e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
114547e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
114647e4937aSGao Xiang 
114747e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
114847e4937aSGao Xiang }
114947e4937aSGao Xiang 
11501e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb,
115147e4937aSGao Xiang 				 z_erofs_next_pcluster_t owned_head,
115247e4937aSGao Xiang 				 struct list_head *pagepool,
1153a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1154a4b1fab1SGao Xiang 				 bool *force_fg)
115547e4937aSGao Xiang {
1156bda17a45SGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
115747e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1158a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
115947e4937aSGao Xiang 	void *bi_private;
116047e4937aSGao Xiang 	/* since bio will be NULL, no need to initialize last_index */
11613f649ab7SKees Cook 	pgoff_t last_index;
11621e4a2955SGao Xiang 	unsigned int nr_bios = 0;
11631e4a2955SGao Xiang 	struct bio *bio = NULL;
116447e4937aSGao Xiang 
1165a4b1fab1SGao Xiang 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1166a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1167a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
116847e4937aSGao Xiang 
116947e4937aSGao Xiang 	/* by default, all need io submission */
117047e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
117147e4937aSGao Xiang 
117247e4937aSGao Xiang 	do {
117347e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
11741e4a2955SGao Xiang 		pgoff_t cur, end;
11751e4a2955SGao Xiang 		unsigned int i = 0;
11761e4a2955SGao Xiang 		bool bypass = true;
117747e4937aSGao Xiang 
117847e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
117947e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
118047e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
118147e4937aSGao Xiang 
118247e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
118347e4937aSGao Xiang 
11841e4a2955SGao Xiang 		cur = pcl->obj.index;
11851e4a2955SGao Xiang 		end = cur + BIT(pcl->clusterbits);
118647e4937aSGao Xiang 
118747e4937aSGao Xiang 		/* close the main owned chain at first */
118847e4937aSGao Xiang 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
118947e4937aSGao Xiang 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
119047e4937aSGao Xiang 
11911e4a2955SGao Xiang 		do {
11921e4a2955SGao Xiang 			struct page *page;
119347e4937aSGao Xiang 
11941e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
119547e4937aSGao Xiang 							  MNGD_MAPPING(sbi),
119647e4937aSGao Xiang 							  GFP_NOFS);
11971e4a2955SGao Xiang 			if (!page)
11981e4a2955SGao Xiang 				continue;
119947e4937aSGao Xiang 
12001e4a2955SGao Xiang 			if (bio && cur != last_index + 1) {
120147e4937aSGao Xiang submit_bio_retry:
120294e4e153SGao Xiang 				submit_bio(bio);
120347e4937aSGao Xiang 				bio = NULL;
120447e4937aSGao Xiang 			}
120547e4937aSGao Xiang 
120647e4937aSGao Xiang 			if (!bio) {
1207a5c0b780SGao Xiang 				bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1208a5c0b780SGao Xiang 
12090c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1210a5c0b780SGao Xiang 				bio_set_dev(bio, sb->s_bdev);
12111e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1212a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1213a5c0b780SGao Xiang 				bio->bi_private = bi_private;
121494e4e153SGao Xiang 				bio->bi_opf = REQ_OP_READ;
121547e4937aSGao Xiang 				++nr_bios;
121647e4937aSGao Xiang 			}
121747e4937aSGao Xiang 
12186c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
121947e4937aSGao Xiang 				goto submit_bio_retry;
122047e4937aSGao Xiang 
12211e4a2955SGao Xiang 			last_index = cur;
12221e4a2955SGao Xiang 			bypass = false;
12231e4a2955SGao Xiang 		} while (++cur < end);
122447e4937aSGao Xiang 
12251e4a2955SGao Xiang 		if (!bypass)
122647e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
122747e4937aSGao Xiang 		else
122847e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
122947e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
123047e4937aSGao Xiang 
123147e4937aSGao Xiang 	if (bio)
123294e4e153SGao Xiang 		submit_bio(bio);
123347e4937aSGao Xiang 
1234587a67b7SGao Xiang 	/*
1235587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1236587a67b7SGao Xiang 	 * don't issue workqueue for decompression but drop it directly instead.
1237587a67b7SGao Xiang 	 */
1238587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1239587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
12401e4a2955SGao Xiang 		return;
1241587a67b7SGao Xiang 	}
1242a4b1fab1SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
124347e4937aSGao Xiang }
124447e4937aSGao Xiang 
12450c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb,
124647e4937aSGao Xiang 			     struct z_erofs_collector *clt,
12470c638f70SGao Xiang 			     struct list_head *pagepool, bool force_fg)
124847e4937aSGao Xiang {
1249a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
125047e4937aSGao Xiang 
12511e4a2955SGao Xiang 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
125247e4937aSGao Xiang 		return;
12531e4a2955SGao Xiang 	z_erofs_submit_queue(sb, clt->owned_head, pagepool, io, &force_fg);
125447e4937aSGao Xiang 
12550c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
12560c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
125747e4937aSGao Xiang 
125847e4937aSGao Xiang 	if (!force_fg)
125947e4937aSGao Xiang 		return;
126047e4937aSGao Xiang 
126147e4937aSGao Xiang 	/* wait until all bios are completed */
1262a93f8c36SGao Xiang 	io_wait_event(io[JQ_SUBMIT].u.wait,
126347e4937aSGao Xiang 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
126447e4937aSGao Xiang 
12650c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
12660c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
126747e4937aSGao Xiang }
126847e4937aSGao Xiang 
12690c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page)
127047e4937aSGao Xiang {
127147e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
127247e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
127347e4937aSGao Xiang 	int err;
127447e4937aSGao Xiang 	LIST_HEAD(pagepool);
127547e4937aSGao Xiang 
127647e4937aSGao Xiang 	trace_erofs_readpage(page, false);
127747e4937aSGao Xiang 
127847e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
127947e4937aSGao Xiang 
1280e3f78d5eSChao Yu 	err = z_erofs_do_read_page(&f, page);
128147e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
128247e4937aSGao Xiang 
128347e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
12840c638f70SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, true);
128547e4937aSGao Xiang 
128647e4937aSGao Xiang 	if (err)
12874f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
128847e4937aSGao Xiang 
128947e4937aSGao Xiang 	if (f.map.mpage)
129047e4937aSGao Xiang 		put_page(f.map.mpage);
129147e4937aSGao Xiang 
129247e4937aSGao Xiang 	/* clean up the remaining free pages */
129347e4937aSGao Xiang 	put_pages_list(&pagepool);
129447e4937aSGao Xiang 	return err;
129547e4937aSGao Xiang }
129647e4937aSGao Xiang 
129747e4937aSGao Xiang static bool should_decompress_synchronously(struct erofs_sb_info *sbi,
129847e4937aSGao Xiang 					    unsigned int nr)
129947e4937aSGao Xiang {
1300f57a3fe4SChao Yu 	return nr <= sbi->ctx.max_sync_decompress_pages;
130147e4937aSGao Xiang }
130247e4937aSGao Xiang 
13030615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
130447e4937aSGao Xiang {
13050615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
130647e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
130747e4937aSGao Xiang 
13080615090cSMatthew Wilcox (Oracle) 	bool sync = should_decompress_synchronously(sbi, readahead_count(rac));
130947e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
13100615090cSMatthew Wilcox (Oracle) 	struct page *page, *head = NULL;
131147e4937aSGao Xiang 	LIST_HEAD(pagepool);
131247e4937aSGao Xiang 
13130615090cSMatthew Wilcox (Oracle) 	trace_erofs_readpages(inode, readahead_index(rac),
13140615090cSMatthew Wilcox (Oracle) 			readahead_count(rac), false);
131547e4937aSGao Xiang 
13160615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
131747e4937aSGao Xiang 
13180615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
131947e4937aSGao Xiang 		prefetchw(&page->flags);
132047e4937aSGao Xiang 
132147e4937aSGao Xiang 		/*
132247e4937aSGao Xiang 		 * A pure asynchronous readahead is indicated if
132347e4937aSGao Xiang 		 * a PG_readahead marked page is hitted at first.
132447e4937aSGao Xiang 		 * Let's also do asynchronous decompression for this case.
132547e4937aSGao Xiang 		 */
132647e4937aSGao Xiang 		sync &= !(PageReadahead(page) && !head);
132747e4937aSGao Xiang 
132847e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
132947e4937aSGao Xiang 		head = page;
133047e4937aSGao Xiang 	}
133147e4937aSGao Xiang 
133247e4937aSGao Xiang 	while (head) {
133347e4937aSGao Xiang 		struct page *page = head;
133447e4937aSGao Xiang 		int err;
133547e4937aSGao Xiang 
133647e4937aSGao Xiang 		/* traversal in reverse order */
133747e4937aSGao Xiang 		head = (void *)page_private(page);
133847e4937aSGao Xiang 
1339e3f78d5eSChao Yu 		err = z_erofs_do_read_page(&f, page);
1340a5876e24SGao Xiang 		if (err)
13414f761fa2SGao Xiang 			erofs_err(inode->i_sb,
13424f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
13434f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
134447e4937aSGao Xiang 		put_page(page);
134547e4937aSGao Xiang 	}
134647e4937aSGao Xiang 
134747e4937aSGao Xiang 	(void)z_erofs_collector_end(&f.clt);
134847e4937aSGao Xiang 
13490c638f70SGao Xiang 	z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, sync);
135047e4937aSGao Xiang 
135147e4937aSGao Xiang 	if (f.map.mpage)
135247e4937aSGao Xiang 		put_page(f.map.mpage);
135347e4937aSGao Xiang 
135447e4937aSGao Xiang 	/* clean up the remaining free pages */
135547e4937aSGao Xiang 	put_pages_list(&pagepool);
135647e4937aSGao Xiang }
135747e4937aSGao Xiang 
13580c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
13590c638f70SGao Xiang 	.readpage = z_erofs_readpage,
13600615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
136147e4937aSGao Xiang };
136247e4937aSGao Xiang 
1363