Skip to content

Commit e03463d

Browse files
Darrick J. Wongaxboe
authored andcommitted
block: hoist block size validation code to a separate function
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>
1 parent c0e473a commit e03463d

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

block/bdev.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,39 @@ static void set_init_blocksize(struct block_device *bdev)
152152
get_order(bsize));
153153
}
154154

155-
int set_blocksize(struct file *file, int size)
155+
/**
156+
* bdev_validate_blocksize - check that this block size is acceptable
157+
* @bdev: blockdevice to check
158+
* @block_size: block size to check
159+
*
160+
* For block device users that do not use buffer heads or the block device
161+
* page cache, make sure that this block size can be used with the device.
162+
*
163+
* Return: On success zero is returned, negative error code on failure.
164+
*/
165+
int bdev_validate_blocksize(struct block_device *bdev, int block_size)
156166
{
157-
struct inode *inode = file->f_mapping->host;
158-
struct block_device *bdev = I_BDEV(inode);
159-
160-
if (blk_validate_block_size(size))
167+
if (blk_validate_block_size(block_size))
161168
return -EINVAL;
162169

163170
/* Size cannot be smaller than the size supported by the device */
164-
if (size < bdev_logical_block_size(bdev))
171+
if (block_size < bdev_logical_block_size(bdev))
165172
return -EINVAL;
166173

174+
return 0;
175+
}
176+
EXPORT_SYMBOL_GPL(bdev_validate_blocksize);
177+
178+
int set_blocksize(struct file *file, int size)
179+
{
180+
struct inode *inode = file->f_mapping->host;
181+
struct block_device *bdev = I_BDEV(inode);
182+
int ret;
183+
184+
ret = bdev_validate_blocksize(bdev, size);
185+
if (ret)
186+
return ret;
187+
167188
if (!file->private_data)
168189
return -EINVAL;
169190

include/linux/blkdev.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,7 @@ static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
16141614
return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
16151615
}
16161616

1617+
int bdev_validate_blocksize(struct block_device *bdev, int block_size);
16171618
int set_blocksize(struct file *file, int size);
16181619

16191620
int lookup_bdev(const char *pathname, dev_t *dev);

0 commit comments

Comments
 (0)