blob: 187f93b4900e121c12d93f9c987d7ff15f842456 [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 Xiang0a0b7e62018-10-09 21:43:53 +080070int __init z_erofs_init_zip_subsystem(void)
Gao Xiang3883a792018-07-26 20:22:06 +080071{
Gao Xiang97e86a82019-07-31 23:57:47 +080072 pcluster_cachep = kmem_cache_create("erofs_compress",
73 Z_EROFS_WORKGROUP_SIZE, 0,
Gao Xiang99634bf2019-09-04 10:09:05 +080074 SLAB_RECLAIM_ACCOUNT,
75 z_erofs_pcluster_init_once);
Gao Xiang97e86a82019-07-31 23:57:47 +080076 if (pcluster_cachep) {
Gao Xiang99634bf2019-09-04 10:09:05 +080077 if (!z_erofs_init_workqueue())
Gao Xiang3883a792018-07-26 20:22:06 +080078 return 0;
79
Gao Xiang97e86a82019-07-31 23:57:47 +080080 kmem_cache_destroy(pcluster_cachep);
Gao Xiang3883a792018-07-26 20:22:06 +080081 }
82 return -ENOMEM;
83}
84
Gao Xiang97e86a82019-07-31 23:57:47 +080085enum z_erofs_collectmode {
86 COLLECT_SECONDARY,
87 COLLECT_PRIMARY,
Gao Xiang3883a792018-07-26 20:22:06 +080088 /*
Gao Xiang97e86a82019-07-31 23:57:47 +080089 * The current collection was the tail of an exist chain, in addition
90 * that the previous processed chained collections are all decided to
91 * be hooked up to it.
92 * A new chain will be created for the remaining collections which are
93 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
94 * the next collection cannot reuse the whole page safely in
95 * the following scenario:
Gao Xianga1121522019-02-27 13:33:32 +080096 * ________________________________________________________________
97 * | tail (partial) page | head (partial) page |
Gao Xiang97e86a82019-07-31 23:57:47 +080098 * | (belongs to the next cl) | (belongs to the current cl) |
Gao Xianga1121522019-02-27 13:33:32 +080099 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
100 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800101 COLLECT_PRIMARY_HOOKED,
102 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
Gao Xianga1121522019-02-27 13:33:32 +0800103 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800104 * The current collection has been linked with the owned chain, and
105 * could also be linked with the remaining collections, which means
106 * if the processing page is the tail page of the collection, thus
107 * the current collection can safely use the whole page (since
108 * the previous collection is under control) for in-place I/O, as
109 * illustrated below:
Gao Xianga1121522019-02-27 13:33:32 +0800110 * ________________________________________________________________
Gao Xiang97e86a82019-07-31 23:57:47 +0800111 * | tail (partial) page | head (partial) page |
112 * | (of the current cl) | (of the previous collection) |
113 * | PRIMARY_FOLLOWED or | |
114 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
Gao Xianga1121522019-02-27 13:33:32 +0800115 *
Gao Xiang97e86a82019-07-31 23:57:47 +0800116 * [ (*) the above page can be used as inplace I/O. ]
Gao Xiang3883a792018-07-26 20:22:06 +0800117 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800118 COLLECT_PRIMARY_FOLLOWED,
Gao Xiang3883a792018-07-26 20:22:06 +0800119};
120
Gao Xiang97e86a82019-07-31 23:57:47 +0800121struct z_erofs_collector {
Gao Xiang3883a792018-07-26 20:22:06 +0800122 struct z_erofs_pagevec_ctor vector;
123
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800124 struct z_erofs_pcluster *pcl, *tailpcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800125 struct z_erofs_collection *cl;
126 struct page **compressedpages;
127 z_erofs_next_pcluster_t owned_head;
128
129 enum z_erofs_collectmode mode;
Gao Xiang3883a792018-07-26 20:22:06 +0800130};
131
Gao Xiang97e86a82019-07-31 23:57:47 +0800132struct z_erofs_decompress_frontend {
133 struct inode *const inode;
134
135 struct z_erofs_collector clt;
136 struct erofs_map_blocks map;
137
138 /* used for applying cache strategy on the fly */
139 bool backmost;
140 erofs_off_t headoffset;
141};
142
143#define COLLECTOR_INIT() { \
144 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
145 .mode = COLLECT_PRIMARY_FOLLOWED }
146
147#define DECOMPRESS_FRONTEND_INIT(__i) { \
148 .inode = __i, .clt = COLLECTOR_INIT(), \
149 .backmost = true, }
150
151static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
152static DEFINE_MUTEX(z_pagemap_global_lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800153
Gao Xiang97e86a82019-07-31 23:57:47 +0800154static void preload_compressed_pages(struct z_erofs_collector *clt,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800155 struct address_space *mc,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800156 enum z_erofs_cache_alloctype type,
Gao Xiang97e86a82019-07-31 23:57:47 +0800157 struct list_head *pagepool)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800158{
Gao Xiang97e86a82019-07-31 23:57:47 +0800159 const struct z_erofs_pcluster *pcl = clt->pcl;
160 const unsigned int clusterpages = BIT(pcl->clusterbits);
161 struct page **pages = clt->compressedpages;
162 pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
Gao Xiang92e6efd2018-12-08 00:19:16 +0800163 bool standalone = true;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800164
Gao Xiang97e86a82019-07-31 23:57:47 +0800165 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800166 return;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800167
Gao Xiang97e86a82019-07-31 23:57:47 +0800168 for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
Gao Xiang92e6efd2018-12-08 00:19:16 +0800169 struct page *page;
170 compressed_page_t t;
171
172 /* the compressed page was loaded before */
Gao Xiang97e86a82019-07-31 23:57:47 +0800173 if (READ_ONCE(*pages))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800174 continue;
175
Gao Xiang97e86a82019-07-31 23:57:47 +0800176 page = find_get_page(mc, index);
Gao Xiang92e6efd2018-12-08 00:19:16 +0800177
178 if (page) {
179 t = tag_compressed_page_justfound(page);
180 } else if (type == DELAYEDALLOC) {
181 t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
182 } else { /* DONTALLOC */
183 if (standalone)
Gao Xiang97e86a82019-07-31 23:57:47 +0800184 clt->compressedpages = pages;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800185 standalone = false;
186 continue;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800187 }
188
Gao Xiang97e86a82019-07-31 23:57:47 +0800189 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800190 continue;
191
Gao Xiang92e6efd2018-12-08 00:19:16 +0800192 if (page)
193 put_page(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800194 }
Gao Xiang92e6efd2018-12-08 00:19:16 +0800195
Gao Xiang97e86a82019-07-31 23:57:47 +0800196 if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
197 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800198}
199
200/* called by erofs_shrinker to get rid of all compressed_pages */
Gao Xiang47e541a2018-07-29 13:34:58 +0800201int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
Gao Xiang97e86a82019-07-31 23:57:47 +0800202 struct erofs_workgroup *grp)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800203{
Gao Xiang97e86a82019-07-31 23:57:47 +0800204 struct z_erofs_pcluster *const pcl =
205 container_of(grp, struct z_erofs_pcluster, obj);
Gao Xiangc1448fa2018-12-08 00:19:13 +0800206 struct address_space *const mapping = MNGD_MAPPING(sbi);
Gao Xiang97e86a82019-07-31 23:57:47 +0800207 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800208 int i;
209
210 /*
211 * refcount of workgroup is now freezed as 1,
212 * therefore no need to worry about available decompression users.
213 */
214 for (i = 0; i < clusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800215 struct page *page = pcl->compressed_pages[i];
Gao Xiang105d4ad2018-07-26 20:22:07 +0800216
Gao Xiang97e86a82019-07-31 23:57:47 +0800217 if (!page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800218 continue;
219
220 /* block other users from reclaiming or migrating the page */
221 if (!trylock_page(page))
222 return -EBUSY;
223
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800224 if (page->mapping != mapping)
Gao Xiang97e86a82019-07-31 23:57:47 +0800225 continue;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800226
Gao Xiang97e86a82019-07-31 23:57:47 +0800227 /* barrier is implied in the following 'unlock_page' */
228 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800229 set_page_private(page, 0);
230 ClearPagePrivate(page);
231
232 unlock_page(page);
233 put_page(page);
234 }
235 return 0;
236}
237
Gao Xiang47e541a2018-07-29 13:34:58 +0800238int erofs_try_to_free_cached_page(struct address_space *mapping,
239 struct page *page)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800240{
Gao Xiang97e86a82019-07-31 23:57:47 +0800241 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
242 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800243 int ret = 0; /* 0 - busy */
244
Gao Xiang97e86a82019-07-31 23:57:47 +0800245 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
Gao Xiang105d4ad2018-07-26 20:22:07 +0800246 unsigned int i;
247
248 for (i = 0; i < clusterpages; ++i) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800249 if (pcl->compressed_pages[i] == page) {
250 WRITE_ONCE(pcl->compressed_pages[i], NULL);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800251 ret = 1;
252 break;
253 }
254 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800255 erofs_workgroup_unfreeze(&pcl->obj, 1);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800256
Gao Xiang047d4ab2019-02-16 16:46:50 +0800257 if (ret) {
258 ClearPagePrivate(page);
259 put_page(page);
260 }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800261 }
262 return ret;
263}
Gao Xiang105d4ad2018-07-26 20:22:07 +0800264
Gao Xiang3883a792018-07-26 20:22:06 +0800265/* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
Gao Xiang99634bf2019-09-04 10:09:05 +0800266static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
267 struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +0800268{
Gao Xiang97e86a82019-07-31 23:57:47 +0800269 struct z_erofs_pcluster *const pcl = clt->pcl;
270 const unsigned int clusterpages = BIT(pcl->clusterbits);
271
272 while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
273 if (!cmpxchg(clt->compressedpages++, NULL, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800274 return true;
275 }
Gao Xiang3883a792018-07-26 20:22:06 +0800276 return false;
277}
278
Gao Xiang97e86a82019-07-31 23:57:47 +0800279/* callers must be with collection lock held */
280static int z_erofs_attach_page(struct z_erofs_collector *clt,
281 struct page *page,
282 enum z_erofs_page_type type)
Gao Xiang3883a792018-07-26 20:22:06 +0800283{
284 int ret;
285 bool occupied;
286
Gao Xiang97e86a82019-07-31 23:57:47 +0800287 /* give priority for inplaceio */
288 if (clt->mode >= COLLECT_PRIMARY &&
Julian Merida447a3622019-03-18 20:58:41 -0300289 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
Gao Xiang99634bf2019-09-04 10:09:05 +0800290 z_erofs_try_inplace_io(clt, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800291 return 0;
292
Gao Xiang97e86a82019-07-31 23:57:47 +0800293 ret = z_erofs_pagevec_enqueue(&clt->vector,
Gao Xiang046d64e2019-07-31 23:57:45 +0800294 page, type, &occupied);
Gao Xiang97e86a82019-07-31 23:57:47 +0800295 clt->cl->vcnt += (unsigned int)ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800296
297 return ret ? 0 : -EAGAIN;
298}
299
Gao Xiang97e86a82019-07-31 23:57:47 +0800300static enum z_erofs_collectmode
301try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
302 z_erofs_next_pcluster_t *owned_head)
Gao Xiang3883a792018-07-26 20:22:06 +0800303{
Gao Xiang97e86a82019-07-31 23:57:47 +0800304 /* let's claim these following types of pclusters */
Gao Xiang3883a792018-07-26 20:22:06 +0800305retry:
Gao Xiang97e86a82019-07-31 23:57:47 +0800306 if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
307 /* type 1, nil pcluster */
308 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
309 *owned_head) != Z_EROFS_PCLUSTER_NIL)
Gao Xiang3883a792018-07-26 20:22:06 +0800310 goto retry;
311
Gao Xiang97e86a82019-07-31 23:57:47 +0800312 *owned_head = &pcl->next;
Gao Xianga1121522019-02-27 13:33:32 +0800313 /* lucky, I am the followee :) */
Gao Xiang97e86a82019-07-31 23:57:47 +0800314 return COLLECT_PRIMARY_FOLLOWED;
315 } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
Gao Xiang3883a792018-07-26 20:22:06 +0800316 /*
317 * type 2, link to the end of a existing open chain,
318 * be careful that its submission itself is governed
319 * by the original owned chain.
320 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800321 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
322 *owned_head) != Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +0800323 goto retry;
Gao Xiang97e86a82019-07-31 23:57:47 +0800324 *owned_head = Z_EROFS_PCLUSTER_TAIL;
325 return COLLECT_PRIMARY_HOOKED;
Gao Xianga1121522019-02-27 13:33:32 +0800326 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800327 return COLLECT_PRIMARY; /* :( better luck next time */
Gao Xiang3883a792018-07-26 20:22:06 +0800328}
329
Gao Xiang9e579fc2019-10-08 20:56:12 +0800330static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
331 struct inode *inode,
332 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800333{
Gao Xiang64094a02020-02-20 10:46:42 +0800334 struct z_erofs_pcluster *pcl = clt->pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800335 struct z_erofs_collection *cl;
336 unsigned int length;
Gao Xiang3883a792018-07-26 20:22:06 +0800337
Gao Xiang64094a02020-02-20 10:46:42 +0800338 /* to avoid unexpected loop formed by corrupted images */
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800339 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
340 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800341 return -EFSCORRUPTED;
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800342 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800343
344 cl = z_erofs_primarycollection(pcl);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800345 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800346 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800347 return -EFSCORRUPTED;
Gao Xiang3883a792018-07-26 20:22:06 +0800348 }
349
Gao Xiang97e86a82019-07-31 23:57:47 +0800350 length = READ_ONCE(pcl->length);
351 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
352 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
353 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800354 return -EFSCORRUPTED;
Gao Xiang97e86a82019-07-31 23:57:47 +0800355 }
356 } else {
357 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
Gao Xiang3883a792018-07-26 20:22:06 +0800358
Gao Xiang97e86a82019-07-31 23:57:47 +0800359 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
360 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
Gao Xiang3883a792018-07-26 20:22:06 +0800361
Gao Xiang97e86a82019-07-31 23:57:47 +0800362 while (llen > length &&
363 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
364 cpu_relax();
365 length = READ_ONCE(pcl->length);
366 }
367 }
368 mutex_lock(&cl->lock);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800369 /* used to check tail merging loop due to corrupted images */
370 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
371 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800372 clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800373 /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
374 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
375 clt->tailpcl = NULL;
Gao Xiang97e86a82019-07-31 23:57:47 +0800376 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800377 return 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800378}
379
Gao Xiang9e579fc2019-10-08 20:56:12 +0800380static int z_erofs_register_collection(struct z_erofs_collector *clt,
381 struct inode *inode,
382 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800383{
Gao Xiang97e86a82019-07-31 23:57:47 +0800384 struct z_erofs_pcluster *pcl;
385 struct z_erofs_collection *cl;
Gao Xiang64094a02020-02-20 10:46:42 +0800386 struct erofs_workgroup *grp;
Gao Xiang97e86a82019-07-31 23:57:47 +0800387 int err;
Gao Xiange5e3abb2018-09-19 13:49:07 +0800388
Gao Xiang3883a792018-07-26 20:22:06 +0800389 /* no available workgroup, let's allocate one */
Gao Xiang97e86a82019-07-31 23:57:47 +0800390 pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800391 if (!pcl)
Gao Xiang9e579fc2019-10-08 20:56:12 +0800392 return -ENOMEM;
Gao Xiang3883a792018-07-26 20:22:06 +0800393
Gao Xiang64094a02020-02-20 10:46:42 +0800394 atomic_set(&pcl->obj.refcount, 1);
Gao Xiang97e86a82019-07-31 23:57:47 +0800395 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
Gao Xiang3883a792018-07-26 20:22:06 +0800396
Gao Xiang97e86a82019-07-31 23:57:47 +0800397 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
398 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
399 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800400
Gao Xiang97e86a82019-07-31 23:57:47 +0800401 if (map->m_flags & EROFS_MAP_ZIPPED)
402 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
403 else
404 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
Gao Xiangb6a76182019-06-24 15:22:58 +0800405
Gao Xianga5876e22019-09-04 10:08:56 +0800406 pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
Gao Xiang97e86a82019-07-31 23:57:47 +0800407 pcl->clusterbits -= PAGE_SHIFT;
Gao Xiang3883a792018-07-26 20:22:06 +0800408
Gao Xiang97e86a82019-07-31 23:57:47 +0800409 /* new pclusters should be claimed as type 1, primary and followed */
410 pcl->next = clt->owned_head;
411 clt->mode = COLLECT_PRIMARY_FOLLOWED;
412
413 cl = z_erofs_primarycollection(pcl);
Gao Xiang64094a02020-02-20 10:46:42 +0800414
415 /* must be cleaned before freeing to slab */
416 DBG_BUGON(cl->nr_pages);
417 DBG_BUGON(cl->vcnt);
418
Gao Xiang97e86a82019-07-31 23:57:47 +0800419 cl->pageofs = map->m_la & ~PAGE_MASK;
Gao Xiang3883a792018-07-26 20:22:06 +0800420
Gao Xiang23edf3a2018-11-23 01:21:47 +0800421 /*
422 * lock all primary followed works before visible to others
Gao Xiang97e86a82019-07-31 23:57:47 +0800423 * and mutex_trylock *never* fails for a new pcluster.
Gao Xiang23edf3a2018-11-23 01:21:47 +0800424 */
Gao Xiang64094a02020-02-20 10:46:42 +0800425 DBG_BUGON(!mutex_trylock(&cl->lock));
Gao Xiang23edf3a2018-11-23 01:21:47 +0800426
Gao Xiang64094a02020-02-20 10:46:42 +0800427 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
428 if (IS_ERR(grp)) {
429 err = PTR_ERR(grp);
430 goto err_out;
431 }
432
433 if (grp != &pcl->obj) {
434 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
435 err = -EEXIST;
436 goto err_out;
Gao Xiang3883a792018-07-26 20:22:06 +0800437 }
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800438 /* used to check tail merging loop due to corrupted images */
439 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
440 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800441 clt->owned_head = &pcl->next;
442 clt->pcl = pcl;
443 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800444 return 0;
Gao Xiang64094a02020-02-20 10:46:42 +0800445
446err_out:
447 mutex_unlock(&cl->lock);
448 kmem_cache_free(pcluster_cachep, pcl);
449 return err;
Gao Xiang3883a792018-07-26 20:22:06 +0800450}
451
Gao Xiang97e86a82019-07-31 23:57:47 +0800452static int z_erofs_collector_begin(struct z_erofs_collector *clt,
453 struct inode *inode,
454 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800455{
Gao Xiang64094a02020-02-20 10:46:42 +0800456 struct erofs_workgroup *grp;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800457 int ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800458
Gao Xiang97e86a82019-07-31 23:57:47 +0800459 DBG_BUGON(clt->cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800460
Gao Xiang97e86a82019-07-31 23:57:47 +0800461 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
462 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
463 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +0800464
Gao Xiang97e86a82019-07-31 23:57:47 +0800465 if (!PAGE_ALIGNED(map->m_pa)) {
466 DBG_BUGON(1);
467 return -EINVAL;
468 }
Gao Xiang3883a792018-07-26 20:22:06 +0800469
Gao Xiang64094a02020-02-20 10:46:42 +0800470 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
471 if (grp) {
472 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
473 } else {
Gao Xiang9e579fc2019-10-08 20:56:12 +0800474 ret = z_erofs_register_collection(clt, inode, map);
Gao Xiangb27661c2018-09-19 13:49:06 +0800475
Gao Xiang64094a02020-02-20 10:46:42 +0800476 if (!ret)
477 goto out;
478 if (ret != -EEXIST)
479 return ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800480 }
481
Gao Xiang64094a02020-02-20 10:46:42 +0800482 ret = z_erofs_lookup_collection(clt, inode, map);
483 if (ret) {
484 erofs_workgroup_put(&clt->pcl->obj);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800485 return ret;
Gao Xiang64094a02020-02-20 10:46:42 +0800486 }
Gao Xiang3883a792018-07-26 20:22:06 +0800487
Gao Xiang64094a02020-02-20 10:46:42 +0800488out:
Gao Xiang97e86a82019-07-31 23:57:47 +0800489 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang9e579fc2019-10-08 20:56:12 +0800490 clt->cl->pagevec, clt->cl->vcnt);
Gao Xiang3883a792018-07-26 20:22:06 +0800491
Gao Xiang97e86a82019-07-31 23:57:47 +0800492 clt->compressedpages = clt->pcl->compressed_pages;
493 if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
494 clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
Gao Xiang3883a792018-07-26 20:22:06 +0800495 return 0;
496}
497
498/*
Gao Xiang97e86a82019-07-31 23:57:47 +0800499 * keep in mind that no referenced pclusters will be freed
500 * only after a RCU grace period.
Gao Xiang3883a792018-07-26 20:22:06 +0800501 */
502static void z_erofs_rcu_callback(struct rcu_head *head)
503{
Gao Xiang97e86a82019-07-31 23:57:47 +0800504 struct z_erofs_collection *const cl =
505 container_of(head, struct z_erofs_collection, rcu);
Gao Xiang3883a792018-07-26 20:22:06 +0800506
Gao Xiang97e86a82019-07-31 23:57:47 +0800507 kmem_cache_free(pcluster_cachep,
508 container_of(cl, struct z_erofs_pcluster,
509 primary_collection));
Gao Xiang3883a792018-07-26 20:22:06 +0800510}
511
512void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
513{
Gao Xiang97e86a82019-07-31 23:57:47 +0800514 struct z_erofs_pcluster *const pcl =
515 container_of(grp, struct z_erofs_pcluster, obj);
516 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800517
Gao Xiang97e86a82019-07-31 23:57:47 +0800518 call_rcu(&cl->rcu, z_erofs_rcu_callback);
Gao Xiang3883a792018-07-26 20:22:06 +0800519}
520
Gao Xiang97e86a82019-07-31 23:57:47 +0800521static void z_erofs_collection_put(struct z_erofs_collection *cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800522{
Gao Xiang97e86a82019-07-31 23:57:47 +0800523 struct z_erofs_pcluster *const pcl =
524 container_of(cl, struct z_erofs_pcluster, primary_collection);
525
526 erofs_workgroup_put(&pcl->obj);
Gao Xiang3883a792018-07-26 20:22:06 +0800527}
528
Gao Xiang97e86a82019-07-31 23:57:47 +0800529static bool z_erofs_collector_end(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800530{
Gao Xiang97e86a82019-07-31 23:57:47 +0800531 struct z_erofs_collection *cl = clt->cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800532
Gao Xiang97e86a82019-07-31 23:57:47 +0800533 if (!cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800534 return false;
535
Gao Xiang97e86a82019-07-31 23:57:47 +0800536 z_erofs_pagevec_ctor_exit(&clt->vector, false);
537 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800538
539 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800540 * if all pending pages are added, don't hold its reference
541 * any longer if the pcluster isn't hosted by ourselves.
Gao Xiang3883a792018-07-26 20:22:06 +0800542 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800543 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
544 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800545
Gao Xiang97e86a82019-07-31 23:57:47 +0800546 clt->cl = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +0800547 return true;
548}
549
Gao Xiang97e86a82019-07-31 23:57:47 +0800550static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800551 unsigned int cachestrategy,
Gao Xiang97e86a82019-07-31 23:57:47 +0800552 erofs_off_t la)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800553{
Gao Xiang4279f3f2019-07-31 23:57:49 +0800554 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
555 return false;
556
Gao Xiang92e6efd2018-12-08 00:19:16 +0800557 if (fe->backmost)
558 return true;
559
Gao Xiang4279f3f2019-07-31 23:57:49 +0800560 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
561 la < fe->headoffset;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800562}
Gao Xiang92e6efd2018-12-08 00:19:16 +0800563
Gao Xiang97e86a82019-07-31 23:57:47 +0800564static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
Gao Xiang3883a792018-07-26 20:22:06 +0800565 struct page *page,
Gao Xiang97e86a82019-07-31 23:57:47 +0800566 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800567{
Gao Xiang97e86a82019-07-31 23:57:47 +0800568 struct inode *const inode = fe->inode;
Gao Xiangbda17a42019-10-08 20:56:13 +0800569 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Chao Yu3b423412019-01-15 09:42:21 +0800570 struct erofs_map_blocks *const map = &fe->map;
Gao Xiang97e86a82019-07-31 23:57:47 +0800571 struct z_erofs_collector *const clt = &fe->clt;
Gao Xiang3883a792018-07-26 20:22:06 +0800572 const loff_t offset = page_offset(page);
Gao Xiangdc76ea82019-09-22 18:04:34 +0800573 bool tight = true;
Gao Xiang3883a792018-07-26 20:22:06 +0800574
Gao Xiang92e6efd2018-12-08 00:19:16 +0800575 enum z_erofs_cache_alloctype cache_strategy;
Gao Xiang3883a792018-07-26 20:22:06 +0800576 enum z_erofs_page_type page_type;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200577 unsigned int cur, end, spiltted, index;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800578 int err = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800579
580 /* register locked file pages as online pages in pack */
581 z_erofs_onlinepage_init(page);
582
583 spiltted = 0;
584 end = PAGE_SIZE;
585repeat:
586 cur = end - 1;
587
588 /* lucky, within the range of the current map_blocks */
589 if (offset + cur >= map->m_la &&
Julian Merida447a3622019-03-18 20:58:41 -0300590 offset + cur < map->m_la + map->m_llen) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800591 /* didn't get a valid collection previously (very rare) */
592 if (!clt->cl)
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800593 goto restart_now;
Gao Xiang3883a792018-07-26 20:22:06 +0800594 goto hitted;
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800595 }
Gao Xiang3883a792018-07-26 20:22:06 +0800596
597 /* go ahead the next map_blocks */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800598 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
Gao Xiang3883a792018-07-26 20:22:06 +0800599
Gao Xiang97e86a82019-07-31 23:57:47 +0800600 if (z_erofs_collector_end(clt))
Gao Xiangf0c519f2018-11-23 01:21:49 +0800601 fe->backmost = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800602
603 map->m_la = offset + cur;
604 map->m_llen = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +0800605 err = z_erofs_map_blocks_iter(inode, map, 0);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800606 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800607 goto err_out;
608
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800609restart_now:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800610 if (!(map->m_flags & EROFS_MAP_MAPPED))
Gao Xiang3883a792018-07-26 20:22:06 +0800611 goto hitted;
612
Gao Xiang97e86a82019-07-31 23:57:47 +0800613 err = z_erofs_collector_begin(clt, inode, map);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800614 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800615 goto err_out;
616
Gao Xiang92e6efd2018-12-08 00:19:16 +0800617 /* preload all compressed pages (maybe downgrade role if necessary) */
Gao Xiang4279f3f2019-07-31 23:57:49 +0800618 if (should_alloc_managed_pages(fe, sbi->cache_strategy, map->m_la))
Gao Xiang92e6efd2018-12-08 00:19:16 +0800619 cache_strategy = DELAYEDALLOC;
620 else
621 cache_strategy = DONTALLOC;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800622
Gao Xiang97e86a82019-07-31 23:57:47 +0800623 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
624 cache_strategy, pagepool);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800625
Gao Xiang3883a792018-07-26 20:22:06 +0800626hitted:
Gao Xiangdc76ea82019-09-22 18:04:34 +0800627 /*
628 * Ensure the current partial page belongs to this submit chain rather
629 * than other concurrent submit chains or the noio(bypass) chain since
630 * those chains are handled asynchronously thus the page cannot be used
631 * for inplace I/O or pagevec (should be processed in strict order.)
632 */
633 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
634 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
635
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200636 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800637 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800638 zero_user_segment(page, cur, end);
639 goto next_part;
640 }
641
642 /* let's derive page type */
643 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
644 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
645 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
646 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
647
Gao Xianga1121522019-02-27 13:33:32 +0800648 if (cur)
Gao Xiang97e86a82019-07-31 23:57:47 +0800649 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xianga1121522019-02-27 13:33:32 +0800650
Gao Xiang3883a792018-07-26 20:22:06 +0800651retry:
Gao Xiang97e86a82019-07-31 23:57:47 +0800652 err = z_erofs_attach_page(clt, page, page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800653 /* should allocate an additional staging page for pagevec */
654 if (err == -EAGAIN) {
655 struct page *const newpage =
Gao Xiang5ddcee12019-11-21 21:59:54 +0800656 erofs_allocpage(pagepool, GFP_NOFS | __GFP_NOFAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800657
Gao Xiang5ddcee12019-11-21 21:59:54 +0800658 newpage->mapping = Z_EROFS_MAPPING_STAGING;
Gao Xiang97e86a82019-07-31 23:57:47 +0800659 err = z_erofs_attach_page(clt, newpage,
660 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800661 if (!err)
Gao Xiang3883a792018-07-26 20:22:06 +0800662 goto retry;
663 }
664
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800665 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800666 goto err_out;
667
Gao Xiang97e86a82019-07-31 23:57:47 +0800668 index = page->index - (map->m_la >> PAGE_SHIFT);
Gao Xiang3883a792018-07-26 20:22:06 +0800669
Gao Xiang3883a792018-07-26 20:22:06 +0800670 z_erofs_onlinepage_fixup(page, index, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800671
Gao Xiang1e05ff32018-09-18 22:27:25 +0800672 /* bump up the number of spiltted parts of a page */
673 ++spiltted;
674 /* also update nr_pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800675 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
Gao Xiang3883a792018-07-26 20:22:06 +0800676next_part:
677 /* can be used for verification */
678 map->m_llen = offset + cur - map->m_la;
679
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300680 end = cur;
681 if (end > 0)
Gao Xiang3883a792018-07-26 20:22:06 +0800682 goto repeat;
683
Gao Xiang1e05ff32018-09-18 22:27:25 +0800684out:
Gao Xiang3883a792018-07-26 20:22:06 +0800685 z_erofs_onlinepage_endio(page);
686
Gao Xiang4f761fa2019-09-04 10:09:09 +0800687 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
688 __func__, page, spiltted, map->m_llen);
Gao Xiang3883a792018-07-26 20:22:06 +0800689 return err;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800690
691 /* if some error occurred while processing this page */
692err_out:
693 SetPageError(page);
694 goto out;
Gao Xiang3883a792018-07-26 20:22:06 +0800695}
696
Gao Xianga4b1fab2019-10-08 20:56:15 +0800697static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
698 bool sync, int bios)
Gao Xiang3883a792018-07-26 20:22:06 +0800699{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800700 /* wake up the caller thread for sync decompression */
701 if (sync) {
Gao Xiang848bd9a2018-12-08 00:19:12 +0800702 unsigned long flags;
Gao Xiang3883a792018-07-26 20:22:06 +0800703
Gao Xiang848bd9a2018-12-08 00:19:12 +0800704 spin_lock_irqsave(&io->u.wait.lock, flags);
705 if (!atomic_add_return(bios, &io->pending_bios))
706 wake_up_locked(&io->u.wait);
707 spin_unlock_irqrestore(&io->u.wait.lock, flags);
708 return;
709 }
710
711 if (!atomic_add_return(bios, &io->pending_bios))
Gao Xiang3883a792018-07-26 20:22:06 +0800712 queue_work(z_erofs_workqueue, &io->u.work);
Gao Xiang3883a792018-07-26 20:22:06 +0800713}
714
Gao Xiang0c638f72019-11-08 11:37:33 +0800715static void z_erofs_decompressqueue_endio(struct bio *bio)
Gao Xiang3883a792018-07-26 20:22:06 +0800716{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800717 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
718 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
Gao Xiang14a56ec2019-03-25 11:40:09 +0800719 blk_status_t err = bio->bi_status;
Gao Xiang3883a792018-07-26 20:22:06 +0800720 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800721 struct bvec_iter_all iter_all;
Gao Xiang3883a792018-07-26 20:22:06 +0800722
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200723 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang3883a792018-07-26 20:22:06 +0800724 struct page *page = bvec->bv_page;
725
726 DBG_BUGON(PageUptodate(page));
Gao Xiang70b17992018-12-11 15:17:49 +0800727 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800728
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800729 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800730 SetPageError(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800731
Gao Xianga4b1fab2019-10-08 20:56:15 +0800732 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
733 if (!err)
734 SetPageUptodate(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800735 unlock_page(page);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800736 }
Gao Xiang3883a792018-07-26 20:22:06 +0800737 }
Gao Xianga4b1fab2019-10-08 20:56:15 +0800738 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
Gao Xiang3883a792018-07-26 20:22:06 +0800739 bio_put(bio);
740}
741
Gao Xiang97e86a82019-07-31 23:57:47 +0800742static int z_erofs_decompress_pcluster(struct super_block *sb,
743 struct z_erofs_pcluster *pcl,
744 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800745{
746 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +0800747 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +0800748 struct z_erofs_pagevec_ctor ctor;
Gao Xiang97e86a82019-07-31 23:57:47 +0800749 unsigned int i, outputsize, llen, nr_pages;
750 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
Gao Xiang3883a792018-07-26 20:22:06 +0800751 struct page **pages, **compressed_pages, *page;
Gao Xiang3883a792018-07-26 20:22:06 +0800752
753 enum z_erofs_page_type page_type;
Gao Xiangb6a76182019-06-24 15:22:58 +0800754 bool overlapped, partial;
Gao Xiang97e86a82019-07-31 23:57:47 +0800755 struct z_erofs_collection *cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800756 int err;
757
758 might_sleep();
Gao Xiang97e86a82019-07-31 23:57:47 +0800759 cl = z_erofs_primarycollection(pcl);
760 DBG_BUGON(!READ_ONCE(cl->nr_pages));
Gao Xiang3883a792018-07-26 20:22:06 +0800761
Gao Xiang97e86a82019-07-31 23:57:47 +0800762 mutex_lock(&cl->lock);
763 nr_pages = cl->nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800764
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800765 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
Gao Xiang3883a792018-07-26 20:22:06 +0800766 pages = pages_onstack;
Gao Xiang97e86a82019-07-31 23:57:47 +0800767 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
768 mutex_trylock(&z_pagemap_global_lock)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800769 pages = z_pagemap_global;
Gao Xiang97e86a82019-07-31 23:57:47 +0800770 } else {
Chao Yu441dfcc2019-07-16 17:44:22 +0800771 gfp_t gfp_flags = GFP_KERNEL;
772
Gao Xiang97e86a82019-07-31 23:57:47 +0800773 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
Chao Yu441dfcc2019-07-16 17:44:22 +0800774 gfp_flags |= __GFP_NOFAIL;
775
Julian Merida447a3622019-03-18 20:58:41 -0300776 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Chao Yu441dfcc2019-07-16 17:44:22 +0800777 gfp_flags);
Gao Xiang3883a792018-07-26 20:22:06 +0800778
779 /* fallback to global pagemap for the lowmem scenario */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800780 if (!pages) {
Chao Yu441dfcc2019-07-16 17:44:22 +0800781 mutex_lock(&z_pagemap_global_lock);
782 pages = z_pagemap_global;
Gao Xiang3883a792018-07-26 20:22:06 +0800783 }
784 }
785
786 for (i = 0; i < nr_pages; ++i)
787 pages[i] = NULL;
788
Gao Xiange12a0ce2019-08-21 22:01:52 +0800789 err = 0;
Gao Xiangfa61a332019-06-24 15:22:53 +0800790 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang97e86a82019-07-31 23:57:47 +0800791 cl->pagevec, 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800792
Gao Xiang97e86a82019-07-31 23:57:47 +0800793 for (i = 0; i < cl->vcnt; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200794 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800795
Gao Xiang046d64e2019-07-31 23:57:45 +0800796 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800797
798 /* all pages in pagevec ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100799 DBG_BUGON(!page);
800 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800801
Gao Xiang97e86a82019-07-31 23:57:47 +0800802 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800803 continue;
804
805 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
806 pagenr = 0;
807 else
808 pagenr = z_erofs_onlinepage_index(page);
809
Gao Xiang70b17992018-12-11 15:17:49 +0800810 DBG_BUGON(pagenr >= nr_pages);
Gao Xiange5e3abb2018-09-19 13:49:07 +0800811
Gao Xiange12a0ce2019-08-21 22:01:52 +0800812 /*
813 * currently EROFS doesn't support multiref(dedup),
814 * so here erroring out one multiref page.
815 */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800816 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800817 DBG_BUGON(1);
818 SetPageError(pages[pagenr]);
819 z_erofs_onlinepage_endio(pages[pagenr]);
820 err = -EFSCORRUPTED;
821 }
Gao Xiang3883a792018-07-26 20:22:06 +0800822 pages[pagenr] = page;
823 }
Gao Xiang3883a792018-07-26 20:22:06 +0800824 z_erofs_pagevec_ctor_exit(&ctor, true);
825
826 overlapped = false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800827 compressed_pages = pcl->compressed_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800828
829 for (i = 0; i < clusterpages; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200830 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800831
832 page = compressed_pages[i];
833
834 /* all compressed pages ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100835 DBG_BUGON(!page);
836 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800837
Gao Xiang27481232019-06-24 15:22:54 +0800838 if (!z_erofs_page_is_staging(page)) {
Gao Xiangd61fbb62019-03-25 11:40:08 +0800839 if (erofs_page_is_managed(sbi, page)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800840 if (!PageUptodate(page))
Gao Xiang11152492019-03-25 11:40:07 +0800841 err = -EIO;
842 continue;
843 }
Gao Xiang3883a792018-07-26 20:22:06 +0800844
Gao Xiang11152492019-03-25 11:40:07 +0800845 /*
846 * only if non-head page can be selected
847 * for inplace decompression
848 */
849 pagenr = z_erofs_onlinepage_index(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800850
Gao Xiang11152492019-03-25 11:40:07 +0800851 DBG_BUGON(pagenr >= nr_pages);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800852 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800853 DBG_BUGON(1);
854 SetPageError(pages[pagenr]);
855 z_erofs_onlinepage_endio(pages[pagenr]);
856 err = -EFSCORRUPTED;
857 }
Gao Xiang11152492019-03-25 11:40:07 +0800858 pages[pagenr] = page;
Gao Xiang3883a792018-07-26 20:22:06 +0800859
Gao Xiang11152492019-03-25 11:40:07 +0800860 overlapped = true;
861 }
862
863 /* PG_error needs checking for inplaced and staging pages */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800864 if (PageError(page)) {
Gao Xiang11152492019-03-25 11:40:07 +0800865 DBG_BUGON(PageUptodate(page));
866 err = -EIO;
867 }
Gao Xiang3883a792018-07-26 20:22:06 +0800868 }
869
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800870 if (err)
Gao Xiang11152492019-03-25 11:40:07 +0800871 goto out;
872
Gao Xiang97e86a82019-07-31 23:57:47 +0800873 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
874 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
875 outputsize = llen;
876 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
Gao Xiangb6a76182019-06-24 15:22:58 +0800877 } else {
Gao Xiang97e86a82019-07-31 23:57:47 +0800878 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
Gao Xiangb6a76182019-06-24 15:22:58 +0800879 partial = true;
880 }
Gao Xiang3883a792018-07-26 20:22:06 +0800881
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800882 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
883 .sb = sb,
884 .in = compressed_pages,
885 .out = pages,
Gao Xiang97e86a82019-07-31 23:57:47 +0800886 .pageofs_out = cl->pageofs,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800887 .inputsize = PAGE_SIZE,
888 .outputsize = outputsize,
Gao Xiang97e86a82019-07-31 23:57:47 +0800889 .alg = pcl->algorithmformat,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800890 .inplace_io = overlapped,
Gao Xiangb6a76182019-06-24 15:22:58 +0800891 .partial_decoding = partial
Gao Xiang97e86a82019-07-31 23:57:47 +0800892 }, pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800893
894out:
Gao Xiangaf692e12019-02-27 13:33:30 +0800895 /* must handle all compressed pages before endding pages */
Gao Xiang3883a792018-07-26 20:22:06 +0800896 for (i = 0; i < clusterpages; ++i) {
897 page = compressed_pages[i];
898
Gao Xiangd61fbb62019-03-25 11:40:08 +0800899 if (erofs_page_is_managed(sbi, page))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800900 continue;
Gao Xiangd61fbb62019-03-25 11:40:08 +0800901
Gao Xiang3883a792018-07-26 20:22:06 +0800902 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800903 (void)z_erofs_put_stagingpage(pagepool, page);
Gao Xiang3883a792018-07-26 20:22:06 +0800904
905 WRITE_ONCE(compressed_pages[i], NULL);
906 }
907
Gao Xiangaf692e12019-02-27 13:33:30 +0800908 for (i = 0; i < nr_pages; ++i) {
909 page = pages[i];
910 if (!page)
911 continue;
912
913 DBG_BUGON(!page->mapping);
914
915 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800916 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiangaf692e12019-02-27 13:33:30 +0800917 continue;
918
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800919 if (err < 0)
Gao Xiangaf692e12019-02-27 13:33:30 +0800920 SetPageError(page);
921
922 z_erofs_onlinepage_endio(page);
923 }
924
Gao Xiang3883a792018-07-26 20:22:06 +0800925 if (pages == z_pagemap_global)
926 mutex_unlock(&z_pagemap_global_lock);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800927 else if (pages != pages_onstack)
Gao Xiang3883a792018-07-26 20:22:06 +0800928 kvfree(pages);
929
Gao Xiang97e86a82019-07-31 23:57:47 +0800930 cl->nr_pages = 0;
931 cl->vcnt = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800932
Gao Xiang97e86a82019-07-31 23:57:47 +0800933 /* all cl locks MUST be taken before the following line */
934 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800935
Gao Xiang97e86a82019-07-31 23:57:47 +0800936 /* all cl locks SHOULD be released right now */
937 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800938
Gao Xiang97e86a82019-07-31 23:57:47 +0800939 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800940 return err;
941}
942
Gao Xiang0c638f72019-11-08 11:37:33 +0800943static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
944 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800945{
Gao Xiang97e86a82019-07-31 23:57:47 +0800946 z_erofs_next_pcluster_t owned = io->head;
Gao Xiang3883a792018-07-26 20:22:06 +0800947
Gao Xiang97e86a82019-07-31 23:57:47 +0800948 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
949 struct z_erofs_pcluster *pcl;
Gao Xiang3883a792018-07-26 20:22:06 +0800950
951 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800952 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800953
954 /* no possible that 'owned' equals NULL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800955 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800956
Gao Xiang97e86a82019-07-31 23:57:47 +0800957 pcl = container_of(owned, struct z_erofs_pcluster, next);
958 owned = READ_ONCE(pcl->next);
Gao Xiang3883a792018-07-26 20:22:06 +0800959
Gao Xianga4b1fab2019-10-08 20:56:15 +0800960 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
Gao Xiang3978c8e2018-08-06 11:27:53 +0800961 }
Gao Xiang3883a792018-07-26 20:22:06 +0800962}
963
Gao Xiang0c638f72019-11-08 11:37:33 +0800964static void z_erofs_decompressqueue_work(struct work_struct *work)
Gao Xiang3883a792018-07-26 20:22:06 +0800965{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800966 struct z_erofs_decompressqueue *bgq =
967 container_of(work, struct z_erofs_decompressqueue, u.work);
Gao Xiang97e86a82019-07-31 23:57:47 +0800968 LIST_HEAD(pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800969
Gao Xianga4b1fab2019-10-08 20:56:15 +0800970 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang0c638f72019-11-08 11:37:33 +0800971 z_erofs_decompress_queue(bgq, &pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800972
Gao Xiang97e86a82019-07-31 23:57:47 +0800973 put_pages_list(&pagepool);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800974 kvfree(bgq);
Gao Xiang3883a792018-07-26 20:22:06 +0800975}
976
Gao Xiang97e86a82019-07-31 23:57:47 +0800977static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
978 unsigned int nr,
979 struct list_head *pagepool,
980 struct address_space *mc,
981 gfp_t gfp)
Gao Xiang9248fce2018-12-08 00:19:15 +0800982{
Gao Xiang97e86a82019-07-31 23:57:47 +0800983 const pgoff_t index = pcl->obj.index;
Gao Xiang9248fce2018-12-08 00:19:15 +0800984 bool tocache = false;
985
986 struct address_space *mapping;
987 struct page *oldpage, *page;
988
Gao Xiang92e6efd2018-12-08 00:19:16 +0800989 compressed_page_t t;
990 int justfound;
991
Gao Xiang9248fce2018-12-08 00:19:15 +0800992repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +0800993 page = READ_ONCE(pcl->compressed_pages[nr]);
Gao Xiang9248fce2018-12-08 00:19:15 +0800994 oldpage = page;
995
996 if (!page)
997 goto out_allocpage;
998
999 /*
1000 * the cached page has not been allocated and
1001 * an placeholder is out there, prepare it now.
1002 */
Gao Xiangbda17a42019-10-08 20:56:13 +08001003 if (page == PAGE_UNALLOCATED) {
Gao Xiang9248fce2018-12-08 00:19:15 +08001004 tocache = true;
1005 goto out_allocpage;
1006 }
1007
Gao Xiang92e6efd2018-12-08 00:19:16 +08001008 /* process the target tagged pointer */
1009 t = tagptr_init(compressed_page_t, page);
1010 justfound = tagptr_unfold_tags(t);
1011 page = tagptr_unfold_ptr(t);
1012
Gao Xiang9248fce2018-12-08 00:19:15 +08001013 mapping = READ_ONCE(page->mapping);
1014
1015 /*
Gao Xiang9248fce2018-12-08 00:19:15 +08001016 * unmanaged (file) pages are all locked solidly,
1017 * therefore it is impossible for `mapping' to be NULL.
1018 */
1019 if (mapping && mapping != mc)
1020 /* ought to be unmanaged pages */
1021 goto out;
1022
1023 lock_page(page);
1024
Gao Xiang92e6efd2018-12-08 00:19:16 +08001025 /* only true if page reclaim goes wrong, should never happen */
1026 DBG_BUGON(justfound && PagePrivate(page));
1027
Gao Xiang9248fce2018-12-08 00:19:15 +08001028 /* the page is still in manage cache */
1029 if (page->mapping == mc) {
Gao Xiang97e86a82019-07-31 23:57:47 +08001030 WRITE_ONCE(pcl->compressed_pages[nr], page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001031
Gao Xiang11152492019-03-25 11:40:07 +08001032 ClearPageError(page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001033 if (!PagePrivate(page)) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001034 /*
1035 * impossible to be !PagePrivate(page) for
1036 * the current restriction as well if
1037 * the page is already in compressed_pages[].
1038 */
1039 DBG_BUGON(!justfound);
1040
1041 justfound = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +08001042 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001043 SetPagePrivate(page);
1044 }
1045
1046 /* no need to submit io if it is already up-to-date */
1047 if (PageUptodate(page)) {
1048 unlock_page(page);
1049 page = NULL;
1050 }
1051 goto out;
1052 }
1053
1054 /*
1055 * the managed page has been truncated, it's unsafe to
1056 * reuse this one, let's allocate a new cache-managed page.
1057 */
1058 DBG_BUGON(page->mapping);
Gao Xiang92e6efd2018-12-08 00:19:16 +08001059 DBG_BUGON(!justfound);
Gao Xiang9248fce2018-12-08 00:19:15 +08001060
1061 tocache = true;
1062 unlock_page(page);
1063 put_page(page);
1064out_allocpage:
Gao Xiang5ddcee12019-11-21 21:59:54 +08001065 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1066 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1067 /* non-LRU / non-movable temporary page is needed */
Gao Xiang9248fce2018-12-08 00:19:15 +08001068 page->mapping = Z_EROFS_MAPPING_STAGING;
Gao Xiang5ddcee12019-11-21 21:59:54 +08001069 tocache = false;
Gao Xiang9248fce2018-12-08 00:19:15 +08001070 }
1071
Gao Xiang5ddcee12019-11-21 21:59:54 +08001072 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1073 if (tocache) {
1074 /* since it added to managed cache successfully */
1075 unlock_page(page);
1076 put_page(page);
1077 } else {
1078 list_add(&page->lru, pagepool);
1079 }
1080 cond_resched();
1081 goto repeat;
1082 }
Gao Xiang97e86a82019-07-31 23:57:47 +08001083 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001084 SetPagePrivate(page);
1085out: /* the only exit (for tracing and debugging) */
1086 return page;
1087}
1088
Gao Xianga4b1fab2019-10-08 20:56:15 +08001089static struct z_erofs_decompressqueue *
1090jobqueue_init(struct super_block *sb,
1091 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001092{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001093 struct z_erofs_decompressqueue *q;
Gao Xiang3883a792018-07-26 20:22:06 +08001094
Gao Xianga4b1fab2019-10-08 20:56:15 +08001095 if (fg && !*fg) {
1096 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1097 if (!q) {
1098 *fg = true;
1099 goto fg_out;
1100 }
Gao Xiang0c638f72019-11-08 11:37:33 +08001101 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
Gao Xianga4b1fab2019-10-08 20:56:15 +08001102 } else {
1103fg_out:
1104 q = fgq;
1105 init_waitqueue_head(&fgq->u.wait);
1106 atomic_set(&fgq->pending_bios, 0);
Gao Xiang3883a792018-07-26 20:22:06 +08001107 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001108 q->sb = sb;
1109 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1110 return q;
Gao Xiang3883a792018-07-26 20:22:06 +08001111}
1112
Gao Xiang97e86a82019-07-31 23:57:47 +08001113/* define decompression jobqueue types */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001114enum {
Gao Xiang7146a4f2018-12-08 00:19:18 +08001115 JQ_BYPASS,
Gao Xiang7146a4f2018-12-08 00:19:18 +08001116 JQ_SUBMIT,
1117 NR_JOBQUEUES,
1118};
1119
1120static void *jobqueueset_init(struct super_block *sb,
Gao Xianga4b1fab2019-10-08 20:56:15 +08001121 struct z_erofs_decompressqueue *q[],
1122 struct z_erofs_decompressqueue *fgq, bool *fg)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001123{
Gao Xiang7146a4f2018-12-08 00:19:18 +08001124 /*
1125 * if managed cache is enabled, bypass jobqueue is needed,
Gao Xiang97e86a82019-07-31 23:57:47 +08001126 * no need to read from device for all pclusters in this queue.
Gao Xiang7146a4f2018-12-08 00:19:18 +08001127 */
Gao Xianga4b1fab2019-10-08 20:56:15 +08001128 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1129 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001130
Gao Xianga4b1fab2019-10-08 20:56:15 +08001131 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
Gao Xiang7146a4f2018-12-08 00:19:18 +08001132}
1133
Gao Xiang97e86a82019-07-31 23:57:47 +08001134static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1135 z_erofs_next_pcluster_t qtail[],
1136 z_erofs_next_pcluster_t owned_head)
Gao Xiang7146a4f2018-12-08 00:19:18 +08001137{
Gao Xiang97e86a82019-07-31 23:57:47 +08001138 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1139 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001140
Gao Xiang97e86a82019-07-31 23:57:47 +08001141 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1142 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1143 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001144
Gao Xiang97e86a82019-07-31 23:57:47 +08001145 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001146
1147 WRITE_ONCE(*submit_qtail, owned_head);
Gao Xiang97e86a82019-07-31 23:57:47 +08001148 WRITE_ONCE(*bypass_qtail, &pcl->next);
Gao Xiang7146a4f2018-12-08 00:19:18 +08001149
Gao Xiang97e86a82019-07-31 23:57:47 +08001150 qtail[JQ_BYPASS] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001151}
1152
Gao Xiang1e4a2952020-01-21 14:48:19 +08001153static void z_erofs_submit_queue(struct super_block *sb,
Gao Xiang0c638f72019-11-08 11:37:33 +08001154 z_erofs_next_pcluster_t owned_head,
1155 struct list_head *pagepool,
1156 struct z_erofs_decompressqueue *fgq,
1157 bool *force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001158{
Gao Xiangbda17a42019-10-08 20:56:13 +08001159 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +08001160 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
Gao Xianga4b1fab2019-10-08 20:56:15 +08001161 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
Gao Xiang7146a4f2018-12-08 00:19:18 +08001162 void *bi_private;
Gao Xiang3883a792018-07-26 20:22:06 +08001163 /* since bio will be NULL, no need to initialize last_index */
1164 pgoff_t uninitialized_var(last_index);
Gao Xiang1e4a2952020-01-21 14:48:19 +08001165 unsigned int nr_bios = 0;
1166 struct bio *bio = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001167
Gao Xianga4b1fab2019-10-08 20:56:15 +08001168 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1169 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1170 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
Gao Xiang3883a792018-07-26 20:22:06 +08001171
1172 /* by default, all need io submission */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001173 q[JQ_SUBMIT]->head = owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001174
1175 do {
Gao Xiang97e86a82019-07-31 23:57:47 +08001176 struct z_erofs_pcluster *pcl;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001177 pgoff_t cur, end;
1178 unsigned int i = 0;
1179 bool bypass = true;
Gao Xiang3883a792018-07-26 20:22:06 +08001180
1181 /* no possible 'owned_head' equals the following */
Gao Xiang97e86a82019-07-31 23:57:47 +08001182 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1183 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001184
Gao Xiang97e86a82019-07-31 23:57:47 +08001185 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1186
Gao Xiang1e4a2952020-01-21 14:48:19 +08001187 cur = pcl->obj.index;
1188 end = cur + BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +08001189
1190 /* close the main owned chain at first */
Gao Xiang97e86a82019-07-31 23:57:47 +08001191 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1192 Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +08001193
Gao Xiang1e4a2952020-01-21 14:48:19 +08001194 do {
1195 struct page *page;
1196 int err;
Gao Xiang9248fce2018-12-08 00:19:15 +08001197
Gao Xiang1e4a2952020-01-21 14:48:19 +08001198 page = pickup_page_for_submission(pcl, i++, pagepool,
1199 MNGD_MAPPING(sbi),
1200 GFP_NOFS);
1201 if (!page)
1202 continue;
Gao Xiang3883a792018-07-26 20:22:06 +08001203
Gao Xiang1e4a2952020-01-21 14:48:19 +08001204 if (bio && cur != last_index + 1) {
Gao Xiang3883a792018-07-26 20:22:06 +08001205submit_bio_retry:
Gao Xiang1e4a2952020-01-21 14:48:19 +08001206 submit_bio(bio);
1207 bio = NULL;
1208 }
Gao Xiang3883a792018-07-26 20:22:06 +08001209
Gao Xiang1e4a2952020-01-21 14:48:19 +08001210 if (!bio) {
1211 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
Gao Xianga5c0b782019-09-04 10:09:02 +08001212
Gao Xiang1e4a2952020-01-21 14:48:19 +08001213 bio->bi_end_io = z_erofs_decompressqueue_endio;
1214 bio_set_dev(bio, sb->s_bdev);
1215 bio->bi_iter.bi_sector = (sector_t)cur <<
1216 LOG_SECTORS_PER_BLOCK;
1217 bio->bi_private = bi_private;
1218 bio->bi_opf = REQ_OP_READ;
1219 ++nr_bios;
1220 }
Gao Xiang94e4e152019-09-04 10:09:04 +08001221
Gao Xiang1e4a2952020-01-21 14:48:19 +08001222 err = bio_add_page(bio, page, PAGE_SIZE, 0);
1223 if (err < PAGE_SIZE)
1224 goto submit_bio_retry;
Gao Xiang3883a792018-07-26 20:22:06 +08001225
Gao Xiang1e4a2952020-01-21 14:48:19 +08001226 last_index = cur;
1227 bypass = false;
1228 } while (++cur < end);
Gao Xiang3883a792018-07-26 20:22:06 +08001229
Gao Xiang1e4a2952020-01-21 14:48:19 +08001230 if (!bypass)
Gao Xiang97e86a82019-07-31 23:57:47 +08001231 qtail[JQ_SUBMIT] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001232 else
Gao Xiang97e86a82019-07-31 23:57:47 +08001233 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1234 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001235
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001236 if (bio)
Gao Xiang94e4e152019-09-04 10:09:04 +08001237 submit_bio(bio);
Gao Xiang3883a792018-07-26 20:22:06 +08001238
Gao Xiang587a67b2020-01-21 14:47:47 +08001239 /*
1240 * although background is preferred, no one is pending for submission.
1241 * don't issue workqueue for decompression but drop it directly instead.
1242 */
1243 if (!*force_fg && !nr_bios) {
1244 kvfree(q[JQ_SUBMIT]);
Gao Xiang1e4a2952020-01-21 14:48:19 +08001245 return;
Gao Xiang587a67b2020-01-21 14:47:47 +08001246 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001247 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
Gao Xiang3883a792018-07-26 20:22:06 +08001248}
1249
Gao Xiang0c638f72019-11-08 11:37:33 +08001250static void z_erofs_runqueue(struct super_block *sb,
1251 struct z_erofs_collector *clt,
1252 struct list_head *pagepool, bool force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001253{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001254 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001255
Gao Xiang1e4a2952020-01-21 14:48:19 +08001256 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +08001257 return;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001258 z_erofs_submit_queue(sb, clt->owned_head, pagepool, io, &force_fg);
Gao Xiang3883a792018-07-26 20:22:06 +08001259
Gao Xiang0c638f72019-11-08 11:37:33 +08001260 /* handle bypass queue (no i/o pclusters) immediately */
1261 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
Gao Xiang4279f3f2019-07-31 23:57:49 +08001262
Gao Xiang3883a792018-07-26 20:22:06 +08001263 if (!force_fg)
1264 return;
1265
1266 /* wait until all bios are completed */
Gao Xianga93f8c32019-10-08 20:56:16 +08001267 io_wait_event(io[JQ_SUBMIT].u.wait,
1268 !atomic_read(&io[JQ_SUBMIT].pending_bios));
Gao Xiang3883a792018-07-26 20:22:06 +08001269
Gao Xiang0c638f72019-11-08 11:37:33 +08001270 /* handle synchronous decompress queue in the caller context */
1271 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001272}
1273
Gao Xiang0c638f72019-11-08 11:37:33 +08001274static int z_erofs_readpage(struct file *file, struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +08001275{
1276 struct inode *const inode = page->mapping->host;
Gao Xiang97e86a82019-07-31 23:57:47 +08001277 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001278 int err;
1279 LIST_HEAD(pagepool);
1280
Gao Xiangba9ce772018-11-23 01:15:58 +08001281 trace_erofs_readpage(page, false);
1282
Gao Xiangf0c519f2018-11-23 01:21:49 +08001283 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1284
Gao Xiang3883a792018-07-26 20:22:06 +08001285 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xiang97e86a82019-07-31 23:57:47 +08001286 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001287
Gao Xiangee451972019-08-19 18:34:21 +08001288 /* if some compressed cluster ready, need submit them anyway */
Gao Xiang0c638f72019-11-08 11:37:33 +08001289 z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, true);
Gao Xiangee451972019-08-19 18:34:21 +08001290
1291 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001292 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
Gao Xiangee451972019-08-19 18:34:21 +08001293
Chao Yu3b423412019-01-15 09:42:21 +08001294 if (f.map.mpage)
1295 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001296
1297 /* clean up the remaining free pages */
1298 put_pages_list(&pagepool);
Gao Xiangee451972019-08-19 18:34:21 +08001299 return err;
Gao Xiang3883a792018-07-26 20:22:06 +08001300}
1301
Gao Xiang14f362b2019-07-31 23:57:36 +08001302static bool should_decompress_synchronously(struct erofs_sb_info *sbi,
1303 unsigned int nr)
1304{
1305 return nr <= sbi->max_sync_decompress_pages;
1306}
1307
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001308static void z_erofs_readahead(struct readahead_control *rac)
Gao Xiang3883a792018-07-26 20:22:06 +08001309{
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001310 struct inode *const inode = rac->mapping->host;
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001311 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001312
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001313 bool sync = should_decompress_synchronously(sbi, readahead_count(rac));
Gao Xiang97e86a82019-07-31 23:57:47 +08001314 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001315 struct page *page, *head = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001316 LIST_HEAD(pagepool);
1317
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001318 trace_erofs_readpages(inode, readahead_index(rac),
1319 readahead_count(rac), false);
Chen Gong284db122018-09-18 22:27:27 +08001320
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001321 f.headoffset = readahead_pos(rac);
Gao Xiangf0c519f2018-11-23 01:21:49 +08001322
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001323 while ((page = readahead_page(rac))) {
Gao Xiang3883a792018-07-26 20:22:06 +08001324 prefetchw(&page->flags);
Gao Xiang3883a792018-07-26 20:22:06 +08001325
Gao Xiang2d9b5dc2018-11-23 01:21:48 +08001326 /*
1327 * A pure asynchronous readahead is indicated if
1328 * a PG_readahead marked page is hitted at first.
1329 * Let's also do asynchronous decompression for this case.
1330 */
1331 sync &= !(PageReadahead(page) && !head);
1332
Gao Xiang3883a792018-07-26 20:22:06 +08001333 set_page_private(page, (unsigned long)head);
1334 head = page;
1335 }
1336
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001337 while (head) {
Gao Xiang3883a792018-07-26 20:22:06 +08001338 struct page *page = head;
1339 int err;
1340
1341 /* traversal in reverse order */
1342 head = (void *)page_private(page);
1343
1344 err = z_erofs_do_read_page(&f, page, &pagepool);
Gao Xianga5876e22019-09-04 10:08:56 +08001345 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001346 erofs_err(inode->i_sb,
1347 "readahead error at page %lu @ nid %llu",
1348 page->index, EROFS_I(inode)->nid);
Gao Xiang3883a792018-07-26 20:22:06 +08001349 put_page(page);
1350 }
1351
Gao Xiang97e86a82019-07-31 23:57:47 +08001352 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001353
Gao Xiang0c638f72019-11-08 11:37:33 +08001354 z_erofs_runqueue(inode->i_sb, &f.clt, &pagepool, sync);
Gao Xiang3883a792018-07-26 20:22:06 +08001355
Chao Yu3b423412019-01-15 09:42:21 +08001356 if (f.map.mpage)
1357 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001358
1359 /* clean up the remaining free pages */
1360 put_pages_list(&pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001361}
1362
Gao Xiang0c638f72019-11-08 11:37:33 +08001363const struct address_space_operations z_erofs_aops = {
1364 .readpage = z_erofs_readpage,
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001365 .readahead = z_erofs_readahead,
Gao Xiang3883a792018-07-26 20:22:06 +08001366};
Gao Xiang02827e12018-07-26 20:21:58 +08001367