1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2022 Alibaba Cloud
9 #include <linux/cpuhotplug.h>
10 #include <trace/events/erofs.h>
12 #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
13 #define Z_EROFS_INLINE_BVECS 2
16 * let's leave a type here in case of introducing
17 * another tagged pointer later.
19 typedef void *z_erofs_next_pcluster_t;
27 #define __Z_EROFS_BVSET(name, total) \
29 /* point to the next page which contains the following bvecs */ \
30 struct page *nextpage; \
31 struct z_erofs_bvec bvec[total]; \
33 __Z_EROFS_BVSET(z_erofs_bvset,);
34 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
37 * Structure fields follow one of the following exclusion rules.
39 * I: Modifiable by initialization/destruction paths and read-only
42 * L: Field should be protected by the pcluster lock;
44 * A: Field should be accessed / updated in atomic for parallelized code.
46 struct z_erofs_pcluster {
47 struct erofs_workgroup obj;
50 /* A: point to next chained pcluster or TAILs */
51 z_erofs_next_pcluster_t next;
53 /* L: the maximum decompression size of this round */
56 /* L: total number of bvecs */
59 /* I: page offset of start position of decompression */
60 unsigned short pageofs_out;
62 /* I: page offset of inline compressed data */
63 unsigned short pageofs_in;
66 /* L: inline a certain number of bvec for bootstrap */
67 struct z_erofs_bvset_inline bvset;
69 /* I: can be used to free the pcluster by RCU. */
74 /* I: physical cluster size in pages */
75 unsigned short pclusterpages;
77 /* I: tailpacking inline compressed size */
78 unsigned short tailpacking_size;
81 /* I: compression algorithm format */
82 unsigned char algorithmformat;
84 /* L: whether partial decompression or not */
87 /* L: indicate several pageofs_outs or not */
90 /* A: compressed bvecs (can be cached or inplaced pages) */
91 struct z_erofs_bvec compressed_bvecs[];
94 /* the end of a chain of pclusters */
95 #define Z_EROFS_PCLUSTER_TAIL ((void *) 0x700 + POISON_POINTER_DELTA)
96 #define Z_EROFS_PCLUSTER_NIL (NULL)
98 struct z_erofs_decompressqueue {
99 struct super_block *sb;
100 atomic_t pending_bios;
101 z_erofs_next_pcluster_t head;
104 struct completion done;
105 struct work_struct work;
106 struct kthread_work kthread_work;
111 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
113 return !pcl->obj.index;
116 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
118 if (z_erofs_is_inline_pcluster(pcl))
120 return pcl->pclusterpages;
124 * bit 30: I/O error occurred on this page
125 * bit 0 - 29: remaining parts to complete this page
127 #define Z_EROFS_PAGE_EIO (1 << 30)
129 static inline void z_erofs_onlinepage_init(struct page *page)
134 } u = { .o = ATOMIC_INIT(1) };
136 set_page_private(page, u.v);
138 SetPagePrivate(page);
141 static inline void z_erofs_onlinepage_split(struct page *page)
143 atomic_inc((atomic_t *)&page->private);
146 static void z_erofs_onlinepage_endio(struct page *page, int err)
150 DBG_BUGON(!PagePrivate(page));
153 orig = atomic_read((atomic_t *)&page->private);
154 v = (orig - 1) | (err ? Z_EROFS_PAGE_EIO : 0);
155 } while (atomic_cmpxchg((atomic_t *)&page->private, orig, v) != orig);
157 if (!(v & ~Z_EROFS_PAGE_EIO)) {
158 set_page_private(page, 0);
159 ClearPagePrivate(page);
160 if (!(v & Z_EROFS_PAGE_EIO))
161 SetPageUptodate(page);
166 #define Z_EROFS_ONSTACK_PAGES 32
169 * since pclustersize is variable for big pcluster feature, introduce slab
170 * pools implementation for different pcluster sizes.
172 struct z_erofs_pcluster_slab {
173 struct kmem_cache *slab;
174 unsigned int maxpages;
178 #define _PCLP(n) { .maxpages = n }
180 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
181 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
182 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
185 struct z_erofs_bvec_iter {
187 struct z_erofs_bvset *bvset;
188 unsigned int nr, cur;
191 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
194 kunmap_local(iter->bvset);
198 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
200 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
201 /* have to access nextpage in advance, otherwise it will be unmapped */
202 struct page *nextpage = iter->bvset->nextpage;
203 struct page *oldpage;
205 DBG_BUGON(!nextpage);
206 oldpage = z_erofs_bvec_iter_end(iter);
207 iter->bvpage = nextpage;
208 iter->bvset = kmap_local_page(nextpage);
209 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
214 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
215 struct z_erofs_bvset_inline *bvset,
216 unsigned int bootstrap_nr,
219 *iter = (struct z_erofs_bvec_iter) {
221 .bvset = (struct z_erofs_bvset *)bvset,
224 while (cur > iter->nr) {
226 z_erofs_bvset_flip(iter);
231 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
232 struct z_erofs_bvec *bvec,
233 struct page **candidate_bvpage,
234 struct page **pagepool)
236 if (iter->cur >= iter->nr) {
237 struct page *nextpage = *candidate_bvpage;
240 nextpage = erofs_allocpage(pagepool, GFP_NOFS);
243 set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
245 DBG_BUGON(iter->bvset->nextpage);
246 iter->bvset->nextpage = nextpage;
247 z_erofs_bvset_flip(iter);
249 iter->bvset->nextpage = NULL;
250 *candidate_bvpage = NULL;
252 iter->bvset->bvec[iter->cur++] = *bvec;
256 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
257 struct z_erofs_bvec *bvec,
258 struct page **old_bvpage)
260 if (iter->cur == iter->nr)
261 *old_bvpage = z_erofs_bvset_flip(iter);
264 *bvec = iter->bvset->bvec[iter->cur++];
267 static void z_erofs_destroy_pcluster_pool(void)
271 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
272 if (!pcluster_pool[i].slab)
274 kmem_cache_destroy(pcluster_pool[i].slab);
275 pcluster_pool[i].slab = NULL;
279 static int z_erofs_create_pcluster_pool(void)
281 struct z_erofs_pcluster_slab *pcs;
282 struct z_erofs_pcluster *a;
285 for (pcs = pcluster_pool;
286 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
287 size = struct_size(a, compressed_bvecs, pcs->maxpages);
289 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
290 pcs->slab = kmem_cache_create(pcs->name, size, 0,
291 SLAB_RECLAIM_ACCOUNT, NULL);
295 z_erofs_destroy_pcluster_pool();
301 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
305 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
306 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
307 struct z_erofs_pcluster *pcl;
309 if (nrpages > pcs->maxpages)
312 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
314 return ERR_PTR(-ENOMEM);
315 pcl->pclusterpages = nrpages;
318 return ERR_PTR(-EINVAL);
321 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
323 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
326 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
327 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
329 if (pclusterpages > pcs->maxpages)
332 kmem_cache_free(pcs->slab, pcl);
338 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
340 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
341 static struct kthread_worker __rcu **z_erofs_pcpu_workers;
343 static void erofs_destroy_percpu_workers(void)
345 struct kthread_worker *worker;
348 for_each_possible_cpu(cpu) {
349 worker = rcu_dereference_protected(
350 z_erofs_pcpu_workers[cpu], 1);
351 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
353 kthread_destroy_worker(worker);
355 kfree(z_erofs_pcpu_workers);
358 static struct kthread_worker *erofs_init_percpu_worker(int cpu)
360 struct kthread_worker *worker =
361 kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
365 if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
366 sched_set_fifo_low(worker->task);
370 static int erofs_init_percpu_workers(void)
372 struct kthread_worker *worker;
375 z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
376 sizeof(struct kthread_worker *), GFP_ATOMIC);
377 if (!z_erofs_pcpu_workers)
380 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
381 worker = erofs_init_percpu_worker(cpu);
383 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
388 static inline void erofs_destroy_percpu_workers(void) {}
389 static inline int erofs_init_percpu_workers(void) { return 0; }
392 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
393 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
394 static enum cpuhp_state erofs_cpuhp_state;
396 static int erofs_cpu_online(unsigned int cpu)
398 struct kthread_worker *worker, *old;
400 worker = erofs_init_percpu_worker(cpu);
402 return PTR_ERR(worker);
404 spin_lock(&z_erofs_pcpu_worker_lock);
405 old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
406 lockdep_is_held(&z_erofs_pcpu_worker_lock));
408 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
409 spin_unlock(&z_erofs_pcpu_worker_lock);
411 kthread_destroy_worker(worker);
415 static int erofs_cpu_offline(unsigned int cpu)
417 struct kthread_worker *worker;
419 spin_lock(&z_erofs_pcpu_worker_lock);
420 worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
421 lockdep_is_held(&z_erofs_pcpu_worker_lock));
422 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
423 spin_unlock(&z_erofs_pcpu_worker_lock);
427 kthread_destroy_worker(worker);
431 static int erofs_cpu_hotplug_init(void)
435 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
436 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
440 erofs_cpuhp_state = state;
444 static void erofs_cpu_hotplug_destroy(void)
446 if (erofs_cpuhp_state)
447 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
449 #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
450 static inline int erofs_cpu_hotplug_init(void) { return 0; }
451 static inline void erofs_cpu_hotplug_destroy(void) {}
454 void z_erofs_exit_zip_subsystem(void)
456 erofs_cpu_hotplug_destroy();
457 erofs_destroy_percpu_workers();
458 destroy_workqueue(z_erofs_workqueue);
459 z_erofs_destroy_pcluster_pool();
462 int __init z_erofs_init_zip_subsystem(void)
464 int err = z_erofs_create_pcluster_pool();
467 goto out_error_pcluster_pool;
469 z_erofs_workqueue = alloc_workqueue("erofs_worker",
470 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
471 if (!z_erofs_workqueue) {
473 goto out_error_workqueue_init;
476 err = erofs_init_percpu_workers();
478 goto out_error_pcpu_worker;
480 err = erofs_cpu_hotplug_init();
482 goto out_error_cpuhp_init;
485 out_error_cpuhp_init:
486 erofs_destroy_percpu_workers();
487 out_error_pcpu_worker:
488 destroy_workqueue(z_erofs_workqueue);
489 out_error_workqueue_init:
490 z_erofs_destroy_pcluster_pool();
491 out_error_pcluster_pool:
495 enum z_erofs_pclustermode {
496 Z_EROFS_PCLUSTER_INFLIGHT,
498 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
499 * could be dispatched into bypass queue later due to uptodated managed
500 * pages. All related online pages cannot be reused for inplace I/O (or
501 * bvpage) since it can be directly decoded without I/O submission.
503 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
505 * The pcluster was just linked to a decompression chain by us. It can
506 * also be linked with the remaining pclusters, which means if the
507 * processing page is the tail page of a pcluster, this pcluster can
508 * safely use the whole page (since the previous pcluster is within the
509 * same chain) for in-place I/O, as illustrated below:
510 * ___________________________________________________
511 * | tail (partial) page | head (partial) page |
512 * | (of the current pcl) | (of the previous pcl) |
513 * |___PCLUSTER_FOLLOWED___|_____PCLUSTER_FOLLOWED_____|
515 * [ (*) the page above can be used as inplace I/O. ]
517 Z_EROFS_PCLUSTER_FOLLOWED,
520 struct z_erofs_decompress_frontend {
521 struct inode *const inode;
522 struct erofs_map_blocks map;
523 struct z_erofs_bvec_iter biter;
525 struct page *pagepool;
526 struct page *candidate_bvpage;
527 struct z_erofs_pcluster *pcl;
528 z_erofs_next_pcluster_t owned_head;
529 enum z_erofs_pclustermode mode;
531 erofs_off_t headoffset;
533 /* a pointer used to pick up inplace I/O pages */
537 #define DECOMPRESS_FRONTEND_INIT(__i) { \
538 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
539 .mode = Z_EROFS_PCLUSTER_FOLLOWED }
541 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
543 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
545 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
548 if (!(fe->map.m_flags & EROFS_MAP_FULL_MAPPED))
551 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
552 fe->map.m_la < fe->headoffset)
558 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe)
560 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
561 struct z_erofs_pcluster *pcl = fe->pcl;
562 bool shouldalloc = z_erofs_should_alloc_cache(fe);
563 bool standalone = true;
565 * optimistic allocation without direct reclaim since inplace I/O
566 * can be used if low memory otherwise.
568 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
569 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
572 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
575 for (i = 0; i < pcl->pclusterpages; ++i) {
577 void *t; /* mark pages just found for debugging */
578 struct page *newpage = NULL;
580 /* the compressed page was loaded before */
581 if (READ_ONCE(pcl->compressed_bvecs[i].page))
584 page = find_get_page(mc, pcl->obj.index + i);
587 t = (void *)((unsigned long)page | 1);
589 /* I/O is needed, no possible to decompress directly */
595 * try to use cached I/O if page allocation
596 * succeeds or fallback to in-place I/O instead
597 * to avoid any direct reclaim.
599 newpage = erofs_allocpage(&fe->pagepool, gfp);
602 set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
603 t = (void *)((unsigned long)newpage | 1);
606 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
612 erofs_pagepool_add(&fe->pagepool, newpage);
616 * don't do inplace I/O if all compressed pages are available in
617 * managed cache since it can be moved to the bypass queue instead.
620 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
623 /* called by erofs_shrinker to get rid of all compressed_pages */
624 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
625 struct erofs_workgroup *grp)
627 struct z_erofs_pcluster *const pcl =
628 container_of(grp, struct z_erofs_pcluster, obj);
631 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
633 * refcount of workgroup is now freezed as 0,
634 * therefore no need to worry about available decompression users.
636 for (i = 0; i < pcl->pclusterpages; ++i) {
637 struct page *page = pcl->compressed_bvecs[i].page;
642 /* block other users from reclaiming or migrating the page */
643 if (!trylock_page(page))
646 if (!erofs_page_is_managed(sbi, page))
649 /* barrier is implied in the following 'unlock_page' */
650 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
651 detach_page_private(page);
657 static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)
659 struct z_erofs_pcluster *pcl = folio_get_private(folio);
663 if (!folio_test_private(folio))
667 spin_lock(&pcl->obj.lockref.lock);
668 if (pcl->obj.lockref.count > 0)
671 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
672 for (i = 0; i < pcl->pclusterpages; ++i) {
673 if (pcl->compressed_bvecs[i].page == &folio->page) {
674 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
680 folio_detach_private(folio);
682 spin_unlock(&pcl->obj.lockref.lock);
687 * It will be called only on inode eviction. In case that there are still some
688 * decompression requests in progress, wait with rescheduling for a bit here.
689 * An extra lock could be introduced instead but it seems unnecessary.
691 static void z_erofs_cache_invalidate_folio(struct folio *folio,
692 size_t offset, size_t length)
694 const size_t stop = length + offset;
696 /* Check for potential overflow in debug mode */
697 DBG_BUGON(stop > folio_size(folio) || stop < length);
699 if (offset == 0 && stop == folio_size(folio))
700 while (!z_erofs_cache_release_folio(folio, GFP_NOFS))
704 static const struct address_space_operations z_erofs_cache_aops = {
705 .release_folio = z_erofs_cache_release_folio,
706 .invalidate_folio = z_erofs_cache_invalidate_folio,
709 int erofs_init_managed_cache(struct super_block *sb)
711 struct inode *const inode = new_inode(sb);
717 inode->i_size = OFFSET_MAX;
718 inode->i_mapping->a_ops = &z_erofs_cache_aops;
719 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
720 EROFS_SB(sb)->managed_cache = inode;
724 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
725 struct z_erofs_bvec *bvec)
727 struct z_erofs_pcluster *const pcl = fe->pcl;
729 while (fe->icur > 0) {
730 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
732 pcl->compressed_bvecs[fe->icur] = *bvec;
739 /* callers must be with pcluster lock held */
740 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
741 struct z_erofs_bvec *bvec, bool exclusive)
746 /* give priority for inplaceio to use file pages first */
747 if (z_erofs_try_inplace_io(fe, bvec))
749 /* otherwise, check if it can be used as a bvpage */
750 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
751 !fe->candidate_bvpage)
752 fe->candidate_bvpage = bvec->page;
754 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
756 fe->pcl->vcnt += (ret >= 0);
760 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
762 struct z_erofs_pcluster *pcl = f->pcl;
763 z_erofs_next_pcluster_t *owned_head = &f->owned_head;
765 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
766 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
767 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
768 *owned_head = &pcl->next;
769 /* so we can attach this pcluster to our submission chain. */
770 f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
774 /* type 2, it belongs to an ongoing chain */
775 f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
778 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
780 struct erofs_map_blocks *map = &fe->map;
781 bool ztailpacking = map->m_flags & EROFS_MAP_META;
782 struct z_erofs_pcluster *pcl;
783 struct erofs_workgroup *grp;
786 if (!(map->m_flags & EROFS_MAP_ENCODED) ||
787 (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
789 return -EFSCORRUPTED;
792 /* no available pcluster, let's allocate one */
793 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
794 map->m_plen >> PAGE_SHIFT);
798 spin_lock_init(&pcl->obj.lockref.lock);
799 pcl->algorithmformat = map->m_algorithmformat;
803 /* new pclusters should be claimed as type 1, primary and followed */
804 pcl->next = fe->owned_head;
805 pcl->pageofs_out = map->m_la & ~PAGE_MASK;
806 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
809 * lock all primary followed works before visible to others
810 * and mutex_trylock *never* fails for a new pcluster.
812 mutex_init(&pcl->lock);
813 DBG_BUGON(!mutex_trylock(&pcl->lock));
816 pcl->obj.index = 0; /* which indicates ztailpacking */
817 pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa);
818 pcl->tailpacking_size = map->m_plen;
820 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
822 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
828 if (grp != &pcl->obj) {
829 fe->pcl = container_of(grp,
830 struct z_erofs_pcluster, obj);
835 fe->owned_head = &pcl->next;
840 mutex_unlock(&pcl->lock);
841 z_erofs_free_pcluster(pcl);
845 static int z_erofs_pcluster_begin(struct z_erofs_decompress_frontend *fe)
847 struct erofs_map_blocks *map = &fe->map;
848 struct super_block *sb = fe->inode->i_sb;
849 erofs_blk_t blknr = erofs_blknr(sb, map->m_pa);
850 struct erofs_workgroup *grp = NULL;
855 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
856 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
858 if (!(map->m_flags & EROFS_MAP_META)) {
859 grp = erofs_find_workgroup(sb, blknr);
860 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
862 return -EFSCORRUPTED;
866 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
869 ret = z_erofs_register_pcluster(fe);
872 if (ret == -EEXIST) {
873 mutex_lock(&fe->pcl->lock);
874 z_erofs_try_to_claim_pcluster(fe);
879 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
880 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
881 if (!z_erofs_is_inline_pcluster(fe->pcl)) {
882 /* bind cache first when cached decompression is preferred */
883 z_erofs_bind_cache(fe);
887 mptr = erofs_read_metabuf(&map->buf, sb, blknr, EROFS_NO_KMAP);
890 erofs_err(sb, "failed to get inline data %d", ret);
893 get_page(map->buf.page);
894 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, map->buf.page);
895 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
897 /* file-backed inplace I/O pages are traversed in reverse order */
898 fe->icur = z_erofs_pclusterpages(fe->pcl);
903 * keep in mind that no referenced pclusters will be freed
904 * only after a RCU grace period.
906 static void z_erofs_rcu_callback(struct rcu_head *head)
908 z_erofs_free_pcluster(container_of(head,
909 struct z_erofs_pcluster, rcu));
912 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
914 struct z_erofs_pcluster *const pcl =
915 container_of(grp, struct z_erofs_pcluster, obj);
917 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
920 static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe)
922 struct z_erofs_pcluster *pcl = fe->pcl;
927 z_erofs_bvec_iter_end(&fe->biter);
928 mutex_unlock(&pcl->lock);
930 if (fe->candidate_bvpage)
931 fe->candidate_bvpage = NULL;
934 * if all pending pages are added, don't hold its reference
935 * any longer if the pcluster isn't hosted by ourselves.
937 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
938 erofs_workgroup_put(&pcl->obj);
943 static int z_erofs_read_fragment(struct super_block *sb, struct page *page,
944 unsigned int cur, unsigned int end, erofs_off_t pos)
946 struct inode *packed_inode = EROFS_SB(sb)->packed_inode;
947 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
952 return -EFSCORRUPTED;
954 buf.inode = packed_inode;
955 for (; cur < end; cur += cnt, pos += cnt) {
956 cnt = min_t(unsigned int, end - cur,
957 sb->s_blocksize - erofs_blkoff(sb, pos));
958 src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
960 erofs_put_metabuf(&buf);
963 memcpy_to_page(page, cur, src + erofs_blkoff(sb, pos), cnt);
965 erofs_put_metabuf(&buf);
969 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
972 struct inode *const inode = fe->inode;
973 struct erofs_map_blocks *const map = &fe->map;
974 const loff_t offset = page_offset(page);
975 bool tight = true, exclusive;
976 unsigned int cur, end, len, split;
979 z_erofs_onlinepage_init(page);
984 if (offset + end - 1 < map->m_la ||
985 offset + end - 1 >= map->m_la + map->m_llen) {
986 z_erofs_pcluster_end(fe);
987 map->m_la = offset + end - 1;
989 err = z_erofs_map_blocks_iter(inode, map, 0);
994 cur = offset > map->m_la ? 0 : map->m_la - offset;
995 /* bump split parts first to avoid several separate cases */
998 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
999 zero_user_segment(page, cur, end);
1004 if (map->m_flags & EROFS_MAP_FRAGMENT) {
1005 erofs_off_t fpos = offset + cur - map->m_la;
1007 len = min_t(unsigned int, map->m_llen - fpos, end - cur);
1008 err = z_erofs_read_fragment(inode->i_sb, page, cur, cur + len,
1009 EROFS_I(inode)->z_fragmentoff + fpos);
1017 err = z_erofs_pcluster_begin(fe);
1023 * Ensure the current partial page belongs to this submit chain rather
1024 * than other concurrent submit chains or the noio(bypass) chain since
1025 * those chains are handled asynchronously thus the page cannot be used
1026 * for inplace I/O or bvpage (should be processed in a strict order.)
1028 tight &= (fe->mode > Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1029 exclusive = (!cur && ((split <= 1) || tight));
1031 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1033 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
1035 .offset = offset - map->m_la,
1041 z_erofs_onlinepage_split(page);
1042 if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1043 fe->pcl->multibases = true;
1044 if (fe->pcl->length < offset + end - map->m_la) {
1045 fe->pcl->length = offset + end - map->m_la;
1046 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1048 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1049 !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1050 fe->pcl->length == map->m_llen)
1051 fe->pcl->partial = false;
1053 /* shorten the remaining extent to update progress */
1054 map->m_llen = offset + cur - map->m_la;
1055 map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
1062 z_erofs_onlinepage_endio(page, err);
1066 static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
1067 unsigned int readahead_pages)
1069 /* auto: enable for read_folio, disable for readahead */
1070 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
1074 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
1075 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
1081 static bool z_erofs_page_is_invalidated(struct page *page)
1083 return !page->mapping && !z_erofs_is_shortlived_page(page);
1086 struct z_erofs_decompress_backend {
1087 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1088 struct super_block *sb;
1089 struct z_erofs_pcluster *pcl;
1091 /* pages with the longest decompressed length for deduplication */
1092 struct page **decompressed_pages;
1093 /* pages to keep the compressed data */
1094 struct page **compressed_pages;
1096 struct list_head decompressed_secondary_bvecs;
1097 struct page **pagepool;
1098 unsigned int onstack_used, nr_pages;
1101 struct z_erofs_bvec_item {
1102 struct z_erofs_bvec bvec;
1103 struct list_head list;
1106 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
1107 struct z_erofs_bvec *bvec)
1109 struct z_erofs_bvec_item *item;
1112 if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK) &&
1113 (bvec->end == PAGE_SIZE ||
1114 bvec->offset + bvec->end == be->pcl->length)) {
1115 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
1116 DBG_BUGON(pgnr >= be->nr_pages);
1117 if (!be->decompressed_pages[pgnr]) {
1118 be->decompressed_pages[pgnr] = bvec->page;
1123 /* (cold path) one pcluster is requested multiple times */
1124 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1126 list_add(&item->list, &be->decompressed_secondary_bvecs);
1129 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1132 unsigned int off0 = be->pcl->pageofs_out;
1133 struct list_head *p, *n;
1135 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1136 struct z_erofs_bvec_item *bvi;
1137 unsigned int end, cur;
1140 bvi = container_of(p, struct z_erofs_bvec_item, list);
1141 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1142 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1144 dst = kmap_local_page(bvi->bvec.page);
1146 unsigned int pgnr, scur, len;
1148 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1149 DBG_BUGON(pgnr >= be->nr_pages);
1151 scur = bvi->bvec.offset + cur -
1152 ((pgnr << PAGE_SHIFT) - off0);
1153 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1154 if (!be->decompressed_pages[pgnr]) {
1155 err = -EFSCORRUPTED;
1159 src = kmap_local_page(be->decompressed_pages[pgnr]);
1160 memcpy(dst + cur, src + scur, len);
1165 z_erofs_onlinepage_endio(bvi->bvec.page, err);
1171 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
1173 struct z_erofs_pcluster *pcl = be->pcl;
1174 struct z_erofs_bvec_iter biter;
1175 struct page *old_bvpage;
1178 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1179 for (i = 0; i < pcl->vcnt; ++i) {
1180 struct z_erofs_bvec bvec;
1182 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1185 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1187 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1188 z_erofs_do_decompressed_bvec(be, &bvec);
1191 old_bvpage = z_erofs_bvec_iter_end(&biter);
1193 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1196 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
1199 struct z_erofs_pcluster *pcl = be->pcl;
1200 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1203 *overlapped = false;
1204 for (i = 0; i < pclusterpages; ++i) {
1205 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1206 struct page *page = bvec->page;
1208 /* compressed pages ought to be present before decompressing */
1213 be->compressed_pages[i] = page;
1215 if (z_erofs_is_inline_pcluster(pcl)) {
1216 if (!PageUptodate(page))
1221 DBG_BUGON(z_erofs_page_is_invalidated(page));
1222 if (!z_erofs_is_shortlived_page(page)) {
1223 if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
1224 if (!PageUptodate(page))
1228 z_erofs_do_decompressed_bvec(be, bvec);
1238 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
1241 struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1242 struct z_erofs_pcluster *pcl = be->pcl;
1243 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1244 const struct z_erofs_decompressor *decompressor =
1245 &erofs_decompressors[pcl->algorithmformat];
1246 unsigned int i, inputsize;
1251 mutex_lock(&pcl->lock);
1252 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1254 /* allocate (de)compressed page arrays if cannot be kept on stack */
1255 be->decompressed_pages = NULL;
1256 be->compressed_pages = NULL;
1257 be->onstack_used = 0;
1258 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1259 be->decompressed_pages = be->onstack_pages;
1260 be->onstack_used = be->nr_pages;
1261 memset(be->decompressed_pages, 0,
1262 sizeof(struct page *) * be->nr_pages);
1265 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1266 be->compressed_pages = be->onstack_pages + be->onstack_used;
1268 if (!be->decompressed_pages)
1269 be->decompressed_pages =
1270 kvcalloc(be->nr_pages, sizeof(struct page *),
1271 GFP_KERNEL | __GFP_NOFAIL);
1272 if (!be->compressed_pages)
1273 be->compressed_pages =
1274 kvcalloc(pclusterpages, sizeof(struct page *),
1275 GFP_KERNEL | __GFP_NOFAIL);
1277 z_erofs_parse_out_bvecs(be);
1278 err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1284 if (z_erofs_is_inline_pcluster(pcl))
1285 inputsize = pcl->tailpacking_size;
1287 inputsize = pclusterpages * PAGE_SIZE;
1289 err = decompressor->decompress(&(struct z_erofs_decompress_req) {
1291 .in = be->compressed_pages,
1292 .out = be->decompressed_pages,
1293 .pageofs_in = pcl->pageofs_in,
1294 .pageofs_out = pcl->pageofs_out,
1295 .inputsize = inputsize,
1296 .outputsize = pcl->length,
1297 .alg = pcl->algorithmformat,
1298 .inplace_io = overlapped,
1299 .partial_decoding = pcl->partial,
1300 .fillgaps = pcl->multibases,
1304 /* must handle all compressed pages before actual file pages */
1305 if (z_erofs_is_inline_pcluster(pcl)) {
1306 page = pcl->compressed_bvecs[0].page;
1307 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1310 for (i = 0; i < pclusterpages; ++i) {
1311 page = pcl->compressed_bvecs[i].page;
1313 if (erofs_page_is_managed(sbi, page))
1316 /* recycle all individual short-lived pages */
1317 (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1318 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1321 if (be->compressed_pages < be->onstack_pages ||
1322 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1323 kvfree(be->compressed_pages);
1324 z_erofs_fill_other_copies(be, err);
1326 for (i = 0; i < be->nr_pages; ++i) {
1327 page = be->decompressed_pages[i];
1331 DBG_BUGON(z_erofs_page_is_invalidated(page));
1333 /* recycle all individual short-lived pages */
1334 if (z_erofs_put_shortlivedpage(be->pagepool, page))
1336 z_erofs_onlinepage_endio(page, err);
1339 if (be->decompressed_pages != be->onstack_pages)
1340 kvfree(be->decompressed_pages);
1343 pcl->partial = true;
1344 pcl->multibases = false;
1345 pcl->bvset.nextpage = NULL;
1348 /* pcluster lock MUST be taken before the following line */
1349 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1350 mutex_unlock(&pcl->lock);
1354 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1355 struct page **pagepool)
1357 struct z_erofs_decompress_backend be = {
1359 .pagepool = pagepool,
1360 .decompressed_secondary_bvecs =
1361 LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1363 z_erofs_next_pcluster_t owned = io->head;
1365 while (owned != Z_EROFS_PCLUSTER_TAIL) {
1366 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1368 be.pcl = container_of(owned, struct z_erofs_pcluster, next);
1369 owned = READ_ONCE(be.pcl->next);
1371 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
1372 if (z_erofs_is_inline_pcluster(be.pcl))
1373 z_erofs_free_pcluster(be.pcl);
1375 erofs_workgroup_put(&be.pcl->obj);
1379 static void z_erofs_decompressqueue_work(struct work_struct *work)
1381 struct z_erofs_decompressqueue *bgq =
1382 container_of(work, struct z_erofs_decompressqueue, u.work);
1383 struct page *pagepool = NULL;
1385 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
1386 z_erofs_decompress_queue(bgq, &pagepool);
1387 erofs_release_pages(&pagepool);
1391 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1392 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1394 z_erofs_decompressqueue_work((struct work_struct *)work);
1398 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1401 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1403 /* wake up the caller thread for sync decompression */
1405 if (!atomic_add_return(bios, &io->pending_bios))
1406 complete(&io->u.done);
1410 if (atomic_add_return(bios, &io->pending_bios))
1412 /* Use (kthread_)work and sync decompression for atomic contexts only */
1413 if (!in_task() || irqs_disabled() || rcu_read_lock_any_held()) {
1414 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1415 struct kthread_worker *worker;
1418 worker = rcu_dereference(
1419 z_erofs_pcpu_workers[raw_smp_processor_id()]);
1421 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1422 queue_work(z_erofs_workqueue, &io->u.work);
1424 kthread_queue_work(worker, &io->u.kthread_work);
1428 queue_work(z_erofs_workqueue, &io->u.work);
1430 /* enable sync decompression for readahead */
1431 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1432 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1435 z_erofs_decompressqueue_work(&io->u.work);
1438 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1440 struct page **pagepool,
1441 struct address_space *mc)
1443 const pgoff_t index = pcl->obj.index;
1444 gfp_t gfp = mapping_gfp_mask(mc);
1445 bool tocache = false;
1447 struct address_space *mapping;
1448 struct page *oldpage, *page;
1452 page = READ_ONCE(pcl->compressed_bvecs[nr].page);
1458 justfound = (unsigned long)page & 1UL;
1459 page = (struct page *)((unsigned long)page & ~1UL);
1462 * preallocated cached pages, which is used to avoid direct reclaim
1463 * otherwise, it will go inplace I/O path instead.
1465 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1466 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1467 set_page_private(page, 0);
1471 mapping = READ_ONCE(page->mapping);
1474 * file-backed online pages in plcuster are all locked steady,
1475 * therefore it is impossible for `mapping' to be NULL.
1477 if (mapping && mapping != mc)
1478 /* ought to be unmanaged pages */
1481 /* directly return for shortlived page as well */
1482 if (z_erofs_is_shortlived_page(page))
1487 /* only true if page reclaim goes wrong, should never happen */
1488 DBG_BUGON(justfound && PagePrivate(page));
1490 /* the page is still in manage cache */
1491 if (page->mapping == mc) {
1492 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1494 if (!PagePrivate(page)) {
1496 * impossible to be !PagePrivate(page) for
1497 * the current restriction as well if
1498 * the page is already in compressed_bvecs[].
1500 DBG_BUGON(!justfound);
1503 set_page_private(page, (unsigned long)pcl);
1504 SetPagePrivate(page);
1507 /* no need to submit io if it is already up-to-date */
1508 if (PageUptodate(page)) {
1516 * the managed page has been truncated, it's unsafe to
1517 * reuse this one, let's allocate a new cache-managed page.
1519 DBG_BUGON(page->mapping);
1520 DBG_BUGON(!justfound);
1526 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1527 if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1529 erofs_pagepool_add(pagepool, page);
1534 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1535 /* turn into temporary page if fails (1 ref) */
1536 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1539 attach_page_private(page, pcl);
1540 /* drop a refcount added by allocpage (then we have 2 refs here) */
1543 out: /* the only exit (for tracing and debugging) */
1547 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1548 struct z_erofs_decompressqueue *fgq, bool *fg)
1550 struct z_erofs_decompressqueue *q;
1553 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1558 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1559 kthread_init_work(&q->u.kthread_work,
1560 z_erofs_decompressqueue_kthread_work);
1562 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1567 init_completion(&fgq->u.done);
1568 atomic_set(&fgq->pending_bios, 0);
1573 q->head = Z_EROFS_PCLUSTER_TAIL;
1577 /* define decompression jobqueue types */
1584 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1585 z_erofs_next_pcluster_t qtail[],
1586 z_erofs_next_pcluster_t owned_head)
1588 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1589 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1591 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
1593 WRITE_ONCE(*submit_qtail, owned_head);
1594 WRITE_ONCE(*bypass_qtail, &pcl->next);
1596 qtail[JQ_BYPASS] = &pcl->next;
1599 static void z_erofs_decompressqueue_endio(struct bio *bio)
1601 struct z_erofs_decompressqueue *q = bio->bi_private;
1602 blk_status_t err = bio->bi_status;
1603 struct bio_vec *bvec;
1604 struct bvec_iter_all iter_all;
1606 bio_for_each_segment_all(bvec, bio, iter_all) {
1607 struct page *page = bvec->bv_page;
1609 DBG_BUGON(PageUptodate(page));
1610 DBG_BUGON(z_erofs_page_is_invalidated(page));
1612 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1614 SetPageUptodate(page);
1620 z_erofs_decompress_kickoff(q, -1);
1624 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1625 struct z_erofs_decompressqueue *fgq,
1626 bool *force_fg, bool readahead)
1628 struct super_block *sb = f->inode->i_sb;
1629 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1630 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1631 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1632 z_erofs_next_pcluster_t owned_head = f->owned_head;
1633 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1635 struct block_device *last_bdev;
1636 unsigned int nr_bios = 0;
1637 struct bio *bio = NULL;
1638 unsigned long pflags;
1642 * if managed cache is enabled, bypass jobqueue is needed,
1643 * no need to read from device for all pclusters in this queue.
1645 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1646 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1648 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1649 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1651 /* by default, all need io submission */
1652 q[JQ_SUBMIT]->head = owned_head;
1655 struct erofs_map_dev mdev;
1656 struct z_erofs_pcluster *pcl;
1661 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1662 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1663 owned_head = READ_ONCE(pcl->next);
1665 if (z_erofs_is_inline_pcluster(pcl)) {
1666 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1670 /* no device id here, thus it will always succeed */
1671 mdev = (struct erofs_map_dev) {
1672 .m_pa = erofs_pos(sb, pcl->obj.index),
1674 (void)erofs_map_dev(sb, &mdev);
1676 cur = erofs_blknr(sb, mdev.m_pa);
1677 end = cur + pcl->pclusterpages;
1682 page = pickup_page_for_submission(pcl, i++,
1687 if (bio && (cur != last_index + 1 ||
1688 last_bdev != mdev.m_bdev)) {
1692 psi_memstall_leave(&pflags);
1698 if (unlikely(PageWorkingset(page)) && !memstall) {
1699 psi_memstall_enter(&pflags);
1704 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1705 REQ_OP_READ, GFP_NOIO);
1706 bio->bi_end_io = z_erofs_decompressqueue_endio;
1708 last_bdev = mdev.m_bdev;
1709 bio->bi_iter.bi_sector = (sector_t)cur <<
1710 (sb->s_blocksize_bits - 9);
1711 bio->bi_private = q[JQ_SUBMIT];
1713 bio->bi_opf |= REQ_RAHEAD;
1717 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1718 goto submit_bio_retry;
1722 } while (++cur < end);
1725 qtail[JQ_SUBMIT] = &pcl->next;
1727 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1728 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1733 psi_memstall_leave(&pflags);
1737 * although background is preferred, no one is pending for submission.
1738 * don't issue decompression but drop it directly instead.
1740 if (!*force_fg && !nr_bios) {
1741 kvfree(q[JQ_SUBMIT]);
1744 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1747 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1748 bool force_fg, bool ra)
1750 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1752 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1754 z_erofs_submit_queue(f, io, &force_fg, ra);
1756 /* handle bypass queue (no i/o pclusters) immediately */
1757 z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
1762 /* wait until all bios are completed */
1763 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1765 /* handle synchronous decompress queue in the caller context */
1766 z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool);
1770 * Since partial uptodate is still unimplemented for now, we have to use
1771 * approximate readmore strategies as a start.
1773 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1774 struct readahead_control *rac, bool backmost)
1776 struct inode *inode = f->inode;
1777 struct erofs_map_blocks *map = &f->map;
1778 erofs_off_t cur, end, headoffset = f->headoffset;
1783 end = headoffset + readahead_length(rac) - 1;
1785 end = headoffset + PAGE_SIZE - 1;
1787 err = z_erofs_map_blocks_iter(inode, map,
1788 EROFS_GET_BLOCKS_READMORE);
1792 /* expand ra for the trailing edge if readahead */
1794 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1795 readahead_expand(rac, headoffset, cur - headoffset);
1798 end = round_up(end, PAGE_SIZE);
1800 end = round_up(map->m_la, PAGE_SIZE);
1806 cur = map->m_la + map->m_llen - 1;
1807 while ((cur >= end) && (cur < i_size_read(inode))) {
1808 pgoff_t index = cur >> PAGE_SHIFT;
1811 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1813 if (PageUptodate(page))
1816 (void)z_erofs_do_read_page(f, page);
1820 if (cur < PAGE_SIZE)
1822 cur = (index << PAGE_SHIFT) - 1;
1826 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1828 struct inode *const inode = folio->mapping->host;
1829 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1830 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1833 trace_erofs_read_folio(folio, false);
1834 f.headoffset = (erofs_off_t)folio->index << PAGE_SHIFT;
1836 z_erofs_pcluster_readmore(&f, NULL, true);
1837 err = z_erofs_do_read_page(&f, &folio->page);
1838 z_erofs_pcluster_readmore(&f, NULL, false);
1839 z_erofs_pcluster_end(&f);
1841 /* if some compressed cluster ready, need submit them anyway */
1842 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, 0), false);
1844 if (err && err != -EINTR)
1845 erofs_err(inode->i_sb, "read error %d @ %lu of nid %llu",
1846 err, folio->index, EROFS_I(inode)->nid);
1848 erofs_put_metabuf(&f.map.buf);
1849 erofs_release_pages(&f.pagepool);
1853 static void z_erofs_readahead(struct readahead_control *rac)
1855 struct inode *const inode = rac->mapping->host;
1856 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1857 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1858 struct folio *head = NULL, *folio;
1859 unsigned int nr_folios;
1862 f.headoffset = readahead_pos(rac);
1864 z_erofs_pcluster_readmore(&f, rac, true);
1865 nr_folios = readahead_count(rac);
1866 trace_erofs_readpages(inode, readahead_index(rac), nr_folios, false);
1868 while ((folio = readahead_folio(rac))) {
1869 folio->private = head;
1873 /* traverse in reverse order for best metadata I/O performance */
1876 head = folio_get_private(folio);
1878 err = z_erofs_do_read_page(&f, &folio->page);
1879 if (err && err != -EINTR)
1880 erofs_err(inode->i_sb, "readahead error at folio %lu @ nid %llu",
1881 folio->index, EROFS_I(inode)->nid);
1883 z_erofs_pcluster_readmore(&f, rac, false);
1884 z_erofs_pcluster_end(&f);
1886 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, nr_folios), true);
1887 erofs_put_metabuf(&f.map.buf);
1888 erofs_release_pages(&f.pagepool);
1891 const struct address_space_operations z_erofs_aops = {
1892 .read_folio = z_erofs_read_folio,
1893 .readahead = z_erofs_readahead,