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