zlib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. /* Plan: call deflate() with avail_in == *sourcelen,
  34. avail_out = *dstlen - 12 and flush == Z_FINISH.
  35. If it doesn't manage to finish, call it again with
  36. avail_in == 0 and avail_out set to the remaining 12
  37. bytes for it to clean up.
  38. Q: Is 12 bytes sufficient?
  39. */
  40. #define STREAM_END_SPACE 12
  41. struct workspace {
  42. z_stream inf_strm;
  43. z_stream def_strm;
  44. char *buf;
  45. struct list_head list;
  46. };
  47. static LIST_HEAD(idle_workspace);
  48. static DEFINE_SPINLOCK(workspace_lock);
  49. static unsigned long num_workspace;
  50. static atomic_t alloc_workspace = ATOMIC_INIT(0);
  51. static DECLARE_WAIT_QUEUE_HEAD(workspace_wait);
  52. /*
  53. * this finds an available zlib workspace or allocates a new one
  54. * NULL or an ERR_PTR is returned if things go bad.
  55. */
  56. static struct workspace *find_zlib_workspace(void)
  57. {
  58. struct workspace *workspace;
  59. int ret;
  60. int cpus = num_online_cpus();
  61. again:
  62. spin_lock(&workspace_lock);
  63. if (!list_empty(&idle_workspace)) {
  64. workspace = list_entry(idle_workspace.next, struct workspace,
  65. list);
  66. list_del(&workspace->list);
  67. num_workspace--;
  68. spin_unlock(&workspace_lock);
  69. return workspace;
  70. }
  71. if (atomic_read(&alloc_workspace) > cpus) {
  72. DEFINE_WAIT(wait);
  73. spin_unlock(&workspace_lock);
  74. prepare_to_wait(&workspace_wait, &wait, TASK_UNINTERRUPTIBLE);
  75. if (atomic_read(&alloc_workspace) > cpus && !num_workspace)
  76. schedule();
  77. finish_wait(&workspace_wait, &wait);
  78. goto again;
  79. }
  80. atomic_inc(&alloc_workspace);
  81. spin_unlock(&workspace_lock);
  82. workspace = kzalloc(sizeof(*workspace), GFP_NOFS);
  83. if (!workspace) {
  84. ret = -ENOMEM;
  85. goto fail;
  86. }
  87. workspace->def_strm.workspace = vmalloc(zlib_deflate_workspacesize());
  88. if (!workspace->def_strm.workspace) {
  89. ret = -ENOMEM;
  90. goto fail;
  91. }
  92. workspace->inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
  93. if (!workspace->inf_strm.workspace) {
  94. ret = -ENOMEM;
  95. goto fail_inflate;
  96. }
  97. workspace->buf = kmalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  98. if (!workspace->buf) {
  99. ret = -ENOMEM;
  100. goto fail_kmalloc;
  101. }
  102. return workspace;
  103. fail_kmalloc:
  104. vfree(workspace->inf_strm.workspace);
  105. fail_inflate:
  106. vfree(workspace->def_strm.workspace);
  107. fail:
  108. kfree(workspace);
  109. atomic_dec(&alloc_workspace);
  110. wake_up(&workspace_wait);
  111. return ERR_PTR(ret);
  112. }
  113. /*
  114. * put a workspace struct back on the list or free it if we have enough
  115. * idle ones sitting around
  116. */
  117. static int free_workspace(struct workspace *workspace)
  118. {
  119. spin_lock(&workspace_lock);
  120. if (num_workspace < num_online_cpus()) {
  121. list_add_tail(&workspace->list, &idle_workspace);
  122. num_workspace++;
  123. spin_unlock(&workspace_lock);
  124. if (waitqueue_active(&workspace_wait))
  125. wake_up(&workspace_wait);
  126. return 0;
  127. }
  128. spin_unlock(&workspace_lock);
  129. vfree(workspace->def_strm.workspace);
  130. vfree(workspace->inf_strm.workspace);
  131. kfree(workspace->buf);
  132. kfree(workspace);
  133. atomic_dec(&alloc_workspace);
  134. if (waitqueue_active(&workspace_wait))
  135. wake_up(&workspace_wait);
  136. return 0;
  137. }
  138. /*
  139. * cleanup function for module exit
  140. */
  141. static void free_workspaces(void)
  142. {
  143. struct workspace *workspace;
  144. while (!list_empty(&idle_workspace)) {
  145. workspace = list_entry(idle_workspace.next, struct workspace,
  146. list);
  147. list_del(&workspace->list);
  148. vfree(workspace->def_strm.workspace);
  149. vfree(workspace->inf_strm.workspace);
  150. kfree(workspace->buf);
  151. kfree(workspace);
  152. atomic_dec(&alloc_workspace);
  153. }
  154. }
  155. /*
  156. * given an address space and start/len, compress the bytes.
  157. *
  158. * pages are allocated to hold the compressed result and stored
  159. * in 'pages'
  160. *
  161. * out_pages is used to return the number of pages allocated. There
  162. * may be pages allocated even if we return an error
  163. *
  164. * total_in is used to return the number of bytes actually read. It
  165. * may be smaller then len if we had to exit early because we
  166. * ran out of room in the pages array or because we cross the
  167. * max_out threshold.
  168. *
  169. * total_out is used to return the total number of compressed bytes
  170. *
  171. * max_out tells us the max number of bytes that we're allowed to
  172. * stuff into pages
  173. */
  174. int btrfs_zlib_compress_pages(struct address_space *mapping,
  175. u64 start, unsigned long len,
  176. struct page **pages,
  177. unsigned long nr_dest_pages,
  178. unsigned long *out_pages,
  179. unsigned long *total_in,
  180. unsigned long *total_out,
  181. unsigned long max_out)
  182. {
  183. int ret;
  184. struct workspace *workspace;
  185. char *data_in;
  186. char *cpage_out;
  187. int nr_pages = 0;
  188. struct page *in_page = NULL;
  189. struct page *out_page = NULL;
  190. unsigned long bytes_left;
  191. *out_pages = 0;
  192. *total_out = 0;
  193. *total_in = 0;
  194. workspace = find_zlib_workspace();
  195. if (IS_ERR(workspace))
  196. return -1;
  197. if (Z_OK != zlib_deflateInit(&workspace->def_strm, 3)) {
  198. printk(KERN_WARNING "deflateInit failed\n");
  199. ret = -1;
  200. goto out;
  201. }
  202. workspace->def_strm.total_in = 0;
  203. workspace->def_strm.total_out = 0;
  204. in_page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  205. data_in = kmap(in_page);
  206. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  207. if (out_page == NULL) {
  208. ret = -1;
  209. goto out;
  210. }
  211. cpage_out = kmap(out_page);
  212. pages[0] = out_page;
  213. nr_pages = 1;
  214. workspace->def_strm.next_in = data_in;
  215. workspace->def_strm.next_out = cpage_out;
  216. workspace->def_strm.avail_out = PAGE_CACHE_SIZE;
  217. workspace->def_strm.avail_in = min(len, PAGE_CACHE_SIZE);
  218. while (workspace->def_strm.total_in < len) {
  219. ret = zlib_deflate(&workspace->def_strm, Z_SYNC_FLUSH);
  220. if (ret != Z_OK) {
  221. printk(KERN_DEBUG "btrfs deflate in loop returned %d\n",
  222. ret);
  223. zlib_deflateEnd(&workspace->def_strm);
  224. ret = -1;
  225. goto out;
  226. }
  227. /* we're making it bigger, give up */
  228. if (workspace->def_strm.total_in > 8192 &&
  229. workspace->def_strm.total_in <
  230. workspace->def_strm.total_out) {
  231. ret = -1;
  232. goto out;
  233. }
  234. /* we need another page for writing out. Test this
  235. * before the total_in so we will pull in a new page for
  236. * the stream end if required
  237. */
  238. if (workspace->def_strm.avail_out == 0) {
  239. kunmap(out_page);
  240. if (nr_pages == nr_dest_pages) {
  241. out_page = NULL;
  242. ret = -1;
  243. goto out;
  244. }
  245. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  246. if (out_page == NULL) {
  247. ret = -1;
  248. goto out;
  249. }
  250. cpage_out = kmap(out_page);
  251. pages[nr_pages] = out_page;
  252. nr_pages++;
  253. workspace->def_strm.avail_out = PAGE_CACHE_SIZE;
  254. workspace->def_strm.next_out = cpage_out;
  255. }
  256. /* we're all done */
  257. if (workspace->def_strm.total_in >= len)
  258. break;
  259. /* we've read in a full page, get a new one */
  260. if (workspace->def_strm.avail_in == 0) {
  261. if (workspace->def_strm.total_out > max_out)
  262. break;
  263. bytes_left = len - workspace->def_strm.total_in;
  264. kunmap(in_page);
  265. page_cache_release(in_page);
  266. start += PAGE_CACHE_SIZE;
  267. in_page = find_get_page(mapping,
  268. start >> PAGE_CACHE_SHIFT);
  269. data_in = kmap(in_page);
  270. workspace->def_strm.avail_in = min(bytes_left,
  271. PAGE_CACHE_SIZE);
  272. workspace->def_strm.next_in = data_in;
  273. }
  274. }
  275. workspace->def_strm.avail_in = 0;
  276. ret = zlib_deflate(&workspace->def_strm, Z_FINISH);
  277. zlib_deflateEnd(&workspace->def_strm);
  278. if (ret != Z_STREAM_END) {
  279. ret = -1;
  280. goto out;
  281. }
  282. if (workspace->def_strm.total_out >= workspace->def_strm.total_in) {
  283. ret = -1;
  284. goto out;
  285. }
  286. ret = 0;
  287. *total_out = workspace->def_strm.total_out;
  288. *total_in = workspace->def_strm.total_in;
  289. out:
  290. *out_pages = nr_pages;
  291. if (out_page)
  292. kunmap(out_page);
  293. if (in_page) {
  294. kunmap(in_page);
  295. page_cache_release(in_page);
  296. }
  297. free_workspace(workspace);
  298. return ret;
  299. }
  300. /*
  301. * pages_in is an array of pages with compressed data.
  302. *
  303. * disk_start is the starting logical offset of this array in the file
  304. *
  305. * bvec is a bio_vec of pages from the file that we want to decompress into
  306. *
  307. * vcnt is the count of pages in the biovec
  308. *
  309. * srclen is the number of bytes in pages_in
  310. *
  311. * The basic idea is that we have a bio that was created by readpages.
  312. * The pages in the bio are for the uncompressed data, and they may not
  313. * be contiguous. They all correspond to the range of bytes covered by
  314. * the compressed extent.
  315. */
  316. int btrfs_zlib_decompress_biovec(struct page **pages_in,
  317. u64 disk_start,
  318. struct bio_vec *bvec,
  319. int vcnt,
  320. size_t srclen)
  321. {
  322. int ret = 0;
  323. int wbits = MAX_WBITS;
  324. struct workspace *workspace;
  325. char *data_in;
  326. size_t total_out = 0;
  327. unsigned long page_bytes_left;
  328. unsigned long page_in_index = 0;
  329. unsigned long page_out_index = 0;
  330. struct page *page_out;
  331. unsigned long total_pages_in = (srclen + PAGE_CACHE_SIZE - 1) /
  332. PAGE_CACHE_SIZE;
  333. unsigned long buf_start;
  334. unsigned long buf_offset;
  335. unsigned long bytes;
  336. unsigned long working_bytes;
  337. unsigned long pg_offset;
  338. unsigned long start_byte;
  339. unsigned long current_buf_start;
  340. char *kaddr;
  341. workspace = find_zlib_workspace();
  342. if (IS_ERR(workspace))
  343. return -ENOMEM;
  344. data_in = kmap(pages_in[page_in_index]);
  345. workspace->inf_strm.next_in = data_in;
  346. workspace->inf_strm.avail_in = min_t(size_t, srclen, PAGE_CACHE_SIZE);
  347. workspace->inf_strm.total_in = 0;
  348. workspace->inf_strm.total_out = 0;
  349. workspace->inf_strm.next_out = workspace->buf;
  350. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  351. page_out = bvec[page_out_index].bv_page;
  352. page_bytes_left = PAGE_CACHE_SIZE;
  353. pg_offset = 0;
  354. /* If it's deflate, and it's got no preset dictionary, then
  355. we can tell zlib to skip the adler32 check. */
  356. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  357. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  358. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  359. wbits = -((data_in[0] >> 4) + 8);
  360. workspace->inf_strm.next_in += 2;
  361. workspace->inf_strm.avail_in -= 2;
  362. }
  363. if (Z_OK != zlib_inflateInit2(&workspace->inf_strm, wbits)) {
  364. printk(KERN_WARNING "inflateInit failed\n");
  365. ret = -1;
  366. goto out;
  367. }
  368. while (workspace->inf_strm.total_in < srclen) {
  369. ret = zlib_inflate(&workspace->inf_strm, Z_NO_FLUSH);
  370. if (ret != Z_OK && ret != Z_STREAM_END)
  371. break;
  372. /*
  373. * buf start is the byte offset we're of the start of
  374. * our workspace buffer
  375. */
  376. buf_start = total_out;
  377. /* total_out is the last byte of the workspace buffer */
  378. total_out = workspace->inf_strm.total_out;
  379. working_bytes = total_out - buf_start;
  380. /*
  381. * start byte is the first byte of the page we're currently
  382. * copying into relative to the start of the compressed data.
  383. */
  384. start_byte = page_offset(page_out) - disk_start;
  385. if (working_bytes == 0) {
  386. /* we didn't make progress in this inflate
  387. * call, we're done
  388. */
  389. if (ret != Z_STREAM_END)
  390. ret = -1;
  391. break;
  392. }
  393. /* we haven't yet hit data corresponding to this page */
  394. if (total_out <= start_byte)
  395. goto next;
  396. /*
  397. * the start of the data we care about is offset into
  398. * the middle of our working buffer
  399. */
  400. if (total_out > start_byte && buf_start < start_byte) {
  401. buf_offset = start_byte - buf_start;
  402. working_bytes -= buf_offset;
  403. } else {
  404. buf_offset = 0;
  405. }
  406. current_buf_start = buf_start;
  407. /* copy bytes from the working buffer into the pages */
  408. while (working_bytes > 0) {
  409. bytes = min(PAGE_CACHE_SIZE - pg_offset,
  410. PAGE_CACHE_SIZE - buf_offset);
  411. bytes = min(bytes, working_bytes);
  412. kaddr = kmap_atomic(page_out, KM_USER0);
  413. memcpy(kaddr + pg_offset, workspace->buf + buf_offset,
  414. bytes);
  415. kunmap_atomic(kaddr, KM_USER0);
  416. flush_dcache_page(page_out);
  417. pg_offset += bytes;
  418. page_bytes_left -= bytes;
  419. buf_offset += bytes;
  420. working_bytes -= bytes;
  421. current_buf_start += bytes;
  422. /* check if we need to pick another page */
  423. if (page_bytes_left == 0) {
  424. page_out_index++;
  425. if (page_out_index >= vcnt) {
  426. ret = 0;
  427. goto done;
  428. }
  429. page_out = bvec[page_out_index].bv_page;
  430. pg_offset = 0;
  431. page_bytes_left = PAGE_CACHE_SIZE;
  432. start_byte = page_offset(page_out) - disk_start;
  433. /*
  434. * make sure our new page is covered by this
  435. * working buffer
  436. */
  437. if (total_out <= start_byte)
  438. goto next;
  439. /* the next page in the biovec might not
  440. * be adjacent to the last page, but it
  441. * might still be found inside this working
  442. * buffer. bump our offset pointer
  443. */
  444. if (total_out > start_byte &&
  445. current_buf_start < start_byte) {
  446. buf_offset = start_byte - buf_start;
  447. working_bytes = total_out - start_byte;
  448. current_buf_start = buf_start +
  449. buf_offset;
  450. }
  451. }
  452. }
  453. next:
  454. workspace->inf_strm.next_out = workspace->buf;
  455. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  456. if (workspace->inf_strm.avail_in == 0) {
  457. unsigned long tmp;
  458. kunmap(pages_in[page_in_index]);
  459. page_in_index++;
  460. if (page_in_index >= total_pages_in) {
  461. data_in = NULL;
  462. break;
  463. }
  464. data_in = kmap(pages_in[page_in_index]);
  465. workspace->inf_strm.next_in = data_in;
  466. tmp = srclen - workspace->inf_strm.total_in;
  467. workspace->inf_strm.avail_in = min(tmp,
  468. PAGE_CACHE_SIZE);
  469. }
  470. }
  471. if (ret != Z_STREAM_END)
  472. ret = -1;
  473. else
  474. ret = 0;
  475. done:
  476. zlib_inflateEnd(&workspace->inf_strm);
  477. if (data_in)
  478. kunmap(pages_in[page_in_index]);
  479. out:
  480. free_workspace(workspace);
  481. return ret;
  482. }
  483. /*
  484. * a less complex decompression routine. Our compressed data fits in a
  485. * single page, and we want to read a single page out of it.
  486. * start_byte tells us the offset into the compressed data we're interested in
  487. */
  488. int btrfs_zlib_decompress(unsigned char *data_in,
  489. struct page *dest_page,
  490. unsigned long start_byte,
  491. size_t srclen, size_t destlen)
  492. {
  493. int ret = 0;
  494. int wbits = MAX_WBITS;
  495. struct workspace *workspace;
  496. unsigned long bytes_left = destlen;
  497. unsigned long total_out = 0;
  498. char *kaddr;
  499. if (destlen > PAGE_CACHE_SIZE)
  500. return -ENOMEM;
  501. workspace = find_zlib_workspace();
  502. if (IS_ERR(workspace))
  503. return -ENOMEM;
  504. workspace->inf_strm.next_in = data_in;
  505. workspace->inf_strm.avail_in = srclen;
  506. workspace->inf_strm.total_in = 0;
  507. workspace->inf_strm.next_out = workspace->buf;
  508. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  509. workspace->inf_strm.total_out = 0;
  510. /* If it's deflate, and it's got no preset dictionary, then
  511. we can tell zlib to skip the adler32 check. */
  512. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  513. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  514. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  515. wbits = -((data_in[0] >> 4) + 8);
  516. workspace->inf_strm.next_in += 2;
  517. workspace->inf_strm.avail_in -= 2;
  518. }
  519. if (Z_OK != zlib_inflateInit2(&workspace->inf_strm, wbits)) {
  520. printk(KERN_WARNING "inflateInit failed\n");
  521. ret = -1;
  522. goto out;
  523. }
  524. while (bytes_left > 0) {
  525. unsigned long buf_start;
  526. unsigned long buf_offset;
  527. unsigned long bytes;
  528. unsigned long pg_offset = 0;
  529. ret = zlib_inflate(&workspace->inf_strm, Z_NO_FLUSH);
  530. if (ret != Z_OK && ret != Z_STREAM_END)
  531. break;
  532. buf_start = total_out;
  533. total_out = workspace->inf_strm.total_out;
  534. if (total_out == buf_start) {
  535. ret = -1;
  536. break;
  537. }
  538. if (total_out <= start_byte)
  539. goto next;
  540. if (total_out > start_byte && buf_start < start_byte)
  541. buf_offset = start_byte - buf_start;
  542. else
  543. buf_offset = 0;
  544. bytes = min(PAGE_CACHE_SIZE - pg_offset,
  545. PAGE_CACHE_SIZE - buf_offset);
  546. bytes = min(bytes, bytes_left);
  547. kaddr = kmap_atomic(dest_page, KM_USER0);
  548. memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
  549. kunmap_atomic(kaddr, KM_USER0);
  550. pg_offset += bytes;
  551. bytes_left -= bytes;
  552. next:
  553. workspace->inf_strm.next_out = workspace->buf;
  554. workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
  555. }
  556. if (ret != Z_STREAM_END && bytes_left != 0)
  557. ret = -1;
  558. else
  559. ret = 0;
  560. zlib_inflateEnd(&workspace->inf_strm);
  561. out:
  562. free_workspace(workspace);
  563. return ret;
  564. }
  565. void btrfs_zlib_exit(void)
  566. {
  567. free_workspaces();
  568. }