storage_driver.c 67.6 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
    return -1;
}

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
static int
storagePoolListAllVolumes(virStoragePoolPtr pool,
                          virStorageVolPtr **vols,
                          unsigned int flags) {
    virStorageDriverStatePtr driver = pool->conn->storagePrivateData;
    virStoragePoolObjPtr obj;
    int i;
    virStorageVolPtr *tmp_vols = NULL;
    virStorageVolPtr vol = NULL;
    int nvols = 0;
    int ret = -1;

    virCheckFlags(0, -1);

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

    if (!obj) {
        virReportError(VIR_ERR_NO_STORAGE_POOL, "%s",
                       _("no storage pool with matching uuid"));
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(obj)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("storage pool is not active"));
        goto cleanup;
    }

     /* Just returns the volumes count */
    if (!vols) {
        ret = obj->volumes.count;
        goto cleanup;
    }

    if (VIR_ALLOC_N(tmp_vols, obj->volumes.count + 1) < 0) {
         virReportOOMError();
         goto cleanup;
    }

    for (i = 0 ; i < obj->volumes.count; i++) {
        if (!(vol = virGetStorageVol(pool->conn, obj->def->name,
                                     obj->volumes.objs[i]->name,
                                     obj->volumes.objs[i]->key)))
            goto cleanup;
        tmp_vols[nvols++] = vol;
    }

    *vols = tmp_vols;
    tmp_vols = NULL;
    ret = nvols;

 cleanup:
    if (tmp_vols) {
        for (i = 0; i < nvols; i++) {
            if (tmp_vols[i])
                virStorageVolFree(tmp_vols[i]);
        }
    }

    if (obj)
        virStoragePoolObjUnlock(obj);

    return ret;
}
1222 1223 1224 1225

static virStorageVolPtr
storageVolumeLookupByName(virStoragePoolPtr obj,
                          const char *name) {
1226 1227
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1228
    virStorageVolDefPtr vol;
1229
    virStorageVolPtr ret = NULL;
1230

1231
    storageDriverLock(driver);
1232
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1233 1234
    storageDriverUnlock(driver);

1235
    if (!pool) {
1236 1237
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1238
        goto cleanup;
1239 1240 1241
    }

    if (!virStoragePoolObjIsActive(pool)) {
1242 1243
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1244
        goto cleanup;
1245 1246 1247 1248 1249
    }

    vol = virStorageVolDefFindByName(pool, name);

    if (!vol) {
1250 1251 1252
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       name);
1253
        goto cleanup;
1254 1255
    }

1256 1257 1258
    ret = virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key);

cleanup:
1259 1260
    if (pool)
        virStoragePoolObjUnlock(pool);
1261
    return ret;
1262 1263 1264 1265 1266 1267
}


static virStorageVolPtr
storageVolumeLookupByKey(virConnectPtr conn,
                         const char *key) {
1268
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1269
    unsigned int i;
1270
    virStorageVolPtr ret = NULL;
1271

1272 1273 1274
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1275 1276 1277
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
            virStorageVolDefPtr vol =
                virStorageVolDefFindByKey(driver->pools.objs[i], key);
1278

1279
            if (vol)
1280
                ret = virGetStorageVol(conn,
1281 1282 1283
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1284
        }
1285
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1286
    }
1287
    storageDriverUnlock(driver);
1288

1289
    if (!ret)
1290 1291
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       "%s", _("no storage vol with matching key"));
1292 1293

    return ret;
1294 1295 1296 1297 1298
}

static virStorageVolPtr
storageVolumeLookupByPath(virConnectPtr conn,
                          const char *path) {
1299
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1300
    unsigned int i;
1301
    virStorageVolPtr ret = NULL;
1302 1303 1304 1305 1306
    char *cleanpath;

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

1308 1309 1310
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1311
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
1312
            virStorageVolDefPtr vol;
1313 1314
            const char *stable_path;

1315
            stable_path = virStorageBackendStablePath(driver->pools.objs[i],
1316
                                                      cleanpath);
1317
            if (stable_path == NULL) {
1318 1319 1320 1321 1322
                /* 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);
1323
                virStoragePoolObjUnlock(driver->pools.objs[i]);
1324
                continue;
1325
            }
1326 1327 1328 1329

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

1331
            if (vol)
1332 1333 1334 1335
                ret = virGetStorageVol(conn,
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1336
        }
1337
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1338 1339
    }

1340
    if (!ret)
1341 1342
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       "%s", _("no storage vol with matching path"));
1343

1344
    VIR_FREE(cleanpath);
1345
    storageDriverUnlock(driver);
1346
    return ret;
1347 1348
}

1349 1350
static int storageVolumeDelete(virStorageVolPtr obj, unsigned int flags);

1351 1352 1353
static virStorageVolPtr
storageVolumeCreateXML(virStoragePoolPtr obj,
                       const char *xmldesc,
E
Eric Blake 已提交
1354 1355
                       unsigned int flags)
{
1356 1357
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1358
    virStorageBackendPtr backend;
1359 1360
    virStorageVolDefPtr voldef = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1361

E
Eric Blake 已提交
1362 1363
    virCheckFlags(0, NULL);

1364
    storageDriverLock(driver);
1365
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1366 1367
    storageDriverUnlock(driver);

1368
    if (!pool) {
1369 1370
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1371
        goto cleanup;
1372 1373 1374
    }

    if (!virStoragePoolObjIsActive(pool)) {
1375 1376
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1377
        goto cleanup;
1378 1379 1380
    }

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

1383
    voldef = virStorageVolDefParseString(pool->def, xmldesc);
1384
    if (voldef == NULL)
1385
        goto cleanup;
1386

1387
    if (virStorageVolDefFindByName(pool, voldef->name)) {
1388 1389
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       "%s", _("storage vol already exists"));
1390
        goto cleanup;
1391 1392
    }

1393 1394
    if (VIR_REALLOC_N(pool->volumes.objs,
                      pool->volumes.count+1) < 0) {
1395
        virReportOOMError();
1396
        goto cleanup;
1397 1398
    }

1399
    if (!backend->createVol) {
1400 1401 1402
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("storage pool does not support volume "
                               "creation"));
1403
        goto cleanup;
1404 1405
    }

1406
    if (backend->createVol(obj->conn, pool, voldef) < 0) {
1407
        goto cleanup;
1408 1409
    }

1410 1411 1412
    pool->volumes.objs[pool->volumes.count++] = voldef;
    volobj = virGetStorageVol(obj->conn, pool->def->name, voldef->name,
                              voldef->key);
1413 1414 1415 1416
    if (!volobj) {
        pool->volumes.count--;
        goto cleanup;
    }
1417

1418
    if (backend->buildVol) {
1419 1420 1421 1422
        int buildret;
        virStorageVolDefPtr buildvoldef = NULL;

        if (VIR_ALLOC(buildvoldef) < 0) {
1423
            virReportOOMError();
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
            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);

1439
        buildret = backend->buildVol(obj->conn, pool, buildvoldef);
1440

1441
        storageDriverLock(driver);
1442
        virStoragePoolObjLock(pool);
1443 1444
        storageDriverUnlock(driver);

1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
        voldef->building = 0;
        pool->asyncjobs--;

        voldef = NULL;
        VIR_FREE(buildvoldef);

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

    }

1460
    VIR_INFO("Creating volume '%s' in storage pool '%s'",
1461
             volobj->name, pool->def->name);
1462 1463 1464
    ret = volobj;
    volobj = NULL;
    voldef = NULL;
1465

1466
cleanup:
1467
    virObjectUnref(volobj);
1468
    virStorageVolDefFree(voldef);
1469 1470
    if (pool)
        virStoragePoolObjUnlock(pool);
1471
    return ret;
1472 1473
}

1474 1475 1476 1477
static virStorageVolPtr
storageVolumeCreateXMLFrom(virStoragePoolPtr obj,
                           const char *xmldesc,
                           virStorageVolPtr vobj,
E
Eric Blake 已提交
1478 1479
                           unsigned int flags)
{
1480 1481 1482 1483 1484
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool, origpool = NULL;
    virStorageBackendPtr backend;
    virStorageVolDefPtr origvol = NULL, newvol = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1485
    int buildret;
1486

E
Eric Blake 已提交
1487 1488
    virCheckFlags(0, NULL);

1489 1490
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1491
    if (pool && STRNEQ(obj->name, vobj->pool)) {
1492
        virStoragePoolObjUnlock(pool);
1493
        origpool = virStoragePoolObjFindByName(&driver->pools, vobj->pool);
1494
        virStoragePoolObjLock(pool);
1495
    }
1496 1497
    storageDriverUnlock(driver);
    if (!pool) {
1498 1499
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1500 1501 1502
        goto cleanup;
    }

1503
    if (STRNEQ(obj->name, vobj->pool) && !origpool) {
1504 1505 1506
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching name '%s'"),
                       vobj->pool);
1507 1508 1509 1510
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1511 1512
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1513 1514 1515
        goto cleanup;
    }

1516
    if (origpool && !virStoragePoolObjIsActive(origpool)) {
1517 1518
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1519 1520 1521 1522 1523 1524
        goto cleanup;
    }

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

1525
    origvol = virStorageVolDefFindByName(origpool ? origpool : pool, vobj->name);
1526
    if (!origvol) {
1527 1528 1529
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vobj->name);
1530 1531 1532
        goto cleanup;
    }

1533
    newvol = virStorageVolDefParseString(pool->def, xmldesc);
1534 1535 1536 1537
    if (newvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(pool, newvol->name)) {
1538 1539 1540
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("storage volume name '%s' already in use."),
                       newvol->name);
1541 1542 1543 1544 1545 1546 1547
        goto cleanup;
    }

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

1548 1549 1550 1551 1552
    /* 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;

1553
    if (!backend->buildVolFrom) {
1554 1555
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("storage pool does not support volume creation from an existing volume"));
1556 1557 1558 1559
        goto cleanup;
    }

    if (origvol->building) {
1560 1561 1562
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       origvol->name);
1563 1564 1565 1566 1567 1568 1569 1570 1571
        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) {
1572
        virReportOOMError();
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
        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);

1591
    if (origpool) {
1592 1593 1594 1595
        origpool->asyncjobs++;
        virStoragePoolObjUnlock(origpool);
    }

1596
    buildret = backend->buildVolFrom(obj->conn, pool, newvol, origvol, flags);
1597 1598 1599

    storageDriverLock(driver);
    virStoragePoolObjLock(pool);
1600
    if (origpool)
1601 1602 1603 1604 1605 1606 1607 1608
        virStoragePoolObjLock(origpool);
    storageDriverUnlock(driver);

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

1609
    if (origpool) {
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
        origpool->asyncjobs--;
        virStoragePoolObjUnlock(origpool);
        origpool = NULL;
    }

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

1622
    VIR_INFO("Creating volume '%s' in storage pool '%s'",
1623
             volobj->name, pool->def->name);
1624 1625 1626 1627
    ret = volobj;
    volobj = NULL;

cleanup:
1628
    virObjectUnref(volobj);
1629 1630 1631
    virStorageVolDefFree(newvol);
    if (pool)
        virStoragePoolObjUnlock(pool);
1632
    if (origpool)
1633 1634 1635 1636
        virStoragePoolObjUnlock(origpool);
    return ret;
}

1637

1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
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) {
1657 1658
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1659 1660 1661 1662
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1663 1664
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1665 1666 1667 1668 1669 1670
        goto out;
    }

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

    if (vol == NULL) {
1671 1672 1673
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
1674 1675 1676 1677
        goto out;
    }

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

    if (virFDStreamOpenFile(stream,
                            vol->target.path,
                            offset, length,
E
Eric Blake 已提交
1687
                            O_RDONLY) < 0)
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
        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) {
1719 1720
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
1721 1722 1723 1724
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1725 1726
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
1727 1728 1729 1730 1731 1732
        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 1745 1746 1747 1748 1749 1750
        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 已提交
1751
                            O_WRONLY) < 0)
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
        goto out;

    ret = 0;

out:
    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}

1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
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) {
1782
        virReportError(VIR_ERR_NO_STORAGE_POOL, "%s",
1783
                       _("no storage pool with matching uuid"));
1784 1785 1786 1787
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1788
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
1789
                       _("storage pool is not active"));
1790 1791 1792 1793 1794 1795 1796 1797 1798
        goto out;
    }

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

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

    if (vol == NULL) {
1799 1800 1801
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
1802 1803 1804 1805
        goto out;
    }

    if (vol->building) {
1806 1807 1808
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
1809 1810
        goto out;
    }
1811

1812 1813 1814 1815 1816 1817 1818 1819
    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) {
1820
        virReportError(VIR_ERR_INVALID_ARG, "%s",
1821 1822
                       _("can't shrink capacity below "
                         "existing allocation"));
1823 1824 1825
        goto out;
    }

1826
    if (abs_capacity > vol->capacity + pool->def->available) {
1827
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
1828
                       _("Not enough space left on storage pool"));
1829 1830 1831 1832
        goto out;
    }

    if (!backend->resizeVol) {
1833
        virReportError(VIR_ERR_NO_SUPPORT, "%s",
1834 1835
                       _("storage pool does not support changing of "
                         "volume capacity"));
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
        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;
}
1851

1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
/* 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 "
1886
                               "path '%s' to %ju bytes"),
E
Eric Blake 已提交
1887
                             vol->target.path, (uintmax_t)size);
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
    }

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 已提交
1909
              (uintmax_t)extent_start, (uintmax_t)extent_length);
1910 1911 1912 1913 1914

    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 已提交
1915
                             (uintmax_t)extent_start, vol->target.path);
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
        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;
    }

1937 1938 1939 1940 1941 1942 1943 1944
    if (fdatasync(fd) < 0) {
        ret = -errno;
        virReportSystemError(errno,
                             _("cannot sync data to volume with path '%s'"),
                             vol->target.path);
        goto out;
    }

1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
    VIR_DEBUG("Wrote %zu bytes to volume with path '%s'",
              *bytes_wiped, vol->target.path);

    ret = 0;

out:
    return ret;
}


static int
1956 1957
storageVolumeWipeInternal(virStorageVolDefPtr def,
                          unsigned int algorithm)
1958 1959 1960 1961 1962
{
    int ret = -1, fd = -1;
    struct stat st;
    char *writebuf = NULL;
    size_t bytes_wiped = 0;
1963
    virCommandPtr cmd = NULL;
1964

1965 1966
    VIR_DEBUG("Wiping volume with path '%s' and algorithm %u",
              def->target.path, algorithm);
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982

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

1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
    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 已提交
1999
            alg_char = "schneier";
2000 2001 2002 2003 2004
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_PFITZNER7:
            alg_char = "pfitzner7";
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_PFITZNER33:
M
Michal Privoznik 已提交
2005
            alg_char = "pfitzner33";
2006 2007 2008 2009 2010
            break;
        case VIR_STORAGE_VOL_WIPE_ALG_RANDOM:
            alg_char = "random";
            break;
        default:
2011 2012 2013
            virReportError(VIR_ERR_INVALID_ARG,
                           _("unsupported algorithm %d"),
                           algorithm);
2014 2015 2016 2017
        }
        cmd = virCommandNew(SCRUB);
        virCommandAddArgList(cmd, "-f", "-p", alg_char,
                             def->target.path, NULL);
2018

2019
        if (virCommandRun(cmd, NULL) < 0)
2020 2021
            goto out;

2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
        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);
        }
2042 2043 2044
    }

out:
2045
    virCommandFree(cmd);
2046
    VIR_FREE(writebuf);
2047
    VIR_FORCE_CLOSE(fd);
2048 2049 2050 2051 2052
    return ret;
}


static int
2053 2054 2055
storageVolumeWipePattern(virStorageVolPtr obj,
                         unsigned int algorithm,
                         unsigned int flags)
2056 2057 2058 2059 2060 2061
{
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool = NULL;
    virStorageVolDefPtr vol = NULL;
    int ret = -1;

2062
    virCheckFlags(0, -1);
2063

2064
    if (algorithm >= VIR_STORAGE_VOL_WIPE_ALG_LAST) {
2065 2066 2067
        virReportError(VIR_ERR_INVALID_ARG,
                       _("wiping algorithm %d not supported"),
                       algorithm);
2068 2069 2070
        return -1;
    }

2071 2072 2073 2074 2075
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);

    if (!pool) {
2076 2077
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2078 2079 2080 2081
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
2082 2083
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2084 2085 2086 2087 2088 2089
        goto out;
    }

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

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

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

2103
    if (storageVolumeWipeInternal(vol, algorithm) == -1) {
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
        goto out;
    }

    ret = 0;

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

    return ret;

}

2118 2119 2120 2121 2122 2123 2124
static int
storageVolumeWipe(virStorageVolPtr obj,
                  unsigned int flags)
{
    return storageVolumeWipePattern(obj, VIR_STORAGE_VOL_WIPE_ALG_ZERO, flags);
}

2125 2126 2127
static int
storageVolumeDelete(virStorageVolPtr obj,
                    unsigned int flags) {
2128 2129
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2130
    virStorageBackendPtr backend;
2131
    virStorageVolDefPtr vol = NULL;
2132
    unsigned int i;
2133
    int ret = -1;
2134

2135
    storageDriverLock(driver);
2136
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2137 2138
    storageDriverUnlock(driver);

2139
    if (!pool) {
2140 2141
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2142
        goto cleanup;
2143 2144 2145
    }

    if (!virStoragePoolObjIsActive(pool)) {
2146 2147
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2148
        goto cleanup;
2149 2150 2151
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2152
        goto cleanup;
2153 2154 2155 2156

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

    if (!vol) {
2157 2158 2159
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2160
        goto cleanup;
2161 2162
    }

2163
    if (vol->building) {
2164 2165 2166
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
2167 2168 2169
        goto cleanup;
    }

2170
    if (!backend->deleteVol) {
2171 2172
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("storage pool does not support vol deletion"));
2173

2174
        goto cleanup;
2175 2176
    }

2177 2178 2179
    if (backend->deleteVol(obj->conn, pool, vol, flags) < 0)
        goto cleanup;

2180 2181
    for (i = 0 ; i < pool->volumes.count ; i++) {
        if (pool->volumes.objs[i] == vol) {
2182
            VIR_INFO("Deleting volume '%s' from storage pool '%s'",
2183
                     vol->name, pool->def->name);
2184
            virStorageVolDefFree(vol);
2185
            vol = NULL;
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195

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

2196 2197 2198
            break;
        }
    }
2199
    ret = 0;
2200

2201
cleanup:
2202 2203
    if (pool)
        virStoragePoolObjUnlock(pool);
2204
    return ret;
2205 2206 2207 2208 2209
}

static int
storageVolumeGetInfo(virStorageVolPtr obj,
                     virStorageVolInfoPtr info) {
2210 2211
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2212 2213
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
2214
    int ret = -1;
2215

2216
    storageDriverLock(driver);
2217
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2218 2219
    storageDriverUnlock(driver);

2220
    if (!pool) {
2221 2222
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2223
        goto cleanup;
2224 2225 2226
    }

    if (!virStoragePoolObjIsActive(pool)) {
2227 2228
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2229
        goto cleanup;
2230 2231 2232 2233 2234
    }

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

    if (!vol) {
2235 2236 2237
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2238
        goto cleanup;
2239 2240 2241
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2242
        goto cleanup;
2243 2244 2245

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

    memset(info, 0, sizeof(*info));
2249
    info->type = vol->type;
2250 2251
    info->capacity = vol->capacity;
    info->allocation = vol->allocation;
2252
    ret = 0;
2253

2254
cleanup:
2255 2256
    if (pool)
        virStoragePoolObjUnlock(pool);
2257
    return ret;
2258 2259 2260 2261
}

static char *
storageVolumeGetXMLDesc(virStorageVolPtr obj,
E
Eric Blake 已提交
2262 2263
                        unsigned int flags)
{
2264 2265
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2266 2267
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
2268
    char *ret = NULL;
2269

E
Eric Blake 已提交
2270 2271
    virCheckFlags(0, NULL);

2272
    storageDriverLock(driver);
2273
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2274 2275
    storageDriverUnlock(driver);

2276
    if (!pool) {
2277 2278
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2279
        goto cleanup;
2280 2281 2282
    }

    if (!virStoragePoolObjIsActive(pool)) {
2283 2284
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2285
        goto cleanup;
2286 2287 2288 2289 2290
    }

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

    if (!vol) {
2291 2292 2293
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2294
        goto cleanup;
2295 2296 2297
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2298
        goto cleanup;
2299 2300 2301 2302

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

2304
    ret = virStorageVolDefFormat(pool->def, vol);
2305 2306

cleanup:
2307 2308 2309
    if (pool)
        virStoragePoolObjUnlock(pool);

2310
    return ret;
2311 2312 2313 2314
}

static char *
storageVolumeGetPath(virStorageVolPtr obj) {
2315
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
2316
    virStoragePoolObjPtr pool;
2317
    virStorageVolDefPtr vol;
2318
    char *ret = NULL;
2319

2320 2321 2322
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);
2323
    if (!pool) {
2324 2325
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       "%s", _("no storage pool with matching uuid"));
2326
        goto cleanup;
2327 2328 2329
    }

    if (!virStoragePoolObjIsActive(pool)) {
2330 2331
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("storage pool is not active"));
2332
        goto cleanup;
2333 2334 2335 2336 2337
    }

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

    if (!vol) {
2338 2339 2340
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2341
        goto cleanup;
2342 2343 2344
    }

    ret = strdup(vol->target.path);
2345
    if (ret == NULL)
2346
        virReportOOMError();
2347 2348

cleanup:
2349 2350
    if (pool)
        virStoragePoolObjUnlock(pool);
2351 2352 2353
    return ret;
}

2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
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;
}

2371
static virStorageDriver storageDriver = {
2372
    .name = "storage",
2373 2374 2375 2376 2377 2378
    .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 */
2379
    .listAllPools = storageListAllPools, /* 0.10.2 */
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
    .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 */
2398
    .poolListAllVolumes = storagePoolListAllVolumes, /* 0.10.2 */
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408

    .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 */
2409
    .volWipePattern = storageVolumeWipePattern, /* 0.9.10 */
2410 2411 2412
    .volGetInfo = storageVolumeGetInfo, /* 0.4.0 */
    .volGetXMLDesc = storageVolumeGetXMLDesc, /* 0.4.0 */
    .volGetPath = storageVolumeGetPath, /* 0.4.0 */
2413
    .volResize = storageVolumeResize, /* 0.9.10 */
2414 2415 2416

    .poolIsActive = storagePoolIsActive, /* 0.7.3 */
    .poolIsPersistent = storagePoolIsPersistent, /* 0.7.3 */
2417 2418 2419 2420
};


static virStateDriver stateDriver = {
2421
    .name = "Storage",
2422 2423 2424 2425
    .initialize = storageDriverStartup,
    .cleanup = storageDriverShutdown,
    .reload = storageDriverReload,
    .active = storageDriverActive,
2426 2427 2428 2429 2430 2431 2432
};

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