block: consolidate blocksize properties consistency checks

Several block device properties related to blocksize configuration must
be in certain relationship WRT each other: physical block must be no
smaller than logical block; min_io_size, opt_io_size, and
discard_granularity must be a multiple of a logical block.

To ensure these requirements are met, add corresponding consistency
checks to blkconf_blocksizes, adjusting its signature to communicate
possible error to the caller.  Also remove the now redundant consistency
checks from the specific devices.

Signed-off-by: Roman Kagan <rvkagan@yandex-team.ru>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paul Durrant <paul@xen.org>
Message-Id: <20200528225516.1676602-3-rvkagan@yandex-team.ru>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Roman Kagan 2020-05-29 01:55:10 +03:00 committed by Kevin Wolf
parent 6abee26085
commit c56ee92fcb
11 changed files with 57 additions and 26 deletions

View File

@ -61,7 +61,7 @@ bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size,
return true; return true;
} }
void blkconf_blocksizes(BlockConf *conf) bool blkconf_blocksizes(BlockConf *conf, Error **errp)
{ {
BlockBackend *blk = conf->blk; BlockBackend *blk = conf->blk;
BlockSizes blocksizes; BlockSizes blocksizes;
@ -83,6 +83,34 @@ void blkconf_blocksizes(BlockConf *conf)
conf->logical_block_size = BDRV_SECTOR_SIZE; conf->logical_block_size = BDRV_SECTOR_SIZE;
} }
} }
if (conf->logical_block_size > conf->physical_block_size) {
error_setg(errp,
"logical_block_size > physical_block_size not supported");
return false;
}
if (!QEMU_IS_ALIGNED(conf->min_io_size, conf->logical_block_size)) {
error_setg(errp,
"min_io_size must be a multiple of logical_block_size");
return false;
}
if (!QEMU_IS_ALIGNED(conf->opt_io_size, conf->logical_block_size)) {
error_setg(errp,
"opt_io_size must be a multiple of logical_block_size");
return false;
}
if (conf->discard_granularity != -1 &&
!QEMU_IS_ALIGNED(conf->discard_granularity,
conf->logical_block_size)) {
error_setg(errp, "discard_granularity must be "
"a multiple of logical_block_size");
return false;
}
return true;
} }
bool blkconf_apply_backend_options(BlockConf *conf, bool readonly, bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,

View File

@ -554,7 +554,10 @@ static void floppy_drive_realize(DeviceState *qdev, Error **errp)
read_only = !blk_bs(dev->conf.blk) || blk_is_read_only(dev->conf.blk); read_only = !blk_bs(dev->conf.blk) || blk_is_read_only(dev->conf.blk);
} }
blkconf_blocksizes(&dev->conf); if (!blkconf_blocksizes(&dev->conf, errp)) {
return;
}
if (dev->conf.logical_block_size != 512 || if (dev->conf.logical_block_size != 512 ||
dev->conf.physical_block_size != 512) dev->conf.physical_block_size != 512)
{ {

View File

@ -1425,7 +1425,9 @@ static void nvme_init_state(NvmeCtrl *n)
static void nvme_init_blk(NvmeCtrl *n, Error **errp) static void nvme_init_blk(NvmeCtrl *n, Error **errp)
{ {
blkconf_blocksizes(&n->conf); if (!blkconf_blocksizes(&n->conf, errp)) {
return;
}
blkconf_apply_backend_options(&n->conf, blk_is_read_only(n->conf.blk), blkconf_apply_backend_options(&n->conf, blk_is_read_only(n->conf.blk),
false, errp); false, errp);
} }

View File

@ -189,7 +189,10 @@ static void swim_drive_realize(DeviceState *qdev, Error **errp)
assert(ret == 0); assert(ret == 0);
} }
blkconf_blocksizes(&dev->conf); if (!blkconf_blocksizes(&dev->conf, errp)) {
return;
}
if (dev->conf.logical_block_size != 512 || if (dev->conf.logical_block_size != 512 ||
dev->conf.physical_block_size != 512) dev->conf.physical_block_size != 512)
{ {

View File

@ -1174,12 +1174,7 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
return; return;
} }
blkconf_blocksizes(&conf->conf); if (!blkconf_blocksizes(&conf->conf, errp)) {
if (conf->conf.logical_block_size >
conf->conf.physical_block_size) {
error_setg(errp,
"logical_block_size > physical_block_size not supported");
return; return;
} }

View File

@ -239,11 +239,7 @@ static void xen_block_realize(XenDevice *xendev, Error **errp)
return; return;
} }
blkconf_blocksizes(conf); if (!blkconf_blocksizes(conf, errp)) {
if (conf->logical_block_size > conf->physical_block_size) {
error_setg(
errp, "logical_block_size > physical_block_size not supported");
return; return;
} }

View File

@ -187,7 +187,10 @@ static void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp)
return; return;
} }
blkconf_blocksizes(&dev->conf); if (!blkconf_blocksizes(&dev->conf, errp)) {
return;
}
if (dev->conf.logical_block_size != 512) { if (dev->conf.logical_block_size != 512) {
error_setg(errp, "logical_block_size must be 512 for IDE"); error_setg(errp, "logical_block_size must be 512 for IDE");
return; return;

View File

@ -2346,12 +2346,7 @@ static void scsi_realize(SCSIDevice *dev, Error **errp)
return; return;
} }
blkconf_blocksizes(&s->qdev.conf); if (!blkconf_blocksizes(&s->qdev.conf, errp)) {
if (s->qdev.conf.logical_block_size >
s->qdev.conf.physical_block_size) {
error_setg(errp,
"logical_block_size > physical_block_size not supported");
return; return;
} }
@ -2436,7 +2431,9 @@ static void scsi_hd_realize(SCSIDevice *dev, Error **errp)
if (s->qdev.conf.blk) { if (s->qdev.conf.blk) {
ctx = blk_get_aio_context(s->qdev.conf.blk); ctx = blk_get_aio_context(s->qdev.conf.blk);
aio_context_acquire(ctx); aio_context_acquire(ctx);
blkconf_blocksizes(&s->qdev.conf); if (!blkconf_blocksizes(&s->qdev.conf, errp)) {
goto out;
}
} }
s->qdev.blocksize = s->qdev.conf.logical_block_size; s->qdev.blocksize = s->qdev.conf.logical_block_size;
s->qdev.type = TYPE_DISK; s->qdev.type = TYPE_DISK;
@ -2444,6 +2441,7 @@ static void scsi_hd_realize(SCSIDevice *dev, Error **errp)
s->product = g_strdup("QEMU HARDDISK"); s->product = g_strdup("QEMU HARDDISK");
} }
scsi_realize(&s->qdev, errp); scsi_realize(&s->qdev, errp);
out:
if (ctx) { if (ctx) {
aio_context_release(ctx); aio_context_release(ctx);
} }

View File

@ -612,7 +612,10 @@ static void usb_msd_storage_realize(USBDevice *dev, Error **errp)
return; return;
} }
blkconf_blocksizes(&s->conf); if (!blkconf_blocksizes(&s->conf, errp)) {
return;
}
if (!blkconf_apply_backend_options(&s->conf, blk_is_read_only(blk), true, if (!blkconf_apply_backend_options(&s->conf, blk_is_read_only(blk), true,
errp)) { errp)) {
return; return;

View File

@ -87,7 +87,7 @@ bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size,
bool blkconf_geometry(BlockConf *conf, int *trans, bool blkconf_geometry(BlockConf *conf, int *trans,
unsigned cyls_max, unsigned heads_max, unsigned secs_max, unsigned cyls_max, unsigned heads_max, unsigned secs_max,
Error **errp); Error **errp);
void blkconf_blocksizes(BlockConf *conf); bool blkconf_blocksizes(BlockConf *conf, Error **errp);
bool blkconf_apply_backend_options(BlockConf *conf, bool readonly, bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,
bool resizable, Error **errp); bool resizable, Error **errp);

View File

@ -1204,7 +1204,7 @@ Testing: -drive if=none,file=TEST_DIR/t.qcow2 -device floppy,drive=none0,physica
drive-type = "144" drive-type = "144"
Testing: -drive if=none,file=TEST_DIR/t.qcow2 -device floppy,drive=none0,logical_block_size=4096 Testing: -drive if=none,file=TEST_DIR/t.qcow2 -device floppy,drive=none0,logical_block_size=4096
QEMU_PROG: -device floppy,drive=none0,logical_block_size=4096: Physical and logical block size must be 512 for floppy QEMU_PROG: -device floppy,drive=none0,logical_block_size=4096: logical_block_size > physical_block_size not supported
Testing: -drive if=none,file=TEST_DIR/t.qcow2 -device floppy,drive=none0,physical_block_size=1024 Testing: -drive if=none,file=TEST_DIR/t.qcow2 -device floppy,drive=none0,physical_block_size=1024
QEMU_PROG: -device floppy,drive=none0,physical_block_size=1024: Physical and logical block size must be 512 for floppy QEMU_PROG: -device floppy,drive=none0,physical_block_size=1024: Physical and logical block size must be 512 for floppy