btrfs: zoned: skip missing devices when releasing active zone reservations
PATCH 1/1 btrfs: zoned: skip missing devices when releasing active zone reservations
On a degraded mount of a zoned filesystem with a missing device, mount can crash in btrfs_check_active_zone_reservation():
KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f] RIP: 0010:btrfs_check_active_zone_reservation+0x369/0x5b0 Call Trace: open_ctree+0x377d/0x4140 btrfs_get_tree.cold+0xb/0x1de vfs_get_tree+0x80/0x2d0
The first loop of the function skips devices without a block device, but the second one, which releases the reservation for the stripes of the active metadata and system block groups, uses the zone_info of every stripe's device. A missing device has no zone_info, so a RAID1 metadata block group with a stripe on it crashes the mount.
Skip stripes on devices without a block device, as the first loop does. The same was fixed for zone activation and zone finishing in commit 2bbc4a45e5eb ("btrfs: zoned: fix zone activation with missing devices") and commit 35fec1089ebb ("btrfs: zoned: fix zone finishing with missing devices").
Reproducer: two zoned null_blk devices, mkfs.btrfs -d raid1 -m raid1 -O raid-stripe-tree, write and sync a file, unmount, remove the second device, mount the first with -o degraded.
---
Found with FuzzyRAID, a fault-injection tester for RAID that is not public at the moment. The analysis, the fix, the tests and the commit message were prepared with the help of an LLM. I reviewed everything.
This applies on top of 0c595e53b0c6 ("btrfs: zoned: don't release active zone reservations for conventional zones") in for-next. Stable trees without it need the same check in the old one line loop.
Tested on btrfs for-next (6f4bbde7e036) in QEMU with KASAN and zonednull_blk: without the patch, 2 of 2 degraded mounts crash as above. With it, both mount and the file reads back. A normal mount with both devices present works with and without the patch.
fs/btrfs/zoned.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/zoned.c +6 -1
@@ -3160,9 +3160,14 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)continue ;for (int i =0 ; i < map->num_stripes; i++) { -struct btrfs_zoned_device_info *zinfo = map->stripes[i].dev->zone_info; +struct btrfs_device *device = map->stripes[i].dev; +struct btrfs_zoned_device_info *zinfo = device->zone_info;u64 physical = map->stripes[i].physical; +/* A missing device has no zone_info. */ +if (!device->bdev) +continue ; +if (!test_bit (physical >> zinfo->zone_size_shift, zinfo->active_zones))continue ;
Signature · 1 line
2.53.0
Looks good,
Added to for-next,
Thanks