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