Skip to contentSkip to conversation

blk_holder_ops, freeze/thaw

PATCH In review Kent Overstreet 2 participants 10 messages Mar 7, 2025 VFS

lore
Share & tools
lore
#0 lore raw

cover blk_holder_ops, freeze/thaw

Until now, it turns out bcachefs has been lacking freeze/thaw support, which is important for hibernate.

We thought we had freeze/thaw support, but it turns out that when .freeze_fs and .unfreeze_fs were implemented in super_operations, they were never being called - those functions are only called from the blk_holder callbacks, and the engineer implementing them never thought to check that.

So - there's a lesson here, and this is why I keep harping on testing. It's not enough to just fire off the automated test suite and call it a day, you do have to put actual thought into checking that your code is behaving as expected :)

It turns out the method we were originally talking about for freeze/thaw of just using our existing read-only/read-write paths doesn't work, for reasons that I neglected to remember when hooking up blk_holder_ops (locking and conflicting state management, I assume), so we need to block IOs at a lower level - which conveniently we can do with our existing per-device IO references.

This patchset also implements the other blk holder ops - the big one being mark_dead, which now offlines a single device from the filesystem if possible (leaving the filesystem read-write), or if not possible to run in degraded mode, sets the entire filesystem read only.

Kent Overstreet (7):
fs: export invalidate_inodes()
bcachefs: Stash a pointer to the filesystem for blk_holder_ops
bcachefs: Make sure c->vfs_sb is set before starting fs
bcachefs: Implement blk_holder_ops
bcachefs: Fix btree_node_scan io_ref handling
bcachefs: bch2_dev_get_ioref() may now sleep
bcachefs: Implement freeze/thaw

 fs/bcachefs/bcachefs.h        |   3 +
 fs/bcachefs/btree_node_scan.c |  17 ++-
 fs/bcachefs/fs.c              |  11 +-
 fs/bcachefs/journal_io.c      |   7 +-
 fs/bcachefs/sb-members.c      |  49 +++++++++
 fs/bcachefs/sb-members.h      |  18 +---
 fs/bcachefs/super-io.c        |   5 +-
 fs/bcachefs/super.c           | 191 +++++++++++++++++++++++++++++++++-
 fs/bcachefs/super.h           |   2 +
 fs/bcachefs/super_types.h     |   8 +-
 fs/inode.c                    |   1 +
 fs/internal.h                 |   1 -
 include/linux/fs.h            |   1 +
 13 files changed, 272 insertions(+), 42 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/7 fs: export invalidate_inodes()

Needed in bcachefs for implementing blk_holder_ops.mark_dead, since we can't use the standard fs holder ops (whicth assume a single block device filesystem).

Cc Alexander Viro <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Jan Kara <[email protected]>
---
 fs/inode.c         | 1 +
 fs/internal.h      | 1 -
 include/linux/fs.h | 1 +
 3 files changed, 2 insertions(+), 1 deletion(-)
fs/inode.c +1 -0
index 5587aabdaa5e..6364779a7006 100644
@@ -939,6 +939,7 @@ void invalidate_inodes(struct super_block *sb)
 
 	dispose_list(&dispose);
 }
+EXPORT_SYMBOL_GPL(invalidate_inodes);
 
 /*
  * Isolate the inode from the LRU in preparation for freeing it.
fs/internal.h +0 -1
index e7f02ae1e098..7cb515cede3f 100644
@@ -207,7 +207,6 @@ bool in_group_or_capable(struct mnt_idmap *idmap,
  * fs-writeback.c
  */
 extern long get_nr_dirty_inodes(void);
-void invalidate_inodes(struct super_block *sb);
 
 /*
  * dcache.c
include/linux/fs.h +1 -0
index be3ad155ec9f..5196317598ac 100644
@@ -3251,6 +3251,7 @@ extern void unlock_new_inode(struct inode *);
 extern void discard_new_inode(struct inode *);
 extern unsigned int get_next_ino(void);
 extern void evict_inodes(struct super_block *sb);
+extern void invalidate_inodes(struct super_block *sb);
 void dump_mapping(const struct address_space *);
 
 /*
Signature · 1 line
2.47.2
reviewer
#2 lore raw

Re: [PATCH 1/7] fs: export invalidate_inodes()

Quoted 8 lines · Kent Overstreet

Needed in bcachefs for implementing blk_holder_ops.mark_dead, since we can't use the standard fs holder ops (whicth assume a single block device filesystem).

Cc Alexander Viro <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Jan Kara <[email protected]>

Please use evict_inodes(). It is exactly the same and is actually already exported. Since you are the second one in last month to ask for this let's clean this up [1].

Honza

[1] https://lore.kernel.org/[email protected]

Quoted 45 lines
---
 fs/inode.c         | 1 +
 fs/internal.h      | 1 -
 include/linux/fs.h | 1 +
 3 files changed, 2 insertions(+), 1 deletion(-)
fs/inode.c +1 -0
index 5587aabdaa5e..6364779a7006 100644
@@ -939,6 +939,7 @@ void invalidate_inodes(struct super_block *sb)
 
 	dispose_list(&dispose);
 }
+EXPORT_SYMBOL_GPL(invalidate_inodes);
 
 /*
  * Isolate the inode from the LRU in preparation for freeing it.
fs/internal.h +0 -1
index e7f02ae1e098..7cb515cede3f 100644
@@ -207,7 +207,6 @@ bool in_group_or_capable(struct mnt_idmap *idmap,
  * fs-writeback.c
  */
 extern long get_nr_dirty_inodes(void);
-void invalidate_inodes(struct super_block *sb);
 
 /*
  * dcache.c
include/linux/fs.h +1 -0
index be3ad155ec9f..5196317598ac 100644
@@ -3251,6 +3251,7 @@ extern void unlock_new_inode(struct inode *);
 extern void discard_new_inode(struct inode *);
 extern unsigned int get_next_ino(void);
 extern void evict_inodes(struct super_block *sb);
+extern void invalidate_inodes(struct super_block *sb);
 void dump_mapping(const struct address_space *);
 
 /*
Signature · 1 line
2.47.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw

Re: [PATCH 1/7] fs: export invalidate_inodes()

Quoted 5 lines · Jan Kara
Quoted 4 lines · Kent Overstreet

Needed in bcachefs for implementing blk_holder_ops.mark_dead, since we can't use the standard fs holder ops (whicth assume a single block device filesystem).

Quoted 8 lines
Quoted 4 lines
Cc Alexander Viro <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Jan Kara <[email protected]>

Please use evict_inodes(). It is exactly the same and is actually already exported. Since you are the second one in last month to ask for this let's clean this up [1].

Switched it to evict_inodes().

#4 lore raw

PATCH 2/7 bcachefs: Stash a pointer to the filesystem for blk_holder_ops

In reply to Kent Overstreet

Note that we open block devices before we allocate bch_fs, but once attached to a filesystem they will be closed before the bch_fs is torn down - so stashing a pointer without a refcount looks incorrect but it's not.

---
 fs/bcachefs/super-io.c    | 2 +-
 fs/bcachefs/super.c       | 7 +++++++
 fs/bcachefs/super_types.h | 8 +++++++-
 3 files changed, 15 insertions(+), 2 deletions(-)
fs/bcachefs/super-io.c +1 -1
index 28724aee2c09..bd24b4f7eeb6 100644
@@ -748,7 +748,7 @@ static int __bch2_read_super(const char *path, struct bch_opts *opts,
 	memset(sb, 0, sizeof(*sb));
 	sb->mode	= BLK_OPEN_READ;
 	sb->have_bio	= true;
-	sb->holder	= kmalloc(1, GFP_KERNEL);
+	sb->holder	= kzalloc(sizeof(*sb->holder), GFP_KERNEL);
 	if (!sb->holder)
 		return -ENOMEM;
 
fs/bcachefs/super.c +7 -0
index 10c281ad96eb..b653dd480591 100644
@@ -1431,6 +1431,13 @@ static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
 	ca->disk_sb = *sb;
 	memset(sb, 0, sizeof(*sb));
 
+	/*
+	 * Stash pointer to the filesystem for blk_holder_ops - note that once
+	 * attached to a filesystem, we will always close the block device
+	 * before tearing down the filesystem object.
+	 */
+	ca->disk_sb.holder->c = ca->fs;
+
 	ca->dev = ca->disk_sb.bdev->bd_dev;
 
 	percpu_ref_reinit(&ca->io_ref);
fs/bcachefs/super_types.h +7 -1
index 368a63d938cf..3a899f799d1d 100644
@@ -2,13 +2,19 @@
 #ifndef _BCACHEFS_SUPER_TYPES_H
 #define _BCACHEFS_SUPER_TYPES_H
 
+struct bch_fs;
+
+struct bch_sb_handle_holder {
+	struct bch_fs		*c;
+};
+
 struct bch_sb_handle {
 	struct bch_sb		*sb;
 	struct file		*s_bdev_file;
 	struct block_device	*bdev;
 	char			*sb_name;
 	struct bio		*bio;
-	void			*holder;
+	struct bch_sb_handle_holder *holder;
 	size_t			buffer_size;
 	blk_mode_t		mode;
 	unsigned		have_layout:1;
Signature · 1 line
2.47.2
#5 lore raw

PATCH 3/7 bcachefs: Make sure c->vfs_sb is set before starting fs

In reply to Kent Overstreet

This is necessary for the new blk_holder_ops, which want the vfs super_block available for synchronization.

---
 fs/bcachefs/fs.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/bcachefs/fs.c +8 -3
index 2c011a465588..459ca8259fc0 100644
@@ -2218,9 +2218,10 @@ static int bch2_fs_get_tree(struct fs_context *fc)
 
 	bch2_opts_apply(&c->opts, opts);
 
-	ret = bch2_fs_start(c);
-	if (ret)
-		goto err_stop_fs;
+	/*
+	 * need to initialise sb and set c->vfs_sb _before_ starting fs,
+	 * for blk_holder_ops
+	 */
 
 	sb = sget(fc->fs_type, NULL, bch2_set_super, fc->sb_flags|SB_NOSEC, c);
 	ret = PTR_ERR_OR_ZERO(sb);
@@ -2282,6 +2283,10 @@ static int bch2_fs_get_tree(struct fs_context *fc)
 
 	sb->s_shrink->seeks = 0;
 
+	ret = bch2_fs_start(c);
+	if (ret)
+		goto err_put_super;
+
 	vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
 	ret = PTR_ERR_OR_ZERO(vinode);
 	bch_err_msg(c, ret, "mounting: error getting root inode");
Signature · 1 line
2.47.2
#6 lore raw

PATCH 4/7 bcachefs: Implement blk_holder_ops

In reply to Kent Overstreet

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.

---
 fs/bcachefs/super-io.c |  3 --
 fs/bcachefs/super.c    | 97 ++++++++++++++++++++++++++++++++++++++++++
 fs/bcachefs/super.h    |  2 +
 3 files changed, 99 insertions(+), 3 deletions(-)
fs/bcachefs/super-io.c +0 -3
index bd24b4f7eeb6..dcb69c72555e 100644
@@ -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;
fs/bcachefs/super.c +97 -0

109 lines not on this page · raw message

fs/bcachefs/super.h +2 -0
index 04f8287eff5c..23533bce5709 100644
@@ -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 */
Signature · 1 line
2.47.2
#7 lore raw

PATCH 5/7 bcachefs: Fix btree_node_scan io_ref handling

In reply to Kent Overstreet

This was completely fubar; it's now simplified a bit as well. Note that for_each_online_member() takes and releases io_refs as it iterates, so we need to release that if we break.

---
 fs/bcachefs/btree_node_scan.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
fs/bcachefs/btree_node_scan.c +8 -9
index fb73ec77c099..678161321e42 100644
@@ -270,7 +270,7 @@ static int read_btree_nodes_worker(void *p)
 err:
 	bio_put(bio);
 	free_page((unsigned long) buf);
-	percpu_ref_get(&ca->io_ref);
+	percpu_ref_put(&ca->io_ref);
 	closure_put(w->cl);
 	kfree(w);
 	return 0;
@@ -289,29 +289,28 @@ static int read_btree_nodes(struct find_btree_nodes *f)
 			continue;
 
 		struct find_btree_nodes_worker *w = kmalloc(sizeof(*w), GFP_KERNEL);
-		struct task_struct *t;
-
 		if (!w) {
 			percpu_ref_put(&ca->io_ref);
 			ret = -ENOMEM;
 			goto err;
 		}
 
-		percpu_ref_get(&ca->io_ref);
-		closure_get(&cl);
 		w->cl		= &cl;
 		w->f		= f;
 		w->ca		= ca;
 
-		t = kthread_run(read_btree_nodes_worker, w, "read_btree_nodes/%s", ca->name);
+		struct task_struct *t = kthread_create(read_btree_nodes_worker, w, "read_btree_nodes/%s", ca->name);
 		ret = PTR_ERR_OR_ZERO(t);
 		if (ret) {
 			percpu_ref_put(&ca->io_ref);
-			closure_put(&cl);
-			f->ret = ret;
-			bch_err(c, "error starting kthread: %i", ret);
+			kfree(w);
+			bch_err_msg(c, ret, "starting kthread");
 			break;
 		}
+
+		closure_get(&cl);
+		percpu_ref_get(&ca->io_ref);
+		wake_up_process(t);
 	}
 err:
 	closure_sync(&cl);
Signature · 1 line
2.47.2
#8 lore raw

PATCH 6/7 bcachefs: bch2_dev_get_ioref() may now sleep

In reply to Kent Overstreet

The next patch implementing freezing will change bch2_dev_get_ioref() to sleep if a device is currently frozen.

Add an annotation and fix the journal code accordingly.

---
 fs/bcachefs/journal_io.c | 5 ++++-
 fs/bcachefs/sb-members.h | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)
fs/bcachefs/journal_io.c +4 -1
index c12d9f9bd536..a510755a8364 100644
@@ -1664,6 +1664,7 @@ static CLOSURE_CALLBACK(journal_write_done)
 	}
 
 	bool completed = false;
+	bool do_discards = false;
 
 	for (seq = journal_last_unwritten_seq(j);
 	     seq <= journal_cur_seq(j);
@@ -1676,7 +1677,6 @@ static CLOSURE_CALLBACK(journal_write_done)
 			j->flushed_seq_ondisk = seq;
 			j->last_seq_ondisk = w->last_seq;
 
-			bch2_do_discards(c);
 			closure_wake_up(&c->freelist_wait);
 			bch2_reset_alloc_cursors(c);
 		}
@@ -1727,6 +1727,9 @@ static CLOSURE_CALLBACK(journal_write_done)
 	 */
 	bch2_journal_do_writes(j);
 	spin_unlock(&j->lock);
+
+	if (do_discards)
+		bch2_do_discards(c);
 }
 
 static void journal_write_endio(struct bio *bio)
fs/bcachefs/sb-members.h +2 -0
index b29b6c6c21dd..df91b02ce575 100644
@@ -283,6 +283,8 @@ static inline struct bch_dev *bch2_dev_iterate(struct bch_fs *c, struct bch_dev
 
 static inline struct bch_dev *bch2_dev_get_ioref(struct bch_fs *c, unsigned dev, int rw)
 {
+	might_sleep();
+
 	rcu_read_lock();
 	struct bch_dev *ca = bch2_dev_rcu(c, dev);
 	if (ca && !percpu_ref_tryget(&ca->io_ref))
Signature · 1 line
2.47.2
#9 lore raw

PATCH 7/7 bcachefs: Implement freeze/thaw

In reply to Kent Overstreet

This fills out our blk_holders_ops with freeze and thaw callbacks, for shutting down IO (generally during a system suspend).

This is implemented completely differently as on other filesystems since we have a low level synchronization object which conveniently works well for us - bch_dev.io_ref, normally used for guarding against a device being offlined while in use.

bch2_dev_get_ioref() now checks if a freeze is in progress if it fails to get ca->io_ref, and sleeps until complete and ca->io_ref is alive.

We also need a bit of synchronization for freeze/suspend vs. device online/offline, done with the new bch_dev.io_ref_statechange_lock.

---
 fs/bcachefs/bcachefs.h   |  3 ++
 fs/bcachefs/journal_io.c |  2 +-
 fs/bcachefs/sb-members.c | 49 ++++++++++++++++++++++
 fs/bcachefs/sb-members.h | 20 +--------
 fs/bcachefs/super.c      | 87 +++++++++++++++++++++++++++++++++++++---
 5 files changed, 136 insertions(+), 25 deletions(-)
fs/bcachefs/bcachefs.h +3 -0
index d2c3f59a668f..d03aa62907ad 100644
@@ -526,6 +526,9 @@ struct bch_dev {
 	struct completion	ref_completion;
 	struct percpu_ref	io_ref;
 	struct completion	io_ref_completion;
+	struct mutex		io_ref_statechange_lock;
+	unsigned		frozen;
+	wait_queue_head_t	frozen_wait;
 
 	struct bch_fs		*fs;
 
fs/bcachefs/journal_io.c +1 -1
index a510755a8364..6979fef5c128 100644
@@ -1769,7 +1769,7 @@ static CLOSURE_CALLBACK(journal_write_submit)
 		struct bch_dev *ca = bch2_dev_get_ioref(c, ptr->dev, WRITE);
 		if (!ca) {
 			/* XXX: fix this */
-			bch_err(c, "missing device for journal write\n");
+			bch_err(c, "missing device for journal write");
 			continue;
 		}
 
fs/bcachefs/sb-members.c +49 -0

55 lines not on this page · raw message

fs/bcachefs/sb-members.h +1 -19
index df91b02ce575..b3359ee63b0e 100644
@@ -281,25 +281,7 @@ static inline struct bch_dev *bch2_dev_iterate(struct bch_fs *c, struct bch_dev
 	return bch2_dev_tryget(c, dev_idx);
 }
 
-static inline struct bch_dev *bch2_dev_get_ioref(struct bch_fs *c, unsigned dev, int rw)
-{
-	might_sleep();
-
-	rcu_read_lock();
-	struct bch_dev *ca = bch2_dev_rcu(c, dev);
-	if (ca && !percpu_ref_tryget(&ca->io_ref))
-		ca = NULL;
-	rcu_read_unlock();
-
-	if (ca &&
-	    (ca->mi.state == BCH_MEMBER_STATE_rw ||
-	    (ca->mi.state == BCH_MEMBER_STATE_ro && rw == READ)))
-		return ca;
-
-	if (ca)
-		percpu_ref_put(&ca->io_ref);
-	return NULL;
-}
+struct bch_dev *bch2_dev_get_ioref(struct bch_fs *, unsigned, int);
 
 /* XXX kill, move to struct bch_fs */
 static inline struct bch_devs_mask bch2_online_devs(struct bch_fs *c)
fs/bcachefs/super.c +82 -5

129 lines not on this page · raw message

Signature · 1 line
2.47.2