storage_driver.c 60.8 KB
Newer Older
1 2 3
/*
 * storage_driver.c: core driver for storage APIs
 *
4
 * Copyright (C) 2006-2010 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"
48
#include "files.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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    return ret;
}

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

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

    conn->storagePrivateData = driverState;
    return VIR_DRV_OPEN_SUCCESS;
}

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

static int
storageNumPools(virConnectPtr conn) {
341
    virStorageDriverStatePtr driver = conn->storagePrivateData;
342 343
    unsigned int i, nactive = 0;

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

    return nactive;
354 355 356 357 358 359
}

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

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

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

static int
storageNumDefinedPools(virConnectPtr conn) {
389
    virStorageDriverStatePtr driver = conn->storagePrivateData;
390 391
    unsigned int i, nactive = 0;

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

    return nactive;
402 403 404 405 406 407
}

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

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

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

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

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

    backend = virStorageBackendForType(backend_type);
    if (backend == NULL)
457
        goto cleanup;
458

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

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

468 469
cleanup:
    return ret;
470 471 472
}


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

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

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

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

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

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


516 517 518 519
static virStoragePoolPtr
storagePoolCreate(virConnectPtr conn,
                  const char *xml,
                  unsigned int flags ATTRIBUTE_UNUSED) {
520
    virStorageDriverStatePtr driver = conn->storagePrivateData;
521
    virStoragePoolDefPtr def;
522
    virStoragePoolObjPtr pool = NULL;
523
    virStoragePoolPtr ret = NULL;
524 525
    virStorageBackendPtr backend;

526
    storageDriverLock(driver);
527
    if (!(def = virStoragePoolDefParseString(xml)))
528
        goto cleanup;
529

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

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

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

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

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

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

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

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

576
    storageDriverLock(driver);
577
    if (!(def = virStoragePoolDefParseString(xml)))
578
        goto cleanup;
579

580 581 582
    if (virStoragePoolObjIsDuplicate(&driver->pools, def, 0) < 0)
        goto cleanup;

583
    if (virStorageBackendForType(def->type) == NULL)
584
        goto cleanup;
585

586
    if (!(pool = virStoragePoolObjAssignDef(&driver->pools, def)))
587
        goto cleanup;
588

589
    if (virStoragePoolObjSaveDef(driver, pool, def) < 0) {
590
        virStoragePoolObjRemove(&driver->pools, pool);
591
        def = NULL;
592
        goto cleanup;
593
    }
594
    def = NULL;
595

596
    VIR_INFO(_("Defining storage pool '%s'"), pool->def->name);
597
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
598 599 600

cleanup:
    virStoragePoolDefFree(def);
601 602 603
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
604 605 606 607 608
    return ret;
}

static int
storagePoolUndefine(virStoragePoolPtr obj) {
609 610 611
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
612

613
    storageDriverLock(driver);
614
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
615
    if (!pool) {
616
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
617
                              "%s", _("no storage pool with matching uuid"));
618
        goto cleanup;
619 620 621
    }

    if (virStoragePoolObjIsActive(pool)) {
622
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
623
                              "%s", _("pool is still active"));
624
        goto cleanup;
625 626
    }

627
    if (pool->asyncjobs > 0) {
628
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
629 630 631 632 633
                              _("pool '%s' has asynchronous jobs running."),
                              pool->def->name);
        goto cleanup;
    }

634
    if (virStoragePoolObjDeleteDef(pool) < 0)
635
        goto cleanup;
636

637 638
    if (unlink(pool->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
        char ebuf[1024];
639
        VIR_ERROR(_("Failed to delete autostart link '%s': %s"),
640 641
                   pool->autostartLink, virStrerror(errno, ebuf, sizeof ebuf));
    }
642

643 644
    VIR_FREE(pool->configFile);
    VIR_FREE(pool->autostartLink);
645

646
    VIR_INFO(_("Undefining storage pool '%s'"), pool->def->name);
647
    virStoragePoolObjRemove(&driver->pools, pool);
648
    pool = NULL;
649
    ret = 0;
650

651
cleanup:
652 653 654
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
655
    return ret;
656 657 658 659 660
}

static int
storagePoolStart(virStoragePoolPtr obj,
                 unsigned int flags ATTRIBUTE_UNUSED) {
661 662
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
663
    virStorageBackendPtr backend;
664
    int ret = -1;
665

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

670
    if (!pool) {
671
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
672
                              "%s", _("no storage pool with matching uuid"));
673
        goto cleanup;
674 675
    }

676 677
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
678 679

    if (virStoragePoolObjIsActive(pool)) {
680
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
681
                              "%s", _("pool already active"));
682
        goto cleanup;
683 684 685
    }
    if (backend->startPool &&
        backend->startPool(obj->conn, pool) < 0)
686 687
        goto cleanup;

688 689 690
    if (backend->refreshPool(obj->conn, pool) < 0) {
        if (backend->stopPool)
            backend->stopPool(obj->conn, pool);
691
        goto cleanup;
692 693
    }

694
    VIR_INFO(_("Starting up storage pool '%s'"), pool->def->name);
695
    pool->active = 1;
696
    ret = 0;
697

698
cleanup:
699 700
    if (pool)
        virStoragePoolObjUnlock(pool);
701
    return ret;
702 703 704 705 706
}

static int
storagePoolBuild(virStoragePoolPtr obj,
                 unsigned int flags) {
707 708
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
709
    virStorageBackendPtr backend;
710
    int ret = -1;
711

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

716
    if (!pool) {
717
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
718
                              "%s", _("no storage pool with matching uuid"));
719
        goto cleanup;
720 721
    }

722 723
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
724 725

    if (virStoragePoolObjIsActive(pool)) {
726
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
727
                              "%s", _("storage pool is already active"));
728
        goto cleanup;
729 730 731 732
    }

    if (backend->buildPool &&
        backend->buildPool(obj->conn, pool, flags) < 0)
733 734
        goto cleanup;
    ret = 0;
735

736
cleanup:
737 738
    if (pool)
        virStoragePoolObjUnlock(pool);
739
    return ret;
740 741 742 743 744
}


static int
storagePoolDestroy(virStoragePoolPtr obj) {
745 746
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
747
    virStorageBackendPtr backend;
748
    int ret = -1;
749

750
    storageDriverLock(driver);
751
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
752

753
    if (!pool) {
754
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
755
                              "%s", _("no storage pool with matching uuid"));
756
        goto cleanup;
757 758
    }

759 760
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
761 762

    if (!virStoragePoolObjIsActive(pool)) {
763
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
764
                              "%s", _("storage pool is not active"));
765
        goto cleanup;
766 767
    }

768
    if (pool->asyncjobs > 0) {
769
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
770 771 772 773 774
                              _("pool '%s' has asynchronous jobs running."),
                              pool->def->name);
        goto cleanup;
    }

775 776
    if (backend->stopPool &&
        backend->stopPool(obj->conn, pool) < 0)
777
        goto cleanup;
778 779 780 781

    virStoragePoolObjClearVols(pool);

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

784
    if (pool->configFile == NULL) {
785
        virStoragePoolObjRemove(&driver->pools, pool);
786 787
        pool = NULL;
    }
788
    ret = 0;
789

790
cleanup:
791 792 793
    if (pool)
        virStoragePoolObjUnlock(pool);
    storageDriverUnlock(driver);
794
    return ret;
795 796 797 798 799 800
}


static int
storagePoolDelete(virStoragePoolPtr obj,
                  unsigned int flags) {
801 802
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
803
    virStorageBackendPtr backend;
804
    int ret = -1;
805

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

810
    if (!pool) {
811
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
812
                              "%s", _("no storage pool with matching uuid"));
813
        goto cleanup;
814 815
    }

816 817
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
        goto cleanup;
818 819

    if (virStoragePoolObjIsActive(pool)) {
820
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
821
                              "%s", _("storage pool is still active"));
822
        goto cleanup;
823 824
    }

825
    if (pool->asyncjobs > 0) {
826
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
827 828 829 830 831
                              _("pool '%s' has asynchronous jobs running."),
                              pool->def->name);
        goto cleanup;
    }

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

842
cleanup:
843 844
    if (pool)
        virStoragePoolObjUnlock(pool);
845
    return ret;
846 847 848 849 850 851
}


static int
storagePoolRefresh(virStoragePoolPtr obj,
                   unsigned int flags ATTRIBUTE_UNUSED) {
852 853
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
854
    virStorageBackendPtr backend;
855
    int ret = -1;
856

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

860
    if (!pool) {
861
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
862
                              "%s", _("no storage pool with matching uuid"));
863
        goto cleanup;
864 865
    }

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

    if (!virStoragePoolObjIsActive(pool)) {
870
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
871
                              "%s", _("storage pool is not active"));
872
        goto cleanup;
873 874
    }

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

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

        pool->active = 0;

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

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


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

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

916
    if (!pool) {
917
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
918
                              "%s", _("no storage pool with matching uuid"));
919
        goto cleanup;
920 921
    }

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

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

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

static char *
storagePoolDumpXML(virStoragePoolPtr obj,
                   unsigned int flags ATTRIBUTE_UNUSED) {
944 945 946
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    char *ret = NULL;
947

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

952
    if (!pool) {
953
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
954
                              "%s", _("no storage pool with matching uuid"));
955
        goto cleanup;
956 957
    }

958
    ret = virStoragePoolDefFormat(pool->def);
959 960

cleanup:
961 962
    if (pool)
        virStoragePoolObjUnlock(pool);
963
    return ret;
964 965 966 967 968
}

static int
storagePoolGetAutostart(virStoragePoolPtr obj,
                        int *autostart) {
969 970 971
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
972

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

977
    if (!pool) {
978
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
979
                              "%s", _("no pool with matching uuid"));
980
        goto cleanup;
981 982 983 984 985 986 987
    }

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

990
cleanup:
991 992
    if (pool)
        virStoragePoolObjUnlock(pool);
993
    return ret;
994 995 996 997 998
}

static int
storagePoolSetAutostart(virStoragePoolPtr obj,
                        int autostart) {
999 1000 1001
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
1002

1003
    storageDriverLock(driver);
1004
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1005

1006
    if (!pool) {
1007
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1008
                              "%s", _("no pool with matching uuid"));
1009
        goto cleanup;
1010 1011 1012
    }

    if (!pool->configFile) {
1013
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1014
                              "%s", _("pool has no config file"));
1015
        goto cleanup;
1016 1017 1018 1019
    }

    autostart = (autostart != 0);

1020 1021 1022
    if (pool->autostart != autostart) {
        if (autostart) {
            int err;
1023

1024
            if ((err = virFileMakePath(driver->autostartDir))) {
1025
                virReportSystemError(err,
1026 1027
                                     _("cannot create autostart directory %s"),
                                     driver->autostartDir);
1028 1029
                goto cleanup;
            }
1030

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

1050
cleanup:
1051 1052
    if (pool)
        virStoragePoolObjUnlock(pool);
1053
    storageDriverUnlock(driver);
1054
    return ret;
1055 1056 1057 1058 1059
}


static int
storagePoolNumVolumes(virStoragePoolPtr obj) {
1060 1061 1062
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
    int ret = -1;
1063

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

1068
    if (!pool) {
1069
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1070
                              "%s", _("no storage pool with matching uuid"));
1071
        goto cleanup;
1072 1073 1074
    }

    if (!virStoragePoolObjIsActive(pool)) {
1075
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1076
                              "%s", _("storage pool is not active"));
1077
        goto cleanup;
1078
    }
1079
    ret = pool->volumes.count;
1080

1081
cleanup:
1082 1083
    if (pool)
        virStoragePoolObjUnlock(pool);
1084
    return ret;
1085 1086 1087 1088 1089 1090
}

static int
storagePoolListVolumes(virStoragePoolPtr obj,
                       char **const names,
                       int maxnames) {
1091 1092
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1093
    int i, n = 0;
1094

1095 1096
    memset(names, 0, maxnames * sizeof(*names));

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

1101
    if (!pool) {
1102
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1103
                              "%s", _("no storage pool with matching uuid"));
1104
        goto cleanup;
1105 1106 1107
    }

    if (!virStoragePoolObjIsActive(pool)) {
1108
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1109
                              "%s", _("storage pool is not active"));
1110
        goto cleanup;
1111 1112
    }

1113 1114
    for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) {
1115
            virReportOOMError();
1116 1117 1118 1119
            goto cleanup;
        }
    }

1120
    virStoragePoolObjUnlock(pool);
1121
    return n;
1122 1123

 cleanup:
1124 1125
    if (pool)
        virStoragePoolObjUnlock(pool);
1126
    for (n = 0 ; n < maxnames ; n++)
1127
        VIR_FREE(names[n]);
1128

1129
    memset(names, 0, maxnames * sizeof(*names));
1130 1131 1132 1133 1134 1135 1136
    return -1;
}


static virStorageVolPtr
storageVolumeLookupByName(virStoragePoolPtr obj,
                          const char *name) {
1137 1138
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1139
    virStorageVolDefPtr vol;
1140
    virStorageVolPtr ret = NULL;
1141

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

1146
    if (!pool) {
1147
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1148
                              "%s", _("no storage pool with matching uuid"));
1149
        goto cleanup;
1150 1151 1152
    }

    if (!virStoragePoolObjIsActive(pool)) {
1153
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1154
                              "%s", _("storage pool is not active"));
1155
        goto cleanup;
1156 1157 1158 1159 1160
    }

    vol = virStorageVolDefFindByName(pool, name);

    if (!vol) {
1161 1162
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
1163
                              name);
1164
        goto cleanup;
1165 1166
    }

1167 1168 1169
    ret = virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key);

cleanup:
1170 1171
    if (pool)
        virStoragePoolObjUnlock(pool);
1172
    return ret;
1173 1174 1175 1176 1177 1178
}


static virStorageVolPtr
storageVolumeLookupByKey(virConnectPtr conn,
                         const char *key) {
1179
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1180
    unsigned int i;
1181
    virStorageVolPtr ret = NULL;
1182

1183 1184 1185
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1186 1187 1188
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
            virStorageVolDefPtr vol =
                virStorageVolDefFindByKey(driver->pools.objs[i], key);
1189

1190
            if (vol)
1191
                ret = virGetStorageVol(conn,
1192 1193 1194
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1195
        }
1196
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1197
    }
1198
    storageDriverUnlock(driver);
1199

1200
    if (!ret)
1201
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
1202 1203 1204
                              "%s", _("no storage vol with matching key"));

    return ret;
1205 1206 1207 1208 1209
}

static virStorageVolPtr
storageVolumeLookupByPath(virConnectPtr conn,
                          const char *path) {
1210
    virStorageDriverStatePtr driver = conn->storagePrivateData;
1211
    unsigned int i;
1212
    virStorageVolPtr ret = NULL;
1213 1214 1215 1216 1217
    char *cleanpath;

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

1219 1220 1221
    storageDriverLock(driver);
    for (i = 0 ; i < driver->pools.count && !ret ; i++) {
        virStoragePoolObjLock(driver->pools.objs[i]);
1222
        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
1223
            virStorageVolDefPtr vol;
1224 1225
            const char *stable_path;

1226
            stable_path = virStorageBackendStablePath(driver->pools.objs[i],
1227
                                                      cleanpath);
1228 1229 1230 1231 1232
            /*
             * virStorageBackendStablePath already does
             * virStorageReportError if it fails; we just need to keep
             * propagating the return code
             */
1233 1234
            if (stable_path == NULL) {
                virStoragePoolObjUnlock(driver->pools.objs[i]);
1235
                goto cleanup;
1236
            }
1237 1238 1239 1240

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

1242
            if (vol)
1243 1244 1245 1246
                ret = virGetStorageVol(conn,
                                       driver->pools.objs[i]->def->name,
                                       vol->name,
                                       vol->key);
1247
        }
1248
        virStoragePoolObjUnlock(driver->pools.objs[i]);
1249 1250
    }

1251
    if (!ret)
1252
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
1253 1254 1255
                              "%s", _("no storage vol with matching path"));

cleanup:
1256
    VIR_FREE(cleanpath);
1257
    storageDriverUnlock(driver);
1258
    return ret;
1259 1260
}

1261 1262
static int storageVolumeDelete(virStorageVolPtr obj, unsigned int flags);

1263 1264 1265 1266
static virStorageVolPtr
storageVolumeCreateXML(virStoragePoolPtr obj,
                       const char *xmldesc,
                       unsigned int flags ATTRIBUTE_UNUSED) {
1267 1268
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1269
    virStorageBackendPtr backend;
1270 1271
    virStorageVolDefPtr voldef = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1272

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

1277
    if (!pool) {
1278
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1279
                              "%s", _("no storage pool with matching uuid"));
1280
        goto cleanup;
1281 1282 1283
    }

    if (!virStoragePoolObjIsActive(pool)) {
1284
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1285
                              "%s", _("storage pool is not active"));
1286
        goto cleanup;
1287 1288 1289
    }

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

1292
    voldef = virStorageVolDefParseString(pool->def, xmldesc);
1293
    if (voldef == NULL)
1294
        goto cleanup;
1295

1296
    if (virStorageVolDefFindByName(pool, voldef->name)) {
1297
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
J
Jim Meyering 已提交
1298
                              "%s", _("storage vol already exists"));
1299
        goto cleanup;
1300 1301
    }

1302 1303
    if (VIR_REALLOC_N(pool->volumes.objs,
                      pool->volumes.count+1) < 0) {
1304
        virReportOOMError();
1305
        goto cleanup;
1306 1307
    }

1308
    if (!backend->createVol) {
1309
        virStorageReportError(VIR_ERR_NO_SUPPORT,
1310 1311
                              "%s", _("storage pool does not support volume "
                                      "creation"));
1312
        goto cleanup;
1313 1314
    }

1315
    if (backend->createVol(obj->conn, pool, voldef) < 0) {
1316
        goto cleanup;
1317 1318
    }

1319 1320 1321
    pool->volumes.objs[pool->volumes.count++] = voldef;
    volobj = virGetStorageVol(obj->conn, pool->def->name, voldef->name,
                              voldef->key);
1322

1323 1324 1325 1326 1327
    if (volobj && backend->buildVol) {
        int buildret;
        virStorageVolDefPtr buildvoldef = NULL;

        if (VIR_ALLOC(buildvoldef) < 0) {
1328
            virReportOOMError();
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
            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);

1344
        buildret = backend->buildVol(obj->conn, pool, buildvoldef);
1345

1346
        storageDriverLock(driver);
1347
        virStoragePoolObjLock(pool);
1348 1349
        storageDriverUnlock(driver);

1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
        voldef->building = 0;
        pool->asyncjobs--;

        voldef = NULL;
        VIR_FREE(buildvoldef);

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

    }

1365 1366
    VIR_INFO(_("Creating volume '%s' in storage pool '%s'"),
             volobj->name, pool->def->name);
1367 1368 1369
    ret = volobj;
    volobj = NULL;
    voldef = NULL;
1370

1371
cleanup:
1372 1373 1374
    if (volobj)
        virUnrefStorageVol(volobj);
    virStorageVolDefFree(voldef);
1375 1376
    if (pool)
        virStoragePoolObjUnlock(pool);
1377
    return ret;
1378 1379
}

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
static virStorageVolPtr
storageVolumeCreateXMLFrom(virStoragePoolPtr obj,
                           const char *xmldesc,
                           virStorageVolPtr vobj,
                           unsigned int flags ATTRIBUTE_UNUSED) {
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool, origpool = NULL;
    virStorageBackendPtr backend;
    virStorageVolDefPtr origvol = NULL, newvol = NULL;
    virStorageVolPtr ret = NULL, volobj = NULL;
1390
    int buildret;
1391 1392 1393

    storageDriverLock(driver);
    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
1394
    if (pool && STRNEQ(obj->name, vobj->pool)) {
1395
        virStoragePoolObjUnlock(pool);
1396
        origpool = virStoragePoolObjFindByName(&driver->pools, vobj->pool);
1397
        virStoragePoolObjLock(pool);
1398
    }
1399 1400
    storageDriverUnlock(driver);
    if (!pool) {
1401
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
1402 1403 1404 1405
                              "%s", _("no storage pool with matching uuid"));
        goto cleanup;
    }

1406
    if (STRNEQ(obj->name, vobj->pool) && !origpool) {
1407 1408
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
                              _("no storage pool with matching name '%s'"),
1409
                              vobj->pool);
1410 1411 1412 1413
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1414
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1415 1416 1417 1418
                              "%s", _("storage pool is not active"));
        goto cleanup;
    }

1419
    if (origpool && !virStoragePoolObjIsActive(origpool)) {
1420
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1421 1422 1423 1424 1425 1426 1427
                              "%s", _("storage pool is not active"));
        goto cleanup;
    }

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

1428
    origvol = virStorageVolDefFindByName(origpool ? origpool : pool, vobj->name);
1429
    if (!origvol) {
1430 1431
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
1432
                              vobj->name);
1433 1434 1435
        goto cleanup;
    }

1436
    newvol = virStorageVolDefParseString(pool->def, xmldesc);
1437 1438 1439 1440
    if (newvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(pool, newvol->name)) {
1441
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1442 1443 1444 1445 1446 1447 1448 1449 1450
                              _("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;

1451 1452 1453 1454 1455
    /* 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;

1456
    if (!backend->buildVolFrom) {
1457
        virStorageReportError(VIR_ERR_NO_SUPPORT,
1458 1459 1460 1461 1462
                              "%s", _("storage pool does not support volume creation from an existing volume"));
        goto cleanup;
    }

    if (origvol->building) {
1463
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
                              _("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) {
1475
        virReportOOMError();
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
        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);

1494
    if (origpool) {
1495 1496 1497 1498
        origpool->asyncjobs++;
        virStoragePoolObjUnlock(origpool);
    }

1499
    buildret = backend->buildVolFrom(obj->conn, pool, newvol, origvol, flags);
1500 1501 1502

    storageDriverLock(driver);
    virStoragePoolObjLock(pool);
1503
    if (origpool)
1504 1505 1506 1507 1508 1509 1510 1511
        virStoragePoolObjLock(origpool);
    storageDriverUnlock(driver);

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

1512
    if (origpool) {
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
        origpool->asyncjobs--;
        virStoragePoolObjUnlock(origpool);
        origpool = NULL;
    }

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

1525 1526
    VIR_INFO(_("Creating volume '%s' in storage pool '%s'"),
             volobj->name, pool->def->name);
1527 1528 1529 1530 1531 1532 1533 1534 1535
    ret = volobj;
    volobj = NULL;

cleanup:
    if (volobj)
        virUnrefStorageVol(volobj);
    virStorageVolDefFree(newvol);
    if (pool)
        virStoragePoolObjUnlock(pool);
1536
    if (origpool)
1537 1538 1539 1540
        virStoragePoolObjUnlock(origpool);
    return ret;
}

1541

1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 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 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 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
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) {
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
                              _("volume '%s' is still being allocated."),
                              vol->name);
        goto out;
    }

    if (virFDStreamOpenFile(stream,
                            vol->target.path,
                            offset, length,
                            O_RDONLY) < 0)
        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) {
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
                              _("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,
                            O_WRONLY) < 0)
        goto out;

    ret = 0;

out:
    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}



1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
/* 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 "
1703
                               "path '%s' to %ju bytes"),
E
Eric Blake 已提交
1704
                             vol->target.path, (uintmax_t)size);
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
    }

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 已提交
1726
              (uintmax_t)extent_start, (uintmax_t)extent_length);
1727 1728 1729 1730 1731

    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 已提交
1732
                             (uintmax_t)extent_start, vol->target.path);
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 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
        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);

1810
    VIR_FORCE_CLOSE(fd);
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824

    return ret;
}


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

1825
    virCheckFlags(0, -1);
1826 1827 1828 1829 1830 1831

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

    if (!pool) {
1832
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
1833 1834 1835 1836 1837
                              "%s", _("no storage pool with matching uuid"));
        goto out;
    }

    if (!virStoragePoolObjIsActive(pool)) {
1838
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1839 1840 1841 1842 1843 1844 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
                              "%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) {
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
                              _("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;

}

1874 1875 1876
static int
storageVolumeDelete(virStorageVolPtr obj,
                    unsigned int flags) {
1877 1878
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1879
    virStorageBackendPtr backend;
1880
    virStorageVolDefPtr vol = NULL;
1881
    unsigned int i;
1882
    int ret = -1;
1883

1884
    storageDriverLock(driver);
1885
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
1886 1887
    storageDriverUnlock(driver);

1888
    if (!pool) {
1889
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1890
                              "%s", _("no storage pool with matching uuid"));
1891
        goto cleanup;
1892 1893 1894
    }

    if (!virStoragePoolObjIsActive(pool)) {
1895
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1896
                              "%s", _("storage pool is not active"));
1897
        goto cleanup;
1898 1899 1900
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
1901
        goto cleanup;
1902 1903 1904 1905

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

    if (!vol) {
1906
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
1907 1908
                             _("no storage vol with matching name '%s'"),
                              obj->name);
1909
        goto cleanup;
1910 1911
    }

1912
    if (vol->building) {
1913
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
1914 1915 1916 1917 1918
                              _("volume '%s' is still being allocated."),
                              vol->name);
        goto cleanup;
    }

1919
    if (!backend->deleteVol) {
1920
        virStorageReportError(VIR_ERR_NO_SUPPORT,
J
Jim Meyering 已提交
1921
                              "%s", _("storage pool does not support vol deletion"));
1922

1923
        goto cleanup;
1924 1925
    }

1926 1927 1928
    if (backend->deleteVol(obj->conn, pool, vol, flags) < 0)
        goto cleanup;

1929 1930
    for (i = 0 ; i < pool->volumes.count ; i++) {
        if (pool->volumes.objs[i] == vol) {
1931 1932
            VIR_INFO(_("Deleting volume '%s' from storage pool '%s'"),
                     vol->name, pool->def->name);
1933
            virStorageVolDefFree(vol);
1934
            vol = NULL;
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944

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

1945 1946 1947
            break;
        }
    }
1948
    ret = 0;
1949

1950
cleanup:
1951 1952
    if (pool)
        virStoragePoolObjUnlock(pool);
1953
    return ret;
1954 1955 1956 1957 1958
}

static int
storageVolumeGetInfo(virStorageVolPtr obj,
                     virStorageVolInfoPtr info) {
1959 1960
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
1961 1962
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
1963
    int ret = -1;
1964

1965
    storageDriverLock(driver);
1966
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
1967 1968
    storageDriverUnlock(driver);

1969
    if (!pool) {
1970
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
1971
                              "%s", _("no storage pool with matching uuid"));
1972
        goto cleanup;
1973 1974 1975
    }

    if (!virStoragePoolObjIsActive(pool)) {
1976
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
1977
                              "%s", _("storage pool is not active"));
1978
        goto cleanup;
1979 1980 1981 1982 1983
    }

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

    if (!vol) {
1984 1985
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
1986
                              obj->name);
1987
        goto cleanup;
1988 1989 1990
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
1991
        goto cleanup;
1992 1993 1994

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

    memset(info, 0, sizeof(*info));
1998
    info->type = vol->type;
1999 2000
    info->capacity = vol->capacity;
    info->allocation = vol->allocation;
2001
    ret = 0;
2002

2003
cleanup:
2004 2005
    if (pool)
        virStoragePoolObjUnlock(pool);
2006
    return ret;
2007 2008 2009 2010 2011
}

static char *
storageVolumeGetXMLDesc(virStorageVolPtr obj,
                        unsigned int flags ATTRIBUTE_UNUSED) {
2012 2013
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
    virStoragePoolObjPtr pool;
2014 2015
    virStorageBackendPtr backend;
    virStorageVolDefPtr vol;
2016
    char *ret = NULL;
2017

2018
    storageDriverLock(driver);
2019
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
2020 2021
    storageDriverUnlock(driver);

2022
    if (!pool) {
2023
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
2024
                              "%s", _("no storage pool with matching uuid"));
2025
        goto cleanup;
2026 2027 2028
    }

    if (!virStoragePoolObjIsActive(pool)) {
2029
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
2030
                              "%s", _("storage pool is not active"));
2031
        goto cleanup;
2032 2033 2034 2035 2036
    }

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

    if (!vol) {
2037
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
2038 2039
                             _("no storage vol with matching name '%s'"),
                              obj->name);
2040
        goto cleanup;
2041 2042 2043
    }

    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
2044
        goto cleanup;
2045 2046 2047 2048

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

2050
    ret = virStorageVolDefFormat(pool->def, vol);
2051 2052

cleanup:
2053 2054 2055
    if (pool)
        virStoragePoolObjUnlock(pool);

2056
    return ret;
2057 2058 2059 2060
}

static char *
storageVolumeGetPath(virStorageVolPtr obj) {
2061
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
2062
    virStoragePoolObjPtr pool;
2063
    virStorageVolDefPtr vol;
2064
    char *ret = NULL;
2065

2066 2067 2068
    storageDriverLock(driver);
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
    storageDriverUnlock(driver);
2069
    if (!pool) {
2070
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
J
Jim Meyering 已提交
2071
                              "%s", _("no storage pool with matching uuid"));
2072
        goto cleanup;
2073 2074 2075
    }

    if (!virStoragePoolObjIsActive(pool)) {
2076
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
J
Jim Meyering 已提交
2077
                              "%s", _("storage pool is not active"));
2078
        goto cleanup;
2079 2080 2081 2082 2083
    }

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

    if (!vol) {
2084 2085
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
                              _("no storage vol with matching name '%s'"),
2086
                              obj->name);
2087
        goto cleanup;
2088 2089 2090
    }

    ret = strdup(vol->target.path);
2091
    if (ret == NULL)
2092
        virReportOOMError();
2093 2094

cleanup:
2095 2096
    if (pool)
        virStoragePoolObjUnlock(pool);
2097 2098 2099 2100
    return ret;
}

static virStorageDriver storageDriver = {
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
    .name = "storage",
    .open = storageOpen,
    .close = storageClose,
    .numOfPools = storageNumPools,
    .listPools = storageListPools,
    .numOfDefinedPools = storageNumDefinedPools,
    .listDefinedPools = storageListDefinedPools,
    .findPoolSources = storageFindPoolSources,
    .poolLookupByName = storagePoolLookupByName,
    .poolLookupByUUID = storagePoolLookupByUUID,
    .poolLookupByVolume = storagePoolLookupByVolume,
    .poolCreateXML = storagePoolCreate,
    .poolDefineXML = storagePoolDefine,
    .poolBuild = storagePoolBuild,
    .poolUndefine = storagePoolUndefine,
    .poolCreate = storagePoolStart,
    .poolDestroy = storagePoolDestroy,
    .poolDelete = storagePoolDelete,
    .poolRefresh = storagePoolRefresh,
    .poolGetInfo = storagePoolGetInfo,
    .poolGetXMLDesc = storagePoolDumpXML,
    .poolGetAutostart = storagePoolGetAutostart,
    .poolSetAutostart = storagePoolSetAutostart,
    .poolNumOfVolumes = storagePoolNumVolumes,
    .poolListVolumes = storagePoolListVolumes,

    .volLookupByName = storageVolumeLookupByName,
    .volLookupByKey = storageVolumeLookupByKey,
    .volLookupByPath = storageVolumeLookupByPath,
    .volCreateXML = storageVolumeCreateXML,
2131
    .volCreateXMLFrom = storageVolumeCreateXMLFrom,
2132 2133
    .volDownload = storageVolumeDownload,
    .volUpload = storageVolumeUpload,
2134
    .volDelete = storageVolumeDelete,
2135
    .volWipe = storageVolumeWipe,
2136 2137 2138
    .volGetInfo = storageVolumeGetInfo,
    .volGetXMLDesc = storageVolumeGetXMLDesc,
    .volGetPath = storageVolumeGetPath,
2139 2140 2141

    .poolIsActive = storagePoolIsActive,
    .poolIsPersistent = storagePoolIsPersistent,
2142 2143 2144 2145
};


static virStateDriver stateDriver = {
2146
    .name = "Storage",
2147 2148 2149 2150
    .initialize = storageDriverStartup,
    .cleanup = storageDriverShutdown,
    .reload = storageDriverReload,
    .active = storageDriverActive,
2151 2152 2153 2154 2155 2156 2157
};

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