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