2 * Copyright (c) 2018 Citrix Systems Inc.
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
8 #include "qemu/osdep.h"
9 #include "qemu/cutils.h"
10 #include "qemu/main-loop.h"
11 #include "qemu/module.h"
12 #include "qemu/option.h"
13 #include "qapi/error.h"
14 #include "qapi/qapi-commands-block-core.h"
15 #include "qapi/qapi-commands-qom.h"
16 #include "qapi/qapi-visit-block-core.h"
17 #include "qapi/qobject-input-visitor.h"
18 #include "qapi/visitor.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qstring.h"
21 #include "qom/object_interfaces.h"
22 #include "hw/xen/xen_common.h"
23 #include "hw/block/xen_blkif.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/xen/xen-block.h"
26 #include "hw/xen/xen-backend.h"
27 #include "sysemu/blockdev.h"
28 #include "sysemu/block-backend.h"
29 #include "sysemu/iothread.h"
30 #include "dataplane/xen-block.h"
33 static char *xen_block_get_name(XenDevice
*xendev
, Error
**errp
)
35 XenBlockDevice
*blockdev
= XEN_BLOCK_DEVICE(xendev
);
36 XenBlockVdev
*vdev
= &blockdev
->props
.vdev
;
38 return g_strdup_printf("%lu", vdev
->number
);
41 static void xen_block_disconnect(XenDevice
*xendev
, Error
**errp
)
43 XenBlockDevice
*blockdev
= XEN_BLOCK_DEVICE(xendev
);
44 const char *type
= object_get_typename(OBJECT(blockdev
));
45 XenBlockVdev
*vdev
= &blockdev
->props
.vdev
;
47 trace_xen_block_disconnect(type
, vdev
->disk
, vdev
->partition
);
49 xen_block_dataplane_stop(blockdev
->dataplane
);
52 static void xen_block_connect(XenDevice
*xendev
, Error
**errp
)
54 XenBlockDevice
*blockdev
= XEN_BLOCK_DEVICE(xendev
);
55 const char *type
= object_get_typename(OBJECT(blockdev
));
56 XenBlockVdev
*vdev
= &blockdev
->props
.vdev
;
57 BlockConf
*conf
= &blockdev
->props
.conf
;
58 unsigned int feature_large_sector_size
;
59 unsigned int order
, nr_ring_ref
, *ring_ref
, event_channel
, protocol
;
62 trace_xen_block_connect(type
, vdev
->disk
, vdev
->partition
);
64 if (xen_device_frontend_scanf(xendev
, "feature-large-sector-size", "%u",
65 &feature_large_sector_size
) != 1) {
66 feature_large_sector_size
= 0;
69 if (feature_large_sector_size
!= 1 &&
70 conf
->logical_block_size
!= XEN_BLKIF_SECTOR_SIZE
) {
71 error_setg(errp
, "logical_block_size != %u not supported by frontend",
72 XEN_BLKIF_SECTOR_SIZE
);
76 if (xen_device_frontend_scanf(xendev
, "ring-page-order", "%u",
79 ring_ref
= g_new(unsigned int, nr_ring_ref
);
81 if (xen_device_frontend_scanf(xendev
, "ring-ref", "%u",
83 error_setg(errp
, "failed to read ring-ref");
87 } else if (order
<= blockdev
->props
.max_ring_page_order
) {
90 nr_ring_ref
= 1 << order
;
91 ring_ref
= g_new(unsigned int, nr_ring_ref
);
93 for (i
= 0; i
< nr_ring_ref
; i
++) {
94 const char *key
= g_strdup_printf("ring-ref%u", i
);
96 if (xen_device_frontend_scanf(xendev
, key
, "%u",
98 error_setg(errp
, "failed to read %s", key
);
99 g_free((gpointer
)key
);
104 g_free((gpointer
)key
);
107 error_setg(errp
, "invalid ring-page-order (%d)", order
);
111 if (xen_device_frontend_scanf(xendev
, "event-channel", "%u",
112 &event_channel
) != 1) {
113 error_setg(errp
, "failed to read event-channel");
118 if (xen_device_frontend_scanf(xendev
, "protocol", "%ms",
120 protocol
= BLKIF_PROTOCOL_NATIVE
;
122 if (strcmp(str
, XEN_IO_PROTO_ABI_X86_32
) == 0) {
123 protocol
= BLKIF_PROTOCOL_X86_32
;
124 } else if (strcmp(str
, XEN_IO_PROTO_ABI_X86_64
) == 0) {
125 protocol
= BLKIF_PROTOCOL_X86_64
;
127 protocol
= BLKIF_PROTOCOL_NATIVE
;
133 xen_block_dataplane_start(blockdev
->dataplane
, ring_ref
, nr_ring_ref
,
134 event_channel
, protocol
, errp
);
139 static void xen_block_unrealize(XenDevice
*xendev
)
141 XenBlockDevice
*blockdev
= XEN_BLOCK_DEVICE(xendev
);
142 XenBlockDeviceClass
*blockdev_class
=
143 XEN_BLOCK_DEVICE_GET_CLASS(xendev
);
144 const char *type
= object_get_typename(OBJECT(blockdev
));
145 XenBlockVdev
*vdev
= &blockdev
->props
.vdev
;
147 if (vdev
->type
== XEN_BLOCK_VDEV_TYPE_INVALID
) {
151 trace_xen_block_unrealize(type
, vdev
->disk
, vdev
->partition
);
153 /* Disconnect from the frontend in case this has not already happened */
154 xen_block_disconnect(xendev
, NULL
);
156 xen_block_dataplane_destroy(blockdev
->dataplane
);
157 blockdev
->dataplane
= NULL
;
159 if (blockdev_class
->unrealize
) {
160 blockdev_class
->unrealize(blockdev
);
164 static void xen_block_set_size(XenBlockDevice
*blockdev
)
166 const char *type
= object_get_typename(OBJECT(blockdev
));
167 XenBlockVdev
*vdev
= &blockdev
->props
.vdev
;
168 BlockConf
*conf
= &blockdev
->props
.conf
;
169 int64_t sectors
= blk_getlength(conf
->blk
) / conf
->logical_block_size
;
170 XenDevice
*xendev
= XEN_DEVICE(blockdev
);
172 trace_xen_block_size(type
, vdev
->disk
, vdev
->partition
, sectors
);
174 xen_device_backend_printf(xendev
, "sectors", "%"PRIi64
, sectors
);
177 static void xen_block_resize_cb(void *opaque
)
179 XenBlockDevice
*blockdev
= opaque
;
180 XenDevice
*xendev
= XEN_DEVICE(blockdev
);
181 enum xenbus_state state
= xen_device_backend_get_state(xendev
);
183 xen_block_set_size(blockdev
);
186 * Mimic the behaviour of Linux xen-blkback and re-write the state
187 * to trigger the frontend watch.
189 xen_device_backend_printf(xendev
, "state", "%u", state
);
192 static const BlockDevOps xen_block_dev_ops
= {
193 .resize_cb
= xen_block_resize_cb
,
196 static void xen_block_realize(XenDevice
*xendev
, Error
**errp
)
199 XenBlockDevice
*blockdev
= XEN_BLOCK_DEVICE(xendev
);
200 XenBlockDeviceClass
*blockdev_class
=
201 XEN_BLOCK_DEVICE_GET_CLASS(xendev
);
202 const char *type
= object_get_typename(OBJECT(blockdev
));
203 XenBlockVdev
*vdev
= &blockdev
->props
.vdev
;
204 BlockConf
*conf
= &blockdev
->props
.conf
;
205 BlockBackend
*blk
= conf
->blk
;
207 if (vdev
->type
== XEN_BLOCK_VDEV_TYPE_INVALID
) {
208 error_setg(errp
, "vdev property not set");
212 trace_xen_block_realize(type
, vdev
->disk
, vdev
->partition
);
214 if (blockdev_class
->realize
) {
215 blockdev_class
->realize(blockdev
, errp
);
222 * The blkif protocol does not deal with removable media, so it must
223 * always be present, even for CDRom devices.
226 if (!blk_is_inserted(blk
)) {
227 error_setg(errp
, "device needs media, but drive is empty");
231 if (!blkconf_apply_backend_options(conf
, blockdev
->info
& VDISK_READONLY
,
236 if (!(blockdev
->info
& VDISK_CDROM
) &&
237 !blkconf_geometry(conf
, NULL
, 65535, 255, 255, errp
)) {
241 if (!blkconf_blocksizes(conf
, errp
)) {
245 blk_set_dev_ops(blk
, &xen_block_dev_ops
, blockdev
);
246 blk_set_guest_block_size(blk
, conf
->logical_block_size
);
248 if (conf
->discard_granularity
== -1) {
249 conf
->discard_granularity
= conf
->physical_block_size
;
252 if (blk_get_flags(blk
) & BDRV_O_UNMAP
) {
253 xen_device_backend_printf(xendev
, "feature-discard", "%u", 1);
254 xen_device_backend_printf(xendev
, "discard-granularity", "%u",
255 conf
->discard_granularity
);
256 xen_device_backend_printf(xendev
, "discard-alignment", "%u", 0);
259 xen_device_backend_printf(xendev
, "feature-flush-cache", "%u", 1);
260 xen_device_backend_printf(xendev
, "max-ring-page-order", "%u",
261 blockdev
->props
.max_ring_page_order
);
262 xen_device_backend_printf(xendev
, "info", "%u", blockdev
->info
);
264 xen_device_frontend_printf(xendev
, "virtual-device", "%lu",
266 xen_device_frontend_printf(xendev
, "device-type", "%s",
267 blockdev
->device_type
);
269 xen_device_backend_printf(xendev
, "sector-size", "%u",
270 conf
->logical_block_size
);
272 xen_block_set_size(blockdev
);
274 blockdev
->dataplane
=
275 xen_block_dataplane_create(xendev
, blk
, conf
->logical_block_size
,
276 blockdev
->props
.iothread
);
279 static void xen_block_frontend_changed(XenDevice
*xendev
,
280 enum xenbus_state frontend_state
,
284 enum xenbus_state backend_state
= xen_device_backend_get_state(xendev
);
286 switch (frontend_state
) {
287 case XenbusStateInitialised
:
288 case XenbusStateConnected
:
289 if (backend_state
== XenbusStateConnected
) {
293 xen_block_disconnect(xendev
, errp
);
298 xen_block_connect(xendev
, errp
);
303 xen_device_backend_set_state(xendev
, XenbusStateConnected
);
306 case XenbusStateClosing
:
307 xen_device_backend_set_state(xendev
, XenbusStateClosing
);
310 case XenbusStateClosed
:
311 case XenbusStateUnknown
:
312 xen_block_disconnect(xendev
, errp
);
317 xen_device_backend_set_state(xendev
, XenbusStateClosed
);
325 static char *disk_to_vbd_name(unsigned int disk
)
327 char *name
, *prefix
= (disk
>= 26) ?
328 disk_to_vbd_name((disk
/ 26) - 1) : g_strdup("");
330 name
= g_strdup_printf("%s%c", prefix
, 'a' + disk
% 26);
336 static void xen_block_get_vdev(Object
*obj
, Visitor
*v
, const char *name
,
337 void *opaque
, Error
**errp
)
339 Property
*prop
= opaque
;
340 XenBlockVdev
*vdev
= object_field_prop_ptr(obj
, prop
);
343 switch (vdev
->type
) {
344 case XEN_BLOCK_VDEV_TYPE_DP
:
345 str
= g_strdup_printf("d%lup%lu", vdev
->disk
, vdev
->partition
);
348 case XEN_BLOCK_VDEV_TYPE_XVD
:
349 case XEN_BLOCK_VDEV_TYPE_HD
:
350 case XEN_BLOCK_VDEV_TYPE_SD
: {
351 char *name
= disk_to_vbd_name(vdev
->disk
);
353 str
= g_strdup_printf("%s%s%lu",
354 (vdev
->type
== XEN_BLOCK_VDEV_TYPE_XVD
) ?
356 (vdev
->type
== XEN_BLOCK_VDEV_TYPE_HD
) ?
359 name
, vdev
->partition
);
364 error_setg(errp
, "invalid vdev type");
368 visit_type_str(v
, name
, &str
, errp
);
372 static int vbd_name_to_disk(const char *name
, const char **endp
,
377 while (*name
!= '\0') {
378 if (!g_ascii_isalpha(*name
) || !g_ascii_islower(*name
)) {
383 n
+= *name
++ - 'a' + 1;
396 static void xen_block_set_vdev(Object
*obj
, Visitor
*v
, const char *name
,
397 void *opaque
, Error
**errp
)
399 Property
*prop
= opaque
;
400 XenBlockVdev
*vdev
= object_field_prop_ptr(obj
, prop
);
404 if (!visit_type_str(v
, name
, &str
, errp
)) {
408 p
= strchr(str
, 'd');
415 vdev
->type
= XEN_BLOCK_VDEV_TYPE_DP
;
416 } else if (strcmp(str
, "xv") == 0) {
417 vdev
->type
= XEN_BLOCK_VDEV_TYPE_XVD
;
418 } else if (strcmp(str
, "h") == 0) {
419 vdev
->type
= XEN_BLOCK_VDEV_TYPE_HD
;
420 } else if (strcmp(str
, "s") == 0) {
421 vdev
->type
= XEN_BLOCK_VDEV_TYPE_SD
;
426 if (vdev
->type
== XEN_BLOCK_VDEV_TYPE_DP
) {
427 if (qemu_strtoul(p
, &end
, 10, &vdev
->disk
)) {
432 if (*(++end
) == '\0') {
437 if (vbd_name_to_disk(p
, &end
, &vdev
->disk
)) {
445 if (qemu_strtoul(p
, &end
, 10, &vdev
->partition
)) {
456 switch (vdev
->type
) {
457 case XEN_BLOCK_VDEV_TYPE_DP
:
458 case XEN_BLOCK_VDEV_TYPE_XVD
:
459 if (vdev
->disk
< (1 << 4) && vdev
->partition
< (1 << 4)) {
460 vdev
->number
= (202 << 8) | (vdev
->disk
<< 4) |
462 } else if (vdev
->disk
< (1 << 20) && vdev
->partition
< (1 << 8)) {
463 vdev
->number
= (1 << 28) | (vdev
->disk
<< 8) |
470 case XEN_BLOCK_VDEV_TYPE_HD
:
471 if ((vdev
->disk
== 0 || vdev
->disk
== 1) &&
472 vdev
->partition
< (1 << 6)) {
473 vdev
->number
= (3 << 8) | (vdev
->disk
<< 6) | vdev
->partition
;
474 } else if ((vdev
->disk
== 2 || vdev
->disk
== 3) &&
475 vdev
->partition
< (1 << 6)) {
476 vdev
->number
= (22 << 8) | ((vdev
->disk
- 2) << 6) |
483 case XEN_BLOCK_VDEV_TYPE_SD
:
484 if (vdev
->disk
< (1 << 4) && vdev
->partition
< (1 << 4)) {
485 vdev
->number
= (8 << 8) | (vdev
->disk
<< 4) | vdev
->partition
;
499 error_setg(errp
, "invalid virtual disk specifier");
501 vdev
->type
= XEN_BLOCK_VDEV_TYPE_INVALID
;
506 * This property deals with 'vdev' names adhering to the Xen VBD naming
507 * scheme described in:
509 * https://xenbits.xen.org/docs/unstable/man/xen-vbd-interface.7.html
511 const PropertyInfo xen_block_prop_vdev
= {
513 .description
= "Virtual Disk specifier: d*p*/xvd*/hd*/sd*",
514 .get
= xen_block_get_vdev
,
515 .set
= xen_block_set_vdev
,
518 static Property xen_block_props
[] = {
519 DEFINE_PROP("vdev", XenBlockDevice
, props
.vdev
,
520 xen_block_prop_vdev
, XenBlockVdev
),
521 DEFINE_BLOCK_PROPERTIES(XenBlockDevice
, props
.conf
),
522 DEFINE_PROP_UINT32("max-ring-page-order", XenBlockDevice
,
523 props
.max_ring_page_order
, 4),
524 DEFINE_PROP_LINK("iothread", XenBlockDevice
, props
.iothread
,
525 TYPE_IOTHREAD
, IOThread
*),
526 DEFINE_PROP_END_OF_LIST()
529 static void xen_block_class_init(ObjectClass
*class, void *data
)
531 DeviceClass
*dev_class
= DEVICE_CLASS(class);
532 XenDeviceClass
*xendev_class
= XEN_DEVICE_CLASS(class);
534 xendev_class
->backend
= "qdisk";
535 xendev_class
->device
= "vbd";
536 xendev_class
->get_name
= xen_block_get_name
;
537 xendev_class
->realize
= xen_block_realize
;
538 xendev_class
->frontend_changed
= xen_block_frontend_changed
;
539 xendev_class
->unrealize
= xen_block_unrealize
;
541 device_class_set_props(dev_class
, xen_block_props
);
544 static const TypeInfo xen_block_type_info
= {
545 .name
= TYPE_XEN_BLOCK_DEVICE
,
546 .parent
= TYPE_XEN_DEVICE
,
547 .instance_size
= sizeof(XenBlockDevice
),
549 .class_size
= sizeof(XenBlockDeviceClass
),
550 .class_init
= xen_block_class_init
,
553 static void xen_disk_unrealize(XenBlockDevice
*blockdev
)
555 trace_xen_disk_unrealize();
558 static void xen_disk_realize(XenBlockDevice
*blockdev
, Error
**errp
)
560 BlockConf
*conf
= &blockdev
->props
.conf
;
562 trace_xen_disk_realize();
564 blockdev
->device_type
= "disk";
567 error_setg(errp
, "drive property not set");
571 blockdev
->info
= blk_supports_write_perm(conf
->blk
) ? 0 : VDISK_READONLY
;
574 static void xen_disk_class_init(ObjectClass
*class, void *data
)
576 DeviceClass
*dev_class
= DEVICE_CLASS(class);
577 XenBlockDeviceClass
*blockdev_class
= XEN_BLOCK_DEVICE_CLASS(class);
579 blockdev_class
->realize
= xen_disk_realize
;
580 blockdev_class
->unrealize
= xen_disk_unrealize
;
582 dev_class
->desc
= "Xen Disk Device";
585 static const TypeInfo xen_disk_type_info
= {
586 .name
= TYPE_XEN_DISK_DEVICE
,
587 .parent
= TYPE_XEN_BLOCK_DEVICE
,
588 .instance_size
= sizeof(XenDiskDevice
),
589 .class_init
= xen_disk_class_init
,
592 static void xen_cdrom_unrealize(XenBlockDevice
*blockdev
)
594 trace_xen_cdrom_unrealize();
597 static void xen_cdrom_realize(XenBlockDevice
*blockdev
, Error
**errp
)
599 BlockConf
*conf
= &blockdev
->props
.conf
;
601 trace_xen_cdrom_realize();
603 blockdev
->device_type
= "cdrom";
608 /* Set up an empty drive */
609 conf
->blk
= blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
611 rc
= blk_attach_dev(conf
->blk
, DEVICE(blockdev
));
613 error_setg_errno(errp
, -rc
, "failed to create drive");
618 blockdev
->info
= VDISK_READONLY
| VDISK_CDROM
;
621 static void xen_cdrom_class_init(ObjectClass
*class, void *data
)
623 DeviceClass
*dev_class
= DEVICE_CLASS(class);
624 XenBlockDeviceClass
*blockdev_class
= XEN_BLOCK_DEVICE_CLASS(class);
626 blockdev_class
->realize
= xen_cdrom_realize
;
627 blockdev_class
->unrealize
= xen_cdrom_unrealize
;
629 dev_class
->desc
= "Xen CD-ROM Device";
632 static const TypeInfo xen_cdrom_type_info
= {
633 .name
= TYPE_XEN_CDROM_DEVICE
,
634 .parent
= TYPE_XEN_BLOCK_DEVICE
,
635 .instance_size
= sizeof(XenCDRomDevice
),
636 .class_init
= xen_cdrom_class_init
,
639 static void xen_block_register_types(void)
641 type_register_static(&xen_block_type_info
);
642 type_register_static(&xen_disk_type_info
);
643 type_register_static(&xen_cdrom_type_info
);
646 type_init(xen_block_register_types
)
648 static void xen_block_blockdev_del(const char *node_name
, Error
**errp
)
650 trace_xen_block_blockdev_del(node_name
);
652 qmp_blockdev_del(node_name
, errp
);
655 static char *xen_block_blockdev_add(const char *id
, QDict
*qdict
,
659 const char *driver
= qdict_get_try_str(qdict
, "driver");
660 BlockdevOptions
*options
= NULL
;
665 error_setg(errp
, "no 'driver' parameter");
669 node_name
= g_strdup_printf("%s-%s", id
, driver
);
670 qdict_put_str(qdict
, "node-name", node_name
);
672 trace_xen_block_blockdev_add(node_name
);
674 v
= qobject_input_visitor_new(QOBJECT(qdict
));
675 visit_type_BlockdevOptions(v
, NULL
, &options
, errp
);
681 qmp_blockdev_add(options
, errp
);
687 qapi_free_BlockdevOptions(options
);
693 qapi_free_BlockdevOptions(options
);
700 static void xen_block_drive_destroy(XenBlockDrive
*drive
, Error
**errp
)
703 char *node_name
= drive
->node_name
;
706 xen_block_blockdev_del(node_name
, errp
);
711 drive
->node_name
= NULL
;
717 static XenBlockDrive
*xen_block_drive_create(const char *id
,
718 const char *device_type
,
719 QDict
*opts
, Error
**errp
)
722 const char *params
= qdict_get_try_str(opts
, "params");
723 const char *mode
= qdict_get_try_str(opts
, "mode");
724 const char *direct_io_safe
= qdict_get_try_str(opts
, "direct-io-safe");
725 const char *discard_enable
= qdict_get_try_str(opts
, "discard-enable");
727 char *filename
= NULL
;
728 XenBlockDrive
*drive
= NULL
;
735 char **v
= g_strsplit(params
, ":", 2);
738 filename
= g_strdup(v
[0]);
739 driver
= g_strdup("raw");
741 if (strcmp(v
[0], "aio") == 0) {
742 driver
= g_strdup("raw");
743 } else if (strcmp(v
[0], "vhd") == 0) {
744 driver
= g_strdup("vpc");
746 driver
= g_strdup(v
[0]);
748 filename
= g_strdup(v
[1]);
753 error_setg(errp
, "no params");
760 drive
= g_new0(XenBlockDrive
, 1);
761 drive
->id
= g_strdup(id
);
763 file_layer
= qdict_new();
764 driver_layer
= qdict_new();
766 rc
= stat(filename
, &st
);
768 error_setg_errno(errp
, errno
, "Could not stat file '%s'", filename
);
771 if (S_ISBLK(st
.st_mode
)) {
772 qdict_put_str(file_layer
, "driver", "host_device");
774 qdict_put_str(file_layer
, "driver", "file");
777 qdict_put_str(file_layer
, "filename", filename
);
780 if (mode
&& *mode
!= 'w') {
781 qdict_put_bool(file_layer
, "read-only", true);
784 if (direct_io_safe
) {
787 if (!qemu_strtoul(direct_io_safe
, NULL
, 2, &value
) && !!value
) {
788 QDict
*cache_qdict
= qdict_new();
790 qdict_put_bool(cache_qdict
, "direct", true);
791 qdict_put(file_layer
, "cache", cache_qdict
);
793 qdict_put_str(file_layer
, "aio", "native");
797 if (discard_enable
) {
800 if (!qemu_strtoul(discard_enable
, NULL
, 2, &value
) && !!value
) {
801 qdict_put_str(file_layer
, "discard", "unmap");
802 qdict_put_str(driver_layer
, "discard", "unmap");
807 * It is necessary to turn file locking off as an emulated device
808 * may have already opened the same image file.
810 qdict_put_str(file_layer
, "locking", "off");
812 qdict_put_str(driver_layer
, "driver", driver
);
815 qdict_put(driver_layer
, "file", file_layer
);
817 g_assert(!drive
->node_name
);
818 drive
->node_name
= xen_block_blockdev_add(drive
->id
, driver_layer
,
821 qobject_unref(driver_layer
);
825 xen_block_drive_destroy(drive
, NULL
);
832 static const char *xen_block_drive_get_node_name(XenBlockDrive
*drive
)
834 return drive
->node_name
? drive
->node_name
: "";
837 static void xen_block_iothread_destroy(XenBlockIOThread
*iothread
,
840 qmp_object_del(iothread
->id
, errp
);
842 g_free(iothread
->id
);
846 static XenBlockIOThread
*xen_block_iothread_create(const char *id
,
850 XenBlockIOThread
*iothread
= g_new(XenBlockIOThread
, 1);
853 iothread
->id
= g_strdup(id
);
855 opts
= g_new(ObjectOptions
, 1);
856 *opts
= (ObjectOptions
) {
857 .qom_type
= OBJECT_TYPE_IOTHREAD
,
860 qmp_object_add(opts
, errp
);
861 qapi_free_ObjectOptions(opts
);
864 g_free(iothread
->id
);
872 static void xen_block_device_create(XenBackendInstance
*backend
,
873 QDict
*opts
, Error
**errp
)
876 XenBus
*xenbus
= xen_backend_get_bus(backend
);
877 const char *name
= xen_backend_get_name(backend
);
878 unsigned long number
;
879 const char *vdev
, *device_type
;
880 XenBlockDrive
*drive
= NULL
;
881 XenBlockIOThread
*iothread
= NULL
;
882 XenDevice
*xendev
= NULL
;
884 XenBlockDevice
*blockdev
;
886 if (qemu_strtoul(name
, NULL
, 10, &number
)) {
887 error_setg(errp
, "failed to parse name '%s'", name
);
891 trace_xen_block_device_create(number
);
893 vdev
= qdict_get_try_str(opts
, "dev");
895 error_setg(errp
, "no dev parameter");
899 device_type
= qdict_get_try_str(opts
, "device-type");
901 error_setg(errp
, "no device-type parameter");
905 if (!strcmp(device_type
, "disk")) {
906 type
= TYPE_XEN_DISK_DEVICE
;
907 } else if (!strcmp(device_type
, "cdrom")) {
908 type
= TYPE_XEN_CDROM_DEVICE
;
910 error_setg(errp
, "invalid device-type parameter '%s'", device_type
);
914 drive
= xen_block_drive_create(vdev
, device_type
, opts
, errp
);
916 error_prepend(errp
, "failed to create drive: ");
920 iothread
= xen_block_iothread_create(vdev
, errp
);
922 error_prepend(errp
, "failed to create iothread: ");
926 xendev
= XEN_DEVICE(qdev_new(type
));
927 blockdev
= XEN_BLOCK_DEVICE(xendev
);
929 if (!object_property_set_str(OBJECT(xendev
), "vdev", vdev
,
931 error_prepend(errp
, "failed to set 'vdev': ");
935 if (!object_property_set_str(OBJECT(xendev
), "drive",
936 xen_block_drive_get_node_name(drive
),
938 error_prepend(errp
, "failed to set 'drive': ");
942 if (!object_property_set_str(OBJECT(xendev
), "iothread", iothread
->id
,
944 error_prepend(errp
, "failed to set 'iothread': ");
948 blockdev
->iothread
= iothread
;
949 blockdev
->drive
= drive
;
951 if (!qdev_realize_and_unref(DEVICE(xendev
), BUS(xenbus
), errp
)) {
952 error_prepend(errp
, "realization of device %s failed: ", type
);
956 xen_backend_set_device(backend
, xendev
);
961 object_unparent(OBJECT(xendev
));
965 xen_block_iothread_destroy(iothread
, NULL
);
969 xen_block_drive_destroy(drive
, NULL
);
973 static void xen_block_device_destroy(XenBackendInstance
*backend
,
977 XenDevice
*xendev
= xen_backend_get_device(backend
);
978 XenBlockDevice
*blockdev
= XEN_BLOCK_DEVICE(xendev
);
979 XenBlockVdev
*vdev
= &blockdev
->props
.vdev
;
980 XenBlockDrive
*drive
= blockdev
->drive
;
981 XenBlockIOThread
*iothread
= blockdev
->iothread
;
983 trace_xen_block_device_destroy(vdev
->number
);
985 object_unparent(OBJECT(xendev
));
988 * Drain all pending RCU callbacks as object_unparent() frees `xendev'
990 * And due to the property "drive" still existing in `xendev', we
991 * can't destroy the XenBlockDrive associated with `xendev' with
992 * xen_block_drive_destroy() below.
997 xen_block_iothread_destroy(iothread
, errp
);
999 error_prepend(errp
, "failed to destroy iothread: ");
1005 xen_block_drive_destroy(drive
, errp
);
1007 error_prepend(errp
, "failed to destroy drive: ");
1013 static const XenBackendInfo xen_block_backend_info
= {
1015 .create
= xen_block_device_create
,
1016 .destroy
= xen_block_device_destroy
,
1019 static void xen_block_register_backend(void)
1021 xen_backend_register(&xen_block_backend_info
);
1024 xen_backend_init(xen_block_register_backend
);