blob: 498b7666efe852b94c93cc3379ee2b4216d10a94 [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{
Yue Hucecf8642021-12-29 07:29:19 +080085 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
Gao Xiang9f6cc762021-04-07 12:39:20 +080086 int i;
87
88 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
89 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
90
Yue Hucecf8642021-12-29 07:29:19 +080091 if (pclusterpages > pcs->maxpages)
Gao Xiang9f6cc762021-04-07 12:39:20 +080092 continue;
93
94 kmem_cache_free(pcs->slab, pcl);
95 return;
96 }
97 DBG_BUGON(1);
98}
99
Gao Xiang97e86a82019-07-31 23:57:47 +0800100/* how to allocate cached pages for a pcluster */
Gao Xiang92e6efd2018-12-08 00:19:16 +0800101enum z_erofs_cache_alloctype {
102 DONTALLOC, /* don't allocate any cached pages */
Gao Xiang1825c8d2020-12-09 20:37:17 +0800103 /*
104 * try to use cached I/O if page allocation succeeds or fallback
105 * to in-place I/O instead to avoid any direct reclaim.
106 */
107 TRYALLOC,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800108};
109
110/*
111 * tagged pointer with 1-bit tag for all compressed pages
112 * tag 0 - the page is just found with an extra page reference
113 */
114typedef tagptr1_t compressed_page_t;
115
116#define tag_compressed_page_justfound(page) \
117 tagptr_fold(compressed_page_t, page, 1)
118
Gao Xiang3883a792018-07-26 20:22:06 +0800119static struct workqueue_struct *z_erofs_workqueue __read_mostly;
Gao Xiang3883a792018-07-26 20:22:06 +0800120
121void z_erofs_exit_zip_subsystem(void)
122{
Gao Xiang3883a792018-07-26 20:22:06 +0800123 destroy_workqueue(z_erofs_workqueue);
Gao Xiang9f6cc762021-04-07 12:39:20 +0800124 z_erofs_destroy_pcluster_pool();
Gao Xiang3883a792018-07-26 20:22:06 +0800125}
126
Gao Xiang99634bf2019-09-04 10:09:05 +0800127static inline int z_erofs_init_workqueue(void)
Gao Xiang3883a792018-07-26 20:22:06 +0800128{
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200129 const unsigned int onlinecpus = num_possible_cpus();
Gao Xiang3883a792018-07-26 20:22:06 +0800130
131 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800132 * no need to spawn too many threads, limiting threads could minimum
133 * scheduling overhead, perhaps per-CPU threads should be better?
Gao Xiang3883a792018-07-26 20:22:06 +0800134 */
Gao Xiang0e62ea32020-07-31 10:40:49 +0800135 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
136 WQ_UNBOUND | WQ_HIGHPRI,
Gao Xiang97e86a82019-07-31 23:57:47 +0800137 onlinecpus + onlinecpus / 4);
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100138 return z_erofs_workqueue ? 0 : -ENOMEM;
Gao Xiang3883a792018-07-26 20:22:06 +0800139}
140
Gao Xiang0a0b7e62018-10-09 21:43:53 +0800141int __init z_erofs_init_zip_subsystem(void)
Gao Xiang3883a792018-07-26 20:22:06 +0800142{
Gao Xiang9f6cc762021-04-07 12:39:20 +0800143 int err = z_erofs_create_pcluster_pool();
Gao Xiang3883a792018-07-26 20:22:06 +0800144
Gao Xiang9f6cc762021-04-07 12:39:20 +0800145 if (err)
146 return err;
147 err = z_erofs_init_workqueue();
148 if (err)
149 z_erofs_destroy_pcluster_pool();
150 return err;
Gao Xiang3883a792018-07-26 20:22:06 +0800151}
152
Gao Xiang97e86a82019-07-31 23:57:47 +0800153enum z_erofs_collectmode {
154 COLLECT_SECONDARY,
155 COLLECT_PRIMARY,
Gao Xiang3883a792018-07-26 20:22:06 +0800156 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800157 * The current collection was the tail of an exist chain, in addition
158 * that the previous processed chained collections are all decided to
159 * be hooked up to it.
160 * A new chain will be created for the remaining collections which are
161 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
162 * the next collection cannot reuse the whole page safely in
163 * the following scenario:
Gao Xianga1121522019-02-27 13:33:32 +0800164 * ________________________________________________________________
165 * | tail (partial) page | head (partial) page |
Gao Xiang97e86a82019-07-31 23:57:47 +0800166 * | (belongs to the next cl) | (belongs to the current cl) |
Gao Xianga1121522019-02-27 13:33:32 +0800167 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
168 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800169 COLLECT_PRIMARY_HOOKED,
Gao Xiang0b964602021-03-22 02:32:27 +0800170 /*
171 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
172 * could be dispatched into bypass queue later due to uptodated managed
173 * pages. All related online pages cannot be reused for inplace I/O (or
174 * pagevec) since it can be directly decoded without I/O submission.
175 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800176 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
Gao Xianga1121522019-02-27 13:33:32 +0800177 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800178 * The current collection has been linked with the owned chain, and
179 * could also be linked with the remaining collections, which means
180 * if the processing page is the tail page of the collection, thus
181 * the current collection can safely use the whole page (since
182 * the previous collection is under control) for in-place I/O, as
183 * illustrated below:
Gao Xianga1121522019-02-27 13:33:32 +0800184 * ________________________________________________________________
Gao Xiang97e86a82019-07-31 23:57:47 +0800185 * | tail (partial) page | head (partial) page |
186 * | (of the current cl) | (of the previous collection) |
187 * | PRIMARY_FOLLOWED or | |
188 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
Gao Xianga1121522019-02-27 13:33:32 +0800189 *
Gao Xiang97e86a82019-07-31 23:57:47 +0800190 * [ (*) the above page can be used as inplace I/O. ]
Gao Xiang3883a792018-07-26 20:22:06 +0800191 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800192 COLLECT_PRIMARY_FOLLOWED,
Gao Xiang3883a792018-07-26 20:22:06 +0800193};
194
Gao Xiang97e86a82019-07-31 23:57:47 +0800195struct z_erofs_collector {
Gao Xiang3883a792018-07-26 20:22:06 +0800196 struct z_erofs_pagevec_ctor vector;
197
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800198 struct z_erofs_pcluster *pcl, *tailpcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800199 struct z_erofs_collection *cl;
Gao Xiang81382f52021-04-07 12:39:21 +0800200 /* a pointer used to pick up inplace I/O pages */
201 struct page **icpage_ptr;
Gao Xiang97e86a82019-07-31 23:57:47 +0800202 z_erofs_next_pcluster_t owned_head;
203
204 enum z_erofs_collectmode mode;
Gao Xiang3883a792018-07-26 20:22:06 +0800205};
206
Gao Xiang97e86a82019-07-31 23:57:47 +0800207struct z_erofs_decompress_frontend {
208 struct inode *const inode;
209
210 struct z_erofs_collector clt;
211 struct erofs_map_blocks map;
212
Gao Xiang6ea5aad2020-09-19 15:27:30 +0800213 bool readahead;
Gao Xiang97e86a82019-07-31 23:57:47 +0800214 /* used for applying cache strategy on the fly */
215 bool backmost;
216 erofs_off_t headoffset;
217};
218
219#define COLLECTOR_INIT() { \
220 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
221 .mode = COLLECT_PRIMARY_FOLLOWED }
222
223#define DECOMPRESS_FRONTEND_INIT(__i) { \
224 .inode = __i, .clt = COLLECTOR_INIT(), \
225 .backmost = true, }
226
227static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
228static DEFINE_MUTEX(z_pagemap_global_lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800229
Gao Xiang97e86a82019-07-31 23:57:47 +0800230static void preload_compressed_pages(struct z_erofs_collector *clt,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800231 struct address_space *mc,
Gao Xiang1825c8d2020-12-09 20:37:17 +0800232 enum z_erofs_cache_alloctype type,
Gao Xiangeaa91722021-10-22 17:01:20 +0800233 struct page **pagepool)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800234{
Gao Xiang81382f52021-04-07 12:39:21 +0800235 struct z_erofs_pcluster *pcl = clt->pcl;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800236 bool standalone = true;
Gao Xiang1825c8d2020-12-09 20:37:17 +0800237 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
238 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
Gao Xiang81382f52021-04-07 12:39:21 +0800239 struct page **pages;
240 pgoff_t index;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800241
Gao Xiang97e86a82019-07-31 23:57:47 +0800242 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800243 return;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800244
Gao Xiang81382f52021-04-07 12:39:21 +0800245 pages = pcl->compressed_pages;
246 index = pcl->obj.index;
247 for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
Gao Xiang92e6efd2018-12-08 00:19:16 +0800248 struct page *page;
249 compressed_page_t t;
Gao Xiang1825c8d2020-12-09 20:37:17 +0800250 struct page *newpage = NULL;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800251
252 /* the compressed page was loaded before */
Gao Xiang97e86a82019-07-31 23:57:47 +0800253 if (READ_ONCE(*pages))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800254 continue;
255
Gao Xiang97e86a82019-07-31 23:57:47 +0800256 page = find_get_page(mc, index);
Gao Xiang92e6efd2018-12-08 00:19:16 +0800257
258 if (page) {
259 t = tag_compressed_page_justfound(page);
Gao Xiang0b964602021-03-22 02:32:27 +0800260 } else {
261 /* I/O is needed, no possible to decompress directly */
Gao Xiang92e6efd2018-12-08 00:19:16 +0800262 standalone = false;
Gao Xiang0b964602021-03-22 02:32:27 +0800263 switch (type) {
Gao Xiang0b964602021-03-22 02:32:27 +0800264 case TRYALLOC:
265 newpage = erofs_allocpage(pagepool, gfp);
266 if (!newpage)
267 continue;
268 set_page_private(newpage,
269 Z_EROFS_PREALLOCATED_PAGE);
270 t = tag_compressed_page_justfound(newpage);
271 break;
272 default: /* DONTALLOC */
273 continue;
274 }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800275 }
276
Gao Xiang97e86a82019-07-31 23:57:47 +0800277 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800278 continue;
279
Gao Xiangeaa91722021-10-22 17:01:20 +0800280 if (page)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800281 put_page(page);
Gao Xiangeaa91722021-10-22 17:01:20 +0800282 else if (newpage)
283 erofs_pagepool_add(pagepool, newpage);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800284 }
Gao Xiang92e6efd2018-12-08 00:19:16 +0800285
Gao Xiang0b964602021-03-22 02:32:27 +0800286 /*
287 * don't do inplace I/O if all compressed pages are available in
288 * managed cache since it can be moved to the bypass queue instead.
289 */
290 if (standalone)
Gao Xiang97e86a82019-07-31 23:57:47 +0800291 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800292}
293
294/* called by erofs_shrinker to get rid of all compressed_pages */
Gao Xiang47e541a2018-07-29 13:34:58 +0800295int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
Gao Xiang97e86a82019-07-31 23:57:47 +0800296 struct erofs_workgroup *grp)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800297{
Gao Xiang97e86a82019-07-31 23:57:47 +0800298 struct z_erofs_pcluster *const pcl =
299 container_of(grp, struct z_erofs_pcluster, obj);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800300 int i;
301
Yue Hucecf8642021-12-29 07:29:19 +0800302 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
Gao Xiang105d4ad2018-07-26 20:22:07 +0800303 /*
304 * refcount of workgroup is now freezed as 1,
305 * therefore no need to worry about available decompression users.
306 */
Gao Xiang9f6cc762021-04-07 12:39:20 +0800307 for (i = 0; i < pcl->pclusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800308 struct page *page = pcl->compressed_pages[i];
Gao Xiang105d4ad2018-07-26 20:22:07 +0800309
Gao Xiang97e86a82019-07-31 23:57:47 +0800310 if (!page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800311 continue;
312
313 /* block other users from reclaiming or migrating the page */
314 if (!trylock_page(page))
315 return -EBUSY;
316
Yue Huf4d4e5f2021-08-10 14:54:50 +0800317 if (!erofs_page_is_managed(sbi, page))
Gao Xiang97e86a82019-07-31 23:57:47 +0800318 continue;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800319
Gao Xiang97e86a82019-07-31 23:57:47 +0800320 /* barrier is implied in the following 'unlock_page' */
321 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800322 detach_page_private(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800323 unlock_page(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800324 }
325 return 0;
326}
327
Yue Hud252ff32021-08-10 15:24:16 +0800328int erofs_try_to_free_cached_page(struct page *page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800329{
Gao Xiang97e86a82019-07-31 23:57:47 +0800330 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800331 int ret = 0; /* 0 - busy */
332
Gao Xiang97e86a82019-07-31 23:57:47 +0800333 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
Gao Xiang105d4ad2018-07-26 20:22:07 +0800334 unsigned int i;
335
Yue Hucecf8642021-12-29 07:29:19 +0800336 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
Gao Xiang9f6cc762021-04-07 12:39:20 +0800337 for (i = 0; i < pcl->pclusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800338 if (pcl->compressed_pages[i] == page) {
339 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800340 ret = 1;
341 break;
342 }
343 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800344 erofs_workgroup_unfreeze(&pcl->obj, 1);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800345
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800346 if (ret)
347 detach_page_private(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800348 }
349 return ret;
350}
Gao Xiang105d4ad2018-07-26 20:22:07 +0800351
Gao Xiang3883a792018-07-26 20:22:06 +0800352/* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
Gao Xiang81382f52021-04-07 12:39:21 +0800353static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
354 struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +0800355{
Gao Xiang97e86a82019-07-31 23:57:47 +0800356 struct z_erofs_pcluster *const pcl = clt->pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800357
Gao Xiang81382f52021-04-07 12:39:21 +0800358 while (clt->icpage_ptr > pcl->compressed_pages)
359 if (!cmpxchg(--clt->icpage_ptr, NULL, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800360 return true;
Gao Xiang3883a792018-07-26 20:22:06 +0800361 return false;
362}
363
Gao Xiang97e86a82019-07-31 23:57:47 +0800364/* callers must be with collection lock held */
365static int z_erofs_attach_page(struct z_erofs_collector *clt,
Gao Xiang86432a62021-11-04 02:20:06 +0800366 struct page *page, enum z_erofs_page_type type,
367 bool pvec_safereuse)
Gao Xiang3883a792018-07-26 20:22:06 +0800368{
369 int ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800370
Gao Xiang97e86a82019-07-31 23:57:47 +0800371 /* give priority for inplaceio */
372 if (clt->mode >= COLLECT_PRIMARY &&
Julian Merida447a3622019-03-18 20:58:41 -0300373 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
Gao Xiang99634bf2019-09-04 10:09:05 +0800374 z_erofs_try_inplace_io(clt, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800375 return 0;
376
Gao Xiang86432a62021-11-04 02:20:06 +0800377 ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
378 pvec_safereuse);
Gao Xiang97e86a82019-07-31 23:57:47 +0800379 clt->cl->vcnt += (unsigned int)ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800380 return ret ? 0 : -EAGAIN;
381}
382
Gao Xiang473e15b2020-12-08 17:58:34 +0800383static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800384{
Gao Xiang473e15b2020-12-08 17:58:34 +0800385 struct z_erofs_pcluster *pcl = clt->pcl;
386 z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +0800387
Gao Xiang473e15b2020-12-08 17:58:34 +0800388 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
389 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
390 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800391 *owned_head = &pcl->next;
Gao Xiang473e15b2020-12-08 17:58:34 +0800392 /* so we can attach this pcluster to our submission chain. */
393 clt->mode = COLLECT_PRIMARY_FOLLOWED;
394 return;
Gao Xianga1121522019-02-27 13:33:32 +0800395 }
Gao Xiang473e15b2020-12-08 17:58:34 +0800396
397 /*
398 * type 2, link to the end of an existing open chain, be careful
399 * that its submission is controlled by the original attached chain.
400 */
401 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
402 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
403 *owned_head = Z_EROFS_PCLUSTER_TAIL;
404 clt->mode = COLLECT_PRIMARY_HOOKED;
405 clt->tailpcl = NULL;
406 return;
407 }
408 /* type 3, it belongs to a chain, but it isn't the end of the chain */
409 clt->mode = COLLECT_PRIMARY;
Gao Xiang3883a792018-07-26 20:22:06 +0800410}
411
Gao Xiang9e579fc2019-10-08 20:56:12 +0800412static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
413 struct inode *inode,
414 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800415{
Gao Xiang64094a02020-02-20 10:46:42 +0800416 struct z_erofs_pcluster *pcl = clt->pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800417 struct z_erofs_collection *cl;
418 unsigned int length;
Gao Xiang3883a792018-07-26 20:22:06 +0800419
Gao Xiang64094a02020-02-20 10:46:42 +0800420 /* to avoid unexpected loop formed by corrupted images */
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800421 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
422 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800423 return -EFSCORRUPTED;
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800424 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800425
426 cl = z_erofs_primarycollection(pcl);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800427 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800428 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800429 return -EFSCORRUPTED;
Gao Xiang3883a792018-07-26 20:22:06 +0800430 }
431
Gao Xiang97e86a82019-07-31 23:57:47 +0800432 length = READ_ONCE(pcl->length);
433 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
434 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
435 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800436 return -EFSCORRUPTED;
Gao Xiang97e86a82019-07-31 23:57:47 +0800437 }
438 } else {
439 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
Gao Xiang3883a792018-07-26 20:22:06 +0800440
Gao Xiang97e86a82019-07-31 23:57:47 +0800441 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
442 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
Gao Xiang3883a792018-07-26 20:22:06 +0800443
Gao Xiang97e86a82019-07-31 23:57:47 +0800444 while (llen > length &&
445 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
446 cpu_relax();
447 length = READ_ONCE(pcl->length);
448 }
449 }
450 mutex_lock(&cl->lock);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800451 /* used to check tail merging loop due to corrupted images */
452 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
453 clt->tailpcl = pcl;
Gao Xiang473e15b2020-12-08 17:58:34 +0800454
455 z_erofs_try_to_claim_pcluster(clt);
Gao Xiang97e86a82019-07-31 23:57:47 +0800456 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800457 return 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800458}
459
Gao Xiang9e579fc2019-10-08 20:56:12 +0800460static int z_erofs_register_collection(struct z_erofs_collector *clt,
461 struct inode *inode,
462 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800463{
Yue Hucecf8642021-12-29 07:29:19 +0800464 bool ztailpacking = map->m_flags & EROFS_MAP_META;
Gao Xiang97e86a82019-07-31 23:57:47 +0800465 struct z_erofs_pcluster *pcl;
466 struct z_erofs_collection *cl;
Gao Xiang64094a02020-02-20 10:46:42 +0800467 struct erofs_workgroup *grp;
Gao Xiang97e86a82019-07-31 23:57:47 +0800468 int err;
Gao Xiange5e3abb2018-09-19 13:49:07 +0800469
Gao Xiang8f899262021-10-09 04:08:37 +0800470 if (!(map->m_flags & EROFS_MAP_ENCODED)) {
471 DBG_BUGON(1);
472 return -EFSCORRUPTED;
473 }
474
Gao Xiang9f6cc762021-04-07 12:39:20 +0800475 /* no available pcluster, let's allocate one */
Yue Hucecf8642021-12-29 07:29:19 +0800476 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
477 map->m_plen >> PAGE_SHIFT);
Gao Xiang9f6cc762021-04-07 12:39:20 +0800478 if (IS_ERR(pcl))
479 return PTR_ERR(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800480
Gao Xiang64094a02020-02-20 10:46:42 +0800481 atomic_set(&pcl->obj.refcount, 1);
Gao Xiang8f899262021-10-09 04:08:37 +0800482 pcl->algorithmformat = map->m_algorithmformat;
Gao Xiang97e86a82019-07-31 23:57:47 +0800483 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
484 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
485 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800486
Gao Xiang97e86a82019-07-31 23:57:47 +0800487 /* new pclusters should be claimed as type 1, primary and followed */
488 pcl->next = clt->owned_head;
489 clt->mode = COLLECT_PRIMARY_FOLLOWED;
490
491 cl = z_erofs_primarycollection(pcl);
492 cl->pageofs = map->m_la & ~PAGE_MASK;
Gao Xiang3883a792018-07-26 20:22:06 +0800493
Gao Xiang23edf3a2018-11-23 01:21:47 +0800494 /*
495 * lock all primary followed works before visible to others
Gao Xiang97e86a82019-07-31 23:57:47 +0800496 * and mutex_trylock *never* fails for a new pcluster.
Gao Xiang23edf3a2018-11-23 01:21:47 +0800497 */
Gao Xiang9f6cc762021-04-07 12:39:20 +0800498 mutex_init(&cl->lock);
Gao Xiang64094a02020-02-20 10:46:42 +0800499 DBG_BUGON(!mutex_trylock(&cl->lock));
Gao Xiang23edf3a2018-11-23 01:21:47 +0800500
Yue Hucecf8642021-12-29 07:29:19 +0800501 if (ztailpacking) {
502 pcl->obj.index = 0; /* which indicates ztailpacking */
503 pcl->pageofs_in = erofs_blkoff(map->m_pa);
504 pcl->tailpacking_size = map->m_plen;
505 } else {
506 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
Gao Xiang64094a02020-02-20 10:46:42 +0800507
Yue Hucecf8642021-12-29 07:29:19 +0800508 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
509 if (IS_ERR(grp)) {
510 err = PTR_ERR(grp);
511 goto err_out;
512 }
513
514 if (grp != &pcl->obj) {
515 clt->pcl = container_of(grp,
516 struct z_erofs_pcluster, obj);
517 err = -EEXIST;
518 goto err_out;
519 }
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
Yue Hucecf8642021-12-29 07:29:19 +0800548 if (map->m_flags & EROFS_MAP_META) {
549 if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
550 DBG_BUGON(1);
551 return -EFSCORRUPTED;
552 }
553 goto tailpacking;
Gao Xiang97e86a82019-07-31 23:57:47 +0800554 }
Gao Xiang3883a792018-07-26 20:22:06 +0800555
Gao Xiang64094a02020-02-20 10:46:42 +0800556 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
557 if (grp) {
558 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
559 } else {
Yue Hucecf8642021-12-29 07:29:19 +0800560tailpacking:
Gao Xiang9e579fc2019-10-08 20:56:12 +0800561 ret = z_erofs_register_collection(clt, inode, map);
Gao Xiang64094a02020-02-20 10:46:42 +0800562 if (!ret)
563 goto out;
564 if (ret != -EEXIST)
565 return ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800566 }
567
Gao Xiang64094a02020-02-20 10:46:42 +0800568 ret = z_erofs_lookup_collection(clt, inode, map);
569 if (ret) {
570 erofs_workgroup_put(&clt->pcl->obj);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800571 return ret;
Gao Xiang64094a02020-02-20 10:46:42 +0800572 }
Gao Xiang3883a792018-07-26 20:22:06 +0800573
Gao Xiang64094a02020-02-20 10:46:42 +0800574out:
Gao Xiang97e86a82019-07-31 23:57:47 +0800575 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang9e579fc2019-10-08 20:56:12 +0800576 clt->cl->pagevec, clt->cl->vcnt);
Gao Xiang81382f52021-04-07 12:39:21 +0800577 /* since file-backed online pages are traversed in reverse order */
Yue Hucecf8642021-12-29 07:29:19 +0800578 clt->icpage_ptr = clt->pcl->compressed_pages +
579 z_erofs_pclusterpages(clt->pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800580 return 0;
581}
582
583/*
Gao Xiang97e86a82019-07-31 23:57:47 +0800584 * keep in mind that no referenced pclusters will be freed
585 * only after a RCU grace period.
Gao Xiang3883a792018-07-26 20:22:06 +0800586 */
587static void z_erofs_rcu_callback(struct rcu_head *head)
588{
Gao Xiang97e86a82019-07-31 23:57:47 +0800589 struct z_erofs_collection *const cl =
590 container_of(head, struct z_erofs_collection, rcu);
Gao Xiang3883a792018-07-26 20:22:06 +0800591
Gao Xiang9f6cc762021-04-07 12:39:20 +0800592 z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
593 primary_collection));
Gao Xiang3883a792018-07-26 20:22:06 +0800594}
595
596void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
597{
Gao Xiang97e86a82019-07-31 23:57:47 +0800598 struct z_erofs_pcluster *const pcl =
599 container_of(grp, struct z_erofs_pcluster, obj);
600 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800601
Gao Xiang97e86a82019-07-31 23:57:47 +0800602 call_rcu(&cl->rcu, z_erofs_rcu_callback);
Gao Xiang3883a792018-07-26 20:22:06 +0800603}
604
Gao Xiang97e86a82019-07-31 23:57:47 +0800605static void z_erofs_collection_put(struct z_erofs_collection *cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800606{
Gao Xiang97e86a82019-07-31 23:57:47 +0800607 struct z_erofs_pcluster *const pcl =
608 container_of(cl, struct z_erofs_pcluster, primary_collection);
609
610 erofs_workgroup_put(&pcl->obj);
Gao Xiang3883a792018-07-26 20:22:06 +0800611}
612
Gao Xiang97e86a82019-07-31 23:57:47 +0800613static bool z_erofs_collector_end(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800614{
Gao Xiang97e86a82019-07-31 23:57:47 +0800615 struct z_erofs_collection *cl = clt->cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800616
Gao Xiang97e86a82019-07-31 23:57:47 +0800617 if (!cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800618 return false;
619
Gao Xiang97e86a82019-07-31 23:57:47 +0800620 z_erofs_pagevec_ctor_exit(&clt->vector, false);
621 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800622
623 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800624 * if all pending pages are added, don't hold its reference
625 * any longer if the pcluster isn't hosted by ourselves.
Gao Xiang3883a792018-07-26 20:22:06 +0800626 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800627 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
628 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800629
Gao Xiang97e86a82019-07-31 23:57:47 +0800630 clt->cl = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +0800631 return true;
632}
633
Gao Xiang97e86a82019-07-31 23:57:47 +0800634static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800635 unsigned int cachestrategy,
Gao Xiang97e86a82019-07-31 23:57:47 +0800636 erofs_off_t la)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800637{
Gao Xiang4279f3f2019-07-31 23:57:49 +0800638 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
639 return false;
640
Gao Xiang92e6efd2018-12-08 00:19:16 +0800641 if (fe->backmost)
642 return true;
643
Gao Xiang4279f3f2019-07-31 23:57:49 +0800644 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
645 la < fe->headoffset;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800646}
Gao Xiang92e6efd2018-12-08 00:19:16 +0800647
Gao Xiang97e86a82019-07-31 23:57:47 +0800648static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
Gao Xiangeaa91722021-10-22 17:01:20 +0800649 struct page *page, struct page **pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800650{
Gao Xiang97e86a82019-07-31 23:57:47 +0800651 struct inode *const inode = fe->inode;
Gao Xiangbda17a42019-10-08 20:56:13 +0800652 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Chao Yu3b423412019-01-15 09:42:21 +0800653 struct erofs_map_blocks *const map = &fe->map;
Gao Xiang97e86a82019-07-31 23:57:47 +0800654 struct z_erofs_collector *const clt = &fe->clt;
Gao Xiang3883a792018-07-26 20:22:06 +0800655 const loff_t offset = page_offset(page);
Gao Xiangdc76ea82019-09-22 18:04:34 +0800656 bool tight = true;
Gao Xiang3883a792018-07-26 20:22:06 +0800657
Gao Xiang92e6efd2018-12-08 00:19:16 +0800658 enum z_erofs_cache_alloctype cache_strategy;
Gao Xiang3883a792018-07-26 20:22:06 +0800659 enum z_erofs_page_type page_type;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200660 unsigned int cur, end, spiltted, index;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800661 int err = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800662
663 /* register locked file pages as online pages in pack */
664 z_erofs_onlinepage_init(page);
665
666 spiltted = 0;
667 end = PAGE_SIZE;
668repeat:
669 cur = end - 1;
670
671 /* lucky, within the range of the current map_blocks */
672 if (offset + cur >= map->m_la &&
Julian Merida447a3622019-03-18 20:58:41 -0300673 offset + cur < map->m_la + map->m_llen) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800674 /* didn't get a valid collection previously (very rare) */
675 if (!clt->cl)
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800676 goto restart_now;
Gao Xiang3883a792018-07-26 20:22:06 +0800677 goto hitted;
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800678 }
Gao Xiang3883a792018-07-26 20:22:06 +0800679
680 /* go ahead the next map_blocks */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800681 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
Gao Xiang3883a792018-07-26 20:22:06 +0800682
Gao Xiang97e86a82019-07-31 23:57:47 +0800683 if (z_erofs_collector_end(clt))
Gao Xiangf0c519f2018-11-23 01:21:49 +0800684 fe->backmost = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800685
686 map->m_la = offset + cur;
687 map->m_llen = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +0800688 err = z_erofs_map_blocks_iter(inode, map, 0);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800689 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800690 goto err_out;
691
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800692restart_now:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800693 if (!(map->m_flags & EROFS_MAP_MAPPED))
Gao Xiang3883a792018-07-26 20:22:06 +0800694 goto hitted;
695
Gao Xiang97e86a82019-07-31 23:57:47 +0800696 err = z_erofs_collector_begin(clt, inode, map);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800697 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800698 goto err_out;
699
Yue Hucecf8642021-12-29 07:29:19 +0800700 if (z_erofs_is_inline_pcluster(clt->pcl)) {
Gao Xiang09c54372022-01-02 12:00:17 +0800701 void *mp;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800702
Gao Xiang09c54372022-01-02 12:00:17 +0800703 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
704 erofs_blknr(map->m_pa), EROFS_NO_KMAP);
705 if (IS_ERR(mp)) {
706 err = PTR_ERR(mp);
Yue Hucecf8642021-12-29 07:29:19 +0800707 erofs_err(inode->i_sb,
708 "failed to get inline page, err %d", err);
709 goto err_out;
710 }
Gao Xiang09c54372022-01-02 12:00:17 +0800711 get_page(fe->map.buf.page);
712 WRITE_ONCE(clt->pcl->compressed_pages[0], fe->map.buf.page);
Yue Hucecf8642021-12-29 07:29:19 +0800713 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
714 } else {
715 /* preload all compressed pages (can change mode if needed) */
716 if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy,
717 map->m_la))
718 cache_strategy = TRYALLOC;
719 else
720 cache_strategy = DONTALLOC;
721
722 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
723 cache_strategy, pagepool);
724 }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800725
Gao Xiang3883a792018-07-26 20:22:06 +0800726hitted:
Gao Xiangdc76ea82019-09-22 18:04:34 +0800727 /*
728 * Ensure the current partial page belongs to this submit chain rather
729 * than other concurrent submit chains or the noio(bypass) chain since
730 * those chains are handled asynchronously thus the page cannot be used
731 * for inplace I/O or pagevec (should be processed in strict order.)
732 */
733 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
734 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
735
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200736 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800737 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800738 zero_user_segment(page, cur, end);
739 goto next_part;
740 }
741
742 /* let's derive page type */
743 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
744 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
745 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
746 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
747
Gao Xianga1121522019-02-27 13:33:32 +0800748 if (cur)
Gao Xiang97e86a82019-07-31 23:57:47 +0800749 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xianga1121522019-02-27 13:33:32 +0800750
Gao Xiang3883a792018-07-26 20:22:06 +0800751retry:
Gao Xiang86432a62021-11-04 02:20:06 +0800752 err = z_erofs_attach_page(clt, page, page_type,
753 clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800754 /* should allocate an additional short-lived page for pagevec */
Gao Xiang3883a792018-07-26 20:22:06 +0800755 if (err == -EAGAIN) {
756 struct page *const newpage =
Chao Yue3f78d52020-09-17 09:18:21 +0800757 alloc_page(GFP_NOFS | __GFP_NOFAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800758
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800759 set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
Gao Xiang97e86a82019-07-31 23:57:47 +0800760 err = z_erofs_attach_page(clt, newpage,
Gao Xiang86432a62021-11-04 02:20:06 +0800761 Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800762 if (!err)
Gao Xiang3883a792018-07-26 20:22:06 +0800763 goto retry;
764 }
765
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800766 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800767 goto err_out;
768
Gao Xiang97e86a82019-07-31 23:57:47 +0800769 index = page->index - (map->m_la >> PAGE_SHIFT);
Gao Xiang3883a792018-07-26 20:22:06 +0800770
Gao Xiang3883a792018-07-26 20:22:06 +0800771 z_erofs_onlinepage_fixup(page, index, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800772
Gao Xiang1e05ff32018-09-18 22:27:25 +0800773 /* bump up the number of spiltted parts of a page */
774 ++spiltted;
775 /* also update nr_pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800776 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
Gao Xiang3883a792018-07-26 20:22:06 +0800777next_part:
778 /* can be used for verification */
779 map->m_llen = offset + cur - map->m_la;
780
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300781 end = cur;
782 if (end > 0)
Gao Xiang3883a792018-07-26 20:22:06 +0800783 goto repeat;
784
Gao Xiang1e05ff32018-09-18 22:27:25 +0800785out:
Gao Xiang3883a792018-07-26 20:22:06 +0800786 z_erofs_onlinepage_endio(page);
787
Gao Xiang4f761fa2019-09-04 10:09:09 +0800788 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
789 __func__, page, spiltted, map->m_llen);
Gao Xiang3883a792018-07-26 20:22:06 +0800790 return err;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800791
792 /* if some error occurred while processing this page */
793err_out:
794 SetPageError(page);
795 goto out;
Gao Xiang3883a792018-07-26 20:22:06 +0800796}
797
Huang Jianan40452ff2021-12-06 22:35:52 +0800798static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
799 unsigned int readahead_pages)
800{
801 /* auto: enable for readpage, disable for readahead */
802 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
803 !readahead_pages)
804 return true;
805
806 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
807 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
808 return true;
809
810 return false;
811}
812
Huang Jianan648f2de2021-03-17 11:54:47 +0800813static void z_erofs_decompressqueue_work(struct work_struct *work);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800814static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
815 bool sync, int bios)
Gao Xiang3883a792018-07-26 20:22:06 +0800816{
Huang Jianan30048cd2021-03-17 11:54:48 +0800817 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
818
Gao Xianga4b1fab2019-10-08 20:56:15 +0800819 /* wake up the caller thread for sync decompression */
820 if (sync) {
Gao Xiang848bd9a2018-12-08 00:19:12 +0800821 unsigned long flags;
Gao Xiang3883a792018-07-26 20:22:06 +0800822
Gao Xiang848bd9a2018-12-08 00:19:12 +0800823 spin_lock_irqsave(&io->u.wait.lock, flags);
824 if (!atomic_add_return(bios, &io->pending_bios))
825 wake_up_locked(&io->u.wait);
826 spin_unlock_irqrestore(&io->u.wait.lock, flags);
827 return;
828 }
829
Huang Jianan648f2de2021-03-17 11:54:47 +0800830 if (atomic_add_return(bios, &io->pending_bios))
831 return;
Huang Jianan30048cd2021-03-17 11:54:48 +0800832 /* Use workqueue and sync decompression for atomic contexts only */
Huang Jianan648f2de2021-03-17 11:54:47 +0800833 if (in_atomic() || irqs_disabled()) {
Gao Xiang3883a792018-07-26 20:22:06 +0800834 queue_work(z_erofs_workqueue, &io->u.work);
Huang Jianan40452ff2021-12-06 22:35:52 +0800835 /* enable sync decompression for readahead */
836 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
837 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
Huang Jianan648f2de2021-03-17 11:54:47 +0800838 return;
839 }
840 z_erofs_decompressqueue_work(&io->u.work);
Gao Xiang3883a792018-07-26 20:22:06 +0800841}
842
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800843static bool z_erofs_page_is_invalidated(struct page *page)
844{
845 return !page->mapping && !z_erofs_is_shortlived_page(page);
846}
847
Gao Xiang0c638f72019-11-08 11:37:33 +0800848static void z_erofs_decompressqueue_endio(struct bio *bio)
Gao Xiang3883a792018-07-26 20:22:06 +0800849{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800850 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
851 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
Gao Xiang14a56ec2019-03-25 11:40:09 +0800852 blk_status_t err = bio->bi_status;
Gao Xiang3883a792018-07-26 20:22:06 +0800853 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800854 struct bvec_iter_all iter_all;
Gao Xiang3883a792018-07-26 20:22:06 +0800855
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200856 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang3883a792018-07-26 20:22:06 +0800857 struct page *page = bvec->bv_page;
858
859 DBG_BUGON(PageUptodate(page));
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800860 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800861
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800862 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800863 SetPageError(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800864
Gao Xianga4b1fab2019-10-08 20:56:15 +0800865 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
866 if (!err)
867 SetPageUptodate(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800868 unlock_page(page);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800869 }
Gao Xiang3883a792018-07-26 20:22:06 +0800870 }
Gao Xianga4b1fab2019-10-08 20:56:15 +0800871 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
Gao Xiang3883a792018-07-26 20:22:06 +0800872 bio_put(bio);
873}
874
Gao Xiang97e86a82019-07-31 23:57:47 +0800875static int z_erofs_decompress_pcluster(struct super_block *sb,
876 struct z_erofs_pcluster *pcl,
Gao Xiangeaa91722021-10-22 17:01:20 +0800877 struct page **pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800878{
879 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Yue Hucecf8642021-12-29 07:29:19 +0800880 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800881 struct z_erofs_pagevec_ctor ctor;
Gao Xiang9f6cc762021-04-07 12:39:20 +0800882 unsigned int i, inputsize, outputsize, llen, nr_pages;
Gao Xiang97e86a82019-07-31 23:57:47 +0800883 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
Gao Xiang3883a792018-07-26 20:22:06 +0800884 struct page **pages, **compressed_pages, *page;
Gao Xiang3883a792018-07-26 20:22:06 +0800885
886 enum z_erofs_page_type page_type;
Gao Xiangb6a76182019-06-24 15:22:58 +0800887 bool overlapped, partial;
Gao Xiang97e86a82019-07-31 23:57:47 +0800888 struct z_erofs_collection *cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800889 int err;
890
891 might_sleep();
Gao Xiang97e86a82019-07-31 23:57:47 +0800892 cl = z_erofs_primarycollection(pcl);
893 DBG_BUGON(!READ_ONCE(cl->nr_pages));
Gao Xiang3883a792018-07-26 20:22:06 +0800894
Gao Xiang97e86a82019-07-31 23:57:47 +0800895 mutex_lock(&cl->lock);
896 nr_pages = cl->nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800897
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800898 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
Gao Xiang3883a792018-07-26 20:22:06 +0800899 pages = pages_onstack;
Gao Xiang97e86a82019-07-31 23:57:47 +0800900 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
901 mutex_trylock(&z_pagemap_global_lock)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800902 pages = z_pagemap_global;
Gao Xiang97e86a82019-07-31 23:57:47 +0800903 } else {
Chao Yu441dfcc2019-07-16 17:44:22 +0800904 gfp_t gfp_flags = GFP_KERNEL;
905
Gao Xiang97e86a82019-07-31 23:57:47 +0800906 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
Chao Yu441dfcc2019-07-16 17:44:22 +0800907 gfp_flags |= __GFP_NOFAIL;
908
Julian Merida447a3622019-03-18 20:58:41 -0300909 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Chao Yu441dfcc2019-07-16 17:44:22 +0800910 gfp_flags);
Gao Xiang3883a792018-07-26 20:22:06 +0800911
912 /* fallback to global pagemap for the lowmem scenario */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800913 if (!pages) {
Chao Yu441dfcc2019-07-16 17:44:22 +0800914 mutex_lock(&z_pagemap_global_lock);
915 pages = z_pagemap_global;
Gao Xiang3883a792018-07-26 20:22:06 +0800916 }
917 }
918
919 for (i = 0; i < nr_pages; ++i)
920 pages[i] = NULL;
921
Gao Xiange12a0ce2019-08-21 22:01:52 +0800922 err = 0;
Gao Xiangfa61a332019-06-24 15:22:53 +0800923 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang97e86a82019-07-31 23:57:47 +0800924 cl->pagevec, 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800925
Gao Xiang97e86a82019-07-31 23:57:47 +0800926 for (i = 0; i < cl->vcnt; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200927 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800928
Gao Xiang046d64e2019-07-31 23:57:45 +0800929 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800930
931 /* all pages in pagevec ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100932 DBG_BUGON(!page);
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800933 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800934
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800935 if (z_erofs_put_shortlivedpage(pagepool, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800936 continue;
937
938 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
939 pagenr = 0;
940 else
941 pagenr = z_erofs_onlinepage_index(page);
942
Gao Xiang70b17992018-12-11 15:17:49 +0800943 DBG_BUGON(pagenr >= nr_pages);
Gao Xiange5e3abb2018-09-19 13:49:07 +0800944
Gao Xiange12a0ce2019-08-21 22:01:52 +0800945 /*
946 * currently EROFS doesn't support multiref(dedup),
947 * so here erroring out one multiref page.
948 */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800949 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800950 DBG_BUGON(1);
951 SetPageError(pages[pagenr]);
952 z_erofs_onlinepage_endio(pages[pagenr]);
953 err = -EFSCORRUPTED;
954 }
Gao Xiang3883a792018-07-26 20:22:06 +0800955 pages[pagenr] = page;
956 }
Gao Xiang3883a792018-07-26 20:22:06 +0800957 z_erofs_pagevec_ctor_exit(&ctor, true);
958
959 overlapped = false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800960 compressed_pages = pcl->compressed_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800961
Yue Hucecf8642021-12-29 07:29:19 +0800962 for (i = 0; i < pclusterpages; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200963 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800964
965 page = compressed_pages[i];
Gao Xiang3883a792018-07-26 20:22:06 +0800966 /* all compressed pages ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100967 DBG_BUGON(!page);
Gao Xiang3883a792018-07-26 20:22:06 +0800968
Yue Hucecf8642021-12-29 07:29:19 +0800969 if (z_erofs_is_inline_pcluster(pcl)) {
970 if (!PageUptodate(page))
971 err = -EIO;
972 continue;
973 }
974
975 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800976 if (!z_erofs_is_shortlived_page(page)) {
Gao Xiangd61fbb62019-03-25 11:40:08 +0800977 if (erofs_page_is_managed(sbi, page)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800978 if (!PageUptodate(page))
Gao Xiang11152492019-03-25 11:40:07 +0800979 err = -EIO;
980 continue;
981 }
Gao Xiang3883a792018-07-26 20:22:06 +0800982
Gao Xiang11152492019-03-25 11:40:07 +0800983 /*
984 * only if non-head page can be selected
985 * for inplace decompression
986 */
987 pagenr = z_erofs_onlinepage_index(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800988
Gao Xiang11152492019-03-25 11:40:07 +0800989 DBG_BUGON(pagenr >= nr_pages);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800990 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800991 DBG_BUGON(1);
992 SetPageError(pages[pagenr]);
993 z_erofs_onlinepage_endio(pages[pagenr]);
994 err = -EFSCORRUPTED;
995 }
Gao Xiang11152492019-03-25 11:40:07 +0800996 pages[pagenr] = page;
Gao Xiang3883a792018-07-26 20:22:06 +0800997
Gao Xiang11152492019-03-25 11:40:07 +0800998 overlapped = true;
999 }
1000
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001001 /* PG_error needs checking for all non-managed pages */
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001002 if (PageError(page)) {
Gao Xiang11152492019-03-25 11:40:07 +08001003 DBG_BUGON(PageUptodate(page));
1004 err = -EIO;
1005 }
Gao Xiang3883a792018-07-26 20:22:06 +08001006 }
1007
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001008 if (err)
Gao Xiang11152492019-03-25 11:40:07 +08001009 goto out;
1010
Gao Xiang97e86a82019-07-31 23:57:47 +08001011 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
1012 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
1013 outputsize = llen;
1014 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
Gao Xiangb6a76182019-06-24 15:22:58 +08001015 } else {
Gao Xiang97e86a82019-07-31 23:57:47 +08001016 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
Gao Xiangb6a76182019-06-24 15:22:58 +08001017 partial = true;
1018 }
Gao Xiang3883a792018-07-26 20:22:06 +08001019
Yue Hucecf8642021-12-29 07:29:19 +08001020 if (z_erofs_is_inline_pcluster(pcl))
1021 inputsize = pcl->tailpacking_size;
1022 else
1023 inputsize = pclusterpages * PAGE_SIZE;
1024
Gao Xiang88aaf5a2019-06-24 15:22:57 +08001025 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
1026 .sb = sb,
1027 .in = compressed_pages,
1028 .out = pages,
Yue Hucecf8642021-12-29 07:29:19 +08001029 .pageofs_in = pcl->pageofs_in,
Gao Xiang97e86a82019-07-31 23:57:47 +08001030 .pageofs_out = cl->pageofs,
Gao Xiang9f6cc762021-04-07 12:39:20 +08001031 .inputsize = inputsize,
Gao Xiang88aaf5a2019-06-24 15:22:57 +08001032 .outputsize = outputsize,
Gao Xiang97e86a82019-07-31 23:57:47 +08001033 .alg = pcl->algorithmformat,
Gao Xiang88aaf5a2019-06-24 15:22:57 +08001034 .inplace_io = overlapped,
Gao Xiangb6a76182019-06-24 15:22:58 +08001035 .partial_decoding = partial
Gao Xiang97e86a82019-07-31 23:57:47 +08001036 }, pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001037
1038out:
Yue Hucecf8642021-12-29 07:29:19 +08001039 /* must handle all compressed pages before actual file pages */
1040 if (z_erofs_is_inline_pcluster(pcl)) {
1041 page = compressed_pages[0];
1042 WRITE_ONCE(compressed_pages[0], NULL);
1043 put_page(page);
1044 } else {
1045 for (i = 0; i < pclusterpages; ++i) {
1046 page = compressed_pages[i];
Gao Xiang3883a792018-07-26 20:22:06 +08001047
Yue Hucecf8642021-12-29 07:29:19 +08001048 if (erofs_page_is_managed(sbi, page))
1049 continue;
Gao Xiangd61fbb62019-03-25 11:40:08 +08001050
Yue Hucecf8642021-12-29 07:29:19 +08001051 /* recycle all individual short-lived pages */
1052 (void)z_erofs_put_shortlivedpage(pagepool, page);
1053 WRITE_ONCE(compressed_pages[i], NULL);
1054 }
Gao Xiang3883a792018-07-26 20:22:06 +08001055 }
1056
Gao Xiangaf692e12019-02-27 13:33:30 +08001057 for (i = 0; i < nr_pages; ++i) {
1058 page = pages[i];
1059 if (!page)
1060 continue;
1061
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001062 DBG_BUGON(z_erofs_page_is_invalidated(page));
Gao Xiangaf692e12019-02-27 13:33:30 +08001063
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001064 /* recycle all individual short-lived pages */
1065 if (z_erofs_put_shortlivedpage(pagepool, page))
Gao Xiangaf692e12019-02-27 13:33:30 +08001066 continue;
1067
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001068 if (err < 0)
Gao Xiangaf692e12019-02-27 13:33:30 +08001069 SetPageError(page);
1070
1071 z_erofs_onlinepage_endio(page);
1072 }
1073
Gao Xiang3883a792018-07-26 20:22:06 +08001074 if (pages == z_pagemap_global)
1075 mutex_unlock(&z_pagemap_global_lock);
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001076 else if (pages != pages_onstack)
Gao Xiang3883a792018-07-26 20:22:06 +08001077 kvfree(pages);
1078
Gao Xiang97e86a82019-07-31 23:57:47 +08001079 cl->nr_pages = 0;
1080 cl->vcnt = 0;
Gao Xiang3883a792018-07-26 20:22:06 +08001081
Gao Xiang97e86a82019-07-31 23:57:47 +08001082 /* all cl locks MUST be taken before the following line */
1083 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001084
Gao Xiang97e86a82019-07-31 23:57:47 +08001085 /* all cl locks SHOULD be released right now */
1086 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +08001087
Gao Xiang97e86a82019-07-31 23:57:47 +08001088 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +08001089 return err;
1090}
1091
Gao Xiang0c638f72019-11-08 11:37:33 +08001092static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
Gao Xiangeaa91722021-10-22 17:01:20 +08001093 struct page **pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +08001094{
Gao Xiang97e86a82019-07-31 23:57:47 +08001095 z_erofs_next_pcluster_t owned = io->head;
Gao Xiang3883a792018-07-26 20:22:06 +08001096
Gao Xiang97e86a82019-07-31 23:57:47 +08001097 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1098 struct z_erofs_pcluster *pcl;
Gao Xiang3883a792018-07-26 20:22:06 +08001099
1100 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
Gao Xiang97e86a82019-07-31 23:57:47 +08001101 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001102
1103 /* no possible that 'owned' equals NULL */
Gao Xiang97e86a82019-07-31 23:57:47 +08001104 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001105
Gao Xiang97e86a82019-07-31 23:57:47 +08001106 pcl = container_of(owned, struct z_erofs_pcluster, next);
1107 owned = READ_ONCE(pcl->next);
Gao Xiang3883a792018-07-26 20:22:06 +08001108
Gao Xianga4b1fab2019-10-08 20:56:15 +08001109 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
Gao Xiang3978c8e2018-08-06 11:27:53 +08001110 }
Gao Xiang3883a792018-07-26 20:22:06 +08001111}
1112
Gao Xiang0c638f72019-11-08 11:37:33 +08001113static void z_erofs_decompressqueue_work(struct work_struct *work)
Gao Xiang3883a792018-07-26 20:22:06 +08001114{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001115 struct z_erofs_decompressqueue *bgq =
1116 container_of(work, struct z_erofs_decompressqueue, u.work);
Gao Xiangeaa91722021-10-22 17:01:20 +08001117 struct page *pagepool = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001118
Gao Xianga4b1fab2019-10-08 20:56:15 +08001119 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang0c638f72019-11-08 11:37:33 +08001120 z_erofs_decompress_queue(bgq, &pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001121
Gao Xiangeaa91722021-10-22 17:01:20 +08001122 erofs_release_pages(&pagepool);
Gao Xianga4b1fab2019-10-08 20:56:15 +08001123 kvfree(bgq);
Gao Xiang3883a792018-07-26 20:22:06 +08001124}
1125
Gao Xiang97e86a82019-07-31 23:57:47 +08001126static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1127 unsigned int nr,
Gao Xiangeaa91722021-10-22 17:01:20 +08001128 struct page **pagepool,
Gao Xiang97e86a82019-07-31 23:57:47 +08001129 struct address_space *mc,
1130 gfp_t gfp)
Gao Xiang9248fce2018-12-08 00:19:15 +08001131{
Gao Xiang97e86a82019-07-31 23:57:47 +08001132 const pgoff_t index = pcl->obj.index;
Gao Xiang9248fce2018-12-08 00:19:15 +08001133 bool tocache = false;
1134
1135 struct address_space *mapping;
1136 struct page *oldpage, *page;
1137
Gao Xiang92e6efd2018-12-08 00:19:16 +08001138 compressed_page_t t;
1139 int justfound;
1140
Gao Xiang9248fce2018-12-08 00:19:15 +08001141repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +08001142 page = READ_ONCE(pcl->compressed_pages[nr]);
Gao Xiang9248fce2018-12-08 00:19:15 +08001143 oldpage = page;
1144
1145 if (!page)
1146 goto out_allocpage;
1147
Gao Xiang92e6efd2018-12-08 00:19:16 +08001148 /* process the target tagged pointer */
1149 t = tagptr_init(compressed_page_t, page);
1150 justfound = tagptr_unfold_tags(t);
1151 page = tagptr_unfold_ptr(t);
1152
Gao Xiang1825c8d2020-12-09 20:37:17 +08001153 /*
1154 * preallocated cached pages, which is used to avoid direct reclaim
1155 * otherwise, it will go inplace I/O path instead.
1156 */
1157 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1158 WRITE_ONCE(pcl->compressed_pages[nr], page);
1159 set_page_private(page, 0);
1160 tocache = true;
1161 goto out_tocache;
1162 }
Gao Xiang9248fce2018-12-08 00:19:15 +08001163 mapping = READ_ONCE(page->mapping);
1164
1165 /*
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001166 * file-backed online pages in plcuster are all locked steady,
Gao Xiang9248fce2018-12-08 00:19:15 +08001167 * therefore it is impossible for `mapping' to be NULL.
1168 */
1169 if (mapping && mapping != mc)
1170 /* ought to be unmanaged pages */
1171 goto out;
1172
Gao Xiang6aaa7b02020-12-08 17:58:32 +08001173 /* directly return for shortlived page as well */
1174 if (z_erofs_is_shortlived_page(page))
1175 goto out;
1176
Gao Xiang9248fce2018-12-08 00:19:15 +08001177 lock_page(page);
1178
Gao Xiang92e6efd2018-12-08 00:19:16 +08001179 /* only true if page reclaim goes wrong, should never happen */
1180 DBG_BUGON(justfound && PagePrivate(page));
1181
Gao Xiang9248fce2018-12-08 00:19:15 +08001182 /* the page is still in manage cache */
1183 if (page->mapping == mc) {
Gao Xiang97e86a82019-07-31 23:57:47 +08001184 WRITE_ONCE(pcl->compressed_pages[nr], page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001185
Gao Xiang11152492019-03-25 11:40:07 +08001186 ClearPageError(page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001187 if (!PagePrivate(page)) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001188 /*
1189 * impossible to be !PagePrivate(page) for
1190 * the current restriction as well if
1191 * the page is already in compressed_pages[].
1192 */
1193 DBG_BUGON(!justfound);
1194
1195 justfound = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +08001196 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001197 SetPagePrivate(page);
1198 }
1199
1200 /* no need to submit io if it is already up-to-date */
1201 if (PageUptodate(page)) {
1202 unlock_page(page);
1203 page = NULL;
1204 }
1205 goto out;
1206 }
1207
1208 /*
1209 * the managed page has been truncated, it's unsafe to
1210 * reuse this one, let's allocate a new cache-managed page.
1211 */
1212 DBG_BUGON(page->mapping);
Gao Xiang92e6efd2018-12-08 00:19:16 +08001213 DBG_BUGON(!justfound);
Gao Xiang9248fce2018-12-08 00:19:15 +08001214
1215 tocache = true;
1216 unlock_page(page);
1217 put_page(page);
1218out_allocpage:
Gao Xiang5ddcee12019-11-21 21:59:54 +08001219 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
Gao Xiang5ddcee12019-11-21 21:59:54 +08001220 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
Gao Xiangeaa91722021-10-22 17:01:20 +08001221 erofs_pagepool_add(pagepool, page);
Gao Xiang5ddcee12019-11-21 21:59:54 +08001222 cond_resched();
1223 goto repeat;
1224 }
Gao Xiang1825c8d2020-12-09 20:37:17 +08001225out_tocache:
Gao Xiangbf225072020-12-08 17:58:33 +08001226 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1227 /* turn into temporary page if fails (1 ref) */
1228 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1229 goto out;
Gao Xianga30573b2020-10-22 22:57:21 +08001230 }
Gao Xiangbf225072020-12-08 17:58:33 +08001231 attach_page_private(page, pcl);
1232 /* drop a refcount added by allocpage (then we have 2 refs here) */
1233 put_page(page);
1234
Gao Xiang9248fce2018-12-08 00:19:15 +08001235out: /* the only exit (for tracing and debugging) */
1236 return page;
1237}
1238
Gao Xianga4b1fab2019-10-08 20:56:15 +08001239static struct z_erofs_decompressqueue *
1240jobqueue_init(struct super_block *sb,
1241 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001242{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001243 struct z_erofs_decompressqueue *q;
Gao Xiang3883a792018-07-26 20:22:06 +08001244
Gao Xianga4b1fab2019-10-08 20:56:15 +08001245 if (fg && !*fg) {
1246 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1247 if (!q) {
1248 *fg = true;
1249 goto fg_out;
1250 }
Gao Xiang0c638f72019-11-08 11:37:33 +08001251 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
Gao Xianga4b1fab2019-10-08 20:56:15 +08001252 } else {
1253fg_out:
1254 q = fgq;
1255 init_waitqueue_head(&fgq->u.wait);
1256 atomic_set(&fgq->pending_bios, 0);
Gao Xiang3883a792018-07-26 20:22:06 +08001257 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001258 q->sb = sb;
1259 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1260 return q;
Gao Xiang3883a792018-07-26 20:22:06 +08001261}
1262
Gao Xiang97e86a82019-07-31 23:57:47 +08001263/* define decompression jobqueue types */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001264enum {
Gao Xiang7146a4f2018-12-08 00:19:18 +08001265 JQ_BYPASS,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001266 JQ_SUBMIT,
1267 NR_JOBQUEUES,
1268};
1269
1270static void *jobqueueset_init(struct super_block *sb,
Gao Xianga4b1fab2019-10-08 20:56:15 +08001271 struct z_erofs_decompressqueue *q[],
1272 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001273{
Gao Xiang7146a4f2018-12-08 00:19:18 +08001274 /*
1275 * if managed cache is enabled, bypass jobqueue is needed,
Gao Xiang97e86a82019-07-31 23:57:47 +08001276 * no need to read from device for all pclusters in this queue.
Gao Xiang7146a4f2018-12-08 00:19:18 +08001277 */
Gao Xianga4b1fab2019-10-08 20:56:15 +08001278 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1279 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001280
Gao Xianga4b1fab2019-10-08 20:56:15 +08001281 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
Gao Xiang7146a4f2018-12-08 00:19:18 +08001282}
1283
Gao Xiang97e86a82019-07-31 23:57:47 +08001284static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1285 z_erofs_next_pcluster_t qtail[],
1286 z_erofs_next_pcluster_t owned_head)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001287{
Gao Xiang97e86a82019-07-31 23:57:47 +08001288 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1289 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001290
Gao Xiang97e86a82019-07-31 23:57:47 +08001291 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1292 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1293 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001294
Gao Xiang97e86a82019-07-31 23:57:47 +08001295 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001296
1297 WRITE_ONCE(*submit_qtail, owned_head);
Gao Xiang97e86a82019-07-31 23:57:47 +08001298 WRITE_ONCE(*bypass_qtail, &pcl->next);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001299
Gao Xiang97e86a82019-07-31 23:57:47 +08001300 qtail[JQ_BYPASS] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001301}
1302
Gao Xiang1e4a2952020-01-21 14:48:19 +08001303static void z_erofs_submit_queue(struct super_block *sb,
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001304 struct z_erofs_decompress_frontend *f,
Gao Xiangeaa91722021-10-22 17:01:20 +08001305 struct page **pagepool,
Gao Xiang0c638f72019-11-08 11:37:33 +08001306 struct z_erofs_decompressqueue *fgq,
1307 bool *force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001308{
Gao Xiangbda17a42019-10-08 20:56:13 +08001309 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +08001310 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
Gao Xianga4b1fab2019-10-08 20:56:15 +08001311 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001312 void *bi_private;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001313 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001314 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
Kees Cook3f649ab2020-06-03 13:09:38 -07001315 pgoff_t last_index;
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001316 struct block_device *last_bdev;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001317 unsigned int nr_bios = 0;
1318 struct bio *bio = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001319
Gao Xianga4b1fab2019-10-08 20:56:15 +08001320 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1321 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1322 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
Gao Xiang3883a792018-07-26 20:22:06 +08001323
1324 /* by default, all need io submission */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001325 q[JQ_SUBMIT]->head = owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001326
1327 do {
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001328 struct erofs_map_dev mdev;
Gao Xiang97e86a82019-07-31 23:57:47 +08001329 struct z_erofs_pcluster *pcl;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001330 pgoff_t cur, end;
1331 unsigned int i = 0;
1332 bool bypass = true;
Gao Xiang3883a792018-07-26 20:22:06 +08001333
1334 /* no possible 'owned_head' equals the following */
Gao Xiang97e86a82019-07-31 23:57:47 +08001335 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1336 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001337
Gao Xiang97e86a82019-07-31 23:57:47 +08001338 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1339
Yue Hucecf8642021-12-29 07:29:19 +08001340 /* close the main owned chain at first */
1341 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1342 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1343 if (z_erofs_is_inline_pcluster(pcl)) {
1344 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1345 continue;
1346 }
1347
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001348 /* no device id here, thus it will always succeed */
1349 mdev = (struct erofs_map_dev) {
1350 .m_pa = blknr_to_addr(pcl->obj.index),
1351 };
1352 (void)erofs_map_dev(sb, &mdev);
1353
1354 cur = erofs_blknr(mdev.m_pa);
Gao Xiang9f6cc762021-04-07 12:39:20 +08001355 end = cur + pcl->pclusterpages;
Gao Xiang3883a792018-07-26 20:22:06 +08001356
Gao Xiang1e4a2952020-01-21 14:48:19 +08001357 do {
1358 struct page *page;
Gao Xiang9248fce2018-12-08 00:19:15 +08001359
Gao Xiang1e4a2952020-01-21 14:48:19 +08001360 page = pickup_page_for_submission(pcl, i++, pagepool,
1361 MNGD_MAPPING(sbi),
1362 GFP_NOFS);
1363 if (!page)
1364 continue;
Gao Xiang3883a792018-07-26 20:22:06 +08001365
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001366 if (bio && (cur != last_index + 1 ||
1367 last_bdev != mdev.m_bdev)) {
Gao Xiang3883a792018-07-26 20:22:06 +08001368submit_bio_retry:
Gao Xiang1e4a2952020-01-21 14:48:19 +08001369 submit_bio(bio);
1370 bio = NULL;
1371 }
Gao Xiang3883a792018-07-26 20:22:06 +08001372
Gao Xiang1e4a2952020-01-21 14:48:19 +08001373 if (!bio) {
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001374 bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS);
Gao Xiang1e4a2952020-01-21 14:48:19 +08001375 bio->bi_end_io = z_erofs_decompressqueue_endio;
Gao Xiangdfeab2e2021-10-14 16:10:10 +08001376
1377 bio_set_dev(bio, mdev.m_bdev);
1378 last_bdev = mdev.m_bdev;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001379 bio->bi_iter.bi_sector = (sector_t)cur <<
1380 LOG_SECTORS_PER_BLOCK;
1381 bio->bi_private = bi_private;
1382 bio->bi_opf = REQ_OP_READ;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001383 if (f->readahead)
1384 bio->bi_opf |= REQ_RAHEAD;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001385 ++nr_bios;
1386 }
Gao Xiang94e4e152019-09-04 10:09:04 +08001387
Gao Xiang6c3e4852020-09-19 15:27:28 +08001388 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
Gao Xiang1e4a2952020-01-21 14:48:19 +08001389 goto submit_bio_retry;
Gao Xiang3883a792018-07-26 20:22:06 +08001390
Gao Xiang1e4a2952020-01-21 14:48:19 +08001391 last_index = cur;
1392 bypass = false;
1393 } while (++cur < end);
Gao Xiang3883a792018-07-26 20:22:06 +08001394
Gao Xiang1e4a2952020-01-21 14:48:19 +08001395 if (!bypass)
Gao Xiang97e86a82019-07-31 23:57:47 +08001396 qtail[JQ_SUBMIT] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001397 else
Gao Xiang97e86a82019-07-31 23:57:47 +08001398 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1399 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001400
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001401 if (bio)
Gao Xiang94e4e152019-09-04 10:09:04 +08001402 submit_bio(bio);
Gao Xiang3883a792018-07-26 20:22:06 +08001403
Gao Xiang587a67b2020-01-21 14:47:47 +08001404 /*
1405 * although background is preferred, no one is pending for submission.
1406 * don't issue workqueue for decompression but drop it directly instead.
1407 */
1408 if (!*force_fg && !nr_bios) {
1409 kvfree(q[JQ_SUBMIT]);
Gao Xiang1e4a2952020-01-21 14:48:19 +08001410 return;
Gao Xiang587a67b2020-01-21 14:47:47 +08001411 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001412 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
Gao Xiang3883a792018-07-26 20:22:06 +08001413}
1414
Gao Xiang0c638f72019-11-08 11:37:33 +08001415static void z_erofs_runqueue(struct super_block *sb,
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001416 struct z_erofs_decompress_frontend *f,
Gao Xiangeaa91722021-10-22 17:01:20 +08001417 struct page **pagepool, bool force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001418{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001419 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001420
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001421 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +08001422 return;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001423 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
Gao Xiang3883a792018-07-26 20:22:06 +08001424
Gao Xiang0c638f72019-11-08 11:37:33 +08001425 /* handle bypass queue (no i/o pclusters) immediately */
1426 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
Gao Xiang4279f3f2019-07-31 23:57:49 +08001427
Gao Xiang3883a792018-07-26 20:22:06 +08001428 if (!force_fg)
1429 return;
1430
1431 /* wait until all bios are completed */
Gao Xianga93f8c32019-10-08 20:56:16 +08001432 io_wait_event(io[JQ_SUBMIT].u.wait,
1433 !atomic_read(&io[JQ_SUBMIT].pending_bios));
Gao Xiang3883a792018-07-26 20:22:06 +08001434
Gao Xiang0c638f72019-11-08 11:37:33 +08001435 /* handle synchronous decompress queue in the caller context */
1436 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001437}
1438
Gao Xiang38629292021-10-09 04:08:39 +08001439/*
1440 * Since partial uptodate is still unimplemented for now, we have to use
1441 * approximate readmore strategies as a start.
1442 */
1443static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1444 struct readahead_control *rac,
1445 erofs_off_t end,
Gao Xiangeaa91722021-10-22 17:01:20 +08001446 struct page **pagepool,
Gao Xiang38629292021-10-09 04:08:39 +08001447 bool backmost)
1448{
1449 struct inode *inode = f->inode;
1450 struct erofs_map_blocks *map = &f->map;
1451 erofs_off_t cur;
1452 int err;
1453
1454 if (backmost) {
1455 map->m_la = end;
Gao Xiang622cead2021-10-11 05:31:45 +08001456 err = z_erofs_map_blocks_iter(inode, map,
1457 EROFS_GET_BLOCKS_READMORE);
Gao Xiang38629292021-10-09 04:08:39 +08001458 if (err)
1459 return;
1460
1461 /* expend ra for the trailing edge if readahead */
1462 if (rac) {
1463 loff_t newstart = readahead_pos(rac);
1464
1465 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1466 readahead_expand(rac, newstart, cur - newstart);
1467 return;
1468 }
1469 end = round_up(end, PAGE_SIZE);
1470 } else {
1471 end = round_up(map->m_la, PAGE_SIZE);
1472
1473 if (!map->m_llen)
1474 return;
1475 }
1476
1477 cur = map->m_la + map->m_llen - 1;
1478 while (cur >= end) {
1479 pgoff_t index = cur >> PAGE_SHIFT;
1480 struct page *page;
1481
1482 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1483 if (!page)
1484 goto skip;
1485
1486 if (PageUptodate(page)) {
1487 unlock_page(page);
1488 put_page(page);
1489 goto skip;
1490 }
1491
1492 err = z_erofs_do_read_page(f, page, pagepool);
1493 if (err)
1494 erofs_err(inode->i_sb,
1495 "readmore error at page %lu @ nid %llu",
1496 index, EROFS_I(inode)->nid);
1497 put_page(page);
1498skip:
1499 if (cur < PAGE_SIZE)
1500 break;
1501 cur = (index << PAGE_SHIFT) - 1;
1502 }
1503}
1504
Gao Xiang0c638f72019-11-08 11:37:33 +08001505static int z_erofs_readpage(struct file *file, struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +08001506{
1507 struct inode *const inode = page->mapping->host;
Huang Jianan40452ff2021-12-06 22:35:52 +08001508 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Gao Xiang97e86a82019-07-31 23:57:47 +08001509 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiangeaa91722021-10-22 17:01:20 +08001510 struct page *pagepool = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001511 int err;
Gao Xiang3883a792018-07-26 20:22:06 +08001512
Gao Xiangba9ce772018-11-23 01:15:58 +08001513 trace_erofs_readpage(page, false);
Gao Xiangf0c519f2018-11-23 01:21:49 +08001514 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1515
Gao Xiang38629292021-10-09 04:08:39 +08001516 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
1517 &pagepool, true);
Gao Xiang1825c8d2020-12-09 20:37:17 +08001518 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xiang38629292021-10-09 04:08:39 +08001519 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
1520
Gao Xiang97e86a82019-07-31 23:57:47 +08001521 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001522
Gao Xiangee451972019-08-19 18:34:21 +08001523 /* if some compressed cluster ready, need submit them anyway */
Huang Jianan40452ff2021-12-06 22:35:52 +08001524 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1525 z_erofs_get_sync_decompress_policy(sbi, 0));
Gao Xiangee451972019-08-19 18:34:21 +08001526
1527 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001528 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
Gao Xiangee451972019-08-19 18:34:21 +08001529
Gao Xiang09c54372022-01-02 12:00:17 +08001530 erofs_put_metabuf(&f.map.buf);
Gao Xiangeaa91722021-10-22 17:01:20 +08001531 erofs_release_pages(&pagepool);
Gao Xiangee451972019-08-19 18:34:21 +08001532 return err;
Gao Xiang3883a792018-07-26 20:22:06 +08001533}
1534
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001535static void z_erofs_readahead(struct readahead_control *rac)
Gao Xiang3883a792018-07-26 20:22:06 +08001536{
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001537 struct inode *const inode = rac->mapping->host;
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001538 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Gao Xiang97e86a82019-07-31 23:57:47 +08001539 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiangeaa91722021-10-22 17:01:20 +08001540 struct page *pagepool = NULL, *head = NULL, *page;
Gao Xiang38629292021-10-09 04:08:39 +08001541 unsigned int nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +08001542
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001543 f.readahead = true;
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001544 f.headoffset = readahead_pos(rac);
Gao Xiangf0c519f2018-11-23 01:21:49 +08001545
Gao Xiang38629292021-10-09 04:08:39 +08001546 z_erofs_pcluster_readmore(&f, rac, f.headoffset +
1547 readahead_length(rac) - 1, &pagepool, true);
1548 nr_pages = readahead_count(rac);
1549 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1550
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001551 while ((page = readahead_page(rac))) {
Gao Xiang3883a792018-07-26 20:22:06 +08001552 set_page_private(page, (unsigned long)head);
1553 head = page;
1554 }
1555
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001556 while (head) {
Gao Xiang3883a792018-07-26 20:22:06 +08001557 struct page *page = head;
1558 int err;
1559
1560 /* traversal in reverse order */
1561 head = (void *)page_private(page);
1562
Gao Xiang1825c8d2020-12-09 20:37:17 +08001563 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xianga5876e22019-09-04 10:08:56 +08001564 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001565 erofs_err(inode->i_sb,
1566 "readahead error at page %lu @ nid %llu",
1567 page->index, EROFS_I(inode)->nid);
Gao Xiang3883a792018-07-26 20:22:06 +08001568 put_page(page);
1569 }
Gao Xiang38629292021-10-09 04:08:39 +08001570 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
Gao Xiang97e86a82019-07-31 23:57:47 +08001571 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001572
Gao Xiang38629292021-10-09 04:08:39 +08001573 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
Huang Jianan40452ff2021-12-06 22:35:52 +08001574 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
Gao Xiang09c54372022-01-02 12:00:17 +08001575 erofs_put_metabuf(&f.map.buf);
Gao Xiangeaa91722021-10-22 17:01:20 +08001576 erofs_release_pages(&pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001577}
1578
Gao Xiang0c638f72019-11-08 11:37:33 +08001579const struct address_space_operations z_erofs_aops = {
1580 .readpage = z_erofs_readpage,
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001581 .readahead = z_erofs_readahead,
Gao Xiang3883a792018-07-26 20:22:06 +08001582};