btrf_show_devname related fixes
RFC PATCH v2 1/4 btrfs: consolidate device_list_mutex in prepare_sprout to its parent
In reply to Anand Jainbtrfs_prepare_sprout() moves seed devices into its own struct fs_devices, so that its parent function btrfs_init_new_device() can add the new sprout device to fs_info->fs_devices.
Both btrfs_prepare_sprout() and btrfs_init_new_device() needs device_list_mutex. But they are holding it sequentially, thus creates a small window to an opportunity to race. Close this opportunity and hold device_list_mutex common to both btrfs_init_new_device() and btrfs_prepare_sprout().
---
RFC because I haven't identified the other thread which could race with this, but still does this cleanup makes sense?
v2: fix the missing mutex_unlock in the error return
fs/btrfs/volumes.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/btrfs/volumes.c +4 -3
@@ -2366,6 +2366,8 @@ static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)u64 super_flags;lockdep_assert_held (&uuid_mutex); +lockdep_assert_held (&fs_devices->device_list_mutex); +if (!fs_devices->seeding)return -EINVAL ;
@@ -2397,7 +2399,6 @@ static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)INIT_LIST_HEAD (&seed_devices->alloc_list);mutex_init (&seed_devices->device_list_mutex); -mutex_lock (&fs_devices->device_list_mutex);list_splice_init_rcu (&fs_devices->devices, &seed_devices->devices, synchronize_rcu);list_for_each_entry (device, &seed_devices->devices, dev_list)
@@ -2413,7 +2414,6 @@ static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)generate_random_uuid (fs_devices->fsid);memcpy (fs_devices->metadata_uuid, fs_devices->fsid,BTRFS_FSID_SIZE );memcpy (disk_super->fsid, fs_devices->fsid,BTRFS_FSID_SIZE ); -mutex_unlock (&fs_devices->device_list_mutex); super_flags =btrfs_super_flags (disk_super) & ~BTRFS_SUPER_FLAG_SEEDING ;
@@ -2588,10 +2588,12 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path device->dev_stats_valid =1 ;set_blocksize (device->bdev,BTRFS_BDEV_BLOCKSIZE ); +mutex_lock (&fs_devices->device_list_mutex);if (seeding_dev) {btrfs_clear_sb_rdonly (sb); ret =btrfs_prepare_sprout (fs_info);if (ret) { +mutex_unlock (&fs_devices->device_list_mutex);btrfs_abort_transaction (trans, ret); goto error_trans; }
@@ -2599,7 +2601,6 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path device->fs_devices = fs_devices; -mutex_lock (&fs_devices->device_list_mutex);mutex_lock (&fs_info->chunk_mutex);list_add_rcu (&device->dev_list, &fs_devices->devices);list_add (&device->dev_alloc_list, &fs_devices->alloc_list);
Signature · 1 line
2.31.1
v2 cover btrf_show_devname related fixes
These fixes are inspired by the bug report and its discussions in the mailing list subject
btrfs: traverse seed devices if fs_devices::devices is empty in show_devname
Anand Jain (4):
btrfs: consolidate device_list_mutex in prepare_sprout to its parent
btrfs: save latest btrfs_device instead of its block_device in
fs_devices
btrfs: use latest_dev in btrfs_show_devname
btrfs: update latest_dev when we sprout
fs/btrfs/disk-io.c | 6 +++--- fs/btrfs/extent_io.c | 2 +- fs/btrfs/inode.c | 2 +- fs/btrfs/procfs.c | 6 +++--- fs/btrfs/super.c | 26 +++----------------------- fs/btrfs/volumes.c | 19 +++++++++++-------- fs/btrfs/volumes.h | 2 +- 7 files changed, 23 insertions(+), 40 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 2/4 btrfs: save latest btrfs_device instead of its block_device in fs_devices
In preparation to fix a bug in btrfs_show_devname(), save the device with the largest generation in fs_devices instead of just its bdev (as of now). So that btrfs_show_devname() can read device's name.
---
v2: born
fs/btrfs/disk-io.c | 6 +++--- fs/btrfs/extent_io.c | 2 +- fs/btrfs/inode.c | 2 +- fs/btrfs/procfs.c | 6 +++--- fs/btrfs/super.c | 2 +- fs/btrfs/volumes.c | 10 +++++----- fs/btrfs/volumes.h | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-)
fs/btrfs/disk-io.c +3 -3
@@ -3228,12 +3228,12 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devicemapping_set_gfp_mask (fs_info->btree_inode->i_mapping,GFP_NOFS );btrfs_init_btree_inode (fs_info); -invalidate_bdev (fs_devices->latest_bdev); +invalidate_bdev (fs_devices->latest_dev->bdev);/* * Read super block and check the signature bytes only */ - disk_super =btrfs_read_dev_super (fs_devices->latest_bdev); + disk_super =btrfs_read_dev_super (fs_devices->latest_dev->bdev);if (IS_ERR (disk_super)) { err =PTR_ERR (disk_super); goto fail_alloc;
@@ -3466,7 +3466,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device* below in btrfs_init_dev_replace(). */ btrfs_free_extra_devids (fs_devices); -if (!fs_devices->latest_bdev) { +if (!fs_devices->latest_dev->bdev) {btrfs_err (fs_info,"failed to read devices" ); goto fail_tree_roots; }
fs/btrfs/extent_io.c +1 -1
@@ -3327,7 +3327,7 @@ static int alloc_new_bio(struct btrfs_inode *inode,if (wbc) {struct block_device *bdev; - bdev = fs_info->fs_devices->latest_bdev; + bdev = fs_info->fs_devices->latest_dev->bdev;bio_set_dev (bio, bdev);wbc_init_bio (wbc, bio); }
fs/btrfs/inode.c +1 -1
@@ -7961,7 +7961,7 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, iomap->type =IOMAP_MAPPED ; } iomap->offset = start; - iomap->bdev = fs_info->fs_devices->latest_bdev; + iomap->bdev = fs_info->fs_devices->latest_dev->bdev; iomap->length = len;if (write &&btrfs_use_zone_append (BTRFS_I (inode), em->block_start))
fs/btrfs/procfs.c +3 -3
@@ -291,9 +291,9 @@ void btrfs_print_fsinfo(struct seq_file *seq) bdevname(fs_info->sb->s_bdev, b) : "null");BTRFS_SEQ_PRINT2 ("\tlatest_bdev:\t\t%s\n" , - fs_devices->latest_bdev ? -bdevname (fs_devices->latest_bdev, b) : -"null" ); + fs_devices->latest_dev->bdev ? +bdevname (fs_devices->latest_dev->bdev, b) : +"null" );fs_state_to_str (fs_info, fs_str);BTRFS_SEQ_PRINT2 ("\tfs_state:\t\t%s\n" , fs_str);
fs/btrfs/super.c +1 -1
@@ -1706,7 +1706,7 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, goto error_close_devices; } - bdev = fs_devices->latest_bdev; + bdev = fs_devices->latest_dev->bdev; s =sget (fs_type, btrfs_test_super, btrfs_set_super, flags |SB_NOSEC , fs_info);if (IS_ERR (s)) {
fs/btrfs/volumes.c +5 -5
@@ -1091,7 +1091,7 @@ void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices)list_for_each_entry (seed_dev, &fs_devices->seed_list, seed_list)__btrfs_free_extra_devids (seed_dev, &latest_dev); - fs_devices->latest_bdev = latest_dev->bdev; + fs_devices->latest_dev = latest_dev;mutex_unlock (&uuid_mutex); }
@@ -1206,7 +1206,7 @@ static int open_fs_devices(struct btrfs_fs_devices *fs_devices,return -EINVAL ; fs_devices->opened =1 ; - fs_devices->latest_bdev = latest_dev->bdev; + fs_devices->latest_dev = latest_dev; fs_devices->total_rw_bytes =0 ; fs_devices->chunk_alloc_policy =BTRFS_CHUNK_ALLOC_REGULAR ; fs_devices->read_policy =BTRFS_READ_POLICY_PID ;
@@ -1968,7 +1968,7 @@ static struct btrfs_device * btrfs_find_next_active_device( }/* -* Helper function to check if the given device is part of s_bdev / latest_bdev +* Helper function to check if the given device is part of s_bdev / latest_dev * and replace it with the provided or the next active device, in the context * where this function called, there should be always be another device (or * this_dev) which is active.
@@ -1987,8 +1987,8 @@ void __cold btrfs_assign_next_active_device(struct btrfs_device *device, (fs_info->sb->s_bdev == device->bdev)) fs_info->sb->s_bdev = next_device->bdev; -if (fs_info->fs_devices->latest_bdev == device->bdev) - fs_info->fs_devices->latest_bdev = next_device->bdev; +if (fs_info->fs_devices->latest_dev->bdev == device->bdev) + fs_info->fs_devices->latest_dev = next_device; }/*
fs/btrfs/volumes.h +1 -1
@@ -246,7 +246,7 @@ struct btrfs_fs_devices {/* Highest generation number of seen devices */ u64 latest_generation; -struct block_device *latest_bdev; +struct btrfs_device *latest_dev;/* all of the devices in the FS, protected by a mutex * so we can safely walk it to write out the supers without
Signature · 1 line
2.31.1
RFC PATCH v2 3/4 btrfs: use latest_dev in btrfs_show_devname
In reply to Anand Jainlatest_dev is updated according to the changes to the device list. That means we could use the latest_dev->name to show the device name in /proc/self/mounts. So this patch makes that change.
---
RFC because,
1. With this patch, /proc/self/mounts might not show the lowest devid device as we did before. We show the device that has the greatest generation and, we used it to build the tree. Are we ok with this change and, it won't affect the ABI? IMO it should be ok.
v2 use latest_dev so that device path is also shown
fs/btrfs/super.c | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-)
fs/btrfs/super.c +2 -22
@@ -2464,30 +2464,10 @@ static int btrfs_unfreeze(struct super_block *sb)static int btrfs_show_devname (struct seq_file *m,struct dentry *root) {struct btrfs_fs_info *fs_info =btrfs_sb (root->d_sb); -struct btrfs_device *dev, *first_dev =NULL ; -/* -* Lightweight locking of the devices. We should not need -* device_list_mutex here as we only read the device data and the list -* is protected by RCU. Even if a device is deleted during the list -* traversals, we'll get valid data, the freeing callback will wait at -* least until the rcu_read_unlock. -*/ -rcu_read_lock (); -list_for_each_entry_rcu (dev, &fs_info->fs_devices->devices, dev_list) { -if (test_bit (BTRFS_DEV_STATE_MISSING , &dev->dev_state)) -continue ; -if (!dev->name) -continue ; -if (!first_dev || dev->devid < first_dev->devid) - first_dev = dev; - } +seq_escape (m,rcu_str_deref (fs_info->fs_devices->latest_dev->name), +" \t\n\\" ); -if (first_dev) -seq_escape (m,rcu_str_deref (first_dev->name)," \t\n\\" ); -else -WARN_ON (1 ); -rcu_read_unlock ();return 0 ; }
Signature · 1 line
2.31.1
PATCH v2 4/4 btrfs: update latest_dev when we sprout
In reply to Anand JainWhen we add a device to the seed filesystem (sprouting) it is a new filesystem (and fsid) on the device added. Update the latest_device so that /proc/self/mounts shows the correct device.
For example:
$ btrfstune -S1 /dev/vg/scratch1 $ mount /dev/vg/scratch1 /btrfs mount: /btrfs: WARNING: device write-protected, mounted read-only.
$ cat /proc/self/mounts | grep btrfs /dev/mapper/vg-scratch1 /btrfs btrfs ro,relatime,space_cache,subvolid=5,subvol=/ 0 0
$ btrfs dev add -f /dev/vg/scratch0 /btrfs
Before:
$ cat /proc/self/mounts | grep btrfs /dev/mapper/vg-scratch1 /btrfs btrfs ro,relatime,space_cache,subvolid=5,subvol=/ 0 0
After:
$ cat /proc/self/mounts | grep btrfs /dev/mapper/vg-scratch0 /btrfs btrfs ro,relatime,space_cache,subvolid=5,subvol=/ 0 0
---
v2: born
fs/btrfs/volumes.c | 2 ++ 1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +2 -0
@@ -2597,6 +2597,8 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_pathbtrfs_abort_transaction (trans, ret); goto error_trans; } +btrfs_assign_next_active_device (fs_info->fs_devices->latest_dev, + device); } device->fs_devices = fs_devices;
Signature · 1 line
2.31.1
Re: [PATCH RFC v2 3/4] btrfs: use latest_dev in btrfs_show_devname
In reply to Anand JainQuoted 36 lines · Anand Jain
latest_devis updated according to the changes to the device list. That means we could use thelatest_dev->nameto show the device name in/proc/self/mounts. So this patch makes that change.Signed-off-by Anand Jain <[email protected]>---
RFC because,
1. With this patch,/proc/self/mountsmight not show the lowest devid device as we did before. We show the device that has the greatest generation and, we used it to build the tree. Are we ok with this change and, it won't affect the ABI? IMO it should be ok.v2 use
latest_devso that device path is also shownfs/btrfs/super.c | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-)fs/btrfs/super.c +0
-10@@ -2464,30 +2464,10 @@ static int btrfs_unfreeze(struct super_block *sb)static int btrfs_show_devname (struct seq_file *m,struct dentry *root) {struct btrfs_fs_info *fs_info =btrfs_sb (root->d_sb); -struct btrfs_device *dev, *first_dev =NULL ; -/* -* Lightweight locking of the devices. We should not need -* device_list_mutex here as we only read the device data and the list -* is protected by RCU. Even if a device is deleted during the list -* traversals, we'll get valid data, the freeing callback will wait at -* least until the rcu_read_unlock. -*/ -rcu_read_lock (); -list_for_each_entry_rcu (dev, &fs_info->fs_devices->devices, dev_list) {
fs/btrfs/super.c static int btrfs_unfreeze(struct super_block *sb) · line 2470
Quoted 10 lines
fs/btrfs/super.c +2
-7-if (test_bit (BTRFS_DEV_STATE_MISSING , &dev->dev_state)) -continue ; -if (!dev->name) -continue ; -if (!first_dev || dev->devid < first_dev->devid) - first_dev = dev; - } +seq_escape (m,rcu_str_deref (fs_info->fs_devices->latest_dev->name), +" \t\n\\" );
I missed rcu_lock here. I am fixing it in v3. Thx.
Quoted 9 lines
fs/btrfs/super.c +0
-5-if (first_dev) -seq_escape (m,rcu_str_deref (first_dev->name)," \t\n\\" ); -else -WARN_ON (1 ); -rcu_read_unlock ();return 0 ; }