@@ -113,7 +113,7 @@ BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk);
void blk_iostatus_disable(BlockBackend *blk);
void blk_iostatus_reset(BlockBackend *blk);
void blk_iostatus_set_err(BlockBackend *blk, int error);
-int blk_attach_dev(BlockBackend *blk, DeviceState *dev);
+int blk_attach_dev(BlockBackend *blk, DeviceState *dev, Error **errp);
void blk_detach_dev(BlockBackend *blk, DeviceState *dev);
DeviceState *blk_get_attached_dev(BlockBackend *blk);
char *blk_get_attached_dev_id(BlockBackend *blk);
@@ -882,12 +882,21 @@ void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)
/*
* Attach device model @dev to @blk.
+ *
+ * @blk: Block backend
+ * @dev: Device to attach the block backend to
+ * @errp: pointer to NULL initialized error object
+ *
* Return 0 on success, -EBUSY when a device model is attached already.
*/
-int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
+int blk_attach_dev(BlockBackend *blk, DeviceState *dev, Error **errp)
{
trace_blk_attach_dev(blk_name(blk), object_get_typename(OBJECT(dev)));
if (blk->dev) {
+ error_setg(errp, "cannot attach blk '%s' to device '%s' because it is "
+ "already attached by device '%s'",
+ blk_name(blk), object_get_typename(OBJECT(dev)),
+ object_get_typename(OBJECT(blk->dev)));
return -EBUSY;
}
@@ -519,7 +519,6 @@ static void floppy_drive_realize(DeviceState *qdev, Error **errp)
FloppyBus *bus = FLOPPY_BUS(qdev->parent_bus);
FDrive *drive;
bool read_only;
- int ret;
if (dev->unit == -1) {
for (dev->unit = 0; dev->unit < MAX_FD; dev->unit++) {
@@ -545,8 +544,7 @@ static void floppy_drive_realize(DeviceState *qdev, Error **errp)
if (!dev->conf.blk) {
/* Anonymous BlockBackend for an empty drive */
dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
- ret = blk_attach_dev(dev->conf.blk, qdev);
- assert(ret == 0);
+ blk_attach_dev(dev->conf.blk, qdev, &error_abort);
/* Don't take write permissions on an empty drive to allow attaching a
* read-only node later */
@@ -159,7 +159,6 @@ static void swim_drive_realize(DeviceState *qdev, Error **errp)
SWIMDrive *dev = SWIM_DRIVE(qdev);
SWIMBus *bus = SWIM_BUS(qdev->parent_bus);
FDrive *drive;
- int ret;
if (dev->unit == -1) {
for (dev->unit = 0; dev->unit < SWIM_MAX_FD; dev->unit++) {
@@ -185,8 +184,7 @@ static void swim_drive_realize(DeviceState *qdev, Error **errp)
if (!dev->conf.blk) {
/* Anonymous BlockBackend for an empty drive */
dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
- ret = blk_attach_dev(dev->conf.blk, qdev);
- assert(ret == 0);
+ blk_attach_dev(dev->conf.blk, qdev, &error_abort);
}
if (!blkconf_blocksizes(&dev->conf, errp)) {
@@ -616,14 +616,15 @@ static void xen_cdrom_realize(XenBlockDevice *blockdev, Error **errp)
blockdev->device_type = "cdrom";
if (!conf->blk) {
+ Error *local_err = NULL;
int rc;
/* Set up an empty drive */
conf->blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
- rc = blk_attach_dev(conf->blk, DEVICE(blockdev));
+ rc = blk_attach_dev(conf->blk, DEVICE(blockdev), &local_err);
if (!rc) {
- error_setg_errno(errp, -rc, "failed to create drive");
+ error_propagate_prepend(errp, local_err, "failed to create drive");
return;
}
}
@@ -139,18 +139,21 @@ static void set_drive_helper(Object *obj, Visitor *v, const char *name,
object_get_typename(OBJECT(dev)), prop->name, str);
goto fail;
}
- if (blk_attach_dev(blk, dev) < 0) {
+ if (blk_attach_dev(blk, dev, &local_err) < 0) {
DriveInfo *dinfo = blk_legacy_dinfo(blk);
if (dinfo && dinfo->type != IF_NONE) {
- error_setg(errp, "Drive '%s' is already in use because "
- "it has been automatically connected to another "
- "device (did you need 'if=none' in the drive options?)",
- str);
+ error_append_hint(&local_err,
+ "Drive '%s' is already in use because it has "
+ "been automatically connected to another device "
+ "(did you need 'if=none' in the drive options?)"
+ "\n", str);
} else {
- error_setg(errp, "Drive '%s' is already in use by another device",
- str);
+ error_append_hint(&local_err,
+ "Drive '%s' is already in use by another device",
+ str);
}
+ error_propagate(errp, local_err);
goto fail;
}
@@ -165,7 +165,6 @@ static void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp)
{
IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
IDEState *s = bus->ifs + dev->unit;
- int ret;
if (!dev->conf.blk) {
if (kind != IDE_CD) {
@@ -174,8 +173,7 @@ static void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp)
} else {
/* Anonymous BlockBackend for an empty drive */
dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
- ret = blk_attach_dev(dev->conf.blk, &dev->qdev);
- assert(ret == 0);
+ blk_attach_dev(dev->conf.blk, &dev->qdev, &error_abort);
}
}
@@ -2451,14 +2451,12 @@ static void scsi_cd_realize(SCSIDevice *dev, Error **errp)
{
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
AioContext *ctx;
- int ret;
if (!dev->conf.blk) {
/* Anonymous BlockBackend for an empty drive. As we put it into
* dev->conf, qdev takes care of detaching on unplug. */
dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
- ret = blk_attach_dev(dev->conf.blk, &dev->qdev);
- assert(ret == 0);
+ blk_attach_dev(dev->conf.blk, &dev->qdev, &error_abort);
}
ctx = blk_get_aio_context(dev->conf.blk);
Let blk_attach_dev() take an Error* object to return helpful information. Adapt the callers. $ qemu-system-arm -M n800 qemu-system-arm: sd_init failed: cannot attach blk 'sd0' to device 'sd-card' because it is already attached by device 'omap2-mmc' Drive 'sd0' is already in use because it has been automatically connected to another device (did you need 'if=none' in the drive options?) Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org> --- RFC: I'm not sure the error API is correctly used in set_drive_helper(). include/sysemu/block-backend.h | 2 +- block/block-backend.c | 11 ++++++++++- hw/block/fdc.c | 4 +--- hw/block/swim.c | 4 +--- hw/block/xen-block.c | 5 +++-- hw/core/qdev-properties-system.c | 17 ++++++++++------- hw/ide/qdev.c | 4 +--- hw/scsi/scsi-disk.c | 4 +--- 8 files changed, 28 insertions(+), 23 deletions(-)