zlib.c 10 KB

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