storage_driver.c 61.5 KB
Newer Older
1 2 3
/*
 * storage_driver.c: core driver for storage APIs
 *
4
 * Copyright (C) 2006-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * Copyright (C) 2006-2008 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
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
        uid_t uid = geteuid();
149
        char *userdir = virGetUserDirectory(uid);
150 151 152

        if (!userdir)
            goto error;
153

154 155
        if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) {
            VIR_FREE(userdir);
156 157
            goto out_of_memory;
        }
158
        VIR_FREE(userdir);
159 160 161 162 163
    }

    /* Configuration paths are either ~/.libvirt/storage/... (session) or
     * /etc/libvirt/storage/... (system).
     */
164 165
    if (virAsprintf(&driverState->configDir,
                    "%s/storage", base) == -1)
166 167
        goto out_of_memory;

168 169
    if (virAsprintf(&driverState->autostartDir,
                    "%s/storage/autostart", base) == -1)
170 171
        goto out_of_memory;

172
    VIR_FREE(base);
173

174
    if (virStoragePoolLoadAllConfigs(&driverState->pools,
175
                                     driverState->configDir,
176 177
                                     driverState->autostartDir) < 0)
        goto error;
178 179
    storageDriverAutostart(driverState);

180
    storageDriverUnlock(driverState);
181 182
    return 0;

183
out_of_memory:
184
    virReportOOMError();
185 186 187 188
error:
    VIR_FREE(base);
    storageDriverUnlock(driverState);
    storageDriverShutdown();
189 190 191 192 193 194 195 196 197 198 199
    return -1;
}

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

203
    storageDriverLock(driverState);
204
    virStoragePoolLoadAllConfigs(&driverState->pools,
205 206
                                 driverState->configDir,
                                 driverState->autostartDir);
207
    storageDriverAutostart(driverState);
208
    storageDriverUnlock(driverState);
209 210 211 212 213 214 215 216 217 218 219 220 221

    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) {
222
    unsigned int i;
223
    int active = 0;
224 225 226 227

    if (!driverState)
        return 0;

228 229 230 231
    storageDriverLock(driverState);

    for (i = 0 ; i < driverState->pools.count ; i++) {
        virStoragePoolObjLock(driverState->pools.objs[i]);
232
        if (virStoragePoolObjIsActive(driverState->pools.objs[i]))
233 234 235
            active = 1;
        virStoragePoolObjUnlock(driverState->pools.objs[i]);
    }
236

237 238
    storageDriverUnlock(driverState);
    return active;
239 240 241 242 243 244 245 246 247 248 249 250
}

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

251
    storageDriverLock(driverState);
252 253

    /* free inactive pools */
254 255 256 257
    virStoragePoolObjListFree(&driverState->pools);

    VIR_FREE(driverState->configDir);
    VIR_FREE(driverState->autostartDir);
258
    storageDriverUnlock(driverState);
259
    virMutexDestroy(&driverState->lock);
260
    VIR_FREE(driverState);
261 262 263 264 265 266 267 268 269

    return 0;
}



static virStoragePoolPtr
storagePoolLookupByUUID(virConnectPtr conn,
                        const unsigned char *uuid) {
270 271 272
    virStorageDriverStatePtr driver = conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
273

274
    storageDriverLock(driver);
275
    pool = virStoragePoolObjFindByUUID(&driver->pools, uuid);
276 277
    storageDriverUnlock(driver);

278
    if (!pool) {
279
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
280
                              "%s", _("no pool with matching uuid"));
281
        goto cleanup;
282 283 284
    }

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

cleanup:
287 288
    if (pool)
        virStoragePoolObjUnlock(pool);
289 290 291 292 293 294
    return ret;
}

static virStoragePoolPtr
storagePoolLookupByName(virConnectPtr conn,
                        const char *name) {
295 296 297
    virStorageDriverStatePtr driver = conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
298

299
    storageDriverLock(driver);
300
    pool = virStoragePoolObjFindByName(&driver->pools, name);
301 302
    storageDriverUnlock(driver);

303
    if (!pool) {
304
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
305
                              _("no pool with matching name '%s'"), name);
306
        goto cleanup;
307 308 309
    }

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

cleanup:
312 313
    if (pool)
        virStoragePoolObjUnlock(pool);
314 315 316 317 318 319 320 321 322 323 324
    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 已提交
325 326 327 328
            unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    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) {
344
    virStorageDriverStatePtr driver = conn->storagePrivateData;
345 346
    unsigned int i, nactive = 0;

347 348 349
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
350 351
        if (virStoragePoolObjIsActive(driver->pools.objs[i]))
            nactive++;
352 353 354
        virStoragePoolObjUnlock(driver->pools.objs[i]);
    }
    storageDriverUnlock(driver);
355 356

    return nactive;
357 358 359 360 361 362
}

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

366
    storageDriverLock(driver);
367
    for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
368
        virStoragePoolObjLock(driver->pools.objs[i]);
369 370
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
371
                virStoragePoolObjUnlock(driver->pools.objs[i]);
372
                virReportOOMError();
373 374 375 376
                goto cleanup;
            }
            got++;
        }
377
        virStoragePoolObjUnlock(driver->pools.objs[i]);
378
    }
379
    storageDriverUnlock(driver);
380 381 382
    return got;

 cleanup:
383 384 385
    storageDriverUnlock(driver);
    for (i = 0 ; i < got ; i++)
        VIR_FREE(names[i]);
386
    memset(names, 0, nnames * sizeof(*names));
387 388 389 390 391
    return -1;
}

static int
storageNumDefinedPools(virConnectPtr conn) {
392
    virStorageDriverStatePtr driver = conn->storagePrivateData;
393 394
    unsigned int i, nactive = 0;

395 396 397
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
398 399
        if (!virStoragePoolObjIsActive(driver->pools.objs[i]))
            nactive++;
400 401 402
        virStoragePoolObjUnlock(driver->pools.objs[i]);
    }
    storageDriverUnlock(driver);
403 404

    return nactive;
405 406 407 408 409 410
}

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

414
    storageDriverLock(driver);
415
    for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
416
        virStoragePoolObjLock(driver->pools.objs[i]);
417 418
        if (!virStoragePoolObjIsActive(driver->pools.objs[i])) {
            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
419
                virStoragePoolObjUnlock(driver->pools.objs[i]);
420
                virReportOOMError();
421 422 423 424
                goto cleanup;
            }
            got++;
        }
425
        virStoragePoolObjUnlock(driver->pools.objs[i]);
426
    }
427
    storageDriverUnlock(driver);
428 429 430
    return got;

 cleanup:
431
    storageDriverUnlock(driver);
432
    for (i = 0 ; i < got ; i++) {
433
        VIR_FREE(names[i]);
434
    }
435
    memset(names, 0, nnames * sizeof(*names));
436 437 438
    return -1;
}

439 440
/* This method is required to be re-entrant / thread safe, so
   uses no driver lock */
441 442 443 444 445 446 447 448
static char *
storageFindPoolSources(virConnectPtr conn,
                       const char *type,
                       const char *srcSpec,
                       unsigned int flags)
{
    int backend_type;
    virStorageBackendPtr backend;
449
    char *ret = NULL;
450

451
    backend_type = virStoragePoolTypeFromString(type);
452
    if (backend_type < 0) {
453
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
454
                              _("unknown storage pool type %s"), type);
455
        goto cleanup;
456
    }
457 458 459

    backend = virStorageBackendForType(backend_type);
    if (backend == NULL)
460
        goto cleanup;
461

462
    if (!backend->findPoolSources) {
463
        virStorageReportError(VIR_ERR_NO_SUPPORT,
464 465 466 467 468 469
                              _("pool type '%s' does not support source "
                                "discovery"), type);
        goto cleanup;
    }

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

471 472
cleanup:
    return ret;
473 474 475
}


476
static int storagePoolIsActive(virStoragePoolPtr pool)
477
{
478
    virStorageDriverStatePtr driver = pool->conn->storagePrivateData;
479 480 481 482
    virStoragePoolObjPtr obj;
    int ret = -1;

    storageDriverLock(driver);
483
    obj = virStoragePoolObjFindByUUID(&driver->pools, pool->uuid);
484 485
    storageDriverUnlock(driver);
    if (!obj) {
486
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
487 488 489 490 491 492 493 494 495 496
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

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

497
static int storagePoolIsPersistent(virStoragePoolPtr pool)
498
{
499
    virStorageDriverStatePtr driver = pool->conn->storagePrivateData;
500 501 502 503
    virStoragePoolObjPtr obj;
    int ret = -1;

    storageDriverLock(driver);
504
    obj = virStoragePoolObjFindByUUID(&driver->pools, pool->uuid);
505 506
    storageDriverUnlock(driver);
    if (!obj) {
507
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
508 509 510 511 512 513 514 515 516 517 518
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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


519 520 521
static virStoragePoolPtr
storagePoolCreate(virConnectPtr conn,
                  const char *xml,
E
Eric Blake 已提交
522 523
                  unsigned int flags)
{
524
    virStorageDriverStatePtr driver = conn->storagePrivateData;
525
    virStoragePoolDefPtr def;
526
    virStoragePoolObjPtr pool = NULL;
527
    virStoragePoolPtr ret = NULL;
528 529
    virStorageBackendPtr backend;

E
Eric Blake 已提交
530 531
    virCheckFlags(0, NULL);

532
    storageDriverLock(driver);
533
    if (!(def = virStoragePoolDefParseString(xml)))
534
        goto cleanup;
535

536
    if (virStoragePoolObjIsDuplicate(&driver->pools, def, 1) < 0)
537
        goto cleanup;
538

539 540
    if ((backend = virStorageBackendForType(def->type)) == NULL)
        goto cleanup;
541

542
    if (!(pool = virStoragePoolObjAssignDef(&driver->pools, def)))
543 544
        goto cleanup;
    def = NULL;
545

546
    if (backend->startPool &&
547 548 549
        backend->startPool(conn, pool) < 0) {
        virStoragePoolObjRemove(&driver->pools, pool);
        pool = NULL;
550
        goto cleanup;
551
    }
552

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

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

565 566
cleanup:
    virStoragePoolDefFree(def);
567
    if (pool)
568
        virStoragePoolObjUnlock(pool);
569
    storageDriverUnlock(driver);
570 571 572 573 574 575
    return ret;
}

static virStoragePoolPtr
storagePoolDefine(virConnectPtr conn,
                  const char *xml,
E
Eric Blake 已提交
576 577
                  unsigned int flags)
{
578
    virStorageDriverStatePtr driver = conn->storagePrivateData;
579
    virStoragePoolDefPtr def;
580
    virStoragePoolObjPtr pool = NULL;
581
    virStoragePoolPtr ret = NULL;
582

E
Eric Blake 已提交
583 584
    virCheckFlags(0, NULL);

585
    storageDriverLock(driver);
586
    if (!(def = virStoragePoolDefParseString(xml)))
587
        goto cleanup;
588

589 590 591
    if (virStoragePoolObjIsDuplicate(&driver->pools, def, 0) < 0)
        goto cleanup;

592
    if (virStorageBackendForType(def->type) == NULL)
593
        goto cleanup;
594

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

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

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

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

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

622
    storageDriverLock(driver);
623
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
624
    if (!pool) {
625
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
626
                              "%s", _("no storage pool with matching uuid"));
627
        goto cleanup;
628 629 630
    }

    if (virStoragePoolObjIsActive(pool)) {
631
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
632
                              "%s", _("pool is still active"));
633
        goto cleanup;
634 635
    }

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

643
    if (virStoragePoolObjDeleteDef(pool) < 0)
644
        goto cleanup;
645

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

652 653
    VIR_FREE(pool->configFile);
    VIR_FREE(pool->autostartLink);
654

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

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

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

E
Eric Blake 已提交
676 677
    virCheckFlags(0, -1);

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

682
    if (!pool) {
683
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
684
                              "%s", _("no storage pool with matching uuid"));
685
        goto cleanup;
686 687
    }

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

    if (virStoragePoolObjIsActive(pool)) {
692
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
693
                              "%s", _("pool already active"));
694
        goto cleanup;
695 696 697
    }
    if (backend->startPool &&
        backend->startPool(obj->conn, pool) < 0)
698 699
        goto cleanup;

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

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

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

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

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

728
    if (!pool) {
729
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
730
                              "%s", _("no storage pool with matching uuid"));
731
        goto cleanup;
732 733
    }

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

    if (virStoragePoolObjIsActive(pool)) {
738
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
739
                              "%s", _("storage pool is already active"));
740
        goto cleanup;
741 742 743 744
    }

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

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


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

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

765
    if (!pool) {
766
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
767
                              "%s", _("no storage pool with matching uuid"));
768
        goto cleanup;
769 770
    }

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

    if (!virStoragePoolObjIsActive(pool)) {
775
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
776
                              "%s", _("storage pool is not active"));
777
        goto cleanup;
778 779
    }

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

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

    virStoragePoolObjClearVols(pool);

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

796
    if (pool->configFile == NULL) {
797
        virStoragePoolObjRemove(&driver->pools, pool);
798 799
        pool = NULL;
    }
800
    ret = 0;
801

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


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

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

822
    if (!pool) {
823
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
824
                              "%s", _("no storage pool with matching uuid"));
825
        goto cleanup;
826 827
    }

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

    if (virStoragePoolObjIsActive(pool)) {
832
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
833
                              "%s", _("storage pool is still active"));
834
        goto cleanup;
835 836
    }

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

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

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


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

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

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

875
    if (!pool) {
876
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
877
                              "%s", _("no storage pool with matching uuid"));
878
        goto cleanup;
879 880
    }

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

    if (!virStoragePoolObjIsActive(pool)) {
885
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
886
                              "%s", _("storage pool is not active"));
887
        goto cleanup;
888 889
    }

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

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

        pool->active = 0;

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

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


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

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

931
    if (!pool) {
932
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
933
                              "%s", _("no storage pool with matching uuid"));
934
        goto cleanup;
935 936
    }

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

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

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

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

E
Eric Blake 已提交
964 965
    virCheckFlags(0, NULL);

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

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

976
    ret = virStoragePoolDefFormat(pool->def);
977 978

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

static int
storagePoolGetAutostart(virStoragePoolPtr obj,
                        int *autostart) {
987 988 989
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
990

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

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

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

1008
cleanup:
1009 1010
    if (pool)
        virStoragePoolObjUnlock(pool);
1011
    return ret;
1012 1013 1014 1015 1016
}

static int
storagePoolSetAutostart(virStoragePoolPtr obj,
                        int autostart) {
1017 1018 1019
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
1020

1021
    storageDriverLock(driver);
1022
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1023

1024
    if (!pool) {
1025
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1026
                              "%s", _("no pool with matching uuid"));
1027
        goto cleanup;
1028 1029 1030
    }

    if (!pool->configFile) {
1031
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1032
                              "%s", _("pool has no config file"));
1033
        goto cleanup;
1034 1035 1036 1037
    }

    autostart = (autostart != 0);

1038 1039
    if (pool->autostart != autostart) {
        if (autostart) {
1040 1041
            if (virFileMakePath(driver->autostartDir) < 0) {
                virReportSystemError(errno,
1042 1043
                                     _("cannot create autostart directory %s"),
                                     driver->autostartDir);
1044 1045
                goto cleanup;
            }
1046

1047
            if (symlink(pool->configFile, pool->autostartLink) < 0) {
1048
                virReportSystemError(errno,
1049 1050
                                     _("Failed to create symlink '%s' to '%s'"),
                                     pool->autostartLink, pool->configFile);
1051 1052 1053 1054 1055
                goto cleanup;
            }
        } else {
            if (unlink(pool->autostartLink) < 0 &&
                errno != ENOENT && errno != ENOTDIR) {
1056
                virReportSystemError(errno,
1057 1058
                                     _("Failed to delete symlink '%s'"),
                                     pool->autostartLink);
1059 1060
                goto cleanup;
            }
1061
        }
1062
        pool->autostart = autostart;
1063
    }
1064
    ret = 0;
1065

1066
cleanup:
1067 1068
    if (pool)
        virStoragePoolObjUnlock(pool);
1069
    storageDriverUnlock(driver);
1070
    return ret;
1071 1072 1073 1074 1075
}


static int
storagePoolNumVolumes(virStoragePoolPtr obj) {
1076 1077 1078
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
1079

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

1084
    if (!pool) {
1085
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1086
                              "%s", _("no storage pool with matching uuid"));
1087
        goto cleanup;
1088 1089 1090
    }

    if (!virStoragePoolObjIsActive(pool)) {
1091
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1092
                              "%s", _("storage pool is not active"));
1093
        goto cleanup;
1094
    }
1095
    ret = pool->volumes.count;
1096

1097
cleanup:
1098 1099
    if (pool)
        virStoragePoolObjUnlock(pool);
1100
    return ret;
1101 1102 1103 1104 1105 1106
}

static int
storagePoolListVolumes(virStoragePoolPtr obj,
                       char **const names,
                       int maxnames) {
1107 1108
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1109
    int i, n = 0;
1110

1111 1112
    memset(names, 0, maxnames * sizeof(*names));

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

1117
    if (!pool) {
1118
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1119
                              "%s", _("no storage pool with matching uuid"));
1120
        goto cleanup;
1121 1122 1123
    }

    if (!virStoragePoolObjIsActive(pool)) {
1124
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1125
                              "%s", _("storage pool is not active"));
1126
        goto cleanup;
1127 1128
    }

1129 1130
    for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) {
1131
            virReportOOMError();
1132 1133 1134 1135
            goto cleanup;
        }
    }

1136
    virStoragePoolObjUnlock(pool);
1137
    return n;
1138 1139

 cleanup:
1140 1141
    if (pool)
        virStoragePoolObjUnlock(pool);
1142
    for (n = 0 ; n < maxnames ; n++)
1143
        VIR_FREE(names[n]);
1144

1145
    memset(names, 0, maxnames * sizeof(*names));
1146 1147 1148 1149 1150 1151 1152
    return -1;
}


static virStorageVolPtr
storageVolumeLookupByName(virStoragePoolPtr obj,
                          const char *name) {
1153 1154
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1155
    virStorageVolDefPtr vol;
1156
    virStorageVolPtr ret = NULL;
1157

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

1162
    if (!pool) {
1163
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1164
                              "%s", _("no storage pool with matching uuid"));
1165
        goto cleanup;
1166 1167 1168
    }

    if (!virStoragePoolObjIsActive(pool)) {
1169
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1170
                              "%s", _("storage pool is not active"));
1171
        goto cleanup;
1172 1173 1174 1175 1176
    }

    vol = virStorageVolDefFindByName(pool, name);

    if (!vol) {
1177 1178
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
1179
                              name);
1180
        goto cleanup;
1181 1182
    }

1183 1184 1185
    ret = virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key);

cleanup:
1186 1187
    if (pool)
        virStoragePoolObjUnlock(pool);
1188
    return ret;
1189 1190 1191 1192 1193 1194
}


static virStorageVolPtr
storageVolumeLookupByKey(virConnectPtr conn,
                         const char *key) {
1195
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1196
    unsigned int i;
1197
    virStorageVolPtr ret = NULL;
1198

1199 1200 1201
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1202 1203 1204
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
            virStorageVolDefPtr vol =
                virStorageVolDefFindByKey(driver->pools.objs[i], key);
1205

1206
            if (vol)
1207
                ret = virGetStorageVol(conn,
1208 1209 1210
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1211
        }
1212
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1213
    }
1214
    storageDriverUnlock(driver);
1215

1216
    if (!ret)
1217
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
1218 1219 1220
                              "%s", _("no storage vol with matching key"));

    return ret;
1221 1222 1223 1224 1225
}

static virStorageVolPtr
storageVolumeLookupByPath(virConnectPtr conn,
                          const char *path) {
1226
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1227
    unsigned int i;
1228
    virStorageVolPtr ret = NULL;
1229 1230 1231 1232 1233
    char *cleanpath;

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

1235 1236 1237
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1238
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
1239
            virStorageVolDefPtr vol;
1240 1241
            const char *stable_path;

1242
            stable_path = virStorageBackendStablePath(driver->pools.objs[i],
1243
                                                      cleanpath);
1244 1245 1246 1247 1248
            /*
             * virStorageBackendStablePath already does
             * virStorageReportError if it fails; we just need to keep
             * propagating the return code
             */
1249 1250
            if (stable_path == NULL) {
                virStoragePoolObjUnlock(driver->pools.objs[i]);
1251
                goto cleanup;
1252
            }
1253 1254 1255 1256

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

1258
            if (vol)
1259 1260 1261 1262
                ret = virGetStorageVol(conn,
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1263
        }
1264
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1265 1266
    }

1267
    if (!ret)
1268
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
1269 1270 1271
                              "%s", _("no storage vol with matching path"));

cleanup:
1272
    VIR_FREE(cleanpath);
1273
    storageDriverUnlock(driver);
1274
    return ret;
1275 1276
}

1277 1278
static int storageVolumeDelete(virStorageVolPtr obj, unsigned int flags);

1279 1280 1281
static virStorageVolPtr
storageVolumeCreateXML(virStoragePoolPtr obj,
                       const char *xmldesc,
E
Eric Blake 已提交
1282 1283
                       unsigned int flags)
{
1284 1285
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1286
    virStorageBackendPtr backend;
1287 1288
    virStorageVolDefPtr voldef = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1289

E
Eric Blake 已提交
1290 1291
    virCheckFlags(0, NULL);

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

1296
    if (!pool) {
1297
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1298
                              "%s", _("no storage pool with matching uuid"));
1299
        goto cleanup;
1300 1301 1302
    }

    if (!virStoragePoolObjIsActive(pool)) {
1303
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1304
                              "%s", _("storage pool is not active"));
1305
        goto cleanup;
1306 1307 1308
    }

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

1311
    voldef = virStorageVolDefParseString(pool->def, xmldesc);
1312
    if (voldef == NULL)
1313
        goto cleanup;
1314

1315
    if (virStorageVolDefFindByName(pool, voldef->name)) {
1316
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
J
Jim Meyering 已提交
1317
                              "%s", _("storage vol already exists"));
1318
        goto cleanup;
1319 1320
    }

1321 1322
    if (VIR_REALLOC_N(pool->volumes.objs,
                      pool->volumes.count+1) < 0) {
1323
        virReportOOMError();
1324
        goto cleanup;
1325 1326
    }

1327
    if (!backend->createVol) {
1328
        virStorageReportError(VIR_ERR_NO_SUPPORT,
1329 1330
                              "%s", _("storage pool does not support volume "
                                      "creation"));
1331
        goto cleanup;
1332 1333
    }

1334
    if (backend->createVol(obj->conn, pool, voldef) < 0) {
1335
        goto cleanup;
1336 1337
    }

1338 1339 1340
    pool->volumes.objs[pool->volumes.count++] = voldef;
    volobj = virGetStorageVol(obj->conn, pool->def->name, voldef->name,
                              voldef->key);
1341 1342 1343 1344
    if (!volobj) {
        pool->volumes.count--;
        goto cleanup;
    }
1345

1346
    if (backend->buildVol) {
1347 1348 1349 1350
        int buildret;
        virStorageVolDefPtr buildvoldef = NULL;

        if (VIR_ALLOC(buildvoldef) < 0) {
1351
            virReportOOMError();
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
            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);

1367
        buildret = backend->buildVol(obj->conn, pool, buildvoldef);
1368

1369
        storageDriverLock(driver);
1370
        virStoragePoolObjLock(pool);
1371 1372
        storageDriverUnlock(driver);

1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
        voldef->building = 0;
        pool->asyncjobs--;

        voldef = NULL;
        VIR_FREE(buildvoldef);

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

    }

1388
    VIR_INFO("Creating volume '%s' in storage pool '%s'",
1389
             volobj->name, pool->def->name);
1390 1391 1392
    ret = volobj;
    volobj = NULL;
    voldef = NULL;
1393

1394
cleanup:
1395 1396 1397
    if (volobj)
        virUnrefStorageVol(volobj);
    virStorageVolDefFree(voldef);
1398 1399
    if (pool)
        virStoragePoolObjUnlock(pool);
1400
    return ret;
1401 1402
}

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

E
Eric Blake 已提交
1416 1417
    virCheckFlags(0, NULL);

1418 1419
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1420
    if (pool && STRNEQ(obj->name, vobj->pool)) {
1421
        virStoragePoolObjUnlock(pool);
1422
        origpool = virStoragePoolObjFindByName(&driver->pools, vobj->pool);
1423
        virStoragePoolObjLock(pool);
1424
    }
1425 1426
    storageDriverUnlock(driver);
    if (!pool) {
1427
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
1428 1429 1430 1431
                              "%s", _("no storage pool with matching uuid"));
        goto cleanup;
    }

1432
    if (STRNEQ(obj->name, vobj->pool) && !origpool) {
1433 1434
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
                              _("no storage pool with matching name '%s'"),
1435
                              vobj->pool);
1436 1437 1438 1439
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1440
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1441 1442 1443 1444
                              "%s", _("storage pool is not active"));
        goto cleanup;
    }

1445
    if (origpool && !virStoragePoolObjIsActive(origpool)) {
1446
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1447 1448 1449 1450 1451 1452 1453
                              "%s", _("storage pool is not active"));
        goto cleanup;
    }

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

1454
    origvol = virStorageVolDefFindByName(origpool ? origpool : pool, vobj->name);
1455
    if (!origvol) {
1456 1457
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
1458
                              vobj->name);
1459 1460 1461
        goto cleanup;
    }

1462
    newvol = virStorageVolDefParseString(pool->def, xmldesc);
1463 1464 1465 1466
    if (newvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(pool, newvol->name)) {
1467
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1468 1469 1470 1471 1472 1473 1474 1475 1476
                              _("storage volume name '%s' already in use."),
                              newvol->name);
        goto cleanup;
    }

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

1477 1478 1479 1480 1481
    /* 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;

1482
    if (!backend->buildVolFrom) {
1483
        virStorageReportError(VIR_ERR_NO_SUPPORT,
1484 1485 1486 1487 1488
                              "%s", _("storage pool does not support volume creation from an existing volume"));
        goto cleanup;
    }

    if (origvol->building) {
1489
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
                              _("volume '%s' is still being allocated."),
                              origvol->name);
        goto cleanup;
    }

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

    if (VIR_REALLOC_N(pool->volumes.objs,
                      pool->volumes.count+1) < 0) {
1501
        virReportOOMError();
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
        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);

1520
    if (origpool) {
1521 1522 1523 1524
        origpool->asyncjobs++;
        virStoragePoolObjUnlock(origpool);
    }

1525
    buildret = backend->buildVolFrom(obj->conn, pool, newvol, origvol, flags);
1526 1527 1528

    storageDriverLock(driver);
    virStoragePoolObjLock(pool);
1529
    if (origpool)
1530 1531 1532 1533 1534 1535 1536 1537
        virStoragePoolObjLock(origpool);
    storageDriverUnlock(driver);

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

1538
    if (origpool) {
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
        origpool->asyncjobs--;
        virStoragePoolObjUnlock(origpool);
        origpool = NULL;
    }

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

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

cleanup:
    if (volobj)
        virUnrefStorageVol(volobj);
    virStorageVolDefFree(newvol);
    if (pool)
        virStoragePoolObjUnlock(pool);
1562
    if (origpool)
1563 1564 1565 1566
        virStoragePoolObjUnlock(origpool);
    return ret;
}

1567

1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
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) {
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
                              "%s", _("no storage pool with matching uuid"));
        goto out;
    }

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

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

    if (vol == NULL) {
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                             _("no storage vol with matching name '%s'"),
                              obj->name);
        goto out;
    }

    if (vol->building) {
1608
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1609 1610 1611 1612 1613 1614 1615 1616
                              _("volume '%s' is still being allocated."),
                              vol->name);
        goto out;
    }

    if (virFDStreamOpenFile(stream,
                            vol->target.path,
                            offset, length,
1617
                            O_RDONLY, false) < 0)
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
        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) {
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
                              "%s", _("no storage pool with matching uuid"));
        goto out;
    }

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

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

    if (vol == NULL) {
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                             _("no storage vol with matching name '%s'"),
                              obj->name);
        goto out;
    }

    if (vol->building) {
1670
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
                              _("volume '%s' is still being allocated."),
                              vol->name);
        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,
1681
                            O_WRONLY, false) < 0)
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
        goto out;

    ret = 0;

out:
    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}



1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
/* 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 "
1729
                               "path '%s' to %ju bytes"),
E
Eric Blake 已提交
1730
                             vol->target.path, (uintmax_t)size);
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
    }

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 已提交
1752
              (uintmax_t)extent_start, (uintmax_t)extent_length);
1753 1754 1755 1756 1757

    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 已提交
1758
                             (uintmax_t)extent_start, vol->target.path);
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
        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;
    }

    VIR_DEBUG("Wrote %zu bytes to volume with path '%s'",
              *bytes_wiped, vol->target.path);

    ret = 0;

out:
    return ret;
}


static int
storageVolumeWipeInternal(virStorageVolDefPtr def)
{
    int ret = -1, fd = -1;
    struct stat st;
    char *writebuf = NULL;
    size_t bytes_wiped = 0;

    VIR_DEBUG("Wiping volume with path '%s'", def->target.path);

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

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

out:
    VIR_FREE(writebuf);

1836
    VIR_FORCE_CLOSE(fd);
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850

    return ret;
}


static int
storageVolumeWipe(virStorageVolPtr obj,
                  unsigned int flags)
{
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool = NULL;
    virStorageVolDefPtr vol = NULL;
    int ret = -1;

1851
    virCheckFlags(0, -1);
1852 1853 1854 1855 1856 1857

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

    if (!pool) {
1858
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
1859 1860 1861 1862 1863
                              "%s", _("no storage pool with matching uuid"));
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1864
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
                              "%s", _("storage pool is not active"));
        goto out;
    }

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

    if (vol == NULL) {
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                             _("no storage vol with matching name '%s'"),
                              obj->name);
        goto out;
    }

    if (vol->building) {
1879
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
                              _("volume '%s' is still being allocated."),
                              vol->name);
        goto out;
    }

    if (storageVolumeWipeInternal(vol) == -1) {
        goto out;
    }

    ret = 0;

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

    return ret;

}

1900 1901 1902
static int
storageVolumeDelete(virStorageVolPtr obj,
                    unsigned int flags) {
1903 1904
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1905
    virStorageBackendPtr backend;
1906
    virStorageVolDefPtr vol = NULL;
1907
    unsigned int i;
1908
    int ret = -1;
1909

1910
    storageDriverLock(driver);
1911
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
1912 1913
    storageDriverUnlock(driver);

1914
    if (!pool) {
1915
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1916
                              "%s", _("no storage pool with matching uuid"));
1917
        goto cleanup;
1918 1919 1920
    }

    if (!virStoragePoolObjIsActive(pool)) {
1921
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1922
                              "%s", _("storage pool is not active"));
1923
        goto cleanup;
1924 1925 1926
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
1927
        goto cleanup;
1928 1929 1930 1931

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

    if (!vol) {
1932
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
1933 1934
                             _("no storage vol with matching name '%s'"),
                              obj->name);
1935
        goto cleanup;
1936 1937
    }

1938
    if (vol->building) {
1939
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1940 1941 1942 1943 1944
                              _("volume '%s' is still being allocated."),
                              vol->name);
        goto cleanup;
    }

1945
    if (!backend->deleteVol) {
1946
        virStorageReportError(VIR_ERR_NO_SUPPORT,
J
Jim Meyering 已提交
1947
                              "%s", _("storage pool does not support vol deletion"));
1948

1949
        goto cleanup;
1950 1951
    }

1952 1953 1954
    if (backend->deleteVol(obj->conn, pool, vol, flags) < 0)
        goto cleanup;

1955 1956
    for (i = 0 ; i < pool->volumes.count ; i++) {
        if (pool->volumes.objs[i] == vol) {
1957
            VIR_INFO("Deleting volume '%s' from storage pool '%s'",
1958
                     vol->name, pool->def->name);
1959
            virStorageVolDefFree(vol);
1960
            vol = NULL;
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970

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

1971 1972 1973
            break;
        }
    }
1974
    ret = 0;
1975

1976
cleanup:
1977 1978
    if (pool)
        virStoragePoolObjUnlock(pool);
1979
    return ret;
1980 1981 1982 1983 1984
}

static int
storageVolumeGetInfo(virStorageVolPtr obj,
                     virStorageVolInfoPtr info) {
1985 1986
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1987 1988
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
1989
    int ret = -1;
1990

1991
    storageDriverLock(driver);
1992
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
1993 1994
    storageDriverUnlock(driver);

1995
    if (!pool) {
1996
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1997
                              "%s", _("no storage pool with matching uuid"));
1998
        goto cleanup;
1999 2000 2001
    }

    if (!virStoragePoolObjIsActive(pool)) {
2002
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
2003
                              "%s", _("storage pool is not active"));
2004
        goto cleanup;
2005 2006 2007 2008 2009
    }

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

    if (!vol) {
2010 2011
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
2012
                              obj->name);
2013
        goto cleanup;
2014 2015 2016
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2017
        goto cleanup;
2018 2019 2020

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

    memset(info, 0, sizeof(*info));
2024
    info->type = vol->type;
2025 2026
    info->capacity = vol->capacity;
    info->allocation = vol->allocation;
2027
    ret = 0;
2028

2029
cleanup:
2030 2031
    if (pool)
        virStoragePoolObjUnlock(pool);
2032
    return ret;
2033 2034 2035 2036
}

static char *
storageVolumeGetXMLDesc(virStorageVolPtr obj,
E
Eric Blake 已提交
2037 2038
                        unsigned int flags)
{
2039 2040
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2041 2042
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
2043
    char *ret = NULL;
2044

E
Eric Blake 已提交
2045 2046
    virCheckFlags(0, NULL);

2047
    storageDriverLock(driver);
2048
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2049 2050
    storageDriverUnlock(driver);

2051
    if (!pool) {
2052
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
2053
                              "%s", _("no storage pool with matching uuid"));
2054
        goto cleanup;
2055 2056 2057
    }

    if (!virStoragePoolObjIsActive(pool)) {
2058
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
2059
                              "%s", _("storage pool is not active"));
2060
        goto cleanup;
2061 2062 2063 2064 2065
    }

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

    if (!vol) {
2066
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
2067 2068
                             _("no storage vol with matching name '%s'"),
                              obj->name);
2069
        goto cleanup;
2070 2071 2072
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2073
        goto cleanup;
2074 2075 2076 2077

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

2079
    ret = virStorageVolDefFormat(pool->def, vol);
2080 2081

cleanup:
2082 2083 2084
    if (pool)
        virStoragePoolObjUnlock(pool);

2085
    return ret;
2086 2087 2088 2089
}

static char *
storageVolumeGetPath(virStorageVolPtr obj) {
2090
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
2091
    virStoragePoolObjPtr pool;
2092
    virStorageVolDefPtr vol;
2093
    char *ret = NULL;
2094

2095 2096 2097
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);
2098
    if (!pool) {
2099
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
2100
                              "%s", _("no storage pool with matching uuid"));
2101
        goto cleanup;
2102 2103 2104
    }

    if (!virStoragePoolObjIsActive(pool)) {
2105
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
2106
                              "%s", _("storage pool is not active"));
2107
        goto cleanup;
2108 2109 2110 2111 2112
    }

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

    if (!vol) {
2113 2114
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
2115
                              obj->name);
2116
        goto cleanup;
2117 2118 2119
    }

    ret = strdup(vol->target.path);
2120
    if (ret == NULL)
2121
        virReportOOMError();
2122 2123

cleanup:
2124 2125
    if (pool)
        virStoragePoolObjUnlock(pool);
2126 2127 2128 2129
    return ret;
}

static virStorageDriver storageDriver = {
2130
    .name = "storage",
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
    .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 */
    .findPoolSources = storageFindPoolSources, /* 0.4.0 */
    .poolLookupByName = storagePoolLookupByName, /* 0.4.0 */
    .poolLookupByUUID = storagePoolLookupByUUID, /* 0.4.0 */
    .poolLookupByVolume = storagePoolLookupByVolume, /* 0.4.0 */
    .poolCreateXML = storagePoolCreate, /* 0.4.0 */
    .poolDefineXML = storagePoolDefine, /* 0.4.0 */
    .poolBuild = storagePoolBuild, /* 0.4.0 */
    .poolUndefine = storagePoolUndefine, /* 0.4.0 */
    .poolCreate = storagePoolStart, /* 0.4.0 */
    .poolDestroy = storagePoolDestroy, /* 0.4.0 */
    .poolDelete = storagePoolDelete, /* 0.4.0 */
    .poolRefresh = storagePoolRefresh, /* 0.4.0 */
    .poolGetInfo = storagePoolGetInfo, /* 0.4.0 */
    .poolGetXMLDesc = storagePoolGetXMLDesc, /* 0.4.0 */
    .poolGetAutostart = storagePoolGetAutostart, /* 0.4.0 */
    .poolSetAutostart = storagePoolSetAutostart, /* 0.4.0 */
    .poolNumOfVolumes = storagePoolNumVolumes, /* 0.4.0 */
    .poolListVolumes = storagePoolListVolumes, /* 0.4.0 */

    .volLookupByName = storageVolumeLookupByName, /* 0.4.0 */
    .volLookupByKey = storageVolumeLookupByKey, /* 0.4.0 */
    .volLookupByPath = storageVolumeLookupByPath, /* 0.4.0 */
    .volCreateXML = storageVolumeCreateXML, /* 0.4.0 */
    .volCreateXMLFrom = storageVolumeCreateXMLFrom, /* 0.6.4 */
    .volDownload = storageVolumeDownload, /* 0.9.0 */
    .volUpload = storageVolumeUpload, /* 0.9.0 */
    .volDelete = storageVolumeDelete, /* 0.4.0 */
    .volWipe = storageVolumeWipe, /* 0.8.0 */
    .volGetInfo = storageVolumeGetInfo, /* 0.4.0 */
    .volGetXMLDesc = storageVolumeGetXMLDesc, /* 0.4.0 */
    .volGetPath = storageVolumeGetPath, /* 0.4.0 */

    .poolIsActive = storagePoolIsActive, /* 0.7.3 */
    .poolIsPersistent = storagePoolIsPersistent, /* 0.7.3 */
2171 2172 2173 2174
};


static virStateDriver stateDriver = {
2175
    .name = "Storage",
2176 2177 2178 2179
    .initialize = storageDriverStartup,
    .cleanup = storageDriverShutdown,
    .reload = storageDriverReload,
    .active = storageDriverActive,
2180 2181 2182 2183 2184 2185 2186
};

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