lzo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/mm.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 <linux/refcount.h>
  28. #include "compression.h"
  29. #define LZO_LEN 4
  30. struct workspace {
  31. void *mem;
  32. void *buf; /* where decompressed data goes */
  33. void *cbuf; /* where compressed data goes */
  34. struct list_head list;
  35. };
  36. static void lzo_free_workspace(struct list_head *ws)
  37. {
  38. struct workspace *workspace = list_entry(ws, struct workspace, list);
  39. kvfree(workspace->buf);
  40. kvfree(workspace->cbuf);
  41. kvfree(workspace->mem);
  42. kfree(workspace);
  43. }
  44. static struct list_head *lzo_alloc_workspace(void)
  45. {
  46. struct workspace *workspace;
  47. workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
  48. if (!workspace)
  49. return ERR_PTR(-ENOMEM);
  50. workspace->mem = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
  51. workspace->buf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
  52. workspace->cbuf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
  53. if (!workspace->mem || !workspace->buf || !workspace->cbuf)
  54. goto fail;
  55. INIT_LIST_HEAD(&workspace->list);
  56. return &workspace->list;
  57. fail:
  58. lzo_free_workspace(&workspace->list);
  59. return ERR_PTR(-ENOMEM);
  60. }
  61. static inline void write_compress_length(char *buf, size_t len)
  62. {
  63. __le32 dlen;
  64. dlen = cpu_to_le32(len);
  65. memcpy(buf, &dlen, LZO_LEN);
  66. }
  67. static inline size_t read_compress_length(const char *buf)
  68. {
  69. __le32 dlen;
  70. memcpy(&dlen, buf, LZO_LEN);
  71. return le32_to_cpu(dlen);
  72. }
  73. static int lzo_compress_pages(struct list_head *ws,
  74. struct address_space *mapping,
  75. u64 start,
  76. struct page **pages,
  77. unsigned long *out_pages,
  78. unsigned long *total_in,
  79. unsigned long *total_out)
  80. {
  81. struct workspace *workspace = list_entry(ws, struct workspace, list);
  82. int ret = 0;
  83. char *data_in;
  84. char *cpage_out;
  85. int nr_pages = 0;
  86. struct page *in_page = NULL;
  87. struct page *out_page = NULL;
  88. unsigned long bytes_left;
  89. unsigned long len = *total_out;
  90. unsigned long nr_dest_pages = *out_pages;
  91. const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
  92. size_t in_len;
  93. size_t out_len;
  94. char *buf;
  95. unsigned long tot_in = 0;
  96. unsigned long tot_out = 0;
  97. unsigned long pg_bytes_left;
  98. unsigned long out_offset;
  99. unsigned long bytes;
  100. *out_pages = 0;
  101. *total_out = 0;
  102. *total_in = 0;
  103. in_page = find_get_page(mapping, start >> PAGE_SHIFT);
  104. data_in = kmap(in_page);
  105. /*
  106. * store the size of all chunks of compressed data in
  107. * the first 4 bytes
  108. */
  109. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  110. if (out_page == NULL) {
  111. ret = -ENOMEM;
  112. goto out;
  113. }
  114. cpage_out = kmap(out_page);
  115. out_offset = LZO_LEN;
  116. tot_out = LZO_LEN;
  117. pages[0] = out_page;
  118. nr_pages = 1;
  119. pg_bytes_left = PAGE_SIZE - LZO_LEN;
  120. /* compress at most one page of data each time */
  121. in_len = min(len, PAGE_SIZE);
  122. while (tot_in < len) {
  123. ret = lzo1x_1_compress(data_in, in_len, workspace->cbuf,
  124. &out_len, workspace->mem);
  125. if (ret != LZO_E_OK) {
  126. pr_debug("BTRFS: lzo in loop returned %d\n",
  127. ret);
  128. ret = -EIO;
  129. goto out;
  130. }
  131. /* store the size of this chunk of compressed data */
  132. write_compress_length(cpage_out + out_offset, out_len);
  133. tot_out += LZO_LEN;
  134. out_offset += LZO_LEN;
  135. pg_bytes_left -= LZO_LEN;
  136. tot_in += in_len;
  137. tot_out += out_len;
  138. /* copy bytes from the working buffer into the pages */
  139. buf = workspace->cbuf;
  140. while (out_len) {
  141. bytes = min_t(unsigned long, pg_bytes_left, out_len);
  142. memcpy(cpage_out + out_offset, buf, bytes);
  143. out_len -= bytes;
  144. pg_bytes_left -= bytes;
  145. buf += bytes;
  146. out_offset += bytes;
  147. /*
  148. * we need another page for writing out.
  149. *
  150. * Note if there's less than 4 bytes left, we just
  151. * skip to a new page.
  152. */
  153. if ((out_len == 0 && pg_bytes_left < LZO_LEN) ||
  154. pg_bytes_left == 0) {
  155. if (pg_bytes_left) {
  156. memset(cpage_out + out_offset, 0,
  157. pg_bytes_left);
  158. tot_out += pg_bytes_left;
  159. }
  160. /* we're done, don't allocate new page */
  161. if (out_len == 0 && tot_in >= len)
  162. break;
  163. kunmap(out_page);
  164. if (nr_pages == nr_dest_pages) {
  165. out_page = NULL;
  166. ret = -E2BIG;
  167. goto out;
  168. }
  169. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  170. if (out_page == NULL) {
  171. ret = -ENOMEM;
  172. goto out;
  173. }
  174. cpage_out = kmap(out_page);
  175. pages[nr_pages++] = out_page;
  176. pg_bytes_left = PAGE_SIZE;
  177. out_offset = 0;
  178. }
  179. }
  180. /* we're making it bigger, give up */
  181. if (tot_in > 8192 && tot_in < tot_out) {
  182. ret = -E2BIG;
  183. goto out;
  184. }
  185. /* we're all done */
  186. if (tot_in >= len)
  187. break;
  188. if (tot_out > max_out)
  189. break;
  190. bytes_left = len - tot_in;
  191. kunmap(in_page);
  192. put_page(in_page);
  193. start += PAGE_SIZE;
  194. in_page = find_get_page(mapping, start >> PAGE_SHIFT);
  195. data_in = kmap(in_page);
  196. in_len = min(bytes_left, PAGE_SIZE);
  197. }
  198. if (tot_out >= tot_in) {
  199. ret = -E2BIG;
  200. goto out;
  201. }
  202. /* store the size of all chunks of compressed data */
  203. cpage_out = kmap(pages[0]);
  204. write_compress_length(cpage_out, tot_out);
  205. kunmap(pages[0]);
  206. ret = 0;
  207. *total_out = tot_out;
  208. *total_in = tot_in;
  209. out:
  210. *out_pages = nr_pages;
  211. if (out_page)
  212. kunmap(out_page);
  213. if (in_page) {
  214. kunmap(in_page);
  215. put_page(in_page);
  216. }
  217. return ret;
  218. }
  219. static int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
  220. {
  221. struct workspace *workspace = list_entry(ws, struct workspace, list);
  222. int ret = 0, ret2;
  223. char *data_in;
  224. unsigned long page_in_index = 0;
  225. size_t srclen = cb->compressed_len;
  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. struct page **pages_in = cb->compressed_pages;
  241. u64 disk_start = cb->start;
  242. struct bio *orig_bio = cb->orig_bio;
  243. data_in = kmap(pages_in[0]);
  244. tot_len = read_compress_length(data_in);
  245. tot_in = LZO_LEN;
  246. in_offset = LZO_LEN;
  247. tot_len = min_t(size_t, srclen, tot_len);
  248. in_page_bytes_left = PAGE_SIZE - LZO_LEN;
  249. tot_out = 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_SIZE;
  292. in_offset = 0;
  293. }
  294. }
  295. out_len = lzo1x_worst_compress(PAGE_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. pr_warn("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, orig_bio);
  309. if (ret2 == 0)
  310. break;
  311. }
  312. done:
  313. kunmap(pages_in[page_in_index]);
  314. if (!ret)
  315. zero_fill_bio(orig_bio);
  316. return ret;
  317. }
  318. static int lzo_decompress(struct list_head *ws, unsigned char *data_in,
  319. struct page *dest_page,
  320. unsigned long start_byte,
  321. size_t srclen, size_t destlen)
  322. {
  323. struct workspace *workspace = list_entry(ws, struct workspace, list);
  324. size_t in_len;
  325. size_t out_len;
  326. size_t tot_len;
  327. int ret = 0;
  328. char *kaddr;
  329. unsigned long bytes;
  330. BUG_ON(srclen < LZO_LEN);
  331. tot_len = read_compress_length(data_in);
  332. data_in += LZO_LEN;
  333. in_len = read_compress_length(data_in);
  334. data_in += LZO_LEN;
  335. out_len = PAGE_SIZE;
  336. ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len);
  337. if (ret != LZO_E_OK) {
  338. pr_warn("BTRFS: decompress failed!\n");
  339. ret = -EIO;
  340. goto out;
  341. }
  342. if (out_len < start_byte) {
  343. ret = -EIO;
  344. goto out;
  345. }
  346. /*
  347. * the caller is already checking against PAGE_SIZE, but lets
  348. * move this check closer to the memcpy/memset
  349. */
  350. destlen = min_t(unsigned long, destlen, PAGE_SIZE);
  351. bytes = min_t(unsigned long, destlen, out_len - start_byte);
  352. kaddr = kmap_atomic(dest_page);
  353. memcpy(kaddr, workspace->buf + start_byte, bytes);
  354. /*
  355. * btrfs_getblock is doing a zero on the tail of the page too,
  356. * but this will cover anything missing from the decompressed
  357. * data.
  358. */
  359. if (bytes < destlen)
  360. memset(kaddr+bytes, 0, destlen-bytes);
  361. kunmap_atomic(kaddr);
  362. out:
  363. return ret;
  364. }
  365. const struct btrfs_compress_op btrfs_lzo_compress = {
  366. .alloc_workspace = lzo_alloc_workspace,
  367. .free_workspace = lzo_free_workspace,
  368. .compress_pages = lzo_compress_pages,
  369. .decompress_bio = lzo_decompress_bio,
  370. .decompress = lzo_decompress,
  371. };