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