block: hoist block size validation code to a separate function
authorDarrick J. Wong <djwong@kernel.org>
Wed, 23 Apr 2025 19:53:57 +0000 (12:53 -0700)
committerJens Axboe <axboe@kernel.dk>
Wed, 23 Apr 2025 19:58:06 +0000 (13:58 -0600)
Hoist the block size validation code to bdev_validate_blocksize so that
we can call it from filesystems that don't care about the bdev pagecache
manipulations of set_blocksize.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/174543795720.4139148.840349813093799165.stgit@frogsfrogsfrogs
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/bdev.c
include/linux/blkdev.h

index 3c856e56d5df58b7bc68daacd35b5b6a6a8ca274..e29b3b6d1707b8b7c8fe840f4e94623f539752d2 100644 (file)
@@ -152,18 +152,39 @@ static void set_init_blocksize(struct block_device *bdev)
                                    get_order(bsize));
 }
 
-int set_blocksize(struct file *file, int size)
+/**
+ * bdev_validate_blocksize - check that this block size is acceptable
+ * @bdev:      blockdevice to check
+ * @block_size:        block size to check
+ *
+ * For block device users that do not use buffer heads or the block device
+ * page cache, make sure that this block size can be used with the device.
+ *
+ * Return: On success zero is returned, negative error code on failure.
+ */
+int bdev_validate_blocksize(struct block_device *bdev, int block_size)
 {
-       struct inode *inode = file->f_mapping->host;
-       struct block_device *bdev = I_BDEV(inode);
-
-       if (blk_validate_block_size(size))
+       if (blk_validate_block_size(block_size))
                return -EINVAL;
 
        /* Size cannot be smaller than the size supported by the device */
-       if (size < bdev_logical_block_size(bdev))
+       if (block_size < bdev_logical_block_size(bdev))
                return -EINVAL;
 
+       return 0;
+}
+EXPORT_SYMBOL_GPL(bdev_validate_blocksize);
+
+int set_blocksize(struct file *file, int size)
+{
+       struct inode *inode = file->f_mapping->host;
+       struct block_device *bdev = I_BDEV(inode);
+       int ret;
+
+       ret = bdev_validate_blocksize(bdev, size);
+       if (ret)
+               return ret;
+
        if (!file->private_data)
                return -EINVAL;
 
index e39c45bc0a97ee18e532b0d2520fa93762fc0fe3..c6b478cfb32d6850cfa746e15b87c32be026494b 100644 (file)
@@ -1614,6 +1614,7 @@ static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
        return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
 }
 
+int bdev_validate_blocksize(struct block_device *bdev, int block_size);
 int set_blocksize(struct file *file, int size);
 
 int lookup_bdev(const char *pathname, dev_t *dev);