xref: /openbmc/linux/fs/btrfs/zstd.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
25c1aab1dSNick Terrell /*
35c1aab1dSNick Terrell  * Copyright (c) 2016-present, Facebook, Inc.
45c1aab1dSNick Terrell  * All rights reserved.
55c1aab1dSNick Terrell  *
65c1aab1dSNick Terrell  */
7c1d7c514SDavid Sterba 
85c1aab1dSNick Terrell #include <linux/bio.h>
93f93aef5SDennis Zhou #include <linux/bitmap.h>
105c1aab1dSNick Terrell #include <linux/err.h>
115c1aab1dSNick Terrell #include <linux/init.h>
125c1aab1dSNick Terrell #include <linux/kernel.h>
135c1aab1dSNick Terrell #include <linux/mm.h>
143f93aef5SDennis Zhou #include <linux/sched/mm.h>
155c1aab1dSNick Terrell #include <linux/pagemap.h>
165c1aab1dSNick Terrell #include <linux/refcount.h>
175c1aab1dSNick Terrell #include <linux/sched.h>
185c1aab1dSNick Terrell #include <linux/slab.h>
195c1aab1dSNick Terrell #include <linux/zstd.h>
20602cbe91SDavid Sterba #include "misc.h"
215c1aab1dSNick Terrell #include "compression.h"
223f93aef5SDennis Zhou #include "ctree.h"
235c1aab1dSNick Terrell 
245c1aab1dSNick Terrell #define ZSTD_BTRFS_MAX_WINDOWLOG 17
255c1aab1dSNick Terrell #define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
265c1aab1dSNick Terrell #define ZSTD_BTRFS_DEFAULT_LEVEL 3
27d3c6ab75SDennis Zhou #define ZSTD_BTRFS_MAX_LEVEL 15
283f93aef5SDennis Zhou /* 307s to avoid pathologically clashing with transaction commit */
293f93aef5SDennis Zhou #define ZSTD_BTRFS_RECLAIM_JIFFIES (307 * HZ)
305c1aab1dSNick Terrell 
zstd_get_btrfs_parameters(unsigned int level,size_t src_len)31cf30f6a5SNick Terrell static zstd_parameters zstd_get_btrfs_parameters(unsigned int level,
32e0dc87afSDennis Zhou 						 size_t src_len)
335c1aab1dSNick Terrell {
34cf30f6a5SNick Terrell 	zstd_parameters params = zstd_get_params(level, src_len);
355c1aab1dSNick Terrell 
365c1aab1dSNick Terrell 	if (params.cParams.windowLog > ZSTD_BTRFS_MAX_WINDOWLOG)
375c1aab1dSNick Terrell 		params.cParams.windowLog = ZSTD_BTRFS_MAX_WINDOWLOG;
385c1aab1dSNick Terrell 	WARN_ON(src_len > ZSTD_BTRFS_MAX_INPUT);
395c1aab1dSNick Terrell 	return params;
405c1aab1dSNick Terrell }
415c1aab1dSNick Terrell 
425c1aab1dSNick Terrell struct workspace {
435c1aab1dSNick Terrell 	void *mem;
445c1aab1dSNick Terrell 	size_t size;
455c1aab1dSNick Terrell 	char *buf;
463f93aef5SDennis Zhou 	unsigned int level;
47e0dc87afSDennis Zhou 	unsigned int req_level;
483f93aef5SDennis Zhou 	unsigned long last_used; /* jiffies */
495c1aab1dSNick Terrell 	struct list_head list;
503f93aef5SDennis Zhou 	struct list_head lru_list;
51cf30f6a5SNick Terrell 	zstd_in_buffer in_buf;
52cf30f6a5SNick Terrell 	zstd_out_buffer out_buf;
535c1aab1dSNick Terrell };
545c1aab1dSNick Terrell 
553f93aef5SDennis Zhou /*
563f93aef5SDennis Zhou  * Zstd Workspace Management
573f93aef5SDennis Zhou  *
583f93aef5SDennis Zhou  * Zstd workspaces have different memory requirements depending on the level.
593f93aef5SDennis Zhou  * The zstd workspaces are managed by having individual lists for each level
603f93aef5SDennis Zhou  * and a global lru.  Forward progress is maintained by protecting a max level
613f93aef5SDennis Zhou  * workspace.
623f93aef5SDennis Zhou  *
633f93aef5SDennis Zhou  * Getting a workspace is done by using the bitmap to identify the levels that
643f93aef5SDennis Zhou  * have available workspaces and scans up.  This lets us recycle higher level
653f93aef5SDennis Zhou  * workspaces because of the monotonic memory guarantee.  A workspace's
663f93aef5SDennis Zhou  * last_used is only updated if it is being used by the corresponding memory
673f93aef5SDennis Zhou  * level.  Putting a workspace involves adding it back to the appropriate places
683f93aef5SDennis Zhou  * and adding it back to the lru if necessary.
693f93aef5SDennis Zhou  *
703f93aef5SDennis Zhou  * A timer is used to reclaim workspaces if they have not been used for
713f93aef5SDennis Zhou  * ZSTD_BTRFS_RECLAIM_JIFFIES.  This helps keep only active workspaces around.
723f93aef5SDennis Zhou  * The upper bound is provided by the workqueue limit which is 2 (percpu limit).
733f93aef5SDennis Zhou  */
743f93aef5SDennis Zhou 
753f93aef5SDennis Zhou struct zstd_workspace_manager {
763f93aef5SDennis Zhou 	const struct btrfs_compress_op *ops;
773f93aef5SDennis Zhou 	spinlock_t lock;
783f93aef5SDennis Zhou 	struct list_head lru_list;
793f93aef5SDennis Zhou 	struct list_head idle_ws[ZSTD_BTRFS_MAX_LEVEL];
803f93aef5SDennis Zhou 	unsigned long active_map;
813f93aef5SDennis Zhou 	wait_queue_head_t wait;
823f93aef5SDennis Zhou 	struct timer_list timer;
833f93aef5SDennis Zhou };
843f93aef5SDennis Zhou 
853f93aef5SDennis Zhou static struct zstd_workspace_manager wsm;
8692ee5530SDennis Zhou 
87d3c6ab75SDennis Zhou static size_t zstd_ws_mem_sizes[ZSTD_BTRFS_MAX_LEVEL];
88d3c6ab75SDennis Zhou 
list_to_workspace(struct list_head * list)893f93aef5SDennis Zhou static inline struct workspace *list_to_workspace(struct list_head *list)
903f93aef5SDennis Zhou {
913f93aef5SDennis Zhou 	return container_of(list, struct workspace, list);
923f93aef5SDennis Zhou }
933f93aef5SDennis Zhou 
94d20f395fSDavid Sterba void zstd_free_workspace(struct list_head *ws);
95d20f395fSDavid Sterba struct list_head *zstd_alloc_workspace(unsigned int level);
96dd7382a2SSchspa Shi 
9743dd529aSDavid Sterba /*
98dd7382a2SSchspa Shi  * Timer callback to free unused workspaces.
99dd7382a2SSchspa Shi  *
1003f93aef5SDennis Zhou  * @t: timer
1013f93aef5SDennis Zhou  *
1023f93aef5SDennis Zhou  * This scans the lru_list and attempts to reclaim any workspace that hasn't
1033f93aef5SDennis Zhou  * been used for ZSTD_BTRFS_RECLAIM_JIFFIES.
104dd7382a2SSchspa Shi  *
105dd7382a2SSchspa Shi  * The context is softirq and does not need the _bh locking primitives.
1063f93aef5SDennis Zhou  */
zstd_reclaim_timer_fn(struct timer_list * timer)1073f93aef5SDennis Zhou static void zstd_reclaim_timer_fn(struct timer_list *timer)
1083f93aef5SDennis Zhou {
1093f93aef5SDennis Zhou 	unsigned long reclaim_threshold = jiffies - ZSTD_BTRFS_RECLAIM_JIFFIES;
1103f93aef5SDennis Zhou 	struct list_head *pos, *next;
1113f93aef5SDennis Zhou 
112dd7382a2SSchspa Shi 	spin_lock(&wsm.lock);
1133f93aef5SDennis Zhou 
1143f93aef5SDennis Zhou 	if (list_empty(&wsm.lru_list)) {
115dd7382a2SSchspa Shi 		spin_unlock(&wsm.lock);
1163f93aef5SDennis Zhou 		return;
1173f93aef5SDennis Zhou 	}
1183f93aef5SDennis Zhou 
1193f93aef5SDennis Zhou 	list_for_each_prev_safe(pos, next, &wsm.lru_list) {
1203f93aef5SDennis Zhou 		struct workspace *victim = container_of(pos, struct workspace,
1213f93aef5SDennis Zhou 							lru_list);
1223f93aef5SDennis Zhou 		unsigned int level;
1233f93aef5SDennis Zhou 
1243f93aef5SDennis Zhou 		if (time_after(victim->last_used, reclaim_threshold))
1253f93aef5SDennis Zhou 			break;
1263f93aef5SDennis Zhou 
1273f93aef5SDennis Zhou 		/* workspace is in use */
1283f93aef5SDennis Zhou 		if (victim->req_level)
1293f93aef5SDennis Zhou 			continue;
1303f93aef5SDennis Zhou 
1313f93aef5SDennis Zhou 		level = victim->level;
1323f93aef5SDennis Zhou 		list_del(&victim->lru_list);
1333f93aef5SDennis Zhou 		list_del(&victim->list);
134b2423496SDennis Zhou 		zstd_free_workspace(&victim->list);
1353f93aef5SDennis Zhou 
1363f93aef5SDennis Zhou 		if (list_empty(&wsm.idle_ws[level - 1]))
1373f93aef5SDennis Zhou 			clear_bit(level - 1, &wsm.active_map);
1383f93aef5SDennis Zhou 
1393f93aef5SDennis Zhou 	}
1403f93aef5SDennis Zhou 
1413f93aef5SDennis Zhou 	if (!list_empty(&wsm.lru_list))
1423f93aef5SDennis Zhou 		mod_timer(&wsm.timer, jiffies + ZSTD_BTRFS_RECLAIM_JIFFIES);
1433f93aef5SDennis Zhou 
144dd7382a2SSchspa Shi 	spin_unlock(&wsm.lock);
1453f93aef5SDennis Zhou }
1463f93aef5SDennis Zhou 
147d3c6ab75SDennis Zhou /*
148d3c6ab75SDennis Zhou  * zstd_calc_ws_mem_sizes - calculate monotonic memory bounds
149d3c6ab75SDennis Zhou  *
150d3c6ab75SDennis Zhou  * It is possible based on the level configurations that a higher level
151d3c6ab75SDennis Zhou  * workspace uses less memory than a lower level workspace.  In order to reuse
152d3c6ab75SDennis Zhou  * workspaces, this must be made a monotonic relationship.  This precomputes
153d3c6ab75SDennis Zhou  * the required memory for each level and enforces the monotonicity between
154d3c6ab75SDennis Zhou  * level and memory required.
155d3c6ab75SDennis Zhou  */
zstd_calc_ws_mem_sizes(void)156d3c6ab75SDennis Zhou static void zstd_calc_ws_mem_sizes(void)
157d3c6ab75SDennis Zhou {
158d3c6ab75SDennis Zhou 	size_t max_size = 0;
159d3c6ab75SDennis Zhou 	unsigned int level;
160d3c6ab75SDennis Zhou 
161d3c6ab75SDennis Zhou 	for (level = 1; level <= ZSTD_BTRFS_MAX_LEVEL; level++) {
162cf30f6a5SNick Terrell 		zstd_parameters params =
163d3c6ab75SDennis Zhou 			zstd_get_btrfs_parameters(level, ZSTD_BTRFS_MAX_INPUT);
164d3c6ab75SDennis Zhou 		size_t level_size =
165d3c6ab75SDennis Zhou 			max_t(size_t,
166cf30f6a5SNick Terrell 			      zstd_cstream_workspace_bound(&params.cParams),
167cf30f6a5SNick Terrell 			      zstd_dstream_workspace_bound(ZSTD_BTRFS_MAX_INPUT));
168d3c6ab75SDennis Zhou 
169d3c6ab75SDennis Zhou 		max_size = max_t(size_t, max_size, level_size);
170d3c6ab75SDennis Zhou 		zstd_ws_mem_sizes[level - 1] = max_size;
171d3c6ab75SDennis Zhou 	}
172d3c6ab75SDennis Zhou }
173d3c6ab75SDennis Zhou 
zstd_init_workspace_manager(void)174d5517033SDavid Sterba void zstd_init_workspace_manager(void)
17592ee5530SDennis Zhou {
1763f93aef5SDennis Zhou 	struct list_head *ws;
1773f93aef5SDennis Zhou 	int i;
1783f93aef5SDennis Zhou 
179d3c6ab75SDennis Zhou 	zstd_calc_ws_mem_sizes();
180d3c6ab75SDennis Zhou 
1813f93aef5SDennis Zhou 	wsm.ops = &btrfs_zstd_compress;
1823f93aef5SDennis Zhou 	spin_lock_init(&wsm.lock);
1833f93aef5SDennis Zhou 	init_waitqueue_head(&wsm.wait);
1843f93aef5SDennis Zhou 	timer_setup(&wsm.timer, zstd_reclaim_timer_fn, 0);
1853f93aef5SDennis Zhou 
1863f93aef5SDennis Zhou 	INIT_LIST_HEAD(&wsm.lru_list);
1873f93aef5SDennis Zhou 	for (i = 0; i < ZSTD_BTRFS_MAX_LEVEL; i++)
1883f93aef5SDennis Zhou 		INIT_LIST_HEAD(&wsm.idle_ws[i]);
1893f93aef5SDennis Zhou 
190b2423496SDennis Zhou 	ws = zstd_alloc_workspace(ZSTD_BTRFS_MAX_LEVEL);
1913f93aef5SDennis Zhou 	if (IS_ERR(ws)) {
1923f93aef5SDennis Zhou 		pr_warn(
1933f93aef5SDennis Zhou 		"BTRFS: cannot preallocate zstd compression workspace\n");
1943f93aef5SDennis Zhou 	} else {
1953f93aef5SDennis Zhou 		set_bit(ZSTD_BTRFS_MAX_LEVEL - 1, &wsm.active_map);
1963f93aef5SDennis Zhou 		list_add(ws, &wsm.idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1]);
1973f93aef5SDennis Zhou 	}
19892ee5530SDennis Zhou }
19992ee5530SDennis Zhou 
zstd_cleanup_workspace_manager(void)2002510307eSDavid Sterba void zstd_cleanup_workspace_manager(void)
20192ee5530SDennis Zhou {
2023f93aef5SDennis Zhou 	struct workspace *workspace;
2033f93aef5SDennis Zhou 	int i;
2043f93aef5SDennis Zhou 
205fee13fe9SDennis Zhou 	spin_lock_bh(&wsm.lock);
2063f93aef5SDennis Zhou 	for (i = 0; i < ZSTD_BTRFS_MAX_LEVEL; i++) {
2073f93aef5SDennis Zhou 		while (!list_empty(&wsm.idle_ws[i])) {
2083f93aef5SDennis Zhou 			workspace = container_of(wsm.idle_ws[i].next,
2093f93aef5SDennis Zhou 						 struct workspace, list);
2103f93aef5SDennis Zhou 			list_del(&workspace->list);
2113f93aef5SDennis Zhou 			list_del(&workspace->lru_list);
212b2423496SDennis Zhou 			zstd_free_workspace(&workspace->list);
2133f93aef5SDennis Zhou 		}
2143f93aef5SDennis Zhou 	}
215fee13fe9SDennis Zhou 	spin_unlock_bh(&wsm.lock);
216d3865159SDennis Zhou 
217d3865159SDennis Zhou 	del_timer_sync(&wsm.timer);
21892ee5530SDennis Zhou }
21992ee5530SDennis Zhou 
2203f93aef5SDennis Zhou /*
2213f93aef5SDennis Zhou  * zstd_find_workspace - find workspace
2223f93aef5SDennis Zhou  * @level: compression level
2233f93aef5SDennis Zhou  *
2243f93aef5SDennis Zhou  * This iterates over the set bits in the active_map beginning at the requested
2253f93aef5SDennis Zhou  * compression level.  This lets us utilize already allocated workspaces before
2263f93aef5SDennis Zhou  * allocating a new one.  If the workspace is of a larger size, it is used, but
2273f93aef5SDennis Zhou  * the place in the lru_list and last_used times are not updated.  This is to
2283f93aef5SDennis Zhou  * offer the opportunity to reclaim the workspace in favor of allocating an
2293f93aef5SDennis Zhou  * appropriately sized one in the future.
2303f93aef5SDennis Zhou  */
zstd_find_workspace(unsigned int level)2313f93aef5SDennis Zhou static struct list_head *zstd_find_workspace(unsigned int level)
2323f93aef5SDennis Zhou {
2333f93aef5SDennis Zhou 	struct list_head *ws;
2343f93aef5SDennis Zhou 	struct workspace *workspace;
2353f93aef5SDennis Zhou 	int i = level - 1;
2363f93aef5SDennis Zhou 
237fee13fe9SDennis Zhou 	spin_lock_bh(&wsm.lock);
2383f93aef5SDennis Zhou 	for_each_set_bit_from(i, &wsm.active_map, ZSTD_BTRFS_MAX_LEVEL) {
2393f93aef5SDennis Zhou 		if (!list_empty(&wsm.idle_ws[i])) {
2403f93aef5SDennis Zhou 			ws = wsm.idle_ws[i].next;
2413f93aef5SDennis Zhou 			workspace = list_to_workspace(ws);
2423f93aef5SDennis Zhou 			list_del_init(ws);
2433f93aef5SDennis Zhou 			/* keep its place if it's a lower level using this */
2443f93aef5SDennis Zhou 			workspace->req_level = level;
2453f93aef5SDennis Zhou 			if (level == workspace->level)
2463f93aef5SDennis Zhou 				list_del(&workspace->lru_list);
2473f93aef5SDennis Zhou 			if (list_empty(&wsm.idle_ws[i]))
2483f93aef5SDennis Zhou 				clear_bit(i, &wsm.active_map);
249fee13fe9SDennis Zhou 			spin_unlock_bh(&wsm.lock);
2503f93aef5SDennis Zhou 			return ws;
2513f93aef5SDennis Zhou 		}
2523f93aef5SDennis Zhou 	}
253fee13fe9SDennis Zhou 	spin_unlock_bh(&wsm.lock);
2543f93aef5SDennis Zhou 
2553f93aef5SDennis Zhou 	return NULL;
2563f93aef5SDennis Zhou }
2573f93aef5SDennis Zhou 
2583f93aef5SDennis Zhou /*
2593f93aef5SDennis Zhou  * zstd_get_workspace - zstd's get_workspace
2603f93aef5SDennis Zhou  * @level: compression level
2613f93aef5SDennis Zhou  *
2623f93aef5SDennis Zhou  * If @level is 0, then any compression level can be used.  Therefore, we begin
2633f93aef5SDennis Zhou  * scanning from 1.  We first scan through possible workspaces and then after
2643f93aef5SDennis Zhou  * attempt to allocate a new workspace.  If we fail to allocate one due to
2653f93aef5SDennis Zhou  * memory pressure, go to sleep waiting for the max level workspace to free up.
2663f93aef5SDennis Zhou  */
zstd_get_workspace(unsigned int level)267d20f395fSDavid Sterba struct list_head *zstd_get_workspace(unsigned int level)
26892ee5530SDennis Zhou {
2693f93aef5SDennis Zhou 	struct list_head *ws;
2703f93aef5SDennis Zhou 	unsigned int nofs_flag;
271e0dc87afSDennis Zhou 
2723f93aef5SDennis Zhou 	/* level == 0 means we can use any workspace */
2733f93aef5SDennis Zhou 	if (!level)
2743f93aef5SDennis Zhou 		level = 1;
2753f93aef5SDennis Zhou 
2763f93aef5SDennis Zhou again:
2773f93aef5SDennis Zhou 	ws = zstd_find_workspace(level);
2783f93aef5SDennis Zhou 	if (ws)
2793f93aef5SDennis Zhou 		return ws;
2803f93aef5SDennis Zhou 
2813f93aef5SDennis Zhou 	nofs_flag = memalloc_nofs_save();
282b2423496SDennis Zhou 	ws = zstd_alloc_workspace(level);
2833f93aef5SDennis Zhou 	memalloc_nofs_restore(nofs_flag);
2843f93aef5SDennis Zhou 
2853f93aef5SDennis Zhou 	if (IS_ERR(ws)) {
2863f93aef5SDennis Zhou 		DEFINE_WAIT(wait);
2873f93aef5SDennis Zhou 
2883f93aef5SDennis Zhou 		prepare_to_wait(&wsm.wait, &wait, TASK_UNINTERRUPTIBLE);
2893f93aef5SDennis Zhou 		schedule();
2903f93aef5SDennis Zhou 		finish_wait(&wsm.wait, &wait);
2913f93aef5SDennis Zhou 
2923f93aef5SDennis Zhou 		goto again;
2933f93aef5SDennis Zhou 	}
294e0dc87afSDennis Zhou 
295e0dc87afSDennis Zhou 	return ws;
29692ee5530SDennis Zhou }
29792ee5530SDennis Zhou 
2983f93aef5SDennis Zhou /*
2993f93aef5SDennis Zhou  * zstd_put_workspace - zstd put_workspace
3003f93aef5SDennis Zhou  * @ws: list_head for the workspace
3013f93aef5SDennis Zhou  *
3023f93aef5SDennis Zhou  * When putting back a workspace, we only need to update the LRU if we are of
3033f93aef5SDennis Zhou  * the requested compression level.  Here is where we continue to protect the
3043f93aef5SDennis Zhou  * max level workspace or update last_used accordingly.  If the reclaim timer
3053f93aef5SDennis Zhou  * isn't set, it is also set here.  Only the max level workspace tries and wakes
3063f93aef5SDennis Zhou  * up waiting workspaces.
3073f93aef5SDennis Zhou  */
zstd_put_workspace(struct list_head * ws)308d20f395fSDavid Sterba void zstd_put_workspace(struct list_head *ws)
30992ee5530SDennis Zhou {
3103f93aef5SDennis Zhou 	struct workspace *workspace = list_to_workspace(ws);
3113f93aef5SDennis Zhou 
312fee13fe9SDennis Zhou 	spin_lock_bh(&wsm.lock);
3133f93aef5SDennis Zhou 
3143f93aef5SDennis Zhou 	/* A node is only taken off the lru if we are the corresponding level */
3153f93aef5SDennis Zhou 	if (workspace->req_level == workspace->level) {
3163f93aef5SDennis Zhou 		/* Hide a max level workspace from reclaim */
3173f93aef5SDennis Zhou 		if (list_empty(&wsm.idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1])) {
3183f93aef5SDennis Zhou 			INIT_LIST_HEAD(&workspace->lru_list);
3193f93aef5SDennis Zhou 		} else {
3203f93aef5SDennis Zhou 			workspace->last_used = jiffies;
3213f93aef5SDennis Zhou 			list_add(&workspace->lru_list, &wsm.lru_list);
3223f93aef5SDennis Zhou 			if (!timer_pending(&wsm.timer))
3233f93aef5SDennis Zhou 				mod_timer(&wsm.timer,
3243f93aef5SDennis Zhou 					  jiffies + ZSTD_BTRFS_RECLAIM_JIFFIES);
3253f93aef5SDennis Zhou 		}
3263f93aef5SDennis Zhou 	}
3273f93aef5SDennis Zhou 
3283f93aef5SDennis Zhou 	set_bit(workspace->level - 1, &wsm.active_map);
3293f93aef5SDennis Zhou 	list_add(&workspace->list, &wsm.idle_ws[workspace->level - 1]);
3303f93aef5SDennis Zhou 	workspace->req_level = 0;
3313f93aef5SDennis Zhou 
332fee13fe9SDennis Zhou 	spin_unlock_bh(&wsm.lock);
3333f93aef5SDennis Zhou 
3343f93aef5SDennis Zhou 	if (workspace->level == ZSTD_BTRFS_MAX_LEVEL)
3353f93aef5SDennis Zhou 		cond_wake_up(&wsm.wait);
33692ee5530SDennis Zhou }
33792ee5530SDennis Zhou 
zstd_free_workspace(struct list_head * ws)338d20f395fSDavid Sterba void zstd_free_workspace(struct list_head *ws)
3395c1aab1dSNick Terrell {
3405c1aab1dSNick Terrell 	struct workspace *workspace = list_entry(ws, struct workspace, list);
3415c1aab1dSNick Terrell 
3425c1aab1dSNick Terrell 	kvfree(workspace->mem);
3435c1aab1dSNick Terrell 	kfree(workspace->buf);
3445c1aab1dSNick Terrell 	kfree(workspace);
3455c1aab1dSNick Terrell }
3465c1aab1dSNick Terrell 
zstd_alloc_workspace(unsigned int level)347d20f395fSDavid Sterba struct list_head *zstd_alloc_workspace(unsigned int level)
3485c1aab1dSNick Terrell {
3495c1aab1dSNick Terrell 	struct workspace *workspace;
3505c1aab1dSNick Terrell 
3515c1aab1dSNick Terrell 	workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
3525c1aab1dSNick Terrell 	if (!workspace)
3535c1aab1dSNick Terrell 		return ERR_PTR(-ENOMEM);
3545c1aab1dSNick Terrell 
355d3c6ab75SDennis Zhou 	workspace->size = zstd_ws_mem_sizes[level - 1];
3563f93aef5SDennis Zhou 	workspace->level = level;
3573f93aef5SDennis Zhou 	workspace->req_level = level;
3583f93aef5SDennis Zhou 	workspace->last_used = jiffies;
3595c1aab1dSNick Terrell 	workspace->mem = kvmalloc(workspace->size, GFP_KERNEL | __GFP_NOWARN);
3605c1aab1dSNick Terrell 	workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3615c1aab1dSNick Terrell 	if (!workspace->mem || !workspace->buf)
3625c1aab1dSNick Terrell 		goto fail;
3635c1aab1dSNick Terrell 
3645c1aab1dSNick Terrell 	INIT_LIST_HEAD(&workspace->list);
3653f93aef5SDennis Zhou 	INIT_LIST_HEAD(&workspace->lru_list);
3665c1aab1dSNick Terrell 
3675c1aab1dSNick Terrell 	return &workspace->list;
3685c1aab1dSNick Terrell fail:
3695c1aab1dSNick Terrell 	zstd_free_workspace(&workspace->list);
3705c1aab1dSNick Terrell 	return ERR_PTR(-ENOMEM);
3715c1aab1dSNick Terrell }
3725c1aab1dSNick Terrell 
zstd_compress_pages(struct list_head * ws,struct address_space * mapping,u64 start,struct page ** pages,unsigned long * out_pages,unsigned long * total_in,unsigned long * total_out)373c4bf665aSDavid Sterba int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
374c4bf665aSDavid Sterba 		u64 start, struct page **pages, unsigned long *out_pages,
375c4bf665aSDavid Sterba 		unsigned long *total_in, unsigned long *total_out)
3765c1aab1dSNick Terrell {
3775c1aab1dSNick Terrell 	struct workspace *workspace = list_entry(ws, struct workspace, list);
378cf30f6a5SNick Terrell 	zstd_cstream *stream;
3795c1aab1dSNick Terrell 	int ret = 0;
3805c1aab1dSNick Terrell 	int nr_pages = 0;
3815c1aab1dSNick Terrell 	struct page *in_page = NULL;  /* The current page to read */
3825c1aab1dSNick Terrell 	struct page *out_page = NULL; /* The current page to write to */
3835c1aab1dSNick Terrell 	unsigned long tot_in = 0;
3845c1aab1dSNick Terrell 	unsigned long tot_out = 0;
3855c1aab1dSNick Terrell 	unsigned long len = *total_out;
3865c1aab1dSNick Terrell 	const unsigned long nr_dest_pages = *out_pages;
3875c1aab1dSNick Terrell 	unsigned long max_out = nr_dest_pages * PAGE_SIZE;
388cf30f6a5SNick Terrell 	zstd_parameters params = zstd_get_btrfs_parameters(workspace->req_level,
389e0dc87afSDennis Zhou 							   len);
3905c1aab1dSNick Terrell 
3915c1aab1dSNick Terrell 	*out_pages = 0;
3925c1aab1dSNick Terrell 	*total_out = 0;
3935c1aab1dSNick Terrell 	*total_in = 0;
3945c1aab1dSNick Terrell 
3955c1aab1dSNick Terrell 	/* Initialize the stream */
396cf30f6a5SNick Terrell 	stream = zstd_init_cstream(&params, len, workspace->mem,
3975c1aab1dSNick Terrell 			workspace->size);
3985c1aab1dSNick Terrell 	if (!stream) {
399cf30f6a5SNick Terrell 		pr_warn("BTRFS: zstd_init_cstream failed\n");
4005c1aab1dSNick Terrell 		ret = -EIO;
4015c1aab1dSNick Terrell 		goto out;
4025c1aab1dSNick Terrell 	}
4035c1aab1dSNick Terrell 
4045c1aab1dSNick Terrell 	/* map in the first page of input data */
4055c1aab1dSNick Terrell 	in_page = find_get_page(mapping, start >> PAGE_SHIFT);
406ebd23482SFabio M. De Francesco 	workspace->in_buf.src = kmap_local_page(in_page);
407431e9822SDavid Sterba 	workspace->in_buf.pos = 0;
408431e9822SDavid Sterba 	workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
4095c1aab1dSNick Terrell 
4105c1aab1dSNick Terrell 
4115c1aab1dSNick Terrell 	/* Allocate and map in the output buffer */
412b0ee5e1eSDavid Sterba 	out_page = alloc_page(GFP_NOFS);
4135c1aab1dSNick Terrell 	if (out_page == NULL) {
4145c1aab1dSNick Terrell 		ret = -ENOMEM;
4155c1aab1dSNick Terrell 		goto out;
4165c1aab1dSNick Terrell 	}
4175c1aab1dSNick Terrell 	pages[nr_pages++] = out_page;
418ebd23482SFabio M. De Francesco 	workspace->out_buf.dst = page_address(out_page);
419431e9822SDavid Sterba 	workspace->out_buf.pos = 0;
420431e9822SDavid Sterba 	workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
4215c1aab1dSNick Terrell 
4225c1aab1dSNick Terrell 	while (1) {
4235c1aab1dSNick Terrell 		size_t ret2;
4245c1aab1dSNick Terrell 
425cf30f6a5SNick Terrell 		ret2 = zstd_compress_stream(stream, &workspace->out_buf,
426431e9822SDavid Sterba 				&workspace->in_buf);
427cf30f6a5SNick Terrell 		if (zstd_is_error(ret2)) {
428cf30f6a5SNick Terrell 			pr_debug("BTRFS: zstd_compress_stream returned %d\n",
429cf30f6a5SNick Terrell 					zstd_get_error_code(ret2));
4305c1aab1dSNick Terrell 			ret = -EIO;
4315c1aab1dSNick Terrell 			goto out;
4325c1aab1dSNick Terrell 		}
4335c1aab1dSNick Terrell 
4345c1aab1dSNick Terrell 		/* Check to see if we are making it bigger */
435431e9822SDavid Sterba 		if (tot_in + workspace->in_buf.pos > 8192 &&
436431e9822SDavid Sterba 				tot_in + workspace->in_buf.pos <
437431e9822SDavid Sterba 				tot_out + workspace->out_buf.pos) {
4385c1aab1dSNick Terrell 			ret = -E2BIG;
4395c1aab1dSNick Terrell 			goto out;
4405c1aab1dSNick Terrell 		}
4415c1aab1dSNick Terrell 
4425c1aab1dSNick Terrell 		/* We've reached the end of our output range */
443431e9822SDavid Sterba 		if (workspace->out_buf.pos >= max_out) {
444431e9822SDavid Sterba 			tot_out += workspace->out_buf.pos;
4455c1aab1dSNick Terrell 			ret = -E2BIG;
4465c1aab1dSNick Terrell 			goto out;
4475c1aab1dSNick Terrell 		}
4485c1aab1dSNick Terrell 
4495c1aab1dSNick Terrell 		/* Check if we need more output space */
450431e9822SDavid Sterba 		if (workspace->out_buf.pos == workspace->out_buf.size) {
4515c1aab1dSNick Terrell 			tot_out += PAGE_SIZE;
4525c1aab1dSNick Terrell 			max_out -= PAGE_SIZE;
4535c1aab1dSNick Terrell 			if (nr_pages == nr_dest_pages) {
4545c1aab1dSNick Terrell 				ret = -E2BIG;
4555c1aab1dSNick Terrell 				goto out;
4565c1aab1dSNick Terrell 			}
457b0ee5e1eSDavid Sterba 			out_page = alloc_page(GFP_NOFS);
4585c1aab1dSNick Terrell 			if (out_page == NULL) {
4595c1aab1dSNick Terrell 				ret = -ENOMEM;
4605c1aab1dSNick Terrell 				goto out;
4615c1aab1dSNick Terrell 			}
4625c1aab1dSNick Terrell 			pages[nr_pages++] = out_page;
463ebd23482SFabio M. De Francesco 			workspace->out_buf.dst = page_address(out_page);
464431e9822SDavid Sterba 			workspace->out_buf.pos = 0;
465431e9822SDavid Sterba 			workspace->out_buf.size = min_t(size_t, max_out,
466431e9822SDavid Sterba 							PAGE_SIZE);
4675c1aab1dSNick Terrell 		}
4685c1aab1dSNick Terrell 
4695c1aab1dSNick Terrell 		/* We've reached the end of the input */
470431e9822SDavid Sterba 		if (workspace->in_buf.pos >= len) {
471431e9822SDavid Sterba 			tot_in += workspace->in_buf.pos;
4725c1aab1dSNick Terrell 			break;
4735c1aab1dSNick Terrell 		}
4745c1aab1dSNick Terrell 
4755c1aab1dSNick Terrell 		/* Check if we need more input */
476431e9822SDavid Sterba 		if (workspace->in_buf.pos == workspace->in_buf.size) {
4775c1aab1dSNick Terrell 			tot_in += PAGE_SIZE;
478ebd23482SFabio M. De Francesco 			kunmap_local(workspace->in_buf.src);
4795c1aab1dSNick Terrell 			put_page(in_page);
4805c1aab1dSNick Terrell 			start += PAGE_SIZE;
4815c1aab1dSNick Terrell 			len -= PAGE_SIZE;
4825c1aab1dSNick Terrell 			in_page = find_get_page(mapping, start >> PAGE_SHIFT);
483ebd23482SFabio M. De Francesco 			workspace->in_buf.src = kmap_local_page(in_page);
484431e9822SDavid Sterba 			workspace->in_buf.pos = 0;
485431e9822SDavid Sterba 			workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
4865c1aab1dSNick Terrell 		}
4875c1aab1dSNick Terrell 	}
4885c1aab1dSNick Terrell 	while (1) {
4895c1aab1dSNick Terrell 		size_t ret2;
4905c1aab1dSNick Terrell 
491cf30f6a5SNick Terrell 		ret2 = zstd_end_stream(stream, &workspace->out_buf);
492cf30f6a5SNick Terrell 		if (zstd_is_error(ret2)) {
493cf30f6a5SNick Terrell 			pr_debug("BTRFS: zstd_end_stream returned %d\n",
494cf30f6a5SNick Terrell 					zstd_get_error_code(ret2));
4955c1aab1dSNick Terrell 			ret = -EIO;
4965c1aab1dSNick Terrell 			goto out;
4975c1aab1dSNick Terrell 		}
4985c1aab1dSNick Terrell 		if (ret2 == 0) {
499431e9822SDavid Sterba 			tot_out += workspace->out_buf.pos;
5005c1aab1dSNick Terrell 			break;
5015c1aab1dSNick Terrell 		}
502431e9822SDavid Sterba 		if (workspace->out_buf.pos >= max_out) {
503431e9822SDavid Sterba 			tot_out += workspace->out_buf.pos;
5045c1aab1dSNick Terrell 			ret = -E2BIG;
5055c1aab1dSNick Terrell 			goto out;
5065c1aab1dSNick Terrell 		}
5075c1aab1dSNick Terrell 
5085c1aab1dSNick Terrell 		tot_out += PAGE_SIZE;
5095c1aab1dSNick Terrell 		max_out -= PAGE_SIZE;
5105c1aab1dSNick Terrell 		if (nr_pages == nr_dest_pages) {
5115c1aab1dSNick Terrell 			ret = -E2BIG;
5125c1aab1dSNick Terrell 			goto out;
5135c1aab1dSNick Terrell 		}
514b0ee5e1eSDavid Sterba 		out_page = alloc_page(GFP_NOFS);
5155c1aab1dSNick Terrell 		if (out_page == NULL) {
5165c1aab1dSNick Terrell 			ret = -ENOMEM;
5175c1aab1dSNick Terrell 			goto out;
5185c1aab1dSNick Terrell 		}
5195c1aab1dSNick Terrell 		pages[nr_pages++] = out_page;
520ebd23482SFabio M. De Francesco 		workspace->out_buf.dst = page_address(out_page);
521431e9822SDavid Sterba 		workspace->out_buf.pos = 0;
522431e9822SDavid Sterba 		workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
5235c1aab1dSNick Terrell 	}
5245c1aab1dSNick Terrell 
5255c1aab1dSNick Terrell 	if (tot_out >= tot_in) {
5265c1aab1dSNick Terrell 		ret = -E2BIG;
5275c1aab1dSNick Terrell 		goto out;
5285c1aab1dSNick Terrell 	}
5295c1aab1dSNick Terrell 
5305c1aab1dSNick Terrell 	ret = 0;
5315c1aab1dSNick Terrell 	*total_in = tot_in;
5325c1aab1dSNick Terrell 	*total_out = tot_out;
5335c1aab1dSNick Terrell out:
5345c1aab1dSNick Terrell 	*out_pages = nr_pages;
535ebd23482SFabio M. De Francesco 	if (workspace->in_buf.src) {
536ebd23482SFabio M. De Francesco 		kunmap_local(workspace->in_buf.src);
5375c1aab1dSNick Terrell 		put_page(in_page);
53856ee254dSDavid Sterba 	}
5395c1aab1dSNick Terrell 	return ret;
5405c1aab1dSNick Terrell }
5415c1aab1dSNick Terrell 
zstd_decompress_bio(struct list_head * ws,struct compressed_bio * cb)542c4bf665aSDavid Sterba int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
5435c1aab1dSNick Terrell {
5445c1aab1dSNick Terrell 	struct workspace *workspace = list_entry(ws, struct workspace, list);
5455c1aab1dSNick Terrell 	struct page **pages_in = cb->compressed_pages;
5465c1aab1dSNick Terrell 	size_t srclen = cb->compressed_len;
547cf30f6a5SNick Terrell 	zstd_dstream *stream;
5485c1aab1dSNick Terrell 	int ret = 0;
5495c1aab1dSNick Terrell 	unsigned long page_in_index = 0;
5505c1aab1dSNick Terrell 	unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
5515c1aab1dSNick Terrell 	unsigned long buf_start;
5525c1aab1dSNick Terrell 	unsigned long total_out = 0;
5535c1aab1dSNick Terrell 
554cf30f6a5SNick Terrell 	stream = zstd_init_dstream(
5555c1aab1dSNick Terrell 			ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
5565c1aab1dSNick Terrell 	if (!stream) {
557cf30f6a5SNick Terrell 		pr_debug("BTRFS: zstd_init_dstream failed\n");
5585c1aab1dSNick Terrell 		ret = -EIO;
5595c1aab1dSNick Terrell 		goto done;
5605c1aab1dSNick Terrell 	}
5615c1aab1dSNick Terrell 
562ebd23482SFabio M. De Francesco 	workspace->in_buf.src = kmap_local_page(pages_in[page_in_index]);
563431e9822SDavid Sterba 	workspace->in_buf.pos = 0;
564431e9822SDavid Sterba 	workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
5655c1aab1dSNick Terrell 
566431e9822SDavid Sterba 	workspace->out_buf.dst = workspace->buf;
567431e9822SDavid Sterba 	workspace->out_buf.pos = 0;
568431e9822SDavid Sterba 	workspace->out_buf.size = PAGE_SIZE;
5695c1aab1dSNick Terrell 
5705c1aab1dSNick Terrell 	while (1) {
5715c1aab1dSNick Terrell 		size_t ret2;
5725c1aab1dSNick Terrell 
573cf30f6a5SNick Terrell 		ret2 = zstd_decompress_stream(stream, &workspace->out_buf,
574431e9822SDavid Sterba 				&workspace->in_buf);
575cf30f6a5SNick Terrell 		if (zstd_is_error(ret2)) {
576cf30f6a5SNick Terrell 			pr_debug("BTRFS: zstd_decompress_stream returned %d\n",
577cf30f6a5SNick Terrell 					zstd_get_error_code(ret2));
5785c1aab1dSNick Terrell 			ret = -EIO;
5795c1aab1dSNick Terrell 			goto done;
5805c1aab1dSNick Terrell 		}
5815c1aab1dSNick Terrell 		buf_start = total_out;
582431e9822SDavid Sterba 		total_out += workspace->out_buf.pos;
583431e9822SDavid Sterba 		workspace->out_buf.pos = 0;
5845c1aab1dSNick Terrell 
585431e9822SDavid Sterba 		ret = btrfs_decompress_buf2page(workspace->out_buf.dst,
5861c3dc173SQu Wenruo 				total_out - buf_start, cb, buf_start);
5875c1aab1dSNick Terrell 		if (ret == 0)
5885c1aab1dSNick Terrell 			break;
5895c1aab1dSNick Terrell 
590431e9822SDavid Sterba 		if (workspace->in_buf.pos >= srclen)
5915c1aab1dSNick Terrell 			break;
5925c1aab1dSNick Terrell 
5935c1aab1dSNick Terrell 		/* Check if we've hit the end of a frame */
5945c1aab1dSNick Terrell 		if (ret2 == 0)
5955c1aab1dSNick Terrell 			break;
5965c1aab1dSNick Terrell 
597431e9822SDavid Sterba 		if (workspace->in_buf.pos == workspace->in_buf.size) {
598ebd23482SFabio M. De Francesco 			kunmap_local(workspace->in_buf.src);
599ebd23482SFabio M. De Francesco 			page_in_index++;
6005c1aab1dSNick Terrell 			if (page_in_index >= total_pages_in) {
601431e9822SDavid Sterba 				workspace->in_buf.src = NULL;
6025c1aab1dSNick Terrell 				ret = -EIO;
6035c1aab1dSNick Terrell 				goto done;
6045c1aab1dSNick Terrell 			}
6055c1aab1dSNick Terrell 			srclen -= PAGE_SIZE;
606ebd23482SFabio M. De Francesco 			workspace->in_buf.src = kmap_local_page(pages_in[page_in_index]);
607431e9822SDavid Sterba 			workspace->in_buf.pos = 0;
608431e9822SDavid Sterba 			workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
6095c1aab1dSNick Terrell 		}
6105c1aab1dSNick Terrell 	}
6115c1aab1dSNick Terrell 	ret = 0;
6125c1aab1dSNick Terrell done:
61356ee254dSDavid Sterba 	if (workspace->in_buf.src)
614ebd23482SFabio M. De Francesco 		kunmap_local(workspace->in_buf.src);
6155c1aab1dSNick Terrell 	return ret;
6165c1aab1dSNick Terrell }
6175c1aab1dSNick Terrell 
zstd_decompress(struct list_head * ws,const u8 * data_in,struct page * dest_page,unsigned long start_byte,size_t srclen,size_t destlen)618*3e09b5b2SDavid Sterba int zstd_decompress(struct list_head *ws, const u8 *data_in,
619c4bf665aSDavid Sterba 		struct page *dest_page, unsigned long start_byte, size_t srclen,
620c4bf665aSDavid Sterba 		size_t destlen)
6215c1aab1dSNick Terrell {
6225c1aab1dSNick Terrell 	struct workspace *workspace = list_entry(ws, struct workspace, list);
623cf30f6a5SNick Terrell 	zstd_dstream *stream;
6245c1aab1dSNick Terrell 	int ret = 0;
6255c1aab1dSNick Terrell 	size_t ret2;
6265c1aab1dSNick Terrell 	unsigned long total_out = 0;
6275c1aab1dSNick Terrell 	unsigned long pg_offset = 0;
6285c1aab1dSNick Terrell 
629cf30f6a5SNick Terrell 	stream = zstd_init_dstream(
6305c1aab1dSNick Terrell 			ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
6315c1aab1dSNick Terrell 	if (!stream) {
632cf30f6a5SNick Terrell 		pr_warn("BTRFS: zstd_init_dstream failed\n");
6335c1aab1dSNick Terrell 		ret = -EIO;
6345c1aab1dSNick Terrell 		goto finish;
6355c1aab1dSNick Terrell 	}
6365c1aab1dSNick Terrell 
6375c1aab1dSNick Terrell 	destlen = min_t(size_t, destlen, PAGE_SIZE);
6385c1aab1dSNick Terrell 
639431e9822SDavid Sterba 	workspace->in_buf.src = data_in;
640431e9822SDavid Sterba 	workspace->in_buf.pos = 0;
641431e9822SDavid Sterba 	workspace->in_buf.size = srclen;
6425c1aab1dSNick Terrell 
643431e9822SDavid Sterba 	workspace->out_buf.dst = workspace->buf;
644431e9822SDavid Sterba 	workspace->out_buf.pos = 0;
645431e9822SDavid Sterba 	workspace->out_buf.size = PAGE_SIZE;
6465c1aab1dSNick Terrell 
6475c1aab1dSNick Terrell 	ret2 = 1;
648431e9822SDavid Sterba 	while (pg_offset < destlen
649431e9822SDavid Sterba 	       && workspace->in_buf.pos < workspace->in_buf.size) {
6505c1aab1dSNick Terrell 		unsigned long buf_start;
6515c1aab1dSNick Terrell 		unsigned long buf_offset;
6525c1aab1dSNick Terrell 		unsigned long bytes;
6535c1aab1dSNick Terrell 
6545c1aab1dSNick Terrell 		/* Check if the frame is over and we still need more input */
6555c1aab1dSNick Terrell 		if (ret2 == 0) {
656cf30f6a5SNick Terrell 			pr_debug("BTRFS: zstd_decompress_stream ended early\n");
6575c1aab1dSNick Terrell 			ret = -EIO;
6585c1aab1dSNick Terrell 			goto finish;
6595c1aab1dSNick Terrell 		}
660cf30f6a5SNick Terrell 		ret2 = zstd_decompress_stream(stream, &workspace->out_buf,
661431e9822SDavid Sterba 				&workspace->in_buf);
662cf30f6a5SNick Terrell 		if (zstd_is_error(ret2)) {
663cf30f6a5SNick Terrell 			pr_debug("BTRFS: zstd_decompress_stream returned %d\n",
664cf30f6a5SNick Terrell 					zstd_get_error_code(ret2));
6655c1aab1dSNick Terrell 			ret = -EIO;
6665c1aab1dSNick Terrell 			goto finish;
6675c1aab1dSNick Terrell 		}
6685c1aab1dSNick Terrell 
6695c1aab1dSNick Terrell 		buf_start = total_out;
670431e9822SDavid Sterba 		total_out += workspace->out_buf.pos;
671431e9822SDavid Sterba 		workspace->out_buf.pos = 0;
6725c1aab1dSNick Terrell 
6735c1aab1dSNick Terrell 		if (total_out <= start_byte)
6745c1aab1dSNick Terrell 			continue;
6755c1aab1dSNick Terrell 
6765c1aab1dSNick Terrell 		if (total_out > start_byte && buf_start < start_byte)
6775c1aab1dSNick Terrell 			buf_offset = start_byte - buf_start;
6785c1aab1dSNick Terrell 		else
6795c1aab1dSNick Terrell 			buf_offset = 0;
6805c1aab1dSNick Terrell 
6815c1aab1dSNick Terrell 		bytes = min_t(unsigned long, destlen - pg_offset,
682431e9822SDavid Sterba 				workspace->out_buf.size - buf_offset);
6835c1aab1dSNick Terrell 
6843590ec58SIra Weiny 		memcpy_to_page(dest_page, pg_offset,
6853590ec58SIra Weiny 			       workspace->out_buf.dst + buf_offset, bytes);
6865c1aab1dSNick Terrell 
6875c1aab1dSNick Terrell 		pg_offset += bytes;
6885c1aab1dSNick Terrell 	}
6895c1aab1dSNick Terrell 	ret = 0;
6905c1aab1dSNick Terrell finish:
6915c1aab1dSNick Terrell 	if (pg_offset < destlen) {
692d048b9c2SIra Weiny 		memzero_page(dest_page, pg_offset, destlen - pg_offset);
6935c1aab1dSNick Terrell 	}
6945c1aab1dSNick Terrell 	return ret;
6955c1aab1dSNick Terrell }
6965c1aab1dSNick Terrell 
6975c1aab1dSNick Terrell const struct btrfs_compress_op btrfs_zstd_compress = {
698be951045SDavid Sterba 	/* ZSTD uses own workspace manager */
699be951045SDavid Sterba 	.workspace_manager = NULL,
700e18333a7SDavid Sterba 	.max_level	= ZSTD_BTRFS_MAX_LEVEL,
701e18333a7SDavid Sterba 	.default_level	= ZSTD_BTRFS_DEFAULT_LEVEL,
7025c1aab1dSNick Terrell };
703