OSDN Git Service

staging: erofs: fix error handling when failed to read compresssed data
[uclinux-h8/linux.git] / drivers / staging / erofs / unzip_vle.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/drivers/staging/erofs/unzip_vle.c
4  *
5  * Copyright (C) 2018 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Gao Xiang <gaoxiang25@huawei.com>
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file COPYING in the main directory of the Linux
11  * distribution for more details.
12  */
13 #include "unzip_vle.h"
14 #include <linux/prefetch.h>
15
16 #include <trace/events/erofs.h>
17
18 /*
19  * a compressed_pages[] placeholder in order to avoid
20  * being filled with file pages for in-place decompression.
21  */
22 #define PAGE_UNALLOCATED     ((void *)0x5F0E4B1D)
23
24 /* how to allocate cached pages for a workgroup */
25 enum z_erofs_cache_alloctype {
26         DONTALLOC,      /* don't allocate any cached pages */
27         DELAYEDALLOC,   /* delayed allocation (at the time of submitting io) */
28 };
29
30 /*
31  * tagged pointer with 1-bit tag for all compressed pages
32  * tag 0 - the page is just found with an extra page reference
33  */
34 typedef tagptr1_t compressed_page_t;
35
36 #define tag_compressed_page_justfound(page) \
37         tagptr_fold(compressed_page_t, page, 1)
38
39 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
40 static struct kmem_cache *z_erofs_workgroup_cachep __read_mostly;
41
42 void z_erofs_exit_zip_subsystem(void)
43 {
44         destroy_workqueue(z_erofs_workqueue);
45         kmem_cache_destroy(z_erofs_workgroup_cachep);
46 }
47
48 static inline int init_unzip_workqueue(void)
49 {
50         const unsigned int onlinecpus = num_possible_cpus();
51
52         /*
53          * we don't need too many threads, limiting threads
54          * could improve scheduling performance.
55          */
56         z_erofs_workqueue =
57                 alloc_workqueue("erofs_unzipd",
58                                 WQ_UNBOUND | WQ_HIGHPRI | WQ_CPU_INTENSIVE,
59                                 onlinecpus + onlinecpus / 4);
60
61         return z_erofs_workqueue ? 0 : -ENOMEM;
62 }
63
64 static void init_once(void *ptr)
65 {
66         struct z_erofs_vle_workgroup *grp = ptr;
67         struct z_erofs_vle_work *const work =
68                 z_erofs_vle_grab_primary_work(grp);
69         unsigned int i;
70
71         mutex_init(&work->lock);
72         work->nr_pages = 0;
73         work->vcnt = 0;
74         for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i)
75                 grp->compressed_pages[i] = NULL;
76 }
77
78 static void init_always(struct z_erofs_vle_workgroup *grp)
79 {
80         struct z_erofs_vle_work *const work =
81                 z_erofs_vle_grab_primary_work(grp);
82
83         atomic_set(&grp->obj.refcount, 1);
84         grp->flags = 0;
85
86         DBG_BUGON(work->nr_pages);
87         DBG_BUGON(work->vcnt);
88 }
89
90 int __init z_erofs_init_zip_subsystem(void)
91 {
92         z_erofs_workgroup_cachep =
93                 kmem_cache_create("erofs_compress",
94                                   Z_EROFS_WORKGROUP_SIZE, 0,
95                                   SLAB_RECLAIM_ACCOUNT, init_once);
96
97         if (z_erofs_workgroup_cachep) {
98                 if (!init_unzip_workqueue())
99                         return 0;
100
101                 kmem_cache_destroy(z_erofs_workgroup_cachep);
102         }
103         return -ENOMEM;
104 }
105
106 enum z_erofs_vle_work_role {
107         Z_EROFS_VLE_WORK_SECONDARY,
108         Z_EROFS_VLE_WORK_PRIMARY,
109         /*
110          * The current work was the tail of an exist chain, and the previous
111          * processed chained works are all decided to be hooked up to it.
112          * A new chain should be created for the remaining unprocessed works,
113          * therefore different from Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED,
114          * the next work cannot reuse the whole page in the following scenario:
115          *  ________________________________________________________________
116          * |      tail (partial) page     |       head (partial) page       |
117          * |  (belongs to the next work)  |  (belongs to the current work)  |
118          * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
119          */
120         Z_EROFS_VLE_WORK_PRIMARY_HOOKED,
121         /*
122          * The current work has been linked with the processed chained works,
123          * and could be also linked with the potential remaining works, which
124          * means if the processing page is the tail partial page of the work,
125          * the current work can safely use the whole page (since the next work
126          * is under control) for in-place decompression, as illustrated below:
127          *  ________________________________________________________________
128          * |  tail (partial) page  |          head (partial) page           |
129          * | (of the current work) |         (of the previous work)         |
130          * |  PRIMARY_FOLLOWED or  |                                        |
131          * |_____PRIMARY_HOOKED____|____________PRIMARY_FOLLOWED____________|
132          *
133          * [  (*) the above page can be used for the current work itself.  ]
134          */
135         Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED,
136         Z_EROFS_VLE_WORK_MAX
137 };
138
139 struct z_erofs_vle_work_builder {
140         enum z_erofs_vle_work_role role;
141         /*
142          * 'hosted = false' means that the current workgroup doesn't belong to
143          * the owned chained workgroups. In the other words, it is none of our
144          * business to submit this workgroup.
145          */
146         bool hosted;
147
148         struct z_erofs_vle_workgroup *grp;
149         struct z_erofs_vle_work *work;
150         struct z_erofs_pagevec_ctor vector;
151
152         /* pages used for reading the compressed data */
153         struct page **compressed_pages;
154         unsigned int compressed_deficit;
155 };
156
157 #define VLE_WORK_BUILDER_INIT() \
158         { .work = NULL, .role = Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED }
159
160 #ifdef EROFS_FS_HAS_MANAGED_CACHE
161 static void preload_compressed_pages(struct z_erofs_vle_work_builder *bl,
162                                      struct address_space *mc,
163                                      pgoff_t index,
164                                      unsigned int clusterpages,
165                                      enum z_erofs_cache_alloctype type,
166                                      struct list_head *pagepool,
167                                      gfp_t gfp)
168 {
169         struct page **const pages = bl->compressed_pages;
170         const unsigned int remaining = bl->compressed_deficit;
171         bool standalone = true;
172         unsigned int i, j = 0;
173
174         if (bl->role < Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED)
175                 return;
176
177         gfp = mapping_gfp_constraint(mc, gfp) & ~__GFP_RECLAIM;
178
179         index += clusterpages - remaining;
180
181         for (i = 0; i < remaining; ++i) {
182                 struct page *page;
183                 compressed_page_t t;
184
185                 /* the compressed page was loaded before */
186                 if (READ_ONCE(pages[i]))
187                         continue;
188
189                 page = find_get_page(mc, index + i);
190
191                 if (page) {
192                         t = tag_compressed_page_justfound(page);
193                 } else if (type == DELAYEDALLOC) {
194                         t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
195                 } else {        /* DONTALLOC */
196                         if (standalone)
197                                 j = i;
198                         standalone = false;
199                         continue;
200                 }
201
202                 if (!cmpxchg_relaxed(&pages[i], NULL, tagptr_cast_ptr(t)))
203                         continue;
204
205                 if (page)
206                         put_page(page);
207         }
208         bl->compressed_pages += j;
209         bl->compressed_deficit = remaining - j;
210
211         if (standalone)
212                 bl->role = Z_EROFS_VLE_WORK_PRIMARY;
213 }
214
215 /* called by erofs_shrinker to get rid of all compressed_pages */
216 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
217                                        struct erofs_workgroup *egrp)
218 {
219         struct z_erofs_vle_workgroup *const grp =
220                 container_of(egrp, struct z_erofs_vle_workgroup, obj);
221         struct address_space *const mapping = MNGD_MAPPING(sbi);
222         const int clusterpages = erofs_clusterpages(sbi);
223         int i;
224
225         /*
226          * refcount of workgroup is now freezed as 1,
227          * therefore no need to worry about available decompression users.
228          */
229         for (i = 0; i < clusterpages; ++i) {
230                 struct page *page = grp->compressed_pages[i];
231
232                 if (!page || page->mapping != mapping)
233                         continue;
234
235                 /* block other users from reclaiming or migrating the page */
236                 if (!trylock_page(page))
237                         return -EBUSY;
238
239                 /* barrier is implied in the following 'unlock_page' */
240                 WRITE_ONCE(grp->compressed_pages[i], NULL);
241
242                 set_page_private(page, 0);
243                 ClearPagePrivate(page);
244
245                 unlock_page(page);
246                 put_page(page);
247         }
248         return 0;
249 }
250
251 int erofs_try_to_free_cached_page(struct address_space *mapping,
252                                   struct page *page)
253 {
254         struct erofs_sb_info *const sbi = EROFS_SB(mapping->host->i_sb);
255         const unsigned int clusterpages = erofs_clusterpages(sbi);
256         struct z_erofs_vle_workgroup *const grp = (void *)page_private(page);
257         int ret = 0;    /* 0 - busy */
258
259         if (erofs_workgroup_try_to_freeze(&grp->obj, 1)) {
260                 unsigned int i;
261
262                 for (i = 0; i < clusterpages; ++i) {
263                         if (grp->compressed_pages[i] == page) {
264                                 WRITE_ONCE(grp->compressed_pages[i], NULL);
265                                 ret = 1;
266                                 break;
267                         }
268                 }
269                 erofs_workgroup_unfreeze(&grp->obj, 1);
270
271                 if (ret) {
272                         ClearPagePrivate(page);
273                         put_page(page);
274                 }
275         }
276         return ret;
277 }
278 #else
279 static void preload_compressed_pages(struct z_erofs_vle_work_builder *bl,
280                                      struct address_space *mc,
281                                      pgoff_t index,
282                                      unsigned int clusterpages,
283                                      enum z_erofs_cache_alloctype type,
284                                      struct list_head *pagepool,
285                                      gfp_t gfp)
286 {
287         /* nowhere to load compressed pages from */
288 }
289 #endif
290
291 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
292 static inline bool try_to_reuse_as_compressed_page(
293         struct z_erofs_vle_work_builder *b,
294         struct page *page)
295 {
296         while (b->compressed_deficit) {
297                 --b->compressed_deficit;
298                 if (!cmpxchg(b->compressed_pages++, NULL, page))
299                         return true;
300         }
301
302         return false;
303 }
304
305 /* callers must be with work->lock held */
306 static int z_erofs_vle_work_add_page(
307         struct z_erofs_vle_work_builder *builder,
308         struct page *page,
309         enum z_erofs_page_type type)
310 {
311         int ret;
312         bool occupied;
313
314         /* give priority for the compressed data storage */
315         if (builder->role >= Z_EROFS_VLE_WORK_PRIMARY &&
316                 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
317                 try_to_reuse_as_compressed_page(builder, page))
318                 return 0;
319
320         ret = z_erofs_pagevec_ctor_enqueue(&builder->vector,
321                 page, type, &occupied);
322         builder->work->vcnt += (unsigned int)ret;
323
324         return ret ? 0 : -EAGAIN;
325 }
326
327 static enum z_erofs_vle_work_role
328 try_to_claim_workgroup(struct z_erofs_vle_workgroup *grp,
329                        z_erofs_vle_owned_workgrp_t *owned_head,
330                        bool *hosted)
331 {
332         DBG_BUGON(*hosted == true);
333
334         /* let's claim these following types of workgroup */
335 retry:
336         if (grp->next == Z_EROFS_VLE_WORKGRP_NIL) {
337                 /* type 1, nil workgroup */
338                 if (cmpxchg(&grp->next, Z_EROFS_VLE_WORKGRP_NIL,
339                             *owned_head) != Z_EROFS_VLE_WORKGRP_NIL)
340                         goto retry;
341
342                 *owned_head = &grp->next;
343                 *hosted = true;
344                 /* lucky, I am the followee :) */
345                 return Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED;
346
347         } else if (grp->next == Z_EROFS_VLE_WORKGRP_TAIL) {
348                 /*
349                  * type 2, link to the end of a existing open chain,
350                  * be careful that its submission itself is governed
351                  * by the original owned chain.
352                  */
353                 if (cmpxchg(&grp->next, Z_EROFS_VLE_WORKGRP_TAIL,
354                             *owned_head) != Z_EROFS_VLE_WORKGRP_TAIL)
355                         goto retry;
356                 *owned_head = Z_EROFS_VLE_WORKGRP_TAIL;
357                 return Z_EROFS_VLE_WORK_PRIMARY_HOOKED;
358         }
359
360         return Z_EROFS_VLE_WORK_PRIMARY; /* :( better luck next time */
361 }
362
363 struct z_erofs_vle_work_finder {
364         struct super_block *sb;
365         pgoff_t idx;
366         unsigned int pageofs;
367
368         struct z_erofs_vle_workgroup **grp_ret;
369         enum z_erofs_vle_work_role *role;
370         z_erofs_vle_owned_workgrp_t *owned_head;
371         bool *hosted;
372 };
373
374 static struct z_erofs_vle_work *
375 z_erofs_vle_work_lookup(const struct z_erofs_vle_work_finder *f)
376 {
377         bool tag, primary;
378         struct erofs_workgroup *egrp;
379         struct z_erofs_vle_workgroup *grp;
380         struct z_erofs_vle_work *work;
381
382         egrp = erofs_find_workgroup(f->sb, f->idx, &tag);
383         if (!egrp) {
384                 *f->grp_ret = NULL;
385                 return NULL;
386         }
387
388         grp = container_of(egrp, struct z_erofs_vle_workgroup, obj);
389         *f->grp_ret = grp;
390
391         work = z_erofs_vle_grab_work(grp, f->pageofs);
392         /* if multiref is disabled, `primary' is always true */
393         primary = true;
394
395         DBG_BUGON(work->pageofs != f->pageofs);
396
397         /*
398          * lock must be taken first to avoid grp->next == NIL between
399          * claiming workgroup and adding pages:
400          *                        grp->next != NIL
401          *   grp->next = NIL
402          *   mutex_unlock_all
403          *                        mutex_lock(&work->lock)
404          *                        add all pages to pagevec
405          *
406          * [correct locking case 1]:
407          *   mutex_lock(grp->work[a])
408          *   ...
409          *   mutex_lock(grp->work[b])     mutex_lock(grp->work[c])
410          *   ...                          *role = SECONDARY
411          *                                add all pages to pagevec
412          *                                ...
413          *                                mutex_unlock(grp->work[c])
414          *   mutex_lock(grp->work[c])
415          *   ...
416          *   grp->next = NIL
417          *   mutex_unlock_all
418          *
419          * [correct locking case 2]:
420          *   mutex_lock(grp->work[b])
421          *   ...
422          *   mutex_lock(grp->work[a])
423          *   ...
424          *   mutex_lock(grp->work[c])
425          *   ...
426          *   grp->next = NIL
427          *   mutex_unlock_all
428          *                                mutex_lock(grp->work[a])
429          *                                *role = PRIMARY_OWNER
430          *                                add all pages to pagevec
431          *                                ...
432          */
433         mutex_lock(&work->lock);
434
435         *f->hosted = false;
436         if (!primary)
437                 *f->role = Z_EROFS_VLE_WORK_SECONDARY;
438         else    /* claim the workgroup if possible */
439                 *f->role = try_to_claim_workgroup(grp, f->owned_head,
440                                                   f->hosted);
441         return work;
442 }
443
444 static struct z_erofs_vle_work *
445 z_erofs_vle_work_register(const struct z_erofs_vle_work_finder *f,
446                           struct erofs_map_blocks *map)
447 {
448         bool gnew = false;
449         struct z_erofs_vle_workgroup *grp = *f->grp_ret;
450         struct z_erofs_vle_work *work;
451
452         /* if multiref is disabled, grp should never be nullptr */
453         if (unlikely(grp)) {
454                 DBG_BUGON(1);
455                 return ERR_PTR(-EINVAL);
456         }
457
458         /* no available workgroup, let's allocate one */
459         grp = kmem_cache_alloc(z_erofs_workgroup_cachep, GFP_NOFS);
460         if (unlikely(!grp))
461                 return ERR_PTR(-ENOMEM);
462
463         init_always(grp);
464         grp->obj.index = f->idx;
465         grp->llen = map->m_llen;
466
467         z_erofs_vle_set_workgrp_fmt(grp,
468                 (map->m_flags & EROFS_MAP_ZIPPED) ?
469                         Z_EROFS_VLE_WORKGRP_FMT_LZ4 :
470                         Z_EROFS_VLE_WORKGRP_FMT_PLAIN);
471
472         /* new workgrps have been claimed as type 1 */
473         WRITE_ONCE(grp->next, *f->owned_head);
474         /* primary and followed work for all new workgrps */
475         *f->role = Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED;
476         /* it should be submitted by ourselves */
477         *f->hosted = true;
478
479         gnew = true;
480         work = z_erofs_vle_grab_primary_work(grp);
481         work->pageofs = f->pageofs;
482
483         /*
484          * lock all primary followed works before visible to others
485          * and mutex_trylock *never* fails for a new workgroup.
486          */
487         mutex_trylock(&work->lock);
488
489         if (gnew) {
490                 int err = erofs_register_workgroup(f->sb, &grp->obj, 0);
491
492                 if (err) {
493                         mutex_unlock(&work->lock);
494                         kmem_cache_free(z_erofs_workgroup_cachep, grp);
495                         return ERR_PTR(-EAGAIN);
496                 }
497         }
498
499         *f->owned_head = &grp->next;
500         *f->grp_ret = grp;
501         return work;
502 }
503
504 #define builder_is_hooked(builder) \
505         ((builder)->role >= Z_EROFS_VLE_WORK_PRIMARY_HOOKED)
506
507 #define builder_is_followed(builder) \
508         ((builder)->role >= Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED)
509
510 static int z_erofs_vle_work_iter_begin(struct z_erofs_vle_work_builder *builder,
511                                        struct super_block *sb,
512                                        struct erofs_map_blocks *map,
513                                        z_erofs_vle_owned_workgrp_t *owned_head)
514 {
515         const unsigned int clusterpages = erofs_clusterpages(EROFS_SB(sb));
516         struct z_erofs_vle_workgroup *grp;
517         const struct z_erofs_vle_work_finder finder = {
518                 .sb = sb,
519                 .idx = erofs_blknr(map->m_pa),
520                 .pageofs = map->m_la & ~PAGE_MASK,
521                 .grp_ret = &grp,
522                 .role = &builder->role,
523                 .owned_head = owned_head,
524                 .hosted = &builder->hosted
525         };
526         struct z_erofs_vle_work *work;
527
528         DBG_BUGON(builder->work);
529
530         /* must be Z_EROFS_WORK_TAIL or the next chained work */
531         DBG_BUGON(*owned_head == Z_EROFS_VLE_WORKGRP_NIL);
532         DBG_BUGON(*owned_head == Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
533
534         DBG_BUGON(erofs_blkoff(map->m_pa));
535
536 repeat:
537         work = z_erofs_vle_work_lookup(&finder);
538         if (work) {
539                 unsigned int orig_llen;
540
541                 /* increase workgroup `llen' if needed */
542                 while ((orig_llen = READ_ONCE(grp->llen)) < map->m_llen &&
543                        orig_llen != cmpxchg_relaxed(&grp->llen,
544                                                     orig_llen, map->m_llen))
545                         cpu_relax();
546                 goto got_it;
547         }
548
549         work = z_erofs_vle_work_register(&finder, map);
550         if (unlikely(work == ERR_PTR(-EAGAIN)))
551                 goto repeat;
552
553         if (IS_ERR(work))
554                 return PTR_ERR(work);
555 got_it:
556         z_erofs_pagevec_ctor_init(&builder->vector,
557                 Z_EROFS_VLE_INLINE_PAGEVECS, work->pagevec, work->vcnt);
558
559         if (builder->role >= Z_EROFS_VLE_WORK_PRIMARY) {
560                 /* enable possibly in-place decompression */
561                 builder->compressed_pages = grp->compressed_pages;
562                 builder->compressed_deficit = clusterpages;
563         } else {
564                 builder->compressed_pages = NULL;
565                 builder->compressed_deficit = 0;
566         }
567
568         builder->grp = grp;
569         builder->work = work;
570         return 0;
571 }
572
573 /*
574  * keep in mind that no referenced workgroups will be freed
575  * only after a RCU grace period, so rcu_read_lock() could
576  * prevent a workgroup from being freed.
577  */
578 static void z_erofs_rcu_callback(struct rcu_head *head)
579 {
580         struct z_erofs_vle_work *work = container_of(head,
581                 struct z_erofs_vle_work, rcu);
582         struct z_erofs_vle_workgroup *grp =
583                 z_erofs_vle_work_workgroup(work, true);
584
585         kmem_cache_free(z_erofs_workgroup_cachep, grp);
586 }
587
588 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
589 {
590         struct z_erofs_vle_workgroup *const vgrp = container_of(grp,
591                 struct z_erofs_vle_workgroup, obj);
592         struct z_erofs_vle_work *const work = &vgrp->work;
593
594         call_rcu(&work->rcu, z_erofs_rcu_callback);
595 }
596
597 static void __z_erofs_vle_work_release(struct z_erofs_vle_workgroup *grp,
598         struct z_erofs_vle_work *work __maybe_unused)
599 {
600         erofs_workgroup_put(&grp->obj);
601 }
602
603 static void z_erofs_vle_work_release(struct z_erofs_vle_work *work)
604 {
605         struct z_erofs_vle_workgroup *grp =
606                 z_erofs_vle_work_workgroup(work, true);
607
608         __z_erofs_vle_work_release(grp, work);
609 }
610
611 static inline bool
612 z_erofs_vle_work_iter_end(struct z_erofs_vle_work_builder *builder)
613 {
614         struct z_erofs_vle_work *work = builder->work;
615
616         if (!work)
617                 return false;
618
619         z_erofs_pagevec_ctor_exit(&builder->vector, false);
620         mutex_unlock(&work->lock);
621
622         /*
623          * if all pending pages are added, don't hold work reference
624          * any longer if the current work isn't hosted by ourselves.
625          */
626         if (!builder->hosted)
627                 __z_erofs_vle_work_release(builder->grp, work);
628
629         builder->work = NULL;
630         builder->grp = NULL;
631         return true;
632 }
633
634 static inline struct page *__stagingpage_alloc(struct list_head *pagepool,
635                                                gfp_t gfp)
636 {
637         struct page *page = erofs_allocpage(pagepool, gfp);
638
639         if (unlikely(!page))
640                 return NULL;
641
642         page->mapping = Z_EROFS_MAPPING_STAGING;
643         return page;
644 }
645
646 struct z_erofs_vle_frontend {
647         struct inode *const inode;
648
649         struct z_erofs_vle_work_builder builder;
650         struct erofs_map_blocks map;
651
652         z_erofs_vle_owned_workgrp_t owned_head;
653
654         /* used for applying cache strategy on the fly */
655         bool backmost;
656         erofs_off_t headoffset;
657 };
658
659 #define VLE_FRONTEND_INIT(__i) { \
660         .inode = __i, \
661         .map = { \
662                 .m_llen = 0, \
663                 .m_plen = 0, \
664                 .mpage = NULL \
665         }, \
666         .builder = VLE_WORK_BUILDER_INIT(), \
667         .owned_head = Z_EROFS_VLE_WORKGRP_TAIL, \
668         .backmost = true, }
669
670 #ifdef EROFS_FS_HAS_MANAGED_CACHE
671 static inline bool
672 should_alloc_managed_pages(struct z_erofs_vle_frontend *fe, erofs_off_t la)
673 {
674         if (fe->backmost)
675                 return true;
676
677         if (EROFS_FS_ZIP_CACHE_LVL >= 2)
678                 return la < fe->headoffset;
679
680         return false;
681 }
682 #else
683 static inline bool
684 should_alloc_managed_pages(struct z_erofs_vle_frontend *fe, erofs_off_t la)
685 {
686         return false;
687 }
688 #endif
689
690 static int z_erofs_do_read_page(struct z_erofs_vle_frontend *fe,
691                                 struct page *page,
692                                 struct list_head *page_pool)
693 {
694         struct super_block *const sb = fe->inode->i_sb;
695         struct erofs_sb_info *const sbi __maybe_unused = EROFS_SB(sb);
696         struct erofs_map_blocks *const map = &fe->map;
697         struct z_erofs_vle_work_builder *const builder = &fe->builder;
698         const loff_t offset = page_offset(page);
699
700         bool tight = builder_is_hooked(builder);
701         struct z_erofs_vle_work *work = builder->work;
702
703         enum z_erofs_cache_alloctype cache_strategy;
704         enum z_erofs_page_type page_type;
705         unsigned int cur, end, spiltted, index;
706         int err = 0;
707
708         /* register locked file pages as online pages in pack */
709         z_erofs_onlinepage_init(page);
710
711         spiltted = 0;
712         end = PAGE_SIZE;
713 repeat:
714         cur = end - 1;
715
716         /* lucky, within the range of the current map_blocks */
717         if (offset + cur >= map->m_la &&
718                 offset + cur < map->m_la + map->m_llen) {
719                 /* didn't get a valid unzip work previously (very rare) */
720                 if (!builder->work)
721                         goto restart_now;
722                 goto hitted;
723         }
724
725         /* go ahead the next map_blocks */
726         debugln("%s: [out-of-range] pos %llu", __func__, offset + cur);
727
728         if (z_erofs_vle_work_iter_end(builder))
729                 fe->backmost = false;
730
731         map->m_la = offset + cur;
732         map->m_llen = 0;
733         err = z_erofs_map_blocks_iter(fe->inode, map, 0);
734         if (unlikely(err))
735                 goto err_out;
736
737 restart_now:
738         if (unlikely(!(map->m_flags & EROFS_MAP_MAPPED)))
739                 goto hitted;
740
741         DBG_BUGON(map->m_plen != 1 << sbi->clusterbits);
742         DBG_BUGON(erofs_blkoff(map->m_pa));
743
744         err = z_erofs_vle_work_iter_begin(builder, sb, map, &fe->owned_head);
745         if (unlikely(err))
746                 goto err_out;
747
748         /* preload all compressed pages (maybe downgrade role if necessary) */
749         if (should_alloc_managed_pages(fe, map->m_la))
750                 cache_strategy = DELAYEDALLOC;
751         else
752                 cache_strategy = DONTALLOC;
753
754         preload_compressed_pages(builder, MNGD_MAPPING(sbi),
755                                  map->m_pa / PAGE_SIZE,
756                                  map->m_plen / PAGE_SIZE,
757                                  cache_strategy, page_pool, GFP_KERNEL);
758
759         tight &= builder_is_hooked(builder);
760         work = builder->work;
761 hitted:
762         cur = end - min_t(unsigned int, offset + end - map->m_la, end);
763         if (unlikely(!(map->m_flags & EROFS_MAP_MAPPED))) {
764                 zero_user_segment(page, cur, end);
765                 goto next_part;
766         }
767
768         /* let's derive page type */
769         page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
770                 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
771                         (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
772                                 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
773
774         if (cur)
775                 tight &= builder_is_followed(builder);
776
777 retry:
778         err = z_erofs_vle_work_add_page(builder, page, page_type);
779         /* should allocate an additional staging page for pagevec */
780         if (err == -EAGAIN) {
781                 struct page *const newpage =
782                         __stagingpage_alloc(page_pool, GFP_NOFS);
783
784                 err = z_erofs_vle_work_add_page(builder,
785                         newpage, Z_EROFS_PAGE_TYPE_EXCLUSIVE);
786                 if (likely(!err))
787                         goto retry;
788         }
789
790         if (unlikely(err))
791                 goto err_out;
792
793         index = page->index - map->m_la / PAGE_SIZE;
794
795         /* FIXME! avoid the last relundant fixup & endio */
796         z_erofs_onlinepage_fixup(page, index, true);
797
798         /* bump up the number of spiltted parts of a page */
799         ++spiltted;
800         /* also update nr_pages */
801         work->nr_pages = max_t(pgoff_t, work->nr_pages, index + 1);
802 next_part:
803         /* can be used for verification */
804         map->m_llen = offset + cur - map->m_la;
805
806         end = cur;
807         if (end > 0)
808                 goto repeat;
809
810 out:
811         /* FIXME! avoid the last relundant fixup & endio */
812         z_erofs_onlinepage_endio(page);
813
814         debugln("%s, finish page: %pK spiltted: %u map->m_llen %llu",
815                 __func__, page, spiltted, map->m_llen);
816         return err;
817
818         /* if some error occurred while processing this page */
819 err_out:
820         SetPageError(page);
821         goto out;
822 }
823
824 static void z_erofs_vle_unzip_kickoff(void *ptr, int bios)
825 {
826         tagptr1_t t = tagptr_init(tagptr1_t, ptr);
827         struct z_erofs_vle_unzip_io *io = tagptr_unfold_ptr(t);
828         bool background = tagptr_unfold_tags(t);
829
830         if (!background) {
831                 unsigned long flags;
832
833                 spin_lock_irqsave(&io->u.wait.lock, flags);
834                 if (!atomic_add_return(bios, &io->pending_bios))
835                         wake_up_locked(&io->u.wait);
836                 spin_unlock_irqrestore(&io->u.wait.lock, flags);
837                 return;
838         }
839
840         if (!atomic_add_return(bios, &io->pending_bios))
841                 queue_work(z_erofs_workqueue, &io->u.work);
842 }
843
844 static inline void z_erofs_vle_read_endio(struct bio *bio)
845 {
846         const blk_status_t err = bio->bi_status;
847         unsigned int i;
848         struct bio_vec *bvec;
849 #ifdef EROFS_FS_HAS_MANAGED_CACHE
850         struct address_space *mc = NULL;
851 #endif
852         struct bvec_iter_all iter_all;
853
854         bio_for_each_segment_all(bvec, bio, i, iter_all) {
855                 struct page *page = bvec->bv_page;
856                 bool cachemngd = false;
857
858                 DBG_BUGON(PageUptodate(page));
859                 DBG_BUGON(!page->mapping);
860
861 #ifdef EROFS_FS_HAS_MANAGED_CACHE
862                 if (unlikely(!mc && !z_erofs_is_stagingpage(page))) {
863                         struct inode *const inode = page->mapping->host;
864                         struct super_block *const sb = inode->i_sb;
865
866                         mc = MNGD_MAPPING(EROFS_SB(sb));
867                 }
868
869                 /*
870                  * If mc has not gotten, it equals NULL,
871                  * however, page->mapping never be NULL if working properly.
872                  */
873                 cachemngd = (page->mapping == mc);
874 #endif
875
876                 if (unlikely(err))
877                         SetPageError(page);
878                 else if (cachemngd)
879                         SetPageUptodate(page);
880
881                 if (cachemngd)
882                         unlock_page(page);
883         }
884
885         z_erofs_vle_unzip_kickoff(bio->bi_private, -1);
886         bio_put(bio);
887 }
888
889 static struct page *z_pagemap_global[Z_EROFS_VLE_VMAP_GLOBAL_PAGES];
890 static DEFINE_MUTEX(z_pagemap_global_lock);
891
892 static int z_erofs_vle_unzip(struct super_block *sb,
893         struct z_erofs_vle_workgroup *grp,
894         struct list_head *page_pool)
895 {
896         struct erofs_sb_info *const sbi = EROFS_SB(sb);
897         const unsigned int clusterpages = erofs_clusterpages(sbi);
898
899         struct z_erofs_pagevec_ctor ctor;
900         unsigned int nr_pages;
901         unsigned int sparsemem_pages = 0;
902         struct page *pages_onstack[Z_EROFS_VLE_VMAP_ONSTACK_PAGES];
903         struct page **pages, **compressed_pages, *page;
904         unsigned int i, llen;
905
906         enum z_erofs_page_type page_type;
907         bool overlapped;
908         struct z_erofs_vle_work *work;
909         void *vout;
910         int err;
911
912         might_sleep();
913         work = z_erofs_vle_grab_primary_work(grp);
914         DBG_BUGON(!READ_ONCE(work->nr_pages));
915
916         mutex_lock(&work->lock);
917         nr_pages = work->nr_pages;
918
919         if (likely(nr_pages <= Z_EROFS_VLE_VMAP_ONSTACK_PAGES))
920                 pages = pages_onstack;
921         else if (nr_pages <= Z_EROFS_VLE_VMAP_GLOBAL_PAGES &&
922                 mutex_trylock(&z_pagemap_global_lock))
923                 pages = z_pagemap_global;
924         else {
925 repeat:
926                 pages = kvmalloc_array(nr_pages,
927                         sizeof(struct page *), GFP_KERNEL);
928
929                 /* fallback to global pagemap for the lowmem scenario */
930                 if (unlikely(!pages)) {
931                         if (nr_pages > Z_EROFS_VLE_VMAP_GLOBAL_PAGES)
932                                 goto repeat;
933                         else {
934                                 mutex_lock(&z_pagemap_global_lock);
935                                 pages = z_pagemap_global;
936                         }
937                 }
938         }
939
940         for (i = 0; i < nr_pages; ++i)
941                 pages[i] = NULL;
942
943         z_erofs_pagevec_ctor_init(&ctor,
944                 Z_EROFS_VLE_INLINE_PAGEVECS, work->pagevec, 0);
945
946         for (i = 0; i < work->vcnt; ++i) {
947                 unsigned int pagenr;
948
949                 page = z_erofs_pagevec_ctor_dequeue(&ctor, &page_type);
950
951                 /* all pages in pagevec ought to be valid */
952                 DBG_BUGON(!page);
953                 DBG_BUGON(!page->mapping);
954
955                 if (z_erofs_gather_if_stagingpage(page_pool, page))
956                         continue;
957
958                 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
959                         pagenr = 0;
960                 else
961                         pagenr = z_erofs_onlinepage_index(page);
962
963                 DBG_BUGON(pagenr >= nr_pages);
964                 DBG_BUGON(pages[pagenr]);
965
966                 pages[pagenr] = page;
967         }
968         sparsemem_pages = i;
969
970         z_erofs_pagevec_ctor_exit(&ctor, true);
971
972         overlapped = false;
973         compressed_pages = grp->compressed_pages;
974
975         err = 0;
976         for (i = 0; i < clusterpages; ++i) {
977                 unsigned int pagenr;
978
979                 page = compressed_pages[i];
980
981                 /* all compressed pages ought to be valid */
982                 DBG_BUGON(!page);
983                 DBG_BUGON(!page->mapping);
984
985                 if (!z_erofs_is_stagingpage(page)) {
986 #ifdef EROFS_FS_HAS_MANAGED_CACHE
987                         if (page->mapping == MNGD_MAPPING(sbi)) {
988                                 if (unlikely(!PageUptodate(page)))
989                                         err = -EIO;
990                                 continue;
991                         }
992 #endif
993
994                         /*
995                          * only if non-head page can be selected
996                          * for inplace decompression
997                          */
998                         pagenr = z_erofs_onlinepage_index(page);
999
1000                         DBG_BUGON(pagenr >= nr_pages);
1001                         DBG_BUGON(pages[pagenr]);
1002                         ++sparsemem_pages;
1003                         pages[pagenr] = page;
1004
1005                         overlapped = true;
1006                 }
1007
1008                 /* PG_error needs checking for inplaced and staging pages */
1009                 if (unlikely(PageError(page))) {
1010                         DBG_BUGON(PageUptodate(page));
1011                         err = -EIO;
1012                 }
1013         }
1014
1015         if (unlikely(err))
1016                 goto out;
1017
1018         llen = (nr_pages << PAGE_SHIFT) - work->pageofs;
1019
1020         if (z_erofs_vle_workgrp_fmt(grp) == Z_EROFS_VLE_WORKGRP_FMT_PLAIN) {
1021                 err = z_erofs_vle_plain_copy(compressed_pages, clusterpages,
1022                         pages, nr_pages, work->pageofs);
1023                 goto out;
1024         }
1025
1026         if (llen > grp->llen)
1027                 llen = grp->llen;
1028
1029         err = z_erofs_vle_unzip_fast_percpu(compressed_pages, clusterpages,
1030                                             pages, llen, work->pageofs);
1031         if (err != -ENOTSUPP)
1032                 goto out;
1033
1034         if (sparsemem_pages >= nr_pages)
1035                 goto skip_allocpage;
1036
1037         for (i = 0; i < nr_pages; ++i) {
1038                 if (pages[i])
1039                         continue;
1040
1041                 pages[i] = __stagingpage_alloc(page_pool, GFP_NOFS);
1042         }
1043
1044 skip_allocpage:
1045         vout = erofs_vmap(pages, nr_pages);
1046         if (!vout) {
1047                 err = -ENOMEM;
1048                 goto out;
1049         }
1050
1051         err = z_erofs_vle_unzip_vmap(compressed_pages,
1052                 clusterpages, vout, llen, work->pageofs, overlapped);
1053
1054         erofs_vunmap(vout, nr_pages);
1055
1056 out:
1057         /* must handle all compressed pages before endding pages */
1058         for (i = 0; i < clusterpages; ++i) {
1059                 page = compressed_pages[i];
1060
1061 #ifdef EROFS_FS_HAS_MANAGED_CACHE
1062                 if (page->mapping == MNGD_MAPPING(sbi))
1063                         continue;
1064 #endif
1065                 /* recycle all individual staging pages */
1066                 (void)z_erofs_gather_if_stagingpage(page_pool, page);
1067
1068                 WRITE_ONCE(compressed_pages[i], NULL);
1069         }
1070
1071         for (i = 0; i < nr_pages; ++i) {
1072                 page = pages[i];
1073                 if (!page)
1074                         continue;
1075
1076                 DBG_BUGON(!page->mapping);
1077
1078                 /* recycle all individual staging pages */
1079                 if (z_erofs_gather_if_stagingpage(page_pool, page))
1080                         continue;
1081
1082                 if (unlikely(err < 0))
1083                         SetPageError(page);
1084
1085                 z_erofs_onlinepage_endio(page);
1086         }
1087
1088         if (pages == z_pagemap_global)
1089                 mutex_unlock(&z_pagemap_global_lock);
1090         else if (unlikely(pages != pages_onstack))
1091                 kvfree(pages);
1092
1093         work->nr_pages = 0;
1094         work->vcnt = 0;
1095
1096         /* all work locks MUST be taken before the following line */
1097
1098         WRITE_ONCE(grp->next, Z_EROFS_VLE_WORKGRP_NIL);
1099
1100         /* all work locks SHOULD be released right now */
1101         mutex_unlock(&work->lock);
1102
1103         z_erofs_vle_work_release(work);
1104         return err;
1105 }
1106
1107 static void z_erofs_vle_unzip_all(struct super_block *sb,
1108                                   struct z_erofs_vle_unzip_io *io,
1109                                   struct list_head *page_pool)
1110 {
1111         z_erofs_vle_owned_workgrp_t owned = io->head;
1112
1113         while (owned != Z_EROFS_VLE_WORKGRP_TAIL_CLOSED) {
1114                 struct z_erofs_vle_workgroup *grp;
1115
1116                 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1117                 DBG_BUGON(owned == Z_EROFS_VLE_WORKGRP_TAIL);
1118
1119                 /* no possible that 'owned' equals NULL */
1120                 DBG_BUGON(owned == Z_EROFS_VLE_WORKGRP_NIL);
1121
1122                 grp = container_of(owned, struct z_erofs_vle_workgroup, next);
1123                 owned = READ_ONCE(grp->next);
1124
1125                 z_erofs_vle_unzip(sb, grp, page_pool);
1126         }
1127 }
1128
1129 static void z_erofs_vle_unzip_wq(struct work_struct *work)
1130 {
1131         struct z_erofs_vle_unzip_io_sb *iosb = container_of(work,
1132                 struct z_erofs_vle_unzip_io_sb, io.u.work);
1133         LIST_HEAD(page_pool);
1134
1135         DBG_BUGON(iosb->io.head == Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1136         z_erofs_vle_unzip_all(iosb->sb, &iosb->io, &page_pool);
1137
1138         put_pages_list(&page_pool);
1139         kvfree(iosb);
1140 }
1141
1142 static struct page *
1143 pickup_page_for_submission(struct z_erofs_vle_workgroup *grp,
1144                            unsigned int nr,
1145                            struct list_head *pagepool,
1146                            struct address_space *mc,
1147                            gfp_t gfp)
1148 {
1149         /* determined at compile time to avoid too many #ifdefs */
1150         const bool nocache = __builtin_constant_p(mc) ? !mc : false;
1151         const pgoff_t index = grp->obj.index;
1152         bool tocache = false;
1153
1154         struct address_space *mapping;
1155         struct page *oldpage, *page;
1156
1157         compressed_page_t t;
1158         int justfound;
1159
1160 repeat:
1161         page = READ_ONCE(grp->compressed_pages[nr]);
1162         oldpage = page;
1163
1164         if (!page)
1165                 goto out_allocpage;
1166
1167         /*
1168          * the cached page has not been allocated and
1169          * an placeholder is out there, prepare it now.
1170          */
1171         if (!nocache && page == PAGE_UNALLOCATED) {
1172                 tocache = true;
1173                 goto out_allocpage;
1174         }
1175
1176         /* process the target tagged pointer */
1177         t = tagptr_init(compressed_page_t, page);
1178         justfound = tagptr_unfold_tags(t);
1179         page = tagptr_unfold_ptr(t);
1180
1181         mapping = READ_ONCE(page->mapping);
1182
1183         /*
1184          * if managed cache is disabled, it's no way to
1185          * get such a cached-like page.
1186          */
1187         if (nocache) {
1188                 /* if managed cache is disabled, it is impossible `justfound' */
1189                 DBG_BUGON(justfound);
1190
1191                 /* and it should be locked, not uptodate, and not truncated */
1192                 DBG_BUGON(!PageLocked(page));
1193                 DBG_BUGON(PageUptodate(page));
1194                 DBG_BUGON(!mapping);
1195                 goto out;
1196         }
1197
1198         /*
1199          * unmanaged (file) pages are all locked solidly,
1200          * therefore it is impossible for `mapping' to be NULL.
1201          */
1202         if (mapping && mapping != mc)
1203                 /* ought to be unmanaged pages */
1204                 goto out;
1205
1206         lock_page(page);
1207
1208         /* only true if page reclaim goes wrong, should never happen */
1209         DBG_BUGON(justfound && PagePrivate(page));
1210
1211         /* the page is still in manage cache */
1212         if (page->mapping == mc) {
1213                 WRITE_ONCE(grp->compressed_pages[nr], page);
1214
1215                 ClearPageError(page);
1216                 if (!PagePrivate(page)) {
1217                         /*
1218                          * impossible to be !PagePrivate(page) for
1219                          * the current restriction as well if
1220                          * the page is already in compressed_pages[].
1221                          */
1222                         DBG_BUGON(!justfound);
1223
1224                         justfound = 0;
1225                         set_page_private(page, (unsigned long)grp);
1226                         SetPagePrivate(page);
1227                 }
1228
1229                 /* no need to submit io if it is already up-to-date */
1230                 if (PageUptodate(page)) {
1231                         unlock_page(page);
1232                         page = NULL;
1233                 }
1234                 goto out;
1235         }
1236
1237         /*
1238          * the managed page has been truncated, it's unsafe to
1239          * reuse this one, let's allocate a new cache-managed page.
1240          */
1241         DBG_BUGON(page->mapping);
1242         DBG_BUGON(!justfound);
1243
1244         tocache = true;
1245         unlock_page(page);
1246         put_page(page);
1247 out_allocpage:
1248         page = __stagingpage_alloc(pagepool, gfp);
1249         if (oldpage != cmpxchg(&grp->compressed_pages[nr], oldpage, page)) {
1250                 list_add(&page->lru, pagepool);
1251                 cpu_relax();
1252                 goto repeat;
1253         }
1254         if (nocache || !tocache)
1255                 goto out;
1256         if (add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1257                 page->mapping = Z_EROFS_MAPPING_STAGING;
1258                 goto out;
1259         }
1260
1261         set_page_private(page, (unsigned long)grp);
1262         SetPagePrivate(page);
1263 out:    /* the only exit (for tracing and debugging) */
1264         return page;
1265 }
1266
1267 static struct z_erofs_vle_unzip_io *
1268 jobqueue_init(struct super_block *sb,
1269               struct z_erofs_vle_unzip_io *io,
1270               bool foreground)
1271 {
1272         struct z_erofs_vle_unzip_io_sb *iosb;
1273
1274         if (foreground) {
1275                 /* waitqueue available for foreground io */
1276                 DBG_BUGON(!io);
1277
1278                 init_waitqueue_head(&io->u.wait);
1279                 atomic_set(&io->pending_bios, 0);
1280                 goto out;
1281         }
1282
1283         iosb = kvzalloc(sizeof(struct z_erofs_vle_unzip_io_sb),
1284                         GFP_KERNEL | __GFP_NOFAIL);
1285         DBG_BUGON(!iosb);
1286
1287         /* initialize fields in the allocated descriptor */
1288         io = &iosb->io;
1289         iosb->sb = sb;
1290         INIT_WORK(&io->u.work, z_erofs_vle_unzip_wq);
1291 out:
1292         io->head = Z_EROFS_VLE_WORKGRP_TAIL_CLOSED;
1293         return io;
1294 }
1295
1296 /* define workgroup jobqueue types */
1297 enum {
1298 #ifdef EROFS_FS_HAS_MANAGED_CACHE
1299         JQ_BYPASS,
1300 #endif
1301         JQ_SUBMIT,
1302         NR_JOBQUEUES,
1303 };
1304
1305 static void *jobqueueset_init(struct super_block *sb,
1306                               z_erofs_vle_owned_workgrp_t qtail[],
1307                               struct z_erofs_vle_unzip_io *q[],
1308                               struct z_erofs_vle_unzip_io *fgq,
1309                               bool forcefg)
1310 {
1311 #ifdef EROFS_FS_HAS_MANAGED_CACHE
1312         /*
1313          * if managed cache is enabled, bypass jobqueue is needed,
1314          * no need to read from device for all workgroups in this queue.
1315          */
1316         q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, true);
1317         qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1318 #endif
1319
1320         q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, forcefg);
1321         qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1322
1323         return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], !forcefg));
1324 }
1325
1326 #ifdef EROFS_FS_HAS_MANAGED_CACHE
1327 static void move_to_bypass_jobqueue(struct z_erofs_vle_workgroup *grp,
1328                                     z_erofs_vle_owned_workgrp_t qtail[],
1329                                     z_erofs_vle_owned_workgrp_t owned_head)
1330 {
1331         z_erofs_vle_owned_workgrp_t *const submit_qtail = qtail[JQ_SUBMIT];
1332         z_erofs_vle_owned_workgrp_t *const bypass_qtail = qtail[JQ_BYPASS];
1333
1334         DBG_BUGON(owned_head == Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1335         if (owned_head == Z_EROFS_VLE_WORKGRP_TAIL)
1336                 owned_head = Z_EROFS_VLE_WORKGRP_TAIL_CLOSED;
1337
1338         WRITE_ONCE(grp->next, Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1339
1340         WRITE_ONCE(*submit_qtail, owned_head);
1341         WRITE_ONCE(*bypass_qtail, &grp->next);
1342
1343         qtail[JQ_BYPASS] = &grp->next;
1344 }
1345
1346 static bool postsubmit_is_all_bypassed(struct z_erofs_vle_unzip_io *q[],
1347                                        unsigned int nr_bios,
1348                                        bool force_fg)
1349 {
1350         /*
1351          * although background is preferred, no one is pending for submission.
1352          * don't issue workqueue for decompression but drop it directly instead.
1353          */
1354         if (force_fg || nr_bios)
1355                 return false;
1356
1357         kvfree(container_of(q[JQ_SUBMIT],
1358                             struct z_erofs_vle_unzip_io_sb,
1359                             io));
1360         return true;
1361 }
1362 #else
1363 static void move_to_bypass_jobqueue(struct z_erofs_vle_workgroup *grp,
1364                                     z_erofs_vle_owned_workgrp_t qtail[],
1365                                     z_erofs_vle_owned_workgrp_t owned_head)
1366 {
1367         /* impossible to bypass submission for managed cache disabled */
1368         DBG_BUGON(1);
1369 }
1370
1371 static bool postsubmit_is_all_bypassed(struct z_erofs_vle_unzip_io *q[],
1372                                        unsigned int nr_bios,
1373                                        bool force_fg)
1374 {
1375         /* bios should be >0 if managed cache is disabled */
1376         DBG_BUGON(!nr_bios);
1377         return false;
1378 }
1379 #endif
1380
1381 static bool z_erofs_vle_submit_all(struct super_block *sb,
1382                                    z_erofs_vle_owned_workgrp_t owned_head,
1383                                    struct list_head *pagepool,
1384                                    struct z_erofs_vle_unzip_io *fgq,
1385                                    bool force_fg)
1386 {
1387         struct erofs_sb_info *const sbi = EROFS_SB(sb);
1388         const unsigned int clusterpages = erofs_clusterpages(sbi);
1389         const gfp_t gfp = GFP_NOFS;
1390
1391         z_erofs_vle_owned_workgrp_t qtail[NR_JOBQUEUES];
1392         struct z_erofs_vle_unzip_io *q[NR_JOBQUEUES];
1393         struct bio *bio;
1394         void *bi_private;
1395         /* since bio will be NULL, no need to initialize last_index */
1396         pgoff_t uninitialized_var(last_index);
1397         bool force_submit = false;
1398         unsigned int nr_bios;
1399
1400         if (unlikely(owned_head == Z_EROFS_VLE_WORKGRP_TAIL))
1401                 return false;
1402
1403         force_submit = false;
1404         bio = NULL;
1405         nr_bios = 0;
1406         bi_private = jobqueueset_init(sb, qtail, q, fgq, force_fg);
1407
1408         /* by default, all need io submission */
1409         q[JQ_SUBMIT]->head = owned_head;
1410
1411         do {
1412                 struct z_erofs_vle_workgroup *grp;
1413                 pgoff_t first_index;
1414                 struct page *page;
1415                 unsigned int i = 0, bypass = 0;
1416                 int err;
1417
1418                 /* no possible 'owned_head' equals the following */
1419                 DBG_BUGON(owned_head == Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1420                 DBG_BUGON(owned_head == Z_EROFS_VLE_WORKGRP_NIL);
1421
1422                 grp = container_of(owned_head,
1423                                    struct z_erofs_vle_workgroup, next);
1424
1425                 /* close the main owned chain at first */
1426                 owned_head = cmpxchg(&grp->next, Z_EROFS_VLE_WORKGRP_TAIL,
1427                                      Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1428
1429                 first_index = grp->obj.index;
1430                 force_submit |= (first_index != last_index + 1);
1431
1432 repeat:
1433                 page = pickup_page_for_submission(grp, i, pagepool,
1434                                                   MNGD_MAPPING(sbi), gfp);
1435                 if (!page) {
1436                         force_submit = true;
1437                         ++bypass;
1438                         goto skippage;
1439                 }
1440
1441                 if (bio && force_submit) {
1442 submit_bio_retry:
1443                         __submit_bio(bio, REQ_OP_READ, 0);
1444                         bio = NULL;
1445                 }
1446
1447                 if (!bio) {
1448                         bio = erofs_grab_bio(sb, first_index + i,
1449                                              BIO_MAX_PAGES,
1450                                              z_erofs_vle_read_endio, true);
1451                         bio->bi_private = bi_private;
1452
1453                         ++nr_bios;
1454                 }
1455
1456                 err = bio_add_page(bio, page, PAGE_SIZE, 0);
1457                 if (err < PAGE_SIZE)
1458                         goto submit_bio_retry;
1459
1460                 force_submit = false;
1461                 last_index = first_index + i;
1462 skippage:
1463                 if (++i < clusterpages)
1464                         goto repeat;
1465
1466                 if (bypass < clusterpages)
1467                         qtail[JQ_SUBMIT] = &grp->next;
1468                 else
1469                         move_to_bypass_jobqueue(grp, qtail, owned_head);
1470         } while (owned_head != Z_EROFS_VLE_WORKGRP_TAIL);
1471
1472         if (bio)
1473                 __submit_bio(bio, REQ_OP_READ, 0);
1474
1475         if (postsubmit_is_all_bypassed(q, nr_bios, force_fg))
1476                 return true;
1477
1478         z_erofs_vle_unzip_kickoff(bi_private, nr_bios);
1479         return true;
1480 }
1481
1482 static void z_erofs_submit_and_unzip(struct z_erofs_vle_frontend *f,
1483                                      struct list_head *pagepool,
1484                                      bool force_fg)
1485 {
1486         struct super_block *sb = f->inode->i_sb;
1487         struct z_erofs_vle_unzip_io io[NR_JOBQUEUES];
1488
1489         if (!z_erofs_vle_submit_all(sb, f->owned_head, pagepool, io, force_fg))
1490                 return;
1491
1492 #ifdef EROFS_FS_HAS_MANAGED_CACHE
1493         z_erofs_vle_unzip_all(sb, &io[JQ_BYPASS], pagepool);
1494 #endif
1495         if (!force_fg)
1496                 return;
1497
1498         /* wait until all bios are completed */
1499         wait_event(io[JQ_SUBMIT].u.wait,
1500                    !atomic_read(&io[JQ_SUBMIT].pending_bios));
1501
1502         /* let's synchronous decompression */
1503         z_erofs_vle_unzip_all(sb, &io[JQ_SUBMIT], pagepool);
1504 }
1505
1506 static int z_erofs_vle_normalaccess_readpage(struct file *file,
1507                                              struct page *page)
1508 {
1509         struct inode *const inode = page->mapping->host;
1510         struct z_erofs_vle_frontend f = VLE_FRONTEND_INIT(inode);
1511         int err;
1512         LIST_HEAD(pagepool);
1513
1514         trace_erofs_readpage(page, false);
1515
1516         f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1517
1518         err = z_erofs_do_read_page(&f, page, &pagepool);
1519         (void)z_erofs_vle_work_iter_end(&f.builder);
1520
1521         if (err) {
1522                 errln("%s, failed to read, err [%d]", __func__, err);
1523                 goto out;
1524         }
1525
1526         z_erofs_submit_and_unzip(&f, &pagepool, true);
1527 out:
1528         if (f.map.mpage)
1529                 put_page(f.map.mpage);
1530
1531         /* clean up the remaining free pages */
1532         put_pages_list(&pagepool);
1533         return 0;
1534 }
1535
1536 static int z_erofs_vle_normalaccess_readpages(struct file *filp,
1537                                               struct address_space *mapping,
1538                                               struct list_head *pages,
1539                                               unsigned int nr_pages)
1540 {
1541         struct inode *const inode = mapping->host;
1542         struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1543
1544         bool sync = __should_decompress_synchronously(sbi, nr_pages);
1545         struct z_erofs_vle_frontend f = VLE_FRONTEND_INIT(inode);
1546         gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
1547         struct page *head = NULL;
1548         LIST_HEAD(pagepool);
1549
1550         trace_erofs_readpages(mapping->host, lru_to_page(pages),
1551                               nr_pages, false);
1552
1553         f.headoffset = (erofs_off_t)lru_to_page(pages)->index << PAGE_SHIFT;
1554
1555         for (; nr_pages; --nr_pages) {
1556                 struct page *page = lru_to_page(pages);
1557
1558                 prefetchw(&page->flags);
1559                 list_del(&page->lru);
1560
1561                 /*
1562                  * A pure asynchronous readahead is indicated if
1563                  * a PG_readahead marked page is hitted at first.
1564                  * Let's also do asynchronous decompression for this case.
1565                  */
1566                 sync &= !(PageReadahead(page) && !head);
1567
1568                 if (add_to_page_cache_lru(page, mapping, page->index, gfp)) {
1569                         list_add(&page->lru, &pagepool);
1570                         continue;
1571                 }
1572
1573                 set_page_private(page, (unsigned long)head);
1574                 head = page;
1575         }
1576
1577         while (head) {
1578                 struct page *page = head;
1579                 int err;
1580
1581                 /* traversal in reverse order */
1582                 head = (void *)page_private(page);
1583
1584                 err = z_erofs_do_read_page(&f, page, &pagepool);
1585                 if (err) {
1586                         struct erofs_vnode *vi = EROFS_V(inode);
1587
1588                         errln("%s, readahead error at page %lu of nid %llu",
1589                                 __func__, page->index, vi->nid);
1590                 }
1591
1592                 put_page(page);
1593         }
1594
1595         (void)z_erofs_vle_work_iter_end(&f.builder);
1596
1597         z_erofs_submit_and_unzip(&f, &pagepool, sync);
1598
1599         if (f.map.mpage)
1600                 put_page(f.map.mpage);
1601
1602         /* clean up the remaining free pages */
1603         put_pages_list(&pagepool);
1604         return 0;
1605 }
1606
1607 const struct address_space_operations z_erofs_vle_normalaccess_aops = {
1608         .readpage = z_erofs_vle_normalaccess_readpage,
1609         .readpages = z_erofs_vle_normalaccess_readpages,
1610 };
1611
1612 /*
1613  * Variable-sized Logical Extent (Fixed Physical Cluster) Compression Mode
1614  * ---
1615  * VLE compression mode attempts to compress a number of logical data into
1616  * a physical cluster with a fixed size.
1617  * VLE compression mode uses "struct z_erofs_vle_decompressed_index".
1618  */
1619 #define __vle_cluster_advise(x, bit, bits) \
1620         ((le16_to_cpu(x) >> (bit)) & ((1 << (bits)) - 1))
1621
1622 #define __vle_cluster_type(advise) __vle_cluster_advise(advise, \
1623         Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT, Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS)
1624
1625 #define vle_cluster_type(di)    \
1626         __vle_cluster_type((di)->di_advise)
1627
1628 static int
1629 vle_decompressed_index_clusterofs(unsigned int *clusterofs,
1630                                   unsigned int clustersize,
1631                                   struct z_erofs_vle_decompressed_index *di)
1632 {
1633         switch (vle_cluster_type(di)) {
1634         case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
1635                 *clusterofs = clustersize;
1636                 break;
1637         case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
1638         case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
1639                 *clusterofs = le16_to_cpu(di->di_clusterofs);
1640                 break;
1641         default:
1642                 DBG_BUGON(1);
1643                 return -EIO;
1644         }
1645         return 0;
1646 }
1647
1648 static inline erofs_blk_t
1649 vle_extent_blkaddr(struct inode *inode, pgoff_t index)
1650 {
1651         struct erofs_sb_info *sbi = EROFS_I_SB(inode);
1652         struct erofs_vnode *vi = EROFS_V(inode);
1653
1654         unsigned int ofs = Z_EROFS_VLE_EXTENT_ALIGN(vi->inode_isize +
1655                 vi->xattr_isize) + sizeof(struct erofs_extent_header) +
1656                 index * sizeof(struct z_erofs_vle_decompressed_index);
1657
1658         return erofs_blknr(iloc(sbi, vi->nid) + ofs);
1659 }
1660
1661 static inline unsigned int
1662 vle_extent_blkoff(struct inode *inode, pgoff_t index)
1663 {
1664         struct erofs_sb_info *sbi = EROFS_I_SB(inode);
1665         struct erofs_vnode *vi = EROFS_V(inode);
1666
1667         unsigned int ofs = Z_EROFS_VLE_EXTENT_ALIGN(vi->inode_isize +
1668                 vi->xattr_isize) + sizeof(struct erofs_extent_header) +
1669                 index * sizeof(struct z_erofs_vle_decompressed_index);
1670
1671         return erofs_blkoff(iloc(sbi, vi->nid) + ofs);
1672 }
1673
1674 struct vle_map_blocks_iter_ctx {
1675         struct inode *inode;
1676         struct super_block *sb;
1677         unsigned int clusterbits;
1678
1679         struct page **mpage_ret;
1680         void **kaddr_ret;
1681 };
1682
1683 static int
1684 vle_get_logical_extent_head(const struct vle_map_blocks_iter_ctx *ctx,
1685                             unsigned int lcn,   /* logical cluster number */
1686                             unsigned long long *ofs,
1687                             erofs_blk_t *pblk,
1688                             unsigned int *flags)
1689 {
1690         const unsigned int clustersize = 1 << ctx->clusterbits;
1691         const erofs_blk_t mblk = vle_extent_blkaddr(ctx->inode, lcn);
1692         struct page *mpage = *ctx->mpage_ret;   /* extent metapage */
1693
1694         struct z_erofs_vle_decompressed_index *di;
1695         unsigned int cluster_type, delta0;
1696
1697         if (mpage->index != mblk) {
1698                 kunmap_atomic(*ctx->kaddr_ret);
1699                 unlock_page(mpage);
1700                 put_page(mpage);
1701
1702                 mpage = erofs_get_meta_page(ctx->sb, mblk, false);
1703                 if (IS_ERR(mpage)) {
1704                         *ctx->mpage_ret = NULL;
1705                         return PTR_ERR(mpage);
1706                 }
1707                 *ctx->mpage_ret = mpage;
1708                 *ctx->kaddr_ret = kmap_atomic(mpage);
1709         }
1710
1711         di = *ctx->kaddr_ret + vle_extent_blkoff(ctx->inode, lcn);
1712
1713         cluster_type = vle_cluster_type(di);
1714         switch (cluster_type) {
1715         case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
1716                 delta0 = le16_to_cpu(di->di_u.delta[0]);
1717                 if (unlikely(!delta0 || delta0 > lcn)) {
1718                         errln("invalid NONHEAD dl0 %u at lcn %u of nid %llu",
1719                               delta0, lcn, EROFS_V(ctx->inode)->nid);
1720                         DBG_BUGON(1);
1721                         return -EIO;
1722                 }
1723                 return vle_get_logical_extent_head(ctx,
1724                         lcn - delta0, ofs, pblk, flags);
1725         case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
1726                 *flags ^= EROFS_MAP_ZIPPED;
1727                 /* fallthrough */
1728         case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
1729                 /* clustersize should be a power of two */
1730                 *ofs = ((u64)lcn << ctx->clusterbits) +
1731                         (le16_to_cpu(di->di_clusterofs) & (clustersize - 1));
1732                 *pblk = le32_to_cpu(di->di_u.blkaddr);
1733                 break;
1734         default:
1735                 errln("unknown cluster type %u at lcn %u of nid %llu",
1736                       cluster_type, lcn, EROFS_V(ctx->inode)->nid);
1737                 DBG_BUGON(1);
1738                 return -EIO;
1739         }
1740         return 0;
1741 }
1742
1743 int z_erofs_map_blocks_iter(struct inode *inode,
1744         struct erofs_map_blocks *map,
1745         int flags)
1746 {
1747         void *kaddr;
1748         const struct vle_map_blocks_iter_ctx ctx = {
1749                 .inode = inode,
1750                 .sb = inode->i_sb,
1751                 .clusterbits = EROFS_I_SB(inode)->clusterbits,
1752                 .mpage_ret = &map->mpage,
1753                 .kaddr_ret = &kaddr
1754         };
1755         const unsigned int clustersize = 1 << ctx.clusterbits;
1756         /* if both m_(l,p)len are 0, regularize l_lblk, l_lofs, etc... */
1757         const bool initial = !map->m_llen;
1758
1759         /* logicial extent (start, end) offset */
1760         unsigned long long ofs, end;
1761         unsigned int lcn;
1762         u32 ofs_rem;
1763
1764         /* initialize `pblk' to keep gcc from printing foolish warnings */
1765         erofs_blk_t mblk, pblk = 0;
1766         struct page *mpage = map->mpage;
1767         struct z_erofs_vle_decompressed_index *di;
1768         unsigned int cluster_type, logical_cluster_ofs;
1769         int err = 0;
1770
1771         trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
1772
1773         /* when trying to read beyond EOF, leave it unmapped */
1774         if (unlikely(map->m_la >= inode->i_size)) {
1775                 DBG_BUGON(!initial);
1776                 map->m_llen = map->m_la + 1 - inode->i_size;
1777                 map->m_la = inode->i_size;
1778                 map->m_flags = 0;
1779                 goto out;
1780         }
1781
1782         debugln("%s, m_la %llu m_llen %llu --- start", __func__,
1783                 map->m_la, map->m_llen);
1784
1785         ofs = map->m_la + map->m_llen;
1786
1787         /* clustersize should be power of two */
1788         lcn = ofs >> ctx.clusterbits;
1789         ofs_rem = ofs & (clustersize - 1);
1790
1791         mblk = vle_extent_blkaddr(inode, lcn);
1792
1793         if (!mpage || mpage->index != mblk) {
1794                 if (mpage)
1795                         put_page(mpage);
1796
1797                 mpage = erofs_get_meta_page(ctx.sb, mblk, false);
1798                 if (IS_ERR(mpage)) {
1799                         err = PTR_ERR(mpage);
1800                         goto out;
1801                 }
1802                 map->mpage = mpage;
1803         } else {
1804                 lock_page(mpage);
1805                 DBG_BUGON(!PageUptodate(mpage));
1806         }
1807
1808         kaddr = kmap_atomic(mpage);
1809         di = kaddr + vle_extent_blkoff(inode, lcn);
1810
1811         debugln("%s, lcn %u mblk %u e_blkoff %u", __func__, lcn,
1812                 mblk, vle_extent_blkoff(inode, lcn));
1813
1814         err = vle_decompressed_index_clusterofs(&logical_cluster_ofs,
1815                                                 clustersize, di);
1816         if (unlikely(err))
1817                 goto unmap_out;
1818
1819         if (!initial) {
1820                 /* [walking mode] 'map' has been already initialized */
1821                 map->m_llen += logical_cluster_ofs;
1822                 goto unmap_out;
1823         }
1824
1825         /* by default, compressed */
1826         map->m_flags |= EROFS_MAP_ZIPPED;
1827
1828         end = ((u64)lcn + 1) * clustersize;
1829
1830         cluster_type = vle_cluster_type(di);
1831
1832         switch (cluster_type) {
1833         case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
1834                 if (ofs_rem >= logical_cluster_ofs)
1835                         map->m_flags ^= EROFS_MAP_ZIPPED;
1836                 /* fallthrough */
1837         case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
1838                 if (ofs_rem == logical_cluster_ofs) {
1839                         pblk = le32_to_cpu(di->di_u.blkaddr);
1840                         goto exact_hitted;
1841                 }
1842
1843                 if (ofs_rem > logical_cluster_ofs) {
1844                         ofs = (u64)lcn * clustersize | logical_cluster_ofs;
1845                         pblk = le32_to_cpu(di->di_u.blkaddr);
1846                         break;
1847                 }
1848
1849                 /* logical cluster number should be >= 1 */
1850                 if (unlikely(!lcn)) {
1851                         errln("invalid logical cluster 0 at nid %llu",
1852                                 EROFS_V(inode)->nid);
1853                         err = -EIO;
1854                         goto unmap_out;
1855                 }
1856                 end = ((u64)lcn-- * clustersize) | logical_cluster_ofs;
1857                 /* fallthrough */
1858         case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
1859                 /* get the correspoinding first chunk */
1860                 err = vle_get_logical_extent_head(&ctx, lcn, &ofs,
1861                                                   &pblk, &map->m_flags);
1862                 mpage = map->mpage;
1863
1864                 if (unlikely(err)) {
1865                         if (mpage)
1866                                 goto unmap_out;
1867                         goto out;
1868                 }
1869                 break;
1870         default:
1871                 errln("unknown cluster type %u at offset %llu of nid %llu",
1872                         cluster_type, ofs, EROFS_V(inode)->nid);
1873                 err = -EIO;
1874                 goto unmap_out;
1875         }
1876
1877         map->m_la = ofs;
1878 exact_hitted:
1879         map->m_llen = end - ofs;
1880         map->m_plen = clustersize;
1881         map->m_pa = blknr_to_addr(pblk);
1882         map->m_flags |= EROFS_MAP_MAPPED;
1883 unmap_out:
1884         kunmap_atomic(kaddr);
1885         unlock_page(mpage);
1886 out:
1887         debugln("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
1888                 __func__, map->m_la, map->m_pa,
1889                 map->m_llen, map->m_plen, map->m_flags);
1890
1891         trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
1892
1893         /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
1894         DBG_BUGON(err < 0 && err != -ENOMEM);
1895         return err;
1896 }
1897