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/ 547e4937aSGao Xiang */ 647e4937aSGao Xiang #include "compress.h" 747e4937aSGao Xiang #include <linux/module.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 19*d67aee76SGao Xiang struct z_erofs_lz4_decompress_ctx { 20*d67aee76SGao Xiang struct z_erofs_decompress_req *rq; 21*d67aee76SGao Xiang /* # of encoded, decoded pages */ 22*d67aee76SGao Xiang unsigned int inpages, outpages; 23*d67aee76SGao Xiang /* decoded block total length (used for in-place decompression) */ 24*d67aee76SGao Xiang unsigned int oend; 25*d67aee76SGao Xiang }; 26*d67aee76SGao Xiang 275d50538fSHuang Jianan int z_erofs_load_lz4_config(struct super_block *sb, 2846249cdeSGao Xiang struct erofs_super_block *dsb, 2946249cdeSGao Xiang struct z_erofs_lz4_cfgs *lz4, int size) 305d50538fSHuang Jianan { 314fea63f7SGao Xiang struct erofs_sb_info *sbi = EROFS_SB(sb); 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 > 454fea63f7SGao Xiang Z_EROFS_PCLUSTER_MAX_SIZE / EROFS_BLKSIZ) { 464fea63f7SGao Xiang erofs_err(sb, "too large lz4 pclusterblks %u", 474fea63f7SGao Xiang sbi->lz4.max_pclusterblks); 484fea63f7SGao Xiang return -EINVAL; 494fea63f7SGao Xiang } else if (sbi->lz4.max_pclusterblks >= 2) { 504fea63f7SGao Xiang erofs_info(sb, "EXPERIMENTAL big pcluster feature in use. Use at your own risk!"); 514fea63f7SGao Xiang } 5246249cdeSGao Xiang } else { 5314373711SGao Xiang distance = le16_to_cpu(dsb->u1.lz4_max_distance); 544fea63f7SGao Xiang sbi->lz4.max_pclusterblks = 1; 5546249cdeSGao Xiang } 565d50538fSHuang Jianan 574fea63f7SGao Xiang sbi->lz4.max_distance_pages = distance ? 585d50538fSHuang Jianan DIV_ROUND_UP(distance, PAGE_SIZE) + 1 : 595d50538fSHuang Jianan LZ4_MAX_DISTANCE_PAGES; 604fea63f7SGao Xiang return erofs_pcpubuf_growsize(sbi->lz4.max_pclusterblks); 615d50538fSHuang Jianan } 625d50538fSHuang Jianan 63966edfb0SGao Xiang /* 64966edfb0SGao Xiang * Fill all gaps with bounce pages if it's a sparse page list. Also check if 65966edfb0SGao Xiang * all physical pages are consecutive, which can be seen for moderate CR. 66966edfb0SGao Xiang */ 67*d67aee76SGao Xiang static int z_erofs_lz4_prepare_dstpages(struct z_erofs_lz4_decompress_ctx *ctx, 68eaa9172aSGao Xiang struct page **pagepool) 6947e4937aSGao Xiang { 70*d67aee76SGao Xiang struct z_erofs_decompress_req *rq = ctx->rq; 7147e4937aSGao Xiang struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL }; 7247e4937aSGao Xiang unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES, 7347e4937aSGao Xiang BITS_PER_LONG)] = { 0 }; 745d50538fSHuang Jianan unsigned int lz4_max_distance_pages = 755d50538fSHuang Jianan EROFS_SB(rq->sb)->lz4.max_distance_pages; 7647e4937aSGao Xiang void *kaddr = NULL; 7747e4937aSGao Xiang unsigned int i, j, top; 7847e4937aSGao Xiang 7947e4937aSGao Xiang top = 0; 80*d67aee76SGao Xiang for (i = j = 0; i < ctx->outpages; ++i, ++j) { 8147e4937aSGao Xiang struct page *const page = rq->out[i]; 8247e4937aSGao Xiang struct page *victim; 8347e4937aSGao Xiang 845d50538fSHuang Jianan if (j >= lz4_max_distance_pages) 8547e4937aSGao Xiang j = 0; 8647e4937aSGao Xiang 8747e4937aSGao Xiang /* 'valid' bounced can only be tested after a complete round */ 8847e4937aSGao Xiang if (test_bit(j, bounced)) { 895d50538fSHuang Jianan DBG_BUGON(i < lz4_max_distance_pages); 905d50538fSHuang Jianan DBG_BUGON(top >= lz4_max_distance_pages); 915d50538fSHuang Jianan availables[top++] = rq->out[i - lz4_max_distance_pages]; 9247e4937aSGao Xiang } 9347e4937aSGao Xiang 9447e4937aSGao Xiang if (page) { 9547e4937aSGao Xiang __clear_bit(j, bounced); 9647e4937aSGao Xiang if (kaddr) { 9747e4937aSGao Xiang if (kaddr + PAGE_SIZE == page_address(page)) 9847e4937aSGao Xiang kaddr += PAGE_SIZE; 9947e4937aSGao Xiang else 10047e4937aSGao Xiang kaddr = NULL; 10147e4937aSGao Xiang } else if (!i) { 10247e4937aSGao Xiang kaddr = page_address(page); 10347e4937aSGao Xiang } 10447e4937aSGao Xiang continue; 10547e4937aSGao Xiang } 10647e4937aSGao Xiang kaddr = NULL; 10747e4937aSGao Xiang __set_bit(j, bounced); 10847e4937aSGao Xiang 10947e4937aSGao Xiang if (top) { 11047e4937aSGao Xiang victim = availables[--top]; 11147e4937aSGao Xiang get_page(victim); 11247e4937aSGao Xiang } else { 113b4892fa3SHuang Jianan victim = erofs_allocpage(pagepool, 114b4892fa3SHuang Jianan GFP_KERNEL | __GFP_NOFAIL); 1156aaa7b06SGao Xiang set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE); 11647e4937aSGao Xiang } 11747e4937aSGao Xiang rq->out[i] = victim; 11847e4937aSGao Xiang } 11947e4937aSGao Xiang return kaddr ? 1 : 0; 12047e4937aSGao Xiang } 12147e4937aSGao Xiang 122*d67aee76SGao Xiang static void *z_erofs_lz4_handle_overlap(struct z_erofs_lz4_decompress_ctx *ctx, 123598162d0SGao Xiang void *inpage, unsigned int *inputmargin, int *maptype, 124598162d0SGao Xiang bool support_0padding) 12547e4937aSGao Xiang { 126*d67aee76SGao Xiang struct z_erofs_decompress_req *rq = ctx->rq; 127*d67aee76SGao Xiang unsigned int omargin, total, i, j; 128598162d0SGao Xiang struct page **in; 129598162d0SGao Xiang void *src, *tmp; 13047e4937aSGao Xiang 131598162d0SGao Xiang if (rq->inplace_io) { 132*d67aee76SGao Xiang omargin = PAGE_ALIGN(ctx->oend) - ctx->oend; 133598162d0SGao Xiang if (rq->partial_decoding || !support_0padding || 134*d67aee76SGao Xiang omargin < LZ4_DECOMPRESS_INPLACE_MARGIN(rq->inputsize)) 135598162d0SGao Xiang goto docopy; 136598162d0SGao Xiang 137*d67aee76SGao Xiang for (i = 0; i < ctx->inpages; ++i) { 138598162d0SGao Xiang DBG_BUGON(rq->in[i] == NULL); 139*d67aee76SGao Xiang for (j = 0; j < ctx->outpages - ctx->inpages + i; ++j) 140598162d0SGao Xiang if (rq->out[j] == rq->in[i]) 141598162d0SGao Xiang goto docopy; 14247e4937aSGao Xiang } 143598162d0SGao Xiang } 144598162d0SGao Xiang 145*d67aee76SGao Xiang if (ctx->inpages <= 1) { 146598162d0SGao Xiang *maptype = 0; 147598162d0SGao Xiang return inpage; 148598162d0SGao Xiang } 149598162d0SGao Xiang kunmap_atomic(inpage); 150598162d0SGao Xiang might_sleep(); 151*d67aee76SGao Xiang src = erofs_vm_map_ram(rq->in, ctx->inpages); 152598162d0SGao Xiang if (!src) 153598162d0SGao Xiang return ERR_PTR(-ENOMEM); 154598162d0SGao Xiang *maptype = 1; 155598162d0SGao Xiang return src; 156598162d0SGao Xiang 157598162d0SGao Xiang docopy: 158598162d0SGao Xiang /* Or copy compressed data which can be overlapped to per-CPU buffer */ 159598162d0SGao Xiang in = rq->in; 160*d67aee76SGao Xiang src = erofs_get_pcpubuf(ctx->inpages); 161598162d0SGao Xiang if (!src) { 162598162d0SGao Xiang DBG_BUGON(1); 163598162d0SGao Xiang kunmap_atomic(inpage); 164598162d0SGao Xiang return ERR_PTR(-EFAULT); 165598162d0SGao Xiang } 166598162d0SGao Xiang 167598162d0SGao Xiang tmp = src; 168598162d0SGao Xiang total = rq->inputsize; 169598162d0SGao Xiang while (total) { 170598162d0SGao Xiang unsigned int page_copycnt = 171598162d0SGao Xiang min_t(unsigned int, total, PAGE_SIZE - *inputmargin); 172598162d0SGao Xiang 173598162d0SGao Xiang if (!inpage) 174598162d0SGao Xiang inpage = kmap_atomic(*in); 175598162d0SGao Xiang memcpy(tmp, inpage + *inputmargin, page_copycnt); 176598162d0SGao Xiang kunmap_atomic(inpage); 177598162d0SGao Xiang inpage = NULL; 178598162d0SGao Xiang tmp += page_copycnt; 179598162d0SGao Xiang total -= page_copycnt; 180598162d0SGao Xiang ++in; 181598162d0SGao Xiang *inputmargin = 0; 182598162d0SGao Xiang } 183598162d0SGao Xiang *maptype = 2; 184598162d0SGao Xiang return src; 18547e4937aSGao Xiang } 18647e4937aSGao Xiang 187*d67aee76SGao Xiang static int z_erofs_lz4_decompress_mem(struct z_erofs_lz4_decompress_ctx *ctx, 188966edfb0SGao Xiang u8 *out) 18947e4937aSGao Xiang { 190*d67aee76SGao Xiang struct z_erofs_decompress_req *rq = ctx->rq; 191598162d0SGao Xiang unsigned int inputmargin; 192598162d0SGao Xiang u8 *headpage, *src; 193598162d0SGao Xiang bool support_0padding; 194598162d0SGao Xiang int ret, maptype; 19547e4937aSGao Xiang 196598162d0SGao Xiang DBG_BUGON(*rq->in == NULL); 197598162d0SGao Xiang headpage = kmap_atomic(*rq->in); 19847e4937aSGao Xiang inputmargin = 0; 19947e4937aSGao Xiang support_0padding = false; 20047e4937aSGao Xiang 2017e508f2cSHuang Jianan /* decompression inplace is only safe when zero_padding is enabled */ 2027e508f2cSHuang Jianan if (erofs_sb_has_zero_padding(EROFS_SB(rq->sb))) { 20347e4937aSGao Xiang support_0padding = true; 20447e4937aSGao Xiang 205598162d0SGao Xiang while (!headpage[inputmargin & ~PAGE_MASK]) 20647e4937aSGao Xiang if (!(++inputmargin & ~PAGE_MASK)) 20747e4937aSGao Xiang break; 20847e4937aSGao Xiang 20947e4937aSGao Xiang if (inputmargin >= rq->inputsize) { 210598162d0SGao Xiang kunmap_atomic(headpage); 21147e4937aSGao Xiang return -EIO; 21247e4937aSGao Xiang } 21347e4937aSGao Xiang } 21447e4937aSGao Xiang 215598162d0SGao Xiang rq->inputsize -= inputmargin; 216*d67aee76SGao Xiang src = z_erofs_lz4_handle_overlap(ctx, headpage, &inputmargin, 217966edfb0SGao Xiang &maptype, support_0padding); 218598162d0SGao Xiang if (IS_ERR(src)) 219598162d0SGao Xiang return PTR_ERR(src); 22047e4937aSGao Xiang 221af1038abSGao Xiang /* legacy format could compress extra data in a pcluster. */ 222af1038abSGao Xiang if (rq->partial_decoding || !support_0padding) 22347e4937aSGao Xiang ret = LZ4_decompress_safe_partial(src + inputmargin, out, 224598162d0SGao Xiang rq->inputsize, rq->outputsize, rq->outputsize); 225af1038abSGao Xiang else 226af1038abSGao Xiang ret = LZ4_decompress_safe(src + inputmargin, out, 227598162d0SGao Xiang rq->inputsize, rq->outputsize); 228af1038abSGao Xiang 229aa99a76bSGao Xiang if (ret != rq->outputsize) { 230aa99a76bSGao Xiang erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]", 231598162d0SGao Xiang ret, rq->inputsize, inputmargin, rq->outputsize); 232aa99a76bSGao Xiang 23347e4937aSGao Xiang print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET, 234598162d0SGao Xiang 16, 1, src + inputmargin, rq->inputsize, true); 23547e4937aSGao Xiang print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET, 23647e4937aSGao Xiang 16, 1, out, rq->outputsize, true); 237aa99a76bSGao Xiang 238aa99a76bSGao Xiang if (ret >= 0) 239aa99a76bSGao Xiang memset(out + ret, 0, rq->outputsize - ret); 24047e4937aSGao Xiang ret = -EIO; 2415b6e7e12SYue Hu } else { 2425b6e7e12SYue Hu ret = 0; 24347e4937aSGao Xiang } 24447e4937aSGao Xiang 245598162d0SGao Xiang if (maptype == 0) { 246*d67aee76SGao Xiang kunmap_atomic(headpage); 247598162d0SGao Xiang } else if (maptype == 1) { 248*d67aee76SGao Xiang vm_unmap_ram(src, ctx->inpages); 249598162d0SGao Xiang } else if (maptype == 2) { 250598162d0SGao Xiang erofs_put_pcpubuf(src); 251598162d0SGao Xiang } else { 252598162d0SGao Xiang DBG_BUGON(1); 253598162d0SGao Xiang return -EFAULT; 254598162d0SGao Xiang } 25547e4937aSGao Xiang return ret; 25647e4937aSGao Xiang } 25747e4937aSGao Xiang 258966edfb0SGao Xiang static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, 259eaa9172aSGao Xiang struct page **pagepool) 26047e4937aSGao Xiang { 261*d67aee76SGao Xiang struct z_erofs_lz4_decompress_ctx ctx; 26247e4937aSGao Xiang unsigned int dst_maptype; 26347e4937aSGao Xiang void *dst; 264598162d0SGao Xiang int ret; 26547e4937aSGao Xiang 266*d67aee76SGao Xiang ctx.rq = rq; 267*d67aee76SGao Xiang ctx.oend = rq->pageofs_out + rq->outputsize; 268*d67aee76SGao Xiang ctx.outpages = PAGE_ALIGN(ctx.oend) >> PAGE_SHIFT; 269*d67aee76SGao Xiang ctx.inpages = PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT; 270*d67aee76SGao Xiang 2715b6e7e12SYue Hu /* one optimized fast path only for non bigpcluster cases yet */ 272*d67aee76SGao Xiang if (ctx.inpages == 1 && ctx.outpages == 1 && !rq->inplace_io) { 27347e4937aSGao Xiang DBG_BUGON(!*rq->out); 27447e4937aSGao Xiang dst = kmap_atomic(*rq->out); 27547e4937aSGao Xiang dst_maptype = 0; 27647e4937aSGao Xiang goto dstmap_out; 27747e4937aSGao Xiang } 27847e4937aSGao Xiang 279598162d0SGao Xiang /* general decoding path which can be used for all cases */ 280*d67aee76SGao Xiang ret = z_erofs_lz4_prepare_dstpages(&ctx, pagepool); 281*d67aee76SGao Xiang if (ret < 0) { 28247e4937aSGao Xiang return ret; 283*d67aee76SGao Xiang } else if (ret > 0) { 28447e4937aSGao Xiang dst = page_address(*rq->out); 28547e4937aSGao Xiang dst_maptype = 1; 286*d67aee76SGao Xiang } else { 287*d67aee76SGao Xiang dst = erofs_vm_map_ram(rq->out, ctx.outpages); 28847e4937aSGao Xiang if (!dst) 28947e4937aSGao Xiang return -ENOMEM; 29047e4937aSGao Xiang dst_maptype = 2; 291*d67aee76SGao Xiang } 29247e4937aSGao Xiang 29347e4937aSGao Xiang dstmap_out: 294*d67aee76SGao Xiang ret = z_erofs_lz4_decompress_mem(&ctx, dst + rq->pageofs_out); 29547e4937aSGao Xiang if (!dst_maptype) 29647e4937aSGao Xiang kunmap_atomic(dst); 29747e4937aSGao Xiang else if (dst_maptype == 2) 298*d67aee76SGao Xiang vm_unmap_ram(dst, ctx.outpages); 29947e4937aSGao Xiang return ret; 30047e4937aSGao Xiang } 30147e4937aSGao Xiang 302966edfb0SGao Xiang static int z_erofs_shifted_transform(struct z_erofs_decompress_req *rq, 303eaa9172aSGao Xiang struct page **pagepool) 30447e4937aSGao Xiang { 30547e4937aSGao Xiang const unsigned int nrpages_out = 30647e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; 30747e4937aSGao Xiang const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out; 30847e4937aSGao Xiang unsigned char *src, *dst; 30947e4937aSGao Xiang 31047e4937aSGao Xiang if (nrpages_out > 2) { 31147e4937aSGao Xiang DBG_BUGON(1); 31247e4937aSGao Xiang return -EIO; 31347e4937aSGao Xiang } 31447e4937aSGao Xiang 31547e4937aSGao Xiang if (rq->out[0] == *rq->in) { 31647e4937aSGao Xiang DBG_BUGON(nrpages_out != 1); 31747e4937aSGao Xiang return 0; 31847e4937aSGao Xiang } 31947e4937aSGao Xiang 32047e4937aSGao Xiang src = kmap_atomic(*rq->in); 3214d202437SGao Xiang if (rq->out[0]) { 32247e4937aSGao Xiang dst = kmap_atomic(rq->out[0]); 32347e4937aSGao Xiang memcpy(dst + rq->pageofs_out, src, righthalf); 3244d202437SGao Xiang kunmap_atomic(dst); 32547e4937aSGao Xiang } 32647e4937aSGao Xiang 3274d202437SGao Xiang if (nrpages_out == 2) { 3284d202437SGao Xiang DBG_BUGON(!rq->out[1]); 32947e4937aSGao Xiang if (rq->out[1] == *rq->in) { 33047e4937aSGao Xiang memmove(src, src + righthalf, rq->pageofs_out); 3314d202437SGao Xiang } else { 33247e4937aSGao Xiang dst = kmap_atomic(rq->out[1]); 33347e4937aSGao Xiang memcpy(dst, src + righthalf, rq->pageofs_out); 33447e4937aSGao Xiang kunmap_atomic(dst); 3354d202437SGao Xiang } 3364d202437SGao Xiang } 33747e4937aSGao Xiang kunmap_atomic(src); 33847e4937aSGao Xiang return 0; 33947e4937aSGao Xiang } 34047e4937aSGao Xiang 341966edfb0SGao Xiang static struct z_erofs_decompressor decompressors[] = { 342966edfb0SGao Xiang [Z_EROFS_COMPRESSION_SHIFTED] = { 343966edfb0SGao Xiang .decompress = z_erofs_shifted_transform, 344966edfb0SGao Xiang .name = "shifted" 345966edfb0SGao Xiang }, 346966edfb0SGao Xiang [Z_EROFS_COMPRESSION_LZ4] = { 347966edfb0SGao Xiang .decompress = z_erofs_lz4_decompress, 348966edfb0SGao Xiang .name = "lz4" 349966edfb0SGao Xiang }, 350622ceaddSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP_LZMA 351622ceaddSGao Xiang [Z_EROFS_COMPRESSION_LZMA] = { 352622ceaddSGao Xiang .decompress = z_erofs_lzma_decompress, 353622ceaddSGao Xiang .name = "lzma" 354622ceaddSGao Xiang }, 355622ceaddSGao Xiang #endif 356966edfb0SGao Xiang }; 357966edfb0SGao Xiang 35847e4937aSGao Xiang int z_erofs_decompress(struct z_erofs_decompress_req *rq, 359eaa9172aSGao Xiang struct page **pagepool) 36047e4937aSGao Xiang { 361966edfb0SGao Xiang return decompressors[rq->alg].decompress(rq, pagepool); 36247e4937aSGao Xiang } 363