1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2022 Alibaba Cloud
9 #include <linux/prefetch.h>
10 #include <linux/psi.h>
12 #include <trace/events/erofs.h>
15 * since pclustersize is variable for big pcluster feature, introduce slab
16 * pools implementation for different pcluster sizes.
18 struct z_erofs_pcluster_slab {
19 struct kmem_cache *slab;
20 unsigned int maxpages;
24 #define _PCLP(n) { .maxpages = n }
26 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
27 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
28 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
31 struct z_erofs_bvec_iter {
33 struct z_erofs_bvset *bvset;
37 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
40 kunmap_local(iter->bvset);
44 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
46 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
47 /* have to access nextpage in advance, otherwise it will be unmapped */
48 struct page *nextpage = iter->bvset->nextpage;
52 oldpage = z_erofs_bvec_iter_end(iter);
53 iter->bvpage = nextpage;
54 iter->bvset = kmap_local_page(nextpage);
55 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
60 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
61 struct z_erofs_bvset_inline *bvset,
62 unsigned int bootstrap_nr,
65 *iter = (struct z_erofs_bvec_iter) {
67 .bvset = (struct z_erofs_bvset *)bvset,
70 while (cur > iter->nr) {
72 z_erofs_bvset_flip(iter);
77 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
78 struct z_erofs_bvec *bvec,
79 struct page **candidate_bvpage)
81 if (iter->cur == iter->nr) {
82 if (!*candidate_bvpage)
85 DBG_BUGON(iter->bvset->nextpage);
86 iter->bvset->nextpage = *candidate_bvpage;
87 z_erofs_bvset_flip(iter);
89 iter->bvset->nextpage = NULL;
90 *candidate_bvpage = NULL;
92 iter->bvset->bvec[iter->cur++] = *bvec;
96 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
97 struct z_erofs_bvec *bvec,
98 struct page **old_bvpage)
100 if (iter->cur == iter->nr)
101 *old_bvpage = z_erofs_bvset_flip(iter);
104 *bvec = iter->bvset->bvec[iter->cur++];
107 static void z_erofs_destroy_pcluster_pool(void)
111 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
112 if (!pcluster_pool[i].slab)
114 kmem_cache_destroy(pcluster_pool[i].slab);
115 pcluster_pool[i].slab = NULL;
119 static int z_erofs_create_pcluster_pool(void)
121 struct z_erofs_pcluster_slab *pcs;
122 struct z_erofs_pcluster *a;
125 for (pcs = pcluster_pool;
126 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
127 size = struct_size(a, compressed_bvecs, pcs->maxpages);
129 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
130 pcs->slab = kmem_cache_create(pcs->name, size, 0,
131 SLAB_RECLAIM_ACCOUNT, NULL);
135 z_erofs_destroy_pcluster_pool();
141 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
145 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
146 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
147 struct z_erofs_pcluster *pcl;
149 if (nrpages > pcs->maxpages)
152 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
154 return ERR_PTR(-ENOMEM);
155 pcl->pclusterpages = nrpages;
158 return ERR_PTR(-EINVAL);
161 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
163 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
166 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
167 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
169 if (pclusterpages > pcs->maxpages)
172 kmem_cache_free(pcs->slab, pcl);
178 /* how to allocate cached pages for a pcluster */
179 enum z_erofs_cache_alloctype {
180 DONTALLOC, /* don't allocate any cached pages */
182 * try to use cached I/O if page allocation succeeds or fallback
183 * to in-place I/O instead to avoid any direct reclaim.
189 * tagged pointer with 1-bit tag for all compressed pages
190 * tag 0 - the page is just found with an extra page reference
192 typedef tagptr1_t compressed_page_t;
194 #define tag_compressed_page_justfound(page) \
195 tagptr_fold(compressed_page_t, page, 1)
197 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
199 void z_erofs_exit_zip_subsystem(void)
201 destroy_workqueue(z_erofs_workqueue);
202 z_erofs_destroy_pcluster_pool();
205 static inline int z_erofs_init_workqueue(void)
207 const unsigned int onlinecpus = num_possible_cpus();
210 * no need to spawn too many threads, limiting threads could minimum
211 * scheduling overhead, perhaps per-CPU threads should be better?
213 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
214 WQ_UNBOUND | WQ_HIGHPRI,
215 onlinecpus + onlinecpus / 4);
216 return z_erofs_workqueue ? 0 : -ENOMEM;
219 int __init z_erofs_init_zip_subsystem(void)
221 int err = z_erofs_create_pcluster_pool();
225 err = z_erofs_init_workqueue();
227 z_erofs_destroy_pcluster_pool();
231 enum z_erofs_pclustermode {
232 Z_EROFS_PCLUSTER_INFLIGHT,
234 * The current pclusters was the tail of an exist chain, in addition
235 * that the previous processed chained pclusters are all decided to
236 * be hooked up to it.
237 * A new chain will be created for the remaining pclusters which are
238 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
239 * the next pcluster cannot reuse the whole page safely for inplace I/O
240 * in the following scenario:
241 * ________________________________________________________________
242 * | tail (partial) page | head (partial) page |
243 * | (belongs to the next pcl) | (belongs to the current pcl) |
244 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
246 Z_EROFS_PCLUSTER_HOOKED,
248 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
249 * could be dispatched into bypass queue later due to uptodated managed
250 * pages. All related online pages cannot be reused for inplace I/O (or
251 * bvpage) since it can be directly decoded without I/O submission.
253 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
255 * The current collection has been linked with the owned chain, and
256 * could also be linked with the remaining collections, which means
257 * if the processing page is the tail page of the collection, thus
258 * the current collection can safely use the whole page (since
259 * the previous collection is under control) for in-place I/O, as
261 * ________________________________________________________________
262 * | tail (partial) page | head (partial) page |
263 * | (of the current cl) | (of the previous collection) |
264 * | PCLUSTER_FOLLOWED or | |
265 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
267 * [ (*) the above page can be used as inplace I/O. ]
269 Z_EROFS_PCLUSTER_FOLLOWED,
272 struct z_erofs_decompress_frontend {
273 struct inode *const inode;
274 struct erofs_map_blocks map;
275 struct z_erofs_bvec_iter biter;
277 struct page *candidate_bvpage;
278 struct z_erofs_pcluster *pcl, *tailpcl;
279 z_erofs_next_pcluster_t owned_head;
280 enum z_erofs_pclustermode mode;
283 /* used for applying cache strategy on the fly */
285 erofs_off_t headoffset;
287 /* a pointer used to pick up inplace I/O pages */
291 #define DECOMPRESS_FRONTEND_INIT(__i) { \
292 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
293 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
295 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
296 enum z_erofs_cache_alloctype type,
297 struct page **pagepool)
299 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
300 struct z_erofs_pcluster *pcl = fe->pcl;
301 bool standalone = true;
303 * optimistic allocation without direct reclaim since inplace I/O
304 * can be used if low memory otherwise.
306 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
307 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
310 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
313 for (i = 0; i < pcl->pclusterpages; ++i) {
316 struct page *newpage = NULL;
318 /* the compressed page was loaded before */
319 if (READ_ONCE(pcl->compressed_bvecs[i].page))
322 page = find_get_page(mc, pcl->obj.index + i);
325 t = tag_compressed_page_justfound(page);
327 /* I/O is needed, no possible to decompress directly */
331 newpage = erofs_allocpage(pagepool, gfp);
334 set_page_private(newpage,
335 Z_EROFS_PREALLOCATED_PAGE);
336 t = tag_compressed_page_justfound(newpage);
338 default: /* DONTALLOC */
343 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL,
350 erofs_pagepool_add(pagepool, newpage);
354 * don't do inplace I/O if all compressed pages are available in
355 * managed cache since it can be moved to the bypass queue instead.
358 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
361 /* called by erofs_shrinker to get rid of all compressed_pages */
362 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
363 struct erofs_workgroup *grp)
365 struct z_erofs_pcluster *const pcl =
366 container_of(grp, struct z_erofs_pcluster, obj);
369 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
371 * refcount of workgroup is now freezed as 1,
372 * therefore no need to worry about available decompression users.
374 for (i = 0; i < pcl->pclusterpages; ++i) {
375 struct page *page = pcl->compressed_bvecs[i].page;
380 /* block other users from reclaiming or migrating the page */
381 if (!trylock_page(page))
384 if (!erofs_page_is_managed(sbi, page))
387 /* barrier is implied in the following 'unlock_page' */
388 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
389 detach_page_private(page);
395 int erofs_try_to_free_cached_page(struct page *page)
397 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
400 if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
404 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
405 for (i = 0; i < pcl->pclusterpages; ++i) {
406 if (pcl->compressed_bvecs[i].page == page) {
407 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
412 erofs_workgroup_unfreeze(&pcl->obj, 1);
414 detach_page_private(page);
418 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
419 struct z_erofs_bvec *bvec)
421 struct z_erofs_pcluster *const pcl = fe->pcl;
423 while (fe->icur > 0) {
424 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
426 pcl->compressed_bvecs[fe->icur] = *bvec;
433 /* callers must be with pcluster lock held */
434 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
435 struct z_erofs_bvec *bvec, bool exclusive)
440 /* give priority for inplaceio to use file pages first */
441 if (z_erofs_try_inplace_io(fe, bvec))
443 /* otherwise, check if it can be used as a bvpage */
444 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
445 !fe->candidate_bvpage)
446 fe->candidate_bvpage = bvec->page;
448 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
449 fe->pcl->vcnt += (ret >= 0);
453 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
455 struct z_erofs_pcluster *pcl = f->pcl;
456 z_erofs_next_pcluster_t *owned_head = &f->owned_head;
458 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
459 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
460 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
461 *owned_head = &pcl->next;
462 /* so we can attach this pcluster to our submission chain. */
463 f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
468 * type 2, link to the end of an existing open chain, be careful
469 * that its submission is controlled by the original attached chain.
471 if (*owned_head != &pcl->next && pcl != f->tailpcl &&
472 cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
473 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
474 *owned_head = Z_EROFS_PCLUSTER_TAIL;
475 f->mode = Z_EROFS_PCLUSTER_HOOKED;
479 /* type 3, it belongs to a chain, but it isn't the end of the chain */
480 f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
483 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
485 struct erofs_map_blocks *map = &fe->map;
486 bool ztailpacking = map->m_flags & EROFS_MAP_META;
487 struct z_erofs_pcluster *pcl;
488 struct erofs_workgroup *grp;
491 if (!(map->m_flags & EROFS_MAP_ENCODED)) {
493 return -EFSCORRUPTED;
496 /* no available pcluster, let's allocate one */
497 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
498 map->m_plen >> PAGE_SHIFT);
502 atomic_set(&pcl->obj.refcount, 1);
503 pcl->algorithmformat = map->m_algorithmformat;
507 /* new pclusters should be claimed as type 1, primary and followed */
508 pcl->next = fe->owned_head;
509 pcl->pageofs_out = map->m_la & ~PAGE_MASK;
510 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
513 * lock all primary followed works before visible to others
514 * and mutex_trylock *never* fails for a new pcluster.
516 mutex_init(&pcl->lock);
517 DBG_BUGON(!mutex_trylock(&pcl->lock));
520 pcl->obj.index = 0; /* which indicates ztailpacking */
521 pcl->pageofs_in = erofs_blkoff(map->m_pa);
522 pcl->tailpacking_size = map->m_plen;
524 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
526 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
532 if (grp != &pcl->obj) {
533 fe->pcl = container_of(grp,
534 struct z_erofs_pcluster, obj);
539 /* used to check tail merging loop due to corrupted images */
540 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
542 fe->owned_head = &pcl->next;
547 mutex_unlock(&pcl->lock);
548 z_erofs_free_pcluster(pcl);
552 static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
554 struct erofs_map_blocks *map = &fe->map;
555 struct erofs_workgroup *grp = NULL;
560 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
561 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
562 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
564 if (!(map->m_flags & EROFS_MAP_META)) {
565 grp = erofs_find_workgroup(fe->inode->i_sb,
566 map->m_pa >> PAGE_SHIFT);
567 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
569 return -EFSCORRUPTED;
573 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
576 ret = z_erofs_register_pcluster(fe);
579 if (ret == -EEXIST) {
580 mutex_lock(&fe->pcl->lock);
581 /* used to check tail merging loop due to corrupted images */
582 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
583 fe->tailpcl = fe->pcl;
585 z_erofs_try_to_claim_pcluster(fe);
589 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
590 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
591 /* since file-backed online pages are traversed in reverse order */
592 fe->icur = z_erofs_pclusterpages(fe->pcl);
597 * keep in mind that no referenced pclusters will be freed
598 * only after a RCU grace period.
600 static void z_erofs_rcu_callback(struct rcu_head *head)
602 z_erofs_free_pcluster(container_of(head,
603 struct z_erofs_pcluster, rcu));
606 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
608 struct z_erofs_pcluster *const pcl =
609 container_of(grp, struct z_erofs_pcluster, obj);
611 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
614 static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
616 struct z_erofs_pcluster *pcl = fe->pcl;
621 z_erofs_bvec_iter_end(&fe->biter);
622 mutex_unlock(&pcl->lock);
624 if (fe->candidate_bvpage) {
625 DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
626 fe->candidate_bvpage = NULL;
630 * if all pending pages are added, don't hold its reference
631 * any longer if the pcluster isn't hosted by ourselves.
633 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
634 erofs_workgroup_put(&pcl->obj);
640 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
641 unsigned int cachestrategy,
644 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
650 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
654 static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
655 struct page *page, unsigned int pageofs,
658 struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
659 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
663 pos += EROFS_I(inode)->z_fragmentoff;
664 for (i = 0; i < len; i += cnt) {
665 cnt = min_t(unsigned int, len - i,
666 EROFS_BLKSIZ - erofs_blkoff(pos));
667 src = erofs_bread(&buf, packed_inode,
668 erofs_blknr(pos), EROFS_KMAP);
670 erofs_put_metabuf(&buf);
674 dst = kmap_local_page(page);
675 memcpy(dst + pageofs + i, src + erofs_blkoff(pos), cnt);
679 erofs_put_metabuf(&buf);
683 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
684 struct page *page, struct page **pagepool)
686 struct inode *const inode = fe->inode;
687 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
688 struct erofs_map_blocks *const map = &fe->map;
689 const loff_t offset = page_offset(page);
690 bool tight = true, exclusive;
692 enum z_erofs_cache_alloctype cache_strategy;
693 unsigned int cur, end, spiltted;
696 /* register locked file pages as online pages in pack */
697 z_erofs_onlinepage_init(page);
704 if (offset + cur < map->m_la ||
705 offset + cur >= map->m_la + map->m_llen) {
706 erofs_dbg("out-of-range map @ pos %llu", offset + cur);
708 if (z_erofs_collector_end(fe))
709 fe->backmost = false;
710 map->m_la = offset + cur;
712 err = z_erofs_map_blocks_iter(inode, map, 0);
718 /* didn't get a valid pcluster previously (very rare) */
721 if (!(map->m_flags & EROFS_MAP_MAPPED) ||
722 map->m_flags & EROFS_MAP_FRAGMENT)
725 err = z_erofs_collector_begin(fe);
729 if (z_erofs_is_inline_pcluster(fe->pcl)) {
732 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
733 erofs_blknr(map->m_pa), EROFS_NO_KMAP);
736 erofs_err(inode->i_sb,
737 "failed to get inline page, err %d", err);
740 get_page(fe->map.buf.page);
741 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
743 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
745 /* bind cache first when cached decompression is preferred */
746 if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy,
748 cache_strategy = TRYALLOC;
750 cache_strategy = DONTALLOC;
752 z_erofs_bind_cache(fe, cache_strategy, pagepool);
756 * Ensure the current partial page belongs to this submit chain rather
757 * than other concurrent submit chains or the noio(bypass) chain since
758 * those chains are handled asynchronously thus the page cannot be used
759 * for inplace I/O or bvpage (should be processed in a strict order.)
761 tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
762 fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
764 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
765 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
766 zero_user_segment(page, cur, end);
769 if (map->m_flags & EROFS_MAP_FRAGMENT) {
770 unsigned int pageofs, skip, len;
772 if (offset > map->m_la) {
774 skip = offset - map->m_la;
776 pageofs = map->m_la & ~PAGE_MASK;
779 len = min_t(unsigned int, map->m_llen - skip, end - cur);
780 err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
788 exclusive = (!cur && (!spiltted || tight));
790 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
793 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
795 .offset = offset - map->m_la,
798 /* should allocate an additional short-lived page for bvset */
799 if (err == -EAGAIN && !fe->candidate_bvpage) {
800 fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
801 set_page_private(fe->candidate_bvpage,
802 Z_EROFS_SHORTLIVED_PAGE);
807 DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
811 z_erofs_onlinepage_split(page);
812 /* bump up the number of spiltted parts of a page */
814 if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
815 fe->pcl->multibases = true;
816 if (fe->pcl->length < offset + end - map->m_la) {
817 fe->pcl->length = offset + end - map->m_la;
818 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
820 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
821 !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
822 fe->pcl->length == map->m_llen)
823 fe->pcl->partial = false;
825 /* shorten the remaining extent to update progress */
826 map->m_llen = offset + cur - map->m_la;
827 map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
835 z_erofs_page_mark_eio(page);
836 z_erofs_onlinepage_endio(page);
838 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
839 __func__, page, spiltted, map->m_llen);
843 static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
844 unsigned int readahead_pages)
846 /* auto: enable for read_folio, disable for readahead */
847 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
851 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
852 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
858 static bool z_erofs_page_is_invalidated(struct page *page)
860 return !page->mapping && !z_erofs_is_shortlived_page(page);
863 struct z_erofs_decompress_backend {
864 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
865 struct super_block *sb;
866 struct z_erofs_pcluster *pcl;
868 /* pages with the longest decompressed length for deduplication */
869 struct page **decompressed_pages;
870 /* pages to keep the compressed data */
871 struct page **compressed_pages;
873 struct list_head decompressed_secondary_bvecs;
874 struct page **pagepool;
875 unsigned int onstack_used, nr_pages;
878 struct z_erofs_bvec_item {
879 struct z_erofs_bvec bvec;
880 struct list_head list;
883 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
884 struct z_erofs_bvec *bvec)
886 struct z_erofs_bvec_item *item;
888 if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
891 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
892 DBG_BUGON(pgnr >= be->nr_pages);
893 if (!be->decompressed_pages[pgnr]) {
894 be->decompressed_pages[pgnr] = bvec->page;
899 /* (cold path) one pcluster is requested multiple times */
900 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
902 list_add(&item->list, &be->decompressed_secondary_bvecs);
905 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
908 unsigned int off0 = be->pcl->pageofs_out;
909 struct list_head *p, *n;
911 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
912 struct z_erofs_bvec_item *bvi;
913 unsigned int end, cur;
916 bvi = container_of(p, struct z_erofs_bvec_item, list);
917 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
918 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
920 dst = kmap_local_page(bvi->bvec.page);
922 unsigned int pgnr, scur, len;
924 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
925 DBG_BUGON(pgnr >= be->nr_pages);
927 scur = bvi->bvec.offset + cur -
928 ((pgnr << PAGE_SHIFT) - off0);
929 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
930 if (!be->decompressed_pages[pgnr]) {
935 src = kmap_local_page(be->decompressed_pages[pgnr]);
936 memcpy(dst + cur, src + scur, len);
942 z_erofs_page_mark_eio(bvi->bvec.page);
943 z_erofs_onlinepage_endio(bvi->bvec.page);
949 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
951 struct z_erofs_pcluster *pcl = be->pcl;
952 struct z_erofs_bvec_iter biter;
953 struct page *old_bvpage;
956 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
957 for (i = 0; i < pcl->vcnt; ++i) {
958 struct z_erofs_bvec bvec;
960 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
963 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
965 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
966 z_erofs_do_decompressed_bvec(be, &bvec);
969 old_bvpage = z_erofs_bvec_iter_end(&biter);
971 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
974 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
977 struct z_erofs_pcluster *pcl = be->pcl;
978 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
982 for (i = 0; i < pclusterpages; ++i) {
983 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
984 struct page *page = bvec->page;
986 /* compressed pages ought to be present before decompressing */
991 be->compressed_pages[i] = page;
993 if (z_erofs_is_inline_pcluster(pcl)) {
994 if (!PageUptodate(page))
999 DBG_BUGON(z_erofs_page_is_invalidated(page));
1000 if (!z_erofs_is_shortlived_page(page)) {
1001 if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
1002 if (!PageUptodate(page))
1006 z_erofs_do_decompressed_bvec(be, bvec);
1016 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
1019 struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1020 struct z_erofs_pcluster *pcl = be->pcl;
1021 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1022 unsigned int i, inputsize;
1027 mutex_lock(&pcl->lock);
1028 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1030 /* allocate (de)compressed page arrays if cannot be kept on stack */
1031 be->decompressed_pages = NULL;
1032 be->compressed_pages = NULL;
1033 be->onstack_used = 0;
1034 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1035 be->decompressed_pages = be->onstack_pages;
1036 be->onstack_used = be->nr_pages;
1037 memset(be->decompressed_pages, 0,
1038 sizeof(struct page *) * be->nr_pages);
1041 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1042 be->compressed_pages = be->onstack_pages + be->onstack_used;
1044 if (!be->decompressed_pages)
1045 be->decompressed_pages =
1046 kvcalloc(be->nr_pages, sizeof(struct page *),
1047 GFP_KERNEL | __GFP_NOFAIL);
1048 if (!be->compressed_pages)
1049 be->compressed_pages =
1050 kvcalloc(pclusterpages, sizeof(struct page *),
1051 GFP_KERNEL | __GFP_NOFAIL);
1053 z_erofs_parse_out_bvecs(be);
1054 err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1060 if (z_erofs_is_inline_pcluster(pcl))
1061 inputsize = pcl->tailpacking_size;
1063 inputsize = pclusterpages * PAGE_SIZE;
1065 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
1067 .in = be->compressed_pages,
1068 .out = be->decompressed_pages,
1069 .pageofs_in = pcl->pageofs_in,
1070 .pageofs_out = pcl->pageofs_out,
1071 .inputsize = inputsize,
1072 .outputsize = pcl->length,
1073 .alg = pcl->algorithmformat,
1074 .inplace_io = overlapped,
1075 .partial_decoding = pcl->partial,
1076 .fillgaps = pcl->multibases,
1080 /* must handle all compressed pages before actual file pages */
1081 if (z_erofs_is_inline_pcluster(pcl)) {
1082 page = pcl->compressed_bvecs[0].page;
1083 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1086 for (i = 0; i < pclusterpages; ++i) {
1087 page = pcl->compressed_bvecs[i].page;
1089 if (erofs_page_is_managed(sbi, page))
1092 /* recycle all individual short-lived pages */
1093 (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1094 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1097 if (be->compressed_pages < be->onstack_pages ||
1098 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1099 kvfree(be->compressed_pages);
1100 z_erofs_fill_other_copies(be, err);
1102 for (i = 0; i < be->nr_pages; ++i) {
1103 page = be->decompressed_pages[i];
1107 DBG_BUGON(z_erofs_page_is_invalidated(page));
1109 /* recycle all individual short-lived pages */
1110 if (z_erofs_put_shortlivedpage(be->pagepool, page))
1113 z_erofs_page_mark_eio(page);
1114 z_erofs_onlinepage_endio(page);
1117 if (be->decompressed_pages != be->onstack_pages)
1118 kvfree(be->decompressed_pages);
1121 pcl->partial = true;
1122 pcl->multibases = false;
1123 pcl->bvset.nextpage = NULL;
1126 /* pcluster lock MUST be taken before the following line */
1127 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1128 mutex_unlock(&pcl->lock);
1132 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1133 struct page **pagepool)
1135 struct z_erofs_decompress_backend be = {
1137 .pagepool = pagepool,
1138 .decompressed_secondary_bvecs =
1139 LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1141 z_erofs_next_pcluster_t owned = io->head;
1143 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1144 /* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1145 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
1146 /* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */
1147 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1149 be.pcl = container_of(owned, struct z_erofs_pcluster, next);
1150 owned = READ_ONCE(be.pcl->next);
1152 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
1153 erofs_workgroup_put(&be.pcl->obj);
1157 static void z_erofs_decompressqueue_work(struct work_struct *work)
1159 struct z_erofs_decompressqueue *bgq =
1160 container_of(work, struct z_erofs_decompressqueue, u.work);
1161 struct page *pagepool = NULL;
1163 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1164 z_erofs_decompress_queue(bgq, &pagepool);
1166 erofs_release_pages(&pagepool);
1170 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1171 bool sync, int bios)
1173 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1175 /* wake up the caller thread for sync decompression */
1177 if (!atomic_add_return(bios, &io->pending_bios))
1178 complete(&io->u.done);
1182 if (atomic_add_return(bios, &io->pending_bios))
1184 /* Use workqueue and sync decompression for atomic contexts only */
1185 if (in_atomic() || irqs_disabled()) {
1186 queue_work(z_erofs_workqueue, &io->u.work);
1187 /* enable sync decompression for readahead */
1188 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1189 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1192 z_erofs_decompressqueue_work(&io->u.work);
1195 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1197 struct page **pagepool,
1198 struct address_space *mc)
1200 const pgoff_t index = pcl->obj.index;
1201 gfp_t gfp = mapping_gfp_mask(mc);
1202 bool tocache = false;
1204 struct address_space *mapping;
1205 struct page *oldpage, *page;
1207 compressed_page_t t;
1211 page = READ_ONCE(pcl->compressed_bvecs[nr].page);
1217 /* process the target tagged pointer */
1218 t = tagptr_init(compressed_page_t, page);
1219 justfound = tagptr_unfold_tags(t);
1220 page = tagptr_unfold_ptr(t);
1223 * preallocated cached pages, which is used to avoid direct reclaim
1224 * otherwise, it will go inplace I/O path instead.
1226 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1227 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1228 set_page_private(page, 0);
1232 mapping = READ_ONCE(page->mapping);
1235 * file-backed online pages in plcuster are all locked steady,
1236 * therefore it is impossible for `mapping' to be NULL.
1238 if (mapping && mapping != mc)
1239 /* ought to be unmanaged pages */
1242 /* directly return for shortlived page as well */
1243 if (z_erofs_is_shortlived_page(page))
1248 /* only true if page reclaim goes wrong, should never happen */
1249 DBG_BUGON(justfound && PagePrivate(page));
1251 /* the page is still in manage cache */
1252 if (page->mapping == mc) {
1253 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1255 if (!PagePrivate(page)) {
1257 * impossible to be !PagePrivate(page) for
1258 * the current restriction as well if
1259 * the page is already in compressed_bvecs[].
1261 DBG_BUGON(!justfound);
1264 set_page_private(page, (unsigned long)pcl);
1265 SetPagePrivate(page);
1268 /* no need to submit io if it is already up-to-date */
1269 if (PageUptodate(page)) {
1277 * the managed page has been truncated, it's unsafe to
1278 * reuse this one, let's allocate a new cache-managed page.
1280 DBG_BUGON(page->mapping);
1281 DBG_BUGON(!justfound);
1287 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1288 if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1290 erofs_pagepool_add(pagepool, page);
1295 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1296 /* turn into temporary page if fails (1 ref) */
1297 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1300 attach_page_private(page, pcl);
1301 /* drop a refcount added by allocpage (then we have 2 refs here) */
1304 out: /* the only exit (for tracing and debugging) */
1308 static struct z_erofs_decompressqueue *
1309 jobqueue_init(struct super_block *sb,
1310 struct z_erofs_decompressqueue *fgq, bool *fg)
1312 struct z_erofs_decompressqueue *q;
1315 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1320 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1324 init_completion(&fgq->u.done);
1325 atomic_set(&fgq->pending_bios, 0);
1329 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1333 /* define decompression jobqueue types */
1340 static void *jobqueueset_init(struct super_block *sb,
1341 struct z_erofs_decompressqueue *q[],
1342 struct z_erofs_decompressqueue *fgq, bool *fg)
1345 * if managed cache is enabled, bypass jobqueue is needed,
1346 * no need to read from device for all pclusters in this queue.
1348 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1349 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1351 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1354 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1355 z_erofs_next_pcluster_t qtail[],
1356 z_erofs_next_pcluster_t owned_head)
1358 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1359 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1361 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1362 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1363 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1365 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1367 WRITE_ONCE(*submit_qtail, owned_head);
1368 WRITE_ONCE(*bypass_qtail, &pcl->next);
1370 qtail[JQ_BYPASS] = &pcl->next;
1373 static void z_erofs_decompressqueue_endio(struct bio *bio)
1375 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
1376 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
1377 blk_status_t err = bio->bi_status;
1378 struct bio_vec *bvec;
1379 struct bvec_iter_all iter_all;
1381 bio_for_each_segment_all(bvec, bio, iter_all) {
1382 struct page *page = bvec->bv_page;
1384 DBG_BUGON(PageUptodate(page));
1385 DBG_BUGON(z_erofs_page_is_invalidated(page));
1387 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1389 SetPageUptodate(page);
1395 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
1399 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1400 struct page **pagepool,
1401 struct z_erofs_decompressqueue *fgq,
1404 struct super_block *sb = f->inode->i_sb;
1405 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1406 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1407 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1409 z_erofs_next_pcluster_t owned_head = f->owned_head;
1410 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1412 struct block_device *last_bdev;
1413 unsigned int nr_bios = 0;
1414 struct bio *bio = NULL;
1415 /* initialize to 1 to make skip psi_memstall_leave unless needed */
1416 unsigned long pflags = 1;
1418 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1419 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1420 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1422 /* by default, all need io submission */
1423 q[JQ_SUBMIT]->head = owned_head;
1426 struct erofs_map_dev mdev;
1427 struct z_erofs_pcluster *pcl;
1432 /* no possible 'owned_head' equals the following */
1433 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1434 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1436 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1438 /* close the main owned chain at first */
1439 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1440 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1441 if (z_erofs_is_inline_pcluster(pcl)) {
1442 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1446 /* no device id here, thus it will always succeed */
1447 mdev = (struct erofs_map_dev) {
1448 .m_pa = blknr_to_addr(pcl->obj.index),
1450 (void)erofs_map_dev(sb, &mdev);
1452 cur = erofs_blknr(mdev.m_pa);
1453 end = cur + pcl->pclusterpages;
1458 page = pickup_page_for_submission(pcl, i++, pagepool,
1463 if (bio && (cur != last_index + 1 ||
1464 last_bdev != mdev.m_bdev)) {
1467 psi_memstall_leave(&pflags);
1472 if (unlikely(PageWorkingset(page)))
1473 psi_memstall_enter(&pflags);
1476 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1477 REQ_OP_READ, GFP_NOIO);
1478 bio->bi_end_io = z_erofs_decompressqueue_endio;
1480 last_bdev = mdev.m_bdev;
1481 bio->bi_iter.bi_sector = (sector_t)cur <<
1482 LOG_SECTORS_PER_BLOCK;
1483 bio->bi_private = bi_private;
1485 bio->bi_opf |= REQ_RAHEAD;
1489 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1490 goto submit_bio_retry;
1494 } while (++cur < end);
1497 qtail[JQ_SUBMIT] = &pcl->next;
1499 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1500 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1504 psi_memstall_leave(&pflags);
1509 * although background is preferred, no one is pending for submission.
1510 * don't issue workqueue for decompression but drop it directly instead.
1512 if (!*force_fg && !nr_bios) {
1513 kvfree(q[JQ_SUBMIT]);
1516 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1519 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1520 struct page **pagepool, bool force_fg)
1522 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1524 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1526 z_erofs_submit_queue(f, pagepool, io, &force_fg);
1528 /* handle bypass queue (no i/o pclusters) immediately */
1529 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1534 /* wait until all bios are completed */
1535 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1537 /* handle synchronous decompress queue in the caller context */
1538 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1542 * Since partial uptodate is still unimplemented for now, we have to use
1543 * approximate readmore strategies as a start.
1545 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1546 struct readahead_control *rac,
1548 struct page **pagepool,
1551 struct inode *inode = f->inode;
1552 struct erofs_map_blocks *map = &f->map;
1558 err = z_erofs_map_blocks_iter(inode, map,
1559 EROFS_GET_BLOCKS_READMORE);
1563 /* expend ra for the trailing edge if readahead */
1565 loff_t newstart = readahead_pos(rac);
1567 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1568 readahead_expand(rac, newstart, cur - newstart);
1571 end = round_up(end, PAGE_SIZE);
1573 end = round_up(map->m_la, PAGE_SIZE);
1579 cur = map->m_la + map->m_llen - 1;
1580 while (cur >= end) {
1581 pgoff_t index = cur >> PAGE_SHIFT;
1584 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1586 if (PageUptodate(page)) {
1589 err = z_erofs_do_read_page(f, page, pagepool);
1591 erofs_err(inode->i_sb,
1592 "readmore error at page %lu @ nid %llu",
1593 index, EROFS_I(inode)->nid);
1598 if (cur < PAGE_SIZE)
1600 cur = (index << PAGE_SHIFT) - 1;
1604 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1606 struct page *page = &folio->page;
1607 struct inode *const inode = page->mapping->host;
1608 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1609 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1610 struct page *pagepool = NULL;
1613 trace_erofs_readpage(page, false);
1614 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1616 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
1618 err = z_erofs_do_read_page(&f, page, &pagepool);
1619 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
1621 (void)z_erofs_collector_end(&f);
1623 /* if some compressed cluster ready, need submit them anyway */
1624 z_erofs_runqueue(&f, &pagepool,
1625 z_erofs_get_sync_decompress_policy(sbi, 0));
1628 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1630 erofs_put_metabuf(&f.map.buf);
1631 erofs_release_pages(&pagepool);
1635 static void z_erofs_readahead(struct readahead_control *rac)
1637 struct inode *const inode = rac->mapping->host;
1638 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1639 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1640 struct page *pagepool = NULL, *head = NULL, *page;
1641 unsigned int nr_pages;
1644 f.headoffset = readahead_pos(rac);
1646 z_erofs_pcluster_readmore(&f, rac, f.headoffset +
1647 readahead_length(rac) - 1, &pagepool, true);
1648 nr_pages = readahead_count(rac);
1649 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1651 while ((page = readahead_page(rac))) {
1652 set_page_private(page, (unsigned long)head);
1657 struct page *page = head;
1660 /* traversal in reverse order */
1661 head = (void *)page_private(page);
1663 err = z_erofs_do_read_page(&f, page, &pagepool);
1665 erofs_err(inode->i_sb,
1666 "readahead error at page %lu @ nid %llu",
1667 page->index, EROFS_I(inode)->nid);
1670 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
1671 (void)z_erofs_collector_end(&f);
1673 z_erofs_runqueue(&f, &pagepool,
1674 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
1675 erofs_put_metabuf(&f.map.buf);
1676 erofs_release_pages(&pagepool);
1679 const struct address_space_operations z_erofs_aops = {
1680 .read_folio = z_erofs_read_folio,
1681 .readahead = z_erofs_readahead,