zstd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016-present, Facebook, Inc.
  4. * All rights reserved.
  5. *
  6. */
  7. #include <linux/bio.h>
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/refcount.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/zstd.h>
  17. #include "compression.h"
  18. #define ZSTD_BTRFS_MAX_WINDOWLOG 17
  19. #define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
  20. #define ZSTD_BTRFS_DEFAULT_LEVEL 3
  21. static ZSTD_parameters zstd_get_btrfs_parameters(size_t src_len)
  22. {
  23. ZSTD_parameters params = ZSTD_getParams(ZSTD_BTRFS_DEFAULT_LEVEL,
  24. src_len, 0);
  25. if (params.cParams.windowLog > ZSTD_BTRFS_MAX_WINDOWLOG)
  26. params.cParams.windowLog = ZSTD_BTRFS_MAX_WINDOWLOG;
  27. WARN_ON(src_len > ZSTD_BTRFS_MAX_INPUT);
  28. return params;
  29. }
  30. struct workspace {
  31. void *mem;
  32. size_t size;
  33. char *buf;
  34. struct list_head list;
  35. ZSTD_inBuffer in_buf;
  36. ZSTD_outBuffer out_buf;
  37. };
  38. static void zstd_free_workspace(struct list_head *ws)
  39. {
  40. struct workspace *workspace = list_entry(ws, struct workspace, list);
  41. kvfree(workspace->mem);
  42. kfree(workspace->buf);
  43. kfree(workspace);
  44. }
  45. static struct list_head *zstd_alloc_workspace(void)
  46. {
  47. ZSTD_parameters params =
  48. zstd_get_btrfs_parameters(ZSTD_BTRFS_MAX_INPUT);
  49. struct workspace *workspace;
  50. workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
  51. if (!workspace)
  52. return ERR_PTR(-ENOMEM);
  53. workspace->size = max_t(size_t,
  54. ZSTD_CStreamWorkspaceBound(params.cParams),
  55. ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT));
  56. workspace->mem = kvmalloc(workspace->size, GFP_KERNEL);
  57. workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  58. if (!workspace->mem || !workspace->buf)
  59. goto fail;
  60. INIT_LIST_HEAD(&workspace->list);
  61. return &workspace->list;
  62. fail:
  63. zstd_free_workspace(&workspace->list);
  64. return ERR_PTR(-ENOMEM);
  65. }
  66. static int zstd_compress_pages(struct list_head *ws,
  67. struct address_space *mapping,
  68. u64 start,
  69. struct page **pages,
  70. unsigned long *out_pages,
  71. unsigned long *total_in,
  72. unsigned long *total_out)
  73. {
  74. struct workspace *workspace = list_entry(ws, struct workspace, list);
  75. ZSTD_CStream *stream;
  76. int ret = 0;
  77. int nr_pages = 0;
  78. struct page *in_page = NULL; /* The current page to read */
  79. struct page *out_page = NULL; /* The current page to write to */
  80. unsigned long tot_in = 0;
  81. unsigned long tot_out = 0;
  82. unsigned long len = *total_out;
  83. const unsigned long nr_dest_pages = *out_pages;
  84. unsigned long max_out = nr_dest_pages * PAGE_SIZE;
  85. ZSTD_parameters params = zstd_get_btrfs_parameters(len);
  86. *out_pages = 0;
  87. *total_out = 0;
  88. *total_in = 0;
  89. /* Initialize the stream */
  90. stream = ZSTD_initCStream(params, len, workspace->mem,
  91. workspace->size);
  92. if (!stream) {
  93. pr_warn("BTRFS: ZSTD_initCStream failed\n");
  94. ret = -EIO;
  95. goto out;
  96. }
  97. /* map in the first page of input data */
  98. in_page = find_get_page(mapping, start >> PAGE_SHIFT);
  99. workspace->in_buf.src = kmap(in_page);
  100. workspace->in_buf.pos = 0;
  101. workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
  102. /* Allocate and map in the output buffer */
  103. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  104. if (out_page == NULL) {
  105. ret = -ENOMEM;
  106. goto out;
  107. }
  108. pages[nr_pages++] = out_page;
  109. workspace->out_buf.dst = kmap(out_page);
  110. workspace->out_buf.pos = 0;
  111. workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
  112. while (1) {
  113. size_t ret2;
  114. ret2 = ZSTD_compressStream(stream, &workspace->out_buf,
  115. &workspace->in_buf);
  116. if (ZSTD_isError(ret2)) {
  117. pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
  118. ZSTD_getErrorCode(ret2));
  119. ret = -EIO;
  120. goto out;
  121. }
  122. /* Check to see if we are making it bigger */
  123. if (tot_in + workspace->in_buf.pos > 8192 &&
  124. tot_in + workspace->in_buf.pos <
  125. tot_out + workspace->out_buf.pos) {
  126. ret = -E2BIG;
  127. goto out;
  128. }
  129. /* We've reached the end of our output range */
  130. if (workspace->out_buf.pos >= max_out) {
  131. tot_out += workspace->out_buf.pos;
  132. ret = -E2BIG;
  133. goto out;
  134. }
  135. /* Check if we need more output space */
  136. if (workspace->out_buf.pos == workspace->out_buf.size) {
  137. tot_out += PAGE_SIZE;
  138. max_out -= PAGE_SIZE;
  139. kunmap(out_page);
  140. if (nr_pages == nr_dest_pages) {
  141. out_page = NULL;
  142. ret = -E2BIG;
  143. goto out;
  144. }
  145. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  146. if (out_page == NULL) {
  147. ret = -ENOMEM;
  148. goto out;
  149. }
  150. pages[nr_pages++] = out_page;
  151. workspace->out_buf.dst = kmap(out_page);
  152. workspace->out_buf.pos = 0;
  153. workspace->out_buf.size = min_t(size_t, max_out,
  154. PAGE_SIZE);
  155. }
  156. /* We've reached the end of the input */
  157. if (workspace->in_buf.pos >= len) {
  158. tot_in += workspace->in_buf.pos;
  159. break;
  160. }
  161. /* Check if we need more input */
  162. if (workspace->in_buf.pos == workspace->in_buf.size) {
  163. tot_in += PAGE_SIZE;
  164. kunmap(in_page);
  165. put_page(in_page);
  166. start += PAGE_SIZE;
  167. len -= PAGE_SIZE;
  168. in_page = find_get_page(mapping, start >> PAGE_SHIFT);
  169. workspace->in_buf.src = kmap(in_page);
  170. workspace->in_buf.pos = 0;
  171. workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
  172. }
  173. }
  174. while (1) {
  175. size_t ret2;
  176. ret2 = ZSTD_endStream(stream, &workspace->out_buf);
  177. if (ZSTD_isError(ret2)) {
  178. pr_debug("BTRFS: ZSTD_endStream returned %d\n",
  179. ZSTD_getErrorCode(ret2));
  180. ret = -EIO;
  181. goto out;
  182. }
  183. if (ret2 == 0) {
  184. tot_out += workspace->out_buf.pos;
  185. break;
  186. }
  187. if (workspace->out_buf.pos >= max_out) {
  188. tot_out += workspace->out_buf.pos;
  189. ret = -E2BIG;
  190. goto out;
  191. }
  192. tot_out += PAGE_SIZE;
  193. max_out -= PAGE_SIZE;
  194. kunmap(out_page);
  195. if (nr_pages == nr_dest_pages) {
  196. out_page = NULL;
  197. ret = -E2BIG;
  198. goto out;
  199. }
  200. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  201. if (out_page == NULL) {
  202. ret = -ENOMEM;
  203. goto out;
  204. }
  205. pages[nr_pages++] = out_page;
  206. workspace->out_buf.dst = kmap(out_page);
  207. workspace->out_buf.pos = 0;
  208. workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
  209. }
  210. if (tot_out >= tot_in) {
  211. ret = -E2BIG;
  212. goto out;
  213. }
  214. ret = 0;
  215. *total_in = tot_in;
  216. *total_out = tot_out;
  217. out:
  218. *out_pages = nr_pages;
  219. /* Cleanup */
  220. if (in_page) {
  221. kunmap(in_page);
  222. put_page(in_page);
  223. }
  224. if (out_page)
  225. kunmap(out_page);
  226. return ret;
  227. }
  228. static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
  229. {
  230. struct workspace *workspace = list_entry(ws, struct workspace, list);
  231. struct page **pages_in = cb->compressed_pages;
  232. u64 disk_start = cb->start;
  233. struct bio *orig_bio = cb->orig_bio;
  234. size_t srclen = cb->compressed_len;
  235. ZSTD_DStream *stream;
  236. int ret = 0;
  237. unsigned long page_in_index = 0;
  238. unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
  239. unsigned long buf_start;
  240. unsigned long total_out = 0;
  241. stream = ZSTD_initDStream(
  242. ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
  243. if (!stream) {
  244. pr_debug("BTRFS: ZSTD_initDStream failed\n");
  245. ret = -EIO;
  246. goto done;
  247. }
  248. workspace->in_buf.src = kmap(pages_in[page_in_index]);
  249. workspace->in_buf.pos = 0;
  250. workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
  251. workspace->out_buf.dst = workspace->buf;
  252. workspace->out_buf.pos = 0;
  253. workspace->out_buf.size = PAGE_SIZE;
  254. while (1) {
  255. size_t ret2;
  256. ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
  257. &workspace->in_buf);
  258. if (ZSTD_isError(ret2)) {
  259. pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
  260. ZSTD_getErrorCode(ret2));
  261. ret = -EIO;
  262. goto done;
  263. }
  264. buf_start = total_out;
  265. total_out += workspace->out_buf.pos;
  266. workspace->out_buf.pos = 0;
  267. ret = btrfs_decompress_buf2page(workspace->out_buf.dst,
  268. buf_start, total_out, disk_start, orig_bio);
  269. if (ret == 0)
  270. break;
  271. if (workspace->in_buf.pos >= srclen)
  272. break;
  273. /* Check if we've hit the end of a frame */
  274. if (ret2 == 0)
  275. break;
  276. if (workspace->in_buf.pos == workspace->in_buf.size) {
  277. kunmap(pages_in[page_in_index++]);
  278. if (page_in_index >= total_pages_in) {
  279. workspace->in_buf.src = NULL;
  280. ret = -EIO;
  281. goto done;
  282. }
  283. srclen -= PAGE_SIZE;
  284. workspace->in_buf.src = kmap(pages_in[page_in_index]);
  285. workspace->in_buf.pos = 0;
  286. workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
  287. }
  288. }
  289. ret = 0;
  290. zero_fill_bio(orig_bio);
  291. done:
  292. if (workspace->in_buf.src)
  293. kunmap(pages_in[page_in_index]);
  294. return ret;
  295. }
  296. static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
  297. struct page *dest_page,
  298. unsigned long start_byte,
  299. size_t srclen, size_t destlen)
  300. {
  301. struct workspace *workspace = list_entry(ws, struct workspace, list);
  302. ZSTD_DStream *stream;
  303. int ret = 0;
  304. size_t ret2;
  305. unsigned long total_out = 0;
  306. unsigned long pg_offset = 0;
  307. char *kaddr;
  308. stream = ZSTD_initDStream(
  309. ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
  310. if (!stream) {
  311. pr_warn("BTRFS: ZSTD_initDStream failed\n");
  312. ret = -EIO;
  313. goto finish;
  314. }
  315. destlen = min_t(size_t, destlen, PAGE_SIZE);
  316. workspace->in_buf.src = data_in;
  317. workspace->in_buf.pos = 0;
  318. workspace->in_buf.size = srclen;
  319. workspace->out_buf.dst = workspace->buf;
  320. workspace->out_buf.pos = 0;
  321. workspace->out_buf.size = PAGE_SIZE;
  322. ret2 = 1;
  323. while (pg_offset < destlen
  324. && workspace->in_buf.pos < workspace->in_buf.size) {
  325. unsigned long buf_start;
  326. unsigned long buf_offset;
  327. unsigned long bytes;
  328. /* Check if the frame is over and we still need more input */
  329. if (ret2 == 0) {
  330. pr_debug("BTRFS: ZSTD_decompressStream ended early\n");
  331. ret = -EIO;
  332. goto finish;
  333. }
  334. ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
  335. &workspace->in_buf);
  336. if (ZSTD_isError(ret2)) {
  337. pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
  338. ZSTD_getErrorCode(ret2));
  339. ret = -EIO;
  340. goto finish;
  341. }
  342. buf_start = total_out;
  343. total_out += workspace->out_buf.pos;
  344. workspace->out_buf.pos = 0;
  345. if (total_out <= start_byte)
  346. continue;
  347. if (total_out > start_byte && buf_start < start_byte)
  348. buf_offset = start_byte - buf_start;
  349. else
  350. buf_offset = 0;
  351. bytes = min_t(unsigned long, destlen - pg_offset,
  352. workspace->out_buf.size - buf_offset);
  353. kaddr = kmap_atomic(dest_page);
  354. memcpy(kaddr + pg_offset, workspace->out_buf.dst + buf_offset,
  355. bytes);
  356. kunmap_atomic(kaddr);
  357. pg_offset += bytes;
  358. }
  359. ret = 0;
  360. finish:
  361. if (pg_offset < destlen) {
  362. kaddr = kmap_atomic(dest_page);
  363. memset(kaddr + pg_offset, 0, destlen - pg_offset);
  364. kunmap_atomic(kaddr);
  365. }
  366. return ret;
  367. }
  368. static void zstd_set_level(struct list_head *ws, unsigned int type)
  369. {
  370. }
  371. const struct btrfs_compress_op btrfs_zstd_compress = {
  372. .alloc_workspace = zstd_alloc_workspace,
  373. .free_workspace = zstd_free_workspace,
  374. .compress_pages = zstd_compress_pages,
  375. .decompress_bio = zstd_decompress_bio,
  376. .decompress = zstd_decompress,
  377. .set_level = zstd_set_level,
  378. };