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