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;
48 static void zstd_free_workspace(struct list_head *ws)
50 struct workspace *workspace = list_entry(ws, struct workspace, list);
52 kvfree(workspace->mem);
53 kfree(workspace->buf);
57 static struct list_head *zstd_alloc_workspace(void)
59 ZSTD_parameters params =
60 zstd_get_btrfs_parameters(ZSTD_BTRFS_MAX_INPUT);
61 struct workspace *workspace;
63 workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
65 return ERR_PTR(-ENOMEM);
67 workspace->size = max_t(size_t,
68 ZSTD_CStreamWorkspaceBound(params.cParams),
69 ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT));
70 workspace->mem = kvmalloc(workspace->size, GFP_KERNEL);
71 workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
72 if (!workspace->mem || !workspace->buf)
75 INIT_LIST_HEAD(&workspace->list);
77 return &workspace->list;
79 zstd_free_workspace(&workspace->list);
80 return ERR_PTR(-ENOMEM);
83 static int zstd_compress_pages(struct list_head *ws,
84 struct address_space *mapping,
87 unsigned long *out_pages,
88 unsigned long *total_in,
89 unsigned long *total_out)
91 struct workspace *workspace = list_entry(ws, struct workspace, list);
95 struct page *in_page = NULL; /* The current page to read */
96 struct page *out_page = NULL; /* The current page to write to */
97 ZSTD_inBuffer in_buf = { NULL, 0, 0 };
98 ZSTD_outBuffer out_buf = { NULL, 0, 0 };
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 in_buf.src = kmap(in_page);
123 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 out_buf.dst = kmap(out_page);
135 out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
140 ret2 = ZSTD_compressStream(stream, &out_buf, &in_buf);
141 if (ZSTD_isError(ret2)) {
142 pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
143 ZSTD_getErrorCode(ret2));
148 /* Check to see if we are making it bigger */
149 if (tot_in + in_buf.pos > 8192 &&
150 tot_in + in_buf.pos <
151 tot_out + out_buf.pos) {
156 /* We've reached the end of our output range */
157 if (out_buf.pos >= max_out) {
158 tot_out += out_buf.pos;
163 /* Check if we need more output space */
164 if (out_buf.pos == out_buf.size) {
165 tot_out += PAGE_SIZE;
166 max_out -= PAGE_SIZE;
168 if (nr_pages == nr_dest_pages) {
173 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
174 if (out_page == NULL) {
178 pages[nr_pages++] = out_page;
179 out_buf.dst = kmap(out_page);
181 out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
184 /* We've reached the end of the input */
185 if (in_buf.pos >= len) {
186 tot_in += in_buf.pos;
190 /* Check if we need more input */
191 if (in_buf.pos == in_buf.size) {
198 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
199 in_buf.src = kmap(in_page);
201 in_buf.size = min_t(size_t, len, PAGE_SIZE);
207 ret2 = ZSTD_endStream(stream, &out_buf);
208 if (ZSTD_isError(ret2)) {
209 pr_debug("BTRFS: ZSTD_endStream returned %d\n",
210 ZSTD_getErrorCode(ret2));
215 tot_out += out_buf.pos;
218 if (out_buf.pos >= max_out) {
219 tot_out += out_buf.pos;
224 tot_out += PAGE_SIZE;
225 max_out -= PAGE_SIZE;
227 if (nr_pages == nr_dest_pages) {
232 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
233 if (out_page == NULL) {
237 pages[nr_pages++] = out_page;
238 out_buf.dst = kmap(out_page);
240 out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
243 if (tot_out >= tot_in) {
250 *total_out = tot_out;
252 *out_pages = nr_pages;
263 static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
265 struct workspace *workspace = list_entry(ws, struct workspace, list);
266 struct page **pages_in = cb->compressed_pages;
267 u64 disk_start = cb->start;
268 struct bio *orig_bio = cb->orig_bio;
269 size_t srclen = cb->compressed_len;
270 ZSTD_DStream *stream;
272 unsigned long page_in_index = 0;
273 unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
274 unsigned long buf_start;
275 unsigned long total_out = 0;
276 ZSTD_inBuffer in_buf = { NULL, 0, 0 };
277 ZSTD_outBuffer out_buf = { NULL, 0, 0 };
279 stream = ZSTD_initDStream(
280 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
282 pr_debug("BTRFS: ZSTD_initDStream failed\n");
287 in_buf.src = kmap(pages_in[page_in_index]);
289 in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
291 out_buf.dst = workspace->buf;
293 out_buf.size = PAGE_SIZE;
298 ret2 = ZSTD_decompressStream(stream, &out_buf, &in_buf);
299 if (ZSTD_isError(ret2)) {
300 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
301 ZSTD_getErrorCode(ret2));
305 buf_start = total_out;
306 total_out += out_buf.pos;
309 ret = btrfs_decompress_buf2page(out_buf.dst, buf_start,
310 total_out, disk_start, orig_bio);
314 if (in_buf.pos >= srclen)
317 /* Check if we've hit the end of a frame */
321 if (in_buf.pos == in_buf.size) {
322 kunmap(pages_in[page_in_index++]);
323 if (page_in_index >= total_pages_in) {
329 in_buf.src = kmap(pages_in[page_in_index]);
331 in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
335 zero_fill_bio(orig_bio);
338 kunmap(pages_in[page_in_index]);
342 static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
343 struct page *dest_page,
344 unsigned long start_byte,
345 size_t srclen, size_t destlen)
347 struct workspace *workspace = list_entry(ws, struct workspace, list);
348 ZSTD_DStream *stream;
351 ZSTD_inBuffer in_buf = { NULL, 0, 0 };
352 ZSTD_outBuffer out_buf = { NULL, 0, 0 };
353 unsigned long total_out = 0;
354 unsigned long pg_offset = 0;
357 stream = ZSTD_initDStream(
358 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
360 pr_warn("BTRFS: ZSTD_initDStream failed\n");
365 destlen = min_t(size_t, destlen, PAGE_SIZE);
367 in_buf.src = data_in;
369 in_buf.size = srclen;
371 out_buf.dst = workspace->buf;
373 out_buf.size = PAGE_SIZE;
376 while (pg_offset < destlen && in_buf.pos < 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, &out_buf, &in_buf);
388 if (ZSTD_isError(ret2)) {
389 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
390 ZSTD_getErrorCode(ret2));
395 buf_start = total_out;
396 total_out += out_buf.pos;
399 if (total_out <= start_byte)
402 if (total_out > start_byte && buf_start < start_byte)
403 buf_offset = start_byte - buf_start;
407 bytes = min_t(unsigned long, destlen - pg_offset,
408 out_buf.size - buf_offset);
410 kaddr = kmap_atomic(dest_page);
411 memcpy(kaddr + pg_offset, out_buf.dst + buf_offset, bytes);
412 kunmap_atomic(kaddr);
418 if (pg_offset < destlen) {
419 kaddr = kmap_atomic(dest_page);
420 memset(kaddr + pg_offset, 0, destlen - pg_offset);
421 kunmap_atomic(kaddr);
426 const struct btrfs_compress_op btrfs_zstd_compress = {
427 .alloc_workspace = zstd_alloc_workspace,
428 .free_workspace = zstd_free_workspace,
429 .compress_pages = zstd_compress_pages,
430 .decompress_bio = zstd_decompress_bio,
431 .decompress = zstd_decompress,