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 | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 5 | * Copyright (C) 2022 Alibaba Cloud |
Gao Xiang | 02827e1 | 2018-07-26 20:21:58 +0800 | [diff] [blame] | 6 | */ |
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> |
Christoph Hellwig | 99486c5 | 2022-09-15 10:41:59 +0100 | [diff] [blame] | 9 | #include <linux/psi.h> |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 10 | #include <linux/cpuhotplug.h> |
Chen Gong | 284db12 | 2018-09-18 22:27:27 +0800 | [diff] [blame] | 11 | #include <trace/events/erofs.h> |
| 12 | |
Gao Xiang | a9a94d9 | 2023-02-04 17:30:38 +0800 | [diff] [blame] | 13 | #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE) |
| 14 | #define Z_EROFS_INLINE_BVECS 2 |
| 15 | |
| 16 | /* |
| 17 | * let's leave a type here in case of introducing |
| 18 | * another tagged pointer later. |
| 19 | */ |
| 20 | typedef void *z_erofs_next_pcluster_t; |
| 21 | |
| 22 | struct z_erofs_bvec { |
| 23 | struct page *page; |
| 24 | int offset; |
| 25 | unsigned int end; |
| 26 | }; |
| 27 | |
| 28 | #define __Z_EROFS_BVSET(name, total) \ |
| 29 | struct name { \ |
| 30 | /* point to the next page which contains the following bvecs */ \ |
| 31 | struct page *nextpage; \ |
| 32 | struct z_erofs_bvec bvec[total]; \ |
| 33 | } |
| 34 | __Z_EROFS_BVSET(z_erofs_bvset,); |
| 35 | __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS); |
| 36 | |
| 37 | /* |
| 38 | * Structure fields follow one of the following exclusion rules. |
| 39 | * |
| 40 | * I: Modifiable by initialization/destruction paths and read-only |
| 41 | * for everyone else; |
| 42 | * |
| 43 | * L: Field should be protected by the pcluster lock; |
| 44 | * |
| 45 | * A: Field should be accessed / updated in atomic for parallelized code. |
| 46 | */ |
| 47 | struct z_erofs_pcluster { |
| 48 | struct erofs_workgroup obj; |
| 49 | struct mutex lock; |
| 50 | |
| 51 | /* A: point to next chained pcluster or TAILs */ |
| 52 | z_erofs_next_pcluster_t next; |
| 53 | |
| 54 | /* L: the maximum decompression size of this round */ |
| 55 | unsigned int length; |
| 56 | |
| 57 | /* L: total number of bvecs */ |
| 58 | unsigned int vcnt; |
| 59 | |
| 60 | /* I: page offset of start position of decompression */ |
| 61 | unsigned short pageofs_out; |
| 62 | |
| 63 | /* I: page offset of inline compressed data */ |
| 64 | unsigned short pageofs_in; |
| 65 | |
| 66 | union { |
| 67 | /* L: inline a certain number of bvec for bootstrap */ |
| 68 | struct z_erofs_bvset_inline bvset; |
| 69 | |
| 70 | /* I: can be used to free the pcluster by RCU. */ |
| 71 | struct rcu_head rcu; |
| 72 | }; |
| 73 | |
| 74 | union { |
| 75 | /* I: physical cluster size in pages */ |
| 76 | unsigned short pclusterpages; |
| 77 | |
| 78 | /* I: tailpacking inline compressed size */ |
| 79 | unsigned short tailpacking_size; |
| 80 | }; |
| 81 | |
| 82 | /* I: compression algorithm format */ |
| 83 | unsigned char algorithmformat; |
| 84 | |
| 85 | /* L: whether partial decompression or not */ |
| 86 | bool partial; |
| 87 | |
| 88 | /* L: indicate several pageofs_outs or not */ |
| 89 | bool multibases; |
| 90 | |
| 91 | /* A: compressed bvecs (can be cached or inplaced pages) */ |
| 92 | struct z_erofs_bvec compressed_bvecs[]; |
| 93 | }; |
| 94 | |
| 95 | /* let's avoid the valid 32-bit kernel addresses */ |
| 96 | |
| 97 | /* the chained workgroup has't submitted io (still open) */ |
| 98 | #define Z_EROFS_PCLUSTER_TAIL ((void *)0x5F0ECAFE) |
| 99 | /* the chained workgroup has already submitted io */ |
| 100 | #define Z_EROFS_PCLUSTER_TAIL_CLOSED ((void *)0x5F0EDEAD) |
| 101 | |
| 102 | #define Z_EROFS_PCLUSTER_NIL (NULL) |
| 103 | |
| 104 | struct z_erofs_decompressqueue { |
| 105 | struct super_block *sb; |
| 106 | atomic_t pending_bios; |
| 107 | z_erofs_next_pcluster_t head; |
| 108 | |
| 109 | union { |
| 110 | struct completion done; |
| 111 | struct work_struct work; |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 112 | struct kthread_work kthread_work; |
Gao Xiang | a9a94d9 | 2023-02-04 17:30:38 +0800 | [diff] [blame] | 113 | } u; |
| 114 | bool eio, sync; |
| 115 | }; |
| 116 | |
| 117 | static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl) |
| 118 | { |
| 119 | return !pcl->obj.index; |
| 120 | } |
| 121 | |
| 122 | static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl) |
| 123 | { |
| 124 | if (z_erofs_is_inline_pcluster(pcl)) |
| 125 | return 1; |
| 126 | return pcl->pclusterpages; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * bit 30: I/O error occurred on this page |
| 131 | * bit 0 - 29: remaining parts to complete this page |
| 132 | */ |
| 133 | #define Z_EROFS_PAGE_EIO (1 << 30) |
| 134 | |
| 135 | static inline void z_erofs_onlinepage_init(struct page *page) |
| 136 | { |
| 137 | union { |
| 138 | atomic_t o; |
| 139 | unsigned long v; |
| 140 | } u = { .o = ATOMIC_INIT(1) }; |
| 141 | |
| 142 | set_page_private(page, u.v); |
| 143 | smp_wmb(); |
| 144 | SetPagePrivate(page); |
| 145 | } |
| 146 | |
| 147 | static inline void z_erofs_onlinepage_split(struct page *page) |
| 148 | { |
| 149 | atomic_inc((atomic_t *)&page->private); |
| 150 | } |
| 151 | |
| 152 | static inline void z_erofs_page_mark_eio(struct page *page) |
| 153 | { |
| 154 | int orig; |
| 155 | |
| 156 | do { |
| 157 | orig = atomic_read((atomic_t *)&page->private); |
| 158 | } while (atomic_cmpxchg((atomic_t *)&page->private, orig, |
| 159 | orig | Z_EROFS_PAGE_EIO) != orig); |
| 160 | } |
| 161 | |
| 162 | static inline void z_erofs_onlinepage_endio(struct page *page) |
| 163 | { |
| 164 | unsigned int v; |
| 165 | |
| 166 | DBG_BUGON(!PagePrivate(page)); |
| 167 | v = atomic_dec_return((atomic_t *)&page->private); |
| 168 | if (!(v & ~Z_EROFS_PAGE_EIO)) { |
| 169 | set_page_private(page, 0); |
| 170 | ClearPagePrivate(page); |
| 171 | if (!(v & Z_EROFS_PAGE_EIO)) |
| 172 | SetPageUptodate(page); |
| 173 | unlock_page(page); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | #define Z_EROFS_ONSTACK_PAGES 32 |
| 178 | |
Gao Xiang | 672e547 | 2018-12-08 00:19:14 +0800 | [diff] [blame] | 179 | /* |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 180 | * since pclustersize is variable for big pcluster feature, introduce slab |
| 181 | * pools implementation for different pcluster sizes. |
| 182 | */ |
| 183 | struct z_erofs_pcluster_slab { |
| 184 | struct kmem_cache *slab; |
| 185 | unsigned int maxpages; |
| 186 | char name[48]; |
| 187 | }; |
| 188 | |
| 189 | #define _PCLP(n) { .maxpages = n } |
| 190 | |
| 191 | static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = { |
| 192 | _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128), |
| 193 | _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES) |
| 194 | }; |
| 195 | |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 196 | struct z_erofs_bvec_iter { |
| 197 | struct page *bvpage; |
| 198 | struct z_erofs_bvset *bvset; |
| 199 | unsigned int nr, cur; |
| 200 | }; |
| 201 | |
| 202 | static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter) |
| 203 | { |
| 204 | if (iter->bvpage) |
| 205 | kunmap_local(iter->bvset); |
| 206 | return iter->bvpage; |
| 207 | } |
| 208 | |
| 209 | static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter) |
| 210 | { |
| 211 | unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec; |
| 212 | /* have to access nextpage in advance, otherwise it will be unmapped */ |
| 213 | struct page *nextpage = iter->bvset->nextpage; |
| 214 | struct page *oldpage; |
| 215 | |
| 216 | DBG_BUGON(!nextpage); |
| 217 | oldpage = z_erofs_bvec_iter_end(iter); |
| 218 | iter->bvpage = nextpage; |
| 219 | iter->bvset = kmap_local_page(nextpage); |
| 220 | iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec); |
| 221 | iter->cur = 0; |
| 222 | return oldpage; |
| 223 | } |
| 224 | |
| 225 | static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter, |
| 226 | struct z_erofs_bvset_inline *bvset, |
| 227 | unsigned int bootstrap_nr, |
| 228 | unsigned int cur) |
| 229 | { |
| 230 | *iter = (struct z_erofs_bvec_iter) { |
| 231 | .nr = bootstrap_nr, |
| 232 | .bvset = (struct z_erofs_bvset *)bvset, |
| 233 | }; |
| 234 | |
| 235 | while (cur > iter->nr) { |
| 236 | cur -= iter->nr; |
| 237 | z_erofs_bvset_flip(iter); |
| 238 | } |
| 239 | iter->cur = cur; |
| 240 | } |
| 241 | |
| 242 | static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter, |
| 243 | struct z_erofs_bvec *bvec, |
| 244 | struct page **candidate_bvpage) |
| 245 | { |
| 246 | if (iter->cur == iter->nr) { |
| 247 | if (!*candidate_bvpage) |
| 248 | return -EAGAIN; |
| 249 | |
| 250 | DBG_BUGON(iter->bvset->nextpage); |
| 251 | iter->bvset->nextpage = *candidate_bvpage; |
| 252 | z_erofs_bvset_flip(iter); |
| 253 | |
| 254 | iter->bvset->nextpage = NULL; |
| 255 | *candidate_bvpage = NULL; |
| 256 | } |
| 257 | iter->bvset->bvec[iter->cur++] = *bvec; |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter, |
| 262 | struct z_erofs_bvec *bvec, |
| 263 | struct page **old_bvpage) |
| 264 | { |
| 265 | if (iter->cur == iter->nr) |
| 266 | *old_bvpage = z_erofs_bvset_flip(iter); |
| 267 | else |
| 268 | *old_bvpage = NULL; |
| 269 | *bvec = iter->bvset->bvec[iter->cur++]; |
| 270 | } |
| 271 | |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 272 | static void z_erofs_destroy_pcluster_pool(void) |
| 273 | { |
| 274 | int i; |
| 275 | |
| 276 | for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { |
| 277 | if (!pcluster_pool[i].slab) |
| 278 | continue; |
| 279 | kmem_cache_destroy(pcluster_pool[i].slab); |
| 280 | pcluster_pool[i].slab = NULL; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | static int z_erofs_create_pcluster_pool(void) |
| 285 | { |
| 286 | struct z_erofs_pcluster_slab *pcs; |
| 287 | struct z_erofs_pcluster *a; |
| 288 | unsigned int size; |
| 289 | |
| 290 | for (pcs = pcluster_pool; |
| 291 | pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) { |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 292 | size = struct_size(a, compressed_bvecs, pcs->maxpages); |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 293 | |
| 294 | sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages); |
| 295 | pcs->slab = kmem_cache_create(pcs->name, size, 0, |
| 296 | SLAB_RECLAIM_ACCOUNT, NULL); |
| 297 | if (pcs->slab) |
| 298 | continue; |
| 299 | |
| 300 | z_erofs_destroy_pcluster_pool(); |
| 301 | return -ENOMEM; |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages) |
| 307 | { |
| 308 | int i; |
| 309 | |
| 310 | for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { |
| 311 | struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; |
| 312 | struct z_erofs_pcluster *pcl; |
| 313 | |
| 314 | if (nrpages > pcs->maxpages) |
| 315 | continue; |
| 316 | |
| 317 | pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS); |
| 318 | if (!pcl) |
| 319 | return ERR_PTR(-ENOMEM); |
| 320 | pcl->pclusterpages = nrpages; |
| 321 | return pcl; |
| 322 | } |
| 323 | return ERR_PTR(-EINVAL); |
| 324 | } |
| 325 | |
| 326 | static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl) |
| 327 | { |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 328 | unsigned int pclusterpages = z_erofs_pclusterpages(pcl); |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 329 | int i; |
| 330 | |
| 331 | for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { |
| 332 | struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; |
| 333 | |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 334 | if (pclusterpages > pcs->maxpages) |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 335 | continue; |
| 336 | |
| 337 | kmem_cache_free(pcs->slab, pcl); |
| 338 | return; |
| 339 | } |
| 340 | DBG_BUGON(1); |
| 341 | } |
| 342 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 343 | static struct workqueue_struct *z_erofs_workqueue __read_mostly; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 344 | |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 345 | #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD |
| 346 | static struct kthread_worker __rcu **z_erofs_pcpu_workers; |
| 347 | |
| 348 | static void erofs_destroy_percpu_workers(void) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 349 | { |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 350 | struct kthread_worker *worker; |
| 351 | unsigned int cpu; |
| 352 | |
| 353 | for_each_possible_cpu(cpu) { |
| 354 | worker = rcu_dereference_protected( |
| 355 | z_erofs_pcpu_workers[cpu], 1); |
| 356 | rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL); |
| 357 | if (worker) |
| 358 | kthread_destroy_worker(worker); |
| 359 | } |
| 360 | kfree(z_erofs_pcpu_workers); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 361 | } |
| 362 | |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 363 | static struct kthread_worker *erofs_init_percpu_worker(int cpu) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 364 | { |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 365 | struct kthread_worker *worker = |
| 366 | kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 367 | |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 368 | if (IS_ERR(worker)) |
| 369 | return worker; |
| 370 | if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI)) |
| 371 | sched_set_fifo_low(worker->task); |
| 372 | else |
| 373 | sched_set_normal(worker->task, 0); |
| 374 | return worker; |
| 375 | } |
| 376 | |
| 377 | static int erofs_init_percpu_workers(void) |
| 378 | { |
| 379 | struct kthread_worker *worker; |
| 380 | unsigned int cpu; |
| 381 | |
| 382 | z_erofs_pcpu_workers = kcalloc(num_possible_cpus(), |
| 383 | sizeof(struct kthread_worker *), GFP_ATOMIC); |
| 384 | if (!z_erofs_pcpu_workers) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */ |
| 388 | worker = erofs_init_percpu_worker(cpu); |
| 389 | if (!IS_ERR(worker)) |
| 390 | rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker); |
| 391 | } |
| 392 | return 0; |
| 393 | } |
| 394 | #else |
| 395 | static inline void erofs_destroy_percpu_workers(void) {} |
| 396 | static inline int erofs_init_percpu_workers(void) { return 0; } |
| 397 | #endif |
| 398 | |
| 399 | #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD) |
| 400 | static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock); |
| 401 | static enum cpuhp_state erofs_cpuhp_state; |
| 402 | |
| 403 | static int erofs_cpu_online(unsigned int cpu) |
| 404 | { |
| 405 | struct kthread_worker *worker, *old; |
| 406 | |
| 407 | worker = erofs_init_percpu_worker(cpu); |
| 408 | if (IS_ERR(worker)) |
| 409 | return PTR_ERR(worker); |
| 410 | |
| 411 | spin_lock(&z_erofs_pcpu_worker_lock); |
| 412 | old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu], |
| 413 | lockdep_is_held(&z_erofs_pcpu_worker_lock)); |
| 414 | if (!old) |
| 415 | rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker); |
| 416 | spin_unlock(&z_erofs_pcpu_worker_lock); |
| 417 | if (old) |
| 418 | kthread_destroy_worker(worker); |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static int erofs_cpu_offline(unsigned int cpu) |
| 423 | { |
| 424 | struct kthread_worker *worker; |
| 425 | |
| 426 | spin_lock(&z_erofs_pcpu_worker_lock); |
| 427 | worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu], |
| 428 | lockdep_is_held(&z_erofs_pcpu_worker_lock)); |
| 429 | rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL); |
| 430 | spin_unlock(&z_erofs_pcpu_worker_lock); |
| 431 | |
| 432 | synchronize_rcu(); |
| 433 | if (worker) |
| 434 | kthread_destroy_worker(worker); |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static int erofs_cpu_hotplug_init(void) |
| 439 | { |
| 440 | int state; |
| 441 | |
| 442 | state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
| 443 | "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline); |
| 444 | if (state < 0) |
| 445 | return state; |
| 446 | |
| 447 | erofs_cpuhp_state = state; |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static void erofs_cpu_hotplug_destroy(void) |
| 452 | { |
| 453 | if (erofs_cpuhp_state) |
| 454 | cpuhp_remove_state_nocalls(erofs_cpuhp_state); |
| 455 | } |
| 456 | #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */ |
| 457 | static inline int erofs_cpu_hotplug_init(void) { return 0; } |
| 458 | static inline void erofs_cpu_hotplug_destroy(void) {} |
| 459 | #endif |
| 460 | |
| 461 | void z_erofs_exit_zip_subsystem(void) |
| 462 | { |
| 463 | erofs_cpu_hotplug_destroy(); |
| 464 | erofs_destroy_percpu_workers(); |
| 465 | destroy_workqueue(z_erofs_workqueue); |
| 466 | z_erofs_destroy_pcluster_pool(); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 467 | } |
| 468 | |
Gao Xiang | 0a0b7e6 | 2018-10-09 21:43:53 +0800 | [diff] [blame] | 469 | int __init z_erofs_init_zip_subsystem(void) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 470 | { |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 471 | int err = z_erofs_create_pcluster_pool(); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 472 | |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 473 | if (err) |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 474 | goto out_error_pcluster_pool; |
| 475 | |
| 476 | z_erofs_workqueue = alloc_workqueue("erofs_worker", |
| 477 | WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus()); |
Dan Carpenter | 8d1b80a7 | 2023-02-16 15:13:04 +0300 | [diff] [blame] | 478 | if (!z_erofs_workqueue) { |
| 479 | err = -ENOMEM; |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 480 | goto out_error_workqueue_init; |
Dan Carpenter | 8d1b80a7 | 2023-02-16 15:13:04 +0300 | [diff] [blame] | 481 | } |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 482 | |
| 483 | err = erofs_init_percpu_workers(); |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 484 | if (err) |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 485 | goto out_error_pcpu_worker; |
| 486 | |
| 487 | err = erofs_cpu_hotplug_init(); |
| 488 | if (err < 0) |
| 489 | goto out_error_cpuhp_init; |
| 490 | return err; |
| 491 | |
| 492 | out_error_cpuhp_init: |
| 493 | erofs_destroy_percpu_workers(); |
| 494 | out_error_pcpu_worker: |
| 495 | destroy_workqueue(z_erofs_workqueue); |
| 496 | out_error_workqueue_init: |
| 497 | z_erofs_destroy_pcluster_pool(); |
| 498 | out_error_pcluster_pool: |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 499 | return err; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 500 | } |
| 501 | |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 502 | enum z_erofs_pclustermode { |
| 503 | Z_EROFS_PCLUSTER_INFLIGHT, |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 504 | /* |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 505 | * The current pclusters was the tail of an exist chain, in addition |
| 506 | * that the previous processed chained pclusters are all decided to |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 507 | * be hooked up to it. |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 508 | * A new chain will be created for the remaining pclusters which are |
| 509 | * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED, |
| 510 | * the next pcluster cannot reuse the whole page safely for inplace I/O |
| 511 | * in the following scenario: |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 512 | * ________________________________________________________________ |
| 513 | * | tail (partial) page | head (partial) page | |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 514 | * | (belongs to the next pcl) | (belongs to the current pcl) | |
| 515 | * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________| |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 516 | */ |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 517 | Z_EROFS_PCLUSTER_HOOKED, |
Gao Xiang | 0b96460 | 2021-03-22 02:32:27 +0800 | [diff] [blame] | 518 | /* |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 519 | * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it |
Gao Xiang | 0b96460 | 2021-03-22 02:32:27 +0800 | [diff] [blame] | 520 | * could be dispatched into bypass queue later due to uptodated managed |
| 521 | * pages. All related online pages cannot be reused for inplace I/O (or |
Gao Xiang | 387bab8 | 2022-07-15 23:41:52 +0800 | [diff] [blame] | 522 | * bvpage) since it can be directly decoded without I/O submission. |
Gao Xiang | 0b96460 | 2021-03-22 02:32:27 +0800 | [diff] [blame] | 523 | */ |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 524 | Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE, |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 525 | /* |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 526 | * The current collection has been linked with the owned chain, and |
| 527 | * could also be linked with the remaining collections, which means |
| 528 | * if the processing page is the tail page of the collection, thus |
| 529 | * the current collection can safely use the whole page (since |
| 530 | * the previous collection is under control) for in-place I/O, as |
| 531 | * illustrated below: |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 532 | * ________________________________________________________________ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 533 | * | tail (partial) page | head (partial) page | |
| 534 | * | (of the current cl) | (of the previous collection) | |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 535 | * | PCLUSTER_FOLLOWED or | | |
| 536 | * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________| |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 537 | * |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 538 | * [ (*) the above page can be used as inplace I/O. ] |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 539 | */ |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 540 | Z_EROFS_PCLUSTER_FOLLOWED, |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 541 | }; |
| 542 | |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 543 | struct z_erofs_decompress_frontend { |
| 544 | struct inode *const inode; |
| 545 | struct erofs_map_blocks map; |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 546 | struct z_erofs_bvec_iter biter; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 547 | |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 548 | struct page *candidate_bvpage; |
Gao Xiang | bfc4ccb | 2019-08-21 11:09:08 +0800 | [diff] [blame] | 549 | struct z_erofs_pcluster *pcl, *tailpcl; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 550 | z_erofs_next_pcluster_t owned_head; |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 551 | enum z_erofs_pclustermode mode; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 552 | |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 553 | bool readahead; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 554 | /* used for applying cache strategy on the fly */ |
| 555 | bool backmost; |
| 556 | erofs_off_t headoffset; |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 557 | |
| 558 | /* a pointer used to pick up inplace I/O pages */ |
| 559 | unsigned int icur; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 560 | }; |
| 561 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 562 | #define DECOMPRESS_FRONTEND_INIT(__i) { \ |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 563 | .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \ |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 564 | .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true } |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 565 | |
Gao Xiang | 1282dea | 2022-12-06 14:03:52 +0800 | [diff] [blame] | 566 | static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe) |
| 567 | { |
| 568 | unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy; |
| 569 | |
| 570 | if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) |
| 571 | return false; |
| 572 | |
| 573 | if (fe->backmost) |
| 574 | return true; |
| 575 | |
| 576 | if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND && |
| 577 | fe->map.m_la < fe->headoffset) |
| 578 | return true; |
| 579 | |
| 580 | return false; |
| 581 | } |
| 582 | |
Gao Xiang | 6f39d1e | 2022-03-02 03:49:51 +0800 | [diff] [blame] | 583 | static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe, |
Gao Xiang | 6f39d1e | 2022-03-02 03:49:51 +0800 | [diff] [blame] | 584 | struct page **pagepool) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 585 | { |
Gao Xiang | 6f39d1e | 2022-03-02 03:49:51 +0800 | [diff] [blame] | 586 | struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode)); |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 587 | struct z_erofs_pcluster *pcl = fe->pcl; |
Gao Xiang | 1282dea | 2022-12-06 14:03:52 +0800 | [diff] [blame] | 588 | bool shouldalloc = z_erofs_should_alloc_cache(fe); |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 589 | bool standalone = true; |
Gao Xiang | 6f39d1e | 2022-03-02 03:49:51 +0800 | [diff] [blame] | 590 | /* |
| 591 | * optimistic allocation without direct reclaim since inplace I/O |
| 592 | * can be used if low memory otherwise. |
| 593 | */ |
Gao Xiang | 1825c8d | 2020-12-09 20:37:17 +0800 | [diff] [blame] | 594 | gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | |
| 595 | __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 596 | unsigned int i; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 597 | |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 598 | if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED) |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 599 | return; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 600 | |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 601 | for (i = 0; i < pcl->pclusterpages; ++i) { |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 602 | struct page *page; |
Gao Xiang | b1ed220 | 2023-02-04 17:30:37 +0800 | [diff] [blame] | 603 | void *t; /* mark pages just found for debugging */ |
Gao Xiang | 1825c8d | 2020-12-09 20:37:17 +0800 | [diff] [blame] | 604 | struct page *newpage = NULL; |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 605 | |
| 606 | /* the compressed page was loaded before */ |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 607 | if (READ_ONCE(pcl->compressed_bvecs[i].page)) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 608 | continue; |
| 609 | |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 610 | page = find_get_page(mc, pcl->obj.index + i); |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 611 | |
| 612 | if (page) { |
Gao Xiang | b1ed220 | 2023-02-04 17:30:37 +0800 | [diff] [blame] | 613 | t = (void *)((unsigned long)page | 1); |
Gao Xiang | 0b96460 | 2021-03-22 02:32:27 +0800 | [diff] [blame] | 614 | } else { |
| 615 | /* I/O is needed, no possible to decompress directly */ |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 616 | standalone = false; |
Gao Xiang | 1282dea | 2022-12-06 14:03:52 +0800 | [diff] [blame] | 617 | if (!shouldalloc) |
Gao Xiang | 0b96460 | 2021-03-22 02:32:27 +0800 | [diff] [blame] | 618 | continue; |
Gao Xiang | 1282dea | 2022-12-06 14:03:52 +0800 | [diff] [blame] | 619 | |
| 620 | /* |
| 621 | * try to use cached I/O if page allocation |
| 622 | * succeeds or fallback to in-place I/O instead |
| 623 | * to avoid any direct reclaim. |
| 624 | */ |
| 625 | newpage = erofs_allocpage(pagepool, gfp); |
| 626 | if (!newpage) |
| 627 | continue; |
| 628 | set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE); |
Gao Xiang | b1ed220 | 2023-02-04 17:30:37 +0800 | [diff] [blame] | 629 | t = (void *)((unsigned long)newpage | 1); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 630 | } |
| 631 | |
Gao Xiang | b1ed220 | 2023-02-04 17:30:37 +0800 | [diff] [blame] | 632 | if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t)) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 633 | continue; |
| 634 | |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 635 | if (page) |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 636 | put_page(page); |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 637 | else if (newpage) |
| 638 | erofs_pagepool_add(pagepool, newpage); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 639 | } |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 640 | |
Gao Xiang | 0b96460 | 2021-03-22 02:32:27 +0800 | [diff] [blame] | 641 | /* |
| 642 | * don't do inplace I/O if all compressed pages are available in |
| 643 | * managed cache since it can be moved to the bypass queue instead. |
| 644 | */ |
| 645 | if (standalone) |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 646 | fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /* called by erofs_shrinker to get rid of all compressed_pages */ |
Gao Xiang | 47e541a | 2018-07-29 13:34:58 +0800 | [diff] [blame] | 650 | 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] | 651 | struct erofs_workgroup *grp) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 652 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 653 | struct z_erofs_pcluster *const pcl = |
| 654 | container_of(grp, struct z_erofs_pcluster, obj); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 655 | int i; |
| 656 | |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 657 | DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 658 | /* |
| 659 | * refcount of workgroup is now freezed as 1, |
| 660 | * therefore no need to worry about available decompression users. |
| 661 | */ |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 662 | for (i = 0; i < pcl->pclusterpages; ++i) { |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 663 | struct page *page = pcl->compressed_bvecs[i].page; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 664 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 665 | if (!page) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 666 | continue; |
| 667 | |
| 668 | /* block other users from reclaiming or migrating the page */ |
| 669 | if (!trylock_page(page)) |
| 670 | return -EBUSY; |
| 671 | |
Yue Hu | f4d4e5f | 2021-08-10 14:54:50 +0800 | [diff] [blame] | 672 | if (!erofs_page_is_managed(sbi, page)) |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 673 | continue; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 674 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 675 | /* barrier is implied in the following 'unlock_page' */ |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 676 | WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame] | 677 | detach_page_private(page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 678 | unlock_page(page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | |
Yue Hu | d252ff3 | 2021-08-10 15:24:16 +0800 | [diff] [blame] | 683 | int erofs_try_to_free_cached_page(struct page *page) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 684 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 685 | struct z_erofs_pcluster *const pcl = (void *)page_private(page); |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 686 | int ret, i; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 687 | |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 688 | if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1)) |
| 689 | return 0; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 690 | |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 691 | ret = 0; |
| 692 | DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); |
| 693 | for (i = 0; i < pcl->pclusterpages; ++i) { |
| 694 | if (pcl->compressed_bvecs[i].page == page) { |
| 695 | WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); |
| 696 | ret = 1; |
| 697 | break; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 698 | } |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 699 | } |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 700 | erofs_workgroup_unfreeze(&pcl->obj, 1); |
| 701 | if (ret) |
| 702 | detach_page_private(page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 703 | return ret; |
| 704 | } |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 705 | |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 706 | static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe, |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 707 | struct z_erofs_bvec *bvec) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 708 | { |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 709 | struct z_erofs_pcluster *const pcl = fe->pcl; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 710 | |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 711 | while (fe->icur > 0) { |
| 712 | if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page, |
| 713 | NULL, bvec->page)) { |
| 714 | pcl->compressed_bvecs[fe->icur] = *bvec; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 715 | return true; |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 716 | } |
| 717 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 718 | return false; |
| 719 | } |
| 720 | |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 721 | /* callers must be with pcluster lock held */ |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 722 | static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe, |
Gao Xiang | 5b220b2 | 2022-07-15 23:41:56 +0800 | [diff] [blame] | 723 | struct z_erofs_bvec *bvec, bool exclusive) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 724 | { |
| 725 | int ret; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 726 | |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 727 | if (exclusive) { |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 728 | /* give priority for inplaceio to use file pages first */ |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 729 | if (z_erofs_try_inplace_io(fe, bvec)) |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 730 | return 0; |
| 731 | /* otherwise, check if it can be used as a bvpage */ |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 732 | if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED && |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 733 | !fe->candidate_bvpage) |
| 734 | fe->candidate_bvpage = bvec->page; |
| 735 | } |
| 736 | ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage); |
| 737 | fe->pcl->vcnt += (ret >= 0); |
| 738 | return ret; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 739 | } |
| 740 | |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 741 | static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 742 | { |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 743 | struct z_erofs_pcluster *pcl = f->pcl; |
| 744 | z_erofs_next_pcluster_t *owned_head = &f->owned_head; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 745 | |
Gao Xiang | 473e15b | 2020-12-08 17:58:34 +0800 | [diff] [blame] | 746 | /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ |
| 747 | if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, |
| 748 | *owned_head) == Z_EROFS_PCLUSTER_NIL) { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 749 | *owned_head = &pcl->next; |
Gao Xiang | 473e15b | 2020-12-08 17:58:34 +0800 | [diff] [blame] | 750 | /* so we can attach this pcluster to our submission chain. */ |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 751 | f->mode = Z_EROFS_PCLUSTER_FOLLOWED; |
Gao Xiang | 473e15b | 2020-12-08 17:58:34 +0800 | [diff] [blame] | 752 | return; |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 753 | } |
Gao Xiang | 473e15b | 2020-12-08 17:58:34 +0800 | [diff] [blame] | 754 | |
| 755 | /* |
| 756 | * type 2, link to the end of an existing open chain, be careful |
| 757 | * that its submission is controlled by the original attached chain. |
| 758 | */ |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 759 | if (*owned_head != &pcl->next && pcl != f->tailpcl && |
| 760 | cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, |
Gao Xiang | 473e15b | 2020-12-08 17:58:34 +0800 | [diff] [blame] | 761 | *owned_head) == Z_EROFS_PCLUSTER_TAIL) { |
| 762 | *owned_head = Z_EROFS_PCLUSTER_TAIL; |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 763 | f->mode = Z_EROFS_PCLUSTER_HOOKED; |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 764 | f->tailpcl = NULL; |
Gao Xiang | 473e15b | 2020-12-08 17:58:34 +0800 | [diff] [blame] | 765 | return; |
| 766 | } |
| 767 | /* type 3, it belongs to a chain, but it isn't the end of the chain */ |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 768 | f->mode = Z_EROFS_PCLUSTER_INFLIGHT; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 769 | } |
| 770 | |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 771 | static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 772 | { |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 773 | struct erofs_map_blocks *map = &fe->map; |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 774 | bool ztailpacking = map->m_flags & EROFS_MAP_META; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 775 | struct z_erofs_pcluster *pcl; |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 776 | struct erofs_workgroup *grp; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 777 | int err; |
Gao Xiang | e5e3abb | 2018-09-19 13:49:07 +0800 | [diff] [blame] | 778 | |
Chen Zhongjin | c42c0ff | 2022-12-05 11:49:57 +0800 | [diff] [blame] | 779 | if (!(map->m_flags & EROFS_MAP_ENCODED) || |
| 780 | (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) { |
Gao Xiang | 8f89926 | 2021-10-09 04:08:37 +0800 | [diff] [blame] | 781 | DBG_BUGON(1); |
| 782 | return -EFSCORRUPTED; |
| 783 | } |
| 784 | |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 785 | /* no available pcluster, let's allocate one */ |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 786 | pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 : |
| 787 | map->m_plen >> PAGE_SHIFT); |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 788 | if (IS_ERR(pcl)) |
| 789 | return PTR_ERR(pcl); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 790 | |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 791 | atomic_set(&pcl->obj.refcount, 1); |
Gao Xiang | 8f89926 | 2021-10-09 04:08:37 +0800 | [diff] [blame] | 792 | pcl->algorithmformat = map->m_algorithmformat; |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 793 | pcl->length = 0; |
| 794 | pcl->partial = true; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 795 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 796 | /* new pclusters should be claimed as type 1, primary and followed */ |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 797 | pcl->next = fe->owned_head; |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 798 | pcl->pageofs_out = map->m_la & ~PAGE_MASK; |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 799 | fe->mode = Z_EROFS_PCLUSTER_FOLLOWED; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 800 | |
Gao Xiang | 23edf3a | 2018-11-23 01:21:47 +0800 | [diff] [blame] | 801 | /* |
| 802 | * lock all primary followed works before visible to others |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 803 | * and mutex_trylock *never* fails for a new pcluster. |
Gao Xiang | 23edf3a | 2018-11-23 01:21:47 +0800 | [diff] [blame] | 804 | */ |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 805 | mutex_init(&pcl->lock); |
| 806 | DBG_BUGON(!mutex_trylock(&pcl->lock)); |
Gao Xiang | 23edf3a | 2018-11-23 01:21:47 +0800 | [diff] [blame] | 807 | |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 808 | if (ztailpacking) { |
| 809 | pcl->obj.index = 0; /* which indicates ztailpacking */ |
| 810 | pcl->pageofs_in = erofs_blkoff(map->m_pa); |
| 811 | pcl->tailpacking_size = map->m_plen; |
| 812 | } else { |
| 813 | pcl->obj.index = map->m_pa >> PAGE_SHIFT; |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 814 | |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 815 | grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj); |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 816 | if (IS_ERR(grp)) { |
| 817 | err = PTR_ERR(grp); |
| 818 | goto err_out; |
| 819 | } |
| 820 | |
| 821 | if (grp != &pcl->obj) { |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 822 | fe->pcl = container_of(grp, |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 823 | struct z_erofs_pcluster, obj); |
| 824 | err = -EEXIST; |
| 825 | goto err_out; |
| 826 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 827 | } |
Gao Xiang | bfc4ccb | 2019-08-21 11:09:08 +0800 | [diff] [blame] | 828 | /* used to check tail merging loop due to corrupted images */ |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 829 | if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) |
| 830 | fe->tailpcl = pcl; |
| 831 | fe->owned_head = &pcl->next; |
| 832 | fe->pcl = pcl; |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 833 | return 0; |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 834 | |
| 835 | err_out: |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 836 | mutex_unlock(&pcl->lock); |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 837 | z_erofs_free_pcluster(pcl); |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 838 | return err; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 839 | } |
| 840 | |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 841 | static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 842 | { |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 843 | struct erofs_map_blocks *map = &fe->map; |
Gao Xiang | 0d823b4 | 2022-07-15 23:41:49 +0800 | [diff] [blame] | 844 | struct erofs_workgroup *grp = NULL; |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 845 | int ret; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 846 | |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 847 | DBG_BUGON(fe->pcl); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 848 | |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 849 | /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */ |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 850 | DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL); |
| 851 | DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 852 | |
Gao Xiang | 0d823b4 | 2022-07-15 23:41:49 +0800 | [diff] [blame] | 853 | if (!(map->m_flags & EROFS_MAP_META)) { |
| 854 | grp = erofs_find_workgroup(fe->inode->i_sb, |
| 855 | map->m_pa >> PAGE_SHIFT); |
| 856 | } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) { |
| 857 | DBG_BUGON(1); |
| 858 | return -EFSCORRUPTED; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 859 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 860 | |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 861 | if (grp) { |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 862 | fe->pcl = container_of(grp, struct z_erofs_pcluster, obj); |
Gao Xiang | 0d823b4 | 2022-07-15 23:41:49 +0800 | [diff] [blame] | 863 | ret = -EEXIST; |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 864 | } else { |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 865 | ret = z_erofs_register_pcluster(fe); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 866 | } |
| 867 | |
Gao Xiang | 0d823b4 | 2022-07-15 23:41:49 +0800 | [diff] [blame] | 868 | if (ret == -EEXIST) { |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 869 | mutex_lock(&fe->pcl->lock); |
| 870 | /* used to check tail merging loop due to corrupted images */ |
| 871 | if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) |
| 872 | fe->tailpcl = fe->pcl; |
| 873 | |
| 874 | z_erofs_try_to_claim_pcluster(fe); |
Gao Xiang | 0d823b4 | 2022-07-15 23:41:49 +0800 | [diff] [blame] | 875 | } else if (ret) { |
Gao Xiang | 9e579fc | 2019-10-08 20:56:12 +0800 | [diff] [blame] | 876 | return ret; |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 877 | } |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 878 | z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset, |
Gao Xiang | 387bab8 | 2022-07-15 23:41:52 +0800 | [diff] [blame] | 879 | Z_EROFS_INLINE_BVECS, fe->pcl->vcnt); |
Gao Xiang | 81382f5 | 2021-04-07 12:39:21 +0800 | [diff] [blame] | 880 | /* since file-backed online pages are traversed in reverse order */ |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 881 | fe->icur = z_erofs_pclusterpages(fe->pcl); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | /* |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 886 | * keep in mind that no referenced pclusters will be freed |
| 887 | * only after a RCU grace period. |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 888 | */ |
| 889 | static void z_erofs_rcu_callback(struct rcu_head *head) |
| 890 | { |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 891 | z_erofs_free_pcluster(container_of(head, |
| 892 | struct z_erofs_pcluster, rcu)); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) |
| 896 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 897 | struct z_erofs_pcluster *const pcl = |
| 898 | container_of(grp, struct z_erofs_pcluster, obj); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 899 | |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 900 | call_rcu(&pcl->rcu, z_erofs_rcu_callback); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 901 | } |
| 902 | |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 903 | static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 904 | { |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 905 | struct z_erofs_pcluster *pcl = fe->pcl; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 906 | |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 907 | if (!pcl) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 908 | return false; |
| 909 | |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 910 | z_erofs_bvec_iter_end(&fe->biter); |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 911 | mutex_unlock(&pcl->lock); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 912 | |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 913 | if (fe->candidate_bvpage) { |
| 914 | DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage)); |
| 915 | fe->candidate_bvpage = NULL; |
| 916 | } |
| 917 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 918 | /* |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 919 | * if all pending pages are added, don't hold its reference |
| 920 | * any longer if the pcluster isn't hosted by ourselves. |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 921 | */ |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 922 | if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE) |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 923 | erofs_workgroup_put(&pcl->obj); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 924 | |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 925 | fe->pcl = NULL; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 926 | return true; |
| 927 | } |
| 928 | |
Yue Hu | b15b2e3 | 2022-09-23 10:11:22 +0800 | [diff] [blame] | 929 | static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos, |
| 930 | struct page *page, unsigned int pageofs, |
| 931 | unsigned int len) |
| 932 | { |
| 933 | struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode; |
| 934 | struct erofs_buf buf = __EROFS_BUF_INITIALIZER; |
| 935 | u8 *src, *dst; |
| 936 | unsigned int i, cnt; |
| 937 | |
Yue Hu | e5126de | 2022-10-21 16:53:25 +0800 | [diff] [blame] | 938 | if (!packed_inode) |
| 939 | return -EFSCORRUPTED; |
| 940 | |
Yue Hu | b15b2e3 | 2022-09-23 10:11:22 +0800 | [diff] [blame] | 941 | pos += EROFS_I(inode)->z_fragmentoff; |
| 942 | for (i = 0; i < len; i += cnt) { |
| 943 | cnt = min_t(unsigned int, len - i, |
| 944 | EROFS_BLKSIZ - erofs_blkoff(pos)); |
| 945 | src = erofs_bread(&buf, packed_inode, |
| 946 | erofs_blknr(pos), EROFS_KMAP); |
| 947 | if (IS_ERR(src)) { |
| 948 | erofs_put_metabuf(&buf); |
| 949 | return PTR_ERR(src); |
| 950 | } |
| 951 | |
| 952 | dst = kmap_local_page(page); |
| 953 | memcpy(dst + pageofs + i, src + erofs_blkoff(pos), cnt); |
| 954 | kunmap_local(dst); |
| 955 | pos += cnt; |
| 956 | } |
| 957 | erofs_put_metabuf(&buf); |
| 958 | return 0; |
| 959 | } |
| 960 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 961 | 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] | 962 | struct page *page, struct page **pagepool) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 963 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 964 | struct inode *const inode = fe->inode; |
Chao Yu | 3b42341 | 2019-01-15 09:42:21 +0800 | [diff] [blame] | 965 | struct erofs_map_blocks *const map = &fe->map; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 966 | const loff_t offset = page_offset(page); |
Gao Xiang | 5b220b2 | 2022-07-15 23:41:56 +0800 | [diff] [blame] | 967 | bool tight = true, exclusive; |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 968 | unsigned int cur, end, spiltted; |
Gao Xiang | 1e05ff3 | 2018-09-18 22:27:25 +0800 | [diff] [blame] | 969 | int err = 0; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 970 | |
| 971 | /* register locked file pages as online pages in pack */ |
| 972 | z_erofs_onlinepage_init(page); |
| 973 | |
| 974 | spiltted = 0; |
| 975 | end = PAGE_SIZE; |
| 976 | repeat: |
| 977 | cur = end - 1; |
| 978 | |
Gao Xiang | 39397a4 | 2022-05-29 13:54:24 +0800 | [diff] [blame] | 979 | if (offset + cur < map->m_la || |
| 980 | offset + cur >= map->m_la + map->m_llen) { |
| 981 | erofs_dbg("out-of-range map @ pos %llu", offset + cur); |
| 982 | |
| 983 | if (z_erofs_collector_end(fe)) |
| 984 | fe->backmost = false; |
| 985 | map->m_la = offset + cur; |
| 986 | map->m_llen = 0; |
| 987 | err = z_erofs_map_blocks_iter(inode, map, 0); |
| 988 | if (err) |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 989 | goto out; |
Gao Xiang | 39397a4 | 2022-05-29 13:54:24 +0800 | [diff] [blame] | 990 | } else { |
| 991 | if (fe->pcl) |
| 992 | goto hitted; |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 993 | /* didn't get a valid pcluster previously (very rare) */ |
Gao Xiang | 1e5ceea | 2019-02-27 13:33:31 +0800 | [diff] [blame] | 994 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 995 | |
Yue Hu | b15b2e3 | 2022-09-23 10:11:22 +0800 | [diff] [blame] | 996 | if (!(map->m_flags & EROFS_MAP_MAPPED) || |
| 997 | map->m_flags & EROFS_MAP_FRAGMENT) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 998 | goto hitted; |
| 999 | |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 1000 | err = z_erofs_collector_begin(fe); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 1001 | if (err) |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 1002 | goto out; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1003 | |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 1004 | if (z_erofs_is_inline_pcluster(fe->pcl)) { |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame] | 1005 | void *mp; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 1006 | |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame] | 1007 | mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb, |
| 1008 | erofs_blknr(map->m_pa), EROFS_NO_KMAP); |
| 1009 | if (IS_ERR(mp)) { |
| 1010 | err = PTR_ERR(mp); |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1011 | erofs_err(inode->i_sb, |
| 1012 | "failed to get inline page, err %d", err); |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 1013 | goto out; |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1014 | } |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame] | 1015 | get_page(fe->map.buf.page); |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1016 | WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, |
| 1017 | fe->map.buf.page); |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 1018 | fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1019 | } else { |
Gao Xiang | 6f39d1e | 2022-03-02 03:49:51 +0800 | [diff] [blame] | 1020 | /* bind cache first when cached decompression is preferred */ |
Gao Xiang | 1282dea | 2022-12-06 14:03:52 +0800 | [diff] [blame] | 1021 | z_erofs_bind_cache(fe, pagepool); |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1022 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1023 | hitted: |
Gao Xiang | dc76ea8 | 2019-09-22 18:04:34 +0800 | [diff] [blame] | 1024 | /* |
| 1025 | * Ensure the current partial page belongs to this submit chain rather |
| 1026 | * than other concurrent submit chains or the noio(bypass) chain since |
| 1027 | * those chains are handled asynchronously thus the page cannot be used |
Gao Xiang | 387bab8 | 2022-07-15 23:41:52 +0800 | [diff] [blame] | 1028 | * for inplace I/O or bvpage (should be processed in a strict order.) |
Gao Xiang | dc76ea8 | 2019-09-22 18:04:34 +0800 | [diff] [blame] | 1029 | */ |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 1030 | tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED && |
| 1031 | fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE); |
Gao Xiang | dc76ea8 | 2019-09-22 18:04:34 +0800 | [diff] [blame] | 1032 | |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 1033 | cur = end - min_t(unsigned int, offset + end - map->m_la, end); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 1034 | if (!(map->m_flags & EROFS_MAP_MAPPED)) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1035 | zero_user_segment(page, cur, end); |
| 1036 | goto next_part; |
| 1037 | } |
Yue Hu | b15b2e3 | 2022-09-23 10:11:22 +0800 | [diff] [blame] | 1038 | if (map->m_flags & EROFS_MAP_FRAGMENT) { |
| 1039 | unsigned int pageofs, skip, len; |
| 1040 | |
| 1041 | if (offset > map->m_la) { |
| 1042 | pageofs = 0; |
| 1043 | skip = offset - map->m_la; |
| 1044 | } else { |
| 1045 | pageofs = map->m_la & ~PAGE_MASK; |
| 1046 | skip = 0; |
| 1047 | } |
| 1048 | len = min_t(unsigned int, map->m_llen - skip, end - cur); |
| 1049 | err = z_erofs_read_fragment(inode, skip, page, pageofs, len); |
| 1050 | if (err) |
| 1051 | goto out; |
| 1052 | ++spiltted; |
| 1053 | tight = false; |
| 1054 | goto next_part; |
| 1055 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1056 | |
Gao Xiang | 5b220b2 | 2022-07-15 23:41:56 +0800 | [diff] [blame] | 1057 | exclusive = (!cur && (!spiltted || tight)); |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 1058 | if (cur) |
Gao Xiang | db166fc | 2022-07-15 23:41:57 +0800 | [diff] [blame] | 1059 | tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED); |
Gao Xiang | a112152 | 2019-02-27 13:33:32 +0800 | [diff] [blame] | 1060 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1061 | retry: |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1062 | err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) { |
| 1063 | .page = page, |
| 1064 | .offset = offset - map->m_la, |
| 1065 | .end = end, |
Gao Xiang | 5b220b2 | 2022-07-15 23:41:56 +0800 | [diff] [blame] | 1066 | }), exclusive); |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1067 | /* should allocate an additional short-lived page for bvset */ |
| 1068 | if (err == -EAGAIN && !fe->candidate_bvpage) { |
| 1069 | fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL); |
| 1070 | set_page_private(fe->candidate_bvpage, |
| 1071 | Z_EROFS_SHORTLIVED_PAGE); |
| 1072 | goto retry; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1075 | if (err) { |
| 1076 | DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage); |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 1077 | goto out; |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1078 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1079 | |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 1080 | z_erofs_onlinepage_split(page); |
Gao Xiang | 1e05ff3 | 2018-09-18 22:27:25 +0800 | [diff] [blame] | 1081 | /* bump up the number of spiltted parts of a page */ |
| 1082 | ++spiltted; |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1083 | if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) |
| 1084 | fe->pcl->multibases = true; |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1085 | if (fe->pcl->length < offset + end - map->m_la) { |
| 1086 | fe->pcl->length = offset + end - map->m_la; |
| 1087 | fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK; |
| 1088 | } |
Gao Xiang | e793327 | 2022-10-14 14:49:15 +0800 | [diff] [blame] | 1089 | if ((map->m_flags & EROFS_MAP_FULL_MAPPED) && |
| 1090 | !(map->m_flags & EROFS_MAP_PARTIAL_REF) && |
| 1091 | fe->pcl->length == map->m_llen) |
| 1092 | fe->pcl->partial = false; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1093 | next_part: |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1094 | /* shorten the remaining extent to update progress */ |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1095 | map->m_llen = offset + cur - map->m_la; |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1096 | map->m_flags &= ~EROFS_MAP_FULL_MAPPED; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1097 | |
Kristaps ÄŒivkulis | 2bc7596 | 2018-08-05 18:21:01 +0300 | [diff] [blame] | 1098 | end = cur; |
| 1099 | if (end > 0) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1100 | goto repeat; |
| 1101 | |
Gao Xiang | 1e05ff3 | 2018-09-18 22:27:25 +0800 | [diff] [blame] | 1102 | out: |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 1103 | if (err) |
| 1104 | z_erofs_page_mark_eio(page); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1105 | z_erofs_onlinepage_endio(page); |
| 1106 | |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 1107 | erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", |
| 1108 | __func__, page, spiltted, map->m_llen); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1109 | return err; |
| 1110 | } |
| 1111 | |
Huang Jianan | 40452ff | 2021-12-06 22:35:52 +0800 | [diff] [blame] | 1112 | static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi, |
| 1113 | unsigned int readahead_pages) |
| 1114 | { |
Matthew Wilcox (Oracle) | a2e20a2 | 2022-04-29 11:12:16 -0400 | [diff] [blame] | 1115 | /* auto: enable for read_folio, disable for readahead */ |
Huang Jianan | 40452ff | 2021-12-06 22:35:52 +0800 | [diff] [blame] | 1116 | if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) && |
| 1117 | !readahead_pages) |
| 1118 | return true; |
| 1119 | |
| 1120 | if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) && |
| 1121 | (readahead_pages <= sbi->opt.max_sync_decompress_pages)) |
| 1122 | return true; |
| 1123 | |
| 1124 | return false; |
| 1125 | } |
| 1126 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame] | 1127 | static bool z_erofs_page_is_invalidated(struct page *page) |
| 1128 | { |
| 1129 | return !page->mapping && !z_erofs_is_shortlived_page(page); |
| 1130 | } |
| 1131 | |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1132 | struct z_erofs_decompress_backend { |
| 1133 | struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES]; |
| 1134 | struct super_block *sb; |
| 1135 | struct z_erofs_pcluster *pcl; |
| 1136 | |
| 1137 | /* pages with the longest decompressed length for deduplication */ |
| 1138 | struct page **decompressed_pages; |
| 1139 | /* pages to keep the compressed data */ |
| 1140 | struct page **compressed_pages; |
| 1141 | |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1142 | struct list_head decompressed_secondary_bvecs; |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1143 | struct page **pagepool; |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1144 | unsigned int onstack_used, nr_pages; |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1145 | }; |
| 1146 | |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1147 | struct z_erofs_bvec_item { |
| 1148 | struct z_erofs_bvec bvec; |
| 1149 | struct list_head list; |
| 1150 | }; |
| 1151 | |
| 1152 | static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be, |
| 1153 | struct z_erofs_bvec *bvec) |
Gao Xiang | 3fe96ee | 2022-07-15 23:42:01 +0800 | [diff] [blame] | 1154 | { |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1155 | struct z_erofs_bvec_item *item; |
Gao Xiang | 3fe96ee | 2022-07-15 23:42:01 +0800 | [diff] [blame] | 1156 | |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1157 | if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) { |
| 1158 | unsigned int pgnr; |
Gao Xiang | 3fe96ee | 2022-07-15 23:42:01 +0800 | [diff] [blame] | 1159 | |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1160 | pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT; |
| 1161 | DBG_BUGON(pgnr >= be->nr_pages); |
Gao Xiang | 63bbb85 | 2022-10-12 12:50:56 +0800 | [diff] [blame] | 1162 | if (!be->decompressed_pages[pgnr]) { |
| 1163 | be->decompressed_pages[pgnr] = bvec->page; |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1164 | return; |
Gao Xiang | 63bbb85 | 2022-10-12 12:50:56 +0800 | [diff] [blame] | 1165 | } |
Gao Xiang | 3fe96ee | 2022-07-15 23:42:01 +0800 | [diff] [blame] | 1166 | } |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1167 | |
| 1168 | /* (cold path) one pcluster is requested multiple times */ |
| 1169 | item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL); |
| 1170 | item->bvec = *bvec; |
| 1171 | list_add(&item->list, &be->decompressed_secondary_bvecs); |
Gao Xiang | 3fe96ee | 2022-07-15 23:42:01 +0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1174 | static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be, |
| 1175 | int err) |
| 1176 | { |
| 1177 | unsigned int off0 = be->pcl->pageofs_out; |
| 1178 | struct list_head *p, *n; |
| 1179 | |
| 1180 | list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) { |
| 1181 | struct z_erofs_bvec_item *bvi; |
| 1182 | unsigned int end, cur; |
| 1183 | void *dst, *src; |
| 1184 | |
| 1185 | bvi = container_of(p, struct z_erofs_bvec_item, list); |
| 1186 | cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0; |
| 1187 | end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset, |
| 1188 | bvi->bvec.end); |
| 1189 | dst = kmap_local_page(bvi->bvec.page); |
| 1190 | while (cur < end) { |
| 1191 | unsigned int pgnr, scur, len; |
| 1192 | |
| 1193 | pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT; |
| 1194 | DBG_BUGON(pgnr >= be->nr_pages); |
| 1195 | |
| 1196 | scur = bvi->bvec.offset + cur - |
| 1197 | ((pgnr << PAGE_SHIFT) - off0); |
| 1198 | len = min_t(unsigned int, end - cur, PAGE_SIZE - scur); |
| 1199 | if (!be->decompressed_pages[pgnr]) { |
| 1200 | err = -EFSCORRUPTED; |
| 1201 | cur += len; |
| 1202 | continue; |
| 1203 | } |
| 1204 | src = kmap_local_page(be->decompressed_pages[pgnr]); |
| 1205 | memcpy(dst + cur, src + scur, len); |
| 1206 | kunmap_local(src); |
| 1207 | cur += len; |
| 1208 | } |
| 1209 | kunmap_local(dst); |
| 1210 | if (err) |
| 1211 | z_erofs_page_mark_eio(bvi->bvec.page); |
| 1212 | z_erofs_onlinepage_endio(bvi->bvec.page); |
| 1213 | list_del(p); |
| 1214 | kfree(bvi); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be) |
Gao Xiang | 42fec23 | 2022-07-15 23:41:50 +0800 | [diff] [blame] | 1219 | { |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1220 | struct z_erofs_pcluster *pcl = be->pcl; |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1221 | struct z_erofs_bvec_iter biter; |
| 1222 | struct page *old_bvpage; |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1223 | int i; |
Gao Xiang | 42fec23 | 2022-07-15 23:41:50 +0800 | [diff] [blame] | 1224 | |
Gao Xiang | 387bab8 | 2022-07-15 23:41:52 +0800 | [diff] [blame] | 1225 | z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0); |
Gao Xiang | 42fec23 | 2022-07-15 23:41:50 +0800 | [diff] [blame] | 1226 | for (i = 0; i < pcl->vcnt; ++i) { |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1227 | struct z_erofs_bvec bvec; |
Gao Xiang | 42fec23 | 2022-07-15 23:41:50 +0800 | [diff] [blame] | 1228 | |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1229 | z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage); |
Gao Xiang | 42fec23 | 2022-07-15 23:41:50 +0800 | [diff] [blame] | 1230 | |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1231 | if (old_bvpage) |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1232 | z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); |
Gao Xiang | 42fec23 | 2022-07-15 23:41:50 +0800 | [diff] [blame] | 1233 | |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1234 | DBG_BUGON(z_erofs_page_is_invalidated(bvec.page)); |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1235 | z_erofs_do_decompressed_bvec(be, &bvec); |
Gao Xiang | 42fec23 | 2022-07-15 23:41:50 +0800 | [diff] [blame] | 1236 | } |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1237 | |
| 1238 | old_bvpage = z_erofs_bvec_iter_end(&biter); |
| 1239 | if (old_bvpage) |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1240 | z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); |
Gao Xiang | 42fec23 | 2022-07-15 23:41:50 +0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1243 | static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be, |
| 1244 | bool *overlapped) |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1245 | { |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1246 | struct z_erofs_pcluster *pcl = be->pcl; |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1247 | unsigned int pclusterpages = z_erofs_pclusterpages(pcl); |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1248 | int i, err = 0; |
| 1249 | |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1250 | *overlapped = false; |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1251 | for (i = 0; i < pclusterpages; ++i) { |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1252 | struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i]; |
| 1253 | struct page *page = bvec->page; |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1254 | |
| 1255 | /* compressed pages ought to be present before decompressing */ |
| 1256 | if (!page) { |
| 1257 | DBG_BUGON(1); |
| 1258 | continue; |
| 1259 | } |
Gao Xiang | fe3e591 | 2022-07-15 23:42:00 +0800 | [diff] [blame] | 1260 | be->compressed_pages[i] = page; |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1261 | |
| 1262 | if (z_erofs_is_inline_pcluster(pcl)) { |
| 1263 | if (!PageUptodate(page)) |
| 1264 | err = -EIO; |
| 1265 | continue; |
| 1266 | } |
| 1267 | |
| 1268 | DBG_BUGON(z_erofs_page_is_invalidated(page)); |
| 1269 | if (!z_erofs_is_shortlived_page(page)) { |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1270 | if (erofs_page_is_managed(EROFS_SB(be->sb), page)) { |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1271 | if (!PageUptodate(page)) |
| 1272 | err = -EIO; |
| 1273 | continue; |
| 1274 | } |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1275 | z_erofs_do_decompressed_bvec(be, bvec); |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1276 | *overlapped = true; |
| 1277 | } |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1278 | } |
| 1279 | |
Gao Xiang | fe3e591 | 2022-07-15 23:42:00 +0800 | [diff] [blame] | 1280 | if (err) |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1281 | return err; |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1282 | return 0; |
Gao Xiang | 67139e3 | 2022-07-15 23:41:53 +0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1285 | static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be, |
| 1286 | int err) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1287 | { |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1288 | struct erofs_sb_info *const sbi = EROFS_SB(be->sb); |
| 1289 | struct z_erofs_pcluster *pcl = be->pcl; |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1290 | unsigned int pclusterpages = z_erofs_pclusterpages(pcl); |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1291 | unsigned int i, inputsize; |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 1292 | int err2; |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1293 | struct page *page; |
| 1294 | bool overlapped; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1295 | |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 1296 | mutex_lock(&pcl->lock); |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1297 | be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1298 | |
Gao Xiang | fe3e591 | 2022-07-15 23:42:00 +0800 | [diff] [blame] | 1299 | /* allocate (de)compressed page arrays if cannot be kept on stack */ |
| 1300 | be->decompressed_pages = NULL; |
| 1301 | be->compressed_pages = NULL; |
| 1302 | be->onstack_used = 0; |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1303 | if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) { |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1304 | be->decompressed_pages = be->onstack_pages; |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1305 | be->onstack_used = be->nr_pages; |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1306 | memset(be->decompressed_pages, 0, |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1307 | sizeof(struct page *) * be->nr_pages); |
Gao Xiang | fe3e591 | 2022-07-15 23:42:00 +0800 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES) |
| 1311 | be->compressed_pages = be->onstack_pages + be->onstack_used; |
| 1312 | |
| 1313 | if (!be->decompressed_pages) |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1314 | be->decompressed_pages = |
Gao Xiang | 12724ba | 2023-01-10 15:49:27 +0800 | [diff] [blame] | 1315 | kcalloc(be->nr_pages, sizeof(struct page *), |
| 1316 | GFP_KERNEL | __GFP_NOFAIL); |
Gao Xiang | fe3e591 | 2022-07-15 23:42:00 +0800 | [diff] [blame] | 1317 | if (!be->compressed_pages) |
| 1318 | be->compressed_pages = |
Gao Xiang | 12724ba | 2023-01-10 15:49:27 +0800 | [diff] [blame] | 1319 | kcalloc(pclusterpages, sizeof(struct page *), |
| 1320 | GFP_KERNEL | __GFP_NOFAIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1321 | |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1322 | z_erofs_parse_out_bvecs(be); |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1323 | err2 = z_erofs_parse_in_bvecs(be, &overlapped); |
| 1324 | if (err2) |
| 1325 | err = err2; |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 1326 | if (err) |
Gao Xiang | 1115249 | 2019-03-25 11:40:07 +0800 | [diff] [blame] | 1327 | goto out; |
| 1328 | |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1329 | if (z_erofs_is_inline_pcluster(pcl)) |
| 1330 | inputsize = pcl->tailpacking_size; |
| 1331 | else |
| 1332 | inputsize = pclusterpages * PAGE_SIZE; |
| 1333 | |
Gao Xiang | 88aaf5a | 2019-06-24 15:22:57 +0800 | [diff] [blame] | 1334 | err = z_erofs_decompress(&(struct z_erofs_decompress_req) { |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1335 | .sb = be->sb, |
| 1336 | .in = be->compressed_pages, |
| 1337 | .out = be->decompressed_pages, |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1338 | .pageofs_in = pcl->pageofs_in, |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 1339 | .pageofs_out = pcl->pageofs_out, |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 1340 | .inputsize = inputsize, |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1341 | .outputsize = pcl->length, |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1342 | .alg = pcl->algorithmformat, |
Gao Xiang | 88aaf5a | 2019-06-24 15:22:57 +0800 | [diff] [blame] | 1343 | .inplace_io = overlapped, |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1344 | .partial_decoding = pcl->partial, |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1345 | .fillgaps = pcl->multibases, |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1346 | }, be->pagepool); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1347 | |
| 1348 | out: |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1349 | /* must handle all compressed pages before actual file pages */ |
| 1350 | if (z_erofs_is_inline_pcluster(pcl)) { |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1351 | page = pcl->compressed_bvecs[0].page; |
| 1352 | WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL); |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1353 | put_page(page); |
| 1354 | } else { |
| 1355 | for (i = 0; i < pclusterpages; ++i) { |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1356 | page = pcl->compressed_bvecs[i].page; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1357 | |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1358 | if (erofs_page_is_managed(sbi, page)) |
| 1359 | continue; |
Gao Xiang | d61fbb6 | 2019-03-25 11:40:08 +0800 | [diff] [blame] | 1360 | |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1361 | /* recycle all individual short-lived pages */ |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1362 | (void)z_erofs_put_shortlivedpage(be->pagepool, page); |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1363 | WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1364 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1365 | } |
Gao Xiang | fe3e591 | 2022-07-15 23:42:00 +0800 | [diff] [blame] | 1366 | if (be->compressed_pages < be->onstack_pages || |
| 1367 | be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES) |
Gao Xiang | 12724ba | 2023-01-10 15:49:27 +0800 | [diff] [blame] | 1368 | kfree(be->compressed_pages); |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1369 | z_erofs_fill_other_copies(be, err); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1370 | |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1371 | for (i = 0; i < be->nr_pages; ++i) { |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1372 | page = be->decompressed_pages[i]; |
Gao Xiang | af692e1 | 2019-02-27 13:33:30 +0800 | [diff] [blame] | 1373 | if (!page) |
| 1374 | continue; |
| 1375 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame] | 1376 | DBG_BUGON(z_erofs_page_is_invalidated(page)); |
Gao Xiang | af692e1 | 2019-02-27 13:33:30 +0800 | [diff] [blame] | 1377 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame] | 1378 | /* recycle all individual short-lived pages */ |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1379 | if (z_erofs_put_shortlivedpage(be->pagepool, page)) |
Gao Xiang | af692e1 | 2019-02-27 13:33:30 +0800 | [diff] [blame] | 1380 | continue; |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 1381 | if (err) |
| 1382 | z_erofs_page_mark_eio(page); |
Gao Xiang | af692e1 | 2019-02-27 13:33:30 +0800 | [diff] [blame] | 1383 | z_erofs_onlinepage_endio(page); |
| 1384 | } |
| 1385 | |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1386 | if (be->decompressed_pages != be->onstack_pages) |
Gao Xiang | 12724ba | 2023-01-10 15:49:27 +0800 | [diff] [blame] | 1387 | kfree(be->decompressed_pages); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1388 | |
Gao Xiang | 2bfab9c | 2022-07-15 23:42:02 +0800 | [diff] [blame] | 1389 | pcl->length = 0; |
| 1390 | pcl->partial = true; |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1391 | pcl->multibases = false; |
Gao Xiang | 06a304c | 2022-07-15 23:41:51 +0800 | [diff] [blame] | 1392 | pcl->bvset.nextpage = NULL; |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 1393 | pcl->vcnt = 0; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1394 | |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 1395 | /* pcluster lock MUST be taken before the following line */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1396 | WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); |
Gao Xiang | 87ca34a | 2022-05-29 13:54:23 +0800 | [diff] [blame] | 1397 | mutex_unlock(&pcl->lock); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1398 | return err; |
| 1399 | } |
| 1400 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1401 | static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1402 | struct page **pagepool) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1403 | { |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1404 | struct z_erofs_decompress_backend be = { |
| 1405 | .sb = io->sb, |
| 1406 | .pagepool = pagepool, |
Gao Xiang | 267f249 | 2022-07-15 23:42:03 +0800 | [diff] [blame] | 1407 | .decompressed_secondary_bvecs = |
| 1408 | LIST_HEAD_INIT(be.decompressed_secondary_bvecs), |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1409 | }; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1410 | z_erofs_next_pcluster_t owned = io->head; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1411 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1412 | while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1413 | /* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1414 | DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1415 | /* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1416 | DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1417 | |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1418 | be.pcl = container_of(owned, struct z_erofs_pcluster, next); |
| 1419 | owned = READ_ONCE(be.pcl->next); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1420 | |
Gao Xiang | 4f05687 | 2022-07-15 23:41:59 +0800 | [diff] [blame] | 1421 | z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0); |
| 1422 | erofs_workgroup_put(&be.pcl->obj); |
Gao Xiang | 3978c8e | 2018-08-06 11:27:53 +0800 | [diff] [blame] | 1423 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1424 | } |
| 1425 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1426 | static void z_erofs_decompressqueue_work(struct work_struct *work) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1427 | { |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1428 | struct z_erofs_decompressqueue *bgq = |
| 1429 | container_of(work, struct z_erofs_decompressqueue, u.work); |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1430 | struct page *pagepool = NULL; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1431 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1432 | DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1433 | z_erofs_decompress_queue(bgq, &pagepool); |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1434 | erofs_release_pages(&pagepool); |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1435 | kvfree(bgq); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1436 | } |
| 1437 | |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 1438 | #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD |
| 1439 | static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work) |
| 1440 | { |
| 1441 | z_erofs_decompressqueue_work((struct work_struct *)work); |
| 1442 | } |
| 1443 | #endif |
| 1444 | |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1445 | static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, |
Gao Xiang | cdba550 | 2023-02-04 17:30:36 +0800 | [diff] [blame] | 1446 | int bios) |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1447 | { |
| 1448 | struct erofs_sb_info *const sbi = EROFS_SB(io->sb); |
| 1449 | |
| 1450 | /* wake up the caller thread for sync decompression */ |
Gao Xiang | cdba550 | 2023-02-04 17:30:36 +0800 | [diff] [blame] | 1451 | if (io->sync) { |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1452 | if (!atomic_add_return(bios, &io->pending_bios)) |
Hongyu Jin | 60b3005 | 2022-04-01 19:55:27 +0800 | [diff] [blame] | 1453 | complete(&io->u.done); |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1454 | return; |
| 1455 | } |
| 1456 | |
| 1457 | if (atomic_add_return(bios, &io->pending_bios)) |
| 1458 | return; |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 1459 | /* Use (kthread_)work and sync decompression for atomic contexts only */ |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1460 | if (in_atomic() || irqs_disabled()) { |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 1461 | #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD |
| 1462 | struct kthread_worker *worker; |
| 1463 | |
| 1464 | rcu_read_lock(); |
| 1465 | worker = rcu_dereference( |
| 1466 | z_erofs_pcpu_workers[raw_smp_processor_id()]); |
| 1467 | if (!worker) { |
| 1468 | INIT_WORK(&io->u.work, z_erofs_decompressqueue_work); |
| 1469 | queue_work(z_erofs_workqueue, &io->u.work); |
| 1470 | } else { |
| 1471 | kthread_queue_work(worker, &io->u.kthread_work); |
| 1472 | } |
| 1473 | rcu_read_unlock(); |
| 1474 | #else |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1475 | queue_work(z_erofs_workqueue, &io->u.work); |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 1476 | #endif |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1477 | /* enable sync decompression for readahead */ |
| 1478 | if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) |
| 1479 | sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON; |
| 1480 | return; |
| 1481 | } |
| 1482 | z_erofs_decompressqueue_work(&io->u.work); |
| 1483 | } |
| 1484 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1485 | static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, |
| 1486 | unsigned int nr, |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1487 | struct page **pagepool, |
Gao Xiang | 9f2731d | 2022-03-11 02:27:43 +0800 | [diff] [blame] | 1488 | struct address_space *mc) |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1489 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1490 | const pgoff_t index = pcl->obj.index; |
Gao Xiang | 9f2731d | 2022-03-11 02:27:43 +0800 | [diff] [blame] | 1491 | gfp_t gfp = mapping_gfp_mask(mc); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1492 | bool tocache = false; |
| 1493 | |
| 1494 | struct address_space *mapping; |
| 1495 | struct page *oldpage, *page; |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1496 | int justfound; |
| 1497 | |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1498 | repeat: |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1499 | page = READ_ONCE(pcl->compressed_bvecs[nr].page); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1500 | oldpage = page; |
| 1501 | |
| 1502 | if (!page) |
| 1503 | goto out_allocpage; |
| 1504 | |
Gao Xiang | b1ed220 | 2023-02-04 17:30:37 +0800 | [diff] [blame] | 1505 | justfound = (unsigned long)page & 1UL; |
| 1506 | page = (struct page *)((unsigned long)page & ~1UL); |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1507 | |
Gao Xiang | 1825c8d | 2020-12-09 20:37:17 +0800 | [diff] [blame] | 1508 | /* |
| 1509 | * preallocated cached pages, which is used to avoid direct reclaim |
| 1510 | * otherwise, it will go inplace I/O path instead. |
| 1511 | */ |
| 1512 | if (page->private == Z_EROFS_PREALLOCATED_PAGE) { |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1513 | WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); |
Gao Xiang | 1825c8d | 2020-12-09 20:37:17 +0800 | [diff] [blame] | 1514 | set_page_private(page, 0); |
| 1515 | tocache = true; |
| 1516 | goto out_tocache; |
| 1517 | } |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1518 | mapping = READ_ONCE(page->mapping); |
| 1519 | |
| 1520 | /* |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame] | 1521 | * file-backed online pages in plcuster are all locked steady, |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1522 | * therefore it is impossible for `mapping' to be NULL. |
| 1523 | */ |
| 1524 | if (mapping && mapping != mc) |
| 1525 | /* ought to be unmanaged pages */ |
| 1526 | goto out; |
| 1527 | |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame] | 1528 | /* directly return for shortlived page as well */ |
| 1529 | if (z_erofs_is_shortlived_page(page)) |
| 1530 | goto out; |
| 1531 | |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1532 | lock_page(page); |
| 1533 | |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1534 | /* only true if page reclaim goes wrong, should never happen */ |
| 1535 | DBG_BUGON(justfound && PagePrivate(page)); |
| 1536 | |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1537 | /* the page is still in manage cache */ |
| 1538 | if (page->mapping == mc) { |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1539 | WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1540 | |
| 1541 | if (!PagePrivate(page)) { |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1542 | /* |
| 1543 | * impossible to be !PagePrivate(page) for |
| 1544 | * the current restriction as well if |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1545 | * the page is already in compressed_bvecs[]. |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1546 | */ |
| 1547 | DBG_BUGON(!justfound); |
| 1548 | |
| 1549 | justfound = 0; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1550 | set_page_private(page, (unsigned long)pcl); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1551 | SetPagePrivate(page); |
| 1552 | } |
| 1553 | |
| 1554 | /* no need to submit io if it is already up-to-date */ |
| 1555 | if (PageUptodate(page)) { |
| 1556 | unlock_page(page); |
| 1557 | page = NULL; |
| 1558 | } |
| 1559 | goto out; |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * the managed page has been truncated, it's unsafe to |
| 1564 | * reuse this one, let's allocate a new cache-managed page. |
| 1565 | */ |
| 1566 | DBG_BUGON(page->mapping); |
Gao Xiang | 92e6efd | 2018-12-08 00:19:16 +0800 | [diff] [blame] | 1567 | DBG_BUGON(!justfound); |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1568 | |
| 1569 | tocache = true; |
| 1570 | unlock_page(page); |
| 1571 | put_page(page); |
| 1572 | out_allocpage: |
Gao Xiang | 5ddcee1 | 2019-11-21 21:59:54 +0800 | [diff] [blame] | 1573 | page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); |
Gao Xiang | ed722fb | 2022-07-15 23:41:54 +0800 | [diff] [blame] | 1574 | if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page, |
| 1575 | oldpage, page)) { |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1576 | erofs_pagepool_add(pagepool, page); |
Gao Xiang | 5ddcee1 | 2019-11-21 21:59:54 +0800 | [diff] [blame] | 1577 | cond_resched(); |
| 1578 | goto repeat; |
| 1579 | } |
Gao Xiang | 1825c8d | 2020-12-09 20:37:17 +0800 | [diff] [blame] | 1580 | out_tocache: |
Gao Xiang | bf22507 | 2020-12-08 17:58:33 +0800 | [diff] [blame] | 1581 | if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { |
| 1582 | /* turn into temporary page if fails (1 ref) */ |
| 1583 | set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); |
| 1584 | goto out; |
Gao Xiang | a30573b | 2020-10-22 22:57:21 +0800 | [diff] [blame] | 1585 | } |
Gao Xiang | bf22507 | 2020-12-08 17:58:33 +0800 | [diff] [blame] | 1586 | attach_page_private(page, pcl); |
| 1587 | /* drop a refcount added by allocpage (then we have 2 refs here) */ |
| 1588 | put_page(page); |
| 1589 | |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1590 | out: /* the only exit (for tracing and debugging) */ |
| 1591 | return page; |
| 1592 | } |
| 1593 | |
Gao Xiang | cdba550 | 2023-02-04 17:30:36 +0800 | [diff] [blame] | 1594 | static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb, |
| 1595 | struct z_erofs_decompressqueue *fgq, bool *fg) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1596 | { |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1597 | struct z_erofs_decompressqueue *q; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1598 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1599 | if (fg && !*fg) { |
| 1600 | q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); |
| 1601 | if (!q) { |
| 1602 | *fg = true; |
| 1603 | goto fg_out; |
| 1604 | } |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 1605 | #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD |
| 1606 | kthread_init_work(&q->u.kthread_work, |
| 1607 | z_erofs_decompressqueue_kthread_work); |
| 1608 | #else |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1609 | INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 1610 | #endif |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1611 | } else { |
| 1612 | fg_out: |
| 1613 | q = fgq; |
Hongyu Jin | 60b3005 | 2022-04-01 19:55:27 +0800 | [diff] [blame] | 1614 | init_completion(&fgq->u.done); |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1615 | atomic_set(&fgq->pending_bios, 0); |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 1616 | q->eio = false; |
Gao Xiang | cdba550 | 2023-02-04 17:30:36 +0800 | [diff] [blame] | 1617 | q->sync = true; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1618 | } |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1619 | q->sb = sb; |
| 1620 | q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; |
| 1621 | return q; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1622 | } |
| 1623 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1624 | /* define decompression jobqueue types */ |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1625 | enum { |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1626 | JQ_BYPASS, |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1627 | JQ_SUBMIT, |
| 1628 | NR_JOBQUEUES, |
| 1629 | }; |
| 1630 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1631 | static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, |
| 1632 | z_erofs_next_pcluster_t qtail[], |
| 1633 | z_erofs_next_pcluster_t owned_head) |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1634 | { |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1635 | z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; |
| 1636 | z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1637 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1638 | DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); |
| 1639 | if (owned_head == Z_EROFS_PCLUSTER_TAIL) |
| 1640 | owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1641 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1642 | WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1643 | |
| 1644 | WRITE_ONCE(*submit_qtail, owned_head); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1645 | WRITE_ONCE(*bypass_qtail, &pcl->next); |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1646 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1647 | qtail[JQ_BYPASS] = &pcl->next; |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1648 | } |
| 1649 | |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1650 | static void z_erofs_decompressqueue_endio(struct bio *bio) |
| 1651 | { |
Gao Xiang | cdba550 | 2023-02-04 17:30:36 +0800 | [diff] [blame] | 1652 | struct z_erofs_decompressqueue *q = bio->bi_private; |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1653 | blk_status_t err = bio->bi_status; |
| 1654 | struct bio_vec *bvec; |
| 1655 | struct bvec_iter_all iter_all; |
| 1656 | |
| 1657 | bio_for_each_segment_all(bvec, bio, iter_all) { |
| 1658 | struct page *page = bvec->bv_page; |
| 1659 | |
| 1660 | DBG_BUGON(PageUptodate(page)); |
| 1661 | DBG_BUGON(z_erofs_page_is_invalidated(page)); |
| 1662 | |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1663 | if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { |
| 1664 | if (!err) |
| 1665 | SetPageUptodate(page); |
| 1666 | unlock_page(page); |
| 1667 | } |
| 1668 | } |
Gao Xiang | 6714855 | 2022-07-15 23:41:55 +0800 | [diff] [blame] | 1669 | if (err) |
| 1670 | q->eio = true; |
Gao Xiang | cdba550 | 2023-02-04 17:30:36 +0800 | [diff] [blame] | 1671 | z_erofs_decompress_kickoff(q, -1); |
Gao Xiang | 7865827 | 2022-01-21 17:14:12 +0800 | [diff] [blame] | 1672 | bio_put(bio); |
| 1673 | } |
| 1674 | |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 1675 | static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f, |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1676 | struct page **pagepool, |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1677 | struct z_erofs_decompressqueue *fgq, |
| 1678 | bool *force_fg) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1679 | { |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 1680 | struct super_block *sb = f->inode->i_sb; |
| 1681 | struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb)); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1682 | z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1683 | struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 1684 | z_erofs_next_pcluster_t owned_head = f->owned_head; |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 1685 | /* bio is NULL initially, so no need to initialize last_{index,bdev} */ |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 1686 | pgoff_t last_index; |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 1687 | struct block_device *last_bdev; |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1688 | unsigned int nr_bios = 0; |
| 1689 | struct bio *bio = NULL; |
Johannes Weiner | 82e60d0 | 2022-11-03 17:34:31 -0400 | [diff] [blame] | 1690 | unsigned long pflags; |
| 1691 | int memstall = 0; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1692 | |
Gao Xiang | cdba550 | 2023-02-04 17:30:36 +0800 | [diff] [blame] | 1693 | /* |
| 1694 | * if managed cache is enabled, bypass jobqueue is needed, |
| 1695 | * no need to read from device for all pclusters in this queue. |
| 1696 | */ |
| 1697 | q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); |
| 1698 | q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg); |
| 1699 | |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1700 | qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; |
| 1701 | qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1702 | |
| 1703 | /* by default, all need io submission */ |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1704 | q[JQ_SUBMIT]->head = owned_head; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1705 | |
| 1706 | do { |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 1707 | struct erofs_map_dev mdev; |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1708 | struct z_erofs_pcluster *pcl; |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1709 | pgoff_t cur, end; |
| 1710 | unsigned int i = 0; |
| 1711 | bool bypass = true; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1712 | |
| 1713 | /* no possible 'owned_head' equals the following */ |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1714 | DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); |
| 1715 | DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1716 | |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1717 | pcl = container_of(owned_head, struct z_erofs_pcluster, next); |
| 1718 | |
Yue Hu | cecf864 | 2021-12-29 07:29:19 +0800 | [diff] [blame] | 1719 | /* close the main owned chain at first */ |
| 1720 | owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, |
| 1721 | Z_EROFS_PCLUSTER_TAIL_CLOSED); |
| 1722 | if (z_erofs_is_inline_pcluster(pcl)) { |
| 1723 | move_to_bypass_jobqueue(pcl, qtail, owned_head); |
| 1724 | continue; |
| 1725 | } |
| 1726 | |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 1727 | /* no device id here, thus it will always succeed */ |
| 1728 | mdev = (struct erofs_map_dev) { |
| 1729 | .m_pa = blknr_to_addr(pcl->obj.index), |
| 1730 | }; |
| 1731 | (void)erofs_map_dev(sb, &mdev); |
| 1732 | |
| 1733 | cur = erofs_blknr(mdev.m_pa); |
Gao Xiang | 9f6cc76 | 2021-04-07 12:39:20 +0800 | [diff] [blame] | 1734 | end = cur + pcl->pclusterpages; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1735 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1736 | do { |
| 1737 | struct page *page; |
Gao Xiang | 9248fce | 2018-12-08 00:19:15 +0800 | [diff] [blame] | 1738 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1739 | page = pickup_page_for_submission(pcl, i++, pagepool, |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 1740 | mc); |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1741 | if (!page) |
| 1742 | continue; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1743 | |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 1744 | if (bio && (cur != last_index + 1 || |
| 1745 | last_bdev != mdev.m_bdev)) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1746 | submit_bio_retry: |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1747 | submit_bio(bio); |
Johannes Weiner | 82e60d0 | 2022-11-03 17:34:31 -0400 | [diff] [blame] | 1748 | if (memstall) { |
| 1749 | psi_memstall_leave(&pflags); |
| 1750 | memstall = 0; |
| 1751 | } |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1752 | bio = NULL; |
| 1753 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1754 | |
Johannes Weiner | 82e60d0 | 2022-11-03 17:34:31 -0400 | [diff] [blame] | 1755 | if (unlikely(PageWorkingset(page)) && !memstall) { |
Christoph Hellwig | 99486c5 | 2022-09-15 10:41:59 +0100 | [diff] [blame] | 1756 | psi_memstall_enter(&pflags); |
Johannes Weiner | 82e60d0 | 2022-11-03 17:34:31 -0400 | [diff] [blame] | 1757 | memstall = 1; |
| 1758 | } |
Christoph Hellwig | 99486c5 | 2022-09-15 10:41:59 +0100 | [diff] [blame] | 1759 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1760 | if (!bio) { |
Christoph Hellwig | 07888c66 | 2022-01-24 10:11:05 +0100 | [diff] [blame] | 1761 | bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS, |
| 1762 | REQ_OP_READ, GFP_NOIO); |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1763 | bio->bi_end_io = z_erofs_decompressqueue_endio; |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 1764 | |
Gao Xiang | dfeab2e | 2021-10-14 16:10:10 +0800 | [diff] [blame] | 1765 | last_bdev = mdev.m_bdev; |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1766 | bio->bi_iter.bi_sector = (sector_t)cur << |
| 1767 | LOG_SECTORS_PER_BLOCK; |
Gao Xiang | cdba550 | 2023-02-04 17:30:36 +0800 | [diff] [blame] | 1768 | bio->bi_private = q[JQ_SUBMIT]; |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1769 | if (f->readahead) |
| 1770 | bio->bi_opf |= REQ_RAHEAD; |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1771 | ++nr_bios; |
| 1772 | } |
Gao Xiang | 94e4e15 | 2019-09-04 10:09:04 +0800 | [diff] [blame] | 1773 | |
Gao Xiang | 6c3e485 | 2020-09-19 15:27:28 +0800 | [diff] [blame] | 1774 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1775 | goto submit_bio_retry; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1776 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1777 | last_index = cur; |
| 1778 | bypass = false; |
| 1779 | } while (++cur < end); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1780 | |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1781 | if (!bypass) |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1782 | qtail[JQ_SUBMIT] = &pcl->next; |
Gao Xiang | 7146a4f | 2018-12-08 00:19:18 +0800 | [diff] [blame] | 1783 | else |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1784 | move_to_bypass_jobqueue(pcl, qtail, owned_head); |
| 1785 | } while (owned_head != Z_EROFS_PCLUSTER_TAIL); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1786 | |
Christoph Hellwig | 99486c5 | 2022-09-15 10:41:59 +0100 | [diff] [blame] | 1787 | if (bio) { |
Gao Xiang | 94e4e15 | 2019-09-04 10:09:04 +0800 | [diff] [blame] | 1788 | submit_bio(bio); |
Johannes Weiner | 82e60d0 | 2022-11-03 17:34:31 -0400 | [diff] [blame] | 1789 | if (memstall) |
| 1790 | psi_memstall_leave(&pflags); |
Christoph Hellwig | 99486c5 | 2022-09-15 10:41:59 +0100 | [diff] [blame] | 1791 | } |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1792 | |
Gao Xiang | 587a67b7 | 2020-01-21 14:47:47 +0800 | [diff] [blame] | 1793 | /* |
| 1794 | * although background is preferred, no one is pending for submission. |
Sandeep Dhavale | 3fffb58 | 2023-02-08 17:33:22 +0800 | [diff] [blame] | 1795 | * don't issue decompression but drop it directly instead. |
Gao Xiang | 587a67b7 | 2020-01-21 14:47:47 +0800 | [diff] [blame] | 1796 | */ |
| 1797 | if (!*force_fg && !nr_bios) { |
| 1798 | kvfree(q[JQ_SUBMIT]); |
Gao Xiang | 1e4a295 | 2020-01-21 14:48:19 +0800 | [diff] [blame] | 1799 | return; |
Gao Xiang | 587a67b7 | 2020-01-21 14:47:47 +0800 | [diff] [blame] | 1800 | } |
Gao Xiang | cdba550 | 2023-02-04 17:30:36 +0800 | [diff] [blame] | 1801 | z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1802 | } |
| 1803 | |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 1804 | static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f, |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1805 | struct page **pagepool, bool force_fg) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1806 | { |
Gao Xiang | a4b1fab | 2019-10-08 20:56:15 +0800 | [diff] [blame] | 1807 | struct z_erofs_decompressqueue io[NR_JOBQUEUES]; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1808 | |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 1809 | if (f->owned_head == Z_EROFS_PCLUSTER_TAIL) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1810 | return; |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 1811 | z_erofs_submit_queue(f, pagepool, io, &force_fg); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1812 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1813 | /* handle bypass queue (no i/o pclusters) immediately */ |
| 1814 | z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool); |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 1815 | |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1816 | if (!force_fg) |
| 1817 | return; |
| 1818 | |
| 1819 | /* wait until all bios are completed */ |
Hongyu Jin | 60b3005 | 2022-04-01 19:55:27 +0800 | [diff] [blame] | 1820 | wait_for_completion_io(&io[JQ_SUBMIT].u.done); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1821 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1822 | /* handle synchronous decompress queue in the caller context */ |
| 1823 | z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1824 | } |
| 1825 | |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1826 | /* |
| 1827 | * Since partial uptodate is still unimplemented for now, we have to use |
| 1828 | * approximate readmore strategies as a start. |
| 1829 | */ |
| 1830 | static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f, |
| 1831 | struct readahead_control *rac, |
| 1832 | erofs_off_t end, |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1833 | struct page **pagepool, |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1834 | bool backmost) |
| 1835 | { |
| 1836 | struct inode *inode = f->inode; |
| 1837 | struct erofs_map_blocks *map = &f->map; |
| 1838 | erofs_off_t cur; |
| 1839 | int err; |
| 1840 | |
| 1841 | if (backmost) { |
| 1842 | map->m_la = end; |
Gao Xiang | 622cead | 2021-10-11 05:31:45 +0800 | [diff] [blame] | 1843 | err = z_erofs_map_blocks_iter(inode, map, |
| 1844 | EROFS_GET_BLOCKS_READMORE); |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1845 | if (err) |
| 1846 | return; |
| 1847 | |
| 1848 | /* expend ra for the trailing edge if readahead */ |
| 1849 | if (rac) { |
| 1850 | loff_t newstart = readahead_pos(rac); |
| 1851 | |
| 1852 | cur = round_up(map->m_la + map->m_llen, PAGE_SIZE); |
| 1853 | readahead_expand(rac, newstart, cur - newstart); |
| 1854 | return; |
| 1855 | } |
| 1856 | end = round_up(end, PAGE_SIZE); |
| 1857 | } else { |
| 1858 | end = round_up(map->m_la, PAGE_SIZE); |
| 1859 | |
| 1860 | if (!map->m_llen) |
| 1861 | return; |
| 1862 | } |
| 1863 | |
| 1864 | cur = map->m_la + map->m_llen - 1; |
| 1865 | while (cur >= end) { |
| 1866 | pgoff_t index = cur >> PAGE_SHIFT; |
| 1867 | struct page *page; |
| 1868 | |
| 1869 | page = erofs_grab_cache_page_nowait(inode->i_mapping, index); |
Gao Xiang | aa793b46 | 2022-05-29 13:54:25 +0800 | [diff] [blame] | 1870 | if (page) { |
| 1871 | if (PageUptodate(page)) { |
| 1872 | unlock_page(page); |
| 1873 | } else { |
| 1874 | err = z_erofs_do_read_page(f, page, pagepool); |
| 1875 | if (err) |
| 1876 | erofs_err(inode->i_sb, |
| 1877 | "readmore error at page %lu @ nid %llu", |
| 1878 | index, EROFS_I(inode)->nid); |
| 1879 | } |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1880 | put_page(page); |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1881 | } |
| 1882 | |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1883 | if (cur < PAGE_SIZE) |
| 1884 | break; |
| 1885 | cur = (index << PAGE_SHIFT) - 1; |
| 1886 | } |
| 1887 | } |
| 1888 | |
Matthew Wilcox (Oracle) | a2e20a2 | 2022-04-29 11:12:16 -0400 | [diff] [blame] | 1889 | static int z_erofs_read_folio(struct file *file, struct folio *folio) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1890 | { |
Matthew Wilcox (Oracle) | a2e20a2 | 2022-04-29 11:12:16 -0400 | [diff] [blame] | 1891 | struct page *page = &folio->page; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1892 | struct inode *const inode = page->mapping->host; |
Huang Jianan | 40452ff | 2021-12-06 22:35:52 +0800 | [diff] [blame] | 1893 | struct erofs_sb_info *const sbi = EROFS_I_SB(inode); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1894 | struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1895 | struct page *pagepool = NULL; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1896 | int err; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1897 | |
Gao Xiang | ba9ce77 | 2018-11-23 01:15:58 +0800 | [diff] [blame] | 1898 | trace_erofs_readpage(page, false); |
Gao Xiang | f0c519f | 2018-11-23 01:21:49 +0800 | [diff] [blame] | 1899 | f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; |
| 1900 | |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1901 | z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1, |
| 1902 | &pagepool, true); |
Gao Xiang | 1825c8d | 2020-12-09 20:37:17 +0800 | [diff] [blame] | 1903 | err = z_erofs_do_read_page(&f, page, &pagepool); |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1904 | z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false); |
| 1905 | |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 1906 | (void)z_erofs_collector_end(&f); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1907 | |
Gao Xiang | ee45197 | 2019-08-19 18:34:21 +0800 | [diff] [blame] | 1908 | /* if some compressed cluster ready, need submit them anyway */ |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 1909 | z_erofs_runqueue(&f, &pagepool, |
Huang Jianan | 40452ff | 2021-12-06 22:35:52 +0800 | [diff] [blame] | 1910 | z_erofs_get_sync_decompress_policy(sbi, 0)); |
Gao Xiang | ee45197 | 2019-08-19 18:34:21 +0800 | [diff] [blame] | 1911 | |
| 1912 | if (err) |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 1913 | erofs_err(inode->i_sb, "failed to read, err [%d]", err); |
Gao Xiang | ee45197 | 2019-08-19 18:34:21 +0800 | [diff] [blame] | 1914 | |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame] | 1915 | erofs_put_metabuf(&f.map.buf); |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1916 | erofs_release_pages(&pagepool); |
Gao Xiang | ee45197 | 2019-08-19 18:34:21 +0800 | [diff] [blame] | 1917 | return err; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1918 | } |
| 1919 | |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1920 | static void z_erofs_readahead(struct readahead_control *rac) |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1921 | { |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1922 | struct inode *const inode = rac->mapping->host; |
Gao Xiang | 5fb76bb | 2018-09-20 00:06:56 +0800 | [diff] [blame] | 1923 | struct erofs_sb_info *const sbi = EROFS_I_SB(inode); |
Gao Xiang | 97e86a8 | 2019-07-31 23:57:47 +0800 | [diff] [blame] | 1924 | struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1925 | struct page *pagepool = NULL, *head = NULL, *page; |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1926 | unsigned int nr_pages; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1927 | |
Gao Xiang | 6ea5aad | 2020-09-19 15:27:30 +0800 | [diff] [blame] | 1928 | f.readahead = true; |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1929 | f.headoffset = readahead_pos(rac); |
Gao Xiang | f0c519f | 2018-11-23 01:21:49 +0800 | [diff] [blame] | 1930 | |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1931 | z_erofs_pcluster_readmore(&f, rac, f.headoffset + |
| 1932 | readahead_length(rac) - 1, &pagepool, true); |
| 1933 | nr_pages = readahead_count(rac); |
| 1934 | trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); |
| 1935 | |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1936 | while ((page = readahead_page(rac))) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1937 | set_page_private(page, (unsigned long)head); |
| 1938 | head = page; |
| 1939 | } |
| 1940 | |
Cristian Sicilia | 42d40b4 | 2018-11-12 21:43:57 +0100 | [diff] [blame] | 1941 | while (head) { |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1942 | struct page *page = head; |
| 1943 | int err; |
| 1944 | |
| 1945 | /* traversal in reverse order */ |
| 1946 | head = (void *)page_private(page); |
| 1947 | |
Gao Xiang | 1825c8d | 2020-12-09 20:37:17 +0800 | [diff] [blame] | 1948 | err = z_erofs_do_read_page(&f, page, &pagepool); |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 1949 | if (err) |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 1950 | erofs_err(inode->i_sb, |
| 1951 | "readahead error at page %lu @ nid %llu", |
| 1952 | page->index, EROFS_I(inode)->nid); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1953 | put_page(page); |
| 1954 | } |
Gao Xiang | 3862929 | 2021-10-09 04:08:39 +0800 | [diff] [blame] | 1955 | z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false); |
Gao Xiang | 5c6dcc5 | 2022-03-02 03:49:50 +0800 | [diff] [blame] | 1956 | (void)z_erofs_collector_end(&f); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1957 | |
Gao Xiang | 83a386c | 2022-07-15 23:41:48 +0800 | [diff] [blame] | 1958 | z_erofs_runqueue(&f, &pagepool, |
Huang Jianan | 40452ff | 2021-12-06 22:35:52 +0800 | [diff] [blame] | 1959 | z_erofs_get_sync_decompress_policy(sbi, nr_pages)); |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame] | 1960 | erofs_put_metabuf(&f.map.buf); |
Gao Xiang | eaa9172 | 2021-10-22 17:01:20 +0800 | [diff] [blame] | 1961 | erofs_release_pages(&pagepool); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1962 | } |
| 1963 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 1964 | const struct address_space_operations z_erofs_aops = { |
Matthew Wilcox (Oracle) | a2e20a2 | 2022-04-29 11:12:16 -0400 | [diff] [blame] | 1965 | .read_folio = z_erofs_read_folio, |
Matthew Wilcox (Oracle) | 0615090 | 2020-06-01 21:47:13 -0700 | [diff] [blame] | 1966 | .readahead = z_erofs_readahead, |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 1967 | }; |