]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/erofs/zdata.c
proc: convert everything to "struct proc_ops"
[mirror_ubuntu-jammy-kernel.git] / fs / erofs / zdata.c
CommitLineData
29b24f6c 1// SPDX-License-Identifier: GPL-2.0-only
02827e17 2/*
02827e17
GX
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * http://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
02827e17 6 */
57b78c9f 7#include "zdata.h"
27481233 8#include "compress.h"
3883a79a
GX
9#include <linux/prefetch.h>
10
284db12c
CG
11#include <trace/events/erofs.h>
12
672e5476
GX
13/*
14 * a compressed_pages[] placeholder in order to avoid
15 * being filled with file pages for in-place decompression.
16 */
17#define PAGE_UNALLOCATED ((void *)0x5F0E4B1D)
18
97e86a85 19/* how to allocate cached pages for a pcluster */
92e6efd5
GX
20enum z_erofs_cache_alloctype {
21 DONTALLOC, /* don't allocate any cached pages */
22 DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */
23};
24
25/*
26 * tagged pointer with 1-bit tag for all compressed pages
27 * tag 0 - the page is just found with an extra page reference
28 */
29typedef tagptr1_t compressed_page_t;
30
31#define tag_compressed_page_justfound(page) \
32 tagptr_fold(compressed_page_t, page, 1)
33
3883a79a 34static struct workqueue_struct *z_erofs_workqueue __read_mostly;
97e86a85 35static struct kmem_cache *pcluster_cachep __read_mostly;
3883a79a
GX
36
37void z_erofs_exit_zip_subsystem(void)
38{
3883a79a 39 destroy_workqueue(z_erofs_workqueue);
97e86a85 40 kmem_cache_destroy(pcluster_cachep);
3883a79a
GX
41}
42
99634bf3 43static inline int z_erofs_init_workqueue(void)
3883a79a 44{
7dd68b14 45 const unsigned int onlinecpus = num_possible_cpus();
97e86a85 46 const unsigned int flags = WQ_UNBOUND | WQ_HIGHPRI | WQ_CPU_INTENSIVE;
3883a79a
GX
47
48 /*
97e86a85
GX
49 * no need to spawn too many threads, limiting threads could minimum
50 * scheduling overhead, perhaps per-CPU threads should be better?
3883a79a 51 */
97e86a85
GX
52 z_erofs_workqueue = alloc_workqueue("erofs_unzipd", flags,
53 onlinecpus + onlinecpus / 4);
42d40b4a 54 return z_erofs_workqueue ? 0 : -ENOMEM;
3883a79a
GX
55}
56
99634bf3 57static void z_erofs_pcluster_init_once(void *ptr)
48d4bf3b 58{
97e86a85
GX
59 struct z_erofs_pcluster *pcl = ptr;
60 struct z_erofs_collection *cl = z_erofs_primarycollection(pcl);
48d4bf3b
GX
61 unsigned int i;
62
97e86a85
GX
63 mutex_init(&cl->lock);
64 cl->nr_pages = 0;
65 cl->vcnt = 0;
48d4bf3b 66 for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i)
97e86a85 67 pcl->compressed_pages[i] = NULL;
48d4bf3b
GX
68}
69
99634bf3 70static void z_erofs_pcluster_init_always(struct z_erofs_pcluster *pcl)
48d4bf3b 71{
97e86a85 72 struct z_erofs_collection *cl = z_erofs_primarycollection(pcl);
48d4bf3b 73
97e86a85 74 atomic_set(&pcl->obj.refcount, 1);
48d4bf3b 75
97e86a85
GX
76 DBG_BUGON(cl->nr_pages);
77 DBG_BUGON(cl->vcnt);
48d4bf3b
GX
78}
79
0a0b7e62 80int __init z_erofs_init_zip_subsystem(void)
3883a79a 81{
97e86a85
GX
82 pcluster_cachep = kmem_cache_create("erofs_compress",
83 Z_EROFS_WORKGROUP_SIZE, 0,
99634bf3
GX
84 SLAB_RECLAIM_ACCOUNT,
85 z_erofs_pcluster_init_once);
97e86a85 86 if (pcluster_cachep) {
99634bf3 87 if (!z_erofs_init_workqueue())
3883a79a
GX
88 return 0;
89
97e86a85 90 kmem_cache_destroy(pcluster_cachep);
3883a79a
GX
91 }
92 return -ENOMEM;
93}
94
97e86a85
GX
95enum z_erofs_collectmode {
96 COLLECT_SECONDARY,
97 COLLECT_PRIMARY,
3883a79a 98 /*
97e86a85
GX
99 * The current collection was the tail of an exist chain, in addition
100 * that the previous processed chained collections are all decided to
101 * be hooked up to it.
102 * A new chain will be created for the remaining collections which are
103 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
104 * the next collection cannot reuse the whole page safely in
105 * the following scenario:
a112152f
GX
106 * ________________________________________________________________
107 * | tail (partial) page | head (partial) page |
97e86a85 108 * | (belongs to the next cl) | (belongs to the current cl) |
a112152f
GX
109 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
110 */
97e86a85
GX
111 COLLECT_PRIMARY_HOOKED,
112 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
a112152f 113 /*
97e86a85
GX
114 * The current collection has been linked with the owned chain, and
115 * could also be linked with the remaining collections, which means
116 * if the processing page is the tail page of the collection, thus
117 * the current collection can safely use the whole page (since
118 * the previous collection is under control) for in-place I/O, as
119 * illustrated below:
a112152f 120 * ________________________________________________________________
97e86a85
GX
121 * | tail (partial) page | head (partial) page |
122 * | (of the current cl) | (of the previous collection) |
123 * | PRIMARY_FOLLOWED or | |
124 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
a112152f 125 *
97e86a85 126 * [ (*) the above page can be used as inplace I/O. ]
3883a79a 127 */
97e86a85 128 COLLECT_PRIMARY_FOLLOWED,
3883a79a
GX
129};
130
97e86a85 131struct z_erofs_collector {
3883a79a
GX
132 struct z_erofs_pagevec_ctor vector;
133
bfc4ccb1 134 struct z_erofs_pcluster *pcl, *tailpcl;
97e86a85
GX
135 struct z_erofs_collection *cl;
136 struct page **compressedpages;
137 z_erofs_next_pcluster_t owned_head;
138
139 enum z_erofs_collectmode mode;
3883a79a
GX
140};
141
97e86a85
GX
142struct z_erofs_decompress_frontend {
143 struct inode *const inode;
144
145 struct z_erofs_collector clt;
146 struct erofs_map_blocks map;
147
148 /* used for applying cache strategy on the fly */
149 bool backmost;
150 erofs_off_t headoffset;
151};
152
153#define COLLECTOR_INIT() { \
154 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
155 .mode = COLLECT_PRIMARY_FOLLOWED }
156
157#define DECOMPRESS_FRONTEND_INIT(__i) { \
158 .inode = __i, .clt = COLLECTOR_INIT(), \
159 .backmost = true, }
160
161static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
162static DEFINE_MUTEX(z_pagemap_global_lock);
3883a79a 163
97e86a85 164static void preload_compressed_pages(struct z_erofs_collector *clt,
92e6efd5 165 struct address_space *mc,
92e6efd5 166 enum z_erofs_cache_alloctype type,
97e86a85 167 struct list_head *pagepool)
105d4ad8 168{
97e86a85
GX
169 const struct z_erofs_pcluster *pcl = clt->pcl;
170 const unsigned int clusterpages = BIT(pcl->clusterbits);
171 struct page **pages = clt->compressedpages;
172 pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
92e6efd5 173 bool standalone = true;
92e6efd5 174
97e86a85 175 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
92e6efd5
GX
176 return;
177
97e86a85 178 for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
92e6efd5
GX
179 struct page *page;
180 compressed_page_t t;
181
182 /* the compressed page was loaded before */
97e86a85 183 if (READ_ONCE(*pages))
105d4ad8
GX
184 continue;
185
97e86a85 186 page = find_get_page(mc, index);
92e6efd5
GX
187
188 if (page) {
189 t = tag_compressed_page_justfound(page);
190 } else if (type == DELAYEDALLOC) {
191 t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
192 } else { /* DONTALLOC */
193 if (standalone)
97e86a85 194 clt->compressedpages = pages;
92e6efd5
GX
195 standalone = false;
196 continue;
105d4ad8
GX
197 }
198
97e86a85 199 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
105d4ad8
GX
200 continue;
201
92e6efd5
GX
202 if (page)
203 put_page(page);
105d4ad8 204 }
92e6efd5 205
97e86a85
GX
206 if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
207 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
105d4ad8
GX
208}
209
210/* called by erofs_shrinker to get rid of all compressed_pages */
47e541a1 211int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
97e86a85 212 struct erofs_workgroup *grp)
105d4ad8 213{
97e86a85
GX
214 struct z_erofs_pcluster *const pcl =
215 container_of(grp, struct z_erofs_pcluster, obj);
c1448fa8 216 struct address_space *const mapping = MNGD_MAPPING(sbi);
97e86a85 217 const unsigned int clusterpages = BIT(pcl->clusterbits);
105d4ad8
GX
218 int i;
219
220 /*
221 * refcount of workgroup is now freezed as 1,
222 * therefore no need to worry about available decompression users.
223 */
224 for (i = 0; i < clusterpages; ++i) {
97e86a85 225 struct page *page = pcl->compressed_pages[i];
105d4ad8 226
97e86a85 227 if (!page)
105d4ad8
GX
228 continue;
229
230 /* block other users from reclaiming or migrating the page */
231 if (!trylock_page(page))
232 return -EBUSY;
233
8d8a09b0 234 if (page->mapping != mapping)
97e86a85 235 continue;
105d4ad8 236
97e86a85
GX
237 /* barrier is implied in the following 'unlock_page' */
238 WRITE_ONCE(pcl->compressed_pages[i], NULL);
105d4ad8
GX
239 set_page_private(page, 0);
240 ClearPagePrivate(page);
241
242 unlock_page(page);
243 put_page(page);
244 }
245 return 0;
246}
247
47e541a1
GX
248int erofs_try_to_free_cached_page(struct address_space *mapping,
249 struct page *page)
105d4ad8 250{
97e86a85
GX
251 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
252 const unsigned int clusterpages = BIT(pcl->clusterbits);
105d4ad8
GX
253 int ret = 0; /* 0 - busy */
254
97e86a85 255 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
105d4ad8
GX
256 unsigned int i;
257
258 for (i = 0; i < clusterpages; ++i) {
97e86a85
GX
259 if (pcl->compressed_pages[i] == page) {
260 WRITE_ONCE(pcl->compressed_pages[i], NULL);
105d4ad8
GX
261 ret = 1;
262 break;
263 }
264 }
97e86a85 265 erofs_workgroup_unfreeze(&pcl->obj, 1);
105d4ad8 266
047d4abc
GX
267 if (ret) {
268 ClearPagePrivate(page);
269 put_page(page);
270 }
105d4ad8
GX
271 }
272 return ret;
273}
105d4ad8 274
3883a79a 275/* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
99634bf3
GX
276static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
277 struct page *page)
3883a79a 278{
97e86a85
GX
279 struct z_erofs_pcluster *const pcl = clt->pcl;
280 const unsigned int clusterpages = BIT(pcl->clusterbits);
281
282 while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
283 if (!cmpxchg(clt->compressedpages++, NULL, page))
3883a79a
GX
284 return true;
285 }
3883a79a
GX
286 return false;
287}
288
97e86a85
GX
289/* callers must be with collection lock held */
290static int z_erofs_attach_page(struct z_erofs_collector *clt,
291 struct page *page,
292 enum z_erofs_page_type type)
3883a79a
GX
293{
294 int ret;
295 bool occupied;
296
97e86a85
GX
297 /* give priority for inplaceio */
298 if (clt->mode >= COLLECT_PRIMARY &&
447a3621 299 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
99634bf3 300 z_erofs_try_inplace_io(clt, page))
3883a79a
GX
301 return 0;
302
97e86a85 303 ret = z_erofs_pagevec_enqueue(&clt->vector,
046d64e1 304 page, type, &occupied);
97e86a85 305 clt->cl->vcnt += (unsigned int)ret;
3883a79a
GX
306
307 return ret ? 0 : -EAGAIN;
308}
309
97e86a85
GX
310static enum z_erofs_collectmode
311try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
312 z_erofs_next_pcluster_t *owned_head)
3883a79a 313{
97e86a85 314 /* let's claim these following types of pclusters */
3883a79a 315retry:
97e86a85
GX
316 if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
317 /* type 1, nil pcluster */
318 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
319 *owned_head) != Z_EROFS_PCLUSTER_NIL)
3883a79a
GX
320 goto retry;
321
97e86a85 322 *owned_head = &pcl->next;
a112152f 323 /* lucky, I am the followee :) */
97e86a85
GX
324 return COLLECT_PRIMARY_FOLLOWED;
325 } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
3883a79a
GX
326 /*
327 * type 2, link to the end of a existing open chain,
328 * be careful that its submission itself is governed
329 * by the original owned chain.
330 */
97e86a85
GX
331 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
332 *owned_head) != Z_EROFS_PCLUSTER_TAIL)
3883a79a 333 goto retry;
97e86a85
GX
334 *owned_head = Z_EROFS_PCLUSTER_TAIL;
335 return COLLECT_PRIMARY_HOOKED;
a112152f 336 }
97e86a85 337 return COLLECT_PRIMARY; /* :( better luck next time */
3883a79a
GX
338}
339
9e579fc1
GX
340static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
341 struct inode *inode,
342 struct erofs_map_blocks *map)
3883a79a 343{
97e86a85
GX
344 struct erofs_workgroup *grp;
345 struct z_erofs_pcluster *pcl;
346 struct z_erofs_collection *cl;
347 unsigned int length;
97e86a85 348
997626d8 349 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
97e86a85 350 if (!grp)
9e579fc1 351 return -ENOENT;
3883a79a 352
97e86a85 353 pcl = container_of(grp, struct z_erofs_pcluster, obj);
bfc4ccb1
GX
354 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
355 DBG_BUGON(1);
356 erofs_workgroup_put(grp);
9e579fc1 357 return -EFSCORRUPTED;
bfc4ccb1 358 }
97e86a85
GX
359
360 cl = z_erofs_primarycollection(pcl);
8d8a09b0 361 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
97e86a85 362 DBG_BUGON(1);
138e1a09 363 erofs_workgroup_put(grp);
9e579fc1 364 return -EFSCORRUPTED;
97e86a85 365 }
3883a79a 366
97e86a85
GX
367 length = READ_ONCE(pcl->length);
368 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
369 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
370 DBG_BUGON(1);
138e1a09 371 erofs_workgroup_put(grp);
9e579fc1 372 return -EFSCORRUPTED;
97e86a85
GX
373 }
374 } else {
375 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
3883a79a 376
97e86a85
GX
377 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
378 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
3883a79a 379
97e86a85
GX
380 while (llen > length &&
381 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
382 cpu_relax();
383 length = READ_ONCE(pcl->length);
384 }
385 }
386 mutex_lock(&cl->lock);
bfc4ccb1
GX
387 /* used to check tail merging loop due to corrupted images */
388 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
389 clt->tailpcl = pcl;
97e86a85 390 clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
bfc4ccb1
GX
391 /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
392 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
393 clt->tailpcl = NULL;
97e86a85
GX
394 clt->pcl = pcl;
395 clt->cl = cl;
9e579fc1 396 return 0;
3883a79a
GX
397}
398
9e579fc1
GX
399static int z_erofs_register_collection(struct z_erofs_collector *clt,
400 struct inode *inode,
401 struct erofs_map_blocks *map)
3883a79a 402{
97e86a85
GX
403 struct z_erofs_pcluster *pcl;
404 struct z_erofs_collection *cl;
405 int err;
e5e3abba 406
3883a79a 407 /* no available workgroup, let's allocate one */
97e86a85 408 pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
8d8a09b0 409 if (!pcl)
9e579fc1 410 return -ENOMEM;
3883a79a 411
99634bf3 412 z_erofs_pcluster_init_always(pcl);
97e86a85 413 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
3883a79a 414
97e86a85
GX
415 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
416 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
417 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
3883a79a 418
97e86a85
GX
419 if (map->m_flags & EROFS_MAP_ZIPPED)
420 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
421 else
422 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
423
a5876e24 424 pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
97e86a85 425 pcl->clusterbits -= PAGE_SHIFT;
b6a76183 426
97e86a85
GX
427 /* new pclusters should be claimed as type 1, primary and followed */
428 pcl->next = clt->owned_head;
429 clt->mode = COLLECT_PRIMARY_FOLLOWED;
3883a79a 430
97e86a85
GX
431 cl = z_erofs_primarycollection(pcl);
432 cl->pageofs = map->m_la & ~PAGE_MASK;
3883a79a 433
23edf3ab
GX
434 /*
435 * lock all primary followed works before visible to others
97e86a85 436 * and mutex_trylock *never* fails for a new pcluster.
23edf3ab 437 */
97e86a85 438 mutex_trylock(&cl->lock);
23edf3ab 439
e5e9a432 440 err = erofs_register_workgroup(inode->i_sb, &pcl->obj);
97e86a85
GX
441 if (err) {
442 mutex_unlock(&cl->lock);
443 kmem_cache_free(pcluster_cachep, pcl);
9e579fc1 444 return -EAGAIN;
3883a79a 445 }
bfc4ccb1
GX
446 /* used to check tail merging loop due to corrupted images */
447 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
448 clt->tailpcl = pcl;
97e86a85
GX
449 clt->owned_head = &pcl->next;
450 clt->pcl = pcl;
451 clt->cl = cl;
9e579fc1 452 return 0;
3883a79a
GX
453}
454
97e86a85
GX
455static int z_erofs_collector_begin(struct z_erofs_collector *clt,
456 struct inode *inode,
457 struct erofs_map_blocks *map)
458{
9e579fc1 459 int ret;
a112152f 460
97e86a85 461 DBG_BUGON(clt->cl);
3883a79a 462
97e86a85
GX
463 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
464 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
465 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
3883a79a 466
97e86a85
GX
467 if (!PAGE_ALIGNED(map->m_pa)) {
468 DBG_BUGON(1);
469 return -EINVAL;
3883a79a
GX
470 }
471
97e86a85 472repeat:
9e579fc1
GX
473 ret = z_erofs_lookup_collection(clt, inode, map);
474 if (ret == -ENOENT) {
475 ret = z_erofs_register_collection(clt, inode, map);
3883a79a 476
9e579fc1
GX
477 /* someone registered at the same time, give another try */
478 if (ret == -EAGAIN) {
479 cond_resched();
97e86a85 480 goto repeat;
9e579fc1 481 }
3883a79a
GX
482 }
483
9e579fc1
GX
484 if (ret)
485 return ret;
97e86a85
GX
486
487 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
9e579fc1 488 clt->cl->pagevec, clt->cl->vcnt);
97e86a85
GX
489
490 clt->compressedpages = clt->pcl->compressed_pages;
491 if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
492 clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
3883a79a
GX
493 return 0;
494}
495
496/*
97e86a85
GX
497 * keep in mind that no referenced pclusters will be freed
498 * only after a RCU grace period.
3883a79a
GX
499 */
500static void z_erofs_rcu_callback(struct rcu_head *head)
501{
97e86a85
GX
502 struct z_erofs_collection *const cl =
503 container_of(head, struct z_erofs_collection, rcu);
3883a79a 504
97e86a85
GX
505 kmem_cache_free(pcluster_cachep,
506 container_of(cl, struct z_erofs_pcluster,
507 primary_collection));
3883a79a
GX
508}
509
510void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
511{
97e86a85
GX
512 struct z_erofs_pcluster *const pcl =
513 container_of(grp, struct z_erofs_pcluster, obj);
514 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
3883a79a 515
97e86a85 516 call_rcu(&cl->rcu, z_erofs_rcu_callback);
3883a79a
GX
517}
518
97e86a85 519static void z_erofs_collection_put(struct z_erofs_collection *cl)
3883a79a 520{
97e86a85
GX
521 struct z_erofs_pcluster *const pcl =
522 container_of(cl, struct z_erofs_pcluster, primary_collection);
3883a79a 523
97e86a85 524 erofs_workgroup_put(&pcl->obj);
3883a79a
GX
525}
526
97e86a85 527static bool z_erofs_collector_end(struct z_erofs_collector *clt)
3883a79a 528{
97e86a85 529 struct z_erofs_collection *cl = clt->cl;
3883a79a 530
97e86a85 531 if (!cl)
3883a79a
GX
532 return false;
533
97e86a85
GX
534 z_erofs_pagevec_ctor_exit(&clt->vector, false);
535 mutex_unlock(&cl->lock);
3883a79a
GX
536
537 /*
97e86a85
GX
538 * if all pending pages are added, don't hold its reference
539 * any longer if the pcluster isn't hosted by ourselves.
3883a79a 540 */
97e86a85
GX
541 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
542 z_erofs_collection_put(cl);
3883a79a 543
97e86a85 544 clt->cl = NULL;
3883a79a
GX
545 return true;
546}
547
97e86a85 548static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
4279f3f9 549 unsigned int cachestrategy,
97e86a85 550 erofs_off_t la)
92e6efd5 551{
4279f3f9
GX
552 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
553 return false;
554
92e6efd5
GX
555 if (fe->backmost)
556 return true;
557
4279f3f9
GX
558 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
559 la < fe->headoffset;
92e6efd5 560}
92e6efd5 561
97e86a85 562static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
3883a79a 563 struct page *page,
97e86a85 564 struct list_head *pagepool)
3883a79a 565{
97e86a85 566 struct inode *const inode = fe->inode;
bda17a45 567 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
3b423417 568 struct erofs_map_blocks *const map = &fe->map;
97e86a85 569 struct z_erofs_collector *const clt = &fe->clt;
3883a79a 570 const loff_t offset = page_offset(page);
dc76ea8c 571 bool tight = true;
3883a79a 572
92e6efd5 573 enum z_erofs_cache_alloctype cache_strategy;
3883a79a 574 enum z_erofs_page_type page_type;
7dd68b14 575 unsigned int cur, end, spiltted, index;
1e05ff36 576 int err = 0;
3883a79a
GX
577
578 /* register locked file pages as online pages in pack */
579 z_erofs_onlinepage_init(page);
580
581 spiltted = 0;
582 end = PAGE_SIZE;
583repeat:
584 cur = end - 1;
585
586 /* lucky, within the range of the current map_blocks */
587 if (offset + cur >= map->m_la &&
447a3621 588 offset + cur < map->m_la + map->m_llen) {
97e86a85
GX
589 /* didn't get a valid collection previously (very rare) */
590 if (!clt->cl)
1e5ceeab 591 goto restart_now;
3883a79a 592 goto hitted;
1e5ceeab 593 }
3883a79a
GX
594
595 /* go ahead the next map_blocks */
4f761fa2 596 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
3883a79a 597
97e86a85 598 if (z_erofs_collector_end(clt))
f0c519fc 599 fe->backmost = false;
3883a79a
GX
600
601 map->m_la = offset + cur;
602 map->m_llen = 0;
97e86a85 603 err = z_erofs_map_blocks_iter(inode, map, 0);
8d8a09b0 604 if (err)
3883a79a
GX
605 goto err_out;
606
1e5ceeab 607restart_now:
8d8a09b0 608 if (!(map->m_flags & EROFS_MAP_MAPPED))
3883a79a
GX
609 goto hitted;
610
97e86a85 611 err = z_erofs_collector_begin(clt, inode, map);
8d8a09b0 612 if (err)
3883a79a
GX
613 goto err_out;
614
92e6efd5 615 /* preload all compressed pages (maybe downgrade role if necessary) */
4279f3f9 616 if (should_alloc_managed_pages(fe, sbi->cache_strategy, map->m_la))
92e6efd5
GX
617 cache_strategy = DELAYEDALLOC;
618 else
619 cache_strategy = DONTALLOC;
620
97e86a85
GX
621 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
622 cache_strategy, pagepool);
105d4ad8 623
3883a79a 624hitted:
dc76ea8c
GX
625 /*
626 * Ensure the current partial page belongs to this submit chain rather
627 * than other concurrent submit chains or the noio(bypass) chain since
628 * those chains are handled asynchronously thus the page cannot be used
629 * for inplace I/O or pagevec (should be processed in strict order.)
630 */
631 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
632 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
633
7dd68b14 634 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
8d8a09b0 635 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
3883a79a
GX
636 zero_user_segment(page, cur, end);
637 goto next_part;
638 }
639
640 /* let's derive page type */
641 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
642 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
643 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
644 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
645
a112152f 646 if (cur)
97e86a85 647 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
a112152f 648
3883a79a 649retry:
97e86a85 650 err = z_erofs_attach_page(clt, page, page_type);
3883a79a
GX
651 /* should allocate an additional staging page for pagevec */
652 if (err == -EAGAIN) {
653 struct page *const newpage =
5ddcee1f 654 erofs_allocpage(pagepool, GFP_NOFS | __GFP_NOFAIL);
3883a79a 655
5ddcee1f 656 newpage->mapping = Z_EROFS_MAPPING_STAGING;
97e86a85
GX
657 err = z_erofs_attach_page(clt, newpage,
658 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
8d8a09b0 659 if (!err)
3883a79a
GX
660 goto retry;
661 }
662
8d8a09b0 663 if (err)
3883a79a
GX
664 goto err_out;
665
97e86a85 666 index = page->index - (map->m_la >> PAGE_SHIFT);
3883a79a 667
3883a79a 668 z_erofs_onlinepage_fixup(page, index, true);
3883a79a 669
1e05ff36
GX
670 /* bump up the number of spiltted parts of a page */
671 ++spiltted;
672 /* also update nr_pages */
97e86a85 673 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
3883a79a
GX
674next_part:
675 /* can be used for verification */
676 map->m_llen = offset + cur - map->m_la;
677
2bc75964
678 end = cur;
679 if (end > 0)
3883a79a
GX
680 goto repeat;
681
1e05ff36 682out:
3883a79a
GX
683 z_erofs_onlinepage_endio(page);
684
4f761fa2
GX
685 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
686 __func__, page, spiltted, map->m_llen);
1e05ff36 687 return err;
3883a79a 688
1e05ff36 689 /* if some error occurred while processing this page */
3883a79a 690err_out:
1e05ff36
GX
691 SetPageError(page);
692 goto out;
3883a79a
GX
693}
694
a4b1fab1
GX
695static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
696 bool sync, int bios)
3883a79a 697{
a4b1fab1
GX
698 /* wake up the caller thread for sync decompression */
699 if (sync) {
848bd9ac
GX
700 unsigned long flags;
701
702 spin_lock_irqsave(&io->u.wait.lock, flags);
703 if (!atomic_add_return(bios, &io->pending_bios))
704 wake_up_locked(&io->u.wait);
705 spin_unlock_irqrestore(&io->u.wait.lock, flags);
3883a79a 706 return;
848bd9ac 707 }
3883a79a 708
848bd9ac 709 if (!atomic_add_return(bios, &io->pending_bios))
3883a79a 710 queue_work(z_erofs_workqueue, &io->u.work);
3883a79a
GX
711}
712
0c638f70 713static void z_erofs_decompressqueue_endio(struct bio *bio)
3883a79a 714{
a4b1fab1
GX
715 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
716 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
14a56ec6 717 blk_status_t err = bio->bi_status;
3883a79a 718 struct bio_vec *bvec;
6dc4f100 719 struct bvec_iter_all iter_all;
3883a79a 720
2b070cfe 721 bio_for_each_segment_all(bvec, bio, iter_all) {
3883a79a
GX
722 struct page *page = bvec->bv_page;
723
724 DBG_BUGON(PageUptodate(page));
70b17991 725 DBG_BUGON(!page->mapping);
3883a79a 726
8d8a09b0 727 if (err)
3883a79a 728 SetPageError(page);
105d4ad8 729
a4b1fab1
GX
730 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
731 if (!err)
732 SetPageUptodate(page);
105d4ad8 733 unlock_page(page);
a4b1fab1 734 }
3883a79a 735 }
a4b1fab1 736 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
3883a79a
GX
737 bio_put(bio);
738}
739
97e86a85
GX
740static int z_erofs_decompress_pcluster(struct super_block *sb,
741 struct z_erofs_pcluster *pcl,
742 struct list_head *pagepool)
3883a79a
GX
743{
744 struct erofs_sb_info *const sbi = EROFS_SB(sb);
97e86a85 745 const unsigned int clusterpages = BIT(pcl->clusterbits);
3883a79a 746 struct z_erofs_pagevec_ctor ctor;
97e86a85
GX
747 unsigned int i, outputsize, llen, nr_pages;
748 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
3883a79a 749 struct page **pages, **compressed_pages, *page;
3883a79a
GX
750
751 enum z_erofs_page_type page_type;
b6a76183 752 bool overlapped, partial;
97e86a85 753 struct z_erofs_collection *cl;
3883a79a
GX
754 int err;
755
756 might_sleep();
97e86a85
GX
757 cl = z_erofs_primarycollection(pcl);
758 DBG_BUGON(!READ_ONCE(cl->nr_pages));
3883a79a 759
97e86a85
GX
760 mutex_lock(&cl->lock);
761 nr_pages = cl->nr_pages;
3883a79a 762
8d8a09b0 763 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
3883a79a 764 pages = pages_onstack;
97e86a85
GX
765 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
766 mutex_trylock(&z_pagemap_global_lock)) {
3883a79a 767 pages = z_pagemap_global;
97e86a85 768 } else {
441dfcc8
CY
769 gfp_t gfp_flags = GFP_KERNEL;
770
97e86a85 771 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
441dfcc8
CY
772 gfp_flags |= __GFP_NOFAIL;
773
447a3621 774 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
441dfcc8 775 gfp_flags);
3883a79a
GX
776
777 /* fallback to global pagemap for the lowmem scenario */
8d8a09b0 778 if (!pages) {
441dfcc8
CY
779 mutex_lock(&z_pagemap_global_lock);
780 pages = z_pagemap_global;
3883a79a
GX
781 }
782 }
783
784 for (i = 0; i < nr_pages; ++i)
785 pages[i] = NULL;
786
e12a0ce2 787 err = 0;
fa61a33f 788 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
97e86a85 789 cl->pagevec, 0);
3883a79a 790
97e86a85 791 for (i = 0; i < cl->vcnt; ++i) {
7dd68b14 792 unsigned int pagenr;
3883a79a 793
046d64e1 794 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
3883a79a
GX
795
796 /* all pages in pagevec ought to be valid */
42d40b4a
CS
797 DBG_BUGON(!page);
798 DBG_BUGON(!page->mapping);
3883a79a 799
97e86a85 800 if (z_erofs_put_stagingpage(pagepool, page))
3883a79a
GX
801 continue;
802
803 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
804 pagenr = 0;
805 else
806 pagenr = z_erofs_onlinepage_index(page);
807
70b17991 808 DBG_BUGON(pagenr >= nr_pages);
e5e3abba 809
e12a0ce2
GX
810 /*
811 * currently EROFS doesn't support multiref(dedup),
812 * so here erroring out one multiref page.
813 */
8d8a09b0 814 if (pages[pagenr]) {
e12a0ce2
GX
815 DBG_BUGON(1);
816 SetPageError(pages[pagenr]);
817 z_erofs_onlinepage_endio(pages[pagenr]);
818 err = -EFSCORRUPTED;
819 }
3883a79a
GX
820 pages[pagenr] = page;
821 }
3883a79a
GX
822 z_erofs_pagevec_ctor_exit(&ctor, true);
823
824 overlapped = false;
97e86a85 825 compressed_pages = pcl->compressed_pages;
3883a79a
GX
826
827 for (i = 0; i < clusterpages; ++i) {
7dd68b14 828 unsigned int pagenr;
3883a79a
GX
829
830 page = compressed_pages[i];
831
832 /* all compressed pages ought to be valid */
42d40b4a
CS
833 DBG_BUGON(!page);
834 DBG_BUGON(!page->mapping);
3883a79a 835
27481233 836 if (!z_erofs_page_is_staging(page)) {
d61fbb6b 837 if (erofs_page_is_managed(sbi, page)) {
8d8a09b0 838 if (!PageUptodate(page))
11152496
GX
839 err = -EIO;
840 continue;
841 }
3883a79a 842
11152496
GX
843 /*
844 * only if non-head page can be selected
845 * for inplace decompression
846 */
847 pagenr = z_erofs_onlinepage_index(page);
3883a79a 848
11152496 849 DBG_BUGON(pagenr >= nr_pages);
8d8a09b0 850 if (pages[pagenr]) {
e12a0ce2
GX
851 DBG_BUGON(1);
852 SetPageError(pages[pagenr]);
853 z_erofs_onlinepage_endio(pages[pagenr]);
854 err = -EFSCORRUPTED;
855 }
11152496
GX
856 pages[pagenr] = page;
857
858 overlapped = true;
859 }
3883a79a 860
11152496 861 /* PG_error needs checking for inplaced and staging pages */
8d8a09b0 862 if (PageError(page)) {
11152496
GX
863 DBG_BUGON(PageUptodate(page));
864 err = -EIO;
865 }
3883a79a
GX
866 }
867
8d8a09b0 868 if (err)
11152496
GX
869 goto out;
870
97e86a85
GX
871 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
872 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
873 outputsize = llen;
874 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
b6a76183 875 } else {
97e86a85 876 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
b6a76183
GX
877 partial = true;
878 }
3883a79a 879
88aaf5a7
GX
880 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
881 .sb = sb,
882 .in = compressed_pages,
883 .out = pages,
97e86a85 884 .pageofs_out = cl->pageofs,
88aaf5a7
GX
885 .inputsize = PAGE_SIZE,
886 .outputsize = outputsize,
97e86a85 887 .alg = pcl->algorithmformat,
88aaf5a7 888 .inplace_io = overlapped,
b6a76183 889 .partial_decoding = partial
97e86a85 890 }, pagepool);
3883a79a
GX
891
892out:
af692e11
GX
893 /* must handle all compressed pages before endding pages */
894 for (i = 0; i < clusterpages; ++i) {
895 page = compressed_pages[i];
896
d61fbb6b 897 if (erofs_page_is_managed(sbi, page))
af692e11 898 continue;
d61fbb6b 899
af692e11 900 /* recycle all individual staging pages */
97e86a85 901 (void)z_erofs_put_stagingpage(pagepool, page);
af692e11
GX
902
903 WRITE_ONCE(compressed_pages[i], NULL);
904 }
905
3883a79a
GX
906 for (i = 0; i < nr_pages; ++i) {
907 page = pages[i];
af692e11
GX
908 if (!page)
909 continue;
910
42d40b4a 911 DBG_BUGON(!page->mapping);
3883a79a
GX
912
913 /* recycle all individual staging pages */
97e86a85 914 if (z_erofs_put_stagingpage(pagepool, page))
3883a79a
GX
915 continue;
916
8d8a09b0 917 if (err < 0)
3883a79a
GX
918 SetPageError(page);
919
920 z_erofs_onlinepage_endio(page);
921 }
922
3883a79a
GX
923 if (pages == z_pagemap_global)
924 mutex_unlock(&z_pagemap_global_lock);
8d8a09b0 925 else if (pages != pages_onstack)
3883a79a
GX
926 kvfree(pages);
927
97e86a85
GX
928 cl->nr_pages = 0;
929 cl->vcnt = 0;
3883a79a 930
97e86a85
GX
931 /* all cl locks MUST be taken before the following line */
932 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
3883a79a 933
97e86a85
GX
934 /* all cl locks SHOULD be released right now */
935 mutex_unlock(&cl->lock);
3883a79a 936
97e86a85 937 z_erofs_collection_put(cl);
3883a79a
GX
938 return err;
939}
940
0c638f70
GX
941static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
942 struct list_head *pagepool)
3883a79a 943{
97e86a85 944 z_erofs_next_pcluster_t owned = io->head;
3883a79a 945
97e86a85
GX
946 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
947 struct z_erofs_pcluster *pcl;
3883a79a
GX
948
949 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
97e86a85 950 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
3883a79a
GX
951
952 /* no possible that 'owned' equals NULL */
97e86a85 953 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
3883a79a 954
97e86a85
GX
955 pcl = container_of(owned, struct z_erofs_pcluster, next);
956 owned = READ_ONCE(pcl->next);
3883a79a 957
a4b1fab1 958 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
3978c8e3 959 }
3883a79a
GX
960}
961
0c638f70 962static void z_erofs_decompressqueue_work(struct work_struct *work)
3883a79a 963{
a4b1fab1
GX
964 struct z_erofs_decompressqueue *bgq =
965 container_of(work, struct z_erofs_decompressqueue, u.work);
97e86a85 966 LIST_HEAD(pagepool);
3883a79a 967
a4b1fab1 968 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
0c638f70 969 z_erofs_decompress_queue(bgq, &pagepool);
3883a79a 970
97e86a85 971 put_pages_list(&pagepool);
a4b1fab1 972 kvfree(bgq);
3883a79a
GX
973}
974
97e86a85
GX
975static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
976 unsigned int nr,
977 struct list_head *pagepool,
978 struct address_space *mc,
979 gfp_t gfp)
9248fce7 980{
97e86a85 981 const pgoff_t index = pcl->obj.index;
9248fce7
GX
982 bool tocache = false;
983
984 struct address_space *mapping;
985 struct page *oldpage, *page;
986
92e6efd5
GX
987 compressed_page_t t;
988 int justfound;
989
9248fce7 990repeat:
97e86a85 991 page = READ_ONCE(pcl->compressed_pages[nr]);
9248fce7
GX
992 oldpage = page;
993
994 if (!page)
995 goto out_allocpage;
996
997 /*
998 * the cached page has not been allocated and
999 * an placeholder is out there, prepare it now.
1000 */
bda17a45 1001 if (page == PAGE_UNALLOCATED) {
9248fce7
GX
1002 tocache = true;
1003 goto out_allocpage;
1004 }
1005
92e6efd5
GX
1006 /* process the target tagged pointer */
1007 t = tagptr_init(compressed_page_t, page);
1008 justfound = tagptr_unfold_tags(t);
1009 page = tagptr_unfold_ptr(t);
1010
9248fce7
GX
1011 mapping = READ_ONCE(page->mapping);
1012
9248fce7
GX
1013 /*
1014 * unmanaged (file) pages are all locked solidly,
1015 * therefore it is impossible for `mapping' to be NULL.
1016 */
1017 if (mapping && mapping != mc)
1018 /* ought to be unmanaged pages */
1019 goto out;
1020
1021 lock_page(page);
1022
92e6efd5
GX
1023 /* only true if page reclaim goes wrong, should never happen */
1024 DBG_BUGON(justfound && PagePrivate(page));
1025
9248fce7
GX
1026 /* the page is still in manage cache */
1027 if (page->mapping == mc) {
97e86a85 1028 WRITE_ONCE(pcl->compressed_pages[nr], page);
9248fce7 1029
11152496 1030 ClearPageError(page);
9248fce7 1031 if (!PagePrivate(page)) {
92e6efd5
GX
1032 /*
1033 * impossible to be !PagePrivate(page) for
1034 * the current restriction as well if
1035 * the page is already in compressed_pages[].
1036 */
1037 DBG_BUGON(!justfound);
1038
1039 justfound = 0;
97e86a85 1040 set_page_private(page, (unsigned long)pcl);
9248fce7
GX
1041 SetPagePrivate(page);
1042 }
1043
1044 /* no need to submit io if it is already up-to-date */
1045 if (PageUptodate(page)) {
1046 unlock_page(page);
1047 page = NULL;
1048 }
1049 goto out;
1050 }
1051
1052 /*
1053 * the managed page has been truncated, it's unsafe to
1054 * reuse this one, let's allocate a new cache-managed page.
1055 */
1056 DBG_BUGON(page->mapping);
92e6efd5 1057 DBG_BUGON(!justfound);
9248fce7
GX
1058
1059 tocache = true;
1060 unlock_page(page);
1061 put_page(page);
1062out_allocpage:
5ddcee1f
GX
1063 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1064 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1065 /* non-LRU / non-movable temporary page is needed */
9248fce7 1066 page->mapping = Z_EROFS_MAPPING_STAGING;
5ddcee1f 1067 tocache = false;
9248fce7
GX
1068 }
1069
5ddcee1f
GX
1070 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1071 if (tocache) {
1072 /* since it added to managed cache successfully */
1073 unlock_page(page);
1074 put_page(page);
1075 } else {
1076 list_add(&page->lru, pagepool);
1077 }
1078 cond_resched();
1079 goto repeat;
1080 }
97e86a85 1081 set_page_private(page, (unsigned long)pcl);
9248fce7
GX
1082 SetPagePrivate(page);
1083out: /* the only exit (for tracing and debugging) */
1084 return page;
1085}
1086
a4b1fab1
GX
1087static struct z_erofs_decompressqueue *
1088jobqueue_init(struct super_block *sb,
1089 struct z_erofs_decompressqueue *fgq, bool *fg)
3883a79a 1090{
a4b1fab1 1091 struct z_erofs_decompressqueue *q;
3883a79a 1092
a4b1fab1
GX
1093 if (fg && !*fg) {
1094 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1095 if (!q) {
1096 *fg = true;
1097 goto fg_out;
1098 }
0c638f70 1099 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
a4b1fab1
GX
1100 } else {
1101fg_out:
1102 q = fgq;
1103 init_waitqueue_head(&fgq->u.wait);
1104 atomic_set(&fgq->pending_bios, 0);
3883a79a 1105 }
a4b1fab1
GX
1106 q->sb = sb;
1107 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1108 return q;
3883a79a
GX
1109}
1110
97e86a85 1111/* define decompression jobqueue types */
7146a4f0 1112enum {
7146a4f0 1113 JQ_BYPASS,
7146a4f0
GX
1114 JQ_SUBMIT,
1115 NR_JOBQUEUES,
1116};
1117
1118static void *jobqueueset_init(struct super_block *sb,
a4b1fab1
GX
1119 struct z_erofs_decompressqueue *q[],
1120 struct z_erofs_decompressqueue *fgq, bool *fg)
7146a4f0 1121{
7146a4f0
GX
1122 /*
1123 * if managed cache is enabled, bypass jobqueue is needed,
97e86a85 1124 * no need to read from device for all pclusters in this queue.
7146a4f0 1125 */
a4b1fab1
GX
1126 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1127 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
7146a4f0 1128
a4b1fab1 1129 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
7146a4f0
GX
1130}
1131
97e86a85
GX
1132static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1133 z_erofs_next_pcluster_t qtail[],
1134 z_erofs_next_pcluster_t owned_head)
7146a4f0 1135{
97e86a85
GX
1136 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1137 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
7146a4f0 1138
97e86a85
GX
1139 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1140 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1141 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
7146a4f0 1142
97e86a85 1143 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
7146a4f0
GX
1144
1145 WRITE_ONCE(*submit_qtail, owned_head);
97e86a85 1146 WRITE_ONCE(*bypass_qtail, &pcl->next);
7146a4f0 1147
97e86a85 1148 qtail[JQ_BYPASS] = &pcl->next;
7146a4f0
GX
1149}
1150
1e4a2955 1151static void z_erofs_submit_queue(struct super_block *sb,
0c638f70
GX
1152 z_erofs_next_pcluster_t owned_head,
1153 struct list_head *pagepool,
1154 struct z_erofs_decompressqueue *fgq,
1155 bool *force_fg)
3883a79a 1156{
bda17a45 1157 struct erofs_sb_info *const sbi = EROFS_SB(sb);
97e86a85 1158 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
a4b1fab1 1159 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
7146a4f0 1160 void *bi_private;
3883a79a
GX
1161 /* since bio will be NULL, no need to initialize last_index */
1162 pgoff_t uninitialized_var(last_index);
1e4a2955
GX
1163 unsigned int nr_bios = 0;
1164 struct bio *bio = NULL;
3883a79a 1165
a4b1fab1
GX
1166 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1167 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1168 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
3883a79a
GX
1169
1170 /* by default, all need io submission */
7146a4f0 1171 q[JQ_SUBMIT]->head = owned_head;
3883a79a
GX
1172
1173 do {
97e86a85 1174 struct z_erofs_pcluster *pcl;
1e4a2955
GX
1175 pgoff_t cur, end;
1176 unsigned int i = 0;
1177 bool bypass = true;
3883a79a
GX
1178
1179 /* no possible 'owned_head' equals the following */
97e86a85
GX
1180 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1181 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1182
1183 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
3883a79a 1184
1e4a2955
GX
1185 cur = pcl->obj.index;
1186 end = cur + BIT(pcl->clusterbits);
3883a79a
GX
1187
1188 /* close the main owned chain at first */
97e86a85
GX
1189 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1190 Z_EROFS_PCLUSTER_TAIL_CLOSED);
3883a79a 1191
1e4a2955
GX
1192 do {
1193 struct page *page;
1194 int err;
3883a79a 1195
1e4a2955
GX
1196 page = pickup_page_for_submission(pcl, i++, pagepool,
1197 MNGD_MAPPING(sbi),
1198 GFP_NOFS);
1199 if (!page)
1200 continue;
3883a79a 1201
1e4a2955 1202 if (bio && cur != last_index + 1) {
3883a79a 1203submit_bio_retry:
1e4a2955
GX
1204 submit_bio(bio);
1205 bio = NULL;
1206 }
a5c0b780 1207
1e4a2955
GX
1208 if (!bio) {
1209 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
94e4e153 1210
1e4a2955
GX
1211 bio->bi_end_io = z_erofs_decompressqueue_endio;
1212 bio_set_dev(bio, sb->s_bdev);
1213 bio->bi_iter.bi_sector = (sector_t)cur <<
1214 LOG_SECTORS_PER_BLOCK;
1215 bio->bi_private = bi_private;
1216 bio->bi_opf = REQ_OP_READ;
1217 ++nr_bios;
1218 }
3883a79a 1219
1e4a2955
GX
1220 err = bio_add_page(bio, page, PAGE_SIZE, 0);
1221 if (err < PAGE_SIZE)
1222 goto submit_bio_retry;
3883a79a 1223
1e4a2955
GX
1224 last_index = cur;
1225 bypass = false;
1226 } while (++cur < end);
105d4ad8 1227
1e4a2955 1228 if (!bypass)
97e86a85 1229 qtail[JQ_SUBMIT] = &pcl->next;
7146a4f0 1230 else
97e86a85
GX
1231 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1232 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
3883a79a 1233
42d40b4a 1234 if (bio)
94e4e153 1235 submit_bio(bio);
3883a79a 1236
587a67b7
GX
1237 /*
1238 * although background is preferred, no one is pending for submission.
1239 * don't issue workqueue for decompression but drop it directly instead.
1240 */
1241 if (!*force_fg && !nr_bios) {
1242 kvfree(q[JQ_SUBMIT]);
1e4a2955 1243 return;
587a67b7 1244 }
a4b1fab1 1245 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
3883a79a
GX
1246}
1247
0c638f70
GX
1248static void z_erofs_runqueue(struct super_block *sb,
1249 struct z_erofs_collector *clt,
1250 struct list_head *pagepool, bool force_fg)
3883a79a 1251{
a4b1fab1 1252 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
3883a79a 1253
1e4a2955 1254 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
3883a79a 1255 return;
1e4a2955 1256 z_erofs_submit_queue(sb, clt->owned_head, pagepool, io, &force_fg);
3883a79a 1257
0c638f70
GX
1258 /* handle bypass queue (no i/o pclusters) immediately */
1259 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
4279f3f9 1260
3883a79a
GX
1261 if (!force_fg)
1262 return;
1263
1264 /* wait until all bios are completed */
a93f8c36
GX
1265 io_wait_event(io[JQ_SUBMIT].u.wait,
1266 !atomic_read(&io[JQ_SUBMIT].pending_bios));
3883a79a 1267
0c638f70
GX
1268 /* handle synchronous decompress queue in the caller context */
1269 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
3883a79a
GX
1270}
1271
0c638f70 1272static int z_erofs_readpage(struct file *file, struct page *page)
3883a79a
GX
1273{
1274 struct inode *const inode = page->mapping->host;
97e86a85 1275 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
3883a79a
GX
1276 int err;
1277 LIST_HEAD(pagepool);
1278
ba9ce771
GX
1279 trace_erofs_readpage(page, false);
1280
f0c519fc
GX
1281 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1282
3883a79a 1283 err = z_erofs_do_read_page(&f, page, &pagepool);
97e86a85 1284 (void)z_erofs_collector_end(&f.clt);
3883a79a 1285
ee45197c 1286 /* if some compressed cluster ready, need submit them anyway */
0c638f70 1287 z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, true);
ee45197c
GX
1288
1289 if (err)
4f761fa2 1290 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
3883a79a 1291
3b423417
CY
1292 if (f.map.mpage)
1293 put_page(f.map.mpage);
3883a79a
GX
1294
1295 /* clean up the remaining free pages */
1296 put_pages_list(&pagepool);
ee45197c 1297 return err;
3883a79a
GX
1298}
1299
14f362b4
GX
1300static bool should_decompress_synchronously(struct erofs_sb_info *sbi,
1301 unsigned int nr)
1302{
1303 return nr <= sbi->max_sync_decompress_pages;
1304}
1305
0c638f70
GX
1306static int z_erofs_readpages(struct file *filp, struct address_space *mapping,
1307 struct list_head *pages, unsigned int nr_pages)
3883a79a
GX
1308{
1309 struct inode *const inode = mapping->host;
5fb76bb0 1310 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
3883a79a 1311
14f362b4 1312 bool sync = should_decompress_synchronously(sbi, nr_pages);
97e86a85 1313 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
3883a79a
GX
1314 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
1315 struct page *head = NULL;
1316 LIST_HEAD(pagepool);
1317
284db12c
CG
1318 trace_erofs_readpages(mapping->host, lru_to_page(pages),
1319 nr_pages, false);
1320
f0c519fc
GX
1321 f.headoffset = (erofs_off_t)lru_to_page(pages)->index << PAGE_SHIFT;
1322
3883a79a
GX
1323 for (; nr_pages; --nr_pages) {
1324 struct page *page = lru_to_page(pages);
1325
1326 prefetchw(&page->flags);
1327 list_del(&page->lru);
1328
2d9b5dcd
GX
1329 /*
1330 * A pure asynchronous readahead is indicated if
1331 * a PG_readahead marked page is hitted at first.
1332 * Let's also do asynchronous decompression for this case.
1333 */
1334 sync &= !(PageReadahead(page) && !head);
1335
3883a79a
GX
1336 if (add_to_page_cache_lru(page, mapping, page->index, gfp)) {
1337 list_add(&page->lru, &pagepool);
1338 continue;
1339 }
1340
3883a79a
GX
1341 set_page_private(page, (unsigned long)head);
1342 head = page;
1343 }
1344
42d40b4a 1345 while (head) {
3883a79a
GX
1346 struct page *page = head;
1347 int err;
1348
1349 /* traversal in reverse order */
1350 head = (void *)page_private(page);
1351
1352 err = z_erofs_do_read_page(&f, page, &pagepool);
a5876e24 1353 if (err)
4f761fa2
GX
1354 erofs_err(inode->i_sb,
1355 "readahead error at page %lu @ nid %llu",
1356 page->index, EROFS_I(inode)->nid);
3883a79a
GX
1357 put_page(page);
1358 }
1359
97e86a85 1360 (void)z_erofs_collector_end(&f.clt);
3883a79a 1361
0c638f70 1362 z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, sync);
3883a79a 1363
3b423417
CY
1364 if (f.map.mpage)
1365 put_page(f.map.mpage);
3883a79a
GX
1366
1367 /* clean up the remaining free pages */
1368 put_pages_list(&pagepool);
1369 return 0;
1370}
1371
0c638f70
GX
1372const struct address_space_operations z_erofs_aops = {
1373 .readpage = z_erofs_readpage,
1374 .readpages = z_erofs_readpages,
3883a79a 1375};
02827e17 1376