zlib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 inf_strm;
  35. z_stream def_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. vfree(workspace->def_strm.workspace);
  43. vfree(workspace->inf_strm.workspace);
  44. kfree(workspace->buf);
  45. kfree(workspace);
  46. }
  47. static struct list_head *zlib_alloc_workspace(void)
  48. {
  49. struct workspace *workspace;
  50. workspace = kzalloc(sizeof(*workspace), GFP_NOFS);
  51. if (!workspace)
  52. return ERR_PTR(-ENOMEM);
  53. workspace->def_strm.workspace = vmalloc(zlib_deflate_workspacesize(
  54. MAX_WBITS, MAX_MEM_LEVEL));
  55. workspace->inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
  56. workspace->buf = kmalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  57. if (!workspace->def_strm.workspace ||
  58. !workspace->inf_strm.workspace || !workspace->buf)
  59. goto fail;
  60. INIT_LIST_HEAD(&workspace->list);
  61. return &workspace->list;
  62. fail:
  63. zlib_free_workspace(&workspace->list);
  64. return ERR_PTR(-ENOMEM);
  65. }
  66. static int zlib_compress_pages(struct list_head *ws,
  67. struct address_space *mapping,
  68. u64 start, unsigned long len,
  69. struct page **pages,
  70. unsigned long nr_dest_pages,
  71. unsigned long *out_pages,
  72. unsigned long *total_in,
  73. unsigned long *total_out,
  74. unsigned long max_out)
  75. {
  76. struct workspace *workspace = list_entry(ws, struct workspace, list);
  77. int ret;
  78. char *data_in;
  79. char *cpage_out;
  80. int nr_pages = 0;
  81. struct page *in_page = NULL;
  82. struct page *out_page = NULL;
  83. unsigned long bytes_left;
  84. *out_pages = 0;
  85. *total_out = 0;
  86. *total_in = 0;
  87. if (Z_OK != zlib_deflateInit(&workspace->def_strm, 3)) {
  88. printk(KERN_WARNING "deflateInit failed\n");
  89. ret = -1;
  90. goto out;
  91. }
  92. workspace->def_strm.total_in = 0;
  93. workspace->def_strm.total_out = 0;
  94. in_page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  95. data_in = kmap(in_page);
  96. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  97. if (out_page == NULL) {
  98. ret = -1;
  99. goto out;
  100. }
  101. cpage_out = kmap(out_page);
  102. pages[0] = out_page;
  103. nr_pages = 1;
  104. workspace->def_strm.next_in = data_in;
  105. workspace->def_strm.next_out = cpage_out;
  106. workspace->def_strm.avail_out = PAGE_CACHE_SIZE;
  107. workspace->def_strm.avail_in = min(len, PAGE_CACHE_SIZE);
  108. while (workspace->def_strm.total_in < len) {
  109. ret = zlib_deflate(&workspace->def_strm, Z_SYNC_FLUSH);
  110. if (ret != Z_OK) {
  111. printk(KERN_DEBUG "btrfs deflate in loop returned %d\n",
  112. ret);
  113. zlib_deflateEnd(&workspace->def_strm);
  114. ret = -1;
  115. goto out;
  116. }
  117. /* we're making it bigger, give up */
  118. if (workspace->def_strm.total_in > 8192 &&
  119. workspace->def_strm.total_in <
  120. workspace->def_strm.total_out) {
  121. ret = -1;
  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->def_strm.avail_out == 0) {
  129. kunmap(out_page);
  130. if (nr_pages == nr_dest_pages) {
  131. out_page = NULL;
  132. ret = -1;
  133. goto out;
  134. }
  135. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  136. if (out_page == NULL) {
  137. ret = -1;
  138. goto out;
  139. }
  140. cpage_out = kmap(out_page);
  141. pages[nr_pages] = out_page;
  142. nr_pages++;
  143. workspace->def_strm.avail_out = PAGE_CACHE_SIZE;
  144. workspace->def_strm.next_out = cpage_out;
  145. }
  146. /* we're all done */
  147. if (workspace->def_strm.total_in >= len)
  148. break;
  149. /* we've read in a full page, get a new one */
  150. if (workspace->def_strm.avail_in == 0) {
  151. if (workspace->def_strm.total_out > max_out)
  152. break;
  153. bytes_left = len - workspace->def_strm.total_in;
  154. kunmap(in_page);
  155. page_cache_release(in_page);
  156. start += PAGE_CACHE_SIZE;
  157. in_page = find_get_page(mapping,
  158. start >> PAGE_CACHE_SHIFT);
  159. data_in = kmap(in_page);
  160. workspace->def_strm.avail_in = min(bytes_left,
  161. PAGE_CACHE_SIZE);
  162. workspace->def_strm.next_in = data_in;
  163. }
  164. }
  165. workspace->def_strm.avail_in = 0;
  166. ret = zlib_deflate(&workspace->def_strm, Z_FINISH);
  167. zlib_deflateEnd(&workspace->def_strm);
  168. if (ret != Z_STREAM_END) {
  169. ret = -1;
  170. goto out;
  171. }
  172. if (workspace->def_strm.total_out >= workspace->def_strm.total_in) {
  173. ret = -1;
  174. goto out;
  175. }
  176. ret = 0;
  177. *total_out = workspace->def_strm.total_out;
  178. *total_in = workspace->def_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. page_cache_release(in_page);
  186. }
  187. return ret;
  188. }
  189. static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
  190. u64 disk_start,
  191. struct bio_vec *bvec,
  192. int vcnt,
  193. size_t srclen)
  194. {
  195. struct workspace *workspace = list_entry(ws, struct workspace, list);
  196. int ret = 0, ret2;
  197. int wbits = MAX_WBITS;
  198. char *data_in;
  199. size_t total_out = 0;
  200. unsigned long page_in_index = 0;
  201. unsigned long page_out_index = 0;
  202. unsigned long total_pages_in = (srclen + PAGE_CACHE_SIZE - 1) /
  203. PAGE_CACHE_SIZE;
  204. unsigned long buf_start;
  205. unsigned long pg_offset;
  206. data_in = kmap(pages_in[page_in_index]);
  207. workspace->inf_strm.next_in = data_in;
  208. workspace->inf_strm.avail_in = min_t(size_t, srclen, PAGE_CACHE_SIZE);
  209. workspace->inf_strm.total_in = 0;
  210. workspace->inf_strm.total_out = 0;
  211. workspace->inf_strm.next_out = workspace->buf;
  212. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  213. pg_offset = 0;
  214. /* If it's deflate, and it's got no preset dictionary, then
  215. we can tell zlib to skip the adler32 check. */
  216. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  217. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  218. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  219. wbits = -((data_in[0] >> 4) + 8);
  220. workspace->inf_strm.next_in += 2;
  221. workspace->inf_strm.avail_in -= 2;
  222. }
  223. if (Z_OK != zlib_inflateInit2(&workspace->inf_strm, wbits)) {
  224. printk(KERN_WARNING "inflateInit failed\n");
  225. return -1;
  226. }
  227. while (workspace->inf_strm.total_in < srclen) {
  228. ret = zlib_inflate(&workspace->inf_strm, Z_NO_FLUSH);
  229. if (ret != Z_OK && ret != Z_STREAM_END)
  230. break;
  231. buf_start = total_out;
  232. total_out = workspace->inf_strm.total_out;
  233. /* we didn't make progress in this inflate call, we're done */
  234. if (buf_start == total_out)
  235. break;
  236. ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
  237. total_out, disk_start,
  238. bvec, vcnt,
  239. &page_out_index, &pg_offset);
  240. if (ret2 == 0) {
  241. ret = 0;
  242. goto done;
  243. }
  244. workspace->inf_strm.next_out = workspace->buf;
  245. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  246. if (workspace->inf_strm.avail_in == 0) {
  247. unsigned long tmp;
  248. kunmap(pages_in[page_in_index]);
  249. page_in_index++;
  250. if (page_in_index >= total_pages_in) {
  251. data_in = NULL;
  252. break;
  253. }
  254. data_in = kmap(pages_in[page_in_index]);
  255. workspace->inf_strm.next_in = data_in;
  256. tmp = srclen - workspace->inf_strm.total_in;
  257. workspace->inf_strm.avail_in = min(tmp,
  258. PAGE_CACHE_SIZE);
  259. }
  260. }
  261. if (ret != Z_STREAM_END)
  262. ret = -1;
  263. else
  264. ret = 0;
  265. done:
  266. zlib_inflateEnd(&workspace->inf_strm);
  267. if (data_in)
  268. kunmap(pages_in[page_in_index]);
  269. return ret;
  270. }
  271. static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
  272. struct page *dest_page,
  273. unsigned long start_byte,
  274. size_t srclen, size_t destlen)
  275. {
  276. struct workspace *workspace = list_entry(ws, struct workspace, list);
  277. int ret = 0;
  278. int wbits = MAX_WBITS;
  279. unsigned long bytes_left = destlen;
  280. unsigned long total_out = 0;
  281. char *kaddr;
  282. workspace->inf_strm.next_in = data_in;
  283. workspace->inf_strm.avail_in = srclen;
  284. workspace->inf_strm.total_in = 0;
  285. workspace->inf_strm.next_out = workspace->buf;
  286. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  287. workspace->inf_strm.total_out = 0;
  288. /* If it's deflate, and it's got no preset dictionary, then
  289. we can tell zlib to skip the adler32 check. */
  290. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  291. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  292. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  293. wbits = -((data_in[0] >> 4) + 8);
  294. workspace->inf_strm.next_in += 2;
  295. workspace->inf_strm.avail_in -= 2;
  296. }
  297. if (Z_OK != zlib_inflateInit2(&workspace->inf_strm, wbits)) {
  298. printk(KERN_WARNING "inflateInit failed\n");
  299. return -1;
  300. }
  301. while (bytes_left > 0) {
  302. unsigned long buf_start;
  303. unsigned long buf_offset;
  304. unsigned long bytes;
  305. unsigned long pg_offset = 0;
  306. ret = zlib_inflate(&workspace->inf_strm, Z_NO_FLUSH);
  307. if (ret != Z_OK && ret != Z_STREAM_END)
  308. break;
  309. buf_start = total_out;
  310. total_out = workspace->inf_strm.total_out;
  311. if (total_out == buf_start) {
  312. ret = -1;
  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_CACHE_SIZE - pg_offset,
  322. PAGE_CACHE_SIZE - buf_offset);
  323. bytes = min(bytes, bytes_left);
  324. kaddr = kmap_atomic(dest_page, KM_USER0);
  325. memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
  326. kunmap_atomic(kaddr, KM_USER0);
  327. pg_offset += bytes;
  328. bytes_left -= bytes;
  329. next:
  330. workspace->inf_strm.next_out = workspace->buf;
  331. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  332. }
  333. if (ret != Z_STREAM_END && bytes_left != 0)
  334. ret = -1;
  335. else
  336. ret = 0;
  337. zlib_inflateEnd(&workspace->inf_strm);
  338. return ret;
  339. }
  340. struct btrfs_compress_op btrfs_zlib_compress = {
  341. .alloc_workspace = zlib_alloc_workspace,
  342. .free_workspace = zlib_free_workspace,
  343. .compress_pages = zlib_compress_pages,
  344. .decompress_biovec = zlib_decompress_biovec,
  345. .decompress = zlib_decompress,
  346. };