storage_conf.c 52.1 KB
Newer Older
1 2 3
/*
 * storage_conf.c: config handling for storage driver
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2006-2012 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

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

36
#include "virterror_internal.h"
37
#include "datatypes.h"
38
#include "storage_conf.h"
39
#include "storage_file.h"
40

41 42 43 44
#include "xml.h"
#include "uuid.h"
#include "buf.h"
#include "util.h"
45
#include "memory.h"
E
Eric Blake 已提交
46
#include "virfile.h"
47

48 49
#define VIR_FROM_THIS VIR_FROM_STORAGE

50

51 52 53 54
VIR_ENUM_IMPL(virStoragePool,
              VIR_STORAGE_POOL_LAST,
              "dir", "fs", "netfs",
              "logical", "disk", "iscsi",
D
Dave Allan 已提交
55
              "scsi", "mpath")
56 57 58 59 60

VIR_ENUM_IMPL(virStoragePoolFormatFileSystem,
              VIR_STORAGE_POOL_FS_LAST,
              "auto", "ext2", "ext3",
              "ext4", "ufs", "iso9660", "udf",
J
Jim Fehlig 已提交
61
              "gfs", "gfs2", "vfat", "hfs+", "xfs", "ocfs2")
62 63 64

VIR_ENUM_IMPL(virStoragePoolFormatFileSystemNet,
              VIR_STORAGE_POOL_NETFS_LAST,
65
              "auto", "nfs", "glusterfs", "cifs")
66 67 68 69

VIR_ENUM_IMPL(virStoragePoolFormatDisk,
              VIR_STORAGE_POOL_DISK_LAST,
              "unknown", "dos", "dvh", "gpt",
70
              "mac", "bsd", "pc98", "sun", "lvm2")
71 72 73

VIR_ENUM_IMPL(virStoragePoolFormatLogical,
              VIR_STORAGE_POOL_LOGICAL_LAST,
74
              "unknown", "lvm2")
75 76 77 78 79 80 81


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

84 85
VIR_ENUM_IMPL(virStoragePartedFsType,
              VIR_STORAGE_PARTED_FS_TYPE_LAST,
86
              "ext2", "ext2", "fat16",
87 88 89
              "fat32", "linux-swap",
              "ext2", "ext2",
              "extended")
90 91 92 93 94 95 96 97 98 99

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 {
100
    int defaultFormat;
101 102 103 104 105 106
    virStorageVolFormatToString formatToString;
    virStorageVolFormatFromString formatFromString;
};

/* Flags to indicate mandatory components in the pool source */
enum {
D
David Allan 已提交
107 108 109 110 111 112
    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),
113 114 115 116 117 118 119
};



typedef struct _virStoragePoolOptions virStoragePoolOptions;
typedef virStoragePoolOptions *virStoragePoolOptionsPtr;
struct _virStoragePoolOptions {
E
Eric Blake 已提交
120
    unsigned int flags;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    int defaultFormat;
    virStoragePoolFormatToString formatToString;
    virStoragePoolFormatFromString formatFromString;
};

typedef struct _virStoragePoolTypeInfo virStoragePoolTypeInfo;
typedef virStoragePoolTypeInfo *virStoragePoolTypeInfoPtr;

struct _virStoragePoolTypeInfo {
    int poolType;
    virStoragePoolOptions poolOptions;
    virStorageVolOptions volOptions;
};

static virStoragePoolTypeInfo poolTypeInfo[] = {
    { .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,
        },
    },
    { .poolType = VIR_STORAGE_POOL_DIR,
      .volOptions = {
147 148 149
            .defaultFormat = VIR_STORAGE_FILE_RAW,
            .formatFromString = virStorageFileFormatTypeFromString,
            .formatToString = virStorageFileFormatTypeToString,
150 151 152 153 154
        },
    },
    { .poolType = VIR_STORAGE_POOL_FS,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_DEVICE),
155
            .defaultFormat = VIR_STORAGE_POOL_FS_AUTO,
156 157 158 159
            .formatFromString = virStoragePoolFormatFileSystemTypeFromString,
            .formatToString = virStoragePoolFormatFileSystemTypeToString,
        },
      .volOptions = {
160 161 162
            .defaultFormat = VIR_STORAGE_FILE_RAW,
            .formatFromString = virStorageFileFormatTypeFromString,
            .formatToString = virStorageFileFormatTypeToString,
163 164 165 166 167 168
        },
    },
    { .poolType = VIR_STORAGE_POOL_NETFS,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
                      VIR_STORAGE_POOL_SOURCE_DIR),
169
            .defaultFormat = VIR_STORAGE_POOL_NETFS_AUTO,
170 171 172 173
            .formatFromString = virStoragePoolFormatFileSystemNetTypeFromString,
            .formatToString = virStoragePoolFormatFileSystemNetTypeToString,
        },
      .volOptions = {
174 175 176
            .defaultFormat = VIR_STORAGE_FILE_RAW,
            .formatFromString = virStorageFileFormatTypeFromString,
            .formatToString = virStorageFileFormatTypeToString,
177 178 179 180 181
        },
    },
    { .poolType = VIR_STORAGE_POOL_ISCSI,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
D
David Allan 已提交
182 183
                      VIR_STORAGE_POOL_SOURCE_DEVICE |
                      VIR_STORAGE_POOL_SOURCE_INITIATOR_IQN),
184 185 186 187 188
        },
      .volOptions = {
            .formatToString = virStoragePoolFormatDiskTypeToString,
        }
    },
189 190 191 192 193 194 195 196
    { .poolType = VIR_STORAGE_POOL_SCSI,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_ADAPTER),
        },
      .volOptions = {
            .formatToString = virStoragePoolFormatDiskTypeToString,
        }
    },
D
Dave Allan 已提交
197 198 199 200 201
    { .poolType = VIR_STORAGE_POOL_MPATH,
      .volOptions = {
            .formatToString = virStoragePoolFormatDiskTypeToString,
        }
    },
202 203 204 205 206 207 208 209
    { .poolType = VIR_STORAGE_POOL_DISK,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_DEVICE),
            .defaultFormat = VIR_STORAGE_POOL_DISK_UNKNOWN,
            .formatFromString = virStoragePoolFormatDiskTypeFromString,
            .formatToString = virStoragePoolFormatDiskTypeToString,
        },
      .volOptions = {
210
            .defaultFormat = VIR_STORAGE_VOL_DISK_NONE,
211 212 213 214 215 216 217 218 219 220 221 222 223 224
            .formatFromString = virStorageVolFormatDiskTypeFromString,
            .formatToString = virStorageVolFormatDiskTypeToString,
        },
    }
};


static virStoragePoolTypeInfoPtr
virStoragePoolTypeInfoLookup(int type) {
    unsigned int i;
    for (i = 0; i < ARRAY_CARDINALITY(poolTypeInfo) ; i++)
        if (poolTypeInfo[i].poolType == type)
            return &poolTypeInfo[i];

225
    virStorageReportError(VIR_ERR_INTERNAL_ERROR,
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                          _("missing backend for pool type %d"), type);
    return NULL;
}

static virStoragePoolOptionsPtr
virStoragePoolOptionsForPoolType(int type) {
    virStoragePoolTypeInfoPtr backend = virStoragePoolTypeInfoLookup(type);
    if (backend == NULL)
        return NULL;
    return &backend->poolOptions;
}

static virStorageVolOptionsPtr
virStorageVolOptionsForPoolType(int type) {
    virStoragePoolTypeInfoPtr backend = virStoragePoolTypeInfoLookup(type);
    if (backend == NULL)
        return NULL;
    return &backend->volOptions;
}


247 248 249
void
virStorageVolDefFree(virStorageVolDefPtr def) {
    int i;
250 251 252 253

    if (!def)
        return;

254 255
    VIR_FREE(def->name);
    VIR_FREE(def->key);
256 257

    for (i = 0 ; i < def->source.nextent ; i++) {
258
        VIR_FREE(def->source.extents[i].path);
259
    }
260
    VIR_FREE(def->source.extents);
261

262 263
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
264
    virStorageEncryptionFree(def->target.encryption);
265 266
    VIR_FREE(def->backingStore.path);
    VIR_FREE(def->backingStore.perms.label);
267
    virStorageEncryptionFree(def->backingStore.encryption);
268
    VIR_FREE(def);
269 270 271
}

void
272 273
virStoragePoolSourceClear(virStoragePoolSourcePtr source)
{
274 275
    int i;

276
    if (!source)
277 278
        return;

279 280 281 282 283
    for (i = 0 ; i < source->nhost ; i++) {
        VIR_FREE(source->hosts[i].name);
    }
    VIR_FREE(source->hosts);

284 285 286
    for (i = 0 ; i < source->ndevice ; i++) {
        VIR_FREE(source->devices[i].freeExtents);
        VIR_FREE(source->devices[i].path);
287
    }
288 289 290
    VIR_FREE(source->devices);
    VIR_FREE(source->dir);
    VIR_FREE(source->name);
291
    VIR_FREE(source->adapter);
D
David Allan 已提交
292
    VIR_FREE(source->initiator.iqn);
293 294
    VIR_FREE(source->vendor);
    VIR_FREE(source->product);
295

296 297 298
    if (source->authType == VIR_STORAGE_POOL_AUTH_CHAP) {
        VIR_FREE(source->auth.chap.login);
        VIR_FREE(source->auth.chap.passwd);
299
    }
300 301
}

302 303 304 305 306 307 308
void
virStoragePoolSourceFree(virStoragePoolSourcePtr source)
{
    virStoragePoolSourceClear(source);
    VIR_FREE(source);
}

309 310 311 312 313 314 315
void
virStoragePoolDefFree(virStoragePoolDefPtr def) {
    if (!def)
        return;

    VIR_FREE(def->name);

316
    virStoragePoolSourceClear(&def->source);
317

318 319 320
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
    VIR_FREE(def);
321 322 323 324 325
}


void
virStoragePoolObjFree(virStoragePoolObjPtr obj) {
326 327 328
    if (!obj)
        return;

329 330
    virStoragePoolObjClearVols(obj);

331 332
    virStoragePoolDefFree(obj->def);
    virStoragePoolDefFree(obj->newDef);
333

334 335
    VIR_FREE(obj->configFile);
    VIR_FREE(obj->autostartLink);
336 337 338

    virMutexDestroy(&obj->lock);

339
    VIR_FREE(obj);
340 341
}

342 343 344 345 346 347 348 349 350
void virStoragePoolObjListFree(virStoragePoolObjListPtr pools)
{
    unsigned int i;
    for (i = 0 ; i < pools->count ; i++)
        virStoragePoolObjFree(pools->objs[i]);
    VIR_FREE(pools->objs);
    pools->count = 0;
}

351
void
352
virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
353 354
                        virStoragePoolObjPtr pool)
{
355
    unsigned int i;
356

357 358
    virStoragePoolObjUnlock(pool);

359
    for (i = 0 ; i < pools->count ; i++) {
360
        virStoragePoolObjLock(pools->objs[i]);
361
        if (pools->objs[i] == pool) {
362
            virStoragePoolObjUnlock(pools->objs[i]);
363
            virStoragePoolObjFree(pools->objs[i]);
364

365 366 367
            if (i < (pools->count - 1))
                memmove(pools->objs + i, pools->objs + i + 1,
                        sizeof(*(pools->objs)) * (pools->count - (i + 1)));
368

369 370 371 372
            if (VIR_REALLOC_N(pools->objs, pools->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            pools->count--;
373

374 375
            break;
        }
376
        virStoragePoolObjUnlock(pools->objs[i]);
377
    }
378 379 380 381
}


static int
382
virStoragePoolDefParseAuthChap(xmlXPathContextPtr ctxt,
383
                               virStoragePoolAuthChapPtr auth) {
384
    auth->login = virXPathString("string(./auth/@login)", ctxt);
385
    if (auth->login == NULL) {
386
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
387
                              "%s", _("missing auth host attribute"));
388 389 390
        return -1;
    }

391
    auth->passwd = virXPathString("string(./auth/@passwd)", ctxt);
392
    if (auth->passwd == NULL) {
393
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
394
                              "%s", _("missing auth passwd attribute"));
395 396 397 398 399 400
        return -1;
    }

    return 0;
}

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

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

    if ((options = virStoragePoolOptionsForPoolType(pool_type)) == NULL) {
        goto cleanup;
    }

421
    source->name = virXPathString("string(./name)", ctxt);
422 423

    if (options->formatFromString) {
424
        char *format = virXPathString("string(./format/@type)", ctxt);
425 426 427 428 429 430
        if (format == NULL)
            source->format = options->defaultFormat;
        else
            source->format = options->formatFromString(format);

        if (source->format < 0) {
431
            virStorageReportError(VIR_ERR_XML_ERROR,
432 433 434 435 436 437 438
                                  _("unknown pool format type %s"), format);
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

439 440 441 442 443
    source->nhost = virXPathNodeSet("./host", ctxt, &nodeset);

    if (source->nhost) {
        if (VIR_ALLOC_N(source->hosts, source->nhost) < 0) {
            virReportOOMError();
444 445 446
            goto cleanup;
        }

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
        for (i = 0 ; i < source->nhost ; i++) {
            name = virXMLPropString(nodeset[i], "name");
            if (name == NULL) {
                virStorageReportError(VIR_ERR_XML_ERROR,
                        "%s", _("missing storage pool host name"));
                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) {
                    virStorageReportError(VIR_ERR_XML_ERROR,
                                          _("Invalid port number: %s"),
                                          port);
                    goto cleanup;
                }
            }
        }
    }
467

468
    VIR_FREE(nodeset);
469
    source->initiator.iqn = virXPathString("string(./initiator/iqn/@name)", ctxt);
470

471
    nsource = virXPathNodeSet("./device", ctxt, &nodeset);
472 473 474
    if (nsource < 0)
        goto cleanup;

475 476 477
    if (nsource > 0) {
        if (VIR_ALLOC_N(source->devices, nsource) < 0) {
            VIR_FREE(nodeset);
478
            virReportOOMError();
479 480 481 482
            goto cleanup;
        }

        for (i = 0 ; i < nsource ; i++) {
483
            char *path = virXMLPropString(nodeset[i], "path");
484 485
            if (path == NULL) {
                VIR_FREE(nodeset);
486
                virStorageReportError(VIR_ERR_XML_ERROR,
487 488 489
                        "%s", _("missing storage pool source device path"));
                goto cleanup;
            }
490
            source->devices[i].path = path;
491 492 493 494
        }
        source->ndevice = nsource;
    }

495 496
    source->dir = virXPathString("string(./dir/@path)", ctxt);
    source->adapter = virXPathString("string(./adapter/@name)", ctxt);
497

498
    authType = virXPathString("string(./auth/@type)", ctxt);
499 500 501 502 503 504
    if (authType == NULL) {
        source->authType = VIR_STORAGE_POOL_AUTH_NONE;
    } else {
        if (STREQ(authType, "chap")) {
            source->authType = VIR_STORAGE_POOL_AUTH_CHAP;
        } else {
505
            virStorageReportError(VIR_ERR_XML_ERROR,
506 507 508 509 510 511 512
                                  _("unknown auth type '%s'"),
                                  (const char *)authType);
            goto cleanup;
        }
    }

    if (source->authType == VIR_STORAGE_POOL_AUTH_CHAP) {
513
        if (virStoragePoolDefParseAuthChap(ctxt, &source->auth.chap) < 0)
514 515 516
            goto cleanup;
    }

517 518 519
    source->vendor = virXPathString("string(./vendor/@name)", ctxt);
    source->product = virXPathString("string(./product/@name)", ctxt);

520 521 522 523
    ret = 0;
cleanup:
    ctxt->node = relnode;

524
    VIR_FREE(port);
525 526 527 528
    VIR_FREE(authType);
    VIR_FREE(nodeset);
    return ret;
}
529

530
virStoragePoolSourcePtr
531
virStoragePoolDefParseSourceString(const char *srcSpec,
532 533 534 535 536 537 538
                                   int pool_type)
{
    xmlDocPtr doc = NULL;
    xmlNodePtr node = NULL;
    xmlXPathContextPtr xpath_ctxt = NULL;
    virStoragePoolSourcePtr def = NULL, ret = NULL;

539
    if (!(doc = virXMLParseStringCtxt(srcSpec, _("(storage_source_specification)"), &xpath_ctxt))) {
540 541 542 543
        goto cleanup;
    }

    if (VIR_ALLOC(def) < 0) {
544
        virReportOOMError();
545 546 547
        goto cleanup;
    }

548
    node = virXPathNode("/source", xpath_ctxt);
549
    if (!node) {
550
        virStorageReportError(VIR_ERR_XML_ERROR,
551 552 553 554
                              "%s", _("root element was not source"));
        goto cleanup;
    }

555
    if (virStoragePoolDefParseSource(xpath_ctxt, def, pool_type,
556 557 558 559 560 561
                                     node) < 0)
        goto cleanup;

    ret = def;
    def = NULL;
cleanup:
562
    virStoragePoolSourceFree(def);
563 564 565 566 567
    xmlFreeDoc(doc);
    xmlXPathFreeContext(xpath_ctxt);

    return ret;
}
568
static int
569
virStorageDefParsePerms(xmlXPathContextPtr ctxt,
570 571 572
                        virStoragePermsPtr perms,
                        const char *permxpath,
                        int defaultmode) {
573 574
    char *mode;
    long v;
575 576 577
    int ret = -1;
    xmlNodePtr relnode;
    xmlNodePtr node;
578

579
    node = virXPathNode(permxpath, ctxt);
580 581 582
    if (node == NULL) {
        /* Set default values if there is not <permissions> element */
        perms->mode = defaultmode;
583 584
        perms->uid = -1;
        perms->gid = -1;
585 586 587 588 589 590 591
        perms->label = NULL;
        return 0;
    }

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

592
    mode = virXPathString("string(./mode)", ctxt);
593
    if (!mode) {
594
        perms->mode = defaultmode;
595
    } else {
596 597 598
        int tmp;

        if (virStrToLong_i(mode, NULL, 8, &tmp) < 0 || (tmp & ~0777)) {
599
            VIR_FREE(mode);
600
            virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
601
                                  "%s", _("malformed octal mode"));
602
            goto error;
603
        }
604
        perms->mode = tmp;
605
        VIR_FREE(mode);
606 607
    }

608
    if (virXPathNode("./owner", ctxt) == NULL) {
609
        perms->uid = -1;
610
    } else {
611
        if (virXPathLong("number(./owner)", ctxt, &v) < 0) {
612
            virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
613
                                  "%s", _("malformed owner element"));
614
            goto error;
615 616 617 618
        }
        perms->uid = (int)v;
    }

619
    if (virXPathNode("./group", ctxt) == NULL) {
620
        perms->gid = -1;
621
    } else {
622
        if (virXPathLong("number(./group)", ctxt, &v) < 0) {
623
            virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
624
                                  "%s", _("malformed group element"));
625
            goto error;
626 627 628 629 630
        }
        perms->gid = (int)v;
    }

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

633 634 635 636
    ret = 0;
error:
    ctxt->node = relnode;
    return ret;
637 638 639
}

static virStoragePoolDefPtr
640
virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) {
641
    virStoragePoolOptionsPtr options;
642
    virStoragePoolDefPtr ret;
643
    xmlNodePtr source_node;
644
    char *type = NULL;
645
    char *uuid = NULL;
646
    char *tmppath;
647

648
    if (VIR_ALLOC(ret) < 0) {
649
        virReportOOMError();
650
        return NULL;
651
    }
652

653
    type = virXPathString("string(./@type)", ctxt);
654
    if ((ret->type = virStoragePoolTypeFromString((const char *)type)) < 0) {
655
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
656
                              _("unknown storage pool type %s"), (const char*)type);
657
        goto cleanup;
658 659
    }

660 661 662
    xmlFree(type);
    type = NULL;

663
    if ((options = virStoragePoolOptionsForPoolType(ret->type)) == NULL) {
664 665 666
        goto cleanup;
    }

667
    source_node = virXPathNode("./source", ctxt);
668
    if (source_node) {
669
        if (virStoragePoolDefParseSource(ctxt, &ret->source, ret->type,
670 671 672 673
                                         source_node) < 0)
            goto cleanup;
    }

674
    ret->name = virXPathString("string(./name)", ctxt);
675
    if (ret->name == NULL &&
676
        options->flags & VIR_STORAGE_POOL_SOURCE_NAME)
677
        ret->name = ret->source.name;
678
    if (ret->name == NULL) {
679
        virStorageReportError(VIR_ERR_XML_ERROR,
680
                              "%s", _("missing pool source name element"));
681 682 683
        goto cleanup;
    }

684
    uuid = virXPathString("string(./uuid)", ctxt);
685 686
    if (uuid == NULL) {
        if (virUUIDGenerate(ret->uuid) < 0) {
687
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
688
                                  "%s", _("unable to generate uuid"));
689 690 691 692
            goto cleanup;
        }
    } else {
        if (virUUIDParse(uuid, ret->uuid) < 0) {
693
            virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
694
                                  "%s", _("malformed uuid element"));
695 696
            goto cleanup;
        }
697
        VIR_FREE(uuid);
698 699
    }

700
    if (options->flags & VIR_STORAGE_POOL_SOURCE_HOST) {
701
        if (!ret->source.nhost) {
702
            virStorageReportError(VIR_ERR_XML_ERROR,
703 704
                                  "%s",
                                  _("missing storage pool source host name"));
705 706 707 708
            goto cleanup;
        }
    }

709
    if (options->flags & VIR_STORAGE_POOL_SOURCE_DIR) {
710
        if (!ret->source.dir) {
711
            virStorageReportError(VIR_ERR_XML_ERROR,
712
                                  "%s", _("missing storage pool source path"));
713 714 715
            goto cleanup;
        }
    }
716
    if (options->flags & VIR_STORAGE_POOL_SOURCE_NAME) {
717 718 719
        if (ret->source.name == NULL) {
            /* source name defaults to pool name */
            ret->source.name = strdup(ret->name);
720
            if (ret->source.name == NULL) {
721
                virReportOOMError();
722 723
                goto cleanup;
            }
724 725
        }
    }
726

727
    if (options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) {
728
        if (!ret->source.adapter) {
729
            virStorageReportError(VIR_ERR_XML_ERROR,
730
                                  "%s", _("missing storage pool source adapter name"));
731 732 733
            goto cleanup;
        }
    }
734

735 736 737 738 739 740 741 742 743
    /* If DEVICE is the only source type, then its required */
    if (options->flags == VIR_STORAGE_POOL_SOURCE_DEVICE) {
        if (!ret->source.ndevice) {
            virStorageReportError(VIR_ERR_XML_ERROR,
                                  "%s", _("missing storage pool source device name"));
            goto cleanup;
        }
    }

744
    if ((tmppath = virXPathString("string(./target/path)", ctxt)) == NULL) {
745
        virStorageReportError(VIR_ERR_XML_ERROR,
746
                              "%s", _("missing storage pool target path"));
747 748
        goto cleanup;
    }
749 750 751 752 753
    ret->target.path = virFileSanitizePath(tmppath);
    VIR_FREE(tmppath);
    if (!ret->target.path)
        goto cleanup;

754

755
    if (virStorageDefParsePerms(ctxt, &ret->target.perms,
756
                                "./target/permissions", 0700) < 0)
757 758 759 760 761
        goto cleanup;

    return ret;

 cleanup:
762
    VIR_FREE(uuid);
763
    xmlFree(type);
764 765 766 767 768
    virStoragePoolDefFree(ret);
    return NULL;
}

virStoragePoolDefPtr
769
virStoragePoolDefParseNode(xmlDocPtr xml,
770 771 772 773 774
                           xmlNodePtr root) {
    xmlXPathContextPtr ctxt = NULL;
    virStoragePoolDefPtr def = NULL;

    if (STRNEQ((const char *)root->name, "pool")) {
775 776
        virStorageReportError(VIR_ERR_XML_ERROR,
                              "%s", _("unknown root element for storage pool"));
777 778 779 780 781
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
782
        virReportOOMError();
783 784 785 786
        goto cleanup;
    }

    ctxt->node = root;
787
    def = virStoragePoolDefParseXML(ctxt);
788 789 790 791 792 793
cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

static virStoragePoolDefPtr
794
virStoragePoolDefParse(const char *xmlStr,
795 796
                       const char *filename) {
    virStoragePoolDefPtr ret = NULL;
J
Jiri Denemark 已提交
797
    xmlDocPtr xml;
798

799
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_pool_definition)")))) {
J
Jiri Denemark 已提交
800 801
        ret = virStoragePoolDefParseNode(xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
802 803
    }

804 805 806
    return ret;
}

807
virStoragePoolDefPtr
808
virStoragePoolDefParseString(const char *xmlStr)
809
{
810
    return virStoragePoolDefParse(xmlStr, NULL);
811 812 813
}

virStoragePoolDefPtr
814
virStoragePoolDefParseFile(const char *filename)
815
{
816
    return virStoragePoolDefParse(NULL, filename);
817 818
}

819
static int
820
virStoragePoolSourceFormat(virBufferPtr buf,
821
                           virStoragePoolOptionsPtr options,
822 823 824 825 826
                           virStoragePoolSourcePtr src)
{
    int i, j;

    virBufferAddLit(buf,"  <source>\n");
827 828
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_HOST) && src->nhost) {
        for (i = 0; i < src->nhost; i++) {
E
Eric Blake 已提交
829
            virBufferAsprintf(buf, "    <host name='%s'", src->hosts[i].name);
830 831 832 833
            if (src->hosts[i].port)
                virBufferAsprintf(buf, " port='%d'", src->hosts[i].port);
            virBufferAddLit(buf, "/>\n");
        }
834
    }
835

836
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_DEVICE) &&
837 838 839
        src->ndevice) {
        for (i = 0 ; i < src->ndevice ; i++) {
            if (src->devices[i].nfreeExtent) {
840
                virBufferAsprintf(buf,"    <device path='%s'>\n",
841 842
                                  src->devices[i].path);
                for (j = 0 ; j < src->devices[i].nfreeExtent ; j++) {
843
                    virBufferAsprintf(buf, "    <freeExtent start='%llu' end='%llu'/>\n",
844 845 846 847 848 849
                                      src->devices[i].freeExtents[j].start,
                                      src->devices[i].freeExtents[j].end);
                }
                virBufferAddLit(buf,"    </device>\n");
            }
            else
850
                virBufferAsprintf(buf, "    <device path='%s'/>\n",
851 852 853
                                  src->devices[i].path);
        }
    }
854
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_DIR) &&
855
        src->dir)
856
        virBufferAsprintf(buf,"    <dir path='%s'/>\n", src->dir);
857
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
858
        src->adapter)
859
        virBufferAsprintf(buf,"    <adapter name='%s'/>\n", src->adapter);
860
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_NAME) &&
861
        src->name)
862
        virBufferAsprintf(buf,"    <name>%s</name>\n", src->name);
863

D
David Allan 已提交
864 865 866 867 868 869 870
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_INITIATOR_IQN) &&
        src->initiator.iqn) {
        virBufferAddLit(buf,"    <initiator>\n");
        virBufferEscapeString(buf,"      <iqn name='%s'/>\n", src->initiator.iqn);
        virBufferAddLit(buf,"    </initiator>\n");
    }

871 872 873
    if (options->formatToString) {
        const char *format = (options->formatToString)(src->format);
        if (!format) {
874
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
875 876 877 878
                                  _("unknown pool format number %d"),
                                  src->format);
            return -1;
        }
879
        virBufferAsprintf(buf,"    <format type='%s'/>\n", format);
880 881 882 883
    }


    if (src->authType == VIR_STORAGE_POOL_AUTH_CHAP)
884
        virBufferAsprintf(buf,"    <auth type='chap' login='%s' passwd='%s'/>\n",
885 886
                          src->auth.chap.login,
                          src->auth.chap.passwd);
887 888 889 890 891 892 893 894 895

    if (src->vendor != NULL) {
        virBufferEscapeString(buf,"    <vendor name='%s'/>\n", src->vendor);
    }

    if (src->product != NULL) {
        virBufferEscapeString(buf,"    <product name='%s'/>\n", src->product);
    }

896 897 898 899 900
    virBufferAddLit(buf,"  </source>\n");

    return 0;
}

901 902

char *
903
virStoragePoolDefFormat(virStoragePoolDefPtr def) {
904
    virStoragePoolOptionsPtr options;
905
    virBuffer buf = VIR_BUFFER_INITIALIZER;
906 907 908
    const char *type;
    char uuid[VIR_UUID_STRING_BUFLEN];

909
    options = virStoragePoolOptionsForPoolType(def->type);
910 911 912
    if (options == NULL)
        return NULL;

913
    type = virStoragePoolTypeToString(def->type);
914
    if (!type) {
915
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
916
                              "%s", _("unexpected pool type"));
917 918
        goto cleanup;
    }
919 920
    virBufferAsprintf(&buf, "<pool type='%s'>\n", type);
    virBufferAsprintf(&buf,"  <name>%s</name>\n", def->name);
921 922

    virUUIDFormat(def->uuid, uuid);
923
    virBufferAsprintf(&buf,"  <uuid>%s</uuid>\n", uuid);
924

E
Eric Blake 已提交
925
    virBufferAsprintf(&buf,"  <capacity unit='bytes'>%llu</capacity>\n",
926
                      def->capacity);
E
Eric Blake 已提交
927
    virBufferAsprintf(&buf,"  <allocation unit='bytes'>%llu</allocation>\n",
928
                      def->allocation);
E
Eric Blake 已提交
929
    virBufferAsprintf(&buf,"  <available unit='bytes'>%llu</available>\n",
930
                      def->available);
931

932
    if (virStoragePoolSourceFormat(&buf, options, &def->source) < 0)
933
        goto cleanup;
934

935
    virBufferAddLit(&buf,"  <target>\n");
936

937
    if (def->target.path)
938
        virBufferAsprintf(&buf,"    <path>%s</path>\n", def->target.path);
939

940
    virBufferAddLit(&buf,"    <permissions>\n");
941
    virBufferAsprintf(&buf,"      <mode>0%o</mode>\n",
942
                      def->target.perms.mode);
E
Eric Blake 已提交
943 944 945 946
    virBufferAsprintf(&buf,"      <owner>%u</owner>\n",
                      (unsigned int) def->target.perms.uid);
    virBufferAsprintf(&buf,"      <group>%u</group>\n",
                      (unsigned int) def->target.perms.gid);
947

948
    if (def->target.perms.label)
949
        virBufferAsprintf(&buf,"      <label>%s</label>\n",
950
                          def->target.perms.label);
951

952 953 954
    virBufferAddLit(&buf,"    </permissions>\n");
    virBufferAddLit(&buf,"  </target>\n");
    virBufferAddLit(&buf,"</pool>\n");
955

956
    if (virBufferError(&buf))
957 958
        goto no_memory;

959
    return virBufferContentAndReset(&buf);
960 961

 no_memory:
962
    virReportOOMError();
963
 cleanup:
964
    virBufferFreeAndReset(&buf);
965 966 967 968 969
    return NULL;
}


static int
970
virStorageSize(const char *unit,
971
               const char *val,
972 973 974
               unsigned long long *ret)
{
    if (virStrToLong_ull(val, NULL, 10, ret) < 0) {
975
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
976
                              "%s", _("malformed capacity element"));
977 978
        return -1;
    }
979 980 981 982
    /* 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;
983 984 985 986 987

    return 0;
}

static virStorageVolDefPtr
988
virStorageVolDefParseXML(virStoragePoolDefPtr pool,
989
                         xmlXPathContextPtr ctxt) {
990
    virStorageVolDefPtr ret;
991
    virStorageVolOptionsPtr options;
992 993 994
    char *allocation = NULL;
    char *capacity = NULL;
    char *unit = NULL;
995
    xmlNodePtr node;
996

997
    options = virStorageVolOptionsForPoolType(pool->type);
998 999 1000
    if (options == NULL)
        return NULL;

1001
    if (VIR_ALLOC(ret) < 0) {
1002
        virReportOOMError();
1003
        return NULL;
1004
    }
1005

1006
    ret->name = virXPathString("string(./name)", ctxt);
1007
    if (ret->name == NULL) {
1008
        virStorageReportError(VIR_ERR_XML_ERROR,
1009
                              "%s", _("missing volume name element"));
1010 1011 1012
        goto cleanup;
    }

R
Richard W.M. Jones 已提交
1013
    /* Auto-generated so deliberately ignore */
1014
    /*ret->key = virXPathString("string(./key)", ctxt);*/
1015

1016 1017
    capacity = virXPathString("string(./capacity)", ctxt);
    unit = virXPathString("string(./capacity/@unit)", ctxt);
1018
    if (capacity == NULL) {
1019
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
1020
                              "%s", _("missing capacity element"));
1021 1022
        goto cleanup;
    }
1023
    if (virStorageSize(unit, capacity, &ret->capacity) < 0)
1024
        goto cleanup;
1025 1026
    VIR_FREE(capacity);
    VIR_FREE(unit);
1027

1028
    allocation = virXPathString("string(./allocation)", ctxt);
1029
    if (allocation) {
1030
        unit = virXPathString("string(./allocation/@unit)", ctxt);
1031
        if (virStorageSize(unit, allocation, &ret->allocation) < 0)
1032
            goto cleanup;
1033 1034
        VIR_FREE(allocation);
        VIR_FREE(unit);
1035 1036 1037 1038
    } else {
        ret->allocation = ret->capacity;
    }

1039
    ret->target.path = virXPathString("string(./target/path)", ctxt);
1040
    if (options->formatFromString) {
1041
        char *format = virXPathString("string(./target/format/@type)", ctxt);
1042 1043 1044 1045 1046 1047
        if (format == NULL)
            ret->target.format = options->defaultFormat;
        else
            ret->target.format = (options->formatFromString)(format);

        if (ret->target.format < 0) {
1048
            virStorageReportError(VIR_ERR_XML_ERROR,
1049
                                  _("unknown volume format type %s"), format);
1050
            VIR_FREE(format);
1051 1052
            goto cleanup;
        }
1053
        VIR_FREE(format);
1054 1055
    }

1056
    if (virStorageDefParsePerms(ctxt, &ret->target.perms,
1057
                                "./target/permissions", 0600) < 0)
1058 1059
        goto cleanup;

1060
    node = virXPathNode("./target/encryption", ctxt);
1061
    if (node != NULL) {
1062
        ret->target.encryption = virStorageEncryptionParseNode(ctxt->doc,
1063 1064 1065 1066 1067
                                                               node);
        if (ret->target.encryption == NULL)
            goto cleanup;
    }

1068 1069


1070
    ret->backingStore.path = virXPathString("string(./backingStore/path)", ctxt);
1071
    if (options->formatFromString) {
1072
        char *format = virXPathString("string(./backingStore/format/@type)", ctxt);
1073 1074 1075 1076 1077 1078
        if (format == NULL)
            ret->backingStore.format = options->defaultFormat;
        else
            ret->backingStore.format = (options->formatFromString)(format);

        if (ret->backingStore.format < 0) {
1079
            virStorageReportError(VIR_ERR_XML_ERROR,
1080 1081 1082 1083 1084 1085 1086
                                  _("unknown volume format type %s"), format);
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

1087
    if (virStorageDefParsePerms(ctxt, &ret->backingStore.perms,
1088
                                "./backingStore/permissions", 0600) < 0)
1089 1090
        goto cleanup;

1091 1092 1093
    return ret;

 cleanup:
1094 1095 1096
    VIR_FREE(allocation);
    VIR_FREE(capacity);
    VIR_FREE(unit);
1097 1098 1099 1100 1101
    virStorageVolDefFree(ret);
    return NULL;
}

virStorageVolDefPtr
1102
virStorageVolDefParseNode(virStoragePoolDefPtr pool,
1103 1104 1105 1106 1107 1108
                          xmlDocPtr xml,
                          xmlNodePtr root) {
    xmlXPathContextPtr ctxt = NULL;
    virStorageVolDefPtr def = NULL;

    if (STRNEQ((const char *)root->name, "volume")) {
1109
        virStorageReportError(VIR_ERR_XML_ERROR,
1110 1111 1112 1113 1114 1115
                          "%s", _("unknown root element for storage vol"));
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1116
        virReportOOMError();
1117 1118 1119 1120
        goto cleanup;
    }

    ctxt->node = root;
1121
    def = virStorageVolDefParseXML(pool, ctxt);
1122 1123 1124 1125 1126 1127
cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

static virStorageVolDefPtr
1128
virStorageVolDefParse(virStoragePoolDefPtr pool,
1129 1130 1131
                      const char *xmlStr,
                      const char *filename) {
    virStorageVolDefPtr ret = NULL;
J
Jiri Denemark 已提交
1132
    xmlDocPtr xml;
1133

1134
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_volume_definition)")))) {
J
Jiri Denemark 已提交
1135 1136
        ret = virStorageVolDefParseNode(pool, xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
1137 1138 1139 1140 1141
    }

    return ret;
}

1142
virStorageVolDefPtr
1143
virStorageVolDefParseString(virStoragePoolDefPtr pool,
1144 1145
                            const char *xmlStr)
{
1146
    return virStorageVolDefParse(pool, xmlStr, NULL);
1147 1148 1149
}

virStorageVolDefPtr
1150
virStorageVolDefParseFile(virStoragePoolDefPtr pool,
1151 1152
                          const char *filename)
{
1153
    return virStorageVolDefParse(pool, NULL, filename);
1154
}
1155

1156
static int
1157
virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
1158 1159 1160
                             virBufferPtr buf,
                             virStorageVolTargetPtr def,
                             const char *type) {
1161
    virBufferAsprintf(buf, "  <%s>\n", type);
1162 1163

    if (def->path)
1164
        virBufferAsprintf(buf,"    <path>%s</path>\n", def->path);
1165 1166 1167 1168

    if (options->formatToString) {
        const char *format = (options->formatToString)(def->format);
        if (!format) {
1169
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1170 1171 1172 1173
                                  _("unknown volume format number %d"),
                                  def->format);
            return -1;
        }
1174
        virBufferAsprintf(buf,"    <format type='%s'/>\n", format);
1175 1176 1177
    }

    virBufferAddLit(buf,"    <permissions>\n");
1178
    virBufferAsprintf(buf,"      <mode>0%o</mode>\n",
1179
                      def->perms.mode);
E
Eric Blake 已提交
1180 1181 1182 1183
    virBufferAsprintf(buf,"      <owner>%u</owner>\n",
                      (unsigned int) def->perms.uid);
    virBufferAsprintf(buf,"      <group>%u</group>\n",
                      (unsigned int) def->perms.gid);
1184 1185 1186


    if (def->perms.label)
1187
        virBufferAsprintf(buf,"      <label>%s</label>\n",
1188 1189 1190 1191
                          def->perms.label);

    virBufferAddLit(buf,"    </permissions>\n");

1192 1193 1194 1195 1196 1197
    if (def->encryption) {
        virBufferAdjustIndent(buf, 4);
        if (virStorageEncryptionFormat(buf, def->encryption) < 0)
            return -1;
        virBufferAdjustIndent(buf, -4);
    }
1198

1199
    virBufferAsprintf(buf, "  </%s>\n", type);
1200 1201 1202

    return 0;
}
1203 1204

char *
1205
virStorageVolDefFormat(virStoragePoolDefPtr pool,
1206
                       virStorageVolDefPtr def) {
1207
    virStorageVolOptionsPtr options;
1208
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1209

1210
    options = virStorageVolOptionsForPoolType(pool->type);
1211 1212 1213
    if (options == NULL)
        return NULL;

1214
    virBufferAddLit(&buf, "<volume>\n");
1215 1216
    virBufferAsprintf(&buf,"  <name>%s</name>\n", def->name);
    virBufferAsprintf(&buf,"  <key>%s</key>\n", def->key);
1217
    virBufferAddLit(&buf, "  <source>\n");
1218 1219 1220 1221 1222 1223 1224 1225

    if (def->source.nextent) {
        int i;
        const char *thispath = NULL;
        for (i = 0 ; i < def->source.nextent ; i++) {
            if (thispath == NULL ||
                STRNEQ(thispath, def->source.extents[i].path)) {
                if (thispath != NULL)
1226 1227
                    virBufferAddLit(&buf, "    </device>\n");

1228
                virBufferAsprintf(&buf, "    <device path='%s'>\n",
1229
                                  def->source.extents[i].path);
1230 1231
            }

1232
            virBufferAsprintf(&buf,
1233 1234 1235
                              "      <extent start='%llu' end='%llu'/>\n",
                              def->source.extents[i].start,
                              def->source.extents[i].end);
1236 1237 1238
            thispath = def->source.extents[i].path;
        }
        if (thispath != NULL)
1239
            virBufferAddLit(&buf, "    </device>\n");
1240
    }
1241
    virBufferAddLit(&buf, "  </source>\n");
1242

E
Eric Blake 已提交
1243
    virBufferAsprintf(&buf,"  <capacity unit='bytes'>%llu</capacity>\n",
1244
                      def->capacity);
E
Eric Blake 已提交
1245
    virBufferAsprintf(&buf,"  <allocation unit='bytes'>%llu</allocation>\n",
1246
                      def->allocation);
1247

1248
    if (virStorageVolTargetDefFormat(options, &buf,
1249 1250
                                     &def->target, "target") < 0)
        goto cleanup;
1251

1252
    if (def->backingStore.path &&
1253
        virStorageVolTargetDefFormat(options, &buf,
1254 1255
                                     &def->backingStore, "backingStore") < 0)
        goto cleanup;
1256 1257

    virBufferAddLit(&buf,"</volume>\n");
1258

1259
    if (virBufferError(&buf))
1260 1261
        goto no_memory;

1262
    return virBufferContentAndReset(&buf);
1263 1264

 no_memory:
1265
    virReportOOMError();
1266
 cleanup:
1267
    virBufferFreeAndReset(&buf);
1268 1269 1270 1271 1272
    return NULL;
}


virStoragePoolObjPtr
1273
virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
1274
                            const unsigned char *uuid) {
1275
    unsigned int i;
1276

1277 1278
    for (i = 0 ; i < pools->count ; i++) {
        virStoragePoolObjLock(pools->objs[i]);
1279 1280
        if (!memcmp(pools->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
            return pools->objs[i];
1281 1282
        virStoragePoolObjUnlock(pools->objs[i]);
    }
1283 1284 1285 1286 1287

    return NULL;
}

virStoragePoolObjPtr
1288
virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
1289
                            const char *name) {
1290
    unsigned int i;
1291

1292 1293
    for (i = 0 ; i < pools->count ; i++) {
        virStoragePoolObjLock(pools->objs[i]);
1294 1295
        if (STREQ(pools->objs[i]->def->name, name))
            return pools->objs[i];
1296 1297
        virStoragePoolObjUnlock(pools->objs[i]);
    }
1298 1299 1300 1301

    return NULL;
}

1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
virStoragePoolObjPtr
virStoragePoolSourceFindDuplicateDevices(virStoragePoolObjPtr pool,
                                         virStoragePoolDefPtr def) {
    unsigned int i, j;

    for (i = 0; i < pool->def->source.ndevice; i++) {
        for (j = 0; j < def->source.ndevice; j++) {
            if (STREQ(pool->def->source.devices[i].path, def->source.devices[j].path))
                return pool;
        }
    }

    return NULL;
}

1317 1318 1319
void
virStoragePoolObjClearVols(virStoragePoolObjPtr pool)
{
1320 1321 1322 1323 1324 1325
    unsigned int i;
    for (i = 0 ; i < pool->volumes.count ; i++)
        virStorageVolDefFree(pool->volumes.objs[i]);

    VIR_FREE(pool->volumes.objs);
    pool->volumes.count = 0;
1326 1327 1328 1329 1330
}

virStorageVolDefPtr
virStorageVolDefFindByKey(virStoragePoolObjPtr pool,
                          const char *key) {
1331
    unsigned int i;
1332

1333 1334 1335
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->key, key))
            return pool->volumes.objs[i];
1336 1337 1338 1339 1340 1341 1342

    return NULL;
}

virStorageVolDefPtr
virStorageVolDefFindByPath(virStoragePoolObjPtr pool,
                           const char *path) {
1343
    unsigned int i;
1344

1345 1346 1347
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->target.path, path))
            return pool->volumes.objs[i];
1348 1349 1350 1351 1352 1353 1354

    return NULL;
}

virStorageVolDefPtr
virStorageVolDefFindByName(virStoragePoolObjPtr pool,
                           const char *name) {
1355
    unsigned int i;
1356

1357 1358 1359
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->name, name))
            return pool->volumes.objs[i];
1360 1361 1362 1363 1364

    return NULL;
}

virStoragePoolObjPtr
1365
virStoragePoolObjAssignDef(virStoragePoolObjListPtr pools,
1366 1367 1368
                           virStoragePoolDefPtr def) {
    virStoragePoolObjPtr pool;

1369
    if ((pool = virStoragePoolObjFindByName(pools, def->name))) {
1370 1371 1372 1373
        if (!virStoragePoolObjIsActive(pool)) {
            virStoragePoolDefFree(pool->def);
            pool->def = def;
        } else {
1374
            virStoragePoolDefFree(pool->newDef);
1375 1376 1377 1378 1379
            pool->newDef = def;
        }
        return pool;
    }

1380
    if (VIR_ALLOC(pool) < 0) {
1381
        virReportOOMError();
1382 1383 1384
        return NULL;
    }

1385
    if (virMutexInit(&pool->lock) < 0) {
1386
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1387 1388 1389 1390
                              "%s", _("cannot initialize mutex"));
        VIR_FREE(pool);
        return NULL;
    }
1391
    virStoragePoolObjLock(pool);
1392 1393 1394
    pool->active = 0;
    pool->def = def;

1395 1396
    if (VIR_REALLOC_N(pools->objs, pools->count+1) < 0) {
        pool->def = NULL;
1397
        virStoragePoolObjUnlock(pool);
1398
        virStoragePoolObjFree(pool);
1399
        virReportOOMError();
1400 1401 1402
        return NULL;
    }
    pools->objs[pools->count++] = pool;
1403 1404 1405 1406 1407

    return pool;
}

static virStoragePoolObjPtr
1408
virStoragePoolObjLoad(virStoragePoolObjListPtr pools,
1409 1410 1411 1412 1413 1414
                      const char *file,
                      const char *path,
                      const char *autostartLink) {
    virStoragePoolDefPtr def;
    virStoragePoolObjPtr pool;

1415
    if (!(def = virStoragePoolDefParseFile(path))) {
1416 1417 1418 1419
        return NULL;
    }

    if (!virFileMatchesNameSuffix(file, def->name, ".xml")) {
1420
        virStorageReportError(VIR_ERR_XML_ERROR,
1421
                              _("Storage pool config filename '%s' does not match pool name '%s'"),
1422
                              path, def->name);
1423 1424 1425 1426
        virStoragePoolDefFree(def);
        return NULL;
    }

1427
    if (!(pool = virStoragePoolObjAssignDef(pools, def))) {
1428 1429 1430 1431
        virStoragePoolDefFree(def);
        return NULL;
    }

1432
    VIR_FREE(pool->configFile);  /* for driver reload */
1433 1434
    pool->configFile = strdup(path);
    if (pool->configFile == NULL) {
1435
        virReportOOMError();
1436 1437 1438
        virStoragePoolDefFree(def);
        return NULL;
    }
1439
    VIR_FREE(pool->autostartLink); /* for driver reload */
1440 1441
    pool->autostartLink = strdup(autostartLink);
    if (pool->autostartLink == NULL) {
1442
        virReportOOMError();
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
        virStoragePoolDefFree(def);
        return NULL;
    }

    pool->autostart = virFileLinkPointsTo(pool->autostartLink,
                                          pool->configFile);

    return pool;
}


int
1455
virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
1456 1457
                             const char *configDir,
                             const char *autostartDir) {
1458 1459 1460
    DIR *dir;
    struct dirent *entry;

1461
    if (!(dir = opendir(configDir))) {
1462 1463
        if (errno == ENOENT)
            return 0;
1464
        virReportSystemError(errno, _("Failed to open dir '%s'"),
1465
                             configDir);
1466 1467 1468 1469
        return -1;
    }

    while ((entry = readdir(dir))) {
1470 1471
        char *path;
        char *autostartLink;
1472
        virStoragePoolObjPtr pool;
1473 1474 1475 1476 1477 1478 1479

        if (entry->d_name[0] == '.')
            continue;

        if (!virFileHasSuffix(entry->d_name, ".xml"))
            continue;

1480
        if (!(path = virFileBuildPath(configDir, entry->d_name, NULL)))
1481 1482
            continue;

1483 1484 1485
        if (!(autostartLink = virFileBuildPath(autostartDir, entry->d_name,
                                               NULL))) {
            VIR_FREE(path);
1486 1487 1488
            continue;
        }

1489
        pool = virStoragePoolObjLoad(pools, entry->d_name, path,
1490
                                     autostartLink);
1491 1492
        if (pool)
            virStoragePoolObjUnlock(pool);
1493 1494 1495

        VIR_FREE(path);
        VIR_FREE(autostartLink);
1496 1497 1498 1499 1500 1501 1502 1503
    }

    closedir(dir);

    return 0;
}

int
1504
virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
1505
                         virStoragePoolObjPtr pool,
1506 1507
                         virStoragePoolDefPtr def)
{
1508
    char *xml;
1509
    int ret = -1;
1510 1511

    if (!pool->configFile) {
1512 1513
        if (virFileMakePath(driver->configDir) < 0) {
            virReportSystemError(errno,
C
Cole Robinson 已提交
1514 1515
                                 _("cannot create config directory %s"),
                                 driver->configDir);
1516 1517 1518
            return -1;
        }

1519 1520
        if (!(pool->configFile = virFileBuildPath(driver->configDir,
                                                  def->name, ".xml"))) {
1521 1522 1523
            return -1;
        }

1524 1525
        if (!(pool->autostartLink = virFileBuildPath(driver->autostartDir,
                                                     def->name, ".xml"))) {
1526
            VIR_FREE(pool->configFile);
1527 1528 1529 1530
            return -1;
        }
    }

1531 1532
    if (!(xml = virStoragePoolDefFormat(def))) {
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1533
                              "%s", _("failed to generate XML"));
1534 1535 1536
        return -1;
    }

1537
    ret = virXMLSaveFile(pool->configFile, def->name, "pool-edit", xml);
1538
    VIR_FREE(xml);
1539 1540 1541 1542 1543

    return ret;
}

int
1544
virStoragePoolObjDeleteDef(virStoragePoolObjPtr pool) {
1545
    if (!pool->configFile) {
1546
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1547 1548 1549 1550 1551
                              _("no config file for %s"), pool->def->name);
        return -1;
    }

    if (unlink(pool->configFile) < 0) {
1552
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1553 1554 1555 1556 1557 1558 1559
                              _("cannot remove config for %s"),
                              pool->def->name);
        return -1;
    }

    return 0;
}
1560

1561
virStoragePoolSourcePtr
1562
virStoragePoolSourceListNewSource(virStoragePoolSourceListPtr list)
1563 1564 1565 1566
{
    virStoragePoolSourcePtr source;

    if (VIR_REALLOC_N(list->sources, list->nsources+1) < 0) {
1567
        virReportOOMError();
1568 1569 1570 1571 1572 1573 1574 1575 1576
        return NULL;
    }

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

    return source;
}

1577
char *virStoragePoolSourceListFormat(virStoragePoolSourceListPtr def)
1578
{
1579
    virStoragePoolOptionsPtr options;
1580
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1581 1582
    const char *type;
    int i;
1583

1584
    options = virStoragePoolOptionsForPoolType(def->type);
1585 1586 1587
    if (options == NULL)
        return NULL;

1588
    type = virStoragePoolTypeToString(def->type);
1589
    if (!type) {
1590
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1591 1592 1593 1594 1595
                              "%s", _("unexpected pool type"));
        goto cleanup;
    }

    virBufferAddLit(&buf, "<sources>\n");
1596 1597

    for (i = 0; i < def->nsources; i++) {
1598
        virStoragePoolSourceFormat(&buf, options, &def->sources[i]);
1599 1600
    }

1601 1602 1603 1604
    virBufferAddLit(&buf, "</sources>\n");

    if (virBufferError(&buf))
        goto no_memory;
1605 1606

    return virBufferContentAndReset(&buf);
1607 1608

 no_memory:
1609
    virReportOOMError();
1610
 cleanup:
1611
    virBufferFreeAndReset(&buf);
1612
    return NULL;
1613
}
D
Daniel P. Berrange 已提交
1614 1615


1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
/*
 * virStoragePoolObjIsDuplicate:
 * @doms : virStoragePoolObjListPtr to search
 * @def  : virStoragePoolDefPtr definition of pool to lookup
 * @check_active: If true, ensure that pool is not active
 *
 * Returns: -1 on error
 *          0 if pool is new
 *          1 if pool is a duplicate
 */
int virStoragePoolObjIsDuplicate(virStoragePoolObjListPtr pools,
                                 virStoragePoolDefPtr def,
                                 unsigned int check_active)
{
    int ret = -1;
    int dupPool = 0;
    virStoragePoolObjPtr pool = NULL;

    /* See if a Pool with matching UUID already exists */
    pool = virStoragePoolObjFindByUUID(pools, def->uuid);
    if (pool) {
        /* UUID matches, but if names don't match, refuse it */
        if (STRNEQ(pool->def->name, def->name)) {
            char uuidstr[VIR_UUID_STRING_BUFLEN];
            virUUIDFormat(pool->def->uuid, uuidstr);
            virStorageReportError(VIR_ERR_OPERATION_FAILED,
                                  _("pool '%s' is already defined with uuid %s"),
                                  pool->def->name, uuidstr);
            goto cleanup;
        }

        if (check_active) {
            /* UUID & name match, but if Pool is already active, refuse it */
            if (virStoragePoolObjIsActive(pool)) {
                virStorageReportError(VIR_ERR_OPERATION_INVALID,
                                      _("pool is already active as '%s'"),
                                      pool->def->name);
                goto cleanup;
            }
        }

        dupPool = 1;
    } else {
        /* UUID does not match, but if a name matches, refuse it */
        pool = virStoragePoolObjFindByName(pools, def->name);
        if (pool) {
            char uuidstr[VIR_UUID_STRING_BUFLEN];
            virUUIDFormat(pool->def->uuid, uuidstr);
            virStorageReportError(VIR_ERR_OPERATION_FAILED,
                                  _("pool '%s' already exists with uuid %s"),
                                  def->name, uuidstr);
            goto cleanup;
        }
    }

    ret = dupPool;
cleanup:
    if (pool)
        virStoragePoolObjUnlock(pool);
    return ret;
}

1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
int virStoragePoolSourceFindDuplicate(virStoragePoolObjListPtr pools,
                                      virStoragePoolDefPtr def)
{
    int i;
    int ret = 1;
    virStoragePoolObjPtr pool = NULL;
    virStoragePoolObjPtr matchpool = NULL;

    /* Check the pool list for duplicate underlying storage */
    for (i = 0; i < pools->count; i++) {
        pool = pools->objs[i];
        if (def->type != pool->def->type)
            continue;

1692 1693 1694 1695
        /* Don't mach against ourself if re-defining existing pool ! */
        if (STREQ(pool->def->name, def->name))
            continue;

1696 1697 1698 1699 1700 1701 1702 1703 1704
        virStoragePoolObjLock(pool);

        switch (pool->def->type) {
        case VIR_STORAGE_POOL_DIR:
            if (STREQ(pool->def->target.path, def->target.path))
                matchpool = pool;
            break;
        case VIR_STORAGE_POOL_NETFS:
            if ((STREQ(pool->def->source.dir, def->source.dir)) \
1705 1706
                && (pool->def->source.nhost == 1 && def->source.nhost == 1) \
                && (STREQ(pool->def->source.hosts[0].name, def->source.hosts[0].name)))
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
                matchpool = pool;
            break;
        case VIR_STORAGE_POOL_SCSI:
            if (STREQ(pool->def->source.adapter, def->source.adapter))
                matchpool = pool;
            break;
        case VIR_STORAGE_POOL_ISCSI:
        {
            matchpool = virStoragePoolSourceFindDuplicateDevices(pool, def);
            if (matchpool) {
1717 1718 1719 1720 1721 1722 1723 1724
                if (matchpool->def->source.nhost == 1 && def->source.nhost == 1) {
                    if (STREQ(matchpool->def->source.hosts[0].name, def->source.hosts[0].name)) {
                        if ((matchpool->def->source.initiator.iqn) && (def->source.initiator.iqn)) {
                            if (STREQ(matchpool->def->source.initiator.iqn, def->source.initiator.iqn))
                                break;
                            matchpool = NULL;
                        }
                        break;
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
                    }
                }
                matchpool = NULL;
            }
            break;
        }
        case VIR_STORAGE_POOL_FS:
        case VIR_STORAGE_POOL_LOGICAL:
        case VIR_STORAGE_POOL_DISK:
            matchpool = virStoragePoolSourceFindDuplicateDevices(pool, def);
            break;
        default:
            break;
        }
        virStoragePoolObjUnlock(pool);
1740 1741 1742

        if (matchpool)
            break;
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
    }

    if (matchpool) {
        virStorageReportError(VIR_ERR_OPERATION_FAILED,
                              _("Storage source conflict with pool: '%s'"),
                              matchpool->def->name);
        ret = -1;
    }
    return ret;
}
1753

1754 1755
void virStoragePoolObjLock(virStoragePoolObjPtr obj)
{
1756
    virMutexLock(&obj->lock);
1757 1758 1759 1760
}

void virStoragePoolObjUnlock(virStoragePoolObjPtr obj)
{
1761
    virMutexUnlock(&obj->lock);
1762
}