xref: /openbmc/linux/fs/erofs/zdata.c (revision 967c28b2)
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 
94a9a94d93SGao Xiang /* let's avoid the valid 32-bit kernel addresses */
95a9a94d93SGao Xiang 
96*967c28b2SGao Xiang /* the end of a chain of pclusters */
97a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_TAIL           ((void *)0x5F0ECAFE)
98a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_NIL            (NULL)
99a9a94d93SGao Xiang 
100a9a94d93SGao Xiang struct z_erofs_decompressqueue {
101a9a94d93SGao Xiang 	struct super_block *sb;
102a9a94d93SGao Xiang 	atomic_t pending_bios;
103a9a94d93SGao Xiang 	z_erofs_next_pcluster_t head;
104a9a94d93SGao Xiang 
105a9a94d93SGao Xiang 	union {
106a9a94d93SGao Xiang 		struct completion done;
107a9a94d93SGao Xiang 		struct work_struct work;
1083fffb589SSandeep Dhavale 		struct kthread_work kthread_work;
109a9a94d93SGao Xiang 	} u;
110a9a94d93SGao Xiang 	bool eio, sync;
111a9a94d93SGao Xiang };
112a9a94d93SGao Xiang 
113a9a94d93SGao Xiang static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
114a9a94d93SGao Xiang {
115a9a94d93SGao Xiang 	return !pcl->obj.index;
116a9a94d93SGao Xiang }
117a9a94d93SGao Xiang 
118a9a94d93SGao Xiang static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
119a9a94d93SGao Xiang {
120a9a94d93SGao Xiang 	if (z_erofs_is_inline_pcluster(pcl))
121a9a94d93SGao Xiang 		return 1;
122a9a94d93SGao Xiang 	return pcl->pclusterpages;
123a9a94d93SGao Xiang }
124a9a94d93SGao Xiang 
125a9a94d93SGao Xiang /*
126a9a94d93SGao Xiang  * bit 30: I/O error occurred on this page
127a9a94d93SGao Xiang  * bit 0 - 29: remaining parts to complete this page
128a9a94d93SGao Xiang  */
129a9a94d93SGao Xiang #define Z_EROFS_PAGE_EIO			(1 << 30)
130a9a94d93SGao Xiang 
131a9a94d93SGao Xiang static inline void z_erofs_onlinepage_init(struct page *page)
132a9a94d93SGao Xiang {
133a9a94d93SGao Xiang 	union {
134a9a94d93SGao Xiang 		atomic_t o;
135a9a94d93SGao Xiang 		unsigned long v;
136a9a94d93SGao Xiang 	} u = { .o = ATOMIC_INIT(1) };
137a9a94d93SGao Xiang 
138a9a94d93SGao Xiang 	set_page_private(page, u.v);
139a9a94d93SGao Xiang 	smp_wmb();
140a9a94d93SGao Xiang 	SetPagePrivate(page);
141a9a94d93SGao Xiang }
142a9a94d93SGao Xiang 
143a9a94d93SGao Xiang static inline void z_erofs_onlinepage_split(struct page *page)
144a9a94d93SGao Xiang {
145a9a94d93SGao Xiang 	atomic_inc((atomic_t *)&page->private);
146a9a94d93SGao Xiang }
147a9a94d93SGao Xiang 
148a9a94d93SGao Xiang static inline void z_erofs_page_mark_eio(struct page *page)
149a9a94d93SGao Xiang {
150a9a94d93SGao Xiang 	int orig;
151a9a94d93SGao Xiang 
152a9a94d93SGao Xiang 	do {
153a9a94d93SGao Xiang 		orig = atomic_read((atomic_t *)&page->private);
154a9a94d93SGao Xiang 	} while (atomic_cmpxchg((atomic_t *)&page->private, orig,
155a9a94d93SGao Xiang 				orig | Z_EROFS_PAGE_EIO) != orig);
156a9a94d93SGao Xiang }
157a9a94d93SGao Xiang 
158a9a94d93SGao Xiang static inline void z_erofs_onlinepage_endio(struct page *page)
159a9a94d93SGao Xiang {
160a9a94d93SGao Xiang 	unsigned int v;
161a9a94d93SGao Xiang 
162a9a94d93SGao Xiang 	DBG_BUGON(!PagePrivate(page));
163a9a94d93SGao Xiang 	v = atomic_dec_return((atomic_t *)&page->private);
164a9a94d93SGao Xiang 	if (!(v & ~Z_EROFS_PAGE_EIO)) {
165a9a94d93SGao Xiang 		set_page_private(page, 0);
166a9a94d93SGao Xiang 		ClearPagePrivate(page);
167a9a94d93SGao Xiang 		if (!(v & Z_EROFS_PAGE_EIO))
168a9a94d93SGao Xiang 			SetPageUptodate(page);
169a9a94d93SGao Xiang 		unlock_page(page);
170a9a94d93SGao Xiang 	}
171a9a94d93SGao Xiang }
172a9a94d93SGao Xiang 
173a9a94d93SGao Xiang #define Z_EROFS_ONSTACK_PAGES		32
174a9a94d93SGao Xiang 
17547e4937aSGao Xiang /*
1769f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
1779f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
1789f6cc76eSGao Xiang  */
1799f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
1809f6cc76eSGao Xiang 	struct kmem_cache *slab;
1819f6cc76eSGao Xiang 	unsigned int maxpages;
1829f6cc76eSGao Xiang 	char name[48];
1839f6cc76eSGao Xiang };
1849f6cc76eSGao Xiang 
1859f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
1869f6cc76eSGao Xiang 
1879f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
1889f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
1899f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
1909f6cc76eSGao Xiang };
1919f6cc76eSGao Xiang 
19206a304cdSGao Xiang struct z_erofs_bvec_iter {
19306a304cdSGao Xiang 	struct page *bvpage;
19406a304cdSGao Xiang 	struct z_erofs_bvset *bvset;
19506a304cdSGao Xiang 	unsigned int nr, cur;
19606a304cdSGao Xiang };
19706a304cdSGao Xiang 
19806a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
19906a304cdSGao Xiang {
20006a304cdSGao Xiang 	if (iter->bvpage)
20106a304cdSGao Xiang 		kunmap_local(iter->bvset);
20206a304cdSGao Xiang 	return iter->bvpage;
20306a304cdSGao Xiang }
20406a304cdSGao Xiang 
20506a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
20606a304cdSGao Xiang {
20706a304cdSGao Xiang 	unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
20806a304cdSGao Xiang 	/* have to access nextpage in advance, otherwise it will be unmapped */
20906a304cdSGao Xiang 	struct page *nextpage = iter->bvset->nextpage;
21006a304cdSGao Xiang 	struct page *oldpage;
21106a304cdSGao Xiang 
21206a304cdSGao Xiang 	DBG_BUGON(!nextpage);
21306a304cdSGao Xiang 	oldpage = z_erofs_bvec_iter_end(iter);
21406a304cdSGao Xiang 	iter->bvpage = nextpage;
21506a304cdSGao Xiang 	iter->bvset = kmap_local_page(nextpage);
21606a304cdSGao Xiang 	iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
21706a304cdSGao Xiang 	iter->cur = 0;
21806a304cdSGao Xiang 	return oldpage;
21906a304cdSGao Xiang }
22006a304cdSGao Xiang 
22106a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
22206a304cdSGao Xiang 				    struct z_erofs_bvset_inline *bvset,
22306a304cdSGao Xiang 				    unsigned int bootstrap_nr,
22406a304cdSGao Xiang 				    unsigned int cur)
22506a304cdSGao Xiang {
22606a304cdSGao Xiang 	*iter = (struct z_erofs_bvec_iter) {
22706a304cdSGao Xiang 		.nr = bootstrap_nr,
22806a304cdSGao Xiang 		.bvset = (struct z_erofs_bvset *)bvset,
22906a304cdSGao Xiang 	};
23006a304cdSGao Xiang 
23106a304cdSGao Xiang 	while (cur > iter->nr) {
23206a304cdSGao Xiang 		cur -= iter->nr;
23306a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
23406a304cdSGao Xiang 	}
23506a304cdSGao Xiang 	iter->cur = cur;
23606a304cdSGao Xiang }
23706a304cdSGao Xiang 
23806a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
23906a304cdSGao Xiang 				struct z_erofs_bvec *bvec,
2406ab5eed6SGao Xiang 				struct page **candidate_bvpage,
2416ab5eed6SGao Xiang 				struct page **pagepool)
24206a304cdSGao Xiang {
24305b63d2bSGao Xiang 	if (iter->cur >= iter->nr) {
24405b63d2bSGao Xiang 		struct page *nextpage = *candidate_bvpage;
24506a304cdSGao Xiang 
24605b63d2bSGao Xiang 		if (!nextpage) {
2476ab5eed6SGao Xiang 			nextpage = erofs_allocpage(pagepool, GFP_NOFS);
24805b63d2bSGao Xiang 			if (!nextpage)
24905b63d2bSGao Xiang 				return -ENOMEM;
25005b63d2bSGao Xiang 			set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
25105b63d2bSGao Xiang 		}
25206a304cdSGao Xiang 		DBG_BUGON(iter->bvset->nextpage);
25305b63d2bSGao Xiang 		iter->bvset->nextpage = nextpage;
25406a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
25506a304cdSGao Xiang 
25606a304cdSGao Xiang 		iter->bvset->nextpage = NULL;
25706a304cdSGao Xiang 		*candidate_bvpage = NULL;
25806a304cdSGao Xiang 	}
25906a304cdSGao Xiang 	iter->bvset->bvec[iter->cur++] = *bvec;
26006a304cdSGao Xiang 	return 0;
26106a304cdSGao Xiang }
26206a304cdSGao Xiang 
26306a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
26406a304cdSGao Xiang 				 struct z_erofs_bvec *bvec,
26506a304cdSGao Xiang 				 struct page **old_bvpage)
26606a304cdSGao Xiang {
26706a304cdSGao Xiang 	if (iter->cur == iter->nr)
26806a304cdSGao Xiang 		*old_bvpage = z_erofs_bvset_flip(iter);
26906a304cdSGao Xiang 	else
27006a304cdSGao Xiang 		*old_bvpage = NULL;
27106a304cdSGao Xiang 	*bvec = iter->bvset->bvec[iter->cur++];
27206a304cdSGao Xiang }
27306a304cdSGao Xiang 
2749f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
2759f6cc76eSGao Xiang {
2769f6cc76eSGao Xiang 	int i;
2779f6cc76eSGao Xiang 
2789f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
2799f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
2809f6cc76eSGao Xiang 			continue;
2819f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
2829f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
2839f6cc76eSGao Xiang 	}
2849f6cc76eSGao Xiang }
2859f6cc76eSGao Xiang 
2869f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
2879f6cc76eSGao Xiang {
2889f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
2899f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
2909f6cc76eSGao Xiang 	unsigned int size;
2919f6cc76eSGao Xiang 
2929f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
2939f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
294ed722fbcSGao Xiang 		size = struct_size(a, compressed_bvecs, pcs->maxpages);
2959f6cc76eSGao Xiang 
2969f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
2979f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
2989f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
2999f6cc76eSGao Xiang 		if (pcs->slab)
3009f6cc76eSGao Xiang 			continue;
3019f6cc76eSGao Xiang 
3029f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
3039f6cc76eSGao Xiang 		return -ENOMEM;
3049f6cc76eSGao Xiang 	}
3059f6cc76eSGao Xiang 	return 0;
3069f6cc76eSGao Xiang }
3079f6cc76eSGao Xiang 
3089f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
3099f6cc76eSGao Xiang {
3109f6cc76eSGao Xiang 	int i;
3119f6cc76eSGao Xiang 
3129f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3139f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3149f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
3159f6cc76eSGao Xiang 
3169f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
3179f6cc76eSGao Xiang 			continue;
3189f6cc76eSGao Xiang 
3199f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
3209f6cc76eSGao Xiang 		if (!pcl)
3219f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
3229f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
3239f6cc76eSGao Xiang 		return pcl;
3249f6cc76eSGao Xiang 	}
3259f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
3269f6cc76eSGao Xiang }
3279f6cc76eSGao Xiang 
3289f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
3299f6cc76eSGao Xiang {
330cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
3319f6cc76eSGao Xiang 	int i;
3329f6cc76eSGao Xiang 
3339f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3349f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3359f6cc76eSGao Xiang 
336cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
3379f6cc76eSGao Xiang 			continue;
3389f6cc76eSGao Xiang 
3399f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
3409f6cc76eSGao Xiang 		return;
3419f6cc76eSGao Xiang 	}
3429f6cc76eSGao Xiang 	DBG_BUGON(1);
3439f6cc76eSGao Xiang }
3449f6cc76eSGao Xiang 
34547e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
34647e4937aSGao Xiang 
3473fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
3483fffb589SSandeep Dhavale static struct kthread_worker __rcu **z_erofs_pcpu_workers;
3493fffb589SSandeep Dhavale 
3503fffb589SSandeep Dhavale static void erofs_destroy_percpu_workers(void)
35147e4937aSGao Xiang {
3523fffb589SSandeep Dhavale 	struct kthread_worker *worker;
3533fffb589SSandeep Dhavale 	unsigned int cpu;
3543fffb589SSandeep Dhavale 
3553fffb589SSandeep Dhavale 	for_each_possible_cpu(cpu) {
3563fffb589SSandeep Dhavale 		worker = rcu_dereference_protected(
3573fffb589SSandeep Dhavale 					z_erofs_pcpu_workers[cpu], 1);
3583fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
3593fffb589SSandeep Dhavale 		if (worker)
3603fffb589SSandeep Dhavale 			kthread_destroy_worker(worker);
3613fffb589SSandeep Dhavale 	}
3623fffb589SSandeep Dhavale 	kfree(z_erofs_pcpu_workers);
36347e4937aSGao Xiang }
36447e4937aSGao Xiang 
3653fffb589SSandeep Dhavale static struct kthread_worker *erofs_init_percpu_worker(int cpu)
36647e4937aSGao Xiang {
3673fffb589SSandeep Dhavale 	struct kthread_worker *worker =
3683fffb589SSandeep Dhavale 		kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
36947e4937aSGao Xiang 
3703fffb589SSandeep Dhavale 	if (IS_ERR(worker))
3713fffb589SSandeep Dhavale 		return worker;
3723fffb589SSandeep Dhavale 	if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
3733fffb589SSandeep Dhavale 		sched_set_fifo_low(worker->task);
3743fffb589SSandeep Dhavale 	return worker;
3753fffb589SSandeep Dhavale }
3763fffb589SSandeep Dhavale 
3773fffb589SSandeep Dhavale static int erofs_init_percpu_workers(void)
3783fffb589SSandeep Dhavale {
3793fffb589SSandeep Dhavale 	struct kthread_worker *worker;
3803fffb589SSandeep Dhavale 	unsigned int cpu;
3813fffb589SSandeep Dhavale 
3823fffb589SSandeep Dhavale 	z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
3833fffb589SSandeep Dhavale 			sizeof(struct kthread_worker *), GFP_ATOMIC);
3843fffb589SSandeep Dhavale 	if (!z_erofs_pcpu_workers)
3853fffb589SSandeep Dhavale 		return -ENOMEM;
3863fffb589SSandeep Dhavale 
3873fffb589SSandeep Dhavale 	for_each_online_cpu(cpu) {	/* could miss cpu{off,on}line? */
3883fffb589SSandeep Dhavale 		worker = erofs_init_percpu_worker(cpu);
3893fffb589SSandeep Dhavale 		if (!IS_ERR(worker))
3903fffb589SSandeep Dhavale 			rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
3913fffb589SSandeep Dhavale 	}
3923fffb589SSandeep Dhavale 	return 0;
3933fffb589SSandeep Dhavale }
3943fffb589SSandeep Dhavale #else
3953fffb589SSandeep Dhavale static inline void erofs_destroy_percpu_workers(void) {}
3963fffb589SSandeep Dhavale static inline int erofs_init_percpu_workers(void) { return 0; }
3973fffb589SSandeep Dhavale #endif
3983fffb589SSandeep Dhavale 
3993fffb589SSandeep Dhavale #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
4003fffb589SSandeep Dhavale static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
4013fffb589SSandeep Dhavale static enum cpuhp_state erofs_cpuhp_state;
4023fffb589SSandeep Dhavale 
4033fffb589SSandeep Dhavale static int erofs_cpu_online(unsigned int cpu)
4043fffb589SSandeep Dhavale {
4053fffb589SSandeep Dhavale 	struct kthread_worker *worker, *old;
4063fffb589SSandeep Dhavale 
4073fffb589SSandeep Dhavale 	worker = erofs_init_percpu_worker(cpu);
4083fffb589SSandeep Dhavale 	if (IS_ERR(worker))
4093fffb589SSandeep Dhavale 		return PTR_ERR(worker);
4103fffb589SSandeep Dhavale 
4113fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
4123fffb589SSandeep Dhavale 	old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
4133fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
4143fffb589SSandeep Dhavale 	if (!old)
4153fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
4163fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
4173fffb589SSandeep Dhavale 	if (old)
4183fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
4193fffb589SSandeep Dhavale 	return 0;
4203fffb589SSandeep Dhavale }
4213fffb589SSandeep Dhavale 
4223fffb589SSandeep Dhavale static int erofs_cpu_offline(unsigned int cpu)
4233fffb589SSandeep Dhavale {
4243fffb589SSandeep Dhavale 	struct kthread_worker *worker;
4253fffb589SSandeep Dhavale 
4263fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
4273fffb589SSandeep Dhavale 	worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
4283fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
4293fffb589SSandeep Dhavale 	rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
4303fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
4313fffb589SSandeep Dhavale 
4323fffb589SSandeep Dhavale 	synchronize_rcu();
4333fffb589SSandeep Dhavale 	if (worker)
4343fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
4353fffb589SSandeep Dhavale 	return 0;
4363fffb589SSandeep Dhavale }
4373fffb589SSandeep Dhavale 
4383fffb589SSandeep Dhavale static int erofs_cpu_hotplug_init(void)
4393fffb589SSandeep Dhavale {
4403fffb589SSandeep Dhavale 	int state;
4413fffb589SSandeep Dhavale 
4423fffb589SSandeep Dhavale 	state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
4433fffb589SSandeep Dhavale 			"fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
4443fffb589SSandeep Dhavale 	if (state < 0)
4453fffb589SSandeep Dhavale 		return state;
4463fffb589SSandeep Dhavale 
4473fffb589SSandeep Dhavale 	erofs_cpuhp_state = state;
4483fffb589SSandeep Dhavale 	return 0;
4493fffb589SSandeep Dhavale }
4503fffb589SSandeep Dhavale 
4513fffb589SSandeep Dhavale static void erofs_cpu_hotplug_destroy(void)
4523fffb589SSandeep Dhavale {
4533fffb589SSandeep Dhavale 	if (erofs_cpuhp_state)
4543fffb589SSandeep Dhavale 		cpuhp_remove_state_nocalls(erofs_cpuhp_state);
4553fffb589SSandeep Dhavale }
4563fffb589SSandeep Dhavale #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
4573fffb589SSandeep Dhavale static inline int erofs_cpu_hotplug_init(void) { return 0; }
4583fffb589SSandeep Dhavale static inline void erofs_cpu_hotplug_destroy(void) {}
4593fffb589SSandeep Dhavale #endif
4603fffb589SSandeep Dhavale 
4613fffb589SSandeep Dhavale void z_erofs_exit_zip_subsystem(void)
4623fffb589SSandeep Dhavale {
4633fffb589SSandeep Dhavale 	erofs_cpu_hotplug_destroy();
4643fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
4653fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
4663fffb589SSandeep Dhavale 	z_erofs_destroy_pcluster_pool();
46747e4937aSGao Xiang }
46847e4937aSGao Xiang 
46947e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
47047e4937aSGao Xiang {
4719f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
47247e4937aSGao Xiang 
4739f6cc76eSGao Xiang 	if (err)
4743fffb589SSandeep Dhavale 		goto out_error_pcluster_pool;
4753fffb589SSandeep Dhavale 
4763fffb589SSandeep Dhavale 	z_erofs_workqueue = alloc_workqueue("erofs_worker",
4773fffb589SSandeep Dhavale 			WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
4788d1b80a7SDan Carpenter 	if (!z_erofs_workqueue) {
4798d1b80a7SDan Carpenter 		err = -ENOMEM;
4803fffb589SSandeep Dhavale 		goto out_error_workqueue_init;
4818d1b80a7SDan Carpenter 	}
4823fffb589SSandeep Dhavale 
4833fffb589SSandeep Dhavale 	err = erofs_init_percpu_workers();
4849f6cc76eSGao Xiang 	if (err)
4853fffb589SSandeep Dhavale 		goto out_error_pcpu_worker;
4863fffb589SSandeep Dhavale 
4873fffb589SSandeep Dhavale 	err = erofs_cpu_hotplug_init();
4883fffb589SSandeep Dhavale 	if (err < 0)
4893fffb589SSandeep Dhavale 		goto out_error_cpuhp_init;
4903fffb589SSandeep Dhavale 	return err;
4913fffb589SSandeep Dhavale 
4923fffb589SSandeep Dhavale out_error_cpuhp_init:
4933fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
4943fffb589SSandeep Dhavale out_error_pcpu_worker:
4953fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
4963fffb589SSandeep Dhavale out_error_workqueue_init:
4979f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
4983fffb589SSandeep Dhavale out_error_pcluster_pool:
4999f6cc76eSGao Xiang 	return err;
50047e4937aSGao Xiang }
50147e4937aSGao Xiang 
502db166fc2SGao Xiang enum z_erofs_pclustermode {
503db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_INFLIGHT,
50447e4937aSGao Xiang 	/*
505db166fc2SGao Xiang 	 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
5060b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
5070b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
508387bab87SGao Xiang 	 * bvpage) since it can be directly decoded without I/O submission.
5090b964600SGao Xiang 	 */
510db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
51147e4937aSGao Xiang 	/*
51247e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
51347e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
51447e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
51547e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
51647e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
51747e4937aSGao Xiang 	 * illustrated below:
51847e4937aSGao Xiang 	 *  ________________________________________________________________
51947e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
52047e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
521*967c28b2SGao Xiang 	 * |                      |                                        |
522*967c28b2SGao Xiang 	 * |__PCLUSTER_FOLLOWED___|___________PCLUSTER_FOLLOWED____________|
52347e4937aSGao Xiang 	 *
52447e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
52547e4937aSGao Xiang 	 */
526db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED,
52747e4937aSGao Xiang };
52847e4937aSGao Xiang 
5295c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
5305c6dcc57SGao Xiang 	struct inode *const inode;
5315c6dcc57SGao Xiang 	struct erofs_map_blocks map;
53206a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
53347e4937aSGao Xiang 
5346ab5eed6SGao Xiang 	struct page *pagepool;
53506a304cdSGao Xiang 	struct page *candidate_bvpage;
536*967c28b2SGao Xiang 	struct z_erofs_pcluster *pcl;
53747e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
538db166fc2SGao Xiang 	enum z_erofs_pclustermode mode;
53947e4937aSGao Xiang 
54047e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
54147e4937aSGao Xiang 	bool backmost;
54247e4937aSGao Xiang 	erofs_off_t headoffset;
543ed722fbcSGao Xiang 
544ed722fbcSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
545ed722fbcSGao Xiang 	unsigned int icur;
54647e4937aSGao Xiang };
54747e4937aSGao Xiang 
54847e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
5495c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
550db166fc2SGao Xiang 	.mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
55147e4937aSGao Xiang 
5521282dea3SGao Xiang static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
5531282dea3SGao Xiang {
5541282dea3SGao Xiang 	unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
5551282dea3SGao Xiang 
5561282dea3SGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
5571282dea3SGao Xiang 		return false;
5581282dea3SGao Xiang 
5591282dea3SGao Xiang 	if (fe->backmost)
5601282dea3SGao Xiang 		return true;
5611282dea3SGao Xiang 
5621282dea3SGao Xiang 	if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
5631282dea3SGao Xiang 	    fe->map.m_la < fe->headoffset)
5641282dea3SGao Xiang 		return true;
5651282dea3SGao Xiang 
5661282dea3SGao Xiang 	return false;
5671282dea3SGao Xiang }
5681282dea3SGao Xiang 
5696ab5eed6SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe)
57047e4937aSGao Xiang {
5716f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
5725c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
5731282dea3SGao Xiang 	bool shouldalloc = z_erofs_should_alloc_cache(fe);
57447e4937aSGao Xiang 	bool standalone = true;
5756f39d1e1SGao Xiang 	/*
5766f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
5776f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
5786f39d1e1SGao Xiang 	 */
5791825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
5801825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
581ed722fbcSGao Xiang 	unsigned int i;
58247e4937aSGao Xiang 
583db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
58447e4937aSGao Xiang 		return;
58547e4937aSGao Xiang 
586ed722fbcSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
58747e4937aSGao Xiang 		struct page *page;
588b1ed220cSGao Xiang 		void *t;	/* mark pages just found for debugging */
5891825c8d7SGao Xiang 		struct page *newpage = NULL;
59047e4937aSGao Xiang 
59147e4937aSGao Xiang 		/* the compressed page was loaded before */
592ed722fbcSGao Xiang 		if (READ_ONCE(pcl->compressed_bvecs[i].page))
59347e4937aSGao Xiang 			continue;
59447e4937aSGao Xiang 
595ed722fbcSGao Xiang 		page = find_get_page(mc, pcl->obj.index + i);
59647e4937aSGao Xiang 
59747e4937aSGao Xiang 		if (page) {
598b1ed220cSGao Xiang 			t = (void *)((unsigned long)page | 1);
5990b964600SGao Xiang 		} else {
6000b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
6010b964600SGao Xiang 			standalone = false;
6021282dea3SGao Xiang 			if (!shouldalloc)
6031282dea3SGao Xiang 				continue;
6041282dea3SGao Xiang 
6051282dea3SGao Xiang 			/*
6061282dea3SGao Xiang 			 * try to use cached I/O if page allocation
6071282dea3SGao Xiang 			 * succeeds or fallback to in-place I/O instead
6081282dea3SGao Xiang 			 * to avoid any direct reclaim.
6091282dea3SGao Xiang 			 */
6106ab5eed6SGao Xiang 			newpage = erofs_allocpage(&fe->pagepool, gfp);
6111825c8d7SGao Xiang 			if (!newpage)
61247e4937aSGao Xiang 				continue;
6131282dea3SGao Xiang 			set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
614b1ed220cSGao Xiang 			t = (void *)((unsigned long)newpage | 1);
61547e4937aSGao Xiang 		}
61647e4937aSGao Xiang 
617b1ed220cSGao Xiang 		if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
61847e4937aSGao Xiang 			continue;
61947e4937aSGao Xiang 
620eaa9172aSGao Xiang 		if (page)
62147e4937aSGao Xiang 			put_page(page);
622eaa9172aSGao Xiang 		else if (newpage)
6236ab5eed6SGao Xiang 			erofs_pagepool_add(&fe->pagepool, newpage);
62447e4937aSGao Xiang 	}
62547e4937aSGao Xiang 
6260b964600SGao Xiang 	/*
6270b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
6280b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
6290b964600SGao Xiang 	 */
6300b964600SGao Xiang 	if (standalone)
631db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
63247e4937aSGao Xiang }
63347e4937aSGao Xiang 
63447e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
63547e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
63647e4937aSGao Xiang 				       struct erofs_workgroup *grp)
63747e4937aSGao Xiang {
63847e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
63947e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
64047e4937aSGao Xiang 	int i;
64147e4937aSGao Xiang 
642cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
64347e4937aSGao Xiang 	/*
64447e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
64547e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
64647e4937aSGao Xiang 	 */
6479f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
648ed722fbcSGao Xiang 		struct page *page = pcl->compressed_bvecs[i].page;
64947e4937aSGao Xiang 
65047e4937aSGao Xiang 		if (!page)
65147e4937aSGao Xiang 			continue;
65247e4937aSGao Xiang 
65347e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
65447e4937aSGao Xiang 		if (!trylock_page(page))
65547e4937aSGao Xiang 			return -EBUSY;
65647e4937aSGao Xiang 
657f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
65847e4937aSGao Xiang 			continue;
65947e4937aSGao Xiang 
66047e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
661ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
6626aaa7b06SGao Xiang 		detach_page_private(page);
66347e4937aSGao Xiang 		unlock_page(page);
66447e4937aSGao Xiang 	}
66547e4937aSGao Xiang 	return 0;
66647e4937aSGao Xiang }
66747e4937aSGao Xiang 
668d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
66947e4937aSGao Xiang {
67047e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
671ed722fbcSGao Xiang 	int ret, i;
67247e4937aSGao Xiang 
673ed722fbcSGao Xiang 	if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
674ed722fbcSGao Xiang 		return 0;
67547e4937aSGao Xiang 
676ed722fbcSGao Xiang 	ret = 0;
677cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
6789f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
679ed722fbcSGao Xiang 		if (pcl->compressed_bvecs[i].page == page) {
680ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
68147e4937aSGao Xiang 			ret = 1;
68247e4937aSGao Xiang 			break;
68347e4937aSGao Xiang 		}
68447e4937aSGao Xiang 	}
68547e4937aSGao Xiang 	erofs_workgroup_unfreeze(&pcl->obj, 1);
6866aaa7b06SGao Xiang 	if (ret)
6876aaa7b06SGao Xiang 		detach_page_private(page);
68847e4937aSGao Xiang 	return ret;
68947e4937aSGao Xiang }
69047e4937aSGao Xiang 
6915c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
692ed722fbcSGao Xiang 				   struct z_erofs_bvec *bvec)
69347e4937aSGao Xiang {
6945c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
69547e4937aSGao Xiang 
696ed722fbcSGao Xiang 	while (fe->icur > 0) {
697ed722fbcSGao Xiang 		if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
698ed722fbcSGao Xiang 			     NULL, bvec->page)) {
699ed722fbcSGao Xiang 			pcl->compressed_bvecs[fe->icur] = *bvec;
70047e4937aSGao Xiang 			return true;
701ed722fbcSGao Xiang 		}
702ed722fbcSGao Xiang 	}
70347e4937aSGao Xiang 	return false;
70447e4937aSGao Xiang }
70547e4937aSGao Xiang 
70687ca34a7SGao Xiang /* callers must be with pcluster lock held */
7075c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
7085b220b20SGao Xiang 			       struct z_erofs_bvec *bvec, bool exclusive)
70947e4937aSGao Xiang {
71047e4937aSGao Xiang 	int ret;
71147e4937aSGao Xiang 
712db166fc2SGao Xiang 	if (exclusive) {
71306a304cdSGao Xiang 		/* give priority for inplaceio to use file pages first */
714ed722fbcSGao Xiang 		if (z_erofs_try_inplace_io(fe, bvec))
71547e4937aSGao Xiang 			return 0;
71606a304cdSGao Xiang 		/* otherwise, check if it can be used as a bvpage */
717db166fc2SGao Xiang 		if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
71806a304cdSGao Xiang 		    !fe->candidate_bvpage)
71906a304cdSGao Xiang 			fe->candidate_bvpage = bvec->page;
72006a304cdSGao Xiang 	}
7216ab5eed6SGao Xiang 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
7226ab5eed6SGao Xiang 				   &fe->pagepool);
72306a304cdSGao Xiang 	fe->pcl->vcnt += (ret >= 0);
72406a304cdSGao Xiang 	return ret;
72547e4937aSGao Xiang }
72647e4937aSGao Xiang 
7275c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
72847e4937aSGao Xiang {
7295c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
7305c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
73147e4937aSGao Xiang 
732473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
733473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
734473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
73547e4937aSGao Xiang 		*owned_head = &pcl->next;
736473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
737db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
738473e15b0SGao Xiang 		return;
739473e15b0SGao Xiang 	}
740473e15b0SGao Xiang 
741*967c28b2SGao Xiang 	/* type 2, it belongs to an ongoing chain */
742db166fc2SGao Xiang 	f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
74347e4937aSGao Xiang }
74447e4937aSGao Xiang 
74583a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
74647e4937aSGao Xiang {
74783a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
748cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
74947e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
75064094a04SGao Xiang 	struct erofs_workgroup *grp;
75147e4937aSGao Xiang 	int err;
75247e4937aSGao Xiang 
753c42c0ffeSChen Zhongjin 	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
754c42c0ffeSChen Zhongjin 	    (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
7558f899262SGao Xiang 		DBG_BUGON(1);
7568f899262SGao Xiang 		return -EFSCORRUPTED;
7578f899262SGao Xiang 	}
7588f899262SGao Xiang 
7599f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
760cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
761cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
7629f6cc76eSGao Xiang 	if (IS_ERR(pcl))
7639f6cc76eSGao Xiang 		return PTR_ERR(pcl);
76447e4937aSGao Xiang 
76564094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
7668f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
7672bfab9c0SGao Xiang 	pcl->length = 0;
7682bfab9c0SGao Xiang 	pcl->partial = true;
76947e4937aSGao Xiang 
77047e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
7715c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
77287ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
773db166fc2SGao Xiang 	fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
77447e4937aSGao Xiang 
77547e4937aSGao Xiang 	/*
77647e4937aSGao Xiang 	 * lock all primary followed works before visible to others
77747e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
77847e4937aSGao Xiang 	 */
77987ca34a7SGao Xiang 	mutex_init(&pcl->lock);
78087ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
78147e4937aSGao Xiang 
782cecf864dSYue Hu 	if (ztailpacking) {
783cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
7843acea5fcSJingbo Xu 		pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa);
785cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
786cecf864dSYue Hu 	} else {
787cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
788cecf864dSYue Hu 
78983a386c0SGao Xiang 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
79064094a04SGao Xiang 		if (IS_ERR(grp)) {
79164094a04SGao Xiang 			err = PTR_ERR(grp);
79264094a04SGao Xiang 			goto err_out;
79364094a04SGao Xiang 		}
79464094a04SGao Xiang 
79564094a04SGao Xiang 		if (grp != &pcl->obj) {
7965c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
797cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
79864094a04SGao Xiang 			err = -EEXIST;
79964094a04SGao Xiang 			goto err_out;
80047e4937aSGao Xiang 		}
801cecf864dSYue Hu 	}
8025c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
8035c6dcc57SGao Xiang 	fe->pcl = pcl;
8049e579fc1SGao Xiang 	return 0;
80564094a04SGao Xiang 
80664094a04SGao Xiang err_out:
80787ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
8089f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
80964094a04SGao Xiang 	return err;
81047e4937aSGao Xiang }
81147e4937aSGao Xiang 
81283a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
81347e4937aSGao Xiang {
81483a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
8150d823b42SGao Xiang 	struct erofs_workgroup *grp = NULL;
8169e579fc1SGao Xiang 	int ret;
81747e4937aSGao Xiang 
81887ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
81947e4937aSGao Xiang 
82087ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
8215c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
82247e4937aSGao Xiang 
8230d823b42SGao Xiang 	if (!(map->m_flags & EROFS_MAP_META)) {
8240d823b42SGao Xiang 		grp = erofs_find_workgroup(fe->inode->i_sb,
8250d823b42SGao Xiang 					   map->m_pa >> PAGE_SHIFT);
8260d823b42SGao Xiang 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
82747e4937aSGao Xiang 		DBG_BUGON(1);
828cecf864dSYue Hu 		return -EFSCORRUPTED;
829cecf864dSYue Hu 	}
83047e4937aSGao Xiang 
83164094a04SGao Xiang 	if (grp) {
8325c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
8330d823b42SGao Xiang 		ret = -EEXIST;
83464094a04SGao Xiang 	} else {
83583a386c0SGao Xiang 		ret = z_erofs_register_pcluster(fe);
83664094a04SGao Xiang 	}
83747e4937aSGao Xiang 
8380d823b42SGao Xiang 	if (ret == -EEXIST) {
839267f2492SGao Xiang 		mutex_lock(&fe->pcl->lock);
840267f2492SGao Xiang 		z_erofs_try_to_claim_pcluster(fe);
8410d823b42SGao Xiang 	} else if (ret) {
8420d823b42SGao Xiang 		return ret;
8430d823b42SGao Xiang 	}
84406a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
845387bab87SGao Xiang 				Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
84681382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
847ed722fbcSGao Xiang 	fe->icur = z_erofs_pclusterpages(fe->pcl);
84847e4937aSGao Xiang 	return 0;
84947e4937aSGao Xiang }
85047e4937aSGao Xiang 
85147e4937aSGao Xiang /*
85247e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
85347e4937aSGao Xiang  * only after a RCU grace period.
85447e4937aSGao Xiang  */
85547e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
85647e4937aSGao Xiang {
85787ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
85887ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
85947e4937aSGao Xiang }
86047e4937aSGao Xiang 
86147e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
86247e4937aSGao Xiang {
86347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
86447e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
86547e4937aSGao Xiang 
86687ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
86747e4937aSGao Xiang }
86847e4937aSGao Xiang 
8695c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
87047e4937aSGao Xiang {
87187ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
87247e4937aSGao Xiang 
87387ca34a7SGao Xiang 	if (!pcl)
87447e4937aSGao Xiang 		return false;
87547e4937aSGao Xiang 
87606a304cdSGao Xiang 	z_erofs_bvec_iter_end(&fe->biter);
87787ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
87847e4937aSGao Xiang 
87905b63d2bSGao Xiang 	if (fe->candidate_bvpage)
88006a304cdSGao Xiang 		fe->candidate_bvpage = NULL;
88106a304cdSGao Xiang 
88247e4937aSGao Xiang 	/*
88347e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
88447e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
88547e4937aSGao Xiang 	 */
886db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
88787ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
88847e4937aSGao Xiang 
88987ca34a7SGao Xiang 	fe->pcl = NULL;
89047e4937aSGao Xiang 	return true;
89147e4937aSGao Xiang }
89247e4937aSGao Xiang 
893b15b2e30SYue Hu static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
894b15b2e30SYue Hu 				 struct page *page, unsigned int pageofs,
895b15b2e30SYue Hu 				 unsigned int len)
896b15b2e30SYue Hu {
8973acea5fcSJingbo Xu 	struct super_block *sb = inode->i_sb;
898b15b2e30SYue Hu 	struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
899b15b2e30SYue Hu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
900b15b2e30SYue Hu 	u8 *src, *dst;
901b15b2e30SYue Hu 	unsigned int i, cnt;
902b15b2e30SYue Hu 
903e5126de1SYue Hu 	if (!packed_inode)
904e5126de1SYue Hu 		return -EFSCORRUPTED;
905e5126de1SYue Hu 
906eb2c5e41SGao Xiang 	buf.inode = packed_inode;
907b15b2e30SYue Hu 	pos += EROFS_I(inode)->z_fragmentoff;
908b15b2e30SYue Hu 	for (i = 0; i < len; i += cnt) {
909b15b2e30SYue Hu 		cnt = min_t(unsigned int, len - i,
9103acea5fcSJingbo Xu 			    sb->s_blocksize - erofs_blkoff(sb, pos));
911eb2c5e41SGao Xiang 		src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
912b15b2e30SYue Hu 		if (IS_ERR(src)) {
913b15b2e30SYue Hu 			erofs_put_metabuf(&buf);
914b15b2e30SYue Hu 			return PTR_ERR(src);
915b15b2e30SYue Hu 		}
916b15b2e30SYue Hu 
917b15b2e30SYue Hu 		dst = kmap_local_page(page);
9183acea5fcSJingbo Xu 		memcpy(dst + pageofs + i, src + erofs_blkoff(sb, pos), cnt);
919b15b2e30SYue Hu 		kunmap_local(dst);
920b15b2e30SYue Hu 		pos += cnt;
921b15b2e30SYue Hu 	}
922b15b2e30SYue Hu 	erofs_put_metabuf(&buf);
923b15b2e30SYue Hu 	return 0;
924b15b2e30SYue Hu }
925b15b2e30SYue Hu 
92647e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
9276ab5eed6SGao Xiang 				struct page *page)
92847e4937aSGao Xiang {
92947e4937aSGao Xiang 	struct inode *const inode = fe->inode;
93047e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
93147e4937aSGao Xiang 	const loff_t offset = page_offset(page);
9325b220b20SGao Xiang 	bool tight = true, exclusive;
9332bfab9c0SGao Xiang 	unsigned int cur, end, spiltted;
93447e4937aSGao Xiang 	int err = 0;
93547e4937aSGao Xiang 
93647e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
93747e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
93847e4937aSGao Xiang 
93947e4937aSGao Xiang 	spiltted = 0;
94047e4937aSGao Xiang 	end = PAGE_SIZE;
94147e4937aSGao Xiang repeat:
94247e4937aSGao Xiang 	cur = end - 1;
94347e4937aSGao Xiang 
94439397a46SGao Xiang 	if (offset + cur < map->m_la ||
94539397a46SGao Xiang 	    offset + cur >= map->m_la + map->m_llen) {
9465c6dcc57SGao Xiang 		if (z_erofs_collector_end(fe))
94747e4937aSGao Xiang 			fe->backmost = false;
94847e4937aSGao Xiang 		map->m_la = offset + cur;
94947e4937aSGao Xiang 		map->m_llen = 0;
95047e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
9518d8a09b0SGao Xiang 		if (err)
95267148551SGao Xiang 			goto out;
95339397a46SGao Xiang 	} else {
95439397a46SGao Xiang 		if (fe->pcl)
95539397a46SGao Xiang 			goto hitted;
95639397a46SGao Xiang 		/* didn't get a valid pcluster previously (very rare) */
95739397a46SGao Xiang 	}
95847e4937aSGao Xiang 
959b15b2e30SYue Hu 	if (!(map->m_flags & EROFS_MAP_MAPPED) ||
960b15b2e30SYue Hu 	    map->m_flags & EROFS_MAP_FRAGMENT)
96147e4937aSGao Xiang 		goto hitted;
96247e4937aSGao Xiang 
96383a386c0SGao Xiang 	err = z_erofs_collector_begin(fe);
9648d8a09b0SGao Xiang 	if (err)
96567148551SGao Xiang 		goto out;
96647e4937aSGao Xiang 
9675c6dcc57SGao Xiang 	if (z_erofs_is_inline_pcluster(fe->pcl)) {
96809c54379SGao Xiang 		void *mp;
969cecf864dSYue Hu 
97009c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
9713acea5fcSJingbo Xu 					erofs_blknr(inode->i_sb, map->m_pa),
9723acea5fcSJingbo Xu 					EROFS_NO_KMAP);
97309c54379SGao Xiang 		if (IS_ERR(mp)) {
97409c54379SGao Xiang 			err = PTR_ERR(mp);
975cecf864dSYue Hu 			erofs_err(inode->i_sb,
976cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
97767148551SGao Xiang 			goto out;
978cecf864dSYue Hu 		}
97909c54379SGao Xiang 		get_page(fe->map.buf.page);
980ed722fbcSGao Xiang 		WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
981ed722fbcSGao Xiang 			   fe->map.buf.page);
982db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
983cecf864dSYue Hu 	} else {
9846f39d1e1SGao Xiang 		/* bind cache first when cached decompression is preferred */
9856ab5eed6SGao Xiang 		z_erofs_bind_cache(fe);
986cecf864dSYue Hu 	}
98747e4937aSGao Xiang hitted:
988dc76ea8cSGao Xiang 	/*
989dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
990dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
991dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
992387bab87SGao Xiang 	 * for inplace I/O or bvpage (should be processed in a strict order.)
993dc76ea8cSGao Xiang 	 */
994*967c28b2SGao Xiang 	tight &= (fe->mode > Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
995dc76ea8cSGao Xiang 
99647e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
9978d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
99847e4937aSGao Xiang 		zero_user_segment(page, cur, end);
99947e4937aSGao Xiang 		goto next_part;
100047e4937aSGao Xiang 	}
1001b15b2e30SYue Hu 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
1002b15b2e30SYue Hu 		unsigned int pageofs, skip, len;
1003b15b2e30SYue Hu 
1004b15b2e30SYue Hu 		if (offset > map->m_la) {
1005b15b2e30SYue Hu 			pageofs = 0;
1006b15b2e30SYue Hu 			skip = offset - map->m_la;
1007b15b2e30SYue Hu 		} else {
1008b15b2e30SYue Hu 			pageofs = map->m_la & ~PAGE_MASK;
1009b15b2e30SYue Hu 			skip = 0;
1010b15b2e30SYue Hu 		}
1011b15b2e30SYue Hu 		len = min_t(unsigned int, map->m_llen - skip, end - cur);
1012b15b2e30SYue Hu 		err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
1013b15b2e30SYue Hu 		if (err)
1014b15b2e30SYue Hu 			goto out;
1015b15b2e30SYue Hu 		++spiltted;
1016b15b2e30SYue Hu 		tight = false;
1017b15b2e30SYue Hu 		goto next_part;
1018b15b2e30SYue Hu 	}
101947e4937aSGao Xiang 
10205b220b20SGao Xiang 	exclusive = (!cur && (!spiltted || tight));
102147e4937aSGao Xiang 	if (cur)
1022db166fc2SGao Xiang 		tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
102347e4937aSGao Xiang 
102406a304cdSGao Xiang 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
102506a304cdSGao Xiang 					.page = page,
102606a304cdSGao Xiang 					.offset = offset - map->m_la,
102706a304cdSGao Xiang 					.end = end,
10285b220b20SGao Xiang 				  }), exclusive);
102905b63d2bSGao Xiang 	if (err)
103067148551SGao Xiang 		goto out;
103147e4937aSGao Xiang 
103267148551SGao Xiang 	z_erofs_onlinepage_split(page);
103347e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
103447e4937aSGao Xiang 	++spiltted;
1035267f2492SGao Xiang 	if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1036267f2492SGao Xiang 		fe->pcl->multibases = true;
10372bfab9c0SGao Xiang 	if (fe->pcl->length < offset + end - map->m_la) {
10382bfab9c0SGao Xiang 		fe->pcl->length = offset + end - map->m_la;
10392bfab9c0SGao Xiang 		fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
10402bfab9c0SGao Xiang 	}
1041e7933278SGao Xiang 	if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1042e7933278SGao Xiang 	    !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1043e7933278SGao Xiang 	    fe->pcl->length == map->m_llen)
1044e7933278SGao Xiang 		fe->pcl->partial = false;
104547e4937aSGao Xiang next_part:
10462bfab9c0SGao Xiang 	/* shorten the remaining extent to update progress */
104747e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
10482bfab9c0SGao Xiang 	map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
104947e4937aSGao Xiang 
105047e4937aSGao Xiang 	end = cur;
105147e4937aSGao Xiang 	if (end > 0)
105247e4937aSGao Xiang 		goto repeat;
105347e4937aSGao Xiang 
105447e4937aSGao Xiang out:
105567148551SGao Xiang 	if (err)
105667148551SGao Xiang 		z_erofs_page_mark_eio(page);
105747e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
105847e4937aSGao Xiang 	return err;
105947e4937aSGao Xiang }
106047e4937aSGao Xiang 
1061ef4b4b46SYue Hu static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
106240452ffcSHuang Jianan 				       unsigned int readahead_pages)
106340452ffcSHuang Jianan {
1064a2e20a25SMatthew Wilcox (Oracle) 	/* auto: enable for read_folio, disable for readahead */
106540452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
106640452ffcSHuang Jianan 	    !readahead_pages)
106740452ffcSHuang Jianan 		return true;
106840452ffcSHuang Jianan 
106940452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
107040452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
107140452ffcSHuang Jianan 		return true;
107240452ffcSHuang Jianan 
107340452ffcSHuang Jianan 	return false;
107440452ffcSHuang Jianan }
107540452ffcSHuang Jianan 
10766aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
10776aaa7b06SGao Xiang {
10786aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
10796aaa7b06SGao Xiang }
10806aaa7b06SGao Xiang 
10814f05687fSGao Xiang struct z_erofs_decompress_backend {
10824f05687fSGao Xiang 	struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
10834f05687fSGao Xiang 	struct super_block *sb;
10844f05687fSGao Xiang 	struct z_erofs_pcluster *pcl;
10854f05687fSGao Xiang 
10864f05687fSGao Xiang 	/* pages with the longest decompressed length for deduplication */
10874f05687fSGao Xiang 	struct page **decompressed_pages;
10884f05687fSGao Xiang 	/* pages to keep the compressed data */
10894f05687fSGao Xiang 	struct page **compressed_pages;
10904f05687fSGao Xiang 
1091267f2492SGao Xiang 	struct list_head decompressed_secondary_bvecs;
10924f05687fSGao Xiang 	struct page **pagepool;
10932bfab9c0SGao Xiang 	unsigned int onstack_used, nr_pages;
10944f05687fSGao Xiang };
10954f05687fSGao Xiang 
1096267f2492SGao Xiang struct z_erofs_bvec_item {
1097267f2492SGao Xiang 	struct z_erofs_bvec bvec;
1098267f2492SGao Xiang 	struct list_head list;
1099267f2492SGao Xiang };
1100267f2492SGao Xiang 
1101267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
11023fe96ee0SGao Xiang 					 struct z_erofs_bvec *bvec)
11033fe96ee0SGao Xiang {
1104267f2492SGao Xiang 	struct z_erofs_bvec_item *item;
1105267f2492SGao Xiang 
1106267f2492SGao Xiang 	if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1107267f2492SGao Xiang 		unsigned int pgnr;
11083fe96ee0SGao Xiang 
1109267f2492SGao Xiang 		pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
11102bfab9c0SGao Xiang 		DBG_BUGON(pgnr >= be->nr_pages);
111163bbb856SGao Xiang 		if (!be->decompressed_pages[pgnr]) {
11123fe96ee0SGao Xiang 			be->decompressed_pages[pgnr] = bvec->page;
1113267f2492SGao Xiang 			return;
11143fe96ee0SGao Xiang 		}
111563bbb856SGao Xiang 	}
11163fe96ee0SGao Xiang 
1117267f2492SGao Xiang 	/* (cold path) one pcluster is requested multiple times */
1118267f2492SGao Xiang 	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1119267f2492SGao Xiang 	item->bvec = *bvec;
1120267f2492SGao Xiang 	list_add(&item->list, &be->decompressed_secondary_bvecs);
1121267f2492SGao Xiang }
1122267f2492SGao Xiang 
1123267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1124267f2492SGao Xiang 				      int err)
1125267f2492SGao Xiang {
1126267f2492SGao Xiang 	unsigned int off0 = be->pcl->pageofs_out;
1127267f2492SGao Xiang 	struct list_head *p, *n;
1128267f2492SGao Xiang 
1129267f2492SGao Xiang 	list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1130267f2492SGao Xiang 		struct z_erofs_bvec_item *bvi;
1131267f2492SGao Xiang 		unsigned int end, cur;
1132267f2492SGao Xiang 		void *dst, *src;
1133267f2492SGao Xiang 
1134267f2492SGao Xiang 		bvi = container_of(p, struct z_erofs_bvec_item, list);
1135267f2492SGao Xiang 		cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1136267f2492SGao Xiang 		end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1137267f2492SGao Xiang 			    bvi->bvec.end);
1138267f2492SGao Xiang 		dst = kmap_local_page(bvi->bvec.page);
1139267f2492SGao Xiang 		while (cur < end) {
1140267f2492SGao Xiang 			unsigned int pgnr, scur, len;
1141267f2492SGao Xiang 
1142267f2492SGao Xiang 			pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1143267f2492SGao Xiang 			DBG_BUGON(pgnr >= be->nr_pages);
1144267f2492SGao Xiang 
1145267f2492SGao Xiang 			scur = bvi->bvec.offset + cur -
1146267f2492SGao Xiang 					((pgnr << PAGE_SHIFT) - off0);
1147267f2492SGao Xiang 			len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1148267f2492SGao Xiang 			if (!be->decompressed_pages[pgnr]) {
1149267f2492SGao Xiang 				err = -EFSCORRUPTED;
1150267f2492SGao Xiang 				cur += len;
1151267f2492SGao Xiang 				continue;
1152267f2492SGao Xiang 			}
1153267f2492SGao Xiang 			src = kmap_local_page(be->decompressed_pages[pgnr]);
1154267f2492SGao Xiang 			memcpy(dst + cur, src + scur, len);
1155267f2492SGao Xiang 			kunmap_local(src);
1156267f2492SGao Xiang 			cur += len;
1157267f2492SGao Xiang 		}
1158267f2492SGao Xiang 		kunmap_local(dst);
1159267f2492SGao Xiang 		if (err)
1160267f2492SGao Xiang 			z_erofs_page_mark_eio(bvi->bvec.page);
1161267f2492SGao Xiang 		z_erofs_onlinepage_endio(bvi->bvec.page);
1162267f2492SGao Xiang 		list_del(p);
1163267f2492SGao Xiang 		kfree(bvi);
1164267f2492SGao Xiang 	}
1165267f2492SGao Xiang }
1166267f2492SGao Xiang 
1167267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
116842fec235SGao Xiang {
11694f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
117006a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
117106a304cdSGao Xiang 	struct page *old_bvpage;
1172267f2492SGao Xiang 	int i;
117342fec235SGao Xiang 
1174387bab87SGao Xiang 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
117542fec235SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
117606a304cdSGao Xiang 		struct z_erofs_bvec bvec;
117742fec235SGao Xiang 
117806a304cdSGao Xiang 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
117942fec235SGao Xiang 
118006a304cdSGao Xiang 		if (old_bvpage)
11814f05687fSGao Xiang 			z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
118242fec235SGao Xiang 
118306a304cdSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1184267f2492SGao Xiang 		z_erofs_do_decompressed_bvec(be, &bvec);
118542fec235SGao Xiang 	}
118606a304cdSGao Xiang 
118706a304cdSGao Xiang 	old_bvpage = z_erofs_bvec_iter_end(&biter);
118806a304cdSGao Xiang 	if (old_bvpage)
11894f05687fSGao Xiang 		z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
119042fec235SGao Xiang }
119142fec235SGao Xiang 
11924f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
11934f05687fSGao Xiang 				  bool *overlapped)
119467139e36SGao Xiang {
11954f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
119667139e36SGao Xiang 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
119767139e36SGao Xiang 	int i, err = 0;
119867139e36SGao Xiang 
119967139e36SGao Xiang 	*overlapped = false;
120067139e36SGao Xiang 	for (i = 0; i < pclusterpages; ++i) {
1201ed722fbcSGao Xiang 		struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1202ed722fbcSGao Xiang 		struct page *page = bvec->page;
120367139e36SGao Xiang 
120467139e36SGao Xiang 		/* compressed pages ought to be present before decompressing */
120567139e36SGao Xiang 		if (!page) {
120667139e36SGao Xiang 			DBG_BUGON(1);
120767139e36SGao Xiang 			continue;
120867139e36SGao Xiang 		}
1209fe3e5914SGao Xiang 		be->compressed_pages[i] = page;
121067139e36SGao Xiang 
121167139e36SGao Xiang 		if (z_erofs_is_inline_pcluster(pcl)) {
121267139e36SGao Xiang 			if (!PageUptodate(page))
121367139e36SGao Xiang 				err = -EIO;
121467139e36SGao Xiang 			continue;
121567139e36SGao Xiang 		}
121667139e36SGao Xiang 
121767139e36SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
121867139e36SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
12194f05687fSGao Xiang 			if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
122067139e36SGao Xiang 				if (!PageUptodate(page))
122167139e36SGao Xiang 					err = -EIO;
122267139e36SGao Xiang 				continue;
122367139e36SGao Xiang 			}
1224267f2492SGao Xiang 			z_erofs_do_decompressed_bvec(be, bvec);
122567139e36SGao Xiang 			*overlapped = true;
122667139e36SGao Xiang 		}
122767139e36SGao Xiang 	}
122867139e36SGao Xiang 
1229fe3e5914SGao Xiang 	if (err)
12304f05687fSGao Xiang 		return err;
12314f05687fSGao Xiang 	return 0;
123267139e36SGao Xiang }
123367139e36SGao Xiang 
12344f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
12354f05687fSGao Xiang 				       int err)
123647e4937aSGao Xiang {
12374f05687fSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
12384f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
1239cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1240597e2953SYue Hu 	const struct z_erofs_decompressor *decompressor =
1241597e2953SYue Hu 				&erofs_decompressors[pcl->algorithmformat];
12422bfab9c0SGao Xiang 	unsigned int i, inputsize;
124367148551SGao Xiang 	int err2;
12442bfab9c0SGao Xiang 	struct page *page;
12452bfab9c0SGao Xiang 	bool overlapped;
124647e4937aSGao Xiang 
124787ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
12482bfab9c0SGao Xiang 	be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
124947e4937aSGao Xiang 
1250fe3e5914SGao Xiang 	/* allocate (de)compressed page arrays if cannot be kept on stack */
1251fe3e5914SGao Xiang 	be->decompressed_pages = NULL;
1252fe3e5914SGao Xiang 	be->compressed_pages = NULL;
1253fe3e5914SGao Xiang 	be->onstack_used = 0;
12542bfab9c0SGao Xiang 	if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
12554f05687fSGao Xiang 		be->decompressed_pages = be->onstack_pages;
12562bfab9c0SGao Xiang 		be->onstack_used = be->nr_pages;
12574f05687fSGao Xiang 		memset(be->decompressed_pages, 0,
12582bfab9c0SGao Xiang 		       sizeof(struct page *) * be->nr_pages);
1259fe3e5914SGao Xiang 	}
1260fe3e5914SGao Xiang 
1261fe3e5914SGao Xiang 	if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1262fe3e5914SGao Xiang 		be->compressed_pages = be->onstack_pages + be->onstack_used;
1263fe3e5914SGao Xiang 
1264fe3e5914SGao Xiang 	if (!be->decompressed_pages)
12654f05687fSGao Xiang 		be->decompressed_pages =
1266647dd2c3SGao Xiang 			kvcalloc(be->nr_pages, sizeof(struct page *),
1267e7368187SGao Xiang 				 GFP_KERNEL | __GFP_NOFAIL);
1268fe3e5914SGao Xiang 	if (!be->compressed_pages)
1269fe3e5914SGao Xiang 		be->compressed_pages =
1270647dd2c3SGao Xiang 			kvcalloc(pclusterpages, sizeof(struct page *),
1271fe3e5914SGao Xiang 				 GFP_KERNEL | __GFP_NOFAIL);
127247e4937aSGao Xiang 
1273267f2492SGao Xiang 	z_erofs_parse_out_bvecs(be);
12744f05687fSGao Xiang 	err2 = z_erofs_parse_in_bvecs(be, &overlapped);
12754f05687fSGao Xiang 	if (err2)
12764f05687fSGao Xiang 		err = err2;
12778d8a09b0SGao Xiang 	if (err)
127847e4937aSGao Xiang 		goto out;
127947e4937aSGao Xiang 
1280cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1281cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1282cecf864dSYue Hu 	else
1283cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1284cecf864dSYue Hu 
1285597e2953SYue Hu 	err = decompressor->decompress(&(struct z_erofs_decompress_req) {
12864f05687fSGao Xiang 					.sb = be->sb,
12874f05687fSGao Xiang 					.in = be->compressed_pages,
12884f05687fSGao Xiang 					.out = be->decompressed_pages,
1289cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
129087ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
12919f6cc76eSGao Xiang 					.inputsize = inputsize,
12922bfab9c0SGao Xiang 					.outputsize = pcl->length,
129347e4937aSGao Xiang 					.alg = pcl->algorithmformat,
129447e4937aSGao Xiang 					.inplace_io = overlapped,
12952bfab9c0SGao Xiang 					.partial_decoding = pcl->partial,
1296267f2492SGao Xiang 					.fillgaps = pcl->multibases,
12974f05687fSGao Xiang 				 }, be->pagepool);
129847e4937aSGao Xiang 
129947e4937aSGao Xiang out:
1300cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1301cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1302ed722fbcSGao Xiang 		page = pcl->compressed_bvecs[0].page;
1303ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1304cecf864dSYue Hu 		put_page(page);
1305cecf864dSYue Hu 	} else {
1306cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
1307ed722fbcSGao Xiang 			page = pcl->compressed_bvecs[i].page;
130847e4937aSGao Xiang 
130947e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
131047e4937aSGao Xiang 				continue;
131147e4937aSGao Xiang 
13126aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
13134f05687fSGao Xiang 			(void)z_erofs_put_shortlivedpage(be->pagepool, page);
1314ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
131547e4937aSGao Xiang 		}
1316cecf864dSYue Hu 	}
1317fe3e5914SGao Xiang 	if (be->compressed_pages < be->onstack_pages ||
1318fe3e5914SGao Xiang 	    be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1319647dd2c3SGao Xiang 		kvfree(be->compressed_pages);
1320267f2492SGao Xiang 	z_erofs_fill_other_copies(be, err);
132147e4937aSGao Xiang 
13222bfab9c0SGao Xiang 	for (i = 0; i < be->nr_pages; ++i) {
13234f05687fSGao Xiang 		page = be->decompressed_pages[i];
132447e4937aSGao Xiang 		if (!page)
132547e4937aSGao Xiang 			continue;
132647e4937aSGao Xiang 
13276aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
132847e4937aSGao Xiang 
13296aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
13304f05687fSGao Xiang 		if (z_erofs_put_shortlivedpage(be->pagepool, page))
133147e4937aSGao Xiang 			continue;
133267148551SGao Xiang 		if (err)
133367148551SGao Xiang 			z_erofs_page_mark_eio(page);
133447e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
133547e4937aSGao Xiang 	}
133647e4937aSGao Xiang 
13374f05687fSGao Xiang 	if (be->decompressed_pages != be->onstack_pages)
1338647dd2c3SGao Xiang 		kvfree(be->decompressed_pages);
133947e4937aSGao Xiang 
13402bfab9c0SGao Xiang 	pcl->length = 0;
13412bfab9c0SGao Xiang 	pcl->partial = true;
1342267f2492SGao Xiang 	pcl->multibases = false;
134306a304cdSGao Xiang 	pcl->bvset.nextpage = NULL;
134487ca34a7SGao Xiang 	pcl->vcnt = 0;
134547e4937aSGao Xiang 
134687ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
134747e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
134887ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
134947e4937aSGao Xiang 	return err;
135047e4937aSGao Xiang }
135147e4937aSGao Xiang 
13520c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1353eaa9172aSGao Xiang 				     struct page **pagepool)
135447e4937aSGao Xiang {
13554f05687fSGao Xiang 	struct z_erofs_decompress_backend be = {
13564f05687fSGao Xiang 		.sb = io->sb,
13574f05687fSGao Xiang 		.pagepool = pagepool,
1358267f2492SGao Xiang 		.decompressed_secondary_bvecs =
1359267f2492SGao Xiang 			LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
13604f05687fSGao Xiang 	};
136147e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
136247e4937aSGao Xiang 
1363*967c28b2SGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL) {
136447e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
136547e4937aSGao Xiang 
13664f05687fSGao Xiang 		be.pcl = container_of(owned, struct z_erofs_pcluster, next);
13674f05687fSGao Xiang 		owned = READ_ONCE(be.pcl->next);
136847e4937aSGao Xiang 
13694f05687fSGao Xiang 		z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
13704f05687fSGao Xiang 		erofs_workgroup_put(&be.pcl->obj);
137147e4937aSGao Xiang 	}
137247e4937aSGao Xiang }
137347e4937aSGao Xiang 
13740c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
137547e4937aSGao Xiang {
1376a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1377a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1378eaa9172aSGao Xiang 	struct page *pagepool = NULL;
137947e4937aSGao Xiang 
1380*967c28b2SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
13810c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
1382eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1383a4b1fab1SGao Xiang 	kvfree(bgq);
138447e4937aSGao Xiang }
138547e4937aSGao Xiang 
13863fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
13873fffb589SSandeep Dhavale static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
13883fffb589SSandeep Dhavale {
13893fffb589SSandeep Dhavale 	z_erofs_decompressqueue_work((struct work_struct *)work);
13903fffb589SSandeep Dhavale }
13913fffb589SSandeep Dhavale #endif
13923fffb589SSandeep Dhavale 
13937865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1394cdba5506SGao Xiang 				       int bios)
13957865827cSGao Xiang {
13967865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
13977865827cSGao Xiang 
13987865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
1399cdba5506SGao Xiang 	if (io->sync) {
14007865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
140160b30050SHongyu Jin 			complete(&io->u.done);
14027865827cSGao Xiang 		return;
14037865827cSGao Xiang 	}
14047865827cSGao Xiang 
14057865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
14067865827cSGao Xiang 		return;
14073fffb589SSandeep Dhavale 	/* Use (kthread_)work and sync decompression for atomic contexts only */
14087865827cSGao Xiang 	if (in_atomic() || irqs_disabled()) {
14093fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
14103fffb589SSandeep Dhavale 		struct kthread_worker *worker;
14113fffb589SSandeep Dhavale 
14123fffb589SSandeep Dhavale 		rcu_read_lock();
14133fffb589SSandeep Dhavale 		worker = rcu_dereference(
14143fffb589SSandeep Dhavale 				z_erofs_pcpu_workers[raw_smp_processor_id()]);
14153fffb589SSandeep Dhavale 		if (!worker) {
14163fffb589SSandeep Dhavale 			INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
14177865827cSGao Xiang 			queue_work(z_erofs_workqueue, &io->u.work);
14183fffb589SSandeep Dhavale 		} else {
14193fffb589SSandeep Dhavale 			kthread_queue_work(worker, &io->u.kthread_work);
14203fffb589SSandeep Dhavale 		}
14213fffb589SSandeep Dhavale 		rcu_read_unlock();
14223fffb589SSandeep Dhavale #else
14233fffb589SSandeep Dhavale 		queue_work(z_erofs_workqueue, &io->u.work);
14243fffb589SSandeep Dhavale #endif
14257865827cSGao Xiang 		/* enable sync decompression for readahead */
14267865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
14277865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
14287865827cSGao Xiang 		return;
14297865827cSGao Xiang 	}
14307865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
14317865827cSGao Xiang }
14327865827cSGao Xiang 
143347e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
143447e4937aSGao Xiang 					       unsigned int nr,
1435eaa9172aSGao Xiang 					       struct page **pagepool,
14369f2731d6SGao Xiang 					       struct address_space *mc)
143747e4937aSGao Xiang {
143847e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
14399f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
144047e4937aSGao Xiang 	bool tocache = false;
144147e4937aSGao Xiang 
144247e4937aSGao Xiang 	struct address_space *mapping;
144347e4937aSGao Xiang 	struct page *oldpage, *page;
144447e4937aSGao Xiang 	int justfound;
144547e4937aSGao Xiang 
144647e4937aSGao Xiang repeat:
1447ed722fbcSGao Xiang 	page = READ_ONCE(pcl->compressed_bvecs[nr].page);
144847e4937aSGao Xiang 	oldpage = page;
144947e4937aSGao Xiang 
145047e4937aSGao Xiang 	if (!page)
145147e4937aSGao Xiang 		goto out_allocpage;
145247e4937aSGao Xiang 
1453b1ed220cSGao Xiang 	justfound = (unsigned long)page & 1UL;
1454b1ed220cSGao Xiang 	page = (struct page *)((unsigned long)page & ~1UL);
145547e4937aSGao Xiang 
14561825c8d7SGao Xiang 	/*
14571825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
14581825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
14591825c8d7SGao Xiang 	 */
14601825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1461ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
14621825c8d7SGao Xiang 		set_page_private(page, 0);
14631825c8d7SGao Xiang 		tocache = true;
14641825c8d7SGao Xiang 		goto out_tocache;
14651825c8d7SGao Xiang 	}
146647e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
146747e4937aSGao Xiang 
146847e4937aSGao Xiang 	/*
14696aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
147047e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
147147e4937aSGao Xiang 	 */
147247e4937aSGao Xiang 	if (mapping && mapping != mc)
147347e4937aSGao Xiang 		/* ought to be unmanaged pages */
147447e4937aSGao Xiang 		goto out;
147547e4937aSGao Xiang 
14766aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
14776aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
14786aaa7b06SGao Xiang 		goto out;
14796aaa7b06SGao Xiang 
148047e4937aSGao Xiang 	lock_page(page);
148147e4937aSGao Xiang 
148247e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
148347e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
148447e4937aSGao Xiang 
148547e4937aSGao Xiang 	/* the page is still in manage cache */
148647e4937aSGao Xiang 	if (page->mapping == mc) {
1487ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
148847e4937aSGao Xiang 
148947e4937aSGao Xiang 		if (!PagePrivate(page)) {
149047e4937aSGao Xiang 			/*
149147e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
149247e4937aSGao Xiang 			 * the current restriction as well if
1493ed722fbcSGao Xiang 			 * the page is already in compressed_bvecs[].
149447e4937aSGao Xiang 			 */
149547e4937aSGao Xiang 			DBG_BUGON(!justfound);
149647e4937aSGao Xiang 
149747e4937aSGao Xiang 			justfound = 0;
149847e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
149947e4937aSGao Xiang 			SetPagePrivate(page);
150047e4937aSGao Xiang 		}
150147e4937aSGao Xiang 
150247e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
150347e4937aSGao Xiang 		if (PageUptodate(page)) {
150447e4937aSGao Xiang 			unlock_page(page);
150547e4937aSGao Xiang 			page = NULL;
150647e4937aSGao Xiang 		}
150747e4937aSGao Xiang 		goto out;
150847e4937aSGao Xiang 	}
150947e4937aSGao Xiang 
151047e4937aSGao Xiang 	/*
151147e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
151247e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
151347e4937aSGao Xiang 	 */
151447e4937aSGao Xiang 	DBG_BUGON(page->mapping);
151547e4937aSGao Xiang 	DBG_BUGON(!justfound);
151647e4937aSGao Xiang 
151747e4937aSGao Xiang 	tocache = true;
151847e4937aSGao Xiang 	unlock_page(page);
151947e4937aSGao Xiang 	put_page(page);
152047e4937aSGao Xiang out_allocpage:
15215ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1522ed722fbcSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1523ed722fbcSGao Xiang 			       oldpage, page)) {
1524eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
15255ddcee1fSGao Xiang 		cond_resched();
15265ddcee1fSGao Xiang 		goto repeat;
15275ddcee1fSGao Xiang 	}
15281825c8d7SGao Xiang out_tocache:
1529bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1530bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1531bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1532bf225074SGao Xiang 		goto out;
1533a30573b3SGao Xiang 	}
1534bf225074SGao Xiang 	attach_page_private(page, pcl);
1535bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1536bf225074SGao Xiang 	put_page(page);
1537bf225074SGao Xiang 
153847e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
153947e4937aSGao Xiang 	return page;
154047e4937aSGao Xiang }
154147e4937aSGao Xiang 
1542cdba5506SGao Xiang static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1543a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
154447e4937aSGao Xiang {
1545a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
154647e4937aSGao Xiang 
1547a4b1fab1SGao Xiang 	if (fg && !*fg) {
1548a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1549a4b1fab1SGao Xiang 		if (!q) {
1550a4b1fab1SGao Xiang 			*fg = true;
1551a4b1fab1SGao Xiang 			goto fg_out;
155247e4937aSGao Xiang 		}
15533fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
15543fffb589SSandeep Dhavale 		kthread_init_work(&q->u.kthread_work,
15553fffb589SSandeep Dhavale 				  z_erofs_decompressqueue_kthread_work);
15563fffb589SSandeep Dhavale #else
15570c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
15583fffb589SSandeep Dhavale #endif
1559a4b1fab1SGao Xiang 	} else {
1560a4b1fab1SGao Xiang fg_out:
1561a4b1fab1SGao Xiang 		q = fgq;
156260b30050SHongyu Jin 		init_completion(&fgq->u.done);
1563a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
156467148551SGao Xiang 		q->eio = false;
1565cdba5506SGao Xiang 		q->sync = true;
1566a4b1fab1SGao Xiang 	}
1567a4b1fab1SGao Xiang 	q->sb = sb;
1568*967c28b2SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL;
1569a4b1fab1SGao Xiang 	return q;
157047e4937aSGao Xiang }
157147e4937aSGao Xiang 
157247e4937aSGao Xiang /* define decompression jobqueue types */
157347e4937aSGao Xiang enum {
157447e4937aSGao Xiang 	JQ_BYPASS,
157547e4937aSGao Xiang 	JQ_SUBMIT,
157647e4937aSGao Xiang 	NR_JOBQUEUES,
157747e4937aSGao Xiang };
157847e4937aSGao Xiang 
157947e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
158047e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
158147e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
158247e4937aSGao Xiang {
158347e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
158447e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
158547e4937aSGao Xiang 
1586*967c28b2SGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
158747e4937aSGao Xiang 
158847e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
158947e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
159047e4937aSGao Xiang 
159147e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
159247e4937aSGao Xiang }
159347e4937aSGao Xiang 
15947865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
15957865827cSGao Xiang {
1596cdba5506SGao Xiang 	struct z_erofs_decompressqueue *q = bio->bi_private;
15977865827cSGao Xiang 	blk_status_t err = bio->bi_status;
15987865827cSGao Xiang 	struct bio_vec *bvec;
15997865827cSGao Xiang 	struct bvec_iter_all iter_all;
16007865827cSGao Xiang 
16017865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
16027865827cSGao Xiang 		struct page *page = bvec->bv_page;
16037865827cSGao Xiang 
16047865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
16057865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
16067865827cSGao Xiang 
16077865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
16087865827cSGao Xiang 			if (!err)
16097865827cSGao Xiang 				SetPageUptodate(page);
16107865827cSGao Xiang 			unlock_page(page);
16117865827cSGao Xiang 		}
16127865827cSGao Xiang 	}
161367148551SGao Xiang 	if (err)
161467148551SGao Xiang 		q->eio = true;
1615cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q, -1);
16167865827cSGao Xiang 	bio_put(bio);
16177865827cSGao Xiang }
16187865827cSGao Xiang 
161983a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1620a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1621ef4b4b46SYue Hu 				 bool *force_fg, bool readahead)
162247e4937aSGao Xiang {
162383a386c0SGao Xiang 	struct super_block *sb = f->inode->i_sb;
162483a386c0SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
162547e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1626a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
16275c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1628dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
16293f649ab7SKees Cook 	pgoff_t last_index;
1630dfeab2e9SGao Xiang 	struct block_device *last_bdev;
16311e4a2955SGao Xiang 	unsigned int nr_bios = 0;
16321e4a2955SGao Xiang 	struct bio *bio = NULL;
163382e60d00SJohannes Weiner 	unsigned long pflags;
163482e60d00SJohannes Weiner 	int memstall = 0;
163547e4937aSGao Xiang 
1636cdba5506SGao Xiang 	/*
1637cdba5506SGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
1638cdba5506SGao Xiang 	 * no need to read from device for all pclusters in this queue.
1639cdba5506SGao Xiang 	 */
1640cdba5506SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1641cdba5506SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1642cdba5506SGao Xiang 
1643a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1644a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
164547e4937aSGao Xiang 
164647e4937aSGao Xiang 	/* by default, all need io submission */
164747e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
164847e4937aSGao Xiang 
164947e4937aSGao Xiang 	do {
1650dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
165147e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
16521e4a2955SGao Xiang 		pgoff_t cur, end;
16531e4a2955SGao Xiang 		unsigned int i = 0;
16541e4a2955SGao Xiang 		bool bypass = true;
165547e4937aSGao Xiang 
165647e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
165747e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1658*967c28b2SGao Xiang 		owned_head = READ_ONCE(pcl->next);
165947e4937aSGao Xiang 
1660cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1661cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1662cecf864dSYue Hu 			continue;
1663cecf864dSYue Hu 		}
1664cecf864dSYue Hu 
1665dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1666dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
16673acea5fcSJingbo Xu 			.m_pa = erofs_pos(sb, pcl->obj.index),
1668dfeab2e9SGao Xiang 		};
1669dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1670dfeab2e9SGao Xiang 
16713acea5fcSJingbo Xu 		cur = erofs_blknr(sb, mdev.m_pa);
16729f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
167347e4937aSGao Xiang 
16741e4a2955SGao Xiang 		do {
16751e4a2955SGao Xiang 			struct page *page;
167647e4937aSGao Xiang 
16776ab5eed6SGao Xiang 			page = pickup_page_for_submission(pcl, i++,
16786ab5eed6SGao Xiang 					&f->pagepool, mc);
16791e4a2955SGao Xiang 			if (!page)
16801e4a2955SGao Xiang 				continue;
168147e4937aSGao Xiang 
1682dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1683dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
168447e4937aSGao Xiang submit_bio_retry:
168594e4e153SGao Xiang 				submit_bio(bio);
168682e60d00SJohannes Weiner 				if (memstall) {
168782e60d00SJohannes Weiner 					psi_memstall_leave(&pflags);
168882e60d00SJohannes Weiner 					memstall = 0;
168982e60d00SJohannes Weiner 				}
169047e4937aSGao Xiang 				bio = NULL;
169147e4937aSGao Xiang 			}
169247e4937aSGao Xiang 
169382e60d00SJohannes Weiner 			if (unlikely(PageWorkingset(page)) && !memstall) {
169499486c51SChristoph Hellwig 				psi_memstall_enter(&pflags);
169582e60d00SJohannes Weiner 				memstall = 1;
169682e60d00SJohannes Weiner 			}
169799486c51SChristoph Hellwig 
169847e4937aSGao Xiang 			if (!bio) {
169907888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
170007888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
17010c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1702dfeab2e9SGao Xiang 
1703dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
17041e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
17053acea5fcSJingbo Xu 					(sb->s_blocksize_bits - 9);
1706cdba5506SGao Xiang 				bio->bi_private = q[JQ_SUBMIT];
1707ef4b4b46SYue Hu 				if (readahead)
17086ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
170947e4937aSGao Xiang 				++nr_bios;
171047e4937aSGao Xiang 			}
171147e4937aSGao Xiang 
17126c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
171347e4937aSGao Xiang 				goto submit_bio_retry;
171447e4937aSGao Xiang 
17151e4a2955SGao Xiang 			last_index = cur;
17161e4a2955SGao Xiang 			bypass = false;
17171e4a2955SGao Xiang 		} while (++cur < end);
171847e4937aSGao Xiang 
17191e4a2955SGao Xiang 		if (!bypass)
172047e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
172147e4937aSGao Xiang 		else
172247e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
172347e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
172447e4937aSGao Xiang 
172599486c51SChristoph Hellwig 	if (bio) {
172694e4e153SGao Xiang 		submit_bio(bio);
172782e60d00SJohannes Weiner 		if (memstall)
172882e60d00SJohannes Weiner 			psi_memstall_leave(&pflags);
172999486c51SChristoph Hellwig 	}
173047e4937aSGao Xiang 
1731587a67b7SGao Xiang 	/*
1732587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
17333fffb589SSandeep Dhavale 	 * don't issue decompression but drop it directly instead.
1734587a67b7SGao Xiang 	 */
1735587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1736587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
17371e4a2955SGao Xiang 		return;
1738587a67b7SGao Xiang 	}
1739cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
174047e4937aSGao Xiang }
174147e4937aSGao Xiang 
174283a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
17436ab5eed6SGao Xiang 			     bool force_fg, bool ra)
174447e4937aSGao Xiang {
1745a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
174647e4937aSGao Xiang 
17475c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
174847e4937aSGao Xiang 		return;
17496ab5eed6SGao Xiang 	z_erofs_submit_queue(f, io, &force_fg, ra);
175047e4937aSGao Xiang 
17510c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
17526ab5eed6SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
175347e4937aSGao Xiang 
175447e4937aSGao Xiang 	if (!force_fg)
175547e4937aSGao Xiang 		return;
175647e4937aSGao Xiang 
175747e4937aSGao Xiang 	/* wait until all bios are completed */
175860b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
175947e4937aSGao Xiang 
17600c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
17616ab5eed6SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool);
176247e4937aSGao Xiang }
176347e4937aSGao Xiang 
176438629291SGao Xiang /*
176538629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
176638629291SGao Xiang  * approximate readmore strategies as a start.
176738629291SGao Xiang  */
176838629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
17696ab5eed6SGao Xiang 		struct readahead_control *rac, bool backmost)
177038629291SGao Xiang {
177138629291SGao Xiang 	struct inode *inode = f->inode;
177238629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
1773796e9149SYue Hu 	erofs_off_t cur, end, headoffset = f->headoffset;
177438629291SGao Xiang 	int err;
177538629291SGao Xiang 
177638629291SGao Xiang 	if (backmost) {
1777796e9149SYue Hu 		if (rac)
1778796e9149SYue Hu 			end = headoffset + readahead_length(rac) - 1;
1779796e9149SYue Hu 		else
1780796e9149SYue Hu 			end = headoffset + PAGE_SIZE - 1;
178138629291SGao Xiang 		map->m_la = end;
1782622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1783622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
178438629291SGao Xiang 		if (err)
178538629291SGao Xiang 			return;
178638629291SGao Xiang 
1787796e9149SYue Hu 		/* expand ra for the trailing edge if readahead */
178838629291SGao Xiang 		if (rac) {
178938629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1790796e9149SYue Hu 			readahead_expand(rac, headoffset, cur - headoffset);
179138629291SGao Xiang 			return;
179238629291SGao Xiang 		}
179338629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
179438629291SGao Xiang 	} else {
179538629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
179638629291SGao Xiang 
179738629291SGao Xiang 		if (!map->m_llen)
179838629291SGao Xiang 			return;
179938629291SGao Xiang 	}
180038629291SGao Xiang 
180138629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
180238629291SGao Xiang 	while (cur >= end) {
180338629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
180438629291SGao Xiang 		struct page *page;
180538629291SGao Xiang 
180638629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1807aa793b46SGao Xiang 		if (page) {
180838629291SGao Xiang 			if (PageUptodate(page)) {
180938629291SGao Xiang 				unlock_page(page);
1810aa793b46SGao Xiang 			} else {
18116ab5eed6SGao Xiang 				err = z_erofs_do_read_page(f, page);
181238629291SGao Xiang 				if (err)
181338629291SGao Xiang 					erofs_err(inode->i_sb,
181438629291SGao Xiang 						  "readmore error at page %lu @ nid %llu",
181538629291SGao Xiang 						  index, EROFS_I(inode)->nid);
1816aa793b46SGao Xiang 			}
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);
18405c6dcc57SGao Xiang 	(void)z_erofs_collector_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 
184547e4937aSGao Xiang 	if (err)
18464f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
184747e4937aSGao Xiang 
184809c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
18496ab5eed6SGao Xiang 	erofs_release_pages(&f.pagepool);
185047e4937aSGao Xiang 	return err;
185147e4937aSGao Xiang }
185247e4937aSGao Xiang 
18530615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
185447e4937aSGao Xiang {
18550615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
185647e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
185747e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
18586ab5eed6SGao Xiang 	struct page *head = NULL, *page;
185938629291SGao Xiang 	unsigned int nr_pages;
186047e4937aSGao Xiang 
18610615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
186247e4937aSGao Xiang 
18636ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, true);
186438629291SGao Xiang 	nr_pages = readahead_count(rac);
186538629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
186638629291SGao Xiang 
18670615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
186847e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
186947e4937aSGao Xiang 		head = page;
187047e4937aSGao Xiang 	}
187147e4937aSGao Xiang 
187247e4937aSGao Xiang 	while (head) {
187347e4937aSGao Xiang 		struct page *page = head;
187447e4937aSGao Xiang 		int err;
187547e4937aSGao Xiang 
187647e4937aSGao Xiang 		/* traversal in reverse order */
187747e4937aSGao Xiang 		head = (void *)page_private(page);
187847e4937aSGao Xiang 
18796ab5eed6SGao Xiang 		err = z_erofs_do_read_page(&f, page);
1880a5876e24SGao Xiang 		if (err)
18814f761fa2SGao Xiang 			erofs_err(inode->i_sb,
18824f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
18834f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
188447e4937aSGao Xiang 		put_page(page);
188547e4937aSGao Xiang 	}
18866ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, false);
18875c6dcc57SGao Xiang 	(void)z_erofs_collector_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