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