storage_conf.c 48.8 KB
Newer Older
1 2 3
/*
 * storage_conf.c: config handling for storage driver
 *
4
 * Copyright (C) 2006-2016 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006-2008 Daniel P. Berrange
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>

35
#include "virerror.h"
36
#include "datatypes.h"
37
#include "node_device_conf.h"
38
#include "storage_adapter_conf.h"
39
#include "storage_conf.h"
40
#include "virstoragefile.h"
41

42
#include "virxml.h"
43
#include "viruuid.h"
44
#include "virbuffer.h"
45
#include "viralloc.h"
E
Eric Blake 已提交
46
#include "virfile.h"
47
#include "virscsihost.h"
48
#include "virstring.h"
49
#include "virlog.h"
50
#include "virvhba.h"
51

52 53
#define VIR_FROM_THIS VIR_FROM_STORAGE

54 55
VIR_LOG_INIT("conf.storage_conf");

56 57
VIR_ENUM_IMPL(virStorageVol,
              VIR_STORAGE_VOL_LAST,
O
Olga Krishtal 已提交
58 59
              "file", "block", "dir", "network",
              "netdir", "ploop")
60

61 62 63 64
VIR_ENUM_IMPL(virStoragePool,
              VIR_STORAGE_POOL_LAST,
              "dir", "fs", "netfs",
              "logical", "disk", "iscsi",
R
Roman Bogorodskiy 已提交
65
              "scsi", "mpath", "rbd",
66 67
              "sheepdog", "gluster", "zfs",
              "vstorage")
68 69 70 71 72

VIR_ENUM_IMPL(virStoragePoolFormatFileSystem,
              VIR_STORAGE_POOL_FS_LAST,
              "auto", "ext2", "ext3",
              "ext4", "ufs", "iso9660", "udf",
J
Jim Fehlig 已提交
73
              "gfs", "gfs2", "vfat", "hfs+", "xfs", "ocfs2")
74 75 76

VIR_ENUM_IMPL(virStoragePoolFormatFileSystemNet,
              VIR_STORAGE_POOL_NETFS_LAST,
77
              "auto", "nfs", "glusterfs", "cifs")
78 79 80 81

VIR_ENUM_IMPL(virStoragePoolFormatDisk,
              VIR_STORAGE_POOL_DISK_LAST,
              "unknown", "dos", "dvh", "gpt",
82
              "mac", "bsd", "pc98", "sun", "lvm2")
83 84 85

VIR_ENUM_IMPL(virStoragePoolFormatLogical,
              VIR_STORAGE_POOL_LOGICAL_LAST,
86
              "unknown", "lvm2")
87 88 89 90 91 92 93


VIR_ENUM_IMPL(virStorageVolFormatDisk,
              VIR_STORAGE_VOL_DISK_LAST,
              "none", "linux", "fat16",
              "fat32", "linux-swap",
              "linux-lvm", "linux-raid",
94
              "extended")
95

96
VIR_ENUM_IMPL(virStoragePartedFs,
97
              VIR_STORAGE_PARTED_FS_TYPE_LAST,
98
              "ext2", "ext2", "fat16",
99 100 101
              "fat32", "linux-swap",
              "ext2", "ext2",
              "extended")
102 103 104 105 106 107 108 109 110 111

typedef const char *(*virStorageVolFormatToString)(int format);
typedef int (*virStorageVolFormatFromString)(const char *format);

typedef const char *(*virStoragePoolFormatToString)(int format);
typedef int (*virStoragePoolFormatFromString)(const char *format);

typedef struct _virStorageVolOptions virStorageVolOptions;
typedef virStorageVolOptions *virStorageVolOptionsPtr;
struct _virStorageVolOptions {
112
    int defaultFormat;
113 114 115 116 117 118
    virStorageVolFormatToString formatToString;
    virStorageVolFormatFromString formatFromString;
};

/* Flags to indicate mandatory components in the pool source */
enum {
O
Osier Yang 已提交
119 120 121 122 123 124 125
    VIR_STORAGE_POOL_SOURCE_HOST            = (1 << 0),
    VIR_STORAGE_POOL_SOURCE_DEVICE          = (1 << 1),
    VIR_STORAGE_POOL_SOURCE_DIR             = (1 << 2),
    VIR_STORAGE_POOL_SOURCE_ADAPTER         = (1 << 3),
    VIR_STORAGE_POOL_SOURCE_NAME            = (1 << 4),
    VIR_STORAGE_POOL_SOURCE_INITIATOR_IQN   = (1 << 5),
    VIR_STORAGE_POOL_SOURCE_NETWORK         = (1 << 6),
126 127 128 129 130
};

typedef struct _virStoragePoolOptions virStoragePoolOptions;
typedef virStoragePoolOptions *virStoragePoolOptionsPtr;
struct _virStoragePoolOptions {
E
Eric Blake 已提交
131
    unsigned int flags;
132 133 134 135 136 137 138 139 140 141 142 143 144
    int defaultFormat;
    virStoragePoolFormatToString formatToString;
    virStoragePoolFormatFromString formatFromString;
};

typedef struct _virStoragePoolTypeInfo virStoragePoolTypeInfo;
typedef virStoragePoolTypeInfo *virStoragePoolTypeInfoPtr;
struct _virStoragePoolTypeInfo {
    int poolType;
    virStoragePoolOptions poolOptions;
    virStorageVolOptions volOptions;
};

145

E
Eric Blake 已提交
146 147 148 149 150 151 152 153 154
static int
virStorageVolumeFormatFromString(const char *format)
{
    int ret = virStorageFileFormatTypeFromString(format);
    if (ret == VIR_STORAGE_FILE_NONE)
        return -1;
    return ret;
}

155

156
static virStoragePoolTypeInfo poolTypeInfo[] = {
157 158 159 160 161 162 163 164
    {.poolType = VIR_STORAGE_POOL_LOGICAL,
     .poolOptions = {
         .flags = (VIR_STORAGE_POOL_SOURCE_NAME |
                   VIR_STORAGE_POOL_SOURCE_DEVICE),
         .defaultFormat = VIR_STORAGE_POOL_LOGICAL_LVM2,
         .formatFromString = virStoragePoolFormatLogicalTypeFromString,
         .formatToString = virStoragePoolFormatLogicalTypeToString,
     },
165
    },
166 167 168 169 170 171
    {.poolType = VIR_STORAGE_POOL_DIR,
     .volOptions = {
         .defaultFormat = VIR_STORAGE_FILE_RAW,
         .formatFromString = virStorageVolumeFormatFromString,
         .formatToString = virStorageFileFormatTypeToString,
     },
172
    },
173 174 175 176 177 178 179
    {.poolType = VIR_STORAGE_POOL_FS,
     .poolOptions = {
         .flags = (VIR_STORAGE_POOL_SOURCE_DEVICE),
         .defaultFormat = VIR_STORAGE_POOL_FS_AUTO,
         .formatFromString = virStoragePoolFormatFileSystemTypeFromString,
         .formatToString = virStoragePoolFormatFileSystemTypeToString,
      },
180
      .volOptions = {
181 182 183 184
         .defaultFormat = VIR_STORAGE_FILE_RAW,
         .formatFromString = virStorageVolumeFormatFromString,
         .formatToString = virStorageFileFormatTypeToString,
      },
185
    },
186 187 188 189 190 191 192 193
    {.poolType = VIR_STORAGE_POOL_NETFS,
     .poolOptions = {
         .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
                   VIR_STORAGE_POOL_SOURCE_DIR),
         .defaultFormat = VIR_STORAGE_POOL_NETFS_AUTO,
         .formatFromString = virStoragePoolFormatFileSystemNetTypeFromString,
         .formatToString = virStoragePoolFormatFileSystemNetTypeToString,
      },
194
      .volOptions = {
195 196 197 198
         .defaultFormat = VIR_STORAGE_FILE_RAW,
         .formatFromString = virStorageVolumeFormatFromString,
         .formatToString = virStorageFileFormatTypeToString,
      },
199
    },
200 201 202 203 204 205
    {.poolType = VIR_STORAGE_POOL_ISCSI,
     .poolOptions = {
         .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
                   VIR_STORAGE_POOL_SOURCE_DEVICE |
                   VIR_STORAGE_POOL_SOURCE_INITIATOR_IQN),
      },
206
      .volOptions = {
207 208 209 210 211 212 213 214 215 216
         .formatToString = virStoragePoolFormatDiskTypeToString,
      }
    },
    {.poolType = VIR_STORAGE_POOL_SCSI,
     .poolOptions = {
         .flags = (VIR_STORAGE_POOL_SOURCE_ADAPTER),
     },
     .volOptions = {
         .formatToString = virStoragePoolFormatDiskTypeToString,
     }
217
    },
218 219 220 221 222 223
    {.poolType = VIR_STORAGE_POOL_RBD,
     .poolOptions = {
         .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
                   VIR_STORAGE_POOL_SOURCE_NETWORK |
                   VIR_STORAGE_POOL_SOURCE_NAME),
      },
224
      .volOptions = {
225
          .defaultFormat = VIR_STORAGE_FILE_RAW,
226 227
          .formatFromString = virStorageVolumeFormatFromString,
          .formatToString = virStorageFileFormatTypeToString,
228
      }
229
    },
230 231 232 233 234 235 236 237 238 239
    {.poolType = VIR_STORAGE_POOL_SHEEPDOG,
     .poolOptions = {
         .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
                   VIR_STORAGE_POOL_SOURCE_NETWORK |
                   VIR_STORAGE_POOL_SOURCE_NAME),
     },
     .volOptions = {
         .defaultFormat = VIR_STORAGE_FILE_RAW,
         .formatToString = virStoragePoolFormatDiskTypeToString,
     }
240
    },
241 242 243 244 245 246 247 248 249 250 251 252 253
    {.poolType = VIR_STORAGE_POOL_GLUSTER,
     .poolOptions = {
         .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
                   VIR_STORAGE_POOL_SOURCE_NETWORK |
                   VIR_STORAGE_POOL_SOURCE_NAME |
                   VIR_STORAGE_POOL_SOURCE_DIR),
     },
     .volOptions = {
         .defaultFormat = VIR_STORAGE_FILE_RAW,
         .formatToString = virStorageFileFormatTypeToString,
         .formatFromString = virStorageVolumeFormatFromString,
     }
    },
254 255 256 257
    {.poolType = VIR_STORAGE_POOL_MPATH,
     .volOptions = {
         .formatToString = virStoragePoolFormatDiskTypeToString,
     }
258
    },
259 260 261 262 263 264 265 266 267 268 269 270
    {.poolType = VIR_STORAGE_POOL_DISK,
     .poolOptions = {
         .flags = (VIR_STORAGE_POOL_SOURCE_DEVICE),
         .defaultFormat = VIR_STORAGE_POOL_DISK_UNKNOWN,
         .formatFromString = virStoragePoolFormatDiskTypeFromString,
         .formatToString = virStoragePoolFormatDiskTypeToString,
     },
     .volOptions = {
         .defaultFormat = VIR_STORAGE_VOL_DISK_NONE,
         .formatFromString = virStorageVolFormatDiskTypeFromString,
         .formatToString = virStorageVolFormatDiskTypeToString,
     },
R
Roman Bogorodskiy 已提交
271 272 273
    },
    {.poolType = VIR_STORAGE_POOL_ZFS,
     .poolOptions = {
274 275
         .flags = (VIR_STORAGE_POOL_SOURCE_NAME |
                   VIR_STORAGE_POOL_SOURCE_DEVICE),
R
Roman Bogorodskiy 已提交
276 277 278
         .defaultFormat = VIR_STORAGE_FILE_RAW,
     },
    },
279 280 281 282 283 284 285 286 287 288
    {.poolType = VIR_STORAGE_POOL_VSTORAGE,
     .poolOptions = {
        .flags = VIR_STORAGE_POOL_SOURCE_NAME,
     },
     .volOptions = {
        .defaultFormat = VIR_STORAGE_FILE_RAW,
        .formatFromString = virStorageVolumeFormatFromString,
        .formatToString = virStorageFileFormatTypeToString,
     },
    },
289 290 291 292
};


static virStoragePoolTypeInfoPtr
293 294
virStoragePoolTypeInfoLookup(int type)
{
295
    size_t i;
296
    for (i = 0; i < ARRAY_CARDINALITY(poolTypeInfo); i++)
297 298 299
        if (poolTypeInfo[i].poolType == type)
            return &poolTypeInfo[i];

300 301
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("missing backend for pool type %d"), type);
302 303 304
    return NULL;
}

305

306
static virStoragePoolOptionsPtr
307 308
virStoragePoolOptionsForPoolType(int type)
{
309 310 311 312 313 314
    virStoragePoolTypeInfoPtr backend = virStoragePoolTypeInfoLookup(type);
    if (backend == NULL)
        return NULL;
    return &backend->poolOptions;
}

315

316
static virStorageVolOptionsPtr
317 318
virStorageVolOptionsForPoolType(int type)
{
319 320 321 322 323 324 325
    virStoragePoolTypeInfoPtr backend = virStoragePoolTypeInfoLookup(type);
    if (backend == NULL)
        return NULL;
    return &backend->volOptions;
}


326
void
327 328
virStorageVolDefFree(virStorageVolDefPtr def)
{
329
    size_t i;
330 331 332 333

    if (!def)
        return;

334 335
    VIR_FREE(def->name);
    VIR_FREE(def->key);
336

337
    for (i = 0; i < def->source.nextent; i++)
338 339
        VIR_FREE(def->source.extents[i].path);
    VIR_FREE(def->source.extents);
340

341
    virStorageSourceClear(&def->target);
342
    VIR_FREE(def);
343 344
}

345

346 347 348 349 350 351 352
void
virStoragePoolSourceDeviceClear(virStoragePoolSourceDevicePtr dev)
{
    VIR_FREE(dev->freeExtents);
    VIR_FREE(dev->path);
}

353

354
void
355 356
virStoragePoolSourceClear(virStoragePoolSourcePtr source)
{
357
    size_t i;
358

359
    if (!source)
360 361
        return;

362
    for (i = 0; i < source->nhost; i++)
363 364 365
        VIR_FREE(source->hosts[i].name);
    VIR_FREE(source->hosts);

366 367
    for (i = 0; i < source->ndevice; i++)
        virStoragePoolSourceDeviceClear(&source->devices[i]);
368 369 370
    VIR_FREE(source->devices);
    VIR_FREE(source->dir);
    VIR_FREE(source->name);
371
    virStorageAdapterClear(&source->adapter);
D
David Allan 已提交
372
    VIR_FREE(source->initiator.iqn);
373
    virStorageAuthDefFree(source->auth);
374 375
    VIR_FREE(source->vendor);
    VIR_FREE(source->product);
376 377
}

378

379 380 381 382 383 384 385
void
virStoragePoolSourceFree(virStoragePoolSourcePtr source)
{
    virStoragePoolSourceClear(source);
    VIR_FREE(source);
}

386

387
void
388 389
virStoragePoolDefFree(virStoragePoolDefPtr def)
{
390 391 392 393 394
    if (!def)
        return;

    VIR_FREE(def->name);

395
    virStoragePoolSourceClear(&def->source);
396

397 398 399
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
    VIR_FREE(def);
400 401 402
}


403
static int
404
virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
405 406
                             virStoragePoolSourcePtr source,
                             int pool_type,
407 408
                             xmlNodePtr node)
{
409
    int ret = -1;
410
    xmlNodePtr relnode, authnode, *nodeset = NULL;
411
    xmlNodePtr adapternode;
412 413
    int nsource;
    size_t i;
414
    virStoragePoolOptionsPtr options;
415
    virStorageAuthDefPtr authdef = NULL;
416
    char *name = NULL;
417
    char *port = NULL;
418
    int n;
419 420 421 422

    relnode = ctxt->node;
    ctxt->node = node;

423
    if ((options = virStoragePoolOptionsForPoolType(pool_type)) == NULL)
424 425
        goto cleanup;

426
    source->name = virXPathString("string(./name)", ctxt);
427
    if (pool_type == VIR_STORAGE_POOL_RBD && source->name == NULL) {
428
        virReportError(VIR_ERR_XML_ERROR, "%s",
429
                       _("element 'name' is mandatory for RBD pool"));
430 431
        goto cleanup;
    }
432 433

    if (options->formatFromString) {
434
        char *format = virXPathString("string(./format/@type)", ctxt);
435 436 437 438 439 440
        if (format == NULL)
            source->format = options->defaultFormat;
        else
            source->format = options->formatFromString(format);

        if (source->format < 0) {
441
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
442
                           _("unknown pool format type %s"), format);
443 444 445 446 447 448
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

449 450
    if ((n = virXPathNodeSet("./host", ctxt, &nodeset)) < 0)
        goto cleanup;
451

452 453
    if (n) {
        if (VIR_ALLOC_N(source->hosts, n) < 0)
454
            goto cleanup;
455
        source->nhost = n;
456

457
        for (i = 0; i < source->nhost; i++) {
458 459
            name = virXMLPropString(nodeset[i], "name");
            if (name == NULL) {
460 461
                virReportError(VIR_ERR_XML_ERROR, "%s",
                               _("missing storage pool host name"));
462 463 464 465 466 467 468
                goto cleanup;
            }
            source->hosts[i].name = name;

            port = virXMLPropString(nodeset[i], "port");
            if (port) {
                if (virStrToLong_i(port, NULL, 10, &source->hosts[i].port) < 0) {
469 470 471
                    virReportError(VIR_ERR_XML_ERROR,
                                   _("Invalid port number: %s"),
                                   port);
472 473 474 475 476
                    goto cleanup;
                }
            }
        }
    }
477

478
    VIR_FREE(nodeset);
479
    source->initiator.iqn = virXPathString("string(./initiator/iqn/@name)", ctxt);
480

481
    nsource = virXPathNodeSet("./device", ctxt, &nodeset);
482 483 484
    if (nsource < 0)
        goto cleanup;

485
    for (i = 0; i < nsource; i++) {
486
        char *partsep;
487 488 489 490 491 492
        virStoragePoolSourceDevice dev = { .path = NULL };
        dev.path = virXMLPropString(nodeset[i], "path");

        if (dev.path == NULL) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("missing storage pool source device path"));
493 494 495
            goto cleanup;
        }

496 497 498 499 500 501 502 503 504 505 506 507 508 509
        partsep = virXMLPropString(nodeset[i], "part_separator");
        if (partsep) {
            dev.part_separator = virTristateBoolTypeFromString(partsep);
            if (dev.part_separator <= 0) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("invalid part_separator setting '%s'"),
                               partsep);
                virStoragePoolSourceDeviceClear(&dev);
                VIR_FREE(partsep);
                goto cleanup;
            }
            VIR_FREE(partsep);
        }

510 511 512
        if (VIR_APPEND_ELEMENT(source->devices, source->ndevice, dev) < 0) {
            virStoragePoolSourceDeviceClear(&dev);
            goto cleanup;
513
        }
514

515 516
    }

517
    source->dir = virXPathString("string(./dir/@path)", ctxt);
518 519 520 521
    /* In gluster, a missing dir defaults to "/" */
    if (!source->dir && pool_type == VIR_STORAGE_POOL_GLUSTER &&
        VIR_STRDUP(source->dir, "/") < 0)
        goto cleanup;
522

523
    if ((adapternode = virXPathNode("./adapter", ctxt))) {
524
        if (virStorageAdapterParseXML(&source->adapter, adapternode, ctxt) < 0)
525 526
            goto cleanup;
    }
527

528 529 530 531 532 533 534 535 536 537 538
    if ((authnode = virXPathNode("./auth", ctxt))) {
        if (!(authdef = virStorageAuthDefParse(node->doc, authnode)))
            goto cleanup;

        if (authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("storage pool missing auth type"));
            goto cleanup;
        }

        source->auth = authdef;
539
        authdef = NULL;
540
    }
541

542 543 544
    source->vendor = virXPathString("string(./vendor/@name)", ctxt);
    source->product = virXPathString("string(./product/@name)", ctxt);

545
    ret = 0;
546
 cleanup:
547 548
    ctxt->node = relnode;

549
    VIR_FREE(port);
550
    VIR_FREE(nodeset);
551
    virStorageAuthDefFree(authdef);
552 553
    return ret;
}
554

555

556
virStoragePoolSourcePtr
557
virStoragePoolDefParseSourceString(const char *srcSpec,
558 559 560 561 562 563 564
                                   int pool_type)
{
    xmlDocPtr doc = NULL;
    xmlNodePtr node = NULL;
    xmlXPathContextPtr xpath_ctxt = NULL;
    virStoragePoolSourcePtr def = NULL, ret = NULL;

565 566 567
    if (!(doc = virXMLParseStringCtxt(srcSpec,
                                      _("(storage_source_specification)"),
                                      &xpath_ctxt)))
568 569
        goto cleanup;

570
    if (VIR_ALLOC(def) < 0)
571 572
        goto cleanup;

573
    if (!(node = virXPathNode("/source", xpath_ctxt))) {
574 575
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("root element was not source"));
576 577 578
        goto cleanup;
    }

579
    if (virStoragePoolDefParseSource(xpath_ctxt, def, pool_type,
580 581 582 583 584
                                     node) < 0)
        goto cleanup;

    ret = def;
    def = NULL;
585
 cleanup:
586
    virStoragePoolSourceFree(def);
587 588 589 590 591
    xmlFreeDoc(doc);
    xmlXPathFreeContext(xpath_ctxt);

    return ret;
}
592

593

594
static int
595
virStorageDefParsePerms(xmlXPathContextPtr ctxt,
596
                        virStoragePermsPtr perms,
597
                        const char *permxpath)
598
{
599
    char *mode;
600
    long long val;
601 602 603
    int ret = -1;
    xmlNodePtr relnode;
    xmlNodePtr node;
604

605
    node = virXPathNode(permxpath, ctxt);
606 607
    if (node == NULL) {
        /* Set default values if there is not <permissions> element */
608
        perms->mode = (mode_t) -1;
P
Philipp Hahn 已提交
609 610
        perms->uid = (uid_t) -1;
        perms->gid = (gid_t) -1;
611 612 613 614 615 616 617
        perms->label = NULL;
        return 0;
    }

    relnode = ctxt->node;
    ctxt->node = node;

618
    if ((mode = virXPathString("string(./mode)", ctxt))) {
619 620 621
        int tmp;

        if (virStrToLong_i(mode, NULL, 8, &tmp) < 0 || (tmp & ~0777)) {
622
            VIR_FREE(mode);
623 624
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("malformed octal mode"));
625
            goto error;
626
        }
627
        perms->mode = tmp;
628
        VIR_FREE(mode);
629 630
    } else {
        perms->mode = (mode_t) -1;
631 632
    }

633
    if (virXPathNode("./owner", ctxt) == NULL) {
P
Philipp Hahn 已提交
634
        perms->uid = (uid_t) -1;
635
    } else {
636
        /* We previously could output -1, so continue to parse it */
637
        if (virXPathLongLong("number(./owner)", ctxt, &val) < 0 ||
638 639
            ((uid_t)val != val &&
             val != -1)) {
640 641
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("malformed owner element"));
642
            goto error;
643
        }
644 645

        perms->uid = val;
646 647
    }

648
    if (virXPathNode("./group", ctxt) == NULL) {
P
Philipp Hahn 已提交
649
        perms->gid = (gid_t) -1;
650
    } else {
651
        /* We previously could output -1, so continue to parse it */
652
        if (virXPathLongLong("number(./group)", ctxt, &val) < 0 ||
653 654
            ((gid_t) val != val &&
             val != -1)) {
655 656
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("malformed group element"));
657
            goto error;
658
        }
659
        perms->gid = val;
660 661 662
    }

    /* NB, we're ignoring missing labels here - they'll simply inherit */
663
    perms->label = virXPathString("string(./label)", ctxt);
664

665
    ret = 0;
666
 error:
667 668
    ctxt->node = relnode;
    return ret;
669 670
}

671

J
John Ferlan 已提交
672
virStoragePoolDefPtr
673 674
virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
{
675
    virStoragePoolOptionsPtr options;
676
    virStoragePoolDefPtr ret;
677
    xmlNodePtr source_node;
678
    char *type = NULL;
679
    char *uuid = NULL;
680
    char *target_path = NULL;
681

682
    if (VIR_ALLOC(ret) < 0)
683 684
        return NULL;

685
    type = virXPathString("string(./@type)", ctxt);
686 687 688 689 690 691
    if (type == NULL) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("storage pool missing type attribute"));
        goto error;
    }

692
    if ((ret->type = virStoragePoolTypeFromString(type)) < 0) {
693
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
694
                       _("unknown storage pool type %s"), type);
695
        goto error;
696 697
    }

698
    if ((options = virStoragePoolOptionsForPoolType(ret->type)) == NULL)
699
        goto error;
700

701
    source_node = virXPathNode("./source", ctxt);
702
    if (source_node) {
703
        if (virStoragePoolDefParseSource(ctxt, &ret->source, ret->type,
704
                                         source_node) < 0)
705
            goto error;
706 707 708
    } else {
        if (options->formatFromString)
            ret->source.format = options->defaultFormat;
709 710
    }

711
    ret->name = virXPathString("string(./name)", ctxt);
712
    if (ret->name == NULL &&
713
        options->flags & VIR_STORAGE_POOL_SOURCE_NAME)
714
        ret->name = ret->source.name;
715
    if (ret->name == NULL) {
716 717
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("missing pool source name element"));
718
        goto error;
719 720
    }

721 722 723 724 725 726
    if (strchr(ret->name, '/')) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("name %s cannot contain '/'"), ret->name);
        goto error;
    }

727
    uuid = virXPathString("string(./uuid)", ctxt);
728 729
    if (uuid == NULL) {
        if (virUUIDGenerate(ret->uuid) < 0) {
730 731
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("unable to generate uuid"));
732
            goto error;
733 734 735
        }
    } else {
        if (virUUIDParse(uuid, ret->uuid) < 0) {
736 737
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("malformed uuid element"));
738
            goto error;
739 740 741
        }
    }

742
    if (options->flags & VIR_STORAGE_POOL_SOURCE_HOST) {
743
        if (!ret->source.nhost) {
744
            virReportError(VIR_ERR_XML_ERROR, "%s",
745
                           _("missing storage pool source host name"));
746
            goto error;
747 748 749
        }
    }

750
    if (options->flags & VIR_STORAGE_POOL_SOURCE_DIR) {
751
        if (!ret->source.dir) {
752 753
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("missing storage pool source path"));
754
            goto error;
755 756
        }
    }
757
    if (options->flags & VIR_STORAGE_POOL_SOURCE_NAME) {
758 759
        if (ret->source.name == NULL) {
            /* source name defaults to pool name */
760
            if (VIR_STRDUP(ret->source.name, ret->name) < 0)
761
                goto error;
762 763
        }
    }
764

765
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
766
        (virStorageAdapterValidate(&ret->source.adapter)) < 0)
767
            goto error;
768

769 770 771
    /* If DEVICE is the only source type, then its required */
    if (options->flags == VIR_STORAGE_POOL_SOURCE_DEVICE) {
        if (!ret->source.ndevice) {
772 773
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("missing storage pool source device name"));
774
            goto error;
775 776 777
        }
    }

778 779 780
    /* When we are working with a virtual disk we can skip the target
     * path and permissions */
    if (!(options->flags & VIR_STORAGE_POOL_SOURCE_NETWORK)) {
781
        if (ret->type == VIR_STORAGE_POOL_LOGICAL) {
782
            if (virAsprintf(&target_path, "/dev/%s", ret->source.name) < 0)
783
                goto error;
R
Roman Bogorodskiy 已提交
784
        } else if (ret->type == VIR_STORAGE_POOL_ZFS) {
785
            if (virAsprintf(&target_path, "/dev/zvol/%s", ret->source.name) < 0)
R
Roman Bogorodskiy 已提交
786
                goto error;
787 788 789 790 791 792 793
        } else {
            target_path = virXPathString("string(./target/path)", ctxt);
            if (!target_path) {
                virReportError(VIR_ERR_XML_ERROR, "%s",
                               _("missing storage pool target path"));
                goto error;
            }
794
        }
795
        ret->target.path = virFileSanitizePath(target_path);
796
        if (!ret->target.path)
797
            goto error;
798

799
        if (virStorageDefParsePerms(ctxt, &ret->target.perms,
800
                                    "./target/permissions") < 0)
801
            goto error;
802
    }
803

804
 cleanup:
805
    VIR_FREE(uuid);
806 807 808 809
    VIR_FREE(type);
    VIR_FREE(target_path);
    return ret;

810
 error:
811
    virStoragePoolDefFree(ret);
812 813
    ret = NULL;
    goto cleanup;
814 815
}

816

817
virStoragePoolDefPtr
818
virStoragePoolDefParseNode(xmlDocPtr xml,
819 820
                           xmlNodePtr root)
{
821 822 823
    xmlXPathContextPtr ctxt = NULL;
    virStoragePoolDefPtr def = NULL;

824
    if (!xmlStrEqual(root->name, BAD_CAST "pool")) {
825
        virReportError(VIR_ERR_XML_ERROR,
826 827 828
                       _("unexpected root element <%s>, "
                         "expecting <pool>"),
                       root->name);
829 830 831 832 833
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
834
        virReportOOMError();
835 836 837 838
        goto cleanup;
    }

    ctxt->node = root;
839
    def = virStoragePoolDefParseXML(ctxt);
840
 cleanup:
841 842 843 844
    xmlXPathFreeContext(ctxt);
    return def;
}

845

846
static virStoragePoolDefPtr
847
virStoragePoolDefParse(const char *xmlStr,
848 849
                       const char *filename)
{
850
    virStoragePoolDefPtr ret = NULL;
J
Jiri Denemark 已提交
851
    xmlDocPtr xml;
852

853
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_pool_definition)")))) {
J
Jiri Denemark 已提交
854 855
        ret = virStoragePoolDefParseNode(xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
856 857
    }

858 859 860
    return ret;
}

861

862
virStoragePoolDefPtr
863
virStoragePoolDefParseString(const char *xmlStr)
864
{
865
    return virStoragePoolDefParse(xmlStr, NULL);
866 867
}

868

869
virStoragePoolDefPtr
870
virStoragePoolDefParseFile(const char *filename)
871
{
872
    return virStoragePoolDefParse(NULL, filename);
873 874
}

875

876
static int
877
virStoragePoolSourceFormat(virBufferPtr buf,
878
                           virStoragePoolOptionsPtr options,
879 880
                           virStoragePoolSourcePtr src)
{
881
    size_t i, j;
882

883 884 885
    virBufferAddLit(buf, "<source>\n");
    virBufferAdjustIndent(buf, 2);

886 887
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_HOST) && src->nhost) {
        for (i = 0; i < src->nhost; i++) {
888
            virBufferEscapeString(buf, "<host name='%s'",
889
                                  src->hosts[i].name);
890 891 892 893
            if (src->hosts[i].port)
                virBufferAsprintf(buf, " port='%d'", src->hosts[i].port);
            virBufferAddLit(buf, "/>\n");
        }
894
    }
895

896
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_DEVICE) &&
897
        src->ndevice) {
898
        for (i = 0; i < src->ndevice; i++) {
899 900 901 902 903 904 905
            virBufferEscapeString(buf, "<device path='%s'",
                                  src->devices[i].path);
            if (src->devices[i].part_separator !=
                VIR_TRISTATE_SWITCH_ABSENT) {
                virBufferAsprintf(buf, " part_separator='%s'",
                                  virTristateBoolTypeToString(src->devices[i].part_separator));
            }
906
            if (src->devices[i].nfreeExtent) {
907
                virBufferAddLit(buf, ">\n");
908
                virBufferAdjustIndent(buf, 2);
909
                for (j = 0; j < src->devices[i].nfreeExtent; j++) {
910
                    virBufferAsprintf(buf, "<freeExtent start='%llu' end='%llu'/>\n",
911 912 913
                                      src->devices[i].freeExtents[j].start,
                                      src->devices[i].freeExtents[j].end);
                }
914 915
                virBufferAdjustIndent(buf, -2);
                virBufferAddLit(buf, "</device>\n");
916
            } else {
917
                virBufferAddLit(buf, "/>\n");
918
            }
919 920
        }
    }
921

922
    if (options->flags & VIR_STORAGE_POOL_SOURCE_DIR)
923
        virBufferEscapeString(buf, "<dir path='%s'/>\n", src->dir);
924

925
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
926 927 928
        (src->adapter.type == VIR_STORAGE_ADAPTER_TYPE_FC_HOST ||
         src->adapter.type == VIR_STORAGE_ADAPTER_TYPE_SCSI_HOST))
        virStorageAdapterFormat(buf, &src->adapter);
929

930
    if (options->flags & VIR_STORAGE_POOL_SOURCE_NAME)
931
        virBufferEscapeString(buf, "<name>%s</name>\n", src->name);
932

D
David Allan 已提交
933 934
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_INITIATOR_IQN) &&
        src->initiator.iqn) {
935 936 937
        virBufferAddLit(buf, "<initiator>\n");
        virBufferAdjustIndent(buf, 2);
        virBufferEscapeString(buf, "<iqn name='%s'/>\n",
E
Eric Blake 已提交
938
                              src->initiator.iqn);
939 940
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</initiator>\n");
D
David Allan 已提交
941 942
    }

943 944 945
    if (options->formatToString) {
        const char *format = (options->formatToString)(src->format);
        if (!format) {
946 947 948
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown pool format number %d"),
                           src->format);
949 950
            return -1;
        }
951
        virBufferAsprintf(buf, "<format type='%s'/>\n", format);
952 953
    }

954 955 956
    if (src->auth) {
        if (virStorageAuthDefFormat(buf, src->auth) < 0)
            return -1;
957 958
    }

959 960
    virBufferEscapeString(buf, "<vendor name='%s'/>\n", src->vendor);
    virBufferEscapeString(buf, "<product name='%s'/>\n", src->product);
961

962 963
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</source>\n");
964 965 966
    return 0;
}

967

968 969 970
static int
virStoragePoolDefFormatBuf(virBufferPtr buf,
                           virStoragePoolDefPtr def)
971
{
972
    virStoragePoolOptionsPtr options;
973
    char uuid[VIR_UUID_STRING_BUFLEN];
974
    const char *type;
975

976
    options = virStoragePoolOptionsForPoolType(def->type);
977
    if (options == NULL)
978
        return -1;
979

980
    type = virStoragePoolTypeToString(def->type);
981
    if (!type) {
982 983
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unexpected pool type"));
984
        return -1;
985
    }
986 987 988
    virBufferAsprintf(buf, "<pool type='%s'>\n", type);
    virBufferAdjustIndent(buf, 2);
    virBufferEscapeString(buf, "<name>%s</name>\n", def->name);
989 990

    virUUIDFormat(def->uuid, uuid);
991
    virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuid);
992

993
    virBufferAsprintf(buf, "<capacity unit='bytes'>%llu</capacity>\n",
994
                      def->capacity);
995
    virBufferAsprintf(buf, "<allocation unit='bytes'>%llu</allocation>\n",
996
                      def->allocation);
997
    virBufferAsprintf(buf, "<available unit='bytes'>%llu</available>\n",
998
                      def->available);
999

1000 1001
    if (virStoragePoolSourceFormat(buf, options, &def->source) < 0)
        return -1;
1002

1003 1004
    /* RBD, Sheepdog, and Gluster devices are not local block devs nor
     * files, so they don't have a target */
1005
    if (def->type != VIR_STORAGE_POOL_RBD &&
1006 1007
        def->type != VIR_STORAGE_POOL_SHEEPDOG &&
        def->type != VIR_STORAGE_POOL_GLUSTER) {
1008 1009
        virBufferAddLit(buf, "<target>\n");
        virBufferAdjustIndent(buf, 2);
1010

1011
        virBufferEscapeString(buf, "<path>%s</path>\n", def->target.path);
1012

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        if (def->target.perms.mode != (mode_t) -1 ||
            def->target.perms.uid != (uid_t) -1 ||
            def->target.perms.gid != (gid_t) -1 ||
            def->target.perms.label) {
            virBufferAddLit(buf, "<permissions>\n");
            virBufferAdjustIndent(buf, 2);
            if (def->target.perms.mode != (mode_t) -1)
                virBufferAsprintf(buf, "<mode>0%o</mode>\n",
                                  def->target.perms.mode);
            if (def->target.perms.uid != (uid_t) -1)
                virBufferAsprintf(buf, "<owner>%d</owner>\n",
                                  (int) def->target.perms.uid);
            if (def->target.perms.gid != (gid_t) -1)
                virBufferAsprintf(buf, "<group>%d</group>\n",
                                  (int) def->target.perms.gid);
            virBufferEscapeString(buf, "<label>%s</label>\n",
                                  def->target.perms.label);

            virBufferAdjustIndent(buf, -2);
            virBufferAddLit(buf, "</permissions>\n");
        }
1034

1035 1036
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</target>\n");
1037
    }
1038 1039 1040 1041 1042 1043
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</pool>\n");

    return 0;
}

1044

1045 1046 1047 1048 1049 1050 1051
char *
virStoragePoolDefFormat(virStoragePoolDefPtr def)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (virStoragePoolDefFormatBuf(&buf, def) < 0)
        goto error;
1052

1053
    if (virBufferCheckError(&buf) < 0)
1054
        goto error;
1055

1056
    return virBufferContentAndReset(&buf);
1057

1058
 error:
1059
    virBufferFreeAndReset(&buf);
1060 1061 1062 1063 1064
    return NULL;
}


static int
1065
virStorageSize(const char *unit,
1066
               const char *val,
1067 1068
               unsigned long long *ret)
{
J
Ján Tomko 已提交
1069
    if (virStrToLong_ullp(val, NULL, 10, ret) < 0) {
1070 1071
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("malformed capacity element"));
1072 1073
        return -1;
    }
1074 1075 1076 1077
    /* off_t is signed, so you cannot create a file larger than 2**63
     * bytes in the first place.  */
    if (virScaleInteger(ret, unit, 1, LLONG_MAX) < 0)
        return -1;
1078 1079 1080 1081

    return 0;
}

1082

1083
static virStorageVolDefPtr
1084
virStorageVolDefParseXML(virStoragePoolDefPtr pool,
1085 1086
                         xmlXPathContextPtr ctxt,
                         unsigned int flags)
1087
{
1088
    virStorageVolDefPtr ret;
1089
    virStorageVolOptionsPtr options;
1090
    char *type = NULL;
1091 1092 1093
    char *allocation = NULL;
    char *capacity = NULL;
    char *unit = NULL;
1094
    char *backingStore = NULL;
1095
    xmlNodePtr node;
1096
    xmlNodePtr *nodes = NULL;
1097 1098
    size_t i;
    int n;
1099

1100 1101
    virCheckFlags(VIR_VOL_XML_PARSE_NO_CAPACITY |
                  VIR_VOL_XML_PARSE_OPT_CAPACITY, NULL);
1102

1103
    options = virStorageVolOptionsForPoolType(pool->type);
1104 1105 1106
    if (options == NULL)
        return NULL;

1107
    if (VIR_ALLOC(ret) < 0)
1108 1109
        return NULL;

1110
    ret->name = virXPathString("string(./name)", ctxt);
1111
    if (ret->name == NULL) {
1112 1113
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("missing volume name element"));
1114
        goto error;
1115 1116
    }

1117 1118
    /* Normally generated by pool refresh, but useful for unit tests */
    ret->key = virXPathString("string(./key)", ctxt);
1119

1120 1121 1122 1123
    /* Technically overridden by pool refresh, but useful for unit tests */
    type = virXPathString("string(./@type)", ctxt);
    if (type) {
        if ((ret->type = virStorageVolTypeFromString(type)) < 0) {
1124
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1125 1126 1127 1128 1129
                           _("unknown volume type '%s'"), type);
            goto error;
        }
    }

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
    if ((backingStore = virXPathString("string(./backingStore/path)", ctxt))) {
        if (VIR_ALLOC(ret->target.backingStore) < 0)
            goto error;

        ret->target.backingStore->path = backingStore;
        backingStore = NULL;

        if (options->formatFromString) {
            char *format = virXPathString("string(./backingStore/format/@type)", ctxt);
            if (format == NULL)
                ret->target.backingStore->format = options->defaultFormat;
            else
                ret->target.backingStore->format = (options->formatFromString)(format);

            if (ret->target.backingStore->format < 0) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unknown volume format type %s"), format);
                VIR_FREE(format);
                goto error;
            }
            VIR_FREE(format);
        }

        if (VIR_ALLOC(ret->target.backingStore->perms) < 0)
            goto error;
        if (virStorageDefParsePerms(ctxt, ret->target.backingStore->perms,
1156
                                    "./backingStore/permissions") < 0)
1157 1158 1159
            goto error;
    }

1160 1161
    capacity = virXPathString("string(./capacity)", ctxt);
    unit = virXPathString("string(./capacity/@unit)", ctxt);
1162 1163 1164
    if (capacity) {
        if (virStorageSize(unit, capacity, &ret->target.capacity) < 0)
            goto error;
1165 1166
    } else if (!(flags & VIR_VOL_XML_PARSE_NO_CAPACITY) &&
               !((flags & VIR_VOL_XML_PARSE_OPT_CAPACITY) && ret->target.backingStore)) {
1167
        virReportError(VIR_ERR_XML_ERROR, "%s", _("missing capacity element"));
1168
        goto error;
1169
    }
1170
    VIR_FREE(unit);
1171

1172
    allocation = virXPathString("string(./allocation)", ctxt);
1173
    if (allocation) {
1174
        unit = virXPathString("string(./allocation/@unit)", ctxt);
1175
        if (virStorageSize(unit, allocation, &ret->target.allocation) < 0)
1176
            goto error;
1177
        ret->target.has_allocation = true;
1178
    } else {
1179
        ret->target.allocation = ret->target.capacity;
1180 1181
    }

1182
    ret->target.path = virXPathString("string(./target/path)", ctxt);
1183
    if (options->formatFromString) {
1184
        char *format = virXPathString("string(./target/format/@type)", ctxt);
1185 1186 1187 1188 1189 1190
        if (format == NULL)
            ret->target.format = options->defaultFormat;
        else
            ret->target.format = (options->formatFromString)(format);

        if (ret->target.format < 0) {
1191
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1192
                           _("unknown volume format type %s"), format);
1193
            VIR_FREE(format);
1194
            goto error;
1195
        }
1196
        VIR_FREE(format);
1197 1198
    }

1199 1200 1201
    if (VIR_ALLOC(ret->target.perms) < 0)
        goto error;
    if (virStorageDefParsePerms(ctxt, ret->target.perms,
1202
                                "./target/permissions") < 0)
1203
        goto error;
1204

1205
    node = virXPathNode("./target/encryption", ctxt);
1206
    if (node != NULL) {
1207
        ret->target.encryption = virStorageEncryptionParseNode(ctxt->doc,
1208 1209
                                                               node);
        if (ret->target.encryption == NULL)
1210
            goto error;
1211 1212
    }

1213
    ret->target.compat = virXPathString("string(./target/compat)", ctxt);
1214 1215
    if (virStorageFileCheckCompat(ret->target.compat) < 0)
        goto error;
1216

C
Chunyan Liu 已提交
1217 1218 1219
    if (virXPathNode("./target/nocow", ctxt))
        ret->target.nocow = true;

1220
    if (virXPathNode("./target/features", ctxt)) {
1221 1222 1223 1224 1225 1226 1227
        if ((n = virXPathNodeSet("./target/features/*", ctxt, &nodes)) < 0)
            goto error;

        if (!ret->target.compat && VIR_STRDUP(ret->target.compat, "1.1") < 0)
            goto error;

        if (!(ret->target.features = virBitmapNew(VIR_STORAGE_FILE_FEATURE_LAST)))
1228
            goto error;
1229 1230

        for (i = 0; i < n; i++) {
1231
            int f = virStorageFileFeatureTypeFromString((const char*)nodes[i]->name);
1232 1233

            if (f < 0) {
1234
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unsupported feature %s"),
1235 1236 1237 1238 1239 1240 1241 1242
                               (const char*)nodes[i]->name);
                goto error;
            }
            ignore_value(virBitmapSetBit(ret->target.features, f));
        }
        VIR_FREE(nodes);
    }

1243
 cleanup:
1244
    VIR_FREE(nodes);
1245 1246 1247
    VIR_FREE(allocation);
    VIR_FREE(capacity);
    VIR_FREE(unit);
1248
    VIR_FREE(type);
1249
    VIR_FREE(backingStore);
1250 1251
    return ret;

1252
 error:
1253
    virStorageVolDefFree(ret);
1254 1255
    ret = NULL;
    goto cleanup;
1256 1257
}

1258

1259
virStorageVolDefPtr
1260
virStorageVolDefParseNode(virStoragePoolDefPtr pool,
1261
                          xmlDocPtr xml,
1262 1263
                          xmlNodePtr root,
                          unsigned int flags)
1264
{
1265 1266 1267
    xmlXPathContextPtr ctxt = NULL;
    virStorageVolDefPtr def = NULL;

1268
    if (!xmlStrEqual(root->name, BAD_CAST "volume")) {
1269
        virReportError(VIR_ERR_XML_ERROR,
1270 1271 1272
                       _("unexpected root element <%s>, "
                         "expecting <volume>"),
                       root->name);
1273 1274 1275 1276 1277
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1278
        virReportOOMError();
1279 1280 1281 1282
        goto cleanup;
    }

    ctxt->node = root;
1283
    def = virStorageVolDefParseXML(pool, ctxt, flags);
1284
 cleanup:
1285 1286 1287 1288
    xmlXPathFreeContext(ctxt);
    return def;
}

1289

1290
static virStorageVolDefPtr
1291
virStorageVolDefParse(virStoragePoolDefPtr pool,
1292
                      const char *xmlStr,
1293 1294
                      const char *filename,
                      unsigned int flags)
1295
{
1296
    virStorageVolDefPtr ret = NULL;
J
Jiri Denemark 已提交
1297
    xmlDocPtr xml;
1298

1299
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_volume_definition)")))) {
1300
        ret = virStorageVolDefParseNode(pool, xml, xmlDocGetRootElement(xml), flags);
J
Jiri Denemark 已提交
1301
        xmlFreeDoc(xml);
1302 1303 1304 1305 1306
    }

    return ret;
}

1307

1308
virStorageVolDefPtr
1309
virStorageVolDefParseString(virStoragePoolDefPtr pool,
1310 1311
                            const char *xmlStr,
                            unsigned int flags)
1312
{
1313
    return virStorageVolDefParse(pool, xmlStr, NULL, flags);
1314 1315
}

1316

1317
virStorageVolDefPtr
1318
virStorageVolDefParseFile(virStoragePoolDefPtr pool,
1319 1320
                          const char *filename,
                          unsigned int flags)
1321
{
1322
    return virStorageVolDefParse(pool, NULL, filename, flags);
1323
}
1324

1325

1326 1327 1328 1329 1330 1331
static void
virStorageVolTimestampFormat(virBufferPtr buf, const char *name,
                             struct timespec *ts)
{
    if (ts->tv_nsec < 0)
        return;
1332
    virBufferAsprintf(buf, "<%s>%llu", name,
1333 1334 1335 1336 1337 1338
                      (unsigned long long) ts->tv_sec);
    if (ts->tv_nsec)
       virBufferAsprintf(buf, ".%09ld", ts->tv_nsec);
    virBufferAsprintf(buf, "</%s>\n", name);
}

1339

1340
static int
1341
virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
1342
                             virBufferPtr buf,
1343
                             virStorageSourcePtr def,
1344 1345
                             const char *type)
{
1346 1347
    virBufferAsprintf(buf, "<%s>\n", type);
    virBufferAdjustIndent(buf, 2);
1348

1349
    virBufferEscapeString(buf, "<path>%s</path>\n", def->path);
1350 1351 1352 1353

    if (options->formatToString) {
        const char *format = (options->formatToString)(def->format);
        if (!format) {
1354 1355 1356
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown volume format number %d"),
                           def->format);
1357 1358
            return -1;
        }
1359
        virBufferAsprintf(buf, "<format type='%s'/>\n", format);
1360 1361
    }

1362 1363 1364 1365 1366
    if (def->perms &&
        (def->perms->mode != (mode_t) -1 ||
         def->perms->uid != (uid_t) -1 ||
         def->perms->gid != (gid_t) -1 ||
         def->perms->label)) {
1367 1368
        virBufferAddLit(buf, "<permissions>\n");
        virBufferAdjustIndent(buf, 2);
1369

1370 1371 1372
        if (def->perms->mode != (mode_t) -1)
            virBufferAsprintf(buf, "<mode>0%o</mode>\n",
                              def->perms->mode);
1373 1374 1375 1376 1377 1378
        if (def->perms->uid != (uid_t) -1)
            virBufferAsprintf(buf, "<owner>%d</owner>\n",
                              (int) def->perms->uid);
        if (def->perms->gid != (gid_t) -1)
            virBufferAsprintf(buf, "<group>%d</group>\n",
                              (int) def->perms->gid);
1379

1380 1381
        virBufferEscapeString(buf, "<label>%s</label>\n",
                              def->perms->label);
1382

1383 1384 1385
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</permissions>\n");
    }
1386

1387
    if (def->timestamps) {
1388 1389
        virBufferAddLit(buf, "<timestamps>\n");
        virBufferAdjustIndent(buf, 2);
1390 1391 1392 1393
        virStorageVolTimestampFormat(buf, "atime", &def->timestamps->atime);
        virStorageVolTimestampFormat(buf, "mtime", &def->timestamps->mtime);
        virStorageVolTimestampFormat(buf, "ctime", &def->timestamps->ctime);
        virStorageVolTimestampFormat(buf, "btime", &def->timestamps->btime);
1394 1395
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</timestamps>\n");
1396 1397
    }

1398 1399
    if (def->encryption &&
        virStorageEncryptionFormat(buf, def->encryption) < 0)
1400
            return -1;
1401

1402
    virBufferEscapeString(buf, "<compat>%s</compat>\n", def->compat);
1403

1404
    if (def->features) {
1405
        size_t i;
1406 1407
        bool empty = virBitmapIsAllClear(def->features);

1408 1409 1410 1411 1412 1413
        if (empty) {
            virBufferAddLit(buf, "<features/>\n");
        } else {
            virBufferAddLit(buf, "<features>\n");
            virBufferAdjustIndent(buf, 2);
        }
1414 1415

        for (i = 0; i < VIR_STORAGE_FILE_FEATURE_LAST; i++) {
J
Ján Tomko 已提交
1416
            if (virBitmapIsBitSet(def->features, i))
1417
                virBufferAsprintf(buf, "<%s/>\n",
1418
                                  virStorageFileFeatureTypeToString(i));
1419
        }
1420 1421 1422 1423
        if (!empty) {
            virBufferAdjustIndent(buf, -2);
            virBufferAddLit(buf, "</features>\n");
        }
1424 1425
    }

1426 1427
    virBufferAdjustIndent(buf, -2);
    virBufferAsprintf(buf, "</%s>\n", type);
1428 1429
    return 0;
}
1430

1431

1432
char *
1433
virStorageVolDefFormat(virStoragePoolDefPtr pool,
1434 1435
                       virStorageVolDefPtr def)
{
1436
    virStorageVolOptionsPtr options;
1437
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1438

1439
    options = virStorageVolOptionsForPoolType(pool->type);
1440 1441 1442
    if (options == NULL)
        return NULL;

1443 1444
    virBufferAsprintf(&buf, "<volume type='%s'>\n",
                      virStorageVolTypeToString(def->type));
1445 1446 1447 1448 1449 1450
    virBufferAdjustIndent(&buf, 2);

    virBufferEscapeString(&buf, "<name>%s</name>\n", def->name);
    virBufferEscapeString(&buf, "<key>%s</key>\n", def->key);
    virBufferAddLit(&buf, "<source>\n");
    virBufferAdjustIndent(&buf, 2);
1451 1452

    if (def->source.nextent) {
1453
        size_t i;
1454
        const char *thispath = NULL;
1455
        for (i = 0; i < def->source.nextent; i++) {
1456 1457 1458
            if (thispath == NULL ||
                STRNEQ(thispath, def->source.extents[i].path)) {
                if (thispath != NULL)
1459
                    virBufferAddLit(&buf, "</device>\n");
1460

1461
                virBufferEscapeString(&buf, "<device path='%s'>\n",
1462
                                      def->source.extents[i].path);
1463 1464
            }

1465 1466
            virBufferAdjustIndent(&buf, 2);
            virBufferAsprintf(&buf, "<extent start='%llu' end='%llu'/>\n",
1467 1468
                              def->source.extents[i].start,
                              def->source.extents[i].end);
1469
            virBufferAdjustIndent(&buf, -2);
1470 1471 1472
            thispath = def->source.extents[i].path;
        }
        if (thispath != NULL)
1473
            virBufferAddLit(&buf, "</device>\n");
1474 1475
    }

1476 1477 1478 1479
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</source>\n");

    virBufferAsprintf(&buf, "<capacity unit='bytes'>%llu</capacity>\n",
1480
                      def->target.capacity);
1481
    virBufferAsprintf(&buf, "<allocation unit='bytes'>%llu</allocation>\n",
1482
                      def->target.allocation);
1483 1484 1485 1486 1487 1488
    /* NB: Display only - since virStorageVolInfo is limited to just
     * 'capacity' and 'allocation' on output. Since we don't read this
     * in, be sure it was filled in before printing */
    if (def->target.physical)
        virBufferAsprintf(&buf, "<physical unit='bytes'>%llu</physical>\n",
                          def->target.physical);
1489

1490
    if (virStorageVolTargetDefFormat(options, &buf,
1491 1492
                                     &def->target, "target") < 0)
        goto cleanup;
1493

1494
    if (def->target.backingStore &&
1495
        virStorageVolTargetDefFormat(options, &buf,
1496 1497
                                     def->target.backingStore,
                                     "backingStore") < 0)
1498
        goto cleanup;
1499

1500
    virBufferAdjustIndent(&buf, -2);
E
Eric Blake 已提交
1501
    virBufferAddLit(&buf, "</volume>\n");
1502

1503 1504
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
1505

1506
    return virBufferContentAndReset(&buf);
1507

1508
 cleanup:
1509
    virBufferFreeAndReset(&buf);
1510 1511 1512 1513
    return NULL;
}


1514 1515 1516 1517
static int
virStoragePoolSaveXML(const char *path,
                      virStoragePoolDefPtr def,
                      const char *xml)
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    int ret = -1;

    virUUIDFormat(def->uuid, uuidstr);
    ret = virXMLSaveFile(path,
                         virXMLPickShellSafeComment(def->name, uuidstr),
                         "pool-edit", xml);

    return ret;
}
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562


int
virStoragePoolSaveState(const char *stateFile,
                        virStoragePoolDefPtr def)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    int ret = -1;
    char *xml;

    virBufferAddLit(&buf, "<poolstate>\n");
    virBufferAdjustIndent(&buf, 2);

    if (virStoragePoolDefFormatBuf(&buf, def) < 0)
        goto error;

    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</poolstate>\n");

    if (virBufferCheckError(&buf) < 0)
        goto error;

    if (!(xml = virBufferContentAndReset(&buf)))
        goto error;

    if (virStoragePoolSaveXML(stateFile, def, xml))
        goto error;

    ret = 0;

 error:
    VIR_FREE(xml);
    return ret;
}
1563 1564


1565
int
1566
virStoragePoolSaveConfig(const char *configFile,
1567 1568
                         virStoragePoolDefPtr def)
{
1569
    char *xml;
1570
    int ret = -1;
1571

1572 1573 1574 1575 1576 1577
    if (!(xml = virStoragePoolDefFormat(def))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to generate XML"));
        return -1;
    }

1578 1579
    if (virStoragePoolSaveXML(configFile, def, xml))
        goto cleanup;
1580

1581 1582 1583
    ret = 0;
 cleanup:
    VIR_FREE(xml);
1584 1585 1586
    return ret;
}

1587

1588
virStoragePoolSourcePtr
1589
virStoragePoolSourceListNewSource(virStoragePoolSourceListPtr list)
1590 1591 1592
{
    virStoragePoolSourcePtr source;

1593
    if (VIR_REALLOC_N(list->sources, list->nsources + 1) < 0)
1594 1595 1596 1597 1598 1599 1600 1601
        return NULL;

    source = &list->sources[list->nsources++];
    memset(source, 0, sizeof(*source));

    return source;
}

1602

1603 1604
char *
virStoragePoolSourceListFormat(virStoragePoolSourceListPtr def)
1605
{
1606
    virStoragePoolOptionsPtr options;
1607
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1608
    const char *type;
1609
    size_t i;
1610

1611
    options = virStoragePoolOptionsForPoolType(def->type);
1612 1613 1614
    if (options == NULL)
        return NULL;

1615
    type = virStoragePoolTypeToString(def->type);
1616
    if (!type) {
1617 1618
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unexpected pool type"));
1619 1620 1621 1622
        goto cleanup;
    }

    virBufferAddLit(&buf, "<sources>\n");
1623
    virBufferAdjustIndent(&buf, 2);
1624

1625
    for (i = 0; i < def->nsources; i++)
1626
        virStoragePoolSourceFormat(&buf, options, &def->sources[i]);
1627

1628
    virBufferAdjustIndent(&buf, -2);
1629 1630
    virBufferAddLit(&buf, "</sources>\n");

1631 1632
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
1633 1634

    return virBufferContentAndReset(&buf);
1635

1636
 cleanup:
1637
    virBufferFreeAndReset(&buf);
1638
    return NULL;
1639
}