storage_conf.c 51.0 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
        char *end = NULL;
574
        perms->mode = strtol(mode, &end, 8);
575
        if (*end || (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

E
Eric Blake 已提交
900
    virBufferAsprintf(&buf,"  <capacity unit='bytes'>%llu</capacity>\n",
901
                      def->capacity);
E
Eric Blake 已提交
902
    virBufferAsprintf(&buf,"  <allocation unit='bytes'>%llu</allocation>\n",
903
                      def->allocation);
E
Eric Blake 已提交
904
    virBufferAsprintf(&buf,"  <available unit='bytes'>%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
               const char *val,
947 948 949
               unsigned long long *ret)
{
    if (virStrToLong_ull(val, NULL, 10, ret) < 0) {
950
        virStorageReportError(VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
951
                              "%s", _("malformed capacity element"));
952 953
        return -1;
    }
954 955 956 957
    /* 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;
958 959 960 961 962

    return 0;
}

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

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

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

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

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

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

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

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

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

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

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

1043 1044


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

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

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

1066 1067 1068
    return ret;

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

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

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

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

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

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

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

    return ret;
}

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

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

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

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

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

    virBufferAddLit(buf,"    <permissions>\n");
1153
    virBufferAsprintf(buf,"      <mode>0%o</mode>\n",
1154
                      def->perms.mode);
1155
    virBufferAsprintf(buf,"      <owner>%d</owner>\n",
1156
                      def->perms.uid);
1157
    virBufferAsprintf(buf,"      <group>%d</group>\n",
1158 1159 1160 1161
                      def->perms.gid);


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

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

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

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

    return 0;
}
1178 1179

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

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

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

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

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

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

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

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

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

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

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

1237
    return virBufferContentAndReset(&buf);
1238 1239

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


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

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

    return NULL;
}

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

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

    return NULL;
}

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

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

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

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

    return NULL;
}

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

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

    return NULL;
}

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

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

    return NULL;
}

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

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

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

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

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

    return pool;
}

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

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

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

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

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

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

    return pool;
}


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

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

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

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

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

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

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

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

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

    closedir(dir);

    return 0;
}

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

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

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

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

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

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

    return ret;
}

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

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

    return 0;
}
1535

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

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

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

    return source;
}

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

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

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

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

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

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

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

    return virBufferContentAndReset(&buf);
1582 1583

 no_memory:
1584
    virReportOOMError();
1585
 cleanup:
1586
    virBufferFreeAndReset(&buf);
1587
    return NULL;
1588
}
D
Daniel P. Berrange 已提交
1589 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
/*
 * 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;
}

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

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

1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
        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;
}
1722

1723 1724
void virStoragePoolObjLock(virStoragePoolObjPtr obj)
{
1725
    virMutexLock(&obj->lock);
1726 1727 1728 1729
}

void virStoragePoolObjUnlock(virStoragePoolObjPtr obj)
{
1730
    virMutexUnlock(&obj->lock);
1731
}