storage_driver.c 61.3 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"
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
    return ret;
}

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

static virDrvOpenStatus
storageOpen(virConnectPtr conn,
            virConnectAuthPtr auth ATTRIBUTE_UNUSED,
325
            unsigned int flags ATTRIBUTE_UNUSED) {
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    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
}

static char *
942 943
storagePoolGetXMLDesc(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
    if (pool->autostart != autostart) {
        if (autostart) {
1022 1023
            if (virFileMakePath(driver->autostartDir) < 0) {
                virReportSystemError(errno,
1024 1025
                                     _("cannot create autostart directory %s"),
                                     driver->autostartDir);
1026 1027
                goto cleanup;
            }
1028

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

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


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

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

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

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

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

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

1093 1094
    memset(names, 0, maxnames * sizeof(*names));

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

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

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

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

1118
    virStoragePoolObjUnlock(pool);
1119
    return n;
1120 1121

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

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


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

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

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

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

    vol = virStorageVolDefFindByName(pool, name);

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

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

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


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

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

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

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

    return ret;
1203 1204 1205 1206 1207
}

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

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

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

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

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

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

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

cleanup:
1254
    VIR_FREE(cleanpath);
1255
    storageDriverUnlock(driver);
1256
    return ret;
1257 1258
}

1259 1260
static int storageVolumeDelete(virStorageVolPtr obj, unsigned int flags);

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

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

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

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

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

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

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

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

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

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

1317 1318 1319
    pool->volumes.objs[pool->volumes.count++] = voldef;
    volobj = virGetStorageVol(obj->conn, pool->def->name, voldef->name,
                              voldef->key);
1320 1321 1322 1323
    if (!volobj) {
        pool->volumes.count--;
        goto cleanup;
    }
1324

1325
    if (backend->buildVol) {
1326 1327 1328 1329
        int buildret;
        virStorageVolDefPtr buildvoldef = NULL;

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

1346
        buildret = backend->buildVol(obj->conn, pool, buildvoldef);
1347

1348
        storageDriverLock(driver);
1349
        virStoragePoolObjLock(pool);
1350 1351
        storageDriverUnlock(driver);

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

        voldef = NULL;
        VIR_FREE(buildvoldef);

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

    }

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

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

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
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;
1392
    int buildret;
1393 1394 1395

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

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

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

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

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

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

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

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

1453 1454 1455 1456 1457
    /* 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;

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

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

1496
    if (origpool) {
1497 1498 1499 1500
        origpool->asyncjobs++;
        virStoragePoolObjUnlock(origpool);
    }

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

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

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

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

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

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

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

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
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) {
1584
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1585 1586 1587 1588 1589 1590 1591 1592
                              _("volume '%s' is still being allocated."),
                              vol->name);
        goto out;
    }

    if (virFDStreamOpenFile(stream,
                            vol->target.path,
                            offset, length,
1593
                            O_RDONLY, false) < 0)
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
        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,
1657
                            O_WRONLY, false) < 0)
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
        goto out;

    ret = 0;

out:
    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}



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 1703 1704
/* 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 "
1705
                               "path '%s' to %ju bytes"),
E
Eric Blake 已提交
1706
                             vol->target.path, (uintmax_t)size);
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
    }

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

    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 已提交
1734
                             (uintmax_t)extent_start, vol->target.path);
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 1810 1811
        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);

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

    return ret;
}


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

1827
    virCheckFlags(0, -1);
1828 1829 1830 1831 1832 1833

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

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

    if (!virStoragePoolObjIsActive(pool)) {
1840
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
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 1874 1875
                              "%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;

}

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

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

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

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

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

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

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

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

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

1925
        goto cleanup;
1926 1927
    }

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

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

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

1947 1948 1949
            break;
        }
    }
1950
    ret = 0;
1951

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2052
    ret = virStorageVolDefFormat(pool->def, vol);
2053 2054

cleanup:
2055 2056 2057
    if (pool)
        virStoragePoolObjUnlock(pool);

2058
    return ret;
2059 2060 2061 2062
}

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

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

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

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

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

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

cleanup:
2097 2098
    if (pool)
        virStoragePoolObjUnlock(pool);
2099 2100 2101 2102
    return ret;
}

static virStorageDriver storageDriver = {
2103
    .name = "storage",
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 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
    .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 */
2144 2145 2146 2147
};


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

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