Skip to content

Commit 80be08c

Browse files
author
Kent Overstreet
committed
bcachefs: Filesystem discard option now propagates to devices
the discard option is special, because it's both a filesystem and a device option. When set at the filesytsem level, it's supposed to propagate to (if set persistently via sysfs) or override (if non persistently as a mount option) the devices - that now works correctly. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 8d7b7ac commit 80be08c

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

fs/bcachefs/alloc_background.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,19 @@ struct discard_buckets_state {
18061806
u64 discarded;
18071807
};
18081808

1809+
/*
1810+
* This is needed because discard is both a filesystem option and a device
1811+
* option, and mount options are supposed to apply to that mount and not be
1812+
* persisted, i.e. if it's set as a mount option we can't propagate it to the
1813+
* device.
1814+
*/
1815+
static inline bool discard_opt_enabled(struct bch_fs *c, struct bch_dev *ca)
1816+
{
1817+
return test_bit(BCH_FS_discard_mount_opt_set, &c->flags)
1818+
? c->opts.discard
1819+
: ca->mi.discard;
1820+
}
1821+
18091822
static int bch2_discard_one_bucket(struct btree_trans *trans,
18101823
struct bch_dev *ca,
18111824
struct btree_iter *need_discard_iter,
@@ -1869,7 +1882,7 @@ static int bch2_discard_one_bucket(struct btree_trans *trans,
18691882
s->discarded++;
18701883
*discard_pos_done = iter.pos;
18711884

1872-
if (ca->mi.discard && !c->opts.nochanges) {
1885+
if (discard_opt_enabled(c, ca) && !c->opts.nochanges) {
18731886
/*
18741887
* This works without any other locks because this is the only
18751888
* thread that removes items from the need_discard tree

fs/bcachefs/bcachefs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,8 @@ struct bch_dev {
627627
x(topology_error) \
628628
x(errors_fixed) \
629629
x(errors_not_fixed) \
630-
x(no_invalid_checks)
630+
x(no_invalid_checks) \
631+
x(discard_mount_opt_set) \
631632

632633
enum bch_fs_flags {
633634
#define x(n) BCH_FS_##n,

fs/bcachefs/fs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,9 @@ static int bch2_fs_get_tree(struct fs_context *fc)
21722172
if (ret)
21732173
goto err;
21742174

2175+
if (opt_defined(opts, discard))
2176+
set_bit(BCH_FS_discard_mount_opt_set, &c->flags);
2177+
21752178
/* Some options can't be parsed until after the fs is started: */
21762179
opts = bch2_opts_empty();
21772180
ret = bch2_parse_mount_opts(c, &opts, NULL, opts_parse->parse_later.buf);

fs/bcachefs/sysfs.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,15 @@ static ssize_t sysfs_opt_store(struct bch_fs *c,
664664
c->copygc_thread)
665665
wake_up_process(c->copygc_thread);
666666

667+
if (id == Opt_discard && !ca) {
668+
mutex_lock(&c->sb_lock);
669+
for_each_member_device(c, ca)
670+
opt->set_member(bch2_members_v2_get_mut(ca->disk_sb.sb, ca->dev_idx), v);
671+
672+
bch2_write_super(c);
673+
mutex_unlock(&c->sb_lock);
674+
}
675+
667676
ret = size;
668677
err:
669678
up_write(&c->state_lock);

0 commit comments

Comments
 (0)