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