storage_driver.c 65.8 KB
Newer Older
1 2 3
/*
 * storage_driver.c: core driver for storage APIs
 *
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
O
Osier Yang 已提交
18 19
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
29 30 31 32
#include <sys/stat.h>
#include <sys/param.h>
#include <fcntl.h>

R
Richard W.M. Jones 已提交
33
#if HAVE_PWD_H
34
# include <pwd.h>
R
Richard W.M. Jones 已提交
35
#endif
36 37 38
#include <errno.h>
#include <string.h>

39
#include "virterror_internal.h"
40
#include "datatypes.h"
41 42 43 44
#include "driver.h"
#include "util.h"
#include "storage_driver.h"
#include "storage_conf.h"
45
#include "memory.h"
46
#include "storage_backend.h"
47
#include "logging.h"
E
Eric Blake 已提交
48
#include "virfile.h"
49
#include "fdstream.h"
50
#include "configmake.h"
51

52 53
#define VIR_FROM_THIS VIR_FROM_STORAGE

54 55 56 57
static virStorageDriverStatePtr driverState;

static int storageDriverShutdown(void);

58 59
static void storageDriverLock(virStorageDriverStatePtr driver)
{
60
    virMutexLock(&driver->lock);
61 62 63
}
static void storageDriverUnlock(virStorageDriverStatePtr driver)
{
64
    virMutexUnlock(&driver->lock);
65
}
66 67 68

static void
storageDriverAutostart(virStorageDriverStatePtr driver) {
69
    unsigned int i;
70

71 72
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjPtr pool = driver->pools.objs[i];
73 74
        virStorageBackendPtr backend;
        bool started = false;
75

76
        virStoragePoolObjLock(pool);
77 78 79 80 81
        if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
            VIR_ERROR(_("Missing backend %d"), pool->def->type);
            virStoragePoolObjUnlock(pool);
            continue;
        }
82

83 84 85 86 87 88 89 90 91 92 93 94 95
        if (backend->checkPool &&
            backend->checkPool(NULL, pool, &started) < 0) {
            virErrorPtr err = virGetLastError();
            VIR_ERROR(_("Failed to initialize storage pool '%s': %s"),
                      pool->def->name, err ? err->message :
                      _("no error message found"));
            virStoragePoolObjUnlock(pool);
            continue;
        }

        if (!started &&
            pool->autostart &&
            !virStoragePoolObjIsActive(pool)) {
96 97 98
            if (backend->startPool &&
                backend->startPool(NULL, pool) < 0) {
                virErrorPtr err = virGetLastError();
99
                VIR_ERROR(_("Failed to autostart storage pool '%s': %s"),
100
                          pool->def->name, err ? err->message :
101
                          _("no error message found"));
102
                virStoragePoolObjUnlock(pool);
103 104
                continue;
            }
105 106
            started = true;
        }
107

108
        if (started) {
109 110 111 112
            if (backend->refreshPool(NULL, pool) < 0) {
                virErrorPtr err = virGetLastError();
                if (backend->stopPool)
                    backend->stopPool(NULL, pool);
113
                VIR_ERROR(_("Failed to autostart storage pool '%s': %s"),
114
                          pool->def->name, err ? err->message :
115
                          _("no error message found"));
116
                virStoragePoolObjUnlock(pool);
117 118 119 120
                continue;
            }
            pool->active = 1;
        }
121
        virStoragePoolObjUnlock(pool);
122 123 124 125 126 127 128 129 130
    }
}

/**
 * virStorageStartup:
 *
 * Initialization function for the QEmu daemon
 */
static int
131 132
storageDriverStartup(int privileged)
{
133 134
    char *base = NULL;

135
    if (VIR_ALLOC(driverState) < 0)
136 137
        return -1;

138 139 140 141
    if (virMutexInit(&driverState->lock) < 0) {
        VIR_FREE(driverState);
        return -1;
    }
142 143
    storageDriverLock(driverState);

144
    if (privileged) {
145
        if ((base = strdup (SYSCONFDIR "/libvirt")) == NULL)
146 147
            goto out_of_memory;
    } else {
148
        base = virGetUserConfigDirectory();
149
        if (!base)
150
            goto error;
151 152
    }

153
    /* Configuration paths are either $USER_CONFIG_HOME/libvirt/storage/... (session) or
154 155
     * /etc/libvirt/storage/... (system).
     */
156 157
    if (virAsprintf(&driverState->configDir,
                    "%s/storage", base) == -1)
158 159
        goto out_of_memory;

160 161
    if (virAsprintf(&driverState->autostartDir,
                    "%s/storage/autostart", base) == -1)
162 163
        goto out_of_memory;

164
    VIR_FREE(base);
165

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

172
    storageDriverUnlock(driverState);
173 174
    return 0;

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

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

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

    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) {
214
    unsigned int i;
215
    int active = 0;
216 217 218 219

    if (!driverState)
        return 0;

220 221 222 223
    storageDriverLock(driverState);

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

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

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

243
    storageDriverLock(driverState);
244 245

    /* free inactive pools */
246 247 248 249
    virStoragePoolObjListFree(&driverState->pools);

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

    return 0;
}



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

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

270
    if (!pool) {
271 272
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no pool with matching uuid"));
273
        goto cleanup;
274 275 276
    }

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

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

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

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

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

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

cleanup:
304 305
    if (pool)
        virStoragePoolObjUnlock(pool);
306 307 308 309 310 311 312 313 314 315 316
    return ret;
}

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

static virDrvOpenStatus
storageOpen(virConnectPtr conn,
            virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
317 318 319 320
            unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    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) {
336
    virStorageDriverStatePtr driver = conn->storagePrivateData;
337 338
    unsigned int i, nactive = 0;

339 340 341
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
342 343
        if (virStoragePoolObjIsActive(driver->pools.objs[i]))
            nactive++;
344 345 346
        virStoragePoolObjUnlock(driver->pools.objs[i]);
    }
    storageDriverUnlock(driver);
347 348

    return nactive;
349 350 351 352 353 354
}

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

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

 cleanup:
375 376 377
    storageDriverUnlock(driver);
    for (i = 0 ; i < got ; i++)
        VIR_FREE(names[i]);
378
    memset(names, 0, nnames * sizeof(*names));
379 380 381 382 383
    return -1;
}

static int
storageNumDefinedPools(virConnectPtr conn) {
384
    virStorageDriverStatePtr driver = conn->storagePrivateData;
385 386
    unsigned int i, nactive = 0;

387 388 389
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
390 391
        if (!virStoragePoolObjIsActive(driver->pools.objs[i]))
            nactive++;
392 393 394
        virStoragePoolObjUnlock(driver->pools.objs[i]);
    }
    storageDriverUnlock(driver);
395 396

    return nactive;
397 398 399 400 401 402
}

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

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

 cleanup:
423
    storageDriverUnlock(driver);
424
    for (i = 0 ; i < got ; i++) {
425
        VIR_FREE(names[i]);
426
    }
427
    memset(names, 0, nnames * sizeof(*names));
428 429 430
    return -1;
}

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

443
    backend_type = virStoragePoolTypeFromString(type);
444
    if (backend_type < 0) {
445 446
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
447
        goto cleanup;
448
    }
449 450 451

    backend = virStorageBackendForType(backend_type);
    if (backend == NULL)
452
        goto cleanup;
453

454
    if (!backend->findPoolSources) {
455 456 457
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source "
                         "discovery"), type);
458 459 460 461
        goto cleanup;
    }

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

463 464
cleanup:
    return ret;
465 466 467
}


468
static int storagePoolIsActive(virStoragePoolPtr pool)
469
{
470
    virStorageDriverStatePtr driver = pool->conn->storagePrivateData;
471 472 473 474
    virStoragePoolObjPtr obj;
    int ret = -1;

    storageDriverLock(driver);
475
    obj = virStoragePoolObjFindByUUID(&driver->pools, pool->uuid);
476 477
    storageDriverUnlock(driver);
    if (!obj) {
478
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
479 480 481 482 483 484 485 486 487 488
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

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

489
static int storagePoolIsPersistent(virStoragePoolPtr pool)
490
{
491
    virStorageDriverStatePtr driver = pool->conn->storagePrivateData;
492 493 494 495
    virStoragePoolObjPtr obj;
    int ret = -1;

    storageDriverLock(driver);
496
    obj = virStoragePoolObjFindByUUID(&driver->pools, pool->uuid);
497 498
    storageDriverUnlock(driver);
    if (!obj) {
499
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
500 501 502 503 504 505 506 507 508 509 510
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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


511 512 513
static virStoragePoolPtr
storagePoolCreate(virConnectPtr conn,
                  const char *xml,
E
Eric Blake 已提交
514 515
                  unsigned int flags)
{
516
    virStorageDriverStatePtr driver = conn->storagePrivateData;
517
    virStoragePoolDefPtr def;
518
    virStoragePoolObjPtr pool = NULL;
519
    virStoragePoolPtr ret = NULL;
520 521
    virStorageBackendPtr backend;

E
Eric Blake 已提交
522 523
    virCheckFlags(0, NULL);

524
    storageDriverLock(driver);
525
    if (!(def = virStoragePoolDefParseString(xml)))
526
        goto cleanup;
527

528
    if (virStoragePoolObjIsDuplicate(&driver->pools, def, 1) < 0)
529
        goto cleanup;
530

531 532 533
    if (virStoragePoolSourceFindDuplicate(&driver->pools, def) < 0)
        goto cleanup;

534 535
    if ((backend = virStorageBackendForType(def->type)) == NULL)
        goto cleanup;
536

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

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

548 549 550
    if (backend->refreshPool(conn, pool) < 0) {
        if (backend->stopPool)
            backend->stopPool(conn, pool);
551 552
        virStoragePoolObjRemove(&driver->pools, pool);
        pool = NULL;
553
        goto cleanup;
554
    }
555
    VIR_INFO("Creating storage pool '%s'", pool->def->name);
556 557 558 559
    pool->active = 1;

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

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

static virStoragePoolPtr
storagePoolDefine(virConnectPtr conn,
                  const char *xml,
E
Eric Blake 已提交
571 572
                  unsigned int flags)
{
573
    virStorageDriverStatePtr driver = conn->storagePrivateData;
574
    virStoragePoolDefPtr def;
575
    virStoragePoolObjPtr pool = NULL;
576
    virStoragePoolPtr ret = NULL;
577

E
Eric Blake 已提交
578 579
    virCheckFlags(0, NULL);

580
    storageDriverLock(driver);
581
    if (!(def = virStoragePoolDefParseString(xml)))
582
        goto cleanup;
583

584 585 586
    if (virStoragePoolObjIsDuplicate(&driver->pools, def, 0) < 0)
        goto cleanup;

587 588 589
    if (virStoragePoolSourceFindDuplicate(&driver->pools, def) < 0)
        goto cleanup;

590
    if (virStorageBackendForType(def->type) == NULL)
591
        goto cleanup;
592

593
    if (!(pool = virStoragePoolObjAssignDef(&driver->pools, def)))
594
        goto cleanup;
595

596
    if (virStoragePoolObjSaveDef(driver, pool, def) < 0) {
597
        virStoragePoolObjRemove(&driver->pools, pool);
598
        def = NULL;
599
        goto cleanup;
600
    }
601
    def = NULL;
602

603
    VIR_INFO("Defining storage pool '%s'", pool->def->name);
604
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
605 606 607

cleanup:
    virStoragePoolDefFree(def);
608 609 610
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
611 612 613 614 615
    return ret;
}

static int
storagePoolUndefine(virStoragePoolPtr obj) {
616 617 618
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
619

620
    storageDriverLock(driver);
621
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
622
    if (!pool) {
623 624
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
625
        goto cleanup;
626 627 628
    }

    if (virStoragePoolObjIsActive(pool)) {
629 630
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("pool is still active"));
631
        goto cleanup;
632 633
    }

634
    if (pool->asyncjobs > 0) {
635 636 637
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("pool '%s' has asynchronous jobs running."),
                       pool->def->name);
638 639 640
        goto cleanup;
    }

641
    if (virStoragePoolObjDeleteDef(pool) < 0)
642
        goto cleanup;
643

644 645
    if (unlink(pool->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
        char ebuf[1024];
646
        VIR_ERROR(_("Failed to delete autostart link '%s': %s"),
647
                  pool->autostartLink, virStrerror(errno, ebuf, sizeof(ebuf)));
648
    }
649

650 651
    VIR_FREE(pool->configFile);
    VIR_FREE(pool->autostartLink);
652

653
    VIR_INFO("Undefining storage pool '%s'", pool->def->name);
654
    virStoragePoolObjRemove(&driver->pools, pool);
655
    pool = NULL;
656
    ret = 0;
657

658
cleanup:
659 660 661
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
662
    return ret;
663 664 665 666
}

static int
storagePoolStart(virStoragePoolPtr obj,
E
Eric Blake 已提交
667 668
                 unsigned int flags)
{
669 670
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
671
    virStorageBackendPtr backend;
672
    int ret = -1;
673

E
Eric Blake 已提交
674 675
    virCheckFlags(0, -1);

676
    storageDriverLock(driver);
677
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
678 679
    storageDriverUnlock(driver);

680
    if (!pool) {
681 682
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
683
        goto cleanup;
684 685
    }

686 687
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
688 689

    if (virStoragePoolObjIsActive(pool)) {
690 691
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("pool already active"));
692
        goto cleanup;
693 694 695
    }
    if (backend->startPool &&
        backend->startPool(obj->conn, pool) < 0)
696 697
        goto cleanup;

698 699 700
    if (backend->refreshPool(obj->conn, pool) < 0) {
        if (backend->stopPool)
            backend->stopPool(obj->conn, pool);
701
        goto cleanup;
702 703
    }

704
    VIR_INFO("Starting up storage pool '%s'", pool->def->name);
705
    pool->active = 1;
706
    ret = 0;
707

708
cleanup:
709 710
    if (pool)
        virStoragePoolObjUnlock(pool);
711
    return ret;
712 713 714 715 716
}

static int
storagePoolBuild(virStoragePoolPtr obj,
                 unsigned int flags) {
717 718
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
719
    virStorageBackendPtr backend;
720
    int ret = -1;
721

722
    storageDriverLock(driver);
723
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
724 725
    storageDriverUnlock(driver);

726
    if (!pool) {
727 728
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
729
        goto cleanup;
730 731
    }

732 733
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
734 735

    if (virStoragePoolObjIsActive(pool)) {
736 737
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is already active"));
738
        goto cleanup;
739 740 741 742
    }

    if (backend->buildPool &&
        backend->buildPool(obj->conn, pool, flags) < 0)
743 744
        goto cleanup;
    ret = 0;
745

746
cleanup:
747 748
    if (pool)
        virStoragePoolObjUnlock(pool);
749
    return ret;
750 751 752 753 754
}


static int
storagePoolDestroy(virStoragePoolPtr obj) {
755 756
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
757
    virStorageBackendPtr backend;
758
    int ret = -1;
759

760
    storageDriverLock(driver);
761
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
762

763
    if (!pool) {
764 765
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
766
        goto cleanup;
767 768
    }

769 770
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
771 772

    if (!virStoragePoolObjIsActive(pool)) {
773 774
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
775
        goto cleanup;
776 777
    }

778
    if (pool->asyncjobs > 0) {
779 780 781
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("pool '%s' has asynchronous jobs running."),
                       pool->def->name);
782 783 784
        goto cleanup;
    }

785 786
    if (backend->stopPool &&
        backend->stopPool(obj->conn, pool) < 0)
787
        goto cleanup;
788 789 790 791

    virStoragePoolObjClearVols(pool);

    pool->active = 0;
792
    VIR_INFO("Shutting down storage pool '%s'", pool->def->name);
793

794
    if (pool->configFile == NULL) {
795
        virStoragePoolObjRemove(&driver->pools, pool);
796
        pool = NULL;
797 798 799 800
    } else if (pool->newDef) {
        virStoragePoolDefFree(pool->def);
        pool->def = pool->newDef;
        pool->newDef = NULL;
801
    }
802
    ret = 0;
803

804
cleanup:
805 806 807
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
808
    return ret;
809 810 811 812 813
}

static int
storagePoolDelete(virStoragePoolPtr obj,
                  unsigned int flags) {
814 815
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
816
    virStorageBackendPtr backend;
817
    int ret = -1;
818

819
    storageDriverLock(driver);
820
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
821 822
    storageDriverUnlock(driver);

823
    if (!pool) {
824 825
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
826
        goto cleanup;
827 828
    }

829 830
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
831 832

    if (virStoragePoolObjIsActive(pool)) {
833 834
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is still active"));
835
        goto cleanup;
836 837
    }

838
    if (pool->asyncjobs > 0) {
839 840
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("pool '%s' has asynchronous jobs running."),
841 842 843 844
                              pool->def->name);
        goto cleanup;
    }

845
    if (!backend->deletePool) {
846 847
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("pool does not support pool deletion"));
848
        goto cleanup;
849 850
    }
    if (backend->deletePool(obj->conn, pool, flags) < 0)
851
        goto cleanup;
852
    VIR_INFO("Deleting storage pool '%s'", pool->def->name);
853
    ret = 0;
854

855
cleanup:
856 857
    if (pool)
        virStoragePoolObjUnlock(pool);
858
    return ret;
859 860 861 862 863
}


static int
storagePoolRefresh(virStoragePoolPtr obj,
E
Eric Blake 已提交
864 865
                   unsigned int flags)
{
866 867
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
868
    virStorageBackendPtr backend;
869
    int ret = -1;
870

E
Eric Blake 已提交
871 872
    virCheckFlags(0, -1);

873
    storageDriverLock(driver);
874
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
875

876
    if (!pool) {
877 878
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
879
        goto cleanup;
880 881
    }

882 883
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
884 885

    if (!virStoragePoolObjIsActive(pool)) {
886 887
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
888
        goto cleanup;
889 890
    }

891
    if (pool->asyncjobs > 0) {
892 893 894
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("pool '%s' has asynchronous jobs running."),
                       pool->def->name);
895 896 897
        goto cleanup;
    }

898
    virStoragePoolObjClearVols(pool);
899
    if (backend->refreshPool(obj->conn, pool) < 0) {
900 901 902 903 904
        if (backend->stopPool)
            backend->stopPool(obj->conn, pool);

        pool->active = 0;

905
        if (pool->configFile == NULL) {
906
            virStoragePoolObjRemove(&driver->pools, pool);
907 908
            pool = NULL;
        }
909
        goto cleanup;
910
    }
911
    ret = 0;
912

913
cleanup:
914 915
    if (pool)
        virStoragePoolObjUnlock(pool);
916
    storageDriverUnlock(driver);
917 918 919 920 921 922 923
    return ret;
}


static int
storagePoolGetInfo(virStoragePoolPtr obj,
                   virStoragePoolInfoPtr info) {
924 925 926
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
927

928
    storageDriverLock(driver);
929
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
930 931
    storageDriverUnlock(driver);

932
    if (!pool) {
933 934
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
935
        goto cleanup;
936 937
    }

938
    if (virStorageBackendForType(pool->def->type) == NULL)
939
        goto cleanup;
940 941 942 943 944 945 946 947 948

    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;
949
    ret = 0;
950

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

static char *
958
storagePoolGetXMLDesc(virStoragePoolPtr obj,
E
Eric Blake 已提交
959 960
                      unsigned int flags)
{
961 962
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
963
    virStoragePoolDefPtr def;
964
    char *ret = NULL;
965

966
    virCheckFlags(VIR_STORAGE_XML_INACTIVE, NULL);
E
Eric Blake 已提交
967

968
    storageDriverLock(driver);
969
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
970 971
    storageDriverUnlock(driver);

972
    if (!pool) {
973 974
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
975
        goto cleanup;
976 977
    }

978 979 980 981 982 983
    if ((flags & VIR_STORAGE_XML_INACTIVE) && pool->newDef)
        def = pool->newDef;
    else
        def = pool->def;

    ret = virStoragePoolDefFormat(def);
984 985

cleanup:
986 987
    if (pool)
        virStoragePoolObjUnlock(pool);
988
    return ret;
989 990 991 992 993
}

static int
storagePoolGetAutostart(virStoragePoolPtr obj,
                        int *autostart) {
994 995 996
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
997

998
    storageDriverLock(driver);
999
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1000 1001
    storageDriverUnlock(driver);

1002
    if (!pool) {
1003 1004
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no pool with matching uuid"));
1005
        goto cleanup;
1006 1007 1008 1009 1010 1011 1012
    }

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

1015
cleanup:
1016 1017
    if (pool)
        virStoragePoolObjUnlock(pool);
1018
    return ret;
1019 1020 1021 1022 1023
}

static int
storagePoolSetAutostart(virStoragePoolPtr obj,
                        int autostart) {
1024 1025 1026
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
1027

1028
    storageDriverLock(driver);
1029
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1030

1031
    if (!pool) {
1032 1033
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no pool with matching uuid"));
1034
        goto cleanup;
1035 1036 1037
    }

    if (!pool->configFile) {
1038 1039
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("pool has no config file"));
1040
        goto cleanup;
1041 1042 1043 1044
    }

    autostart = (autostart != 0);

1045 1046
    if (pool->autostart != autostart) {
        if (autostart) {
1047 1048
            if (virFileMakePath(driver->autostartDir) < 0) {
                virReportSystemError(errno,
1049 1050
                                     _("cannot create autostart directory %s"),
                                     driver->autostartDir);
1051 1052
                goto cleanup;
            }
1053

1054
            if (symlink(pool->configFile, pool->autostartLink) < 0) {
1055
                virReportSystemError(errno,
1056 1057
                                     _("Failed to create symlink '%s' to '%s'"),
                                     pool->autostartLink, pool->configFile);
1058 1059 1060 1061 1062
                goto cleanup;
            }
        } else {
            if (unlink(pool->autostartLink) < 0 &&
                errno != ENOENT && errno != ENOTDIR) {
1063
                virReportSystemError(errno,
1064 1065
                                     _("Failed to delete symlink '%s'"),
                                     pool->autostartLink);
1066 1067
                goto cleanup;
            }
1068
        }
1069
        pool->autostart = autostart;
1070
    }
1071
    ret = 0;
1072

1073
cleanup:
1074 1075
    if (pool)
        virStoragePoolObjUnlock(pool);
1076
    storageDriverUnlock(driver);
1077
    return ret;
1078 1079 1080 1081 1082
}


static int
storagePoolNumVolumes(virStoragePoolPtr obj) {
1083 1084 1085
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
1086

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

1091
    if (!pool) {
1092 1093
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1094
        goto cleanup;
1095 1096 1097
    }

    if (!virStoragePoolObjIsActive(pool)) {
1098 1099
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1100
        goto cleanup;
1101
    }
1102
    ret = pool->volumes.count;
1103

1104
cleanup:
1105 1106
    if (pool)
        virStoragePoolObjUnlock(pool);
1107
    return ret;
1108 1109 1110 1111 1112 1113
}

static int
storagePoolListVolumes(virStoragePoolPtr obj,
                       char **const names,
                       int maxnames) {
1114 1115
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1116
    int i, n = 0;
1117

1118 1119
    memset(names, 0, maxnames * sizeof(*names));

1120
    storageDriverLock(driver);
1121
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1122 1123
    storageDriverUnlock(driver);

1124
    if (!pool) {
1125 1126
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1127
        goto cleanup;
1128 1129 1130
    }

    if (!virStoragePoolObjIsActive(pool)) {
1131 1132
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1133
        goto cleanup;
1134 1135
    }

1136 1137
    for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) {
1138
            virReportOOMError();
1139 1140 1141 1142
            goto cleanup;
        }
    }

1143
    virStoragePoolObjUnlock(pool);
1144
    return n;
1145 1146

 cleanup:
1147 1148
    if (pool)
        virStoragePoolObjUnlock(pool);
1149
    for (n = 0 ; n < maxnames ; n++)
1150
        VIR_FREE(names[n]);
1151

1152
    memset(names, 0, maxnames * sizeof(*names));
1153 1154 1155 1156 1157 1158 1159
    return -1;
}


static virStorageVolPtr
storageVolumeLookupByName(virStoragePoolPtr obj,
                          const char *name) {
1160 1161
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1162
    virStorageVolDefPtr vol;
1163
    virStorageVolPtr ret = NULL;
1164

1165
    storageDriverLock(driver);
1166
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1167 1168
    storageDriverUnlock(driver);

1169
    if (!pool) {
1170 1171
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1172
        goto cleanup;
1173 1174 1175
    }

    if (!virStoragePoolObjIsActive(pool)) {
1176 1177
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1178
        goto cleanup;
1179 1180 1181 1182 1183
    }

    vol = virStorageVolDefFindByName(pool, name);

    if (!vol) {
1184 1185 1186
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       name);
1187
        goto cleanup;
1188 1189
    }

1190 1191 1192
    ret = virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key);

cleanup:
1193 1194
    if (pool)
        virStoragePoolObjUnlock(pool);
1195
    return ret;
1196 1197 1198 1199 1200 1201
}


static virStorageVolPtr
storageVolumeLookupByKey(virConnectPtr conn,
                         const char *key) {
1202
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1203
    unsigned int i;
1204
    virStorageVolPtr ret = NULL;
1205

1206 1207 1208
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1209 1210 1211
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
            virStorageVolDefPtr vol =
                virStorageVolDefFindByKey(driver->pools.objs[i], key);
1212

1213
            if (vol)
1214
                ret = virGetStorageVol(conn,
1215 1216 1217
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1218
        }
1219
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1220
    }
1221
    storageDriverUnlock(driver);
1222

1223
    if (!ret)
1224 1225
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       "%s", _("no storage vol with matching key"));
1226 1227

    return ret;
1228 1229 1230 1231 1232
}

static virStorageVolPtr
storageVolumeLookupByPath(virConnectPtr conn,
                          const char *path) {
1233
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1234
    unsigned int i;
1235
    virStorageVolPtr ret = NULL;
1236 1237 1238 1239 1240
    char *cleanpath;

    cleanpath = virFileSanitizePath(path);
    if (!cleanpath)
        return NULL;
1241

1242 1243 1244
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1245
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
1246
            virStorageVolDefPtr vol;
1247 1248
            const char *stable_path;

1249
            stable_path = virStorageBackendStablePath(driver->pools.objs[i],
1250
                                                      cleanpath);
1251
            if (stable_path == NULL) {
1252 1253 1254 1255 1256
                /* Don't break the whole lookup process if it fails on
                 * getting the stable path for some of the pools.
                 */
                VIR_WARN("Failed to get stable path for pool '%s'",
                         driver->pools.objs[i]->def->name);
1257
                virStoragePoolObjUnlock(driver->pools.objs[i]);
1258
                continue;
1259
            }
1260 1261 1262 1263

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

1265
            if (vol)
1266 1267 1268 1269
                ret = virGetStorageVol(conn,
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1270
        }
1271
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1272 1273
    }

1274
    if (!ret)
1275 1276
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       "%s", _("no storage vol with matching path"));
1277

1278
    VIR_FREE(cleanpath);
1279
    storageDriverUnlock(driver);
1280
    return ret;
1281 1282
}

1283 1284
static int storageVolumeDelete(virStorageVolPtr obj, unsigned int flags);

1285 1286 1287
static virStorageVolPtr
storageVolumeCreateXML(virStoragePoolPtr obj,
                       const char *xmldesc,
E
Eric Blake 已提交
1288 1289
                       unsigned int flags)
{
1290 1291
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1292
    virStorageBackendPtr backend;
1293 1294
    virStorageVolDefPtr voldef = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1295

E
Eric Blake 已提交
1296 1297
    virCheckFlags(0, NULL);

1298
    storageDriverLock(driver);
1299
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1300 1301
    storageDriverUnlock(driver);

1302
    if (!pool) {
1303 1304
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1305
        goto cleanup;
1306 1307 1308
    }

    if (!virStoragePoolObjIsActive(pool)) {
1309 1310
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1311
        goto cleanup;
1312 1313 1314
    }

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

1317
    voldef = virStorageVolDefParseString(pool->def, xmldesc);
1318
    if (voldef == NULL)
1319
        goto cleanup;
1320

1321
    if (virStorageVolDefFindByName(pool, voldef->name)) {
1322 1323
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       "%s", _("storage vol already exists"));
1324
        goto cleanup;
1325 1326
    }

1327 1328
    if (VIR_REALLOC_N(pool->volumes.objs,
                      pool->volumes.count+1) < 0) {
1329
        virReportOOMError();
1330
        goto cleanup;
1331 1332
    }

1333
    if (!backend->createVol) {
1334 1335 1336
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("storage pool does not support volume "
                               "creation"));
1337
        goto cleanup;
1338 1339
    }

1340
    if (backend->createVol(obj->conn, pool, voldef) < 0) {
1341
        goto cleanup;
1342 1343
    }

1344 1345 1346
    pool->volumes.objs[pool->volumes.count++] = voldef;
    volobj = virGetStorageVol(obj->conn, pool->def->name, voldef->name,
                              voldef->key);
1347 1348 1349 1350
    if (!volobj) {
        pool->volumes.count--;
        goto cleanup;
    }
1351

1352
    if (backend->buildVol) {
1353 1354 1355 1356
        int buildret;
        virStorageVolDefPtr buildvoldef = NULL;

        if (VIR_ALLOC(buildvoldef) < 0) {
1357
            virReportOOMError();
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
            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);

1373
        buildret = backend->buildVol(obj->conn, pool, buildvoldef);
1374

1375
        storageDriverLock(driver);
1376
        virStoragePoolObjLock(pool);
1377 1378
        storageDriverUnlock(driver);

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
        voldef->building = 0;
        pool->asyncjobs--;

        voldef = NULL;
        VIR_FREE(buildvoldef);

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

    }

1394
    VIR_INFO("Creating volume '%s' in storage pool '%s'",
1395
             volobj->name, pool->def->name);
1396 1397 1398
    ret = volobj;
    volobj = NULL;
    voldef = NULL;
1399

1400
cleanup:
1401
    virObjectUnref(volobj);
1402
    virStorageVolDefFree(voldef);
1403 1404
    if (pool)
        virStoragePoolObjUnlock(pool);
1405
    return ret;
1406 1407
}

1408 1409 1410 1411
static virStorageVolPtr
storageVolumeCreateXMLFrom(virStoragePoolPtr obj,
                           const char *xmldesc,
                           virStorageVolPtr vobj,
E
Eric Blake 已提交
1412 1413
                           unsigned int flags)
{
1414 1415 1416 1417 1418
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool, origpool = NULL;
    virStorageBackendPtr backend;
    virStorageVolDefPtr origvol = NULL, newvol = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1419
    int buildret;
1420

E
Eric Blake 已提交
1421 1422
    virCheckFlags(0, NULL);

1423 1424
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1425
    if (pool && STRNEQ(obj->name, vobj->pool)) {
1426
        virStoragePoolObjUnlock(pool);
1427
        origpool = virStoragePoolObjFindByName(&driver->pools, vobj->pool);
1428
        virStoragePoolObjLock(pool);
1429
    }
1430 1431
    storageDriverUnlock(driver);
    if (!pool) {
1432 1433
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1434 1435 1436
        goto cleanup;
    }

1437
    if (STRNEQ(obj->name, vobj->pool) && !origpool) {
1438 1439 1440
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching name '%s'"),
                       vobj->pool);
1441 1442 1443 1444
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1445 1446
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1447 1448 1449
        goto cleanup;
    }

1450
    if (origpool && !virStoragePoolObjIsActive(origpool)) {
1451 1452
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1453 1454 1455 1456 1457 1458
        goto cleanup;
    }

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

1459
    origvol = virStorageVolDefFindByName(origpool ? origpool : pool, vobj->name);
1460
    if (!origvol) {
1461 1462 1463
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vobj->name);
1464 1465 1466
        goto cleanup;
    }

1467
    newvol = virStorageVolDefParseString(pool->def, xmldesc);
1468 1469 1470 1471
    if (newvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(pool, newvol->name)) {
1472 1473 1474
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("storage volume name '%s' already in use."),
                       newvol->name);
1475 1476 1477 1478 1479 1480 1481
        goto cleanup;
    }

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

1482 1483 1484 1485 1486
    /* 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;

1487
    if (!backend->buildVolFrom) {
1488 1489
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("storage pool does not support volume creation from an existing volume"));
1490 1491 1492 1493
        goto cleanup;
    }

    if (origvol->building) {
1494 1495 1496
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       origvol->name);
1497 1498 1499 1500 1501 1502 1503 1504 1505
        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) {
1506
        virReportOOMError();
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
        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);

1525
    if (origpool) {
1526 1527 1528 1529
        origpool->asyncjobs++;
        virStoragePoolObjUnlock(origpool);
    }

1530
    buildret = backend->buildVolFrom(obj->conn, pool, newvol, origvol, flags);
1531 1532 1533

    storageDriverLock(driver);
    virStoragePoolObjLock(pool);
1534
    if (origpool)
1535 1536 1537 1538 1539 1540 1541 1542
        virStoragePoolObjLock(origpool);
    storageDriverUnlock(driver);

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

1543
    if (origpool) {
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
        origpool->asyncjobs--;
        virStoragePoolObjUnlock(origpool);
        origpool = NULL;
    }

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

1556
    VIR_INFO("Creating volume '%s' in storage pool '%s'",
1557
             volobj->name, pool->def->name);
1558 1559 1560 1561
    ret = volobj;
    volobj = NULL;

cleanup:
1562
    virObjectUnref(volobj);
1563 1564 1565
    virStorageVolDefFree(newvol);
    if (pool)
        virStoragePoolObjUnlock(pool);
1566
    if (origpool)
1567 1568 1569 1570
        virStoragePoolObjUnlock(origpool);
    return ret;
}

1571

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
static int
storageVolumeDownload(virStorageVolPtr obj,
                      virStreamPtr stream,
                      unsigned long long offset,
                      unsigned long long length,
                      unsigned int flags)
{
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool = NULL;
    virStorageVolDefPtr vol = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);

    if (!pool) {
1591 1592
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1593 1594 1595 1596
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1597 1598
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1599 1600 1601 1602 1603 1604
        goto out;
    }

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

    if (vol == NULL) {
1605 1606 1607
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
1608 1609 1610 1611
        goto out;
    }

    if (vol->building) {
1612 1613 1614
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
1615 1616 1617 1618 1619 1620
        goto out;
    }

    if (virFDStreamOpenFile(stream,
                            vol->target.path,
                            offset, length,
E
Eric Blake 已提交
1621
                            O_RDONLY) < 0)
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
        goto out;

    ret = 0;

out:
    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}


static int
storageVolumeUpload(virStorageVolPtr obj,
                    virStreamPtr stream,
                    unsigned long long offset,
                    unsigned long long length,
                    unsigned int flags)
{
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool = NULL;
    virStorageVolDefPtr vol = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);

    if (!pool) {
1653 1654
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1655 1656 1657 1658
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1659 1660
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1661 1662 1663 1664 1665 1666
        goto out;
    }

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

    if (vol == NULL) {
1667 1668 1669
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
1670 1671 1672 1673
        goto out;
    }

    if (vol->building) {
1674 1675 1676
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
1677 1678 1679 1680 1681 1682 1683 1684
        goto out;
    }

    /* Not using O_CREAT because the file is required to
     * already exist at this point */
    if (virFDStreamOpenFile(stream,
                            vol->target.path,
                            offset, length,
E
Eric Blake 已提交
1685
                            O_WRONLY) < 0)
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
        goto out;

    ret = 0;

out:
    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}

1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
static int
storageVolumeResize(virStorageVolPtr obj,
                    unsigned long long capacity,
                    unsigned int flags)
{
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStorageBackendPtr backend;
    virStoragePoolObjPtr pool = NULL;
    virStorageVolDefPtr vol = NULL;
    unsigned long long abs_capacity;
    int ret = -1;

    virCheckFlags(VIR_STORAGE_VOL_RESIZE_DELTA, -1);

    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);

    if (!pool) {
1716
        virReportError(VIR_ERR_NO_STORAGE_POOL, "%s",
1717
                       _("no storage pool with matching uuid"));
1718 1719 1720 1721
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1722
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
1723
                       _("storage pool is not active"));
1724 1725 1726 1727 1728 1729 1730 1731 1732
        goto out;
    }

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

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

    if (vol == NULL) {
1733 1734 1735
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
1736 1737 1738 1739
        goto out;
    }

    if (vol->building) {
1740 1741 1742
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
1743 1744
        goto out;
    }
1745

1746 1747 1748 1749 1750 1751 1752 1753
    if (flags & VIR_STORAGE_VOL_RESIZE_DELTA) {
        abs_capacity = vol->capacity + capacity;
        flags &= ~VIR_STORAGE_VOL_RESIZE_DELTA;
    } else {
        abs_capacity = capacity;
    }

    if (abs_capacity < vol->allocation) {
1754
        virReportError(VIR_ERR_INVALID_ARG, "%s",
1755 1756
                       _("can't shrink capacity below "
                         "existing allocation"));
1757 1758 1759
        goto out;
    }

1760
    if (abs_capacity > vol->capacity + pool->def->available) {
1761
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
1762
                       _("Not enough space left on storage pool"));
1763 1764 1765 1766
        goto out;
    }

    if (!backend->resizeVol) {
1767
        virReportError(VIR_ERR_NO_SUPPORT, "%s",
1768 1769
                       _("storage pool does not support changing of "
                         "volume capacity"));
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
        goto out;
    }

    if (backend->resizeVol(obj->conn, pool, vol, abs_capacity, flags) < 0)
        goto out;

   vol->capacity = abs_capacity;
   ret = 0;

out:
    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
1785

1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
/* If the volume we're wiping is already a sparse file, we simply
 * truncate and extend it to its original size, filling it with
 * zeroes.  This behavior is guaranteed by POSIX:
 *
 * http://www.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
 *
 * If fildes refers to a regular file, the ftruncate() function shall
 * cause the size of the file to be truncated to length. If the size
 * of the file previously exceeded length, the extra data shall no
 * longer be available to reads on the file. If the file previously
 * was smaller than this size, ftruncate() shall increase the size of
 * the file. If the file size is increased, the extended area shall
 * appear as if it were zero-filled.
 */
static int
storageVolumeZeroSparseFile(virStorageVolDefPtr vol,
                            off_t size,
                            int fd)
{
    int ret = -1;

    ret = ftruncate(fd, 0);
    if (ret == -1) {
        virReportSystemError(errno,
                             _("Failed to truncate volume with "
                               "path '%s' to 0 bytes"),
                             vol->target.path);
        goto out;
    }

    ret = ftruncate(fd, size);
    if (ret == -1) {
        virReportSystemError(errno,
                             _("Failed to truncate volume with "
1820
                               "path '%s' to %ju bytes"),
E
Eric Blake 已提交
1821
                             vol->target.path, (uintmax_t)size);
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
    }

out:
    return ret;
}


static int
storageWipeExtent(virStorageVolDefPtr vol,
                  int fd,
                  off_t extent_start,
                  off_t extent_length,
                  char *writebuf,
                  size_t writebuf_length,
                  size_t *bytes_wiped)
{
    int ret = -1, written = 0;
    off_t remaining = 0;
    size_t write_size = 0;

    VIR_DEBUG("extent logical start: %ju len: %ju",
E
Eric Blake 已提交
1843
              (uintmax_t)extent_start, (uintmax_t)extent_length);
1844 1845 1846 1847 1848

    if ((ret = lseek(fd, extent_start, SEEK_SET)) < 0) {
        virReportSystemError(errno,
                             _("Failed to seek to position %ju in volume "
                               "with path '%s'"),
E
Eric Blake 已提交
1849
                             (uintmax_t)extent_start, vol->target.path);
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
        goto out;
    }

    remaining = extent_length;
    while (remaining > 0) {

        write_size = (writebuf_length < remaining) ? writebuf_length : remaining;
        written = safewrite(fd, writebuf, write_size);
        if (written < 0) {
            virReportSystemError(errno,
                                 _("Failed to write %zu bytes to "
                                   "storage volume with path '%s'"),
                                 write_size, vol->target.path);

            goto out;
        }

        *bytes_wiped += written;
        remaining -= written;
    }

1871 1872 1873 1874 1875 1876 1877 1878
    if (fdatasync(fd) < 0) {
        ret = -errno;
        virReportSystemError(errno,
                             _("cannot sync data to volume with path '%s'"),
                             vol->target.path);
        goto out;
    }

1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
    VIR_DEBUG("Wrote %zu bytes to volume with path '%s'",
              *bytes_wiped, vol->target.path);

    ret = 0;

out:
    return ret;
}


static int
1890 1891
storageVolumeWipeInternal(virStorageVolDefPtr def,
                          unsigned int algorithm)
1892 1893 1894 1895 1896
{
    int ret = -1, fd = -1;
    struct stat st;
    char *writebuf = NULL;
    size_t bytes_wiped = 0;
1897
    virCommandPtr cmd = NULL;
1898

1899 1900
    VIR_DEBUG("Wiping volume with path '%s' and algorithm %u",
              def->target.path, algorithm);
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916

    fd = open(def->target.path, O_RDWR);
    if (fd == -1) {
        virReportSystemError(errno,
                             _("Failed to open storage volume with path '%s'"),
                             def->target.path);
        goto out;
    }

    if (fstat(fd, &st) == -1) {
        virReportSystemError(errno,
                             _("Failed to stat storage volume with path '%s'"),
                             def->target.path);
        goto out;
    }

1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
    if (algorithm != VIR_STORAGE_VOL_WIPE_ALG_ZERO) {
        const char *alg_char ATTRIBUTE_UNUSED = NULL;
        switch (algorithm) {
        case VIR_STORAGE_VOL_WIPE_ALG_NNSA:
            alg_char = "nnsa";
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_DOD:
            alg_char = "dod";
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_BSI:
            alg_char = "bsi";
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_GUTMANN:
            alg_char = "gutmann";
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_SCHNEIER:
A
Alex Jia 已提交
1933
            alg_char = "schneier";
1934 1935 1936 1937 1938
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_PFITZNER7:
            alg_char = "pfitzner7";
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_PFITZNER33:
M
Michal Privoznik 已提交
1939
            alg_char = "pfitzner33";
1940 1941 1942 1943 1944
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_RANDOM:
            alg_char = "random";
            break;
        default:
1945 1946 1947
            virReportError(VIR_ERR_INVALID_ARG,
                           _("unsupported algorithm %d"),
                           algorithm);
1948 1949 1950 1951
        }
        cmd = virCommandNew(SCRUB);
        virCommandAddArgList(cmd, "-f", "-p", alg_char,
                             def->target.path, NULL);
1952

1953
        if (virCommandRun(cmd, NULL) < 0)
1954 1955
            goto out;

1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
        ret = 0;
        goto out;
    } else {
        if (S_ISREG(st.st_mode) && st.st_blocks < (st.st_size / DEV_BSIZE)) {
            ret = storageVolumeZeroSparseFile(def, st.st_size, fd);
        } else {

            if (VIR_ALLOC_N(writebuf, st.st_blksize) != 0) {
                virReportOOMError();
                goto out;
            }

            ret = storageWipeExtent(def,
                                    fd,
                                    0,
                                    def->allocation,
                                    writebuf,
                                    st.st_blksize,
                                    &bytes_wiped);
        }
1976 1977 1978
    }

out:
1979
    virCommandFree(cmd);
1980
    VIR_FREE(writebuf);
1981
    VIR_FORCE_CLOSE(fd);
1982 1983 1984 1985 1986
    return ret;
}


static int
1987 1988 1989
storageVolumeWipePattern(virStorageVolPtr obj,
                         unsigned int algorithm,
                         unsigned int flags)
1990 1991 1992 1993 1994 1995
{
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool = NULL;
    virStorageVolDefPtr vol = NULL;
    int ret = -1;

1996
    virCheckFlags(0, -1);
1997

1998
    if (algorithm >= VIR_STORAGE_VOL_WIPE_ALG_LAST) {
1999 2000 2001
        virReportError(VIR_ERR_INVALID_ARG,
                       _("wiping algorithm %d not supported"),
                       algorithm);
2002 2003 2004
        return -1;
    }

2005 2006 2007 2008 2009
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);

    if (!pool) {
2010 2011
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2012 2013 2014 2015
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
2016 2017
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2018 2019 2020 2021 2022 2023
        goto out;
    }

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

    if (vol == NULL) {
2024 2025 2026
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2027 2028 2029 2030
        goto out;
    }

    if (vol->building) {
2031 2032 2033
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
2034 2035 2036
        goto out;
    }

2037
    if (storageVolumeWipeInternal(vol, algorithm) == -1) {
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
        goto out;
    }

    ret = 0;

out:
    if (pool) {
        virStoragePoolObjUnlock(pool);
    }

    return ret;

}

2052 2053 2054 2055 2056 2057 2058
static int
storageVolumeWipe(virStorageVolPtr obj,
                  unsigned int flags)
{
    return storageVolumeWipePattern(obj, VIR_STORAGE_VOL_WIPE_ALG_ZERO, flags);
}

2059 2060 2061
static int
storageVolumeDelete(virStorageVolPtr obj,
                    unsigned int flags) {
2062 2063
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2064
    virStorageBackendPtr backend;
2065
    virStorageVolDefPtr vol = NULL;
2066
    unsigned int i;
2067
    int ret = -1;
2068

2069
    storageDriverLock(driver);
2070
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2071 2072
    storageDriverUnlock(driver);

2073
    if (!pool) {
2074 2075
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2076
        goto cleanup;
2077 2078 2079
    }

    if (!virStoragePoolObjIsActive(pool)) {
2080 2081
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2082
        goto cleanup;
2083 2084 2085
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2086
        goto cleanup;
2087 2088 2089 2090

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

    if (!vol) {
2091 2092 2093
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2094
        goto cleanup;
2095 2096
    }

2097
    if (vol->building) {
2098 2099 2100
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
2101 2102 2103
        goto cleanup;
    }

2104
    if (!backend->deleteVol) {
2105 2106
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("storage pool does not support vol deletion"));
2107

2108
        goto cleanup;
2109 2110
    }

2111 2112 2113
    if (backend->deleteVol(obj->conn, pool, vol, flags) < 0)
        goto cleanup;

2114 2115
    for (i = 0 ; i < pool->volumes.count ; i++) {
        if (pool->volumes.objs[i] == vol) {
2116
            VIR_INFO("Deleting volume '%s' from storage pool '%s'",
2117
                     vol->name, pool->def->name);
2118
            virStorageVolDefFree(vol);
2119
            vol = NULL;
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129

            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--;

2130 2131 2132
            break;
        }
    }
2133
    ret = 0;
2134

2135
cleanup:
2136 2137
    if (pool)
        virStoragePoolObjUnlock(pool);
2138
    return ret;
2139 2140 2141 2142 2143
}

static int
storageVolumeGetInfo(virStorageVolPtr obj,
                     virStorageVolInfoPtr info) {
2144 2145
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2146 2147
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
2148
    int ret = -1;
2149

2150
    storageDriverLock(driver);
2151
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2152 2153
    storageDriverUnlock(driver);

2154
    if (!pool) {
2155 2156
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2157
        goto cleanup;
2158 2159 2160
    }

    if (!virStoragePoolObjIsActive(pool)) {
2161 2162
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2163
        goto cleanup;
2164 2165 2166 2167 2168
    }

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

    if (!vol) {
2169 2170 2171
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2172
        goto cleanup;
2173 2174 2175
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2176
        goto cleanup;
2177 2178 2179

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

    memset(info, 0, sizeof(*info));
2183
    info->type = vol->type;
2184 2185
    info->capacity = vol->capacity;
    info->allocation = vol->allocation;
2186
    ret = 0;
2187

2188
cleanup:
2189 2190
    if (pool)
        virStoragePoolObjUnlock(pool);
2191
    return ret;
2192 2193 2194 2195
}

static char *
storageVolumeGetXMLDesc(virStorageVolPtr obj,
E
Eric Blake 已提交
2196 2197
                        unsigned int flags)
{
2198 2199
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2200 2201
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
2202
    char *ret = NULL;
2203

E
Eric Blake 已提交
2204 2205
    virCheckFlags(0, NULL);

2206
    storageDriverLock(driver);
2207
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2208 2209
    storageDriverUnlock(driver);

2210
    if (!pool) {
2211 2212
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2213
        goto cleanup;
2214 2215 2216
    }

    if (!virStoragePoolObjIsActive(pool)) {
2217 2218
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2219
        goto cleanup;
2220 2221 2222 2223 2224
    }

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

    if (!vol) {
2225 2226 2227
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2228
        goto cleanup;
2229 2230 2231
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2232
        goto cleanup;
2233 2234 2235 2236

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

2238
    ret = virStorageVolDefFormat(pool->def, vol);
2239 2240

cleanup:
2241 2242 2243
    if (pool)
        virStoragePoolObjUnlock(pool);

2244
    return ret;
2245 2246 2247 2248
}

static char *
storageVolumeGetPath(virStorageVolPtr obj) {
2249
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
2250
    virStoragePoolObjPtr pool;
2251
    virStorageVolDefPtr vol;
2252
    char *ret = NULL;
2253

2254 2255 2256
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);
2257
    if (!pool) {
2258 2259
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2260
        goto cleanup;
2261 2262 2263
    }

    if (!virStoragePoolObjIsActive(pool)) {
2264 2265
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2266
        goto cleanup;
2267 2268 2269 2270 2271
    }

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

    if (!vol) {
2272 2273 2274
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2275
        goto cleanup;
2276 2277 2278
    }

    ret = strdup(vol->target.path);
2279
    if (ret == NULL)
2280
        virReportOOMError();
2281 2282

cleanup:
2283 2284
    if (pool)
        virStoragePoolObjUnlock(pool);
2285 2286 2287
    return ret;
}

2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
static int
storageListAllPools(virConnectPtr conn,
                    virStoragePoolPtr **pools,
                    unsigned int flags)
{
    virStorageDriverStatePtr driver = conn->storagePrivateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    storageDriverLock(driver);
    ret = virStoragePoolList(conn, driver->pools, pools, flags);
    storageDriverUnlock(driver);

    return ret;
}

2305
static virStorageDriver storageDriver = {
2306
    .name = "storage",
2307 2308 2309 2310 2311 2312
    .open = storageOpen, /* 0.4.0 */
    .close = storageClose, /* 0.4.0 */
    .numOfPools = storageNumPools, /* 0.4.0 */
    .listPools = storageListPools, /* 0.4.0 */
    .numOfDefinedPools = storageNumDefinedPools, /* 0.4.0 */
    .listDefinedPools = storageListDefinedPools, /* 0.4.0 */
2313
    .listAllPools = storageListAllPools, /* 0.10.2 */
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
    .findPoolSources = storageFindPoolSources, /* 0.4.0 */
    .poolLookupByName = storagePoolLookupByName, /* 0.4.0 */
    .poolLookupByUUID = storagePoolLookupByUUID, /* 0.4.0 */
    .poolLookupByVolume = storagePoolLookupByVolume, /* 0.4.0 */
    .poolCreateXML = storagePoolCreate, /* 0.4.0 */
    .poolDefineXML = storagePoolDefine, /* 0.4.0 */
    .poolBuild = storagePoolBuild, /* 0.4.0 */
    .poolUndefine = storagePoolUndefine, /* 0.4.0 */
    .poolCreate = storagePoolStart, /* 0.4.0 */
    .poolDestroy = storagePoolDestroy, /* 0.4.0 */
    .poolDelete = storagePoolDelete, /* 0.4.0 */
    .poolRefresh = storagePoolRefresh, /* 0.4.0 */
    .poolGetInfo = storagePoolGetInfo, /* 0.4.0 */
    .poolGetXMLDesc = storagePoolGetXMLDesc, /* 0.4.0 */
    .poolGetAutostart = storagePoolGetAutostart, /* 0.4.0 */
    .poolSetAutostart = storagePoolSetAutostart, /* 0.4.0 */
    .poolNumOfVolumes = storagePoolNumVolumes, /* 0.4.0 */
    .poolListVolumes = storagePoolListVolumes, /* 0.4.0 */

    .volLookupByName = storageVolumeLookupByName, /* 0.4.0 */
    .volLookupByKey = storageVolumeLookupByKey, /* 0.4.0 */
    .volLookupByPath = storageVolumeLookupByPath, /* 0.4.0 */
    .volCreateXML = storageVolumeCreateXML, /* 0.4.0 */
    .volCreateXMLFrom = storageVolumeCreateXMLFrom, /* 0.6.4 */
    .volDownload = storageVolumeDownload, /* 0.9.0 */
    .volUpload = storageVolumeUpload, /* 0.9.0 */
    .volDelete = storageVolumeDelete, /* 0.4.0 */
    .volWipe = storageVolumeWipe, /* 0.8.0 */
2342
    .volWipePattern = storageVolumeWipePattern, /* 0.9.10 */
2343 2344 2345
    .volGetInfo = storageVolumeGetInfo, /* 0.4.0 */
    .volGetXMLDesc = storageVolumeGetXMLDesc, /* 0.4.0 */
    .volGetPath = storageVolumeGetPath, /* 0.4.0 */
2346
    .volResize = storageVolumeResize, /* 0.9.10 */
2347 2348 2349

    .poolIsActive = storagePoolIsActive, /* 0.7.3 */
    .poolIsPersistent = storagePoolIsPersistent, /* 0.7.3 */
2350 2351 2352 2353
};


static virStateDriver stateDriver = {
2354
    .name = "Storage",
2355 2356 2357 2358
    .initialize = storageDriverStartup,
    .cleanup = storageDriverShutdown,
    .reload = storageDriverReload,
    .active = storageDriverActive,
2359 2360 2361 2362 2363 2364 2365
};

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