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