1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016-present, Facebook, Inc.
10 #include <linux/init.h>
11 #include <linux/kernel.h>
13 #include <linux/pagemap.h>
14 #include <linux/refcount.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/zstd.h>
18 #include "compression.h"
20 #define ZSTD_BTRFS_MAX_WINDOWLOG 17
21 #define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
22 #define ZSTD_BTRFS_DEFAULT_LEVEL 3
24 static ZSTD_parameters zstd_get_btrfs_parameters(unsigned int level,
27 ZSTD_parameters params = ZSTD_getParams(level, src_len, 0);
29 if (params.cParams.windowLog > ZSTD_BTRFS_MAX_WINDOWLOG)
30 params.cParams.windowLog = ZSTD_BTRFS_MAX_WINDOWLOG;
31 WARN_ON(src_len > ZSTD_BTRFS_MAX_INPUT);
39 unsigned int req_level;
40 struct list_head list;
42 ZSTD_outBuffer out_buf;
45 static struct workspace_manager wsm;
47 static void zstd_init_workspace_manager(void)
49 btrfs_init_workspace_manager(&wsm, &btrfs_zstd_compress);
52 static void zstd_cleanup_workspace_manager(void)
54 btrfs_cleanup_workspace_manager(&wsm);
57 static struct list_head *zstd_get_workspace(unsigned int level)
59 struct list_head *ws = btrfs_get_workspace(&wsm, level);
60 struct workspace *workspace = list_entry(ws, struct workspace, list);
62 workspace->req_level = level;
67 static void zstd_put_workspace(struct list_head *ws)
69 btrfs_put_workspace(&wsm, ws);
72 static void zstd_free_workspace(struct list_head *ws)
74 struct workspace *workspace = list_entry(ws, struct workspace, list);
76 kvfree(workspace->mem);
77 kfree(workspace->buf);
81 static struct list_head *zstd_alloc_workspace(unsigned int level)
83 ZSTD_parameters params =
84 zstd_get_btrfs_parameters(level, ZSTD_BTRFS_MAX_INPUT);
85 struct workspace *workspace;
87 workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
89 return ERR_PTR(-ENOMEM);
91 workspace->size = max_t(size_t,
92 ZSTD_CStreamWorkspaceBound(params.cParams),
93 ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT));
94 workspace->mem = kvmalloc(workspace->size, GFP_KERNEL);
95 workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
96 if (!workspace->mem || !workspace->buf)
99 INIT_LIST_HEAD(&workspace->list);
101 return &workspace->list;
103 zstd_free_workspace(&workspace->list);
104 return ERR_PTR(-ENOMEM);
107 static int zstd_compress_pages(struct list_head *ws,
108 struct address_space *mapping,
111 unsigned long *out_pages,
112 unsigned long *total_in,
113 unsigned long *total_out)
115 struct workspace *workspace = list_entry(ws, struct workspace, list);
116 ZSTD_CStream *stream;
119 struct page *in_page = NULL; /* The current page to read */
120 struct page *out_page = NULL; /* The current page to write to */
121 unsigned long tot_in = 0;
122 unsigned long tot_out = 0;
123 unsigned long len = *total_out;
124 const unsigned long nr_dest_pages = *out_pages;
125 unsigned long max_out = nr_dest_pages * PAGE_SIZE;
126 ZSTD_parameters params = zstd_get_btrfs_parameters(workspace->req_level,
133 /* Initialize the stream */
134 stream = ZSTD_initCStream(params, len, workspace->mem,
137 pr_warn("BTRFS: ZSTD_initCStream failed\n");
142 /* map in the first page of input data */
143 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
144 workspace->in_buf.src = kmap(in_page);
145 workspace->in_buf.pos = 0;
146 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
149 /* Allocate and map in the output buffer */
150 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
151 if (out_page == NULL) {
155 pages[nr_pages++] = out_page;
156 workspace->out_buf.dst = kmap(out_page);
157 workspace->out_buf.pos = 0;
158 workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
163 ret2 = ZSTD_compressStream(stream, &workspace->out_buf,
165 if (ZSTD_isError(ret2)) {
166 pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
167 ZSTD_getErrorCode(ret2));
172 /* Check to see if we are making it bigger */
173 if (tot_in + workspace->in_buf.pos > 8192 &&
174 tot_in + workspace->in_buf.pos <
175 tot_out + workspace->out_buf.pos) {
180 /* We've reached the end of our output range */
181 if (workspace->out_buf.pos >= max_out) {
182 tot_out += workspace->out_buf.pos;
187 /* Check if we need more output space */
188 if (workspace->out_buf.pos == workspace->out_buf.size) {
189 tot_out += PAGE_SIZE;
190 max_out -= PAGE_SIZE;
192 if (nr_pages == nr_dest_pages) {
197 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
198 if (out_page == NULL) {
202 pages[nr_pages++] = out_page;
203 workspace->out_buf.dst = kmap(out_page);
204 workspace->out_buf.pos = 0;
205 workspace->out_buf.size = min_t(size_t, max_out,
209 /* We've reached the end of the input */
210 if (workspace->in_buf.pos >= len) {
211 tot_in += workspace->in_buf.pos;
215 /* Check if we need more input */
216 if (workspace->in_buf.pos == workspace->in_buf.size) {
223 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
224 workspace->in_buf.src = kmap(in_page);
225 workspace->in_buf.pos = 0;
226 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
232 ret2 = ZSTD_endStream(stream, &workspace->out_buf);
233 if (ZSTD_isError(ret2)) {
234 pr_debug("BTRFS: ZSTD_endStream returned %d\n",
235 ZSTD_getErrorCode(ret2));
240 tot_out += workspace->out_buf.pos;
243 if (workspace->out_buf.pos >= max_out) {
244 tot_out += workspace->out_buf.pos;
249 tot_out += PAGE_SIZE;
250 max_out -= PAGE_SIZE;
252 if (nr_pages == nr_dest_pages) {
257 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
258 if (out_page == NULL) {
262 pages[nr_pages++] = out_page;
263 workspace->out_buf.dst = kmap(out_page);
264 workspace->out_buf.pos = 0;
265 workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
268 if (tot_out >= tot_in) {
275 *total_out = tot_out;
277 *out_pages = nr_pages;
288 static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
290 struct workspace *workspace = list_entry(ws, struct workspace, list);
291 struct page **pages_in = cb->compressed_pages;
292 u64 disk_start = cb->start;
293 struct bio *orig_bio = cb->orig_bio;
294 size_t srclen = cb->compressed_len;
295 ZSTD_DStream *stream;
297 unsigned long page_in_index = 0;
298 unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
299 unsigned long buf_start;
300 unsigned long total_out = 0;
302 stream = ZSTD_initDStream(
303 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
305 pr_debug("BTRFS: ZSTD_initDStream failed\n");
310 workspace->in_buf.src = kmap(pages_in[page_in_index]);
311 workspace->in_buf.pos = 0;
312 workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
314 workspace->out_buf.dst = workspace->buf;
315 workspace->out_buf.pos = 0;
316 workspace->out_buf.size = PAGE_SIZE;
321 ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
323 if (ZSTD_isError(ret2)) {
324 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
325 ZSTD_getErrorCode(ret2));
329 buf_start = total_out;
330 total_out += workspace->out_buf.pos;
331 workspace->out_buf.pos = 0;
333 ret = btrfs_decompress_buf2page(workspace->out_buf.dst,
334 buf_start, total_out, disk_start, orig_bio);
338 if (workspace->in_buf.pos >= srclen)
341 /* Check if we've hit the end of a frame */
345 if (workspace->in_buf.pos == workspace->in_buf.size) {
346 kunmap(pages_in[page_in_index++]);
347 if (page_in_index >= total_pages_in) {
348 workspace->in_buf.src = NULL;
353 workspace->in_buf.src = kmap(pages_in[page_in_index]);
354 workspace->in_buf.pos = 0;
355 workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
359 zero_fill_bio(orig_bio);
361 if (workspace->in_buf.src)
362 kunmap(pages_in[page_in_index]);
366 static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
367 struct page *dest_page,
368 unsigned long start_byte,
369 size_t srclen, size_t destlen)
371 struct workspace *workspace = list_entry(ws, struct workspace, list);
372 ZSTD_DStream *stream;
375 unsigned long total_out = 0;
376 unsigned long pg_offset = 0;
379 stream = ZSTD_initDStream(
380 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
382 pr_warn("BTRFS: ZSTD_initDStream failed\n");
387 destlen = min_t(size_t, destlen, PAGE_SIZE);
389 workspace->in_buf.src = data_in;
390 workspace->in_buf.pos = 0;
391 workspace->in_buf.size = srclen;
393 workspace->out_buf.dst = workspace->buf;
394 workspace->out_buf.pos = 0;
395 workspace->out_buf.size = PAGE_SIZE;
398 while (pg_offset < destlen
399 && workspace->in_buf.pos < workspace->in_buf.size) {
400 unsigned long buf_start;
401 unsigned long buf_offset;
404 /* Check if the frame is over and we still need more input */
406 pr_debug("BTRFS: ZSTD_decompressStream ended early\n");
410 ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
412 if (ZSTD_isError(ret2)) {
413 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
414 ZSTD_getErrorCode(ret2));
419 buf_start = total_out;
420 total_out += workspace->out_buf.pos;
421 workspace->out_buf.pos = 0;
423 if (total_out <= start_byte)
426 if (total_out > start_byte && buf_start < start_byte)
427 buf_offset = start_byte - buf_start;
431 bytes = min_t(unsigned long, destlen - pg_offset,
432 workspace->out_buf.size - buf_offset);
434 kaddr = kmap_atomic(dest_page);
435 memcpy(kaddr + pg_offset, workspace->out_buf.dst + buf_offset,
437 kunmap_atomic(kaddr);
443 if (pg_offset < destlen) {
444 kaddr = kmap_atomic(dest_page);
445 memset(kaddr + pg_offset, 0, destlen - pg_offset);
446 kunmap_atomic(kaddr);
451 static unsigned int zstd_set_level(unsigned int level)
453 return ZSTD_BTRFS_DEFAULT_LEVEL;
456 const struct btrfs_compress_op btrfs_zstd_compress = {
457 .init_workspace_manager = zstd_init_workspace_manager,
458 .cleanup_workspace_manager = zstd_cleanup_workspace_manager,
459 .get_workspace = zstd_get_workspace,
460 .put_workspace = zstd_put_workspace,
461 .alloc_workspace = zstd_alloc_workspace,
462 .free_workspace = zstd_free_workspace,
463 .compress_pages = zstd_compress_pages,
464 .decompress_bio = zstd_decompress_bio,
465 .decompress = zstd_decompress,
466 .set_level = zstd_set_level,