Skip to content

Commit a17ece7

Browse files
Christoph Hellwigaxboe
authored andcommitted
loop: regularize upgrading the block size for direct I/O
The LOOP_CONFIGURE path automatically upgrades the block size to that of the underlying file for O_DIRECT file descriptors, but the LOOP_SET_BLOCK_SIZE path does not. Fix this by lifting the code to pick the block size into common code. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Hannes Reinecke <[email protected]> Reviewed-by: Bart Van Assche <[email protected]> Reviewed-by: Damien Le Moal <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent ae0d40f commit a17ece7

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

drivers/block/loop.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -975,10 +975,24 @@ loop_set_status_from_info(struct loop_device *lo,
975975
return 0;
976976
}
977977

978+
static unsigned short loop_default_blocksize(struct loop_device *lo,
979+
struct block_device *backing_bdev)
980+
{
981+
/* In case of direct I/O, match underlying block size */
982+
if ((lo->lo_backing_file->f_flags & O_DIRECT) && backing_bdev)
983+
return bdev_logical_block_size(backing_bdev);
984+
return SECTOR_SIZE;
985+
}
986+
978987
static int loop_reconfigure_limits(struct loop_device *lo, unsigned short bsize)
979988
{
989+
struct file *file = lo->lo_backing_file;
990+
struct inode *inode = file->f_mapping->host;
980991
struct queue_limits lim;
981992

993+
if (!bsize)
994+
bsize = loop_default_blocksize(lo, inode->i_sb->s_bdev);
995+
982996
lim = queue_limits_start_update(lo->lo_queue);
983997
lim.logical_block_size = bsize;
984998
lim.physical_block_size = bsize;
@@ -997,7 +1011,6 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
9971011
int error;
9981012
loff_t size;
9991013
bool partscan;
1000-
unsigned short bsize;
10011014
bool is_loop;
10021015

10031016
if (!file)
@@ -1076,15 +1089,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
10761089
if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
10771090
blk_queue_write_cache(lo->lo_queue, true, false);
10781091

1079-
if (config->block_size)
1080-
bsize = config->block_size;
1081-
else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev)
1082-
/* In case of direct I/O, match underlying block size */
1083-
bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
1084-
else
1085-
bsize = 512;
1086-
1087-
error = loop_reconfigure_limits(lo, bsize);
1092+
error = loop_reconfigure_limits(lo, config->block_size);
10881093
if (WARN_ON_ONCE(error))
10891094
goto out_unlock;
10901095

0 commit comments

Comments
 (0)