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
z_erofs_exit_zip_subsystem(void)37 void z_erofs_exit_zip_subsystem(void)
38 {
39 destroy_workqueue(z_erofs_workqueue);
40 kmem_cache_destroy(pcluster_cachep);
41 }
42
z_erofs_init_workqueue(void)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
z_erofs_pcluster_init_once(void * ptr)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
z_erofs_init_zip_subsystem(void)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
preload_compressed_pages(struct z_erofs_collector * clt,struct address_space * mc,enum z_erofs_cache_alloctype type)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 */
erofs_try_to_free_all_cached_pages(struct erofs_sb_info * sbi,struct erofs_workgroup * grp)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
erofs_try_to_free_cached_page(struct address_space * mapping,struct page * page)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 */
z_erofs_try_inplace_io(struct z_erofs_collector * clt,struct page * page)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 */
z_erofs_attach_page(struct z_erofs_collector * clt,struct page * page,enum z_erofs_page_type type,bool pvec_safereuse)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
try_to_claim_pcluster(struct z_erofs_pcluster * pcl,z_erofs_next_pcluster_t * owned_head)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
z_erofs_lookup_collection(struct z_erofs_collector * clt,struct inode * inode,struct erofs_map_blocks * map)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
z_erofs_register_collection(struct z_erofs_collector * clt,struct inode * inode,struct erofs_map_blocks * map)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
z_erofs_collector_begin(struct z_erofs_collector * clt,struct inode * inode,struct erofs_map_blocks * map)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 */
z_erofs_rcu_callback(struct rcu_head * head)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
erofs_workgroup_free_rcu(struct erofs_workgroup * grp)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
z_erofs_collection_put(struct z_erofs_collection * cl)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
z_erofs_collector_end(struct z_erofs_collector * clt)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
should_alloc_managed_pages(struct z_erofs_decompress_frontend * fe,unsigned int cachestrategy,erofs_off_t la)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
z_erofs_do_read_page(struct z_erofs_decompress_frontend * fe,struct page * page)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(unsigned int, offset + end - map->m_la, end);
633 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
634 zero_user_segment(page, cur, end);
635 goto next_part;
636 }
637
638 /* let's derive page type */
639 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
640 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
641 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
642 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
643
644 if (cur)
645 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
646
647 retry:
648 err = z_erofs_attach_page(clt, page, page_type,
649 clt->mode >= COLLECT_PRIMARY_FOLLOWED);
650 /* should allocate an additional staging page for pagevec */
651 if (err == -EAGAIN) {
652 struct page *const newpage =
653 alloc_page(GFP_NOFS | __GFP_NOFAIL);
654
655 newpage->mapping = Z_EROFS_MAPPING_STAGING;
656 err = z_erofs_attach_page(clt, newpage,
657 Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
658 if (!err)
659 goto retry;
660 }
661
662 if (err)
663 goto err_out;
664
665 index = page->index - (map->m_la >> PAGE_SHIFT);
666
667 z_erofs_onlinepage_fixup(page, index, true);
668
669 /* bump up the number of spiltted parts of a page */
670 ++spiltted;
671 /* also update nr_pages */
672 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
673 next_part:
674 /* can be used for verification */
675 map->m_llen = offset + cur - map->m_la;
676
677 end = cur;
678 if (end > 0)
679 goto repeat;
680
681 out:
682 z_erofs_onlinepage_endio(page);
683
684 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
685 __func__, page, spiltted, map->m_llen);
686 return err;
687
688 /* if some error occurred while processing this page */
689 err_out:
690 SetPageError(page);
691 goto out;
692 }
693
z_erofs_decompress_kickoff(struct z_erofs_decompressqueue * io,bool sync,int bios)694 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
695 bool sync, int bios)
696 {
697 /* wake up the caller thread for sync decompression */
698 if (sync) {
699 unsigned long flags;
700
701 spin_lock_irqsave(&io->u.wait.lock, flags);
702 if (!atomic_add_return(bios, &io->pending_bios))
703 wake_up_locked(&io->u.wait);
704 spin_unlock_irqrestore(&io->u.wait.lock, flags);
705 return;
706 }
707
708 if (!atomic_add_return(bios, &io->pending_bios))
709 queue_work(z_erofs_workqueue, &io->u.work);
710 }
711
z_erofs_decompressqueue_endio(struct bio * bio)712 static void z_erofs_decompressqueue_endio(struct bio *bio)
713 {
714 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
715 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
716 blk_status_t err = bio->bi_status;
717 struct bio_vec *bvec;
718 struct bvec_iter_all iter_all;
719
720 bio_for_each_segment_all(bvec, bio, iter_all) {
721 struct page *page = bvec->bv_page;
722
723 DBG_BUGON(PageUptodate(page));
724 DBG_BUGON(!page->mapping);
725
726 if (err)
727 SetPageError(page);
728
729 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
730 if (!err)
731 SetPageUptodate(page);
732 unlock_page(page);
733 }
734 }
735 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
736 bio_put(bio);
737 }
738
z_erofs_decompress_pcluster(struct super_block * sb,struct z_erofs_pcluster * pcl,struct list_head * pagepool)739 static int z_erofs_decompress_pcluster(struct super_block *sb,
740 struct z_erofs_pcluster *pcl,
741 struct list_head *pagepool)
742 {
743 struct erofs_sb_info *const sbi = EROFS_SB(sb);
744 const unsigned int clusterpages = BIT(pcl->clusterbits);
745 struct z_erofs_pagevec_ctor ctor;
746 unsigned int i, outputsize, llen, nr_pages;
747 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
748 struct page **pages, **compressed_pages, *page;
749
750 enum z_erofs_page_type page_type;
751 bool overlapped, partial;
752 struct z_erofs_collection *cl;
753 int err;
754
755 might_sleep();
756 cl = z_erofs_primarycollection(pcl);
757 DBG_BUGON(!READ_ONCE(cl->nr_pages));
758
759 mutex_lock(&cl->lock);
760 nr_pages = cl->nr_pages;
761
762 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
763 pages = pages_onstack;
764 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
765 mutex_trylock(&z_pagemap_global_lock)) {
766 pages = z_pagemap_global;
767 } else {
768 gfp_t gfp_flags = GFP_KERNEL;
769
770 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
771 gfp_flags |= __GFP_NOFAIL;
772
773 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
774 gfp_flags);
775
776 /* fallback to global pagemap for the lowmem scenario */
777 if (!pages) {
778 mutex_lock(&z_pagemap_global_lock);
779 pages = z_pagemap_global;
780 }
781 }
782
783 for (i = 0; i < nr_pages; ++i)
784 pages[i] = NULL;
785
786 err = 0;
787 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
788 cl->pagevec, 0);
789
790 for (i = 0; i < cl->vcnt; ++i) {
791 unsigned int pagenr;
792
793 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
794
795 /* all pages in pagevec ought to be valid */
796 DBG_BUGON(!page);
797 DBG_BUGON(!page->mapping);
798
799 if (z_erofs_put_stagingpage(pagepool, page))
800 continue;
801
802 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
803 pagenr = 0;
804 else
805 pagenr = z_erofs_onlinepage_index(page);
806
807 DBG_BUGON(pagenr >= nr_pages);
808
809 /*
810 * currently EROFS doesn't support multiref(dedup),
811 * so here erroring out one multiref page.
812 */
813 if (pages[pagenr]) {
814 DBG_BUGON(1);
815 SetPageError(pages[pagenr]);
816 z_erofs_onlinepage_endio(pages[pagenr]);
817 err = -EFSCORRUPTED;
818 }
819 pages[pagenr] = page;
820 }
821 z_erofs_pagevec_ctor_exit(&ctor, true);
822
823 overlapped = false;
824 compressed_pages = pcl->compressed_pages;
825
826 for (i = 0; i < clusterpages; ++i) {
827 unsigned int pagenr;
828
829 page = compressed_pages[i];
830
831 /* all compressed pages ought to be valid */
832 DBG_BUGON(!page);
833 DBG_BUGON(!page->mapping);
834
835 if (!z_erofs_page_is_staging(page)) {
836 if (erofs_page_is_managed(sbi, page)) {
837 if (!PageUptodate(page))
838 err = -EIO;
839 continue;
840 }
841
842 /*
843 * only if non-head page can be selected
844 * for inplace decompression
845 */
846 pagenr = z_erofs_onlinepage_index(page);
847
848 DBG_BUGON(pagenr >= nr_pages);
849 if (pages[pagenr]) {
850 DBG_BUGON(1);
851 SetPageError(pages[pagenr]);
852 z_erofs_onlinepage_endio(pages[pagenr]);
853 err = -EFSCORRUPTED;
854 }
855 pages[pagenr] = page;
856
857 overlapped = true;
858 }
859
860 /* PG_error needs checking for inplaced and staging pages */
861 if (PageError(page)) {
862 DBG_BUGON(PageUptodate(page));
863 err = -EIO;
864 }
865 }
866
867 if (err)
868 goto out;
869
870 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
871 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
872 outputsize = llen;
873 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
874 } else {
875 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
876 partial = true;
877 }
878
879 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
880 .sb = sb,
881 .in = compressed_pages,
882 .out = pages,
883 .pageofs_out = cl->pageofs,
884 .inputsize = PAGE_SIZE,
885 .outputsize = outputsize,
886 .alg = pcl->algorithmformat,
887 .inplace_io = overlapped,
888 .partial_decoding = partial
889 }, pagepool);
890
891 out:
892 /* must handle all compressed pages before endding pages */
893 for (i = 0; i < clusterpages; ++i) {
894 page = compressed_pages[i];
895
896 if (erofs_page_is_managed(sbi, page))
897 continue;
898
899 /* recycle all individual staging pages */
900 (void)z_erofs_put_stagingpage(pagepool, page);
901
902 WRITE_ONCE(compressed_pages[i], NULL);
903 }
904
905 for (i = 0; i < nr_pages; ++i) {
906 page = pages[i];
907 if (!page)
908 continue;
909
910 DBG_BUGON(!page->mapping);
911
912 /* recycle all individual staging pages */
913 if (z_erofs_put_stagingpage(pagepool, page))
914 continue;
915
916 if (err < 0)
917 SetPageError(page);
918
919 z_erofs_onlinepage_endio(page);
920 }
921
922 if (pages == z_pagemap_global)
923 mutex_unlock(&z_pagemap_global_lock);
924 else if (pages != pages_onstack)
925 kvfree(pages);
926
927 cl->nr_pages = 0;
928 cl->vcnt = 0;
929
930 /* all cl locks MUST be taken before the following line */
931 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
932
933 /* all cl locks SHOULD be released right now */
934 mutex_unlock(&cl->lock);
935
936 z_erofs_collection_put(cl);
937 return err;
938 }
939
z_erofs_decompress_queue(const struct z_erofs_decompressqueue * io,struct list_head * pagepool)940 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
941 struct list_head *pagepool)
942 {
943 z_erofs_next_pcluster_t owned = io->head;
944
945 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
946 struct z_erofs_pcluster *pcl;
947
948 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
949 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
950
951 /* no possible that 'owned' equals NULL */
952 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
953
954 pcl = container_of(owned, struct z_erofs_pcluster, next);
955 owned = READ_ONCE(pcl->next);
956
957 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
958 }
959 }
960
z_erofs_decompressqueue_work(struct work_struct * work)961 static void z_erofs_decompressqueue_work(struct work_struct *work)
962 {
963 struct z_erofs_decompressqueue *bgq =
964 container_of(work, struct z_erofs_decompressqueue, u.work);
965 LIST_HEAD(pagepool);
966
967 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
968 z_erofs_decompress_queue(bgq, &pagepool);
969
970 put_pages_list(&pagepool);
971 kvfree(bgq);
972 }
973
pickup_page_for_submission(struct z_erofs_pcluster * pcl,unsigned int nr,struct list_head * pagepool,struct address_space * mc,gfp_t gfp)974 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
975 unsigned int nr,
976 struct list_head *pagepool,
977 struct address_space *mc,
978 gfp_t gfp)
979 {
980 const pgoff_t index = pcl->obj.index;
981 bool tocache = false;
982
983 struct address_space *mapping;
984 struct page *oldpage, *page;
985
986 compressed_page_t t;
987 int justfound;
988
989 repeat:
990 page = READ_ONCE(pcl->compressed_pages[nr]);
991 oldpage = page;
992
993 if (!page)
994 goto out_allocpage;
995
996 /*
997 * the cached page has not been allocated and
998 * an placeholder is out there, prepare it now.
999 */
1000 if (page == PAGE_UNALLOCATED) {
1001 tocache = true;
1002 goto out_allocpage;
1003 }
1004
1005 /* process the target tagged pointer */
1006 t = tagptr_init(compressed_page_t, page);
1007 justfound = tagptr_unfold_tags(t);
1008 page = tagptr_unfold_ptr(t);
1009
1010 mapping = READ_ONCE(page->mapping);
1011
1012 /*
1013 * unmanaged (file) pages are all locked solidly,
1014 * therefore it is impossible for `mapping' to be NULL.
1015 */
1016 if (mapping && mapping != mc)
1017 /* ought to be unmanaged pages */
1018 goto out;
1019
1020 lock_page(page);
1021
1022 /* only true if page reclaim goes wrong, should never happen */
1023 DBG_BUGON(justfound && PagePrivate(page));
1024
1025 /* the page is still in manage cache */
1026 if (page->mapping == mc) {
1027 WRITE_ONCE(pcl->compressed_pages[nr], page);
1028
1029 ClearPageError(page);
1030 if (!PagePrivate(page)) {
1031 /*
1032 * impossible to be !PagePrivate(page) for
1033 * the current restriction as well if
1034 * the page is already in compressed_pages[].
1035 */
1036 DBG_BUGON(!justfound);
1037
1038 justfound = 0;
1039 set_page_private(page, (unsigned long)pcl);
1040 SetPagePrivate(page);
1041 }
1042
1043 /* no need to submit io if it is already up-to-date */
1044 if (PageUptodate(page)) {
1045 unlock_page(page);
1046 page = NULL;
1047 }
1048 goto out;
1049 }
1050
1051 /*
1052 * the managed page has been truncated, it's unsafe to
1053 * reuse this one, let's allocate a new cache-managed page.
1054 */
1055 DBG_BUGON(page->mapping);
1056 DBG_BUGON(!justfound);
1057
1058 tocache = true;
1059 unlock_page(page);
1060 put_page(page);
1061 out_allocpage:
1062 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1063 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1064 /* non-LRU / non-movable temporary page is needed */
1065 page->mapping = Z_EROFS_MAPPING_STAGING;
1066 tocache = false;
1067 }
1068
1069 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1070 if (tocache) {
1071 /* since it added to managed cache successfully */
1072 unlock_page(page);
1073 put_page(page);
1074 } else {
1075 list_add(&page->lru, pagepool);
1076 }
1077 cond_resched();
1078 goto repeat;
1079 }
1080
1081 if (tocache) {
1082 set_page_private(page, (unsigned long)pcl);
1083 SetPagePrivate(page);
1084 }
1085 out: /* the only exit (for tracing and debugging) */
1086 return page;
1087 }
1088
1089 static struct z_erofs_decompressqueue *
jobqueue_init(struct super_block * sb,struct z_erofs_decompressqueue * fgq,bool * fg)1090 jobqueue_init(struct super_block *sb,
1091 struct z_erofs_decompressqueue *fgq, bool *fg)
1092 {
1093 struct z_erofs_decompressqueue *q;
1094
1095 if (fg && !*fg) {
1096 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1097 if (!q) {
1098 *fg = true;
1099 goto fg_out;
1100 }
1101 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1102 } else {
1103 fg_out:
1104 q = fgq;
1105 init_waitqueue_head(&fgq->u.wait);
1106 atomic_set(&fgq->pending_bios, 0);
1107 }
1108 q->sb = sb;
1109 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1110 return q;
1111 }
1112
1113 /* define decompression jobqueue types */
1114 enum {
1115 JQ_BYPASS,
1116 JQ_SUBMIT,
1117 NR_JOBQUEUES,
1118 };
1119
jobqueueset_init(struct super_block * sb,struct z_erofs_decompressqueue * q[],struct z_erofs_decompressqueue * fgq,bool * fg)1120 static void *jobqueueset_init(struct super_block *sb,
1121 struct z_erofs_decompressqueue *q[],
1122 struct z_erofs_decompressqueue *fgq, bool *fg)
1123 {
1124 /*
1125 * if managed cache is enabled, bypass jobqueue is needed,
1126 * no need to read from device for all pclusters in this queue.
1127 */
1128 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1129 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1130
1131 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1132 }
1133
move_to_bypass_jobqueue(struct z_erofs_pcluster * pcl,z_erofs_next_pcluster_t qtail[],z_erofs_next_pcluster_t owned_head)1134 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1135 z_erofs_next_pcluster_t qtail[],
1136 z_erofs_next_pcluster_t owned_head)
1137 {
1138 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1139 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1140
1141 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1142 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1143 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1144
1145 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1146
1147 WRITE_ONCE(*submit_qtail, owned_head);
1148 WRITE_ONCE(*bypass_qtail, &pcl->next);
1149
1150 qtail[JQ_BYPASS] = &pcl->next;
1151 }
1152
z_erofs_submit_queue(struct super_block * sb,struct z_erofs_decompress_frontend * f,struct list_head * pagepool,struct z_erofs_decompressqueue * fgq,bool * force_fg)1153 static void z_erofs_submit_queue(struct super_block *sb,
1154 struct z_erofs_decompress_frontend *f,
1155 struct list_head *pagepool,
1156 struct z_erofs_decompressqueue *fgq,
1157 bool *force_fg)
1158 {
1159 struct erofs_sb_info *const sbi = EROFS_SB(sb);
1160 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1161 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1162 void *bi_private;
1163 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1164 /* since bio will be NULL, no need to initialize last_index */
1165 pgoff_t last_index;
1166 unsigned int nr_bios = 0;
1167 struct bio *bio = NULL;
1168
1169 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1170 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1171 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1172
1173 /* by default, all need io submission */
1174 q[JQ_SUBMIT]->head = owned_head;
1175
1176 do {
1177 struct z_erofs_pcluster *pcl;
1178 pgoff_t cur, end;
1179 unsigned int i = 0;
1180 bool bypass = true;
1181
1182 /* no possible 'owned_head' equals the following */
1183 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1184 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1185
1186 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1187
1188 cur = pcl->obj.index;
1189 end = cur + BIT(pcl->clusterbits);
1190
1191 /* close the main owned chain at first */
1192 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1193 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1194
1195 do {
1196 struct page *page;
1197
1198 page = pickup_page_for_submission(pcl, i++, pagepool,
1199 MNGD_MAPPING(sbi),
1200 GFP_NOFS);
1201 if (!page)
1202 continue;
1203
1204 if (bio && cur != last_index + 1) {
1205 submit_bio_retry:
1206 submit_bio(bio);
1207 bio = NULL;
1208 }
1209
1210 if (!bio) {
1211 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1212
1213 bio->bi_end_io = z_erofs_decompressqueue_endio;
1214 bio_set_dev(bio, sb->s_bdev);
1215 bio->bi_iter.bi_sector = (sector_t)cur <<
1216 LOG_SECTORS_PER_BLOCK;
1217 bio->bi_private = bi_private;
1218 bio->bi_opf = REQ_OP_READ;
1219 if (f->readahead)
1220 bio->bi_opf |= REQ_RAHEAD;
1221 ++nr_bios;
1222 }
1223
1224 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1225 goto submit_bio_retry;
1226
1227 last_index = cur;
1228 bypass = false;
1229 } while (++cur < end);
1230
1231 if (!bypass)
1232 qtail[JQ_SUBMIT] = &pcl->next;
1233 else
1234 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1235 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1236
1237 if (bio)
1238 submit_bio(bio);
1239
1240 /*
1241 * although background is preferred, no one is pending for submission.
1242 * don't issue workqueue for decompression but drop it directly instead.
1243 */
1244 if (!*force_fg && !nr_bios) {
1245 kvfree(q[JQ_SUBMIT]);
1246 return;
1247 }
1248 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1249 }
1250
z_erofs_runqueue(struct super_block * sb,struct z_erofs_decompress_frontend * f,struct list_head * pagepool,bool force_fg)1251 static void z_erofs_runqueue(struct super_block *sb,
1252 struct z_erofs_decompress_frontend *f,
1253 struct list_head *pagepool, bool force_fg)
1254 {
1255 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1256
1257 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
1258 return;
1259 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
1260
1261 /* handle bypass queue (no i/o pclusters) immediately */
1262 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1263
1264 if (!force_fg)
1265 return;
1266
1267 /* wait until all bios are completed */
1268 io_wait_event(io[JQ_SUBMIT].u.wait,
1269 !atomic_read(&io[JQ_SUBMIT].pending_bios));
1270
1271 /* handle synchronous decompress queue in the caller context */
1272 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1273 }
1274
z_erofs_readpage(struct file * file,struct page * page)1275 static int z_erofs_readpage(struct file *file, struct page *page)
1276 {
1277 struct inode *const inode = page->mapping->host;
1278 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1279 int err;
1280 LIST_HEAD(pagepool);
1281
1282 trace_erofs_readpage(page, false);
1283
1284 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1285
1286 err = z_erofs_do_read_page(&f, page);
1287 (void)z_erofs_collector_end(&f.clt);
1288
1289 /* if some compressed cluster ready, need submit them anyway */
1290 z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
1291
1292 if (err)
1293 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1294
1295 if (f.map.mpage)
1296 put_page(f.map.mpage);
1297
1298 /* clean up the remaining free pages */
1299 put_pages_list(&pagepool);
1300 return err;
1301 }
1302
z_erofs_readahead(struct readahead_control * rac)1303 static void z_erofs_readahead(struct readahead_control *rac)
1304 {
1305 struct inode *const inode = rac->mapping->host;
1306 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1307
1308 unsigned int nr_pages = readahead_count(rac);
1309 bool sync = (nr_pages <= sbi->ctx.max_sync_decompress_pages);
1310 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1311 struct page *page, *head = NULL;
1312 LIST_HEAD(pagepool);
1313
1314 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1315
1316 f.readahead = true;
1317 f.headoffset = readahead_pos(rac);
1318
1319 while ((page = readahead_page(rac))) {
1320 prefetchw(&page->flags);
1321
1322 /*
1323 * A pure asynchronous readahead is indicated if
1324 * a PG_readahead marked page is hitted at first.
1325 * Let's also do asynchronous decompression for this case.
1326 */
1327 sync &= !(PageReadahead(page) && !head);
1328
1329 set_page_private(page, (unsigned long)head);
1330 head = page;
1331 }
1332
1333 while (head) {
1334 struct page *page = head;
1335 int err;
1336
1337 /* traversal in reverse order */
1338 head = (void *)page_private(page);
1339
1340 err = z_erofs_do_read_page(&f, page);
1341 if (err)
1342 erofs_err(inode->i_sb,
1343 "readahead error at page %lu @ nid %llu",
1344 page->index, EROFS_I(inode)->nid);
1345 put_page(page);
1346 }
1347
1348 (void)z_erofs_collector_end(&f.clt);
1349
1350 z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
1351
1352 if (f.map.mpage)
1353 put_page(f.map.mpage);
1354
1355 /* clean up the remaining free pages */
1356 put_pages_list(&pagepool);
1357 }
1358
1359 const struct address_space_operations z_erofs_aops = {
1360 .readpage = z_erofs_readpage,
1361 .readahead = z_erofs_readahead,
1362 };
1363
1364