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