lzo.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/sched.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/bio.h>
  26. #include <linux/lzo.h>
  27. #include "compression.h"
  28. #define LZO_LEN 4
  29. struct workspace {
  30. void *mem;
  31. void *buf; /* where compressed data goes */
  32. void *cbuf; /* where decompressed data goes */
  33. struct list_head list;
  34. };
  35. static void lzo_free_workspace(struct list_head *ws)
  36. {
  37. struct workspace *workspace = list_entry(ws, struct workspace, list);
  38. vfree(workspace->buf);
  39. vfree(workspace->cbuf);
  40. vfree(workspace->mem);
  41. kfree(workspace);
  42. }
  43. static struct list_head *lzo_alloc_workspace(void)
  44. {
  45. struct workspace *workspace;
  46. workspace = kzalloc(sizeof(*workspace), GFP_NOFS);
  47. if (!workspace)
  48. return ERR_PTR(-ENOMEM);
  49. workspace->mem = vmalloc(LZO1X_MEM_COMPRESS);
  50. workspace->buf = vmalloc(lzo1x_worst_compress(PAGE_CACHE_SIZE));
  51. workspace->cbuf = vmalloc(lzo1x_worst_compress(PAGE_CACHE_SIZE));
  52. if (!workspace->mem || !workspace->buf || !workspace->cbuf)
  53. goto fail;
  54. INIT_LIST_HEAD(&workspace->list);
  55. return &workspace->list;
  56. fail:
  57. lzo_free_workspace(&workspace->list);
  58. return ERR_PTR(-ENOMEM);
  59. }
  60. static inline void write_compress_length(char *buf, size_t len)
  61. {
  62. __le32 dlen;
  63. dlen = cpu_to_le32(len);
  64. memcpy(buf, &dlen, LZO_LEN);
  65. }
  66. static inline size_t read_compress_length(char *buf)
  67. {
  68. __le32 dlen;
  69. memcpy(&dlen, buf, LZO_LEN);
  70. return le32_to_cpu(dlen);
  71. }
  72. static int lzo_compress_pages(struct list_head *ws,
  73. struct address_space *mapping,
  74. u64 start, unsigned long len,
  75. struct page **pages,
  76. unsigned long nr_dest_pages,
  77. unsigned long *out_pages,
  78. unsigned long *total_in,
  79. unsigned long *total_out,
  80. unsigned long max_out)
  81. {
  82. struct workspace *workspace = list_entry(ws, struct workspace, list);
  83. int ret = 0;
  84. char *data_in;
  85. char *cpage_out;
  86. int nr_pages = 0;
  87. struct page *in_page = NULL;
  88. struct page *out_page = NULL;
  89. unsigned long bytes_left;
  90. size_t in_len;
  91. size_t out_len;
  92. char *buf;
  93. unsigned long tot_in = 0;
  94. unsigned long tot_out = 0;
  95. unsigned long pg_bytes_left;
  96. unsigned long out_offset;
  97. unsigned long bytes;
  98. *out_pages = 0;
  99. *total_out = 0;
  100. *total_in = 0;
  101. in_page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  102. data_in = kmap(in_page);
  103. /*
  104. * store the size of all chunks of compressed data in
  105. * the first 4 bytes
  106. */
  107. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  108. if (out_page == NULL) {
  109. ret = -ENOMEM;
  110. goto out;
  111. }
  112. cpage_out = kmap(out_page);
  113. out_offset = LZO_LEN;
  114. tot_out = LZO_LEN;
  115. pages[0] = out_page;
  116. nr_pages = 1;
  117. pg_bytes_left = PAGE_CACHE_SIZE - LZO_LEN;
  118. /* compress at most one page of data each time */
  119. in_len = min(len, PAGE_CACHE_SIZE);
  120. while (tot_in < len) {
  121. ret = lzo1x_1_compress(data_in, in_len, workspace->cbuf,
  122. &out_len, workspace->mem);
  123. if (ret != LZO_E_OK) {
  124. printk(KERN_DEBUG "btrfs deflate in loop returned %d\n",
  125. ret);
  126. ret = -1;
  127. goto out;
  128. }
  129. /* store the size of this chunk of compressed data */
  130. write_compress_length(cpage_out + out_offset, out_len);
  131. tot_out += LZO_LEN;
  132. out_offset += LZO_LEN;
  133. pg_bytes_left -= LZO_LEN;
  134. tot_in += in_len;
  135. tot_out += out_len;
  136. /* copy bytes from the working buffer into the pages */
  137. buf = workspace->cbuf;
  138. while (out_len) {
  139. bytes = min_t(unsigned long, pg_bytes_left, out_len);
  140. memcpy(cpage_out + out_offset, buf, bytes);
  141. out_len -= bytes;
  142. pg_bytes_left -= bytes;
  143. buf += bytes;
  144. out_offset += bytes;
  145. /*
  146. * we need another page for writing out.
  147. *
  148. * Note if there's less than 4 bytes left, we just
  149. * skip to a new page.
  150. */
  151. if ((out_len == 0 && pg_bytes_left < LZO_LEN) ||
  152. pg_bytes_left == 0) {
  153. if (pg_bytes_left) {
  154. memset(cpage_out + out_offset, 0,
  155. pg_bytes_left);
  156. tot_out += pg_bytes_left;
  157. }
  158. /* we're done, don't allocate new page */
  159. if (out_len == 0 && tot_in >= len)
  160. break;
  161. kunmap(out_page);
  162. if (nr_pages == nr_dest_pages) {
  163. out_page = NULL;
  164. ret = -1;
  165. goto out;
  166. }
  167. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  168. if (out_page == NULL) {
  169. ret = -ENOMEM;
  170. goto out;
  171. }
  172. cpage_out = kmap(out_page);
  173. pages[nr_pages++] = out_page;
  174. pg_bytes_left = PAGE_CACHE_SIZE;
  175. out_offset = 0;
  176. }
  177. }
  178. /* we're making it bigger, give up */
  179. if (tot_in > 8192 && tot_in < tot_out)
  180. goto out;
  181. /* we're all done */
  182. if (tot_in >= len)
  183. break;
  184. if (tot_out > max_out)
  185. break;
  186. bytes_left = len - tot_in;
  187. kunmap(in_page);
  188. page_cache_release(in_page);
  189. start += PAGE_CACHE_SIZE;
  190. in_page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  191. data_in = kmap(in_page);
  192. in_len = min(bytes_left, PAGE_CACHE_SIZE);
  193. }
  194. if (tot_out > tot_in)
  195. goto out;
  196. /* store the size of all chunks of compressed data */
  197. cpage_out = kmap(pages[0]);
  198. write_compress_length(cpage_out, tot_out);
  199. kunmap(pages[0]);
  200. ret = 0;
  201. *total_out = tot_out;
  202. *total_in = tot_in;
  203. out:
  204. *out_pages = nr_pages;
  205. if (out_page)
  206. kunmap(out_page);
  207. if (in_page) {
  208. kunmap(in_page);
  209. page_cache_release(in_page);
  210. }
  211. return ret;
  212. }
  213. static int lzo_decompress_biovec(struct list_head *ws,
  214. struct page **pages_in,
  215. u64 disk_start,
  216. struct bio_vec *bvec,
  217. int vcnt,
  218. size_t srclen)
  219. {
  220. struct workspace *workspace = list_entry(ws, struct workspace, list);
  221. int ret = 0, ret2;
  222. char *data_in;
  223. unsigned long page_in_index = 0;
  224. unsigned long page_out_index = 0;
  225. unsigned long total_pages_in = (srclen + PAGE_CACHE_SIZE - 1) /
  226. PAGE_CACHE_SIZE;
  227. unsigned long buf_start;
  228. unsigned long buf_offset = 0;
  229. unsigned long bytes;
  230. unsigned long working_bytes;
  231. unsigned long pg_offset;
  232. size_t in_len;
  233. size_t out_len;
  234. unsigned long in_offset;
  235. unsigned long in_page_bytes_left;
  236. unsigned long tot_in;
  237. unsigned long tot_out;
  238. unsigned long tot_len;
  239. char *buf;
  240. bool may_late_unmap, need_unmap;
  241. data_in = kmap(pages_in[0]);
  242. tot_len = read_compress_length(data_in);
  243. tot_in = LZO_LEN;
  244. in_offset = LZO_LEN;
  245. tot_len = min_t(size_t, srclen, tot_len);
  246. in_page_bytes_left = PAGE_CACHE_SIZE - LZO_LEN;
  247. tot_out = 0;
  248. pg_offset = 0;
  249. while (tot_in < tot_len) {
  250. in_len = read_compress_length(data_in + in_offset);
  251. in_page_bytes_left -= LZO_LEN;
  252. in_offset += LZO_LEN;
  253. tot_in += LZO_LEN;
  254. tot_in += in_len;
  255. working_bytes = in_len;
  256. may_late_unmap = need_unmap = false;
  257. /* fast path: avoid using the working buffer */
  258. if (in_page_bytes_left >= in_len) {
  259. buf = data_in + in_offset;
  260. bytes = in_len;
  261. may_late_unmap = true;
  262. goto cont;
  263. }
  264. /* copy bytes from the pages into the working buffer */
  265. buf = workspace->cbuf;
  266. buf_offset = 0;
  267. while (working_bytes) {
  268. bytes = min(working_bytes, in_page_bytes_left);
  269. memcpy(buf + buf_offset, data_in + in_offset, bytes);
  270. buf_offset += bytes;
  271. cont:
  272. working_bytes -= bytes;
  273. in_page_bytes_left -= bytes;
  274. in_offset += bytes;
  275. /* check if we need to pick another page */
  276. if ((working_bytes == 0 && in_page_bytes_left < LZO_LEN)
  277. || in_page_bytes_left == 0) {
  278. tot_in += in_page_bytes_left;
  279. if (working_bytes == 0 && tot_in >= tot_len)
  280. break;
  281. if (page_in_index + 1 >= total_pages_in) {
  282. ret = -1;
  283. goto done;
  284. }
  285. if (may_late_unmap)
  286. need_unmap = true;
  287. else
  288. kunmap(pages_in[page_in_index]);
  289. data_in = kmap(pages_in[++page_in_index]);
  290. in_page_bytes_left = PAGE_CACHE_SIZE;
  291. in_offset = 0;
  292. }
  293. }
  294. out_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
  295. ret = lzo1x_decompress_safe(buf, in_len, workspace->buf,
  296. &out_len);
  297. if (need_unmap)
  298. kunmap(pages_in[page_in_index - 1]);
  299. if (ret != LZO_E_OK) {
  300. printk(KERN_WARNING "btrfs decompress failed\n");
  301. ret = -1;
  302. break;
  303. }
  304. buf_start = tot_out;
  305. tot_out += out_len;
  306. ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
  307. tot_out, disk_start,
  308. bvec, vcnt,
  309. &page_out_index, &pg_offset);
  310. if (ret2 == 0)
  311. break;
  312. }
  313. done:
  314. kunmap(pages_in[page_in_index]);
  315. return ret;
  316. }
  317. static int lzo_decompress(struct list_head *ws, unsigned char *data_in,
  318. struct page *dest_page,
  319. unsigned long start_byte,
  320. size_t srclen, size_t destlen)
  321. {
  322. struct workspace *workspace = list_entry(ws, struct workspace, list);
  323. size_t in_len;
  324. size_t out_len;
  325. size_t tot_len;
  326. int ret = 0;
  327. char *kaddr;
  328. unsigned long bytes;
  329. BUG_ON(srclen < LZO_LEN);
  330. tot_len = read_compress_length(data_in);
  331. data_in += LZO_LEN;
  332. in_len = read_compress_length(data_in);
  333. data_in += LZO_LEN;
  334. out_len = PAGE_CACHE_SIZE;
  335. ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len);
  336. if (ret != LZO_E_OK) {
  337. printk(KERN_WARNING "btrfs decompress failed!\n");
  338. ret = -1;
  339. goto out;
  340. }
  341. if (out_len < start_byte) {
  342. ret = -1;
  343. goto out;
  344. }
  345. bytes = min_t(unsigned long, destlen, out_len - start_byte);
  346. kaddr = kmap_atomic(dest_page, KM_USER0);
  347. memcpy(kaddr, workspace->buf + start_byte, bytes);
  348. kunmap_atomic(kaddr, KM_USER0);
  349. out:
  350. return ret;
  351. }
  352. struct btrfs_compress_op btrfs_lzo_compress = {
  353. .alloc_workspace = lzo_alloc_workspace,
  354. .free_workspace = lzo_free_workspace,
  355. .compress_pages = lzo_compress_pages,
  356. .decompress_biovec = lzo_decompress_biovec,
  357. .decompress = lzo_decompress,
  358. };