Commit 63a7cb13 authored by David Sterba's avatar David Sterba

btrfs: auto enable discard=async when possible

There's a request to automatically enable async discard for capable
devices. We can do that, the async mode is designed to wait for larger
freed extents and is not intrusive, with limits to iops, kbps or latency.

The status and tunables will be exported in /sys/fs/btrfs/FSID/discard .

The automatic selection is done if there's at least one discard capable
device in the filesystem (not capable devices are skipped). Mounting
with any other discard option will honor that option, notably mounting
with nodiscard will keep it disabled.

Link: https://lore.kernel.org/linux-btrfs/CAEg-Je_b1YtdsCR0zS5XZ_SbvJgN70ezwvRwLiCZgDGLbeMB=w@mail.gmail.com/Reviewed-by: default avatarBoris Burkov <boris@bur.io>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 467761f9
...@@ -1396,6 +1396,7 @@ enum { ...@@ -1396,6 +1396,7 @@ enum {
BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 28), BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 28),
BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 29), BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 29),
BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 30), BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 30),
BTRFS_MOUNT_NODISCARD = (1UL << 31),
}; };
#define BTRFS_DEFAULT_COMMIT_INTERVAL (30) #define BTRFS_DEFAULT_COMMIT_INTERVAL (30)
......
...@@ -3746,6 +3746,20 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device ...@@ -3746,6 +3746,20 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations");
} }
/*
* For devices supporting discard turn on discard=async automatically,
* unless it's already set or disabled. This could be turned off by
* nodiscard for the same mount.
*/
if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) ||
btrfs_test_opt(fs_info, DISCARD_ASYNC) ||
btrfs_test_opt(fs_info, NODISCARD)) &&
fs_info->fs_devices->discardable) {
btrfs_set_and_info(fs_info, DISCARD_ASYNC,
"auto enabling async discard");
btrfs_clear_opt(fs_info->mount_opt, NODISCARD);
}
/* /*
* Mount does not set all options immediately, we can do it now and do * Mount does not set all options immediately, we can do it now and do
* not have to wait for transaction commit * not have to wait for transaction commit
......
...@@ -918,12 +918,14 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, ...@@ -918,12 +918,14 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
btrfs_clear_opt(info->mount_opt, NODISCARD);
break; break;
case Opt_nodiscard: case Opt_nodiscard:
btrfs_clear_and_info(info, DISCARD_SYNC, btrfs_clear_and_info(info, DISCARD_SYNC,
"turning off discard"); "turning off discard");
btrfs_clear_and_info(info, DISCARD_ASYNC, btrfs_clear_and_info(info, DISCARD_ASYNC,
"turning off async discard"); "turning off async discard");
btrfs_set_opt(info->mount_opt, NODISCARD);
break; break;
case Opt_space_cache: case Opt_space_cache:
case Opt_space_cache_version: case Opt_space_cache_version:
......
...@@ -641,6 +641,9 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, ...@@ -641,6 +641,9 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
if (!bdev_nonrot(bdev)) if (!bdev_nonrot(bdev))
fs_devices->rotating = true; fs_devices->rotating = true;
if (bdev_max_discard_sectors(bdev))
fs_devices->discardable = true;
device->bdev = bdev; device->bdev = bdev;
clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
device->mode = flags; device->mode = flags;
......
...@@ -354,6 +354,8 @@ struct btrfs_fs_devices { ...@@ -354,6 +354,8 @@ struct btrfs_fs_devices {
* nonrot flag set * nonrot flag set
*/ */
bool rotating; bool rotating;
/* Devices support TRIM/discard commands */
bool discardable;
struct btrfs_fs_info *fs_info; struct btrfs_fs_info *fs_info;
/* sysfs kobjects */ /* sysfs kobjects */
......
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