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