zlib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. *
  5. * Based on jffs2 zlib code:
  6. * Copyright © 2001-2007 Red Hat, Inc.
  7. * Created by David Woodhouse <dwmw2@infradead.org>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/zlib.h>
  12. #include <linux/zutil.h>
  13. #include <linux/mm.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <linux/sched.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/bio.h>
  19. #include <linux/refcount.h>
  20. #include "compression.h"
  21. struct workspace {
  22. z_stream strm;
  23. char *buf;
  24. struct list_head list;
  25. int level;
  26. };
  27. static void zlib_free_workspace(struct list_head *ws)
  28. {
  29. struct workspace *workspace = list_entry(ws, struct workspace, list);
  30. kvfree(workspace->strm.workspace);
  31. kfree(workspace->buf);
  32. kfree(workspace);
  33. }
  34. static struct list_head *zlib_alloc_workspace(void)
  35. {
  36. struct workspace *workspace;
  37. int workspacesize;
  38. workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
  39. if (!workspace)
  40. return ERR_PTR(-ENOMEM);
  41. workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
  42. zlib_inflate_workspacesize());
  43. workspace->strm.workspace = kvmalloc(workspacesize, GFP_KERNEL);
  44. workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  45. if (!workspace->strm.workspace || !workspace->buf)
  46. goto fail;
  47. INIT_LIST_HEAD(&workspace->list);
  48. return &workspace->list;
  49. fail:
  50. zlib_free_workspace(&workspace->list);
  51. return ERR_PTR(-ENOMEM);
  52. }
  53. static int zlib_compress_pages(struct list_head *ws,
  54. struct address_space *mapping,
  55. u64 start,
  56. struct page **pages,
  57. unsigned long *out_pages,
  58. unsigned long *total_in,
  59. unsigned long *total_out)
  60. {
  61. struct workspace *workspace = list_entry(ws, struct workspace, list);
  62. int ret;
  63. char *data_in;
  64. char *cpage_out;
  65. int nr_pages = 0;
  66. struct page *in_page = NULL;
  67. struct page *out_page = NULL;
  68. unsigned long bytes_left;
  69. unsigned long len = *total_out;
  70. unsigned long nr_dest_pages = *out_pages;
  71. const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
  72. *out_pages = 0;
  73. *total_out = 0;
  74. *total_in = 0;
  75. if (Z_OK != zlib_deflateInit(&workspace->strm, workspace->level)) {
  76. pr_warn("BTRFS: deflateInit failed\n");
  77. ret = -EIO;
  78. goto out;
  79. }
  80. workspace->strm.total_in = 0;
  81. workspace->strm.total_out = 0;
  82. in_page = find_get_page(mapping, start >> PAGE_SHIFT);
  83. data_in = kmap(in_page);
  84. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  85. if (out_page == NULL) {
  86. ret = -ENOMEM;
  87. goto out;
  88. }
  89. cpage_out = kmap(out_page);
  90. pages[0] = out_page;
  91. nr_pages = 1;
  92. workspace->strm.next_in = data_in;
  93. workspace->strm.next_out = cpage_out;
  94. workspace->strm.avail_out = PAGE_SIZE;
  95. workspace->strm.avail_in = min(len, PAGE_SIZE);
  96. while (workspace->strm.total_in < len) {
  97. ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
  98. if (ret != Z_OK) {
  99. pr_debug("BTRFS: deflate in loop returned %d\n",
  100. ret);
  101. zlib_deflateEnd(&workspace->strm);
  102. ret = -EIO;
  103. goto out;
  104. }
  105. /* we're making it bigger, give up */
  106. if (workspace->strm.total_in > 8192 &&
  107. workspace->strm.total_in <
  108. workspace->strm.total_out) {
  109. ret = -E2BIG;
  110. goto out;
  111. }
  112. /* we need another page for writing out. Test this
  113. * before the total_in so we will pull in a new page for
  114. * the stream end if required
  115. */
  116. if (workspace->strm.avail_out == 0) {
  117. kunmap(out_page);
  118. if (nr_pages == nr_dest_pages) {
  119. out_page = NULL;
  120. ret = -E2BIG;
  121. goto out;
  122. }
  123. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  124. if (out_page == NULL) {
  125. ret = -ENOMEM;
  126. goto out;
  127. }
  128. cpage_out = kmap(out_page);
  129. pages[nr_pages] = out_page;
  130. nr_pages++;
  131. workspace->strm.avail_out = PAGE_SIZE;
  132. workspace->strm.next_out = cpage_out;
  133. }
  134. /* we're all done */
  135. if (workspace->strm.total_in >= len)
  136. break;
  137. /* we've read in a full page, get a new one */
  138. if (workspace->strm.avail_in == 0) {
  139. if (workspace->strm.total_out > max_out)
  140. break;
  141. bytes_left = len - workspace->strm.total_in;
  142. kunmap(in_page);
  143. put_page(in_page);
  144. start += PAGE_SIZE;
  145. in_page = find_get_page(mapping,
  146. start >> PAGE_SHIFT);
  147. data_in = kmap(in_page);
  148. workspace->strm.avail_in = min(bytes_left,
  149. PAGE_SIZE);
  150. workspace->strm.next_in = data_in;
  151. }
  152. }
  153. workspace->strm.avail_in = 0;
  154. ret = zlib_deflate(&workspace->strm, Z_FINISH);
  155. zlib_deflateEnd(&workspace->strm);
  156. if (ret != Z_STREAM_END) {
  157. ret = -EIO;
  158. goto out;
  159. }
  160. if (workspace->strm.total_out >= workspace->strm.total_in) {
  161. ret = -E2BIG;
  162. goto out;
  163. }
  164. ret = 0;
  165. *total_out = workspace->strm.total_out;
  166. *total_in = workspace->strm.total_in;
  167. out:
  168. *out_pages = nr_pages;
  169. if (out_page)
  170. kunmap(out_page);
  171. if (in_page) {
  172. kunmap(in_page);
  173. put_page(in_page);
  174. }
  175. return ret;
  176. }
  177. static int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
  178. {
  179. struct workspace *workspace = list_entry(ws, struct workspace, list);
  180. int ret = 0, ret2;
  181. int wbits = MAX_WBITS;
  182. char *data_in;
  183. size_t total_out = 0;
  184. unsigned long page_in_index = 0;
  185. size_t srclen = cb->compressed_len;
  186. unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
  187. unsigned long buf_start;
  188. struct page **pages_in = cb->compressed_pages;
  189. u64 disk_start = cb->start;
  190. struct bio *orig_bio = cb->orig_bio;
  191. data_in = kmap(pages_in[page_in_index]);
  192. workspace->strm.next_in = data_in;
  193. workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
  194. workspace->strm.total_in = 0;
  195. workspace->strm.total_out = 0;
  196. workspace->strm.next_out = workspace->buf;
  197. workspace->strm.avail_out = PAGE_SIZE;
  198. /* If it's deflate, and it's got no preset dictionary, then
  199. we can tell zlib to skip the adler32 check. */
  200. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  201. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  202. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  203. wbits = -((data_in[0] >> 4) + 8);
  204. workspace->strm.next_in += 2;
  205. workspace->strm.avail_in -= 2;
  206. }
  207. if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
  208. pr_warn("BTRFS: inflateInit failed\n");
  209. kunmap(pages_in[page_in_index]);
  210. return -EIO;
  211. }
  212. while (workspace->strm.total_in < srclen) {
  213. ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
  214. if (ret != Z_OK && ret != Z_STREAM_END)
  215. break;
  216. buf_start = total_out;
  217. total_out = workspace->strm.total_out;
  218. /* we didn't make progress in this inflate call, we're done */
  219. if (buf_start == total_out)
  220. break;
  221. ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
  222. total_out, disk_start,
  223. orig_bio);
  224. if (ret2 == 0) {
  225. ret = 0;
  226. goto done;
  227. }
  228. workspace->strm.next_out = workspace->buf;
  229. workspace->strm.avail_out = PAGE_SIZE;
  230. if (workspace->strm.avail_in == 0) {
  231. unsigned long tmp;
  232. kunmap(pages_in[page_in_index]);
  233. page_in_index++;
  234. if (page_in_index >= total_pages_in) {
  235. data_in = NULL;
  236. break;
  237. }
  238. data_in = kmap(pages_in[page_in_index]);
  239. workspace->strm.next_in = data_in;
  240. tmp = srclen - workspace->strm.total_in;
  241. workspace->strm.avail_in = min(tmp,
  242. PAGE_SIZE);
  243. }
  244. }
  245. if (ret != Z_STREAM_END)
  246. ret = -EIO;
  247. else
  248. ret = 0;
  249. done:
  250. zlib_inflateEnd(&workspace->strm);
  251. if (data_in)
  252. kunmap(pages_in[page_in_index]);
  253. if (!ret)
  254. zero_fill_bio(orig_bio);
  255. return ret;
  256. }
  257. static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
  258. struct page *dest_page,
  259. unsigned long start_byte,
  260. size_t srclen, size_t destlen)
  261. {
  262. struct workspace *workspace = list_entry(ws, struct workspace, list);
  263. int ret = 0;
  264. int wbits = MAX_WBITS;
  265. unsigned long bytes_left;
  266. unsigned long total_out = 0;
  267. unsigned long pg_offset = 0;
  268. char *kaddr;
  269. destlen = min_t(unsigned long, destlen, PAGE_SIZE);
  270. bytes_left = destlen;
  271. workspace->strm.next_in = data_in;
  272. workspace->strm.avail_in = srclen;
  273. workspace->strm.total_in = 0;
  274. workspace->strm.next_out = workspace->buf;
  275. workspace->strm.avail_out = PAGE_SIZE;
  276. workspace->strm.total_out = 0;
  277. /* If it's deflate, and it's got no preset dictionary, then
  278. we can tell zlib to skip the adler32 check. */
  279. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  280. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  281. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  282. wbits = -((data_in[0] >> 4) + 8);
  283. workspace->strm.next_in += 2;
  284. workspace->strm.avail_in -= 2;
  285. }
  286. if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
  287. pr_warn("BTRFS: inflateInit failed\n");
  288. return -EIO;
  289. }
  290. while (bytes_left > 0) {
  291. unsigned long buf_start;
  292. unsigned long buf_offset;
  293. unsigned long bytes;
  294. ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
  295. if (ret != Z_OK && ret != Z_STREAM_END)
  296. break;
  297. buf_start = total_out;
  298. total_out = workspace->strm.total_out;
  299. if (total_out == buf_start) {
  300. ret = -EIO;
  301. break;
  302. }
  303. if (total_out <= start_byte)
  304. goto next;
  305. if (total_out > start_byte && buf_start < start_byte)
  306. buf_offset = start_byte - buf_start;
  307. else
  308. buf_offset = 0;
  309. bytes = min(PAGE_SIZE - pg_offset,
  310. PAGE_SIZE - buf_offset);
  311. bytes = min(bytes, bytes_left);
  312. kaddr = kmap_atomic(dest_page);
  313. memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
  314. kunmap_atomic(kaddr);
  315. pg_offset += bytes;
  316. bytes_left -= bytes;
  317. next:
  318. workspace->strm.next_out = workspace->buf;
  319. workspace->strm.avail_out = PAGE_SIZE;
  320. }
  321. if (ret != Z_STREAM_END && bytes_left != 0)
  322. ret = -EIO;
  323. else
  324. ret = 0;
  325. zlib_inflateEnd(&workspace->strm);
  326. /*
  327. * this should only happen if zlib returned fewer bytes than we
  328. * expected. btrfs_get_block is responsible for zeroing from the
  329. * end of the inline extent (destlen) to the end of the page
  330. */
  331. if (pg_offset < destlen) {
  332. kaddr = kmap_atomic(dest_page);
  333. memset(kaddr + pg_offset, 0, destlen - pg_offset);
  334. kunmap_atomic(kaddr);
  335. }
  336. return ret;
  337. }
  338. static void zlib_set_level(struct list_head *ws, unsigned int type)
  339. {
  340. struct workspace *workspace = list_entry(ws, struct workspace, list);
  341. unsigned level = (type & 0xF0) >> 4;
  342. if (level > 9)
  343. level = 9;
  344. workspace->level = level > 0 ? level : 3;
  345. }
  346. const struct btrfs_compress_op btrfs_zlib_compress = {
  347. .alloc_workspace = zlib_alloc_workspace,
  348. .free_workspace = zlib_free_workspace,
  349. .compress_pages = zlib_compress_pages,
  350. .decompress_bio = zlib_decompress_bio,
  351. .decompress = zlib_decompress,
  352. .set_level = zlib_set_level,
  353. };