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 "zdata.h" 847e4937aSGao Xiang #include "compress.h" 947e4937aSGao Xiang #include <linux/prefetch.h> 1047e4937aSGao Xiang 1147e4937aSGao Xiang #include <trace/events/erofs.h> 1247e4937aSGao Xiang 1347e4937aSGao Xiang /* 149f6cc76eSGao Xiang * since pclustersize is variable for big pcluster feature, introduce slab 159f6cc76eSGao Xiang * pools implementation for different pcluster sizes. 169f6cc76eSGao Xiang */ 179f6cc76eSGao Xiang struct z_erofs_pcluster_slab { 189f6cc76eSGao Xiang struct kmem_cache *slab; 199f6cc76eSGao Xiang unsigned int maxpages; 209f6cc76eSGao Xiang char name[48]; 219f6cc76eSGao Xiang }; 229f6cc76eSGao Xiang 239f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n } 249f6cc76eSGao Xiang 259f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = { 269f6cc76eSGao Xiang _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128), 279f6cc76eSGao Xiang _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES) 289f6cc76eSGao Xiang }; 299f6cc76eSGao Xiang 3006a304cdSGao Xiang struct z_erofs_bvec_iter { 3106a304cdSGao Xiang struct page *bvpage; 3206a304cdSGao Xiang struct z_erofs_bvset *bvset; 3306a304cdSGao Xiang unsigned int nr, cur; 3406a304cdSGao Xiang }; 3506a304cdSGao Xiang 3606a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter) 3706a304cdSGao Xiang { 3806a304cdSGao Xiang if (iter->bvpage) 3906a304cdSGao Xiang kunmap_local(iter->bvset); 4006a304cdSGao Xiang return iter->bvpage; 4106a304cdSGao Xiang } 4206a304cdSGao Xiang 4306a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter) 4406a304cdSGao Xiang { 4506a304cdSGao Xiang unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec; 4606a304cdSGao Xiang /* have to access nextpage in advance, otherwise it will be unmapped */ 4706a304cdSGao Xiang struct page *nextpage = iter->bvset->nextpage; 4806a304cdSGao Xiang struct page *oldpage; 4906a304cdSGao Xiang 5006a304cdSGao Xiang DBG_BUGON(!nextpage); 5106a304cdSGao Xiang oldpage = z_erofs_bvec_iter_end(iter); 5206a304cdSGao Xiang iter->bvpage = nextpage; 5306a304cdSGao Xiang iter->bvset = kmap_local_page(nextpage); 5406a304cdSGao Xiang iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec); 5506a304cdSGao Xiang iter->cur = 0; 5606a304cdSGao Xiang return oldpage; 5706a304cdSGao Xiang } 5806a304cdSGao Xiang 5906a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter, 6006a304cdSGao Xiang struct z_erofs_bvset_inline *bvset, 6106a304cdSGao Xiang unsigned int bootstrap_nr, 6206a304cdSGao Xiang unsigned int cur) 6306a304cdSGao Xiang { 6406a304cdSGao Xiang *iter = (struct z_erofs_bvec_iter) { 6506a304cdSGao Xiang .nr = bootstrap_nr, 6606a304cdSGao Xiang .bvset = (struct z_erofs_bvset *)bvset, 6706a304cdSGao Xiang }; 6806a304cdSGao Xiang 6906a304cdSGao Xiang while (cur > iter->nr) { 7006a304cdSGao Xiang cur -= iter->nr; 7106a304cdSGao Xiang z_erofs_bvset_flip(iter); 7206a304cdSGao Xiang } 7306a304cdSGao Xiang iter->cur = cur; 7406a304cdSGao Xiang } 7506a304cdSGao Xiang 7606a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter, 7706a304cdSGao Xiang struct z_erofs_bvec *bvec, 7806a304cdSGao Xiang struct page **candidate_bvpage) 7906a304cdSGao Xiang { 8006a304cdSGao Xiang if (iter->cur == iter->nr) { 8106a304cdSGao Xiang if (!*candidate_bvpage) 8206a304cdSGao Xiang return -EAGAIN; 8306a304cdSGao Xiang 8406a304cdSGao Xiang DBG_BUGON(iter->bvset->nextpage); 8506a304cdSGao Xiang iter->bvset->nextpage = *candidate_bvpage; 8606a304cdSGao Xiang z_erofs_bvset_flip(iter); 8706a304cdSGao Xiang 8806a304cdSGao Xiang iter->bvset->nextpage = NULL; 8906a304cdSGao Xiang *candidate_bvpage = NULL; 9006a304cdSGao Xiang } 9106a304cdSGao Xiang iter->bvset->bvec[iter->cur++] = *bvec; 9206a304cdSGao Xiang return 0; 9306a304cdSGao Xiang } 9406a304cdSGao Xiang 9506a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter, 9606a304cdSGao Xiang struct z_erofs_bvec *bvec, 9706a304cdSGao Xiang struct page **old_bvpage) 9806a304cdSGao Xiang { 9906a304cdSGao Xiang if (iter->cur == iter->nr) 10006a304cdSGao Xiang *old_bvpage = z_erofs_bvset_flip(iter); 10106a304cdSGao Xiang else 10206a304cdSGao Xiang *old_bvpage = NULL; 10306a304cdSGao Xiang *bvec = iter->bvset->bvec[iter->cur++]; 10406a304cdSGao Xiang } 10506a304cdSGao Xiang 1069f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void) 1079f6cc76eSGao Xiang { 1089f6cc76eSGao Xiang int i; 1099f6cc76eSGao Xiang 1109f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 1119f6cc76eSGao Xiang if (!pcluster_pool[i].slab) 1129f6cc76eSGao Xiang continue; 1139f6cc76eSGao Xiang kmem_cache_destroy(pcluster_pool[i].slab); 1149f6cc76eSGao Xiang pcluster_pool[i].slab = NULL; 1159f6cc76eSGao Xiang } 1169f6cc76eSGao Xiang } 1179f6cc76eSGao Xiang 1189f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void) 1199f6cc76eSGao Xiang { 1209f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs; 1219f6cc76eSGao Xiang struct z_erofs_pcluster *a; 1229f6cc76eSGao Xiang unsigned int size; 1239f6cc76eSGao Xiang 1249f6cc76eSGao Xiang for (pcs = pcluster_pool; 1259f6cc76eSGao Xiang pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) { 126ed722fbcSGao Xiang size = struct_size(a, compressed_bvecs, pcs->maxpages); 1279f6cc76eSGao Xiang 1289f6cc76eSGao Xiang sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages); 1299f6cc76eSGao Xiang pcs->slab = kmem_cache_create(pcs->name, size, 0, 1309f6cc76eSGao Xiang SLAB_RECLAIM_ACCOUNT, NULL); 1319f6cc76eSGao Xiang if (pcs->slab) 1329f6cc76eSGao Xiang continue; 1339f6cc76eSGao Xiang 1349f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 1359f6cc76eSGao Xiang return -ENOMEM; 1369f6cc76eSGao Xiang } 1379f6cc76eSGao Xiang return 0; 1389f6cc76eSGao Xiang } 1399f6cc76eSGao Xiang 1409f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages) 1419f6cc76eSGao Xiang { 1429f6cc76eSGao Xiang int i; 1439f6cc76eSGao Xiang 1449f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 1459f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 1469f6cc76eSGao Xiang struct z_erofs_pcluster *pcl; 1479f6cc76eSGao Xiang 1489f6cc76eSGao Xiang if (nrpages > pcs->maxpages) 1499f6cc76eSGao Xiang continue; 1509f6cc76eSGao Xiang 1519f6cc76eSGao Xiang pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS); 1529f6cc76eSGao Xiang if (!pcl) 1539f6cc76eSGao Xiang return ERR_PTR(-ENOMEM); 1549f6cc76eSGao Xiang pcl->pclusterpages = nrpages; 1559f6cc76eSGao Xiang return pcl; 1569f6cc76eSGao Xiang } 1579f6cc76eSGao Xiang return ERR_PTR(-EINVAL); 1589f6cc76eSGao Xiang } 1599f6cc76eSGao Xiang 1609f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl) 1619f6cc76eSGao Xiang { 162cecf864dSYue Hu unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 1639f6cc76eSGao Xiang int i; 1649f6cc76eSGao Xiang 1659f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 1669f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 1679f6cc76eSGao Xiang 168cecf864dSYue Hu if (pclusterpages > pcs->maxpages) 1699f6cc76eSGao Xiang continue; 1709f6cc76eSGao Xiang 1719f6cc76eSGao Xiang kmem_cache_free(pcs->slab, pcl); 1729f6cc76eSGao Xiang return; 1739f6cc76eSGao Xiang } 1749f6cc76eSGao Xiang DBG_BUGON(1); 1759f6cc76eSGao Xiang } 1769f6cc76eSGao Xiang 17747e4937aSGao Xiang /* how to allocate cached pages for a pcluster */ 17847e4937aSGao Xiang enum z_erofs_cache_alloctype { 17947e4937aSGao Xiang DONTALLOC, /* don't allocate any cached pages */ 1801825c8d7SGao Xiang /* 1811825c8d7SGao Xiang * try to use cached I/O if page allocation succeeds or fallback 1821825c8d7SGao Xiang * to in-place I/O instead to avoid any direct reclaim. 1831825c8d7SGao Xiang */ 1841825c8d7SGao Xiang TRYALLOC, 18547e4937aSGao Xiang }; 18647e4937aSGao Xiang 18747e4937aSGao Xiang /* 18847e4937aSGao Xiang * tagged pointer with 1-bit tag for all compressed pages 18947e4937aSGao Xiang * tag 0 - the page is just found with an extra page reference 19047e4937aSGao Xiang */ 19147e4937aSGao Xiang typedef tagptr1_t compressed_page_t; 19247e4937aSGao Xiang 19347e4937aSGao Xiang #define tag_compressed_page_justfound(page) \ 19447e4937aSGao Xiang tagptr_fold(compressed_page_t, page, 1) 19547e4937aSGao Xiang 19647e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly; 19747e4937aSGao Xiang 19847e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void) 19947e4937aSGao Xiang { 20047e4937aSGao Xiang destroy_workqueue(z_erofs_workqueue); 2019f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 20247e4937aSGao Xiang } 20347e4937aSGao Xiang 20499634bf3SGao Xiang static inline int z_erofs_init_workqueue(void) 20547e4937aSGao Xiang { 20647e4937aSGao Xiang const unsigned int onlinecpus = num_possible_cpus(); 20747e4937aSGao Xiang 20847e4937aSGao Xiang /* 20947e4937aSGao Xiang * no need to spawn too many threads, limiting threads could minimum 21047e4937aSGao Xiang * scheduling overhead, perhaps per-CPU threads should be better? 21147e4937aSGao Xiang */ 2120e62ea33SGao Xiang z_erofs_workqueue = alloc_workqueue("erofs_unzipd", 2130e62ea33SGao Xiang WQ_UNBOUND | WQ_HIGHPRI, 21447e4937aSGao Xiang onlinecpus + onlinecpus / 4); 21547e4937aSGao Xiang return z_erofs_workqueue ? 0 : -ENOMEM; 21647e4937aSGao Xiang } 21747e4937aSGao Xiang 21847e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void) 21947e4937aSGao Xiang { 2209f6cc76eSGao Xiang int err = z_erofs_create_pcluster_pool(); 22147e4937aSGao Xiang 2229f6cc76eSGao Xiang if (err) 2239f6cc76eSGao Xiang return err; 2249f6cc76eSGao Xiang err = z_erofs_init_workqueue(); 2259f6cc76eSGao Xiang if (err) 2269f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 2279f6cc76eSGao Xiang return err; 22847e4937aSGao Xiang } 22947e4937aSGao Xiang 230db166fc2SGao Xiang enum z_erofs_pclustermode { 231db166fc2SGao Xiang Z_EROFS_PCLUSTER_INFLIGHT, 23247e4937aSGao Xiang /* 233db166fc2SGao Xiang * The current pclusters was the tail of an exist chain, in addition 234db166fc2SGao Xiang * that the previous processed chained pclusters are all decided to 23547e4937aSGao Xiang * be hooked up to it. 236db166fc2SGao Xiang * A new chain will be created for the remaining pclusters which are 237db166fc2SGao Xiang * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED, 238db166fc2SGao Xiang * the next pcluster cannot reuse the whole page safely for inplace I/O 239db166fc2SGao Xiang * in the following scenario: 24047e4937aSGao Xiang * ________________________________________________________________ 24147e4937aSGao Xiang * | tail (partial) page | head (partial) page | 242db166fc2SGao Xiang * | (belongs to the next pcl) | (belongs to the current pcl) | 243db166fc2SGao Xiang * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________| 24447e4937aSGao Xiang */ 245db166fc2SGao Xiang Z_EROFS_PCLUSTER_HOOKED, 2460b964600SGao Xiang /* 247db166fc2SGao Xiang * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it 2480b964600SGao Xiang * could be dispatched into bypass queue later due to uptodated managed 2490b964600SGao Xiang * pages. All related online pages cannot be reused for inplace I/O (or 250387bab87SGao Xiang * bvpage) since it can be directly decoded without I/O submission. 2510b964600SGao Xiang */ 252db166fc2SGao Xiang Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE, 25347e4937aSGao Xiang /* 25447e4937aSGao Xiang * The current collection has been linked with the owned chain, and 25547e4937aSGao Xiang * could also be linked with the remaining collections, which means 25647e4937aSGao Xiang * if the processing page is the tail page of the collection, thus 25747e4937aSGao Xiang * the current collection can safely use the whole page (since 25847e4937aSGao Xiang * the previous collection is under control) for in-place I/O, as 25947e4937aSGao Xiang * illustrated below: 26047e4937aSGao Xiang * ________________________________________________________________ 26147e4937aSGao Xiang * | tail (partial) page | head (partial) page | 26247e4937aSGao Xiang * | (of the current cl) | (of the previous collection) | 263db166fc2SGao Xiang * | PCLUSTER_FOLLOWED or | | 264db166fc2SGao Xiang * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________| 26547e4937aSGao Xiang * 26647e4937aSGao Xiang * [ (*) the above page can be used as inplace I/O. ] 26747e4937aSGao Xiang */ 268db166fc2SGao Xiang Z_EROFS_PCLUSTER_FOLLOWED, 26947e4937aSGao Xiang }; 27047e4937aSGao Xiang 2715c6dcc57SGao Xiang struct z_erofs_decompress_frontend { 2725c6dcc57SGao Xiang struct inode *const inode; 2735c6dcc57SGao Xiang struct erofs_map_blocks map; 27406a304cdSGao Xiang struct z_erofs_bvec_iter biter; 27547e4937aSGao Xiang 27606a304cdSGao Xiang struct page *candidate_bvpage; 27747e4937aSGao Xiang struct z_erofs_pcluster *pcl, *tailpcl; 27847e4937aSGao Xiang z_erofs_next_pcluster_t owned_head; 279db166fc2SGao Xiang enum z_erofs_pclustermode mode; 28047e4937aSGao Xiang 2816ea5aad3SGao Xiang bool readahead; 28247e4937aSGao Xiang /* used for applying cache strategy on the fly */ 28347e4937aSGao Xiang bool backmost; 28447e4937aSGao Xiang erofs_off_t headoffset; 285ed722fbcSGao Xiang 286ed722fbcSGao Xiang /* a pointer used to pick up inplace I/O pages */ 287ed722fbcSGao Xiang unsigned int icur; 28847e4937aSGao Xiang }; 28947e4937aSGao Xiang 29047e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \ 2915c6dcc57SGao Xiang .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 292db166fc2SGao Xiang .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true } 29347e4937aSGao Xiang 2946f39d1e1SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe, 2951825c8d7SGao Xiang enum z_erofs_cache_alloctype type, 296eaa9172aSGao Xiang struct page **pagepool) 29747e4937aSGao Xiang { 2986f39d1e1SGao Xiang struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode)); 2995c6dcc57SGao Xiang struct z_erofs_pcluster *pcl = fe->pcl; 30047e4937aSGao Xiang bool standalone = true; 3016f39d1e1SGao Xiang /* 3026f39d1e1SGao Xiang * optimistic allocation without direct reclaim since inplace I/O 3036f39d1e1SGao Xiang * can be used if low memory otherwise. 3046f39d1e1SGao Xiang */ 3051825c8d7SGao Xiang gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 3061825c8d7SGao Xiang __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 307ed722fbcSGao Xiang unsigned int i; 30847e4937aSGao Xiang 309db166fc2SGao Xiang if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED) 31047e4937aSGao Xiang return; 31147e4937aSGao Xiang 312ed722fbcSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 31347e4937aSGao Xiang struct page *page; 31447e4937aSGao Xiang compressed_page_t t; 3151825c8d7SGao Xiang struct page *newpage = NULL; 31647e4937aSGao Xiang 31747e4937aSGao Xiang /* the compressed page was loaded before */ 318ed722fbcSGao Xiang if (READ_ONCE(pcl->compressed_bvecs[i].page)) 31947e4937aSGao Xiang continue; 32047e4937aSGao Xiang 321ed722fbcSGao Xiang page = find_get_page(mc, pcl->obj.index + i); 32247e4937aSGao Xiang 32347e4937aSGao Xiang if (page) { 32447e4937aSGao Xiang t = tag_compressed_page_justfound(page); 3250b964600SGao Xiang } else { 3260b964600SGao Xiang /* I/O is needed, no possible to decompress directly */ 3270b964600SGao Xiang standalone = false; 3280b964600SGao Xiang switch (type) { 3290b964600SGao Xiang case TRYALLOC: 3301825c8d7SGao Xiang newpage = erofs_allocpage(pagepool, gfp); 3311825c8d7SGao Xiang if (!newpage) 33247e4937aSGao Xiang continue; 3330b964600SGao Xiang set_page_private(newpage, 3340b964600SGao Xiang Z_EROFS_PREALLOCATED_PAGE); 3350b964600SGao Xiang t = tag_compressed_page_justfound(newpage); 3360b964600SGao Xiang break; 3370b964600SGao Xiang default: /* DONTALLOC */ 3380b964600SGao Xiang continue; 3390b964600SGao Xiang } 34047e4937aSGao Xiang } 34147e4937aSGao Xiang 342ed722fbcSGao Xiang if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, 343ed722fbcSGao Xiang tagptr_cast_ptr(t))) 34447e4937aSGao Xiang continue; 34547e4937aSGao Xiang 346eaa9172aSGao Xiang if (page) 34747e4937aSGao Xiang put_page(page); 348eaa9172aSGao Xiang else if (newpage) 349eaa9172aSGao Xiang erofs_pagepool_add(pagepool, newpage); 35047e4937aSGao Xiang } 35147e4937aSGao Xiang 3520b964600SGao Xiang /* 3530b964600SGao Xiang * don't do inplace I/O if all compressed pages are available in 3540b964600SGao Xiang * managed cache since it can be moved to the bypass queue instead. 3550b964600SGao Xiang */ 3560b964600SGao Xiang if (standalone) 357db166fc2SGao Xiang fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 35847e4937aSGao Xiang } 35947e4937aSGao Xiang 36047e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */ 36147e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 36247e4937aSGao Xiang struct erofs_workgroup *grp) 36347e4937aSGao Xiang { 36447e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 36547e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 36647e4937aSGao Xiang int i; 36747e4937aSGao Xiang 368cecf864dSYue Hu DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 36947e4937aSGao Xiang /* 37047e4937aSGao Xiang * refcount of workgroup is now freezed as 1, 37147e4937aSGao Xiang * therefore no need to worry about available decompression users. 37247e4937aSGao Xiang */ 3739f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 374ed722fbcSGao Xiang struct page *page = pcl->compressed_bvecs[i].page; 37547e4937aSGao Xiang 37647e4937aSGao Xiang if (!page) 37747e4937aSGao Xiang continue; 37847e4937aSGao Xiang 37947e4937aSGao Xiang /* block other users from reclaiming or migrating the page */ 38047e4937aSGao Xiang if (!trylock_page(page)) 38147e4937aSGao Xiang return -EBUSY; 38247e4937aSGao Xiang 383f4d4e5fcSYue Hu if (!erofs_page_is_managed(sbi, page)) 38447e4937aSGao Xiang continue; 38547e4937aSGao Xiang 38647e4937aSGao Xiang /* barrier is implied in the following 'unlock_page' */ 387ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 3886aaa7b06SGao Xiang detach_page_private(page); 38947e4937aSGao Xiang unlock_page(page); 39047e4937aSGao Xiang } 39147e4937aSGao Xiang return 0; 39247e4937aSGao Xiang } 39347e4937aSGao Xiang 394d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page) 39547e4937aSGao Xiang { 39647e4937aSGao Xiang struct z_erofs_pcluster *const pcl = (void *)page_private(page); 397ed722fbcSGao Xiang int ret, i; 39847e4937aSGao Xiang 399ed722fbcSGao Xiang if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1)) 400ed722fbcSGao Xiang return 0; 40147e4937aSGao Xiang 402ed722fbcSGao Xiang ret = 0; 403cecf864dSYue Hu DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 4049f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 405ed722fbcSGao Xiang if (pcl->compressed_bvecs[i].page == page) { 406ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 40747e4937aSGao Xiang ret = 1; 40847e4937aSGao Xiang break; 40947e4937aSGao Xiang } 41047e4937aSGao Xiang } 41147e4937aSGao Xiang erofs_workgroup_unfreeze(&pcl->obj, 1); 4126aaa7b06SGao Xiang if (ret) 4136aaa7b06SGao Xiang detach_page_private(page); 41447e4937aSGao Xiang return ret; 41547e4937aSGao Xiang } 41647e4937aSGao Xiang 4175c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe, 418ed722fbcSGao Xiang struct z_erofs_bvec *bvec) 41947e4937aSGao Xiang { 4205c6dcc57SGao Xiang struct z_erofs_pcluster *const pcl = fe->pcl; 42147e4937aSGao Xiang 422ed722fbcSGao Xiang while (fe->icur > 0) { 423ed722fbcSGao Xiang if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page, 424ed722fbcSGao Xiang NULL, bvec->page)) { 425ed722fbcSGao Xiang pcl->compressed_bvecs[fe->icur] = *bvec; 42647e4937aSGao Xiang return true; 427ed722fbcSGao Xiang } 428ed722fbcSGao Xiang } 42947e4937aSGao Xiang return false; 43047e4937aSGao Xiang } 43147e4937aSGao Xiang 43287ca34a7SGao Xiang /* callers must be with pcluster lock held */ 4335c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe, 4345b220b20SGao Xiang struct z_erofs_bvec *bvec, bool exclusive) 43547e4937aSGao Xiang { 43647e4937aSGao Xiang int ret; 43747e4937aSGao Xiang 438db166fc2SGao Xiang if (exclusive) { 43906a304cdSGao Xiang /* give priority for inplaceio to use file pages first */ 440ed722fbcSGao Xiang if (z_erofs_try_inplace_io(fe, bvec)) 44147e4937aSGao Xiang return 0; 44206a304cdSGao Xiang /* otherwise, check if it can be used as a bvpage */ 443db166fc2SGao Xiang if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED && 44406a304cdSGao Xiang !fe->candidate_bvpage) 44506a304cdSGao Xiang fe->candidate_bvpage = bvec->page; 44606a304cdSGao Xiang } 44706a304cdSGao Xiang ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage); 44806a304cdSGao Xiang fe->pcl->vcnt += (ret >= 0); 44906a304cdSGao Xiang return ret; 45047e4937aSGao Xiang } 45147e4937aSGao Xiang 4525c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f) 45347e4937aSGao Xiang { 4545c6dcc57SGao Xiang struct z_erofs_pcluster *pcl = f->pcl; 4555c6dcc57SGao Xiang z_erofs_next_pcluster_t *owned_head = &f->owned_head; 45647e4937aSGao Xiang 457473e15b0SGao Xiang /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ 458473e15b0SGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 459473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_NIL) { 46047e4937aSGao Xiang *owned_head = &pcl->next; 461473e15b0SGao Xiang /* so we can attach this pcluster to our submission chain. */ 462db166fc2SGao Xiang f->mode = Z_EROFS_PCLUSTER_FOLLOWED; 463473e15b0SGao Xiang return; 464473e15b0SGao Xiang } 465473e15b0SGao Xiang 46647e4937aSGao Xiang /* 467473e15b0SGao Xiang * type 2, link to the end of an existing open chain, be careful 468473e15b0SGao Xiang * that its submission is controlled by the original attached chain. 46947e4937aSGao Xiang */ 470267f2492SGao Xiang if (*owned_head != &pcl->next && pcl != f->tailpcl && 471267f2492SGao Xiang cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 472473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_TAIL) { 47347e4937aSGao Xiang *owned_head = Z_EROFS_PCLUSTER_TAIL; 474db166fc2SGao Xiang f->mode = Z_EROFS_PCLUSTER_HOOKED; 4755c6dcc57SGao Xiang f->tailpcl = NULL; 476473e15b0SGao Xiang return; 47747e4937aSGao Xiang } 478473e15b0SGao Xiang /* type 3, it belongs to a chain, but it isn't the end of the chain */ 479db166fc2SGao Xiang f->mode = Z_EROFS_PCLUSTER_INFLIGHT; 48047e4937aSGao Xiang } 48147e4937aSGao Xiang 48283a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe) 48347e4937aSGao Xiang { 48483a386c0SGao Xiang struct erofs_map_blocks *map = &fe->map; 485cecf864dSYue Hu bool ztailpacking = map->m_flags & EROFS_MAP_META; 48647e4937aSGao Xiang struct z_erofs_pcluster *pcl; 48764094a04SGao Xiang struct erofs_workgroup *grp; 48847e4937aSGao Xiang int err; 48947e4937aSGao Xiang 4908f899262SGao Xiang if (!(map->m_flags & EROFS_MAP_ENCODED)) { 4918f899262SGao Xiang DBG_BUGON(1); 4928f899262SGao Xiang return -EFSCORRUPTED; 4938f899262SGao Xiang } 4948f899262SGao Xiang 4959f6cc76eSGao Xiang /* no available pcluster, let's allocate one */ 496cecf864dSYue Hu pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 : 497cecf864dSYue Hu map->m_plen >> PAGE_SHIFT); 4989f6cc76eSGao Xiang if (IS_ERR(pcl)) 4999f6cc76eSGao Xiang return PTR_ERR(pcl); 50047e4937aSGao Xiang 50164094a04SGao Xiang atomic_set(&pcl->obj.refcount, 1); 5028f899262SGao Xiang pcl->algorithmformat = map->m_algorithmformat; 5032bfab9c0SGao Xiang pcl->length = 0; 5042bfab9c0SGao Xiang pcl->partial = true; 50547e4937aSGao Xiang 50647e4937aSGao Xiang /* new pclusters should be claimed as type 1, primary and followed */ 5075c6dcc57SGao Xiang pcl->next = fe->owned_head; 50887ca34a7SGao Xiang pcl->pageofs_out = map->m_la & ~PAGE_MASK; 509db166fc2SGao Xiang fe->mode = Z_EROFS_PCLUSTER_FOLLOWED; 51047e4937aSGao Xiang 51147e4937aSGao Xiang /* 51247e4937aSGao Xiang * lock all primary followed works before visible to others 51347e4937aSGao Xiang * and mutex_trylock *never* fails for a new pcluster. 51447e4937aSGao Xiang */ 51587ca34a7SGao Xiang mutex_init(&pcl->lock); 51687ca34a7SGao Xiang DBG_BUGON(!mutex_trylock(&pcl->lock)); 51747e4937aSGao Xiang 518cecf864dSYue Hu if (ztailpacking) { 519cecf864dSYue Hu pcl->obj.index = 0; /* which indicates ztailpacking */ 520cecf864dSYue Hu pcl->pageofs_in = erofs_blkoff(map->m_pa); 521cecf864dSYue Hu pcl->tailpacking_size = map->m_plen; 522cecf864dSYue Hu } else { 523cecf864dSYue Hu pcl->obj.index = map->m_pa >> PAGE_SHIFT; 524cecf864dSYue Hu 52583a386c0SGao Xiang grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj); 52664094a04SGao Xiang if (IS_ERR(grp)) { 52764094a04SGao Xiang err = PTR_ERR(grp); 52864094a04SGao Xiang goto err_out; 52964094a04SGao Xiang } 53064094a04SGao Xiang 53164094a04SGao Xiang if (grp != &pcl->obj) { 5325c6dcc57SGao Xiang fe->pcl = container_of(grp, 533cecf864dSYue Hu struct z_erofs_pcluster, obj); 53464094a04SGao Xiang err = -EEXIST; 53564094a04SGao Xiang goto err_out; 53647e4937aSGao Xiang } 537cecf864dSYue Hu } 53847e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 5395c6dcc57SGao Xiang if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) 5405c6dcc57SGao Xiang fe->tailpcl = pcl; 5415c6dcc57SGao Xiang fe->owned_head = &pcl->next; 5425c6dcc57SGao Xiang fe->pcl = pcl; 5439e579fc1SGao Xiang return 0; 54464094a04SGao Xiang 54564094a04SGao Xiang err_out: 54687ca34a7SGao Xiang mutex_unlock(&pcl->lock); 5479f6cc76eSGao Xiang z_erofs_free_pcluster(pcl); 54864094a04SGao Xiang return err; 54947e4937aSGao Xiang } 55047e4937aSGao Xiang 55183a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe) 55247e4937aSGao Xiang { 55383a386c0SGao Xiang struct erofs_map_blocks *map = &fe->map; 5540d823b42SGao Xiang struct erofs_workgroup *grp = NULL; 5559e579fc1SGao Xiang int ret; 55647e4937aSGao Xiang 55787ca34a7SGao Xiang DBG_BUGON(fe->pcl); 55847e4937aSGao Xiang 55987ca34a7SGao Xiang /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */ 5605c6dcc57SGao Xiang DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL); 5615c6dcc57SGao Xiang DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 56247e4937aSGao Xiang 5630d823b42SGao Xiang if (!(map->m_flags & EROFS_MAP_META)) { 5640d823b42SGao Xiang grp = erofs_find_workgroup(fe->inode->i_sb, 5650d823b42SGao Xiang map->m_pa >> PAGE_SHIFT); 5660d823b42SGao Xiang } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) { 56747e4937aSGao Xiang DBG_BUGON(1); 568cecf864dSYue Hu return -EFSCORRUPTED; 569cecf864dSYue Hu } 57047e4937aSGao Xiang 57164094a04SGao Xiang if (grp) { 5725c6dcc57SGao Xiang fe->pcl = container_of(grp, struct z_erofs_pcluster, obj); 5730d823b42SGao Xiang ret = -EEXIST; 57464094a04SGao Xiang } else { 57583a386c0SGao Xiang ret = z_erofs_register_pcluster(fe); 57664094a04SGao Xiang } 57747e4937aSGao Xiang 5780d823b42SGao Xiang if (ret == -EEXIST) { 579267f2492SGao Xiang mutex_lock(&fe->pcl->lock); 580267f2492SGao Xiang /* used to check tail merging loop due to corrupted images */ 581267f2492SGao Xiang if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) 582267f2492SGao Xiang fe->tailpcl = fe->pcl; 583267f2492SGao Xiang 584267f2492SGao Xiang z_erofs_try_to_claim_pcluster(fe); 5850d823b42SGao Xiang } else if (ret) { 5860d823b42SGao Xiang return ret; 5870d823b42SGao Xiang } 58806a304cdSGao Xiang z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset, 589387bab87SGao Xiang Z_EROFS_INLINE_BVECS, fe->pcl->vcnt); 59081382f5fSGao Xiang /* since file-backed online pages are traversed in reverse order */ 591ed722fbcSGao Xiang fe->icur = z_erofs_pclusterpages(fe->pcl); 59247e4937aSGao Xiang return 0; 59347e4937aSGao Xiang } 59447e4937aSGao Xiang 59547e4937aSGao Xiang /* 59647e4937aSGao Xiang * keep in mind that no referenced pclusters will be freed 59747e4937aSGao Xiang * only after a RCU grace period. 59847e4937aSGao Xiang */ 59947e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head) 60047e4937aSGao Xiang { 60187ca34a7SGao Xiang z_erofs_free_pcluster(container_of(head, 60287ca34a7SGao Xiang struct z_erofs_pcluster, rcu)); 60347e4937aSGao Xiang } 60447e4937aSGao Xiang 60547e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 60647e4937aSGao Xiang { 60747e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 60847e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 60947e4937aSGao Xiang 61087ca34a7SGao Xiang call_rcu(&pcl->rcu, z_erofs_rcu_callback); 61147e4937aSGao Xiang } 61247e4937aSGao Xiang 6135c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe) 61447e4937aSGao Xiang { 61587ca34a7SGao Xiang struct z_erofs_pcluster *pcl = fe->pcl; 61647e4937aSGao Xiang 61787ca34a7SGao Xiang if (!pcl) 61847e4937aSGao Xiang return false; 61947e4937aSGao Xiang 62006a304cdSGao Xiang z_erofs_bvec_iter_end(&fe->biter); 62187ca34a7SGao Xiang mutex_unlock(&pcl->lock); 62247e4937aSGao Xiang 62306a304cdSGao Xiang if (fe->candidate_bvpage) { 62406a304cdSGao Xiang DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage)); 62506a304cdSGao Xiang fe->candidate_bvpage = NULL; 62606a304cdSGao Xiang } 62706a304cdSGao Xiang 62847e4937aSGao Xiang /* 62947e4937aSGao Xiang * if all pending pages are added, don't hold its reference 63047e4937aSGao Xiang * any longer if the pcluster isn't hosted by ourselves. 63147e4937aSGao Xiang */ 632db166fc2SGao Xiang if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE) 63387ca34a7SGao Xiang erofs_workgroup_put(&pcl->obj); 63447e4937aSGao Xiang 63587ca34a7SGao Xiang fe->pcl = NULL; 63647e4937aSGao Xiang return true; 63747e4937aSGao Xiang } 63847e4937aSGao Xiang 63947e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe, 64047e4937aSGao Xiang unsigned int cachestrategy, 64147e4937aSGao Xiang erofs_off_t la) 64247e4937aSGao Xiang { 64347e4937aSGao Xiang if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 64447e4937aSGao Xiang return false; 64547e4937aSGao Xiang 64647e4937aSGao Xiang if (fe->backmost) 64747e4937aSGao Xiang return true; 64847e4937aSGao Xiang 64947e4937aSGao Xiang return cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 65047e4937aSGao Xiang la < fe->headoffset; 65147e4937aSGao Xiang } 65247e4937aSGao Xiang 653b15b2e30SYue Hu static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos, 654b15b2e30SYue Hu struct page *page, unsigned int pageofs, 655b15b2e30SYue Hu unsigned int len) 656b15b2e30SYue Hu { 657b15b2e30SYue Hu struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode; 658b15b2e30SYue Hu struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 659b15b2e30SYue Hu u8 *src, *dst; 660b15b2e30SYue Hu unsigned int i, cnt; 661b15b2e30SYue Hu 662b15b2e30SYue Hu pos += EROFS_I(inode)->z_fragmentoff; 663b15b2e30SYue Hu for (i = 0; i < len; i += cnt) { 664b15b2e30SYue Hu cnt = min_t(unsigned int, len - i, 665b15b2e30SYue Hu EROFS_BLKSIZ - erofs_blkoff(pos)); 666b15b2e30SYue Hu src = erofs_bread(&buf, packed_inode, 667b15b2e30SYue Hu erofs_blknr(pos), EROFS_KMAP); 668b15b2e30SYue Hu if (IS_ERR(src)) { 669b15b2e30SYue Hu erofs_put_metabuf(&buf); 670b15b2e30SYue Hu return PTR_ERR(src); 671b15b2e30SYue Hu } 672b15b2e30SYue Hu 673b15b2e30SYue Hu dst = kmap_local_page(page); 674b15b2e30SYue Hu memcpy(dst + pageofs + i, src + erofs_blkoff(pos), cnt); 675b15b2e30SYue Hu kunmap_local(dst); 676b15b2e30SYue Hu pos += cnt; 677b15b2e30SYue Hu } 678b15b2e30SYue Hu erofs_put_metabuf(&buf); 679b15b2e30SYue Hu return 0; 680b15b2e30SYue Hu } 681b15b2e30SYue Hu 68247e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 683eaa9172aSGao Xiang struct page *page, struct page **pagepool) 68447e4937aSGao Xiang { 68547e4937aSGao Xiang struct inode *const inode = fe->inode; 686bda17a45SGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 68747e4937aSGao Xiang struct erofs_map_blocks *const map = &fe->map; 68847e4937aSGao Xiang const loff_t offset = page_offset(page); 6895b220b20SGao Xiang bool tight = true, exclusive; 69047e4937aSGao Xiang 69147e4937aSGao Xiang enum z_erofs_cache_alloctype cache_strategy; 6922bfab9c0SGao Xiang unsigned int cur, end, spiltted; 69347e4937aSGao Xiang int err = 0; 69447e4937aSGao Xiang 69547e4937aSGao Xiang /* register locked file pages as online pages in pack */ 69647e4937aSGao Xiang z_erofs_onlinepage_init(page); 69747e4937aSGao Xiang 69847e4937aSGao Xiang spiltted = 0; 69947e4937aSGao Xiang end = PAGE_SIZE; 70047e4937aSGao Xiang repeat: 70147e4937aSGao Xiang cur = end - 1; 70247e4937aSGao Xiang 70339397a46SGao Xiang if (offset + cur < map->m_la || 70439397a46SGao Xiang offset + cur >= map->m_la + map->m_llen) { 70539397a46SGao Xiang erofs_dbg("out-of-range map @ pos %llu", offset + cur); 70647e4937aSGao Xiang 7075c6dcc57SGao Xiang if (z_erofs_collector_end(fe)) 70847e4937aSGao Xiang fe->backmost = false; 70947e4937aSGao Xiang map->m_la = offset + cur; 71047e4937aSGao Xiang map->m_llen = 0; 71147e4937aSGao Xiang err = z_erofs_map_blocks_iter(inode, map, 0); 7128d8a09b0SGao Xiang if (err) 71367148551SGao Xiang goto out; 71439397a46SGao Xiang } else { 71539397a46SGao Xiang if (fe->pcl) 71639397a46SGao Xiang goto hitted; 71739397a46SGao Xiang /* didn't get a valid pcluster previously (very rare) */ 71839397a46SGao Xiang } 71947e4937aSGao Xiang 720b15b2e30SYue Hu if (!(map->m_flags & EROFS_MAP_MAPPED) || 721b15b2e30SYue Hu map->m_flags & EROFS_MAP_FRAGMENT) 72247e4937aSGao Xiang goto hitted; 72347e4937aSGao Xiang 72483a386c0SGao Xiang err = z_erofs_collector_begin(fe); 7258d8a09b0SGao Xiang if (err) 72667148551SGao Xiang goto out; 72747e4937aSGao Xiang 7285c6dcc57SGao Xiang if (z_erofs_is_inline_pcluster(fe->pcl)) { 72909c54379SGao Xiang void *mp; 730cecf864dSYue Hu 73109c54379SGao Xiang mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb, 73209c54379SGao Xiang erofs_blknr(map->m_pa), EROFS_NO_KMAP); 73309c54379SGao Xiang if (IS_ERR(mp)) { 73409c54379SGao Xiang err = PTR_ERR(mp); 735cecf864dSYue Hu erofs_err(inode->i_sb, 736cecf864dSYue Hu "failed to get inline page, err %d", err); 73767148551SGao Xiang goto out; 738cecf864dSYue Hu } 73909c54379SGao Xiang get_page(fe->map.buf.page); 740ed722fbcSGao Xiang WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, 741ed722fbcSGao Xiang fe->map.buf.page); 742db166fc2SGao Xiang fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 743cecf864dSYue Hu } else { 7446f39d1e1SGao Xiang /* bind cache first when cached decompression is preferred */ 745cecf864dSYue Hu if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy, 746cecf864dSYue Hu map->m_la)) 7471825c8d7SGao Xiang cache_strategy = TRYALLOC; 74847e4937aSGao Xiang else 74947e4937aSGao Xiang cache_strategy = DONTALLOC; 75047e4937aSGao Xiang 7516f39d1e1SGao Xiang z_erofs_bind_cache(fe, cache_strategy, pagepool); 752cecf864dSYue Hu } 75347e4937aSGao Xiang hitted: 754dc76ea8cSGao Xiang /* 755dc76ea8cSGao Xiang * Ensure the current partial page belongs to this submit chain rather 756dc76ea8cSGao Xiang * than other concurrent submit chains or the noio(bypass) chain since 757dc76ea8cSGao Xiang * those chains are handled asynchronously thus the page cannot be used 758387bab87SGao Xiang * for inplace I/O or bvpage (should be processed in a strict order.) 759dc76ea8cSGao Xiang */ 760db166fc2SGao Xiang tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED && 761db166fc2SGao Xiang fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE); 762dc76ea8cSGao Xiang 76347e4937aSGao Xiang cur = end - min_t(unsigned int, offset + end - map->m_la, end); 7648d8a09b0SGao Xiang if (!(map->m_flags & EROFS_MAP_MAPPED)) { 76547e4937aSGao Xiang zero_user_segment(page, cur, end); 76647e4937aSGao Xiang goto next_part; 76747e4937aSGao Xiang } 768b15b2e30SYue Hu if (map->m_flags & EROFS_MAP_FRAGMENT) { 769b15b2e30SYue Hu unsigned int pageofs, skip, len; 770b15b2e30SYue Hu 771b15b2e30SYue Hu if (offset > map->m_la) { 772b15b2e30SYue Hu pageofs = 0; 773b15b2e30SYue Hu skip = offset - map->m_la; 774b15b2e30SYue Hu } else { 775b15b2e30SYue Hu pageofs = map->m_la & ~PAGE_MASK; 776b15b2e30SYue Hu skip = 0; 777b15b2e30SYue Hu } 778b15b2e30SYue Hu len = min_t(unsigned int, map->m_llen - skip, end - cur); 779b15b2e30SYue Hu err = z_erofs_read_fragment(inode, skip, page, pageofs, len); 780b15b2e30SYue Hu if (err) 781b15b2e30SYue Hu goto out; 782b15b2e30SYue Hu ++spiltted; 783b15b2e30SYue Hu tight = false; 784b15b2e30SYue Hu goto next_part; 785b15b2e30SYue Hu } 78647e4937aSGao Xiang 7875b220b20SGao Xiang exclusive = (!cur && (!spiltted || tight)); 78847e4937aSGao Xiang if (cur) 789db166fc2SGao Xiang tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED); 79047e4937aSGao Xiang 79147e4937aSGao Xiang retry: 79206a304cdSGao Xiang err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) { 79306a304cdSGao Xiang .page = page, 79406a304cdSGao Xiang .offset = offset - map->m_la, 79506a304cdSGao Xiang .end = end, 7965b220b20SGao Xiang }), exclusive); 79706a304cdSGao Xiang /* should allocate an additional short-lived page for bvset */ 79806a304cdSGao Xiang if (err == -EAGAIN && !fe->candidate_bvpage) { 79906a304cdSGao Xiang fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL); 80006a304cdSGao Xiang set_page_private(fe->candidate_bvpage, 80106a304cdSGao Xiang Z_EROFS_SHORTLIVED_PAGE); 80247e4937aSGao Xiang goto retry; 80347e4937aSGao Xiang } 80447e4937aSGao Xiang 80506a304cdSGao Xiang if (err) { 80606a304cdSGao Xiang DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage); 80767148551SGao Xiang goto out; 80806a304cdSGao Xiang } 80947e4937aSGao Xiang 81067148551SGao Xiang z_erofs_onlinepage_split(page); 81147e4937aSGao Xiang /* bump up the number of spiltted parts of a page */ 81247e4937aSGao Xiang ++spiltted; 813267f2492SGao Xiang if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) 814267f2492SGao Xiang fe->pcl->multibases = true; 81567148551SGao Xiang 8162bfab9c0SGao Xiang if ((map->m_flags & EROFS_MAP_FULL_MAPPED) && 817*5c2a6425SGao Xiang !(map->m_flags & EROFS_MAP_PARTIAL_REF) && 8182bfab9c0SGao Xiang fe->pcl->length == map->m_llen) 8192bfab9c0SGao Xiang fe->pcl->partial = false; 8202bfab9c0SGao Xiang if (fe->pcl->length < offset + end - map->m_la) { 8212bfab9c0SGao Xiang fe->pcl->length = offset + end - map->m_la; 8222bfab9c0SGao Xiang fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK; 8232bfab9c0SGao Xiang } 82447e4937aSGao Xiang next_part: 8252bfab9c0SGao Xiang /* shorten the remaining extent to update progress */ 82647e4937aSGao Xiang map->m_llen = offset + cur - map->m_la; 8272bfab9c0SGao Xiang map->m_flags &= ~EROFS_MAP_FULL_MAPPED; 82847e4937aSGao Xiang 82947e4937aSGao Xiang end = cur; 83047e4937aSGao Xiang if (end > 0) 83147e4937aSGao Xiang goto repeat; 83247e4937aSGao Xiang 83347e4937aSGao Xiang out: 83467148551SGao Xiang if (err) 83567148551SGao Xiang z_erofs_page_mark_eio(page); 83647e4937aSGao Xiang z_erofs_onlinepage_endio(page); 83747e4937aSGao Xiang 8384f761fa2SGao Xiang erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", 83947e4937aSGao Xiang __func__, page, spiltted, map->m_llen); 84047e4937aSGao Xiang return err; 84147e4937aSGao Xiang } 84247e4937aSGao Xiang 84340452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi, 84440452ffcSHuang Jianan unsigned int readahead_pages) 84540452ffcSHuang Jianan { 846a2e20a25SMatthew Wilcox (Oracle) /* auto: enable for read_folio, disable for readahead */ 84740452ffcSHuang Jianan if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) && 84840452ffcSHuang Jianan !readahead_pages) 84940452ffcSHuang Jianan return true; 85040452ffcSHuang Jianan 85140452ffcSHuang Jianan if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) && 85240452ffcSHuang Jianan (readahead_pages <= sbi->opt.max_sync_decompress_pages)) 85340452ffcSHuang Jianan return true; 85440452ffcSHuang Jianan 85540452ffcSHuang Jianan return false; 85640452ffcSHuang Jianan } 85740452ffcSHuang Jianan 8586aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page) 8596aaa7b06SGao Xiang { 8606aaa7b06SGao Xiang return !page->mapping && !z_erofs_is_shortlived_page(page); 8616aaa7b06SGao Xiang } 8626aaa7b06SGao Xiang 8634f05687fSGao Xiang struct z_erofs_decompress_backend { 8644f05687fSGao Xiang struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES]; 8654f05687fSGao Xiang struct super_block *sb; 8664f05687fSGao Xiang struct z_erofs_pcluster *pcl; 8674f05687fSGao Xiang 8684f05687fSGao Xiang /* pages with the longest decompressed length for deduplication */ 8694f05687fSGao Xiang struct page **decompressed_pages; 8704f05687fSGao Xiang /* pages to keep the compressed data */ 8714f05687fSGao Xiang struct page **compressed_pages; 8724f05687fSGao Xiang 873267f2492SGao Xiang struct list_head decompressed_secondary_bvecs; 8744f05687fSGao Xiang struct page **pagepool; 8752bfab9c0SGao Xiang unsigned int onstack_used, nr_pages; 8764f05687fSGao Xiang }; 8774f05687fSGao Xiang 878267f2492SGao Xiang struct z_erofs_bvec_item { 879267f2492SGao Xiang struct z_erofs_bvec bvec; 880267f2492SGao Xiang struct list_head list; 881267f2492SGao Xiang }; 882267f2492SGao Xiang 883267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be, 8843fe96ee0SGao Xiang struct z_erofs_bvec *bvec) 8853fe96ee0SGao Xiang { 886267f2492SGao Xiang struct z_erofs_bvec_item *item; 887267f2492SGao Xiang 888267f2492SGao Xiang if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) { 889267f2492SGao Xiang unsigned int pgnr; 8903fe96ee0SGao Xiang struct page *oldpage; 8913fe96ee0SGao Xiang 892267f2492SGao Xiang pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT; 8932bfab9c0SGao Xiang DBG_BUGON(pgnr >= be->nr_pages); 8943fe96ee0SGao Xiang oldpage = be->decompressed_pages[pgnr]; 8953fe96ee0SGao Xiang be->decompressed_pages[pgnr] = bvec->page; 8963fe96ee0SGao Xiang 897267f2492SGao Xiang if (!oldpage) 898267f2492SGao Xiang return; 8993fe96ee0SGao Xiang } 9003fe96ee0SGao Xiang 901267f2492SGao Xiang /* (cold path) one pcluster is requested multiple times */ 902267f2492SGao Xiang item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL); 903267f2492SGao Xiang item->bvec = *bvec; 904267f2492SGao Xiang list_add(&item->list, &be->decompressed_secondary_bvecs); 905267f2492SGao Xiang } 906267f2492SGao Xiang 907267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be, 908267f2492SGao Xiang int err) 909267f2492SGao Xiang { 910267f2492SGao Xiang unsigned int off0 = be->pcl->pageofs_out; 911267f2492SGao Xiang struct list_head *p, *n; 912267f2492SGao Xiang 913267f2492SGao Xiang list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) { 914267f2492SGao Xiang struct z_erofs_bvec_item *bvi; 915267f2492SGao Xiang unsigned int end, cur; 916267f2492SGao Xiang void *dst, *src; 917267f2492SGao Xiang 918267f2492SGao Xiang bvi = container_of(p, struct z_erofs_bvec_item, list); 919267f2492SGao Xiang cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0; 920267f2492SGao Xiang end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset, 921267f2492SGao Xiang bvi->bvec.end); 922267f2492SGao Xiang dst = kmap_local_page(bvi->bvec.page); 923267f2492SGao Xiang while (cur < end) { 924267f2492SGao Xiang unsigned int pgnr, scur, len; 925267f2492SGao Xiang 926267f2492SGao Xiang pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT; 927267f2492SGao Xiang DBG_BUGON(pgnr >= be->nr_pages); 928267f2492SGao Xiang 929267f2492SGao Xiang scur = bvi->bvec.offset + cur - 930267f2492SGao Xiang ((pgnr << PAGE_SHIFT) - off0); 931267f2492SGao Xiang len = min_t(unsigned int, end - cur, PAGE_SIZE - scur); 932267f2492SGao Xiang if (!be->decompressed_pages[pgnr]) { 933267f2492SGao Xiang err = -EFSCORRUPTED; 934267f2492SGao Xiang cur += len; 935267f2492SGao Xiang continue; 936267f2492SGao Xiang } 937267f2492SGao Xiang src = kmap_local_page(be->decompressed_pages[pgnr]); 938267f2492SGao Xiang memcpy(dst + cur, src + scur, len); 939267f2492SGao Xiang kunmap_local(src); 940267f2492SGao Xiang cur += len; 941267f2492SGao Xiang } 942267f2492SGao Xiang kunmap_local(dst); 943267f2492SGao Xiang if (err) 944267f2492SGao Xiang z_erofs_page_mark_eio(bvi->bvec.page); 945267f2492SGao Xiang z_erofs_onlinepage_endio(bvi->bvec.page); 946267f2492SGao Xiang list_del(p); 947267f2492SGao Xiang kfree(bvi); 948267f2492SGao Xiang } 949267f2492SGao Xiang } 950267f2492SGao Xiang 951267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be) 95242fec235SGao Xiang { 9534f05687fSGao Xiang struct z_erofs_pcluster *pcl = be->pcl; 95406a304cdSGao Xiang struct z_erofs_bvec_iter biter; 95506a304cdSGao Xiang struct page *old_bvpage; 956267f2492SGao Xiang int i; 95742fec235SGao Xiang 958387bab87SGao Xiang z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0); 95942fec235SGao Xiang for (i = 0; i < pcl->vcnt; ++i) { 96006a304cdSGao Xiang struct z_erofs_bvec bvec; 96142fec235SGao Xiang 96206a304cdSGao Xiang z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage); 96342fec235SGao Xiang 96406a304cdSGao Xiang if (old_bvpage) 9654f05687fSGao Xiang z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 96642fec235SGao Xiang 96706a304cdSGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(bvec.page)); 968267f2492SGao Xiang z_erofs_do_decompressed_bvec(be, &bvec); 96942fec235SGao Xiang } 97006a304cdSGao Xiang 97106a304cdSGao Xiang old_bvpage = z_erofs_bvec_iter_end(&biter); 97206a304cdSGao Xiang if (old_bvpage) 9734f05687fSGao Xiang z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 97442fec235SGao Xiang } 97542fec235SGao Xiang 9764f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be, 9774f05687fSGao Xiang bool *overlapped) 97867139e36SGao Xiang { 9794f05687fSGao Xiang struct z_erofs_pcluster *pcl = be->pcl; 98067139e36SGao Xiang unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 98167139e36SGao Xiang int i, err = 0; 98267139e36SGao Xiang 98367139e36SGao Xiang *overlapped = false; 98467139e36SGao Xiang for (i = 0; i < pclusterpages; ++i) { 985ed722fbcSGao Xiang struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i]; 986ed722fbcSGao Xiang struct page *page = bvec->page; 98767139e36SGao Xiang 98867139e36SGao Xiang /* compressed pages ought to be present before decompressing */ 98967139e36SGao Xiang if (!page) { 99067139e36SGao Xiang DBG_BUGON(1); 99167139e36SGao Xiang continue; 99267139e36SGao Xiang } 993fe3e5914SGao Xiang be->compressed_pages[i] = page; 99467139e36SGao Xiang 99567139e36SGao Xiang if (z_erofs_is_inline_pcluster(pcl)) { 99667139e36SGao Xiang if (!PageUptodate(page)) 99767139e36SGao Xiang err = -EIO; 99867139e36SGao Xiang continue; 99967139e36SGao Xiang } 100067139e36SGao Xiang 100167139e36SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 100267139e36SGao Xiang if (!z_erofs_is_shortlived_page(page)) { 10034f05687fSGao Xiang if (erofs_page_is_managed(EROFS_SB(be->sb), page)) { 100467139e36SGao Xiang if (!PageUptodate(page)) 100567139e36SGao Xiang err = -EIO; 100667139e36SGao Xiang continue; 100767139e36SGao Xiang } 1008267f2492SGao Xiang z_erofs_do_decompressed_bvec(be, bvec); 100967139e36SGao Xiang *overlapped = true; 101067139e36SGao Xiang } 101167139e36SGao Xiang } 101267139e36SGao Xiang 1013fe3e5914SGao Xiang if (err) 10144f05687fSGao Xiang return err; 10154f05687fSGao Xiang return 0; 101667139e36SGao Xiang } 101767139e36SGao Xiang 10184f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be, 10194f05687fSGao Xiang int err) 102047e4937aSGao Xiang { 10214f05687fSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(be->sb); 10224f05687fSGao Xiang struct z_erofs_pcluster *pcl = be->pcl; 1023cecf864dSYue Hu unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 10242bfab9c0SGao Xiang unsigned int i, inputsize; 102567148551SGao Xiang int err2; 10262bfab9c0SGao Xiang struct page *page; 10272bfab9c0SGao Xiang bool overlapped; 102847e4937aSGao Xiang 102987ca34a7SGao Xiang mutex_lock(&pcl->lock); 10302bfab9c0SGao Xiang be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT; 103147e4937aSGao Xiang 1032fe3e5914SGao Xiang /* allocate (de)compressed page arrays if cannot be kept on stack */ 1033fe3e5914SGao Xiang be->decompressed_pages = NULL; 1034fe3e5914SGao Xiang be->compressed_pages = NULL; 1035fe3e5914SGao Xiang be->onstack_used = 0; 10362bfab9c0SGao Xiang if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) { 10374f05687fSGao Xiang be->decompressed_pages = be->onstack_pages; 10382bfab9c0SGao Xiang be->onstack_used = be->nr_pages; 10394f05687fSGao Xiang memset(be->decompressed_pages, 0, 10402bfab9c0SGao Xiang sizeof(struct page *) * be->nr_pages); 1041fe3e5914SGao Xiang } 1042fe3e5914SGao Xiang 1043fe3e5914SGao Xiang if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES) 1044fe3e5914SGao Xiang be->compressed_pages = be->onstack_pages + be->onstack_used; 1045fe3e5914SGao Xiang 1046fe3e5914SGao Xiang if (!be->decompressed_pages) 10474f05687fSGao Xiang be->decompressed_pages = 10482bfab9c0SGao Xiang kvcalloc(be->nr_pages, sizeof(struct page *), 1049e7368187SGao Xiang GFP_KERNEL | __GFP_NOFAIL); 1050fe3e5914SGao Xiang if (!be->compressed_pages) 1051fe3e5914SGao Xiang be->compressed_pages = 1052fe3e5914SGao Xiang kvcalloc(pclusterpages, sizeof(struct page *), 1053fe3e5914SGao Xiang GFP_KERNEL | __GFP_NOFAIL); 105447e4937aSGao Xiang 1055267f2492SGao Xiang z_erofs_parse_out_bvecs(be); 10564f05687fSGao Xiang err2 = z_erofs_parse_in_bvecs(be, &overlapped); 10574f05687fSGao Xiang if (err2) 10584f05687fSGao Xiang err = err2; 10598d8a09b0SGao Xiang if (err) 106047e4937aSGao Xiang goto out; 106147e4937aSGao Xiang 1062cecf864dSYue Hu if (z_erofs_is_inline_pcluster(pcl)) 1063cecf864dSYue Hu inputsize = pcl->tailpacking_size; 1064cecf864dSYue Hu else 1065cecf864dSYue Hu inputsize = pclusterpages * PAGE_SIZE; 1066cecf864dSYue Hu 106747e4937aSGao Xiang err = z_erofs_decompress(&(struct z_erofs_decompress_req) { 10684f05687fSGao Xiang .sb = be->sb, 10694f05687fSGao Xiang .in = be->compressed_pages, 10704f05687fSGao Xiang .out = be->decompressed_pages, 1071cecf864dSYue Hu .pageofs_in = pcl->pageofs_in, 107287ca34a7SGao Xiang .pageofs_out = pcl->pageofs_out, 10739f6cc76eSGao Xiang .inputsize = inputsize, 10742bfab9c0SGao Xiang .outputsize = pcl->length, 107547e4937aSGao Xiang .alg = pcl->algorithmformat, 107647e4937aSGao Xiang .inplace_io = overlapped, 10772bfab9c0SGao Xiang .partial_decoding = pcl->partial, 1078267f2492SGao Xiang .fillgaps = pcl->multibases, 10794f05687fSGao Xiang }, be->pagepool); 108047e4937aSGao Xiang 108147e4937aSGao Xiang out: 1082cecf864dSYue Hu /* must handle all compressed pages before actual file pages */ 1083cecf864dSYue Hu if (z_erofs_is_inline_pcluster(pcl)) { 1084ed722fbcSGao Xiang page = pcl->compressed_bvecs[0].page; 1085ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL); 1086cecf864dSYue Hu put_page(page); 1087cecf864dSYue Hu } else { 1088cecf864dSYue Hu for (i = 0; i < pclusterpages; ++i) { 1089ed722fbcSGao Xiang page = pcl->compressed_bvecs[i].page; 109047e4937aSGao Xiang 109147e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) 109247e4937aSGao Xiang continue; 109347e4937aSGao Xiang 10946aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 10954f05687fSGao Xiang (void)z_erofs_put_shortlivedpage(be->pagepool, page); 1096ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 109747e4937aSGao Xiang } 1098cecf864dSYue Hu } 1099fe3e5914SGao Xiang if (be->compressed_pages < be->onstack_pages || 1100fe3e5914SGao Xiang be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES) 1101fe3e5914SGao Xiang kvfree(be->compressed_pages); 1102267f2492SGao Xiang z_erofs_fill_other_copies(be, err); 110347e4937aSGao Xiang 11042bfab9c0SGao Xiang for (i = 0; i < be->nr_pages; ++i) { 11054f05687fSGao Xiang page = be->decompressed_pages[i]; 110647e4937aSGao Xiang if (!page) 110747e4937aSGao Xiang continue; 110847e4937aSGao Xiang 11096aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 111047e4937aSGao Xiang 11116aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 11124f05687fSGao Xiang if (z_erofs_put_shortlivedpage(be->pagepool, page)) 111347e4937aSGao Xiang continue; 111467148551SGao Xiang if (err) 111567148551SGao Xiang z_erofs_page_mark_eio(page); 111647e4937aSGao Xiang z_erofs_onlinepage_endio(page); 111747e4937aSGao Xiang } 111847e4937aSGao Xiang 11194f05687fSGao Xiang if (be->decompressed_pages != be->onstack_pages) 11204f05687fSGao Xiang kvfree(be->decompressed_pages); 112147e4937aSGao Xiang 11222bfab9c0SGao Xiang pcl->length = 0; 11232bfab9c0SGao Xiang pcl->partial = true; 1124267f2492SGao Xiang pcl->multibases = false; 112506a304cdSGao Xiang pcl->bvset.nextpage = NULL; 112687ca34a7SGao Xiang pcl->vcnt = 0; 112747e4937aSGao Xiang 112887ca34a7SGao Xiang /* pcluster lock MUST be taken before the following line */ 112947e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 113087ca34a7SGao Xiang mutex_unlock(&pcl->lock); 113147e4937aSGao Xiang return err; 113247e4937aSGao Xiang } 113347e4937aSGao Xiang 11340c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 1135eaa9172aSGao Xiang struct page **pagepool) 113647e4937aSGao Xiang { 11374f05687fSGao Xiang struct z_erofs_decompress_backend be = { 11384f05687fSGao Xiang .sb = io->sb, 11394f05687fSGao Xiang .pagepool = pagepool, 1140267f2492SGao Xiang .decompressed_secondary_bvecs = 1141267f2492SGao Xiang LIST_HEAD_INIT(be.decompressed_secondary_bvecs), 11424f05687fSGao Xiang }; 114347e4937aSGao Xiang z_erofs_next_pcluster_t owned = io->head; 114447e4937aSGao Xiang 114547e4937aSGao Xiang while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { 11464f05687fSGao Xiang /* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ 114747e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); 11484f05687fSGao Xiang /* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */ 114947e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 115047e4937aSGao Xiang 11514f05687fSGao Xiang be.pcl = container_of(owned, struct z_erofs_pcluster, next); 11524f05687fSGao Xiang owned = READ_ONCE(be.pcl->next); 115347e4937aSGao Xiang 11544f05687fSGao Xiang z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0); 11554f05687fSGao Xiang erofs_workgroup_put(&be.pcl->obj); 115647e4937aSGao Xiang } 115747e4937aSGao Xiang } 115847e4937aSGao Xiang 11590c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work) 116047e4937aSGao Xiang { 1161a4b1fab1SGao Xiang struct z_erofs_decompressqueue *bgq = 1162a4b1fab1SGao Xiang container_of(work, struct z_erofs_decompressqueue, u.work); 1163eaa9172aSGao Xiang struct page *pagepool = NULL; 116447e4937aSGao Xiang 1165a4b1fab1SGao Xiang DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 11660c638f70SGao Xiang z_erofs_decompress_queue(bgq, &pagepool); 116747e4937aSGao Xiang 1168eaa9172aSGao Xiang erofs_release_pages(&pagepool); 1169a4b1fab1SGao Xiang kvfree(bgq); 117047e4937aSGao Xiang } 117147e4937aSGao Xiang 11727865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 11737865827cSGao Xiang bool sync, int bios) 11747865827cSGao Xiang { 11757865827cSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(io->sb); 11767865827cSGao Xiang 11777865827cSGao Xiang /* wake up the caller thread for sync decompression */ 11787865827cSGao Xiang if (sync) { 11797865827cSGao Xiang if (!atomic_add_return(bios, &io->pending_bios)) 118060b30050SHongyu Jin complete(&io->u.done); 11817865827cSGao Xiang return; 11827865827cSGao Xiang } 11837865827cSGao Xiang 11847865827cSGao Xiang if (atomic_add_return(bios, &io->pending_bios)) 11857865827cSGao Xiang return; 11867865827cSGao Xiang /* Use workqueue and sync decompression for atomic contexts only */ 11877865827cSGao Xiang if (in_atomic() || irqs_disabled()) { 11887865827cSGao Xiang queue_work(z_erofs_workqueue, &io->u.work); 11897865827cSGao Xiang /* enable sync decompression for readahead */ 11907865827cSGao Xiang if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) 11917865827cSGao Xiang sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON; 11927865827cSGao Xiang return; 11937865827cSGao Xiang } 11947865827cSGao Xiang z_erofs_decompressqueue_work(&io->u.work); 11957865827cSGao Xiang } 11967865827cSGao Xiang 119747e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 119847e4937aSGao Xiang unsigned int nr, 1199eaa9172aSGao Xiang struct page **pagepool, 12009f2731d6SGao Xiang struct address_space *mc) 120147e4937aSGao Xiang { 120247e4937aSGao Xiang const pgoff_t index = pcl->obj.index; 12039f2731d6SGao Xiang gfp_t gfp = mapping_gfp_mask(mc); 120447e4937aSGao Xiang bool tocache = false; 120547e4937aSGao Xiang 120647e4937aSGao Xiang struct address_space *mapping; 120747e4937aSGao Xiang struct page *oldpage, *page; 120847e4937aSGao Xiang 120947e4937aSGao Xiang compressed_page_t t; 121047e4937aSGao Xiang int justfound; 121147e4937aSGao Xiang 121247e4937aSGao Xiang repeat: 1213ed722fbcSGao Xiang page = READ_ONCE(pcl->compressed_bvecs[nr].page); 121447e4937aSGao Xiang oldpage = page; 121547e4937aSGao Xiang 121647e4937aSGao Xiang if (!page) 121747e4937aSGao Xiang goto out_allocpage; 121847e4937aSGao Xiang 121947e4937aSGao Xiang /* process the target tagged pointer */ 122047e4937aSGao Xiang t = tagptr_init(compressed_page_t, page); 122147e4937aSGao Xiang justfound = tagptr_unfold_tags(t); 122247e4937aSGao Xiang page = tagptr_unfold_ptr(t); 122347e4937aSGao Xiang 12241825c8d7SGao Xiang /* 12251825c8d7SGao Xiang * preallocated cached pages, which is used to avoid direct reclaim 12261825c8d7SGao Xiang * otherwise, it will go inplace I/O path instead. 12271825c8d7SGao Xiang */ 12281825c8d7SGao Xiang if (page->private == Z_EROFS_PREALLOCATED_PAGE) { 1229ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 12301825c8d7SGao Xiang set_page_private(page, 0); 12311825c8d7SGao Xiang tocache = true; 12321825c8d7SGao Xiang goto out_tocache; 12331825c8d7SGao Xiang } 123447e4937aSGao Xiang mapping = READ_ONCE(page->mapping); 123547e4937aSGao Xiang 123647e4937aSGao Xiang /* 12376aaa7b06SGao Xiang * file-backed online pages in plcuster are all locked steady, 123847e4937aSGao Xiang * therefore it is impossible for `mapping' to be NULL. 123947e4937aSGao Xiang */ 124047e4937aSGao Xiang if (mapping && mapping != mc) 124147e4937aSGao Xiang /* ought to be unmanaged pages */ 124247e4937aSGao Xiang goto out; 124347e4937aSGao Xiang 12446aaa7b06SGao Xiang /* directly return for shortlived page as well */ 12456aaa7b06SGao Xiang if (z_erofs_is_shortlived_page(page)) 12466aaa7b06SGao Xiang goto out; 12476aaa7b06SGao Xiang 124847e4937aSGao Xiang lock_page(page); 124947e4937aSGao Xiang 125047e4937aSGao Xiang /* only true if page reclaim goes wrong, should never happen */ 125147e4937aSGao Xiang DBG_BUGON(justfound && PagePrivate(page)); 125247e4937aSGao Xiang 125347e4937aSGao Xiang /* the page is still in manage cache */ 125447e4937aSGao Xiang if (page->mapping == mc) { 1255ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 125647e4937aSGao Xiang 125747e4937aSGao Xiang if (!PagePrivate(page)) { 125847e4937aSGao Xiang /* 125947e4937aSGao Xiang * impossible to be !PagePrivate(page) for 126047e4937aSGao Xiang * the current restriction as well if 1261ed722fbcSGao Xiang * the page is already in compressed_bvecs[]. 126247e4937aSGao Xiang */ 126347e4937aSGao Xiang DBG_BUGON(!justfound); 126447e4937aSGao Xiang 126547e4937aSGao Xiang justfound = 0; 126647e4937aSGao Xiang set_page_private(page, (unsigned long)pcl); 126747e4937aSGao Xiang SetPagePrivate(page); 126847e4937aSGao Xiang } 126947e4937aSGao Xiang 127047e4937aSGao Xiang /* no need to submit io if it is already up-to-date */ 127147e4937aSGao Xiang if (PageUptodate(page)) { 127247e4937aSGao Xiang unlock_page(page); 127347e4937aSGao Xiang page = NULL; 127447e4937aSGao Xiang } 127547e4937aSGao Xiang goto out; 127647e4937aSGao Xiang } 127747e4937aSGao Xiang 127847e4937aSGao Xiang /* 127947e4937aSGao Xiang * the managed page has been truncated, it's unsafe to 128047e4937aSGao Xiang * reuse this one, let's allocate a new cache-managed page. 128147e4937aSGao Xiang */ 128247e4937aSGao Xiang DBG_BUGON(page->mapping); 128347e4937aSGao Xiang DBG_BUGON(!justfound); 128447e4937aSGao Xiang 128547e4937aSGao Xiang tocache = true; 128647e4937aSGao Xiang unlock_page(page); 128747e4937aSGao Xiang put_page(page); 128847e4937aSGao Xiang out_allocpage: 12895ddcee1fSGao Xiang page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 1290ed722fbcSGao Xiang if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page, 1291ed722fbcSGao Xiang oldpage, page)) { 1292eaa9172aSGao Xiang erofs_pagepool_add(pagepool, page); 12935ddcee1fSGao Xiang cond_resched(); 12945ddcee1fSGao Xiang goto repeat; 12955ddcee1fSGao Xiang } 12961825c8d7SGao Xiang out_tocache: 1297bf225074SGao Xiang if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1298bf225074SGao Xiang /* turn into temporary page if fails (1 ref) */ 1299bf225074SGao Xiang set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); 1300bf225074SGao Xiang goto out; 1301a30573b3SGao Xiang } 1302bf225074SGao Xiang attach_page_private(page, pcl); 1303bf225074SGao Xiang /* drop a refcount added by allocpage (then we have 2 refs here) */ 1304bf225074SGao Xiang put_page(page); 1305bf225074SGao Xiang 130647e4937aSGao Xiang out: /* the only exit (for tracing and debugging) */ 130747e4937aSGao Xiang return page; 130847e4937aSGao Xiang } 130947e4937aSGao Xiang 1310a4b1fab1SGao Xiang static struct z_erofs_decompressqueue * 1311a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb, 1312a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 131347e4937aSGao Xiang { 1314a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q; 131547e4937aSGao Xiang 1316a4b1fab1SGao Xiang if (fg && !*fg) { 1317a4b1fab1SGao Xiang q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1318a4b1fab1SGao Xiang if (!q) { 1319a4b1fab1SGao Xiang *fg = true; 1320a4b1fab1SGao Xiang goto fg_out; 132147e4937aSGao Xiang } 13220c638f70SGao Xiang INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1323a4b1fab1SGao Xiang } else { 1324a4b1fab1SGao Xiang fg_out: 1325a4b1fab1SGao Xiang q = fgq; 132660b30050SHongyu Jin init_completion(&fgq->u.done); 1327a4b1fab1SGao Xiang atomic_set(&fgq->pending_bios, 0); 132867148551SGao Xiang q->eio = false; 1329a4b1fab1SGao Xiang } 1330a4b1fab1SGao Xiang q->sb = sb; 1331a4b1fab1SGao Xiang q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1332a4b1fab1SGao Xiang return q; 133347e4937aSGao Xiang } 133447e4937aSGao Xiang 133547e4937aSGao Xiang /* define decompression jobqueue types */ 133647e4937aSGao Xiang enum { 133747e4937aSGao Xiang JQ_BYPASS, 133847e4937aSGao Xiang JQ_SUBMIT, 133947e4937aSGao Xiang NR_JOBQUEUES, 134047e4937aSGao Xiang }; 134147e4937aSGao Xiang 134247e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb, 1343a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[], 1344a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 134547e4937aSGao Xiang { 134647e4937aSGao Xiang /* 134747e4937aSGao Xiang * if managed cache is enabled, bypass jobqueue is needed, 134847e4937aSGao Xiang * no need to read from device for all pclusters in this queue. 134947e4937aSGao Xiang */ 1350a4b1fab1SGao Xiang q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1351a4b1fab1SGao Xiang q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg); 135247e4937aSGao Xiang 1353a4b1fab1SGao Xiang return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg)); 135447e4937aSGao Xiang } 135547e4937aSGao Xiang 135647e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 135747e4937aSGao Xiang z_erofs_next_pcluster_t qtail[], 135847e4937aSGao Xiang z_erofs_next_pcluster_t owned_head) 135947e4937aSGao Xiang { 136047e4937aSGao Xiang z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 136147e4937aSGao Xiang z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 136247e4937aSGao Xiang 136347e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 136447e4937aSGao Xiang if (owned_head == Z_EROFS_PCLUSTER_TAIL) 136547e4937aSGao Xiang owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 136647e4937aSGao Xiang 136747e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); 136847e4937aSGao Xiang 136947e4937aSGao Xiang WRITE_ONCE(*submit_qtail, owned_head); 137047e4937aSGao Xiang WRITE_ONCE(*bypass_qtail, &pcl->next); 137147e4937aSGao Xiang 137247e4937aSGao Xiang qtail[JQ_BYPASS] = &pcl->next; 137347e4937aSGao Xiang } 137447e4937aSGao Xiang 13757865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio) 13767865827cSGao Xiang { 13777865827cSGao Xiang tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private); 13787865827cSGao Xiang struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t); 13797865827cSGao Xiang blk_status_t err = bio->bi_status; 13807865827cSGao Xiang struct bio_vec *bvec; 13817865827cSGao Xiang struct bvec_iter_all iter_all; 13827865827cSGao Xiang 13837865827cSGao Xiang bio_for_each_segment_all(bvec, bio, iter_all) { 13847865827cSGao Xiang struct page *page = bvec->bv_page; 13857865827cSGao Xiang 13867865827cSGao Xiang DBG_BUGON(PageUptodate(page)); 13877865827cSGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 13887865827cSGao Xiang 13897865827cSGao Xiang if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 13907865827cSGao Xiang if (!err) 13917865827cSGao Xiang SetPageUptodate(page); 13927865827cSGao Xiang unlock_page(page); 13937865827cSGao Xiang } 13947865827cSGao Xiang } 139567148551SGao Xiang if (err) 139667148551SGao Xiang q->eio = true; 13977865827cSGao Xiang z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1); 13987865827cSGao Xiang bio_put(bio); 13997865827cSGao Xiang } 14007865827cSGao Xiang 140183a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f, 1402eaa9172aSGao Xiang struct page **pagepool, 1403a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, 1404a4b1fab1SGao Xiang bool *force_fg) 140547e4937aSGao Xiang { 140683a386c0SGao Xiang struct super_block *sb = f->inode->i_sb; 140783a386c0SGao Xiang struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb)); 140847e4937aSGao Xiang z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1409a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 141047e4937aSGao Xiang void *bi_private; 14115c6dcc57SGao Xiang z_erofs_next_pcluster_t owned_head = f->owned_head; 1412dfeab2e9SGao Xiang /* bio is NULL initially, so no need to initialize last_{index,bdev} */ 14133f649ab7SKees Cook pgoff_t last_index; 1414dfeab2e9SGao Xiang struct block_device *last_bdev; 14151e4a2955SGao Xiang unsigned int nr_bios = 0; 14161e4a2955SGao Xiang struct bio *bio = NULL; 141747e4937aSGao Xiang 1418a4b1fab1SGao Xiang bi_private = jobqueueset_init(sb, q, fgq, force_fg); 1419a4b1fab1SGao Xiang qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1420a4b1fab1SGao Xiang qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 142147e4937aSGao Xiang 142247e4937aSGao Xiang /* by default, all need io submission */ 142347e4937aSGao Xiang q[JQ_SUBMIT]->head = owned_head; 142447e4937aSGao Xiang 142547e4937aSGao Xiang do { 1426dfeab2e9SGao Xiang struct erofs_map_dev mdev; 142747e4937aSGao Xiang struct z_erofs_pcluster *pcl; 14281e4a2955SGao Xiang pgoff_t cur, end; 14291e4a2955SGao Xiang unsigned int i = 0; 14301e4a2955SGao Xiang bool bypass = true; 143147e4937aSGao Xiang 143247e4937aSGao Xiang /* no possible 'owned_head' equals the following */ 143347e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 143447e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 143547e4937aSGao Xiang 143647e4937aSGao Xiang pcl = container_of(owned_head, struct z_erofs_pcluster, next); 143747e4937aSGao Xiang 1438cecf864dSYue Hu /* close the main owned chain at first */ 1439cecf864dSYue Hu owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 1440cecf864dSYue Hu Z_EROFS_PCLUSTER_TAIL_CLOSED); 1441cecf864dSYue Hu if (z_erofs_is_inline_pcluster(pcl)) { 1442cecf864dSYue Hu move_to_bypass_jobqueue(pcl, qtail, owned_head); 1443cecf864dSYue Hu continue; 1444cecf864dSYue Hu } 1445cecf864dSYue Hu 1446dfeab2e9SGao Xiang /* no device id here, thus it will always succeed */ 1447dfeab2e9SGao Xiang mdev = (struct erofs_map_dev) { 1448dfeab2e9SGao Xiang .m_pa = blknr_to_addr(pcl->obj.index), 1449dfeab2e9SGao Xiang }; 1450dfeab2e9SGao Xiang (void)erofs_map_dev(sb, &mdev); 1451dfeab2e9SGao Xiang 1452dfeab2e9SGao Xiang cur = erofs_blknr(mdev.m_pa); 14539f6cc76eSGao Xiang end = cur + pcl->pclusterpages; 145447e4937aSGao Xiang 14551e4a2955SGao Xiang do { 14561e4a2955SGao Xiang struct page *page; 145747e4937aSGao Xiang 14581e4a2955SGao Xiang page = pickup_page_for_submission(pcl, i++, pagepool, 145983a386c0SGao Xiang mc); 14601e4a2955SGao Xiang if (!page) 14611e4a2955SGao Xiang continue; 146247e4937aSGao Xiang 1463dfeab2e9SGao Xiang if (bio && (cur != last_index + 1 || 1464dfeab2e9SGao Xiang last_bdev != mdev.m_bdev)) { 146547e4937aSGao Xiang submit_bio_retry: 146694e4e153SGao Xiang submit_bio(bio); 146747e4937aSGao Xiang bio = NULL; 146847e4937aSGao Xiang } 146947e4937aSGao Xiang 147047e4937aSGao Xiang if (!bio) { 147107888c66SChristoph Hellwig bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS, 147207888c66SChristoph Hellwig REQ_OP_READ, GFP_NOIO); 14730c638f70SGao Xiang bio->bi_end_io = z_erofs_decompressqueue_endio; 1474dfeab2e9SGao Xiang 1475dfeab2e9SGao Xiang last_bdev = mdev.m_bdev; 14761e4a2955SGao Xiang bio->bi_iter.bi_sector = (sector_t)cur << 1477a5c0b780SGao Xiang LOG_SECTORS_PER_BLOCK; 1478a5c0b780SGao Xiang bio->bi_private = bi_private; 14796ea5aad3SGao Xiang if (f->readahead) 14806ea5aad3SGao Xiang bio->bi_opf |= REQ_RAHEAD; 148147e4937aSGao Xiang ++nr_bios; 148247e4937aSGao Xiang } 148347e4937aSGao Xiang 14846c3e485eSGao Xiang if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 148547e4937aSGao Xiang goto submit_bio_retry; 148647e4937aSGao Xiang 14871e4a2955SGao Xiang last_index = cur; 14881e4a2955SGao Xiang bypass = false; 14891e4a2955SGao Xiang } while (++cur < end); 149047e4937aSGao Xiang 14911e4a2955SGao Xiang if (!bypass) 149247e4937aSGao Xiang qtail[JQ_SUBMIT] = &pcl->next; 149347e4937aSGao Xiang else 149447e4937aSGao Xiang move_to_bypass_jobqueue(pcl, qtail, owned_head); 149547e4937aSGao Xiang } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 149647e4937aSGao Xiang 149747e4937aSGao Xiang if (bio) 149894e4e153SGao Xiang submit_bio(bio); 149947e4937aSGao Xiang 1500587a67b7SGao Xiang /* 1501587a67b7SGao Xiang * although background is preferred, no one is pending for submission. 1502587a67b7SGao Xiang * don't issue workqueue for decompression but drop it directly instead. 1503587a67b7SGao Xiang */ 1504587a67b7SGao Xiang if (!*force_fg && !nr_bios) { 1505587a67b7SGao Xiang kvfree(q[JQ_SUBMIT]); 15061e4a2955SGao Xiang return; 1507587a67b7SGao Xiang } 1508a4b1fab1SGao Xiang z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios); 150947e4937aSGao Xiang } 151047e4937aSGao Xiang 151183a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f, 1512eaa9172aSGao Xiang struct page **pagepool, bool force_fg) 151347e4937aSGao Xiang { 1514a4b1fab1SGao Xiang struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 151547e4937aSGao Xiang 15165c6dcc57SGao Xiang if (f->owned_head == Z_EROFS_PCLUSTER_TAIL) 151747e4937aSGao Xiang return; 151883a386c0SGao Xiang z_erofs_submit_queue(f, pagepool, io, &force_fg); 151947e4937aSGao Xiang 15200c638f70SGao Xiang /* handle bypass queue (no i/o pclusters) immediately */ 15210c638f70SGao Xiang z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool); 152247e4937aSGao Xiang 152347e4937aSGao Xiang if (!force_fg) 152447e4937aSGao Xiang return; 152547e4937aSGao Xiang 152647e4937aSGao Xiang /* wait until all bios are completed */ 152760b30050SHongyu Jin wait_for_completion_io(&io[JQ_SUBMIT].u.done); 152847e4937aSGao Xiang 15290c638f70SGao Xiang /* handle synchronous decompress queue in the caller context */ 15300c638f70SGao Xiang z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool); 153147e4937aSGao Xiang } 153247e4937aSGao Xiang 153338629291SGao Xiang /* 153438629291SGao Xiang * Since partial uptodate is still unimplemented for now, we have to use 153538629291SGao Xiang * approximate readmore strategies as a start. 153638629291SGao Xiang */ 153738629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f, 153838629291SGao Xiang struct readahead_control *rac, 153938629291SGao Xiang erofs_off_t end, 1540eaa9172aSGao Xiang struct page **pagepool, 154138629291SGao Xiang bool backmost) 154238629291SGao Xiang { 154338629291SGao Xiang struct inode *inode = f->inode; 154438629291SGao Xiang struct erofs_map_blocks *map = &f->map; 154538629291SGao Xiang erofs_off_t cur; 154638629291SGao Xiang int err; 154738629291SGao Xiang 154838629291SGao Xiang if (backmost) { 154938629291SGao Xiang map->m_la = end; 1550622ceaddSGao Xiang err = z_erofs_map_blocks_iter(inode, map, 1551622ceaddSGao Xiang EROFS_GET_BLOCKS_READMORE); 155238629291SGao Xiang if (err) 155338629291SGao Xiang return; 155438629291SGao Xiang 155538629291SGao Xiang /* expend ra for the trailing edge if readahead */ 155638629291SGao Xiang if (rac) { 155738629291SGao Xiang loff_t newstart = readahead_pos(rac); 155838629291SGao Xiang 155938629291SGao Xiang cur = round_up(map->m_la + map->m_llen, PAGE_SIZE); 156038629291SGao Xiang readahead_expand(rac, newstart, cur - newstart); 156138629291SGao Xiang return; 156238629291SGao Xiang } 156338629291SGao Xiang end = round_up(end, PAGE_SIZE); 156438629291SGao Xiang } else { 156538629291SGao Xiang end = round_up(map->m_la, PAGE_SIZE); 156638629291SGao Xiang 156738629291SGao Xiang if (!map->m_llen) 156838629291SGao Xiang return; 156938629291SGao Xiang } 157038629291SGao Xiang 157138629291SGao Xiang cur = map->m_la + map->m_llen - 1; 157238629291SGao Xiang while (cur >= end) { 157338629291SGao Xiang pgoff_t index = cur >> PAGE_SHIFT; 157438629291SGao Xiang struct page *page; 157538629291SGao Xiang 157638629291SGao Xiang page = erofs_grab_cache_page_nowait(inode->i_mapping, index); 1577aa793b46SGao Xiang if (page) { 157838629291SGao Xiang if (PageUptodate(page)) { 157938629291SGao Xiang unlock_page(page); 1580aa793b46SGao Xiang } else { 158138629291SGao Xiang err = z_erofs_do_read_page(f, page, pagepool); 158238629291SGao Xiang if (err) 158338629291SGao Xiang erofs_err(inode->i_sb, 158438629291SGao Xiang "readmore error at page %lu @ nid %llu", 158538629291SGao Xiang index, EROFS_I(inode)->nid); 1586aa793b46SGao Xiang } 158738629291SGao Xiang put_page(page); 1588aa793b46SGao Xiang } 1589aa793b46SGao Xiang 159038629291SGao Xiang if (cur < PAGE_SIZE) 159138629291SGao Xiang break; 159238629291SGao Xiang cur = (index << PAGE_SHIFT) - 1; 159338629291SGao Xiang } 159438629291SGao Xiang } 159538629291SGao Xiang 1596a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio) 159747e4937aSGao Xiang { 1598a2e20a25SMatthew Wilcox (Oracle) struct page *page = &folio->page; 159947e4937aSGao Xiang struct inode *const inode = page->mapping->host; 160040452ffcSHuang Jianan struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 160147e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1602eaa9172aSGao Xiang struct page *pagepool = NULL; 160347e4937aSGao Xiang int err; 160447e4937aSGao Xiang 160547e4937aSGao Xiang trace_erofs_readpage(page, false); 160647e4937aSGao Xiang f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 160747e4937aSGao Xiang 160838629291SGao Xiang z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1, 160938629291SGao Xiang &pagepool, true); 16101825c8d7SGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 161138629291SGao Xiang z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false); 161238629291SGao Xiang 16135c6dcc57SGao Xiang (void)z_erofs_collector_end(&f); 161447e4937aSGao Xiang 161547e4937aSGao Xiang /* if some compressed cluster ready, need submit them anyway */ 161683a386c0SGao Xiang z_erofs_runqueue(&f, &pagepool, 161740452ffcSHuang Jianan z_erofs_get_sync_decompress_policy(sbi, 0)); 161847e4937aSGao Xiang 161947e4937aSGao Xiang if (err) 16204f761fa2SGao Xiang erofs_err(inode->i_sb, "failed to read, err [%d]", err); 162147e4937aSGao Xiang 162209c54379SGao Xiang erofs_put_metabuf(&f.map.buf); 1623eaa9172aSGao Xiang erofs_release_pages(&pagepool); 162447e4937aSGao Xiang return err; 162547e4937aSGao Xiang } 162647e4937aSGao Xiang 16270615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac) 162847e4937aSGao Xiang { 16290615090cSMatthew Wilcox (Oracle) struct inode *const inode = rac->mapping->host; 163047e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 163147e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1632eaa9172aSGao Xiang struct page *pagepool = NULL, *head = NULL, *page; 163338629291SGao Xiang unsigned int nr_pages; 163447e4937aSGao Xiang 16356ea5aad3SGao Xiang f.readahead = true; 16360615090cSMatthew Wilcox (Oracle) f.headoffset = readahead_pos(rac); 163747e4937aSGao Xiang 163838629291SGao Xiang z_erofs_pcluster_readmore(&f, rac, f.headoffset + 163938629291SGao Xiang readahead_length(rac) - 1, &pagepool, true); 164038629291SGao Xiang nr_pages = readahead_count(rac); 164138629291SGao Xiang trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); 164238629291SGao Xiang 16430615090cSMatthew Wilcox (Oracle) while ((page = readahead_page(rac))) { 164447e4937aSGao Xiang set_page_private(page, (unsigned long)head); 164547e4937aSGao Xiang head = page; 164647e4937aSGao Xiang } 164747e4937aSGao Xiang 164847e4937aSGao Xiang while (head) { 164947e4937aSGao Xiang struct page *page = head; 165047e4937aSGao Xiang int err; 165147e4937aSGao Xiang 165247e4937aSGao Xiang /* traversal in reverse order */ 165347e4937aSGao Xiang head = (void *)page_private(page); 165447e4937aSGao Xiang 16551825c8d7SGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 1656a5876e24SGao Xiang if (err) 16574f761fa2SGao Xiang erofs_err(inode->i_sb, 16584f761fa2SGao Xiang "readahead error at page %lu @ nid %llu", 16594f761fa2SGao Xiang page->index, EROFS_I(inode)->nid); 166047e4937aSGao Xiang put_page(page); 166147e4937aSGao Xiang } 166238629291SGao Xiang z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false); 16635c6dcc57SGao Xiang (void)z_erofs_collector_end(&f); 166447e4937aSGao Xiang 166583a386c0SGao Xiang z_erofs_runqueue(&f, &pagepool, 166640452ffcSHuang Jianan z_erofs_get_sync_decompress_policy(sbi, nr_pages)); 166709c54379SGao Xiang erofs_put_metabuf(&f.map.buf); 1668eaa9172aSGao Xiang erofs_release_pages(&pagepool); 166947e4937aSGao Xiang } 167047e4937aSGao Xiang 16710c638f70SGao Xiang const struct address_space_operations z_erofs_aops = { 1672a2e20a25SMatthew Wilcox (Oracle) .read_folio = z_erofs_read_folio, 16730615090cSMatthew Wilcox (Oracle) .readahead = z_erofs_readahead, 167447e4937aSGao Xiang }; 1675