blob: 8cb2cf612e49b816b10f9979d1952b5a868fd8e7 [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.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang02827e12018-07-26 20:21:58 +08005 * 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 Xiang3883a792018-07-26 20:22:06 +080046
47 /*
Gao Xiang97e86a82019-07-31 23:57:47 +080048 * no need to spawn too many threads, limiting threads could minimum
49 * scheduling overhead, perhaps per-CPU threads should be better?
Gao Xiang3883a792018-07-26 20:22:06 +080050 */
Gao Xiang0e62ea32020-07-31 10:40:49 +080051 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
52 WQ_UNBOUND | WQ_HIGHPRI,
Gao Xiang97e86a82019-07-31 23:57:47 +080053 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
Gao Xiang6ea5aad2020-09-19 15:27:30 +0800138 bool readahead;
Gao Xiang97e86a82019-07-31 23:57:47 +0800139 /* used for applying cache strategy on the fly */
140 bool backmost;
141 erofs_off_t headoffset;
142};
143
144#define COLLECTOR_INIT() { \
145 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
146 .mode = COLLECT_PRIMARY_FOLLOWED }
147
148#define DECOMPRESS_FRONTEND_INIT(__i) { \
149 .inode = __i, .clt = COLLECTOR_INIT(), \
150 .backmost = true, }
151
152static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
153static DEFINE_MUTEX(z_pagemap_global_lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800154
Gao Xiang97e86a82019-07-31 23:57:47 +0800155static void preload_compressed_pages(struct z_erofs_collector *clt,
Gao Xiang92e6efd2018-12-08 00:19:16 +0800156 struct address_space *mc,
Chao Yue3f78d52020-09-17 09:18:21 +0800157 enum z_erofs_cache_alloctype type)
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,
Gao Xiangbe3f6032021-11-16 09:08:19 +0800281 struct page *page, enum z_erofs_page_type type,
282 bool pvec_safereuse)
Gao Xiang3883a792018-07-26 20:22:06 +0800283{
284 int ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800285
Gao Xiang97e86a82019-07-31 23:57:47 +0800286 /* give priority for inplaceio */
287 if (clt->mode >= COLLECT_PRIMARY &&
Julian Merida447a3622019-03-18 20:58:41 -0300288 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
Gao Xiang99634bf2019-09-04 10:09:05 +0800289 z_erofs_try_inplace_io(clt, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800290 return 0;
291
Gao Xiangbe3f6032021-11-16 09:08:19 +0800292 ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
293 pvec_safereuse);
Gao Xiang97e86a82019-07-31 23:57:47 +0800294 clt->cl->vcnt += (unsigned int)ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800295 return ret ? 0 : -EAGAIN;
296}
297
Gao Xiang97e86a82019-07-31 23:57:47 +0800298static enum z_erofs_collectmode
299try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
300 z_erofs_next_pcluster_t *owned_head)
Gao Xiang3883a792018-07-26 20:22:06 +0800301{
Gao Xiang97e86a82019-07-31 23:57:47 +0800302 /* let's claim these following types of pclusters */
Gao Xiang3883a792018-07-26 20:22:06 +0800303retry:
Gao Xiang97e86a82019-07-31 23:57:47 +0800304 if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
305 /* type 1, nil pcluster */
306 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
307 *owned_head) != Z_EROFS_PCLUSTER_NIL)
Gao Xiang3883a792018-07-26 20:22:06 +0800308 goto retry;
309
Gao Xiang97e86a82019-07-31 23:57:47 +0800310 *owned_head = &pcl->next;
Gao Xianga1121522019-02-27 13:33:32 +0800311 /* lucky, I am the followee :) */
Gao Xiang97e86a82019-07-31 23:57:47 +0800312 return COLLECT_PRIMARY_FOLLOWED;
313 } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
Gao Xiang3883a792018-07-26 20:22:06 +0800314 /*
315 * type 2, link to the end of a existing open chain,
316 * be careful that its submission itself is governed
317 * by the original owned chain.
318 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800319 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
320 *owned_head) != Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +0800321 goto retry;
Gao Xiang97e86a82019-07-31 23:57:47 +0800322 *owned_head = Z_EROFS_PCLUSTER_TAIL;
323 return COLLECT_PRIMARY_HOOKED;
Gao Xianga1121522019-02-27 13:33:32 +0800324 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800325 return COLLECT_PRIMARY; /* :( better luck next time */
Gao Xiang3883a792018-07-26 20:22:06 +0800326}
327
Gao Xiang9e579fc2019-10-08 20:56:12 +0800328static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
329 struct inode *inode,
330 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800331{
Gao Xiang64094a02020-02-20 10:46:42 +0800332 struct z_erofs_pcluster *pcl = clt->pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800333 struct z_erofs_collection *cl;
334 unsigned int length;
Gao Xiang3883a792018-07-26 20:22:06 +0800335
Gao Xiang64094a02020-02-20 10:46:42 +0800336 /* to avoid unexpected loop formed by corrupted images */
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800337 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
338 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800339 return -EFSCORRUPTED;
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800340 }
Gao Xiang97e86a82019-07-31 23:57:47 +0800341
342 cl = z_erofs_primarycollection(pcl);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800343 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800344 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800345 return -EFSCORRUPTED;
Gao Xiang3883a792018-07-26 20:22:06 +0800346 }
347
Gao Xiang97e86a82019-07-31 23:57:47 +0800348 length = READ_ONCE(pcl->length);
349 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
350 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
351 DBG_BUGON(1);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800352 return -EFSCORRUPTED;
Gao Xiang97e86a82019-07-31 23:57:47 +0800353 }
354 } else {
355 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
Gao Xiang3883a792018-07-26 20:22:06 +0800356
Gao Xiang97e86a82019-07-31 23:57:47 +0800357 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
358 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
Gao Xiang3883a792018-07-26 20:22:06 +0800359
Gao Xiang97e86a82019-07-31 23:57:47 +0800360 while (llen > length &&
361 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
362 cpu_relax();
363 length = READ_ONCE(pcl->length);
364 }
365 }
366 mutex_lock(&cl->lock);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800367 /* used to check tail merging loop due to corrupted images */
368 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
369 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800370 clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800371 /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
372 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
373 clt->tailpcl = NULL;
Gao Xiang97e86a82019-07-31 23:57:47 +0800374 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800375 return 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800376}
377
Gao Xiang9e579fc2019-10-08 20:56:12 +0800378static int z_erofs_register_collection(struct z_erofs_collector *clt,
379 struct inode *inode,
380 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800381{
Gao Xiang97e86a82019-07-31 23:57:47 +0800382 struct z_erofs_pcluster *pcl;
383 struct z_erofs_collection *cl;
Gao Xiang64094a02020-02-20 10:46:42 +0800384 struct erofs_workgroup *grp;
Gao Xiang97e86a82019-07-31 23:57:47 +0800385 int err;
Gao Xiange5e3abb2018-09-19 13:49:07 +0800386
Gao Xiang3883a792018-07-26 20:22:06 +0800387 /* no available workgroup, let's allocate one */
Gao Xiang97e86a82019-07-31 23:57:47 +0800388 pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800389 if (!pcl)
Gao Xiang9e579fc2019-10-08 20:56:12 +0800390 return -ENOMEM;
Gao Xiang3883a792018-07-26 20:22:06 +0800391
Gao Xiang64094a02020-02-20 10:46:42 +0800392 atomic_set(&pcl->obj.refcount, 1);
Gao Xiang97e86a82019-07-31 23:57:47 +0800393 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
Gao Xiang3883a792018-07-26 20:22:06 +0800394
Gao Xiang97e86a82019-07-31 23:57:47 +0800395 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
396 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
397 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800398
Gao Xiang97e86a82019-07-31 23:57:47 +0800399 if (map->m_flags & EROFS_MAP_ZIPPED)
400 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
401 else
402 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
Gao Xiangb6a76182019-06-24 15:22:58 +0800403
Gao Xianga5876e22019-09-04 10:08:56 +0800404 pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
Gao Xiang97e86a82019-07-31 23:57:47 +0800405 pcl->clusterbits -= PAGE_SHIFT;
Gao Xiang3883a792018-07-26 20:22:06 +0800406
Gao Xiang97e86a82019-07-31 23:57:47 +0800407 /* new pclusters should be claimed as type 1, primary and followed */
408 pcl->next = clt->owned_head;
409 clt->mode = COLLECT_PRIMARY_FOLLOWED;
410
411 cl = z_erofs_primarycollection(pcl);
Gao Xiang64094a02020-02-20 10:46:42 +0800412
413 /* must be cleaned before freeing to slab */
414 DBG_BUGON(cl->nr_pages);
415 DBG_BUGON(cl->vcnt);
416
Gao Xiang97e86a82019-07-31 23:57:47 +0800417 cl->pageofs = map->m_la & ~PAGE_MASK;
Gao Xiang3883a792018-07-26 20:22:06 +0800418
Gao Xiang23edf3a2018-11-23 01:21:47 +0800419 /*
420 * lock all primary followed works before visible to others
Gao Xiang97e86a82019-07-31 23:57:47 +0800421 * and mutex_trylock *never* fails for a new pcluster.
Gao Xiang23edf3a2018-11-23 01:21:47 +0800422 */
Gao Xiang64094a02020-02-20 10:46:42 +0800423 DBG_BUGON(!mutex_trylock(&cl->lock));
Gao Xiang23edf3a2018-11-23 01:21:47 +0800424
Gao Xiang64094a02020-02-20 10:46:42 +0800425 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
426 if (IS_ERR(grp)) {
427 err = PTR_ERR(grp);
428 goto err_out;
429 }
430
431 if (grp != &pcl->obj) {
432 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
433 err = -EEXIST;
434 goto err_out;
Gao Xiang3883a792018-07-26 20:22:06 +0800435 }
Gao Xiangbfc4ccb2019-08-21 11:09:08 +0800436 /* used to check tail merging loop due to corrupted images */
437 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
438 clt->tailpcl = pcl;
Gao Xiang97e86a82019-07-31 23:57:47 +0800439 clt->owned_head = &pcl->next;
440 clt->pcl = pcl;
441 clt->cl = cl;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800442 return 0;
Gao Xiang64094a02020-02-20 10:46:42 +0800443
444err_out:
445 mutex_unlock(&cl->lock);
446 kmem_cache_free(pcluster_cachep, pcl);
447 return err;
Gao Xiang3883a792018-07-26 20:22:06 +0800448}
449
Gao Xiang97e86a82019-07-31 23:57:47 +0800450static int z_erofs_collector_begin(struct z_erofs_collector *clt,
451 struct inode *inode,
452 struct erofs_map_blocks *map)
Gao Xiang3883a792018-07-26 20:22:06 +0800453{
Gao Xiang64094a02020-02-20 10:46:42 +0800454 struct erofs_workgroup *grp;
Gao Xiang9e579fc2019-10-08 20:56:12 +0800455 int ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800456
Gao Xiang97e86a82019-07-31 23:57:47 +0800457 DBG_BUGON(clt->cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800458
Gao Xiang97e86a82019-07-31 23:57:47 +0800459 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
460 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
461 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +0800462
Gao Xiang97e86a82019-07-31 23:57:47 +0800463 if (!PAGE_ALIGNED(map->m_pa)) {
464 DBG_BUGON(1);
465 return -EINVAL;
466 }
Gao Xiang3883a792018-07-26 20:22:06 +0800467
Gao Xiang64094a02020-02-20 10:46:42 +0800468 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
469 if (grp) {
470 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
471 } else {
Gao Xiang9e579fc2019-10-08 20:56:12 +0800472 ret = z_erofs_register_collection(clt, inode, map);
Gao Xiangb27661c2018-09-19 13:49:06 +0800473
Gao Xiang64094a02020-02-20 10:46:42 +0800474 if (!ret)
475 goto out;
476 if (ret != -EEXIST)
477 return ret;
Gao Xiang3883a792018-07-26 20:22:06 +0800478 }
479
Gao Xiang64094a02020-02-20 10:46:42 +0800480 ret = z_erofs_lookup_collection(clt, inode, map);
481 if (ret) {
482 erofs_workgroup_put(&clt->pcl->obj);
Gao Xiang9e579fc2019-10-08 20:56:12 +0800483 return ret;
Gao Xiang64094a02020-02-20 10:46:42 +0800484 }
Gao Xiang3883a792018-07-26 20:22:06 +0800485
Gao Xiang64094a02020-02-20 10:46:42 +0800486out:
Gao Xiang97e86a82019-07-31 23:57:47 +0800487 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang9e579fc2019-10-08 20:56:12 +0800488 clt->cl->pagevec, clt->cl->vcnt);
Gao Xiang3883a792018-07-26 20:22:06 +0800489
Gao Xiang97e86a82019-07-31 23:57:47 +0800490 clt->compressedpages = clt->pcl->compressed_pages;
491 if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
492 clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
Gao Xiang3883a792018-07-26 20:22:06 +0800493 return 0;
494}
495
496/*
Gao Xiang97e86a82019-07-31 23:57:47 +0800497 * keep in mind that no referenced pclusters will be freed
498 * only after a RCU grace period.
Gao Xiang3883a792018-07-26 20:22:06 +0800499 */
500static void z_erofs_rcu_callback(struct rcu_head *head)
501{
Gao Xiang97e86a82019-07-31 23:57:47 +0800502 struct z_erofs_collection *const cl =
503 container_of(head, struct z_erofs_collection, rcu);
Gao Xiang3883a792018-07-26 20:22:06 +0800504
Gao Xiang97e86a82019-07-31 23:57:47 +0800505 kmem_cache_free(pcluster_cachep,
506 container_of(cl, struct z_erofs_pcluster,
507 primary_collection));
Gao Xiang3883a792018-07-26 20:22:06 +0800508}
509
510void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
511{
Gao Xiang97e86a82019-07-31 23:57:47 +0800512 struct z_erofs_pcluster *const pcl =
513 container_of(grp, struct z_erofs_pcluster, obj);
514 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
Gao Xiang3883a792018-07-26 20:22:06 +0800515
Gao Xiang97e86a82019-07-31 23:57:47 +0800516 call_rcu(&cl->rcu, z_erofs_rcu_callback);
Gao Xiang3883a792018-07-26 20:22:06 +0800517}
518
Gao Xiang97e86a82019-07-31 23:57:47 +0800519static void z_erofs_collection_put(struct z_erofs_collection *cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800520{
Gao Xiang97e86a82019-07-31 23:57:47 +0800521 struct z_erofs_pcluster *const pcl =
522 container_of(cl, struct z_erofs_pcluster, primary_collection);
523
524 erofs_workgroup_put(&pcl->obj);
Gao Xiang3883a792018-07-26 20:22:06 +0800525}
526
Gao Xiang97e86a82019-07-31 23:57:47 +0800527static bool z_erofs_collector_end(struct z_erofs_collector *clt)
Gao Xiang3883a792018-07-26 20:22:06 +0800528{
Gao Xiang97e86a82019-07-31 23:57:47 +0800529 struct z_erofs_collection *cl = clt->cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800530
Gao Xiang97e86a82019-07-31 23:57:47 +0800531 if (!cl)
Gao Xiang3883a792018-07-26 20:22:06 +0800532 return false;
533
Gao Xiang97e86a82019-07-31 23:57:47 +0800534 z_erofs_pagevec_ctor_exit(&clt->vector, false);
535 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800536
537 /*
Gao Xiang97e86a82019-07-31 23:57:47 +0800538 * if all pending pages are added, don't hold its reference
539 * any longer if the pcluster isn't hosted by ourselves.
Gao Xiang3883a792018-07-26 20:22:06 +0800540 */
Gao Xiang97e86a82019-07-31 23:57:47 +0800541 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
542 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800543
Gao Xiang97e86a82019-07-31 23:57:47 +0800544 clt->cl = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +0800545 return true;
546}
547
Gao Xiang97e86a82019-07-31 23:57:47 +0800548static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800549 unsigned int cachestrategy,
Gao Xiang97e86a82019-07-31 23:57:47 +0800550 erofs_off_t la)
Gao Xiang92e6efd2018-12-08 00:19:16 +0800551{
Gao Xiang4279f3f2019-07-31 23:57:49 +0800552 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
553 return false;
554
Gao Xiang92e6efd2018-12-08 00:19:16 +0800555 if (fe->backmost)
556 return true;
557
Gao Xiang4279f3f2019-07-31 23:57:49 +0800558 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
559 la < fe->headoffset;
Gao Xiang92e6efd2018-12-08 00:19:16 +0800560}
Gao Xiang92e6efd2018-12-08 00:19:16 +0800561
Gao Xiang97e86a82019-07-31 23:57:47 +0800562static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
Chao Yue3f78d52020-09-17 09:18:21 +0800563 struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +0800564{
Gao Xiang97e86a82019-07-31 23:57:47 +0800565 struct inode *const inode = fe->inode;
Gao Xiangbda17a42019-10-08 20:56:13 +0800566 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Chao Yu3b423412019-01-15 09:42:21 +0800567 struct erofs_map_blocks *const map = &fe->map;
Gao Xiang97e86a82019-07-31 23:57:47 +0800568 struct z_erofs_collector *const clt = &fe->clt;
Gao Xiang3883a792018-07-26 20:22:06 +0800569 const loff_t offset = page_offset(page);
Gao Xiangdc76ea82019-09-22 18:04:34 +0800570 bool tight = true;
Gao Xiang3883a792018-07-26 20:22:06 +0800571
Gao Xiang92e6efd2018-12-08 00:19:16 +0800572 enum z_erofs_cache_alloctype cache_strategy;
Gao Xiang3883a792018-07-26 20:22:06 +0800573 enum z_erofs_page_type page_type;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200574 unsigned int cur, end, spiltted, index;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800575 int err = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800576
577 /* register locked file pages as online pages in pack */
578 z_erofs_onlinepage_init(page);
579
580 spiltted = 0;
581 end = PAGE_SIZE;
582repeat:
583 cur = end - 1;
584
585 /* lucky, within the range of the current map_blocks */
586 if (offset + cur >= map->m_la &&
Julian Merida447a3622019-03-18 20:58:41 -0300587 offset + cur < map->m_la + map->m_llen) {
Gao Xiang97e86a82019-07-31 23:57:47 +0800588 /* didn't get a valid collection previously (very rare) */
589 if (!clt->cl)
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800590 goto restart_now;
Gao Xiang3883a792018-07-26 20:22:06 +0800591 goto hitted;
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800592 }
Gao Xiang3883a792018-07-26 20:22:06 +0800593
594 /* go ahead the next map_blocks */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800595 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
Gao Xiang3883a792018-07-26 20:22:06 +0800596
Gao Xiang97e86a82019-07-31 23:57:47 +0800597 if (z_erofs_collector_end(clt))
Gao Xiangf0c519f2018-11-23 01:21:49 +0800598 fe->backmost = false;
Gao Xiang3883a792018-07-26 20:22:06 +0800599
600 map->m_la = offset + cur;
601 map->m_llen = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +0800602 err = z_erofs_map_blocks_iter(inode, map, 0);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800603 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800604 goto err_out;
605
Gao Xiang1e5ceea2019-02-27 13:33:31 +0800606restart_now:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800607 if (!(map->m_flags & EROFS_MAP_MAPPED))
Gao Xiang3883a792018-07-26 20:22:06 +0800608 goto hitted;
609
Gao Xiang97e86a82019-07-31 23:57:47 +0800610 err = z_erofs_collector_begin(clt, inode, map);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800611 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800612 goto err_out;
613
Gao Xiang92e6efd2018-12-08 00:19:16 +0800614 /* preload all compressed pages (maybe downgrade role if necessary) */
Chao Yuf57a3fe2020-05-29 18:48:36 +0800615 if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
Gao Xiang92e6efd2018-12-08 00:19:16 +0800616 cache_strategy = DELAYEDALLOC;
617 else
618 cache_strategy = DONTALLOC;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800619
Chao Yue3f78d52020-09-17 09:18:21 +0800620 preload_compressed_pages(clt, MNGD_MAPPING(sbi), cache_strategy);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800621
Gao Xiang3883a792018-07-26 20:22:06 +0800622hitted:
Gao Xiangdc76ea82019-09-22 18:04:34 +0800623 /*
624 * Ensure the current partial page belongs to this submit chain rather
625 * than other concurrent submit chains or the noio(bypass) chain since
626 * those chains are handled asynchronously thus the page cannot be used
627 * for inplace I/O or pagevec (should be processed in strict order.)
628 */
629 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
630 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
631
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 Xiangbe3f6032021-11-16 09:08:19 +0800648 err = z_erofs_attach_page(clt, page, page_type,
649 clt->mode >= COLLECT_PRIMARY_FOLLOWED);
Gao Xiang3883a792018-07-26 20:22:06 +0800650 /* should allocate an additional staging page for pagevec */
651 if (err == -EAGAIN) {
652 struct page *const newpage =
Chao Yue3f78d52020-09-17 09:18:21 +0800653 alloc_page(GFP_NOFS | __GFP_NOFAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800654
Gao Xiang5ddcee12019-11-21 21:59:54 +0800655 newpage->mapping = Z_EROFS_MAPPING_STAGING;
Gao Xiang97e86a82019-07-31 23:57:47 +0800656 err = z_erofs_attach_page(clt, newpage,
Gao Xiangbe3f6032021-11-16 09:08:19 +0800657 Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800658 if (!err)
Gao Xiang3883a792018-07-26 20:22:06 +0800659 goto retry;
660 }
661
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800662 if (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800663 goto err_out;
664
Gao Xiang97e86a82019-07-31 23:57:47 +0800665 index = page->index - (map->m_la >> PAGE_SHIFT);
Gao Xiang3883a792018-07-26 20:22:06 +0800666
Gao Xiang3883a792018-07-26 20:22:06 +0800667 z_erofs_onlinepage_fixup(page, index, true);
Gao Xiang3883a792018-07-26 20:22:06 +0800668
Gao Xiang1e05ff32018-09-18 22:27:25 +0800669 /* bump up the number of spiltted parts of a page */
670 ++spiltted;
671 /* also update nr_pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800672 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
Gao Xiang3883a792018-07-26 20:22:06 +0800673next_part:
674 /* can be used for verification */
675 map->m_llen = offset + cur - map->m_la;
676
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300677 end = cur;
678 if (end > 0)
Gao Xiang3883a792018-07-26 20:22:06 +0800679 goto repeat;
680
Gao Xiang1e05ff32018-09-18 22:27:25 +0800681out:
Gao Xiang3883a792018-07-26 20:22:06 +0800682 z_erofs_onlinepage_endio(page);
683
Gao Xiang4f761fa2019-09-04 10:09:09 +0800684 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
685 __func__, page, spiltted, map->m_llen);
Gao Xiang3883a792018-07-26 20:22:06 +0800686 return err;
Gao Xiang1e05ff32018-09-18 22:27:25 +0800687
688 /* if some error occurred while processing this page */
689err_out:
690 SetPageError(page);
691 goto out;
Gao Xiang3883a792018-07-26 20:22:06 +0800692}
693
Gao Xianga4b1fab2019-10-08 20:56:15 +0800694static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
695 bool sync, int bios)
Gao Xiang3883a792018-07-26 20:22:06 +0800696{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800697 /* wake up the caller thread for sync decompression */
698 if (sync) {
Gao Xiang848bd9a2018-12-08 00:19:12 +0800699 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
Gao Xiang0c638f72019-11-08 11:37:33 +0800712static void z_erofs_decompressqueue_endio(struct bio *bio)
Gao Xiang3883a792018-07-26 20:22:06 +0800713{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800714 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
715 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
Gao Xiang14a56ec2019-03-25 11:40:09 +0800716 blk_status_t err = bio->bi_status;
Gao Xiang3883a792018-07-26 20:22:06 +0800717 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800718 struct bvec_iter_all iter_all;
Gao Xiang3883a792018-07-26 20:22:06 +0800719
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200720 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang3883a792018-07-26 20:22:06 +0800721 struct page *page = bvec->bv_page;
722
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 (err)
Gao Xiang3883a792018-07-26 20:22:06 +0800727 SetPageError(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800728
Gao Xianga4b1fab2019-10-08 20:56:15 +0800729 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
730 if (!err)
731 SetPageUptodate(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800732 unlock_page(page);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800733 }
Gao Xiang3883a792018-07-26 20:22:06 +0800734 }
Gao Xianga4b1fab2019-10-08 20:56:15 +0800735 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
Gao Xiang3883a792018-07-26 20:22:06 +0800736 bio_put(bio);
737}
738
Gao Xiang97e86a82019-07-31 23:57:47 +0800739static int z_erofs_decompress_pcluster(struct super_block *sb,
740 struct z_erofs_pcluster *pcl,
741 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800742{
743 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Gao Xiang97e86a82019-07-31 23:57:47 +0800744 const unsigned int clusterpages = BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +0800745 struct z_erofs_pagevec_ctor ctor;
Gao Xiang97e86a82019-07-31 23:57:47 +0800746 unsigned int i, outputsize, llen, nr_pages;
747 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
Gao Xiang3883a792018-07-26 20:22:06 +0800748 struct page **pages, **compressed_pages, *page;
Gao Xiang3883a792018-07-26 20:22:06 +0800749
750 enum z_erofs_page_type page_type;
Gao Xiangb6a76182019-06-24 15:22:58 +0800751 bool overlapped, partial;
Gao Xiang97e86a82019-07-31 23:57:47 +0800752 struct z_erofs_collection *cl;
Gao Xiang3883a792018-07-26 20:22:06 +0800753 int err;
754
755 might_sleep();
Gao Xiang97e86a82019-07-31 23:57:47 +0800756 cl = z_erofs_primarycollection(pcl);
757 DBG_BUGON(!READ_ONCE(cl->nr_pages));
Gao Xiang3883a792018-07-26 20:22:06 +0800758
Gao Xiang97e86a82019-07-31 23:57:47 +0800759 mutex_lock(&cl->lock);
760 nr_pages = cl->nr_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800761
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800762 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
Gao Xiang3883a792018-07-26 20:22:06 +0800763 pages = pages_onstack;
Gao Xiang97e86a82019-07-31 23:57:47 +0800764 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
765 mutex_trylock(&z_pagemap_global_lock)) {
Gao Xiang3883a792018-07-26 20:22:06 +0800766 pages = z_pagemap_global;
Gao Xiang97e86a82019-07-31 23:57:47 +0800767 } else {
Chao Yu441dfcc2019-07-16 17:44:22 +0800768 gfp_t gfp_flags = GFP_KERNEL;
769
Gao Xiang97e86a82019-07-31 23:57:47 +0800770 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
Chao Yu441dfcc2019-07-16 17:44:22 +0800771 gfp_flags |= __GFP_NOFAIL;
772
Julian Merida447a3622019-03-18 20:58:41 -0300773 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Chao Yu441dfcc2019-07-16 17:44:22 +0800774 gfp_flags);
Gao Xiang3883a792018-07-26 20:22:06 +0800775
776 /* fallback to global pagemap for the lowmem scenario */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800777 if (!pages) {
Chao Yu441dfcc2019-07-16 17:44:22 +0800778 mutex_lock(&z_pagemap_global_lock);
779 pages = z_pagemap_global;
Gao Xiang3883a792018-07-26 20:22:06 +0800780 }
781 }
782
783 for (i = 0; i < nr_pages; ++i)
784 pages[i] = NULL;
785
Gao Xiange12a0ce2019-08-21 22:01:52 +0800786 err = 0;
Gao Xiangfa61a332019-06-24 15:22:53 +0800787 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
Gao Xiang97e86a82019-07-31 23:57:47 +0800788 cl->pagevec, 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800789
Gao Xiang97e86a82019-07-31 23:57:47 +0800790 for (i = 0; i < cl->vcnt; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200791 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800792
Gao Xiang046d64e2019-07-31 23:57:45 +0800793 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800794
795 /* all pages in pagevec ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100796 DBG_BUGON(!page);
797 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800798
Gao Xiang97e86a82019-07-31 23:57:47 +0800799 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiang3883a792018-07-26 20:22:06 +0800800 continue;
801
802 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
803 pagenr = 0;
804 else
805 pagenr = z_erofs_onlinepage_index(page);
806
Gao Xiang70b17992018-12-11 15:17:49 +0800807 DBG_BUGON(pagenr >= nr_pages);
Gao Xiange5e3abb2018-09-19 13:49:07 +0800808
Gao Xiange12a0ce2019-08-21 22:01:52 +0800809 /*
810 * currently EROFS doesn't support multiref(dedup),
811 * so here erroring out one multiref page.
812 */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800813 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800814 DBG_BUGON(1);
815 SetPageError(pages[pagenr]);
816 z_erofs_onlinepage_endio(pages[pagenr]);
817 err = -EFSCORRUPTED;
818 }
Gao Xiang3883a792018-07-26 20:22:06 +0800819 pages[pagenr] = page;
820 }
Gao Xiang3883a792018-07-26 20:22:06 +0800821 z_erofs_pagevec_ctor_exit(&ctor, true);
822
823 overlapped = false;
Gao Xiang97e86a82019-07-31 23:57:47 +0800824 compressed_pages = pcl->compressed_pages;
Gao Xiang3883a792018-07-26 20:22:06 +0800825
826 for (i = 0; i < clusterpages; ++i) {
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200827 unsigned int pagenr;
Gao Xiang3883a792018-07-26 20:22:06 +0800828
829 page = compressed_pages[i];
830
831 /* all compressed pages ought to be valid */
Cristian Sicilia42d40b42018-11-12 21:43:57 +0100832 DBG_BUGON(!page);
833 DBG_BUGON(!page->mapping);
Gao Xiang3883a792018-07-26 20:22:06 +0800834
Gao Xiang27481232019-06-24 15:22:54 +0800835 if (!z_erofs_page_is_staging(page)) {
Gao Xiangd61fbb62019-03-25 11:40:08 +0800836 if (erofs_page_is_managed(sbi, page)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800837 if (!PageUptodate(page))
Gao Xiang11152492019-03-25 11:40:07 +0800838 err = -EIO;
839 continue;
840 }
Gao Xiang3883a792018-07-26 20:22:06 +0800841
Gao Xiang11152492019-03-25 11:40:07 +0800842 /*
843 * only if non-head page can be selected
844 * for inplace decompression
845 */
846 pagenr = z_erofs_onlinepage_index(page);
Gao Xiang3883a792018-07-26 20:22:06 +0800847
Gao Xiang11152492019-03-25 11:40:07 +0800848 DBG_BUGON(pagenr >= nr_pages);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800849 if (pages[pagenr]) {
Gao Xiange12a0ce2019-08-21 22:01:52 +0800850 DBG_BUGON(1);
851 SetPageError(pages[pagenr]);
852 z_erofs_onlinepage_endio(pages[pagenr]);
853 err = -EFSCORRUPTED;
854 }
Gao Xiang11152492019-03-25 11:40:07 +0800855 pages[pagenr] = page;
Gao Xiang3883a792018-07-26 20:22:06 +0800856
Gao Xiang11152492019-03-25 11:40:07 +0800857 overlapped = true;
858 }
859
860 /* PG_error needs checking for inplaced and staging pages */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800861 if (PageError(page)) {
Gao Xiang11152492019-03-25 11:40:07 +0800862 DBG_BUGON(PageUptodate(page));
863 err = -EIO;
864 }
Gao Xiang3883a792018-07-26 20:22:06 +0800865 }
866
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800867 if (err)
Gao Xiang11152492019-03-25 11:40:07 +0800868 goto out;
869
Gao Xiang97e86a82019-07-31 23:57:47 +0800870 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
871 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
872 outputsize = llen;
873 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
Gao Xiangb6a76182019-06-24 15:22:58 +0800874 } else {
Gao Xiang97e86a82019-07-31 23:57:47 +0800875 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
Gao Xiangb6a76182019-06-24 15:22:58 +0800876 partial = true;
877 }
Gao Xiang3883a792018-07-26 20:22:06 +0800878
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800879 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
880 .sb = sb,
881 .in = compressed_pages,
882 .out = pages,
Gao Xiang97e86a82019-07-31 23:57:47 +0800883 .pageofs_out = cl->pageofs,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800884 .inputsize = PAGE_SIZE,
885 .outputsize = outputsize,
Gao Xiang97e86a82019-07-31 23:57:47 +0800886 .alg = pcl->algorithmformat,
Gao Xiang88aaf5a2019-06-24 15:22:57 +0800887 .inplace_io = overlapped,
Gao Xiangb6a76182019-06-24 15:22:58 +0800888 .partial_decoding = partial
Gao Xiang97e86a82019-07-31 23:57:47 +0800889 }, pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800890
891out:
Gao Xiangaf692e12019-02-27 13:33:30 +0800892 /* must handle all compressed pages before endding pages */
Gao Xiang3883a792018-07-26 20:22:06 +0800893 for (i = 0; i < clusterpages; ++i) {
894 page = compressed_pages[i];
895
Gao Xiangd61fbb62019-03-25 11:40:08 +0800896 if (erofs_page_is_managed(sbi, page))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800897 continue;
Gao Xiangd61fbb62019-03-25 11:40:08 +0800898
Gao Xiang3883a792018-07-26 20:22:06 +0800899 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800900 (void)z_erofs_put_stagingpage(pagepool, page);
Gao Xiang3883a792018-07-26 20:22:06 +0800901
902 WRITE_ONCE(compressed_pages[i], NULL);
903 }
904
Gao Xiangaf692e12019-02-27 13:33:30 +0800905 for (i = 0; i < nr_pages; ++i) {
906 page = pages[i];
907 if (!page)
908 continue;
909
910 DBG_BUGON(!page->mapping);
911
912 /* recycle all individual staging pages */
Gao Xiang97e86a82019-07-31 23:57:47 +0800913 if (z_erofs_put_stagingpage(pagepool, page))
Gao Xiangaf692e12019-02-27 13:33:30 +0800914 continue;
915
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800916 if (err < 0)
Gao Xiangaf692e12019-02-27 13:33:30 +0800917 SetPageError(page);
918
919 z_erofs_onlinepage_endio(page);
920 }
921
Gao Xiang3883a792018-07-26 20:22:06 +0800922 if (pages == z_pagemap_global)
923 mutex_unlock(&z_pagemap_global_lock);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800924 else if (pages != pages_onstack)
Gao Xiang3883a792018-07-26 20:22:06 +0800925 kvfree(pages);
926
Gao Xiang97e86a82019-07-31 23:57:47 +0800927 cl->nr_pages = 0;
928 cl->vcnt = 0;
Gao Xiang3883a792018-07-26 20:22:06 +0800929
Gao Xiang97e86a82019-07-31 23:57:47 +0800930 /* all cl locks MUST be taken before the following line */
931 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800932
Gao Xiang97e86a82019-07-31 23:57:47 +0800933 /* all cl locks SHOULD be released right now */
934 mutex_unlock(&cl->lock);
Gao Xiang3883a792018-07-26 20:22:06 +0800935
Gao Xiang97e86a82019-07-31 23:57:47 +0800936 z_erofs_collection_put(cl);
Gao Xiang3883a792018-07-26 20:22:06 +0800937 return err;
938}
939
Gao Xiang0c638f72019-11-08 11:37:33 +0800940static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
941 struct list_head *pagepool)
Gao Xiang3883a792018-07-26 20:22:06 +0800942{
Gao Xiang97e86a82019-07-31 23:57:47 +0800943 z_erofs_next_pcluster_t owned = io->head;
Gao Xiang3883a792018-07-26 20:22:06 +0800944
Gao Xiang97e86a82019-07-31 23:57:47 +0800945 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
946 struct z_erofs_pcluster *pcl;
Gao Xiang3883a792018-07-26 20:22:06 +0800947
948 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800949 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800950
951 /* no possible that 'owned' equals NULL */
Gao Xiang97e86a82019-07-31 23:57:47 +0800952 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +0800953
Gao Xiang97e86a82019-07-31 23:57:47 +0800954 pcl = container_of(owned, struct z_erofs_pcluster, next);
955 owned = READ_ONCE(pcl->next);
Gao Xiang3883a792018-07-26 20:22:06 +0800956
Gao Xianga4b1fab2019-10-08 20:56:15 +0800957 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
Gao Xiang3978c8e2018-08-06 11:27:53 +0800958 }
Gao Xiang3883a792018-07-26 20:22:06 +0800959}
960
Gao Xiang0c638f72019-11-08 11:37:33 +0800961static void z_erofs_decompressqueue_work(struct work_struct *work)
Gao Xiang3883a792018-07-26 20:22:06 +0800962{
Gao Xianga4b1fab2019-10-08 20:56:15 +0800963 struct z_erofs_decompressqueue *bgq =
964 container_of(work, struct z_erofs_decompressqueue, u.work);
Gao Xiang97e86a82019-07-31 23:57:47 +0800965 LIST_HEAD(pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800966
Gao Xianga4b1fab2019-10-08 20:56:15 +0800967 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang0c638f72019-11-08 11:37:33 +0800968 z_erofs_decompress_queue(bgq, &pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +0800969
Gao Xiang97e86a82019-07-31 23:57:47 +0800970 put_pages_list(&pagepool);
Gao Xianga4b1fab2019-10-08 20:56:15 +0800971 kvfree(bgq);
Gao Xiang3883a792018-07-26 20:22:06 +0800972}
973
Gao Xiang97e86a82019-07-31 23:57:47 +0800974static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
975 unsigned int nr,
976 struct list_head *pagepool,
977 struct address_space *mc,
978 gfp_t gfp)
Gao Xiang9248fce2018-12-08 00:19:15 +0800979{
Gao Xiang97e86a82019-07-31 23:57:47 +0800980 const pgoff_t index = pcl->obj.index;
Gao Xiang9248fce2018-12-08 00:19:15 +0800981 bool tocache = false;
982
983 struct address_space *mapping;
984 struct page *oldpage, *page;
985
Gao Xiang92e6efd2018-12-08 00:19:16 +0800986 compressed_page_t t;
987 int justfound;
988
Gao Xiang9248fce2018-12-08 00:19:15 +0800989repeat:
Gao Xiang97e86a82019-07-31 23:57:47 +0800990 page = READ_ONCE(pcl->compressed_pages[nr]);
Gao Xiang9248fce2018-12-08 00:19:15 +0800991 oldpage = page;
992
993 if (!page)
994 goto out_allocpage;
995
996 /*
997 * the cached page has not been allocated and
998 * an placeholder is out there, prepare it now.
999 */
Gao Xiangbda17a42019-10-08 20:56:13 +08001000 if (page == PAGE_UNALLOCATED) {
Gao Xiang9248fce2018-12-08 00:19:15 +08001001 tocache = true;
1002 goto out_allocpage;
1003 }
1004
Gao Xiang92e6efd2018-12-08 00:19:16 +08001005 /* process the target tagged pointer */
1006 t = tagptr_init(compressed_page_t, page);
1007 justfound = tagptr_unfold_tags(t);
1008 page = tagptr_unfold_ptr(t);
1009
Gao Xiang9248fce2018-12-08 00:19:15 +08001010 mapping = READ_ONCE(page->mapping);
1011
1012 /*
Gao Xiang9248fce2018-12-08 00:19:15 +08001013 * unmanaged (file) pages are all locked solidly,
1014 * therefore it is impossible for `mapping' to be NULL.
1015 */
1016 if (mapping && mapping != mc)
1017 /* ought to be unmanaged pages */
1018 goto out;
1019
1020 lock_page(page);
1021
Gao Xiang92e6efd2018-12-08 00:19:16 +08001022 /* only true if page reclaim goes wrong, should never happen */
1023 DBG_BUGON(justfound && PagePrivate(page));
1024
Gao Xiang9248fce2018-12-08 00:19:15 +08001025 /* the page is still in manage cache */
1026 if (page->mapping == mc) {
Gao Xiang97e86a82019-07-31 23:57:47 +08001027 WRITE_ONCE(pcl->compressed_pages[nr], page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001028
Gao Xiang11152492019-03-25 11:40:07 +08001029 ClearPageError(page);
Gao Xiang9248fce2018-12-08 00:19:15 +08001030 if (!PagePrivate(page)) {
Gao Xiang92e6efd2018-12-08 00:19:16 +08001031 /*
1032 * impossible to be !PagePrivate(page) for
1033 * the current restriction as well if
1034 * the page is already in compressed_pages[].
1035 */
1036 DBG_BUGON(!justfound);
1037
1038 justfound = 0;
Gao Xiang97e86a82019-07-31 23:57:47 +08001039 set_page_private(page, (unsigned long)pcl);
Gao Xiang9248fce2018-12-08 00:19:15 +08001040 SetPagePrivate(page);
1041 }
1042
1043 /* no need to submit io if it is already up-to-date */
1044 if (PageUptodate(page)) {
1045 unlock_page(page);
1046 page = NULL;
1047 }
1048 goto out;
1049 }
1050
1051 /*
1052 * the managed page has been truncated, it's unsafe to
1053 * reuse this one, let's allocate a new cache-managed page.
1054 */
1055 DBG_BUGON(page->mapping);
Gao Xiang92e6efd2018-12-08 00:19:16 +08001056 DBG_BUGON(!justfound);
Gao Xiang9248fce2018-12-08 00:19:15 +08001057
1058 tocache = true;
1059 unlock_page(page);
1060 put_page(page);
1061out_allocpage:
Gao Xiang5ddcee12019-11-21 21:59:54 +08001062 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1063 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1064 /* non-LRU / non-movable temporary page is needed */
Gao Xiang9248fce2018-12-08 00:19:15 +08001065 page->mapping = Z_EROFS_MAPPING_STAGING;
Gao Xiang5ddcee12019-11-21 21:59:54 +08001066 tocache = false;
Gao Xiang9248fce2018-12-08 00:19:15 +08001067 }
1068
Gao Xiang5ddcee12019-11-21 21:59:54 +08001069 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1070 if (tocache) {
1071 /* since it added to managed cache successfully */
1072 unlock_page(page);
1073 put_page(page);
1074 } else {
1075 list_add(&page->lru, pagepool);
1076 }
1077 cond_resched();
1078 goto repeat;
1079 }
Gao Xianga30573b2020-10-22 22:57:21 +08001080
1081 if (tocache) {
1082 set_page_private(page, (unsigned long)pcl);
1083 SetPagePrivate(page);
1084 }
Gao Xiang9248fce2018-12-08 00:19:15 +08001085out: /* 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 Xiang6ea5aad2020-09-19 15:27:30 +08001154 struct z_erofs_decompress_frontend *f,
Gao Xiang0c638f72019-11-08 11:37:33 +08001155 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 Xiang6ea5aad2020-09-19 15:27:30 +08001163 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001164 /* since bio will be NULL, no need to initialize last_index */
Kees Cook3f649ab2020-06-03 13:09:38 -07001165 pgoff_t last_index;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001166 unsigned int nr_bios = 0;
1167 struct bio *bio = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001168
Gao Xianga4b1fab2019-10-08 20:56:15 +08001169 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1170 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1171 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
Gao Xiang3883a792018-07-26 20:22:06 +08001172
1173 /* by default, all need io submission */
Gao Xiang7146a4f2018-12-08 00:19:18 +08001174 q[JQ_SUBMIT]->head = owned_head;
Gao Xiang3883a792018-07-26 20:22:06 +08001175
1176 do {
Gao Xiang97e86a82019-07-31 23:57:47 +08001177 struct z_erofs_pcluster *pcl;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001178 pgoff_t cur, end;
1179 unsigned int i = 0;
1180 bool bypass = true;
Gao Xiang3883a792018-07-26 20:22:06 +08001181
1182 /* no possible 'owned_head' equals the following */
Gao Xiang97e86a82019-07-31 23:57:47 +08001183 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1184 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001185
Gao Xiang97e86a82019-07-31 23:57:47 +08001186 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1187
Gao Xiang1e4a2952020-01-21 14:48:19 +08001188 cur = pcl->obj.index;
1189 end = cur + BIT(pcl->clusterbits);
Gao Xiang3883a792018-07-26 20:22:06 +08001190
1191 /* close the main owned chain at first */
Gao Xiang97e86a82019-07-31 23:57:47 +08001192 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1193 Z_EROFS_PCLUSTER_TAIL_CLOSED);
Gao Xiang3883a792018-07-26 20:22:06 +08001194
Gao Xiang1e4a2952020-01-21 14:48:19 +08001195 do {
1196 struct page *page;
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;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001219 if (f->readahead)
1220 bio->bi_opf |= REQ_RAHEAD;
Gao Xiang1e4a2952020-01-21 14:48:19 +08001221 ++nr_bios;
1222 }
Gao Xiang94e4e152019-09-04 10:09:04 +08001223
Gao Xiang6c3e4852020-09-19 15:27:28 +08001224 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
Gao Xiang1e4a2952020-01-21 14:48:19 +08001225 goto submit_bio_retry;
Gao Xiang3883a792018-07-26 20:22:06 +08001226
Gao Xiang1e4a2952020-01-21 14:48:19 +08001227 last_index = cur;
1228 bypass = false;
1229 } while (++cur < end);
Gao Xiang3883a792018-07-26 20:22:06 +08001230
Gao Xiang1e4a2952020-01-21 14:48:19 +08001231 if (!bypass)
Gao Xiang97e86a82019-07-31 23:57:47 +08001232 qtail[JQ_SUBMIT] = &pcl->next;
Gao Xiang7146a4f2018-12-08 00:19:18 +08001233 else
Gao Xiang97e86a82019-07-31 23:57:47 +08001234 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1235 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
Gao Xiang3883a792018-07-26 20:22:06 +08001236
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001237 if (bio)
Gao Xiang94e4e152019-09-04 10:09:04 +08001238 submit_bio(bio);
Gao Xiang3883a792018-07-26 20:22:06 +08001239
Gao Xiang587a67b2020-01-21 14:47:47 +08001240 /*
1241 * although background is preferred, no one is pending for submission.
1242 * don't issue workqueue for decompression but drop it directly instead.
1243 */
1244 if (!*force_fg && !nr_bios) {
1245 kvfree(q[JQ_SUBMIT]);
Gao Xiang1e4a2952020-01-21 14:48:19 +08001246 return;
Gao Xiang587a67b2020-01-21 14:47:47 +08001247 }
Gao Xianga4b1fab2019-10-08 20:56:15 +08001248 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
Gao Xiang3883a792018-07-26 20:22:06 +08001249}
1250
Gao Xiang0c638f72019-11-08 11:37:33 +08001251static void z_erofs_runqueue(struct super_block *sb,
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001252 struct z_erofs_decompress_frontend *f,
Gao Xiang0c638f72019-11-08 11:37:33 +08001253 struct list_head *pagepool, bool force_fg)
Gao Xiang3883a792018-07-26 20:22:06 +08001254{
Gao Xianga4b1fab2019-10-08 20:56:15 +08001255 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
Gao Xiang3883a792018-07-26 20:22:06 +08001256
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001257 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
Gao Xiang3883a792018-07-26 20:22:06 +08001258 return;
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001259 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
Gao Xiang3883a792018-07-26 20:22:06 +08001260
Gao Xiang0c638f72019-11-08 11:37:33 +08001261 /* handle bypass queue (no i/o pclusters) immediately */
1262 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
Gao Xiang4279f3f2019-07-31 23:57:49 +08001263
Gao Xiang3883a792018-07-26 20:22:06 +08001264 if (!force_fg)
1265 return;
1266
1267 /* wait until all bios are completed */
Gao Xianga93f8c32019-10-08 20:56:16 +08001268 io_wait_event(io[JQ_SUBMIT].u.wait,
1269 !atomic_read(&io[JQ_SUBMIT].pending_bios));
Gao Xiang3883a792018-07-26 20:22:06 +08001270
Gao Xiang0c638f72019-11-08 11:37:33 +08001271 /* handle synchronous decompress queue in the caller context */
1272 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001273}
1274
Gao Xiang0c638f72019-11-08 11:37:33 +08001275static int z_erofs_readpage(struct file *file, struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +08001276{
1277 struct inode *const inode = page->mapping->host;
Gao Xiang97e86a82019-07-31 23:57:47 +08001278 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001279 int err;
1280 LIST_HEAD(pagepool);
1281
Gao Xiangba9ce772018-11-23 01:15:58 +08001282 trace_erofs_readpage(page, false);
1283
Gao Xiangf0c519f2018-11-23 01:21:49 +08001284 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1285
Chao Yue3f78d52020-09-17 09:18:21 +08001286 err = z_erofs_do_read_page(&f, page);
Gao Xiang97e86a82019-07-31 23:57:47 +08001287 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001288
Gao Xiangee451972019-08-19 18:34:21 +08001289 /* if some compressed cluster ready, need submit them anyway */
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001290 z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
Gao Xiangee451972019-08-19 18:34:21 +08001291
1292 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001293 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
Gao Xiangee451972019-08-19 18:34:21 +08001294
Chao Yu3b423412019-01-15 09:42:21 +08001295 if (f.map.mpage)
1296 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001297
1298 /* clean up the remaining free pages */
1299 put_pages_list(&pagepool);
Gao Xiangee451972019-08-19 18:34:21 +08001300 return err;
Gao Xiang3883a792018-07-26 20:22:06 +08001301}
1302
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001303static void z_erofs_readahead(struct readahead_control *rac)
Gao Xiang3883a792018-07-26 20:22:06 +08001304{
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001305 struct inode *const inode = rac->mapping->host;
Gao Xiang5fb76bb2018-09-20 00:06:56 +08001306 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
Gao Xiang3883a792018-07-26 20:22:06 +08001307
Gao Xiangbf9a1232020-09-19 15:27:29 +08001308 unsigned int nr_pages = readahead_count(rac);
1309 bool sync = (nr_pages <= sbi->ctx.max_sync_decompress_pages);
Gao Xiang97e86a82019-07-31 23:57:47 +08001310 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001311 struct page *page, *head = NULL;
Gao Xiang3883a792018-07-26 20:22:06 +08001312 LIST_HEAD(pagepool);
1313
Gao Xiangbf9a1232020-09-19 15:27:29 +08001314 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
Chen Gong284db122018-09-18 22:27:27 +08001315
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001316 f.readahead = true;
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001317 f.headoffset = readahead_pos(rac);
Gao Xiangf0c519f2018-11-23 01:21:49 +08001318
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001319 while ((page = readahead_page(rac))) {
Gao Xiang3883a792018-07-26 20:22:06 +08001320 prefetchw(&page->flags);
Gao Xiang3883a792018-07-26 20:22:06 +08001321
Gao Xiang2d9b5dc2018-11-23 01:21:48 +08001322 /*
1323 * A pure asynchronous readahead is indicated if
1324 * a PG_readahead marked page is hitted at first.
1325 * Let's also do asynchronous decompression for this case.
1326 */
1327 sync &= !(PageReadahead(page) && !head);
1328
Gao Xiang3883a792018-07-26 20:22:06 +08001329 set_page_private(page, (unsigned long)head);
1330 head = page;
1331 }
1332
Cristian Sicilia42d40b42018-11-12 21:43:57 +01001333 while (head) {
Gao Xiang3883a792018-07-26 20:22:06 +08001334 struct page *page = head;
1335 int err;
1336
1337 /* traversal in reverse order */
1338 head = (void *)page_private(page);
1339
Chao Yue3f78d52020-09-17 09:18:21 +08001340 err = z_erofs_do_read_page(&f, page);
Gao Xianga5876e22019-09-04 10:08:56 +08001341 if (err)
Gao Xiang4f761fa2019-09-04 10:09:09 +08001342 erofs_err(inode->i_sb,
1343 "readahead error at page %lu @ nid %llu",
1344 page->index, EROFS_I(inode)->nid);
Gao Xiang3883a792018-07-26 20:22:06 +08001345 put_page(page);
1346 }
1347
Gao Xiang97e86a82019-07-31 23:57:47 +08001348 (void)z_erofs_collector_end(&f.clt);
Gao Xiang3883a792018-07-26 20:22:06 +08001349
Gao Xiang6ea5aad2020-09-19 15:27:30 +08001350 z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
Gao Xiang3883a792018-07-26 20:22:06 +08001351
Chao Yu3b423412019-01-15 09:42:21 +08001352 if (f.map.mpage)
1353 put_page(f.map.mpage);
Gao Xiang3883a792018-07-26 20:22:06 +08001354
1355 /* clean up the remaining free pages */
1356 put_pages_list(&pagepool);
Gao Xiang3883a792018-07-26 20:22:06 +08001357}
1358
Gao Xiang0c638f72019-11-08 11:37:33 +08001359const struct address_space_operations z_erofs_aops = {
1360 .readpage = z_erofs_readpage,
Matthew Wilcox (Oracle)06150902020-06-01 21:47:13 -07001361 .readahead = z_erofs_readahead,
Gao Xiang3883a792018-07-26 20:22:06 +08001362};
Gao Xiang02827e12018-07-26 20:21:58 +08001363