qemu_block.c 16.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
/*
 * qemu_block.c: helper functions for QEMU block subsystem
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "qemu_block.h"
#include "qemu_domain.h"

#include "viralloc.h"
#include "virstring.h"

#define VIR_FROM_THIS VIR_FROM_QEMU


static void
qemuBlockNodeNameBackingChainDataFree(qemuBlockNodeNameBackingChainDataPtr data)
{
    size_t i;

    if (!data)
        return;

    for (i = 0; i < data->nelems; i++)
        virJSONValueFree(data->elems[i]);

    VIR_FREE(data->nodeformat);
    VIR_FREE(data->nodestorage);
    VIR_FREE(data->nodebacking);

    VIR_FREE(data->qemufilename);
    VIR_FREE(data->backingstore);

    VIR_FREE(data);
}


static void
qemuBlockNodeNameBackingChainDataHashEntryFree(void *opaque,
                                               const void *name ATTRIBUTE_UNUSED)
{
    qemuBlockNodeNameBackingChainDataFree(opaque);
}


struct qemuBlockNodeNameGetBackingChainData {
    virHashTablePtr table;
    qemuBlockNodeNameBackingChainDataPtr *entries;
    size_t nentries;
};


static int
qemuBlockNodeNameDetectProcessByFilename(size_t pos ATTRIBUTE_UNUSED,
                                         virJSONValuePtr item,
                                         void *opaque)
{
    struct qemuBlockNodeNameGetBackingChainData *data = opaque;
    qemuBlockNodeNameBackingChainDataPtr entry;
    const char *file;

    if (!(file = virJSONValueObjectGetString(item, "file")))
        return 1;

    if (!(entry = virHashLookup(data->table, file))) {
        if (VIR_ALLOC(entry) < 0)
            return -1;

        if (VIR_APPEND_ELEMENT_COPY(data->entries, data->nentries, entry) < 0) {
            VIR_FREE(entry);
            return -1;
        }

        if (VIR_STRDUP(entry->qemufilename, file) < 0)
            return -1;

        if (virHashAddEntry(data->table, file, entry) < 0)
            return -1;
    }

    if (VIR_APPEND_ELEMENT(entry->elems, entry->nelems, item) < 0)
        return -1;

    return 0;
}


static const char *qemuBlockDriversFormat[] = {
    "qcow2", "raw", "qcow", "luks", "qed", "bochs", "cloop", "dmg", "parallels",
    "vdi", "vhdx", "vmdk", "vpc", "vvfat", NULL};
static const char *qemuBlockDriversStorage[] = {
    "file", "iscsi", "nbd", "host_cdrom", "host_device", "ftp", "ftps",
    "gluster", "http", "https", "nfs", "rbd", "sheepdog", "ssh", "tftp", NULL};


static bool
qemuBlockDriverMatch(const char *drvname,
                     const char **drivers)
{
    while (*drivers) {
        if (STREQ(drvname, *drivers))
            return true;

        drivers++;
    }

    return false;
}


static int
qemuBlockNodeNameDetectProcessExtract(qemuBlockNodeNameBackingChainDataPtr data)
{
    const char *drv;
    const char *nodename;
    const char *backingstore;
    size_t i;

    /* Since the only way to construct the backing chain is to look up the files
     * by file name, if two disks share a backing image we can't know which node
     * belongs to which backing chain. Refuse to detect such chains. */
    if (data->nelems > 2)
        return 0;

    for (i = 0; i < data->nelems; i++) {
        drv = virJSONValueObjectGetString(data->elems[i], "drv");
        nodename = virJSONValueObjectGetString(data->elems[i], "node-name");
        backingstore = virJSONValueObjectGetString(data->elems[i], "backing_file");

        if (!drv || !nodename)
            continue;

        if (qemuBlockDriverMatch(drv, qemuBlockDriversFormat)) {
            if (data->nodeformat)
                continue;

            if (VIR_STRDUP(data->nodeformat, nodename) < 0)
                return -1;

            /* extract the backing store file name for the protocol layer */
            if (VIR_STRDUP(data->backingstore, backingstore) < 0)
                return -1;
        } else if (qemuBlockDriverMatch(drv, qemuBlockDriversStorage)) {
            if (data->nodestorage)
                continue;

            if (VIR_STRDUP(data->nodestorage, nodename) < 0)
                return -1;
        }
    }

    return 0;
}


static int
qemuBlockNodeNameDetectProcessLinkBacking(qemuBlockNodeNameBackingChainDataPtr data,
                                          virHashTablePtr table)
{
    qemuBlockNodeNameBackingChainDataPtr backing;

    if (!data->backingstore)
        return 0;

    if (!(backing = virHashLookup(table, data->backingstore)))
        return 0;

    if (VIR_STRDUP(data->nodebacking, backing->nodeformat) < 0)
        return -1;

    return 0;
}


static void
qemuBlockNodeNameGetBackingChainDataClearLookup(qemuBlockNodeNameBackingChainDataPtr data)
{
    size_t i;

    for (i = 0; i < data->nelems; i++)
        virJSONValueFree(data->elems[i]);

    VIR_FREE(data->elems);
    data->nelems = 0;
}


/**
 * qemuBlockNodeNameGetBackingChain:
 * @json: JSON array of data returned from 'query-named-block-nodes'
 *
 * Tries to reconstruct the backing chain from @json to allow detection of
 * node names that were auto-assigned by qemu. This is a best-effort operation
 * and may not be successful. The returned hash table contains the entries as
 * qemuBlockNodeNameBackingChainDataPtr accessible by the node name. The fields
 * then can be used to recover the full backing chain.
 *
 * Returns a hash table on success and NULL on failure.
 */
virHashTablePtr
qemuBlockNodeNameGetBackingChain(virJSONValuePtr json)
{
    struct qemuBlockNodeNameGetBackingChainData data;
    virHashTablePtr nodetable = NULL;
    virHashTablePtr ret = NULL;
    size_t i;

    memset(&data, 0, sizeof(data));

    /* hash table keeps the entries accessible by the 'file' in qemu */
    if (!(data.table = virHashCreate(50, NULL)))
        goto cleanup;

    /* first group the named entries by the 'file' field */
    if (virJSONValueArrayForeachSteal(json,
                                      qemuBlockNodeNameDetectProcessByFilename,
                                      &data) < 0)
        goto cleanup;

    /* extract the node names for the format and storage layer */
    for (i = 0; i < data.nentries; i++) {
        if (qemuBlockNodeNameDetectProcessExtract(data.entries[i]) < 0)
            goto cleanup;
    }

    /* extract the node name for the backing file */
    for (i = 0; i < data.nentries; i++) {
        if (qemuBlockNodeNameDetectProcessLinkBacking(data.entries[i],
                                                      data.table) < 0)
            goto cleanup;
    }

    /* clear JSON data necessary only for the lookup procedure */
    for (i = 0; i < data.nentries; i++)
        qemuBlockNodeNameGetBackingChainDataClearLookup(data.entries[i]);

    /* create hash table hashed by the format node name */
    if (!(nodetable = virHashCreate(50,
                                    qemuBlockNodeNameBackingChainDataHashEntryFree)))
        goto cleanup;

    /* fill the entries */
    for (i = 0; i < data.nentries; i++) {
        if (!data.entries[i]->nodeformat)
            continue;

        if (virHashAddEntry(nodetable, data.entries[i]->nodeformat,
                            data.entries[i]) < 0)
            goto cleanup;

        /* hash table steals the entry and then frees it by itself */
        data.entries[i] = NULL;
    }

    VIR_STEAL_PTR(ret, nodetable);

 cleanup:
     virHashFree(data.table);
     virHashFree(nodetable);
     for (i = 0; i < data.nentries; i++)
         qemuBlockNodeNameBackingChainDataFree(data.entries[i]);

    VIR_FREE(data.entries);

     return ret;
}
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338


static void
qemuBlockDiskClearDetectedNodes(virDomainDiskDefPtr disk)
{
    virStorageSourcePtr next = disk->src;

    while (next) {
        VIR_FREE(next->nodeformat);
        VIR_FREE(next->nodebacking);

        next = next->backingStore;
    }
}


static int
qemuBlockDiskDetectNodes(virDomainDiskDefPtr disk,
                         const char *parentnode,
                         virHashTablePtr table)
{
    qemuBlockNodeNameBackingChainDataPtr entry = NULL;
    virStorageSourcePtr src = disk->src;

    /* don't attempt the detection if the top level already has node names */
    if (!parentnode || src->nodeformat || src->nodebacking)
        return 0;

    while (src && parentnode) {
        if (!(entry = virHashLookup(table, parentnode)))
            break;

        if (src->nodeformat || src->nodebacking) {
            if (STRNEQ_NULLABLE(src->nodeformat, entry->nodeformat) ||
                STRNEQ_NULLABLE(src->nodebacking, entry->nodestorage))
                goto error;

            break;
        } else {
            if (VIR_STRDUP(src->nodeformat, entry->nodeformat) < 0 ||
                VIR_STRDUP(src->nodebacking, entry->nodestorage) < 0)
                goto error;
        }

        parentnode = entry->nodebacking;
        src = src->backingStore;
    }

    return 0;

 error:
    qemuBlockDiskClearDetectedNodes(disk);
    return -1;
}


int
qemuBlockNodeNamesDetect(virQEMUDriverPtr driver,
339 340
                         virDomainObjPtr vm,
                         qemuDomainAsyncJob asyncJob)
341 342 343 344 345 346 347 348 349 350 351 352 353
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virHashTablePtr disktable = NULL;
    virHashTablePtr nodenametable = NULL;
    virJSONValuePtr data = NULL;
    virDomainDiskDefPtr disk;
    struct qemuDomainDiskInfo *info;
    size_t i;
    int ret = -1;

    if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_QUERY_NAMED_BLOCK_NODES))
        return 0;

354 355
    if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
        return -1;
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

    disktable = qemuMonitorGetBlockInfo(qemuDomainGetMonitor(vm));
    data = qemuMonitorQueryNamedBlockNodes(qemuDomainGetMonitor(vm));

    if (qemuDomainObjExitMonitor(driver, vm) < 0 || !data || !disktable)
        goto cleanup;

    if (!(nodenametable = qemuBlockNodeNameGetBackingChain(data)))
        goto cleanup;

    for (i = 0; i < vm->def->ndisks; i++) {
        disk = vm->def->disks[i];

        if (!(info = virHashLookup(disktable, disk->info.alias)))
            continue;

        if (qemuBlockDiskDetectNodes(disk, info->nodename, nodenametable) < 0)
            goto cleanup;
    }

    ret = 0;

 cleanup:
    virJSONValueFree(data);
    virHashFree(nodenametable);
    virHashFree(disktable);

    return ret;
}
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430


static int
qemuBlockFillNodeData(size_t pos ATTRIBUTE_UNUSED,
                      virJSONValuePtr item,
                      void *opaque)
{
    virHashTablePtr table = opaque;
    const char *name;

    if (!(name = virJSONValueObjectGetString(item, "node-name")))
        return 1;

    if (virHashAddEntry(table, name, item) < 0)
        return -1;

    return 0;
}


/**
 * qemuBlockGetNodeData:
 * @data: JSON object returned from query-named-block-nodes
 *
 * Returns a hash table organized by the node name of the JSON value objects of
 * data for given qemu block nodes.
 *
 * Returns a filled virHashTablePtr on success NULL on error.
 */
virHashTablePtr
qemuBlockGetNodeData(virJSONValuePtr data)
{
    virHashTablePtr ret = NULL;

    if (!(ret = virHashCreate(50, virJSONValueHashFree)))
        return NULL;

    if (virJSONValueArrayForeachSteal(data, qemuBlockFillNodeData, ret) < 0)
        goto error;

    return ret;

 error:
    virHashFree(ret);
    return NULL;
}
431 432


433 434 435 436 437 438 439 440
/**
 * qemuBlockStorageSourceBuildHostsJSONSocketAddress:
 * @src: disk storage source
 * @legacy: use 'tcp' instead of 'inet' for compatibility reasons
 *
 * Formats src->hosts into a json object conforming to the 'SocketAddress' type
 * in qemu.
 */
441
static virJSONValuePtr
442 443
qemuBlockStorageSourceBuildHostsJSONSocketAddress(virStorageSourcePtr src,
                                                  bool legacy)
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
{
    virJSONValuePtr servers = NULL;
    virJSONValuePtr server = NULL;
    virJSONValuePtr ret = NULL;
    virStorageNetHostDefPtr host;
    const char *transport;
    size_t i;

    if (!(servers = virJSONValueNewArray()))
        goto cleanup;

    for (i = 0; i < src->nhosts; i++) {
        host = src->hosts + i;

        switch ((virStorageNetHostTransport) host->transport) {
        case VIR_STORAGE_NET_HOST_TRANS_TCP:
460 461 462 463 464 465 466 467 468 469
            if (legacy)
                transport = "tcp";
            else
                transport = "inet";

            if (virJSONValueObjectCreate(&server,
                                         "s:type", transport,
                                         "s:host", host->name,
                                         "s:port", host->port,
                                         NULL) < 0)
470 471 472 473
                goto cleanup;
            break;

        case VIR_STORAGE_NET_HOST_TRANS_UNIX:
474 475 476 477
            if (virJSONValueObjectCreate(&server,
                                         "s:type", "unix",
                                         "s:socket", host->socket,
                                         NULL) < 0)
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
                goto cleanup;
            break;

        case VIR_STORAGE_NET_HOST_TRANS_RDMA:
        case VIR_STORAGE_NET_HOST_TRANS_LAST:
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("transport protocol '%s' is not yet supported"),
                           transport);
            goto cleanup;
        }

        if (virJSONValueArrayAppend(servers, server) < 0)
            goto cleanup;

        server = NULL;
    }

495
    VIR_STEAL_PTR(ret, servers);
496 497 498 499 500 501 502 503 504 505

 cleanup:
    virJSONValueFree(servers);
    virJSONValueFree(server);

    return ret;
}


static virJSONValuePtr
506
qemuBlockStorageSourceGetGlusterProps(virStorageSourcePtr src)
507 508 509 510
{
    virJSONValuePtr servers = NULL;
    virJSONValuePtr ret = NULL;

511
    if (!(servers = qemuBlockStorageSourceBuildHostsJSONSocketAddress(src, true)))
512 513 514 515 516 517 518 519 520
        return NULL;

     /* { driver:"gluster",
      *   volume:"testvol",
      *   path:"/a.img",
      *   server :[{type:"tcp", host:"1.2.3.4", port:24007},
      *            {type:"unix", socket:"/tmp/glusterd.socket"}, ...]}
      */
    if (virJSONValueObjectCreate(&ret,
521
                                 "s:driver", "gluster",
522 523 524 525 526 527 528 529 530
                                 "s:volume", src->volume,
                                 "s:path", src->path,
                                 "a:server", servers, NULL) < 0)
          virJSONValueFree(servers);

    return ret;
}


531 532 533 534 535 536 537 538 539
/**
 * qemuBlockStorageSourceGetBackendProps:
 * @src: disk source
 *
 * Creates a JSON object describing the underlying storage or protocol of a
 * storage source. Returns NULL on error and reports an appropriate error message.
 */
virJSONValuePtr
qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src)
540 541 542
{
    int actualType = virStorageSourceGetActualType(src);
    virJSONValuePtr fileprops = NULL;
543
    virJSONValuePtr ret = NULL;
544 545 546 547 548 549 550 551 552 553 554

    switch ((virStorageType) actualType) {
    case VIR_STORAGE_TYPE_BLOCK:
    case VIR_STORAGE_TYPE_FILE:
    case VIR_STORAGE_TYPE_DIR:
    case VIR_STORAGE_TYPE_VOLUME:
    case VIR_STORAGE_TYPE_NONE:
    case VIR_STORAGE_TYPE_LAST:
        break;

    case VIR_STORAGE_TYPE_NETWORK:
555 556
        switch ((virStorageNetProtocol) src->protocol) {
        case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
557
            if (!(fileprops = qemuBlockStorageSourceGetGlusterProps(src)))
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
                goto cleanup;
            break;

        case VIR_STORAGE_NET_PROTOCOL_NBD:
        case VIR_STORAGE_NET_PROTOCOL_RBD:
        case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
        case VIR_STORAGE_NET_PROTOCOL_ISCSI:
        case VIR_STORAGE_NET_PROTOCOL_HTTP:
        case VIR_STORAGE_NET_PROTOCOL_HTTPS:
        case VIR_STORAGE_NET_PROTOCOL_FTP:
        case VIR_STORAGE_NET_PROTOCOL_FTPS:
        case VIR_STORAGE_NET_PROTOCOL_TFTP:
        case VIR_STORAGE_NET_PROTOCOL_SSH:
        case VIR_STORAGE_NET_PROTOCOL_NONE:
        case VIR_STORAGE_NET_PROTOCOL_LAST:
            break;
574 575 576 577
        }
        break;
    }

578 579
    if (virJSONValueObjectCreate(&ret, "a:file", fileprops, NULL) < 0)
        goto cleanup;
580

581 582 583 584 585
    fileprops = NULL;

 cleanup:
    virJSONValueFree(fileprops);
    return ret;
586
}