lzo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 decompressed data goes */
  32. void *cbuf; /* where compressed 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 = -EIO;
  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 = -E2BIG;
  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. ret = -E2BIG;
  181. goto out;
  182. }
  183. /* we're all done */
  184. if (tot_in >= len)
  185. break;
  186. if (tot_out > max_out)
  187. break;
  188. bytes_left = len - tot_in;
  189. kunmap(in_page);
  190. page_cache_release(in_page);
  191. start += PAGE_CACHE_SIZE;
  192. in_page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  193. data_in = kmap(in_page);
  194. in_len = min(bytes_left, PAGE_CACHE_SIZE);
  195. }
  196. if (tot_out > tot_in)
  197. goto out;
  198. /* store the size of all chunks of compressed data */
  199. cpage_out = kmap(pages[0]);
  200. write_compress_length(cpage_out, tot_out);
  201. kunmap(pages[0]);
  202. ret = 0;
  203. *total_out = tot_out;
  204. *total_in = tot_in;
  205. out:
  206. *out_pages = nr_pages;
  207. if (out_page)
  208. kunmap(out_page);
  209. if (in_page) {
  210. kunmap(in_page);
  211. page_cache_release(in_page);
  212. }
  213. return ret;
  214. }
  215. static int lzo_decompress_biovec(struct list_head *ws,
  216. struct page **pages_in,
  217. u64 disk_start,
  218. struct bio_vec *bvec,
  219. int vcnt,
  220. size_t srclen)
  221. {
  222. struct workspace *workspace = list_entry(ws, struct workspace, list);
  223. int ret = 0, ret2;
  224. char *data_in;
  225. unsigned long page_in_index = 0;
  226. unsigned long page_out_index = 0;
  227. unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_CACHE_SIZE);
  228. unsigned long buf_start;
  229. unsigned long buf_offset = 0;
  230. unsigned long bytes;
  231. unsigned long working_bytes;
  232. unsigned long pg_offset;
  233. size_t in_len;
  234. size_t out_len;
  235. unsigned long in_offset;
  236. unsigned long in_page_bytes_left;
  237. unsigned long tot_in;
  238. unsigned long tot_out;
  239. unsigned long tot_len;
  240. char *buf;
  241. bool may_late_unmap, need_unmap;
  242. data_in = kmap(pages_in[0]);
  243. tot_len = read_compress_length(data_in);
  244. tot_in = LZO_LEN;
  245. in_offset = LZO_LEN;
  246. tot_len = min_t(size_t, srclen, tot_len);
  247. in_page_bytes_left = PAGE_CACHE_SIZE - LZO_LEN;
  248. tot_out = 0;
  249. pg_offset = 0;
  250. while (tot_in < tot_len) {
  251. in_len = read_compress_length(data_in + in_offset);
  252. in_page_bytes_left -= LZO_LEN;
  253. in_offset += LZO_LEN;
  254. tot_in += LZO_LEN;
  255. tot_in += in_len;
  256. working_bytes = in_len;
  257. may_late_unmap = need_unmap = false;
  258. /* fast path: avoid using the working buffer */
  259. if (in_page_bytes_left >= in_len) {
  260. buf = data_in + in_offset;
  261. bytes = in_len;
  262. may_late_unmap = true;
  263. goto cont;
  264. }
  265. /* copy bytes from the pages into the working buffer */
  266. buf = workspace->cbuf;
  267. buf_offset = 0;
  268. while (working_bytes) {
  269. bytes = min(working_bytes, in_page_bytes_left);
  270. memcpy(buf + buf_offset, data_in + in_offset, bytes);
  271. buf_offset += bytes;
  272. cont:
  273. working_bytes -= bytes;
  274. in_page_bytes_left -= bytes;
  275. in_offset += bytes;
  276. /* check if we need to pick another page */
  277. if ((working_bytes == 0 && in_page_bytes_left < LZO_LEN)
  278. || in_page_bytes_left == 0) {
  279. tot_in += in_page_bytes_left;
  280. if (working_bytes == 0 && tot_in >= tot_len)
  281. break;
  282. if (page_in_index + 1 >= total_pages_in) {
  283. ret = -EIO;
  284. goto done;
  285. }
  286. if (may_late_unmap)
  287. need_unmap = true;
  288. else
  289. kunmap(pages_in[page_in_index]);
  290. data_in = kmap(pages_in[++page_in_index]);
  291. in_page_bytes_left = PAGE_CACHE_SIZE;
  292. in_offset = 0;
  293. }
  294. }
  295. out_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
  296. ret = lzo1x_decompress_safe(buf, in_len, workspace->buf,
  297. &out_len);
  298. if (need_unmap)
  299. kunmap(pages_in[page_in_index - 1]);
  300. if (ret != LZO_E_OK) {
  301. printk(KERN_WARNING "BTRFS: decompress failed\n");
  302. ret = -EIO;
  303. break;
  304. }
  305. buf_start = tot_out;
  306. tot_out += out_len;
  307. ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
  308. tot_out, disk_start,
  309. bvec, vcnt,
  310. &page_out_index, &pg_offset);
  311. if (ret2 == 0)
  312. break;
  313. }
  314. done:
  315. kunmap(pages_in[page_in_index]);
  316. if (!ret)
  317. btrfs_clear_biovec_end(bvec, vcnt, page_out_index, pg_offset);
  318. return ret;
  319. }
  320. static int lzo_decompress(struct list_head *ws, unsigned char *data_in,
  321. struct page *dest_page,
  322. unsigned long start_byte,
  323. size_t srclen, size_t destlen)
  324. {
  325. struct workspace *workspace = list_entry(ws, struct workspace, list);
  326. size_t in_len;
  327. size_t out_len;
  328. size_t tot_len;
  329. int ret = 0;
  330. char *kaddr;
  331. unsigned long bytes;
  332. BUG_ON(srclen < LZO_LEN);
  333. tot_len = read_compress_length(data_in);
  334. data_in += LZO_LEN;
  335. in_len = read_compress_length(data_in);
  336. data_in += LZO_LEN;
  337. out_len = PAGE_CACHE_SIZE;
  338. ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len);
  339. if (ret != LZO_E_OK) {
  340. printk(KERN_WARNING "BTRFS: decompress failed!\n");
  341. ret = -EIO;
  342. goto out;
  343. }
  344. if (out_len < start_byte) {
  345. ret = -EIO;
  346. goto out;
  347. }
  348. /*
  349. * the caller is already checking against PAGE_SIZE, but lets
  350. * move this check closer to the memcpy/memset
  351. */
  352. destlen = min_t(unsigned long, destlen, PAGE_SIZE);
  353. bytes = min_t(unsigned long, destlen, out_len - start_byte);
  354. kaddr = kmap_atomic(dest_page);
  355. memcpy(kaddr, workspace->buf + start_byte, bytes);
  356. /*
  357. * btrfs_getblock is doing a zero on the tail of the page too,
  358. * but this will cover anything missing from the decompressed
  359. * data.
  360. */
  361. if (bytes < destlen)
  362. memset(kaddr+bytes, 0, destlen-bytes);
  363. kunmap_atomic(kaddr);
  364. out:
  365. return ret;
  366. }
  367. const struct btrfs_compress_op btrfs_lzo_compress = {
  368. .alloc_workspace = lzo_alloc_workspace,
  369. .free_workspace = lzo_free_workspace,
  370. .compress_pages = lzo_compress_pages,
  371. .decompress_biovec = lzo_decompress_biovec,
  372. .decompress = lzo_decompress,
  373. };