Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | 02827e1 | 2018-07-26 20:21:58 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | 02827e1 | 2018-07-26 20:21:58 +0800 | [diff] [blame] | 3 | * Copyright (C) 2018 HUAWEI, Inc. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | 02827e1 | 2018-07-26 20:21:58 +0800 | [diff] [blame] | 5 | * Created by Gao Xiang <gaoxiang25@huawei.com> |
Gao Xiang | 02827e1 | 2018-07-26 20:21:58 +0800 | [diff] [blame] | 6 | */ |
Gao Xiang | 57b78c9 | 2019-07-31 23:57:32 +0800 | [diff] [blame] | 7 | #include "zdata.h" |
Gao Xiang | 2748123 | 2019-06-24 15:22:54 +0800 | [diff] [blame] | 8 | #include "compress.h" |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 9 | #include <linux/prefetch.h> |
| 10 | |
Chen Gong | 284db12 | 2018-09-18 22:27:27 +0800 | [diff] [blame] | 11 | #include <trace/events/erofs.h> |
| 12 | |
Gao Xiang | 672e547 | 2018-12-08 00:19:14 +0800 | [diff] [blame] | 13 | /* |
| 14 | * a compressed_pages[] placeholder in order to avoid |
| 15 | * being filled with file pages for in-place decompression. |
| 16 | */ |
| 17 | #define PAGE_UNALLOCATED ((void *)0x5F0E4B1D) |
| 18 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 19 | /* how to allocate cached pages for a pcluster */ |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 20 | enum z_erofs_cache_alloctype { |
| 21 | DONTALLOC, /* don't allocate any cached pages */ |
| 22 | DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */ |
| 23 | }; |
| 24 | |
| 25 | /* |
| 26 | * tagged pointer with 1-bit tag for all compressed pages |
| 27 | * tag 0 - the page is just found with an extra page reference |
| 28 | */ |
| 29 | typedef tagptr1_t compressed_page_t; |
| 30 | |
| 31 | #define tag_compressed_page_justfound(page) \ |
| 32 | tagptr_fold(compressed_page_t, page, 1) |
| 33 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 34 | static struct workqueue_struct *z_erofs_workqueue __read_mostly; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 35 | static struct kmem_cache *pcluster_cachep __read_mostly; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 36 | |
| 37 | void z_erofs_exit_zip_subsystem(void) |
| 38 | { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 39 | destroy_workqueue(z_erofs_workqueue); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 40 | kmem_cache_destroy(pcluster_cachep); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 41 | } |
| 42 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 43 | static inline int z_erofs_init_workqueue(void) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 44 | { |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 45 | const unsigned int onlinecpus = num_possible_cpus(); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 46 | |
| 47 | /* |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 48 | * no need to spawn too many threads, limiting threads could minimum |
| 49 | * scheduling overhead, perhaps per-CPU threads should be better? |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 50 | */ |
Gao Xiang | 0e62ea3 | 2020-07-31 10:40:49 +0800 | [diff] [blame] | 51 | z_erofs_workqueue = alloc_workqueue("erofs_unzipd", |
| 52 | WQ_UNBOUND | WQ_HIGHPRI, |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 53 | onlinecpus + onlinecpus / 4); |
Cristian Sicilia | 42d40b4 | 2018-11-12 21:43:57 +0100 | [diff] [blame] | 54 | return z_erofs_workqueue ? 0 : -ENOMEM; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 55 | } |
| 56 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 57 | static void z_erofs_pcluster_init_once(void *ptr) |
Gao Xiang | 48d4bf3 | 2018-11-23 01:21:46 +0800 | [diff] [blame] | 58 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 59 | struct z_erofs_pcluster *pcl = ptr; |
| 60 | struct z_erofs_collection *cl = z_erofs_primarycollection(pcl); |
Gao Xiang | 48d4bf3 | 2018-11-23 01:21:46 +0800 | [diff] [blame] | 61 | unsigned int i; |
| 62 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 63 | mutex_init(&cl->lock); |
| 64 | cl->nr_pages = 0; |
| 65 | cl->vcnt = 0; |
Gao Xiang | 48d4bf3 | 2018-11-23 01:21:46 +0800 | [diff] [blame] | 66 | for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i) |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 67 | pcl->compressed_pages[i] = NULL; |
Gao Xiang | 48d4bf3 | 2018-11-23 01:21:46 +0800 | [diff] [blame] | 68 | } |
| 69 | |
Gao Xiang | 0a0b7e6 | 2018-10-09 21:43:53 +0800 | [diff] [blame] | 70 | int __init z_erofs_init_zip_subsystem(void) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 71 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 72 | pcluster_cachep = kmem_cache_create("erofs_compress", |
| 73 | Z_EROFS_WORKGROUP_SIZE, 0, |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 74 | SLAB_RECLAIM_ACCOUNT, |
| 75 | z_erofs_pcluster_init_once); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 76 | if (pcluster_cachep) { |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 77 | if (!z_erofs_init_workqueue()) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 78 | return 0; |
| 79 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 80 | kmem_cache_destroy(pcluster_cachep); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 81 | } |
| 82 | return -ENOMEM; |
| 83 | } |
| 84 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 85 | enum z_erofs_collectmode { |
| 86 | COLLECT_SECONDARY, |
| 87 | COLLECT_PRIMARY, |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 88 | /* |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 89 | * The current collection was the tail of an exist chain, in addition |
| 90 | * that the previous processed chained collections are all decided to |
| 91 | * be hooked up to it. |
| 92 | * A new chain will be created for the remaining collections which are |
| 93 | * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED, |
| 94 | * the next collection cannot reuse the whole page safely in |
| 95 | * the following scenario: |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 96 | * ________________________________________________________________ |
| 97 | * | tail (partial) page | head (partial) page | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 98 | * | (belongs to the next cl) | (belongs to the current cl) | |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 99 | * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________| |
| 100 | */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 101 | COLLECT_PRIMARY_HOOKED, |
| 102 | COLLECT_PRIMARY_FOLLOWED_NOINPLACE, |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 103 | /* |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 104 | * The current collection has been linked with the owned chain, and |
| 105 | * could also be linked with the remaining collections, which means |
| 106 | * if the processing page is the tail page of the collection, thus |
| 107 | * the current collection can safely use the whole page (since |
| 108 | * the previous collection is under control) for in-place I/O, as |
| 109 | * illustrated below: |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 110 | * ________________________________________________________________ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 111 | * | tail (partial) page | head (partial) page | |
| 112 | * | (of the current cl) | (of the previous collection) | |
| 113 | * | PRIMARY_FOLLOWED or | | |
| 114 | * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________| |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 115 | * |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 116 | * [ (*) the above page can be used as inplace I/O. ] |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 117 | */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 118 | COLLECT_PRIMARY_FOLLOWED, |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 119 | }; |
| 120 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 121 | struct z_erofs_collector { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 122 | struct z_erofs_pagevec_ctor vector; |
| 123 | |
Gao Xiang | bfc4ccb | 2019-08-21 11:09:08 +0800 | [diff] [blame] | 124 | struct z_erofs_pcluster *pcl, *tailpcl; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 125 | struct z_erofs_collection *cl; |
| 126 | struct page **compressedpages; |
| 127 | z_erofs_next_pcluster_t owned_head; |
| 128 | |
| 129 | enum z_erofs_collectmode mode; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 130 | }; |
| 131 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 132 | struct z_erofs_decompress_frontend { |
| 133 | struct inode *const inode; |
| 134 | |
| 135 | struct z_erofs_collector clt; |
| 136 | struct erofs_map_blocks map; |
| 137 | |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 138 | bool readahead; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 139 | /* used for applying cache strategy on the fly */ |
| 140 | bool backmost; |
| 141 | erofs_off_t headoffset; |
| 142 | }; |
| 143 | |
| 144 | #define COLLECTOR_INIT() { \ |
| 145 | .owned_head = Z_EROFS_PCLUSTER_TAIL, \ |
| 146 | .mode = COLLECT_PRIMARY_FOLLOWED } |
| 147 | |
| 148 | #define DECOMPRESS_FRONTEND_INIT(__i) { \ |
| 149 | .inode = __i, .clt = COLLECTOR_INIT(), \ |
| 150 | .backmost = true, } |
| 151 | |
| 152 | static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES]; |
| 153 | static DEFINE_MUTEX(z_pagemap_global_lock); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 154 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 155 | static void preload_compressed_pages(struct z_erofs_collector *clt, |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 156 | struct address_space *mc, |
Chao Yu | e3f78d5 | 2020-09-17 09:18:21 +0800 | [diff] [blame] | 157 | enum z_erofs_cache_alloctype type) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 158 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 159 | const struct z_erofs_pcluster *pcl = clt->pcl; |
| 160 | const unsigned int clusterpages = BIT(pcl->clusterbits); |
| 161 | struct page **pages = clt->compressedpages; |
| 162 | pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages); |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 163 | bool standalone = true; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 164 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 165 | if (clt->mode < COLLECT_PRIMARY_FOLLOWED) |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 166 | return; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 167 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 168 | for (; pages < pcl->compressed_pages + clusterpages; ++pages) { |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 169 | struct page *page; |
| 170 | compressed_page_t t; |
| 171 | |
| 172 | /* the compressed page was loaded before */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 173 | if (READ_ONCE(*pages)) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 174 | continue; |
| 175 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 176 | page = find_get_page(mc, index); |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 177 | |
| 178 | if (page) { |
| 179 | t = tag_compressed_page_justfound(page); |
| 180 | } else if (type == DELAYEDALLOC) { |
| 181 | t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED); |
| 182 | } else { /* DONTALLOC */ |
| 183 | if (standalone) |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 184 | clt->compressedpages = pages; |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 185 | standalone = false; |
| 186 | continue; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 187 | } |
| 188 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 189 | if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t))) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 190 | continue; |
| 191 | |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 192 | if (page) |
| 193 | put_page(page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 194 | } |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 195 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 196 | if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */ |
| 197 | clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* called by erofs_shrinker to get rid of all compressed_pages */ |
Gao Xiang | 47e541a | 2018-07-29 13:34:58 +0800 | [diff] [blame] | 201 | int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 202 | struct erofs_workgroup *grp) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 203 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 204 | struct z_erofs_pcluster *const pcl = |
| 205 | container_of(grp, struct z_erofs_pcluster, obj); |
Gao Xiang | c1448fa | 2018-12-08 00:19:13 +0800 | [diff] [blame] | 206 | struct address_space *const mapping = MNGD_MAPPING(sbi); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 207 | const unsigned int clusterpages = BIT(pcl->clusterbits); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 208 | int i; |
| 209 | |
| 210 | /* |
| 211 | * refcount of workgroup is now freezed as 1, |
| 212 | * therefore no need to worry about available decompression users. |
| 213 | */ |
| 214 | for (i = 0; i < clusterpages; ++i) { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 215 | struct page *page = pcl->compressed_pages[i]; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 216 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 217 | if (!page) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 218 | continue; |
| 219 | |
| 220 | /* block other users from reclaiming or migrating the page */ |
| 221 | if (!trylock_page(page)) |
| 222 | return -EBUSY; |
| 223 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 224 | if (page->mapping != mapping) |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 225 | continue; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 226 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 227 | /* barrier is implied in the following 'unlock_page' */ |
| 228 | WRITE_ONCE(pcl->compressed_pages[i], NULL); |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 229 | detach_page_private(page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 230 | unlock_page(page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 231 | } |
| 232 | return 0; |
| 233 | } |
| 234 | |
Gao Xiang | 47e541a | 2018-07-29 13:34:58 +0800 | [diff] [blame] | 235 | int erofs_try_to_free_cached_page(struct address_space *mapping, |
| 236 | struct page *page) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 237 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 238 | struct z_erofs_pcluster *const pcl = (void *)page_private(page); |
| 239 | const unsigned int clusterpages = BIT(pcl->clusterbits); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 240 | int ret = 0; /* 0 - busy */ |
| 241 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 242 | if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) { |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 243 | unsigned int i; |
| 244 | |
| 245 | for (i = 0; i < clusterpages; ++i) { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 246 | if (pcl->compressed_pages[i] == page) { |
| 247 | WRITE_ONCE(pcl->compressed_pages[i], NULL); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 248 | ret = 1; |
| 249 | break; |
| 250 | } |
| 251 | } |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 252 | erofs_workgroup_unfreeze(&pcl->obj, 1); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 253 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 254 | if (ret) |
| 255 | detach_page_private(page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 256 | } |
| 257 | return ret; |
| 258 | } |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 259 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 260 | /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */ |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 261 | static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt, |
| 262 | struct page *page) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 263 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 264 | struct z_erofs_pcluster *const pcl = clt->pcl; |
| 265 | const unsigned int clusterpages = BIT(pcl->clusterbits); |
| 266 | |
| 267 | while (clt->compressedpages < pcl->compressed_pages + clusterpages) { |
| 268 | if (!cmpxchg(clt->compressedpages++, NULL, page)) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 269 | return true; |
| 270 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 274 | /* callers must be with collection lock held */ |
| 275 | static int z_erofs_attach_page(struct z_erofs_collector *clt, |
| 276 | struct page *page, |
| 277 | enum z_erofs_page_type type) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 278 | { |
| 279 | int ret; |
| 280 | bool occupied; |
| 281 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 282 | /* give priority for inplaceio */ |
| 283 | if (clt->mode >= COLLECT_PRIMARY && |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 284 | type == Z_EROFS_PAGE_TYPE_EXCLUSIVE && |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 285 | z_erofs_try_inplace_io(clt, page)) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 286 | return 0; |
| 287 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 288 | ret = z_erofs_pagevec_enqueue(&clt->vector, |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 289 | page, type, &occupied); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 290 | clt->cl->vcnt += (unsigned int)ret; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 291 | |
| 292 | return ret ? 0 : -EAGAIN; |
| 293 | } |
| 294 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 295 | static enum z_erofs_collectmode |
| 296 | try_to_claim_pcluster(struct z_erofs_pcluster *pcl, |
| 297 | z_erofs_next_pcluster_t *owned_head) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 298 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 299 | /* let's claim these following types of pclusters */ |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 300 | retry: |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 301 | if (pcl->next == Z_EROFS_PCLUSTER_NIL) { |
| 302 | /* type 1, nil pcluster */ |
| 303 | if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, |
| 304 | *owned_head) != Z_EROFS_PCLUSTER_NIL) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 305 | goto retry; |
| 306 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 307 | *owned_head = &pcl->next; |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 308 | /* lucky, I am the followee :) */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 309 | return COLLECT_PRIMARY_FOLLOWED; |
| 310 | } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 311 | /* |
| 312 | * type 2, link to the end of a existing open chain, |
| 313 | * be careful that its submission itself is governed |
| 314 | * by the original owned chain. |
| 315 | */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 316 | if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, |
| 317 | *owned_head) != Z_EROFS_PCLUSTER_TAIL) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 318 | goto retry; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 319 | *owned_head = Z_EROFS_PCLUSTER_TAIL; |
| 320 | return COLLECT_PRIMARY_HOOKED; |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 321 | } |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 322 | return COLLECT_PRIMARY; /* :( better luck next time */ |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 323 | } |
| 324 | |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 325 | static int z_erofs_lookup_collection(struct z_erofs_collector *clt, |
| 326 | struct inode *inode, |
| 327 | struct erofs_map_blocks *map) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 328 | { |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 329 | struct z_erofs_pcluster *pcl = clt->pcl; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 330 | struct z_erofs_collection *cl; |
| 331 | unsigned int length; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 332 | |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 333 | /* to avoid unexpected loop formed by corrupted images */ |
Gao Xiang | bfc4ccb | 2019-08-21 11:09:08 +0800 | [diff] [blame] | 334 | if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) { |
| 335 | DBG_BUGON(1); |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 336 | return -EFSCORRUPTED; |
Gao Xiang | bfc4ccb | 2019-08-21 11:09:08 +0800 | [diff] [blame] | 337 | } |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 338 | |
| 339 | cl = z_erofs_primarycollection(pcl); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 340 | if (cl->pageofs != (map->m_la & ~PAGE_MASK)) { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 341 | DBG_BUGON(1); |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 342 | return -EFSCORRUPTED; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 343 | } |
| 344 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 345 | length = READ_ONCE(pcl->length); |
| 346 | if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) { |
| 347 | if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) { |
| 348 | DBG_BUGON(1); |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 349 | return -EFSCORRUPTED; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 350 | } |
| 351 | } else { |
| 352 | unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 353 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 354 | if (map->m_flags & EROFS_MAP_FULL_MAPPED) |
| 355 | llen |= Z_EROFS_PCLUSTER_FULL_LENGTH; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 356 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 357 | while (llen > length && |
| 358 | length != cmpxchg_relaxed(&pcl->length, length, llen)) { |
| 359 | cpu_relax(); |
| 360 | length = READ_ONCE(pcl->length); |
| 361 | } |
| 362 | } |
| 363 | mutex_lock(&cl->lock); |
Gao Xiang | bfc4ccb | 2019-08-21 11:09:08 +0800 | [diff] [blame] | 364 | /* used to check tail merging loop due to corrupted images */ |
| 365 | if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) |
| 366 | clt->tailpcl = pcl; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 367 | clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head); |
Gao Xiang | bfc4ccb | 2019-08-21 11:09:08 +0800 | [diff] [blame] | 368 | /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */ |
| 369 | if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) |
| 370 | clt->tailpcl = NULL; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 371 | clt->cl = cl; |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 372 | return 0; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 373 | } |
| 374 | |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 375 | static int z_erofs_register_collection(struct z_erofs_collector *clt, |
| 376 | struct inode *inode, |
| 377 | struct erofs_map_blocks *map) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 378 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 379 | struct z_erofs_pcluster *pcl; |
| 380 | struct z_erofs_collection *cl; |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 381 | struct erofs_workgroup *grp; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 382 | int err; |
Gao Xiang | e5e3abb | 2018-09-19 13:49:07 +0800 | [diff] [blame] | 383 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 384 | /* no available workgroup, let's allocate one */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 385 | pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 386 | if (!pcl) |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 387 | return -ENOMEM; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 388 | |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 389 | atomic_set(&pcl->obj.refcount, 1); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 390 | pcl->obj.index = map->m_pa >> PAGE_SHIFT; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 391 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 392 | pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) | |
| 393 | (map->m_flags & EROFS_MAP_FULL_MAPPED ? |
| 394 | Z_EROFS_PCLUSTER_FULL_LENGTH : 0); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 395 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 396 | if (map->m_flags & EROFS_MAP_ZIPPED) |
| 397 | pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4; |
| 398 | else |
| 399 | pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED; |
Gao Xiang | b6a7618 | 2019-06-24 15:22:58 +0800 | [diff] [blame] | 400 | |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 401 | pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0]; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 402 | pcl->clusterbits -= PAGE_SHIFT; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 403 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 404 | /* new pclusters should be claimed as type 1, primary and followed */ |
| 405 | pcl->next = clt->owned_head; |
| 406 | clt->mode = COLLECT_PRIMARY_FOLLOWED; |
| 407 | |
| 408 | cl = z_erofs_primarycollection(pcl); |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 409 | |
| 410 | /* must be cleaned before freeing to slab */ |
| 411 | DBG_BUGON(cl->nr_pages); |
| 412 | DBG_BUGON(cl->vcnt); |
| 413 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 414 | cl->pageofs = map->m_la & ~PAGE_MASK; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 415 | |
Gao Xiang | 23edf3a | 2018-11-23 01:21:47 +0800 | [diff] [blame] | 416 | /* |
| 417 | * lock all primary followed works before visible to others |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 418 | * and mutex_trylock *never* fails for a new pcluster. |
Gao Xiang | 23edf3a | 2018-11-23 01:21:47 +0800 | [diff] [blame] | 419 | */ |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 420 | DBG_BUGON(!mutex_trylock(&cl->lock)); |
Gao Xiang | 23edf3a | 2018-11-23 01:21:47 +0800 | [diff] [blame] | 421 | |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 422 | grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj); |
| 423 | if (IS_ERR(grp)) { |
| 424 | err = PTR_ERR(grp); |
| 425 | goto err_out; |
| 426 | } |
| 427 | |
| 428 | if (grp != &pcl->obj) { |
| 429 | clt->pcl = container_of(grp, struct z_erofs_pcluster, obj); |
| 430 | err = -EEXIST; |
| 431 | goto err_out; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 432 | } |
Gao Xiang | bfc4ccb | 2019-08-21 11:09:08 +0800 | [diff] [blame] | 433 | /* used to check tail merging loop due to corrupted images */ |
| 434 | if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) |
| 435 | clt->tailpcl = pcl; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 436 | clt->owned_head = &pcl->next; |
| 437 | clt->pcl = pcl; |
| 438 | clt->cl = cl; |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 439 | return 0; |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 440 | |
| 441 | err_out: |
| 442 | mutex_unlock(&cl->lock); |
| 443 | kmem_cache_free(pcluster_cachep, pcl); |
| 444 | return err; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 445 | } |
| 446 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 447 | static int z_erofs_collector_begin(struct z_erofs_collector *clt, |
| 448 | struct inode *inode, |
| 449 | struct erofs_map_blocks *map) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 450 | { |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 451 | struct erofs_workgroup *grp; |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 452 | int ret; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 453 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 454 | DBG_BUGON(clt->cl); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 455 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 456 | /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */ |
| 457 | DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL); |
| 458 | DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 459 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 460 | if (!PAGE_ALIGNED(map->m_pa)) { |
| 461 | DBG_BUGON(1); |
| 462 | return -EINVAL; |
| 463 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 464 | |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 465 | grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT); |
| 466 | if (grp) { |
| 467 | clt->pcl = container_of(grp, struct z_erofs_pcluster, obj); |
| 468 | } else { |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 469 | ret = z_erofs_register_collection(clt, inode, map); |
Gao Xiang | b27661c | 2018-09-19 13:49:06 +0800 | [diff] [blame] | 470 | |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 471 | if (!ret) |
| 472 | goto out; |
| 473 | if (ret != -EEXIST) |
| 474 | return ret; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 475 | } |
| 476 | |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 477 | ret = z_erofs_lookup_collection(clt, inode, map); |
| 478 | if (ret) { |
| 479 | erofs_workgroup_put(&clt->pcl->obj); |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 480 | return ret; |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 481 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 482 | |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 483 | out: |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 484 | z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS, |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 485 | clt->cl->pagevec, clt->cl->vcnt); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 486 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 487 | clt->compressedpages = clt->pcl->compressed_pages; |
| 488 | if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */ |
| 489 | clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /* |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 494 | * keep in mind that no referenced pclusters will be freed |
| 495 | * only after a RCU grace period. |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 496 | */ |
| 497 | static void z_erofs_rcu_callback(struct rcu_head *head) |
| 498 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 499 | struct z_erofs_collection *const cl = |
| 500 | container_of(head, struct z_erofs_collection, rcu); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 501 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 502 | kmem_cache_free(pcluster_cachep, |
| 503 | container_of(cl, struct z_erofs_pcluster, |
| 504 | primary_collection)); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) |
| 508 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 509 | struct z_erofs_pcluster *const pcl = |
| 510 | container_of(grp, struct z_erofs_pcluster, obj); |
| 511 | struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 512 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 513 | call_rcu(&cl->rcu, z_erofs_rcu_callback); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 514 | } |
| 515 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 516 | static void z_erofs_collection_put(struct z_erofs_collection *cl) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 517 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 518 | struct z_erofs_pcluster *const pcl = |
| 519 | container_of(cl, struct z_erofs_pcluster, primary_collection); |
| 520 | |
| 521 | erofs_workgroup_put(&pcl->obj); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 522 | } |
| 523 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 524 | static bool z_erofs_collector_end(struct z_erofs_collector *clt) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 525 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 526 | struct z_erofs_collection *cl = clt->cl; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 527 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 528 | if (!cl) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 529 | return false; |
| 530 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 531 | z_erofs_pagevec_ctor_exit(&clt->vector, false); |
| 532 | mutex_unlock(&cl->lock); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 533 | |
| 534 | /* |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 535 | * if all pending pages are added, don't hold its reference |
| 536 | * any longer if the pcluster isn't hosted by ourselves. |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 537 | */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 538 | if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE) |
| 539 | z_erofs_collection_put(cl); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 540 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 541 | clt->cl = NULL; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 545 | static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe, |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 546 | unsigned int cachestrategy, |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 547 | erofs_off_t la) |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 548 | { |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 549 | if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) |
| 550 | return false; |
| 551 | |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 552 | if (fe->backmost) |
| 553 | return true; |
| 554 | |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 555 | return cachestrategy >= EROFS_ZIP_CACHE_READAROUND && |
| 556 | la < fe->headoffset; |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 557 | } |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 558 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 559 | static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, |
Chao Yu | e3f78d5 | 2020-09-17 09:18:21 +0800 | [diff] [blame] | 560 | struct page *page) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 561 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 562 | struct inode *const inode = fe->inode; |
Gao Xiang | bda17a4 | 2019-10-08 20:56:13 +0800 | [diff] [blame] | 563 | struct erofs_sb_info *const sbi = EROFS_I_SB(inode); |
Chao Yu | 3b42341 | 2019-01-15 09:42:21 +0800 | [diff] [blame] | 564 | struct erofs_map_blocks *const map = &fe->map; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 565 | struct z_erofs_collector *const clt = &fe->clt; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 566 | const loff_t offset = page_offset(page); |
Gao Xiang | dc76ea8 | 2019-09-22 18:04:34 +0800 | [diff] [blame] | 567 | bool tight = true; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 568 | |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 569 | enum z_erofs_cache_alloctype cache_strategy; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 570 | enum z_erofs_page_type page_type; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 571 | unsigned int cur, end, spiltted, index; |
Gao Xiang | 1e05ff3 | 2018-09-18 22:27:25 +0800 | [diff] [blame] | 572 | int err = 0; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 573 | |
| 574 | /* register locked file pages as online pages in pack */ |
| 575 | z_erofs_onlinepage_init(page); |
| 576 | |
| 577 | spiltted = 0; |
| 578 | end = PAGE_SIZE; |
| 579 | repeat: |
| 580 | cur = end - 1; |
| 581 | |
| 582 | /* lucky, within the range of the current map_blocks */ |
| 583 | if (offset + cur >= map->m_la && |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 584 | offset + cur < map->m_la + map->m_llen) { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 585 | /* didn't get a valid collection previously (very rare) */ |
| 586 | if (!clt->cl) |
Gao Xiang | 1e5ceea | 2019-02-27 13:33:31 +0800 | [diff] [blame] | 587 | goto restart_now; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 588 | goto hitted; |
Gao Xiang | 1e5ceea | 2019-02-27 13:33:31 +0800 | [diff] [blame] | 589 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 590 | |
| 591 | /* go ahead the next map_blocks */ |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 592 | erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 593 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 594 | if (z_erofs_collector_end(clt)) |
Gao Xiang | f0c519f | 2018-11-23 01:21:49 +0800 | [diff] [blame] | 595 | fe->backmost = false; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 596 | |
| 597 | map->m_la = offset + cur; |
| 598 | map->m_llen = 0; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 599 | err = z_erofs_map_blocks_iter(inode, map, 0); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 600 | if (err) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 601 | goto err_out; |
| 602 | |
Gao Xiang | 1e5ceea | 2019-02-27 13:33:31 +0800 | [diff] [blame] | 603 | restart_now: |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 604 | if (!(map->m_flags & EROFS_MAP_MAPPED)) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 605 | goto hitted; |
| 606 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 607 | err = z_erofs_collector_begin(clt, inode, map); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 608 | if (err) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 609 | goto err_out; |
| 610 | |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 611 | /* preload all compressed pages (maybe downgrade role if necessary) */ |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 612 | if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la)) |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 613 | cache_strategy = DELAYEDALLOC; |
| 614 | else |
| 615 | cache_strategy = DONTALLOC; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 616 | |
Chao Yu | e3f78d5 | 2020-09-17 09:18:21 +0800 | [diff] [blame] | 617 | preload_compressed_pages(clt, MNGD_MAPPING(sbi), cache_strategy); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 618 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 619 | hitted: |
Gao Xiang | dc76ea8 | 2019-09-22 18:04:34 +0800 | [diff] [blame] | 620 | /* |
| 621 | * Ensure the current partial page belongs to this submit chain rather |
| 622 | * than other concurrent submit chains or the noio(bypass) chain since |
| 623 | * those chains are handled asynchronously thus the page cannot be used |
| 624 | * for inplace I/O or pagevec (should be processed in strict order.) |
| 625 | */ |
| 626 | tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED && |
| 627 | clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE); |
| 628 | |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 629 | cur = end - min_t(unsigned int, offset + end - map->m_la, end); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 630 | if (!(map->m_flags & EROFS_MAP_MAPPED)) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 631 | zero_user_segment(page, cur, end); |
| 632 | goto next_part; |
| 633 | } |
| 634 | |
| 635 | /* let's derive page type */ |
| 636 | page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD : |
| 637 | (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : |
| 638 | (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : |
| 639 | Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED)); |
| 640 | |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 641 | if (cur) |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 642 | tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED); |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 643 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 644 | retry: |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 645 | err = z_erofs_attach_page(clt, page, page_type); |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 646 | /* should allocate an additional short-lived page for pagevec */ |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 647 | if (err == -EAGAIN) { |
| 648 | struct page *const newpage = |
Chao Yu | e3f78d5 | 2020-09-17 09:18:21 +0800 | [diff] [blame] | 649 | alloc_page(GFP_NOFS | __GFP_NOFAIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 650 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 651 | set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 652 | err = z_erofs_attach_page(clt, newpage, |
| 653 | Z_EROFS_PAGE_TYPE_EXCLUSIVE); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 654 | if (!err) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 655 | goto retry; |
| 656 | } |
| 657 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 658 | if (err) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 659 | goto err_out; |
| 660 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 661 | index = page->index - (map->m_la >> PAGE_SHIFT); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 662 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 663 | z_erofs_onlinepage_fixup(page, index, true); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 664 | |
Gao Xiang | 1e05ff3 | 2018-09-18 22:27:25 +0800 | [diff] [blame] | 665 | /* bump up the number of spiltted parts of a page */ |
| 666 | ++spiltted; |
| 667 | /* also update nr_pages */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 668 | clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 669 | next_part: |
| 670 | /* can be used for verification */ |
| 671 | map->m_llen = offset + cur - map->m_la; |
| 672 | |
Kristaps Čivkulis | 2bc7596 | 2018-08-05 18:21:01 +0300 | [diff] [blame] | 673 | end = cur; |
| 674 | if (end > 0) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 675 | goto repeat; |
| 676 | |
Gao Xiang | 1e05ff3 | 2018-09-18 22:27:25 +0800 | [diff] [blame] | 677 | out: |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 678 | z_erofs_onlinepage_endio(page); |
| 679 | |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 680 | erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", |
| 681 | __func__, page, spiltted, map->m_llen); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 682 | return err; |
Gao Xiang | 1e05ff3 | 2018-09-18 22:27:25 +0800 | [diff] [blame] | 683 | |
| 684 | /* if some error occurred while processing this page */ |
| 685 | err_out: |
| 686 | SetPageError(page); |
| 687 | goto out; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 688 | } |
| 689 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 690 | static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, |
| 691 | bool sync, int bios) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 692 | { |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 693 | /* wake up the caller thread for sync decompression */ |
| 694 | if (sync) { |
Gao Xiang | 848bd9a | 2018-12-08 00:19:12 +0800 | [diff] [blame] | 695 | unsigned long flags; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 696 | |
Gao Xiang | 848bd9a | 2018-12-08 00:19:12 +0800 | [diff] [blame] | 697 | spin_lock_irqsave(&io->u.wait.lock, flags); |
| 698 | if (!atomic_add_return(bios, &io->pending_bios)) |
| 699 | wake_up_locked(&io->u.wait); |
| 700 | spin_unlock_irqrestore(&io->u.wait.lock, flags); |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | if (!atomic_add_return(bios, &io->pending_bios)) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 705 | queue_work(z_erofs_workqueue, &io->u.work); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 706 | } |
| 707 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 708 | static bool z_erofs_page_is_invalidated(struct page *page) |
| 709 | { |
| 710 | return !page->mapping && !z_erofs_is_shortlived_page(page); |
| 711 | } |
| 712 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 713 | static void z_erofs_decompressqueue_endio(struct bio *bio) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 714 | { |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 715 | tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private); |
| 716 | struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t); |
Gao Xiang | 14a56ec | 2019-03-25 11:40:09 +0800 | [diff] [blame] | 717 | blk_status_t err = bio->bi_status; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 718 | struct bio_vec *bvec; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 719 | struct bvec_iter_all iter_all; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 720 | |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 721 | bio_for_each_segment_all(bvec, bio, iter_all) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 722 | struct page *page = bvec->bv_page; |
| 723 | |
| 724 | DBG_BUGON(PageUptodate(page)); |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 725 | DBG_BUGON(z_erofs_page_is_invalidated(page)); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 726 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 727 | if (err) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 728 | SetPageError(page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 729 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 730 | if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { |
| 731 | if (!err) |
| 732 | SetPageUptodate(page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 733 | unlock_page(page); |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 734 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 735 | } |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 736 | z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 737 | bio_put(bio); |
| 738 | } |
| 739 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 740 | static int z_erofs_decompress_pcluster(struct super_block *sb, |
| 741 | struct z_erofs_pcluster *pcl, |
| 742 | struct list_head *pagepool) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 743 | { |
| 744 | struct erofs_sb_info *const sbi = EROFS_SB(sb); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 745 | const unsigned int clusterpages = BIT(pcl->clusterbits); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 746 | struct z_erofs_pagevec_ctor ctor; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 747 | unsigned int i, outputsize, llen, nr_pages; |
| 748 | struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES]; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 749 | struct page **pages, **compressed_pages, *page; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 750 | |
| 751 | enum z_erofs_page_type page_type; |
Gao Xiang | b6a7618 | 2019-06-24 15:22:58 +0800 | [diff] [blame] | 752 | bool overlapped, partial; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 753 | struct z_erofs_collection *cl; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 754 | int err; |
| 755 | |
| 756 | might_sleep(); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 757 | cl = z_erofs_primarycollection(pcl); |
| 758 | DBG_BUGON(!READ_ONCE(cl->nr_pages)); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 759 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 760 | mutex_lock(&cl->lock); |
| 761 | nr_pages = cl->nr_pages; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 762 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 763 | if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 764 | pages = pages_onstack; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 765 | } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES && |
| 766 | mutex_trylock(&z_pagemap_global_lock)) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 767 | pages = z_pagemap_global; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 768 | } else { |
Chao Yu | 441dfcc | 2019-07-16 17:44:22 +0800 | [diff] [blame] | 769 | gfp_t gfp_flags = GFP_KERNEL; |
| 770 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 771 | if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES) |
Chao Yu | 441dfcc | 2019-07-16 17:44:22 +0800 | [diff] [blame] | 772 | gfp_flags |= __GFP_NOFAIL; |
| 773 | |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 774 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Chao Yu | 441dfcc | 2019-07-16 17:44:22 +0800 | [diff] [blame] | 775 | gfp_flags); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 776 | |
| 777 | /* fallback to global pagemap for the lowmem scenario */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 778 | if (!pages) { |
Chao Yu | 441dfcc | 2019-07-16 17:44:22 +0800 | [diff] [blame] | 779 | mutex_lock(&z_pagemap_global_lock); |
| 780 | pages = z_pagemap_global; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
| 784 | for (i = 0; i < nr_pages; ++i) |
| 785 | pages[i] = NULL; |
| 786 | |
Gao Xiang | e12a0ce | 2019-08-21 22:01:52 +0800 | [diff] [blame] | 787 | err = 0; |
Gao Xiang | fa61a33 | 2019-06-24 15:22:53 +0800 | [diff] [blame] | 788 | z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS, |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 789 | cl->pagevec, 0); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 790 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 791 | for (i = 0; i < cl->vcnt; ++i) { |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 792 | unsigned int pagenr; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 793 | |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 794 | page = z_erofs_pagevec_dequeue(&ctor, &page_type); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 795 | |
| 796 | /* all pages in pagevec ought to be valid */ |
Cristian Sicilia | 42d40b4 | 2018-11-12 21:43:57 +0100 | [diff] [blame] | 797 | DBG_BUGON(!page); |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 798 | DBG_BUGON(z_erofs_page_is_invalidated(page)); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 799 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 800 | if (z_erofs_put_shortlivedpage(pagepool, page)) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 801 | continue; |
| 802 | |
| 803 | if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD) |
| 804 | pagenr = 0; |
| 805 | else |
| 806 | pagenr = z_erofs_onlinepage_index(page); |
| 807 | |
Gao Xiang | 70b1799 | 2018-12-11 15:17:49 +0800 | [diff] [blame] | 808 | DBG_BUGON(pagenr >= nr_pages); |
Gao Xiang | e5e3abb | 2018-09-19 13:49:07 +0800 | [diff] [blame] | 809 | |
Gao Xiang | e12a0ce | 2019-08-21 22:01:52 +0800 | [diff] [blame] | 810 | /* |
| 811 | * currently EROFS doesn't support multiref(dedup), |
| 812 | * so here erroring out one multiref page. |
| 813 | */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 814 | if (pages[pagenr]) { |
Gao Xiang | e12a0ce | 2019-08-21 22:01:52 +0800 | [diff] [blame] | 815 | DBG_BUGON(1); |
| 816 | SetPageError(pages[pagenr]); |
| 817 | z_erofs_onlinepage_endio(pages[pagenr]); |
| 818 | err = -EFSCORRUPTED; |
| 819 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 820 | pages[pagenr] = page; |
| 821 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 822 | z_erofs_pagevec_ctor_exit(&ctor, true); |
| 823 | |
| 824 | overlapped = false; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 825 | compressed_pages = pcl->compressed_pages; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 826 | |
| 827 | for (i = 0; i < clusterpages; ++i) { |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 828 | unsigned int pagenr; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 829 | |
| 830 | page = compressed_pages[i]; |
| 831 | |
| 832 | /* all compressed pages ought to be valid */ |
Cristian Sicilia | 42d40b4 | 2018-11-12 21:43:57 +0100 | [diff] [blame] | 833 | DBG_BUGON(!page); |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 834 | DBG_BUGON(z_erofs_page_is_invalidated(page)); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 835 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 836 | if (!z_erofs_is_shortlived_page(page)) { |
Gao Xiang | d61fbb6 | 2019-03-25 11:40:08 +0800 | [diff] [blame] | 837 | if (erofs_page_is_managed(sbi, page)) { |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 838 | if (!PageUptodate(page)) |
Gao Xiang | 1115249 | 2019-03-25 11:40:07 +0800 | [diff] [blame] | 839 | err = -EIO; |
| 840 | continue; |
| 841 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 842 | |
Gao Xiang | 1115249 | 2019-03-25 11:40:07 +0800 | [diff] [blame] | 843 | /* |
| 844 | * only if non-head page can be selected |
| 845 | * for inplace decompression |
| 846 | */ |
| 847 | pagenr = z_erofs_onlinepage_index(page); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 848 | |
Gao Xiang | 1115249 | 2019-03-25 11:40:07 +0800 | [diff] [blame] | 849 | DBG_BUGON(pagenr >= nr_pages); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 850 | if (pages[pagenr]) { |
Gao Xiang | e12a0ce | 2019-08-21 22:01:52 +0800 | [diff] [blame] | 851 | DBG_BUGON(1); |
| 852 | SetPageError(pages[pagenr]); |
| 853 | z_erofs_onlinepage_endio(pages[pagenr]); |
| 854 | err = -EFSCORRUPTED; |
| 855 | } |
Gao Xiang | 1115249 | 2019-03-25 11:40:07 +0800 | [diff] [blame] | 856 | pages[pagenr] = page; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 857 | |
Gao Xiang | 1115249 | 2019-03-25 11:40:07 +0800 | [diff] [blame] | 858 | overlapped = true; |
| 859 | } |
| 860 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 861 | /* PG_error needs checking for all non-managed pages */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 862 | if (PageError(page)) { |
Gao Xiang | 1115249 | 2019-03-25 11:40:07 +0800 | [diff] [blame] | 863 | DBG_BUGON(PageUptodate(page)); |
| 864 | err = -EIO; |
| 865 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 866 | } |
| 867 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 868 | if (err) |
Gao Xiang | 1115249 | 2019-03-25 11:40:07 +0800 | [diff] [blame] | 869 | goto out; |
| 870 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 871 | llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT; |
| 872 | if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) { |
| 873 | outputsize = llen; |
| 874 | partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH); |
Gao Xiang | b6a7618 | 2019-06-24 15:22:58 +0800 | [diff] [blame] | 875 | } else { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 876 | outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs; |
Gao Xiang | b6a7618 | 2019-06-24 15:22:58 +0800 | [diff] [blame] | 877 | partial = true; |
| 878 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 879 | |
Gao Xiang | 88aaf5a | 2019-06-24 15:22:57 +0800 | [diff] [blame] | 880 | err = z_erofs_decompress(&(struct z_erofs_decompress_req) { |
| 881 | .sb = sb, |
| 882 | .in = compressed_pages, |
| 883 | .out = pages, |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 884 | .pageofs_out = cl->pageofs, |
Gao Xiang | 88aaf5a | 2019-06-24 15:22:57 +0800 | [diff] [blame] | 885 | .inputsize = PAGE_SIZE, |
| 886 | .outputsize = outputsize, |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 887 | .alg = pcl->algorithmformat, |
Gao Xiang | 88aaf5a | 2019-06-24 15:22:57 +0800 | [diff] [blame] | 888 | .inplace_io = overlapped, |
Gao Xiang | b6a7618 | 2019-06-24 15:22:58 +0800 | [diff] [blame] | 889 | .partial_decoding = partial |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 890 | }, pagepool); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 891 | |
| 892 | out: |
Gao Xiang | af692e1 | 2019-02-27 13:33:30 +0800 | [diff] [blame] | 893 | /* must handle all compressed pages before endding pages */ |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 894 | for (i = 0; i < clusterpages; ++i) { |
| 895 | page = compressed_pages[i]; |
| 896 | |
Gao Xiang | d61fbb6 | 2019-03-25 11:40:08 +0800 | [diff] [blame] | 897 | if (erofs_page_is_managed(sbi, page)) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 898 | continue; |
Gao Xiang | d61fbb6 | 2019-03-25 11:40:08 +0800 | [diff] [blame] | 899 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 900 | /* recycle all individual short-lived pages */ |
| 901 | (void)z_erofs_put_shortlivedpage(pagepool, page); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 902 | |
| 903 | WRITE_ONCE(compressed_pages[i], NULL); |
| 904 | } |
| 905 | |
Gao Xiang | af692e1 | 2019-02-27 13:33:30 +0800 | [diff] [blame] | 906 | for (i = 0; i < nr_pages; ++i) { |
| 907 | page = pages[i]; |
| 908 | if (!page) |
| 909 | continue; |
| 910 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 911 | DBG_BUGON(z_erofs_page_is_invalidated(page)); |
Gao Xiang | af692e1 | 2019-02-27 13:33:30 +0800 | [diff] [blame] | 912 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 913 | /* recycle all individual short-lived pages */ |
| 914 | if (z_erofs_put_shortlivedpage(pagepool, page)) |
Gao Xiang | af692e1 | 2019-02-27 13:33:30 +0800 | [diff] [blame] | 915 | continue; |
| 916 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 917 | if (err < 0) |
Gao Xiang | af692e1 | 2019-02-27 13:33:30 +0800 | [diff] [blame] | 918 | SetPageError(page); |
| 919 | |
| 920 | z_erofs_onlinepage_endio(page); |
| 921 | } |
| 922 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 923 | if (pages == z_pagemap_global) |
| 924 | mutex_unlock(&z_pagemap_global_lock); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 925 | else if (pages != pages_onstack) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 926 | kvfree(pages); |
| 927 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 928 | cl->nr_pages = 0; |
| 929 | cl->vcnt = 0; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 930 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 931 | /* all cl locks MUST be taken before the following line */ |
| 932 | WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 933 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 934 | /* all cl locks SHOULD be released right now */ |
| 935 | mutex_unlock(&cl->lock); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 936 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 937 | z_erofs_collection_put(cl); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 938 | return err; |
| 939 | } |
| 940 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 941 | static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, |
| 942 | struct list_head *pagepool) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 943 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 944 | z_erofs_next_pcluster_t owned = io->head; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 945 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 946 | while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { |
| 947 | struct z_erofs_pcluster *pcl; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 948 | |
| 949 | /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 950 | DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 951 | |
| 952 | /* no possible that 'owned' equals NULL */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 953 | DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 954 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 955 | pcl = container_of(owned, struct z_erofs_pcluster, next); |
| 956 | owned = READ_ONCE(pcl->next); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 957 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 958 | z_erofs_decompress_pcluster(io->sb, pcl, pagepool); |
Gao Xiang | 3978c8e | 2018-08-06 11:27:53 +0800 | [diff] [blame] | 959 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 960 | } |
| 961 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 962 | static void z_erofs_decompressqueue_work(struct work_struct *work) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 963 | { |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 964 | struct z_erofs_decompressqueue *bgq = |
| 965 | container_of(work, struct z_erofs_decompressqueue, u.work); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 966 | LIST_HEAD(pagepool); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 967 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 968 | DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 969 | z_erofs_decompress_queue(bgq, &pagepool); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 970 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 971 | put_pages_list(&pagepool); |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 972 | kvfree(bgq); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 973 | } |
| 974 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 975 | static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, |
| 976 | unsigned int nr, |
| 977 | struct list_head *pagepool, |
| 978 | struct address_space *mc, |
| 979 | gfp_t gfp) |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 980 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 981 | const pgoff_t index = pcl->obj.index; |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 982 | bool tocache = false; |
| 983 | |
| 984 | struct address_space *mapping; |
| 985 | struct page *oldpage, *page; |
| 986 | |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 987 | compressed_page_t t; |
| 988 | int justfound; |
| 989 | |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 990 | repeat: |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 991 | page = READ_ONCE(pcl->compressed_pages[nr]); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 992 | oldpage = page; |
| 993 | |
| 994 | if (!page) |
| 995 | goto out_allocpage; |
| 996 | |
| 997 | /* |
| 998 | * the cached page has not been allocated and |
| 999 | * an placeholder is out there, prepare it now. |
| 1000 | */ |
Gao Xiang | bda17a4 | 2019-10-08 20:56:13 +0800 | [diff] [blame] | 1001 | if (page == PAGE_UNALLOCATED) { |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1002 | tocache = true; |
| 1003 | goto out_allocpage; |
| 1004 | } |
| 1005 | |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1006 | /* process the target tagged pointer */ |
| 1007 | t = tagptr_init(compressed_page_t, page); |
| 1008 | justfound = tagptr_unfold_tags(t); |
| 1009 | page = tagptr_unfold_ptr(t); |
| 1010 | |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1011 | mapping = READ_ONCE(page->mapping); |
| 1012 | |
| 1013 | /* |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 1014 | * file-backed online pages in plcuster are all locked steady, |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1015 | * therefore it is impossible for `mapping' to be NULL. |
| 1016 | */ |
| 1017 | if (mapping && mapping != mc) |
| 1018 | /* ought to be unmanaged pages */ |
| 1019 | goto out; |
| 1020 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 1021 | /* directly return for shortlived page as well */ |
| 1022 | if (z_erofs_is_shortlived_page(page)) |
| 1023 | goto out; |
| 1024 | |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1025 | lock_page(page); |
| 1026 | |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1027 | /* only true if page reclaim goes wrong, should never happen */ |
| 1028 | DBG_BUGON(justfound && PagePrivate(page)); |
| 1029 | |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1030 | /* the page is still in manage cache */ |
| 1031 | if (page->mapping == mc) { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1032 | WRITE_ONCE(pcl->compressed_pages[nr], page); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1033 | |
Gao Xiang | 1115249 | 2019-03-25 11:40:07 +0800 | [diff] [blame] | 1034 | ClearPageError(page); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1035 | if (!PagePrivate(page)) { |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1036 | /* |
| 1037 | * impossible to be !PagePrivate(page) for |
| 1038 | * the current restriction as well if |
| 1039 | * the page is already in compressed_pages[]. |
| 1040 | */ |
| 1041 | DBG_BUGON(!justfound); |
| 1042 | |
| 1043 | justfound = 0; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1044 | set_page_private(page, (unsigned long)pcl); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1045 | SetPagePrivate(page); |
| 1046 | } |
| 1047 | |
| 1048 | /* no need to submit io if it is already up-to-date */ |
| 1049 | if (PageUptodate(page)) { |
| 1050 | unlock_page(page); |
| 1051 | page = NULL; |
| 1052 | } |
| 1053 | goto out; |
| 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | * the managed page has been truncated, it's unsafe to |
| 1058 | * reuse this one, let's allocate a new cache-managed page. |
| 1059 | */ |
| 1060 | DBG_BUGON(page->mapping); |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1061 | DBG_BUGON(!justfound); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1062 | |
| 1063 | tocache = true; |
| 1064 | unlock_page(page); |
| 1065 | put_page(page); |
| 1066 | out_allocpage: |
Gao Xiang | 5ddcee1 | 2019-11-21 21:59:54 +0800 | [diff] [blame] | 1067 | page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); |
| 1068 | if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 1069 | /* turn into temporary page if fails */ |
| 1070 | set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); |
Gao Xiang | 5ddcee1 | 2019-11-21 21:59:54 +0800 | [diff] [blame] | 1071 | tocache = false; |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Gao Xiang | 5ddcee1 | 2019-11-21 21:59:54 +0800 | [diff] [blame] | 1074 | if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) { |
| 1075 | if (tocache) { |
| 1076 | /* since it added to managed cache successfully */ |
| 1077 | unlock_page(page); |
| 1078 | put_page(page); |
| 1079 | } else { |
| 1080 | list_add(&page->lru, pagepool); |
| 1081 | } |
| 1082 | cond_resched(); |
| 1083 | goto repeat; |
| 1084 | } |
Gao Xiang | a30573b | 2020-10-22 22:57:21 +0800 | [diff] [blame] | 1085 | |
| 1086 | if (tocache) { |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame^] | 1087 | attach_page_private(page, pcl); |
| 1088 | /* drop a ref added by allocpage (then we have 2 refs here) */ |
| 1089 | put_page(page); |
Gao Xiang | a30573b | 2020-10-22 22:57:21 +0800 | [diff] [blame] | 1090 | } |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1091 | out: /* the only exit (for tracing and debugging) */ |
| 1092 | return page; |
| 1093 | } |
| 1094 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1095 | static struct z_erofs_decompressqueue * |
| 1096 | jobqueue_init(struct super_block *sb, |
| 1097 | struct z_erofs_decompressqueue *fgq, bool *fg) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1098 | { |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1099 | struct z_erofs_decompressqueue *q; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1100 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1101 | if (fg && !*fg) { |
| 1102 | q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); |
| 1103 | if (!q) { |
| 1104 | *fg = true; |
| 1105 | goto fg_out; |
| 1106 | } |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1107 | INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1108 | } else { |
| 1109 | fg_out: |
| 1110 | q = fgq; |
| 1111 | init_waitqueue_head(&fgq->u.wait); |
| 1112 | atomic_set(&fgq->pending_bios, 0); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1113 | } |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1114 | q->sb = sb; |
| 1115 | q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; |
| 1116 | return q; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1117 | } |
| 1118 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1119 | /* define decompression jobqueue types */ |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1120 | enum { |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1121 | JQ_BYPASS, |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1122 | JQ_SUBMIT, |
| 1123 | NR_JOBQUEUES, |
| 1124 | }; |
| 1125 | |
| 1126 | static void *jobqueueset_init(struct super_block *sb, |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1127 | struct z_erofs_decompressqueue *q[], |
| 1128 | struct z_erofs_decompressqueue *fgq, bool *fg) |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1129 | { |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1130 | /* |
| 1131 | * if managed cache is enabled, bypass jobqueue is needed, |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1132 | * no need to read from device for all pclusters in this queue. |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1133 | */ |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1134 | q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); |
| 1135 | q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg); |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1136 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1137 | return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg)); |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1140 | static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, |
| 1141 | z_erofs_next_pcluster_t qtail[], |
| 1142 | z_erofs_next_pcluster_t owned_head) |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1143 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1144 | z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; |
| 1145 | z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1146 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1147 | DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); |
| 1148 | if (owned_head == Z_EROFS_PCLUSTER_TAIL) |
| 1149 | owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1150 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1151 | WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1152 | |
| 1153 | WRITE_ONCE(*submit_qtail, owned_head); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1154 | WRITE_ONCE(*bypass_qtail, &pcl->next); |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1155 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1156 | qtail[JQ_BYPASS] = &pcl->next; |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1159 | static void z_erofs_submit_queue(struct super_block *sb, |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1160 | struct z_erofs_decompress_frontend *f, |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1161 | struct list_head *pagepool, |
| 1162 | struct z_erofs_decompressqueue *fgq, |
| 1163 | bool *force_fg) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1164 | { |
Gao Xiang | bda17a4 | 2019-10-08 20:56:13 +0800 | [diff] [blame] | 1165 | struct erofs_sb_info *const sbi = EROFS_SB(sb); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1166 | z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1167 | struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1168 | void *bi_private; |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1169 | z_erofs_next_pcluster_t owned_head = f->clt.owned_head; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1170 | /* since bio will be NULL, no need to initialize last_index */ |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 1171 | pgoff_t last_index; |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1172 | unsigned int nr_bios = 0; |
| 1173 | struct bio *bio = NULL; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1174 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1175 | bi_private = jobqueueset_init(sb, q, fgq, force_fg); |
| 1176 | qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; |
| 1177 | qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1178 | |
| 1179 | /* by default, all need io submission */ |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1180 | q[JQ_SUBMIT]->head = owned_head; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1181 | |
| 1182 | do { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1183 | struct z_erofs_pcluster *pcl; |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1184 | pgoff_t cur, end; |
| 1185 | unsigned int i = 0; |
| 1186 | bool bypass = true; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1187 | |
| 1188 | /* no possible 'owned_head' equals the following */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1189 | DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); |
| 1190 | DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1191 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1192 | pcl = container_of(owned_head, struct z_erofs_pcluster, next); |
| 1193 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1194 | cur = pcl->obj.index; |
| 1195 | end = cur + BIT(pcl->clusterbits); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1196 | |
| 1197 | /* close the main owned chain at first */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1198 | owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, |
| 1199 | Z_EROFS_PCLUSTER_TAIL_CLOSED); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1200 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1201 | do { |
| 1202 | struct page *page; |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1203 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1204 | page = pickup_page_for_submission(pcl, i++, pagepool, |
| 1205 | MNGD_MAPPING(sbi), |
| 1206 | GFP_NOFS); |
| 1207 | if (!page) |
| 1208 | continue; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1209 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1210 | if (bio && cur != last_index + 1) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1211 | submit_bio_retry: |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1212 | submit_bio(bio); |
| 1213 | bio = NULL; |
| 1214 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1215 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1216 | if (!bio) { |
| 1217 | bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES); |
Gao Xiang | a5c0b78 | 2019-09-04 10:09:02 +0800 | [diff] [blame] | 1218 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1219 | bio->bi_end_io = z_erofs_decompressqueue_endio; |
| 1220 | bio_set_dev(bio, sb->s_bdev); |
| 1221 | bio->bi_iter.bi_sector = (sector_t)cur << |
| 1222 | LOG_SECTORS_PER_BLOCK; |
| 1223 | bio->bi_private = bi_private; |
| 1224 | bio->bi_opf = REQ_OP_READ; |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1225 | if (f->readahead) |
| 1226 | bio->bi_opf |= REQ_RAHEAD; |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1227 | ++nr_bios; |
| 1228 | } |
Gao Xiang | 94e4e15 | 2019-09-04 10:09:04 +0800 | [diff] [blame] | 1229 | |
Gao Xiang | 6c3e485 | 2020-09-19 15:27:28 +0800 | [diff] [blame] | 1230 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1231 | goto submit_bio_retry; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1232 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1233 | last_index = cur; |
| 1234 | bypass = false; |
| 1235 | } while (++cur < end); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1236 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1237 | if (!bypass) |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1238 | qtail[JQ_SUBMIT] = &pcl->next; |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1239 | else |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1240 | move_to_bypass_jobqueue(pcl, qtail, owned_head); |
| 1241 | } while (owned_head != Z_EROFS_PCLUSTER_TAIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1242 | |
Cristian Sicilia | 42d40b4 | 2018-11-12 21:43:57 +0100 | [diff] [blame] | 1243 | if (bio) |
Gao Xiang | 94e4e15 | 2019-09-04 10:09:04 +0800 | [diff] [blame] | 1244 | submit_bio(bio); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1245 | |
Gao Xiang | 587a67b | 2020-01-21 14:47:47 +0800 | [diff] [blame] | 1246 | /* |
| 1247 | * although background is preferred, no one is pending for submission. |
| 1248 | * don't issue workqueue for decompression but drop it directly instead. |
| 1249 | */ |
| 1250 | if (!*force_fg && !nr_bios) { |
| 1251 | kvfree(q[JQ_SUBMIT]); |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1252 | return; |
Gao Xiang | 587a67b | 2020-01-21 14:47:47 +0800 | [diff] [blame] | 1253 | } |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1254 | z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1257 | static void z_erofs_runqueue(struct super_block *sb, |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1258 | struct z_erofs_decompress_frontend *f, |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1259 | struct list_head *pagepool, bool force_fg) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1260 | { |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1261 | struct z_erofs_decompressqueue io[NR_JOBQUEUES]; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1262 | |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1263 | if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1264 | return; |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1265 | z_erofs_submit_queue(sb, f, pagepool, io, &force_fg); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1266 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1267 | /* handle bypass queue (no i/o pclusters) immediately */ |
| 1268 | z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool); |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 1269 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1270 | if (!force_fg) |
| 1271 | return; |
| 1272 | |
| 1273 | /* wait until all bios are completed */ |
Gao Xiang | a93f8c3 | 2019-10-08 20:56:16 +0800 | [diff] [blame] | 1274 | io_wait_event(io[JQ_SUBMIT].u.wait, |
| 1275 | !atomic_read(&io[JQ_SUBMIT].pending_bios)); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1276 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1277 | /* handle synchronous decompress queue in the caller context */ |
| 1278 | z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1279 | } |
| 1280 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1281 | static int z_erofs_readpage(struct file *file, struct page *page) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1282 | { |
| 1283 | struct inode *const inode = page->mapping->host; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1284 | struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1285 | int err; |
| 1286 | LIST_HEAD(pagepool); |
| 1287 | |
Gao Xiang | ba9ce77 | 2018-11-23 01:15:58 +0800 | [diff] [blame] | 1288 | trace_erofs_readpage(page, false); |
| 1289 | |
Gao Xiang | f0c519f | 2018-11-23 01:21:49 +0800 | [diff] [blame] | 1290 | f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; |
| 1291 | |
Chao Yu | e3f78d5 | 2020-09-17 09:18:21 +0800 | [diff] [blame] | 1292 | err = z_erofs_do_read_page(&f, page); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1293 | (void)z_erofs_collector_end(&f.clt); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1294 | |
Gao Xiang | ee45197 | 2019-08-19 18:34:21 +0800 | [diff] [blame] | 1295 | /* if some compressed cluster ready, need submit them anyway */ |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1296 | z_erofs_runqueue(inode->i_sb, &f, &pagepool, true); |
Gao Xiang | ee45197 | 2019-08-19 18:34:21 +0800 | [diff] [blame] | 1297 | |
| 1298 | if (err) |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 1299 | erofs_err(inode->i_sb, "failed to read, err [%d]", err); |
Gao Xiang | ee45197 | 2019-08-19 18:34:21 +0800 | [diff] [blame] | 1300 | |
Chao Yu | 3b42341 | 2019-01-15 09:42:21 +0800 | [diff] [blame] | 1301 | if (f.map.mpage) |
| 1302 | put_page(f.map.mpage); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1303 | |
| 1304 | /* clean up the remaining free pages */ |
| 1305 | put_pages_list(&pagepool); |
Gao Xiang | ee45197 | 2019-08-19 18:34:21 +0800 | [diff] [blame] | 1306 | return err; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1307 | } |
| 1308 | |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1309 | static void z_erofs_readahead(struct readahead_control *rac) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1310 | { |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1311 | struct inode *const inode = rac->mapping->host; |
Gao Xiang | 5fb76bb | 2018-09-20 00:06:56 +0800 | [diff] [blame] | 1312 | struct erofs_sb_info *const sbi = EROFS_I_SB(inode); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1313 | |
Gao Xiang | bf9a123 | 2020-09-19 15:27:29 +0800 | [diff] [blame] | 1314 | unsigned int nr_pages = readahead_count(rac); |
| 1315 | bool sync = (nr_pages <= sbi->ctx.max_sync_decompress_pages); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1316 | struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1317 | struct page *page, *head = NULL; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1318 | LIST_HEAD(pagepool); |
| 1319 | |
Gao Xiang | bf9a123 | 2020-09-19 15:27:29 +0800 | [diff] [blame] | 1320 | trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); |
Chen Gong | 284db12 | 2018-09-18 22:27:27 +0800 | [diff] [blame] | 1321 | |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1322 | f.readahead = true; |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1323 | f.headoffset = readahead_pos(rac); |
Gao Xiang | f0c519f | 2018-11-23 01:21:49 +0800 | [diff] [blame] | 1324 | |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1325 | while ((page = readahead_page(rac))) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1326 | prefetchw(&page->flags); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1327 | |
Gao Xiang | 2d9b5dc | 2018-11-23 01:21:48 +0800 | [diff] [blame] | 1328 | /* |
| 1329 | * A pure asynchronous readahead is indicated if |
| 1330 | * a PG_readahead marked page is hitted at first. |
| 1331 | * Let's also do asynchronous decompression for this case. |
| 1332 | */ |
| 1333 | sync &= !(PageReadahead(page) && !head); |
| 1334 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1335 | set_page_private(page, (unsigned long)head); |
| 1336 | head = page; |
| 1337 | } |
| 1338 | |
Cristian Sicilia | 42d40b4 | 2018-11-12 21:43:57 +0100 | [diff] [blame] | 1339 | while (head) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1340 | struct page *page = head; |
| 1341 | int err; |
| 1342 | |
| 1343 | /* traversal in reverse order */ |
| 1344 | head = (void *)page_private(page); |
| 1345 | |
Chao Yu | e3f78d5 | 2020-09-17 09:18:21 +0800 | [diff] [blame] | 1346 | err = z_erofs_do_read_page(&f, page); |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 1347 | if (err) |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 1348 | erofs_err(inode->i_sb, |
| 1349 | "readahead error at page %lu @ nid %llu", |
| 1350 | page->index, EROFS_I(inode)->nid); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1351 | put_page(page); |
| 1352 | } |
| 1353 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1354 | (void)z_erofs_collector_end(&f.clt); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1355 | |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1356 | z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1357 | |
Chao Yu | 3b42341 | 2019-01-15 09:42:21 +0800 | [diff] [blame] | 1358 | if (f.map.mpage) |
| 1359 | put_page(f.map.mpage); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1360 | |
| 1361 | /* clean up the remaining free pages */ |
| 1362 | put_pages_list(&pagepool); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1363 | } |
| 1364 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1365 | const struct address_space_operations z_erofs_aops = { |
| 1366 | .readpage = z_erofs_readpage, |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1367 | .readahead = z_erofs_readahead, |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1368 | }; |
Gao Xiang | 02827e1 | 2018-07-26 20:21:58 +0800 | [diff] [blame] | 1369 | |