zlib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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;
  196. int wbits = MAX_WBITS;
  197. char *data_in;
  198. size_t total_out = 0;
  199. unsigned long page_bytes_left;
  200. unsigned long page_in_index = 0;
  201. unsigned long page_out_index = 0;
  202. struct page *page_out;
  203. unsigned long total_pages_in = (srclen + PAGE_CACHE_SIZE - 1) /
  204. PAGE_CACHE_SIZE;
  205. unsigned long buf_start;
  206. unsigned long buf_offset;
  207. unsigned long bytes;
  208. unsigned long working_bytes;
  209. unsigned long pg_offset;
  210. unsigned long start_byte;
  211. unsigned long current_buf_start;
  212. char *kaddr;
  213. data_in = kmap(pages_in[page_in_index]);
  214. workspace->inf_strm.next_in = data_in;
  215. workspace->inf_strm.avail_in = min_t(size_t, srclen, PAGE_CACHE_SIZE);
  216. workspace->inf_strm.total_in = 0;
  217. workspace->inf_strm.total_out = 0;
  218. workspace->inf_strm.next_out = workspace->buf;
  219. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  220. page_out = bvec[page_out_index].bv_page;
  221. page_bytes_left = PAGE_CACHE_SIZE;
  222. pg_offset = 0;
  223. /* If it's deflate, and it's got no preset dictionary, then
  224. we can tell zlib to skip the adler32 check. */
  225. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  226. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  227. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  228. wbits = -((data_in[0] >> 4) + 8);
  229. workspace->inf_strm.next_in += 2;
  230. workspace->inf_strm.avail_in -= 2;
  231. }
  232. if (Z_OK != zlib_inflateInit2(&workspace->inf_strm, wbits)) {
  233. printk(KERN_WARNING "inflateInit failed\n");
  234. return -1;
  235. }
  236. while (workspace->inf_strm.total_in < srclen) {
  237. ret = zlib_inflate(&workspace->inf_strm, Z_NO_FLUSH);
  238. if (ret != Z_OK && ret != Z_STREAM_END)
  239. break;
  240. /*
  241. * buf start is the byte offset we're of the start of
  242. * our workspace buffer
  243. */
  244. buf_start = total_out;
  245. /* total_out is the last byte of the workspace buffer */
  246. total_out = workspace->inf_strm.total_out;
  247. working_bytes = total_out - buf_start;
  248. /*
  249. * start byte is the first byte of the page we're currently
  250. * copying into relative to the start of the compressed data.
  251. */
  252. start_byte = page_offset(page_out) - disk_start;
  253. if (working_bytes == 0) {
  254. /* we didn't make progress in this inflate
  255. * call, we're done
  256. */
  257. if (ret != Z_STREAM_END)
  258. ret = -1;
  259. break;
  260. }
  261. /* we haven't yet hit data corresponding to this page */
  262. if (total_out <= start_byte)
  263. goto next;
  264. /*
  265. * the start of the data we care about is offset into
  266. * the middle of our working buffer
  267. */
  268. if (total_out > start_byte && buf_start < start_byte) {
  269. buf_offset = start_byte - buf_start;
  270. working_bytes -= buf_offset;
  271. } else {
  272. buf_offset = 0;
  273. }
  274. current_buf_start = buf_start;
  275. /* copy bytes from the working buffer into the pages */
  276. while (working_bytes > 0) {
  277. bytes = min(PAGE_CACHE_SIZE - pg_offset,
  278. PAGE_CACHE_SIZE - buf_offset);
  279. bytes = min(bytes, working_bytes);
  280. kaddr = kmap_atomic(page_out, KM_USER0);
  281. memcpy(kaddr + pg_offset, workspace->buf + buf_offset,
  282. bytes);
  283. kunmap_atomic(kaddr, KM_USER0);
  284. flush_dcache_page(page_out);
  285. pg_offset += bytes;
  286. page_bytes_left -= bytes;
  287. buf_offset += bytes;
  288. working_bytes -= bytes;
  289. current_buf_start += bytes;
  290. /* check if we need to pick another page */
  291. if (page_bytes_left == 0) {
  292. page_out_index++;
  293. if (page_out_index >= vcnt) {
  294. ret = 0;
  295. goto done;
  296. }
  297. page_out = bvec[page_out_index].bv_page;
  298. pg_offset = 0;
  299. page_bytes_left = PAGE_CACHE_SIZE;
  300. start_byte = page_offset(page_out) - disk_start;
  301. /*
  302. * make sure our new page is covered by this
  303. * working buffer
  304. */
  305. if (total_out <= start_byte)
  306. goto next;
  307. /* the next page in the biovec might not
  308. * be adjacent to the last page, but it
  309. * might still be found inside this working
  310. * buffer. bump our offset pointer
  311. */
  312. if (total_out > start_byte &&
  313. current_buf_start < start_byte) {
  314. buf_offset = start_byte - buf_start;
  315. working_bytes = total_out - start_byte;
  316. current_buf_start = buf_start +
  317. buf_offset;
  318. }
  319. }
  320. }
  321. next:
  322. workspace->inf_strm.next_out = workspace->buf;
  323. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  324. if (workspace->inf_strm.avail_in == 0) {
  325. unsigned long tmp;
  326. kunmap(pages_in[page_in_index]);
  327. page_in_index++;
  328. if (page_in_index >= total_pages_in) {
  329. data_in = NULL;
  330. break;
  331. }
  332. data_in = kmap(pages_in[page_in_index]);
  333. workspace->inf_strm.next_in = data_in;
  334. tmp = srclen - workspace->inf_strm.total_in;
  335. workspace->inf_strm.avail_in = min(tmp,
  336. PAGE_CACHE_SIZE);
  337. }
  338. }
  339. if (ret != Z_STREAM_END)
  340. ret = -1;
  341. else
  342. ret = 0;
  343. done:
  344. zlib_inflateEnd(&workspace->inf_strm);
  345. if (data_in)
  346. kunmap(pages_in[page_in_index]);
  347. return ret;
  348. }
  349. static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
  350. struct page *dest_page,
  351. unsigned long start_byte,
  352. size_t srclen, size_t destlen)
  353. {
  354. struct workspace *workspace = list_entry(ws, struct workspace, list);
  355. int ret = 0;
  356. int wbits = MAX_WBITS;
  357. unsigned long bytes_left = destlen;
  358. unsigned long total_out = 0;
  359. char *kaddr;
  360. workspace->inf_strm.next_in = data_in;
  361. workspace->inf_strm.avail_in = srclen;
  362. workspace->inf_strm.total_in = 0;
  363. workspace->inf_strm.next_out = workspace->buf;
  364. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  365. workspace->inf_strm.total_out = 0;
  366. /* If it's deflate, and it's got no preset dictionary, then
  367. we can tell zlib to skip the adler32 check. */
  368. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  369. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  370. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  371. wbits = -((data_in[0] >> 4) + 8);
  372. workspace->inf_strm.next_in += 2;
  373. workspace->inf_strm.avail_in -= 2;
  374. }
  375. if (Z_OK != zlib_inflateInit2(&workspace->inf_strm, wbits)) {
  376. printk(KERN_WARNING "inflateInit failed\n");
  377. return -1;
  378. }
  379. while (bytes_left > 0) {
  380. unsigned long buf_start;
  381. unsigned long buf_offset;
  382. unsigned long bytes;
  383. unsigned long pg_offset = 0;
  384. ret = zlib_inflate(&workspace->inf_strm, Z_NO_FLUSH);
  385. if (ret != Z_OK && ret != Z_STREAM_END)
  386. break;
  387. buf_start = total_out;
  388. total_out = workspace->inf_strm.total_out;
  389. if (total_out == buf_start) {
  390. ret = -1;
  391. break;
  392. }
  393. if (total_out <= start_byte)
  394. goto next;
  395. if (total_out > start_byte && buf_start < start_byte)
  396. buf_offset = start_byte - buf_start;
  397. else
  398. buf_offset = 0;
  399. bytes = min(PAGE_CACHE_SIZE - pg_offset,
  400. PAGE_CACHE_SIZE - buf_offset);
  401. bytes = min(bytes, bytes_left);
  402. kaddr = kmap_atomic(dest_page, KM_USER0);
  403. memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
  404. kunmap_atomic(kaddr, KM_USER0);
  405. pg_offset += bytes;
  406. bytes_left -= bytes;
  407. next:
  408. workspace->inf_strm.next_out = workspace->buf;
  409. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  410. }
  411. if (ret != Z_STREAM_END && bytes_left != 0)
  412. ret = -1;
  413. else
  414. ret = 0;
  415. zlib_inflateEnd(&workspace->inf_strm);
  416. return ret;
  417. }
  418. struct btrfs_compress_op btrfs_zlib_compress = {
  419. .alloc_workspace = zlib_alloc_workspace,
  420. .free_workspace = zlib_free_workspace,
  421. .compress_pages = zlib_compress_pages,
  422. .decompress_biovec = zlib_decompress_biovec,
  423. .decompress = zlib_decompress,
  424. };