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
23 #define ZSTD_BTRFS_MAX_LEVEL 15
25 static ZSTD_parameters zstd_get_btrfs_parameters(unsigned int level,
28 ZSTD_parameters params = ZSTD_getParams(level, src_len, 0);
30 if (params.cParams.windowLog > ZSTD_BTRFS_MAX_WINDOWLOG)
31 params.cParams.windowLog = ZSTD_BTRFS_MAX_WINDOWLOG;
32 WARN_ON(src_len > ZSTD_BTRFS_MAX_INPUT);
40 unsigned int req_level;
41 struct list_head list;
43 ZSTD_outBuffer out_buf;
46 static struct workspace_manager wsm;
48 static size_t zstd_ws_mem_sizes[ZSTD_BTRFS_MAX_LEVEL];
51 * zstd_calc_ws_mem_sizes - calculate monotonic memory bounds
53 * It is possible based on the level configurations that a higher level
54 * workspace uses less memory than a lower level workspace. In order to reuse
55 * workspaces, this must be made a monotonic relationship. This precomputes
56 * the required memory for each level and enforces the monotonicity between
57 * level and memory required.
59 static void zstd_calc_ws_mem_sizes(void)
64 for (level = 1; level <= ZSTD_BTRFS_MAX_LEVEL; level++) {
65 ZSTD_parameters params =
66 zstd_get_btrfs_parameters(level, ZSTD_BTRFS_MAX_INPUT);
69 ZSTD_CStreamWorkspaceBound(params.cParams),
70 ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT));
72 max_size = max_t(size_t, max_size, level_size);
73 zstd_ws_mem_sizes[level - 1] = max_size;
77 static void zstd_init_workspace_manager(void)
79 zstd_calc_ws_mem_sizes();
81 btrfs_init_workspace_manager(&wsm, &btrfs_zstd_compress);
84 static void zstd_cleanup_workspace_manager(void)
86 btrfs_cleanup_workspace_manager(&wsm);
89 static struct list_head *zstd_get_workspace(unsigned int level)
91 struct list_head *ws = btrfs_get_workspace(&wsm, level);
92 struct workspace *workspace = list_entry(ws, struct workspace, list);
94 workspace->req_level = level;
99 static void zstd_put_workspace(struct list_head *ws)
101 btrfs_put_workspace(&wsm, ws);
104 static void zstd_free_workspace(struct list_head *ws)
106 struct workspace *workspace = list_entry(ws, struct workspace, list);
108 kvfree(workspace->mem);
109 kfree(workspace->buf);
113 static struct list_head *zstd_alloc_workspace(unsigned int level)
115 struct workspace *workspace;
117 workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
119 return ERR_PTR(-ENOMEM);
121 workspace->size = zstd_ws_mem_sizes[level - 1];
122 workspace->mem = kvmalloc(workspace->size, GFP_KERNEL);
123 workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
124 if (!workspace->mem || !workspace->buf)
127 INIT_LIST_HEAD(&workspace->list);
129 return &workspace->list;
131 zstd_free_workspace(&workspace->list);
132 return ERR_PTR(-ENOMEM);
135 static int zstd_compress_pages(struct list_head *ws,
136 struct address_space *mapping,
139 unsigned long *out_pages,
140 unsigned long *total_in,
141 unsigned long *total_out)
143 struct workspace *workspace = list_entry(ws, struct workspace, list);
144 ZSTD_CStream *stream;
147 struct page *in_page = NULL; /* The current page to read */
148 struct page *out_page = NULL; /* The current page to write to */
149 unsigned long tot_in = 0;
150 unsigned long tot_out = 0;
151 unsigned long len = *total_out;
152 const unsigned long nr_dest_pages = *out_pages;
153 unsigned long max_out = nr_dest_pages * PAGE_SIZE;
154 ZSTD_parameters params = zstd_get_btrfs_parameters(workspace->req_level,
161 /* Initialize the stream */
162 stream = ZSTD_initCStream(params, len, workspace->mem,
165 pr_warn("BTRFS: ZSTD_initCStream failed\n");
170 /* map in the first page of input data */
171 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
172 workspace->in_buf.src = kmap(in_page);
173 workspace->in_buf.pos = 0;
174 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
177 /* Allocate and map in the output buffer */
178 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
179 if (out_page == NULL) {
183 pages[nr_pages++] = out_page;
184 workspace->out_buf.dst = kmap(out_page);
185 workspace->out_buf.pos = 0;
186 workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
191 ret2 = ZSTD_compressStream(stream, &workspace->out_buf,
193 if (ZSTD_isError(ret2)) {
194 pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
195 ZSTD_getErrorCode(ret2));
200 /* Check to see if we are making it bigger */
201 if (tot_in + workspace->in_buf.pos > 8192 &&
202 tot_in + workspace->in_buf.pos <
203 tot_out + workspace->out_buf.pos) {
208 /* We've reached the end of our output range */
209 if (workspace->out_buf.pos >= max_out) {
210 tot_out += workspace->out_buf.pos;
215 /* Check if we need more output space */
216 if (workspace->out_buf.pos == workspace->out_buf.size) {
217 tot_out += PAGE_SIZE;
218 max_out -= PAGE_SIZE;
220 if (nr_pages == nr_dest_pages) {
225 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
226 if (out_page == NULL) {
230 pages[nr_pages++] = out_page;
231 workspace->out_buf.dst = kmap(out_page);
232 workspace->out_buf.pos = 0;
233 workspace->out_buf.size = min_t(size_t, max_out,
237 /* We've reached the end of the input */
238 if (workspace->in_buf.pos >= len) {
239 tot_in += workspace->in_buf.pos;
243 /* Check if we need more input */
244 if (workspace->in_buf.pos == workspace->in_buf.size) {
251 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
252 workspace->in_buf.src = kmap(in_page);
253 workspace->in_buf.pos = 0;
254 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
260 ret2 = ZSTD_endStream(stream, &workspace->out_buf);
261 if (ZSTD_isError(ret2)) {
262 pr_debug("BTRFS: ZSTD_endStream returned %d\n",
263 ZSTD_getErrorCode(ret2));
268 tot_out += workspace->out_buf.pos;
271 if (workspace->out_buf.pos >= max_out) {
272 tot_out += workspace->out_buf.pos;
277 tot_out += PAGE_SIZE;
278 max_out -= PAGE_SIZE;
280 if (nr_pages == nr_dest_pages) {
285 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
286 if (out_page == NULL) {
290 pages[nr_pages++] = out_page;
291 workspace->out_buf.dst = kmap(out_page);
292 workspace->out_buf.pos = 0;
293 workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
296 if (tot_out >= tot_in) {
303 *total_out = tot_out;
305 *out_pages = nr_pages;
316 static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
318 struct workspace *workspace = list_entry(ws, struct workspace, list);
319 struct page **pages_in = cb->compressed_pages;
320 u64 disk_start = cb->start;
321 struct bio *orig_bio = cb->orig_bio;
322 size_t srclen = cb->compressed_len;
323 ZSTD_DStream *stream;
325 unsigned long page_in_index = 0;
326 unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
327 unsigned long buf_start;
328 unsigned long total_out = 0;
330 stream = ZSTD_initDStream(
331 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
333 pr_debug("BTRFS: ZSTD_initDStream failed\n");
338 workspace->in_buf.src = kmap(pages_in[page_in_index]);
339 workspace->in_buf.pos = 0;
340 workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
342 workspace->out_buf.dst = workspace->buf;
343 workspace->out_buf.pos = 0;
344 workspace->out_buf.size = PAGE_SIZE;
349 ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
351 if (ZSTD_isError(ret2)) {
352 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
353 ZSTD_getErrorCode(ret2));
357 buf_start = total_out;
358 total_out += workspace->out_buf.pos;
359 workspace->out_buf.pos = 0;
361 ret = btrfs_decompress_buf2page(workspace->out_buf.dst,
362 buf_start, total_out, disk_start, orig_bio);
366 if (workspace->in_buf.pos >= srclen)
369 /* Check if we've hit the end of a frame */
373 if (workspace->in_buf.pos == workspace->in_buf.size) {
374 kunmap(pages_in[page_in_index++]);
375 if (page_in_index >= total_pages_in) {
376 workspace->in_buf.src = NULL;
381 workspace->in_buf.src = kmap(pages_in[page_in_index]);
382 workspace->in_buf.pos = 0;
383 workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
387 zero_fill_bio(orig_bio);
389 if (workspace->in_buf.src)
390 kunmap(pages_in[page_in_index]);
394 static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
395 struct page *dest_page,
396 unsigned long start_byte,
397 size_t srclen, size_t destlen)
399 struct workspace *workspace = list_entry(ws, struct workspace, list);
400 ZSTD_DStream *stream;
403 unsigned long total_out = 0;
404 unsigned long pg_offset = 0;
407 stream = ZSTD_initDStream(
408 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
410 pr_warn("BTRFS: ZSTD_initDStream failed\n");
415 destlen = min_t(size_t, destlen, PAGE_SIZE);
417 workspace->in_buf.src = data_in;
418 workspace->in_buf.pos = 0;
419 workspace->in_buf.size = srclen;
421 workspace->out_buf.dst = workspace->buf;
422 workspace->out_buf.pos = 0;
423 workspace->out_buf.size = PAGE_SIZE;
426 while (pg_offset < destlen
427 && workspace->in_buf.pos < workspace->in_buf.size) {
428 unsigned long buf_start;
429 unsigned long buf_offset;
432 /* Check if the frame is over and we still need more input */
434 pr_debug("BTRFS: ZSTD_decompressStream ended early\n");
438 ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
440 if (ZSTD_isError(ret2)) {
441 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
442 ZSTD_getErrorCode(ret2));
447 buf_start = total_out;
448 total_out += workspace->out_buf.pos;
449 workspace->out_buf.pos = 0;
451 if (total_out <= start_byte)
454 if (total_out > start_byte && buf_start < start_byte)
455 buf_offset = start_byte - buf_start;
459 bytes = min_t(unsigned long, destlen - pg_offset,
460 workspace->out_buf.size - buf_offset);
462 kaddr = kmap_atomic(dest_page);
463 memcpy(kaddr + pg_offset, workspace->out_buf.dst + buf_offset,
465 kunmap_atomic(kaddr);
471 if (pg_offset < destlen) {
472 kaddr = kmap_atomic(dest_page);
473 memset(kaddr + pg_offset, 0, destlen - pg_offset);
474 kunmap_atomic(kaddr);
479 static unsigned int zstd_set_level(unsigned int level)
481 return ZSTD_BTRFS_DEFAULT_LEVEL;
484 const struct btrfs_compress_op btrfs_zstd_compress = {
485 .init_workspace_manager = zstd_init_workspace_manager,
486 .cleanup_workspace_manager = zstd_cleanup_workspace_manager,
487 .get_workspace = zstd_get_workspace,
488 .put_workspace = zstd_put_workspace,
489 .alloc_workspace = zstd_alloc_workspace,
490 .free_workspace = zstd_free_workspace,
491 .compress_pages = zstd_compress_pages,
492 .decompress_bio = zstd_decompress_bio,
493 .decompress = zstd_decompress,
494 .set_level = zstd_set_level,