blob: 9d85b6c11c6b0c5d8a857ad164328947bf5faa9d [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang7fc45db2019-06-24 15:22:55 +08002/*
Gao Xiang7fc45db2019-06-24 15:22:55 +08003 * Copyright (C) 2019 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang7fc45db2019-06-24 15:22:55 +08005 */
6#include "compress.h"
7#include <linux/lz4.h>
8
9#ifndef LZ4_DISTANCE_MAX /* history window size */
10#define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
11#endif
12
Gao Xiangaf89bce2019-07-03 14:52:09 +080013#define LZ4_MAX_DISTANCE_PAGES (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
Gao Xiang0ffd71b2019-06-24 15:22:56 +080014#ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
15#define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32)
16#endif
Gao Xiang7fc45db2019-06-24 15:22:55 +080017
Gao Xiangd67aee72021-12-28 13:46:00 +080018struct z_erofs_lz4_decompress_ctx {
19 struct z_erofs_decompress_req *rq;
20 /* # of encoded, decoded pages */
21 unsigned int inpages, outpages;
22 /* decoded block total length (used for in-place decompression) */
23 unsigned int oend;
24};
25
Gao Xiangefb4fb02023-10-22 21:09:57 +080026static int z_erofs_load_lz4_config(struct super_block *sb,
27 struct erofs_super_block *dsb, void *data, int size)
Huang Jianan5d505382021-03-29 09:23:06 +080028{
Gao Xiang4fea63f2021-04-07 12:39:23 +080029 struct erofs_sb_info *sbi = EROFS_SB(sb);
Gao Xiangefb4fb02023-10-22 21:09:57 +080030 struct z_erofs_lz4_cfgs *lz4 = data;
Gao Xiang46249cd2021-03-29 09:23:07 +080031 u16 distance;
32
33 if (lz4) {
34 if (size < sizeof(struct z_erofs_lz4_cfgs)) {
35 erofs_err(sb, "invalid lz4 cfgs, size=%u", size);
36 return -EINVAL;
37 }
38 distance = le16_to_cpu(lz4->max_distance);
Gao Xiang4fea63f2021-04-07 12:39:23 +080039
40 sbi->lz4.max_pclusterblks = le16_to_cpu(lz4->max_pclusterblks);
41 if (!sbi->lz4.max_pclusterblks) {
42 sbi->lz4.max_pclusterblks = 1; /* reserved case */
43 } else if (sbi->lz4.max_pclusterblks >
Jingbo Xu3acea5f2023-03-13 21:53:08 +080044 erofs_blknr(sb, Z_EROFS_PCLUSTER_MAX_SIZE)) {
Gao Xiang4fea63f2021-04-07 12:39:23 +080045 erofs_err(sb, "too large lz4 pclusterblks %u",
46 sbi->lz4.max_pclusterblks);
47 return -EINVAL;
Gao Xiang4fea63f2021-04-07 12:39:23 +080048 }
Gao Xiang46249cd2021-03-29 09:23:07 +080049 } else {
Gao Xiang14373712021-03-29 18:00:12 +080050 distance = le16_to_cpu(dsb->u1.lz4_max_distance);
Gao Xiang4fea63f2021-04-07 12:39:23 +080051 sbi->lz4.max_pclusterblks = 1;
Gao Xiang46249cd2021-03-29 09:23:07 +080052 }
Huang Jianan5d505382021-03-29 09:23:06 +080053
Gao Xiang4fea63f2021-04-07 12:39:23 +080054 sbi->lz4.max_distance_pages = distance ?
Huang Jianan5d505382021-03-29 09:23:06 +080055 DIV_ROUND_UP(distance, PAGE_SIZE) + 1 :
56 LZ4_MAX_DISTANCE_PAGES;
Chunhai Guof36f3012024-04-02 04:00:36 -060057 return z_erofs_gbuf_growsize(sbi->lz4.max_pclusterblks);
Huang Jianan5d505382021-03-29 09:23:06 +080058}
59
Gao Xiang966edfb2021-10-11 05:31:44 +080060/*
61 * Fill all gaps with bounce pages if it's a sparse page list. Also check if
62 * all physical pages are consecutive, which can be seen for moderate CR.
63 */
Gao Xiangd67aee72021-12-28 13:46:00 +080064static int z_erofs_lz4_prepare_dstpages(struct z_erofs_lz4_decompress_ctx *ctx,
Gao Xiangeaa91722021-10-22 17:01:20 +080065 struct page **pagepool)
Gao Xiang7fc45db2019-06-24 15:22:55 +080066{
Gao Xiangd67aee72021-12-28 13:46:00 +080067 struct z_erofs_decompress_req *rq = ctx->rq;
Gao Xiang7fc45db2019-06-24 15:22:55 +080068 struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
Gao Xiangaf89bce2019-07-03 14:52:09 +080069 unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
70 BITS_PER_LONG)] = { 0 };
Huang Jianan5d505382021-03-29 09:23:06 +080071 unsigned int lz4_max_distance_pages =
72 EROFS_SB(rq->sb)->lz4.max_distance_pages;
Gao Xiang7fc45db2019-06-24 15:22:55 +080073 void *kaddr = NULL;
Gao Xiangaf89bce2019-07-03 14:52:09 +080074 unsigned int i, j, top;
Gao Xiang7fc45db2019-06-24 15:22:55 +080075
Gao Xiangaf89bce2019-07-03 14:52:09 +080076 top = 0;
Gao Xiangd67aee72021-12-28 13:46:00 +080077 for (i = j = 0; i < ctx->outpages; ++i, ++j) {
Gao Xiang7fc45db2019-06-24 15:22:55 +080078 struct page *const page = rq->out[i];
Gao Xiangaf89bce2019-07-03 14:52:09 +080079 struct page *victim;
Gao Xiang7fc45db2019-06-24 15:22:55 +080080
Huang Jianan5d505382021-03-29 09:23:06 +080081 if (j >= lz4_max_distance_pages)
Gao Xiangaf89bce2019-07-03 14:52:09 +080082 j = 0;
83
84 /* 'valid' bounced can only be tested after a complete round */
Gao Xiang267f2492022-07-15 23:42:03 +080085 if (!rq->fillgaps && test_bit(j, bounced)) {
Huang Jianan5d505382021-03-29 09:23:06 +080086 DBG_BUGON(i < lz4_max_distance_pages);
87 DBG_BUGON(top >= lz4_max_distance_pages);
88 availables[top++] = rq->out[i - lz4_max_distance_pages];
Gao Xiangaf89bce2019-07-03 14:52:09 +080089 }
Gao Xiang7fc45db2019-06-24 15:22:55 +080090
91 if (page) {
Gao Xiangaf89bce2019-07-03 14:52:09 +080092 __clear_bit(j, bounced);
Gao Xiang448b5a12022-07-08 18:10:01 +080093 if (!PageHighMem(page)) {
94 if (!i) {
95 kaddr = page_address(page);
96 continue;
97 }
98 if (kaddr &&
99 kaddr + PAGE_SIZE == page_address(page)) {
Gao Xiang7fc45db2019-06-24 15:22:55 +0800100 kaddr += PAGE_SIZE;
Gao Xiang448b5a12022-07-08 18:10:01 +0800101 continue;
102 }
Gao Xiang7fc45db2019-06-24 15:22:55 +0800103 }
Gao Xiang448b5a12022-07-08 18:10:01 +0800104 kaddr = NULL;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800105 continue;
106 }
107 kaddr = NULL;
Gao Xiangaf89bce2019-07-03 14:52:09 +0800108 __set_bit(j, bounced);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800109
Gao Xiangaf89bce2019-07-03 14:52:09 +0800110 if (top) {
111 victim = availables[--top];
112 get_page(victim);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800113 } else {
Chunhai Guo0f6273a2024-04-02 07:15:23 -0600114 victim = __erofs_allocpage(pagepool, rq->gfp, true);
Chunhai Guod9281662024-01-26 22:01:42 +0800115 if (!victim)
116 return -ENOMEM;
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800117 set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800118 }
Gao Xiangaf89bce2019-07-03 14:52:09 +0800119 rq->out[i] = victim;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800120 }
121 return kaddr ? 1 : 0;
122}
123
Gao Xiangd67aee72021-12-28 13:46:00 +0800124static void *z_erofs_lz4_handle_overlap(struct z_erofs_lz4_decompress_ctx *ctx,
Gao Xiang3c124662023-12-06 12:55:34 +0800125 void *inpage, void *out, unsigned int *inputmargin,
126 int *maptype, bool may_inplace)
Gao Xiang7fc45db2019-06-24 15:22:55 +0800127{
Gao Xiangd67aee72021-12-28 13:46:00 +0800128 struct z_erofs_decompress_req *rq = ctx->rq;
Gao Xiang3c124662023-12-06 12:55:34 +0800129 unsigned int omargin, total, i;
Gao Xiang598162d2021-04-07 12:39:26 +0800130 struct page **in;
131 void *src, *tmp;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800132
Gao Xiang598162d2021-04-07 12:39:26 +0800133 if (rq->inplace_io) {
Gao Xiangd67aee72021-12-28 13:46:00 +0800134 omargin = PAGE_ALIGN(ctx->oend) - ctx->oend;
Gao Xiangab749ba2021-12-28 13:46:02 +0800135 if (rq->partial_decoding || !may_inplace ||
Gao Xiangd67aee72021-12-28 13:46:00 +0800136 omargin < LZ4_DECOMPRESS_INPLACE_MARGIN(rq->inputsize))
Gao Xiang598162d2021-04-07 12:39:26 +0800137 goto docopy;
138
Gao Xiang3c124662023-12-06 12:55:34 +0800139 for (i = 0; i < ctx->inpages; ++i)
140 if (rq->out[ctx->outpages - ctx->inpages + i] !=
141 rq->in[i])
142 goto docopy;
143 kunmap_local(inpage);
144 *maptype = 3;
145 return out + ((ctx->outpages - ctx->inpages) << PAGE_SHIFT);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800146 }
Gao Xiang598162d2021-04-07 12:39:26 +0800147
Gao Xiangd67aee72021-12-28 13:46:00 +0800148 if (ctx->inpages <= 1) {
Gao Xiang598162d2021-04-07 12:39:26 +0800149 *maptype = 0;
150 return inpage;
151 }
Gao Xiang123ec242023-06-28 00:12:39 +0800152 kunmap_local(inpage);
Gao Xiangd67aee72021-12-28 13:46:00 +0800153 src = erofs_vm_map_ram(rq->in, ctx->inpages);
Gao Xiang598162d2021-04-07 12:39:26 +0800154 if (!src)
155 return ERR_PTR(-ENOMEM);
156 *maptype = 1;
157 return src;
158
159docopy:
160 /* Or copy compressed data which can be overlapped to per-CPU buffer */
161 in = rq->in;
Chunhai Guof36f3012024-04-02 04:00:36 -0600162 src = z_erofs_get_gbuf(ctx->inpages);
Gao Xiang598162d2021-04-07 12:39:26 +0800163 if (!src) {
164 DBG_BUGON(1);
Gao Xiang123ec242023-06-28 00:12:39 +0800165 kunmap_local(inpage);
Gao Xiang598162d2021-04-07 12:39:26 +0800166 return ERR_PTR(-EFAULT);
167 }
168
169 tmp = src;
170 total = rq->inputsize;
171 while (total) {
172 unsigned int page_copycnt =
173 min_t(unsigned int, total, PAGE_SIZE - *inputmargin);
174
175 if (!inpage)
Gao Xiang123ec242023-06-28 00:12:39 +0800176 inpage = kmap_local_page(*in);
Gao Xiang598162d2021-04-07 12:39:26 +0800177 memcpy(tmp, inpage + *inputmargin, page_copycnt);
Gao Xiang123ec242023-06-28 00:12:39 +0800178 kunmap_local(inpage);
Gao Xiang598162d2021-04-07 12:39:26 +0800179 inpage = NULL;
180 tmp += page_copycnt;
181 total -= page_copycnt;
182 ++in;
183 *inputmargin = 0;
184 }
185 *maptype = 2;
186 return src;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800187}
188
Gao Xiang10e5f6e2021-12-28 13:46:01 +0800189/*
190 * Get the exact inputsize with zero_padding feature.
191 * - For LZ4, it should work if zero_padding feature is on (5.3+);
192 * - For MicroLZMA, it'd be enabled all the time.
193 */
194int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
195 unsigned int padbufsize)
196{
197 const char *padend;
198
199 padend = memchr_inv(padbuf, 0, padbufsize);
200 if (!padend)
201 return -EFSCORRUPTED;
202 rq->inputsize -= padend - padbuf;
203 rq->pageofs_in += padend - padbuf;
204 return 0;
205}
206
Gao Xiangd67aee72021-12-28 13:46:00 +0800207static int z_erofs_lz4_decompress_mem(struct z_erofs_lz4_decompress_ctx *ctx,
Gao Xiang3c124662023-12-06 12:55:34 +0800208 u8 *dst)
Gao Xiang7fc45db2019-06-24 15:22:55 +0800209{
Gao Xiangd67aee72021-12-28 13:46:00 +0800210 struct z_erofs_decompress_req *rq = ctx->rq;
Gao Xiangab749ba2021-12-28 13:46:02 +0800211 bool support_0padding = false, may_inplace = false;
Gao Xiang598162d2021-04-07 12:39:26 +0800212 unsigned int inputmargin;
Gao Xiang3c124662023-12-06 12:55:34 +0800213 u8 *out, *headpage, *src;
Gao Xiang598162d2021-04-07 12:39:26 +0800214 int ret, maptype;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800215
Gao Xiang598162d2021-04-07 12:39:26 +0800216 DBG_BUGON(*rq->in == NULL);
Gao Xiang123ec242023-06-28 00:12:39 +0800217 headpage = kmap_local_page(*rq->in);
Gao Xiang0ffd71b2019-06-24 15:22:56 +0800218
Gao Xiang10e5f6e2021-12-28 13:46:01 +0800219 /* LZ4 decompression inplace is only safe if zero_padding is enabled */
Huang Jianan7e508f22021-11-13 00:09:33 +0800220 if (erofs_sb_has_zero_padding(EROFS_SB(rq->sb))) {
Gao Xiang0ffd71b2019-06-24 15:22:56 +0800221 support_0padding = true;
Gao Xiang10e5f6e2021-12-28 13:46:01 +0800222 ret = z_erofs_fixup_insize(rq, headpage + rq->pageofs_in,
223 min_t(unsigned int, rq->inputsize,
Jingbo Xu3acea5f2023-03-13 21:53:08 +0800224 rq->sb->s_blocksize - rq->pageofs_in));
Gao Xiang10e5f6e2021-12-28 13:46:01 +0800225 if (ret) {
Gao Xiang123ec242023-06-28 00:12:39 +0800226 kunmap_local(headpage);
Gao Xiang10e5f6e2021-12-28 13:46:01 +0800227 return ret;
Gao Xiang0ffd71b2019-06-24 15:22:56 +0800228 }
Gao Xiangab749ba2021-12-28 13:46:02 +0800229 may_inplace = !((rq->pageofs_in + rq->inputsize) &
Jingbo Xu3acea5f2023-03-13 21:53:08 +0800230 (rq->sb->s_blocksize - 1));
Gao Xiang0ffd71b2019-06-24 15:22:56 +0800231 }
Gao Xiang7fc45db2019-06-24 15:22:55 +0800232
Gao Xiang10e5f6e2021-12-28 13:46:01 +0800233 inputmargin = rq->pageofs_in;
Gao Xiang3c124662023-12-06 12:55:34 +0800234 src = z_erofs_lz4_handle_overlap(ctx, headpage, dst, &inputmargin,
Gao Xiangab749ba2021-12-28 13:46:02 +0800235 &maptype, may_inplace);
Gao Xiang598162d2021-04-07 12:39:26 +0800236 if (IS_ERR(src))
237 return PTR_ERR(src);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800238
Gao Xiang3c124662023-12-06 12:55:34 +0800239 out = dst + rq->pageofs_out;
Gao Xiangaf1038ab2020-02-26 16:10:07 +0800240 /* legacy format could compress extra data in a pcluster. */
241 if (rq->partial_decoding || !support_0padding)
242 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
Gao Xiang598162d2021-04-07 12:39:26 +0800243 rq->inputsize, rq->outputsize, rq->outputsize);
Gao Xiangaf1038ab2020-02-26 16:10:07 +0800244 else
245 ret = LZ4_decompress_safe(src + inputmargin, out,
Gao Xiang598162d2021-04-07 12:39:26 +0800246 rq->inputsize, rq->outputsize);
Gao Xiangaf1038ab2020-02-26 16:10:07 +0800247
Gao Xiangaa99a762020-02-26 16:10:08 +0800248 if (ret != rq->outputsize) {
249 erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
Gao Xiang598162d2021-04-07 12:39:26 +0800250 ret, rq->inputsize, inputmargin, rq->outputsize);
Gao Xiangaa99a762020-02-26 16:10:08 +0800251 if (ret >= 0)
252 memset(out + ret, 0, rq->outputsize - ret);
Gao Xiang496530c2023-12-27 23:19:03 +0800253 ret = -EFSCORRUPTED;
Yue Hu5b6e7e12021-10-14 14:57:44 +0800254 } else {
255 ret = 0;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800256 }
257
Gao Xiang598162d2021-04-07 12:39:26 +0800258 if (maptype == 0) {
Gao Xiang123ec242023-06-28 00:12:39 +0800259 kunmap_local(headpage);
Gao Xiang598162d2021-04-07 12:39:26 +0800260 } else if (maptype == 1) {
Gao Xiangd67aee72021-12-28 13:46:00 +0800261 vm_unmap_ram(src, ctx->inpages);
Gao Xiang598162d2021-04-07 12:39:26 +0800262 } else if (maptype == 2) {
Chunhai Guof36f3012024-04-02 04:00:36 -0600263 z_erofs_put_gbuf(src);
Gao Xiang3c124662023-12-06 12:55:34 +0800264 } else if (maptype != 3) {
Gao Xiang598162d2021-04-07 12:39:26 +0800265 DBG_BUGON(1);
266 return -EFAULT;
267 }
Gao Xiang7fc45db2019-06-24 15:22:55 +0800268 return ret;
269}
270
Gao Xiang966edfb2021-10-11 05:31:44 +0800271static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq,
Gao Xiangeaa91722021-10-22 17:01:20 +0800272 struct page **pagepool)
Gao Xiang7fc45db2019-06-24 15:22:55 +0800273{
Gao Xiangd67aee72021-12-28 13:46:00 +0800274 struct z_erofs_lz4_decompress_ctx ctx;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800275 unsigned int dst_maptype;
276 void *dst;
Gao Xiang598162d2021-04-07 12:39:26 +0800277 int ret;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800278
Gao Xiangd67aee72021-12-28 13:46:00 +0800279 ctx.rq = rq;
280 ctx.oend = rq->pageofs_out + rq->outputsize;
281 ctx.outpages = PAGE_ALIGN(ctx.oend) >> PAGE_SHIFT;
282 ctx.inpages = PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT;
283
Yue Hu5b6e7e12021-10-14 14:57:44 +0800284 /* one optimized fast path only for non bigpcluster cases yet */
Gao Xiangd67aee72021-12-28 13:46:00 +0800285 if (ctx.inpages == 1 && ctx.outpages == 1 && !rq->inplace_io) {
Yue Hu5b6e7e12021-10-14 14:57:44 +0800286 DBG_BUGON(!*rq->out);
Gao Xiang123ec242023-06-28 00:12:39 +0800287 dst = kmap_local_page(*rq->out);
Yue Hu5b6e7e12021-10-14 14:57:44 +0800288 dst_maptype = 0;
289 goto dstmap_out;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800290 }
291
Gao Xiang598162d2021-04-07 12:39:26 +0800292 /* general decoding path which can be used for all cases */
Gao Xiangd67aee72021-12-28 13:46:00 +0800293 ret = z_erofs_lz4_prepare_dstpages(&ctx, pagepool);
294 if (ret < 0) {
Gao Xiang7fc45db2019-06-24 15:22:55 +0800295 return ret;
Gao Xiangd67aee72021-12-28 13:46:00 +0800296 } else if (ret > 0) {
Gao Xiang7fc45db2019-06-24 15:22:55 +0800297 dst = page_address(*rq->out);
298 dst_maptype = 1;
Gao Xiangd67aee72021-12-28 13:46:00 +0800299 } else {
300 dst = erofs_vm_map_ram(rq->out, ctx.outpages);
301 if (!dst)
302 return -ENOMEM;
303 dst_maptype = 2;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800304 }
305
Gao Xiang7fc45db2019-06-24 15:22:55 +0800306dstmap_out:
Gao Xiang3c124662023-12-06 12:55:34 +0800307 ret = z_erofs_lz4_decompress_mem(&ctx, dst);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800308 if (!dst_maptype)
Gao Xiang123ec242023-06-28 00:12:39 +0800309 kunmap_local(dst);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800310 else if (dst_maptype == 2)
Gao Xiangd67aee72021-12-28 13:46:00 +0800311 vm_unmap_ram(dst, ctx.outpages);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800312 return ret;
313}
314
Yue Hufdffc092022-09-23 10:11:21 +0800315static int z_erofs_transform_plain(struct z_erofs_decompress_req *rq,
316 struct page **pagepool)
Gao Xiang7fc45db2019-06-24 15:22:55 +0800317{
Gao Xiang1ca01522023-12-06 17:10:56 +0800318 const unsigned int nrpages_in =
319 PAGE_ALIGN(rq->pageofs_in + rq->inputsize) >> PAGE_SHIFT;
320 const unsigned int nrpages_out =
Gao Xiang7fc45db2019-06-24 15:22:55 +0800321 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
Gao Xiang1ca01522023-12-06 17:10:56 +0800322 const unsigned int bs = rq->sb->s_blocksize;
323 unsigned int cur = 0, ni = 0, no, pi, po, insz, cnt;
324 u8 *kin;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800325
Gao Xiang893e5e92024-03-04 11:53:39 +0800326 if (rq->outputsize > rq->inputsize)
327 return -EOPNOTSUPP;
Gao Xiang1ca01522023-12-06 17:10:56 +0800328 if (rq->alg == Z_EROFS_COMPRESSION_INTERLACED) {
329 cur = bs - (rq->pageofs_out & (bs - 1));
330 pi = (rq->pageofs_in + rq->inputsize - cur) & ~PAGE_MASK;
331 cur = min(cur, rq->outputsize);
332 if (cur && rq->out[0]) {
333 kin = kmap_local_page(rq->in[nrpages_in - 1]);
334 if (rq->out[0] == rq->in[nrpages_in - 1]) {
335 memmove(kin + rq->pageofs_out, kin + pi, cur);
336 flush_dcache_page(rq->out[0]);
337 } else {
338 memcpy_to_page(rq->out[0], rq->pageofs_out,
339 kin + pi, cur);
340 }
341 kunmap_local(kin);
Gao Xiang4d202432020-01-07 10:25:46 +0800342 }
Gao Xiang1ca01522023-12-06 17:10:56 +0800343 rq->outputsize -= cur;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800344 }
Gao Xiang1ca01522023-12-06 17:10:56 +0800345
346 for (; rq->outputsize; rq->pageofs_in = 0, cur += PAGE_SIZE, ni++) {
347 insz = min(PAGE_SIZE - rq->pageofs_in, rq->outputsize);
348 rq->outputsize -= insz;
349 if (!rq->in[ni])
350 continue;
351 kin = kmap_local_page(rq->in[ni]);
352 pi = 0;
353 do {
354 no = (rq->pageofs_out + cur + pi) >> PAGE_SHIFT;
355 po = (rq->pageofs_out + cur + pi) & ~PAGE_MASK;
356 DBG_BUGON(no >= nrpages_out);
357 cnt = min(insz - pi, PAGE_SIZE - po);
358 if (rq->out[no] == rq->in[ni]) {
359 memmove(kin + po,
360 kin + rq->pageofs_in + pi, cnt);
361 flush_dcache_page(rq->out[no]);
362 } else if (rq->out[no]) {
363 memcpy_to_page(rq->out[no], po,
364 kin + rq->pageofs_in + pi, cnt);
365 }
366 pi += cnt;
367 } while (pi < insz);
368 kunmap_local(kin);
369 }
370 DBG_BUGON(ni > nrpages_in);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800371 return 0;
372}
373
Yue Hu597e2952023-04-26 16:44:49 +0800374const struct z_erofs_decompressor erofs_decompressors[] = {
Gao Xiang966edfb2021-10-11 05:31:44 +0800375 [Z_EROFS_COMPRESSION_SHIFTED] = {
Yue Hufdffc092022-09-23 10:11:21 +0800376 .decompress = z_erofs_transform_plain,
Gao Xiang966edfb2021-10-11 05:31:44 +0800377 .name = "shifted"
378 },
Yue Hufdffc092022-09-23 10:11:21 +0800379 [Z_EROFS_COMPRESSION_INTERLACED] = {
380 .decompress = z_erofs_transform_plain,
381 .name = "interlaced"
382 },
Gao Xiang966edfb2021-10-11 05:31:44 +0800383 [Z_EROFS_COMPRESSION_LZ4] = {
Gao Xiangefb4fb02023-10-22 21:09:57 +0800384 .config = z_erofs_load_lz4_config,
Gao Xiang966edfb2021-10-11 05:31:44 +0800385 .decompress = z_erofs_lz4_decompress,
386 .name = "lz4"
387 },
Gao Xiang622cead2021-10-11 05:31:45 +0800388#ifdef CONFIG_EROFS_FS_ZIP_LZMA
389 [Z_EROFS_COMPRESSION_LZMA] = {
Gao Xiangefb4fb02023-10-22 21:09:57 +0800390 .config = z_erofs_load_lzma_config,
Gao Xiang622cead2021-10-11 05:31:45 +0800391 .decompress = z_erofs_lzma_decompress,
392 .name = "lzma"
393 },
394#endif
Gao Xiangffa09b3bd2023-08-10 23:48:59 +0800395#ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
396 [Z_EROFS_COMPRESSION_DEFLATE] = {
Gao Xiangefb4fb02023-10-22 21:09:57 +0800397 .config = z_erofs_load_deflate_config,
Gao Xiangffa09b3bd2023-08-10 23:48:59 +0800398 .decompress = z_erofs_deflate_decompress,
399 .name = "deflate"
400 },
401#endif
Gao Xiang7c35de42024-05-09 07:44:53 +0800402#ifdef CONFIG_EROFS_FS_ZIP_ZSTD
403 [Z_EROFS_COMPRESSION_ZSTD] = {
404 .config = z_erofs_load_zstd_config,
405 .decompress = z_erofs_zstd_decompress,
406 .name = "zstd"
407 },
408#endif
Gao Xiang966edfb2021-10-11 05:31:44 +0800409};
Gao Xiangefb4fb02023-10-22 21:09:57 +0800410
411int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
412{
413 struct erofs_sb_info *sbi = EROFS_SB(sb);
414 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
415 unsigned int algs, alg;
416 erofs_off_t offset;
417 int size, ret = 0;
418
419 if (!erofs_sb_has_compr_cfgs(sbi)) {
Gao Xiang118a8cf2024-01-13 23:06:02 +0800420 sbi->available_compr_algs = 1 << Z_EROFS_COMPRESSION_LZ4;
Gao Xiangefb4fb02023-10-22 21:09:57 +0800421 return z_erofs_load_lz4_config(sb, dsb, NULL, 0);
422 }
423
424 sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
425 if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
426 erofs_err(sb, "unidentified algorithms %x, please upgrade kernel",
427 sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
428 return -EOPNOTSUPP;
429 }
430
431 erofs_init_metabuf(&buf, sb);
432 offset = EROFS_SUPER_OFFSET + sbi->sb_size;
433 alg = 0;
434 for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
435 void *data;
436
437 if (!(algs & 1))
438 continue;
439
440 data = erofs_read_metadata(sb, &buf, &offset, &size);
441 if (IS_ERR(data)) {
442 ret = PTR_ERR(data);
443 break;
444 }
445
446 if (alg >= ARRAY_SIZE(erofs_decompressors) ||
447 !erofs_decompressors[alg].config) {
448 erofs_err(sb, "algorithm %d isn't enabled on this kernel",
449 alg);
450 ret = -EOPNOTSUPP;
451 } else {
452 ret = erofs_decompressors[alg].config(sb,
453 dsb, data, size);
454 }
455
456 kfree(data);
457 if (ret)
458 break;
459 }
460 erofs_put_metabuf(&buf);
461 return ret;
462}