zlib.c 10 KB

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