]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bcachefs: Implement blk_holder_ops
authorKent Overstreet <kent.overstreet@linux.dev>
Tue, 25 Feb 2025 23:50:38 +0000 (18:50 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sat, 15 Mar 2025 01:02:16 +0000 (21:02 -0400)
We can't use the standard fs_holder_ops because they're meant for single
device filesystems - fs_bdev_mark_dead() in particular - and they assume
that the blk_holder is the super_block, which also doesn't work for a
multi device filesystem.

These generally follow the standard fs_holder_ops; the
locking/refcounting is a bit simplified because c->ro_ref suffices, and
bch2_fs_bdev_mark_dead() is not necessarily shutting down the entire
filesystem.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/super-io.c
fs/bcachefs/super.c
fs/bcachefs/super.h

index 74f1e45980db6ced62a4a86cd9862d6c2f0fd6ef..918e4e7704dde437b47aa90cd22eba47d2bcad1d 100644 (file)
@@ -25,9 +25,6 @@
 #include <linux/sort.h>
 #include <linux/string_choices.h>
 
-static const struct blk_holder_ops bch2_sb_handle_bdev_ops = {
-};
-
 struct bch2_metadata_version {
        u16             version;
        const char      *name;
index 4d656139561cf0d7329b7efad565854b3fa02b45..78a8daa80fcce31fe11bb393470ee32a8e6d2893 100644 (file)
@@ -1075,6 +1075,7 @@ int bch2_fs_start(struct bch_fs *c)
        }
 
        set_bit(BCH_FS_started, &c->flags);
+       wake_up(&c->ro_ref_wait);
 
        if (c->opts.read_only) {
                bch2_fs_read_only(c);
@@ -2024,6 +2025,102 @@ struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name)
        return ERR_PTR(-BCH_ERR_ENOENT_dev_not_found);
 }
 
+/* blk_holder_ops: */
+
+static struct bch_fs *bdev_get_fs(struct block_device *bdev)
+       __releases(&bdev->bd_holder_lock)
+{
+       struct bch_sb_handle_holder *holder = bdev->bd_holder;
+       struct bch_fs *c = holder->c;
+
+       if (c && !bch2_ro_ref_tryget(c))
+               c = NULL;
+
+       mutex_unlock(&bdev->bd_holder_lock);
+
+       if (c)
+               wait_event(c->ro_ref_wait, test_bit(BCH_FS_started, &c->flags));
+       return c;
+}
+
+/* returns with ref on ca->ref */
+static struct bch_dev *bdev_to_bch_dev(struct bch_fs *c, struct block_device *bdev)
+{
+       for_each_member_device(c, ca)
+               if (ca->disk_sb.bdev == bdev)
+                       return ca;
+       return NULL;
+}
+
+static void bch2_fs_bdev_mark_dead(struct block_device *bdev, bool surprise)
+{
+       struct bch_fs *c = bdev_get_fs(bdev);
+       if (!c)
+               return;
+
+       struct super_block *sb = c->vfs_sb;
+       if (sb) {
+               /*
+                * Not necessary, c->ro_ref guards against the filesystem being
+                * unmounted - we only take this to avoid a warning in
+                * sync_filesystem:
+                */
+               down_read(&sb->s_umount);
+       }
+
+       down_write(&c->state_lock);
+       struct bch_dev *ca = bdev_to_bch_dev(c, bdev);
+       if (!ca)
+               goto unlock;
+
+       if (bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, BCH_FORCE_IF_DEGRADED)) {
+               __bch2_dev_offline(c, ca);
+       } else {
+               if (sb) {
+                       if (!surprise)
+                               sync_filesystem(sb);
+                       shrink_dcache_sb(sb);
+                       evict_inodes(sb);
+               }
+
+               bch2_journal_flush(&c->journal);
+               bch2_fs_emergency_read_only(c);
+       }
+
+       bch2_dev_put(ca);
+unlock:
+       if (sb)
+               up_read(&sb->s_umount);
+       up_write(&c->state_lock);
+       bch2_ro_ref_put(c);
+}
+
+static void bch2_fs_bdev_sync(struct block_device *bdev)
+{
+       struct bch_fs *c = bdev_get_fs(bdev);
+       if (!c)
+               return;
+
+       struct super_block *sb = c->vfs_sb;
+       if (sb) {
+               /*
+                * Not necessary, c->ro_ref guards against the filesystem being
+                * unmounted - we only take this to avoid a warning in
+                * sync_filesystem:
+                */
+               down_read(&sb->s_umount);
+               sync_filesystem(sb);
+               up_read(&sb->s_umount);
+       }
+
+       bch2_ro_ref_put(c);
+}
+
+const struct blk_holder_ops bch2_sb_handle_bdev_ops = {
+       .mark_dead              = bch2_fs_bdev_mark_dead,
+       .sync                   = bch2_fs_bdev_sync,
+};
+
 /* Filesystem open: */
 
 static inline int sb_cmp(struct bch_sb *l, struct bch_sb *r)
index 04f8287eff5c3d30f035f8763b69cf11e5bcb5aa..23533bce570956ad41fe2acfafc86b4de8100205 100644 (file)
@@ -42,4 +42,6 @@ void bch2_fs_stop(struct bch_fs *);
 int bch2_fs_start(struct bch_fs *);
 struct bch_fs *bch2_fs_open(char * const *, unsigned, struct bch_opts);
 
+extern const struct blk_holder_ops bch2_sb_handle_bdev_ops;
+
 #endif /* _BCACHEFS_SUPER_H */