storage_conf.c 51.1 KB
Newer Older
1 2 3
/*
 * storage_conf.c: config handling for storage driver
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2006-2012 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * Copyright (C) 2006-2008 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

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

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

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

48 49
#define VIR_FROM_THIS VIR_FROM_STORAGE

50

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

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

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

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

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


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

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

typedef const char *(*virStorageVolFormatToString)(int format);
typedef int (*virStorageVolFormatFromString)(const char *format);

typedef const char *(*virStoragePoolFormatToString)(int format);
typedef int (*virStoragePoolFormatFromString)(const char *format);

typedef struct _virStorageVolOptions virStorageVolOptions;
typedef virStorageVolOptions *virStorageVolOptionsPtr;
struct _virStorageVolOptions {
100
    int defaultFormat;
101 102 103 104 105 106
    virStorageVolFormatToString formatToString;
    virStorageVolFormatFromString formatFromString;
};

/* Flags to indicate mandatory components in the pool source */
enum {
D
David Allan 已提交
107 108 109 110 111 112
    VIR_STORAGE_POOL_SOURCE_HOST            = (1<<0),
    VIR_STORAGE_POOL_SOURCE_DEVICE          = (1<<1),
    VIR_STORAGE_POOL_SOURCE_DIR             = (1<<2),
    VIR_STORAGE_POOL_SOURCE_ADAPTER         = (1<<3),
    VIR_STORAGE_POOL_SOURCE_NAME            = (1<<4),
    VIR_STORAGE_POOL_SOURCE_INITIATOR_IQN   = (1<<5),
113 114 115 116 117 118 119
};



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

typedef struct _virStoragePoolTypeInfo virStoragePoolTypeInfo;
typedef virStoragePoolTypeInfo *virStoragePoolTypeInfoPtr;

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

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


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

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

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

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


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

    if (!def)
        return;

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

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

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

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

276
    if (!source)
277 278
        return;

279 280 281 282
    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 574 575
        int tmp;

        if (virStrToLong_i(mode, NULL, 8, &tmp) < 0 || (tmp & ~0777)) {
576
            VIR_FREE(mode);
577
            virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
578
                                  "%s", _("malformed octal mode"));
579
            goto error;
580
        }
581
        perms->mode = tmp;
582
        VIR_FREE(mode);
583 584
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

731

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

    return ret;

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

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

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

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

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

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

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

781 782 783
    return ret;
}

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

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

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

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

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

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

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


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

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

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

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

    return 0;
}

877 878

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

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

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

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

E
Eric Blake 已提交
901
    virBufferAsprintf(&buf,"  <capacity unit='bytes'>%llu</capacity>\n",
902
                      def->capacity);
E
Eric Blake 已提交
903
    virBufferAsprintf(&buf,"  <allocation unit='bytes'>%llu</allocation>\n",
904
                      def->allocation);
E
Eric Blake 已提交
905
    virBufferAsprintf(&buf,"  <available unit='bytes'>%llu</available>\n",
906
                      def->available);
907

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

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

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

916
    virBufferAddLit(&buf,"    <permissions>\n");
917
    virBufferAsprintf(&buf,"      <mode>0%o</mode>\n",
918
                      def->target.perms.mode);
E
Eric Blake 已提交
919 920 921 922
    virBufferAsprintf(&buf,"      <owner>%u</owner>\n",
                      (unsigned int) def->target.perms.uid);
    virBufferAsprintf(&buf,"      <group>%u</group>\n",
                      (unsigned int) def->target.perms.gid);
923

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

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

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

935
    return virBufferContentAndReset(&buf);
936 937

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


static int
946
virStorageSize(const char *unit,
947
               const char *val,
948 949 950
               unsigned long long *ret)
{
    if (virStrToLong_ull(val, NULL, 10, ret) < 0) {
951
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
952
                              "%s", _("malformed capacity element"));
953 954
        return -1;
    }
955 956 957 958
    /* off_t is signed, so you cannot create a file larger than 2**63
     * bytes in the first place.  */
    if (virScaleInteger(ret, unit, 1, LLONG_MAX) < 0)
        return -1;
959 960 961 962 963

    return 0;
}

static virStorageVolDefPtr
964
virStorageVolDefParseXML(virStoragePoolDefPtr pool,
965
                         xmlXPathContextPtr ctxt) {
966
    virStorageVolDefPtr ret;
967
    virStorageVolOptionsPtr options;
968 969 970
    char *allocation = NULL;
    char *capacity = NULL;
    char *unit = NULL;
971
    xmlNodePtr node;
972

973
    options = virStorageVolOptionsForPoolType(pool->type);
974 975 976
    if (options == NULL)
        return NULL;

977
    if (VIR_ALLOC(ret) < 0) {
978
        virReportOOMError();
979
        return NULL;
980
    }
981

982
    ret->name = virXPathString("string(./name)", ctxt);
983
    if (ret->name == NULL) {
984
        virStorageReportError(VIR_ERR_XML_ERROR,
985
                              "%s", _("missing volume name element"));
986 987 988
        goto cleanup;
    }

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

992 993
    capacity = virXPathString("string(./capacity)", ctxt);
    unit = virXPathString("string(./capacity/@unit)", ctxt);
994
    if (capacity == NULL) {
995
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
996
                              "%s", _("missing capacity element"));
997 998
        goto cleanup;
    }
999
    if (virStorageSize(unit, capacity, &ret->capacity) < 0)
1000
        goto cleanup;
1001 1002
    VIR_FREE(capacity);
    VIR_FREE(unit);
1003

1004
    allocation = virXPathString("string(./allocation)", ctxt);
1005
    if (allocation) {
1006
        unit = virXPathString("string(./allocation/@unit)", ctxt);
1007
        if (virStorageSize(unit, allocation, &ret->allocation) < 0)
1008
            goto cleanup;
1009 1010
        VIR_FREE(allocation);
        VIR_FREE(unit);
1011 1012 1013 1014
    } else {
        ret->allocation = ret->capacity;
    }

1015
    ret->target.path = virXPathString("string(./target/path)", ctxt);
1016
    if (options->formatFromString) {
1017
        char *format = virXPathString("string(./target/format/@type)", ctxt);
1018 1019 1020 1021 1022 1023
        if (format == NULL)
            ret->target.format = options->defaultFormat;
        else
            ret->target.format = (options->formatFromString)(format);

        if (ret->target.format < 0) {
1024
            virStorageReportError(VIR_ERR_XML_ERROR,
1025
                                  _("unknown volume format type %s"), format);
1026
            VIR_FREE(format);
1027 1028
            goto cleanup;
        }
1029
        VIR_FREE(format);
1030 1031
    }

1032
    if (virStorageDefParsePerms(ctxt, &ret->target.perms,
1033
                                "./target/permissions", 0600) < 0)
1034 1035
        goto cleanup;

1036
    node = virXPathNode("./target/encryption", ctxt);
1037
    if (node != NULL) {
1038
        ret->target.encryption = virStorageEncryptionParseNode(ctxt->doc,
1039 1040 1041 1042 1043
                                                               node);
        if (ret->target.encryption == NULL)
            goto cleanup;
    }

1044 1045


1046
    ret->backingStore.path = virXPathString("string(./backingStore/path)", ctxt);
1047
    if (options->formatFromString) {
1048
        char *format = virXPathString("string(./backingStore/format/@type)", ctxt);
1049 1050 1051 1052 1053 1054
        if (format == NULL)
            ret->backingStore.format = options->defaultFormat;
        else
            ret->backingStore.format = (options->formatFromString)(format);

        if (ret->backingStore.format < 0) {
1055
            virStorageReportError(VIR_ERR_XML_ERROR,
1056 1057 1058 1059 1060 1061 1062
                                  _("unknown volume format type %s"), format);
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

1063
    if (virStorageDefParsePerms(ctxt, &ret->backingStore.perms,
1064
                                "./backingStore/permissions", 0600) < 0)
1065 1066
        goto cleanup;

1067 1068 1069
    return ret;

 cleanup:
1070 1071 1072
    VIR_FREE(allocation);
    VIR_FREE(capacity);
    VIR_FREE(unit);
1073 1074 1075 1076 1077
    virStorageVolDefFree(ret);
    return NULL;
}

virStorageVolDefPtr
1078
virStorageVolDefParseNode(virStoragePoolDefPtr pool,
1079 1080 1081 1082 1083 1084
                          xmlDocPtr xml,
                          xmlNodePtr root) {
    xmlXPathContextPtr ctxt = NULL;
    virStorageVolDefPtr def = NULL;

    if (STRNEQ((const char *)root->name, "volume")) {
1085
        virStorageReportError(VIR_ERR_XML_ERROR,
1086 1087 1088 1089 1090 1091
                          "%s", _("unknown root element for storage vol"));
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1092
        virReportOOMError();
1093 1094 1095 1096
        goto cleanup;
    }

    ctxt->node = root;
1097
    def = virStorageVolDefParseXML(pool, ctxt);
1098 1099 1100 1101 1102 1103
cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

static virStorageVolDefPtr
1104
virStorageVolDefParse(virStoragePoolDefPtr pool,
1105 1106 1107
                      const char *xmlStr,
                      const char *filename) {
    virStorageVolDefPtr ret = NULL;
J
Jiri Denemark 已提交
1108
    xmlDocPtr xml;
1109

1110
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_volume_definition)")))) {
J
Jiri Denemark 已提交
1111 1112
        ret = virStorageVolDefParseNode(pool, xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
1113 1114 1115 1116 1117
    }

    return ret;
}

1118
virStorageVolDefPtr
1119
virStorageVolDefParseString(virStoragePoolDefPtr pool,
1120 1121
                            const char *xmlStr)
{
1122
    return virStorageVolDefParse(pool, xmlStr, NULL);
1123 1124 1125
}

virStorageVolDefPtr
1126
virStorageVolDefParseFile(virStoragePoolDefPtr pool,
1127 1128
                          const char *filename)
{
1129
    return virStorageVolDefParse(pool, NULL, filename);
1130
}
1131

1132
static int
1133
virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
1134 1135 1136
                             virBufferPtr buf,
                             virStorageVolTargetPtr def,
                             const char *type) {
1137
    virBufferAsprintf(buf, "  <%s>\n", type);
1138 1139

    if (def->path)
1140
        virBufferAsprintf(buf,"    <path>%s</path>\n", def->path);
1141 1142 1143 1144

    if (options->formatToString) {
        const char *format = (options->formatToString)(def->format);
        if (!format) {
1145
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1146 1147 1148 1149
                                  _("unknown volume format number %d"),
                                  def->format);
            return -1;
        }
1150
        virBufferAsprintf(buf,"    <format type='%s'/>\n", format);
1151 1152 1153
    }

    virBufferAddLit(buf,"    <permissions>\n");
1154
    virBufferAsprintf(buf,"      <mode>0%o</mode>\n",
1155
                      def->perms.mode);
E
Eric Blake 已提交
1156 1157 1158 1159
    virBufferAsprintf(buf,"      <owner>%u</owner>\n",
                      (unsigned int) def->perms.uid);
    virBufferAsprintf(buf,"      <group>%u</group>\n",
                      (unsigned int) def->perms.gid);
1160 1161 1162


    if (def->perms.label)
1163
        virBufferAsprintf(buf,"      <label>%s</label>\n",
1164 1165 1166 1167
                          def->perms.label);

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

1168 1169 1170 1171 1172 1173
    if (def->encryption) {
        virBufferAdjustIndent(buf, 4);
        if (virStorageEncryptionFormat(buf, def->encryption) < 0)
            return -1;
        virBufferAdjustIndent(buf, -4);
    }
1174

1175
    virBufferAsprintf(buf, "  </%s>\n", type);
1176 1177 1178

    return 0;
}
1179 1180

char *
1181
virStorageVolDefFormat(virStoragePoolDefPtr pool,
1182
                       virStorageVolDefPtr def) {
1183
    virStorageVolOptionsPtr options;
1184
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1185

1186
    options = virStorageVolOptionsForPoolType(pool->type);
1187 1188 1189
    if (options == NULL)
        return NULL;

1190
    virBufferAddLit(&buf, "<volume>\n");
1191 1192
    virBufferAsprintf(&buf,"  <name>%s</name>\n", def->name);
    virBufferAsprintf(&buf,"  <key>%s</key>\n", def->key);
1193
    virBufferAddLit(&buf, "  <source>\n");
1194 1195 1196 1197 1198 1199 1200 1201

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

1204
                virBufferAsprintf(&buf, "    <device path='%s'>\n",
1205
                                  def->source.extents[i].path);
1206 1207
            }

1208
            virBufferAsprintf(&buf,
1209 1210 1211
                              "      <extent start='%llu' end='%llu'/>\n",
                              def->source.extents[i].start,
                              def->source.extents[i].end);
1212 1213 1214
            thispath = def->source.extents[i].path;
        }
        if (thispath != NULL)
1215
            virBufferAddLit(&buf, "    </device>\n");
1216
    }
1217
    virBufferAddLit(&buf, "  </source>\n");
1218

E
Eric Blake 已提交
1219
    virBufferAsprintf(&buf,"  <capacity unit='bytes'>%llu</capacity>\n",
1220
                      def->capacity);
E
Eric Blake 已提交
1221
    virBufferAsprintf(&buf,"  <allocation unit='bytes'>%llu</allocation>\n",
1222
                      def->allocation);
1223

1224
    if (virStorageVolTargetDefFormat(options, &buf,
1225 1226
                                     &def->target, "target") < 0)
        goto cleanup;
1227

1228
    if (def->backingStore.path &&
1229
        virStorageVolTargetDefFormat(options, &buf,
1230 1231
                                     &def->backingStore, "backingStore") < 0)
        goto cleanup;
1232 1233

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

1235
    if (virBufferError(&buf))
1236 1237
        goto no_memory;

1238
    return virBufferContentAndReset(&buf);
1239 1240

 no_memory:
1241
    virReportOOMError();
1242
 cleanup:
1243
    virBufferFreeAndReset(&buf);
1244 1245 1246 1247 1248
    return NULL;
}


virStoragePoolObjPtr
1249
virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
1250
                            const unsigned char *uuid) {
1251
    unsigned int i;
1252

1253 1254
    for (i = 0 ; i < pools->count ; i++) {
        virStoragePoolObjLock(pools->objs[i]);
1255 1256
        if (!memcmp(pools->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
            return pools->objs[i];
1257 1258
        virStoragePoolObjUnlock(pools->objs[i]);
    }
1259 1260 1261 1262 1263

    return NULL;
}

virStoragePoolObjPtr
1264
virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
1265
                            const char *name) {
1266
    unsigned int i;
1267

1268 1269
    for (i = 0 ; i < pools->count ; i++) {
        virStoragePoolObjLock(pools->objs[i]);
1270 1271
        if (STREQ(pools->objs[i]->def->name, name))
            return pools->objs[i];
1272 1273
        virStoragePoolObjUnlock(pools->objs[i]);
    }
1274 1275 1276 1277

    return NULL;
}

1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
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;
}

1293 1294 1295
void
virStoragePoolObjClearVols(virStoragePoolObjPtr pool)
{
1296 1297 1298 1299 1300 1301
    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;
1302 1303 1304 1305 1306
}

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

1309 1310 1311
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->key, key))
            return pool->volumes.objs[i];
1312 1313 1314 1315 1316 1317 1318

    return NULL;
}

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

1321 1322 1323
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->target.path, path))
            return pool->volumes.objs[i];
1324 1325 1326 1327 1328 1329 1330

    return NULL;
}

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

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

    return NULL;
}

virStoragePoolObjPtr
1341
virStoragePoolObjAssignDef(virStoragePoolObjListPtr pools,
1342 1343 1344
                           virStoragePoolDefPtr def) {
    virStoragePoolObjPtr pool;

1345
    if ((pool = virStoragePoolObjFindByName(pools, def->name))) {
1346 1347 1348 1349
        if (!virStoragePoolObjIsActive(pool)) {
            virStoragePoolDefFree(pool->def);
            pool->def = def;
        } else {
1350
            virStoragePoolDefFree(pool->newDef);
1351 1352 1353 1354 1355
            pool->newDef = def;
        }
        return pool;
    }

1356
    if (VIR_ALLOC(pool) < 0) {
1357
        virReportOOMError();
1358 1359 1360
        return NULL;
    }

1361
    if (virMutexInit(&pool->lock) < 0) {
1362
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1363 1364 1365 1366
                              "%s", _("cannot initialize mutex"));
        VIR_FREE(pool);
        return NULL;
    }
1367
    virStoragePoolObjLock(pool);
1368 1369 1370
    pool->active = 0;
    pool->def = def;

1371 1372
    if (VIR_REALLOC_N(pools->objs, pools->count+1) < 0) {
        pool->def = NULL;
1373
        virStoragePoolObjUnlock(pool);
1374
        virStoragePoolObjFree(pool);
1375
        virReportOOMError();
1376 1377 1378
        return NULL;
    }
    pools->objs[pools->count++] = pool;
1379 1380 1381 1382 1383

    return pool;
}

static virStoragePoolObjPtr
1384
virStoragePoolObjLoad(virStoragePoolObjListPtr pools,
1385 1386 1387 1388 1389 1390
                      const char *file,
                      const char *path,
                      const char *autostartLink) {
    virStoragePoolDefPtr def;
    virStoragePoolObjPtr pool;

1391
    if (!(def = virStoragePoolDefParseFile(path))) {
1392 1393 1394 1395
        return NULL;
    }

    if (!virFileMatchesNameSuffix(file, def->name, ".xml")) {
1396
        virStorageReportError(VIR_ERR_XML_ERROR,
1397
                              _("Storage pool config filename '%s' does not match pool name '%s'"),
1398
                              path, def->name);
1399 1400 1401 1402
        virStoragePoolDefFree(def);
        return NULL;
    }

1403
    if (!(pool = virStoragePoolObjAssignDef(pools, def))) {
1404 1405 1406 1407
        virStoragePoolDefFree(def);
        return NULL;
    }

1408
    VIR_FREE(pool->configFile);  /* for driver reload */
1409 1410
    pool->configFile = strdup(path);
    if (pool->configFile == NULL) {
1411
        virReportOOMError();
1412 1413 1414
        virStoragePoolDefFree(def);
        return NULL;
    }
1415
    VIR_FREE(pool->autostartLink); /* for driver reload */
1416 1417
    pool->autostartLink = strdup(autostartLink);
    if (pool->autostartLink == NULL) {
1418
        virReportOOMError();
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
        virStoragePoolDefFree(def);
        return NULL;
    }

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

    return pool;
}


int
1431
virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
1432 1433
                             const char *configDir,
                             const char *autostartDir) {
1434 1435 1436
    DIR *dir;
    struct dirent *entry;

1437
    if (!(dir = opendir(configDir))) {
1438 1439
        if (errno == ENOENT)
            return 0;
1440
        virReportSystemError(errno, _("Failed to open dir '%s'"),
1441
                             configDir);
1442 1443 1444 1445
        return -1;
    }

    while ((entry = readdir(dir))) {
1446 1447
        char *path;
        char *autostartLink;
1448
        virStoragePoolObjPtr pool;
1449 1450 1451 1452 1453 1454 1455

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

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

1456
        if (!(path = virFileBuildPath(configDir, entry->d_name, NULL)))
1457 1458
            continue;

1459 1460 1461
        if (!(autostartLink = virFileBuildPath(autostartDir, entry->d_name,
                                               NULL))) {
            VIR_FREE(path);
1462 1463 1464
            continue;
        }

1465
        pool = virStoragePoolObjLoad(pools, entry->d_name, path,
1466
                                     autostartLink);
1467 1468
        if (pool)
            virStoragePoolObjUnlock(pool);
1469 1470 1471

        VIR_FREE(path);
        VIR_FREE(autostartLink);
1472 1473 1474 1475 1476 1477 1478 1479
    }

    closedir(dir);

    return 0;
}

int
1480
virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
1481
                         virStoragePoolObjPtr pool,
1482 1483
                         virStoragePoolDefPtr def)
{
1484
    char *xml;
1485
    int ret = -1;
1486 1487

    if (!pool->configFile) {
1488 1489
        if (virFileMakePath(driver->configDir) < 0) {
            virReportSystemError(errno,
C
Cole Robinson 已提交
1490 1491
                                 _("cannot create config directory %s"),
                                 driver->configDir);
1492 1493 1494
            return -1;
        }

1495 1496
        if (!(pool->configFile = virFileBuildPath(driver->configDir,
                                                  def->name, ".xml"))) {
1497 1498 1499
            return -1;
        }

1500 1501
        if (!(pool->autostartLink = virFileBuildPath(driver->autostartDir,
                                                     def->name, ".xml"))) {
1502
            VIR_FREE(pool->configFile);
1503 1504 1505 1506
            return -1;
        }
    }

1507 1508
    if (!(xml = virStoragePoolDefFormat(def))) {
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1509
                              "%s", _("failed to generate XML"));
1510 1511 1512
        return -1;
    }

1513
    ret = virXMLSaveFile(pool->configFile, def->name, "pool-edit", xml);
1514
    VIR_FREE(xml);
1515 1516 1517 1518 1519

    return ret;
}

int
1520
virStoragePoolObjDeleteDef(virStoragePoolObjPtr pool) {
1521
    if (!pool->configFile) {
1522
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1523 1524 1525 1526 1527
                              _("no config file for %s"), pool->def->name);
        return -1;
    }

    if (unlink(pool->configFile) < 0) {
1528
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1529 1530 1531 1532 1533 1534 1535
                              _("cannot remove config for %s"),
                              pool->def->name);
        return -1;
    }

    return 0;
}
1536

1537
virStoragePoolSourcePtr
1538
virStoragePoolSourceListNewSource(virStoragePoolSourceListPtr list)
1539 1540 1541 1542
{
    virStoragePoolSourcePtr source;

    if (VIR_REALLOC_N(list->sources, list->nsources+1) < 0) {
1543
        virReportOOMError();
1544 1545 1546 1547 1548 1549 1550 1551 1552
        return NULL;
    }

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

    return source;
}

1553
char *virStoragePoolSourceListFormat(virStoragePoolSourceListPtr def)
1554
{
1555
    virStoragePoolOptionsPtr options;
1556
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1557 1558
    const char *type;
    int i;
1559

1560
    options = virStoragePoolOptionsForPoolType(def->type);
1561 1562 1563
    if (options == NULL)
        return NULL;

1564
    type = virStoragePoolTypeToString(def->type);
1565
    if (!type) {
1566
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1567 1568 1569 1570 1571
                              "%s", _("unexpected pool type"));
        goto cleanup;
    }

    virBufferAddLit(&buf, "<sources>\n");
1572 1573

    for (i = 0; i < def->nsources; i++) {
1574
        virStoragePoolSourceFormat(&buf, options, &def->sources[i]);
1575 1576
    }

1577 1578 1579 1580
    virBufferAddLit(&buf, "</sources>\n");

    if (virBufferError(&buf))
        goto no_memory;
1581 1582

    return virBufferContentAndReset(&buf);
1583 1584

 no_memory:
1585
    virReportOOMError();
1586
 cleanup:
1587
    virBufferFreeAndReset(&buf);
1588
    return NULL;
1589
}
D
Daniel P. Berrange 已提交
1590 1591


1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
/*
 * 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;
}

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
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;

1668 1669 1670 1671
        /* Don't mach against ourself if re-defining existing pool ! */
        if (STREQ(pool->def->name, def->name))
            continue;

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
        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);
1713 1714 1715

        if (matchpool)
            break;
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
    }

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

1727 1728
void virStoragePoolObjLock(virStoragePoolObjPtr obj)
{
1729
    virMutexLock(&obj->lock);
1730 1731 1732 1733
}

void virStoragePoolObjUnlock(virStoragePoolObjPtr obj)
{
1734
    virMutexUnlock(&obj->lock);
1735
}