xref: /openbmc/qemu/hw/block/xen-block.c (revision c63ca4ff)
1 /*
2  * Copyright (c) 2018  Citrix Systems Inc.
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7 
8 #include "qemu/osdep.h"
9 #include "qemu/cutils.h"
10 #include "qemu/main-loop.h"
11 #include "qemu/module.h"
12 #include "qemu/option.h"
13 #include "qapi/error.h"
14 #include "qapi/qapi-commands-block-core.h"
15 #include "qapi/qapi-commands-qom.h"
16 #include "qapi/qapi-visit-block-core.h"
17 #include "qapi/qobject-input-visitor.h"
18 #include "qapi/visitor.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qstring.h"
21 #include "qom/object_interfaces.h"
22 #include "hw/xen/xen_common.h"
23 #include "hw/block/xen_blkif.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/xen/xen-block.h"
26 #include "hw/xen/xen-backend.h"
27 #include "sysemu/blockdev.h"
28 #include "sysemu/block-backend.h"
29 #include "sysemu/iothread.h"
30 #include "dataplane/xen-block.h"
31 #include "trace.h"
32 
33 static char *xen_block_get_name(XenDevice *xendev, Error **errp)
34 {
35     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
36     XenBlockVdev *vdev = &blockdev->props.vdev;
37 
38     return g_strdup_printf("%lu", vdev->number);
39 }
40 
41 static void xen_block_disconnect(XenDevice *xendev, Error **errp)
42 {
43     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
44     const char *type = object_get_typename(OBJECT(blockdev));
45     XenBlockVdev *vdev = &blockdev->props.vdev;
46 
47     trace_xen_block_disconnect(type, vdev->disk, vdev->partition);
48 
49     xen_block_dataplane_stop(blockdev->dataplane);
50 }
51 
52 static void xen_block_connect(XenDevice *xendev, Error **errp)
53 {
54     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
55     const char *type = object_get_typename(OBJECT(blockdev));
56     XenBlockVdev *vdev = &blockdev->props.vdev;
57     BlockConf *conf = &blockdev->props.conf;
58     unsigned int feature_large_sector_size;
59     unsigned int order, nr_ring_ref, *ring_ref, event_channel, protocol;
60     char *str;
61 
62     trace_xen_block_connect(type, vdev->disk, vdev->partition);
63 
64     if (xen_device_frontend_scanf(xendev, "feature-large-sector-size", "%u",
65                                   &feature_large_sector_size) != 1) {
66         feature_large_sector_size = 0;
67     }
68 
69     if (feature_large_sector_size != 1 &&
70         conf->logical_block_size != XEN_BLKIF_SECTOR_SIZE) {
71         error_setg(errp, "logical_block_size != %u not supported by frontend",
72                    XEN_BLKIF_SECTOR_SIZE);
73         return;
74     }
75 
76     if (xen_device_frontend_scanf(xendev, "ring-page-order", "%u",
77                                   &order) != 1) {
78         nr_ring_ref = 1;
79         ring_ref = g_new(unsigned int, nr_ring_ref);
80 
81         if (xen_device_frontend_scanf(xendev, "ring-ref", "%u",
82                                       &ring_ref[0]) != 1) {
83             error_setg(errp, "failed to read ring-ref");
84             g_free(ring_ref);
85             return;
86         }
87     } else if (order <= blockdev->props.max_ring_page_order) {
88         unsigned int i;
89 
90         nr_ring_ref = 1 << order;
91         ring_ref = g_new(unsigned int, nr_ring_ref);
92 
93         for (i = 0; i < nr_ring_ref; i++) {
94             const char *key = g_strdup_printf("ring-ref%u", i);
95 
96             if (xen_device_frontend_scanf(xendev, key, "%u",
97                                           &ring_ref[i]) != 1) {
98                 error_setg(errp, "failed to read %s", key);
99                 g_free((gpointer)key);
100                 g_free(ring_ref);
101                 return;
102             }
103 
104             g_free((gpointer)key);
105         }
106     } else {
107         error_setg(errp, "invalid ring-page-order (%d)", order);
108         return;
109     }
110 
111     if (xen_device_frontend_scanf(xendev, "event-channel", "%u",
112                                   &event_channel) != 1) {
113         error_setg(errp, "failed to read event-channel");
114         g_free(ring_ref);
115         return;
116     }
117 
118     if (xen_device_frontend_scanf(xendev, "protocol", "%ms",
119                                   &str) != 1) {
120         protocol = BLKIF_PROTOCOL_NATIVE;
121     } else {
122         if (strcmp(str, XEN_IO_PROTO_ABI_X86_32) == 0) {
123             protocol = BLKIF_PROTOCOL_X86_32;
124         } else if (strcmp(str, XEN_IO_PROTO_ABI_X86_64) == 0) {
125             protocol = BLKIF_PROTOCOL_X86_64;
126         } else {
127             protocol = BLKIF_PROTOCOL_NATIVE;
128         }
129 
130         free(str);
131     }
132 
133     xen_block_dataplane_start(blockdev->dataplane, ring_ref, nr_ring_ref,
134                               event_channel, protocol, errp);
135 
136     g_free(ring_ref);
137 }
138 
139 static void xen_block_unrealize(XenDevice *xendev)
140 {
141     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
142     XenBlockDeviceClass *blockdev_class =
143         XEN_BLOCK_DEVICE_GET_CLASS(xendev);
144     const char *type = object_get_typename(OBJECT(blockdev));
145     XenBlockVdev *vdev = &blockdev->props.vdev;
146 
147     if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
148         return;
149     }
150 
151     trace_xen_block_unrealize(type, vdev->disk, vdev->partition);
152 
153     /* Disconnect from the frontend in case this has not already happened */
154     xen_block_disconnect(xendev, NULL);
155 
156     xen_block_dataplane_destroy(blockdev->dataplane);
157     blockdev->dataplane = NULL;
158 
159     if (blockdev_class->unrealize) {
160         blockdev_class->unrealize(blockdev);
161     }
162 }
163 
164 static void xen_block_set_size(XenBlockDevice *blockdev)
165 {
166     const char *type = object_get_typename(OBJECT(blockdev));
167     XenBlockVdev *vdev = &blockdev->props.vdev;
168     BlockConf *conf = &blockdev->props.conf;
169     int64_t sectors = blk_getlength(conf->blk) / conf->logical_block_size;
170     XenDevice *xendev = XEN_DEVICE(blockdev);
171 
172     trace_xen_block_size(type, vdev->disk, vdev->partition, sectors);
173 
174     xen_device_backend_printf(xendev, "sectors", "%"PRIi64, sectors);
175 }
176 
177 static void xen_block_resize_cb(void *opaque)
178 {
179     XenBlockDevice *blockdev = opaque;
180     XenDevice *xendev = XEN_DEVICE(blockdev);
181     enum xenbus_state state = xen_device_backend_get_state(xendev);
182 
183     xen_block_set_size(blockdev);
184 
185     /*
186      * Mimic the behaviour of Linux xen-blkback and re-write the state
187      * to trigger the frontend watch.
188      */
189     xen_device_backend_printf(xendev, "state", "%u", state);
190 }
191 
192 static const BlockDevOps xen_block_dev_ops = {
193     .resize_cb = xen_block_resize_cb,
194 };
195 
196 static void xen_block_realize(XenDevice *xendev, Error **errp)
197 {
198     ERRP_GUARD();
199     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
200     XenBlockDeviceClass *blockdev_class =
201         XEN_BLOCK_DEVICE_GET_CLASS(xendev);
202     const char *type = object_get_typename(OBJECT(blockdev));
203     XenBlockVdev *vdev = &blockdev->props.vdev;
204     BlockConf *conf = &blockdev->props.conf;
205     BlockBackend *blk = conf->blk;
206 
207     if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
208         error_setg(errp, "vdev property not set");
209         return;
210     }
211 
212     trace_xen_block_realize(type, vdev->disk, vdev->partition);
213 
214     if (blockdev_class->realize) {
215         blockdev_class->realize(blockdev, errp);
216         if (*errp) {
217             return;
218         }
219     }
220 
221     /*
222      * The blkif protocol does not deal with removable media, so it must
223      * always be present, even for CDRom devices.
224      */
225     assert(blk);
226     if (!blk_is_inserted(blk)) {
227         error_setg(errp, "device needs media, but drive is empty");
228         return;
229     }
230 
231     if (!blkconf_apply_backend_options(conf, blockdev->info & VDISK_READONLY,
232                                        true, errp)) {
233         return;
234     }
235 
236     if (!(blockdev->info & VDISK_CDROM) &&
237         !blkconf_geometry(conf, NULL, 65535, 255, 255, errp)) {
238         return;
239     }
240 
241     if (!blkconf_blocksizes(conf, errp)) {
242         return;
243     }
244 
245     blk_set_dev_ops(blk, &xen_block_dev_ops, blockdev);
246     blk_set_guest_block_size(blk, conf->logical_block_size);
247 
248     if (conf->discard_granularity == -1) {
249         conf->discard_granularity = conf->physical_block_size;
250     }
251 
252     if (blk_get_flags(blk) & BDRV_O_UNMAP) {
253         xen_device_backend_printf(xendev, "feature-discard", "%u", 1);
254         xen_device_backend_printf(xendev, "discard-granularity", "%u",
255                                   conf->discard_granularity);
256     }
257 
258     xen_device_backend_printf(xendev, "feature-flush-cache", "%u", 1);
259     xen_device_backend_printf(xendev, "max-ring-page-order", "%u",
260                               blockdev->props.max_ring_page_order);
261     xen_device_backend_printf(xendev, "info", "%u", blockdev->info);
262 
263     xen_device_frontend_printf(xendev, "virtual-device", "%lu",
264                                vdev->number);
265     xen_device_frontend_printf(xendev, "device-type", "%s",
266                                blockdev->device_type);
267 
268     xen_device_backend_printf(xendev, "sector-size", "%u",
269                               conf->logical_block_size);
270 
271     xen_block_set_size(blockdev);
272 
273     blockdev->dataplane =
274         xen_block_dataplane_create(xendev, blk, conf->logical_block_size,
275                                    blockdev->props.iothread);
276 }
277 
278 static void xen_block_frontend_changed(XenDevice *xendev,
279                                        enum xenbus_state frontend_state,
280                                        Error **errp)
281 {
282     ERRP_GUARD();
283     enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
284 
285     switch (frontend_state) {
286     case XenbusStateInitialised:
287     case XenbusStateConnected:
288         if (backend_state == XenbusStateConnected) {
289             break;
290         }
291 
292         xen_block_disconnect(xendev, errp);
293         if (*errp) {
294             break;
295         }
296 
297         xen_block_connect(xendev, errp);
298         if (*errp) {
299             break;
300         }
301 
302         xen_device_backend_set_state(xendev, XenbusStateConnected);
303         break;
304 
305     case XenbusStateClosing:
306         xen_device_backend_set_state(xendev, XenbusStateClosing);
307         break;
308 
309     case XenbusStateClosed:
310     case XenbusStateUnknown:
311         xen_block_disconnect(xendev, errp);
312         if (*errp) {
313             break;
314         }
315 
316         xen_device_backend_set_state(xendev, XenbusStateClosed);
317         break;
318 
319     default:
320         break;
321     }
322 }
323 
324 static char *disk_to_vbd_name(unsigned int disk)
325 {
326     char *name, *prefix = (disk >= 26) ?
327         disk_to_vbd_name((disk / 26) - 1) : g_strdup("");
328 
329     name = g_strdup_printf("%s%c", prefix, 'a' + disk % 26);
330     g_free(prefix);
331 
332     return name;
333 }
334 
335 static void xen_block_get_vdev(Object *obj, Visitor *v, const char *name,
336                                void *opaque, Error **errp)
337 {
338     Property *prop = opaque;
339     XenBlockVdev *vdev = qdev_get_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     DeviceState *dev = DEVICE(obj);
399     Property *prop = opaque;
400     XenBlockVdev *vdev = qdev_get_prop_ptr(obj, prop);
401     char *str, *p;
402     const char *end;
403 
404     if (dev->realized) {
405         qdev_prop_set_after_realize(dev, name, errp);
406         return;
407     }
408 
409     if (!visit_type_str(v, name, &str, errp)) {
410         return;
411     }
412 
413     p = strchr(str, 'd');
414     if (!p) {
415         goto invalid;
416     }
417 
418     *p++ = '\0';
419     if (*str == '\0') {
420         vdev->type = XEN_BLOCK_VDEV_TYPE_DP;
421     } else if (strcmp(str, "xv") == 0) {
422         vdev->type = XEN_BLOCK_VDEV_TYPE_XVD;
423     } else if (strcmp(str, "h") == 0) {
424         vdev->type = XEN_BLOCK_VDEV_TYPE_HD;
425     } else if (strcmp(str, "s") == 0) {
426         vdev->type = XEN_BLOCK_VDEV_TYPE_SD;
427     } else {
428         goto invalid;
429     }
430 
431     if (vdev->type == XEN_BLOCK_VDEV_TYPE_DP) {
432         if (qemu_strtoul(p, &end, 10, &vdev->disk)) {
433             goto invalid;
434         }
435 
436         if (*end == 'p') {
437             if (*(++end) == '\0') {
438                 goto invalid;
439             }
440         }
441     } else {
442         if (vbd_name_to_disk(p, &end, &vdev->disk)) {
443             goto invalid;
444         }
445     }
446 
447     if (*end != '\0') {
448         p = (char *)end;
449 
450         if (qemu_strtoul(p, &end, 10, &vdev->partition)) {
451             goto invalid;
452         }
453 
454         if (*end != '\0') {
455             goto invalid;
456         }
457     } else {
458         vdev->partition = 0;
459     }
460 
461     switch (vdev->type) {
462     case XEN_BLOCK_VDEV_TYPE_DP:
463     case XEN_BLOCK_VDEV_TYPE_XVD:
464         if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
465             vdev->number = (202 << 8) | (vdev->disk << 4) |
466                 vdev->partition;
467         } else if (vdev->disk < (1 << 20) && vdev->partition < (1 << 8)) {
468             vdev->number = (1 << 28) | (vdev->disk << 8) |
469                 vdev->partition;
470         } else {
471             goto invalid;
472         }
473         break;
474 
475     case XEN_BLOCK_VDEV_TYPE_HD:
476         if ((vdev->disk == 0 || vdev->disk == 1) &&
477             vdev->partition < (1 << 6)) {
478             vdev->number = (3 << 8) | (vdev->disk << 6) | vdev->partition;
479         } else if ((vdev->disk == 2 || vdev->disk == 3) &&
480                    vdev->partition < (1 << 6)) {
481             vdev->number = (22 << 8) | ((vdev->disk - 2) << 6) |
482                 vdev->partition;
483         } else {
484             goto invalid;
485         }
486         break;
487 
488     case XEN_BLOCK_VDEV_TYPE_SD:
489         if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
490             vdev->number = (8 << 8) | (vdev->disk << 4) | vdev->partition;
491         } else {
492             goto invalid;
493         }
494         break;
495 
496     default:
497         goto invalid;
498     }
499 
500     g_free(str);
501     return;
502 
503 invalid:
504     error_setg(errp, "invalid virtual disk specifier");
505 
506     vdev->type = XEN_BLOCK_VDEV_TYPE_INVALID;
507     g_free(str);
508 }
509 
510 /*
511  * This property deals with 'vdev' names adhering to the Xen VBD naming
512  * scheme described in:
513  *
514  * https://xenbits.xen.org/docs/unstable/man/xen-vbd-interface.7.html
515  */
516 const PropertyInfo xen_block_prop_vdev = {
517     .name  = "str",
518     .description = "Virtual Disk specifier: d*p*/xvd*/hd*/sd*",
519     .get = xen_block_get_vdev,
520     .set = xen_block_set_vdev,
521 };
522 
523 static Property xen_block_props[] = {
524     DEFINE_PROP("vdev", XenBlockDevice, props.vdev,
525                 xen_block_prop_vdev, XenBlockVdev),
526     DEFINE_BLOCK_PROPERTIES(XenBlockDevice, props.conf),
527     DEFINE_PROP_UINT32("max-ring-page-order", XenBlockDevice,
528                        props.max_ring_page_order, 4),
529     DEFINE_PROP_LINK("iothread", XenBlockDevice, props.iothread,
530                      TYPE_IOTHREAD, IOThread *),
531     DEFINE_PROP_END_OF_LIST()
532 };
533 
534 static void xen_block_class_init(ObjectClass *class, void *data)
535 {
536     DeviceClass *dev_class = DEVICE_CLASS(class);
537     XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
538 
539     xendev_class->backend = "qdisk";
540     xendev_class->device = "vbd";
541     xendev_class->get_name = xen_block_get_name;
542     xendev_class->realize = xen_block_realize;
543     xendev_class->frontend_changed = xen_block_frontend_changed;
544     xendev_class->unrealize = xen_block_unrealize;
545 
546     device_class_set_props(dev_class, xen_block_props);
547 }
548 
549 static const TypeInfo xen_block_type_info = {
550     .name = TYPE_XEN_BLOCK_DEVICE,
551     .parent = TYPE_XEN_DEVICE,
552     .instance_size = sizeof(XenBlockDevice),
553     .abstract = true,
554     .class_size = sizeof(XenBlockDeviceClass),
555     .class_init = xen_block_class_init,
556 };
557 
558 static void xen_disk_unrealize(XenBlockDevice *blockdev)
559 {
560     trace_xen_disk_unrealize();
561 }
562 
563 static void xen_disk_realize(XenBlockDevice *blockdev, Error **errp)
564 {
565     BlockConf *conf = &blockdev->props.conf;
566 
567     trace_xen_disk_realize();
568 
569     blockdev->device_type = "disk";
570 
571     if (!conf->blk) {
572         error_setg(errp, "drive property not set");
573         return;
574     }
575 
576     blockdev->info = blk_is_read_only(conf->blk) ? VDISK_READONLY : 0;
577 }
578 
579 static void xen_disk_class_init(ObjectClass *class, void *data)
580 {
581     DeviceClass *dev_class = DEVICE_CLASS(class);
582     XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
583 
584     blockdev_class->realize = xen_disk_realize;
585     blockdev_class->unrealize = xen_disk_unrealize;
586 
587     dev_class->desc = "Xen Disk Device";
588 }
589 
590 static const TypeInfo xen_disk_type_info = {
591     .name = TYPE_XEN_DISK_DEVICE,
592     .parent = TYPE_XEN_BLOCK_DEVICE,
593     .instance_size = sizeof(XenDiskDevice),
594     .class_init = xen_disk_class_init,
595 };
596 
597 static void xen_cdrom_unrealize(XenBlockDevice *blockdev)
598 {
599     trace_xen_cdrom_unrealize();
600 }
601 
602 static void xen_cdrom_realize(XenBlockDevice *blockdev, Error **errp)
603 {
604     BlockConf *conf = &blockdev->props.conf;
605 
606     trace_xen_cdrom_realize();
607 
608     blockdev->device_type = "cdrom";
609 
610     if (!conf->blk) {
611         int rc;
612 
613         /* Set up an empty drive */
614         conf->blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
615 
616         rc = blk_attach_dev(conf->blk, DEVICE(blockdev));
617         if (!rc) {
618             error_setg_errno(errp, -rc, "failed to create drive");
619             return;
620         }
621     }
622 
623     blockdev->info = VDISK_READONLY | VDISK_CDROM;
624 }
625 
626 static void xen_cdrom_class_init(ObjectClass *class, void *data)
627 {
628     DeviceClass *dev_class = DEVICE_CLASS(class);
629     XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
630 
631     blockdev_class->realize = xen_cdrom_realize;
632     blockdev_class->unrealize = xen_cdrom_unrealize;
633 
634     dev_class->desc = "Xen CD-ROM Device";
635 }
636 
637 static const TypeInfo xen_cdrom_type_info = {
638     .name = TYPE_XEN_CDROM_DEVICE,
639     .parent = TYPE_XEN_BLOCK_DEVICE,
640     .instance_size = sizeof(XenCDRomDevice),
641     .class_init = xen_cdrom_class_init,
642 };
643 
644 static void xen_block_register_types(void)
645 {
646     type_register_static(&xen_block_type_info);
647     type_register_static(&xen_disk_type_info);
648     type_register_static(&xen_cdrom_type_info);
649 }
650 
651 type_init(xen_block_register_types)
652 
653 static void xen_block_blockdev_del(const char *node_name, Error **errp)
654 {
655     trace_xen_block_blockdev_del(node_name);
656 
657     qmp_blockdev_del(node_name, errp);
658 }
659 
660 static char *xen_block_blockdev_add(const char *id, QDict *qdict,
661                                     Error **errp)
662 {
663     ERRP_GUARD();
664     const char *driver = qdict_get_try_str(qdict, "driver");
665     BlockdevOptions *options = NULL;
666     char *node_name;
667     Visitor *v;
668 
669     if (!driver) {
670         error_setg(errp, "no 'driver' parameter");
671         return NULL;
672     }
673 
674     node_name = g_strdup_printf("%s-%s", id, driver);
675     qdict_put_str(qdict, "node-name", node_name);
676 
677     trace_xen_block_blockdev_add(node_name);
678 
679     v = qobject_input_visitor_new(QOBJECT(qdict));
680     visit_type_BlockdevOptions(v, NULL, &options, errp);
681     visit_free(v);
682     if (!options) {
683         goto fail;
684     }
685 
686     qmp_blockdev_add(options, errp);
687 
688     if (*errp) {
689         goto fail;
690     }
691 
692     qapi_free_BlockdevOptions(options);
693 
694     return node_name;
695 
696 fail:
697     if (options) {
698         qapi_free_BlockdevOptions(options);
699     }
700     g_free(node_name);
701 
702     return NULL;
703 }
704 
705 static void xen_block_drive_destroy(XenBlockDrive *drive, Error **errp)
706 {
707     ERRP_GUARD();
708     char *node_name = drive->node_name;
709 
710     if (node_name) {
711         xen_block_blockdev_del(node_name, errp);
712         if (*errp) {
713             return;
714         }
715         g_free(node_name);
716         drive->node_name = NULL;
717     }
718     g_free(drive->id);
719     g_free(drive);
720 }
721 
722 static XenBlockDrive *xen_block_drive_create(const char *id,
723                                              const char *device_type,
724                                              QDict *opts, Error **errp)
725 {
726     ERRP_GUARD();
727     const char *params = qdict_get_try_str(opts, "params");
728     const char *mode = qdict_get_try_str(opts, "mode");
729     const char *direct_io_safe = qdict_get_try_str(opts, "direct-io-safe");
730     const char *discard_enable = qdict_get_try_str(opts, "discard-enable");
731     char *driver = NULL;
732     char *filename = NULL;
733     XenBlockDrive *drive = NULL;
734     QDict *file_layer;
735     QDict *driver_layer;
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     qdict_put_str(file_layer, "driver", "file");
770     qdict_put_str(file_layer, "filename", filename);
771     g_free(filename);
772 
773     if (mode && *mode != 'w') {
774         qdict_put_bool(file_layer, "read-only", true);
775     }
776 
777     if (direct_io_safe) {
778         unsigned long value;
779 
780         if (!qemu_strtoul(direct_io_safe, NULL, 2, &value) && !!value) {
781             QDict *cache_qdict = qdict_new();
782 
783             qdict_put_bool(cache_qdict, "direct", true);
784             qdict_put(file_layer, "cache", cache_qdict);
785 
786             qdict_put_str(file_layer, "aio", "native");
787         }
788     }
789 
790     if (discard_enable) {
791         unsigned long value;
792 
793         if (!qemu_strtoul(discard_enable, NULL, 2, &value) && !!value) {
794             qdict_put_str(file_layer, "discard", "unmap");
795             qdict_put_str(driver_layer, "discard", "unmap");
796         }
797     }
798 
799     /*
800      * It is necessary to turn file locking off as an emulated device
801      * may have already opened the same image file.
802      */
803     qdict_put_str(file_layer, "locking", "off");
804 
805     qdict_put_str(driver_layer, "driver", driver);
806     g_free(driver);
807 
808     qdict_put(driver_layer, "file", file_layer);
809 
810     g_assert(!drive->node_name);
811     drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
812                                               errp);
813 
814     qobject_unref(driver_layer);
815 
816 done:
817     if (*errp) {
818         xen_block_drive_destroy(drive, NULL);
819         return NULL;
820     }
821 
822     return drive;
823 }
824 
825 static const char *xen_block_drive_get_node_name(XenBlockDrive *drive)
826 {
827     return drive->node_name ? drive->node_name : "";
828 }
829 
830 static void xen_block_iothread_destroy(XenBlockIOThread *iothread,
831                                        Error **errp)
832 {
833     qmp_object_del(iothread->id, errp);
834 
835     g_free(iothread->id);
836     g_free(iothread);
837 }
838 
839 static XenBlockIOThread *xen_block_iothread_create(const char *id,
840                                                    Error **errp)
841 {
842     ERRP_GUARD();
843     XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
844     QDict *opts;
845     QObject *ret_data = NULL;
846 
847     iothread->id = g_strdup(id);
848 
849     opts = qdict_new();
850     qdict_put_str(opts, "qom-type", TYPE_IOTHREAD);
851     qdict_put_str(opts, "id", id);
852     qmp_object_add(opts, &ret_data, errp);
853     qobject_unref(opts);
854     qobject_unref(ret_data);
855 
856     if (*errp) {
857         g_free(iothread->id);
858         g_free(iothread);
859         return NULL;
860     }
861 
862     return iothread;
863 }
864 
865 static void xen_block_device_create(XenBackendInstance *backend,
866                                     QDict *opts, Error **errp)
867 {
868     ERRP_GUARD();
869     XenBus *xenbus = xen_backend_get_bus(backend);
870     const char *name = xen_backend_get_name(backend);
871     unsigned long number;
872     const char *vdev, *device_type;
873     XenBlockDrive *drive = NULL;
874     XenBlockIOThread *iothread = NULL;
875     XenDevice *xendev = NULL;
876     const char *type;
877     XenBlockDevice *blockdev;
878 
879     if (qemu_strtoul(name, NULL, 10, &number)) {
880         error_setg(errp, "failed to parse name '%s'", name);
881         goto fail;
882     }
883 
884     trace_xen_block_device_create(number);
885 
886     vdev = qdict_get_try_str(opts, "dev");
887     if (!vdev) {
888         error_setg(errp, "no dev parameter");
889         goto fail;
890     }
891 
892     device_type = qdict_get_try_str(opts, "device-type");
893     if (!device_type) {
894         error_setg(errp, "no device-type parameter");
895         goto fail;
896     }
897 
898     if (!strcmp(device_type, "disk")) {
899         type = TYPE_XEN_DISK_DEVICE;
900     } else if (!strcmp(device_type, "cdrom")) {
901         type = TYPE_XEN_CDROM_DEVICE;
902     } else {
903         error_setg(errp, "invalid device-type parameter '%s'", device_type);
904         goto fail;
905     }
906 
907     drive = xen_block_drive_create(vdev, device_type, opts, errp);
908     if (!drive) {
909         error_prepend(errp, "failed to create drive: ");
910         goto fail;
911     }
912 
913     iothread = xen_block_iothread_create(vdev, errp);
914     if (*errp) {
915         error_prepend(errp, "failed to create iothread: ");
916         goto fail;
917     }
918 
919     xendev = XEN_DEVICE(qdev_new(type));
920     blockdev = XEN_BLOCK_DEVICE(xendev);
921 
922     if (!object_property_set_str(OBJECT(xendev), "vdev", vdev,
923                                  errp)) {
924         error_prepend(errp, "failed to set 'vdev': ");
925         goto fail;
926     }
927 
928     if (!object_property_set_str(OBJECT(xendev), "drive",
929                                  xen_block_drive_get_node_name(drive),
930                                  errp)) {
931         error_prepend(errp, "failed to set 'drive': ");
932         goto fail;
933     }
934 
935     if (!object_property_set_str(OBJECT(xendev), "iothread", iothread->id,
936                                  errp)) {
937         error_prepend(errp, "failed to set 'iothread': ");
938         goto fail;
939     }
940 
941     blockdev->iothread = iothread;
942     blockdev->drive = drive;
943 
944     if (!qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
945         error_prepend(errp, "realization of device %s failed: ", type);
946         goto fail;
947     }
948 
949     xen_backend_set_device(backend, xendev);
950     return;
951 
952 fail:
953     if (xendev) {
954         object_unparent(OBJECT(xendev));
955     }
956 
957     if (iothread) {
958         xen_block_iothread_destroy(iothread, NULL);
959     }
960 
961     if (drive) {
962         xen_block_drive_destroy(drive, NULL);
963     }
964 }
965 
966 static void xen_block_device_destroy(XenBackendInstance *backend,
967                                      Error **errp)
968 {
969     ERRP_GUARD();
970     XenDevice *xendev = xen_backend_get_device(backend);
971     XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
972     XenBlockVdev *vdev = &blockdev->props.vdev;
973     XenBlockDrive *drive = blockdev->drive;
974     XenBlockIOThread *iothread = blockdev->iothread;
975 
976     trace_xen_block_device_destroy(vdev->number);
977 
978     object_unparent(OBJECT(xendev));
979 
980     if (iothread) {
981         xen_block_iothread_destroy(iothread, errp);
982         if (*errp) {
983             error_prepend(errp, "failed to destroy iothread: ");
984             return;
985         }
986     }
987 
988     if (drive) {
989         xen_block_drive_destroy(drive, errp);
990         if (*errp) {
991             error_prepend(errp, "failed to destroy drive: ");
992             return;
993         }
994     }
995 }
996 
997 static const XenBackendInfo xen_block_backend_info = {
998     .type = "qdisk",
999     .create = xen_block_device_create,
1000     .destroy = xen_block_device_destroy,
1001 };
1002 
1003 static void xen_block_register_backend(void)
1004 {
1005     xen_backend_register(&xen_block_backend_info);
1006 }
1007 
1008 xen_backend_init(xen_block_register_backend);
1009