1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2018 HUAWEI, Inc. 4 * https://www.huawei.com/ 5 * Copyright (C) 2022 Alibaba Cloud 6 */ 7 #include "compress.h" 8 #include <linux/psi.h> 9 #include <linux/cpuhotplug.h> 10 #include <trace/events/erofs.h> 11 12 #define Z_EROFS_MAX_SYNC_DECOMPRESS_BYTES 12288 13 #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE) 14 #define Z_EROFS_INLINE_BVECS 2 15 16 struct z_erofs_bvec { 17 struct page *page; 18 int offset; 19 unsigned int end; 20 }; 21 22 #define __Z_EROFS_BVSET(name, total) \ 23 struct name { \ 24 /* point to the next page which contains the following bvecs */ \ 25 struct page *nextpage; \ 26 struct z_erofs_bvec bvec[total]; \ 27 } 28 __Z_EROFS_BVSET(z_erofs_bvset,); 29 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS); 30 31 /* 32 * Structure fields follow one of the following exclusion rules. 33 * 34 * I: Modifiable by initialization/destruction paths and read-only 35 * for everyone else; 36 * 37 * L: Field should be protected by the pcluster lock; 38 * 39 * A: Field should be accessed / updated in atomic for parallelized code. 40 */ 41 struct z_erofs_pcluster { 42 struct mutex lock; 43 struct lockref lockref; 44 45 /* A: point to next chained pcluster or TAILs */ 46 struct z_erofs_pcluster *next; 47 48 /* I: start physical position of this pcluster */ 49 erofs_off_t pos; 50 51 /* L: the maximum decompression size of this round */ 52 unsigned int length; 53 54 /* L: total number of bvecs */ 55 unsigned int vcnt; 56 57 /* I: pcluster size (compressed size) in bytes */ 58 unsigned int pclustersize; 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 /* I: compression algorithm format */ 75 unsigned char algorithmformat; 76 77 /* I: whether compressed data is in-lined or not */ 78 bool from_meta; 79 80 /* L: whether partial decompression or not */ 81 bool partial; 82 83 /* L: whether extra buffer allocations are best-effort */ 84 bool besteffort; 85 86 /* A: compressed bvecs (can be cached or inplaced pages) */ 87 struct z_erofs_bvec compressed_bvecs[]; 88 }; 89 90 /* the end of a chain of pclusters */ 91 #define Z_EROFS_PCLUSTER_TAIL ((void *) 0x700 + POISON_POINTER_DELTA) 92 93 struct z_erofs_decompressqueue { 94 struct super_block *sb; 95 struct z_erofs_pcluster *head; 96 atomic_t pending_bios; 97 98 union { 99 struct completion done; 100 struct work_struct work; 101 struct kthread_work kthread_work; 102 } u; 103 bool eio, sync; 104 }; 105 106 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl) 107 { 108 return PAGE_ALIGN(pcl->pageofs_in + pcl->pclustersize) >> PAGE_SHIFT; 109 } 110 111 static bool erofs_folio_is_managed(struct erofs_sb_info *sbi, struct folio *fo) 112 { 113 return fo->mapping == MNGD_MAPPING(sbi); 114 } 115 116 #define Z_EROFS_ONSTACK_PAGES 32 117 118 /* 119 * since pclustersize is variable for big pcluster feature, introduce slab 120 * pools implementation for different pcluster sizes. 121 */ 122 struct z_erofs_pcluster_slab { 123 struct kmem_cache *slab; 124 unsigned int maxpages; 125 char name[48]; 126 }; 127 128 #define _PCLP(n) { .maxpages = n } 129 130 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = { 131 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128), 132 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES + 1) 133 }; 134 135 struct z_erofs_bvec_iter { 136 struct page *bvpage; 137 struct z_erofs_bvset *bvset; 138 unsigned int nr, cur; 139 }; 140 141 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter) 142 { 143 if (iter->bvpage) 144 kunmap_local(iter->bvset); 145 return iter->bvpage; 146 } 147 148 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter) 149 { 150 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec; 151 /* have to access nextpage in advance, otherwise it will be unmapped */ 152 struct page *nextpage = iter->bvset->nextpage; 153 struct page *oldpage; 154 155 DBG_BUGON(!nextpage); 156 oldpage = z_erofs_bvec_iter_end(iter); 157 iter->bvpage = nextpage; 158 iter->bvset = kmap_local_page(nextpage); 159 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec); 160 iter->cur = 0; 161 return oldpage; 162 } 163 164 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter, 165 struct z_erofs_bvset_inline *bvset, 166 unsigned int bootstrap_nr, 167 unsigned int cur) 168 { 169 *iter = (struct z_erofs_bvec_iter) { 170 .nr = bootstrap_nr, 171 .bvset = (struct z_erofs_bvset *)bvset, 172 }; 173 174 while (cur > iter->nr) { 175 cur -= iter->nr; 176 z_erofs_bvset_flip(iter); 177 } 178 iter->cur = cur; 179 } 180 181 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter, 182 struct z_erofs_bvec *bvec, 183 struct page **candidate_bvpage, 184 struct page **pagepool) 185 { 186 if (iter->cur >= iter->nr) { 187 struct page *nextpage = *candidate_bvpage; 188 189 if (!nextpage) { 190 nextpage = __erofs_allocpage(pagepool, GFP_KERNEL, 191 true); 192 if (!nextpage) 193 return -ENOMEM; 194 set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE); 195 } 196 DBG_BUGON(iter->bvset->nextpage); 197 iter->bvset->nextpage = nextpage; 198 z_erofs_bvset_flip(iter); 199 200 iter->bvset->nextpage = NULL; 201 *candidate_bvpage = NULL; 202 } 203 iter->bvset->bvec[iter->cur++] = *bvec; 204 return 0; 205 } 206 207 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter, 208 struct z_erofs_bvec *bvec, 209 struct page **old_bvpage) 210 { 211 if (iter->cur == iter->nr) 212 *old_bvpage = z_erofs_bvset_flip(iter); 213 else 214 *old_bvpage = NULL; 215 *bvec = iter->bvset->bvec[iter->cur++]; 216 } 217 218 static void z_erofs_destroy_pcluster_pool(void) 219 { 220 int i; 221 222 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 223 if (!pcluster_pool[i].slab) 224 continue; 225 kmem_cache_destroy(pcluster_pool[i].slab); 226 pcluster_pool[i].slab = NULL; 227 } 228 } 229 230 static int z_erofs_create_pcluster_pool(void) 231 { 232 struct z_erofs_pcluster_slab *pcs; 233 struct z_erofs_pcluster *a; 234 unsigned int size; 235 236 for (pcs = pcluster_pool; 237 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) { 238 size = struct_size(a, compressed_bvecs, pcs->maxpages); 239 240 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages); 241 pcs->slab = kmem_cache_create(pcs->name, size, 0, 242 SLAB_RECLAIM_ACCOUNT, NULL); 243 if (pcs->slab) 244 continue; 245 246 z_erofs_destroy_pcluster_pool(); 247 return -ENOMEM; 248 } 249 return 0; 250 } 251 252 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int size) 253 { 254 unsigned int nrpages = PAGE_ALIGN(size) >> PAGE_SHIFT; 255 struct z_erofs_pcluster_slab *pcs = pcluster_pool; 256 257 for (; pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) { 258 struct z_erofs_pcluster *pcl; 259 260 if (nrpages > pcs->maxpages) 261 continue; 262 263 pcl = kmem_cache_zalloc(pcs->slab, GFP_KERNEL); 264 if (!pcl) 265 return ERR_PTR(-ENOMEM); 266 return pcl; 267 } 268 return ERR_PTR(-EINVAL); 269 } 270 271 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl) 272 { 273 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 274 int i; 275 276 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 277 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 278 279 if (pclusterpages > pcs->maxpages) 280 continue; 281 282 kmem_cache_free(pcs->slab, pcl); 283 return; 284 } 285 DBG_BUGON(1); 286 } 287 288 static struct workqueue_struct *z_erofs_workqueue __read_mostly; 289 290 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 291 static struct kthread_worker __rcu **z_erofs_pcpu_workers; 292 static atomic_t erofs_percpu_workers_initialized = ATOMIC_INIT(0); 293 294 static void erofs_destroy_percpu_workers(void) 295 { 296 struct kthread_worker *worker; 297 unsigned int cpu; 298 299 for_each_possible_cpu(cpu) { 300 worker = rcu_dereference_protected( 301 z_erofs_pcpu_workers[cpu], 1); 302 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL); 303 if (worker) 304 kthread_destroy_worker(worker); 305 } 306 kfree(z_erofs_pcpu_workers); 307 } 308 309 static struct kthread_worker *erofs_init_percpu_worker(int cpu) 310 { 311 struct kthread_worker *worker = 312 kthread_run_worker_on_cpu(cpu, 0, "erofs_worker/%u"); 313 314 if (IS_ERR(worker)) 315 return worker; 316 if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI)) 317 sched_set_fifo_low(worker->task); 318 return worker; 319 } 320 321 static int erofs_init_percpu_workers(void) 322 { 323 struct kthread_worker *worker; 324 unsigned int cpu; 325 326 z_erofs_pcpu_workers = kzalloc_objs(struct kthread_worker *, 327 num_possible_cpus(), GFP_ATOMIC); 328 if (!z_erofs_pcpu_workers) 329 return -ENOMEM; 330 331 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */ 332 worker = erofs_init_percpu_worker(cpu); 333 if (!IS_ERR(worker)) 334 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker); 335 } 336 return 0; 337 } 338 339 #ifdef CONFIG_HOTPLUG_CPU 340 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock); 341 static enum cpuhp_state erofs_cpuhp_state; 342 343 static int erofs_cpu_online(unsigned int cpu) 344 { 345 struct kthread_worker *worker, *old; 346 347 worker = erofs_init_percpu_worker(cpu); 348 if (IS_ERR(worker)) 349 return PTR_ERR(worker); 350 351 spin_lock(&z_erofs_pcpu_worker_lock); 352 old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu], 353 lockdep_is_held(&z_erofs_pcpu_worker_lock)); 354 if (!old) 355 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker); 356 spin_unlock(&z_erofs_pcpu_worker_lock); 357 if (old) 358 kthread_destroy_worker(worker); 359 return 0; 360 } 361 362 static int erofs_cpu_offline(unsigned int cpu) 363 { 364 struct kthread_worker *worker; 365 366 spin_lock(&z_erofs_pcpu_worker_lock); 367 worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu], 368 lockdep_is_held(&z_erofs_pcpu_worker_lock)); 369 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL); 370 spin_unlock(&z_erofs_pcpu_worker_lock); 371 372 synchronize_rcu(); 373 if (worker) 374 kthread_destroy_worker(worker); 375 return 0; 376 } 377 378 static int erofs_cpu_hotplug_init(void) 379 { 380 int state; 381 382 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 383 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline); 384 if (state < 0) 385 return state; 386 387 erofs_cpuhp_state = state; 388 return 0; 389 } 390 391 static void erofs_cpu_hotplug_destroy(void) 392 { 393 if (erofs_cpuhp_state) 394 cpuhp_remove_state_nocalls(erofs_cpuhp_state); 395 } 396 #else /* !CONFIG_HOTPLUG_CPU */ 397 static inline int erofs_cpu_hotplug_init(void) { return 0; } 398 static inline void erofs_cpu_hotplug_destroy(void) {} 399 #endif/* CONFIG_HOTPLUG_CPU */ 400 static int z_erofs_init_pcpu_workers(struct super_block *sb) 401 { 402 int err; 403 404 if (atomic_xchg(&erofs_percpu_workers_initialized, 1)) 405 return 0; 406 407 err = erofs_init_percpu_workers(); 408 if (err) { 409 erofs_err(sb, "per-cpu workers: failed to allocate."); 410 goto err_init_percpu_workers; 411 } 412 413 err = erofs_cpu_hotplug_init(); 414 if (err < 0) { 415 erofs_err(sb, "per-cpu workers: failed CPU hotplug init."); 416 goto err_cpuhp_init; 417 } 418 erofs_info(sb, "initialized per-cpu workers successfully."); 419 return err; 420 421 err_cpuhp_init: 422 erofs_destroy_percpu_workers(); 423 err_init_percpu_workers: 424 atomic_set(&erofs_percpu_workers_initialized, 0); 425 return err; 426 } 427 428 static void z_erofs_destroy_pcpu_workers(void) 429 { 430 if (!atomic_xchg(&erofs_percpu_workers_initialized, 0)) 431 return; 432 erofs_cpu_hotplug_destroy(); 433 erofs_destroy_percpu_workers(); 434 } 435 #else /* !CONFIG_EROFS_FS_PCPU_KTHREAD */ 436 static inline int z_erofs_init_pcpu_workers(struct super_block *sb) { return 0; } 437 static inline void z_erofs_destroy_pcpu_workers(void) {} 438 #endif/* CONFIG_EROFS_FS_PCPU_KTHREAD */ 439 440 void z_erofs_exit_subsystem(void) 441 { 442 z_erofs_destroy_pcpu_workers(); 443 destroy_workqueue(z_erofs_workqueue); 444 z_erofs_destroy_pcluster_pool(); 445 z_erofs_crypto_disable_all_engines(); 446 z_erofs_exit_decompressor(); 447 } 448 449 int __init z_erofs_init_subsystem(void) 450 { 451 int err = z_erofs_init_decompressor(); 452 453 if (err) 454 goto err_decompressor; 455 456 err = z_erofs_create_pcluster_pool(); 457 if (err) 458 goto err_pcluster_pool; 459 460 z_erofs_workqueue = alloc_workqueue("erofs_worker", 461 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus()); 462 if (!z_erofs_workqueue) { 463 err = -ENOMEM; 464 goto err_workqueue_init; 465 } 466 467 return err; 468 469 err_workqueue_init: 470 z_erofs_destroy_pcluster_pool(); 471 err_pcluster_pool: 472 z_erofs_exit_decompressor(); 473 err_decompressor: 474 return err; 475 } 476 477 enum z_erofs_pclustermode { 478 /* It has previously been linked into another processing chain */ 479 Z_EROFS_PCLUSTER_INFLIGHT, 480 /* 481 * A weaker form of Z_EROFS_PCLUSTER_FOLLOWED; the difference is that it 482 * may be dispatched to the bypass queue later due to uptodated managed 483 * folios. All file-backed folios related to this pcluster cannot be 484 * reused for in-place I/O (or bvpage) since the pcluster may be decoded 485 * in a separate queue (and thus out of order). 486 */ 487 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE, 488 /* 489 * The pcluster has just been linked to our processing chain. 490 * File-backed folios (except for the head page) related to it can be 491 * used for in-place I/O (or bvpage). 492 */ 493 Z_EROFS_PCLUSTER_FOLLOWED, 494 }; 495 496 struct z_erofs_frontend { 497 struct inode *inode, *sharedinode; 498 struct erofs_map_blocks map; 499 struct z_erofs_bvec_iter biter; 500 501 struct page *pagepool; 502 struct page *candidate_bvpage; 503 struct z_erofs_pcluster *pcl, *head; 504 enum z_erofs_pclustermode mode; 505 506 erofs_off_t headoffset; 507 508 /* a pointer used to pick up inplace I/O pages */ 509 unsigned int icur; 510 }; 511 512 #define Z_EROFS_DEFINE_FRONTEND(fe, i, si, ho) struct z_erofs_frontend fe = { \ 513 .inode = i, .sharedinode = si, .head = Z_EROFS_PCLUSTER_TAIL, \ 514 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .headoffset = ho } 515 516 static bool z_erofs_should_alloc_cache(struct z_erofs_frontend *fe) 517 { 518 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy; 519 520 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 521 return false; 522 523 if (fe->map.m_flags & EROFS_MAP_PARTIAL_MAPPED) 524 return true; 525 526 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 527 fe->map.m_la < fe->headoffset) 528 return true; 529 530 return false; 531 } 532 533 static void z_erofs_bind_cache(struct z_erofs_frontend *fe) 534 { 535 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode)); 536 struct z_erofs_pcluster *pcl = fe->pcl; 537 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 538 bool shouldalloc = z_erofs_should_alloc_cache(fe); 539 pgoff_t poff = pcl->pos >> PAGE_SHIFT; 540 bool may_bypass = true; 541 /* Optimistic allocation, as in-place I/O can be used as a fallback */ 542 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 543 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 544 struct folio *folio, *newfolio; 545 unsigned int i; 546 547 if (i_blocksize(fe->inode) != PAGE_SIZE || 548 fe->mode < Z_EROFS_PCLUSTER_FOLLOWED) 549 return; 550 551 for (i = 0; i < pclusterpages; ++i) { 552 /* Inaccurate check w/o locking to avoid unneeded lookups */ 553 if (READ_ONCE(pcl->compressed_bvecs[i].page)) 554 continue; 555 556 folio = filemap_get_folio(mc, poff + i); 557 if (IS_ERR(folio)) { 558 may_bypass = false; 559 if (!shouldalloc) 560 continue; 561 562 /* 563 * Allocate a managed folio for cached I/O, or it may be 564 * then filled with a file-backed folio for in-place I/O 565 */ 566 newfolio = filemap_alloc_folio(gfp, 0, NULL); 567 if (!newfolio) 568 continue; 569 newfolio->private = Z_EROFS_PREALLOCATED_FOLIO; 570 folio = NULL; 571 } 572 spin_lock(&pcl->lockref.lock); 573 if (!pcl->compressed_bvecs[i].page) { 574 pcl->compressed_bvecs[i].page = 575 folio_page(folio ?: newfolio, 0); 576 spin_unlock(&pcl->lockref.lock); 577 continue; 578 } 579 spin_unlock(&pcl->lockref.lock); 580 folio_put(folio ?: newfolio); 581 } 582 583 /* 584 * Don't perform in-place I/O if all compressed pages are available in 585 * the managed cache, as the pcluster can be moved to the bypass queue. 586 */ 587 if (may_bypass) 588 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 589 } 590 591 /* (erofs_shrinker) disconnect cached encoded data with pclusters */ 592 static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi, 593 struct z_erofs_pcluster *pcl) 594 { 595 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 596 struct folio *folio; 597 int i; 598 599 DBG_BUGON(pcl->from_meta); 600 /* Each cached folio contains one page unless bs > ps is supported */ 601 for (i = 0; i < pclusterpages; ++i) { 602 if (pcl->compressed_bvecs[i].page) { 603 folio = page_folio(pcl->compressed_bvecs[i].page); 604 /* Avoid reclaiming or migrating this folio */ 605 if (!folio_trylock(folio)) 606 return -EBUSY; 607 608 DBG_BUGON(!erofs_folio_is_managed(sbi, folio)); 609 pcl->compressed_bvecs[i].page = NULL; 610 folio_detach_private(folio); 611 folio_unlock(folio); 612 } 613 } 614 return 0; 615 } 616 617 static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp) 618 { 619 struct z_erofs_pcluster *pcl = folio_get_private(folio); 620 struct z_erofs_bvec *bvec = pcl->compressed_bvecs; 621 struct z_erofs_bvec *end = bvec + z_erofs_pclusterpages(pcl); 622 bool ret; 623 624 if (!folio_test_private(folio)) 625 return true; 626 627 ret = false; 628 spin_lock(&pcl->lockref.lock); 629 if (pcl->lockref.count <= 0) { 630 DBG_BUGON(pcl->from_meta); 631 for (; bvec < end; ++bvec) { 632 if (bvec->page && page_folio(bvec->page) == folio) { 633 bvec->page = NULL; 634 folio_detach_private(folio); 635 ret = true; 636 break; 637 } 638 } 639 } 640 spin_unlock(&pcl->lockref.lock); 641 return ret; 642 } 643 644 /* 645 * It will be called only on inode eviction. In case that there are still some 646 * decompression requests in progress, wait with rescheduling for a bit here. 647 * An extra lock could be introduced instead but it seems unnecessary. 648 */ 649 static void z_erofs_cache_invalidate_folio(struct folio *folio, 650 size_t offset, size_t length) 651 { 652 const size_t stop = length + offset; 653 654 /* Check for potential overflow in debug mode */ 655 DBG_BUGON(stop > folio_size(folio) || stop < length); 656 657 if (offset == 0 && stop == folio_size(folio)) 658 while (!z_erofs_cache_release_folio(folio, 0)) 659 cond_resched(); 660 } 661 662 static const struct address_space_operations z_erofs_cache_aops = { 663 .release_folio = z_erofs_cache_release_folio, 664 .invalidate_folio = z_erofs_cache_invalidate_folio, 665 }; 666 667 int z_erofs_init_super(struct super_block *sb) 668 { 669 struct inode *inode; 670 int err; 671 672 err = z_erofs_init_pcpu_workers(sb); 673 if (err) 674 return err; 675 676 inode = new_inode(sb); 677 if (!inode) 678 return -ENOMEM; 679 set_nlink(inode, 1); 680 inode->i_size = OFFSET_MAX; 681 inode->i_mapping->a_ops = &z_erofs_cache_aops; 682 mapping_set_gfp_mask(inode->i_mapping, GFP_KERNEL); 683 EROFS_SB(sb)->managed_cache = inode; 684 xa_init(&EROFS_SB(sb)->managed_pslots); 685 return 0; 686 } 687 688 /* callers must be with pcluster lock held */ 689 static int z_erofs_attach_page(struct z_erofs_frontend *fe, 690 struct z_erofs_bvec *bvec, bool exclusive) 691 { 692 struct z_erofs_pcluster *pcl = fe->pcl; 693 int ret; 694 695 if (exclusive) { 696 /* Inplace I/O is limited to one page for uncompressed data */ 697 if (pcl->algorithmformat < Z_EROFS_COMPRESSION_MAX || 698 fe->icur <= 1) { 699 /* Try to prioritize inplace I/O here */ 700 spin_lock(&pcl->lockref.lock); 701 while (fe->icur > 0) { 702 if (pcl->compressed_bvecs[--fe->icur].page) 703 continue; 704 pcl->compressed_bvecs[fe->icur] = *bvec; 705 spin_unlock(&pcl->lockref.lock); 706 return 0; 707 } 708 spin_unlock(&pcl->lockref.lock); 709 } 710 711 /* otherwise, check if it can be used as a bvpage */ 712 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED && 713 !fe->candidate_bvpage) 714 fe->candidate_bvpage = bvec->page; 715 } 716 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage, 717 &fe->pagepool); 718 fe->pcl->vcnt += (ret >= 0); 719 return ret; 720 } 721 722 static bool z_erofs_get_pcluster(struct z_erofs_pcluster *pcl) 723 { 724 if (lockref_get_not_zero(&pcl->lockref)) 725 return true; 726 727 spin_lock(&pcl->lockref.lock); 728 if (__lockref_is_dead(&pcl->lockref)) { 729 spin_unlock(&pcl->lockref.lock); 730 return false; 731 } 732 733 if (!pcl->lockref.count++) 734 atomic_long_dec(&erofs_global_shrink_cnt); 735 spin_unlock(&pcl->lockref.lock); 736 return true; 737 } 738 739 static int z_erofs_register_pcluster(struct z_erofs_frontend *fe) 740 { 741 struct erofs_map_blocks *map = &fe->map; 742 struct super_block *sb = fe->inode->i_sb; 743 struct erofs_sb_info *sbi = EROFS_SB(sb); 744 struct z_erofs_pcluster *pcl, *pre; 745 unsigned int pageofs_in; 746 int err; 747 748 pageofs_in = erofs_blkoff(sb, map->m_pa); 749 pcl = z_erofs_alloc_pcluster(pageofs_in + map->m_plen); 750 if (IS_ERR(pcl)) 751 return PTR_ERR(pcl); 752 753 lockref_init(&pcl->lockref); /* one ref for this request */ 754 pcl->algorithmformat = map->m_algorithmformat; 755 pcl->pclustersize = map->m_plen; 756 pcl->length = 0; 757 pcl->partial = true; 758 pcl->next = fe->head; 759 pcl->pos = map->m_pa; 760 pcl->pageofs_in = pageofs_in; 761 pcl->pageofs_out = map->m_la & ~PAGE_MASK; 762 pcl->from_meta = map->m_flags & EROFS_MAP_META; 763 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED; 764 765 /* 766 * lock all primary followed works before visible to others 767 * and mutex_trylock *never* fails for a new pcluster. 768 */ 769 mutex_init(&pcl->lock); 770 DBG_BUGON(!mutex_trylock(&pcl->lock)); 771 772 if (!pcl->from_meta) { 773 while (1) { 774 xa_lock(&sbi->managed_pslots); 775 pre = __xa_cmpxchg(&sbi->managed_pslots, pcl->pos, 776 NULL, pcl, GFP_KERNEL); 777 if (!pre || xa_is_err(pre) || z_erofs_get_pcluster(pre)) { 778 xa_unlock(&sbi->managed_pslots); 779 break; 780 } 781 /* try to legitimize the current in-tree one */ 782 xa_unlock(&sbi->managed_pslots); 783 cond_resched(); 784 } 785 if (xa_is_err(pre)) { 786 err = xa_err(pre); 787 goto err_out; 788 } else if (pre) { 789 fe->pcl = pre; 790 err = -EEXIST; 791 goto err_out; 792 } 793 } 794 fe->head = fe->pcl = pcl; 795 return 0; 796 797 err_out: 798 mutex_unlock(&pcl->lock); 799 z_erofs_free_pcluster(pcl); 800 return err; 801 } 802 803 static int z_erofs_pcluster_begin(struct z_erofs_frontend *fe) 804 { 805 struct erofs_map_blocks *map = &fe->map; 806 struct super_block *sb = fe->inode->i_sb; 807 struct z_erofs_pcluster *pcl = NULL; 808 void *ptr = NULL; 809 bool needretry; 810 int ret; 811 812 DBG_BUGON(fe->pcl); 813 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */ 814 DBG_BUGON(!fe->head); 815 816 if (map->m_flags & EROFS_MAP_META) { 817 ret = erofs_init_metabuf(&map->buf, sb, 818 erofs_inode_in_metabox(fe->inode)); 819 if (ret) 820 return ret; 821 ptr = erofs_bread(&map->buf, map->m_pa, false); 822 if (IS_ERR(ptr)) { 823 erofs_err(sb, "failed to read inline data %pe @ pa %llu of nid %llu", 824 ptr, map->m_pa, EROFS_I(fe->inode)->nid); 825 return PTR_ERR(ptr); 826 } 827 ptr = map->buf.page; 828 } else { 829 do { 830 rcu_read_lock(); 831 pcl = xa_load(&EROFS_SB(sb)->managed_pslots, map->m_pa); 832 needretry = pcl && !z_erofs_get_pcluster(pcl); 833 rcu_read_unlock(); 834 } while (needretry); 835 } 836 837 if (pcl) { 838 DBG_BUGON(map->m_pa != pcl->pos); 839 fe->pcl = pcl; 840 ret = -EEXIST; 841 } else { 842 ret = z_erofs_register_pcluster(fe); 843 } 844 845 if (ret == -EEXIST) { 846 mutex_lock(&fe->pcl->lock); 847 /* check if this pcluster hasn't been linked into any chain. */ 848 if (!cmpxchg(&fe->pcl->next, NULL, fe->head)) { 849 /* .. so it can be attached to our submission chain */ 850 fe->head = fe->pcl; 851 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED; 852 } else { /* otherwise, it belongs to an inflight chain */ 853 fe->mode = Z_EROFS_PCLUSTER_INFLIGHT; 854 } 855 } else if (ret) { 856 return ret; 857 } 858 859 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset, 860 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt); 861 if (!fe->pcl->from_meta) { 862 /* bind cache first when cached decompression is preferred */ 863 z_erofs_bind_cache(fe); 864 } else { 865 folio_get(page_folio((struct page *)ptr)); 866 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, ptr); 867 fe->pcl->pageofs_in = map->m_pa & ~PAGE_MASK; 868 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 869 } 870 /* file-backed inplace I/O pages are traversed in reverse order */ 871 fe->icur = z_erofs_pclusterpages(fe->pcl); 872 return 0; 873 } 874 875 static void z_erofs_rcu_callback(struct rcu_head *head) 876 { 877 z_erofs_free_pcluster(container_of(head, struct z_erofs_pcluster, rcu)); 878 } 879 880 static bool __erofs_try_to_release_pcluster(struct erofs_sb_info *sbi, 881 struct z_erofs_pcluster *pcl) 882 { 883 if (pcl->lockref.count) 884 return false; 885 886 /* 887 * Note that all cached folios should be detached before deleted from 888 * the XArray. Otherwise some folios could be still attached to the 889 * orphan old pcluster when the new one is available in the tree. 890 */ 891 if (erofs_try_to_free_all_cached_folios(sbi, pcl)) 892 return false; 893 894 /* 895 * It's impossible to fail after the pcluster is freezed, but in order 896 * to avoid some race conditions, add a DBG_BUGON to observe this. 897 */ 898 DBG_BUGON(__xa_erase(&sbi->managed_pslots, pcl->pos) != pcl); 899 900 lockref_mark_dead(&pcl->lockref); 901 return true; 902 } 903 904 static bool erofs_try_to_release_pcluster(struct erofs_sb_info *sbi, 905 struct z_erofs_pcluster *pcl) 906 { 907 bool free; 908 909 spin_lock(&pcl->lockref.lock); 910 free = __erofs_try_to_release_pcluster(sbi, pcl); 911 spin_unlock(&pcl->lockref.lock); 912 if (free) { 913 atomic_long_dec(&erofs_global_shrink_cnt); 914 call_rcu(&pcl->rcu, z_erofs_rcu_callback); 915 } 916 return free; 917 } 918 919 unsigned long z_erofs_shrink_scan(struct erofs_sb_info *sbi, unsigned long nr) 920 { 921 struct z_erofs_pcluster *pcl; 922 unsigned long index, freed = 0; 923 924 xa_lock(&sbi->managed_pslots); 925 xa_for_each(&sbi->managed_pslots, index, pcl) { 926 /* try to shrink each valid pcluster */ 927 if (!erofs_try_to_release_pcluster(sbi, pcl)) 928 continue; 929 xa_unlock(&sbi->managed_pslots); 930 931 ++freed; 932 if (!--nr) 933 return freed; 934 xa_lock(&sbi->managed_pslots); 935 } 936 xa_unlock(&sbi->managed_pslots); 937 return freed; 938 } 939 940 static void z_erofs_put_pcluster(struct erofs_sb_info *sbi, 941 struct z_erofs_pcluster *pcl, bool try_free) 942 { 943 bool free = false; 944 945 if (lockref_put_or_lock(&pcl->lockref)) 946 return; 947 948 DBG_BUGON(__lockref_is_dead(&pcl->lockref)); 949 if (!--pcl->lockref.count) { 950 if (try_free && xa_trylock(&sbi->managed_pslots)) { 951 free = __erofs_try_to_release_pcluster(sbi, pcl); 952 xa_unlock(&sbi->managed_pslots); 953 } 954 atomic_long_add(!free, &erofs_global_shrink_cnt); 955 } 956 spin_unlock(&pcl->lockref.lock); 957 if (free) 958 call_rcu(&pcl->rcu, z_erofs_rcu_callback); 959 } 960 961 static void z_erofs_pcluster_end(struct z_erofs_frontend *fe) 962 { 963 struct z_erofs_pcluster *pcl = fe->pcl; 964 965 if (!pcl) 966 return; 967 968 z_erofs_bvec_iter_end(&fe->biter); 969 mutex_unlock(&pcl->lock); 970 971 if (fe->candidate_bvpage) 972 fe->candidate_bvpage = NULL; 973 974 /* Drop refcount if it doesn't belong to our processing chain */ 975 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE) 976 z_erofs_put_pcluster(EROFS_I_SB(fe->inode), pcl, false); 977 fe->pcl = NULL; 978 } 979 980 static int z_erofs_read_fragment(struct super_block *sb, struct folio *folio, 981 unsigned int cur, unsigned int end, erofs_off_t pos) 982 { 983 struct inode *packed_inode = EROFS_SB(sb)->packed_inode; 984 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 985 unsigned int cnt; 986 u8 *src; 987 988 if (!packed_inode) 989 return -EFSCORRUPTED; 990 991 buf.mapping = packed_inode->i_mapping; 992 for (; cur < end; cur += cnt, pos += cnt) { 993 cnt = min(end - cur, sb->s_blocksize - erofs_blkoff(sb, pos)); 994 src = erofs_bread(&buf, pos, true); 995 if (IS_ERR(src)) { 996 erofs_put_metabuf(&buf); 997 return PTR_ERR(src); 998 } 999 memcpy_to_folio(folio, cur, src, cnt); 1000 } 1001 erofs_put_metabuf(&buf); 1002 return 0; 1003 } 1004 1005 static int z_erofs_scan_folio(struct z_erofs_frontend *f, 1006 struct folio *folio, bool ra) 1007 { 1008 struct inode *const inode = f->inode; 1009 struct erofs_map_blocks *const map = &f->map; 1010 const loff_t offset = folio_pos(folio); 1011 const unsigned int bs = i_blocksize(inode); 1012 unsigned int end = folio_size(folio), split = 0, cur, pgs; 1013 bool tight, excl; 1014 int err = 0; 1015 1016 tight = (bs == PAGE_SIZE); 1017 erofs_onlinefolio_init(folio); 1018 do { 1019 if (offset + end - 1 < map->m_la || 1020 offset + end - 1 >= map->m_la + map->m_llen) { 1021 z_erofs_pcluster_end(f); 1022 map->m_la = offset + end - 1; 1023 map->m_llen = 0; 1024 err = z_erofs_map_blocks_iter(inode, map, 0); 1025 if (err) 1026 break; 1027 } 1028 1029 cur = offset > map->m_la ? 0 : map->m_la - offset; 1030 pgs = round_down(cur, PAGE_SIZE); 1031 /* bump split parts first to avoid several separate cases */ 1032 ++split; 1033 1034 if (map->m_flags & EROFS_MAP_FRAGMENT) { 1035 erofs_off_t fpos = offset + cur - map->m_la; 1036 1037 err = z_erofs_read_fragment(inode->i_sb, folio, cur, 1038 cur + min(map->m_llen - fpos, end - cur), 1039 EROFS_I(inode)->z_fragmentoff + fpos); 1040 if (err) 1041 break; 1042 tight = false; 1043 } else if (!(map->m_flags & EROFS_MAP_MAPPED)) { 1044 folio_zero_segment(folio, cur, end); 1045 tight = false; 1046 } else { 1047 if (!f->pcl) { 1048 err = z_erofs_pcluster_begin(f); 1049 if (err) 1050 break; 1051 f->pcl->besteffort |= !ra; 1052 } 1053 1054 pgs = round_down(end - 1, PAGE_SIZE); 1055 /* 1056 * Ensure this partial page belongs to this submit chain 1057 * rather than other concurrent submit chains or 1058 * noio(bypass) chains since those chains are handled 1059 * asynchronously thus it cannot be used for inplace I/O 1060 * or bvpage (should be processed in the strict order.) 1061 */ 1062 tight &= (f->mode >= Z_EROFS_PCLUSTER_FOLLOWED); 1063 excl = false; 1064 if (cur <= pgs) { 1065 excl = (split <= 1) || tight; 1066 cur = pgs; 1067 } 1068 1069 err = z_erofs_attach_page(f, &((struct z_erofs_bvec) { 1070 .page = folio_page(folio, pgs >> PAGE_SHIFT), 1071 .offset = offset + pgs - map->m_la, 1072 .end = end - pgs, }), excl); 1073 if (err) 1074 break; 1075 1076 erofs_onlinefolio_split(folio); 1077 if (f->pcl->length < offset + end - map->m_la) { 1078 f->pcl->length = offset + end - map->m_la; 1079 f->pcl->pageofs_out = map->m_la & ~PAGE_MASK; 1080 } 1081 if (EROFS_MAP_FULL(map->m_flags) && 1082 f->pcl->length == map->m_llen) 1083 f->pcl->partial = false; 1084 } 1085 /* shorten the remaining extent to update progress */ 1086 map->m_llen = offset + cur - map->m_la; 1087 map->m_flags |= EROFS_MAP_PARTIAL_MAPPED; 1088 if (cur <= pgs) { 1089 split = cur < pgs; 1090 tight = (bs == PAGE_SIZE); 1091 } 1092 } while ((end = cur) > 0); 1093 erofs_onlinefolio_end(folio, err, false); 1094 return err; 1095 } 1096 1097 static bool z_erofs_page_is_invalidated(struct page *page) 1098 { 1099 return !page_folio(page)->mapping && !z_erofs_is_shortlived_page(page); 1100 } 1101 1102 struct z_erofs_backend { 1103 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES]; 1104 struct super_block *sb; 1105 struct z_erofs_pcluster *pcl; 1106 /* pages with the longest decompressed length for deduplication */ 1107 struct page **decompressed_pages; 1108 /* pages to keep the compressed data */ 1109 struct page **compressed_pages; 1110 1111 struct list_head decompressed_secondary_bvecs; 1112 struct page **pagepool; 1113 unsigned int onstack_used, nr_pages; 1114 /* indicate if temporary copies should be preserved for later use */ 1115 bool keepxcpy; 1116 }; 1117 1118 struct z_erofs_bvec_item { 1119 struct z_erofs_bvec bvec; 1120 struct list_head list; 1121 }; 1122 1123 static void z_erofs_do_decompressed_bvec(struct z_erofs_backend *be, 1124 struct z_erofs_bvec *bvec) 1125 { 1126 int poff = bvec->offset + be->pcl->pageofs_out; 1127 struct z_erofs_bvec_item *item; 1128 struct page **page; 1129 1130 if (!(poff & ~PAGE_MASK) && (bvec->end == PAGE_SIZE || 1131 bvec->offset + bvec->end == be->pcl->length)) { 1132 DBG_BUGON((poff >> PAGE_SHIFT) >= be->nr_pages); 1133 page = be->decompressed_pages + (poff >> PAGE_SHIFT); 1134 if (!*page) { 1135 *page = bvec->page; 1136 return; 1137 } 1138 } else { 1139 be->keepxcpy = true; 1140 } 1141 1142 /* (cold path) one pcluster is requested multiple times */ 1143 item = kmalloc_obj(*item, GFP_KERNEL | __GFP_NOFAIL); 1144 item->bvec = *bvec; 1145 list_add(&item->list, &be->decompressed_secondary_bvecs); 1146 } 1147 1148 static void z_erofs_fill_other_copies(struct z_erofs_backend *be, int err) 1149 { 1150 unsigned int off0 = be->pcl->pageofs_out; 1151 struct list_head *p, *n; 1152 1153 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) { 1154 struct z_erofs_bvec_item *bvi; 1155 unsigned int end, cur; 1156 void *dst, *src; 1157 1158 bvi = container_of(p, struct z_erofs_bvec_item, list); 1159 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0; 1160 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset, 1161 bvi->bvec.end); 1162 dst = kmap_local_page(bvi->bvec.page); 1163 while (cur < end) { 1164 unsigned int pgnr, scur, len; 1165 1166 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT; 1167 DBG_BUGON(pgnr >= be->nr_pages); 1168 1169 scur = bvi->bvec.offset + cur - 1170 ((pgnr << PAGE_SHIFT) - off0); 1171 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur); 1172 if (!be->decompressed_pages[pgnr]) { 1173 err = -EFSCORRUPTED; 1174 cur += len; 1175 continue; 1176 } 1177 src = kmap_local_page(be->decompressed_pages[pgnr]); 1178 memcpy(dst + cur, src + scur, len); 1179 kunmap_local(src); 1180 cur += len; 1181 } 1182 kunmap_local(dst); 1183 erofs_onlinefolio_end(page_folio(bvi->bvec.page), err, true); 1184 list_del(p); 1185 kfree(bvi); 1186 } 1187 } 1188 1189 static void z_erofs_parse_out_bvecs(struct z_erofs_backend *be) 1190 { 1191 struct z_erofs_pcluster *pcl = be->pcl; 1192 struct z_erofs_bvec_iter biter; 1193 struct page *old_bvpage; 1194 int i; 1195 1196 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0); 1197 for (i = 0; i < pcl->vcnt; ++i) { 1198 struct z_erofs_bvec bvec; 1199 1200 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage); 1201 1202 if (old_bvpage) 1203 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 1204 1205 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page)); 1206 z_erofs_do_decompressed_bvec(be, &bvec); 1207 } 1208 1209 old_bvpage = z_erofs_bvec_iter_end(&biter); 1210 if (old_bvpage) 1211 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 1212 } 1213 1214 static int z_erofs_parse_in_bvecs(struct z_erofs_backend *be, bool *overlapped) 1215 { 1216 struct z_erofs_pcluster *pcl = be->pcl; 1217 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 1218 int i, err = 0; 1219 1220 *overlapped = false; 1221 for (i = 0; i < pclusterpages; ++i) { 1222 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i]; 1223 struct page *page = bvec->page; 1224 1225 /* compressed data ought to be valid when decompressing */ 1226 if (IS_ERR(page) || !page) { 1227 bvec->page = NULL; /* clear the failure reason */ 1228 err = page ? PTR_ERR(page) : -EIO; 1229 continue; 1230 } 1231 be->compressed_pages[i] = page; 1232 1233 if (pcl->from_meta || 1234 erofs_folio_is_managed(EROFS_SB(be->sb), page_folio(page))) { 1235 if (!PageUptodate(page)) 1236 err = -EIO; 1237 continue; 1238 } 1239 1240 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1241 if (z_erofs_is_shortlived_page(page)) 1242 continue; 1243 z_erofs_do_decompressed_bvec(be, bvec); 1244 *overlapped = true; 1245 } 1246 return err; 1247 } 1248 1249 static int z_erofs_decompress_pcluster(struct z_erofs_backend *be, bool eio) 1250 { 1251 struct erofs_sb_info *const sbi = EROFS_SB(be->sb); 1252 struct z_erofs_pcluster *pcl = be->pcl; 1253 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 1254 const struct z_erofs_decompressor *alg = 1255 z_erofs_decomp[pcl->algorithmformat]; 1256 bool try_free = true; 1257 int i, j, jtop, err2, err = eio ? -EIO : 0; 1258 struct page *page; 1259 bool overlapped; 1260 const char *reason; 1261 1262 mutex_lock(&pcl->lock); 1263 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT; 1264 1265 /* allocate (de)compressed page arrays if cannot be kept on stack */ 1266 be->decompressed_pages = NULL; 1267 be->compressed_pages = NULL; 1268 be->onstack_used = 0; 1269 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) { 1270 be->decompressed_pages = be->onstack_pages; 1271 be->onstack_used = be->nr_pages; 1272 memset(be->decompressed_pages, 0, 1273 sizeof(struct page *) * be->nr_pages); 1274 } 1275 1276 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES) 1277 be->compressed_pages = be->onstack_pages + be->onstack_used; 1278 1279 if (!be->decompressed_pages) 1280 be->decompressed_pages = 1281 kvzalloc_objs(struct page *, be->nr_pages, 1282 GFP_KERNEL | __GFP_NOFAIL); 1283 if (!be->compressed_pages) 1284 be->compressed_pages = 1285 kvzalloc_objs(struct page *, pclusterpages, 1286 GFP_KERNEL | __GFP_NOFAIL); 1287 1288 z_erofs_parse_out_bvecs(be); 1289 err2 = z_erofs_parse_in_bvecs(be, &overlapped); 1290 if (err2) 1291 err = err2; 1292 if (!err) { 1293 reason = alg->decompress(&(struct z_erofs_decompress_req) { 1294 .sb = be->sb, 1295 .in = be->compressed_pages, 1296 .out = be->decompressed_pages, 1297 .inpages = pclusterpages, 1298 .outpages = be->nr_pages, 1299 .pageofs_in = pcl->pageofs_in, 1300 .pageofs_out = pcl->pageofs_out, 1301 .inputsize = pcl->pclustersize, 1302 .outputsize = pcl->length, 1303 .alg = pcl->algorithmformat, 1304 .inplace_io = overlapped, 1305 .partial_decoding = pcl->partial, 1306 .fillgaps = be->keepxcpy, 1307 .gfp = pcl->besteffort ? GFP_KERNEL : 1308 GFP_NOWAIT | __GFP_NORETRY 1309 }, be->pagepool); 1310 if (IS_ERR(reason)) { 1311 if (pcl->besteffort || reason != ERR_PTR(-ENOMEM)) 1312 erofs_err(be->sb, "failed to decompress (%s) %pe @ pa %llu size %u => %u", 1313 alg->name, reason, pcl->pos, 1314 pcl->pclustersize, pcl->length); 1315 err = PTR_ERR(reason); 1316 } else if (unlikely(reason)) { 1317 erofs_err(be->sb, "failed to decompress (%s) %s @ pa %llu size %u => %u", 1318 alg->name, reason, pcl->pos, 1319 pcl->pclustersize, pcl->length); 1320 err = -EFSCORRUPTED; 1321 } 1322 } 1323 1324 /* must handle all compressed pages before actual file pages */ 1325 if (pcl->from_meta) { 1326 folio_put(page_folio(pcl->compressed_bvecs[0].page)); 1327 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL); 1328 } else { 1329 /* managed folios are still left in compressed_bvecs[] */ 1330 for (i = 0; i < pclusterpages; ++i) { 1331 page = be->compressed_pages[i]; 1332 if (!page) 1333 continue; 1334 if (erofs_folio_is_managed(sbi, page_folio(page))) { 1335 try_free = false; 1336 continue; 1337 } 1338 (void)z_erofs_put_shortlivedpage(be->pagepool, page); 1339 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 1340 } 1341 } 1342 if (be->compressed_pages < be->onstack_pages || 1343 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES) 1344 kvfree(be->compressed_pages); 1345 1346 jtop = 0; 1347 z_erofs_fill_other_copies(be, err); 1348 for (i = 0; i < be->nr_pages; ++i) { 1349 page = be->decompressed_pages[i]; 1350 if (!page) 1351 continue; 1352 1353 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1354 if (!z_erofs_is_shortlived_page(page)) { 1355 erofs_onlinefolio_end(page_folio(page), err, true); 1356 continue; 1357 } 1358 if (pcl->algorithmformat != Z_EROFS_COMPRESSION_LZ4) { 1359 erofs_pagepool_add(be->pagepool, page); 1360 continue; 1361 } 1362 for (j = 0; j < jtop && be->decompressed_pages[j] != page; ++j) 1363 ; 1364 if (j >= jtop) /* this bounce page is newly detected */ 1365 be->decompressed_pages[jtop++] = page; 1366 } 1367 while (jtop) 1368 erofs_pagepool_add(be->pagepool, 1369 be->decompressed_pages[--jtop]); 1370 if (be->decompressed_pages != be->onstack_pages) 1371 kvfree(be->decompressed_pages); 1372 1373 pcl->length = 0; 1374 pcl->partial = true; 1375 pcl->besteffort = false; 1376 pcl->bvset.nextpage = NULL; 1377 pcl->vcnt = 0; 1378 1379 /* pcluster lock MUST be taken before the following line */ 1380 WRITE_ONCE(pcl->next, NULL); 1381 mutex_unlock(&pcl->lock); 1382 1383 if (pcl->from_meta) 1384 z_erofs_free_pcluster(pcl); 1385 else 1386 z_erofs_put_pcluster(sbi, pcl, try_free); 1387 return err; 1388 } 1389 1390 static int z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 1391 struct page **pagepool) 1392 { 1393 struct z_erofs_backend be = { 1394 .sb = io->sb, 1395 .pagepool = pagepool, 1396 .decompressed_secondary_bvecs = 1397 LIST_HEAD_INIT(be.decompressed_secondary_bvecs), 1398 .pcl = io->head, 1399 }; 1400 struct z_erofs_pcluster *next; 1401 int err = 0; 1402 1403 for (; be.pcl != Z_EROFS_PCLUSTER_TAIL; be.pcl = next) { 1404 DBG_BUGON(!be.pcl); 1405 next = READ_ONCE(be.pcl->next); 1406 err = z_erofs_decompress_pcluster(&be, io->eio) ?: err; 1407 } 1408 return err; 1409 } 1410 1411 static void z_erofs_decompressqueue_work(struct work_struct *work) 1412 { 1413 struct z_erofs_decompressqueue *bgq = 1414 container_of(work, struct z_erofs_decompressqueue, u.work); 1415 struct page *pagepool = NULL; 1416 1417 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL); 1418 z_erofs_decompress_queue(bgq, &pagepool); 1419 erofs_release_pages(&pagepool); 1420 kvfree(bgq); 1421 } 1422 1423 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 1424 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work) 1425 { 1426 z_erofs_decompressqueue_work((struct work_struct *)work); 1427 } 1428 #endif 1429 1430 /* Use (kthread_)work in atomic contexts to minimize scheduling overhead */ 1431 static inline bool z_erofs_in_atomic(void) 1432 { 1433 if (IS_ENABLED(CONFIG_PREEMPTION) && rcu_preempt_depth()) 1434 return true; 1435 if (!IS_ENABLED(CONFIG_PREEMPT_COUNT)) 1436 return true; 1437 return !preemptible(); 1438 } 1439 1440 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 1441 int bios) 1442 { 1443 struct erofs_sb_info *const sbi = EROFS_SB(io->sb); 1444 int gfp_flag; 1445 1446 /* wake up the caller thread for sync decompression */ 1447 if (io->sync) { 1448 if (!atomic_add_return(bios, &io->pending_bios)) 1449 complete(&io->u.done); 1450 return; 1451 } 1452 1453 if (atomic_add_return(bios, &io->pending_bios)) 1454 return; 1455 if (z_erofs_in_atomic()) { 1456 /* See `sync_decompress` in sysfs-fs-erofs for more details */ 1457 if (sbi->sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) 1458 sbi->sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON; 1459 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 1460 scoped_guard(rcu) { 1461 struct kthread_worker *worker; 1462 1463 worker = rcu_dereference( 1464 z_erofs_pcpu_workers[raw_smp_processor_id()]); 1465 if (worker) { 1466 kthread_queue_work(worker, &io->u.kthread_work); 1467 return; 1468 } 1469 } 1470 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work); 1471 #endif 1472 queue_work(z_erofs_workqueue, &io->u.work); 1473 return; 1474 } 1475 gfp_flag = memalloc_noio_save(); 1476 z_erofs_decompressqueue_work(&io->u.work); 1477 memalloc_noio_restore(gfp_flag); 1478 } 1479 1480 static void z_erofs_fill_bio_vec(struct bio_vec *bvec, 1481 struct z_erofs_frontend *f, 1482 struct z_erofs_pcluster *pcl, 1483 unsigned int nr, 1484 struct address_space *mc) 1485 { 1486 gfp_t gfp = mapping_gfp_mask(mc); 1487 bool tocache = false; 1488 struct z_erofs_bvec zbv; 1489 struct address_space *mapping; 1490 struct folio *folio; 1491 struct page *page; 1492 int bs = i_blocksize(f->inode); 1493 1494 /* Except for inplace folios, the entire folio can be used for I/Os */ 1495 bvec->bv_offset = 0; 1496 bvec->bv_len = PAGE_SIZE; 1497 repeat: 1498 spin_lock(&pcl->lockref.lock); 1499 zbv = pcl->compressed_bvecs[nr]; 1500 spin_unlock(&pcl->lockref.lock); 1501 if (!zbv.page) 1502 goto out_allocfolio; 1503 1504 bvec->bv_page = zbv.page; 1505 DBG_BUGON(z_erofs_is_shortlived_page(bvec->bv_page)); 1506 1507 folio = page_folio(zbv.page); 1508 /* 1509 * Preallocated folios are added to the managed cache here rather than 1510 * in z_erofs_bind_cache() in order to keep these folios locked in 1511 * increasing (physical) address order. 1512 * Clear folio->private before these folios become visible to others in 1513 * the managed cache to avoid duplicate additions for unaligned extents. 1514 */ 1515 if (folio->private == Z_EROFS_PREALLOCATED_FOLIO) { 1516 folio->private = NULL; 1517 tocache = true; 1518 goto out_tocache; 1519 } 1520 1521 mapping = READ_ONCE(folio->mapping); 1522 /* 1523 * File-backed folios for inplace I/Os are all locked steady, 1524 * therefore it is impossible for `mapping` to be NULL. 1525 */ 1526 if (mapping && mapping != mc) { 1527 if (zbv.offset < 0) 1528 bvec->bv_offset = round_up(-zbv.offset, bs); 1529 bvec->bv_len = round_up(zbv.end, bs) - bvec->bv_offset; 1530 return; 1531 } 1532 1533 folio_lock(folio); 1534 if (likely(folio->mapping == mc)) { 1535 /* 1536 * The cached folio is still in managed cache but without 1537 * a valid `->private` pcluster hint. Let's reconnect them. 1538 */ 1539 if (!folio_test_private(folio)) { 1540 folio_attach_private(folio, pcl); 1541 /* compressed_bvecs[] already takes a ref before */ 1542 folio_put(folio); 1543 } 1544 if (likely(folio->private == pcl)) { 1545 /* don't submit cache I/Os again if already uptodate */ 1546 if (folio_test_uptodate(folio)) { 1547 folio_unlock(folio); 1548 bvec->bv_page = NULL; 1549 } 1550 return; 1551 } 1552 } else { 1553 DBG_BUGON(1); /* referenced managed folios can't be truncated */ 1554 } 1555 folio_unlock(folio); 1556 folio_put(folio); 1557 out_allocfolio: 1558 page = __erofs_allocpage(&f->pagepool, gfp, true); 1559 spin_lock(&pcl->lockref.lock); 1560 if (unlikely(pcl->compressed_bvecs[nr].page != zbv.page)) { 1561 if (page) 1562 erofs_pagepool_add(&f->pagepool, page); 1563 spin_unlock(&pcl->lockref.lock); 1564 cond_resched(); 1565 goto repeat; 1566 } 1567 pcl->compressed_bvecs[nr].page = page ? page : ERR_PTR(-ENOMEM); 1568 spin_unlock(&pcl->lockref.lock); 1569 bvec->bv_page = page; 1570 if (!page) 1571 return; 1572 folio = page_folio(page); 1573 out_tocache: 1574 if (!tocache || bs != PAGE_SIZE || 1575 filemap_add_folio(mc, folio, (pcl->pos >> PAGE_SHIFT) + nr, gfp)) { 1576 /* turn into a temporary shortlived folio (1 ref) */ 1577 folio->private = (void *)Z_EROFS_SHORTLIVED_PAGE; 1578 return; 1579 } 1580 folio_attach_private(folio, pcl); 1581 /* drop a refcount added by allocpage (then 2 refs in total here) */ 1582 folio_put(folio); 1583 } 1584 1585 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb, 1586 struct z_erofs_decompressqueue *fgq, bool *fg) 1587 { 1588 struct z_erofs_decompressqueue *q; 1589 1590 if (fg && !*fg) { 1591 q = kvzalloc_obj(*q, GFP_KERNEL | __GFP_NOWARN); 1592 if (!q) { 1593 *fg = true; 1594 goto fg_out; 1595 } 1596 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 1597 kthread_init_work(&q->u.kthread_work, 1598 z_erofs_decompressqueue_kthread_work); 1599 #else 1600 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1601 #endif 1602 } else { 1603 fg_out: 1604 q = fgq; 1605 init_completion(&fgq->u.done); 1606 atomic_set(&fgq->pending_bios, 0); 1607 q->eio = false; 1608 q->sync = true; 1609 } 1610 q->sb = sb; 1611 q->head = Z_EROFS_PCLUSTER_TAIL; 1612 return q; 1613 } 1614 1615 /* define decompression jobqueue types */ 1616 enum { 1617 JQ_BYPASS, 1618 JQ_SUBMIT, 1619 NR_JOBQUEUES, 1620 }; 1621 1622 static void z_erofs_move_to_bypass_queue(struct z_erofs_pcluster *pcl, 1623 struct z_erofs_pcluster *next, 1624 struct z_erofs_pcluster **qtail[]) 1625 { 1626 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL); 1627 WRITE_ONCE(*qtail[JQ_SUBMIT], next); 1628 WRITE_ONCE(*qtail[JQ_BYPASS], pcl); 1629 qtail[JQ_BYPASS] = &pcl->next; 1630 } 1631 1632 static void z_erofs_endio(struct bio *bio) 1633 { 1634 struct z_erofs_decompressqueue *q = bio->bi_private; 1635 blk_status_t err = bio->bi_status; 1636 struct folio_iter fi; 1637 1638 bio_for_each_folio_all(fi, bio) { 1639 struct folio *folio = fi.folio; 1640 1641 DBG_BUGON(folio_test_uptodate(folio)); 1642 DBG_BUGON(z_erofs_page_is_invalidated(&folio->page)); 1643 if (!erofs_folio_is_managed(EROFS_SB(q->sb), folio)) 1644 continue; 1645 1646 if (!err) 1647 folio_mark_uptodate(folio); 1648 folio_unlock(folio); 1649 } 1650 if (err) 1651 q->eio = true; 1652 z_erofs_decompress_kickoff(q, -1); 1653 if (bio->bi_bdev) 1654 bio_put(bio); 1655 } 1656 1657 static void z_erofs_submit_queue(struct z_erofs_frontend *f, 1658 struct z_erofs_decompressqueue *fgq, 1659 bool *force_fg, bool readahead) 1660 { 1661 struct super_block *sb = f->inode->i_sb; 1662 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb)); 1663 struct z_erofs_pcluster **qtail[NR_JOBQUEUES]; 1664 struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 1665 struct z_erofs_pcluster *pcl, *next; 1666 /* bio is NULL initially, so no need to initialize last_{index,bdev} */ 1667 erofs_off_t last_pa; 1668 unsigned int nr_bios = 0; 1669 struct bio *bio = NULL; 1670 unsigned long pflags; 1671 int memstall = 0; 1672 1673 /* No need to read from device for pclusters in the bypass queue. */ 1674 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1675 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg); 1676 1677 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1678 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 1679 1680 /* by default, all need io submission */ 1681 q[JQ_SUBMIT]->head = next = f->head; 1682 1683 do { 1684 struct erofs_map_dev mdev; 1685 erofs_off_t cur, end; 1686 struct bio_vec bvec; 1687 unsigned int i = 0; 1688 bool bypass = true; 1689 1690 pcl = next; 1691 next = READ_ONCE(pcl->next); 1692 if (pcl->from_meta) { 1693 z_erofs_move_to_bypass_queue(pcl, next, qtail); 1694 continue; 1695 } 1696 1697 /* no device id here, thus it will always succeed */ 1698 mdev = (struct erofs_map_dev) { 1699 .m_pa = round_down(pcl->pos, sb->s_blocksize), 1700 }; 1701 (void)erofs_map_dev(sb, &mdev); 1702 1703 cur = mdev.m_pa; 1704 end = round_up(cur + pcl->pageofs_in + pcl->pclustersize, 1705 sb->s_blocksize); 1706 do { 1707 bvec.bv_page = NULL; 1708 if (bio && (cur != last_pa || 1709 bio->bi_bdev != mdev.m_bdev)) { 1710 drain_io: 1711 if (erofs_is_fileio_mode(EROFS_SB(sb))) 1712 erofs_fileio_submit_bio(bio); 1713 else 1714 submit_bio(bio); 1715 1716 if (memstall) { 1717 psi_memstall_leave(&pflags); 1718 memstall = 0; 1719 } 1720 bio = NULL; 1721 } 1722 1723 if (!bvec.bv_page) { 1724 z_erofs_fill_bio_vec(&bvec, f, pcl, i++, mc); 1725 if (!bvec.bv_page) 1726 continue; 1727 if (cur + bvec.bv_len > end) 1728 bvec.bv_len = end - cur; 1729 DBG_BUGON(bvec.bv_len < sb->s_blocksize); 1730 } 1731 1732 if (unlikely(PageWorkingset(bvec.bv_page)) && 1733 !memstall) { 1734 psi_memstall_enter(&pflags); 1735 memstall = 1; 1736 } 1737 1738 if (!bio) { 1739 if (erofs_is_fileio_mode(EROFS_SB(sb))) 1740 bio = erofs_fileio_bio_alloc(&mdev); 1741 else 1742 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS, 1743 REQ_OP_READ, GFP_NOIO); 1744 bio->bi_end_io = z_erofs_endio; 1745 bio->bi_iter.bi_sector = 1746 (mdev.m_dif->fsoff + cur) >> 9; 1747 bio->bi_private = q[JQ_SUBMIT]; 1748 if (readahead) 1749 bio->bi_opf |= REQ_RAHEAD; 1750 ++nr_bios; 1751 } 1752 1753 if (!bio_add_page(bio, bvec.bv_page, bvec.bv_len, 1754 bvec.bv_offset)) 1755 goto drain_io; 1756 last_pa = cur + bvec.bv_len; 1757 bypass = false; 1758 } while ((cur += bvec.bv_len) < end); 1759 1760 if (!bypass) 1761 qtail[JQ_SUBMIT] = &pcl->next; 1762 else 1763 z_erofs_move_to_bypass_queue(pcl, next, qtail); 1764 } while (next != Z_EROFS_PCLUSTER_TAIL); 1765 1766 if (bio) { 1767 if (erofs_is_fileio_mode(EROFS_SB(sb))) 1768 erofs_fileio_submit_bio(bio); 1769 else 1770 submit_bio(bio); 1771 } 1772 if (memstall) 1773 psi_memstall_leave(&pflags); 1774 1775 /* 1776 * although background is preferred, no one is pending for submission. 1777 * don't issue decompression but drop it directly instead. 1778 */ 1779 if (!*force_fg && !nr_bios) { 1780 kvfree(q[JQ_SUBMIT]); 1781 return; 1782 } 1783 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios); 1784 } 1785 1786 static int z_erofs_runqueue(struct z_erofs_frontend *f, unsigned int rabytes) 1787 { 1788 struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 1789 struct erofs_sb_info *sbi = EROFS_I_SB(f->inode); 1790 int syncmode = sbi->sync_decompress; 1791 bool force_fg; 1792 int err; 1793 1794 force_fg = (syncmode == EROFS_SYNC_DECOMPRESS_AUTO && !rabytes) || 1795 (syncmode == EROFS_SYNC_DECOMPRESS_FORCE_ON && 1796 (rabytes <= Z_EROFS_MAX_SYNC_DECOMPRESS_BYTES)); 1797 1798 if (f->head == Z_EROFS_PCLUSTER_TAIL) 1799 return 0; 1800 z_erofs_submit_queue(f, io, &force_fg, !!rabytes); 1801 1802 /* handle bypass queue (no i/o pclusters) immediately */ 1803 err = z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool); 1804 if (!force_fg) 1805 return err; 1806 1807 /* wait until all bios are completed */ 1808 wait_for_completion_io(&io[JQ_SUBMIT].u.done); 1809 1810 /* handle synchronous decompress queue in the caller context */ 1811 return z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool) ?: err; 1812 } 1813 1814 /* 1815 * Since partial uptodate is still unimplemented for now, we have to use 1816 * approximate readmore strategies as a start. 1817 */ 1818 static void z_erofs_pcluster_readmore(struct z_erofs_frontend *f, 1819 struct readahead_control *rac, bool backmost) 1820 { 1821 struct inode *inode = f->inode; 1822 struct erofs_map_blocks *map = &f->map; 1823 erofs_off_t cur, end, headoffset = f->headoffset; 1824 int err; 1825 1826 if (backmost) { 1827 if (rac) 1828 end = headoffset + readahead_length(rac) - 1; 1829 else 1830 end = headoffset + PAGE_SIZE - 1; 1831 map->m_la = end; 1832 err = z_erofs_map_blocks_iter(inode, map, 1833 EROFS_GET_BLOCKS_READMORE); 1834 if (err || !(map->m_flags & EROFS_MAP_MAPPED)) 1835 return; 1836 1837 /* expand ra for the trailing edge if readahead */ 1838 if (rac) { 1839 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE); 1840 readahead_expand(rac, headoffset, cur - headoffset); 1841 return; 1842 } 1843 end = round_up(end, PAGE_SIZE); 1844 } else { 1845 end = round_up(map->m_la, PAGE_SIZE); 1846 if (!(map->m_flags & EROFS_MAP_MAPPED) || !map->m_llen) 1847 return; 1848 } 1849 1850 cur = map->m_la + map->m_llen - 1; 1851 while ((cur >= end) && (cur < i_size_read(inode))) { 1852 pgoff_t index = cur >> PAGE_SHIFT; 1853 struct folio *folio; 1854 1855 folio = erofs_grab_folio_nowait(f->sharedinode->i_mapping, index); 1856 if (!IS_ERR_OR_NULL(folio)) { 1857 if (folio_test_uptodate(folio)) 1858 folio_unlock(folio); 1859 else 1860 z_erofs_scan_folio(f, folio, !!rac); 1861 folio_put(folio); 1862 } 1863 1864 if (cur < PAGE_SIZE) 1865 break; 1866 cur = ((loff_t)index << PAGE_SHIFT) - 1; 1867 } 1868 } 1869 1870 static int z_erofs_read_folio(struct file *file, struct folio *folio) 1871 { 1872 struct inode *sharedinode = folio->mapping->host; 1873 bool need_iput; 1874 struct inode *realinode = erofs_real_inode(sharedinode, &need_iput); 1875 Z_EROFS_DEFINE_FRONTEND(f, realinode, sharedinode, folio_pos(folio)); 1876 int err; 1877 1878 trace_erofs_read_folio(realinode, folio, false); 1879 z_erofs_pcluster_readmore(&f, NULL, true); 1880 err = z_erofs_scan_folio(&f, folio, false); 1881 z_erofs_pcluster_readmore(&f, NULL, false); 1882 z_erofs_pcluster_end(&f); 1883 1884 /* if some pclusters are ready, need submit them anyway */ 1885 err = z_erofs_runqueue(&f, 0) ?: err; 1886 if (err && err != -EINTR) 1887 erofs_err(realinode->i_sb, "read error %d @ %lu of nid %llu", 1888 err, folio->index, EROFS_I(realinode)->nid); 1889 1890 erofs_put_metabuf(&f.map.buf); 1891 erofs_release_pages(&f.pagepool); 1892 1893 if (need_iput) 1894 iput(realinode); 1895 return err; 1896 } 1897 1898 static void z_erofs_readahead(struct readahead_control *rac) 1899 { 1900 struct inode *sharedinode = rac->mapping->host; 1901 bool need_iput; 1902 struct inode *realinode = erofs_real_inode(sharedinode, &need_iput); 1903 Z_EROFS_DEFINE_FRONTEND(f, realinode, sharedinode, readahead_pos(rac)); 1904 unsigned int nrpages = readahead_count(rac); 1905 struct folio *head = NULL, *folio; 1906 int err; 1907 1908 trace_erofs_readahead(realinode, readahead_index(rac), nrpages, false); 1909 z_erofs_pcluster_readmore(&f, rac, true); 1910 while ((folio = readahead_folio(rac))) { 1911 folio->private = head; 1912 head = folio; 1913 } 1914 1915 /* traverse in reverse order for best metadata I/O performance */ 1916 while (head) { 1917 folio = head; 1918 head = folio_get_private(folio); 1919 1920 err = z_erofs_scan_folio(&f, folio, true); 1921 if (err && err != -EINTR) 1922 erofs_err(realinode->i_sb, "readahead error at folio %lu @ nid %llu", 1923 folio->index, EROFS_I(realinode)->nid); 1924 } 1925 z_erofs_pcluster_readmore(&f, rac, false); 1926 z_erofs_pcluster_end(&f); 1927 1928 (void)z_erofs_runqueue(&f, nrpages << PAGE_SHIFT); 1929 erofs_put_metabuf(&f.map.buf); 1930 erofs_release_pages(&f.pagepool); 1931 1932 if (need_iput) 1933 iput(realinode); 1934 } 1935 1936 const struct address_space_operations z_erofs_aops = { 1937 .read_folio = z_erofs_read_folio, 1938 .readahead = z_erofs_readahead, 1939 }; 1940