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