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
, Error
**errp
)
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
, errp
);
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
)
198 XenBlockDevice
*blockdev
= XEN_BLOCK_DEVICE(xendev
);
199 XenBlockDeviceClass
*blockdev_class
=
200 XEN_BLOCK_DEVICE_GET_CLASS(xendev
);
201 const char *type
= object_get_typename(OBJECT(blockdev
));
202 XenBlockVdev
*vdev
= &blockdev
->props
.vdev
;
203 BlockConf
*conf
= &blockdev
->props
.conf
;
204 BlockBackend
*blk
= conf
->blk
;
205 Error
*local_err
= NULL
;
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
, &local_err
);
217 error_propagate(errp
, local_err
);
223 * The blkif protocol does not deal with removable media, so it must
224 * always be present, even for CDRom devices.
227 if (!blk_is_inserted(blk
)) {
228 error_setg(errp
, "device needs media, but drive is empty");
232 if (!blkconf_apply_backend_options(conf
, blockdev
->info
& VDISK_READONLY
,
237 if (!(blockdev
->info
& VDISK_CDROM
) &&
238 !blkconf_geometry(conf
, NULL
, 65535, 255, 255, errp
)) {
242 blkconf_blocksizes(conf
);
244 if (conf
->logical_block_size
> conf
->physical_block_size
) {
246 errp
, "logical_block_size > physical_block_size not supported");
250 blk_set_dev_ops(blk
, &xen_block_dev_ops
, blockdev
);
251 blk_set_guest_block_size(blk
, conf
->logical_block_size
);
253 if (conf
->discard_granularity
== -1) {
254 conf
->discard_granularity
= conf
->physical_block_size
;
257 if (blk_get_flags(blk
) & BDRV_O_UNMAP
) {
258 xen_device_backend_printf(xendev
, "feature-discard", "%u", 1);
259 xen_device_backend_printf(xendev
, "discard-granularity", "%u",
260 conf
->discard_granularity
);
263 xen_device_backend_printf(xendev
, "feature-flush-cache", "%u", 1);
264 xen_device_backend_printf(xendev
, "max-ring-page-order", "%u",
265 blockdev
->props
.max_ring_page_order
);
266 xen_device_backend_printf(xendev
, "info", "%u", blockdev
->info
);
268 xen_device_frontend_printf(xendev
, "virtual-device", "%lu",
270 xen_device_frontend_printf(xendev
, "device-type", "%s",
271 blockdev
->device_type
);
273 xen_device_backend_printf(xendev
, "sector-size", "%u",
274 conf
->logical_block_size
);
276 xen_block_set_size(blockdev
);
278 blockdev
->dataplane
=
279 xen_block_dataplane_create(xendev
, blk
, conf
->logical_block_size
,
280 blockdev
->props
.iothread
);
283 static void xen_block_frontend_changed(XenDevice
*xendev
,
284 enum xenbus_state frontend_state
,
287 enum xenbus_state backend_state
= xen_device_backend_get_state(xendev
);
288 Error
*local_err
= NULL
;
290 switch (frontend_state
) {
291 case XenbusStateInitialised
:
292 case XenbusStateConnected
:
293 if (backend_state
== XenbusStateConnected
) {
297 xen_block_disconnect(xendev
, &local_err
);
299 error_propagate(errp
, local_err
);
303 xen_block_connect(xendev
, &local_err
);
305 error_propagate(errp
, local_err
);
309 xen_device_backend_set_state(xendev
, XenbusStateConnected
);
312 case XenbusStateClosing
:
313 xen_device_backend_set_state(xendev
, XenbusStateClosing
);
316 case XenbusStateClosed
:
317 case XenbusStateUnknown
:
318 xen_block_disconnect(xendev
, &local_err
);
320 error_propagate(errp
, local_err
);
324 xen_device_backend_set_state(xendev
, XenbusStateClosed
);
332 static char *disk_to_vbd_name(unsigned int disk
)
334 char *name
, *prefix
= (disk
>= 26) ?
335 disk_to_vbd_name((disk
/ 26) - 1) : g_strdup("");
337 name
= g_strdup_printf("%s%c", prefix
, 'a' + disk
% 26);
343 static void xen_block_get_vdev(Object
*obj
, Visitor
*v
, const char *name
,
344 void *opaque
, Error
**errp
)
346 DeviceState
*dev
= DEVICE(obj
);
347 Property
*prop
= opaque
;
348 XenBlockVdev
*vdev
= qdev_get_prop_ptr(dev
, prop
);
351 switch (vdev
->type
) {
352 case XEN_BLOCK_VDEV_TYPE_DP
:
353 str
= g_strdup_printf("d%lup%lu", vdev
->disk
, vdev
->partition
);
356 case XEN_BLOCK_VDEV_TYPE_XVD
:
357 case XEN_BLOCK_VDEV_TYPE_HD
:
358 case XEN_BLOCK_VDEV_TYPE_SD
: {
359 char *name
= disk_to_vbd_name(vdev
->disk
);
361 str
= g_strdup_printf("%s%s%lu",
362 (vdev
->type
== XEN_BLOCK_VDEV_TYPE_XVD
) ?
364 (vdev
->type
== XEN_BLOCK_VDEV_TYPE_HD
) ?
367 name
, vdev
->partition
);
372 error_setg(errp
, "invalid vdev type");
376 visit_type_str(v
, name
, &str
, errp
);
380 static int vbd_name_to_disk(const char *name
, const char **endp
,
385 while (*name
!= '\0') {
386 if (!g_ascii_isalpha(*name
) || !g_ascii_islower(*name
)) {
391 n
+= *name
++ - 'a' + 1;
404 static void xen_block_set_vdev(Object
*obj
, Visitor
*v
, const char *name
,
405 void *opaque
, Error
**errp
)
407 DeviceState
*dev
= DEVICE(obj
);
408 Property
*prop
= opaque
;
409 XenBlockVdev
*vdev
= qdev_get_prop_ptr(dev
, prop
);
410 Error
*local_err
= NULL
;
415 qdev_prop_set_after_realize(dev
, name
, errp
);
419 visit_type_str(v
, name
, &str
, &local_err
);
421 error_propagate(errp
, local_err
);
425 p
= strchr(str
, 'd');
432 vdev
->type
= XEN_BLOCK_VDEV_TYPE_DP
;
433 } else if (strcmp(str
, "xv") == 0) {
434 vdev
->type
= XEN_BLOCK_VDEV_TYPE_XVD
;
435 } else if (strcmp(str
, "h") == 0) {
436 vdev
->type
= XEN_BLOCK_VDEV_TYPE_HD
;
437 } else if (strcmp(str
, "s") == 0) {
438 vdev
->type
= XEN_BLOCK_VDEV_TYPE_SD
;
443 if (vdev
->type
== XEN_BLOCK_VDEV_TYPE_DP
) {
444 if (qemu_strtoul(p
, &end
, 10, &vdev
->disk
)) {
449 if (*(++end
) == '\0') {
454 if (vbd_name_to_disk(p
, &end
, &vdev
->disk
)) {
462 if (qemu_strtoul(p
, &end
, 10, &vdev
->partition
)) {
473 switch (vdev
->type
) {
474 case XEN_BLOCK_VDEV_TYPE_DP
:
475 case XEN_BLOCK_VDEV_TYPE_XVD
:
476 if (vdev
->disk
< (1 << 4) && vdev
->partition
< (1 << 4)) {
477 vdev
->number
= (202 << 8) | (vdev
->disk
<< 4) |
479 } else if (vdev
->disk
< (1 << 20) && vdev
->partition
< (1 << 8)) {
480 vdev
->number
= (1 << 28) | (vdev
->disk
<< 8) |
487 case XEN_BLOCK_VDEV_TYPE_HD
:
488 if ((vdev
->disk
== 0 || vdev
->disk
== 1) &&
489 vdev
->partition
< (1 << 6)) {
490 vdev
->number
= (3 << 8) | (vdev
->disk
<< 6) | vdev
->partition
;
491 } else if ((vdev
->disk
== 2 || vdev
->disk
== 3) &&
492 vdev
->partition
< (1 << 6)) {
493 vdev
->number
= (22 << 8) | ((vdev
->disk
- 2) << 6) |
500 case XEN_BLOCK_VDEV_TYPE_SD
:
501 if (vdev
->disk
< (1 << 4) && vdev
->partition
< (1 << 4)) {
502 vdev
->number
= (8 << 8) | (vdev
->disk
<< 4) | vdev
->partition
;
516 error_setg(errp
, "invalid virtual disk specifier");
518 vdev
->type
= XEN_BLOCK_VDEV_TYPE_INVALID
;
523 * This property deals with 'vdev' names adhering to the Xen VBD naming
524 * scheme described in:
526 * https://xenbits.xen.org/docs/unstable/man/xen-vbd-interface.7.html
528 const PropertyInfo xen_block_prop_vdev
= {
530 .description
= "Virtual Disk specifier: d*p*/xvd*/hd*/sd*",
531 .get
= xen_block_get_vdev
,
532 .set
= xen_block_set_vdev
,
535 static Property xen_block_props
[] = {
536 DEFINE_PROP("vdev", XenBlockDevice
, props
.vdev
,
537 xen_block_prop_vdev
, XenBlockVdev
),
538 DEFINE_BLOCK_PROPERTIES(XenBlockDevice
, props
.conf
),
539 DEFINE_PROP_UINT32("max-ring-page-order", XenBlockDevice
,
540 props
.max_ring_page_order
, 4),
541 DEFINE_PROP_LINK("iothread", XenBlockDevice
, props
.iothread
,
542 TYPE_IOTHREAD
, IOThread
*),
543 DEFINE_PROP_END_OF_LIST()
546 static void xen_block_class_init(ObjectClass
*class, void *data
)
548 DeviceClass
*dev_class
= DEVICE_CLASS(class);
549 XenDeviceClass
*xendev_class
= XEN_DEVICE_CLASS(class);
551 xendev_class
->backend
= "qdisk";
552 xendev_class
->device
= "vbd";
553 xendev_class
->get_name
= xen_block_get_name
;
554 xendev_class
->realize
= xen_block_realize
;
555 xendev_class
->frontend_changed
= xen_block_frontend_changed
;
556 xendev_class
->unrealize
= xen_block_unrealize
;
558 device_class_set_props(dev_class
, xen_block_props
);
561 static const TypeInfo xen_block_type_info
= {
562 .name
= TYPE_XEN_BLOCK_DEVICE
,
563 .parent
= TYPE_XEN_DEVICE
,
564 .instance_size
= sizeof(XenBlockDevice
),
566 .class_size
= sizeof(XenBlockDeviceClass
),
567 .class_init
= xen_block_class_init
,
570 static void xen_disk_unrealize(XenBlockDevice
*blockdev
, Error
**errp
)
572 trace_xen_disk_unrealize();
575 static void xen_disk_realize(XenBlockDevice
*blockdev
, Error
**errp
)
577 BlockConf
*conf
= &blockdev
->props
.conf
;
579 trace_xen_disk_realize();
581 blockdev
->device_type
= "disk";
584 error_setg(errp
, "drive property not set");
588 blockdev
->info
= blk_is_read_only(conf
->blk
) ? VDISK_READONLY
: 0;
591 static void xen_disk_class_init(ObjectClass
*class, void *data
)
593 DeviceClass
*dev_class
= DEVICE_CLASS(class);
594 XenBlockDeviceClass
*blockdev_class
= XEN_BLOCK_DEVICE_CLASS(class);
596 blockdev_class
->realize
= xen_disk_realize
;
597 blockdev_class
->unrealize
= xen_disk_unrealize
;
599 dev_class
->desc
= "Xen Disk Device";
602 static const TypeInfo xen_disk_type_info
= {
603 .name
= TYPE_XEN_DISK_DEVICE
,
604 .parent
= TYPE_XEN_BLOCK_DEVICE
,
605 .instance_size
= sizeof(XenDiskDevice
),
606 .class_init
= xen_disk_class_init
,
609 static void xen_cdrom_unrealize(XenBlockDevice
*blockdev
, Error
**errp
)
611 trace_xen_cdrom_unrealize();
614 static void xen_cdrom_realize(XenBlockDevice
*blockdev
, Error
**errp
)
616 BlockConf
*conf
= &blockdev
->props
.conf
;
618 trace_xen_cdrom_realize();
620 blockdev
->device_type
= "cdrom";
625 /* Set up an empty drive */
626 conf
->blk
= blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
628 rc
= blk_attach_dev(conf
->blk
, DEVICE(blockdev
));
630 error_setg_errno(errp
, -rc
, "failed to create drive");
635 blockdev
->info
= VDISK_READONLY
| VDISK_CDROM
;
638 static void xen_cdrom_class_init(ObjectClass
*class, void *data
)
640 DeviceClass
*dev_class
= DEVICE_CLASS(class);
641 XenBlockDeviceClass
*blockdev_class
= XEN_BLOCK_DEVICE_CLASS(class);
643 blockdev_class
->realize
= xen_cdrom_realize
;
644 blockdev_class
->unrealize
= xen_cdrom_unrealize
;
646 dev_class
->desc
= "Xen CD-ROM Device";
649 static const TypeInfo xen_cdrom_type_info
= {
650 .name
= TYPE_XEN_CDROM_DEVICE
,
651 .parent
= TYPE_XEN_BLOCK_DEVICE
,
652 .instance_size
= sizeof(XenCDRomDevice
),
653 .class_init
= xen_cdrom_class_init
,
656 static void xen_block_register_types(void)
658 type_register_static(&xen_block_type_info
);
659 type_register_static(&xen_disk_type_info
);
660 type_register_static(&xen_cdrom_type_info
);
663 type_init(xen_block_register_types
)
665 static void xen_block_blockdev_del(const char *node_name
, Error
**errp
)
667 trace_xen_block_blockdev_del(node_name
);
669 qmp_blockdev_del(node_name
, errp
);
672 static char *xen_block_blockdev_add(const char *id
, QDict
*qdict
,
675 const char *driver
= qdict_get_try_str(qdict
, "driver");
676 BlockdevOptions
*options
= NULL
;
677 Error
*local_err
= NULL
;
682 error_setg(errp
, "no 'driver' parameter");
686 node_name
= g_strdup_printf("%s-%s", id
, driver
);
687 qdict_put_str(qdict
, "node-name", node_name
);
689 trace_xen_block_blockdev_add(node_name
);
691 v
= qobject_input_visitor_new(QOBJECT(qdict
));
692 visit_type_BlockdevOptions(v
, NULL
, &options
, &local_err
);
696 error_propagate(errp
, local_err
);
700 qmp_blockdev_add(options
, &local_err
);
703 error_propagate(errp
, local_err
);
707 qapi_free_BlockdevOptions(options
);
713 qapi_free_BlockdevOptions(options
);
720 static void xen_block_drive_destroy(XenBlockDrive
*drive
, Error
**errp
)
722 char *node_name
= drive
->node_name
;
725 Error
*local_err
= NULL
;
727 xen_block_blockdev_del(node_name
, &local_err
);
729 error_propagate(errp
, local_err
);
733 drive
->node_name
= NULL
;
739 static XenBlockDrive
*xen_block_drive_create(const char *id
,
740 const char *device_type
,
741 QDict
*opts
, Error
**errp
)
743 const char *params
= qdict_get_try_str(opts
, "params");
744 const char *mode
= qdict_get_try_str(opts
, "mode");
745 const char *direct_io_safe
= qdict_get_try_str(opts
, "direct-io-safe");
746 const char *discard_enable
= qdict_get_try_str(opts
, "discard-enable");
748 char *filename
= NULL
;
749 XenBlockDrive
*drive
= NULL
;
750 Error
*local_err
= NULL
;
755 char **v
= g_strsplit(params
, ":", 2);
758 filename
= g_strdup(v
[0]);
759 driver
= g_strdup("raw");
761 if (strcmp(v
[0], "aio") == 0) {
762 driver
= g_strdup("raw");
763 } else if (strcmp(v
[0], "vhd") == 0) {
764 driver
= g_strdup("vpc");
766 driver
= g_strdup(v
[0]);
768 filename
= g_strdup(v
[1]);
773 error_setg(errp
, "no params");
780 drive
= g_new0(XenBlockDrive
, 1);
781 drive
->id
= g_strdup(id
);
783 file_layer
= qdict_new();
784 driver_layer
= qdict_new();
786 qdict_put_str(file_layer
, "driver", "file");
787 qdict_put_str(file_layer
, "filename", filename
);
790 if (mode
&& *mode
!= 'w') {
791 qdict_put_bool(file_layer
, "read-only", true);
794 if (direct_io_safe
) {
797 if (!qemu_strtoul(direct_io_safe
, NULL
, 2, &value
) && !!value
) {
798 QDict
*cache_qdict
= qdict_new();
800 qdict_put_bool(cache_qdict
, "direct", true);
801 qdict_put(file_layer
, "cache", cache_qdict
);
803 qdict_put_str(file_layer
, "aio", "native");
807 if (discard_enable
) {
810 if (!qemu_strtoul(discard_enable
, NULL
, 2, &value
) && !!value
) {
811 qdict_put_str(file_layer
, "discard", "unmap");
812 qdict_put_str(driver_layer
, "discard", "unmap");
817 * It is necessary to turn file locking off as an emulated device
818 * may have already opened the same image file.
820 qdict_put_str(file_layer
, "locking", "off");
822 qdict_put_str(driver_layer
, "driver", driver
);
825 qdict_put(driver_layer
, "file", file_layer
);
827 g_assert(!drive
->node_name
);
828 drive
->node_name
= xen_block_blockdev_add(drive
->id
, driver_layer
,
831 qobject_unref(driver_layer
);
835 error_propagate(errp
, local_err
);
836 xen_block_drive_destroy(drive
, NULL
);
843 static const char *xen_block_drive_get_node_name(XenBlockDrive
*drive
)
845 return drive
->node_name
? drive
->node_name
: "";
848 static void xen_block_iothread_destroy(XenBlockIOThread
*iothread
,
851 qmp_object_del(iothread
->id
, errp
);
853 g_free(iothread
->id
);
857 static XenBlockIOThread
*xen_block_iothread_create(const char *id
,
860 XenBlockIOThread
*iothread
= g_new(XenBlockIOThread
, 1);
861 Error
*local_err
= NULL
;
863 QObject
*ret_data
= NULL
;
865 iothread
->id
= g_strdup(id
);
868 qdict_put_str(opts
, "qom-type", TYPE_IOTHREAD
);
869 qdict_put_str(opts
, "id", id
);
870 qmp_object_add(opts
, &ret_data
, &local_err
);
872 qobject_unref(ret_data
);
875 error_propagate(errp
, local_err
);
877 g_free(iothread
->id
);
885 static void xen_block_device_create(XenBackendInstance
*backend
,
886 QDict
*opts
, Error
**errp
)
888 XenBus
*xenbus
= xen_backend_get_bus(backend
);
889 const char *name
= xen_backend_get_name(backend
);
890 unsigned long number
;
891 const char *vdev
, *device_type
;
892 XenBlockDrive
*drive
= NULL
;
893 XenBlockIOThread
*iothread
= NULL
;
894 XenDevice
*xendev
= NULL
;
895 Error
*local_err
= NULL
;
897 XenBlockDevice
*blockdev
;
899 if (qemu_strtoul(name
, NULL
, 10, &number
)) {
900 error_setg(errp
, "failed to parse name '%s'", name
);
904 trace_xen_block_device_create(number
);
906 vdev
= qdict_get_try_str(opts
, "dev");
908 error_setg(errp
, "no dev parameter");
912 device_type
= qdict_get_try_str(opts
, "device-type");
914 error_setg(errp
, "no device-type parameter");
918 if (!strcmp(device_type
, "disk")) {
919 type
= TYPE_XEN_DISK_DEVICE
;
920 } else if (!strcmp(device_type
, "cdrom")) {
921 type
= TYPE_XEN_CDROM_DEVICE
;
923 error_setg(errp
, "invalid device-type parameter '%s'", device_type
);
927 drive
= xen_block_drive_create(vdev
, device_type
, opts
, &local_err
);
929 error_propagate_prepend(errp
, local_err
, "failed to create drive: ");
933 iothread
= xen_block_iothread_create(vdev
, &local_err
);
935 error_propagate_prepend(errp
, local_err
,
936 "failed to create iothread: ");
940 xendev
= XEN_DEVICE(qdev_create(BUS(xenbus
), type
));
941 blockdev
= XEN_BLOCK_DEVICE(xendev
);
943 object_property_set_str(OBJECT(xendev
), vdev
, "vdev", &local_err
);
945 error_propagate_prepend(errp
, local_err
, "failed to set 'vdev': ");
949 object_property_set_str(OBJECT(xendev
),
950 xen_block_drive_get_node_name(drive
), "drive",
953 error_propagate_prepend(errp
, local_err
, "failed to set 'drive': ");
957 object_property_set_str(OBJECT(xendev
), iothread
->id
, "iothread",
960 error_propagate_prepend(errp
, local_err
,
961 "failed to set 'iothread': ");
965 blockdev
->iothread
= iothread
;
966 blockdev
->drive
= drive
;
968 object_property_set_bool(OBJECT(xendev
), true, "realized", &local_err
);
970 error_propagate_prepend(errp
, local_err
,
971 "realization of device %s failed: ",
976 xen_backend_set_device(backend
, xendev
);
981 object_unparent(OBJECT(xendev
));
985 xen_block_iothread_destroy(iothread
, NULL
);
989 xen_block_drive_destroy(drive
, NULL
);
993 static void xen_block_device_destroy(XenBackendInstance
*backend
,
996 XenDevice
*xendev
= xen_backend_get_device(backend
);
997 XenBlockDevice
*blockdev
= XEN_BLOCK_DEVICE(xendev
);
998 XenBlockVdev
*vdev
= &blockdev
->props
.vdev
;
999 XenBlockDrive
*drive
= blockdev
->drive
;
1000 XenBlockIOThread
*iothread
= blockdev
->iothread
;
1001 Error
*local_err
= NULL
;
1003 trace_xen_block_device_destroy(vdev
->number
);
1005 object_unparent(OBJECT(xendev
));
1008 xen_block_iothread_destroy(iothread
, &local_err
);
1010 error_propagate_prepend(errp
, local_err
,
1011 "failed to destroy iothread: ");
1017 xen_block_drive_destroy(drive
, &local_err
);
1019 error_propagate_prepend(errp
, local_err
,
1020 "failed to destroy drive: ");
1026 static const XenBackendInfo xen_block_backend_info
= {
1028 .create
= xen_block_device_create
,
1029 .destroy
= xen_block_device_destroy
,
1032 static void xen_block_register_backend(void)
1034 xen_backend_register(&xen_block_backend_info
);
1037 xen_backend_init(xen_block_register_backend
);