target/arm: Remove fp_status from helper_{recpe, rsqrte}_u32
[qemu/ar7.git] / hw / block / xen-block.c
blob99cb4c67cb09aa2bd8f12982984fc4560cc20aba
1 /*
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.
6 */
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"
31 #include "trace.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;
60 char *str;
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);
73 return;
76 if (xen_device_frontend_scanf(xendev, "ring-page-order", "%u",
77 &order) != 1) {
78 nr_ring_ref = 1;
79 ring_ref = g_new(unsigned int, nr_ring_ref);
81 if (xen_device_frontend_scanf(xendev, "ring-ref", "%u",
82 &ring_ref[0]) != 1) {
83 error_setg(errp, "failed to read ring-ref");
84 g_free(ring_ref);
85 return;
87 } else if (order <= blockdev->props.max_ring_page_order) {
88 unsigned int i;
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",
97 &ring_ref[i]) != 1) {
98 error_setg(errp, "failed to read %s", key);
99 g_free((gpointer)key);
100 g_free(ring_ref);
101 return;
104 g_free((gpointer)key);
106 } else {
107 error_setg(errp, "invalid ring-page-order (%d)", order);
108 return;
111 if (xen_device_frontend_scanf(xendev, "event-channel", "%u",
112 &event_channel) != 1) {
113 error_setg(errp, "failed to read event-channel");
114 g_free(ring_ref);
115 return;
118 if (xen_device_frontend_scanf(xendev, "protocol", "%ms",
119 &str) != 1) {
120 protocol = BLKIF_PROTOCOL_NATIVE;
121 } else {
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;
126 } else {
127 protocol = BLKIF_PROTOCOL_NATIVE;
130 free(str);
133 xen_block_dataplane_start(blockdev->dataplane, ring_ref, nr_ring_ref,
134 event_channel, protocol, errp);
136 g_free(ring_ref);
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) {
148 return;
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");
209 return;
212 trace_xen_block_realize(type, vdev->disk, vdev->partition);
214 if (blockdev_class->realize) {
215 blockdev_class->realize(blockdev, &local_err);
216 if (local_err) {
217 error_propagate(errp, local_err);
218 return;
223 * The blkif protocol does not deal with removable media, so it must
224 * always be present, even for CDRom devices.
226 assert(blk);
227 if (!blk_is_inserted(blk)) {
228 error_setg(errp, "device needs media, but drive is empty");
229 return;
232 if (!blkconf_apply_backend_options(conf, blockdev->info & VDISK_READONLY,
233 true, errp)) {
234 return;
237 if (!(blockdev->info & VDISK_CDROM) &&
238 !blkconf_geometry(conf, NULL, 65535, 255, 255, errp)) {
239 return;
242 blkconf_blocksizes(conf);
244 if (conf->logical_block_size > conf->physical_block_size) {
245 error_setg(
246 errp, "logical_block_size > physical_block_size not supported");
247 return;
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",
269 vdev->number);
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,
285 Error **errp)
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) {
294 break;
297 xen_block_disconnect(xendev, &local_err);
298 if (local_err) {
299 error_propagate(errp, local_err);
300 break;
303 xen_block_connect(xendev, &local_err);
304 if (local_err) {
305 error_propagate(errp, local_err);
306 break;
309 xen_device_backend_set_state(xendev, XenbusStateConnected);
310 break;
312 case XenbusStateClosing:
313 xen_device_backend_set_state(xendev, XenbusStateClosing);
314 break;
316 case XenbusStateClosed:
317 case XenbusStateUnknown:
318 xen_block_disconnect(xendev, &local_err);
319 if (local_err) {
320 error_propagate(errp, local_err);
321 break;
324 xen_device_backend_set_state(xendev, XenbusStateClosed);
325 break;
327 default:
328 break;
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);
338 g_free(prefix);
340 return name;
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);
349 char *str;
351 switch (vdev->type) {
352 case XEN_BLOCK_VDEV_TYPE_DP:
353 str = g_strdup_printf("d%lup%lu", vdev->disk, vdev->partition);
354 break;
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) ?
363 "xvd" :
364 (vdev->type == XEN_BLOCK_VDEV_TYPE_HD) ?
365 "hd" :
366 "sd",
367 name, vdev->partition);
368 g_free(name);
369 break;
371 default:
372 error_setg(errp, "invalid vdev type");
373 return;
376 visit_type_str(v, name, &str, errp);
377 g_free(str);
380 static int vbd_name_to_disk(const char *name, const char **endp,
381 unsigned long *disk)
383 unsigned int n = 0;
385 while (*name != '\0') {
386 if (!g_ascii_isalpha(*name) || !g_ascii_islower(*name)) {
387 break;
390 n *= 26;
391 n += *name++ - 'a' + 1;
393 *endp = name;
395 if (!n) {
396 return -1;
399 *disk = n - 1;
401 return 0;
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;
411 char *str, *p;
412 const char *end;
414 if (dev->realized) {
415 qdev_prop_set_after_realize(dev, name, errp);
416 return;
419 visit_type_str(v, name, &str, &local_err);
420 if (local_err) {
421 error_propagate(errp, local_err);
422 return;
425 p = strchr(str, 'd');
426 if (!p) {
427 goto invalid;
430 *p++ = '\0';
431 if (*str == '\0') {
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;
439 } else {
440 goto invalid;
443 if (vdev->type == XEN_BLOCK_VDEV_TYPE_DP) {
444 if (qemu_strtoul(p, &end, 10, &vdev->disk)) {
445 goto invalid;
448 if (*end == 'p') {
449 if (*(++end) == '\0') {
450 goto invalid;
453 } else {
454 if (vbd_name_to_disk(p, &end, &vdev->disk)) {
455 goto invalid;
459 if (*end != '\0') {
460 p = (char *)end;
462 if (qemu_strtoul(p, &end, 10, &vdev->partition)) {
463 goto invalid;
466 if (*end != '\0') {
467 goto invalid;
469 } else {
470 vdev->partition = 0;
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) |
478 vdev->partition;
479 } else if (vdev->disk < (1 << 20) && vdev->partition < (1 << 8)) {
480 vdev->number = (1 << 28) | (vdev->disk << 8) |
481 vdev->partition;
482 } else {
483 goto invalid;
485 break;
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) |
494 vdev->partition;
495 } else {
496 goto invalid;
498 break;
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;
503 } else {
504 goto invalid;
506 break;
508 default:
509 goto invalid;
512 g_free(str);
513 return;
515 invalid:
516 error_setg(errp, "invalid virtual disk specifier");
518 vdev->type = XEN_BLOCK_VDEV_TYPE_INVALID;
519 g_free(str);
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 = {
529 .name = "str",
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),
565 .abstract = true,
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";
583 if (!conf->blk) {
584 error_setg(errp, "drive property not set");
585 return;
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";
622 if (!conf->blk) {
623 int rc;
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));
629 if (!rc) {
630 error_setg_errno(errp, -rc, "failed to create drive");
631 return;
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,
673 Error **errp)
675 const char *driver = qdict_get_try_str(qdict, "driver");
676 BlockdevOptions *options = NULL;
677 Error *local_err = NULL;
678 char *node_name;
679 Visitor *v;
681 if (!driver) {
682 error_setg(errp, "no 'driver' parameter");
683 return NULL;
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);
693 visit_free(v);
695 if (local_err) {
696 error_propagate(errp, local_err);
697 goto fail;
700 qmp_blockdev_add(options, &local_err);
702 if (local_err) {
703 error_propagate(errp, local_err);
704 goto fail;
707 qapi_free_BlockdevOptions(options);
709 return node_name;
711 fail:
712 if (options) {
713 qapi_free_BlockdevOptions(options);
715 g_free(node_name);
717 return NULL;
720 static void xen_block_drive_destroy(XenBlockDrive *drive, Error **errp)
722 char *node_name = drive->node_name;
724 if (node_name) {
725 Error *local_err = NULL;
727 xen_block_blockdev_del(node_name, &local_err);
728 if (local_err) {
729 error_propagate(errp, local_err);
730 return;
732 g_free(node_name);
733 drive->node_name = NULL;
735 g_free(drive->id);
736 g_free(drive);
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");
747 char *driver = NULL;
748 char *filename = NULL;
749 XenBlockDrive *drive = NULL;
750 Error *local_err = NULL;
751 QDict *file_layer;
752 QDict *driver_layer;
754 if (params) {
755 char **v = g_strsplit(params, ":", 2);
757 if (v[1] == NULL) {
758 filename = g_strdup(v[0]);
759 driver = g_strdup("raw");
760 } else {
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");
765 } else {
766 driver = g_strdup(v[0]);
768 filename = g_strdup(v[1]);
771 g_strfreev(v);
772 } else {
773 error_setg(errp, "no params");
774 goto done;
777 assert(filename);
778 assert(driver);
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);
788 g_free(filename);
790 if (mode && *mode != 'w') {
791 qdict_put_bool(file_layer, "read-only", true);
794 if (direct_io_safe) {
795 unsigned long value;
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) {
808 unsigned long value;
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);
823 g_free(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,
829 &local_err);
831 qobject_unref(driver_layer);
833 done:
834 if (local_err) {
835 error_propagate(errp, local_err);
836 xen_block_drive_destroy(drive, NULL);
837 return NULL;
840 return drive;
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,
849 Error **errp)
851 qmp_object_del(iothread->id, errp);
853 g_free(iothread->id);
854 g_free(iothread);
857 static XenBlockIOThread *xen_block_iothread_create(const char *id,
858 Error **errp)
860 XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
861 Error *local_err = NULL;
862 QDict *opts;
863 QObject *ret_data = NULL;
865 iothread->id = g_strdup(id);
867 opts = qdict_new();
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);
871 qobject_unref(opts);
872 qobject_unref(ret_data);
874 if (local_err) {
875 error_propagate(errp, local_err);
877 g_free(iothread->id);
878 g_free(iothread);
879 return NULL;
882 return iothread;
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;
896 const char *type;
897 XenBlockDevice *blockdev;
899 if (qemu_strtoul(name, NULL, 10, &number)) {
900 error_setg(errp, "failed to parse name '%s'", name);
901 goto fail;
904 trace_xen_block_device_create(number);
906 vdev = qdict_get_try_str(opts, "dev");
907 if (!vdev) {
908 error_setg(errp, "no dev parameter");
909 goto fail;
912 device_type = qdict_get_try_str(opts, "device-type");
913 if (!device_type) {
914 error_setg(errp, "no device-type parameter");
915 goto fail;
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;
922 } else {
923 error_setg(errp, "invalid device-type parameter '%s'", device_type);
924 goto fail;
927 drive = xen_block_drive_create(vdev, device_type, opts, &local_err);
928 if (!drive) {
929 error_propagate_prepend(errp, local_err, "failed to create drive: ");
930 goto fail;
933 iothread = xen_block_iothread_create(vdev, &local_err);
934 if (local_err) {
935 error_propagate_prepend(errp, local_err,
936 "failed to create iothread: ");
937 goto fail;
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);
944 if (local_err) {
945 error_propagate_prepend(errp, local_err, "failed to set 'vdev': ");
946 goto fail;
949 object_property_set_str(OBJECT(xendev),
950 xen_block_drive_get_node_name(drive), "drive",
951 &local_err);
952 if (local_err) {
953 error_propagate_prepend(errp, local_err, "failed to set 'drive': ");
954 goto fail;
957 object_property_set_str(OBJECT(xendev), iothread->id, "iothread",
958 &local_err);
959 if (local_err) {
960 error_propagate_prepend(errp, local_err,
961 "failed to set 'iothread': ");
962 goto fail;
965 blockdev->iothread = iothread;
966 blockdev->drive = drive;
968 object_property_set_bool(OBJECT(xendev), true, "realized", &local_err);
969 if (local_err) {
970 error_propagate_prepend(errp, local_err,
971 "realization of device %s failed: ",
972 type);
973 goto fail;
976 xen_backend_set_device(backend, xendev);
977 return;
979 fail:
980 if (xendev) {
981 object_unparent(OBJECT(xendev));
984 if (iothread) {
985 xen_block_iothread_destroy(iothread, NULL);
988 if (drive) {
989 xen_block_drive_destroy(drive, NULL);
993 static void xen_block_device_destroy(XenBackendInstance *backend,
994 Error **errp)
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));
1007 if (iothread) {
1008 xen_block_iothread_destroy(iothread, &local_err);
1009 if (local_err) {
1010 error_propagate_prepend(errp, local_err,
1011 "failed to destroy iothread: ");
1012 return;
1016 if (drive) {
1017 xen_block_drive_destroy(drive, &local_err);
1018 if (local_err) {
1019 error_propagate_prepend(errp, local_err,
1020 "failed to destroy drive: ");
1021 return;
1026 static const XenBackendInfo xen_block_backend_info = {
1027 .type = "qdisk",
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);