decompressor.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. else
  301. memcpy_to_page(rq->out[0], rq->pageofs_out,
  302. kin + pi, cur);
  303. kunmap_local(kin);
  304. }
  305. rq->outputsize -= cur;
  306. }
  307. for (; rq->outputsize; rq->pageofs_in = 0, cur += PAGE_SIZE, ni++) {
  308. insz = min(PAGE_SIZE - rq->pageofs_in, rq->outputsize);
  309. rq->outputsize -= insz;
  310. if (!rq->in[ni])
  311. continue;
  312. kin = kmap_local_page(rq->in[ni]);
  313. pi = 0;
  314. do {
  315. no = (rq->pageofs_out + cur + pi) >> PAGE_SHIFT;
  316. po = (rq->pageofs_out + cur + pi) & ~PAGE_MASK;
  317. DBG_BUGON(no >= nrpages_out);
  318. cnt = min(insz - pi, PAGE_SIZE - po);
  319. if (rq->out[no] == rq->in[ni])
  320. memmove(kin + po,
  321. kin + rq->pageofs_in + pi, cnt);
  322. else if (rq->out[no])
  323. memcpy_to_page(rq->out[no], po,
  324. kin + rq->pageofs_in + pi, cnt);
  325. pi += cnt;
  326. } while (pi < insz);
  327. kunmap_local(kin);
  328. }
  329. DBG_BUGON(ni > nrpages_in);
  330. return 0;
  331. }
  332. int z_erofs_stream_switch_bufs(struct z_erofs_stream_dctx *dctx, void **dst,
  333. void **src, struct page **pgpl)
  334. {
  335. struct z_erofs_decompress_req *rq = dctx->rq;
  336. struct super_block *sb = rq->sb;
  337. struct page **pgo, *tmppage;
  338. unsigned int j;
  339. if (!dctx->avail_out) {
  340. if (++dctx->no >= dctx->outpages || !rq->outputsize) {
  341. erofs_err(sb, "insufficient space for decompressed data");
  342. return -EFSCORRUPTED;
  343. }
  344. if (dctx->kout)
  345. kunmap_local(dctx->kout);
  346. dctx->avail_out = min(rq->outputsize, PAGE_SIZE - rq->pageofs_out);
  347. rq->outputsize -= dctx->avail_out;
  348. pgo = &rq->out[dctx->no];
  349. if (!*pgo && rq->fillgaps) { /* deduped */
  350. *pgo = erofs_allocpage(pgpl, rq->gfp);
  351. if (!*pgo) {
  352. dctx->kout = NULL;
  353. return -ENOMEM;
  354. }
  355. set_page_private(*pgo, Z_EROFS_SHORTLIVED_PAGE);
  356. }
  357. if (*pgo) {
  358. dctx->kout = kmap_local_page(*pgo);
  359. *dst = dctx->kout + rq->pageofs_out;
  360. } else {
  361. *dst = dctx->kout = NULL;
  362. }
  363. rq->pageofs_out = 0;
  364. }
  365. if (dctx->inbuf_pos == dctx->inbuf_sz && rq->inputsize) {
  366. if (++dctx->ni >= dctx->inpages) {
  367. erofs_err(sb, "invalid compressed data");
  368. return -EFSCORRUPTED;
  369. }
  370. if (dctx->kout) /* unlike kmap(), take care of the orders */
  371. kunmap_local(dctx->kout);
  372. kunmap_local(dctx->kin);
  373. dctx->inbuf_sz = min_t(u32, rq->inputsize, PAGE_SIZE);
  374. rq->inputsize -= dctx->inbuf_sz;
  375. dctx->kin = kmap_local_page(rq->in[dctx->ni]);
  376. *src = dctx->kin;
  377. dctx->bounced = false;
  378. if (dctx->kout) {
  379. j = (u8 *)*dst - dctx->kout;
  380. dctx->kout = kmap_local_page(rq->out[dctx->no]);
  381. *dst = dctx->kout + j;
  382. }
  383. dctx->inbuf_pos = 0;
  384. }
  385. /*
  386. * Handle overlapping: Use the given bounce buffer if the input data is
  387. * under processing; Or utilize short-lived pages from the on-stack page
  388. * pool, where pages are shared among the same request. Note that only
  389. * a few inplace I/O pages need to be doubled.
  390. */
  391. if (!dctx->bounced && rq->out[dctx->no] == rq->in[dctx->ni]) {
  392. memcpy(dctx->bounce, *src, dctx->inbuf_sz);
  393. *src = dctx->bounce;
  394. dctx->bounced = true;
  395. }
  396. for (j = dctx->ni + 1; j < dctx->inpages; ++j) {
  397. if (rq->out[dctx->no] != rq->in[j])
  398. continue;
  399. tmppage = erofs_allocpage(pgpl, rq->gfp);
  400. if (!tmppage)
  401. return -ENOMEM;
  402. set_page_private(tmppage, Z_EROFS_SHORTLIVED_PAGE);
  403. copy_highpage(tmppage, rq->in[j]);
  404. rq->in[j] = tmppage;
  405. }
  406. return 0;
  407. }
  408. const struct z_erofs_decompressor *z_erofs_decomp[] = {
  409. [Z_EROFS_COMPRESSION_SHIFTED] = &(const struct z_erofs_decompressor) {
  410. .decompress = z_erofs_transform_plain,
  411. .name = "shifted"
  412. },
  413. [Z_EROFS_COMPRESSION_INTERLACED] = &(const struct z_erofs_decompressor) {
  414. .decompress = z_erofs_transform_plain,
  415. .name = "interlaced"
  416. },
  417. [Z_EROFS_COMPRESSION_LZ4] = &(const struct z_erofs_decompressor) {
  418. .config = z_erofs_load_lz4_config,
  419. .decompress = z_erofs_lz4_decompress,
  420. .init = z_erofs_gbuf_init,
  421. .exit = z_erofs_gbuf_exit,
  422. .name = "lz4"
  423. },
  424. #ifdef CONFIG_EROFS_FS_ZIP_LZMA
  425. [Z_EROFS_COMPRESSION_LZMA] = &z_erofs_lzma_decomp,
  426. #endif
  427. #ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
  428. [Z_EROFS_COMPRESSION_DEFLATE] = &z_erofs_deflate_decomp,
  429. #endif
  430. #ifdef CONFIG_EROFS_FS_ZIP_ZSTD
  431. [Z_EROFS_COMPRESSION_ZSTD] = &z_erofs_zstd_decomp,
  432. #endif
  433. };
  434. int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
  435. {
  436. struct erofs_sb_info *sbi = EROFS_SB(sb);
  437. struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
  438. unsigned int algs, alg;
  439. erofs_off_t offset;
  440. int size, ret = 0;
  441. if (!erofs_sb_has_compr_cfgs(sbi)) {
  442. sbi->available_compr_algs = 1 << Z_EROFS_COMPRESSION_LZ4;
  443. return z_erofs_load_lz4_config(sb, dsb, NULL, 0);
  444. }
  445. sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
  446. if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
  447. erofs_err(sb, "unidentified algorithms %x, please upgrade kernel",
  448. sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
  449. return -EOPNOTSUPP;
  450. }
  451. erofs_init_metabuf(&buf, sb);
  452. offset = EROFS_SUPER_OFFSET + sbi->sb_size;
  453. alg = 0;
  454. for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
  455. const struct z_erofs_decompressor *dec = z_erofs_decomp[alg];
  456. void *data;
  457. if (!(algs & 1))
  458. continue;
  459. data = erofs_read_metadata(sb, &buf, &offset, &size);
  460. if (IS_ERR(data)) {
  461. ret = PTR_ERR(data);
  462. break;
  463. }
  464. if (alg < Z_EROFS_COMPRESSION_MAX && dec && dec->config) {
  465. ret = dec->config(sb, dsb, data, size);
  466. } else {
  467. erofs_err(sb, "algorithm %d isn't enabled on this kernel",
  468. alg);
  469. ret = -EOPNOTSUPP;
  470. }
  471. kfree(data);
  472. if (ret)
  473. break;
  474. }
  475. erofs_put_metabuf(&buf);
  476. return ret;
  477. }
  478. int __init z_erofs_init_decompressor(void)
  479. {
  480. int i, err;
  481. for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
  482. err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
  483. if (err) {
  484. while (i--)
  485. if (z_erofs_decomp[i])
  486. z_erofs_decomp[i]->exit();
  487. return err;
  488. }
  489. }
  490. return 0;
  491. }
  492. void z_erofs_exit_decompressor(void)
  493. {
  494. int i;
  495. for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i)
  496. if (z_erofs_decomp[i])
  497. z_erofs_decomp[i]->exit();
  498. }