2 * Copyright (c) 2016-present, Facebook, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 #include <linux/bio.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
19 #include <linux/pagemap.h>
20 #include <linux/refcount.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/zstd.h>
24 #include "compression.h"
26 #define ZSTD_BTRFS_MAX_WINDOWLOG 17
27 #define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
28 #define ZSTD_BTRFS_DEFAULT_LEVEL 3
30 static ZSTD_parameters
zstd_get_btrfs_parameters(size_t src_len
)
32 ZSTD_parameters params
= ZSTD_getParams(ZSTD_BTRFS_DEFAULT_LEVEL
,
35 if (params
.cParams
.windowLog
> ZSTD_BTRFS_MAX_WINDOWLOG
)
36 params
.cParams
.windowLog
= ZSTD_BTRFS_MAX_WINDOWLOG
;
37 WARN_ON(src_len
> ZSTD_BTRFS_MAX_INPUT
);
45 struct list_head list
;
47 ZSTD_outBuffer out_buf
;
50 static void zstd_free_workspace(struct list_head
*ws
)
52 struct workspace
*workspace
= list_entry(ws
, struct workspace
, list
);
54 kvfree(workspace
->mem
);
55 kfree(workspace
->buf
);
59 static struct list_head
*zstd_alloc_workspace(void)
61 ZSTD_parameters params
=
62 zstd_get_btrfs_parameters(ZSTD_BTRFS_MAX_INPUT
);
63 struct workspace
*workspace
;
65 workspace
= kzalloc(sizeof(*workspace
), GFP_KERNEL
);
67 return ERR_PTR(-ENOMEM
);
69 workspace
->size
= max_t(size_t,
70 ZSTD_CStreamWorkspaceBound(params
.cParams
),
71 ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT
));
72 workspace
->mem
= kvmalloc(workspace
->size
, GFP_KERNEL
);
73 workspace
->buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
74 if (!workspace
->mem
|| !workspace
->buf
)
77 INIT_LIST_HEAD(&workspace
->list
);
79 return &workspace
->list
;
81 zstd_free_workspace(&workspace
->list
);
82 return ERR_PTR(-ENOMEM
);
85 static int zstd_compress_pages(struct list_head
*ws
,
86 struct address_space
*mapping
,
89 unsigned long *out_pages
,
90 unsigned long *total_in
,
91 unsigned long *total_out
)
93 struct workspace
*workspace
= list_entry(ws
, struct workspace
, list
);
97 struct page
*in_page
= NULL
; /* The current page to read */
98 struct page
*out_page
= NULL
; /* The current page to write to */
99 unsigned long tot_in
= 0;
100 unsigned long tot_out
= 0;
101 unsigned long len
= *total_out
;
102 const unsigned long nr_dest_pages
= *out_pages
;
103 unsigned long max_out
= nr_dest_pages
* PAGE_SIZE
;
104 ZSTD_parameters params
= zstd_get_btrfs_parameters(len
);
110 /* Initialize the stream */
111 stream
= ZSTD_initCStream(params
, len
, workspace
->mem
,
114 pr_warn("BTRFS: ZSTD_initCStream failed\n");
119 /* map in the first page of input data */
120 in_page
= find_get_page(mapping
, start
>> PAGE_SHIFT
);
121 workspace
->in_buf
.src
= kmap(in_page
);
122 workspace
->in_buf
.pos
= 0;
123 workspace
->in_buf
.size
= min_t(size_t, len
, PAGE_SIZE
);
126 /* Allocate and map in the output buffer */
127 out_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
128 if (out_page
== NULL
) {
132 pages
[nr_pages
++] = out_page
;
133 workspace
->out_buf
.dst
= kmap(out_page
);
134 workspace
->out_buf
.pos
= 0;
135 workspace
->out_buf
.size
= min_t(size_t, max_out
, PAGE_SIZE
);
140 ret2
= ZSTD_compressStream(stream
, &workspace
->out_buf
,
142 if (ZSTD_isError(ret2
)) {
143 pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
144 ZSTD_getErrorCode(ret2
));
149 /* Check to see if we are making it bigger */
150 if (tot_in
+ workspace
->in_buf
.pos
> 8192 &&
151 tot_in
+ workspace
->in_buf
.pos
<
152 tot_out
+ workspace
->out_buf
.pos
) {
157 /* We've reached the end of our output range */
158 if (workspace
->out_buf
.pos
>= max_out
) {
159 tot_out
+= workspace
->out_buf
.pos
;
164 /* Check if we need more output space */
165 if (workspace
->out_buf
.pos
== workspace
->out_buf
.size
) {
166 tot_out
+= PAGE_SIZE
;
167 max_out
-= PAGE_SIZE
;
169 if (nr_pages
== nr_dest_pages
) {
174 out_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
175 if (out_page
== NULL
) {
179 pages
[nr_pages
++] = out_page
;
180 workspace
->out_buf
.dst
= kmap(out_page
);
181 workspace
->out_buf
.pos
= 0;
182 workspace
->out_buf
.size
= min_t(size_t, max_out
,
186 /* We've reached the end of the input */
187 if (workspace
->in_buf
.pos
>= len
) {
188 tot_in
+= workspace
->in_buf
.pos
;
192 /* Check if we need more input */
193 if (workspace
->in_buf
.pos
== workspace
->in_buf
.size
) {
200 in_page
= find_get_page(mapping
, start
>> PAGE_SHIFT
);
201 workspace
->in_buf
.src
= kmap(in_page
);
202 workspace
->in_buf
.pos
= 0;
203 workspace
->in_buf
.size
= min_t(size_t, len
, PAGE_SIZE
);
209 ret2
= ZSTD_endStream(stream
, &workspace
->out_buf
);
210 if (ZSTD_isError(ret2
)) {
211 pr_debug("BTRFS: ZSTD_endStream returned %d\n",
212 ZSTD_getErrorCode(ret2
));
217 tot_out
+= workspace
->out_buf
.pos
;
220 if (workspace
->out_buf
.pos
>= max_out
) {
221 tot_out
+= workspace
->out_buf
.pos
;
226 tot_out
+= PAGE_SIZE
;
227 max_out
-= PAGE_SIZE
;
229 if (nr_pages
== nr_dest_pages
) {
234 out_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
235 if (out_page
== NULL
) {
239 pages
[nr_pages
++] = out_page
;
240 workspace
->out_buf
.dst
= kmap(out_page
);
241 workspace
->out_buf
.pos
= 0;
242 workspace
->out_buf
.size
= min_t(size_t, max_out
, PAGE_SIZE
);
245 if (tot_out
>= tot_in
) {
252 *total_out
= tot_out
;
254 *out_pages
= nr_pages
;
265 static int zstd_decompress_bio(struct list_head
*ws
, struct compressed_bio
*cb
)
267 struct workspace
*workspace
= list_entry(ws
, struct workspace
, list
);
268 struct page
**pages_in
= cb
->compressed_pages
;
269 u64 disk_start
= cb
->start
;
270 struct bio
*orig_bio
= cb
->orig_bio
;
271 size_t srclen
= cb
->compressed_len
;
272 ZSTD_DStream
*stream
;
274 unsigned long page_in_index
= 0;
275 unsigned long total_pages_in
= DIV_ROUND_UP(srclen
, PAGE_SIZE
);
276 unsigned long buf_start
;
277 unsigned long total_out
= 0;
279 stream
= ZSTD_initDStream(
280 ZSTD_BTRFS_MAX_INPUT
, workspace
->mem
, workspace
->size
);
282 pr_debug("BTRFS: ZSTD_initDStream failed\n");
287 workspace
->in_buf
.src
= kmap(pages_in
[page_in_index
]);
288 workspace
->in_buf
.pos
= 0;
289 workspace
->in_buf
.size
= min_t(size_t, srclen
, PAGE_SIZE
);
291 workspace
->out_buf
.dst
= workspace
->buf
;
292 workspace
->out_buf
.pos
= 0;
293 workspace
->out_buf
.size
= PAGE_SIZE
;
298 ret2
= ZSTD_decompressStream(stream
, &workspace
->out_buf
,
300 if (ZSTD_isError(ret2
)) {
301 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
302 ZSTD_getErrorCode(ret2
));
306 buf_start
= total_out
;
307 total_out
+= workspace
->out_buf
.pos
;
308 workspace
->out_buf
.pos
= 0;
310 ret
= btrfs_decompress_buf2page(workspace
->out_buf
.dst
,
311 buf_start
, total_out
, disk_start
, orig_bio
);
315 if (workspace
->in_buf
.pos
>= srclen
)
318 /* Check if we've hit the end of a frame */
322 if (workspace
->in_buf
.pos
== workspace
->in_buf
.size
) {
323 kunmap(pages_in
[page_in_index
++]);
324 if (page_in_index
>= total_pages_in
) {
325 workspace
->in_buf
.src
= NULL
;
330 workspace
->in_buf
.src
= kmap(pages_in
[page_in_index
]);
331 workspace
->in_buf
.pos
= 0;
332 workspace
->in_buf
.size
= min_t(size_t, srclen
, PAGE_SIZE
);
336 zero_fill_bio(orig_bio
);
338 if (workspace
->in_buf
.src
)
339 kunmap(pages_in
[page_in_index
]);
343 static int zstd_decompress(struct list_head
*ws
, unsigned char *data_in
,
344 struct page
*dest_page
,
345 unsigned long start_byte
,
346 size_t srclen
, size_t destlen
)
348 struct workspace
*workspace
= list_entry(ws
, struct workspace
, list
);
349 ZSTD_DStream
*stream
;
352 unsigned long total_out
= 0;
353 unsigned long pg_offset
= 0;
356 stream
= ZSTD_initDStream(
357 ZSTD_BTRFS_MAX_INPUT
, workspace
->mem
, workspace
->size
);
359 pr_warn("BTRFS: ZSTD_initDStream failed\n");
364 destlen
= min_t(size_t, destlen
, PAGE_SIZE
);
366 workspace
->in_buf
.src
= data_in
;
367 workspace
->in_buf
.pos
= 0;
368 workspace
->in_buf
.size
= srclen
;
370 workspace
->out_buf
.dst
= workspace
->buf
;
371 workspace
->out_buf
.pos
= 0;
372 workspace
->out_buf
.size
= PAGE_SIZE
;
375 while (pg_offset
< destlen
376 && workspace
->in_buf
.pos
< workspace
->in_buf
.size
) {
377 unsigned long buf_start
;
378 unsigned long buf_offset
;
381 /* Check if the frame is over and we still need more input */
383 pr_debug("BTRFS: ZSTD_decompressStream ended early\n");
387 ret2
= ZSTD_decompressStream(stream
, &workspace
->out_buf
,
389 if (ZSTD_isError(ret2
)) {
390 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
391 ZSTD_getErrorCode(ret2
));
396 buf_start
= total_out
;
397 total_out
+= workspace
->out_buf
.pos
;
398 workspace
->out_buf
.pos
= 0;
400 if (total_out
<= start_byte
)
403 if (total_out
> start_byte
&& buf_start
< start_byte
)
404 buf_offset
= start_byte
- buf_start
;
408 bytes
= min_t(unsigned long, destlen
- pg_offset
,
409 workspace
->out_buf
.size
- buf_offset
);
411 kaddr
= kmap_atomic(dest_page
);
412 memcpy(kaddr
+ pg_offset
, workspace
->out_buf
.dst
+ buf_offset
,
414 kunmap_atomic(kaddr
);
420 if (pg_offset
< destlen
) {
421 kaddr
= kmap_atomic(dest_page
);
422 memset(kaddr
+ pg_offset
, 0, destlen
- pg_offset
);
423 kunmap_atomic(kaddr
);
428 static void zstd_set_level(struct list_head
*ws
, unsigned int type
)
432 const struct btrfs_compress_op btrfs_zstd_compress
= {
433 .alloc_workspace
= zstd_alloc_workspace
,
434 .free_workspace
= zstd_free_workspace
,
435 .compress_pages
= zstd_compress_pages
,
436 .decompress_bio
= zstd_decompress_bio
,
437 .decompress
= zstd_decompress
,
438 .set_level
= zstd_set_level
,