Skip to content

Commit b772a86

Browse files
Li Zefankdave
authored andcommitted
Btrfs: fix oops when calling statfs on readonly device
To reproduce this bug: # dd if=/dev/zero of=img bs=1M count=256 # mkfs.btrfs img # losetup -r /dev/loop1 img # mount /dev/loop1 /mnt OOPS!! It triggered BUG_ON(!nr_devices) in btrfs_calc_avail_data_space(). To fix this, instead of checking write-only devices, we check all open deivces: # df -h /dev/loop1 Filesystem Size Used Avail Use% Mounted on /dev/loop1 250M 28K 238M 1% /mnt Signed-off-by: Li Zefan <[email protected]>
1 parent ece7d20 commit b772a86

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

fs/btrfs/super.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
10831083
int i = 0, nr_devices;
10841084
int ret;
10851085

1086-
nr_devices = fs_info->fs_devices->rw_devices;
1086+
nr_devices = fs_info->fs_devices->open_devices;
10871087
BUG_ON(!nr_devices);
10881088

10891089
devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
@@ -1105,8 +1105,8 @@ static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
11051105
else
11061106
min_stripe_size = BTRFS_STRIPE_LEN;
11071107

1108-
list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
1109-
if (!device->in_fs_metadata)
1108+
list_for_each_entry(device, &fs_devices->devices, dev_list) {
1109+
if (!device->in_fs_metadata || !device->bdev)
11101110
continue;
11111111

11121112
avail_space = device->total_bytes - device->bytes_used;

0 commit comments

Comments
 (0)