storage_conf.c 61.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
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * 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 "virstoragefile.h"
40

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

48 49
#define VIR_FROM_THIS VIR_FROM_STORAGE

50 51
#define DEFAULT_POOL_PERM_MODE 0755
#define DEFAULT_VOL_PERM_MODE  0600
52

53 54 55 56
VIR_ENUM_IMPL(virStoragePool,
              VIR_STORAGE_POOL_LAST,
              "dir", "fs", "netfs",
              "logical", "disk", "iscsi",
57
              "scsi", "mpath", "rbd", "sheepdog")
58 59 60 61 62

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

VIR_ENUM_IMPL(virStoragePoolFormatFileSystemNet,
              VIR_STORAGE_POOL_NETFS_LAST,
67
              "auto", "nfs", "glusterfs", "cifs")
68 69 70 71

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

VIR_ENUM_IMPL(virStoragePoolFormatLogical,
              VIR_STORAGE_POOL_LOGICAL_LAST,
76
              "unknown", "lvm2")
77 78 79 80 81 82 83


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

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

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

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



typedef struct _virStoragePoolOptions virStoragePoolOptions;
typedef virStoragePoolOptions *virStoragePoolOptionsPtr;
struct _virStoragePoolOptions {
E
Eric Blake 已提交
123
    unsigned int flags;
124 125 126 127 128 129 130 131 132 133 134 135 136 137
    int defaultFormat;
    virStoragePoolFormatToString formatToString;
    virStoragePoolFormatFromString formatFromString;
};

typedef struct _virStoragePoolTypeInfo virStoragePoolTypeInfo;
typedef virStoragePoolTypeInfo *virStoragePoolTypeInfoPtr;

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

E
Eric Blake 已提交
138 139 140 141 142 143 144 145 146
static int
virStorageVolumeFormatFromString(const char *format)
{
    int ret = virStorageFileFormatTypeFromString(format);
    if (ret == VIR_STORAGE_FILE_NONE)
        return -1;
    return ret;
}

147 148 149 150 151 152 153 154 155 156 157 158
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 = {
159
            .defaultFormat = VIR_STORAGE_FILE_RAW,
E
Eric Blake 已提交
160
            .formatFromString = virStorageVolumeFormatFromString,
161
            .formatToString = virStorageFileFormatTypeToString,
162 163 164 165 166
        },
    },
    { .poolType = VIR_STORAGE_POOL_FS,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_DEVICE),
167
            .defaultFormat = VIR_STORAGE_POOL_FS_AUTO,
168 169 170 171
            .formatFromString = virStoragePoolFormatFileSystemTypeFromString,
            .formatToString = virStoragePoolFormatFileSystemTypeToString,
        },
      .volOptions = {
172
            .defaultFormat = VIR_STORAGE_FILE_RAW,
E
Eric Blake 已提交
173
            .formatFromString = virStorageVolumeFormatFromString,
174
            .formatToString = virStorageFileFormatTypeToString,
175 176 177 178 179 180
        },
    },
    { .poolType = VIR_STORAGE_POOL_NETFS,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
                      VIR_STORAGE_POOL_SOURCE_DIR),
181
            .defaultFormat = VIR_STORAGE_POOL_NETFS_AUTO,
182 183 184 185
            .formatFromString = virStoragePoolFormatFileSystemNetTypeFromString,
            .formatToString = virStoragePoolFormatFileSystemNetTypeToString,
        },
      .volOptions = {
186
            .defaultFormat = VIR_STORAGE_FILE_RAW,
E
Eric Blake 已提交
187
            .formatFromString = virStorageVolumeFormatFromString,
188
            .formatToString = virStorageFileFormatTypeToString,
189 190 191 192 193
        },
    },
    { .poolType = VIR_STORAGE_POOL_ISCSI,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
D
David Allan 已提交
194 195
                      VIR_STORAGE_POOL_SOURCE_DEVICE |
                      VIR_STORAGE_POOL_SOURCE_INITIATOR_IQN),
196 197 198 199 200
        },
      .volOptions = {
            .formatToString = virStoragePoolFormatDiskTypeToString,
        }
    },
201 202 203 204 205 206 207 208
    { .poolType = VIR_STORAGE_POOL_SCSI,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_ADAPTER),
        },
      .volOptions = {
            .formatToString = virStoragePoolFormatDiskTypeToString,
        }
    },
209 210 211 212 213 214 215 216 217 218 219
    { .poolType = VIR_STORAGE_POOL_RBD,
      .poolOptions = {
             .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
                       VIR_STORAGE_POOL_SOURCE_NETWORK |
                       VIR_STORAGE_POOL_SOURCE_NAME),
        },
       .volOptions = {
            .defaultFormat = VIR_STORAGE_FILE_RAW,
            .formatToString = virStoragePoolFormatDiskTypeToString,
        }
    },
220 221 222 223 224 225 226 227 228 229 230
    { .poolType = VIR_STORAGE_POOL_SHEEPDOG,
      .poolOptions = {
             .flags = (VIR_STORAGE_POOL_SOURCE_HOST |
                       VIR_STORAGE_POOL_SOURCE_NETWORK |
                       VIR_STORAGE_POOL_SOURCE_NAME),
        },
       .volOptions = {
            .defaultFormat = VIR_STORAGE_FILE_RAW,
            .formatToString = virStoragePoolFormatDiskTypeToString,
        }
    },
D
Dave Allan 已提交
231 232 233 234 235
    { .poolType = VIR_STORAGE_POOL_MPATH,
      .volOptions = {
            .formatToString = virStoragePoolFormatDiskTypeToString,
        }
    },
236 237 238 239 240 241 242 243
    { .poolType = VIR_STORAGE_POOL_DISK,
      .poolOptions = {
            .flags = (VIR_STORAGE_POOL_SOURCE_DEVICE),
            .defaultFormat = VIR_STORAGE_POOL_DISK_UNKNOWN,
            .formatFromString = virStoragePoolFormatDiskTypeFromString,
            .formatToString = virStoragePoolFormatDiskTypeToString,
        },
      .volOptions = {
244
            .defaultFormat = VIR_STORAGE_VOL_DISK_NONE,
245 246 247 248 249 250 251 252 253 254 255 256 257 258
            .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];

259 260
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("missing backend for pool type %d"), type);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    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;
}


281 282 283
void
virStorageVolDefFree(virStorageVolDefPtr def) {
    int i;
284 285 286 287

    if (!def)
        return;

288 289
    VIR_FREE(def->name);
    VIR_FREE(def->key);
290 291

    for (i = 0 ; i < def->source.nextent ; i++) {
292
        VIR_FREE(def->source.extents[i].path);
293
    }
294
    VIR_FREE(def->source.extents);
295

296 297
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
298
    VIR_FREE(def->target.timestamps);
299
    virStorageEncryptionFree(def->target.encryption);
300 301
    VIR_FREE(def->backingStore.path);
    VIR_FREE(def->backingStore.perms.label);
302
    VIR_FREE(def->backingStore.timestamps);
303
    virStorageEncryptionFree(def->backingStore.encryption);
304
    VIR_FREE(def);
305 306 307
}

void
308 309
virStoragePoolSourceClear(virStoragePoolSourcePtr source)
{
310 311
    int i;

312
    if (!source)
313 314
        return;

315 316 317 318 319
    for (i = 0 ; i < source->nhost ; i++) {
        VIR_FREE(source->hosts[i].name);
    }
    VIR_FREE(source->hosts);

320 321 322
    for (i = 0 ; i < source->ndevice ; i++) {
        VIR_FREE(source->devices[i].freeExtents);
        VIR_FREE(source->devices[i].path);
323
    }
324 325 326
    VIR_FREE(source->devices);
    VIR_FREE(source->dir);
    VIR_FREE(source->name);
327
    VIR_FREE(source->adapter);
D
David Allan 已提交
328
    VIR_FREE(source->initiator.iqn);
329 330
    VIR_FREE(source->vendor);
    VIR_FREE(source->product);
331

332 333 334
    if (source->authType == VIR_STORAGE_POOL_AUTH_CHAP) {
        VIR_FREE(source->auth.chap.login);
        VIR_FREE(source->auth.chap.passwd);
335
    }
336 337 338 339 340

    if (source->authType == VIR_STORAGE_POOL_AUTH_CEPHX) {
        VIR_FREE(source->auth.cephx.username);
        VIR_FREE(source->auth.cephx.secret.usage);
    }
341 342
}

343 344 345 346 347 348 349
void
virStoragePoolSourceFree(virStoragePoolSourcePtr source)
{
    virStoragePoolSourceClear(source);
    VIR_FREE(source);
}

350 351 352 353 354 355 356
void
virStoragePoolDefFree(virStoragePoolDefPtr def) {
    if (!def)
        return;

    VIR_FREE(def->name);

357
    virStoragePoolSourceClear(&def->source);
358

359 360 361
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
    VIR_FREE(def);
362 363 364 365 366
}


void
virStoragePoolObjFree(virStoragePoolObjPtr obj) {
367 368 369
    if (!obj)
        return;

370 371
    virStoragePoolObjClearVols(obj);

372 373
    virStoragePoolDefFree(obj->def);
    virStoragePoolDefFree(obj->newDef);
374

375 376
    VIR_FREE(obj->configFile);
    VIR_FREE(obj->autostartLink);
377 378 379

    virMutexDestroy(&obj->lock);

380
    VIR_FREE(obj);
381 382
}

383 384 385 386 387 388 389 390 391
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;
}

392
void
393
virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
394 395
                        virStoragePoolObjPtr pool)
{
396
    unsigned int i;
397

398 399
    virStoragePoolObjUnlock(pool);

400
    for (i = 0 ; i < pools->count ; i++) {
401
        virStoragePoolObjLock(pools->objs[i]);
402
        if (pools->objs[i] == pool) {
403
            virStoragePoolObjUnlock(pools->objs[i]);
404
            virStoragePoolObjFree(pools->objs[i]);
405

406 407 408
            if (i < (pools->count - 1))
                memmove(pools->objs + i, pools->objs + i + 1,
                        sizeof(*(pools->objs)) * (pools->count - (i + 1)));
409

410 411 412 413
            if (VIR_REALLOC_N(pools->objs, pools->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            pools->count--;
414

415 416
            break;
        }
417
        virStoragePoolObjUnlock(pools->objs[i]);
418
    }
419 420 421 422
}


static int
423
virStoragePoolDefParseAuthChap(xmlXPathContextPtr ctxt,
424
                               virStoragePoolAuthChapPtr auth) {
425
    auth->login = virXPathString("string(./auth/@login)", ctxt);
426
    if (auth->login == NULL) {
427 428
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("missing auth host attribute"));
429 430 431
        return -1;
    }

432
    auth->passwd = virXPathString("string(./auth/@passwd)", ctxt);
433
    if (auth->passwd == NULL) {
434 435
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("missing auth passwd attribute"));
436 437 438 439 440 441
        return -1;
    }

    return 0;
}

442 443
static int
virStoragePoolDefParseAuthCephx(xmlXPathContextPtr ctxt,
P
Peter Krempa 已提交
444
                                virStoragePoolAuthCephxPtr auth) {
445 446 447
    char *uuid = NULL;
    auth->username = virXPathString("string(./auth/@username)", ctxt);
    if (auth->username == NULL) {
448 449
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("missing auth username attribute"));
450 451 452 453 454 455
        return -1;
    }

    uuid = virXPathString("string(./auth/secret/@uuid)", ctxt);
    auth->secret.usage = virXPathString("string(./auth/secret/@usage)", ctxt);
    if (uuid == NULL && auth->secret.usage == NULL) {
456 457
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("missing auth secret uuid or usage attribute"));
458 459 460
        return -1;
    }

461 462 463 464 465 466 467 468 469 470 471 472 473 474
    if (uuid != NULL) {
        if (auth->secret.usage != NULL) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("either auth secret uuid or usage expected"));
            return -1;
        }
        if (virUUIDParse(uuid, auth->secret.uuid) < 0) {
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("invalid auth secret uuid"));
            return -1;
        }
        auth->secret.uuidUsable = true;
    } else {
        auth->secret.uuidUsable = false;
475 476 477 478 479
    }

    return 0;
}

480
static int
481
virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
482 483 484 485 486 487 488 489
                             virStoragePoolSourcePtr source,
                             int pool_type,
                             xmlNodePtr node) {
    int ret = -1;
    xmlNodePtr relnode, *nodeset = NULL;
    char *authType = NULL;
    int nsource, i;
    virStoragePoolOptionsPtr options;
490
    char *name = NULL;
491
    char *port = NULL;
492
    int n;
493 494 495 496 497 498 499 500

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

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

501
    source->name = virXPathString("string(./name)", ctxt);
502
    if (pool_type == VIR_STORAGE_POOL_RBD && source->name == NULL) {
503 504
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("missing mandatory 'name' field for RBD pool name"));
505 506
        goto cleanup;
    }
507 508

    if (options->formatFromString) {
509
        char *format = virXPathString("string(./format/@type)", ctxt);
510 511 512 513 514 515
        if (format == NULL)
            source->format = options->defaultFormat;
        else
            source->format = options->formatFromString(format);

        if (source->format < 0) {
516 517
            virReportError(VIR_ERR_XML_ERROR,
                           _("unknown pool format type %s"), format);
518 519 520 521 522 523
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

524 525 526
    if ((n = virXPathNodeSet("./host", ctxt, &nodeset)) < 0)
        goto cleanup;
    source->nhost = n;
527 528 529 530

    if (source->nhost) {
        if (VIR_ALLOC_N(source->hosts, source->nhost) < 0) {
            virReportOOMError();
531 532 533
            goto cleanup;
        }

534 535 536
        for (i = 0 ; i < source->nhost ; i++) {
            name = virXMLPropString(nodeset[i], "name");
            if (name == NULL) {
537 538
                virReportError(VIR_ERR_XML_ERROR,
                               "%s", _("missing storage pool host name"));
539 540 541 542 543 544 545
                goto cleanup;
            }
            source->hosts[i].name = name;

            port = virXMLPropString(nodeset[i], "port");
            if (port) {
                if (virStrToLong_i(port, NULL, 10, &source->hosts[i].port) < 0) {
546 547 548
                    virReportError(VIR_ERR_XML_ERROR,
                                   _("Invalid port number: %s"),
                                   port);
549 550 551 552 553
                    goto cleanup;
                }
            }
        }
    }
554

555
    VIR_FREE(nodeset);
556
    source->initiator.iqn = virXPathString("string(./initiator/iqn/@name)", ctxt);
557

558
    nsource = virXPathNodeSet("./device", ctxt, &nodeset);
559 560 561
    if (nsource < 0)
        goto cleanup;

562 563 564
    if (nsource > 0) {
        if (VIR_ALLOC_N(source->devices, nsource) < 0) {
            VIR_FREE(nodeset);
565
            virReportOOMError();
566 567 568 569
            goto cleanup;
        }

        for (i = 0 ; i < nsource ; i++) {
570
            char *path = virXMLPropString(nodeset[i], "path");
571 572
            if (path == NULL) {
                VIR_FREE(nodeset);
573 574
                virReportError(VIR_ERR_XML_ERROR,
                               "%s", _("missing storage pool source device path"));
575 576
                goto cleanup;
            }
577
            source->devices[i].path = path;
578 579 580 581
        }
        source->ndevice = nsource;
    }

582 583
    source->dir = virXPathString("string(./dir/@path)", ctxt);
    source->adapter = virXPathString("string(./adapter/@name)", ctxt);
584

585
    authType = virXPathString("string(./auth/@type)", ctxt);
586 587 588 589 590
    if (authType == NULL) {
        source->authType = VIR_STORAGE_POOL_AUTH_NONE;
    } else {
        if (STREQ(authType, "chap")) {
            source->authType = VIR_STORAGE_POOL_AUTH_CHAP;
591 592
        } else if (STREQ(authType, "ceph")) {
            source->authType = VIR_STORAGE_POOL_AUTH_CEPHX;
593
        } else {
594 595 596
            virReportError(VIR_ERR_XML_ERROR,
                           _("unknown auth type '%s'"),
                           (const char *)authType);
597 598 599 600 601
            goto cleanup;
        }
    }

    if (source->authType == VIR_STORAGE_POOL_AUTH_CHAP) {
602
        if (virStoragePoolDefParseAuthChap(ctxt, &source->auth.chap) < 0)
603 604 605
            goto cleanup;
    }

606 607 608 609 610
    if (source->authType == VIR_STORAGE_POOL_AUTH_CEPHX) {
        if (virStoragePoolDefParseAuthCephx(ctxt, &source->auth.cephx) < 0)
            goto cleanup;
    }

611 612 613
    source->vendor = virXPathString("string(./vendor/@name)", ctxt);
    source->product = virXPathString("string(./product/@name)", ctxt);

614 615 616 617
    ret = 0;
cleanup:
    ctxt->node = relnode;

618
    VIR_FREE(port);
619 620 621 622
    VIR_FREE(authType);
    VIR_FREE(nodeset);
    return ret;
}
623

624
virStoragePoolSourcePtr
625
virStoragePoolDefParseSourceString(const char *srcSpec,
626 627 628 629 630 631 632
                                   int pool_type)
{
    xmlDocPtr doc = NULL;
    xmlNodePtr node = NULL;
    xmlXPathContextPtr xpath_ctxt = NULL;
    virStoragePoolSourcePtr def = NULL, ret = NULL;

633 634 635
    if (!(doc = virXMLParseStringCtxt(srcSpec,
                                      _("(storage_source_specification)"),
                                      &xpath_ctxt)))
636 637 638
        goto cleanup;

    if (VIR_ALLOC(def) < 0) {
639
        virReportOOMError();
640 641 642
        goto cleanup;
    }

643
    if (!(node = virXPathNode("/source", xpath_ctxt))) {
644 645
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("root element was not source"));
646 647 648
        goto cleanup;
    }

649
    if (virStoragePoolDefParseSource(xpath_ctxt, def, pool_type,
650 651 652 653 654 655
                                     node) < 0)
        goto cleanup;

    ret = def;
    def = NULL;
cleanup:
656
    virStoragePoolSourceFree(def);
657 658 659 660 661
    xmlFreeDoc(doc);
    xmlXPathFreeContext(xpath_ctxt);

    return ret;
}
662
static int
663
virStorageDefParsePerms(xmlXPathContextPtr ctxt,
664 665 666
                        virStoragePermsPtr perms,
                        const char *permxpath,
                        int defaultmode) {
667 668
    char *mode;
    long v;
669 670 671
    int ret = -1;
    xmlNodePtr relnode;
    xmlNodePtr node;
672

673
    node = virXPathNode(permxpath, ctxt);
674 675 676
    if (node == NULL) {
        /* Set default values if there is not <permissions> element */
        perms->mode = defaultmode;
677 678
        perms->uid = -1;
        perms->gid = -1;
679 680 681 682 683 684 685
        perms->label = NULL;
        return 0;
    }

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

686
    mode = virXPathString("string(./mode)", ctxt);
687
    if (!mode) {
688
        perms->mode = defaultmode;
689
    } else {
690 691 692
        int tmp;

        if (virStrToLong_i(mode, NULL, 8, &tmp) < 0 || (tmp & ~0777)) {
693
            VIR_FREE(mode);
694 695
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("malformed octal mode"));
696
            goto error;
697
        }
698
        perms->mode = tmp;
699
        VIR_FREE(mode);
700 701
    }

702
    if (virXPathNode("./owner", ctxt) == NULL) {
703
        perms->uid = -1;
704
    } else {
705
        if (virXPathLong("number(./owner)", ctxt, &v) < 0) {
706 707
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("malformed owner element"));
708
            goto error;
709 710 711 712
        }
        perms->uid = (int)v;
    }

713
    if (virXPathNode("./group", ctxt) == NULL) {
714
        perms->gid = -1;
715
    } else {
716
        if (virXPathLong("number(./group)", ctxt, &v) < 0) {
717 718
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("malformed group element"));
719
            goto error;
720 721 722 723 724
        }
        perms->gid = (int)v;
    }

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

727 728 729 730
    ret = 0;
error:
    ctxt->node = relnode;
    return ret;
731 732 733
}

static virStoragePoolDefPtr
734
virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) {
735
    virStoragePoolOptionsPtr options;
736
    virStoragePoolDefPtr ret;
737
    xmlNodePtr source_node;
738
    char *type = NULL;
739
    char *uuid = NULL;
740
    char *tmppath;
741

742
    if (VIR_ALLOC(ret) < 0) {
743
        virReportOOMError();
744
        return NULL;
745
    }
746

747
    type = virXPathString("string(./@type)", ctxt);
748
    if ((ret->type = virStoragePoolTypeFromString((const char *)type)) < 0) {
749 750
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), (const char*)type);
751
        goto cleanup;
752 753
    }

754 755 756
    xmlFree(type);
    type = NULL;

757
    if ((options = virStoragePoolOptionsForPoolType(ret->type)) == NULL) {
758 759 760
        goto cleanup;
    }

761
    source_node = virXPathNode("./source", ctxt);
762
    if (source_node) {
763
        if (virStoragePoolDefParseSource(ctxt, &ret->source, ret->type,
764 765 766 767
                                         source_node) < 0)
            goto cleanup;
    }

768
    ret->name = virXPathString("string(./name)", ctxt);
769
    if (ret->name == NULL &&
770
        options->flags & VIR_STORAGE_POOL_SOURCE_NAME)
771
        ret->name = ret->source.name;
772
    if (ret->name == NULL) {
773 774
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("missing pool source name element"));
775 776 777
        goto cleanup;
    }

778
    uuid = virXPathString("string(./uuid)", ctxt);
779 780
    if (uuid == NULL) {
        if (virUUIDGenerate(ret->uuid) < 0) {
781 782
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("unable to generate uuid"));
783 784 785 786
            goto cleanup;
        }
    } else {
        if (virUUIDParse(uuid, ret->uuid) < 0) {
787 788
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("malformed uuid element"));
789 790
            goto cleanup;
        }
791
        VIR_FREE(uuid);
792 793
    }

794
    if (options->flags & VIR_STORAGE_POOL_SOURCE_HOST) {
795
        if (!ret->source.nhost) {
796 797 798
            virReportError(VIR_ERR_XML_ERROR,
                           "%s",
                           _("missing storage pool source host name"));
799 800 801 802
            goto cleanup;
        }
    }

803
    if (options->flags & VIR_STORAGE_POOL_SOURCE_DIR) {
804
        if (!ret->source.dir) {
805 806
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("missing storage pool source path"));
807 808 809
            goto cleanup;
        }
    }
810
    if (options->flags & VIR_STORAGE_POOL_SOURCE_NAME) {
811 812 813
        if (ret->source.name == NULL) {
            /* source name defaults to pool name */
            ret->source.name = strdup(ret->name);
814
            if (ret->source.name == NULL) {
815
                virReportOOMError();
816 817
                goto cleanup;
            }
818 819
        }
    }
820

821
    if (options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) {
822
        if (!ret->source.adapter) {
823 824
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("missing storage pool source adapter name"));
825 826 827
            goto cleanup;
        }
    }
828

829 830 831
    /* If DEVICE is the only source type, then its required */
    if (options->flags == VIR_STORAGE_POOL_SOURCE_DEVICE) {
        if (!ret->source.ndevice) {
832 833
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("missing storage pool source device name"));
834 835 836 837
            goto cleanup;
        }
    }

838 839 840 841
    /* When we are working with a virtual disk we can skip the target
     * path and permissions */
    if (!(options->flags & VIR_STORAGE_POOL_SOURCE_NETWORK)) {
        if ((tmppath = virXPathString("string(./target/path)", ctxt)) == NULL) {
842 843
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("missing storage pool target path"));
844 845 846 847 848 849
            goto cleanup;
        }
        ret->target.path = virFileSanitizePath(tmppath);
        VIR_FREE(tmppath);
        if (!ret->target.path)
            goto cleanup;
850

851
        if (virStorageDefParsePerms(ctxt, &ret->target.perms,
852 853
                                    "./target/permissions",
                                    DEFAULT_POOL_PERM_MODE) < 0)
854 855
            goto cleanup;
    }
856 857 858 859

    return ret;

 cleanup:
860
    VIR_FREE(uuid);
861
    xmlFree(type);
862 863 864 865 866
    virStoragePoolDefFree(ret);
    return NULL;
}

virStoragePoolDefPtr
867
virStoragePoolDefParseNode(xmlDocPtr xml,
868 869 870 871 872
                           xmlNodePtr root) {
    xmlXPathContextPtr ctxt = NULL;
    virStoragePoolDefPtr def = NULL;

    if (STRNEQ((const char *)root->name, "pool")) {
873 874
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("unknown root element for storage pool"));
875 876 877 878 879
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
880
        virReportOOMError();
881 882 883 884
        goto cleanup;
    }

    ctxt->node = root;
885
    def = virStoragePoolDefParseXML(ctxt);
886 887 888 889 890 891
cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

static virStoragePoolDefPtr
892
virStoragePoolDefParse(const char *xmlStr,
893 894
                       const char *filename) {
    virStoragePoolDefPtr ret = NULL;
J
Jiri Denemark 已提交
895
    xmlDocPtr xml;
896

897
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_pool_definition)")))) {
J
Jiri Denemark 已提交
898 899
        ret = virStoragePoolDefParseNode(xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
900 901
    }

902 903 904
    return ret;
}

905
virStoragePoolDefPtr
906
virStoragePoolDefParseString(const char *xmlStr)
907
{
908
    return virStoragePoolDefParse(xmlStr, NULL);
909 910 911
}

virStoragePoolDefPtr
912
virStoragePoolDefParseFile(const char *filename)
913
{
914
    return virStoragePoolDefParse(NULL, filename);
915 916
}

917
static int
918
virStoragePoolSourceFormat(virBufferPtr buf,
919
                           virStoragePoolOptionsPtr options,
920 921 922
                           virStoragePoolSourcePtr src)
{
    int i, j;
923
    char uuid[VIR_UUID_STRING_BUFLEN];
924 925

    virBufferAddLit(buf,"  <source>\n");
926 927
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_HOST) && src->nhost) {
        for (i = 0; i < src->nhost; i++) {
E
Eric Blake 已提交
928
            virBufferAsprintf(buf, "    <host name='%s'", src->hosts[i].name);
929 930 931 932
            if (src->hosts[i].port)
                virBufferAsprintf(buf, " port='%d'", src->hosts[i].port);
            virBufferAddLit(buf, "/>\n");
        }
933
    }
934

935
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_DEVICE) &&
936 937 938
        src->ndevice) {
        for (i = 0 ; i < src->ndevice ; i++) {
            if (src->devices[i].nfreeExtent) {
939
                virBufferAsprintf(buf,"    <device path='%s'>\n",
940 941
                                  src->devices[i].path);
                for (j = 0 ; j < src->devices[i].nfreeExtent ; j++) {
942
                    virBufferAsprintf(buf, "    <freeExtent start='%llu' end='%llu'/>\n",
943 944 945 946 947 948
                                      src->devices[i].freeExtents[j].start,
                                      src->devices[i].freeExtents[j].end);
                }
                virBufferAddLit(buf,"    </device>\n");
            }
            else
949
                virBufferAsprintf(buf, "    <device path='%s'/>\n",
950 951 952
                                  src->devices[i].path);
        }
    }
953
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_DIR) &&
954
        src->dir)
955
        virBufferAsprintf(buf,"    <dir path='%s'/>\n", src->dir);
956
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
957
        src->adapter)
958
        virBufferAsprintf(buf,"    <adapter name='%s'/>\n", src->adapter);
959
    if ((options->flags & VIR_STORAGE_POOL_SOURCE_NAME) &&
960
        src->name)
961
        virBufferAsprintf(buf,"    <name>%s</name>\n", src->name);
962

D
David Allan 已提交
963 964 965 966 967 968 969
    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");
    }

970 971 972
    if (options->formatToString) {
        const char *format = (options->formatToString)(src->format);
        if (!format) {
973 974 975
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown pool format number %d"),
                           src->format);
976 977
            return -1;
        }
978
        virBufferAsprintf(buf,"    <format type='%s'/>\n", format);
979 980 981 982
    }


    if (src->authType == VIR_STORAGE_POOL_AUTH_CHAP)
983
        virBufferAsprintf(buf,"    <auth type='chap' login='%s' passwd='%s'/>\n",
984 985
                          src->auth.chap.login,
                          src->auth.chap.passwd);
986

987 988 989 990 991
    if (src->authType == VIR_STORAGE_POOL_AUTH_CEPHX) {
        virBufferAsprintf(buf,"    <auth username='%s' type='ceph'>\n",
                          src->auth.cephx.username);

        virBufferAsprintf(buf,"      %s", "<secret");
992
        if (src->auth.cephx.secret.uuidUsable) {
993 994 995 996 997 998 999 1000 1001 1002 1003 1004
            virUUIDFormat(src->auth.cephx.secret.uuid, uuid);
            virBufferAsprintf(buf," uuid='%s'", uuid);
        }

        if (src->auth.cephx.secret.usage != NULL) {
            virBufferAsprintf(buf," usage='%s'", src->auth.cephx.secret.usage);
        }
        virBufferAsprintf(buf,"%s", "/>\n");

        virBufferAsprintf(buf,"    %s", "</auth>\n");
    }

1005 1006 1007 1008 1009 1010 1011 1012
    if (src->vendor != NULL) {
        virBufferEscapeString(buf,"    <vendor name='%s'/>\n", src->vendor);
    }

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

1013 1014 1015 1016 1017
    virBufferAddLit(buf,"  </source>\n");

    return 0;
}

1018 1019

char *
1020
virStoragePoolDefFormat(virStoragePoolDefPtr def) {
1021
    virStoragePoolOptionsPtr options;
1022
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1023 1024 1025
    const char *type;
    char uuid[VIR_UUID_STRING_BUFLEN];

1026
    options = virStoragePoolOptionsForPoolType(def->type);
1027 1028 1029
    if (options == NULL)
        return NULL;

1030
    type = virStoragePoolTypeToString(def->type);
1031
    if (!type) {
1032 1033
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unexpected pool type"));
1034 1035
        goto cleanup;
    }
1036 1037
    virBufferAsprintf(&buf, "<pool type='%s'>\n", type);
    virBufferAsprintf(&buf,"  <name>%s</name>\n", def->name);
1038 1039

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

E
Eric Blake 已提交
1042
    virBufferAsprintf(&buf,"  <capacity unit='bytes'>%llu</capacity>\n",
1043
                      def->capacity);
E
Eric Blake 已提交
1044
    virBufferAsprintf(&buf,"  <allocation unit='bytes'>%llu</allocation>\n",
1045
                      def->allocation);
E
Eric Blake 已提交
1046
    virBufferAsprintf(&buf,"  <available unit='bytes'>%llu</available>\n",
1047
                      def->available);
1048

1049
    if (virStoragePoolSourceFormat(&buf, options, &def->source) < 0)
1050
        goto cleanup;
1051

1052
    /* RBD and Sheepdog devices are not local block devs nor files, so it doesn't
1053
     * have a target */
1054
    if (def->type != VIR_STORAGE_POOL_RBD && def->type != VIR_STORAGE_POOL_SHEEPDOG) {
1055
        virBufferAddLit(&buf,"  <target>\n");
1056

1057 1058
        if (def->target.path)
            virBufferAsprintf(&buf,"    <path>%s</path>\n", def->target.path);
1059

1060 1061 1062
        virBufferAddLit(&buf,"    <permissions>\n");
        virBufferAsprintf(&buf,"      <mode>0%o</mode>\n",
                          def->target.perms.mode);
1063 1064 1065 1066
        virBufferAsprintf(&buf,"      <owner>%d</owner>\n",
                          (int) def->target.perms.uid);
        virBufferAsprintf(&buf,"      <group>%d</group>\n",
                          (int) def->target.perms.gid);
1067

1068 1069 1070
        if (def->target.perms.label)
            virBufferAsprintf(&buf,"      <label>%s</label>\n",
                            def->target.perms.label);
1071

1072 1073 1074
        virBufferAddLit(&buf,"    </permissions>\n");
        virBufferAddLit(&buf,"  </target>\n");
    }
1075
    virBufferAddLit(&buf,"</pool>\n");
1076

1077
    if (virBufferError(&buf))
1078 1079
        goto no_memory;

1080
    return virBufferContentAndReset(&buf);
1081 1082

 no_memory:
1083
    virReportOOMError();
1084
 cleanup:
1085
    virBufferFreeAndReset(&buf);
1086 1087 1088 1089 1090
    return NULL;
}


static int
1091
virStorageSize(const char *unit,
1092
               const char *val,
1093 1094 1095
               unsigned long long *ret)
{
    if (virStrToLong_ull(val, NULL, 10, ret) < 0) {
1096 1097
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("malformed capacity element"));
1098 1099
        return -1;
    }
1100 1101 1102 1103
    /* 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;
1104 1105 1106 1107 1108

    return 0;
}

static virStorageVolDefPtr
1109
virStorageVolDefParseXML(virStoragePoolDefPtr pool,
1110
                         xmlXPathContextPtr ctxt) {
1111
    virStorageVolDefPtr ret;
1112
    virStorageVolOptionsPtr options;
1113 1114 1115
    char *allocation = NULL;
    char *capacity = NULL;
    char *unit = NULL;
1116
    xmlNodePtr node;
1117

1118
    options = virStorageVolOptionsForPoolType(pool->type);
1119 1120 1121
    if (options == NULL)
        return NULL;

1122
    if (VIR_ALLOC(ret) < 0) {
1123
        virReportOOMError();
1124
        return NULL;
1125
    }
1126

1127
    ret->name = virXPathString("string(./name)", ctxt);
1128
    if (ret->name == NULL) {
1129 1130
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("missing volume name element"));
1131 1132 1133
        goto cleanup;
    }

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

1137 1138
    capacity = virXPathString("string(./capacity)", ctxt);
    unit = virXPathString("string(./capacity/@unit)", ctxt);
1139
    if (capacity == NULL) {
1140 1141
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("missing capacity element"));
1142 1143
        goto cleanup;
    }
1144
    if (virStorageSize(unit, capacity, &ret->capacity) < 0)
1145
        goto cleanup;
1146 1147
    VIR_FREE(capacity);
    VIR_FREE(unit);
1148

1149
    allocation = virXPathString("string(./allocation)", ctxt);
1150
    if (allocation) {
1151
        unit = virXPathString("string(./allocation/@unit)", ctxt);
1152
        if (virStorageSize(unit, allocation, &ret->allocation) < 0)
1153
            goto cleanup;
1154 1155
        VIR_FREE(allocation);
        VIR_FREE(unit);
1156 1157 1158 1159
    } else {
        ret->allocation = ret->capacity;
    }

1160
    ret->target.path = virXPathString("string(./target/path)", ctxt);
1161
    if (options->formatFromString) {
1162
        char *format = virXPathString("string(./target/format/@type)", ctxt);
1163 1164 1165 1166 1167 1168
        if (format == NULL)
            ret->target.format = options->defaultFormat;
        else
            ret->target.format = (options->formatFromString)(format);

        if (ret->target.format < 0) {
1169 1170
            virReportError(VIR_ERR_XML_ERROR,
                           _("unknown volume format type %s"), format);
1171
            VIR_FREE(format);
1172 1173
            goto cleanup;
        }
1174
        VIR_FREE(format);
1175 1176
    }

1177
    if (virStorageDefParsePerms(ctxt, &ret->target.perms,
1178 1179
                                "./target/permissions",
                                DEFAULT_VOL_PERM_MODE) < 0)
1180 1181
        goto cleanup;

1182
    node = virXPathNode("./target/encryption", ctxt);
1183
    if (node != NULL) {
1184
        ret->target.encryption = virStorageEncryptionParseNode(ctxt->doc,
1185 1186 1187 1188 1189
                                                               node);
        if (ret->target.encryption == NULL)
            goto cleanup;
    }

1190 1191


1192
    ret->backingStore.path = virXPathString("string(./backingStore/path)", ctxt);
1193
    if (options->formatFromString) {
1194
        char *format = virXPathString("string(./backingStore/format/@type)", ctxt);
1195 1196 1197 1198 1199 1200
        if (format == NULL)
            ret->backingStore.format = options->defaultFormat;
        else
            ret->backingStore.format = (options->formatFromString)(format);

        if (ret->backingStore.format < 0) {
1201 1202
            virReportError(VIR_ERR_XML_ERROR,
                           _("unknown volume format type %s"), format);
1203 1204 1205 1206 1207 1208
            VIR_FREE(format);
            goto cleanup;
        }
        VIR_FREE(format);
    }

1209
    if (virStorageDefParsePerms(ctxt, &ret->backingStore.perms,
1210 1211
                                "./backingStore/permissions",
                                DEFAULT_VOL_PERM_MODE) < 0)
1212 1213
        goto cleanup;

1214 1215 1216
    return ret;

 cleanup:
1217 1218 1219
    VIR_FREE(allocation);
    VIR_FREE(capacity);
    VIR_FREE(unit);
1220 1221 1222 1223 1224
    virStorageVolDefFree(ret);
    return NULL;
}

virStorageVolDefPtr
1225
virStorageVolDefParseNode(virStoragePoolDefPtr pool,
1226 1227 1228 1229 1230 1231
                          xmlDocPtr xml,
                          xmlNodePtr root) {
    xmlXPathContextPtr ctxt = NULL;
    virStorageVolDefPtr def = NULL;

    if (STRNEQ((const char *)root->name, "volume")) {
1232 1233
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("unknown root element for storage vol"));
1234 1235 1236 1237 1238
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1239
        virReportOOMError();
1240 1241 1242 1243
        goto cleanup;
    }

    ctxt->node = root;
1244
    def = virStorageVolDefParseXML(pool, ctxt);
1245 1246 1247 1248 1249 1250
cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

static virStorageVolDefPtr
1251
virStorageVolDefParse(virStoragePoolDefPtr pool,
1252 1253 1254
                      const char *xmlStr,
                      const char *filename) {
    virStorageVolDefPtr ret = NULL;
J
Jiri Denemark 已提交
1255
    xmlDocPtr xml;
1256

1257
    if ((xml = virXMLParse(filename, xmlStr, _("(storage_volume_definition)")))) {
J
Jiri Denemark 已提交
1258 1259
        ret = virStorageVolDefParseNode(pool, xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
1260 1261 1262 1263 1264
    }

    return ret;
}

1265
virStorageVolDefPtr
1266
virStorageVolDefParseString(virStoragePoolDefPtr pool,
1267 1268
                            const char *xmlStr)
{
1269
    return virStorageVolDefParse(pool, xmlStr, NULL);
1270 1271 1272
}

virStorageVolDefPtr
1273
virStorageVolDefParseFile(virStoragePoolDefPtr pool,
1274 1275
                          const char *filename)
{
1276
    return virStorageVolDefParse(pool, NULL, filename);
1277
}
1278

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
static void
virStorageVolTimestampFormat(virBufferPtr buf, const char *name,
                             struct timespec *ts)
{
    if (ts->tv_nsec < 0)
        return;
    virBufferAsprintf(buf, "      <%s>%llu", name,
                      (unsigned long long) ts->tv_sec);
    if (ts->tv_nsec)
       virBufferAsprintf(buf, ".%09ld", ts->tv_nsec);
    virBufferAsprintf(buf, "</%s>\n", name);
}

1292
static int
1293
virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
1294 1295 1296
                             virBufferPtr buf,
                             virStorageVolTargetPtr def,
                             const char *type) {
1297
    virBufferAsprintf(buf, "  <%s>\n", type);
1298 1299

    if (def->path)
1300
        virBufferAsprintf(buf,"    <path>%s</path>\n", def->path);
1301 1302 1303 1304

    if (options->formatToString) {
        const char *format = (options->formatToString)(def->format);
        if (!format) {
1305 1306 1307
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown volume format number %d"),
                           def->format);
1308 1309
            return -1;
        }
1310
        virBufferAsprintf(buf,"    <format type='%s'/>\n", format);
1311 1312 1313
    }

    virBufferAddLit(buf,"    <permissions>\n");
1314
    virBufferAsprintf(buf,"      <mode>0%o</mode>\n",
1315
                      def->perms.mode);
E
Eric Blake 已提交
1316 1317 1318 1319
    virBufferAsprintf(buf,"      <owner>%u</owner>\n",
                      (unsigned int) def->perms.uid);
    virBufferAsprintf(buf,"      <group>%u</group>\n",
                      (unsigned int) def->perms.gid);
1320 1321 1322


    if (def->perms.label)
1323
        virBufferAsprintf(buf,"      <label>%s</label>\n",
1324 1325 1326 1327
                          def->perms.label);

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

1328 1329 1330 1331 1332 1333 1334 1335 1336
    if (def->timestamps) {
        virBufferAddLit(buf, "    <timestamps>\n");
        virStorageVolTimestampFormat(buf, "atime", &def->timestamps->atime);
        virStorageVolTimestampFormat(buf, "mtime", &def->timestamps->mtime);
        virStorageVolTimestampFormat(buf, "ctime", &def->timestamps->ctime);
        virStorageVolTimestampFormat(buf, "btime", &def->timestamps->btime);
        virBufferAddLit(buf, "    </timestamps>\n");
    }

1337 1338 1339 1340 1341 1342
    if (def->encryption) {
        virBufferAdjustIndent(buf, 4);
        if (virStorageEncryptionFormat(buf, def->encryption) < 0)
            return -1;
        virBufferAdjustIndent(buf, -4);
    }
1343

1344
    virBufferAsprintf(buf, "  </%s>\n", type);
1345 1346 1347

    return 0;
}
1348 1349

char *
1350
virStorageVolDefFormat(virStoragePoolDefPtr pool,
1351
                       virStorageVolDefPtr def) {
1352
    virStorageVolOptionsPtr options;
1353
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1354

1355
    options = virStorageVolOptionsForPoolType(pool->type);
1356 1357 1358
    if (options == NULL)
        return NULL;

1359
    virBufferAddLit(&buf, "<volume>\n");
1360
    virBufferAsprintf(&buf,"  <name>%s</name>\n", def->name);
1361
    virBufferAsprintf(&buf,"  <key>%s</key>\n", def->key ? def->key : "(null)");
1362
    virBufferAddLit(&buf, "  <source>\n");
1363 1364 1365 1366 1367 1368 1369 1370

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

1373
                virBufferAsprintf(&buf, "    <device path='%s'>\n",
1374
                                  def->source.extents[i].path);
1375 1376
            }

1377
            virBufferAsprintf(&buf,
1378 1379 1380
                              "      <extent start='%llu' end='%llu'/>\n",
                              def->source.extents[i].start,
                              def->source.extents[i].end);
1381 1382 1383
            thispath = def->source.extents[i].path;
        }
        if (thispath != NULL)
1384
            virBufferAddLit(&buf, "    </device>\n");
1385
    }
1386
    virBufferAddLit(&buf, "  </source>\n");
1387

E
Eric Blake 已提交
1388
    virBufferAsprintf(&buf,"  <capacity unit='bytes'>%llu</capacity>\n",
1389
                      def->capacity);
E
Eric Blake 已提交
1390
    virBufferAsprintf(&buf,"  <allocation unit='bytes'>%llu</allocation>\n",
1391
                      def->allocation);
1392

1393
    if (virStorageVolTargetDefFormat(options, &buf,
1394 1395
                                     &def->target, "target") < 0)
        goto cleanup;
1396

1397
    if (def->backingStore.path &&
1398
        virStorageVolTargetDefFormat(options, &buf,
1399 1400
                                     &def->backingStore, "backingStore") < 0)
        goto cleanup;
1401 1402

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

1404
    if (virBufferError(&buf))
1405 1406
        goto no_memory;

1407
    return virBufferContentAndReset(&buf);
1408 1409

 no_memory:
1410
    virReportOOMError();
1411
 cleanup:
1412
    virBufferFreeAndReset(&buf);
1413 1414 1415 1416 1417
    return NULL;
}


virStoragePoolObjPtr
1418
virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
1419
                            const unsigned char *uuid) {
1420
    unsigned int i;
1421

1422 1423
    for (i = 0 ; i < pools->count ; i++) {
        virStoragePoolObjLock(pools->objs[i]);
1424 1425
        if (!memcmp(pools->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
            return pools->objs[i];
1426 1427
        virStoragePoolObjUnlock(pools->objs[i]);
    }
1428 1429 1430 1431 1432

    return NULL;
}

virStoragePoolObjPtr
1433
virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
1434
                            const char *name) {
1435
    unsigned int i;
1436

1437 1438
    for (i = 0 ; i < pools->count ; i++) {
        virStoragePoolObjLock(pools->objs[i]);
1439 1440
        if (STREQ(pools->objs[i]->def->name, name))
            return pools->objs[i];
1441 1442
        virStoragePoolObjUnlock(pools->objs[i]);
    }
1443 1444 1445 1446

    return NULL;
}

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
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;
}

1462 1463 1464
void
virStoragePoolObjClearVols(virStoragePoolObjPtr pool)
{
1465 1466 1467 1468 1469 1470
    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;
1471 1472 1473 1474 1475
}

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

1478 1479 1480
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->key, key))
            return pool->volumes.objs[i];
1481 1482 1483 1484 1485 1486 1487

    return NULL;
}

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

1490 1491 1492
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->target.path, path))
            return pool->volumes.objs[i];
1493 1494 1495 1496 1497 1498 1499

    return NULL;
}

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

1502 1503 1504
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->name, name))
            return pool->volumes.objs[i];
1505 1506 1507 1508 1509

    return NULL;
}

virStoragePoolObjPtr
1510
virStoragePoolObjAssignDef(virStoragePoolObjListPtr pools,
1511 1512 1513
                           virStoragePoolDefPtr def) {
    virStoragePoolObjPtr pool;

1514
    if ((pool = virStoragePoolObjFindByName(pools, def->name))) {
1515 1516 1517 1518
        if (!virStoragePoolObjIsActive(pool)) {
            virStoragePoolDefFree(pool->def);
            pool->def = def;
        } else {
1519
            virStoragePoolDefFree(pool->newDef);
1520 1521 1522 1523 1524
            pool->newDef = def;
        }
        return pool;
    }

1525
    if (VIR_ALLOC(pool) < 0) {
1526
        virReportOOMError();
1527 1528 1529
        return NULL;
    }

1530
    if (virMutexInit(&pool->lock) < 0) {
1531 1532
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
1533 1534 1535
        VIR_FREE(pool);
        return NULL;
    }
1536
    virStoragePoolObjLock(pool);
1537 1538 1539
    pool->active = 0;
    pool->def = def;

1540 1541
    if (VIR_REALLOC_N(pools->objs, pools->count+1) < 0) {
        pool->def = NULL;
1542
        virStoragePoolObjUnlock(pool);
1543
        virStoragePoolObjFree(pool);
1544
        virReportOOMError();
1545 1546 1547
        return NULL;
    }
    pools->objs[pools->count++] = pool;
1548 1549 1550 1551 1552

    return pool;
}

static virStoragePoolObjPtr
1553
virStoragePoolObjLoad(virStoragePoolObjListPtr pools,
1554 1555 1556 1557 1558 1559
                      const char *file,
                      const char *path,
                      const char *autostartLink) {
    virStoragePoolDefPtr def;
    virStoragePoolObjPtr pool;

1560
    if (!(def = virStoragePoolDefParseFile(path))) {
1561 1562 1563 1564
        return NULL;
    }

    if (!virFileMatchesNameSuffix(file, def->name, ".xml")) {
1565 1566 1567
        virReportError(VIR_ERR_XML_ERROR,
                       _("Storage pool config filename '%s' does not match pool name '%s'"),
                       path, def->name);
1568 1569 1570 1571
        virStoragePoolDefFree(def);
        return NULL;
    }

1572
    if (!(pool = virStoragePoolObjAssignDef(pools, def))) {
1573 1574 1575 1576
        virStoragePoolDefFree(def);
        return NULL;
    }

1577
    VIR_FREE(pool->configFile);  /* for driver reload */
1578 1579
    pool->configFile = strdup(path);
    if (pool->configFile == NULL) {
1580
        virReportOOMError();
1581 1582 1583
        virStoragePoolDefFree(def);
        return NULL;
    }
1584
    VIR_FREE(pool->autostartLink); /* for driver reload */
1585 1586
    pool->autostartLink = strdup(autostartLink);
    if (pool->autostartLink == NULL) {
1587
        virReportOOMError();
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
        virStoragePoolDefFree(def);
        return NULL;
    }

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

    return pool;
}


int
1600
virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
1601 1602
                             const char *configDir,
                             const char *autostartDir) {
1603 1604 1605
    DIR *dir;
    struct dirent *entry;

1606
    if (!(dir = opendir(configDir))) {
1607 1608
        if (errno == ENOENT)
            return 0;
1609
        virReportSystemError(errno, _("Failed to open dir '%s'"),
1610
                             configDir);
1611 1612 1613 1614
        return -1;
    }

    while ((entry = readdir(dir))) {
1615 1616
        char *path;
        char *autostartLink;
1617
        virStoragePoolObjPtr pool;
1618 1619 1620 1621 1622 1623 1624

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

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

1625
        if (!(path = virFileBuildPath(configDir, entry->d_name, NULL)))
1626 1627
            continue;

1628 1629 1630
        if (!(autostartLink = virFileBuildPath(autostartDir, entry->d_name,
                                               NULL))) {
            VIR_FREE(path);
1631 1632 1633
            continue;
        }

1634
        pool = virStoragePoolObjLoad(pools, entry->d_name, path,
1635
                                     autostartLink);
1636 1637
        if (pool)
            virStoragePoolObjUnlock(pool);
1638 1639 1640

        VIR_FREE(path);
        VIR_FREE(autostartLink);
1641 1642 1643 1644 1645 1646 1647 1648
    }

    closedir(dir);

    return 0;
}

int
1649
virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
1650
                         virStoragePoolObjPtr pool,
1651 1652
                         virStoragePoolDefPtr def)
{
J
Ján Tomko 已提交
1653
    char uuidstr[VIR_UUID_STRING_BUFLEN];
1654
    char *xml;
1655
    int ret = -1;
1656 1657

    if (!pool->configFile) {
1658 1659
        if (virFileMakePath(driver->configDir) < 0) {
            virReportSystemError(errno,
C
Cole Robinson 已提交
1660 1661
                                 _("cannot create config directory %s"),
                                 driver->configDir);
1662 1663 1664
            return -1;
        }

1665 1666
        if (!(pool->configFile = virFileBuildPath(driver->configDir,
                                                  def->name, ".xml"))) {
1667 1668 1669
            return -1;
        }

1670 1671
        if (!(pool->autostartLink = virFileBuildPath(driver->autostartDir,
                                                     def->name, ".xml"))) {
1672
            VIR_FREE(pool->configFile);
1673 1674 1675 1676
            return -1;
        }
    }

1677
    if (!(xml = virStoragePoolDefFormat(def))) {
1678 1679
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("failed to generate XML"));
1680 1681 1682
        return -1;
    }

J
Ján Tomko 已提交
1683 1684 1685 1686
    virUUIDFormat(def->uuid, uuidstr);
    ret = virXMLSaveFile(pool->configFile,
                         virXMLPickShellSafeComment(def->name, uuidstr),
                         "pool-edit", xml);
1687
    VIR_FREE(xml);
1688 1689 1690 1691 1692

    return ret;
}

int
1693
virStoragePoolObjDeleteDef(virStoragePoolObjPtr pool) {
1694
    if (!pool->configFile) {
1695 1696
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no config file for %s"), pool->def->name);
1697 1698 1699 1700
        return -1;
    }

    if (unlink(pool->configFile) < 0) {
1701 1702 1703
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot remove config for %s"),
                       pool->def->name);
1704 1705 1706 1707 1708
        return -1;
    }

    return 0;
}
1709

1710
virStoragePoolSourcePtr
1711
virStoragePoolSourceListNewSource(virStoragePoolSourceListPtr list)
1712 1713 1714 1715
{
    virStoragePoolSourcePtr source;

    if (VIR_REALLOC_N(list->sources, list->nsources+1) < 0) {
1716
        virReportOOMError();
1717 1718 1719 1720 1721 1722 1723 1724 1725
        return NULL;
    }

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

    return source;
}

1726
char *virStoragePoolSourceListFormat(virStoragePoolSourceListPtr def)
1727
{
1728
    virStoragePoolOptionsPtr options;
1729
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1730 1731
    const char *type;
    int i;
1732

1733
    options = virStoragePoolOptionsForPoolType(def->type);
1734 1735 1736
    if (options == NULL)
        return NULL;

1737
    type = virStoragePoolTypeToString(def->type);
1738
    if (!type) {
1739 1740
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unexpected pool type"));
1741 1742 1743 1744
        goto cleanup;
    }

    virBufferAddLit(&buf, "<sources>\n");
1745 1746

    for (i = 0; i < def->nsources; i++) {
1747
        virStoragePoolSourceFormat(&buf, options, &def->sources[i]);
1748 1749
    }

1750 1751 1752 1753
    virBufferAddLit(&buf, "</sources>\n");

    if (virBufferError(&buf))
        goto no_memory;
1754 1755

    return virBufferContentAndReset(&buf);
1756 1757

 no_memory:
1758
    virReportOOMError();
1759
 cleanup:
1760
    virBufferFreeAndReset(&buf);
1761
    return NULL;
1762
}
D
Daniel P. Berrange 已提交
1763 1764


1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
/*
 * 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);
1790 1791 1792
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("pool '%s' is already defined with uuid %s"),
                           pool->def->name, uuidstr);
1793 1794 1795 1796 1797 1798
            goto cleanup;
        }

        if (check_active) {
            /* UUID & name match, but if Pool is already active, refuse it */
            if (virStoragePoolObjIsActive(pool)) {
1799 1800 1801
                virReportError(VIR_ERR_OPERATION_INVALID,
                               _("pool is already active as '%s'"),
                               pool->def->name);
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
                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);
1813 1814 1815
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("pool '%s' already exists with uuid %s"),
                           def->name, uuidstr);
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
            goto cleanup;
        }
    }

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

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
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;

1841 1842 1843 1844
        /* Don't mach against ourself if re-defining existing pool ! */
        if (STREQ(pool->def->name, def->name))
            continue;

1845 1846 1847 1848 1849 1850 1851 1852 1853
        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)) \
1854 1855
                && (pool->def->source.nhost == 1 && def->source.nhost == 1) \
                && (STREQ(pool->def->source.hosts[0].name, def->source.hosts[0].name)))
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
                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) {
1866 1867 1868 1869 1870 1871 1872 1873
                if (matchpool->def->source.nhost == 1 && def->source.nhost == 1) {
                    if (STREQ(matchpool->def->source.hosts[0].name, def->source.hosts[0].name)) {
                        if ((matchpool->def->source.initiator.iqn) && (def->source.initiator.iqn)) {
                            if (STREQ(matchpool->def->source.initiator.iqn, def->source.initiator.iqn))
                                break;
                            matchpool = NULL;
                        }
                        break;
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
                    }
                }
                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);
1889 1890 1891

        if (matchpool)
            break;
1892 1893 1894
    }

    if (matchpool) {
1895 1896 1897
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Storage source conflict with pool: '%s'"),
                       matchpool->def->name);
1898 1899 1900 1901
        ret = -1;
    }
    return ret;
}
1902

1903 1904
void virStoragePoolObjLock(virStoragePoolObjPtr obj)
{
1905
    virMutexLock(&obj->lock);
1906 1907 1908 1909
}

void virStoragePoolObjUnlock(virStoragePoolObjPtr obj)
{
1910
    virMutexUnlock(&obj->lock);
1911
}
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996

#define MATCH(FLAG) (flags & (FLAG))
static bool
virStoragePoolMatch(virStoragePoolObjPtr poolobj,
                    unsigned int flags)
{
    /* filter by active state */
    if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE) &&
        !((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE) &&
           virStoragePoolObjIsActive(poolobj)) ||
          (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE) &&
           !virStoragePoolObjIsActive(poolobj))))
        return false;

    /* filter by persistence */
    if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_PERSISTENT) &&
        !((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT) &&
           poolobj->configFile) ||
          (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_TRANSIENT) &&
           !poolobj->configFile)))
        return false;

    /* filter by autostart option */
    if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART) &&
        !((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_AUTOSTART) &&
           poolobj->autostart) ||
          (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_NO_AUTOSTART) &&
           !poolobj->autostart)))
        return false;

    /* filter by pool type */
    if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE)) {
        if (!((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_DIR) &&
               (poolobj->def->type == VIR_STORAGE_POOL_DIR))     ||
              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FS) &&
               (poolobj->def->type == VIR_STORAGE_POOL_FS))      ||
              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_NETFS) &&
               (poolobj->def->type == VIR_STORAGE_POOL_NETFS))   ||
              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_LOGICAL) &&
               (poolobj->def->type == VIR_STORAGE_POOL_LOGICAL)) ||
              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_DISK) &&
               (poolobj->def->type == VIR_STORAGE_POOL_DISK))    ||
              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI) &&
               (poolobj->def->type == VIR_STORAGE_POOL_ISCSI))   ||
              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_SCSI) &&
               (poolobj->def->type == VIR_STORAGE_POOL_SCSI))    ||
              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_MPATH) &&
               (poolobj->def->type == VIR_STORAGE_POOL_MPATH))   ||
              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_RBD) &&
               (poolobj->def->type == VIR_STORAGE_POOL_RBD))     ||
              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_SHEEPDOG) &&
               (poolobj->def->type == VIR_STORAGE_POOL_SHEEPDOG))))
            return false;
    }

    return true;
}
#undef MATCH

int
virStoragePoolList(virConnectPtr conn,
                   virStoragePoolObjList poolobjs,
                   virStoragePoolPtr **pools,
                   unsigned int flags)
{
    virStoragePoolPtr *tmp_pools = NULL;
    virStoragePoolPtr pool = NULL;
    int npools = 0;
    int ret = -1;
    int i;

    if (pools) {
        if (VIR_ALLOC_N(tmp_pools, poolobjs.count + 1) < 0) {
            virReportOOMError();
            goto cleanup;
        }
    }

    for (i = 0; i < poolobjs.count; i++) {
        virStoragePoolObjPtr poolobj = poolobjs.objs[i];
        virStoragePoolObjLock(poolobj);
        if (virStoragePoolMatch(poolobj, flags)) {
            if (pools) {
                if (!(pool = virGetStoragePool(conn,
                                               poolobj->def->name,
1997 1998
                                               poolobj->def->uuid,
                                               NULL, NULL))) {
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
                    virStoragePoolObjUnlock(poolobj);
                    goto cleanup;
                }
                tmp_pools[npools] = pool;
            }
            npools++;
        }
        virStoragePoolObjUnlock(poolobj);
    }

    if (tmp_pools) {
        /* trim the array to the final size */
        ignore_value(VIR_REALLOC_N(tmp_pools, npools + 1));
        *pools = tmp_pools;
        tmp_pools = NULL;
    }

    ret = npools;

cleanup:
    if (tmp_pools) {
        for (i = 0; i < npools; i++) {
            if (tmp_pools[i])
                virStoragePoolFree(tmp_pools[i]);
        }
    }

    VIR_FREE(tmp_pools);
    return ret;
}