blob: fad80c97d2476fbeaff3858e7a10c0b4c6e583c6 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang02827e12018-07-26 20:21:58 +08002/*
Gao Xiang02827e12018-07-26 20:21:58 +08003 * Copyright (C) 2018 HUAWEI, Inc.
4 * http://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
Gao Xiang02827e12018-07-26 20:21:58 +08006 */
Gao Xiang57b78c92019-07-31 23:57:32 +08007#include "zdata.h"
Gao Xiang27481232019-06-24 15:22:54 +08008#include "compress.h"
Gao Xiang3883a792018-07-26 20:22:06 +08009#include <linux/prefetch.h>
10
Chen Gong284db122018-09-18 22:27:27 +080011#include <trace/events/erofs.h>
12
Gao Xiang672e5472018-12-08 00:19:14 +080013/*
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
Gao Xiang97e86a82019-07-31 23:57:47 +080019/* how to allocate cached pages for a pcluster */
Gao Xiang92e6efd2018-12-08 00:19:16 +080020enum 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 */
29typedef tagptr1_t compressed_page_t;
30
31#define tag_compressed_page_justfound(page) \
32 tagptr_fold(compressed_page_t, page, 1)
33
Gao Xiang3883a792018-07-26 20:22:06 +080034static struct workqueue_struct *z_erofs_workqueue __read_mostly;
Gao Xiang97e86a82019-07-31 23:57:47 +080035static struct kmem_cache *pcluster_cachep __read_mostly;
Gao Xiang3883a792018-07-26 20:22:06 +080036
37void z_erofs_exit_zip_subsystem(void)
38{
Gao Xiang3883a792018-07-26 20:22:06 +080039 destroy_workqueue(z_erofs_workqueue);
Gao Xiang97e86a82019-07-31 23:57:47 +080040 kmem_cache_destroy(pcluster_cachep);
Gao Xiang3883a792018-07-26 20:22:06 +080041}
42
Gao Xiang99634bf2019-09-04 10:09:05 +080043static inline int z_erofs_init_workqueue(void)
Gao Xiang3883a792018-07-26 20:22:06 +080044{
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +020045 const unsigned int onlinecpus = num_possible_cpus();
Gao Xiang97e86a82019-07-31 23:57:47 +080046 const unsigned int flags = WQ_UNBOUND | WQ_HIGHPRI | WQ_CPU_INTENSIVE;
Gao Xiang3883a792018-07-26 20:22:06 +080047
48 /*
Gao Xiang97e86a82019-07-31 23:57:47 +080049 * no need to spawn too many threads, limiting threads could minimum
50 * scheduling overhead, perhaps per-CPU threads should be better?
Gao Xiang3883a792018-07-26 20:22:06 +080051 */
Gao Xiang97e86a82019-07-31 23:57:47 +080052 z_erofs_workqueue = alloc_workqueue("erofs_unzipd", flags,
53 onlinecpus + onlinecpus / 4);
Cristian Sicilia42d40b42018-11-12 21:43:57 +010054 return z_erofs_workqueue ? 0 : -ENOMEM;
Gao Xiang3883a792018-07-26 20:22:06 +080055}
56
Gao Xiang99634bf2019-09-04 10:09:05 +080057static void z_erofs_pcluster_init_once(void *ptr)
Gao Xiang48d4bf32018-11-23 01:21:46 +080058{
Gao Xiang97e86a82019-07-31 23:57:47 +080059 struct z_erofs_pcluster *pcl = ptr;
60 struct z_erofs_collection *cl = z_erofs_primarycollection(pcl);
Gao Xiang48d4bf32018-11-23 01:21:46 +080061 unsigned int i;
62
Gao Xiang97e86a82019-07-31 23:57:47 +080063 mutex_init(&cl->lock);
64 cl->nr_pages = 0;
65 cl->vcnt = 0;
Gao Xiang48d4bf32018-11-23 01:21:46 +080066 for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i)
Gao Xiang97e86a82019-07-31 23:57:47 +080067 pcl->compressed_pages[i] = NULL;
Gao Xiang48d4bf32018-11-23 01:21:46 +080068}
69
Gao Xiang99634bf2019-09-04 10:09:05 +080070static void z_erofs_pcluster_init_always(struct z_erofs_pcluster *pcl)
Gao Xiang48d4bf32018-11-23 01:21:46 +080071{
Gao Xiang97e86a82019-07-31 23:57:47 +080072 struct z_erofs_collection *cl = z_erofs_primarycollection(pcl);
Gao Xiang48d4bf32018-11-23 01:21:46 +080073
Gao Xiang97e86a82019-07-31 23:57:47 +080074 atomic_set(&pcl->obj.refcount, 1);
Gao Xiang48d4bf32018-11-23 01:21:46 +080075
Gao Xiang97e86a82019-07-31 23:57:47 +080076 DBG_BUGON(cl->nr_pages);
77 DBG_BUGON(cl->vcnt);
Gao Xiang48d4bf32018-11-23 01:21:46 +080078}
79
Gao Xiang0a0b7e62018-10-09 21:43:53 +080080int __init z_erofs_init_zip_subsystem(void)
Gao Xiang3883a792018-07-26 20:22:06 +080081{
Gao Xiang97e86a82019-07-31 23:57:47 +080082 pcluster_cachep = kmem_cache_create("erofs_compress",
83 Z_EROFS_WORKGROUP_SIZE, 0,
Gao Xiang99634bf2019-09-04 10:09:05 +080084 SLAB_RECLAIM_ACCOUNT,
85 z_erofs_pcluster_init_once);
Gao Xiang97e86a82019-07-31 23:57:47 +080086 if (pcluster_cachep) {
Gao Xiang99634bf2019-09-04 10:09:05 +080087 if (!z_erofs_init_workqueue())
Gao Xiang3883a792018-07-26 20:22:06 +080088 return 0;
89
Gao Xiang97e86a82019-07-31 23:57:47 +080090 kmem_cache_destroy(pcluster_cachep);
Gao Xiang3883a792018-07-26 20:22:06 +080091 }
92 return -ENOMEM;
93}
94
Gao Xiang97e86a82019-07-31 23:57:47 +080095enum z_erofs_collectmode {
96 COLLECT_SECONDARY,
97 COLLECT_PRIMARY,
Gao Xiang3883a792018-07-26 20:22:06 +080098 /*
Gao Xiang97e86a82019-07-31 23:57:47 +080099 * The current collection was the tail of an exist chain, in addition
100 * that the previous processed chained collections are all decided to
101 * be hooked up to it.
102 * A new chain will be created for the remaining collections which are
103 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
104 * the next collection cannot reuse the whole page safely in
105 * the following scenario:
Gao Xianga1121522019-02-27 13:33:32 +0800106 * ________________________________________________________________
107 * | tail (partial) page | head (partial) page |
Gao Xiang97e86a82019-07-31 23:57:47 +0800108 * | (belongs to the next cl) | (belongs to the current cl) |
Gao Xianga1121522019-02-27 13:33:32 +0800109 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
110 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800111 COLLECT_PRIMARY_HOOKED,
112 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
Gao Xianga1121522019-02-27 13:33:32 +0800113 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800114 * The current collection has been linked with the owned chain, and
115 * could also be linked with the remaining collections, which means
116 * if the processing page is the tail page of the collection, thus
117 * the current collection can safely use the whole page (since
118 * the previous collection is under control) for in-place I/O, as
119 * illustrated below:
Gao Xianga1121522019-02-27 13:33:32 +0800120 * ________________________________________________________________
Gao Xiang97e86a82019-07-31 23:57:47 +0800121 * | tail (partial) page | head (partial) page |
122 * | (of the current cl) | (of the previous collection) |
123 * | PRIMARY_FOLLOWED or | |
124 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
Gao Xianga1121522019-02-27 13:33:32 +0800125 *
Gao Xiang97e86a82019-07-31 23:57:47 +0800126 * [ (*) the above page can be used as inplace I/O. ]
Gao Xiang3883a792018-07-26 20:22:06 +0800127 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800128 COLLECT_PRIMARY_FOLLOWED,
Gao Xiang3883a792018-07-26 20:22:06 +0800129};
130
Gao Xiang97e86a82019-07-31 23:57:47 +0800131struct z_erofs_collector {
Gao Xiang3883a792018-07-26 20:22:06 +0800132 struct z_erofs_pagevec_ctor vector;
133
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800134 struct z_erofs_pcluster *pcl, *tailpcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800135 struct z_erofs_collection *cl;
136 struct page **compressedpages;
137 z_erofs_next_pcluster_t owned_head;
138
139 enum z_erofs_collectmode mode;
Gao Xiang3883a792018-07-26 20:22:06 +0800140};
141
Gao Xiang97e86a82019-07-31 23:57:47 +0800142struct z_erofs_decompress_frontend {
143 struct inode *const inode;
144
145 struct z_erofs_collector clt;
146 struct erofs_map_blocks map;
147
148 /* used for applying cache strategy on the fly */
149 bool backmost;
150 erofs_off_t headoffset;
151};
152
153#define COLLECTOR_INIT() { \
154 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
155 .mode = COLLECT_PRIMARY_FOLLOWED }
156
157#define DECOMPRESS_FRONTEND_INIT(__i) { \
158 .inode = __i, .clt = COLLECTOR_INIT(), \
159 .backmost = true, }
160
161static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
162static DEFINE_MUTEX(z_pagemap_global_lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800163
Gao Xiang97e86a82019-07-31 23:57:47 +0800164static void preload_compressed_pages(struct z_erofs_collector *clt,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800165 struct address_space *mc,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800166 enum z_erofs_cache_alloctype type,
Gao Xiang97e86a82019-07-31 23:57:47 +0800167 struct list_head *pagepool)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800168{
Gao Xiang97e86a82019-07-31 23:57:47 +0800169 const struct z_erofs_pcluster *pcl = clt->pcl;
170 const unsigned int clusterpages = BIT(pcl->clusterbits);
171 struct page **pages = clt->compressedpages;
172 pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
Gao Xiang92e6efd2018-12-08 00:19:16 +0800173 bool standalone = true;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800174
Gao Xiang97e86a82019-07-31 23:57:47 +0800175 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800176 return;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800177
Gao Xiang97e86a82019-07-31 23:57:47 +0800178 for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
Gao Xiang92e6efd2018-12-08 00:19:16 +0800179 struct page *page;
180 compressed_page_t t;
181
182 /* the compressed page was loaded before */
Gao Xiang97e86a82019-07-31 23:57:47 +0800183 if (READ_ONCE(*pages))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800184 continue;
185
Gao Xiang97e86a82019-07-31 23:57:47 +0800186 page = find_get_page(mc, index);
Gao Xiang92e6efd2018-12-08 00:19:16 +0800187
188 if (page) {
189 t = tag_compressed_page_justfound(page);
190 } else if (type == DELAYEDALLOC) {
191 t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
192 } else { /* DONTALLOC */
193 if (standalone)
Gao Xiang97e86a82019-07-31 23:57:47 +0800194 clt->compressedpages = pages;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800195 standalone = false;
196 continue;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800197 }
198
Gao Xiang97e86a82019-07-31 23:57:47 +0800199 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800200 continue;
201
Gao Xiang92e6efd2018-12-08 00:19:16 +0800202 if (page)
203 put_page(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800204 }
Gao Xiang92e6efd2018-12-08 00:19:16 +0800205
Gao Xiang97e86a82019-07-31 23:57:47 +0800206 if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
207 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800208}
209
210/* called by erofs_shrinker to get rid of all compressed_pages */
Gao Xiang47e541a2018-07-29 13:34:58 +0800211int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
Gao Xiang97e86a82019-07-31 23:57:47 +0800212 struct erofs_workgroup *grp)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800213{
Gao Xiang97e86a82019-07-31 23:57:47 +0800214 struct z_erofs_pcluster *const pcl =
215 container_of(grp, struct z_erofs_pcluster, obj);
Gao Xiangc1448fa2018-12-08 00:19:13 +0800216 struct address_space *const mapping = MNGD_MAPPING(sbi);
Gao Xiang97e86a82019-07-31 23:57:47 +0800217 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800218 int i;
219
220 /*
221 * refcount of workgroup is now freezed as 1,
222 * therefore no need to worry about available decompression users.
223 */
224 for (i = 0; i < clusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800225 struct page *page = pcl->compressed_pages[i];
Gao Xiang105d4ad2018-07-26 20:22:07 +0800226
Gao Xiang97e86a82019-07-31 23:57:47 +0800227 if (!page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800228 continue;
229
230 /* block other users from reclaiming or migrating the page */
231 if (!trylock_page(page))
232 return -EBUSY;
233
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800234 if (page->mapping != mapping)
Gao Xiang97e86a82019-07-31 23:57:47 +0800235 continue;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800236
Gao Xiang97e86a82019-07-31 23:57:47 +0800237 /* barrier is implied in the following 'unlock_page' */
238 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800239 set_page_private(page, 0);
240 ClearPagePrivate(page);
241
242 unlock_page(page);
243 put_page(page);
244 }
245 return 0;
246}
247
Gao Xiang47e541a2018-07-29 13:34:58 +0800248int erofs_try_to_free_cached_page(struct address_space *mapping,
249 struct page *page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800250{
Gao Xiang97e86a82019-07-31 23:57:47 +0800251 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
252 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800253 int ret = 0; /* 0 - busy */
254
Gao Xiang97e86a82019-07-31 23:57:47 +0800255 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
Gao Xiang105d4ad2018-07-26 20:22:07 +0800256 unsigned int i;
257
258 for (i = 0; i < clusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800259 if (pcl->compressed_pages[i] == page) {
260 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800261 ret = 1;
262 break;
263 }
264 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800265 erofs_workgroup_unfreeze(&pcl->obj, 1);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800266
Gao Xiang047d4ab2019-02-16 16:46:50 +0800267 if (ret) {
268 ClearPagePrivate(page);
269 put_page(page);
270 }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800271 }
272 return ret;
273}
Gao Xiang105d4ad2018-07-26 20:22:07 +0800274
Gao Xiang3883a792018-07-26 20:22:06 +0800275/* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
Gao Xiang99634bf2019-09-04 10:09:05 +0800276static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
277 struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +0800278{
Gao Xiang97e86a82019-07-31 23:57:47 +0800279 struct z_erofs_pcluster *const pcl = clt->pcl;
280 const unsigned int clusterpages = BIT(pcl->clusterbits);
281
282 while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
283 if (!cmpxchg(clt->compressedpages++, NULL, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800284 return true;
285 }
Gao Xiang3883a792018-07-26 20:22:06 +0800286 return false;
287}
288
Gao Xiang97e86a82019-07-31 23:57:47 +0800289/* callers must be with collection lock held */
290static int z_erofs_attach_page(struct z_erofs_collector *clt,
291 struct page *page,
292 enum z_erofs_page_type type)
Gao Xiang3883a792018-07-26 20:22:06 +0800293{
294 int ret;
295 bool occupied;
296
Gao Xiang97e86a82019-07-31 23:57:47 +0800297 /* give priority for inplaceio */
298 if (clt->mode >= COLLECT_PRIMARY &&
Julian Merida447a3622019-03-18 20:58:41 -0300299 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
Gao Xiang99634bf2019-09-04 10:09:05 +0800300 z_erofs_try_inplace_io(clt, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800301 return 0;
302
Gao Xiang97e86a82019-07-31 23:57:47 +0800303 ret = z_erofs_pagevec_enqueue(&clt->vector,
Gao Xiang046d64e2019-07-31 23:57:45 +0800304 page, type, &occupied);
Gao Xiang97e86a82019-07-31 23:57:47 +0800305 clt->cl->vcnt += (unsigned int)ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800306
307 return ret ? 0 : -EAGAIN;
308}
309
Gao Xiang97e86a82019-07-31 23:57:47 +0800310static enum z_erofs_collectmode
311try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
312 z_erofs_next_pcluster_t *owned_head)
Gao Xiang3883a792018-07-26 20:22:06 +0800313{
Gao Xiang97e86a82019-07-31 23:57:47 +0800314 /* let's claim these following types of pclusters */
Gao Xiang3883a792018-07-26 20:22:06 +0800315retry:
Gao Xiang97e86a82019-07-31 23:57:47 +0800316 if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
317 /* type 1, nil pcluster */
318 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
319 *owned_head) != Z_EROFS_PCLUSTER_NIL)
Gao Xiang3883a792018-07-26 20:22:06 +0800320 goto retry;
321
Gao Xiang97e86a82019-07-31 23:57:47 +0800322 *owned_head = &pcl->next;
Gao Xianga1121522019-02-27 13:33:32 +0800323 /* lucky, I am the followee :) */
Gao Xiang97e86a82019-07-31 23:57:47 +0800324 return COLLECT_PRIMARY_FOLLOWED;
325 } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
Gao Xiang3883a792018-07-26 20:22:06 +0800326 /*
327 * type 2, link to the end of a existing open chain,
328 * be careful that its submission itself is governed
329 * by the original owned chain.
330 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800331 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
332 *owned_head) != Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +0800333 goto retry;
Gao Xiang97e86a82019-07-31 23:57:47 +0800334 *owned_head = Z_EROFS_PCLUSTER_TAIL;
335 return COLLECT_PRIMARY_HOOKED;
Gao Xianga1121522019-02-27 13:33:32 +0800336 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800337 return COLLECT_PRIMARY; /* :( better luck next time */
Gao Xiang3883a792018-07-26 20:22:06 +0800338}
339
Gao Xiang97e86a82019-07-31 23:57:47 +0800340static struct z_erofs_collection *cllookup(struct z_erofs_collector *clt,
341 struct inode *inode,
342 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800343{
Gao Xiang97e86a82019-07-31 23:57:47 +0800344 struct erofs_workgroup *grp;
345 struct z_erofs_pcluster *pcl;
346 struct z_erofs_collection *cl;
347 unsigned int length;
348 bool tag;
Gao Xiang3883a792018-07-26 20:22:06 +0800349
Gao Xiang97e86a82019-07-31 23:57:47 +0800350 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT, &tag);
351 if (!grp)
Gao Xiang3883a792018-07-26 20:22:06 +0800352 return NULL;
Gao Xiang97e86a82019-07-31 23:57:47 +0800353
354 pcl = container_of(grp, struct z_erofs_pcluster, obj);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800355 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
356 DBG_BUGON(1);
357 erofs_workgroup_put(grp);
358 return ERR_PTR(-EFSCORRUPTED);
359 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800360
361 cl = z_erofs_primarycollection(pcl);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800362 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800363 DBG_BUGON(1);
Gao Xiang138e1a02019-08-19 18:34:23 +0800364 erofs_workgroup_put(grp);
365 return ERR_PTR(-EFSCORRUPTED);
Gao Xiang3883a792018-07-26 20:22:06 +0800366 }
367
Gao Xiang97e86a82019-07-31 23:57:47 +0800368 length = READ_ONCE(pcl->length);
369 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
370 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
371 DBG_BUGON(1);
Gao Xiang138e1a02019-08-19 18:34:23 +0800372 erofs_workgroup_put(grp);
373 return ERR_PTR(-EFSCORRUPTED);
Gao Xiang97e86a82019-07-31 23:57:47 +0800374 }
375 } else {
376 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
Gao Xiang3883a792018-07-26 20:22:06 +0800377
Gao Xiang97e86a82019-07-31 23:57:47 +0800378 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
379 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
Gao Xiang3883a792018-07-26 20:22:06 +0800380
Gao Xiang97e86a82019-07-31 23:57:47 +0800381 while (llen > length &&
382 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
383 cpu_relax();
384 length = READ_ONCE(pcl->length);
385 }
386 }
387 mutex_lock(&cl->lock);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800388 /* used to check tail merging loop due to corrupted images */
389 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
390 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800391 clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800392 /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
393 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
394 clt->tailpcl = NULL;
Gao Xiang97e86a82019-07-31 23:57:47 +0800395 clt->pcl = pcl;
396 clt->cl = cl;
397 return cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800398}
399
Gao Xiang97e86a82019-07-31 23:57:47 +0800400static struct z_erofs_collection *clregister(struct z_erofs_collector *clt,
401 struct inode *inode,
402 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800403{
Gao Xiang97e86a82019-07-31 23:57:47 +0800404 struct z_erofs_pcluster *pcl;
405 struct z_erofs_collection *cl;
406 int err;
Gao Xiange5e3abb2018-09-19 13:49:07 +0800407
Gao Xiang3883a792018-07-26 20:22:06 +0800408 /* no available workgroup, let's allocate one */
Gao Xiang97e86a82019-07-31 23:57:47 +0800409 pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800410 if (!pcl)
Gao Xiang3883a792018-07-26 20:22:06 +0800411 return ERR_PTR(-ENOMEM);
412
Gao Xiang99634bf2019-09-04 10:09:05 +0800413 z_erofs_pcluster_init_always(pcl);
Gao Xiang97e86a82019-07-31 23:57:47 +0800414 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
Gao Xiang3883a792018-07-26 20:22:06 +0800415
Gao Xiang97e86a82019-07-31 23:57:47 +0800416 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
417 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
418 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800419
Gao Xiang97e86a82019-07-31 23:57:47 +0800420 if (map->m_flags & EROFS_MAP_ZIPPED)
421 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
422 else
423 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
Gao Xiangb6a76182019-06-24 15:22:58 +0800424
Gao Xianga5876e22019-09-04 10:08:56 +0800425 pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
Gao Xiang97e86a82019-07-31 23:57:47 +0800426 pcl->clusterbits -= PAGE_SHIFT;
Gao Xiang3883a792018-07-26 20:22:06 +0800427
Gao Xiang97e86a82019-07-31 23:57:47 +0800428 /* new pclusters should be claimed as type 1, primary and followed */
429 pcl->next = clt->owned_head;
430 clt->mode = COLLECT_PRIMARY_FOLLOWED;
431
432 cl = z_erofs_primarycollection(pcl);
433 cl->pageofs = map->m_la & ~PAGE_MASK;
Gao Xiang3883a792018-07-26 20:22:06 +0800434
Gao Xiang23edf3a2018-11-23 01:21:47 +0800435 /*
436 * lock all primary followed works before visible to others
Gao Xiang97e86a82019-07-31 23:57:47 +0800437 * and mutex_trylock *never* fails for a new pcluster.
Gao Xiang23edf3a2018-11-23 01:21:47 +0800438 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800439 mutex_trylock(&cl->lock);
Gao Xiang23edf3a2018-11-23 01:21:47 +0800440
Gao Xiang97e86a82019-07-31 23:57:47 +0800441 err = erofs_register_workgroup(inode->i_sb, &pcl->obj, 0);
442 if (err) {
443 mutex_unlock(&cl->lock);
444 kmem_cache_free(pcluster_cachep, pcl);
445 return ERR_PTR(-EAGAIN);
Gao Xiang3883a792018-07-26 20:22:06 +0800446 }
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800447 /* used to check tail merging loop due to corrupted images */
448 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
449 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800450 clt->owned_head = &pcl->next;
451 clt->pcl = pcl;
452 clt->cl = cl;
453 return cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800454}
455
Gao Xiang97e86a82019-07-31 23:57:47 +0800456static int z_erofs_collector_begin(struct z_erofs_collector *clt,
457 struct inode *inode,
458 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800459{
Gao Xiang97e86a82019-07-31 23:57:47 +0800460 struct z_erofs_collection *cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800461
Gao Xiang97e86a82019-07-31 23:57:47 +0800462 DBG_BUGON(clt->cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800463
Gao Xiang97e86a82019-07-31 23:57:47 +0800464 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
465 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
466 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +0800467
Gao Xiang97e86a82019-07-31 23:57:47 +0800468 if (!PAGE_ALIGNED(map->m_pa)) {
469 DBG_BUGON(1);
470 return -EINVAL;
471 }
Gao Xiang3883a792018-07-26 20:22:06 +0800472
473repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +0800474 cl = cllookup(clt, inode, map);
475 if (!cl) {
476 cl = clregister(clt, inode, map);
Gao Xiangb27661c2018-09-19 13:49:06 +0800477
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800478 if (cl == ERR_PTR(-EAGAIN))
Gao Xiang97e86a82019-07-31 23:57:47 +0800479 goto repeat;
Gao Xiang3883a792018-07-26 20:22:06 +0800480 }
481
Gao Xiang97e86a82019-07-31 23:57:47 +0800482 if (IS_ERR(cl))
483 return PTR_ERR(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800484
Gao Xiang97e86a82019-07-31 23:57:47 +0800485 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
486 cl->pagevec, cl->vcnt);
Gao Xiang3883a792018-07-26 20:22:06 +0800487
Gao Xiang97e86a82019-07-31 23:57:47 +0800488 clt->compressedpages = clt->pcl->compressed_pages;
489 if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
490 clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
Gao Xiang3883a792018-07-26 20:22:06 +0800491 return 0;
492}
493
494/*
Gao Xiang97e86a82019-07-31 23:57:47 +0800495 * keep in mind that no referenced pclusters will be freed
496 * only after a RCU grace period.
Gao Xiang3883a792018-07-26 20:22:06 +0800497 */
498static void z_erofs_rcu_callback(struct rcu_head *head)
499{
Gao Xiang97e86a82019-07-31 23:57:47 +0800500 struct z_erofs_collection *const cl =
501 container_of(head, struct z_erofs_collection, rcu);
Gao Xiang3883a792018-07-26 20:22:06 +0800502
Gao Xiang97e86a82019-07-31 23:57:47 +0800503 kmem_cache_free(pcluster_cachep,
504 container_of(cl, struct z_erofs_pcluster,
505 primary_collection));
Gao Xiang3883a792018-07-26 20:22:06 +0800506}
507
508void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
509{
Gao Xiang97e86a82019-07-31 23:57:47 +0800510 struct z_erofs_pcluster *const pcl =
511 container_of(grp, struct z_erofs_pcluster, obj);
512 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800513
Gao Xiang97e86a82019-07-31 23:57:47 +0800514 call_rcu(&cl->rcu, z_erofs_rcu_callback);
Gao Xiang3883a792018-07-26 20:22:06 +0800515}
516
Gao Xiang97e86a82019-07-31 23:57:47 +0800517static void z_erofs_collection_put(struct z_erofs_collection *cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800518{
Gao Xiang97e86a82019-07-31 23:57:47 +0800519 struct z_erofs_pcluster *const pcl =
520 container_of(cl, struct z_erofs_pcluster, primary_collection);
521
522 erofs_workgroup_put(&pcl->obj);
Gao Xiang3883a792018-07-26 20:22:06 +0800523}
524
Gao Xiang97e86a82019-07-31 23:57:47 +0800525static bool z_erofs_collector_end(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800526{
Gao Xiang97e86a82019-07-31 23:57:47 +0800527 struct z_erofs_collection *cl = clt->cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800528
Gao Xiang97e86a82019-07-31 23:57:47 +0800529 if (!cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800530 return false;
531
Gao Xiang97e86a82019-07-31 23:57:47 +0800532 z_erofs_pagevec_ctor_exit(&clt->vector, false);
533 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800534
535 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800536 * if all pending pages are added, don't hold its reference
537 * any longer if the pcluster isn't hosted by ourselves.
Gao Xiang3883a792018-07-26 20:22:06 +0800538 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800539 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
540 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800541
Gao Xiang97e86a82019-07-31 23:57:47 +0800542 clt->cl = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +0800543 return true;
544}
545
546static inline struct page *__stagingpage_alloc(struct list_head *pagepool,
547 gfp_t gfp)
548{
Gao Xiangb25a1512019-07-31 23:57:43 +0800549 struct page *page = erofs_allocpage(pagepool, gfp, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800550
551 page->mapping = Z_EROFS_MAPPING_STAGING;
552 return page;
553}
554
Gao Xiang97e86a82019-07-31 23:57:47 +0800555static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800556 unsigned int cachestrategy,
Gao Xiang97e86a82019-07-31 23:57:47 +0800557 erofs_off_t la)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800558{
Gao Xiang4279f3f2019-07-31 23:57:49 +0800559 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
560 return false;
561
Gao Xiang92e6efd2018-12-08 00:19:16 +0800562 if (fe->backmost)
563 return true;
564
Gao Xiang4279f3f2019-07-31 23:57:49 +0800565 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
566 la < fe->headoffset;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800567}
Gao Xiang92e6efd2018-12-08 00:19:16 +0800568
Gao Xiang97e86a82019-07-31 23:57:47 +0800569static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
Gao Xiang3883a792018-07-26 20:22:06 +0800570 struct page *page,
Gao Xiang97e86a82019-07-31 23:57:47 +0800571 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800572{
Gao Xiang97e86a82019-07-31 23:57:47 +0800573 struct inode *const inode = fe->inode;
574 struct erofs_sb_info *const sbi __maybe_unused = EROFS_I_SB(inode);
Chao Yu3b423412019-01-15 09:42:21 +0800575 struct erofs_map_blocks *const map = &fe->map;
Gao Xiang97e86a82019-07-31 23:57:47 +0800576 struct z_erofs_collector *const clt = &fe->clt;
Gao Xiang3883a792018-07-26 20:22:06 +0800577 const loff_t offset = page_offset(page);
Gao Xiangdc76ea82019-09-22 18:04:34 +0800578 bool tight = true;
Gao Xiang3883a792018-07-26 20:22:06 +0800579
Gao Xiang92e6efd2018-12-08 00:19:16 +0800580 enum z_erofs_cache_alloctype cache_strategy;
Gao Xiang3883a792018-07-26 20:22:06 +0800581 enum z_erofs_page_type page_type;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200582 unsigned int cur, end, spiltted, index;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800583 int err = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800584
585 /* register locked file pages as online pages in pack */
586 z_erofs_onlinepage_init(page);
587
588 spiltted = 0;
589 end = PAGE_SIZE;
590repeat:
591 cur = end - 1;
592
593 /* lucky, within the range of the current map_blocks */
594 if (offset + cur >= map->m_la &&
Julian Merida447a3622019-03-18 20:58:41 -0300595 offset + cur < map->m_la + map->m_llen) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800596 /* didn't get a valid collection previously (very rare) */
597 if (!clt->cl)
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800598 goto restart_now;
Gao Xiang3883a792018-07-26 20:22:06 +0800599 goto hitted;
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800600 }
Gao Xiang3883a792018-07-26 20:22:06 +0800601
602 /* go ahead the next map_blocks */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800603 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
Gao Xiang3883a792018-07-26 20:22:06 +0800604
Gao Xiang97e86a82019-07-31 23:57:47 +0800605 if (z_erofs_collector_end(clt))
Gao Xiangf0c519f2018-11-23 01:21:49 +0800606 fe->backmost = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800607
608 map->m_la = offset + cur;
609 map->m_llen = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +0800610 err = z_erofs_map_blocks_iter(inode, map, 0);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800611 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800612 goto err_out;
613
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800614restart_now:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800615 if (!(map->m_flags & EROFS_MAP_MAPPED))
Gao Xiang3883a792018-07-26 20:22:06 +0800616 goto hitted;
617
Gao Xiang97e86a82019-07-31 23:57:47 +0800618 err = z_erofs_collector_begin(clt, inode, map);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800619 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800620 goto err_out;
621
Gao Xiang92e6efd2018-12-08 00:19:16 +0800622 /* preload all compressed pages (maybe downgrade role if necessary) */
Gao Xiang4279f3f2019-07-31 23:57:49 +0800623 if (should_alloc_managed_pages(fe, sbi->cache_strategy, map->m_la))
Gao Xiang92e6efd2018-12-08 00:19:16 +0800624 cache_strategy = DELAYEDALLOC;
625 else
626 cache_strategy = DONTALLOC;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800627
Gao Xiang97e86a82019-07-31 23:57:47 +0800628 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
629 cache_strategy, pagepool);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800630
Gao Xiang3883a792018-07-26 20:22:06 +0800631hitted:
Gao Xiangdc76ea82019-09-22 18:04:34 +0800632 /*
633 * Ensure the current partial page belongs to this submit chain rather
634 * than other concurrent submit chains or the noio(bypass) chain since
635 * those chains are handled asynchronously thus the page cannot be used
636 * for inplace I/O or pagevec (should be processed in strict order.)
637 */
638 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
639 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
640
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200641 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800642 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800643 zero_user_segment(page, cur, end);
644 goto next_part;
645 }
646
647 /* let's derive page type */
648 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
649 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
650 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
651 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
652
Gao Xianga1121522019-02-27 13:33:32 +0800653 if (cur)
Gao Xiang97e86a82019-07-31 23:57:47 +0800654 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xianga1121522019-02-27 13:33:32 +0800655
Gao Xiang3883a792018-07-26 20:22:06 +0800656retry:
Gao Xiang97e86a82019-07-31 23:57:47 +0800657 err = z_erofs_attach_page(clt, page, page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800658 /* should allocate an additional staging page for pagevec */
659 if (err == -EAGAIN) {
660 struct page *const newpage =
Gao Xiang97e86a82019-07-31 23:57:47 +0800661 __stagingpage_alloc(pagepool, GFP_NOFS);
Gao Xiang3883a792018-07-26 20:22:06 +0800662
Gao Xiang97e86a82019-07-31 23:57:47 +0800663 err = z_erofs_attach_page(clt, newpage,
664 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800665 if (!err)
Gao Xiang3883a792018-07-26 20:22:06 +0800666 goto retry;
667 }
668
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800669 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800670 goto err_out;
671
Gao Xiang97e86a82019-07-31 23:57:47 +0800672 index = page->index - (map->m_la >> PAGE_SHIFT);
Gao Xiang3883a792018-07-26 20:22:06 +0800673
Gao Xiang3883a792018-07-26 20:22:06 +0800674 z_erofs_onlinepage_fixup(page, index, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800675
Gao Xiang1e05ff32018-09-18 22:27:25 +0800676 /* bump up the number of spiltted parts of a page */
677 ++spiltted;
678 /* also update nr_pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800679 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
Gao Xiang3883a792018-07-26 20:22:06 +0800680next_part:
681 /* can be used for verification */
682 map->m_llen = offset + cur - map->m_la;
683
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300684 end = cur;
685 if (end > 0)
Gao Xiang3883a792018-07-26 20:22:06 +0800686 goto repeat;
687
Gao Xiang1e05ff32018-09-18 22:27:25 +0800688out:
Gao Xiang3883a792018-07-26 20:22:06 +0800689 z_erofs_onlinepage_endio(page);
690
Gao Xiang4f761fa2019-09-04 10:09:09 +0800691 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
692 __func__, page, spiltted, map->m_llen);
Gao Xiang3883a792018-07-26 20:22:06 +0800693 return err;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800694
695 /* if some error occurred while processing this page */
696err_out:
697 SetPageError(page);
698 goto out;
Gao Xiang3883a792018-07-26 20:22:06 +0800699}
700
701static void z_erofs_vle_unzip_kickoff(void *ptr, int bios)
702{
703 tagptr1_t t = tagptr_init(tagptr1_t, ptr);
Gao Xiang97e86a82019-07-31 23:57:47 +0800704 struct z_erofs_unzip_io *io = tagptr_unfold_ptr(t);
Gao Xiang3883a792018-07-26 20:22:06 +0800705 bool background = tagptr_unfold_tags(t);
706
Gao Xiang848bd9a2018-12-08 00:19:12 +0800707 if (!background) {
708 unsigned long flags;
Gao Xiang3883a792018-07-26 20:22:06 +0800709
Gao Xiang848bd9a2018-12-08 00:19:12 +0800710 spin_lock_irqsave(&io->u.wait.lock, flags);
711 if (!atomic_add_return(bios, &io->pending_bios))
712 wake_up_locked(&io->u.wait);
713 spin_unlock_irqrestore(&io->u.wait.lock, flags);
714 return;
715 }
716
717 if (!atomic_add_return(bios, &io->pending_bios))
Gao Xiang3883a792018-07-26 20:22:06 +0800718 queue_work(z_erofs_workqueue, &io->u.work);
Gao Xiang3883a792018-07-26 20:22:06 +0800719}
720
721static inline void z_erofs_vle_read_endio(struct bio *bio)
722{
Gao Xiangd61fbb62019-03-25 11:40:08 +0800723 struct erofs_sb_info *sbi = NULL;
Gao Xiang14a56ec2019-03-25 11:40:09 +0800724 blk_status_t err = bio->bi_status;
Gao Xiang3883a792018-07-26 20:22:06 +0800725 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800726 struct bvec_iter_all iter_all;
Gao Xiang3883a792018-07-26 20:22:06 +0800727
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200728 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang3883a792018-07-26 20:22:06 +0800729 struct page *page = bvec->bv_page;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800730 bool cachemngd = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800731
732 DBG_BUGON(PageUptodate(page));
Gao Xiang70b17992018-12-11 15:17:49 +0800733 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800734
Gao Xiange2c71e72019-09-04 10:09:06 +0800735 if (!sbi && !z_erofs_page_is_staging(page))
Gao Xiangd61fbb62019-03-25 11:40:08 +0800736 sbi = EROFS_SB(page->mapping->host->i_sb);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800737
Gao Xiangd61fbb62019-03-25 11:40:08 +0800738 /* sbi should already be gotten if the page is managed */
739 if (sbi)
740 cachemngd = erofs_page_is_managed(sbi, page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800741
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800742 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800743 SetPageError(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800744 else if (cachemngd)
745 SetPageUptodate(page);
746
747 if (cachemngd)
748 unlock_page(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800749 }
750
751 z_erofs_vle_unzip_kickoff(bio->bi_private, -1);
752 bio_put(bio);
753}
754
Gao Xiang97e86a82019-07-31 23:57:47 +0800755static int z_erofs_decompress_pcluster(struct super_block *sb,
756 struct z_erofs_pcluster *pcl,
757 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800758{
759 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +0800760 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +0800761 struct z_erofs_pagevec_ctor ctor;
Gao Xiang97e86a82019-07-31 23:57:47 +0800762 unsigned int i, outputsize, llen, nr_pages;
763 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
Gao Xiang3883a792018-07-26 20:22:06 +0800764 struct page **pages, **compressed_pages, *page;
Gao Xiang3883a792018-07-26 20:22:06 +0800765
766 enum z_erofs_page_type page_type;
Gao Xiangb6a76182019-06-24 15:22:58 +0800767 bool overlapped, partial;
Gao Xiang97e86a82019-07-31 23:57:47 +0800768 struct z_erofs_collection *cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800769 int err;
770
771 might_sleep();
Gao Xiang97e86a82019-07-31 23:57:47 +0800772 cl = z_erofs_primarycollection(pcl);
773 DBG_BUGON(!READ_ONCE(cl->nr_pages));
Gao Xiang3883a792018-07-26 20:22:06 +0800774
Gao Xiang97e86a82019-07-31 23:57:47 +0800775 mutex_lock(&cl->lock);
776 nr_pages = cl->nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800777
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800778 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
Gao Xiang3883a792018-07-26 20:22:06 +0800779 pages = pages_onstack;
Gao Xiang97e86a82019-07-31 23:57:47 +0800780 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
781 mutex_trylock(&z_pagemap_global_lock)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800782 pages = z_pagemap_global;
Gao Xiang97e86a82019-07-31 23:57:47 +0800783 } else {
Chao Yu441dfcc2019-07-16 17:44:22 +0800784 gfp_t gfp_flags = GFP_KERNEL;
785
Gao Xiang97e86a82019-07-31 23:57:47 +0800786 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
Chao Yu441dfcc2019-07-16 17:44:22 +0800787 gfp_flags |= __GFP_NOFAIL;
788
Julian Merida447a3622019-03-18 20:58:41 -0300789 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Chao Yu441dfcc2019-07-16 17:44:22 +0800790 gfp_flags);
Gao Xiang3883a792018-07-26 20:22:06 +0800791
792 /* fallback to global pagemap for the lowmem scenario */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800793 if (!pages) {
Chao Yu441dfcc2019-07-16 17:44:22 +0800794 mutex_lock(&z_pagemap_global_lock);
795 pages = z_pagemap_global;
Gao Xiang3883a792018-07-26 20:22:06 +0800796 }
797 }
798
799 for (i = 0; i < nr_pages; ++i)
800 pages[i] = NULL;
801
Gao Xiange12a0ce2019-08-21 22:01:52 +0800802 err = 0;
Gao Xiangfa61a332019-06-24 15:22:53 +0800803 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang97e86a82019-07-31 23:57:47 +0800804 cl->pagevec, 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800805
Gao Xiang97e86a82019-07-31 23:57:47 +0800806 for (i = 0; i < cl->vcnt; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200807 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800808
Gao Xiang046d64e2019-07-31 23:57:45 +0800809 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800810
811 /* all pages in pagevec ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100812 DBG_BUGON(!page);
813 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800814
Gao Xiang97e86a82019-07-31 23:57:47 +0800815 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800816 continue;
817
818 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
819 pagenr = 0;
820 else
821 pagenr = z_erofs_onlinepage_index(page);
822
Gao Xiang70b17992018-12-11 15:17:49 +0800823 DBG_BUGON(pagenr >= nr_pages);
Gao Xiange5e3abb2018-09-19 13:49:07 +0800824
Gao Xiange12a0ce2019-08-21 22:01:52 +0800825 /*
826 * currently EROFS doesn't support multiref(dedup),
827 * so here erroring out one multiref page.
828 */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800829 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800830 DBG_BUGON(1);
831 SetPageError(pages[pagenr]);
832 z_erofs_onlinepage_endio(pages[pagenr]);
833 err = -EFSCORRUPTED;
834 }
Gao Xiang3883a792018-07-26 20:22:06 +0800835 pages[pagenr] = page;
836 }
Gao Xiang3883a792018-07-26 20:22:06 +0800837 z_erofs_pagevec_ctor_exit(&ctor, true);
838
839 overlapped = false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800840 compressed_pages = pcl->compressed_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800841
842 for (i = 0; i < clusterpages; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200843 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800844
845 page = compressed_pages[i];
846
847 /* all compressed pages ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100848 DBG_BUGON(!page);
849 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800850
Gao Xiang27481232019-06-24 15:22:54 +0800851 if (!z_erofs_page_is_staging(page)) {
Gao Xiangd61fbb62019-03-25 11:40:08 +0800852 if (erofs_page_is_managed(sbi, page)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800853 if (!PageUptodate(page))
Gao Xiang11152492019-03-25 11:40:07 +0800854 err = -EIO;
855 continue;
856 }
Gao Xiang3883a792018-07-26 20:22:06 +0800857
Gao Xiang11152492019-03-25 11:40:07 +0800858 /*
859 * only if non-head page can be selected
860 * for inplace decompression
861 */
862 pagenr = z_erofs_onlinepage_index(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800863
Gao Xiang11152492019-03-25 11:40:07 +0800864 DBG_BUGON(pagenr >= nr_pages);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800865 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800866 DBG_BUGON(1);
867 SetPageError(pages[pagenr]);
868 z_erofs_onlinepage_endio(pages[pagenr]);
869 err = -EFSCORRUPTED;
870 }
Gao Xiang11152492019-03-25 11:40:07 +0800871 pages[pagenr] = page;
Gao Xiang3883a792018-07-26 20:22:06 +0800872
Gao Xiang11152492019-03-25 11:40:07 +0800873 overlapped = true;
874 }
875
876 /* PG_error needs checking for inplaced and staging pages */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800877 if (PageError(page)) {
Gao Xiang11152492019-03-25 11:40:07 +0800878 DBG_BUGON(PageUptodate(page));
879 err = -EIO;
880 }
Gao Xiang3883a792018-07-26 20:22:06 +0800881 }
882
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800883 if (err)
Gao Xiang11152492019-03-25 11:40:07 +0800884 goto out;
885
Gao Xiang97e86a82019-07-31 23:57:47 +0800886 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
887 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
888 outputsize = llen;
889 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
Gao Xiangb6a76182019-06-24 15:22:58 +0800890 } else {
Gao Xiang97e86a82019-07-31 23:57:47 +0800891 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
Gao Xiangb6a76182019-06-24 15:22:58 +0800892 partial = true;
893 }
Gao Xiang3883a792018-07-26 20:22:06 +0800894
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800895 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
896 .sb = sb,
897 .in = compressed_pages,
898 .out = pages,
Gao Xiang97e86a82019-07-31 23:57:47 +0800899 .pageofs_out = cl->pageofs,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800900 .inputsize = PAGE_SIZE,
901 .outputsize = outputsize,
Gao Xiang97e86a82019-07-31 23:57:47 +0800902 .alg = pcl->algorithmformat,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800903 .inplace_io = overlapped,
Gao Xiangb6a76182019-06-24 15:22:58 +0800904 .partial_decoding = partial
Gao Xiang97e86a82019-07-31 23:57:47 +0800905 }, pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800906
907out:
Gao Xiangaf692e12019-02-27 13:33:30 +0800908 /* must handle all compressed pages before endding pages */
Gao Xiang3883a792018-07-26 20:22:06 +0800909 for (i = 0; i < clusterpages; ++i) {
910 page = compressed_pages[i];
911
Gao Xiangd61fbb62019-03-25 11:40:08 +0800912 if (erofs_page_is_managed(sbi, page))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800913 continue;
Gao Xiangd61fbb62019-03-25 11:40:08 +0800914
Gao Xiang3883a792018-07-26 20:22:06 +0800915 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800916 (void)z_erofs_put_stagingpage(pagepool, page);
Gao Xiang3883a792018-07-26 20:22:06 +0800917
918 WRITE_ONCE(compressed_pages[i], NULL);
919 }
920
Gao Xiangaf692e12019-02-27 13:33:30 +0800921 for (i = 0; i < nr_pages; ++i) {
922 page = pages[i];
923 if (!page)
924 continue;
925
926 DBG_BUGON(!page->mapping);
927
928 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800929 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiangaf692e12019-02-27 13:33:30 +0800930 continue;
931
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800932 if (err < 0)
Gao Xiangaf692e12019-02-27 13:33:30 +0800933 SetPageError(page);
934
935 z_erofs_onlinepage_endio(page);
936 }
937
Gao Xiang3883a792018-07-26 20:22:06 +0800938 if (pages == z_pagemap_global)
939 mutex_unlock(&z_pagemap_global_lock);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800940 else if (pages != pages_onstack)
Gao Xiang3883a792018-07-26 20:22:06 +0800941 kvfree(pages);
942
Gao Xiang97e86a82019-07-31 23:57:47 +0800943 cl->nr_pages = 0;
944 cl->vcnt = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800945
Gao Xiang97e86a82019-07-31 23:57:47 +0800946 /* all cl locks MUST be taken before the following line */
947 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800948
Gao Xiang97e86a82019-07-31 23:57:47 +0800949 /* all cl locks SHOULD be released right now */
950 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800951
Gao Xiang97e86a82019-07-31 23:57:47 +0800952 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800953 return err;
954}
955
956static void z_erofs_vle_unzip_all(struct super_block *sb,
Gao Xiang97e86a82019-07-31 23:57:47 +0800957 struct z_erofs_unzip_io *io,
958 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800959{
Gao Xiang97e86a82019-07-31 23:57:47 +0800960 z_erofs_next_pcluster_t owned = io->head;
Gao Xiang3883a792018-07-26 20:22:06 +0800961
Gao Xiang97e86a82019-07-31 23:57:47 +0800962 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
963 struct z_erofs_pcluster *pcl;
Gao Xiang3883a792018-07-26 20:22:06 +0800964
965 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800966 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800967
968 /* no possible that 'owned' equals NULL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800969 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800970
Gao Xiang97e86a82019-07-31 23:57:47 +0800971 pcl = container_of(owned, struct z_erofs_pcluster, next);
972 owned = READ_ONCE(pcl->next);
Gao Xiang3883a792018-07-26 20:22:06 +0800973
Gao Xiang97e86a82019-07-31 23:57:47 +0800974 z_erofs_decompress_pcluster(sb, pcl, pagepool);
Gao Xiang3978c8e2018-08-06 11:27:53 +0800975 }
Gao Xiang3883a792018-07-26 20:22:06 +0800976}
977
978static void z_erofs_vle_unzip_wq(struct work_struct *work)
979{
Gao Xiang97e86a82019-07-31 23:57:47 +0800980 struct z_erofs_unzip_io_sb *iosb =
981 container_of(work, struct z_erofs_unzip_io_sb, io.u.work);
982 LIST_HEAD(pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800983
Gao Xiang97e86a82019-07-31 23:57:47 +0800984 DBG_BUGON(iosb->io.head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
985 z_erofs_vle_unzip_all(iosb->sb, &iosb->io, &pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800986
Gao Xiang97e86a82019-07-31 23:57:47 +0800987 put_pages_list(&pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800988 kvfree(iosb);
989}
990
Gao Xiang97e86a82019-07-31 23:57:47 +0800991static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
992 unsigned int nr,
993 struct list_head *pagepool,
994 struct address_space *mc,
995 gfp_t gfp)
Gao Xiang9248fce2018-12-08 00:19:15 +0800996{
997 /* determined at compile time to avoid too many #ifdefs */
998 const bool nocache = __builtin_constant_p(mc) ? !mc : false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800999 const pgoff_t index = pcl->obj.index;
Gao Xiang9248fce2018-12-08 00:19:15 +08001000 bool tocache = false;
1001
1002 struct address_space *mapping;
1003 struct page *oldpage, *page;
1004
Gao Xiang92e6efd2018-12-08 00:19:16 +08001005 compressed_page_t t;
1006 int justfound;
1007
Gao Xiang9248fce2018-12-08 00:19:15 +08001008repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +08001009 page = READ_ONCE(pcl->compressed_pages[nr]);
Gao Xiang9248fce2018-12-08 00:19:15 +08001010 oldpage = page;
1011
1012 if (!page)
1013 goto out_allocpage;
1014
1015 /*
1016 * the cached page has not been allocated and
1017 * an placeholder is out there, prepare it now.
1018 */
1019 if (!nocache && page == PAGE_UNALLOCATED) {
1020 tocache = true;
1021 goto out_allocpage;
1022 }
1023
Gao Xiang92e6efd2018-12-08 00:19:16 +08001024 /* process the target tagged pointer */
1025 t = tagptr_init(compressed_page_t, page);
1026 justfound = tagptr_unfold_tags(t);
1027 page = tagptr_unfold_ptr(t);
1028
Gao Xiang9248fce2018-12-08 00:19:15 +08001029 mapping = READ_ONCE(page->mapping);
1030
1031 /*
1032 * if managed cache is disabled, it's no way to
1033 * get such a cached-like page.
1034 */
1035 if (nocache) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001036 /* if managed cache is disabled, it is impossible `justfound' */
1037 DBG_BUGON(justfound);
1038
1039 /* and it should be locked, not uptodate, and not truncated */
Gao Xiang9248fce2018-12-08 00:19:15 +08001040 DBG_BUGON(!PageLocked(page));
1041 DBG_BUGON(PageUptodate(page));
1042 DBG_BUGON(!mapping);
1043 goto out;
1044 }
1045
1046 /*
1047 * unmanaged (file) pages are all locked solidly,
1048 * therefore it is impossible for `mapping' to be NULL.
1049 */
1050 if (mapping && mapping != mc)
1051 /* ought to be unmanaged pages */
1052 goto out;
1053
1054 lock_page(page);
1055
Gao Xiang92e6efd2018-12-08 00:19:16 +08001056 /* only true if page reclaim goes wrong, should never happen */
1057 DBG_BUGON(justfound && PagePrivate(page));
1058
Gao Xiang9248fce2018-12-08 00:19:15 +08001059 /* the page is still in manage cache */
1060 if (page->mapping == mc) {
Gao Xiang97e86a82019-07-31 23:57:47 +08001061 WRITE_ONCE(pcl->compressed_pages[nr], page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001062
Gao Xiang11152492019-03-25 11:40:07 +08001063 ClearPageError(page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001064 if (!PagePrivate(page)) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001065 /*
1066 * impossible to be !PagePrivate(page) for
1067 * the current restriction as well if
1068 * the page is already in compressed_pages[].
1069 */
1070 DBG_BUGON(!justfound);
1071
1072 justfound = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +08001073 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001074 SetPagePrivate(page);
1075 }
1076
1077 /* no need to submit io if it is already up-to-date */
1078 if (PageUptodate(page)) {
1079 unlock_page(page);
1080 page = NULL;
1081 }
1082 goto out;
1083 }
1084
1085 /*
1086 * the managed page has been truncated, it's unsafe to
1087 * reuse this one, let's allocate a new cache-managed page.
1088 */
1089 DBG_BUGON(page->mapping);
Gao Xiang92e6efd2018-12-08 00:19:16 +08001090 DBG_BUGON(!justfound);
Gao Xiang9248fce2018-12-08 00:19:15 +08001091
1092 tocache = true;
1093 unlock_page(page);
1094 put_page(page);
1095out_allocpage:
1096 page = __stagingpage_alloc(pagepool, gfp);
Gao Xiang97e86a82019-07-31 23:57:47 +08001097 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
Gao Xiang9248fce2018-12-08 00:19:15 +08001098 list_add(&page->lru, pagepool);
1099 cpu_relax();
1100 goto repeat;
1101 }
1102 if (nocache || !tocache)
1103 goto out;
1104 if (add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1105 page->mapping = Z_EROFS_MAPPING_STAGING;
1106 goto out;
1107 }
1108
Gao Xiang97e86a82019-07-31 23:57:47 +08001109 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001110 SetPagePrivate(page);
1111out: /* the only exit (for tracing and debugging) */
1112 return page;
1113}
1114
Gao Xiang97e86a82019-07-31 23:57:47 +08001115static struct z_erofs_unzip_io *jobqueue_init(struct super_block *sb,
1116 struct z_erofs_unzip_io *io,
1117 bool foreground)
Gao Xiang3883a792018-07-26 20:22:06 +08001118{
Gao Xiang97e86a82019-07-31 23:57:47 +08001119 struct z_erofs_unzip_io_sb *iosb;
Gao Xiang3883a792018-07-26 20:22:06 +08001120
Gao Xiang7146a4f2018-12-08 00:19:18 +08001121 if (foreground) {
Gao Xiang3883a792018-07-26 20:22:06 +08001122 /* waitqueue available for foreground io */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001123 DBG_BUGON(!io);
Gao Xiang3883a792018-07-26 20:22:06 +08001124
1125 init_waitqueue_head(&io->u.wait);
1126 atomic_set(&io->pending_bios, 0);
1127 goto out;
1128 }
1129
Shobhit Kukretia9f69bd2019-06-26 22:31:18 -07001130 iosb = kvzalloc(sizeof(*iosb), GFP_KERNEL | __GFP_NOFAIL);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001131 DBG_BUGON(!iosb);
Gao Xiang3883a792018-07-26 20:22:06 +08001132
Gao Xiang7146a4f2018-12-08 00:19:18 +08001133 /* initialize fields in the allocated descriptor */
1134 io = &iosb->io;
Gao Xiang3883a792018-07-26 20:22:06 +08001135 iosb->sb = sb;
1136 INIT_WORK(&io->u.work, z_erofs_vle_unzip_wq);
1137out:
Gao Xiang97e86a82019-07-31 23:57:47 +08001138 io->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
Gao Xiang3883a792018-07-26 20:22:06 +08001139 return io;
1140}
1141
Gao Xiang97e86a82019-07-31 23:57:47 +08001142/* define decompression jobqueue types */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001143enum {
Gao Xiang7146a4f2018-12-08 00:19:18 +08001144 JQ_BYPASS,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001145 JQ_SUBMIT,
1146 NR_JOBQUEUES,
1147};
1148
1149static void *jobqueueset_init(struct super_block *sb,
Gao Xiang97e86a82019-07-31 23:57:47 +08001150 z_erofs_next_pcluster_t qtail[],
1151 struct z_erofs_unzip_io *q[],
1152 struct z_erofs_unzip_io *fgq,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001153 bool forcefg)
1154{
Gao Xiang7146a4f2018-12-08 00:19:18 +08001155 /*
1156 * if managed cache is enabled, bypass jobqueue is needed,
Gao Xiang97e86a82019-07-31 23:57:47 +08001157 * no need to read from device for all pclusters in this queue.
Gao Xiang7146a4f2018-12-08 00:19:18 +08001158 */
1159 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, true);
1160 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001161
1162 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, forcefg);
1163 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1164
1165 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], !forcefg));
1166}
1167
Gao Xiang97e86a82019-07-31 23:57:47 +08001168static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1169 z_erofs_next_pcluster_t qtail[],
1170 z_erofs_next_pcluster_t owned_head)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001171{
Gao Xiang97e86a82019-07-31 23:57:47 +08001172 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1173 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001174
Gao Xiang97e86a82019-07-31 23:57:47 +08001175 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1176 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1177 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001178
Gao Xiang97e86a82019-07-31 23:57:47 +08001179 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001180
1181 WRITE_ONCE(*submit_qtail, owned_head);
Gao Xiang97e86a82019-07-31 23:57:47 +08001182 WRITE_ONCE(*bypass_qtail, &pcl->next);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001183
Gao Xiang97e86a82019-07-31 23:57:47 +08001184 qtail[JQ_BYPASS] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001185}
1186
Gao Xiang97e86a82019-07-31 23:57:47 +08001187static bool postsubmit_is_all_bypassed(struct z_erofs_unzip_io *q[],
Gao Xiang7146a4f2018-12-08 00:19:18 +08001188 unsigned int nr_bios,
1189 bool force_fg)
1190{
1191 /*
1192 * although background is preferred, no one is pending for submission.
1193 * don't issue workqueue for decompression but drop it directly instead.
1194 */
1195 if (force_fg || nr_bios)
1196 return false;
1197
Gao Xiang97e86a82019-07-31 23:57:47 +08001198 kvfree(container_of(q[JQ_SUBMIT], struct z_erofs_unzip_io_sb, io));
Gao Xiang7146a4f2018-12-08 00:19:18 +08001199 return true;
1200}
Gao Xiang3883a792018-07-26 20:22:06 +08001201
1202static bool z_erofs_vle_submit_all(struct super_block *sb,
Gao Xiang97e86a82019-07-31 23:57:47 +08001203 z_erofs_next_pcluster_t owned_head,
Gao Xiang3883a792018-07-26 20:22:06 +08001204 struct list_head *pagepool,
Gao Xiang97e86a82019-07-31 23:57:47 +08001205 struct z_erofs_unzip_io *fgq,
Gao Xiang3883a792018-07-26 20:22:06 +08001206 bool force_fg)
1207{
Gao Xiang97e86a82019-07-31 23:57:47 +08001208 struct erofs_sb_info *const sbi __maybe_unused = EROFS_SB(sb);
1209 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1210 struct z_erofs_unzip_io *q[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001211 struct bio *bio;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001212 void *bi_private;
Gao Xiang3883a792018-07-26 20:22:06 +08001213 /* since bio will be NULL, no need to initialize last_index */
1214 pgoff_t uninitialized_var(last_index);
1215 bool force_submit = false;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +02001216 unsigned int nr_bios;
Gao Xiang3883a792018-07-26 20:22:06 +08001217
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001218 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +08001219 return false;
1220
Gao Xiang3883a792018-07-26 20:22:06 +08001221 force_submit = false;
1222 bio = NULL;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001223 nr_bios = 0;
1224 bi_private = jobqueueset_init(sb, qtail, q, fgq, force_fg);
Gao Xiang3883a792018-07-26 20:22:06 +08001225
1226 /* by default, all need io submission */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001227 q[JQ_SUBMIT]->head = owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001228
1229 do {
Gao Xiang97e86a82019-07-31 23:57:47 +08001230 struct z_erofs_pcluster *pcl;
1231 unsigned int clusterpages;
Gao Xiang3883a792018-07-26 20:22:06 +08001232 pgoff_t first_index;
Gao Xiang9248fce2018-12-08 00:19:15 +08001233 struct page *page;
1234 unsigned int i = 0, bypass = 0;
Gao Xiang3883a792018-07-26 20:22:06 +08001235 int err;
1236
1237 /* no possible 'owned_head' equals the following */
Gao Xiang97e86a82019-07-31 23:57:47 +08001238 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1239 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001240
Gao Xiang97e86a82019-07-31 23:57:47 +08001241 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1242
1243 clusterpages = BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +08001244
1245 /* close the main owned chain at first */
Gao Xiang97e86a82019-07-31 23:57:47 +08001246 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1247 Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +08001248
Gao Xiang97e86a82019-07-31 23:57:47 +08001249 first_index = pcl->obj.index;
Gao Xiang3883a792018-07-26 20:22:06 +08001250 force_submit |= (first_index != last_index + 1);
Gao Xiang9248fce2018-12-08 00:19:15 +08001251
Gao Xiang3883a792018-07-26 20:22:06 +08001252repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +08001253 page = pickup_page_for_submission(pcl, i, pagepool,
1254 MNGD_MAPPING(sbi),
1255 GFP_NOFS);
Gao Xiang9248fce2018-12-08 00:19:15 +08001256 if (!page) {
1257 force_submit = true;
1258 ++bypass;
1259 goto skippage;
Gao Xiang3883a792018-07-26 20:22:06 +08001260 }
1261
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001262 if (bio && force_submit) {
Gao Xiang3883a792018-07-26 20:22:06 +08001263submit_bio_retry:
Gao Xiang94e4e152019-09-04 10:09:04 +08001264 submit_bio(bio);
Gao Xiang3883a792018-07-26 20:22:06 +08001265 bio = NULL;
1266 }
1267
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001268 if (!bio) {
Gao Xianga5c0b782019-09-04 10:09:02 +08001269 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1270
1271 bio->bi_end_io = z_erofs_vle_read_endio;
1272 bio_set_dev(bio, sb->s_bdev);
1273 bio->bi_iter.bi_sector = (sector_t)(first_index + i) <<
1274 LOG_SECTORS_PER_BLOCK;
1275 bio->bi_private = bi_private;
Gao Xiang94e4e152019-09-04 10:09:04 +08001276 bio->bi_opf = REQ_OP_READ;
1277
Gao Xiang3883a792018-07-26 20:22:06 +08001278 ++nr_bios;
1279 }
1280
1281 err = bio_add_page(bio, page, PAGE_SIZE, 0);
1282 if (err < PAGE_SIZE)
1283 goto submit_bio_retry;
1284
1285 force_submit = false;
1286 last_index = first_index + i;
Gao Xiang105d4ad2018-07-26 20:22:07 +08001287skippage:
Gao Xiang3883a792018-07-26 20:22:06 +08001288 if (++i < clusterpages)
1289 goto repeat;
Gao Xiang105d4ad2018-07-26 20:22:07 +08001290
Gao Xiang7146a4f2018-12-08 00:19:18 +08001291 if (bypass < clusterpages)
Gao Xiang97e86a82019-07-31 23:57:47 +08001292 qtail[JQ_SUBMIT] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001293 else
Gao Xiang97e86a82019-07-31 23:57:47 +08001294 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1295 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001296
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001297 if (bio)
Gao Xiang94e4e152019-09-04 10:09:04 +08001298 submit_bio(bio);
Gao Xiang3883a792018-07-26 20:22:06 +08001299
Gao Xiang7146a4f2018-12-08 00:19:18 +08001300 if (postsubmit_is_all_bypassed(q, nr_bios, force_fg))
Gao Xiang105d4ad2018-07-26 20:22:07 +08001301 return true;
Gao Xiang3883a792018-07-26 20:22:06 +08001302
Gao Xiang7146a4f2018-12-08 00:19:18 +08001303 z_erofs_vle_unzip_kickoff(bi_private, nr_bios);
Gao Xiang3883a792018-07-26 20:22:06 +08001304 return true;
1305}
1306
Gao Xiang97e86a82019-07-31 23:57:47 +08001307static void z_erofs_submit_and_unzip(struct super_block *sb,
1308 struct z_erofs_collector *clt,
Gao Xiang3883a792018-07-26 20:22:06 +08001309 struct list_head *pagepool,
1310 bool force_fg)
1311{
Gao Xiang97e86a82019-07-31 23:57:47 +08001312 struct z_erofs_unzip_io io[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001313
Gao Xiang97e86a82019-07-31 23:57:47 +08001314 if (!z_erofs_vle_submit_all(sb, clt->owned_head,
1315 pagepool, io, force_fg))
Gao Xiang3883a792018-07-26 20:22:06 +08001316 return;
1317
Gao Xiang97e86a82019-07-31 23:57:47 +08001318 /* decompress no I/O pclusters immediately */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001319 z_erofs_vle_unzip_all(sb, &io[JQ_BYPASS], pagepool);
Gao Xiang4279f3f2019-07-31 23:57:49 +08001320
Gao Xiang3883a792018-07-26 20:22:06 +08001321 if (!force_fg)
1322 return;
1323
1324 /* wait until all bios are completed */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001325 wait_event(io[JQ_SUBMIT].u.wait,
1326 !atomic_read(&io[JQ_SUBMIT].pending_bios));
Gao Xiang3883a792018-07-26 20:22:06 +08001327
1328 /* let's synchronous decompression */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001329 z_erofs_vle_unzip_all(sb, &io[JQ_SUBMIT], pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001330}
1331
1332static int z_erofs_vle_normalaccess_readpage(struct file *file,
1333 struct page *page)
1334{
1335 struct inode *const inode = page->mapping->host;
Gao Xiang97e86a82019-07-31 23:57:47 +08001336 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001337 int err;
1338 LIST_HEAD(pagepool);
1339
Gao Xiangba9ce772018-11-23 01:15:58 +08001340 trace_erofs_readpage(page, false);
1341
Gao Xiangf0c519f2018-11-23 01:21:49 +08001342 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1343
Gao Xiang3883a792018-07-26 20:22:06 +08001344 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xiang97e86a82019-07-31 23:57:47 +08001345 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001346
Gao Xiangee451972019-08-19 18:34:21 +08001347 /* if some compressed cluster ready, need submit them anyway */
Gao Xiang97e86a82019-07-31 23:57:47 +08001348 z_erofs_submit_and_unzip(inode->i_sb, &f.clt, &pagepool, true);
Gao Xiangee451972019-08-19 18:34:21 +08001349
1350 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001351 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
Gao Xiangee451972019-08-19 18:34:21 +08001352
Chao Yu3b423412019-01-15 09:42:21 +08001353 if (f.map.mpage)
1354 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001355
1356 /* clean up the remaining free pages */
1357 put_pages_list(&pagepool);
Gao Xiangee451972019-08-19 18:34:21 +08001358 return err;
Gao Xiang3883a792018-07-26 20:22:06 +08001359}
1360
Gao Xiang14f362b2019-07-31 23:57:36 +08001361static bool should_decompress_synchronously(struct erofs_sb_info *sbi,
1362 unsigned int nr)
1363{
1364 return nr <= sbi->max_sync_decompress_pages;
1365}
1366
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001367static int z_erofs_vle_normalaccess_readpages(struct file *filp,
1368 struct address_space *mapping,
1369 struct list_head *pages,
1370 unsigned int nr_pages)
Gao Xiang3883a792018-07-26 20:22:06 +08001371{
1372 struct inode *const inode = mapping->host;
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001373 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001374
Gao Xiang14f362b2019-07-31 23:57:36 +08001375 bool sync = should_decompress_synchronously(sbi, nr_pages);
Gao Xiang97e86a82019-07-31 23:57:47 +08001376 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001377 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
1378 struct page *head = NULL;
1379 LIST_HEAD(pagepool);
1380
Chen Gong284db122018-09-18 22:27:27 +08001381 trace_erofs_readpages(mapping->host, lru_to_page(pages),
1382 nr_pages, false);
1383
Gao Xiangf0c519f2018-11-23 01:21:49 +08001384 f.headoffset = (erofs_off_t)lru_to_page(pages)->index << PAGE_SHIFT;
1385
Gao Xiang3883a792018-07-26 20:22:06 +08001386 for (; nr_pages; --nr_pages) {
1387 struct page *page = lru_to_page(pages);
1388
1389 prefetchw(&page->flags);
1390 list_del(&page->lru);
1391
Gao Xiang2d9b5dc2018-11-23 01:21:48 +08001392 /*
1393 * A pure asynchronous readahead is indicated if
1394 * a PG_readahead marked page is hitted at first.
1395 * Let's also do asynchronous decompression for this case.
1396 */
1397 sync &= !(PageReadahead(page) && !head);
1398
Gao Xiang3883a792018-07-26 20:22:06 +08001399 if (add_to_page_cache_lru(page, mapping, page->index, gfp)) {
1400 list_add(&page->lru, &pagepool);
1401 continue;
1402 }
1403
Gao Xiang3883a792018-07-26 20:22:06 +08001404 set_page_private(page, (unsigned long)head);
1405 head = page;
1406 }
1407
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001408 while (head) {
Gao Xiang3883a792018-07-26 20:22:06 +08001409 struct page *page = head;
1410 int err;
1411
1412 /* traversal in reverse order */
1413 head = (void *)page_private(page);
1414
1415 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xianga5876e22019-09-04 10:08:56 +08001416 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001417 erofs_err(inode->i_sb,
1418 "readahead error at page %lu @ nid %llu",
1419 page->index, EROFS_I(inode)->nid);
Gao Xiang3883a792018-07-26 20:22:06 +08001420 put_page(page);
1421 }
1422
Gao Xiang97e86a82019-07-31 23:57:47 +08001423 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001424
Gao Xiang97e86a82019-07-31 23:57:47 +08001425 z_erofs_submit_and_unzip(inode->i_sb, &f.clt, &pagepool, sync);
Gao Xiang3883a792018-07-26 20:22:06 +08001426
Chao Yu3b423412019-01-15 09:42:21 +08001427 if (f.map.mpage)
1428 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001429
1430 /* clean up the remaining free pages */
1431 put_pages_list(&pagepool);
1432 return 0;
1433}
1434
Gao Xiang3883a792018-07-26 20:22:06 +08001435const struct address_space_operations z_erofs_vle_normalaccess_aops = {
1436 .readpage = z_erofs_vle_normalaccess_readpage,
1437 .readpages = z_erofs_vle_normalaccess_readpages,
1438};
Gao Xiang02827e12018-07-26 20:21:58 +08001439