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 int z_erofs_lookup_collection(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 -ENOENT; 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 -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 -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 -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 0; 398 } 399 400 static int z_erofs_register_collection(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 -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 -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 0; 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 int ret; 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 ret = z_erofs_lookup_collection(clt, inode, map); 475 if (ret == -ENOENT) { 476 ret = z_erofs_register_collection(clt, inode, map); 477 478 /* someone registered at the same time, give another try */ 479 if (ret == -EAGAIN) { 480 cond_resched(); 481 goto repeat; 482 } 483 } 484 485 if (ret) 486 return ret; 487 488 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS, 489 clt->cl->pagevec, clt->cl->vcnt); 490 491 clt->compressedpages = clt->pcl->compressed_pages; 492 if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */ 493 clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES; 494 return 0; 495 } 496 497 /* 498 * keep in mind that no referenced pclusters will be freed 499 * only after a RCU grace period. 500 */ 501 static void z_erofs_rcu_callback(struct rcu_head *head) 502 { 503 struct z_erofs_collection *const cl = 504 container_of(head, struct z_erofs_collection, rcu); 505 506 kmem_cache_free(pcluster_cachep, 507 container_of(cl, struct z_erofs_pcluster, 508 primary_collection)); 509 } 510 511 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 512 { 513 struct z_erofs_pcluster *const pcl = 514 container_of(grp, struct z_erofs_pcluster, obj); 515 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl); 516 517 call_rcu(&cl->rcu, z_erofs_rcu_callback); 518 } 519 520 static void z_erofs_collection_put(struct z_erofs_collection *cl) 521 { 522 struct z_erofs_pcluster *const pcl = 523 container_of(cl, struct z_erofs_pcluster, primary_collection); 524 525 erofs_workgroup_put(&pcl->obj); 526 } 527 528 static bool z_erofs_collector_end(struct z_erofs_collector *clt) 529 { 530 struct z_erofs_collection *cl = clt->cl; 531 532 if (!cl) 533 return false; 534 535 z_erofs_pagevec_ctor_exit(&clt->vector, false); 536 mutex_unlock(&cl->lock); 537 538 /* 539 * if all pending pages are added, don't hold its reference 540 * any longer if the pcluster isn't hosted by ourselves. 541 */ 542 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE) 543 z_erofs_collection_put(cl); 544 545 clt->cl = NULL; 546 return true; 547 } 548 549 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe, 550 unsigned int cachestrategy, 551 erofs_off_t la) 552 { 553 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 554 return false; 555 556 if (fe->backmost) 557 return true; 558 559 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 560 la < fe->headoffset; 561 } 562 563 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 564 struct page *page, 565 struct list_head *pagepool) 566 { 567 struct inode *const inode = fe->inode; 568 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 569 struct erofs_map_blocks *const map = &fe->map; 570 struct z_erofs_collector *const clt = &fe->clt; 571 const loff_t offset = page_offset(page); 572 bool tight = true; 573 574 enum z_erofs_cache_alloctype cache_strategy; 575 enum z_erofs_page_type page_type; 576 unsigned int cur, end, spiltted, index; 577 int err = 0; 578 579 /* register locked file pages as online pages in pack */ 580 z_erofs_onlinepage_init(page); 581 582 spiltted = 0; 583 end = PAGE_SIZE; 584 repeat: 585 cur = end - 1; 586 587 /* lucky, within the range of the current map_blocks */ 588 if (offset + cur >= map->m_la && 589 offset + cur < map->m_la + map->m_llen) { 590 /* didn't get a valid collection previously (very rare) */ 591 if (!clt->cl) 592 goto restart_now; 593 goto hitted; 594 } 595 596 /* go ahead the next map_blocks */ 597 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur); 598 599 if (z_erofs_collector_end(clt)) 600 fe->backmost = false; 601 602 map->m_la = offset + cur; 603 map->m_llen = 0; 604 err = z_erofs_map_blocks_iter(inode, map, 0); 605 if (err) 606 goto err_out; 607 608 restart_now: 609 if (!(map->m_flags & EROFS_MAP_MAPPED)) 610 goto hitted; 611 612 err = z_erofs_collector_begin(clt, inode, map); 613 if (err) 614 goto err_out; 615 616 /* preload all compressed pages (maybe downgrade role if necessary) */ 617 if (should_alloc_managed_pages(fe, sbi->cache_strategy, map->m_la)) 618 cache_strategy = DELAYEDALLOC; 619 else 620 cache_strategy = DONTALLOC; 621 622 preload_compressed_pages(clt, MNGD_MAPPING(sbi), 623 cache_strategy, pagepool); 624 625 hitted: 626 /* 627 * Ensure the current partial page belongs to this submit chain rather 628 * than other concurrent submit chains or the noio(bypass) chain since 629 * those chains are handled asynchronously thus the page cannot be used 630 * for inplace I/O or pagevec (should be processed in strict order.) 631 */ 632 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED && 633 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE); 634 635 cur = end - min_t(unsigned int, offset + end - map->m_la, end); 636 if (!(map->m_flags & EROFS_MAP_MAPPED)) { 637 zero_user_segment(page, cur, end); 638 goto next_part; 639 } 640 641 /* let's derive page type */ 642 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD : 643 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 644 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 645 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED)); 646 647 if (cur) 648 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED); 649 650 retry: 651 err = z_erofs_attach_page(clt, page, page_type); 652 /* should allocate an additional staging page for pagevec */ 653 if (err == -EAGAIN) { 654 struct page *const newpage = 655 erofs_allocpage(pagepool, GFP_NOFS | __GFP_NOFAIL); 656 657 newpage->mapping = Z_EROFS_MAPPING_STAGING; 658 err = z_erofs_attach_page(clt, newpage, 659 Z_EROFS_PAGE_TYPE_EXCLUSIVE); 660 if (!err) 661 goto retry; 662 } 663 664 if (err) 665 goto err_out; 666 667 index = page->index - (map->m_la >> PAGE_SHIFT); 668 669 z_erofs_onlinepage_fixup(page, index, true); 670 671 /* bump up the number of spiltted parts of a page */ 672 ++spiltted; 673 /* also update nr_pages */ 674 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1); 675 next_part: 676 /* can be used for verification */ 677 map->m_llen = offset + cur - map->m_la; 678 679 end = cur; 680 if (end > 0) 681 goto repeat; 682 683 out: 684 z_erofs_onlinepage_endio(page); 685 686 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", 687 __func__, page, spiltted, map->m_llen); 688 return err; 689 690 /* if some error occurred while processing this page */ 691 err_out: 692 SetPageError(page); 693 goto out; 694 } 695 696 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 697 bool sync, int bios) 698 { 699 /* wake up the caller thread for sync decompression */ 700 if (sync) { 701 unsigned long flags; 702 703 spin_lock_irqsave(&io->u.wait.lock, flags); 704 if (!atomic_add_return(bios, &io->pending_bios)) 705 wake_up_locked(&io->u.wait); 706 spin_unlock_irqrestore(&io->u.wait.lock, flags); 707 return; 708 } 709 710 if (!atomic_add_return(bios, &io->pending_bios)) 711 queue_work(z_erofs_workqueue, &io->u.work); 712 } 713 714 static void z_erofs_decompressqueue_endio(struct bio *bio) 715 { 716 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private); 717 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t); 718 blk_status_t err = bio->bi_status; 719 struct bio_vec *bvec; 720 struct bvec_iter_all iter_all; 721 722 bio_for_each_segment_all(bvec, bio, iter_all) { 723 struct page *page = bvec->bv_page; 724 725 DBG_BUGON(PageUptodate(page)); 726 DBG_BUGON(!page->mapping); 727 728 if (err) 729 SetPageError(page); 730 731 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 732 if (!err) 733 SetPageUptodate(page); 734 unlock_page(page); 735 } 736 } 737 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1); 738 bio_put(bio); 739 } 740 741 static int z_erofs_decompress_pcluster(struct super_block *sb, 742 struct z_erofs_pcluster *pcl, 743 struct list_head *pagepool) 744 { 745 struct erofs_sb_info *const sbi = EROFS_SB(sb); 746 const unsigned int clusterpages = BIT(pcl->clusterbits); 747 struct z_erofs_pagevec_ctor ctor; 748 unsigned int i, outputsize, llen, nr_pages; 749 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES]; 750 struct page **pages, **compressed_pages, *page; 751 752 enum z_erofs_page_type page_type; 753 bool overlapped, partial; 754 struct z_erofs_collection *cl; 755 int err; 756 757 might_sleep(); 758 cl = z_erofs_primarycollection(pcl); 759 DBG_BUGON(!READ_ONCE(cl->nr_pages)); 760 761 mutex_lock(&cl->lock); 762 nr_pages = cl->nr_pages; 763 764 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) { 765 pages = pages_onstack; 766 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES && 767 mutex_trylock(&z_pagemap_global_lock)) { 768 pages = z_pagemap_global; 769 } else { 770 gfp_t gfp_flags = GFP_KERNEL; 771 772 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES) 773 gfp_flags |= __GFP_NOFAIL; 774 775 pages = kvmalloc_array(nr_pages, sizeof(struct page *), 776 gfp_flags); 777 778 /* fallback to global pagemap for the lowmem scenario */ 779 if (!pages) { 780 mutex_lock(&z_pagemap_global_lock); 781 pages = z_pagemap_global; 782 } 783 } 784 785 for (i = 0; i < nr_pages; ++i) 786 pages[i] = NULL; 787 788 err = 0; 789 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS, 790 cl->pagevec, 0); 791 792 for (i = 0; i < cl->vcnt; ++i) { 793 unsigned int pagenr; 794 795 page = z_erofs_pagevec_dequeue(&ctor, &page_type); 796 797 /* all pages in pagevec ought to be valid */ 798 DBG_BUGON(!page); 799 DBG_BUGON(!page->mapping); 800 801 if (z_erofs_put_stagingpage(pagepool, page)) 802 continue; 803 804 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD) 805 pagenr = 0; 806 else 807 pagenr = z_erofs_onlinepage_index(page); 808 809 DBG_BUGON(pagenr >= nr_pages); 810 811 /* 812 * currently EROFS doesn't support multiref(dedup), 813 * so here erroring out one multiref page. 814 */ 815 if (pages[pagenr]) { 816 DBG_BUGON(1); 817 SetPageError(pages[pagenr]); 818 z_erofs_onlinepage_endio(pages[pagenr]); 819 err = -EFSCORRUPTED; 820 } 821 pages[pagenr] = page; 822 } 823 z_erofs_pagevec_ctor_exit(&ctor, true); 824 825 overlapped = false; 826 compressed_pages = pcl->compressed_pages; 827 828 for (i = 0; i < clusterpages; ++i) { 829 unsigned int pagenr; 830 831 page = compressed_pages[i]; 832 833 /* all compressed pages ought to be valid */ 834 DBG_BUGON(!page); 835 DBG_BUGON(!page->mapping); 836 837 if (!z_erofs_page_is_staging(page)) { 838 if (erofs_page_is_managed(sbi, page)) { 839 if (!PageUptodate(page)) 840 err = -EIO; 841 continue; 842 } 843 844 /* 845 * only if non-head page can be selected 846 * for inplace decompression 847 */ 848 pagenr = z_erofs_onlinepage_index(page); 849 850 DBG_BUGON(pagenr >= nr_pages); 851 if (pages[pagenr]) { 852 DBG_BUGON(1); 853 SetPageError(pages[pagenr]); 854 z_erofs_onlinepage_endio(pages[pagenr]); 855 err = -EFSCORRUPTED; 856 } 857 pages[pagenr] = page; 858 859 overlapped = true; 860 } 861 862 /* PG_error needs checking for inplaced and staging pages */ 863 if (PageError(page)) { 864 DBG_BUGON(PageUptodate(page)); 865 err = -EIO; 866 } 867 } 868 869 if (err) 870 goto out; 871 872 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT; 873 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) { 874 outputsize = llen; 875 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH); 876 } else { 877 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs; 878 partial = true; 879 } 880 881 err = z_erofs_decompress(&(struct z_erofs_decompress_req) { 882 .sb = sb, 883 .in = compressed_pages, 884 .out = pages, 885 .pageofs_out = cl->pageofs, 886 .inputsize = PAGE_SIZE, 887 .outputsize = outputsize, 888 .alg = pcl->algorithmformat, 889 .inplace_io = overlapped, 890 .partial_decoding = partial 891 }, pagepool); 892 893 out: 894 /* must handle all compressed pages before endding pages */ 895 for (i = 0; i < clusterpages; ++i) { 896 page = compressed_pages[i]; 897 898 if (erofs_page_is_managed(sbi, page)) 899 continue; 900 901 /* recycle all individual staging pages */ 902 (void)z_erofs_put_stagingpage(pagepool, page); 903 904 WRITE_ONCE(compressed_pages[i], NULL); 905 } 906 907 for (i = 0; i < nr_pages; ++i) { 908 page = pages[i]; 909 if (!page) 910 continue; 911 912 DBG_BUGON(!page->mapping); 913 914 /* recycle all individual staging pages */ 915 if (z_erofs_put_stagingpage(pagepool, page)) 916 continue; 917 918 if (err < 0) 919 SetPageError(page); 920 921 z_erofs_onlinepage_endio(page); 922 } 923 924 if (pages == z_pagemap_global) 925 mutex_unlock(&z_pagemap_global_lock); 926 else if (pages != pages_onstack) 927 kvfree(pages); 928 929 cl->nr_pages = 0; 930 cl->vcnt = 0; 931 932 /* all cl locks MUST be taken before the following line */ 933 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 934 935 /* all cl locks SHOULD be released right now */ 936 mutex_unlock(&cl->lock); 937 938 z_erofs_collection_put(cl); 939 return err; 940 } 941 942 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 943 struct list_head *pagepool) 944 { 945 z_erofs_next_pcluster_t owned = io->head; 946 947 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { 948 struct z_erofs_pcluster *pcl; 949 950 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ 951 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); 952 953 /* no possible that 'owned' equals NULL */ 954 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 955 956 pcl = container_of(owned, struct z_erofs_pcluster, next); 957 owned = READ_ONCE(pcl->next); 958 959 z_erofs_decompress_pcluster(io->sb, pcl, pagepool); 960 } 961 } 962 963 static void z_erofs_decompressqueue_work(struct work_struct *work) 964 { 965 struct z_erofs_decompressqueue *bgq = 966 container_of(work, struct z_erofs_decompressqueue, u.work); 967 LIST_HEAD(pagepool); 968 969 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 970 z_erofs_decompress_queue(bgq, &pagepool); 971 972 put_pages_list(&pagepool); 973 kvfree(bgq); 974 } 975 976 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 977 unsigned int nr, 978 struct list_head *pagepool, 979 struct address_space *mc, 980 gfp_t gfp) 981 { 982 const pgoff_t index = pcl->obj.index; 983 bool tocache = false; 984 985 struct address_space *mapping; 986 struct page *oldpage, *page; 987 988 compressed_page_t t; 989 int justfound; 990 991 repeat: 992 page = READ_ONCE(pcl->compressed_pages[nr]); 993 oldpage = page; 994 995 if (!page) 996 goto out_allocpage; 997 998 /* 999 * the cached page has not been allocated and 1000 * an placeholder is out there, prepare it now. 1001 */ 1002 if (page == PAGE_UNALLOCATED) { 1003 tocache = true; 1004 goto out_allocpage; 1005 } 1006 1007 /* process the target tagged pointer */ 1008 t = tagptr_init(compressed_page_t, page); 1009 justfound = tagptr_unfold_tags(t); 1010 page = tagptr_unfold_ptr(t); 1011 1012 mapping = READ_ONCE(page->mapping); 1013 1014 /* 1015 * unmanaged (file) pages are all locked solidly, 1016 * therefore it is impossible for `mapping' to be NULL. 1017 */ 1018 if (mapping && mapping != mc) 1019 /* ought to be unmanaged pages */ 1020 goto out; 1021 1022 lock_page(page); 1023 1024 /* only true if page reclaim goes wrong, should never happen */ 1025 DBG_BUGON(justfound && PagePrivate(page)); 1026 1027 /* the page is still in manage cache */ 1028 if (page->mapping == mc) { 1029 WRITE_ONCE(pcl->compressed_pages[nr], page); 1030 1031 ClearPageError(page); 1032 if (!PagePrivate(page)) { 1033 /* 1034 * impossible to be !PagePrivate(page) for 1035 * the current restriction as well if 1036 * the page is already in compressed_pages[]. 1037 */ 1038 DBG_BUGON(!justfound); 1039 1040 justfound = 0; 1041 set_page_private(page, (unsigned long)pcl); 1042 SetPagePrivate(page); 1043 } 1044 1045 /* no need to submit io if it is already up-to-date */ 1046 if (PageUptodate(page)) { 1047 unlock_page(page); 1048 page = NULL; 1049 } 1050 goto out; 1051 } 1052 1053 /* 1054 * the managed page has been truncated, it's unsafe to 1055 * reuse this one, let's allocate a new cache-managed page. 1056 */ 1057 DBG_BUGON(page->mapping); 1058 DBG_BUGON(!justfound); 1059 1060 tocache = true; 1061 unlock_page(page); 1062 put_page(page); 1063 out_allocpage: 1064 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 1065 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1066 /* non-LRU / non-movable temporary page is needed */ 1067 page->mapping = Z_EROFS_MAPPING_STAGING; 1068 tocache = false; 1069 } 1070 1071 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) { 1072 if (tocache) { 1073 /* since it added to managed cache successfully */ 1074 unlock_page(page); 1075 put_page(page); 1076 } else { 1077 list_add(&page->lru, pagepool); 1078 } 1079 cond_resched(); 1080 goto repeat; 1081 } 1082 set_page_private(page, (unsigned long)pcl); 1083 SetPagePrivate(page); 1084 out: /* the only exit (for tracing and debugging) */ 1085 return page; 1086 } 1087 1088 static struct z_erofs_decompressqueue * 1089 jobqueue_init(struct super_block *sb, 1090 struct z_erofs_decompressqueue *fgq, bool *fg) 1091 { 1092 struct z_erofs_decompressqueue *q; 1093 1094 if (fg && !*fg) { 1095 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1096 if (!q) { 1097 *fg = true; 1098 goto fg_out; 1099 } 1100 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1101 } else { 1102 fg_out: 1103 q = fgq; 1104 init_waitqueue_head(&fgq->u.wait); 1105 atomic_set(&fgq->pending_bios, 0); 1106 } 1107 q->sb = sb; 1108 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1109 return q; 1110 } 1111 1112 /* define decompression jobqueue types */ 1113 enum { 1114 JQ_BYPASS, 1115 JQ_SUBMIT, 1116 NR_JOBQUEUES, 1117 }; 1118 1119 static void *jobqueueset_init(struct super_block *sb, 1120 struct z_erofs_decompressqueue *q[], 1121 struct z_erofs_decompressqueue *fgq, bool *fg) 1122 { 1123 /* 1124 * if managed cache is enabled, bypass jobqueue is needed, 1125 * no need to read from device for all pclusters in this queue. 1126 */ 1127 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1128 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg); 1129 1130 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg)); 1131 } 1132 1133 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 1134 z_erofs_next_pcluster_t qtail[], 1135 z_erofs_next_pcluster_t owned_head) 1136 { 1137 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 1138 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 1139 1140 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 1141 if (owned_head == Z_EROFS_PCLUSTER_TAIL) 1142 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1143 1144 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); 1145 1146 WRITE_ONCE(*submit_qtail, owned_head); 1147 WRITE_ONCE(*bypass_qtail, &pcl->next); 1148 1149 qtail[JQ_BYPASS] = &pcl->next; 1150 } 1151 1152 static bool postsubmit_is_all_bypassed(struct z_erofs_decompressqueue *q[], 1153 unsigned int nr_bios, bool force_fg) 1154 { 1155 /* 1156 * although background is preferred, no one is pending for submission. 1157 * don't issue workqueue for decompression but drop it directly instead. 1158 */ 1159 if (force_fg || nr_bios) 1160 return false; 1161 1162 kvfree(q[JQ_SUBMIT]); 1163 return true; 1164 } 1165 1166 static bool z_erofs_submit_queue(struct super_block *sb, 1167 z_erofs_next_pcluster_t owned_head, 1168 struct list_head *pagepool, 1169 struct z_erofs_decompressqueue *fgq, 1170 bool *force_fg) 1171 { 1172 struct erofs_sb_info *const sbi = EROFS_SB(sb); 1173 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1174 struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 1175 struct bio *bio; 1176 void *bi_private; 1177 /* since bio will be NULL, no need to initialize last_index */ 1178 pgoff_t uninitialized_var(last_index); 1179 bool force_submit = false; 1180 unsigned int nr_bios; 1181 1182 if (owned_head == Z_EROFS_PCLUSTER_TAIL) 1183 return false; 1184 1185 force_submit = false; 1186 bio = NULL; 1187 nr_bios = 0; 1188 bi_private = jobqueueset_init(sb, q, fgq, force_fg); 1189 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1190 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 1191 1192 /* by default, all need io submission */ 1193 q[JQ_SUBMIT]->head = owned_head; 1194 1195 do { 1196 struct z_erofs_pcluster *pcl; 1197 unsigned int clusterpages; 1198 pgoff_t first_index; 1199 struct page *page; 1200 unsigned int i = 0, bypass = 0; 1201 int err; 1202 1203 /* no possible 'owned_head' equals the following */ 1204 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 1205 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 1206 1207 pcl = container_of(owned_head, struct z_erofs_pcluster, next); 1208 1209 clusterpages = BIT(pcl->clusterbits); 1210 1211 /* close the main owned chain at first */ 1212 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 1213 Z_EROFS_PCLUSTER_TAIL_CLOSED); 1214 1215 first_index = pcl->obj.index; 1216 force_submit |= (first_index != last_index + 1); 1217 1218 repeat: 1219 page = pickup_page_for_submission(pcl, i, pagepool, 1220 MNGD_MAPPING(sbi), 1221 GFP_NOFS); 1222 if (!page) { 1223 force_submit = true; 1224 ++bypass; 1225 goto skippage; 1226 } 1227 1228 if (bio && force_submit) { 1229 submit_bio_retry: 1230 submit_bio(bio); 1231 bio = NULL; 1232 } 1233 1234 if (!bio) { 1235 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES); 1236 1237 bio->bi_end_io = z_erofs_decompressqueue_endio; 1238 bio_set_dev(bio, sb->s_bdev); 1239 bio->bi_iter.bi_sector = (sector_t)(first_index + i) << 1240 LOG_SECTORS_PER_BLOCK; 1241 bio->bi_private = bi_private; 1242 bio->bi_opf = REQ_OP_READ; 1243 1244 ++nr_bios; 1245 } 1246 1247 err = bio_add_page(bio, page, PAGE_SIZE, 0); 1248 if (err < PAGE_SIZE) 1249 goto submit_bio_retry; 1250 1251 force_submit = false; 1252 last_index = first_index + i; 1253 skippage: 1254 if (++i < clusterpages) 1255 goto repeat; 1256 1257 if (bypass < clusterpages) 1258 qtail[JQ_SUBMIT] = &pcl->next; 1259 else 1260 move_to_bypass_jobqueue(pcl, qtail, owned_head); 1261 } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 1262 1263 if (bio) 1264 submit_bio(bio); 1265 1266 if (postsubmit_is_all_bypassed(q, nr_bios, *force_fg)) 1267 return true; 1268 1269 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios); 1270 return true; 1271 } 1272 1273 static void z_erofs_runqueue(struct super_block *sb, 1274 struct z_erofs_collector *clt, 1275 struct list_head *pagepool, bool force_fg) 1276 { 1277 struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 1278 1279 if (!z_erofs_submit_queue(sb, clt->owned_head, 1280 pagepool, io, &force_fg)) 1281 return; 1282 1283 /* handle bypass queue (no i/o pclusters) immediately */ 1284 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool); 1285 1286 if (!force_fg) 1287 return; 1288 1289 /* wait until all bios are completed */ 1290 io_wait_event(io[JQ_SUBMIT].u.wait, 1291 !atomic_read(&io[JQ_SUBMIT].pending_bios)); 1292 1293 /* handle synchronous decompress queue in the caller context */ 1294 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool); 1295 } 1296 1297 static int z_erofs_readpage(struct file *file, struct page *page) 1298 { 1299 struct inode *const inode = page->mapping->host; 1300 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1301 int err; 1302 LIST_HEAD(pagepool); 1303 1304 trace_erofs_readpage(page, false); 1305 1306 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 1307 1308 err = z_erofs_do_read_page(&f, page, &pagepool); 1309 (void)z_erofs_collector_end(&f.clt); 1310 1311 /* if some compressed cluster ready, need submit them anyway */ 1312 z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, true); 1313 1314 if (err) 1315 erofs_err(inode->i_sb, "failed to read, err [%d]", err); 1316 1317 if (f.map.mpage) 1318 put_page(f.map.mpage); 1319 1320 /* clean up the remaining free pages */ 1321 put_pages_list(&pagepool); 1322 return err; 1323 } 1324 1325 static bool should_decompress_synchronously(struct erofs_sb_info *sbi, 1326 unsigned int nr) 1327 { 1328 return nr <= sbi->max_sync_decompress_pages; 1329 } 1330 1331 static int z_erofs_readpages(struct file *filp, struct address_space *mapping, 1332 struct list_head *pages, unsigned int nr_pages) 1333 { 1334 struct inode *const inode = mapping->host; 1335 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 1336 1337 bool sync = should_decompress_synchronously(sbi, nr_pages); 1338 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1339 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL); 1340 struct page *head = NULL; 1341 LIST_HEAD(pagepool); 1342 1343 trace_erofs_readpages(mapping->host, lru_to_page(pages), 1344 nr_pages, false); 1345 1346 f.headoffset = (erofs_off_t)lru_to_page(pages)->index << PAGE_SHIFT; 1347 1348 for (; nr_pages; --nr_pages) { 1349 struct page *page = lru_to_page(pages); 1350 1351 prefetchw(&page->flags); 1352 list_del(&page->lru); 1353 1354 /* 1355 * A pure asynchronous readahead is indicated if 1356 * a PG_readahead marked page is hitted at first. 1357 * Let's also do asynchronous decompression for this case. 1358 */ 1359 sync &= !(PageReadahead(page) && !head); 1360 1361 if (add_to_page_cache_lru(page, mapping, page->index, gfp)) { 1362 list_add(&page->lru, &pagepool); 1363 continue; 1364 } 1365 1366 set_page_private(page, (unsigned long)head); 1367 head = page; 1368 } 1369 1370 while (head) { 1371 struct page *page = head; 1372 int err; 1373 1374 /* traversal in reverse order */ 1375 head = (void *)page_private(page); 1376 1377 err = z_erofs_do_read_page(&f, page, &pagepool); 1378 if (err) 1379 erofs_err(inode->i_sb, 1380 "readahead error at page %lu @ nid %llu", 1381 page->index, EROFS_I(inode)->nid); 1382 put_page(page); 1383 } 1384 1385 (void)z_erofs_collector_end(&f.clt); 1386 1387 z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, sync); 1388 1389 if (f.map.mpage) 1390 put_page(f.map.mpage); 1391 1392 /* clean up the remaining free pages */ 1393 put_pages_list(&pagepool); 1394 return 0; 1395 } 1396 1397 const struct address_space_operations z_erofs_aops = { 1398 .readpage = z_erofs_readpage, 1399 .readpages = z_erofs_readpages, 1400 }; 1401 1402