147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only
247e4937aSGao Xiang /*
347e4937aSGao Xiang * Copyright (C) 2019 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov * https://www.huawei.com/
55a7cce82SGao Xiang * Copyright (C) 2024 Alibaba Cloud
647e4937aSGao Xiang */
747e4937aSGao Xiang #include "compress.h"
847e4937aSGao Xiang #include <linux/lz4.h>
947e4937aSGao Xiang
1047e4937aSGao Xiang #ifndef LZ4_DISTANCE_MAX /* history window size */
1147e4937aSGao Xiang #define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
1247e4937aSGao Xiang #endif
1347e4937aSGao Xiang
1447e4937aSGao Xiang #define LZ4_MAX_DISTANCE_PAGES (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
1547e4937aSGao Xiang #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
1647e4937aSGao Xiang #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32)
1747e4937aSGao Xiang #endif
1847e4937aSGao Xiang
19d67aee76SGao Xiang struct z_erofs_lz4_decompress_ctx {
20d67aee76SGao Xiang struct z_erofs_decompress_req *rq;
21d67aee76SGao Xiang /* # of encoded, decoded pages */
22d67aee76SGao Xiang unsigned int inpages, outpages;
23d67aee76SGao Xiang /* decoded block total length (used for in-place decompression) */
24d67aee76SGao Xiang unsigned int oend;
25d67aee76SGao Xiang };
26d67aee76SGao Xiang
z_erofs_load_lz4_config(struct super_block * sb,struct erofs_super_block * dsb,void * data,int size)27efb4fb02SGao Xiang static int z_erofs_load_lz4_config(struct super_block *sb,
28efb4fb02SGao Xiang struct erofs_super_block *dsb, void *data, int size)
295d50538fSHuang Jianan {
304fea63f7SGao Xiang struct erofs_sb_info *sbi = EROFS_SB(sb);
31efb4fb02SGao Xiang struct z_erofs_lz4_cfgs *lz4 = data;
3246249cdeSGao Xiang u16 distance;
3346249cdeSGao Xiang
3446249cdeSGao Xiang if (lz4) {
3546249cdeSGao Xiang if (size < sizeof(struct z_erofs_lz4_cfgs)) {
3646249cdeSGao Xiang erofs_err(sb, "invalid lz4 cfgs, size=%u", size);
3746249cdeSGao Xiang return -EINVAL;
3846249cdeSGao Xiang }
3946249cdeSGao Xiang distance = le16_to_cpu(lz4->max_distance);
404fea63f7SGao Xiang
414fea63f7SGao Xiang sbi->lz4.max_pclusterblks = le16_to_cpu(lz4->max_pclusterblks);
424fea63f7SGao Xiang if (!sbi->lz4.max_pclusterblks) {
434fea63f7SGao Xiang sbi->lz4.max_pclusterblks = 1; /* reserved case */
444fea63f7SGao Xiang } else if (sbi->lz4.max_pclusterblks >
453acea5fcSJingbo Xu erofs_blknr(sb, Z_EROFS_PCLUSTER_MAX_SIZE)) {
464fea63f7SGao Xiang erofs_err(sb, "too large lz4 pclusterblks %u",
474fea63f7SGao Xiang sbi->lz4.max_pclusterblks);
484fea63f7SGao Xiang return -EINVAL;
494fea63f7SGao Xiang }
5046249cdeSGao Xiang } else {
5114373711SGao Xiang distance = le16_to_cpu(dsb->u1.lz4_max_distance);
524fea63f7SGao Xiang sbi->lz4.max_pclusterblks = 1;
5346249cdeSGao Xiang }
545d50538fSHuang Jianan
554fea63f7SGao Xiang sbi->lz4.max_distance_pages = distance ?
565d50538fSHuang Jianan DIV_ROUND_UP(distance, PAGE_SIZE) + 1 :
575d50538fSHuang Jianan LZ4_MAX_DISTANCE_PAGES;
58f36f3010SChunhai Guo return z_erofs_gbuf_growsize(sbi->lz4.max_pclusterblks);
595d50538fSHuang Jianan }
605d50538fSHuang Jianan
61966edfb0SGao Xiang /*
62966edfb0SGao Xiang * Fill all gaps with bounce pages if it's a sparse page list. Also check if
63966edfb0SGao Xiang * all physical pages are consecutive, which can be seen for moderate CR.
64966edfb0SGao Xiang */
z_erofs_lz4_prepare_dstpages(struct z_erofs_lz4_decompress_ctx * ctx,struct page ** pagepool)65d67aee76SGao Xiang static int z_erofs_lz4_prepare_dstpages(struct z_erofs_lz4_decompress_ctx *ctx,
66eaa9172aSGao Xiang struct page **pagepool)
6747e4937aSGao Xiang {
68d67aee76SGao Xiang struct z_erofs_decompress_req *rq = ctx->rq;
6947e4937aSGao Xiang struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
7047e4937aSGao Xiang unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
7147e4937aSGao Xiang BITS_PER_LONG)] = { 0 };
725d50538fSHuang Jianan unsigned int lz4_max_distance_pages =
735d50538fSHuang Jianan EROFS_SB(rq->sb)->lz4.max_distance_pages;
7447e4937aSGao Xiang void *kaddr = NULL;
7547e4937aSGao Xiang unsigned int i, j, top;
7647e4937aSGao Xiang
7747e4937aSGao Xiang top = 0;
78d67aee76SGao Xiang for (i = j = 0; i < ctx->outpages; ++i, ++j) {
7947e4937aSGao Xiang struct page *const page = rq->out[i];
8047e4937aSGao Xiang struct page *victim;
8147e4937aSGao Xiang
825d50538fSHuang Jianan if (j >= lz4_max_distance_pages)
8347e4937aSGao Xiang j = 0;
8447e4937aSGao Xiang
8547e4937aSGao Xiang /* 'valid' bounced can only be tested after a complete round */
86267f2492SGao Xiang if (!rq->fillgaps && test_bit(j, bounced)) {
875d50538fSHuang Jianan DBG_BUGON(i < lz4_max_distance_pages);
885d50538fSHuang Jianan DBG_BUGON(top >= lz4_max_distance_pages);
895d50538fSHuang Jianan availables[top++] = rq->out[i - lz4_max_distance_pages];
9047e4937aSGao Xiang }
9147e4937aSGao Xiang
9247e4937aSGao Xiang if (page) {
9347e4937aSGao Xiang __clear_bit(j, bounced);
94448b5a15SGao Xiang if (!PageHighMem(page)) {
95448b5a15SGao Xiang if (!i) {
9647e4937aSGao Xiang kaddr = page_address(page);
97448b5a15SGao Xiang continue;
9847e4937aSGao Xiang }
99448b5a15SGao Xiang if (kaddr &&
100448b5a15SGao Xiang kaddr + PAGE_SIZE == page_address(page)) {
101448b5a15SGao Xiang kaddr += PAGE_SIZE;
102448b5a15SGao Xiang continue;
103448b5a15SGao Xiang }
104448b5a15SGao Xiang }
105448b5a15SGao Xiang kaddr = NULL;
10647e4937aSGao Xiang continue;
10747e4937aSGao Xiang }
10847e4937aSGao Xiang kaddr = NULL;
10947e4937aSGao Xiang __set_bit(j, bounced);
11047e4937aSGao Xiang
11147e4937aSGao Xiang if (top) {
11247e4937aSGao Xiang victim = availables[--top];
11347e4937aSGao Xiang } else {
1140f6273abSChunhai Guo victim = __erofs_allocpage(pagepool, rq->gfp, true);
115d9281660SChunhai Guo if (!victim)
116d9281660SChunhai Guo return -ENOMEM;
1176aaa7b06SGao Xiang set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE);
11847e4937aSGao Xiang }
11947e4937aSGao Xiang rq->out[i] = victim;
12047e4937aSGao Xiang }
12147e4937aSGao Xiang return kaddr ? 1 : 0;
12247e4937aSGao Xiang }
12347e4937aSGao Xiang
z_erofs_lz4_handle_overlap(struct z_erofs_lz4_decompress_ctx * ctx,void * inpage,void * out,unsigned int * inputmargin,int * maptype,bool may_inplace)124d67aee76SGao Xiang static void *z_erofs_lz4_handle_overlap(struct z_erofs_lz4_decompress_ctx *ctx,
1253c12466bSGao Xiang void *inpage, void *out, unsigned int *inputmargin,
1263c12466bSGao Xiang int *maptype, bool may_inplace)
12747e4937aSGao Xiang {
128d67aee76SGao Xiang struct z_erofs_decompress_req *rq = ctx->rq;
1293c12466bSGao Xiang unsigned int omargin, total, i;
130598162d0SGao Xiang struct page **in;
131598162d0SGao Xiang void *src, *tmp;
13247e4937aSGao Xiang
133598162d0SGao Xiang if (rq->inplace_io) {
134d67aee76SGao Xiang omargin = PAGE_ALIGN(ctx->oend) - ctx->oend;
135ab749badSGao Xiang if (rq->partial_decoding || !may_inplace ||
136d67aee76SGao Xiang omargin < LZ4_DECOMPRESS_INPLACE_MARGIN(rq->inputsize))
137598162d0SGao Xiang goto docopy;
138598162d0SGao Xiang
1393c12466bSGao Xiang for (i = 0; i < ctx->inpages; ++i)
1403c12466bSGao Xiang if (rq->out[ctx->outpages - ctx->inpages + i] !=
1413c12466bSGao Xiang rq->in[i])
142598162d0SGao Xiang goto docopy;
1433c12466bSGao Xiang kunmap_local(inpage);
1443c12466bSGao Xiang *maptype = 3;
1453c12466bSGao Xiang return out + ((ctx->outpages - ctx->inpages) << PAGE_SHIFT);
146598162d0SGao Xiang }
147598162d0SGao Xiang
148d67aee76SGao Xiang if (ctx->inpages <= 1) {
149598162d0SGao Xiang *maptype = 0;
150598162d0SGao Xiang return inpage;
151598162d0SGao Xiang }
152123ec246SGao Xiang kunmap_local(inpage);
153d67aee76SGao Xiang src = erofs_vm_map_ram(rq->in, ctx->inpages);
154598162d0SGao Xiang if (!src)
155598162d0SGao Xiang return ERR_PTR(-ENOMEM);
156598162d0SGao Xiang *maptype = 1;
157598162d0SGao Xiang return src;
158598162d0SGao Xiang
159598162d0SGao Xiang docopy:
160598162d0SGao Xiang /* Or copy compressed data which can be overlapped to per-CPU buffer */
161598162d0SGao Xiang in = rq->in;
162f36f3010SChunhai Guo src = z_erofs_get_gbuf(ctx->inpages);
163598162d0SGao Xiang if (!src) {
164598162d0SGao Xiang DBG_BUGON(1);
165123ec246SGao Xiang kunmap_local(inpage);
166598162d0SGao Xiang return ERR_PTR(-EFAULT);
167598162d0SGao Xiang }
168598162d0SGao Xiang
169598162d0SGao Xiang tmp = src;
170598162d0SGao Xiang total = rq->inputsize;
171598162d0SGao Xiang while (total) {
172598162d0SGao Xiang unsigned int page_copycnt =
173598162d0SGao Xiang min_t(unsigned int, total, PAGE_SIZE - *inputmargin);
174598162d0SGao Xiang
175598162d0SGao Xiang if (!inpage)
176123ec246SGao Xiang inpage = kmap_local_page(*in);
177598162d0SGao Xiang memcpy(tmp, inpage + *inputmargin, page_copycnt);
178123ec246SGao Xiang kunmap_local(inpage);
179598162d0SGao Xiang inpage = NULL;
180598162d0SGao Xiang tmp += page_copycnt;
181598162d0SGao Xiang total -= page_copycnt;
182598162d0SGao Xiang ++in;
183598162d0SGao Xiang *inputmargin = 0;
184598162d0SGao Xiang }
185598162d0SGao Xiang *maptype = 2;
186598162d0SGao Xiang return src;
18747e4937aSGao Xiang }
18847e4937aSGao Xiang
18910e5f6e4SGao Xiang /*
19010e5f6e4SGao Xiang * Get the exact inputsize with zero_padding feature.
19110e5f6e4SGao Xiang * - For LZ4, it should work if zero_padding feature is on (5.3+);
19210e5f6e4SGao Xiang * - For MicroLZMA, it'd be enabled all the time.
19310e5f6e4SGao Xiang */
z_erofs_fixup_insize(struct z_erofs_decompress_req * rq,const char * padbuf,unsigned int padbufsize)19410e5f6e4SGao Xiang int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
19510e5f6e4SGao Xiang unsigned int padbufsize)
19610e5f6e4SGao Xiang {
19710e5f6e4SGao Xiang const char *padend;
19810e5f6e4SGao Xiang
19910e5f6e4SGao Xiang padend = memchr_inv(padbuf, 0, padbufsize);
20010e5f6e4SGao Xiang if (!padend)
20110e5f6e4SGao Xiang return -EFSCORRUPTED;
20210e5f6e4SGao Xiang rq->inputsize -= padend - padbuf;
20310e5f6e4SGao Xiang rq->pageofs_in += padend - padbuf;
20410e5f6e4SGao Xiang return 0;
20510e5f6e4SGao Xiang }
20610e5f6e4SGao Xiang
z_erofs_lz4_decompress_mem(struct z_erofs_lz4_decompress_ctx * ctx,u8 * dst)207d67aee76SGao Xiang static int z_erofs_lz4_decompress_mem(struct z_erofs_lz4_decompress_ctx *ctx,
2083c12466bSGao Xiang u8 *dst)
20947e4937aSGao Xiang {
210d67aee76SGao Xiang struct z_erofs_decompress_req *rq = ctx->rq;
211ab749badSGao Xiang bool support_0padding = false, may_inplace = false;
212598162d0SGao Xiang unsigned int inputmargin;
2133c12466bSGao Xiang u8 *out, *headpage, *src;
214598162d0SGao Xiang int ret, maptype;
21547e4937aSGao Xiang
216598162d0SGao Xiang DBG_BUGON(*rq->in == NULL);
217123ec246SGao Xiang headpage = kmap_local_page(*rq->in);
21847e4937aSGao Xiang
21910e5f6e4SGao Xiang /* LZ4 decompression inplace is only safe if zero_padding is enabled */
2207e508f2cSHuang Jianan if (erofs_sb_has_zero_padding(EROFS_SB(rq->sb))) {
22147e4937aSGao Xiang support_0padding = true;
22210e5f6e4SGao Xiang ret = z_erofs_fixup_insize(rq, headpage + rq->pageofs_in,
22310e5f6e4SGao Xiang min_t(unsigned int, rq->inputsize,
2243acea5fcSJingbo Xu rq->sb->s_blocksize - rq->pageofs_in));
22510e5f6e4SGao Xiang if (ret) {
226123ec246SGao Xiang kunmap_local(headpage);
22710e5f6e4SGao Xiang return ret;
22847e4937aSGao Xiang }
229ab749badSGao Xiang may_inplace = !((rq->pageofs_in + rq->inputsize) &
2303acea5fcSJingbo Xu (rq->sb->s_blocksize - 1));
23147e4937aSGao Xiang }
23247e4937aSGao Xiang
23310e5f6e4SGao Xiang inputmargin = rq->pageofs_in;
2343c12466bSGao Xiang src = z_erofs_lz4_handle_overlap(ctx, headpage, dst, &inputmargin,
235ab749badSGao Xiang &maptype, may_inplace);
236598162d0SGao Xiang if (IS_ERR(src))
237598162d0SGao Xiang return PTR_ERR(src);
23847e4937aSGao Xiang
2393c12466bSGao Xiang out = dst + rq->pageofs_out;
240af1038abSGao Xiang /* legacy format could compress extra data in a pcluster. */
241af1038abSGao Xiang if (rq->partial_decoding || !support_0padding)
24247e4937aSGao Xiang ret = LZ4_decompress_safe_partial(src + inputmargin, out,
243598162d0SGao Xiang rq->inputsize, rq->outputsize, rq->outputsize);
244af1038abSGao Xiang else
245af1038abSGao Xiang ret = LZ4_decompress_safe(src + inputmargin, out,
246598162d0SGao Xiang rq->inputsize, rq->outputsize);
247af1038abSGao Xiang
248aa99a76bSGao Xiang if (ret != rq->outputsize) {
249aa99a76bSGao Xiang erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
250598162d0SGao Xiang ret, rq->inputsize, inputmargin, rq->outputsize);
251aa99a76bSGao Xiang if (ret >= 0)
252aa99a76bSGao Xiang memset(out + ret, 0, rq->outputsize - ret);
253496530c7SGao Xiang ret = -EFSCORRUPTED;
2545b6e7e12SYue Hu } else {
2555b6e7e12SYue Hu ret = 0;
25647e4937aSGao Xiang }
25747e4937aSGao Xiang
258598162d0SGao Xiang if (maptype == 0) {
259123ec246SGao Xiang kunmap_local(headpage);
260598162d0SGao Xiang } else if (maptype == 1) {
261d67aee76SGao Xiang vm_unmap_ram(src, ctx->inpages);
262598162d0SGao Xiang } else if (maptype == 2) {
263f36f3010SChunhai Guo z_erofs_put_gbuf(src);
2643c12466bSGao Xiang } else if (maptype != 3) {
265598162d0SGao Xiang DBG_BUGON(1);
266598162d0SGao Xiang return -EFAULT;
267598162d0SGao Xiang }
26847e4937aSGao Xiang return ret;
26947e4937aSGao Xiang }
27047e4937aSGao Xiang
z_erofs_lz4_decompress(struct z_erofs_decompress_req * rq,struct page ** pagepool)271966edfb0SGao Xiang static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq,
272eaa9172aSGao Xiang struct page **pagepool)
27347e4937aSGao Xiang {
274d67aee76SGao Xiang struct z_erofs_lz4_decompress_ctx ctx;
27547e4937aSGao Xiang unsigned int dst_maptype;
27647e4937aSGao Xiang void *dst;
277598162d0SGao Xiang int ret;
27847e4937aSGao Xiang
279d67aee76SGao Xiang ctx.rq = rq;
280d67aee76SGao Xiang ctx.oend = rq->pageofs_out + rq->outputsize;
281d67aee76SGao Xiang ctx.outpages = PAGE_ALIGN(ctx.oend) >> PAGE_SHIFT;
282d67aee76SGao Xiang ctx.inpages = PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT;
283d67aee76SGao Xiang
2845b6e7e12SYue Hu /* one optimized fast path only for non bigpcluster cases yet */
285d67aee76SGao Xiang if (ctx.inpages == 1 && ctx.outpages == 1 && !rq->inplace_io) {
28647e4937aSGao Xiang DBG_BUGON(!*rq->out);
287123ec246SGao Xiang dst = kmap_local_page(*rq->out);
28847e4937aSGao Xiang dst_maptype = 0;
28947e4937aSGao Xiang goto dstmap_out;
29047e4937aSGao Xiang }
29147e4937aSGao Xiang
292598162d0SGao Xiang /* general decoding path which can be used for all cases */
293d67aee76SGao Xiang ret = z_erofs_lz4_prepare_dstpages(&ctx, pagepool);
294d67aee76SGao Xiang if (ret < 0) {
29547e4937aSGao Xiang return ret;
296d67aee76SGao Xiang } else if (ret > 0) {
29747e4937aSGao Xiang dst = page_address(*rq->out);
29847e4937aSGao Xiang dst_maptype = 1;
299d67aee76SGao Xiang } else {
300d67aee76SGao Xiang dst = erofs_vm_map_ram(rq->out, ctx.outpages);
30147e4937aSGao Xiang if (!dst)
30247e4937aSGao Xiang return -ENOMEM;
30347e4937aSGao Xiang dst_maptype = 2;
304d67aee76SGao Xiang }
30547e4937aSGao Xiang
30647e4937aSGao Xiang dstmap_out:
3073c12466bSGao Xiang ret = z_erofs_lz4_decompress_mem(&ctx, dst);
30847e4937aSGao Xiang if (!dst_maptype)
309123ec246SGao Xiang kunmap_local(dst);
31047e4937aSGao Xiang else if (dst_maptype == 2)
311d67aee76SGao Xiang vm_unmap_ram(dst, ctx.outpages);
31247e4937aSGao Xiang return ret;
31347e4937aSGao Xiang }
31447e4937aSGao Xiang
z_erofs_transform_plain(struct z_erofs_decompress_req * rq,struct page ** pagepool)315fdffc091SYue Hu static int z_erofs_transform_plain(struct z_erofs_decompress_req *rq,
316eaa9172aSGao Xiang struct page **pagepool)
31747e4937aSGao Xiang {
3181ca01520SGao Xiang const unsigned int nrpages_in =
3191ca01520SGao Xiang PAGE_ALIGN(rq->pageofs_in + rq->inputsize) >> PAGE_SHIFT;
3201ca01520SGao Xiang const unsigned int nrpages_out =
32147e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
3221ca01520SGao Xiang const unsigned int bs = rq->sb->s_blocksize;
3231ca01520SGao Xiang unsigned int cur = 0, ni = 0, no, pi, po, insz, cnt;
3241ca01520SGao Xiang u8 *kin;
32547e4937aSGao Xiang
326893e5e9bSGao Xiang if (rq->outputsize > rq->inputsize)
327893e5e9bSGao Xiang return -EOPNOTSUPP;
3281ca01520SGao Xiang if (rq->alg == Z_EROFS_COMPRESSION_INTERLACED) {
3291ca01520SGao Xiang cur = bs - (rq->pageofs_out & (bs - 1));
3301ca01520SGao Xiang pi = (rq->pageofs_in + rq->inputsize - cur) & ~PAGE_MASK;
3311ca01520SGao Xiang cur = min(cur, rq->outputsize);
3321ca01520SGao Xiang if (cur && rq->out[0]) {
3331ca01520SGao Xiang kin = kmap_local_page(rq->in[nrpages_in - 1]);
3341ca01520SGao Xiang if (rq->out[0] == rq->in[nrpages_in - 1]) {
3351ca01520SGao Xiang memmove(kin + rq->pageofs_out, kin + pi, cur);
3361ca01520SGao Xiang flush_dcache_page(rq->out[0]);
3371ca01520SGao Xiang } else {
338c5539762SGao Xiang memcpy_to_page(rq->out[0], rq->pageofs_out,
3391ca01520SGao Xiang kin + pi, cur);
3401ca01520SGao Xiang }
3411ca01520SGao Xiang kunmap_local(kin);
3421ca01520SGao Xiang }
3431ca01520SGao Xiang rq->outputsize -= cur;
3441ca01520SGao Xiang }
34547e4937aSGao Xiang
3461ca01520SGao Xiang for (; rq->outputsize; rq->pageofs_in = 0, cur += PAGE_SIZE, ni++) {
3471ca01520SGao Xiang insz = min(PAGE_SIZE - rq->pageofs_in, rq->outputsize);
3481ca01520SGao Xiang rq->outputsize -= insz;
3491ca01520SGao Xiang if (!rq->in[ni])
3501ca01520SGao Xiang continue;
3511ca01520SGao Xiang kin = kmap_local_page(rq->in[ni]);
3521ca01520SGao Xiang pi = 0;
3531ca01520SGao Xiang do {
3541ca01520SGao Xiang no = (rq->pageofs_out + cur + pi) >> PAGE_SHIFT;
3551ca01520SGao Xiang po = (rq->pageofs_out + cur + pi) & ~PAGE_MASK;
3561ca01520SGao Xiang DBG_BUGON(no >= nrpages_out);
3571ca01520SGao Xiang cnt = min(insz - pi, PAGE_SIZE - po);
3581ca01520SGao Xiang if (rq->out[no] == rq->in[ni]) {
3591ca01520SGao Xiang memmove(kin + po,
3601ca01520SGao Xiang kin + rq->pageofs_in + pi, cnt);
3611ca01520SGao Xiang flush_dcache_page(rq->out[no]);
3621ca01520SGao Xiang } else if (rq->out[no]) {
3631ca01520SGao Xiang memcpy_to_page(rq->out[no], po,
3641ca01520SGao Xiang kin + rq->pageofs_in + pi, cnt);
3654d202437SGao Xiang }
3661ca01520SGao Xiang pi += cnt;
3671ca01520SGao Xiang } while (pi < insz);
3681ca01520SGao Xiang kunmap_local(kin);
3694d202437SGao Xiang }
3701ca01520SGao Xiang DBG_BUGON(ni > nrpages_in);
37147e4937aSGao Xiang return 0;
37247e4937aSGao Xiang }
37347e4937aSGao Xiang
z_erofs_stream_switch_bufs(struct z_erofs_stream_dctx * dctx,void ** dst,void ** src,struct page ** pgpl)37484a2ceefSGao Xiang int z_erofs_stream_switch_bufs(struct z_erofs_stream_dctx *dctx, void **dst,
37584a2ceefSGao Xiang void **src, struct page **pgpl)
37684a2ceefSGao Xiang {
37784a2ceefSGao Xiang struct z_erofs_decompress_req *rq = dctx->rq;
37884a2ceefSGao Xiang struct super_block *sb = rq->sb;
37984a2ceefSGao Xiang struct page **pgo, *tmppage;
38084a2ceefSGao Xiang unsigned int j;
38184a2ceefSGao Xiang
38284a2ceefSGao Xiang if (!dctx->avail_out) {
38384a2ceefSGao Xiang if (++dctx->no >= dctx->outpages || !rq->outputsize) {
38484a2ceefSGao Xiang erofs_err(sb, "insufficient space for decompressed data");
38584a2ceefSGao Xiang return -EFSCORRUPTED;
38684a2ceefSGao Xiang }
38784a2ceefSGao Xiang
38884a2ceefSGao Xiang if (dctx->kout)
38984a2ceefSGao Xiang kunmap_local(dctx->kout);
39084a2ceefSGao Xiang dctx->avail_out = min(rq->outputsize, PAGE_SIZE - rq->pageofs_out);
39184a2ceefSGao Xiang rq->outputsize -= dctx->avail_out;
39284a2ceefSGao Xiang pgo = &rq->out[dctx->no];
39384a2ceefSGao Xiang if (!*pgo && rq->fillgaps) { /* deduped */
39484a2ceefSGao Xiang *pgo = erofs_allocpage(pgpl, rq->gfp);
39584a2ceefSGao Xiang if (!*pgo) {
39684a2ceefSGao Xiang dctx->kout = NULL;
39784a2ceefSGao Xiang return -ENOMEM;
39884a2ceefSGao Xiang }
39984a2ceefSGao Xiang set_page_private(*pgo, Z_EROFS_SHORTLIVED_PAGE);
40084a2ceefSGao Xiang }
40184a2ceefSGao Xiang if (*pgo) {
40284a2ceefSGao Xiang dctx->kout = kmap_local_page(*pgo);
40384a2ceefSGao Xiang *dst = dctx->kout + rq->pageofs_out;
40484a2ceefSGao Xiang } else {
40584a2ceefSGao Xiang *dst = dctx->kout = NULL;
40684a2ceefSGao Xiang }
40784a2ceefSGao Xiang rq->pageofs_out = 0;
40884a2ceefSGao Xiang }
40984a2ceefSGao Xiang
41084a2ceefSGao Xiang if (dctx->inbuf_pos == dctx->inbuf_sz && rq->inputsize) {
41184a2ceefSGao Xiang if (++dctx->ni >= dctx->inpages) {
41284a2ceefSGao Xiang erofs_err(sb, "invalid compressed data");
41384a2ceefSGao Xiang return -EFSCORRUPTED;
41484a2ceefSGao Xiang }
41584a2ceefSGao Xiang if (dctx->kout) /* unlike kmap(), take care of the orders */
41684a2ceefSGao Xiang kunmap_local(dctx->kout);
41784a2ceefSGao Xiang kunmap_local(dctx->kin);
41884a2ceefSGao Xiang
41984a2ceefSGao Xiang dctx->inbuf_sz = min_t(u32, rq->inputsize, PAGE_SIZE);
42084a2ceefSGao Xiang rq->inputsize -= dctx->inbuf_sz;
42184a2ceefSGao Xiang dctx->kin = kmap_local_page(rq->in[dctx->ni]);
42284a2ceefSGao Xiang *src = dctx->kin;
42384a2ceefSGao Xiang dctx->bounced = false;
42484a2ceefSGao Xiang if (dctx->kout) {
42584a2ceefSGao Xiang j = (u8 *)*dst - dctx->kout;
42684a2ceefSGao Xiang dctx->kout = kmap_local_page(rq->out[dctx->no]);
42784a2ceefSGao Xiang *dst = dctx->kout + j;
42884a2ceefSGao Xiang }
42984a2ceefSGao Xiang dctx->inbuf_pos = 0;
43084a2ceefSGao Xiang }
43184a2ceefSGao Xiang
43284a2ceefSGao Xiang /*
43384a2ceefSGao Xiang * Handle overlapping: Use the given bounce buffer if the input data is
43484a2ceefSGao Xiang * under processing; Or utilize short-lived pages from the on-stack page
43584a2ceefSGao Xiang * pool, where pages are shared among the same request. Note that only
43684a2ceefSGao Xiang * a few inplace I/O pages need to be doubled.
43784a2ceefSGao Xiang */
43884a2ceefSGao Xiang if (!dctx->bounced && rq->out[dctx->no] == rq->in[dctx->ni]) {
43984a2ceefSGao Xiang memcpy(dctx->bounce, *src, dctx->inbuf_sz);
44084a2ceefSGao Xiang *src = dctx->bounce;
44184a2ceefSGao Xiang dctx->bounced = true;
44284a2ceefSGao Xiang }
44384a2ceefSGao Xiang
44484a2ceefSGao Xiang for (j = dctx->ni + 1; j < dctx->inpages; ++j) {
44584a2ceefSGao Xiang if (rq->out[dctx->no] != rq->in[j])
44684a2ceefSGao Xiang continue;
44784a2ceefSGao Xiang tmppage = erofs_allocpage(pgpl, rq->gfp);
44884a2ceefSGao Xiang if (!tmppage)
44984a2ceefSGao Xiang return -ENOMEM;
45084a2ceefSGao Xiang set_page_private(tmppage, Z_EROFS_SHORTLIVED_PAGE);
45184a2ceefSGao Xiang copy_highpage(tmppage, rq->in[j]);
45284a2ceefSGao Xiang rq->in[j] = tmppage;
45384a2ceefSGao Xiang }
45484a2ceefSGao Xiang return 0;
45584a2ceefSGao Xiang }
45684a2ceefSGao Xiang
457392d20ccSGao Xiang const struct z_erofs_decompressor *z_erofs_decomp[] = {
458392d20ccSGao Xiang [Z_EROFS_COMPRESSION_SHIFTED] = &(const struct z_erofs_decompressor) {
459fdffc091SYue Hu .decompress = z_erofs_transform_plain,
460966edfb0SGao Xiang .name = "shifted"
461966edfb0SGao Xiang },
462392d20ccSGao Xiang [Z_EROFS_COMPRESSION_INTERLACED] = &(const struct z_erofs_decompressor) {
463fdffc091SYue Hu .decompress = z_erofs_transform_plain,
464fdffc091SYue Hu .name = "interlaced"
465fdffc091SYue Hu },
466392d20ccSGao Xiang [Z_EROFS_COMPRESSION_LZ4] = &(const struct z_erofs_decompressor) {
467efb4fb02SGao Xiang .config = z_erofs_load_lz4_config,
468966edfb0SGao Xiang .decompress = z_erofs_lz4_decompress,
4695a7cce82SGao Xiang .init = z_erofs_gbuf_init,
4705a7cce82SGao Xiang .exit = z_erofs_gbuf_exit,
471966edfb0SGao Xiang .name = "lz4"
472966edfb0SGao Xiang },
473622ceaddSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP_LZMA
474392d20ccSGao Xiang [Z_EROFS_COMPRESSION_LZMA] = &z_erofs_lzma_decomp,
475622ceaddSGao Xiang #endif
476ffa09b3bSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
477392d20ccSGao Xiang [Z_EROFS_COMPRESSION_DEFLATE] = &z_erofs_deflate_decomp,
478ffa09b3bSGao Xiang #endif
4797c35de4dSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP_ZSTD
480392d20ccSGao Xiang [Z_EROFS_COMPRESSION_ZSTD] = &z_erofs_zstd_decomp,
4817c35de4dSGao Xiang #endif
482966edfb0SGao Xiang };
483efb4fb02SGao Xiang
z_erofs_parse_cfgs(struct super_block * sb,struct erofs_super_block * dsb)484efb4fb02SGao Xiang int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
485efb4fb02SGao Xiang {
486efb4fb02SGao Xiang struct erofs_sb_info *sbi = EROFS_SB(sb);
487efb4fb02SGao Xiang struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
488efb4fb02SGao Xiang unsigned int algs, alg;
489efb4fb02SGao Xiang erofs_off_t offset;
490efb4fb02SGao Xiang int size, ret = 0;
491efb4fb02SGao Xiang
492efb4fb02SGao Xiang if (!erofs_sb_has_compr_cfgs(sbi)) {
493118a8cf5SGao Xiang sbi->available_compr_algs = 1 << Z_EROFS_COMPRESSION_LZ4;
494efb4fb02SGao Xiang return z_erofs_load_lz4_config(sb, dsb, NULL, 0);
495efb4fb02SGao Xiang }
496efb4fb02SGao Xiang
497efb4fb02SGao Xiang sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
498efb4fb02SGao Xiang if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
499efb4fb02SGao Xiang erofs_err(sb, "unidentified algorithms %x, please upgrade kernel",
500efb4fb02SGao Xiang sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
501efb4fb02SGao Xiang return -EOPNOTSUPP;
502efb4fb02SGao Xiang }
503efb4fb02SGao Xiang
504efb4fb02SGao Xiang erofs_init_metabuf(&buf, sb);
505efb4fb02SGao Xiang offset = EROFS_SUPER_OFFSET + sbi->sb_size;
506efb4fb02SGao Xiang alg = 0;
507efb4fb02SGao Xiang for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
508392d20ccSGao Xiang const struct z_erofs_decompressor *dec = z_erofs_decomp[alg];
509efb4fb02SGao Xiang void *data;
510efb4fb02SGao Xiang
511efb4fb02SGao Xiang if (!(algs & 1))
512efb4fb02SGao Xiang continue;
513efb4fb02SGao Xiang
514efb4fb02SGao Xiang data = erofs_read_metadata(sb, &buf, &offset, &size);
515efb4fb02SGao Xiang if (IS_ERR(data)) {
516efb4fb02SGao Xiang ret = PTR_ERR(data);
517efb4fb02SGao Xiang break;
518efb4fb02SGao Xiang }
519efb4fb02SGao Xiang
520392d20ccSGao Xiang if (alg < Z_EROFS_COMPRESSION_MAX && dec && dec->config) {
521392d20ccSGao Xiang ret = dec->config(sb, dsb, data, size);
522392d20ccSGao Xiang } else {
523efb4fb02SGao Xiang erofs_err(sb, "algorithm %d isn't enabled on this kernel",
524efb4fb02SGao Xiang alg);
525efb4fb02SGao Xiang ret = -EOPNOTSUPP;
526efb4fb02SGao Xiang }
527efb4fb02SGao Xiang kfree(data);
528efb4fb02SGao Xiang if (ret)
529efb4fb02SGao Xiang break;
530efb4fb02SGao Xiang }
531efb4fb02SGao Xiang erofs_put_metabuf(&buf);
532efb4fb02SGao Xiang return ret;
533efb4fb02SGao Xiang }
5345a7cce82SGao Xiang
z_erofs_init_decompressor(void)5355a7cce82SGao Xiang int __init z_erofs_init_decompressor(void)
5365a7cce82SGao Xiang {
5375a7cce82SGao Xiang int i, err;
5385a7cce82SGao Xiang
5395a7cce82SGao Xiang for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
5405a7cce82SGao Xiang err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
5415a7cce82SGao Xiang if (err) {
542*3fc3e45fSSandeep Dhavale while (i--)
5435a7cce82SGao Xiang if (z_erofs_decomp[i])
5445a7cce82SGao Xiang z_erofs_decomp[i]->exit();
5455a7cce82SGao Xiang return err;
5465a7cce82SGao Xiang }
5475a7cce82SGao Xiang }
5485a7cce82SGao Xiang return 0;
5495a7cce82SGao Xiang }
5505a7cce82SGao Xiang
z_erofs_exit_decompressor(void)5515a7cce82SGao Xiang void z_erofs_exit_decompressor(void)
5525a7cce82SGao Xiang {
5535a7cce82SGao Xiang int i;
5545a7cce82SGao Xiang
5555a7cce82SGao Xiang for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i)
5565a7cce82SGao Xiang if (z_erofs_decomp[i])
5575a7cce82SGao Xiang z_erofs_decomp[i]->exit();
5585a7cce82SGao Xiang }
559