Skip to content

Commit 63a7cb1

Browse files
committed
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: Boris Burkov <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 467761f commit 63a7cb1

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

fs/btrfs/ctree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,7 @@ enum {
13961396
BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 28),
13971397
BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 29),
13981398
BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 30),
1399+
BTRFS_MOUNT_NODISCARD = (1UL << 31),
13991400
};
14001401

14011402
#define BTRFS_DEFAULT_COMMIT_INTERVAL (30)

fs/btrfs/disk-io.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3746,6 +3746,20 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
37463746
btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations");
37473747
}
37483748

3749+
/*
3750+
* For devices supporting discard turn on discard=async automatically,
3751+
* unless it's already set or disabled. This could be turned off by
3752+
* nodiscard for the same mount.
3753+
*/
3754+
if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) ||
3755+
btrfs_test_opt(fs_info, DISCARD_ASYNC) ||
3756+
btrfs_test_opt(fs_info, NODISCARD)) &&
3757+
fs_info->fs_devices->discardable) {
3758+
btrfs_set_and_info(fs_info, DISCARD_ASYNC,
3759+
"auto enabling async discard");
3760+
btrfs_clear_opt(fs_info->mount_opt, NODISCARD);
3761+
}
3762+
37493763
/*
37503764
* Mount does not set all options immediately, we can do it now and do
37513765
* not have to wait for transaction commit

fs/btrfs/super.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,12 +918,14 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
918918
ret = -EINVAL;
919919
goto out;
920920
}
921+
btrfs_clear_opt(info->mount_opt, NODISCARD);
921922
break;
922923
case Opt_nodiscard:
923924
btrfs_clear_and_info(info, DISCARD_SYNC,
924925
"turning off discard");
925926
btrfs_clear_and_info(info, DISCARD_ASYNC,
926927
"turning off async discard");
928+
btrfs_set_opt(info->mount_opt, NODISCARD);
927929
break;
928930
case Opt_space_cache:
929931
case Opt_space_cache_version:

fs/btrfs/volumes.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,9 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
641641
if (!bdev_nonrot(bdev))
642642
fs_devices->rotating = true;
643643

644+
if (bdev_max_discard_sectors(bdev))
645+
fs_devices->discardable = true;
646+
644647
device->bdev = bdev;
645648
clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
646649
device->mode = flags;

fs/btrfs/volumes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ struct btrfs_fs_devices {
354354
* nonrot flag set
355355
*/
356356
bool rotating;
357+
/* Devices support TRIM/discard commands */
358+
bool discardable;
357359

358360
struct btrfs_fs_info *fs_info;
359361
/* sysfs kobjects */

0 commit comments

Comments
 (0)