lzo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/slab.h>
  7. #include <linux/mm.h>
  8. #include <linux/init.h>
  9. #include <linux/err.h>
  10. #include <linux/sched.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/bio.h>
  13. #include <linux/lzo.h>
  14. #include <linux/refcount.h>
  15. #include "compression.h"
  16. #define LZO_LEN 4
  17. /*
  18. * Btrfs LZO compression format
  19. *
  20. * Regular and inlined LZO compressed data extents consist of:
  21. *
  22. * 1. Header
  23. * Fixed size. LZO_LEN (4) bytes long, LE32.
  24. * Records the total size (including the header) of compressed data.
  25. *
  26. * 2. Segment(s)
  27. * Variable size. Each segment includes one segment header, followd by data
  28. * payload.
  29. * One regular LZO compressed extent can have one or more segments.
  30. * For inlined LZO compressed extent, only one segment is allowed.
  31. * One segment represents at most one page of uncompressed data.
  32. *
  33. * 2.1 Segment header
  34. * Fixed size. LZO_LEN (4) bytes long, LE32.
  35. * Records the total size of the segment (not including the header).
  36. * Segment header never crosses page boundary, thus it's possible to
  37. * have at most 3 padding zeros at the end of the page.
  38. *
  39. * 2.2 Data Payload
  40. * Variable size. Size up limit should be lzo1x_worst_compress(PAGE_SIZE)
  41. * which is 4419 for a 4KiB page.
  42. *
  43. * Example:
  44. * Page 1:
  45. * 0 0x2 0x4 0x6 0x8 0xa 0xc 0xe 0x10
  46. * 0x0000 | Header | SegHdr 01 | Data payload 01 ... |
  47. * ...
  48. * 0x0ff0 | SegHdr N | Data payload N ... |00|
  49. * ^^ padding zeros
  50. * Page 2:
  51. * 0x1000 | SegHdr N+1| Data payload N+1 ... |
  52. */
  53. struct workspace {
  54. void *mem;
  55. void *buf; /* where decompressed data goes */
  56. void *cbuf; /* where compressed data goes */
  57. struct list_head list;
  58. };
  59. static void lzo_free_workspace(struct list_head *ws)
  60. {
  61. struct workspace *workspace = list_entry(ws, struct workspace, list);
  62. kvfree(workspace->buf);
  63. kvfree(workspace->cbuf);
  64. kvfree(workspace->mem);
  65. kfree(workspace);
  66. }
  67. static struct list_head *lzo_alloc_workspace(void)
  68. {
  69. struct workspace *workspace;
  70. workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
  71. if (!workspace)
  72. return ERR_PTR(-ENOMEM);
  73. workspace->mem = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
  74. workspace->buf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
  75. workspace->cbuf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
  76. if (!workspace->mem || !workspace->buf || !workspace->cbuf)
  77. goto fail;
  78. INIT_LIST_HEAD(&workspace->list);
  79. return &workspace->list;
  80. fail:
  81. lzo_free_workspace(&workspace->list);
  82. return ERR_PTR(-ENOMEM);
  83. }
  84. static inline void write_compress_length(char *buf, size_t len)
  85. {
  86. __le32 dlen;
  87. dlen = cpu_to_le32(len);
  88. memcpy(buf, &dlen, LZO_LEN);
  89. }
  90. static inline size_t read_compress_length(const char *buf)
  91. {
  92. __le32 dlen;
  93. memcpy(&dlen, buf, LZO_LEN);
  94. return le32_to_cpu(dlen);
  95. }
  96. static int lzo_compress_pages(struct list_head *ws,
  97. struct address_space *mapping,
  98. u64 start,
  99. struct page **pages,
  100. unsigned long *out_pages,
  101. unsigned long *total_in,
  102. unsigned long *total_out)
  103. {
  104. struct workspace *workspace = list_entry(ws, struct workspace, list);
  105. int ret = 0;
  106. char *data_in;
  107. char *cpage_out;
  108. int nr_pages = 0;
  109. struct page *in_page = NULL;
  110. struct page *out_page = NULL;
  111. unsigned long bytes_left;
  112. unsigned long len = *total_out;
  113. unsigned long nr_dest_pages = *out_pages;
  114. const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
  115. size_t in_len;
  116. size_t out_len;
  117. char *buf;
  118. unsigned long tot_in = 0;
  119. unsigned long tot_out = 0;
  120. unsigned long pg_bytes_left;
  121. unsigned long out_offset;
  122. unsigned long bytes;
  123. *out_pages = 0;
  124. *total_out = 0;
  125. *total_in = 0;
  126. in_page = find_get_page(mapping, start >> PAGE_SHIFT);
  127. data_in = kmap(in_page);
  128. /*
  129. * store the size of all chunks of compressed data in
  130. * the first 4 bytes
  131. */
  132. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  133. if (out_page == NULL) {
  134. ret = -ENOMEM;
  135. goto out;
  136. }
  137. cpage_out = kmap(out_page);
  138. out_offset = LZO_LEN;
  139. tot_out = LZO_LEN;
  140. pages[0] = out_page;
  141. nr_pages = 1;
  142. pg_bytes_left = PAGE_SIZE - LZO_LEN;
  143. /* compress at most one page of data each time */
  144. in_len = min(len, PAGE_SIZE);
  145. while (tot_in < len) {
  146. ret = lzo1x_1_compress(data_in, in_len, workspace->cbuf,
  147. &out_len, workspace->mem);
  148. if (ret != LZO_E_OK) {
  149. pr_debug("BTRFS: lzo in loop returned %d\n",
  150. ret);
  151. ret = -EIO;
  152. goto out;
  153. }
  154. /* store the size of this chunk of compressed data */
  155. write_compress_length(cpage_out + out_offset, out_len);
  156. tot_out += LZO_LEN;
  157. out_offset += LZO_LEN;
  158. pg_bytes_left -= LZO_LEN;
  159. tot_in += in_len;
  160. tot_out += out_len;
  161. /* copy bytes from the working buffer into the pages */
  162. buf = workspace->cbuf;
  163. while (out_len) {
  164. bytes = min_t(unsigned long, pg_bytes_left, out_len);
  165. memcpy(cpage_out + out_offset, buf, bytes);
  166. out_len -= bytes;
  167. pg_bytes_left -= bytes;
  168. buf += bytes;
  169. out_offset += bytes;
  170. /*
  171. * we need another page for writing out.
  172. *
  173. * Note if there's less than 4 bytes left, we just
  174. * skip to a new page.
  175. */
  176. if ((out_len == 0 && pg_bytes_left < LZO_LEN) ||
  177. pg_bytes_left == 0) {
  178. if (pg_bytes_left) {
  179. memset(cpage_out + out_offset, 0,
  180. pg_bytes_left);
  181. tot_out += pg_bytes_left;
  182. }
  183. /* we're done, don't allocate new page */
  184. if (out_len == 0 && tot_in >= len)
  185. break;
  186. kunmap(out_page);
  187. if (nr_pages == nr_dest_pages) {
  188. out_page = NULL;
  189. ret = -E2BIG;
  190. goto out;
  191. }
  192. out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  193. if (out_page == NULL) {
  194. ret = -ENOMEM;
  195. goto out;
  196. }
  197. cpage_out = kmap(out_page);
  198. pages[nr_pages++] = out_page;
  199. pg_bytes_left = PAGE_SIZE;
  200. out_offset = 0;
  201. }
  202. }
  203. /* we're making it bigger, give up */
  204. if (tot_in > 8192 && tot_in < tot_out) {
  205. ret = -E2BIG;
  206. goto out;
  207. }
  208. /* we're all done */
  209. if (tot_in >= len)
  210. break;
  211. if (tot_out > max_out)
  212. break;
  213. bytes_left = len - tot_in;
  214. kunmap(in_page);
  215. put_page(in_page);
  216. start += PAGE_SIZE;
  217. in_page = find_get_page(mapping, start >> PAGE_SHIFT);
  218. data_in = kmap(in_page);
  219. in_len = min(bytes_left, PAGE_SIZE);
  220. }
  221. if (tot_out >= tot_in) {
  222. ret = -E2BIG;
  223. goto out;
  224. }
  225. /* store the size of all chunks of compressed data */
  226. cpage_out = kmap(pages[0]);
  227. write_compress_length(cpage_out, tot_out);
  228. kunmap(pages[0]);
  229. ret = 0;
  230. *total_out = tot_out;
  231. *total_in = tot_in;
  232. out:
  233. *out_pages = nr_pages;
  234. if (out_page)
  235. kunmap(out_page);
  236. if (in_page) {
  237. kunmap(in_page);
  238. put_page(in_page);
  239. }
  240. return ret;
  241. }
  242. static int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
  243. {
  244. struct workspace *workspace = list_entry(ws, struct workspace, list);
  245. int ret = 0, ret2;
  246. char *data_in;
  247. unsigned long page_in_index = 0;
  248. size_t srclen = cb->compressed_len;
  249. unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
  250. unsigned long buf_start;
  251. unsigned long buf_offset = 0;
  252. unsigned long bytes;
  253. unsigned long working_bytes;
  254. size_t in_len;
  255. size_t out_len;
  256. const size_t max_segment_len = lzo1x_worst_compress(PAGE_SIZE);
  257. unsigned long in_offset;
  258. unsigned long in_page_bytes_left;
  259. unsigned long tot_in;
  260. unsigned long tot_out;
  261. unsigned long tot_len;
  262. char *buf;
  263. bool may_late_unmap, need_unmap;
  264. struct page **pages_in = cb->compressed_pages;
  265. u64 disk_start = cb->start;
  266. struct bio *orig_bio = cb->orig_bio;
  267. data_in = kmap(pages_in[0]);
  268. tot_len = read_compress_length(data_in);
  269. /*
  270. * Compressed data header check.
  271. *
  272. * The real compressed size can't exceed the maximum extent length, and
  273. * all pages should be used (whole unused page with just the segment
  274. * header is not possible). If this happens it means the compressed
  275. * extent is corrupted.
  276. */
  277. if (tot_len > min_t(size_t, BTRFS_MAX_COMPRESSED, srclen) ||
  278. tot_len < srclen - PAGE_SIZE) {
  279. ret = -EUCLEAN;
  280. goto done;
  281. }
  282. tot_in = LZO_LEN;
  283. in_offset = LZO_LEN;
  284. in_page_bytes_left = PAGE_SIZE - LZO_LEN;
  285. tot_out = 0;
  286. while (tot_in < tot_len) {
  287. in_len = read_compress_length(data_in + in_offset);
  288. in_page_bytes_left -= LZO_LEN;
  289. in_offset += LZO_LEN;
  290. tot_in += LZO_LEN;
  291. /*
  292. * Segment header check.
  293. *
  294. * The segment length must not exceed the maximum LZO
  295. * compression size, nor the total compressed size.
  296. */
  297. if (in_len > max_segment_len || tot_in + in_len > tot_len) {
  298. ret = -EUCLEAN;
  299. goto done;
  300. }
  301. tot_in += in_len;
  302. working_bytes = in_len;
  303. may_late_unmap = need_unmap = false;
  304. /* fast path: avoid using the working buffer */
  305. if (in_page_bytes_left >= in_len) {
  306. buf = data_in + in_offset;
  307. bytes = in_len;
  308. may_late_unmap = true;
  309. goto cont;
  310. }
  311. /* copy bytes from the pages into the working buffer */
  312. buf = workspace->cbuf;
  313. buf_offset = 0;
  314. while (working_bytes) {
  315. bytes = min(working_bytes, in_page_bytes_left);
  316. memcpy(buf + buf_offset, data_in + in_offset, bytes);
  317. buf_offset += bytes;
  318. cont:
  319. working_bytes -= bytes;
  320. in_page_bytes_left -= bytes;
  321. in_offset += bytes;
  322. /* check if we need to pick another page */
  323. if ((working_bytes == 0 && in_page_bytes_left < LZO_LEN)
  324. || in_page_bytes_left == 0) {
  325. tot_in += in_page_bytes_left;
  326. if (working_bytes == 0 && tot_in >= tot_len)
  327. break;
  328. if (page_in_index + 1 >= total_pages_in) {
  329. ret = -EIO;
  330. goto done;
  331. }
  332. if (may_late_unmap)
  333. need_unmap = true;
  334. else
  335. kunmap(pages_in[page_in_index]);
  336. data_in = kmap(pages_in[++page_in_index]);
  337. in_page_bytes_left = PAGE_SIZE;
  338. in_offset = 0;
  339. }
  340. }
  341. out_len = max_segment_len;
  342. ret = lzo1x_decompress_safe(buf, in_len, workspace->buf,
  343. &out_len);
  344. if (need_unmap)
  345. kunmap(pages_in[page_in_index - 1]);
  346. if (ret != LZO_E_OK) {
  347. pr_warn("BTRFS: decompress failed\n");
  348. ret = -EIO;
  349. break;
  350. }
  351. buf_start = tot_out;
  352. tot_out += out_len;
  353. ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
  354. tot_out, disk_start, orig_bio);
  355. if (ret2 == 0)
  356. break;
  357. }
  358. done:
  359. kunmap(pages_in[page_in_index]);
  360. if (!ret)
  361. zero_fill_bio(orig_bio);
  362. return ret;
  363. }
  364. static int lzo_decompress(struct list_head *ws, unsigned char *data_in,
  365. struct page *dest_page,
  366. unsigned long start_byte,
  367. size_t srclen, size_t destlen)
  368. {
  369. struct workspace *workspace = list_entry(ws, struct workspace, list);
  370. size_t in_len;
  371. size_t out_len;
  372. size_t max_segment_len = lzo1x_worst_compress(PAGE_SIZE);
  373. int ret = 0;
  374. char *kaddr;
  375. unsigned long bytes;
  376. if (srclen < LZO_LEN || srclen > max_segment_len + LZO_LEN * 2)
  377. return -EUCLEAN;
  378. in_len = read_compress_length(data_in);
  379. if (in_len != srclen)
  380. return -EUCLEAN;
  381. data_in += LZO_LEN;
  382. in_len = read_compress_length(data_in);
  383. if (in_len != srclen - LZO_LEN * 2) {
  384. ret = -EUCLEAN;
  385. goto out;
  386. }
  387. data_in += LZO_LEN;
  388. out_len = PAGE_SIZE;
  389. ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len);
  390. if (ret != LZO_E_OK) {
  391. pr_warn("BTRFS: decompress failed!\n");
  392. ret = -EIO;
  393. goto out;
  394. }
  395. if (out_len < start_byte) {
  396. ret = -EIO;
  397. goto out;
  398. }
  399. /*
  400. * the caller is already checking against PAGE_SIZE, but lets
  401. * move this check closer to the memcpy/memset
  402. */
  403. destlen = min_t(unsigned long, destlen, PAGE_SIZE);
  404. bytes = min_t(unsigned long, destlen, out_len - start_byte);
  405. kaddr = kmap_atomic(dest_page);
  406. memcpy(kaddr, workspace->buf + start_byte, bytes);
  407. /*
  408. * btrfs_getblock is doing a zero on the tail of the page too,
  409. * but this will cover anything missing from the decompressed
  410. * data.
  411. */
  412. if (bytes < destlen)
  413. memset(kaddr+bytes, 0, destlen-bytes);
  414. kunmap_atomic(kaddr);
  415. out:
  416. return ret;
  417. }
  418. static void lzo_set_level(struct list_head *ws, unsigned int type)
  419. {
  420. }
  421. const struct btrfs_compress_op btrfs_lzo_compress = {
  422. .alloc_workspace = lzo_alloc_workspace,
  423. .free_workspace = lzo_free_workspace,
  424. .compress_pages = lzo_compress_pages,
  425. .decompress_bio = lzo_decompress_bio,
  426. .decompress = lzo_decompress,
  427. .set_level = lzo_set_level,
  428. };