Skip to content

Commit 6605fd2

Browse files
asjkdave
authored andcommitted
btrfs: use latest_dev in btrfs_show_devname
The test case btrfs/238 reports the warning below: WARNING: CPU: 3 PID: 481 at fs/btrfs/super.c:2509 btrfs_show_devname+0x104/0x1e8 [btrfs] CPU: 2 PID: 1 Comm: systemd Tainted: G W O 5.14.0-rc1-custom #72 Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 Call trace: btrfs_show_devname+0x108/0x1b4 [btrfs] show_mountinfo+0x234/0x2c4 m_show+0x28/0x34 seq_read_iter+0x12c/0x3c4 vfs_read+0x29c/0x2c8 ksys_read+0x80/0xec __arm64_sys_read+0x28/0x34 invoke_syscall+0x50/0xf8 do_el0_svc+0x88/0x138 el0_svc+0x2c/0x8c el0t_64_sync_handler+0x84/0xe4 el0t_64_sync+0x198/0x19c Reason: While btrfs_prepare_sprout() moves the fs_devices::devices into fs_devices::seed_list, the btrfs_show_devname() searches for the devices and found none, leading to the warning as in above. Fix: latest_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, the pointer will be always valid as it's assigned before the device is deleted from the list in remove or replace. The RCU protection is sufficient as the device structure is freed after synchronization. Reported-by: Su Yue <[email protected]> Tested-by: Su Yue <[email protected]> Signed-off-by: Anand Jain <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent d24fa5c commit 6605fd2

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

fs/btrfs/super.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,30 +2463,16 @@ static int btrfs_unfreeze(struct super_block *sb)
24632463
static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
24642464
{
24652465
struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
2466-
struct btrfs_device *dev, *first_dev = NULL;
24672466

24682467
/*
2469-
* Lightweight locking of the devices. We should not need
2470-
* device_list_mutex here as we only read the device data and the list
2471-
* is protected by RCU. Even if a device is deleted during the list
2472-
* traversals, we'll get valid data, the freeing callback will wait at
2473-
* least until the rcu_read_unlock.
2468+
* There should be always a valid pointer in latest_dev, it may be stale
2469+
* for a short moment in case it's being deleted but still valid until
2470+
* the end of RCU grace period.
24742471
*/
24752472
rcu_read_lock();
2476-
list_for_each_entry_rcu(dev, &fs_info->fs_devices->devices, dev_list) {
2477-
if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
2478-
continue;
2479-
if (!dev->name)
2480-
continue;
2481-
if (!first_dev || dev->devid < first_dev->devid)
2482-
first_dev = dev;
2483-
}
2484-
2485-
if (first_dev)
2486-
seq_escape(m, rcu_str_deref(first_dev->name), " \t\n\\");
2487-
else
2488-
WARN_ON(1);
2473+
seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\");
24892474
rcu_read_unlock();
2475+
24902476
return 0;
24912477
}
24922478

0 commit comments

Comments
 (0)