Skip to contentSkip to conversation

btrfs: don't allow adding block device of less than 1 MB

PATCH In review Mark Harmstone 3 participants 4 messages Sep 2, 2025 Btrfs file system

lore
Share & tools
lore
Reviewed-by on v2 1/1
#1 lore raw
In reply to Mark Harmstone
Quoted 12 lines · Mark Harmstone

Commit 15ae0410 in btrfs-progs inadvertently changed it so that if the BLKGETSIZE64 ioctl on a block device returned a size of 0, this was no longer seen as an error condition.

Unfortunately this is how disconnected NBD devices behave, meaning that with btrfs-progs 6.16 it's now possible to add a device you can't remove:

~ # btrfs device add /dev/nbd0 /root/temp
~ # btrfs device remove /dev/nbd0 /root/temp
ERROR: error removing device '/dev/nbd0': Invalid argument

Quoted 5 lines

This check should always have been done kernel-side anyway, so add a check in btrfs_init_new_device() that the new device doesn't have a size less than BTRFS_DEVICE_RANGE_RESERVED (i.e. 1 MB).

Thanks,
Qu

Quoted 20 lines
---
  fs/btrfs/volumes.c | 5 +++++
  1 file changed, 5 insertions(+)
fs/btrfs/volumes.c +5 -0
index 63b65f70a2b3..77a371f92ec0 100644
@@ -2726,6 +2726,11 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
  		goto error;
  	}
 
+	if (bdev_nr_bytes(file_bdev(bdev_file)) <= BTRFS_DEVICE_RANGE_RESERVED) {
+		ret = -EINVAL;
+		goto error;
+	}
+
  	if (fs_devices->seeding) {
  		seeding_dev = true;
  		down_write(&sb->s_umount);
#0 lore raw

PATCH v2 1/1 btrfs: don't allow adding block device of less than 1 MB

Commit 15ae0410 in btrfs-progs inadvertently changed it so that if the BLKGETSIZE64 ioctl on a block device returned a size of 0, this was no longer seen as an error condition.

Unfortunately this is how disconnected NBD devices behave, meaning that with btrfs-progs 6.16 it's now possible to add a device you can't remove:

~ # btrfs device add /dev/nbd0 /root/temp
~ # btrfs device remove /dev/nbd0 /root/temp
ERROR: error removing device '/dev/nbd0': Invalid argument

This check should always have been done kernel-side anyway, so add a check in btrfs_init_new_device() that the new device doesn't have a size less than BTRFS_DEVICE_RANGE_RESERVED (i.e. 1 MB).

---
 fs/btrfs/volumes.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/btrfs/volumes.c +5 -0
index 63b65f70a2b3..77a371f92ec0 100644
@@ -2726,6 +2726,11 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 		goto error;
 	}
 
+	if (bdev_nr_bytes(file_bdev(bdev_file)) <= BTRFS_DEVICE_RANGE_RESERVED) {
+		ret = -EINVAL;
+		goto error;
+	}
+
 	if (fs_devices->seeding) {
 		seeding_dev = true;
 		down_write(&sb->s_umount);
Signature · 1 line
2.49.1
#2 lore raw
Quoted 1 line · Mark Harmstone

Commit 15ae0410 in btrfs-progs inadvertently changed it so that if the

Please use full reference to commits, like 15ae0410c37a79 ("btrfs-progs:
add error handling for device_get_partition_size_fd_stat()").

Reviewed-by on v2 1/1
#3 lore raw
In reply to Mark Harmstone
Quoted 12 lines · Mark Harmstone

Commit 15ae0410 in btrfs-progs inadvertently changed it so that if the BLKGETSIZE64 ioctl on a block device returned a size of 0, this was no longer seen as an error condition.

Unfortunately this is how disconnected NBD devices behave, meaning that with btrfs-progs 6.16 it's now possible to add a device you can't remove:

~ # btrfs device add /dev/nbd0 /root/temp
~ # btrfs device remove /dev/nbd0 /root/temp
ERROR: error removing device '/dev/nbd0': Invalid argument

Quoted 5 lines

This check should always have been done kernel-side anyway, so add a check in btrfs_init_new_device() that the new device doesn't have a size less than BTRFS_DEVICE_RANGE_RESERVED (i.e. 1 MB).