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