blob: 9a249bfc2770581dd91cbcf212c2d2e7a7bbfd8a [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang02827e12018-07-26 20:21:58 +08002/*
Gao Xiang02827e12018-07-26 20:21:58 +08003 * Copyright (C) 2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang02827e12018-07-26 20:21:58 +08005 */
Gao Xiang57b78c92019-07-31 23:57:32 +08006#include "zdata.h"
Gao Xiang27481232019-06-24 15:22:54 +08007#include "compress.h"
Gao Xiang3883a792018-07-26 20:22:06 +08008#include <linux/prefetch.h>
9
Chen Gong284db122018-09-18 22:27:27 +080010#include <trace/events/erofs.h>
11
Gao Xiang672e5472018-12-08 00:19:14 +080012/*
Gao Xiang9f6cc762021-04-07 12:39:20 +080013 * since pclustersize is variable for big pcluster feature, introduce slab
14 * pools implementation for different pcluster sizes.
15 */
16struct z_erofs_pcluster_slab {
17 struct kmem_cache *slab;
18 unsigned int maxpages;
19 char name[48];
20};
21
22#define _PCLP(n) { .maxpages = n }
23
24static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
25 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
26 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
27};
28
29static void z_erofs_destroy_pcluster_pool(void)
30{
31 int i;
32
33 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
34 if (!pcluster_pool[i].slab)
35 continue;
36 kmem_cache_destroy(pcluster_pool[i].slab);
37 pcluster_pool[i].slab = NULL;
38 }
39}
40
41static int z_erofs_create_pcluster_pool(void)
42{
43 struct z_erofs_pcluster_slab *pcs;
44 struct z_erofs_pcluster *a;
45 unsigned int size;
46
47 for (pcs = pcluster_pool;
48 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
49 size = struct_size(a, compressed_pages, pcs->maxpages);
50
51 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
52 pcs->slab = kmem_cache_create(pcs->name, size, 0,
53 SLAB_RECLAIM_ACCOUNT, NULL);
54 if (pcs->slab)
55 continue;
56
57 z_erofs_destroy_pcluster_pool();
58 return -ENOMEM;
59 }
60 return 0;
61}
62
63static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
64{
65 int i;
66
67 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
68 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
69 struct z_erofs_pcluster *pcl;
70
71 if (nrpages > pcs->maxpages)
72 continue;
73
74 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
75 if (!pcl)
76 return ERR_PTR(-ENOMEM);
77 pcl->pclusterpages = nrpages;
78 return pcl;
79 }
80 return ERR_PTR(-EINVAL);
81}
82
83static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
84{
85 int i;
86
87 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
88 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
89
90 if (pcl->pclusterpages > pcs->maxpages)
91 continue;
92
93 kmem_cache_free(pcs->slab, pcl);
94 return;
95 }
96 DBG_BUGON(1);
97}
98
Gao Xiang97e86a82019-07-31 23:57:47 +080099/* how to allocate cached pages for a pcluster */
Gao Xiang92e6efd2018-12-08 00:19:16 +0800100enum z_erofs_cache_alloctype {
101 DONTALLOC, /* don't allocate any cached pages */
Gao Xiang1825c8d2020-12-09 20:37:17 +0800102 /*
103 * try to use cached I/O if page allocation succeeds or fallback
104 * to in-place I/O instead to avoid any direct reclaim.
105 */
106 TRYALLOC,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800107};
108
109/*
110 * tagged pointer with 1-bit tag for all compressed pages
111 * tag 0 - the page is just found with an extra page reference
112 */
113typedef tagptr1_t compressed_page_t;
114
115#define tag_compressed_page_justfound(page) \
116 tagptr_fold(compressed_page_t, page, 1)
117
Gao Xiang3883a792018-07-26 20:22:06 +0800118static struct workqueue_struct *z_erofs_workqueue __read_mostly;
Gao Xiang3883a792018-07-26 20:22:06 +0800119
120void z_erofs_exit_zip_subsystem(void)
121{
Gao Xiang3883a792018-07-26 20:22:06 +0800122 destroy_workqueue(z_erofs_workqueue);
Gao Xiang9f6cc762021-04-07 12:39:20 +0800123 z_erofs_destroy_pcluster_pool();
Gao Xiang3883a792018-07-26 20:22:06 +0800124}
125
Gao Xiang99634bf2019-09-04 10:09:05 +0800126static inline int z_erofs_init_workqueue(void)
Gao Xiang3883a792018-07-26 20:22:06 +0800127{
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200128 const unsigned int onlinecpus = num_possible_cpus();
Gao Xiang3883a792018-07-26 20:22:06 +0800129
130 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800131 * no need to spawn too many threads, limiting threads could minimum
132 * scheduling overhead, perhaps per-CPU threads should be better?
Gao Xiang3883a792018-07-26 20:22:06 +0800133 */
Gao Xiang0e62ea32020-07-31 10:40:49 +0800134 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
135 WQ_UNBOUND | WQ_HIGHPRI,
Gao Xiang97e86a82019-07-31 23:57:47 +0800136 onlinecpus + onlinecpus / 4);
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100137 return z_erofs_workqueue ? 0 : -ENOMEM;
Gao Xiang3883a792018-07-26 20:22:06 +0800138}
139
Gao Xiang0a0b7e62018-10-09 21:43:53 +0800140int __init z_erofs_init_zip_subsystem(void)
Gao Xiang3883a792018-07-26 20:22:06 +0800141{
Gao Xiang9f6cc762021-04-07 12:39:20 +0800142 int err = z_erofs_create_pcluster_pool();
Gao Xiang3883a792018-07-26 20:22:06 +0800143
Gao Xiang9f6cc762021-04-07 12:39:20 +0800144 if (err)
145 return err;
146 err = z_erofs_init_workqueue();
147 if (err)
148 z_erofs_destroy_pcluster_pool();
149 return err;
Gao Xiang3883a792018-07-26 20:22:06 +0800150}
151
Gao Xiang97e86a82019-07-31 23:57:47 +0800152enum z_erofs_collectmode {
153 COLLECT_SECONDARY,
154 COLLECT_PRIMARY,
Gao Xiang3883a792018-07-26 20:22:06 +0800155 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800156 * The current collection was the tail of an exist chain, in addition
157 * that the previous processed chained collections are all decided to
158 * be hooked up to it.
159 * A new chain will be created for the remaining collections which are
160 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
161 * the next collection cannot reuse the whole page safely in
162 * the following scenario:
Gao Xianga1121522019-02-27 13:33:32 +0800163 * ________________________________________________________________
164 * | tail (partial) page | head (partial) page |
Gao Xiang97e86a82019-07-31 23:57:47 +0800165 * | (belongs to the next cl) | (belongs to the current cl) |
Gao Xianga1121522019-02-27 13:33:32 +0800166 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
167 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800168 COLLECT_PRIMARY_HOOKED,
Gao Xiang0b964602021-03-22 02:32:27 +0800169 /*
170 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
171 * could be dispatched into bypass queue later due to uptodated managed
172 * pages. All related online pages cannot be reused for inplace I/O (or
173 * pagevec) since it can be directly decoded without I/O submission.
174 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800175 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
Gao Xianga1121522019-02-27 13:33:32 +0800176 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800177 * The current collection has been linked with the owned chain, and
178 * could also be linked with the remaining collections, which means
179 * if the processing page is the tail page of the collection, thus
180 * the current collection can safely use the whole page (since
181 * the previous collection is under control) for in-place I/O, as
182 * illustrated below:
Gao Xianga1121522019-02-27 13:33:32 +0800183 * ________________________________________________________________
Gao Xiang97e86a82019-07-31 23:57:47 +0800184 * | tail (partial) page | head (partial) page |
185 * | (of the current cl) | (of the previous collection) |
186 * | PRIMARY_FOLLOWED or | |
187 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
Gao Xianga1121522019-02-27 13:33:32 +0800188 *
Gao Xiang97e86a82019-07-31 23:57:47 +0800189 * [ (*) the above page can be used as inplace I/O. ]
Gao Xiang3883a792018-07-26 20:22:06 +0800190 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800191 COLLECT_PRIMARY_FOLLOWED,
Gao Xiang3883a792018-07-26 20:22:06 +0800192};
193
Gao Xiang97e86a82019-07-31 23:57:47 +0800194struct z_erofs_collector {
Gao Xiang3883a792018-07-26 20:22:06 +0800195 struct z_erofs_pagevec_ctor vector;
196
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800197 struct z_erofs_pcluster *pcl, *tailpcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800198 struct z_erofs_collection *cl;
Gao Xiang81382f52021-04-07 12:39:21 +0800199 /* a pointer used to pick up inplace I/O pages */
200 struct page **icpage_ptr;
Gao Xiang97e86a82019-07-31 23:57:47 +0800201 z_erofs_next_pcluster_t owned_head;
202
203 enum z_erofs_collectmode mode;
Gao Xiang3883a792018-07-26 20:22:06 +0800204};
205
Gao Xiang97e86a82019-07-31 23:57:47 +0800206struct z_erofs_decompress_frontend {
207 struct inode *const inode;
208
209 struct z_erofs_collector clt;
210 struct erofs_map_blocks map;
211
Gao Xiang6ea5aad2020-09-19 15:27:30 +0800212 bool readahead;
Gao Xiang97e86a82019-07-31 23:57:47 +0800213 /* used for applying cache strategy on the fly */
214 bool backmost;
215 erofs_off_t headoffset;
216};
217
218#define COLLECTOR_INIT() { \
219 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
220 .mode = COLLECT_PRIMARY_FOLLOWED }
221
222#define DECOMPRESS_FRONTEND_INIT(__i) { \
223 .inode = __i, .clt = COLLECTOR_INIT(), \
224 .backmost = true, }
225
226static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
227static DEFINE_MUTEX(z_pagemap_global_lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800228
Gao Xiang97e86a82019-07-31 23:57:47 +0800229static void preload_compressed_pages(struct z_erofs_collector *clt,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800230 struct address_space *mc,
Gao Xiang1825c8d2020-12-09 20:37:17 +0800231 enum z_erofs_cache_alloctype type,
Gao Xiangeaa91722021-10-22 17:01:20 +0800232 struct page **pagepool)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800233{
Gao Xiang81382f52021-04-07 12:39:21 +0800234 struct z_erofs_pcluster *pcl = clt->pcl;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800235 bool standalone = true;
Gao Xiang1825c8d2020-12-09 20:37:17 +0800236 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
237 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
Gao Xiang81382f52021-04-07 12:39:21 +0800238 struct page **pages;
239 pgoff_t index;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800240
Gao Xiang97e86a82019-07-31 23:57:47 +0800241 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800242 return;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800243
Gao Xiang81382f52021-04-07 12:39:21 +0800244 pages = pcl->compressed_pages;
245 index = pcl->obj.index;
246 for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
Gao Xiang92e6efd2018-12-08 00:19:16 +0800247 struct page *page;
248 compressed_page_t t;
Gao Xiang1825c8d2020-12-09 20:37:17 +0800249 struct page *newpage = NULL;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800250
251 /* the compressed page was loaded before */
Gao Xiang97e86a82019-07-31 23:57:47 +0800252 if (READ_ONCE(*pages))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800253 continue;
254
Gao Xiang97e86a82019-07-31 23:57:47 +0800255 page = find_get_page(mc, index);
Gao Xiang92e6efd2018-12-08 00:19:16 +0800256
257 if (page) {
258 t = tag_compressed_page_justfound(page);
Gao Xiang0b964602021-03-22 02:32:27 +0800259 } else {
260 /* I/O is needed, no possible to decompress directly */
Gao Xiang92e6efd2018-12-08 00:19:16 +0800261 standalone = false;
Gao Xiang0b964602021-03-22 02:32:27 +0800262 switch (type) {
Gao Xiang0b964602021-03-22 02:32:27 +0800263 case TRYALLOC:
264 newpage = erofs_allocpage(pagepool, gfp);
265 if (!newpage)
266 continue;
267 set_page_private(newpage,
268 Z_EROFS_PREALLOCATED_PAGE);
269 t = tag_compressed_page_justfound(newpage);
270 break;
271 default: /* DONTALLOC */
272 continue;
273 }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800274 }
275
Gao Xiang97e86a82019-07-31 23:57:47 +0800276 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800277 continue;
278
Gao Xiangeaa91722021-10-22 17:01:20 +0800279 if (page)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800280 put_page(page);
Gao Xiangeaa91722021-10-22 17:01:20 +0800281 else if (newpage)
282 erofs_pagepool_add(pagepool, newpage);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800283 }
Gao Xiang92e6efd2018-12-08 00:19:16 +0800284
Gao Xiang0b964602021-03-22 02:32:27 +0800285 /*
286 * don't do inplace I/O if all compressed pages are available in
287 * managed cache since it can be moved to the bypass queue instead.
288 */
289 if (standalone)
Gao Xiang97e86a82019-07-31 23:57:47 +0800290 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800291}
292
293/* called by erofs_shrinker to get rid of all compressed_pages */
Gao Xiang47e541a2018-07-29 13:34:58 +0800294int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
Gao Xiang97e86a82019-07-31 23:57:47 +0800295 struct erofs_workgroup *grp)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800296{
Gao Xiang97e86a82019-07-31 23:57:47 +0800297 struct z_erofs_pcluster *const pcl =
298 container_of(grp, struct z_erofs_pcluster, obj);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800299 int i;
300
301 /*
302 * refcount of workgroup is now freezed as 1,
303 * therefore no need to worry about available decompression users.
304 */
Gao Xiang9f6cc762021-04-07 12:39:20 +0800305 for (i = 0; i < pcl->pclusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800306 struct page *page = pcl->compressed_pages[i];
Gao Xiang105d4ad2018-07-26 20:22:07 +0800307
Gao Xiang97e86a82019-07-31 23:57:47 +0800308 if (!page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800309 continue;
310
311 /* block other users from reclaiming or migrating the page */
312 if (!trylock_page(page))
313 return -EBUSY;
314
Yue Huf4d4e5f2021-08-10 14:54:50 +0800315 if (!erofs_page_is_managed(sbi, page))
Gao Xiang97e86a82019-07-31 23:57:47 +0800316 continue;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800317
Gao Xiang97e86a82019-07-31 23:57:47 +0800318 /* barrier is implied in the following 'unlock_page' */
319 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800320 detach_page_private(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800321 unlock_page(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800322 }
323 return 0;
324}
325
Yue Hud252ff32021-08-10 15:24:16 +0800326int erofs_try_to_free_cached_page(struct page *page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800327{
Gao Xiang97e86a82019-07-31 23:57:47 +0800328 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800329 int ret = 0; /* 0 - busy */
330
Gao Xiang97e86a82019-07-31 23:57:47 +0800331 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
Gao Xiang105d4ad2018-07-26 20:22:07 +0800332 unsigned int i;
333
Gao Xiang9f6cc762021-04-07 12:39:20 +0800334 for (i = 0; i < pcl->pclusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800335 if (pcl->compressed_pages[i] == page) {
336 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800337 ret = 1;
338 break;
339 }
340 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800341 erofs_workgroup_unfreeze(&pcl->obj, 1);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800342
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800343 if (ret)
344 detach_page_private(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800345 }
346 return ret;
347}
Gao Xiang105d4ad2018-07-26 20:22:07 +0800348
Gao Xiang3883a792018-07-26 20:22:06 +0800349/* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
Gao Xiang81382f52021-04-07 12:39:21 +0800350static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
351 struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +0800352{
Gao Xiang97e86a82019-07-31 23:57:47 +0800353 struct z_erofs_pcluster *const pcl = clt->pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800354
Gao Xiang81382f52021-04-07 12:39:21 +0800355 while (clt->icpage_ptr > pcl->compressed_pages)
356 if (!cmpxchg(--clt->icpage_ptr, NULL, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800357 return true;
Gao Xiang3883a792018-07-26 20:22:06 +0800358 return false;
359}
360
Gao Xiang97e86a82019-07-31 23:57:47 +0800361/* callers must be with collection lock held */
362static int z_erofs_attach_page(struct z_erofs_collector *clt,
Gao Xiang86432a62021-11-04 02:20:06 +0800363 struct page *page, enum z_erofs_page_type type,
364 bool pvec_safereuse)
Gao Xiang3883a792018-07-26 20:22:06 +0800365{
366 int ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800367
Gao Xiang97e86a82019-07-31 23:57:47 +0800368 /* give priority for inplaceio */
369 if (clt->mode >= COLLECT_PRIMARY &&
Julian Merida447a3622019-03-18 20:58:41 -0300370 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
Gao Xiang99634bf2019-09-04 10:09:05 +0800371 z_erofs_try_inplace_io(clt, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800372 return 0;
373
Gao Xiang86432a62021-11-04 02:20:06 +0800374 ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
375 pvec_safereuse);
Gao Xiang97e86a82019-07-31 23:57:47 +0800376 clt->cl->vcnt += (unsigned int)ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800377 return ret ? 0 : -EAGAIN;
378}
379
Gao Xiang473e15b2020-12-08 17:58:34 +0800380static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800381{
Gao Xiang473e15b2020-12-08 17:58:34 +0800382 struct z_erofs_pcluster *pcl = clt->pcl;
383 z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +0800384
Gao Xiang473e15b2020-12-08 17:58:34 +0800385 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
386 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
387 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800388 *owned_head = &pcl->next;
Gao Xiang473e15b2020-12-08 17:58:34 +0800389 /* so we can attach this pcluster to our submission chain. */
390 clt->mode = COLLECT_PRIMARY_FOLLOWED;
391 return;
Gao Xianga1121522019-02-27 13:33:32 +0800392 }
Gao Xiang473e15b2020-12-08 17:58:34 +0800393
394 /*
395 * type 2, link to the end of an existing open chain, be careful
396 * that its submission is controlled by the original attached chain.
397 */
398 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
399 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
400 *owned_head = Z_EROFS_PCLUSTER_TAIL;
401 clt->mode = COLLECT_PRIMARY_HOOKED;
402 clt->tailpcl = NULL;
403 return;
404 }
405 /* type 3, it belongs to a chain, but it isn't the end of the chain */
406 clt->mode = COLLECT_PRIMARY;
Gao Xiang3883a792018-07-26 20:22:06 +0800407}
408
Gao Xiang9e579fc2019-10-08 20:56:12 +0800409static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
410 struct inode *inode,
411 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800412{
Gao Xiang64094a02020-02-20 10:46:42 +0800413 struct z_erofs_pcluster *pcl = clt->pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800414 struct z_erofs_collection *cl;
415 unsigned int length;
Gao Xiang3883a792018-07-26 20:22:06 +0800416
Gao Xiang64094a02020-02-20 10:46:42 +0800417 /* to avoid unexpected loop formed by corrupted images */
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800418 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
419 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800420 return -EFSCORRUPTED;
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800421 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800422
423 cl = z_erofs_primarycollection(pcl);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800424 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800425 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800426 return -EFSCORRUPTED;
Gao Xiang3883a792018-07-26 20:22:06 +0800427 }
428
Gao Xiang97e86a82019-07-31 23:57:47 +0800429 length = READ_ONCE(pcl->length);
430 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
431 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
432 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800433 return -EFSCORRUPTED;
Gao Xiang97e86a82019-07-31 23:57:47 +0800434 }
435 } else {
436 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
Gao Xiang3883a792018-07-26 20:22:06 +0800437
Gao Xiang97e86a82019-07-31 23:57:47 +0800438 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
439 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
Gao Xiang3883a792018-07-26 20:22:06 +0800440
Gao Xiang97e86a82019-07-31 23:57:47 +0800441 while (llen > length &&
442 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
443 cpu_relax();
444 length = READ_ONCE(pcl->length);
445 }
446 }
447 mutex_lock(&cl->lock);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800448 /* used to check tail merging loop due to corrupted images */
449 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
450 clt->tailpcl = pcl;
Gao Xiang473e15b2020-12-08 17:58:34 +0800451
452 z_erofs_try_to_claim_pcluster(clt);
Gao Xiang97e86a82019-07-31 23:57:47 +0800453 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800454 return 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800455}
456
Gao Xiang9e579fc2019-10-08 20:56:12 +0800457static int z_erofs_register_collection(struct z_erofs_collector *clt,
458 struct inode *inode,
459 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800460{
Gao Xiang97e86a82019-07-31 23:57:47 +0800461 struct z_erofs_pcluster *pcl;
462 struct z_erofs_collection *cl;
Gao Xiang64094a02020-02-20 10:46:42 +0800463 struct erofs_workgroup *grp;
Gao Xiang97e86a82019-07-31 23:57:47 +0800464 int err;
Gao Xiange5e3abb2018-09-19 13:49:07 +0800465
Gao Xiang8f899262021-10-09 04:08:37 +0800466 if (!(map->m_flags & EROFS_MAP_ENCODED)) {
467 DBG_BUGON(1);
468 return -EFSCORRUPTED;
469 }
470
Gao Xiang9f6cc762021-04-07 12:39:20 +0800471 /* no available pcluster, let's allocate one */
472 pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
473 if (IS_ERR(pcl))
474 return PTR_ERR(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800475
Gao Xiang64094a02020-02-20 10:46:42 +0800476 atomic_set(&pcl->obj.refcount, 1);
Gao Xiang97e86a82019-07-31 23:57:47 +0800477 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
Gao Xiang8f899262021-10-09 04:08:37 +0800478 pcl->algorithmformat = map->m_algorithmformat;
Gao Xiang97e86a82019-07-31 23:57:47 +0800479 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
480 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
481 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800482
Gao Xiang97e86a82019-07-31 23:57:47 +0800483 /* new pclusters should be claimed as type 1, primary and followed */
484 pcl->next = clt->owned_head;
485 clt->mode = COLLECT_PRIMARY_FOLLOWED;
486
487 cl = z_erofs_primarycollection(pcl);
488 cl->pageofs = map->m_la & ~PAGE_MASK;
Gao Xiang3883a792018-07-26 20:22:06 +0800489
Gao Xiang23edf3a2018-11-23 01:21:47 +0800490 /*
491 * lock all primary followed works before visible to others
Gao Xiang97e86a82019-07-31 23:57:47 +0800492 * and mutex_trylock *never* fails for a new pcluster.
Gao Xiang23edf3a2018-11-23 01:21:47 +0800493 */
Gao Xiang9f6cc762021-04-07 12:39:20 +0800494 mutex_init(&cl->lock);
Gao Xiang64094a02020-02-20 10:46:42 +0800495 DBG_BUGON(!mutex_trylock(&cl->lock));
Gao Xiang23edf3a2018-11-23 01:21:47 +0800496
Gao Xiang64094a02020-02-20 10:46:42 +0800497 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
498 if (IS_ERR(grp)) {
499 err = PTR_ERR(grp);
500 goto err_out;
501 }
502
503 if (grp != &pcl->obj) {
504 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
505 err = -EEXIST;
506 goto err_out;
Gao Xiang3883a792018-07-26 20:22:06 +0800507 }
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800508 /* used to check tail merging loop due to corrupted images */
509 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
510 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800511 clt->owned_head = &pcl->next;
512 clt->pcl = pcl;
513 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800514 return 0;
Gao Xiang64094a02020-02-20 10:46:42 +0800515
516err_out:
517 mutex_unlock(&cl->lock);
Gao Xiang9f6cc762021-04-07 12:39:20 +0800518 z_erofs_free_pcluster(pcl);
Gao Xiang64094a02020-02-20 10:46:42 +0800519 return err;
Gao Xiang3883a792018-07-26 20:22:06 +0800520}
521
Gao Xiang97e86a82019-07-31 23:57:47 +0800522static int z_erofs_collector_begin(struct z_erofs_collector *clt,
523 struct inode *inode,
524 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800525{
Gao Xiang64094a02020-02-20 10:46:42 +0800526 struct erofs_workgroup *grp;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800527 int ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800528
Gao Xiang97e86a82019-07-31 23:57:47 +0800529 DBG_BUGON(clt->cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800530
Gao Xiang97e86a82019-07-31 23:57:47 +0800531 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
532 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
533 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +0800534
Gao Xiang97e86a82019-07-31 23:57:47 +0800535 if (!PAGE_ALIGNED(map->m_pa)) {
536 DBG_BUGON(1);
537 return -EINVAL;
538 }
Gao Xiang3883a792018-07-26 20:22:06 +0800539
Gao Xiang64094a02020-02-20 10:46:42 +0800540 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
541 if (grp) {
542 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
543 } else {
Gao Xiang9e579fc2019-10-08 20:56:12 +0800544 ret = z_erofs_register_collection(clt, inode, map);
Gao Xiangb27661c2018-09-19 13:49:06 +0800545
Gao Xiang64094a02020-02-20 10:46:42 +0800546 if (!ret)
547 goto out;
548 if (ret != -EEXIST)
549 return ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800550 }
551
Gao Xiang64094a02020-02-20 10:46:42 +0800552 ret = z_erofs_lookup_collection(clt, inode, map);
553 if (ret) {
554 erofs_workgroup_put(&clt->pcl->obj);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800555 return ret;
Gao Xiang64094a02020-02-20 10:46:42 +0800556 }
Gao Xiang3883a792018-07-26 20:22:06 +0800557
Gao Xiang64094a02020-02-20 10:46:42 +0800558out:
Gao Xiang97e86a82019-07-31 23:57:47 +0800559 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang9e579fc2019-10-08 20:56:12 +0800560 clt->cl->pagevec, clt->cl->vcnt);
Gao Xiang3883a792018-07-26 20:22:06 +0800561
Gao Xiang81382f52021-04-07 12:39:21 +0800562 /* since file-backed online pages are traversed in reverse order */
563 clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages;
Gao Xiang3883a792018-07-26 20:22:06 +0800564 return 0;
565}
566
567/*
Gao Xiang97e86a82019-07-31 23:57:47 +0800568 * keep in mind that no referenced pclusters will be freed
569 * only after a RCU grace period.
Gao Xiang3883a792018-07-26 20:22:06 +0800570 */
571static void z_erofs_rcu_callback(struct rcu_head *head)
572{
Gao Xiang97e86a82019-07-31 23:57:47 +0800573 struct z_erofs_collection *const cl =
574 container_of(head, struct z_erofs_collection, rcu);
Gao Xiang3883a792018-07-26 20:22:06 +0800575
Gao Xiang9f6cc762021-04-07 12:39:20 +0800576 z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
577 primary_collection));
Gao Xiang3883a792018-07-26 20:22:06 +0800578}
579
580void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
581{
Gao Xiang97e86a82019-07-31 23:57:47 +0800582 struct z_erofs_pcluster *const pcl =
583 container_of(grp, struct z_erofs_pcluster, obj);
584 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800585
Gao Xiang97e86a82019-07-31 23:57:47 +0800586 call_rcu(&cl->rcu, z_erofs_rcu_callback);
Gao Xiang3883a792018-07-26 20:22:06 +0800587}
588
Gao Xiang97e86a82019-07-31 23:57:47 +0800589static void z_erofs_collection_put(struct z_erofs_collection *cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800590{
Gao Xiang97e86a82019-07-31 23:57:47 +0800591 struct z_erofs_pcluster *const pcl =
592 container_of(cl, struct z_erofs_pcluster, primary_collection);
593
594 erofs_workgroup_put(&pcl->obj);
Gao Xiang3883a792018-07-26 20:22:06 +0800595}
596
Gao Xiang97e86a82019-07-31 23:57:47 +0800597static bool z_erofs_collector_end(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800598{
Gao Xiang97e86a82019-07-31 23:57:47 +0800599 struct z_erofs_collection *cl = clt->cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800600
Gao Xiang97e86a82019-07-31 23:57:47 +0800601 if (!cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800602 return false;
603
Gao Xiang97e86a82019-07-31 23:57:47 +0800604 z_erofs_pagevec_ctor_exit(&clt->vector, false);
605 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800606
607 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800608 * if all pending pages are added, don't hold its reference
609 * any longer if the pcluster isn't hosted by ourselves.
Gao Xiang3883a792018-07-26 20:22:06 +0800610 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800611 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
612 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800613
Gao Xiang97e86a82019-07-31 23:57:47 +0800614 clt->cl = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +0800615 return true;
616}
617
Gao Xiang97e86a82019-07-31 23:57:47 +0800618static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800619 unsigned int cachestrategy,
Gao Xiang97e86a82019-07-31 23:57:47 +0800620 erofs_off_t la)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800621{
Gao Xiang4279f3f2019-07-31 23:57:49 +0800622 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
623 return false;
624
Gao Xiang92e6efd2018-12-08 00:19:16 +0800625 if (fe->backmost)
626 return true;
627
Gao Xiang4279f3f2019-07-31 23:57:49 +0800628 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
629 la < fe->headoffset;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800630}
Gao Xiang92e6efd2018-12-08 00:19:16 +0800631
Gao Xiang97e86a82019-07-31 23:57:47 +0800632static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
Gao Xiangeaa91722021-10-22 17:01:20 +0800633 struct page *page, struct page **pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800634{
Gao Xiang97e86a82019-07-31 23:57:47 +0800635 struct inode *const inode = fe->inode;
Gao Xiangbda17a42019-10-08 20:56:13 +0800636 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Chao Yu3b423412019-01-15 09:42:21 +0800637 struct erofs_map_blocks *const map = &fe->map;
Gao Xiang97e86a82019-07-31 23:57:47 +0800638 struct z_erofs_collector *const clt = &fe->clt;
Gao Xiang3883a792018-07-26 20:22:06 +0800639 const loff_t offset = page_offset(page);
Gao Xiangdc76ea82019-09-22 18:04:34 +0800640 bool tight = true;
Gao Xiang3883a792018-07-26 20:22:06 +0800641
Gao Xiang92e6efd2018-12-08 00:19:16 +0800642 enum z_erofs_cache_alloctype cache_strategy;
Gao Xiang3883a792018-07-26 20:22:06 +0800643 enum z_erofs_page_type page_type;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200644 unsigned int cur, end, spiltted, index;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800645 int err = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800646
647 /* register locked file pages as online pages in pack */
648 z_erofs_onlinepage_init(page);
649
650 spiltted = 0;
651 end = PAGE_SIZE;
652repeat:
653 cur = end - 1;
654
655 /* lucky, within the range of the current map_blocks */
656 if (offset + cur >= map->m_la &&
Julian Merida447a3622019-03-18 20:58:41 -0300657 offset + cur < map->m_la + map->m_llen) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800658 /* didn't get a valid collection previously (very rare) */
659 if (!clt->cl)
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800660 goto restart_now;
Gao Xiang3883a792018-07-26 20:22:06 +0800661 goto hitted;
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800662 }
Gao Xiang3883a792018-07-26 20:22:06 +0800663
664 /* go ahead the next map_blocks */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800665 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
Gao Xiang3883a792018-07-26 20:22:06 +0800666
Gao Xiang97e86a82019-07-31 23:57:47 +0800667 if (z_erofs_collector_end(clt))
Gao Xiangf0c519f2018-11-23 01:21:49 +0800668 fe->backmost = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800669
670 map->m_la = offset + cur;
671 map->m_llen = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +0800672 err = z_erofs_map_blocks_iter(inode, map, 0);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800673 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800674 goto err_out;
675
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800676restart_now:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800677 if (!(map->m_flags & EROFS_MAP_MAPPED))
Gao Xiang3883a792018-07-26 20:22:06 +0800678 goto hitted;
679
Gao Xiang97e86a82019-07-31 23:57:47 +0800680 err = z_erofs_collector_begin(clt, inode, map);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800681 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800682 goto err_out;
683
Gao Xiang92e6efd2018-12-08 00:19:16 +0800684 /* preload all compressed pages (maybe downgrade role if necessary) */
Gao Xiange6242462021-10-07 15:02:23 +0800685 if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy, map->m_la))
Gao Xiang1825c8d2020-12-09 20:37:17 +0800686 cache_strategy = TRYALLOC;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800687 else
688 cache_strategy = DONTALLOC;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800689
Gao Xiang1825c8d2020-12-09 20:37:17 +0800690 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
691 cache_strategy, pagepool);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800692
Gao Xiang3883a792018-07-26 20:22:06 +0800693hitted:
Gao Xiangdc76ea82019-09-22 18:04:34 +0800694 /*
695 * Ensure the current partial page belongs to this submit chain rather
696 * than other concurrent submit chains or the noio(bypass) chain since
697 * those chains are handled asynchronously thus the page cannot be used
698 * for inplace I/O or pagevec (should be processed in strict order.)
699 */
700 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
701 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
702
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200703 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800704 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800705 zero_user_segment(page, cur, end);
706 goto next_part;
707 }
708
709 /* let's derive page type */
710 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
711 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
712 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
713 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
714
Gao Xianga1121522019-02-27 13:33:32 +0800715 if (cur)
Gao Xiang97e86a82019-07-31 23:57:47 +0800716 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xianga1121522019-02-27 13:33:32 +0800717
Gao Xiang3883a792018-07-26 20:22:06 +0800718retry:
Gao Xiang86432a62021-11-04 02:20:06 +0800719 err = z_erofs_attach_page(clt, page, page_type,
720 clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800721 /* should allocate an additional short-lived page for pagevec */
Gao Xiang3883a792018-07-26 20:22:06 +0800722 if (err == -EAGAIN) {
723 struct page *const newpage =
Chao Yue3f78d52020-09-17 09:18:21 +0800724 alloc_page(GFP_NOFS | __GFP_NOFAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800725
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800726 set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
Gao Xiang97e86a82019-07-31 23:57:47 +0800727 err = z_erofs_attach_page(clt, newpage,
Gao Xiang86432a62021-11-04 02:20:06 +0800728 Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800729 if (!err)
Gao Xiang3883a792018-07-26 20:22:06 +0800730 goto retry;
731 }
732
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800733 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800734 goto err_out;
735
Gao Xiang97e86a82019-07-31 23:57:47 +0800736 index = page->index - (map->m_la >> PAGE_SHIFT);
Gao Xiang3883a792018-07-26 20:22:06 +0800737
Gao Xiang3883a792018-07-26 20:22:06 +0800738 z_erofs_onlinepage_fixup(page, index, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800739
Gao Xiang1e05ff32018-09-18 22:27:25 +0800740 /* bump up the number of spiltted parts of a page */
741 ++spiltted;
742 /* also update nr_pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800743 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
Gao Xiang3883a792018-07-26 20:22:06 +0800744next_part:
745 /* can be used for verification */
746 map->m_llen = offset + cur - map->m_la;
747
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300748 end = cur;
749 if (end > 0)
Gao Xiang3883a792018-07-26 20:22:06 +0800750 goto repeat;
751
Gao Xiang1e05ff32018-09-18 22:27:25 +0800752out:
Gao Xiang3883a792018-07-26 20:22:06 +0800753 z_erofs_onlinepage_endio(page);
754
Gao Xiang4f761fa2019-09-04 10:09:09 +0800755 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
756 __func__, page, spiltted, map->m_llen);
Gao Xiang3883a792018-07-26 20:22:06 +0800757 return err;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800758
759 /* if some error occurred while processing this page */
760err_out:
761 SetPageError(page);
762 goto out;
Gao Xiang3883a792018-07-26 20:22:06 +0800763}
764
Huang Jianan648f2de2021-03-17 11:54:47 +0800765static void z_erofs_decompressqueue_work(struct work_struct *work);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800766static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
767 bool sync, int bios)
Gao Xiang3883a792018-07-26 20:22:06 +0800768{
Huang Jianan30048cd2021-03-17 11:54:48 +0800769 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
770
Gao Xianga4b1fab2019-10-08 20:56:15 +0800771 /* wake up the caller thread for sync decompression */
772 if (sync) {
Gao Xiang848bd9a2018-12-08 00:19:12 +0800773 unsigned long flags;
Gao Xiang3883a792018-07-26 20:22:06 +0800774
Gao Xiang848bd9a2018-12-08 00:19:12 +0800775 spin_lock_irqsave(&io->u.wait.lock, flags);
776 if (!atomic_add_return(bios, &io->pending_bios))
777 wake_up_locked(&io->u.wait);
778 spin_unlock_irqrestore(&io->u.wait.lock, flags);
779 return;
780 }
781
Huang Jianan648f2de2021-03-17 11:54:47 +0800782 if (atomic_add_return(bios, &io->pending_bios))
783 return;
Huang Jianan30048cd2021-03-17 11:54:48 +0800784 /* Use workqueue and sync decompression for atomic contexts only */
Huang Jianan648f2de2021-03-17 11:54:47 +0800785 if (in_atomic() || irqs_disabled()) {
Gao Xiang3883a792018-07-26 20:22:06 +0800786 queue_work(z_erofs_workqueue, &io->u.work);
Gao Xiange6242462021-10-07 15:02:23 +0800787 sbi->opt.readahead_sync_decompress = true;
Huang Jianan648f2de2021-03-17 11:54:47 +0800788 return;
789 }
790 z_erofs_decompressqueue_work(&io->u.work);
Gao Xiang3883a792018-07-26 20:22:06 +0800791}
792
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800793static bool z_erofs_page_is_invalidated(struct page *page)
794{
795 return !page->mapping && !z_erofs_is_shortlived_page(page);
796}
797
Gao Xiang0c638f72019-11-08 11:37:33 +0800798static void z_erofs_decompressqueue_endio(struct bio *bio)
Gao Xiang3883a792018-07-26 20:22:06 +0800799{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800800 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
801 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
Gao Xiang14a56ec2019-03-25 11:40:09 +0800802 blk_status_t err = bio->bi_status;
Gao Xiang3883a792018-07-26 20:22:06 +0800803 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800804 struct bvec_iter_all iter_all;
Gao Xiang3883a792018-07-26 20:22:06 +0800805
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200806 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang3883a792018-07-26 20:22:06 +0800807 struct page *page = bvec->bv_page;
808
809 DBG_BUGON(PageUptodate(page));
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800810 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800811
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800812 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800813 SetPageError(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800814
Gao Xianga4b1fab2019-10-08 20:56:15 +0800815 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
816 if (!err)
817 SetPageUptodate(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800818 unlock_page(page);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800819 }
Gao Xiang3883a792018-07-26 20:22:06 +0800820 }
Gao Xianga4b1fab2019-10-08 20:56:15 +0800821 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
Gao Xiang3883a792018-07-26 20:22:06 +0800822 bio_put(bio);
823}
824
Gao Xiang97e86a82019-07-31 23:57:47 +0800825static int z_erofs_decompress_pcluster(struct super_block *sb,
826 struct z_erofs_pcluster *pcl,
Gao Xiangeaa91722021-10-22 17:01:20 +0800827 struct page **pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800828{
829 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang3883a792018-07-26 20:22:06 +0800830 struct z_erofs_pagevec_ctor ctor;
Gao Xiang9f6cc762021-04-07 12:39:20 +0800831 unsigned int i, inputsize, outputsize, llen, nr_pages;
Gao Xiang97e86a82019-07-31 23:57:47 +0800832 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
Gao Xiang3883a792018-07-26 20:22:06 +0800833 struct page **pages, **compressed_pages, *page;
Gao Xiang3883a792018-07-26 20:22:06 +0800834
835 enum z_erofs_page_type page_type;
Gao Xiangb6a76182019-06-24 15:22:58 +0800836 bool overlapped, partial;
Gao Xiang97e86a82019-07-31 23:57:47 +0800837 struct z_erofs_collection *cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800838 int err;
839
840 might_sleep();
Gao Xiang97e86a82019-07-31 23:57:47 +0800841 cl = z_erofs_primarycollection(pcl);
842 DBG_BUGON(!READ_ONCE(cl->nr_pages));
Gao Xiang3883a792018-07-26 20:22:06 +0800843
Gao Xiang97e86a82019-07-31 23:57:47 +0800844 mutex_lock(&cl->lock);
845 nr_pages = cl->nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800846
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800847 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
Gao Xiang3883a792018-07-26 20:22:06 +0800848 pages = pages_onstack;
Gao Xiang97e86a82019-07-31 23:57:47 +0800849 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
850 mutex_trylock(&z_pagemap_global_lock)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800851 pages = z_pagemap_global;
Gao Xiang97e86a82019-07-31 23:57:47 +0800852 } else {
Chao Yu441dfcc2019-07-16 17:44:22 +0800853 gfp_t gfp_flags = GFP_KERNEL;
854
Gao Xiang97e86a82019-07-31 23:57:47 +0800855 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
Chao Yu441dfcc2019-07-16 17:44:22 +0800856 gfp_flags |= __GFP_NOFAIL;
857
Julian Merida447a3622019-03-18 20:58:41 -0300858 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Chao Yu441dfcc2019-07-16 17:44:22 +0800859 gfp_flags);
Gao Xiang3883a792018-07-26 20:22:06 +0800860
861 /* fallback to global pagemap for the lowmem scenario */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800862 if (!pages) {
Chao Yu441dfcc2019-07-16 17:44:22 +0800863 mutex_lock(&z_pagemap_global_lock);
864 pages = z_pagemap_global;
Gao Xiang3883a792018-07-26 20:22:06 +0800865 }
866 }
867
868 for (i = 0; i < nr_pages; ++i)
869 pages[i] = NULL;
870
Gao Xiange12a0ce2019-08-21 22:01:52 +0800871 err = 0;
Gao Xiangfa61a332019-06-24 15:22:53 +0800872 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang97e86a82019-07-31 23:57:47 +0800873 cl->pagevec, 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800874
Gao Xiang97e86a82019-07-31 23:57:47 +0800875 for (i = 0; i < cl->vcnt; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200876 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800877
Gao Xiang046d64e2019-07-31 23:57:45 +0800878 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800879
880 /* all pages in pagevec ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100881 DBG_BUGON(!page);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800882 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800883
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800884 if (z_erofs_put_shortlivedpage(pagepool, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800885 continue;
886
887 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
888 pagenr = 0;
889 else
890 pagenr = z_erofs_onlinepage_index(page);
891
Gao Xiang70b17992018-12-11 15:17:49 +0800892 DBG_BUGON(pagenr >= nr_pages);
Gao Xiange5e3abb2018-09-19 13:49:07 +0800893
Gao Xiange12a0ce2019-08-21 22:01:52 +0800894 /*
895 * currently EROFS doesn't support multiref(dedup),
896 * so here erroring out one multiref page.
897 */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800898 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800899 DBG_BUGON(1);
900 SetPageError(pages[pagenr]);
901 z_erofs_onlinepage_endio(pages[pagenr]);
902 err = -EFSCORRUPTED;
903 }
Gao Xiang3883a792018-07-26 20:22:06 +0800904 pages[pagenr] = page;
905 }
Gao Xiang3883a792018-07-26 20:22:06 +0800906 z_erofs_pagevec_ctor_exit(&ctor, true);
907
908 overlapped = false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800909 compressed_pages = pcl->compressed_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800910
Gao Xiang9f6cc762021-04-07 12:39:20 +0800911 for (i = 0; i < pcl->pclusterpages; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200912 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800913
914 page = compressed_pages[i];
915
916 /* all compressed pages ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100917 DBG_BUGON(!page);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800918 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800919
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800920 if (!z_erofs_is_shortlived_page(page)) {
Gao Xiangd61fbb62019-03-25 11:40:08 +0800921 if (erofs_page_is_managed(sbi, page)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800922 if (!PageUptodate(page))
Gao Xiang11152492019-03-25 11:40:07 +0800923 err = -EIO;
924 continue;
925 }
Gao Xiang3883a792018-07-26 20:22:06 +0800926
Gao Xiang11152492019-03-25 11:40:07 +0800927 /*
928 * only if non-head page can be selected
929 * for inplace decompression
930 */
931 pagenr = z_erofs_onlinepage_index(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800932
Gao Xiang11152492019-03-25 11:40:07 +0800933 DBG_BUGON(pagenr >= nr_pages);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800934 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800935 DBG_BUGON(1);
936 SetPageError(pages[pagenr]);
937 z_erofs_onlinepage_endio(pages[pagenr]);
938 err = -EFSCORRUPTED;
939 }
Gao Xiang11152492019-03-25 11:40:07 +0800940 pages[pagenr] = page;
Gao Xiang3883a792018-07-26 20:22:06 +0800941
Gao Xiang11152492019-03-25 11:40:07 +0800942 overlapped = true;
943 }
944
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800945 /* PG_error needs checking for all non-managed pages */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800946 if (PageError(page)) {
Gao Xiang11152492019-03-25 11:40:07 +0800947 DBG_BUGON(PageUptodate(page));
948 err = -EIO;
949 }
Gao Xiang3883a792018-07-26 20:22:06 +0800950 }
951
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800952 if (err)
Gao Xiang11152492019-03-25 11:40:07 +0800953 goto out;
954
Gao Xiang97e86a82019-07-31 23:57:47 +0800955 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
956 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
957 outputsize = llen;
958 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
Gao Xiangb6a76182019-06-24 15:22:58 +0800959 } else {
Gao Xiang97e86a82019-07-31 23:57:47 +0800960 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
Gao Xiangb6a76182019-06-24 15:22:58 +0800961 partial = true;
962 }
Gao Xiang3883a792018-07-26 20:22:06 +0800963
Gao Xiang9f6cc762021-04-07 12:39:20 +0800964 inputsize = pcl->pclusterpages * PAGE_SIZE;
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800965 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
966 .sb = sb,
967 .in = compressed_pages,
968 .out = pages,
Gao Xiang97e86a82019-07-31 23:57:47 +0800969 .pageofs_out = cl->pageofs,
Gao Xiang9f6cc762021-04-07 12:39:20 +0800970 .inputsize = inputsize,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800971 .outputsize = outputsize,
Gao Xiang97e86a82019-07-31 23:57:47 +0800972 .alg = pcl->algorithmformat,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800973 .inplace_io = overlapped,
Gao Xiangb6a76182019-06-24 15:22:58 +0800974 .partial_decoding = partial
Gao Xiang97e86a82019-07-31 23:57:47 +0800975 }, pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800976
977out:
Ruiqi Gongfe6adcc2021-03-31 05:39:20 -0400978 /* must handle all compressed pages before ending pages */
Gao Xiang9f6cc762021-04-07 12:39:20 +0800979 for (i = 0; i < pcl->pclusterpages; ++i) {
Gao Xiang3883a792018-07-26 20:22:06 +0800980 page = compressed_pages[i];
981
Gao Xiangd61fbb62019-03-25 11:40:08 +0800982 if (erofs_page_is_managed(sbi, page))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800983 continue;
Gao Xiangd61fbb62019-03-25 11:40:08 +0800984
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800985 /* recycle all individual short-lived pages */
986 (void)z_erofs_put_shortlivedpage(pagepool, page);
Gao Xiang3883a792018-07-26 20:22:06 +0800987
988 WRITE_ONCE(compressed_pages[i], NULL);
989 }
990
Gao Xiangaf692e12019-02-27 13:33:30 +0800991 for (i = 0; i < nr_pages; ++i) {
992 page = pages[i];
993 if (!page)
994 continue;
995
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800996 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiangaf692e12019-02-27 13:33:30 +0800997
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800998 /* recycle all individual short-lived pages */
999 if (z_erofs_put_shortlivedpage(pagepool, page))
Gao Xiangaf692e12019-02-27 13:33:30 +08001000 continue;
1001
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001002 if (err < 0)
Gao Xiangaf692e12019-02-27 13:33:30 +08001003 SetPageError(page);
1004
1005 z_erofs_onlinepage_endio(page);
1006 }
1007
Gao Xiang3883a792018-07-26 20:22:06 +08001008 if (pages == z_pagemap_global)
1009 mutex_unlock(&z_pagemap_global_lock);
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001010 else if (pages != pages_onstack)
Gao Xiang3883a792018-07-26 20:22:06 +08001011 kvfree(pages);
1012
Gao Xiang97e86a82019-07-31 23:57:47 +08001013 cl->nr_pages = 0;
1014 cl->vcnt = 0;
Gao Xiang3883a792018-07-26 20:22:06 +08001015
Gao Xiang97e86a82019-07-31 23:57:47 +08001016 /* all cl locks MUST be taken before the following line */
1017 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001018
Gao Xiang97e86a82019-07-31 23:57:47 +08001019 /* all cl locks SHOULD be released right now */
1020 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +08001021
Gao Xiang97e86a82019-07-31 23:57:47 +08001022 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +08001023 return err;
1024}
1025
Gao Xiang0c638f72019-11-08 11:37:33 +08001026static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
Gao Xiangeaa91722021-10-22 17:01:20 +08001027 struct page **pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +08001028{
Gao Xiang97e86a82019-07-31 23:57:47 +08001029 z_erofs_next_pcluster_t owned = io->head;
Gao Xiang3883a792018-07-26 20:22:06 +08001030
Gao Xiang97e86a82019-07-31 23:57:47 +08001031 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1032 struct z_erofs_pcluster *pcl;
Gao Xiang3883a792018-07-26 20:22:06 +08001033
1034 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
Gao Xiang97e86a82019-07-31 23:57:47 +08001035 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001036
1037 /* no possible that 'owned' equals NULL */
Gao Xiang97e86a82019-07-31 23:57:47 +08001038 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001039
Gao Xiang97e86a82019-07-31 23:57:47 +08001040 pcl = container_of(owned, struct z_erofs_pcluster, next);
1041 owned = READ_ONCE(pcl->next);
Gao Xiang3883a792018-07-26 20:22:06 +08001042
Gao Xianga4b1fab2019-10-08 20:56:15 +08001043 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
Gao Xiang3978c8e2018-08-06 11:27:53 +08001044 }
Gao Xiang3883a792018-07-26 20:22:06 +08001045}
1046
Gao Xiang0c638f72019-11-08 11:37:33 +08001047static void z_erofs_decompressqueue_work(struct work_struct *work)
Gao Xiang3883a792018-07-26 20:22:06 +08001048{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001049 struct z_erofs_decompressqueue *bgq =
1050 container_of(work, struct z_erofs_decompressqueue, u.work);
Gao Xiangeaa91722021-10-22 17:01:20 +08001051 struct page *pagepool = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001052
Gao Xianga4b1fab2019-10-08 20:56:15 +08001053 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang0c638f72019-11-08 11:37:33 +08001054 z_erofs_decompress_queue(bgq, &pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001055
Gao Xiangeaa91722021-10-22 17:01:20 +08001056 erofs_release_pages(&pagepool);
Gao Xianga4b1fab2019-10-08 20:56:15 +08001057 kvfree(bgq);
Gao Xiang3883a792018-07-26 20:22:06 +08001058}
1059
Gao Xiang97e86a82019-07-31 23:57:47 +08001060static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1061 unsigned int nr,
Gao Xiangeaa91722021-10-22 17:01:20 +08001062 struct page **pagepool,
Gao Xiang97e86a82019-07-31 23:57:47 +08001063 struct address_space *mc,
1064 gfp_t gfp)
Gao Xiang9248fce2018-12-08 00:19:15 +08001065{
Gao Xiang97e86a82019-07-31 23:57:47 +08001066 const pgoff_t index = pcl->obj.index;
Gao Xiang9248fce2018-12-08 00:19:15 +08001067 bool tocache = false;
1068
1069 struct address_space *mapping;
1070 struct page *oldpage, *page;
1071
Gao Xiang92e6efd2018-12-08 00:19:16 +08001072 compressed_page_t t;
1073 int justfound;
1074
Gao Xiang9248fce2018-12-08 00:19:15 +08001075repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +08001076 page = READ_ONCE(pcl->compressed_pages[nr]);
Gao Xiang9248fce2018-12-08 00:19:15 +08001077 oldpage = page;
1078
1079 if (!page)
1080 goto out_allocpage;
1081
Gao Xiang92e6efd2018-12-08 00:19:16 +08001082 /* process the target tagged pointer */
1083 t = tagptr_init(compressed_page_t, page);
1084 justfound = tagptr_unfold_tags(t);
1085 page = tagptr_unfold_ptr(t);
1086
Gao Xiang1825c8d2020-12-09 20:37:17 +08001087 /*
1088 * preallocated cached pages, which is used to avoid direct reclaim
1089 * otherwise, it will go inplace I/O path instead.
1090 */
1091 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1092 WRITE_ONCE(pcl->compressed_pages[nr], page);
1093 set_page_private(page, 0);
1094 tocache = true;
1095 goto out_tocache;
1096 }
Gao Xiang9248fce2018-12-08 00:19:15 +08001097 mapping = READ_ONCE(page->mapping);
1098
1099 /*
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001100 * file-backed online pages in plcuster are all locked steady,
Gao Xiang9248fce2018-12-08 00:19:15 +08001101 * therefore it is impossible for `mapping' to be NULL.
1102 */
1103 if (mapping && mapping != mc)
1104 /* ought to be unmanaged pages */
1105 goto out;
1106
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001107 /* directly return for shortlived page as well */
1108 if (z_erofs_is_shortlived_page(page))
1109 goto out;
1110
Gao Xiang9248fce2018-12-08 00:19:15 +08001111 lock_page(page);
1112
Gao Xiang92e6efd2018-12-08 00:19:16 +08001113 /* only true if page reclaim goes wrong, should never happen */
1114 DBG_BUGON(justfound && PagePrivate(page));
1115
Gao Xiang9248fce2018-12-08 00:19:15 +08001116 /* the page is still in manage cache */
1117 if (page->mapping == mc) {
Gao Xiang97e86a82019-07-31 23:57:47 +08001118 WRITE_ONCE(pcl->compressed_pages[nr], page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001119
Gao Xiang11152492019-03-25 11:40:07 +08001120 ClearPageError(page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001121 if (!PagePrivate(page)) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001122 /*
1123 * impossible to be !PagePrivate(page) for
1124 * the current restriction as well if
1125 * the page is already in compressed_pages[].
1126 */
1127 DBG_BUGON(!justfound);
1128
1129 justfound = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +08001130 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001131 SetPagePrivate(page);
1132 }
1133
1134 /* no need to submit io if it is already up-to-date */
1135 if (PageUptodate(page)) {
1136 unlock_page(page);
1137 page = NULL;
1138 }
1139 goto out;
1140 }
1141
1142 /*
1143 * the managed page has been truncated, it's unsafe to
1144 * reuse this one, let's allocate a new cache-managed page.
1145 */
1146 DBG_BUGON(page->mapping);
Gao Xiang92e6efd2018-12-08 00:19:16 +08001147 DBG_BUGON(!justfound);
Gao Xiang9248fce2018-12-08 00:19:15 +08001148
1149 tocache = true;
1150 unlock_page(page);
1151 put_page(page);
1152out_allocpage:
Gao Xiang5ddcee12019-11-21 21:59:54 +08001153 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
Gao Xiang5ddcee12019-11-21 21:59:54 +08001154 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
Gao Xiangeaa91722021-10-22 17:01:20 +08001155 erofs_pagepool_add(pagepool, page);
Gao Xiang5ddcee12019-11-21 21:59:54 +08001156 cond_resched();
1157 goto repeat;
1158 }
Gao Xiang1825c8d2020-12-09 20:37:17 +08001159out_tocache:
Gao Xiangbf225072020-12-08 17:58:33 +08001160 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1161 /* turn into temporary page if fails (1 ref) */
1162 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1163 goto out;
Gao Xianga30573b2020-10-22 22:57:21 +08001164 }
Gao Xiangbf225072020-12-08 17:58:33 +08001165 attach_page_private(page, pcl);
1166 /* drop a refcount added by allocpage (then we have 2 refs here) */
1167 put_page(page);
1168
Gao Xiang9248fce2018-12-08 00:19:15 +08001169out: /* the only exit (for tracing and debugging) */
1170 return page;
1171}
1172
Gao Xianga4b1fab2019-10-08 20:56:15 +08001173static struct z_erofs_decompressqueue *
1174jobqueue_init(struct super_block *sb,
1175 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001176{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001177 struct z_erofs_decompressqueue *q;
Gao Xiang3883a792018-07-26 20:22:06 +08001178
Gao Xianga4b1fab2019-10-08 20:56:15 +08001179 if (fg && !*fg) {
1180 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1181 if (!q) {
1182 *fg = true;
1183 goto fg_out;
1184 }
Gao Xiang0c638f72019-11-08 11:37:33 +08001185 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
Gao Xianga4b1fab2019-10-08 20:56:15 +08001186 } else {
1187fg_out:
1188 q = fgq;
1189 init_waitqueue_head(&fgq->u.wait);
1190 atomic_set(&fgq->pending_bios, 0);
Gao Xiang3883a792018-07-26 20:22:06 +08001191 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001192 q->sb = sb;
1193 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1194 return q;
Gao Xiang3883a792018-07-26 20:22:06 +08001195}
1196
Gao Xiang97e86a82019-07-31 23:57:47 +08001197/* define decompression jobqueue types */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001198enum {
Gao Xiang7146a4f2018-12-08 00:19:18 +08001199 JQ_BYPASS,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001200 JQ_SUBMIT,
1201 NR_JOBQUEUES,
1202};
1203
1204static void *jobqueueset_init(struct super_block *sb,
Gao Xianga4b1fab2019-10-08 20:56:15 +08001205 struct z_erofs_decompressqueue *q[],
1206 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001207{
Gao Xiang7146a4f2018-12-08 00:19:18 +08001208 /*
1209 * if managed cache is enabled, bypass jobqueue is needed,
Gao Xiang97e86a82019-07-31 23:57:47 +08001210 * no need to read from device for all pclusters in this queue.
Gao Xiang7146a4f2018-12-08 00:19:18 +08001211 */
Gao Xianga4b1fab2019-10-08 20:56:15 +08001212 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1213 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001214
Gao Xianga4b1fab2019-10-08 20:56:15 +08001215 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
Gao Xiang7146a4f2018-12-08 00:19:18 +08001216}
1217
Gao Xiang97e86a82019-07-31 23:57:47 +08001218static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1219 z_erofs_next_pcluster_t qtail[],
1220 z_erofs_next_pcluster_t owned_head)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001221{
Gao Xiang97e86a82019-07-31 23:57:47 +08001222 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1223 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001224
Gao Xiang97e86a82019-07-31 23:57:47 +08001225 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1226 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1227 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001228
Gao Xiang97e86a82019-07-31 23:57:47 +08001229 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001230
1231 WRITE_ONCE(*submit_qtail, owned_head);
Gao Xiang97e86a82019-07-31 23:57:47 +08001232 WRITE_ONCE(*bypass_qtail, &pcl->next);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001233
Gao Xiang97e86a82019-07-31 23:57:47 +08001234 qtail[JQ_BYPASS] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001235}
1236
Gao Xiang1e4a2952020-01-21 14:48:19 +08001237static void z_erofs_submit_queue(struct super_block *sb,
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001238 struct z_erofs_decompress_frontend *f,
Gao Xiangeaa91722021-10-22 17:01:20 +08001239 struct page **pagepool,
Gao Xiang0c638f72019-11-08 11:37:33 +08001240 struct z_erofs_decompressqueue *fgq,
1241 bool *force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001242{
Gao Xiangbda17a42019-10-08 20:56:13 +08001243 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +08001244 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
Gao Xianga4b1fab2019-10-08 20:56:15 +08001245 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001246 void *bi_private;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001247 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001248 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
Kees Cook3f649ab2020-06-03 13:09:38 -07001249 pgoff_t last_index;
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001250 struct block_device *last_bdev;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001251 unsigned int nr_bios = 0;
1252 struct bio *bio = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001253
Gao Xianga4b1fab2019-10-08 20:56:15 +08001254 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1255 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1256 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
Gao Xiang3883a792018-07-26 20:22:06 +08001257
1258 /* by default, all need io submission */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001259 q[JQ_SUBMIT]->head = owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001260
1261 do {
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001262 struct erofs_map_dev mdev;
Gao Xiang97e86a82019-07-31 23:57:47 +08001263 struct z_erofs_pcluster *pcl;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001264 pgoff_t cur, end;
1265 unsigned int i = 0;
1266 bool bypass = true;
Gao Xiang3883a792018-07-26 20:22:06 +08001267
1268 /* no possible 'owned_head' equals the following */
Gao Xiang97e86a82019-07-31 23:57:47 +08001269 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1270 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001271
Gao Xiang97e86a82019-07-31 23:57:47 +08001272 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1273
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001274 /* no device id here, thus it will always succeed */
1275 mdev = (struct erofs_map_dev) {
1276 .m_pa = blknr_to_addr(pcl->obj.index),
1277 };
1278 (void)erofs_map_dev(sb, &mdev);
1279
1280 cur = erofs_blknr(mdev.m_pa);
Gao Xiang9f6cc762021-04-07 12:39:20 +08001281 end = cur + pcl->pclusterpages;
Gao Xiang3883a792018-07-26 20:22:06 +08001282
1283 /* close the main owned chain at first */
Gao Xiang97e86a82019-07-31 23:57:47 +08001284 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1285 Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +08001286
Gao Xiang1e4a2952020-01-21 14:48:19 +08001287 do {
1288 struct page *page;
Gao Xiang9248fce2018-12-08 00:19:15 +08001289
Gao Xiang1e4a2952020-01-21 14:48:19 +08001290 page = pickup_page_for_submission(pcl, i++, pagepool,
1291 MNGD_MAPPING(sbi),
1292 GFP_NOFS);
1293 if (!page)
1294 continue;
Gao Xiang3883a792018-07-26 20:22:06 +08001295
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001296 if (bio && (cur != last_index + 1 ||
1297 last_bdev != mdev.m_bdev)) {
Gao Xiang3883a792018-07-26 20:22:06 +08001298submit_bio_retry:
Gao Xiang1e4a2952020-01-21 14:48:19 +08001299 submit_bio(bio);
1300 bio = NULL;
1301 }
Gao Xiang3883a792018-07-26 20:22:06 +08001302
Gao Xiang1e4a2952020-01-21 14:48:19 +08001303 if (!bio) {
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001304 bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS);
Gao Xiang1e4a2952020-01-21 14:48:19 +08001305 bio->bi_end_io = z_erofs_decompressqueue_endio;
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001306
1307 bio_set_dev(bio, mdev.m_bdev);
1308 last_bdev = mdev.m_bdev;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001309 bio->bi_iter.bi_sector = (sector_t)cur <<
1310 LOG_SECTORS_PER_BLOCK;
1311 bio->bi_private = bi_private;
1312 bio->bi_opf = REQ_OP_READ;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001313 if (f->readahead)
1314 bio->bi_opf |= REQ_RAHEAD;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001315 ++nr_bios;
1316 }
Gao Xiang94e4e152019-09-04 10:09:04 +08001317
Gao Xiang6c3e4852020-09-19 15:27:28 +08001318 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
Gao Xiang1e4a2952020-01-21 14:48:19 +08001319 goto submit_bio_retry;
Gao Xiang3883a792018-07-26 20:22:06 +08001320
Gao Xiang1e4a2952020-01-21 14:48:19 +08001321 last_index = cur;
1322 bypass = false;
1323 } while (++cur < end);
Gao Xiang3883a792018-07-26 20:22:06 +08001324
Gao Xiang1e4a2952020-01-21 14:48:19 +08001325 if (!bypass)
Gao Xiang97e86a82019-07-31 23:57:47 +08001326 qtail[JQ_SUBMIT] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001327 else
Gao Xiang97e86a82019-07-31 23:57:47 +08001328 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1329 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001330
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001331 if (bio)
Gao Xiang94e4e152019-09-04 10:09:04 +08001332 submit_bio(bio);
Gao Xiang3883a792018-07-26 20:22:06 +08001333
Gao Xiang587a67b2020-01-21 14:47:47 +08001334 /*
1335 * although background is preferred, no one is pending for submission.
1336 * don't issue workqueue for decompression but drop it directly instead.
1337 */
1338 if (!*force_fg && !nr_bios) {
1339 kvfree(q[JQ_SUBMIT]);
Gao Xiang1e4a2952020-01-21 14:48:19 +08001340 return;
Gao Xiang587a67b2020-01-21 14:47:47 +08001341 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001342 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
Gao Xiang3883a792018-07-26 20:22:06 +08001343}
1344
Gao Xiang0c638f72019-11-08 11:37:33 +08001345static void z_erofs_runqueue(struct super_block *sb,
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001346 struct z_erofs_decompress_frontend *f,
Gao Xiangeaa91722021-10-22 17:01:20 +08001347 struct page **pagepool, bool force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001348{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001349 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001350
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001351 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +08001352 return;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001353 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
Gao Xiang3883a792018-07-26 20:22:06 +08001354
Gao Xiang0c638f72019-11-08 11:37:33 +08001355 /* handle bypass queue (no i/o pclusters) immediately */
1356 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
Gao Xiang4279f3f2019-07-31 23:57:49 +08001357
Gao Xiang3883a792018-07-26 20:22:06 +08001358 if (!force_fg)
1359 return;
1360
1361 /* wait until all bios are completed */
Gao Xianga93f8c32019-10-08 20:56:16 +08001362 io_wait_event(io[JQ_SUBMIT].u.wait,
1363 !atomic_read(&io[JQ_SUBMIT].pending_bios));
Gao Xiang3883a792018-07-26 20:22:06 +08001364
Gao Xiang0c638f72019-11-08 11:37:33 +08001365 /* handle synchronous decompress queue in the caller context */
1366 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001367}
1368
Gao Xiang38629292021-10-09 04:08:39 +08001369/*
1370 * Since partial uptodate is still unimplemented for now, we have to use
1371 * approximate readmore strategies as a start.
1372 */
1373static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1374 struct readahead_control *rac,
1375 erofs_off_t end,
Gao Xiangeaa91722021-10-22 17:01:20 +08001376 struct page **pagepool,
Gao Xiang38629292021-10-09 04:08:39 +08001377 bool backmost)
1378{
1379 struct inode *inode = f->inode;
1380 struct erofs_map_blocks *map = &f->map;
1381 erofs_off_t cur;
1382 int err;
1383
1384 if (backmost) {
1385 map->m_la = end;
Gao Xiang622cead2021-10-11 05:31:45 +08001386 err = z_erofs_map_blocks_iter(inode, map,
1387 EROFS_GET_BLOCKS_READMORE);
Gao Xiang38629292021-10-09 04:08:39 +08001388 if (err)
1389 return;
1390
1391 /* expend ra for the trailing edge if readahead */
1392 if (rac) {
1393 loff_t newstart = readahead_pos(rac);
1394
1395 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1396 readahead_expand(rac, newstart, cur - newstart);
1397 return;
1398 }
1399 end = round_up(end, PAGE_SIZE);
1400 } else {
1401 end = round_up(map->m_la, PAGE_SIZE);
1402
1403 if (!map->m_llen)
1404 return;
1405 }
1406
1407 cur = map->m_la + map->m_llen - 1;
1408 while (cur >= end) {
1409 pgoff_t index = cur >> PAGE_SHIFT;
1410 struct page *page;
1411
1412 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1413 if (!page)
1414 goto skip;
1415
1416 if (PageUptodate(page)) {
1417 unlock_page(page);
1418 put_page(page);
1419 goto skip;
1420 }
1421
1422 err = z_erofs_do_read_page(f, page, pagepool);
1423 if (err)
1424 erofs_err(inode->i_sb,
1425 "readmore error at page %lu @ nid %llu",
1426 index, EROFS_I(inode)->nid);
1427 put_page(page);
1428skip:
1429 if (cur < PAGE_SIZE)
1430 break;
1431 cur = (index << PAGE_SHIFT) - 1;
1432 }
1433}
1434
Gao Xiang0c638f72019-11-08 11:37:33 +08001435static int z_erofs_readpage(struct file *file, struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +08001436{
1437 struct inode *const inode = page->mapping->host;
Gao Xiang97e86a82019-07-31 23:57:47 +08001438 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiangeaa91722021-10-22 17:01:20 +08001439 struct page *pagepool = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001440 int err;
Gao Xiang3883a792018-07-26 20:22:06 +08001441
Gao Xiangba9ce772018-11-23 01:15:58 +08001442 trace_erofs_readpage(page, false);
Gao Xiangf0c519f2018-11-23 01:21:49 +08001443 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1444
Gao Xiang38629292021-10-09 04:08:39 +08001445 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
1446 &pagepool, true);
Gao Xiang1825c8d2020-12-09 20:37:17 +08001447 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xiang38629292021-10-09 04:08:39 +08001448 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
1449
Gao Xiang97e86a82019-07-31 23:57:47 +08001450 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001451
Gao Xiangee451972019-08-19 18:34:21 +08001452 /* if some compressed cluster ready, need submit them anyway */
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001453 z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
Gao Xiangee451972019-08-19 18:34:21 +08001454
1455 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001456 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
Gao Xiangee451972019-08-19 18:34:21 +08001457
Chao Yu3b423412019-01-15 09:42:21 +08001458 if (f.map.mpage)
1459 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001460
Gao Xiangeaa91722021-10-22 17:01:20 +08001461 erofs_release_pages(&pagepool);
Gao Xiangee451972019-08-19 18:34:21 +08001462 return err;
Gao Xiang3883a792018-07-26 20:22:06 +08001463}
1464
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001465static void z_erofs_readahead(struct readahead_control *rac)
Gao Xiang3883a792018-07-26 20:22:06 +08001466{
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001467 struct inode *const inode = rac->mapping->host;
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001468 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Gao Xiang97e86a82019-07-31 23:57:47 +08001469 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiangeaa91722021-10-22 17:01:20 +08001470 struct page *pagepool = NULL, *head = NULL, *page;
Gao Xiang38629292021-10-09 04:08:39 +08001471 unsigned int nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +08001472
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001473 f.readahead = true;
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001474 f.headoffset = readahead_pos(rac);
Gao Xiangf0c519f2018-11-23 01:21:49 +08001475
Gao Xiang38629292021-10-09 04:08:39 +08001476 z_erofs_pcluster_readmore(&f, rac, f.headoffset +
1477 readahead_length(rac) - 1, &pagepool, true);
1478 nr_pages = readahead_count(rac);
1479 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1480
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001481 while ((page = readahead_page(rac))) {
Gao Xiang3883a792018-07-26 20:22:06 +08001482 set_page_private(page, (unsigned long)head);
1483 head = page;
1484 }
1485
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001486 while (head) {
Gao Xiang3883a792018-07-26 20:22:06 +08001487 struct page *page = head;
1488 int err;
1489
1490 /* traversal in reverse order */
1491 head = (void *)page_private(page);
1492
Gao Xiang1825c8d2020-12-09 20:37:17 +08001493 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xianga5876e22019-09-04 10:08:56 +08001494 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001495 erofs_err(inode->i_sb,
1496 "readahead error at page %lu @ nid %llu",
1497 page->index, EROFS_I(inode)->nid);
Gao Xiang3883a792018-07-26 20:22:06 +08001498 put_page(page);
1499 }
Gao Xiang38629292021-10-09 04:08:39 +08001500 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
Gao Xiang97e86a82019-07-31 23:57:47 +08001501 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001502
Gao Xiang38629292021-10-09 04:08:39 +08001503 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1504 sbi->opt.readahead_sync_decompress &&
1505 nr_pages <= sbi->opt.max_sync_decompress_pages);
Chao Yu3b423412019-01-15 09:42:21 +08001506 if (f.map.mpage)
1507 put_page(f.map.mpage);
Gao Xiangeaa91722021-10-22 17:01:20 +08001508 erofs_release_pages(&pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001509}
1510
Gao Xiang0c638f72019-11-08 11:37:33 +08001511const struct address_space_operations z_erofs_aops = {
1512 .readpage = z_erofs_readpage,
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001513 .readahead = z_erofs_readahead,
Gao Xiang3883a792018-07-26 20:22:06 +08001514};