storage_conf.c 52.0 KB
Newer Older
1 2 3
/*
 * storage_conf.c: config handling for storage driver
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2006-2011 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
    VIR_FREE(source->host.name);
    for (i = 0 ; i < source->ndevice ; i++) {
        VIR_FREE(source->devices[i].freeExtents);
        VIR_FREE(source->devices[i].path);
283
    }
284 285 286
    VIR_FREE(source->devices);
    VIR_FREE(source->dir);
    VIR_FREE(source->name);
287
    VIR_FREE(source->adapter);
D
David Allan 已提交
288
    VIR_FREE(source->initiator.iqn);
289 290
    VIR_FREE(source->vendor);
    VIR_FREE(source->product);
291

292 293 294
    if (source->authType == VIR_STORAGE_POOL_AUTH_CHAP) {
        VIR_FREE(source->auth.chap.login);
        VIR_FREE(source->auth.chap.passwd);
295
    }
296 297
}

298 299 300 301 302 303 304
void
virStoragePoolSourceFree(virStoragePoolSourcePtr source)
{
    virStoragePoolSourceClear(source);
    VIR_FREE(source);
}

305 306 307 308 309 310 311
void
virStoragePoolDefFree(virStoragePoolDefPtr def) {
    if (!def)
        return;

    VIR_FREE(def->name);

312
    virStoragePoolSourceClear(&def->source);
313

314 315 316
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
    VIR_FREE(def);
317 318 319 320 321
}


void
virStoragePoolObjFree(virStoragePoolObjPtr obj) {
322 323 324
    if (!obj)
        return;

325 326
    virStoragePoolObjClearVols(obj);

327 328
    virStoragePoolDefFree(obj->def);
    virStoragePoolDefFree(obj->newDef);
329

330 331
    VIR_FREE(obj->configFile);
    VIR_FREE(obj->autostartLink);
332 333 334

    virMutexDestroy(&obj->lock);

335
    VIR_FREE(obj);
336 337
}

338 339 340 341 342 343 344 345 346
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;
}

347
void
348
virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
349 350
                        virStoragePoolObjPtr pool)
{
351
    unsigned int i;
352

353 354
    virStoragePoolObjUnlock(pool);

355
    for (i = 0 ; i < pools->count ; i++) {
356
        virStoragePoolObjLock(pools->objs[i]);
357
        if (pools->objs[i] == pool) {
358
            virStoragePoolObjUnlock(pools->objs[i]);
359
            virStoragePoolObjFree(pools->objs[i]);
360

361 362 363
            if (i < (pools->count - 1))
                memmove(pools->objs + i, pools->objs + i + 1,
                        sizeof(*(pools->objs)) * (pools->count - (i + 1)));
364

365 366 367 368
            if (VIR_REALLOC_N(pools->objs, pools->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            pools->count--;
369

370 371
            break;
        }
372
        virStoragePoolObjUnlock(pools->objs[i]);
373
    }
374 375 376 377
}


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

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

    return 0;
}

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

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

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

416
    source->name = virXPathString("string(./name)", ctxt);
417 418

    if (options->formatFromString) {
419
        char *format = virXPathString("string(./format/@type)", ctxt);
420 421 422 423 424 425
        if (format == NULL)
            source->format = options->defaultFormat;
        else
            source->format = options->formatFromString(format);

        if (source->format < 0) {
426
            virStorageReportError(VIR_ERR_XML_ERROR,
427 428 429 430 431 432 433
                                  _("unknown pool format type %s"), format);
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

434
    source->host.name = virXPathString("string(./host/@name)", ctxt);
435 436 437 438 439 440 441 442 443 444 445
    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;
        }
    }


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

448
    nsource = virXPathNodeSet("./device", ctxt, &nodeset);
449 450 451
    if (nsource < 0)
        goto cleanup;

452 453 454
    if (nsource > 0) {
        if (VIR_ALLOC_N(source->devices, nsource) < 0) {
            VIR_FREE(nodeset);
455
            virReportOOMError();
456 457 458 459
            goto cleanup;
        }

        for (i = 0 ; i < nsource ; i++) {
460
            char *path = virXMLPropString(nodeset[i], "path");
461 462
            if (path == NULL) {
                VIR_FREE(nodeset);
463
                virStorageReportError(VIR_ERR_XML_ERROR,
464 465 466
                        "%s", _("missing storage pool source device path"));
                goto cleanup;
            }
467
            source->devices[i].path = path;
468 469 470 471
        }
        source->ndevice = nsource;
    }

472 473
    source->dir = virXPathString("string(./dir/@path)", ctxt);
    source->adapter = virXPathString("string(./adapter/@name)", ctxt);
474

475
    authType = virXPathString("string(./auth/@type)", ctxt);
476 477 478 479 480 481
    if (authType == NULL) {
        source->authType = VIR_STORAGE_POOL_AUTH_NONE;
    } else {
        if (STREQ(authType, "chap")) {
            source->authType = VIR_STORAGE_POOL_AUTH_CHAP;
        } else {
482
            virStorageReportError(VIR_ERR_XML_ERROR,
483 484 485 486 487 488 489
                                  _("unknown auth type '%s'"),
                                  (const char *)authType);
            goto cleanup;
        }
    }

    if (source->authType == VIR_STORAGE_POOL_AUTH_CHAP) {
490
        if (virStoragePoolDefParseAuthChap(ctxt, &source->auth.chap) < 0)
491 492 493
            goto cleanup;
    }

494 495 496
    source->vendor = virXPathString("string(./vendor/@name)", ctxt);
    source->product = virXPathString("string(./product/@name)", ctxt);

497 498 499 500
    ret = 0;
cleanup:
    ctxt->node = relnode;

501
    VIR_FREE(port);
502 503 504 505
    VIR_FREE(authType);
    VIR_FREE(nodeset);
    return ret;
}
506

507
virStoragePoolSourcePtr
508
virStoragePoolDefParseSourceString(const char *srcSpec,
509 510 511 512 513 514 515
                                   int pool_type)
{
    xmlDocPtr doc = NULL;
    xmlNodePtr node = NULL;
    xmlXPathContextPtr xpath_ctxt = NULL;
    virStoragePoolSourcePtr def = NULL, ret = NULL;

516
    if (!(doc = virXMLParseStringCtxt(srcSpec, _("(storage_source_specification)"), &xpath_ctxt))) {
517 518 519 520
        goto cleanup;
    }

    if (VIR_ALLOC(def) < 0) {
521
        virReportOOMError();
522 523 524
        goto cleanup;
    }

525
    node = virXPathNode("/source", xpath_ctxt);
526
    if (!node) {
527
        virStorageReportError(VIR_ERR_XML_ERROR,
528 529 530 531
                              "%s", _("root element was not source"));
        goto cleanup;
    }

532
    if (virStoragePoolDefParseSource(xpath_ctxt, def, pool_type,
533 534 535 536 537 538
                                     node) < 0)
        goto cleanup;

    ret = def;
    def = NULL;
cleanup:
539
    virStoragePoolSourceFree(def);
540 541 542 543 544
    xmlFreeDoc(doc);
    xmlXPathFreeContext(xpath_ctxt);

    return ret;
}
545
static int
546
virStorageDefParsePerms(xmlXPathContextPtr ctxt,
547 548 549
                        virStoragePermsPtr perms,
                        const char *permxpath,
                        int defaultmode) {
550 551
    char *mode;
    long v;
552 553 554
    int ret = -1;
    xmlNodePtr relnode;
    xmlNodePtr node;
555

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

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

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

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

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

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

609 610 611 612
    ret = 0;
error:
    ctxt->node = relnode;
    return ret;
613 614 615
}

static virStoragePoolDefPtr
616
virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) {
617
    virStoragePoolOptionsPtr options;
618
    virStoragePoolDefPtr ret;
619
    xmlNodePtr source_node;
620
    char *type = NULL;
621
    char *uuid = NULL;
622
    char *tmppath;
623

624
    if (VIR_ALLOC(ret) < 0) {
625
        virReportOOMError();
626
        return NULL;
627
    }
628

629
    type = virXPathString("string(./@type)", ctxt);
630
    if ((ret->type = virStoragePoolTypeFromString((const char *)type)) < 0) {
631
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
632
                              _("unknown storage pool type %s"), (const char*)type);
633
        goto cleanup;
634 635
    }

636 637 638
    xmlFree(type);
    type = NULL;

639
    if ((options = virStoragePoolOptionsForPoolType(ret->type)) == NULL) {
640 641 642
        goto cleanup;
    }

643
    source_node = virXPathNode("./source", ctxt);
644
    if (source_node) {
645
        if (virStoragePoolDefParseSource(ctxt, &ret->source, ret->type,
646 647 648 649
                                         source_node) < 0)
            goto cleanup;
    }

650
    ret->name = virXPathString("string(./name)", ctxt);
651
    if (ret->name == NULL &&
652
        options->flags & VIR_STORAGE_POOL_SOURCE_NAME)
653
        ret->name = ret->source.name;
654
    if (ret->name == NULL) {
655
        virStorageReportError(VIR_ERR_XML_ERROR,
656
                              "%s", _("missing pool source name element"));
657 658 659
        goto cleanup;
    }

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

676
    if (options->flags & VIR_STORAGE_POOL_SOURCE_HOST) {
677
        if (!ret->source.host.name) {
678
            virStorageReportError(VIR_ERR_XML_ERROR,
679 680
                                  "%s",
                                  _("missing storage pool source host name"));
681 682 683 684
            goto cleanup;
        }
    }

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

703
    if (options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) {
704
        if (!ret->source.adapter) {
705
            virStorageReportError(VIR_ERR_XML_ERROR,
706
                                  "%s", _("missing storage pool source adapter name"));
707 708 709
            goto cleanup;
        }
    }
710

711 712 713 714 715 716 717 718 719
    /* 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;
        }
    }

720
    if ((tmppath = virXPathString("string(./target/path)", ctxt)) == NULL) {
721
        virStorageReportError(VIR_ERR_XML_ERROR,
722
                              "%s", _("missing storage pool target path"));
723 724
        goto cleanup;
    }
725 726 727 728 729
    ret->target.path = virFileSanitizePath(tmppath);
    VIR_FREE(tmppath);
    if (!ret->target.path)
        goto cleanup;

730

731
    if (virStorageDefParsePerms(ctxt, &ret->target.perms,
732
                                "./target/permissions", 0700) < 0)
733 734 735 736 737
        goto cleanup;

    return ret;

 cleanup:
738
    VIR_FREE(uuid);
739
    xmlFree(type);
740 741 742 743 744
    virStoragePoolDefFree(ret);
    return NULL;
}

virStoragePoolDefPtr
745
virStoragePoolDefParseNode(xmlDocPtr xml,
746 747 748 749 750
                           xmlNodePtr root) {
    xmlXPathContextPtr ctxt = NULL;
    virStoragePoolDefPtr def = NULL;

    if (STRNEQ((const char *)root->name, "pool")) {
751 752
        virStorageReportError(VIR_ERR_XML_ERROR,
                              "%s", _("unknown root element for storage pool"));
753 754 755 756 757
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
758
        virReportOOMError();
759 760 761 762
        goto cleanup;
    }

    ctxt->node = root;
763
    def = virStoragePoolDefParseXML(ctxt);
764 765 766 767 768 769
cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

static virStoragePoolDefPtr
770
virStoragePoolDefParse(const char *xmlStr,
771 772
                       const char *filename) {
    virStoragePoolDefPtr ret = NULL;
J
Jiri Denemark 已提交
773
    xmlDocPtr xml;
774

775
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_pool_definition)")))) {
J
Jiri Denemark 已提交
776 777
        ret = virStoragePoolDefParseNode(xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
778 779
    }

780 781 782
    return ret;
}

783
virStoragePoolDefPtr
784
virStoragePoolDefParseString(const char *xmlStr)
785
{
786
    return virStoragePoolDefParse(xmlStr, NULL);
787 788 789
}

virStoragePoolDefPtr
790
virStoragePoolDefParseFile(const char *filename)
791
{
792
    return virStoragePoolDefParse(NULL, filename);
793 794
}

795
static int
796
virStoragePoolSourceFormat(virBufferPtr buf,
797
                           virStoragePoolOptionsPtr options,
798 799 800 801 802
                           virStoragePoolSourcePtr src)
{
    int i, j;

    virBufferAddLit(buf,"  <source>\n");
803
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_HOST) &&
804
        src->host.name) {
805
        virBufferAsprintf(buf, "    <host name='%s'", src->host.name);
806
        if (src->host.port)
807
            virBufferAsprintf(buf, " port='%d'", src->host.port);
808 809
        virBufferAddLit(buf, "/>\n");
    }
810

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

D
David Allan 已提交
839 840 841 842 843 844 845
    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");
    }

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


    if (src->authType == VIR_STORAGE_POOL_AUTH_CHAP)
859
        virBufferAsprintf(buf,"    <auth type='chap' login='%s' passwd='%s'/>\n",
860 861
                          src->auth.chap.login,
                          src->auth.chap.passwd);
862 863 864 865 866 867 868 869 870

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

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

871 872 873 874 875
    virBufferAddLit(buf,"  </source>\n");

    return 0;
}

876 877

char *
878
virStoragePoolDefFormat(virStoragePoolDefPtr def) {
879
    virStoragePoolOptionsPtr options;
880
    virBuffer buf = VIR_BUFFER_INITIALIZER;
881 882 883
    const char *type;
    char uuid[VIR_UUID_STRING_BUFLEN];

884
    options = virStoragePoolOptionsForPoolType(def->type);
885 886 887
    if (options == NULL)
        return NULL;

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

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

900
    virBufferAsprintf(&buf,"  <capacity>%llu</capacity>\n",
901
                      def->capacity);
902
    virBufferAsprintf(&buf,"  <allocation>%llu</allocation>\n",
903
                      def->allocation);
904
    virBufferAsprintf(&buf,"  <available>%llu</available>\n",
905
                      def->available);
906

907
    if (virStoragePoolSourceFormat(&buf, options, &def->source) < 0)
908
        goto cleanup;
909

910
    virBufferAddLit(&buf,"  <target>\n");
911

912
    if (def->target.path)
913
        virBufferAsprintf(&buf,"    <path>%s</path>\n", def->target.path);
914

915
    virBufferAddLit(&buf,"    <permissions>\n");
916
    virBufferAsprintf(&buf,"      <mode>0%o</mode>\n",
917
                      def->target.perms.mode);
918
    virBufferAsprintf(&buf,"      <owner>%d</owner>\n",
919
                      def->target.perms.uid);
920
    virBufferAsprintf(&buf,"      <group>%d</group>\n",
921
                      def->target.perms.gid);
922

923
    if (def->target.perms.label)
924
        virBufferAsprintf(&buf,"      <label>%s</label>\n",
925
                          def->target.perms.label);
926

927 928 929
    virBufferAddLit(&buf,"    </permissions>\n");
    virBufferAddLit(&buf,"  </target>\n");
    virBufferAddLit(&buf,"</pool>\n");
930

931
    if (virBufferError(&buf))
932 933
        goto no_memory;

934
    return virBufferContentAndReset(&buf);
935 936

 no_memory:
937
    virReportOOMError();
938
 cleanup:
939
    virBufferFreeAndReset(&buf);
940 941 942 943 944
    return NULL;
}


static int
945
virStorageSize(const char *unit,
946 947 948 949 950 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
               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;

980 981
        case 'e':
        case 'E':
982 983 984 985 986
            mult = 1024ull * 1024ull * 1024ull * 1024ull * 1024ull *
                1024ull;
            break;

        default:
987
            virStorageReportError(VIR_ERR_XML_ERROR,
988 989 990 991 992 993
                                  _("unknown size units '%s'"), unit);
            return -1;
        }
    }

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

    *ret *= mult;

    return 0;
}

static virStorageVolDefPtr
1010
virStorageVolDefParseXML(virStoragePoolDefPtr pool,
1011
                         xmlXPathContextPtr ctxt) {
1012
    virStorageVolDefPtr ret;
1013
    virStorageVolOptionsPtr options;
1014 1015 1016
    char *allocation = NULL;
    char *capacity = NULL;
    char *unit = NULL;
1017
    xmlNodePtr node;
1018

1019
    options = virStorageVolOptionsForPoolType(pool->type);
1020 1021 1022
    if (options == NULL)
        return NULL;

1023
    if (VIR_ALLOC(ret) < 0) {
1024
        virReportOOMError();
1025
        return NULL;
1026
    }
1027

1028
    ret->name = virXPathString("string(./name)", ctxt);
1029
    if (ret->name == NULL) {
1030
        virStorageReportError(VIR_ERR_XML_ERROR,
1031
                              "%s", _("missing volume name element"));
1032 1033 1034
        goto cleanup;
    }

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

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

1050
    allocation = virXPathString("string(./allocation)", ctxt);
1051
    if (allocation) {
1052
        unit = virXPathString("string(./allocation/@unit)", ctxt);
1053
        if (virStorageSize(unit, allocation, &ret->allocation) < 0)
1054
            goto cleanup;
1055 1056
        VIR_FREE(allocation);
        VIR_FREE(unit);
1057 1058 1059 1060
    } else {
        ret->allocation = ret->capacity;
    }

1061
    ret->target.path = virXPathString("string(./target/path)", ctxt);
1062
    if (options->formatFromString) {
1063
        char *format = virXPathString("string(./target/format/@type)", ctxt);
1064 1065 1066 1067 1068 1069
        if (format == NULL)
            ret->target.format = options->defaultFormat;
        else
            ret->target.format = (options->formatFromString)(format);

        if (ret->target.format < 0) {
1070
            virStorageReportError(VIR_ERR_XML_ERROR,
1071
                                  _("unknown volume format type %s"), format);
1072
            VIR_FREE(format);
1073 1074
            goto cleanup;
        }
1075
        VIR_FREE(format);
1076 1077
    }

1078
    if (virStorageDefParsePerms(ctxt, &ret->target.perms,
1079
                                "./target/permissions", 0600) < 0)
1080 1081
        goto cleanup;

1082
    node = virXPathNode("./target/encryption", ctxt);
1083
    if (node != NULL) {
1084
        ret->target.encryption = virStorageEncryptionParseNode(ctxt->doc,
1085 1086 1087 1088 1089
                                                               node);
        if (ret->target.encryption == NULL)
            goto cleanup;
    }

1090 1091


1092
    ret->backingStore.path = virXPathString("string(./backingStore/path)", ctxt);
1093
    if (options->formatFromString) {
1094
        char *format = virXPathString("string(./backingStore/format/@type)", ctxt);
1095 1096 1097 1098 1099 1100
        if (format == NULL)
            ret->backingStore.format = options->defaultFormat;
        else
            ret->backingStore.format = (options->formatFromString)(format);

        if (ret->backingStore.format < 0) {
1101
            virStorageReportError(VIR_ERR_XML_ERROR,
1102 1103 1104 1105 1106 1107 1108
                                  _("unknown volume format type %s"), format);
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

1109
    if (virStorageDefParsePerms(ctxt, &ret->backingStore.perms,
1110
                                "./backingStore/permissions", 0600) < 0)
1111 1112
        goto cleanup;

1113 1114 1115
    return ret;

 cleanup:
1116 1117 1118
    VIR_FREE(allocation);
    VIR_FREE(capacity);
    VIR_FREE(unit);
1119 1120 1121 1122 1123
    virStorageVolDefFree(ret);
    return NULL;
}

virStorageVolDefPtr
1124
virStorageVolDefParseNode(virStoragePoolDefPtr pool,
1125 1126 1127 1128 1129 1130
                          xmlDocPtr xml,
                          xmlNodePtr root) {
    xmlXPathContextPtr ctxt = NULL;
    virStorageVolDefPtr def = NULL;

    if (STRNEQ((const char *)root->name, "volume")) {
1131
        virStorageReportError(VIR_ERR_XML_ERROR,
1132 1133 1134 1135 1136 1137
                          "%s", _("unknown root element for storage vol"));
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1138
        virReportOOMError();
1139 1140 1141 1142
        goto cleanup;
    }

    ctxt->node = root;
1143
    def = virStorageVolDefParseXML(pool, ctxt);
1144 1145 1146 1147 1148 1149
cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

static virStorageVolDefPtr
1150
virStorageVolDefParse(virStoragePoolDefPtr pool,
1151 1152 1153
                      const char *xmlStr,
                      const char *filename) {
    virStorageVolDefPtr ret = NULL;
J
Jiri Denemark 已提交
1154
    xmlDocPtr xml;
1155

1156
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_volume_definition)")))) {
J
Jiri Denemark 已提交
1157 1158
        ret = virStorageVolDefParseNode(pool, xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
1159 1160 1161 1162 1163
    }

    return ret;
}

1164
virStorageVolDefPtr
1165
virStorageVolDefParseString(virStoragePoolDefPtr pool,
1166 1167
                            const char *xmlStr)
{
1168
    return virStorageVolDefParse(pool, xmlStr, NULL);
1169 1170 1171
}

virStorageVolDefPtr
1172
virStorageVolDefParseFile(virStoragePoolDefPtr pool,
1173 1174
                          const char *filename)
{
1175
    return virStorageVolDefParse(pool, NULL, filename);
1176
}
1177

1178
static int
1179
virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
1180 1181 1182
                             virBufferPtr buf,
                             virStorageVolTargetPtr def,
                             const char *type) {
1183
    virBufferAsprintf(buf, "  <%s>\n", type);
1184 1185

    if (def->path)
1186
        virBufferAsprintf(buf,"    <path>%s</path>\n", def->path);
1187 1188 1189 1190

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

    virBufferAddLit(buf,"    <permissions>\n");
1200
    virBufferAsprintf(buf,"      <mode>0%o</mode>\n",
1201
                      def->perms.mode);
1202
    virBufferAsprintf(buf,"      <owner>%d</owner>\n",
1203
                      def->perms.uid);
1204
    virBufferAsprintf(buf,"      <group>%d</group>\n",
1205 1206 1207 1208
                      def->perms.gid);


    if (def->perms.label)
1209
        virBufferAsprintf(buf,"      <label>%s</label>\n",
1210 1211 1212 1213
                          def->perms.label);

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

1214 1215 1216 1217 1218 1219
    if (def->encryption) {
        virBufferAdjustIndent(buf, 4);
        if (virStorageEncryptionFormat(buf, def->encryption) < 0)
            return -1;
        virBufferAdjustIndent(buf, -4);
    }
1220

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

    return 0;
}
1225 1226

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

1232
    options = virStorageVolOptionsForPoolType(pool->type);
1233 1234 1235
    if (options == NULL)
        return NULL;

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

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

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

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

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

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

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

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

1281
    if (virBufferError(&buf))
1282 1283
        goto no_memory;

1284
    return virBufferContentAndReset(&buf);
1285 1286

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


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

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

    return NULL;
}

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

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

    return NULL;
}

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
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;
}

1339 1340 1341
void
virStoragePoolObjClearVols(virStoragePoolObjPtr pool)
{
1342 1343 1344 1345 1346 1347
    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;
1348 1349 1350 1351 1352
}

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

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

    return NULL;
}

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

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

    return NULL;
}

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

1379 1380 1381
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->name, name))
            return pool->volumes.objs[i];
1382 1383 1384 1385 1386

    return NULL;
}

virStoragePoolObjPtr
1387
virStoragePoolObjAssignDef(virStoragePoolObjListPtr pools,
1388 1389 1390
                           virStoragePoolDefPtr def) {
    virStoragePoolObjPtr pool;

1391
    if ((pool = virStoragePoolObjFindByName(pools, def->name))) {
1392 1393 1394 1395
        if (!virStoragePoolObjIsActive(pool)) {
            virStoragePoolDefFree(pool->def);
            pool->def = def;
        } else {
1396
            virStoragePoolDefFree(pool->newDef);
1397 1398 1399 1400 1401
            pool->newDef = def;
        }
        return pool;
    }

1402
    if (VIR_ALLOC(pool) < 0) {
1403
        virReportOOMError();
1404 1405 1406
        return NULL;
    }

1407
    if (virMutexInit(&pool->lock) < 0) {
1408
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1409 1410 1411 1412
                              "%s", _("cannot initialize mutex"));
        VIR_FREE(pool);
        return NULL;
    }
1413
    virStoragePoolObjLock(pool);
1414 1415 1416
    pool->active = 0;
    pool->def = def;

1417 1418
    if (VIR_REALLOC_N(pools->objs, pools->count+1) < 0) {
        pool->def = NULL;
1419
        virStoragePoolObjUnlock(pool);
1420
        virStoragePoolObjFree(pool);
1421
        virReportOOMError();
1422 1423 1424
        return NULL;
    }
    pools->objs[pools->count++] = pool;
1425 1426 1427 1428 1429

    return pool;
}

static virStoragePoolObjPtr
1430
virStoragePoolObjLoad(virStoragePoolObjListPtr pools,
1431 1432 1433 1434 1435 1436
                      const char *file,
                      const char *path,
                      const char *autostartLink) {
    virStoragePoolDefPtr def;
    virStoragePoolObjPtr pool;

1437
    if (!(def = virStoragePoolDefParseFile(path))) {
1438 1439 1440 1441
        return NULL;
    }

    if (!virFileMatchesNameSuffix(file, def->name, ".xml")) {
1442
        virStorageReportError(VIR_ERR_XML_ERROR,
1443
                              _("Storage pool config filename '%s' does not match pool name '%s'"),
1444
                              path, def->name);
1445 1446 1447 1448
        virStoragePoolDefFree(def);
        return NULL;
    }

1449
    if (!(pool = virStoragePoolObjAssignDef(pools, def))) {
1450 1451 1452 1453
        virStoragePoolDefFree(def);
        return NULL;
    }

1454
    VIR_FREE(pool->configFile);  /* for driver reload */
1455 1456
    pool->configFile = strdup(path);
    if (pool->configFile == NULL) {
1457
        virReportOOMError();
1458 1459 1460
        virStoragePoolDefFree(def);
        return NULL;
    }
1461
    VIR_FREE(pool->autostartLink); /* for driver reload */
1462 1463
    pool->autostartLink = strdup(autostartLink);
    if (pool->autostartLink == NULL) {
1464
        virReportOOMError();
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
        virStoragePoolDefFree(def);
        return NULL;
    }

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

    return pool;
}


int
1477
virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
1478 1479
                             const char *configDir,
                             const char *autostartDir) {
1480 1481 1482
    DIR *dir;
    struct dirent *entry;

1483
    if (!(dir = opendir(configDir))) {
1484 1485
        if (errno == ENOENT)
            return 0;
1486
        virReportSystemError(errno, _("Failed to open dir '%s'"),
1487
                             configDir);
1488 1489 1490 1491
        return -1;
    }

    while ((entry = readdir(dir))) {
1492 1493
        char *path;
        char *autostartLink;
1494
        virStoragePoolObjPtr pool;
1495 1496 1497 1498 1499 1500 1501

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

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

1502
        if (!(path = virFileBuildPath(configDir, entry->d_name, NULL)))
1503 1504
            continue;

1505 1506 1507
        if (!(autostartLink = virFileBuildPath(autostartDir, entry->d_name,
                                               NULL))) {
            VIR_FREE(path);
1508 1509 1510
            continue;
        }

1511
        pool = virStoragePoolObjLoad(pools, entry->d_name, path,
1512
                                     autostartLink);
1513 1514
        if (pool)
            virStoragePoolObjUnlock(pool);
1515 1516 1517

        VIR_FREE(path);
        VIR_FREE(autostartLink);
1518 1519 1520 1521 1522 1523 1524 1525
    }

    closedir(dir);

    return 0;
}

int
1526
virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
1527
                         virStoragePoolObjPtr pool,
1528 1529
                         virStoragePoolDefPtr def)
{
1530
    char *xml;
1531
    int ret = -1;
1532 1533

    if (!pool->configFile) {
1534 1535
        if (virFileMakePath(driver->configDir) < 0) {
            virReportSystemError(errno,
C
Cole Robinson 已提交
1536 1537
                                 _("cannot create config directory %s"),
                                 driver->configDir);
1538 1539 1540
            return -1;
        }

1541 1542
        if (!(pool->configFile = virFileBuildPath(driver->configDir,
                                                  def->name, ".xml"))) {
1543 1544 1545
            return -1;
        }

1546 1547
        if (!(pool->autostartLink = virFileBuildPath(driver->autostartDir,
                                                     def->name, ".xml"))) {
1548
            VIR_FREE(pool->configFile);
1549 1550 1551 1552
            return -1;
        }
    }

1553 1554
    if (!(xml = virStoragePoolDefFormat(def))) {
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1555
                              "%s", _("failed to generate XML"));
1556 1557 1558
        return -1;
    }

1559
    ret = virXMLSaveFile(pool->configFile, def->name, "pool-edit", xml);
1560
    VIR_FREE(xml);
1561 1562 1563 1564 1565

    return ret;
}

int
1566
virStoragePoolObjDeleteDef(virStoragePoolObjPtr pool) {
1567
    if (!pool->configFile) {
1568
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1569 1570 1571 1572 1573
                              _("no config file for %s"), pool->def->name);
        return -1;
    }

    if (unlink(pool->configFile) < 0) {
1574
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1575 1576 1577 1578 1579 1580 1581
                              _("cannot remove config for %s"),
                              pool->def->name);
        return -1;
    }

    return 0;
}
1582

1583
virStoragePoolSourcePtr
1584
virStoragePoolSourceListNewSource(virStoragePoolSourceListPtr list)
1585 1586 1587 1588
{
    virStoragePoolSourcePtr source;

    if (VIR_REALLOC_N(list->sources, list->nsources+1) < 0) {
1589
        virReportOOMError();
1590 1591 1592 1593 1594 1595 1596 1597 1598
        return NULL;
    }

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

    return source;
}

1599
char *virStoragePoolSourceListFormat(virStoragePoolSourceListPtr def)
1600
{
1601
    virStoragePoolOptionsPtr options;
1602
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1603 1604
    const char *type;
    int i;
1605

1606
    options = virStoragePoolOptionsForPoolType(def->type);
1607 1608 1609
    if (options == NULL)
        return NULL;

1610
    type = virStoragePoolTypeToString(def->type);
1611
    if (!type) {
1612
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1613 1614 1615 1616 1617
                              "%s", _("unexpected pool type"));
        goto cleanup;
    }

    virBufferAddLit(&buf, "<sources>\n");
1618 1619

    for (i = 0; i < def->nsources; i++) {
1620
        virStoragePoolSourceFormat(&buf, options, &def->sources[i]);
1621 1622
    }

1623 1624 1625 1626
    virBufferAddLit(&buf, "</sources>\n");

    if (virBufferError(&buf))
        goto no_memory;
1627 1628

    return virBufferContentAndReset(&buf);
1629 1630

 no_memory:
1631
    virReportOOMError();
1632
 cleanup:
1633
    virBufferFreeAndReset(&buf);
1634
    return NULL;
1635
}
D
Daniel P. Berrange 已提交
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 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
/*
 * 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;
}

1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
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;

1714 1715 1716 1717
        /* Don't mach against ourself if re-defining existing pool ! */
        if (STREQ(pool->def->name, def->name))
            continue;

1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
        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)) \
                && (STREQ(pool->def->source.host.name, def->source.host.name)))
                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) {
                if (STREQ(matchpool->def->source.host.name, def->source.host.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;
                }
                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);
    }

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

1770 1771
void virStoragePoolObjLock(virStoragePoolObjPtr obj)
{
1772
    virMutexLock(&obj->lock);
1773 1774 1775 1776
}

void virStoragePoolObjUnlock(virStoragePoolObjPtr obj)
{
1777
    virMutexUnlock(&obj->lock);
1778
}