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 195d50538fSHuang Jianan int z_erofs_load_lz4_config(struct super_block *sb, 2046249cdeSGao Xiang struct erofs_super_block *dsb, 2146249cdeSGao Xiang struct z_erofs_lz4_cfgs *lz4, int size) 225d50538fSHuang Jianan { 234fea63f7SGao Xiang struct erofs_sb_info *sbi = EROFS_SB(sb); 2446249cdeSGao Xiang u16 distance; 2546249cdeSGao Xiang 2646249cdeSGao Xiang if (lz4) { 2746249cdeSGao Xiang if (size < sizeof(struct z_erofs_lz4_cfgs)) { 2846249cdeSGao Xiang erofs_err(sb, "invalid lz4 cfgs, size=%u", size); 2946249cdeSGao Xiang return -EINVAL; 3046249cdeSGao Xiang } 3146249cdeSGao Xiang distance = le16_to_cpu(lz4->max_distance); 324fea63f7SGao Xiang 334fea63f7SGao Xiang sbi->lz4.max_pclusterblks = le16_to_cpu(lz4->max_pclusterblks); 344fea63f7SGao Xiang if (!sbi->lz4.max_pclusterblks) { 354fea63f7SGao Xiang sbi->lz4.max_pclusterblks = 1; /* reserved case */ 364fea63f7SGao Xiang } else if (sbi->lz4.max_pclusterblks > 374fea63f7SGao Xiang Z_EROFS_PCLUSTER_MAX_SIZE / EROFS_BLKSIZ) { 384fea63f7SGao Xiang erofs_err(sb, "too large lz4 pclusterblks %u", 394fea63f7SGao Xiang sbi->lz4.max_pclusterblks); 404fea63f7SGao Xiang return -EINVAL; 414fea63f7SGao Xiang } else if (sbi->lz4.max_pclusterblks >= 2) { 424fea63f7SGao Xiang erofs_info(sb, "EXPERIMENTAL big pcluster feature in use. Use at your own risk!"); 434fea63f7SGao Xiang } 4446249cdeSGao Xiang } else { 4514373711SGao Xiang distance = le16_to_cpu(dsb->u1.lz4_max_distance); 464fea63f7SGao Xiang sbi->lz4.max_pclusterblks = 1; 4746249cdeSGao Xiang } 485d50538fSHuang Jianan 494fea63f7SGao Xiang sbi->lz4.max_distance_pages = distance ? 505d50538fSHuang Jianan DIV_ROUND_UP(distance, PAGE_SIZE) + 1 : 515d50538fSHuang Jianan LZ4_MAX_DISTANCE_PAGES; 524fea63f7SGao Xiang return erofs_pcpubuf_growsize(sbi->lz4.max_pclusterblks); 535d50538fSHuang Jianan } 545d50538fSHuang Jianan 55966edfb0SGao Xiang /* 56966edfb0SGao Xiang * Fill all gaps with bounce pages if it's a sparse page list. Also check if 57966edfb0SGao Xiang * all physical pages are consecutive, which can be seen for moderate CR. 58966edfb0SGao Xiang */ 59966edfb0SGao Xiang static int z_erofs_lz4_prepare_dstpages(struct z_erofs_decompress_req *rq, 60eaa9172aSGao Xiang struct page **pagepool) 6147e4937aSGao Xiang { 6247e4937aSGao Xiang const unsigned int nr = 6347e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; 6447e4937aSGao Xiang struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL }; 6547e4937aSGao Xiang unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES, 6647e4937aSGao Xiang BITS_PER_LONG)] = { 0 }; 675d50538fSHuang Jianan unsigned int lz4_max_distance_pages = 685d50538fSHuang Jianan EROFS_SB(rq->sb)->lz4.max_distance_pages; 6947e4937aSGao Xiang void *kaddr = NULL; 7047e4937aSGao Xiang unsigned int i, j, top; 7147e4937aSGao Xiang 7247e4937aSGao Xiang top = 0; 7347e4937aSGao Xiang for (i = j = 0; i < nr; ++i, ++j) { 7447e4937aSGao Xiang struct page *const page = rq->out[i]; 7547e4937aSGao Xiang struct page *victim; 7647e4937aSGao Xiang 775d50538fSHuang Jianan if (j >= lz4_max_distance_pages) 7847e4937aSGao Xiang j = 0; 7947e4937aSGao Xiang 8047e4937aSGao Xiang /* 'valid' bounced can only be tested after a complete round */ 8147e4937aSGao Xiang if (test_bit(j, bounced)) { 825d50538fSHuang Jianan DBG_BUGON(i < lz4_max_distance_pages); 835d50538fSHuang Jianan DBG_BUGON(top >= lz4_max_distance_pages); 845d50538fSHuang Jianan availables[top++] = rq->out[i - lz4_max_distance_pages]; 8547e4937aSGao Xiang } 8647e4937aSGao Xiang 8747e4937aSGao Xiang if (page) { 8847e4937aSGao Xiang __clear_bit(j, bounced); 8947e4937aSGao Xiang if (kaddr) { 9047e4937aSGao Xiang if (kaddr + PAGE_SIZE == page_address(page)) 9147e4937aSGao Xiang kaddr += PAGE_SIZE; 9247e4937aSGao Xiang else 9347e4937aSGao Xiang kaddr = NULL; 9447e4937aSGao Xiang } else if (!i) { 9547e4937aSGao Xiang kaddr = page_address(page); 9647e4937aSGao Xiang } 9747e4937aSGao Xiang continue; 9847e4937aSGao Xiang } 9947e4937aSGao Xiang kaddr = NULL; 10047e4937aSGao Xiang __set_bit(j, bounced); 10147e4937aSGao Xiang 10247e4937aSGao Xiang if (top) { 10347e4937aSGao Xiang victim = availables[--top]; 10447e4937aSGao Xiang get_page(victim); 10547e4937aSGao Xiang } else { 106b4892fa3SHuang Jianan victim = erofs_allocpage(pagepool, 107b4892fa3SHuang Jianan GFP_KERNEL | __GFP_NOFAIL); 1086aaa7b06SGao Xiang set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE); 10947e4937aSGao Xiang } 11047e4937aSGao Xiang rq->out[i] = victim; 11147e4937aSGao Xiang } 11247e4937aSGao Xiang return kaddr ? 1 : 0; 11347e4937aSGao Xiang } 11447e4937aSGao Xiang 115966edfb0SGao Xiang static void *z_erofs_lz4_handle_inplace_io(struct z_erofs_decompress_req *rq, 116598162d0SGao Xiang void *inpage, unsigned int *inputmargin, int *maptype, 117598162d0SGao Xiang bool support_0padding) 11847e4937aSGao Xiang { 119598162d0SGao Xiang unsigned int nrpages_in, nrpages_out; 120598162d0SGao Xiang unsigned int ofull, oend, inputsize, total, i, j; 121598162d0SGao Xiang struct page **in; 122598162d0SGao Xiang void *src, *tmp; 12347e4937aSGao Xiang 124598162d0SGao Xiang inputsize = rq->inputsize; 125598162d0SGao Xiang nrpages_in = PAGE_ALIGN(inputsize) >> PAGE_SHIFT; 126598162d0SGao Xiang oend = rq->pageofs_out + rq->outputsize; 127598162d0SGao Xiang ofull = PAGE_ALIGN(oend); 128598162d0SGao Xiang nrpages_out = ofull >> PAGE_SHIFT; 129598162d0SGao Xiang 130598162d0SGao Xiang if (rq->inplace_io) { 131598162d0SGao Xiang if (rq->partial_decoding || !support_0padding || 132598162d0SGao Xiang ofull - oend < LZ4_DECOMPRESS_INPLACE_MARGIN(inputsize)) 133598162d0SGao Xiang goto docopy; 134598162d0SGao Xiang 135598162d0SGao Xiang for (i = 0; i < nrpages_in; ++i) { 136598162d0SGao Xiang DBG_BUGON(rq->in[i] == NULL); 137598162d0SGao Xiang for (j = 0; j < nrpages_out - nrpages_in + i; ++j) 138598162d0SGao Xiang if (rq->out[j] == rq->in[i]) 139598162d0SGao Xiang goto docopy; 14047e4937aSGao Xiang } 141598162d0SGao Xiang } 142598162d0SGao Xiang 143598162d0SGao Xiang if (nrpages_in <= 1) { 144598162d0SGao Xiang *maptype = 0; 145598162d0SGao Xiang return inpage; 146598162d0SGao Xiang } 147598162d0SGao Xiang kunmap_atomic(inpage); 148598162d0SGao Xiang might_sleep(); 149598162d0SGao Xiang src = erofs_vm_map_ram(rq->in, nrpages_in); 150598162d0SGao Xiang if (!src) 151598162d0SGao Xiang return ERR_PTR(-ENOMEM); 152598162d0SGao Xiang *maptype = 1; 153598162d0SGao Xiang return src; 154598162d0SGao Xiang 155598162d0SGao Xiang docopy: 156598162d0SGao Xiang /* Or copy compressed data which can be overlapped to per-CPU buffer */ 157598162d0SGao Xiang in = rq->in; 158598162d0SGao Xiang src = erofs_get_pcpubuf(nrpages_in); 159598162d0SGao Xiang if (!src) { 160598162d0SGao Xiang DBG_BUGON(1); 161598162d0SGao Xiang kunmap_atomic(inpage); 162598162d0SGao Xiang return ERR_PTR(-EFAULT); 163598162d0SGao Xiang } 164598162d0SGao Xiang 165598162d0SGao Xiang tmp = src; 166598162d0SGao Xiang total = rq->inputsize; 167598162d0SGao Xiang while (total) { 168598162d0SGao Xiang unsigned int page_copycnt = 169598162d0SGao Xiang min_t(unsigned int, total, PAGE_SIZE - *inputmargin); 170598162d0SGao Xiang 171598162d0SGao Xiang if (!inpage) 172598162d0SGao Xiang inpage = kmap_atomic(*in); 173598162d0SGao Xiang memcpy(tmp, inpage + *inputmargin, page_copycnt); 174598162d0SGao Xiang kunmap_atomic(inpage); 175598162d0SGao Xiang inpage = NULL; 176598162d0SGao Xiang tmp += page_copycnt; 177598162d0SGao Xiang total -= page_copycnt; 178598162d0SGao Xiang ++in; 179598162d0SGao Xiang *inputmargin = 0; 180598162d0SGao Xiang } 181598162d0SGao Xiang *maptype = 2; 182598162d0SGao Xiang return src; 18347e4937aSGao Xiang } 18447e4937aSGao Xiang 185966edfb0SGao Xiang static int z_erofs_lz4_decompress_mem(struct z_erofs_decompress_req *rq, 186966edfb0SGao Xiang u8 *out) 18747e4937aSGao Xiang { 188598162d0SGao Xiang unsigned int inputmargin; 189598162d0SGao Xiang u8 *headpage, *src; 190598162d0SGao Xiang bool support_0padding; 191598162d0SGao Xiang int ret, maptype; 19247e4937aSGao Xiang 193598162d0SGao Xiang DBG_BUGON(*rq->in == NULL); 194598162d0SGao Xiang headpage = kmap_atomic(*rq->in); 19547e4937aSGao Xiang inputmargin = 0; 19647e4937aSGao Xiang support_0padding = false; 19747e4937aSGao Xiang 198*7e508f2cSHuang Jianan /* decompression inplace is only safe when zero_padding is enabled */ 199*7e508f2cSHuang Jianan if (erofs_sb_has_zero_padding(EROFS_SB(rq->sb))) { 20047e4937aSGao Xiang support_0padding = true; 20147e4937aSGao Xiang 202598162d0SGao Xiang while (!headpage[inputmargin & ~PAGE_MASK]) 20347e4937aSGao Xiang if (!(++inputmargin & ~PAGE_MASK)) 20447e4937aSGao Xiang break; 20547e4937aSGao Xiang 20647e4937aSGao Xiang if (inputmargin >= rq->inputsize) { 207598162d0SGao Xiang kunmap_atomic(headpage); 20847e4937aSGao Xiang return -EIO; 20947e4937aSGao Xiang } 21047e4937aSGao Xiang } 21147e4937aSGao Xiang 212598162d0SGao Xiang rq->inputsize -= inputmargin; 213966edfb0SGao Xiang src = z_erofs_lz4_handle_inplace_io(rq, headpage, &inputmargin, 214966edfb0SGao Xiang &maptype, support_0padding); 215598162d0SGao Xiang if (IS_ERR(src)) 216598162d0SGao Xiang return PTR_ERR(src); 21747e4937aSGao Xiang 218af1038abSGao Xiang /* legacy format could compress extra data in a pcluster. */ 219af1038abSGao Xiang if (rq->partial_decoding || !support_0padding) 22047e4937aSGao Xiang ret = LZ4_decompress_safe_partial(src + inputmargin, out, 221598162d0SGao Xiang rq->inputsize, rq->outputsize, rq->outputsize); 222af1038abSGao Xiang else 223af1038abSGao Xiang ret = LZ4_decompress_safe(src + inputmargin, out, 224598162d0SGao Xiang rq->inputsize, rq->outputsize); 225af1038abSGao Xiang 226aa99a76bSGao Xiang if (ret != rq->outputsize) { 227aa99a76bSGao Xiang erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]", 228598162d0SGao Xiang ret, rq->inputsize, inputmargin, rq->outputsize); 229aa99a76bSGao Xiang 23047e4937aSGao Xiang print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET, 231598162d0SGao Xiang 16, 1, src + inputmargin, rq->inputsize, true); 23247e4937aSGao Xiang print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET, 23347e4937aSGao Xiang 16, 1, out, rq->outputsize, true); 234aa99a76bSGao Xiang 235aa99a76bSGao Xiang if (ret >= 0) 236aa99a76bSGao Xiang memset(out + ret, 0, rq->outputsize - ret); 23747e4937aSGao Xiang ret = -EIO; 2385b6e7e12SYue Hu } else { 2395b6e7e12SYue Hu ret = 0; 24047e4937aSGao Xiang } 24147e4937aSGao Xiang 242598162d0SGao Xiang if (maptype == 0) { 24347e4937aSGao Xiang kunmap_atomic(src); 244598162d0SGao Xiang } else if (maptype == 1) { 245598162d0SGao Xiang vm_unmap_ram(src, PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT); 246598162d0SGao Xiang } else if (maptype == 2) { 247598162d0SGao Xiang erofs_put_pcpubuf(src); 248598162d0SGao Xiang } else { 249598162d0SGao Xiang DBG_BUGON(1); 250598162d0SGao Xiang return -EFAULT; 251598162d0SGao Xiang } 25247e4937aSGao Xiang return ret; 25347e4937aSGao Xiang } 25447e4937aSGao Xiang 255966edfb0SGao Xiang static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, 256eaa9172aSGao Xiang struct page **pagepool) 25747e4937aSGao Xiang { 25847e4937aSGao Xiang const unsigned int nrpages_out = 25947e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; 26047e4937aSGao Xiang unsigned int dst_maptype; 26147e4937aSGao Xiang void *dst; 262598162d0SGao Xiang int ret; 26347e4937aSGao Xiang 2645b6e7e12SYue Hu /* one optimized fast path only for non bigpcluster cases yet */ 2655b6e7e12SYue Hu if (rq->inputsize <= PAGE_SIZE && nrpages_out == 1 && !rq->inplace_io) { 26647e4937aSGao Xiang DBG_BUGON(!*rq->out); 26747e4937aSGao Xiang dst = kmap_atomic(*rq->out); 26847e4937aSGao Xiang dst_maptype = 0; 26947e4937aSGao Xiang goto dstmap_out; 27047e4937aSGao Xiang } 27147e4937aSGao Xiang 272598162d0SGao Xiang /* general decoding path which can be used for all cases */ 273966edfb0SGao Xiang ret = z_erofs_lz4_prepare_dstpages(rq, pagepool); 274598162d0SGao Xiang if (ret < 0) 27547e4937aSGao Xiang return ret; 276598162d0SGao Xiang if (ret) { 27747e4937aSGao Xiang dst = page_address(*rq->out); 27847e4937aSGao Xiang dst_maptype = 1; 27947e4937aSGao Xiang goto dstmap_out; 28047e4937aSGao Xiang } 28147e4937aSGao Xiang 282598162d0SGao Xiang dst = erofs_vm_map_ram(rq->out, nrpages_out); 28347e4937aSGao Xiang if (!dst) 28447e4937aSGao Xiang return -ENOMEM; 28547e4937aSGao Xiang dst_maptype = 2; 28647e4937aSGao Xiang 28747e4937aSGao Xiang dstmap_out: 288966edfb0SGao Xiang ret = z_erofs_lz4_decompress_mem(rq, dst + rq->pageofs_out); 28947e4937aSGao Xiang 29047e4937aSGao Xiang if (!dst_maptype) 29147e4937aSGao Xiang kunmap_atomic(dst); 29247e4937aSGao Xiang else if (dst_maptype == 2) 29373d03931SGao Xiang vm_unmap_ram(dst, nrpages_out); 29447e4937aSGao Xiang return ret; 29547e4937aSGao Xiang } 29647e4937aSGao Xiang 297966edfb0SGao Xiang static int z_erofs_shifted_transform(struct z_erofs_decompress_req *rq, 298eaa9172aSGao Xiang struct page **pagepool) 29947e4937aSGao Xiang { 30047e4937aSGao Xiang const unsigned int nrpages_out = 30147e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; 30247e4937aSGao Xiang const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out; 30347e4937aSGao Xiang unsigned char *src, *dst; 30447e4937aSGao Xiang 30547e4937aSGao Xiang if (nrpages_out > 2) { 30647e4937aSGao Xiang DBG_BUGON(1); 30747e4937aSGao Xiang return -EIO; 30847e4937aSGao Xiang } 30947e4937aSGao Xiang 31047e4937aSGao Xiang if (rq->out[0] == *rq->in) { 31147e4937aSGao Xiang DBG_BUGON(nrpages_out != 1); 31247e4937aSGao Xiang return 0; 31347e4937aSGao Xiang } 31447e4937aSGao Xiang 31547e4937aSGao Xiang src = kmap_atomic(*rq->in); 3164d202437SGao Xiang if (rq->out[0]) { 31747e4937aSGao Xiang dst = kmap_atomic(rq->out[0]); 31847e4937aSGao Xiang memcpy(dst + rq->pageofs_out, src, righthalf); 3194d202437SGao Xiang kunmap_atomic(dst); 32047e4937aSGao Xiang } 32147e4937aSGao Xiang 3224d202437SGao Xiang if (nrpages_out == 2) { 3234d202437SGao Xiang DBG_BUGON(!rq->out[1]); 32447e4937aSGao Xiang if (rq->out[1] == *rq->in) { 32547e4937aSGao Xiang memmove(src, src + righthalf, rq->pageofs_out); 3264d202437SGao Xiang } else { 32747e4937aSGao Xiang dst = kmap_atomic(rq->out[1]); 32847e4937aSGao Xiang memcpy(dst, src + righthalf, rq->pageofs_out); 32947e4937aSGao Xiang kunmap_atomic(dst); 3304d202437SGao Xiang } 3314d202437SGao Xiang } 33247e4937aSGao Xiang kunmap_atomic(src); 33347e4937aSGao Xiang return 0; 33447e4937aSGao Xiang } 33547e4937aSGao Xiang 336966edfb0SGao Xiang static struct z_erofs_decompressor decompressors[] = { 337966edfb0SGao Xiang [Z_EROFS_COMPRESSION_SHIFTED] = { 338966edfb0SGao Xiang .decompress = z_erofs_shifted_transform, 339966edfb0SGao Xiang .name = "shifted" 340966edfb0SGao Xiang }, 341966edfb0SGao Xiang [Z_EROFS_COMPRESSION_LZ4] = { 342966edfb0SGao Xiang .decompress = z_erofs_lz4_decompress, 343966edfb0SGao Xiang .name = "lz4" 344966edfb0SGao Xiang }, 345622ceaddSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP_LZMA 346622ceaddSGao Xiang [Z_EROFS_COMPRESSION_LZMA] = { 347622ceaddSGao Xiang .decompress = z_erofs_lzma_decompress, 348622ceaddSGao Xiang .name = "lzma" 349622ceaddSGao Xiang }, 350622ceaddSGao Xiang #endif 351966edfb0SGao Xiang }; 352966edfb0SGao Xiang 35347e4937aSGao Xiang int z_erofs_decompress(struct z_erofs_decompress_req *rq, 354eaa9172aSGao Xiang struct page **pagepool) 35547e4937aSGao Xiang { 356966edfb0SGao Xiang return decompressors[rq->alg].decompress(rq, pagepool); 35747e4937aSGao Xiang } 358