zlib.c 10 KB

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