blob: e59e22852c784e980f49dc864a0dc5de1e47fb9c [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
99/*
Gao Xiang672e5472018-12-08 00:19:14 +0800100 * a compressed_pages[] placeholder in order to avoid
101 * being filled with file pages for in-place decompression.
102 */
103#define PAGE_UNALLOCATED ((void *)0x5F0E4B1D)
104
Gao Xiang97e86a82019-07-31 23:57:47 +0800105/* how to allocate cached pages for a pcluster */
Gao Xiang92e6efd2018-12-08 00:19:16 +0800106enum z_erofs_cache_alloctype {
107 DONTALLOC, /* don't allocate any cached pages */
108 DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */
Gao Xiang1825c8d2020-12-09 20:37:17 +0800109 /*
110 * try to use cached I/O if page allocation succeeds or fallback
111 * to in-place I/O instead to avoid any direct reclaim.
112 */
113 TRYALLOC,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800114};
115
116/*
117 * tagged pointer with 1-bit tag for all compressed pages
118 * tag 0 - the page is just found with an extra page reference
119 */
120typedef tagptr1_t compressed_page_t;
121
122#define tag_compressed_page_justfound(page) \
123 tagptr_fold(compressed_page_t, page, 1)
124
Gao Xiang3883a792018-07-26 20:22:06 +0800125static struct workqueue_struct *z_erofs_workqueue __read_mostly;
Gao Xiang3883a792018-07-26 20:22:06 +0800126
127void z_erofs_exit_zip_subsystem(void)
128{
Gao Xiang3883a792018-07-26 20:22:06 +0800129 destroy_workqueue(z_erofs_workqueue);
Gao Xiang9f6cc762021-04-07 12:39:20 +0800130 z_erofs_destroy_pcluster_pool();
Gao Xiang3883a792018-07-26 20:22:06 +0800131}
132
Gao Xiang99634bf2019-09-04 10:09:05 +0800133static inline int z_erofs_init_workqueue(void)
Gao Xiang3883a792018-07-26 20:22:06 +0800134{
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200135 const unsigned int onlinecpus = num_possible_cpus();
Gao Xiang3883a792018-07-26 20:22:06 +0800136
137 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800138 * no need to spawn too many threads, limiting threads could minimum
139 * scheduling overhead, perhaps per-CPU threads should be better?
Gao Xiang3883a792018-07-26 20:22:06 +0800140 */
Gao Xiang0e62ea32020-07-31 10:40:49 +0800141 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
142 WQ_UNBOUND | WQ_HIGHPRI,
Gao Xiang97e86a82019-07-31 23:57:47 +0800143 onlinecpus + onlinecpus / 4);
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100144 return z_erofs_workqueue ? 0 : -ENOMEM;
Gao Xiang3883a792018-07-26 20:22:06 +0800145}
146
Gao Xiang0a0b7e62018-10-09 21:43:53 +0800147int __init z_erofs_init_zip_subsystem(void)
Gao Xiang3883a792018-07-26 20:22:06 +0800148{
Gao Xiang9f6cc762021-04-07 12:39:20 +0800149 int err = z_erofs_create_pcluster_pool();
Gao Xiang3883a792018-07-26 20:22:06 +0800150
Gao Xiang9f6cc762021-04-07 12:39:20 +0800151 if (err)
152 return err;
153 err = z_erofs_init_workqueue();
154 if (err)
155 z_erofs_destroy_pcluster_pool();
156 return err;
Gao Xiang3883a792018-07-26 20:22:06 +0800157}
158
Gao Xiang97e86a82019-07-31 23:57:47 +0800159enum z_erofs_collectmode {
160 COLLECT_SECONDARY,
161 COLLECT_PRIMARY,
Gao Xiang3883a792018-07-26 20:22:06 +0800162 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800163 * The current collection was the tail of an exist chain, in addition
164 * that the previous processed chained collections are all decided to
165 * be hooked up to it.
166 * A new chain will be created for the remaining collections which are
167 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
168 * the next collection cannot reuse the whole page safely in
169 * the following scenario:
Gao Xianga1121522019-02-27 13:33:32 +0800170 * ________________________________________________________________
171 * | tail (partial) page | head (partial) page |
Gao Xiang97e86a82019-07-31 23:57:47 +0800172 * | (belongs to the next cl) | (belongs to the current cl) |
Gao Xianga1121522019-02-27 13:33:32 +0800173 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
174 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800175 COLLECT_PRIMARY_HOOKED,
Gao Xiang0b964602021-03-22 02:32:27 +0800176 /*
177 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
178 * could be dispatched into bypass queue later due to uptodated managed
179 * pages. All related online pages cannot be reused for inplace I/O (or
180 * pagevec) since it can be directly decoded without I/O submission.
181 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800182 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
Gao Xianga1121522019-02-27 13:33:32 +0800183 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800184 * The current collection has been linked with the owned chain, and
185 * could also be linked with the remaining collections, which means
186 * if the processing page is the tail page of the collection, thus
187 * the current collection can safely use the whole page (since
188 * the previous collection is under control) for in-place I/O, as
189 * illustrated below:
Gao Xianga1121522019-02-27 13:33:32 +0800190 * ________________________________________________________________
Gao Xiang97e86a82019-07-31 23:57:47 +0800191 * | tail (partial) page | head (partial) page |
192 * | (of the current cl) | (of the previous collection) |
193 * | PRIMARY_FOLLOWED or | |
194 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
Gao Xianga1121522019-02-27 13:33:32 +0800195 *
Gao Xiang97e86a82019-07-31 23:57:47 +0800196 * [ (*) the above page can be used as inplace I/O. ]
Gao Xiang3883a792018-07-26 20:22:06 +0800197 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800198 COLLECT_PRIMARY_FOLLOWED,
Gao Xiang3883a792018-07-26 20:22:06 +0800199};
200
Gao Xiang97e86a82019-07-31 23:57:47 +0800201struct z_erofs_collector {
Gao Xiang3883a792018-07-26 20:22:06 +0800202 struct z_erofs_pagevec_ctor vector;
203
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800204 struct z_erofs_pcluster *pcl, *tailpcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800205 struct z_erofs_collection *cl;
Gao Xiang81382f52021-04-07 12:39:21 +0800206 /* a pointer used to pick up inplace I/O pages */
207 struct page **icpage_ptr;
Gao Xiang97e86a82019-07-31 23:57:47 +0800208 z_erofs_next_pcluster_t owned_head;
209
210 enum z_erofs_collectmode mode;
Gao Xiang3883a792018-07-26 20:22:06 +0800211};
212
Gao Xiang97e86a82019-07-31 23:57:47 +0800213struct z_erofs_decompress_frontend {
214 struct inode *const inode;
215
216 struct z_erofs_collector clt;
217 struct erofs_map_blocks map;
218
Gao Xiang6ea5aad2020-09-19 15:27:30 +0800219 bool readahead;
Gao Xiang97e86a82019-07-31 23:57:47 +0800220 /* used for applying cache strategy on the fly */
221 bool backmost;
222 erofs_off_t headoffset;
223};
224
225#define COLLECTOR_INIT() { \
226 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
227 .mode = COLLECT_PRIMARY_FOLLOWED }
228
229#define DECOMPRESS_FRONTEND_INIT(__i) { \
230 .inode = __i, .clt = COLLECTOR_INIT(), \
231 .backmost = true, }
232
233static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
234static DEFINE_MUTEX(z_pagemap_global_lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800235
Gao Xiang97e86a82019-07-31 23:57:47 +0800236static void preload_compressed_pages(struct z_erofs_collector *clt,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800237 struct address_space *mc,
Gao Xiang1825c8d2020-12-09 20:37:17 +0800238 enum z_erofs_cache_alloctype type,
239 struct list_head *pagepool)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800240{
Gao Xiang81382f52021-04-07 12:39:21 +0800241 struct z_erofs_pcluster *pcl = clt->pcl;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800242 bool standalone = true;
Gao Xiang1825c8d2020-12-09 20:37:17 +0800243 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
244 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
Gao Xiang81382f52021-04-07 12:39:21 +0800245 struct page **pages;
246 pgoff_t index;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800247
Gao Xiang97e86a82019-07-31 23:57:47 +0800248 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800249 return;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800250
Gao Xiang81382f52021-04-07 12:39:21 +0800251 pages = pcl->compressed_pages;
252 index = pcl->obj.index;
253 for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
Gao Xiang92e6efd2018-12-08 00:19:16 +0800254 struct page *page;
255 compressed_page_t t;
Gao Xiang1825c8d2020-12-09 20:37:17 +0800256 struct page *newpage = NULL;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800257
258 /* the compressed page was loaded before */
Gao Xiang97e86a82019-07-31 23:57:47 +0800259 if (READ_ONCE(*pages))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800260 continue;
261
Gao Xiang97e86a82019-07-31 23:57:47 +0800262 page = find_get_page(mc, index);
Gao Xiang92e6efd2018-12-08 00:19:16 +0800263
264 if (page) {
265 t = tag_compressed_page_justfound(page);
Gao Xiang0b964602021-03-22 02:32:27 +0800266 } else {
267 /* I/O is needed, no possible to decompress directly */
Gao Xiang92e6efd2018-12-08 00:19:16 +0800268 standalone = false;
Gao Xiang0b964602021-03-22 02:32:27 +0800269 switch (type) {
270 case DELAYEDALLOC:
271 t = tagptr_init(compressed_page_t,
272 PAGE_UNALLOCATED);
273 break;
274 case TRYALLOC:
275 newpage = erofs_allocpage(pagepool, gfp);
276 if (!newpage)
277 continue;
278 set_page_private(newpage,
279 Z_EROFS_PREALLOCATED_PAGE);
280 t = tag_compressed_page_justfound(newpage);
281 break;
282 default: /* DONTALLOC */
283 continue;
284 }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800285 }
286
Gao Xiang97e86a82019-07-31 23:57:47 +0800287 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800288 continue;
289
Gao Xiang1825c8d2020-12-09 20:37:17 +0800290 if (page) {
Gao Xiang92e6efd2018-12-08 00:19:16 +0800291 put_page(page);
Gao Xiang1825c8d2020-12-09 20:37:17 +0800292 } else if (newpage) {
293 set_page_private(newpage, 0);
294 list_add(&newpage->lru, pagepool);
295 }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800296 }
Gao Xiang92e6efd2018-12-08 00:19:16 +0800297
Gao Xiang0b964602021-03-22 02:32:27 +0800298 /*
299 * don't do inplace I/O if all compressed pages are available in
300 * managed cache since it can be moved to the bypass queue instead.
301 */
302 if (standalone)
Gao Xiang97e86a82019-07-31 23:57:47 +0800303 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800304}
305
306/* called by erofs_shrinker to get rid of all compressed_pages */
Gao Xiang47e541a2018-07-29 13:34:58 +0800307int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
Gao Xiang97e86a82019-07-31 23:57:47 +0800308 struct erofs_workgroup *grp)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800309{
Gao Xiang97e86a82019-07-31 23:57:47 +0800310 struct z_erofs_pcluster *const pcl =
311 container_of(grp, struct z_erofs_pcluster, obj);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800312 int i;
313
314 /*
315 * refcount of workgroup is now freezed as 1,
316 * therefore no need to worry about available decompression users.
317 */
Gao Xiang9f6cc762021-04-07 12:39:20 +0800318 for (i = 0; i < pcl->pclusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800319 struct page *page = pcl->compressed_pages[i];
Gao Xiang105d4ad2018-07-26 20:22:07 +0800320
Gao Xiang97e86a82019-07-31 23:57:47 +0800321 if (!page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800322 continue;
323
324 /* block other users from reclaiming or migrating the page */
325 if (!trylock_page(page))
326 return -EBUSY;
327
Yue Huf4d4e5f2021-08-10 14:54:50 +0800328 if (!erofs_page_is_managed(sbi, page))
Gao Xiang97e86a82019-07-31 23:57:47 +0800329 continue;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800330
Gao Xiang97e86a82019-07-31 23:57:47 +0800331 /* barrier is implied in the following 'unlock_page' */
332 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800333 detach_page_private(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800334 unlock_page(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800335 }
336 return 0;
337}
338
Yue Hud252ff32021-08-10 15:24:16 +0800339int erofs_try_to_free_cached_page(struct page *page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800340{
Gao Xiang97e86a82019-07-31 23:57:47 +0800341 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800342 int ret = 0; /* 0 - busy */
343
Gao Xiang97e86a82019-07-31 23:57:47 +0800344 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
Gao Xiang105d4ad2018-07-26 20:22:07 +0800345 unsigned int i;
346
Gao Xiang9f6cc762021-04-07 12:39:20 +0800347 for (i = 0; i < pcl->pclusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800348 if (pcl->compressed_pages[i] == page) {
349 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800350 ret = 1;
351 break;
352 }
353 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800354 erofs_workgroup_unfreeze(&pcl->obj, 1);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800355
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800356 if (ret)
357 detach_page_private(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800358 }
359 return ret;
360}
Gao Xiang105d4ad2018-07-26 20:22:07 +0800361
Gao Xiang3883a792018-07-26 20:22:06 +0800362/* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
Gao Xiang81382f52021-04-07 12:39:21 +0800363static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
364 struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +0800365{
Gao Xiang97e86a82019-07-31 23:57:47 +0800366 struct z_erofs_pcluster *const pcl = clt->pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800367
Gao Xiang81382f52021-04-07 12:39:21 +0800368 while (clt->icpage_ptr > pcl->compressed_pages)
369 if (!cmpxchg(--clt->icpage_ptr, NULL, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800370 return true;
Gao Xiang3883a792018-07-26 20:22:06 +0800371 return false;
372}
373
Gao Xiang97e86a82019-07-31 23:57:47 +0800374/* callers must be with collection lock held */
375static int z_erofs_attach_page(struct z_erofs_collector *clt,
376 struct page *page,
377 enum z_erofs_page_type type)
Gao Xiang3883a792018-07-26 20:22:06 +0800378{
379 int ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800380
Gao Xiang97e86a82019-07-31 23:57:47 +0800381 /* give priority for inplaceio */
382 if (clt->mode >= COLLECT_PRIMARY &&
Julian Merida447a3622019-03-18 20:58:41 -0300383 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
Gao Xiang99634bf2019-09-04 10:09:05 +0800384 z_erofs_try_inplace_io(clt, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800385 return 0;
386
Yue Hu7dea3de72021-04-19 18:26:23 +0800387 ret = z_erofs_pagevec_enqueue(&clt->vector, page, type);
Gao Xiang97e86a82019-07-31 23:57:47 +0800388 clt->cl->vcnt += (unsigned int)ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800389
390 return ret ? 0 : -EAGAIN;
391}
392
Gao Xiang473e15b2020-12-08 17:58:34 +0800393static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800394{
Gao Xiang473e15b2020-12-08 17:58:34 +0800395 struct z_erofs_pcluster *pcl = clt->pcl;
396 z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +0800397
Gao Xiang473e15b2020-12-08 17:58:34 +0800398 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
399 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
400 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800401 *owned_head = &pcl->next;
Gao Xiang473e15b2020-12-08 17:58:34 +0800402 /* so we can attach this pcluster to our submission chain. */
403 clt->mode = COLLECT_PRIMARY_FOLLOWED;
404 return;
Gao Xianga1121522019-02-27 13:33:32 +0800405 }
Gao Xiang473e15b2020-12-08 17:58:34 +0800406
407 /*
408 * type 2, link to the end of an existing open chain, be careful
409 * that its submission is controlled by the original attached chain.
410 */
411 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
412 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
413 *owned_head = Z_EROFS_PCLUSTER_TAIL;
414 clt->mode = COLLECT_PRIMARY_HOOKED;
415 clt->tailpcl = NULL;
416 return;
417 }
418 /* type 3, it belongs to a chain, but it isn't the end of the chain */
419 clt->mode = COLLECT_PRIMARY;
Gao Xiang3883a792018-07-26 20:22:06 +0800420}
421
Gao Xiang9e579fc2019-10-08 20:56:12 +0800422static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
423 struct inode *inode,
424 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800425{
Gao Xiang64094a02020-02-20 10:46:42 +0800426 struct z_erofs_pcluster *pcl = clt->pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800427 struct z_erofs_collection *cl;
428 unsigned int length;
Gao Xiang3883a792018-07-26 20:22:06 +0800429
Gao Xiang64094a02020-02-20 10:46:42 +0800430 /* to avoid unexpected loop formed by corrupted images */
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800431 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
432 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800433 return -EFSCORRUPTED;
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800434 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800435
436 cl = z_erofs_primarycollection(pcl);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800437 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800438 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800439 return -EFSCORRUPTED;
Gao Xiang3883a792018-07-26 20:22:06 +0800440 }
441
Gao Xiang97e86a82019-07-31 23:57:47 +0800442 length = READ_ONCE(pcl->length);
443 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
444 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
445 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800446 return -EFSCORRUPTED;
Gao Xiang97e86a82019-07-31 23:57:47 +0800447 }
448 } else {
449 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
Gao Xiang3883a792018-07-26 20:22:06 +0800450
Gao Xiang97e86a82019-07-31 23:57:47 +0800451 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
452 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
Gao Xiang3883a792018-07-26 20:22:06 +0800453
Gao Xiang97e86a82019-07-31 23:57:47 +0800454 while (llen > length &&
455 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
456 cpu_relax();
457 length = READ_ONCE(pcl->length);
458 }
459 }
460 mutex_lock(&cl->lock);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800461 /* used to check tail merging loop due to corrupted images */
462 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
463 clt->tailpcl = pcl;
Gao Xiang473e15b2020-12-08 17:58:34 +0800464
465 z_erofs_try_to_claim_pcluster(clt);
Gao Xiang97e86a82019-07-31 23:57:47 +0800466 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800467 return 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800468}
469
Gao Xiang9e579fc2019-10-08 20:56:12 +0800470static int z_erofs_register_collection(struct z_erofs_collector *clt,
471 struct inode *inode,
472 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800473{
Gao Xiang97e86a82019-07-31 23:57:47 +0800474 struct z_erofs_pcluster *pcl;
475 struct z_erofs_collection *cl;
Gao Xiang64094a02020-02-20 10:46:42 +0800476 struct erofs_workgroup *grp;
Gao Xiang97e86a82019-07-31 23:57:47 +0800477 int err;
Gao Xiange5e3abb2018-09-19 13:49:07 +0800478
Gao Xiang9f6cc762021-04-07 12:39:20 +0800479 /* no available pcluster, let's allocate one */
480 pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
481 if (IS_ERR(pcl))
482 return PTR_ERR(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800483
Gao Xiang64094a02020-02-20 10:46:42 +0800484 atomic_set(&pcl->obj.refcount, 1);
Gao Xiang97e86a82019-07-31 23:57:47 +0800485 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
Gao Xiang3883a792018-07-26 20:22:06 +0800486
Gao Xiang97e86a82019-07-31 23:57:47 +0800487 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
488 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
489 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800490
Gao Xiang97e86a82019-07-31 23:57:47 +0800491 if (map->m_flags & EROFS_MAP_ZIPPED)
492 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
493 else
494 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
Gao Xiangb6a76182019-06-24 15:22:58 +0800495
Gao Xiang97e86a82019-07-31 23:57:47 +0800496 /* new pclusters should be claimed as type 1, primary and followed */
497 pcl->next = clt->owned_head;
498 clt->mode = COLLECT_PRIMARY_FOLLOWED;
499
500 cl = z_erofs_primarycollection(pcl);
501 cl->pageofs = map->m_la & ~PAGE_MASK;
Gao Xiang3883a792018-07-26 20:22:06 +0800502
Gao Xiang23edf3a2018-11-23 01:21:47 +0800503 /*
504 * lock all primary followed works before visible to others
Gao Xiang97e86a82019-07-31 23:57:47 +0800505 * and mutex_trylock *never* fails for a new pcluster.
Gao Xiang23edf3a2018-11-23 01:21:47 +0800506 */
Gao Xiang9f6cc762021-04-07 12:39:20 +0800507 mutex_init(&cl->lock);
Gao Xiang64094a02020-02-20 10:46:42 +0800508 DBG_BUGON(!mutex_trylock(&cl->lock));
Gao Xiang23edf3a2018-11-23 01:21:47 +0800509
Gao Xiang64094a02020-02-20 10:46:42 +0800510 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
511 if (IS_ERR(grp)) {
512 err = PTR_ERR(grp);
513 goto err_out;
514 }
515
516 if (grp != &pcl->obj) {
517 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
518 err = -EEXIST;
519 goto err_out;
Gao Xiang3883a792018-07-26 20:22:06 +0800520 }
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800521 /* used to check tail merging loop due to corrupted images */
522 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
523 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800524 clt->owned_head = &pcl->next;
525 clt->pcl = pcl;
526 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800527 return 0;
Gao Xiang64094a02020-02-20 10:46:42 +0800528
529err_out:
530 mutex_unlock(&cl->lock);
Gao Xiang9f6cc762021-04-07 12:39:20 +0800531 z_erofs_free_pcluster(pcl);
Gao Xiang64094a02020-02-20 10:46:42 +0800532 return err;
Gao Xiang3883a792018-07-26 20:22:06 +0800533}
534
Gao Xiang97e86a82019-07-31 23:57:47 +0800535static int z_erofs_collector_begin(struct z_erofs_collector *clt,
536 struct inode *inode,
537 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800538{
Gao Xiang64094a02020-02-20 10:46:42 +0800539 struct erofs_workgroup *grp;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800540 int ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800541
Gao Xiang97e86a82019-07-31 23:57:47 +0800542 DBG_BUGON(clt->cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800543
Gao Xiang97e86a82019-07-31 23:57:47 +0800544 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
545 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
546 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +0800547
Gao Xiang97e86a82019-07-31 23:57:47 +0800548 if (!PAGE_ALIGNED(map->m_pa)) {
549 DBG_BUGON(1);
550 return -EINVAL;
551 }
Gao Xiang3883a792018-07-26 20:22:06 +0800552
Gao Xiang64094a02020-02-20 10:46:42 +0800553 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
554 if (grp) {
555 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
556 } else {
Gao Xiang9e579fc2019-10-08 20:56:12 +0800557 ret = z_erofs_register_collection(clt, inode, map);
Gao Xiangb27661c2018-09-19 13:49:06 +0800558
Gao Xiang64094a02020-02-20 10:46:42 +0800559 if (!ret)
560 goto out;
561 if (ret != -EEXIST)
562 return ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800563 }
564
Gao Xiang64094a02020-02-20 10:46:42 +0800565 ret = z_erofs_lookup_collection(clt, inode, map);
566 if (ret) {
567 erofs_workgroup_put(&clt->pcl->obj);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800568 return ret;
Gao Xiang64094a02020-02-20 10:46:42 +0800569 }
Gao Xiang3883a792018-07-26 20:22:06 +0800570
Gao Xiang64094a02020-02-20 10:46:42 +0800571out:
Gao Xiang97e86a82019-07-31 23:57:47 +0800572 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang9e579fc2019-10-08 20:56:12 +0800573 clt->cl->pagevec, clt->cl->vcnt);
Gao Xiang3883a792018-07-26 20:22:06 +0800574
Gao Xiang81382f52021-04-07 12:39:21 +0800575 /* since file-backed online pages are traversed in reverse order */
576 clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages;
Gao Xiang3883a792018-07-26 20:22:06 +0800577 return 0;
578}
579
580/*
Gao Xiang97e86a82019-07-31 23:57:47 +0800581 * keep in mind that no referenced pclusters will be freed
582 * only after a RCU grace period.
Gao Xiang3883a792018-07-26 20:22:06 +0800583 */
584static void z_erofs_rcu_callback(struct rcu_head *head)
585{
Gao Xiang97e86a82019-07-31 23:57:47 +0800586 struct z_erofs_collection *const cl =
587 container_of(head, struct z_erofs_collection, rcu);
Gao Xiang3883a792018-07-26 20:22:06 +0800588
Gao Xiang9f6cc762021-04-07 12:39:20 +0800589 z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
590 primary_collection));
Gao Xiang3883a792018-07-26 20:22:06 +0800591}
592
593void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
594{
Gao Xiang97e86a82019-07-31 23:57:47 +0800595 struct z_erofs_pcluster *const pcl =
596 container_of(grp, struct z_erofs_pcluster, obj);
597 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800598
Gao Xiang97e86a82019-07-31 23:57:47 +0800599 call_rcu(&cl->rcu, z_erofs_rcu_callback);
Gao Xiang3883a792018-07-26 20:22:06 +0800600}
601
Gao Xiang97e86a82019-07-31 23:57:47 +0800602static void z_erofs_collection_put(struct z_erofs_collection *cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800603{
Gao Xiang97e86a82019-07-31 23:57:47 +0800604 struct z_erofs_pcluster *const pcl =
605 container_of(cl, struct z_erofs_pcluster, primary_collection);
606
607 erofs_workgroup_put(&pcl->obj);
Gao Xiang3883a792018-07-26 20:22:06 +0800608}
609
Gao Xiang97e86a82019-07-31 23:57:47 +0800610static bool z_erofs_collector_end(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800611{
Gao Xiang97e86a82019-07-31 23:57:47 +0800612 struct z_erofs_collection *cl = clt->cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800613
Gao Xiang97e86a82019-07-31 23:57:47 +0800614 if (!cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800615 return false;
616
Gao Xiang97e86a82019-07-31 23:57:47 +0800617 z_erofs_pagevec_ctor_exit(&clt->vector, false);
618 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800619
620 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800621 * if all pending pages are added, don't hold its reference
622 * any longer if the pcluster isn't hosted by ourselves.
Gao Xiang3883a792018-07-26 20:22:06 +0800623 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800624 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
625 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800626
Gao Xiang97e86a82019-07-31 23:57:47 +0800627 clt->cl = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +0800628 return true;
629}
630
Gao Xiang97e86a82019-07-31 23:57:47 +0800631static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800632 unsigned int cachestrategy,
Gao Xiang97e86a82019-07-31 23:57:47 +0800633 erofs_off_t la)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800634{
Gao Xiang4279f3f2019-07-31 23:57:49 +0800635 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
636 return false;
637
Gao Xiang92e6efd2018-12-08 00:19:16 +0800638 if (fe->backmost)
639 return true;
640
Gao Xiang4279f3f2019-07-31 23:57:49 +0800641 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
642 la < fe->headoffset;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800643}
Gao Xiang92e6efd2018-12-08 00:19:16 +0800644
Gao Xiang97e86a82019-07-31 23:57:47 +0800645static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
Gao Xiang1825c8d2020-12-09 20:37:17 +0800646 struct page *page, struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800647{
Gao Xiang97e86a82019-07-31 23:57:47 +0800648 struct inode *const inode = fe->inode;
Gao Xiangbda17a42019-10-08 20:56:13 +0800649 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Chao Yu3b423412019-01-15 09:42:21 +0800650 struct erofs_map_blocks *const map = &fe->map;
Gao Xiang97e86a82019-07-31 23:57:47 +0800651 struct z_erofs_collector *const clt = &fe->clt;
Gao Xiang3883a792018-07-26 20:22:06 +0800652 const loff_t offset = page_offset(page);
Gao Xiangdc76ea82019-09-22 18:04:34 +0800653 bool tight = true;
Gao Xiang3883a792018-07-26 20:22:06 +0800654
Gao Xiang92e6efd2018-12-08 00:19:16 +0800655 enum z_erofs_cache_alloctype cache_strategy;
Gao Xiang3883a792018-07-26 20:22:06 +0800656 enum z_erofs_page_type page_type;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200657 unsigned int cur, end, spiltted, index;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800658 int err = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800659
660 /* register locked file pages as online pages in pack */
661 z_erofs_onlinepage_init(page);
662
663 spiltted = 0;
664 end = PAGE_SIZE;
665repeat:
666 cur = end - 1;
667
668 /* lucky, within the range of the current map_blocks */
669 if (offset + cur >= map->m_la &&
Julian Merida447a3622019-03-18 20:58:41 -0300670 offset + cur < map->m_la + map->m_llen) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800671 /* didn't get a valid collection previously (very rare) */
672 if (!clt->cl)
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800673 goto restart_now;
Gao Xiang3883a792018-07-26 20:22:06 +0800674 goto hitted;
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800675 }
Gao Xiang3883a792018-07-26 20:22:06 +0800676
677 /* go ahead the next map_blocks */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800678 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
Gao Xiang3883a792018-07-26 20:22:06 +0800679
Gao Xiang97e86a82019-07-31 23:57:47 +0800680 if (z_erofs_collector_end(clt))
Gao Xiangf0c519f2018-11-23 01:21:49 +0800681 fe->backmost = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800682
683 map->m_la = offset + cur;
684 map->m_llen = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +0800685 err = z_erofs_map_blocks_iter(inode, map, 0);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800686 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800687 goto err_out;
688
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800689restart_now:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800690 if (!(map->m_flags & EROFS_MAP_MAPPED))
Gao Xiang3883a792018-07-26 20:22:06 +0800691 goto hitted;
692
Gao Xiang97e86a82019-07-31 23:57:47 +0800693 err = z_erofs_collector_begin(clt, inode, map);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800694 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800695 goto err_out;
696
Gao Xiang92e6efd2018-12-08 00:19:16 +0800697 /* preload all compressed pages (maybe downgrade role if necessary) */
Gao Xiange6242462021-10-07 15:02:23 +0800698 if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy, map->m_la))
Gao Xiang1825c8d2020-12-09 20:37:17 +0800699 cache_strategy = TRYALLOC;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800700 else
701 cache_strategy = DONTALLOC;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800702
Gao Xiang1825c8d2020-12-09 20:37:17 +0800703 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
704 cache_strategy, pagepool);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800705
Gao Xiang3883a792018-07-26 20:22:06 +0800706hitted:
Gao Xiangdc76ea82019-09-22 18:04:34 +0800707 /*
708 * Ensure the current partial page belongs to this submit chain rather
709 * than other concurrent submit chains or the noio(bypass) chain since
710 * those chains are handled asynchronously thus the page cannot be used
711 * for inplace I/O or pagevec (should be processed in strict order.)
712 */
713 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
714 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
715
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200716 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800717 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800718 zero_user_segment(page, cur, end);
719 goto next_part;
720 }
721
722 /* let's derive page type */
723 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
724 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
725 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
726 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
727
Gao Xianga1121522019-02-27 13:33:32 +0800728 if (cur)
Gao Xiang97e86a82019-07-31 23:57:47 +0800729 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xianga1121522019-02-27 13:33:32 +0800730
Gao Xiang3883a792018-07-26 20:22:06 +0800731retry:
Gao Xiang97e86a82019-07-31 23:57:47 +0800732 err = z_erofs_attach_page(clt, page, page_type);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800733 /* should allocate an additional short-lived page for pagevec */
Gao Xiang3883a792018-07-26 20:22:06 +0800734 if (err == -EAGAIN) {
735 struct page *const newpage =
Chao Yue3f78d52020-09-17 09:18:21 +0800736 alloc_page(GFP_NOFS | __GFP_NOFAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800737
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800738 set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
Gao Xiang97e86a82019-07-31 23:57:47 +0800739 err = z_erofs_attach_page(clt, newpage,
740 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800741 if (!err)
Gao Xiang3883a792018-07-26 20:22:06 +0800742 goto retry;
743 }
744
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800745 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800746 goto err_out;
747
Gao Xiang97e86a82019-07-31 23:57:47 +0800748 index = page->index - (map->m_la >> PAGE_SHIFT);
Gao Xiang3883a792018-07-26 20:22:06 +0800749
Gao Xiang3883a792018-07-26 20:22:06 +0800750 z_erofs_onlinepage_fixup(page, index, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800751
Gao Xiang1e05ff32018-09-18 22:27:25 +0800752 /* bump up the number of spiltted parts of a page */
753 ++spiltted;
754 /* also update nr_pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800755 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
Gao Xiang3883a792018-07-26 20:22:06 +0800756next_part:
757 /* can be used for verification */
758 map->m_llen = offset + cur - map->m_la;
759
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300760 end = cur;
761 if (end > 0)
Gao Xiang3883a792018-07-26 20:22:06 +0800762 goto repeat;
763
Gao Xiang1e05ff32018-09-18 22:27:25 +0800764out:
Gao Xiang3883a792018-07-26 20:22:06 +0800765 z_erofs_onlinepage_endio(page);
766
Gao Xiang4f761fa2019-09-04 10:09:09 +0800767 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
768 __func__, page, spiltted, map->m_llen);
Gao Xiang3883a792018-07-26 20:22:06 +0800769 return err;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800770
771 /* if some error occurred while processing this page */
772err_out:
773 SetPageError(page);
774 goto out;
Gao Xiang3883a792018-07-26 20:22:06 +0800775}
776
Huang Jianan648f2de2021-03-17 11:54:47 +0800777static void z_erofs_decompressqueue_work(struct work_struct *work);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800778static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
779 bool sync, int bios)
Gao Xiang3883a792018-07-26 20:22:06 +0800780{
Huang Jianan30048cd2021-03-17 11:54:48 +0800781 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
782
Gao Xianga4b1fab2019-10-08 20:56:15 +0800783 /* wake up the caller thread for sync decompression */
784 if (sync) {
Gao Xiang848bd9a2018-12-08 00:19:12 +0800785 unsigned long flags;
Gao Xiang3883a792018-07-26 20:22:06 +0800786
Gao Xiang848bd9a2018-12-08 00:19:12 +0800787 spin_lock_irqsave(&io->u.wait.lock, flags);
788 if (!atomic_add_return(bios, &io->pending_bios))
789 wake_up_locked(&io->u.wait);
790 spin_unlock_irqrestore(&io->u.wait.lock, flags);
791 return;
792 }
793
Huang Jianan648f2de2021-03-17 11:54:47 +0800794 if (atomic_add_return(bios, &io->pending_bios))
795 return;
Huang Jianan30048cd2021-03-17 11:54:48 +0800796 /* Use workqueue and sync decompression for atomic contexts only */
Huang Jianan648f2de2021-03-17 11:54:47 +0800797 if (in_atomic() || irqs_disabled()) {
Gao Xiang3883a792018-07-26 20:22:06 +0800798 queue_work(z_erofs_workqueue, &io->u.work);
Gao Xiange6242462021-10-07 15:02:23 +0800799 sbi->opt.readahead_sync_decompress = true;
Huang Jianan648f2de2021-03-17 11:54:47 +0800800 return;
801 }
802 z_erofs_decompressqueue_work(&io->u.work);
Gao Xiang3883a792018-07-26 20:22:06 +0800803}
804
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800805static bool z_erofs_page_is_invalidated(struct page *page)
806{
807 return !page->mapping && !z_erofs_is_shortlived_page(page);
808}
809
Gao Xiang0c638f72019-11-08 11:37:33 +0800810static void z_erofs_decompressqueue_endio(struct bio *bio)
Gao Xiang3883a792018-07-26 20:22:06 +0800811{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800812 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
813 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
Gao Xiang14a56ec2019-03-25 11:40:09 +0800814 blk_status_t err = bio->bi_status;
Gao Xiang3883a792018-07-26 20:22:06 +0800815 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800816 struct bvec_iter_all iter_all;
Gao Xiang3883a792018-07-26 20:22:06 +0800817
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200818 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang3883a792018-07-26 20:22:06 +0800819 struct page *page = bvec->bv_page;
820
821 DBG_BUGON(PageUptodate(page));
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800822 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800823
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800824 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800825 SetPageError(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800826
Gao Xianga4b1fab2019-10-08 20:56:15 +0800827 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
828 if (!err)
829 SetPageUptodate(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800830 unlock_page(page);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800831 }
Gao Xiang3883a792018-07-26 20:22:06 +0800832 }
Gao Xianga4b1fab2019-10-08 20:56:15 +0800833 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
Gao Xiang3883a792018-07-26 20:22:06 +0800834 bio_put(bio);
835}
836
Gao Xiang97e86a82019-07-31 23:57:47 +0800837static int z_erofs_decompress_pcluster(struct super_block *sb,
838 struct z_erofs_pcluster *pcl,
839 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800840{
841 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang3883a792018-07-26 20:22:06 +0800842 struct z_erofs_pagevec_ctor ctor;
Gao Xiang9f6cc762021-04-07 12:39:20 +0800843 unsigned int i, inputsize, outputsize, llen, nr_pages;
Gao Xiang97e86a82019-07-31 23:57:47 +0800844 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
Gao Xiang3883a792018-07-26 20:22:06 +0800845 struct page **pages, **compressed_pages, *page;
Gao Xiang3883a792018-07-26 20:22:06 +0800846
847 enum z_erofs_page_type page_type;
Gao Xiangb6a76182019-06-24 15:22:58 +0800848 bool overlapped, partial;
Gao Xiang97e86a82019-07-31 23:57:47 +0800849 struct z_erofs_collection *cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800850 int err;
851
852 might_sleep();
Gao Xiang97e86a82019-07-31 23:57:47 +0800853 cl = z_erofs_primarycollection(pcl);
854 DBG_BUGON(!READ_ONCE(cl->nr_pages));
Gao Xiang3883a792018-07-26 20:22:06 +0800855
Gao Xiang97e86a82019-07-31 23:57:47 +0800856 mutex_lock(&cl->lock);
857 nr_pages = cl->nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800858
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800859 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
Gao Xiang3883a792018-07-26 20:22:06 +0800860 pages = pages_onstack;
Gao Xiang97e86a82019-07-31 23:57:47 +0800861 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
862 mutex_trylock(&z_pagemap_global_lock)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800863 pages = z_pagemap_global;
Gao Xiang97e86a82019-07-31 23:57:47 +0800864 } else {
Chao Yu441dfcc2019-07-16 17:44:22 +0800865 gfp_t gfp_flags = GFP_KERNEL;
866
Gao Xiang97e86a82019-07-31 23:57:47 +0800867 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
Chao Yu441dfcc2019-07-16 17:44:22 +0800868 gfp_flags |= __GFP_NOFAIL;
869
Julian Merida447a3622019-03-18 20:58:41 -0300870 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Chao Yu441dfcc2019-07-16 17:44:22 +0800871 gfp_flags);
Gao Xiang3883a792018-07-26 20:22:06 +0800872
873 /* fallback to global pagemap for the lowmem scenario */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800874 if (!pages) {
Chao Yu441dfcc2019-07-16 17:44:22 +0800875 mutex_lock(&z_pagemap_global_lock);
876 pages = z_pagemap_global;
Gao Xiang3883a792018-07-26 20:22:06 +0800877 }
878 }
879
880 for (i = 0; i < nr_pages; ++i)
881 pages[i] = NULL;
882
Gao Xiange12a0ce2019-08-21 22:01:52 +0800883 err = 0;
Gao Xiangfa61a332019-06-24 15:22:53 +0800884 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang97e86a82019-07-31 23:57:47 +0800885 cl->pagevec, 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800886
Gao Xiang97e86a82019-07-31 23:57:47 +0800887 for (i = 0; i < cl->vcnt; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200888 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800889
Gao Xiang046d64e2019-07-31 23:57:45 +0800890 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800891
892 /* all pages in pagevec ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100893 DBG_BUGON(!page);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800894 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800895
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800896 if (z_erofs_put_shortlivedpage(pagepool, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800897 continue;
898
899 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
900 pagenr = 0;
901 else
902 pagenr = z_erofs_onlinepage_index(page);
903
Gao Xiang70b17992018-12-11 15:17:49 +0800904 DBG_BUGON(pagenr >= nr_pages);
Gao Xiange5e3abb2018-09-19 13:49:07 +0800905
Gao Xiange12a0ce2019-08-21 22:01:52 +0800906 /*
907 * currently EROFS doesn't support multiref(dedup),
908 * so here erroring out one multiref page.
909 */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800910 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800911 DBG_BUGON(1);
912 SetPageError(pages[pagenr]);
913 z_erofs_onlinepage_endio(pages[pagenr]);
914 err = -EFSCORRUPTED;
915 }
Gao Xiang3883a792018-07-26 20:22:06 +0800916 pages[pagenr] = page;
917 }
Gao Xiang3883a792018-07-26 20:22:06 +0800918 z_erofs_pagevec_ctor_exit(&ctor, true);
919
920 overlapped = false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800921 compressed_pages = pcl->compressed_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800922
Gao Xiang9f6cc762021-04-07 12:39:20 +0800923 for (i = 0; i < pcl->pclusterpages; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200924 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800925
926 page = compressed_pages[i];
927
928 /* all compressed pages ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100929 DBG_BUGON(!page);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800930 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800931
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800932 if (!z_erofs_is_shortlived_page(page)) {
Gao Xiangd61fbb62019-03-25 11:40:08 +0800933 if (erofs_page_is_managed(sbi, page)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800934 if (!PageUptodate(page))
Gao Xiang11152492019-03-25 11:40:07 +0800935 err = -EIO;
936 continue;
937 }
Gao Xiang3883a792018-07-26 20:22:06 +0800938
Gao Xiang11152492019-03-25 11:40:07 +0800939 /*
940 * only if non-head page can be selected
941 * for inplace decompression
942 */
943 pagenr = z_erofs_onlinepage_index(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800944
Gao Xiang11152492019-03-25 11:40:07 +0800945 DBG_BUGON(pagenr >= nr_pages);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800946 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800947 DBG_BUGON(1);
948 SetPageError(pages[pagenr]);
949 z_erofs_onlinepage_endio(pages[pagenr]);
950 err = -EFSCORRUPTED;
951 }
Gao Xiang11152492019-03-25 11:40:07 +0800952 pages[pagenr] = page;
Gao Xiang3883a792018-07-26 20:22:06 +0800953
Gao Xiang11152492019-03-25 11:40:07 +0800954 overlapped = true;
955 }
956
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800957 /* PG_error needs checking for all non-managed pages */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800958 if (PageError(page)) {
Gao Xiang11152492019-03-25 11:40:07 +0800959 DBG_BUGON(PageUptodate(page));
960 err = -EIO;
961 }
Gao Xiang3883a792018-07-26 20:22:06 +0800962 }
963
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800964 if (err)
Gao Xiang11152492019-03-25 11:40:07 +0800965 goto out;
966
Gao Xiang97e86a82019-07-31 23:57:47 +0800967 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
968 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
969 outputsize = llen;
970 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
Gao Xiangb6a76182019-06-24 15:22:58 +0800971 } else {
Gao Xiang97e86a82019-07-31 23:57:47 +0800972 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
Gao Xiangb6a76182019-06-24 15:22:58 +0800973 partial = true;
974 }
Gao Xiang3883a792018-07-26 20:22:06 +0800975
Gao Xiang9f6cc762021-04-07 12:39:20 +0800976 inputsize = pcl->pclusterpages * PAGE_SIZE;
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800977 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
978 .sb = sb,
979 .in = compressed_pages,
980 .out = pages,
Gao Xiang97e86a82019-07-31 23:57:47 +0800981 .pageofs_out = cl->pageofs,
Gao Xiang9f6cc762021-04-07 12:39:20 +0800982 .inputsize = inputsize,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800983 .outputsize = outputsize,
Gao Xiang97e86a82019-07-31 23:57:47 +0800984 .alg = pcl->algorithmformat,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800985 .inplace_io = overlapped,
Gao Xiangb6a76182019-06-24 15:22:58 +0800986 .partial_decoding = partial
Gao Xiang97e86a82019-07-31 23:57:47 +0800987 }, pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800988
989out:
Ruiqi Gongfe6adcc2021-03-31 05:39:20 -0400990 /* must handle all compressed pages before ending pages */
Gao Xiang9f6cc762021-04-07 12:39:20 +0800991 for (i = 0; i < pcl->pclusterpages; ++i) {
Gao Xiang3883a792018-07-26 20:22:06 +0800992 page = compressed_pages[i];
993
Gao Xiangd61fbb62019-03-25 11:40:08 +0800994 if (erofs_page_is_managed(sbi, page))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800995 continue;
Gao Xiangd61fbb62019-03-25 11:40:08 +0800996
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800997 /* recycle all individual short-lived pages */
998 (void)z_erofs_put_shortlivedpage(pagepool, page);
Gao Xiang3883a792018-07-26 20:22:06 +0800999
1000 WRITE_ONCE(compressed_pages[i], NULL);
1001 }
1002
Gao Xiangaf692e12019-02-27 13:33:30 +08001003 for (i = 0; i < nr_pages; ++i) {
1004 page = pages[i];
1005 if (!page)
1006 continue;
1007
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001008 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiangaf692e12019-02-27 13:33:30 +08001009
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001010 /* recycle all individual short-lived pages */
1011 if (z_erofs_put_shortlivedpage(pagepool, page))
Gao Xiangaf692e12019-02-27 13:33:30 +08001012 continue;
1013
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001014 if (err < 0)
Gao Xiangaf692e12019-02-27 13:33:30 +08001015 SetPageError(page);
1016
1017 z_erofs_onlinepage_endio(page);
1018 }
1019
Gao Xiang3883a792018-07-26 20:22:06 +08001020 if (pages == z_pagemap_global)
1021 mutex_unlock(&z_pagemap_global_lock);
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001022 else if (pages != pages_onstack)
Gao Xiang3883a792018-07-26 20:22:06 +08001023 kvfree(pages);
1024
Gao Xiang97e86a82019-07-31 23:57:47 +08001025 cl->nr_pages = 0;
1026 cl->vcnt = 0;
Gao Xiang3883a792018-07-26 20:22:06 +08001027
Gao Xiang97e86a82019-07-31 23:57:47 +08001028 /* all cl locks MUST be taken before the following line */
1029 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001030
Gao Xiang97e86a82019-07-31 23:57:47 +08001031 /* all cl locks SHOULD be released right now */
1032 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +08001033
Gao Xiang97e86a82019-07-31 23:57:47 +08001034 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +08001035 return err;
1036}
1037
Gao Xiang0c638f72019-11-08 11:37:33 +08001038static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1039 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +08001040{
Gao Xiang97e86a82019-07-31 23:57:47 +08001041 z_erofs_next_pcluster_t owned = io->head;
Gao Xiang3883a792018-07-26 20:22:06 +08001042
Gao Xiang97e86a82019-07-31 23:57:47 +08001043 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1044 struct z_erofs_pcluster *pcl;
Gao Xiang3883a792018-07-26 20:22:06 +08001045
1046 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
Gao Xiang97e86a82019-07-31 23:57:47 +08001047 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001048
1049 /* no possible that 'owned' equals NULL */
Gao Xiang97e86a82019-07-31 23:57:47 +08001050 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001051
Gao Xiang97e86a82019-07-31 23:57:47 +08001052 pcl = container_of(owned, struct z_erofs_pcluster, next);
1053 owned = READ_ONCE(pcl->next);
Gao Xiang3883a792018-07-26 20:22:06 +08001054
Gao Xianga4b1fab2019-10-08 20:56:15 +08001055 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
Gao Xiang3978c8e2018-08-06 11:27:53 +08001056 }
Gao Xiang3883a792018-07-26 20:22:06 +08001057}
1058
Gao Xiang0c638f72019-11-08 11:37:33 +08001059static void z_erofs_decompressqueue_work(struct work_struct *work)
Gao Xiang3883a792018-07-26 20:22:06 +08001060{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001061 struct z_erofs_decompressqueue *bgq =
1062 container_of(work, struct z_erofs_decompressqueue, u.work);
Gao Xiang97e86a82019-07-31 23:57:47 +08001063 LIST_HEAD(pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001064
Gao Xianga4b1fab2019-10-08 20:56:15 +08001065 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang0c638f72019-11-08 11:37:33 +08001066 z_erofs_decompress_queue(bgq, &pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001067
Gao Xiang97e86a82019-07-31 23:57:47 +08001068 put_pages_list(&pagepool);
Gao Xianga4b1fab2019-10-08 20:56:15 +08001069 kvfree(bgq);
Gao Xiang3883a792018-07-26 20:22:06 +08001070}
1071
Gao Xiang97e86a82019-07-31 23:57:47 +08001072static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1073 unsigned int nr,
1074 struct list_head *pagepool,
1075 struct address_space *mc,
1076 gfp_t gfp)
Gao Xiang9248fce2018-12-08 00:19:15 +08001077{
Gao Xiang97e86a82019-07-31 23:57:47 +08001078 const pgoff_t index = pcl->obj.index;
Gao Xiang9248fce2018-12-08 00:19:15 +08001079 bool tocache = false;
1080
1081 struct address_space *mapping;
1082 struct page *oldpage, *page;
1083
Gao Xiang92e6efd2018-12-08 00:19:16 +08001084 compressed_page_t t;
1085 int justfound;
1086
Gao Xiang9248fce2018-12-08 00:19:15 +08001087repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +08001088 page = READ_ONCE(pcl->compressed_pages[nr]);
Gao Xiang9248fce2018-12-08 00:19:15 +08001089 oldpage = page;
1090
1091 if (!page)
1092 goto out_allocpage;
1093
1094 /*
1095 * the cached page has not been allocated and
1096 * an placeholder is out there, prepare it now.
1097 */
Gao Xiangbda17a42019-10-08 20:56:13 +08001098 if (page == PAGE_UNALLOCATED) {
Gao Xiang9248fce2018-12-08 00:19:15 +08001099 tocache = true;
1100 goto out_allocpage;
1101 }
1102
Gao Xiang92e6efd2018-12-08 00:19:16 +08001103 /* process the target tagged pointer */
1104 t = tagptr_init(compressed_page_t, page);
1105 justfound = tagptr_unfold_tags(t);
1106 page = tagptr_unfold_ptr(t);
1107
Gao Xiang1825c8d2020-12-09 20:37:17 +08001108 /*
1109 * preallocated cached pages, which is used to avoid direct reclaim
1110 * otherwise, it will go inplace I/O path instead.
1111 */
1112 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1113 WRITE_ONCE(pcl->compressed_pages[nr], page);
1114 set_page_private(page, 0);
1115 tocache = true;
1116 goto out_tocache;
1117 }
Gao Xiang9248fce2018-12-08 00:19:15 +08001118 mapping = READ_ONCE(page->mapping);
1119
1120 /*
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001121 * file-backed online pages in plcuster are all locked steady,
Gao Xiang9248fce2018-12-08 00:19:15 +08001122 * therefore it is impossible for `mapping' to be NULL.
1123 */
1124 if (mapping && mapping != mc)
1125 /* ought to be unmanaged pages */
1126 goto out;
1127
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001128 /* directly return for shortlived page as well */
1129 if (z_erofs_is_shortlived_page(page))
1130 goto out;
1131
Gao Xiang9248fce2018-12-08 00:19:15 +08001132 lock_page(page);
1133
Gao Xiang92e6efd2018-12-08 00:19:16 +08001134 /* only true if page reclaim goes wrong, should never happen */
1135 DBG_BUGON(justfound && PagePrivate(page));
1136
Gao Xiang9248fce2018-12-08 00:19:15 +08001137 /* the page is still in manage cache */
1138 if (page->mapping == mc) {
Gao Xiang97e86a82019-07-31 23:57:47 +08001139 WRITE_ONCE(pcl->compressed_pages[nr], page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001140
Gao Xiang11152492019-03-25 11:40:07 +08001141 ClearPageError(page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001142 if (!PagePrivate(page)) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001143 /*
1144 * impossible to be !PagePrivate(page) for
1145 * the current restriction as well if
1146 * the page is already in compressed_pages[].
1147 */
1148 DBG_BUGON(!justfound);
1149
1150 justfound = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +08001151 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001152 SetPagePrivate(page);
1153 }
1154
1155 /* no need to submit io if it is already up-to-date */
1156 if (PageUptodate(page)) {
1157 unlock_page(page);
1158 page = NULL;
1159 }
1160 goto out;
1161 }
1162
1163 /*
1164 * the managed page has been truncated, it's unsafe to
1165 * reuse this one, let's allocate a new cache-managed page.
1166 */
1167 DBG_BUGON(page->mapping);
Gao Xiang92e6efd2018-12-08 00:19:16 +08001168 DBG_BUGON(!justfound);
Gao Xiang9248fce2018-12-08 00:19:15 +08001169
1170 tocache = true;
1171 unlock_page(page);
1172 put_page(page);
1173out_allocpage:
Gao Xiang5ddcee12019-11-21 21:59:54 +08001174 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
Gao Xiang5ddcee12019-11-21 21:59:54 +08001175 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
Gao Xiangbf225072020-12-08 17:58:33 +08001176 list_add(&page->lru, pagepool);
Gao Xiang5ddcee12019-11-21 21:59:54 +08001177 cond_resched();
1178 goto repeat;
1179 }
Gao Xiang1825c8d2020-12-09 20:37:17 +08001180out_tocache:
Gao Xiangbf225072020-12-08 17:58:33 +08001181 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1182 /* turn into temporary page if fails (1 ref) */
1183 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1184 goto out;
Gao Xianga30573b2020-10-22 22:57:21 +08001185 }
Gao Xiangbf225072020-12-08 17:58:33 +08001186 attach_page_private(page, pcl);
1187 /* drop a refcount added by allocpage (then we have 2 refs here) */
1188 put_page(page);
1189
Gao Xiang9248fce2018-12-08 00:19:15 +08001190out: /* the only exit (for tracing and debugging) */
1191 return page;
1192}
1193
Gao Xianga4b1fab2019-10-08 20:56:15 +08001194static struct z_erofs_decompressqueue *
1195jobqueue_init(struct super_block *sb,
1196 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001197{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001198 struct z_erofs_decompressqueue *q;
Gao Xiang3883a792018-07-26 20:22:06 +08001199
Gao Xianga4b1fab2019-10-08 20:56:15 +08001200 if (fg && !*fg) {
1201 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1202 if (!q) {
1203 *fg = true;
1204 goto fg_out;
1205 }
Gao Xiang0c638f72019-11-08 11:37:33 +08001206 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
Gao Xianga4b1fab2019-10-08 20:56:15 +08001207 } else {
1208fg_out:
1209 q = fgq;
1210 init_waitqueue_head(&fgq->u.wait);
1211 atomic_set(&fgq->pending_bios, 0);
Gao Xiang3883a792018-07-26 20:22:06 +08001212 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001213 q->sb = sb;
1214 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1215 return q;
Gao Xiang3883a792018-07-26 20:22:06 +08001216}
1217
Gao Xiang97e86a82019-07-31 23:57:47 +08001218/* define decompression jobqueue types */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001219enum {
Gao Xiang7146a4f2018-12-08 00:19:18 +08001220 JQ_BYPASS,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001221 JQ_SUBMIT,
1222 NR_JOBQUEUES,
1223};
1224
1225static void *jobqueueset_init(struct super_block *sb,
Gao Xianga4b1fab2019-10-08 20:56:15 +08001226 struct z_erofs_decompressqueue *q[],
1227 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001228{
Gao Xiang7146a4f2018-12-08 00:19:18 +08001229 /*
1230 * if managed cache is enabled, bypass jobqueue is needed,
Gao Xiang97e86a82019-07-31 23:57:47 +08001231 * no need to read from device for all pclusters in this queue.
Gao Xiang7146a4f2018-12-08 00:19:18 +08001232 */
Gao Xianga4b1fab2019-10-08 20:56:15 +08001233 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1234 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001235
Gao Xianga4b1fab2019-10-08 20:56:15 +08001236 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
Gao Xiang7146a4f2018-12-08 00:19:18 +08001237}
1238
Gao Xiang97e86a82019-07-31 23:57:47 +08001239static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1240 z_erofs_next_pcluster_t qtail[],
1241 z_erofs_next_pcluster_t owned_head)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001242{
Gao Xiang97e86a82019-07-31 23:57:47 +08001243 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1244 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001245
Gao Xiang97e86a82019-07-31 23:57:47 +08001246 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1247 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1248 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001249
Gao Xiang97e86a82019-07-31 23:57:47 +08001250 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001251
1252 WRITE_ONCE(*submit_qtail, owned_head);
Gao Xiang97e86a82019-07-31 23:57:47 +08001253 WRITE_ONCE(*bypass_qtail, &pcl->next);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001254
Gao Xiang97e86a82019-07-31 23:57:47 +08001255 qtail[JQ_BYPASS] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001256}
1257
Gao Xiang1e4a2952020-01-21 14:48:19 +08001258static void z_erofs_submit_queue(struct super_block *sb,
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001259 struct z_erofs_decompress_frontend *f,
Gao Xiang0c638f72019-11-08 11:37:33 +08001260 struct list_head *pagepool,
1261 struct z_erofs_decompressqueue *fgq,
1262 bool *force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001263{
Gao Xiangbda17a42019-10-08 20:56:13 +08001264 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +08001265 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
Gao Xianga4b1fab2019-10-08 20:56:15 +08001266 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001267 void *bi_private;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001268 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001269 /* since bio will be NULL, no need to initialize last_index */
Kees Cook3f649ab2020-06-03 13:09:38 -07001270 pgoff_t last_index;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001271 unsigned int nr_bios = 0;
1272 struct bio *bio = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001273
Gao Xianga4b1fab2019-10-08 20:56:15 +08001274 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1275 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1276 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
Gao Xiang3883a792018-07-26 20:22:06 +08001277
1278 /* by default, all need io submission */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001279 q[JQ_SUBMIT]->head = owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001280
1281 do {
Gao Xiang97e86a82019-07-31 23:57:47 +08001282 struct z_erofs_pcluster *pcl;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001283 pgoff_t cur, end;
1284 unsigned int i = 0;
1285 bool bypass = true;
Gao Xiang3883a792018-07-26 20:22:06 +08001286
1287 /* no possible 'owned_head' equals the following */
Gao Xiang97e86a82019-07-31 23:57:47 +08001288 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1289 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001290
Gao Xiang97e86a82019-07-31 23:57:47 +08001291 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1292
Gao Xiang1e4a2952020-01-21 14:48:19 +08001293 cur = pcl->obj.index;
Gao Xiang9f6cc762021-04-07 12:39:20 +08001294 end = cur + pcl->pclusterpages;
Gao Xiang3883a792018-07-26 20:22:06 +08001295
1296 /* close the main owned chain at first */
Gao Xiang97e86a82019-07-31 23:57:47 +08001297 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1298 Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +08001299
Gao Xiang1e4a2952020-01-21 14:48:19 +08001300 do {
1301 struct page *page;
Gao Xiang9248fce2018-12-08 00:19:15 +08001302
Gao Xiang1e4a2952020-01-21 14:48:19 +08001303 page = pickup_page_for_submission(pcl, i++, pagepool,
1304 MNGD_MAPPING(sbi),
1305 GFP_NOFS);
1306 if (!page)
1307 continue;
Gao Xiang3883a792018-07-26 20:22:06 +08001308
Gao Xiang1e4a2952020-01-21 14:48:19 +08001309 if (bio && cur != last_index + 1) {
Gao Xiang3883a792018-07-26 20:22:06 +08001310submit_bio_retry:
Gao Xiang1e4a2952020-01-21 14:48:19 +08001311 submit_bio(bio);
1312 bio = NULL;
1313 }
Gao Xiang3883a792018-07-26 20:22:06 +08001314
Gao Xiang1e4a2952020-01-21 14:48:19 +08001315 if (!bio) {
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001316 bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS);
Gao Xianga5c0b782019-09-04 10:09:02 +08001317
Gao Xiang1e4a2952020-01-21 14:48:19 +08001318 bio->bi_end_io = z_erofs_decompressqueue_endio;
1319 bio_set_dev(bio, sb->s_bdev);
1320 bio->bi_iter.bi_sector = (sector_t)cur <<
1321 LOG_SECTORS_PER_BLOCK;
1322 bio->bi_private = bi_private;
1323 bio->bi_opf = REQ_OP_READ;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001324 if (f->readahead)
1325 bio->bi_opf |= REQ_RAHEAD;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001326 ++nr_bios;
1327 }
Gao Xiang94e4e152019-09-04 10:09:04 +08001328
Gao Xiang6c3e4852020-09-19 15:27:28 +08001329 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
Gao Xiang1e4a2952020-01-21 14:48:19 +08001330 goto submit_bio_retry;
Gao Xiang3883a792018-07-26 20:22:06 +08001331
Gao Xiang1e4a2952020-01-21 14:48:19 +08001332 last_index = cur;
1333 bypass = false;
1334 } while (++cur < end);
Gao Xiang3883a792018-07-26 20:22:06 +08001335
Gao Xiang1e4a2952020-01-21 14:48:19 +08001336 if (!bypass)
Gao Xiang97e86a82019-07-31 23:57:47 +08001337 qtail[JQ_SUBMIT] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001338 else
Gao Xiang97e86a82019-07-31 23:57:47 +08001339 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1340 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001341
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001342 if (bio)
Gao Xiang94e4e152019-09-04 10:09:04 +08001343 submit_bio(bio);
Gao Xiang3883a792018-07-26 20:22:06 +08001344
Gao Xiang587a67b2020-01-21 14:47:47 +08001345 /*
1346 * although background is preferred, no one is pending for submission.
1347 * don't issue workqueue for decompression but drop it directly instead.
1348 */
1349 if (!*force_fg && !nr_bios) {
1350 kvfree(q[JQ_SUBMIT]);
Gao Xiang1e4a2952020-01-21 14:48:19 +08001351 return;
Gao Xiang587a67b2020-01-21 14:47:47 +08001352 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001353 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
Gao Xiang3883a792018-07-26 20:22:06 +08001354}
1355
Gao Xiang0c638f72019-11-08 11:37:33 +08001356static void z_erofs_runqueue(struct super_block *sb,
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001357 struct z_erofs_decompress_frontend *f,
Gao Xiang0c638f72019-11-08 11:37:33 +08001358 struct list_head *pagepool, bool force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001359{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001360 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001361
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001362 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +08001363 return;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001364 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
Gao Xiang3883a792018-07-26 20:22:06 +08001365
Gao Xiang0c638f72019-11-08 11:37:33 +08001366 /* handle bypass queue (no i/o pclusters) immediately */
1367 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
Gao Xiang4279f3f2019-07-31 23:57:49 +08001368
Gao Xiang3883a792018-07-26 20:22:06 +08001369 if (!force_fg)
1370 return;
1371
1372 /* wait until all bios are completed */
Gao Xianga93f8c32019-10-08 20:56:16 +08001373 io_wait_event(io[JQ_SUBMIT].u.wait,
1374 !atomic_read(&io[JQ_SUBMIT].pending_bios));
Gao Xiang3883a792018-07-26 20:22:06 +08001375
Gao Xiang0c638f72019-11-08 11:37:33 +08001376 /* handle synchronous decompress queue in the caller context */
1377 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001378}
1379
Gao Xiang0c638f72019-11-08 11:37:33 +08001380static int z_erofs_readpage(struct file *file, struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +08001381{
1382 struct inode *const inode = page->mapping->host;
Gao Xiang97e86a82019-07-31 23:57:47 +08001383 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001384 int err;
1385 LIST_HEAD(pagepool);
1386
Gao Xiangba9ce772018-11-23 01:15:58 +08001387 trace_erofs_readpage(page, false);
1388
Gao Xiangf0c519f2018-11-23 01:21:49 +08001389 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1390
Gao Xiang1825c8d2020-12-09 20:37:17 +08001391 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xiang97e86a82019-07-31 23:57:47 +08001392 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001393
Gao Xiangee451972019-08-19 18:34:21 +08001394 /* if some compressed cluster ready, need submit them anyway */
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001395 z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
Gao Xiangee451972019-08-19 18:34:21 +08001396
1397 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001398 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
Gao Xiangee451972019-08-19 18:34:21 +08001399
Chao Yu3b423412019-01-15 09:42:21 +08001400 if (f.map.mpage)
1401 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001402
1403 /* clean up the remaining free pages */
1404 put_pages_list(&pagepool);
Gao Xiangee451972019-08-19 18:34:21 +08001405 return err;
Gao Xiang3883a792018-07-26 20:22:06 +08001406}
1407
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001408static void z_erofs_readahead(struct readahead_control *rac)
Gao Xiang3883a792018-07-26 20:22:06 +08001409{
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001410 struct inode *const inode = rac->mapping->host;
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001411 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001412
Gao Xiangbf9a1232020-09-19 15:27:29 +08001413 unsigned int nr_pages = readahead_count(rac);
Gao Xiange6242462021-10-07 15:02:23 +08001414 bool sync = (sbi->opt.readahead_sync_decompress &&
1415 nr_pages <= sbi->opt.max_sync_decompress_pages);
Gao Xiang97e86a82019-07-31 23:57:47 +08001416 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001417 struct page *page, *head = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001418 LIST_HEAD(pagepool);
1419
Gao Xiangbf9a1232020-09-19 15:27:29 +08001420 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
Chen Gong284db122018-09-18 22:27:27 +08001421
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001422 f.readahead = true;
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001423 f.headoffset = readahead_pos(rac);
Gao Xiangf0c519f2018-11-23 01:21:49 +08001424
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001425 while ((page = readahead_page(rac))) {
Gao Xiang3883a792018-07-26 20:22:06 +08001426 prefetchw(&page->flags);
Gao Xiang3883a792018-07-26 20:22:06 +08001427
Gao Xiang2d9b5dc2018-11-23 01:21:48 +08001428 /*
1429 * A pure asynchronous readahead is indicated if
1430 * a PG_readahead marked page is hitted at first.
1431 * Let's also do asynchronous decompression for this case.
1432 */
1433 sync &= !(PageReadahead(page) && !head);
1434
Gao Xiang3883a792018-07-26 20:22:06 +08001435 set_page_private(page, (unsigned long)head);
1436 head = page;
1437 }
1438
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001439 while (head) {
Gao Xiang3883a792018-07-26 20:22:06 +08001440 struct page *page = head;
1441 int err;
1442
1443 /* traversal in reverse order */
1444 head = (void *)page_private(page);
1445
Gao Xiang1825c8d2020-12-09 20:37:17 +08001446 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xianga5876e22019-09-04 10:08:56 +08001447 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001448 erofs_err(inode->i_sb,
1449 "readahead error at page %lu @ nid %llu",
1450 page->index, EROFS_I(inode)->nid);
Gao Xiang3883a792018-07-26 20:22:06 +08001451 put_page(page);
1452 }
1453
Gao Xiang97e86a82019-07-31 23:57:47 +08001454 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001455
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001456 z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
Gao Xiang3883a792018-07-26 20:22:06 +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
1461 /* clean up the remaining free pages */
1462 put_pages_list(&pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001463}
1464
Gao Xiang0c638f72019-11-08 11:37:33 +08001465const struct address_space_operations z_erofs_aops = {
1466 .readpage = z_erofs_readpage,
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001467 .readahead = z_erofs_readahead,
Gao Xiang3883a792018-07-26 20:22:06 +08001468};