blob: 4fedeb4496e41e0f4d50c8c251f8c2251c98a827 [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 Xiang9e579fc2019-10-08 20:56:12 +0800340static int z_erofs_lookup_collection(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;
Gao Xiang3883a792018-07-26 20:22:06 +0800348
Vladimir Zapolskiy997626d2020-01-02 14:01:16 +0200349 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
Gao Xiang97e86a82019-07-31 23:57:47 +0800350 if (!grp)
Gao Xiang9e579fc2019-10-08 20:56:12 +0800351 return -ENOENT;
Gao Xiang97e86a82019-07-31 23:57:47 +0800352
353 pcl = container_of(grp, struct z_erofs_pcluster, obj);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800354 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
355 DBG_BUGON(1);
356 erofs_workgroup_put(grp);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800357 return -EFSCORRUPTED;
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800358 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800359
360 cl = z_erofs_primarycollection(pcl);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800361 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800362 DBG_BUGON(1);
Gao Xiang138e1a02019-08-19 18:34:23 +0800363 erofs_workgroup_put(grp);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800364 return -EFSCORRUPTED;
Gao Xiang3883a792018-07-26 20:22:06 +0800365 }
366
Gao Xiang97e86a82019-07-31 23:57:47 +0800367 length = READ_ONCE(pcl->length);
368 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
369 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
370 DBG_BUGON(1);
Gao Xiang138e1a02019-08-19 18:34:23 +0800371 erofs_workgroup_put(grp);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800372 return -EFSCORRUPTED;
Gao Xiang97e86a82019-07-31 23:57:47 +0800373 }
374 } else {
375 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
Gao Xiang3883a792018-07-26 20:22:06 +0800376
Gao Xiang97e86a82019-07-31 23:57:47 +0800377 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
378 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
Gao Xiang3883a792018-07-26 20:22:06 +0800379
Gao Xiang97e86a82019-07-31 23:57:47 +0800380 while (llen > length &&
381 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
382 cpu_relax();
383 length = READ_ONCE(pcl->length);
384 }
385 }
386 mutex_lock(&cl->lock);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800387 /* used to check tail merging loop due to corrupted images */
388 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
389 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800390 clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800391 /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
392 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
393 clt->tailpcl = NULL;
Gao Xiang97e86a82019-07-31 23:57:47 +0800394 clt->pcl = pcl;
395 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800396 return 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800397}
398
Gao Xiang9e579fc2019-10-08 20:56:12 +0800399static int z_erofs_register_collection(struct z_erofs_collector *clt,
400 struct inode *inode,
401 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800402{
Gao Xiang97e86a82019-07-31 23:57:47 +0800403 struct z_erofs_pcluster *pcl;
404 struct z_erofs_collection *cl;
405 int err;
Gao Xiange5e3abb2018-09-19 13:49:07 +0800406
Gao Xiang3883a792018-07-26 20:22:06 +0800407 /* no available workgroup, let's allocate one */
Gao Xiang97e86a82019-07-31 23:57:47 +0800408 pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800409 if (!pcl)
Gao Xiang9e579fc2019-10-08 20:56:12 +0800410 return -ENOMEM;
Gao Xiang3883a792018-07-26 20:22:06 +0800411
Gao Xiang99634bf2019-09-04 10:09:05 +0800412 z_erofs_pcluster_init_always(pcl);
Gao Xiang97e86a82019-07-31 23:57:47 +0800413 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
Gao Xiang3883a792018-07-26 20:22:06 +0800414
Gao Xiang97e86a82019-07-31 23:57:47 +0800415 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
416 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
417 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800418
Gao Xiang97e86a82019-07-31 23:57:47 +0800419 if (map->m_flags & EROFS_MAP_ZIPPED)
420 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
421 else
422 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
Gao Xiangb6a76182019-06-24 15:22:58 +0800423
Gao Xianga5876e22019-09-04 10:08:56 +0800424 pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
Gao Xiang97e86a82019-07-31 23:57:47 +0800425 pcl->clusterbits -= PAGE_SHIFT;
Gao Xiang3883a792018-07-26 20:22:06 +0800426
Gao Xiang97e86a82019-07-31 23:57:47 +0800427 /* new pclusters should be claimed as type 1, primary and followed */
428 pcl->next = clt->owned_head;
429 clt->mode = COLLECT_PRIMARY_FOLLOWED;
430
431 cl = z_erofs_primarycollection(pcl);
432 cl->pageofs = map->m_la & ~PAGE_MASK;
Gao Xiang3883a792018-07-26 20:22:06 +0800433
Gao Xiang23edf3a2018-11-23 01:21:47 +0800434 /*
435 * lock all primary followed works before visible to others
Gao Xiang97e86a82019-07-31 23:57:47 +0800436 * and mutex_trylock *never* fails for a new pcluster.
Gao Xiang23edf3a2018-11-23 01:21:47 +0800437 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800438 mutex_trylock(&cl->lock);
Gao Xiang23edf3a2018-11-23 01:21:47 +0800439
Vladimir Zapolskiye5e9a432020-01-02 14:01:17 +0200440 err = erofs_register_workgroup(inode->i_sb, &pcl->obj);
Gao Xiang97e86a82019-07-31 23:57:47 +0800441 if (err) {
442 mutex_unlock(&cl->lock);
443 kmem_cache_free(pcluster_cachep, pcl);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800444 return -EAGAIN;
Gao Xiang3883a792018-07-26 20:22:06 +0800445 }
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800446 /* used to check tail merging loop due to corrupted images */
447 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
448 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800449 clt->owned_head = &pcl->next;
450 clt->pcl = pcl;
451 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800452 return 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800453}
454
Gao Xiang97e86a82019-07-31 23:57:47 +0800455static int z_erofs_collector_begin(struct z_erofs_collector *clt,
456 struct inode *inode,
457 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800458{
Gao Xiang9e579fc2019-10-08 20:56:12 +0800459 int ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800460
Gao Xiang97e86a82019-07-31 23:57:47 +0800461 DBG_BUGON(clt->cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800462
Gao Xiang97e86a82019-07-31 23:57:47 +0800463 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
464 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
465 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +0800466
Gao Xiang97e86a82019-07-31 23:57:47 +0800467 if (!PAGE_ALIGNED(map->m_pa)) {
468 DBG_BUGON(1);
469 return -EINVAL;
470 }
Gao Xiang3883a792018-07-26 20:22:06 +0800471
472repeat:
Gao Xiang9e579fc2019-10-08 20:56:12 +0800473 ret = z_erofs_lookup_collection(clt, inode, map);
474 if (ret == -ENOENT) {
475 ret = z_erofs_register_collection(clt, inode, map);
Gao Xiangb27661c2018-09-19 13:49:06 +0800476
Gao Xiang9e579fc2019-10-08 20:56:12 +0800477 /* someone registered at the same time, give another try */
478 if (ret == -EAGAIN) {
479 cond_resched();
Gao Xiang97e86a82019-07-31 23:57:47 +0800480 goto repeat;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800481 }
Gao Xiang3883a792018-07-26 20:22:06 +0800482 }
483
Gao Xiang9e579fc2019-10-08 20:56:12 +0800484 if (ret)
485 return ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800486
Gao Xiang97e86a82019-07-31 23:57:47 +0800487 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang9e579fc2019-10-08 20:56:12 +0800488 clt->cl->pagevec, clt->cl->vcnt);
Gao Xiang3883a792018-07-26 20:22:06 +0800489
Gao Xiang97e86a82019-07-31 23:57:47 +0800490 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;
Gao Xiang3883a792018-07-26 20:22:06 +0800493 return 0;
494}
495
496/*
Gao Xiang97e86a82019-07-31 23:57:47 +0800497 * keep in mind that no referenced pclusters will be freed
498 * only after a RCU grace period.
Gao Xiang3883a792018-07-26 20:22:06 +0800499 */
500static void z_erofs_rcu_callback(struct rcu_head *head)
501{
Gao Xiang97e86a82019-07-31 23:57:47 +0800502 struct z_erofs_collection *const cl =
503 container_of(head, struct z_erofs_collection, rcu);
Gao Xiang3883a792018-07-26 20:22:06 +0800504
Gao Xiang97e86a82019-07-31 23:57:47 +0800505 kmem_cache_free(pcluster_cachep,
506 container_of(cl, struct z_erofs_pcluster,
507 primary_collection));
Gao Xiang3883a792018-07-26 20:22:06 +0800508}
509
510void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
511{
Gao Xiang97e86a82019-07-31 23:57:47 +0800512 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);
Gao Xiang3883a792018-07-26 20:22:06 +0800515
Gao Xiang97e86a82019-07-31 23:57:47 +0800516 call_rcu(&cl->rcu, z_erofs_rcu_callback);
Gao Xiang3883a792018-07-26 20:22:06 +0800517}
518
Gao Xiang97e86a82019-07-31 23:57:47 +0800519static void z_erofs_collection_put(struct z_erofs_collection *cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800520{
Gao Xiang97e86a82019-07-31 23:57:47 +0800521 struct z_erofs_pcluster *const pcl =
522 container_of(cl, struct z_erofs_pcluster, primary_collection);
523
524 erofs_workgroup_put(&pcl->obj);
Gao Xiang3883a792018-07-26 20:22:06 +0800525}
526
Gao Xiang97e86a82019-07-31 23:57:47 +0800527static bool z_erofs_collector_end(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800528{
Gao Xiang97e86a82019-07-31 23:57:47 +0800529 struct z_erofs_collection *cl = clt->cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800530
Gao Xiang97e86a82019-07-31 23:57:47 +0800531 if (!cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800532 return false;
533
Gao Xiang97e86a82019-07-31 23:57:47 +0800534 z_erofs_pagevec_ctor_exit(&clt->vector, false);
535 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800536
537 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800538 * if all pending pages are added, don't hold its reference
539 * any longer if the pcluster isn't hosted by ourselves.
Gao Xiang3883a792018-07-26 20:22:06 +0800540 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800541 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
542 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800543
Gao Xiang97e86a82019-07-31 23:57:47 +0800544 clt->cl = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +0800545 return true;
546}
547
Gao Xiang97e86a82019-07-31 23:57:47 +0800548static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800549 unsigned int cachestrategy,
Gao Xiang97e86a82019-07-31 23:57:47 +0800550 erofs_off_t la)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800551{
Gao Xiang4279f3f2019-07-31 23:57:49 +0800552 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
553 return false;
554
Gao Xiang92e6efd2018-12-08 00:19:16 +0800555 if (fe->backmost)
556 return true;
557
Gao Xiang4279f3f2019-07-31 23:57:49 +0800558 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
559 la < fe->headoffset;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800560}
Gao Xiang92e6efd2018-12-08 00:19:16 +0800561
Gao Xiang97e86a82019-07-31 23:57:47 +0800562static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
Gao Xiang3883a792018-07-26 20:22:06 +0800563 struct page *page,
Gao Xiang97e86a82019-07-31 23:57:47 +0800564 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800565{
Gao Xiang97e86a82019-07-31 23:57:47 +0800566 struct inode *const inode = fe->inode;
Gao Xiangbda17a42019-10-08 20:56:13 +0800567 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Chao Yu3b423412019-01-15 09:42:21 +0800568 struct erofs_map_blocks *const map = &fe->map;
Gao Xiang97e86a82019-07-31 23:57:47 +0800569 struct z_erofs_collector *const clt = &fe->clt;
Gao Xiang3883a792018-07-26 20:22:06 +0800570 const loff_t offset = page_offset(page);
Gao Xiangdc76ea82019-09-22 18:04:34 +0800571 bool tight = true;
Gao Xiang3883a792018-07-26 20:22:06 +0800572
Gao Xiang92e6efd2018-12-08 00:19:16 +0800573 enum z_erofs_cache_alloctype cache_strategy;
Gao Xiang3883a792018-07-26 20:22:06 +0800574 enum z_erofs_page_type page_type;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200575 unsigned int cur, end, spiltted, index;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800576 int err = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800577
578 /* register locked file pages as online pages in pack */
579 z_erofs_onlinepage_init(page);
580
581 spiltted = 0;
582 end = PAGE_SIZE;
583repeat:
584 cur = end - 1;
585
586 /* lucky, within the range of the current map_blocks */
587 if (offset + cur >= map->m_la &&
Julian Merida447a3622019-03-18 20:58:41 -0300588 offset + cur < map->m_la + map->m_llen) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800589 /* didn't get a valid collection previously (very rare) */
590 if (!clt->cl)
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800591 goto restart_now;
Gao Xiang3883a792018-07-26 20:22:06 +0800592 goto hitted;
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800593 }
Gao Xiang3883a792018-07-26 20:22:06 +0800594
595 /* go ahead the next map_blocks */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800596 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
Gao Xiang3883a792018-07-26 20:22:06 +0800597
Gao Xiang97e86a82019-07-31 23:57:47 +0800598 if (z_erofs_collector_end(clt))
Gao Xiangf0c519f2018-11-23 01:21:49 +0800599 fe->backmost = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800600
601 map->m_la = offset + cur;
602 map->m_llen = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +0800603 err = z_erofs_map_blocks_iter(inode, map, 0);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800604 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800605 goto err_out;
606
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800607restart_now:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800608 if (!(map->m_flags & EROFS_MAP_MAPPED))
Gao Xiang3883a792018-07-26 20:22:06 +0800609 goto hitted;
610
Gao Xiang97e86a82019-07-31 23:57:47 +0800611 err = z_erofs_collector_begin(clt, inode, map);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800612 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800613 goto err_out;
614
Gao Xiang92e6efd2018-12-08 00:19:16 +0800615 /* preload all compressed pages (maybe downgrade role if necessary) */
Gao Xiang4279f3f2019-07-31 23:57:49 +0800616 if (should_alloc_managed_pages(fe, sbi->cache_strategy, map->m_la))
Gao Xiang92e6efd2018-12-08 00:19:16 +0800617 cache_strategy = DELAYEDALLOC;
618 else
619 cache_strategy = DONTALLOC;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800620
Gao Xiang97e86a82019-07-31 23:57:47 +0800621 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
622 cache_strategy, pagepool);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800623
Gao Xiang3883a792018-07-26 20:22:06 +0800624hitted:
Gao Xiangdc76ea82019-09-22 18:04:34 +0800625 /*
626 * Ensure the current partial page belongs to this submit chain rather
627 * than other concurrent submit chains or the noio(bypass) chain since
628 * those chains are handled asynchronously thus the page cannot be used
629 * for inplace I/O or pagevec (should be processed in strict order.)
630 */
631 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
632 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
633
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200634 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800635 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800636 zero_user_segment(page, cur, end);
637 goto next_part;
638 }
639
640 /* let's derive page type */
641 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
642 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
643 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
644 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
645
Gao Xianga1121522019-02-27 13:33:32 +0800646 if (cur)
Gao Xiang97e86a82019-07-31 23:57:47 +0800647 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xianga1121522019-02-27 13:33:32 +0800648
Gao Xiang3883a792018-07-26 20:22:06 +0800649retry:
Gao Xiang97e86a82019-07-31 23:57:47 +0800650 err = z_erofs_attach_page(clt, page, page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800651 /* should allocate an additional staging page for pagevec */
652 if (err == -EAGAIN) {
653 struct page *const newpage =
Gao Xiang5ddcee12019-11-21 21:59:54 +0800654 erofs_allocpage(pagepool, GFP_NOFS | __GFP_NOFAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800655
Gao Xiang5ddcee12019-11-21 21:59:54 +0800656 newpage->mapping = Z_EROFS_MAPPING_STAGING;
Gao Xiang97e86a82019-07-31 23:57:47 +0800657 err = z_erofs_attach_page(clt, newpage,
658 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800659 if (!err)
Gao Xiang3883a792018-07-26 20:22:06 +0800660 goto retry;
661 }
662
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800663 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800664 goto err_out;
665
Gao Xiang97e86a82019-07-31 23:57:47 +0800666 index = page->index - (map->m_la >> PAGE_SHIFT);
Gao Xiang3883a792018-07-26 20:22:06 +0800667
Gao Xiang3883a792018-07-26 20:22:06 +0800668 z_erofs_onlinepage_fixup(page, index, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800669
Gao Xiang1e05ff32018-09-18 22:27:25 +0800670 /* bump up the number of spiltted parts of a page */
671 ++spiltted;
672 /* also update nr_pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800673 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
Gao Xiang3883a792018-07-26 20:22:06 +0800674next_part:
675 /* can be used for verification */
676 map->m_llen = offset + cur - map->m_la;
677
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300678 end = cur;
679 if (end > 0)
Gao Xiang3883a792018-07-26 20:22:06 +0800680 goto repeat;
681
Gao Xiang1e05ff32018-09-18 22:27:25 +0800682out:
Gao Xiang3883a792018-07-26 20:22:06 +0800683 z_erofs_onlinepage_endio(page);
684
Gao Xiang4f761fa2019-09-04 10:09:09 +0800685 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
686 __func__, page, spiltted, map->m_llen);
Gao Xiang3883a792018-07-26 20:22:06 +0800687 return err;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800688
689 /* if some error occurred while processing this page */
690err_out:
691 SetPageError(page);
692 goto out;
Gao Xiang3883a792018-07-26 20:22:06 +0800693}
694
Gao Xianga4b1fab2019-10-08 20:56:15 +0800695static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
696 bool sync, int bios)
Gao Xiang3883a792018-07-26 20:22:06 +0800697{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800698 /* wake up the caller thread for sync decompression */
699 if (sync) {
Gao Xiang848bd9a2018-12-08 00:19:12 +0800700 unsigned long flags;
Gao Xiang3883a792018-07-26 20:22:06 +0800701
Gao Xiang848bd9a2018-12-08 00:19:12 +0800702 spin_lock_irqsave(&io->u.wait.lock, flags);
703 if (!atomic_add_return(bios, &io->pending_bios))
704 wake_up_locked(&io->u.wait);
705 spin_unlock_irqrestore(&io->u.wait.lock, flags);
706 return;
707 }
708
709 if (!atomic_add_return(bios, &io->pending_bios))
Gao Xiang3883a792018-07-26 20:22:06 +0800710 queue_work(z_erofs_workqueue, &io->u.work);
Gao Xiang3883a792018-07-26 20:22:06 +0800711}
712
Gao Xiang0c638f72019-11-08 11:37:33 +0800713static void z_erofs_decompressqueue_endio(struct bio *bio)
Gao Xiang3883a792018-07-26 20:22:06 +0800714{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800715 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
716 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
Gao Xiang14a56ec2019-03-25 11:40:09 +0800717 blk_status_t err = bio->bi_status;
Gao Xiang3883a792018-07-26 20:22:06 +0800718 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800719 struct bvec_iter_all iter_all;
Gao Xiang3883a792018-07-26 20:22:06 +0800720
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200721 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang3883a792018-07-26 20:22:06 +0800722 struct page *page = bvec->bv_page;
723
724 DBG_BUGON(PageUptodate(page));
Gao Xiang70b17992018-12-11 15:17:49 +0800725 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800726
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800727 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800728 SetPageError(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800729
Gao Xianga4b1fab2019-10-08 20:56:15 +0800730 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
731 if (!err)
732 SetPageUptodate(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800733 unlock_page(page);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800734 }
Gao Xiang3883a792018-07-26 20:22:06 +0800735 }
Gao Xianga4b1fab2019-10-08 20:56:15 +0800736 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
Gao Xiang3883a792018-07-26 20:22:06 +0800737 bio_put(bio);
738}
739
Gao Xiang97e86a82019-07-31 23:57:47 +0800740static int z_erofs_decompress_pcluster(struct super_block *sb,
741 struct z_erofs_pcluster *pcl,
742 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800743{
744 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +0800745 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +0800746 struct z_erofs_pagevec_ctor ctor;
Gao Xiang97e86a82019-07-31 23:57:47 +0800747 unsigned int i, outputsize, llen, nr_pages;
748 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
Gao Xiang3883a792018-07-26 20:22:06 +0800749 struct page **pages, **compressed_pages, *page;
Gao Xiang3883a792018-07-26 20:22:06 +0800750
751 enum z_erofs_page_type page_type;
Gao Xiangb6a76182019-06-24 15:22:58 +0800752 bool overlapped, partial;
Gao Xiang97e86a82019-07-31 23:57:47 +0800753 struct z_erofs_collection *cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800754 int err;
755
756 might_sleep();
Gao Xiang97e86a82019-07-31 23:57:47 +0800757 cl = z_erofs_primarycollection(pcl);
758 DBG_BUGON(!READ_ONCE(cl->nr_pages));
Gao Xiang3883a792018-07-26 20:22:06 +0800759
Gao Xiang97e86a82019-07-31 23:57:47 +0800760 mutex_lock(&cl->lock);
761 nr_pages = cl->nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800762
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800763 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
Gao Xiang3883a792018-07-26 20:22:06 +0800764 pages = pages_onstack;
Gao Xiang97e86a82019-07-31 23:57:47 +0800765 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
766 mutex_trylock(&z_pagemap_global_lock)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800767 pages = z_pagemap_global;
Gao Xiang97e86a82019-07-31 23:57:47 +0800768 } else {
Chao Yu441dfcc2019-07-16 17:44:22 +0800769 gfp_t gfp_flags = GFP_KERNEL;
770
Gao Xiang97e86a82019-07-31 23:57:47 +0800771 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
Chao Yu441dfcc2019-07-16 17:44:22 +0800772 gfp_flags |= __GFP_NOFAIL;
773
Julian Merida447a3622019-03-18 20:58:41 -0300774 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Chao Yu441dfcc2019-07-16 17:44:22 +0800775 gfp_flags);
Gao Xiang3883a792018-07-26 20:22:06 +0800776
777 /* fallback to global pagemap for the lowmem scenario */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800778 if (!pages) {
Chao Yu441dfcc2019-07-16 17:44:22 +0800779 mutex_lock(&z_pagemap_global_lock);
780 pages = z_pagemap_global;
Gao Xiang3883a792018-07-26 20:22:06 +0800781 }
782 }
783
784 for (i = 0; i < nr_pages; ++i)
785 pages[i] = NULL;
786
Gao Xiange12a0ce2019-08-21 22:01:52 +0800787 err = 0;
Gao Xiangfa61a332019-06-24 15:22:53 +0800788 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang97e86a82019-07-31 23:57:47 +0800789 cl->pagevec, 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800790
Gao Xiang97e86a82019-07-31 23:57:47 +0800791 for (i = 0; i < cl->vcnt; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200792 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800793
Gao Xiang046d64e2019-07-31 23:57:45 +0800794 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800795
796 /* all pages in pagevec ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100797 DBG_BUGON(!page);
798 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800799
Gao Xiang97e86a82019-07-31 23:57:47 +0800800 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800801 continue;
802
803 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
804 pagenr = 0;
805 else
806 pagenr = z_erofs_onlinepage_index(page);
807
Gao Xiang70b17992018-12-11 15:17:49 +0800808 DBG_BUGON(pagenr >= nr_pages);
Gao Xiange5e3abb2018-09-19 13:49:07 +0800809
Gao Xiange12a0ce2019-08-21 22:01:52 +0800810 /*
811 * currently EROFS doesn't support multiref(dedup),
812 * so here erroring out one multiref page.
813 */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800814 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800815 DBG_BUGON(1);
816 SetPageError(pages[pagenr]);
817 z_erofs_onlinepage_endio(pages[pagenr]);
818 err = -EFSCORRUPTED;
819 }
Gao Xiang3883a792018-07-26 20:22:06 +0800820 pages[pagenr] = page;
821 }
Gao Xiang3883a792018-07-26 20:22:06 +0800822 z_erofs_pagevec_ctor_exit(&ctor, true);
823
824 overlapped = false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800825 compressed_pages = pcl->compressed_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800826
827 for (i = 0; i < clusterpages; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200828 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800829
830 page = compressed_pages[i];
831
832 /* all compressed pages ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100833 DBG_BUGON(!page);
834 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800835
Gao Xiang27481232019-06-24 15:22:54 +0800836 if (!z_erofs_page_is_staging(page)) {
Gao Xiangd61fbb62019-03-25 11:40:08 +0800837 if (erofs_page_is_managed(sbi, page)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800838 if (!PageUptodate(page))
Gao Xiang11152492019-03-25 11:40:07 +0800839 err = -EIO;
840 continue;
841 }
Gao Xiang3883a792018-07-26 20:22:06 +0800842
Gao Xiang11152492019-03-25 11:40:07 +0800843 /*
844 * only if non-head page can be selected
845 * for inplace decompression
846 */
847 pagenr = z_erofs_onlinepage_index(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800848
Gao Xiang11152492019-03-25 11:40:07 +0800849 DBG_BUGON(pagenr >= nr_pages);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800850 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800851 DBG_BUGON(1);
852 SetPageError(pages[pagenr]);
853 z_erofs_onlinepage_endio(pages[pagenr]);
854 err = -EFSCORRUPTED;
855 }
Gao Xiang11152492019-03-25 11:40:07 +0800856 pages[pagenr] = page;
Gao Xiang3883a792018-07-26 20:22:06 +0800857
Gao Xiang11152492019-03-25 11:40:07 +0800858 overlapped = true;
859 }
860
861 /* PG_error needs checking for inplaced and staging pages */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800862 if (PageError(page)) {
Gao Xiang11152492019-03-25 11:40:07 +0800863 DBG_BUGON(PageUptodate(page));
864 err = -EIO;
865 }
Gao Xiang3883a792018-07-26 20:22:06 +0800866 }
867
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800868 if (err)
Gao Xiang11152492019-03-25 11:40:07 +0800869 goto out;
870
Gao Xiang97e86a82019-07-31 23:57:47 +0800871 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
872 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
873 outputsize = llen;
874 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
Gao Xiangb6a76182019-06-24 15:22:58 +0800875 } else {
Gao Xiang97e86a82019-07-31 23:57:47 +0800876 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
Gao Xiangb6a76182019-06-24 15:22:58 +0800877 partial = true;
878 }
Gao Xiang3883a792018-07-26 20:22:06 +0800879
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800880 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
881 .sb = sb,
882 .in = compressed_pages,
883 .out = pages,
Gao Xiang97e86a82019-07-31 23:57:47 +0800884 .pageofs_out = cl->pageofs,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800885 .inputsize = PAGE_SIZE,
886 .outputsize = outputsize,
Gao Xiang97e86a82019-07-31 23:57:47 +0800887 .alg = pcl->algorithmformat,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800888 .inplace_io = overlapped,
Gao Xiangb6a76182019-06-24 15:22:58 +0800889 .partial_decoding = partial
Gao Xiang97e86a82019-07-31 23:57:47 +0800890 }, pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800891
892out:
Gao Xiangaf692e12019-02-27 13:33:30 +0800893 /* must handle all compressed pages before endding pages */
Gao Xiang3883a792018-07-26 20:22:06 +0800894 for (i = 0; i < clusterpages; ++i) {
895 page = compressed_pages[i];
896
Gao Xiangd61fbb62019-03-25 11:40:08 +0800897 if (erofs_page_is_managed(sbi, page))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800898 continue;
Gao Xiangd61fbb62019-03-25 11:40:08 +0800899
Gao Xiang3883a792018-07-26 20:22:06 +0800900 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800901 (void)z_erofs_put_stagingpage(pagepool, page);
Gao Xiang3883a792018-07-26 20:22:06 +0800902
903 WRITE_ONCE(compressed_pages[i], NULL);
904 }
905
Gao Xiangaf692e12019-02-27 13:33:30 +0800906 for (i = 0; i < nr_pages; ++i) {
907 page = pages[i];
908 if (!page)
909 continue;
910
911 DBG_BUGON(!page->mapping);
912
913 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800914 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiangaf692e12019-02-27 13:33:30 +0800915 continue;
916
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800917 if (err < 0)
Gao Xiangaf692e12019-02-27 13:33:30 +0800918 SetPageError(page);
919
920 z_erofs_onlinepage_endio(page);
921 }
922
Gao Xiang3883a792018-07-26 20:22:06 +0800923 if (pages == z_pagemap_global)
924 mutex_unlock(&z_pagemap_global_lock);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800925 else if (pages != pages_onstack)
Gao Xiang3883a792018-07-26 20:22:06 +0800926 kvfree(pages);
927
Gao Xiang97e86a82019-07-31 23:57:47 +0800928 cl->nr_pages = 0;
929 cl->vcnt = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800930
Gao Xiang97e86a82019-07-31 23:57:47 +0800931 /* all cl locks MUST be taken before the following line */
932 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800933
Gao Xiang97e86a82019-07-31 23:57:47 +0800934 /* all cl locks SHOULD be released right now */
935 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800936
Gao Xiang97e86a82019-07-31 23:57:47 +0800937 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800938 return err;
939}
940
Gao Xiang0c638f72019-11-08 11:37:33 +0800941static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
942 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800943{
Gao Xiang97e86a82019-07-31 23:57:47 +0800944 z_erofs_next_pcluster_t owned = io->head;
Gao Xiang3883a792018-07-26 20:22:06 +0800945
Gao Xiang97e86a82019-07-31 23:57:47 +0800946 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
947 struct z_erofs_pcluster *pcl;
Gao Xiang3883a792018-07-26 20:22:06 +0800948
949 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800950 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800951
952 /* no possible that 'owned' equals NULL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800953 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800954
Gao Xiang97e86a82019-07-31 23:57:47 +0800955 pcl = container_of(owned, struct z_erofs_pcluster, next);
956 owned = READ_ONCE(pcl->next);
Gao Xiang3883a792018-07-26 20:22:06 +0800957
Gao Xianga4b1fab2019-10-08 20:56:15 +0800958 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
Gao Xiang3978c8e2018-08-06 11:27:53 +0800959 }
Gao Xiang3883a792018-07-26 20:22:06 +0800960}
961
Gao Xiang0c638f72019-11-08 11:37:33 +0800962static void z_erofs_decompressqueue_work(struct work_struct *work)
Gao Xiang3883a792018-07-26 20:22:06 +0800963{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800964 struct z_erofs_decompressqueue *bgq =
965 container_of(work, struct z_erofs_decompressqueue, u.work);
Gao Xiang97e86a82019-07-31 23:57:47 +0800966 LIST_HEAD(pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800967
Gao Xianga4b1fab2019-10-08 20:56:15 +0800968 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang0c638f72019-11-08 11:37:33 +0800969 z_erofs_decompress_queue(bgq, &pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800970
Gao Xiang97e86a82019-07-31 23:57:47 +0800971 put_pages_list(&pagepool);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800972 kvfree(bgq);
Gao Xiang3883a792018-07-26 20:22:06 +0800973}
974
Gao Xiang97e86a82019-07-31 23:57:47 +0800975static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
976 unsigned int nr,
977 struct list_head *pagepool,
978 struct address_space *mc,
979 gfp_t gfp)
Gao Xiang9248fce2018-12-08 00:19:15 +0800980{
Gao Xiang97e86a82019-07-31 23:57:47 +0800981 const pgoff_t index = pcl->obj.index;
Gao Xiang9248fce2018-12-08 00:19:15 +0800982 bool tocache = false;
983
984 struct address_space *mapping;
985 struct page *oldpage, *page;
986
Gao Xiang92e6efd2018-12-08 00:19:16 +0800987 compressed_page_t t;
988 int justfound;
989
Gao Xiang9248fce2018-12-08 00:19:15 +0800990repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +0800991 page = READ_ONCE(pcl->compressed_pages[nr]);
Gao Xiang9248fce2018-12-08 00:19:15 +0800992 oldpage = page;
993
994 if (!page)
995 goto out_allocpage;
996
997 /*
998 * the cached page has not been allocated and
999 * an placeholder is out there, prepare it now.
1000 */
Gao Xiangbda17a42019-10-08 20:56:13 +08001001 if (page == PAGE_UNALLOCATED) {
Gao Xiang9248fce2018-12-08 00:19:15 +08001002 tocache = true;
1003 goto out_allocpage;
1004 }
1005
Gao Xiang92e6efd2018-12-08 00:19:16 +08001006 /* process the target tagged pointer */
1007 t = tagptr_init(compressed_page_t, page);
1008 justfound = tagptr_unfold_tags(t);
1009 page = tagptr_unfold_ptr(t);
1010
Gao Xiang9248fce2018-12-08 00:19:15 +08001011 mapping = READ_ONCE(page->mapping);
1012
1013 /*
Gao Xiang9248fce2018-12-08 00:19:15 +08001014 * unmanaged (file) pages are all locked solidly,
1015 * therefore it is impossible for `mapping' to be NULL.
1016 */
1017 if (mapping && mapping != mc)
1018 /* ought to be unmanaged pages */
1019 goto out;
1020
1021 lock_page(page);
1022
Gao Xiang92e6efd2018-12-08 00:19:16 +08001023 /* only true if page reclaim goes wrong, should never happen */
1024 DBG_BUGON(justfound && PagePrivate(page));
1025
Gao Xiang9248fce2018-12-08 00:19:15 +08001026 /* the page is still in manage cache */
1027 if (page->mapping == mc) {
Gao Xiang97e86a82019-07-31 23:57:47 +08001028 WRITE_ONCE(pcl->compressed_pages[nr], page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001029
Gao Xiang11152492019-03-25 11:40:07 +08001030 ClearPageError(page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001031 if (!PagePrivate(page)) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001032 /*
1033 * impossible to be !PagePrivate(page) for
1034 * the current restriction as well if
1035 * the page is already in compressed_pages[].
1036 */
1037 DBG_BUGON(!justfound);
1038
1039 justfound = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +08001040 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001041 SetPagePrivate(page);
1042 }
1043
1044 /* no need to submit io if it is already up-to-date */
1045 if (PageUptodate(page)) {
1046 unlock_page(page);
1047 page = NULL;
1048 }
1049 goto out;
1050 }
1051
1052 /*
1053 * the managed page has been truncated, it's unsafe to
1054 * reuse this one, let's allocate a new cache-managed page.
1055 */
1056 DBG_BUGON(page->mapping);
Gao Xiang92e6efd2018-12-08 00:19:16 +08001057 DBG_BUGON(!justfound);
Gao Xiang9248fce2018-12-08 00:19:15 +08001058
1059 tocache = true;
1060 unlock_page(page);
1061 put_page(page);
1062out_allocpage:
Gao Xiang5ddcee12019-11-21 21:59:54 +08001063 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1064 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1065 /* non-LRU / non-movable temporary page is needed */
Gao Xiang9248fce2018-12-08 00:19:15 +08001066 page->mapping = Z_EROFS_MAPPING_STAGING;
Gao Xiang5ddcee12019-11-21 21:59:54 +08001067 tocache = false;
Gao Xiang9248fce2018-12-08 00:19:15 +08001068 }
1069
Gao Xiang5ddcee12019-11-21 21:59:54 +08001070 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1071 if (tocache) {
1072 /* since it added to managed cache successfully */
1073 unlock_page(page);
1074 put_page(page);
1075 } else {
1076 list_add(&page->lru, pagepool);
1077 }
1078 cond_resched();
1079 goto repeat;
1080 }
Gao Xiang97e86a82019-07-31 23:57:47 +08001081 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001082 SetPagePrivate(page);
1083out: /* the only exit (for tracing and debugging) */
1084 return page;
1085}
1086
Gao Xianga4b1fab2019-10-08 20:56:15 +08001087static struct z_erofs_decompressqueue *
1088jobqueue_init(struct super_block *sb,
1089 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001090{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001091 struct z_erofs_decompressqueue *q;
Gao Xiang3883a792018-07-26 20:22:06 +08001092
Gao Xianga4b1fab2019-10-08 20:56:15 +08001093 if (fg && !*fg) {
1094 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1095 if (!q) {
1096 *fg = true;
1097 goto fg_out;
1098 }
Gao Xiang0c638f72019-11-08 11:37:33 +08001099 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
Gao Xianga4b1fab2019-10-08 20:56:15 +08001100 } else {
1101fg_out:
1102 q = fgq;
1103 init_waitqueue_head(&fgq->u.wait);
1104 atomic_set(&fgq->pending_bios, 0);
Gao Xiang3883a792018-07-26 20:22:06 +08001105 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001106 q->sb = sb;
1107 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1108 return q;
Gao Xiang3883a792018-07-26 20:22:06 +08001109}
1110
Gao Xiang97e86a82019-07-31 23:57:47 +08001111/* define decompression jobqueue types */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001112enum {
Gao Xiang7146a4f2018-12-08 00:19:18 +08001113 JQ_BYPASS,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001114 JQ_SUBMIT,
1115 NR_JOBQUEUES,
1116};
1117
1118static void *jobqueueset_init(struct super_block *sb,
Gao Xianga4b1fab2019-10-08 20:56:15 +08001119 struct z_erofs_decompressqueue *q[],
1120 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001121{
Gao Xiang7146a4f2018-12-08 00:19:18 +08001122 /*
1123 * if managed cache is enabled, bypass jobqueue is needed,
Gao Xiang97e86a82019-07-31 23:57:47 +08001124 * no need to read from device for all pclusters in this queue.
Gao Xiang7146a4f2018-12-08 00:19:18 +08001125 */
Gao Xianga4b1fab2019-10-08 20:56:15 +08001126 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1127 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001128
Gao Xianga4b1fab2019-10-08 20:56:15 +08001129 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
Gao Xiang7146a4f2018-12-08 00:19:18 +08001130}
1131
Gao Xiang97e86a82019-07-31 23:57:47 +08001132static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1133 z_erofs_next_pcluster_t qtail[],
1134 z_erofs_next_pcluster_t owned_head)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001135{
Gao Xiang97e86a82019-07-31 23:57:47 +08001136 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1137 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001138
Gao Xiang97e86a82019-07-31 23:57:47 +08001139 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1140 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1141 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001142
Gao Xiang97e86a82019-07-31 23:57:47 +08001143 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001144
1145 WRITE_ONCE(*submit_qtail, owned_head);
Gao Xiang97e86a82019-07-31 23:57:47 +08001146 WRITE_ONCE(*bypass_qtail, &pcl->next);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001147
Gao Xiang97e86a82019-07-31 23:57:47 +08001148 qtail[JQ_BYPASS] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001149}
1150
Gao Xianga4b1fab2019-10-08 20:56:15 +08001151static bool postsubmit_is_all_bypassed(struct z_erofs_decompressqueue *q[],
1152 unsigned int nr_bios, bool force_fg)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001153{
1154 /*
1155 * although background is preferred, no one is pending for submission.
1156 * don't issue workqueue for decompression but drop it directly instead.
1157 */
1158 if (force_fg || nr_bios)
1159 return false;
1160
Gao Xianga4b1fab2019-10-08 20:56:15 +08001161 kvfree(q[JQ_SUBMIT]);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001162 return true;
1163}
Gao Xiang3883a792018-07-26 20:22:06 +08001164
Gao Xiang0c638f72019-11-08 11:37:33 +08001165static bool z_erofs_submit_queue(struct super_block *sb,
1166 z_erofs_next_pcluster_t owned_head,
1167 struct list_head *pagepool,
1168 struct z_erofs_decompressqueue *fgq,
1169 bool *force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001170{
Gao Xiangbda17a42019-10-08 20:56:13 +08001171 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +08001172 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
Gao Xianga4b1fab2019-10-08 20:56:15 +08001173 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001174 struct bio *bio;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001175 void *bi_private;
Gao Xiang3883a792018-07-26 20:22:06 +08001176 /* since bio will be NULL, no need to initialize last_index */
1177 pgoff_t uninitialized_var(last_index);
1178 bool force_submit = false;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +02001179 unsigned int nr_bios;
Gao Xiang3883a792018-07-26 20:22:06 +08001180
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001181 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +08001182 return false;
1183
Gao Xiang3883a792018-07-26 20:22:06 +08001184 force_submit = false;
1185 bio = NULL;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001186 nr_bios = 0;
Gao Xianga4b1fab2019-10-08 20:56:15 +08001187 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1188 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1189 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
Gao Xiang3883a792018-07-26 20:22:06 +08001190
1191 /* by default, all need io submission */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001192 q[JQ_SUBMIT]->head = owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001193
1194 do {
Gao Xiang97e86a82019-07-31 23:57:47 +08001195 struct z_erofs_pcluster *pcl;
1196 unsigned int clusterpages;
Gao Xiang3883a792018-07-26 20:22:06 +08001197 pgoff_t first_index;
Gao Xiang9248fce2018-12-08 00:19:15 +08001198 struct page *page;
1199 unsigned int i = 0, bypass = 0;
Gao Xiang3883a792018-07-26 20:22:06 +08001200 int err;
1201
1202 /* no possible 'owned_head' equals the following */
Gao Xiang97e86a82019-07-31 23:57:47 +08001203 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1204 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001205
Gao Xiang97e86a82019-07-31 23:57:47 +08001206 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1207
1208 clusterpages = BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +08001209
1210 /* close the main owned chain at first */
Gao Xiang97e86a82019-07-31 23:57:47 +08001211 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1212 Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +08001213
Gao Xiang97e86a82019-07-31 23:57:47 +08001214 first_index = pcl->obj.index;
Gao Xiang3883a792018-07-26 20:22:06 +08001215 force_submit |= (first_index != last_index + 1);
Gao Xiang9248fce2018-12-08 00:19:15 +08001216
Gao Xiang3883a792018-07-26 20:22:06 +08001217repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +08001218 page = pickup_page_for_submission(pcl, i, pagepool,
1219 MNGD_MAPPING(sbi),
1220 GFP_NOFS);
Gao Xiang9248fce2018-12-08 00:19:15 +08001221 if (!page) {
1222 force_submit = true;
1223 ++bypass;
1224 goto skippage;
Gao Xiang3883a792018-07-26 20:22:06 +08001225 }
1226
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001227 if (bio && force_submit) {
Gao Xiang3883a792018-07-26 20:22:06 +08001228submit_bio_retry:
Gao Xiang94e4e152019-09-04 10:09:04 +08001229 submit_bio(bio);
Gao Xiang3883a792018-07-26 20:22:06 +08001230 bio = NULL;
1231 }
1232
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001233 if (!bio) {
Gao Xianga5c0b782019-09-04 10:09:02 +08001234 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1235
Gao Xiang0c638f72019-11-08 11:37:33 +08001236 bio->bi_end_io = z_erofs_decompressqueue_endio;
Gao Xianga5c0b782019-09-04 10:09:02 +08001237 bio_set_dev(bio, sb->s_bdev);
1238 bio->bi_iter.bi_sector = (sector_t)(first_index + i) <<
1239 LOG_SECTORS_PER_BLOCK;
1240 bio->bi_private = bi_private;
Gao Xiang94e4e152019-09-04 10:09:04 +08001241 bio->bi_opf = REQ_OP_READ;
1242
Gao Xiang3883a792018-07-26 20:22:06 +08001243 ++nr_bios;
1244 }
1245
1246 err = bio_add_page(bio, page, PAGE_SIZE, 0);
1247 if (err < PAGE_SIZE)
1248 goto submit_bio_retry;
1249
1250 force_submit = false;
1251 last_index = first_index + i;
Gao Xiang105d4ad2018-07-26 20:22:07 +08001252skippage:
Gao Xiang3883a792018-07-26 20:22:06 +08001253 if (++i < clusterpages)
1254 goto repeat;
Gao Xiang105d4ad2018-07-26 20:22:07 +08001255
Gao Xiang7146a4f2018-12-08 00:19:18 +08001256 if (bypass < clusterpages)
Gao Xiang97e86a82019-07-31 23:57:47 +08001257 qtail[JQ_SUBMIT] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001258 else
Gao Xiang97e86a82019-07-31 23:57:47 +08001259 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1260 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001261
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001262 if (bio)
Gao Xiang94e4e152019-09-04 10:09:04 +08001263 submit_bio(bio);
Gao Xiang3883a792018-07-26 20:22:06 +08001264
Gao Xianga4b1fab2019-10-08 20:56:15 +08001265 if (postsubmit_is_all_bypassed(q, nr_bios, *force_fg))
Gao Xiang105d4ad2018-07-26 20:22:07 +08001266 return true;
Gao Xiang3883a792018-07-26 20:22:06 +08001267
Gao Xianga4b1fab2019-10-08 20:56:15 +08001268 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
Gao Xiang3883a792018-07-26 20:22:06 +08001269 return true;
1270}
1271
Gao Xiang0c638f72019-11-08 11:37:33 +08001272static void z_erofs_runqueue(struct super_block *sb,
1273 struct z_erofs_collector *clt,
1274 struct list_head *pagepool, bool force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001275{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001276 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001277
Gao Xiang0c638f72019-11-08 11:37:33 +08001278 if (!z_erofs_submit_queue(sb, clt->owned_head,
1279 pagepool, io, &force_fg))
Gao Xiang3883a792018-07-26 20:22:06 +08001280 return;
1281
Gao Xiang0c638f72019-11-08 11:37:33 +08001282 /* handle bypass queue (no i/o pclusters) immediately */
1283 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
Gao Xiang4279f3f2019-07-31 23:57:49 +08001284
Gao Xiang3883a792018-07-26 20:22:06 +08001285 if (!force_fg)
1286 return;
1287
1288 /* wait until all bios are completed */
Gao Xianga93f8c32019-10-08 20:56:16 +08001289 io_wait_event(io[JQ_SUBMIT].u.wait,
1290 !atomic_read(&io[JQ_SUBMIT].pending_bios));
Gao Xiang3883a792018-07-26 20:22:06 +08001291
Gao Xiang0c638f72019-11-08 11:37:33 +08001292 /* handle synchronous decompress queue in the caller context */
1293 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001294}
1295
Gao Xiang0c638f72019-11-08 11:37:33 +08001296static int z_erofs_readpage(struct file *file, struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +08001297{
1298 struct inode *const inode = page->mapping->host;
Gao Xiang97e86a82019-07-31 23:57:47 +08001299 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001300 int err;
1301 LIST_HEAD(pagepool);
1302
Gao Xiangba9ce772018-11-23 01:15:58 +08001303 trace_erofs_readpage(page, false);
1304
Gao Xiangf0c519f2018-11-23 01:21:49 +08001305 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1306
Gao Xiang3883a792018-07-26 20:22:06 +08001307 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xiang97e86a82019-07-31 23:57:47 +08001308 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001309
Gao Xiangee451972019-08-19 18:34:21 +08001310 /* if some compressed cluster ready, need submit them anyway */
Gao Xiang0c638f72019-11-08 11:37:33 +08001311 z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, true);
Gao Xiangee451972019-08-19 18:34:21 +08001312
1313 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001314 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
Gao Xiangee451972019-08-19 18:34:21 +08001315
Chao Yu3b423412019-01-15 09:42:21 +08001316 if (f.map.mpage)
1317 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001318
1319 /* clean up the remaining free pages */
1320 put_pages_list(&pagepool);
Gao Xiangee451972019-08-19 18:34:21 +08001321 return err;
Gao Xiang3883a792018-07-26 20:22:06 +08001322}
1323
Gao Xiang14f362b2019-07-31 23:57:36 +08001324static bool should_decompress_synchronously(struct erofs_sb_info *sbi,
1325 unsigned int nr)
1326{
1327 return nr <= sbi->max_sync_decompress_pages;
1328}
1329
Gao Xiang0c638f72019-11-08 11:37:33 +08001330static int z_erofs_readpages(struct file *filp, struct address_space *mapping,
1331 struct list_head *pages, unsigned int nr_pages)
Gao Xiang3883a792018-07-26 20:22:06 +08001332{
1333 struct inode *const inode = mapping->host;
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001334 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001335
Gao Xiang14f362b2019-07-31 23:57:36 +08001336 bool sync = should_decompress_synchronously(sbi, nr_pages);
Gao Xiang97e86a82019-07-31 23:57:47 +08001337 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001338 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
1339 struct page *head = NULL;
1340 LIST_HEAD(pagepool);
1341
Chen Gong284db122018-09-18 22:27:27 +08001342 trace_erofs_readpages(mapping->host, lru_to_page(pages),
1343 nr_pages, false);
1344
Gao Xiangf0c519f2018-11-23 01:21:49 +08001345 f.headoffset = (erofs_off_t)lru_to_page(pages)->index << PAGE_SHIFT;
1346
Gao Xiang3883a792018-07-26 20:22:06 +08001347 for (; nr_pages; --nr_pages) {
1348 struct page *page = lru_to_page(pages);
1349
1350 prefetchw(&page->flags);
1351 list_del(&page->lru);
1352
Gao Xiang2d9b5dc2018-11-23 01:21:48 +08001353 /*
1354 * A pure asynchronous readahead is indicated if
1355 * a PG_readahead marked page is hitted at first.
1356 * Let's also do asynchronous decompression for this case.
1357 */
1358 sync &= !(PageReadahead(page) && !head);
1359
Gao Xiang3883a792018-07-26 20:22:06 +08001360 if (add_to_page_cache_lru(page, mapping, page->index, gfp)) {
1361 list_add(&page->lru, &pagepool);
1362 continue;
1363 }
1364
Gao Xiang3883a792018-07-26 20:22:06 +08001365 set_page_private(page, (unsigned long)head);
1366 head = page;
1367 }
1368
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001369 while (head) {
Gao Xiang3883a792018-07-26 20:22:06 +08001370 struct page *page = head;
1371 int err;
1372
1373 /* traversal in reverse order */
1374 head = (void *)page_private(page);
1375
1376 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xianga5876e22019-09-04 10:08:56 +08001377 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001378 erofs_err(inode->i_sb,
1379 "readahead error at page %lu @ nid %llu",
1380 page->index, EROFS_I(inode)->nid);
Gao Xiang3883a792018-07-26 20:22:06 +08001381 put_page(page);
1382 }
1383
Gao Xiang97e86a82019-07-31 23:57:47 +08001384 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001385
Gao Xiang0c638f72019-11-08 11:37:33 +08001386 z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, sync);
Gao Xiang3883a792018-07-26 20:22:06 +08001387
Chao Yu3b423412019-01-15 09:42:21 +08001388 if (f.map.mpage)
1389 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001390
1391 /* clean up the remaining free pages */
1392 put_pages_list(&pagepool);
1393 return 0;
1394}
1395
Gao Xiang0c638f72019-11-08 11:37:33 +08001396const struct address_space_operations z_erofs_aops = {
1397 .readpage = z_erofs_readpage,
1398 .readpages = z_erofs_readpages,
Gao Xiang3883a792018-07-26 20:22:06 +08001399};
Gao Xiang02827e12018-07-26 20:21:58 +08001400