storage_driver.c 50.8 KB
Newer Older
1 2 3
/*
 * storage_driver.c: core driver for storage APIs
 *
4
 * Copyright (C) 2006-2009 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
 * 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 <stdio.h>
#include <unistd.h>
#include <sys/types.h>
R
Richard W.M. Jones 已提交
29
#if HAVE_PWD_H
30
# include <pwd.h>
R
Richard W.M. Jones 已提交
31
#endif
32 33 34
#include <errno.h>
#include <string.h>

35
#include "virterror_internal.h"
36
#include "datatypes.h"
37 38 39 40
#include "driver.h"
#include "util.h"
#include "storage_driver.h"
#include "storage_conf.h"
41
#include "memory.h"
42
#include "storage_backend.h"
43
#include "logging.h"
44

45 46
#define VIR_FROM_THIS VIR_FROM_STORAGE

47 48 49 50
static virStorageDriverStatePtr driverState;

static int storageDriverShutdown(void);

51 52
static void storageDriverLock(virStorageDriverStatePtr driver)
{
53
    virMutexLock(&driver->lock);
54 55 56
}
static void storageDriverUnlock(virStorageDriverStatePtr driver)
{
57
    virMutexUnlock(&driver->lock);
58
}
59 60 61

static void
storageDriverAutostart(virStorageDriverStatePtr driver) {
62
    unsigned int i;
63

64 65
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjPtr pool = driver->pools.objs[i];
66

67
        virStoragePoolObjLock(pool);
68 69 70 71
        if (pool->autostart &&
            !virStoragePoolObjIsActive(pool)) {
            virStorageBackendPtr backend;
            if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
72
                VIR_ERROR("Missing backend %d", pool->def->type);
73
                virStoragePoolObjUnlock(pool);
74 75 76 77 78 79
                continue;
            }

            if (backend->startPool &&
                backend->startPool(NULL, pool) < 0) {
                virErrorPtr err = virGetLastError();
80 81 82
                VIR_ERROR("Failed to autostart storage pool '%s': %s",
                          pool->def->name, err ? err->message :
                          "no error message found");
83
                virStoragePoolObjUnlock(pool);
84 85 86 87 88 89 90
                continue;
            }

            if (backend->refreshPool(NULL, pool) < 0) {
                virErrorPtr err = virGetLastError();
                if (backend->stopPool)
                    backend->stopPool(NULL, pool);
91 92 93
                VIR_ERROR("Failed to autostart storage pool '%s': %s",
                          pool->def->name, err ? err->message :
                          "no error message found");
94
                virStoragePoolObjUnlock(pool);
95 96 97 98
                continue;
            }
            pool->active = 1;
        }
99
        virStoragePoolObjUnlock(pool);
100 101 102 103 104 105 106 107 108
    }
}

/**
 * virStorageStartup:
 *
 * Initialization function for the QEmu daemon
 */
static int
109
storageDriverStartup(int privileged) {
110 111 112
    char *base = NULL;
    char driverConf[PATH_MAX];

113
    if (VIR_ALLOC(driverState) < 0)
114 115
        return -1;

116 117 118 119
    if (virMutexInit(&driverState->lock) < 0) {
        VIR_FREE(driverState);
        return -1;
    }
120 121
    storageDriverLock(driverState);

122
    if (privileged) {
123 124 125
        if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
            goto out_of_memory;
    } else {
126
        uid_t uid = geteuid();
127
        char *userdir = virGetUserDirectory(uid);
128 129 130

        if (!userdir)
            goto error;
131

132 133
        if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) {
            VIR_FREE(userdir);
134 135
            goto out_of_memory;
        }
136
        VIR_FREE(userdir);
137 138 139 140 141 142 143 144 145 146
    }

    /* Configuration paths are either ~/.libvirt/storage/... (session) or
     * /etc/libvirt/storage/... (system).
     */
    if (snprintf (driverConf, sizeof(driverConf),
                  "%s/storage.conf", base) == -1)
        goto out_of_memory;
    driverConf[sizeof(driverConf)-1] = '\0';

147 148
    if (virAsprintf(&driverState->configDir,
                    "%s/storage", base) == -1)
149 150
        goto out_of_memory;

151 152
    if (virAsprintf(&driverState->autostartDir,
                    "%s/storage/autostart", base) == -1)
153 154
        goto out_of_memory;

155
    VIR_FREE(base);
156 157 158 159 160 161 162 163

    /*
    if (virStorageLoadDriverConfig(driver, driverConf) < 0) {
        virStorageDriverShutdown();
        return -1;
    }
    */

164
    if (virStoragePoolLoadAllConfigs(&driverState->pools,
165
                                     driverState->configDir,
166 167
                                     driverState->autostartDir) < 0)
        goto error;
168 169
    storageDriverAutostart(driverState);

170
    storageDriverUnlock(driverState);
171 172
    return 0;

173
out_of_memory:
174
    virReportOOMError();
175 176 177 178
error:
    VIR_FREE(base);
    storageDriverUnlock(driverState);
    storageDriverShutdown();
179 180 181 182 183 184 185 186 187 188 189
    return -1;
}

/**
 * virStorageReload:
 *
 * Function to restart the storage driver, it will recheck the configuration
 * files and update its state
 */
static int
storageDriverReload(void) {
190 191 192
    if (!driverState)
        return -1;

193
    storageDriverLock(driverState);
194
    virStoragePoolLoadAllConfigs(&driverState->pools,
195 196
                                 driverState->configDir,
                                 driverState->autostartDir);
197
    storageDriverAutostart(driverState);
198
    storageDriverUnlock(driverState);
199 200 201 202 203 204 205 206 207 208 209 210 211

    return 0;
}

/**
 * virStorageActive:
 *
 * Checks if the storage driver is active, i.e. has an active pool
 *
 * Returns 1 if active, 0 otherwise
 */
static int
storageDriverActive(void) {
212
    unsigned int i;
213
    int active = 0;
214 215 216 217

    if (!driverState)
        return 0;

218 219 220 221
    storageDriverLock(driverState);

    for (i = 0 ; i < driverState->pools.count ; i++) {
        virStoragePoolObjLock(driverState->pools.objs[i]);
222
        if (virStoragePoolObjIsActive(driverState->pools.objs[i]))
223 224 225
            active = 1;
        virStoragePoolObjUnlock(driverState->pools.objs[i]);
    }
226

227 228
    storageDriverUnlock(driverState);
    return active;
229 230 231 232 233 234 235 236 237 238 239 240
}

/**
 * virStorageShutdown:
 *
 * Shutdown the storage driver, it will stop all active storage pools
 */
static int
storageDriverShutdown(void) {
    if (!driverState)
        return -1;

241
    storageDriverLock(driverState);
242 243

    /* free inactive pools */
244 245 246 247
    virStoragePoolObjListFree(&driverState->pools);

    VIR_FREE(driverState->configDir);
    VIR_FREE(driverState->autostartDir);
248
    storageDriverUnlock(driverState);
249
    virMutexDestroy(&driverState->lock);
250
    VIR_FREE(driverState);
251 252 253 254 255 256 257 258 259

    return 0;
}



static virStoragePoolPtr
storagePoolLookupByUUID(virConnectPtr conn,
                        const unsigned char *uuid) {
260 261 262
    virStorageDriverStatePtr driver = conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
263

264
    storageDriverLock(driver);
265
    pool = virStoragePoolObjFindByUUID(&driver->pools, uuid);
266 267
    storageDriverUnlock(driver);

268
    if (!pool) {
269
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
270
                              "%s", _("no pool with matching uuid"));
271
        goto cleanup;
272 273 274
    }

    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
275 276

cleanup:
277 278
    if (pool)
        virStoragePoolObjUnlock(pool);
279 280 281 282 283 284
    return ret;
}

static virStoragePoolPtr
storagePoolLookupByName(virConnectPtr conn,
                        const char *name) {
285 286 287
    virStorageDriverStatePtr driver = conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
288

289
    storageDriverLock(driver);
290
    pool = virStoragePoolObjFindByName(&driver->pools, name);
291 292
    storageDriverUnlock(driver);

293
    if (!pool) {
294
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
295
                              _("no pool with matching name '%s'"), name);
296
        goto cleanup;
297 298 299
    }

    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
300 301

cleanup:
302 303
    if (pool)
        virStoragePoolObjUnlock(pool);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    return ret;
}

static virStoragePoolPtr
storagePoolLookupByVolume(virStorageVolPtr vol) {
    return storagePoolLookupByName(vol->conn, vol->pool);
}

static virDrvOpenStatus
storageOpen(virConnectPtr conn,
            virConnectAuthPtr auth ATTRIBUTE_UNUSED,
            int flags ATTRIBUTE_UNUSED) {
    if (!driverState)
        return VIR_DRV_OPEN_DECLINED;

    conn->storagePrivateData = driverState;
    return VIR_DRV_OPEN_SUCCESS;
}

static int
storageClose(virConnectPtr conn) {
    conn->storagePrivateData = NULL;
    return 0;
}

static int
storageNumPools(virConnectPtr conn) {
331
    virStorageDriverStatePtr driver = conn->storagePrivateData;
332 333
    unsigned int i, nactive = 0;

334 335 336
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
337 338
        if (virStoragePoolObjIsActive(driver->pools.objs[i]))
            nactive++;
339 340 341
        virStoragePoolObjUnlock(driver->pools.objs[i]);
    }
    storageDriverUnlock(driver);
342 343

    return nactive;
344 345 346 347 348 349
}

static int
storageListPools(virConnectPtr conn,
                 char **const names,
                 int nnames) {
350
    virStorageDriverStatePtr driver = conn->storagePrivateData;
351
    int got = 0, i;
352

353
    storageDriverLock(driver);
354
    for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
355
        virStoragePoolObjLock(driver->pools.objs[i]);
356 357
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
358
                virStoragePoolObjUnlock(driver->pools.objs[i]);
359
                virReportOOMError();
360 361 362 363
                goto cleanup;
            }
            got++;
        }
364
        virStoragePoolObjUnlock(driver->pools.objs[i]);
365
    }
366
    storageDriverUnlock(driver);
367 368 369
    return got;

 cleanup:
370 371 372
    storageDriverUnlock(driver);
    for (i = 0 ; i < got ; i++)
        VIR_FREE(names[i]);
373
    memset(names, 0, nnames * sizeof(*names));
374 375 376 377 378
    return -1;
}

static int
storageNumDefinedPools(virConnectPtr conn) {
379
    virStorageDriverStatePtr driver = conn->storagePrivateData;
380 381
    unsigned int i, nactive = 0;

382 383 384
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
385 386
        if (!virStoragePoolObjIsActive(driver->pools.objs[i]))
            nactive++;
387 388 389
        virStoragePoolObjUnlock(driver->pools.objs[i]);
    }
    storageDriverUnlock(driver);
390 391

    return nactive;
392 393 394 395 396 397
}

static int
storageListDefinedPools(virConnectPtr conn,
                        char **const names,
                        int nnames) {
398
    virStorageDriverStatePtr driver = conn->storagePrivateData;
399
    int got = 0, i;
400

401
    storageDriverLock(driver);
402
    for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
403
        virStoragePoolObjLock(driver->pools.objs[i]);
404 405
        if (!virStoragePoolObjIsActive(driver->pools.objs[i])) {
            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
406
                virStoragePoolObjUnlock(driver->pools.objs[i]);
407
                virReportOOMError();
408 409 410 411
                goto cleanup;
            }
            got++;
        }
412
        virStoragePoolObjUnlock(driver->pools.objs[i]);
413
    }
414
    storageDriverUnlock(driver);
415 416 417
    return got;

 cleanup:
418
    storageDriverUnlock(driver);
419
    for (i = 0 ; i < got ; i++) {
420
        VIR_FREE(names[i]);
421
    }
422
    memset(names, 0, nnames * sizeof(*names));
423 424 425
    return -1;
}

426 427
/* This method is required to be re-entrant / thread safe, so
   uses no driver lock */
428 429 430 431 432 433 434 435
static char *
storageFindPoolSources(virConnectPtr conn,
                       const char *type,
                       const char *srcSpec,
                       unsigned int flags)
{
    int backend_type;
    virStorageBackendPtr backend;
436
    char *ret = NULL;
437

438
    backend_type = virStoragePoolTypeFromString(type);
439
    if (backend_type < 0) {
440
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
441
                              _("unknown storage pool type %s"), type);
442
        goto cleanup;
443
    }
444 445 446

    backend = virStorageBackendForType(backend_type);
    if (backend == NULL)
447
        goto cleanup;
448

449
    if (!backend->findPoolSources) {
450
        virStorageReportError(VIR_ERR_NO_SUPPORT,
451 452 453 454 455 456
                              _("pool type '%s' does not support source "
                                "discovery"), type);
        goto cleanup;
    }

    ret = backend->findPoolSources(conn, srcSpec, flags);
457

458 459
cleanup:
    return ret;
460 461 462
}


463
static int storagePoolIsActive(virStoragePoolPtr pool)
464
{
465
    virStorageDriverStatePtr driver = pool->conn->storagePrivateData;
466 467 468 469
    virStoragePoolObjPtr obj;
    int ret = -1;

    storageDriverLock(driver);
470
    obj = virStoragePoolObjFindByUUID(&driver->pools, pool->uuid);
471 472
    storageDriverUnlock(driver);
    if (!obj) {
473
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
474 475 476 477 478 479 480 481 482 483
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

cleanup:
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}

484
static int storagePoolIsPersistent(virStoragePoolPtr pool)
485
{
486
    virStorageDriverStatePtr driver = pool->conn->storagePrivateData;
487 488 489 490
    virStoragePoolObjPtr obj;
    int ret = -1;

    storageDriverLock(driver);
491
    obj = virStoragePoolObjFindByUUID(&driver->pools, pool->uuid);
492 493
    storageDriverUnlock(driver);
    if (!obj) {
494
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
495 496 497 498 499 500 501 502 503 504 505
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

cleanup:
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}


506 507 508 509
static virStoragePoolPtr
storagePoolCreate(virConnectPtr conn,
                  const char *xml,
                  unsigned int flags ATTRIBUTE_UNUSED) {
510
    virStorageDriverStatePtr driver = conn->storagePrivateData;
511
    virStoragePoolDefPtr def;
512
    virStoragePoolObjPtr pool = NULL;
513
    virStoragePoolPtr ret = NULL;
514 515
    virStorageBackendPtr backend;

516
    storageDriverLock(driver);
517
    if (!(def = virStoragePoolDefParseString(xml)))
518
        goto cleanup;
519

520 521 522 523 524
    pool = virStoragePoolObjFindByUUID(&driver->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&driver->pools, def->name);

    if (pool) {
525
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
526
                              "%s", _("storage pool already exists"));
527 528
        virStoragePoolObjUnlock(pool);
        pool = NULL;
529
        goto cleanup;
530 531
    }

532 533
    if ((backend = virStorageBackendForType(def->type)) == NULL)
        goto cleanup;
534

535
    if (!(pool = virStoragePoolObjAssignDef(&driver->pools, def)))
536 537
        goto cleanup;
    def = NULL;
538

539
    if (backend->startPool &&
540 541 542
        backend->startPool(conn, pool) < 0) {
        virStoragePoolObjRemove(&driver->pools, pool);
        pool = NULL;
543
        goto cleanup;
544
    }
545

546 547 548
    if (backend->refreshPool(conn, pool) < 0) {
        if (backend->stopPool)
            backend->stopPool(conn, pool);
549 550
        virStoragePoolObjRemove(&driver->pools, pool);
        pool = NULL;
551
        goto cleanup;
552 553 554 555 556
    }
    pool->active = 1;

    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

557 558
cleanup:
    virStoragePoolDefFree(def);
559
    if (pool)
560
        virStoragePoolObjUnlock(pool);
561
    storageDriverUnlock(driver);
562 563 564 565 566 567 568
    return ret;
}

static virStoragePoolPtr
storagePoolDefine(virConnectPtr conn,
                  const char *xml,
                  unsigned int flags ATTRIBUTE_UNUSED) {
569
    virStorageDriverStatePtr driver = conn->storagePrivateData;
570
    virStoragePoolDefPtr def;
571
    virStoragePoolObjPtr pool = NULL;
572
    virStoragePoolPtr ret = NULL;
573

574
    storageDriverLock(driver);
575
    if (!(def = virStoragePoolDefParseString(xml)))
576
        goto cleanup;
577

578
    if (virStorageBackendForType(def->type) == NULL)
579
        goto cleanup;
580

581
    if (!(pool = virStoragePoolObjAssignDef(&driver->pools, def)))
582
        goto cleanup;
583

584
    if (virStoragePoolObjSaveDef(driver, pool, def) < 0) {
585
        virStoragePoolObjRemove(&driver->pools, pool);
586
        def = NULL;
587
        goto cleanup;
588
    }
589
    def = NULL;
590 591

    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
592 593 594

cleanup:
    virStoragePoolDefFree(def);
595 596 597
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
598 599 600 601 602
    return ret;
}

static int
storagePoolUndefine(virStoragePoolPtr obj) {
603 604 605
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
606

607
    storageDriverLock(driver);
608
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
609
    if (!pool) {
610
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
611
                              "%s", _("no storage pool with matching uuid"));
612
        goto cleanup;
613 614 615
    }

    if (virStoragePoolObjIsActive(pool)) {
616
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
617
                              "%s", _("pool is still active"));
618
        goto cleanup;
619 620
    }

621
    if (pool->asyncjobs > 0) {
622
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
623 624 625 626 627
                              _("pool '%s' has asynchronous jobs running."),
                              pool->def->name);
        goto cleanup;
    }

628
    if (virStoragePoolObjDeleteDef(pool) < 0)
629
        goto cleanup;
630

631 632
    if (unlink(pool->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
        char ebuf[1024];
633
        VIR_ERROR("Failed to delete autostart link '%s': %s",
634 635
                   pool->autostartLink, virStrerror(errno, ebuf, sizeof ebuf));
    }
636

637 638
    VIR_FREE(pool->configFile);
    VIR_FREE(pool->autostartLink);
639

640
    virStoragePoolObjRemove(&driver->pools, pool);
641
    pool = NULL;
642
    ret = 0;
643

644
cleanup:
645 646 647
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
648
    return ret;
649 650 651 652 653
}

static int
storagePoolStart(virStoragePoolPtr obj,
                 unsigned int flags ATTRIBUTE_UNUSED) {
654 655
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
656
    virStorageBackendPtr backend;
657
    int ret = -1;
658

659
    storageDriverLock(driver);
660
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
661 662
    storageDriverUnlock(driver);

663
    if (!pool) {
664
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
665
                              "%s", _("no storage pool with matching uuid"));
666
        goto cleanup;
667 668
    }

669 670
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
671 672

    if (virStoragePoolObjIsActive(pool)) {
673
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
674
                              "%s", _("pool already active"));
675
        goto cleanup;
676 677 678
    }
    if (backend->startPool &&
        backend->startPool(obj->conn, pool) < 0)
679 680
        goto cleanup;

681 682 683
    if (backend->refreshPool(obj->conn, pool) < 0) {
        if (backend->stopPool)
            backend->stopPool(obj->conn, pool);
684
        goto cleanup;
685 686 687
    }

    pool->active = 1;
688
    ret = 0;
689

690
cleanup:
691 692
    if (pool)
        virStoragePoolObjUnlock(pool);
693
    return ret;
694 695 696 697 698
}

static int
storagePoolBuild(virStoragePoolPtr obj,
                 unsigned int flags) {
699 700
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
701
    virStorageBackendPtr backend;
702
    int ret = -1;
703

704
    storageDriverLock(driver);
705
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
706 707
    storageDriverUnlock(driver);

708
    if (!pool) {
709
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
710
                              "%s", _("no storage pool with matching uuid"));
711
        goto cleanup;
712 713
    }

714 715
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
716 717

    if (virStoragePoolObjIsActive(pool)) {
718
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
719
                              "%s", _("storage pool is already active"));
720
        goto cleanup;
721 722 723 724
    }

    if (backend->buildPool &&
        backend->buildPool(obj->conn, pool, flags) < 0)
725 726
        goto cleanup;
    ret = 0;
727

728
cleanup:
729 730
    if (pool)
        virStoragePoolObjUnlock(pool);
731
    return ret;
732 733 734 735 736
}


static int
storagePoolDestroy(virStoragePoolPtr obj) {
737 738
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
739
    virStorageBackendPtr backend;
740
    int ret = -1;
741

742
    storageDriverLock(driver);
743
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
744

745
    if (!pool) {
746
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
747
                              "%s", _("no storage pool with matching uuid"));
748
        goto cleanup;
749 750
    }

751 752
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
753 754

    if (!virStoragePoolObjIsActive(pool)) {
755
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
756
                              "%s", _("storage pool is not active"));
757
        goto cleanup;
758 759
    }

760
    if (pool->asyncjobs > 0) {
761
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
762 763 764 765 766
                              _("pool '%s' has asynchronous jobs running."),
                              pool->def->name);
        goto cleanup;
    }

767 768
    if (backend->stopPool &&
        backend->stopPool(obj->conn, pool) < 0)
769
        goto cleanup;
770 771 772 773 774

    virStoragePoolObjClearVols(pool);

    pool->active = 0;

775
    if (pool->configFile == NULL) {
776
        virStoragePoolObjRemove(&driver->pools, pool);
777 778
        pool = NULL;
    }
779
    ret = 0;
780

781
cleanup:
782 783 784
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
785
    return ret;
786 787 788 789 790 791
}


static int
storagePoolDelete(virStoragePoolPtr obj,
                  unsigned int flags) {
792 793
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
794
    virStorageBackendPtr backend;
795
    int ret = -1;
796

797
    storageDriverLock(driver);
798
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
799 800
    storageDriverUnlock(driver);

801
    if (!pool) {
802
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
803
                              "%s", _("no storage pool with matching uuid"));
804
        goto cleanup;
805 806
    }

807 808
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
809 810

    if (virStoragePoolObjIsActive(pool)) {
811
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
812
                              "%s", _("storage pool is still active"));
813
        goto cleanup;
814 815
    }

816
    if (pool->asyncjobs > 0) {
817
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
818 819 820 821 822
                              _("pool '%s' has asynchronous jobs running."),
                              pool->def->name);
        goto cleanup;
    }

823
    if (!backend->deletePool) {
824
        virStorageReportError(VIR_ERR_NO_SUPPORT,
825
                              "%s", _("pool does not support pool deletion"));
826
        goto cleanup;
827 828
    }
    if (backend->deletePool(obj->conn, pool, flags) < 0)
829 830
        goto cleanup;
    ret = 0;
831

832
cleanup:
833 834
    if (pool)
        virStoragePoolObjUnlock(pool);
835
    return ret;
836 837 838 839 840 841
}


static int
storagePoolRefresh(virStoragePoolPtr obj,
                   unsigned int flags ATTRIBUTE_UNUSED) {
842 843
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
844
    virStorageBackendPtr backend;
845
    int ret = -1;
846

847
    storageDriverLock(driver);
848
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
849

850
    if (!pool) {
851
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
852
                              "%s", _("no storage pool with matching uuid"));
853
        goto cleanup;
854 855
    }

856 857
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
858 859

    if (!virStoragePoolObjIsActive(pool)) {
860
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
861
                              "%s", _("storage pool is not active"));
862
        goto cleanup;
863 864
    }

865
    if (pool->asyncjobs > 0) {
866
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
867 868 869 870 871
                              _("pool '%s' has asynchronous jobs running."),
                              pool->def->name);
        goto cleanup;
    }

872
    virStoragePoolObjClearVols(pool);
873
    if (backend->refreshPool(obj->conn, pool) < 0) {
874 875 876 877 878
        if (backend->stopPool)
            backend->stopPool(obj->conn, pool);

        pool->active = 0;

879
        if (pool->configFile == NULL) {
880
            virStoragePoolObjRemove(&driver->pools, pool);
881 882
            pool = NULL;
        }
883
        goto cleanup;
884
    }
885
    ret = 0;
886

887
cleanup:
888 889
    if (pool)
        virStoragePoolObjUnlock(pool);
890
    storageDriverUnlock(driver);
891 892 893 894 895 896 897
    return ret;
}


static int
storagePoolGetInfo(virStoragePoolPtr obj,
                   virStoragePoolInfoPtr info) {
898 899 900
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
901

902
    storageDriverLock(driver);
903
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
904 905
    storageDriverUnlock(driver);

906
    if (!pool) {
907
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
908
                              "%s", _("no storage pool with matching uuid"));
909
        goto cleanup;
910 911
    }

912
    if (virStorageBackendForType(pool->def->type) == NULL)
913
        goto cleanup;
914 915 916 917 918 919 920 921 922

    memset(info, 0, sizeof(virStoragePoolInfo));
    if (pool->active)
        info->state = VIR_STORAGE_POOL_RUNNING;
    else
        info->state = VIR_STORAGE_POOL_INACTIVE;
    info->capacity = pool->def->capacity;
    info->allocation = pool->def->allocation;
    info->available = pool->def->available;
923
    ret = 0;
924

925
cleanup:
926 927
    if (pool)
        virStoragePoolObjUnlock(pool);
928
    return ret;
929 930 931 932 933
}

static char *
storagePoolDumpXML(virStoragePoolPtr obj,
                   unsigned int flags ATTRIBUTE_UNUSED) {
934 935 936
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    char *ret = NULL;
937

938
    storageDriverLock(driver);
939
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
940 941
    storageDriverUnlock(driver);

942
    if (!pool) {
943
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
944
                              "%s", _("no storage pool with matching uuid"));
945
        goto cleanup;
946 947
    }

948
    ret = virStoragePoolDefFormat(pool->def);
949 950

cleanup:
951 952
    if (pool)
        virStoragePoolObjUnlock(pool);
953
    return ret;
954 955 956 957 958
}

static int
storagePoolGetAutostart(virStoragePoolPtr obj,
                        int *autostart) {
959 960 961
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
962

963
    storageDriverLock(driver);
964
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
965 966
    storageDriverUnlock(driver);

967
    if (!pool) {
968
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
969
                              "%s", _("no pool with matching uuid"));
970
        goto cleanup;
971 972 973 974 975 976 977
    }

    if (!pool->configFile) {
        *autostart = 0;
    } else {
        *autostart = pool->autostart;
    }
978
    ret = 0;
979

980
cleanup:
981 982
    if (pool)
        virStoragePoolObjUnlock(pool);
983
    return ret;
984 985 986 987 988
}

static int
storagePoolSetAutostart(virStoragePoolPtr obj,
                        int autostart) {
989 990 991
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
992

993
    storageDriverLock(driver);
994
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
995

996
    if (!pool) {
997
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
998
                              "%s", _("no pool with matching uuid"));
999
        goto cleanup;
1000 1001 1002
    }

    if (!pool->configFile) {
1003
        virStorageReportError(VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
1004
                              "%s", _("pool has no config file"));
1005
        goto cleanup;
1006 1007 1008 1009
    }

    autostart = (autostart != 0);

1010 1011 1012
    if (pool->autostart != autostart) {
        if (autostart) {
            int err;
1013

1014
            if ((err = virFileMakePath(driver->autostartDir))) {
1015
                virReportSystemError(err,
1016 1017
                                     _("cannot create autostart directory %s"),
                                     driver->autostartDir);
1018 1019
                goto cleanup;
            }
1020

1021
            if (symlink(pool->configFile, pool->autostartLink) < 0) {
1022
                virReportSystemError(errno,
1023 1024
                                     _("Failed to create symlink '%s' to '%s'"),
                                     pool->autostartLink, pool->configFile);
1025 1026 1027 1028 1029
                goto cleanup;
            }
        } else {
            if (unlink(pool->autostartLink) < 0 &&
                errno != ENOENT && errno != ENOTDIR) {
1030
                virReportSystemError(errno,
1031 1032
                                     _("Failed to delete symlink '%s'"),
                                     pool->autostartLink);
1033 1034
                goto cleanup;
            }
1035
        }
1036
        pool->autostart = autostart;
1037
    }
1038
    ret = 0;
1039

1040
cleanup:
1041 1042
    if (pool)
        virStoragePoolObjUnlock(pool);
1043
    storageDriverUnlock(driver);
1044
    return ret;
1045 1046 1047 1048 1049
}


static int
storagePoolNumVolumes(virStoragePoolPtr obj) {
1050 1051 1052
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
1053

1054
    storageDriverLock(driver);
1055
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1056 1057
    storageDriverUnlock(driver);

1058
    if (!pool) {
1059
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
1060
                              "%s", _("no storage pool with matching uuid"));
1061
        goto cleanup;
1062 1063 1064
    }

    if (!virStoragePoolObjIsActive(pool)) {
1065
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1066
                              "%s", _("storage pool is not active"));
1067
        goto cleanup;
1068
    }
1069
    ret = pool->volumes.count;
1070

1071
cleanup:
1072 1073
    if (pool)
        virStoragePoolObjUnlock(pool);
1074
    return ret;
1075 1076 1077 1078 1079 1080
}

static int
storagePoolListVolumes(virStoragePoolPtr obj,
                       char **const names,
                       int maxnames) {
1081 1082
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1083
    int i, n = 0;
1084

1085 1086
    memset(names, 0, maxnames * sizeof(*names));

1087
    storageDriverLock(driver);
1088
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1089 1090
    storageDriverUnlock(driver);

1091
    if (!pool) {
1092
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
1093
                              "%s", _("no storage pool with matching uuid"));
1094
        goto cleanup;
1095 1096 1097
    }

    if (!virStoragePoolObjIsActive(pool)) {
1098
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1099
                              "%s", _("storage pool is not active"));
1100
        goto cleanup;
1101 1102
    }

1103 1104
    for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) {
1105
            virReportOOMError();
1106 1107 1108 1109
            goto cleanup;
        }
    }

1110
    virStoragePoolObjUnlock(pool);
1111
    return n;
1112 1113

 cleanup:
1114 1115
    if (pool)
        virStoragePoolObjUnlock(pool);
1116
    for (n = 0 ; n < maxnames ; n++)
1117
        VIR_FREE(names[n]);
1118

1119
    memset(names, 0, maxnames * sizeof(*names));
1120 1121 1122 1123 1124 1125 1126
    return -1;
}


static virStorageVolPtr
storageVolumeLookupByName(virStoragePoolPtr obj,
                          const char *name) {
1127 1128
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1129
    virStorageVolDefPtr vol;
1130
    virStorageVolPtr ret = NULL;
1131

1132
    storageDriverLock(driver);
1133
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1134 1135
    storageDriverUnlock(driver);

1136
    if (!pool) {
1137
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
1138
                              "%s", _("no storage pool with matching uuid"));
1139
        goto cleanup;
1140 1141 1142
    }

    if (!virStoragePoolObjIsActive(pool)) {
1143
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1144
                              "%s", _("storage pool is not active"));
1145
        goto cleanup;
1146 1147 1148 1149 1150
    }

    vol = virStorageVolDefFindByName(pool, name);

    if (!vol) {
1151 1152
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
1153
                              name);
1154
        goto cleanup;
1155 1156
    }

1157 1158 1159
    ret = virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key);

cleanup:
1160 1161
    if (pool)
        virStoragePoolObjUnlock(pool);
1162
    return ret;
1163 1164 1165 1166 1167 1168
}


static virStorageVolPtr
storageVolumeLookupByKey(virConnectPtr conn,
                         const char *key) {
1169
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1170
    unsigned int i;
1171
    virStorageVolPtr ret = NULL;
1172

1173 1174 1175
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1176 1177 1178
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
            virStorageVolDefPtr vol =
                virStorageVolDefFindByKey(driver->pools.objs[i], key);
1179

1180
            if (vol)
1181
                ret = virGetStorageVol(conn,
1182 1183 1184
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1185
        }
1186
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1187
    }
1188
    storageDriverUnlock(driver);
1189

1190
    if (!ret)
1191
        virStorageReportError(VIR_ERR_INVALID_STORAGE_VOL,
1192 1193 1194
                              "%s", _("no storage vol with matching key"));

    return ret;
1195 1196 1197 1198 1199
}

static virStorageVolPtr
storageVolumeLookupByPath(virConnectPtr conn,
                          const char *path) {
1200
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1201
    unsigned int i;
1202
    virStorageVolPtr ret = NULL;
1203

1204 1205 1206
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1207
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
1208
            virStorageVolDefPtr vol;
1209 1210
            const char *stable_path;

1211
            stable_path = virStorageBackendStablePath(driver->pools.objs[i],
1212 1213 1214 1215 1216 1217
                                                      path);
            /*
             * virStorageBackendStablePath already does
             * virStorageReportError if it fails; we just need to keep
             * propagating the return code
             */
1218 1219
            if (stable_path == NULL) {
                virStoragePoolObjUnlock(driver->pools.objs[i]);
1220
                goto cleanup;
1221
            }
1222 1223 1224 1225

            vol = virStorageVolDefFindByPath(driver->pools.objs[i],
                                             stable_path);
            VIR_FREE(stable_path);
1226

1227
            if (vol)
1228 1229 1230 1231
                ret = virGetStorageVol(conn,
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1232
        }
1233
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1234 1235
    }

1236
    if (!ret)
1237
        virStorageReportError(VIR_ERR_INVALID_STORAGE_VOL,
1238 1239 1240
                              "%s", _("no storage vol with matching path"));

cleanup:
1241
    storageDriverUnlock(driver);
1242
    return ret;
1243 1244
}

1245 1246
static int storageVolumeDelete(virStorageVolPtr obj, unsigned int flags);

1247 1248 1249 1250
static virStorageVolPtr
storageVolumeCreateXML(virStoragePoolPtr obj,
                       const char *xmldesc,
                       unsigned int flags ATTRIBUTE_UNUSED) {
1251 1252
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1253
    virStorageBackendPtr backend;
1254 1255
    virStorageVolDefPtr voldef = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1256

1257
    storageDriverLock(driver);
1258
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1259 1260
    storageDriverUnlock(driver);

1261
    if (!pool) {
1262
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
1263
                              "%s", _("no storage pool with matching uuid"));
1264
        goto cleanup;
1265 1266 1267
    }

    if (!virStoragePoolObjIsActive(pool)) {
1268
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1269
                              "%s", _("storage pool is not active"));
1270
        goto cleanup;
1271 1272 1273
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
1274
        goto cleanup;
1275

1276
    voldef = virStorageVolDefParseString(pool->def, xmldesc);
1277
    if (voldef == NULL)
1278
        goto cleanup;
1279

1280
    if (virStorageVolDefFindByName(pool, voldef->name)) {
1281
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
1282
                              "%s", _("storage vol already exists"));
1283
        goto cleanup;
1284 1285
    }

1286 1287
    if (VIR_REALLOC_N(pool->volumes.objs,
                      pool->volumes.count+1) < 0) {
1288
        virReportOOMError();
1289
        goto cleanup;
1290 1291
    }

1292
    if (!backend->createVol) {
1293
        virStorageReportError(VIR_ERR_NO_SUPPORT,
1294 1295
                              "%s", _("storage pool does not support volume "
                                      "creation"));
1296
        goto cleanup;
1297 1298
    }

1299
    if (backend->createVol(obj->conn, pool, voldef) < 0) {
1300
        goto cleanup;
1301 1302
    }

1303 1304 1305
    pool->volumes.objs[pool->volumes.count++] = voldef;
    volobj = virGetStorageVol(obj->conn, pool->def->name, voldef->name,
                              voldef->key);
1306

1307 1308 1309 1310 1311
    if (volobj && backend->buildVol) {
        int buildret;
        virStorageVolDefPtr buildvoldef = NULL;

        if (VIR_ALLOC(buildvoldef) < 0) {
1312
            virReportOOMError();
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
            voldef = NULL;
            goto cleanup;
        }

        /* Make a shallow copy of the 'defined' volume definition, since the
         * original allocation value will change as the user polls 'info',
         * but we only need the initial requested values
         */
        memcpy(buildvoldef, voldef, sizeof(*voldef));

        /* Drop the pool lock during volume allocation */
        pool->asyncjobs++;
        voldef->building = 1;
        virStoragePoolObjUnlock(pool);

1328
        buildret = backend->buildVol(obj->conn, pool, buildvoldef);
1329

1330
        storageDriverLock(driver);
1331
        virStoragePoolObjLock(pool);
1332 1333
        storageDriverUnlock(driver);

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
        voldef->building = 0;
        pool->asyncjobs--;

        voldef = NULL;
        VIR_FREE(buildvoldef);

        if (buildret < 0) {
            virStoragePoolObjUnlock(pool);
            storageVolumeDelete(volobj, 0);
            pool = NULL;
            goto cleanup;
        }

    }

    ret = volobj;
    volobj = NULL;
    voldef = NULL;
1352

1353
cleanup:
1354 1355 1356
    if (volobj)
        virUnrefStorageVol(volobj);
    virStorageVolDefFree(voldef);
1357 1358
    if (pool)
        virStoragePoolObjUnlock(pool);
1359
    return ret;
1360 1361
}

1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
static virStorageVolPtr
storageVolumeCreateXMLFrom(virStoragePoolPtr obj,
                           const char *xmldesc,
                           virStorageVolPtr vobj,
                           unsigned int flags ATTRIBUTE_UNUSED) {
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool, origpool = NULL;
    virStorageBackendPtr backend;
    virStorageVolDefPtr origvol = NULL, newvol = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1372
    int buildret;
1373 1374 1375

    storageDriverLock(driver);
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1376
    if (pool && STRNEQ(obj->name, vobj->pool)) {
1377
        virStoragePoolObjUnlock(pool);
1378
        origpool = virStoragePoolObjFindByName(&driver->pools, vobj->pool);
1379
        virStoragePoolObjLock(pool);
1380
    }
1381 1382
    storageDriverUnlock(driver);
    if (!pool) {
1383
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
1384 1385 1386 1387
                              "%s", _("no storage pool with matching uuid"));
        goto cleanup;
    }

1388
    if (STRNEQ(obj->name, vobj->pool) && !origpool) {
1389 1390
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
                              _("no storage pool with matching name '%s'"),
1391
                              vobj->pool);
1392 1393 1394 1395
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1396
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1397 1398 1399 1400
                              "%s", _("storage pool is not active"));
        goto cleanup;
    }

1401
    if (origpool && !virStoragePoolObjIsActive(origpool)) {
1402
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1403 1404 1405 1406 1407 1408 1409
                              "%s", _("storage pool is not active"));
        goto cleanup;
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;

1410
    origvol = virStorageVolDefFindByName(origpool ? origpool : pool, vobj->name);
1411
    if (!origvol) {
1412 1413
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
1414
                              vobj->name);
1415 1416 1417
        goto cleanup;
    }

1418
    newvol = virStorageVolDefParseString(pool->def, xmldesc);
1419 1420 1421 1422
    if (newvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(pool, newvol->name)) {
1423
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
1424 1425 1426 1427 1428 1429 1430 1431 1432
                              _("storage volume name '%s' already in use."),
                              newvol->name);
        goto cleanup;
    }

    /* Is there ever a valid case for this? */
    if (newvol->capacity < origvol->capacity)
        newvol->capacity = origvol->capacity;

1433 1434 1435 1436 1437
    /* Make sure allocation is at least as large as the destination cap,
     * to make absolutely sure we copy all possible contents */
    if (newvol->allocation < origvol->capacity)
        newvol->allocation = origvol->capacity;

1438
    if (!backend->buildVolFrom) {
1439
        virStorageReportError(VIR_ERR_NO_SUPPORT,
1440 1441 1442 1443 1444
                              "%s", _("storage pool does not support volume creation from an existing volume"));
        goto cleanup;
    }

    if (origvol->building) {
1445
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
                              _("volume '%s' is still being allocated."),
                              origvol->name);
        goto cleanup;
    }

    if (backend->refreshVol &&
        backend->refreshVol(obj->conn, pool, origvol) < 0)
        goto cleanup;

    if (VIR_REALLOC_N(pool->volumes.objs,
                      pool->volumes.count+1) < 0) {
1457
        virReportOOMError();
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
        goto cleanup;
    }

    /* 'Define' the new volume so we get async progress reporting */
    if (backend->createVol(obj->conn, pool, newvol) < 0) {
        goto cleanup;
    }

    pool->volumes.objs[pool->volumes.count++] = newvol;
    volobj = virGetStorageVol(obj->conn, pool->def->name, newvol->name,
                              newvol->key);

    /* Drop the pool lock during volume allocation */
    pool->asyncjobs++;
    origvol->building = 1;
    newvol->building = 1;
    virStoragePoolObjUnlock(pool);

1476
    if (origpool) {
1477 1478 1479 1480
        origpool->asyncjobs++;
        virStoragePoolObjUnlock(origpool);
    }

1481
    buildret = backend->buildVolFrom(obj->conn, pool, newvol, origvol, flags);
1482 1483 1484

    storageDriverLock(driver);
    virStoragePoolObjLock(pool);
1485
    if (origpool)
1486 1487 1488 1489 1490 1491 1492 1493
        virStoragePoolObjLock(origpool);
    storageDriverUnlock(driver);

    origvol->building = 0;
    newvol->building = 0;
    newvol = NULL;
    pool->asyncjobs--;

1494
    if (origpool) {
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
        origpool->asyncjobs--;
        virStoragePoolObjUnlock(origpool);
        origpool = NULL;
    }

    if (buildret < 0) {
        virStoragePoolObjUnlock(pool);
        storageVolumeDelete(volobj, 0);
        pool = NULL;
        goto cleanup;
    }

    ret = volobj;
    volobj = NULL;

cleanup:
    if (volobj)
        virUnrefStorageVol(volobj);
    virStorageVolDefFree(newvol);
    if (pool)
        virStoragePoolObjUnlock(pool);
1516
    if (origpool)
1517 1518 1519 1520
        virStoragePoolObjUnlock(origpool);
    return ret;
}

1521 1522 1523
static int
storageVolumeDelete(virStorageVolPtr obj,
                    unsigned int flags) {
1524 1525
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1526
    virStorageBackendPtr backend;
1527
    virStorageVolDefPtr vol = NULL;
1528
    unsigned int i;
1529
    int ret = -1;
1530

1531
    storageDriverLock(driver);
1532
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
1533 1534
    storageDriverUnlock(driver);

1535
    if (!pool) {
1536
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
1537
                              "%s", _("no storage pool with matching uuid"));
1538
        goto cleanup;
1539 1540 1541
    }

    if (!virStoragePoolObjIsActive(pool)) {
1542
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1543
                              "%s", _("storage pool is not active"));
1544
        goto cleanup;
1545 1546 1547
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
1548
        goto cleanup;
1549 1550 1551 1552

    vol = virStorageVolDefFindByName(pool, obj->name);

    if (!vol) {
1553
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
1554 1555
                             _("no storage vol with matching name '%s'"),
                              obj->name);
1556
        goto cleanup;
1557 1558
    }

1559
    if (vol->building) {
1560
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1561 1562 1563 1564 1565
                              _("volume '%s' is still being allocated."),
                              vol->name);
        goto cleanup;
    }

1566
    if (!backend->deleteVol) {
1567
        virStorageReportError(VIR_ERR_NO_SUPPORT,
J
Jim Meyering 已提交
1568
                              "%s", _("storage pool does not support vol deletion"));
1569

1570
        goto cleanup;
1571 1572
    }

1573 1574 1575
    if (backend->deleteVol(obj->conn, pool, vol, flags) < 0)
        goto cleanup;

1576 1577 1578
    for (i = 0 ; i < pool->volumes.count ; i++) {
        if (pool->volumes.objs[i] == vol) {
            virStorageVolDefFree(vol);
1579
            vol = NULL;
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589

            if (i < (pool->volumes.count - 1))
                memmove(pool->volumes.objs + i, pool->volumes.objs + i + 1,
                        sizeof(*(pool->volumes.objs)) * (pool->volumes.count - (i + 1)));

            if (VIR_REALLOC_N(pool->volumes.objs, pool->volumes.count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            pool->volumes.count--;

1590 1591 1592
            break;
        }
    }
1593
    ret = 0;
1594

1595
cleanup:
1596 1597
    if (pool)
        virStoragePoolObjUnlock(pool);
1598
    return ret;
1599 1600 1601 1602 1603
}

static int
storageVolumeGetInfo(virStorageVolPtr obj,
                     virStorageVolInfoPtr info) {
1604 1605
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1606 1607
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
1608
    int ret = -1;
1609

1610
    storageDriverLock(driver);
1611
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
1612 1613
    storageDriverUnlock(driver);

1614
    if (!pool) {
1615
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
1616
                              "%s", _("no storage pool with matching uuid"));
1617
        goto cleanup;
1618 1619 1620
    }

    if (!virStoragePoolObjIsActive(pool)) {
1621
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1622
                              "%s", _("storage pool is not active"));
1623
        goto cleanup;
1624 1625 1626 1627 1628
    }

    vol = virStorageVolDefFindByName(pool, obj->name);

    if (!vol) {
1629 1630
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
1631
                              obj->name);
1632
        goto cleanup;
1633 1634 1635
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
1636
        goto cleanup;
1637 1638 1639

    if (backend->refreshVol &&
        backend->refreshVol(obj->conn, pool, vol) < 0)
1640
        goto cleanup;
1641 1642

    memset(info, 0, sizeof(*info));
1643
    info->type = vol->type;
1644 1645
    info->capacity = vol->capacity;
    info->allocation = vol->allocation;
1646
    ret = 0;
1647

1648
cleanup:
1649 1650
    if (pool)
        virStoragePoolObjUnlock(pool);
1651
    return ret;
1652 1653 1654 1655 1656
}

static char *
storageVolumeGetXMLDesc(virStorageVolPtr obj,
                        unsigned int flags ATTRIBUTE_UNUSED) {
1657 1658
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1659 1660
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
1661
    char *ret = NULL;
1662

1663
    storageDriverLock(driver);
1664
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
1665 1666
    storageDriverUnlock(driver);

1667
    if (!pool) {
1668
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
1669
                              "%s", _("no storage pool with matching uuid"));
1670
        goto cleanup;
1671 1672 1673
    }

    if (!virStoragePoolObjIsActive(pool)) {
1674
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1675
                              "%s", _("storage pool is not active"));
1676
        goto cleanup;
1677 1678 1679 1680 1681
    }

    vol = virStorageVolDefFindByName(pool, obj->name);

    if (!vol) {
1682
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
1683 1684
                             _("no storage vol with matching name '%s'"),
                              obj->name);
1685
        goto cleanup;
1686 1687 1688
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
1689
        goto cleanup;
1690 1691 1692 1693

    if (backend->refreshVol &&
        backend->refreshVol(obj->conn, pool, vol) < 0)
        goto cleanup;
1694

1695
    ret = virStorageVolDefFormat(pool->def, vol);
1696 1697

cleanup:
1698 1699 1700
    if (pool)
        virStoragePoolObjUnlock(pool);

1701
    return ret;
1702 1703 1704 1705
}

static char *
storageVolumeGetPath(virStorageVolPtr obj) {
1706
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
1707
    virStoragePoolObjPtr pool;
1708
    virStorageVolDefPtr vol;
1709
    char *ret = NULL;
1710

1711 1712 1713
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);
1714
    if (!pool) {
1715
        virStorageReportError(VIR_ERR_INVALID_STORAGE_POOL,
J
Jim Meyering 已提交
1716
                              "%s", _("no storage pool with matching uuid"));
1717
        goto cleanup;
1718 1719 1720
    }

    if (!virStoragePoolObjIsActive(pool)) {
1721
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1722
                              "%s", _("storage pool is not active"));
1723
        goto cleanup;
1724 1725 1726 1727 1728
    }

    vol = virStorageVolDefFindByName(pool, obj->name);

    if (!vol) {
1729 1730
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
1731
                              obj->name);
1732
        goto cleanup;
1733 1734 1735
    }

    ret = strdup(vol->target.path);
1736
    if (ret == NULL)
1737
        virReportOOMError();
1738 1739

cleanup:
1740 1741
    if (pool)
        virStoragePoolObjUnlock(pool);
1742 1743 1744 1745
    return ret;
}

static virStorageDriver storageDriver = {
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
    .name = "storage",
    .open = storageOpen,
    .close = storageClose,
    .numOfPools = storageNumPools,
    .listPools = storageListPools,
    .numOfDefinedPools = storageNumDefinedPools,
    .listDefinedPools = storageListDefinedPools,
    .findPoolSources = storageFindPoolSources,
    .poolLookupByName = storagePoolLookupByName,
    .poolLookupByUUID = storagePoolLookupByUUID,
    .poolLookupByVolume = storagePoolLookupByVolume,
    .poolCreateXML = storagePoolCreate,
    .poolDefineXML = storagePoolDefine,
    .poolBuild = storagePoolBuild,
    .poolUndefine = storagePoolUndefine,
    .poolCreate = storagePoolStart,
    .poolDestroy = storagePoolDestroy,
    .poolDelete = storagePoolDelete,
    .poolRefresh = storagePoolRefresh,
    .poolGetInfo = storagePoolGetInfo,
    .poolGetXMLDesc = storagePoolDumpXML,
    .poolGetAutostart = storagePoolGetAutostart,
    .poolSetAutostart = storagePoolSetAutostart,
    .poolNumOfVolumes = storagePoolNumVolumes,
    .poolListVolumes = storagePoolListVolumes,

    .volLookupByName = storageVolumeLookupByName,
    .volLookupByKey = storageVolumeLookupByKey,
    .volLookupByPath = storageVolumeLookupByPath,
    .volCreateXML = storageVolumeCreateXML,
1776
    .volCreateXMLFrom = storageVolumeCreateXMLFrom,
1777 1778 1779 1780
    .volDelete = storageVolumeDelete,
    .volGetInfo = storageVolumeGetInfo,
    .volGetXMLDesc = storageVolumeGetXMLDesc,
    .volGetPath = storageVolumeGetPath,
1781 1782 1783

    .poolIsActive = storagePoolIsActive,
    .poolIsPersistent = storagePoolIsPersistent,
1784 1785 1786 1787
};


static virStateDriver stateDriver = {
1788
    .name = "Storage",
1789 1790 1791 1792
    .initialize = storageDriverStartup,
    .cleanup = storageDriverShutdown,
    .reload = storageDriverReload,
    .active = storageDriverActive,
1793 1794 1795 1796 1797 1798 1799
};

int storageRegister(void) {
    virRegisterStorageDriver(&storageDriver);
    virRegisterStateDriver(&stateDriver);
    return 0;
}