lzo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_SIZE));
  51. workspace->cbuf = vmalloc(lzo1x_worst_compress(PAGE_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(const 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,
  75. struct page **pages,
  76. unsigned long *out_pages,
  77. unsigned long *total_in,
  78. unsigned long *total_out)
  79. {
  80. struct workspace *workspace = list_entry(ws, struct workspace, list);
  81. int ret = 0;
  82. char *data_in;
  83. char *cpage_out;
  84. int nr_pages = 0;
  85. struct page *in_page = NULL;
  86. struct page *out_page = NULL;
  87. unsigned long bytes_left;
  88. unsigned long len = *total_out;
  89. unsigned long nr_dest_pages = *out_pages;
  90. const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
  91. size_t in_len;
  92. size_t out_len;
  93. char *buf;
  94. unsigned long tot_in = 0;
  95. unsigned long tot_out = 0;
  96. unsigned long pg_bytes_left;
  97. unsigned long out_offset;
  98. unsigned long bytes;
  99. *out_pages = 0;
  100. *total_out = 0;
  101. *total_in = 0;
  102. in_page = find_get_page(mapping, start >> PAGE_SHIFT);
  103. data_in = kmap(in_page);
  104. /*
  105. * store the size of all chunks of compressed data in
  106. * the first 4 bytes
  107. */
  108. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  109. if (out_page == NULL) {
  110. ret = -ENOMEM;
  111. goto out;
  112. }
  113. cpage_out = kmap(out_page);
  114. out_offset = LZO_LEN;
  115. tot_out = LZO_LEN;
  116. pages[0] = out_page;
  117. nr_pages = 1;
  118. pg_bytes_left = PAGE_SIZE - LZO_LEN;
  119. /* compress at most one page of data each time */
  120. in_len = min(len, PAGE_SIZE);
  121. while (tot_in < len) {
  122. ret = lzo1x_1_compress(data_in, in_len, workspace->cbuf,
  123. &out_len, workspace->mem);
  124. if (ret != LZO_E_OK) {
  125. pr_debug("BTRFS: deflate in loop returned %d\n",
  126. ret);
  127. ret = -EIO;
  128. goto out;
  129. }
  130. /* store the size of this chunk of compressed data */
  131. write_compress_length(cpage_out + out_offset, out_len);
  132. tot_out += LZO_LEN;
  133. out_offset += LZO_LEN;
  134. pg_bytes_left -= LZO_LEN;
  135. tot_in += in_len;
  136. tot_out += out_len;
  137. /* copy bytes from the working buffer into the pages */
  138. buf = workspace->cbuf;
  139. while (out_len) {
  140. bytes = min_t(unsigned long, pg_bytes_left, out_len);
  141. memcpy(cpage_out + out_offset, buf, bytes);
  142. out_len -= bytes;
  143. pg_bytes_left -= bytes;
  144. buf += bytes;
  145. out_offset += bytes;
  146. /*
  147. * we need another page for writing out.
  148. *
  149. * Note if there's less than 4 bytes left, we just
  150. * skip to a new page.
  151. */
  152. if ((out_len == 0 && pg_bytes_left < LZO_LEN) ||
  153. pg_bytes_left == 0) {
  154. if (pg_bytes_left) {
  155. memset(cpage_out + out_offset, 0,
  156. pg_bytes_left);
  157. tot_out += pg_bytes_left;
  158. }
  159. /* we're done, don't allocate new page */
  160. if (out_len == 0 && tot_in >= len)
  161. break;
  162. kunmap(out_page);
  163. if (nr_pages == nr_dest_pages) {
  164. out_page = NULL;
  165. ret = -E2BIG;
  166. goto out;
  167. }
  168. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  169. if (out_page == NULL) {
  170. ret = -ENOMEM;
  171. goto out;
  172. }
  173. cpage_out = kmap(out_page);
  174. pages[nr_pages++] = out_page;
  175. pg_bytes_left = PAGE_SIZE;
  176. out_offset = 0;
  177. }
  178. }
  179. /* we're making it bigger, give up */
  180. if (tot_in > 8192 && tot_in < tot_out) {
  181. ret = -E2BIG;
  182. goto out;
  183. }
  184. /* we're all done */
  185. if (tot_in >= len)
  186. break;
  187. if (tot_out > max_out)
  188. break;
  189. bytes_left = len - tot_in;
  190. kunmap(in_page);
  191. put_page(in_page);
  192. start += PAGE_SIZE;
  193. in_page = find_get_page(mapping, start >> PAGE_SHIFT);
  194. data_in = kmap(in_page);
  195. in_len = min(bytes_left, PAGE_SIZE);
  196. }
  197. if (tot_out > tot_in)
  198. goto out;
  199. /* store the size of all chunks of compressed data */
  200. cpage_out = kmap(pages[0]);
  201. write_compress_length(cpage_out, tot_out);
  202. kunmap(pages[0]);
  203. ret = 0;
  204. *total_out = tot_out;
  205. *total_in = tot_in;
  206. out:
  207. *out_pages = nr_pages;
  208. if (out_page)
  209. kunmap(out_page);
  210. if (in_page) {
  211. kunmap(in_page);
  212. put_page(in_page);
  213. }
  214. return ret;
  215. }
  216. static int lzo_decompress_bio(struct list_head *ws,
  217. struct page **pages_in,
  218. u64 disk_start,
  219. struct bio *orig_bio,
  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 total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
  227. unsigned long buf_start;
  228. unsigned long buf_offset = 0;
  229. unsigned long bytes;
  230. unsigned long working_bytes;
  231. size_t in_len;
  232. size_t out_len;
  233. unsigned long in_offset;
  234. unsigned long in_page_bytes_left;
  235. unsigned long tot_in;
  236. unsigned long tot_out;
  237. unsigned long tot_len;
  238. char *buf;
  239. bool may_late_unmap, need_unmap;
  240. data_in = kmap(pages_in[0]);
  241. tot_len = read_compress_length(data_in);
  242. tot_in = LZO_LEN;
  243. in_offset = LZO_LEN;
  244. tot_len = min_t(size_t, srclen, tot_len);
  245. in_page_bytes_left = PAGE_SIZE - LZO_LEN;
  246. tot_out = 0;
  247. while (tot_in < tot_len) {
  248. in_len = read_compress_length(data_in + in_offset);
  249. in_page_bytes_left -= LZO_LEN;
  250. in_offset += LZO_LEN;
  251. tot_in += LZO_LEN;
  252. tot_in += in_len;
  253. working_bytes = in_len;
  254. may_late_unmap = need_unmap = false;
  255. /* fast path: avoid using the working buffer */
  256. if (in_page_bytes_left >= in_len) {
  257. buf = data_in + in_offset;
  258. bytes = in_len;
  259. may_late_unmap = true;
  260. goto cont;
  261. }
  262. /* copy bytes from the pages into the working buffer */
  263. buf = workspace->cbuf;
  264. buf_offset = 0;
  265. while (working_bytes) {
  266. bytes = min(working_bytes, in_page_bytes_left);
  267. memcpy(buf + buf_offset, data_in + in_offset, bytes);
  268. buf_offset += bytes;
  269. cont:
  270. working_bytes -= bytes;
  271. in_page_bytes_left -= bytes;
  272. in_offset += bytes;
  273. /* check if we need to pick another page */
  274. if ((working_bytes == 0 && in_page_bytes_left < LZO_LEN)
  275. || in_page_bytes_left == 0) {
  276. tot_in += in_page_bytes_left;
  277. if (working_bytes == 0 && tot_in >= tot_len)
  278. break;
  279. if (page_in_index + 1 >= total_pages_in) {
  280. ret = -EIO;
  281. goto done;
  282. }
  283. if (may_late_unmap)
  284. need_unmap = true;
  285. else
  286. kunmap(pages_in[page_in_index]);
  287. data_in = kmap(pages_in[++page_in_index]);
  288. in_page_bytes_left = PAGE_SIZE;
  289. in_offset = 0;
  290. }
  291. }
  292. out_len = lzo1x_worst_compress(PAGE_SIZE);
  293. ret = lzo1x_decompress_safe(buf, in_len, workspace->buf,
  294. &out_len);
  295. if (need_unmap)
  296. kunmap(pages_in[page_in_index - 1]);
  297. if (ret != LZO_E_OK) {
  298. pr_warn("BTRFS: decompress failed\n");
  299. ret = -EIO;
  300. break;
  301. }
  302. buf_start = tot_out;
  303. tot_out += out_len;
  304. ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
  305. tot_out, disk_start, orig_bio);
  306. if (ret2 == 0)
  307. break;
  308. }
  309. done:
  310. kunmap(pages_in[page_in_index]);
  311. if (!ret)
  312. zero_fill_bio(orig_bio);
  313. return ret;
  314. }
  315. static int lzo_decompress(struct list_head *ws, unsigned char *data_in,
  316. struct page *dest_page,
  317. unsigned long start_byte,
  318. size_t srclen, size_t destlen)
  319. {
  320. struct workspace *workspace = list_entry(ws, struct workspace, list);
  321. size_t in_len;
  322. size_t out_len;
  323. size_t tot_len;
  324. int ret = 0;
  325. char *kaddr;
  326. unsigned long bytes;
  327. BUG_ON(srclen < LZO_LEN);
  328. tot_len = read_compress_length(data_in);
  329. data_in += LZO_LEN;
  330. in_len = read_compress_length(data_in);
  331. data_in += LZO_LEN;
  332. out_len = PAGE_SIZE;
  333. ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len);
  334. if (ret != LZO_E_OK) {
  335. pr_warn("BTRFS: decompress failed!\n");
  336. ret = -EIO;
  337. goto out;
  338. }
  339. if (out_len < start_byte) {
  340. ret = -EIO;
  341. goto out;
  342. }
  343. /*
  344. * the caller is already checking against PAGE_SIZE, but lets
  345. * move this check closer to the memcpy/memset
  346. */
  347. destlen = min_t(unsigned long, destlen, PAGE_SIZE);
  348. bytes = min_t(unsigned long, destlen, out_len - start_byte);
  349. kaddr = kmap_atomic(dest_page);
  350. memcpy(kaddr, workspace->buf + start_byte, bytes);
  351. /*
  352. * btrfs_getblock is doing a zero on the tail of the page too,
  353. * but this will cover anything missing from the decompressed
  354. * data.
  355. */
  356. if (bytes < destlen)
  357. memset(kaddr+bytes, 0, destlen-bytes);
  358. kunmap_atomic(kaddr);
  359. out:
  360. return ret;
  361. }
  362. const struct btrfs_compress_op btrfs_lzo_compress = {
  363. .alloc_workspace = lzo_alloc_workspace,
  364. .free_workspace = lzo_free_workspace,
  365. .compress_pages = lzo_compress_pages,
  366. .decompress_bio = lzo_decompress_bio,
  367. .decompress = lzo_decompress,
  368. };