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