storage_conf.c 54.4 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
 */

#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

29
#include "virerror.h"
30
#include "datatypes.h"
31
#include "node_device_conf.h"
32
#include "storage_adapter_conf.h"
33
#include "storage_conf.h"
34
#include "virstoragefile.h"
35

36
#include "virxml.h"
37
#include "viruuid.h"
38
#include "virbuffer.h"
39
#include "viralloc.h"
E
Eric Blake 已提交
40
#include "virfile.h"
41
#include "virscsihost.h"
42
#include "virstring.h"
43
#include "virlog.h"
44
#include "virvhba.h"
45

46 47
#define VIR_FROM_THIS VIR_FROM_STORAGE

48 49
VIR_LOG_INIT("conf.storage_conf");

50 51
VIR_ENUM_IMPL(virStorageVol,
              VIR_STORAGE_VOL_LAST,
O
Olga Krishtal 已提交
52
              "file", "block", "dir", "network",
53 54
              "netdir", "ploop",
);
55

56 57 58 59
VIR_ENUM_IMPL(virStoragePool,
              VIR_STORAGE_POOL_LAST,
              "dir", "fs", "netfs",
              "logical", "disk", "iscsi",
60 61
              "iscsi-direct", "scsi", "mpath",
              "rbd", "sheepdog", "gluster",
62 63
              "zfs", "vstorage",
);
64 65 66 67 68

VIR_ENUM_IMPL(virStoragePoolFormatFileSystem,
              VIR_STORAGE_POOL_FS_LAST,
              "auto", "ext2", "ext3",
              "ext4", "ufs", "iso9660", "udf",
69 70
              "gfs", "gfs2", "vfat", "hfs+", "xfs", "ocfs2",
);
71 72 73

VIR_ENUM_IMPL(virStoragePoolFormatFileSystemNet,
              VIR_STORAGE_POOL_NETFS_LAST,
74 75
              "auto", "nfs", "glusterfs", "cifs",
);
76 77 78 79

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

VIR_ENUM_IMPL(virStoragePoolFormatLogical,
              VIR_STORAGE_POOL_LOGICAL_LAST,
85 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 95
              "extended",
);
96

97 98 99 100 101
VIR_ENUM_IMPL(virStorageVolDefRefreshAllocation,
              VIR_STORAGE_VOL_DEF_REFRESH_ALLOCATION_LAST,
              "default", "capacity",
);

102
VIR_ENUM_IMPL(virStoragePartedFs,
103
              VIR_STORAGE_PARTED_FS_TYPE_LAST,
104
              "ext2", "ext2", "fat16",
105 106
              "fat32", "linux-swap",
              "ext2", "ext2",
107 108
              "extended",
);
109 110 111 112 113 114 115 116 117 118

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 {
119
    int defaultFormat;
120
    int lastFormat;
121 122 123 124 125 126
    virStorageVolFormatToString formatToString;
    virStorageVolFormatFromString formatFromString;
};

/* Flags to indicate mandatory components in the pool source */
enum {
O
Osier Yang 已提交
127 128 129 130 131 132 133
    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),
134 135 136 137 138
};

typedef struct _virStoragePoolOptions virStoragePoolOptions;
typedef virStoragePoolOptions *virStoragePoolOptionsPtr;
struct _virStoragePoolOptions {
E
Eric Blake 已提交
139
    unsigned int flags;
140
    int defaultFormat;
141
    int lastFormat;
142 143 144

    virStoragePoolXMLNamespace ns;

145 146 147 148 149 150 151 152 153 154 155 156
    virStoragePoolFormatToString formatToString;
    virStoragePoolFormatFromString formatFromString;
};

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

157

E
Eric Blake 已提交
158 159 160 161 162 163 164 165 166
static int
virStorageVolumeFormatFromString(const char *format)
{
    int ret = virStorageFileFormatTypeFromString(format);
    if (ret == VIR_STORAGE_FILE_NONE)
        return -1;
    return ret;
}

167

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


static virStoragePoolTypeInfoPtr
304 305
virStoragePoolTypeInfoLookup(int type)
{
306
    size_t i;
307
    for (i = 0; i < ARRAY_CARDINALITY(poolTypeInfo); i++)
308 309 310
        if (poolTypeInfo[i].poolType == type)
            return &poolTypeInfo[i];

311 312
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("missing backend for pool type %d"), type);
313 314 315
    return NULL;
}

316

317
static virStoragePoolOptionsPtr
318 319
virStoragePoolOptionsForPoolType(int type)
{
320 321 322 323 324 325
    virStoragePoolTypeInfoPtr backend = virStoragePoolTypeInfoLookup(type);
    if (backend == NULL)
        return NULL;
    return &backend->poolOptions;
}

326

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
/* virStoragePoolOptionsPoolTypeSetXMLNamespace:
 * @type: virStoragePoolType
 * @ns: xmlopt namespace pointer
 *
 * Store the @ns in the pool options for the particular backend.
 * This allows the parse/format code to then directly call the Namespace
 * method space (parse, format, href, free) as needed during processing.
 *
 * Returns: 0 on success, -1 on failure.
 */
int
virStoragePoolOptionsPoolTypeSetXMLNamespace(int type,
                                             virStoragePoolXMLNamespacePtr ns)
{
    int ret = -1;
    virStoragePoolTypeInfoPtr backend = virStoragePoolTypeInfoLookup(type);

    if (!backend)
        goto cleanup;

    backend->poolOptions.ns = *ns;
    ret = 0;

 cleanup:
    return ret;
}


355
static virStorageVolOptionsPtr
356 357
virStorageVolOptionsForPoolType(int type)
{
358 359 360 361 362 363 364
    virStoragePoolTypeInfoPtr backend = virStoragePoolTypeInfoLookup(type);
    if (backend == NULL)
        return NULL;
    return &backend->volOptions;
}


365 366 367 368 369 370 371 372 373
int
virStoragePoolOptionsFormatPool(virBufferPtr buf,
                                int type)
{
    virStoragePoolOptionsPtr poolOptions;

    if (!(poolOptions = virStoragePoolOptionsForPoolType(type)))
        return -1;

374
    if (!poolOptions->formatToString)
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        return 0;

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

    if (poolOptions->formatToString) {
        size_t i;

        virBufferAsprintf(buf, "<defaultFormat type='%s'/>\n",
                          (poolOptions->formatToString)(poolOptions->defaultFormat));

        virBufferAddLit(buf, "<enum name='sourceFormatType'>\n");
        virBufferAdjustIndent(buf, 2);

        for (i = 0; i < poolOptions->lastFormat; i++)
            virBufferAsprintf(buf, "<value>%s</value>\n",
                              (poolOptions->formatToString)(i));

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

    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</poolOptions>\n");
    return 0;
}


int
virStoragePoolOptionsFormatVolume(virBufferPtr buf,
                                  int type)
{
    size_t i;
    virStorageVolOptionsPtr volOptions;

    if (!(volOptions = virStorageVolOptionsForPoolType(type)))
        return -1;

    if (!volOptions->formatToString)
        return 0;

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

    virBufferAsprintf(buf, "<defaultFormat type='%s'/>\n",
                      (volOptions->formatToString)(volOptions->defaultFormat));

    virBufferAddLit(buf, "<enum name='targetFormatType'>\n");
    virBufferAdjustIndent(buf, 2);

    for (i = 0; i < volOptions->lastFormat; i++)
        virBufferAsprintf(buf, "<value>%s</value>\n",
                          (volOptions->formatToString)(i));

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

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

    return 0;
}


439
void
440 441
virStorageVolDefFree(virStorageVolDefPtr def)
{
442
    size_t i;
443 444 445 446

    if (!def)
        return;

447 448
    VIR_FREE(def->name);
    VIR_FREE(def->key);
449

450
    for (i = 0; i < def->source.nextent; i++)
451 452
        VIR_FREE(def->source.extents[i].path);
    VIR_FREE(def->source.extents);
453

454
    virStorageSourceClear(&def->target);
455
    VIR_FREE(def);
456 457
}

458

459 460 461 462 463 464 465
void
virStoragePoolSourceDeviceClear(virStoragePoolSourceDevicePtr dev)
{
    VIR_FREE(dev->freeExtents);
    VIR_FREE(dev->path);
}

466

467
void
468 469
virStoragePoolSourceClear(virStoragePoolSourcePtr source)
{
470
    size_t i;
471

472
    if (!source)
473 474
        return;

475
    for (i = 0; i < source->nhost; i++)
476 477 478
        VIR_FREE(source->hosts[i].name);
    VIR_FREE(source->hosts);

479 480
    for (i = 0; i < source->ndevice; i++)
        virStoragePoolSourceDeviceClear(&source->devices[i]);
481 482 483
    VIR_FREE(source->devices);
    VIR_FREE(source->dir);
    VIR_FREE(source->name);
484
    virStorageAdapterClear(&source->adapter);
485
    virStorageSourceInitiatorClear(&source->initiator);
486
    virStorageAuthDefFree(source->auth);
487 488
    VIR_FREE(source->vendor);
    VIR_FREE(source->product);
489 490
}

491

492 493 494 495 496 497 498
void
virStoragePoolSourceFree(virStoragePoolSourcePtr source)
{
    virStoragePoolSourceClear(source);
    VIR_FREE(source);
}

499

500
void
501 502
virStoragePoolDefFree(virStoragePoolDefPtr def)
{
503 504 505 506 507
    if (!def)
        return;

    VIR_FREE(def->name);

508
    virStoragePoolSourceClear(&def->source);
509

510 511
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
512 513
    if (def->namespaceData && def->ns.free)
        (def->ns.free)(def->namespaceData);
514
    VIR_FREE(def);
515 516 517
}


518
static int
519
virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
520 521
                             virStoragePoolSourcePtr source,
                             int pool_type,
522 523
                             xmlNodePtr node)
{
524
    int ret = -1;
525
    xmlNodePtr relnode, authnode;
526
    xmlNodePtr adapternode;
527 528
    int nsource;
    size_t i;
529
    virStoragePoolOptionsPtr options;
530
    int n;
531
    VIR_AUTOPTR(virStorageAuthDef) authdef = NULL;
532 533 534
    VIR_AUTOFREE(char *) port = NULL;
    VIR_AUTOFREE(char *) ver = NULL;
    VIR_AUTOFREE(xmlNodePtr *) nodeset = NULL;
535 536 537 538

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

539
    if ((options = virStoragePoolOptionsForPoolType(pool_type)) == NULL)
540 541
        goto cleanup;

542
    source->name = virXPathString("string(./name)", ctxt);
543
    if (pool_type == VIR_STORAGE_POOL_RBD && source->name == NULL) {
544
        virReportError(VIR_ERR_XML_ERROR, "%s",
545
                       _("element 'name' is mandatory for RBD pool"));
546 547
        goto cleanup;
    }
548 549

    if (options->formatFromString) {
550 551 552
        VIR_AUTOFREE(char *) format = NULL;

        format = virXPathString("string(./format/@type)", ctxt);
553 554 555 556 557 558
        if (format == NULL)
            source->format = options->defaultFormat;
        else
            source->format = options->formatFromString(format);

        if (source->format < 0) {
559
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
560
                           _("unknown pool format type %s"), format);
561 562 563 564
            goto cleanup;
        }
    }

565 566
    if ((n = virXPathNodeSet("./host", ctxt, &nodeset)) < 0)
        goto cleanup;
567

568 569
    if (n) {
        if (VIR_ALLOC_N(source->hosts, n) < 0)
570
            goto cleanup;
571
        source->nhost = n;
572

573
        for (i = 0; i < source->nhost; i++) {
574 575
            source->hosts[i].name = virXMLPropString(nodeset[i], "name");
            if (!source->hosts[i].name) {
576 577
                virReportError(VIR_ERR_XML_ERROR, "%s",
                               _("missing storage pool host name"));
578 579 580 581 582 583
                goto cleanup;
            }

            port = virXMLPropString(nodeset[i], "port");
            if (port) {
                if (virStrToLong_i(port, NULL, 10, &source->hosts[i].port) < 0) {
584 585 586
                    virReportError(VIR_ERR_XML_ERROR,
                                   _("Invalid port number: %s"),
                                   port);
587 588 589 590 591
                    goto cleanup;
                }
            }
        }
    }
592

593
    VIR_FREE(nodeset);
594 595

    virStorageSourceInitiatorParseXML(ctxt, &source->initiator);
596

597
    nsource = virXPathNodeSet("./device", ctxt, &nodeset);
598 599 600
    if (nsource < 0)
        goto cleanup;

601
    for (i = 0; i < nsource; i++) {
602
        VIR_AUTOFREE(char *) partsep = NULL;
603 604 605 606 607 608
        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"));
609 610 611
            goto cleanup;
        }

612 613 614 615 616 617 618 619 620 621 622 623
        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);
                goto cleanup;
            }
        }

624 625 626
        if (VIR_APPEND_ELEMENT(source->devices, source->ndevice, dev) < 0) {
            virStoragePoolSourceDeviceClear(&dev);
            goto cleanup;
627
        }
628

629 630
    }

631
    source->dir = virXPathString("string(./dir/@path)", ctxt);
632 633 634 635
    /* In gluster, a missing dir defaults to "/" */
    if (!source->dir && pool_type == VIR_STORAGE_POOL_GLUSTER &&
        VIR_STRDUP(source->dir, "/") < 0)
        goto cleanup;
636

637
    if ((adapternode = virXPathNode("./adapter", ctxt))) {
638
        if (virStorageAdapterParseXML(&source->adapter, adapternode, ctxt) < 0)
639 640
            goto cleanup;
    }
641

642
    if ((authnode = virXPathNode("./auth", ctxt))) {
643
        if (!(authdef = virStorageAuthDefParse(authnode, ctxt)))
644 645 646 647 648 649 650 651
            goto cleanup;

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

652
        VIR_STEAL_PTR(source->auth, authdef);
653
    }
654

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    /* Option protocol version string (NFSvN) */
    if ((ver = virXPathString("string(./protocol/@ver)", ctxt))) {
        if ((source->format != VIR_STORAGE_POOL_NETFS_NFS) &&
            (source->format != VIR_STORAGE_POOL_NETFS_AUTO)) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("storage pool protocol ver unsupported for "
                             "pool type '%s'"),
                           virStoragePoolFormatFileSystemNetTypeToString(source->format));
            goto cleanup;
        }
        if (virStrToLong_uip(ver, NULL, 0, &source->protocolVer) < 0) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("storage pool protocol ver '%s' is malformaed"),
                           ver);
            goto cleanup;
        }
    }

673 674 675
    source->vendor = virXPathString("string(./vendor/@name)", ctxt);
    source->product = virXPathString("string(./product/@name)", ctxt);

676
    ret = 0;
677
 cleanup:
678 679 680 681
    ctxt->node = relnode;

    return ret;
}
682

683

684
virStoragePoolSourcePtr
685
virStoragePoolDefParseSourceString(const char *srcSpec,
686 687 688 689 690
                                   int pool_type)
{
    xmlDocPtr doc = NULL;
    xmlNodePtr node = NULL;
    xmlXPathContextPtr xpath_ctxt = NULL;
691 692
    virStoragePoolSourcePtr ret = NULL;
    VIR_AUTOPTR(virStoragePoolSource) def = NULL;
693

694 695 696
    if (!(doc = virXMLParseStringCtxt(srcSpec,
                                      _("(storage_source_specification)"),
                                      &xpath_ctxt)))
697 698
        goto cleanup;

699
    if (VIR_ALLOC(def) < 0)
700 701
        goto cleanup;

702
    if (!(node = virXPathNode("/source", xpath_ctxt))) {
703 704
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("root element was not source"));
705 706 707
        goto cleanup;
    }

708
    if (virStoragePoolDefParseSource(xpath_ctxt, def, pool_type,
709 710 711
                                     node) < 0)
        goto cleanup;

712
    VIR_STEAL_PTR(ret, def);
713
 cleanup:
714 715 716 717 718
    xmlFreeDoc(doc);
    xmlXPathFreeContext(xpath_ctxt);

    return ret;
}
719

720

721
static int
722
virStorageDefParsePerms(xmlXPathContextPtr ctxt,
723
                        virStoragePermsPtr perms,
724
                        const char *permxpath)
725
{
726
    long long val;
727 728 729
    int ret = -1;
    xmlNodePtr relnode;
    xmlNodePtr node;
730
    VIR_AUTOFREE(char *) mode = NULL;
731

732
    node = virXPathNode(permxpath, ctxt);
733 734
    if (node == NULL) {
        /* Set default values if there is not <permissions> element */
735
        perms->mode = (mode_t) -1;
P
Philipp Hahn 已提交
736 737
        perms->uid = (uid_t) -1;
        perms->gid = (gid_t) -1;
738 739 740 741 742 743 744
        perms->label = NULL;
        return 0;
    }

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

745
    if ((mode = virXPathString("string(./mode)", ctxt))) {
746 747 748
        int tmp;

        if (virStrToLong_i(mode, NULL, 8, &tmp) < 0 || (tmp & ~0777)) {
749 750
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("malformed octal mode"));
751
            goto error;
752
        }
753
        perms->mode = tmp;
754 755
    } else {
        perms->mode = (mode_t) -1;
756 757
    }

758
    if (virXPathNode("./owner", ctxt) == NULL) {
P
Philipp Hahn 已提交
759
        perms->uid = (uid_t) -1;
760
    } else {
761
        /* We previously could output -1, so continue to parse it */
762
        if (virXPathLongLong("number(./owner)", ctxt, &val) < 0 ||
763 764
            ((uid_t)val != val &&
             val != -1)) {
765 766
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("malformed owner element"));
767
            goto error;
768
        }
769 770

        perms->uid = val;
771 772
    }

773
    if (virXPathNode("./group", ctxt) == NULL) {
P
Philipp Hahn 已提交
774
        perms->gid = (gid_t) -1;
775
    } else {
776
        /* We previously could output -1, so continue to parse it */
777
        if (virXPathLongLong("number(./group)", ctxt, &val) < 0 ||
778 779
            ((gid_t) val != val &&
             val != -1)) {
780 781
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("malformed group element"));
782
            goto error;
783
        }
784
        perms->gid = val;
785 786 787
    }

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

790
    ret = 0;
791
 error:
792 793
    ctxt->node = relnode;
    return ret;
794 795
}

796

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
static int
virStoragePoolDefRefreshParse(xmlXPathContextPtr ctxt,
                              virStoragePoolDefPtr def)
{
    VIR_AUTOFREE(virStoragePoolDefRefreshPtr) refresh = NULL;
    VIR_AUTOFREE(char *) allocation = NULL;
    int tmp;

    allocation = virXPathString("string(./refresh/volume/@allocation)", ctxt);

    if (!allocation)
        return 0;

    if ((tmp = virStorageVolDefRefreshAllocationTypeFromString(allocation)) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unknown storage pool volume refresh allocation type %s"),
                       allocation);
        return -1;
    }

    if (VIR_ALLOC(refresh) < 0)
        return -1;

    refresh->volume.allocation = tmp;
    VIR_STEAL_PTR(def->refresh, refresh);
    return 0;
}


static void
virStoragePoolDefRefreshFormat(virBufferPtr buf,
                               virStoragePoolDefRefreshPtr refresh)
{
    if (!refresh)
        return;

    virBufferAddLit(buf, "<refresh>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferAsprintf(buf, "<volume allocation='%s'/>\n",
                      virStorageVolDefRefreshAllocationTypeToString(refresh->volume.allocation));
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</refresh>\n");
}


J
John Ferlan 已提交
842
virStoragePoolDefPtr
843 844
virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
{
845
    virStoragePoolOptionsPtr options;
846
    virStoragePoolDefPtr ret = NULL;
847
    xmlNodePtr source_node;
848
    VIR_AUTOPTR(virStoragePoolDef) def = NULL;
849 850 851
    VIR_AUTOFREE(char *) type = NULL;
    VIR_AUTOFREE(char *) uuid = NULL;
    VIR_AUTOFREE(char *) target_path = NULL;
852
    VIR_AUTOFREE(char *) refresh_volume_allocation = NULL;
853

854
    if (VIR_ALLOC(def) < 0)
855 856
        return NULL;

857
    type = virXPathString("string(./@type)", ctxt);
858 859 860
    if (type == NULL) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("storage pool missing type attribute"));
861
        return NULL;
862 863
    }

864
    if ((def->type = virStoragePoolTypeFromString(type)) < 0) {
865
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
866
                       _("unknown storage pool type %s"), type);
867
        return NULL;
868 869
    }

870
    if ((options = virStoragePoolOptionsForPoolType(def->type)) == NULL)
871
        return NULL;
872

873
    source_node = virXPathNode("./source", ctxt);
874
    if (source_node) {
875
        if (virStoragePoolDefParseSource(ctxt, &def->source, def->type,
876
                                         source_node) < 0)
877
            return NULL;
878 879
    } else {
        if (options->formatFromString)
880
            def->source.format = options->defaultFormat;
881 882
    }

883 884
    def->name = virXPathString("string(./name)", ctxt);
    if (def->name == NULL &&
885
        options->flags & VIR_STORAGE_POOL_SOURCE_NAME &&
886
        VIR_STRDUP(def->name, def->source.name) < 0)
887 888
        return NULL;

889
    if (def->name == NULL) {
890 891
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("missing pool source name element"));
892
        return NULL;
893 894
    }

895
    if (strchr(def->name, '/')) {
896
        virReportError(VIR_ERR_XML_ERROR,
897
                       _("name %s cannot contain '/'"), def->name);
898
        return NULL;
899 900
    }

901
    uuid = virXPathString("string(./uuid)", ctxt);
902
    if (uuid == NULL) {
903
        if (virUUIDGenerate(def->uuid) < 0) {
904 905
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("unable to generate uuid"));
906
            return NULL;
907 908
        }
    } else {
909
        if (virUUIDParse(uuid, def->uuid) < 0) {
910 911
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("malformed uuid element"));
912
            return NULL;
913 914 915
        }
    }

916
    if (options->flags & VIR_STORAGE_POOL_SOURCE_HOST) {
917
        if (!def->source.nhost) {
918
            virReportError(VIR_ERR_XML_ERROR, "%s",
919
                           _("missing storage pool source host name"));
920
            return NULL;
921 922 923
        }
    }

924
    if (options->flags & VIR_STORAGE_POOL_SOURCE_DIR) {
925
        if (!def->source.dir) {
926 927
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("missing storage pool source path"));
928
            return NULL;
929 930
        }
    }
931
    if (options->flags & VIR_STORAGE_POOL_SOURCE_NAME) {
932
        if (def->source.name == NULL) {
933
            /* source name defaults to pool name */
934
            if (VIR_STRDUP(def->source.name, def->name) < 0)
935
                return NULL;
936 937
        }
    }
938

939
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
940
        (virStorageAdapterValidate(&def->source.adapter)) < 0)
941
            return NULL;
942

943 944
    /* If DEVICE is the only source type, then its required */
    if (options->flags == VIR_STORAGE_POOL_SOURCE_DEVICE) {
945
        if (!def->source.ndevice) {
946 947
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("missing storage pool source device name"));
948
            return NULL;
949 950 951
        }
    }

952 953 954
    /* When we are working with a virtual disk we can skip the target
     * path and permissions */
    if (!(options->flags & VIR_STORAGE_POOL_SOURCE_NETWORK)) {
955 956
        if (def->type == VIR_STORAGE_POOL_LOGICAL) {
            if (virAsprintf(&target_path, "/dev/%s", def->source.name) < 0)
957
                return NULL;
958 959
        } else if (def->type == VIR_STORAGE_POOL_ZFS) {
            if (virAsprintf(&target_path, "/dev/zvol/%s", def->source.name) < 0)
960
                return NULL;
961 962 963 964 965
        } else {
            target_path = virXPathString("string(./target/path)", ctxt);
            if (!target_path) {
                virReportError(VIR_ERR_XML_ERROR, "%s",
                               _("missing storage pool target path"));
966
                return NULL;
967
            }
968
        }
969 970
        def->target.path = virFileSanitizePath(target_path);
        if (!def->target.path)
971
            return NULL;
972

973
        if (virStorageDefParsePerms(ctxt, &def->target.perms,
974
                                    "./target/permissions") < 0)
975
            return NULL;
976
    }
977

978 979
    if (def->type == VIR_STORAGE_POOL_ISCSI_DIRECT &&
        !def->source.initiator.iqn) {
980 981
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("missing initiator IQN"));
982
        return NULL;
983 984
    }

985 986 987
    if (virStoragePoolDefRefreshParse(ctxt, def) < 0)
        return NULL;

988 989
    /* Make a copy of all the callback pointers here for easier use,
     * especially during the virStoragePoolSourceClear method */
990 991 992
    def->ns = options->ns;
    if (def->ns.parse &&
        (def->ns.parse)(ctxt, &def->namespaceData) < 0)
993
        return NULL;
994

995
    VIR_STEAL_PTR(ret, def);
996
    return ret;
997 998
}

999

1000
virStoragePoolDefPtr
1001
virStoragePoolDefParseNode(xmlDocPtr xml,
1002 1003
                           xmlNodePtr root)
{
1004 1005 1006
    xmlXPathContextPtr ctxt = NULL;
    virStoragePoolDefPtr def = NULL;

1007
    if (!virXMLNodeNameEqual(root, "pool")) {
1008
        virReportError(VIR_ERR_XML_ERROR,
1009 1010 1011
                       _("unexpected root element <%s>, "
                         "expecting <pool>"),
                       root->name);
1012 1013 1014 1015 1016
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1017
        virReportOOMError();
1018 1019 1020 1021
        goto cleanup;
    }

    ctxt->node = root;
1022
    def = virStoragePoolDefParseXML(ctxt);
1023
 cleanup:
1024 1025 1026 1027
    xmlXPathFreeContext(ctxt);
    return def;
}

1028

1029
static virStoragePoolDefPtr
1030
virStoragePoolDefParse(const char *xmlStr,
1031 1032
                       const char *filename)
{
1033
    virStoragePoolDefPtr ret = NULL;
J
Jiri Denemark 已提交
1034
    xmlDocPtr xml;
1035

1036
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_pool_definition)")))) {
J
Jiri Denemark 已提交
1037 1038
        ret = virStoragePoolDefParseNode(xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
1039 1040
    }

1041 1042 1043
    return ret;
}

1044

1045
virStoragePoolDefPtr
1046
virStoragePoolDefParseString(const char *xmlStr)
1047
{
1048
    return virStoragePoolDefParse(xmlStr, NULL);
1049 1050
}

1051

1052
virStoragePoolDefPtr
1053
virStoragePoolDefParseFile(const char *filename)
1054
{
1055
    return virStoragePoolDefParse(NULL, filename);
1056 1057
}

1058

1059
static int
1060
virStoragePoolSourceFormat(virBufferPtr buf,
1061
                           virStoragePoolOptionsPtr options,
1062 1063
                           virStoragePoolSourcePtr src)
{
1064
    size_t i, j;
1065

1066 1067 1068
    virBufferAddLit(buf, "<source>\n");
    virBufferAdjustIndent(buf, 2);

1069 1070
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_HOST) && src->nhost) {
        for (i = 0; i < src->nhost; i++) {
1071
            virBufferEscapeString(buf, "<host name='%s'",
1072
                                  src->hosts[i].name);
1073 1074 1075 1076
            if (src->hosts[i].port)
                virBufferAsprintf(buf, " port='%d'", src->hosts[i].port);
            virBufferAddLit(buf, "/>\n");
        }
1077
    }
1078

1079
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_DEVICE) &&
1080
        src->ndevice) {
1081
        for (i = 0; i < src->ndevice; i++) {
1082 1083 1084 1085 1086 1087 1088
            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));
            }
1089
            if (src->devices[i].nfreeExtent) {
1090
                virBufferAddLit(buf, ">\n");
1091
                virBufferAdjustIndent(buf, 2);
1092
                for (j = 0; j < src->devices[i].nfreeExtent; j++) {
1093
                    virBufferAsprintf(buf, "<freeExtent start='%llu' end='%llu'/>\n",
1094 1095 1096
                                      src->devices[i].freeExtents[j].start,
                                      src->devices[i].freeExtents[j].end);
                }
1097 1098
                virBufferAdjustIndent(buf, -2);
                virBufferAddLit(buf, "</device>\n");
1099
            } else {
1100
                virBufferAddLit(buf, "/>\n");
1101
            }
1102 1103
        }
    }
1104

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

1108
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
1109 1110 1111
        (src->adapter.type == VIR_STORAGE_ADAPTER_TYPE_FC_HOST ||
         src->adapter.type == VIR_STORAGE_ADAPTER_TYPE_SCSI_HOST))
        virStorageAdapterFormat(buf, &src->adapter);
1112

1113
    if (options->flags & VIR_STORAGE_POOL_SOURCE_NAME)
1114
        virBufferEscapeString(buf, "<name>%s</name>\n", src->name);
1115

1116 1117
    if (options->flags & VIR_STORAGE_POOL_SOURCE_INITIATOR_IQN)
        virStorageSourceInitiatorFormatXML(&src->initiator, buf);
D
David Allan 已提交
1118

1119 1120 1121
    if (options->formatToString) {
        const char *format = (options->formatToString)(src->format);
        if (!format) {
1122 1123 1124
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown pool format number %d"),
                           src->format);
1125 1126
            return -1;
        }
1127
        virBufferAsprintf(buf, "<format type='%s'/>\n", format);
1128 1129
    }

1130 1131
    if (src->auth)
        virStorageAuthDefFormat(buf, src->auth);
1132

1133 1134 1135
    if (src->protocolVer)
        virBufferAsprintf(buf, "<protocol ver='%u'/>\n", src->protocolVer);

1136 1137
    virBufferEscapeString(buf, "<vendor name='%s'/>\n", src->vendor);
    virBufferEscapeString(buf, "<product name='%s'/>\n", src->product);
1138

1139 1140
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</source>\n");
1141 1142 1143
    return 0;
}

1144

1145 1146 1147
static int
virStoragePoolDefFormatBuf(virBufferPtr buf,
                           virStoragePoolDefPtr def)
1148
{
1149
    virStoragePoolOptionsPtr options;
1150
    char uuid[VIR_UUID_STRING_BUFLEN];
1151
    const char *type;
1152

1153
    options = virStoragePoolOptionsForPoolType(def->type);
1154
    if (options == NULL)
1155
        return -1;
1156

1157
    type = virStoragePoolTypeToString(def->type);
1158
    if (!type) {
1159 1160
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unexpected pool type"));
1161
        return -1;
1162
    }
1163 1164 1165 1166
    virBufferAsprintf(buf, "<pool type='%s'", type);
    if (def->namespaceData && def->ns.href)
        virBufferAsprintf(buf, " %s", (def->ns.href)());
    virBufferAddLit(buf, ">\n");
1167 1168
    virBufferAdjustIndent(buf, 2);
    virBufferEscapeString(buf, "<name>%s</name>\n", def->name);
1169 1170

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

1173
    virBufferAsprintf(buf, "<capacity unit='bytes'>%llu</capacity>\n",
1174
                      def->capacity);
1175
    virBufferAsprintf(buf, "<allocation unit='bytes'>%llu</allocation>\n",
1176
                      def->allocation);
1177
    virBufferAsprintf(buf, "<available unit='bytes'>%llu</available>\n",
1178
                      def->available);
1179

1180 1181
    if (virStoragePoolSourceFormat(buf, options, &def->source) < 0)
        return -1;
1182

1183
    /* RBD, Sheepdog, Gluster and Iscsi-direct devices are not local block devs nor
1184
     * files, so they don't have a target */
1185
    if (def->type != VIR_STORAGE_POOL_RBD &&
1186
        def->type != VIR_STORAGE_POOL_SHEEPDOG &&
1187 1188
        def->type != VIR_STORAGE_POOL_GLUSTER &&
        def->type != VIR_STORAGE_POOL_ISCSI_DIRECT) {
1189 1190
        virBufferAddLit(buf, "<target>\n");
        virBufferAdjustIndent(buf, 2);
1191

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

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
        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");
        }
1215

1216 1217
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</target>\n");
1218
    }
1219

1220 1221
    virStoragePoolDefRefreshFormat(buf, def->refresh);

1222 1223 1224 1225 1226
    if (def->namespaceData && def->ns.format) {
        if ((def->ns.format)(buf, def->namespaceData) < 0)
            return -1;
    }

1227 1228 1229 1230 1231 1232
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</pool>\n");

    return 0;
}

1233

1234 1235 1236 1237 1238 1239 1240
char *
virStoragePoolDefFormat(virStoragePoolDefPtr def)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

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

1242
    if (virBufferCheckError(&buf) < 0)
1243
        goto error;
1244

1245
    return virBufferContentAndReset(&buf);
1246

1247
 error:
1248
    virBufferFreeAndReset(&buf);
1249 1250 1251 1252 1253
    return NULL;
}


static int
1254
virStorageSize(const char *unit,
1255
               const char *val,
1256 1257
               unsigned long long *ret)
{
J
Ján Tomko 已提交
1258
    if (virStrToLong_ullp(val, NULL, 10, ret) < 0) {
1259 1260
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("malformed capacity element"));
1261 1262
        return -1;
    }
1263 1264 1265 1266
    /* 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;
1267 1268 1269 1270

    return 0;
}

1271

1272
static virStorageVolDefPtr
1273
virStorageVolDefParseXML(virStoragePoolDefPtr pool,
1274 1275
                         xmlXPathContextPtr ctxt,
                         unsigned int flags)
1276
{
1277
    virStorageVolDefPtr ret = NULL;
1278
    virStorageVolOptionsPtr options;
1279
    xmlNodePtr node;
1280 1281
    size_t i;
    int n;
1282
    VIR_AUTOPTR(virStorageVolDef) def = NULL;
1283 1284 1285 1286 1287 1288
    VIR_AUTOFREE(char *) type = NULL;
    VIR_AUTOFREE(char *) allocation = NULL;
    VIR_AUTOFREE(char *) capacity = NULL;
    VIR_AUTOFREE(char *) unit = NULL;
    VIR_AUTOFREE(char *) backingStore = NULL;
    VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
1289

1290 1291
    virCheckFlags(VIR_VOL_XML_PARSE_NO_CAPACITY |
                  VIR_VOL_XML_PARSE_OPT_CAPACITY, NULL);
1292

1293
    options = virStorageVolOptionsForPoolType(pool->type);
1294 1295 1296
    if (options == NULL)
        return NULL;

1297
    if (VIR_ALLOC(def) < 0)
1298 1299
        return NULL;

1300
    def->target.type = VIR_STORAGE_TYPE_FILE;
1301

1302 1303
    def->name = virXPathString("string(./name)", ctxt);
    if (def->name == NULL) {
1304 1305
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("missing volume name element"));
1306
        return NULL;
1307 1308
    }

1309
    /* Normally generated by pool refresh, but useful for unit tests */
1310
    def->key = virXPathString("string(./key)", ctxt);
1311

1312 1313 1314
    /* Technically overridden by pool refresh, but useful for unit tests */
    type = virXPathString("string(./@type)", ctxt);
    if (type) {
1315
        if ((def->type = virStorageVolTypeFromString(type)) < 0) {
1316
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1317
                           _("unknown volume type '%s'"), type);
1318
            return NULL;
1319 1320 1321
        }
    }

1322
    if ((backingStore = virXPathString("string(./backingStore/path)", ctxt))) {
1323
        if (!(def->target.backingStore = virStorageSourceNew()))
1324
            return NULL;
1325

1326
        def->target.backingStore->type = VIR_STORAGE_TYPE_FILE;
1327

1328
        def->target.backingStore->path = backingStore;
1329 1330 1331
        backingStore = NULL;

        if (options->formatFromString) {
1332 1333 1334
            VIR_AUTOFREE(char *) format = NULL;

            format = virXPathString("string(./backingStore/format/@type)", ctxt);
1335
            if (format == NULL)
1336
                def->target.backingStore->format = options->defaultFormat;
1337
            else
1338
                def->target.backingStore->format = (options->formatFromString)(format);
1339

1340
            if (def->target.backingStore->format < 0) {
1341 1342
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unknown volume format type %s"), format);
1343
                return NULL;
1344 1345 1346
            }
        }

1347
        if (VIR_ALLOC(def->target.backingStore->perms) < 0)
1348
            return NULL;
1349
        if (virStorageDefParsePerms(ctxt, def->target.backingStore->perms,
1350
                                    "./backingStore/permissions") < 0)
1351
            return NULL;
1352 1353
    }

1354 1355
    capacity = virXPathString("string(./capacity)", ctxt);
    unit = virXPathString("string(./capacity/@unit)", ctxt);
1356
    if (capacity) {
1357
        if (virStorageSize(unit, capacity, &def->target.capacity) < 0)
1358
            return NULL;
1359
    } else if (!(flags & VIR_VOL_XML_PARSE_NO_CAPACITY) &&
1360
               !((flags & VIR_VOL_XML_PARSE_OPT_CAPACITY) &&
1361
                 virStorageSourceHasBacking(&def->target))) {
1362
        virReportError(VIR_ERR_XML_ERROR, "%s", _("missing capacity element"));
1363
        return NULL;
1364
    }
1365
    VIR_FREE(unit);
1366

1367
    allocation = virXPathString("string(./allocation)", ctxt);
1368
    if (allocation) {
1369
        unit = virXPathString("string(./allocation/@unit)", ctxt);
1370
        if (virStorageSize(unit, allocation, &def->target.allocation) < 0)
1371
            return NULL;
1372
        def->target.has_allocation = true;
1373
    } else {
1374
        def->target.allocation = def->target.capacity;
1375 1376
    }

1377
    def->target.path = virXPathString("string(./target/path)", ctxt);
1378
    if (options->formatFromString) {
1379 1380 1381
        VIR_AUTOFREE(char *) format = NULL;

        format = virXPathString("string(./target/format/@type)", ctxt);
1382
        if (format == NULL)
1383
            def->target.format = options->defaultFormat;
1384
        else
1385
            def->target.format = (options->formatFromString)(format);
1386

1387
        if (def->target.format < 0) {
1388
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1389
                           _("unknown volume format type %s"), format);
1390
            return NULL;
1391 1392 1393
        }
    }

1394
    if (VIR_ALLOC(def->target.perms) < 0)
1395
        return NULL;
1396
    if (virStorageDefParsePerms(ctxt, def->target.perms,
1397
                                "./target/permissions") < 0)
1398
        return NULL;
1399

1400
    node = virXPathNode("./target/encryption", ctxt);
1401
    if (node != NULL) {
1402 1403
        def->target.encryption = virStorageEncryptionParseNode(node, ctxt);
        if (def->target.encryption == NULL)
1404
            return NULL;
1405 1406
    }

1407 1408
    def->target.compat = virXPathString("string(./target/compat)", ctxt);
    if (virStorageFileCheckCompat(def->target.compat) < 0)
1409
        return NULL;
1410

C
Chunyan Liu 已提交
1411
    if (virXPathNode("./target/nocow", ctxt))
1412
        def->target.nocow = true;
C
Chunyan Liu 已提交
1413

1414
    if (virXPathNode("./target/features", ctxt)) {
1415
        if ((n = virXPathNodeSet("./target/features/*", ctxt, &nodes)) < 0)
1416
            return NULL;
1417

1418
        if (!def->target.compat && VIR_STRDUP(def->target.compat, "1.1") < 0)
1419
            return NULL;
1420

1421
        if (!(def->target.features = virBitmapNew(VIR_STORAGE_FILE_FEATURE_LAST)))
1422
            return NULL;
1423 1424

        for (i = 0; i < n; i++) {
1425
            int f = virStorageFileFeatureTypeFromString((const char*)nodes[i]->name);
1426 1427

            if (f < 0) {
1428
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unsupported feature %s"),
1429
                               (const char*)nodes[i]->name);
1430
                return NULL;
1431
            }
1432
            ignore_value(virBitmapSetBit(def->target.features, f));
1433 1434 1435 1436
        }
        VIR_FREE(nodes);
    }

1437
    VIR_STEAL_PTR(ret, def);
1438
    return ret;
1439 1440
}

1441

1442
virStorageVolDefPtr
1443
virStorageVolDefParseNode(virStoragePoolDefPtr pool,
1444
                          xmlDocPtr xml,
1445 1446
                          xmlNodePtr root,
                          unsigned int flags)
1447
{
1448 1449 1450
    xmlXPathContextPtr ctxt = NULL;
    virStorageVolDefPtr def = NULL;

1451
    if (!virXMLNodeNameEqual(root, "volume")) {
1452
        virReportError(VIR_ERR_XML_ERROR,
1453 1454 1455
                       _("unexpected root element <%s>, "
                         "expecting <volume>"),
                       root->name);
1456 1457 1458 1459 1460
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1461
        virReportOOMError();
1462 1463 1464 1465
        goto cleanup;
    }

    ctxt->node = root;
1466
    def = virStorageVolDefParseXML(pool, ctxt, flags);
1467
 cleanup:
1468 1469 1470 1471
    xmlXPathFreeContext(ctxt);
    return def;
}

1472

1473
static virStorageVolDefPtr
1474
virStorageVolDefParse(virStoragePoolDefPtr pool,
1475
                      const char *xmlStr,
1476 1477
                      const char *filename,
                      unsigned int flags)
1478
{
1479
    virStorageVolDefPtr ret = NULL;
J
Jiri Denemark 已提交
1480
    xmlDocPtr xml;
1481

1482
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_volume_definition)")))) {
1483
        ret = virStorageVolDefParseNode(pool, xml, xmlDocGetRootElement(xml), flags);
J
Jiri Denemark 已提交
1484
        xmlFreeDoc(xml);
1485 1486 1487 1488 1489
    }

    return ret;
}

1490

1491
virStorageVolDefPtr
1492
virStorageVolDefParseString(virStoragePoolDefPtr pool,
1493 1494
                            const char *xmlStr,
                            unsigned int flags)
1495
{
1496
    return virStorageVolDefParse(pool, xmlStr, NULL, flags);
1497 1498
}

1499

1500
virStorageVolDefPtr
1501
virStorageVolDefParseFile(virStoragePoolDefPtr pool,
1502 1503
                          const char *filename,
                          unsigned int flags)
1504
{
1505
    return virStorageVolDefParse(pool, NULL, filename, flags);
1506
}
1507

1508

1509 1510 1511 1512 1513 1514
static void
virStorageVolTimestampFormat(virBufferPtr buf, const char *name,
                             struct timespec *ts)
{
    if (ts->tv_nsec < 0)
        return;
1515
    virBufferAsprintf(buf, "<%s>%llu", name,
1516 1517 1518 1519 1520 1521
                      (unsigned long long) ts->tv_sec);
    if (ts->tv_nsec)
       virBufferAsprintf(buf, ".%09ld", ts->tv_nsec);
    virBufferAsprintf(buf, "</%s>\n", name);
}

1522

1523
static int
1524
virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
1525
                             virBufferPtr buf,
1526
                             virStorageSourcePtr def,
1527 1528
                             const char *type)
{
1529 1530
    virBufferAsprintf(buf, "<%s>\n", type);
    virBufferAdjustIndent(buf, 2);
1531

1532
    virBufferEscapeString(buf, "<path>%s</path>\n", def->path);
1533 1534 1535 1536

    if (options->formatToString) {
        const char *format = (options->formatToString)(def->format);
        if (!format) {
1537 1538 1539
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown volume format number %d"),
                           def->format);
1540 1541
            return -1;
        }
1542
        virBufferAsprintf(buf, "<format type='%s'/>\n", format);
1543 1544
    }

1545 1546 1547 1548 1549
    if (def->perms &&
        (def->perms->mode != (mode_t) -1 ||
         def->perms->uid != (uid_t) -1 ||
         def->perms->gid != (gid_t) -1 ||
         def->perms->label)) {
1550 1551
        virBufferAddLit(buf, "<permissions>\n");
        virBufferAdjustIndent(buf, 2);
1552

1553 1554 1555
        if (def->perms->mode != (mode_t) -1)
            virBufferAsprintf(buf, "<mode>0%o</mode>\n",
                              def->perms->mode);
1556 1557 1558 1559 1560 1561
        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);
1562

1563 1564
        virBufferEscapeString(buf, "<label>%s</label>\n",
                              def->perms->label);
1565

1566 1567 1568
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</permissions>\n");
    }
1569

1570
    if (def->timestamps) {
1571 1572
        virBufferAddLit(buf, "<timestamps>\n");
        virBufferAdjustIndent(buf, 2);
1573 1574 1575 1576
        virStorageVolTimestampFormat(buf, "atime", &def->timestamps->atime);
        virStorageVolTimestampFormat(buf, "mtime", &def->timestamps->mtime);
        virStorageVolTimestampFormat(buf, "ctime", &def->timestamps->ctime);
        virStorageVolTimestampFormat(buf, "btime", &def->timestamps->btime);
1577 1578
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</timestamps>\n");
1579 1580
    }

1581 1582
    if (def->encryption &&
        virStorageEncryptionFormat(buf, def->encryption) < 0)
1583
            return -1;
1584

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

1587
    if (def->features) {
1588
        size_t i;
1589 1590
        bool empty = virBitmapIsAllClear(def->features);

1591 1592 1593 1594 1595 1596
        if (empty) {
            virBufferAddLit(buf, "<features/>\n");
        } else {
            virBufferAddLit(buf, "<features>\n");
            virBufferAdjustIndent(buf, 2);
        }
1597 1598

        for (i = 0; i < VIR_STORAGE_FILE_FEATURE_LAST; i++) {
J
Ján Tomko 已提交
1599
            if (virBitmapIsBitSet(def->features, i))
1600
                virBufferAsprintf(buf, "<%s/>\n",
1601
                                  virStorageFileFeatureTypeToString(i));
1602
        }
1603 1604 1605 1606
        if (!empty) {
            virBufferAdjustIndent(buf, -2);
            virBufferAddLit(buf, "</features>\n");
        }
1607 1608
    }

1609 1610
    virBufferAdjustIndent(buf, -2);
    virBufferAsprintf(buf, "</%s>\n", type);
1611 1612
    return 0;
}
1613

1614

1615
char *
1616
virStorageVolDefFormat(virStoragePoolDefPtr pool,
1617 1618
                       virStorageVolDefPtr def)
{
1619
    virStorageVolOptionsPtr options;
1620
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1621

1622
    options = virStorageVolOptionsForPoolType(pool->type);
1623 1624 1625
    if (options == NULL)
        return NULL;

1626 1627
    virBufferAsprintf(&buf, "<volume type='%s'>\n",
                      virStorageVolTypeToString(def->type));
1628 1629 1630 1631 1632 1633
    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);
1634 1635

    if (def->source.nextent) {
1636
        size_t i;
1637
        const char *thispath = NULL;
1638
        for (i = 0; i < def->source.nextent; i++) {
1639 1640 1641
            if (thispath == NULL ||
                STRNEQ(thispath, def->source.extents[i].path)) {
                if (thispath != NULL)
1642
                    virBufferAddLit(&buf, "</device>\n");
1643

1644
                virBufferEscapeString(&buf, "<device path='%s'>\n",
1645
                                      def->source.extents[i].path);
1646 1647
            }

1648 1649
            virBufferAdjustIndent(&buf, 2);
            virBufferAsprintf(&buf, "<extent start='%llu' end='%llu'/>\n",
1650 1651
                              def->source.extents[i].start,
                              def->source.extents[i].end);
1652
            virBufferAdjustIndent(&buf, -2);
1653 1654 1655
            thispath = def->source.extents[i].path;
        }
        if (thispath != NULL)
1656
            virBufferAddLit(&buf, "</device>\n");
1657 1658
    }

1659 1660 1661 1662
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</source>\n");

    virBufferAsprintf(&buf, "<capacity unit='bytes'>%llu</capacity>\n",
1663
                      def->target.capacity);
1664
    virBufferAsprintf(&buf, "<allocation unit='bytes'>%llu</allocation>\n",
1665
                      def->target.allocation);
1666 1667 1668 1669 1670 1671
    /* 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);
1672

1673
    if (virStorageVolTargetDefFormat(options, &buf,
1674 1675
                                     &def->target, "target") < 0)
        goto cleanup;
1676

1677
    if (virStorageSourceHasBacking(&def->target) &&
1678
        virStorageVolTargetDefFormat(options, &buf,
1679 1680
                                     def->target.backingStore,
                                     "backingStore") < 0)
1681
        goto cleanup;
1682

1683
    virBufferAdjustIndent(&buf, -2);
E
Eric Blake 已提交
1684
    virBufferAddLit(&buf, "</volume>\n");
1685

1686 1687
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
1688

1689
    return virBufferContentAndReset(&buf);
1690

1691
 cleanup:
1692
    virBufferFreeAndReset(&buf);
1693 1694 1695 1696
    return NULL;
}


1697 1698 1699 1700
static int
virStoragePoolSaveXML(const char *path,
                      virStoragePoolDefPtr def,
                      const char *xml)
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
{
    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;
}
1712 1713 1714 1715 1716 1717 1718


int
virStoragePoolSaveState(const char *stateFile,
                        virStoragePoolDefPtr def)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1719
    VIR_AUTOFREE(char *) xml = NULL;
1720 1721 1722 1723 1724

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

    if (virStoragePoolDefFormatBuf(&buf, def) < 0)
1725
        return -1;
1726 1727 1728 1729 1730

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

    if (virBufferCheckError(&buf) < 0)
1731
        return -1;
1732 1733

    if (!(xml = virBufferContentAndReset(&buf)))
1734
        return -1;
1735 1736

    if (virStoragePoolSaveXML(stateFile, def, xml))
1737
        return -1;
1738

1739
    return 0;
1740
}
1741 1742


1743
int
1744
virStoragePoolSaveConfig(const char *configFile,
1745 1746
                         virStoragePoolDefPtr def)
{
1747
    VIR_AUTOFREE(char *) xml = NULL;
1748

1749 1750 1751 1752 1753 1754
    if (!(xml = virStoragePoolDefFormat(def))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to generate XML"));
        return -1;
    }

1755
    return virStoragePoolSaveXML(configFile, def, xml);
1756 1757
}

1758

1759
virStoragePoolSourcePtr
1760
virStoragePoolSourceListNewSource(virStoragePoolSourceListPtr list)
1761 1762 1763
{
    virStoragePoolSourcePtr source;

1764
    if (VIR_REALLOC_N(list->sources, list->nsources + 1) < 0)
1765 1766 1767 1768 1769 1770 1771 1772
        return NULL;

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

    return source;
}

1773

1774 1775
char *
virStoragePoolSourceListFormat(virStoragePoolSourceListPtr def)
1776
{
1777
    virStoragePoolOptionsPtr options;
1778
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1779
    const char *type;
1780
    size_t i;
1781

1782
    options = virStoragePoolOptionsForPoolType(def->type);
1783 1784 1785
    if (options == NULL)
        return NULL;

1786
    type = virStoragePoolTypeToString(def->type);
1787
    if (!type) {
1788 1789
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unexpected pool type"));
1790 1791 1792 1793
        goto cleanup;
    }

    virBufferAddLit(&buf, "<sources>\n");
1794
    virBufferAdjustIndent(&buf, 2);
1795

1796
    for (i = 0; i < def->nsources; i++)
1797
        virStoragePoolSourceFormat(&buf, options, &def->sources[i]);
1798

1799
    virBufferAdjustIndent(&buf, -2);
1800 1801
    virBufferAddLit(&buf, "</sources>\n");

1802 1803
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;
1804 1805

    return virBufferContentAndReset(&buf);
1806

1807
 cleanup:
1808
    virBufferFreeAndReset(&buf);
1809
    return NULL;
1810
}