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