storage_backend_fs.c 29.4 KB
Newer Older
1 2 3
/*
 * storage_backend_fs.c: storage backend for FS and directory handling
 *
4
 * Copyright (C) 2007-2010 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * Copyright (C) 2007-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 <sys/statvfs.h>
#include <sys/types.h>
#include <sys/stat.h>
29
#include <stdbool.h>
30 31 32 33 34 35 36
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

37 38 39 40
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>

41
#include "virterror_internal.h"
42 43
#include "storage_backend_fs.h"
#include "storage_conf.h"
44
#include "storage_file.h"
45
#include "util.h"
46
#include "memory.h"
47
#include "xml.h"
48

49
#define VIR_FROM_THIS VIR_FROM_STORAGE
50

51
static int
52
virStorageBackendProbeTarget(virStorageVolTargetPtr target,
53
                             char **backingStore,
54
                             int *backingStoreFormat,
55 56 57 58 59
                             unsigned long long *allocation,
                             unsigned long long *capacity,
                             virStorageEncryptionPtr *encryption)
{
    int fd, ret;
60
    virStorageFileMetadata meta;
61

62 63 64 65
    if (backingStore)
        *backingStore = NULL;
    if (backingStoreFormat)
        *backingStoreFormat = VIR_STORAGE_FILE_AUTO;
66 67 68
    if (encryption)
        *encryption = NULL;

69 70 71 72
    if ((ret = virStorageBackendVolOpenCheckMode(target->path,
                                                 (VIR_STORAGE_VOL_OPEN_DEFAULT & ~VIR_STORAGE_VOL_OPEN_ERROR))) < 0)
        return ret; /* Take care to propagate ret, it is not always -1 */
    fd = ret;
73

74
    if ((ret = virStorageBackendUpdateVolTargetInfoFD(target, fd,
75 76 77
                                                      allocation,
                                                      capacity)) < 0) {
        close(fd);
78
        return -1;
79 80
    }

81 82
    memset(&meta, 0, sizeof(meta));

83
    if ((target->format = virStorageFileProbeFormatFromFD(target->path, fd)) < 0) {
84 85 86 87
        close(fd);
        return -1;
    }

88 89 90 91 92 93
    if (virStorageFileGetMetadataFromFD(target->path, fd,
                                        target->format,
                                        &meta) < 0) {
        close(fd);
        return -1;
    }
94

95
    close(fd);
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    if (meta.backingStore) {
        if (backingStore) {
            *backingStore = meta.backingStore;
            meta.backingStore = NULL;
            if (meta.backingStoreFormat == VIR_STORAGE_FILE_AUTO) {
                if ((*backingStoreFormat = virStorageFileProbeFormat(*backingStore)) < 0) {
                    close(fd);
                    goto cleanup;
                }
            } else {
                *backingStoreFormat = meta.backingStoreFormat;
            }
        } else {
            VIR_FREE(meta.backingStore);
        }
112 113 114 115 116 117
    }

    if (capacity && meta.capacity)
        *capacity = meta.capacity;

    if (encryption != NULL && meta.encrypted) {
118
        if (VIR_ALLOC(*encryption) < 0) {
119
            virReportOOMError();
120
            goto cleanup;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        }

        switch (target->format) {
        case VIR_STORAGE_FILE_QCOW:
        case VIR_STORAGE_FILE_QCOW2:
            (*encryption)->format = VIR_STORAGE_ENCRYPTION_FORMAT_QCOW;
            break;
        default:
            break;
        }

        /* XXX ideally we'd fill in secret UUID here
         * but we cannot guarentee 'conn' is non-NULL
         * at this point in time :-(  So we only fill
         * in secrets when someone first queries a vol
         */
    }

139
    return 0;
140 141 142 143 144

cleanup:
    if (backingStore)
        VIR_FREE(*backingStore);
    return -1;
145 146 147
}

#if WITH_STORAGE_FS
148

149
# include <mntent.h>
150

151 152
struct _virNetfsDiscoverState {
    const char *host;
153
    virStoragePoolSourceList list;
154 155 156 157 158
};

typedef struct _virNetfsDiscoverState virNetfsDiscoverState;

static int
159
virStorageBackendFileSystemNetFindPoolSourcesFunc(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
160 161 162 163 164
                                                  char **const groups,
                                                  void *data)
{
    virNetfsDiscoverState *state = data;
    const char *name, *path;
165 166
    virStoragePoolSource *src = NULL;
    int ret = -1;
167 168 169 170 171

    path = groups[0];

    name = strrchr(path, '/');
    if (name == NULL) {
172
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
173
                              _("invalid netfs path (no /): %s"), path);
174
        goto cleanup;
175 176 177
    }
    name += 1;
    if (*name == '\0') {
178
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
179
                              _("invalid netfs path (ends in /): %s"), path);
180
        goto cleanup;
181 182
    }

183
    if (!(src = virStoragePoolSourceListNewSource(&state->list)))
184
        goto cleanup;
185

186
    if (!(src->host.name = strdup(state->host)) ||
187
        !(src->dir = strdup(path))) {
188
        virReportOOMError();
189 190
        goto cleanup;
    }
191
    src->format = VIR_STORAGE_POOL_NETFS_NFS;
192

193 194 195
    src = NULL;
    ret = 0;
cleanup:
196 197
    virStoragePoolSourceFree(src);
    VIR_FREE(src);
198
    return ret;
199 200
}

201

202
static char *
203
virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                                              const char *srcSpec,
                                              unsigned int flags ATTRIBUTE_UNUSED)
{
    /*
     *  # showmount --no-headers -e HOSTNAME
     *  /tmp   *
     *  /A dir demo1.foo.bar,demo2.foo.bar
     *
     * Extract directory name (including possible interior spaces ...).
     */

    const char *regexes[] = {
        "^(/.*\\S) +\\S+$"
    };
    int vars[] = {
        1
    };
221 222 223 224 225 226 227 228
    virNetfsDiscoverState state = {
        .host = NULL,
        .list = {
            .type = VIR_STORAGE_POOL_NETFS,
            .nsources = 0,
            .sources = NULL
        }
    };
229
    const char *prog[] = { SHOWMOUNT, "--no-headers", "--exports", NULL, NULL };
230
    virStoragePoolSourcePtr source = NULL;
231 232
    int exitstatus;
    char *retval = NULL;
233
    unsigned int i;
234

235
    source = virStoragePoolDefParseSourceString(srcSpec,
236 237
                                                VIR_STORAGE_POOL_NETFS);
    if (!source)
238 239
        goto cleanup;

240 241
    state.host = source->host.name;
    prog[3] = source->host.name;
242

243
    if (virStorageBackendRunProgRegex(NULL, prog, 1, regexes, vars,
244 245 246 247
                                      virStorageBackendFileSystemNetFindPoolSourcesFunc,
                                      &state, &exitstatus) < 0)
        goto cleanup;

248
    retval = virStoragePoolSourceListFormat(&state.list);
249
    if (retval == NULL) {
250
        virReportOOMError();
251 252 253 254
        goto cleanup;
    }

 cleanup:
255 256 257
    for (i = 0; i < state.list.nsources; i++)
        virStoragePoolSourceFree(&state.list.sources[i]);

258 259
    virStoragePoolSourceFree(source);
    VIR_FREE(source);
260

261
    VIR_FREE(state.list.sources);
262 263 264 265 266

    return retval;
}


267 268 269 270 271 272 273 274 275
/**
 * @conn connection to report errors against
 * @pool storage pool to check for status
 *
 * Determine if a storage pool is already mounted
 *
 * Return 0 if not mounted, 1 if mounted, -1 on error
 */
static int
276
virStorageBackendFileSystemIsMounted(virStoragePoolObjPtr pool) {
277
    FILE *mtab;
278 279
    struct mntent ent;
    char buf[1024];
280 281

    if ((mtab = fopen(_PATH_MOUNTED, "r")) == NULL) {
282
        virReportSystemError(errno,
283 284
                             _("cannot read mount list '%s'"),
                             _PATH_MOUNTED);
285 286 287
        return -1;
    }

288 289
    while ((getmntent_r(mtab, &ent, buf, sizeof(buf))) != NULL) {
        if (STREQ(ent.mnt_dir, pool->def->target.path)) {
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
            fclose(mtab);
            return 1;
        }
    }

    fclose(mtab);
    return 0;
}

/**
 * @conn connection to report errors against
 * @pool storage pool to mount
 *
 * Ensure that a FS storage pool is mounted on its target location.
 * If already mounted, this is a no-op
 *
 * Returns 0 if successfully mounted, -1 on error
 */
static int
309
virStorageBackendFileSystemMount(virStoragePoolObjPtr pool) {
310
    char *src;
311
    char *options = NULL;
312 313 314 315 316 317 318
    const char **mntargv;

    /* 'mount -t auto' doesn't seem to auto determine nfs (or cifs),
     *  while plain 'mount' does. We have to craft separate argvs to
     *  accommodate this */
    int netauto = (pool->def->type == VIR_STORAGE_POOL_NETFS &&
                   pool->def->source.format == VIR_STORAGE_POOL_NETFS_AUTO);
319 320 321 322
    int glusterfs = (pool->def->type == VIR_STORAGE_POOL_NETFS &&
                 pool->def->source.format == VIR_STORAGE_POOL_NETFS_GLUSTERFS);

    int option_index;
323 324 325 326 327 328 329 330 331 332
    int source_index;

    const char *netfs_auto_argv[] = {
        MOUNT,
        NULL, /* source path */
        pool->def->target.path,
        NULL,
    };

    const char *fs_argv[] =  {
333 334 335
        MOUNT,
        "-t",
        pool->def->type == VIR_STORAGE_POOL_FS ?
336 337
        virStoragePoolFormatFileSystemTypeToString(pool->def->source.format) :
        virStoragePoolFormatFileSystemNetTypeToString(pool->def->source.format),
338 339
        NULL, /* Fill in shortly - careful not to add extra fields
                 before this */
340 341 342
        pool->def->target.path,
        NULL,
    };
343

344 345 346 347 348 349 350 351 352 353 354 355 356
    const char *glusterfs_argv[] = {
        MOUNT,
        "-t",
        pool->def->type == VIR_STORAGE_POOL_FS ?
        virStoragePoolFormatFileSystemTypeToString(pool->def->source.format) :
        virStoragePoolFormatFileSystemNetTypeToString(pool->def->source.format),
        NULL,
        "-o",
        NULL,
        pool->def->target.path,
        NULL,
    };

357 358 359
    if (netauto) {
        mntargv = netfs_auto_argv;
        source_index = 1;
360 361 362 363
    } else if (glusterfs) {
        mntargv = glusterfs_argv;
        source_index = 3;
        option_index = 5;
364 365 366 367 368
    } else {
        mntargv = fs_argv;
        source_index = 3;
    }

369 370 371 372
    int ret;

    if (pool->def->type == VIR_STORAGE_POOL_NETFS) {
        if (pool->def->source.host.name == NULL) {
373
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
374
                                  "%s", _("missing source host"));
375 376 377
            return -1;
        }
        if (pool->def->source.dir == NULL) {
378
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
379
                                  "%s", _("missing source path"));
380 381 382 383
            return -1;
        }
    } else {
        if (pool->def->source.ndevice != 1) {
384
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
385
                                  "%s", _("missing source device"));
386 387 388 389
            return -1;
        }
    }

J
Jim Meyering 已提交
390
    /* Short-circuit if already mounted */
391
    if ((ret = virStorageBackendFileSystemIsMounted(pool)) != 0) {
392 393 394 395 396 397 398
        if (ret < 0)
            return -1;
        else
            return 0;
    }

    if (pool->def->type == VIR_STORAGE_POOL_NETFS) {
399
        if (pool->def->source.format == VIR_STORAGE_POOL_NETFS_GLUSTERFS) {
400
            if ((options = strdup("direct-io-mode=1")) == NULL) {
401
                virReportOOMError();
402 403 404
                return -1;
            }
        }
405 406 407
        if (virAsprintf(&src, "%s:%s",
                        pool->def->source.host.name,
                        pool->def->source.dir) == -1) {
408
            virReportOOMError();
409 410
            return -1;
        }
411

412
    } else {
413
        if ((src = strdup(pool->def->source.devices[0].path)) == NULL) {
414
            virReportOOMError();
415 416
            return -1;
        }
417
    }
418
    mntargv[source_index] = src;
419

420 421 422 423
    if (glusterfs) {
        mntargv[option_index] = options;
    }

424
    if (virRun(mntargv, NULL) < 0) {
425
        VIR_FREE(src);
426 427
        return -1;
    }
428
    VIR_FREE(src);
429 430 431 432 433 434 435 436 437 438 439 440 441
    return 0;
}

/**
 * @conn connection to report errors against
 * @pool storage pool to unmount
 *
 * Ensure that a FS storage pool is not mounted on its target location.
 * If already unmounted, this is a no-op
 *
 * Returns 0 if successfully unmounted, -1 on error
 */
static int
442
virStorageBackendFileSystemUnmount(virStoragePoolObjPtr pool) {
443 444 445 446 447
    const char *mntargv[3];
    int ret;

    if (pool->def->type == VIR_STORAGE_POOL_NETFS) {
        if (pool->def->source.host.name == NULL) {
448
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
449
                                  "%s", _("missing source host"));
450 451 452
            return -1;
        }
        if (pool->def->source.dir == NULL) {
453
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
454
                                  "%s", _("missing source dir"));
455 456 457 458
            return -1;
        }
    } else {
        if (pool->def->source.ndevice != 1) {
459
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
460
                                  "%s", _("missing source device"));
461 462 463 464 465
            return -1;
        }
    }

    /* Short-circuit if already unmounted */
466
    if ((ret = virStorageBackendFileSystemIsMounted(pool)) != 1) {
467 468 469 470 471 472 473 474 475 476
        if (ret < 0)
            return -1;
        else
            return 0;
    }

    mntargv[0] = UMOUNT;
    mntargv[1] = pool->def->target.path;
    mntargv[2] = NULL;

477
    if (virRun(mntargv, NULL) < 0) {
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
        return -1;
    }
    return 0;
}
#endif /* WITH_STORAGE_FS */


/**
 * @conn connection to report errors against
 * @pool storage pool to start
 *
 * Starts a directory or FS based storage pool.
 *
 *  - If it is a FS based pool, mounts the unlying source device on the pool
 *
 * Returns 0 on success, -1 on error
 */
#if WITH_STORAGE_FS
static int
497
virStorageBackendFileSystemStart(virConnectPtr conn ATTRIBUTE_UNUSED,
498 499 500
                                 virStoragePoolObjPtr pool)
{
    if (pool->def->type != VIR_STORAGE_POOL_DIR &&
501
        virStorageBackendFileSystemMount(pool) < 0)
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        return -1;

    return 0;
}
#endif /* WITH_STORAGE_FS */


/**
 * @conn connection to report errors against
 * @pool storage pool to build
 *
 * Build a directory or FS based storage pool.
 *
 *  - If it is a FS based pool, mounts the unlying source device on the pool
 *
 * Returns 0 on success, -1 on error
 */
static int
520
virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
521 522 523
                                 virStoragePoolObjPtr pool,
                                 unsigned int flags ATTRIBUTE_UNUSED)
{
524 525 526 527 528
    int err, ret = -1;
    char *parent;
    char *p;

    if ((parent = strdup(pool->def->target.path)) == NULL) {
529
        virReportOOMError();
530 531 532
        goto error;
    }
    if (!(p = strrchr(parent, '/'))) {
533
        virStorageReportError(VIR_ERR_INVALID_ARG,
534 535 536
                              _("path '%s' is not absolute"),
                              pool->def->target.path);
        goto error;
537 538
    }

539 540 541 542 543
    if (p != parent) {
        /* assure all directories in the path prior to the final dir
         * exist, with default uid/gid/mode. */
        *p = '\0';
        if ((err = virFileMakePath(parent)) != 0) {
544
            virReportSystemError(err, _("cannot create path '%s'"),
545 546 547 548 549 550 551 552
                                 parent);
            goto error;
        }
    }

    /* Now create the final dir in the path with the uid/gid/mode
     * requested in the config. If the dir already exists, just set
     * the perms. */
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

    struct stat st;

    if ((stat(pool->def->target.path, &st) < 0)
        || (pool->def->target.perms.uid != -1)) {

        uid_t uid = (pool->def->target.perms.uid == -1)
            ? getuid() : pool->def->target.perms.uid;
        gid_t gid = (pool->def->target.perms.gid == -1)
            ? getgid() : pool->def->target.perms.gid;

        if ((err = virDirCreate(pool->def->target.path,
                                pool->def->target.perms.mode,
                                uid, gid,
                                VIR_DIR_CREATE_FORCE_PERMS |
                                VIR_DIR_CREATE_ALLOW_EXIST |
                                (pool->def->type == VIR_STORAGE_POOL_NETFS
570 571
                                 ? VIR_DIR_CREATE_AS_UID : 0)) < 0)) {
            virReportSystemError(-err, _("cannot create path '%s'"),
572 573 574
                                 pool->def->target.path);
            goto error;
        }
575 576 577 578 579
    }
    ret = 0;
error:
    VIR_FREE(parent);
    return ret;
580 581 582 583 584 585 586 587
}


/**
 * Iterate over the pool's directory and enumerate all disk images
 * within it. This is non-recursive.
 */
static int
588
virStorageBackendFileSystemRefresh(virConnectPtr conn ATTRIBUTE_UNUSED,
589 590 591 592 593
                                   virStoragePoolObjPtr pool)
{
    DIR *dir;
    struct dirent *ent;
    struct statvfs sb;
594
    virStorageVolDefPtr vol = NULL;
595 596

    if (!(dir = opendir(pool->def->target.path))) {
597
        virReportSystemError(errno,
598 599
                             _("cannot open path '%s'"),
                             pool->def->target.path);
600 601 602 603 604
        goto cleanup;
    }

    while ((ent = readdir(dir)) != NULL) {
        int ret;
605
        char *backingStore;
606
        int backingStoreFormat;
607

608 609
        if (VIR_ALLOC(vol) < 0)
            goto no_memory;
610

611 612
        if ((vol->name = strdup(ent->d_name)) == NULL)
            goto no_memory;
613

614
        vol->type = VIR_STORAGE_VOL_FILE;
615
        vol->target.format = VIR_STORAGE_FILE_RAW; /* Real value is filled in during probe */
616 617 618
        if (virAsprintf(&vol->target.path, "%s/%s",
                        pool->def->target.path,
                        vol->name) == -1)
619 620 621 622
            goto no_memory;

        if ((vol->key = strdup(vol->target.path)) == NULL)
            goto no_memory;
623

624
        if ((ret = virStorageBackendProbeTarget(&vol->target,
625
                                                &backingStore,
626
                                                &backingStoreFormat,
627
                                                &vol->allocation,
628
                                                &vol->capacity,
629
                                                &vol->target.encryption)) < 0) {
630
            if (ret == -1)
631
                goto cleanup;
632
            else {
633 634
                /* Silently ignore non-regular files,
                 * eg '.' '..', 'lost+found' */
635 636
                virStorageVolDefFree(vol);
                vol = NULL;
637
                continue;
638
            }
639 640
        }

641
        if (backingStore != NULL) {
642 643 644 645 646 647 648 649
            vol->backingStore.path = backingStore;
            vol->backingStore.format = backingStoreFormat;

            if (virStorageBackendUpdateVolTargetInfo(&vol->backingStore,
                                                     NULL,
                                                     NULL) < 0) {
                VIR_FREE(vol->backingStore.path);
                goto cleanup;
650 651 652 653
            }
        }


654 655 656 657 658
        if (VIR_REALLOC_N(pool->volumes.objs,
                          pool->volumes.count+1) < 0)
            goto no_memory;
        pool->volumes.objs[pool->volumes.count++] = vol;
        vol = NULL;
659 660 661 662 663
    }
    closedir(dir);


    if (statvfs(pool->def->target.path, &sb) < 0) {
664
        virReportSystemError(errno,
665 666
                             _("cannot statvfs path '%s'"),
                             pool->def->target.path);
667 668 669 670 671 672 673 674 675 676
        return -1;
    }
    pool->def->capacity = ((unsigned long long)sb.f_frsize *
                           (unsigned long long)sb.f_blocks);
    pool->def->available = ((unsigned long long)sb.f_bfree *
                            (unsigned long long)sb.f_bsize);
    pool->def->allocation = pool->def->capacity - pool->def->available;

    return 0;

677
no_memory:
678
    virReportOOMError();
679 680
    /* fallthrough */

681
 cleanup:
682 683
    if (dir)
        closedir(dir);
684
    virStorageVolDefFree(vol);
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
    virStoragePoolObjClearVols(pool);
    return -1;
}


/**
 * @conn connection to report errors against
 * @pool storage pool to start
 *
 * Stops a directory or FS based storage pool.
 *
 *  - If it is a FS based pool, unmounts the unlying source device on the pool
 *  - Releases all cached data about volumes
 */
#if WITH_STORAGE_FS
static int
701
virStorageBackendFileSystemStop(virConnectPtr conn ATTRIBUTE_UNUSED,
702 703 704
                                virStoragePoolObjPtr pool)
{
    if (pool->def->type != VIR_STORAGE_POOL_DIR &&
705
        virStorageBackendFileSystemUnmount(pool) < 0)
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
        return -1;

    return 0;
}
#endif /* WITH_STORAGE_FS */


/**
 * @conn connection to report errors against
 * @pool storage pool to build
 *
 * Build a directory or FS based storage pool.
 *
 *  - If it is a FS based pool, mounts the unlying source device on the pool
 *
 * Returns 0 on success, -1 on error
 */
static int
724
virStorageBackendFileSystemDelete(virConnectPtr conn ATTRIBUTE_UNUSED,
725 726 727 728 729
                                  virStoragePoolObjPtr pool,
                                  unsigned int flags ATTRIBUTE_UNUSED)
{
    /* XXX delete all vols first ? */

730
    if (rmdir(pool->def->target.path) < 0) {
731
        virReportSystemError(errno,
732
                             _("failed to remove pool '%s'"),
733
                             pool->def->target.path);
734 735 736 737 738 739 740 741
        return -1;
    }

    return 0;
}


/**
742 743 744 745
 * Set up a volume definition to be added to a pool's volume list, but
 * don't do any file creation or allocation. By separating the two processes,
 * we allow allocation progress reporting (by polling the volume's 'info'
 * function), and can drop the parent pool lock during the (slow) allocation.
746 747
 */
static int
748
virStorageBackendFileSystemVolCreate(virConnectPtr conn ATTRIBUTE_UNUSED,
749 750 751 752
                                     virStoragePoolObjPtr pool,
                                     virStorageVolDefPtr vol)
{

753 754
    vol->type = VIR_STORAGE_VOL_FILE;

R
Ryota Ozaki 已提交
755
    VIR_FREE(vol->target.path);
756 757 758
    if (virAsprintf(&vol->target.path, "%s/%s",
                    pool->def->target.path,
                    vol->name) == -1) {
759
        virReportOOMError();
760 761
        return -1;
    }
762

R
Ryota Ozaki 已提交
763
    VIR_FREE(vol->key);
764 765
    vol->key = strdup(vol->target.path);
    if (vol->key == NULL) {
766
        virReportOOMError();
767 768 769
        return -1;
    }

770 771 772
    return 0;
}

773
static int createFileDir(virConnectPtr conn ATTRIBUTE_UNUSED,
774
                         virStoragePoolObjPtr pool,
775
                         virStorageVolDefPtr vol,
776 777
                         virStorageVolDefPtr inputvol,
                         unsigned int flags ATTRIBUTE_UNUSED) {
778 779
    int err;

780
    if (inputvol) {
781
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
782 783 784 785 786
                              "%s",
                              _("cannot copy from volume to a directory volume"));
        return -1;
    }

787 788 789 790 791
    uid_t uid = (vol->target.perms.uid == -1)
        ? getuid() : vol->target.perms.uid;
    gid_t gid = (vol->target.perms.gid == -1)
        ? getgid() : vol->target.perms.gid;

792
    if ((err = virDirCreate(vol->target.path, vol->target.perms.mode,
793
                            uid, gid,
794
                            VIR_DIR_CREATE_FORCE_PERMS |
795
                            (pool->def->type == VIR_STORAGE_POOL_NETFS
796 797
                             ? VIR_DIR_CREATE_AS_UID : 0))) < 0) {
        virReportSystemError(-err, _("cannot create path '%s'"),
798 799 800
                             vol->target.path);
        return -1;
    }
801

802 803
    return 0;
}
804

805
static int
806
_virStorageBackendFileSystemVolBuild(virConnectPtr conn,
807
                                     virStoragePoolObjPtr pool,
808 809
                                     virStorageVolDefPtr vol,
                                     virStorageVolDefPtr inputvol)
810
{
811
    virStorageBackendBuildVolFrom create_func;
812
    int tool_type;
813

814
    if (inputvol) {
815
        if (vol->target.encryption != NULL) {
816
            virStorageReportError(VIR_ERR_NO_SUPPORT,
817 818 819 820 821
                                  "%s", _("storage pool does not support "
                                          "building encrypted volumes from "
                                          "other volumes"));
            return -1;
        }
822
        create_func = virStorageBackendGetBuildVolFromFunction(vol,
823
                                                               inputvol);
824 825
        if (!create_func)
            return -1;
826
    } else if (vol->target.format == VIR_STORAGE_FILE_RAW) {
827
        create_func = virStorageBackendCreateRaw;
828
    } else if (vol->target.format == VIR_STORAGE_FILE_DIR) {
829
        create_func = createFileDir;
830
    } else if ((tool_type = virStorageBackendFindFSImageTool(NULL)) != -1) {
831
        create_func = virStorageBackendFSImageToolTypeToFunc(tool_type);
832 833

        if (!create_func)
834
            return -1;
835
    } else {
836
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
837 838
                              "%s", _("creation of non-raw images "
                                      "is not supported without qemu-img"));
839 840 841
        return -1;
    }

842
    if (create_func(conn, pool, vol, inputvol, 0) < 0)
843
        return -1;
844 845 846
    return 0;
}

847 848 849 850 851 852 853
/**
 * Allocate a new file as a volume. This is either done directly
 * for raw/sparse files, or by calling qemu-img/qcow-create for
 * special kinds of files
 */
static int
virStorageBackendFileSystemVolBuild(virConnectPtr conn,
854
                                    virStoragePoolObjPtr pool,
855
                                    virStorageVolDefPtr vol) {
856
    return _virStorageBackendFileSystemVolBuild(conn, pool, vol, NULL);
857 858 859 860 861 862 863
}

/*
 * Create a storage vol using 'inputvol' as input
 */
static int
virStorageBackendFileSystemVolBuildFrom(virConnectPtr conn,
864
                                        virStoragePoolObjPtr pool,
865 866 867
                                        virStorageVolDefPtr vol,
                                        virStorageVolDefPtr inputvol,
                                        unsigned int flags ATTRIBUTE_UNUSED) {
868
    return _virStorageBackendFileSystemVolBuild(conn, pool, vol, inputvol);
869
}
870 871 872 873 874

/**
 * Remove a volume - just unlinks for now
 */
static int
875
virStorageBackendFileSystemVolDelete(virConnectPtr conn ATTRIBUTE_UNUSED,
876 877 878 879 880 881 882
                                     virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
                                     virStorageVolDefPtr vol,
                                     unsigned int flags ATTRIBUTE_UNUSED)
{
    if (unlink(vol->target.path) < 0) {
        /* Silently ignore failures where the vol has already gone away */
        if (errno != ENOENT) {
883
            virReportSystemError(errno,
884 885
                                 _("cannot unlink file '%s'"),
                                 vol->target.path);
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
            return -1;
        }
    }
    return 0;
}


/**
 * Update info about a volume's capacity/allocation
 */
static int
virStorageBackendFileSystemVolRefresh(virConnectPtr conn,
                                      virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
                                      virStorageVolDefPtr vol)
{
901 902
    int ret;

903
    /* Refresh allocation / permissions info in case its changed */
904
    ret = virStorageBackendUpdateVolInfo(vol, 0);
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    if (ret < 0)
        return ret;

    /* Load any secrets if posible */
    if (vol->target.encryption &&
        vol->target.encryption->format == VIR_STORAGE_ENCRYPTION_FORMAT_QCOW &&
        vol->target.encryption->nsecrets == 0) {
        virSecretPtr sec;
        virStorageEncryptionSecretPtr encsec = NULL;

        sec = virSecretLookupByUsage(conn,
                                     VIR_SECRET_USAGE_TYPE_VOLUME,
                                     vol->target.path);
        if (sec) {
            if (VIR_ALLOC_N(vol->target.encryption->secrets, 1) < 0 ||
                VIR_ALLOC(encsec) < 0) {
                VIR_FREE(vol->target.encryption->secrets);
922
                virReportOOMError();
923 924 925 926 927 928 929 930 931 932 933 934 935 936
                virSecretFree(sec);
                return -1;
            }

            vol->target.encryption->nsecrets = 1;
            vol->target.encryption->secrets[0] = encsec;

            encsec->type = VIR_STORAGE_ENCRYPTION_SECRET_TYPE_PASSPHRASE;
            virSecretGetUUID(sec, encsec->uuid);
            virSecretFree(sec);
        }
    }

    return 0;
937 938 939 940 941 942 943 944
}

virStorageBackend virStorageBackendDirectory = {
    .type = VIR_STORAGE_POOL_DIR,

    .buildPool = virStorageBackendFileSystemBuild,
    .refreshPool = virStorageBackendFileSystemRefresh,
    .deletePool = virStorageBackendFileSystemDelete,
945
    .buildVol = virStorageBackendFileSystemVolBuild,
946
    .buildVolFrom = virStorageBackendFileSystemVolBuildFrom,
947 948 949 950 951 952 953 954 955 956 957 958 959 960
    .createVol = virStorageBackendFileSystemVolCreate,
    .refreshVol = virStorageBackendFileSystemVolRefresh,
    .deleteVol = virStorageBackendFileSystemVolDelete,
};

#if WITH_STORAGE_FS
virStorageBackend virStorageBackendFileSystem = {
    .type = VIR_STORAGE_POOL_FS,

    .buildPool = virStorageBackendFileSystemBuild,
    .startPool = virStorageBackendFileSystemStart,
    .refreshPool = virStorageBackendFileSystemRefresh,
    .stopPool = virStorageBackendFileSystemStop,
    .deletePool = virStorageBackendFileSystemDelete,
961
    .buildVol = virStorageBackendFileSystemVolBuild,
962
    .buildVolFrom = virStorageBackendFileSystemVolBuildFrom,
963 964 965 966 967 968 969 970 971
    .createVol = virStorageBackendFileSystemVolCreate,
    .refreshVol = virStorageBackendFileSystemVolRefresh,
    .deleteVol = virStorageBackendFileSystemVolDelete,
};
virStorageBackend virStorageBackendNetFileSystem = {
    .type = VIR_STORAGE_POOL_NETFS,

    .buildPool = virStorageBackendFileSystemBuild,
    .startPool = virStorageBackendFileSystemStart,
972
    .findPoolSources = virStorageBackendFileSystemNetFindPoolSources,
973 974 975
    .refreshPool = virStorageBackendFileSystemRefresh,
    .stopPool = virStorageBackendFileSystemStop,
    .deletePool = virStorageBackendFileSystemDelete,
976
    .buildVol = virStorageBackendFileSystemVolBuild,
977
    .buildVolFrom = virStorageBackendFileSystemVolBuildFrom,
978 979 980 981 982
    .createVol = virStorageBackendFileSystemVolCreate,
    .refreshVol = virStorageBackendFileSystemVolRefresh,
    .deleteVol = virStorageBackendFileSystemVolDelete,
};
#endif /* WITH_STORAGE_FS */