storage_conf.c 48.9 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
                    goto cleanup;
                }
474
                VIR_FREE(port);
475 476 477
            }
        }
    }
478

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

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

486
    for (i = 0; i < nsource; i++) {
487
        char *partsep;
488 489 490 491 492 493
        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"));
494 495 496
            goto cleanup;
        }

497 498 499 500 501 502 503 504 505 506 507 508 509 510
        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);
        }

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

516 517
    }

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

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

529
    if ((authnode = virXPathNode("./auth", ctxt))) {
530
        if (!(authdef = virStorageAuthDefParse(authnode, ctxt)))
531 532 533 534 535 536 537 538 539
            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;
540
        authdef = NULL;
541
    }
542

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

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

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

556

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

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

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

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

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

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

    return ret;
}
593

594

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

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

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

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

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

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

        perms->uid = val;
647 648
    }

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

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

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

672

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

806
 cleanup:
807
    VIR_FREE(uuid);
808 809 810 811
    VIR_FREE(type);
    VIR_FREE(target_path);
    return ret;

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

818

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

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

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

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

847

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

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

860 861 862
    return ret;
}

863

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

870

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

877

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

885 886 887
    virBufferAddLit(buf, "<source>\n");
    virBufferAdjustIndent(buf, 2);

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

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

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

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

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

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

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

956 957
    if (src->auth)
        virStorageAuthDefFormat(buf, src->auth);
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 1111
    ret->target.type = VIR_STORAGE_TYPE_FILE;

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

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

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

1132 1133 1134 1135
    if ((backingStore = virXPathString("string(./backingStore/path)", ctxt))) {
        if (VIR_ALLOC(ret->target.backingStore) < 0)
            goto error;

1136 1137
        ret->target.backingStore->type = VIR_STORAGE_TYPE_FILE;

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
        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,
1160
                                    "./backingStore/permissions") < 0)
1161 1162 1163
            goto error;
    }

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

1177
    allocation = virXPathString("string(./allocation)", ctxt);
1178
    if (allocation) {
1179
        unit = virXPathString("string(./allocation/@unit)", ctxt);
1180
        if (virStorageSize(unit, allocation, &ret->target.allocation) < 0)
1181
            goto error;
1182
        ret->target.has_allocation = true;
1183
    } else {
1184
        ret->target.allocation = ret->target.capacity;
1185 1186
    }

1187
    ret->target.path = virXPathString("string(./target/path)", ctxt);
1188
    if (options->formatFromString) {
1189
        char *format = virXPathString("string(./target/format/@type)", ctxt);
1190 1191 1192 1193 1194 1195
        if (format == NULL)
            ret->target.format = options->defaultFormat;
        else
            ret->target.format = (options->formatFromString)(format);

        if (ret->target.format < 0) {
1196
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1197
                           _("unknown volume format type %s"), format);
1198
            VIR_FREE(format);
1199
            goto error;
1200
        }
1201
        VIR_FREE(format);
1202 1203
    }

1204 1205 1206
    if (VIR_ALLOC(ret->target.perms) < 0)
        goto error;
    if (virStorageDefParsePerms(ctxt, ret->target.perms,
1207
                                "./target/permissions") < 0)
1208
        goto error;
1209

1210
    node = virXPathNode("./target/encryption", ctxt);
1211
    if (node != NULL) {
1212
        ret->target.encryption = virStorageEncryptionParseNode(ctxt->doc,
1213 1214
                                                               node);
        if (ret->target.encryption == NULL)
1215
            goto error;
1216 1217
    }

1218
    ret->target.compat = virXPathString("string(./target/compat)", ctxt);
1219 1220
    if (virStorageFileCheckCompat(ret->target.compat) < 0)
        goto error;
1221

C
Chunyan Liu 已提交
1222 1223 1224
    if (virXPathNode("./target/nocow", ctxt))
        ret->target.nocow = true;

1225
    if (virXPathNode("./target/features", ctxt)) {
1226 1227 1228 1229 1230 1231 1232
        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)))
1233
            goto error;
1234 1235

        for (i = 0; i < n; i++) {
1236
            int f = virStorageFileFeatureTypeFromString((const char*)nodes[i]->name);
1237 1238

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

1248
 cleanup:
1249
    VIR_FREE(nodes);
1250 1251 1252
    VIR_FREE(allocation);
    VIR_FREE(capacity);
    VIR_FREE(unit);
1253
    VIR_FREE(type);
1254
    VIR_FREE(backingStore);
1255 1256
    return ret;

1257
 error:
1258
    virStorageVolDefFree(ret);
1259 1260
    ret = NULL;
    goto cleanup;
1261 1262
}

1263

1264
virStorageVolDefPtr
1265
virStorageVolDefParseNode(virStoragePoolDefPtr pool,
1266
                          xmlDocPtr xml,
1267 1268
                          xmlNodePtr root,
                          unsigned int flags)
1269
{
1270 1271 1272
    xmlXPathContextPtr ctxt = NULL;
    virStorageVolDefPtr def = NULL;

1273
    if (!virXMLNodeNameEqual(root, "volume")) {
1274
        virReportError(VIR_ERR_XML_ERROR,
1275 1276 1277
                       _("unexpected root element <%s>, "
                         "expecting <volume>"),
                       root->name);
1278 1279 1280 1281 1282
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1283
        virReportOOMError();
1284 1285 1286 1287
        goto cleanup;
    }

    ctxt->node = root;
1288
    def = virStorageVolDefParseXML(pool, ctxt, flags);
1289
 cleanup:
1290 1291 1292 1293
    xmlXPathFreeContext(ctxt);
    return def;
}

1294

1295
static virStorageVolDefPtr
1296
virStorageVolDefParse(virStoragePoolDefPtr pool,
1297
                      const char *xmlStr,
1298 1299
                      const char *filename,
                      unsigned int flags)
1300
{
1301
    virStorageVolDefPtr ret = NULL;
J
Jiri Denemark 已提交
1302
    xmlDocPtr xml;
1303

1304
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_volume_definition)")))) {
1305
        ret = virStorageVolDefParseNode(pool, xml, xmlDocGetRootElement(xml), flags);
J
Jiri Denemark 已提交
1306
        xmlFreeDoc(xml);
1307 1308 1309 1310 1311
    }

    return ret;
}

1312

1313
virStorageVolDefPtr
1314
virStorageVolDefParseString(virStoragePoolDefPtr pool,
1315 1316
                            const char *xmlStr,
                            unsigned int flags)
1317
{
1318
    return virStorageVolDefParse(pool, xmlStr, NULL, flags);
1319 1320
}

1321

1322
virStorageVolDefPtr
1323
virStorageVolDefParseFile(virStoragePoolDefPtr pool,
1324 1325
                          const char *filename,
                          unsigned int flags)
1326
{
1327
    return virStorageVolDefParse(pool, NULL, filename, flags);
1328
}
1329

1330

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

1344

1345
static int
1346
virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
1347
                             virBufferPtr buf,
1348
                             virStorageSourcePtr def,
1349 1350
                             const char *type)
{
1351 1352
    virBufferAsprintf(buf, "<%s>\n", type);
    virBufferAdjustIndent(buf, 2);
1353

1354
    virBufferEscapeString(buf, "<path>%s</path>\n", def->path);
1355 1356 1357 1358

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

1367 1368 1369 1370 1371
    if (def->perms &&
        (def->perms->mode != (mode_t) -1 ||
         def->perms->uid != (uid_t) -1 ||
         def->perms->gid != (gid_t) -1 ||
         def->perms->label)) {
1372 1373
        virBufferAddLit(buf, "<permissions>\n");
        virBufferAdjustIndent(buf, 2);
1374

1375 1376 1377
        if (def->perms->mode != (mode_t) -1)
            virBufferAsprintf(buf, "<mode>0%o</mode>\n",
                              def->perms->mode);
1378 1379 1380 1381 1382 1383
        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);
1384

1385 1386
        virBufferEscapeString(buf, "<label>%s</label>\n",
                              def->perms->label);
1387

1388 1389 1390
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</permissions>\n");
    }
1391

1392
    if (def->timestamps) {
1393 1394
        virBufferAddLit(buf, "<timestamps>\n");
        virBufferAdjustIndent(buf, 2);
1395 1396 1397 1398
        virStorageVolTimestampFormat(buf, "atime", &def->timestamps->atime);
        virStorageVolTimestampFormat(buf, "mtime", &def->timestamps->mtime);
        virStorageVolTimestampFormat(buf, "ctime", &def->timestamps->ctime);
        virStorageVolTimestampFormat(buf, "btime", &def->timestamps->btime);
1399 1400
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</timestamps>\n");
1401 1402
    }

1403 1404
    if (def->encryption &&
        virStorageEncryptionFormat(buf, def->encryption) < 0)
1405
            return -1;
1406

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

1409
    if (def->features) {
1410
        size_t i;
1411 1412
        bool empty = virBitmapIsAllClear(def->features);

1413 1414 1415 1416 1417 1418
        if (empty) {
            virBufferAddLit(buf, "<features/>\n");
        } else {
            virBufferAddLit(buf, "<features>\n");
            virBufferAdjustIndent(buf, 2);
        }
1419 1420

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

1431 1432
    virBufferAdjustIndent(buf, -2);
    virBufferAsprintf(buf, "</%s>\n", type);
1433 1434
    return 0;
}
1435

1436

1437
char *
1438
virStorageVolDefFormat(virStoragePoolDefPtr pool,
1439 1440
                       virStorageVolDefPtr def)
{
1441
    virStorageVolOptionsPtr options;
1442
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1443

1444
    options = virStorageVolOptionsForPoolType(pool->type);
1445 1446 1447
    if (options == NULL)
        return NULL;

1448 1449
    virBufferAsprintf(&buf, "<volume type='%s'>\n",
                      virStorageVolTypeToString(def->type));
1450 1451 1452 1453 1454 1455
    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);
1456 1457

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

1466
                virBufferEscapeString(&buf, "<device path='%s'>\n",
1467
                                      def->source.extents[i].path);
1468 1469
            }

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

1481 1482 1483 1484
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</source>\n");

    virBufferAsprintf(&buf, "<capacity unit='bytes'>%llu</capacity>\n",
1485
                      def->target.capacity);
1486
    virBufferAsprintf(&buf, "<allocation unit='bytes'>%llu</allocation>\n",
1487
                      def->target.allocation);
1488 1489 1490 1491 1492 1493
    /* 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);
1494

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

1499
    if (virStorageSourceHasBacking(&def->target) &&
1500
        virStorageVolTargetDefFormat(options, &buf,
1501 1502
                                     def->target.backingStore,
                                     "backingStore") < 0)
1503
        goto cleanup;
1504

1505
    virBufferAdjustIndent(&buf, -2);
E
Eric Blake 已提交
1506
    virBufferAddLit(&buf, "</volume>\n");
1507

1508 1509
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
1510

1511
    return virBufferContentAndReset(&buf);
1512

1513
 cleanup:
1514
    virBufferFreeAndReset(&buf);
1515 1516 1517 1518
    return NULL;
}


1519 1520 1521 1522
static int
virStoragePoolSaveXML(const char *path,
                      virStoragePoolDefPtr def,
                      const char *xml)
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
{
    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;
}
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 1563 1564 1565 1566 1567


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;
}
1568 1569


1570
int
1571
virStoragePoolSaveConfig(const char *configFile,
1572 1573
                         virStoragePoolDefPtr def)
{
1574
    char *xml;
1575
    int ret = -1;
1576

1577 1578 1579 1580 1581 1582
    if (!(xml = virStoragePoolDefFormat(def))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to generate XML"));
        return -1;
    }

1583 1584
    if (virStoragePoolSaveXML(configFile, def, xml))
        goto cleanup;
1585

1586 1587 1588
    ret = 0;
 cleanup:
    VIR_FREE(xml);
1589 1590 1591
    return ret;
}

1592

1593
virStoragePoolSourcePtr
1594
virStoragePoolSourceListNewSource(virStoragePoolSourceListPtr list)
1595 1596 1597
{
    virStoragePoolSourcePtr source;

1598
    if (VIR_REALLOC_N(list->sources, list->nsources + 1) < 0)
1599 1600 1601 1602 1603 1604 1605 1606
        return NULL;

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

    return source;
}

1607

1608 1609
char *
virStoragePoolSourceListFormat(virStoragePoolSourceListPtr def)
1610
{
1611
    virStoragePoolOptionsPtr options;
1612
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1613
    const char *type;
1614
    size_t i;
1615

1616
    options = virStoragePoolOptionsForPoolType(def->type);
1617 1618 1619
    if (options == NULL)
        return NULL;

1620
    type = virStoragePoolTypeToString(def->type);
1621
    if (!type) {
1622 1623
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unexpected pool type"));
1624 1625 1626 1627
        goto cleanup;
    }

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

1630
    for (i = 0; i < def->nsources; i++)
1631
        virStoragePoolSourceFormat(&buf, options, &def->sources[i]);
1632

1633
    virBufferAdjustIndent(&buf, -2);
1634 1635
    virBufferAddLit(&buf, "</sources>\n");

1636 1637
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
1638 1639

    return virBufferContentAndReset(&buf);
1640

1641
 cleanup:
1642
    virBufferFreeAndReset(&buf);
1643
    return NULL;
1644
}