storage_driver.c 60.9 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 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
}

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 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
    if (!volobj) {
        pool->volumes.count--;
        goto cleanup;
    }
1326

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

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

1348
        buildret = backend->buildVol(obj->conn, pool, buildvoldef);
1349

1350
        storageDriverLock(driver);
1351
        virStoragePoolObjLock(pool);
1352 1353
        storageDriverUnlock(driver);

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

        voldef = NULL;
        VIR_FREE(buildvoldef);

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

    }

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

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

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

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

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

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

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

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

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

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

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

1455 1456 1457 1458 1459
    /* 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;

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

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

1498
    if (origpool) {
1499 1500 1501 1502
        origpool->asyncjobs++;
        virStoragePoolObjUnlock(origpool);
    }

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

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

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

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

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

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

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

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 1669 1670 1671 1672
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;
}



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

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

    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 已提交
1736
                             (uintmax_t)extent_start, vol->target.path);
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 1812 1813
        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);

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

    return ret;
}


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

1829
    virCheckFlags(0, -1);
1830 1831 1832 1833 1834 1835

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

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

    if (!virStoragePoolObjIsActive(pool)) {
1842
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
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 1876 1877
                              "%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;

}

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

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

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

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

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

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

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

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

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

1927
        goto cleanup;
1928 1929
    }

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

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

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

1949 1950 1951
            break;
        }
    }
1952
    ret = 0;
1953

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2054
    ret = virStorageVolDefFormat(pool->def, vol);
2055 2056

cleanup:
2057 2058 2059
    if (pool)
        virStoragePoolObjUnlock(pool);

2060
    return ret;
2061 2062 2063 2064
}

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

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

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

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

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

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

cleanup:
2099 2100
    if (pool)
        virStoragePoolObjUnlock(pool);
2101 2102 2103 2104
    return ret;
}

static virStorageDriver storageDriver = {
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
    .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,
2125
    .poolGetXMLDesc = storagePoolGetXMLDesc,
2126 2127 2128 2129 2130 2131 2132 2133 2134
    .poolGetAutostart = storagePoolGetAutostart,
    .poolSetAutostart = storagePoolSetAutostart,
    .poolNumOfVolumes = storagePoolNumVolumes,
    .poolListVolumes = storagePoolListVolumes,

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

    .poolIsActive = storagePoolIsActive,
    .poolIsPersistent = storagePoolIsPersistent,
2146 2147 2148 2149
};


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

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