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