Commit 1666edab authored by Dennis Zhou's avatar Dennis Zhou Committed by David Sterba

btrfs: add helper methods for workspace manager init and cleanup

Workspace manager init and cleanup code is open coded inside a for loop
over the compression types. This forces each compression type to rely on
the same workspace manager implementation. This patch creates helper
methods that will be the generic implementation for btrfs workspace
management.
Reviewed-by: default avatarNikolay Borisov <nborisov@suse.com>
Reviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
Signed-off-by: default avatarDennis Zhou <dennis@kernel.org>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 10b94a51
...@@ -796,31 +796,42 @@ static const struct btrfs_compress_op * const btrfs_compress_op[] = { ...@@ -796,31 +796,42 @@ static const struct btrfs_compress_op * const btrfs_compress_op[] = {
&btrfs_zstd_compress, &btrfs_zstd_compress,
}; };
void __init btrfs_init_compress(void) static void btrfs_init_workspace_manager(int type)
{ {
struct workspace_manager *wsman = &wsm[type];
struct list_head *workspace; struct list_head *workspace;
int i;
for (i = 0; i < BTRFS_NR_WORKSPACE_MANAGERS; i++) { wsman->ops = btrfs_compress_op[type];
wsm[i].ops = btrfs_compress_op[i];
INIT_LIST_HEAD(&wsm[i].idle_ws); INIT_LIST_HEAD(&wsman->idle_ws);
spin_lock_init(&wsm[i].ws_lock); spin_lock_init(&wsman->ws_lock);
atomic_set(&wsm[i].total_ws, 0); atomic_set(&wsman->total_ws, 0);
init_waitqueue_head(&wsm[i].ws_wait); init_waitqueue_head(&wsman->ws_wait);
/* /*
* Preallocate one workspace for each compression type so * Preallocate one workspace for each compression type so we can
* we can guarantee forward progress in the worst case * guarantee forward progress in the worst case
*/ */
workspace = wsm[i].ops->alloc_workspace(); workspace = wsman->ops->alloc_workspace();
if (IS_ERR(workspace)) { if (IS_ERR(workspace)) {
pr_warn("BTRFS: cannot preallocate compression workspace, will try later\n"); pr_warn(
"BTRFS: cannot preallocate compression workspace, will try later\n");
} else { } else {
atomic_set(&wsm[i].total_ws, 1); atomic_set(&wsman->total_ws, 1);
wsm[i].free_ws = 1; wsman->free_ws = 1;
list_add(workspace, &wsm[i].idle_ws); list_add(workspace, &wsman->idle_ws);
} }
}
static void btrfs_cleanup_workspace_manager(struct workspace_manager *wsman)
{
struct list_head *ws;
while (!list_empty(&wsman->idle_ws)) {
ws = wsman->idle_ws.next;
list_del(ws);
wsman->ops->free_workspace(ws);
atomic_dec(&wsman->total_ws);
} }
} }
...@@ -940,24 +951,6 @@ static void free_workspace(int type, struct list_head *workspace) ...@@ -940,24 +951,6 @@ static void free_workspace(int type, struct list_head *workspace)
cond_wake_up(ws_wait); cond_wake_up(ws_wait);
} }
/*
* cleanup function for module exit
*/
static void free_workspaces(void)
{
struct list_head *workspace;
int i;
for (i = 0; i < BTRFS_NR_WORKSPACE_MANAGERS; i++) {
while (!list_empty(&wsm[i].idle_ws)) {
workspace = wsm[i].idle_ws.next;
list_del(workspace);
wsm[i].ops->free_workspace(workspace);
atomic_dec(&wsm[i].total_ws);
}
}
}
/* /*
* Given an address space and start and length, compress the bytes into @pages * Given an address space and start and length, compress the bytes into @pages
* that are allocated on demand. * that are allocated on demand.
...@@ -1050,9 +1043,20 @@ int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page, ...@@ -1050,9 +1043,20 @@ int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
return ret; return ret;
} }
void __init btrfs_init_compress(void)
{
int i;
for (i = 0; i < BTRFS_NR_WORKSPACE_MANAGERS; i++)
btrfs_init_workspace_manager(i);
}
void __cold btrfs_exit_compress(void) void __cold btrfs_exit_compress(void)
{ {
free_workspaces(); int i;
for (i = 0; i < BTRFS_NR_WORKSPACE_MANAGERS; i++)
btrfs_cleanup_workspace_manager(&wsm[i]);
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment