blob: f06a2fad7af254279e56dd60240fdab7d604e1bf [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
43static inline int init_unzip_workqueue(void)
44{
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 Xiang48d4bf32018-11-23 01:21:46 +080057static void init_once(void *ptr)
58{
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 Xiang97e86a82019-07-31 23:57:47 +080070static void 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,
84 SLAB_RECLAIM_ACCOUNT, init_once);
85 if (pcluster_cachep) {
Gao Xiang3883a792018-07-26 20:22:06 +080086 if (!init_unzip_workqueue())
87 return 0;
88
Gao Xiang97e86a82019-07-31 23:57:47 +080089 kmem_cache_destroy(pcluster_cachep);
Gao Xiang3883a792018-07-26 20:22:06 +080090 }
91 return -ENOMEM;
92}
93
Gao Xiang97e86a82019-07-31 23:57:47 +080094enum z_erofs_collectmode {
95 COLLECT_SECONDARY,
96 COLLECT_PRIMARY,
Gao Xiang3883a792018-07-26 20:22:06 +080097 /*
Gao Xiang97e86a82019-07-31 23:57:47 +080098 * The current collection was the tail of an exist chain, in addition
99 * that the previous processed chained collections are all decided to
100 * be hooked up to it.
101 * A new chain will be created for the remaining collections which are
102 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
103 * the next collection cannot reuse the whole page safely in
104 * the following scenario:
Gao Xianga1121522019-02-27 13:33:32 +0800105 * ________________________________________________________________
106 * | tail (partial) page | head (partial) page |
Gao Xiang97e86a82019-07-31 23:57:47 +0800107 * | (belongs to the next cl) | (belongs to the current cl) |
Gao Xianga1121522019-02-27 13:33:32 +0800108 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
109 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800110 COLLECT_PRIMARY_HOOKED,
111 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
Gao Xianga1121522019-02-27 13:33:32 +0800112 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800113 * The current collection has been linked with the owned chain, and
114 * could also be linked with the remaining collections, which means
115 * if the processing page is the tail page of the collection, thus
116 * the current collection can safely use the whole page (since
117 * the previous collection is under control) for in-place I/O, as
118 * illustrated below:
Gao Xianga1121522019-02-27 13:33:32 +0800119 * ________________________________________________________________
Gao Xiang97e86a82019-07-31 23:57:47 +0800120 * | tail (partial) page | head (partial) page |
121 * | (of the current cl) | (of the previous collection) |
122 * | PRIMARY_FOLLOWED or | |
123 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
Gao Xianga1121522019-02-27 13:33:32 +0800124 *
Gao Xiang97e86a82019-07-31 23:57:47 +0800125 * [ (*) the above page can be used as inplace I/O. ]
Gao Xiang3883a792018-07-26 20:22:06 +0800126 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800127 COLLECT_PRIMARY_FOLLOWED,
Gao Xiang3883a792018-07-26 20:22:06 +0800128};
129
Gao Xiang97e86a82019-07-31 23:57:47 +0800130struct z_erofs_collector {
Gao Xiang3883a792018-07-26 20:22:06 +0800131 struct z_erofs_pagevec_ctor vector;
132
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800133 struct z_erofs_pcluster *pcl, *tailpcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800134 struct z_erofs_collection *cl;
135 struct page **compressedpages;
136 z_erofs_next_pcluster_t owned_head;
137
138 enum z_erofs_collectmode mode;
Gao Xiang3883a792018-07-26 20:22:06 +0800139};
140
Gao Xiang97e86a82019-07-31 23:57:47 +0800141struct z_erofs_decompress_frontend {
142 struct inode *const inode;
143
144 struct z_erofs_collector clt;
145 struct erofs_map_blocks map;
146
147 /* used for applying cache strategy on the fly */
148 bool backmost;
149 erofs_off_t headoffset;
150};
151
152#define COLLECTOR_INIT() { \
153 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
154 .mode = COLLECT_PRIMARY_FOLLOWED }
155
156#define DECOMPRESS_FRONTEND_INIT(__i) { \
157 .inode = __i, .clt = COLLECTOR_INIT(), \
158 .backmost = true, }
159
160static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
161static DEFINE_MUTEX(z_pagemap_global_lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800162
Gao Xiang97e86a82019-07-31 23:57:47 +0800163static void preload_compressed_pages(struct z_erofs_collector *clt,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800164 struct address_space *mc,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800165 enum z_erofs_cache_alloctype type,
Gao Xiang97e86a82019-07-31 23:57:47 +0800166 struct list_head *pagepool)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800167{
Gao Xiang97e86a82019-07-31 23:57:47 +0800168 const struct z_erofs_pcluster *pcl = clt->pcl;
169 const unsigned int clusterpages = BIT(pcl->clusterbits);
170 struct page **pages = clt->compressedpages;
171 pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
Gao Xiang92e6efd2018-12-08 00:19:16 +0800172 bool standalone = true;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800173
Gao Xiang97e86a82019-07-31 23:57:47 +0800174 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800175 return;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800176
Gao Xiang97e86a82019-07-31 23:57:47 +0800177 for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
Gao Xiang92e6efd2018-12-08 00:19:16 +0800178 struct page *page;
179 compressed_page_t t;
180
181 /* the compressed page was loaded before */
Gao Xiang97e86a82019-07-31 23:57:47 +0800182 if (READ_ONCE(*pages))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800183 continue;
184
Gao Xiang97e86a82019-07-31 23:57:47 +0800185 page = find_get_page(mc, index);
Gao Xiang92e6efd2018-12-08 00:19:16 +0800186
187 if (page) {
188 t = tag_compressed_page_justfound(page);
189 } else if (type == DELAYEDALLOC) {
190 t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
191 } else { /* DONTALLOC */
192 if (standalone)
Gao Xiang97e86a82019-07-31 23:57:47 +0800193 clt->compressedpages = pages;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800194 standalone = false;
195 continue;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800196 }
197
Gao Xiang97e86a82019-07-31 23:57:47 +0800198 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800199 continue;
200
Gao Xiang92e6efd2018-12-08 00:19:16 +0800201 if (page)
202 put_page(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800203 }
Gao Xiang92e6efd2018-12-08 00:19:16 +0800204
Gao Xiang97e86a82019-07-31 23:57:47 +0800205 if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
206 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800207}
208
209/* called by erofs_shrinker to get rid of all compressed_pages */
Gao Xiang47e541a2018-07-29 13:34:58 +0800210int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
Gao Xiang97e86a82019-07-31 23:57:47 +0800211 struct erofs_workgroup *grp)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800212{
Gao Xiang97e86a82019-07-31 23:57:47 +0800213 struct z_erofs_pcluster *const pcl =
214 container_of(grp, struct z_erofs_pcluster, obj);
Gao Xiangc1448fa2018-12-08 00:19:13 +0800215 struct address_space *const mapping = MNGD_MAPPING(sbi);
Gao Xiang97e86a82019-07-31 23:57:47 +0800216 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800217 int i;
218
219 /*
220 * refcount of workgroup is now freezed as 1,
221 * therefore no need to worry about available decompression users.
222 */
223 for (i = 0; i < clusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800224 struct page *page = pcl->compressed_pages[i];
Gao Xiang105d4ad2018-07-26 20:22:07 +0800225
Gao Xiang97e86a82019-07-31 23:57:47 +0800226 if (!page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800227 continue;
228
229 /* block other users from reclaiming or migrating the page */
230 if (!trylock_page(page))
231 return -EBUSY;
232
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800233 if (page->mapping != mapping)
Gao Xiang97e86a82019-07-31 23:57:47 +0800234 continue;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800235
Gao Xiang97e86a82019-07-31 23:57:47 +0800236 /* barrier is implied in the following 'unlock_page' */
237 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800238 set_page_private(page, 0);
239 ClearPagePrivate(page);
240
241 unlock_page(page);
242 put_page(page);
243 }
244 return 0;
245}
246
Gao Xiang47e541a2018-07-29 13:34:58 +0800247int erofs_try_to_free_cached_page(struct address_space *mapping,
248 struct page *page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800249{
Gao Xiang97e86a82019-07-31 23:57:47 +0800250 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
251 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800252 int ret = 0; /* 0 - busy */
253
Gao Xiang97e86a82019-07-31 23:57:47 +0800254 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
Gao Xiang105d4ad2018-07-26 20:22:07 +0800255 unsigned int i;
256
257 for (i = 0; i < clusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800258 if (pcl->compressed_pages[i] == page) {
259 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800260 ret = 1;
261 break;
262 }
263 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800264 erofs_workgroup_unfreeze(&pcl->obj, 1);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800265
Gao Xiang047d4ab2019-02-16 16:46:50 +0800266 if (ret) {
267 ClearPagePrivate(page);
268 put_page(page);
269 }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800270 }
271 return ret;
272}
Gao Xiang105d4ad2018-07-26 20:22:07 +0800273
Gao Xiang3883a792018-07-26 20:22:06 +0800274/* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
Gao Xiang97e86a82019-07-31 23:57:47 +0800275static inline bool try_inplace_io(struct z_erofs_collector *clt,
276 struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +0800277{
Gao Xiang97e86a82019-07-31 23:57:47 +0800278 struct z_erofs_pcluster *const pcl = clt->pcl;
279 const unsigned int clusterpages = BIT(pcl->clusterbits);
280
281 while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
282 if (!cmpxchg(clt->compressedpages++, NULL, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800283 return true;
284 }
Gao Xiang3883a792018-07-26 20:22:06 +0800285 return false;
286}
287
Gao Xiang97e86a82019-07-31 23:57:47 +0800288/* callers must be with collection lock held */
289static int z_erofs_attach_page(struct z_erofs_collector *clt,
290 struct page *page,
291 enum z_erofs_page_type type)
Gao Xiang3883a792018-07-26 20:22:06 +0800292{
293 int ret;
294 bool occupied;
295
Gao Xiang97e86a82019-07-31 23:57:47 +0800296 /* give priority for inplaceio */
297 if (clt->mode >= COLLECT_PRIMARY &&
Julian Merida447a3622019-03-18 20:58:41 -0300298 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
Gao Xiang97e86a82019-07-31 23:57:47 +0800299 try_inplace_io(clt, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800300 return 0;
301
Gao Xiang97e86a82019-07-31 23:57:47 +0800302 ret = z_erofs_pagevec_enqueue(&clt->vector,
Gao Xiang046d64e2019-07-31 23:57:45 +0800303 page, type, &occupied);
Gao Xiang97e86a82019-07-31 23:57:47 +0800304 clt->cl->vcnt += (unsigned int)ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800305
306 return ret ? 0 : -EAGAIN;
307}
308
Gao Xiang97e86a82019-07-31 23:57:47 +0800309static enum z_erofs_collectmode
310try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
311 z_erofs_next_pcluster_t *owned_head)
Gao Xiang3883a792018-07-26 20:22:06 +0800312{
Gao Xiang97e86a82019-07-31 23:57:47 +0800313 /* let's claim these following types of pclusters */
Gao Xiang3883a792018-07-26 20:22:06 +0800314retry:
Gao Xiang97e86a82019-07-31 23:57:47 +0800315 if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
316 /* type 1, nil pcluster */
317 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
318 *owned_head) != Z_EROFS_PCLUSTER_NIL)
Gao Xiang3883a792018-07-26 20:22:06 +0800319 goto retry;
320
Gao Xiang97e86a82019-07-31 23:57:47 +0800321 *owned_head = &pcl->next;
Gao Xianga1121522019-02-27 13:33:32 +0800322 /* lucky, I am the followee :) */
Gao Xiang97e86a82019-07-31 23:57:47 +0800323 return COLLECT_PRIMARY_FOLLOWED;
324 } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
Gao Xiang3883a792018-07-26 20:22:06 +0800325 /*
326 * type 2, link to the end of a existing open chain,
327 * be careful that its submission itself is governed
328 * by the original owned chain.
329 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800330 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
331 *owned_head) != Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +0800332 goto retry;
Gao Xiang97e86a82019-07-31 23:57:47 +0800333 *owned_head = Z_EROFS_PCLUSTER_TAIL;
334 return COLLECT_PRIMARY_HOOKED;
Gao Xianga1121522019-02-27 13:33:32 +0800335 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800336 return COLLECT_PRIMARY; /* :( better luck next time */
Gao Xiang3883a792018-07-26 20:22:06 +0800337}
338
Gao Xiang97e86a82019-07-31 23:57:47 +0800339static struct z_erofs_collection *cllookup(struct z_erofs_collector *clt,
340 struct inode *inode,
341 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800342{
Gao Xiang97e86a82019-07-31 23:57:47 +0800343 struct erofs_workgroup *grp;
344 struct z_erofs_pcluster *pcl;
345 struct z_erofs_collection *cl;
346 unsigned int length;
347 bool tag;
Gao Xiang3883a792018-07-26 20:22:06 +0800348
Gao Xiang97e86a82019-07-31 23:57:47 +0800349 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT, &tag);
350 if (!grp)
Gao Xiang3883a792018-07-26 20:22:06 +0800351 return NULL;
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);
357 return ERR_PTR(-EFSCORRUPTED);
358 }
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);
364 return ERR_PTR(-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);
372 return ERR_PTR(-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;
396 return cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800397}
398
Gao Xiang97e86a82019-07-31 23:57:47 +0800399static struct z_erofs_collection *clregister(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 Xiang3883a792018-07-26 20:22:06 +0800410 return ERR_PTR(-ENOMEM);
411
Gao Xiang97e86a82019-07-31 23:57:47 +0800412 init_always(pcl);
413 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
Gao Xiang97e86a82019-07-31 23:57:47 +0800440 err = erofs_register_workgroup(inode->i_sb, &pcl->obj, 0);
441 if (err) {
442 mutex_unlock(&cl->lock);
443 kmem_cache_free(pcluster_cachep, pcl);
444 return ERR_PTR(-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;
452 return cl;
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 Xiang97e86a82019-07-31 23:57:47 +0800459 struct z_erofs_collection *cl;
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 Xiang97e86a82019-07-31 23:57:47 +0800473 cl = cllookup(clt, inode, map);
474 if (!cl) {
475 cl = clregister(clt, inode, map);
Gao Xiangb27661c2018-09-19 13:49:06 +0800476
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800477 if (cl == ERR_PTR(-EAGAIN))
Gao Xiang97e86a82019-07-31 23:57:47 +0800478 goto repeat;
Gao Xiang3883a792018-07-26 20:22:06 +0800479 }
480
Gao Xiang97e86a82019-07-31 23:57:47 +0800481 if (IS_ERR(cl))
482 return PTR_ERR(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800483
Gao Xiang97e86a82019-07-31 23:57:47 +0800484 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
485 cl->pagevec, cl->vcnt);
Gao Xiang3883a792018-07-26 20:22:06 +0800486
Gao Xiang97e86a82019-07-31 23:57:47 +0800487 clt->compressedpages = clt->pcl->compressed_pages;
488 if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
489 clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
Gao Xiang3883a792018-07-26 20:22:06 +0800490 return 0;
491}
492
493/*
Gao Xiang97e86a82019-07-31 23:57:47 +0800494 * keep in mind that no referenced pclusters will be freed
495 * only after a RCU grace period.
Gao Xiang3883a792018-07-26 20:22:06 +0800496 */
497static void z_erofs_rcu_callback(struct rcu_head *head)
498{
Gao Xiang97e86a82019-07-31 23:57:47 +0800499 struct z_erofs_collection *const cl =
500 container_of(head, struct z_erofs_collection, rcu);
Gao Xiang3883a792018-07-26 20:22:06 +0800501
Gao Xiang97e86a82019-07-31 23:57:47 +0800502 kmem_cache_free(pcluster_cachep,
503 container_of(cl, struct z_erofs_pcluster,
504 primary_collection));
Gao Xiang3883a792018-07-26 20:22:06 +0800505}
506
507void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
508{
Gao Xiang97e86a82019-07-31 23:57:47 +0800509 struct z_erofs_pcluster *const pcl =
510 container_of(grp, struct z_erofs_pcluster, obj);
511 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800512
Gao Xiang97e86a82019-07-31 23:57:47 +0800513 call_rcu(&cl->rcu, z_erofs_rcu_callback);
Gao Xiang3883a792018-07-26 20:22:06 +0800514}
515
Gao Xiang97e86a82019-07-31 23:57:47 +0800516static void z_erofs_collection_put(struct z_erofs_collection *cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800517{
Gao Xiang97e86a82019-07-31 23:57:47 +0800518 struct z_erofs_pcluster *const pcl =
519 container_of(cl, struct z_erofs_pcluster, primary_collection);
520
521 erofs_workgroup_put(&pcl->obj);
Gao Xiang3883a792018-07-26 20:22:06 +0800522}
523
Gao Xiang97e86a82019-07-31 23:57:47 +0800524static bool z_erofs_collector_end(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800525{
Gao Xiang97e86a82019-07-31 23:57:47 +0800526 struct z_erofs_collection *cl = clt->cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800527
Gao Xiang97e86a82019-07-31 23:57:47 +0800528 if (!cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800529 return false;
530
Gao Xiang97e86a82019-07-31 23:57:47 +0800531 z_erofs_pagevec_ctor_exit(&clt->vector, false);
532 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800533
534 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800535 * if all pending pages are added, don't hold its reference
536 * any longer if the pcluster isn't hosted by ourselves.
Gao Xiang3883a792018-07-26 20:22:06 +0800537 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800538 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
539 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800540
Gao Xiang97e86a82019-07-31 23:57:47 +0800541 clt->cl = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +0800542 return true;
543}
544
545static inline struct page *__stagingpage_alloc(struct list_head *pagepool,
546 gfp_t gfp)
547{
Gao Xiangb25a1512019-07-31 23:57:43 +0800548 struct page *page = erofs_allocpage(pagepool, gfp, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800549
550 page->mapping = Z_EROFS_MAPPING_STAGING;
551 return page;
552}
553
Gao Xiang97e86a82019-07-31 23:57:47 +0800554static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800555 unsigned int cachestrategy,
Gao Xiang97e86a82019-07-31 23:57:47 +0800556 erofs_off_t la)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800557{
Gao Xiang4279f3f2019-07-31 23:57:49 +0800558 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
559 return false;
560
Gao Xiang92e6efd2018-12-08 00:19:16 +0800561 if (fe->backmost)
562 return true;
563
Gao Xiang4279f3f2019-07-31 23:57:49 +0800564 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
565 la < fe->headoffset;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800566}
Gao Xiang92e6efd2018-12-08 00:19:16 +0800567
Gao Xiang97e86a82019-07-31 23:57:47 +0800568static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
Gao Xiang3883a792018-07-26 20:22:06 +0800569 struct page *page,
Gao Xiang97e86a82019-07-31 23:57:47 +0800570 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800571{
Gao Xiang97e86a82019-07-31 23:57:47 +0800572 struct inode *const inode = fe->inode;
573 struct erofs_sb_info *const sbi __maybe_unused = EROFS_I_SB(inode);
Chao Yu3b423412019-01-15 09:42:21 +0800574 struct erofs_map_blocks *const map = &fe->map;
Gao Xiang97e86a82019-07-31 23:57:47 +0800575 struct z_erofs_collector *const clt = &fe->clt;
Gao Xiang3883a792018-07-26 20:22:06 +0800576 const loff_t offset = page_offset(page);
Gao Xiang97e86a82019-07-31 23:57:47 +0800577 bool tight = (clt->mode >= COLLECT_PRIMARY_HOOKED);
Gao Xiang3883a792018-07-26 20:22:06 +0800578
Gao Xiang92e6efd2018-12-08 00:19:16 +0800579 enum z_erofs_cache_alloctype cache_strategy;
Gao Xiang3883a792018-07-26 20:22:06 +0800580 enum z_erofs_page_type page_type;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200581 unsigned int cur, end, spiltted, index;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800582 int err = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800583
584 /* register locked file pages as online pages in pack */
585 z_erofs_onlinepage_init(page);
586
587 spiltted = 0;
588 end = PAGE_SIZE;
589repeat:
590 cur = end - 1;
591
592 /* lucky, within the range of the current map_blocks */
593 if (offset + cur >= map->m_la &&
Julian Merida447a3622019-03-18 20:58:41 -0300594 offset + cur < map->m_la + map->m_llen) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800595 /* didn't get a valid collection previously (very rare) */
596 if (!clt->cl)
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800597 goto restart_now;
Gao Xiang3883a792018-07-26 20:22:06 +0800598 goto hitted;
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800599 }
Gao Xiang3883a792018-07-26 20:22:06 +0800600
601 /* go ahead the next map_blocks */
602 debugln("%s: [out-of-range] pos %llu", __func__, offset + cur);
603
Gao Xiang97e86a82019-07-31 23:57:47 +0800604 if (z_erofs_collector_end(clt))
Gao Xiangf0c519f2018-11-23 01:21:49 +0800605 fe->backmost = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800606
607 map->m_la = offset + cur;
608 map->m_llen = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +0800609 err = z_erofs_map_blocks_iter(inode, map, 0);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800610 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800611 goto err_out;
612
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800613restart_now:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800614 if (!(map->m_flags & EROFS_MAP_MAPPED))
Gao Xiang3883a792018-07-26 20:22:06 +0800615 goto hitted;
616
Gao Xiang97e86a82019-07-31 23:57:47 +0800617 err = z_erofs_collector_begin(clt, inode, map);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800618 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800619 goto err_out;
620
Gao Xiang92e6efd2018-12-08 00:19:16 +0800621 /* preload all compressed pages (maybe downgrade role if necessary) */
Gao Xiang4279f3f2019-07-31 23:57:49 +0800622 if (should_alloc_managed_pages(fe, sbi->cache_strategy, map->m_la))
Gao Xiang92e6efd2018-12-08 00:19:16 +0800623 cache_strategy = DELAYEDALLOC;
624 else
625 cache_strategy = DONTALLOC;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800626
Gao Xiang97e86a82019-07-31 23:57:47 +0800627 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
628 cache_strategy, pagepool);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800629
Gao Xiang97e86a82019-07-31 23:57:47 +0800630 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED);
Gao Xiang3883a792018-07-26 20:22:06 +0800631hitted:
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200632 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800633 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800634 zero_user_segment(page, cur, end);
635 goto next_part;
636 }
637
638 /* let's derive page type */
639 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
640 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
641 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
642 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
643
Gao Xianga1121522019-02-27 13:33:32 +0800644 if (cur)
Gao Xiang97e86a82019-07-31 23:57:47 +0800645 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xianga1121522019-02-27 13:33:32 +0800646
Gao Xiang3883a792018-07-26 20:22:06 +0800647retry:
Gao Xiang97e86a82019-07-31 23:57:47 +0800648 err = z_erofs_attach_page(clt, page, page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800649 /* should allocate an additional staging page for pagevec */
650 if (err == -EAGAIN) {
651 struct page *const newpage =
Gao Xiang97e86a82019-07-31 23:57:47 +0800652 __stagingpage_alloc(pagepool, GFP_NOFS);
Gao Xiang3883a792018-07-26 20:22:06 +0800653
Gao Xiang97e86a82019-07-31 23:57:47 +0800654 err = z_erofs_attach_page(clt, newpage,
655 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800656 if (!err)
Gao Xiang3883a792018-07-26 20:22:06 +0800657 goto retry;
658 }
659
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800660 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800661 goto err_out;
662
Gao Xiang97e86a82019-07-31 23:57:47 +0800663 index = page->index - (map->m_la >> PAGE_SHIFT);
Gao Xiang3883a792018-07-26 20:22:06 +0800664
Gao Xiang3883a792018-07-26 20:22:06 +0800665 z_erofs_onlinepage_fixup(page, index, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800666
Gao Xiang1e05ff32018-09-18 22:27:25 +0800667 /* bump up the number of spiltted parts of a page */
668 ++spiltted;
669 /* also update nr_pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800670 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
Gao Xiang3883a792018-07-26 20:22:06 +0800671next_part:
672 /* can be used for verification */
673 map->m_llen = offset + cur - map->m_la;
674
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300675 end = cur;
676 if (end > 0)
Gao Xiang3883a792018-07-26 20:22:06 +0800677 goto repeat;
678
Gao Xiang1e05ff32018-09-18 22:27:25 +0800679out:
Gao Xiang3883a792018-07-26 20:22:06 +0800680 z_erofs_onlinepage_endio(page);
681
682 debugln("%s, finish page: %pK spiltted: %u map->m_llen %llu",
683 __func__, page, spiltted, map->m_llen);
Gao Xiang3883a792018-07-26 20:22:06 +0800684 return err;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800685
686 /* if some error occurred while processing this page */
687err_out:
688 SetPageError(page);
689 goto out;
Gao Xiang3883a792018-07-26 20:22:06 +0800690}
691
692static void z_erofs_vle_unzip_kickoff(void *ptr, int bios)
693{
694 tagptr1_t t = tagptr_init(tagptr1_t, ptr);
Gao Xiang97e86a82019-07-31 23:57:47 +0800695 struct z_erofs_unzip_io *io = tagptr_unfold_ptr(t);
Gao Xiang3883a792018-07-26 20:22:06 +0800696 bool background = tagptr_unfold_tags(t);
697
Gao Xiang848bd9a2018-12-08 00:19:12 +0800698 if (!background) {
699 unsigned long flags;
Gao Xiang3883a792018-07-26 20:22:06 +0800700
Gao Xiang848bd9a2018-12-08 00:19:12 +0800701 spin_lock_irqsave(&io->u.wait.lock, flags);
702 if (!atomic_add_return(bios, &io->pending_bios))
703 wake_up_locked(&io->u.wait);
704 spin_unlock_irqrestore(&io->u.wait.lock, flags);
705 return;
706 }
707
708 if (!atomic_add_return(bios, &io->pending_bios))
Gao Xiang3883a792018-07-26 20:22:06 +0800709 queue_work(z_erofs_workqueue, &io->u.work);
Gao Xiang3883a792018-07-26 20:22:06 +0800710}
711
712static inline void z_erofs_vle_read_endio(struct bio *bio)
713{
Gao Xiangd61fbb62019-03-25 11:40:08 +0800714 struct erofs_sb_info *sbi = NULL;
Gao Xiang14a56ec2019-03-25 11:40:09 +0800715 blk_status_t err = bio->bi_status;
Gao Xiang3883a792018-07-26 20:22:06 +0800716 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800717 struct bvec_iter_all iter_all;
Gao Xiang3883a792018-07-26 20:22:06 +0800718
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200719 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang3883a792018-07-26 20:22:06 +0800720 struct page *page = bvec->bv_page;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800721 bool cachemngd = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800722
723 DBG_BUGON(PageUptodate(page));
Gao Xiang70b17992018-12-11 15:17:49 +0800724 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800725
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800726 if (!sbi && !z_erofs_page_is_staging(page)) {
Gao Xiangd61fbb62019-03-25 11:40:08 +0800727 sbi = EROFS_SB(page->mapping->host->i_sb);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800728
Gao Xiang14a56ec2019-03-25 11:40:09 +0800729 if (time_to_inject(sbi, FAULT_READ_IO)) {
730 erofs_show_injection_info(FAULT_READ_IO);
731 err = BLK_STS_IOERR;
732 }
733 }
734
Gao Xiangd61fbb62019-03-25 11:40:08 +0800735 /* sbi should already be gotten if the page is managed */
736 if (sbi)
737 cachemngd = erofs_page_is_managed(sbi, page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800738
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800739 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800740 SetPageError(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800741 else if (cachemngd)
742 SetPageUptodate(page);
743
744 if (cachemngd)
745 unlock_page(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800746 }
747
748 z_erofs_vle_unzip_kickoff(bio->bi_private, -1);
749 bio_put(bio);
750}
751
Gao Xiang97e86a82019-07-31 23:57:47 +0800752static int z_erofs_decompress_pcluster(struct super_block *sb,
753 struct z_erofs_pcluster *pcl,
754 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800755{
756 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +0800757 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +0800758 struct z_erofs_pagevec_ctor ctor;
Gao Xiang97e86a82019-07-31 23:57:47 +0800759 unsigned int i, outputsize, llen, nr_pages;
760 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
Gao Xiang3883a792018-07-26 20:22:06 +0800761 struct page **pages, **compressed_pages, *page;
Gao Xiang3883a792018-07-26 20:22:06 +0800762
763 enum z_erofs_page_type page_type;
Gao Xiangb6a76182019-06-24 15:22:58 +0800764 bool overlapped, partial;
Gao Xiang97e86a82019-07-31 23:57:47 +0800765 struct z_erofs_collection *cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800766 int err;
767
768 might_sleep();
Gao Xiang97e86a82019-07-31 23:57:47 +0800769 cl = z_erofs_primarycollection(pcl);
770 DBG_BUGON(!READ_ONCE(cl->nr_pages));
Gao Xiang3883a792018-07-26 20:22:06 +0800771
Gao Xiang97e86a82019-07-31 23:57:47 +0800772 mutex_lock(&cl->lock);
773 nr_pages = cl->nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800774
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800775 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
Gao Xiang3883a792018-07-26 20:22:06 +0800776 pages = pages_onstack;
Gao Xiang97e86a82019-07-31 23:57:47 +0800777 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
778 mutex_trylock(&z_pagemap_global_lock)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800779 pages = z_pagemap_global;
Gao Xiang97e86a82019-07-31 23:57:47 +0800780 } else {
Chao Yu441dfcc2019-07-16 17:44:22 +0800781 gfp_t gfp_flags = GFP_KERNEL;
782
Gao Xiang97e86a82019-07-31 23:57:47 +0800783 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
Chao Yu441dfcc2019-07-16 17:44:22 +0800784 gfp_flags |= __GFP_NOFAIL;
785
Julian Merida447a3622019-03-18 20:58:41 -0300786 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Chao Yu441dfcc2019-07-16 17:44:22 +0800787 gfp_flags);
Gao Xiang3883a792018-07-26 20:22:06 +0800788
789 /* fallback to global pagemap for the lowmem scenario */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800790 if (!pages) {
Chao Yu441dfcc2019-07-16 17:44:22 +0800791 mutex_lock(&z_pagemap_global_lock);
792 pages = z_pagemap_global;
Gao Xiang3883a792018-07-26 20:22:06 +0800793 }
794 }
795
796 for (i = 0; i < nr_pages; ++i)
797 pages[i] = NULL;
798
Gao Xiange12a0ce2019-08-21 22:01:52 +0800799 err = 0;
Gao Xiangfa61a332019-06-24 15:22:53 +0800800 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang97e86a82019-07-31 23:57:47 +0800801 cl->pagevec, 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800802
Gao Xiang97e86a82019-07-31 23:57:47 +0800803 for (i = 0; i < cl->vcnt; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200804 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800805
Gao Xiang046d64e2019-07-31 23:57:45 +0800806 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800807
808 /* all pages in pagevec ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100809 DBG_BUGON(!page);
810 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800811
Gao Xiang97e86a82019-07-31 23:57:47 +0800812 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800813 continue;
814
815 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
816 pagenr = 0;
817 else
818 pagenr = z_erofs_onlinepage_index(page);
819
Gao Xiang70b17992018-12-11 15:17:49 +0800820 DBG_BUGON(pagenr >= nr_pages);
Gao Xiange5e3abb2018-09-19 13:49:07 +0800821
Gao Xiange12a0ce2019-08-21 22:01:52 +0800822 /*
823 * currently EROFS doesn't support multiref(dedup),
824 * so here erroring out one multiref page.
825 */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800826 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800827 DBG_BUGON(1);
828 SetPageError(pages[pagenr]);
829 z_erofs_onlinepage_endio(pages[pagenr]);
830 err = -EFSCORRUPTED;
831 }
Gao Xiang3883a792018-07-26 20:22:06 +0800832 pages[pagenr] = page;
833 }
Gao Xiang3883a792018-07-26 20:22:06 +0800834 z_erofs_pagevec_ctor_exit(&ctor, true);
835
836 overlapped = false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800837 compressed_pages = pcl->compressed_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800838
839 for (i = 0; i < clusterpages; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200840 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800841
842 page = compressed_pages[i];
843
844 /* all compressed pages ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100845 DBG_BUGON(!page);
846 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800847
Gao Xiang27481232019-06-24 15:22:54 +0800848 if (!z_erofs_page_is_staging(page)) {
Gao Xiangd61fbb62019-03-25 11:40:08 +0800849 if (erofs_page_is_managed(sbi, page)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800850 if (!PageUptodate(page))
Gao Xiang11152492019-03-25 11:40:07 +0800851 err = -EIO;
852 continue;
853 }
Gao Xiang3883a792018-07-26 20:22:06 +0800854
Gao Xiang11152492019-03-25 11:40:07 +0800855 /*
856 * only if non-head page can be selected
857 * for inplace decompression
858 */
859 pagenr = z_erofs_onlinepage_index(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800860
Gao Xiang11152492019-03-25 11:40:07 +0800861 DBG_BUGON(pagenr >= nr_pages);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800862 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800863 DBG_BUGON(1);
864 SetPageError(pages[pagenr]);
865 z_erofs_onlinepage_endio(pages[pagenr]);
866 err = -EFSCORRUPTED;
867 }
Gao Xiang11152492019-03-25 11:40:07 +0800868 pages[pagenr] = page;
Gao Xiang3883a792018-07-26 20:22:06 +0800869
Gao Xiang11152492019-03-25 11:40:07 +0800870 overlapped = true;
871 }
872
873 /* PG_error needs checking for inplaced and staging pages */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800874 if (PageError(page)) {
Gao Xiang11152492019-03-25 11:40:07 +0800875 DBG_BUGON(PageUptodate(page));
876 err = -EIO;
877 }
Gao Xiang3883a792018-07-26 20:22:06 +0800878 }
879
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800880 if (err)
Gao Xiang11152492019-03-25 11:40:07 +0800881 goto out;
882
Gao Xiang97e86a82019-07-31 23:57:47 +0800883 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
884 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
885 outputsize = llen;
886 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
Gao Xiangb6a76182019-06-24 15:22:58 +0800887 } else {
Gao Xiang97e86a82019-07-31 23:57:47 +0800888 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
Gao Xiangb6a76182019-06-24 15:22:58 +0800889 partial = true;
890 }
Gao Xiang3883a792018-07-26 20:22:06 +0800891
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800892 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
893 .sb = sb,
894 .in = compressed_pages,
895 .out = pages,
Gao Xiang97e86a82019-07-31 23:57:47 +0800896 .pageofs_out = cl->pageofs,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800897 .inputsize = PAGE_SIZE,
898 .outputsize = outputsize,
Gao Xiang97e86a82019-07-31 23:57:47 +0800899 .alg = pcl->algorithmformat,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800900 .inplace_io = overlapped,
Gao Xiangb6a76182019-06-24 15:22:58 +0800901 .partial_decoding = partial
Gao Xiang97e86a82019-07-31 23:57:47 +0800902 }, pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800903
904out:
Gao Xiangaf692e12019-02-27 13:33:30 +0800905 /* must handle all compressed pages before endding pages */
Gao Xiang3883a792018-07-26 20:22:06 +0800906 for (i = 0; i < clusterpages; ++i) {
907 page = compressed_pages[i];
908
Gao Xiangd61fbb62019-03-25 11:40:08 +0800909 if (erofs_page_is_managed(sbi, page))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800910 continue;
Gao Xiangd61fbb62019-03-25 11:40:08 +0800911
Gao Xiang3883a792018-07-26 20:22:06 +0800912 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800913 (void)z_erofs_put_stagingpage(pagepool, page);
Gao Xiang3883a792018-07-26 20:22:06 +0800914
915 WRITE_ONCE(compressed_pages[i], NULL);
916 }
917
Gao Xiangaf692e12019-02-27 13:33:30 +0800918 for (i = 0; i < nr_pages; ++i) {
919 page = pages[i];
920 if (!page)
921 continue;
922
923 DBG_BUGON(!page->mapping);
924
925 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800926 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiangaf692e12019-02-27 13:33:30 +0800927 continue;
928
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800929 if (err < 0)
Gao Xiangaf692e12019-02-27 13:33:30 +0800930 SetPageError(page);
931
932 z_erofs_onlinepage_endio(page);
933 }
934
Gao Xiang3883a792018-07-26 20:22:06 +0800935 if (pages == z_pagemap_global)
936 mutex_unlock(&z_pagemap_global_lock);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800937 else if (pages != pages_onstack)
Gao Xiang3883a792018-07-26 20:22:06 +0800938 kvfree(pages);
939
Gao Xiang97e86a82019-07-31 23:57:47 +0800940 cl->nr_pages = 0;
941 cl->vcnt = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800942
Gao Xiang97e86a82019-07-31 23:57:47 +0800943 /* all cl locks MUST be taken before the following line */
944 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800945
Gao Xiang97e86a82019-07-31 23:57:47 +0800946 /* all cl locks SHOULD be released right now */
947 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800948
Gao Xiang97e86a82019-07-31 23:57:47 +0800949 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800950 return err;
951}
952
953static void z_erofs_vle_unzip_all(struct super_block *sb,
Gao Xiang97e86a82019-07-31 23:57:47 +0800954 struct z_erofs_unzip_io *io,
955 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800956{
Gao Xiang97e86a82019-07-31 23:57:47 +0800957 z_erofs_next_pcluster_t owned = io->head;
Gao Xiang3883a792018-07-26 20:22:06 +0800958
Gao Xiang97e86a82019-07-31 23:57:47 +0800959 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
960 struct z_erofs_pcluster *pcl;
Gao Xiang3883a792018-07-26 20:22:06 +0800961
962 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800963 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800964
965 /* no possible that 'owned' equals NULL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800966 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800967
Gao Xiang97e86a82019-07-31 23:57:47 +0800968 pcl = container_of(owned, struct z_erofs_pcluster, next);
969 owned = READ_ONCE(pcl->next);
Gao Xiang3883a792018-07-26 20:22:06 +0800970
Gao Xiang97e86a82019-07-31 23:57:47 +0800971 z_erofs_decompress_pcluster(sb, pcl, pagepool);
Gao Xiang3978c8e2018-08-06 11:27:53 +0800972 }
Gao Xiang3883a792018-07-26 20:22:06 +0800973}
974
975static void z_erofs_vle_unzip_wq(struct work_struct *work)
976{
Gao Xiang97e86a82019-07-31 23:57:47 +0800977 struct z_erofs_unzip_io_sb *iosb =
978 container_of(work, struct z_erofs_unzip_io_sb, io.u.work);
979 LIST_HEAD(pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800980
Gao Xiang97e86a82019-07-31 23:57:47 +0800981 DBG_BUGON(iosb->io.head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
982 z_erofs_vle_unzip_all(iosb->sb, &iosb->io, &pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800983
Gao Xiang97e86a82019-07-31 23:57:47 +0800984 put_pages_list(&pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800985 kvfree(iosb);
986}
987
Gao Xiang97e86a82019-07-31 23:57:47 +0800988static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
989 unsigned int nr,
990 struct list_head *pagepool,
991 struct address_space *mc,
992 gfp_t gfp)
Gao Xiang9248fce2018-12-08 00:19:15 +0800993{
994 /* determined at compile time to avoid too many #ifdefs */
995 const bool nocache = __builtin_constant_p(mc) ? !mc : false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800996 const pgoff_t index = pcl->obj.index;
Gao Xiang9248fce2018-12-08 00:19:15 +0800997 bool tocache = false;
998
999 struct address_space *mapping;
1000 struct page *oldpage, *page;
1001
Gao Xiang92e6efd2018-12-08 00:19:16 +08001002 compressed_page_t t;
1003 int justfound;
1004
Gao Xiang9248fce2018-12-08 00:19:15 +08001005repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +08001006 page = READ_ONCE(pcl->compressed_pages[nr]);
Gao Xiang9248fce2018-12-08 00:19:15 +08001007 oldpage = page;
1008
1009 if (!page)
1010 goto out_allocpage;
1011
1012 /*
1013 * the cached page has not been allocated and
1014 * an placeholder is out there, prepare it now.
1015 */
1016 if (!nocache && page == PAGE_UNALLOCATED) {
1017 tocache = true;
1018 goto out_allocpage;
1019 }
1020
Gao Xiang92e6efd2018-12-08 00:19:16 +08001021 /* process the target tagged pointer */
1022 t = tagptr_init(compressed_page_t, page);
1023 justfound = tagptr_unfold_tags(t);
1024 page = tagptr_unfold_ptr(t);
1025
Gao Xiang9248fce2018-12-08 00:19:15 +08001026 mapping = READ_ONCE(page->mapping);
1027
1028 /*
1029 * if managed cache is disabled, it's no way to
1030 * get such a cached-like page.
1031 */
1032 if (nocache) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001033 /* if managed cache is disabled, it is impossible `justfound' */
1034 DBG_BUGON(justfound);
1035
1036 /* and it should be locked, not uptodate, and not truncated */
Gao Xiang9248fce2018-12-08 00:19:15 +08001037 DBG_BUGON(!PageLocked(page));
1038 DBG_BUGON(PageUptodate(page));
1039 DBG_BUGON(!mapping);
1040 goto out;
1041 }
1042
1043 /*
1044 * unmanaged (file) pages are all locked solidly,
1045 * therefore it is impossible for `mapping' to be NULL.
1046 */
1047 if (mapping && mapping != mc)
1048 /* ought to be unmanaged pages */
1049 goto out;
1050
1051 lock_page(page);
1052
Gao Xiang92e6efd2018-12-08 00:19:16 +08001053 /* only true if page reclaim goes wrong, should never happen */
1054 DBG_BUGON(justfound && PagePrivate(page));
1055
Gao Xiang9248fce2018-12-08 00:19:15 +08001056 /* the page is still in manage cache */
1057 if (page->mapping == mc) {
Gao Xiang97e86a82019-07-31 23:57:47 +08001058 WRITE_ONCE(pcl->compressed_pages[nr], page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001059
Gao Xiang11152492019-03-25 11:40:07 +08001060 ClearPageError(page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001061 if (!PagePrivate(page)) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001062 /*
1063 * impossible to be !PagePrivate(page) for
1064 * the current restriction as well if
1065 * the page is already in compressed_pages[].
1066 */
1067 DBG_BUGON(!justfound);
1068
1069 justfound = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +08001070 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001071 SetPagePrivate(page);
1072 }
1073
1074 /* no need to submit io if it is already up-to-date */
1075 if (PageUptodate(page)) {
1076 unlock_page(page);
1077 page = NULL;
1078 }
1079 goto out;
1080 }
1081
1082 /*
1083 * the managed page has been truncated, it's unsafe to
1084 * reuse this one, let's allocate a new cache-managed page.
1085 */
1086 DBG_BUGON(page->mapping);
Gao Xiang92e6efd2018-12-08 00:19:16 +08001087 DBG_BUGON(!justfound);
Gao Xiang9248fce2018-12-08 00:19:15 +08001088
1089 tocache = true;
1090 unlock_page(page);
1091 put_page(page);
1092out_allocpage:
1093 page = __stagingpage_alloc(pagepool, gfp);
Gao Xiang97e86a82019-07-31 23:57:47 +08001094 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
Gao Xiang9248fce2018-12-08 00:19:15 +08001095 list_add(&page->lru, pagepool);
1096 cpu_relax();
1097 goto repeat;
1098 }
1099 if (nocache || !tocache)
1100 goto out;
1101 if (add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1102 page->mapping = Z_EROFS_MAPPING_STAGING;
1103 goto out;
1104 }
1105
Gao Xiang97e86a82019-07-31 23:57:47 +08001106 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001107 SetPagePrivate(page);
1108out: /* the only exit (for tracing and debugging) */
1109 return page;
1110}
1111
Gao Xiang97e86a82019-07-31 23:57:47 +08001112static struct z_erofs_unzip_io *jobqueue_init(struct super_block *sb,
1113 struct z_erofs_unzip_io *io,
1114 bool foreground)
Gao Xiang3883a792018-07-26 20:22:06 +08001115{
Gao Xiang97e86a82019-07-31 23:57:47 +08001116 struct z_erofs_unzip_io_sb *iosb;
Gao Xiang3883a792018-07-26 20:22:06 +08001117
Gao Xiang7146a4f2018-12-08 00:19:18 +08001118 if (foreground) {
Gao Xiang3883a792018-07-26 20:22:06 +08001119 /* waitqueue available for foreground io */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001120 DBG_BUGON(!io);
Gao Xiang3883a792018-07-26 20:22:06 +08001121
1122 init_waitqueue_head(&io->u.wait);
1123 atomic_set(&io->pending_bios, 0);
1124 goto out;
1125 }
1126
Shobhit Kukretia9f69bd2019-06-26 22:31:18 -07001127 iosb = kvzalloc(sizeof(*iosb), GFP_KERNEL | __GFP_NOFAIL);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001128 DBG_BUGON(!iosb);
Gao Xiang3883a792018-07-26 20:22:06 +08001129
Gao Xiang7146a4f2018-12-08 00:19:18 +08001130 /* initialize fields in the allocated descriptor */
1131 io = &iosb->io;
Gao Xiang3883a792018-07-26 20:22:06 +08001132 iosb->sb = sb;
1133 INIT_WORK(&io->u.work, z_erofs_vle_unzip_wq);
1134out:
Gao Xiang97e86a82019-07-31 23:57:47 +08001135 io->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
Gao Xiang3883a792018-07-26 20:22:06 +08001136 return io;
1137}
1138
Gao Xiang97e86a82019-07-31 23:57:47 +08001139/* define decompression jobqueue types */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001140enum {
Gao Xiang7146a4f2018-12-08 00:19:18 +08001141 JQ_BYPASS,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001142 JQ_SUBMIT,
1143 NR_JOBQUEUES,
1144};
1145
1146static void *jobqueueset_init(struct super_block *sb,
Gao Xiang97e86a82019-07-31 23:57:47 +08001147 z_erofs_next_pcluster_t qtail[],
1148 struct z_erofs_unzip_io *q[],
1149 struct z_erofs_unzip_io *fgq,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001150 bool forcefg)
1151{
Gao Xiang7146a4f2018-12-08 00:19:18 +08001152 /*
1153 * if managed cache is enabled, bypass jobqueue is needed,
Gao Xiang97e86a82019-07-31 23:57:47 +08001154 * no need to read from device for all pclusters in this queue.
Gao Xiang7146a4f2018-12-08 00:19:18 +08001155 */
1156 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, true);
1157 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001158
1159 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, forcefg);
1160 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1161
1162 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], !forcefg));
1163}
1164
Gao Xiang97e86a82019-07-31 23:57:47 +08001165static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1166 z_erofs_next_pcluster_t qtail[],
1167 z_erofs_next_pcluster_t owned_head)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001168{
Gao Xiang97e86a82019-07-31 23:57:47 +08001169 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1170 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001171
Gao Xiang97e86a82019-07-31 23:57:47 +08001172 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1173 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1174 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001175
Gao Xiang97e86a82019-07-31 23:57:47 +08001176 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001177
1178 WRITE_ONCE(*submit_qtail, owned_head);
Gao Xiang97e86a82019-07-31 23:57:47 +08001179 WRITE_ONCE(*bypass_qtail, &pcl->next);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001180
Gao Xiang97e86a82019-07-31 23:57:47 +08001181 qtail[JQ_BYPASS] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001182}
1183
Gao Xiang97e86a82019-07-31 23:57:47 +08001184static bool postsubmit_is_all_bypassed(struct z_erofs_unzip_io *q[],
Gao Xiang7146a4f2018-12-08 00:19:18 +08001185 unsigned int nr_bios,
1186 bool force_fg)
1187{
1188 /*
1189 * although background is preferred, no one is pending for submission.
1190 * don't issue workqueue for decompression but drop it directly instead.
1191 */
1192 if (force_fg || nr_bios)
1193 return false;
1194
Gao Xiang97e86a82019-07-31 23:57:47 +08001195 kvfree(container_of(q[JQ_SUBMIT], struct z_erofs_unzip_io_sb, io));
Gao Xiang7146a4f2018-12-08 00:19:18 +08001196 return true;
1197}
Gao Xiang3883a792018-07-26 20:22:06 +08001198
1199static bool z_erofs_vle_submit_all(struct super_block *sb,
Gao Xiang97e86a82019-07-31 23:57:47 +08001200 z_erofs_next_pcluster_t owned_head,
Gao Xiang3883a792018-07-26 20:22:06 +08001201 struct list_head *pagepool,
Gao Xiang97e86a82019-07-31 23:57:47 +08001202 struct z_erofs_unzip_io *fgq,
Gao Xiang3883a792018-07-26 20:22:06 +08001203 bool force_fg)
1204{
Gao Xiang97e86a82019-07-31 23:57:47 +08001205 struct erofs_sb_info *const sbi __maybe_unused = EROFS_SB(sb);
1206 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1207 struct z_erofs_unzip_io *q[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001208 struct bio *bio;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001209 void *bi_private;
Gao Xiang3883a792018-07-26 20:22:06 +08001210 /* since bio will be NULL, no need to initialize last_index */
1211 pgoff_t uninitialized_var(last_index);
1212 bool force_submit = false;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +02001213 unsigned int nr_bios;
Gao Xiang3883a792018-07-26 20:22:06 +08001214
Gao Xiang8d8a09b2019-08-30 00:38:27 +08001215 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +08001216 return false;
1217
Gao Xiang3883a792018-07-26 20:22:06 +08001218 force_submit = false;
1219 bio = NULL;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001220 nr_bios = 0;
1221 bi_private = jobqueueset_init(sb, qtail, q, fgq, force_fg);
Gao Xiang3883a792018-07-26 20:22:06 +08001222
1223 /* by default, all need io submission */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001224 q[JQ_SUBMIT]->head = owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001225
1226 do {
Gao Xiang97e86a82019-07-31 23:57:47 +08001227 struct z_erofs_pcluster *pcl;
1228 unsigned int clusterpages;
Gao Xiang3883a792018-07-26 20:22:06 +08001229 pgoff_t first_index;
Gao Xiang9248fce2018-12-08 00:19:15 +08001230 struct page *page;
1231 unsigned int i = 0, bypass = 0;
Gao Xiang3883a792018-07-26 20:22:06 +08001232 int err;
1233
1234 /* no possible 'owned_head' equals the following */
Gao Xiang97e86a82019-07-31 23:57:47 +08001235 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1236 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001237
Gao Xiang97e86a82019-07-31 23:57:47 +08001238 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1239
1240 clusterpages = BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +08001241
1242 /* close the main owned chain at first */
Gao Xiang97e86a82019-07-31 23:57:47 +08001243 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1244 Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +08001245
Gao Xiang97e86a82019-07-31 23:57:47 +08001246 first_index = pcl->obj.index;
Gao Xiang3883a792018-07-26 20:22:06 +08001247 force_submit |= (first_index != last_index + 1);
Gao Xiang9248fce2018-12-08 00:19:15 +08001248
Gao Xiang3883a792018-07-26 20:22:06 +08001249repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +08001250 page = pickup_page_for_submission(pcl, i, pagepool,
1251 MNGD_MAPPING(sbi),
1252 GFP_NOFS);
Gao Xiang9248fce2018-12-08 00:19:15 +08001253 if (!page) {
1254 force_submit = true;
1255 ++bypass;
1256 goto skippage;
Gao Xiang3883a792018-07-26 20:22:06 +08001257 }
1258
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001259 if (bio && force_submit) {
Gao Xiang3883a792018-07-26 20:22:06 +08001260submit_bio_retry:
1261 __submit_bio(bio, REQ_OP_READ, 0);
1262 bio = NULL;
1263 }
1264
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001265 if (!bio) {
Gao Xiang8be31272018-08-21 22:49:29 +08001266 bio = erofs_grab_bio(sb, first_index + i,
Gao Xiang14a56ec2019-03-25 11:40:09 +08001267 BIO_MAX_PAGES, bi_private,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001268 z_erofs_vle_read_endio, true);
Gao Xiang3883a792018-07-26 20:22:06 +08001269 ++nr_bios;
1270 }
1271
1272 err = bio_add_page(bio, page, PAGE_SIZE, 0);
1273 if (err < PAGE_SIZE)
1274 goto submit_bio_retry;
1275
1276 force_submit = false;
1277 last_index = first_index + i;
Gao Xiang105d4ad2018-07-26 20:22:07 +08001278skippage:
Gao Xiang3883a792018-07-26 20:22:06 +08001279 if (++i < clusterpages)
1280 goto repeat;
Gao Xiang105d4ad2018-07-26 20:22:07 +08001281
Gao Xiang7146a4f2018-12-08 00:19:18 +08001282 if (bypass < clusterpages)
Gao Xiang97e86a82019-07-31 23:57:47 +08001283 qtail[JQ_SUBMIT] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001284 else
Gao Xiang97e86a82019-07-31 23:57:47 +08001285 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1286 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001287
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001288 if (bio)
Gao Xiang3883a792018-07-26 20:22:06 +08001289 __submit_bio(bio, REQ_OP_READ, 0);
1290
Gao Xiang7146a4f2018-12-08 00:19:18 +08001291 if (postsubmit_is_all_bypassed(q, nr_bios, force_fg))
Gao Xiang105d4ad2018-07-26 20:22:07 +08001292 return true;
Gao Xiang3883a792018-07-26 20:22:06 +08001293
Gao Xiang7146a4f2018-12-08 00:19:18 +08001294 z_erofs_vle_unzip_kickoff(bi_private, nr_bios);
Gao Xiang3883a792018-07-26 20:22:06 +08001295 return true;
1296}
1297
Gao Xiang97e86a82019-07-31 23:57:47 +08001298static void z_erofs_submit_and_unzip(struct super_block *sb,
1299 struct z_erofs_collector *clt,
Gao Xiang3883a792018-07-26 20:22:06 +08001300 struct list_head *pagepool,
1301 bool force_fg)
1302{
Gao Xiang97e86a82019-07-31 23:57:47 +08001303 struct z_erofs_unzip_io io[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001304
Gao Xiang97e86a82019-07-31 23:57:47 +08001305 if (!z_erofs_vle_submit_all(sb, clt->owned_head,
1306 pagepool, io, force_fg))
Gao Xiang3883a792018-07-26 20:22:06 +08001307 return;
1308
Gao Xiang97e86a82019-07-31 23:57:47 +08001309 /* decompress no I/O pclusters immediately */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001310 z_erofs_vle_unzip_all(sb, &io[JQ_BYPASS], pagepool);
Gao Xiang4279f3f2019-07-31 23:57:49 +08001311
Gao Xiang3883a792018-07-26 20:22:06 +08001312 if (!force_fg)
1313 return;
1314
1315 /* wait until all bios are completed */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001316 wait_event(io[JQ_SUBMIT].u.wait,
1317 !atomic_read(&io[JQ_SUBMIT].pending_bios));
Gao Xiang3883a792018-07-26 20:22:06 +08001318
1319 /* let's synchronous decompression */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001320 z_erofs_vle_unzip_all(sb, &io[JQ_SUBMIT], pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001321}
1322
1323static int z_erofs_vle_normalaccess_readpage(struct file *file,
1324 struct page *page)
1325{
1326 struct inode *const inode = page->mapping->host;
Gao Xiang97e86a82019-07-31 23:57:47 +08001327 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001328 int err;
1329 LIST_HEAD(pagepool);
1330
Gao Xiangba9ce772018-11-23 01:15:58 +08001331 trace_erofs_readpage(page, false);
1332
Gao Xiangf0c519f2018-11-23 01:21:49 +08001333 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1334
Gao Xiang3883a792018-07-26 20:22:06 +08001335 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xiang97e86a82019-07-31 23:57:47 +08001336 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001337
Gao Xiangee451972019-08-19 18:34:21 +08001338 /* if some compressed cluster ready, need submit them anyway */
Gao Xiang97e86a82019-07-31 23:57:47 +08001339 z_erofs_submit_and_unzip(inode->i_sb, &f.clt, &pagepool, true);
Gao Xiangee451972019-08-19 18:34:21 +08001340
1341 if (err)
1342 errln("%s, failed to read, err [%d]", __func__, err);
1343
Chao Yu3b423412019-01-15 09:42:21 +08001344 if (f.map.mpage)
1345 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001346
1347 /* clean up the remaining free pages */
1348 put_pages_list(&pagepool);
Gao Xiangee451972019-08-19 18:34:21 +08001349 return err;
Gao Xiang3883a792018-07-26 20:22:06 +08001350}
1351
Gao Xiang14f362b2019-07-31 23:57:36 +08001352static bool should_decompress_synchronously(struct erofs_sb_info *sbi,
1353 unsigned int nr)
1354{
1355 return nr <= sbi->max_sync_decompress_pages;
1356}
1357
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001358static int z_erofs_vle_normalaccess_readpages(struct file *filp,
1359 struct address_space *mapping,
1360 struct list_head *pages,
1361 unsigned int nr_pages)
Gao Xiang3883a792018-07-26 20:22:06 +08001362{
1363 struct inode *const inode = mapping->host;
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001364 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001365
Gao Xiang14f362b2019-07-31 23:57:36 +08001366 bool sync = should_decompress_synchronously(sbi, nr_pages);
Gao Xiang97e86a82019-07-31 23:57:47 +08001367 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001368 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
1369 struct page *head = NULL;
1370 LIST_HEAD(pagepool);
1371
Chen Gong284db122018-09-18 22:27:27 +08001372 trace_erofs_readpages(mapping->host, lru_to_page(pages),
1373 nr_pages, false);
1374
Gao Xiangf0c519f2018-11-23 01:21:49 +08001375 f.headoffset = (erofs_off_t)lru_to_page(pages)->index << PAGE_SHIFT;
1376
Gao Xiang3883a792018-07-26 20:22:06 +08001377 for (; nr_pages; --nr_pages) {
1378 struct page *page = lru_to_page(pages);
1379
1380 prefetchw(&page->flags);
1381 list_del(&page->lru);
1382
Gao Xiang2d9b5dc2018-11-23 01:21:48 +08001383 /*
1384 * A pure asynchronous readahead is indicated if
1385 * a PG_readahead marked page is hitted at first.
1386 * Let's also do asynchronous decompression for this case.
1387 */
1388 sync &= !(PageReadahead(page) && !head);
1389
Gao Xiang3883a792018-07-26 20:22:06 +08001390 if (add_to_page_cache_lru(page, mapping, page->index, gfp)) {
1391 list_add(&page->lru, &pagepool);
1392 continue;
1393 }
1394
Gao Xiang3883a792018-07-26 20:22:06 +08001395 set_page_private(page, (unsigned long)head);
1396 head = page;
1397 }
1398
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001399 while (head) {
Gao Xiang3883a792018-07-26 20:22:06 +08001400 struct page *page = head;
1401 int err;
1402
1403 /* traversal in reverse order */
1404 head = (void *)page_private(page);
1405
1406 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xianga5876e22019-09-04 10:08:56 +08001407 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +08001408 errln("%s, readahead error at page %lu of nid %llu",
Gao Xianga5876e22019-09-04 10:08:56 +08001409 __func__, page->index, EROFS_I(inode)->nid);
Gao Xiang3883a792018-07-26 20:22:06 +08001410 put_page(page);
1411 }
1412
Gao Xiang97e86a82019-07-31 23:57:47 +08001413 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001414
Gao Xiang97e86a82019-07-31 23:57:47 +08001415 z_erofs_submit_and_unzip(inode->i_sb, &f.clt, &pagepool, sync);
Gao Xiang3883a792018-07-26 20:22:06 +08001416
Chao Yu3b423412019-01-15 09:42:21 +08001417 if (f.map.mpage)
1418 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001419
1420 /* clean up the remaining free pages */
1421 put_pages_list(&pagepool);
1422 return 0;
1423}
1424
Gao Xiang3883a792018-07-26 20:22:06 +08001425const struct address_space_operations z_erofs_vle_normalaccess_aops = {
1426 .readpage = z_erofs_vle_normalaccess_readpage,
1427 .readpages = z_erofs_vle_normalaccess_readpages,
1428};
Gao Xiang02827e12018-07-26 20:21:58 +08001429