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
                    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 530 531 532 533 534 535 536 537 538 539
    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;
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 958
    if (src->auth) {
        if (virStorageAuthDefFormat(buf, src->auth) < 0)
            return -1;
959 960
    }

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

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

969

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

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

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

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

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

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

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

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

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
        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");
        }
1036

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

    return 0;
}

1046

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

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

1055
    if (virBufferCheckError(&buf) < 0)
1056
        goto error;
1057

1058
    return virBufferContentAndReset(&buf);
1059

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


static int
1067
virStorageSize(const char *unit,
1068
               const char *val,
1069 1070
               unsigned long long *ret)
{
J
Ján Tomko 已提交
1071
    if (virStrToLong_ullp(val, NULL, 10, ret) < 0) {
1072 1073
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("malformed capacity element"));
1074 1075
        return -1;
    }
1076 1077 1078 1079
    /* 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;
1080 1081 1082 1083

    return 0;
}

1084

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

1102 1103
    virCheckFlags(VIR_VOL_XML_PARSE_NO_CAPACITY |
                  VIR_VOL_XML_PARSE_OPT_CAPACITY, NULL);
1104

1105
    options = virStorageVolOptionsForPoolType(pool->type);
1106 1107 1108
    if (options == NULL)
        return NULL;

1109
    if (VIR_ALLOC(ret) < 0)
1110 1111
        return NULL;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1260

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

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

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

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

1291

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

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

    return ret;
}

1309

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

1318

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

1327

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

1341

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1433

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

1441
    options = virStorageVolOptionsForPoolType(pool->type);
1442 1443 1444
    if (options == NULL)
        return NULL;

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

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

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

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

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

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

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

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

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

1505 1506
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
1507

1508
    return virBufferContentAndReset(&buf);
1509

1510
 cleanup:
1511
    virBufferFreeAndReset(&buf);
1512 1513 1514 1515
    return NULL;
}


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


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;
}
1565 1566


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

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

1580 1581
    if (virStoragePoolSaveXML(configFile, def, xml))
        goto cleanup;
1582

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

1589

1590
virStoragePoolSourcePtr
1591
virStoragePoolSourceListNewSource(virStoragePoolSourceListPtr list)
1592 1593 1594
{
    virStoragePoolSourcePtr source;

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

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

    return source;
}

1604

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

1613
    options = virStoragePoolOptionsForPoolType(def->type);
1614 1615 1616
    if (options == NULL)
        return NULL;

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

    virBufferAddLit(&buf, "<sources>\n");
1625
    virBufferAdjustIndent(&buf, 2);
1626

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

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

1633 1634
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
1635 1636

    return virBufferContentAndReset(&buf);
1637

1638
 cleanup:
1639
    virBufferFreeAndReset(&buf);
1640
    return NULL;
1641
}