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 "qobject/qdict.h"
20 #include "qobject/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 "system/blockdev.h"
27 #include "system/block-backend.h"
28 #include "system/iothread.h"
29 #include "dataplane/xen-block.h"
30 #include "hw/xen/interface/io/xs_wire.h"
31 #include "trace.h"
32
33 #define XVDA_MAJOR 202
34 #define XVDQ_MAJOR (1 << 20)
35 #define XVDBGQCV_MAJOR ((1 << 21) - 1)
36 #define HDA_MAJOR 3
37 #define HDC_MAJOR 22
38 #define SDA_MAJOR 8
39
40
vdev_to_diskno(unsigned int vdev_nr)41 static int vdev_to_diskno(unsigned int vdev_nr)
42 {
43 switch (vdev_nr >> 8) {
44 case XVDA_MAJOR:
45 case SDA_MAJOR:
46 return (vdev_nr >> 4) & 0x15;
47
48 case HDA_MAJOR:
49 return (vdev_nr >> 6) & 1;
50
51 case HDC_MAJOR:
52 return ((vdev_nr >> 6) & 1) + 2;
53
54 case XVDQ_MAJOR ... XVDBGQCV_MAJOR:
55 return (vdev_nr >> 8) & 0xfffff;
56
57 default:
58 return -1;
59 }
60 }
61
62 #define MAX_AUTO_VDEV 4096
63
64 /*
65 * Find a free device name in the xvda → xvdfan range and set it in
66 * blockdev->props.vdev. Our definition of "free" is that there must
67 * be no other disk or partition with the same disk number.
68 *
69 * You are technically permitted to have all of hda, hda1, sda, sda1,
70 * xvda and xvda1 as *separate* PV block devices with separate backing
71 * stores. That doesn't make it a good idea. This code will skip xvda
72 * if *any* of those "conflicting" devices already exists.
73 *
74 * The limit of xvdfan (disk 4095) is fairly arbitrary just to avoid a
75 * stupidly sized bitmap, but Linux as of v6.6 doesn't support anything
76 * higher than that anyway.
77 */
xen_block_find_free_vdev(XenBlockDevice * blockdev,Error ** errp)78 static bool xen_block_find_free_vdev(XenBlockDevice *blockdev, Error **errp)
79 {
80 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(blockdev)));
81 unsigned long used_devs[BITS_TO_LONGS(MAX_AUTO_VDEV)];
82 XenBlockVdev *vdev = &blockdev->props.vdev;
83 char fe_path[XENSTORE_ABS_PATH_MAX + 1];
84 char **existing_frontends;
85 unsigned int nr_existing = 0;
86 unsigned int vdev_nr;
87 int i, disk = 0;
88
89 snprintf(fe_path, sizeof(fe_path), "/local/domain/%u/device/vbd",
90 blockdev->xendev.frontend_id);
91
92 existing_frontends = qemu_xen_xs_directory(xenbus->xsh, XBT_NULL, fe_path,
93 &nr_existing);
94 if (!existing_frontends) {
95 if (errno == ENOENT) {
96 /*
97 * If the frontend directory doesn't exist because there are
98 * no existing vbd devices, that's fine. Just ensure that we
99 * don't dereference the NULL existing_frontends pointer, by
100 * checking that nr_existing is zero so the loop below is not
101 * entered.
102 *
103 * In fact this is redundant since nr_existing is initialized
104 * to zero, but setting it again here makes it abundantly clear
105 * to Coverity, and to the human reader who doesn't know the
106 * semantics of qemu_xen_xs_directory() off the top of their
107 * head.
108 */
109 nr_existing = 0;
110 } else {
111 /* All other errors accessing the frontend directory are fatal. */
112 error_setg_errno(errp, errno, "cannot read %s", fe_path);
113 return false;
114 }
115 }
116
117 memset(used_devs, 0, sizeof(used_devs));
118 for (i = 0; i < nr_existing; i++) {
119 if (qemu_strtoui(existing_frontends[i], NULL, 10, &vdev_nr)) {
120 free(existing_frontends[i]);
121 continue;
122 }
123
124 free(existing_frontends[i]);
125
126 disk = vdev_to_diskno(vdev_nr);
127 if (disk < 0 || disk >= MAX_AUTO_VDEV) {
128 continue;
129 }
130
131 set_bit(disk, used_devs);
132 }
133 free(existing_frontends);
134
135 disk = find_first_zero_bit(used_devs, MAX_AUTO_VDEV);
136 if (disk == MAX_AUTO_VDEV) {
137 error_setg(errp, "cannot find device vdev for block device");
138 return false;
139 }
140
141 vdev->type = XEN_BLOCK_VDEV_TYPE_XVD;
142 vdev->partition = 0;
143 vdev->disk = disk;
144 if (disk < (1 << 4)) {
145 vdev->number = (XVDA_MAJOR << 8) | (disk << 4);
146 } else {
147 vdev->number = (XVDQ_MAJOR << 8) | (disk << 8);
148 }
149 return true;
150 }
151
xen_block_get_name(XenDevice * xendev,Error ** errp)152 static char *xen_block_get_name(XenDevice *xendev, Error **errp)
153 {
154 XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
155 XenBlockVdev *vdev = &blockdev->props.vdev;
156
157 if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID &&
158 !xen_block_find_free_vdev(blockdev, errp)) {
159 return NULL;
160 }
161 return g_strdup_printf("%lu", vdev->number);
162 }
163
xen_block_disconnect(XenDevice * xendev,Error ** errp)164 static void xen_block_disconnect(XenDevice *xendev, Error **errp)
165 {
166 XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
167 const char *type = object_get_typename(OBJECT(blockdev));
168 XenBlockVdev *vdev = &blockdev->props.vdev;
169
170 trace_xen_block_disconnect(type, vdev->disk, vdev->partition);
171
172 xen_block_dataplane_stop(blockdev->dataplane);
173 }
174
xen_block_connect(XenDevice * xendev,Error ** errp)175 static void xen_block_connect(XenDevice *xendev, Error **errp)
176 {
177 XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
178 const char *type = object_get_typename(OBJECT(blockdev));
179 XenBlockVdev *vdev = &blockdev->props.vdev;
180 BlockConf *conf = &blockdev->props.conf;
181 unsigned int feature_large_sector_size;
182 unsigned int order, nr_ring_ref, *ring_ref, event_channel, protocol;
183 char *str;
184
185 trace_xen_block_connect(type, vdev->disk, vdev->partition);
186
187 if (xen_device_frontend_scanf(xendev, "feature-large-sector-size", "%u",
188 &feature_large_sector_size) != 1) {
189 feature_large_sector_size = 0;
190 }
191
192 if (feature_large_sector_size != 1 &&
193 conf->logical_block_size != XEN_BLKIF_SECTOR_SIZE) {
194 error_setg(errp, "logical_block_size != %u not supported by frontend",
195 XEN_BLKIF_SECTOR_SIZE);
196 return;
197 }
198
199 if (xen_device_frontend_scanf(xendev, "ring-page-order", "%u",
200 &order) != 1) {
201 nr_ring_ref = 1;
202 ring_ref = g_new(unsigned int, nr_ring_ref);
203
204 if (xen_device_frontend_scanf(xendev, "ring-ref", "%u",
205 &ring_ref[0]) != 1) {
206 error_setg(errp, "failed to read ring-ref");
207 g_free(ring_ref);
208 return;
209 }
210 } else if (qemu_xen_gnttab_can_map_multi() &&
211 order <= blockdev->props.max_ring_page_order) {
212 unsigned int i;
213
214 nr_ring_ref = 1 << order;
215 ring_ref = g_new(unsigned int, nr_ring_ref);
216
217 for (i = 0; i < nr_ring_ref; i++) {
218 const char *key = g_strdup_printf("ring-ref%u", i);
219
220 if (xen_device_frontend_scanf(xendev, key, "%u",
221 &ring_ref[i]) != 1) {
222 error_setg(errp, "failed to read %s", key);
223 g_free((gpointer)key);
224 g_free(ring_ref);
225 return;
226 }
227
228 g_free((gpointer)key);
229 }
230 } else {
231 error_setg(errp, "invalid ring-page-order (%d)", order);
232 return;
233 }
234
235 if (xen_device_frontend_scanf(xendev, "event-channel", "%u",
236 &event_channel) != 1) {
237 error_setg(errp, "failed to read event-channel");
238 g_free(ring_ref);
239 return;
240 }
241
242 str = xen_device_frontend_read(xendev, "protocol");
243 if (!str) {
244 /* x86 defaults to the 32-bit protocol even for 64-bit guests. */
245 if (object_dynamic_cast(OBJECT(qdev_get_machine()), "x86-machine")) {
246 protocol = BLKIF_PROTOCOL_X86_32;
247 } else {
248 protocol = BLKIF_PROTOCOL_NATIVE;
249 }
250 } else {
251 if (strcmp(str, XEN_IO_PROTO_ABI_X86_32) == 0) {
252 protocol = BLKIF_PROTOCOL_X86_32;
253 } else if (strcmp(str, XEN_IO_PROTO_ABI_X86_64) == 0) {
254 protocol = BLKIF_PROTOCOL_X86_64;
255 } else {
256 protocol = BLKIF_PROTOCOL_NATIVE;
257 }
258
259 free(str);
260 }
261
262 xen_block_dataplane_start(blockdev->dataplane, ring_ref, nr_ring_ref,
263 event_channel, protocol, errp);
264
265 g_free(ring_ref);
266 }
267
xen_block_unrealize(XenDevice * xendev)268 static void xen_block_unrealize(XenDevice *xendev)
269 {
270 XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
271 XenBlockDeviceClass *blockdev_class =
272 XEN_BLOCK_DEVICE_GET_CLASS(xendev);
273 const char *type = object_get_typename(OBJECT(blockdev));
274 XenBlockVdev *vdev = &blockdev->props.vdev;
275
276 if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
277 return;
278 }
279
280 trace_xen_block_unrealize(type, vdev->disk, vdev->partition);
281
282 /* Disconnect from the frontend in case this has not already happened */
283 xen_block_disconnect(xendev, NULL);
284
285 xen_block_dataplane_destroy(blockdev->dataplane);
286 blockdev->dataplane = NULL;
287
288 if (blockdev_class->unrealize) {
289 blockdev_class->unrealize(blockdev);
290 }
291 }
292
xen_block_set_size(XenBlockDevice * blockdev)293 static void xen_block_set_size(XenBlockDevice *blockdev)
294 {
295 const char *type = object_get_typename(OBJECT(blockdev));
296 XenBlockVdev *vdev = &blockdev->props.vdev;
297 BlockConf *conf = &blockdev->props.conf;
298 int64_t sectors = blk_getlength(conf->blk) / conf->logical_block_size;
299 XenDevice *xendev = XEN_DEVICE(blockdev);
300
301 trace_xen_block_size(type, vdev->disk, vdev->partition, sectors);
302
303 xen_device_backend_printf(xendev, "sectors", "%"PRIi64, sectors);
304 }
305
xen_block_resize_cb(void * opaque)306 static void xen_block_resize_cb(void *opaque)
307 {
308 XenBlockDevice *blockdev = opaque;
309 XenDevice *xendev = XEN_DEVICE(blockdev);
310 enum xenbus_state state = xen_device_backend_get_state(xendev);
311
312 xen_block_set_size(blockdev);
313
314 /*
315 * Mimic the behaviour of Linux xen-blkback and re-write the state
316 * to trigger the frontend watch.
317 */
318 xen_device_backend_printf(xendev, "state", "%u", state);
319 }
320
321 /* Suspend request handling */
xen_block_drained_begin(void * opaque)322 static void xen_block_drained_begin(void *opaque)
323 {
324 XenBlockDevice *blockdev = opaque;
325
326 xen_block_dataplane_detach(blockdev->dataplane);
327 }
328
329 /* Resume request handling */
xen_block_drained_end(void * opaque)330 static void xen_block_drained_end(void *opaque)
331 {
332 XenBlockDevice *blockdev = opaque;
333
334 xen_block_dataplane_attach(blockdev->dataplane);
335 }
336
337 static const BlockDevOps xen_block_dev_ops = {
338 .resize_cb = xen_block_resize_cb,
339 .drained_begin = xen_block_drained_begin,
340 .drained_end = xen_block_drained_end,
341 };
342
xen_block_realize(XenDevice * xendev,Error ** errp)343 static void xen_block_realize(XenDevice *xendev, Error **errp)
344 {
345 ERRP_GUARD();
346 XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
347 XenBlockDeviceClass *blockdev_class =
348 XEN_BLOCK_DEVICE_GET_CLASS(xendev);
349 const char *type = object_get_typename(OBJECT(blockdev));
350 XenBlockVdev *vdev = &blockdev->props.vdev;
351 BlockConf *conf = &blockdev->props.conf;
352 BlockBackend *blk = conf->blk;
353
354 if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
355 error_setg(errp, "vdev property not set");
356 return;
357 }
358
359 trace_xen_block_realize(type, vdev->disk, vdev->partition);
360
361 if (blockdev_class->realize) {
362 blockdev_class->realize(blockdev, errp);
363 if (*errp) {
364 return;
365 }
366 }
367
368 /*
369 * The blkif protocol does not deal with removable media, so it must
370 * always be present, even for CDRom devices.
371 */
372 assert(blk);
373 if (!blk_is_inserted(blk)) {
374 error_setg(errp, "device needs media, but drive is empty");
375 return;
376 }
377
378 if (!blkconf_apply_backend_options(conf, blockdev->info & VDISK_READONLY,
379 true, errp)) {
380 return;
381 }
382
383 if (!(blockdev->info & VDISK_CDROM) &&
384 !blkconf_geometry(conf, NULL, 65535, 255, 255, errp)) {
385 return;
386 }
387
388 if (!blkconf_blocksizes(conf, errp)) {
389 return;
390 }
391
392 if (conf->discard_granularity == -1) {
393 conf->discard_granularity = conf->physical_block_size;
394 }
395
396 if (blk_get_flags(blk) & BDRV_O_UNMAP) {
397 xen_device_backend_printf(xendev, "feature-discard", "%u", 1);
398 xen_device_backend_printf(xendev, "discard-granularity", "%u",
399 conf->discard_granularity);
400 xen_device_backend_printf(xendev, "discard-alignment", "%u", 0);
401 }
402
403 xen_device_backend_printf(xendev, "feature-flush-cache", "%u", 1);
404
405 if (qemu_xen_gnttab_can_map_multi()) {
406 xen_device_backend_printf(xendev, "max-ring-page-order", "%u",
407 blockdev->props.max_ring_page_order);
408 }
409
410 xen_device_backend_printf(xendev, "info", "%u", blockdev->info);
411 xen_device_backend_printf(xendev, "mode",
412 (blockdev->info & VDISK_READONLY) ? "r" : "w");
413
414 xen_device_frontend_printf(xendev, "virtual-device", "%lu",
415 vdev->number);
416 xen_device_frontend_printf(xendev, "device-type", "%s",
417 blockdev->device_type);
418
419 xen_device_backend_printf(xendev, "sector-size", "%u",
420 conf->logical_block_size);
421
422 xen_block_set_size(blockdev);
423
424 blockdev->dataplane =
425 xen_block_dataplane_create(xendev, blk, conf->logical_block_size,
426 blockdev->props.iothread);
427
428 blk_set_dev_ops(blk, &xen_block_dev_ops, blockdev);
429 }
430
xen_block_frontend_changed(XenDevice * xendev,enum xenbus_state frontend_state,Error ** errp)431 static void xen_block_frontend_changed(XenDevice *xendev,
432 enum xenbus_state frontend_state,
433 Error **errp)
434 {
435 ERRP_GUARD();
436 enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
437
438 switch (frontend_state) {
439 case XenbusStateInitialised:
440 case XenbusStateConnected:
441 if (backend_state == XenbusStateConnected) {
442 break;
443 }
444
445 xen_block_disconnect(xendev, errp);
446 if (*errp) {
447 break;
448 }
449
450 xen_block_connect(xendev, errp);
451 if (*errp) {
452 break;
453 }
454
455 xen_device_backend_set_state(xendev, XenbusStateConnected);
456 break;
457
458 case XenbusStateClosing:
459 xen_device_backend_set_state(xendev, XenbusStateClosing);
460 break;
461
462 case XenbusStateClosed:
463 case XenbusStateUnknown:
464 xen_block_disconnect(xendev, errp);
465 if (*errp) {
466 break;
467 }
468
469 xen_device_backend_set_state(xendev, XenbusStateClosed);
470 break;
471
472 default:
473 break;
474 }
475 }
476
disk_to_vbd_name(unsigned int disk)477 static char *disk_to_vbd_name(unsigned int disk)
478 {
479 char *name, *prefix = (disk >= 26) ?
480 disk_to_vbd_name((disk / 26) - 1) : g_strdup("");
481
482 name = g_strdup_printf("%s%c", prefix, 'a' + disk % 26);
483 g_free(prefix);
484
485 return name;
486 }
487
xen_block_get_vdev(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)488 static void xen_block_get_vdev(Object *obj, Visitor *v, const char *name,
489 void *opaque, Error **errp)
490 {
491 const Property *prop = opaque;
492 XenBlockVdev *vdev = object_field_prop_ptr(obj, prop);
493 char *str;
494
495 switch (vdev->type) {
496 case XEN_BLOCK_VDEV_TYPE_DP:
497 str = g_strdup_printf("d%lup%lu", vdev->disk, vdev->partition);
498 break;
499
500 case XEN_BLOCK_VDEV_TYPE_XVD:
501 case XEN_BLOCK_VDEV_TYPE_HD:
502 case XEN_BLOCK_VDEV_TYPE_SD: {
503 char *vbd_name = disk_to_vbd_name(vdev->disk);
504
505 str = g_strdup_printf("%s%s%lu",
506 (vdev->type == XEN_BLOCK_VDEV_TYPE_XVD) ?
507 "xvd" :
508 (vdev->type == XEN_BLOCK_VDEV_TYPE_HD) ?
509 "hd" :
510 "sd",
511 vbd_name, vdev->partition);
512 g_free(vbd_name);
513 break;
514 }
515 default:
516 error_setg(errp, "invalid vdev type");
517 return;
518 }
519
520 visit_type_str(v, name, &str, errp);
521 g_free(str);
522 }
523
vbd_name_to_disk(const char * name,const char ** endp,unsigned long * disk)524 static int vbd_name_to_disk(const char *name, const char **endp,
525 unsigned long *disk)
526 {
527 unsigned int n = 0;
528
529 while (*name != '\0') {
530 if (!g_ascii_isalpha(*name) || !g_ascii_islower(*name)) {
531 break;
532 }
533
534 n *= 26;
535 n += *name++ - 'a' + 1;
536 }
537 *endp = name;
538
539 if (!n) {
540 return -1;
541 }
542
543 *disk = n - 1;
544
545 return 0;
546 }
547
xen_block_set_vdev(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)548 static void xen_block_set_vdev(Object *obj, Visitor *v, const char *name,
549 void *opaque, Error **errp)
550 {
551 const Property *prop = opaque;
552 XenBlockVdev *vdev = object_field_prop_ptr(obj, prop);
553 char *str, *p;
554 const char *end;
555
556 if (!visit_type_str(v, name, &str, errp)) {
557 return;
558 }
559
560 p = strchr(str, 'd');
561 if (!p) {
562 goto invalid;
563 }
564
565 *p++ = '\0';
566 if (*str == '\0') {
567 vdev->type = XEN_BLOCK_VDEV_TYPE_DP;
568 } else if (strcmp(str, "xv") == 0) {
569 vdev->type = XEN_BLOCK_VDEV_TYPE_XVD;
570 } else if (strcmp(str, "h") == 0) {
571 vdev->type = XEN_BLOCK_VDEV_TYPE_HD;
572 } else if (strcmp(str, "s") == 0) {
573 vdev->type = XEN_BLOCK_VDEV_TYPE_SD;
574 } else {
575 goto invalid;
576 }
577
578 if (vdev->type == XEN_BLOCK_VDEV_TYPE_DP) {
579 if (qemu_strtoul(p, &end, 10, &vdev->disk)) {
580 goto invalid;
581 }
582
583 if (*end == 'p') {
584 if (*(++end) == '\0') {
585 goto invalid;
586 }
587 }
588 } else {
589 if (vbd_name_to_disk(p, &end, &vdev->disk)) {
590 goto invalid;
591 }
592 }
593
594 if (*end != '\0') {
595 p = (char *)end;
596
597 if (qemu_strtoul(p, &end, 10, &vdev->partition)) {
598 goto invalid;
599 }
600
601 if (*end != '\0') {
602 goto invalid;
603 }
604 } else {
605 vdev->partition = 0;
606 }
607
608 switch (vdev->type) {
609 case XEN_BLOCK_VDEV_TYPE_DP:
610 case XEN_BLOCK_VDEV_TYPE_XVD:
611 if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
612 vdev->number = (XVDA_MAJOR << 8) | (vdev->disk << 4) |
613 vdev->partition;
614 } else if (vdev->disk < (1 << 20) && vdev->partition < (1 << 8)) {
615 vdev->number = (XVDQ_MAJOR << 8) | (vdev->disk << 8) |
616 vdev->partition;
617 } else {
618 goto invalid;
619 }
620 break;
621
622 case XEN_BLOCK_VDEV_TYPE_HD:
623 if ((vdev->disk == 0 || vdev->disk == 1) &&
624 vdev->partition < (1 << 6)) {
625 vdev->number = (HDA_MAJOR << 8) | (vdev->disk << 6) |
626 vdev->partition;
627 } else if ((vdev->disk == 2 || vdev->disk == 3) &&
628 vdev->partition < (1 << 6)) {
629 vdev->number = (HDC_MAJOR << 8) | ((vdev->disk - 2) << 6) |
630 vdev->partition;
631 } else {
632 goto invalid;
633 }
634 break;
635
636 case XEN_BLOCK_VDEV_TYPE_SD:
637 if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
638 vdev->number = (SDA_MAJOR << 8) | (vdev->disk << 4) |
639 vdev->partition;
640 } else {
641 goto invalid;
642 }
643 break;
644
645 default:
646 goto invalid;
647 }
648
649 g_free(str);
650 return;
651
652 invalid:
653 error_setg(errp, "invalid virtual disk specifier");
654
655 vdev->type = XEN_BLOCK_VDEV_TYPE_INVALID;
656 g_free(str);
657 }
658
659 /*
660 * This property deals with 'vdev' names adhering to the Xen VBD naming
661 * scheme described in:
662 *
663 * https://xenbits.xen.org/docs/unstable/man/xen-vbd-interface.7.html
664 */
665 static const PropertyInfo xen_block_prop_vdev = {
666 .type = "str",
667 .description = "Virtual Disk specifier (d*p*/xvd*/hd*/sd*)",
668 .get = xen_block_get_vdev,
669 .set = xen_block_set_vdev,
670 };
671
672 static const Property xen_block_props[] = {
673 DEFINE_PROP("vdev", XenBlockDevice, props.vdev,
674 xen_block_prop_vdev, XenBlockVdev),
675 DEFINE_BLOCK_PROPERTIES(XenBlockDevice, props.conf),
676 DEFINE_PROP_UINT32("max-ring-page-order", XenBlockDevice,
677 props.max_ring_page_order, 4),
678 DEFINE_PROP_LINK("iothread", XenBlockDevice, props.iothread,
679 TYPE_IOTHREAD, IOThread *),
680 };
681
xen_block_class_init(ObjectClass * class,const void * data)682 static void xen_block_class_init(ObjectClass *class, const void *data)
683 {
684 DeviceClass *dev_class = DEVICE_CLASS(class);
685 XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
686
687 xendev_class->backend = "qdisk";
688 xendev_class->device = "vbd";
689 xendev_class->get_name = xen_block_get_name;
690 xendev_class->realize = xen_block_realize;
691 xendev_class->frontend_changed = xen_block_frontend_changed;
692 xendev_class->unrealize = xen_block_unrealize;
693
694 device_class_set_props(dev_class, xen_block_props);
695 }
696
697 static const TypeInfo xen_block_type_info = {
698 .name = TYPE_XEN_BLOCK_DEVICE,
699 .parent = TYPE_XEN_DEVICE,
700 .instance_size = sizeof(XenBlockDevice),
701 .abstract = true,
702 .class_size = sizeof(XenBlockDeviceClass),
703 .class_init = xen_block_class_init,
704 };
705
xen_disk_unrealize(XenBlockDevice * blockdev)706 static void xen_disk_unrealize(XenBlockDevice *blockdev)
707 {
708 trace_xen_disk_unrealize();
709 }
710
xen_disk_realize(XenBlockDevice * blockdev,Error ** errp)711 static void xen_disk_realize(XenBlockDevice *blockdev, Error **errp)
712 {
713 BlockConf *conf = &blockdev->props.conf;
714
715 trace_xen_disk_realize();
716
717 blockdev->device_type = "disk";
718
719 if (!conf->blk) {
720 error_setg(errp, "drive property not set");
721 return;
722 }
723
724 blockdev->info = blk_supports_write_perm(conf->blk) ? 0 : VDISK_READONLY;
725 }
726
xen_disk_class_init(ObjectClass * class,const void * data)727 static void xen_disk_class_init(ObjectClass *class, const void *data)
728 {
729 DeviceClass *dev_class = DEVICE_CLASS(class);
730 XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
731
732 blockdev_class->realize = xen_disk_realize;
733 blockdev_class->unrealize = xen_disk_unrealize;
734
735 dev_class->desc = "Xen Disk Device";
736 }
737
738 static const TypeInfo xen_disk_type_info = {
739 .name = TYPE_XEN_DISK_DEVICE,
740 .parent = TYPE_XEN_BLOCK_DEVICE,
741 .instance_size = sizeof(XenDiskDevice),
742 .class_init = xen_disk_class_init,
743 };
744
xen_cdrom_unrealize(XenBlockDevice * blockdev)745 static void xen_cdrom_unrealize(XenBlockDevice *blockdev)
746 {
747 trace_xen_cdrom_unrealize();
748 }
749
xen_cdrom_realize(XenBlockDevice * blockdev,Error ** errp)750 static void xen_cdrom_realize(XenBlockDevice *blockdev, Error **errp)
751 {
752 BlockConf *conf = &blockdev->props.conf;
753
754 trace_xen_cdrom_realize();
755
756 blockdev->device_type = "cdrom";
757
758 if (!conf->blk) {
759 int rc;
760
761 /* Set up an empty drive */
762 conf->blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
763
764 rc = blk_attach_dev(conf->blk, DEVICE(blockdev));
765 if (!rc) {
766 error_setg_errno(errp, -rc, "failed to create drive");
767 return;
768 }
769 }
770
771 blockdev->info = VDISK_READONLY | VDISK_CDROM;
772 }
773
xen_cdrom_class_init(ObjectClass * class,const void * data)774 static void xen_cdrom_class_init(ObjectClass *class, const void *data)
775 {
776 DeviceClass *dev_class = DEVICE_CLASS(class);
777 XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
778
779 blockdev_class->realize = xen_cdrom_realize;
780 blockdev_class->unrealize = xen_cdrom_unrealize;
781
782 dev_class->desc = "Xen CD-ROM Device";
783 }
784
785 static const TypeInfo xen_cdrom_type_info = {
786 .name = TYPE_XEN_CDROM_DEVICE,
787 .parent = TYPE_XEN_BLOCK_DEVICE,
788 .instance_size = sizeof(XenCDRomDevice),
789 .class_init = xen_cdrom_class_init,
790 };
791
xen_block_register_types(void)792 static void xen_block_register_types(void)
793 {
794 type_register_static(&xen_block_type_info);
795 type_register_static(&xen_disk_type_info);
796 type_register_static(&xen_cdrom_type_info);
797 }
798
type_init(xen_block_register_types)799 type_init(xen_block_register_types)
800
801 static void xen_block_blockdev_del(const char *node_name, Error **errp)
802 {
803 trace_xen_block_blockdev_del(node_name);
804
805 qmp_blockdev_del(node_name, errp);
806 }
807
xen_block_blockdev_add(const char * id,QDict * qdict,Error ** errp)808 static char *xen_block_blockdev_add(const char *id, QDict *qdict,
809 Error **errp)
810 {
811 ERRP_GUARD();
812 const char *driver = qdict_get_try_str(qdict, "driver");
813 BlockdevOptions *options = NULL;
814 char *node_name;
815 Visitor *v;
816
817 if (!driver) {
818 error_setg(errp, "no 'driver' parameter");
819 return NULL;
820 }
821
822 node_name = g_strdup_printf("%s-%s", id, driver);
823 qdict_put_str(qdict, "node-name", node_name);
824
825 trace_xen_block_blockdev_add(node_name);
826
827 v = qobject_input_visitor_new(QOBJECT(qdict));
828 visit_type_BlockdevOptions(v, NULL, &options, errp);
829 visit_free(v);
830 if (!options) {
831 goto fail;
832 }
833
834 qmp_blockdev_add(options, errp);
835
836 if (*errp) {
837 goto fail;
838 }
839
840 qapi_free_BlockdevOptions(options);
841
842 return node_name;
843
844 fail:
845 if (options) {
846 qapi_free_BlockdevOptions(options);
847 }
848 g_free(node_name);
849
850 return NULL;
851 }
852
xen_block_drive_destroy(XenBlockDrive * drive,Error ** errp)853 static void xen_block_drive_destroy(XenBlockDrive *drive, Error **errp)
854 {
855 ERRP_GUARD();
856 char *node_name = drive->node_name;
857
858 if (node_name) {
859 xen_block_blockdev_del(node_name, errp);
860 if (*errp) {
861 return;
862 }
863 g_free(node_name);
864 drive->node_name = NULL;
865 }
866 g_free(drive->id);
867 g_free(drive);
868 }
869
xen_block_drive_create(const char * id,const char * device_type,QDict * opts,Error ** errp)870 static XenBlockDrive *xen_block_drive_create(const char *id,
871 const char *device_type,
872 QDict *opts, Error **errp)
873 {
874 ERRP_GUARD();
875 const char *params = qdict_get_try_str(opts, "params");
876 const char *mode = qdict_get_try_str(opts, "mode");
877 const char *direct_io_safe = qdict_get_try_str(opts, "direct-io-safe");
878 const char *discard_enable = qdict_get_try_str(opts, "discard-enable");
879 char *driver = NULL;
880 char *filename = NULL;
881 XenBlockDrive *drive = NULL;
882 QDict *file_layer;
883 QDict *driver_layer;
884 struct stat st;
885 int rc;
886
887 if (params) {
888 char **v = g_strsplit(params, ":", 2);
889
890 if (v[1] == NULL) {
891 filename = g_strdup(v[0]);
892 driver = g_strdup("raw");
893 } else {
894 if (strcmp(v[0], "aio") == 0) {
895 driver = g_strdup("raw");
896 } else if (strcmp(v[0], "vhd") == 0) {
897 driver = g_strdup("vpc");
898 } else {
899 driver = g_strdup(v[0]);
900 }
901 filename = g_strdup(v[1]);
902 }
903
904 g_strfreev(v);
905 } else {
906 error_setg(errp, "no params");
907 goto done;
908 }
909
910 assert(filename);
911 assert(driver);
912
913 drive = g_new0(XenBlockDrive, 1);
914 drive->id = g_strdup(id);
915
916 rc = stat(filename, &st);
917 if (rc) {
918 error_setg_errno(errp, errno, "Could not stat file '%s'", filename);
919 goto done;
920 }
921
922 file_layer = qdict_new();
923 driver_layer = qdict_new();
924
925 if (S_ISBLK(st.st_mode)) {
926 qdict_put_str(file_layer, "driver", "host_device");
927 } else {
928 qdict_put_str(file_layer, "driver", "file");
929 }
930
931 qdict_put_str(file_layer, "filename", filename);
932
933 if (mode && *mode != 'w') {
934 qdict_put_bool(file_layer, "read-only", true);
935 }
936
937 if (direct_io_safe) {
938 unsigned long value;
939
940 if (!qemu_strtoul(direct_io_safe, NULL, 2, &value) && !!value) {
941 QDict *cache_qdict = qdict_new();
942
943 qdict_put_bool(cache_qdict, "direct", true);
944 qdict_put(file_layer, "cache", cache_qdict);
945
946 qdict_put_str(file_layer, "aio", "native");
947 }
948 }
949
950 if (discard_enable) {
951 unsigned long value;
952
953 if (!qemu_strtoul(discard_enable, NULL, 2, &value) && !!value) {
954 qdict_put_str(file_layer, "discard", "unmap");
955 qdict_put_str(driver_layer, "discard", "unmap");
956 }
957 }
958
959 /*
960 * It is necessary to turn file locking off as an emulated device
961 * may have already opened the same image file.
962 */
963 qdict_put_str(file_layer, "locking", "off");
964
965 qdict_put_str(driver_layer, "driver", driver);
966
967 qdict_put(driver_layer, "file", file_layer);
968
969 g_assert(!drive->node_name);
970 drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
971 errp);
972
973 qobject_unref(driver_layer);
974
975 done:
976 g_free(filename);
977 g_free(driver);
978 if (*errp) {
979 xen_block_drive_destroy(drive, NULL);
980 return NULL;
981 }
982
983 return drive;
984 }
985
xen_block_drive_get_node_name(XenBlockDrive * drive)986 static const char *xen_block_drive_get_node_name(XenBlockDrive *drive)
987 {
988 return drive->node_name ? drive->node_name : "";
989 }
990
xen_block_iothread_destroy(XenBlockIOThread * iothread,Error ** errp)991 static void xen_block_iothread_destroy(XenBlockIOThread *iothread,
992 Error **errp)
993 {
994 qmp_object_del(iothread->id, errp);
995
996 g_free(iothread->id);
997 g_free(iothread);
998 }
999
xen_block_iothread_create(const char * id,Error ** errp)1000 static XenBlockIOThread *xen_block_iothread_create(const char *id,
1001 Error **errp)
1002 {
1003 ERRP_GUARD();
1004 XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
1005 ObjectOptions *opts;
1006
1007 iothread->id = g_strdup(id);
1008
1009 opts = g_new(ObjectOptions, 1);
1010 *opts = (ObjectOptions) {
1011 .qom_type = OBJECT_TYPE_IOTHREAD,
1012 .id = g_strdup(id),
1013 };
1014 qmp_object_add(opts, errp);
1015 qapi_free_ObjectOptions(opts);
1016
1017 if (*errp) {
1018 g_free(iothread->id);
1019 g_free(iothread);
1020 return NULL;
1021 }
1022
1023 return iothread;
1024 }
1025
xen_block_device_create(XenBackendInstance * backend,QDict * opts,Error ** errp)1026 static void xen_block_device_create(XenBackendInstance *backend,
1027 QDict *opts, Error **errp)
1028 {
1029 ERRP_GUARD();
1030 XenBus *xenbus = xen_backend_get_bus(backend);
1031 const char *name = xen_backend_get_name(backend);
1032 unsigned long number;
1033 const char *vdev, *device_type;
1034 XenBlockDrive *drive = NULL;
1035 XenBlockIOThread *iothread = NULL;
1036 XenDevice *xendev = NULL;
1037 const char *type;
1038 XenBlockDevice *blockdev;
1039
1040 if (qemu_strtoul(name, NULL, 10, &number)) {
1041 error_setg(errp, "failed to parse name '%s'", name);
1042 goto fail;
1043 }
1044
1045 trace_xen_block_device_create(number);
1046
1047 vdev = qdict_get_try_str(opts, "dev");
1048 if (!vdev) {
1049 error_setg(errp, "no dev parameter");
1050 goto fail;
1051 }
1052
1053 device_type = qdict_get_try_str(opts, "device-type");
1054 if (!device_type) {
1055 error_setg(errp, "no device-type parameter");
1056 goto fail;
1057 }
1058
1059 if (!strcmp(device_type, "disk")) {
1060 type = TYPE_XEN_DISK_DEVICE;
1061 } else if (!strcmp(device_type, "cdrom")) {
1062 type = TYPE_XEN_CDROM_DEVICE;
1063 } else {
1064 error_setg(errp, "invalid device-type parameter '%s'", device_type);
1065 goto fail;
1066 }
1067
1068 drive = xen_block_drive_create(vdev, device_type, opts, errp);
1069 if (!drive) {
1070 error_prepend(errp, "failed to create drive: ");
1071 goto fail;
1072 }
1073
1074 iothread = xen_block_iothread_create(vdev, errp);
1075 if (*errp) {
1076 error_prepend(errp, "failed to create iothread: ");
1077 goto fail;
1078 }
1079
1080 xendev = XEN_DEVICE(qdev_new(type));
1081 blockdev = XEN_BLOCK_DEVICE(xendev);
1082
1083 if (!object_property_set_str(OBJECT(xendev), "vdev", vdev,
1084 errp)) {
1085 error_prepend(errp, "failed to set 'vdev': ");
1086 goto fail;
1087 }
1088
1089 if (!object_property_set_str(OBJECT(xendev), "drive",
1090 xen_block_drive_get_node_name(drive),
1091 errp)) {
1092 error_prepend(errp, "failed to set 'drive': ");
1093 goto fail;
1094 }
1095
1096 if (!object_property_set_str(OBJECT(xendev), "iothread", iothread->id,
1097 errp)) {
1098 error_prepend(errp, "failed to set 'iothread': ");
1099 goto fail;
1100 }
1101
1102 blockdev->iothread = iothread;
1103 blockdev->drive = drive;
1104
1105 if (!qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
1106 error_prepend(errp, "realization of device %s failed: ", type);
1107 goto fail;
1108 }
1109
1110 xen_backend_set_device(backend, xendev);
1111 return;
1112
1113 fail:
1114 if (xendev) {
1115 object_unparent(OBJECT(xendev));
1116 }
1117
1118 if (iothread) {
1119 xen_block_iothread_destroy(iothread, NULL);
1120 }
1121
1122 if (drive) {
1123 xen_block_drive_destroy(drive, NULL);
1124 }
1125 }
1126
xen_block_device_destroy(XenBackendInstance * backend,Error ** errp)1127 static void xen_block_device_destroy(XenBackendInstance *backend,
1128 Error **errp)
1129 {
1130 ERRP_GUARD();
1131 XenDevice *xendev = xen_backend_get_device(backend);
1132 XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
1133 XenBlockVdev *vdev = &blockdev->props.vdev;
1134 XenBlockDrive *drive = blockdev->drive;
1135 XenBlockIOThread *iothread = blockdev->iothread;
1136
1137 trace_xen_block_device_destroy(vdev->number);
1138
1139 object_unparent(OBJECT(xendev));
1140
1141 /*
1142 * Drain all pending RCU callbacks as object_unparent() frees `xendev'
1143 * in a RCU callback.
1144 * And due to the property "drive" still existing in `xendev', we
1145 * can't destroy the XenBlockDrive associated with `xendev' with
1146 * xen_block_drive_destroy() below.
1147 */
1148 drain_call_rcu();
1149
1150 if (iothread) {
1151 xen_block_iothread_destroy(iothread, errp);
1152 if (*errp) {
1153 error_prepend(errp, "failed to destroy iothread: ");
1154 return;
1155 }
1156 }
1157
1158 if (drive) {
1159 xen_block_drive_destroy(drive, errp);
1160 if (*errp) {
1161 error_prepend(errp, "failed to destroy drive: ");
1162 return;
1163 }
1164 }
1165 }
1166
1167 static const XenBackendInfo xen_block_backend_info = {
1168 .type = "qdisk",
1169 .create = xen_block_device_create,
1170 .destroy = xen_block_device_destroy,
1171 };
1172
xen_block_register_backend(void)1173 static void xen_block_register_backend(void)
1174 {
1175 xen_backend_register(&xen_block_backend_info);
1176 }
1177
1178 xen_backend_init(xen_block_register_backend);
1179