1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2019 HUAWEI, Inc.
4 * https://www.huawei.com/
7 #include <linux/module.h>
10 #ifndef LZ4_DISTANCE_MAX /* history window size */
11 #define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
14 #define LZ4_MAX_DISTANCE_PAGES (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
15 #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
16 #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32)
19 struct z_erofs_lz4_decompress_ctx {
20 struct z_erofs_decompress_req *rq;
21 /* # of encoded, decoded pages */
22 unsigned int inpages, outpages;
23 /* decoded block total length (used for in-place decompression) */
27 int z_erofs_load_lz4_config(struct super_block *sb,
28 struct erofs_super_block *dsb,
29 struct z_erofs_lz4_cfgs *lz4, int size)
31 struct erofs_sb_info *sbi = EROFS_SB(sb);
35 if (size < sizeof(struct z_erofs_lz4_cfgs)) {
36 erofs_err(sb, "invalid lz4 cfgs, size=%u", size);
39 distance = le16_to_cpu(lz4->max_distance);
41 sbi->lz4.max_pclusterblks = le16_to_cpu(lz4->max_pclusterblks);
42 if (!sbi->lz4.max_pclusterblks) {
43 sbi->lz4.max_pclusterblks = 1; /* reserved case */
44 } else if (sbi->lz4.max_pclusterblks >
45 Z_EROFS_PCLUSTER_MAX_SIZE / EROFS_BLKSIZ) {
46 erofs_err(sb, "too large lz4 pclusterblks %u",
47 sbi->lz4.max_pclusterblks);
49 } else if (sbi->lz4.max_pclusterblks >= 2) {
50 erofs_info(sb, "EXPERIMENTAL big pcluster feature in use. Use at your own risk!");
53 distance = le16_to_cpu(dsb->u1.lz4_max_distance);
54 sbi->lz4.max_pclusterblks = 1;
57 sbi->lz4.max_distance_pages = distance ?
58 DIV_ROUND_UP(distance, PAGE_SIZE) + 1 :
59 LZ4_MAX_DISTANCE_PAGES;
60 return erofs_pcpubuf_growsize(sbi->lz4.max_pclusterblks);
64 * Fill all gaps with bounce pages if it's a sparse page list. Also check if
65 * all physical pages are consecutive, which can be seen for moderate CR.
67 static int z_erofs_lz4_prepare_dstpages(struct z_erofs_lz4_decompress_ctx *ctx,
68 struct page **pagepool)
70 struct z_erofs_decompress_req *rq = ctx->rq;
71 struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
72 unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
73 BITS_PER_LONG)] = { 0 };
74 unsigned int lz4_max_distance_pages =
75 EROFS_SB(rq->sb)->lz4.max_distance_pages;
77 unsigned int i, j, top;
80 for (i = j = 0; i < ctx->outpages; ++i, ++j) {
81 struct page *const page = rq->out[i];
84 if (j >= lz4_max_distance_pages)
87 /* 'valid' bounced can only be tested after a complete round */
88 if (test_bit(j, bounced)) {
89 DBG_BUGON(i < lz4_max_distance_pages);
90 DBG_BUGON(top >= lz4_max_distance_pages);
91 availables[top++] = rq->out[i - lz4_max_distance_pages];
95 __clear_bit(j, bounced);
97 if (kaddr + PAGE_SIZE == page_address(page))
102 kaddr = page_address(page);
107 __set_bit(j, bounced);
110 victim = availables[--top];
113 victim = erofs_allocpage(pagepool,
114 GFP_KERNEL | __GFP_NOFAIL);
115 set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE);
119 return kaddr ? 1 : 0;
122 static void *z_erofs_lz4_handle_overlap(struct z_erofs_lz4_decompress_ctx *ctx,
123 void *inpage, unsigned int *inputmargin, int *maptype,
126 struct z_erofs_decompress_req *rq = ctx->rq;
127 unsigned int omargin, total, i, j;
131 if (rq->inplace_io) {
132 omargin = PAGE_ALIGN(ctx->oend) - ctx->oend;
133 if (rq->partial_decoding || !may_inplace ||
134 omargin < LZ4_DECOMPRESS_INPLACE_MARGIN(rq->inputsize))
137 for (i = 0; i < ctx->inpages; ++i) {
138 DBG_BUGON(rq->in[i] == NULL);
139 for (j = 0; j < ctx->outpages - ctx->inpages + i; ++j)
140 if (rq->out[j] == rq->in[i])
145 if (ctx->inpages <= 1) {
149 kunmap_atomic(inpage);
151 src = erofs_vm_map_ram(rq->in, ctx->inpages);
153 return ERR_PTR(-ENOMEM);
158 /* Or copy compressed data which can be overlapped to per-CPU buffer */
160 src = erofs_get_pcpubuf(ctx->inpages);
163 kunmap_atomic(inpage);
164 return ERR_PTR(-EFAULT);
168 total = rq->inputsize;
170 unsigned int page_copycnt =
171 min_t(unsigned int, total, PAGE_SIZE - *inputmargin);
174 inpage = kmap_atomic(*in);
175 memcpy(tmp, inpage + *inputmargin, page_copycnt);
176 kunmap_atomic(inpage);
179 total -= page_copycnt;
188 * Get the exact inputsize with zero_padding feature.
189 * - For LZ4, it should work if zero_padding feature is on (5.3+);
190 * - For MicroLZMA, it'd be enabled all the time.
192 int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
193 unsigned int padbufsize)
197 padend = memchr_inv(padbuf, 0, padbufsize);
199 return -EFSCORRUPTED;
200 rq->inputsize -= padend - padbuf;
201 rq->pageofs_in += padend - padbuf;
205 static int z_erofs_lz4_decompress_mem(struct z_erofs_lz4_decompress_ctx *ctx,
208 struct z_erofs_decompress_req *rq = ctx->rq;
209 bool support_0padding = false, may_inplace = false;
210 unsigned int inputmargin;
214 DBG_BUGON(*rq->in == NULL);
215 headpage = kmap_atomic(*rq->in);
217 /* LZ4 decompression inplace is only safe if zero_padding is enabled */
218 if (erofs_sb_has_zero_padding(EROFS_SB(rq->sb))) {
219 support_0padding = true;
220 ret = z_erofs_fixup_insize(rq, headpage + rq->pageofs_in,
221 min_t(unsigned int, rq->inputsize,
222 EROFS_BLKSIZ - rq->pageofs_in));
224 kunmap_atomic(headpage);
227 may_inplace = !((rq->pageofs_in + rq->inputsize) &
231 inputmargin = rq->pageofs_in;
232 src = z_erofs_lz4_handle_overlap(ctx, headpage, &inputmargin,
233 &maptype, may_inplace);
237 /* legacy format could compress extra data in a pcluster. */
238 if (rq->partial_decoding || !support_0padding)
239 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
240 rq->inputsize, rq->outputsize, rq->outputsize);
242 ret = LZ4_decompress_safe(src + inputmargin, out,
243 rq->inputsize, rq->outputsize);
245 if (ret != rq->outputsize) {
246 erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
247 ret, rq->inputsize, inputmargin, rq->outputsize);
249 print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
250 16, 1, src + inputmargin, rq->inputsize, true);
251 print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
252 16, 1, out, rq->outputsize, true);
255 memset(out + ret, 0, rq->outputsize - ret);
262 kunmap_atomic(headpage);
263 } else if (maptype == 1) {
264 vm_unmap_ram(src, ctx->inpages);
265 } else if (maptype == 2) {
266 erofs_put_pcpubuf(src);
274 static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq,
275 struct page **pagepool)
277 struct z_erofs_lz4_decompress_ctx ctx;
278 unsigned int dst_maptype;
283 ctx.oend = rq->pageofs_out + rq->outputsize;
284 ctx.outpages = PAGE_ALIGN(ctx.oend) >> PAGE_SHIFT;
285 ctx.inpages = PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT;
287 /* one optimized fast path only for non bigpcluster cases yet */
288 if (ctx.inpages == 1 && ctx.outpages == 1 && !rq->inplace_io) {
289 DBG_BUGON(!*rq->out);
290 dst = kmap_atomic(*rq->out);
295 /* general decoding path which can be used for all cases */
296 ret = z_erofs_lz4_prepare_dstpages(&ctx, pagepool);
299 } else if (ret > 0) {
300 dst = page_address(*rq->out);
303 dst = erofs_vm_map_ram(rq->out, ctx.outpages);
310 ret = z_erofs_lz4_decompress_mem(&ctx, dst + rq->pageofs_out);
313 else if (dst_maptype == 2)
314 vm_unmap_ram(dst, ctx.outpages);
318 static int z_erofs_shifted_transform(struct z_erofs_decompress_req *rq,
319 struct page **pagepool)
321 const unsigned int nrpages_out =
322 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
323 const unsigned int righthalf = min_t(unsigned int, rq->outputsize,
324 PAGE_SIZE - rq->pageofs_out);
325 unsigned char *src, *dst;
327 if (nrpages_out > 2) {
332 if (rq->out[0] == *rq->in) {
333 DBG_BUGON(nrpages_out != 1);
337 src = kmap_atomic(*rq->in) + rq->pageofs_in;
339 dst = kmap_atomic(rq->out[0]);
340 memcpy(dst + rq->pageofs_out, src, righthalf);
344 if (nrpages_out == 2) {
345 DBG_BUGON(!rq->out[1]);
346 if (rq->out[1] == *rq->in) {
347 memmove(src, src + righthalf, rq->pageofs_out);
349 dst = kmap_atomic(rq->out[1]);
350 memcpy(dst, src + righthalf, rq->pageofs_out);
358 static struct z_erofs_decompressor decompressors[] = {
359 [Z_EROFS_COMPRESSION_SHIFTED] = {
360 .decompress = z_erofs_shifted_transform,
363 [Z_EROFS_COMPRESSION_LZ4] = {
364 .decompress = z_erofs_lz4_decompress,
367 #ifdef CONFIG_EROFS_FS_ZIP_LZMA
368 [Z_EROFS_COMPRESSION_LZMA] = {
369 .decompress = z_erofs_lzma_decompress,
375 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
376 struct page **pagepool)
378 return decompressors[rq->alg].decompress(rq, pagepool);