1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2022 Alibaba Cloud
6 */
7 #include "compress.h"
8 #include <linux/psi.h>
9 #include <linux/cpuhotplug.h>
10 #include <trace/events/erofs.h>
11
12 #define Z_EROFS_MAX_SYNC_DECOMPRESS_BYTES 12288
13 #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
14 #define Z_EROFS_INLINE_BVECS 2
15
16 struct z_erofs_bvec {
17 struct page *page;
18 int offset;
19 unsigned int end;
20 };
21
22 #define __Z_EROFS_BVSET(name, total) \
23 struct name { \
24 /* point to the next page which contains the following bvecs */ \
25 struct page *nextpage; \
26 struct z_erofs_bvec bvec[total]; \
27 }
28 __Z_EROFS_BVSET(z_erofs_bvset,);
29 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
30
31 /*
32 * Structure fields follow one of the following exclusion rules.
33 *
34 * I: Modifiable by initialization/destruction paths and read-only
35 * for everyone else;
36 *
37 * L: Field should be protected by the pcluster lock;
38 *
39 * A: Field should be accessed / updated in atomic for parallelized code.
40 */
41 struct z_erofs_pcluster {
42 struct mutex lock;
43 struct lockref lockref;
44
45 /* A: point to next chained pcluster or TAILs */
46 struct z_erofs_pcluster *next;
47
48 /* I: start physical position of this pcluster */
49 erofs_off_t pos;
50
51 /* L: the maximum decompression size of this round */
52 unsigned int length;
53
54 /* L: total number of bvecs */
55 unsigned int vcnt;
56
57 /* I: pcluster size (compressed size) in bytes */
58 unsigned int pclustersize;
59
60 /* I: page offset of start position of decompression */
61 unsigned short pageofs_out;
62
63 /* I: page offset of inline compressed data */
64 unsigned short pageofs_in;
65
66 union {
67 /* L: inline a certain number of bvec for bootstrap */
68 struct z_erofs_bvset_inline bvset;
69
70 /* I: can be used to free the pcluster by RCU. */
71 struct rcu_head rcu;
72 };
73
74 /* I: compression algorithm format */
75 unsigned char algorithmformat;
76
77 /* I: whether compressed data is in-lined or not */
78 bool from_meta;
79
80 /* L: whether partial decompression or not */
81 bool partial;
82
83 /* L: whether extra buffer allocations are best-effort */
84 bool besteffort;
85
86 /* A: compressed bvecs (can be cached or inplaced pages) */
87 struct z_erofs_bvec compressed_bvecs[];
88 };
89
90 /* the end of a chain of pclusters */
91 #define Z_EROFS_PCLUSTER_TAIL ((void *) 0x700 + POISON_POINTER_DELTA)
92
93 struct z_erofs_decompressqueue {
94 struct super_block *sb;
95 struct z_erofs_pcluster *head;
96 atomic_t pending_bios;
97
98 union {
99 struct completion done;
100 struct work_struct work;
101 struct kthread_work kthread_work;
102 } u;
103 bool eio, sync;
104 };
105
z_erofs_pclusterpages(struct z_erofs_pcluster * pcl)106 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
107 {
108 return PAGE_ALIGN(pcl->pageofs_in + pcl->pclustersize) >> PAGE_SHIFT;
109 }
110
erofs_folio_is_managed(struct erofs_sb_info * sbi,struct folio * fo)111 static bool erofs_folio_is_managed(struct erofs_sb_info *sbi, struct folio *fo)
112 {
113 return fo->mapping == MNGD_MAPPING(sbi);
114 }
115
116 #define Z_EROFS_ONSTACK_PAGES 32
117
118 /*
119 * since pclustersize is variable for big pcluster feature, introduce slab
120 * pools implementation for different pcluster sizes.
121 */
122 struct z_erofs_pcluster_slab {
123 struct kmem_cache *slab;
124 unsigned int maxpages;
125 char name[48];
126 };
127
128 #define _PCLP(n) { .maxpages = n }
129
130 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
131 _PCLP(1),
132 _PCLP(4),
133 #if Z_EROFS_PCLUSTER_MAX_PAGES > 16
134 _PCLP(16),
135 #endif
136 #if Z_EROFS_PCLUSTER_MAX_PAGES > 64
137 _PCLP(64),
138 #endif
139 #if Z_EROFS_PCLUSTER_MAX_PAGES > 128
140 _PCLP(128),
141 #endif
142 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES + 1)
143 };
144
145 struct z_erofs_bvec_iter {
146 struct page *bvpage;
147 struct z_erofs_bvset *bvset;
148 unsigned int nr, cur;
149 };
150
z_erofs_bvec_iter_end(struct z_erofs_bvec_iter * iter)151 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
152 {
153 if (iter->bvpage)
154 kunmap_local(iter->bvset);
155 return iter->bvpage;
156 }
157
z_erofs_bvset_flip(struct z_erofs_bvec_iter * iter)158 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
159 {
160 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
161 /* have to access nextpage in advance, otherwise it will be unmapped */
162 struct page *nextpage = iter->bvset->nextpage;
163 struct page *oldpage;
164
165 DBG_BUGON(!nextpage);
166 oldpage = z_erofs_bvec_iter_end(iter);
167 iter->bvpage = nextpage;
168 iter->bvset = kmap_local_page(nextpage);
169 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
170 iter->cur = 0;
171 return oldpage;
172 }
173
z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter * iter,struct z_erofs_bvset_inline * bvset,unsigned int bootstrap_nr,unsigned int cur)174 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
175 struct z_erofs_bvset_inline *bvset,
176 unsigned int bootstrap_nr,
177 unsigned int cur)
178 {
179 *iter = (struct z_erofs_bvec_iter) {
180 .nr = bootstrap_nr,
181 .bvset = (struct z_erofs_bvset *)bvset,
182 };
183
184 while (cur > iter->nr) {
185 cur -= iter->nr;
186 z_erofs_bvset_flip(iter);
187 }
188 iter->cur = cur;
189 }
190
z_erofs_bvec_enqueue(struct z_erofs_bvec_iter * iter,struct z_erofs_bvec * bvec,struct page ** candidate_bvpage,struct page ** pagepool)191 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
192 struct z_erofs_bvec *bvec,
193 struct page **candidate_bvpage,
194 struct page **pagepool)
195 {
196 if (iter->cur >= iter->nr) {
197 struct page *nextpage = *candidate_bvpage;
198
199 if (!nextpage) {
200 nextpage = __erofs_allocpage(pagepool, GFP_KERNEL,
201 true);
202 if (!nextpage)
203 return -ENOMEM;
204 set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
205 }
206 DBG_BUGON(iter->bvset->nextpage);
207 iter->bvset->nextpage = nextpage;
208 z_erofs_bvset_flip(iter);
209
210 iter->bvset->nextpage = NULL;
211 *candidate_bvpage = NULL;
212 }
213 iter->bvset->bvec[iter->cur++] = *bvec;
214 return 0;
215 }
216
z_erofs_bvec_dequeue(struct z_erofs_bvec_iter * iter,struct z_erofs_bvec * bvec,struct page ** old_bvpage)217 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
218 struct z_erofs_bvec *bvec,
219 struct page **old_bvpage)
220 {
221 if (iter->cur == iter->nr)
222 *old_bvpage = z_erofs_bvset_flip(iter);
223 else
224 *old_bvpage = NULL;
225 *bvec = iter->bvset->bvec[iter->cur++];
226 }
227
z_erofs_destroy_pcluster_pool(void)228 static void z_erofs_destroy_pcluster_pool(void)
229 {
230 int i;
231
232 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
233 if (!pcluster_pool[i].slab)
234 continue;
235 kmem_cache_destroy(pcluster_pool[i].slab);
236 pcluster_pool[i].slab = NULL;
237 }
238 }
239
z_erofs_create_pcluster_pool(void)240 static int z_erofs_create_pcluster_pool(void)
241 {
242 struct z_erofs_pcluster_slab *pcs;
243 struct z_erofs_pcluster *a;
244 unsigned int size;
245
246 for (pcs = pcluster_pool;
247 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
248 size = struct_size(a, compressed_bvecs, pcs->maxpages);
249
250 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
251 pcs->slab = kmem_cache_create(pcs->name, size, 0,
252 SLAB_RECLAIM_ACCOUNT, NULL);
253 if (pcs->slab)
254 continue;
255
256 z_erofs_destroy_pcluster_pool();
257 return -ENOMEM;
258 }
259 return 0;
260 }
261
z_erofs_alloc_pcluster(unsigned int size)262 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int size)
263 {
264 unsigned int nrpages = PAGE_ALIGN(size) >> PAGE_SHIFT;
265 struct z_erofs_pcluster_slab *pcs = pcluster_pool;
266
267 for (; pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
268 struct z_erofs_pcluster *pcl;
269
270 if (nrpages > pcs->maxpages)
271 continue;
272
273 pcl = kmem_cache_zalloc(pcs->slab, GFP_KERNEL);
274 if (!pcl)
275 return ERR_PTR(-ENOMEM);
276 return pcl;
277 }
278 return ERR_PTR(-EINVAL);
279 }
280
z_erofs_free_pcluster(struct z_erofs_pcluster * pcl)281 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
282 {
283 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
284 int i;
285
286 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
287 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
288
289 if (pclusterpages > pcs->maxpages)
290 continue;
291
292 kmem_cache_free(pcs->slab, pcl);
293 return;
294 }
295 DBG_BUGON(1);
296 }
297
298 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
299
300 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
301 static struct kthread_worker __rcu **z_erofs_pcpu_workers;
302 static atomic_t erofs_percpu_workers_initialized = ATOMIC_INIT(0);
303
erofs_destroy_percpu_workers(void)304 static void erofs_destroy_percpu_workers(void)
305 {
306 struct kthread_worker *worker;
307 unsigned int cpu;
308
309 for_each_possible_cpu(cpu) {
310 worker = rcu_dereference_protected(
311 z_erofs_pcpu_workers[cpu], 1);
312 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
313 if (worker)
314 kthread_destroy_worker(worker);
315 }
316 kfree(z_erofs_pcpu_workers);
317 }
318
erofs_init_percpu_worker(int cpu)319 static struct kthread_worker *erofs_init_percpu_worker(int cpu)
320 {
321 struct kthread_worker *worker =
322 kthread_run_worker_on_cpu(cpu, 0, "erofs_worker/%u");
323
324 if (IS_ERR(worker))
325 return worker;
326 if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
327 sched_set_fifo_low(worker->task);
328 return worker;
329 }
330
erofs_init_percpu_workers(void)331 static int erofs_init_percpu_workers(void)
332 {
333 struct kthread_worker *worker;
334 unsigned int cpu;
335
336 z_erofs_pcpu_workers = kzalloc_objs(struct kthread_worker *,
337 num_possible_cpus(), GFP_ATOMIC);
338 if (!z_erofs_pcpu_workers)
339 return -ENOMEM;
340
341 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
342 worker = erofs_init_percpu_worker(cpu);
343 if (!IS_ERR(worker))
344 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
345 }
346 return 0;
347 }
348
349 #ifdef CONFIG_HOTPLUG_CPU
350 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
351 static enum cpuhp_state erofs_cpuhp_state;
352
erofs_cpu_online(unsigned int cpu)353 static int erofs_cpu_online(unsigned int cpu)
354 {
355 struct kthread_worker *worker, *old;
356
357 worker = erofs_init_percpu_worker(cpu);
358 if (IS_ERR(worker))
359 return PTR_ERR(worker);
360
361 spin_lock(&z_erofs_pcpu_worker_lock);
362 old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
363 lockdep_is_held(&z_erofs_pcpu_worker_lock));
364 if (!old)
365 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
366 spin_unlock(&z_erofs_pcpu_worker_lock);
367 if (old)
368 kthread_destroy_worker(worker);
369 return 0;
370 }
371
erofs_cpu_offline(unsigned int cpu)372 static int erofs_cpu_offline(unsigned int cpu)
373 {
374 struct kthread_worker *worker;
375
376 spin_lock(&z_erofs_pcpu_worker_lock);
377 worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
378 lockdep_is_held(&z_erofs_pcpu_worker_lock));
379 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
380 spin_unlock(&z_erofs_pcpu_worker_lock);
381
382 synchronize_rcu();
383 if (worker)
384 kthread_destroy_worker(worker);
385 return 0;
386 }
387
erofs_cpu_hotplug_init(void)388 static int erofs_cpu_hotplug_init(void)
389 {
390 int state;
391
392 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
393 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
394 if (state < 0)
395 return state;
396
397 erofs_cpuhp_state = state;
398 return 0;
399 }
400
erofs_cpu_hotplug_destroy(void)401 static void erofs_cpu_hotplug_destroy(void)
402 {
403 if (erofs_cpuhp_state)
404 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
405 }
406 #else /* !CONFIG_HOTPLUG_CPU */
erofs_cpu_hotplug_init(void)407 static inline int erofs_cpu_hotplug_init(void) { return 0; }
erofs_cpu_hotplug_destroy(void)408 static inline void erofs_cpu_hotplug_destroy(void) {}
409 #endif/* CONFIG_HOTPLUG_CPU */
z_erofs_init_pcpu_workers(struct super_block * sb)410 static int z_erofs_init_pcpu_workers(struct super_block *sb)
411 {
412 int err;
413
414 if (atomic_xchg(&erofs_percpu_workers_initialized, 1))
415 return 0;
416
417 err = erofs_init_percpu_workers();
418 if (err) {
419 erofs_err(sb, "per-cpu workers: failed to allocate.");
420 goto err_init_percpu_workers;
421 }
422
423 err = erofs_cpu_hotplug_init();
424 if (err < 0) {
425 erofs_err(sb, "per-cpu workers: failed CPU hotplug init.");
426 goto err_cpuhp_init;
427 }
428 erofs_info(sb, "initialized per-cpu workers successfully.");
429 return err;
430
431 err_cpuhp_init:
432 erofs_destroy_percpu_workers();
433 err_init_percpu_workers:
434 atomic_set(&erofs_percpu_workers_initialized, 0);
435 return err;
436 }
437
z_erofs_destroy_pcpu_workers(void)438 static void z_erofs_destroy_pcpu_workers(void)
439 {
440 if (!atomic_xchg(&erofs_percpu_workers_initialized, 0))
441 return;
442 erofs_cpu_hotplug_destroy();
443 erofs_destroy_percpu_workers();
444 }
445 #else /* !CONFIG_EROFS_FS_PCPU_KTHREAD */
z_erofs_init_pcpu_workers(struct super_block * sb)446 static inline int z_erofs_init_pcpu_workers(struct super_block *sb) { return 0; }
z_erofs_destroy_pcpu_workers(void)447 static inline void z_erofs_destroy_pcpu_workers(void) {}
448 #endif/* CONFIG_EROFS_FS_PCPU_KTHREAD */
449
z_erofs_exit_subsystem(void)450 void z_erofs_exit_subsystem(void)
451 {
452 z_erofs_destroy_pcpu_workers();
453 destroy_workqueue(z_erofs_workqueue);
454 z_erofs_destroy_pcluster_pool();
455 z_erofs_crypto_disable_all_engines();
456 z_erofs_exit_decompressor();
457 }
458
z_erofs_init_subsystem(void)459 int __init z_erofs_init_subsystem(void)
460 {
461 int err = z_erofs_init_decompressor();
462
463 if (err)
464 goto err_decompressor;
465
466 err = z_erofs_create_pcluster_pool();
467 if (err)
468 goto err_pcluster_pool;
469
470 z_erofs_workqueue = alloc_workqueue("erofs_worker",
471 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
472 if (!z_erofs_workqueue) {
473 err = -ENOMEM;
474 goto err_workqueue_init;
475 }
476
477 return err;
478
479 err_workqueue_init:
480 z_erofs_destroy_pcluster_pool();
481 err_pcluster_pool:
482 z_erofs_exit_decompressor();
483 err_decompressor:
484 return err;
485 }
486
487 enum z_erofs_pclustermode {
488 /* It has previously been linked into another processing chain */
489 Z_EROFS_PCLUSTER_INFLIGHT,
490 /*
491 * A weaker form of Z_EROFS_PCLUSTER_FOLLOWED; the difference is that it
492 * may be dispatched to the bypass queue later due to uptodated managed
493 * folios. All file-backed folios related to this pcluster cannot be
494 * reused for in-place I/O (or bvpage) since the pcluster may be decoded
495 * in a separate queue (and thus out of order).
496 */
497 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
498 /*
499 * The pcluster has just been linked to our processing chain.
500 * File-backed folios (except for the head page) related to it can be
501 * used for in-place I/O (or bvpage).
502 */
503 Z_EROFS_PCLUSTER_FOLLOWED,
504 };
505
506 struct z_erofs_frontend {
507 struct inode *inode, *sharedinode;
508 struct erofs_map_blocks map;
509 struct z_erofs_bvec_iter biter;
510
511 struct page *pagepool;
512 struct page *candidate_bvpage;
513 struct z_erofs_pcluster *pcl, *head;
514 enum z_erofs_pclustermode mode;
515
516 erofs_off_t headoffset;
517
518 /* a pointer used to pick up inplace I/O pages */
519 unsigned int icur;
520 };
521
522 #define Z_EROFS_DEFINE_FRONTEND(fe, i, si, ho) struct z_erofs_frontend fe = { \
523 .inode = i, .sharedinode = si, .head = Z_EROFS_PCLUSTER_TAIL, \
524 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .headoffset = ho }
525
z_erofs_should_alloc_cache(struct z_erofs_frontend * fe)526 static bool z_erofs_should_alloc_cache(struct z_erofs_frontend *fe)
527 {
528 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
529
530 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
531 return false;
532
533 if (fe->map.m_flags & EROFS_MAP_PARTIAL_MAPPED)
534 return true;
535
536 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
537 fe->map.m_la < fe->headoffset)
538 return true;
539
540 return false;
541 }
542
z_erofs_bind_cache(struct z_erofs_frontend * fe)543 static void z_erofs_bind_cache(struct z_erofs_frontend *fe)
544 {
545 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
546 struct z_erofs_pcluster *pcl = fe->pcl;
547 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
548 bool shouldalloc = z_erofs_should_alloc_cache(fe);
549 pgoff_t poff = pcl->pos >> PAGE_SHIFT;
550 bool may_bypass = true;
551 /* Optimistic allocation, as in-place I/O can be used as a fallback */
552 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
553 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
554 struct folio *folio, *newfolio;
555 unsigned int i;
556
557 if (i_blocksize(fe->inode) != PAGE_SIZE ||
558 fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
559 return;
560
561 for (i = 0; i < pclusterpages; ++i) {
562 /* Inaccurate check w/o locking to avoid unneeded lookups */
563 if (READ_ONCE(pcl->compressed_bvecs[i].page))
564 continue;
565
566 folio = filemap_get_folio(mc, poff + i);
567 if (IS_ERR(folio)) {
568 may_bypass = false;
569 if (!shouldalloc)
570 continue;
571
572 /*
573 * Allocate a managed folio for cached I/O, or it may be
574 * then filled with a file-backed folio for in-place I/O
575 */
576 newfolio = filemap_alloc_folio(gfp, 0, NULL);
577 if (!newfolio)
578 continue;
579 newfolio->private = Z_EROFS_PREALLOCATED_FOLIO;
580 folio = NULL;
581 }
582 spin_lock(&pcl->lockref.lock);
583 if (!pcl->compressed_bvecs[i].page) {
584 pcl->compressed_bvecs[i].page =
585 folio_page(folio ?: newfolio, 0);
586 spin_unlock(&pcl->lockref.lock);
587 continue;
588 }
589 spin_unlock(&pcl->lockref.lock);
590 folio_put(folio ?: newfolio);
591 }
592
593 /*
594 * Don't perform in-place I/O if all compressed pages are available in
595 * the managed cache, as the pcluster can be moved to the bypass queue.
596 */
597 if (may_bypass)
598 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
599 }
600
601 /* (erofs_shrinker) disconnect cached encoded data with pclusters */
erofs_try_to_free_all_cached_folios(struct erofs_sb_info * sbi,struct z_erofs_pcluster * pcl)602 static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,
603 struct z_erofs_pcluster *pcl)
604 {
605 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
606 struct folio *folio;
607 int i;
608
609 DBG_BUGON(pcl->from_meta);
610 /* Each cached folio contains one page unless bs > ps is supported */
611 for (i = 0; i < pclusterpages; ++i) {
612 if (pcl->compressed_bvecs[i].page) {
613 folio = page_folio(pcl->compressed_bvecs[i].page);
614 /* Avoid reclaiming or migrating this folio */
615 if (!folio_trylock(folio))
616 return -EBUSY;
617
618 DBG_BUGON(!erofs_folio_is_managed(sbi, folio));
619 pcl->compressed_bvecs[i].page = NULL;
620 folio_detach_private(folio);
621 folio_unlock(folio);
622 }
623 }
624 return 0;
625 }
626
z_erofs_cache_release_folio(struct folio * folio,gfp_t gfp)627 static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)
628 {
629 struct z_erofs_pcluster *pcl = folio_get_private(folio);
630 struct z_erofs_bvec *bvec = pcl->compressed_bvecs;
631 struct z_erofs_bvec *end = bvec + z_erofs_pclusterpages(pcl);
632 bool ret;
633
634 if (!folio_test_private(folio))
635 return true;
636
637 ret = false;
638 spin_lock(&pcl->lockref.lock);
639 if (pcl->lockref.count <= 0) {
640 DBG_BUGON(pcl->from_meta);
641 for (; bvec < end; ++bvec) {
642 if (bvec->page && page_folio(bvec->page) == folio) {
643 bvec->page = NULL;
644 folio_detach_private(folio);
645 ret = true;
646 break;
647 }
648 }
649 }
650 spin_unlock(&pcl->lockref.lock);
651 return ret;
652 }
653
654 /*
655 * It will be called only on inode eviction. In case that there are still some
656 * decompression requests in progress, wait with rescheduling for a bit here.
657 * An extra lock could be introduced instead but it seems unnecessary.
658 */
z_erofs_cache_invalidate_folio(struct folio * folio,size_t offset,size_t length)659 static void z_erofs_cache_invalidate_folio(struct folio *folio,
660 size_t offset, size_t length)
661 {
662 const size_t stop = length + offset;
663
664 /* Check for potential overflow in debug mode */
665 DBG_BUGON(stop > folio_size(folio) || stop < length);
666
667 if (offset == 0 && stop == folio_size(folio))
668 while (!z_erofs_cache_release_folio(folio, 0))
669 cond_resched();
670 }
671
672 static const struct address_space_operations z_erofs_cache_aops = {
673 .release_folio = z_erofs_cache_release_folio,
674 .invalidate_folio = z_erofs_cache_invalidate_folio,
675 };
676
z_erofs_init_super(struct super_block * sb)677 int z_erofs_init_super(struct super_block *sb)
678 {
679 int err;
680
681 err = z_erofs_init_pcpu_workers(sb);
682 if (err)
683 return err;
684
685 err = erofs_setup_managed_cache(sb);
686 if (err)
687 return err;
688 EROFS_SB(sb)->managed_cache->i_mapping->a_ops = &z_erofs_cache_aops;
689 xa_init(&EROFS_SB(sb)->managed_pslots);
690 return 0;
691 }
692
693 /* callers must be with pcluster lock held */
z_erofs_attach_page(struct z_erofs_frontend * fe,struct z_erofs_bvec * bvec,bool exclusive)694 static int z_erofs_attach_page(struct z_erofs_frontend *fe,
695 struct z_erofs_bvec *bvec, bool exclusive)
696 {
697 struct z_erofs_pcluster *pcl = fe->pcl;
698 int ret;
699
700 if (exclusive) {
701 /* Inplace I/O is limited to one page for uncompressed data */
702 if (pcl->algorithmformat < Z_EROFS_COMPRESSION_MAX ||
703 fe->icur <= 1) {
704 /* Try to prioritize inplace I/O here */
705 spin_lock(&pcl->lockref.lock);
706 while (fe->icur > 0) {
707 if (pcl->compressed_bvecs[--fe->icur].page)
708 continue;
709 pcl->compressed_bvecs[fe->icur] = *bvec;
710 spin_unlock(&pcl->lockref.lock);
711 return 0;
712 }
713 spin_unlock(&pcl->lockref.lock);
714 }
715
716 /* otherwise, check if it can be used as a bvpage */
717 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
718 !fe->candidate_bvpage)
719 fe->candidate_bvpage = bvec->page;
720 }
721 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
722 &fe->pagepool);
723 fe->pcl->vcnt += (ret >= 0);
724 return ret;
725 }
726
z_erofs_get_pcluster(struct z_erofs_pcluster * pcl)727 static bool z_erofs_get_pcluster(struct z_erofs_pcluster *pcl)
728 {
729 if (lockref_get_not_zero(&pcl->lockref))
730 return true;
731
732 spin_lock(&pcl->lockref.lock);
733 if (lockref_is_dead(&pcl->lockref)) {
734 spin_unlock(&pcl->lockref.lock);
735 return false;
736 }
737
738 if (!pcl->lockref.count++)
739 atomic_long_dec(&erofs_global_shrink_cnt);
740 spin_unlock(&pcl->lockref.lock);
741 return true;
742 }
743
z_erofs_register_pcluster(struct z_erofs_frontend * fe)744 static int z_erofs_register_pcluster(struct z_erofs_frontend *fe)
745 {
746 struct erofs_map_blocks *map = &fe->map;
747 struct super_block *sb = fe->inode->i_sb;
748 struct erofs_sb_info *sbi = EROFS_SB(sb);
749 struct z_erofs_pcluster *pcl, *pre;
750 unsigned int pageofs_in;
751 int err;
752
753 pageofs_in = erofs_blkoff(sb, map->m_pa);
754 pcl = z_erofs_alloc_pcluster(pageofs_in + map->m_plen);
755 if (IS_ERR(pcl))
756 return PTR_ERR(pcl);
757
758 lockref_init(&pcl->lockref); /* one ref for this request */
759 pcl->algorithmformat = map->m_algorithmformat;
760 pcl->pclustersize = map->m_plen;
761 pcl->length = 0;
762 pcl->partial = true;
763 pcl->next = fe->head;
764 pcl->pos = map->m_pa;
765 pcl->pageofs_in = pageofs_in;
766 pcl->pageofs_out = map->m_la & ~PAGE_MASK;
767 pcl->from_meta = map->m_flags & EROFS_MAP_META;
768 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
769
770 /*
771 * lock all primary followed works before visible to others
772 * and mutex_trylock *never* fails for a new pcluster.
773 */
774 mutex_init(&pcl->lock);
775 DBG_BUGON(!mutex_trylock(&pcl->lock));
776
777 if (!pcl->from_meta) {
778 while (1) {
779 xa_lock(&sbi->managed_pslots);
780 pre = __xa_cmpxchg(&sbi->managed_pslots, pcl->pos,
781 NULL, pcl, GFP_KERNEL);
782 if (!pre || xa_is_err(pre) || z_erofs_get_pcluster(pre)) {
783 xa_unlock(&sbi->managed_pslots);
784 break;
785 }
786 /* try to legitimize the current in-tree one */
787 xa_unlock(&sbi->managed_pslots);
788 cond_resched();
789 }
790 if (xa_is_err(pre)) {
791 err = xa_err(pre);
792 goto err_out;
793 } else if (pre) {
794 fe->pcl = pre;
795 err = -EEXIST;
796 goto err_out;
797 }
798 }
799 fe->head = fe->pcl = pcl;
800 return 0;
801
802 err_out:
803 mutex_unlock(&pcl->lock);
804 z_erofs_free_pcluster(pcl);
805 return err;
806 }
807
z_erofs_pcluster_begin(struct z_erofs_frontend * fe)808 static int z_erofs_pcluster_begin(struct z_erofs_frontend *fe)
809 {
810 struct erofs_map_blocks *map = &fe->map;
811 struct super_block *sb = fe->inode->i_sb;
812 struct z_erofs_pcluster *pcl = NULL;
813 void *ptr = NULL;
814 bool needretry;
815 int ret;
816
817 DBG_BUGON(fe->pcl);
818 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
819 DBG_BUGON(!fe->head);
820
821 if (map->m_flags & EROFS_MAP_META) {
822 ret = erofs_init_metabuf(&map->buf, sb,
823 erofs_inode_in_metabox(fe->inode));
824 if (ret)
825 return ret;
826 ptr = erofs_bread(&map->buf, map->m_pa, false);
827 if (IS_ERR(ptr)) {
828 erofs_err(sb, "failed to read inline data %pe @ pa %llu of nid %llu",
829 ptr, map->m_pa, EROFS_I(fe->inode)->nid);
830 return PTR_ERR(ptr);
831 }
832 ptr = map->buf.page;
833 } else {
834 do {
835 rcu_read_lock();
836 pcl = xa_load(&EROFS_SB(sb)->managed_pslots, map->m_pa);
837 needretry = pcl && !z_erofs_get_pcluster(pcl);
838 rcu_read_unlock();
839 } while (needretry);
840 }
841
842 if (pcl) {
843 DBG_BUGON(map->m_pa != pcl->pos);
844 fe->pcl = pcl;
845 ret = -EEXIST;
846 } else {
847 ret = z_erofs_register_pcluster(fe);
848 }
849
850 if (ret == -EEXIST) {
851 mutex_lock(&fe->pcl->lock);
852 /* check if this pcluster hasn't been linked into any chain. */
853 if (!cmpxchg(&fe->pcl->next, NULL, fe->head)) {
854 /* .. so it can be attached to our submission chain */
855 fe->head = fe->pcl;
856 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
857 } else { /* otherwise, it belongs to an inflight chain */
858 fe->mode = Z_EROFS_PCLUSTER_INFLIGHT;
859 }
860 } else if (ret) {
861 return ret;
862 }
863
864 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
865 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
866 if (!fe->pcl->from_meta) {
867 /* bind cache first when cached decompression is preferred */
868 z_erofs_bind_cache(fe);
869 } else {
870 folio_get(page_folio((struct page *)ptr));
871 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, ptr);
872 fe->pcl->pageofs_in = map->m_pa & ~PAGE_MASK;
873 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
874 }
875 /* file-backed inplace I/O pages are traversed in reverse order */
876 fe->icur = z_erofs_pclusterpages(fe->pcl);
877 return 0;
878 }
879
z_erofs_rcu_callback(struct rcu_head * head)880 static void z_erofs_rcu_callback(struct rcu_head *head)
881 {
882 z_erofs_free_pcluster(container_of(head, struct z_erofs_pcluster, rcu));
883 }
884
__erofs_try_to_release_pcluster(struct erofs_sb_info * sbi,struct z_erofs_pcluster * pcl)885 static bool __erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
886 struct z_erofs_pcluster *pcl)
887 {
888 if (pcl->lockref.count)
889 return false;
890
891 /*
892 * Note that all cached folios should be detached before deleted from
893 * the XArray. Otherwise some folios could be still attached to the
894 * orphan old pcluster when the new one is available in the tree.
895 */
896 if (erofs_try_to_free_all_cached_folios(sbi, pcl))
897 return false;
898
899 /*
900 * It's impossible to fail after the pcluster is freezed, but in order
901 * to avoid some race conditions, add a DBG_BUGON to observe this.
902 */
903 DBG_BUGON(__xa_erase(&sbi->managed_pslots, pcl->pos) != pcl);
904
905 lockref_mark_dead(&pcl->lockref);
906 return true;
907 }
908
erofs_try_to_release_pcluster(struct erofs_sb_info * sbi,struct z_erofs_pcluster * pcl)909 static bool erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
910 struct z_erofs_pcluster *pcl)
911 {
912 bool free;
913
914 spin_lock(&pcl->lockref.lock);
915 free = __erofs_try_to_release_pcluster(sbi, pcl);
916 spin_unlock(&pcl->lockref.lock);
917 if (free) {
918 atomic_long_dec(&erofs_global_shrink_cnt);
919 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
920 }
921 return free;
922 }
923
z_erofs_shrink_scan(struct erofs_sb_info * sbi,unsigned long nr)924 unsigned long z_erofs_shrink_scan(struct erofs_sb_info *sbi, unsigned long nr)
925 {
926 struct z_erofs_pcluster *pcl;
927 unsigned long index, freed = 0;
928
929 xa_lock(&sbi->managed_pslots);
930 xa_for_each(&sbi->managed_pslots, index, pcl) {
931 /* try to shrink each valid pcluster */
932 if (!erofs_try_to_release_pcluster(sbi, pcl))
933 continue;
934 xa_unlock(&sbi->managed_pslots);
935
936 ++freed;
937 if (!--nr)
938 return freed;
939 xa_lock(&sbi->managed_pslots);
940 }
941 xa_unlock(&sbi->managed_pslots);
942 return freed;
943 }
944
z_erofs_put_pcluster(struct erofs_sb_info * sbi,struct z_erofs_pcluster * pcl,bool try_free)945 static void z_erofs_put_pcluster(struct erofs_sb_info *sbi,
946 struct z_erofs_pcluster *pcl, bool try_free)
947 {
948 bool free = false;
949
950 if (lockref_put_or_lock(&pcl->lockref))
951 return;
952
953 DBG_BUGON(lockref_is_dead(&pcl->lockref));
954 if (!--pcl->lockref.count) {
955 if (try_free && xa_trylock(&sbi->managed_pslots)) {
956 free = __erofs_try_to_release_pcluster(sbi, pcl);
957 xa_unlock(&sbi->managed_pslots);
958 }
959 atomic_long_add(!free, &erofs_global_shrink_cnt);
960 }
961 spin_unlock(&pcl->lockref.lock);
962 if (free)
963 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
964 }
965
z_erofs_pcluster_end(struct z_erofs_frontend * fe)966 static void z_erofs_pcluster_end(struct z_erofs_frontend *fe)
967 {
968 struct z_erofs_pcluster *pcl = fe->pcl;
969
970 if (!pcl)
971 return;
972
973 z_erofs_bvec_iter_end(&fe->biter);
974 mutex_unlock(&pcl->lock);
975
976 if (fe->candidate_bvpage)
977 fe->candidate_bvpage = NULL;
978
979 /* Drop refcount if it doesn't belong to our processing chain */
980 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
981 z_erofs_put_pcluster(EROFS_I_SB(fe->inode), pcl, false);
982 fe->pcl = NULL;
983 }
984
z_erofs_read_fragment(struct super_block * sb,struct folio * folio,unsigned int cur,unsigned int end,erofs_off_t pos)985 static int z_erofs_read_fragment(struct super_block *sb, struct folio *folio,
986 unsigned int cur, unsigned int end, erofs_off_t pos)
987 {
988 struct inode *packed_inode = EROFS_SB(sb)->packed_inode;
989 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
990 unsigned int cnt;
991 u8 *src;
992
993 if (!packed_inode)
994 return -EFSCORRUPTED;
995
996 buf.mapping = packed_inode->i_mapping;
997 for (; cur < end; cur += cnt, pos += cnt) {
998 cnt = min(end - cur, sb->s_blocksize - erofs_blkoff(sb, pos));
999 src = erofs_bread(&buf, pos, true);
1000 if (IS_ERR(src)) {
1001 erofs_put_metabuf(&buf);
1002 return PTR_ERR(src);
1003 }
1004 memcpy_to_folio(folio, cur, src, cnt);
1005 }
1006 erofs_put_metabuf(&buf);
1007 return 0;
1008 }
1009
z_erofs_scan_folio(struct z_erofs_frontend * f,struct folio * folio,bool ra)1010 static int z_erofs_scan_folio(struct z_erofs_frontend *f,
1011 struct folio *folio, bool ra)
1012 {
1013 struct inode *const inode = f->inode;
1014 struct erofs_map_blocks *const map = &f->map;
1015 const loff_t offset = folio_pos(folio);
1016 const unsigned int bs = i_blocksize(inode);
1017 unsigned int end = folio_size(folio), split = 0, cur, pgs;
1018 bool tight, excl;
1019 int err = 0;
1020
1021 tight = (bs == PAGE_SIZE);
1022 erofs_onlinefolio_init(folio);
1023 do {
1024 if (offset + end - 1 < map->m_la ||
1025 offset + end - 1 >= map->m_la + map->m_llen) {
1026 z_erofs_pcluster_end(f);
1027 map->m_la = offset + end - 1;
1028 map->m_llen = 0;
1029 err = z_erofs_map_blocks_iter(inode, map, 0);
1030 if (err)
1031 break;
1032 }
1033
1034 cur = offset > map->m_la ? 0 : map->m_la - offset;
1035 pgs = round_down(cur, PAGE_SIZE);
1036 /* bump split parts first to avoid several separate cases */
1037 ++split;
1038
1039 if (map->m_flags & EROFS_MAP_FRAGMENT) {
1040 erofs_off_t fpos = offset + cur - map->m_la;
1041
1042 err = z_erofs_read_fragment(inode->i_sb, folio, cur,
1043 cur + min(map->m_llen - fpos, end - cur),
1044 EROFS_I(inode)->z_fragmentoff + fpos);
1045 if (err)
1046 break;
1047 tight = false;
1048 } else if (!(map->m_flags & EROFS_MAP_MAPPED)) {
1049 folio_zero_segment(folio, cur, end);
1050 tight = false;
1051 } else {
1052 if (!f->pcl) {
1053 err = z_erofs_pcluster_begin(f);
1054 if (err)
1055 break;
1056 f->pcl->besteffort |= !ra;
1057 }
1058
1059 pgs = round_down(end - 1, PAGE_SIZE);
1060 /*
1061 * Ensure this partial page belongs to this submit chain
1062 * rather than other concurrent submit chains or
1063 * noio(bypass) chains since those chains are handled
1064 * asynchronously thus it cannot be used for inplace I/O
1065 * or bvpage (should be processed in the strict order.)
1066 */
1067 tight &= (f->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1068 excl = false;
1069 if (cur <= pgs) {
1070 excl = (split <= 1) || tight;
1071 cur = pgs;
1072 }
1073
1074 err = z_erofs_attach_page(f, &((struct z_erofs_bvec) {
1075 .page = folio_page(folio, pgs >> PAGE_SHIFT),
1076 .offset = offset + pgs - map->m_la,
1077 .end = end - pgs, }), excl);
1078 if (err)
1079 break;
1080
1081 erofs_onlinefolio_split(folio);
1082 if (f->pcl->length < offset + end - map->m_la) {
1083 f->pcl->length = offset + end - map->m_la;
1084 f->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1085 }
1086 if (EROFS_MAP_FULL(map->m_flags) &&
1087 f->pcl->length == map->m_llen)
1088 f->pcl->partial = false;
1089 }
1090 /* shorten the remaining extent to update progress */
1091 map->m_llen = offset + cur - map->m_la;
1092 map->m_flags |= EROFS_MAP_PARTIAL_MAPPED;
1093 if (cur <= pgs) {
1094 split = cur < pgs;
1095 tight = (bs == PAGE_SIZE);
1096 }
1097 } while ((end = cur) > 0);
1098 erofs_onlinefolio_end(folio, err, false);
1099 return err;
1100 }
1101
z_erofs_page_is_invalidated(struct page * page)1102 static bool z_erofs_page_is_invalidated(struct page *page)
1103 {
1104 return !page_folio(page)->mapping && !z_erofs_is_shortlived_page(page);
1105 }
1106
1107 struct z_erofs_backend {
1108 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1109 struct super_block *sb;
1110 struct z_erofs_pcluster *pcl;
1111 /* pages with the longest decompressed length for deduplication */
1112 struct page **decompressed_pages;
1113 /* pages to keep the compressed data */
1114 struct page **compressed_pages;
1115
1116 struct list_head decompressed_secondary_bvecs;
1117 struct page **pagepool;
1118 unsigned int onstack_used, nr_pages;
1119 /* indicate if temporary copies should be preserved for later use */
1120 bool keepxcpy;
1121 };
1122
1123 struct z_erofs_bvec_item {
1124 struct z_erofs_bvec bvec;
1125 struct list_head list;
1126 };
1127
z_erofs_do_decompressed_bvec(struct z_erofs_backend * be,struct z_erofs_bvec * bvec)1128 static void z_erofs_do_decompressed_bvec(struct z_erofs_backend *be,
1129 struct z_erofs_bvec *bvec)
1130 {
1131 int poff = bvec->offset + be->pcl->pageofs_out;
1132 struct z_erofs_bvec_item *item;
1133 struct page **page;
1134
1135 if (!(poff & ~PAGE_MASK) && (bvec->end == PAGE_SIZE ||
1136 bvec->offset + bvec->end == be->pcl->length)) {
1137 DBG_BUGON((poff >> PAGE_SHIFT) >= be->nr_pages);
1138 page = be->decompressed_pages + (poff >> PAGE_SHIFT);
1139 if (!*page) {
1140 *page = bvec->page;
1141 return;
1142 }
1143 } else {
1144 be->keepxcpy = true;
1145 }
1146
1147 /* (cold path) one pcluster is requested multiple times */
1148 item = kmalloc_obj(*item, GFP_KERNEL | __GFP_NOFAIL);
1149 item->bvec = *bvec;
1150 list_add(&item->list, &be->decompressed_secondary_bvecs);
1151 }
1152
z_erofs_fill_other_copies(struct z_erofs_backend * be,int err)1153 static void z_erofs_fill_other_copies(struct z_erofs_backend *be, int err)
1154 {
1155 unsigned int off0 = be->pcl->pageofs_out;
1156 struct list_head *p, *n;
1157
1158 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1159 struct z_erofs_bvec_item *bvi;
1160 unsigned int end, cur;
1161 void *dst, *src;
1162
1163 bvi = container_of(p, struct z_erofs_bvec_item, list);
1164 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1165 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1166 bvi->bvec.end);
1167 dst = kmap_local_page(bvi->bvec.page);
1168 while (cur < end) {
1169 unsigned int pgnr, scur, len;
1170
1171 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1172 DBG_BUGON(pgnr >= be->nr_pages);
1173
1174 scur = bvi->bvec.offset + cur -
1175 ((pgnr << PAGE_SHIFT) - off0);
1176 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1177 if (!be->decompressed_pages[pgnr]) {
1178 err = -EFSCORRUPTED;
1179 cur += len;
1180 continue;
1181 }
1182 src = kmap_local_page(be->decompressed_pages[pgnr]);
1183 memcpy(dst + cur, src + scur, len);
1184 kunmap_local(src);
1185 cur += len;
1186 }
1187 kunmap_local(dst);
1188 erofs_onlinefolio_end(page_folio(bvi->bvec.page), err, true);
1189 list_del(p);
1190 kfree(bvi);
1191 }
1192 }
1193
z_erofs_parse_out_bvecs(struct z_erofs_backend * be)1194 static void z_erofs_parse_out_bvecs(struct z_erofs_backend *be)
1195 {
1196 struct z_erofs_pcluster *pcl = be->pcl;
1197 struct z_erofs_bvec_iter biter;
1198 struct page *old_bvpage;
1199 int i;
1200
1201 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1202 for (i = 0; i < pcl->vcnt; ++i) {
1203 struct z_erofs_bvec bvec;
1204
1205 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1206
1207 if (old_bvpage)
1208 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1209
1210 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1211 z_erofs_do_decompressed_bvec(be, &bvec);
1212 }
1213
1214 old_bvpage = z_erofs_bvec_iter_end(&biter);
1215 if (old_bvpage)
1216 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1217 }
1218
z_erofs_parse_in_bvecs(struct z_erofs_backend * be,bool * overlapped)1219 static int z_erofs_parse_in_bvecs(struct z_erofs_backend *be, bool *overlapped)
1220 {
1221 struct z_erofs_pcluster *pcl = be->pcl;
1222 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1223 int i, err = 0;
1224
1225 *overlapped = false;
1226 for (i = 0; i < pclusterpages; ++i) {
1227 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1228 struct page *page = bvec->page;
1229
1230 /* compressed data ought to be valid when decompressing */
1231 if (IS_ERR(page) || !page) {
1232 bvec->page = NULL; /* clear the failure reason */
1233 err = page ? PTR_ERR(page) : -EIO;
1234 continue;
1235 }
1236 be->compressed_pages[i] = page;
1237
1238 if (pcl->from_meta ||
1239 erofs_folio_is_managed(EROFS_SB(be->sb), page_folio(page))) {
1240 if (!PageUptodate(page))
1241 err = -EIO;
1242 continue;
1243 }
1244
1245 DBG_BUGON(z_erofs_page_is_invalidated(page));
1246 if (z_erofs_is_shortlived_page(page))
1247 continue;
1248 z_erofs_do_decompressed_bvec(be, bvec);
1249 *overlapped = true;
1250 }
1251 return err;
1252 }
1253
z_erofs_decompress_pcluster(struct z_erofs_backend * be,bool eio)1254 static int z_erofs_decompress_pcluster(struct z_erofs_backend *be, bool eio)
1255 {
1256 struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1257 struct z_erofs_pcluster *pcl = be->pcl;
1258 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1259 const struct z_erofs_decompressor *alg =
1260 z_erofs_decomp[pcl->algorithmformat];
1261 bool try_free = true;
1262 int i, j, jtop, err2, err = eio ? -EIO : 0;
1263 struct page *page;
1264 bool overlapped;
1265 const char *reason;
1266
1267 mutex_lock(&pcl->lock);
1268 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1269
1270 /* allocate (de)compressed page arrays if cannot be kept on stack */
1271 be->decompressed_pages = NULL;
1272 be->compressed_pages = NULL;
1273 be->onstack_used = 0;
1274 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1275 be->decompressed_pages = be->onstack_pages;
1276 be->onstack_used = be->nr_pages;
1277 memset(be->decompressed_pages, 0,
1278 sizeof(struct page *) * be->nr_pages);
1279 }
1280
1281 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1282 be->compressed_pages = be->onstack_pages + be->onstack_used;
1283
1284 if (!be->decompressed_pages)
1285 be->decompressed_pages =
1286 kvzalloc_objs(struct page *, be->nr_pages,
1287 GFP_KERNEL | __GFP_NOFAIL);
1288 if (!be->compressed_pages)
1289 be->compressed_pages =
1290 kvzalloc_objs(struct page *, pclusterpages,
1291 GFP_KERNEL | __GFP_NOFAIL);
1292
1293 z_erofs_parse_out_bvecs(be);
1294 err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1295 if (err2)
1296 err = err2;
1297 if (!err) {
1298 reason = alg->decompress(&(struct z_erofs_decompress_req) {
1299 .sb = be->sb,
1300 .in = be->compressed_pages,
1301 .out = be->decompressed_pages,
1302 .inpages = pclusterpages,
1303 .outpages = be->nr_pages,
1304 .pageofs_in = pcl->pageofs_in,
1305 .pageofs_out = pcl->pageofs_out,
1306 .inputsize = pcl->pclustersize,
1307 .outputsize = pcl->length,
1308 .alg = pcl->algorithmformat,
1309 .inplace_io = overlapped,
1310 .partial_decoding = pcl->partial,
1311 .fillgaps = be->keepxcpy,
1312 .gfp = pcl->besteffort ? GFP_KERNEL :
1313 GFP_NOWAIT | __GFP_NORETRY
1314 }, be->pagepool);
1315 if (IS_ERR(reason)) {
1316 if (pcl->besteffort || reason != ERR_PTR(-ENOMEM))
1317 erofs_err(be->sb, "failed to decompress (%s) %pe @ pa %llu size %u => %u",
1318 alg->name, reason, pcl->pos,
1319 pcl->pclustersize, pcl->length);
1320 err = PTR_ERR(reason);
1321 } else if (unlikely(reason)) {
1322 erofs_err(be->sb, "failed to decompress (%s) %s @ pa %llu size %u => %u",
1323 alg->name, reason, pcl->pos,
1324 pcl->pclustersize, pcl->length);
1325 err = -EFSCORRUPTED;
1326 }
1327 }
1328
1329 /* must handle all compressed pages before actual file pages */
1330 if (pcl->from_meta) {
1331 folio_put(page_folio(pcl->compressed_bvecs[0].page));
1332 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1333 } else {
1334 /* managed folios are still left in compressed_bvecs[] */
1335 for (i = 0; i < pclusterpages; ++i) {
1336 page = be->compressed_pages[i];
1337 if (!page)
1338 continue;
1339 if (erofs_folio_is_managed(sbi, page_folio(page))) {
1340 try_free = false;
1341 continue;
1342 }
1343 (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1344 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1345 }
1346 }
1347 if (be->compressed_pages < be->onstack_pages ||
1348 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1349 kvfree(be->compressed_pages);
1350
1351 jtop = 0;
1352 z_erofs_fill_other_copies(be, err);
1353 for (i = 0; i < be->nr_pages; ++i) {
1354 page = be->decompressed_pages[i];
1355 if (!page)
1356 continue;
1357
1358 DBG_BUGON(z_erofs_page_is_invalidated(page));
1359 if (!z_erofs_is_shortlived_page(page)) {
1360 erofs_onlinefolio_end(page_folio(page), err, true);
1361 continue;
1362 }
1363 if (pcl->algorithmformat != Z_EROFS_COMPRESSION_LZ4) {
1364 erofs_pagepool_add(be->pagepool, page);
1365 continue;
1366 }
1367 for (j = 0; j < jtop && be->decompressed_pages[j] != page; ++j)
1368 ;
1369 if (j >= jtop) /* this bounce page is newly detected */
1370 be->decompressed_pages[jtop++] = page;
1371 }
1372 while (jtop)
1373 erofs_pagepool_add(be->pagepool,
1374 be->decompressed_pages[--jtop]);
1375 if (be->decompressed_pages != be->onstack_pages)
1376 kvfree(be->decompressed_pages);
1377
1378 pcl->length = 0;
1379 pcl->partial = true;
1380 pcl->besteffort = false;
1381 pcl->bvset.nextpage = NULL;
1382 pcl->vcnt = 0;
1383
1384 /* pcluster lock MUST be taken before the following line */
1385 WRITE_ONCE(pcl->next, NULL);
1386 mutex_unlock(&pcl->lock);
1387
1388 if (pcl->from_meta)
1389 z_erofs_free_pcluster(pcl);
1390 else
1391 z_erofs_put_pcluster(sbi, pcl, try_free);
1392 return err;
1393 }
1394
z_erofs_decompress_queue(const struct z_erofs_decompressqueue * io,struct page ** pagepool)1395 static int z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1396 struct page **pagepool)
1397 {
1398 struct z_erofs_backend be = {
1399 .sb = io->sb,
1400 .pagepool = pagepool,
1401 .decompressed_secondary_bvecs =
1402 LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1403 .pcl = io->head,
1404 };
1405 struct z_erofs_pcluster *next;
1406 int err = 0;
1407
1408 for (; be.pcl != Z_EROFS_PCLUSTER_TAIL; be.pcl = next) {
1409 DBG_BUGON(!be.pcl);
1410 next = READ_ONCE(be.pcl->next);
1411 err = z_erofs_decompress_pcluster(&be, io->eio) ?: err;
1412 }
1413 return err;
1414 }
1415
z_erofs_decompressqueue_work(struct work_struct * work)1416 static void z_erofs_decompressqueue_work(struct work_struct *work)
1417 {
1418 struct z_erofs_decompressqueue *bgq =
1419 container_of(work, struct z_erofs_decompressqueue, u.work);
1420 struct page *pagepool = NULL;
1421
1422 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
1423 z_erofs_decompress_queue(bgq, &pagepool);
1424 erofs_release_pages(&pagepool);
1425 kvfree(bgq);
1426 }
1427
1428 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
z_erofs_decompressqueue_kthread_work(struct kthread_work * work)1429 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1430 {
1431 z_erofs_decompressqueue_work((struct work_struct *)work);
1432 }
1433 #endif
1434
1435
z_erofs_decompress_kickoff(struct z_erofs_decompressqueue * io,int bios)1436 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1437 int bios)
1438 {
1439 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1440 int gfp_flag;
1441
1442 /* wake up the caller thread for sync decompression */
1443 if (io->sync) {
1444 if (!atomic_add_return(bios, &io->pending_bios))
1445 complete(&io->u.done);
1446 return;
1447 }
1448
1449 if (atomic_add_return(bios, &io->pending_bios))
1450 return;
1451 if (bio_in_atomic()) {
1452 /* See `sync_decompress` in sysfs-fs-erofs for more details */
1453 if (sbi->sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1454 sbi->sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1455 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1456 scoped_guard(rcu) {
1457 struct kthread_worker *worker;
1458
1459 worker = rcu_dereference(
1460 z_erofs_pcpu_workers[raw_smp_processor_id()]);
1461 if (worker) {
1462 kthread_queue_work(worker, &io->u.kthread_work);
1463 return;
1464 }
1465 }
1466 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1467 #endif
1468 queue_work(z_erofs_workqueue, &io->u.work);
1469 return;
1470 }
1471 gfp_flag = memalloc_noio_save();
1472 z_erofs_decompressqueue_work(&io->u.work);
1473 memalloc_noio_restore(gfp_flag);
1474 }
1475
z_erofs_fill_bio_vec(struct bio_vec * bvec,struct z_erofs_frontend * f,struct z_erofs_pcluster * pcl,unsigned int nr,struct address_space * mc)1476 static void z_erofs_fill_bio_vec(struct bio_vec *bvec,
1477 struct z_erofs_frontend *f,
1478 struct z_erofs_pcluster *pcl,
1479 unsigned int nr,
1480 struct address_space *mc)
1481 {
1482 gfp_t gfp = mapping_gfp_mask(mc);
1483 bool tocache = false;
1484 struct z_erofs_bvec zbv;
1485 struct address_space *mapping;
1486 struct folio *folio;
1487 struct page *page;
1488 int bs = i_blocksize(f->inode);
1489
1490 /* Except for inplace folios, the entire folio can be used for I/Os */
1491 bvec->bv_offset = 0;
1492 bvec->bv_len = PAGE_SIZE;
1493 repeat:
1494 spin_lock(&pcl->lockref.lock);
1495 zbv = pcl->compressed_bvecs[nr];
1496 spin_unlock(&pcl->lockref.lock);
1497 if (!zbv.page)
1498 goto out_allocfolio;
1499
1500 bvec->bv_page = zbv.page;
1501 DBG_BUGON(z_erofs_is_shortlived_page(bvec->bv_page));
1502
1503 folio = page_folio(zbv.page);
1504 /*
1505 * Preallocated folios are added to the managed cache here rather than
1506 * in z_erofs_bind_cache() in order to keep these folios locked in
1507 * increasing (physical) address order.
1508 * Clear folio->private before these folios become visible to others in
1509 * the managed cache to avoid duplicate additions for unaligned extents.
1510 */
1511 if (folio->private == Z_EROFS_PREALLOCATED_FOLIO) {
1512 folio->private = NULL;
1513 tocache = true;
1514 goto out_tocache;
1515 }
1516
1517 mapping = READ_ONCE(folio->mapping);
1518 /*
1519 * File-backed folios for inplace I/Os are all locked steady,
1520 * therefore it is impossible for `mapping` to be NULL.
1521 */
1522 if (mapping && mapping != mc) {
1523 if (zbv.offset < 0)
1524 bvec->bv_offset = round_up(-zbv.offset, bs);
1525 bvec->bv_len = round_up(zbv.end, bs) - bvec->bv_offset;
1526 return;
1527 }
1528
1529 folio_lock(folio);
1530 if (likely(folio->mapping == mc)) {
1531 /*
1532 * The cached folio is still in managed cache but without
1533 * a valid `->private` pcluster hint. Let's reconnect them.
1534 */
1535 if (!folio_test_private(folio)) {
1536 folio_attach_private(folio, pcl);
1537 /* compressed_bvecs[] already takes a ref before */
1538 folio_put(folio);
1539 }
1540 if (likely(folio->private == pcl)) {
1541 /* don't submit cache I/Os again if already uptodate */
1542 if (folio_test_uptodate(folio)) {
1543 folio_unlock(folio);
1544 bvec->bv_page = NULL;
1545 }
1546 return;
1547 }
1548 } else {
1549 DBG_BUGON(1); /* referenced managed folios can't be truncated */
1550 }
1551 folio_unlock(folio);
1552 folio_put(folio);
1553 out_allocfolio:
1554 page = __erofs_allocpage(&f->pagepool, gfp, true);
1555 spin_lock(&pcl->lockref.lock);
1556 if (unlikely(pcl->compressed_bvecs[nr].page != zbv.page)) {
1557 if (page)
1558 erofs_pagepool_add(&f->pagepool, page);
1559 spin_unlock(&pcl->lockref.lock);
1560 cond_resched();
1561 goto repeat;
1562 }
1563 pcl->compressed_bvecs[nr].page = page ? page : ERR_PTR(-ENOMEM);
1564 spin_unlock(&pcl->lockref.lock);
1565 bvec->bv_page = page;
1566 if (!page)
1567 return;
1568 folio = page_folio(page);
1569 out_tocache:
1570 if (!tocache || bs != PAGE_SIZE ||
1571 filemap_add_folio(mc, folio, (pcl->pos >> PAGE_SHIFT) + nr, gfp)) {
1572 /* turn into a temporary shortlived folio (1 ref) */
1573 folio->private = (void *)Z_EROFS_SHORTLIVED_PAGE;
1574 return;
1575 }
1576 folio_attach_private(folio, pcl);
1577 /* drop a refcount added by allocpage (then 2 refs in total here) */
1578 folio_put(folio);
1579 }
1580
jobqueue_init(struct super_block * sb,struct z_erofs_decompressqueue * fgq,bool * fg)1581 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1582 struct z_erofs_decompressqueue *fgq, bool *fg)
1583 {
1584 struct z_erofs_decompressqueue *q;
1585
1586 if (fg && !*fg) {
1587 q = kvzalloc_obj(*q, GFP_KERNEL | __GFP_NOWARN);
1588 if (!q) {
1589 *fg = true;
1590 goto fg_out;
1591 }
1592 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1593 kthread_init_work(&q->u.kthread_work,
1594 z_erofs_decompressqueue_kthread_work);
1595 #else
1596 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1597 #endif
1598 } else {
1599 fg_out:
1600 q = fgq;
1601 init_completion(&fgq->u.done);
1602 atomic_set(&fgq->pending_bios, 0);
1603 q->eio = false;
1604 q->sync = true;
1605 }
1606 q->sb = sb;
1607 q->head = Z_EROFS_PCLUSTER_TAIL;
1608 return q;
1609 }
1610
1611 /* define decompression jobqueue types */
1612 enum {
1613 JQ_BYPASS,
1614 JQ_SUBMIT,
1615 NR_JOBQUEUES,
1616 };
1617
z_erofs_move_to_bypass_queue(struct z_erofs_pcluster * pcl,struct z_erofs_pcluster * next,struct z_erofs_pcluster ** qtail[])1618 static void z_erofs_move_to_bypass_queue(struct z_erofs_pcluster *pcl,
1619 struct z_erofs_pcluster *next,
1620 struct z_erofs_pcluster **qtail[])
1621 {
1622 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
1623 WRITE_ONCE(*qtail[JQ_SUBMIT], next);
1624 WRITE_ONCE(*qtail[JQ_BYPASS], pcl);
1625 qtail[JQ_BYPASS] = &pcl->next;
1626 }
1627
z_erofs_endio(struct bio * bio)1628 static void z_erofs_endio(struct bio *bio)
1629 {
1630 struct z_erofs_decompressqueue *q = bio->bi_private;
1631 blk_status_t err = bio->bi_status;
1632 struct folio_iter fi;
1633
1634 bio_for_each_folio_all(fi, bio) {
1635 struct folio *folio = fi.folio;
1636
1637 DBG_BUGON(folio_test_uptodate(folio));
1638 DBG_BUGON(z_erofs_page_is_invalidated(&folio->page));
1639 if (!erofs_folio_is_managed(EROFS_SB(q->sb), folio))
1640 continue;
1641
1642 if (!err)
1643 folio_mark_uptodate(folio);
1644 folio_unlock(folio);
1645 }
1646 if (err)
1647 q->eio = true;
1648 z_erofs_decompress_kickoff(q, -1);
1649 if (bio->bi_bdev)
1650 bio_put(bio);
1651 }
1652
z_erofs_submit_queue(struct z_erofs_frontend * f,struct z_erofs_decompressqueue * fgq,bool * force_fg,bool readahead)1653 static void z_erofs_submit_queue(struct z_erofs_frontend *f,
1654 struct z_erofs_decompressqueue *fgq,
1655 bool *force_fg, bool readahead)
1656 {
1657 struct super_block *sb = f->inode->i_sb;
1658 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1659 struct z_erofs_pcluster **qtail[NR_JOBQUEUES];
1660 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1661 struct z_erofs_pcluster *pcl, *next;
1662 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1663 erofs_off_t last_pa;
1664 unsigned int nr_bios = 0;
1665 struct bio *bio = NULL;
1666 unsigned long pflags;
1667 int memstall = 0;
1668
1669 /* No need to read from device for pclusters in the bypass queue. */
1670 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1671 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1672
1673 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1674 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1675
1676 /* by default, all need io submission */
1677 q[JQ_SUBMIT]->head = next = f->head;
1678
1679 do {
1680 struct erofs_map_dev mdev;
1681 erofs_off_t cur, end;
1682 struct bio_vec bvec;
1683 unsigned int i = 0;
1684 bool bypass = true;
1685
1686 pcl = next;
1687 next = READ_ONCE(pcl->next);
1688 if (pcl->from_meta) {
1689 z_erofs_move_to_bypass_queue(pcl, next, qtail);
1690 continue;
1691 }
1692
1693 /* no device id here, thus it will always succeed */
1694 mdev = (struct erofs_map_dev) {
1695 .m_pa = round_down(pcl->pos, sb->s_blocksize),
1696 };
1697 (void)erofs_map_dev(sb, &mdev);
1698
1699 cur = mdev.m_pa;
1700 end = round_up(cur + pcl->pageofs_in + pcl->pclustersize,
1701 sb->s_blocksize);
1702 do {
1703 bvec.bv_page = NULL;
1704 if (bio && (cur != last_pa ||
1705 bio->bi_bdev != mdev.m_bdev)) {
1706 drain_io:
1707 if (erofs_is_fileio_mode(EROFS_SB(sb)))
1708 erofs_fileio_submit_bio(bio);
1709 else
1710 submit_bio(bio);
1711
1712 if (memstall) {
1713 psi_memstall_leave(&pflags);
1714 memstall = 0;
1715 }
1716 bio = NULL;
1717 }
1718
1719 if (!bvec.bv_page) {
1720 z_erofs_fill_bio_vec(&bvec, f, pcl, i++, mc);
1721 if (!bvec.bv_page)
1722 continue;
1723 if (cur + bvec.bv_len > end)
1724 bvec.bv_len = end - cur;
1725 DBG_BUGON(bvec.bv_len < sb->s_blocksize);
1726 }
1727
1728 if (unlikely(PageWorkingset(bvec.bv_page)) &&
1729 !memstall) {
1730 psi_memstall_enter(&pflags);
1731 memstall = 1;
1732 }
1733
1734 if (!bio) {
1735 if (erofs_is_fileio_mode(EROFS_SB(sb)))
1736 bio = erofs_fileio_bio_alloc(&mdev);
1737 else
1738 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1739 REQ_OP_READ, GFP_NOIO);
1740 bio->bi_end_io = z_erofs_endio;
1741 bio->bi_iter.bi_sector =
1742 (mdev.m_dif->fsoff + cur) >> 9;
1743 bio->bi_private = q[JQ_SUBMIT];
1744 if (readahead)
1745 bio->bi_opf |= REQ_RAHEAD;
1746 ++nr_bios;
1747 }
1748
1749 if (!bio_add_page(bio, bvec.bv_page, bvec.bv_len,
1750 bvec.bv_offset))
1751 goto drain_io;
1752 last_pa = cur + bvec.bv_len;
1753 bypass = false;
1754 } while ((cur += bvec.bv_len) < end);
1755
1756 if (!bypass)
1757 qtail[JQ_SUBMIT] = &pcl->next;
1758 else
1759 z_erofs_move_to_bypass_queue(pcl, next, qtail);
1760 } while (next != Z_EROFS_PCLUSTER_TAIL);
1761
1762 if (bio) {
1763 if (erofs_is_fileio_mode(EROFS_SB(sb)))
1764 erofs_fileio_submit_bio(bio);
1765 else
1766 submit_bio(bio);
1767 }
1768 if (memstall)
1769 psi_memstall_leave(&pflags);
1770
1771 /*
1772 * although background is preferred, no one is pending for submission.
1773 * don't issue decompression but drop it directly instead.
1774 */
1775 if (!*force_fg && !nr_bios) {
1776 kvfree(q[JQ_SUBMIT]);
1777 return;
1778 }
1779 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1780 }
1781
z_erofs_runqueue(struct z_erofs_frontend * f,unsigned int rabytes)1782 static int z_erofs_runqueue(struct z_erofs_frontend *f, unsigned int rabytes)
1783 {
1784 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1785 struct erofs_sb_info *sbi = EROFS_I_SB(f->inode);
1786 int syncmode = sbi->sync_decompress;
1787 bool force_fg;
1788 int err;
1789
1790 force_fg = (syncmode == EROFS_SYNC_DECOMPRESS_AUTO && !rabytes) ||
1791 (syncmode == EROFS_SYNC_DECOMPRESS_FORCE_ON &&
1792 (rabytes <= Z_EROFS_MAX_SYNC_DECOMPRESS_BYTES));
1793
1794 if (f->head == Z_EROFS_PCLUSTER_TAIL)
1795 return 0;
1796 z_erofs_submit_queue(f, io, &force_fg, !!rabytes);
1797
1798 /* handle bypass queue (no i/o pclusters) immediately */
1799 err = z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
1800 if (!force_fg)
1801 return err;
1802
1803 /* wait until all bios are completed */
1804 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1805
1806 /* handle synchronous decompress queue in the caller context */
1807 return z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool) ?: err;
1808 }
1809
1810 /*
1811 * Since partial uptodate is still unimplemented for now, we have to use
1812 * approximate readmore strategies as a start.
1813 */
z_erofs_pcluster_readmore(struct z_erofs_frontend * f,struct readahead_control * rac,bool backmost)1814 static void z_erofs_pcluster_readmore(struct z_erofs_frontend *f,
1815 struct readahead_control *rac, bool backmost)
1816 {
1817 struct inode *inode = f->inode;
1818 struct erofs_map_blocks *map = &f->map;
1819 erofs_off_t cur, end, headoffset = f->headoffset;
1820 int err;
1821
1822 if (backmost) {
1823 if (rac)
1824 end = headoffset + readahead_length(rac) - 1;
1825 else
1826 end = headoffset + PAGE_SIZE - 1;
1827 map->m_la = end;
1828 err = z_erofs_map_blocks_iter(inode, map,
1829 EROFS_GET_BLOCKS_READMORE);
1830 if (err || !(map->m_flags & EROFS_MAP_MAPPED))
1831 return;
1832
1833 /* expand ra for the trailing edge if readahead */
1834 if (rac) {
1835 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1836 readahead_expand(rac, headoffset, cur - headoffset);
1837 return;
1838 }
1839 end = round_up(end, PAGE_SIZE);
1840 } else {
1841 end = round_up(map->m_la, PAGE_SIZE);
1842 if (!(map->m_flags & EROFS_MAP_MAPPED) || !map->m_llen)
1843 return;
1844 }
1845
1846 cur = map->m_la + map->m_llen - 1;
1847 while ((cur >= end) && (cur < i_size_read(inode))) {
1848 pgoff_t index = cur >> PAGE_SHIFT;
1849 struct folio *folio;
1850
1851 folio = erofs_grab_folio_nowait(f->sharedinode->i_mapping, index);
1852 if (!IS_ERR_OR_NULL(folio)) {
1853 if (folio_test_uptodate(folio))
1854 folio_unlock(folio);
1855 else
1856 z_erofs_scan_folio(f, folio, !!rac);
1857 folio_put(folio);
1858 }
1859
1860 if (cur < PAGE_SIZE)
1861 break;
1862 cur = ((loff_t)index << PAGE_SHIFT) - 1;
1863 }
1864 }
1865
z_erofs_read_folio(struct file * file,struct folio * folio)1866 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1867 {
1868 struct inode *sharedinode = folio->mapping->host;
1869 bool need_iput;
1870 struct inode *realinode = erofs_real_inode(sharedinode, &need_iput);
1871 Z_EROFS_DEFINE_FRONTEND(f, realinode, sharedinode, folio_pos(folio));
1872 int err;
1873
1874 trace_erofs_read_folio(realinode, folio, false);
1875 z_erofs_pcluster_readmore(&f, NULL, true);
1876 err = z_erofs_scan_folio(&f, folio, false);
1877 z_erofs_pcluster_readmore(&f, NULL, false);
1878 z_erofs_pcluster_end(&f);
1879
1880 /* if some pclusters are ready, need submit them anyway */
1881 err = z_erofs_runqueue(&f, 0) ?: err;
1882 if (err && err != -EINTR)
1883 erofs_err(realinode->i_sb, "read error %d @ %lu of nid %llu",
1884 err, folio->index, EROFS_I(realinode)->nid);
1885
1886 erofs_put_metabuf(&f.map.buf);
1887 erofs_release_pages(&f.pagepool);
1888
1889 if (need_iput)
1890 iput(realinode);
1891 return err;
1892 }
1893
z_erofs_readahead(struct readahead_control * rac)1894 static void z_erofs_readahead(struct readahead_control *rac)
1895 {
1896 struct inode *sharedinode = rac->mapping->host;
1897 bool need_iput;
1898 struct inode *realinode = erofs_real_inode(sharedinode, &need_iput);
1899 Z_EROFS_DEFINE_FRONTEND(f, realinode, sharedinode, readahead_pos(rac));
1900 unsigned int nrpages = readahead_count(rac);
1901 struct folio *head = NULL, *folio;
1902 int err;
1903
1904 trace_erofs_readahead(realinode, readahead_index(rac), nrpages, false);
1905 z_erofs_pcluster_readmore(&f, rac, true);
1906 while ((folio = readahead_folio(rac))) {
1907 folio->private = head;
1908 head = folio;
1909 }
1910
1911 /* traverse in reverse order for best metadata I/O performance */
1912 while (head) {
1913 folio = head;
1914 head = folio_get_private(folio);
1915
1916 err = z_erofs_scan_folio(&f, folio, true);
1917 if (err && err != -EINTR)
1918 erofs_err(realinode->i_sb, "readahead error at folio %lu @ nid %llu",
1919 folio->index, EROFS_I(realinode)->nid);
1920 }
1921 z_erofs_pcluster_readmore(&f, rac, false);
1922 z_erofs_pcluster_end(&f);
1923
1924 (void)z_erofs_runqueue(&f, nrpages << PAGE_SHIFT);
1925 erofs_put_metabuf(&f.map.buf);
1926 erofs_release_pages(&f.pagepool);
1927
1928 if (need_iput)
1929 iput(realinode);
1930 }
1931
1932 const struct address_space_operations z_erofs_aops = {
1933 .read_folio = z_erofs_read_folio,
1934 .readahead = z_erofs_readahead,
1935 };
1936