storage_driver.c 68.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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28
 *
 * 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 "virerror.h"
40
#include "datatypes.h"
41
#include "driver.h"
42
#include "virutil.h"
43 44
#include "storage_driver.h"
#include "storage_conf.h"
45
#include "viralloc.h"
46
#include "storage_backend.h"
47
#include "virlog.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 133
storageDriverStartup(bool privileged,
                     virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                     void *opaque ATTRIBUTE_UNUSED)
134
{
135 136
    char *base = NULL;

137
    if (VIR_ALLOC(driverState) < 0)
138 139
        return -1;

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

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

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

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

166
    VIR_FREE(base);
167

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

174
    storageDriverUnlock(driverState);
175 176
    return 0;

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

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

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

    return 0;
}


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

218
    storageDriverLock(driverState);
219 220

    /* free inactive pools */
221 222 223 224
    virStoragePoolObjListFree(&driverState->pools);

    VIR_FREE(driverState->configDir);
    VIR_FREE(driverState->autostartDir);
225
    storageDriverUnlock(driverState);
226
    virMutexDestroy(&driverState->lock);
227
    VIR_FREE(driverState);
228 229 230 231 232 233 234 235 236

    return 0;
}



static virStoragePoolPtr
storagePoolLookupByUUID(virConnectPtr conn,
                        const unsigned char *uuid) {
237 238 239
    virStorageDriverStatePtr driver = conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
240

241
    storageDriverLock(driver);
242
    pool = virStoragePoolObjFindByUUID(&driver->pools, uuid);
243 244
    storageDriverUnlock(driver);

245
    if (!pool) {
246
        virReportError(VIR_ERR_NO_STORAGE_POOL,
247
                       _("no storage pool with matching uuid %s"), uuid);
248
        goto cleanup;
249 250
    }

251 252
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
253 254

cleanup:
255 256
    if (pool)
        virStoragePoolObjUnlock(pool);
257 258 259 260 261 262
    return ret;
}

static virStoragePoolPtr
storagePoolLookupByName(virConnectPtr conn,
                        const char *name) {
263 264 265
    virStorageDriverStatePtr driver = conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
266

267
    storageDriverLock(driver);
268
    pool = virStoragePoolObjFindByName(&driver->pools, name);
269 270
    storageDriverUnlock(driver);

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

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

cleanup:
281 282
    if (pool)
        virStoragePoolObjUnlock(pool);
283 284 285 286 287 288 289 290 291 292 293
    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 已提交
294 295 296 297
            unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    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) {
313
    virStorageDriverStatePtr driver = conn->storagePrivateData;
314 315
    unsigned int i, nactive = 0;

316 317 318
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
319 320
        if (virStoragePoolObjIsActive(driver->pools.objs[i]))
            nactive++;
321 322 323
        virStoragePoolObjUnlock(driver->pools.objs[i]);
    }
    storageDriverUnlock(driver);
324 325

    return nactive;
326 327 328 329 330 331
}

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

335
    storageDriverLock(driver);
336
    for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
337
        virStoragePoolObjLock(driver->pools.objs[i]);
338 339
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
340
                virStoragePoolObjUnlock(driver->pools.objs[i]);
341
                virReportOOMError();
342 343 344 345
                goto cleanup;
            }
            got++;
        }
346
        virStoragePoolObjUnlock(driver->pools.objs[i]);
347
    }
348
    storageDriverUnlock(driver);
349 350 351
    return got;

 cleanup:
352 353 354
    storageDriverUnlock(driver);
    for (i = 0 ; i < got ; i++)
        VIR_FREE(names[i]);
355
    memset(names, 0, nnames * sizeof(*names));
356 357 358 359 360
    return -1;
}

static int
storageNumDefinedPools(virConnectPtr conn) {
361
    virStorageDriverStatePtr driver = conn->storagePrivateData;
362 363
    unsigned int i, nactive = 0;

364 365 366
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
367 368
        if (!virStoragePoolObjIsActive(driver->pools.objs[i]))
            nactive++;
369 370 371
        virStoragePoolObjUnlock(driver->pools.objs[i]);
    }
    storageDriverUnlock(driver);
372 373

    return nactive;
374 375 376 377 378 379
}

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

383
    storageDriverLock(driver);
384
    for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
385
        virStoragePoolObjLock(driver->pools.objs[i]);
386 387
        if (!virStoragePoolObjIsActive(driver->pools.objs[i])) {
            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
388
                virStoragePoolObjUnlock(driver->pools.objs[i]);
389
                virReportOOMError();
390 391 392 393
                goto cleanup;
            }
            got++;
        }
394
        virStoragePoolObjUnlock(driver->pools.objs[i]);
395
    }
396
    storageDriverUnlock(driver);
397 398 399
    return got;

 cleanup:
400
    storageDriverUnlock(driver);
401
    for (i = 0 ; i < got ; i++) {
402
        VIR_FREE(names[i]);
403
    }
404
    memset(names, 0, nnames * sizeof(*names));
405 406 407
    return -1;
}

408 409
/* This method is required to be re-entrant / thread safe, so
   uses no driver lock */
410 411 412 413 414 415 416 417
static char *
storageFindPoolSources(virConnectPtr conn,
                       const char *type,
                       const char *srcSpec,
                       unsigned int flags)
{
    int backend_type;
    virStorageBackendPtr backend;
418
    char *ret = NULL;
419

420
    backend_type = virStoragePoolTypeFromString(type);
421
    if (backend_type < 0) {
422 423
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
424
        goto cleanup;
425
    }
426 427 428

    backend = virStorageBackendForType(backend_type);
    if (backend == NULL)
429
        goto cleanup;
430

431
    if (!backend->findPoolSources) {
432 433 434
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source "
                         "discovery"), type);
435 436 437 438
        goto cleanup;
    }

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

440 441
cleanup:
    return ret;
442 443 444
}


445
static int storagePoolIsActive(virStoragePoolPtr pool)
446
{
447
    virStorageDriverStatePtr driver = pool->conn->storagePrivateData;
448 449 450 451
    virStoragePoolObjPtr obj;
    int ret = -1;

    storageDriverLock(driver);
452
    obj = virStoragePoolObjFindByUUID(&driver->pools, pool->uuid);
453 454
    storageDriverUnlock(driver);
    if (!obj) {
455
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
456 457 458 459 460 461 462 463 464 465
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

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

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

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

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


488 489 490
static virStoragePoolPtr
storagePoolCreate(virConnectPtr conn,
                  const char *xml,
E
Eric Blake 已提交
491 492
                  unsigned int flags)
{
493
    virStorageDriverStatePtr driver = conn->storagePrivateData;
494
    virStoragePoolDefPtr def;
495
    virStoragePoolObjPtr pool = NULL;
496
    virStoragePoolPtr ret = NULL;
497 498
    virStorageBackendPtr backend;

E
Eric Blake 已提交
499 500
    virCheckFlags(0, NULL);

501
    storageDriverLock(driver);
502
    if (!(def = virStoragePoolDefParseString(xml)))
503
        goto cleanup;
504

505
    if (virStoragePoolObjIsDuplicate(&driver->pools, def, 1) < 0)
506
        goto cleanup;
507

508 509 510
    if (virStoragePoolSourceFindDuplicate(&driver->pools, def) < 0)
        goto cleanup;

511 512
    if ((backend = virStorageBackendForType(def->type)) == NULL)
        goto cleanup;
513

514
    if (!(pool = virStoragePoolObjAssignDef(&driver->pools, def)))
515 516
        goto cleanup;
    def = NULL;
517

518
    if (backend->startPool &&
519 520 521
        backend->startPool(conn, pool) < 0) {
        virStoragePoolObjRemove(&driver->pools, pool);
        pool = NULL;
522
        goto cleanup;
523
    }
524

525 526 527
    if (backend->refreshPool(conn, pool) < 0) {
        if (backend->stopPool)
            backend->stopPool(conn, pool);
528 529
        virStoragePoolObjRemove(&driver->pools, pool);
        pool = NULL;
530
        goto cleanup;
531
    }
532
    VIR_INFO("Creating storage pool '%s'", pool->def->name);
533 534
    pool->active = 1;

535 536
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
537

538 539
cleanup:
    virStoragePoolDefFree(def);
540
    if (pool)
541
        virStoragePoolObjUnlock(pool);
542
    storageDriverUnlock(driver);
543 544 545 546 547 548
    return ret;
}

static virStoragePoolPtr
storagePoolDefine(virConnectPtr conn,
                  const char *xml,
E
Eric Blake 已提交
549 550
                  unsigned int flags)
{
551
    virStorageDriverStatePtr driver = conn->storagePrivateData;
552
    virStoragePoolDefPtr def;
553
    virStoragePoolObjPtr pool = NULL;
554
    virStoragePoolPtr ret = NULL;
555

E
Eric Blake 已提交
556 557
    virCheckFlags(0, NULL);

558
    storageDriverLock(driver);
559
    if (!(def = virStoragePoolDefParseString(xml)))
560
        goto cleanup;
561

562 563 564
    if (virStoragePoolObjIsDuplicate(&driver->pools, def, 0) < 0)
        goto cleanup;

565 566 567
    if (virStoragePoolSourceFindDuplicate(&driver->pools, def) < 0)
        goto cleanup;

568
    if (virStorageBackendForType(def->type) == NULL)
569
        goto cleanup;
570

571
    if (!(pool = virStoragePoolObjAssignDef(&driver->pools, def)))
572
        goto cleanup;
573

574
    if (virStoragePoolObjSaveDef(driver, pool, def) < 0) {
575
        virStoragePoolObjRemove(&driver->pools, pool);
576
        def = NULL;
577
        goto cleanup;
578
    }
579
    def = NULL;
580

581
    VIR_INFO("Defining storage pool '%s'", pool->def->name);
582 583
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
584 585 586

cleanup:
    virStoragePoolDefFree(def);
587 588 589
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
590 591 592 593 594
    return ret;
}

static int
storagePoolUndefine(virStoragePoolPtr obj) {
595 596 597
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
598

599
    storageDriverLock(driver);
600
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
601
    if (!pool) {
602
        virReportError(VIR_ERR_NO_STORAGE_POOL,
603
                       _("no storage pool with matching uuid %s"), obj->uuid);
604
        goto cleanup;
605 606 607
    }

    if (virStoragePoolObjIsActive(pool)) {
608
        virReportError(VIR_ERR_OPERATION_INVALID,
609 610
                       _("storage pool '%s' is still active"),
                       pool->def->name);
611
        goto cleanup;
612 613
    }

614
    if (pool->asyncjobs > 0) {
615 616 617
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("pool '%s' has asynchronous jobs running."),
                       pool->def->name);
618 619 620
        goto cleanup;
    }

621
    if (virStoragePoolObjDeleteDef(pool) < 0)
622
        goto cleanup;
623

624 625
    if (unlink(pool->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
        char ebuf[1024];
626
        VIR_ERROR(_("Failed to delete autostart link '%s': %s"),
627
                  pool->autostartLink, virStrerror(errno, ebuf, sizeof(ebuf)));
628
    }
629

630 631
    VIR_FREE(pool->configFile);
    VIR_FREE(pool->autostartLink);
632

633
    VIR_INFO("Undefining storage pool '%s'", pool->def->name);
634
    virStoragePoolObjRemove(&driver->pools, pool);
635
    pool = NULL;
636
    ret = 0;
637

638
cleanup:
639 640 641
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
642
    return ret;
643 644 645 646
}

static int
storagePoolStart(virStoragePoolPtr obj,
E
Eric Blake 已提交
647 648
                 unsigned int flags)
{
649 650
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
651
    virStorageBackendPtr backend;
652
    int ret = -1;
653

E
Eric Blake 已提交
654 655
    virCheckFlags(0, -1);

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

660
    if (!pool) {
661
        virReportError(VIR_ERR_NO_STORAGE_POOL,
662
                       _("no storage pool with matching uuid %s"), obj->uuid);
663
        goto cleanup;
664 665
    }

666 667
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
668 669

    if (virStoragePoolObjIsActive(pool)) {
670
        virReportError(VIR_ERR_OPERATION_INVALID,
671 672
                       _("storage pool '%s' is already active"),
                       pool->def->name);
673
        goto cleanup;
674 675 676
    }
    if (backend->startPool &&
        backend->startPool(obj->conn, pool) < 0)
677 678
        goto cleanup;

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

685
    VIR_INFO("Starting up storage pool '%s'", pool->def->name);
686
    pool->active = 1;
687
    ret = 0;
688

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

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

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

707
    if (!pool) {
708
        virReportError(VIR_ERR_NO_STORAGE_POOL,
709
                       _("no storage pool with matching uuid %s"), obj->uuid);
710
        goto cleanup;
711 712
    }

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

    if (virStoragePoolObjIsActive(pool)) {
717
        virReportError(VIR_ERR_OPERATION_INVALID,
718 719
                       _("storage pool '%s' is already active"),
                       pool->def->name);
720
        goto cleanup;
721 722 723 724
    }

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

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


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

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

745
    if (!pool) {
746
        virReportError(VIR_ERR_NO_STORAGE_POOL,
747
                       _("no storage pool with matching uuid %s"), obj->uuid);
748
        goto cleanup;
749 750
    }

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

    if (!virStoragePoolObjIsActive(pool)) {
755
        virReportError(VIR_ERR_OPERATION_INVALID,
756
                       _("storage pool '%s' is not active"), pool->def->name);
757
        goto cleanup;
758 759
    }

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

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

    virStoragePoolObjClearVols(pool);

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

776
    if (pool->configFile == NULL) {
777
        virStoragePoolObjRemove(&driver->pools, pool);
778
        pool = NULL;
779 780 781 782
    } else if (pool->newDef) {
        virStoragePoolDefFree(pool->def);
        pool->def = pool->newDef;
        pool->newDef = NULL;
783
    }
784
    ret = 0;
785

786
cleanup:
787 788 789
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
790
    return ret;
791 792 793 794 795
}

static int
storagePoolDelete(virStoragePoolPtr obj,
                  unsigned int flags) {
796 797
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
798
    virStorageBackendPtr backend;
799
    int ret = -1;
800

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

805
    if (!pool) {
806
        virReportError(VIR_ERR_NO_STORAGE_POOL,
807
                       _("no storage pool with matching uuid %s"), obj->uuid);
808
        goto cleanup;
809 810
    }

811 812
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
813 814

    if (virStoragePoolObjIsActive(pool)) {
815
        virReportError(VIR_ERR_OPERATION_INVALID,
816 817
                       _("storage pool '%s' is still active"),
                       pool->def->name);
818
        goto cleanup;
819 820
    }

821
    if (pool->asyncjobs > 0) {
822 823
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("pool '%s' has asynchronous jobs running."),
824 825 826 827
                              pool->def->name);
        goto cleanup;
    }

828
    if (!backend->deletePool) {
829 830
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("pool does not support pool deletion"));
831
        goto cleanup;
832 833
    }
    if (backend->deletePool(obj->conn, pool, flags) < 0)
834
        goto cleanup;
835
    VIR_INFO("Deleting storage pool '%s'", pool->def->name);
836
    ret = 0;
837

838
cleanup:
839 840
    if (pool)
        virStoragePoolObjUnlock(pool);
841
    return ret;
842 843 844 845 846
}


static int
storagePoolRefresh(virStoragePoolPtr obj,
E
Eric Blake 已提交
847 848
                   unsigned int flags)
{
849 850
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
851
    virStorageBackendPtr backend;
852
    int ret = -1;
853

E
Eric Blake 已提交
854 855
    virCheckFlags(0, -1);

856
    storageDriverLock(driver);
857
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
858

859
    if (!pool) {
860
        virReportError(VIR_ERR_NO_STORAGE_POOL,
861
                       _("no storage pool with matching uuid %s"), obj->uuid);
862
        goto cleanup;
863 864
    }

865 866
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
867 868

    if (!virStoragePoolObjIsActive(pool)) {
869
        virReportError(VIR_ERR_OPERATION_INVALID,
870
                       _("storage pool '%s' is not active"), pool->def->name);
871
        goto cleanup;
872 873
    }

874
    if (pool->asyncjobs > 0) {
875 876 877
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("pool '%s' has asynchronous jobs running."),
                       pool->def->name);
878 879 880
        goto cleanup;
    }

881
    virStoragePoolObjClearVols(pool);
882
    if (backend->refreshPool(obj->conn, pool) < 0) {
883 884 885 886 887
        if (backend->stopPool)
            backend->stopPool(obj->conn, pool);

        pool->active = 0;

888
        if (pool->configFile == NULL) {
889
            virStoragePoolObjRemove(&driver->pools, pool);
890 891
            pool = NULL;
        }
892
        goto cleanup;
893
    }
894
    ret = 0;
895

896
cleanup:
897 898
    if (pool)
        virStoragePoolObjUnlock(pool);
899
    storageDriverUnlock(driver);
900 901 902 903 904 905 906
    return ret;
}


static int
storagePoolGetInfo(virStoragePoolPtr obj,
                   virStoragePoolInfoPtr info) {
907 908 909
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
910

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

915
    if (!pool) {
916
        virReportError(VIR_ERR_NO_STORAGE_POOL,
917
                       _("no storage pool with matching uuid %s"), obj->uuid);
918
        goto cleanup;
919 920
    }

921
    if (virStorageBackendForType(pool->def->type) == NULL)
922
        goto cleanup;
923 924 925 926 927 928 929 930 931

    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;
932
    ret = 0;
933

934
cleanup:
935 936
    if (pool)
        virStoragePoolObjUnlock(pool);
937
    return ret;
938 939 940
}

static char *
941
storagePoolGetXMLDesc(virStoragePoolPtr obj,
E
Eric Blake 已提交
942 943
                      unsigned int flags)
{
944 945
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
946
    virStoragePoolDefPtr def;
947
    char *ret = NULL;
948

949
    virCheckFlags(VIR_STORAGE_XML_INACTIVE, NULL);
E
Eric Blake 已提交
950

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

955
    if (!pool) {
956
        virReportError(VIR_ERR_NO_STORAGE_POOL,
957
                       _("no storage pool with matching uuid %s"), obj->uuid);
958
        goto cleanup;
959 960
    }

961 962 963 964 965 966
    if ((flags & VIR_STORAGE_XML_INACTIVE) && pool->newDef)
        def = pool->newDef;
    else
        def = pool->def;

    ret = virStoragePoolDefFormat(def);
967 968

cleanup:
969 970
    if (pool)
        virStoragePoolObjUnlock(pool);
971
    return ret;
972 973 974 975 976
}

static int
storagePoolGetAutostart(virStoragePoolPtr obj,
                        int *autostart) {
977 978 979
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
980

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

985
    if (!pool) {
986
        virReportError(VIR_ERR_NO_STORAGE_POOL,
987
                       _("no storage pool with matching uuid %s"), obj->uuid);
988
        goto cleanup;
989 990 991 992 993 994 995
    }

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

998
cleanup:
999 1000
    if (pool)
        virStoragePoolObjUnlock(pool);
1001
    return ret;
1002 1003 1004 1005 1006
}

static int
storagePoolSetAutostart(virStoragePoolPtr obj,
                        int autostart) {
1007 1008 1009
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
1010

1011
    storageDriverLock(driver);
1012
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1013

1014
    if (!pool) {
1015
        virReportError(VIR_ERR_NO_STORAGE_POOL,
1016
                       _("no storage pool with matching uuid %s"), obj->uuid);
1017
        goto cleanup;
1018 1019 1020
    }

    if (!pool->configFile) {
1021 1022
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("pool has no config file"));
1023
        goto cleanup;
1024 1025 1026 1027
    }

    autostart = (autostart != 0);

1028 1029
    if (pool->autostart != autostart) {
        if (autostart) {
1030 1031
            if (virFileMakePath(driver->autostartDir) < 0) {
                virReportSystemError(errno,
1032 1033
                                     _("cannot create autostart directory %s"),
                                     driver->autostartDir);
1034 1035
                goto cleanup;
            }
1036

1037
            if (symlink(pool->configFile, pool->autostartLink) < 0) {
1038
                virReportSystemError(errno,
1039 1040
                                     _("Failed to create symlink '%s' to '%s'"),
                                     pool->autostartLink, pool->configFile);
1041 1042 1043 1044 1045
                goto cleanup;
            }
        } else {
            if (unlink(pool->autostartLink) < 0 &&
                errno != ENOENT && errno != ENOTDIR) {
1046
                virReportSystemError(errno,
1047 1048
                                     _("Failed to delete symlink '%s'"),
                                     pool->autostartLink);
1049 1050
                goto cleanup;
            }
1051
        }
1052
        pool->autostart = autostart;
1053
    }
1054
    ret = 0;
1055

1056
cleanup:
1057 1058
    if (pool)
        virStoragePoolObjUnlock(pool);
1059
    storageDriverUnlock(driver);
1060
    return ret;
1061 1062 1063 1064 1065
}


static int
storagePoolNumVolumes(virStoragePoolPtr obj) {
1066 1067 1068
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
1069

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

1074
    if (!pool) {
1075
        virReportError(VIR_ERR_NO_STORAGE_POOL,
1076
                       _("no storage pool with matching uuid %s"), obj->uuid);
1077
        goto cleanup;
1078 1079 1080
    }

    if (!virStoragePoolObjIsActive(pool)) {
1081
        virReportError(VIR_ERR_OPERATION_INVALID,
1082
                       _("storage pool '%s' is not active"), pool->def->name);
1083
        goto cleanup;
1084
    }
1085
    ret = pool->volumes.count;
1086

1087
cleanup:
1088 1089
    if (pool)
        virStoragePoolObjUnlock(pool);
1090
    return ret;
1091 1092 1093 1094 1095 1096
}

static int
storagePoolListVolumes(virStoragePoolPtr obj,
                       char **const names,
                       int maxnames) {
1097 1098
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1099
    int i, n = 0;
1100

1101 1102
    memset(names, 0, maxnames * sizeof(*names));

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

1107
    if (!pool) {
1108
        virReportError(VIR_ERR_NO_STORAGE_POOL,
1109
                       _("no storage pool with matching uuid %s"), obj->uuid);
1110
        goto cleanup;
1111 1112 1113
    }

    if (!virStoragePoolObjIsActive(pool)) {
1114
        virReportError(VIR_ERR_OPERATION_INVALID,
1115
                       _("storage pool '%s' is not active"), pool->def->name);
1116
        goto cleanup;
1117 1118
    }

1119 1120
    for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) {
1121
            virReportOOMError();
1122 1123 1124 1125
            goto cleanup;
        }
    }

1126
    virStoragePoolObjUnlock(pool);
1127
    return n;
1128 1129

 cleanup:
1130 1131
    if (pool)
        virStoragePoolObjUnlock(pool);
1132
    for (n = 0 ; n < maxnames ; n++)
1133
        VIR_FREE(names[n]);
1134

1135
    memset(names, 0, maxnames * sizeof(*names));
1136 1137 1138
    return -1;
}

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
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) {
1158 1159 1160
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching uuid %s"),
                       pool->uuid);
1161 1162 1163 1164
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(obj)) {
1165 1166
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), obj->def->name);
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
        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,
1184 1185
                                     obj->volumes.objs[i]->key,
                                     NULL, NULL)))
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
            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]);
        }
1200
        VIR_FREE(tmp_vols);
1201 1202 1203 1204 1205 1206 1207
    }

    if (obj)
        virStoragePoolObjUnlock(obj);

    return ret;
}
1208 1209 1210 1211

static virStorageVolPtr
storageVolumeLookupByName(virStoragePoolPtr obj,
                          const char *name) {
1212 1213
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1214
    virStorageVolDefPtr vol;
1215
    virStorageVolPtr ret = NULL;
1216

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

1221
    if (!pool) {
1222
        virReportError(VIR_ERR_NO_STORAGE_POOL,
1223
                       _("no storage pool with matching uuid %s"), obj->uuid);
1224
        goto cleanup;
1225 1226 1227
    }

    if (!virStoragePoolObjIsActive(pool)) {
1228
        virReportError(VIR_ERR_OPERATION_INVALID,
1229
                       _("storage pool '%s' is not active"), pool->def->name);
1230
        goto cleanup;
1231 1232 1233 1234 1235
    }

    vol = virStorageVolDefFindByName(pool, name);

    if (!vol) {
1236 1237 1238
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       name);
1239
        goto cleanup;
1240 1241
    }

1242 1243
    ret = virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key,
                           NULL, NULL);
1244 1245

cleanup:
1246 1247
    if (pool)
        virStoragePoolObjUnlock(pool);
1248
    return ret;
1249 1250 1251 1252 1253 1254
}


static virStorageVolPtr
storageVolumeLookupByKey(virConnectPtr conn,
                         const char *key) {
1255
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1256
    unsigned int i;
1257
    virStorageVolPtr ret = NULL;
1258

1259 1260 1261
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1262 1263 1264
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
            virStorageVolDefPtr vol =
                virStorageVolDefFindByKey(driver->pools.objs[i], key);
1265

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

1277
    if (!ret)
1278
        virReportError(VIR_ERR_NO_STORAGE_VOL,
1279
                       _("no storage vol with matching key %s"), key);
1280 1281

    return ret;
1282 1283 1284 1285 1286
}

static virStorageVolPtr
storageVolumeLookupByPath(virConnectPtr conn,
                          const char *path) {
1287
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1288
    unsigned int i;
1289
    virStorageVolPtr ret = NULL;
1290 1291 1292 1293 1294
    char *cleanpath;

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

1296 1297 1298
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1299
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
1300
            virStorageVolDefPtr vol;
1301 1302
            const char *stable_path;

1303
            stable_path = virStorageBackendStablePath(driver->pools.objs[i],
1304 1305
                                                      cleanpath,
                                                      false);
1306
            if (stable_path == NULL) {
1307 1308 1309 1310 1311
                /* 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);
1312
                virStoragePoolObjUnlock(driver->pools.objs[i]);
1313
                continue;
1314
            }
1315 1316 1317 1318

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

1320
            if (vol)
1321 1322 1323
                ret = virGetStorageVol(conn,
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
1324 1325
                                       vol->key,
                                       NULL, NULL);
1326
        }
1327
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1328 1329
    }

1330
    if (!ret)
1331
        virReportError(VIR_ERR_NO_STORAGE_VOL,
1332
                       _("no storage vol with matching path %s"), path);
1333

1334
    VIR_FREE(cleanpath);
1335
    storageDriverUnlock(driver);
1336
    return ret;
1337 1338
}

1339 1340
static int storageVolumeDelete(virStorageVolPtr obj, unsigned int flags);

1341 1342 1343
static virStorageVolPtr
storageVolumeCreateXML(virStoragePoolPtr obj,
                       const char *xmldesc,
E
Eric Blake 已提交
1344 1345
                       unsigned int flags)
{
1346 1347
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1348
    virStorageBackendPtr backend;
1349 1350
    virStorageVolDefPtr voldef = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1351

1352
    virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, NULL);
E
Eric Blake 已提交
1353

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

1358
    if (!pool) {
1359
        virReportError(VIR_ERR_NO_STORAGE_POOL,
1360
                       _("no storage pool with matching uuid %s"), obj->uuid);
1361
        goto cleanup;
1362 1363 1364
    }

    if (!virStoragePoolObjIsActive(pool)) {
1365
        virReportError(VIR_ERR_OPERATION_INVALID,
1366
                       _("storage pool '%s' is not active"), pool->def->name);
1367
        goto cleanup;
1368 1369 1370
    }

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

1373
    voldef = virStorageVolDefParseString(pool->def, xmldesc);
1374
    if (voldef == NULL)
1375
        goto cleanup;
1376

1377
    if (virStorageVolDefFindByName(pool, voldef->name)) {
1378
        virReportError(VIR_ERR_NO_STORAGE_VOL,
1379
                       _("storage vol '%s' already exists"), voldef->name);
1380
        goto cleanup;
1381 1382
    }

1383 1384
    if (VIR_REALLOC_N(pool->volumes.objs,
                      pool->volumes.count+1) < 0) {
1385
        virReportOOMError();
1386
        goto cleanup;
1387 1388
    }

1389
    if (!backend->createVol) {
1390 1391 1392
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("storage pool does not support volume "
                               "creation"));
1393
        goto cleanup;
1394 1395
    }

1396
    if (backend->createVol(obj->conn, pool, voldef) < 0) {
1397
        goto cleanup;
1398 1399
    }

1400 1401
    pool->volumes.objs[pool->volumes.count++] = voldef;
    volobj = virGetStorageVol(obj->conn, pool->def->name, voldef->name,
1402
                              voldef->key, NULL, NULL);
1403 1404 1405 1406
    if (!volobj) {
        pool->volumes.count--;
        goto cleanup;
    }
1407

1408
    if (backend->buildVol) {
1409 1410 1411 1412
        int buildret;
        virStorageVolDefPtr buildvoldef = NULL;

        if (VIR_ALLOC(buildvoldef) < 0) {
1413
            virReportOOMError();
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
            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);

1429
        buildret = backend->buildVol(obj->conn, pool, buildvoldef, flags);
1430

1431
        storageDriverLock(driver);
1432
        virStoragePoolObjLock(pool);
1433 1434
        storageDriverUnlock(driver);

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
        voldef->building = 0;
        pool->asyncjobs--;

        voldef = NULL;
        VIR_FREE(buildvoldef);

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

    }

1450
    VIR_INFO("Creating volume '%s' in storage pool '%s'",
1451
             volobj->name, pool->def->name);
1452 1453 1454
    ret = volobj;
    volobj = NULL;
    voldef = NULL;
1455

1456
cleanup:
1457
    virObjectUnref(volobj);
1458
    virStorageVolDefFree(voldef);
1459 1460
    if (pool)
        virStoragePoolObjUnlock(pool);
1461
    return ret;
1462 1463
}

1464 1465 1466 1467
static virStorageVolPtr
storageVolumeCreateXMLFrom(virStoragePoolPtr obj,
                           const char *xmldesc,
                           virStorageVolPtr vobj,
E
Eric Blake 已提交
1468 1469
                           unsigned int flags)
{
1470 1471 1472 1473 1474
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool, origpool = NULL;
    virStorageBackendPtr backend;
    virStorageVolDefPtr origvol = NULL, newvol = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1475
    int buildret;
1476

1477
    virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, NULL);
E
Eric Blake 已提交
1478

1479 1480
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1481
    if (pool && STRNEQ(obj->name, vobj->pool)) {
1482
        virStoragePoolObjUnlock(pool);
1483
        origpool = virStoragePoolObjFindByName(&driver->pools, vobj->pool);
1484
        virStoragePoolObjLock(pool);
1485
    }
1486 1487
    storageDriverUnlock(driver);
    if (!pool) {
1488
        virReportError(VIR_ERR_NO_STORAGE_POOL,
1489
                       _("no storage pool with matching uuid %s"), obj->uuid);
1490 1491 1492
        goto cleanup;
    }

1493
    if (STRNEQ(obj->name, vobj->pool) && !origpool) {
1494 1495 1496
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching name '%s'"),
                       vobj->pool);
1497 1498 1499 1500
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1501
        virReportError(VIR_ERR_OPERATION_INVALID,
1502
                       _("storage pool '%s' is not active"), pool->def->name);
1503 1504 1505
        goto cleanup;
    }

1506
    if (origpool && !virStoragePoolObjIsActive(origpool)) {
1507
        virReportError(VIR_ERR_OPERATION_INVALID,
1508 1509
                       _("storage pool '%s' is not active"),
                       origpool->def->name);
1510 1511 1512 1513 1514 1515
        goto cleanup;
    }

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

1516
    origvol = virStorageVolDefFindByName(origpool ? origpool : pool, vobj->name);
1517
    if (!origvol) {
1518 1519 1520
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vobj->name);
1521 1522 1523
        goto cleanup;
    }

1524
    newvol = virStorageVolDefParseString(pool->def, xmldesc);
1525 1526 1527 1528
    if (newvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(pool, newvol->name)) {
1529 1530 1531
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("storage volume name '%s' already in use."),
                       newvol->name);
1532 1533 1534 1535 1536 1537 1538
        goto cleanup;
    }

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

1539 1540 1541 1542 1543
    /* 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;

1544
    if (!backend->buildVolFrom) {
1545 1546
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("storage pool does not support volume creation from an existing volume"));
1547 1548 1549 1550
        goto cleanup;
    }

    if (origvol->building) {
1551 1552 1553
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       origvol->name);
1554 1555 1556 1557 1558 1559 1560 1561 1562
        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) {
1563
        virReportOOMError();
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
        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,
1574
                              newvol->key, NULL, NULL);
1575 1576 1577 1578 1579 1580 1581

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

1582
    if (origpool) {
1583 1584 1585 1586
        origpool->asyncjobs++;
        virStoragePoolObjUnlock(origpool);
    }

1587
    buildret = backend->buildVolFrom(obj->conn, pool, newvol, origvol, flags);
1588 1589 1590

    storageDriverLock(driver);
    virStoragePoolObjLock(pool);
1591
    if (origpool)
1592 1593 1594 1595 1596 1597 1598 1599
        virStoragePoolObjLock(origpool);
    storageDriverUnlock(driver);

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

1600
    if (origpool) {
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
        origpool->asyncjobs--;
        virStoragePoolObjUnlock(origpool);
        origpool = NULL;
    }

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

1613
    VIR_INFO("Creating volume '%s' in storage pool '%s'",
1614
             volobj->name, pool->def->name);
1615 1616 1617 1618
    ret = volobj;
    volobj = NULL;

cleanup:
1619
    virObjectUnref(volobj);
1620 1621 1622
    virStorageVolDefFree(newvol);
    if (pool)
        virStoragePoolObjUnlock(pool);
1623
    if (origpool)
1624 1625 1626 1627
        virStoragePoolObjUnlock(origpool);
    return ret;
}

1628

1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
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) {
1648
        virReportError(VIR_ERR_NO_STORAGE_POOL,
1649 1650
                       _("no storage pool with matching name '%s'"),
                       obj->pool);
1651 1652 1653 1654
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1655
        virReportError(VIR_ERR_OPERATION_INVALID,
1656
                       _("storage pool '%s' is not active"), pool->def->name);
1657 1658 1659 1660 1661 1662
        goto out;
    }

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

    if (vol == NULL) {
1663 1664 1665
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
1666 1667 1668 1669
        goto out;
    }

    if (vol->building) {
1670 1671 1672
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
1673 1674 1675 1676 1677 1678
        goto out;
    }

    if (virFDStreamOpenFile(stream,
                            vol->target.path,
                            offset, length,
E
Eric Blake 已提交
1679
                            O_RDONLY) < 0)
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
        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) {
1711
        virReportError(VIR_ERR_NO_STORAGE_POOL,
1712 1713
                       _("no storage pool with matching name '%s'"),
                       obj->pool);
1714 1715 1716 1717
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1718
        virReportError(VIR_ERR_OPERATION_INVALID,
1719
                       _("storage pool '%s' is not active"), pool->def->name);
1720 1721 1722 1723 1724 1725
        goto out;
    }

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

    if (vol == NULL) {
1726 1727 1728
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
1729 1730 1731 1732
        goto out;
    }

    if (vol->building) {
1733 1734 1735
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
1736 1737 1738 1739 1740 1741 1742 1743
        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 已提交
1744
                            O_WRONLY) < 0)
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
        goto out;

    ret = 0;

out:
    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}

1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
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) {
1775 1776 1777
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching name '%s'"),
                       obj->pool);
1778 1779 1780 1781
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1782 1783
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->def->name);
1784 1785 1786 1787 1788 1789 1790 1791 1792
        goto out;
    }

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

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

    if (vol == NULL) {
1793 1794 1795
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
1796 1797 1798 1799
        goto out;
    }

    if (vol->building) {
1800 1801 1802
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
1803 1804
        goto out;
    }
1805

1806 1807 1808 1809 1810 1811 1812 1813
    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) {
1814
        virReportError(VIR_ERR_INVALID_ARG, "%s",
1815 1816
                       _("can't shrink capacity below "
                         "existing allocation"));
1817 1818 1819
        goto out;
    }

1820
    if (abs_capacity > vol->capacity + pool->def->available) {
1821
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
1822
                       _("Not enough space left on storage pool"));
1823 1824 1825 1826
        goto out;
    }

    if (!backend->resizeVol) {
1827
        virReportError(VIR_ERR_NO_SUPPORT, "%s",
1828 1829
                       _("storage pool does not support changing of "
                         "volume capacity"));
1830 1831 1832 1833 1834 1835
        goto out;
    }

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

O
Osier Yang 已提交
1836 1837
    vol->capacity = abs_capacity;
    ret = 0;
1838 1839 1840 1841 1842 1843 1844

out:
    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
1845

1846 1847 1848 1849 1850 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
/* 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 "
1880
                               "path '%s' to %ju bytes"),
E
Eric Blake 已提交
1881
                             vol->target.path, (uintmax_t)size);
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
    }

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 已提交
1903
              (uintmax_t)extent_start, (uintmax_t)extent_length);
1904 1905 1906 1907 1908

    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 已提交
1909
                             (uintmax_t)extent_start, vol->target.path);
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
        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;
    }

1931 1932 1933 1934 1935 1936 1937 1938
    if (fdatasync(fd) < 0) {
        ret = -errno;
        virReportSystemError(errno,
                             _("cannot sync data to volume with path '%s'"),
                             vol->target.path);
        goto out;
    }

1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
    VIR_DEBUG("Wrote %zu bytes to volume with path '%s'",
              *bytes_wiped, vol->target.path);

    ret = 0;

out:
    return ret;
}


static int
1950 1951
storageVolumeWipeInternal(virStorageVolDefPtr def,
                          unsigned int algorithm)
1952 1953 1954 1955 1956
{
    int ret = -1, fd = -1;
    struct stat st;
    char *writebuf = NULL;
    size_t bytes_wiped = 0;
1957
    virCommandPtr cmd = NULL;
1958

1959 1960
    VIR_DEBUG("Wiping volume with path '%s' and algorithm %u",
              def->target.path, algorithm);
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976

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

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

2013
        if (virCommandRun(cmd, NULL) < 0)
2014 2015
            goto out;

2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
        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);
        }
2036 2037 2038
    }

out:
2039
    virCommandFree(cmd);
2040
    VIR_FREE(writebuf);
2041
    VIR_FORCE_CLOSE(fd);
2042 2043 2044 2045 2046
    return ret;
}


static int
2047 2048 2049
storageVolumeWipePattern(virStorageVolPtr obj,
                         unsigned int algorithm,
                         unsigned int flags)
2050 2051 2052 2053 2054 2055
{
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool = NULL;
    virStorageVolDefPtr vol = NULL;
    int ret = -1;

2056
    virCheckFlags(0, -1);
2057

2058
    if (algorithm >= VIR_STORAGE_VOL_WIPE_ALG_LAST) {
2059 2060 2061
        virReportError(VIR_ERR_INVALID_ARG,
                       _("wiping algorithm %d not supported"),
                       algorithm);
2062 2063 2064
        return -1;
    }

2065 2066 2067 2068 2069
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);

    if (!pool) {
2070
        virReportError(VIR_ERR_NO_STORAGE_POOL,
2071 2072
                       _("no storage pool with matching name '%s'"),
                       obj->pool);
2073 2074 2075 2076
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
2077
        virReportError(VIR_ERR_OPERATION_INVALID,
2078
                       _("storage pool '%s' is not active"), pool->def->name);
2079 2080 2081 2082 2083 2084
        goto out;
    }

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

    if (vol == NULL) {
2085 2086 2087
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2088 2089 2090 2091
        goto out;
    }

    if (vol->building) {
2092 2093 2094
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
2095 2096 2097
        goto out;
    }

2098
    if (storageVolumeWipeInternal(vol, algorithm) == -1) {
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
        goto out;
    }

    ret = 0;

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

    return ret;

}

2113 2114 2115 2116 2117 2118 2119
static int
storageVolumeWipe(virStorageVolPtr obj,
                  unsigned int flags)
{
    return storageVolumeWipePattern(obj, VIR_STORAGE_VOL_WIPE_ALG_ZERO, flags);
}

2120 2121 2122
static int
storageVolumeDelete(virStorageVolPtr obj,
                    unsigned int flags) {
2123 2124
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2125
    virStorageBackendPtr backend;
2126
    virStorageVolDefPtr vol = NULL;
2127
    unsigned int i;
2128
    int ret = -1;
2129

2130
    storageDriverLock(driver);
2131
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2132 2133
    storageDriverUnlock(driver);

2134
    if (!pool) {
2135
        virReportError(VIR_ERR_NO_STORAGE_POOL,
2136 2137
                       _("no storage pool with matching name '%s'"),
                       obj->pool);
2138
        goto cleanup;
2139 2140 2141
    }

    if (!virStoragePoolObjIsActive(pool)) {
2142
        virReportError(VIR_ERR_OPERATION_INVALID,
2143
                       _("storage pool '%s' is not active"), pool->def->name);
2144
        goto cleanup;
2145 2146 2147
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2148
        goto cleanup;
2149 2150 2151 2152

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

    if (!vol) {
2153 2154 2155
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       obj->name);
2156
        goto cleanup;
2157 2158
    }

2159
    if (vol->building) {
2160 2161 2162
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume '%s' is still being allocated."),
                       vol->name);
2163 2164 2165
        goto cleanup;
    }

2166
    if (!backend->deleteVol) {
2167 2168
        virReportError(VIR_ERR_NO_SUPPORT,
                       "%s", _("storage pool does not support vol deletion"));
2169

2170
        goto cleanup;
2171 2172
    }

2173 2174 2175
    if (backend->deleteVol(obj->conn, pool, vol, flags) < 0)
        goto cleanup;

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

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

2192 2193 2194
            break;
        }
    }
2195
    ret = 0;
2196

2197
cleanup:
2198 2199
    if (pool)
        virStoragePoolObjUnlock(pool);
2200
    return ret;
2201 2202 2203 2204 2205
}

static int
storageVolumeGetInfo(virStorageVolPtr obj,
                     virStorageVolInfoPtr info) {
2206 2207
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2208 2209
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
2210
    int ret = -1;
2211

2212
    storageDriverLock(driver);
2213
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2214 2215
    storageDriverUnlock(driver);

2216
    if (!pool) {
2217
        virReportError(VIR_ERR_NO_STORAGE_POOL,
2218 2219
                       _("no storage pool with matching name '%s'"),
                       obj->pool);
2220
        goto cleanup;
2221 2222 2223
    }

    if (!virStoragePoolObjIsActive(pool)) {
2224
        virReportError(VIR_ERR_OPERATION_INVALID,
2225
                       _("storage pool '%s' is not active"), pool->def->name);
2226
        goto cleanup;
2227 2228 2229 2230 2231
    }

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

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

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2239
        goto cleanup;
2240 2241 2242

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

    memset(info, 0, sizeof(*info));
2246
    info->type = vol->type;
2247 2248
    info->capacity = vol->capacity;
    info->allocation = vol->allocation;
2249
    ret = 0;
2250

2251
cleanup:
2252 2253
    if (pool)
        virStoragePoolObjUnlock(pool);
2254
    return ret;
2255 2256 2257 2258
}

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

E
Eric Blake 已提交
2267 2268
    virCheckFlags(0, NULL);

2269
    storageDriverLock(driver);
2270
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2271 2272
    storageDriverUnlock(driver);

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

    if (!virStoragePoolObjIsActive(pool)) {
2281
        virReportError(VIR_ERR_OPERATION_INVALID,
2282
                       _("storage pool '%s' is not active"), pool->def->name);
2283
        goto cleanup;
2284 2285 2286 2287 2288
    }

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

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

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

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

2302
    ret = virStorageVolDefFormat(pool->def, vol);
2303 2304

cleanup:
2305 2306 2307
    if (pool)
        virStoragePoolObjUnlock(pool);

2308
    return ret;
2309 2310 2311 2312
}

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

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

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

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

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

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

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

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

2370
static virStorageDriver storageDriver = {
2371
    .name = "storage",
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
    .connectOpen = storageOpen, /* 0.4.0 */
    .connectClose = storageClose, /* 0.4.0 */
    .connectNumOfStoragePools = storageNumPools, /* 0.4.0 */
    .connectListStoragePools = storageListPools, /* 0.4.0 */
    .connectNumOfDefinedStoragePools = storageNumDefinedPools, /* 0.4.0 */
    .connectListDefinedStoragePools = storageListDefinedPools, /* 0.4.0 */
    .connectListAllStoragePools = storageListAllPools, /* 0.10.2 */
    .connectFindStoragePoolSources = storageFindPoolSources, /* 0.4.0 */
    .storagePoolLookupByName = storagePoolLookupByName, /* 0.4.0 */
    .storagePoolLookupByUUID = storagePoolLookupByUUID, /* 0.4.0 */
    .storagePoolLookupByVolume = storagePoolLookupByVolume, /* 0.4.0 */
    .storagePoolCreateXML = storagePoolCreate, /* 0.4.0 */
    .storagePoolDefineXML = storagePoolDefine, /* 0.4.0 */
    .storagePoolBuild = storagePoolBuild, /* 0.4.0 */
    .storagePoolUndefine = storagePoolUndefine, /* 0.4.0 */
    .storagePoolCreate = storagePoolStart, /* 0.4.0 */
    .storagePoolDestroy = storagePoolDestroy, /* 0.4.0 */
    .storagePoolDelete = storagePoolDelete, /* 0.4.0 */
    .storagePoolRefresh = storagePoolRefresh, /* 0.4.0 */
    .storagePoolGetInfo = storagePoolGetInfo, /* 0.4.0 */
    .storagePoolGetXMLDesc = storagePoolGetXMLDesc, /* 0.4.0 */
    .storagePoolGetAutostart = storagePoolGetAutostart, /* 0.4.0 */
    .storagePoolSetAutostart = storagePoolSetAutostart, /* 0.4.0 */
    .storagePoolNumOfVolumes = storagePoolNumVolumes, /* 0.4.0 */
    .storagePoolListVolumes = storagePoolListVolumes, /* 0.4.0 */
    .storagePoolListAllVolumes = storagePoolListAllVolumes, /* 0.10.2 */

    .storageVolLookupByName = storageVolumeLookupByName, /* 0.4.0 */
    .storageVolLookupByKey = storageVolumeLookupByKey, /* 0.4.0 */
    .storageVolLookupByPath = storageVolumeLookupByPath, /* 0.4.0 */
    .storageVolCreateXML = storageVolumeCreateXML, /* 0.4.0 */
    .storageVolCreateXMLFrom = storageVolumeCreateXMLFrom, /* 0.6.4 */
    .storageVolDownload = storageVolumeDownload, /* 0.9.0 */
    .storageVolUpload = storageVolumeUpload, /* 0.9.0 */
    .storageVolDelete = storageVolumeDelete, /* 0.4.0 */
    .storageVolWipe = storageVolumeWipe, /* 0.8.0 */
    .storageVolWipePattern = storageVolumeWipePattern, /* 0.9.10 */
    .storageVolGetInfo = storageVolumeGetInfo, /* 0.4.0 */
    .storageVolGetXMLDesc = storageVolumeGetXMLDesc, /* 0.4.0 */
    .storageVolGetPath = storageVolumeGetPath, /* 0.4.0 */
    .storageVolResize = storageVolumeResize, /* 0.9.10 */

    .storagePoolIsActive = storagePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = storagePoolIsPersistent, /* 0.7.3 */
2416 2417 2418 2419
};


static virStateDriver stateDriver = {
2420
    .name = "Storage",
2421 2422 2423
    .stateInitialize = storageDriverStartup,
    .stateCleanup = storageDriverShutdown,
    .stateReload = storageDriverReload,
2424 2425 2426 2427 2428 2429 2430
};

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