decompressor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2019 HUAWEI, Inc.
  4. * https://www.huawei.com/
  5. */
  6. #include "compress.h"
  7. #include <linux/module.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. int z_erofs_load_lz4_config(struct super_block *sb,
  24. struct erofs_super_block *dsb,
  25. struct z_erofs_lz4_cfgs *lz4, int size)
  26. {
  27. struct erofs_sb_info *sbi = EROFS_SB(sb);
  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 erofs_pcpubuf_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. get_page(victim);
  101. } else {
  102. victim = erofs_allocpage(pagepool,
  103. GFP_KERNEL | __GFP_NOFAIL);
  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 = erofs_get_pcpubuf(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. print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
  224. 16, 1, src + inputmargin, rq->inputsize, true);
  225. print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
  226. 16, 1, out, rq->outputsize, true);
  227. if (ret >= 0)
  228. memset(out + ret, 0, rq->outputsize - ret);
  229. ret = -EIO;
  230. } else {
  231. ret = 0;
  232. }
  233. if (maptype == 0) {
  234. kunmap_local(headpage);
  235. } else if (maptype == 1) {
  236. vm_unmap_ram(src, ctx->inpages);
  237. } else if (maptype == 2) {
  238. erofs_put_pcpubuf(src);
  239. } else if (maptype != 3) {
  240. DBG_BUGON(1);
  241. return -EFAULT;
  242. }
  243. return ret;
  244. }
  245. static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq,
  246. struct page **pagepool)
  247. {
  248. struct z_erofs_lz4_decompress_ctx ctx;
  249. unsigned int dst_maptype;
  250. void *dst;
  251. int ret;
  252. ctx.rq = rq;
  253. ctx.oend = rq->pageofs_out + rq->outputsize;
  254. ctx.outpages = PAGE_ALIGN(ctx.oend) >> PAGE_SHIFT;
  255. ctx.inpages = PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT;
  256. /* one optimized fast path only for non bigpcluster cases yet */
  257. if (ctx.inpages == 1 && ctx.outpages == 1 && !rq->inplace_io) {
  258. DBG_BUGON(!*rq->out);
  259. dst = kmap_local_page(*rq->out);
  260. dst_maptype = 0;
  261. goto dstmap_out;
  262. }
  263. /* general decoding path which can be used for all cases */
  264. ret = z_erofs_lz4_prepare_dstpages(&ctx, pagepool);
  265. if (ret < 0) {
  266. return ret;
  267. } else if (ret > 0) {
  268. dst = page_address(*rq->out);
  269. dst_maptype = 1;
  270. } else {
  271. dst = erofs_vm_map_ram(rq->out, ctx.outpages);
  272. if (!dst)
  273. return -ENOMEM;
  274. dst_maptype = 2;
  275. }
  276. dstmap_out:
  277. ret = z_erofs_lz4_decompress_mem(&ctx, dst);
  278. if (!dst_maptype)
  279. kunmap_local(dst);
  280. else if (dst_maptype == 2)
  281. vm_unmap_ram(dst, ctx.outpages);
  282. return ret;
  283. }
  284. static int z_erofs_transform_plain(struct z_erofs_decompress_req *rq,
  285. struct page **pagepool)
  286. {
  287. const unsigned int nrpages_in =
  288. PAGE_ALIGN(rq->pageofs_in + rq->inputsize) >> PAGE_SHIFT;
  289. const unsigned int nrpages_out =
  290. PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
  291. const unsigned int bs = rq->sb->s_blocksize;
  292. unsigned int cur = 0, ni = 0, no, pi, po, insz, cnt;
  293. u8 *kin;
  294. DBG_BUGON(rq->outputsize > rq->inputsize);
  295. if (rq->alg == Z_EROFS_COMPRESSION_INTERLACED) {
  296. cur = bs - (rq->pageofs_out & (bs - 1));
  297. pi = (rq->pageofs_in + rq->inputsize - cur) & ~PAGE_MASK;
  298. cur = min(cur, rq->outputsize);
  299. if (cur && rq->out[0]) {
  300. kin = kmap_local_page(rq->in[nrpages_in - 1]);
  301. if (rq->out[0] == rq->in[nrpages_in - 1]) {
  302. memmove(kin + rq->pageofs_out, kin + pi, cur);
  303. flush_dcache_page(rq->out[0]);
  304. } else {
  305. memcpy_to_page(rq->out[0], rq->pageofs_out,
  306. kin + pi, cur);
  307. }
  308. kunmap_local(kin);
  309. }
  310. rq->outputsize -= cur;
  311. }
  312. for (; rq->outputsize; rq->pageofs_in = 0, cur += PAGE_SIZE, ni++) {
  313. insz = min_t(unsigned int, PAGE_SIZE - rq->pageofs_in,
  314. rq->outputsize);
  315. rq->outputsize -= insz;
  316. if (!rq->in[ni])
  317. continue;
  318. kin = kmap_local_page(rq->in[ni]);
  319. pi = 0;
  320. do {
  321. no = (rq->pageofs_out + cur + pi) >> PAGE_SHIFT;
  322. po = (rq->pageofs_out + cur + pi) & ~PAGE_MASK;
  323. DBG_BUGON(no >= nrpages_out);
  324. cnt = min_t(unsigned int, insz - pi, PAGE_SIZE - po);
  325. if (rq->out[no] == rq->in[ni]) {
  326. memmove(kin + po,
  327. kin + rq->pageofs_in + pi, cnt);
  328. flush_dcache_page(rq->out[no]);
  329. } else if (rq->out[no]) {
  330. memcpy_to_page(rq->out[no], po,
  331. kin + rq->pageofs_in + pi, cnt);
  332. }
  333. pi += cnt;
  334. } while (pi < insz);
  335. kunmap_local(kin);
  336. }
  337. DBG_BUGON(ni > nrpages_in);
  338. return 0;
  339. }
  340. const struct z_erofs_decompressor erofs_decompressors[] = {
  341. [Z_EROFS_COMPRESSION_SHIFTED] = {
  342. .decompress = z_erofs_transform_plain,
  343. .name = "shifted"
  344. },
  345. [Z_EROFS_COMPRESSION_INTERLACED] = {
  346. .decompress = z_erofs_transform_plain,
  347. .name = "interlaced"
  348. },
  349. [Z_EROFS_COMPRESSION_LZ4] = {
  350. .decompress = z_erofs_lz4_decompress,
  351. .name = "lz4"
  352. },
  353. #ifdef CONFIG_EROFS_FS_ZIP_LZMA
  354. [Z_EROFS_COMPRESSION_LZMA] = {
  355. .decompress = z_erofs_lzma_decompress,
  356. .name = "lzma"
  357. },
  358. #endif
  359. };