zlib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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_CACHE_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, unsigned long len,
  67. struct page **pages,
  68. unsigned long nr_dest_pages,
  69. unsigned long *out_pages,
  70. unsigned long *total_in,
  71. unsigned long *total_out,
  72. unsigned long max_out)
  73. {
  74. struct workspace *workspace = list_entry(ws, struct workspace, list);
  75. int ret;
  76. char *data_in;
  77. char *cpage_out;
  78. int nr_pages = 0;
  79. struct page *in_page = NULL;
  80. struct page *out_page = NULL;
  81. unsigned long bytes_left;
  82. *out_pages = 0;
  83. *total_out = 0;
  84. *total_in = 0;
  85. if (Z_OK != zlib_deflateInit(&workspace->strm, 3)) {
  86. printk(KERN_WARNING "BTRFS: deflateInit failed\n");
  87. ret = -EIO;
  88. goto out;
  89. }
  90. workspace->strm.total_in = 0;
  91. workspace->strm.total_out = 0;
  92. in_page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  93. data_in = kmap(in_page);
  94. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  95. if (out_page == NULL) {
  96. ret = -ENOMEM;
  97. goto out;
  98. }
  99. cpage_out = kmap(out_page);
  100. pages[0] = out_page;
  101. nr_pages = 1;
  102. workspace->strm.next_in = data_in;
  103. workspace->strm.next_out = cpage_out;
  104. workspace->strm.avail_out = PAGE_CACHE_SIZE;
  105. workspace->strm.avail_in = min(len, PAGE_CACHE_SIZE);
  106. while (workspace->strm.total_in < len) {
  107. ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
  108. if (ret != Z_OK) {
  109. printk(KERN_DEBUG "BTRFS: deflate in loop returned %d\n",
  110. ret);
  111. zlib_deflateEnd(&workspace->strm);
  112. ret = -EIO;
  113. goto out;
  114. }
  115. /* we're making it bigger, give up */
  116. if (workspace->strm.total_in > 8192 &&
  117. workspace->strm.total_in <
  118. workspace->strm.total_out) {
  119. ret = -E2BIG;
  120. goto out;
  121. }
  122. /* we need another page for writing out. Test this
  123. * before the total_in so we will pull in a new page for
  124. * the stream end if required
  125. */
  126. if (workspace->strm.avail_out == 0) {
  127. kunmap(out_page);
  128. if (nr_pages == nr_dest_pages) {
  129. out_page = NULL;
  130. ret = -E2BIG;
  131. goto out;
  132. }
  133. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  134. if (out_page == NULL) {
  135. ret = -ENOMEM;
  136. goto out;
  137. }
  138. cpage_out = kmap(out_page);
  139. pages[nr_pages] = out_page;
  140. nr_pages++;
  141. workspace->strm.avail_out = PAGE_CACHE_SIZE;
  142. workspace->strm.next_out = cpage_out;
  143. }
  144. /* we're all done */
  145. if (workspace->strm.total_in >= len)
  146. break;
  147. /* we've read in a full page, get a new one */
  148. if (workspace->strm.avail_in == 0) {
  149. if (workspace->strm.total_out > max_out)
  150. break;
  151. bytes_left = len - workspace->strm.total_in;
  152. kunmap(in_page);
  153. page_cache_release(in_page);
  154. start += PAGE_CACHE_SIZE;
  155. in_page = find_get_page(mapping,
  156. start >> PAGE_CACHE_SHIFT);
  157. data_in = kmap(in_page);
  158. workspace->strm.avail_in = min(bytes_left,
  159. PAGE_CACHE_SIZE);
  160. workspace->strm.next_in = data_in;
  161. }
  162. }
  163. workspace->strm.avail_in = 0;
  164. ret = zlib_deflate(&workspace->strm, Z_FINISH);
  165. zlib_deflateEnd(&workspace->strm);
  166. if (ret != Z_STREAM_END) {
  167. ret = -EIO;
  168. goto out;
  169. }
  170. if (workspace->strm.total_out >= workspace->strm.total_in) {
  171. ret = -E2BIG;
  172. goto out;
  173. }
  174. ret = 0;
  175. *total_out = workspace->strm.total_out;
  176. *total_in = workspace->strm.total_in;
  177. out:
  178. *out_pages = nr_pages;
  179. if (out_page)
  180. kunmap(out_page);
  181. if (in_page) {
  182. kunmap(in_page);
  183. page_cache_release(in_page);
  184. }
  185. return ret;
  186. }
  187. static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
  188. u64 disk_start,
  189. struct bio_vec *bvec,
  190. int vcnt,
  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 page_out_index = 0;
  200. unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_CACHE_SIZE);
  201. unsigned long buf_start;
  202. unsigned long pg_offset;
  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_CACHE_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_CACHE_SIZE;
  210. pg_offset = 0;
  211. /* If it's deflate, and it's got no preset dictionary, then
  212. we can tell zlib to skip the adler32 check. */
  213. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  214. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  215. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  216. wbits = -((data_in[0] >> 4) + 8);
  217. workspace->strm.next_in += 2;
  218. workspace->strm.avail_in -= 2;
  219. }
  220. if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
  221. printk(KERN_WARNING "BTRFS: inflateInit failed\n");
  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. bvec, vcnt,
  236. &page_out_index, &pg_offset);
  237. if (ret2 == 0) {
  238. ret = 0;
  239. goto done;
  240. }
  241. workspace->strm.next_out = workspace->buf;
  242. workspace->strm.avail_out = PAGE_CACHE_SIZE;
  243. if (workspace->strm.avail_in == 0) {
  244. unsigned long tmp;
  245. kunmap(pages_in[page_in_index]);
  246. page_in_index++;
  247. if (page_in_index >= total_pages_in) {
  248. data_in = NULL;
  249. break;
  250. }
  251. data_in = kmap(pages_in[page_in_index]);
  252. workspace->strm.next_in = data_in;
  253. tmp = srclen - workspace->strm.total_in;
  254. workspace->strm.avail_in = min(tmp,
  255. PAGE_CACHE_SIZE);
  256. }
  257. }
  258. if (ret != Z_STREAM_END)
  259. ret = -EIO;
  260. else
  261. ret = 0;
  262. done:
  263. zlib_inflateEnd(&workspace->strm);
  264. if (data_in)
  265. kunmap(pages_in[page_in_index]);
  266. if (!ret)
  267. btrfs_clear_biovec_end(bvec, vcnt, page_out_index, pg_offset);
  268. return ret;
  269. }
  270. static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
  271. struct page *dest_page,
  272. unsigned long start_byte,
  273. size_t srclen, size_t destlen)
  274. {
  275. struct workspace *workspace = list_entry(ws, struct workspace, list);
  276. int ret = 0;
  277. int wbits = MAX_WBITS;
  278. unsigned long bytes_left;
  279. unsigned long total_out = 0;
  280. unsigned long pg_offset = 0;
  281. char *kaddr;
  282. destlen = min_t(unsigned long, destlen, PAGE_SIZE);
  283. bytes_left = destlen;
  284. workspace->strm.next_in = data_in;
  285. workspace->strm.avail_in = srclen;
  286. workspace->strm.total_in = 0;
  287. workspace->strm.next_out = workspace->buf;
  288. workspace->strm.avail_out = PAGE_CACHE_SIZE;
  289. workspace->strm.total_out = 0;
  290. /* If it's deflate, and it's got no preset dictionary, then
  291. we can tell zlib to skip the adler32 check. */
  292. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  293. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  294. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  295. wbits = -((data_in[0] >> 4) + 8);
  296. workspace->strm.next_in += 2;
  297. workspace->strm.avail_in -= 2;
  298. }
  299. if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
  300. printk(KERN_WARNING "BTRFS: inflateInit failed\n");
  301. return -EIO;
  302. }
  303. while (bytes_left > 0) {
  304. unsigned long buf_start;
  305. unsigned long buf_offset;
  306. unsigned long bytes;
  307. ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
  308. if (ret != Z_OK && ret != Z_STREAM_END)
  309. break;
  310. buf_start = total_out;
  311. total_out = workspace->strm.total_out;
  312. if (total_out == buf_start) {
  313. ret = -EIO;
  314. break;
  315. }
  316. if (total_out <= start_byte)
  317. goto next;
  318. if (total_out > start_byte && buf_start < start_byte)
  319. buf_offset = start_byte - buf_start;
  320. else
  321. buf_offset = 0;
  322. bytes = min(PAGE_CACHE_SIZE - pg_offset,
  323. PAGE_CACHE_SIZE - buf_offset);
  324. bytes = min(bytes, bytes_left);
  325. kaddr = kmap_atomic(dest_page);
  326. memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
  327. kunmap_atomic(kaddr);
  328. pg_offset += bytes;
  329. bytes_left -= bytes;
  330. next:
  331. workspace->strm.next_out = workspace->buf;
  332. workspace->strm.avail_out = PAGE_CACHE_SIZE;
  333. }
  334. if (ret != Z_STREAM_END && bytes_left != 0)
  335. ret = -EIO;
  336. else
  337. ret = 0;
  338. zlib_inflateEnd(&workspace->strm);
  339. /*
  340. * this should only happen if zlib returned fewer bytes than we
  341. * expected. btrfs_get_block is responsible for zeroing from the
  342. * end of the inline extent (destlen) to the end of the page
  343. */
  344. if (pg_offset < destlen) {
  345. kaddr = kmap_atomic(dest_page);
  346. memset(kaddr + pg_offset, 0, destlen - pg_offset);
  347. kunmap_atomic(kaddr);
  348. }
  349. return ret;
  350. }
  351. const struct btrfs_compress_op btrfs_zlib_compress = {
  352. .alloc_workspace = zlib_alloc_workspace,
  353. .free_workspace = zlib_free_workspace,
  354. .compress_pages = zlib_compress_pages,
  355. .decompress_biovec = zlib_decompress_biovec,
  356. .decompress = zlib_decompress,
  357. };