147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only 247e4937aSGao Xiang /* 347e4937aSGao Xiang * Copyright (C) 2018 HUAWEI, Inc. 4592e7cd0SAlexander A. Klimov * https://www.huawei.com/ 506a304cdSGao Xiang * Copyright (C) 2022 Alibaba Cloud 647e4937aSGao Xiang */ 747e4937aSGao Xiang #include "compress.h" 899486c51SChristoph Hellwig #include <linux/psi.h> 93fffb589SSandeep Dhavale #include <linux/cpuhotplug.h> 1047e4937aSGao Xiang #include <trace/events/erofs.h> 1147e4937aSGao Xiang 12a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE) 13a9a94d93SGao Xiang #define Z_EROFS_INLINE_BVECS 2 14a9a94d93SGao Xiang 15a9a94d93SGao Xiang /* 16a9a94d93SGao Xiang * let's leave a type here in case of introducing 17a9a94d93SGao Xiang * another tagged pointer later. 18a9a94d93SGao Xiang */ 19a9a94d93SGao Xiang typedef void *z_erofs_next_pcluster_t; 20a9a94d93SGao Xiang 21a9a94d93SGao Xiang struct z_erofs_bvec { 22a9a94d93SGao Xiang struct page *page; 23a9a94d93SGao Xiang int offset; 24a9a94d93SGao Xiang unsigned int end; 25a9a94d93SGao Xiang }; 26a9a94d93SGao Xiang 27a9a94d93SGao Xiang #define __Z_EROFS_BVSET(name, total) \ 28a9a94d93SGao Xiang struct name { \ 29a9a94d93SGao Xiang /* point to the next page which contains the following bvecs */ \ 30a9a94d93SGao Xiang struct page *nextpage; \ 31a9a94d93SGao Xiang struct z_erofs_bvec bvec[total]; \ 32a9a94d93SGao Xiang } 33a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset,); 34a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS); 35a9a94d93SGao Xiang 36a9a94d93SGao Xiang /* 37a9a94d93SGao Xiang * Structure fields follow one of the following exclusion rules. 38a9a94d93SGao Xiang * 39a9a94d93SGao Xiang * I: Modifiable by initialization/destruction paths and read-only 40a9a94d93SGao Xiang * for everyone else; 41a9a94d93SGao Xiang * 42a9a94d93SGao Xiang * L: Field should be protected by the pcluster lock; 43a9a94d93SGao Xiang * 44a9a94d93SGao Xiang * A: Field should be accessed / updated in atomic for parallelized code. 45a9a94d93SGao Xiang */ 46a9a94d93SGao Xiang struct z_erofs_pcluster { 47a9a94d93SGao Xiang struct erofs_workgroup obj; 48a9a94d93SGao Xiang struct mutex lock; 49a9a94d93SGao Xiang 50a9a94d93SGao Xiang /* A: point to next chained pcluster or TAILs */ 51a9a94d93SGao Xiang z_erofs_next_pcluster_t next; 52a9a94d93SGao Xiang 53a9a94d93SGao Xiang /* L: the maximum decompression size of this round */ 54a9a94d93SGao Xiang unsigned int length; 55a9a94d93SGao Xiang 56a9a94d93SGao Xiang /* L: total number of bvecs */ 57a9a94d93SGao Xiang unsigned int vcnt; 58a9a94d93SGao Xiang 59a9a94d93SGao Xiang /* I: page offset of start position of decompression */ 60a9a94d93SGao Xiang unsigned short pageofs_out; 61a9a94d93SGao Xiang 62a9a94d93SGao Xiang /* I: page offset of inline compressed data */ 63a9a94d93SGao Xiang unsigned short pageofs_in; 64a9a94d93SGao Xiang 65a9a94d93SGao Xiang union { 66a9a94d93SGao Xiang /* L: inline a certain number of bvec for bootstrap */ 67a9a94d93SGao Xiang struct z_erofs_bvset_inline bvset; 68a9a94d93SGao Xiang 69a9a94d93SGao Xiang /* I: can be used to free the pcluster by RCU. */ 70a9a94d93SGao Xiang struct rcu_head rcu; 71a9a94d93SGao Xiang }; 72a9a94d93SGao Xiang 73a9a94d93SGao Xiang union { 74a9a94d93SGao Xiang /* I: physical cluster size in pages */ 75a9a94d93SGao Xiang unsigned short pclusterpages; 76a9a94d93SGao Xiang 77a9a94d93SGao Xiang /* I: tailpacking inline compressed size */ 78a9a94d93SGao Xiang unsigned short tailpacking_size; 79a9a94d93SGao Xiang }; 80a9a94d93SGao Xiang 81a9a94d93SGao Xiang /* I: compression algorithm format */ 82a9a94d93SGao Xiang unsigned char algorithmformat; 83a9a94d93SGao Xiang 84a9a94d93SGao Xiang /* L: whether partial decompression or not */ 85a9a94d93SGao Xiang bool partial; 86a9a94d93SGao Xiang 87a9a94d93SGao Xiang /* L: indicate several pageofs_outs or not */ 88a9a94d93SGao Xiang bool multibases; 89a9a94d93SGao Xiang 90a9a94d93SGao Xiang /* A: compressed bvecs (can be cached or inplaced pages) */ 91a9a94d93SGao Xiang struct z_erofs_bvec compressed_bvecs[]; 92a9a94d93SGao Xiang }; 93a9a94d93SGao Xiang 94967c28b2SGao Xiang /* the end of a chain of pclusters */ 9543d86ec9SGao Xiang #define Z_EROFS_PCLUSTER_TAIL ((void *) 0x700 + POISON_POINTER_DELTA) 96a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_NIL (NULL) 97a9a94d93SGao Xiang 98a9a94d93SGao Xiang struct z_erofs_decompressqueue { 99a9a94d93SGao Xiang struct super_block *sb; 100a9a94d93SGao Xiang atomic_t pending_bios; 101a9a94d93SGao Xiang z_erofs_next_pcluster_t head; 102a9a94d93SGao Xiang 103a9a94d93SGao Xiang union { 104a9a94d93SGao Xiang struct completion done; 105a9a94d93SGao Xiang struct work_struct work; 1063fffb589SSandeep Dhavale struct kthread_work kthread_work; 107a9a94d93SGao Xiang } u; 108a9a94d93SGao Xiang bool eio, sync; 109a9a94d93SGao Xiang }; 110a9a94d93SGao Xiang 111a9a94d93SGao Xiang static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl) 112a9a94d93SGao Xiang { 113a9a94d93SGao Xiang return !pcl->obj.index; 114a9a94d93SGao Xiang } 115a9a94d93SGao Xiang 116a9a94d93SGao Xiang static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl) 117a9a94d93SGao Xiang { 118a9a94d93SGao Xiang if (z_erofs_is_inline_pcluster(pcl)) 119a9a94d93SGao Xiang return 1; 120a9a94d93SGao Xiang return pcl->pclusterpages; 121a9a94d93SGao Xiang } 122a9a94d93SGao Xiang 123a9a94d93SGao Xiang /* 124a9a94d93SGao Xiang * bit 30: I/O error occurred on this page 125a9a94d93SGao Xiang * bit 0 - 29: remaining parts to complete this page 126a9a94d93SGao Xiang */ 127a9a94d93SGao Xiang #define Z_EROFS_PAGE_EIO (1 << 30) 128a9a94d93SGao Xiang 129a9a94d93SGao Xiang static inline void z_erofs_onlinepage_init(struct page *page) 130a9a94d93SGao Xiang { 131a9a94d93SGao Xiang union { 132a9a94d93SGao Xiang atomic_t o; 133a9a94d93SGao Xiang unsigned long v; 134a9a94d93SGao Xiang } u = { .o = ATOMIC_INIT(1) }; 135a9a94d93SGao Xiang 136a9a94d93SGao Xiang set_page_private(page, u.v); 137a9a94d93SGao Xiang smp_wmb(); 138a9a94d93SGao Xiang SetPagePrivate(page); 139a9a94d93SGao Xiang } 140a9a94d93SGao Xiang 141a9a94d93SGao Xiang static inline void z_erofs_onlinepage_split(struct page *page) 142a9a94d93SGao Xiang { 143a9a94d93SGao Xiang atomic_inc((atomic_t *)&page->private); 144a9a94d93SGao Xiang } 145a9a94d93SGao Xiang 146a9a94d93SGao Xiang static inline void z_erofs_page_mark_eio(struct page *page) 147a9a94d93SGao Xiang { 148a9a94d93SGao Xiang int orig; 149a9a94d93SGao Xiang 150a9a94d93SGao Xiang do { 151a9a94d93SGao Xiang orig = atomic_read((atomic_t *)&page->private); 152a9a94d93SGao Xiang } while (atomic_cmpxchg((atomic_t *)&page->private, orig, 153a9a94d93SGao Xiang orig | Z_EROFS_PAGE_EIO) != orig); 154a9a94d93SGao Xiang } 155a9a94d93SGao Xiang 156a9a94d93SGao Xiang static inline void z_erofs_onlinepage_endio(struct page *page) 157a9a94d93SGao Xiang { 158a9a94d93SGao Xiang unsigned int v; 159a9a94d93SGao Xiang 160a9a94d93SGao Xiang DBG_BUGON(!PagePrivate(page)); 161a9a94d93SGao Xiang v = atomic_dec_return((atomic_t *)&page->private); 162a9a94d93SGao Xiang if (!(v & ~Z_EROFS_PAGE_EIO)) { 163a9a94d93SGao Xiang set_page_private(page, 0); 164a9a94d93SGao Xiang ClearPagePrivate(page); 165a9a94d93SGao Xiang if (!(v & Z_EROFS_PAGE_EIO)) 166a9a94d93SGao Xiang SetPageUptodate(page); 167a9a94d93SGao Xiang unlock_page(page); 168a9a94d93SGao Xiang } 169a9a94d93SGao Xiang } 170a9a94d93SGao Xiang 171a9a94d93SGao Xiang #define Z_EROFS_ONSTACK_PAGES 32 172a9a94d93SGao Xiang 17347e4937aSGao Xiang /* 1749f6cc76eSGao Xiang * since pclustersize is variable for big pcluster feature, introduce slab 1759f6cc76eSGao Xiang * pools implementation for different pcluster sizes. 1769f6cc76eSGao Xiang */ 1779f6cc76eSGao Xiang struct z_erofs_pcluster_slab { 1789f6cc76eSGao Xiang struct kmem_cache *slab; 1799f6cc76eSGao Xiang unsigned int maxpages; 1809f6cc76eSGao Xiang char name[48]; 1819f6cc76eSGao Xiang }; 1829f6cc76eSGao Xiang 1839f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n } 1849f6cc76eSGao Xiang 1859f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = { 1869f6cc76eSGao Xiang _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128), 1879f6cc76eSGao Xiang _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES) 1889f6cc76eSGao Xiang }; 1899f6cc76eSGao Xiang 19006a304cdSGao Xiang struct z_erofs_bvec_iter { 19106a304cdSGao Xiang struct page *bvpage; 19206a304cdSGao Xiang struct z_erofs_bvset *bvset; 19306a304cdSGao Xiang unsigned int nr, cur; 19406a304cdSGao Xiang }; 19506a304cdSGao Xiang 19606a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter) 19706a304cdSGao Xiang { 19806a304cdSGao Xiang if (iter->bvpage) 19906a304cdSGao Xiang kunmap_local(iter->bvset); 20006a304cdSGao Xiang return iter->bvpage; 20106a304cdSGao Xiang } 20206a304cdSGao Xiang 20306a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter) 20406a304cdSGao Xiang { 20506a304cdSGao Xiang unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec; 20606a304cdSGao Xiang /* have to access nextpage in advance, otherwise it will be unmapped */ 20706a304cdSGao Xiang struct page *nextpage = iter->bvset->nextpage; 20806a304cdSGao Xiang struct page *oldpage; 20906a304cdSGao Xiang 21006a304cdSGao Xiang DBG_BUGON(!nextpage); 21106a304cdSGao Xiang oldpage = z_erofs_bvec_iter_end(iter); 21206a304cdSGao Xiang iter->bvpage = nextpage; 21306a304cdSGao Xiang iter->bvset = kmap_local_page(nextpage); 21406a304cdSGao Xiang iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec); 21506a304cdSGao Xiang iter->cur = 0; 21606a304cdSGao Xiang return oldpage; 21706a304cdSGao Xiang } 21806a304cdSGao Xiang 21906a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter, 22006a304cdSGao Xiang struct z_erofs_bvset_inline *bvset, 22106a304cdSGao Xiang unsigned int bootstrap_nr, 22206a304cdSGao Xiang unsigned int cur) 22306a304cdSGao Xiang { 22406a304cdSGao Xiang *iter = (struct z_erofs_bvec_iter) { 22506a304cdSGao Xiang .nr = bootstrap_nr, 22606a304cdSGao Xiang .bvset = (struct z_erofs_bvset *)bvset, 22706a304cdSGao Xiang }; 22806a304cdSGao Xiang 22906a304cdSGao Xiang while (cur > iter->nr) { 23006a304cdSGao Xiang cur -= iter->nr; 23106a304cdSGao Xiang z_erofs_bvset_flip(iter); 23206a304cdSGao Xiang } 23306a304cdSGao Xiang iter->cur = cur; 23406a304cdSGao Xiang } 23506a304cdSGao Xiang 23606a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter, 23706a304cdSGao Xiang struct z_erofs_bvec *bvec, 2386ab5eed6SGao Xiang struct page **candidate_bvpage, 2396ab5eed6SGao Xiang struct page **pagepool) 24006a304cdSGao Xiang { 24105b63d2bSGao Xiang if (iter->cur >= iter->nr) { 24205b63d2bSGao Xiang struct page *nextpage = *candidate_bvpage; 24306a304cdSGao Xiang 24405b63d2bSGao Xiang if (!nextpage) { 2456ab5eed6SGao Xiang nextpage = erofs_allocpage(pagepool, GFP_NOFS); 24605b63d2bSGao Xiang if (!nextpage) 24705b63d2bSGao Xiang return -ENOMEM; 24805b63d2bSGao Xiang set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE); 24905b63d2bSGao Xiang } 25006a304cdSGao Xiang DBG_BUGON(iter->bvset->nextpage); 25105b63d2bSGao Xiang iter->bvset->nextpage = nextpage; 25206a304cdSGao Xiang z_erofs_bvset_flip(iter); 25306a304cdSGao Xiang 25406a304cdSGao Xiang iter->bvset->nextpage = NULL; 25506a304cdSGao Xiang *candidate_bvpage = NULL; 25606a304cdSGao Xiang } 25706a304cdSGao Xiang iter->bvset->bvec[iter->cur++] = *bvec; 25806a304cdSGao Xiang return 0; 25906a304cdSGao Xiang } 26006a304cdSGao Xiang 26106a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter, 26206a304cdSGao Xiang struct z_erofs_bvec *bvec, 26306a304cdSGao Xiang struct page **old_bvpage) 26406a304cdSGao Xiang { 26506a304cdSGao Xiang if (iter->cur == iter->nr) 26606a304cdSGao Xiang *old_bvpage = z_erofs_bvset_flip(iter); 26706a304cdSGao Xiang else 26806a304cdSGao Xiang *old_bvpage = NULL; 26906a304cdSGao Xiang *bvec = iter->bvset->bvec[iter->cur++]; 27006a304cdSGao Xiang } 27106a304cdSGao Xiang 2729f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void) 2739f6cc76eSGao Xiang { 2749f6cc76eSGao Xiang int i; 2759f6cc76eSGao Xiang 2769f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 2779f6cc76eSGao Xiang if (!pcluster_pool[i].slab) 2789f6cc76eSGao Xiang continue; 2799f6cc76eSGao Xiang kmem_cache_destroy(pcluster_pool[i].slab); 2809f6cc76eSGao Xiang pcluster_pool[i].slab = NULL; 2819f6cc76eSGao Xiang } 2829f6cc76eSGao Xiang } 2839f6cc76eSGao Xiang 2849f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void) 2859f6cc76eSGao Xiang { 2869f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs; 2879f6cc76eSGao Xiang struct z_erofs_pcluster *a; 2889f6cc76eSGao Xiang unsigned int size; 2899f6cc76eSGao Xiang 2909f6cc76eSGao Xiang for (pcs = pcluster_pool; 2919f6cc76eSGao Xiang pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) { 292ed722fbcSGao Xiang size = struct_size(a, compressed_bvecs, pcs->maxpages); 2939f6cc76eSGao Xiang 2949f6cc76eSGao Xiang sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages); 2959f6cc76eSGao Xiang pcs->slab = kmem_cache_create(pcs->name, size, 0, 2969f6cc76eSGao Xiang SLAB_RECLAIM_ACCOUNT, NULL); 2979f6cc76eSGao Xiang if (pcs->slab) 2989f6cc76eSGao Xiang continue; 2999f6cc76eSGao Xiang 3009f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 3019f6cc76eSGao Xiang return -ENOMEM; 3029f6cc76eSGao Xiang } 3039f6cc76eSGao Xiang return 0; 3049f6cc76eSGao Xiang } 3059f6cc76eSGao Xiang 3069f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages) 3079f6cc76eSGao Xiang { 3089f6cc76eSGao Xiang int i; 3099f6cc76eSGao Xiang 3109f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 3119f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 3129f6cc76eSGao Xiang struct z_erofs_pcluster *pcl; 3139f6cc76eSGao Xiang 3149f6cc76eSGao Xiang if (nrpages > pcs->maxpages) 3159f6cc76eSGao Xiang continue; 3169f6cc76eSGao Xiang 3179f6cc76eSGao Xiang pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS); 3189f6cc76eSGao Xiang if (!pcl) 3199f6cc76eSGao Xiang return ERR_PTR(-ENOMEM); 3209f6cc76eSGao Xiang pcl->pclusterpages = nrpages; 3219f6cc76eSGao Xiang return pcl; 3229f6cc76eSGao Xiang } 3239f6cc76eSGao Xiang return ERR_PTR(-EINVAL); 3249f6cc76eSGao Xiang } 3259f6cc76eSGao Xiang 3269f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl) 3279f6cc76eSGao Xiang { 328cecf864dSYue Hu unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 3299f6cc76eSGao Xiang int i; 3309f6cc76eSGao Xiang 3319f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 3329f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 3339f6cc76eSGao Xiang 334cecf864dSYue Hu if (pclusterpages > pcs->maxpages) 3359f6cc76eSGao Xiang continue; 3369f6cc76eSGao Xiang 3379f6cc76eSGao Xiang kmem_cache_free(pcs->slab, pcl); 3389f6cc76eSGao Xiang return; 3399f6cc76eSGao Xiang } 3409f6cc76eSGao Xiang DBG_BUGON(1); 3419f6cc76eSGao Xiang } 3429f6cc76eSGao Xiang 34347e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly; 34447e4937aSGao Xiang 3453fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 3463fffb589SSandeep Dhavale static struct kthread_worker __rcu **z_erofs_pcpu_workers; 3473fffb589SSandeep Dhavale 3483fffb589SSandeep Dhavale static void erofs_destroy_percpu_workers(void) 34947e4937aSGao Xiang { 3503fffb589SSandeep Dhavale struct kthread_worker *worker; 3513fffb589SSandeep Dhavale unsigned int cpu; 3523fffb589SSandeep Dhavale 3533fffb589SSandeep Dhavale for_each_possible_cpu(cpu) { 3543fffb589SSandeep Dhavale worker = rcu_dereference_protected( 3553fffb589SSandeep Dhavale z_erofs_pcpu_workers[cpu], 1); 3563fffb589SSandeep Dhavale rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL); 3573fffb589SSandeep Dhavale if (worker) 3583fffb589SSandeep Dhavale kthread_destroy_worker(worker); 3593fffb589SSandeep Dhavale } 3603fffb589SSandeep Dhavale kfree(z_erofs_pcpu_workers); 36147e4937aSGao Xiang } 36247e4937aSGao Xiang 3633fffb589SSandeep Dhavale static struct kthread_worker *erofs_init_percpu_worker(int cpu) 36447e4937aSGao Xiang { 3653fffb589SSandeep Dhavale struct kthread_worker *worker = 3663fffb589SSandeep Dhavale kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu); 36747e4937aSGao Xiang 3683fffb589SSandeep Dhavale if (IS_ERR(worker)) 3693fffb589SSandeep Dhavale return worker; 3703fffb589SSandeep Dhavale if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI)) 3713fffb589SSandeep Dhavale sched_set_fifo_low(worker->task); 3723fffb589SSandeep Dhavale return worker; 3733fffb589SSandeep Dhavale } 3743fffb589SSandeep Dhavale 3753fffb589SSandeep Dhavale static int erofs_init_percpu_workers(void) 3763fffb589SSandeep Dhavale { 3773fffb589SSandeep Dhavale struct kthread_worker *worker; 3783fffb589SSandeep Dhavale unsigned int cpu; 3793fffb589SSandeep Dhavale 3803fffb589SSandeep Dhavale z_erofs_pcpu_workers = kcalloc(num_possible_cpus(), 3813fffb589SSandeep Dhavale sizeof(struct kthread_worker *), GFP_ATOMIC); 3823fffb589SSandeep Dhavale if (!z_erofs_pcpu_workers) 3833fffb589SSandeep Dhavale return -ENOMEM; 3843fffb589SSandeep Dhavale 3853fffb589SSandeep Dhavale for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */ 3863fffb589SSandeep Dhavale worker = erofs_init_percpu_worker(cpu); 3873fffb589SSandeep Dhavale if (!IS_ERR(worker)) 3883fffb589SSandeep Dhavale rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker); 3893fffb589SSandeep Dhavale } 3903fffb589SSandeep Dhavale return 0; 3913fffb589SSandeep Dhavale } 3923fffb589SSandeep Dhavale #else 3933fffb589SSandeep Dhavale static inline void erofs_destroy_percpu_workers(void) {} 3943fffb589SSandeep Dhavale static inline int erofs_init_percpu_workers(void) { return 0; } 3953fffb589SSandeep Dhavale #endif 3963fffb589SSandeep Dhavale 3973fffb589SSandeep Dhavale #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD) 3983fffb589SSandeep Dhavale static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock); 3993fffb589SSandeep Dhavale static enum cpuhp_state erofs_cpuhp_state; 4003fffb589SSandeep Dhavale 4013fffb589SSandeep Dhavale static int erofs_cpu_online(unsigned int cpu) 4023fffb589SSandeep Dhavale { 4033fffb589SSandeep Dhavale struct kthread_worker *worker, *old; 4043fffb589SSandeep Dhavale 4053fffb589SSandeep Dhavale worker = erofs_init_percpu_worker(cpu); 4063fffb589SSandeep Dhavale if (IS_ERR(worker)) 4073fffb589SSandeep Dhavale return PTR_ERR(worker); 4083fffb589SSandeep Dhavale 4093fffb589SSandeep Dhavale spin_lock(&z_erofs_pcpu_worker_lock); 4103fffb589SSandeep Dhavale old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu], 4113fffb589SSandeep Dhavale lockdep_is_held(&z_erofs_pcpu_worker_lock)); 4123fffb589SSandeep Dhavale if (!old) 4133fffb589SSandeep Dhavale rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker); 4143fffb589SSandeep Dhavale spin_unlock(&z_erofs_pcpu_worker_lock); 4153fffb589SSandeep Dhavale if (old) 4163fffb589SSandeep Dhavale kthread_destroy_worker(worker); 4173fffb589SSandeep Dhavale return 0; 4183fffb589SSandeep Dhavale } 4193fffb589SSandeep Dhavale 4203fffb589SSandeep Dhavale static int erofs_cpu_offline(unsigned int cpu) 4213fffb589SSandeep Dhavale { 4223fffb589SSandeep Dhavale struct kthread_worker *worker; 4233fffb589SSandeep Dhavale 4243fffb589SSandeep Dhavale spin_lock(&z_erofs_pcpu_worker_lock); 4253fffb589SSandeep Dhavale worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu], 4263fffb589SSandeep Dhavale lockdep_is_held(&z_erofs_pcpu_worker_lock)); 4273fffb589SSandeep Dhavale rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL); 4283fffb589SSandeep Dhavale spin_unlock(&z_erofs_pcpu_worker_lock); 4293fffb589SSandeep Dhavale 4303fffb589SSandeep Dhavale synchronize_rcu(); 4313fffb589SSandeep Dhavale if (worker) 4323fffb589SSandeep Dhavale kthread_destroy_worker(worker); 4333fffb589SSandeep Dhavale return 0; 4343fffb589SSandeep Dhavale } 4353fffb589SSandeep Dhavale 4363fffb589SSandeep Dhavale static int erofs_cpu_hotplug_init(void) 4373fffb589SSandeep Dhavale { 4383fffb589SSandeep Dhavale int state; 4393fffb589SSandeep Dhavale 4403fffb589SSandeep Dhavale state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 4413fffb589SSandeep Dhavale "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline); 4423fffb589SSandeep Dhavale if (state < 0) 4433fffb589SSandeep Dhavale return state; 4443fffb589SSandeep Dhavale 4453fffb589SSandeep Dhavale erofs_cpuhp_state = state; 4463fffb589SSandeep Dhavale return 0; 4473fffb589SSandeep Dhavale } 4483fffb589SSandeep Dhavale 4493fffb589SSandeep Dhavale static void erofs_cpu_hotplug_destroy(void) 4503fffb589SSandeep Dhavale { 4513fffb589SSandeep Dhavale if (erofs_cpuhp_state) 4523fffb589SSandeep Dhavale cpuhp_remove_state_nocalls(erofs_cpuhp_state); 4533fffb589SSandeep Dhavale } 4543fffb589SSandeep Dhavale #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */ 4553fffb589SSandeep Dhavale static inline int erofs_cpu_hotplug_init(void) { return 0; } 4563fffb589SSandeep Dhavale static inline void erofs_cpu_hotplug_destroy(void) {} 4573fffb589SSandeep Dhavale #endif 4583fffb589SSandeep Dhavale 4593fffb589SSandeep Dhavale void z_erofs_exit_zip_subsystem(void) 4603fffb589SSandeep Dhavale { 4613fffb589SSandeep Dhavale erofs_cpu_hotplug_destroy(); 4623fffb589SSandeep Dhavale erofs_destroy_percpu_workers(); 4633fffb589SSandeep Dhavale destroy_workqueue(z_erofs_workqueue); 4643fffb589SSandeep Dhavale z_erofs_destroy_pcluster_pool(); 46547e4937aSGao Xiang } 46647e4937aSGao Xiang 46747e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void) 46847e4937aSGao Xiang { 4699f6cc76eSGao Xiang int err = z_erofs_create_pcluster_pool(); 47047e4937aSGao Xiang 4719f6cc76eSGao Xiang if (err) 4723fffb589SSandeep Dhavale goto out_error_pcluster_pool; 4733fffb589SSandeep Dhavale 4743fffb589SSandeep Dhavale z_erofs_workqueue = alloc_workqueue("erofs_worker", 4753fffb589SSandeep Dhavale WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus()); 4768d1b80a7SDan Carpenter if (!z_erofs_workqueue) { 4778d1b80a7SDan Carpenter err = -ENOMEM; 4783fffb589SSandeep Dhavale goto out_error_workqueue_init; 4798d1b80a7SDan Carpenter } 4803fffb589SSandeep Dhavale 4813fffb589SSandeep Dhavale err = erofs_init_percpu_workers(); 4829f6cc76eSGao Xiang if (err) 4833fffb589SSandeep Dhavale goto out_error_pcpu_worker; 4843fffb589SSandeep Dhavale 4853fffb589SSandeep Dhavale err = erofs_cpu_hotplug_init(); 4863fffb589SSandeep Dhavale if (err < 0) 4873fffb589SSandeep Dhavale goto out_error_cpuhp_init; 4883fffb589SSandeep Dhavale return err; 4893fffb589SSandeep Dhavale 4903fffb589SSandeep Dhavale out_error_cpuhp_init: 4913fffb589SSandeep Dhavale erofs_destroy_percpu_workers(); 4923fffb589SSandeep Dhavale out_error_pcpu_worker: 4933fffb589SSandeep Dhavale destroy_workqueue(z_erofs_workqueue); 4943fffb589SSandeep Dhavale out_error_workqueue_init: 4959f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 4963fffb589SSandeep Dhavale out_error_pcluster_pool: 4979f6cc76eSGao Xiang return err; 49847e4937aSGao Xiang } 49947e4937aSGao Xiang 500db166fc2SGao Xiang enum z_erofs_pclustermode { 501db166fc2SGao Xiang Z_EROFS_PCLUSTER_INFLIGHT, 50247e4937aSGao Xiang /* 503db166fc2SGao Xiang * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it 5040b964600SGao Xiang * could be dispatched into bypass queue later due to uptodated managed 5050b964600SGao Xiang * pages. All related online pages cannot be reused for inplace I/O (or 506387bab87SGao Xiang * bvpage) since it can be directly decoded without I/O submission. 5070b964600SGao Xiang */ 508db166fc2SGao Xiang Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE, 50947e4937aSGao Xiang /* 510dcba1b23SGao Xiang * The pcluster was just linked to a decompression chain by us. It can 511dcba1b23SGao Xiang * also be linked with the remaining pclusters, which means if the 512dcba1b23SGao Xiang * processing page is the tail page of a pcluster, this pcluster can 513dcba1b23SGao Xiang * safely use the whole page (since the previous pcluster is within the 514dcba1b23SGao Xiang * same chain) for in-place I/O, as illustrated below: 515dcba1b23SGao Xiang * ___________________________________________________ 51647e4937aSGao Xiang * | tail (partial) page | head (partial) page | 517dcba1b23SGao Xiang * | (of the current pcl) | (of the previous pcl) | 518dcba1b23SGao Xiang * |___PCLUSTER_FOLLOWED___|_____PCLUSTER_FOLLOWED_____| 51947e4937aSGao Xiang * 520dcba1b23SGao Xiang * [ (*) the page above can be used as inplace I/O. ] 52147e4937aSGao Xiang */ 522db166fc2SGao Xiang Z_EROFS_PCLUSTER_FOLLOWED, 52347e4937aSGao Xiang }; 52447e4937aSGao Xiang 5255c6dcc57SGao Xiang struct z_erofs_decompress_frontend { 5265c6dcc57SGao Xiang struct inode *const inode; 5275c6dcc57SGao Xiang struct erofs_map_blocks map; 52806a304cdSGao Xiang struct z_erofs_bvec_iter biter; 52947e4937aSGao Xiang 5306ab5eed6SGao Xiang struct page *pagepool; 53106a304cdSGao Xiang struct page *candidate_bvpage; 532967c28b2SGao Xiang struct z_erofs_pcluster *pcl; 53347e4937aSGao Xiang z_erofs_next_pcluster_t owned_head; 534db166fc2SGao Xiang enum z_erofs_pclustermode mode; 53547e4937aSGao Xiang 53647e4937aSGao Xiang /* used for applying cache strategy on the fly */ 53747e4937aSGao Xiang bool backmost; 53847e4937aSGao Xiang erofs_off_t headoffset; 539ed722fbcSGao Xiang 540ed722fbcSGao Xiang /* a pointer used to pick up inplace I/O pages */ 541ed722fbcSGao Xiang unsigned int icur; 54247e4937aSGao Xiang }; 54347e4937aSGao Xiang 54447e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \ 5455c6dcc57SGao Xiang .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 546db166fc2SGao Xiang .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true } 54747e4937aSGao Xiang 5481282dea3SGao Xiang static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe) 5491282dea3SGao Xiang { 5501282dea3SGao Xiang unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy; 5511282dea3SGao Xiang 5521282dea3SGao Xiang if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 5531282dea3SGao Xiang return false; 5541282dea3SGao Xiang 5551282dea3SGao Xiang if (fe->backmost) 5561282dea3SGao Xiang return true; 5571282dea3SGao Xiang 5581282dea3SGao Xiang if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 5591282dea3SGao Xiang fe->map.m_la < fe->headoffset) 5601282dea3SGao Xiang return true; 5611282dea3SGao Xiang 5621282dea3SGao Xiang return false; 5631282dea3SGao Xiang } 5641282dea3SGao Xiang 5656ab5eed6SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe) 56647e4937aSGao Xiang { 5676f39d1e1SGao Xiang struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode)); 5685c6dcc57SGao Xiang struct z_erofs_pcluster *pcl = fe->pcl; 5691282dea3SGao Xiang bool shouldalloc = z_erofs_should_alloc_cache(fe); 57047e4937aSGao Xiang bool standalone = true; 5716f39d1e1SGao Xiang /* 5726f39d1e1SGao Xiang * optimistic allocation without direct reclaim since inplace I/O 5736f39d1e1SGao Xiang * can be used if low memory otherwise. 5746f39d1e1SGao Xiang */ 5751825c8d7SGao Xiang gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 5761825c8d7SGao Xiang __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 577ed722fbcSGao Xiang unsigned int i; 57847e4937aSGao Xiang 579db166fc2SGao Xiang if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED) 58047e4937aSGao Xiang return; 58147e4937aSGao Xiang 582ed722fbcSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 58347e4937aSGao Xiang struct page *page; 584b1ed220cSGao Xiang void *t; /* mark pages just found for debugging */ 5851825c8d7SGao Xiang struct page *newpage = NULL; 58647e4937aSGao Xiang 58747e4937aSGao Xiang /* the compressed page was loaded before */ 588ed722fbcSGao Xiang if (READ_ONCE(pcl->compressed_bvecs[i].page)) 58947e4937aSGao Xiang continue; 59047e4937aSGao Xiang 591ed722fbcSGao Xiang page = find_get_page(mc, pcl->obj.index + i); 59247e4937aSGao Xiang 59347e4937aSGao Xiang if (page) { 594b1ed220cSGao Xiang t = (void *)((unsigned long)page | 1); 5950b964600SGao Xiang } else { 5960b964600SGao Xiang /* I/O is needed, no possible to decompress directly */ 5970b964600SGao Xiang standalone = false; 5981282dea3SGao Xiang if (!shouldalloc) 5991282dea3SGao Xiang continue; 6001282dea3SGao Xiang 6011282dea3SGao Xiang /* 6021282dea3SGao Xiang * try to use cached I/O if page allocation 6031282dea3SGao Xiang * succeeds or fallback to in-place I/O instead 6041282dea3SGao Xiang * to avoid any direct reclaim. 6051282dea3SGao Xiang */ 6066ab5eed6SGao Xiang newpage = erofs_allocpage(&fe->pagepool, gfp); 6071825c8d7SGao Xiang if (!newpage) 60847e4937aSGao Xiang continue; 6091282dea3SGao Xiang set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE); 610b1ed220cSGao Xiang t = (void *)((unsigned long)newpage | 1); 61147e4937aSGao Xiang } 61247e4937aSGao Xiang 613b1ed220cSGao Xiang if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t)) 61447e4937aSGao Xiang continue; 61547e4937aSGao Xiang 616eaa9172aSGao Xiang if (page) 61747e4937aSGao Xiang put_page(page); 618eaa9172aSGao Xiang else if (newpage) 6196ab5eed6SGao Xiang erofs_pagepool_add(&fe->pagepool, newpage); 62047e4937aSGao Xiang } 62147e4937aSGao Xiang 6220b964600SGao Xiang /* 6230b964600SGao Xiang * don't do inplace I/O if all compressed pages are available in 6240b964600SGao Xiang * managed cache since it can be moved to the bypass queue instead. 6250b964600SGao Xiang */ 6260b964600SGao Xiang if (standalone) 627db166fc2SGao Xiang fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 62847e4937aSGao Xiang } 62947e4937aSGao Xiang 63047e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */ 63147e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 63247e4937aSGao Xiang struct erofs_workgroup *grp) 63347e4937aSGao Xiang { 63447e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 63547e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 63647e4937aSGao Xiang int i; 63747e4937aSGao Xiang 638cecf864dSYue Hu DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 63947e4937aSGao Xiang /* 6407674a42fSGao Xiang * refcount of workgroup is now freezed as 0, 64147e4937aSGao Xiang * therefore no need to worry about available decompression users. 64247e4937aSGao Xiang */ 6439f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 644ed722fbcSGao Xiang struct page *page = pcl->compressed_bvecs[i].page; 64547e4937aSGao Xiang 64647e4937aSGao Xiang if (!page) 64747e4937aSGao Xiang continue; 64847e4937aSGao Xiang 64947e4937aSGao Xiang /* block other users from reclaiming or migrating the page */ 65047e4937aSGao Xiang if (!trylock_page(page)) 65147e4937aSGao Xiang return -EBUSY; 65247e4937aSGao Xiang 653f4d4e5fcSYue Hu if (!erofs_page_is_managed(sbi, page)) 65447e4937aSGao Xiang continue; 65547e4937aSGao Xiang 65647e4937aSGao Xiang /* barrier is implied in the following 'unlock_page' */ 657ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 6586aaa7b06SGao Xiang detach_page_private(page); 65947e4937aSGao Xiang unlock_page(page); 66047e4937aSGao Xiang } 66147e4937aSGao Xiang return 0; 66247e4937aSGao Xiang } 66347e4937aSGao Xiang 6647b4e372cSGao Xiang static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp) 66547e4937aSGao Xiang { 6667b4e372cSGao Xiang struct z_erofs_pcluster *pcl = folio_get_private(folio); 6677b4e372cSGao Xiang bool ret; 6687b4e372cSGao Xiang int i; 6697b4e372cSGao Xiang 6707b4e372cSGao Xiang if (!folio_test_private(folio)) 6717b4e372cSGao Xiang return true; 67247e4937aSGao Xiang 6737b4e372cSGao Xiang ret = false; 6747674a42fSGao Xiang spin_lock(&pcl->obj.lockref.lock); 6757674a42fSGao Xiang if (pcl->obj.lockref.count > 0) 6767674a42fSGao Xiang goto out; 6777674a42fSGao Xiang 678cecf864dSYue Hu DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 6799f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 6807b4e372cSGao Xiang if (pcl->compressed_bvecs[i].page == &folio->page) { 681ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 6827b4e372cSGao Xiang ret = true; 68347e4937aSGao Xiang break; 68447e4937aSGao Xiang } 68547e4937aSGao Xiang } 6866aaa7b06SGao Xiang if (ret) 6877b4e372cSGao Xiang folio_detach_private(folio); 6887674a42fSGao Xiang out: 6897674a42fSGao Xiang spin_unlock(&pcl->obj.lockref.lock); 69047e4937aSGao Xiang return ret; 69147e4937aSGao Xiang } 69247e4937aSGao Xiang 6937b4e372cSGao Xiang /* 6947b4e372cSGao Xiang * It will be called only on inode eviction. In case that there are still some 6957b4e372cSGao Xiang * decompression requests in progress, wait with rescheduling for a bit here. 6967b4e372cSGao Xiang * An extra lock could be introduced instead but it seems unnecessary. 6977b4e372cSGao Xiang */ 6987b4e372cSGao Xiang static void z_erofs_cache_invalidate_folio(struct folio *folio, 6997b4e372cSGao Xiang size_t offset, size_t length) 7007b4e372cSGao Xiang { 7017b4e372cSGao Xiang const size_t stop = length + offset; 7027b4e372cSGao Xiang 7037b4e372cSGao Xiang /* Check for potential overflow in debug mode */ 7047b4e372cSGao Xiang DBG_BUGON(stop > folio_size(folio) || stop < length); 7057b4e372cSGao Xiang 7067b4e372cSGao Xiang if (offset == 0 && stop == folio_size(folio)) 7077b4e372cSGao Xiang while (!z_erofs_cache_release_folio(folio, GFP_NOFS)) 7087b4e372cSGao Xiang cond_resched(); 7097b4e372cSGao Xiang } 7107b4e372cSGao Xiang 7117b4e372cSGao Xiang static const struct address_space_operations z_erofs_cache_aops = { 7127b4e372cSGao Xiang .release_folio = z_erofs_cache_release_folio, 7137b4e372cSGao Xiang .invalidate_folio = z_erofs_cache_invalidate_folio, 7147b4e372cSGao Xiang }; 7157b4e372cSGao Xiang 7167b4e372cSGao Xiang int erofs_init_managed_cache(struct super_block *sb) 7177b4e372cSGao Xiang { 7187b4e372cSGao Xiang struct inode *const inode = new_inode(sb); 7197b4e372cSGao Xiang 7207b4e372cSGao Xiang if (!inode) 7217b4e372cSGao Xiang return -ENOMEM; 7227b4e372cSGao Xiang 7237b4e372cSGao Xiang set_nlink(inode, 1); 7247b4e372cSGao Xiang inode->i_size = OFFSET_MAX; 7257b4e372cSGao Xiang inode->i_mapping->a_ops = &z_erofs_cache_aops; 7267b4e372cSGao Xiang mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); 7277b4e372cSGao Xiang EROFS_SB(sb)->managed_cache = inode; 7287b4e372cSGao Xiang return 0; 7297b4e372cSGao Xiang } 7307b4e372cSGao Xiang 7315c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe, 732ed722fbcSGao Xiang struct z_erofs_bvec *bvec) 73347e4937aSGao Xiang { 7345c6dcc57SGao Xiang struct z_erofs_pcluster *const pcl = fe->pcl; 73547e4937aSGao Xiang 736ed722fbcSGao Xiang while (fe->icur > 0) { 737ed722fbcSGao Xiang if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page, 738ed722fbcSGao Xiang NULL, bvec->page)) { 739ed722fbcSGao Xiang pcl->compressed_bvecs[fe->icur] = *bvec; 74047e4937aSGao Xiang return true; 741ed722fbcSGao Xiang } 742ed722fbcSGao Xiang } 74347e4937aSGao Xiang return false; 74447e4937aSGao Xiang } 74547e4937aSGao Xiang 74687ca34a7SGao Xiang /* callers must be with pcluster lock held */ 7475c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe, 7485b220b20SGao Xiang struct z_erofs_bvec *bvec, bool exclusive) 74947e4937aSGao Xiang { 75047e4937aSGao Xiang int ret; 75147e4937aSGao Xiang 752db166fc2SGao Xiang if (exclusive) { 75306a304cdSGao Xiang /* give priority for inplaceio to use file pages first */ 754ed722fbcSGao Xiang if (z_erofs_try_inplace_io(fe, bvec)) 75547e4937aSGao Xiang return 0; 75606a304cdSGao Xiang /* otherwise, check if it can be used as a bvpage */ 757db166fc2SGao Xiang if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED && 75806a304cdSGao Xiang !fe->candidate_bvpage) 75906a304cdSGao Xiang fe->candidate_bvpage = bvec->page; 76006a304cdSGao Xiang } 7616ab5eed6SGao Xiang ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage, 7626ab5eed6SGao Xiang &fe->pagepool); 76306a304cdSGao Xiang fe->pcl->vcnt += (ret >= 0); 76406a304cdSGao Xiang return ret; 76547e4937aSGao Xiang } 76647e4937aSGao Xiang 7675c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f) 76847e4937aSGao Xiang { 7695c6dcc57SGao Xiang struct z_erofs_pcluster *pcl = f->pcl; 7705c6dcc57SGao Xiang z_erofs_next_pcluster_t *owned_head = &f->owned_head; 77147e4937aSGao Xiang 772473e15b0SGao Xiang /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ 773473e15b0SGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 774473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_NIL) { 77547e4937aSGao Xiang *owned_head = &pcl->next; 776473e15b0SGao Xiang /* so we can attach this pcluster to our submission chain. */ 777db166fc2SGao Xiang f->mode = Z_EROFS_PCLUSTER_FOLLOWED; 778473e15b0SGao Xiang return; 779473e15b0SGao Xiang } 780473e15b0SGao Xiang 781967c28b2SGao Xiang /* type 2, it belongs to an ongoing chain */ 782db166fc2SGao Xiang f->mode = Z_EROFS_PCLUSTER_INFLIGHT; 78347e4937aSGao Xiang } 78447e4937aSGao Xiang 78583a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe) 78647e4937aSGao Xiang { 78783a386c0SGao Xiang struct erofs_map_blocks *map = &fe->map; 788cecf864dSYue Hu bool ztailpacking = map->m_flags & EROFS_MAP_META; 78947e4937aSGao Xiang struct z_erofs_pcluster *pcl; 79064094a04SGao Xiang struct erofs_workgroup *grp; 79147e4937aSGao Xiang int err; 79247e4937aSGao Xiang 793c42c0ffeSChen Zhongjin if (!(map->m_flags & EROFS_MAP_ENCODED) || 794c42c0ffeSChen Zhongjin (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) { 7958f899262SGao Xiang DBG_BUGON(1); 7968f899262SGao Xiang return -EFSCORRUPTED; 7978f899262SGao Xiang } 7988f899262SGao Xiang 7999f6cc76eSGao Xiang /* no available pcluster, let's allocate one */ 800cecf864dSYue Hu pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 : 801cecf864dSYue Hu map->m_plen >> PAGE_SHIFT); 8029f6cc76eSGao Xiang if (IS_ERR(pcl)) 8039f6cc76eSGao Xiang return PTR_ERR(pcl); 80447e4937aSGao Xiang 8057674a42fSGao Xiang spin_lock_init(&pcl->obj.lockref.lock); 8068f899262SGao Xiang pcl->algorithmformat = map->m_algorithmformat; 8072bfab9c0SGao Xiang pcl->length = 0; 8082bfab9c0SGao Xiang pcl->partial = true; 80947e4937aSGao Xiang 81047e4937aSGao Xiang /* new pclusters should be claimed as type 1, primary and followed */ 8115c6dcc57SGao Xiang pcl->next = fe->owned_head; 81287ca34a7SGao Xiang pcl->pageofs_out = map->m_la & ~PAGE_MASK; 813db166fc2SGao Xiang fe->mode = Z_EROFS_PCLUSTER_FOLLOWED; 81447e4937aSGao Xiang 81547e4937aSGao Xiang /* 81647e4937aSGao Xiang * lock all primary followed works before visible to others 81747e4937aSGao Xiang * and mutex_trylock *never* fails for a new pcluster. 81847e4937aSGao Xiang */ 81987ca34a7SGao Xiang mutex_init(&pcl->lock); 82087ca34a7SGao Xiang DBG_BUGON(!mutex_trylock(&pcl->lock)); 82147e4937aSGao Xiang 822cecf864dSYue Hu if (ztailpacking) { 823cecf864dSYue Hu pcl->obj.index = 0; /* which indicates ztailpacking */ 8243acea5fcSJingbo Xu pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa); 825cecf864dSYue Hu pcl->tailpacking_size = map->m_plen; 826cecf864dSYue Hu } else { 827cecf864dSYue Hu pcl->obj.index = map->m_pa >> PAGE_SHIFT; 828cecf864dSYue Hu 82983a386c0SGao Xiang grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj); 83064094a04SGao Xiang if (IS_ERR(grp)) { 83164094a04SGao Xiang err = PTR_ERR(grp); 83264094a04SGao Xiang goto err_out; 83364094a04SGao Xiang } 83464094a04SGao Xiang 83564094a04SGao Xiang if (grp != &pcl->obj) { 8365c6dcc57SGao Xiang fe->pcl = container_of(grp, 837cecf864dSYue Hu struct z_erofs_pcluster, obj); 83864094a04SGao Xiang err = -EEXIST; 83964094a04SGao Xiang goto err_out; 84047e4937aSGao Xiang } 841cecf864dSYue Hu } 8425c6dcc57SGao Xiang fe->owned_head = &pcl->next; 8435c6dcc57SGao Xiang fe->pcl = pcl; 8449e579fc1SGao Xiang return 0; 84564094a04SGao Xiang 84664094a04SGao Xiang err_out: 84787ca34a7SGao Xiang mutex_unlock(&pcl->lock); 8489f6cc76eSGao Xiang z_erofs_free_pcluster(pcl); 84964094a04SGao Xiang return err; 85047e4937aSGao Xiang } 85147e4937aSGao Xiang 852dcba1b23SGao Xiang static int z_erofs_pcluster_begin(struct z_erofs_decompress_frontend *fe) 85347e4937aSGao Xiang { 85483a386c0SGao Xiang struct erofs_map_blocks *map = &fe->map; 855*aeebae9dSGao Xiang struct super_block *sb = fe->inode->i_sb; 856*aeebae9dSGao Xiang erofs_blk_t blknr = erofs_blknr(sb, map->m_pa); 8570d823b42SGao Xiang struct erofs_workgroup *grp = NULL; 8589e579fc1SGao Xiang int ret; 85947e4937aSGao Xiang 86087ca34a7SGao Xiang DBG_BUGON(fe->pcl); 86147e4937aSGao Xiang 86287ca34a7SGao Xiang /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */ 8635c6dcc57SGao Xiang DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL); 86447e4937aSGao Xiang 8650d823b42SGao Xiang if (!(map->m_flags & EROFS_MAP_META)) { 866*aeebae9dSGao Xiang grp = erofs_find_workgroup(sb, blknr); 8670d823b42SGao Xiang } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) { 86847e4937aSGao Xiang DBG_BUGON(1); 869cecf864dSYue Hu return -EFSCORRUPTED; 870cecf864dSYue Hu } 87147e4937aSGao Xiang 87264094a04SGao Xiang if (grp) { 8735c6dcc57SGao Xiang fe->pcl = container_of(grp, struct z_erofs_pcluster, obj); 8740d823b42SGao Xiang ret = -EEXIST; 87564094a04SGao Xiang } else { 87683a386c0SGao Xiang ret = z_erofs_register_pcluster(fe); 87764094a04SGao Xiang } 87847e4937aSGao Xiang 8790d823b42SGao Xiang if (ret == -EEXIST) { 880267f2492SGao Xiang mutex_lock(&fe->pcl->lock); 881267f2492SGao Xiang z_erofs_try_to_claim_pcluster(fe); 8820d823b42SGao Xiang } else if (ret) { 8830d823b42SGao Xiang return ret; 8840d823b42SGao Xiang } 885*aeebae9dSGao Xiang 88606a304cdSGao Xiang z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset, 887387bab87SGao Xiang Z_EROFS_INLINE_BVECS, fe->pcl->vcnt); 888*aeebae9dSGao Xiang if (!z_erofs_is_inline_pcluster(fe->pcl)) { 889*aeebae9dSGao Xiang /* bind cache first when cached decompression is preferred */ 890*aeebae9dSGao Xiang z_erofs_bind_cache(fe); 891*aeebae9dSGao Xiang } else { 892*aeebae9dSGao Xiang void *mptr; 893*aeebae9dSGao Xiang 894*aeebae9dSGao Xiang mptr = erofs_read_metabuf(&map->buf, sb, blknr, EROFS_NO_KMAP); 895*aeebae9dSGao Xiang if (IS_ERR(mptr)) { 896*aeebae9dSGao Xiang ret = PTR_ERR(mptr); 897*aeebae9dSGao Xiang erofs_err(sb, "failed to get inline data %d", ret); 898*aeebae9dSGao Xiang return ret; 899*aeebae9dSGao Xiang } 900*aeebae9dSGao Xiang get_page(map->buf.page); 901*aeebae9dSGao Xiang WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, map->buf.page); 902*aeebae9dSGao Xiang fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 903*aeebae9dSGao Xiang } 904*aeebae9dSGao Xiang /* file-backed inplace I/O pages are traversed in reverse order */ 905ed722fbcSGao Xiang fe->icur = z_erofs_pclusterpages(fe->pcl); 90647e4937aSGao Xiang return 0; 90747e4937aSGao Xiang } 90847e4937aSGao Xiang 90947e4937aSGao Xiang /* 91047e4937aSGao Xiang * keep in mind that no referenced pclusters will be freed 91147e4937aSGao Xiang * only after a RCU grace period. 91247e4937aSGao Xiang */ 91347e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head) 91447e4937aSGao Xiang { 91587ca34a7SGao Xiang z_erofs_free_pcluster(container_of(head, 91687ca34a7SGao Xiang struct z_erofs_pcluster, rcu)); 91747e4937aSGao Xiang } 91847e4937aSGao Xiang 91947e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 92047e4937aSGao Xiang { 92147e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 92247e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 92347e4937aSGao Xiang 92487ca34a7SGao Xiang call_rcu(&pcl->rcu, z_erofs_rcu_callback); 92547e4937aSGao Xiang } 92647e4937aSGao Xiang 927dcba1b23SGao Xiang static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe) 92847e4937aSGao Xiang { 92987ca34a7SGao Xiang struct z_erofs_pcluster *pcl = fe->pcl; 93047e4937aSGao Xiang 93187ca34a7SGao Xiang if (!pcl) 932dcba1b23SGao Xiang return; 93347e4937aSGao Xiang 93406a304cdSGao Xiang z_erofs_bvec_iter_end(&fe->biter); 93587ca34a7SGao Xiang mutex_unlock(&pcl->lock); 93647e4937aSGao Xiang 93705b63d2bSGao Xiang if (fe->candidate_bvpage) 93806a304cdSGao Xiang fe->candidate_bvpage = NULL; 93906a304cdSGao Xiang 94047e4937aSGao Xiang /* 94147e4937aSGao Xiang * if all pending pages are added, don't hold its reference 94247e4937aSGao Xiang * any longer if the pcluster isn't hosted by ourselves. 94347e4937aSGao Xiang */ 944db166fc2SGao Xiang if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE) 94587ca34a7SGao Xiang erofs_workgroup_put(&pcl->obj); 94647e4937aSGao Xiang 94787ca34a7SGao Xiang fe->pcl = NULL; 948dcba1b23SGao Xiang fe->backmost = false; 94947e4937aSGao Xiang } 95047e4937aSGao Xiang 9518b00be16SGao Xiang static int z_erofs_read_fragment(struct super_block *sb, struct page *page, 9528b00be16SGao Xiang unsigned int cur, unsigned int end, erofs_off_t pos) 953b15b2e30SYue Hu { 9548b00be16SGao Xiang struct inode *packed_inode = EROFS_SB(sb)->packed_inode; 955b15b2e30SYue Hu struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 9568b00be16SGao Xiang unsigned int cnt; 9578b00be16SGao Xiang u8 *src; 958b15b2e30SYue Hu 959e5126de1SYue Hu if (!packed_inode) 960e5126de1SYue Hu return -EFSCORRUPTED; 961e5126de1SYue Hu 962eb2c5e41SGao Xiang buf.inode = packed_inode; 9638b00be16SGao Xiang for (; cur < end; cur += cnt, pos += cnt) { 9648b00be16SGao Xiang cnt = min_t(unsigned int, end - cur, 9653acea5fcSJingbo Xu sb->s_blocksize - erofs_blkoff(sb, pos)); 966eb2c5e41SGao Xiang src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP); 967b15b2e30SYue Hu if (IS_ERR(src)) { 968b15b2e30SYue Hu erofs_put_metabuf(&buf); 969b15b2e30SYue Hu return PTR_ERR(src); 970b15b2e30SYue Hu } 9718b00be16SGao Xiang memcpy_to_page(page, cur, src + erofs_blkoff(sb, pos), cnt); 972b15b2e30SYue Hu } 973b15b2e30SYue Hu erofs_put_metabuf(&buf); 974b15b2e30SYue Hu return 0; 975b15b2e30SYue Hu } 976b15b2e30SYue Hu 97747e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 9786ab5eed6SGao Xiang struct page *page) 97947e4937aSGao Xiang { 98047e4937aSGao Xiang struct inode *const inode = fe->inode; 98147e4937aSGao Xiang struct erofs_map_blocks *const map = &fe->map; 98247e4937aSGao Xiang const loff_t offset = page_offset(page); 9835b220b20SGao Xiang bool tight = true, exclusive; 9848b00be16SGao Xiang unsigned int cur, end, len, spiltted; 98547e4937aSGao Xiang int err = 0; 98647e4937aSGao Xiang 98747e4937aSGao Xiang /* register locked file pages as online pages in pack */ 98847e4937aSGao Xiang z_erofs_onlinepage_init(page); 98947e4937aSGao Xiang 99047e4937aSGao Xiang spiltted = 0; 99147e4937aSGao Xiang end = PAGE_SIZE; 99247e4937aSGao Xiang repeat: 99347e4937aSGao Xiang cur = end - 1; 99447e4937aSGao Xiang 99539397a46SGao Xiang if (offset + cur < map->m_la || 99639397a46SGao Xiang offset + cur >= map->m_la + map->m_llen) { 997dcba1b23SGao Xiang z_erofs_pcluster_end(fe); 99847e4937aSGao Xiang map->m_la = offset + cur; 99947e4937aSGao Xiang map->m_llen = 0; 100047e4937aSGao Xiang err = z_erofs_map_blocks_iter(inode, map, 0); 10018d8a09b0SGao Xiang if (err) 100267148551SGao Xiang goto out; 1003*aeebae9dSGao Xiang } else if (fe->pcl) { 100439397a46SGao Xiang goto hitted; 100539397a46SGao Xiang } 100647e4937aSGao Xiang 1007*aeebae9dSGao Xiang if ((map->m_flags & EROFS_MAP_MAPPED) && 1008*aeebae9dSGao Xiang !(map->m_flags & EROFS_MAP_FRAGMENT)) { 1009dcba1b23SGao Xiang err = z_erofs_pcluster_begin(fe); 10108d8a09b0SGao Xiang if (err) 101167148551SGao Xiang goto out; 1012cecf864dSYue Hu } 101347e4937aSGao Xiang hitted: 1014dc76ea8cSGao Xiang /* 1015dc76ea8cSGao Xiang * Ensure the current partial page belongs to this submit chain rather 1016dc76ea8cSGao Xiang * than other concurrent submit chains or the noio(bypass) chain since 1017dc76ea8cSGao Xiang * those chains are handled asynchronously thus the page cannot be used 1018387bab87SGao Xiang * for inplace I/O or bvpage (should be processed in a strict order.) 1019dc76ea8cSGao Xiang */ 1020967c28b2SGao Xiang tight &= (fe->mode > Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE); 1021dc76ea8cSGao Xiang 10228191213aSChunhai Guo cur = end - min_t(erofs_off_t, offset + end - map->m_la, end); 10238d8a09b0SGao Xiang if (!(map->m_flags & EROFS_MAP_MAPPED)) { 102447e4937aSGao Xiang zero_user_segment(page, cur, end); 102547e4937aSGao Xiang goto next_part; 102647e4937aSGao Xiang } 1027b15b2e30SYue Hu if (map->m_flags & EROFS_MAP_FRAGMENT) { 10288b00be16SGao Xiang erofs_off_t fpos = offset + cur - map->m_la; 1029b15b2e30SYue Hu 10308b00be16SGao Xiang len = min_t(unsigned int, map->m_llen - fpos, end - cur); 10318b00be16SGao Xiang err = z_erofs_read_fragment(inode->i_sb, page, cur, cur + len, 10328b00be16SGao Xiang EROFS_I(inode)->z_fragmentoff + fpos); 1033b15b2e30SYue Hu if (err) 1034b15b2e30SYue Hu goto out; 1035b15b2e30SYue Hu ++spiltted; 1036b15b2e30SYue Hu tight = false; 1037b15b2e30SYue Hu goto next_part; 1038b15b2e30SYue Hu } 103947e4937aSGao Xiang 10405b220b20SGao Xiang exclusive = (!cur && (!spiltted || tight)); 104147e4937aSGao Xiang if (cur) 1042db166fc2SGao Xiang tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED); 104347e4937aSGao Xiang 104406a304cdSGao Xiang err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) { 104506a304cdSGao Xiang .page = page, 104606a304cdSGao Xiang .offset = offset - map->m_la, 104706a304cdSGao Xiang .end = end, 10485b220b20SGao Xiang }), exclusive); 104905b63d2bSGao Xiang if (err) 105067148551SGao Xiang goto out; 105147e4937aSGao Xiang 105267148551SGao Xiang z_erofs_onlinepage_split(page); 105347e4937aSGao Xiang /* bump up the number of spiltted parts of a page */ 105447e4937aSGao Xiang ++spiltted; 1055267f2492SGao Xiang if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) 1056267f2492SGao Xiang fe->pcl->multibases = true; 10572bfab9c0SGao Xiang if (fe->pcl->length < offset + end - map->m_la) { 10582bfab9c0SGao Xiang fe->pcl->length = offset + end - map->m_la; 10592bfab9c0SGao Xiang fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK; 10602bfab9c0SGao Xiang } 1061e7933278SGao Xiang if ((map->m_flags & EROFS_MAP_FULL_MAPPED) && 1062e7933278SGao Xiang !(map->m_flags & EROFS_MAP_PARTIAL_REF) && 1063e7933278SGao Xiang fe->pcl->length == map->m_llen) 1064e7933278SGao Xiang fe->pcl->partial = false; 106547e4937aSGao Xiang next_part: 10662bfab9c0SGao Xiang /* shorten the remaining extent to update progress */ 106747e4937aSGao Xiang map->m_llen = offset + cur - map->m_la; 10682bfab9c0SGao Xiang map->m_flags &= ~EROFS_MAP_FULL_MAPPED; 106947e4937aSGao Xiang 107047e4937aSGao Xiang end = cur; 107147e4937aSGao Xiang if (end > 0) 107247e4937aSGao Xiang goto repeat; 107347e4937aSGao Xiang 107447e4937aSGao Xiang out: 107567148551SGao Xiang if (err) 107667148551SGao Xiang z_erofs_page_mark_eio(page); 107747e4937aSGao Xiang z_erofs_onlinepage_endio(page); 107847e4937aSGao Xiang return err; 107947e4937aSGao Xiang } 108047e4937aSGao Xiang 1081ef4b4b46SYue Hu static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi, 108240452ffcSHuang Jianan unsigned int readahead_pages) 108340452ffcSHuang Jianan { 1084a2e20a25SMatthew Wilcox (Oracle) /* auto: enable for read_folio, disable for readahead */ 108540452ffcSHuang Jianan if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) && 108640452ffcSHuang Jianan !readahead_pages) 108740452ffcSHuang Jianan return true; 108840452ffcSHuang Jianan 108940452ffcSHuang Jianan if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) && 109040452ffcSHuang Jianan (readahead_pages <= sbi->opt.max_sync_decompress_pages)) 109140452ffcSHuang Jianan return true; 109240452ffcSHuang Jianan 109340452ffcSHuang Jianan return false; 109440452ffcSHuang Jianan } 109540452ffcSHuang Jianan 10966aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page) 10976aaa7b06SGao Xiang { 10986aaa7b06SGao Xiang return !page->mapping && !z_erofs_is_shortlived_page(page); 10996aaa7b06SGao Xiang } 11006aaa7b06SGao Xiang 11014f05687fSGao Xiang struct z_erofs_decompress_backend { 11024f05687fSGao Xiang struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES]; 11034f05687fSGao Xiang struct super_block *sb; 11044f05687fSGao Xiang struct z_erofs_pcluster *pcl; 11054f05687fSGao Xiang 11064f05687fSGao Xiang /* pages with the longest decompressed length for deduplication */ 11074f05687fSGao Xiang struct page **decompressed_pages; 11084f05687fSGao Xiang /* pages to keep the compressed data */ 11094f05687fSGao Xiang struct page **compressed_pages; 11104f05687fSGao Xiang 1111267f2492SGao Xiang struct list_head decompressed_secondary_bvecs; 11124f05687fSGao Xiang struct page **pagepool; 11132bfab9c0SGao Xiang unsigned int onstack_used, nr_pages; 11144f05687fSGao Xiang }; 11154f05687fSGao Xiang 1116267f2492SGao Xiang struct z_erofs_bvec_item { 1117267f2492SGao Xiang struct z_erofs_bvec bvec; 1118267f2492SGao Xiang struct list_head list; 1119267f2492SGao Xiang }; 1120267f2492SGao Xiang 1121267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be, 11223fe96ee0SGao Xiang struct z_erofs_bvec *bvec) 11233fe96ee0SGao Xiang { 1124267f2492SGao Xiang struct z_erofs_bvec_item *item; 1125267f2492SGao Xiang unsigned int pgnr; 11263fe96ee0SGao Xiang 112794c43de7SGao Xiang if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK) && 112894c43de7SGao Xiang (bvec->end == PAGE_SIZE || 112994c43de7SGao Xiang bvec->offset + bvec->end == be->pcl->length)) { 1130267f2492SGao Xiang pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT; 11312bfab9c0SGao Xiang DBG_BUGON(pgnr >= be->nr_pages); 113263bbb856SGao Xiang if (!be->decompressed_pages[pgnr]) { 11333fe96ee0SGao Xiang be->decompressed_pages[pgnr] = bvec->page; 1134267f2492SGao Xiang return; 11353fe96ee0SGao Xiang } 113663bbb856SGao Xiang } 11373fe96ee0SGao Xiang 1138267f2492SGao Xiang /* (cold path) one pcluster is requested multiple times */ 1139267f2492SGao Xiang item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL); 1140267f2492SGao Xiang item->bvec = *bvec; 1141267f2492SGao Xiang list_add(&item->list, &be->decompressed_secondary_bvecs); 1142267f2492SGao Xiang } 1143267f2492SGao Xiang 1144267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be, 1145267f2492SGao Xiang int err) 1146267f2492SGao Xiang { 1147267f2492SGao Xiang unsigned int off0 = be->pcl->pageofs_out; 1148267f2492SGao Xiang struct list_head *p, *n; 1149267f2492SGao Xiang 1150267f2492SGao Xiang list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) { 1151267f2492SGao Xiang struct z_erofs_bvec_item *bvi; 1152267f2492SGao Xiang unsigned int end, cur; 1153267f2492SGao Xiang void *dst, *src; 1154267f2492SGao Xiang 1155267f2492SGao Xiang bvi = container_of(p, struct z_erofs_bvec_item, list); 1156267f2492SGao Xiang cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0; 1157267f2492SGao Xiang end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset, 1158267f2492SGao Xiang bvi->bvec.end); 1159267f2492SGao Xiang dst = kmap_local_page(bvi->bvec.page); 1160267f2492SGao Xiang while (cur < end) { 1161267f2492SGao Xiang unsigned int pgnr, scur, len; 1162267f2492SGao Xiang 1163267f2492SGao Xiang pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT; 1164267f2492SGao Xiang DBG_BUGON(pgnr >= be->nr_pages); 1165267f2492SGao Xiang 1166267f2492SGao Xiang scur = bvi->bvec.offset + cur - 1167267f2492SGao Xiang ((pgnr << PAGE_SHIFT) - off0); 1168267f2492SGao Xiang len = min_t(unsigned int, end - cur, PAGE_SIZE - scur); 1169267f2492SGao Xiang if (!be->decompressed_pages[pgnr]) { 1170267f2492SGao Xiang err = -EFSCORRUPTED; 1171267f2492SGao Xiang cur += len; 1172267f2492SGao Xiang continue; 1173267f2492SGao Xiang } 1174267f2492SGao Xiang src = kmap_local_page(be->decompressed_pages[pgnr]); 1175267f2492SGao Xiang memcpy(dst + cur, src + scur, len); 1176267f2492SGao Xiang kunmap_local(src); 1177267f2492SGao Xiang cur += len; 1178267f2492SGao Xiang } 1179267f2492SGao Xiang kunmap_local(dst); 1180267f2492SGao Xiang if (err) 1181267f2492SGao Xiang z_erofs_page_mark_eio(bvi->bvec.page); 1182267f2492SGao Xiang z_erofs_onlinepage_endio(bvi->bvec.page); 1183267f2492SGao Xiang list_del(p); 1184267f2492SGao Xiang kfree(bvi); 1185267f2492SGao Xiang } 1186267f2492SGao Xiang } 1187267f2492SGao Xiang 1188267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be) 118942fec235SGao Xiang { 11904f05687fSGao Xiang struct z_erofs_pcluster *pcl = be->pcl; 119106a304cdSGao Xiang struct z_erofs_bvec_iter biter; 119206a304cdSGao Xiang struct page *old_bvpage; 1193267f2492SGao Xiang int i; 119442fec235SGao Xiang 1195387bab87SGao Xiang z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0); 119642fec235SGao Xiang for (i = 0; i < pcl->vcnt; ++i) { 119706a304cdSGao Xiang struct z_erofs_bvec bvec; 119842fec235SGao Xiang 119906a304cdSGao Xiang z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage); 120042fec235SGao Xiang 120106a304cdSGao Xiang if (old_bvpage) 12024f05687fSGao Xiang z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 120342fec235SGao Xiang 120406a304cdSGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(bvec.page)); 1205267f2492SGao Xiang z_erofs_do_decompressed_bvec(be, &bvec); 120642fec235SGao Xiang } 120706a304cdSGao Xiang 120806a304cdSGao Xiang old_bvpage = z_erofs_bvec_iter_end(&biter); 120906a304cdSGao Xiang if (old_bvpage) 12104f05687fSGao Xiang z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 121142fec235SGao Xiang } 121242fec235SGao Xiang 12134f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be, 12144f05687fSGao Xiang bool *overlapped) 121567139e36SGao Xiang { 12164f05687fSGao Xiang struct z_erofs_pcluster *pcl = be->pcl; 121767139e36SGao Xiang unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 121867139e36SGao Xiang int i, err = 0; 121967139e36SGao Xiang 122067139e36SGao Xiang *overlapped = false; 122167139e36SGao Xiang for (i = 0; i < pclusterpages; ++i) { 1222ed722fbcSGao Xiang struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i]; 1223ed722fbcSGao Xiang struct page *page = bvec->page; 122467139e36SGao Xiang 122567139e36SGao Xiang /* compressed pages ought to be present before decompressing */ 122667139e36SGao Xiang if (!page) { 122767139e36SGao Xiang DBG_BUGON(1); 122867139e36SGao Xiang continue; 122967139e36SGao Xiang } 1230fe3e5914SGao Xiang be->compressed_pages[i] = page; 123167139e36SGao Xiang 123267139e36SGao Xiang if (z_erofs_is_inline_pcluster(pcl)) { 123367139e36SGao Xiang if (!PageUptodate(page)) 123467139e36SGao Xiang err = -EIO; 123567139e36SGao Xiang continue; 123667139e36SGao Xiang } 123767139e36SGao Xiang 123867139e36SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 123967139e36SGao Xiang if (!z_erofs_is_shortlived_page(page)) { 12404f05687fSGao Xiang if (erofs_page_is_managed(EROFS_SB(be->sb), page)) { 124167139e36SGao Xiang if (!PageUptodate(page)) 124267139e36SGao Xiang err = -EIO; 124367139e36SGao Xiang continue; 124467139e36SGao Xiang } 1245267f2492SGao Xiang z_erofs_do_decompressed_bvec(be, bvec); 124667139e36SGao Xiang *overlapped = true; 124767139e36SGao Xiang } 124867139e36SGao Xiang } 124967139e36SGao Xiang 1250fe3e5914SGao Xiang if (err) 12514f05687fSGao Xiang return err; 12524f05687fSGao Xiang return 0; 125367139e36SGao Xiang } 125467139e36SGao Xiang 12554f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be, 12564f05687fSGao Xiang int err) 125747e4937aSGao Xiang { 12584f05687fSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(be->sb); 12594f05687fSGao Xiang struct z_erofs_pcluster *pcl = be->pcl; 1260cecf864dSYue Hu unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 1261597e2953SYue Hu const struct z_erofs_decompressor *decompressor = 1262597e2953SYue Hu &erofs_decompressors[pcl->algorithmformat]; 12632bfab9c0SGao Xiang unsigned int i, inputsize; 126467148551SGao Xiang int err2; 12652bfab9c0SGao Xiang struct page *page; 12662bfab9c0SGao Xiang bool overlapped; 126747e4937aSGao Xiang 126887ca34a7SGao Xiang mutex_lock(&pcl->lock); 12692bfab9c0SGao Xiang be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT; 127047e4937aSGao Xiang 1271fe3e5914SGao Xiang /* allocate (de)compressed page arrays if cannot be kept on stack */ 1272fe3e5914SGao Xiang be->decompressed_pages = NULL; 1273fe3e5914SGao Xiang be->compressed_pages = NULL; 1274fe3e5914SGao Xiang be->onstack_used = 0; 12752bfab9c0SGao Xiang if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) { 12764f05687fSGao Xiang be->decompressed_pages = be->onstack_pages; 12772bfab9c0SGao Xiang be->onstack_used = be->nr_pages; 12784f05687fSGao Xiang memset(be->decompressed_pages, 0, 12792bfab9c0SGao Xiang sizeof(struct page *) * be->nr_pages); 1280fe3e5914SGao Xiang } 1281fe3e5914SGao Xiang 1282fe3e5914SGao Xiang if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES) 1283fe3e5914SGao Xiang be->compressed_pages = be->onstack_pages + be->onstack_used; 1284fe3e5914SGao Xiang 1285fe3e5914SGao Xiang if (!be->decompressed_pages) 12864f05687fSGao Xiang be->decompressed_pages = 1287647dd2c3SGao Xiang kvcalloc(be->nr_pages, sizeof(struct page *), 1288e7368187SGao Xiang GFP_KERNEL | __GFP_NOFAIL); 1289fe3e5914SGao Xiang if (!be->compressed_pages) 1290fe3e5914SGao Xiang be->compressed_pages = 1291647dd2c3SGao Xiang kvcalloc(pclusterpages, sizeof(struct page *), 1292fe3e5914SGao Xiang GFP_KERNEL | __GFP_NOFAIL); 129347e4937aSGao Xiang 1294267f2492SGao Xiang z_erofs_parse_out_bvecs(be); 12954f05687fSGao Xiang err2 = z_erofs_parse_in_bvecs(be, &overlapped); 12964f05687fSGao Xiang if (err2) 12974f05687fSGao Xiang err = err2; 12988d8a09b0SGao Xiang if (err) 129947e4937aSGao Xiang goto out; 130047e4937aSGao Xiang 1301cecf864dSYue Hu if (z_erofs_is_inline_pcluster(pcl)) 1302cecf864dSYue Hu inputsize = pcl->tailpacking_size; 1303cecf864dSYue Hu else 1304cecf864dSYue Hu inputsize = pclusterpages * PAGE_SIZE; 1305cecf864dSYue Hu 1306597e2953SYue Hu err = decompressor->decompress(&(struct z_erofs_decompress_req) { 13074f05687fSGao Xiang .sb = be->sb, 13084f05687fSGao Xiang .in = be->compressed_pages, 13094f05687fSGao Xiang .out = be->decompressed_pages, 1310cecf864dSYue Hu .pageofs_in = pcl->pageofs_in, 131187ca34a7SGao Xiang .pageofs_out = pcl->pageofs_out, 13129f6cc76eSGao Xiang .inputsize = inputsize, 13132bfab9c0SGao Xiang .outputsize = pcl->length, 131447e4937aSGao Xiang .alg = pcl->algorithmformat, 131547e4937aSGao Xiang .inplace_io = overlapped, 13162bfab9c0SGao Xiang .partial_decoding = pcl->partial, 1317267f2492SGao Xiang .fillgaps = pcl->multibases, 13184f05687fSGao Xiang }, be->pagepool); 131947e4937aSGao Xiang 132047e4937aSGao Xiang out: 1321cecf864dSYue Hu /* must handle all compressed pages before actual file pages */ 1322cecf864dSYue Hu if (z_erofs_is_inline_pcluster(pcl)) { 1323ed722fbcSGao Xiang page = pcl->compressed_bvecs[0].page; 1324ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL); 1325cecf864dSYue Hu put_page(page); 1326cecf864dSYue Hu } else { 1327cecf864dSYue Hu for (i = 0; i < pclusterpages; ++i) { 1328ed722fbcSGao Xiang page = pcl->compressed_bvecs[i].page; 132947e4937aSGao Xiang 133047e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) 133147e4937aSGao Xiang continue; 133247e4937aSGao Xiang 13336aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 13344f05687fSGao Xiang (void)z_erofs_put_shortlivedpage(be->pagepool, page); 1335ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 133647e4937aSGao Xiang } 1337cecf864dSYue Hu } 1338fe3e5914SGao Xiang if (be->compressed_pages < be->onstack_pages || 1339fe3e5914SGao Xiang be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES) 1340647dd2c3SGao Xiang kvfree(be->compressed_pages); 1341267f2492SGao Xiang z_erofs_fill_other_copies(be, err); 134247e4937aSGao Xiang 13432bfab9c0SGao Xiang for (i = 0; i < be->nr_pages; ++i) { 13444f05687fSGao Xiang page = be->decompressed_pages[i]; 134547e4937aSGao Xiang if (!page) 134647e4937aSGao Xiang continue; 134747e4937aSGao Xiang 13486aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 134947e4937aSGao Xiang 13506aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 13514f05687fSGao Xiang if (z_erofs_put_shortlivedpage(be->pagepool, page)) 135247e4937aSGao Xiang continue; 135367148551SGao Xiang if (err) 135467148551SGao Xiang z_erofs_page_mark_eio(page); 135547e4937aSGao Xiang z_erofs_onlinepage_endio(page); 135647e4937aSGao Xiang } 135747e4937aSGao Xiang 13584f05687fSGao Xiang if (be->decompressed_pages != be->onstack_pages) 1359647dd2c3SGao Xiang kvfree(be->decompressed_pages); 136047e4937aSGao Xiang 13612bfab9c0SGao Xiang pcl->length = 0; 13622bfab9c0SGao Xiang pcl->partial = true; 1363267f2492SGao Xiang pcl->multibases = false; 136406a304cdSGao Xiang pcl->bvset.nextpage = NULL; 136587ca34a7SGao Xiang pcl->vcnt = 0; 136647e4937aSGao Xiang 136787ca34a7SGao Xiang /* pcluster lock MUST be taken before the following line */ 136847e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 136987ca34a7SGao Xiang mutex_unlock(&pcl->lock); 137047e4937aSGao Xiang return err; 137147e4937aSGao Xiang } 137247e4937aSGao Xiang 13730c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 1374eaa9172aSGao Xiang struct page **pagepool) 137547e4937aSGao Xiang { 13764f05687fSGao Xiang struct z_erofs_decompress_backend be = { 13774f05687fSGao Xiang .sb = io->sb, 13784f05687fSGao Xiang .pagepool = pagepool, 1379267f2492SGao Xiang .decompressed_secondary_bvecs = 1380267f2492SGao Xiang LIST_HEAD_INIT(be.decompressed_secondary_bvecs), 13814f05687fSGao Xiang }; 138247e4937aSGao Xiang z_erofs_next_pcluster_t owned = io->head; 138347e4937aSGao Xiang 1384967c28b2SGao Xiang while (owned != Z_EROFS_PCLUSTER_TAIL) { 138547e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 138647e4937aSGao Xiang 13874f05687fSGao Xiang be.pcl = container_of(owned, struct z_erofs_pcluster, next); 13884f05687fSGao Xiang owned = READ_ONCE(be.pcl->next); 138947e4937aSGao Xiang 13904f05687fSGao Xiang z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0); 13914f05687fSGao Xiang erofs_workgroup_put(&be.pcl->obj); 139247e4937aSGao Xiang } 139347e4937aSGao Xiang } 139447e4937aSGao Xiang 13950c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work) 139647e4937aSGao Xiang { 1397a4b1fab1SGao Xiang struct z_erofs_decompressqueue *bgq = 1398a4b1fab1SGao Xiang container_of(work, struct z_erofs_decompressqueue, u.work); 1399eaa9172aSGao Xiang struct page *pagepool = NULL; 140047e4937aSGao Xiang 1401967c28b2SGao Xiang DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL); 14020c638f70SGao Xiang z_erofs_decompress_queue(bgq, &pagepool); 1403eaa9172aSGao Xiang erofs_release_pages(&pagepool); 1404a4b1fab1SGao Xiang kvfree(bgq); 140547e4937aSGao Xiang } 140647e4937aSGao Xiang 14073fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 14083fffb589SSandeep Dhavale static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work) 14093fffb589SSandeep Dhavale { 14103fffb589SSandeep Dhavale z_erofs_decompressqueue_work((struct work_struct *)work); 14113fffb589SSandeep Dhavale } 14123fffb589SSandeep Dhavale #endif 14133fffb589SSandeep Dhavale 14147865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 1415cdba5506SGao Xiang int bios) 14167865827cSGao Xiang { 14177865827cSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(io->sb); 14187865827cSGao Xiang 14197865827cSGao Xiang /* wake up the caller thread for sync decompression */ 1420cdba5506SGao Xiang if (io->sync) { 14217865827cSGao Xiang if (!atomic_add_return(bios, &io->pending_bios)) 142260b30050SHongyu Jin complete(&io->u.done); 14237865827cSGao Xiang return; 14247865827cSGao Xiang } 14257865827cSGao Xiang 14267865827cSGao Xiang if (atomic_add_return(bios, &io->pending_bios)) 14277865827cSGao Xiang return; 14283fffb589SSandeep Dhavale /* Use (kthread_)work and sync decompression for atomic contexts only */ 142912d0a24aSSandeep Dhavale if (!in_task() || irqs_disabled() || rcu_read_lock_any_held()) { 14303fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 14313fffb589SSandeep Dhavale struct kthread_worker *worker; 14323fffb589SSandeep Dhavale 14333fffb589SSandeep Dhavale rcu_read_lock(); 14343fffb589SSandeep Dhavale worker = rcu_dereference( 14353fffb589SSandeep Dhavale z_erofs_pcpu_workers[raw_smp_processor_id()]); 14363fffb589SSandeep Dhavale if (!worker) { 14373fffb589SSandeep Dhavale INIT_WORK(&io->u.work, z_erofs_decompressqueue_work); 14387865827cSGao Xiang queue_work(z_erofs_workqueue, &io->u.work); 14393fffb589SSandeep Dhavale } else { 14403fffb589SSandeep Dhavale kthread_queue_work(worker, &io->u.kthread_work); 14413fffb589SSandeep Dhavale } 14423fffb589SSandeep Dhavale rcu_read_unlock(); 14433fffb589SSandeep Dhavale #else 14443fffb589SSandeep Dhavale queue_work(z_erofs_workqueue, &io->u.work); 14453fffb589SSandeep Dhavale #endif 14467865827cSGao Xiang /* enable sync decompression for readahead */ 14477865827cSGao Xiang if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) 14487865827cSGao Xiang sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON; 14497865827cSGao Xiang return; 14507865827cSGao Xiang } 14517865827cSGao Xiang z_erofs_decompressqueue_work(&io->u.work); 14527865827cSGao Xiang } 14537865827cSGao Xiang 145447e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 145547e4937aSGao Xiang unsigned int nr, 1456eaa9172aSGao Xiang struct page **pagepool, 14579f2731d6SGao Xiang struct address_space *mc) 145847e4937aSGao Xiang { 145947e4937aSGao Xiang const pgoff_t index = pcl->obj.index; 14609f2731d6SGao Xiang gfp_t gfp = mapping_gfp_mask(mc); 146147e4937aSGao Xiang bool tocache = false; 146247e4937aSGao Xiang 146347e4937aSGao Xiang struct address_space *mapping; 146447e4937aSGao Xiang struct page *oldpage, *page; 146547e4937aSGao Xiang int justfound; 146647e4937aSGao Xiang 146747e4937aSGao Xiang repeat: 1468ed722fbcSGao Xiang page = READ_ONCE(pcl->compressed_bvecs[nr].page); 146947e4937aSGao Xiang oldpage = page; 147047e4937aSGao Xiang 147147e4937aSGao Xiang if (!page) 147247e4937aSGao Xiang goto out_allocpage; 147347e4937aSGao Xiang 1474b1ed220cSGao Xiang justfound = (unsigned long)page & 1UL; 1475b1ed220cSGao Xiang page = (struct page *)((unsigned long)page & ~1UL); 147647e4937aSGao Xiang 14771825c8d7SGao Xiang /* 14781825c8d7SGao Xiang * preallocated cached pages, which is used to avoid direct reclaim 14791825c8d7SGao Xiang * otherwise, it will go inplace I/O path instead. 14801825c8d7SGao Xiang */ 14811825c8d7SGao Xiang if (page->private == Z_EROFS_PREALLOCATED_PAGE) { 1482ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 14831825c8d7SGao Xiang set_page_private(page, 0); 14841825c8d7SGao Xiang tocache = true; 14851825c8d7SGao Xiang goto out_tocache; 14861825c8d7SGao Xiang } 148747e4937aSGao Xiang mapping = READ_ONCE(page->mapping); 148847e4937aSGao Xiang 148947e4937aSGao Xiang /* 14906aaa7b06SGao Xiang * file-backed online pages in plcuster are all locked steady, 149147e4937aSGao Xiang * therefore it is impossible for `mapping' to be NULL. 149247e4937aSGao Xiang */ 149347e4937aSGao Xiang if (mapping && mapping != mc) 149447e4937aSGao Xiang /* ought to be unmanaged pages */ 149547e4937aSGao Xiang goto out; 149647e4937aSGao Xiang 14976aaa7b06SGao Xiang /* directly return for shortlived page as well */ 14986aaa7b06SGao Xiang if (z_erofs_is_shortlived_page(page)) 14996aaa7b06SGao Xiang goto out; 15006aaa7b06SGao Xiang 150147e4937aSGao Xiang lock_page(page); 150247e4937aSGao Xiang 150347e4937aSGao Xiang /* only true if page reclaim goes wrong, should never happen */ 150447e4937aSGao Xiang DBG_BUGON(justfound && PagePrivate(page)); 150547e4937aSGao Xiang 150647e4937aSGao Xiang /* the page is still in manage cache */ 150747e4937aSGao Xiang if (page->mapping == mc) { 1508ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 150947e4937aSGao Xiang 151047e4937aSGao Xiang if (!PagePrivate(page)) { 151147e4937aSGao Xiang /* 151247e4937aSGao Xiang * impossible to be !PagePrivate(page) for 151347e4937aSGao Xiang * the current restriction as well if 1514ed722fbcSGao Xiang * the page is already in compressed_bvecs[]. 151547e4937aSGao Xiang */ 151647e4937aSGao Xiang DBG_BUGON(!justfound); 151747e4937aSGao Xiang 151847e4937aSGao Xiang justfound = 0; 151947e4937aSGao Xiang set_page_private(page, (unsigned long)pcl); 152047e4937aSGao Xiang SetPagePrivate(page); 152147e4937aSGao Xiang } 152247e4937aSGao Xiang 152347e4937aSGao Xiang /* no need to submit io if it is already up-to-date */ 152447e4937aSGao Xiang if (PageUptodate(page)) { 152547e4937aSGao Xiang unlock_page(page); 152647e4937aSGao Xiang page = NULL; 152747e4937aSGao Xiang } 152847e4937aSGao Xiang goto out; 152947e4937aSGao Xiang } 153047e4937aSGao Xiang 153147e4937aSGao Xiang /* 153247e4937aSGao Xiang * the managed page has been truncated, it's unsafe to 153347e4937aSGao Xiang * reuse this one, let's allocate a new cache-managed page. 153447e4937aSGao Xiang */ 153547e4937aSGao Xiang DBG_BUGON(page->mapping); 153647e4937aSGao Xiang DBG_BUGON(!justfound); 153747e4937aSGao Xiang 153847e4937aSGao Xiang tocache = true; 153947e4937aSGao Xiang unlock_page(page); 154047e4937aSGao Xiang put_page(page); 154147e4937aSGao Xiang out_allocpage: 15425ddcee1fSGao Xiang page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 1543ed722fbcSGao Xiang if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page, 1544ed722fbcSGao Xiang oldpage, page)) { 1545eaa9172aSGao Xiang erofs_pagepool_add(pagepool, page); 15465ddcee1fSGao Xiang cond_resched(); 15475ddcee1fSGao Xiang goto repeat; 15485ddcee1fSGao Xiang } 15491825c8d7SGao Xiang out_tocache: 1550bf225074SGao Xiang if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1551bf225074SGao Xiang /* turn into temporary page if fails (1 ref) */ 1552bf225074SGao Xiang set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); 1553bf225074SGao Xiang goto out; 1554a30573b3SGao Xiang } 1555bf225074SGao Xiang attach_page_private(page, pcl); 1556bf225074SGao Xiang /* drop a refcount added by allocpage (then we have 2 refs here) */ 1557bf225074SGao Xiang put_page(page); 1558bf225074SGao Xiang 155947e4937aSGao Xiang out: /* the only exit (for tracing and debugging) */ 156047e4937aSGao Xiang return page; 156147e4937aSGao Xiang } 156247e4937aSGao Xiang 1563cdba5506SGao Xiang static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb, 1564a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 156547e4937aSGao Xiang { 1566a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q; 156747e4937aSGao Xiang 1568a4b1fab1SGao Xiang if (fg && !*fg) { 1569a4b1fab1SGao Xiang q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1570a4b1fab1SGao Xiang if (!q) { 1571a4b1fab1SGao Xiang *fg = true; 1572a4b1fab1SGao Xiang goto fg_out; 157347e4937aSGao Xiang } 15743fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 15753fffb589SSandeep Dhavale kthread_init_work(&q->u.kthread_work, 15763fffb589SSandeep Dhavale z_erofs_decompressqueue_kthread_work); 15773fffb589SSandeep Dhavale #else 15780c638f70SGao Xiang INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 15793fffb589SSandeep Dhavale #endif 1580a4b1fab1SGao Xiang } else { 1581a4b1fab1SGao Xiang fg_out: 1582a4b1fab1SGao Xiang q = fgq; 158360b30050SHongyu Jin init_completion(&fgq->u.done); 1584a4b1fab1SGao Xiang atomic_set(&fgq->pending_bios, 0); 158567148551SGao Xiang q->eio = false; 1586cdba5506SGao Xiang q->sync = true; 1587a4b1fab1SGao Xiang } 1588a4b1fab1SGao Xiang q->sb = sb; 1589967c28b2SGao Xiang q->head = Z_EROFS_PCLUSTER_TAIL; 1590a4b1fab1SGao Xiang return q; 159147e4937aSGao Xiang } 159247e4937aSGao Xiang 159347e4937aSGao Xiang /* define decompression jobqueue types */ 159447e4937aSGao Xiang enum { 159547e4937aSGao Xiang JQ_BYPASS, 159647e4937aSGao Xiang JQ_SUBMIT, 159747e4937aSGao Xiang NR_JOBQUEUES, 159847e4937aSGao Xiang }; 159947e4937aSGao Xiang 160047e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 160147e4937aSGao Xiang z_erofs_next_pcluster_t qtail[], 160247e4937aSGao Xiang z_erofs_next_pcluster_t owned_head) 160347e4937aSGao Xiang { 160447e4937aSGao Xiang z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 160547e4937aSGao Xiang z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 160647e4937aSGao Xiang 1607967c28b2SGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL); 160847e4937aSGao Xiang 160947e4937aSGao Xiang WRITE_ONCE(*submit_qtail, owned_head); 161047e4937aSGao Xiang WRITE_ONCE(*bypass_qtail, &pcl->next); 161147e4937aSGao Xiang 161247e4937aSGao Xiang qtail[JQ_BYPASS] = &pcl->next; 161347e4937aSGao Xiang } 161447e4937aSGao Xiang 16157865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio) 16167865827cSGao Xiang { 1617cdba5506SGao Xiang struct z_erofs_decompressqueue *q = bio->bi_private; 16187865827cSGao Xiang blk_status_t err = bio->bi_status; 16197865827cSGao Xiang struct bio_vec *bvec; 16207865827cSGao Xiang struct bvec_iter_all iter_all; 16217865827cSGao Xiang 16227865827cSGao Xiang bio_for_each_segment_all(bvec, bio, iter_all) { 16237865827cSGao Xiang struct page *page = bvec->bv_page; 16247865827cSGao Xiang 16257865827cSGao Xiang DBG_BUGON(PageUptodate(page)); 16267865827cSGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 16277865827cSGao Xiang 16287865827cSGao Xiang if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 16297865827cSGao Xiang if (!err) 16307865827cSGao Xiang SetPageUptodate(page); 16317865827cSGao Xiang unlock_page(page); 16327865827cSGao Xiang } 16337865827cSGao Xiang } 163467148551SGao Xiang if (err) 163567148551SGao Xiang q->eio = true; 1636cdba5506SGao Xiang z_erofs_decompress_kickoff(q, -1); 16377865827cSGao Xiang bio_put(bio); 16387865827cSGao Xiang } 16397865827cSGao Xiang 164083a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f, 1641a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, 1642ef4b4b46SYue Hu bool *force_fg, bool readahead) 164347e4937aSGao Xiang { 164483a386c0SGao Xiang struct super_block *sb = f->inode->i_sb; 164583a386c0SGao Xiang struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb)); 164647e4937aSGao Xiang z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1647a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 16485c6dcc57SGao Xiang z_erofs_next_pcluster_t owned_head = f->owned_head; 1649dfeab2e9SGao Xiang /* bio is NULL initially, so no need to initialize last_{index,bdev} */ 16503f649ab7SKees Cook pgoff_t last_index; 1651dfeab2e9SGao Xiang struct block_device *last_bdev; 16521e4a2955SGao Xiang unsigned int nr_bios = 0; 16531e4a2955SGao Xiang struct bio *bio = NULL; 165482e60d00SJohannes Weiner unsigned long pflags; 165582e60d00SJohannes Weiner int memstall = 0; 165647e4937aSGao Xiang 1657cdba5506SGao Xiang /* 1658cdba5506SGao Xiang * if managed cache is enabled, bypass jobqueue is needed, 1659cdba5506SGao Xiang * no need to read from device for all pclusters in this queue. 1660cdba5506SGao Xiang */ 1661cdba5506SGao Xiang q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1662cdba5506SGao Xiang q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg); 1663cdba5506SGao Xiang 1664a4b1fab1SGao Xiang qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1665a4b1fab1SGao Xiang qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 166647e4937aSGao Xiang 166747e4937aSGao Xiang /* by default, all need io submission */ 166847e4937aSGao Xiang q[JQ_SUBMIT]->head = owned_head; 166947e4937aSGao Xiang 167047e4937aSGao Xiang do { 1671dfeab2e9SGao Xiang struct erofs_map_dev mdev; 167247e4937aSGao Xiang struct z_erofs_pcluster *pcl; 16731e4a2955SGao Xiang pgoff_t cur, end; 16741e4a2955SGao Xiang unsigned int i = 0; 16751e4a2955SGao Xiang bool bypass = true; 167647e4937aSGao Xiang 167747e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 167847e4937aSGao Xiang pcl = container_of(owned_head, struct z_erofs_pcluster, next); 1679967c28b2SGao Xiang owned_head = READ_ONCE(pcl->next); 168047e4937aSGao Xiang 1681cecf864dSYue Hu if (z_erofs_is_inline_pcluster(pcl)) { 1682cecf864dSYue Hu move_to_bypass_jobqueue(pcl, qtail, owned_head); 1683cecf864dSYue Hu continue; 1684cecf864dSYue Hu } 1685cecf864dSYue Hu 1686dfeab2e9SGao Xiang /* no device id here, thus it will always succeed */ 1687dfeab2e9SGao Xiang mdev = (struct erofs_map_dev) { 16883acea5fcSJingbo Xu .m_pa = erofs_pos(sb, pcl->obj.index), 1689dfeab2e9SGao Xiang }; 1690dfeab2e9SGao Xiang (void)erofs_map_dev(sb, &mdev); 1691dfeab2e9SGao Xiang 16923acea5fcSJingbo Xu cur = erofs_blknr(sb, mdev.m_pa); 16939f6cc76eSGao Xiang end = cur + pcl->pclusterpages; 169447e4937aSGao Xiang 16951e4a2955SGao Xiang do { 16961e4a2955SGao Xiang struct page *page; 169747e4937aSGao Xiang 16986ab5eed6SGao Xiang page = pickup_page_for_submission(pcl, i++, 16996ab5eed6SGao Xiang &f->pagepool, mc); 17001e4a2955SGao Xiang if (!page) 17011e4a2955SGao Xiang continue; 170247e4937aSGao Xiang 1703dfeab2e9SGao Xiang if (bio && (cur != last_index + 1 || 1704dfeab2e9SGao Xiang last_bdev != mdev.m_bdev)) { 170547e4937aSGao Xiang submit_bio_retry: 170694e4e153SGao Xiang submit_bio(bio); 170782e60d00SJohannes Weiner if (memstall) { 170882e60d00SJohannes Weiner psi_memstall_leave(&pflags); 170982e60d00SJohannes Weiner memstall = 0; 171082e60d00SJohannes Weiner } 171147e4937aSGao Xiang bio = NULL; 171247e4937aSGao Xiang } 171347e4937aSGao Xiang 171482e60d00SJohannes Weiner if (unlikely(PageWorkingset(page)) && !memstall) { 171599486c51SChristoph Hellwig psi_memstall_enter(&pflags); 171682e60d00SJohannes Weiner memstall = 1; 171782e60d00SJohannes Weiner } 171899486c51SChristoph Hellwig 171947e4937aSGao Xiang if (!bio) { 172007888c66SChristoph Hellwig bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS, 172107888c66SChristoph Hellwig REQ_OP_READ, GFP_NOIO); 17220c638f70SGao Xiang bio->bi_end_io = z_erofs_decompressqueue_endio; 1723dfeab2e9SGao Xiang 1724dfeab2e9SGao Xiang last_bdev = mdev.m_bdev; 17251e4a2955SGao Xiang bio->bi_iter.bi_sector = (sector_t)cur << 17263acea5fcSJingbo Xu (sb->s_blocksize_bits - 9); 1727cdba5506SGao Xiang bio->bi_private = q[JQ_SUBMIT]; 1728ef4b4b46SYue Hu if (readahead) 17296ea5aad3SGao Xiang bio->bi_opf |= REQ_RAHEAD; 173047e4937aSGao Xiang ++nr_bios; 173147e4937aSGao Xiang } 173247e4937aSGao Xiang 17336c3e485eSGao Xiang if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 173447e4937aSGao Xiang goto submit_bio_retry; 173547e4937aSGao Xiang 17361e4a2955SGao Xiang last_index = cur; 17371e4a2955SGao Xiang bypass = false; 17381e4a2955SGao Xiang } while (++cur < end); 173947e4937aSGao Xiang 17401e4a2955SGao Xiang if (!bypass) 174147e4937aSGao Xiang qtail[JQ_SUBMIT] = &pcl->next; 174247e4937aSGao Xiang else 174347e4937aSGao Xiang move_to_bypass_jobqueue(pcl, qtail, owned_head); 174447e4937aSGao Xiang } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 174547e4937aSGao Xiang 174699486c51SChristoph Hellwig if (bio) { 174794e4e153SGao Xiang submit_bio(bio); 174882e60d00SJohannes Weiner if (memstall) 174982e60d00SJohannes Weiner psi_memstall_leave(&pflags); 175099486c51SChristoph Hellwig } 175147e4937aSGao Xiang 1752587a67b7SGao Xiang /* 1753587a67b7SGao Xiang * although background is preferred, no one is pending for submission. 17543fffb589SSandeep Dhavale * don't issue decompression but drop it directly instead. 1755587a67b7SGao Xiang */ 1756587a67b7SGao Xiang if (!*force_fg && !nr_bios) { 1757587a67b7SGao Xiang kvfree(q[JQ_SUBMIT]); 17581e4a2955SGao Xiang return; 1759587a67b7SGao Xiang } 1760cdba5506SGao Xiang z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios); 176147e4937aSGao Xiang } 176247e4937aSGao Xiang 176383a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f, 17646ab5eed6SGao Xiang bool force_fg, bool ra) 176547e4937aSGao Xiang { 1766a4b1fab1SGao Xiang struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 176747e4937aSGao Xiang 17685c6dcc57SGao Xiang if (f->owned_head == Z_EROFS_PCLUSTER_TAIL) 176947e4937aSGao Xiang return; 17706ab5eed6SGao Xiang z_erofs_submit_queue(f, io, &force_fg, ra); 177147e4937aSGao Xiang 17720c638f70SGao Xiang /* handle bypass queue (no i/o pclusters) immediately */ 17736ab5eed6SGao Xiang z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool); 177447e4937aSGao Xiang 177547e4937aSGao Xiang if (!force_fg) 177647e4937aSGao Xiang return; 177747e4937aSGao Xiang 177847e4937aSGao Xiang /* wait until all bios are completed */ 177960b30050SHongyu Jin wait_for_completion_io(&io[JQ_SUBMIT].u.done); 178047e4937aSGao Xiang 17810c638f70SGao Xiang /* handle synchronous decompress queue in the caller context */ 17826ab5eed6SGao Xiang z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool); 178347e4937aSGao Xiang } 178447e4937aSGao Xiang 178538629291SGao Xiang /* 178638629291SGao Xiang * Since partial uptodate is still unimplemented for now, we have to use 178738629291SGao Xiang * approximate readmore strategies as a start. 178838629291SGao Xiang */ 178938629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f, 17906ab5eed6SGao Xiang struct readahead_control *rac, bool backmost) 179138629291SGao Xiang { 179238629291SGao Xiang struct inode *inode = f->inode; 179338629291SGao Xiang struct erofs_map_blocks *map = &f->map; 1794796e9149SYue Hu erofs_off_t cur, end, headoffset = f->headoffset; 179538629291SGao Xiang int err; 179638629291SGao Xiang 179738629291SGao Xiang if (backmost) { 1798796e9149SYue Hu if (rac) 1799796e9149SYue Hu end = headoffset + readahead_length(rac) - 1; 1800796e9149SYue Hu else 1801796e9149SYue Hu end = headoffset + PAGE_SIZE - 1; 180238629291SGao Xiang map->m_la = end; 1803622ceaddSGao Xiang err = z_erofs_map_blocks_iter(inode, map, 1804622ceaddSGao Xiang EROFS_GET_BLOCKS_READMORE); 180538629291SGao Xiang if (err) 180638629291SGao Xiang return; 180738629291SGao Xiang 1808796e9149SYue Hu /* expand ra for the trailing edge if readahead */ 180938629291SGao Xiang if (rac) { 181038629291SGao Xiang cur = round_up(map->m_la + map->m_llen, PAGE_SIZE); 1811796e9149SYue Hu readahead_expand(rac, headoffset, cur - headoffset); 181238629291SGao Xiang return; 181338629291SGao Xiang } 181438629291SGao Xiang end = round_up(end, PAGE_SIZE); 181538629291SGao Xiang } else { 181638629291SGao Xiang end = round_up(map->m_la, PAGE_SIZE); 181738629291SGao Xiang 181838629291SGao Xiang if (!map->m_llen) 181938629291SGao Xiang return; 182038629291SGao Xiang } 182138629291SGao Xiang 182238629291SGao Xiang cur = map->m_la + map->m_llen - 1; 1823936aa701SChunhai Guo while ((cur >= end) && (cur < i_size_read(inode))) { 182438629291SGao Xiang pgoff_t index = cur >> PAGE_SHIFT; 182538629291SGao Xiang struct page *page; 182638629291SGao Xiang 182738629291SGao Xiang page = erofs_grab_cache_page_nowait(inode->i_mapping, index); 1828aa793b46SGao Xiang if (page) { 1829e3157bb5SFerry Meng if (PageUptodate(page)) 183038629291SGao Xiang unlock_page(page); 1831e3157bb5SFerry Meng else 1832e3157bb5SFerry Meng (void)z_erofs_do_read_page(f, page); 183338629291SGao Xiang put_page(page); 1834aa793b46SGao Xiang } 1835aa793b46SGao Xiang 183638629291SGao Xiang if (cur < PAGE_SIZE) 183738629291SGao Xiang break; 183838629291SGao Xiang cur = (index << PAGE_SHIFT) - 1; 183938629291SGao Xiang } 184038629291SGao Xiang } 184138629291SGao Xiang 1842a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio) 184347e4937aSGao Xiang { 1844a2e20a25SMatthew Wilcox (Oracle) struct page *page = &folio->page; 184547e4937aSGao Xiang struct inode *const inode = page->mapping->host; 184640452ffcSHuang Jianan struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 184747e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 184847e4937aSGao Xiang int err; 184947e4937aSGao Xiang 185047e4937aSGao Xiang trace_erofs_readpage(page, false); 185147e4937aSGao Xiang f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 185247e4937aSGao Xiang 18536ab5eed6SGao Xiang z_erofs_pcluster_readmore(&f, NULL, true); 18546ab5eed6SGao Xiang err = z_erofs_do_read_page(&f, page); 18556ab5eed6SGao Xiang z_erofs_pcluster_readmore(&f, NULL, false); 1856dcba1b23SGao Xiang z_erofs_pcluster_end(&f); 185747e4937aSGao Xiang 185847e4937aSGao Xiang /* if some compressed cluster ready, need submit them anyway */ 18596ab5eed6SGao Xiang z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, 0), false); 186047e4937aSGao Xiang 1861e3157bb5SFerry Meng if (err && err != -EINTR) 1862e3157bb5SFerry Meng erofs_err(inode->i_sb, "read error %d @ %lu of nid %llu", 1863e3157bb5SFerry Meng err, folio->index, EROFS_I(inode)->nid); 186447e4937aSGao Xiang 186509c54379SGao Xiang erofs_put_metabuf(&f.map.buf); 18666ab5eed6SGao Xiang erofs_release_pages(&f.pagepool); 186747e4937aSGao Xiang return err; 186847e4937aSGao Xiang } 186947e4937aSGao Xiang 18700615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac) 187147e4937aSGao Xiang { 18720615090cSMatthew Wilcox (Oracle) struct inode *const inode = rac->mapping->host; 187347e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 187447e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 18756ab5eed6SGao Xiang struct page *head = NULL, *page; 187638629291SGao Xiang unsigned int nr_pages; 187747e4937aSGao Xiang 18780615090cSMatthew Wilcox (Oracle) f.headoffset = readahead_pos(rac); 187947e4937aSGao Xiang 18806ab5eed6SGao Xiang z_erofs_pcluster_readmore(&f, rac, true); 188138629291SGao Xiang nr_pages = readahead_count(rac); 188238629291SGao Xiang trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); 188338629291SGao Xiang 18840615090cSMatthew Wilcox (Oracle) while ((page = readahead_page(rac))) { 188547e4937aSGao Xiang set_page_private(page, (unsigned long)head); 188647e4937aSGao Xiang head = page; 188747e4937aSGao Xiang } 188847e4937aSGao Xiang 188947e4937aSGao Xiang while (head) { 189047e4937aSGao Xiang struct page *page = head; 189147e4937aSGao Xiang int err; 189247e4937aSGao Xiang 189347e4937aSGao Xiang /* traversal in reverse order */ 189447e4937aSGao Xiang head = (void *)page_private(page); 189547e4937aSGao Xiang 18966ab5eed6SGao Xiang err = z_erofs_do_read_page(&f, page); 1897e3157bb5SFerry Meng if (err && err != -EINTR) 1898e3157bb5SFerry Meng erofs_err(inode->i_sb, "readahead error %d @ %lu of nid %llu", 1899e3157bb5SFerry Meng err, page->index, EROFS_I(inode)->nid); 190047e4937aSGao Xiang put_page(page); 190147e4937aSGao Xiang } 19026ab5eed6SGao Xiang z_erofs_pcluster_readmore(&f, rac, false); 1903dcba1b23SGao Xiang z_erofs_pcluster_end(&f); 190447e4937aSGao Xiang 19056ab5eed6SGao Xiang z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, nr_pages), true); 190609c54379SGao Xiang erofs_put_metabuf(&f.map.buf); 19076ab5eed6SGao Xiang erofs_release_pages(&f.pagepool); 190847e4937aSGao Xiang } 190947e4937aSGao Xiang 19100c638f70SGao Xiang const struct address_space_operations z_erofs_aops = { 1911a2e20a25SMatthew Wilcox (Oracle) .read_folio = z_erofs_read_folio, 19120615090cSMatthew Wilcox (Oracle) .readahead = z_erofs_readahead, 191347e4937aSGao Xiang }; 1914