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