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