147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only 247e4937aSGao Xiang /* 347e4937aSGao Xiang * Copyright (C) 2018 HUAWEI, Inc. 447e4937aSGao Xiang * http://www.huawei.com/ 547e4937aSGao Xiang * Created by Gao Xiang <gaoxiang25@huawei.com> 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 /* 1447e4937aSGao Xiang * a compressed_pages[] placeholder in order to avoid 1547e4937aSGao Xiang * being filled with file pages for in-place decompression. 1647e4937aSGao Xiang */ 1747e4937aSGao Xiang #define PAGE_UNALLOCATED ((void *)0x5F0E4B1D) 1847e4937aSGao Xiang 1947e4937aSGao Xiang /* how to allocate cached pages for a pcluster */ 2047e4937aSGao Xiang enum z_erofs_cache_alloctype { 2147e4937aSGao Xiang DONTALLOC, /* don't allocate any cached pages */ 2247e4937aSGao Xiang DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */ 2347e4937aSGao Xiang }; 2447e4937aSGao Xiang 2547e4937aSGao Xiang /* 2647e4937aSGao Xiang * tagged pointer with 1-bit tag for all compressed pages 2747e4937aSGao Xiang * tag 0 - the page is just found with an extra page reference 2847e4937aSGao Xiang */ 2947e4937aSGao Xiang typedef tagptr1_t compressed_page_t; 3047e4937aSGao Xiang 3147e4937aSGao Xiang #define tag_compressed_page_justfound(page) \ 3247e4937aSGao Xiang tagptr_fold(compressed_page_t, page, 1) 3347e4937aSGao Xiang 3447e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly; 3547e4937aSGao Xiang static struct kmem_cache *pcluster_cachep __read_mostly; 3647e4937aSGao Xiang 3747e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void) 3847e4937aSGao Xiang { 3947e4937aSGao Xiang destroy_workqueue(z_erofs_workqueue); 4047e4937aSGao Xiang kmem_cache_destroy(pcluster_cachep); 4147e4937aSGao Xiang } 4247e4937aSGao Xiang 4347e4937aSGao Xiang static inline int init_unzip_workqueue(void) 4447e4937aSGao Xiang { 4547e4937aSGao Xiang const unsigned int onlinecpus = num_possible_cpus(); 4647e4937aSGao Xiang const unsigned int flags = WQ_UNBOUND | WQ_HIGHPRI | WQ_CPU_INTENSIVE; 4747e4937aSGao Xiang 4847e4937aSGao Xiang /* 4947e4937aSGao Xiang * no need to spawn too many threads, limiting threads could minimum 5047e4937aSGao Xiang * scheduling overhead, perhaps per-CPU threads should be better? 5147e4937aSGao Xiang */ 5247e4937aSGao Xiang z_erofs_workqueue = alloc_workqueue("erofs_unzipd", flags, 5347e4937aSGao Xiang onlinecpus + onlinecpus / 4); 5447e4937aSGao Xiang return z_erofs_workqueue ? 0 : -ENOMEM; 5547e4937aSGao Xiang } 5647e4937aSGao Xiang 5747e4937aSGao Xiang static void init_once(void *ptr) 5847e4937aSGao Xiang { 5947e4937aSGao Xiang struct z_erofs_pcluster *pcl = ptr; 6047e4937aSGao Xiang struct z_erofs_collection *cl = z_erofs_primarycollection(pcl); 6147e4937aSGao Xiang unsigned int i; 6247e4937aSGao Xiang 6347e4937aSGao Xiang mutex_init(&cl->lock); 6447e4937aSGao Xiang cl->nr_pages = 0; 6547e4937aSGao Xiang cl->vcnt = 0; 6647e4937aSGao Xiang for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i) 6747e4937aSGao Xiang pcl->compressed_pages[i] = NULL; 6847e4937aSGao Xiang } 6947e4937aSGao Xiang 7047e4937aSGao Xiang static void init_always(struct z_erofs_pcluster *pcl) 7147e4937aSGao Xiang { 7247e4937aSGao Xiang struct z_erofs_collection *cl = z_erofs_primarycollection(pcl); 7347e4937aSGao Xiang 7447e4937aSGao Xiang atomic_set(&pcl->obj.refcount, 1); 7547e4937aSGao Xiang 7647e4937aSGao Xiang DBG_BUGON(cl->nr_pages); 7747e4937aSGao Xiang DBG_BUGON(cl->vcnt); 7847e4937aSGao Xiang } 7947e4937aSGao Xiang 8047e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void) 8147e4937aSGao Xiang { 8247e4937aSGao Xiang pcluster_cachep = kmem_cache_create("erofs_compress", 8347e4937aSGao Xiang Z_EROFS_WORKGROUP_SIZE, 0, 8447e4937aSGao Xiang SLAB_RECLAIM_ACCOUNT, init_once); 8547e4937aSGao Xiang if (pcluster_cachep) { 8647e4937aSGao Xiang if (!init_unzip_workqueue()) 8747e4937aSGao Xiang return 0; 8847e4937aSGao Xiang 8947e4937aSGao Xiang kmem_cache_destroy(pcluster_cachep); 9047e4937aSGao Xiang } 9147e4937aSGao Xiang return -ENOMEM; 9247e4937aSGao Xiang } 9347e4937aSGao Xiang 9447e4937aSGao Xiang enum z_erofs_collectmode { 9547e4937aSGao Xiang COLLECT_SECONDARY, 9647e4937aSGao Xiang COLLECT_PRIMARY, 9747e4937aSGao Xiang /* 9847e4937aSGao Xiang * The current collection was the tail of an exist chain, in addition 9947e4937aSGao Xiang * that the previous processed chained collections are all decided to 10047e4937aSGao Xiang * be hooked up to it. 10147e4937aSGao Xiang * A new chain will be created for the remaining collections which are 10247e4937aSGao Xiang * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED, 10347e4937aSGao Xiang * the next collection cannot reuse the whole page safely in 10447e4937aSGao Xiang * the following scenario: 10547e4937aSGao Xiang * ________________________________________________________________ 10647e4937aSGao Xiang * | tail (partial) page | head (partial) page | 10747e4937aSGao Xiang * | (belongs to the next cl) | (belongs to the current cl) | 10847e4937aSGao Xiang * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________| 10947e4937aSGao Xiang */ 11047e4937aSGao Xiang COLLECT_PRIMARY_HOOKED, 11147e4937aSGao Xiang COLLECT_PRIMARY_FOLLOWED_NOINPLACE, 11247e4937aSGao Xiang /* 11347e4937aSGao Xiang * The current collection has been linked with the owned chain, and 11447e4937aSGao Xiang * could also be linked with the remaining collections, which means 11547e4937aSGao Xiang * if the processing page is the tail page of the collection, thus 11647e4937aSGao Xiang * the current collection can safely use the whole page (since 11747e4937aSGao Xiang * the previous collection is under control) for in-place I/O, as 11847e4937aSGao Xiang * illustrated below: 11947e4937aSGao Xiang * ________________________________________________________________ 12047e4937aSGao Xiang * | tail (partial) page | head (partial) page | 12147e4937aSGao Xiang * | (of the current cl) | (of the previous collection) | 12247e4937aSGao Xiang * | PRIMARY_FOLLOWED or | | 12347e4937aSGao Xiang * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________| 12447e4937aSGao Xiang * 12547e4937aSGao Xiang * [ (*) the above page can be used as inplace I/O. ] 12647e4937aSGao Xiang */ 12747e4937aSGao Xiang COLLECT_PRIMARY_FOLLOWED, 12847e4937aSGao Xiang }; 12947e4937aSGao Xiang 13047e4937aSGao Xiang struct z_erofs_collector { 13147e4937aSGao Xiang struct z_erofs_pagevec_ctor vector; 13247e4937aSGao Xiang 13347e4937aSGao Xiang struct z_erofs_pcluster *pcl, *tailpcl; 13447e4937aSGao Xiang struct z_erofs_collection *cl; 13547e4937aSGao Xiang struct page **compressedpages; 13647e4937aSGao Xiang z_erofs_next_pcluster_t owned_head; 13747e4937aSGao Xiang 13847e4937aSGao Xiang enum z_erofs_collectmode mode; 13947e4937aSGao Xiang }; 14047e4937aSGao Xiang 14147e4937aSGao Xiang struct z_erofs_decompress_frontend { 14247e4937aSGao Xiang struct inode *const inode; 14347e4937aSGao Xiang 14447e4937aSGao Xiang struct z_erofs_collector clt; 14547e4937aSGao Xiang struct erofs_map_blocks map; 14647e4937aSGao Xiang 14747e4937aSGao Xiang /* used for applying cache strategy on the fly */ 14847e4937aSGao Xiang bool backmost; 14947e4937aSGao Xiang erofs_off_t headoffset; 15047e4937aSGao Xiang }; 15147e4937aSGao Xiang 15247e4937aSGao Xiang #define COLLECTOR_INIT() { \ 15347e4937aSGao Xiang .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 15447e4937aSGao Xiang .mode = COLLECT_PRIMARY_FOLLOWED } 15547e4937aSGao Xiang 15647e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \ 15747e4937aSGao Xiang .inode = __i, .clt = COLLECTOR_INIT(), \ 15847e4937aSGao Xiang .backmost = true, } 15947e4937aSGao Xiang 16047e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES]; 16147e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock); 16247e4937aSGao Xiang 16347e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt, 16447e4937aSGao Xiang struct address_space *mc, 16547e4937aSGao Xiang enum z_erofs_cache_alloctype type, 16647e4937aSGao Xiang struct list_head *pagepool) 16747e4937aSGao Xiang { 16847e4937aSGao Xiang const struct z_erofs_pcluster *pcl = clt->pcl; 16947e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 17047e4937aSGao Xiang struct page **pages = clt->compressedpages; 17147e4937aSGao Xiang pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages); 17247e4937aSGao Xiang bool standalone = true; 17347e4937aSGao Xiang 17447e4937aSGao Xiang if (clt->mode < COLLECT_PRIMARY_FOLLOWED) 17547e4937aSGao Xiang return; 17647e4937aSGao Xiang 17747e4937aSGao Xiang for (; pages < pcl->compressed_pages + clusterpages; ++pages) { 17847e4937aSGao Xiang struct page *page; 17947e4937aSGao Xiang compressed_page_t t; 18047e4937aSGao Xiang 18147e4937aSGao Xiang /* the compressed page was loaded before */ 18247e4937aSGao Xiang if (READ_ONCE(*pages)) 18347e4937aSGao Xiang continue; 18447e4937aSGao Xiang 18547e4937aSGao Xiang page = find_get_page(mc, index); 18647e4937aSGao Xiang 18747e4937aSGao Xiang if (page) { 18847e4937aSGao Xiang t = tag_compressed_page_justfound(page); 18947e4937aSGao Xiang } else if (type == DELAYEDALLOC) { 19047e4937aSGao Xiang t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED); 19147e4937aSGao Xiang } else { /* DONTALLOC */ 19247e4937aSGao Xiang if (standalone) 19347e4937aSGao Xiang clt->compressedpages = pages; 19447e4937aSGao Xiang standalone = false; 19547e4937aSGao Xiang continue; 19647e4937aSGao Xiang } 19747e4937aSGao Xiang 19847e4937aSGao Xiang if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t))) 19947e4937aSGao Xiang continue; 20047e4937aSGao Xiang 20147e4937aSGao Xiang if (page) 20247e4937aSGao Xiang put_page(page); 20347e4937aSGao Xiang } 20447e4937aSGao Xiang 20547e4937aSGao Xiang if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */ 20647e4937aSGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE; 20747e4937aSGao Xiang } 20847e4937aSGao Xiang 20947e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */ 21047e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 21147e4937aSGao Xiang struct erofs_workgroup *grp) 21247e4937aSGao Xiang { 21347e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 21447e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 21547e4937aSGao Xiang struct address_space *const mapping = MNGD_MAPPING(sbi); 21647e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 21747e4937aSGao Xiang int i; 21847e4937aSGao Xiang 21947e4937aSGao Xiang /* 22047e4937aSGao Xiang * refcount of workgroup is now freezed as 1, 22147e4937aSGao Xiang * therefore no need to worry about available decompression users. 22247e4937aSGao Xiang */ 22347e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 22447e4937aSGao Xiang struct page *page = pcl->compressed_pages[i]; 22547e4937aSGao Xiang 22647e4937aSGao Xiang if (!page) 22747e4937aSGao Xiang continue; 22847e4937aSGao Xiang 22947e4937aSGao Xiang /* block other users from reclaiming or migrating the page */ 23047e4937aSGao Xiang if (!trylock_page(page)) 23147e4937aSGao Xiang return -EBUSY; 23247e4937aSGao Xiang 23347e4937aSGao Xiang if (unlikely(page->mapping != mapping)) 23447e4937aSGao Xiang continue; 23547e4937aSGao Xiang 23647e4937aSGao Xiang /* barrier is implied in the following 'unlock_page' */ 23747e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[i], NULL); 23847e4937aSGao Xiang set_page_private(page, 0); 23947e4937aSGao Xiang ClearPagePrivate(page); 24047e4937aSGao Xiang 24147e4937aSGao Xiang unlock_page(page); 24247e4937aSGao Xiang put_page(page); 24347e4937aSGao Xiang } 24447e4937aSGao Xiang return 0; 24547e4937aSGao Xiang } 24647e4937aSGao Xiang 24747e4937aSGao Xiang int erofs_try_to_free_cached_page(struct address_space *mapping, 24847e4937aSGao Xiang struct page *page) 24947e4937aSGao Xiang { 25047e4937aSGao Xiang struct z_erofs_pcluster *const pcl = (void *)page_private(page); 25147e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 25247e4937aSGao Xiang int ret = 0; /* 0 - busy */ 25347e4937aSGao Xiang 25447e4937aSGao Xiang if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) { 25547e4937aSGao Xiang unsigned int i; 25647e4937aSGao Xiang 25747e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 25847e4937aSGao Xiang if (pcl->compressed_pages[i] == page) { 25947e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[i], NULL); 26047e4937aSGao Xiang ret = 1; 26147e4937aSGao Xiang break; 26247e4937aSGao Xiang } 26347e4937aSGao Xiang } 26447e4937aSGao Xiang erofs_workgroup_unfreeze(&pcl->obj, 1); 26547e4937aSGao Xiang 26647e4937aSGao Xiang if (ret) { 26747e4937aSGao Xiang ClearPagePrivate(page); 26847e4937aSGao Xiang put_page(page); 26947e4937aSGao Xiang } 27047e4937aSGao Xiang } 27147e4937aSGao Xiang return ret; 27247e4937aSGao Xiang } 27347e4937aSGao Xiang 27447e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */ 27547e4937aSGao Xiang static inline bool try_inplace_io(struct z_erofs_collector *clt, 27647e4937aSGao Xiang struct page *page) 27747e4937aSGao Xiang { 27847e4937aSGao Xiang struct z_erofs_pcluster *const pcl = clt->pcl; 27947e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 28047e4937aSGao Xiang 28147e4937aSGao Xiang while (clt->compressedpages < pcl->compressed_pages + clusterpages) { 28247e4937aSGao Xiang if (!cmpxchg(clt->compressedpages++, NULL, page)) 28347e4937aSGao Xiang return true; 28447e4937aSGao Xiang } 28547e4937aSGao Xiang return false; 28647e4937aSGao Xiang } 28747e4937aSGao Xiang 28847e4937aSGao Xiang /* callers must be with collection lock held */ 28947e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt, 29047e4937aSGao Xiang struct page *page, 29147e4937aSGao Xiang enum z_erofs_page_type type) 29247e4937aSGao Xiang { 29347e4937aSGao Xiang int ret; 29447e4937aSGao Xiang bool occupied; 29547e4937aSGao Xiang 29647e4937aSGao Xiang /* give priority for inplaceio */ 29747e4937aSGao Xiang if (clt->mode >= COLLECT_PRIMARY && 29847e4937aSGao Xiang type == Z_EROFS_PAGE_TYPE_EXCLUSIVE && 29947e4937aSGao Xiang try_inplace_io(clt, page)) 30047e4937aSGao Xiang return 0; 30147e4937aSGao Xiang 30247e4937aSGao Xiang ret = z_erofs_pagevec_enqueue(&clt->vector, 30347e4937aSGao Xiang page, type, &occupied); 30447e4937aSGao Xiang clt->cl->vcnt += (unsigned int)ret; 30547e4937aSGao Xiang 30647e4937aSGao Xiang return ret ? 0 : -EAGAIN; 30747e4937aSGao Xiang } 30847e4937aSGao Xiang 30947e4937aSGao Xiang static enum z_erofs_collectmode 31047e4937aSGao Xiang try_to_claim_pcluster(struct z_erofs_pcluster *pcl, 31147e4937aSGao Xiang z_erofs_next_pcluster_t *owned_head) 31247e4937aSGao Xiang { 31347e4937aSGao Xiang /* let's claim these following types of pclusters */ 31447e4937aSGao Xiang retry: 31547e4937aSGao Xiang if (pcl->next == Z_EROFS_PCLUSTER_NIL) { 31647e4937aSGao Xiang /* type 1, nil pcluster */ 31747e4937aSGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 31847e4937aSGao Xiang *owned_head) != Z_EROFS_PCLUSTER_NIL) 31947e4937aSGao Xiang goto retry; 32047e4937aSGao Xiang 32147e4937aSGao Xiang *owned_head = &pcl->next; 32247e4937aSGao Xiang /* lucky, I am the followee :) */ 32347e4937aSGao Xiang return COLLECT_PRIMARY_FOLLOWED; 32447e4937aSGao Xiang } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) { 32547e4937aSGao Xiang /* 32647e4937aSGao Xiang * type 2, link to the end of a existing open chain, 32747e4937aSGao Xiang * be careful that its submission itself is governed 32847e4937aSGao Xiang * by the original owned chain. 32947e4937aSGao Xiang */ 33047e4937aSGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 33147e4937aSGao Xiang *owned_head) != Z_EROFS_PCLUSTER_TAIL) 33247e4937aSGao Xiang goto retry; 33347e4937aSGao Xiang *owned_head = Z_EROFS_PCLUSTER_TAIL; 33447e4937aSGao Xiang return COLLECT_PRIMARY_HOOKED; 33547e4937aSGao Xiang } 33647e4937aSGao Xiang return COLLECT_PRIMARY; /* :( better luck next time */ 33747e4937aSGao Xiang } 33847e4937aSGao Xiang 33947e4937aSGao Xiang static struct z_erofs_collection *cllookup(struct z_erofs_collector *clt, 34047e4937aSGao Xiang struct inode *inode, 34147e4937aSGao Xiang struct erofs_map_blocks *map) 34247e4937aSGao Xiang { 34347e4937aSGao Xiang struct erofs_workgroup *grp; 34447e4937aSGao Xiang struct z_erofs_pcluster *pcl; 34547e4937aSGao Xiang struct z_erofs_collection *cl; 34647e4937aSGao Xiang unsigned int length; 34747e4937aSGao Xiang bool tag; 34847e4937aSGao Xiang 34947e4937aSGao Xiang grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT, &tag); 35047e4937aSGao Xiang if (!grp) 35147e4937aSGao Xiang return NULL; 35247e4937aSGao Xiang 35347e4937aSGao Xiang pcl = container_of(grp, struct z_erofs_pcluster, obj); 35447e4937aSGao Xiang if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) { 35547e4937aSGao Xiang DBG_BUGON(1); 35647e4937aSGao Xiang erofs_workgroup_put(grp); 35747e4937aSGao Xiang return ERR_PTR(-EFSCORRUPTED); 35847e4937aSGao Xiang } 35947e4937aSGao Xiang 36047e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 36147e4937aSGao Xiang if (unlikely(cl->pageofs != (map->m_la & ~PAGE_MASK))) { 36247e4937aSGao Xiang DBG_BUGON(1); 36347e4937aSGao Xiang erofs_workgroup_put(grp); 36447e4937aSGao Xiang return ERR_PTR(-EFSCORRUPTED); 36547e4937aSGao Xiang } 36647e4937aSGao Xiang 36747e4937aSGao Xiang length = READ_ONCE(pcl->length); 36847e4937aSGao Xiang if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) { 36947e4937aSGao Xiang if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) { 37047e4937aSGao Xiang DBG_BUGON(1); 37147e4937aSGao Xiang erofs_workgroup_put(grp); 37247e4937aSGao Xiang return ERR_PTR(-EFSCORRUPTED); 37347e4937aSGao Xiang } 37447e4937aSGao Xiang } else { 37547e4937aSGao Xiang unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT; 37647e4937aSGao Xiang 37747e4937aSGao Xiang if (map->m_flags & EROFS_MAP_FULL_MAPPED) 37847e4937aSGao Xiang llen |= Z_EROFS_PCLUSTER_FULL_LENGTH; 37947e4937aSGao Xiang 38047e4937aSGao Xiang while (llen > length && 38147e4937aSGao Xiang length != cmpxchg_relaxed(&pcl->length, length, llen)) { 38247e4937aSGao Xiang cpu_relax(); 38347e4937aSGao Xiang length = READ_ONCE(pcl->length); 38447e4937aSGao Xiang } 38547e4937aSGao Xiang } 38647e4937aSGao Xiang mutex_lock(&cl->lock); 38747e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 38847e4937aSGao Xiang if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) 38947e4937aSGao Xiang clt->tailpcl = pcl; 39047e4937aSGao Xiang clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head); 39147e4937aSGao Xiang /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */ 39247e4937aSGao Xiang if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) 39347e4937aSGao Xiang clt->tailpcl = NULL; 39447e4937aSGao Xiang clt->pcl = pcl; 39547e4937aSGao Xiang clt->cl = cl; 39647e4937aSGao Xiang return cl; 39747e4937aSGao Xiang } 39847e4937aSGao Xiang 39947e4937aSGao Xiang static struct z_erofs_collection *clregister(struct z_erofs_collector *clt, 40047e4937aSGao Xiang struct inode *inode, 40147e4937aSGao Xiang struct erofs_map_blocks *map) 40247e4937aSGao Xiang { 40347e4937aSGao Xiang struct z_erofs_pcluster *pcl; 40447e4937aSGao Xiang struct z_erofs_collection *cl; 40547e4937aSGao Xiang int err; 40647e4937aSGao Xiang 40747e4937aSGao Xiang /* no available workgroup, let's allocate one */ 40847e4937aSGao Xiang pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS); 40947e4937aSGao Xiang if (unlikely(!pcl)) 41047e4937aSGao Xiang return ERR_PTR(-ENOMEM); 41147e4937aSGao Xiang 41247e4937aSGao Xiang init_always(pcl); 41347e4937aSGao Xiang pcl->obj.index = map->m_pa >> PAGE_SHIFT; 41447e4937aSGao Xiang 41547e4937aSGao Xiang pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) | 41647e4937aSGao Xiang (map->m_flags & EROFS_MAP_FULL_MAPPED ? 41747e4937aSGao Xiang Z_EROFS_PCLUSTER_FULL_LENGTH : 0); 41847e4937aSGao Xiang 41947e4937aSGao Xiang if (map->m_flags & EROFS_MAP_ZIPPED) 42047e4937aSGao Xiang pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4; 42147e4937aSGao Xiang else 42247e4937aSGao Xiang pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED; 42347e4937aSGao Xiang 42447e4937aSGao Xiang pcl->clusterbits = EROFS_V(inode)->z_physical_clusterbits[0]; 42547e4937aSGao Xiang pcl->clusterbits -= PAGE_SHIFT; 42647e4937aSGao Xiang 42747e4937aSGao Xiang /* new pclusters should be claimed as type 1, primary and followed */ 42847e4937aSGao Xiang pcl->next = clt->owned_head; 42947e4937aSGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED; 43047e4937aSGao Xiang 43147e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 43247e4937aSGao Xiang cl->pageofs = map->m_la & ~PAGE_MASK; 43347e4937aSGao Xiang 43447e4937aSGao Xiang /* 43547e4937aSGao Xiang * lock all primary followed works before visible to others 43647e4937aSGao Xiang * and mutex_trylock *never* fails for a new pcluster. 43747e4937aSGao Xiang */ 43847e4937aSGao Xiang mutex_trylock(&cl->lock); 43947e4937aSGao Xiang 44047e4937aSGao Xiang err = erofs_register_workgroup(inode->i_sb, &pcl->obj, 0); 44147e4937aSGao Xiang if (err) { 44247e4937aSGao Xiang mutex_unlock(&cl->lock); 44347e4937aSGao Xiang kmem_cache_free(pcluster_cachep, pcl); 44447e4937aSGao Xiang return ERR_PTR(-EAGAIN); 44547e4937aSGao Xiang } 44647e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 44747e4937aSGao Xiang if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) 44847e4937aSGao Xiang clt->tailpcl = pcl; 44947e4937aSGao Xiang clt->owned_head = &pcl->next; 45047e4937aSGao Xiang clt->pcl = pcl; 45147e4937aSGao Xiang clt->cl = cl; 45247e4937aSGao Xiang return cl; 45347e4937aSGao Xiang } 45447e4937aSGao Xiang 45547e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt, 45647e4937aSGao Xiang struct inode *inode, 45747e4937aSGao Xiang struct erofs_map_blocks *map) 45847e4937aSGao Xiang { 45947e4937aSGao Xiang struct z_erofs_collection *cl; 46047e4937aSGao Xiang 46147e4937aSGao Xiang DBG_BUGON(clt->cl); 46247e4937aSGao Xiang 46347e4937aSGao Xiang /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */ 46447e4937aSGao Xiang DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL); 46547e4937aSGao Xiang DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 46647e4937aSGao Xiang 46747e4937aSGao Xiang if (!PAGE_ALIGNED(map->m_pa)) { 46847e4937aSGao Xiang DBG_BUGON(1); 46947e4937aSGao Xiang return -EINVAL; 47047e4937aSGao Xiang } 47147e4937aSGao Xiang 47247e4937aSGao Xiang repeat: 47347e4937aSGao Xiang cl = cllookup(clt, inode, map); 47447e4937aSGao Xiang if (!cl) { 47547e4937aSGao Xiang cl = clregister(clt, inode, map); 47647e4937aSGao Xiang 47747e4937aSGao Xiang if (unlikely(cl == ERR_PTR(-EAGAIN))) 47847e4937aSGao Xiang goto repeat; 47947e4937aSGao Xiang } 48047e4937aSGao Xiang 48147e4937aSGao Xiang if (IS_ERR(cl)) 48247e4937aSGao Xiang return PTR_ERR(cl); 48347e4937aSGao Xiang 48447e4937aSGao Xiang z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS, 48547e4937aSGao Xiang cl->pagevec, cl->vcnt); 48647e4937aSGao Xiang 48747e4937aSGao Xiang clt->compressedpages = clt->pcl->compressed_pages; 48847e4937aSGao Xiang if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */ 48947e4937aSGao Xiang clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES; 49047e4937aSGao Xiang return 0; 49147e4937aSGao Xiang } 49247e4937aSGao Xiang 49347e4937aSGao Xiang /* 49447e4937aSGao Xiang * keep in mind that no referenced pclusters will be freed 49547e4937aSGao Xiang * only after a RCU grace period. 49647e4937aSGao Xiang */ 49747e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head) 49847e4937aSGao Xiang { 49947e4937aSGao Xiang struct z_erofs_collection *const cl = 50047e4937aSGao Xiang container_of(head, struct z_erofs_collection, rcu); 50147e4937aSGao Xiang 50247e4937aSGao Xiang kmem_cache_free(pcluster_cachep, 50347e4937aSGao Xiang container_of(cl, struct z_erofs_pcluster, 50447e4937aSGao Xiang primary_collection)); 50547e4937aSGao Xiang } 50647e4937aSGao Xiang 50747e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 50847e4937aSGao Xiang { 50947e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 51047e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 51147e4937aSGao Xiang struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl); 51247e4937aSGao Xiang 51347e4937aSGao Xiang call_rcu(&cl->rcu, z_erofs_rcu_callback); 51447e4937aSGao Xiang } 51547e4937aSGao Xiang 51647e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl) 51747e4937aSGao Xiang { 51847e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 51947e4937aSGao Xiang container_of(cl, struct z_erofs_pcluster, primary_collection); 52047e4937aSGao Xiang 52147e4937aSGao Xiang erofs_workgroup_put(&pcl->obj); 52247e4937aSGao Xiang } 52347e4937aSGao Xiang 52447e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt) 52547e4937aSGao Xiang { 52647e4937aSGao Xiang struct z_erofs_collection *cl = clt->cl; 52747e4937aSGao Xiang 52847e4937aSGao Xiang if (!cl) 52947e4937aSGao Xiang return false; 53047e4937aSGao Xiang 53147e4937aSGao Xiang z_erofs_pagevec_ctor_exit(&clt->vector, false); 53247e4937aSGao Xiang mutex_unlock(&cl->lock); 53347e4937aSGao Xiang 53447e4937aSGao Xiang /* 53547e4937aSGao Xiang * if all pending pages are added, don't hold its reference 53647e4937aSGao Xiang * any longer if the pcluster isn't hosted by ourselves. 53747e4937aSGao Xiang */ 53847e4937aSGao Xiang if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE) 53947e4937aSGao Xiang z_erofs_collection_put(cl); 54047e4937aSGao Xiang 54147e4937aSGao Xiang clt->cl = NULL; 54247e4937aSGao Xiang return true; 54347e4937aSGao Xiang } 54447e4937aSGao Xiang 54547e4937aSGao Xiang static inline struct page *__stagingpage_alloc(struct list_head *pagepool, 54647e4937aSGao Xiang gfp_t gfp) 54747e4937aSGao Xiang { 54847e4937aSGao Xiang struct page *page = erofs_allocpage(pagepool, gfp, true); 54947e4937aSGao Xiang 55047e4937aSGao Xiang page->mapping = Z_EROFS_MAPPING_STAGING; 55147e4937aSGao Xiang return page; 55247e4937aSGao Xiang } 55347e4937aSGao Xiang 55447e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe, 55547e4937aSGao Xiang unsigned int cachestrategy, 55647e4937aSGao Xiang erofs_off_t la) 55747e4937aSGao Xiang { 55847e4937aSGao Xiang if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 55947e4937aSGao Xiang return false; 56047e4937aSGao Xiang 56147e4937aSGao Xiang if (fe->backmost) 56247e4937aSGao Xiang return true; 56347e4937aSGao Xiang 56447e4937aSGao Xiang return cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 56547e4937aSGao Xiang la < fe->headoffset; 56647e4937aSGao Xiang } 56747e4937aSGao Xiang 56847e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 56947e4937aSGao Xiang struct page *page, 57047e4937aSGao Xiang struct list_head *pagepool) 57147e4937aSGao Xiang { 57247e4937aSGao Xiang struct inode *const inode = fe->inode; 57347e4937aSGao Xiang struct erofs_sb_info *const sbi __maybe_unused = EROFS_I_SB(inode); 57447e4937aSGao Xiang struct erofs_map_blocks *const map = &fe->map; 57547e4937aSGao Xiang struct z_erofs_collector *const clt = &fe->clt; 57647e4937aSGao Xiang const loff_t offset = page_offset(page); 57747e4937aSGao Xiang bool tight = (clt->mode >= COLLECT_PRIMARY_HOOKED); 57847e4937aSGao Xiang 57947e4937aSGao Xiang enum z_erofs_cache_alloctype cache_strategy; 58047e4937aSGao Xiang enum z_erofs_page_type page_type; 58147e4937aSGao Xiang unsigned int cur, end, spiltted, index; 58247e4937aSGao Xiang int err = 0; 58347e4937aSGao Xiang 58447e4937aSGao Xiang /* register locked file pages as online pages in pack */ 58547e4937aSGao Xiang z_erofs_onlinepage_init(page); 58647e4937aSGao Xiang 58747e4937aSGao Xiang spiltted = 0; 58847e4937aSGao Xiang end = PAGE_SIZE; 58947e4937aSGao Xiang repeat: 59047e4937aSGao Xiang cur = end - 1; 59147e4937aSGao Xiang 59247e4937aSGao Xiang /* lucky, within the range of the current map_blocks */ 59347e4937aSGao Xiang if (offset + cur >= map->m_la && 59447e4937aSGao Xiang offset + cur < map->m_la + map->m_llen) { 59547e4937aSGao Xiang /* didn't get a valid collection previously (very rare) */ 59647e4937aSGao Xiang if (!clt->cl) 59747e4937aSGao Xiang goto restart_now; 59847e4937aSGao Xiang goto hitted; 59947e4937aSGao Xiang } 60047e4937aSGao Xiang 60147e4937aSGao Xiang /* go ahead the next map_blocks */ 60247e4937aSGao Xiang debugln("%s: [out-of-range] pos %llu", __func__, offset + cur); 60347e4937aSGao Xiang 60447e4937aSGao Xiang if (z_erofs_collector_end(clt)) 60547e4937aSGao Xiang fe->backmost = false; 60647e4937aSGao Xiang 60747e4937aSGao Xiang map->m_la = offset + cur; 60847e4937aSGao Xiang map->m_llen = 0; 60947e4937aSGao Xiang err = z_erofs_map_blocks_iter(inode, map, 0); 61047e4937aSGao Xiang if (unlikely(err)) 61147e4937aSGao Xiang goto err_out; 61247e4937aSGao Xiang 61347e4937aSGao Xiang restart_now: 61447e4937aSGao Xiang if (unlikely(!(map->m_flags & EROFS_MAP_MAPPED))) 61547e4937aSGao Xiang goto hitted; 61647e4937aSGao Xiang 61747e4937aSGao Xiang err = z_erofs_collector_begin(clt, inode, map); 61847e4937aSGao Xiang if (unlikely(err)) 61947e4937aSGao Xiang goto err_out; 62047e4937aSGao Xiang 62147e4937aSGao Xiang /* preload all compressed pages (maybe downgrade role if necessary) */ 62247e4937aSGao Xiang if (should_alloc_managed_pages(fe, sbi->cache_strategy, map->m_la)) 62347e4937aSGao Xiang cache_strategy = DELAYEDALLOC; 62447e4937aSGao Xiang else 62547e4937aSGao Xiang cache_strategy = DONTALLOC; 62647e4937aSGao Xiang 62747e4937aSGao Xiang preload_compressed_pages(clt, MNGD_MAPPING(sbi), 62847e4937aSGao Xiang cache_strategy, pagepool); 62947e4937aSGao Xiang 63047e4937aSGao Xiang tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED); 63147e4937aSGao Xiang hitted: 63247e4937aSGao Xiang cur = end - min_t(unsigned int, offset + end - map->m_la, end); 63347e4937aSGao Xiang if (unlikely(!(map->m_flags & EROFS_MAP_MAPPED))) { 63447e4937aSGao Xiang zero_user_segment(page, cur, end); 63547e4937aSGao Xiang goto next_part; 63647e4937aSGao Xiang } 63747e4937aSGao Xiang 63847e4937aSGao Xiang /* let's derive page type */ 63947e4937aSGao Xiang page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD : 64047e4937aSGao Xiang (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 64147e4937aSGao Xiang (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 64247e4937aSGao Xiang Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED)); 64347e4937aSGao Xiang 64447e4937aSGao Xiang if (cur) 64547e4937aSGao Xiang tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED); 64647e4937aSGao Xiang 64747e4937aSGao Xiang retry: 64847e4937aSGao Xiang err = z_erofs_attach_page(clt, page, page_type); 64947e4937aSGao Xiang /* should allocate an additional staging page for pagevec */ 65047e4937aSGao Xiang if (err == -EAGAIN) { 65147e4937aSGao Xiang struct page *const newpage = 65247e4937aSGao Xiang __stagingpage_alloc(pagepool, GFP_NOFS); 65347e4937aSGao Xiang 65447e4937aSGao Xiang err = z_erofs_attach_page(clt, newpage, 65547e4937aSGao Xiang Z_EROFS_PAGE_TYPE_EXCLUSIVE); 65647e4937aSGao Xiang if (likely(!err)) 65747e4937aSGao Xiang goto retry; 65847e4937aSGao Xiang } 65947e4937aSGao Xiang 66047e4937aSGao Xiang if (unlikely(err)) 66147e4937aSGao Xiang goto err_out; 66247e4937aSGao Xiang 66347e4937aSGao Xiang index = page->index - (map->m_la >> PAGE_SHIFT); 66447e4937aSGao Xiang 66547e4937aSGao Xiang z_erofs_onlinepage_fixup(page, index, true); 66647e4937aSGao Xiang 66747e4937aSGao Xiang /* bump up the number of spiltted parts of a page */ 66847e4937aSGao Xiang ++spiltted; 66947e4937aSGao Xiang /* also update nr_pages */ 67047e4937aSGao Xiang clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1); 67147e4937aSGao Xiang next_part: 67247e4937aSGao Xiang /* can be used for verification */ 67347e4937aSGao Xiang map->m_llen = offset + cur - map->m_la; 67447e4937aSGao Xiang 67547e4937aSGao Xiang end = cur; 67647e4937aSGao Xiang if (end > 0) 67747e4937aSGao Xiang goto repeat; 67847e4937aSGao Xiang 67947e4937aSGao Xiang out: 68047e4937aSGao Xiang z_erofs_onlinepage_endio(page); 68147e4937aSGao Xiang 68247e4937aSGao Xiang debugln("%s, finish page: %pK spiltted: %u map->m_llen %llu", 68347e4937aSGao Xiang __func__, page, spiltted, map->m_llen); 68447e4937aSGao Xiang return err; 68547e4937aSGao Xiang 68647e4937aSGao Xiang /* if some error occurred while processing this page */ 68747e4937aSGao Xiang err_out: 68847e4937aSGao Xiang SetPageError(page); 68947e4937aSGao Xiang goto out; 69047e4937aSGao Xiang } 69147e4937aSGao Xiang 69247e4937aSGao Xiang static void z_erofs_vle_unzip_kickoff(void *ptr, int bios) 69347e4937aSGao Xiang { 69447e4937aSGao Xiang tagptr1_t t = tagptr_init(tagptr1_t, ptr); 69547e4937aSGao Xiang struct z_erofs_unzip_io *io = tagptr_unfold_ptr(t); 69647e4937aSGao Xiang bool background = tagptr_unfold_tags(t); 69747e4937aSGao Xiang 69847e4937aSGao Xiang if (!background) { 69947e4937aSGao Xiang unsigned long flags; 70047e4937aSGao Xiang 70147e4937aSGao Xiang spin_lock_irqsave(&io->u.wait.lock, flags); 70247e4937aSGao Xiang if (!atomic_add_return(bios, &io->pending_bios)) 70347e4937aSGao Xiang wake_up_locked(&io->u.wait); 70447e4937aSGao Xiang spin_unlock_irqrestore(&io->u.wait.lock, flags); 70547e4937aSGao Xiang return; 70647e4937aSGao Xiang } 70747e4937aSGao Xiang 70847e4937aSGao Xiang if (!atomic_add_return(bios, &io->pending_bios)) 70947e4937aSGao Xiang queue_work(z_erofs_workqueue, &io->u.work); 71047e4937aSGao Xiang } 71147e4937aSGao Xiang 71247e4937aSGao Xiang static inline void z_erofs_vle_read_endio(struct bio *bio) 71347e4937aSGao Xiang { 71447e4937aSGao Xiang struct erofs_sb_info *sbi = NULL; 71547e4937aSGao Xiang blk_status_t err = bio->bi_status; 71647e4937aSGao Xiang struct bio_vec *bvec; 71747e4937aSGao Xiang struct bvec_iter_all iter_all; 71847e4937aSGao Xiang 71947e4937aSGao Xiang bio_for_each_segment_all(bvec, bio, iter_all) { 72047e4937aSGao Xiang struct page *page = bvec->bv_page; 72147e4937aSGao Xiang bool cachemngd = false; 72247e4937aSGao Xiang 72347e4937aSGao Xiang DBG_BUGON(PageUptodate(page)); 72447e4937aSGao Xiang DBG_BUGON(!page->mapping); 72547e4937aSGao Xiang 72647e4937aSGao Xiang if (unlikely(!sbi && !z_erofs_page_is_staging(page))) { 72747e4937aSGao Xiang sbi = EROFS_SB(page->mapping->host->i_sb); 72847e4937aSGao Xiang 72947e4937aSGao Xiang if (time_to_inject(sbi, FAULT_READ_IO)) { 73047e4937aSGao Xiang erofs_show_injection_info(FAULT_READ_IO); 73147e4937aSGao Xiang err = BLK_STS_IOERR; 73247e4937aSGao Xiang } 73347e4937aSGao Xiang } 73447e4937aSGao Xiang 73547e4937aSGao Xiang /* sbi should already be gotten if the page is managed */ 73647e4937aSGao Xiang if (sbi) 73747e4937aSGao Xiang cachemngd = erofs_page_is_managed(sbi, page); 73847e4937aSGao Xiang 73947e4937aSGao Xiang if (unlikely(err)) 74047e4937aSGao Xiang SetPageError(page); 74147e4937aSGao Xiang else if (cachemngd) 74247e4937aSGao Xiang SetPageUptodate(page); 74347e4937aSGao Xiang 74447e4937aSGao Xiang if (cachemngd) 74547e4937aSGao Xiang unlock_page(page); 74647e4937aSGao Xiang } 74747e4937aSGao Xiang 74847e4937aSGao Xiang z_erofs_vle_unzip_kickoff(bio->bi_private, -1); 74947e4937aSGao Xiang bio_put(bio); 75047e4937aSGao Xiang } 75147e4937aSGao Xiang 75247e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb, 75347e4937aSGao Xiang struct z_erofs_pcluster *pcl, 75447e4937aSGao Xiang struct list_head *pagepool) 75547e4937aSGao Xiang { 75647e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(sb); 75747e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 75847e4937aSGao Xiang struct z_erofs_pagevec_ctor ctor; 75947e4937aSGao Xiang unsigned int i, outputsize, llen, nr_pages; 76047e4937aSGao Xiang struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES]; 76147e4937aSGao Xiang struct page **pages, **compressed_pages, *page; 76247e4937aSGao Xiang 76347e4937aSGao Xiang enum z_erofs_page_type page_type; 76447e4937aSGao Xiang bool overlapped, partial; 76547e4937aSGao Xiang struct z_erofs_collection *cl; 76647e4937aSGao Xiang int err; 76747e4937aSGao Xiang 76847e4937aSGao Xiang might_sleep(); 76947e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 77047e4937aSGao Xiang DBG_BUGON(!READ_ONCE(cl->nr_pages)); 77147e4937aSGao Xiang 77247e4937aSGao Xiang mutex_lock(&cl->lock); 77347e4937aSGao Xiang nr_pages = cl->nr_pages; 77447e4937aSGao Xiang 77547e4937aSGao Xiang if (likely(nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES)) { 77647e4937aSGao Xiang pages = pages_onstack; 77747e4937aSGao Xiang } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES && 77847e4937aSGao Xiang mutex_trylock(&z_pagemap_global_lock)) { 77947e4937aSGao Xiang pages = z_pagemap_global; 78047e4937aSGao Xiang } else { 78147e4937aSGao Xiang gfp_t gfp_flags = GFP_KERNEL; 78247e4937aSGao Xiang 78347e4937aSGao Xiang if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES) 78447e4937aSGao Xiang gfp_flags |= __GFP_NOFAIL; 78547e4937aSGao Xiang 78647e4937aSGao Xiang pages = kvmalloc_array(nr_pages, sizeof(struct page *), 78747e4937aSGao Xiang gfp_flags); 78847e4937aSGao Xiang 78947e4937aSGao Xiang /* fallback to global pagemap for the lowmem scenario */ 79047e4937aSGao Xiang if (unlikely(!pages)) { 79147e4937aSGao Xiang mutex_lock(&z_pagemap_global_lock); 79247e4937aSGao Xiang pages = z_pagemap_global; 79347e4937aSGao Xiang } 79447e4937aSGao Xiang } 79547e4937aSGao Xiang 79647e4937aSGao Xiang for (i = 0; i < nr_pages; ++i) 79747e4937aSGao Xiang pages[i] = NULL; 79847e4937aSGao Xiang 79947e4937aSGao Xiang err = 0; 80047e4937aSGao Xiang z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS, 80147e4937aSGao Xiang cl->pagevec, 0); 80247e4937aSGao Xiang 80347e4937aSGao Xiang for (i = 0; i < cl->vcnt; ++i) { 80447e4937aSGao Xiang unsigned int pagenr; 80547e4937aSGao Xiang 80647e4937aSGao Xiang page = z_erofs_pagevec_dequeue(&ctor, &page_type); 80747e4937aSGao Xiang 80847e4937aSGao Xiang /* all pages in pagevec ought to be valid */ 80947e4937aSGao Xiang DBG_BUGON(!page); 81047e4937aSGao Xiang DBG_BUGON(!page->mapping); 81147e4937aSGao Xiang 81247e4937aSGao Xiang if (z_erofs_put_stagingpage(pagepool, page)) 81347e4937aSGao Xiang continue; 81447e4937aSGao Xiang 81547e4937aSGao Xiang if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD) 81647e4937aSGao Xiang pagenr = 0; 81747e4937aSGao Xiang else 81847e4937aSGao Xiang pagenr = z_erofs_onlinepage_index(page); 81947e4937aSGao Xiang 82047e4937aSGao Xiang DBG_BUGON(pagenr >= nr_pages); 82147e4937aSGao Xiang 82247e4937aSGao Xiang /* 82347e4937aSGao Xiang * currently EROFS doesn't support multiref(dedup), 82447e4937aSGao Xiang * so here erroring out one multiref page. 82547e4937aSGao Xiang */ 82647e4937aSGao Xiang if (unlikely(pages[pagenr])) { 82747e4937aSGao Xiang DBG_BUGON(1); 82847e4937aSGao Xiang SetPageError(pages[pagenr]); 82947e4937aSGao Xiang z_erofs_onlinepage_endio(pages[pagenr]); 83047e4937aSGao Xiang err = -EFSCORRUPTED; 83147e4937aSGao Xiang } 83247e4937aSGao Xiang pages[pagenr] = page; 83347e4937aSGao Xiang } 83447e4937aSGao Xiang z_erofs_pagevec_ctor_exit(&ctor, true); 83547e4937aSGao Xiang 83647e4937aSGao Xiang overlapped = false; 83747e4937aSGao Xiang compressed_pages = pcl->compressed_pages; 83847e4937aSGao Xiang 83947e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 84047e4937aSGao Xiang unsigned int pagenr; 84147e4937aSGao Xiang 84247e4937aSGao Xiang page = compressed_pages[i]; 84347e4937aSGao Xiang 84447e4937aSGao Xiang /* all compressed pages ought to be valid */ 84547e4937aSGao Xiang DBG_BUGON(!page); 84647e4937aSGao Xiang DBG_BUGON(!page->mapping); 84747e4937aSGao Xiang 84847e4937aSGao Xiang if (!z_erofs_page_is_staging(page)) { 84947e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) { 85047e4937aSGao Xiang if (unlikely(!PageUptodate(page))) 85147e4937aSGao Xiang err = -EIO; 85247e4937aSGao Xiang continue; 85347e4937aSGao Xiang } 85447e4937aSGao Xiang 85547e4937aSGao Xiang /* 85647e4937aSGao Xiang * only if non-head page can be selected 85747e4937aSGao Xiang * for inplace decompression 85847e4937aSGao Xiang */ 85947e4937aSGao Xiang pagenr = z_erofs_onlinepage_index(page); 86047e4937aSGao Xiang 86147e4937aSGao Xiang DBG_BUGON(pagenr >= nr_pages); 86247e4937aSGao Xiang if (unlikely(pages[pagenr])) { 86347e4937aSGao Xiang DBG_BUGON(1); 86447e4937aSGao Xiang SetPageError(pages[pagenr]); 86547e4937aSGao Xiang z_erofs_onlinepage_endio(pages[pagenr]); 86647e4937aSGao Xiang err = -EFSCORRUPTED; 86747e4937aSGao Xiang } 86847e4937aSGao Xiang pages[pagenr] = page; 86947e4937aSGao Xiang 87047e4937aSGao Xiang overlapped = true; 87147e4937aSGao Xiang } 87247e4937aSGao Xiang 87347e4937aSGao Xiang /* PG_error needs checking for inplaced and staging pages */ 87447e4937aSGao Xiang if (unlikely(PageError(page))) { 87547e4937aSGao Xiang DBG_BUGON(PageUptodate(page)); 87647e4937aSGao Xiang err = -EIO; 87747e4937aSGao Xiang } 87847e4937aSGao Xiang } 87947e4937aSGao Xiang 88047e4937aSGao Xiang if (unlikely(err)) 88147e4937aSGao Xiang goto out; 88247e4937aSGao Xiang 88347e4937aSGao Xiang llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT; 88447e4937aSGao Xiang if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) { 88547e4937aSGao Xiang outputsize = llen; 88647e4937aSGao Xiang partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH); 88747e4937aSGao Xiang } else { 88847e4937aSGao Xiang outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs; 88947e4937aSGao Xiang partial = true; 89047e4937aSGao Xiang } 89147e4937aSGao Xiang 89247e4937aSGao Xiang err = z_erofs_decompress(&(struct z_erofs_decompress_req) { 89347e4937aSGao Xiang .sb = sb, 89447e4937aSGao Xiang .in = compressed_pages, 89547e4937aSGao Xiang .out = pages, 89647e4937aSGao Xiang .pageofs_out = cl->pageofs, 89747e4937aSGao Xiang .inputsize = PAGE_SIZE, 89847e4937aSGao Xiang .outputsize = outputsize, 89947e4937aSGao Xiang .alg = pcl->algorithmformat, 90047e4937aSGao Xiang .inplace_io = overlapped, 90147e4937aSGao Xiang .partial_decoding = partial 90247e4937aSGao Xiang }, pagepool); 90347e4937aSGao Xiang 90447e4937aSGao Xiang out: 90547e4937aSGao Xiang /* must handle all compressed pages before endding pages */ 90647e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 90747e4937aSGao Xiang page = compressed_pages[i]; 90847e4937aSGao Xiang 90947e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) 91047e4937aSGao Xiang continue; 91147e4937aSGao Xiang 91247e4937aSGao Xiang /* recycle all individual staging pages */ 91347e4937aSGao Xiang (void)z_erofs_put_stagingpage(pagepool, page); 91447e4937aSGao Xiang 91547e4937aSGao Xiang WRITE_ONCE(compressed_pages[i], NULL); 91647e4937aSGao Xiang } 91747e4937aSGao Xiang 91847e4937aSGao Xiang for (i = 0; i < nr_pages; ++i) { 91947e4937aSGao Xiang page = pages[i]; 92047e4937aSGao Xiang if (!page) 92147e4937aSGao Xiang continue; 92247e4937aSGao Xiang 92347e4937aSGao Xiang DBG_BUGON(!page->mapping); 92447e4937aSGao Xiang 92547e4937aSGao Xiang /* recycle all individual staging pages */ 92647e4937aSGao Xiang if (z_erofs_put_stagingpage(pagepool, page)) 92747e4937aSGao Xiang continue; 92847e4937aSGao Xiang 92947e4937aSGao Xiang if (unlikely(err < 0)) 93047e4937aSGao Xiang SetPageError(page); 93147e4937aSGao Xiang 93247e4937aSGao Xiang z_erofs_onlinepage_endio(page); 93347e4937aSGao Xiang } 93447e4937aSGao Xiang 93547e4937aSGao Xiang if (pages == z_pagemap_global) 93647e4937aSGao Xiang mutex_unlock(&z_pagemap_global_lock); 93747e4937aSGao Xiang else if (unlikely(pages != pages_onstack)) 93847e4937aSGao Xiang kvfree(pages); 93947e4937aSGao Xiang 94047e4937aSGao Xiang cl->nr_pages = 0; 94147e4937aSGao Xiang cl->vcnt = 0; 94247e4937aSGao Xiang 94347e4937aSGao Xiang /* all cl locks MUST be taken before the following line */ 94447e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 94547e4937aSGao Xiang 94647e4937aSGao Xiang /* all cl locks SHOULD be released right now */ 94747e4937aSGao Xiang mutex_unlock(&cl->lock); 94847e4937aSGao Xiang 94947e4937aSGao Xiang z_erofs_collection_put(cl); 95047e4937aSGao Xiang return err; 95147e4937aSGao Xiang } 95247e4937aSGao Xiang 95347e4937aSGao Xiang static void z_erofs_vle_unzip_all(struct super_block *sb, 95447e4937aSGao Xiang struct z_erofs_unzip_io *io, 95547e4937aSGao Xiang struct list_head *pagepool) 95647e4937aSGao Xiang { 95747e4937aSGao Xiang z_erofs_next_pcluster_t owned = io->head; 95847e4937aSGao Xiang 95947e4937aSGao Xiang while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { 96047e4937aSGao Xiang struct z_erofs_pcluster *pcl; 96147e4937aSGao Xiang 96247e4937aSGao Xiang /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ 96347e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); 96447e4937aSGao Xiang 96547e4937aSGao Xiang /* no possible that 'owned' equals NULL */ 96647e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 96747e4937aSGao Xiang 96847e4937aSGao Xiang pcl = container_of(owned, struct z_erofs_pcluster, next); 96947e4937aSGao Xiang owned = READ_ONCE(pcl->next); 97047e4937aSGao Xiang 97147e4937aSGao Xiang z_erofs_decompress_pcluster(sb, pcl, pagepool); 97247e4937aSGao Xiang } 97347e4937aSGao Xiang } 97447e4937aSGao Xiang 97547e4937aSGao Xiang static void z_erofs_vle_unzip_wq(struct work_struct *work) 97647e4937aSGao Xiang { 97747e4937aSGao Xiang struct z_erofs_unzip_io_sb *iosb = 97847e4937aSGao Xiang container_of(work, struct z_erofs_unzip_io_sb, io.u.work); 97947e4937aSGao Xiang LIST_HEAD(pagepool); 98047e4937aSGao Xiang 98147e4937aSGao Xiang DBG_BUGON(iosb->io.head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 98247e4937aSGao Xiang z_erofs_vle_unzip_all(iosb->sb, &iosb->io, &pagepool); 98347e4937aSGao Xiang 98447e4937aSGao Xiang put_pages_list(&pagepool); 98547e4937aSGao Xiang kvfree(iosb); 98647e4937aSGao Xiang } 98747e4937aSGao Xiang 98847e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 98947e4937aSGao Xiang unsigned int nr, 99047e4937aSGao Xiang struct list_head *pagepool, 99147e4937aSGao Xiang struct address_space *mc, 99247e4937aSGao Xiang gfp_t gfp) 99347e4937aSGao Xiang { 99447e4937aSGao Xiang /* determined at compile time to avoid too many #ifdefs */ 99547e4937aSGao Xiang const bool nocache = __builtin_constant_p(mc) ? !mc : false; 99647e4937aSGao Xiang const pgoff_t index = pcl->obj.index; 99747e4937aSGao Xiang bool tocache = false; 99847e4937aSGao Xiang 99947e4937aSGao Xiang struct address_space *mapping; 100047e4937aSGao Xiang struct page *oldpage, *page; 100147e4937aSGao Xiang 100247e4937aSGao Xiang compressed_page_t t; 100347e4937aSGao Xiang int justfound; 100447e4937aSGao Xiang 100547e4937aSGao Xiang repeat: 100647e4937aSGao Xiang page = READ_ONCE(pcl->compressed_pages[nr]); 100747e4937aSGao Xiang oldpage = page; 100847e4937aSGao Xiang 100947e4937aSGao Xiang if (!page) 101047e4937aSGao Xiang goto out_allocpage; 101147e4937aSGao Xiang 101247e4937aSGao Xiang /* 101347e4937aSGao Xiang * the cached page has not been allocated and 101447e4937aSGao Xiang * an placeholder is out there, prepare it now. 101547e4937aSGao Xiang */ 101647e4937aSGao Xiang if (!nocache && page == PAGE_UNALLOCATED) { 101747e4937aSGao Xiang tocache = true; 101847e4937aSGao Xiang goto out_allocpage; 101947e4937aSGao Xiang } 102047e4937aSGao Xiang 102147e4937aSGao Xiang /* process the target tagged pointer */ 102247e4937aSGao Xiang t = tagptr_init(compressed_page_t, page); 102347e4937aSGao Xiang justfound = tagptr_unfold_tags(t); 102447e4937aSGao Xiang page = tagptr_unfold_ptr(t); 102547e4937aSGao Xiang 102647e4937aSGao Xiang mapping = READ_ONCE(page->mapping); 102747e4937aSGao Xiang 102847e4937aSGao Xiang /* 102947e4937aSGao Xiang * if managed cache is disabled, it's no way to 103047e4937aSGao Xiang * get such a cached-like page. 103147e4937aSGao Xiang */ 103247e4937aSGao Xiang if (nocache) { 103347e4937aSGao Xiang /* if managed cache is disabled, it is impossible `justfound' */ 103447e4937aSGao Xiang DBG_BUGON(justfound); 103547e4937aSGao Xiang 103647e4937aSGao Xiang /* and it should be locked, not uptodate, and not truncated */ 103747e4937aSGao Xiang DBG_BUGON(!PageLocked(page)); 103847e4937aSGao Xiang DBG_BUGON(PageUptodate(page)); 103947e4937aSGao Xiang DBG_BUGON(!mapping); 104047e4937aSGao Xiang goto out; 104147e4937aSGao Xiang } 104247e4937aSGao Xiang 104347e4937aSGao Xiang /* 104447e4937aSGao Xiang * unmanaged (file) pages are all locked solidly, 104547e4937aSGao Xiang * therefore it is impossible for `mapping' to be NULL. 104647e4937aSGao Xiang */ 104747e4937aSGao Xiang if (mapping && mapping != mc) 104847e4937aSGao Xiang /* ought to be unmanaged pages */ 104947e4937aSGao Xiang goto out; 105047e4937aSGao Xiang 105147e4937aSGao Xiang lock_page(page); 105247e4937aSGao Xiang 105347e4937aSGao Xiang /* only true if page reclaim goes wrong, should never happen */ 105447e4937aSGao Xiang DBG_BUGON(justfound && PagePrivate(page)); 105547e4937aSGao Xiang 105647e4937aSGao Xiang /* the page is still in manage cache */ 105747e4937aSGao Xiang if (page->mapping == mc) { 105847e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[nr], page); 105947e4937aSGao Xiang 106047e4937aSGao Xiang ClearPageError(page); 106147e4937aSGao Xiang if (!PagePrivate(page)) { 106247e4937aSGao Xiang /* 106347e4937aSGao Xiang * impossible to be !PagePrivate(page) for 106447e4937aSGao Xiang * the current restriction as well if 106547e4937aSGao Xiang * the page is already in compressed_pages[]. 106647e4937aSGao Xiang */ 106747e4937aSGao Xiang DBG_BUGON(!justfound); 106847e4937aSGao Xiang 106947e4937aSGao Xiang justfound = 0; 107047e4937aSGao Xiang set_page_private(page, (unsigned long)pcl); 107147e4937aSGao Xiang SetPagePrivate(page); 107247e4937aSGao Xiang } 107347e4937aSGao Xiang 107447e4937aSGao Xiang /* no need to submit io if it is already up-to-date */ 107547e4937aSGao Xiang if (PageUptodate(page)) { 107647e4937aSGao Xiang unlock_page(page); 107747e4937aSGao Xiang page = NULL; 107847e4937aSGao Xiang } 107947e4937aSGao Xiang goto out; 108047e4937aSGao Xiang } 108147e4937aSGao Xiang 108247e4937aSGao Xiang /* 108347e4937aSGao Xiang * the managed page has been truncated, it's unsafe to 108447e4937aSGao Xiang * reuse this one, let's allocate a new cache-managed page. 108547e4937aSGao Xiang */ 108647e4937aSGao Xiang DBG_BUGON(page->mapping); 108747e4937aSGao Xiang DBG_BUGON(!justfound); 108847e4937aSGao Xiang 108947e4937aSGao Xiang tocache = true; 109047e4937aSGao Xiang unlock_page(page); 109147e4937aSGao Xiang put_page(page); 109247e4937aSGao Xiang out_allocpage: 109347e4937aSGao Xiang page = __stagingpage_alloc(pagepool, gfp); 109447e4937aSGao Xiang if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) { 109547e4937aSGao Xiang list_add(&page->lru, pagepool); 109647e4937aSGao Xiang cpu_relax(); 109747e4937aSGao Xiang goto repeat; 109847e4937aSGao Xiang } 109947e4937aSGao Xiang if (nocache || !tocache) 110047e4937aSGao Xiang goto out; 110147e4937aSGao Xiang if (add_to_page_cache_lru(page, mc, index + nr, gfp)) { 110247e4937aSGao Xiang page->mapping = Z_EROFS_MAPPING_STAGING; 110347e4937aSGao Xiang goto out; 110447e4937aSGao Xiang } 110547e4937aSGao Xiang 110647e4937aSGao Xiang set_page_private(page, (unsigned long)pcl); 110747e4937aSGao Xiang SetPagePrivate(page); 110847e4937aSGao Xiang out: /* the only exit (for tracing and debugging) */ 110947e4937aSGao Xiang return page; 111047e4937aSGao Xiang } 111147e4937aSGao Xiang 111247e4937aSGao Xiang static struct z_erofs_unzip_io *jobqueue_init(struct super_block *sb, 111347e4937aSGao Xiang struct z_erofs_unzip_io *io, 111447e4937aSGao Xiang bool foreground) 111547e4937aSGao Xiang { 111647e4937aSGao Xiang struct z_erofs_unzip_io_sb *iosb; 111747e4937aSGao Xiang 111847e4937aSGao Xiang if (foreground) { 111947e4937aSGao Xiang /* waitqueue available for foreground io */ 112047e4937aSGao Xiang DBG_BUGON(!io); 112147e4937aSGao Xiang 112247e4937aSGao Xiang init_waitqueue_head(&io->u.wait); 112347e4937aSGao Xiang atomic_set(&io->pending_bios, 0); 112447e4937aSGao Xiang goto out; 112547e4937aSGao Xiang } 112647e4937aSGao Xiang 112747e4937aSGao Xiang iosb = kvzalloc(sizeof(*iosb), GFP_KERNEL | __GFP_NOFAIL); 112847e4937aSGao Xiang DBG_BUGON(!iosb); 112947e4937aSGao Xiang 113047e4937aSGao Xiang /* initialize fields in the allocated descriptor */ 113147e4937aSGao Xiang io = &iosb->io; 113247e4937aSGao Xiang iosb->sb = sb; 113347e4937aSGao Xiang INIT_WORK(&io->u.work, z_erofs_vle_unzip_wq); 113447e4937aSGao Xiang out: 113547e4937aSGao Xiang io->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 113647e4937aSGao Xiang return io; 113747e4937aSGao Xiang } 113847e4937aSGao Xiang 113947e4937aSGao Xiang /* define decompression jobqueue types */ 114047e4937aSGao Xiang enum { 114147e4937aSGao Xiang JQ_BYPASS, 114247e4937aSGao Xiang JQ_SUBMIT, 114347e4937aSGao Xiang NR_JOBQUEUES, 114447e4937aSGao Xiang }; 114547e4937aSGao Xiang 114647e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb, 114747e4937aSGao Xiang z_erofs_next_pcluster_t qtail[], 114847e4937aSGao Xiang struct z_erofs_unzip_io *q[], 114947e4937aSGao Xiang struct z_erofs_unzip_io *fgq, 115047e4937aSGao Xiang bool forcefg) 115147e4937aSGao Xiang { 115247e4937aSGao Xiang /* 115347e4937aSGao Xiang * if managed cache is enabled, bypass jobqueue is needed, 115447e4937aSGao Xiang * no need to read from device for all pclusters in this queue. 115547e4937aSGao Xiang */ 115647e4937aSGao Xiang q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, true); 115747e4937aSGao Xiang qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 115847e4937aSGao Xiang 115947e4937aSGao Xiang q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, forcefg); 116047e4937aSGao Xiang qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 116147e4937aSGao Xiang 116247e4937aSGao Xiang return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], !forcefg)); 116347e4937aSGao Xiang } 116447e4937aSGao Xiang 116547e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 116647e4937aSGao Xiang z_erofs_next_pcluster_t qtail[], 116747e4937aSGao Xiang z_erofs_next_pcluster_t owned_head) 116847e4937aSGao Xiang { 116947e4937aSGao Xiang z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 117047e4937aSGao Xiang z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 117147e4937aSGao Xiang 117247e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 117347e4937aSGao Xiang if (owned_head == Z_EROFS_PCLUSTER_TAIL) 117447e4937aSGao Xiang owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 117547e4937aSGao Xiang 117647e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); 117747e4937aSGao Xiang 117847e4937aSGao Xiang WRITE_ONCE(*submit_qtail, owned_head); 117947e4937aSGao Xiang WRITE_ONCE(*bypass_qtail, &pcl->next); 118047e4937aSGao Xiang 118147e4937aSGao Xiang qtail[JQ_BYPASS] = &pcl->next; 118247e4937aSGao Xiang } 118347e4937aSGao Xiang 118447e4937aSGao Xiang static bool postsubmit_is_all_bypassed(struct z_erofs_unzip_io *q[], 118547e4937aSGao Xiang unsigned int nr_bios, 118647e4937aSGao Xiang bool force_fg) 118747e4937aSGao Xiang { 118847e4937aSGao Xiang /* 118947e4937aSGao Xiang * although background is preferred, no one is pending for submission. 119047e4937aSGao Xiang * don't issue workqueue for decompression but drop it directly instead. 119147e4937aSGao Xiang */ 119247e4937aSGao Xiang if (force_fg || nr_bios) 119347e4937aSGao Xiang return false; 119447e4937aSGao Xiang 119547e4937aSGao Xiang kvfree(container_of(q[JQ_SUBMIT], struct z_erofs_unzip_io_sb, io)); 119647e4937aSGao Xiang return true; 119747e4937aSGao Xiang } 119847e4937aSGao Xiang 119947e4937aSGao Xiang static bool z_erofs_vle_submit_all(struct super_block *sb, 120047e4937aSGao Xiang z_erofs_next_pcluster_t owned_head, 120147e4937aSGao Xiang struct list_head *pagepool, 120247e4937aSGao Xiang struct z_erofs_unzip_io *fgq, 120347e4937aSGao Xiang bool force_fg) 120447e4937aSGao Xiang { 120547e4937aSGao Xiang struct erofs_sb_info *const sbi __maybe_unused = EROFS_SB(sb); 120647e4937aSGao Xiang z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 120747e4937aSGao Xiang struct z_erofs_unzip_io *q[NR_JOBQUEUES]; 120847e4937aSGao Xiang struct bio *bio; 120947e4937aSGao Xiang void *bi_private; 121047e4937aSGao Xiang /* since bio will be NULL, no need to initialize last_index */ 121147e4937aSGao Xiang pgoff_t uninitialized_var(last_index); 121247e4937aSGao Xiang bool force_submit = false; 121347e4937aSGao Xiang unsigned int nr_bios; 121447e4937aSGao Xiang 121547e4937aSGao Xiang if (unlikely(owned_head == Z_EROFS_PCLUSTER_TAIL)) 121647e4937aSGao Xiang return false; 121747e4937aSGao Xiang 121847e4937aSGao Xiang force_submit = false; 121947e4937aSGao Xiang bio = NULL; 122047e4937aSGao Xiang nr_bios = 0; 122147e4937aSGao Xiang bi_private = jobqueueset_init(sb, qtail, q, fgq, force_fg); 122247e4937aSGao Xiang 122347e4937aSGao Xiang /* by default, all need io submission */ 122447e4937aSGao Xiang q[JQ_SUBMIT]->head = owned_head; 122547e4937aSGao Xiang 122647e4937aSGao Xiang do { 122747e4937aSGao Xiang struct z_erofs_pcluster *pcl; 122847e4937aSGao Xiang unsigned int clusterpages; 122947e4937aSGao Xiang pgoff_t first_index; 123047e4937aSGao Xiang struct page *page; 123147e4937aSGao Xiang unsigned int i = 0, bypass = 0; 123247e4937aSGao Xiang int err; 123347e4937aSGao Xiang 123447e4937aSGao Xiang /* no possible 'owned_head' equals the following */ 123547e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 123647e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 123747e4937aSGao Xiang 123847e4937aSGao Xiang pcl = container_of(owned_head, struct z_erofs_pcluster, next); 123947e4937aSGao Xiang 124047e4937aSGao Xiang clusterpages = BIT(pcl->clusterbits); 124147e4937aSGao Xiang 124247e4937aSGao Xiang /* close the main owned chain at first */ 124347e4937aSGao Xiang owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 124447e4937aSGao Xiang Z_EROFS_PCLUSTER_TAIL_CLOSED); 124547e4937aSGao Xiang 124647e4937aSGao Xiang first_index = pcl->obj.index; 124747e4937aSGao Xiang force_submit |= (first_index != last_index + 1); 124847e4937aSGao Xiang 124947e4937aSGao Xiang repeat: 125047e4937aSGao Xiang page = pickup_page_for_submission(pcl, i, pagepool, 125147e4937aSGao Xiang MNGD_MAPPING(sbi), 125247e4937aSGao Xiang GFP_NOFS); 125347e4937aSGao Xiang if (!page) { 125447e4937aSGao Xiang force_submit = true; 125547e4937aSGao Xiang ++bypass; 125647e4937aSGao Xiang goto skippage; 125747e4937aSGao Xiang } 125847e4937aSGao Xiang 125947e4937aSGao Xiang if (bio && force_submit) { 126047e4937aSGao Xiang submit_bio_retry: 126147e4937aSGao Xiang __submit_bio(bio, REQ_OP_READ, 0); 126247e4937aSGao Xiang bio = NULL; 126347e4937aSGao Xiang } 126447e4937aSGao Xiang 126547e4937aSGao Xiang if (!bio) { 126647e4937aSGao Xiang bio = erofs_grab_bio(sb, first_index + i, 126747e4937aSGao Xiang BIO_MAX_PAGES, bi_private, 126847e4937aSGao Xiang z_erofs_vle_read_endio, true); 126947e4937aSGao Xiang ++nr_bios; 127047e4937aSGao Xiang } 127147e4937aSGao Xiang 127247e4937aSGao Xiang err = bio_add_page(bio, page, PAGE_SIZE, 0); 127347e4937aSGao Xiang if (err < PAGE_SIZE) 127447e4937aSGao Xiang goto submit_bio_retry; 127547e4937aSGao Xiang 127647e4937aSGao Xiang force_submit = false; 127747e4937aSGao Xiang last_index = first_index + i; 127847e4937aSGao Xiang skippage: 127947e4937aSGao Xiang if (++i < clusterpages) 128047e4937aSGao Xiang goto repeat; 128147e4937aSGao Xiang 128247e4937aSGao Xiang if (bypass < clusterpages) 128347e4937aSGao Xiang qtail[JQ_SUBMIT] = &pcl->next; 128447e4937aSGao Xiang else 128547e4937aSGao Xiang move_to_bypass_jobqueue(pcl, qtail, owned_head); 128647e4937aSGao Xiang } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 128747e4937aSGao Xiang 128847e4937aSGao Xiang if (bio) 128947e4937aSGao Xiang __submit_bio(bio, REQ_OP_READ, 0); 129047e4937aSGao Xiang 129147e4937aSGao Xiang if (postsubmit_is_all_bypassed(q, nr_bios, force_fg)) 129247e4937aSGao Xiang return true; 129347e4937aSGao Xiang 129447e4937aSGao Xiang z_erofs_vle_unzip_kickoff(bi_private, nr_bios); 129547e4937aSGao Xiang return true; 129647e4937aSGao Xiang } 129747e4937aSGao Xiang 129847e4937aSGao Xiang static void z_erofs_submit_and_unzip(struct super_block *sb, 129947e4937aSGao Xiang struct z_erofs_collector *clt, 130047e4937aSGao Xiang struct list_head *pagepool, 130147e4937aSGao Xiang bool force_fg) 130247e4937aSGao Xiang { 130347e4937aSGao Xiang struct z_erofs_unzip_io io[NR_JOBQUEUES]; 130447e4937aSGao Xiang 130547e4937aSGao Xiang if (!z_erofs_vle_submit_all(sb, clt->owned_head, 130647e4937aSGao Xiang pagepool, io, force_fg)) 130747e4937aSGao Xiang return; 130847e4937aSGao Xiang 130947e4937aSGao Xiang /* decompress no I/O pclusters immediately */ 131047e4937aSGao Xiang z_erofs_vle_unzip_all(sb, &io[JQ_BYPASS], pagepool); 131147e4937aSGao Xiang 131247e4937aSGao Xiang if (!force_fg) 131347e4937aSGao Xiang return; 131447e4937aSGao Xiang 131547e4937aSGao Xiang /* wait until all bios are completed */ 131647e4937aSGao Xiang wait_event(io[JQ_SUBMIT].u.wait, 131747e4937aSGao Xiang !atomic_read(&io[JQ_SUBMIT].pending_bios)); 131847e4937aSGao Xiang 131947e4937aSGao Xiang /* let's synchronous decompression */ 132047e4937aSGao Xiang z_erofs_vle_unzip_all(sb, &io[JQ_SUBMIT], pagepool); 132147e4937aSGao Xiang } 132247e4937aSGao Xiang 132347e4937aSGao Xiang static int z_erofs_vle_normalaccess_readpage(struct file *file, 132447e4937aSGao Xiang struct page *page) 132547e4937aSGao Xiang { 132647e4937aSGao Xiang struct inode *const inode = page->mapping->host; 132747e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 132847e4937aSGao Xiang int err; 132947e4937aSGao Xiang LIST_HEAD(pagepool); 133047e4937aSGao Xiang 133147e4937aSGao Xiang trace_erofs_readpage(page, false); 133247e4937aSGao Xiang 133347e4937aSGao Xiang f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 133447e4937aSGao Xiang 133547e4937aSGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 133647e4937aSGao Xiang (void)z_erofs_collector_end(&f.clt); 133747e4937aSGao Xiang 133847e4937aSGao Xiang /* if some compressed cluster ready, need submit them anyway */ 133947e4937aSGao Xiang z_erofs_submit_and_unzip(inode->i_sb, &f.clt, &pagepool, true); 134047e4937aSGao Xiang 134147e4937aSGao Xiang if (err) 134247e4937aSGao Xiang errln("%s, failed to read, err [%d]", __func__, err); 134347e4937aSGao Xiang 134447e4937aSGao Xiang if (f.map.mpage) 134547e4937aSGao Xiang put_page(f.map.mpage); 134647e4937aSGao Xiang 134747e4937aSGao Xiang /* clean up the remaining free pages */ 134847e4937aSGao Xiang put_pages_list(&pagepool); 134947e4937aSGao Xiang return err; 135047e4937aSGao Xiang } 135147e4937aSGao Xiang 135247e4937aSGao Xiang static bool should_decompress_synchronously(struct erofs_sb_info *sbi, 135347e4937aSGao Xiang unsigned int nr) 135447e4937aSGao Xiang { 135547e4937aSGao Xiang return nr <= sbi->max_sync_decompress_pages; 135647e4937aSGao Xiang } 135747e4937aSGao Xiang 135847e4937aSGao Xiang static int z_erofs_vle_normalaccess_readpages(struct file *filp, 135947e4937aSGao Xiang struct address_space *mapping, 136047e4937aSGao Xiang struct list_head *pages, 136147e4937aSGao Xiang unsigned int nr_pages) 136247e4937aSGao Xiang { 136347e4937aSGao Xiang struct inode *const inode = mapping->host; 136447e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 136547e4937aSGao Xiang 136647e4937aSGao Xiang bool sync = should_decompress_synchronously(sbi, nr_pages); 136747e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 136847e4937aSGao Xiang gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL); 136947e4937aSGao Xiang struct page *head = NULL; 137047e4937aSGao Xiang LIST_HEAD(pagepool); 137147e4937aSGao Xiang 137247e4937aSGao Xiang trace_erofs_readpages(mapping->host, lru_to_page(pages), 137347e4937aSGao Xiang nr_pages, false); 137447e4937aSGao Xiang 137547e4937aSGao Xiang f.headoffset = (erofs_off_t)lru_to_page(pages)->index << PAGE_SHIFT; 137647e4937aSGao Xiang 137747e4937aSGao Xiang for (; nr_pages; --nr_pages) { 137847e4937aSGao Xiang struct page *page = lru_to_page(pages); 137947e4937aSGao Xiang 138047e4937aSGao Xiang prefetchw(&page->flags); 138147e4937aSGao Xiang list_del(&page->lru); 138247e4937aSGao Xiang 138347e4937aSGao Xiang /* 138447e4937aSGao Xiang * A pure asynchronous readahead is indicated if 138547e4937aSGao Xiang * a PG_readahead marked page is hitted at first. 138647e4937aSGao Xiang * Let's also do asynchronous decompression for this case. 138747e4937aSGao Xiang */ 138847e4937aSGao Xiang sync &= !(PageReadahead(page) && !head); 138947e4937aSGao Xiang 139047e4937aSGao Xiang if (add_to_page_cache_lru(page, mapping, page->index, gfp)) { 139147e4937aSGao Xiang list_add(&page->lru, &pagepool); 139247e4937aSGao Xiang continue; 139347e4937aSGao Xiang } 139447e4937aSGao Xiang 139547e4937aSGao Xiang set_page_private(page, (unsigned long)head); 139647e4937aSGao Xiang head = page; 139747e4937aSGao Xiang } 139847e4937aSGao Xiang 139947e4937aSGao Xiang while (head) { 140047e4937aSGao Xiang struct page *page = head; 140147e4937aSGao Xiang int err; 140247e4937aSGao Xiang 140347e4937aSGao Xiang /* traversal in reverse order */ 140447e4937aSGao Xiang head = (void *)page_private(page); 140547e4937aSGao Xiang 140647e4937aSGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 140747e4937aSGao Xiang if (err) { 140847e4937aSGao Xiang struct erofs_vnode *vi = EROFS_V(inode); 140947e4937aSGao Xiang 141047e4937aSGao Xiang errln("%s, readahead error at page %lu of nid %llu", 141147e4937aSGao Xiang __func__, page->index, vi->nid); 141247e4937aSGao Xiang } 141347e4937aSGao Xiang put_page(page); 141447e4937aSGao Xiang } 141547e4937aSGao Xiang 141647e4937aSGao Xiang (void)z_erofs_collector_end(&f.clt); 141747e4937aSGao Xiang 141847e4937aSGao Xiang z_erofs_submit_and_unzip(inode->i_sb, &f.clt, &pagepool, sync); 141947e4937aSGao Xiang 142047e4937aSGao Xiang if (f.map.mpage) 142147e4937aSGao Xiang put_page(f.map.mpage); 142247e4937aSGao Xiang 142347e4937aSGao Xiang /* clean up the remaining free pages */ 142447e4937aSGao Xiang put_pages_list(&pagepool); 142547e4937aSGao Xiang return 0; 142647e4937aSGao Xiang } 142747e4937aSGao Xiang 142847e4937aSGao Xiang const struct address_space_operations z_erofs_vle_normalaccess_aops = { 142947e4937aSGao Xiang .readpage = z_erofs_vle_normalaccess_readpage, 143047e4937aSGao Xiang .readpages = z_erofs_vle_normalaccess_readpages, 143147e4937aSGao Xiang }; 143247e4937aSGao Xiang 1433