Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
c8b97818 CM |
2 | /* |
3 | * Copyright (C) 2008 Oracle. All rights reserved. | |
4 | * | |
c8b97818 CM |
5 | * Based on jffs2 zlib code: |
6 | * Copyright © 2001-2007 Red Hat, Inc. | |
7 | * Created by David Woodhouse <dwmw2@infradead.org> | |
8 | */ | |
9 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/zlib.h> | |
13 | #include <linux/zutil.h> | |
6acafd1e | 14 | #include <linux/mm.h> |
c8b97818 CM |
15 | #include <linux/init.h> |
16 | #include <linux/err.h> | |
17 | #include <linux/sched.h> | |
18 | #include <linux/pagemap.h> | |
19 | #include <linux/bio.h> | |
e1ddce71 | 20 | #include <linux/refcount.h> |
b2950863 | 21 | #include "compression.h" |
c8b97818 | 22 | |
c8b97818 | 23 | struct workspace { |
78809913 | 24 | z_stream strm; |
c8b97818 CM |
25 | char *buf; |
26 | struct list_head list; | |
f51d2b59 | 27 | int level; |
c8b97818 CM |
28 | }; |
29 | ||
92ee5530 DZ |
30 | static struct workspace_manager wsm; |
31 | ||
32 | static void zlib_init_workspace_manager(void) | |
33 | { | |
34 | btrfs_init_workspace_manager(&wsm, &btrfs_zlib_compress); | |
35 | } | |
36 | ||
37 | static void zlib_cleanup_workspace_manager(void) | |
38 | { | |
39 | btrfs_cleanup_workspace_manager(&wsm); | |
40 | } | |
41 | ||
42 | static struct list_head *zlib_get_workspace(void) | |
43 | { | |
44 | return btrfs_get_workspace(&wsm); | |
45 | } | |
46 | ||
47 | static void zlib_put_workspace(struct list_head *ws) | |
48 | { | |
49 | btrfs_put_workspace(&wsm, ws); | |
50 | } | |
51 | ||
261507a0 LZ |
52 | static void zlib_free_workspace(struct list_head *ws) |
53 | { | |
54 | struct workspace *workspace = list_entry(ws, struct workspace, list); | |
c8b97818 | 55 | |
6acafd1e | 56 | kvfree(workspace->strm.workspace); |
261507a0 LZ |
57 | kfree(workspace->buf); |
58 | kfree(workspace); | |
59 | } | |
60 | ||
61 | static struct list_head *zlib_alloc_workspace(void) | |
c8b97818 CM |
62 | { |
63 | struct workspace *workspace; | |
78809913 | 64 | int workspacesize; |
8844355d | 65 | |
389a6cfc | 66 | workspace = kzalloc(sizeof(*workspace), GFP_KERNEL); |
261507a0 LZ |
67 | if (!workspace) |
68 | return ERR_PTR(-ENOMEM); | |
c8b97818 | 69 | |
78809913 SS |
70 | workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL), |
71 | zlib_inflate_workspacesize()); | |
6acafd1e | 72 | workspace->strm.workspace = kvmalloc(workspacesize, GFP_KERNEL); |
389a6cfc | 73 | workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
78809913 | 74 | if (!workspace->strm.workspace || !workspace->buf) |
261507a0 | 75 | goto fail; |
c8b97818 | 76 | |
261507a0 | 77 | INIT_LIST_HEAD(&workspace->list); |
c8b97818 | 78 | |
261507a0 LZ |
79 | return &workspace->list; |
80 | fail: | |
81 | zlib_free_workspace(&workspace->list); | |
82 | return ERR_PTR(-ENOMEM); | |
c8b97818 CM |
83 | } |
84 | ||
261507a0 LZ |
85 | static int zlib_compress_pages(struct list_head *ws, |
86 | struct address_space *mapping, | |
38c31464 | 87 | u64 start, |
261507a0 | 88 | struct page **pages, |
261507a0 LZ |
89 | unsigned long *out_pages, |
90 | unsigned long *total_in, | |
e5d74902 | 91 | unsigned long *total_out) |
c8b97818 | 92 | { |
261507a0 | 93 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
c8b97818 | 94 | int ret; |
c8b97818 CM |
95 | char *data_in; |
96 | char *cpage_out; | |
97 | int nr_pages = 0; | |
98 | struct page *in_page = NULL; | |
99 | struct page *out_page = NULL; | |
c8b97818 | 100 | unsigned long bytes_left; |
38c31464 | 101 | unsigned long len = *total_out; |
4d3a800e | 102 | unsigned long nr_dest_pages = *out_pages; |
e5d74902 | 103 | const unsigned long max_out = nr_dest_pages * PAGE_SIZE; |
c8b97818 CM |
104 | |
105 | *out_pages = 0; | |
106 | *total_out = 0; | |
107 | *total_in = 0; | |
108 | ||
f51d2b59 | 109 | if (Z_OK != zlib_deflateInit(&workspace->strm, workspace->level)) { |
62e85577 | 110 | pr_warn("BTRFS: deflateInit failed\n"); |
60e1975a | 111 | ret = -EIO; |
c8b97818 CM |
112 | goto out; |
113 | } | |
114 | ||
78809913 SS |
115 | workspace->strm.total_in = 0; |
116 | workspace->strm.total_out = 0; | |
c8b97818 | 117 | |
09cbfeaf | 118 | in_page = find_get_page(mapping, start >> PAGE_SHIFT); |
c8b97818 CM |
119 | data_in = kmap(in_page); |
120 | ||
121 | out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
4b72029d | 122 | if (out_page == NULL) { |
60e1975a | 123 | ret = -ENOMEM; |
4b72029d LZ |
124 | goto out; |
125 | } | |
c8b97818 CM |
126 | cpage_out = kmap(out_page); |
127 | pages[0] = out_page; | |
128 | nr_pages = 1; | |
129 | ||
78809913 SS |
130 | workspace->strm.next_in = data_in; |
131 | workspace->strm.next_out = cpage_out; | |
09cbfeaf KS |
132 | workspace->strm.avail_out = PAGE_SIZE; |
133 | workspace->strm.avail_in = min(len, PAGE_SIZE); | |
c8b97818 | 134 | |
78809913 SS |
135 | while (workspace->strm.total_in < len) { |
136 | ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH); | |
c8b97818 | 137 | if (ret != Z_OK) { |
62e85577 | 138 | pr_debug("BTRFS: deflate in loop returned %d\n", |
c8b97818 | 139 | ret); |
78809913 | 140 | zlib_deflateEnd(&workspace->strm); |
60e1975a | 141 | ret = -EIO; |
c8b97818 CM |
142 | goto out; |
143 | } | |
144 | ||
145 | /* we're making it bigger, give up */ | |
78809913 SS |
146 | if (workspace->strm.total_in > 8192 && |
147 | workspace->strm.total_in < | |
148 | workspace->strm.total_out) { | |
130d5b41 | 149 | ret = -E2BIG; |
c8b97818 CM |
150 | goto out; |
151 | } | |
152 | /* we need another page for writing out. Test this | |
153 | * before the total_in so we will pull in a new page for | |
154 | * the stream end if required | |
155 | */ | |
78809913 | 156 | if (workspace->strm.avail_out == 0) { |
c8b97818 CM |
157 | kunmap(out_page); |
158 | if (nr_pages == nr_dest_pages) { | |
159 | out_page = NULL; | |
60e1975a | 160 | ret = -E2BIG; |
c8b97818 CM |
161 | goto out; |
162 | } | |
163 | out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
4b72029d | 164 | if (out_page == NULL) { |
60e1975a | 165 | ret = -ENOMEM; |
4b72029d LZ |
166 | goto out; |
167 | } | |
c8b97818 CM |
168 | cpage_out = kmap(out_page); |
169 | pages[nr_pages] = out_page; | |
170 | nr_pages++; | |
09cbfeaf | 171 | workspace->strm.avail_out = PAGE_SIZE; |
78809913 | 172 | workspace->strm.next_out = cpage_out; |
c8b97818 CM |
173 | } |
174 | /* we're all done */ | |
78809913 | 175 | if (workspace->strm.total_in >= len) |
c8b97818 CM |
176 | break; |
177 | ||
178 | /* we've read in a full page, get a new one */ | |
78809913 SS |
179 | if (workspace->strm.avail_in == 0) { |
180 | if (workspace->strm.total_out > max_out) | |
c8b97818 CM |
181 | break; |
182 | ||
78809913 | 183 | bytes_left = len - workspace->strm.total_in; |
c8b97818 | 184 | kunmap(in_page); |
09cbfeaf | 185 | put_page(in_page); |
c8b97818 | 186 | |
09cbfeaf | 187 | start += PAGE_SIZE; |
c8b97818 | 188 | in_page = find_get_page(mapping, |
09cbfeaf | 189 | start >> PAGE_SHIFT); |
c8b97818 | 190 | data_in = kmap(in_page); |
78809913 | 191 | workspace->strm.avail_in = min(bytes_left, |
09cbfeaf | 192 | PAGE_SIZE); |
78809913 | 193 | workspace->strm.next_in = data_in; |
c8b97818 CM |
194 | } |
195 | } | |
78809913 SS |
196 | workspace->strm.avail_in = 0; |
197 | ret = zlib_deflate(&workspace->strm, Z_FINISH); | |
198 | zlib_deflateEnd(&workspace->strm); | |
c8b97818 CM |
199 | |
200 | if (ret != Z_STREAM_END) { | |
60e1975a | 201 | ret = -EIO; |
c8b97818 CM |
202 | goto out; |
203 | } | |
204 | ||
78809913 | 205 | if (workspace->strm.total_out >= workspace->strm.total_in) { |
60e1975a | 206 | ret = -E2BIG; |
c8b97818 CM |
207 | goto out; |
208 | } | |
209 | ||
210 | ret = 0; | |
78809913 SS |
211 | *total_out = workspace->strm.total_out; |
212 | *total_in = workspace->strm.total_in; | |
c8b97818 CM |
213 | out: |
214 | *out_pages = nr_pages; | |
215 | if (out_page) | |
216 | kunmap(out_page); | |
217 | ||
218 | if (in_page) { | |
219 | kunmap(in_page); | |
09cbfeaf | 220 | put_page(in_page); |
c8b97818 | 221 | } |
c8b97818 CM |
222 | return ret; |
223 | } | |
224 | ||
e1ddce71 | 225 | static int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb) |
c8b97818 | 226 | { |
261507a0 | 227 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
3a39c18d | 228 | int ret = 0, ret2; |
c8b97818 | 229 | int wbits = MAX_WBITS; |
c8b97818 CM |
230 | char *data_in; |
231 | size_t total_out = 0; | |
c8b97818 | 232 | unsigned long page_in_index = 0; |
e1ddce71 | 233 | size_t srclen = cb->compressed_len; |
09cbfeaf | 234 | unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE); |
c8b97818 | 235 | unsigned long buf_start; |
e1ddce71 AJ |
236 | struct page **pages_in = cb->compressed_pages; |
237 | u64 disk_start = cb->start; | |
238 | struct bio *orig_bio = cb->orig_bio; | |
c8b97818 | 239 | |
c8b97818 | 240 | data_in = kmap(pages_in[page_in_index]); |
78809913 | 241 | workspace->strm.next_in = data_in; |
09cbfeaf | 242 | workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE); |
78809913 | 243 | workspace->strm.total_in = 0; |
c8b97818 | 244 | |
78809913 SS |
245 | workspace->strm.total_out = 0; |
246 | workspace->strm.next_out = workspace->buf; | |
09cbfeaf | 247 | workspace->strm.avail_out = PAGE_SIZE; |
c8b97818 CM |
248 | |
249 | /* If it's deflate, and it's got no preset dictionary, then | |
250 | we can tell zlib to skip the adler32 check. */ | |
251 | if (srclen > 2 && !(data_in[1] & PRESET_DICT) && | |
252 | ((data_in[0] & 0x0f) == Z_DEFLATED) && | |
253 | !(((data_in[0]<<8) + data_in[1]) % 31)) { | |
254 | ||
255 | wbits = -((data_in[0] >> 4) + 8); | |
78809913 SS |
256 | workspace->strm.next_in += 2; |
257 | workspace->strm.avail_in -= 2; | |
c8b97818 CM |
258 | } |
259 | ||
78809913 | 260 | if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) { |
62e85577 | 261 | pr_warn("BTRFS: inflateInit failed\n"); |
d1111a75 | 262 | kunmap(pages_in[page_in_index]); |
60e1975a | 263 | return -EIO; |
c8b97818 | 264 | } |
78809913 SS |
265 | while (workspace->strm.total_in < srclen) { |
266 | ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH); | |
d397712b | 267 | if (ret != Z_OK && ret != Z_STREAM_END) |
c8b97818 | 268 | break; |
c8b97818 | 269 | |
3a39c18d | 270 | buf_start = total_out; |
78809913 | 271 | total_out = workspace->strm.total_out; |
c8b97818 | 272 | |
3a39c18d LZ |
273 | /* we didn't make progress in this inflate call, we're done */ |
274 | if (buf_start == total_out) | |
c8b97818 | 275 | break; |
c8b97818 | 276 | |
3a39c18d LZ |
277 | ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start, |
278 | total_out, disk_start, | |
974b1adc | 279 | orig_bio); |
3a39c18d LZ |
280 | if (ret2 == 0) { |
281 | ret = 0; | |
282 | goto done; | |
c8b97818 | 283 | } |
3a39c18d | 284 | |
78809913 | 285 | workspace->strm.next_out = workspace->buf; |
09cbfeaf | 286 | workspace->strm.avail_out = PAGE_SIZE; |
c8b97818 | 287 | |
78809913 | 288 | if (workspace->strm.avail_in == 0) { |
c8b97818 CM |
289 | unsigned long tmp; |
290 | kunmap(pages_in[page_in_index]); | |
291 | page_in_index++; | |
292 | if (page_in_index >= total_pages_in) { | |
293 | data_in = NULL; | |
294 | break; | |
295 | } | |
296 | data_in = kmap(pages_in[page_in_index]); | |
78809913 SS |
297 | workspace->strm.next_in = data_in; |
298 | tmp = srclen - workspace->strm.total_in; | |
299 | workspace->strm.avail_in = min(tmp, | |
09cbfeaf | 300 | PAGE_SIZE); |
c8b97818 CM |
301 | } |
302 | } | |
d397712b | 303 | if (ret != Z_STREAM_END) |
60e1975a | 304 | ret = -EIO; |
d397712b | 305 | else |
c8b97818 | 306 | ret = 0; |
c8b97818 | 307 | done: |
78809913 | 308 | zlib_inflateEnd(&workspace->strm); |
c8b97818 CM |
309 | if (data_in) |
310 | kunmap(pages_in[page_in_index]); | |
2f19cad9 | 311 | if (!ret) |
974b1adc | 312 | zero_fill_bio(orig_bio); |
c8b97818 CM |
313 | return ret; |
314 | } | |
315 | ||
261507a0 LZ |
316 | static int zlib_decompress(struct list_head *ws, unsigned char *data_in, |
317 | struct page *dest_page, | |
318 | unsigned long start_byte, | |
319 | size_t srclen, size_t destlen) | |
c8b97818 | 320 | { |
261507a0 | 321 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
c8b97818 CM |
322 | int ret = 0; |
323 | int wbits = MAX_WBITS; | |
2f19cad9 | 324 | unsigned long bytes_left; |
c8b97818 | 325 | unsigned long total_out = 0; |
2f19cad9 | 326 | unsigned long pg_offset = 0; |
c8b97818 CM |
327 | char *kaddr; |
328 | ||
2f19cad9 CM |
329 | destlen = min_t(unsigned long, destlen, PAGE_SIZE); |
330 | bytes_left = destlen; | |
331 | ||
78809913 SS |
332 | workspace->strm.next_in = data_in; |
333 | workspace->strm.avail_in = srclen; | |
334 | workspace->strm.total_in = 0; | |
c8b97818 | 335 | |
78809913 | 336 | workspace->strm.next_out = workspace->buf; |
09cbfeaf | 337 | workspace->strm.avail_out = PAGE_SIZE; |
78809913 | 338 | workspace->strm.total_out = 0; |
c8b97818 CM |
339 | /* If it's deflate, and it's got no preset dictionary, then |
340 | we can tell zlib to skip the adler32 check. */ | |
341 | if (srclen > 2 && !(data_in[1] & PRESET_DICT) && | |
342 | ((data_in[0] & 0x0f) == Z_DEFLATED) && | |
343 | !(((data_in[0]<<8) + data_in[1]) % 31)) { | |
344 | ||
345 | wbits = -((data_in[0] >> 4) + 8); | |
78809913 SS |
346 | workspace->strm.next_in += 2; |
347 | workspace->strm.avail_in -= 2; | |
c8b97818 CM |
348 | } |
349 | ||
78809913 | 350 | if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) { |
62e85577 | 351 | pr_warn("BTRFS: inflateInit failed\n"); |
60e1975a | 352 | return -EIO; |
c8b97818 CM |
353 | } |
354 | ||
d397712b | 355 | while (bytes_left > 0) { |
c8b97818 CM |
356 | unsigned long buf_start; |
357 | unsigned long buf_offset; | |
358 | unsigned long bytes; | |
c8b97818 | 359 | |
78809913 | 360 | ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH); |
d397712b | 361 | if (ret != Z_OK && ret != Z_STREAM_END) |
c8b97818 | 362 | break; |
c8b97818 CM |
363 | |
364 | buf_start = total_out; | |
78809913 | 365 | total_out = workspace->strm.total_out; |
c8b97818 CM |
366 | |
367 | if (total_out == buf_start) { | |
60e1975a | 368 | ret = -EIO; |
c8b97818 CM |
369 | break; |
370 | } | |
371 | ||
d397712b | 372 | if (total_out <= start_byte) |
c8b97818 | 373 | goto next; |
c8b97818 | 374 | |
d397712b | 375 | if (total_out > start_byte && buf_start < start_byte) |
c8b97818 | 376 | buf_offset = start_byte - buf_start; |
d397712b | 377 | else |
c8b97818 | 378 | buf_offset = 0; |
c8b97818 | 379 | |
09cbfeaf KS |
380 | bytes = min(PAGE_SIZE - pg_offset, |
381 | PAGE_SIZE - buf_offset); | |
c8b97818 CM |
382 | bytes = min(bytes, bytes_left); |
383 | ||
7ac687d9 | 384 | kaddr = kmap_atomic(dest_page); |
c8b97818 | 385 | memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes); |
7ac687d9 | 386 | kunmap_atomic(kaddr); |
c8b97818 CM |
387 | |
388 | pg_offset += bytes; | |
389 | bytes_left -= bytes; | |
390 | next: | |
78809913 | 391 | workspace->strm.next_out = workspace->buf; |
09cbfeaf | 392 | workspace->strm.avail_out = PAGE_SIZE; |
c8b97818 | 393 | } |
d397712b CM |
394 | |
395 | if (ret != Z_STREAM_END && bytes_left != 0) | |
60e1975a | 396 | ret = -EIO; |
d397712b | 397 | else |
c8b97818 | 398 | ret = 0; |
d397712b | 399 | |
78809913 | 400 | zlib_inflateEnd(&workspace->strm); |
2f19cad9 CM |
401 | |
402 | /* | |
403 | * this should only happen if zlib returned fewer bytes than we | |
404 | * expected. btrfs_get_block is responsible for zeroing from the | |
405 | * end of the inline extent (destlen) to the end of the page | |
406 | */ | |
407 | if (pg_offset < destlen) { | |
408 | kaddr = kmap_atomic(dest_page); | |
409 | memset(kaddr + pg_offset, 0, destlen - pg_offset); | |
410 | kunmap_atomic(kaddr); | |
411 | } | |
c8b97818 CM |
412 | return ret; |
413 | } | |
414 | ||
f51d2b59 DS |
415 | static void zlib_set_level(struct list_head *ws, unsigned int type) |
416 | { | |
417 | struct workspace *workspace = list_entry(ws, struct workspace, list); | |
1972708a | 418 | unsigned int level = btrfs_compress_level(type); |
f51d2b59 DS |
419 | |
420 | if (level > 9) | |
421 | level = 9; | |
422 | ||
423 | workspace->level = level > 0 ? level : 3; | |
424 | } | |
425 | ||
e8c9f186 | 426 | const struct btrfs_compress_op btrfs_zlib_compress = { |
92ee5530 DZ |
427 | .init_workspace_manager = zlib_init_workspace_manager, |
428 | .cleanup_workspace_manager = zlib_cleanup_workspace_manager, | |
429 | .get_workspace = zlib_get_workspace, | |
430 | .put_workspace = zlib_put_workspace, | |
261507a0 LZ |
431 | .alloc_workspace = zlib_alloc_workspace, |
432 | .free_workspace = zlib_free_workspace, | |
433 | .compress_pages = zlib_compress_pages, | |
974b1adc | 434 | .decompress_bio = zlib_decompress_bio, |
261507a0 | 435 | .decompress = zlib_decompress, |
f51d2b59 | 436 | .set_level = zlib_set_level, |
261507a0 | 437 | }; |