305426a717925cc74db1af332a1864f92ff0610d
[linux-2.6-block.git] / fs / erofs / zdata.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Copyright (C) 2022 Alibaba Cloud
6  */
7 #include "compress.h"
8 #include <linux/psi.h>
9 #include <linux/cpuhotplug.h>
10 #include <trace/events/erofs.h>
11
12 #define Z_EROFS_PCLUSTER_MAX_PAGES      (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
13 #define Z_EROFS_INLINE_BVECS            2
14
15 /*
16  * let's leave a type here in case of introducing
17  * another tagged pointer later.
18  */
19 typedef void *z_erofs_next_pcluster_t;
20
21 struct z_erofs_bvec {
22         struct page *page;
23         int offset;
24         unsigned int end;
25 };
26
27 #define __Z_EROFS_BVSET(name, total) \
28 struct name { \
29         /* point to the next page which contains the following bvecs */ \
30         struct page *nextpage; \
31         struct z_erofs_bvec bvec[total]; \
32 }
33 __Z_EROFS_BVSET(z_erofs_bvset,);
34 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
35
36 /*
37  * Structure fields follow one of the following exclusion rules.
38  *
39  * I: Modifiable by initialization/destruction paths and read-only
40  *    for everyone else;
41  *
42  * L: Field should be protected by the pcluster lock;
43  *
44  * A: Field should be accessed / updated in atomic for parallelized code.
45  */
46 struct z_erofs_pcluster {
47         struct erofs_workgroup obj;
48         struct mutex lock;
49
50         /* A: point to next chained pcluster or TAILs */
51         z_erofs_next_pcluster_t next;
52
53         /* L: the maximum decompression size of this round */
54         unsigned int length;
55
56         /* L: total number of bvecs */
57         unsigned int vcnt;
58
59         /* I: page offset of start position of decompression */
60         unsigned short pageofs_out;
61
62         /* I: page offset of inline compressed data */
63         unsigned short pageofs_in;
64
65         union {
66                 /* L: inline a certain number of bvec for bootstrap */
67                 struct z_erofs_bvset_inline bvset;
68
69                 /* I: can be used to free the pcluster by RCU. */
70                 struct rcu_head rcu;
71         };
72
73         union {
74                 /* I: physical cluster size in pages */
75                 unsigned short pclusterpages;
76
77                 /* I: tailpacking inline compressed size */
78                 unsigned short tailpacking_size;
79         };
80
81         /* I: compression algorithm format */
82         unsigned char algorithmformat;
83
84         /* L: whether partial decompression or not */
85         bool partial;
86
87         /* L: indicate several pageofs_outs or not */
88         bool multibases;
89
90         /* A: compressed bvecs (can be cached or inplaced pages) */
91         struct z_erofs_bvec compressed_bvecs[];
92 };
93
94 /* let's avoid the valid 32-bit kernel addresses */
95
96 /* the chained workgroup has't submitted io (still open) */
97 #define Z_EROFS_PCLUSTER_TAIL           ((void *)0x5F0ECAFE)
98 /* the chained workgroup has already submitted io */
99 #define Z_EROFS_PCLUSTER_TAIL_CLOSED    ((void *)0x5F0EDEAD)
100
101 #define Z_EROFS_PCLUSTER_NIL            (NULL)
102
103 struct z_erofs_decompressqueue {
104         struct super_block *sb;
105         atomic_t pending_bios;
106         z_erofs_next_pcluster_t head;
107
108         union {
109                 struct completion done;
110                 struct work_struct work;
111                 struct kthread_work kthread_work;
112         } u;
113         bool eio, sync;
114 };
115
116 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
117 {
118         return !pcl->obj.index;
119 }
120
121 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
122 {
123         if (z_erofs_is_inline_pcluster(pcl))
124                 return 1;
125         return pcl->pclusterpages;
126 }
127
128 /*
129  * bit 30: I/O error occurred on this page
130  * bit 0 - 29: remaining parts to complete this page
131  */
132 #define Z_EROFS_PAGE_EIO                        (1 << 30)
133
134 static inline void z_erofs_onlinepage_init(struct page *page)
135 {
136         union {
137                 atomic_t o;
138                 unsigned long v;
139         } u = { .o = ATOMIC_INIT(1) };
140
141         set_page_private(page, u.v);
142         smp_wmb();
143         SetPagePrivate(page);
144 }
145
146 static inline void z_erofs_onlinepage_split(struct page *page)
147 {
148         atomic_inc((atomic_t *)&page->private);
149 }
150
151 static inline void z_erofs_page_mark_eio(struct page *page)
152 {
153         int orig;
154
155         do {
156                 orig = atomic_read((atomic_t *)&page->private);
157         } while (atomic_cmpxchg((atomic_t *)&page->private, orig,
158                                 orig | Z_EROFS_PAGE_EIO) != orig);
159 }
160
161 static inline void z_erofs_onlinepage_endio(struct page *page)
162 {
163         unsigned int v;
164
165         DBG_BUGON(!PagePrivate(page));
166         v = atomic_dec_return((atomic_t *)&page->private);
167         if (!(v & ~Z_EROFS_PAGE_EIO)) {
168                 set_page_private(page, 0);
169                 ClearPagePrivate(page);
170                 if (!(v & Z_EROFS_PAGE_EIO))
171                         SetPageUptodate(page);
172                 unlock_page(page);
173         }
174 }
175
176 #define Z_EROFS_ONSTACK_PAGES           32
177
178 /*
179  * since pclustersize is variable for big pcluster feature, introduce slab
180  * pools implementation for different pcluster sizes.
181  */
182 struct z_erofs_pcluster_slab {
183         struct kmem_cache *slab;
184         unsigned int maxpages;
185         char name[48];
186 };
187
188 #define _PCLP(n) { .maxpages = n }
189
190 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
191         _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
192         _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
193 };
194
195 struct z_erofs_bvec_iter {
196         struct page *bvpage;
197         struct z_erofs_bvset *bvset;
198         unsigned int nr, cur;
199 };
200
201 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
202 {
203         if (iter->bvpage)
204                 kunmap_local(iter->bvset);
205         return iter->bvpage;
206 }
207
208 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
209 {
210         unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
211         /* have to access nextpage in advance, otherwise it will be unmapped */
212         struct page *nextpage = iter->bvset->nextpage;
213         struct page *oldpage;
214
215         DBG_BUGON(!nextpage);
216         oldpage = z_erofs_bvec_iter_end(iter);
217         iter->bvpage = nextpage;
218         iter->bvset = kmap_local_page(nextpage);
219         iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
220         iter->cur = 0;
221         return oldpage;
222 }
223
224 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
225                                     struct z_erofs_bvset_inline *bvset,
226                                     unsigned int bootstrap_nr,
227                                     unsigned int cur)
228 {
229         *iter = (struct z_erofs_bvec_iter) {
230                 .nr = bootstrap_nr,
231                 .bvset = (struct z_erofs_bvset *)bvset,
232         };
233
234         while (cur > iter->nr) {
235                 cur -= iter->nr;
236                 z_erofs_bvset_flip(iter);
237         }
238         iter->cur = cur;
239 }
240
241 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
242                                 struct z_erofs_bvec *bvec,
243                                 struct page **candidate_bvpage)
244 {
245         if (iter->cur == iter->nr) {
246                 if (!*candidate_bvpage)
247                         return -EAGAIN;
248
249                 DBG_BUGON(iter->bvset->nextpage);
250                 iter->bvset->nextpage = *candidate_bvpage;
251                 z_erofs_bvset_flip(iter);
252
253                 iter->bvset->nextpage = NULL;
254                 *candidate_bvpage = NULL;
255         }
256         iter->bvset->bvec[iter->cur++] = *bvec;
257         return 0;
258 }
259
260 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
261                                  struct z_erofs_bvec *bvec,
262                                  struct page **old_bvpage)
263 {
264         if (iter->cur == iter->nr)
265                 *old_bvpage = z_erofs_bvset_flip(iter);
266         else
267                 *old_bvpage = NULL;
268         *bvec = iter->bvset->bvec[iter->cur++];
269 }
270
271 static void z_erofs_destroy_pcluster_pool(void)
272 {
273         int i;
274
275         for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
276                 if (!pcluster_pool[i].slab)
277                         continue;
278                 kmem_cache_destroy(pcluster_pool[i].slab);
279                 pcluster_pool[i].slab = NULL;
280         }
281 }
282
283 static int z_erofs_create_pcluster_pool(void)
284 {
285         struct z_erofs_pcluster_slab *pcs;
286         struct z_erofs_pcluster *a;
287         unsigned int size;
288
289         for (pcs = pcluster_pool;
290              pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
291                 size = struct_size(a, compressed_bvecs, pcs->maxpages);
292
293                 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
294                 pcs->slab = kmem_cache_create(pcs->name, size, 0,
295                                               SLAB_RECLAIM_ACCOUNT, NULL);
296                 if (pcs->slab)
297                         continue;
298
299                 z_erofs_destroy_pcluster_pool();
300                 return -ENOMEM;
301         }
302         return 0;
303 }
304
305 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
306 {
307         int i;
308
309         for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
310                 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
311                 struct z_erofs_pcluster *pcl;
312
313                 if (nrpages > pcs->maxpages)
314                         continue;
315
316                 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
317                 if (!pcl)
318                         return ERR_PTR(-ENOMEM);
319                 pcl->pclusterpages = nrpages;
320                 return pcl;
321         }
322         return ERR_PTR(-EINVAL);
323 }
324
325 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
326 {
327         unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
328         int i;
329
330         for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
331                 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
332
333                 if (pclusterpages > pcs->maxpages)
334                         continue;
335
336                 kmem_cache_free(pcs->slab, pcl);
337                 return;
338         }
339         DBG_BUGON(1);
340 }
341
342 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
343
344 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
345 static struct kthread_worker __rcu **z_erofs_pcpu_workers;
346
347 static void erofs_destroy_percpu_workers(void)
348 {
349         struct kthread_worker *worker;
350         unsigned int cpu;
351
352         for_each_possible_cpu(cpu) {
353                 worker = rcu_dereference_protected(
354                                         z_erofs_pcpu_workers[cpu], 1);
355                 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
356                 if (worker)
357                         kthread_destroy_worker(worker);
358         }
359         kfree(z_erofs_pcpu_workers);
360 }
361
362 static struct kthread_worker *erofs_init_percpu_worker(int cpu)
363 {
364         struct kthread_worker *worker =
365                 kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
366
367         if (IS_ERR(worker))
368                 return worker;
369         if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
370                 sched_set_fifo_low(worker->task);
371         return worker;
372 }
373
374 static int erofs_init_percpu_workers(void)
375 {
376         struct kthread_worker *worker;
377         unsigned int cpu;
378
379         z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
380                         sizeof(struct kthread_worker *), GFP_ATOMIC);
381         if (!z_erofs_pcpu_workers)
382                 return -ENOMEM;
383
384         for_each_online_cpu(cpu) {      /* could miss cpu{off,on}line? */
385                 worker = erofs_init_percpu_worker(cpu);
386                 if (!IS_ERR(worker))
387                         rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
388         }
389         return 0;
390 }
391 #else
392 static inline void erofs_destroy_percpu_workers(void) {}
393 static inline int erofs_init_percpu_workers(void) { return 0; }
394 #endif
395
396 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
397 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
398 static enum cpuhp_state erofs_cpuhp_state;
399
400 static int erofs_cpu_online(unsigned int cpu)
401 {
402         struct kthread_worker *worker, *old;
403
404         worker = erofs_init_percpu_worker(cpu);
405         if (IS_ERR(worker))
406                 return PTR_ERR(worker);
407
408         spin_lock(&z_erofs_pcpu_worker_lock);
409         old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
410                         lockdep_is_held(&z_erofs_pcpu_worker_lock));
411         if (!old)
412                 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
413         spin_unlock(&z_erofs_pcpu_worker_lock);
414         if (old)
415                 kthread_destroy_worker(worker);
416         return 0;
417 }
418
419 static int erofs_cpu_offline(unsigned int cpu)
420 {
421         struct kthread_worker *worker;
422
423         spin_lock(&z_erofs_pcpu_worker_lock);
424         worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
425                         lockdep_is_held(&z_erofs_pcpu_worker_lock));
426         rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
427         spin_unlock(&z_erofs_pcpu_worker_lock);
428
429         synchronize_rcu();
430         if (worker)
431                 kthread_destroy_worker(worker);
432         return 0;
433 }
434
435 static int erofs_cpu_hotplug_init(void)
436 {
437         int state;
438
439         state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
440                         "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
441         if (state < 0)
442                 return state;
443
444         erofs_cpuhp_state = state;
445         return 0;
446 }
447
448 static void erofs_cpu_hotplug_destroy(void)
449 {
450         if (erofs_cpuhp_state)
451                 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
452 }
453 #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
454 static inline int erofs_cpu_hotplug_init(void) { return 0; }
455 static inline void erofs_cpu_hotplug_destroy(void) {}
456 #endif
457
458 void z_erofs_exit_zip_subsystem(void)
459 {
460         erofs_cpu_hotplug_destroy();
461         erofs_destroy_percpu_workers();
462         destroy_workqueue(z_erofs_workqueue);
463         z_erofs_destroy_pcluster_pool();
464 }
465
466 int __init z_erofs_init_zip_subsystem(void)
467 {
468         int err = z_erofs_create_pcluster_pool();
469
470         if (err)
471                 goto out_error_pcluster_pool;
472
473         z_erofs_workqueue = alloc_workqueue("erofs_worker",
474                         WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
475         if (!z_erofs_workqueue) {
476                 err = -ENOMEM;
477                 goto out_error_workqueue_init;
478         }
479
480         err = erofs_init_percpu_workers();
481         if (err)
482                 goto out_error_pcpu_worker;
483
484         err = erofs_cpu_hotplug_init();
485         if (err < 0)
486                 goto out_error_cpuhp_init;
487         return err;
488
489 out_error_cpuhp_init:
490         erofs_destroy_percpu_workers();
491 out_error_pcpu_worker:
492         destroy_workqueue(z_erofs_workqueue);
493 out_error_workqueue_init:
494         z_erofs_destroy_pcluster_pool();
495 out_error_pcluster_pool:
496         return err;
497 }
498
499 enum z_erofs_pclustermode {
500         Z_EROFS_PCLUSTER_INFLIGHT,
501         /*
502          * The current pclusters was the tail of an exist chain, in addition
503          * that the previous processed chained pclusters are all decided to
504          * be hooked up to it.
505          * A new chain will be created for the remaining pclusters which are
506          * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
507          * the next pcluster cannot reuse the whole page safely for inplace I/O
508          * in the following scenario:
509          *  ________________________________________________________________
510          * |      tail (partial) page     |       head (partial) page       |
511          * |   (belongs to the next pcl)  |   (belongs to the current pcl)  |
512          * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
513          */
514         Z_EROFS_PCLUSTER_HOOKED,
515         /*
516          * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
517          * could be dispatched into bypass queue later due to uptodated managed
518          * pages. All related online pages cannot be reused for inplace I/O (or
519          * bvpage) since it can be directly decoded without I/O submission.
520          */
521         Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
522         /*
523          * The current collection has been linked with the owned chain, and
524          * could also be linked with the remaining collections, which means
525          * if the processing page is the tail page of the collection, thus
526          * the current collection can safely use the whole page (since
527          * the previous collection is under control) for in-place I/O, as
528          * illustrated below:
529          *  ________________________________________________________________
530          * |  tail (partial) page |          head (partial) page           |
531          * |  (of the current cl) |      (of the previous collection)      |
532          * | PCLUSTER_FOLLOWED or |                                        |
533          * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
534          *
535          * [  (*) the above page can be used as inplace I/O.               ]
536          */
537         Z_EROFS_PCLUSTER_FOLLOWED,
538 };
539
540 struct z_erofs_decompress_frontend {
541         struct inode *const inode;
542         struct erofs_map_blocks map;
543         struct z_erofs_bvec_iter biter;
544
545         struct page *candidate_bvpage;
546         struct z_erofs_pcluster *pcl, *tailpcl;
547         z_erofs_next_pcluster_t owned_head;
548         enum z_erofs_pclustermode mode;
549
550         /* used for applying cache strategy on the fly */
551         bool backmost;
552         erofs_off_t headoffset;
553
554         /* a pointer used to pick up inplace I/O pages */
555         unsigned int icur;
556 };
557
558 #define DECOMPRESS_FRONTEND_INIT(__i) { \
559         .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
560         .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
561
562 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
563 {
564         unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
565
566         if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
567                 return false;
568
569         if (fe->backmost)
570                 return true;
571
572         if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
573             fe->map.m_la < fe->headoffset)
574                 return true;
575
576         return false;
577 }
578
579 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
580                                struct page **pagepool)
581 {
582         struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
583         struct z_erofs_pcluster *pcl = fe->pcl;
584         bool shouldalloc = z_erofs_should_alloc_cache(fe);
585         bool standalone = true;
586         /*
587          * optimistic allocation without direct reclaim since inplace I/O
588          * can be used if low memory otherwise.
589          */
590         gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
591                         __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
592         unsigned int i;
593
594         if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
595                 return;
596
597         for (i = 0; i < pcl->pclusterpages; ++i) {
598                 struct page *page;
599                 void *t;        /* mark pages just found for debugging */
600                 struct page *newpage = NULL;
601
602                 /* the compressed page was loaded before */
603                 if (READ_ONCE(pcl->compressed_bvecs[i].page))
604                         continue;
605
606                 page = find_get_page(mc, pcl->obj.index + i);
607
608                 if (page) {
609                         t = (void *)((unsigned long)page | 1);
610                 } else {
611                         /* I/O is needed, no possible to decompress directly */
612                         standalone = false;
613                         if (!shouldalloc)
614                                 continue;
615
616                         /*
617                          * try to use cached I/O if page allocation
618                          * succeeds or fallback to in-place I/O instead
619                          * to avoid any direct reclaim.
620                          */
621                         newpage = erofs_allocpage(pagepool, gfp);
622                         if (!newpage)
623                                 continue;
624                         set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
625                         t = (void *)((unsigned long)newpage | 1);
626                 }
627
628                 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
629                         continue;
630
631                 if (page)
632                         put_page(page);
633                 else if (newpage)
634                         erofs_pagepool_add(pagepool, newpage);
635         }
636
637         /*
638          * don't do inplace I/O if all compressed pages are available in
639          * managed cache since it can be moved to the bypass queue instead.
640          */
641         if (standalone)
642                 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
643 }
644
645 /* called by erofs_shrinker to get rid of all compressed_pages */
646 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
647                                        struct erofs_workgroup *grp)
648 {
649         struct z_erofs_pcluster *const pcl =
650                 container_of(grp, struct z_erofs_pcluster, obj);
651         int i;
652
653         DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
654         /*
655          * refcount of workgroup is now freezed as 1,
656          * therefore no need to worry about available decompression users.
657          */
658         for (i = 0; i < pcl->pclusterpages; ++i) {
659                 struct page *page = pcl->compressed_bvecs[i].page;
660
661                 if (!page)
662                         continue;
663
664                 /* block other users from reclaiming or migrating the page */
665                 if (!trylock_page(page))
666                         return -EBUSY;
667
668                 if (!erofs_page_is_managed(sbi, page))
669                         continue;
670
671                 /* barrier is implied in the following 'unlock_page' */
672                 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
673                 detach_page_private(page);
674                 unlock_page(page);
675         }
676         return 0;
677 }
678
679 int erofs_try_to_free_cached_page(struct page *page)
680 {
681         struct z_erofs_pcluster *const pcl = (void *)page_private(page);
682         int ret, i;
683
684         if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
685                 return 0;
686
687         ret = 0;
688         DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
689         for (i = 0; i < pcl->pclusterpages; ++i) {
690                 if (pcl->compressed_bvecs[i].page == page) {
691                         WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
692                         ret = 1;
693                         break;
694                 }
695         }
696         erofs_workgroup_unfreeze(&pcl->obj, 1);
697         if (ret)
698                 detach_page_private(page);
699         return ret;
700 }
701
702 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
703                                    struct z_erofs_bvec *bvec)
704 {
705         struct z_erofs_pcluster *const pcl = fe->pcl;
706
707         while (fe->icur > 0) {
708                 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
709                              NULL, bvec->page)) {
710                         pcl->compressed_bvecs[fe->icur] = *bvec;
711                         return true;
712                 }
713         }
714         return false;
715 }
716
717 /* callers must be with pcluster lock held */
718 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
719                                struct z_erofs_bvec *bvec, bool exclusive)
720 {
721         int ret;
722
723         if (exclusive) {
724                 /* give priority for inplaceio to use file pages first */
725                 if (z_erofs_try_inplace_io(fe, bvec))
726                         return 0;
727                 /* otherwise, check if it can be used as a bvpage */
728                 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
729                     !fe->candidate_bvpage)
730                         fe->candidate_bvpage = bvec->page;
731         }
732         ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
733         fe->pcl->vcnt += (ret >= 0);
734         return ret;
735 }
736
737 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
738 {
739         struct z_erofs_pcluster *pcl = f->pcl;
740         z_erofs_next_pcluster_t *owned_head = &f->owned_head;
741
742         /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
743         if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
744                     *owned_head) == Z_EROFS_PCLUSTER_NIL) {
745                 *owned_head = &pcl->next;
746                 /* so we can attach this pcluster to our submission chain. */
747                 f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
748                 return;
749         }
750
751         /*
752          * type 2, link to the end of an existing open chain, be careful
753          * that its submission is controlled by the original attached chain.
754          */
755         if (*owned_head != &pcl->next && pcl != f->tailpcl &&
756             cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
757                     *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
758                 *owned_head = Z_EROFS_PCLUSTER_TAIL;
759                 f->mode = Z_EROFS_PCLUSTER_HOOKED;
760                 f->tailpcl = NULL;
761                 return;
762         }
763         /* type 3, it belongs to a chain, but it isn't the end of the chain */
764         f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
765 }
766
767 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
768 {
769         struct erofs_map_blocks *map = &fe->map;
770         bool ztailpacking = map->m_flags & EROFS_MAP_META;
771         struct z_erofs_pcluster *pcl;
772         struct erofs_workgroup *grp;
773         int err;
774
775         if (!(map->m_flags & EROFS_MAP_ENCODED) ||
776             (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
777                 DBG_BUGON(1);
778                 return -EFSCORRUPTED;
779         }
780
781         /* no available pcluster, let's allocate one */
782         pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
783                                      map->m_plen >> PAGE_SHIFT);
784         if (IS_ERR(pcl))
785                 return PTR_ERR(pcl);
786
787         atomic_set(&pcl->obj.refcount, 1);
788         pcl->algorithmformat = map->m_algorithmformat;
789         pcl->length = 0;
790         pcl->partial = true;
791
792         /* new pclusters should be claimed as type 1, primary and followed */
793         pcl->next = fe->owned_head;
794         pcl->pageofs_out = map->m_la & ~PAGE_MASK;
795         fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
796
797         /*
798          * lock all primary followed works before visible to others
799          * and mutex_trylock *never* fails for a new pcluster.
800          */
801         mutex_init(&pcl->lock);
802         DBG_BUGON(!mutex_trylock(&pcl->lock));
803
804         if (ztailpacking) {
805                 pcl->obj.index = 0;     /* which indicates ztailpacking */
806                 pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa);
807                 pcl->tailpacking_size = map->m_plen;
808         } else {
809                 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
810
811                 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
812                 if (IS_ERR(grp)) {
813                         err = PTR_ERR(grp);
814                         goto err_out;
815                 }
816
817                 if (grp != &pcl->obj) {
818                         fe->pcl = container_of(grp,
819                                         struct z_erofs_pcluster, obj);
820                         err = -EEXIST;
821                         goto err_out;
822                 }
823         }
824         /* used to check tail merging loop due to corrupted images */
825         if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
826                 fe->tailpcl = pcl;
827         fe->owned_head = &pcl->next;
828         fe->pcl = pcl;
829         return 0;
830
831 err_out:
832         mutex_unlock(&pcl->lock);
833         z_erofs_free_pcluster(pcl);
834         return err;
835 }
836
837 static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
838 {
839         struct erofs_map_blocks *map = &fe->map;
840         struct erofs_workgroup *grp = NULL;
841         int ret;
842
843         DBG_BUGON(fe->pcl);
844
845         /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
846         DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
847         DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
848
849         if (!(map->m_flags & EROFS_MAP_META)) {
850                 grp = erofs_find_workgroup(fe->inode->i_sb,
851                                            map->m_pa >> PAGE_SHIFT);
852         } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
853                 DBG_BUGON(1);
854                 return -EFSCORRUPTED;
855         }
856
857         if (grp) {
858                 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
859                 ret = -EEXIST;
860         } else {
861                 ret = z_erofs_register_pcluster(fe);
862         }
863
864         if (ret == -EEXIST) {
865                 mutex_lock(&fe->pcl->lock);
866                 /* used to check tail merging loop due to corrupted images */
867                 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
868                         fe->tailpcl = fe->pcl;
869
870                 z_erofs_try_to_claim_pcluster(fe);
871         } else if (ret) {
872                 return ret;
873         }
874         z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
875                                 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
876         /* since file-backed online pages are traversed in reverse order */
877         fe->icur = z_erofs_pclusterpages(fe->pcl);
878         return 0;
879 }
880
881 /*
882  * keep in mind that no referenced pclusters will be freed
883  * only after a RCU grace period.
884  */
885 static void z_erofs_rcu_callback(struct rcu_head *head)
886 {
887         z_erofs_free_pcluster(container_of(head,
888                         struct z_erofs_pcluster, rcu));
889 }
890
891 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
892 {
893         struct z_erofs_pcluster *const pcl =
894                 container_of(grp, struct z_erofs_pcluster, obj);
895
896         call_rcu(&pcl->rcu, z_erofs_rcu_callback);
897 }
898
899 static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
900 {
901         struct z_erofs_pcluster *pcl = fe->pcl;
902
903         if (!pcl)
904                 return false;
905
906         z_erofs_bvec_iter_end(&fe->biter);
907         mutex_unlock(&pcl->lock);
908
909         if (fe->candidate_bvpage) {
910                 DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
911                 fe->candidate_bvpage = NULL;
912         }
913
914         /*
915          * if all pending pages are added, don't hold its reference
916          * any longer if the pcluster isn't hosted by ourselves.
917          */
918         if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
919                 erofs_workgroup_put(&pcl->obj);
920
921         fe->pcl = NULL;
922         return true;
923 }
924
925 static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
926                                  struct page *page, unsigned int pageofs,
927                                  unsigned int len)
928 {
929         struct super_block *sb = inode->i_sb;
930         struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
931         struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
932         u8 *src, *dst;
933         unsigned int i, cnt;
934
935         if (!packed_inode)
936                 return -EFSCORRUPTED;
937
938         buf.inode = packed_inode;
939         pos += EROFS_I(inode)->z_fragmentoff;
940         for (i = 0; i < len; i += cnt) {
941                 cnt = min_t(unsigned int, len - i,
942                             sb->s_blocksize - erofs_blkoff(sb, pos));
943                 src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
944                 if (IS_ERR(src)) {
945                         erofs_put_metabuf(&buf);
946                         return PTR_ERR(src);
947                 }
948
949                 dst = kmap_local_page(page);
950                 memcpy(dst + pageofs + i, src + erofs_blkoff(sb, pos), cnt);
951                 kunmap_local(dst);
952                 pos += cnt;
953         }
954         erofs_put_metabuf(&buf);
955         return 0;
956 }
957
958 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
959                                 struct page *page, struct page **pagepool)
960 {
961         struct inode *const inode = fe->inode;
962         struct erofs_map_blocks *const map = &fe->map;
963         const loff_t offset = page_offset(page);
964         bool tight = true, exclusive;
965         unsigned int cur, end, spiltted;
966         int err = 0;
967
968         /* register locked file pages as online pages in pack */
969         z_erofs_onlinepage_init(page);
970
971         spiltted = 0;
972         end = PAGE_SIZE;
973 repeat:
974         cur = end - 1;
975
976         if (offset + cur < map->m_la ||
977             offset + cur >= map->m_la + map->m_llen) {
978                 if (z_erofs_collector_end(fe))
979                         fe->backmost = false;
980                 map->m_la = offset + cur;
981                 map->m_llen = 0;
982                 err = z_erofs_map_blocks_iter(inode, map, 0);
983                 if (err)
984                         goto out;
985         } else {
986                 if (fe->pcl)
987                         goto hitted;
988                 /* didn't get a valid pcluster previously (very rare) */
989         }
990
991         if (!(map->m_flags & EROFS_MAP_MAPPED) ||
992             map->m_flags & EROFS_MAP_FRAGMENT)
993                 goto hitted;
994
995         err = z_erofs_collector_begin(fe);
996         if (err)
997                 goto out;
998
999         if (z_erofs_is_inline_pcluster(fe->pcl)) {
1000                 void *mp;
1001
1002                 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
1003                                         erofs_blknr(inode->i_sb, map->m_pa),
1004                                         EROFS_NO_KMAP);
1005                 if (IS_ERR(mp)) {
1006                         err = PTR_ERR(mp);
1007                         erofs_err(inode->i_sb,
1008                                   "failed to get inline page, err %d", err);
1009                         goto out;
1010                 }
1011                 get_page(fe->map.buf.page);
1012                 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
1013                            fe->map.buf.page);
1014                 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
1015         } else {
1016                 /* bind cache first when cached decompression is preferred */
1017                 z_erofs_bind_cache(fe, pagepool);
1018         }
1019 hitted:
1020         /*
1021          * Ensure the current partial page belongs to this submit chain rather
1022          * than other concurrent submit chains or the noio(bypass) chain since
1023          * those chains are handled asynchronously thus the page cannot be used
1024          * for inplace I/O or bvpage (should be processed in a strict order.)
1025          */
1026         tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
1027                   fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1028
1029         cur = end - min_t(unsigned int, offset + end - map->m_la, end);
1030         if (!(map->m_flags & EROFS_MAP_MAPPED)) {
1031                 zero_user_segment(page, cur, end);
1032                 goto next_part;
1033         }
1034         if (map->m_flags & EROFS_MAP_FRAGMENT) {
1035                 unsigned int pageofs, skip, len;
1036
1037                 if (offset > map->m_la) {
1038                         pageofs = 0;
1039                         skip = offset - map->m_la;
1040                 } else {
1041                         pageofs = map->m_la & ~PAGE_MASK;
1042                         skip = 0;
1043                 }
1044                 len = min_t(unsigned int, map->m_llen - skip, end - cur);
1045                 err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
1046                 if (err)
1047                         goto out;
1048                 ++spiltted;
1049                 tight = false;
1050                 goto next_part;
1051         }
1052
1053         exclusive = (!cur && (!spiltted || tight));
1054         if (cur)
1055                 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1056
1057 retry:
1058         err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
1059                                         .page = page,
1060                                         .offset = offset - map->m_la,
1061                                         .end = end,
1062                                   }), exclusive);
1063         /* should allocate an additional short-lived page for bvset */
1064         if (err == -EAGAIN && !fe->candidate_bvpage) {
1065                 fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
1066                 set_page_private(fe->candidate_bvpage,
1067                                  Z_EROFS_SHORTLIVED_PAGE);
1068                 goto retry;
1069         }
1070
1071         if (err) {
1072                 DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
1073                 goto out;
1074         }
1075
1076         z_erofs_onlinepage_split(page);
1077         /* bump up the number of spiltted parts of a page */
1078         ++spiltted;
1079         if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1080                 fe->pcl->multibases = true;
1081         if (fe->pcl->length < offset + end - map->m_la) {
1082                 fe->pcl->length = offset + end - map->m_la;
1083                 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1084         }
1085         if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1086             !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1087             fe->pcl->length == map->m_llen)
1088                 fe->pcl->partial = false;
1089 next_part:
1090         /* shorten the remaining extent to update progress */
1091         map->m_llen = offset + cur - map->m_la;
1092         map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
1093
1094         end = cur;
1095         if (end > 0)
1096                 goto repeat;
1097
1098 out:
1099         if (err)
1100                 z_erofs_page_mark_eio(page);
1101         z_erofs_onlinepage_endio(page);
1102         return err;
1103 }
1104
1105 static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
1106                                        unsigned int readahead_pages)
1107 {
1108         /* auto: enable for read_folio, disable for readahead */
1109         if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
1110             !readahead_pages)
1111                 return true;
1112
1113         if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
1114             (readahead_pages <= sbi->opt.max_sync_decompress_pages))
1115                 return true;
1116
1117         return false;
1118 }
1119
1120 static bool z_erofs_page_is_invalidated(struct page *page)
1121 {
1122         return !page->mapping && !z_erofs_is_shortlived_page(page);
1123 }
1124
1125 struct z_erofs_decompress_backend {
1126         struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1127         struct super_block *sb;
1128         struct z_erofs_pcluster *pcl;
1129
1130         /* pages with the longest decompressed length for deduplication */
1131         struct page **decompressed_pages;
1132         /* pages to keep the compressed data */
1133         struct page **compressed_pages;
1134
1135         struct list_head decompressed_secondary_bvecs;
1136         struct page **pagepool;
1137         unsigned int onstack_used, nr_pages;
1138 };
1139
1140 struct z_erofs_bvec_item {
1141         struct z_erofs_bvec bvec;
1142         struct list_head list;
1143 };
1144
1145 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
1146                                          struct z_erofs_bvec *bvec)
1147 {
1148         struct z_erofs_bvec_item *item;
1149
1150         if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1151                 unsigned int pgnr;
1152
1153                 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
1154                 DBG_BUGON(pgnr >= be->nr_pages);
1155                 if (!be->decompressed_pages[pgnr]) {
1156                         be->decompressed_pages[pgnr] = bvec->page;
1157                         return;
1158                 }
1159         }
1160
1161         /* (cold path) one pcluster is requested multiple times */
1162         item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1163         item->bvec = *bvec;
1164         list_add(&item->list, &be->decompressed_secondary_bvecs);
1165 }
1166
1167 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1168                                       int err)
1169 {
1170         unsigned int off0 = be->pcl->pageofs_out;
1171         struct list_head *p, *n;
1172
1173         list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1174                 struct z_erofs_bvec_item *bvi;
1175                 unsigned int end, cur;
1176                 void *dst, *src;
1177
1178                 bvi = container_of(p, struct z_erofs_bvec_item, list);
1179                 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1180                 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1181                             bvi->bvec.end);
1182                 dst = kmap_local_page(bvi->bvec.page);
1183                 while (cur < end) {
1184                         unsigned int pgnr, scur, len;
1185
1186                         pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1187                         DBG_BUGON(pgnr >= be->nr_pages);
1188
1189                         scur = bvi->bvec.offset + cur -
1190                                         ((pgnr << PAGE_SHIFT) - off0);
1191                         len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1192                         if (!be->decompressed_pages[pgnr]) {
1193                                 err = -EFSCORRUPTED;
1194                                 cur += len;
1195                                 continue;
1196                         }
1197                         src = kmap_local_page(be->decompressed_pages[pgnr]);
1198                         memcpy(dst + cur, src + scur, len);
1199                         kunmap_local(src);
1200                         cur += len;
1201                 }
1202                 kunmap_local(dst);
1203                 if (err)
1204                         z_erofs_page_mark_eio(bvi->bvec.page);
1205                 z_erofs_onlinepage_endio(bvi->bvec.page);
1206                 list_del(p);
1207                 kfree(bvi);
1208         }
1209 }
1210
1211 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
1212 {
1213         struct z_erofs_pcluster *pcl = be->pcl;
1214         struct z_erofs_bvec_iter biter;
1215         struct page *old_bvpage;
1216         int i;
1217
1218         z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1219         for (i = 0; i < pcl->vcnt; ++i) {
1220                 struct z_erofs_bvec bvec;
1221
1222                 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1223
1224                 if (old_bvpage)
1225                         z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1226
1227                 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1228                 z_erofs_do_decompressed_bvec(be, &bvec);
1229         }
1230
1231         old_bvpage = z_erofs_bvec_iter_end(&biter);
1232         if (old_bvpage)
1233                 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1234 }
1235
1236 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
1237                                   bool *overlapped)
1238 {
1239         struct z_erofs_pcluster *pcl = be->pcl;
1240         unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1241         int i, err = 0;
1242
1243         *overlapped = false;
1244         for (i = 0; i < pclusterpages; ++i) {
1245                 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1246                 struct page *page = bvec->page;
1247
1248                 /* compressed pages ought to be present before decompressing */
1249                 if (!page) {
1250                         DBG_BUGON(1);
1251                         continue;
1252                 }
1253                 be->compressed_pages[i] = page;
1254
1255                 if (z_erofs_is_inline_pcluster(pcl)) {
1256                         if (!PageUptodate(page))
1257                                 err = -EIO;
1258                         continue;
1259                 }
1260
1261                 DBG_BUGON(z_erofs_page_is_invalidated(page));
1262                 if (!z_erofs_is_shortlived_page(page)) {
1263                         if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
1264                                 if (!PageUptodate(page))
1265                                         err = -EIO;
1266                                 continue;
1267                         }
1268                         z_erofs_do_decompressed_bvec(be, bvec);
1269                         *overlapped = true;
1270                 }
1271         }
1272
1273         if (err)
1274                 return err;
1275         return 0;
1276 }
1277
1278 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
1279                                        int err)
1280 {
1281         struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1282         struct z_erofs_pcluster *pcl = be->pcl;
1283         unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1284         const struct z_erofs_decompressor *decompressor =
1285                                 &erofs_decompressors[pcl->algorithmformat];
1286         unsigned int i, inputsize;
1287         int err2;
1288         struct page *page;
1289         bool overlapped;
1290
1291         mutex_lock(&pcl->lock);
1292         be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1293
1294         /* allocate (de)compressed page arrays if cannot be kept on stack */
1295         be->decompressed_pages = NULL;
1296         be->compressed_pages = NULL;
1297         be->onstack_used = 0;
1298         if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1299                 be->decompressed_pages = be->onstack_pages;
1300                 be->onstack_used = be->nr_pages;
1301                 memset(be->decompressed_pages, 0,
1302                        sizeof(struct page *) * be->nr_pages);
1303         }
1304
1305         if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1306                 be->compressed_pages = be->onstack_pages + be->onstack_used;
1307
1308         if (!be->decompressed_pages)
1309                 be->decompressed_pages =
1310                         kvcalloc(be->nr_pages, sizeof(struct page *),
1311                                  GFP_KERNEL | __GFP_NOFAIL);
1312         if (!be->compressed_pages)
1313                 be->compressed_pages =
1314                         kvcalloc(pclusterpages, sizeof(struct page *),
1315                                  GFP_KERNEL | __GFP_NOFAIL);
1316
1317         z_erofs_parse_out_bvecs(be);
1318         err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1319         if (err2)
1320                 err = err2;
1321         if (err)
1322                 goto out;
1323
1324         if (z_erofs_is_inline_pcluster(pcl))
1325                 inputsize = pcl->tailpacking_size;
1326         else
1327                 inputsize = pclusterpages * PAGE_SIZE;
1328
1329         err = decompressor->decompress(&(struct z_erofs_decompress_req) {
1330                                         .sb = be->sb,
1331                                         .in = be->compressed_pages,
1332                                         .out = be->decompressed_pages,
1333                                         .pageofs_in = pcl->pageofs_in,
1334                                         .pageofs_out = pcl->pageofs_out,
1335                                         .inputsize = inputsize,
1336                                         .outputsize = pcl->length,
1337                                         .alg = pcl->algorithmformat,
1338                                         .inplace_io = overlapped,
1339                                         .partial_decoding = pcl->partial,
1340                                         .fillgaps = pcl->multibases,
1341                                  }, be->pagepool);
1342
1343 out:
1344         /* must handle all compressed pages before actual file pages */
1345         if (z_erofs_is_inline_pcluster(pcl)) {
1346                 page = pcl->compressed_bvecs[0].page;
1347                 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1348                 put_page(page);
1349         } else {
1350                 for (i = 0; i < pclusterpages; ++i) {
1351                         page = pcl->compressed_bvecs[i].page;
1352
1353                         if (erofs_page_is_managed(sbi, page))
1354                                 continue;
1355
1356                         /* recycle all individual short-lived pages */
1357                         (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1358                         WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1359                 }
1360         }
1361         if (be->compressed_pages < be->onstack_pages ||
1362             be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1363                 kvfree(be->compressed_pages);
1364         z_erofs_fill_other_copies(be, err);
1365
1366         for (i = 0; i < be->nr_pages; ++i) {
1367                 page = be->decompressed_pages[i];
1368                 if (!page)
1369                         continue;
1370
1371                 DBG_BUGON(z_erofs_page_is_invalidated(page));
1372
1373                 /* recycle all individual short-lived pages */
1374                 if (z_erofs_put_shortlivedpage(be->pagepool, page))
1375                         continue;
1376                 if (err)
1377                         z_erofs_page_mark_eio(page);
1378                 z_erofs_onlinepage_endio(page);
1379         }
1380
1381         if (be->decompressed_pages != be->onstack_pages)
1382                 kvfree(be->decompressed_pages);
1383
1384         pcl->length = 0;
1385         pcl->partial = true;
1386         pcl->multibases = false;
1387         pcl->bvset.nextpage = NULL;
1388         pcl->vcnt = 0;
1389
1390         /* pcluster lock MUST be taken before the following line */
1391         WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1392         mutex_unlock(&pcl->lock);
1393         return err;
1394 }
1395
1396 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1397                                      struct page **pagepool)
1398 {
1399         struct z_erofs_decompress_backend be = {
1400                 .sb = io->sb,
1401                 .pagepool = pagepool,
1402                 .decompressed_secondary_bvecs =
1403                         LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1404         };
1405         z_erofs_next_pcluster_t owned = io->head;
1406
1407         while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1408                 /* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1409                 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
1410                 /* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */
1411                 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1412
1413                 be.pcl = container_of(owned, struct z_erofs_pcluster, next);
1414                 owned = READ_ONCE(be.pcl->next);
1415
1416                 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
1417                 erofs_workgroup_put(&be.pcl->obj);
1418         }
1419 }
1420
1421 static void z_erofs_decompressqueue_work(struct work_struct *work)
1422 {
1423         struct z_erofs_decompressqueue *bgq =
1424                 container_of(work, struct z_erofs_decompressqueue, u.work);
1425         struct page *pagepool = NULL;
1426
1427         DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1428         z_erofs_decompress_queue(bgq, &pagepool);
1429         erofs_release_pages(&pagepool);
1430         kvfree(bgq);
1431 }
1432
1433 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1434 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1435 {
1436         z_erofs_decompressqueue_work((struct work_struct *)work);
1437 }
1438 #endif
1439
1440 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1441                                        int bios)
1442 {
1443         struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1444
1445         /* wake up the caller thread for sync decompression */
1446         if (io->sync) {
1447                 if (!atomic_add_return(bios, &io->pending_bios))
1448                         complete(&io->u.done);
1449                 return;
1450         }
1451
1452         if (atomic_add_return(bios, &io->pending_bios))
1453                 return;
1454         /* Use (kthread_)work and sync decompression for atomic contexts only */
1455         if (in_atomic() || irqs_disabled()) {
1456 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1457                 struct kthread_worker *worker;
1458
1459                 rcu_read_lock();
1460                 worker = rcu_dereference(
1461                                 z_erofs_pcpu_workers[raw_smp_processor_id()]);
1462                 if (!worker) {
1463                         INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1464                         queue_work(z_erofs_workqueue, &io->u.work);
1465                 } else {
1466                         kthread_queue_work(worker, &io->u.kthread_work);
1467                 }
1468                 rcu_read_unlock();
1469 #else
1470                 queue_work(z_erofs_workqueue, &io->u.work);
1471 #endif
1472                 /* enable sync decompression for readahead */
1473                 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1474                         sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1475                 return;
1476         }
1477         z_erofs_decompressqueue_work(&io->u.work);
1478 }
1479
1480 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1481                                                unsigned int nr,
1482                                                struct page **pagepool,
1483                                                struct address_space *mc)
1484 {
1485         const pgoff_t index = pcl->obj.index;
1486         gfp_t gfp = mapping_gfp_mask(mc);
1487         bool tocache = false;
1488
1489         struct address_space *mapping;
1490         struct page *oldpage, *page;
1491         int justfound;
1492
1493 repeat:
1494         page = READ_ONCE(pcl->compressed_bvecs[nr].page);
1495         oldpage = page;
1496
1497         if (!page)
1498                 goto out_allocpage;
1499
1500         justfound = (unsigned long)page & 1UL;
1501         page = (struct page *)((unsigned long)page & ~1UL);
1502
1503         /*
1504          * preallocated cached pages, which is used to avoid direct reclaim
1505          * otherwise, it will go inplace I/O path instead.
1506          */
1507         if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1508                 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1509                 set_page_private(page, 0);
1510                 tocache = true;
1511                 goto out_tocache;
1512         }
1513         mapping = READ_ONCE(page->mapping);
1514
1515         /*
1516          * file-backed online pages in plcuster are all locked steady,
1517          * therefore it is impossible for `mapping' to be NULL.
1518          */
1519         if (mapping && mapping != mc)
1520                 /* ought to be unmanaged pages */
1521                 goto out;
1522
1523         /* directly return for shortlived page as well */
1524         if (z_erofs_is_shortlived_page(page))
1525                 goto out;
1526
1527         lock_page(page);
1528
1529         /* only true if page reclaim goes wrong, should never happen */
1530         DBG_BUGON(justfound && PagePrivate(page));
1531
1532         /* the page is still in manage cache */
1533         if (page->mapping == mc) {
1534                 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1535
1536                 if (!PagePrivate(page)) {
1537                         /*
1538                          * impossible to be !PagePrivate(page) for
1539                          * the current restriction as well if
1540                          * the page is already in compressed_bvecs[].
1541                          */
1542                         DBG_BUGON(!justfound);
1543
1544                         justfound = 0;
1545                         set_page_private(page, (unsigned long)pcl);
1546                         SetPagePrivate(page);
1547                 }
1548
1549                 /* no need to submit io if it is already up-to-date */
1550                 if (PageUptodate(page)) {
1551                         unlock_page(page);
1552                         page = NULL;
1553                 }
1554                 goto out;
1555         }
1556
1557         /*
1558          * the managed page has been truncated, it's unsafe to
1559          * reuse this one, let's allocate a new cache-managed page.
1560          */
1561         DBG_BUGON(page->mapping);
1562         DBG_BUGON(!justfound);
1563
1564         tocache = true;
1565         unlock_page(page);
1566         put_page(page);
1567 out_allocpage:
1568         page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1569         if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1570                                oldpage, page)) {
1571                 erofs_pagepool_add(pagepool, page);
1572                 cond_resched();
1573                 goto repeat;
1574         }
1575 out_tocache:
1576         if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1577                 /* turn into temporary page if fails (1 ref) */
1578                 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1579                 goto out;
1580         }
1581         attach_page_private(page, pcl);
1582         /* drop a refcount added by allocpage (then we have 2 refs here) */
1583         put_page(page);
1584
1585 out:    /* the only exit (for tracing and debugging) */
1586         return page;
1587 }
1588
1589 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1590                               struct z_erofs_decompressqueue *fgq, bool *fg)
1591 {
1592         struct z_erofs_decompressqueue *q;
1593
1594         if (fg && !*fg) {
1595                 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1596                 if (!q) {
1597                         *fg = true;
1598                         goto fg_out;
1599                 }
1600 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1601                 kthread_init_work(&q->u.kthread_work,
1602                                   z_erofs_decompressqueue_kthread_work);
1603 #else
1604                 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1605 #endif
1606         } else {
1607 fg_out:
1608                 q = fgq;
1609                 init_completion(&fgq->u.done);
1610                 atomic_set(&fgq->pending_bios, 0);
1611                 q->eio = false;
1612                 q->sync = true;
1613         }
1614         q->sb = sb;
1615         q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1616         return q;
1617 }
1618
1619 /* define decompression jobqueue types */
1620 enum {
1621         JQ_BYPASS,
1622         JQ_SUBMIT,
1623         NR_JOBQUEUES,
1624 };
1625
1626 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1627                                     z_erofs_next_pcluster_t qtail[],
1628                                     z_erofs_next_pcluster_t owned_head)
1629 {
1630         z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1631         z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1632
1633         DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1634         if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1635                 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1636
1637         WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1638
1639         WRITE_ONCE(*submit_qtail, owned_head);
1640         WRITE_ONCE(*bypass_qtail, &pcl->next);
1641
1642         qtail[JQ_BYPASS] = &pcl->next;
1643 }
1644
1645 static void z_erofs_decompressqueue_endio(struct bio *bio)
1646 {
1647         struct z_erofs_decompressqueue *q = bio->bi_private;
1648         blk_status_t err = bio->bi_status;
1649         struct bio_vec *bvec;
1650         struct bvec_iter_all iter_all;
1651
1652         bio_for_each_segment_all(bvec, bio, iter_all) {
1653                 struct page *page = bvec->bv_page;
1654
1655                 DBG_BUGON(PageUptodate(page));
1656                 DBG_BUGON(z_erofs_page_is_invalidated(page));
1657
1658                 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1659                         if (!err)
1660                                 SetPageUptodate(page);
1661                         unlock_page(page);
1662                 }
1663         }
1664         if (err)
1665                 q->eio = true;
1666         z_erofs_decompress_kickoff(q, -1);
1667         bio_put(bio);
1668 }
1669
1670 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1671                                  struct page **pagepool,
1672                                  struct z_erofs_decompressqueue *fgq,
1673                                  bool *force_fg, bool readahead)
1674 {
1675         struct super_block *sb = f->inode->i_sb;
1676         struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1677         z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1678         struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1679         z_erofs_next_pcluster_t owned_head = f->owned_head;
1680         /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1681         pgoff_t last_index;
1682         struct block_device *last_bdev;
1683         unsigned int nr_bios = 0;
1684         struct bio *bio = NULL;
1685         unsigned long pflags;
1686         int memstall = 0;
1687
1688         /*
1689          * if managed cache is enabled, bypass jobqueue is needed,
1690          * no need to read from device for all pclusters in this queue.
1691          */
1692         q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1693         q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1694
1695         qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1696         qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1697
1698         /* by default, all need io submission */
1699         q[JQ_SUBMIT]->head = owned_head;
1700
1701         do {
1702                 struct erofs_map_dev mdev;
1703                 struct z_erofs_pcluster *pcl;
1704                 pgoff_t cur, end;
1705                 unsigned int i = 0;
1706                 bool bypass = true;
1707
1708                 /* no possible 'owned_head' equals the following */
1709                 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1710                 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1711
1712                 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1713
1714                 /* close the main owned chain at first */
1715                 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1716                                      Z_EROFS_PCLUSTER_TAIL_CLOSED);
1717                 if (z_erofs_is_inline_pcluster(pcl)) {
1718                         move_to_bypass_jobqueue(pcl, qtail, owned_head);
1719                         continue;
1720                 }
1721
1722                 /* no device id here, thus it will always succeed */
1723                 mdev = (struct erofs_map_dev) {
1724                         .m_pa = erofs_pos(sb, pcl->obj.index),
1725                 };
1726                 (void)erofs_map_dev(sb, &mdev);
1727
1728                 cur = erofs_blknr(sb, mdev.m_pa);
1729                 end = cur + pcl->pclusterpages;
1730
1731                 do {
1732                         struct page *page;
1733
1734                         page = pickup_page_for_submission(pcl, i++, pagepool,
1735                                                           mc);
1736                         if (!page)
1737                                 continue;
1738
1739                         if (bio && (cur != last_index + 1 ||
1740                                     last_bdev != mdev.m_bdev)) {
1741 submit_bio_retry:
1742                                 submit_bio(bio);
1743                                 if (memstall) {
1744                                         psi_memstall_leave(&pflags);
1745                                         memstall = 0;
1746                                 }
1747                                 bio = NULL;
1748                         }
1749
1750                         if (unlikely(PageWorkingset(page)) && !memstall) {
1751                                 psi_memstall_enter(&pflags);
1752                                 memstall = 1;
1753                         }
1754
1755                         if (!bio) {
1756                                 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1757                                                 REQ_OP_READ, GFP_NOIO);
1758                                 bio->bi_end_io = z_erofs_decompressqueue_endio;
1759
1760                                 last_bdev = mdev.m_bdev;
1761                                 bio->bi_iter.bi_sector = (sector_t)cur <<
1762                                         (sb->s_blocksize_bits - 9);
1763                                 bio->bi_private = q[JQ_SUBMIT];
1764                                 if (readahead)
1765                                         bio->bi_opf |= REQ_RAHEAD;
1766                                 ++nr_bios;
1767                         }
1768
1769                         if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1770                                 goto submit_bio_retry;
1771
1772                         last_index = cur;
1773                         bypass = false;
1774                 } while (++cur < end);
1775
1776                 if (!bypass)
1777                         qtail[JQ_SUBMIT] = &pcl->next;
1778                 else
1779                         move_to_bypass_jobqueue(pcl, qtail, owned_head);
1780         } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1781
1782         if (bio) {
1783                 submit_bio(bio);
1784                 if (memstall)
1785                         psi_memstall_leave(&pflags);
1786         }
1787
1788         /*
1789          * although background is preferred, no one is pending for submission.
1790          * don't issue decompression but drop it directly instead.
1791          */
1792         if (!*force_fg && !nr_bios) {
1793                 kvfree(q[JQ_SUBMIT]);
1794                 return;
1795         }
1796         z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1797 }
1798
1799 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1800                              struct page **pagepool, bool force_fg, bool ra)
1801 {
1802         struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1803
1804         if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1805                 return;
1806         z_erofs_submit_queue(f, pagepool, io, &force_fg, ra);
1807
1808         /* handle bypass queue (no i/o pclusters) immediately */
1809         z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1810
1811         if (!force_fg)
1812                 return;
1813
1814         /* wait until all bios are completed */
1815         wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1816
1817         /* handle synchronous decompress queue in the caller context */
1818         z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1819 }
1820
1821 /*
1822  * Since partial uptodate is still unimplemented for now, we have to use
1823  * approximate readmore strategies as a start.
1824  */
1825 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1826                                       struct readahead_control *rac,
1827                                       struct page **pagepool, bool backmost)
1828 {
1829         struct inode *inode = f->inode;
1830         struct erofs_map_blocks *map = &f->map;
1831         erofs_off_t cur, end, headoffset = f->headoffset;
1832         int err;
1833
1834         if (backmost) {
1835                 if (rac)
1836                         end = headoffset + readahead_length(rac) - 1;
1837                 else
1838                         end = headoffset + PAGE_SIZE - 1;
1839                 map->m_la = end;
1840                 err = z_erofs_map_blocks_iter(inode, map,
1841                                               EROFS_GET_BLOCKS_READMORE);
1842                 if (err)
1843                         return;
1844
1845                 /* expand ra for the trailing edge if readahead */
1846                 if (rac) {
1847                         cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1848                         readahead_expand(rac, headoffset, cur - headoffset);
1849                         return;
1850                 }
1851                 end = round_up(end, PAGE_SIZE);
1852         } else {
1853                 end = round_up(map->m_la, PAGE_SIZE);
1854
1855                 if (!map->m_llen)
1856                         return;
1857         }
1858
1859         cur = map->m_la + map->m_llen - 1;
1860         while (cur >= end) {
1861                 pgoff_t index = cur >> PAGE_SHIFT;
1862                 struct page *page;
1863
1864                 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1865                 if (page) {
1866                         if (PageUptodate(page)) {
1867                                 unlock_page(page);
1868                         } else {
1869                                 err = z_erofs_do_read_page(f, page, pagepool);
1870                                 if (err)
1871                                         erofs_err(inode->i_sb,
1872                                                   "readmore error at page %lu @ nid %llu",
1873                                                   index, EROFS_I(inode)->nid);
1874                         }
1875                         put_page(page);
1876                 }
1877
1878                 if (cur < PAGE_SIZE)
1879                         break;
1880                 cur = (index << PAGE_SHIFT) - 1;
1881         }
1882 }
1883
1884 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1885 {
1886         struct page *page = &folio->page;
1887         struct inode *const inode = page->mapping->host;
1888         struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1889         struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1890         struct page *pagepool = NULL;
1891         int err;
1892
1893         trace_erofs_readpage(page, false);
1894         f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1895
1896         z_erofs_pcluster_readmore(&f, NULL, &pagepool, true);
1897         err = z_erofs_do_read_page(&f, page, &pagepool);
1898         z_erofs_pcluster_readmore(&f, NULL, &pagepool, false);
1899
1900         (void)z_erofs_collector_end(&f);
1901
1902         /* if some compressed cluster ready, need submit them anyway */
1903         z_erofs_runqueue(&f, &pagepool, z_erofs_is_sync_decompress(sbi, 0),
1904                          false);
1905
1906         if (err)
1907                 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1908
1909         erofs_put_metabuf(&f.map.buf);
1910         erofs_release_pages(&pagepool);
1911         return err;
1912 }
1913
1914 static void z_erofs_readahead(struct readahead_control *rac)
1915 {
1916         struct inode *const inode = rac->mapping->host;
1917         struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1918         struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1919         struct page *pagepool = NULL, *head = NULL, *page;
1920         unsigned int nr_pages;
1921
1922         f.headoffset = readahead_pos(rac);
1923
1924         z_erofs_pcluster_readmore(&f, rac, &pagepool, true);
1925         nr_pages = readahead_count(rac);
1926         trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1927
1928         while ((page = readahead_page(rac))) {
1929                 set_page_private(page, (unsigned long)head);
1930                 head = page;
1931         }
1932
1933         while (head) {
1934                 struct page *page = head;
1935                 int err;
1936
1937                 /* traversal in reverse order */
1938                 head = (void *)page_private(page);
1939
1940                 err = z_erofs_do_read_page(&f, page, &pagepool);
1941                 if (err)
1942                         erofs_err(inode->i_sb,
1943                                   "readahead error at page %lu @ nid %llu",
1944                                   page->index, EROFS_I(inode)->nid);
1945                 put_page(page);
1946         }
1947         z_erofs_pcluster_readmore(&f, rac, &pagepool, false);
1948         (void)z_erofs_collector_end(&f);
1949
1950         z_erofs_runqueue(&f, &pagepool,
1951                          z_erofs_is_sync_decompress(sbi, nr_pages), true);
1952         erofs_put_metabuf(&f.map.buf);
1953         erofs_release_pages(&pagepool);
1954 }
1955
1956 const struct address_space_operations z_erofs_aops = {
1957         .read_folio = z_erofs_read_folio,
1958         .readahead = z_erofs_readahead,
1959 };