2019-01-08 17:48:48 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Citrix Systems Inc.
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/cutils.h"
|
Include qemu/main-loop.h less
In my "build everything" tree, changing qemu/main-loop.h triggers a
recompile of some 5600 out of 6600 objects (not counting tests and
objects that don't depend on qemu/osdep.h). It includes block/aio.h,
which in turn includes qemu/event_notifier.h, qemu/notify.h,
qemu/processor.h, qemu/qsp.h, qemu/queue.h, qemu/thread-posix.h,
qemu/thread.h, qemu/timer.h, and a few more.
Include qemu/main-loop.h only where it's needed. Touching it now
recompiles only some 1700 objects. For block/aio.h and
qemu/event_notifier.h, these numbers drop from 5600 to 2800. For the
others, they shrink only slightly.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190812052359.30071-21-armbru@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-08-12 08:23:50 +03:00
|
|
|
#include "qemu/main-loop.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2019-01-08 17:49:01 +03:00
|
|
|
#include "qemu/option.h"
|
2019-01-08 17:48:48 +03:00
|
|
|
#include "qapi/error.h"
|
2019-01-08 17:49:01 +03:00
|
|
|
#include "qapi/qapi-commands-block-core.h"
|
2019-06-19 23:10:37 +03:00
|
|
|
#include "qapi/qapi-commands-qom.h"
|
2019-01-08 17:49:01 +03:00
|
|
|
#include "qapi/qapi-visit-block-core.h"
|
|
|
|
#include "qapi/qobject-input-visitor.h"
|
2019-01-08 17:48:48 +03:00
|
|
|
#include "qapi/visitor.h"
|
2019-01-08 17:49:01 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
|
|
|
#include "qapi/qmp/qstring.h"
|
2020-02-24 17:29:55 +03:00
|
|
|
#include "qom/object_interfaces.h"
|
2019-01-08 17:48:59 +03:00
|
|
|
#include "hw/xen/xen_common.h"
|
|
|
|
#include "hw/block/xen_blkif.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-01-08 17:48:48 +03:00
|
|
|
#include "hw/xen/xen-block.h"
|
2019-01-08 17:49:01 +03:00
|
|
|
#include "hw/xen/xen-backend.h"
|
2019-01-08 17:48:59 +03:00
|
|
|
#include "sysemu/blockdev.h"
|
|
|
|
#include "sysemu/block-backend.h"
|
|
|
|
#include "sysemu/iothread.h"
|
|
|
|
#include "dataplane/xen-block.h"
|
2019-01-08 17:48:48 +03:00
|
|
|
#include "trace.h"
|
|
|
|
|
2019-01-08 17:48:49 +03:00
|
|
|
static char *xen_block_get_name(XenDevice *xendev, Error **errp)
|
|
|
|
{
|
|
|
|
XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
|
|
|
|
XenBlockVdev *vdev = &blockdev->props.vdev;
|
|
|
|
|
|
|
|
return g_strdup_printf("%lu", vdev->number);
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:50 +03:00
|
|
|
static void xen_block_disconnect(XenDevice *xendev, Error **errp)
|
|
|
|
{
|
|
|
|
XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
|
|
|
|
const char *type = object_get_typename(OBJECT(blockdev));
|
|
|
|
XenBlockVdev *vdev = &blockdev->props.vdev;
|
|
|
|
|
|
|
|
trace_xen_block_disconnect(type, vdev->disk, vdev->partition);
|
2019-01-08 17:48:59 +03:00
|
|
|
|
|
|
|
xen_block_dataplane_stop(blockdev->dataplane);
|
2019-01-08 17:48:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_block_connect(XenDevice *xendev, Error **errp)
|
|
|
|
{
|
|
|
|
XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
|
|
|
|
const char *type = object_get_typename(OBJECT(blockdev));
|
|
|
|
XenBlockVdev *vdev = &blockdev->props.vdev;
|
2019-04-09 19:40:38 +03:00
|
|
|
BlockConf *conf = &blockdev->props.conf;
|
|
|
|
unsigned int feature_large_sector_size;
|
2019-01-08 17:48:59 +03:00
|
|
|
unsigned int order, nr_ring_ref, *ring_ref, event_channel, protocol;
|
|
|
|
char *str;
|
2019-01-08 17:48:50 +03:00
|
|
|
|
|
|
|
trace_xen_block_connect(type, vdev->disk, vdev->partition);
|
2019-01-08 17:48:59 +03:00
|
|
|
|
2019-04-09 19:40:38 +03:00
|
|
|
if (xen_device_frontend_scanf(xendev, "feature-large-sector-size", "%u",
|
|
|
|
&feature_large_sector_size) != 1) {
|
|
|
|
feature_large_sector_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (feature_large_sector_size != 1 &&
|
|
|
|
conf->logical_block_size != XEN_BLKIF_SECTOR_SIZE) {
|
|
|
|
error_setg(errp, "logical_block_size != %u not supported by frontend",
|
|
|
|
XEN_BLKIF_SECTOR_SIZE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:59 +03:00
|
|
|
if (xen_device_frontend_scanf(xendev, "ring-page-order", "%u",
|
|
|
|
&order) != 1) {
|
|
|
|
nr_ring_ref = 1;
|
|
|
|
ring_ref = g_new(unsigned int, nr_ring_ref);
|
|
|
|
|
|
|
|
if (xen_device_frontend_scanf(xendev, "ring-ref", "%u",
|
|
|
|
&ring_ref[0]) != 1) {
|
|
|
|
error_setg(errp, "failed to read ring-ref");
|
|
|
|
g_free(ring_ref);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (order <= blockdev->props.max_ring_page_order) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
nr_ring_ref = 1 << order;
|
|
|
|
ring_ref = g_new(unsigned int, nr_ring_ref);
|
|
|
|
|
|
|
|
for (i = 0; i < nr_ring_ref; i++) {
|
|
|
|
const char *key = g_strdup_printf("ring-ref%u", i);
|
|
|
|
|
|
|
|
if (xen_device_frontend_scanf(xendev, key, "%u",
|
|
|
|
&ring_ref[i]) != 1) {
|
|
|
|
error_setg(errp, "failed to read %s", key);
|
|
|
|
g_free((gpointer)key);
|
|
|
|
g_free(ring_ref);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free((gpointer)key);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error_setg(errp, "invalid ring-page-order (%d)", order);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xen_device_frontend_scanf(xendev, "event-channel", "%u",
|
|
|
|
&event_channel) != 1) {
|
|
|
|
error_setg(errp, "failed to read event-channel");
|
|
|
|
g_free(ring_ref);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xen_device_frontend_scanf(xendev, "protocol", "%ms",
|
|
|
|
&str) != 1) {
|
|
|
|
protocol = BLKIF_PROTOCOL_NATIVE;
|
|
|
|
} else {
|
|
|
|
if (strcmp(str, XEN_IO_PROTO_ABI_X86_32) == 0) {
|
|
|
|
protocol = BLKIF_PROTOCOL_X86_32;
|
|
|
|
} else if (strcmp(str, XEN_IO_PROTO_ABI_X86_64) == 0) {
|
|
|
|
protocol = BLKIF_PROTOCOL_X86_64;
|
|
|
|
} else {
|
|
|
|
protocol = BLKIF_PROTOCOL_NATIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_block_dataplane_start(blockdev->dataplane, ring_ref, nr_ring_ref,
|
|
|
|
event_channel, protocol, errp);
|
|
|
|
|
|
|
|
g_free(ring_ref);
|
2019-01-08 17:48:50 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:48 +03:00
|
|
|
static void xen_block_unrealize(XenDevice *xendev, Error **errp)
|
|
|
|
{
|
|
|
|
XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
|
|
|
|
XenBlockDeviceClass *blockdev_class =
|
|
|
|
XEN_BLOCK_DEVICE_GET_CLASS(xendev);
|
|
|
|
const char *type = object_get_typename(OBJECT(blockdev));
|
|
|
|
XenBlockVdev *vdev = &blockdev->props.vdev;
|
|
|
|
|
|
|
|
if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_xen_block_unrealize(type, vdev->disk, vdev->partition);
|
|
|
|
|
2019-01-08 17:48:50 +03:00
|
|
|
/* Disconnect from the frontend in case this has not already happened */
|
|
|
|
xen_block_disconnect(xendev, NULL);
|
|
|
|
|
2019-01-08 17:48:59 +03:00
|
|
|
xen_block_dataplane_destroy(blockdev->dataplane);
|
|
|
|
blockdev->dataplane = NULL;
|
|
|
|
|
2019-01-08 17:48:48 +03:00
|
|
|
if (blockdev_class->unrealize) {
|
|
|
|
blockdev_class->unrealize(blockdev, errp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:33:16 +03:00
|
|
|
static void xen_block_set_size(XenBlockDevice *blockdev)
|
|
|
|
{
|
|
|
|
const char *type = object_get_typename(OBJECT(blockdev));
|
|
|
|
XenBlockVdev *vdev = &blockdev->props.vdev;
|
|
|
|
BlockConf *conf = &blockdev->props.conf;
|
2019-04-09 19:40:38 +03:00
|
|
|
int64_t sectors = blk_getlength(conf->blk) / conf->logical_block_size;
|
2019-01-31 18:33:16 +03:00
|
|
|
XenDevice *xendev = XEN_DEVICE(blockdev);
|
|
|
|
|
|
|
|
trace_xen_block_size(type, vdev->disk, vdev->partition, sectors);
|
|
|
|
|
|
|
|
xen_device_backend_printf(xendev, "sectors", "%"PRIi64, sectors);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_block_resize_cb(void *opaque)
|
|
|
|
{
|
|
|
|
XenBlockDevice *blockdev = opaque;
|
|
|
|
XenDevice *xendev = XEN_DEVICE(blockdev);
|
|
|
|
enum xenbus_state state = xen_device_backend_get_state(xendev);
|
|
|
|
|
|
|
|
xen_block_set_size(blockdev);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mimic the behaviour of Linux xen-blkback and re-write the state
|
|
|
|
* to trigger the frontend watch.
|
|
|
|
*/
|
|
|
|
xen_device_backend_printf(xendev, "state", "%u", state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const BlockDevOps xen_block_dev_ops = {
|
|
|
|
.resize_cb = xen_block_resize_cb,
|
|
|
|
};
|
|
|
|
|
2019-01-08 17:48:48 +03:00
|
|
|
static void xen_block_realize(XenDevice *xendev, Error **errp)
|
|
|
|
{
|
|
|
|
XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
|
|
|
|
XenBlockDeviceClass *blockdev_class =
|
|
|
|
XEN_BLOCK_DEVICE_GET_CLASS(xendev);
|
|
|
|
const char *type = object_get_typename(OBJECT(blockdev));
|
|
|
|
XenBlockVdev *vdev = &blockdev->props.vdev;
|
2019-01-08 17:48:59 +03:00
|
|
|
BlockConf *conf = &blockdev->props.conf;
|
2019-04-09 19:40:38 +03:00
|
|
|
BlockBackend *blk = conf->blk;
|
2019-01-08 17:48:48 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
|
|
|
|
error_setg(errp, "vdev property not set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_xen_block_realize(type, vdev->disk, vdev->partition);
|
|
|
|
|
|
|
|
if (blockdev_class->realize) {
|
|
|
|
blockdev_class->realize(blockdev, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2019-01-08 17:48:59 +03:00
|
|
|
return;
|
2019-01-08 17:48:48 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-08 17:48:59 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The blkif protocol does not deal with removable media, so it must
|
|
|
|
* always be present, even for CDRom devices.
|
|
|
|
*/
|
2019-04-09 19:40:38 +03:00
|
|
|
assert(blk);
|
|
|
|
if (!blk_is_inserted(blk)) {
|
2019-01-08 17:48:59 +03:00
|
|
|
error_setg(errp, "device needs media, but drive is empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!blkconf_apply_backend_options(conf, blockdev->info & VDISK_READONLY,
|
2019-01-31 18:33:16 +03:00
|
|
|
true, errp)) {
|
2019-01-08 17:48:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(blockdev->info & VDISK_CDROM) &&
|
|
|
|
!blkconf_geometry(conf, NULL, 65535, 255, 255, errp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
blkconf_blocksizes(conf);
|
|
|
|
|
|
|
|
if (conf->logical_block_size > conf->physical_block_size) {
|
|
|
|
error_setg(
|
|
|
|
errp, "logical_block_size > physical_block_size not supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-09 19:40:38 +03:00
|
|
|
blk_set_dev_ops(blk, &xen_block_dev_ops, blockdev);
|
|
|
|
blk_set_guest_block_size(blk, conf->logical_block_size);
|
2019-01-08 17:48:59 +03:00
|
|
|
|
2019-03-20 17:28:25 +03:00
|
|
|
if (conf->discard_granularity == -1) {
|
|
|
|
conf->discard_granularity = conf->physical_block_size;
|
|
|
|
}
|
|
|
|
|
2019-04-09 19:40:38 +03:00
|
|
|
if (blk_get_flags(blk) & BDRV_O_UNMAP) {
|
2019-01-08 17:48:59 +03:00
|
|
|
xen_device_backend_printf(xendev, "feature-discard", "%u", 1);
|
2019-03-20 17:28:25 +03:00
|
|
|
xen_device_backend_printf(xendev, "discard-granularity", "%u",
|
|
|
|
conf->discard_granularity);
|
2019-01-08 17:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
xen_device_backend_printf(xendev, "feature-flush-cache", "%u", 1);
|
|
|
|
xen_device_backend_printf(xendev, "max-ring-page-order", "%u",
|
|
|
|
blockdev->props.max_ring_page_order);
|
|
|
|
xen_device_backend_printf(xendev, "info", "%u", blockdev->info);
|
|
|
|
|
|
|
|
xen_device_frontend_printf(xendev, "virtual-device", "%lu",
|
|
|
|
vdev->number);
|
|
|
|
xen_device_frontend_printf(xendev, "device-type", "%s",
|
|
|
|
blockdev->device_type);
|
|
|
|
|
|
|
|
xen_device_backend_printf(xendev, "sector-size", "%u",
|
2019-04-09 19:40:38 +03:00
|
|
|
conf->logical_block_size);
|
2019-01-31 18:33:16 +03:00
|
|
|
|
|
|
|
xen_block_set_size(blockdev);
|
2019-01-08 17:48:59 +03:00
|
|
|
|
|
|
|
blockdev->dataplane =
|
2019-04-09 19:40:38 +03:00
|
|
|
xen_block_dataplane_create(xendev, blk, conf->logical_block_size,
|
|
|
|
blockdev->props.iothread);
|
2019-01-08 17:48:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:50 +03:00
|
|
|
static void xen_block_frontend_changed(XenDevice *xendev,
|
|
|
|
enum xenbus_state frontend_state,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
switch (frontend_state) {
|
|
|
|
case XenbusStateInitialised:
|
|
|
|
case XenbusStateConnected:
|
|
|
|
if (backend_state == XenbusStateConnected) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_block_disconnect(xendev, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_block_connect(xendev, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_device_backend_set_state(xendev, XenbusStateConnected);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XenbusStateClosing:
|
|
|
|
xen_device_backend_set_state(xendev, XenbusStateClosing);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XenbusStateClosed:
|
2019-09-18 14:57:02 +03:00
|
|
|
case XenbusStateUnknown:
|
2019-01-08 17:48:50 +03:00
|
|
|
xen_block_disconnect(xendev, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_device_backend_set_state(xendev, XenbusStateClosed);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:48 +03:00
|
|
|
static char *disk_to_vbd_name(unsigned int disk)
|
|
|
|
{
|
|
|
|
char *name, *prefix = (disk >= 26) ?
|
|
|
|
disk_to_vbd_name((disk / 26) - 1) : g_strdup("");
|
|
|
|
|
|
|
|
name = g_strdup_printf("%s%c", prefix, 'a' + disk % 26);
|
|
|
|
g_free(prefix);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_block_get_vdev(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
XenBlockVdev *vdev = qdev_get_prop_ptr(dev, prop);
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
switch (vdev->type) {
|
|
|
|
case XEN_BLOCK_VDEV_TYPE_DP:
|
|
|
|
str = g_strdup_printf("d%lup%lu", vdev->disk, vdev->partition);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XEN_BLOCK_VDEV_TYPE_XVD:
|
|
|
|
case XEN_BLOCK_VDEV_TYPE_HD:
|
|
|
|
case XEN_BLOCK_VDEV_TYPE_SD: {
|
|
|
|
char *name = disk_to_vbd_name(vdev->disk);
|
|
|
|
|
|
|
|
str = g_strdup_printf("%s%s%lu",
|
|
|
|
(vdev->type == XEN_BLOCK_VDEV_TYPE_XVD) ?
|
|
|
|
"xvd" :
|
|
|
|
(vdev->type == XEN_BLOCK_VDEV_TYPE_HD) ?
|
|
|
|
"hd" :
|
|
|
|
"sd",
|
|
|
|
name, vdev->partition);
|
|
|
|
g_free(name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
error_setg(errp, "invalid vdev type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_str(v, name, &str, errp);
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:25:33 +03:00
|
|
|
static int vbd_name_to_disk(const char *name, const char **endp,
|
|
|
|
unsigned long *disk)
|
2019-01-08 17:48:48 +03:00
|
|
|
{
|
2019-02-15 19:25:33 +03:00
|
|
|
unsigned int n = 0;
|
2019-01-08 17:48:48 +03:00
|
|
|
|
|
|
|
while (*name != '\0') {
|
|
|
|
if (!g_ascii_isalpha(*name) || !g_ascii_islower(*name)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:25:33 +03:00
|
|
|
n *= 26;
|
|
|
|
n += *name++ - 'a' + 1;
|
2019-01-08 17:48:48 +03:00
|
|
|
}
|
|
|
|
*endp = name;
|
|
|
|
|
2019-02-15 19:25:33 +03:00
|
|
|
if (!n) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*disk = n - 1;
|
|
|
|
|
|
|
|
return 0;
|
2019-01-08 17:48:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_block_set_vdev(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
XenBlockVdev *vdev = qdev_get_prop_ptr(dev, prop);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
char *str, *p;
|
|
|
|
const char *end;
|
|
|
|
|
|
|
|
if (dev->realized) {
|
|
|
|
qdev_prop_set_after_realize(dev, name, errp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_str(v, name, &str, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strchr(str, 'd');
|
|
|
|
if (!p) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p++ = '\0';
|
|
|
|
if (*str == '\0') {
|
|
|
|
vdev->type = XEN_BLOCK_VDEV_TYPE_DP;
|
|
|
|
} else if (strcmp(str, "xv") == 0) {
|
|
|
|
vdev->type = XEN_BLOCK_VDEV_TYPE_XVD;
|
|
|
|
} else if (strcmp(str, "h") == 0) {
|
|
|
|
vdev->type = XEN_BLOCK_VDEV_TYPE_HD;
|
|
|
|
} else if (strcmp(str, "s") == 0) {
|
|
|
|
vdev->type = XEN_BLOCK_VDEV_TYPE_SD;
|
|
|
|
} else {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vdev->type == XEN_BLOCK_VDEV_TYPE_DP) {
|
|
|
|
if (qemu_strtoul(p, &end, 10, &vdev->disk)) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*end == 'p') {
|
2019-02-15 19:25:32 +03:00
|
|
|
if (*(++end) == '\0') {
|
2019-01-08 17:48:48 +03:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-15 19:25:33 +03:00
|
|
|
if (vbd_name_to_disk(p, &end, &vdev->disk)) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
2019-01-08 17:48:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*end != '\0') {
|
|
|
|
p = (char *)end;
|
|
|
|
|
|
|
|
if (qemu_strtoul(p, &end, 10, &vdev->partition)) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*end != '\0') {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vdev->partition = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (vdev->type) {
|
|
|
|
case XEN_BLOCK_VDEV_TYPE_DP:
|
|
|
|
case XEN_BLOCK_VDEV_TYPE_XVD:
|
|
|
|
if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
|
|
|
|
vdev->number = (202 << 8) | (vdev->disk << 4) |
|
|
|
|
vdev->partition;
|
|
|
|
} else if (vdev->disk < (1 << 20) && vdev->partition < (1 << 8)) {
|
|
|
|
vdev->number = (1 << 28) | (vdev->disk << 8) |
|
|
|
|
vdev->partition;
|
|
|
|
} else {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XEN_BLOCK_VDEV_TYPE_HD:
|
|
|
|
if ((vdev->disk == 0 || vdev->disk == 1) &&
|
|
|
|
vdev->partition < (1 << 6)) {
|
|
|
|
vdev->number = (3 << 8) | (vdev->disk << 6) | vdev->partition;
|
|
|
|
} else if ((vdev->disk == 2 || vdev->disk == 3) &&
|
|
|
|
vdev->partition < (1 << 6)) {
|
|
|
|
vdev->number = (22 << 8) | ((vdev->disk - 2) << 6) |
|
|
|
|
vdev->partition;
|
|
|
|
} else {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XEN_BLOCK_VDEV_TYPE_SD:
|
|
|
|
if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
|
|
|
|
vdev->number = (8 << 8) | (vdev->disk << 4) | vdev->partition;
|
|
|
|
} else {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(str);
|
|
|
|
return;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
error_setg(errp, "invalid virtual disk specifier");
|
|
|
|
|
|
|
|
vdev->type = XEN_BLOCK_VDEV_TYPE_INVALID;
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This property deals with 'vdev' names adhering to the Xen VBD naming
|
|
|
|
* scheme described in:
|
|
|
|
*
|
|
|
|
* https://xenbits.xen.org/docs/unstable/man/xen-vbd-interface.7.html
|
|
|
|
*/
|
|
|
|
const PropertyInfo xen_block_prop_vdev = {
|
|
|
|
.name = "str",
|
|
|
|
.description = "Virtual Disk specifier: d*p*/xvd*/hd*/sd*",
|
|
|
|
.get = xen_block_get_vdev,
|
|
|
|
.set = xen_block_set_vdev,
|
|
|
|
};
|
|
|
|
|
|
|
|
static Property xen_block_props[] = {
|
|
|
|
DEFINE_PROP("vdev", XenBlockDevice, props.vdev,
|
|
|
|
xen_block_prop_vdev, XenBlockVdev),
|
2019-01-08 17:48:59 +03:00
|
|
|
DEFINE_BLOCK_PROPERTIES(XenBlockDevice, props.conf),
|
|
|
|
DEFINE_PROP_UINT32("max-ring-page-order", XenBlockDevice,
|
|
|
|
props.max_ring_page_order, 4),
|
|
|
|
DEFINE_PROP_LINK("iothread", XenBlockDevice, props.iothread,
|
|
|
|
TYPE_IOTHREAD, IOThread *),
|
2019-01-08 17:48:48 +03:00
|
|
|
DEFINE_PROP_END_OF_LIST()
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xen_block_class_init(ObjectClass *class, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dev_class = DEVICE_CLASS(class);
|
|
|
|
XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
|
|
|
|
|
2019-01-08 17:49:01 +03:00
|
|
|
xendev_class->backend = "qdisk";
|
2019-01-08 17:48:59 +03:00
|
|
|
xendev_class->device = "vbd";
|
2019-01-08 17:48:49 +03:00
|
|
|
xendev_class->get_name = xen_block_get_name;
|
2019-01-08 17:48:48 +03:00
|
|
|
xendev_class->realize = xen_block_realize;
|
2019-01-08 17:48:50 +03:00
|
|
|
xendev_class->frontend_changed = xen_block_frontend_changed;
|
2019-01-08 17:48:48 +03:00
|
|
|
xendev_class->unrealize = xen_block_unrealize;
|
|
|
|
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dev_class, xen_block_props);
|
2019-01-08 17:48:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo xen_block_type_info = {
|
|
|
|
.name = TYPE_XEN_BLOCK_DEVICE,
|
|
|
|
.parent = TYPE_XEN_DEVICE,
|
|
|
|
.instance_size = sizeof(XenBlockDevice),
|
|
|
|
.abstract = true,
|
|
|
|
.class_size = sizeof(XenBlockDeviceClass),
|
|
|
|
.class_init = xen_block_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xen_disk_unrealize(XenBlockDevice *blockdev, Error **errp)
|
|
|
|
{
|
|
|
|
trace_xen_disk_unrealize();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_disk_realize(XenBlockDevice *blockdev, Error **errp)
|
|
|
|
{
|
2019-01-08 17:48:59 +03:00
|
|
|
BlockConf *conf = &blockdev->props.conf;
|
|
|
|
|
2019-01-08 17:48:48 +03:00
|
|
|
trace_xen_disk_realize();
|
2019-01-08 17:48:59 +03:00
|
|
|
|
|
|
|
blockdev->device_type = "disk";
|
|
|
|
|
|
|
|
if (!conf->blk) {
|
|
|
|
error_setg(errp, "drive property not set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
blockdev->info = blk_is_read_only(conf->blk) ? VDISK_READONLY : 0;
|
2019-01-08 17:48:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_disk_class_init(ObjectClass *class, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dev_class = DEVICE_CLASS(class);
|
|
|
|
XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
|
|
|
|
|
|
|
|
blockdev_class->realize = xen_disk_realize;
|
|
|
|
blockdev_class->unrealize = xen_disk_unrealize;
|
|
|
|
|
|
|
|
dev_class->desc = "Xen Disk Device";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo xen_disk_type_info = {
|
|
|
|
.name = TYPE_XEN_DISK_DEVICE,
|
|
|
|
.parent = TYPE_XEN_BLOCK_DEVICE,
|
|
|
|
.instance_size = sizeof(XenDiskDevice),
|
|
|
|
.class_init = xen_disk_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xen_cdrom_unrealize(XenBlockDevice *blockdev, Error **errp)
|
|
|
|
{
|
|
|
|
trace_xen_cdrom_unrealize();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_cdrom_realize(XenBlockDevice *blockdev, Error **errp)
|
|
|
|
{
|
2019-01-08 17:48:59 +03:00
|
|
|
BlockConf *conf = &blockdev->props.conf;
|
|
|
|
|
2019-01-08 17:48:48 +03:00
|
|
|
trace_xen_cdrom_realize();
|
2019-01-08 17:48:59 +03:00
|
|
|
|
|
|
|
blockdev->device_type = "cdrom";
|
|
|
|
|
|
|
|
if (!conf->blk) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Set up an empty drive */
|
2019-04-25 15:25:10 +03:00
|
|
|
conf->blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
|
2019-01-08 17:48:59 +03:00
|
|
|
|
|
|
|
rc = blk_attach_dev(conf->blk, DEVICE(blockdev));
|
|
|
|
if (!rc) {
|
|
|
|
error_setg_errno(errp, -rc, "failed to create drive");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blockdev->info = VDISK_READONLY | VDISK_CDROM;
|
2019-01-08 17:48:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_cdrom_class_init(ObjectClass *class, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dev_class = DEVICE_CLASS(class);
|
|
|
|
XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
|
|
|
|
|
|
|
|
blockdev_class->realize = xen_cdrom_realize;
|
|
|
|
blockdev_class->unrealize = xen_cdrom_unrealize;
|
|
|
|
|
|
|
|
dev_class->desc = "Xen CD-ROM Device";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo xen_cdrom_type_info = {
|
|
|
|
.name = TYPE_XEN_CDROM_DEVICE,
|
|
|
|
.parent = TYPE_XEN_BLOCK_DEVICE,
|
|
|
|
.instance_size = sizeof(XenCDRomDevice),
|
|
|
|
.class_init = xen_cdrom_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xen_block_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&xen_block_type_info);
|
|
|
|
type_register_static(&xen_disk_type_info);
|
|
|
|
type_register_static(&xen_cdrom_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(xen_block_register_types)
|
2019-01-08 17:49:01 +03:00
|
|
|
|
|
|
|
static void xen_block_blockdev_del(const char *node_name, Error **errp)
|
|
|
|
{
|
|
|
|
trace_xen_block_blockdev_del(node_name);
|
|
|
|
|
|
|
|
qmp_blockdev_del(node_name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *xen_block_blockdev_add(const char *id, QDict *qdict,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
const char *driver = qdict_get_try_str(qdict, "driver");
|
|
|
|
BlockdevOptions *options = NULL;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
char *node_name;
|
|
|
|
Visitor *v;
|
|
|
|
|
|
|
|
if (!driver) {
|
|
|
|
error_setg(errp, "no 'driver' parameter");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
node_name = g_strdup_printf("%s-%s", id, driver);
|
|
|
|
qdict_put_str(qdict, "node-name", node_name);
|
|
|
|
|
|
|
|
trace_xen_block_blockdev_add(node_name);
|
|
|
|
|
|
|
|
v = qobject_input_visitor_new(QOBJECT(qdict));
|
|
|
|
visit_type_BlockdevOptions(v, NULL, &options, &local_err);
|
|
|
|
visit_free(v);
|
|
|
|
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
qmp_blockdev_add(options, &local_err);
|
|
|
|
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
qapi_free_BlockdevOptions(options);
|
|
|
|
|
|
|
|
return node_name;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (options) {
|
|
|
|
qapi_free_BlockdevOptions(options);
|
|
|
|
}
|
|
|
|
g_free(node_name);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_block_drive_destroy(XenBlockDrive *drive, Error **errp)
|
|
|
|
{
|
|
|
|
char *node_name = drive->node_name;
|
|
|
|
|
|
|
|
if (node_name) {
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
xen_block_blockdev_del(node_name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_free(node_name);
|
|
|
|
drive->node_name = NULL;
|
|
|
|
}
|
|
|
|
g_free(drive->id);
|
|
|
|
g_free(drive);
|
|
|
|
}
|
|
|
|
|
|
|
|
static XenBlockDrive *xen_block_drive_create(const char *id,
|
|
|
|
const char *device_type,
|
|
|
|
QDict *opts, Error **errp)
|
|
|
|
{
|
|
|
|
const char *params = qdict_get_try_str(opts, "params");
|
|
|
|
const char *mode = qdict_get_try_str(opts, "mode");
|
|
|
|
const char *direct_io_safe = qdict_get_try_str(opts, "direct-io-safe");
|
|
|
|
const char *discard_enable = qdict_get_try_str(opts, "discard-enable");
|
|
|
|
char *driver = NULL;
|
|
|
|
char *filename = NULL;
|
|
|
|
XenBlockDrive *drive = NULL;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
QDict *file_layer;
|
|
|
|
QDict *driver_layer;
|
|
|
|
|
|
|
|
if (params) {
|
|
|
|
char **v = g_strsplit(params, ":", 2);
|
|
|
|
|
|
|
|
if (v[1] == NULL) {
|
|
|
|
filename = g_strdup(v[0]);
|
|
|
|
driver = g_strdup("raw");
|
|
|
|
} else {
|
|
|
|
if (strcmp(v[0], "aio") == 0) {
|
|
|
|
driver = g_strdup("raw");
|
|
|
|
} else if (strcmp(v[0], "vhd") == 0) {
|
|
|
|
driver = g_strdup("vpc");
|
|
|
|
} else {
|
|
|
|
driver = g_strdup(v[0]);
|
|
|
|
}
|
|
|
|
filename = g_strdup(v[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(v);
|
2019-02-19 19:34:40 +03:00
|
|
|
} else {
|
|
|
|
error_setg(errp, "no params");
|
2019-01-08 17:49:01 +03:00
|
|
|
goto done;
|
|
|
|
}
|
2019-02-19 19:34:40 +03:00
|
|
|
|
|
|
|
assert(filename);
|
2019-01-08 17:49:01 +03:00
|
|
|
assert(driver);
|
|
|
|
|
|
|
|
drive = g_new0(XenBlockDrive, 1);
|
|
|
|
drive->id = g_strdup(id);
|
|
|
|
|
|
|
|
file_layer = qdict_new();
|
2019-03-20 17:28:25 +03:00
|
|
|
driver_layer = qdict_new();
|
2019-01-08 17:49:01 +03:00
|
|
|
|
|
|
|
qdict_put_str(file_layer, "driver", "file");
|
|
|
|
qdict_put_str(file_layer, "filename", filename);
|
2019-02-19 19:34:40 +03:00
|
|
|
g_free(filename);
|
2019-01-08 17:49:01 +03:00
|
|
|
|
|
|
|
if (mode && *mode != 'w') {
|
|
|
|
qdict_put_bool(file_layer, "read-only", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direct_io_safe) {
|
|
|
|
unsigned long value;
|
|
|
|
|
|
|
|
if (!qemu_strtoul(direct_io_safe, NULL, 2, &value) && !!value) {
|
|
|
|
QDict *cache_qdict = qdict_new();
|
|
|
|
|
|
|
|
qdict_put_bool(cache_qdict, "direct", true);
|
2019-03-13 20:44:33 +03:00
|
|
|
qdict_put(file_layer, "cache", cache_qdict);
|
2019-01-08 17:49:01 +03:00
|
|
|
|
|
|
|
qdict_put_str(file_layer, "aio", "native");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (discard_enable) {
|
|
|
|
unsigned long value;
|
|
|
|
|
|
|
|
if (!qemu_strtoul(discard_enable, NULL, 2, &value) && !!value) {
|
|
|
|
qdict_put_str(file_layer, "discard", "unmap");
|
2019-03-20 17:28:25 +03:00
|
|
|
qdict_put_str(driver_layer, "discard", "unmap");
|
2019-01-08 17:49:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is necessary to turn file locking off as an emulated device
|
|
|
|
* may have already opened the same image file.
|
|
|
|
*/
|
|
|
|
qdict_put_str(file_layer, "locking", "off");
|
|
|
|
|
|
|
|
qdict_put_str(driver_layer, "driver", driver);
|
2019-02-19 19:34:40 +03:00
|
|
|
g_free(driver);
|
|
|
|
|
2019-03-13 20:44:33 +03:00
|
|
|
qdict_put(driver_layer, "file", file_layer);
|
2019-01-08 17:49:01 +03:00
|
|
|
|
|
|
|
g_assert(!drive->node_name);
|
|
|
|
drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
|
|
|
|
&local_err);
|
|
|
|
|
2019-02-19 19:34:40 +03:00
|
|
|
qobject_unref(driver_layer);
|
2019-01-08 17:49:01 +03:00
|
|
|
|
2019-02-19 19:34:40 +03:00
|
|
|
done:
|
2019-01-08 17:49:01 +03:00
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
xen_block_drive_destroy(drive, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return drive;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *xen_block_drive_get_node_name(XenBlockDrive *drive)
|
|
|
|
{
|
|
|
|
return drive->node_name ? drive->node_name : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_block_iothread_destroy(XenBlockIOThread *iothread,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
qmp_object_del(iothread->id, errp);
|
|
|
|
|
|
|
|
g_free(iothread->id);
|
|
|
|
g_free(iothread);
|
|
|
|
}
|
|
|
|
|
|
|
|
static XenBlockIOThread *xen_block_iothread_create(const char *id,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
|
|
|
|
Error *local_err = NULL;
|
2020-02-24 17:29:55 +03:00
|
|
|
QDict *opts;
|
|
|
|
QObject *ret_data;
|
2019-01-08 17:49:01 +03:00
|
|
|
|
|
|
|
iothread->id = g_strdup(id);
|
|
|
|
|
2020-02-24 17:29:55 +03:00
|
|
|
opts = qdict_new();
|
|
|
|
qdict_put_str(opts, "qom-type", TYPE_IOTHREAD);
|
|
|
|
qdict_put_str(opts, "id", id);
|
|
|
|
qmp_object_add(opts, &ret_data, &local_err);
|
|
|
|
qobject_unref(opts);
|
|
|
|
qobject_unref(ret_data);
|
|
|
|
|
2019-01-08 17:49:01 +03:00
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
|
|
|
|
g_free(iothread->id);
|
|
|
|
g_free(iothread);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iothread;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_block_device_create(XenBackendInstance *backend,
|
|
|
|
QDict *opts, Error **errp)
|
|
|
|
{
|
|
|
|
XenBus *xenbus = xen_backend_get_bus(backend);
|
|
|
|
const char *name = xen_backend_get_name(backend);
|
|
|
|
unsigned long number;
|
|
|
|
const char *vdev, *device_type;
|
|
|
|
XenBlockDrive *drive = NULL;
|
|
|
|
XenBlockIOThread *iothread = NULL;
|
|
|
|
XenDevice *xendev = NULL;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
const char *type;
|
|
|
|
XenBlockDevice *blockdev;
|
|
|
|
|
|
|
|
if (qemu_strtoul(name, NULL, 10, &number)) {
|
|
|
|
error_setg(errp, "failed to parse name '%s'", name);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_xen_block_device_create(number);
|
|
|
|
|
|
|
|
vdev = qdict_get_try_str(opts, "dev");
|
|
|
|
if (!vdev) {
|
|
|
|
error_setg(errp, "no dev parameter");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_type = qdict_get_try_str(opts, "device-type");
|
|
|
|
if (!device_type) {
|
|
|
|
error_setg(errp, "no device-type parameter");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(device_type, "disk")) {
|
|
|
|
type = TYPE_XEN_DISK_DEVICE;
|
|
|
|
} else if (!strcmp(device_type, "cdrom")) {
|
|
|
|
type = TYPE_XEN_CDROM_DEVICE;
|
|
|
|
} else {
|
|
|
|
error_setg(errp, "invalid device-type parameter '%s'", device_type);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
drive = xen_block_drive_create(vdev, device_type, opts, &local_err);
|
|
|
|
if (!drive) {
|
|
|
|
error_propagate_prepend(errp, local_err, "failed to create drive: ");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
iothread = xen_block_iothread_create(vdev, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate_prepend(errp, local_err,
|
|
|
|
"failed to create iothread: ");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
xendev = XEN_DEVICE(qdev_create(BUS(xenbus), type));
|
|
|
|
blockdev = XEN_BLOCK_DEVICE(xendev);
|
|
|
|
|
|
|
|
object_property_set_str(OBJECT(xendev), vdev, "vdev", &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate_prepend(errp, local_err, "failed to set 'vdev': ");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
object_property_set_str(OBJECT(xendev),
|
|
|
|
xen_block_drive_get_node_name(drive), "drive",
|
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate_prepend(errp, local_err, "failed to set 'drive': ");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
object_property_set_str(OBJECT(xendev), iothread->id, "iothread",
|
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate_prepend(errp, local_err,
|
|
|
|
"failed to set 'iothread': ");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
blockdev->iothread = iothread;
|
|
|
|
blockdev->drive = drive;
|
|
|
|
|
|
|
|
object_property_set_bool(OBJECT(xendev), true, "realized", &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate_prepend(errp, local_err,
|
|
|
|
"realization of device %s failed: ",
|
|
|
|
type);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_backend_set_device(backend, xendev);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (xendev) {
|
|
|
|
object_unparent(OBJECT(xendev));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iothread) {
|
|
|
|
xen_block_iothread_destroy(iothread, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drive) {
|
|
|
|
xen_block_drive_destroy(drive, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_block_device_destroy(XenBackendInstance *backend,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
XenDevice *xendev = xen_backend_get_device(backend);
|
|
|
|
XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
|
|
|
|
XenBlockVdev *vdev = &blockdev->props.vdev;
|
|
|
|
XenBlockDrive *drive = blockdev->drive;
|
|
|
|
XenBlockIOThread *iothread = blockdev->iothread;
|
2020-03-13 20:05:17 +03:00
|
|
|
Error *local_err = NULL;
|
2019-01-08 17:49:01 +03:00
|
|
|
|
|
|
|
trace_xen_block_device_destroy(vdev->number);
|
|
|
|
|
|
|
|
object_unparent(OBJECT(xendev));
|
|
|
|
|
|
|
|
if (iothread) {
|
|
|
|
xen_block_iothread_destroy(iothread, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate_prepend(errp, local_err,
|
2020-03-13 20:05:17 +03:00
|
|
|
"failed to destroy iothread: ");
|
2019-01-08 17:49:01 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drive) {
|
|
|
|
xen_block_drive_destroy(drive, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate_prepend(errp, local_err,
|
2020-03-13 20:05:17 +03:00
|
|
|
"failed to destroy drive: ");
|
|
|
|
return;
|
2019-01-08 17:49:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const XenBackendInfo xen_block_backend_info = {
|
|
|
|
.type = "qdisk",
|
|
|
|
.create = xen_block_device_create,
|
|
|
|
.destroy = xen_block_device_destroy,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xen_block_register_backend(void)
|
|
|
|
{
|
|
|
|
xen_backend_register(&xen_block_backend_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_backend_init(xen_block_register_backend);
|