xref: /openbmc/qemu/hw/block/xen-block.c (revision 7acafcfa)
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     }
257 
258     xen_device_backend_printf(xendev, "feature-flush-cache", "%u", 1);
259     xen_device_backend_printf(xendev, "max-ring-page-order", "%u",
260                               blockdev->props.max_ring_page_order);
261     xen_device_backend_printf(xendev, "info", "%u", blockdev->info);
262 
263     xen_device_frontend_printf(xendev, "virtual-device", "%lu",
264                                vdev->number);
265     xen_device_frontend_printf(xendev, "device-type", "%s",
266                                blockdev->device_type);
267 
268     xen_device_backend_printf(xendev, "sector-size", "%u",
269                               conf->logical_block_size);
270 
271     xen_block_set_size(blockdev);
272 
273     blockdev->dataplane =
274         xen_block_dataplane_create(xendev, blk, conf->logical_block_size,
275                                    blockdev->props.iothread);
276 }
277 
278 static void xen_block_frontend_changed(XenDevice *xendev,
279                                        enum xenbus_state frontend_state,
280                                        Error **errp)
281 {
282     ERRP_GUARD();
283     enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
284 
285     switch (frontend_state) {
286     case XenbusStateInitialised:
287     case XenbusStateConnected:
288         if (backend_state == XenbusStateConnected) {
289             break;
290         }
291 
292         xen_block_disconnect(xendev, errp);
293         if (*errp) {
294             break;
295         }
296 
297         xen_block_connect(xendev, errp);
298         if (*errp) {
299             break;
300         }
301 
302         xen_device_backend_set_state(xendev, XenbusStateConnected);
303         break;
304 
305     case XenbusStateClosing:
306         xen_device_backend_set_state(xendev, XenbusStateClosing);
307         break;
308 
309     case XenbusStateClosed:
310     case XenbusStateUnknown:
311         xen_block_disconnect(xendev, errp);
312         if (*errp) {
313             break;
314         }
315 
316         xen_device_backend_set_state(xendev, XenbusStateClosed);
317         break;
318 
319     default:
320         break;
321     }
322 }
323 
324 static char *disk_to_vbd_name(unsigned int disk)
325 {
326     char *name, *prefix = (disk >= 26) ?
327         disk_to_vbd_name((disk / 26) - 1) : g_strdup("");
328 
329     name = g_strdup_printf("%s%c", prefix, 'a' + disk % 26);
330     g_free(prefix);
331 
332     return name;
333 }
334 
335 static void xen_block_get_vdev(Object *obj, Visitor *v, const char *name,
336                                void *opaque, Error **errp)
337 {
338     DeviceState *dev = DEVICE(obj);
339     Property *prop = opaque;
340     XenBlockVdev *vdev = qdev_get_prop_ptr(dev, 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     DeviceState *dev = DEVICE(obj);
400     Property *prop = opaque;
401     XenBlockVdev *vdev = qdev_get_prop_ptr(dev, prop);
402     char *str, *p;
403     const char *end;
404 
405     if (dev->realized) {
406         qdev_prop_set_after_realize(dev, name, errp);
407         return;
408     }
409 
410     if (!visit_type_str(v, name, &str, errp)) {
411         return;
412     }
413 
414     p = strchr(str, 'd');
415     if (!p) {
416         goto invalid;
417     }
418 
419     *p++ = '\0';
420     if (*str == '\0') {
421         vdev->type = XEN_BLOCK_VDEV_TYPE_DP;
422     } else if (strcmp(str, "xv") == 0) {
423         vdev->type = XEN_BLOCK_VDEV_TYPE_XVD;
424     } else if (strcmp(str, "h") == 0) {
425         vdev->type = XEN_BLOCK_VDEV_TYPE_HD;
426     } else if (strcmp(str, "s") == 0) {
427         vdev->type = XEN_BLOCK_VDEV_TYPE_SD;
428     } else {
429         goto invalid;
430     }
431 
432     if (vdev->type == XEN_BLOCK_VDEV_TYPE_DP) {
433         if (qemu_strtoul(p, &end, 10, &vdev->disk)) {
434             goto invalid;
435         }
436 
437         if (*end == 'p') {
438             if (*(++end) == '\0') {
439                 goto invalid;
440             }
441         }
442     } else {
443         if (vbd_name_to_disk(p, &end, &vdev->disk)) {
444             goto invalid;
445         }
446     }
447 
448     if (*end != '\0') {
449         p = (char *)end;
450 
451         if (qemu_strtoul(p, &end, 10, &vdev->partition)) {
452             goto invalid;
453         }
454 
455         if (*end != '\0') {
456             goto invalid;
457         }
458     } else {
459         vdev->partition = 0;
460     }
461 
462     switch (vdev->type) {
463     case XEN_BLOCK_VDEV_TYPE_DP:
464     case XEN_BLOCK_VDEV_TYPE_XVD:
465         if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
466             vdev->number = (202 << 8) | (vdev->disk << 4) |
467                 vdev->partition;
468         } else if (vdev->disk < (1 << 20) && vdev->partition < (1 << 8)) {
469             vdev->number = (1 << 28) | (vdev->disk << 8) |
470                 vdev->partition;
471         } else {
472             goto invalid;
473         }
474         break;
475 
476     case XEN_BLOCK_VDEV_TYPE_HD:
477         if ((vdev->disk == 0 || vdev->disk == 1) &&
478             vdev->partition < (1 << 6)) {
479             vdev->number = (3 << 8) | (vdev->disk << 6) | vdev->partition;
480         } else if ((vdev->disk == 2 || vdev->disk == 3) &&
481                    vdev->partition < (1 << 6)) {
482             vdev->number = (22 << 8) | ((vdev->disk - 2) << 6) |
483                 vdev->partition;
484         } else {
485             goto invalid;
486         }
487         break;
488 
489     case XEN_BLOCK_VDEV_TYPE_SD:
490         if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
491             vdev->number = (8 << 8) | (vdev->disk << 4) | vdev->partition;
492         } else {
493             goto invalid;
494         }
495         break;
496 
497     default:
498         goto invalid;
499     }
500 
501     g_free(str);
502     return;
503 
504 invalid:
505     error_setg(errp, "invalid virtual disk specifier");
506 
507     vdev->type = XEN_BLOCK_VDEV_TYPE_INVALID;
508     g_free(str);
509 }
510 
511 /*
512  * This property deals with 'vdev' names adhering to the Xen VBD naming
513  * scheme described in:
514  *
515  * https://xenbits.xen.org/docs/unstable/man/xen-vbd-interface.7.html
516  */
517 const PropertyInfo xen_block_prop_vdev = {
518     .name  = "str",
519     .description = "Virtual Disk specifier: d*p*/xvd*/hd*/sd*",
520     .get = xen_block_get_vdev,
521     .set = xen_block_set_vdev,
522 };
523 
524 static Property xen_block_props[] = {
525     DEFINE_PROP("vdev", XenBlockDevice, props.vdev,
526                 xen_block_prop_vdev, XenBlockVdev),
527     DEFINE_BLOCK_PROPERTIES(XenBlockDevice, props.conf),
528     DEFINE_PROP_UINT32("max-ring-page-order", XenBlockDevice,
529                        props.max_ring_page_order, 4),
530     DEFINE_PROP_LINK("iothread", XenBlockDevice, props.iothread,
531                      TYPE_IOTHREAD, IOThread *),
532     DEFINE_PROP_END_OF_LIST()
533 };
534 
535 static void xen_block_class_init(ObjectClass *class, void *data)
536 {
537     DeviceClass *dev_class = DEVICE_CLASS(class);
538     XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
539 
540     xendev_class->backend = "qdisk";
541     xendev_class->device = "vbd";
542     xendev_class->get_name = xen_block_get_name;
543     xendev_class->realize = xen_block_realize;
544     xendev_class->frontend_changed = xen_block_frontend_changed;
545     xendev_class->unrealize = xen_block_unrealize;
546 
547     device_class_set_props(dev_class, xen_block_props);
548 }
549 
550 static const TypeInfo xen_block_type_info = {
551     .name = TYPE_XEN_BLOCK_DEVICE,
552     .parent = TYPE_XEN_DEVICE,
553     .instance_size = sizeof(XenBlockDevice),
554     .abstract = true,
555     .class_size = sizeof(XenBlockDeviceClass),
556     .class_init = xen_block_class_init,
557 };
558 
559 static void xen_disk_unrealize(XenBlockDevice *blockdev)
560 {
561     trace_xen_disk_unrealize();
562 }
563 
564 static void xen_disk_realize(XenBlockDevice *blockdev, Error **errp)
565 {
566     BlockConf *conf = &blockdev->props.conf;
567 
568     trace_xen_disk_realize();
569 
570     blockdev->device_type = "disk";
571 
572     if (!conf->blk) {
573         error_setg(errp, "drive property not set");
574         return;
575     }
576 
577     blockdev->info = blk_is_read_only(conf->blk) ? VDISK_READONLY : 0;
578 }
579 
580 static void xen_disk_class_init(ObjectClass *class, void *data)
581 {
582     DeviceClass *dev_class = DEVICE_CLASS(class);
583     XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
584 
585     blockdev_class->realize = xen_disk_realize;
586     blockdev_class->unrealize = xen_disk_unrealize;
587 
588     dev_class->desc = "Xen Disk Device";
589 }
590 
591 static const TypeInfo xen_disk_type_info = {
592     .name = TYPE_XEN_DISK_DEVICE,
593     .parent = TYPE_XEN_BLOCK_DEVICE,
594     .instance_size = sizeof(XenDiskDevice),
595     .class_init = xen_disk_class_init,
596 };
597 
598 static void xen_cdrom_unrealize(XenBlockDevice *blockdev)
599 {
600     trace_xen_cdrom_unrealize();
601 }
602 
603 static void xen_cdrom_realize(XenBlockDevice *blockdev, Error **errp)
604 {
605     BlockConf *conf = &blockdev->props.conf;
606 
607     trace_xen_cdrom_realize();
608 
609     blockdev->device_type = "cdrom";
610 
611     if (!conf->blk) {
612         int rc;
613 
614         /* Set up an empty drive */
615         conf->blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
616 
617         rc = blk_attach_dev(conf->blk, DEVICE(blockdev));
618         if (!rc) {
619             error_setg_errno(errp, -rc, "failed to create drive");
620             return;
621         }
622     }
623 
624     blockdev->info = VDISK_READONLY | VDISK_CDROM;
625 }
626 
627 static void xen_cdrom_class_init(ObjectClass *class, void *data)
628 {
629     DeviceClass *dev_class = DEVICE_CLASS(class);
630     XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
631 
632     blockdev_class->realize = xen_cdrom_realize;
633     blockdev_class->unrealize = xen_cdrom_unrealize;
634 
635     dev_class->desc = "Xen CD-ROM Device";
636 }
637 
638 static const TypeInfo xen_cdrom_type_info = {
639     .name = TYPE_XEN_CDROM_DEVICE,
640     .parent = TYPE_XEN_BLOCK_DEVICE,
641     .instance_size = sizeof(XenCDRomDevice),
642     .class_init = xen_cdrom_class_init,
643 };
644 
645 static void xen_block_register_types(void)
646 {
647     type_register_static(&xen_block_type_info);
648     type_register_static(&xen_disk_type_info);
649     type_register_static(&xen_cdrom_type_info);
650 }
651 
652 type_init(xen_block_register_types)
653 
654 static void xen_block_blockdev_del(const char *node_name, Error **errp)
655 {
656     trace_xen_block_blockdev_del(node_name);
657 
658     qmp_blockdev_del(node_name, errp);
659 }
660 
661 static char *xen_block_blockdev_add(const char *id, QDict *qdict,
662                                     Error **errp)
663 {
664     ERRP_GUARD();
665     const char *driver = qdict_get_try_str(qdict, "driver");
666     BlockdevOptions *options = NULL;
667     char *node_name;
668     Visitor *v;
669 
670     if (!driver) {
671         error_setg(errp, "no 'driver' parameter");
672         return NULL;
673     }
674 
675     node_name = g_strdup_printf("%s-%s", id, driver);
676     qdict_put_str(qdict, "node-name", node_name);
677 
678     trace_xen_block_blockdev_add(node_name);
679 
680     v = qobject_input_visitor_new(QOBJECT(qdict));
681     visit_type_BlockdevOptions(v, NULL, &options, errp);
682     visit_free(v);
683     if (!options) {
684         goto fail;
685     }
686 
687     qmp_blockdev_add(options, errp);
688 
689     if (*errp) {
690         goto fail;
691     }
692 
693     qapi_free_BlockdevOptions(options);
694 
695     return node_name;
696 
697 fail:
698     if (options) {
699         qapi_free_BlockdevOptions(options);
700     }
701     g_free(node_name);
702 
703     return NULL;
704 }
705 
706 static void xen_block_drive_destroy(XenBlockDrive *drive, Error **errp)
707 {
708     ERRP_GUARD();
709     char *node_name = drive->node_name;
710 
711     if (node_name) {
712         xen_block_blockdev_del(node_name, errp);
713         if (*errp) {
714             return;
715         }
716         g_free(node_name);
717         drive->node_name = NULL;
718     }
719     g_free(drive->id);
720     g_free(drive);
721 }
722 
723 static XenBlockDrive *xen_block_drive_create(const char *id,
724                                              const char *device_type,
725                                              QDict *opts, Error **errp)
726 {
727     ERRP_GUARD();
728     const char *params = qdict_get_try_str(opts, "params");
729     const char *mode = qdict_get_try_str(opts, "mode");
730     const char *direct_io_safe = qdict_get_try_str(opts, "direct-io-safe");
731     const char *discard_enable = qdict_get_try_str(opts, "discard-enable");
732     char *driver = NULL;
733     char *filename = NULL;
734     XenBlockDrive *drive = NULL;
735     QDict *file_layer;
736     QDict *driver_layer;
737 
738     if (params) {
739         char **v = g_strsplit(params, ":", 2);
740 
741         if (v[1] == NULL) {
742             filename = g_strdup(v[0]);
743             driver = g_strdup("raw");
744         } else {
745             if (strcmp(v[0], "aio") == 0) {
746                 driver = g_strdup("raw");
747             } else if (strcmp(v[0], "vhd") == 0) {
748                 driver = g_strdup("vpc");
749             } else {
750                 driver = g_strdup(v[0]);
751             }
752             filename = g_strdup(v[1]);
753         }
754 
755         g_strfreev(v);
756     } else {
757         error_setg(errp, "no params");
758         goto done;
759     }
760 
761     assert(filename);
762     assert(driver);
763 
764     drive = g_new0(XenBlockDrive, 1);
765     drive->id = g_strdup(id);
766 
767     file_layer = qdict_new();
768     driver_layer = qdict_new();
769 
770     qdict_put_str(file_layer, "driver", "file");
771     qdict_put_str(file_layer, "filename", filename);
772     g_free(filename);
773 
774     if (mode && *mode != 'w') {
775         qdict_put_bool(file_layer, "read-only", true);
776     }
777 
778     if (direct_io_safe) {
779         unsigned long value;
780 
781         if (!qemu_strtoul(direct_io_safe, NULL, 2, &value) && !!value) {
782             QDict *cache_qdict = qdict_new();
783 
784             qdict_put_bool(cache_qdict, "direct", true);
785             qdict_put(file_layer, "cache", cache_qdict);
786 
787             qdict_put_str(file_layer, "aio", "native");
788         }
789     }
790 
791     if (discard_enable) {
792         unsigned long value;
793 
794         if (!qemu_strtoul(discard_enable, NULL, 2, &value) && !!value) {
795             qdict_put_str(file_layer, "discard", "unmap");
796             qdict_put_str(driver_layer, "discard", "unmap");
797         }
798     }
799 
800     /*
801      * It is necessary to turn file locking off as an emulated device
802      * may have already opened the same image file.
803      */
804     qdict_put_str(file_layer, "locking", "off");
805 
806     qdict_put_str(driver_layer, "driver", driver);
807     g_free(driver);
808 
809     qdict_put(driver_layer, "file", file_layer);
810 
811     g_assert(!drive->node_name);
812     drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
813                                               errp);
814 
815     qobject_unref(driver_layer);
816 
817 done:
818     if (*errp) {
819         xen_block_drive_destroy(drive, NULL);
820         return NULL;
821     }
822 
823     return drive;
824 }
825 
826 static const char *xen_block_drive_get_node_name(XenBlockDrive *drive)
827 {
828     return drive->node_name ? drive->node_name : "";
829 }
830 
831 static void xen_block_iothread_destroy(XenBlockIOThread *iothread,
832                                        Error **errp)
833 {
834     qmp_object_del(iothread->id, errp);
835 
836     g_free(iothread->id);
837     g_free(iothread);
838 }
839 
840 static XenBlockIOThread *xen_block_iothread_create(const char *id,
841                                                    Error **errp)
842 {
843     ERRP_GUARD();
844     XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
845     QDict *opts;
846     QObject *ret_data = NULL;
847 
848     iothread->id = g_strdup(id);
849 
850     opts = qdict_new();
851     qdict_put_str(opts, "qom-type", TYPE_IOTHREAD);
852     qdict_put_str(opts, "id", id);
853     qmp_object_add(opts, &ret_data, errp);
854     qobject_unref(opts);
855     qobject_unref(ret_data);
856 
857     if (*errp) {
858         g_free(iothread->id);
859         g_free(iothread);
860         return NULL;
861     }
862 
863     return iothread;
864 }
865 
866 static void xen_block_device_create(XenBackendInstance *backend,
867                                     QDict *opts, Error **errp)
868 {
869     ERRP_GUARD();
870     XenBus *xenbus = xen_backend_get_bus(backend);
871     const char *name = xen_backend_get_name(backend);
872     unsigned long number;
873     const char *vdev, *device_type;
874     XenBlockDrive *drive = NULL;
875     XenBlockIOThread *iothread = NULL;
876     XenDevice *xendev = NULL;
877     const char *type;
878     XenBlockDevice *blockdev;
879 
880     if (qemu_strtoul(name, NULL, 10, &number)) {
881         error_setg(errp, "failed to parse name '%s'", name);
882         goto fail;
883     }
884 
885     trace_xen_block_device_create(number);
886 
887     vdev = qdict_get_try_str(opts, "dev");
888     if (!vdev) {
889         error_setg(errp, "no dev parameter");
890         goto fail;
891     }
892 
893     device_type = qdict_get_try_str(opts, "device-type");
894     if (!device_type) {
895         error_setg(errp, "no device-type parameter");
896         goto fail;
897     }
898 
899     if (!strcmp(device_type, "disk")) {
900         type = TYPE_XEN_DISK_DEVICE;
901     } else if (!strcmp(device_type, "cdrom")) {
902         type = TYPE_XEN_CDROM_DEVICE;
903     } else {
904         error_setg(errp, "invalid device-type parameter '%s'", device_type);
905         goto fail;
906     }
907 
908     drive = xen_block_drive_create(vdev, device_type, opts, errp);
909     if (!drive) {
910         error_prepend(errp, "failed to create drive: ");
911         goto fail;
912     }
913 
914     iothread = xen_block_iothread_create(vdev, errp);
915     if (*errp) {
916         error_prepend(errp, "failed to create iothread: ");
917         goto fail;
918     }
919 
920     xendev = XEN_DEVICE(qdev_new(type));
921     blockdev = XEN_BLOCK_DEVICE(xendev);
922 
923     if (!object_property_set_str(OBJECT(xendev), "vdev", vdev,
924                                  errp)) {
925         error_prepend(errp, "failed to set 'vdev': ");
926         goto fail;
927     }
928 
929     if (!object_property_set_str(OBJECT(xendev), "drive",
930                                  xen_block_drive_get_node_name(drive),
931                                  errp)) {
932         error_prepend(errp, "failed to set 'drive': ");
933         goto fail;
934     }
935 
936     if (!object_property_set_str(OBJECT(xendev), "iothread", iothread->id,
937                                  errp)) {
938         error_prepend(errp, "failed to set 'iothread': ");
939         goto fail;
940     }
941 
942     blockdev->iothread = iothread;
943     blockdev->drive = drive;
944 
945     if (!qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
946         error_prepend(errp, "realization of device %s failed: ", type);
947         goto fail;
948     }
949 
950     xen_backend_set_device(backend, xendev);
951     return;
952 
953 fail:
954     if (xendev) {
955         object_unparent(OBJECT(xendev));
956     }
957 
958     if (iothread) {
959         xen_block_iothread_destroy(iothread, NULL);
960     }
961 
962     if (drive) {
963         xen_block_drive_destroy(drive, NULL);
964     }
965 }
966 
967 static void xen_block_device_destroy(XenBackendInstance *backend,
968                                      Error **errp)
969 {
970     ERRP_GUARD();
971     XenDevice *xendev = xen_backend_get_device(backend);
972     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
973     XenBlockVdev *vdev = &blockdev->props.vdev;
974     XenBlockDrive *drive = blockdev->drive;
975     XenBlockIOThread *iothread = blockdev->iothread;
976 
977     trace_xen_block_device_destroy(vdev->number);
978 
979     object_unparent(OBJECT(xendev));
980 
981     if (iothread) {
982         xen_block_iothread_destroy(iothread, errp);
983         if (*errp) {
984             error_prepend(errp, "failed to destroy iothread: ");
985             return;
986         }
987     }
988 
989     if (drive) {
990         xen_block_drive_destroy(drive, errp);
991         if (*errp) {
992             error_prepend(errp, "failed to destroy drive: ");
993             return;
994         }
995     }
996 }
997 
998 static const XenBackendInfo xen_block_backend_info = {
999     .type = "qdisk",
1000     .create = xen_block_device_create,
1001     .destroy = xen_block_device_destroy,
1002 };
1003 
1004 static void xen_block_register_backend(void)
1005 {
1006     xen_backend_register(&xen_block_backend_info);
1007 }
1008 
1009 xen_backend_init(xen_block_register_backend);
1010