xref: /openbmc/linux/fs/erofs/zdata.c (revision 9a05c6a8)
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/
506a304cdSGao Xiang  * Copyright (C) 2022 Alibaba Cloud
647e4937aSGao Xiang  */
747e4937aSGao Xiang #include "compress.h"
899486c51SChristoph Hellwig #include <linux/psi.h>
93fffb589SSandeep Dhavale #include <linux/cpuhotplug.h>
1047e4937aSGao Xiang #include <trace/events/erofs.h>
1147e4937aSGao Xiang 
12a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_MAX_PAGES	(Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
13a9a94d93SGao Xiang #define Z_EROFS_INLINE_BVECS		2
14a9a94d93SGao Xiang 
15a9a94d93SGao Xiang /*
16a9a94d93SGao Xiang  * let's leave a type here in case of introducing
17a9a94d93SGao Xiang  * another tagged pointer later.
18a9a94d93SGao Xiang  */
19a9a94d93SGao Xiang typedef void *z_erofs_next_pcluster_t;
20a9a94d93SGao Xiang 
21a9a94d93SGao Xiang struct z_erofs_bvec {
22a9a94d93SGao Xiang 	struct page *page;
23a9a94d93SGao Xiang 	int offset;
24a9a94d93SGao Xiang 	unsigned int end;
25a9a94d93SGao Xiang };
26a9a94d93SGao Xiang 
27a9a94d93SGao Xiang #define __Z_EROFS_BVSET(name, total) \
28a9a94d93SGao Xiang struct name { \
29a9a94d93SGao Xiang 	/* point to the next page which contains the following bvecs */ \
30a9a94d93SGao Xiang 	struct page *nextpage; \
31a9a94d93SGao Xiang 	struct z_erofs_bvec bvec[total]; \
32a9a94d93SGao Xiang }
33a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset,);
34a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
35a9a94d93SGao Xiang 
36a9a94d93SGao Xiang /*
37a9a94d93SGao Xiang  * Structure fields follow one of the following exclusion rules.
38a9a94d93SGao Xiang  *
39a9a94d93SGao Xiang  * I: Modifiable by initialization/destruction paths and read-only
40a9a94d93SGao Xiang  *    for everyone else;
41a9a94d93SGao Xiang  *
42a9a94d93SGao Xiang  * L: Field should be protected by the pcluster lock;
43a9a94d93SGao Xiang  *
44a9a94d93SGao Xiang  * A: Field should be accessed / updated in atomic for parallelized code.
45a9a94d93SGao Xiang  */
46a9a94d93SGao Xiang struct z_erofs_pcluster {
47a9a94d93SGao Xiang 	struct erofs_workgroup obj;
48a9a94d93SGao Xiang 	struct mutex lock;
49a9a94d93SGao Xiang 
50a9a94d93SGao Xiang 	/* A: point to next chained pcluster or TAILs */
51a9a94d93SGao Xiang 	z_erofs_next_pcluster_t next;
52a9a94d93SGao Xiang 
53a9a94d93SGao Xiang 	/* L: the maximum decompression size of this round */
54a9a94d93SGao Xiang 	unsigned int length;
55a9a94d93SGao Xiang 
56a9a94d93SGao Xiang 	/* L: total number of bvecs */
57a9a94d93SGao Xiang 	unsigned int vcnt;
58a9a94d93SGao Xiang 
59a9a94d93SGao Xiang 	/* I: page offset of start position of decompression */
60a9a94d93SGao Xiang 	unsigned short pageofs_out;
61a9a94d93SGao Xiang 
62a9a94d93SGao Xiang 	/* I: page offset of inline compressed data */
63a9a94d93SGao Xiang 	unsigned short pageofs_in;
64a9a94d93SGao Xiang 
65a9a94d93SGao Xiang 	union {
66a9a94d93SGao Xiang 		/* L: inline a certain number of bvec for bootstrap */
67a9a94d93SGao Xiang 		struct z_erofs_bvset_inline bvset;
68a9a94d93SGao Xiang 
69a9a94d93SGao Xiang 		/* I: can be used to free the pcluster by RCU. */
70a9a94d93SGao Xiang 		struct rcu_head rcu;
71a9a94d93SGao Xiang 	};
72a9a94d93SGao Xiang 
73a9a94d93SGao Xiang 	union {
74a9a94d93SGao Xiang 		/* I: physical cluster size in pages */
75a9a94d93SGao Xiang 		unsigned short pclusterpages;
76a9a94d93SGao Xiang 
77a9a94d93SGao Xiang 		/* I: tailpacking inline compressed size */
78a9a94d93SGao Xiang 		unsigned short tailpacking_size;
79a9a94d93SGao Xiang 	};
80a9a94d93SGao Xiang 
81a9a94d93SGao Xiang 	/* I: compression algorithm format */
82a9a94d93SGao Xiang 	unsigned char algorithmformat;
83a9a94d93SGao Xiang 
84a9a94d93SGao Xiang 	/* L: whether partial decompression or not */
85a9a94d93SGao Xiang 	bool partial;
86a9a94d93SGao Xiang 
87a9a94d93SGao Xiang 	/* L: indicate several pageofs_outs or not */
88a9a94d93SGao Xiang 	bool multibases;
89a9a94d93SGao Xiang 
90a9a94d93SGao Xiang 	/* A: compressed bvecs (can be cached or inplaced pages) */
91a9a94d93SGao Xiang 	struct z_erofs_bvec compressed_bvecs[];
92a9a94d93SGao Xiang };
93a9a94d93SGao Xiang 
94967c28b2SGao Xiang /* the end of a chain of pclusters */
9543d86ec9SGao Xiang #define Z_EROFS_PCLUSTER_TAIL           ((void *) 0x700 + POISON_POINTER_DELTA)
96a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_NIL            (NULL)
97a9a94d93SGao Xiang 
98a9a94d93SGao Xiang struct z_erofs_decompressqueue {
99a9a94d93SGao Xiang 	struct super_block *sb;
100a9a94d93SGao Xiang 	atomic_t pending_bios;
101a9a94d93SGao Xiang 	z_erofs_next_pcluster_t head;
102a9a94d93SGao Xiang 
103a9a94d93SGao Xiang 	union {
104a9a94d93SGao Xiang 		struct completion done;
105a9a94d93SGao Xiang 		struct work_struct work;
1063fffb589SSandeep Dhavale 		struct kthread_work kthread_work;
107a9a94d93SGao Xiang 	} u;
108a9a94d93SGao Xiang 	bool eio, sync;
109a9a94d93SGao Xiang };
110a9a94d93SGao Xiang 
111a9a94d93SGao Xiang static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
112a9a94d93SGao Xiang {
113a9a94d93SGao Xiang 	return !pcl->obj.index;
114a9a94d93SGao Xiang }
115a9a94d93SGao Xiang 
116a9a94d93SGao Xiang static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
117a9a94d93SGao Xiang {
118a9a94d93SGao Xiang 	if (z_erofs_is_inline_pcluster(pcl))
119a9a94d93SGao Xiang 		return 1;
120a9a94d93SGao Xiang 	return pcl->pclusterpages;
121a9a94d93SGao Xiang }
122a9a94d93SGao Xiang 
123a9a94d93SGao Xiang /*
124a9a94d93SGao Xiang  * bit 30: I/O error occurred on this page
125a9a94d93SGao Xiang  * bit 0 - 29: remaining parts to complete this page
126a9a94d93SGao Xiang  */
127a9a94d93SGao Xiang #define Z_EROFS_PAGE_EIO			(1 << 30)
128a9a94d93SGao Xiang 
129a9a94d93SGao Xiang static inline void z_erofs_onlinepage_init(struct page *page)
130a9a94d93SGao Xiang {
131a9a94d93SGao Xiang 	union {
132a9a94d93SGao Xiang 		atomic_t o;
133a9a94d93SGao Xiang 		unsigned long v;
134a9a94d93SGao Xiang 	} u = { .o = ATOMIC_INIT(1) };
135a9a94d93SGao Xiang 
136a9a94d93SGao Xiang 	set_page_private(page, u.v);
137a9a94d93SGao Xiang 	smp_wmb();
138a9a94d93SGao Xiang 	SetPagePrivate(page);
139a9a94d93SGao Xiang }
140a9a94d93SGao Xiang 
141a9a94d93SGao Xiang static inline void z_erofs_onlinepage_split(struct page *page)
142a9a94d93SGao Xiang {
143a9a94d93SGao Xiang 	atomic_inc((atomic_t *)&page->private);
144a9a94d93SGao Xiang }
145a9a94d93SGao Xiang 
146*9a05c6a8SGao Xiang static void z_erofs_onlinepage_endio(struct page *page, int err)
147a9a94d93SGao Xiang {
148*9a05c6a8SGao Xiang 	int orig, v;
149*9a05c6a8SGao Xiang 
150*9a05c6a8SGao Xiang 	DBG_BUGON(!PagePrivate(page));
151a9a94d93SGao Xiang 
152a9a94d93SGao Xiang 	do {
153a9a94d93SGao Xiang 		orig = atomic_read((atomic_t *)&page->private);
154*9a05c6a8SGao Xiang 		v = (orig - 1) | (err ? Z_EROFS_PAGE_EIO : 0);
155*9a05c6a8SGao Xiang 	} while (atomic_cmpxchg((atomic_t *)&page->private, orig, v) != orig);
156a9a94d93SGao Xiang 
157a9a94d93SGao Xiang 	if (!(v & ~Z_EROFS_PAGE_EIO)) {
158a9a94d93SGao Xiang 		set_page_private(page, 0);
159a9a94d93SGao Xiang 		ClearPagePrivate(page);
160a9a94d93SGao Xiang 		if (!(v & Z_EROFS_PAGE_EIO))
161a9a94d93SGao Xiang 			SetPageUptodate(page);
162a9a94d93SGao Xiang 		unlock_page(page);
163a9a94d93SGao Xiang 	}
164a9a94d93SGao Xiang }
165a9a94d93SGao Xiang 
166a9a94d93SGao Xiang #define Z_EROFS_ONSTACK_PAGES		32
167a9a94d93SGao Xiang 
16847e4937aSGao Xiang /*
1699f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
1709f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
1719f6cc76eSGao Xiang  */
1729f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
1739f6cc76eSGao Xiang 	struct kmem_cache *slab;
1749f6cc76eSGao Xiang 	unsigned int maxpages;
1759f6cc76eSGao Xiang 	char name[48];
1769f6cc76eSGao Xiang };
1779f6cc76eSGao Xiang 
1789f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
1799f6cc76eSGao Xiang 
1809f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
1819f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
1829f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
1839f6cc76eSGao Xiang };
1849f6cc76eSGao Xiang 
18506a304cdSGao Xiang struct z_erofs_bvec_iter {
18606a304cdSGao Xiang 	struct page *bvpage;
18706a304cdSGao Xiang 	struct z_erofs_bvset *bvset;
18806a304cdSGao Xiang 	unsigned int nr, cur;
18906a304cdSGao Xiang };
19006a304cdSGao Xiang 
19106a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
19206a304cdSGao Xiang {
19306a304cdSGao Xiang 	if (iter->bvpage)
19406a304cdSGao Xiang 		kunmap_local(iter->bvset);
19506a304cdSGao Xiang 	return iter->bvpage;
19606a304cdSGao Xiang }
19706a304cdSGao Xiang 
19806a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
19906a304cdSGao Xiang {
20006a304cdSGao Xiang 	unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
20106a304cdSGao Xiang 	/* have to access nextpage in advance, otherwise it will be unmapped */
20206a304cdSGao Xiang 	struct page *nextpage = iter->bvset->nextpage;
20306a304cdSGao Xiang 	struct page *oldpage;
20406a304cdSGao Xiang 
20506a304cdSGao Xiang 	DBG_BUGON(!nextpage);
20606a304cdSGao Xiang 	oldpage = z_erofs_bvec_iter_end(iter);
20706a304cdSGao Xiang 	iter->bvpage = nextpage;
20806a304cdSGao Xiang 	iter->bvset = kmap_local_page(nextpage);
20906a304cdSGao Xiang 	iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
21006a304cdSGao Xiang 	iter->cur = 0;
21106a304cdSGao Xiang 	return oldpage;
21206a304cdSGao Xiang }
21306a304cdSGao Xiang 
21406a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
21506a304cdSGao Xiang 				    struct z_erofs_bvset_inline *bvset,
21606a304cdSGao Xiang 				    unsigned int bootstrap_nr,
21706a304cdSGao Xiang 				    unsigned int cur)
21806a304cdSGao Xiang {
21906a304cdSGao Xiang 	*iter = (struct z_erofs_bvec_iter) {
22006a304cdSGao Xiang 		.nr = bootstrap_nr,
22106a304cdSGao Xiang 		.bvset = (struct z_erofs_bvset *)bvset,
22206a304cdSGao Xiang 	};
22306a304cdSGao Xiang 
22406a304cdSGao Xiang 	while (cur > iter->nr) {
22506a304cdSGao Xiang 		cur -= iter->nr;
22606a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
22706a304cdSGao Xiang 	}
22806a304cdSGao Xiang 	iter->cur = cur;
22906a304cdSGao Xiang }
23006a304cdSGao Xiang 
23106a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
23206a304cdSGao Xiang 				struct z_erofs_bvec *bvec,
2336ab5eed6SGao Xiang 				struct page **candidate_bvpage,
2346ab5eed6SGao Xiang 				struct page **pagepool)
23506a304cdSGao Xiang {
23605b63d2bSGao Xiang 	if (iter->cur >= iter->nr) {
23705b63d2bSGao Xiang 		struct page *nextpage = *candidate_bvpage;
23806a304cdSGao Xiang 
23905b63d2bSGao Xiang 		if (!nextpage) {
2406ab5eed6SGao Xiang 			nextpage = erofs_allocpage(pagepool, GFP_NOFS);
24105b63d2bSGao Xiang 			if (!nextpage)
24205b63d2bSGao Xiang 				return -ENOMEM;
24305b63d2bSGao Xiang 			set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
24405b63d2bSGao Xiang 		}
24506a304cdSGao Xiang 		DBG_BUGON(iter->bvset->nextpage);
24605b63d2bSGao Xiang 		iter->bvset->nextpage = nextpage;
24706a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
24806a304cdSGao Xiang 
24906a304cdSGao Xiang 		iter->bvset->nextpage = NULL;
25006a304cdSGao Xiang 		*candidate_bvpage = NULL;
25106a304cdSGao Xiang 	}
25206a304cdSGao Xiang 	iter->bvset->bvec[iter->cur++] = *bvec;
25306a304cdSGao Xiang 	return 0;
25406a304cdSGao Xiang }
25506a304cdSGao Xiang 
25606a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
25706a304cdSGao Xiang 				 struct z_erofs_bvec *bvec,
25806a304cdSGao Xiang 				 struct page **old_bvpage)
25906a304cdSGao Xiang {
26006a304cdSGao Xiang 	if (iter->cur == iter->nr)
26106a304cdSGao Xiang 		*old_bvpage = z_erofs_bvset_flip(iter);
26206a304cdSGao Xiang 	else
26306a304cdSGao Xiang 		*old_bvpage = NULL;
26406a304cdSGao Xiang 	*bvec = iter->bvset->bvec[iter->cur++];
26506a304cdSGao Xiang }
26606a304cdSGao Xiang 
2679f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
2689f6cc76eSGao Xiang {
2699f6cc76eSGao Xiang 	int i;
2709f6cc76eSGao Xiang 
2719f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
2729f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
2739f6cc76eSGao Xiang 			continue;
2749f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
2759f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
2769f6cc76eSGao Xiang 	}
2779f6cc76eSGao Xiang }
2789f6cc76eSGao Xiang 
2799f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
2809f6cc76eSGao Xiang {
2819f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
2829f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
2839f6cc76eSGao Xiang 	unsigned int size;
2849f6cc76eSGao Xiang 
2859f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
2869f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
287ed722fbcSGao Xiang 		size = struct_size(a, compressed_bvecs, pcs->maxpages);
2889f6cc76eSGao Xiang 
2899f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
2909f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
2919f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
2929f6cc76eSGao Xiang 		if (pcs->slab)
2939f6cc76eSGao Xiang 			continue;
2949f6cc76eSGao Xiang 
2959f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
2969f6cc76eSGao Xiang 		return -ENOMEM;
2979f6cc76eSGao Xiang 	}
2989f6cc76eSGao Xiang 	return 0;
2999f6cc76eSGao Xiang }
3009f6cc76eSGao Xiang 
3019f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
3029f6cc76eSGao Xiang {
3039f6cc76eSGao Xiang 	int i;
3049f6cc76eSGao Xiang 
3059f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3069f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3079f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
3089f6cc76eSGao Xiang 
3099f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
3109f6cc76eSGao Xiang 			continue;
3119f6cc76eSGao Xiang 
3129f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
3139f6cc76eSGao Xiang 		if (!pcl)
3149f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
3159f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
3169f6cc76eSGao Xiang 		return pcl;
3179f6cc76eSGao Xiang 	}
3189f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
3199f6cc76eSGao Xiang }
3209f6cc76eSGao Xiang 
3219f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
3229f6cc76eSGao Xiang {
323cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
3249f6cc76eSGao Xiang 	int i;
3259f6cc76eSGao Xiang 
3269f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3279f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3289f6cc76eSGao Xiang 
329cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
3309f6cc76eSGao Xiang 			continue;
3319f6cc76eSGao Xiang 
3329f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
3339f6cc76eSGao Xiang 		return;
3349f6cc76eSGao Xiang 	}
3359f6cc76eSGao Xiang 	DBG_BUGON(1);
3369f6cc76eSGao Xiang }
3379f6cc76eSGao Xiang 
33847e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
33947e4937aSGao Xiang 
3403fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
3413fffb589SSandeep Dhavale static struct kthread_worker __rcu **z_erofs_pcpu_workers;
3423fffb589SSandeep Dhavale 
3433fffb589SSandeep Dhavale static void erofs_destroy_percpu_workers(void)
34447e4937aSGao Xiang {
3453fffb589SSandeep Dhavale 	struct kthread_worker *worker;
3463fffb589SSandeep Dhavale 	unsigned int cpu;
3473fffb589SSandeep Dhavale 
3483fffb589SSandeep Dhavale 	for_each_possible_cpu(cpu) {
3493fffb589SSandeep Dhavale 		worker = rcu_dereference_protected(
3503fffb589SSandeep Dhavale 					z_erofs_pcpu_workers[cpu], 1);
3513fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
3523fffb589SSandeep Dhavale 		if (worker)
3533fffb589SSandeep Dhavale 			kthread_destroy_worker(worker);
3543fffb589SSandeep Dhavale 	}
3553fffb589SSandeep Dhavale 	kfree(z_erofs_pcpu_workers);
35647e4937aSGao Xiang }
35747e4937aSGao Xiang 
3583fffb589SSandeep Dhavale static struct kthread_worker *erofs_init_percpu_worker(int cpu)
35947e4937aSGao Xiang {
3603fffb589SSandeep Dhavale 	struct kthread_worker *worker =
3613fffb589SSandeep Dhavale 		kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
36247e4937aSGao Xiang 
3633fffb589SSandeep Dhavale 	if (IS_ERR(worker))
3643fffb589SSandeep Dhavale 		return worker;
3653fffb589SSandeep Dhavale 	if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
3663fffb589SSandeep Dhavale 		sched_set_fifo_low(worker->task);
3673fffb589SSandeep Dhavale 	return worker;
3683fffb589SSandeep Dhavale }
3693fffb589SSandeep Dhavale 
3703fffb589SSandeep Dhavale static int erofs_init_percpu_workers(void)
3713fffb589SSandeep Dhavale {
3723fffb589SSandeep Dhavale 	struct kthread_worker *worker;
3733fffb589SSandeep Dhavale 	unsigned int cpu;
3743fffb589SSandeep Dhavale 
3753fffb589SSandeep Dhavale 	z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
3763fffb589SSandeep Dhavale 			sizeof(struct kthread_worker *), GFP_ATOMIC);
3773fffb589SSandeep Dhavale 	if (!z_erofs_pcpu_workers)
3783fffb589SSandeep Dhavale 		return -ENOMEM;
3793fffb589SSandeep Dhavale 
3803fffb589SSandeep Dhavale 	for_each_online_cpu(cpu) {	/* could miss cpu{off,on}line? */
3813fffb589SSandeep Dhavale 		worker = erofs_init_percpu_worker(cpu);
3823fffb589SSandeep Dhavale 		if (!IS_ERR(worker))
3833fffb589SSandeep Dhavale 			rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
3843fffb589SSandeep Dhavale 	}
3853fffb589SSandeep Dhavale 	return 0;
3863fffb589SSandeep Dhavale }
3873fffb589SSandeep Dhavale #else
3883fffb589SSandeep Dhavale static inline void erofs_destroy_percpu_workers(void) {}
3893fffb589SSandeep Dhavale static inline int erofs_init_percpu_workers(void) { return 0; }
3903fffb589SSandeep Dhavale #endif
3913fffb589SSandeep Dhavale 
3923fffb589SSandeep Dhavale #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
3933fffb589SSandeep Dhavale static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
3943fffb589SSandeep Dhavale static enum cpuhp_state erofs_cpuhp_state;
3953fffb589SSandeep Dhavale 
3963fffb589SSandeep Dhavale static int erofs_cpu_online(unsigned int cpu)
3973fffb589SSandeep Dhavale {
3983fffb589SSandeep Dhavale 	struct kthread_worker *worker, *old;
3993fffb589SSandeep Dhavale 
4003fffb589SSandeep Dhavale 	worker = erofs_init_percpu_worker(cpu);
4013fffb589SSandeep Dhavale 	if (IS_ERR(worker))
4023fffb589SSandeep Dhavale 		return PTR_ERR(worker);
4033fffb589SSandeep Dhavale 
4043fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
4053fffb589SSandeep Dhavale 	old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
4063fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
4073fffb589SSandeep Dhavale 	if (!old)
4083fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
4093fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
4103fffb589SSandeep Dhavale 	if (old)
4113fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
4123fffb589SSandeep Dhavale 	return 0;
4133fffb589SSandeep Dhavale }
4143fffb589SSandeep Dhavale 
4153fffb589SSandeep Dhavale static int erofs_cpu_offline(unsigned int cpu)
4163fffb589SSandeep Dhavale {
4173fffb589SSandeep Dhavale 	struct kthread_worker *worker;
4183fffb589SSandeep Dhavale 
4193fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
4203fffb589SSandeep Dhavale 	worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
4213fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
4223fffb589SSandeep Dhavale 	rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
4233fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
4243fffb589SSandeep Dhavale 
4253fffb589SSandeep Dhavale 	synchronize_rcu();
4263fffb589SSandeep Dhavale 	if (worker)
4273fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
4283fffb589SSandeep Dhavale 	return 0;
4293fffb589SSandeep Dhavale }
4303fffb589SSandeep Dhavale 
4313fffb589SSandeep Dhavale static int erofs_cpu_hotplug_init(void)
4323fffb589SSandeep Dhavale {
4333fffb589SSandeep Dhavale 	int state;
4343fffb589SSandeep Dhavale 
4353fffb589SSandeep Dhavale 	state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
4363fffb589SSandeep Dhavale 			"fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
4373fffb589SSandeep Dhavale 	if (state < 0)
4383fffb589SSandeep Dhavale 		return state;
4393fffb589SSandeep Dhavale 
4403fffb589SSandeep Dhavale 	erofs_cpuhp_state = state;
4413fffb589SSandeep Dhavale 	return 0;
4423fffb589SSandeep Dhavale }
4433fffb589SSandeep Dhavale 
4443fffb589SSandeep Dhavale static void erofs_cpu_hotplug_destroy(void)
4453fffb589SSandeep Dhavale {
4463fffb589SSandeep Dhavale 	if (erofs_cpuhp_state)
4473fffb589SSandeep Dhavale 		cpuhp_remove_state_nocalls(erofs_cpuhp_state);
4483fffb589SSandeep Dhavale }
4493fffb589SSandeep Dhavale #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
4503fffb589SSandeep Dhavale static inline int erofs_cpu_hotplug_init(void) { return 0; }
4513fffb589SSandeep Dhavale static inline void erofs_cpu_hotplug_destroy(void) {}
4523fffb589SSandeep Dhavale #endif
4533fffb589SSandeep Dhavale 
4543fffb589SSandeep Dhavale void z_erofs_exit_zip_subsystem(void)
4553fffb589SSandeep Dhavale {
4563fffb589SSandeep Dhavale 	erofs_cpu_hotplug_destroy();
4573fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
4583fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
4593fffb589SSandeep Dhavale 	z_erofs_destroy_pcluster_pool();
46047e4937aSGao Xiang }
46147e4937aSGao Xiang 
46247e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
46347e4937aSGao Xiang {
4649f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
46547e4937aSGao Xiang 
4669f6cc76eSGao Xiang 	if (err)
4673fffb589SSandeep Dhavale 		goto out_error_pcluster_pool;
4683fffb589SSandeep Dhavale 
4693fffb589SSandeep Dhavale 	z_erofs_workqueue = alloc_workqueue("erofs_worker",
4703fffb589SSandeep Dhavale 			WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
4718d1b80a7SDan Carpenter 	if (!z_erofs_workqueue) {
4728d1b80a7SDan Carpenter 		err = -ENOMEM;
4733fffb589SSandeep Dhavale 		goto out_error_workqueue_init;
4748d1b80a7SDan Carpenter 	}
4753fffb589SSandeep Dhavale 
4763fffb589SSandeep Dhavale 	err = erofs_init_percpu_workers();
4779f6cc76eSGao Xiang 	if (err)
4783fffb589SSandeep Dhavale 		goto out_error_pcpu_worker;
4793fffb589SSandeep Dhavale 
4803fffb589SSandeep Dhavale 	err = erofs_cpu_hotplug_init();
4813fffb589SSandeep Dhavale 	if (err < 0)
4823fffb589SSandeep Dhavale 		goto out_error_cpuhp_init;
4833fffb589SSandeep Dhavale 	return err;
4843fffb589SSandeep Dhavale 
4853fffb589SSandeep Dhavale out_error_cpuhp_init:
4863fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
4873fffb589SSandeep Dhavale out_error_pcpu_worker:
4883fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
4893fffb589SSandeep Dhavale out_error_workqueue_init:
4909f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
4913fffb589SSandeep Dhavale out_error_pcluster_pool:
4929f6cc76eSGao Xiang 	return err;
49347e4937aSGao Xiang }
49447e4937aSGao Xiang 
495db166fc2SGao Xiang enum z_erofs_pclustermode {
496db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_INFLIGHT,
49747e4937aSGao Xiang 	/*
498db166fc2SGao Xiang 	 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
4990b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
5000b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
501387bab87SGao Xiang 	 * bvpage) since it can be directly decoded without I/O submission.
5020b964600SGao Xiang 	 */
503db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
50447e4937aSGao Xiang 	/*
505dcba1b23SGao Xiang 	 * The pcluster was just linked to a decompression chain by us.  It can
506dcba1b23SGao Xiang 	 * also be linked with the remaining pclusters, which means if the
507dcba1b23SGao Xiang 	 * processing page is the tail page of a pcluster, this pcluster can
508dcba1b23SGao Xiang 	 * safely use the whole page (since the previous pcluster is within the
509dcba1b23SGao Xiang 	 * same chain) for in-place I/O, as illustrated below:
510dcba1b23SGao Xiang 	 *  ___________________________________________________
51147e4937aSGao Xiang 	 * |  tail (partial) page  |    head (partial) page    |
512dcba1b23SGao Xiang 	 * |  (of the current pcl) |   (of the previous pcl)   |
513dcba1b23SGao Xiang 	 * |___PCLUSTER_FOLLOWED___|_____PCLUSTER_FOLLOWED_____|
51447e4937aSGao Xiang 	 *
515dcba1b23SGao Xiang 	 * [  (*) the page above can be used as inplace I/O.   ]
51647e4937aSGao Xiang 	 */
517db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED,
51847e4937aSGao Xiang };
51947e4937aSGao Xiang 
5205c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
5215c6dcc57SGao Xiang 	struct inode *const inode;
5225c6dcc57SGao Xiang 	struct erofs_map_blocks map;
52306a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
52447e4937aSGao Xiang 
5256ab5eed6SGao Xiang 	struct page *pagepool;
52606a304cdSGao Xiang 	struct page *candidate_bvpage;
527967c28b2SGao Xiang 	struct z_erofs_pcluster *pcl;
52847e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
529db166fc2SGao Xiang 	enum z_erofs_pclustermode mode;
53047e4937aSGao Xiang 
53147e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
53247e4937aSGao Xiang 	bool backmost;
53347e4937aSGao Xiang 	erofs_off_t headoffset;
534ed722fbcSGao Xiang 
535ed722fbcSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
536ed722fbcSGao Xiang 	unsigned int icur;
53747e4937aSGao Xiang };
53847e4937aSGao Xiang 
53947e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
5405c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
541db166fc2SGao Xiang 	.mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
54247e4937aSGao Xiang 
5431282dea3SGao Xiang static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
5441282dea3SGao Xiang {
5451282dea3SGao Xiang 	unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
5461282dea3SGao Xiang 
5471282dea3SGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
5481282dea3SGao Xiang 		return false;
5491282dea3SGao Xiang 
5501282dea3SGao Xiang 	if (fe->backmost)
5511282dea3SGao Xiang 		return true;
5521282dea3SGao Xiang 
5531282dea3SGao Xiang 	if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
5541282dea3SGao Xiang 	    fe->map.m_la < fe->headoffset)
5551282dea3SGao Xiang 		return true;
5561282dea3SGao Xiang 
5571282dea3SGao Xiang 	return false;
5581282dea3SGao Xiang }
5591282dea3SGao Xiang 
5606ab5eed6SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe)
56147e4937aSGao Xiang {
5626f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
5635c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
5641282dea3SGao Xiang 	bool shouldalloc = z_erofs_should_alloc_cache(fe);
56547e4937aSGao Xiang 	bool standalone = true;
5666f39d1e1SGao Xiang 	/*
5676f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
5686f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
5696f39d1e1SGao Xiang 	 */
5701825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
5711825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
572ed722fbcSGao Xiang 	unsigned int i;
57347e4937aSGao Xiang 
574db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
57547e4937aSGao Xiang 		return;
57647e4937aSGao Xiang 
577ed722fbcSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
57847e4937aSGao Xiang 		struct page *page;
579b1ed220cSGao Xiang 		void *t;	/* mark pages just found for debugging */
5801825c8d7SGao Xiang 		struct page *newpage = NULL;
58147e4937aSGao Xiang 
58247e4937aSGao Xiang 		/* the compressed page was loaded before */
583ed722fbcSGao Xiang 		if (READ_ONCE(pcl->compressed_bvecs[i].page))
58447e4937aSGao Xiang 			continue;
58547e4937aSGao Xiang 
586ed722fbcSGao Xiang 		page = find_get_page(mc, pcl->obj.index + i);
58747e4937aSGao Xiang 
58847e4937aSGao Xiang 		if (page) {
589b1ed220cSGao Xiang 			t = (void *)((unsigned long)page | 1);
5900b964600SGao Xiang 		} else {
5910b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
5920b964600SGao Xiang 			standalone = false;
5931282dea3SGao Xiang 			if (!shouldalloc)
5941282dea3SGao Xiang 				continue;
5951282dea3SGao Xiang 
5961282dea3SGao Xiang 			/*
5971282dea3SGao Xiang 			 * try to use cached I/O if page allocation
5981282dea3SGao Xiang 			 * succeeds or fallback to in-place I/O instead
5991282dea3SGao Xiang 			 * to avoid any direct reclaim.
6001282dea3SGao Xiang 			 */
6016ab5eed6SGao Xiang 			newpage = erofs_allocpage(&fe->pagepool, gfp);
6021825c8d7SGao Xiang 			if (!newpage)
60347e4937aSGao Xiang 				continue;
6041282dea3SGao Xiang 			set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
605b1ed220cSGao Xiang 			t = (void *)((unsigned long)newpage | 1);
60647e4937aSGao Xiang 		}
60747e4937aSGao Xiang 
608b1ed220cSGao Xiang 		if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
60947e4937aSGao Xiang 			continue;
61047e4937aSGao Xiang 
611eaa9172aSGao Xiang 		if (page)
61247e4937aSGao Xiang 			put_page(page);
613eaa9172aSGao Xiang 		else if (newpage)
6146ab5eed6SGao Xiang 			erofs_pagepool_add(&fe->pagepool, newpage);
61547e4937aSGao Xiang 	}
61647e4937aSGao Xiang 
6170b964600SGao Xiang 	/*
6180b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
6190b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
6200b964600SGao Xiang 	 */
6210b964600SGao Xiang 	if (standalone)
622db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
62347e4937aSGao Xiang }
62447e4937aSGao Xiang 
62547e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
62647e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
62747e4937aSGao Xiang 				       struct erofs_workgroup *grp)
62847e4937aSGao Xiang {
62947e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
63047e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
63147e4937aSGao Xiang 	int i;
63247e4937aSGao Xiang 
633cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
63447e4937aSGao Xiang 	/*
6357674a42fSGao Xiang 	 * refcount of workgroup is now freezed as 0,
63647e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
63747e4937aSGao Xiang 	 */
6389f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
639ed722fbcSGao Xiang 		struct page *page = pcl->compressed_bvecs[i].page;
64047e4937aSGao Xiang 
64147e4937aSGao Xiang 		if (!page)
64247e4937aSGao Xiang 			continue;
64347e4937aSGao Xiang 
64447e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
64547e4937aSGao Xiang 		if (!trylock_page(page))
64647e4937aSGao Xiang 			return -EBUSY;
64747e4937aSGao Xiang 
648f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
64947e4937aSGao Xiang 			continue;
65047e4937aSGao Xiang 
65147e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
652ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
6536aaa7b06SGao Xiang 		detach_page_private(page);
65447e4937aSGao Xiang 		unlock_page(page);
65547e4937aSGao Xiang 	}
65647e4937aSGao Xiang 	return 0;
65747e4937aSGao Xiang }
65847e4937aSGao Xiang 
6597b4e372cSGao Xiang static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)
66047e4937aSGao Xiang {
6617b4e372cSGao Xiang 	struct z_erofs_pcluster *pcl = folio_get_private(folio);
6627b4e372cSGao Xiang 	bool ret;
6637b4e372cSGao Xiang 	int i;
6647b4e372cSGao Xiang 
6657b4e372cSGao Xiang 	if (!folio_test_private(folio))
6667b4e372cSGao Xiang 		return true;
66747e4937aSGao Xiang 
6687b4e372cSGao Xiang 	ret = false;
6697674a42fSGao Xiang 	spin_lock(&pcl->obj.lockref.lock);
6707674a42fSGao Xiang 	if (pcl->obj.lockref.count > 0)
6717674a42fSGao Xiang 		goto out;
6727674a42fSGao Xiang 
673cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
6749f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
6757b4e372cSGao Xiang 		if (pcl->compressed_bvecs[i].page == &folio->page) {
676ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
6777b4e372cSGao Xiang 			ret = true;
67847e4937aSGao Xiang 			break;
67947e4937aSGao Xiang 		}
68047e4937aSGao Xiang 	}
6816aaa7b06SGao Xiang 	if (ret)
6827b4e372cSGao Xiang 		folio_detach_private(folio);
6837674a42fSGao Xiang out:
6847674a42fSGao Xiang 	spin_unlock(&pcl->obj.lockref.lock);
68547e4937aSGao Xiang 	return ret;
68647e4937aSGao Xiang }
68747e4937aSGao Xiang 
6887b4e372cSGao Xiang /*
6897b4e372cSGao Xiang  * It will be called only on inode eviction. In case that there are still some
6907b4e372cSGao Xiang  * decompression requests in progress, wait with rescheduling for a bit here.
6917b4e372cSGao Xiang  * An extra lock could be introduced instead but it seems unnecessary.
6927b4e372cSGao Xiang  */
6937b4e372cSGao Xiang static void z_erofs_cache_invalidate_folio(struct folio *folio,
6947b4e372cSGao Xiang 					   size_t offset, size_t length)
6957b4e372cSGao Xiang {
6967b4e372cSGao Xiang 	const size_t stop = length + offset;
6977b4e372cSGao Xiang 
6987b4e372cSGao Xiang 	/* Check for potential overflow in debug mode */
6997b4e372cSGao Xiang 	DBG_BUGON(stop > folio_size(folio) || stop < length);
7007b4e372cSGao Xiang 
7017b4e372cSGao Xiang 	if (offset == 0 && stop == folio_size(folio))
7027b4e372cSGao Xiang 		while (!z_erofs_cache_release_folio(folio, GFP_NOFS))
7037b4e372cSGao Xiang 			cond_resched();
7047b4e372cSGao Xiang }
7057b4e372cSGao Xiang 
7067b4e372cSGao Xiang static const struct address_space_operations z_erofs_cache_aops = {
7077b4e372cSGao Xiang 	.release_folio = z_erofs_cache_release_folio,
7087b4e372cSGao Xiang 	.invalidate_folio = z_erofs_cache_invalidate_folio,
7097b4e372cSGao Xiang };
7107b4e372cSGao Xiang 
7117b4e372cSGao Xiang int erofs_init_managed_cache(struct super_block *sb)
7127b4e372cSGao Xiang {
7137b4e372cSGao Xiang 	struct inode *const inode = new_inode(sb);
7147b4e372cSGao Xiang 
7157b4e372cSGao Xiang 	if (!inode)
7167b4e372cSGao Xiang 		return -ENOMEM;
7177b4e372cSGao Xiang 
7187b4e372cSGao Xiang 	set_nlink(inode, 1);
7197b4e372cSGao Xiang 	inode->i_size = OFFSET_MAX;
7207b4e372cSGao Xiang 	inode->i_mapping->a_ops = &z_erofs_cache_aops;
7217b4e372cSGao Xiang 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
7227b4e372cSGao Xiang 	EROFS_SB(sb)->managed_cache = inode;
7237b4e372cSGao Xiang 	return 0;
7247b4e372cSGao Xiang }
7257b4e372cSGao Xiang 
7265c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
727ed722fbcSGao Xiang 				   struct z_erofs_bvec *bvec)
72847e4937aSGao Xiang {
7295c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
73047e4937aSGao Xiang 
731ed722fbcSGao Xiang 	while (fe->icur > 0) {
732ed722fbcSGao Xiang 		if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
733ed722fbcSGao Xiang 			     NULL, bvec->page)) {
734ed722fbcSGao Xiang 			pcl->compressed_bvecs[fe->icur] = *bvec;
73547e4937aSGao Xiang 			return true;
736ed722fbcSGao Xiang 		}
737ed722fbcSGao Xiang 	}
73847e4937aSGao Xiang 	return false;
73947e4937aSGao Xiang }
74047e4937aSGao Xiang 
74187ca34a7SGao Xiang /* callers must be with pcluster lock held */
7425c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
7435b220b20SGao Xiang 			       struct z_erofs_bvec *bvec, bool exclusive)
74447e4937aSGao Xiang {
74547e4937aSGao Xiang 	int ret;
74647e4937aSGao Xiang 
747db166fc2SGao Xiang 	if (exclusive) {
74806a304cdSGao Xiang 		/* give priority for inplaceio to use file pages first */
749ed722fbcSGao Xiang 		if (z_erofs_try_inplace_io(fe, bvec))
75047e4937aSGao Xiang 			return 0;
75106a304cdSGao Xiang 		/* otherwise, check if it can be used as a bvpage */
752db166fc2SGao Xiang 		if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
75306a304cdSGao Xiang 		    !fe->candidate_bvpage)
75406a304cdSGao Xiang 			fe->candidate_bvpage = bvec->page;
75506a304cdSGao Xiang 	}
7566ab5eed6SGao Xiang 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
7576ab5eed6SGao Xiang 				   &fe->pagepool);
75806a304cdSGao Xiang 	fe->pcl->vcnt += (ret >= 0);
75906a304cdSGao Xiang 	return ret;
76047e4937aSGao Xiang }
76147e4937aSGao Xiang 
7625c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
76347e4937aSGao Xiang {
7645c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
7655c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
76647e4937aSGao Xiang 
767473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
768473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
769473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
77047e4937aSGao Xiang 		*owned_head = &pcl->next;
771473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
772db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
773473e15b0SGao Xiang 		return;
774473e15b0SGao Xiang 	}
775473e15b0SGao Xiang 
776967c28b2SGao Xiang 	/* type 2, it belongs to an ongoing chain */
777db166fc2SGao Xiang 	f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
77847e4937aSGao Xiang }
77947e4937aSGao Xiang 
78083a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
78147e4937aSGao Xiang {
78283a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
783cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
78447e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
78564094a04SGao Xiang 	struct erofs_workgroup *grp;
78647e4937aSGao Xiang 	int err;
78747e4937aSGao Xiang 
788c42c0ffeSChen Zhongjin 	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
789c42c0ffeSChen Zhongjin 	    (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
7908f899262SGao Xiang 		DBG_BUGON(1);
7918f899262SGao Xiang 		return -EFSCORRUPTED;
7928f899262SGao Xiang 	}
7938f899262SGao Xiang 
7949f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
795cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
796cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
7979f6cc76eSGao Xiang 	if (IS_ERR(pcl))
7989f6cc76eSGao Xiang 		return PTR_ERR(pcl);
79947e4937aSGao Xiang 
8007674a42fSGao Xiang 	spin_lock_init(&pcl->obj.lockref.lock);
8018f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
8022bfab9c0SGao Xiang 	pcl->length = 0;
8032bfab9c0SGao Xiang 	pcl->partial = true;
80447e4937aSGao Xiang 
80547e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
8065c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
80787ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
808db166fc2SGao Xiang 	fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
80947e4937aSGao Xiang 
81047e4937aSGao Xiang 	/*
81147e4937aSGao Xiang 	 * lock all primary followed works before visible to others
81247e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
81347e4937aSGao Xiang 	 */
81487ca34a7SGao Xiang 	mutex_init(&pcl->lock);
81587ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
81647e4937aSGao Xiang 
817cecf864dSYue Hu 	if (ztailpacking) {
818cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
8193acea5fcSJingbo Xu 		pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa);
820cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
821cecf864dSYue Hu 	} else {
822cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
823cecf864dSYue Hu 
82483a386c0SGao Xiang 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
82564094a04SGao Xiang 		if (IS_ERR(grp)) {
82664094a04SGao Xiang 			err = PTR_ERR(grp);
82764094a04SGao Xiang 			goto err_out;
82864094a04SGao Xiang 		}
82964094a04SGao Xiang 
83064094a04SGao Xiang 		if (grp != &pcl->obj) {
8315c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
832cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
83364094a04SGao Xiang 			err = -EEXIST;
83464094a04SGao Xiang 			goto err_out;
83547e4937aSGao Xiang 		}
836cecf864dSYue Hu 	}
8375c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
8385c6dcc57SGao Xiang 	fe->pcl = pcl;
8399e579fc1SGao Xiang 	return 0;
84064094a04SGao Xiang 
84164094a04SGao Xiang err_out:
84287ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
8439f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
84464094a04SGao Xiang 	return err;
84547e4937aSGao Xiang }
84647e4937aSGao Xiang 
847dcba1b23SGao Xiang static int z_erofs_pcluster_begin(struct z_erofs_decompress_frontend *fe)
84847e4937aSGao Xiang {
84983a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
850aeebae9dSGao Xiang 	struct super_block *sb = fe->inode->i_sb;
851aeebae9dSGao Xiang 	erofs_blk_t blknr = erofs_blknr(sb, map->m_pa);
8520d823b42SGao Xiang 	struct erofs_workgroup *grp = NULL;
8539e579fc1SGao Xiang 	int ret;
85447e4937aSGao Xiang 
85587ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
85647e4937aSGao Xiang 
85787ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
8585c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
85947e4937aSGao Xiang 
8600d823b42SGao Xiang 	if (!(map->m_flags & EROFS_MAP_META)) {
861aeebae9dSGao Xiang 		grp = erofs_find_workgroup(sb, blknr);
8620d823b42SGao Xiang 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
86347e4937aSGao Xiang 		DBG_BUGON(1);
864cecf864dSYue Hu 		return -EFSCORRUPTED;
865cecf864dSYue Hu 	}
86647e4937aSGao Xiang 
86764094a04SGao Xiang 	if (grp) {
8685c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
8690d823b42SGao Xiang 		ret = -EEXIST;
87064094a04SGao Xiang 	} else {
87183a386c0SGao Xiang 		ret = z_erofs_register_pcluster(fe);
87264094a04SGao Xiang 	}
87347e4937aSGao Xiang 
8740d823b42SGao Xiang 	if (ret == -EEXIST) {
875267f2492SGao Xiang 		mutex_lock(&fe->pcl->lock);
876267f2492SGao Xiang 		z_erofs_try_to_claim_pcluster(fe);
8770d823b42SGao Xiang 	} else if (ret) {
8780d823b42SGao Xiang 		return ret;
8790d823b42SGao Xiang 	}
880aeebae9dSGao Xiang 
88106a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
882387bab87SGao Xiang 				Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
883aeebae9dSGao Xiang 	if (!z_erofs_is_inline_pcluster(fe->pcl)) {
884aeebae9dSGao Xiang 		/* bind cache first when cached decompression is preferred */
885aeebae9dSGao Xiang 		z_erofs_bind_cache(fe);
886aeebae9dSGao Xiang 	} else {
887aeebae9dSGao Xiang 		void *mptr;
888aeebae9dSGao Xiang 
889aeebae9dSGao Xiang 		mptr = erofs_read_metabuf(&map->buf, sb, blknr, EROFS_NO_KMAP);
890aeebae9dSGao Xiang 		if (IS_ERR(mptr)) {
891aeebae9dSGao Xiang 			ret = PTR_ERR(mptr);
892aeebae9dSGao Xiang 			erofs_err(sb, "failed to get inline data %d", ret);
893aeebae9dSGao Xiang 			return ret;
894aeebae9dSGao Xiang 		}
895aeebae9dSGao Xiang 		get_page(map->buf.page);
896aeebae9dSGao Xiang 		WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, map->buf.page);
897aeebae9dSGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
898aeebae9dSGao Xiang 	}
899aeebae9dSGao Xiang 	/* file-backed inplace I/O pages are traversed in reverse order */
900ed722fbcSGao Xiang 	fe->icur = z_erofs_pclusterpages(fe->pcl);
90147e4937aSGao Xiang 	return 0;
90247e4937aSGao Xiang }
90347e4937aSGao Xiang 
90447e4937aSGao Xiang /*
90547e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
90647e4937aSGao Xiang  * only after a RCU grace period.
90747e4937aSGao Xiang  */
90847e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
90947e4937aSGao Xiang {
91087ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
91187ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
91247e4937aSGao Xiang }
91347e4937aSGao Xiang 
91447e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
91547e4937aSGao Xiang {
91647e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
91747e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
91847e4937aSGao Xiang 
91987ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
92047e4937aSGao Xiang }
92147e4937aSGao Xiang 
922dcba1b23SGao Xiang static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe)
92347e4937aSGao Xiang {
92487ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
92547e4937aSGao Xiang 
92687ca34a7SGao Xiang 	if (!pcl)
927dcba1b23SGao Xiang 		return;
92847e4937aSGao Xiang 
92906a304cdSGao Xiang 	z_erofs_bvec_iter_end(&fe->biter);
93087ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
93147e4937aSGao Xiang 
93205b63d2bSGao Xiang 	if (fe->candidate_bvpage)
93306a304cdSGao Xiang 		fe->candidate_bvpage = NULL;
93406a304cdSGao Xiang 
93547e4937aSGao Xiang 	/*
93647e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
93747e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
93847e4937aSGao Xiang 	 */
939db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
94087ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
94147e4937aSGao Xiang 
94287ca34a7SGao Xiang 	fe->pcl = NULL;
943dcba1b23SGao Xiang 	fe->backmost = false;
94447e4937aSGao Xiang }
94547e4937aSGao Xiang 
9468b00be16SGao Xiang static int z_erofs_read_fragment(struct super_block *sb, struct page *page,
9478b00be16SGao Xiang 			unsigned int cur, unsigned int end, erofs_off_t pos)
948b15b2e30SYue Hu {
9498b00be16SGao Xiang 	struct inode *packed_inode = EROFS_SB(sb)->packed_inode;
950b15b2e30SYue Hu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
9518b00be16SGao Xiang 	unsigned int cnt;
9528b00be16SGao Xiang 	u8 *src;
953b15b2e30SYue Hu 
954e5126de1SYue Hu 	if (!packed_inode)
955e5126de1SYue Hu 		return -EFSCORRUPTED;
956e5126de1SYue Hu 
957eb2c5e41SGao Xiang 	buf.inode = packed_inode;
9588b00be16SGao Xiang 	for (; cur < end; cur += cnt, pos += cnt) {
9598b00be16SGao Xiang 		cnt = min_t(unsigned int, end - cur,
9603acea5fcSJingbo Xu 			    sb->s_blocksize - erofs_blkoff(sb, pos));
961eb2c5e41SGao Xiang 		src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
962b15b2e30SYue Hu 		if (IS_ERR(src)) {
963b15b2e30SYue Hu 			erofs_put_metabuf(&buf);
964b15b2e30SYue Hu 			return PTR_ERR(src);
965b15b2e30SYue Hu 		}
9668b00be16SGao Xiang 		memcpy_to_page(page, cur, src + erofs_blkoff(sb, pos), cnt);
967b15b2e30SYue Hu 	}
968b15b2e30SYue Hu 	erofs_put_metabuf(&buf);
969b15b2e30SYue Hu 	return 0;
970b15b2e30SYue Hu }
971b15b2e30SYue Hu 
97247e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
9736ab5eed6SGao Xiang 				struct page *page)
97447e4937aSGao Xiang {
97547e4937aSGao Xiang 	struct inode *const inode = fe->inode;
97647e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
97747e4937aSGao Xiang 	const loff_t offset = page_offset(page);
9785b220b20SGao Xiang 	bool tight = true, exclusive;
979e4c1cf52SGao Xiang 	unsigned int cur, end, len, split;
98047e4937aSGao Xiang 	int err = 0;
98147e4937aSGao Xiang 
98247e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
98347e4937aSGao Xiang 
984e4c1cf52SGao Xiang 	split = 0;
98547e4937aSGao Xiang 	end = PAGE_SIZE;
98647e4937aSGao Xiang repeat:
987e4c1cf52SGao Xiang 	if (offset + end - 1 < map->m_la ||
988e4c1cf52SGao Xiang 	    offset + end - 1 >= map->m_la + map->m_llen) {
989dcba1b23SGao Xiang 		z_erofs_pcluster_end(fe);
990e4c1cf52SGao Xiang 		map->m_la = offset + end - 1;
99147e4937aSGao Xiang 		map->m_llen = 0;
99247e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
9938d8a09b0SGao Xiang 		if (err)
99467148551SGao Xiang 			goto out;
99539397a46SGao Xiang 	}
99647e4937aSGao Xiang 
997e4c1cf52SGao Xiang 	cur = offset > map->m_la ? 0 : map->m_la - offset;
998e4c1cf52SGao Xiang 	/* bump split parts first to avoid several separate cases */
999e4c1cf52SGao Xiang 	++split;
1000dc76ea8cSGao Xiang 
10018d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
100247e4937aSGao Xiang 		zero_user_segment(page, cur, end);
1003e4c1cf52SGao Xiang 		tight = false;
100447e4937aSGao Xiang 		goto next_part;
100547e4937aSGao Xiang 	}
1006e4c1cf52SGao Xiang 
1007b15b2e30SYue Hu 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
10088b00be16SGao Xiang 		erofs_off_t fpos = offset + cur - map->m_la;
1009b15b2e30SYue Hu 
10108b00be16SGao Xiang 		len = min_t(unsigned int, map->m_llen - fpos, end - cur);
10118b00be16SGao Xiang 		err = z_erofs_read_fragment(inode->i_sb, page, cur, cur + len,
10128b00be16SGao Xiang 				EROFS_I(inode)->z_fragmentoff + fpos);
1013b15b2e30SYue Hu 		if (err)
1014b15b2e30SYue Hu 			goto out;
1015b15b2e30SYue Hu 		tight = false;
1016b15b2e30SYue Hu 		goto next_part;
1017b15b2e30SYue Hu 	}
101847e4937aSGao Xiang 
1019e4c1cf52SGao Xiang 	if (!fe->pcl) {
1020e4c1cf52SGao Xiang 		err = z_erofs_pcluster_begin(fe);
1021e4c1cf52SGao Xiang 		if (err)
1022e4c1cf52SGao Xiang 			goto out;
1023e4c1cf52SGao Xiang 	}
1024e4c1cf52SGao Xiang 
1025e4c1cf52SGao Xiang 	/*
1026e4c1cf52SGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
1027e4c1cf52SGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
1028e4c1cf52SGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
1029e4c1cf52SGao Xiang 	 * for inplace I/O or bvpage (should be processed in a strict order.)
1030e4c1cf52SGao Xiang 	 */
1031e4c1cf52SGao Xiang 	tight &= (fe->mode > Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1032e4c1cf52SGao Xiang 	exclusive = (!cur && ((split <= 1) || tight));
103347e4937aSGao Xiang 	if (cur)
1034db166fc2SGao Xiang 		tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
103547e4937aSGao Xiang 
103606a304cdSGao Xiang 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
103706a304cdSGao Xiang 					.page = page,
103806a304cdSGao Xiang 					.offset = offset - map->m_la,
103906a304cdSGao Xiang 					.end = end,
10405b220b20SGao Xiang 				  }), exclusive);
104105b63d2bSGao Xiang 	if (err)
104267148551SGao Xiang 		goto out;
104347e4937aSGao Xiang 
104467148551SGao Xiang 	z_erofs_onlinepage_split(page);
1045267f2492SGao Xiang 	if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1046267f2492SGao Xiang 		fe->pcl->multibases = true;
10472bfab9c0SGao Xiang 	if (fe->pcl->length < offset + end - map->m_la) {
10482bfab9c0SGao Xiang 		fe->pcl->length = offset + end - map->m_la;
10492bfab9c0SGao Xiang 		fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
10502bfab9c0SGao Xiang 	}
1051e7933278SGao Xiang 	if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1052e7933278SGao Xiang 	    !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1053e7933278SGao Xiang 	    fe->pcl->length == map->m_llen)
1054e7933278SGao Xiang 		fe->pcl->partial = false;
105547e4937aSGao Xiang next_part:
10562bfab9c0SGao Xiang 	/* shorten the remaining extent to update progress */
105747e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
10582bfab9c0SGao Xiang 	map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
105947e4937aSGao Xiang 
106047e4937aSGao Xiang 	end = cur;
106147e4937aSGao Xiang 	if (end > 0)
106247e4937aSGao Xiang 		goto repeat;
106347e4937aSGao Xiang 
106447e4937aSGao Xiang out:
1065*9a05c6a8SGao Xiang 	z_erofs_onlinepage_endio(page, err);
106647e4937aSGao Xiang 	return err;
106747e4937aSGao Xiang }
106847e4937aSGao Xiang 
1069ef4b4b46SYue Hu static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
107040452ffcSHuang Jianan 				       unsigned int readahead_pages)
107140452ffcSHuang Jianan {
1072a2e20a25SMatthew Wilcox (Oracle) 	/* auto: enable for read_folio, disable for readahead */
107340452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
107440452ffcSHuang Jianan 	    !readahead_pages)
107540452ffcSHuang Jianan 		return true;
107640452ffcSHuang Jianan 
107740452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
107840452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
107940452ffcSHuang Jianan 		return true;
108040452ffcSHuang Jianan 
108140452ffcSHuang Jianan 	return false;
108240452ffcSHuang Jianan }
108340452ffcSHuang Jianan 
10846aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
10856aaa7b06SGao Xiang {
10866aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
10876aaa7b06SGao Xiang }
10886aaa7b06SGao Xiang 
10894f05687fSGao Xiang struct z_erofs_decompress_backend {
10904f05687fSGao Xiang 	struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
10914f05687fSGao Xiang 	struct super_block *sb;
10924f05687fSGao Xiang 	struct z_erofs_pcluster *pcl;
10934f05687fSGao Xiang 
10944f05687fSGao Xiang 	/* pages with the longest decompressed length for deduplication */
10954f05687fSGao Xiang 	struct page **decompressed_pages;
10964f05687fSGao Xiang 	/* pages to keep the compressed data */
10974f05687fSGao Xiang 	struct page **compressed_pages;
10984f05687fSGao Xiang 
1099267f2492SGao Xiang 	struct list_head decompressed_secondary_bvecs;
11004f05687fSGao Xiang 	struct page **pagepool;
11012bfab9c0SGao Xiang 	unsigned int onstack_used, nr_pages;
11024f05687fSGao Xiang };
11034f05687fSGao Xiang 
1104267f2492SGao Xiang struct z_erofs_bvec_item {
1105267f2492SGao Xiang 	struct z_erofs_bvec bvec;
1106267f2492SGao Xiang 	struct list_head list;
1107267f2492SGao Xiang };
1108267f2492SGao Xiang 
1109267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
11103fe96ee0SGao Xiang 					 struct z_erofs_bvec *bvec)
11113fe96ee0SGao Xiang {
1112267f2492SGao Xiang 	struct z_erofs_bvec_item *item;
1113267f2492SGao Xiang 	unsigned int pgnr;
11143fe96ee0SGao Xiang 
111594c43de7SGao Xiang 	if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK) &&
111694c43de7SGao Xiang 	    (bvec->end == PAGE_SIZE ||
111794c43de7SGao Xiang 	     bvec->offset + bvec->end == be->pcl->length)) {
1118267f2492SGao Xiang 		pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
11192bfab9c0SGao Xiang 		DBG_BUGON(pgnr >= be->nr_pages);
112063bbb856SGao Xiang 		if (!be->decompressed_pages[pgnr]) {
11213fe96ee0SGao Xiang 			be->decompressed_pages[pgnr] = bvec->page;
1122267f2492SGao Xiang 			return;
11233fe96ee0SGao Xiang 		}
112463bbb856SGao Xiang 	}
11253fe96ee0SGao Xiang 
1126267f2492SGao Xiang 	/* (cold path) one pcluster is requested multiple times */
1127267f2492SGao Xiang 	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1128267f2492SGao Xiang 	item->bvec = *bvec;
1129267f2492SGao Xiang 	list_add(&item->list, &be->decompressed_secondary_bvecs);
1130267f2492SGao Xiang }
1131267f2492SGao Xiang 
1132267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1133267f2492SGao Xiang 				      int err)
1134267f2492SGao Xiang {
1135267f2492SGao Xiang 	unsigned int off0 = be->pcl->pageofs_out;
1136267f2492SGao Xiang 	struct list_head *p, *n;
1137267f2492SGao Xiang 
1138267f2492SGao Xiang 	list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1139267f2492SGao Xiang 		struct z_erofs_bvec_item *bvi;
1140267f2492SGao Xiang 		unsigned int end, cur;
1141267f2492SGao Xiang 		void *dst, *src;
1142267f2492SGao Xiang 
1143267f2492SGao Xiang 		bvi = container_of(p, struct z_erofs_bvec_item, list);
1144267f2492SGao Xiang 		cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1145267f2492SGao Xiang 		end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1146267f2492SGao Xiang 			    bvi->bvec.end);
1147267f2492SGao Xiang 		dst = kmap_local_page(bvi->bvec.page);
1148267f2492SGao Xiang 		while (cur < end) {
1149267f2492SGao Xiang 			unsigned int pgnr, scur, len;
1150267f2492SGao Xiang 
1151267f2492SGao Xiang 			pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1152267f2492SGao Xiang 			DBG_BUGON(pgnr >= be->nr_pages);
1153267f2492SGao Xiang 
1154267f2492SGao Xiang 			scur = bvi->bvec.offset + cur -
1155267f2492SGao Xiang 					((pgnr << PAGE_SHIFT) - off0);
1156267f2492SGao Xiang 			len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1157267f2492SGao Xiang 			if (!be->decompressed_pages[pgnr]) {
1158267f2492SGao Xiang 				err = -EFSCORRUPTED;
1159267f2492SGao Xiang 				cur += len;
1160267f2492SGao Xiang 				continue;
1161267f2492SGao Xiang 			}
1162267f2492SGao Xiang 			src = kmap_local_page(be->decompressed_pages[pgnr]);
1163267f2492SGao Xiang 			memcpy(dst + cur, src + scur, len);
1164267f2492SGao Xiang 			kunmap_local(src);
1165267f2492SGao Xiang 			cur += len;
1166267f2492SGao Xiang 		}
1167267f2492SGao Xiang 		kunmap_local(dst);
1168*9a05c6a8SGao Xiang 		z_erofs_onlinepage_endio(bvi->bvec.page, err);
1169267f2492SGao Xiang 		list_del(p);
1170267f2492SGao Xiang 		kfree(bvi);
1171267f2492SGao Xiang 	}
1172267f2492SGao Xiang }
1173267f2492SGao Xiang 
1174267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
117542fec235SGao Xiang {
11764f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
117706a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
117806a304cdSGao Xiang 	struct page *old_bvpage;
1179267f2492SGao Xiang 	int i;
118042fec235SGao Xiang 
1181387bab87SGao Xiang 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
118242fec235SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
118306a304cdSGao Xiang 		struct z_erofs_bvec bvec;
118442fec235SGao Xiang 
118506a304cdSGao Xiang 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
118642fec235SGao Xiang 
118706a304cdSGao Xiang 		if (old_bvpage)
11884f05687fSGao Xiang 			z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
118942fec235SGao Xiang 
119006a304cdSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1191267f2492SGao Xiang 		z_erofs_do_decompressed_bvec(be, &bvec);
119242fec235SGao Xiang 	}
119306a304cdSGao Xiang 
119406a304cdSGao Xiang 	old_bvpage = z_erofs_bvec_iter_end(&biter);
119506a304cdSGao Xiang 	if (old_bvpage)
11964f05687fSGao Xiang 		z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
119742fec235SGao Xiang }
119842fec235SGao Xiang 
11994f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
12004f05687fSGao Xiang 				  bool *overlapped)
120167139e36SGao Xiang {
12024f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
120367139e36SGao Xiang 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
120467139e36SGao Xiang 	int i, err = 0;
120567139e36SGao Xiang 
120667139e36SGao Xiang 	*overlapped = false;
120767139e36SGao Xiang 	for (i = 0; i < pclusterpages; ++i) {
1208ed722fbcSGao Xiang 		struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1209ed722fbcSGao Xiang 		struct page *page = bvec->page;
121067139e36SGao Xiang 
121167139e36SGao Xiang 		/* compressed pages ought to be present before decompressing */
121267139e36SGao Xiang 		if (!page) {
121367139e36SGao Xiang 			DBG_BUGON(1);
121467139e36SGao Xiang 			continue;
121567139e36SGao Xiang 		}
1216fe3e5914SGao Xiang 		be->compressed_pages[i] = page;
121767139e36SGao Xiang 
121867139e36SGao Xiang 		if (z_erofs_is_inline_pcluster(pcl)) {
121967139e36SGao Xiang 			if (!PageUptodate(page))
122067139e36SGao Xiang 				err = -EIO;
122167139e36SGao Xiang 			continue;
122267139e36SGao Xiang 		}
122367139e36SGao Xiang 
122467139e36SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
122567139e36SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
12264f05687fSGao Xiang 			if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
122767139e36SGao Xiang 				if (!PageUptodate(page))
122867139e36SGao Xiang 					err = -EIO;
122967139e36SGao Xiang 				continue;
123067139e36SGao Xiang 			}
1231267f2492SGao Xiang 			z_erofs_do_decompressed_bvec(be, bvec);
123267139e36SGao Xiang 			*overlapped = true;
123367139e36SGao Xiang 		}
123467139e36SGao Xiang 	}
123567139e36SGao Xiang 
1236fe3e5914SGao Xiang 	if (err)
12374f05687fSGao Xiang 		return err;
12384f05687fSGao Xiang 	return 0;
123967139e36SGao Xiang }
124067139e36SGao Xiang 
12414f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
12424f05687fSGao Xiang 				       int err)
124347e4937aSGao Xiang {
12444f05687fSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
12454f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
1246cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1247597e2953SYue Hu 	const struct z_erofs_decompressor *decompressor =
1248597e2953SYue Hu 				&erofs_decompressors[pcl->algorithmformat];
12492bfab9c0SGao Xiang 	unsigned int i, inputsize;
125067148551SGao Xiang 	int err2;
12512bfab9c0SGao Xiang 	struct page *page;
12522bfab9c0SGao Xiang 	bool overlapped;
125347e4937aSGao Xiang 
125487ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
12552bfab9c0SGao Xiang 	be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
125647e4937aSGao Xiang 
1257fe3e5914SGao Xiang 	/* allocate (de)compressed page arrays if cannot be kept on stack */
1258fe3e5914SGao Xiang 	be->decompressed_pages = NULL;
1259fe3e5914SGao Xiang 	be->compressed_pages = NULL;
1260fe3e5914SGao Xiang 	be->onstack_used = 0;
12612bfab9c0SGao Xiang 	if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
12624f05687fSGao Xiang 		be->decompressed_pages = be->onstack_pages;
12632bfab9c0SGao Xiang 		be->onstack_used = be->nr_pages;
12644f05687fSGao Xiang 		memset(be->decompressed_pages, 0,
12652bfab9c0SGao Xiang 		       sizeof(struct page *) * be->nr_pages);
1266fe3e5914SGao Xiang 	}
1267fe3e5914SGao Xiang 
1268fe3e5914SGao Xiang 	if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1269fe3e5914SGao Xiang 		be->compressed_pages = be->onstack_pages + be->onstack_used;
1270fe3e5914SGao Xiang 
1271fe3e5914SGao Xiang 	if (!be->decompressed_pages)
12724f05687fSGao Xiang 		be->decompressed_pages =
1273647dd2c3SGao Xiang 			kvcalloc(be->nr_pages, sizeof(struct page *),
1274e7368187SGao Xiang 				 GFP_KERNEL | __GFP_NOFAIL);
1275fe3e5914SGao Xiang 	if (!be->compressed_pages)
1276fe3e5914SGao Xiang 		be->compressed_pages =
1277647dd2c3SGao Xiang 			kvcalloc(pclusterpages, sizeof(struct page *),
1278fe3e5914SGao Xiang 				 GFP_KERNEL | __GFP_NOFAIL);
127947e4937aSGao Xiang 
1280267f2492SGao Xiang 	z_erofs_parse_out_bvecs(be);
12814f05687fSGao Xiang 	err2 = z_erofs_parse_in_bvecs(be, &overlapped);
12824f05687fSGao Xiang 	if (err2)
12834f05687fSGao Xiang 		err = err2;
12848d8a09b0SGao Xiang 	if (err)
128547e4937aSGao Xiang 		goto out;
128647e4937aSGao Xiang 
1287cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1288cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1289cecf864dSYue Hu 	else
1290cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1291cecf864dSYue Hu 
1292597e2953SYue Hu 	err = decompressor->decompress(&(struct z_erofs_decompress_req) {
12934f05687fSGao Xiang 					.sb = be->sb,
12944f05687fSGao Xiang 					.in = be->compressed_pages,
12954f05687fSGao Xiang 					.out = be->decompressed_pages,
1296cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
129787ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
12989f6cc76eSGao Xiang 					.inputsize = inputsize,
12992bfab9c0SGao Xiang 					.outputsize = pcl->length,
130047e4937aSGao Xiang 					.alg = pcl->algorithmformat,
130147e4937aSGao Xiang 					.inplace_io = overlapped,
13022bfab9c0SGao Xiang 					.partial_decoding = pcl->partial,
1303267f2492SGao Xiang 					.fillgaps = pcl->multibases,
13044f05687fSGao Xiang 				 }, be->pagepool);
130547e4937aSGao Xiang 
130647e4937aSGao Xiang out:
1307cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1308cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1309ed722fbcSGao Xiang 		page = pcl->compressed_bvecs[0].page;
1310ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1311cecf864dSYue Hu 		put_page(page);
1312cecf864dSYue Hu 	} else {
1313cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
1314ed722fbcSGao Xiang 			page = pcl->compressed_bvecs[i].page;
131547e4937aSGao Xiang 
131647e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
131747e4937aSGao Xiang 				continue;
131847e4937aSGao Xiang 
13196aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
13204f05687fSGao Xiang 			(void)z_erofs_put_shortlivedpage(be->pagepool, page);
1321ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
132247e4937aSGao Xiang 		}
1323cecf864dSYue Hu 	}
1324fe3e5914SGao Xiang 	if (be->compressed_pages < be->onstack_pages ||
1325fe3e5914SGao Xiang 	    be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1326647dd2c3SGao Xiang 		kvfree(be->compressed_pages);
1327267f2492SGao Xiang 	z_erofs_fill_other_copies(be, err);
132847e4937aSGao Xiang 
13292bfab9c0SGao Xiang 	for (i = 0; i < be->nr_pages; ++i) {
13304f05687fSGao Xiang 		page = be->decompressed_pages[i];
133147e4937aSGao Xiang 		if (!page)
133247e4937aSGao Xiang 			continue;
133347e4937aSGao Xiang 
13346aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
133547e4937aSGao Xiang 
13366aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
13374f05687fSGao Xiang 		if (z_erofs_put_shortlivedpage(be->pagepool, page))
133847e4937aSGao Xiang 			continue;
1339*9a05c6a8SGao Xiang 		z_erofs_onlinepage_endio(page, err);
134047e4937aSGao Xiang 	}
134147e4937aSGao Xiang 
13424f05687fSGao Xiang 	if (be->decompressed_pages != be->onstack_pages)
1343647dd2c3SGao Xiang 		kvfree(be->decompressed_pages);
134447e4937aSGao Xiang 
13452bfab9c0SGao Xiang 	pcl->length = 0;
13462bfab9c0SGao Xiang 	pcl->partial = true;
1347267f2492SGao Xiang 	pcl->multibases = false;
134806a304cdSGao Xiang 	pcl->bvset.nextpage = NULL;
134987ca34a7SGao Xiang 	pcl->vcnt = 0;
135047e4937aSGao Xiang 
135187ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
135247e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
135387ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
135447e4937aSGao Xiang 	return err;
135547e4937aSGao Xiang }
135647e4937aSGao Xiang 
13570c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1358eaa9172aSGao Xiang 				     struct page **pagepool)
135947e4937aSGao Xiang {
13604f05687fSGao Xiang 	struct z_erofs_decompress_backend be = {
13614f05687fSGao Xiang 		.sb = io->sb,
13624f05687fSGao Xiang 		.pagepool = pagepool,
1363267f2492SGao Xiang 		.decompressed_secondary_bvecs =
1364267f2492SGao Xiang 			LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
13654f05687fSGao Xiang 	};
136647e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
136747e4937aSGao Xiang 
1368967c28b2SGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL) {
136947e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
137047e4937aSGao Xiang 
13714f05687fSGao Xiang 		be.pcl = container_of(owned, struct z_erofs_pcluster, next);
13724f05687fSGao Xiang 		owned = READ_ONCE(be.pcl->next);
137347e4937aSGao Xiang 
13744f05687fSGao Xiang 		z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
13754f05687fSGao Xiang 		erofs_workgroup_put(&be.pcl->obj);
137647e4937aSGao Xiang 	}
137747e4937aSGao Xiang }
137847e4937aSGao Xiang 
13790c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
138047e4937aSGao Xiang {
1381a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1382a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1383eaa9172aSGao Xiang 	struct page *pagepool = NULL;
138447e4937aSGao Xiang 
1385967c28b2SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
13860c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
1387eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1388a4b1fab1SGao Xiang 	kvfree(bgq);
138947e4937aSGao Xiang }
139047e4937aSGao Xiang 
13913fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
13923fffb589SSandeep Dhavale static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
13933fffb589SSandeep Dhavale {
13943fffb589SSandeep Dhavale 	z_erofs_decompressqueue_work((struct work_struct *)work);
13953fffb589SSandeep Dhavale }
13963fffb589SSandeep Dhavale #endif
13973fffb589SSandeep Dhavale 
13987865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1399cdba5506SGao Xiang 				       int bios)
14007865827cSGao Xiang {
14017865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
14027865827cSGao Xiang 
14037865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
1404cdba5506SGao Xiang 	if (io->sync) {
14057865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
140660b30050SHongyu Jin 			complete(&io->u.done);
14077865827cSGao Xiang 		return;
14087865827cSGao Xiang 	}
14097865827cSGao Xiang 
14107865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
14117865827cSGao Xiang 		return;
14123fffb589SSandeep Dhavale 	/* Use (kthread_)work and sync decompression for atomic contexts only */
141312d0a24aSSandeep Dhavale 	if (!in_task() || irqs_disabled() || rcu_read_lock_any_held()) {
14143fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
14153fffb589SSandeep Dhavale 		struct kthread_worker *worker;
14163fffb589SSandeep Dhavale 
14173fffb589SSandeep Dhavale 		rcu_read_lock();
14183fffb589SSandeep Dhavale 		worker = rcu_dereference(
14193fffb589SSandeep Dhavale 				z_erofs_pcpu_workers[raw_smp_processor_id()]);
14203fffb589SSandeep Dhavale 		if (!worker) {
14213fffb589SSandeep Dhavale 			INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
14227865827cSGao Xiang 			queue_work(z_erofs_workqueue, &io->u.work);
14233fffb589SSandeep Dhavale 		} else {
14243fffb589SSandeep Dhavale 			kthread_queue_work(worker, &io->u.kthread_work);
14253fffb589SSandeep Dhavale 		}
14263fffb589SSandeep Dhavale 		rcu_read_unlock();
14273fffb589SSandeep Dhavale #else
14283fffb589SSandeep Dhavale 		queue_work(z_erofs_workqueue, &io->u.work);
14293fffb589SSandeep Dhavale #endif
14307865827cSGao Xiang 		/* enable sync decompression for readahead */
14317865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
14327865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
14337865827cSGao Xiang 		return;
14347865827cSGao Xiang 	}
14357865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
14367865827cSGao Xiang }
14377865827cSGao Xiang 
143847e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
143947e4937aSGao Xiang 					       unsigned int nr,
1440eaa9172aSGao Xiang 					       struct page **pagepool,
14419f2731d6SGao Xiang 					       struct address_space *mc)
144247e4937aSGao Xiang {
144347e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
14449f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
144547e4937aSGao Xiang 	bool tocache = false;
144647e4937aSGao Xiang 
144747e4937aSGao Xiang 	struct address_space *mapping;
144847e4937aSGao Xiang 	struct page *oldpage, *page;
144947e4937aSGao Xiang 	int justfound;
145047e4937aSGao Xiang 
145147e4937aSGao Xiang repeat:
1452ed722fbcSGao Xiang 	page = READ_ONCE(pcl->compressed_bvecs[nr].page);
145347e4937aSGao Xiang 	oldpage = page;
145447e4937aSGao Xiang 
145547e4937aSGao Xiang 	if (!page)
145647e4937aSGao Xiang 		goto out_allocpage;
145747e4937aSGao Xiang 
1458b1ed220cSGao Xiang 	justfound = (unsigned long)page & 1UL;
1459b1ed220cSGao Xiang 	page = (struct page *)((unsigned long)page & ~1UL);
146047e4937aSGao Xiang 
14611825c8d7SGao Xiang 	/*
14621825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
14631825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
14641825c8d7SGao Xiang 	 */
14651825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1466ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
14671825c8d7SGao Xiang 		set_page_private(page, 0);
14681825c8d7SGao Xiang 		tocache = true;
14691825c8d7SGao Xiang 		goto out_tocache;
14701825c8d7SGao Xiang 	}
147147e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
147247e4937aSGao Xiang 
147347e4937aSGao Xiang 	/*
14746aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
147547e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
147647e4937aSGao Xiang 	 */
147747e4937aSGao Xiang 	if (mapping && mapping != mc)
147847e4937aSGao Xiang 		/* ought to be unmanaged pages */
147947e4937aSGao Xiang 		goto out;
148047e4937aSGao Xiang 
14816aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
14826aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
14836aaa7b06SGao Xiang 		goto out;
14846aaa7b06SGao Xiang 
148547e4937aSGao Xiang 	lock_page(page);
148647e4937aSGao Xiang 
148747e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
148847e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
148947e4937aSGao Xiang 
149047e4937aSGao Xiang 	/* the page is still in manage cache */
149147e4937aSGao Xiang 	if (page->mapping == mc) {
1492ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
149347e4937aSGao Xiang 
149447e4937aSGao Xiang 		if (!PagePrivate(page)) {
149547e4937aSGao Xiang 			/*
149647e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
149747e4937aSGao Xiang 			 * the current restriction as well if
1498ed722fbcSGao Xiang 			 * the page is already in compressed_bvecs[].
149947e4937aSGao Xiang 			 */
150047e4937aSGao Xiang 			DBG_BUGON(!justfound);
150147e4937aSGao Xiang 
150247e4937aSGao Xiang 			justfound = 0;
150347e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
150447e4937aSGao Xiang 			SetPagePrivate(page);
150547e4937aSGao Xiang 		}
150647e4937aSGao Xiang 
150747e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
150847e4937aSGao Xiang 		if (PageUptodate(page)) {
150947e4937aSGao Xiang 			unlock_page(page);
151047e4937aSGao Xiang 			page = NULL;
151147e4937aSGao Xiang 		}
151247e4937aSGao Xiang 		goto out;
151347e4937aSGao Xiang 	}
151447e4937aSGao Xiang 
151547e4937aSGao Xiang 	/*
151647e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
151747e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
151847e4937aSGao Xiang 	 */
151947e4937aSGao Xiang 	DBG_BUGON(page->mapping);
152047e4937aSGao Xiang 	DBG_BUGON(!justfound);
152147e4937aSGao Xiang 
152247e4937aSGao Xiang 	tocache = true;
152347e4937aSGao Xiang 	unlock_page(page);
152447e4937aSGao Xiang 	put_page(page);
152547e4937aSGao Xiang out_allocpage:
15265ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1527ed722fbcSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1528ed722fbcSGao Xiang 			       oldpage, page)) {
1529eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
15305ddcee1fSGao Xiang 		cond_resched();
15315ddcee1fSGao Xiang 		goto repeat;
15325ddcee1fSGao Xiang 	}
15331825c8d7SGao Xiang out_tocache:
1534bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1535bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1536bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1537bf225074SGao Xiang 		goto out;
1538a30573b3SGao Xiang 	}
1539bf225074SGao Xiang 	attach_page_private(page, pcl);
1540bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1541bf225074SGao Xiang 	put_page(page);
1542bf225074SGao Xiang 
154347e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
154447e4937aSGao Xiang 	return page;
154547e4937aSGao Xiang }
154647e4937aSGao Xiang 
1547cdba5506SGao Xiang static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1548a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
154947e4937aSGao Xiang {
1550a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
155147e4937aSGao Xiang 
1552a4b1fab1SGao Xiang 	if (fg && !*fg) {
1553a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1554a4b1fab1SGao Xiang 		if (!q) {
1555a4b1fab1SGao Xiang 			*fg = true;
1556a4b1fab1SGao Xiang 			goto fg_out;
155747e4937aSGao Xiang 		}
15583fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
15593fffb589SSandeep Dhavale 		kthread_init_work(&q->u.kthread_work,
15603fffb589SSandeep Dhavale 				  z_erofs_decompressqueue_kthread_work);
15613fffb589SSandeep Dhavale #else
15620c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
15633fffb589SSandeep Dhavale #endif
1564a4b1fab1SGao Xiang 	} else {
1565a4b1fab1SGao Xiang fg_out:
1566a4b1fab1SGao Xiang 		q = fgq;
156760b30050SHongyu Jin 		init_completion(&fgq->u.done);
1568a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
156967148551SGao Xiang 		q->eio = false;
1570cdba5506SGao Xiang 		q->sync = true;
1571a4b1fab1SGao Xiang 	}
1572a4b1fab1SGao Xiang 	q->sb = sb;
1573967c28b2SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL;
1574a4b1fab1SGao Xiang 	return q;
157547e4937aSGao Xiang }
157647e4937aSGao Xiang 
157747e4937aSGao Xiang /* define decompression jobqueue types */
157847e4937aSGao Xiang enum {
157947e4937aSGao Xiang 	JQ_BYPASS,
158047e4937aSGao Xiang 	JQ_SUBMIT,
158147e4937aSGao Xiang 	NR_JOBQUEUES,
158247e4937aSGao Xiang };
158347e4937aSGao Xiang 
158447e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
158547e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
158647e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
158747e4937aSGao Xiang {
158847e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
158947e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
159047e4937aSGao Xiang 
1591967c28b2SGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
159247e4937aSGao Xiang 
159347e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
159447e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
159547e4937aSGao Xiang 
159647e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
159747e4937aSGao Xiang }
159847e4937aSGao Xiang 
15997865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
16007865827cSGao Xiang {
1601cdba5506SGao Xiang 	struct z_erofs_decompressqueue *q = bio->bi_private;
16027865827cSGao Xiang 	blk_status_t err = bio->bi_status;
16037865827cSGao Xiang 	struct bio_vec *bvec;
16047865827cSGao Xiang 	struct bvec_iter_all iter_all;
16057865827cSGao Xiang 
16067865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
16077865827cSGao Xiang 		struct page *page = bvec->bv_page;
16087865827cSGao Xiang 
16097865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
16107865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
16117865827cSGao Xiang 
16127865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
16137865827cSGao Xiang 			if (!err)
16147865827cSGao Xiang 				SetPageUptodate(page);
16157865827cSGao Xiang 			unlock_page(page);
16167865827cSGao Xiang 		}
16177865827cSGao Xiang 	}
161867148551SGao Xiang 	if (err)
161967148551SGao Xiang 		q->eio = true;
1620cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q, -1);
16217865827cSGao Xiang 	bio_put(bio);
16227865827cSGao Xiang }
16237865827cSGao Xiang 
162483a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1625a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1626ef4b4b46SYue Hu 				 bool *force_fg, bool readahead)
162747e4937aSGao Xiang {
162883a386c0SGao Xiang 	struct super_block *sb = f->inode->i_sb;
162983a386c0SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
163047e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1631a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
16325c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1633dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
16343f649ab7SKees Cook 	pgoff_t last_index;
1635dfeab2e9SGao Xiang 	struct block_device *last_bdev;
16361e4a2955SGao Xiang 	unsigned int nr_bios = 0;
16371e4a2955SGao Xiang 	struct bio *bio = NULL;
163882e60d00SJohannes Weiner 	unsigned long pflags;
163982e60d00SJohannes Weiner 	int memstall = 0;
164047e4937aSGao Xiang 
1641cdba5506SGao Xiang 	/*
1642cdba5506SGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
1643cdba5506SGao Xiang 	 * no need to read from device for all pclusters in this queue.
1644cdba5506SGao Xiang 	 */
1645cdba5506SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1646cdba5506SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1647cdba5506SGao Xiang 
1648a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1649a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
165047e4937aSGao Xiang 
165147e4937aSGao Xiang 	/* by default, all need io submission */
165247e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
165347e4937aSGao Xiang 
165447e4937aSGao Xiang 	do {
1655dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
165647e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
16571e4a2955SGao Xiang 		pgoff_t cur, end;
16581e4a2955SGao Xiang 		unsigned int i = 0;
16591e4a2955SGao Xiang 		bool bypass = true;
166047e4937aSGao Xiang 
166147e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
166247e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1663967c28b2SGao Xiang 		owned_head = READ_ONCE(pcl->next);
166447e4937aSGao Xiang 
1665cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1666cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1667cecf864dSYue Hu 			continue;
1668cecf864dSYue Hu 		}
1669cecf864dSYue Hu 
1670dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1671dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
16723acea5fcSJingbo Xu 			.m_pa = erofs_pos(sb, pcl->obj.index),
1673dfeab2e9SGao Xiang 		};
1674dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1675dfeab2e9SGao Xiang 
16763acea5fcSJingbo Xu 		cur = erofs_blknr(sb, mdev.m_pa);
16779f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
167847e4937aSGao Xiang 
16791e4a2955SGao Xiang 		do {
16801e4a2955SGao Xiang 			struct page *page;
168147e4937aSGao Xiang 
16826ab5eed6SGao Xiang 			page = pickup_page_for_submission(pcl, i++,
16836ab5eed6SGao Xiang 					&f->pagepool, mc);
16841e4a2955SGao Xiang 			if (!page)
16851e4a2955SGao Xiang 				continue;
168647e4937aSGao Xiang 
1687dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1688dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
168947e4937aSGao Xiang submit_bio_retry:
169094e4e153SGao Xiang 				submit_bio(bio);
169182e60d00SJohannes Weiner 				if (memstall) {
169282e60d00SJohannes Weiner 					psi_memstall_leave(&pflags);
169382e60d00SJohannes Weiner 					memstall = 0;
169482e60d00SJohannes Weiner 				}
169547e4937aSGao Xiang 				bio = NULL;
169647e4937aSGao Xiang 			}
169747e4937aSGao Xiang 
169882e60d00SJohannes Weiner 			if (unlikely(PageWorkingset(page)) && !memstall) {
169999486c51SChristoph Hellwig 				psi_memstall_enter(&pflags);
170082e60d00SJohannes Weiner 				memstall = 1;
170182e60d00SJohannes Weiner 			}
170299486c51SChristoph Hellwig 
170347e4937aSGao Xiang 			if (!bio) {
170407888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
170507888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
17060c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1707dfeab2e9SGao Xiang 
1708dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
17091e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
17103acea5fcSJingbo Xu 					(sb->s_blocksize_bits - 9);
1711cdba5506SGao Xiang 				bio->bi_private = q[JQ_SUBMIT];
1712ef4b4b46SYue Hu 				if (readahead)
17136ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
171447e4937aSGao Xiang 				++nr_bios;
171547e4937aSGao Xiang 			}
171647e4937aSGao Xiang 
17176c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
171847e4937aSGao Xiang 				goto submit_bio_retry;
171947e4937aSGao Xiang 
17201e4a2955SGao Xiang 			last_index = cur;
17211e4a2955SGao Xiang 			bypass = false;
17221e4a2955SGao Xiang 		} while (++cur < end);
172347e4937aSGao Xiang 
17241e4a2955SGao Xiang 		if (!bypass)
172547e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
172647e4937aSGao Xiang 		else
172747e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
172847e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
172947e4937aSGao Xiang 
173099486c51SChristoph Hellwig 	if (bio) {
173194e4e153SGao Xiang 		submit_bio(bio);
173282e60d00SJohannes Weiner 		if (memstall)
173382e60d00SJohannes Weiner 			psi_memstall_leave(&pflags);
173499486c51SChristoph Hellwig 	}
173547e4937aSGao Xiang 
1736587a67b7SGao Xiang 	/*
1737587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
17383fffb589SSandeep Dhavale 	 * don't issue decompression but drop it directly instead.
1739587a67b7SGao Xiang 	 */
1740587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1741587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
17421e4a2955SGao Xiang 		return;
1743587a67b7SGao Xiang 	}
1744cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
174547e4937aSGao Xiang }
174647e4937aSGao Xiang 
174783a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
17486ab5eed6SGao Xiang 			     bool force_fg, bool ra)
174947e4937aSGao Xiang {
1750a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
175147e4937aSGao Xiang 
17525c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
175347e4937aSGao Xiang 		return;
17546ab5eed6SGao Xiang 	z_erofs_submit_queue(f, io, &force_fg, ra);
175547e4937aSGao Xiang 
17560c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
17576ab5eed6SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
175847e4937aSGao Xiang 
175947e4937aSGao Xiang 	if (!force_fg)
176047e4937aSGao Xiang 		return;
176147e4937aSGao Xiang 
176247e4937aSGao Xiang 	/* wait until all bios are completed */
176360b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
176447e4937aSGao Xiang 
17650c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
17666ab5eed6SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool);
176747e4937aSGao Xiang }
176847e4937aSGao Xiang 
176938629291SGao Xiang /*
177038629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
177138629291SGao Xiang  * approximate readmore strategies as a start.
177238629291SGao Xiang  */
177338629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
17746ab5eed6SGao Xiang 		struct readahead_control *rac, bool backmost)
177538629291SGao Xiang {
177638629291SGao Xiang 	struct inode *inode = f->inode;
177738629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
1778796e9149SYue Hu 	erofs_off_t cur, end, headoffset = f->headoffset;
177938629291SGao Xiang 	int err;
178038629291SGao Xiang 
178138629291SGao Xiang 	if (backmost) {
1782796e9149SYue Hu 		if (rac)
1783796e9149SYue Hu 			end = headoffset + readahead_length(rac) - 1;
1784796e9149SYue Hu 		else
1785796e9149SYue Hu 			end = headoffset + PAGE_SIZE - 1;
178638629291SGao Xiang 		map->m_la = end;
1787622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1788622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
178938629291SGao Xiang 		if (err)
179038629291SGao Xiang 			return;
179138629291SGao Xiang 
1792796e9149SYue Hu 		/* expand ra for the trailing edge if readahead */
179338629291SGao Xiang 		if (rac) {
179438629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1795796e9149SYue Hu 			readahead_expand(rac, headoffset, cur - headoffset);
179638629291SGao Xiang 			return;
179738629291SGao Xiang 		}
179838629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
179938629291SGao Xiang 	} else {
180038629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
180138629291SGao Xiang 
180238629291SGao Xiang 		if (!map->m_llen)
180338629291SGao Xiang 			return;
180438629291SGao Xiang 	}
180538629291SGao Xiang 
180638629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
1807936aa701SChunhai Guo 	while ((cur >= end) && (cur < i_size_read(inode))) {
180838629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
180938629291SGao Xiang 		struct page *page;
181038629291SGao Xiang 
181138629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1812aa793b46SGao Xiang 		if (page) {
1813e3157bb5SFerry Meng 			if (PageUptodate(page))
181438629291SGao Xiang 				unlock_page(page);
1815e3157bb5SFerry Meng 			else
1816e3157bb5SFerry Meng 				(void)z_erofs_do_read_page(f, page);
181738629291SGao Xiang 			put_page(page);
1818aa793b46SGao Xiang 		}
1819aa793b46SGao Xiang 
182038629291SGao Xiang 		if (cur < PAGE_SIZE)
182138629291SGao Xiang 			break;
182238629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
182338629291SGao Xiang 	}
182438629291SGao Xiang }
182538629291SGao Xiang 
1826a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio)
182747e4937aSGao Xiang {
1828a2e20a25SMatthew Wilcox (Oracle) 	struct page *page = &folio->page;
182947e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
183040452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
183147e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
183247e4937aSGao Xiang 	int err;
183347e4937aSGao Xiang 
183447e4937aSGao Xiang 	trace_erofs_readpage(page, false);
183547e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
183647e4937aSGao Xiang 
18376ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, true);
18386ab5eed6SGao Xiang 	err = z_erofs_do_read_page(&f, page);
18396ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, false);
1840dcba1b23SGao Xiang 	z_erofs_pcluster_end(&f);
184147e4937aSGao Xiang 
184247e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
18436ab5eed6SGao Xiang 	z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, 0), false);
184447e4937aSGao Xiang 
1845e3157bb5SFerry Meng 	if (err && err != -EINTR)
1846e3157bb5SFerry Meng 		erofs_err(inode->i_sb, "read error %d @ %lu of nid %llu",
1847e3157bb5SFerry Meng 			  err, folio->index, EROFS_I(inode)->nid);
184847e4937aSGao Xiang 
184909c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
18506ab5eed6SGao Xiang 	erofs_release_pages(&f.pagepool);
185147e4937aSGao Xiang 	return err;
185247e4937aSGao Xiang }
185347e4937aSGao Xiang 
18540615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
185547e4937aSGao Xiang {
18560615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
185747e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
185847e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
18596ab5eed6SGao Xiang 	struct page *head = NULL, *page;
186038629291SGao Xiang 	unsigned int nr_pages;
186147e4937aSGao Xiang 
18620615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
186347e4937aSGao Xiang 
18646ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, true);
186538629291SGao Xiang 	nr_pages = readahead_count(rac);
186638629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
186738629291SGao Xiang 
18680615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
186947e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
187047e4937aSGao Xiang 		head = page;
187147e4937aSGao Xiang 	}
187247e4937aSGao Xiang 
187347e4937aSGao Xiang 	while (head) {
187447e4937aSGao Xiang 		struct page *page = head;
187547e4937aSGao Xiang 		int err;
187647e4937aSGao Xiang 
187747e4937aSGao Xiang 		/* traversal in reverse order */
187847e4937aSGao Xiang 		head = (void *)page_private(page);
187947e4937aSGao Xiang 
18806ab5eed6SGao Xiang 		err = z_erofs_do_read_page(&f, page);
1881e3157bb5SFerry Meng 		if (err && err != -EINTR)
1882e3157bb5SFerry Meng 			erofs_err(inode->i_sb, "readahead error %d @ %lu of nid %llu",
1883e3157bb5SFerry Meng 				  err, page->index, EROFS_I(inode)->nid);
188447e4937aSGao Xiang 		put_page(page);
188547e4937aSGao Xiang 	}
18866ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, false);
1887dcba1b23SGao Xiang 	z_erofs_pcluster_end(&f);
188847e4937aSGao Xiang 
18896ab5eed6SGao Xiang 	z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, nr_pages), true);
189009c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
18916ab5eed6SGao Xiang 	erofs_release_pages(&f.pagepool);
189247e4937aSGao Xiang }
189347e4937aSGao Xiang 
18940c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
1895a2e20a25SMatthew Wilcox (Oracle) 	.read_folio = z_erofs_read_folio,
18960615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
189747e4937aSGao Xiang };
1898