]> git.hungrycats.org Git - linux/commitdiff
btrfs: validate but do not set bdev blocksize
authorQu Wenruo <wqu@suse.com>
Sat, 24 Oct 2026 23:39:50 +0000 (10:09 +1030)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:19 +0000 (21:36 +0100)
[BACKGROUND]
In the very early days, commit 325cd4bafeb6 ("Btrfs: properly set
blocksize when adding new device.") set the bdev blocksize as at that
time btrfs is using __bread() for super block IO, which went through
buffer heads thus requires the strict block size limit.

Then btrfs super block IO moved away from buffered heads, by commit
8f32380d3f29 ("btrfs: use the page cache for super block reading") and
commit 314b6dd0eebf ("btrfs: use bios instead of buffer_heads from super
block writeout").

So after that there is already no strict blocksize requirement for
btrfs, except the bdev's block size should not exceed 4K, which is also
the super block size of btrfs.

Btrfs data read/writes all follow sectorsize, which can be larger than
4K.
Btrfs metadata read/writes all follow nodesize, which is already 16K by
default.

Furthermore, there is already a bug report that set_blocksize() can
trigger a lockdep warning from btrfs, as set_blocksize() will acquire
inode lock, and under certain races can lead to lockdep warnings.

[ENHANCEMENT]
Remove the set_blocksize() calls, instead call bdev_validate_blocksize()
inside btrfs_open_device_deny_freeze() and btrfs_get_bdev_and_sb().

Btrfs doesn't use buffer heads, thus no strict set_blocksize()
requirement. But we still want to call bdev_validate_blocksize(), just in
case where a future block device with larger blocksize (e.g. 16K) shows
up.

For such devices btrfs super block bios will be rejected, so we want to call
bdev_validate_blocksize() early to reject such devices.

Furthermore, the bdev_validate_blocksize() itself is lockless, thus it
should resolve the lockdep report from syzbot.

Reported-by: syzbot+5c01398781fb51df783a@syzkaller.appspotmail.com
Link: https://lore.kernel.org/linux-btrfs/6adcb3be.4ce6c07b.2dfc0c.0005.GAE@google.com/
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/dev-replace.c
fs/btrfs/disk-io.h
fs/btrfs/volumes.c
fs/btrfs/volumes.h

index cdfe093e5c5c4fdd7bdd67a853e3cd632c6b1b6b..3c48cc714fef2e8447648bdaf873378d84ab9c62 100644 (file)
@@ -249,7 +249,7 @@ static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
        }
 
        /* Unfreezable for the whole replace; see btrfs_dev_replace_start(). */
-       bdev_file = btrfs_open_device_deny_freeze(device_path, fs_info->sb);
+       bdev_file = btrfs_open_device_deny_freeze(fs_info, device_path);
        if (IS_ERR(bdev_file)) {
                btrfs_err(fs_info, "target device %s is invalid!", device_path);
                return PTR_ERR(bdev_file);
@@ -312,7 +312,6 @@ static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
        /* Check the comment in btrfs_init_new_device() for the reason. */
        atomic_inc(&device->dev_stats_ccnt);
        device->dev_stats_valid = 1;
-       set_blocksize(bdev_file, BTRFS_BDEV_BLOCKSIZE);
        device->fs_devices = fs_devices;
 
        ret = btrfs_get_dev_zone_info(device, false);
index 290508894f7ceb7d8c5c9760e93c90ad7c2d3914..d721e58a69f39b0110c953dc438537363b0c132d 100644 (file)
@@ -28,8 +28,7 @@ struct btrfs_transaction;
 #define BTRFS_SUPER_MIRROR_SHIFT 12
 
 /*
- * Fixed blocksize for all devices, applies to specific ways of reading
- * metadata like superblock. Must meet the set_blocksize requirements.
+ * Fixed blocksize for all devices, applies to super block writes only.
  *
  * Do not change.
  */
index ce78a9441c64f2f231bf8fcfb5e2b77cc57c5a10..837aa2fb866f8271489eee941689a777bd2e35c4 100644 (file)
@@ -499,8 +499,10 @@ btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder,
        if (flush)
                sync_blockdev(bdev);
        if (holder) {
-               ret = set_blocksize(*bdev_file, BTRFS_BDEV_BLOCKSIZE);
+               ret = bdev_validate_blocksize(bdev, BTRFS_BDEV_BLOCKSIZE);
                if (ret) {
+                       btrfs_err(NULL, "device %s doesn't support blocksize %u",
+                                 device_path, BTRFS_BDEV_BLOCKSIZE);
                        fs_bdev_file_release(*bdev_file, holder);
                        goto error;
                }
@@ -1468,9 +1470,7 @@ static bool btrfs_skip_registration(struct btrfs_super_block *disk_super,
 }
 
 /*
- * Look for a btrfs signature on a device. This may be called out of the mount path
- * and we are not allowed to call set_blocksize during the scan. The superblock
- * is read via pagecache.
+ * Look for a btrfs signature on a device. The superblock is read via pagecache.
  *
  * With @mount_arg_dev it's a scan during mount time that will always register
  * the device or return an error. Multi-device and seeding devices are registered
@@ -2919,13 +2919,13 @@ next_slot:
 }
 
 /*
- * Open @path for @sb with freezing denied before the holder claim is published,
+ * Open @path for @fs_info->sb with freezing denied before the holder claim is published,
  * so a racing bdev_freeze() can never reach a claim a device add or replace may
  * still abort.  The deny is taken on a throwaway non-holder probe open, then the
  * holder is opened by the probe's dev_t.  Balanced by the caller.
  */
-struct file *btrfs_open_device_deny_freeze(const char *path,
-                                          struct super_block *sb)
+struct file *btrfs_open_device_deny_freeze(struct btrfs_fs_info *fs_info,
+                                          const char *path)
 {
        struct file *probe_file, *bdev_file;
        int ret;
@@ -2942,9 +2942,21 @@ struct file *btrfs_open_device_deny_freeze(const char *path,
        }
 
        bdev_file = fs_bdev_file_open_by_dev(file_bdev(probe_file)->bd_dev,
-                                            BLK_OPEN_WRITE, sb, sb);
-       if (IS_ERR(bdev_file))
+                                            BLK_OPEN_WRITE, fs_info->sb, fs_info->sb);
+       if (IS_ERR(bdev_file)) {
+               bdev_allow_freeze(file_bdev(probe_file));
+               goto out;
+       }
+
+       ret = bdev_validate_blocksize(file_bdev(bdev_file), BTRFS_BDEV_BLOCKSIZE);
+       if (ret < 0) {
+               fs_bdev_file_release(bdev_file, fs_info->sb);
                bdev_allow_freeze(file_bdev(probe_file));
+               btrfs_err(fs_info, "device %s doesn't support blocksize %u",
+                         path, BTRFS_BDEV_BLOCKSIZE);
+               bdev_file = ERR_PTR(ret);
+       }
+out:
        bdev_fput(probe_file);
        return bdev_file;
 }
@@ -2968,7 +2980,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
                return -EROFS;
 
        /* Forbid freezing until the device is a committed member (or unwound). */
-       bdev_file = btrfs_open_device_deny_freeze(device_path, fs_info->sb);
+       bdev_file = btrfs_open_device_deny_freeze(fs_info, device_path);
        if (IS_ERR(bdev_file))
                return PTR_ERR(bdev_file);
 
@@ -3043,7 +3055,6 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
         */
        atomic_inc(&device->dev_stats_ccnt);
        device->dev_stats_valid = 1;
-       set_blocksize(device->bdev_file, BTRFS_BDEV_BLOCKSIZE);
 
        if (seeding_dev) {
                /* GFP_KERNEL allocation must not be under device_list_mutex */
index 75b1179776c80a841967d2dba30656abe7316b68..d5fe5cba0c35abcc8bd7739805f094cfc0836b2b 100644 (file)
@@ -776,8 +776,8 @@ struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices
                                       const struct btrfs_dev_lookup_args *args);
 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
-struct file *btrfs_open_device_deny_freeze(const char *path,
-                                          struct super_block *sb);
+struct file *btrfs_open_device_deny_freeze(struct btrfs_fs_info *fs_info,
+                                          const char *path);
 int btrfs_balance(struct btrfs_fs_info *fs_info,
                  struct btrfs_balance_control *bctl,
                  struct btrfs_ioctl_balance_args *bargs);