Commit 19b2a2c7 authored by Dennis Zhou's avatar Dennis Zhou Committed by David Sterba

btrfs: make max async discard size tunable

Expose max_discard_size as a tunable via sysfs and switch the current
fixed maximum to the default value.
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 4aa9ad52
...@@ -469,6 +469,7 @@ struct btrfs_discard_ctl { ...@@ -469,6 +469,7 @@ struct btrfs_discard_ctl {
u64 prev_discard; u64 prev_discard;
atomic_t discardable_extents; atomic_t discardable_extents;
atomic64_t discardable_bytes; atomic64_t discardable_bytes;
u64 max_discard_size;
unsigned long delay; unsigned long delay;
u32 iops_limit; u32 iops_limit;
u32 kbps_limit; u32 kbps_limit;
......
...@@ -527,6 +527,7 @@ void btrfs_discard_init(struct btrfs_fs_info *fs_info) ...@@ -527,6 +527,7 @@ void btrfs_discard_init(struct btrfs_fs_info *fs_info)
discard_ctl->prev_discard = 0; discard_ctl->prev_discard = 0;
atomic_set(&discard_ctl->discardable_extents, 0); atomic_set(&discard_ctl->discardable_extents, 0);
atomic64_set(&discard_ctl->discardable_bytes, 0); atomic64_set(&discard_ctl->discardable_bytes, 0);
discard_ctl->max_discard_size = BTRFS_ASYNC_DISCARD_DEFAULT_MAX_SIZE;
discard_ctl->delay = BTRFS_DISCARD_MAX_DELAY_MSEC; discard_ctl->delay = BTRFS_DISCARD_MAX_DELAY_MSEC;
discard_ctl->iops_limit = BTRFS_DISCARD_MAX_IOPS; discard_ctl->iops_limit = BTRFS_DISCARD_MAX_IOPS;
discard_ctl->kbps_limit = 0; discard_ctl->kbps_limit = 0;
......
...@@ -10,7 +10,7 @@ struct btrfs_discard_ctl; ...@@ -10,7 +10,7 @@ struct btrfs_discard_ctl;
struct btrfs_block_group; struct btrfs_block_group;
/* Discard size limits */ /* Discard size limits */
#define BTRFS_ASYNC_DISCARD_MAX_SIZE (SZ_64M) #define BTRFS_ASYNC_DISCARD_DEFAULT_MAX_SIZE (SZ_64M)
/* Work operations */ /* Work operations */
void btrfs_discard_cancel_work(struct btrfs_discard_ctl *discard_ctl, void btrfs_discard_cancel_work(struct btrfs_discard_ctl *discard_ctl,
......
...@@ -3428,6 +3428,8 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group, ...@@ -3428,6 +3428,8 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
u64 *total_trimmed, u64 start, u64 end, u64 minlen, u64 *total_trimmed, u64 start, u64 end, u64 minlen,
bool async) bool async)
{ {
struct btrfs_discard_ctl *discard_ctl =
&block_group->fs_info->discard_ctl;
struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
struct btrfs_free_space *entry; struct btrfs_free_space *entry;
struct rb_node *node; struct rb_node *node;
...@@ -3436,6 +3438,7 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group, ...@@ -3436,6 +3438,7 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
u64 extent_bytes; u64 extent_bytes;
enum btrfs_trim_state extent_trim_state; enum btrfs_trim_state extent_trim_state;
u64 bytes; u64 bytes;
const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
while (start < end) { while (start < end) {
struct btrfs_trim_range trim_entry; struct btrfs_trim_range trim_entry;
...@@ -3475,11 +3478,11 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group, ...@@ -3475,11 +3478,11 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
goto next; goto next;
} }
unlink_free_space(ctl, entry); unlink_free_space(ctl, entry);
if (bytes > BTRFS_ASYNC_DISCARD_MAX_SIZE) { if (max_discard_size && bytes > max_discard_size) {
bytes = BTRFS_ASYNC_DISCARD_MAX_SIZE; bytes = max_discard_size;
extent_bytes = BTRFS_ASYNC_DISCARD_MAX_SIZE; extent_bytes = max_discard_size;
entry->offset += BTRFS_ASYNC_DISCARD_MAX_SIZE; entry->offset += max_discard_size;
entry->bytes -= BTRFS_ASYNC_DISCARD_MAX_SIZE; entry->bytes -= max_discard_size;
link_free_space(ctl, entry); link_free_space(ctl, entry);
} else { } else {
kmem_cache_free(btrfs_free_space_cachep, entry); kmem_cache_free(btrfs_free_space_cachep, entry);
...@@ -3584,12 +3587,15 @@ static int trim_bitmaps(struct btrfs_block_group *block_group, ...@@ -3584,12 +3587,15 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
u64 *total_trimmed, u64 start, u64 end, u64 minlen, u64 *total_trimmed, u64 start, u64 end, u64 minlen,
bool async) bool async)
{ {
struct btrfs_discard_ctl *discard_ctl =
&block_group->fs_info->discard_ctl;
struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
struct btrfs_free_space *entry; struct btrfs_free_space *entry;
int ret = 0; int ret = 0;
int ret2; int ret2;
u64 bytes; u64 bytes;
u64 offset = offset_to_bitmap(ctl, start); u64 offset = offset_to_bitmap(ctl, start);
const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
while (offset < end) { while (offset < end) {
bool next_bitmap = false; bool next_bitmap = false;
...@@ -3659,8 +3665,8 @@ static int trim_bitmaps(struct btrfs_block_group *block_group, ...@@ -3659,8 +3665,8 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
goto next; goto next;
} }
if (async && bytes > BTRFS_ASYNC_DISCARD_MAX_SIZE) if (async && max_discard_size && bytes > max_discard_size)
bytes = BTRFS_ASYNC_DISCARD_MAX_SIZE; bytes = max_discard_size;
bitmap_clear_bits(ctl, entry, start, bytes); bitmap_clear_bits(ctl, entry, start, bytes);
if (entry->bytes == 0) if (entry->bytes == 0)
......
...@@ -426,11 +426,42 @@ static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj, ...@@ -426,11 +426,42 @@ static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show, BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
btrfs_discard_kbps_limit_store); btrfs_discard_kbps_limit_store);
static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
struct kobj_attribute *a,
char *buf)
{
struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
return snprintf(buf, PAGE_SIZE, "%llu\n",
READ_ONCE(fs_info->discard_ctl.max_discard_size));
}
static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
struct kobj_attribute *a,
const char *buf, size_t len)
{
struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
u64 max_discard_size;
int ret;
ret = kstrtou64(buf, 10, &max_discard_size);
if (ret)
return -EINVAL;
WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
return len;
}
BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
btrfs_discard_max_discard_size_store);
static const struct attribute *discard_debug_attrs[] = { static const struct attribute *discard_debug_attrs[] = {
BTRFS_ATTR_PTR(discard, discardable_bytes), BTRFS_ATTR_PTR(discard, discardable_bytes),
BTRFS_ATTR_PTR(discard, discardable_extents), BTRFS_ATTR_PTR(discard, discardable_extents),
BTRFS_ATTR_PTR(discard, iops_limit), BTRFS_ATTR_PTR(discard, iops_limit),
BTRFS_ATTR_PTR(discard, kbps_limit), BTRFS_ATTR_PTR(discard, kbps_limit),
BTRFS_ATTR_PTR(discard, max_discard_size),
NULL, NULL,
}; };
......
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