fs: support freeze/thaw/mark_dead/sync with shared devices
Subsystems (10)
Christian Brauner
maintainerv2 cover fs: support freeze/thaw/mark_dead/sync with shared devices
This is a generalization of the device number to superblock so it works for actual block device and anonymous (or even mtd) devices.
fs_holder_ops recovers the affected superblock from bdev->bd_holder. That forces the holder of a block device to be exactly one superblock and makes it impossible for several superblocks to share a single device.
erofs does exactly that. It can mount read-only "blob" devices that are shared between many superblocks: a metadata-only erofs that indexes a set of per-layer blobs (one filesystem instead of one per OCI layer), or an incremental image whose base device is shared by several updates. Because the block layer only tracks a single holder, a freeze, thaw, removal or sync on such a device is never propagated to all the superblocks using it, and the current infrastructure has no way to find them.
This series replaces the bd_holder-based lookup with a global, dev_t-keyed table mapping each block device to the superblock(s) using it. The holder argument becomes purely the block layer's exclusivity token -- a superblock, or the file_system_type for a device shared within one filesystem type -- and the fs_holder_ops callbacks look the device up in the table and act on every superblock registered for it: 1:1 for most filesystems, 1:many for erofs.
Filesystems claim and release their devices through new fs_bdev_file_open_by_{dev,path}() and fs_bdev_file_release() helpers; the per-fs patches convert xfs, btrfs, ext4, f2fs and erofs over to them and fix cramfs and romfs, which released the registered main device with a raw bdev_fput().
Since every superblock is registered under its s_dev the table also replaces the last s_dev-keyed walk of the super_blocks list:user_get_super() resolves device numbers through it, so ustat() and quotactl() now work on any device a filesystem claims and no longer take sb_lock.
The longer-term motivation is to let userspace decide which devices may be onlined from one central place, without having to teach every filesystem about it individually.
---
Changes in v2:
- super: rework the device-to-superblock table reference counting: each
(device, superblock) entry carries a single claim count and holds one
passive reference on its superblock for the entry's lifetime. New prep
patches convert s_count to refcount_t s_passive and make put_super()
self-locking.
- super: preallocate the entry in alloc_super() and register it from the
set callbacks through set_anon_super()/set_bdev_super(); an insert
failure unwinds exactly like a set callback failure. The superblock
stashes the entry in sb->s_super_dev and kill_super_notify() drops the
claim through it.
- super: initialize the table from mnt_init(); the rootfs and shm mounts
are created long before any initcall runs.
- super: fold the v1 "refuse to claim a frozen block device" patch into
the registration helper and restore the EBUSY check for the primary
device in setup_bdev_super(): additional devices (the xfs log, the ext4
journal, erofs blobs) are now refused while frozen as well, answering
Jan's question on v1 3/8.
- Split the core patch into table/helpers/switch-over and move the
xfs/btrfs/ext4 conversions before the fs_holder_ops switch so no
freeze/mark_dead events are lost mid-series; erofs follows the switch.
- New prep patches: the ext4 KUnit tests allocate anonymous devices and
ocfs2 stops resetting s_dev on dismount.
- New: convert user_get_super() to the device table, plus a ustat()
selftest.
- New: fix a pre-existing double release of the realtime device file and
dangling buftarg pointers in xfs_open_devices()'s error unwind.
- New: convert f2fs's additional devices to the helpers; fix cramfs and
romfs releasing the registered main device with a raw bdev_fput().
- erofs: drop the .shutdown() and .remove_bdev() implementations and the
per-device "dead" flag. Immutable filesystems don't need them: the block
layer sets GD_DEAD before fs_bdev_mark_dead() so in-flight bios fail
anyway, erofs has no write path or journal to stop, and the read-only
loop_change_fd() case must not be forced to -EIO. Patch from Gao Xiang,
applied verbatim - thanks!
- btrfs: fix a general protection fault in close_fs_devices() on a failed
mount (reported by syzbot). The release path took the superblock from
device->fs_info, which is still NULL if open_ctree() fails before btrfs_init_devices_late(); it now uses bdev_file->private_data.
- erofs: the v1 conversion was sent with a generic boilerplate changelog;
superseded by Gao's patch above.
- Collect Reviewed-by from Jan Kara and Tested-by from syzbot.
- Rebase onto v7.1-rc1.
- Link to v1: https://patch.msgid.link/20260602-work-super-bdev_holder_global-v1-0-bb0fd82f3861@kernel.org
---
Christian Brauner (18):
xfs: fix the error unwind in xfs_open_devices()
super: convert s_count to refcount_t s_passive
super: take lock after last reference count
fs, block: move blk_mode_t and fop_flags_t into <linux/types.h>
ext4: use anonymous devices for KUnit test superblocks
ocfs2: don't reset s_dev on dismount
fs: maintain a global device-to-superblock table
fs: add dedicated block device open helpers for filesystems
xfs: port to fs_bdev_file_open_by_path()
btrfs: open via dedicated fs bdev helpers
ext4: open via dedicated fs bdev helpers
fs: look up superblocks via the device table in fs_holder_ops
fs: tolerate per-superblock freeze errors on shared devices
erofs: open via dedicated fs bdev helpers
f2fs: open via dedicated fs bdev helpers
super: make fs_holder_ops private
fs: look up the superblock via the device table in user_get_super() selftests/filesystems: add ustat() coverage
fs/btrfs/volumes.c | 31 +- fs/cramfs/inode.c | 2 +- fs/erofs/super.c | 35 +- fs/ext4/extents-test.c | 9 +- fs/ext4/mballoc-test.c | 9 +- fs/ext4/super.c | 12 +- fs/f2fs/super.c | 6 +- fs/internal.h | 1 + fs/namespace.c | 2 + fs/ocfs2/super.c | 1 - fs/romfs/super.c | 2 +- fs/super.c | 620 ++++++++++++++++------- fs/xfs/xfs_buf.c | 2 +- fs/xfs/xfs_super.c | 13 +- include/linux/blkdev.h | 9 - include/linux/fs.h | 2 - include/linux/fs/super.h | 8 + include/linux/fs/super_types.h | 4 +- include/linux/types.h | 2 + tools/testing/selftests/filesystems/.gitignore | 1 + tools/testing/selftests/filesystems/Makefile | 2 +- tools/testing/selftests/filesystems/ustat_test.c | 135 +++++ 22 files changed, 647 insertions(+), 261 deletions(-)
---
base-commit: 0c0d974f62e6603d4514e1a8035658edb353c68f
change-id: 20260602-work-super-bdev_holder_global-8cba5e52bed5
Christian Brauner
maintainerRFC PATCH v2 1/18 xfs: fix the error unwind in xfs_open_devices()
Since the rt and log block devices are closed in xfs_free_buftarg() the buftarg owns the device file. The error unwind does not respect that:
when the log buftarg allocation fails, out_free_rtdev_targ frees the rt buftarg - releasing rtdev_file - and then falls through to out_close_rtdev and releases it a second time.
The unwind also leaves mp->m_rtdev_targp and mp->m_ddev_targp pointing to the freed buftargs. The failed mount continues into deactivate_locked_super() -> xfs_kill_sb() -> xfs_mount_free(), which frees them again.
Clear the buftarg pointers once the unwind freed them and clear rtdev_file once the rt buftarg owns it, so nothing is released twice.
Reachable when a buftarg allocation fails after the data buftarg was set up: an I/O error in sync_blockdev() or an allocation failure in xfs_init_buftarg() while mounting with external rt and log devices.
--- fs/xfs/xfs_super.c | 3 +++ 1 file changed, 3 insertions(+)
fs/xfs/xfs_super.c +3 -0
@@ -534,8 +534,11 @@ xfs_open_devices( out_free_rtdev_targ:if (mp->m_rtdev_targp)xfs_free_buftarg (mp->m_rtdev_targp); + mp->m_rtdev_targp =NULL ; + rtdev_file =NULL ;/* released by xfs_free_buftarg() */ out_free_ddev_targ:xfs_free_buftarg (mp->m_ddev_targp); + mp->m_ddev_targp =NULL ; out_close_rtdev:if (rtdev_file)bdev_fput (rtdev_file);
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 2/18 super: convert s_count to refcount_t s_passive
In reply to Christian BraunerThe superblock carries two counters: s_active, the active reference count that keeps the filesystem usable, and s_count, the passive reference count that merely keeps the structure itself alive. Turn the passive count into a refcount_t and rename it to s_passive to make the pairing with s_active obvious.
Everything is still serialized by sb_lock, so there is no functional change; the conversion buys the usual refcount_t saturation and underflow checking. The following patches start dropping passive references without holding sb_lock and make the device-to-superblock table hold one passive reference per registered entry, which a plain integer cannot support.
--- fs/super.c | 18 +++++++++--------- include/linux/fs/super_types.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-)
fs/super.c +9 -9
@@ -102,7 +102,7 @@ static bool super_flags(const struct super_block *sb, unsigned int flags)* creation will succeed and SB_BORN is set by vfs_get_tree() or we're * woken and we'll see SB_DYING. * -* The caller must have acquired a temporary reference on @sb->s_count. +* The caller must have acquired a temporary reference on @sb->s_passive. * * Return: The function returns true if SB_BORN was set and with * s_umount held. The function returns false if SB_DYING was
@@ -367,7 +367,7 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags,spin_lock_init (&s->s_inode_wblist_lock);fserror_mount (s); - s->s_count =1 ; +refcount_set (&s->s_passive,1 );atomic_set (&s->s_active,1 );mutex_init (&s->s_vfs_rename_mutex);lockdep_set_class (&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
@@ -407,7 +407,7 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags,*/ static void __put_super (struct super_block *s) { -if (!--s->s_count) { +if (refcount_dec_and_test (&s->s_passive)) {list_del_init (&s->s_list);WARN_ON (s->s_dentry_lru.node);WARN_ON (s->s_inode_lru.node);
@@ -529,7 +529,7 @@ static bool grab_super(struct super_block *sb) {bool locked; - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock); locked =super_lock_excl (sb);if (locked) {
@@ -556,7 +556,7 @@ static bool grab_super(struct super_block *sb)* lock held in read mode in case of success. On successful return, * the caller must drop the s_umount lock when done. * -* Note that unlike get_super() et.al. this one does *not* bump ->s_count. +* Note that unlike get_super() et.al. this one does *not* bump ->s_passive. * The reason why it's safe is that we are OK with doing trylock instead * of down_read(). There's a couple of places that are OK with that, but * it's very much not a general-purpose interface.
@@ -858,7 +858,7 @@ static void __iterate_supers(void (*f)(struct super_block *, void *), void *arg, sb = next_super(sb, flags)) {if (super_flags (sb,SB_DYING ))continue ; - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock);if (flags &SUPER_ITER_UNLOCKED ) {
@@ -903,7 +903,7 @@ void iterate_supers_type(struct file_system_type *type,if (super_flags (sb,SB_DYING ))continue ; - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock); locked =super_lock_shared (sb);
@@ -935,7 +935,7 @@ struct super_block *user_get_super(dev_t dev, bool excl)if (sb->s_dev != dev)continue ; - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock); locked =super_lock (sb, excl);
@@ -1369,7 +1369,7 @@ static struct super_block *bdev_super_lock(struct block_device *bdev, bool excl)/* Make sure sb doesn't go away from under us */ spin_lock (&sb_lock); - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock);mutex_unlock (&bdev->bd_holder_lock);
include/linux/fs/super_types.h +1 -1
@@ -145,7 +145,7 @@ struct super_block {unsigned long s_magic;struct dentry *s_root;struct rw_semaphore s_umount; -int s_count; +refcount_t s_passive;atomic_t s_active;#ifdef CONFIG_SECURITY void *s_security;
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 3/18 super: take lock after last reference count
In reply to Christian Brauner__put_super() required the caller to hold sb_lock, so put_super() wrapped it. The per-device superblock table introduced later drops its passive references from contexts that do not hold sb_lock, so make put_super() self-locking: drop the count first and take sb_lock only for the final list_del.
With the count now dropped outside sb_lock a superblock can briefly sit on @super_blocks with s_passive == 0 before it is unlinked, so the list walkers (__iterate_supers(), iterate_supers_type(), user_get_super()) switch to refcount_inc_not_zero() and skip it.
--- fs/super.c | 63 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 28 insertions(+), 35 deletions(-)
fs/super.c +28 -35
@@ -403,12 +403,17 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags,/* Superblock refcounting */ /* -* Drop a superblock's refcount. The caller must hold sb_lock. +* Drop a superblock's passive reference. Must be called WITHOUT sb_lock held; +* put_super() acquires sb_lock itself when the final reference is dropped. */ -static void __put_super (struct super_block *s) +void put_super (struct super_block *s) {if (refcount_dec_and_test (&s->s_passive)) { + +spin_lock (&sb_lock);list_del_init (&s->s_list); +spin_unlock (&sb_lock); +WARN_ON (s->s_dentry_lru.node);WARN_ON (s->s_inode_lru.node);WARN_ON (s->s_mounts);
@@ -416,20 +421,6 @@ static void __put_super(struct super_block *s) } } -/** -* put_super - drop a temporary reference to superblock -* @sb: superblock in question -* -* Drops a temporary reference, frees superblock if there's no -* references left. -*/ -void put_super (struct super_block *sb) -{ -spin_lock (&sb_lock); -__put_super (sb); -spin_unlock (&sb_lock); -} -static void kill_super_notify (struct super_block *sb) {lockdep_assert_not_held (&sb->s_umount);
@@ -478,11 +469,7 @@ void deactivate_locked_super(struct super_block *s)kill_super_notify (s); -/* -* Since list_lru_destroy() may sleep, we cannot call it from -* put_super(), where we hold the sb_lock. Therefore we destroy -* the lru lists right now. -*/ +/* list_lru_destroy() may sleep; put_super() callers may not. */ list_lru_destroy (&s->s_dentry_lru);list_lru_destroy (&s->s_inode_lru);
@@ -851,14 +838,17 @@ static void __iterate_supers(void (*f)(struct super_block *, void *), void *arg,struct super_block *sb, *p =NULL ;bool excl = flags &SUPER_ITER_EXCL ; -guard (spinlock)(&sb_lock); +spin_lock (&sb_lock);for (sb =first_super (flags); !list_entry_is_head (sb, &super_blocks, s_list); sb =next_super (sb, flags)) {if (super_flags (sb,SB_DYING ))continue ; -refcount_inc (&sb->s_passive); + +if (!refcount_inc_not_zero (&sb->s_passive)) +continue ; +spin_unlock (&sb_lock);if (flags &SUPER_ITER_UNLOCKED ) {
@@ -868,13 +858,14 @@ static void __iterate_supers(void (*f)(struct super_block *, void *), void *arg,super_unlock (sb, excl); } -spin_lock (&sb_lock);if (p) -__put_super (p); +put_super (p); p = sb; +spin_lock (&sb_lock); } +spin_unlock (&sb_lock);if (p) -__put_super (p); +put_super (p); }void iterate_supers (void (*f)(struct super_block *,void *),void *arg)
@@ -903,7 +894,9 @@ void iterate_supers_type(struct file_system_type *type,if (super_flags (sb,SB_DYING ))continue ; -refcount_inc (&sb->s_passive); +if (!refcount_inc_not_zero (&sb->s_passive)) +continue ; +spin_unlock (&sb_lock); locked =super_lock_shared (sb);
@@ -912,14 +905,14 @@ void iterate_supers_type(struct file_system_type *type,super_unlock_shared (sb); } -spin_lock (&sb_lock);if (p) -__put_super (p); +put_super (p); p = sb; +spin_lock (&sb_lock); } -if (p) -__put_super (p);spin_unlock (&sb_lock); +if (p) +put_super (p); }EXPORT_SYMBOL (iterate_supers_type);
@@ -935,15 +928,17 @@ struct super_block *user_get_super(dev_t dev, bool excl)if (sb->s_dev != dev)continue ; -refcount_inc (&sb->s_passive); +if (!refcount_inc_not_zero (&sb->s_passive)) +continue ; +spin_unlock (&sb_lock); locked =super_lock (sb, excl);if (locked)return sb; +put_super (sb);spin_lock (&sb_lock); -__put_super (sb);break ; }spin_unlock (&sb_lock);
@@ -1368,9 +1363,7 @@ static struct super_block *bdev_super_lock(struct block_device *bdev, bool excl)lockdep_assert_not_held (&bdev->bd_disk->open_mutex);/* Make sure sb doesn't go away from under us */ -spin_lock (&sb_lock);refcount_inc (&sb->s_passive); -spin_unlock (&sb_lock);mutex_unlock (&bdev->bd_holder_lock);
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 4/18 fs, block: move blk_mode_t and fop_flags_t into <linux/types.h>
In reply to Christian Braunerblk_mode_t and fop_flags_t are both plain 'unsigned int __bitwise' flag typedefs, exactly like the gfp_t, slab_flags_t and fmode_t that already live in <linux/types.h>. Move them there so they are available everywhere without having to drag in a subsystem header.
--- include/linux/blkdev.h | 2 -- include/linux/fs.h | 2 -- include/linux/types.h | 2 ++ 3 files changed, 2 insertions(+), 4 deletions(-)
include/linux/blkdev.h +0 -2
@@ -126,8 +126,6 @@ struct blk_integrity {unsigned char pi_tuple_size; }; -typedef unsigned int __bitwise blk_mode_t ; -/* open for reading */ #define BLK_OPEN_READ ((__force blk_mode_t)(1 << 0))/* open for writing */
include/linux/fs.h +0 -2
@@ -1921,8 +1921,6 @@ struct dir_context {struct io_uring_cmd ;struct offset_ctx ; -typedef unsigned int __bitwise fop_flags_t ; -struct file_operations {struct module *owner;fop_flags_t fop_flags;
include/linux/types.h +2 -0
@@ -163,6 +163,8 @@ typedef u32 dma_addr_t;typedef unsigned int __bitwise gfp_t ;typedef unsigned int __bitwise slab_flags_t ;typedef unsigned int __bitwise fmode_t ; +typedef unsigned int __bitwise blk_mode_t ; +typedef unsigned int __bitwise fop_flags_t ;#ifdef CONFIG_PHYS_ADDR_T_64BIT typedef u64 phys_addr_t ;
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 5/18 ext4: use anonymous devices for KUnit test superblocks
In reply to Christian BraunerThe mballoc and extents KUnit tests create superblocks through sget_fc() with a set callback that never assigns s_dev and a kill_sb that only calls generic_shutdown_super().
The upcoming global device-to-superblock table registers every superblock under its s_dev, so each superblock needs a unique device number. Allocate a proper anonymous device via set_anon_super_fc() and release it through kill_anon_super().
--- fs/ext4/extents-test.c | 9 ++------- fs/ext4/mballoc-test.c | 9 ++------- 2 files changed, 4 insertions(+), 14 deletions(-)
fs/ext4/extents-test.c +2 -7
@@ -126,11 +126,6 @@ struct kunit_ext_test_param {struct kunit_ext_data_state exp_data_state[3 ]; }; -static void ext_kill_sb (struct super_block *sb) -{ -generic_shutdown_super (sb); -} -static int ext_init_fs_context (struct fs_context *fc) {return 0 ;
@@ -138,13 +133,13 @@ static int ext_init_fs_context(struct fs_context *fc)static int ext_set (struct super_block *sb,struct fs_context *fc) { -return 0 ; +return set_anon_super_fc (sb, fc); }static struct file_system_type ext_fs_type = { .name ="extents test" , .init_fs_context = ext_init_fs_context, - .kill_sb = ext_kill_sb, + .kill_sb = kill_anon_super, };static void extents_kunit_exit (struct kunit *test)
fs/ext4/mballoc-test.c +2 -7
@@ -59,11 +59,6 @@ static const struct super_operations mbt_sops = { .free_inode = mbt_free_inode, }; -static void mbt_kill_sb (struct super_block *sb) -{ -generic_shutdown_super (sb); -} -static int mbt_init_fs_context (struct fs_context *fc) {return 0 ;
@@ -72,7 +67,7 @@ static int mbt_init_fs_context(struct fs_context *fc)static struct file_system_type mbt_fs_type = { .name ="mballoc test" , .init_fs_context = mbt_init_fs_context, - .kill_sb = mbt_kill_sb, + .kill_sb = kill_anon_super, };static int mbt_mb_init (struct super_block *sb)
@@ -136,7 +131,7 @@ static void mbt_mb_release(struct super_block *sb)static int mbt_set (struct super_block *sb,struct fs_context *fc) { -return 0 ; +return set_anon_super_fc (sb, fc); }static struct super_block *mbt_ext4_alloc_super_block (void )
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 6/18 ocfs2: don't reset s_dev on dismount
In reply to Christian Braunerocfs2_dismount_volume() has reset sb->s_dev to zero since the original merge in ccd979bdbce9 ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem") as part of scrubbing the super_block. Nothing reads the field afterwards: all ocfs2-internal uses are mount-time log and trace prints, and dev_t-keyed superblock lookups skip a dying superblock anyway - s_root is gone before ->put_super runs and super_lock() refuses SB_DYING superblocks.
The upcoming device-to-superblock table registers every superblock under its s_dev. Drop the reset instead of leaving a superblock around whose s_dev contradicts its registration.
--- fs/ocfs2/super.c | 1 - 1 file changed, 1 deletion(-)
fs/ocfs2/super.c +0 -1
@@ -1882,7 +1882,6 @@ static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)ocfs2_delete_osb (osb);kfree (osb); - sb->s_dev =0 ; sb->s_fs_info =NULL ; }
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 7/18 fs: maintain a global device-to-superblock table
In reply to Christian Braunerfs_holder_ops recovers the owning superblock from bdev->bd_holder, which forces the holder to be exactly one superblock and prevents several superblocks from sharing one block device. That's what erofs is doing.
As a first step introduce a global dev_t-keyed rhltable mapping each device to the superblock(s) using it. The entry is preallocated in alloc_super() and registered under sb->s_dev by the set callback through set_anon_super() and set_bdev_super(), the two helpers every set callback assigns s_dev through. Registration is the final fallible act of a set callback, so an insert failure unwinds through sget_fc()'s existing set-failure path: the fs_context keeps ownership of s_fs_info and the callers' error paths stay correct. set_anon_super() releases the anonymous dev it allocated when registration fails. Unwinding through deactivate_locked_super() instead would run kill_sb() and free s_fs_info behind the caller's back: nfs and ceph free that object through a local pointer when sget_fc() fails and would double-free.
The superblock stashes the entry in sb->s_super_dev and kill_super_notify() drops the claim through it, so teardown doesn't depend on s_dev staying stable; an entry that was never registered is freed together with the superblock in destroy_super_work().
Each table entry holds a passive reference (s_passive) on its superblock, so the struct stays valid for as long as the entry is reachable. Entries are claim-counted through sd_ref: additional claims on the same (device, superblock) pair share the entry, and the unlink is deferred to the last put, so a later iteration cursor never resumes from a removed node.
The table is initialized from mnt_init(): the first superblocks (the tmpfs shm mount and rootfs) are created from start_kernel() long before any initcall runs, so an initcall would be too late.
The table has no readers yet; the fs_holder_ops callbacks are switched over once all devices a filesystem claims are registered.
--- fs/internal.h | 1 + fs/namespace.c | 2 + fs/super.c | 102 ++++++++++++++++++++++++++++++++++++++++- include/linux/fs/super_types.h | 2 + 4 files changed, 105 insertions(+), 2 deletions(-)
fs/internal.h +1 -0
@@ -137,6 +137,7 @@ extern int reconfigure_super(struct fs_context *);extern bool super_trylock_shared (struct super_block *sb);struct super_block *user_get_super (dev_t ,bool excl);void put_super (struct super_block *sb); +void __init super_dev_init (void );extern bool mount_capable (struct fs_context *);int sb_init_dio_done_wq (struct super_block *sb);
fs/namespace.c +2 -0
@@ -6262,6 +6262,8 @@ void __init mnt_init(void)if (!mount_hashtable || !mountpoint_hashtable)panic ("Failed to allocate mount hash table\n" ); +super_dev_init (); +kernfs_init (); err =sysfs_init ();
fs/super.c +100 -2
167 lines not on this page · raw message
include/linux/fs/super_types.h +2 -0
@@ -30,6 +30,7 @@ struct mount;struct mtd_info ;struct quotactl_ops ;struct shrinker ; +struct super_dev ;struct unicode_map ;struct user_namespace ;struct workqueue_struct ;
@@ -132,6 +133,7 @@ struct super_operations {struct super_block {struct list_head s_list;/* Keep this first */ dev_t s_dev;/* search index; _not_ kdev_t */ +struct super_dev *s_super_dev;/* sget_fc()'s device table claim */ unsigned char s_blocksize_bits;unsigned long s_blocksize;loff_t s_maxbytes;/* Max file size */
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 8/18 fs: add dedicated block device open helpers for filesystems
In reply to Christian BraunerAdd fs_bdev_file_open_by_{dev,path}() and fs_bdev_file_release(). They open the device with fs_holder_ops and register a claim in the device-to-superblock table. Claims on the same (device, superblock) pair share one entry, so when a filesystem claims a device it already uses (xfs with its log on the data device), no second entry is added and each superblock will be acted on once.
The holder argument remains purely the block layer's exclusivity token:
a superblock, or a file_system_type for a device shared by several superblocks of that type. The shared case only becomes usable once the fs_holder_ops callbacks resolve superblocks through the table instead of bdev->bd_holder.
Convert the main device, setup_bdev_super() and kill_block_super(),
over: the open finds the entry registered by sget_fc() and claims it again. cramfs and romfs bypass kill_block_super() so they can handle MTD mounts and release the main device with a plain bdev_fput(), which would leave the claim behind: the (dev, sb) entry would never be unregistered and the passive reference it holds would keep the superblock alive forever. Convert their release paths in the same step.
The frozen-device check stays in setup_bdev_super() for the primary device and is added to fs_bdev_register() for new claims, i.e. every additional device a filesystem opens through the helpers. Only a (device, superblock) pair the superblock claimed earlier may be reopened while frozen (xfs with its log on the data device): the freeze already covers that superblock through the existing claim, so nothing escapes it. Without the setup_bdev_super() check a device frozen before the mount even started (dm lock_fs, loop) could be mounted and written to (journal replay) under an active freeze, because the primary open reuses the entry registered by sget_fc() and never takes the new-claim path.
Both checks read bd_fsfreeze_count only after the entry is published (by sget_fc() for the primary, by fs_bdev_register() for new claims) and pair with bdev_freeze() incrementing the count before walking the
table: either the mount sees the elevated freeze count and fails with EBUSY, or the freeze finds the published entry and converges once SB_BORN is set.
--- fs/cramfs/inode.c | 2 +- fs/romfs/super.c | 2 +- fs/super.c | 154 ++++++++++++++++++++++++++++++++++++++++++++--- include/linux/fs/super.h | 7 +++ 4 files changed, 155 insertions(+), 10 deletions(-)
fs/cramfs/inode.c +1 -1
@@ -504,7 +504,7 @@ static void cramfs_kill_sb(struct super_block *sb) sb->s_mtd =NULL ; }else if (IS_ENABLED (CONFIG_CRAMFS_BLOCKDEV ) && sb->s_bdev) {sync_blockdev (sb->s_bdev); -bdev_fput (sb->s_bdev_file); +fs_bdev_file_release (sb->s_bdev_file, sb); }kfree (sbi); }
fs/romfs/super.c +1 -1
@@ -587,7 +587,7 @@ static void romfs_kill_sb(struct super_block *sb)#ifdef CONFIG_ROMFS_ON_BLOCK if (sb->s_bdev) {sync_blockdev (sb->s_bdev); -bdev_fput (sb->s_bdev_file); +fs_bdev_file_release (sb->s_bdev_file, sb); }#endif }
fs/super.c +146 -8
186 lines not on this page · raw message
include/linux/fs/super.h +7 -0
@@ -235,4 +235,11 @@ int freeze_super(struct super_block *super, enum freeze_holder who,int thaw_super (struct super_block *super,enum freeze_holder who,const void *freeze_owner); +struct file ; +struct file *fs_bdev_file_open_by_dev (dev_t dev,blk_mode_t mode,void *holder, +struct super_block *sb); +struct file *fs_bdev_file_open_by_path (const char *path,blk_mode_t mode, +void *holder,struct super_block *sb); +void fs_bdev_file_release (struct file *bdev_file,struct super_block *sb); +#endif /* _LINUX_FS_SUPER_H */
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 9/18 xfs: port to fs_bdev_file_open_by_path()
In reply to Christian BraunerRoute the log and rt device opens through fs_bdev_file_open_by_path() so each external device is registered against mp->m_super, and convert the matching releases to fs_bdev_file_release(). The data device is still opened and released by setup_bdev_super()/kill_block_super(); when the log lives on the data device the open resolves to the existing (dev, sb) entry so the superblock is acted on once.
--- fs/xfs/xfs_buf.c | 2 +- fs/xfs/xfs_super.c | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-)
fs/xfs/xfs_buf.c +1 -1
@@ -1615,7 +1615,7 @@ xfs_free_buftarg(fs_put_dax (btp->bt_daxdev, btp->bt_mount);/* the main block device is closed by kill_block_super */ if (btp->bt_bdev != btp->bt_mount->m_super->s_bdev) -bdev_fput (btp->bt_file); +fs_bdev_file_release (btp->bt_file, btp->bt_mount->m_super);kfree (btp); }
fs/xfs/xfs_super.c +5 -5
@@ -400,8 +400,8 @@ xfs_blkdev_get(blk_mode_t mode; mode =sb_open_mode (mp->m_super->s_flags); - *bdev_filep =bdev_file_open_by_path (name, mode, - mp->m_super, &fs_holder_ops); + *bdev_filep =fs_bdev_file_open_by_path (name, mode, + mp->m_super, mp->m_super);if (IS_ERR (*bdev_filep)) { error =PTR_ERR (*bdev_filep); *bdev_filep =NULL ;
@@ -526,7 +526,7 @@ xfs_open_devices( mp->m_logdev_targp = mp->m_ddev_targp;/* Handle won't be used, drop it */ if (logdev_file) -bdev_fput (logdev_file); +fs_bdev_file_release (logdev_file, mp->m_super); }return 0 ;
@@ -541,10 +541,10 @@ xfs_open_devices( mp->m_ddev_targp =NULL ; out_close_rtdev:if (rtdev_file) -bdev_fput (rtdev_file); +fs_bdev_file_release (rtdev_file, mp->m_super); out_close_logdev:if (logdev_file) -bdev_fput (logdev_file); +fs_bdev_file_release (logdev_file, mp->m_super);return error; }
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 10/18 btrfs: open via dedicated fs bdev helpers
In reply to Christian BraunerRoute the device opens through fs_bdev_file_open_by_path() so each device is registered against the superblock, and convert the matching releases to fs_bdev_file_release().
The temporary identification opens that only read the superblock and close again pass a NULL holder and keep using bdev_fput().
On the close path the superblock is taken from bdev_file->private_data (the holder set at open) rather than device->fs_info->sb: a mount that fails before btrfs_init_devices_late() runs leaves device->fs_info NULL, which close_fs_devices() would otherwise dereference.
--- fs/btrfs/volumes.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)
fs/btrfs/volumes.c +20 -9
@@ -480,7 +480,12 @@ btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder,struct block_device *bdev;int ret; - *bdev_file =bdev_file_open_by_path (device_path, flags, holder, &fs_holder_ops); +if (holder) + *bdev_file =fs_bdev_file_open_by_path (device_path, flags, + holder, holder); +else + *bdev_file =bdev_file_open_by_path (device_path, flags,NULL , +NULL );if (IS_ERR (*bdev_file)) { ret =PTR_ERR (*bdev_file);
@@ -495,7 +500,7 @@ btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder,if (holder) { ret =set_blocksize (*bdev_file,BTRFS_BDEV_BLOCKSIZE );if (ret) { -bdev_fput (*bdev_file); +fs_bdev_file_release (*bdev_file, holder); goto error; } }
@@ -503,7 +508,10 @@ btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder, *disk_super =btrfs_read_disk_super (bdev,0 , false);if (IS_ERR (*disk_super)) { ret =PTR_ERR (*disk_super); -bdev_fput (*bdev_file); +if (holder) +fs_bdev_file_release (*bdev_file, holder); +else +bdev_fput (*bdev_file); goto error; }
@@ -727,7 +735,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, error_free_page:btrfs_release_disk_super (disk_super); -bdev_fput (bdev_file); +fs_bdev_file_release (bdev_file, holder);return -EINVAL ; }
@@ -1087,7 +1095,7 @@ static void __btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices,continue ;if (device->bdev_file) { -bdev_fput (device->bdev_file); +fs_bdev_file_release (device->bdev_file, device->bdev_file->private_data); device->bdev =NULL ; device->bdev_file =NULL ; fs_devices->open_devices--;
@@ -1127,10 +1135,12 @@ void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices)/* Release a device that was made unfreezable for a membership change. */ void btrfs_release_device_allow_freeze (struct file *bdev_file) { +struct super_block *sb = bdev_file->private_data; +/* Yield before allow (strand-safe); file still open for the allow (UAF-safe). */ bdev_yield_claim (bdev_file);bdev_allow_freeze (file_bdev (bdev_file)); -bdev_fput (bdev_file); +fs_bdev_file_release (bdev_file, sb); }static void btrfs_close_bdev (struct btrfs_device *device,bool allow_freeze)
@@ -1147,7 +1157,8 @@ static void btrfs_close_bdev(struct btrfs_device *device, bool allow_freeze)if (allow_freeze)btrfs_release_device_allow_freeze (device->bdev_file);else -bdev_fput (device->bdev_file); +fs_bdev_file_release (device->bdev_file, + device->bdev_file->private_data); }static void btrfs_close_one_device (struct btrfs_device *device)
@@ -2894,8 +2905,8 @@ struct file *btrfs_open_device_deny_freeze(const char *path,return ERR_PTR (ret); } - bdev_file =bdev_file_open_by_dev (file_bdev (probe_file)->bd_dev, -BLK_OPEN_WRITE , sb, &fs_holder_ops); + bdev_file =fs_bdev_file_open_by_dev (file_bdev (probe_file)->bd_dev, +BLK_OPEN_WRITE , sb, sb);if (IS_ERR (bdev_file))bdev_allow_freeze (file_bdev (probe_file));bdev_fput (probe_file);
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 11/18 ext4: open via dedicated fs bdev helpers
In reply to Christian BraunerRoute the external journal device open through fs_bdev_file_open_by_dev() so it is registered against the superblock, and convert the matching releases to fs_bdev_file_release().
--- fs/ext4/super.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/ext4/super.c +6 -6
@@ -5793,7 +5793,7 @@ failed_mount8: __maybe_unusedbrelse (sbi->s_sbh);if (sbi->s_journal_bdev_file) {invalidate_bdev (file_bdev (sbi->s_journal_bdev_file)); -bdev_fput (sbi->s_journal_bdev_file); +fs_bdev_file_release (sbi->s_journal_bdev_file, sb); } out_fail:invalidate_bdev (sb->s_bdev);
@@ -5972,9 +5972,9 @@ static struct file *ext4_get_journal_blkdev(struct super_block *sb,struct ext4_super_block *es;int errno; - bdev_file =bdev_file_open_by_dev (j_dev, + bdev_file =fs_bdev_file_open_by_dev (j_dev,BLK_OPEN_READ |BLK_OPEN_WRITE |BLK_OPEN_RESTRICT_WRITES , - sb, &fs_holder_ops); + sb, sb);if (IS_ERR (bdev_file)) { ext4_msg(sb, KERN_ERR, "failed to open journal device unknown-block(%u,%u) %ld",
@@ -6034,7 +6034,7 @@ static struct file *ext4_get_journal_blkdev(struct super_block *sb, out_bh:brelse (bh); out_bdev: -bdev_fput (bdev_file); +fs_bdev_file_release (bdev_file, sb);return ERR_PTR (errno); }
@@ -6073,7 +6073,7 @@ static journal_t *ext4_open_dev_journal(struct super_block *sb, out_journal:ext4_journal_destroy (EXT4_SB (sb), journal); out_bdev: -bdev_fput (bdev_file); +fs_bdev_file_release (bdev_file, sb);return ERR_PTR (errno); }
@@ -7490,7 +7490,7 @@ static void ext4_kill_sb(struct super_block *sb)kill_block_super (sb);if (bdev_file) -bdev_fput (bdev_file); +fs_bdev_file_release (bdev_file, sb); }static struct file_system_type ext4_fs_type = {
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 12/18 fs: look up superblocks via the device table in fs_holder_ops
In reply to Christian BraunerSwitch the fs_holder_ops callbacks from recovering the single owning superblock out of bdev->bd_holder to walking the device-to-superblock table and acting on every superblock registered for the device. The holder argument becomes purely the block layer's exclusivity token and is no longer needed by the fs specific callbacks.
All devices opened with fs_holder_ops are registered by now: the main device since setup_bdev_super() switched to fs_bdev_file_open_by_dev() and the extra devices (xfs log and realtime devices, btrfs member devices, the ext4 external journal) since the preceding per-filesystem conversions. So no event is lost in the switchover.
The walk uses a refcount-pinning cursor: each step takes a reference on the entry via sd_ref and resumes from its sd_node. Unlinking an entry is deferred to the last unpin, so a cursor never resumes from a removed node.
mark_dead and sync only need the passive reference the entry holds plus s_umount, which they take with super_lock_shared(). freeze and thaw additionally need an active reference and acquire it with get_active_super(), which waits for the superblock to be born before taking s_active. Taking s_active before the superblock is born would pin a still-mounting superblock so a racing mount that aborts could never drop s_active to zero and reach SB_DYING, deadlocking the wait for SB_BORN. This is how filesystems_freeze() and filesystems_thaw() acquire it too.
One semantic change: when no live superblock uses the device anymore (the holder is dying or was never registered), fs_bdev_freeze() and fs_bdev_thaw() now return 0 - freeze after syncing the block device - where they used to return -EINVAL.
The freeze-deny release path moves to the table in the same switchover. A device made unfreezable for a btrfs membership change must drop its table entry before re-allowing freezing; otherwise a freeze racing the release reaches the superblock through the still-registered entry and is stranded once the release unlinks it. Split fs_bdev_unregister() out of fs_bdev_file_release() - the inverse of fs_bdev_register() - so btrfs_release_device_allow_freeze() can drop the {dev, sb} entry, re-allow freezing on the still-open device, then close it. Re-allowing only after the entry is gone keeps a racing freeze from reaching the superblock, and doing it while the file is still open avoids touching the block device after the close. btrfs previously yielded bd_holder before re-allowing, which this commit makes irrelevant to freeze resolution.
--- fs/btrfs/volumes.c | 6 +- fs/super.c | 269 +++++++++++++++++++++++------------------------ include/linux/fs/super.h | 1 + 3 files changed, 138 insertions(+), 138 deletions(-)
fs/btrfs/volumes.c +3 -3
@@ -1137,10 +1137,10 @@ void btrfs_release_device_allow_freeze(struct file *bdev_file) {struct super_block *sb = bdev_file->private_data; -/* Yield before allow (strand-safe); file still open for the allow (UAF-safe). */ -bdev_yield_claim (bdev_file); +/* Unregister before re-allowing (strand-safe); file still open (UAF-safe). */ +fs_bdev_unregister (bdev_file, sb);bdev_allow_freeze (file_bdev (bdev_file)); -fs_bdev_file_release (bdev_file, sb); +bdev_fput (bdev_file); }static void btrfs_close_bdev (struct btrfs_device *device,bool allow_freeze)
fs/super.c +134 -135
345 lines not on this page · raw message
include/linux/fs/super.h +1 -0
@@ -240,6 +240,7 @@ struct file *fs_bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, struct super_block *sb);struct file *fs_bdev_file_open_by_path (const char *path,blk_mode_t mode,void *holder,struct super_block *sb); +void fs_bdev_unregister (struct file *bdev_file,struct super_block *sb);void fs_bdev_file_release (struct file *bdev_file,struct super_block *sb);#endif /* _LINUX_FS_SUPER_H */
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 13/18 fs: tolerate per-superblock freeze errors on shared devices
In reply to Christian BraunerWhen several superblocks share a device, keep it frozen even if some of them failed to freeze and swallow the error: rolling the others back via thaw_super() can fail too, so neither is a clear win. A single filesystem still reports its error, and a sync_blockdev() failure is always reported. Thaw follows the same rule.
A device can only be shared once superblocks claim it with a common exclusivity token, which erofs starts doing in the next patch; for everyone else the loop visits exactly one superblock and the behavior is unchanged.
--- fs/super.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
fs/super.c +20 -3
@@ -1548,13 +1548,15 @@ static void fs_bdev_sync(struct block_device *bdev)* devices is frozen once per device and stays frozen until all are thawed; the * block layer nests these freezes so the count stays balanced. * -* Return: 0, or the first error from freezing a superblock or syncing the -* block device. +* Return: 0, or the error from the one superblock on a single-fs device. When +* several superblocks share @bdev a per-superblock failure is swallowed +* (see below), but a sync_blockdev() failure is always reported. */ static int fs_bdev_freeze (struct block_device *bdev) {dev_t dev = bdev->bd_dev;struct super_dev *sb_dev; +unsigned int count =0 ;int error =0 , err;lockdep_assert_held (&bdev->bd_fsfreeze_mutex);
@@ -1568,8 +1570,17 @@ static int fs_bdev_freeze(struct block_device *bdev)if (err && !error) error = err;deactivate_super (sb_dev->sd_sb); + count++; } +/* +* When several superblocks share the device, keep it frozen even if some +* of them failed to freeze and swallow the error: rolling the rest back +* via thaw_super() can fail too, so neither is a clear win. A single +* filesystem (count == 1) still reports its error. +*/ +if (error && count >1 ) + error =0 ;if (!error) error =sync_blockdev (bdev);return error;
@@ -1583,12 +1594,14 @@ static int fs_bdev_freeze(struct block_device *bdev)* A zero return does not imply a superblock is fully unfrozen; it may have been * frozen more than once (by the kernel or via another device). * -* Return: 0, or the first error from thawing a superblock. +* Return: 0, or the first error on a single-fs device; a shared device swallows +* per-superblock errors, as fs_bdev_freeze() does. */ static int fs_bdev_thaw (struct block_device *bdev) {dev_t dev = bdev->bd_dev;struct super_dev *sb_dev; +unsigned int count =0 ;int error =0 , err;lockdep_assert_held (&bdev->bd_fsfreeze_mutex);
@@ -1602,8 +1615,12 @@ static int fs_bdev_thaw(struct block_device *bdev)if (err && !error) error = err;deactivate_super (sb_dev->sd_sb); + count++; } +/* Shared device: swallow per-superblock errors, like fs_bdev_freeze(). */ +if (error && count >1 ) + error =0 ;return error; }
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 14/18 erofs: open via dedicated fs bdev helpers
In reply to Christian BraunerRoute opens through fs_bdev_file_open_by_path() so each external device is registered against the correct superblock, and convert the matching releases.
Gao Xiang: I think typical immutable filesystems don't need .shutdown() and .remove_bdev() for the following reasons:
- blk_mark_disk_dead() sets GD_DEAD in advance of fs_bdev_mark_dead() so that the following bios will fail immediately; block_device references are still valid so it seems overkill to handle dead blockdevs in the deep filesystem I/O submission path.
- Immutable filesystems like EROFS don't have write paths and journals, so they don't need to block writes (i.e., new dirty pages), metadata changes, and abort journals.
- The comment above loop_change_fd() documents a valid read-only use case we need to support anyway, but it calls disk_force_media_change() which will call fs_bdev_mark_dead() later: we don't want loop_change_fd() shutdowns the active filesystems and return -EIO unconditionally.
Currently I think the default behavior (shrink_dcache_sb + evict_inodes) in fs_bdev_mark_dead() is enough for immutable filesystems, tried to document in the commit here for later reference.
--- fs/erofs/super.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-)
fs/erofs/super.c +23 -12
@@ -153,8 +153,8 @@ static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb, }else if (!sbi->devs->flatdev) { file =erofs_is_fileio_mode (sbi) ?filp_open (dif->path,O_RDONLY |O_LARGEFILE ,0 ) : -bdev_file_open_by_path (dif->path, -BLK_OPEN_READ , sb->s_type,NULL ); +fs_bdev_file_open_by_path (dif->path, +BLK_OPEN_READ , sb->s_type, sb);if (IS_ERR (file)) {if (file ==ERR_PTR (-ENOTBLK ))return -EINVAL ;
@@ -843,11 +843,16 @@ static int erofs_fc_reconfigure(struct fs_context *fc)static int erofs_release_device_info (int id,void *ptr,void *data) { +struct super_block *sb = data;struct erofs_device_info *dif = ptr;fs_put_dax (dif->dax_dev,NULL ); -if (dif->file) -fput (dif->file); +if (dif->file) { +if (S_ISBLK (file_inode (dif->file)->i_mode)) +fs_bdev_file_release (dif->file, sb); +else +fput (dif->file); + }erofs_fscache_unregister_cookie (dif->fscache); dif->fscache =NULL ;kfree (dif->path);
@@ -855,18 +860,19 @@ static int erofs_release_device_info(int id, void *ptr, void *data)return 0 ; } -static void erofs_free_dev_context (struct erofs_dev_context *devs) +static void erofs_free_dev_context (struct erofs_dev_context *devs, +struct super_block *sb) {if (!devs)return ; -idr_for_each (&devs->tree, &erofs_release_device_info,NULL ); +idr_for_each (&devs->tree, &erofs_release_device_info, sb);idr_destroy (&devs->tree);kfree (devs); } -static void erofs_sb_free (struct erofs_sb_info *sbi) +static void erofs_sb_free (struct erofs_sb_info *sbi,struct super_block *sb) { -erofs_free_dev_context (sbi->devs); +erofs_free_dev_context (sbi->devs, sb);kfree (sbi->fsid);kfree_sensitive (sbi->domain_id);if (sbi->dif0.file)
@@ -879,8 +885,13 @@ static void erofs_fc_free(struct fs_context *fc) {struct erofs_sb_info *sbi = fc->s_fs_info; -if (sbi)/* free here if an error occurs before transferring to sb */ -erofs_sb_free (sbi); +/* +* Freed here only if an error occurs before the sb is set up; at that +* point no block-backed device has been claimed (that happens in +* fill_super), so the NULL sb never reaches fs_bdev_file_release(). +*/ +if (sbi) +erofs_sb_free (sbi,NULL ); }static const struct fs_context_operations erofs_context_ops = {
@@ -936,7 +947,7 @@ static void erofs_kill_sb(struct super_block *sb)erofs_drop_internal_inodes (sbi);fs_put_dax (sbi->dif0.dax_dev,NULL );erofs_fscache_unregister_fs (sb); -erofs_sb_free (sbi); +erofs_sb_free (sbi, sb); sb->s_fs_info =NULL ; }
@@ -948,7 +959,7 @@ static void erofs_put_super(struct super_block *sb)erofs_shrinker_unregister (sb);erofs_xattr_prefixes_cleanup (sb);erofs_drop_internal_inodes (sbi); -erofs_free_dev_context (sbi->devs); +erofs_free_dev_context (sbi->devs, sb); sbi->devs =NULL ;erofs_fscache_unregister_fs (sb); }
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 15/18 f2fs: open via dedicated fs bdev helpers
In reply to Christian BraunerRoute the extra device opens of a multi-device f2fs through fs_bdev_file_open_by_path() so each device is registered against the superblock, and convert the matching release in destroy_device_list() to fs_bdev_file_release(). The first device aliases the main bdev file opened by setup_bdev_super() and is already registered through it.
f2fs opened its extra devices without holder ops, so a freeze, sync, or removal of one of them was never propagated to the superblock. Registering them wires those events up: every device now freezes, thaws, syncs, and shuts down the filesystem like the main device does.
--- fs/f2fs/super.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/f2fs/super.c +3 -3
@@ -1970,7 +1970,7 @@ static void destroy_device_list(struct f2fs_sb_info *sbi)for (i =0 ; i < sbi->s_ndevs; i++) {if (i >0 ) -bdev_fput (FDEV (i).bdev_file); +fs_bdev_file_release (FDEV (i).bdev_file, sbi->sb);#ifdef CONFIG_BLK_DEV_ZONED kvfree (FDEV (i).blkz_seq);#endif
@@ -4840,8 +4840,8 @@ static int f2fs_scan_devices(struct f2fs_sb_info *sbi)FDEV (i).end_blk =FDEV (i).start_blk +SEGS_TO_BLKS (sbi,FDEV (i).total_segments) -1 ; -FDEV (i).bdev_file =bdev_file_open_by_path ( -FDEV (i).path, mode, sbi->sb,NULL ); +FDEV (i).bdev_file =fs_bdev_file_open_by_path ( +FDEV (i).path, mode, sbi->sb, sbi->sb); } }if (IS_ERR (FDEV (i).bdev_file))
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 16/18 super: make fs_holder_ops private
In reply to Christian BraunerNow that filesystems open and claim their block devices through fs_bdev_file_open_by_{dev,path}(), nothing outside fs/super.c references fs_holder_ops. Make it static and drop its declaration from blkdev.h.
--- fs/super.c | 3 +-- include/linux/blkdev.h | 7 ------- 2 files changed, 1 insertion(+), 9 deletions(-)
fs/super.c +1 -2
@@ -1624,13 +1624,12 @@ static int fs_bdev_thaw(struct block_device *bdev)return error; } -const struct blk_holder_ops fs_holder_ops = { +static const struct blk_holder_ops fs_holder_ops = { .mark_dead = fs_bdev_mark_dead, .sync = fs_bdev_sync, .freeze = fs_bdev_freeze, .thaw = fs_bdev_thaw, }; -EXPORT_SYMBOL_GPL (fs_holder_ops);static struct super_dev *super_dev_lookup (dev_t dev,struct super_block *sb) {
include/linux/blkdev.h +0 -7
@@ -1772,13 +1772,6 @@ struct blk_holder_ops {__releases (&bdev->bd_holder_lock); }; -/* -* For filesystems using @fs_holder_ops, the @holder argument passed to -* helpers used to open and claim block devices via -* bd_prepare_to_claim() must point to a superblock. -*/ -extern const struct blk_holder_ops fs_holder_ops; -/* * Return the correct open flags for blkdev_get_by_* for super block flags * as stored in sb->s_flags.
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 17/18 fs: look up the superblock via the device table in user_get_super()
In reply to Christian Brauneruser_get_super() still finds the superblock for a device number by walking the global super_blocks list under sb_lock. Every superblock is registered in the device table under its s_dev since sget_fc() inserts it there, including superblocks on anonymous devices, so use the table instead.
The refcount-pinning cursor helpers super_dev_{get,first,next}() only touch table state and do not depend on CONFIG_BLOCK, so drop the CONFIG_BLOCK guard around them: their new caller serves anonymous devices as well (ustat() on e.g. tmpfs) and is built without CONFIG_BLOCK. The guard falls in this patch rather than separately since without this caller the helpers would be unused without CONFIG_BLOCK.
The pinned entry holds a passive reference on the superblock so super_lock() can be called directly; once the superblock is locked grab a passive reference for the caller before dropping the pin.
The device table contains more than the old walk could find: a superblock is also registered for every additional device it claims (the xfs log and realtime devices, btrfs member devices, the ext4 external journal, erofs blob devices). Don't filter those out:
specifying any device a filesystem uses now resolves to that filesystem, so ustat() and quotactl() work on e.g. the xfs log device or a btrfs member device (the latter used to fail outright as btrfs superblocks carry an anonymous s_dev that never matches a member device). When several superblocks share a device (erofs blob devices) the first live superblock wins.
The cursor also keeps scanning past dying superblocks where the old walk gave up after the first s_dev match, so a mount racing with the unmount of the same device (or with the reuse of a recycled anonymous dev_t) finds the live superblock where the old walk could spuriously return NULL.
This removes the last s_dev-keyed walk of the super_blocks list and takes ustat() and quotactl()'s block device lookup off sb_lock entirely.
--- fs/super.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-)
fs/super.c +8 -20
@@ -501,7 +501,6 @@ static int super_dev_register(struct super_block *sb)return err; } -#ifdef CONFIG_BLOCK static struct super_dev *super_dev_get (struct rhlist_head *pos) {struct super_dev *sb_dev;
@@ -535,7 +534,6 @@ static struct super_dev *super_dev_next(struct super_dev *prev)super_dev_put (prev);return sb_dev; } -#endif static void kill_super_notify (struct super_block *sb) {
@@ -1044,29 +1042,19 @@ EXPORT_SYMBOL(iterate_supers_type);struct super_block *user_get_super (dev_t dev,bool excl) { -struct super_block *sb; - -spin_lock (&sb_lock); -list_for_each_entry (sb, &super_blocks, s_list) { -bool locked; +struct super_dev *sb_dev; -if (sb->s_dev != dev) -continue ; +for (sb_dev =super_dev_first (dev); sb_dev; sb_dev =super_dev_next (sb_dev)) { +struct super_block *sb = sb_dev->sd_sb; -if (!refcount_inc_not_zero (&sb->s_passive)) +if (!super_lock (sb, excl))continue ; -spin_unlock (&sb_lock); - - locked =super_lock (sb, excl); -if (locked) -return sb; - -put_super (sb); -spin_lock (&sb_lock); -break ; +/* The pinned entry holds a passive reference, take our own. */ +refcount_inc (&sb->s_passive); +super_dev_put (sb_dev); +return sb; } -spin_unlock (&sb_lock);return NULL ; }
Signature · 1 line
2.47.3
Christian Brauner
maintainerRFC PATCH v2 18/18 selftests/filesystems: add ustat() coverage
In reply to Christian Brauneruser_get_super() is now backed by the global device-to-superblock table instead of a walk of the super_blocks list. ustat(2) is its most direct user-visible consumer but nothing in the tree exercises it.
Add a small regression test: the device number of a mounted tmpfs (an anonymous device, registered in the table by sget_fc()) must resolve, it must stop resolving after the unmount (the entry is dropped again in kill_super_notify()), and bogus device numbers keep reporting EINVAL.
The test passes on kernels before the conversion: it pins down the semantics the table-backed lookup must preserve.
--- tools/testing/selftests/filesystems/.gitignore | 1 + tools/testing/selftests/filesystems/Makefile | 2 +- tools/testing/selftests/filesystems/ustat_test.c | 135 +++++++++++++++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/.gitignore +1 -0
@@ -5,3 +5,4 @@ fclog file_stressor anon_inode_test kernfs_test +ustat_test
tools/testing/selftests/filesystems/Makefile +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS += $(KHDR_INCLUDES) -TEST_GEN_PROGS := devpts_pts file_stressor anon_inode_test kernfs_test fclog +TEST_GEN_PROGS := devpts_pts file_stressor anon_inode_test kernfs_test fclog ustat_test TEST_GEN_PROGS_EXTENDED := dnotify_test include ../lib.mk
tools/testing/selftests/filesystems/ustat_test.c new file +135 -0
135 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [PATCH RFC v2 15/18] f2fs: open via dedicated fs bdev helpers
In reply to Christian BraunerQuoted 6 lines · Christian Brauner
Route the extra device opens of a multi-device f2fs through
fs_bdev_file_open_by_path()so each device is registered against the superblock, and convert the matching release indestroy_device_list()tofs_bdev_file_release(). The first device aliases the main bdev file opened bysetup_bdev_super()and is already registered through it.
Quoted 6 lines
f2fs opened its extra devices without holder ops, so a freeze, sync, or removal of one of them was never propagated to the superblock. Registering them wires those events up: every device now freezes, thaws, syncs, and shuts down the filesystem like the main device does.
Signed-off-by Christian Brauner (Amutable) <[email protected]>
Thanks,
Re: [PATCH RFC v2 01/18] xfs: fix the error unwind in xfs_open_devices()
In reply to Christian BraunerQuoted 14 lines · Christian Brauner
Since the rt and log block devices are closed in
xfs_free_buftarg()the buftarg owns the device file. The error unwind does not respect that:
when the log buftarg allocation fails,out_free_rtdev_targfrees the rt buftarg - releasingrtdev_file- and then falls through toout_close_rtdevand releases it a second time.The unwind also leaves
mp->m_rtdev_targpandmp->m_ddev_targppointing to the freed buftargs. The failed mount continues intodeactivate_locked_super()->xfs_kill_sb()->xfs_mount_free(), which frees them again.Clear the buftarg pointers once the unwind freed them and clear
rtdev_fileonce the rt buftarg owns it, so nothing is released twice.
Quoted 6 lines
Reachable when a buftarg allocation fails after the data buftarg was set up: an I/O error in
sync_blockdev()or an allocation failure inxfs_init_buftarg()while mounting with external rt and log devices.Fixes 41233576e9a4 ("xfs: close the RT and log block devices in xfs_free_buftarg")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good to me. As a small nit I'd probably do rtdev_file = NULL just after we've successfully allocated m_rtdev_targp but that's really minor. Feel free to add:
Honza
Quoted 24 lines
--- fs/xfs/xfs_super.c | 3 +++ 1 file changed, 3 insertions(+)fs/xfs/xfs_super.c +3
-0@@ -534,8 +534,11 @@ xfs_open_devices( out_free_rtdev_targ:if (mp->m_rtdev_targp)xfs_free_buftarg (mp->m_rtdev_targp); + mp->m_rtdev_targp =NULL ; + rtdev_file =NULL ;/* released by xfs_free_buftarg() */ out_free_ddev_targ:xfs_free_buftarg (mp->m_ddev_targp); + mp->m_ddev_targp =NULL ; out_close_rtdev:if (rtdev_file)bdev_fput (rtdev_file);Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC v2 05/18] ext4: use anonymous devices for KUnit test superblocks
In reply to Christian BraunerQuoted 10 lines · Christian Brauner
The mballoc and extents KUnit tests create superblocks through
sget_fc()with a set callback that never assignss_devand akill_sbthat only callsgeneric_shutdown_super().The upcoming global device-to-superblock table registers every superblock under its
s_dev, so each superblock needs a unique device number. Allocate a proper anonymous device viaset_anon_super_fc()and release it throughkill_anon_super().Signed-off-by Christian Brauner (Amutable) <[email protected]>
Ok. Feel free to add:
Honza
Quoted 75 lines
--- fs/ext4/extents-test.c | 9 ++------- fs/ext4/mballoc-test.c | 9 ++------- 2 files changed, 4 insertions(+), 14 deletions(-)fs/ext4/extents-test.c +2
-7@@ -126,11 +126,6 @@ struct kunit_ext_test_param {struct kunit_ext_data_state exp_data_state[3 ]; }; -static void ext_kill_sb (struct super_block *sb) -{ -generic_shutdown_super (sb); -} -static int ext_init_fs_context (struct fs_context *fc) {return 0 ;@@ -138,13 +133,13 @@ static int ext_init_fs_context(struct fs_context *fc)static int ext_set (struct super_block *sb,struct fs_context *fc) { -return 0 ; +return set_anon_super_fc (sb, fc); }static struct file_system_type ext_fs_type = { .name ="extents test" , .init_fs_context = ext_init_fs_context, - .kill_sb = ext_kill_sb, + .kill_sb = kill_anon_super, };static void extents_kunit_exit (struct kunit *test)fs/ext4/mballoc-test.c +2
-7@@ -59,11 +59,6 @@ static const struct super_operations mbt_sops = { .free_inode = mbt_free_inode, }; -static void mbt_kill_sb (struct super_block *sb) -{ -generic_shutdown_super (sb); -} -static int mbt_init_fs_context (struct fs_context *fc) {return 0 ;@@ -72,7 +67,7 @@ static int mbt_init_fs_context(struct fs_context *fc)static struct file_system_type mbt_fs_type = { .name ="mballoc test" , .init_fs_context = mbt_init_fs_context, - .kill_sb = mbt_kill_sb, + .kill_sb = kill_anon_super, };static int mbt_mb_init (struct super_block *sb)@@ -136,7 +131,7 @@ static void mbt_mb_release(struct super_block *sb)static int mbt_set (struct super_block *sb,struct fs_context *fc) { -return 0 ; +return set_anon_super_fc (sb, fc); }static struct super_block *mbt_ext4_alloc_super_block (void )Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC v2 02/18] super: convert s_count to refcount_t s_passive
In reply to Christian BraunerQuoted 6 lines · Christian Brauner
The superblock carries two counters:
s_active, the active reference count that keeps the filesystem usable, ands_count, the passive reference count that merely keeps the structure itself alive. Turn the passive count into arefcount_tand rename it tos_passiveto make the pairing withs_activeobvious.
Quoted 8 lines
Everything is still serialized by
sb_lock, so there is no functional change; the conversion buys the usualrefcount_tsaturation and underflow checking. The following patches start dropping passive references without holdingsb_lockand make the device-to-superblock table hold one passive reference per registered entry, which a plain integer cannot support.Signed-off-by Christian Brauner (Amutable) <[email protected]>
Yeah, looks like a reasonable cleanup. Feel free to add:
Honza
Quoted 107 lines
--- fs/super.c | 18 +++++++++--------- include/linux/fs/super_types.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-)fs/super.c +9
-9@@ -102,7 +102,7 @@ static bool super_flags(const struct super_block *sb, unsigned int flags)* creation will succeed and SB_BORN is set by vfs_get_tree() or we're * woken and we'll see SB_DYING. * -* The caller must have acquired a temporary reference on @sb->s_count. +* The caller must have acquired a temporary reference on @sb->s_passive. * * Return: The function returns true if SB_BORN was set and with * s_umount held. The function returns false if SB_DYING was @@ -367,7 +367,7 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags,spin_lock_init (&s->s_inode_wblist_lock);fserror_mount (s); - s->s_count =1 ; +refcount_set (&s->s_passive,1 );atomic_set (&s->s_active,1 );mutex_init (&s->s_vfs_rename_mutex);lockdep_set_class (&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);@@ -407,7 +407,7 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags,*/ static void __put_super (struct super_block *s) { -if (!--s->s_count) { +if (refcount_dec_and_test (&s->s_passive)) {list_del_init (&s->s_list);WARN_ON (s->s_dentry_lru.node);WARN_ON (s->s_inode_lru.node);@@ -529,7 +529,7 @@ static bool grab_super(struct super_block *sb) {bool locked; - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock); locked =super_lock_excl (sb);if (locked) {@@ -556,7 +556,7 @@ static bool grab_super(struct super_block *sb)* lock held in read mode in case of success. On successful return, * the caller must drop the s_umount lock when done. * -* Note that unlike get_super() et.al. this one does *not* bump ->s_count. +* Note that unlike get_super() et.al. this one does *not* bump ->s_passive. * The reason why it's safe is that we are OK with doing trylock instead * of down_read(). There's a couple of places that are OK with that, but * it's very much not a general-purpose interface. @@ -858,7 +858,7 @@ static void __iterate_supers(void (*f)(struct super_block *, void *), void *arg, sb = next_super(sb, flags)) {if (super_flags (sb,SB_DYING ))continue ; - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock);if (flags &SUPER_ITER_UNLOCKED ) {@@ -903,7 +903,7 @@ void iterate_supers_type(struct file_system_type *type,if (super_flags (sb,SB_DYING ))continue ; - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock); locked =super_lock_shared (sb);@@ -935,7 +935,7 @@ struct super_block *user_get_super(dev_t dev, bool excl)if (sb->s_dev != dev)continue ; - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock); locked =super_lock (sb, excl);@@ -1369,7 +1369,7 @@ static struct super_block *bdev_super_lock(struct block_device *bdev, bool excl)/* Make sure sb doesn't go away from under us */ spin_lock (&sb_lock); - sb->s_count++; +refcount_inc (&sb->s_passive);spin_unlock (&sb_lock);mutex_unlock (&bdev->bd_holder_lock);include/linux/fs/super_types.h +1
-1@@ -145,7 +145,7 @@ struct super_block {unsigned long s_magic;struct dentry *s_root;struct rw_semaphore s_umount; -int s_count; +refcount_t s_passive;atomic_t s_active;#ifdef CONFIG_SECURITY void *s_security;Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC v2 03/18] super: take lock after last reference count
In reply to Christian BraunerQuoted 6 lines · Christian Brauner
__put_super()required the caller to holdsb_lock, soput_super()wrapped it. The per-device superblock table introduced later drops its passive references from contexts that do not holdsb_lock, so makeput_super()self-locking: drop the count first and takesb_lockonly for the finallist_del.
Quoted 6 lines
With the count now dropped outside
sb_locka superblock can briefly sit on @super_blocks withs_passive== 0 before it is unlinked, so the list walkers (__iterate_supers(),iterate_supers_type(),user_get_super()) switch torefcount_inc_not_zero()and skip it.Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good, just one style nit below. Feel free to add:
fs/super.c static struct super_block *alloc_super(struct file_system_type *type, int flags, · line 412
Quoted 5 lines
fs/super.c +2
-1-static void __put_super (struct super_block *s) +void put_super (struct super_block *s) {if (refcount_dec_and_test (&s->s_passive)) { +
I'd delete this empty line.
fs/super.c static struct super_block *alloc_super(struct file_system_type *type, int flags, · line 416
Quoted 4 lines
fs/super.c +3
-0+spin_lock (&sb_lock);list_del_init (&s->s_list); +spin_unlock (&sb_lock); +
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerHi!
Quoted 15 lines · Christian Brauner
This is a generalization of the device number to superblock so it works for actual block device and anonymous (or even mtd) devices.
fs_holder_opsrecovers the affected superblock frombdev->bd_holder. That forces the holder of a block device to be exactly one superblock and makes it impossible for several superblocks to share a single device.erofs does exactly that. It can mount read-only "blob" devices that are shared between many superblocks: a metadata-only erofs that indexes a set of per-layer blobs (one filesystem instead of one per OCI layer), or an incremental image whose base device is shared by several updates. Because the block layer only tracks a single holder, a freeze, thaw, removal or sync on such a device is never propagated to all the superblocks using it, and the current infrastructure has no way to find them.
Quoted 7 lines
This series replaces the bd_holder-based lookup with a global, dev_t-keyed table mapping each block device to the superblock(s) using it. The holder argument becomes purely the block layer's exclusivity token -- a superblock, or the
file_system_typefor a device shared within one filesystem type -- and thefs_holder_opscallbacks look the device up in the table and act on every superblock registered for it: 1:1 for most filesystems, 1:many for erofs.
So I was thinking about this also in the light of Christoph's complaints. I agree with you, Chritian, that this translation table maintains the abstraction of the holder - holder ops define how to transition from bdev to its holder(s) and how to translate the .sync, .freeze and other operations for the holders - and that is kept since your changes are specific to fs_holder_ops.
What I'm wondering about a bit is whether we want this complexity for the only user which is erofs (i.e., whether this wouldn't be better implemented in erofs specific holder ops which could arguably be simpler than this generic solution). On the other hand that will likely have to replicate the locking dances we do in bdev_super_lock() and I'm not sure whether spread of this locking complexity into filesystems is better than this more complex VFS mapping code.
One more thing I was considering is that the need to transition from one bdev to multiple holders isn't actually unique to erofs. For example device mapper will need the same thing, arguably partition bdevs could be also made holders of the complete bdev so events are propagated from the whole bdev into partition bdevs properly (which currently happens in kind of ad hoc manner and only in some cases). Currently your translation mechanism is tied to mapping to superblock but actually rather weakly - we only need the guarantee that the holder stays alive while the mapping entry exists, the rest is protected by the mapping entry refcount AFAICS. So with a bit of effort we could make this a generic bdev -> holders mapping mechanism usable from whichever holder ops decide to employ it, which would then be quite attractive IMO.
But I guess let's leave lifting the mapping code from super.c and converting it into generic mapping mechanism for the moment when we really get into implementing another user.
All this is a long way of saying that I'm OK with the mapping mechanism like this :).
Honza
Quoted 111 lines
Filesystems claim and release their devices through new fs_bdev_file_open_by_{dev,path}() and
fs_bdev_file_release()helpers; the per-fs patches convert xfs, btrfs, ext4, f2fs and erofs over to them and fix cramfs and romfs, which released the registered main device with a rawbdev_fput().Since every superblock is registered under its
s_devthe table also replaces the last s_dev-keyed walk of thesuper_blockslist:user_get_super()resolves device numbers through it, soustat()andquotactl()now work on any device a filesystem claims and no longer takesb_lock.The longer-term motivation is to let userspace decide which devices may be onlined from one central place, without having to teach every filesystem about it individually.
Signed-off-by Christian Brauner (Amutable) <[email protected]>---
Changes in v2:
- super: rework the device-to-superblock table reference counting: each
(device, superblock) entry carries a single claim count and holds one
passive reference on its superblock for the entry's lifetime. New prep
patches converts_counttorefcount_ts_passiveand makeput_super()
self-locking.
- super: preallocate the entry inalloc_super()and register it from the
set callbacks through set_anon_super()/set_bdev_super(); an insert
failure unwinds exactly like a set callback failure. The superblock
stashes the entry insb->s_super_devandkill_super_notify()drops the
claim through it.
- super: initialize the table frommnt_init(); the rootfs and shm mounts
are created long before any initcall runs.
- super: fold the v1 "refuse to claim a frozen block device" patch into
the registration helper and restore the EBUSY check for the primary
device insetup_bdev_super(): additional devices (the xfs log, the ext4
journal, erofs blobs) are now refused while frozen as well, answering
Jan's question on v1 3/8.
- Split the core patch into table/helpers/switch-over and move the
xfs/btrfs/ext4 conversions before thefs_holder_opsswitch so no
freeze/mark_dead events are lost mid-series; erofs follows the switch.
- New prep patches: the ext4 KUnit tests allocate anonymous devices and
ocfs2 stops resettings_devon dismount.
- New: convertuser_get_super()to the device table, plus austat()
selftest.
- New: fix a pre-existing double release of the realtime device file and
dangling buftarg pointers in xfs_open_devices()'s error unwind.
- New: convert f2fs's additional devices to the helpers; fix cramfs and
romfs releasing the registered main device with a rawbdev_fput().
- erofs: drop the .shutdown() and .remove_bdev() implementations and the
per-device "dead" flag. Immutable filesystems don't need them: the block
layer setsGD_DEADbeforefs_bdev_mark_dead()so in-flight bios fail
anyway, erofs has no write path or journal to stop, and the read-only
loop_change_fd()case must not be forced to-EIO. Patch from Gao Xiang,
applied verbatim - thanks!
- btrfs: fix a general protection fault inclose_fs_devices()on a failed
mount (reported by syzbot). The release path took the superblock from
device->fs_info, which is still NULL ifopen_ctree()fails beforebtrfs_init_devices_late(); it now usesbdev_file->private_data.
- erofs: the v1 conversion was sent with a generic boilerplate changelog;
superseded by Gao's patch above.
- Collect Reviewed-by from Jan Kara and Tested-by from syzbot.
- Rebase onto v7.1-rc1.
- Link to v1: https://patch.msgid.link/20260602-work-super-bdev_holder_global-v1-0-bb0fd82f3861@kernel.org---
Christian Brauner (18):
xfs: fix the error unwind inxfs_open_devices()
super: converts_counttorefcount_ts_passive
super: take lock after last reference count
fs, block: moveblk_mode_tandfop_flags_tinto <linux/types.h>
ext4: use anonymous devices for KUnit test superblocks
ocfs2: don't resets_devon dismount
fs: maintain a global device-to-superblock table
fs: add dedicated block device open helpers for filesystems
xfs: port tofs_bdev_file_open_by_path()
btrfs: open via dedicated fs bdev helpers
ext4: open via dedicated fs bdev helpers
fs: look up superblocks via the device table infs_holder_ops
fs: tolerate per-superblock freeze errors on shared devices
erofs: open via dedicated fs bdev helpers
f2fs: open via dedicated fs bdev helpers
super: makefs_holder_opsprivate
fs: look up the superblock via the device table inuser_get_super()selftests/filesystems: addustat()coveragefs/btrfs/volumes.c | 31 +- fs/cramfs/inode.c | 2 +- fs/erofs/super.c | 35 +- fs/ext4/extents-test.c | 9 +- fs/ext4/mballoc-test.c | 9 +- fs/ext4/super.c | 12 +- fs/f2fs/super.c | 6 +- fs/internal.h | 1 + fs/namespace.c | 2 + fs/ocfs2/super.c | 1 - fs/romfs/super.c | 2 +- fs/super.c | 620 ++++++++++++++++------- fs/xfs/xfs_buf.c | 2 +- fs/xfs/xfs_super.c | 13 +- include/linux/blkdev.h | 9 - include/linux/fs.h | 2 - include/linux/fs/super.h | 8 + include/linux/fs/super_types.h | 4 +- include/linux/types.h | 2 + tools/testing/selftests/filesystems/.gitignore | 1 + tools/testing/selftests/filesystems/Makefile | 2 +- tools/testing/selftests/filesystems/ustat_test.c | 135 +++++ 22 files changed, 647 insertions(+), 261 deletions(-)---
base-commit:0c0d974f62e6603d4514e1a8035658edb353c68f
change-id: 20260602-work-super-bdev_holder_global-8cba5e52bed5
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC v2 07/18] fs: maintain a global device-to-superblock table
In reply to Christian BraunerQuoted 29 lines · Christian Brauner
fs_holder_opsrecovers the owning superblock frombdev->bd_holder, which forces the holder to be exactly one superblock and prevents several superblocks from sharing one block device. That's what erofs is doing.As a first step introduce a global dev_t-keyed rhltable mapping each device to the superblock(s) using it. The entry is preallocated in
alloc_super()and registered undersb->s_devby the set callback throughset_anon_super()andset_bdev_super(), the two helpers every set callback assignss_devthrough. Registration is the final fallible act of a set callback, so an insert failure unwinds through sget_fc()'s existing set-failure path: thefs_contextkeeps ownership ofs_fs_infoand the callers' error paths stay correct.set_anon_super()releases the anonymous dev it allocated when registration fails. Unwinding throughdeactivate_locked_super()instead would runkill_sb()and frees_fs_infobehind the caller's back: nfs and ceph free that object through a local pointer whensget_fc()fails and would double-free.The superblock stashes the entry in
sb->s_super_devandkill_super_notify()drops the claim through it, so teardown doesn't depend ons_devstaying stable; an entry that was never registered is freed together with the superblock indestroy_super_work().Each table entry holds a passive reference (
s_passive) on its superblock, so the struct stays valid for as long as the entry is reachable. Entries are claim-counted throughsd_ref: additional claims on the same (device, superblock) pair share the entry, and the unlink is deferred to the last put, so a later iteration cursor never resumes from a removed node.
Quoted 8 lines
The table is initialized from
mnt_init(): the first superblocks (the tmpfs shm mount and rootfs) are created fromstart_kernel()long before any initcall runs, so an initcall would be too late.The table has no readers yet; the
fs_holder_opscallbacks are switched over once all devices a filesystem claims are registered.Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 237 lines
--- fs/internal.h | 1 + fs/namespace.c | 2 + fs/super.c | 102 ++++++++++++++++++++++++++++++++++++++++- include/linux/fs/super_types.h | 2 + 4 files changed, 105 insertions(+), 2 deletions(-)fs/internal.h +1
-0@@ -137,6 +137,7 @@ extern int reconfigure_super(struct fs_context *);extern bool super_trylock_shared (struct super_block *sb);struct super_block *user_get_super (dev_t ,bool excl);void put_super (struct super_block *sb); +void __init super_dev_init (void );extern bool mount_capable (struct fs_context *);int sb_init_dio_done_wq (struct super_block *sb);fs/namespace.c +2
-0@@ -6262,6 +6262,8 @@ void __init mnt_init(void)if (!mount_hashtable || !mountpoint_hashtable)panic ("Failed to allocate mount hash table\n" ); +super_dev_init (); +kernfs_init (); err =sysfs_init ();fs/super.c +100
-2@@ -24,6 +24,7 @@#include <linux/export.h> #include <linux/slab.h> #include <linux/blkdev.h> +#include <linux/rhashtable.h> #include <linux/mount.h> #include <linux/security.h> #include <linux/writeback.h> /* for the emergency remount stuff */ @@ -272,6 +273,8 @@ static unsigned long super_cache_count(struct shrinker *shrink,return total_objects; } +static struct super_dev *super_dev_alloc (dev_t dev,struct super_block *sb); +static void destroy_super_work (struct work_struct *work) { struct super_block *s = container_of(work, struct super_block,@@ -279,6 +282,8 @@ static void destroy_super_work(struct work_struct *work)fsnotify_sb_free (s);security_sb_free (s);put_user_ns (s->s_user_ns); +/* Only an unregistered entry is still owned by the superblock. */ +kfree (s->s_super_dev);kfree (s->s_subtype);for (int i =0 ; i <SB_FREEZE_LEVELS ; i++)percpu_free_rwsem (&s->s_writers.rw_sem[i]);@@ -392,6 +397,10 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags, goto fail;if (list_lru_init_memcg (&s->s_inode_lru, s->s_shrink)) goto fail; + s->s_super_dev =super_dev_alloc (0 , s); +if (!s->s_super_dev) + goto fail; + s->s_min_writeback_pages =MIN_WRITEBACK_PAGES ;return s;@@ -421,6 +430,77 @@ void put_super(struct super_block *s) } } +struct super_dev { +dev_t sd_dev; +struct super_block *sd_sb; +refcount_t sd_ref; +struct rhlist_head sd_node; +struct rcu_head sd_rcu; +}; + +static struct rhltable super_dev_table; +static const struct rhashtable_params super_dev_params = { + .key_len =sizeof (dev_t), + .key_offset = offsetof(struct super_dev , sd_dev), + .head_offset = offsetof(struct super_dev , sd_node), +}; + +static struct super_dev *super_dev_alloc (dev_t dev,struct super_block *sb) +{ +struct super_dev *fsd; + + fsd =kzalloc_obj (*fsd); +if (!fsd) +return NULL ; + fsd->sd_dev = dev; + fsd->sd_sb = sb; +refcount_set (&fsd->sd_ref,1 ); +return fsd; +} + +static void super_dev_put (struct super_dev *fsd) +{ +/* Unlink only once unpinned, so a cursor never resumes from a removed node. */ +if (fsd &&refcount_dec_and_test (&fsd->sd_ref)) { +rhltable_remove (&super_dev_table, &fsd->sd_node, super_dev_params); +put_super (fsd->sd_sb); +kfree_rcu (fsd, sd_rcu); + } +} + +void __init super_dev_init (void ) +{ +if (rhltable_init (&super_dev_table, &super_dev_params)) +panic ("VFS: Cannot initialise super_dev_table\n" ); +} + +static int super_dev_insert (struct super_dev *fsd) +{ +int err; + + err =rhltable_insert (&super_dev_table, &fsd->sd_node, super_dev_params); +if (!err) +refcount_inc (&fsd->sd_sb->s_passive); +return err; +} + +/* Register @sb under @sb->s_dev as the final fallible act of a set callback. */ +static int super_dev_register (struct super_block *sb) +{ +struct super_dev *fsd = sb->s_super_dev; +int err; + +lockdep_assert_held (&sb_lock); +VFS_WARN_ON_ONCE (!sb->s_dev); +VFS_WARN_ON_ONCE (!fsd || fsd->sd_dev); + + fsd->sd_dev = sb->s_dev; + err =super_dev_insert (fsd); +if (err) + fsd->sd_dev =0 ; +return err; +} +static void kill_super_notify (struct super_block *sb) {lockdep_assert_not_held (&sb->s_umount);@@ -440,6 +520,12 @@ static void kill_super_notify(struct super_block *sb)hlist_del_init (&sb->s_instances);spin_unlock (&sb_lock); +/* Drop sget_fc()'s claim; a never-registered entry stays with the sb. */ +if (sb->s_super_dev->sd_dev) { +super_dev_put (sb->s_super_dev); + sb->s_super_dev =NULL ; + } +/* * Let concurrent mounts know that this thing is really dead. * We don't need @sb->s_umount here as every concurrent caller @@ -750,6 +836,7 @@ struct super_block *sget_fc(struct fs_context *fc, }if (!s) {spin_unlock (&sb_lock); + s =alloc_super (fc->fs_type, fc->sb_flags, user_ns);if (!s)return ERR_PTR (-ENOMEM );@@ -759,11 +846,13 @@ struct super_block *sget_fc(struct fs_context *fc, s->s_fs_info = fc->s_fs_info; err =set (s, fc);if (err) { +VFS_WARN_ON_ONCE (s->s_super_dev->sd_dev); s->s_fs_info =NULL ;spin_unlock (&sb_lock);destroy_unused_super (s);return ERR_PTR (err); } +VFS_WARN_ON_ONCE (!s->s_super_dev->sd_dev); fc->s_fs_info =NULL ; s->s_type = fc->fs_type; s->s_iflags |= fc->s_iflags;@@ -1217,7 +1306,16 @@ EXPORT_SYMBOL(free_anon_bdev);int set_anon_super (struct super_block *s,void *data) { -return get_anon_bdev (&s->s_dev); +int error; + + error =get_anon_bdev (&s->s_dev); +if (error) +return error; + + error =super_dev_register (s); +if (error) +free_anon_bdev (s->s_dev); +return error; }EXPORT_SYMBOL (set_anon_super);@@ -1303,7 +1401,7 @@ EXPORT_SYMBOL(get_tree_keyed);static int set_bdev_super (struct super_block *s,void *data) { s->s_dev = *(dev_t *)data; -return 0 ; +return super_dev_register (s); }static int super_s_dev_set (struct super_block *s,struct fs_context *fc)include/linux/fs/super_types.h +2
-0@@ -30,6 +30,7 @@ struct mount;struct mtd_info ;struct quotactl_ops ;struct shrinker ; +struct super_dev ;struct unicode_map ;struct user_namespace ;struct workqueue_struct ;@@ -132,6 +133,7 @@ struct super_operations {struct super_block {struct list_head s_list;/* Keep this first */ dev_t s_dev;/* search index; _not_ kdev_t */ +struct super_dev *s_super_dev;/* sget_fc()'s device table claim */ unsigned char s_blocksize_bits;unsigned long s_blocksize;loff_t s_maxbytes;/* Max file size */ Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerRe: [PATCH RFC v2 08/18] fs: add dedicated block device open helpers for filesystems
In reply to Christian BraunerQuoted 42 lines · Christian Brauner
Add fs_bdev_file_open_by_{dev,path}() and
fs_bdev_file_release(). They open the device withfs_holder_opsand register a claim in the device-to-superblock table. Claims on the same (device, superblock) pair share one entry, so when a filesystem claims a device it already uses (xfs with its log on the data device), no second entry is added and each superblock will be acted on once.The holder argument remains purely the block layer's exclusivity token:
a superblock, or afile_system_typefor a device shared by several superblocks of that type. The shared case only becomes usable once thefs_holder_opscallbacks resolve superblocks through the table instead ofbdev->bd_holder.Convert the main device,
setup_bdev_super()andkill_block_super(),
over: the open finds the entry registered bysget_fc()and claims it again. cramfs and romfs bypasskill_block_super()so they can handle MTD mounts and release the main device with a plainbdev_fput(), which would leave the claim behind: the (dev, sb) entry would never be unregistered and the passive reference it holds would keep the superblock alive forever. Convert their release paths in the same step.The frozen-device check stays in
setup_bdev_super()for the primary device and is added tofs_bdev_register()for new claims, i.e. every additional device a filesystem opens through the helpers. Only a (device, superblock) pair the superblock claimed earlier may be reopened while frozen (xfs with its log on the data device): the freeze already covers that superblock through the existing claim, so nothing escapes it. Without thesetup_bdev_super()check a device frozen before the mount even started (dmlock_fs, loop) could be mounted and written to (journal replay) under an active freeze, because the primary open reuses the entry registered bysget_fc()and never takes the new-claim path.Both checks read
bd_fsfreeze_countonly after the entry is published (bysget_fc()for the primary, byfs_bdev_register()for new claims) and pair withbdev_freeze()incrementing the count before walking the
table: either the mount sees the elevated freeze count and fails with EBUSY, or the freeze finds the published entry and converges onceSB_BORNis set.Signed-off-by Christian Brauner (Amutable) <[email protected]>
...
fs/super.c const struct blk_holder_ops fs_holder_ops = { · line 1656
Quoted 3 lines
fs/super.c +3
-0+static int fs_bdev_register (struct file *bdev_file,struct super_block *sb) +{ + struct super_dev *sb_dev __free(kfree) = NULL;
Frankly I find the use of __free on sb_dev more confusing than helping in this function. If you didn't use it, you could remove the somewhat confusing retain_and_null_ptr() calls below, remove this initialization and just put one kfree() into the error handling branch when super_dev_insert() fails...
Quoted 29 lines
fs/super.c +29
-0+dev_t dev =file_bdev (bdev_file)->bd_dev; +int err; + +scoped_guard (rcu) { + sb_dev =super_dev_lookup (dev, sb); +if (sb_dev &&refcount_inc_not_zero (&sb_dev->sd_ref)) { +retain_and_null_ptr (sb_dev); +return 0 ; + } + } + + sb_dev =super_dev_alloc (dev, sb); +if (!sb_dev) +return -ENOMEM ; + + err =super_dev_insert (sb_dev); +if (err) +return err; + +/* Publish the entry before reading the count; pairs with bdev_freeze(). */ +smp_mb (); +if (atomic_read (&file_bdev (bdev_file)->bd_fsfreeze_count) >0 ) { + err = -EBUSY ; +super_dev_put (sb_dev); + } + +retain_and_null_ptr (sb_dev); +return err; +}
...
Quoted 10 lines
fs/super.c +10
-0+/** +* fs_bdev_file_release - release a block device claimed for a superblock +* @bdev_file: file returned by fs_bdev_file_open_by_{dev,path}() +* @sb: superblock the device was claimed for +* +* Drop one claim on the {dev, @sb} entry; the last claim unregisters it (a +* pinning cursor defers the actual unlink). Then close the block device. +*/ +void fs_bdev_file_release (struct file *bdev_file,struct super_block *sb) +{
fs/super.c const struct blk_holder_ops fs_holder_ops = { · line 1773
Quoted 10 lines
fs/super.c +10
-0+dev_t dev =file_bdev (bdev_file)->bd_dev; +struct super_dev *sb_dev; + +rcu_read_lock (); + sb_dev =super_dev_lookup (dev, sb); +rcu_read_unlock (); +super_dev_put (sb_dev); +bdev_fput (bdev_file); +} +EXPORT_SYMBOL_GPL (fs_bdev_file_release);
Why don't you use sb->s_super_dev in this function?
Otherwise the patch looks good to me.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC v2 08/18] fs: add dedicated block device open helpers for filesystems
Quoted 43 lines · Jan Kara
Quoted 42 lines · Christian Brauner
Add fs_bdev_file_open_by_{dev,path}() and
fs_bdev_file_release(). They open the device withfs_holder_opsand register a claim in the device-to-superblock table. Claims on the same (device, superblock) pair share one entry, so when a filesystem claims a device it already uses (xfs with its log on the data device), no second entry is added and each superblock will be acted on once.The holder argument remains purely the block layer's exclusivity token:
a superblock, or afile_system_typefor a device shared by several superblocks of that type. The shared case only becomes usable once thefs_holder_opscallbacks resolve superblocks through the table instead ofbdev->bd_holder.Convert the main device,
setup_bdev_super()andkill_block_super(),
over: the open finds the entry registered bysget_fc()and claims it again. cramfs and romfs bypasskill_block_super()so they can handle MTD mounts and release the main device with a plainbdev_fput(), which would leave the claim behind: the (dev, sb) entry would never be unregistered and the passive reference it holds would keep the superblock alive forever. Convert their release paths in the same step.The frozen-device check stays in
setup_bdev_super()for the primary device and is added tofs_bdev_register()for new claims, i.e. every additional device a filesystem opens through the helpers. Only a (device, superblock) pair the superblock claimed earlier may be reopened while frozen (xfs with its log on the data device): the freeze already covers that superblock through the existing claim, so nothing escapes it. Without thesetup_bdev_super()check a device frozen before the mount even started (dmlock_fs, loop) could be mounted and written to (journal replay) under an active freeze, because the primary open reuses the entry registered bysget_fc()and never takes the new-claim path.Both checks read
bd_fsfreeze_countonly after the entry is published (bysget_fc()for the primary, byfs_bdev_register()for new claims) and pair withbdev_freeze()incrementing the count before walking the
table: either the mount sees the elevated freeze count and fails with EBUSY, or the freeze finds the published entry and converges onceSB_BORNis set.Signed-off-by Christian Brauner (Amutable) <[email protected]>
...
Quoted 14 lines
Quoted 14 lines
fs/super.c +14
-0+/** +* fs_bdev_file_release - release a block device claimed for a superblock +* @bdev_file: file returned by fs_bdev_file_open_by_{dev,path}() +* @sb: superblock the device was claimed for +* +* Drop one claim on the {dev, @sb} entry; the last claim unregisters it (a +* pinning cursor defers the actual unlink). Then close the block device. +*/ +void fs_bdev_file_release (struct file *bdev_file,struct super_block *sb) +{ +dev_t dev =file_bdev (bdev_file)->bd_dev; +struct super_dev *sb_dev; + +rcu_read_lock ();
Quoted 8 lines
fs/super.c const struct blk_holder_ops fs_holder_ops = { · line 1773
Quoted 6 lines
fs/super.c +6
-0+ sb_dev =super_dev_lookup (dev, sb); +rcu_read_unlock (); +super_dev_put (sb_dev); +bdev_fput (bdev_file); +} +EXPORT_SYMBOL_GPL (fs_bdev_file_release);Why don't you use
sb->s_super_devin this function?
Nevermind, I've realized sb can hold multiple bdevs so this is really needed.
I'd still prefer the __free handling in fs_bdev_register() sorted out but regardless feel free to add:
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC v2 01/18] xfs: fix the error unwind in xfs_open_devices()
In reply to Christian BraunerQuoted 11 lines · Christian Brauner
Since the rt and log block devices are closed in
xfs_free_buftarg()the buftarg owns the device file. The error unwind does not respect that:
when the log buftarg allocation fails,out_free_rtdev_targfrees the rt buftarg - releasingrtdev_file- and then falls through toout_close_rtdevand releases it a second time.The unwind also leaves
mp->m_rtdev_targpandmp->m_ddev_targppointing to the freed buftargs. The failed mount continues intodeactivate_locked_super()->xfs_kill_sb()->xfs_mount_free(), which frees them again.
Quoted 6 lines
Clear the buftarg pointers once the unwind freed them and clear
rtdev_fileonce the rt buftarg owns it, so nothing is released twice.Reachable when a buftarg allocation fails after the data buftarg was set up: an I/O error in
sync_blockdev()or an allocation failure inxfs_init_buftarg()while mounting with external rt and log devices.
Looks good:
I actually have a major rework of this area pending, but it probably won't land for 7.2, so we might as well get this local fix in ASAP.
Re: [PATCH RFC v2 07/18] fs: maintain a global device-to-superblock table
In reply to Christian BraunerFYI, I still think maintaining a dev_t to sb mapping vs having a holder register a claim is a major step backwards architecturally. I spent a lot of effort to get us out of this.
Re: [PATCH RFC v2 08/18] fs: add dedicated block device open helpers for filesystems
In reply to Jan KaraQuoted 9 lines · Jan Kara
fs/super.c const struct blk_holder_ops fs_holder_ops = { · line 1656
Quoted 3 lines
fs/super.c +3
-0+static int fs_bdev_register (struct file *bdev_file,struct super_block *sb) +{ + struct super_dev *sb_dev __free(kfree) = NULL;Frankly I find the use of
__freeonsb_devmore confusing than helping in this function. If you didn't use it, you could remove the somewhat confusingretain_and_null_ptr()calls below, remove this initialization and just put onekfree()into the error handling branch whensuper_dev_insert()fails...
It is. __free is really annoying for anything but trivial local scope only variables.
Re: [PATCH RFC v2 17/18] fs: look up the superblock via the device table in user_get_super()
In reply to Christian BraunerQuoted 5 lines · Christian Brauner
user_get_super()still finds the superblock for a device number by walking the globalsuper_blockslist undersb_lock. Every superblock is registered in the device table under itss_devsincesget_fc()inserts it there, including superblocks on anonymous devices, so use the table instead.
So what is the benefit of this? It's not like any of these are heavily used fast paths.
Re: [PATCH RFC v2 17/18] fs: look up the superblock via the device table in user_get_super()
In reply to Christian BraunerQuoted 18 lines · Christian Brauner
user_get_super()still finds the superblock for a device number by walking the globalsuper_blockslist undersb_lock. Every superblock is registered in the device table under itss_devsincesget_fc()inserts it there, including superblocks on anonymous devices, so use the table instead.The refcount-pinning cursor helpers super_dev_{get,first,next}() only touch table state and do not depend on
CONFIG_BLOCK, so drop theCONFIG_BLOCKguard around them: their new caller serves anonymous devices as well (ustat()on e.g. tmpfs) and is built withoutCONFIG_BLOCK. The guard falls in this patch rather than separately since without this caller the helpers would be unused withoutCONFIG_BLOCK.The pinned entry holds a passive reference on the superblock so
super_lock()can be called directly; once the superblock is locked grab a passive reference for the caller before dropping the pin.
Quoted 10 lines
The device table contains more than the old walk could find: a superblock is also registered for every additional device it claims (the xfs log and realtime devices, btrfs member devices, the ext4 external journal, erofs blob devices). Don't filter those out:
specifying any device a filesystem uses now resolves to that filesystem, soustat()andquotactl()work on e.g. the xfs log device or a btrfs member device (the latter used to fail outright as btrfs superblocks carry an anonymouss_devthat never matches a member device). When several superblocks share a device (erofs blob devices) the first live superblock wins.
Does erofs have a means to find the other superblocks that share a device given a notification coming in on one of them? As hch says, it feels weird to have a lookup mechanism when there's also an upcall mechanism.
<shrug> I've been on vacation for a while so maybe I missed that there's another use for the bdev->sb lookup? There are 1600 more emails for me to go through... :P
--D
Quoted 79 lines
The cursor also keeps scanning past dying superblocks where the old walk gave up after the first
s_devmatch, so a mount racing with the unmount of the same device (or with the reuse of a recycled anonymousdev_t) finds the live superblock where the old walk could spuriously return NULL.This removes the last s_dev-keyed walk of the
super_blockslist and takesustat()and quotactl()'s block device lookup offsb_lockentirely.Signed-off-by Christian Brauner (Amutable) <[email protected]>--- fs/super.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-)fs/super.c +8
-20@@ -501,7 +501,6 @@ static int super_dev_register(struct super_block *sb)return err; } -#ifdef CONFIG_BLOCK static struct super_dev *super_dev_get (struct rhlist_head *pos) {struct super_dev *sb_dev;@@ -535,7 +534,6 @@ static struct super_dev *super_dev_next(struct super_dev *prev)super_dev_put (prev);return sb_dev; } -#endif static void kill_super_notify (struct super_block *sb) {@@ -1044,29 +1042,19 @@ EXPORT_SYMBOL(iterate_supers_type);struct super_block *user_get_super (dev_t dev,bool excl) { -struct super_block *sb; - -spin_lock (&sb_lock); -list_for_each_entry (sb, &super_blocks, s_list) { -bool locked; +struct super_dev *sb_dev; -if (sb->s_dev != dev) -continue ; +for (sb_dev =super_dev_first (dev); sb_dev; sb_dev =super_dev_next (sb_dev)) { +struct super_block *sb = sb_dev->sd_sb; -if (!refcount_inc_not_zero (&sb->s_passive)) +if (!super_lock (sb, excl))continue ; -spin_unlock (&sb_lock); - - locked =super_lock (sb, excl); -if (locked) -return sb; - -put_super (sb); -spin_lock (&sb_lock); -break ; +/* The pinned entry holds a passive reference, take our own. */ +refcount_inc (&sb->s_passive); +super_dev_put (sb_dev); +return sb; } -spin_unlock (&sb_lock);return NULL ; }Signature · 2 lines
-- 2.47.3
Re: [PATCH RFC v2 17/18] fs: look up the superblock via the device table in user_get_super()
Quoted 30 lines · Darrick J. Wong
Quoted 18 lines · Christian Brauner
user_get_super()still finds the superblock for a device number by walking the globalsuper_blockslist undersb_lock. Every superblock is registered in the device table under itss_devsincesget_fc()inserts it there, including superblocks on anonymous devices, so use the table instead.The refcount-pinning cursor helpers super_dev_{get,first,next}() only touch table state and do not depend on
CONFIG_BLOCK, so drop theCONFIG_BLOCKguard around them: their new caller serves anonymous devices as well (ustat()on e.g. tmpfs) and is built withoutCONFIG_BLOCK. The guard falls in this patch rather than separately since without this caller the helpers would be unused withoutCONFIG_BLOCK.The pinned entry holds a passive reference on the superblock so
super_lock()can be called directly; once the superblock is locked grab a passive reference for the caller before dropping the pin.Quoted 10 lines
The device table contains more than the old walk could find: a superblock is also registered for every additional device it claims (the xfs log and realtime devices, btrfs member devices, the ext4 external journal, erofs blob devices). Don't filter those out:
specifying any device a filesystem uses now resolves to that filesystem, soustat()andquotactl()work on e.g. the xfs log device or a btrfs member device (the latter used to fail outright as btrfs superblocks carry an anonymouss_devthat never matches a member device). When several superblocks share a device (erofs blob devices) the first live superblock wins.
Quoted 2 lines
Does erofs have a means to find the other superblocks that share a device given a notification coming in on one of them?
Nope, erofs currently doesn't have a way to find the other superblocks (it doesn't maintain the relationship). My previous thought is that because it's a read-only filesystem, IMHO, there is not a must to implement shutdown or notification mechanism in erofs itself, just because it's strictly immutable (no local write or dirty journals), and block layer can return io error on dead bdevs directly even it's a shared block dev. But I may be wrong if there are reason that we should maintain the relationship.
Currently it only uses sb->s_type as the holder for bdev sharing, I think Christian meant that.
Thanks,
Gao Xiang
Re: [PATCH RFC v2 08/18] fs: add dedicated block device open helpers for filesystems
In reply to Christian BraunerQuoted 34 lines · Christian Brauner
Add fs_bdev_file_open_by_{dev,path}() and
fs_bdev_file_release(). They open the device withfs_holder_opsand register a claim in the device-to-superblock table. Claims on the same (device, superblock) pair share one entry, so when a filesystem claims a device it already uses (xfs with its log on the data device), no second entry is added and each superblock will be acted on once.The holder argument remains purely the block layer's exclusivity token:
a superblock, or afile_system_typefor a device shared by several superblocks of that type. The shared case only becomes usable once thefs_holder_opscallbacks resolve superblocks through the table instead ofbdev->bd_holder.Convert the main device,
setup_bdev_super()andkill_block_super(),
over: the open finds the entry registered bysget_fc()and claims it again. cramfs and romfs bypasskill_block_super()so they can handle MTD mounts and release the main device with a plainbdev_fput(), which would leave the claim behind: the (dev, sb) entry would never be unregistered and the passive reference it holds would keep the superblock alive forever. Convert their release paths in the same step.The frozen-device check stays in
setup_bdev_super()for the primary device and is added tofs_bdev_register()for new claims, i.e. every additional device a filesystem opens through the helpers. Only a (device, superblock) pair the superblock claimed earlier may be reopened while frozen (xfs with its log on the data device): the freeze already covers that superblock through the existing claim, so nothing escapes it. Without thesetup_bdev_super()check a device frozen before the mount even started (dmlock_fs, loop) could be mounted and written to (journal replay) under an active freeze, because the primary open reuses the entry registered bysget_fc()and never takes the new-claim path.
Quoted 6 lines
Both checks read
bd_fsfreeze_countonly after the entry is published (bysget_fc()for the primary, byfs_bdev_register()for new claims) and pair withbdev_freeze()incrementing the count before walking the
table: either the mount sees the elevated freeze count and fails with EBUSY, or the freeze finds the published entry and converges onceSB_BORNis set.
Hmm. I /think/ this might be causing regressions in xfs/006, generic/311, and xfs/264 on XFS realtime filesystems. Each test fails with a mount failure due to a busy device:
--- /run/fstests/bin/tests/xfs/006.out 2025-07-15 14:41:40.170403463 -0700
+++ /run/fstests/logs/xfs/006.out.bad 2026-08-31 12:25:01.790239078 -0700
@@ -6,3 +6,7 @@ error/metadata/EIO/max_retries=-1
error/metadata/EIO/retry_timeout_seconds=-1 error/metadata/ENOSPC/max_retries=-1 error/metadata/ENOSPC/retry_timeout_seconds=-1
+mount: /mnt/s: /dev/mapper/error-test.006 already mounted or mount point busy.
+ dmesg(1) may have more information after failed mount system call.
+umount.nfs: remote share not in 'host:dir' format
+umount.nfs: /mnt/s: not mounted
I'm not sure what's going on here, but dmesg has this to say:
XFS (dm-0): Invalid device [/dev/mapper/error-rttest.006], error=-16
So my guess is that the SCRATCH_RT volume isn't getting unfrozen properly?
--D
Quoted 250 lines
Signed-off-by Christian Brauner (Amutable) <[email protected]>--- fs/cramfs/inode.c | 2 +- fs/romfs/super.c | 2 +- fs/super.c | 154 ++++++++++++++++++++++++++++++++++++++++++++--- include/linux/fs/super.h | 7 +++ 4 files changed, 155 insertions(+), 10 deletions(-)fs/cramfs/inode.c +1
-1@@ -504,7 +504,7 @@ static void cramfs_kill_sb(struct super_block *sb) sb->s_mtd =NULL ; }else if (IS_ENABLED (CONFIG_CRAMFS_BLOCKDEV ) && sb->s_bdev) {sync_blockdev (sb->s_bdev); -bdev_fput (sb->s_bdev_file); +fs_bdev_file_release (sb->s_bdev_file, sb); }kfree (sbi); }fs/romfs/super.c +1
-1@@ -587,7 +587,7 @@ static void romfs_kill_sb(struct super_block *sb)#ifdef CONFIG_ROMFS_ON_BLOCK if (sb->s_bdev) {sync_blockdev (sb->s_bdev); -bdev_fput (sb->s_bdev_file); +fs_bdev_file_release (sb->s_bdev_file, sb); }#endif }fs/super.c +146
-8@@ -1633,6 +1633,145 @@ const struct blk_holder_ops fs_holder_ops = { };EXPORT_SYMBOL_GPL (fs_holder_ops); +static struct super_dev *super_dev_lookup (dev_t dev,struct super_block *sb) +{ +struct super_dev *it; +struct rhlist_head *list, *pos; + +RCU_LOCKDEP_WARN (!rcu_read_lock_held (),"suspicious super_dev_lookup() usage" ); +VFS_WARN_ON_ONCE (!dev); +VFS_WARN_ON_ONCE (!sb); + + list =rhltable_lookup (&super_dev_table, &dev, super_dev_params); +rhl_for_each_entry_rcu (it, pos, list, sd_node) { +if (it->sd_sb == sb) +return it; + } + +return NULL ; +} + +static int fs_bdev_register (struct file *bdev_file,struct super_block *sb) +{ + struct super_dev *sb_dev __free(kfree) = NULL; +dev_t dev =file_bdev (bdev_file)->bd_dev; +int err; + +scoped_guard (rcu) { + sb_dev =super_dev_lookup (dev, sb); +if (sb_dev &&refcount_inc_not_zero (&sb_dev->sd_ref)) { +retain_and_null_ptr (sb_dev); +return 0 ; + } + } + + sb_dev =super_dev_alloc (dev, sb); +if (!sb_dev) +return -ENOMEM ; + + err =super_dev_insert (sb_dev); +if (err) +return err; + +/* Publish the entry before reading the count; pairs with bdev_freeze(). */ +smp_mb (); +if (atomic_read (&file_bdev (bdev_file)->bd_fsfreeze_count) >0 ) { + err = -EBUSY ; +super_dev_put (sb_dev); + } + +retain_and_null_ptr (sb_dev); +return err; +} + +/** +* fs_bdev_file_open_by_dev - claim a block device on behalf of a superblock +* @dev: block device number +* @mode: open mode +* @holder: block-layer exclusivity token (a superblock, or the file_system_type +* when the device may be shared by several superblocks of that type) +* @sb: superblock to drive fs_holder_ops events for +* +* Open @dev with &fs_holder_ops and register that @sb uses it, so device +* removal/sync/freeze/thaw are propagated to @sb (and any other superblock +* sharing @dev). Must be paired with fs_bdev_file_release(). +* +* Return: an opened block-device file or an ERR_PTR(). +*/ +struct file *fs_bdev_file_open_by_dev (dev_t dev,blk_mode_t mode,void *holder, +struct super_block *sb) +{ +struct file *bdev_file; +int err; + + bdev_file =bdev_file_open_by_dev (dev, mode, holder, &fs_holder_ops); +if (IS_ERR (bdev_file)) +return bdev_file; + + err =fs_bdev_register (bdev_file, sb); +if (err) { +bdev_fput (bdev_file); +return ERR_PTR (err); + } +return bdev_file; +} +EXPORT_SYMBOL_GPL (fs_bdev_file_open_by_dev); + +/** +* fs_bdev_file_open_by_path - claim a block device on behalf of a superblock +* @path: path to the block device +* @mode: open mode +* @holder: block-layer exclusivity token (a superblock, or the file_system_type +* when the device may be shared by several superblocks of that type) +* @sb: superblock to drive fs_holder_ops events for +* +* Open the block device at @path with &fs_holder_ops and register that @sb +* uses it, so device removal/sync/freeze/thaw are propagated to @sb (and any +* other superblock sharing the device). Must be paired with +* fs_bdev_file_release(). +* +* Return: an opened block-device file or an ERR_PTR(). +*/ +struct file *fs_bdev_file_open_by_path (const char *path,blk_mode_t mode, +void *holder,struct super_block *sb) +{ +struct file *bdev_file; +int err; + + bdev_file =bdev_file_open_by_path (path, mode, holder, &fs_holder_ops); +if (IS_ERR (bdev_file)) +return bdev_file; + + err =fs_bdev_register (bdev_file, sb); +if (err) { +bdev_fput (bdev_file); +return ERR_PTR (err); + } +return bdev_file; +} +EXPORT_SYMBOL_GPL (fs_bdev_file_open_by_path); + +/** +* fs_bdev_file_release - release a block device claimed for a superblock +* @bdev_file: file returned by fs_bdev_file_open_by_{dev,path}() +* @sb: superblock the device was claimed for +* +* Drop one claim on the {dev, @sb} entry; the last claim unregisters it (a +* pinning cursor defers the actual unlink). Then close the block device. +*/ +void fs_bdev_file_release (struct file *bdev_file,struct super_block *sb) +{ +dev_t dev =file_bdev (bdev_file)->bd_dev; +struct super_dev *sb_dev; + +rcu_read_lock (); + sb_dev =super_dev_lookup (dev, sb); +rcu_read_unlock (); +super_dev_put (sb_dev); +bdev_fput (bdev_file); +} +EXPORT_SYMBOL_GPL (fs_bdev_file_release); +int setup_bdev_super (struct super_block *sb,int sb_flags,struct fs_context *fc) {@@ -1640,7 +1779,7 @@ int setup_bdev_super(struct super_block *sb, int sb_flags,struct file *bdev_file;struct block_device *bdev; - bdev_file =bdev_file_open_by_dev (sb->s_dev, mode, sb, &fs_holder_ops); + bdev_file =fs_bdev_file_open_by_dev (sb->s_dev, mode, sb, sb);if (IS_ERR (bdev_file)) {if (fc)errorf (fc,"%s: Can't open blockdev" , fc->source);@@ -1654,20 +1793,19 @@ int setup_bdev_super(struct super_block *sb, int sb_flags,* writable from userspace even for a read-only block device. */ if ((mode &BLK_OPEN_WRITE ) &&bdev_read_only (bdev)) { -bdev_fput (bdev_file); +fs_bdev_file_release (bdev_file, sb);return -EACCES ; } -/* -* It is enough to check bdev was not frozen before we set -* s_bdev as freezing will wait until SB_BORN is set. -*/ +/* The sget_fc() entry is already published; pairs with bdev_freeze(). */ +smp_mb ();if (atomic_read (&bdev->bd_fsfreeze_count) >0 ) {if (fc)warnf (fc,"%pg: Can't mount, blockdev is frozen" , bdev); -bdev_fput (bdev_file); +fs_bdev_file_release (bdev_file, sb);return -EBUSY ; } +spin_lock (&sb_lock); sb->s_bdev_file = bdev_file; sb->s_bdev = bdev;@@ -1756,7 +1894,7 @@ void kill_block_super(struct super_block *sb)generic_shutdown_super (sb);if (bdev) {sync_blockdev (bdev); -bdev_fput (sb->s_bdev_file); +fs_bdev_file_release (sb->s_bdev_file, sb); } }include/linux/fs/super.h +7
-0@@ -235,4 +235,11 @@ int freeze_super(struct super_block *super, enum freeze_holder who,int thaw_super (struct super_block *super,enum freeze_holder who,const void *freeze_owner); +struct file ; +struct file *fs_bdev_file_open_by_dev (dev_t dev,blk_mode_t mode,void *holder, +struct super_block *sb); +struct file *fs_bdev_file_open_by_path (const char *path,blk_mode_t mode, +void *holder,struct super_block *sb); +void fs_bdev_file_release (struct file *bdev_file,struct super_block *sb); +#endif /* _LINUX_FS_SUPER_H */ Signature · 2 lines
-- 2.47.3
Re: [PATCH RFC v2 08/18] fs: add dedicated block device open helpers for filesystems
Quoted 3 lines · Darrick J. Wong
Hmm. I /think/ this might be causing regressions in xfs/006, generic/311, and xfs/264 on XFS realtime filesystems. Each test fails with a mount failure due to a busy device:
Yes, I bisected thee failures to "xfs: port to fs_bdev_file_open_by_path()" as reported on Saturday. Besides the failures I also still have the same architectural objections to this whole series as before.
Christian Brauner
maintainerRe: [PATCH RFC v2 08/18] fs: add dedicated block device open helpers for filesystems
In reply to Darrick J. WongQuoted 57 lines · Darrick J. Wong
Quoted 34 lines · Christian Brauner
Add fs_bdev_file_open_by_{dev,path}() and
fs_bdev_file_release(). They open the device withfs_holder_opsand register a claim in the device-to-superblock table. Claims on the same (device, superblock) pair share one entry, so when a filesystem claims a device it already uses (xfs with its log on the data device), no second entry is added and each superblock will be acted on once.The holder argument remains purely the block layer's exclusivity token:
a superblock, or afile_system_typefor a device shared by several superblocks of that type. The shared case only becomes usable once thefs_holder_opscallbacks resolve superblocks through the table instead ofbdev->bd_holder.Convert the main device,
setup_bdev_super()andkill_block_super(),
over: the open finds the entry registered bysget_fc()and claims it again. cramfs and romfs bypasskill_block_super()so they can handle MTD mounts and release the main device with a plainbdev_fput(), which would leave the claim behind: the (dev, sb) entry would never be unregistered and the passive reference it holds would keep the superblock alive forever. Convert their release paths in the same step.The frozen-device check stays in
setup_bdev_super()for the primary device and is added tofs_bdev_register()for new claims, i.e. every additional device a filesystem opens through the helpers. Only a (device, superblock) pair the superblock claimed earlier may be reopened while frozen (xfs with its log on the data device): the freeze already covers that superblock through the existing claim, so nothing escapes it. Without thesetup_bdev_super()check a device frozen before the mount even started (dmlock_fs, loop) could be mounted and written to (journal replay) under an active freeze, because the primary open reuses the entry registered bysget_fc()and never takes the new-claim path.Quoted 6 lines
Both checks read
bd_fsfreeze_countonly after the entry is published (bysget_fc()for the primary, byfs_bdev_register()for new claims) and pair withbdev_freeze()incrementing the count before walking the
table: either the mount sees the elevated freeze count and fails with EBUSY, or the freeze finds the published entry and converges onceSB_BORNis set.Hmm. I /think/ this might be causing regressions in xfs/006, generic/311, and xfs/264 on XFS realtime filesystems. Each test fails with a mount failure due to a busy device:
---
/run/fstests/bin/tests/xfs/006.out2025-07-15 14:41:40.170403463 -0700
+++/run/fstests/logs/xfs/006.out.bad2026-08-31 12:25:01.790239078 -0700
@@ -6,3 +6,7 @@ error/metadata/EIO/max_retries=-1
error/metadata/EIO/retry_timeout_seconds=-1 error/metadata/ENOSPC/max_retries=-1 error/metadata/ENOSPC/retry_timeout_seconds=-1
+mount:/mnt/s:/dev/mapper/error-test.006already mounted or mount point busy.
+ dmesg(1) may have more information after failed mount system call.
+umount.nfs: remote share not in 'host:dir' format
+umount.nfs:/mnt/s: not mounted
Quoted 6 lines
I'm not sure what's going on here, but dmesg has this to say:
XFS (dm-0): Invalid device [
/dev/mapper/error-rttest.006], error=-16So my guess is that the
SCRATCH_RTvolume isn't getting unfrozen properly?
That's a pre-existing that this series just made obvious and is fixed by:
fe967191e585 ("fs: don't return -EINVAL for successful nested thaw")
The realtime dm device doesn't get unfrozen after a lockfs suspend/resume cycle.
So xfs/006, xfs/264, and generic/311 wrap the rt device in its own flakey device. That suspends data then rt with lockfs and resume rt then data. So the rt thaw is a nested thaw.
A nested thaw drops the reference but thaw_super_locked() still returns its -EINVAL because it's still frozen. So bdev_thaw() keeps the count on error and dm unlock_fs() ignores the result.
That's been broken since 7366f8b6fc6a8. Before this series it was merely silent on the rt device. We properly refuse mounting devices that are frozen not just the main device.
And it also meant a second lockfs suspend of a leaked rt device never froze the filesystem at all...