decompressor.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2019 HUAWEI, Inc.
  4. * https://www.huawei.com/
  5. * Copyright (C) 2024 Alibaba Cloud
  6. */
  7. #include "compress.h"
  8. #include <linux/lz4.h>
  9. #ifndef LZ4_DISTANCE_MAX /* history window size */
  10. #define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
  11. #endif
  12. #define LZ4_MAX_DISTANCE_PAGES (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
  13. #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
  14. #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32)
  15. #endif
  16. struct z_erofs_lz4_decompress_ctx {
  17. struct z_erofs_decompress_req *rq;
  18. /* # of encoded, decoded pages */
  19. unsigned int inpages, outpages;
  20. /* decoded block total length (used for in-place decompression) */
  21. unsigned int oend;
  22. };
  23. static int z_erofs_load_lz4_config(struct super_block *sb,
  24. struct erofs_super_block *dsb, void *data, int size)
  25. {
  26. struct erofs_sb_info *sbi = EROFS_SB(sb);
  27. struct z_erofs_lz4_cfgs *lz4 = data;
  28. u16 distance;
  29. if (lz4) {
  30. if (size < sizeof(struct z_erofs_lz4_cfgs)) {
  31. erofs_err(sb, "invalid lz4 cfgs, size=%u", size);
  32. return -EINVAL;
  33. }
  34. distance = le16_to_cpu(lz4->max_distance);
  35. sbi->lz4.max_pclusterblks = le16_to_cpu(lz4->max_pclusterblks);
  36. if (!sbi->lz4.max_pclusterblks) {
  37. sbi->lz4.max_pclusterblks = 1; /* reserved case */
  38. } else if (sbi->lz4.max_pclusterblks >
  39. erofs_blknr(sb, Z_EROFS_PCLUSTER_MAX_SIZE)) {
  40. erofs_err(sb, "too large lz4 pclusterblks %u",
  41. sbi->lz4.max_pclusterblks);
  42. return -EINVAL;
  43. }
  44. } else {
  45. distance = le16_to_cpu(dsb->u1.lz4_max_distance);
  46. sbi->lz4.max_pclusterblks = 1;
  47. }
  48. sbi->lz4.max_distance_pages = distance ?
  49. DIV_ROUND_UP(distance, PAGE_SIZE) + 1 :
  50. LZ4_MAX_DISTANCE_PAGES;
  51. return z_erofs_gbuf_growsize(sbi->lz4.max_pclusterblks);
  52. }
  53. /*
  54. * Fill all gaps with bounce pages if it's a sparse page list. Also check if
  55. * all physical pages are consecutive, which can be seen for moderate CR.
  56. */
  57. static int z_erofs_lz4_prepare_dstpages(struct z_erofs_lz4_decompress_ctx *ctx,
  58. struct page **pagepool)
  59. {
  60. struct z_erofs_decompress_req *rq = ctx->rq;
  61. struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
  62. unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
  63. BITS_PER_LONG)] = { 0 };
  64. unsigned int lz4_max_distance_pages =
  65. EROFS_SB(rq->sb)->lz4.max_distance_pages;
  66. void *kaddr = NULL;
  67. unsigned int i, j, top;
  68. top = 0;
  69. for (i = j = 0; i < ctx->outpages; ++i, ++j) {
  70. struct page *const page = rq->out[i];
  71. struct page *victim;
  72. if (j >= lz4_max_distance_pages)
  73. j = 0;
  74. /* 'valid' bounced can only be tested after a complete round */
  75. if (!rq->fillgaps && test_bit(j, bounced)) {
  76. DBG_BUGON(i < lz4_max_distance_pages);
  77. DBG_BUGON(top >= lz4_max_distance_pages);
  78. availables[top++] = rq->out[i - lz4_max_distance_pages];
  79. }
  80. if (page) {
  81. __clear_bit(j, bounced);
  82. if (!PageHighMem(page)) {
  83. if (!i) {
  84. kaddr = page_address(page);
  85. continue;
  86. }
  87. if (kaddr &&
  88. kaddr + PAGE_SIZE == page_address(page)) {
  89. kaddr += PAGE_SIZE;
  90. continue;
  91. }
  92. }
  93. kaddr = NULL;
  94. continue;
  95. }
  96. kaddr = NULL;
  97. __set_bit(j, bounced);
  98. if (top) {
  99. victim = availables[--top];
  100. } else {
  101. victim = __erofs_allocpage(pagepool, rq->gfp, true);
  102. if (!victim)
  103. return -ENOMEM;
  104. set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE);
  105. }
  106. rq->out[i] = victim;
  107. }
  108. return kaddr ? 1 : 0;
  109. }
  110. static void *z_erofs_lz4_handle_overlap(struct z_erofs_lz4_decompress_ctx *ctx,
  111. void *inpage, void *out, unsigned int *inputmargin,
  112. int *maptype, bool may_inplace)
  113. {
  114. struct z_erofs_decompress_req *rq = ctx->rq;
  115. unsigned int omargin, total, i;
  116. struct page **in;
  117. void *src, *tmp;
  118. if (rq->inplace_io) {
  119. omargin = PAGE_ALIGN(ctx->oend) - ctx->oend;
  120. if (rq->partial_decoding || !may_inplace ||
  121. omargin < LZ4_DECOMPRESS_INPLACE_MARGIN(rq->inputsize))
  122. goto docopy;
  123. for (i = 0; i < ctx->inpages; ++i)
  124. if (rq->out[ctx->outpages - ctx->inpages + i] !=
  125. rq->in[i])
  126. goto docopy;
  127. kunmap_local(inpage);
  128. *maptype = 3;
  129. return out + ((ctx->outpages - ctx->inpages) << PAGE_SHIFT);
  130. }
  131. if (ctx->inpages <= 1) {
  132. *maptype = 0;
  133. return inpage;
  134. }
  135. kunmap_local(inpage);
  136. src = erofs_vm_map_ram(rq->in, ctx->inpages);
  137. if (!src)
  138. return ERR_PTR(-ENOMEM);
  139. *maptype = 1;
  140. return src;
  141. docopy:
  142. /* Or copy compressed data which can be overlapped to per-CPU buffer */
  143. in = rq->in;
  144. src = z_erofs_get_gbuf(ctx->inpages);
  145. if (!src) {
  146. DBG_BUGON(1);
  147. kunmap_local(inpage);
  148. return ERR_PTR(-EFAULT);
  149. }
  150. tmp = src;
  151. total = rq->inputsize;
  152. while (total) {
  153. unsigned int page_copycnt =
  154. min_t(unsigned int, total, PAGE_SIZE - *inputmargin);
  155. if (!inpage)
  156. inpage = kmap_local_page(*in);
  157. memcpy(tmp, inpage + *inputmargin, page_copycnt);
  158. kunmap_local(inpage);
  159. inpage = NULL;
  160. tmp += page_copycnt;
  161. total -= page_copycnt;
  162. ++in;
  163. *inputmargin = 0;
  164. }
  165. *maptype = 2;
  166. return src;
  167. }
  168. /*
  169. * Get the exact inputsize with zero_padding feature.
  170. * - For LZ4, it should work if zero_padding feature is on (5.3+);
  171. * - For MicroLZMA, it'd be enabled all the time.
  172. */
  173. int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
  174. unsigned int padbufsize)
  175. {
  176. const char *padend;
  177. padend = memchr_inv(padbuf, 0, padbufsize);
  178. if (!padend)
  179. return -EFSCORRUPTED;
  180. rq->inputsize -= padend - padbuf;
  181. rq->pageofs_in += padend - padbuf;
  182. return 0;
  183. }
  184. static int z_erofs_lz4_decompress_mem(struct z_erofs_lz4_decompress_ctx *ctx,
  185. u8 *dst)
  186. {
  187. struct z_erofs_decompress_req *rq = ctx->rq;
  188. bool support_0padding = false, may_inplace = false;
  189. unsigned int inputmargin;
  190. u8 *out, *headpage, *src;
  191. int ret, maptype;
  192. DBG_BUGON(*rq->in == NULL);
  193. headpage = kmap_local_page(*rq->in);
  194. /* LZ4 decompression inplace is only safe if zero_padding is enabled */
  195. if (erofs_sb_has_zero_padding(EROFS_SB(rq->sb))) {
  196. support_0padding = true;
  197. ret = z_erofs_fixup_insize(rq, headpage + rq->pageofs_in,
  198. min_t(unsigned int, rq->inputsize,
  199. rq->sb->s_blocksize - rq->pageofs_in));
  200. if (ret) {
  201. kunmap_local(headpage);
  202. return ret;
  203. }
  204. may_inplace = !((rq->pageofs_in + rq->inputsize) &
  205. (rq->sb->s_blocksize - 1));
  206. }
  207. inputmargin = rq->pageofs_in;
  208. src = z_erofs_lz4_handle_overlap(ctx, headpage, dst, &inputmargin,
  209. &maptype, may_inplace);
  210. if (IS_ERR(src))
  211. return PTR_ERR(src);
  212. out = dst + rq->pageofs_out;
  213. /* legacy format could compress extra data in a pcluster. */
  214. if (rq->partial_decoding || !support_0padding)
  215. ret = LZ4_decompress_safe_partial(src + inputmargin, out,
  216. rq->inputsize, rq->outputsize, rq->outputsize);
  217. else
  218. ret = LZ4_decompress_safe(src + inputmargin, out,
  219. rq->inputsize, rq->outputsize);
  220. if (ret != rq->outputsize) {
  221. erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
  222. ret, rq->inputsize, inputmargin, rq->outputsize);
  223. if (ret >= 0)
  224. memset(out + ret, 0, rq->outputsize - ret);
  225. ret = -EFSCORRUPTED;
  226. } else {
  227. ret = 0;
  228. }
  229. if (maptype == 0) {
  230. kunmap_local(headpage);
  231. } else if (maptype == 1) {
  232. vm_unmap_ram(src, ctx->inpages);
  233. } else if (maptype == 2) {
  234. z_erofs_put_gbuf(src);
  235. } else if (maptype != 3) {
  236. DBG_BUGON(1);
  237. return -EFAULT;
  238. }
  239. return ret;
  240. }
  241. static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq,
  242. struct page **pagepool)
  243. {
  244. struct z_erofs_lz4_decompress_ctx ctx;
  245. unsigned int dst_maptype;
  246. void *dst;
  247. int ret;
  248. ctx.rq = rq;
  249. ctx.oend = rq->pageofs_out + rq->outputsize;
  250. ctx.outpages = PAGE_ALIGN(ctx.oend) >> PAGE_SHIFT;
  251. ctx.inpages = PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT;
  252. /* one optimized fast path only for non bigpcluster cases yet */
  253. if (ctx.inpages == 1 && ctx.outpages == 1 && !rq->inplace_io) {
  254. DBG_BUGON(!*rq->out);
  255. dst = kmap_local_page(*rq->out);
  256. dst_maptype = 0;
  257. goto dstmap_out;
  258. }
  259. /* general decoding path which can be used for all cases */
  260. ret = z_erofs_lz4_prepare_dstpages(&ctx, pagepool);
  261. if (ret < 0) {
  262. return ret;
  263. } else if (ret > 0) {
  264. dst = page_address(*rq->out);
  265. dst_maptype = 1;
  266. } else {
  267. dst = erofs_vm_map_ram(rq->out, ctx.outpages);
  268. if (!dst)
  269. return -ENOMEM;
  270. dst_maptype = 2;
  271. }
  272. dstmap_out:
  273. ret = z_erofs_lz4_decompress_mem(&ctx, dst);
  274. if (!dst_maptype)
  275. kunmap_local(dst);
  276. else if (dst_maptype == 2)
  277. vm_unmap_ram(dst, ctx.outpages);
  278. return ret;
  279. }
  280. static int z_erofs_transform_plain(struct z_erofs_decompress_req *rq,
  281. struct page **pagepool)
  282. {
  283. const unsigned int nrpages_in =
  284. PAGE_ALIGN(rq->pageofs_in + rq->inputsize) >> PAGE_SHIFT;
  285. const unsigned int nrpages_out =
  286. PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
  287. const unsigned int bs = rq->sb->s_blocksize;
  288. unsigned int cur = 0, ni = 0, no, pi, po, insz, cnt;
  289. u8 *kin;
  290. if (rq->outputsize > rq->inputsize)
  291. return -EOPNOTSUPP;
  292. if (rq->alg == Z_EROFS_COMPRESSION_INTERLACED) {
  293. cur = bs - (rq->pageofs_out & (bs - 1));
  294. pi = (rq->pageofs_in + rq->inputsize - cur) & ~PAGE_MASK;
  295. cur = min(cur, rq->outputsize);
  296. if (cur && rq->out[0]) {
  297. kin = kmap_local_page(rq->in[nrpages_in - 1]);
  298. if (rq->out[0] == rq->in[nrpages_in - 1]) {
  299. memmove(kin + rq->pageofs_out, kin + pi, cur);
  300. flush_dcache_page(rq->out[0]);
  301. } else {
  302. memcpy_to_page(rq->out[0], rq->pageofs_out,
  303. kin + pi, cur);
  304. }
  305. kunmap_local(kin);
  306. }
  307. rq->outputsize -= cur;
  308. }
  309. for (; rq->outputsize; rq->pageofs_in = 0, cur += PAGE_SIZE, ni++) {
  310. insz = min(PAGE_SIZE - rq->pageofs_in, rq->outputsize);
  311. rq->outputsize -= insz;
  312. if (!rq->in[ni])
  313. continue;
  314. kin = kmap_local_page(rq->in[ni]);
  315. pi = 0;
  316. do {
  317. no = (rq->pageofs_out + cur + pi) >> PAGE_SHIFT;
  318. po = (rq->pageofs_out + cur + pi) & ~PAGE_MASK;
  319. DBG_BUGON(no >= nrpages_out);
  320. cnt = min(insz - pi, PAGE_SIZE - po);
  321. if (rq->out[no] == rq->in[ni]) {
  322. memmove(kin + po,
  323. kin + rq->pageofs_in + pi, cnt);
  324. flush_dcache_page(rq->out[no]);
  325. } else if (rq->out[no]) {
  326. memcpy_to_page(rq->out[no], po,
  327. kin + rq->pageofs_in + pi, cnt);
  328. }
  329. pi += cnt;
  330. } while (pi < insz);
  331. kunmap_local(kin);
  332. }
  333. DBG_BUGON(ni > nrpages_in);
  334. return 0;
  335. }
  336. int z_erofs_stream_switch_bufs(struct z_erofs_stream_dctx *dctx, void **dst,
  337. void **src, struct page **pgpl)
  338. {
  339. struct z_erofs_decompress_req *rq = dctx->rq;
  340. struct super_block *sb = rq->sb;
  341. struct page **pgo, *tmppage;
  342. unsigned int j;
  343. if (!dctx->avail_out) {
  344. if (++dctx->no >= dctx->outpages || !rq->outputsize) {
  345. erofs_err(sb, "insufficient space for decompressed data");
  346. return -EFSCORRUPTED;
  347. }
  348. if (dctx->kout)
  349. kunmap_local(dctx->kout);
  350. dctx->avail_out = min(rq->outputsize, PAGE_SIZE - rq->pageofs_out);
  351. rq->outputsize -= dctx->avail_out;
  352. pgo = &rq->out[dctx->no];
  353. if (!*pgo && rq->fillgaps) { /* deduped */
  354. *pgo = erofs_allocpage(pgpl, rq->gfp);
  355. if (!*pgo) {
  356. dctx->kout = NULL;
  357. return -ENOMEM;
  358. }
  359. set_page_private(*pgo, Z_EROFS_SHORTLIVED_PAGE);
  360. }
  361. if (*pgo) {
  362. dctx->kout = kmap_local_page(*pgo);
  363. *dst = dctx->kout + rq->pageofs_out;
  364. } else {
  365. *dst = dctx->kout = NULL;
  366. }
  367. rq->pageofs_out = 0;
  368. }
  369. if (dctx->inbuf_pos == dctx->inbuf_sz && rq->inputsize) {
  370. if (++dctx->ni >= dctx->inpages) {
  371. erofs_err(sb, "invalid compressed data");
  372. return -EFSCORRUPTED;
  373. }
  374. if (dctx->kout) /* unlike kmap(), take care of the orders */
  375. kunmap_local(dctx->kout);
  376. kunmap_local(dctx->kin);
  377. dctx->inbuf_sz = min_t(u32, rq->inputsize, PAGE_SIZE);
  378. rq->inputsize -= dctx->inbuf_sz;
  379. dctx->kin = kmap_local_page(rq->in[dctx->ni]);
  380. *src = dctx->kin;
  381. dctx->bounced = false;
  382. if (dctx->kout) {
  383. j = (u8 *)*dst - dctx->kout;
  384. dctx->kout = kmap_local_page(rq->out[dctx->no]);
  385. *dst = dctx->kout + j;
  386. }
  387. dctx->inbuf_pos = 0;
  388. }
  389. /*
  390. * Handle overlapping: Use the given bounce buffer if the input data is
  391. * under processing; Or utilize short-lived pages from the on-stack page
  392. * pool, where pages are shared among the same request. Note that only
  393. * a few inplace I/O pages need to be doubled.
  394. */
  395. if (!dctx->bounced && rq->out[dctx->no] == rq->in[dctx->ni]) {
  396. memcpy(dctx->bounce, *src, dctx->inbuf_sz);
  397. *src = dctx->bounce;
  398. dctx->bounced = true;
  399. }
  400. for (j = dctx->ni + 1; j < dctx->inpages; ++j) {
  401. if (rq->out[dctx->no] != rq->in[j])
  402. continue;
  403. tmppage = erofs_allocpage(pgpl, rq->gfp);
  404. if (!tmppage)
  405. return -ENOMEM;
  406. set_page_private(tmppage, Z_EROFS_SHORTLIVED_PAGE);
  407. copy_highpage(tmppage, rq->in[j]);
  408. rq->in[j] = tmppage;
  409. }
  410. return 0;
  411. }
  412. const struct z_erofs_decompressor *z_erofs_decomp[] = {
  413. [Z_EROFS_COMPRESSION_SHIFTED] = &(const struct z_erofs_decompressor) {
  414. .decompress = z_erofs_transform_plain,
  415. .name = "shifted"
  416. },
  417. [Z_EROFS_COMPRESSION_INTERLACED] = &(const struct z_erofs_decompressor) {
  418. .decompress = z_erofs_transform_plain,
  419. .name = "interlaced"
  420. },
  421. [Z_EROFS_COMPRESSION_LZ4] = &(const struct z_erofs_decompressor) {
  422. .config = z_erofs_load_lz4_config,
  423. .decompress = z_erofs_lz4_decompress,
  424. .init = z_erofs_gbuf_init,
  425. .exit = z_erofs_gbuf_exit,
  426. .name = "lz4"
  427. },
  428. #ifdef CONFIG_EROFS_FS_ZIP_LZMA
  429. [Z_EROFS_COMPRESSION_LZMA] = &z_erofs_lzma_decomp,
  430. #endif
  431. #ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
  432. [Z_EROFS_COMPRESSION_DEFLATE] = &z_erofs_deflate_decomp,
  433. #endif
  434. #ifdef CONFIG_EROFS_FS_ZIP_ZSTD
  435. [Z_EROFS_COMPRESSION_ZSTD] = &z_erofs_zstd_decomp,
  436. #endif
  437. };
  438. int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
  439. {
  440. struct erofs_sb_info *sbi = EROFS_SB(sb);
  441. struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
  442. unsigned int algs, alg;
  443. erofs_off_t offset;
  444. int size, ret = 0;
  445. if (!erofs_sb_has_compr_cfgs(sbi)) {
  446. sbi->available_compr_algs = 1 << Z_EROFS_COMPRESSION_LZ4;
  447. return z_erofs_load_lz4_config(sb, dsb, NULL, 0);
  448. }
  449. sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
  450. if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
  451. erofs_err(sb, "unidentified algorithms %x, please upgrade kernel",
  452. sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
  453. return -EOPNOTSUPP;
  454. }
  455. erofs_init_metabuf(&buf, sb);
  456. offset = EROFS_SUPER_OFFSET + sbi->sb_size;
  457. alg = 0;
  458. for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
  459. const struct z_erofs_decompressor *dec = z_erofs_decomp[alg];
  460. void *data;
  461. if (!(algs & 1))
  462. continue;
  463. data = erofs_read_metadata(sb, &buf, &offset, &size);
  464. if (IS_ERR(data)) {
  465. ret = PTR_ERR(data);
  466. break;
  467. }
  468. if (alg < Z_EROFS_COMPRESSION_MAX && dec && dec->config) {
  469. ret = dec->config(sb, dsb, data, size);
  470. } else {
  471. erofs_err(sb, "algorithm %d isn't enabled on this kernel",
  472. alg);
  473. ret = -EOPNOTSUPP;
  474. }
  475. kfree(data);
  476. if (ret)
  477. break;
  478. }
  479. erofs_put_metabuf(&buf);
  480. return ret;
  481. }
  482. int __init z_erofs_init_decompressor(void)
  483. {
  484. int i, err;
  485. for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
  486. err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
  487. if (err) {
  488. while (i--)
  489. if (z_erofs_decomp[i])
  490. z_erofs_decomp[i]->exit();
  491. return err;
  492. }
  493. }
  494. return 0;
  495. }
  496. void z_erofs_exit_decompressor(void)
  497. {
  498. int i;
  499. for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i)
  500. if (z_erofs_decomp[i])
  501. z_erofs_decomp[i]->exit();
  502. }