static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
+static int do_open_tray(const char *device, bool force, Error **errp);
+
static const char *const if_name[IF_COUNT] = {
[IF_NONE] = "none",
[IF_IDE] = "ide",
block_job_txn_unref(block_job_txn);
}
-static int do_open_tray(const char *device, bool force, Error **errp);
-
void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
{
Error *local_err = NULL;
}
rc = do_open_tray(device, force, &local_err);
- if (local_err) {
+ if (rc && rc != -ENOSYS) {
error_propagate(errp, local_err);
return;
}
-
- if (rc == EINPROGRESS) {
- error_setg(errp, "Device '%s' is locked and force was not specified, "
- "wait for tray to open and try again", device);
- return;
- }
+ error_free(local_err);
qmp_x_blockdev_remove_medium(device, errp);
}
aio_context_release(aio_context);
}
-/**
- * returns -errno on fatal error, +errno for non-fatal situations.
- * errp will always be set when the return code is negative.
- * May return +ENOSYS if the device has no tray,
- * or +EINPROGRESS if the tray is locked and the guest has been notified.
+/*
+ * Attempt to open the tray of @device.
+ * If @force, ignore its tray lock.
+ * Else, if the tray is locked, don't open it, but ask the guest to open it.
+ * On error, store an error through @errp and return -errno.
+ * If @device does not exist, return -ENODEV.
+ * If it has no removable media, return -ENOTSUP.
+ * If it has no tray, return -ENOSYS.
+ * If the guest was asked to open the tray, return -EINPROGRESS.
+ * Else, return 0.
*/
static int do_open_tray(const char *device, bool force, Error **errp)
{
}
if (!blk_dev_has_tray(blk)) {
- /* Ignore this command on tray-less devices */
- return ENOSYS;
+ error_setg(errp, "Device '%s' does not have a tray", device);
+ return -ENOSYS;
}
if (blk_dev_is_tray_open(blk)) {
}
if (locked && !force) {
- return EINPROGRESS;
+ error_setg(errp, "Device '%s' is locked and force was not specified, "
+ "wait for tray to open and try again", device);
+ return -EINPROGRESS;
}
return 0;
void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
Error **errp)
{
+ Error *local_err = NULL;
+ int rc;
+
if (!has_force) {
force = false;
}
- do_open_tray(device, force, errp);
+ rc = do_open_tray(device, force, &local_err);
+ if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
+ error_propagate(errp, local_err);
+ return;
+ }
+ error_free(local_err);
}
void qmp_blockdev_close_tray(const char *device, Error **errp)
BlockBackend *blk;
BlockDriverState *medium_bs = NULL;
int bdrv_flags;
+ int rc;
QDict *options = NULL;
Error *err = NULL;
goto fail;
}
- qmp_blockdev_open_tray(device, false, false, &err);
- if (err) {
+ rc = do_open_tray(device, false, &err);
+ if (rc && rc != -ENOSYS) {
error_propagate(errp, err);
goto fail;
}
+ error_free(err);
+ err = NULL;
qmp_x_blockdev_remove_medium(device, &err);
if (err) {
backup_start(bs, target_bs, speed, sync, bmap,
on_source_error, on_target_error,
block_job_cb, bs, txn, &local_err);
+ bdrv_unref(target_bs);
if (local_err != NULL) {
- bdrv_unref(target_bs);
error_propagate(errp, local_err);
goto out;
}
BlockdevOnError on_target_error,
BlockJobTxn *txn, Error **errp)
{
- BlockBackend *blk, *target_blk;
+ BlockBackend *blk;
BlockDriverState *bs;
BlockDriverState *target_bs;
Error *local_err = NULL;
}
bs = blk_bs(blk);
- target_blk = blk_by_name(target);
- if (!target_blk) {
- error_setg(errp, "Device '%s' not found", target);
+ target_bs = bdrv_lookup_bs(target, target, errp);
+ if (!target_bs) {
goto out;
}
- if (!blk_is_available(target_blk)) {
- error_setg(errp, "Device '%s' has no medium", target);
- goto out;
+ if (bdrv_get_aio_context(target_bs) != aio_context) {
+ if (!bdrv_has_blk(target_bs)) {
+ /* The target BDS is not attached, we can safely move it to another
+ * AioContext. */
+ bdrv_set_aio_context(target_bs, aio_context);
+ } else {
+ error_setg(errp, "Target is attached to a different thread from "
+ "source.");
+ goto out;
+ }
}
- target_bs = blk_bs(target_blk);
-
- bdrv_ref(target_bs);
- bdrv_set_aio_context(target_bs, aio_context);
backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
on_target_error, block_job_cb, bs, txn, &local_err);
if (local_err != NULL) {
- bdrv_unref(target_bs);
error_propagate(errp, local_err);
}
out:
if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
return;
}
- if (bdrv_has_blk(target)) {
- error_setg(errp, "Cannot mirror to an attached block device");
- return;
- }
if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
sync = MIRROR_SYNC_MODE_FULL;
has_on_target_error, on_target_error,
has_unmap, unmap,
&local_err);
+ bdrv_unref(target_bs);
if (local_err) {
error_propagate(errp, local_err);
- bdrv_unref(target_bs);
}
out:
aio_context_release(aio_context);
aio_context = bdrv_get_aio_context(bs);
aio_context_acquire(aio_context);
- bdrv_ref(target_bs);
bdrv_set_aio_context(target_bs, aio_context);
blockdev_mirror_common(bs, target_bs,
&local_err);
if (local_err) {
error_propagate(errp, local_err);
- bdrv_unref(target_bs);
}
aio_context_release(aio_context);