xref: /openbmc/qemu/hw/block/xen-block.c (revision 2182e405)
1 /*
2  * Copyright (c) 2018  Citrix Systems Inc.
3  *
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  */
7 
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"
32 
33 static char *xen_block_get_name(XenDevice *xendev, Error **errp)
34 {
35     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
36     XenBlockVdev *vdev = &blockdev->props.vdev;
37 
38     return g_strdup_printf("%lu", vdev->number);
39 }
40 
41 static void xen_block_disconnect(XenDevice *xendev, Error **errp)
42 {
43     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
44     const char *type = object_get_typename(OBJECT(blockdev));
45     XenBlockVdev *vdev = &blockdev->props.vdev;
46 
47     trace_xen_block_disconnect(type, vdev->disk, vdev->partition);
48 
49     xen_block_dataplane_stop(blockdev->dataplane);
50 }
51 
52 static void xen_block_connect(XenDevice *xendev, Error **errp)
53 {
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;
61 
62     trace_xen_block_connect(type, vdev->disk, vdev->partition);
63 
64     if (xen_device_frontend_scanf(xendev, "feature-large-sector-size", "%u",
65                                   &feature_large_sector_size) != 1) {
66         feature_large_sector_size = 0;
67     }
68 
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;
74     }
75 
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);
80 
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;
86         }
87     } else if (order <= blockdev->props.max_ring_page_order) {
88         unsigned int i;
89 
90         nr_ring_ref = 1 << order;
91         ring_ref = g_new(unsigned int, nr_ring_ref);
92 
93         for (i = 0; i < nr_ring_ref; i++) {
94             const char *key = g_strdup_printf("ring-ref%u", i);
95 
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;
102             }
103 
104             g_free((gpointer)key);
105         }
106     } else {
107         error_setg(errp, "invalid ring-page-order (%d)", order);
108         return;
109     }
110 
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;
116     }
117 
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;
128         }
129 
130         free(str);
131     }
132 
133     xen_block_dataplane_start(blockdev->dataplane, ring_ref, nr_ring_ref,
134                               event_channel, protocol, errp);
135 
136     g_free(ring_ref);
137 }
138 
139 static void xen_block_unrealize(XenDevice *xendev)
140 {
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;
146 
147     if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
148         return;
149     }
150 
151     trace_xen_block_unrealize(type, vdev->disk, vdev->partition);
152 
153     /* Disconnect from the frontend in case this has not already happened */
154     xen_block_disconnect(xendev, NULL);
155 
156     xen_block_dataplane_destroy(blockdev->dataplane);
157     blockdev->dataplane = NULL;
158 
159     if (blockdev_class->unrealize) {
160         blockdev_class->unrealize(blockdev);
161     }
162 }
163 
164 static void xen_block_set_size(XenBlockDevice *blockdev)
165 {
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);
171 
172     trace_xen_block_size(type, vdev->disk, vdev->partition, sectors);
173 
174     xen_device_backend_printf(xendev, "sectors", "%"PRIi64, sectors);
175 }
176 
177 static void xen_block_resize_cb(void *opaque)
178 {
179     XenBlockDevice *blockdev = opaque;
180     XenDevice *xendev = XEN_DEVICE(blockdev);
181     enum xenbus_state state = xen_device_backend_get_state(xendev);
182 
183     xen_block_set_size(blockdev);
184 
185     /*
186      * Mimic the behaviour of Linux xen-blkback and re-write the state
187      * to trigger the frontend watch.
188      */
189     xen_device_backend_printf(xendev, "state", "%u", state);
190 }
191 
192 static const BlockDevOps xen_block_dev_ops = {
193     .resize_cb = xen_block_resize_cb,
194 };
195 
196 static void xen_block_realize(XenDevice *xendev, Error **errp)
197 {
198     ERRP_GUARD();
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;
206 
207     if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
208         error_setg(errp, "vdev property not set");
209         return;
210     }
211 
212     trace_xen_block_realize(type, vdev->disk, vdev->partition);
213 
214     if (blockdev_class->realize) {
215         blockdev_class->realize(blockdev, errp);
216         if (*errp) {
217             return;
218         }
219     }
220 
221     /*
222      * The blkif protocol does not deal with removable media, so it must
223      * always be present, even for CDRom devices.
224      */
225     assert(blk);
226     if (!blk_is_inserted(blk)) {
227         error_setg(errp, "device needs media, but drive is empty");
228         return;
229     }
230 
231     if (!blkconf_apply_backend_options(conf, blockdev->info & VDISK_READONLY,
232                                        true, errp)) {
233         return;
234     }
235 
236     if (!(blockdev->info & VDISK_CDROM) &&
237         !blkconf_geometry(conf, NULL, 65535, 255, 255, errp)) {
238         return;
239     }
240 
241     if (!blkconf_blocksizes(conf, errp)) {
242         return;
243     }
244 
245     blk_set_dev_ops(blk, &xen_block_dev_ops, blockdev);
246     blk_set_guest_block_size(blk, conf->logical_block_size);
247 
248     if (conf->discard_granularity == -1) {
249         conf->discard_granularity = conf->physical_block_size;
250     }
251 
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);
257     }
258 
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);
263 
264     xen_device_frontend_printf(xendev, "virtual-device", "%lu",
265                                vdev->number);
266     xen_device_frontend_printf(xendev, "device-type", "%s",
267                                blockdev->device_type);
268 
269     xen_device_backend_printf(xendev, "sector-size", "%u",
270                               conf->logical_block_size);
271 
272     xen_block_set_size(blockdev);
273 
274     blockdev->dataplane =
275         xen_block_dataplane_create(xendev, blk, conf->logical_block_size,
276                                    blockdev->props.iothread);
277 }
278 
279 static void xen_block_frontend_changed(XenDevice *xendev,
280                                        enum xenbus_state frontend_state,
281                                        Error **errp)
282 {
283     ERRP_GUARD();
284     enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
285 
286     switch (frontend_state) {
287     case XenbusStateInitialised:
288     case XenbusStateConnected:
289         if (backend_state == XenbusStateConnected) {
290             break;
291         }
292 
293         xen_block_disconnect(xendev, errp);
294         if (*errp) {
295             break;
296         }
297 
298         xen_block_connect(xendev, errp);
299         if (*errp) {
300             break;
301         }
302 
303         xen_device_backend_set_state(xendev, XenbusStateConnected);
304         break;
305 
306     case XenbusStateClosing:
307         xen_device_backend_set_state(xendev, XenbusStateClosing);
308         break;
309 
310     case XenbusStateClosed:
311     case XenbusStateUnknown:
312         xen_block_disconnect(xendev, errp);
313         if (*errp) {
314             break;
315         }
316 
317         xen_device_backend_set_state(xendev, XenbusStateClosed);
318         break;
319 
320     default:
321         break;
322     }
323 }
324 
325 static char *disk_to_vbd_name(unsigned int disk)
326 {
327     char *name, *prefix = (disk >= 26) ?
328         disk_to_vbd_name((disk / 26) - 1) : g_strdup("");
329 
330     name = g_strdup_printf("%s%c", prefix, 'a' + disk % 26);
331     g_free(prefix);
332 
333     return name;
334 }
335 
336 static void xen_block_get_vdev(Object *obj, Visitor *v, const char *name,
337                                void *opaque, Error **errp)
338 {
339     Property *prop = opaque;
340     XenBlockVdev *vdev = object_field_prop_ptr(obj, prop);
341     char *str;
342 
343     switch (vdev->type) {
344     case XEN_BLOCK_VDEV_TYPE_DP:
345         str = g_strdup_printf("d%lup%lu", vdev->disk, vdev->partition);
346         break;
347 
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);
352 
353         str = g_strdup_printf("%s%s%lu",
354                               (vdev->type == XEN_BLOCK_VDEV_TYPE_XVD) ?
355                               "xvd" :
356                               (vdev->type == XEN_BLOCK_VDEV_TYPE_HD) ?
357                               "hd" :
358                               "sd",
359                               name, vdev->partition);
360         g_free(name);
361         break;
362     }
363     default:
364         error_setg(errp, "invalid vdev type");
365         return;
366     }
367 
368     visit_type_str(v, name, &str, errp);
369     g_free(str);
370 }
371 
372 static int vbd_name_to_disk(const char *name, const char **endp,
373                             unsigned long *disk)
374 {
375     unsigned int n = 0;
376 
377     while (*name != '\0') {
378         if (!g_ascii_isalpha(*name) || !g_ascii_islower(*name)) {
379             break;
380         }
381 
382         n *= 26;
383         n += *name++ - 'a' + 1;
384     }
385     *endp = name;
386 
387     if (!n) {
388         return -1;
389     }
390 
391     *disk = n - 1;
392 
393     return 0;
394 }
395 
396 static void xen_block_set_vdev(Object *obj, Visitor *v, const char *name,
397                                void *opaque, Error **errp)
398 {
399     Property *prop = opaque;
400     XenBlockVdev *vdev = object_field_prop_ptr(obj, prop);
401     char *str, *p;
402     const char *end;
403 
404     if (!visit_type_str(v, name, &str, errp)) {
405         return;
406     }
407 
408     p = strchr(str, 'd');
409     if (!p) {
410         goto invalid;
411     }
412 
413     *p++ = '\0';
414     if (*str == '\0') {
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;
422     } else {
423         goto invalid;
424     }
425 
426     if (vdev->type == XEN_BLOCK_VDEV_TYPE_DP) {
427         if (qemu_strtoul(p, &end, 10, &vdev->disk)) {
428             goto invalid;
429         }
430 
431         if (*end == 'p') {
432             if (*(++end) == '\0') {
433                 goto invalid;
434             }
435         }
436     } else {
437         if (vbd_name_to_disk(p, &end, &vdev->disk)) {
438             goto invalid;
439         }
440     }
441 
442     if (*end != '\0') {
443         p = (char *)end;
444 
445         if (qemu_strtoul(p, &end, 10, &vdev->partition)) {
446             goto invalid;
447         }
448 
449         if (*end != '\0') {
450             goto invalid;
451         }
452     } else {
453         vdev->partition = 0;
454     }
455 
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) |
461                 vdev->partition;
462         } else if (vdev->disk < (1 << 20) && vdev->partition < (1 << 8)) {
463             vdev->number = (1 << 28) | (vdev->disk << 8) |
464                 vdev->partition;
465         } else {
466             goto invalid;
467         }
468         break;
469 
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) |
477                 vdev->partition;
478         } else {
479             goto invalid;
480         }
481         break;
482 
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;
486         } else {
487             goto invalid;
488         }
489         break;
490 
491     default:
492         goto invalid;
493     }
494 
495     g_free(str);
496     return;
497 
498 invalid:
499     error_setg(errp, "invalid virtual disk specifier");
500 
501     vdev->type = XEN_BLOCK_VDEV_TYPE_INVALID;
502     g_free(str);
503 }
504 
505 /*
506  * This property deals with 'vdev' names adhering to the Xen VBD naming
507  * scheme described in:
508  *
509  * https://xenbits.xen.org/docs/unstable/man/xen-vbd-interface.7.html
510  */
511 const PropertyInfo xen_block_prop_vdev = {
512     .name  = "str",
513     .description = "Virtual Disk specifier: d*p*/xvd*/hd*/sd*",
514     .get = xen_block_get_vdev,
515     .set = xen_block_set_vdev,
516 };
517 
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()
527 };
528 
529 static void xen_block_class_init(ObjectClass *class, void *data)
530 {
531     DeviceClass *dev_class = DEVICE_CLASS(class);
532     XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
533 
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;
540 
541     device_class_set_props(dev_class, xen_block_props);
542 }
543 
544 static const TypeInfo xen_block_type_info = {
545     .name = TYPE_XEN_BLOCK_DEVICE,
546     .parent = TYPE_XEN_DEVICE,
547     .instance_size = sizeof(XenBlockDevice),
548     .abstract = true,
549     .class_size = sizeof(XenBlockDeviceClass),
550     .class_init = xen_block_class_init,
551 };
552 
553 static void xen_disk_unrealize(XenBlockDevice *blockdev)
554 {
555     trace_xen_disk_unrealize();
556 }
557 
558 static void xen_disk_realize(XenBlockDevice *blockdev, Error **errp)
559 {
560     BlockConf *conf = &blockdev->props.conf;
561 
562     trace_xen_disk_realize();
563 
564     blockdev->device_type = "disk";
565 
566     if (!conf->blk) {
567         error_setg(errp, "drive property not set");
568         return;
569     }
570 
571     blockdev->info = blk_supports_write_perm(conf->blk) ? 0 : VDISK_READONLY;
572 }
573 
574 static void xen_disk_class_init(ObjectClass *class, void *data)
575 {
576     DeviceClass *dev_class = DEVICE_CLASS(class);
577     XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
578 
579     blockdev_class->realize = xen_disk_realize;
580     blockdev_class->unrealize = xen_disk_unrealize;
581 
582     dev_class->desc = "Xen Disk Device";
583 }
584 
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,
590 };
591 
592 static void xen_cdrom_unrealize(XenBlockDevice *blockdev)
593 {
594     trace_xen_cdrom_unrealize();
595 }
596 
597 static void xen_cdrom_realize(XenBlockDevice *blockdev, Error **errp)
598 {
599     BlockConf *conf = &blockdev->props.conf;
600 
601     trace_xen_cdrom_realize();
602 
603     blockdev->device_type = "cdrom";
604 
605     if (!conf->blk) {
606         int rc;
607 
608         /* Set up an empty drive */
609         conf->blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
610 
611         rc = blk_attach_dev(conf->blk, DEVICE(blockdev));
612         if (!rc) {
613             error_setg_errno(errp, -rc, "failed to create drive");
614             return;
615         }
616     }
617 
618     blockdev->info = VDISK_READONLY | VDISK_CDROM;
619 }
620 
621 static void xen_cdrom_class_init(ObjectClass *class, void *data)
622 {
623     DeviceClass *dev_class = DEVICE_CLASS(class);
624     XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
625 
626     blockdev_class->realize = xen_cdrom_realize;
627     blockdev_class->unrealize = xen_cdrom_unrealize;
628 
629     dev_class->desc = "Xen CD-ROM Device";
630 }
631 
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,
637 };
638 
639 static void xen_block_register_types(void)
640 {
641     type_register_static(&xen_block_type_info);
642     type_register_static(&xen_disk_type_info);
643     type_register_static(&xen_cdrom_type_info);
644 }
645 
646 type_init(xen_block_register_types)
647 
648 static void xen_block_blockdev_del(const char *node_name, Error **errp)
649 {
650     trace_xen_block_blockdev_del(node_name);
651 
652     qmp_blockdev_del(node_name, errp);
653 }
654 
655 static char *xen_block_blockdev_add(const char *id, QDict *qdict,
656                                     Error **errp)
657 {
658     ERRP_GUARD();
659     const char *driver = qdict_get_try_str(qdict, "driver");
660     BlockdevOptions *options = NULL;
661     char *node_name;
662     Visitor *v;
663 
664     if (!driver) {
665         error_setg(errp, "no 'driver' parameter");
666         return NULL;
667     }
668 
669     node_name = g_strdup_printf("%s-%s", id, driver);
670     qdict_put_str(qdict, "node-name", node_name);
671 
672     trace_xen_block_blockdev_add(node_name);
673 
674     v = qobject_input_visitor_new(QOBJECT(qdict));
675     visit_type_BlockdevOptions(v, NULL, &options, errp);
676     visit_free(v);
677     if (!options) {
678         goto fail;
679     }
680 
681     qmp_blockdev_add(options, errp);
682 
683     if (*errp) {
684         goto fail;
685     }
686 
687     qapi_free_BlockdevOptions(options);
688 
689     return node_name;
690 
691 fail:
692     if (options) {
693         qapi_free_BlockdevOptions(options);
694     }
695     g_free(node_name);
696 
697     return NULL;
698 }
699 
700 static void xen_block_drive_destroy(XenBlockDrive *drive, Error **errp)
701 {
702     ERRP_GUARD();
703     char *node_name = drive->node_name;
704 
705     if (node_name) {
706         xen_block_blockdev_del(node_name, errp);
707         if (*errp) {
708             return;
709         }
710         g_free(node_name);
711         drive->node_name = NULL;
712     }
713     g_free(drive->id);
714     g_free(drive);
715 }
716 
717 static XenBlockDrive *xen_block_drive_create(const char *id,
718                                              const char *device_type,
719                                              QDict *opts, Error **errp)
720 {
721     ERRP_GUARD();
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");
726     char *driver = NULL;
727     char *filename = NULL;
728     XenBlockDrive *drive = NULL;
729     QDict *file_layer;
730     QDict *driver_layer;
731 
732     if (params) {
733         char **v = g_strsplit(params, ":", 2);
734 
735         if (v[1] == NULL) {
736             filename = g_strdup(v[0]);
737             driver = g_strdup("raw");
738         } else {
739             if (strcmp(v[0], "aio") == 0) {
740                 driver = g_strdup("raw");
741             } else if (strcmp(v[0], "vhd") == 0) {
742                 driver = g_strdup("vpc");
743             } else {
744                 driver = g_strdup(v[0]);
745             }
746             filename = g_strdup(v[1]);
747         }
748 
749         g_strfreev(v);
750     } else {
751         error_setg(errp, "no params");
752         goto done;
753     }
754 
755     assert(filename);
756     assert(driver);
757 
758     drive = g_new0(XenBlockDrive, 1);
759     drive->id = g_strdup(id);
760 
761     file_layer = qdict_new();
762     driver_layer = qdict_new();
763 
764     qdict_put_str(file_layer, "driver", "file");
765     qdict_put_str(file_layer, "filename", filename);
766     g_free(filename);
767 
768     if (mode && *mode != 'w') {
769         qdict_put_bool(file_layer, "read-only", true);
770     }
771 
772     if (direct_io_safe) {
773         unsigned long value;
774 
775         if (!qemu_strtoul(direct_io_safe, NULL, 2, &value) && !!value) {
776             QDict *cache_qdict = qdict_new();
777 
778             qdict_put_bool(cache_qdict, "direct", true);
779             qdict_put(file_layer, "cache", cache_qdict);
780 
781             qdict_put_str(file_layer, "aio", "native");
782         }
783     }
784 
785     if (discard_enable) {
786         unsigned long value;
787 
788         if (!qemu_strtoul(discard_enable, NULL, 2, &value) && !!value) {
789             qdict_put_str(file_layer, "discard", "unmap");
790             qdict_put_str(driver_layer, "discard", "unmap");
791         }
792     }
793 
794     /*
795      * It is necessary to turn file locking off as an emulated device
796      * may have already opened the same image file.
797      */
798     qdict_put_str(file_layer, "locking", "off");
799 
800     qdict_put_str(driver_layer, "driver", driver);
801     g_free(driver);
802 
803     qdict_put(driver_layer, "file", file_layer);
804 
805     g_assert(!drive->node_name);
806     drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
807                                               errp);
808 
809     qobject_unref(driver_layer);
810 
811 done:
812     if (*errp) {
813         xen_block_drive_destroy(drive, NULL);
814         return NULL;
815     }
816 
817     return drive;
818 }
819 
820 static const char *xen_block_drive_get_node_name(XenBlockDrive *drive)
821 {
822     return drive->node_name ? drive->node_name : "";
823 }
824 
825 static void xen_block_iothread_destroy(XenBlockIOThread *iothread,
826                                        Error **errp)
827 {
828     qmp_object_del(iothread->id, errp);
829 
830     g_free(iothread->id);
831     g_free(iothread);
832 }
833 
834 static XenBlockIOThread *xen_block_iothread_create(const char *id,
835                                                    Error **errp)
836 {
837     ERRP_GUARD();
838     XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
839     QDict *opts;
840     QObject *ret_data = NULL;
841 
842     iothread->id = g_strdup(id);
843 
844     opts = qdict_new();
845     qdict_put_str(opts, "qom-type", TYPE_IOTHREAD);
846     qdict_put_str(opts, "id", id);
847     qmp_object_add(opts, &ret_data, errp);
848     qobject_unref(opts);
849     qobject_unref(ret_data);
850 
851     if (*errp) {
852         g_free(iothread->id);
853         g_free(iothread);
854         return NULL;
855     }
856 
857     return iothread;
858 }
859 
860 static void xen_block_device_create(XenBackendInstance *backend,
861                                     QDict *opts, Error **errp)
862 {
863     ERRP_GUARD();
864     XenBus *xenbus = xen_backend_get_bus(backend);
865     const char *name = xen_backend_get_name(backend);
866     unsigned long number;
867     const char *vdev, *device_type;
868     XenBlockDrive *drive = NULL;
869     XenBlockIOThread *iothread = NULL;
870     XenDevice *xendev = NULL;
871     const char *type;
872     XenBlockDevice *blockdev;
873 
874     if (qemu_strtoul(name, NULL, 10, &number)) {
875         error_setg(errp, "failed to parse name '%s'", name);
876         goto fail;
877     }
878 
879     trace_xen_block_device_create(number);
880 
881     vdev = qdict_get_try_str(opts, "dev");
882     if (!vdev) {
883         error_setg(errp, "no dev parameter");
884         goto fail;
885     }
886 
887     device_type = qdict_get_try_str(opts, "device-type");
888     if (!device_type) {
889         error_setg(errp, "no device-type parameter");
890         goto fail;
891     }
892 
893     if (!strcmp(device_type, "disk")) {
894         type = TYPE_XEN_DISK_DEVICE;
895     } else if (!strcmp(device_type, "cdrom")) {
896         type = TYPE_XEN_CDROM_DEVICE;
897     } else {
898         error_setg(errp, "invalid device-type parameter '%s'", device_type);
899         goto fail;
900     }
901 
902     drive = xen_block_drive_create(vdev, device_type, opts, errp);
903     if (!drive) {
904         error_prepend(errp, "failed to create drive: ");
905         goto fail;
906     }
907 
908     iothread = xen_block_iothread_create(vdev, errp);
909     if (*errp) {
910         error_prepend(errp, "failed to create iothread: ");
911         goto fail;
912     }
913 
914     xendev = XEN_DEVICE(qdev_new(type));
915     blockdev = XEN_BLOCK_DEVICE(xendev);
916 
917     if (!object_property_set_str(OBJECT(xendev), "vdev", vdev,
918                                  errp)) {
919         error_prepend(errp, "failed to set 'vdev': ");
920         goto fail;
921     }
922 
923     if (!object_property_set_str(OBJECT(xendev), "drive",
924                                  xen_block_drive_get_node_name(drive),
925                                  errp)) {
926         error_prepend(errp, "failed to set 'drive': ");
927         goto fail;
928     }
929 
930     if (!object_property_set_str(OBJECT(xendev), "iothread", iothread->id,
931                                  errp)) {
932         error_prepend(errp, "failed to set 'iothread': ");
933         goto fail;
934     }
935 
936     blockdev->iothread = iothread;
937     blockdev->drive = drive;
938 
939     if (!qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
940         error_prepend(errp, "realization of device %s failed: ", type);
941         goto fail;
942     }
943 
944     xen_backend_set_device(backend, xendev);
945     return;
946 
947 fail:
948     if (xendev) {
949         object_unparent(OBJECT(xendev));
950     }
951 
952     if (iothread) {
953         xen_block_iothread_destroy(iothread, NULL);
954     }
955 
956     if (drive) {
957         xen_block_drive_destroy(drive, NULL);
958     }
959 }
960 
961 static void xen_block_device_destroy(XenBackendInstance *backend,
962                                      Error **errp)
963 {
964     ERRP_GUARD();
965     XenDevice *xendev = xen_backend_get_device(backend);
966     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
967     XenBlockVdev *vdev = &blockdev->props.vdev;
968     XenBlockDrive *drive = blockdev->drive;
969     XenBlockIOThread *iothread = blockdev->iothread;
970 
971     trace_xen_block_device_destroy(vdev->number);
972 
973     object_unparent(OBJECT(xendev));
974 
975     if (iothread) {
976         xen_block_iothread_destroy(iothread, errp);
977         if (*errp) {
978             error_prepend(errp, "failed to destroy iothread: ");
979             return;
980         }
981     }
982 
983     if (drive) {
984         xen_block_drive_destroy(drive, errp);
985         if (*errp) {
986             error_prepend(errp, "failed to destroy drive: ");
987             return;
988         }
989     }
990 }
991 
992 static const XenBackendInfo xen_block_backend_info = {
993     .type = "qdisk",
994     .create = xen_block_device_create,
995     .destroy = xen_block_device_destroy,
996 };
997 
998 static void xen_block_register_backend(void)
999 {
1000     xen_backend_register(&xen_block_backend_info);
1001 }
1002 
1003 xen_backend_init(xen_block_register_backend);
1004