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> 1099486c51SChristoph Hellwig #include <linux/psi.h> 1147e4937aSGao Xiang 1247e4937aSGao Xiang #include <trace/events/erofs.h> 1347e4937aSGao Xiang 1447e4937aSGao Xiang /* 159f6cc76eSGao Xiang * since pclustersize is variable for big pcluster feature, introduce slab 169f6cc76eSGao Xiang * pools implementation for different pcluster sizes. 179f6cc76eSGao Xiang */ 189f6cc76eSGao Xiang struct z_erofs_pcluster_slab { 199f6cc76eSGao Xiang struct kmem_cache *slab; 209f6cc76eSGao Xiang unsigned int maxpages; 219f6cc76eSGao Xiang char name[48]; 229f6cc76eSGao Xiang }; 239f6cc76eSGao Xiang 249f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n } 259f6cc76eSGao Xiang 269f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = { 279f6cc76eSGao Xiang _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128), 289f6cc76eSGao Xiang _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES) 299f6cc76eSGao Xiang }; 309f6cc76eSGao Xiang 3106a304cdSGao Xiang struct z_erofs_bvec_iter { 3206a304cdSGao Xiang struct page *bvpage; 3306a304cdSGao Xiang struct z_erofs_bvset *bvset; 3406a304cdSGao Xiang unsigned int nr, cur; 3506a304cdSGao Xiang }; 3606a304cdSGao Xiang 3706a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter) 3806a304cdSGao Xiang { 3906a304cdSGao Xiang if (iter->bvpage) 4006a304cdSGao Xiang kunmap_local(iter->bvset); 4106a304cdSGao Xiang return iter->bvpage; 4206a304cdSGao Xiang } 4306a304cdSGao Xiang 4406a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter) 4506a304cdSGao Xiang { 4606a304cdSGao Xiang unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec; 4706a304cdSGao Xiang /* have to access nextpage in advance, otherwise it will be unmapped */ 4806a304cdSGao Xiang struct page *nextpage = iter->bvset->nextpage; 4906a304cdSGao Xiang struct page *oldpage; 5006a304cdSGao Xiang 5106a304cdSGao Xiang DBG_BUGON(!nextpage); 5206a304cdSGao Xiang oldpage = z_erofs_bvec_iter_end(iter); 5306a304cdSGao Xiang iter->bvpage = nextpage; 5406a304cdSGao Xiang iter->bvset = kmap_local_page(nextpage); 5506a304cdSGao Xiang iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec); 5606a304cdSGao Xiang iter->cur = 0; 5706a304cdSGao Xiang return oldpage; 5806a304cdSGao Xiang } 5906a304cdSGao Xiang 6006a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter, 6106a304cdSGao Xiang struct z_erofs_bvset_inline *bvset, 6206a304cdSGao Xiang unsigned int bootstrap_nr, 6306a304cdSGao Xiang unsigned int cur) 6406a304cdSGao Xiang { 6506a304cdSGao Xiang *iter = (struct z_erofs_bvec_iter) { 6606a304cdSGao Xiang .nr = bootstrap_nr, 6706a304cdSGao Xiang .bvset = (struct z_erofs_bvset *)bvset, 6806a304cdSGao Xiang }; 6906a304cdSGao Xiang 7006a304cdSGao Xiang while (cur > iter->nr) { 7106a304cdSGao Xiang cur -= iter->nr; 7206a304cdSGao Xiang z_erofs_bvset_flip(iter); 7306a304cdSGao Xiang } 7406a304cdSGao Xiang iter->cur = cur; 7506a304cdSGao Xiang } 7606a304cdSGao Xiang 7706a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter, 7806a304cdSGao Xiang struct z_erofs_bvec *bvec, 7906a304cdSGao Xiang struct page **candidate_bvpage) 8006a304cdSGao Xiang { 8106a304cdSGao Xiang if (iter->cur == iter->nr) { 8206a304cdSGao Xiang if (!*candidate_bvpage) 8306a304cdSGao Xiang return -EAGAIN; 8406a304cdSGao Xiang 8506a304cdSGao Xiang DBG_BUGON(iter->bvset->nextpage); 8606a304cdSGao Xiang iter->bvset->nextpage = *candidate_bvpage; 8706a304cdSGao Xiang z_erofs_bvset_flip(iter); 8806a304cdSGao Xiang 8906a304cdSGao Xiang iter->bvset->nextpage = NULL; 9006a304cdSGao Xiang *candidate_bvpage = NULL; 9106a304cdSGao Xiang } 9206a304cdSGao Xiang iter->bvset->bvec[iter->cur++] = *bvec; 9306a304cdSGao Xiang return 0; 9406a304cdSGao Xiang } 9506a304cdSGao Xiang 9606a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter, 9706a304cdSGao Xiang struct z_erofs_bvec *bvec, 9806a304cdSGao Xiang struct page **old_bvpage) 9906a304cdSGao Xiang { 10006a304cdSGao Xiang if (iter->cur == iter->nr) 10106a304cdSGao Xiang *old_bvpage = z_erofs_bvset_flip(iter); 10206a304cdSGao Xiang else 10306a304cdSGao Xiang *old_bvpage = NULL; 10406a304cdSGao Xiang *bvec = iter->bvset->bvec[iter->cur++]; 10506a304cdSGao Xiang } 10606a304cdSGao Xiang 1079f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void) 1089f6cc76eSGao Xiang { 1099f6cc76eSGao Xiang int i; 1109f6cc76eSGao Xiang 1119f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 1129f6cc76eSGao Xiang if (!pcluster_pool[i].slab) 1139f6cc76eSGao Xiang continue; 1149f6cc76eSGao Xiang kmem_cache_destroy(pcluster_pool[i].slab); 1159f6cc76eSGao Xiang pcluster_pool[i].slab = NULL; 1169f6cc76eSGao Xiang } 1179f6cc76eSGao Xiang } 1189f6cc76eSGao Xiang 1199f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void) 1209f6cc76eSGao Xiang { 1219f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs; 1229f6cc76eSGao Xiang struct z_erofs_pcluster *a; 1239f6cc76eSGao Xiang unsigned int size; 1249f6cc76eSGao Xiang 1259f6cc76eSGao Xiang for (pcs = pcluster_pool; 1269f6cc76eSGao Xiang pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) { 127ed722fbcSGao Xiang size = struct_size(a, compressed_bvecs, pcs->maxpages); 1289f6cc76eSGao Xiang 1299f6cc76eSGao Xiang sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages); 1309f6cc76eSGao Xiang pcs->slab = kmem_cache_create(pcs->name, size, 0, 1319f6cc76eSGao Xiang SLAB_RECLAIM_ACCOUNT, NULL); 1329f6cc76eSGao Xiang if (pcs->slab) 1339f6cc76eSGao Xiang continue; 1349f6cc76eSGao Xiang 1359f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 1369f6cc76eSGao Xiang return -ENOMEM; 1379f6cc76eSGao Xiang } 1389f6cc76eSGao Xiang return 0; 1399f6cc76eSGao Xiang } 1409f6cc76eSGao Xiang 1419f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages) 1429f6cc76eSGao Xiang { 1439f6cc76eSGao Xiang int i; 1449f6cc76eSGao Xiang 1459f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 1469f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 1479f6cc76eSGao Xiang struct z_erofs_pcluster *pcl; 1489f6cc76eSGao Xiang 1499f6cc76eSGao Xiang if (nrpages > pcs->maxpages) 1509f6cc76eSGao Xiang continue; 1519f6cc76eSGao Xiang 1529f6cc76eSGao Xiang pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS); 1539f6cc76eSGao Xiang if (!pcl) 1549f6cc76eSGao Xiang return ERR_PTR(-ENOMEM); 1559f6cc76eSGao Xiang pcl->pclusterpages = nrpages; 1569f6cc76eSGao Xiang return pcl; 1579f6cc76eSGao Xiang } 1589f6cc76eSGao Xiang return ERR_PTR(-EINVAL); 1599f6cc76eSGao Xiang } 1609f6cc76eSGao Xiang 1619f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl) 1629f6cc76eSGao Xiang { 163cecf864dSYue Hu unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 1649f6cc76eSGao Xiang int i; 1659f6cc76eSGao Xiang 1669f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 1679f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 1689f6cc76eSGao Xiang 169cecf864dSYue Hu if (pclusterpages > pcs->maxpages) 1709f6cc76eSGao Xiang continue; 1719f6cc76eSGao Xiang 1729f6cc76eSGao Xiang kmem_cache_free(pcs->slab, pcl); 1739f6cc76eSGao Xiang return; 1749f6cc76eSGao Xiang } 1759f6cc76eSGao Xiang DBG_BUGON(1); 1769f6cc76eSGao Xiang } 1779f6cc76eSGao Xiang 17847e4937aSGao Xiang /* 17947e4937aSGao Xiang * tagged pointer with 1-bit tag for all compressed pages 18047e4937aSGao Xiang * tag 0 - the page is just found with an extra page reference 18147e4937aSGao Xiang */ 18247e4937aSGao Xiang typedef tagptr1_t compressed_page_t; 18347e4937aSGao Xiang 18447e4937aSGao Xiang #define tag_compressed_page_justfound(page) \ 18547e4937aSGao Xiang tagptr_fold(compressed_page_t, page, 1) 18647e4937aSGao Xiang 18747e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly; 18847e4937aSGao Xiang 18947e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void) 19047e4937aSGao Xiang { 19147e4937aSGao Xiang destroy_workqueue(z_erofs_workqueue); 1929f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 19347e4937aSGao Xiang } 19447e4937aSGao Xiang 19599634bf3SGao Xiang static inline int z_erofs_init_workqueue(void) 19647e4937aSGao Xiang { 19747e4937aSGao Xiang const unsigned int onlinecpus = num_possible_cpus(); 19847e4937aSGao Xiang 19947e4937aSGao Xiang /* 20047e4937aSGao Xiang * no need to spawn too many threads, limiting threads could minimum 20147e4937aSGao Xiang * scheduling overhead, perhaps per-CPU threads should be better? 20247e4937aSGao Xiang */ 2030e62ea33SGao Xiang z_erofs_workqueue = alloc_workqueue("erofs_unzipd", 2040e62ea33SGao Xiang WQ_UNBOUND | WQ_HIGHPRI, 20547e4937aSGao Xiang onlinecpus + onlinecpus / 4); 20647e4937aSGao Xiang return z_erofs_workqueue ? 0 : -ENOMEM; 20747e4937aSGao Xiang } 20847e4937aSGao Xiang 20947e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void) 21047e4937aSGao Xiang { 2119f6cc76eSGao Xiang int err = z_erofs_create_pcluster_pool(); 21247e4937aSGao Xiang 2139f6cc76eSGao Xiang if (err) 2149f6cc76eSGao Xiang return err; 2159f6cc76eSGao Xiang err = z_erofs_init_workqueue(); 2169f6cc76eSGao Xiang if (err) 2179f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 2189f6cc76eSGao Xiang return err; 21947e4937aSGao Xiang } 22047e4937aSGao Xiang 221db166fc2SGao Xiang enum z_erofs_pclustermode { 222db166fc2SGao Xiang Z_EROFS_PCLUSTER_INFLIGHT, 22347e4937aSGao Xiang /* 224db166fc2SGao Xiang * The current pclusters was the tail of an exist chain, in addition 225db166fc2SGao Xiang * that the previous processed chained pclusters are all decided to 22647e4937aSGao Xiang * be hooked up to it. 227db166fc2SGao Xiang * A new chain will be created for the remaining pclusters which are 228db166fc2SGao Xiang * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED, 229db166fc2SGao Xiang * the next pcluster cannot reuse the whole page safely for inplace I/O 230db166fc2SGao Xiang * in the following scenario: 23147e4937aSGao Xiang * ________________________________________________________________ 23247e4937aSGao Xiang * | tail (partial) page | head (partial) page | 233db166fc2SGao Xiang * | (belongs to the next pcl) | (belongs to the current pcl) | 234db166fc2SGao Xiang * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________| 23547e4937aSGao Xiang */ 236db166fc2SGao Xiang Z_EROFS_PCLUSTER_HOOKED, 2370b964600SGao Xiang /* 238db166fc2SGao Xiang * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it 2390b964600SGao Xiang * could be dispatched into bypass queue later due to uptodated managed 2400b964600SGao Xiang * pages. All related online pages cannot be reused for inplace I/O (or 241387bab87SGao Xiang * bvpage) since it can be directly decoded without I/O submission. 2420b964600SGao Xiang */ 243db166fc2SGao Xiang Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE, 24447e4937aSGao Xiang /* 24547e4937aSGao Xiang * The current collection has been linked with the owned chain, and 24647e4937aSGao Xiang * could also be linked with the remaining collections, which means 24747e4937aSGao Xiang * if the processing page is the tail page of the collection, thus 24847e4937aSGao Xiang * the current collection can safely use the whole page (since 24947e4937aSGao Xiang * the previous collection is under control) for in-place I/O, as 25047e4937aSGao Xiang * illustrated below: 25147e4937aSGao Xiang * ________________________________________________________________ 25247e4937aSGao Xiang * | tail (partial) page | head (partial) page | 25347e4937aSGao Xiang * | (of the current cl) | (of the previous collection) | 254db166fc2SGao Xiang * | PCLUSTER_FOLLOWED or | | 255db166fc2SGao Xiang * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________| 25647e4937aSGao Xiang * 25747e4937aSGao Xiang * [ (*) the above page can be used as inplace I/O. ] 25847e4937aSGao Xiang */ 259db166fc2SGao Xiang Z_EROFS_PCLUSTER_FOLLOWED, 26047e4937aSGao Xiang }; 26147e4937aSGao Xiang 2625c6dcc57SGao Xiang struct z_erofs_decompress_frontend { 2635c6dcc57SGao Xiang struct inode *const inode; 2645c6dcc57SGao Xiang struct erofs_map_blocks map; 26506a304cdSGao Xiang struct z_erofs_bvec_iter biter; 26647e4937aSGao Xiang 26706a304cdSGao Xiang struct page *candidate_bvpage; 26847e4937aSGao Xiang struct z_erofs_pcluster *pcl, *tailpcl; 26947e4937aSGao Xiang z_erofs_next_pcluster_t owned_head; 270db166fc2SGao Xiang enum z_erofs_pclustermode mode; 27147e4937aSGao Xiang 2726ea5aad3SGao Xiang bool readahead; 27347e4937aSGao Xiang /* used for applying cache strategy on the fly */ 27447e4937aSGao Xiang bool backmost; 27547e4937aSGao Xiang erofs_off_t headoffset; 276ed722fbcSGao Xiang 277ed722fbcSGao Xiang /* a pointer used to pick up inplace I/O pages */ 278ed722fbcSGao Xiang unsigned int icur; 27947e4937aSGao Xiang }; 28047e4937aSGao Xiang 28147e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \ 2825c6dcc57SGao Xiang .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 283db166fc2SGao Xiang .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true } 28447e4937aSGao Xiang 285*1282dea3SGao Xiang static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe) 286*1282dea3SGao Xiang { 287*1282dea3SGao Xiang unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy; 288*1282dea3SGao Xiang 289*1282dea3SGao Xiang if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 290*1282dea3SGao Xiang return false; 291*1282dea3SGao Xiang 292*1282dea3SGao Xiang if (fe->backmost) 293*1282dea3SGao Xiang return true; 294*1282dea3SGao Xiang 295*1282dea3SGao Xiang if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 296*1282dea3SGao Xiang fe->map.m_la < fe->headoffset) 297*1282dea3SGao Xiang return true; 298*1282dea3SGao Xiang 299*1282dea3SGao Xiang return false; 300*1282dea3SGao Xiang } 301*1282dea3SGao Xiang 3026f39d1e1SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe, 303eaa9172aSGao Xiang struct page **pagepool) 30447e4937aSGao Xiang { 3056f39d1e1SGao Xiang struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode)); 3065c6dcc57SGao Xiang struct z_erofs_pcluster *pcl = fe->pcl; 307*1282dea3SGao Xiang bool shouldalloc = z_erofs_should_alloc_cache(fe); 30847e4937aSGao Xiang bool standalone = true; 3096f39d1e1SGao Xiang /* 3106f39d1e1SGao Xiang * optimistic allocation without direct reclaim since inplace I/O 3116f39d1e1SGao Xiang * can be used if low memory otherwise. 3126f39d1e1SGao Xiang */ 3131825c8d7SGao Xiang gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 3141825c8d7SGao Xiang __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 315ed722fbcSGao Xiang unsigned int i; 31647e4937aSGao Xiang 317db166fc2SGao Xiang if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED) 31847e4937aSGao Xiang return; 31947e4937aSGao Xiang 320ed722fbcSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 32147e4937aSGao Xiang struct page *page; 32247e4937aSGao Xiang compressed_page_t t; 3231825c8d7SGao Xiang struct page *newpage = NULL; 32447e4937aSGao Xiang 32547e4937aSGao Xiang /* the compressed page was loaded before */ 326ed722fbcSGao Xiang if (READ_ONCE(pcl->compressed_bvecs[i].page)) 32747e4937aSGao Xiang continue; 32847e4937aSGao Xiang 329ed722fbcSGao Xiang page = find_get_page(mc, pcl->obj.index + i); 33047e4937aSGao Xiang 33147e4937aSGao Xiang if (page) { 33247e4937aSGao Xiang t = tag_compressed_page_justfound(page); 3330b964600SGao Xiang } else { 3340b964600SGao Xiang /* I/O is needed, no possible to decompress directly */ 3350b964600SGao Xiang standalone = false; 336*1282dea3SGao Xiang if (!shouldalloc) 337*1282dea3SGao Xiang continue; 338*1282dea3SGao Xiang 339*1282dea3SGao Xiang /* 340*1282dea3SGao Xiang * try to use cached I/O if page allocation 341*1282dea3SGao Xiang * succeeds or fallback to in-place I/O instead 342*1282dea3SGao Xiang * to avoid any direct reclaim. 343*1282dea3SGao Xiang */ 3441825c8d7SGao Xiang newpage = erofs_allocpage(pagepool, gfp); 3451825c8d7SGao Xiang if (!newpage) 34647e4937aSGao Xiang continue; 347*1282dea3SGao Xiang set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE); 3480b964600SGao Xiang t = tag_compressed_page_justfound(newpage); 34947e4937aSGao Xiang } 35047e4937aSGao Xiang 351ed722fbcSGao Xiang if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, 352ed722fbcSGao Xiang tagptr_cast_ptr(t))) 35347e4937aSGao Xiang continue; 35447e4937aSGao Xiang 355eaa9172aSGao Xiang if (page) 35647e4937aSGao Xiang put_page(page); 357eaa9172aSGao Xiang else if (newpage) 358eaa9172aSGao Xiang erofs_pagepool_add(pagepool, newpage); 35947e4937aSGao Xiang } 36047e4937aSGao Xiang 3610b964600SGao Xiang /* 3620b964600SGao Xiang * don't do inplace I/O if all compressed pages are available in 3630b964600SGao Xiang * managed cache since it can be moved to the bypass queue instead. 3640b964600SGao Xiang */ 3650b964600SGao Xiang if (standalone) 366db166fc2SGao Xiang fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 36747e4937aSGao Xiang } 36847e4937aSGao Xiang 36947e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */ 37047e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 37147e4937aSGao Xiang struct erofs_workgroup *grp) 37247e4937aSGao Xiang { 37347e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 37447e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 37547e4937aSGao Xiang int i; 37647e4937aSGao Xiang 377cecf864dSYue Hu DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 37847e4937aSGao Xiang /* 37947e4937aSGao Xiang * refcount of workgroup is now freezed as 1, 38047e4937aSGao Xiang * therefore no need to worry about available decompression users. 38147e4937aSGao Xiang */ 3829f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 383ed722fbcSGao Xiang struct page *page = pcl->compressed_bvecs[i].page; 38447e4937aSGao Xiang 38547e4937aSGao Xiang if (!page) 38647e4937aSGao Xiang continue; 38747e4937aSGao Xiang 38847e4937aSGao Xiang /* block other users from reclaiming or migrating the page */ 38947e4937aSGao Xiang if (!trylock_page(page)) 39047e4937aSGao Xiang return -EBUSY; 39147e4937aSGao Xiang 392f4d4e5fcSYue Hu if (!erofs_page_is_managed(sbi, page)) 39347e4937aSGao Xiang continue; 39447e4937aSGao Xiang 39547e4937aSGao Xiang /* barrier is implied in the following 'unlock_page' */ 396ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 3976aaa7b06SGao Xiang detach_page_private(page); 39847e4937aSGao Xiang unlock_page(page); 39947e4937aSGao Xiang } 40047e4937aSGao Xiang return 0; 40147e4937aSGao Xiang } 40247e4937aSGao Xiang 403d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page) 40447e4937aSGao Xiang { 40547e4937aSGao Xiang struct z_erofs_pcluster *const pcl = (void *)page_private(page); 406ed722fbcSGao Xiang int ret, i; 40747e4937aSGao Xiang 408ed722fbcSGao Xiang if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1)) 409ed722fbcSGao Xiang return 0; 41047e4937aSGao Xiang 411ed722fbcSGao Xiang ret = 0; 412cecf864dSYue Hu DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 4139f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 414ed722fbcSGao Xiang if (pcl->compressed_bvecs[i].page == page) { 415ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 41647e4937aSGao Xiang ret = 1; 41747e4937aSGao Xiang break; 41847e4937aSGao Xiang } 41947e4937aSGao Xiang } 42047e4937aSGao Xiang erofs_workgroup_unfreeze(&pcl->obj, 1); 4216aaa7b06SGao Xiang if (ret) 4226aaa7b06SGao Xiang detach_page_private(page); 42347e4937aSGao Xiang return ret; 42447e4937aSGao Xiang } 42547e4937aSGao Xiang 4265c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe, 427ed722fbcSGao Xiang struct z_erofs_bvec *bvec) 42847e4937aSGao Xiang { 4295c6dcc57SGao Xiang struct z_erofs_pcluster *const pcl = fe->pcl; 43047e4937aSGao Xiang 431ed722fbcSGao Xiang while (fe->icur > 0) { 432ed722fbcSGao Xiang if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page, 433ed722fbcSGao Xiang NULL, bvec->page)) { 434ed722fbcSGao Xiang pcl->compressed_bvecs[fe->icur] = *bvec; 43547e4937aSGao Xiang return true; 436ed722fbcSGao Xiang } 437ed722fbcSGao Xiang } 43847e4937aSGao Xiang return false; 43947e4937aSGao Xiang } 44047e4937aSGao Xiang 44187ca34a7SGao Xiang /* callers must be with pcluster lock held */ 4425c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe, 4435b220b20SGao Xiang struct z_erofs_bvec *bvec, bool exclusive) 44447e4937aSGao Xiang { 44547e4937aSGao Xiang int ret; 44647e4937aSGao Xiang 447db166fc2SGao Xiang if (exclusive) { 44806a304cdSGao Xiang /* give priority for inplaceio to use file pages first */ 449ed722fbcSGao Xiang if (z_erofs_try_inplace_io(fe, bvec)) 45047e4937aSGao Xiang return 0; 45106a304cdSGao Xiang /* otherwise, check if it can be used as a bvpage */ 452db166fc2SGao Xiang if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED && 45306a304cdSGao Xiang !fe->candidate_bvpage) 45406a304cdSGao Xiang fe->candidate_bvpage = bvec->page; 45506a304cdSGao Xiang } 45606a304cdSGao Xiang ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage); 45706a304cdSGao Xiang fe->pcl->vcnt += (ret >= 0); 45806a304cdSGao Xiang return ret; 45947e4937aSGao Xiang } 46047e4937aSGao Xiang 4615c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f) 46247e4937aSGao Xiang { 4635c6dcc57SGao Xiang struct z_erofs_pcluster *pcl = f->pcl; 4645c6dcc57SGao Xiang z_erofs_next_pcluster_t *owned_head = &f->owned_head; 46547e4937aSGao Xiang 466473e15b0SGao Xiang /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ 467473e15b0SGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 468473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_NIL) { 46947e4937aSGao Xiang *owned_head = &pcl->next; 470473e15b0SGao Xiang /* so we can attach this pcluster to our submission chain. */ 471db166fc2SGao Xiang f->mode = Z_EROFS_PCLUSTER_FOLLOWED; 472473e15b0SGao Xiang return; 473473e15b0SGao Xiang } 474473e15b0SGao Xiang 47547e4937aSGao Xiang /* 476473e15b0SGao Xiang * type 2, link to the end of an existing open chain, be careful 477473e15b0SGao Xiang * that its submission is controlled by the original attached chain. 47847e4937aSGao Xiang */ 479267f2492SGao Xiang if (*owned_head != &pcl->next && pcl != f->tailpcl && 480267f2492SGao Xiang cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 481473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_TAIL) { 48247e4937aSGao Xiang *owned_head = Z_EROFS_PCLUSTER_TAIL; 483db166fc2SGao Xiang f->mode = Z_EROFS_PCLUSTER_HOOKED; 4845c6dcc57SGao Xiang f->tailpcl = NULL; 485473e15b0SGao Xiang return; 48647e4937aSGao Xiang } 487473e15b0SGao Xiang /* type 3, it belongs to a chain, but it isn't the end of the chain */ 488db166fc2SGao Xiang f->mode = Z_EROFS_PCLUSTER_INFLIGHT; 48947e4937aSGao Xiang } 49047e4937aSGao Xiang 49183a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe) 49247e4937aSGao Xiang { 49383a386c0SGao Xiang struct erofs_map_blocks *map = &fe->map; 494cecf864dSYue Hu bool ztailpacking = map->m_flags & EROFS_MAP_META; 49547e4937aSGao Xiang struct z_erofs_pcluster *pcl; 49664094a04SGao Xiang struct erofs_workgroup *grp; 49747e4937aSGao Xiang int err; 49847e4937aSGao Xiang 4998f899262SGao Xiang if (!(map->m_flags & EROFS_MAP_ENCODED)) { 5008f899262SGao Xiang DBG_BUGON(1); 5018f899262SGao Xiang return -EFSCORRUPTED; 5028f899262SGao Xiang } 5038f899262SGao Xiang 5049f6cc76eSGao Xiang /* no available pcluster, let's allocate one */ 505cecf864dSYue Hu pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 : 506cecf864dSYue Hu map->m_plen >> PAGE_SHIFT); 5079f6cc76eSGao Xiang if (IS_ERR(pcl)) 5089f6cc76eSGao Xiang return PTR_ERR(pcl); 50947e4937aSGao Xiang 51064094a04SGao Xiang atomic_set(&pcl->obj.refcount, 1); 5118f899262SGao Xiang pcl->algorithmformat = map->m_algorithmformat; 5122bfab9c0SGao Xiang pcl->length = 0; 5132bfab9c0SGao Xiang pcl->partial = true; 51447e4937aSGao Xiang 51547e4937aSGao Xiang /* new pclusters should be claimed as type 1, primary and followed */ 5165c6dcc57SGao Xiang pcl->next = fe->owned_head; 51787ca34a7SGao Xiang pcl->pageofs_out = map->m_la & ~PAGE_MASK; 518db166fc2SGao Xiang fe->mode = Z_EROFS_PCLUSTER_FOLLOWED; 51947e4937aSGao Xiang 52047e4937aSGao Xiang /* 52147e4937aSGao Xiang * lock all primary followed works before visible to others 52247e4937aSGao Xiang * and mutex_trylock *never* fails for a new pcluster. 52347e4937aSGao Xiang */ 52487ca34a7SGao Xiang mutex_init(&pcl->lock); 52587ca34a7SGao Xiang DBG_BUGON(!mutex_trylock(&pcl->lock)); 52647e4937aSGao Xiang 527cecf864dSYue Hu if (ztailpacking) { 528cecf864dSYue Hu pcl->obj.index = 0; /* which indicates ztailpacking */ 529cecf864dSYue Hu pcl->pageofs_in = erofs_blkoff(map->m_pa); 530cecf864dSYue Hu pcl->tailpacking_size = map->m_plen; 531cecf864dSYue Hu } else { 532cecf864dSYue Hu pcl->obj.index = map->m_pa >> PAGE_SHIFT; 533cecf864dSYue Hu 53483a386c0SGao Xiang grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj); 53564094a04SGao Xiang if (IS_ERR(grp)) { 53664094a04SGao Xiang err = PTR_ERR(grp); 53764094a04SGao Xiang goto err_out; 53864094a04SGao Xiang } 53964094a04SGao Xiang 54064094a04SGao Xiang if (grp != &pcl->obj) { 5415c6dcc57SGao Xiang fe->pcl = container_of(grp, 542cecf864dSYue Hu struct z_erofs_pcluster, obj); 54364094a04SGao Xiang err = -EEXIST; 54464094a04SGao Xiang goto err_out; 54547e4937aSGao Xiang } 546cecf864dSYue Hu } 54747e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 5485c6dcc57SGao Xiang if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) 5495c6dcc57SGao Xiang fe->tailpcl = pcl; 5505c6dcc57SGao Xiang fe->owned_head = &pcl->next; 5515c6dcc57SGao Xiang fe->pcl = pcl; 5529e579fc1SGao Xiang return 0; 55364094a04SGao Xiang 55464094a04SGao Xiang err_out: 55587ca34a7SGao Xiang mutex_unlock(&pcl->lock); 5569f6cc76eSGao Xiang z_erofs_free_pcluster(pcl); 55764094a04SGao Xiang return err; 55847e4937aSGao Xiang } 55947e4937aSGao Xiang 56083a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe) 56147e4937aSGao Xiang { 56283a386c0SGao Xiang struct erofs_map_blocks *map = &fe->map; 5630d823b42SGao Xiang struct erofs_workgroup *grp = NULL; 5649e579fc1SGao Xiang int ret; 56547e4937aSGao Xiang 56687ca34a7SGao Xiang DBG_BUGON(fe->pcl); 56747e4937aSGao Xiang 56887ca34a7SGao Xiang /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */ 5695c6dcc57SGao Xiang DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL); 5705c6dcc57SGao Xiang DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 57147e4937aSGao Xiang 5720d823b42SGao Xiang if (!(map->m_flags & EROFS_MAP_META)) { 5730d823b42SGao Xiang grp = erofs_find_workgroup(fe->inode->i_sb, 5740d823b42SGao Xiang map->m_pa >> PAGE_SHIFT); 5750d823b42SGao Xiang } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) { 57647e4937aSGao Xiang DBG_BUGON(1); 577cecf864dSYue Hu return -EFSCORRUPTED; 578cecf864dSYue Hu } 57947e4937aSGao Xiang 58064094a04SGao Xiang if (grp) { 5815c6dcc57SGao Xiang fe->pcl = container_of(grp, struct z_erofs_pcluster, obj); 5820d823b42SGao Xiang ret = -EEXIST; 58364094a04SGao Xiang } else { 58483a386c0SGao Xiang ret = z_erofs_register_pcluster(fe); 58564094a04SGao Xiang } 58647e4937aSGao Xiang 5870d823b42SGao Xiang if (ret == -EEXIST) { 588267f2492SGao Xiang mutex_lock(&fe->pcl->lock); 589267f2492SGao Xiang /* used to check tail merging loop due to corrupted images */ 590267f2492SGao Xiang if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) 591267f2492SGao Xiang fe->tailpcl = fe->pcl; 592267f2492SGao Xiang 593267f2492SGao Xiang z_erofs_try_to_claim_pcluster(fe); 5940d823b42SGao Xiang } else if (ret) { 5950d823b42SGao Xiang return ret; 5960d823b42SGao Xiang } 59706a304cdSGao Xiang z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset, 598387bab87SGao Xiang Z_EROFS_INLINE_BVECS, fe->pcl->vcnt); 59981382f5fSGao Xiang /* since file-backed online pages are traversed in reverse order */ 600ed722fbcSGao Xiang fe->icur = z_erofs_pclusterpages(fe->pcl); 60147e4937aSGao Xiang return 0; 60247e4937aSGao Xiang } 60347e4937aSGao Xiang 60447e4937aSGao Xiang /* 60547e4937aSGao Xiang * keep in mind that no referenced pclusters will be freed 60647e4937aSGao Xiang * only after a RCU grace period. 60747e4937aSGao Xiang */ 60847e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head) 60947e4937aSGao Xiang { 61087ca34a7SGao Xiang z_erofs_free_pcluster(container_of(head, 61187ca34a7SGao Xiang struct z_erofs_pcluster, rcu)); 61247e4937aSGao Xiang } 61347e4937aSGao Xiang 61447e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 61547e4937aSGao Xiang { 61647e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 61747e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 61847e4937aSGao Xiang 61987ca34a7SGao Xiang call_rcu(&pcl->rcu, z_erofs_rcu_callback); 62047e4937aSGao Xiang } 62147e4937aSGao Xiang 6225c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe) 62347e4937aSGao Xiang { 62487ca34a7SGao Xiang struct z_erofs_pcluster *pcl = fe->pcl; 62547e4937aSGao Xiang 62687ca34a7SGao Xiang if (!pcl) 62747e4937aSGao Xiang return false; 62847e4937aSGao Xiang 62906a304cdSGao Xiang z_erofs_bvec_iter_end(&fe->biter); 63087ca34a7SGao Xiang mutex_unlock(&pcl->lock); 63147e4937aSGao Xiang 63206a304cdSGao Xiang if (fe->candidate_bvpage) { 63306a304cdSGao Xiang DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage)); 63406a304cdSGao Xiang fe->candidate_bvpage = NULL; 63506a304cdSGao Xiang } 63606a304cdSGao Xiang 63747e4937aSGao Xiang /* 63847e4937aSGao Xiang * if all pending pages are added, don't hold its reference 63947e4937aSGao Xiang * any longer if the pcluster isn't hosted by ourselves. 64047e4937aSGao Xiang */ 641db166fc2SGao Xiang if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE) 64287ca34a7SGao Xiang erofs_workgroup_put(&pcl->obj); 64347e4937aSGao Xiang 64487ca34a7SGao Xiang fe->pcl = NULL; 64547e4937aSGao Xiang return true; 64647e4937aSGao Xiang } 64747e4937aSGao Xiang 648b15b2e30SYue Hu static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos, 649b15b2e30SYue Hu struct page *page, unsigned int pageofs, 650b15b2e30SYue Hu unsigned int len) 651b15b2e30SYue Hu { 652b15b2e30SYue Hu struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode; 653b15b2e30SYue Hu struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 654b15b2e30SYue Hu u8 *src, *dst; 655b15b2e30SYue Hu unsigned int i, cnt; 656b15b2e30SYue Hu 657e5126de1SYue Hu if (!packed_inode) 658e5126de1SYue Hu return -EFSCORRUPTED; 659e5126de1SYue Hu 660b15b2e30SYue Hu pos += EROFS_I(inode)->z_fragmentoff; 661b15b2e30SYue Hu for (i = 0; i < len; i += cnt) { 662b15b2e30SYue Hu cnt = min_t(unsigned int, len - i, 663b15b2e30SYue Hu EROFS_BLKSIZ - erofs_blkoff(pos)); 664b15b2e30SYue Hu src = erofs_bread(&buf, packed_inode, 665b15b2e30SYue Hu erofs_blknr(pos), EROFS_KMAP); 666b15b2e30SYue Hu if (IS_ERR(src)) { 667b15b2e30SYue Hu erofs_put_metabuf(&buf); 668b15b2e30SYue Hu return PTR_ERR(src); 669b15b2e30SYue Hu } 670b15b2e30SYue Hu 671b15b2e30SYue Hu dst = kmap_local_page(page); 672b15b2e30SYue Hu memcpy(dst + pageofs + i, src + erofs_blkoff(pos), cnt); 673b15b2e30SYue Hu kunmap_local(dst); 674b15b2e30SYue Hu pos += cnt; 675b15b2e30SYue Hu } 676b15b2e30SYue Hu erofs_put_metabuf(&buf); 677b15b2e30SYue Hu return 0; 678b15b2e30SYue Hu } 679b15b2e30SYue Hu 68047e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 681eaa9172aSGao Xiang struct page *page, struct page **pagepool) 68247e4937aSGao Xiang { 68347e4937aSGao Xiang struct inode *const inode = fe->inode; 68447e4937aSGao Xiang struct erofs_map_blocks *const map = &fe->map; 68547e4937aSGao Xiang const loff_t offset = page_offset(page); 6865b220b20SGao Xiang bool tight = true, exclusive; 6872bfab9c0SGao Xiang unsigned int cur, end, spiltted; 68847e4937aSGao Xiang int err = 0; 68947e4937aSGao Xiang 69047e4937aSGao Xiang /* register locked file pages as online pages in pack */ 69147e4937aSGao Xiang z_erofs_onlinepage_init(page); 69247e4937aSGao Xiang 69347e4937aSGao Xiang spiltted = 0; 69447e4937aSGao Xiang end = PAGE_SIZE; 69547e4937aSGao Xiang repeat: 69647e4937aSGao Xiang cur = end - 1; 69747e4937aSGao Xiang 69839397a46SGao Xiang if (offset + cur < map->m_la || 69939397a46SGao Xiang offset + cur >= map->m_la + map->m_llen) { 70039397a46SGao Xiang erofs_dbg("out-of-range map @ pos %llu", offset + cur); 70147e4937aSGao Xiang 7025c6dcc57SGao Xiang if (z_erofs_collector_end(fe)) 70347e4937aSGao Xiang fe->backmost = false; 70447e4937aSGao Xiang map->m_la = offset + cur; 70547e4937aSGao Xiang map->m_llen = 0; 70647e4937aSGao Xiang err = z_erofs_map_blocks_iter(inode, map, 0); 7078d8a09b0SGao Xiang if (err) 70867148551SGao Xiang goto out; 70939397a46SGao Xiang } else { 71039397a46SGao Xiang if (fe->pcl) 71139397a46SGao Xiang goto hitted; 71239397a46SGao Xiang /* didn't get a valid pcluster previously (very rare) */ 71339397a46SGao Xiang } 71447e4937aSGao Xiang 715b15b2e30SYue Hu if (!(map->m_flags & EROFS_MAP_MAPPED) || 716b15b2e30SYue Hu map->m_flags & EROFS_MAP_FRAGMENT) 71747e4937aSGao Xiang goto hitted; 71847e4937aSGao Xiang 71983a386c0SGao Xiang err = z_erofs_collector_begin(fe); 7208d8a09b0SGao Xiang if (err) 72167148551SGao Xiang goto out; 72247e4937aSGao Xiang 7235c6dcc57SGao Xiang if (z_erofs_is_inline_pcluster(fe->pcl)) { 72409c54379SGao Xiang void *mp; 725cecf864dSYue Hu 72609c54379SGao Xiang mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb, 72709c54379SGao Xiang erofs_blknr(map->m_pa), EROFS_NO_KMAP); 72809c54379SGao Xiang if (IS_ERR(mp)) { 72909c54379SGao Xiang err = PTR_ERR(mp); 730cecf864dSYue Hu erofs_err(inode->i_sb, 731cecf864dSYue Hu "failed to get inline page, err %d", err); 73267148551SGao Xiang goto out; 733cecf864dSYue Hu } 73409c54379SGao Xiang get_page(fe->map.buf.page); 735ed722fbcSGao Xiang WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, 736ed722fbcSGao Xiang fe->map.buf.page); 737db166fc2SGao Xiang fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 738cecf864dSYue Hu } else { 7396f39d1e1SGao Xiang /* bind cache first when cached decompression is preferred */ 740*1282dea3SGao Xiang z_erofs_bind_cache(fe, pagepool); 741cecf864dSYue Hu } 74247e4937aSGao Xiang hitted: 743dc76ea8cSGao Xiang /* 744dc76ea8cSGao Xiang * Ensure the current partial page belongs to this submit chain rather 745dc76ea8cSGao Xiang * than other concurrent submit chains or the noio(bypass) chain since 746dc76ea8cSGao Xiang * those chains are handled asynchronously thus the page cannot be used 747387bab87SGao Xiang * for inplace I/O or bvpage (should be processed in a strict order.) 748dc76ea8cSGao Xiang */ 749db166fc2SGao Xiang tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED && 750db166fc2SGao Xiang fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE); 751dc76ea8cSGao Xiang 75247e4937aSGao Xiang cur = end - min_t(unsigned int, offset + end - map->m_la, end); 7538d8a09b0SGao Xiang if (!(map->m_flags & EROFS_MAP_MAPPED)) { 75447e4937aSGao Xiang zero_user_segment(page, cur, end); 75547e4937aSGao Xiang goto next_part; 75647e4937aSGao Xiang } 757b15b2e30SYue Hu if (map->m_flags & EROFS_MAP_FRAGMENT) { 758b15b2e30SYue Hu unsigned int pageofs, skip, len; 759b15b2e30SYue Hu 760b15b2e30SYue Hu if (offset > map->m_la) { 761b15b2e30SYue Hu pageofs = 0; 762b15b2e30SYue Hu skip = offset - map->m_la; 763b15b2e30SYue Hu } else { 764b15b2e30SYue Hu pageofs = map->m_la & ~PAGE_MASK; 765b15b2e30SYue Hu skip = 0; 766b15b2e30SYue Hu } 767b15b2e30SYue Hu len = min_t(unsigned int, map->m_llen - skip, end - cur); 768b15b2e30SYue Hu err = z_erofs_read_fragment(inode, skip, page, pageofs, len); 769b15b2e30SYue Hu if (err) 770b15b2e30SYue Hu goto out; 771b15b2e30SYue Hu ++spiltted; 772b15b2e30SYue Hu tight = false; 773b15b2e30SYue Hu goto next_part; 774b15b2e30SYue Hu } 77547e4937aSGao Xiang 7765b220b20SGao Xiang exclusive = (!cur && (!spiltted || tight)); 77747e4937aSGao Xiang if (cur) 778db166fc2SGao Xiang tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED); 77947e4937aSGao Xiang 78047e4937aSGao Xiang retry: 78106a304cdSGao Xiang err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) { 78206a304cdSGao Xiang .page = page, 78306a304cdSGao Xiang .offset = offset - map->m_la, 78406a304cdSGao Xiang .end = end, 7855b220b20SGao Xiang }), exclusive); 78606a304cdSGao Xiang /* should allocate an additional short-lived page for bvset */ 78706a304cdSGao Xiang if (err == -EAGAIN && !fe->candidate_bvpage) { 78806a304cdSGao Xiang fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL); 78906a304cdSGao Xiang set_page_private(fe->candidate_bvpage, 79006a304cdSGao Xiang Z_EROFS_SHORTLIVED_PAGE); 79147e4937aSGao Xiang goto retry; 79247e4937aSGao Xiang } 79347e4937aSGao Xiang 79406a304cdSGao Xiang if (err) { 79506a304cdSGao Xiang DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage); 79667148551SGao Xiang goto out; 79706a304cdSGao Xiang } 79847e4937aSGao Xiang 79967148551SGao Xiang z_erofs_onlinepage_split(page); 80047e4937aSGao Xiang /* bump up the number of spiltted parts of a page */ 80147e4937aSGao Xiang ++spiltted; 802267f2492SGao Xiang if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) 803267f2492SGao Xiang fe->pcl->multibases = true; 8042bfab9c0SGao Xiang if (fe->pcl->length < offset + end - map->m_la) { 8052bfab9c0SGao Xiang fe->pcl->length = offset + end - map->m_la; 8062bfab9c0SGao Xiang fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK; 8072bfab9c0SGao Xiang } 808e7933278SGao Xiang if ((map->m_flags & EROFS_MAP_FULL_MAPPED) && 809e7933278SGao Xiang !(map->m_flags & EROFS_MAP_PARTIAL_REF) && 810e7933278SGao Xiang fe->pcl->length == map->m_llen) 811e7933278SGao Xiang fe->pcl->partial = false; 81247e4937aSGao Xiang next_part: 8132bfab9c0SGao Xiang /* shorten the remaining extent to update progress */ 81447e4937aSGao Xiang map->m_llen = offset + cur - map->m_la; 8152bfab9c0SGao Xiang map->m_flags &= ~EROFS_MAP_FULL_MAPPED; 81647e4937aSGao Xiang 81747e4937aSGao Xiang end = cur; 81847e4937aSGao Xiang if (end > 0) 81947e4937aSGao Xiang goto repeat; 82047e4937aSGao Xiang 82147e4937aSGao Xiang out: 82267148551SGao Xiang if (err) 82367148551SGao Xiang z_erofs_page_mark_eio(page); 82447e4937aSGao Xiang z_erofs_onlinepage_endio(page); 82547e4937aSGao Xiang 8264f761fa2SGao Xiang erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", 82747e4937aSGao Xiang __func__, page, spiltted, map->m_llen); 82847e4937aSGao Xiang return err; 82947e4937aSGao Xiang } 83047e4937aSGao Xiang 83140452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi, 83240452ffcSHuang Jianan unsigned int readahead_pages) 83340452ffcSHuang Jianan { 834a2e20a25SMatthew Wilcox (Oracle) /* auto: enable for read_folio, disable for readahead */ 83540452ffcSHuang Jianan if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) && 83640452ffcSHuang Jianan !readahead_pages) 83740452ffcSHuang Jianan return true; 83840452ffcSHuang Jianan 83940452ffcSHuang Jianan if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) && 84040452ffcSHuang Jianan (readahead_pages <= sbi->opt.max_sync_decompress_pages)) 84140452ffcSHuang Jianan return true; 84240452ffcSHuang Jianan 84340452ffcSHuang Jianan return false; 84440452ffcSHuang Jianan } 84540452ffcSHuang Jianan 8466aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page) 8476aaa7b06SGao Xiang { 8486aaa7b06SGao Xiang return !page->mapping && !z_erofs_is_shortlived_page(page); 8496aaa7b06SGao Xiang } 8506aaa7b06SGao Xiang 8514f05687fSGao Xiang struct z_erofs_decompress_backend { 8524f05687fSGao Xiang struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES]; 8534f05687fSGao Xiang struct super_block *sb; 8544f05687fSGao Xiang struct z_erofs_pcluster *pcl; 8554f05687fSGao Xiang 8564f05687fSGao Xiang /* pages with the longest decompressed length for deduplication */ 8574f05687fSGao Xiang struct page **decompressed_pages; 8584f05687fSGao Xiang /* pages to keep the compressed data */ 8594f05687fSGao Xiang struct page **compressed_pages; 8604f05687fSGao Xiang 861267f2492SGao Xiang struct list_head decompressed_secondary_bvecs; 8624f05687fSGao Xiang struct page **pagepool; 8632bfab9c0SGao Xiang unsigned int onstack_used, nr_pages; 8644f05687fSGao Xiang }; 8654f05687fSGao Xiang 866267f2492SGao Xiang struct z_erofs_bvec_item { 867267f2492SGao Xiang struct z_erofs_bvec bvec; 868267f2492SGao Xiang struct list_head list; 869267f2492SGao Xiang }; 870267f2492SGao Xiang 871267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be, 8723fe96ee0SGao Xiang struct z_erofs_bvec *bvec) 8733fe96ee0SGao Xiang { 874267f2492SGao Xiang struct z_erofs_bvec_item *item; 875267f2492SGao Xiang 876267f2492SGao Xiang if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) { 877267f2492SGao Xiang unsigned int pgnr; 8783fe96ee0SGao Xiang 879267f2492SGao Xiang pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT; 8802bfab9c0SGao Xiang DBG_BUGON(pgnr >= be->nr_pages); 88163bbb856SGao Xiang if (!be->decompressed_pages[pgnr]) { 8823fe96ee0SGao Xiang be->decompressed_pages[pgnr] = bvec->page; 883267f2492SGao Xiang return; 8843fe96ee0SGao Xiang } 88563bbb856SGao Xiang } 8863fe96ee0SGao Xiang 887267f2492SGao Xiang /* (cold path) one pcluster is requested multiple times */ 888267f2492SGao Xiang item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL); 889267f2492SGao Xiang item->bvec = *bvec; 890267f2492SGao Xiang list_add(&item->list, &be->decompressed_secondary_bvecs); 891267f2492SGao Xiang } 892267f2492SGao Xiang 893267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be, 894267f2492SGao Xiang int err) 895267f2492SGao Xiang { 896267f2492SGao Xiang unsigned int off0 = be->pcl->pageofs_out; 897267f2492SGao Xiang struct list_head *p, *n; 898267f2492SGao Xiang 899267f2492SGao Xiang list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) { 900267f2492SGao Xiang struct z_erofs_bvec_item *bvi; 901267f2492SGao Xiang unsigned int end, cur; 902267f2492SGao Xiang void *dst, *src; 903267f2492SGao Xiang 904267f2492SGao Xiang bvi = container_of(p, struct z_erofs_bvec_item, list); 905267f2492SGao Xiang cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0; 906267f2492SGao Xiang end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset, 907267f2492SGao Xiang bvi->bvec.end); 908267f2492SGao Xiang dst = kmap_local_page(bvi->bvec.page); 909267f2492SGao Xiang while (cur < end) { 910267f2492SGao Xiang unsigned int pgnr, scur, len; 911267f2492SGao Xiang 912267f2492SGao Xiang pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT; 913267f2492SGao Xiang DBG_BUGON(pgnr >= be->nr_pages); 914267f2492SGao Xiang 915267f2492SGao Xiang scur = bvi->bvec.offset + cur - 916267f2492SGao Xiang ((pgnr << PAGE_SHIFT) - off0); 917267f2492SGao Xiang len = min_t(unsigned int, end - cur, PAGE_SIZE - scur); 918267f2492SGao Xiang if (!be->decompressed_pages[pgnr]) { 919267f2492SGao Xiang err = -EFSCORRUPTED; 920267f2492SGao Xiang cur += len; 921267f2492SGao Xiang continue; 922267f2492SGao Xiang } 923267f2492SGao Xiang src = kmap_local_page(be->decompressed_pages[pgnr]); 924267f2492SGao Xiang memcpy(dst + cur, src + scur, len); 925267f2492SGao Xiang kunmap_local(src); 926267f2492SGao Xiang cur += len; 927267f2492SGao Xiang } 928267f2492SGao Xiang kunmap_local(dst); 929267f2492SGao Xiang if (err) 930267f2492SGao Xiang z_erofs_page_mark_eio(bvi->bvec.page); 931267f2492SGao Xiang z_erofs_onlinepage_endio(bvi->bvec.page); 932267f2492SGao Xiang list_del(p); 933267f2492SGao Xiang kfree(bvi); 934267f2492SGao Xiang } 935267f2492SGao Xiang } 936267f2492SGao Xiang 937267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be) 93842fec235SGao Xiang { 9394f05687fSGao Xiang struct z_erofs_pcluster *pcl = be->pcl; 94006a304cdSGao Xiang struct z_erofs_bvec_iter biter; 94106a304cdSGao Xiang struct page *old_bvpage; 942267f2492SGao Xiang int i; 94342fec235SGao Xiang 944387bab87SGao Xiang z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0); 94542fec235SGao Xiang for (i = 0; i < pcl->vcnt; ++i) { 94606a304cdSGao Xiang struct z_erofs_bvec bvec; 94742fec235SGao Xiang 94806a304cdSGao Xiang z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage); 94942fec235SGao Xiang 95006a304cdSGao Xiang if (old_bvpage) 9514f05687fSGao Xiang z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 95242fec235SGao Xiang 95306a304cdSGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(bvec.page)); 954267f2492SGao Xiang z_erofs_do_decompressed_bvec(be, &bvec); 95542fec235SGao Xiang } 95606a304cdSGao Xiang 95706a304cdSGao Xiang old_bvpage = z_erofs_bvec_iter_end(&biter); 95806a304cdSGao Xiang if (old_bvpage) 9594f05687fSGao Xiang z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 96042fec235SGao Xiang } 96142fec235SGao Xiang 9624f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be, 9634f05687fSGao Xiang bool *overlapped) 96467139e36SGao Xiang { 9654f05687fSGao Xiang struct z_erofs_pcluster *pcl = be->pcl; 96667139e36SGao Xiang unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 96767139e36SGao Xiang int i, err = 0; 96867139e36SGao Xiang 96967139e36SGao Xiang *overlapped = false; 97067139e36SGao Xiang for (i = 0; i < pclusterpages; ++i) { 971ed722fbcSGao Xiang struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i]; 972ed722fbcSGao Xiang struct page *page = bvec->page; 97367139e36SGao Xiang 97467139e36SGao Xiang /* compressed pages ought to be present before decompressing */ 97567139e36SGao Xiang if (!page) { 97667139e36SGao Xiang DBG_BUGON(1); 97767139e36SGao Xiang continue; 97867139e36SGao Xiang } 979fe3e5914SGao Xiang be->compressed_pages[i] = page; 98067139e36SGao Xiang 98167139e36SGao Xiang if (z_erofs_is_inline_pcluster(pcl)) { 98267139e36SGao Xiang if (!PageUptodate(page)) 98367139e36SGao Xiang err = -EIO; 98467139e36SGao Xiang continue; 98567139e36SGao Xiang } 98667139e36SGao Xiang 98767139e36SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 98867139e36SGao Xiang if (!z_erofs_is_shortlived_page(page)) { 9894f05687fSGao Xiang if (erofs_page_is_managed(EROFS_SB(be->sb), page)) { 99067139e36SGao Xiang if (!PageUptodate(page)) 99167139e36SGao Xiang err = -EIO; 99267139e36SGao Xiang continue; 99367139e36SGao Xiang } 994267f2492SGao Xiang z_erofs_do_decompressed_bvec(be, bvec); 99567139e36SGao Xiang *overlapped = true; 99667139e36SGao Xiang } 99767139e36SGao Xiang } 99867139e36SGao Xiang 999fe3e5914SGao Xiang if (err) 10004f05687fSGao Xiang return err; 10014f05687fSGao Xiang return 0; 100267139e36SGao Xiang } 100367139e36SGao Xiang 10044f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be, 10054f05687fSGao Xiang int err) 100647e4937aSGao Xiang { 10074f05687fSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(be->sb); 10084f05687fSGao Xiang struct z_erofs_pcluster *pcl = be->pcl; 1009cecf864dSYue Hu unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 10102bfab9c0SGao Xiang unsigned int i, inputsize; 101167148551SGao Xiang int err2; 10122bfab9c0SGao Xiang struct page *page; 10132bfab9c0SGao Xiang bool overlapped; 101447e4937aSGao Xiang 101587ca34a7SGao Xiang mutex_lock(&pcl->lock); 10162bfab9c0SGao Xiang be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT; 101747e4937aSGao Xiang 1018fe3e5914SGao Xiang /* allocate (de)compressed page arrays if cannot be kept on stack */ 1019fe3e5914SGao Xiang be->decompressed_pages = NULL; 1020fe3e5914SGao Xiang be->compressed_pages = NULL; 1021fe3e5914SGao Xiang be->onstack_used = 0; 10222bfab9c0SGao Xiang if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) { 10234f05687fSGao Xiang be->decompressed_pages = be->onstack_pages; 10242bfab9c0SGao Xiang be->onstack_used = be->nr_pages; 10254f05687fSGao Xiang memset(be->decompressed_pages, 0, 10262bfab9c0SGao Xiang sizeof(struct page *) * be->nr_pages); 1027fe3e5914SGao Xiang } 1028fe3e5914SGao Xiang 1029fe3e5914SGao Xiang if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES) 1030fe3e5914SGao Xiang be->compressed_pages = be->onstack_pages + be->onstack_used; 1031fe3e5914SGao Xiang 1032fe3e5914SGao Xiang if (!be->decompressed_pages) 10334f05687fSGao Xiang be->decompressed_pages = 10342bfab9c0SGao Xiang kvcalloc(be->nr_pages, sizeof(struct page *), 1035e7368187SGao Xiang GFP_KERNEL | __GFP_NOFAIL); 1036fe3e5914SGao Xiang if (!be->compressed_pages) 1037fe3e5914SGao Xiang be->compressed_pages = 1038fe3e5914SGao Xiang kvcalloc(pclusterpages, sizeof(struct page *), 1039fe3e5914SGao Xiang GFP_KERNEL | __GFP_NOFAIL); 104047e4937aSGao Xiang 1041267f2492SGao Xiang z_erofs_parse_out_bvecs(be); 10424f05687fSGao Xiang err2 = z_erofs_parse_in_bvecs(be, &overlapped); 10434f05687fSGao Xiang if (err2) 10444f05687fSGao Xiang err = err2; 10458d8a09b0SGao Xiang if (err) 104647e4937aSGao Xiang goto out; 104747e4937aSGao Xiang 1048cecf864dSYue Hu if (z_erofs_is_inline_pcluster(pcl)) 1049cecf864dSYue Hu inputsize = pcl->tailpacking_size; 1050cecf864dSYue Hu else 1051cecf864dSYue Hu inputsize = pclusterpages * PAGE_SIZE; 1052cecf864dSYue Hu 105347e4937aSGao Xiang err = z_erofs_decompress(&(struct z_erofs_decompress_req) { 10544f05687fSGao Xiang .sb = be->sb, 10554f05687fSGao Xiang .in = be->compressed_pages, 10564f05687fSGao Xiang .out = be->decompressed_pages, 1057cecf864dSYue Hu .pageofs_in = pcl->pageofs_in, 105887ca34a7SGao Xiang .pageofs_out = pcl->pageofs_out, 10599f6cc76eSGao Xiang .inputsize = inputsize, 10602bfab9c0SGao Xiang .outputsize = pcl->length, 106147e4937aSGao Xiang .alg = pcl->algorithmformat, 106247e4937aSGao Xiang .inplace_io = overlapped, 10632bfab9c0SGao Xiang .partial_decoding = pcl->partial, 1064267f2492SGao Xiang .fillgaps = pcl->multibases, 10654f05687fSGao Xiang }, be->pagepool); 106647e4937aSGao Xiang 106747e4937aSGao Xiang out: 1068cecf864dSYue Hu /* must handle all compressed pages before actual file pages */ 1069cecf864dSYue Hu if (z_erofs_is_inline_pcluster(pcl)) { 1070ed722fbcSGao Xiang page = pcl->compressed_bvecs[0].page; 1071ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL); 1072cecf864dSYue Hu put_page(page); 1073cecf864dSYue Hu } else { 1074cecf864dSYue Hu for (i = 0; i < pclusterpages; ++i) { 1075ed722fbcSGao Xiang page = pcl->compressed_bvecs[i].page; 107647e4937aSGao Xiang 107747e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) 107847e4937aSGao Xiang continue; 107947e4937aSGao Xiang 10806aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 10814f05687fSGao Xiang (void)z_erofs_put_shortlivedpage(be->pagepool, page); 1082ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 108347e4937aSGao Xiang } 1084cecf864dSYue Hu } 1085fe3e5914SGao Xiang if (be->compressed_pages < be->onstack_pages || 1086fe3e5914SGao Xiang be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES) 1087fe3e5914SGao Xiang kvfree(be->compressed_pages); 1088267f2492SGao Xiang z_erofs_fill_other_copies(be, err); 108947e4937aSGao Xiang 10902bfab9c0SGao Xiang for (i = 0; i < be->nr_pages; ++i) { 10914f05687fSGao Xiang page = be->decompressed_pages[i]; 109247e4937aSGao Xiang if (!page) 109347e4937aSGao Xiang continue; 109447e4937aSGao Xiang 10956aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 109647e4937aSGao Xiang 10976aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 10984f05687fSGao Xiang if (z_erofs_put_shortlivedpage(be->pagepool, page)) 109947e4937aSGao Xiang continue; 110067148551SGao Xiang if (err) 110167148551SGao Xiang z_erofs_page_mark_eio(page); 110247e4937aSGao Xiang z_erofs_onlinepage_endio(page); 110347e4937aSGao Xiang } 110447e4937aSGao Xiang 11054f05687fSGao Xiang if (be->decompressed_pages != be->onstack_pages) 11064f05687fSGao Xiang kvfree(be->decompressed_pages); 110747e4937aSGao Xiang 11082bfab9c0SGao Xiang pcl->length = 0; 11092bfab9c0SGao Xiang pcl->partial = true; 1110267f2492SGao Xiang pcl->multibases = false; 111106a304cdSGao Xiang pcl->bvset.nextpage = NULL; 111287ca34a7SGao Xiang pcl->vcnt = 0; 111347e4937aSGao Xiang 111487ca34a7SGao Xiang /* pcluster lock MUST be taken before the following line */ 111547e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 111687ca34a7SGao Xiang mutex_unlock(&pcl->lock); 111747e4937aSGao Xiang return err; 111847e4937aSGao Xiang } 111947e4937aSGao Xiang 11200c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 1121eaa9172aSGao Xiang struct page **pagepool) 112247e4937aSGao Xiang { 11234f05687fSGao Xiang struct z_erofs_decompress_backend be = { 11244f05687fSGao Xiang .sb = io->sb, 11254f05687fSGao Xiang .pagepool = pagepool, 1126267f2492SGao Xiang .decompressed_secondary_bvecs = 1127267f2492SGao Xiang LIST_HEAD_INIT(be.decompressed_secondary_bvecs), 11284f05687fSGao Xiang }; 112947e4937aSGao Xiang z_erofs_next_pcluster_t owned = io->head; 113047e4937aSGao Xiang 113147e4937aSGao Xiang while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { 11324f05687fSGao Xiang /* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ 113347e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); 11344f05687fSGao Xiang /* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */ 113547e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 113647e4937aSGao Xiang 11374f05687fSGao Xiang be.pcl = container_of(owned, struct z_erofs_pcluster, next); 11384f05687fSGao Xiang owned = READ_ONCE(be.pcl->next); 113947e4937aSGao Xiang 11404f05687fSGao Xiang z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0); 11414f05687fSGao Xiang erofs_workgroup_put(&be.pcl->obj); 114247e4937aSGao Xiang } 114347e4937aSGao Xiang } 114447e4937aSGao Xiang 11450c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work) 114647e4937aSGao Xiang { 1147a4b1fab1SGao Xiang struct z_erofs_decompressqueue *bgq = 1148a4b1fab1SGao Xiang container_of(work, struct z_erofs_decompressqueue, u.work); 1149eaa9172aSGao Xiang struct page *pagepool = NULL; 115047e4937aSGao Xiang 1151a4b1fab1SGao Xiang DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 11520c638f70SGao Xiang z_erofs_decompress_queue(bgq, &pagepool); 115347e4937aSGao Xiang 1154eaa9172aSGao Xiang erofs_release_pages(&pagepool); 1155a4b1fab1SGao Xiang kvfree(bgq); 115647e4937aSGao Xiang } 115747e4937aSGao Xiang 11587865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 11597865827cSGao Xiang bool sync, int bios) 11607865827cSGao Xiang { 11617865827cSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(io->sb); 11627865827cSGao Xiang 11637865827cSGao Xiang /* wake up the caller thread for sync decompression */ 11647865827cSGao Xiang if (sync) { 11657865827cSGao Xiang if (!atomic_add_return(bios, &io->pending_bios)) 116660b30050SHongyu Jin complete(&io->u.done); 11677865827cSGao Xiang return; 11687865827cSGao Xiang } 11697865827cSGao Xiang 11707865827cSGao Xiang if (atomic_add_return(bios, &io->pending_bios)) 11717865827cSGao Xiang return; 11727865827cSGao Xiang /* Use workqueue and sync decompression for atomic contexts only */ 11737865827cSGao Xiang if (in_atomic() || irqs_disabled()) { 11747865827cSGao Xiang queue_work(z_erofs_workqueue, &io->u.work); 11757865827cSGao Xiang /* enable sync decompression for readahead */ 11767865827cSGao Xiang if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) 11777865827cSGao Xiang sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON; 11787865827cSGao Xiang return; 11797865827cSGao Xiang } 11807865827cSGao Xiang z_erofs_decompressqueue_work(&io->u.work); 11817865827cSGao Xiang } 11827865827cSGao Xiang 118347e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 118447e4937aSGao Xiang unsigned int nr, 1185eaa9172aSGao Xiang struct page **pagepool, 11869f2731d6SGao Xiang struct address_space *mc) 118747e4937aSGao Xiang { 118847e4937aSGao Xiang const pgoff_t index = pcl->obj.index; 11899f2731d6SGao Xiang gfp_t gfp = mapping_gfp_mask(mc); 119047e4937aSGao Xiang bool tocache = false; 119147e4937aSGao Xiang 119247e4937aSGao Xiang struct address_space *mapping; 119347e4937aSGao Xiang struct page *oldpage, *page; 119447e4937aSGao Xiang 119547e4937aSGao Xiang compressed_page_t t; 119647e4937aSGao Xiang int justfound; 119747e4937aSGao Xiang 119847e4937aSGao Xiang repeat: 1199ed722fbcSGao Xiang page = READ_ONCE(pcl->compressed_bvecs[nr].page); 120047e4937aSGao Xiang oldpage = page; 120147e4937aSGao Xiang 120247e4937aSGao Xiang if (!page) 120347e4937aSGao Xiang goto out_allocpage; 120447e4937aSGao Xiang 120547e4937aSGao Xiang /* process the target tagged pointer */ 120647e4937aSGao Xiang t = tagptr_init(compressed_page_t, page); 120747e4937aSGao Xiang justfound = tagptr_unfold_tags(t); 120847e4937aSGao Xiang page = tagptr_unfold_ptr(t); 120947e4937aSGao Xiang 12101825c8d7SGao Xiang /* 12111825c8d7SGao Xiang * preallocated cached pages, which is used to avoid direct reclaim 12121825c8d7SGao Xiang * otherwise, it will go inplace I/O path instead. 12131825c8d7SGao Xiang */ 12141825c8d7SGao Xiang if (page->private == Z_EROFS_PREALLOCATED_PAGE) { 1215ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 12161825c8d7SGao Xiang set_page_private(page, 0); 12171825c8d7SGao Xiang tocache = true; 12181825c8d7SGao Xiang goto out_tocache; 12191825c8d7SGao Xiang } 122047e4937aSGao Xiang mapping = READ_ONCE(page->mapping); 122147e4937aSGao Xiang 122247e4937aSGao Xiang /* 12236aaa7b06SGao Xiang * file-backed online pages in plcuster are all locked steady, 122447e4937aSGao Xiang * therefore it is impossible for `mapping' to be NULL. 122547e4937aSGao Xiang */ 122647e4937aSGao Xiang if (mapping && mapping != mc) 122747e4937aSGao Xiang /* ought to be unmanaged pages */ 122847e4937aSGao Xiang goto out; 122947e4937aSGao Xiang 12306aaa7b06SGao Xiang /* directly return for shortlived page as well */ 12316aaa7b06SGao Xiang if (z_erofs_is_shortlived_page(page)) 12326aaa7b06SGao Xiang goto out; 12336aaa7b06SGao Xiang 123447e4937aSGao Xiang lock_page(page); 123547e4937aSGao Xiang 123647e4937aSGao Xiang /* only true if page reclaim goes wrong, should never happen */ 123747e4937aSGao Xiang DBG_BUGON(justfound && PagePrivate(page)); 123847e4937aSGao Xiang 123947e4937aSGao Xiang /* the page is still in manage cache */ 124047e4937aSGao Xiang if (page->mapping == mc) { 1241ed722fbcSGao Xiang WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 124247e4937aSGao Xiang 124347e4937aSGao Xiang if (!PagePrivate(page)) { 124447e4937aSGao Xiang /* 124547e4937aSGao Xiang * impossible to be !PagePrivate(page) for 124647e4937aSGao Xiang * the current restriction as well if 1247ed722fbcSGao Xiang * the page is already in compressed_bvecs[]. 124847e4937aSGao Xiang */ 124947e4937aSGao Xiang DBG_BUGON(!justfound); 125047e4937aSGao Xiang 125147e4937aSGao Xiang justfound = 0; 125247e4937aSGao Xiang set_page_private(page, (unsigned long)pcl); 125347e4937aSGao Xiang SetPagePrivate(page); 125447e4937aSGao Xiang } 125547e4937aSGao Xiang 125647e4937aSGao Xiang /* no need to submit io if it is already up-to-date */ 125747e4937aSGao Xiang if (PageUptodate(page)) { 125847e4937aSGao Xiang unlock_page(page); 125947e4937aSGao Xiang page = NULL; 126047e4937aSGao Xiang } 126147e4937aSGao Xiang goto out; 126247e4937aSGao Xiang } 126347e4937aSGao Xiang 126447e4937aSGao Xiang /* 126547e4937aSGao Xiang * the managed page has been truncated, it's unsafe to 126647e4937aSGao Xiang * reuse this one, let's allocate a new cache-managed page. 126747e4937aSGao Xiang */ 126847e4937aSGao Xiang DBG_BUGON(page->mapping); 126947e4937aSGao Xiang DBG_BUGON(!justfound); 127047e4937aSGao Xiang 127147e4937aSGao Xiang tocache = true; 127247e4937aSGao Xiang unlock_page(page); 127347e4937aSGao Xiang put_page(page); 127447e4937aSGao Xiang out_allocpage: 12755ddcee1fSGao Xiang page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 1276ed722fbcSGao Xiang if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page, 1277ed722fbcSGao Xiang oldpage, page)) { 1278eaa9172aSGao Xiang erofs_pagepool_add(pagepool, page); 12795ddcee1fSGao Xiang cond_resched(); 12805ddcee1fSGao Xiang goto repeat; 12815ddcee1fSGao Xiang } 12821825c8d7SGao Xiang out_tocache: 1283bf225074SGao Xiang if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1284bf225074SGao Xiang /* turn into temporary page if fails (1 ref) */ 1285bf225074SGao Xiang set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); 1286bf225074SGao Xiang goto out; 1287a30573b3SGao Xiang } 1288bf225074SGao Xiang attach_page_private(page, pcl); 1289bf225074SGao Xiang /* drop a refcount added by allocpage (then we have 2 refs here) */ 1290bf225074SGao Xiang put_page(page); 1291bf225074SGao Xiang 129247e4937aSGao Xiang out: /* the only exit (for tracing and debugging) */ 129347e4937aSGao Xiang return page; 129447e4937aSGao Xiang } 129547e4937aSGao Xiang 1296a4b1fab1SGao Xiang static struct z_erofs_decompressqueue * 1297a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb, 1298a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 129947e4937aSGao Xiang { 1300a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q; 130147e4937aSGao Xiang 1302a4b1fab1SGao Xiang if (fg && !*fg) { 1303a4b1fab1SGao Xiang q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1304a4b1fab1SGao Xiang if (!q) { 1305a4b1fab1SGao Xiang *fg = true; 1306a4b1fab1SGao Xiang goto fg_out; 130747e4937aSGao Xiang } 13080c638f70SGao Xiang INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1309a4b1fab1SGao Xiang } else { 1310a4b1fab1SGao Xiang fg_out: 1311a4b1fab1SGao Xiang q = fgq; 131260b30050SHongyu Jin init_completion(&fgq->u.done); 1313a4b1fab1SGao Xiang atomic_set(&fgq->pending_bios, 0); 131467148551SGao Xiang q->eio = false; 1315a4b1fab1SGao Xiang } 1316a4b1fab1SGao Xiang q->sb = sb; 1317a4b1fab1SGao Xiang q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1318a4b1fab1SGao Xiang return q; 131947e4937aSGao Xiang } 132047e4937aSGao Xiang 132147e4937aSGao Xiang /* define decompression jobqueue types */ 132247e4937aSGao Xiang enum { 132347e4937aSGao Xiang JQ_BYPASS, 132447e4937aSGao Xiang JQ_SUBMIT, 132547e4937aSGao Xiang NR_JOBQUEUES, 132647e4937aSGao Xiang }; 132747e4937aSGao Xiang 132847e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb, 1329a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[], 1330a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 133147e4937aSGao Xiang { 133247e4937aSGao Xiang /* 133347e4937aSGao Xiang * if managed cache is enabled, bypass jobqueue is needed, 133447e4937aSGao Xiang * no need to read from device for all pclusters in this queue. 133547e4937aSGao Xiang */ 1336a4b1fab1SGao Xiang q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1337a4b1fab1SGao Xiang q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg); 133847e4937aSGao Xiang 1339a4b1fab1SGao Xiang return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg)); 134047e4937aSGao Xiang } 134147e4937aSGao Xiang 134247e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 134347e4937aSGao Xiang z_erofs_next_pcluster_t qtail[], 134447e4937aSGao Xiang z_erofs_next_pcluster_t owned_head) 134547e4937aSGao Xiang { 134647e4937aSGao Xiang z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 134747e4937aSGao Xiang z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 134847e4937aSGao Xiang 134947e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 135047e4937aSGao Xiang if (owned_head == Z_EROFS_PCLUSTER_TAIL) 135147e4937aSGao Xiang owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 135247e4937aSGao Xiang 135347e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); 135447e4937aSGao Xiang 135547e4937aSGao Xiang WRITE_ONCE(*submit_qtail, owned_head); 135647e4937aSGao Xiang WRITE_ONCE(*bypass_qtail, &pcl->next); 135747e4937aSGao Xiang 135847e4937aSGao Xiang qtail[JQ_BYPASS] = &pcl->next; 135947e4937aSGao Xiang } 136047e4937aSGao Xiang 13617865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio) 13627865827cSGao Xiang { 13637865827cSGao Xiang tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private); 13647865827cSGao Xiang struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t); 13657865827cSGao Xiang blk_status_t err = bio->bi_status; 13667865827cSGao Xiang struct bio_vec *bvec; 13677865827cSGao Xiang struct bvec_iter_all iter_all; 13687865827cSGao Xiang 13697865827cSGao Xiang bio_for_each_segment_all(bvec, bio, iter_all) { 13707865827cSGao Xiang struct page *page = bvec->bv_page; 13717865827cSGao Xiang 13727865827cSGao Xiang DBG_BUGON(PageUptodate(page)); 13737865827cSGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 13747865827cSGao Xiang 13757865827cSGao Xiang if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 13767865827cSGao Xiang if (!err) 13777865827cSGao Xiang SetPageUptodate(page); 13787865827cSGao Xiang unlock_page(page); 13797865827cSGao Xiang } 13807865827cSGao Xiang } 138167148551SGao Xiang if (err) 138267148551SGao Xiang q->eio = true; 13837865827cSGao Xiang z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1); 13847865827cSGao Xiang bio_put(bio); 13857865827cSGao Xiang } 13867865827cSGao Xiang 138783a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f, 1388eaa9172aSGao Xiang struct page **pagepool, 1389a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, 1390a4b1fab1SGao Xiang bool *force_fg) 139147e4937aSGao Xiang { 139283a386c0SGao Xiang struct super_block *sb = f->inode->i_sb; 139383a386c0SGao Xiang struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb)); 139447e4937aSGao Xiang z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1395a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 139647e4937aSGao Xiang void *bi_private; 13975c6dcc57SGao Xiang z_erofs_next_pcluster_t owned_head = f->owned_head; 1398dfeab2e9SGao Xiang /* bio is NULL initially, so no need to initialize last_{index,bdev} */ 13993f649ab7SKees Cook pgoff_t last_index; 1400dfeab2e9SGao Xiang struct block_device *last_bdev; 14011e4a2955SGao Xiang unsigned int nr_bios = 0; 14021e4a2955SGao Xiang struct bio *bio = NULL; 140382e60d00SJohannes Weiner unsigned long pflags; 140482e60d00SJohannes Weiner int memstall = 0; 140547e4937aSGao Xiang 1406a4b1fab1SGao Xiang bi_private = jobqueueset_init(sb, q, fgq, force_fg); 1407a4b1fab1SGao Xiang qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1408a4b1fab1SGao Xiang qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 140947e4937aSGao Xiang 141047e4937aSGao Xiang /* by default, all need io submission */ 141147e4937aSGao Xiang q[JQ_SUBMIT]->head = owned_head; 141247e4937aSGao Xiang 141347e4937aSGao Xiang do { 1414dfeab2e9SGao Xiang struct erofs_map_dev mdev; 141547e4937aSGao Xiang struct z_erofs_pcluster *pcl; 14161e4a2955SGao Xiang pgoff_t cur, end; 14171e4a2955SGao Xiang unsigned int i = 0; 14181e4a2955SGao Xiang bool bypass = true; 141947e4937aSGao Xiang 142047e4937aSGao Xiang /* no possible 'owned_head' equals the following */ 142147e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 142247e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 142347e4937aSGao Xiang 142447e4937aSGao Xiang pcl = container_of(owned_head, struct z_erofs_pcluster, next); 142547e4937aSGao Xiang 1426cecf864dSYue Hu /* close the main owned chain at first */ 1427cecf864dSYue Hu owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 1428cecf864dSYue Hu Z_EROFS_PCLUSTER_TAIL_CLOSED); 1429cecf864dSYue Hu if (z_erofs_is_inline_pcluster(pcl)) { 1430cecf864dSYue Hu move_to_bypass_jobqueue(pcl, qtail, owned_head); 1431cecf864dSYue Hu continue; 1432cecf864dSYue Hu } 1433cecf864dSYue Hu 1434dfeab2e9SGao Xiang /* no device id here, thus it will always succeed */ 1435dfeab2e9SGao Xiang mdev = (struct erofs_map_dev) { 1436dfeab2e9SGao Xiang .m_pa = blknr_to_addr(pcl->obj.index), 1437dfeab2e9SGao Xiang }; 1438dfeab2e9SGao Xiang (void)erofs_map_dev(sb, &mdev); 1439dfeab2e9SGao Xiang 1440dfeab2e9SGao Xiang cur = erofs_blknr(mdev.m_pa); 14419f6cc76eSGao Xiang end = cur + pcl->pclusterpages; 144247e4937aSGao Xiang 14431e4a2955SGao Xiang do { 14441e4a2955SGao Xiang struct page *page; 144547e4937aSGao Xiang 14461e4a2955SGao Xiang page = pickup_page_for_submission(pcl, i++, pagepool, 144783a386c0SGao Xiang mc); 14481e4a2955SGao Xiang if (!page) 14491e4a2955SGao Xiang continue; 145047e4937aSGao Xiang 1451dfeab2e9SGao Xiang if (bio && (cur != last_index + 1 || 1452dfeab2e9SGao Xiang last_bdev != mdev.m_bdev)) { 145347e4937aSGao Xiang submit_bio_retry: 145494e4e153SGao Xiang submit_bio(bio); 145582e60d00SJohannes Weiner if (memstall) { 145682e60d00SJohannes Weiner psi_memstall_leave(&pflags); 145782e60d00SJohannes Weiner memstall = 0; 145882e60d00SJohannes Weiner } 145947e4937aSGao Xiang bio = NULL; 146047e4937aSGao Xiang } 146147e4937aSGao Xiang 146282e60d00SJohannes Weiner if (unlikely(PageWorkingset(page)) && !memstall) { 146399486c51SChristoph Hellwig psi_memstall_enter(&pflags); 146482e60d00SJohannes Weiner memstall = 1; 146582e60d00SJohannes Weiner } 146699486c51SChristoph Hellwig 146747e4937aSGao Xiang if (!bio) { 146807888c66SChristoph Hellwig bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS, 146907888c66SChristoph Hellwig REQ_OP_READ, GFP_NOIO); 14700c638f70SGao Xiang bio->bi_end_io = z_erofs_decompressqueue_endio; 1471dfeab2e9SGao Xiang 1472dfeab2e9SGao Xiang last_bdev = mdev.m_bdev; 14731e4a2955SGao Xiang bio->bi_iter.bi_sector = (sector_t)cur << 1474a5c0b780SGao Xiang LOG_SECTORS_PER_BLOCK; 1475a5c0b780SGao Xiang bio->bi_private = bi_private; 14766ea5aad3SGao Xiang if (f->readahead) 14776ea5aad3SGao Xiang bio->bi_opf |= REQ_RAHEAD; 147847e4937aSGao Xiang ++nr_bios; 147947e4937aSGao Xiang } 148047e4937aSGao Xiang 14816c3e485eSGao Xiang if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 148247e4937aSGao Xiang goto submit_bio_retry; 148347e4937aSGao Xiang 14841e4a2955SGao Xiang last_index = cur; 14851e4a2955SGao Xiang bypass = false; 14861e4a2955SGao Xiang } while (++cur < end); 148747e4937aSGao Xiang 14881e4a2955SGao Xiang if (!bypass) 148947e4937aSGao Xiang qtail[JQ_SUBMIT] = &pcl->next; 149047e4937aSGao Xiang else 149147e4937aSGao Xiang move_to_bypass_jobqueue(pcl, qtail, owned_head); 149247e4937aSGao Xiang } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 149347e4937aSGao Xiang 149499486c51SChristoph Hellwig if (bio) { 149594e4e153SGao Xiang submit_bio(bio); 149682e60d00SJohannes Weiner if (memstall) 149782e60d00SJohannes Weiner psi_memstall_leave(&pflags); 149899486c51SChristoph Hellwig } 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