storage_conf.c 50.1 KB
Newer Older
1 2 3
/*
 * storage_conf.c: config handling for storage driver
 *
4
 * Copyright (C) 2006-2010 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"
46
#include "files.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 120 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
};



typedef struct _virStoragePoolOptions virStoragePoolOptions;
typedef virStoragePoolOptions *virStoragePoolOptionsPtr;
struct _virStoragePoolOptions {
    int flags;
    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
virStoragePoolSourceFree(virStoragePoolSourcePtr source) {
273 274
    int i;

275
    if (!source)
276 277
        return;

278 279 280 281
    VIR_FREE(source->host.name);
    for (i = 0 ; i < source->ndevice ; i++) {
        VIR_FREE(source->devices[i].freeExtents);
        VIR_FREE(source->devices[i].path);
282
    }
283 284 285
    VIR_FREE(source->devices);
    VIR_FREE(source->dir);
    VIR_FREE(source->name);
286
    VIR_FREE(source->adapter);
D
David Allan 已提交
287
    VIR_FREE(source->initiator.iqn);
288 289
    VIR_FREE(source->vendor);
    VIR_FREE(source->product);
290

291 292 293
    if (source->authType == VIR_STORAGE_POOL_AUTH_CHAP) {
        VIR_FREE(source->auth.chap.login);
        VIR_FREE(source->auth.chap.passwd);
294
    }
295 296 297 298 299 300 301 302 303 304
}

void
virStoragePoolDefFree(virStoragePoolDefPtr def) {
    if (!def)
        return;

    VIR_FREE(def->name);

    virStoragePoolSourceFree(&def->source);
305

306 307 308
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
    VIR_FREE(def);
309 310 311 312 313
}


void
virStoragePoolObjFree(virStoragePoolObjPtr obj) {
314 315 316
    if (!obj)
        return;

317 318
    virStoragePoolObjClearVols(obj);

319 320
    virStoragePoolDefFree(obj->def);
    virStoragePoolDefFree(obj->newDef);
321

322 323
    VIR_FREE(obj->configFile);
    VIR_FREE(obj->autostartLink);
324 325 326

    virMutexDestroy(&obj->lock);

327
    VIR_FREE(obj);
328 329
}

330 331 332 333 334 335 336 337 338
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;
}

339
void
340
virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
341 342
                        virStoragePoolObjPtr pool)
{
343
    unsigned int i;
344

345 346
    virStoragePoolObjUnlock(pool);

347
    for (i = 0 ; i < pools->count ; i++) {
348
        virStoragePoolObjLock(pools->objs[i]);
349
        if (pools->objs[i] == pool) {
350
            virStoragePoolObjUnlock(pools->objs[i]);
351
            virStoragePoolObjFree(pools->objs[i]);
352

353 354 355
            if (i < (pools->count - 1))
                memmove(pools->objs + i, pools->objs + i + 1,
                        sizeof(*(pools->objs)) * (pools->count - (i + 1)));
356

357 358 359 360
            if (VIR_REALLOC_N(pools->objs, pools->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            pools->count--;
361

362 363
            break;
        }
364
        virStoragePoolObjUnlock(pools->objs[i]);
365
    }
366 367 368 369
}


static int
370
virStoragePoolDefParseAuthChap(xmlXPathContextPtr ctxt,
371
                               virStoragePoolAuthChapPtr auth) {
372
    auth->login = virXPathString("string(./auth/@login)", ctxt);
373
    if (auth->login == NULL) {
374
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
375
                              "%s", _("missing auth host attribute"));
376 377 378
        return -1;
    }

379
    auth->passwd = virXPathString("string(./auth/@passwd)", ctxt);
380
    if (auth->passwd == NULL) {
381
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
382
                              "%s", _("missing auth passwd attribute"));
383 384 385 386 387 388
        return -1;
    }

    return 0;
}

389
static int
390
virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
391 392 393 394 395 396 397 398
                             virStoragePoolSourcePtr source,
                             int pool_type,
                             xmlNodePtr node) {
    int ret = -1;
    xmlNodePtr relnode, *nodeset = NULL;
    char *authType = NULL;
    int nsource, i;
    virStoragePoolOptionsPtr options;
399
    char *port = NULL;
400 401 402 403 404 405 406 407

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

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

408
    source->name = virXPathString("string(./name)", ctxt);
409 410

    if (options->formatFromString) {
411
        char *format = virXPathString("string(./format/@type)", ctxt);
412 413 414 415 416 417
        if (format == NULL)
            source->format = options->defaultFormat;
        else
            source->format = options->formatFromString(format);

        if (source->format < 0) {
418
            virStorageReportError(VIR_ERR_XML_ERROR,
419 420 421 422 423 424 425
                                  _("unknown pool format type %s"), format);
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

426
    source->host.name = virXPathString("string(./host/@name)", ctxt);
427 428 429 430 431 432 433 434 435 436 437
    port = virXPathString("string(./host/@port)", ctxt);
    if (port) {
        if (virStrToLong_i(port, NULL, 10, &source->host.port) < 0) {
            virStorageReportError(VIR_ERR_XML_ERROR,
                                  _("Invalid port number: %s"),
                                  port);
            goto cleanup;
        }
    }


438
    source->initiator.iqn = virXPathString("string(./initiator/iqn/@name)", ctxt);
439

440
    nsource = virXPathNodeSet("./device", ctxt, &nodeset);
441 442 443
    if (nsource < 0)
        goto cleanup;

444 445 446
    if (nsource > 0) {
        if (VIR_ALLOC_N(source->devices, nsource) < 0) {
            VIR_FREE(nodeset);
447
            virReportOOMError();
448 449 450 451
            goto cleanup;
        }

        for (i = 0 ; i < nsource ; i++) {
452
            char *path = virXMLPropString(nodeset[i], "path");
453 454
            if (path == NULL) {
                VIR_FREE(nodeset);
455
                virStorageReportError(VIR_ERR_XML_ERROR,
456 457 458
                        "%s", _("missing storage pool source device path"));
                goto cleanup;
            }
459
            source->devices[i].path = path;
460 461 462 463
        }
        source->ndevice = nsource;
    }

464 465
    source->dir = virXPathString("string(./dir/@path)", ctxt);
    source->adapter = virXPathString("string(./adapter/@name)", ctxt);
466

467
    authType = virXPathString("string(./auth/@type)", ctxt);
468 469 470 471 472 473
    if (authType == NULL) {
        source->authType = VIR_STORAGE_POOL_AUTH_NONE;
    } else {
        if (STREQ(authType, "chap")) {
            source->authType = VIR_STORAGE_POOL_AUTH_CHAP;
        } else {
474
            virStorageReportError(VIR_ERR_XML_ERROR,
475 476 477 478 479 480 481
                                  _("unknown auth type '%s'"),
                                  (const char *)authType);
            goto cleanup;
        }
    }

    if (source->authType == VIR_STORAGE_POOL_AUTH_CHAP) {
482
        if (virStoragePoolDefParseAuthChap(ctxt, &source->auth.chap) < 0)
483 484 485
            goto cleanup;
    }

486 487 488
    source->vendor = virXPathString("string(./vendor/@name)", ctxt);
    source->product = virXPathString("string(./product/@name)", ctxt);

489 490 491 492
    ret = 0;
cleanup:
    ctxt->node = relnode;

493
    VIR_FREE(port);
494 495 496 497
    VIR_FREE(authType);
    VIR_FREE(nodeset);
    return ret;
}
498

499
virStoragePoolSourcePtr
500
virStoragePoolDefParseSourceString(const char *srcSpec,
501 502 503 504 505 506 507 508 509 510 511 512
                                   int pool_type)
{
    xmlDocPtr doc = NULL;
    xmlNodePtr node = NULL;
    xmlXPathContextPtr xpath_ctxt = NULL;
    virStoragePoolSourcePtr def = NULL, ret = NULL;

    doc = xmlReadDoc((const xmlChar *)srcSpec, "srcSpec.xml", NULL,
                     XML_PARSE_NOENT | XML_PARSE_NONET |
                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);

    if (doc == NULL) {
513
        virStorageReportError(VIR_ERR_XML_ERROR,
514 515 516 517 518 519
                              "%s", _("bad <source> spec"));
        goto cleanup;
    }

    xpath_ctxt = xmlXPathNewContext(doc);
    if (xpath_ctxt == NULL) {
520
        virReportOOMError();
521 522 523 524
        goto cleanup;
    }

    if (VIR_ALLOC(def) < 0) {
525
        virReportOOMError();
526 527 528
        goto cleanup;
    }

529
    node = virXPathNode("/source", xpath_ctxt);
530
    if (!node) {
531
        virStorageReportError(VIR_ERR_XML_ERROR,
532 533 534 535
                              "%s", _("root element was not source"));
        goto cleanup;
    }

536
    if (virStoragePoolDefParseSource(xpath_ctxt, def, pool_type,
537 538 539 540 541 542
                                     node) < 0)
        goto cleanup;

    ret = def;
    def = NULL;
cleanup:
543 544
    virStoragePoolSourceFree(def);
    VIR_FREE(def);
545 546 547 548 549
    xmlFreeDoc(doc);
    xmlXPathFreeContext(xpath_ctxt);

    return ret;
}
550
static int
551
virStorageDefParsePerms(xmlXPathContextPtr ctxt,
552 553 554
                        virStoragePermsPtr perms,
                        const char *permxpath,
                        int defaultmode) {
555 556
    char *mode;
    long v;
557 558 559
    int ret = -1;
    xmlNodePtr relnode;
    xmlNodePtr node;
560

561
    node = virXPathNode(permxpath, ctxt);
562 563 564
    if (node == NULL) {
        /* Set default values if there is not <permissions> element */
        perms->mode = defaultmode;
565 566
        perms->uid = -1;
        perms->gid = -1;
567 568 569 570 571 572 573
        perms->label = NULL;
        return 0;
    }

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

574
    mode = virXPathString("string(./mode)", ctxt);
575
    if (!mode) {
576
        perms->mode = defaultmode;
577
    } else {
578
        char *end = NULL;
579 580
        perms->mode = strtol(mode, &end, 8);
        if (*end || perms->mode < 0 || perms->mode > 0777) {
581
            VIR_FREE(mode);
582
            virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
583
                                  "%s", _("malformed octal mode"));
584
            goto error;
585
        }
586
        VIR_FREE(mode);
587 588
    }

589
    if (virXPathNode("./owner", ctxt) == NULL) {
590
        perms->uid = -1;
591
    } else {
592
        if (virXPathLong("number(./owner)", ctxt, &v) < 0) {
593
            virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
594
                                  "%s", _("malformed owner element"));
595
            goto error;
596 597 598 599
        }
        perms->uid = (int)v;
    }

600
    if (virXPathNode("./group", ctxt) == NULL) {
601
        perms->gid = -1;
602
    } else {
603
        if (virXPathLong("number(./group)", ctxt, &v) < 0) {
604
            virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
605
                                  "%s", _("malformed group element"));
606
            goto error;
607 608 609 610 611
        }
        perms->gid = (int)v;
    }

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

614 615 616 617
    ret = 0;
error:
    ctxt->node = relnode;
    return ret;
618 619 620
}

static virStoragePoolDefPtr
621
virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) {
622
    virStoragePoolOptionsPtr options;
623
    virStoragePoolDefPtr ret;
624
    xmlNodePtr source_node;
625
    char *type = NULL;
626
    char *uuid = NULL;
627
    char *tmppath;
628

629
    if (VIR_ALLOC(ret) < 0) {
630
        virReportOOMError();
631
        return NULL;
632
    }
633

634
    type = virXPathString("string(./@type)", ctxt);
635
    if ((ret->type = virStoragePoolTypeFromString((const char *)type)) < 0) {
636
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
637
                              _("unknown storage pool type %s"), (const char*)type);
638
        goto cleanup;
639 640
    }

641 642 643
    xmlFree(type);
    type = NULL;

644
    if ((options = virStoragePoolOptionsForPoolType(ret->type)) == NULL) {
645 646 647
        goto cleanup;
    }

648
    source_node = virXPathNode("./source", ctxt);
649
    if (source_node) {
650
        if (virStoragePoolDefParseSource(ctxt, &ret->source, ret->type,
651 652 653 654
                                         source_node) < 0)
            goto cleanup;
    }

655
    ret->name = virXPathString("string(./name)", ctxt);
656
    if (ret->name == NULL &&
657
        options->flags & VIR_STORAGE_POOL_SOURCE_NAME)
658
        ret->name = ret->source.name;
659
    if (ret->name == NULL) {
660
        virStorageReportError(VIR_ERR_XML_ERROR,
661
                              "%s", _("missing pool source name element"));
662 663 664
        goto cleanup;
    }

665
    uuid = virXPathString("string(./uuid)", ctxt);
666 667
    if (uuid == NULL) {
        if (virUUIDGenerate(ret->uuid) < 0) {
668
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
669
                                  "%s", _("unable to generate uuid"));
670 671 672 673
            goto cleanup;
        }
    } else {
        if (virUUIDParse(uuid, ret->uuid) < 0) {
674
            virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
675
                                  "%s", _("malformed uuid element"));
676 677
            goto cleanup;
        }
678
        VIR_FREE(uuid);
679 680
    }

681
    if (options->flags & VIR_STORAGE_POOL_SOURCE_HOST) {
682
        if (!ret->source.host.name) {
683
            virStorageReportError(VIR_ERR_XML_ERROR,
684 685
                                  "%s",
                                  _("missing storage pool source host name"));
686 687 688 689
            goto cleanup;
        }
    }

690
    if (options->flags & VIR_STORAGE_POOL_SOURCE_DIR) {
691
        if (!ret->source.dir) {
692
            virStorageReportError(VIR_ERR_XML_ERROR,
693
                                  "%s", _("missing storage pool source path"));
694 695 696
            goto cleanup;
        }
    }
697
    if (options->flags & VIR_STORAGE_POOL_SOURCE_NAME) {
698 699 700
        if (ret->source.name == NULL) {
            /* source name defaults to pool name */
            ret->source.name = strdup(ret->name);
701
            if (ret->source.name == NULL) {
702
                virReportOOMError();
703 704
                goto cleanup;
            }
705 706
        }
    }
707

708
    if (options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) {
709
        if (!ret->source.adapter) {
710
            virStorageReportError(VIR_ERR_XML_ERROR,
711
                                  "%s", _("missing storage pool source adapter name"));
712 713 714
            goto cleanup;
        }
    }
715

716 717 718 719 720 721 722 723 724
    /* 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;
        }
    }

725
    if ((tmppath = virXPathString("string(./target/path)", ctxt)) == NULL) {
726
        virStorageReportError(VIR_ERR_XML_ERROR,
727
                              "%s", _("missing storage pool target path"));
728 729
        goto cleanup;
    }
730 731 732 733 734
    ret->target.path = virFileSanitizePath(tmppath);
    VIR_FREE(tmppath);
    if (!ret->target.path)
        goto cleanup;

735

736
    if (virStorageDefParsePerms(ctxt, &ret->target.perms,
737
                                "./target/permissions", 0700) < 0)
738 739 740 741 742
        goto cleanup;

    return ret;

 cleanup:
743
    VIR_FREE(uuid);
744
    xmlFree(type);
745 746 747 748 749
    virStoragePoolDefFree(ret);
    return NULL;
}

virStoragePoolDefPtr
750
virStoragePoolDefParseNode(xmlDocPtr xml,
751 752 753 754 755
                           xmlNodePtr root) {
    xmlXPathContextPtr ctxt = NULL;
    virStoragePoolDefPtr def = NULL;

    if (STRNEQ((const char *)root->name, "pool")) {
756 757
        virStorageReportError(VIR_ERR_XML_ERROR,
                              "%s", _("unknown root element for storage pool"));
758 759 760 761 762
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
763
        virReportOOMError();
764 765 766 767
        goto cleanup;
    }

    ctxt->node = root;
768
    def = virStoragePoolDefParseXML(ctxt);
769 770 771 772 773 774
cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

static virStoragePoolDefPtr
775
virStoragePoolDefParse(const char *xmlStr,
776 777
                       const char *filename) {
    virStoragePoolDefPtr ret = NULL;
J
Jiri Denemark 已提交
778
    xmlDocPtr xml;
779

J
Jiri Denemark 已提交
780 781 782
    if ((xml = virXMLParse(filename, xmlStr, "storage.xml"))) {
        ret = virStoragePoolDefParseNode(xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
783 784
    }

785 786 787
    return ret;
}

788
virStoragePoolDefPtr
789
virStoragePoolDefParseString(const char *xmlStr)
790
{
791
    return virStoragePoolDefParse(xmlStr, NULL);
792 793 794
}

virStoragePoolDefPtr
795
virStoragePoolDefParseFile(const char *filename)
796
{
797
    return virStoragePoolDefParse(NULL, filename);
798 799
}

800
static int
801
virStoragePoolSourceFormat(virBufferPtr buf,
802
                           virStoragePoolOptionsPtr options,
803 804 805 806 807
                           virStoragePoolSourcePtr src)
{
    int i, j;

    virBufferAddLit(buf,"  <source>\n");
808
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_HOST) &&
809
        src->host.name) {
810
        virBufferAsprintf(buf, "    <host name='%s'", src->host.name);
811
        if (src->host.port)
812
            virBufferAsprintf(buf, " port='%d'", src->host.port);
813 814
        virBufferAddLit(buf, "/>\n");
    }
815

816
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_DEVICE) &&
817 818 819
        src->ndevice) {
        for (i = 0 ; i < src->ndevice ; i++) {
            if (src->devices[i].nfreeExtent) {
820
                virBufferAsprintf(buf,"    <device path='%s'>\n",
821 822
                                  src->devices[i].path);
                for (j = 0 ; j < src->devices[i].nfreeExtent ; j++) {
823
                    virBufferAsprintf(buf, "    <freeExtent start='%llu' end='%llu'/>\n",
824 825 826 827 828 829
                                      src->devices[i].freeExtents[j].start,
                                      src->devices[i].freeExtents[j].end);
                }
                virBufferAddLit(buf,"    </device>\n");
            }
            else
830
                virBufferAsprintf(buf, "    <device path='%s'/>\n",
831 832 833
                                  src->devices[i].path);
        }
    }
834
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_DIR) &&
835
        src->dir)
836
        virBufferAsprintf(buf,"    <dir path='%s'/>\n", src->dir);
837
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
838
        src->adapter)
839
        virBufferAsprintf(buf,"    <adapter name='%s'/>\n", src->adapter);
840
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_NAME) &&
841
        src->name)
842
        virBufferAsprintf(buf,"    <name>%s</name>\n", src->name);
843

D
David Allan 已提交
844 845 846 847 848 849 850
    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");
    }

851 852 853
    if (options->formatToString) {
        const char *format = (options->formatToString)(src->format);
        if (!format) {
854
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
855 856 857 858
                                  _("unknown pool format number %d"),
                                  src->format);
            return -1;
        }
859
        virBufferAsprintf(buf,"    <format type='%s'/>\n", format);
860 861 862 863
    }


    if (src->authType == VIR_STORAGE_POOL_AUTH_CHAP)
864
        virBufferAsprintf(buf,"    <auth type='chap' login='%s' passwd='%s'/>\n",
865 866
                          src->auth.chap.login,
                          src->auth.chap.passwd);
867 868 869 870 871 872 873 874 875

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

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

876 877 878 879 880
    virBufferAddLit(buf,"  </source>\n");

    return 0;
}

881 882

char *
883
virStoragePoolDefFormat(virStoragePoolDefPtr def) {
884
    virStoragePoolOptionsPtr options;
885
    virBuffer buf = VIR_BUFFER_INITIALIZER;
886 887 888
    const char *type;
    char uuid[VIR_UUID_STRING_BUFLEN];

889
    options = virStoragePoolOptionsForPoolType(def->type);
890 891 892
    if (options == NULL)
        return NULL;

893
    type = virStoragePoolTypeToString(def->type);
894
    if (!type) {
895
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
896
                              "%s", _("unexpected pool type"));
897 898
        goto cleanup;
    }
899 900
    virBufferAsprintf(&buf, "<pool type='%s'>\n", type);
    virBufferAsprintf(&buf,"  <name>%s</name>\n", def->name);
901 902

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

905
    virBufferAsprintf(&buf,"  <capacity>%llu</capacity>\n",
906
                      def->capacity);
907
    virBufferAsprintf(&buf,"  <allocation>%llu</allocation>\n",
908
                      def->allocation);
909
    virBufferAsprintf(&buf,"  <available>%llu</available>\n",
910
                      def->available);
911

912
    if (virStoragePoolSourceFormat(&buf, options, &def->source) < 0)
913
        goto cleanup;
914

915
    virBufferAddLit(&buf,"  <target>\n");
916

917
    if (def->target.path)
918
        virBufferAsprintf(&buf,"    <path>%s</path>\n", def->target.path);
919

920
    virBufferAddLit(&buf,"    <permissions>\n");
921
    virBufferAsprintf(&buf,"      <mode>0%o</mode>\n",
922
                      def->target.perms.mode);
923
    virBufferAsprintf(&buf,"      <owner>%d</owner>\n",
924
                      def->target.perms.uid);
925
    virBufferAsprintf(&buf,"      <group>%d</group>\n",
926
                      def->target.perms.gid);
927

928
    if (def->target.perms.label)
929
        virBufferAsprintf(&buf,"      <label>%s</label>\n",
930
                          def->target.perms.label);
931

932 933 934
    virBufferAddLit(&buf,"    </permissions>\n");
    virBufferAddLit(&buf,"  </target>\n");
    virBufferAddLit(&buf,"</pool>\n");
935

936
    if (virBufferError(&buf))
937 938
        goto no_memory;

939
    return virBufferContentAndReset(&buf);
940 941

 no_memory:
942
    virReportOOMError();
943
 cleanup:
944
    virBufferFreeAndReset(&buf);
945 946 947 948 949
    return NULL;
}


static int
950
virStorageSize(const char *unit,
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
               const char *val,
               unsigned long long *ret) {
    unsigned long long mult;
    char *end;

    if (!unit) {
        mult = 1;
    } else {
        switch (unit[0]) {
        case 'k':
        case 'K':
            mult = 1024ull;
            break;

        case 'm':
        case 'M':
            mult = 1024ull * 1024ull;
            break;

        case 'g':
        case 'G':
            mult = 1024ull * 1024ull * 1024ull;
            break;

        case 't':
        case 'T':
            mult = 1024ull * 1024ull * 1024ull * 1024ull;
            break;

        case 'p':
        case 'P':
            mult = 1024ull * 1024ull * 1024ull * 1024ull * 1024ull;
            break;

985 986
        case 'e':
        case 'E':
987 988 989 990 991
            mult = 1024ull * 1024ull * 1024ull * 1024ull * 1024ull *
                1024ull;
            break;

        default:
992
            virStorageReportError(VIR_ERR_XML_ERROR,
993 994 995 996 997 998
                                  _("unknown size units '%s'"), unit);
            return -1;
        }
    }

    if (virStrToLong_ull (val, &end, 10, ret) < 0) {
999
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
1000
                              "%s", _("malformed capacity element"));
1001 1002 1003
        return -1;
    }
    if (*ret > (ULLONG_MAX / mult)) {
1004
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
1005
                              "%s", _("capacity element value too large"));
1006 1007 1008 1009 1010 1011 1012 1013 1014
            return -1;
    }

    *ret *= mult;

    return 0;
}

static virStorageVolDefPtr
1015
virStorageVolDefParseXML(virStoragePoolDefPtr pool,
1016
                         xmlXPathContextPtr ctxt) {
1017
    virStorageVolDefPtr ret;
1018
    virStorageVolOptionsPtr options;
1019 1020 1021
    char *allocation = NULL;
    char *capacity = NULL;
    char *unit = NULL;
1022
    xmlNodePtr node;
1023

1024
    options = virStorageVolOptionsForPoolType(pool->type);
1025 1026 1027
    if (options == NULL)
        return NULL;

1028
    if (VIR_ALLOC(ret) < 0) {
1029
        virReportOOMError();
1030
        return NULL;
1031
    }
1032

1033
    ret->name = virXPathString("string(./name)", ctxt);
1034
    if (ret->name == NULL) {
1035
        virStorageReportError(VIR_ERR_XML_ERROR,
1036
                              "%s", _("missing volume name element"));
1037 1038 1039
        goto cleanup;
    }

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

1043 1044
    capacity = virXPathString("string(./capacity)", ctxt);
    unit = virXPathString("string(./capacity/@unit)", ctxt);
1045
    if (capacity == NULL) {
1046
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
1047
                              "%s", _("missing capacity element"));
1048 1049
        goto cleanup;
    }
1050
    if (virStorageSize(unit, capacity, &ret->capacity) < 0)
1051
        goto cleanup;
1052 1053
    VIR_FREE(capacity);
    VIR_FREE(unit);
1054

1055
    allocation = virXPathString("string(./allocation)", ctxt);
1056
    if (allocation) {
1057
        unit = virXPathString("string(./allocation/@unit)", ctxt);
1058
        if (virStorageSize(unit, allocation, &ret->allocation) < 0)
1059
            goto cleanup;
1060 1061
        VIR_FREE(allocation);
        VIR_FREE(unit);
1062 1063 1064 1065
    } else {
        ret->allocation = ret->capacity;
    }

1066
    ret->target.path = virXPathString("string(./target/path)", ctxt);
1067
    if (options->formatFromString) {
1068
        char *format = virXPathString("string(./target/format/@type)", ctxt);
1069 1070 1071 1072 1073 1074
        if (format == NULL)
            ret->target.format = options->defaultFormat;
        else
            ret->target.format = (options->formatFromString)(format);

        if (ret->target.format < 0) {
1075
            virStorageReportError(VIR_ERR_XML_ERROR,
1076
                                  _("unknown volume format type %s"), format);
1077
            VIR_FREE(format);
1078 1079
            goto cleanup;
        }
1080
        VIR_FREE(format);
1081 1082
    }

1083
    if (virStorageDefParsePerms(ctxt, &ret->target.perms,
1084
                                "./target/permissions", 0600) < 0)
1085 1086
        goto cleanup;

1087
    node = virXPathNode("./target/encryption", ctxt);
1088
    if (node != NULL) {
1089
        ret->target.encryption = virStorageEncryptionParseNode(ctxt->doc,
1090 1091 1092 1093 1094
                                                               node);
        if (ret->target.encryption == NULL)
            goto cleanup;
    }

1095 1096


1097
    ret->backingStore.path = virXPathString("string(./backingStore/path)", ctxt);
1098
    if (options->formatFromString) {
1099
        char *format = virXPathString("string(./backingStore/format/@type)", ctxt);
1100 1101 1102 1103 1104 1105
        if (format == NULL)
            ret->backingStore.format = options->defaultFormat;
        else
            ret->backingStore.format = (options->formatFromString)(format);

        if (ret->backingStore.format < 0) {
1106
            virStorageReportError(VIR_ERR_XML_ERROR,
1107 1108 1109 1110 1111 1112 1113
                                  _("unknown volume format type %s"), format);
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

1114
    if (virStorageDefParsePerms(ctxt, &ret->backingStore.perms,
1115
                                "./backingStore/permissions", 0600) < 0)
1116 1117
        goto cleanup;

1118 1119 1120
    return ret;

 cleanup:
1121 1122 1123
    VIR_FREE(allocation);
    VIR_FREE(capacity);
    VIR_FREE(unit);
1124 1125 1126 1127 1128
    virStorageVolDefFree(ret);
    return NULL;
}

virStorageVolDefPtr
1129
virStorageVolDefParseNode(virStoragePoolDefPtr pool,
1130 1131 1132 1133 1134 1135
                          xmlDocPtr xml,
                          xmlNodePtr root) {
    xmlXPathContextPtr ctxt = NULL;
    virStorageVolDefPtr def = NULL;

    if (STRNEQ((const char *)root->name, "volume")) {
1136
        virStorageReportError(VIR_ERR_XML_ERROR,
1137 1138 1139 1140 1141 1142
                          "%s", _("unknown root element for storage vol"));
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1143
        virReportOOMError();
1144 1145 1146 1147
        goto cleanup;
    }

    ctxt->node = root;
1148
    def = virStorageVolDefParseXML(pool, ctxt);
1149 1150 1151 1152 1153 1154
cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

static virStorageVolDefPtr
1155
virStorageVolDefParse(virStoragePoolDefPtr pool,
1156 1157 1158
                      const char *xmlStr,
                      const char *filename) {
    virStorageVolDefPtr ret = NULL;
J
Jiri Denemark 已提交
1159
    xmlDocPtr xml;
1160

J
Jiri Denemark 已提交
1161 1162 1163
    if ((xml = virXMLParse(filename, xmlStr, "storage.xml"))) {
        ret = virStorageVolDefParseNode(pool, xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
1164 1165 1166 1167 1168
    }

    return ret;
}

1169
virStorageVolDefPtr
1170
virStorageVolDefParseString(virStoragePoolDefPtr pool,
1171 1172
                            const char *xmlStr)
{
1173
    return virStorageVolDefParse(pool, xmlStr, NULL);
1174 1175 1176
}

virStorageVolDefPtr
1177
virStorageVolDefParseFile(virStoragePoolDefPtr pool,
1178 1179
                          const char *filename)
{
1180
    return virStorageVolDefParse(pool, NULL, filename);
1181
}
1182

1183
static int
1184
virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
1185 1186 1187
                             virBufferPtr buf,
                             virStorageVolTargetPtr def,
                             const char *type) {
1188
    virBufferAsprintf(buf, "  <%s>\n", type);
1189 1190

    if (def->path)
1191
        virBufferAsprintf(buf,"    <path>%s</path>\n", def->path);
1192 1193 1194 1195

    if (options->formatToString) {
        const char *format = (options->formatToString)(def->format);
        if (!format) {
1196
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1197 1198 1199 1200
                                  _("unknown volume format number %d"),
                                  def->format);
            return -1;
        }
1201
        virBufferAsprintf(buf,"    <format type='%s'/>\n", format);
1202 1203 1204
    }

    virBufferAddLit(buf,"    <permissions>\n");
1205
    virBufferAsprintf(buf,"      <mode>0%o</mode>\n",
1206
                      def->perms.mode);
1207
    virBufferAsprintf(buf,"      <owner>%d</owner>\n",
1208
                      def->perms.uid);
1209
    virBufferAsprintf(buf,"      <group>%d</group>\n",
1210 1211 1212 1213
                      def->perms.gid);


    if (def->perms.label)
1214
        virBufferAsprintf(buf,"      <label>%s</label>\n",
1215 1216 1217 1218
                          def->perms.label);

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

1219
    if (def->encryption != NULL &&
1220
        virStorageEncryptionFormat(buf, def->encryption, 4) < 0)
1221 1222
        return -1;

1223
    virBufferAsprintf(buf, "  </%s>\n", type);
1224 1225 1226

    return 0;
}
1227 1228

char *
1229
virStorageVolDefFormat(virStoragePoolDefPtr pool,
1230
                       virStorageVolDefPtr def) {
1231
    virStorageVolOptionsPtr options;
1232
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1233

1234
    options = virStorageVolOptionsForPoolType(pool->type);
1235 1236 1237
    if (options == NULL)
        return NULL;

1238
    virBufferAddLit(&buf, "<volume>\n");
1239 1240
    virBufferAsprintf(&buf,"  <name>%s</name>\n", def->name);
    virBufferAsprintf(&buf,"  <key>%s</key>\n", def->key);
1241
    virBufferAddLit(&buf, "  <source>\n");
1242 1243 1244 1245 1246 1247 1248 1249

    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)
1250 1251
                    virBufferAddLit(&buf, "    </device>\n");

1252
                virBufferAsprintf(&buf, "    <device path='%s'>\n",
1253
                                  def->source.extents[i].path);
1254 1255
            }

1256
            virBufferAsprintf(&buf,
1257 1258 1259
                              "      <extent start='%llu' end='%llu'/>\n",
                              def->source.extents[i].start,
                              def->source.extents[i].end);
1260 1261 1262
            thispath = def->source.extents[i].path;
        }
        if (thispath != NULL)
1263
            virBufferAddLit(&buf, "    </device>\n");
1264
    }
1265
    virBufferAddLit(&buf, "  </source>\n");
1266

1267
    virBufferAsprintf(&buf,"  <capacity>%llu</capacity>\n",
1268
                      def->capacity);
1269
    virBufferAsprintf(&buf,"  <allocation>%llu</allocation>\n",
1270
                      def->allocation);
1271

1272
    if (virStorageVolTargetDefFormat(options, &buf,
1273 1274
                                     &def->target, "target") < 0)
        goto cleanup;
1275

1276
    if (def->backingStore.path &&
1277
        virStorageVolTargetDefFormat(options, &buf,
1278 1279
                                     &def->backingStore, "backingStore") < 0)
        goto cleanup;
1280 1281

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

1283
    if (virBufferError(&buf))
1284 1285
        goto no_memory;

1286
    return virBufferContentAndReset(&buf);
1287 1288

 no_memory:
1289
    virReportOOMError();
1290
 cleanup:
1291
    virBufferFreeAndReset(&buf);
1292 1293 1294 1295 1296
    return NULL;
}


virStoragePoolObjPtr
1297
virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
1298
                            const unsigned char *uuid) {
1299
    unsigned int i;
1300

1301 1302
    for (i = 0 ; i < pools->count ; i++) {
        virStoragePoolObjLock(pools->objs[i]);
1303 1304
        if (!memcmp(pools->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
            return pools->objs[i];
1305 1306
        virStoragePoolObjUnlock(pools->objs[i]);
    }
1307 1308 1309 1310 1311

    return NULL;
}

virStoragePoolObjPtr
1312
virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
1313
                            const char *name) {
1314
    unsigned int i;
1315

1316 1317
    for (i = 0 ; i < pools->count ; i++) {
        virStoragePoolObjLock(pools->objs[i]);
1318 1319
        if (STREQ(pools->objs[i]->def->name, name))
            return pools->objs[i];
1320 1321
        virStoragePoolObjUnlock(pools->objs[i]);
    }
1322 1323 1324 1325 1326 1327 1328

    return NULL;
}

void
virStoragePoolObjClearVols(virStoragePoolObjPtr pool)
{
1329 1330 1331 1332 1333 1334
    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;
1335 1336 1337 1338 1339
}

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

1342 1343 1344
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->key, key))
            return pool->volumes.objs[i];
1345 1346 1347 1348 1349 1350 1351

    return NULL;
}

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

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

    return NULL;
}

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

1366 1367 1368
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->name, name))
            return pool->volumes.objs[i];
1369 1370 1371 1372 1373

    return NULL;
}

virStoragePoolObjPtr
1374
virStoragePoolObjAssignDef(virStoragePoolObjListPtr pools,
1375 1376 1377
                           virStoragePoolDefPtr def) {
    virStoragePoolObjPtr pool;

1378
    if ((pool = virStoragePoolObjFindByName(pools, def->name))) {
1379 1380 1381 1382
        if (!virStoragePoolObjIsActive(pool)) {
            virStoragePoolDefFree(pool->def);
            pool->def = def;
        } else {
1383
            virStoragePoolDefFree(pool->newDef);
1384 1385 1386 1387 1388
            pool->newDef = def;
        }
        return pool;
    }

1389
    if (VIR_ALLOC(pool) < 0) {
1390
        virReportOOMError();
1391 1392 1393
        return NULL;
    }

1394
    if (virMutexInit(&pool->lock) < 0) {
1395
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1396 1397 1398 1399
                              "%s", _("cannot initialize mutex"));
        VIR_FREE(pool);
        return NULL;
    }
1400
    virStoragePoolObjLock(pool);
1401 1402 1403
    pool->active = 0;
    pool->def = def;

1404 1405
    if (VIR_REALLOC_N(pools->objs, pools->count+1) < 0) {
        pool->def = NULL;
1406
        virStoragePoolObjUnlock(pool);
1407
        virStoragePoolObjFree(pool);
1408
        virReportOOMError();
1409 1410 1411
        return NULL;
    }
    pools->objs[pools->count++] = pool;
1412 1413 1414 1415 1416

    return pool;
}

static virStoragePoolObjPtr
1417
virStoragePoolObjLoad(virStoragePoolObjListPtr pools,
1418 1419 1420 1421 1422 1423
                      const char *file,
                      const char *path,
                      const char *autostartLink) {
    virStoragePoolDefPtr def;
    virStoragePoolObjPtr pool;

1424
    if (!(def = virStoragePoolDefParseFile(path))) {
1425 1426 1427 1428
        return NULL;
    }

    if (!virFileMatchesNameSuffix(file, def->name, ".xml")) {
1429
        virStorageReportError(VIR_ERR_XML_ERROR,
1430
                              _("Storage pool config filename '%s' does not match pool name '%s'"),
1431
                              path, def->name);
1432 1433 1434 1435
        virStoragePoolDefFree(def);
        return NULL;
    }

1436
    if (!(pool = virStoragePoolObjAssignDef(pools, def))) {
1437 1438 1439 1440
        virStoragePoolDefFree(def);
        return NULL;
    }

1441
    VIR_FREE(pool->configFile);  /* for driver reload */
1442 1443
    pool->configFile = strdup(path);
    if (pool->configFile == NULL) {
1444
        virReportOOMError();
1445 1446 1447
        virStoragePoolDefFree(def);
        return NULL;
    }
1448
    VIR_FREE(pool->autostartLink); /* for driver reload */
1449 1450
    pool->autostartLink = strdup(autostartLink);
    if (pool->autostartLink == NULL) {
1451
        virReportOOMError();
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
        virStoragePoolDefFree(def);
        return NULL;
    }

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

    return pool;
}


int
1464
virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
1465 1466
                             const char *configDir,
                             const char *autostartDir) {
1467 1468 1469
    DIR *dir;
    struct dirent *entry;

1470
    if (!(dir = opendir(configDir))) {
1471 1472
        if (errno == ENOENT)
            return 0;
1473
        virReportSystemError(errno, _("Failed to open dir '%s'"),
1474
                             configDir);
1475 1476 1477 1478
        return -1;
    }

    while ((entry = readdir(dir))) {
1479 1480
        char *path;
        char *autostartLink;
1481
        virStoragePoolObjPtr pool;
1482 1483 1484 1485 1486 1487 1488

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

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

1489
        if (!(path = virFileBuildPath(configDir, entry->d_name, NULL)))
1490 1491
            continue;

1492 1493 1494
        if (!(autostartLink = virFileBuildPath(autostartDir, entry->d_name,
                                               NULL))) {
            VIR_FREE(path);
1495 1496 1497
            continue;
        }

1498
        pool = virStoragePoolObjLoad(pools, entry->d_name, path,
1499
                                     autostartLink);
1500 1501
        if (pool)
            virStoragePoolObjUnlock(pool);
1502 1503 1504

        VIR_FREE(path);
        VIR_FREE(autostartLink);
1505 1506 1507 1508 1509 1510 1511 1512
    }

    closedir(dir);

    return 0;
}

int
1513
virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
                         virStoragePoolObjPtr pool,
                         virStoragePoolDefPtr def) {
    char *xml;
    int fd = -1, ret = -1;
    ssize_t towrite;

    if (!pool->configFile) {
        int err;

        if ((err = virFileMakePath(driver->configDir))) {
1524
            virReportSystemError(err,
C
Cole Robinson 已提交
1525 1526
                                 _("cannot create config directory %s"),
                                 driver->configDir);
1527 1528 1529
            return -1;
        }

1530 1531
        if (!(pool->configFile = virFileBuildPath(driver->configDir,
                                                  def->name, ".xml"))) {
1532 1533 1534
            return -1;
        }

1535 1536
        if (!(pool->autostartLink = virFileBuildPath(driver->autostartDir,
                                                     def->name, ".xml"))) {
1537
            VIR_FREE(pool->configFile);
1538 1539 1540 1541
            return -1;
        }
    }

1542 1543
    if (!(xml = virStoragePoolDefFormat(def))) {
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1544
                              "%s", _("failed to generate XML"));
1545 1546 1547 1548 1549 1550
        return -1;
    }

    if ((fd = open(pool->configFile,
                   O_WRONLY | O_CREAT | O_TRUNC,
                   S_IRUSR | S_IWUSR )) < 0) {
1551
        virReportSystemError(errno,
1552 1553
                             _("cannot create config file %s"),
                             pool->configFile);
1554 1555 1556
        goto cleanup;
    }

1557 1558
    virEmitXMLWarning(fd, def->name, "pool-edit");

1559 1560
    towrite = strlen(xml);
    if (safewrite(fd, xml, towrite) != towrite) {
1561
        virReportSystemError(errno,
1562 1563
                             _("cannot write config file %s"),
                             pool->configFile);
1564 1565 1566
        goto cleanup;
    }

1567
    if (VIR_CLOSE(fd) < 0) {
1568
        virReportSystemError(errno,
1569 1570
                             _("cannot save config file %s"),
                             pool->configFile);
1571 1572 1573 1574 1575 1576
        goto cleanup;
    }

    ret = 0;

 cleanup:
1577
    VIR_FORCE_CLOSE(fd);
1578
    VIR_FREE(xml);
1579 1580 1581 1582 1583

    return ret;
}

int
1584
virStoragePoolObjDeleteDef(virStoragePoolObjPtr pool) {
1585
    if (!pool->configFile) {
1586
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1587 1588 1589 1590 1591
                              _("no config file for %s"), pool->def->name);
        return -1;
    }

    if (unlink(pool->configFile) < 0) {
1592
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1593 1594 1595 1596 1597 1598 1599
                              _("cannot remove config for %s"),
                              pool->def->name);
        return -1;
    }

    return 0;
}
1600

1601
virStoragePoolSourcePtr
1602
virStoragePoolSourceListNewSource(virStoragePoolSourceListPtr list)
1603 1604 1605 1606
{
    virStoragePoolSourcePtr source;

    if (VIR_REALLOC_N(list->sources, list->nsources+1) < 0) {
1607
        virReportOOMError();
1608 1609 1610 1611 1612 1613 1614 1615 1616
        return NULL;
    }

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

    return source;
}

1617
char *virStoragePoolSourceListFormat(virStoragePoolSourceListPtr def)
1618
{
1619
    virStoragePoolOptionsPtr options;
1620
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1621 1622
    const char *type;
    int i;
1623

1624
    options = virStoragePoolOptionsForPoolType(def->type);
1625 1626 1627
    if (options == NULL)
        return NULL;

1628
    type = virStoragePoolTypeToString(def->type);
1629
    if (!type) {
1630
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1631 1632 1633 1634 1635
                              "%s", _("unexpected pool type"));
        goto cleanup;
    }

    virBufferAddLit(&buf, "<sources>\n");
1636 1637

    for (i = 0; i < def->nsources; i++) {
1638
        virStoragePoolSourceFormat(&buf, options, &def->sources[i]);
1639 1640
    }

1641 1642 1643 1644
    virBufferAddLit(&buf, "</sources>\n");

    if (virBufferError(&buf))
        goto no_memory;
1645 1646

    return virBufferContentAndReset(&buf);
1647 1648

 no_memory:
1649
    virReportOOMError();
1650
 cleanup:
1651
    virBufferFreeAndReset(&buf);
1652
    return NULL;
1653
}
D
Daniel P. Berrange 已提交
1654 1655


1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
/*
 * 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;
}


1719 1720
void virStoragePoolObjLock(virStoragePoolObjPtr obj)
{
1721
    virMutexLock(&obj->lock);
1722 1723 1724 1725
}

void virStoragePoolObjUnlock(virStoragePoolObjPtr obj)
{
1726
    virMutexUnlock(&obj->lock);
1727
}