]> Git Repo - linux.git/blame - fs/btrfs/zstd.c
Merge tag 'hsi-for-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-hsi
[linux.git] / fs / btrfs / zstd.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
5c1aab1d
NT
2/*
3 * Copyright (c) 2016-present, Facebook, Inc.
4 * All rights reserved.
5 *
5c1aab1d 6 */
c1d7c514 7
5c1aab1d 8#include <linux/bio.h>
3f93aef5 9#include <linux/bitmap.h>
5c1aab1d
NT
10#include <linux/err.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/mm.h>
3f93aef5 14#include <linux/sched/mm.h>
5c1aab1d
NT
15#include <linux/pagemap.h>
16#include <linux/refcount.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/zstd.h>
20#include "compression.h"
3f93aef5 21#include "ctree.h"
5c1aab1d
NT
22
23#define ZSTD_BTRFS_MAX_WINDOWLOG 17
24#define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
25#define ZSTD_BTRFS_DEFAULT_LEVEL 3
d3c6ab75 26#define ZSTD_BTRFS_MAX_LEVEL 15
3f93aef5
DZ
27/* 307s to avoid pathologically clashing with transaction commit */
28#define ZSTD_BTRFS_RECLAIM_JIFFIES (307 * HZ)
5c1aab1d 29
e0dc87af
DZ
30static ZSTD_parameters zstd_get_btrfs_parameters(unsigned int level,
31 size_t src_len)
5c1aab1d 32{
e0dc87af 33 ZSTD_parameters params = ZSTD_getParams(level, src_len, 0);
5c1aab1d
NT
34
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);
38 return params;
39}
40
41struct workspace {
42 void *mem;
43 size_t size;
44 char *buf;
3f93aef5 45 unsigned int level;
e0dc87af 46 unsigned int req_level;
3f93aef5 47 unsigned long last_used; /* jiffies */
5c1aab1d 48 struct list_head list;
3f93aef5 49 struct list_head lru_list;
431e9822
DS
50 ZSTD_inBuffer in_buf;
51 ZSTD_outBuffer out_buf;
5c1aab1d
NT
52};
53
3f93aef5
DZ
54/*
55 * Zstd Workspace Management
56 *
57 * Zstd workspaces have different memory requirements depending on the level.
58 * The zstd workspaces are managed by having individual lists for each level
59 * and a global lru. Forward progress is maintained by protecting a max level
60 * workspace.
61 *
62 * Getting a workspace is done by using the bitmap to identify the levels that
63 * have available workspaces and scans up. This lets us recycle higher level
64 * workspaces because of the monotonic memory guarantee. A workspace's
65 * last_used is only updated if it is being used by the corresponding memory
66 * level. Putting a workspace involves adding it back to the appropriate places
67 * and adding it back to the lru if necessary.
68 *
69 * A timer is used to reclaim workspaces if they have not been used for
70 * ZSTD_BTRFS_RECLAIM_JIFFIES. This helps keep only active workspaces around.
71 * The upper bound is provided by the workqueue limit which is 2 (percpu limit).
72 */
73
74struct zstd_workspace_manager {
75 const struct btrfs_compress_op *ops;
76 spinlock_t lock;
77 struct list_head lru_list;
78 struct list_head idle_ws[ZSTD_BTRFS_MAX_LEVEL];
79 unsigned long active_map;
80 wait_queue_head_t wait;
81 struct timer_list timer;
82};
83
84static struct zstd_workspace_manager wsm;
92ee5530 85
d3c6ab75
DZ
86static size_t zstd_ws_mem_sizes[ZSTD_BTRFS_MAX_LEVEL];
87
3f93aef5
DZ
88static inline struct workspace *list_to_workspace(struct list_head *list)
89{
90 return container_of(list, struct workspace, list);
91}
92
93/*
94 * zstd_reclaim_timer_fn - reclaim timer
95 * @t: timer
96 *
97 * This scans the lru_list and attempts to reclaim any workspace that hasn't
98 * been used for ZSTD_BTRFS_RECLAIM_JIFFIES.
99 */
100static void zstd_reclaim_timer_fn(struct timer_list *timer)
101{
102 unsigned long reclaim_threshold = jiffies - ZSTD_BTRFS_RECLAIM_JIFFIES;
103 struct list_head *pos, *next;
104
105 spin_lock(&wsm.lock);
106
107 if (list_empty(&wsm.lru_list)) {
108 spin_unlock(&wsm.lock);
109 return;
110 }
111
112 list_for_each_prev_safe(pos, next, &wsm.lru_list) {
113 struct workspace *victim = container_of(pos, struct workspace,
114 lru_list);
115 unsigned int level;
116
117 if (time_after(victim->last_used, reclaim_threshold))
118 break;
119
120 /* workspace is in use */
121 if (victim->req_level)
122 continue;
123
124 level = victim->level;
125 list_del(&victim->lru_list);
126 list_del(&victim->list);
127 wsm.ops->free_workspace(&victim->list);
128
129 if (list_empty(&wsm.idle_ws[level - 1]))
130 clear_bit(level - 1, &wsm.active_map);
131
132 }
133
134 if (!list_empty(&wsm.lru_list))
135 mod_timer(&wsm.timer, jiffies + ZSTD_BTRFS_RECLAIM_JIFFIES);
136
137 spin_unlock(&wsm.lock);
138}
139
d3c6ab75
DZ
140/*
141 * zstd_calc_ws_mem_sizes - calculate monotonic memory bounds
142 *
143 * It is possible based on the level configurations that a higher level
144 * workspace uses less memory than a lower level workspace. In order to reuse
145 * workspaces, this must be made a monotonic relationship. This precomputes
146 * the required memory for each level and enforces the monotonicity between
147 * level and memory required.
148 */
149static void zstd_calc_ws_mem_sizes(void)
150{
151 size_t max_size = 0;
152 unsigned int level;
153
154 for (level = 1; level <= ZSTD_BTRFS_MAX_LEVEL; level++) {
155 ZSTD_parameters params =
156 zstd_get_btrfs_parameters(level, ZSTD_BTRFS_MAX_INPUT);
157 size_t level_size =
158 max_t(size_t,
159 ZSTD_CStreamWorkspaceBound(params.cParams),
160 ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT));
161
162 max_size = max_t(size_t, max_size, level_size);
163 zstd_ws_mem_sizes[level - 1] = max_size;
164 }
165}
166
92ee5530
DZ
167static void zstd_init_workspace_manager(void)
168{
3f93aef5
DZ
169 struct list_head *ws;
170 int i;
171
d3c6ab75
DZ
172 zstd_calc_ws_mem_sizes();
173
3f93aef5
DZ
174 wsm.ops = &btrfs_zstd_compress;
175 spin_lock_init(&wsm.lock);
176 init_waitqueue_head(&wsm.wait);
177 timer_setup(&wsm.timer, zstd_reclaim_timer_fn, 0);
178
179 INIT_LIST_HEAD(&wsm.lru_list);
180 for (i = 0; i < ZSTD_BTRFS_MAX_LEVEL; i++)
181 INIT_LIST_HEAD(&wsm.idle_ws[i]);
182
183 ws = wsm.ops->alloc_workspace(ZSTD_BTRFS_MAX_LEVEL);
184 if (IS_ERR(ws)) {
185 pr_warn(
186 "BTRFS: cannot preallocate zstd compression workspace\n");
187 } else {
188 set_bit(ZSTD_BTRFS_MAX_LEVEL - 1, &wsm.active_map);
189 list_add(ws, &wsm.idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1]);
190 }
92ee5530
DZ
191}
192
193static void zstd_cleanup_workspace_manager(void)
194{
3f93aef5
DZ
195 struct workspace *workspace;
196 int i;
197
198 del_timer(&wsm.timer);
199
200 for (i = 0; i < ZSTD_BTRFS_MAX_LEVEL; i++) {
201 while (!list_empty(&wsm.idle_ws[i])) {
202 workspace = container_of(wsm.idle_ws[i].next,
203 struct workspace, list);
204 list_del(&workspace->list);
205 list_del(&workspace->lru_list);
206 wsm.ops->free_workspace(&workspace->list);
207 }
208 }
209}
210
211/*
212 * zstd_find_workspace - find workspace
213 * @level: compression level
214 *
215 * This iterates over the set bits in the active_map beginning at the requested
216 * compression level. This lets us utilize already allocated workspaces before
217 * allocating a new one. If the workspace is of a larger size, it is used, but
218 * the place in the lru_list and last_used times are not updated. This is to
219 * offer the opportunity to reclaim the workspace in favor of allocating an
220 * appropriately sized one in the future.
221 */
222static struct list_head *zstd_find_workspace(unsigned int level)
223{
224 struct list_head *ws;
225 struct workspace *workspace;
226 int i = level - 1;
227
228 spin_lock(&wsm.lock);
229 for_each_set_bit_from(i, &wsm.active_map, ZSTD_BTRFS_MAX_LEVEL) {
230 if (!list_empty(&wsm.idle_ws[i])) {
231 ws = wsm.idle_ws[i].next;
232 workspace = list_to_workspace(ws);
233 list_del_init(ws);
234 /* keep its place if it's a lower level using this */
235 workspace->req_level = level;
236 if (level == workspace->level)
237 list_del(&workspace->lru_list);
238 if (list_empty(&wsm.idle_ws[i]))
239 clear_bit(i, &wsm.active_map);
240 spin_unlock(&wsm.lock);
241 return ws;
242 }
243 }
244 spin_unlock(&wsm.lock);
245
246 return NULL;
92ee5530
DZ
247}
248
3f93aef5
DZ
249/*
250 * zstd_get_workspace - zstd's get_workspace
251 * @level: compression level
252 *
253 * If @level is 0, then any compression level can be used. Therefore, we begin
254 * scanning from 1. We first scan through possible workspaces and then after
255 * attempt to allocate a new workspace. If we fail to allocate one due to
256 * memory pressure, go to sleep waiting for the max level workspace to free up.
257 */
7bf49943 258static struct list_head *zstd_get_workspace(unsigned int level)
92ee5530 259{
3f93aef5
DZ
260 struct list_head *ws;
261 unsigned int nofs_flag;
e0dc87af 262
3f93aef5
DZ
263 /* level == 0 means we can use any workspace */
264 if (!level)
265 level = 1;
266
267again:
268 ws = zstd_find_workspace(level);
269 if (ws)
270 return ws;
271
272 nofs_flag = memalloc_nofs_save();
273 ws = wsm.ops->alloc_workspace(level);
274 memalloc_nofs_restore(nofs_flag);
275
276 if (IS_ERR(ws)) {
277 DEFINE_WAIT(wait);
278
279 prepare_to_wait(&wsm.wait, &wait, TASK_UNINTERRUPTIBLE);
280 schedule();
281 finish_wait(&wsm.wait, &wait);
282
283 goto again;
284 }
e0dc87af
DZ
285
286 return ws;
92ee5530
DZ
287}
288
3f93aef5
DZ
289/*
290 * zstd_put_workspace - zstd put_workspace
291 * @ws: list_head for the workspace
292 *
293 * When putting back a workspace, we only need to update the LRU if we are of
294 * the requested compression level. Here is where we continue to protect the
295 * max level workspace or update last_used accordingly. If the reclaim timer
296 * isn't set, it is also set here. Only the max level workspace tries and wakes
297 * up waiting workspaces.
298 */
92ee5530
DZ
299static void zstd_put_workspace(struct list_head *ws)
300{
3f93aef5
DZ
301 struct workspace *workspace = list_to_workspace(ws);
302
303 spin_lock(&wsm.lock);
304
305 /* A node is only taken off the lru if we are the corresponding level */
306 if (workspace->req_level == workspace->level) {
307 /* Hide a max level workspace from reclaim */
308 if (list_empty(&wsm.idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1])) {
309 INIT_LIST_HEAD(&workspace->lru_list);
310 } else {
311 workspace->last_used = jiffies;
312 list_add(&workspace->lru_list, &wsm.lru_list);
313 if (!timer_pending(&wsm.timer))
314 mod_timer(&wsm.timer,
315 jiffies + ZSTD_BTRFS_RECLAIM_JIFFIES);
316 }
317 }
318
319 set_bit(workspace->level - 1, &wsm.active_map);
320 list_add(&workspace->list, &wsm.idle_ws[workspace->level - 1]);
321 workspace->req_level = 0;
322
323 spin_unlock(&wsm.lock);
324
325 if (workspace->level == ZSTD_BTRFS_MAX_LEVEL)
326 cond_wake_up(&wsm.wait);
92ee5530
DZ
327}
328
5c1aab1d
NT
329static void zstd_free_workspace(struct list_head *ws)
330{
331 struct workspace *workspace = list_entry(ws, struct workspace, list);
332
333 kvfree(workspace->mem);
334 kfree(workspace->buf);
335 kfree(workspace);
336}
337
7bf49943 338static struct list_head *zstd_alloc_workspace(unsigned int level)
5c1aab1d 339{
5c1aab1d
NT
340 struct workspace *workspace;
341
342 workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
343 if (!workspace)
344 return ERR_PTR(-ENOMEM);
345
d3c6ab75 346 workspace->size = zstd_ws_mem_sizes[level - 1];
3f93aef5
DZ
347 workspace->level = level;
348 workspace->req_level = level;
349 workspace->last_used = jiffies;
5c1aab1d
NT
350 workspace->mem = kvmalloc(workspace->size, GFP_KERNEL);
351 workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
352 if (!workspace->mem || !workspace->buf)
353 goto fail;
354
355 INIT_LIST_HEAD(&workspace->list);
3f93aef5 356 INIT_LIST_HEAD(&workspace->lru_list);
5c1aab1d
NT
357
358 return &workspace->list;
359fail:
360 zstd_free_workspace(&workspace->list);
361 return ERR_PTR(-ENOMEM);
362}
363
364static int zstd_compress_pages(struct list_head *ws,
365 struct address_space *mapping,
366 u64 start,
367 struct page **pages,
368 unsigned long *out_pages,
369 unsigned long *total_in,
370 unsigned long *total_out)
371{
372 struct workspace *workspace = list_entry(ws, struct workspace, list);
373 ZSTD_CStream *stream;
374 int ret = 0;
375 int nr_pages = 0;
376 struct page *in_page = NULL; /* The current page to read */
377 struct page *out_page = NULL; /* The current page to write to */
5c1aab1d
NT
378 unsigned long tot_in = 0;
379 unsigned long tot_out = 0;
380 unsigned long len = *total_out;
381 const unsigned long nr_dest_pages = *out_pages;
382 unsigned long max_out = nr_dest_pages * PAGE_SIZE;
e0dc87af
DZ
383 ZSTD_parameters params = zstd_get_btrfs_parameters(workspace->req_level,
384 len);
5c1aab1d
NT
385
386 *out_pages = 0;
387 *total_out = 0;
388 *total_in = 0;
389
390 /* Initialize the stream */
391 stream = ZSTD_initCStream(params, len, workspace->mem,
392 workspace->size);
393 if (!stream) {
394 pr_warn("BTRFS: ZSTD_initCStream failed\n");
395 ret = -EIO;
396 goto out;
397 }
398
399 /* map in the first page of input data */
400 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
431e9822
DS
401 workspace->in_buf.src = kmap(in_page);
402 workspace->in_buf.pos = 0;
403 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
5c1aab1d
NT
404
405
406 /* Allocate and map in the output buffer */
407 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
408 if (out_page == NULL) {
409 ret = -ENOMEM;
410 goto out;
411 }
412 pages[nr_pages++] = out_page;
431e9822
DS
413 workspace->out_buf.dst = kmap(out_page);
414 workspace->out_buf.pos = 0;
415 workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
5c1aab1d
NT
416
417 while (1) {
418 size_t ret2;
419
431e9822
DS
420 ret2 = ZSTD_compressStream(stream, &workspace->out_buf,
421 &workspace->in_buf);
5c1aab1d
NT
422 if (ZSTD_isError(ret2)) {
423 pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
424 ZSTD_getErrorCode(ret2));
425 ret = -EIO;
426 goto out;
427 }
428
429 /* Check to see if we are making it bigger */
431e9822
DS
430 if (tot_in + workspace->in_buf.pos > 8192 &&
431 tot_in + workspace->in_buf.pos <
432 tot_out + workspace->out_buf.pos) {
5c1aab1d
NT
433 ret = -E2BIG;
434 goto out;
435 }
436
437 /* We've reached the end of our output range */
431e9822
DS
438 if (workspace->out_buf.pos >= max_out) {
439 tot_out += workspace->out_buf.pos;
5c1aab1d
NT
440 ret = -E2BIG;
441 goto out;
442 }
443
444 /* Check if we need more output space */
431e9822 445 if (workspace->out_buf.pos == workspace->out_buf.size) {
5c1aab1d
NT
446 tot_out += PAGE_SIZE;
447 max_out -= PAGE_SIZE;
448 kunmap(out_page);
449 if (nr_pages == nr_dest_pages) {
450 out_page = NULL;
451 ret = -E2BIG;
452 goto out;
453 }
454 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
455 if (out_page == NULL) {
456 ret = -ENOMEM;
457 goto out;
458 }
459 pages[nr_pages++] = out_page;
431e9822
DS
460 workspace->out_buf.dst = kmap(out_page);
461 workspace->out_buf.pos = 0;
462 workspace->out_buf.size = min_t(size_t, max_out,
463 PAGE_SIZE);
5c1aab1d
NT
464 }
465
466 /* We've reached the end of the input */
431e9822
DS
467 if (workspace->in_buf.pos >= len) {
468 tot_in += workspace->in_buf.pos;
5c1aab1d
NT
469 break;
470 }
471
472 /* Check if we need more input */
431e9822 473 if (workspace->in_buf.pos == workspace->in_buf.size) {
5c1aab1d
NT
474 tot_in += PAGE_SIZE;
475 kunmap(in_page);
476 put_page(in_page);
477
478 start += PAGE_SIZE;
479 len -= PAGE_SIZE;
480 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
431e9822
DS
481 workspace->in_buf.src = kmap(in_page);
482 workspace->in_buf.pos = 0;
483 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
5c1aab1d
NT
484 }
485 }
486 while (1) {
487 size_t ret2;
488
431e9822 489 ret2 = ZSTD_endStream(stream, &workspace->out_buf);
5c1aab1d
NT
490 if (ZSTD_isError(ret2)) {
491 pr_debug("BTRFS: ZSTD_endStream returned %d\n",
492 ZSTD_getErrorCode(ret2));
493 ret = -EIO;
494 goto out;
495 }
496 if (ret2 == 0) {
431e9822 497 tot_out += workspace->out_buf.pos;
5c1aab1d
NT
498 break;
499 }
431e9822
DS
500 if (workspace->out_buf.pos >= max_out) {
501 tot_out += workspace->out_buf.pos;
5c1aab1d
NT
502 ret = -E2BIG;
503 goto out;
504 }
505
506 tot_out += PAGE_SIZE;
507 max_out -= PAGE_SIZE;
508 kunmap(out_page);
509 if (nr_pages == nr_dest_pages) {
510 out_page = NULL;
511 ret = -E2BIG;
512 goto out;
513 }
514 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
515 if (out_page == NULL) {
516 ret = -ENOMEM;
517 goto out;
518 }
519 pages[nr_pages++] = out_page;
431e9822
DS
520 workspace->out_buf.dst = kmap(out_page);
521 workspace->out_buf.pos = 0;
522 workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
5c1aab1d
NT
523 }
524
525 if (tot_out >= tot_in) {
526 ret = -E2BIG;
527 goto out;
528 }
529
530 ret = 0;
531 *total_in = tot_in;
532 *total_out = tot_out;
533out:
534 *out_pages = nr_pages;
535 /* Cleanup */
536 if (in_page) {
537 kunmap(in_page);
538 put_page(in_page);
539 }
540 if (out_page)
541 kunmap(out_page);
542 return ret;
543}
544
545static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
546{
547 struct workspace *workspace = list_entry(ws, struct workspace, list);
548 struct page **pages_in = cb->compressed_pages;
549 u64 disk_start = cb->start;
550 struct bio *orig_bio = cb->orig_bio;
551 size_t srclen = cb->compressed_len;
552 ZSTD_DStream *stream;
553 int ret = 0;
554 unsigned long page_in_index = 0;
555 unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
556 unsigned long buf_start;
557 unsigned long total_out = 0;
5c1aab1d
NT
558
559 stream = ZSTD_initDStream(
560 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
561 if (!stream) {
562 pr_debug("BTRFS: ZSTD_initDStream failed\n");
563 ret = -EIO;
564 goto done;
565 }
566
431e9822
DS
567 workspace->in_buf.src = kmap(pages_in[page_in_index]);
568 workspace->in_buf.pos = 0;
569 workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
5c1aab1d 570
431e9822
DS
571 workspace->out_buf.dst = workspace->buf;
572 workspace->out_buf.pos = 0;
573 workspace->out_buf.size = PAGE_SIZE;
5c1aab1d
NT
574
575 while (1) {
576 size_t ret2;
577
431e9822
DS
578 ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
579 &workspace->in_buf);
5c1aab1d
NT
580 if (ZSTD_isError(ret2)) {
581 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
582 ZSTD_getErrorCode(ret2));
583 ret = -EIO;
584 goto done;
585 }
586 buf_start = total_out;
431e9822
DS
587 total_out += workspace->out_buf.pos;
588 workspace->out_buf.pos = 0;
5c1aab1d 589
431e9822
DS
590 ret = btrfs_decompress_buf2page(workspace->out_buf.dst,
591 buf_start, total_out, disk_start, orig_bio);
5c1aab1d
NT
592 if (ret == 0)
593 break;
594
431e9822 595 if (workspace->in_buf.pos >= srclen)
5c1aab1d
NT
596 break;
597
598 /* Check if we've hit the end of a frame */
599 if (ret2 == 0)
600 break;
601
431e9822 602 if (workspace->in_buf.pos == workspace->in_buf.size) {
5c1aab1d
NT
603 kunmap(pages_in[page_in_index++]);
604 if (page_in_index >= total_pages_in) {
431e9822 605 workspace->in_buf.src = NULL;
5c1aab1d
NT
606 ret = -EIO;
607 goto done;
608 }
609 srclen -= PAGE_SIZE;
431e9822
DS
610 workspace->in_buf.src = kmap(pages_in[page_in_index]);
611 workspace->in_buf.pos = 0;
612 workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
5c1aab1d
NT
613 }
614 }
615 ret = 0;
616 zero_fill_bio(orig_bio);
617done:
431e9822 618 if (workspace->in_buf.src)
5c1aab1d
NT
619 kunmap(pages_in[page_in_index]);
620 return ret;
621}
622
623static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
624 struct page *dest_page,
625 unsigned long start_byte,
626 size_t srclen, size_t destlen)
627{
628 struct workspace *workspace = list_entry(ws, struct workspace, list);
629 ZSTD_DStream *stream;
630 int ret = 0;
631 size_t ret2;
5c1aab1d
NT
632 unsigned long total_out = 0;
633 unsigned long pg_offset = 0;
634 char *kaddr;
635
636 stream = ZSTD_initDStream(
637 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
638 if (!stream) {
639 pr_warn("BTRFS: ZSTD_initDStream failed\n");
640 ret = -EIO;
641 goto finish;
642 }
643
644 destlen = min_t(size_t, destlen, PAGE_SIZE);
645
431e9822
DS
646 workspace->in_buf.src = data_in;
647 workspace->in_buf.pos = 0;
648 workspace->in_buf.size = srclen;
5c1aab1d 649
431e9822
DS
650 workspace->out_buf.dst = workspace->buf;
651 workspace->out_buf.pos = 0;
652 workspace->out_buf.size = PAGE_SIZE;
5c1aab1d
NT
653
654 ret2 = 1;
431e9822
DS
655 while (pg_offset < destlen
656 && workspace->in_buf.pos < workspace->in_buf.size) {
5c1aab1d
NT
657 unsigned long buf_start;
658 unsigned long buf_offset;
659 unsigned long bytes;
660
661 /* Check if the frame is over and we still need more input */
662 if (ret2 == 0) {
663 pr_debug("BTRFS: ZSTD_decompressStream ended early\n");
664 ret = -EIO;
665 goto finish;
666 }
431e9822
DS
667 ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
668 &workspace->in_buf);
5c1aab1d
NT
669 if (ZSTD_isError(ret2)) {
670 pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
671 ZSTD_getErrorCode(ret2));
672 ret = -EIO;
673 goto finish;
674 }
675
676 buf_start = total_out;
431e9822
DS
677 total_out += workspace->out_buf.pos;
678 workspace->out_buf.pos = 0;
5c1aab1d
NT
679
680 if (total_out <= start_byte)
681 continue;
682
683 if (total_out > start_byte && buf_start < start_byte)
684 buf_offset = start_byte - buf_start;
685 else
686 buf_offset = 0;
687
688 bytes = min_t(unsigned long, destlen - pg_offset,
431e9822 689 workspace->out_buf.size - buf_offset);
5c1aab1d
NT
690
691 kaddr = kmap_atomic(dest_page);
431e9822
DS
692 memcpy(kaddr + pg_offset, workspace->out_buf.dst + buf_offset,
693 bytes);
5c1aab1d
NT
694 kunmap_atomic(kaddr);
695
696 pg_offset += bytes;
697 }
698 ret = 0;
699finish:
700 if (pg_offset < destlen) {
701 kaddr = kmap_atomic(dest_page);
702 memset(kaddr + pg_offset, 0, destlen - pg_offset);
703 kunmap_atomic(kaddr);
704 }
705 return ret;
706}
707
d0ab62ce 708static unsigned int zstd_set_level(unsigned int level)
f51d2b59 709{
3f93aef5
DZ
710 if (!level)
711 return ZSTD_BTRFS_DEFAULT_LEVEL;
712
713 return min_t(unsigned int, level, ZSTD_BTRFS_MAX_LEVEL);
f51d2b59
DS
714}
715
5c1aab1d 716const struct btrfs_compress_op btrfs_zstd_compress = {
92ee5530
DZ
717 .init_workspace_manager = zstd_init_workspace_manager,
718 .cleanup_workspace_manager = zstd_cleanup_workspace_manager,
719 .get_workspace = zstd_get_workspace,
720 .put_workspace = zstd_put_workspace,
5c1aab1d
NT
721 .alloc_workspace = zstd_alloc_workspace,
722 .free_workspace = zstd_free_workspace,
723 .compress_pages = zstd_compress_pages,
724 .decompress_bio = zstd_decompress_bio,
725 .decompress = zstd_decompress,
f51d2b59 726 .set_level = zstd_set_level,
5c1aab1d 727};
This page took 0.243122 seconds and 4 git commands to generate.