zstd.c 10 KB

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