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