virstoragefile.c 53.9 KB
Newer Older
1
/*
2
 * virstoragefile.c: file utility functions for FS storage backend
3
 *
4
 * Copyright (C) 2007-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>
25
#include "virstoragefile.h"
26

27
#include <sys/stat.h>
28
#include <unistd.h>
29
#include <fcntl.h>
30
#include <stdlib.h>
31
#include "viralloc.h"
32
#include "virerror.h"
33
#include "virlog.h"
E
Eric Blake 已提交
34
#include "virfile.h"
35
#include "c-ctype.h"
36
#include "vircommand.h"
37
#include "virhash.h"
E
Eric Blake 已提交
38
#include "virendian.h"
39 40
#include "virstring.h"
#include "virutil.h"
41 42
#include "viruri.h"
#include "dirname.h"
43 44 45
#if HAVE_SYS_SYSCALL_H
# include <sys/syscall.h>
#endif
46 47 48

#define VIR_FROM_THIS VIR_FROM_STORAGE

49 50
VIR_LOG_INIT("util.storagefile");

E
Eric Blake 已提交
51
VIR_ENUM_IMPL(virStorage, VIR_STORAGE_TYPE_LAST,
52
              "none",
E
Eric Blake 已提交
53
              "file",
54
              "block",
E
Eric Blake 已提交
55 56 57 58
              "dir",
              "network",
              "volume")

59 60
VIR_ENUM_IMPL(virStorageFileFormat,
              VIR_STORAGE_FILE_LAST,
E
Eric Blake 已提交
61
              "none",
62
              "raw", "dir", "bochs",
63 64 65
              "cloop", "dmg", "iso",
              "vpc", "vdi",
              /* Not direct file formats, but used for various drivers */
66
              "fat", "vhd", "ploop",
67 68
              /* Formats with backing file below here */
              "cow", "qcow", "qcow2", "qed", "vmdk")
69

70 71 72 73 74
VIR_ENUM_IMPL(virStorageFileFeature,
              VIR_STORAGE_FILE_FEATURE_LAST,
              "lazy_refcounts",
              )

75
VIR_ENUM_IMPL(virStorageNetProtocol, VIR_STORAGE_NET_PROTOCOL_LAST,
76
              "none",
77 78 79 80 81 82 83 84 85 86
              "nbd",
              "rbd",
              "sheepdog",
              "gluster",
              "iscsi",
              "http",
              "https",
              "ftp",
              "ftps",
              "tftp")
87 88 89 90 91 92

VIR_ENUM_IMPL(virStorageNetHostTransport, VIR_STORAGE_NET_HOST_TRANS_LAST,
              "tcp",
              "unix",
              "rdma")

93 94 95 96 97
VIR_ENUM_IMPL(virStorageSourcePoolMode,
              VIR_STORAGE_SOURCE_POOL_MODE_LAST,
              "default",
              "host",
              "direct")
98

99 100 101 102 103 104 105 106 107 108 109
enum lv_endian {
    LV_LITTLE_ENDIAN = 1, /* 1234 */
    LV_BIG_ENDIAN         /* 4321 */
};

enum {
    BACKING_STORE_OK,
    BACKING_STORE_INVALID,
    BACKING_STORE_ERROR,
};

110 111
#define FILE_TYPE_VERSIONS_LAST 2

112 113
/* Either 'magic' or 'extension' *must* be provided */
struct FileTypeInfo {
114
    int magicOffset;    /* Byte offset of the magic */
115 116 117 118 119 120
    const char *magic;  /* Optional string of file magic
                         * to check at head of file */
    const char *extension; /* Optional file extension to check */
    enum lv_endian endian; /* Endianness of file format */
    int versionOffset;    /* Byte offset from start of file
                           * where we find version number,
121 122
                           * -1 to always fail the version test,
                           * -2 to always pass the version test */
123 124
    int versionNumbers[FILE_TYPE_VERSIONS_LAST];
                          /* Version numbers to validate. Zeroes are ignored. */
125 126 127 128 129 130 131 132 133 134 135
    int sizeOffset;       /* Byte offset from start of file
                           * where we find capacity info,
                           * -1 to use st_size as capacity */
    int sizeBytes;        /* Number of bytes for size field */
    int sizeMultiplier;   /* A scaling factor if size is not in bytes */
                          /* Store a COW base image path (possibly relative),
                           * or NULL if there is no COW base image, to RES;
                           * return BACKING_STORE_* */
    int qcowCryptOffset;  /* Byte offset from start of file
                           * where to find encryption mode,
                           * -1 if encryption is not used */
136
    int (*getBackingStore)(char **res, int *format,
E
Eric Blake 已提交
137
                           const char *buf, size_t buf_size);
138
    int (*getFeatures)(virBitmapPtr *features, int format,
E
Eric Blake 已提交
139
                       char *buf, ssize_t len);
140 141
};

142
static int cowGetBackingStore(char **, int *,
E
Eric Blake 已提交
143
                              const char *, size_t);
144
static int qcow1GetBackingStore(char **, int *,
E
Eric Blake 已提交
145
                                const char *, size_t);
146
static int qcow2GetBackingStore(char **, int *,
E
Eric Blake 已提交
147
                                const char *, size_t);
148
static int qcow2GetFeatures(virBitmapPtr *features, int format,
E
Eric Blake 已提交
149
                            char *buf, ssize_t len);
150
static int vmdk4GetBackingStore(char **, int *,
E
Eric Blake 已提交
151
                                const char *, size_t);
152
static int
E
Eric Blake 已提交
153
qedGetBackingStore(char **, int *, const char *, size_t);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

#define QCOWX_HDR_VERSION (4)
#define QCOWX_HDR_BACKING_FILE_OFFSET (QCOWX_HDR_VERSION+4)
#define QCOWX_HDR_BACKING_FILE_SIZE (QCOWX_HDR_BACKING_FILE_OFFSET+8)
#define QCOWX_HDR_IMAGE_SIZE (QCOWX_HDR_BACKING_FILE_SIZE+4+4)

#define QCOW1_HDR_CRYPT (QCOWX_HDR_IMAGE_SIZE+8+1+1)
#define QCOW2_HDR_CRYPT (QCOWX_HDR_IMAGE_SIZE+8)

#define QCOW1_HDR_TOTAL_SIZE (QCOW1_HDR_CRYPT+4+8)
#define QCOW2_HDR_TOTAL_SIZE (QCOW2_HDR_CRYPT+4+4+8+8+4+4+8)

#define QCOW2_HDR_EXTENSION_END 0
#define QCOW2_HDR_EXTENSION_BACKING_FORMAT 0xE2792ACA

169 170 171 172 173 174 175
#define QCOW2v3_HDR_FEATURES_INCOMPATIBLE (QCOW2_HDR_TOTAL_SIZE)
#define QCOW2v3_HDR_FEATURES_COMPATIBLE (QCOW2v3_HDR_FEATURES_INCOMPATIBLE+8)
#define QCOW2v3_HDR_FEATURES_AUTOCLEAR (QCOW2v3_HDR_FEATURES_COMPATIBLE+8)

/* The location of the header size [4 bytes] */
#define QCOW2v3_HDR_SIZE       (QCOW2_HDR_TOTAL_SIZE+8+8+8+4)

176
#define QED_HDR_FEATURES_OFFSET (4+4+4+4)
177 178
#define QED_HDR_IMAGE_SIZE (QED_HDR_FEATURES_OFFSET+8+8+8+8)
#define QED_HDR_BACKING_FILE_OFFSET (QED_HDR_IMAGE_SIZE+8)
179 180 181
#define QED_HDR_BACKING_FILE_SIZE (QED_HDR_BACKING_FILE_OFFSET+4)
#define QED_F_BACKING_FILE 0x01
#define QED_F_BACKING_FORMAT_NO_PROBE 0x04
A
Adam Litke 已提交
182

183 184

static struct FileTypeInfo const fileTypeInfo[] = {
185
    [VIR_STORAGE_FILE_NONE] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
186
                                -1, {0}, 0, 0, 0, 0, NULL, NULL },
187
    [VIR_STORAGE_FILE_RAW] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
188
                               -1, {0}, 0, 0, 0, 0, NULL, NULL },
189
    [VIR_STORAGE_FILE_DIR] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
190
                               -1, {0}, 0, 0, 0, 0, NULL, NULL },
191
    [VIR_STORAGE_FILE_BOCHS] = {
192 193
        /*"Bochs Virtual HD Image", */ /* Untested */
        0, NULL, NULL,
194 195
        LV_LITTLE_ENDIAN, 64, {0x20000},
        32+16+16+4+4+4+4+4, 8, 1, -1, NULL, NULL
196 197
    },
    [VIR_STORAGE_FILE_CLOOP] = {
198 199 200 201 202
        /* #!/bin/sh
           #V2.0 Format
           modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1
        */ /* Untested */
        0, NULL, NULL,
203 204
        LV_LITTLE_ENDIAN, -1, {0},
        -1, 0, 0, -1, NULL, NULL
205 206
    },
    [VIR_STORAGE_FILE_DMG] = {
207 208 209 210
        /* XXX QEMU says there's no magic for dmg,
         * /usr/share/misc/magic lists double magic (both offsets
         * would have to match) but then disables that check. */
        0, NULL, ".dmg",
211 212
        0, -1, {0},
        -1, 0, 0, -1, NULL, NULL
213 214
    },
    [VIR_STORAGE_FILE_ISO] = {
215
        32769, "CD001", ".iso",
216 217
        LV_LITTLE_ENDIAN, -2, {0},
        -1, 0, 0, -1, NULL, NULL
218
    },
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    [VIR_STORAGE_FILE_VPC] = {
        0, "conectix", NULL,
        LV_BIG_ENDIAN, 12, {0x10000},
        8 + 4 + 4 + 8 + 4 + 4 + 2 + 2 + 4, 8, 1, -1, NULL, NULL
    },
    /* TODO: add getBackingStore function */
    [VIR_STORAGE_FILE_VDI] = {
        64, "\x7f\x10\xda\xbe", ".vdi",
        LV_LITTLE_ENDIAN, 68, {0x00010001},
        64 + 5 * 4 + 256 + 7 * 4, 8, 1, -1, NULL, NULL},

    /* Not direct file formats, but used for various drivers */
    [VIR_STORAGE_FILE_FAT] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
                               -1, {0}, 0, 0, 0, 0, NULL, NULL },
    [VIR_STORAGE_FILE_VHD] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
                               -1, {0}, 0, 0, 0, 0, NULL, NULL },
235 236
    [VIR_STORAGE_FILE_PLOOP] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
                               -1, {0}, 0, 0, 0, 0, NULL, NULL },
237 238 239 240 241 242 243

    /* All formats with a backing store probe below here */
    [VIR_STORAGE_FILE_COW] = {
        0, "OOOM", NULL,
        LV_BIG_ENDIAN, 4, {2},
        4+4+1024+4, 8, 1, -1, cowGetBackingStore, NULL
    },
244
    [VIR_STORAGE_FILE_QCOW] = {
245
        0, "QFI", NULL,
246 247
        LV_BIG_ENDIAN, 4, {1},
        QCOWX_HDR_IMAGE_SIZE, 8, 1, QCOW1_HDR_CRYPT, qcow1GetBackingStore, NULL
248 249
    },
    [VIR_STORAGE_FILE_QCOW2] = {
250
        0, "QFI", NULL,
251
        LV_BIG_ENDIAN, 4, {2, 3},
252
        QCOWX_HDR_IMAGE_SIZE, 8, 1, QCOW2_HDR_CRYPT, qcow2GetBackingStore,
253
        qcow2GetFeatures
254
    },
A
Adam Litke 已提交
255 256
    [VIR_STORAGE_FILE_QED] = {
        /* http://wiki.qemu.org/Features/QED */
257
        0, "QED", NULL,
258 259
        LV_LITTLE_ENDIAN, -2, {0},
        QED_HDR_IMAGE_SIZE, 8, 1, -1, qedGetBackingStore, NULL
A
Adam Litke 已提交
260
    },
261
    [VIR_STORAGE_FILE_VMDK] = {
262
        0, "KDMV", NULL,
263
        LV_LITTLE_ENDIAN, 4, {1, 2},
264
        4+4+4, 8, 512, -1, vmdk4GetBackingStore, NULL
265
    },
266
};
267
verify(ARRAY_CARDINALITY(fileTypeInfo) == VIR_STORAGE_FILE_LAST);
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282
/* qcow2 compatible features in the order they appear on-disk */
enum qcow2CompatibleFeature {
    QCOW2_COMPATIBLE_FEATURE_LAZY_REFCOUNTS = 0,

    QCOW2_COMPATIBLE_FEATURE_LAST
};

/* conversion to virStorageFileFeature */
static const int qcow2CompatibleFeatureArray[] = {
    VIR_STORAGE_FILE_FEATURE_LAZY_REFCOUNTS,
};
verify(ARRAY_CARDINALITY(qcow2CompatibleFeatureArray) ==
       QCOW2_COMPATIBLE_FEATURE_LAST);

283
static int
284
cowGetBackingStore(char **res,
285
                   int *format,
E
Eric Blake 已提交
286
                   const char *buf,
287 288 289 290
                   size_t buf_size)
{
#define COW_FILENAME_MAXLEN 1024
    *res = NULL;
291 292
    *format = VIR_STORAGE_FILE_AUTO;

293 294
    if (buf_size < 4+4+ COW_FILENAME_MAXLEN)
        return BACKING_STORE_INVALID;
E
Eric Blake 已提交
295 296
    if (buf[4+4] == '\0') { /* cow_header_v2.backing_file[0] */
        *format = VIR_STORAGE_FILE_NONE;
297
        return BACKING_STORE_OK;
E
Eric Blake 已提交
298
    }
299

300
    if (VIR_STRNDUP(*res, (const char*)buf + 4 + 4, COW_FILENAME_MAXLEN) < 0)
301 302 303 304
        return BACKING_STORE_ERROR;
    return BACKING_STORE_OK;
}

305 306 307

static int
qcow2GetBackingStoreFormat(int *format,
E
Eric Blake 已提交
308
                           const char *buf,
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
                           size_t buf_size,
                           size_t extension_start,
                           size_t extension_end)
{
    size_t offset = extension_start;

    /*
     * The extensions take format of
     *
     * int32: magic
     * int32: length
     * byte[length]: payload
     *
     * Unknown extensions can be ignored by skipping
     * over "length" bytes in the data stream.
     */
    while (offset < (buf_size-8) &&
           offset < (extension_end-8)) {
E
Eric Blake 已提交
327 328
        unsigned int magic = virReadBufInt32BE(buf + offset);
        unsigned int len = virReadBufInt32BE(buf + offset + 4);
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

        offset += 8;

        if ((offset + len) < offset)
            break;

        if ((offset + len) > buf_size)
            break;

        switch (magic) {
        case QCOW2_HDR_EXTENSION_END:
            goto done;

        case QCOW2_HDR_EXTENSION_BACKING_FORMAT:
            if (buf[offset+len] != '\0')
                break;
            *format = virStorageFileFormatTypeFromString(
                ((const char *)buf)+offset);
E
Eric Blake 已提交
347 348
            if (*format <= VIR_STORAGE_FILE_NONE)
                return -1;
349 350 351 352 353
        }

        offset += len;
    }

354
 done:
355 356 357 358 359

    return 0;
}


360
static int
361
qcowXGetBackingStore(char **res,
362
                     int *format,
E
Eric Blake 已提交
363
                     const char *buf,
364 365
                     size_t buf_size,
                     bool isQCow2)
366 367
{
    unsigned long long offset;
368
    unsigned int size;
369 370
    unsigned long long start;
    int version;
371 372

    *res = NULL;
373 374 375 376
    if (format)
        *format = VIR_STORAGE_FILE_AUTO;

    if (buf_size < QCOWX_HDR_BACKING_FILE_OFFSET+8+4)
377
        return BACKING_STORE_INVALID;
E
Eric Blake 已提交
378
    offset = virReadBufInt64BE(buf + QCOWX_HDR_BACKING_FILE_OFFSET);
379 380
    if (offset > buf_size)
        return BACKING_STORE_INVALID;
E
Eric Blake 已提交
381
    size = virReadBufInt32BE(buf + QCOWX_HDR_BACKING_FILE_SIZE);
E
Eric Blake 已提交
382 383 384
    if (size == 0) {
        if (format)
            *format = VIR_STORAGE_FILE_NONE;
385
        return BACKING_STORE_OK;
E
Eric Blake 已提交
386
    }
387 388 389 390
    if (offset + size > buf_size || offset + size < offset)
        return BACKING_STORE_INVALID;
    if (size + 1 == 0)
        return BACKING_STORE_INVALID;
391
    if (VIR_ALLOC_N(*res, size + 1) < 0)
392 393 394
        return BACKING_STORE_ERROR;
    memcpy(*res, buf + offset, size);
    (*res)[size] = '\0';
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

    /*
     * Traditionally QCow2 files had a layout of
     *
     * [header]
     * [backingStoreName]
     *
     * Although the backingStoreName typically followed
     * the header immediately, this was not required by
     * the format. By specifying a higher byte offset for
     * the backing file offset in the header, it was
     * possible to leave space between the header and
     * start of backingStore.
     *
     * This hack is now used to store extensions to the
     * qcow2 format:
     *
     * [header]
     * [extensions]
     * [backingStoreName]
     *
     * Thus the file region to search for extensions is
     * between the end of the header (QCOW2_HDR_TOTAL_SIZE)
     * and the start of the backingStoreName (offset)
419 420 421
     *
     * for qcow2 v3 images, the length of the header
     * is stored at QCOW2v3_HDR_SIZE
422
     */
423 424 425 426 427 428 429 430 431 432
    if (isQCow2 && format) {
        version = virReadBufInt32BE(buf + QCOWX_HDR_VERSION);
        if (version == 2)
            start = QCOW2_HDR_TOTAL_SIZE;
        else
            start = virReadBufInt32BE(buf + QCOW2v3_HDR_SIZE);
        if (qcow2GetBackingStoreFormat(format, buf, buf_size,
                                       start, offset) < 0)
            return BACKING_STORE_INVALID;
    }
433

434 435 436 437
    return BACKING_STORE_OK;
}


438 439 440
static int
qcow1GetBackingStore(char **res,
                     int *format,
E
Eric Blake 已提交
441
                     const char *buf,
442 443
                     size_t buf_size)
{
E
Eric Blake 已提交
444 445
    int ret;

446 447 448
    /* QCow1 doesn't have the extensions capability
     * used to store backing format */
    *format = VIR_STORAGE_FILE_AUTO;
E
Eric Blake 已提交
449 450 451 452
    ret = qcowXGetBackingStore(res, NULL, buf, buf_size, false);
    if (ret == 0 && *buf == '\0')
        *format = VIR_STORAGE_FILE_NONE;
    return ret;
453 454 455 456 457
}

static int
qcow2GetBackingStore(char **res,
                     int *format,
E
Eric Blake 已提交
458
                     const char *buf,
459 460 461 462 463 464
                     size_t buf_size)
{
    return qcowXGetBackingStore(res, format, buf, buf_size, true);
}


465
static int
466
vmdk4GetBackingStore(char **res,
467
                     int *format,
E
Eric Blake 已提交
468
                     const char *buf,
469 470 471
                     size_t buf_size)
{
    static const char prefix[] = "parentFileNameHint=\"";
472
    char *desc, *start, *end;
473
    size_t len;
474 475
    int ret = BACKING_STORE_ERROR;

476
    if (VIR_ALLOC_N(desc, VIR_STORAGE_MAX_HEADER) < 0)
477
        goto cleanup;
478 479

    *res = NULL;
480 481 482 483 484 485 486 487
    /*
     * Technically this should have been VMDK, since
     * VMDK spec / VMWare impl only support VMDK backed
     * by VMDK. QEMU isn't following this though and
     * does probing on VMDK backing files, hence we set
     * AUTO
     */
    *format = VIR_STORAGE_FILE_AUTO;
488

489 490 491 492
    if (buf_size <= 0x200) {
        ret = BACKING_STORE_INVALID;
        goto cleanup;
    }
493
    len = buf_size - 0x200;
494 495
    if (len > VIR_STORAGE_MAX_HEADER)
        len = VIR_STORAGE_MAX_HEADER;
496 497 498
    memcpy(desc, buf + 0x200, len);
    desc[len] = '\0';
    start = strstr(desc, prefix);
499
    if (start == NULL) {
E
Eric Blake 已提交
500
        *format = VIR_STORAGE_FILE_NONE;
501 502 503
        ret = BACKING_STORE_OK;
        goto cleanup;
    }
504 505
    start += strlen(prefix);
    end = strchr(start, '"');
506 507 508 509 510
    if (end == NULL) {
        ret = BACKING_STORE_INVALID;
        goto cleanup;
    }
    if (end == start) {
E
Eric Blake 已提交
511
        *format = VIR_STORAGE_FILE_NONE;
512 513 514
        ret = BACKING_STORE_OK;
        goto cleanup;
    }
515
    *end = '\0';
516
    if (VIR_STRDUP(*res, start) < 0)
517 518 519 520
        goto cleanup;

    ret = BACKING_STORE_OK;

521
 cleanup:
522 523
    VIR_FREE(desc);
    return ret;
524 525
}

526 527 528
static int
qedGetBackingStore(char **res,
                   int *format,
E
Eric Blake 已提交
529
                   const char *buf,
530 531 532 533 534 535 536 537 538
                   size_t buf_size)
{
    unsigned long long flags;
    unsigned long offset, size;

    *res = NULL;
    /* Check if this image has a backing file */
    if (buf_size < QED_HDR_FEATURES_OFFSET+8)
        return BACKING_STORE_INVALID;
E
Eric Blake 已提交
539
    flags = virReadBufInt64LE(buf + QED_HDR_FEATURES_OFFSET);
E
Eric Blake 已提交
540 541
    if (!(flags & QED_F_BACKING_FILE)) {
        *format = VIR_STORAGE_FILE_NONE;
542
        return BACKING_STORE_OK;
E
Eric Blake 已提交
543
    }
544 545 546 547

    /* Parse the backing file */
    if (buf_size < QED_HDR_BACKING_FILE_OFFSET+8)
        return BACKING_STORE_INVALID;
E
Eric Blake 已提交
548
    offset = virReadBufInt32LE(buf + QED_HDR_BACKING_FILE_OFFSET);
549 550
    if (offset > buf_size)
        return BACKING_STORE_INVALID;
E
Eric Blake 已提交
551
    size = virReadBufInt32LE(buf + QED_HDR_BACKING_FILE_SIZE);
552 553 554 555
    if (size == 0)
        return BACKING_STORE_OK;
    if (offset + size > buf_size || offset + size < offset)
        return BACKING_STORE_INVALID;
556
    if (VIR_ALLOC_N(*res, size + 1) < 0)
557 558 559 560
        return BACKING_STORE_ERROR;
    memcpy(*res, buf + offset, size);
    (*res)[size] = '\0';

E
Eric Blake 已提交
561 562 563 564
    if (flags & QED_F_BACKING_FORMAT_NO_PROBE)
        *format = VIR_STORAGE_FILE_RAW;
    else
        *format = VIR_STORAGE_FILE_AUTO_SAFE;
565 566 567 568

    return BACKING_STORE_OK;
}

569 570 571

static bool
virStorageFileMatchesMagic(int format,
E
Eric Blake 已提交
572
                           char *buf,
573
                           size_t buflen)
574
{
575
    int mlen;
576 577
    int magicOffset = fileTypeInfo[format].magicOffset;
    const char *magic = fileTypeInfo[format].magic;
578

579
    if (magic == NULL)
580
        return false;
581

582
    /* Validate magic data */
583 584
    mlen = strlen(magic);
    if (magicOffset + mlen > buflen)
585
        return false;
586

587
    if (memcmp(buf + magicOffset, magic, mlen) != 0)
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
        return false;

    return true;
}


static bool
virStorageFileMatchesExtension(int format,
                               const char *path)
{
    if (fileTypeInfo[format].extension == NULL)
        return false;

    if (virFileHasSuffix(path, fileTypeInfo[format].extension))
        return true;

    return false;
}


static bool
virStorageFileMatchesVersion(int format,
E
Eric Blake 已提交
610
                             char *buf,
611 612
                             size_t buflen)
{
613 614
    int version;
    size_t i;
615 616 617

    /* Validate version number info */
    if (fileTypeInfo[format].versionOffset == -1)
E
Eric Blake 已提交
618
        return false;
619

620 621 622 623
    /* -2 == non-versioned file format, so trivially match */
    if (fileTypeInfo[format].versionOffset == -2)
        return true;

624 625 626
    if ((fileTypeInfo[format].versionOffset + 4) > buflen)
        return false;

E
Eric Blake 已提交
627 628 629 630
    if (fileTypeInfo[format].endian == LV_LITTLE_ENDIAN)
        version = virReadBufInt32LE(buf + fileTypeInfo[format].versionOffset);
    else
        version = virReadBufInt32BE(buf + fileTypeInfo[format].versionOffset);
631

632 633 634 635 636 637 638 639
    for (i = 0;
         i < FILE_TYPE_VERSIONS_LAST && fileTypeInfo[format].versionNumbers[i];
         i++) {
        VIR_DEBUG("Compare detected version %d vs one of the expected versions %d",
                  version, fileTypeInfo[format].versionNumbers[i]);
        if (version == fileTypeInfo[format].versionNumbers[i])
            return true;
    }
640

641
    return false;
642
}
643

644 645
bool
virStorageIsFile(const char *backing)
A
Adam Litke 已提交
646
{
647 648 649 650 651 652 653 654
    char *colon;
    char *slash;

    if (!backing)
        return false;

    colon = strchr(backing, ':');
    slash = strchr(backing, '/');
655 656 657 658 659

    /* Reject anything that looks like a protocol (such as nbd: or
     * rbd:); if someone really does want a relative file name that
     * includes ':', they can always prefix './'.  */
    if (colon && (!slash || colon < slash))
A
Adam Litke 已提交
660 661 662
        return false;
    return true;
}
663

E
Eric Blake 已提交
664

665 666 667 668 669 670 671 672 673 674 675 676 677
static bool
virStorageIsRelative(const char *backing)
{
    if (backing[0] == '/')
        return false;

    if (!virStorageIsFile(backing))
        return false;

    return true;
}


E
Eric Blake 已提交
678
static int
E
Eric Blake 已提交
679
virStorageFileProbeFormatFromBuf(const char *path,
E
Eric Blake 已提交
680
                                 char *buf,
E
Eric Blake 已提交
681 682 683
                                 size_t buflen)
{
    int format = VIR_STORAGE_FILE_RAW;
684
    size_t i;
E
Eric Blake 已提交
685
    int possibleFormat = VIR_STORAGE_FILE_RAW;
686
    VIR_DEBUG("path=%s, buf=%p, buflen=%zu", path, buf, buflen);
E
Eric Blake 已提交
687 688

    /* First check file magic */
689
    for (i = 0; i < VIR_STORAGE_FILE_LAST; i++) {
E
Eric Blake 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
        if (virStorageFileMatchesMagic(i, buf, buflen)) {
            if (!virStorageFileMatchesVersion(i, buf, buflen)) {
                possibleFormat = i;
                continue;
            }
            format = i;
            goto cleanup;
        }
    }

    if (possibleFormat != VIR_STORAGE_FILE_RAW)
        VIR_WARN("File %s matches %s magic, but version is wrong. "
                 "Please report new version to libvir-list@redhat.com",
                 path, virStorageFileFormatTypeToString(possibleFormat));

    /* No magic, so check file extension */
706
    for (i = 0; i < VIR_STORAGE_FILE_LAST; i++) {
E
Eric Blake 已提交
707 708 709 710 711 712
        if (virStorageFileMatchesExtension(i, path)) {
            format = i;
            goto cleanup;
        }
    }

713
 cleanup:
E
Eric Blake 已提交
714 715 716 717 718
    VIR_DEBUG("format=%d", format);
    return format;
}


719 720 721
static int
qcow2GetFeatures(virBitmapPtr *features,
                 int format,
E
Eric Blake 已提交
722
                 char *buf,
723 724 725 726 727
                 ssize_t len)
{
    int version = -1;
    virBitmapPtr feat = NULL;
    uint64_t bits;
728
    size_t i;
729 730 731 732 733 734 735 736 737

    version = virReadBufInt32BE(buf + fileTypeInfo[format].versionOffset);

    if (version == 2)
        return 0;

    if (len < QCOW2v3_HDR_SIZE)
        return -1;

738
    if (!(feat = virBitmapNew(VIR_STORAGE_FILE_FEATURE_LAST)))
739 740 741 742 743 744 745 746 747 748 749 750 751 752
        return -1;

    /* todo: check for incompatible or autoclear features? */
    bits = virReadBufInt64BE(buf + QCOW2v3_HDR_FEATURES_COMPATIBLE);
    for (i = 0; i < QCOW2_COMPATIBLE_FEATURE_LAST; i++) {
        if (bits & ((uint64_t) 1 << i))
            ignore_value(virBitmapSetBit(feat, qcow2CompatibleFeatureArray[i]));
    }

    *features = feat;
    return 0;
}


753 754 755 756 757
/* Given a header in BUF with length LEN, as parsed from the storage file
 * assuming it has the given FORMAT, populate information into META
 * with information about the file and its backing store. Return format
 * of the backing store as BACKING_FORMAT. PATH and FORMAT have to be
 * pre-populated in META */
758
int
759
virStorageFileGetMetadataInternal(virStorageSourcePtr meta,
760 761
                                  char *buf,
                                  size_t len,
762
                                  int *backingFormat)
763
{
764
    int ret = -1;
E
Eric Blake 已提交
765

766 767
    VIR_DEBUG("relPath=%s, buf=%p, len=%zu, meta->format=%d",
              meta->relPath, buf, len, meta->format);
E
Eric Blake 已提交
768

769
    if (meta->format == VIR_STORAGE_FILE_AUTO)
770
        meta->format = virStorageFileProbeFormatFromBuf(meta->relPath, buf, len);
771

772 773 774 775
    if (meta->format <= VIR_STORAGE_FILE_NONE ||
        meta->format >= VIR_STORAGE_FILE_LAST) {
        virReportSystemError(EINVAL, _("unknown storage file meta->format %d"),
                             meta->format);
E
Eric Blake 已提交
776 777
        goto cleanup;
    }
778

779 780 781
    /* XXX we should consider moving virStorageBackendUpdateVolInfo
     * code into this method, for non-magic files
     */
782
    if (!fileTypeInfo[meta->format].magic)
E
Eric Blake 已提交
783
        goto done;
784

785
    /* Optionally extract capacity from file */
786 787
    if (fileTypeInfo[meta->format].sizeOffset != -1) {
        if ((fileTypeInfo[meta->format].sizeOffset + 8) > len)
E
Eric Blake 已提交
788
            goto done;
789

790
        if (fileTypeInfo[meta->format].endian == LV_LITTLE_ENDIAN)
E
Eric Blake 已提交
791
            meta->capacity = virReadBufInt64LE(buf +
792
                                               fileTypeInfo[meta->format].sizeOffset);
E
Eric Blake 已提交
793 794
        else
            meta->capacity = virReadBufInt64BE(buf +
795
                                               fileTypeInfo[meta->format].sizeOffset);
796
        /* Avoid unlikely, but theoretically possible overflow */
E
Eric Blake 已提交
797
        if (meta->capacity > (ULLONG_MAX /
798
                              fileTypeInfo[meta->format].sizeMultiplier))
E
Eric Blake 已提交
799
            goto done;
800
        meta->capacity *= fileTypeInfo[meta->format].sizeMultiplier;
801
    }
802

803
    if (fileTypeInfo[meta->format].qcowCryptOffset != -1) {
804
        int crypt_format;
805

E
Eric Blake 已提交
806
        crypt_format = virReadBufInt32BE(buf +
807
                                         fileTypeInfo[meta->format].qcowCryptOffset);
808 809
        if (crypt_format && !meta->encryption &&
            VIR_ALLOC(meta->encryption) < 0)
810
            goto cleanup;
811
    }
812

813 814
    if (fileTypeInfo[meta->format].getBackingStore != NULL) {
        int store = fileTypeInfo[meta->format].getBackingStore(&meta->backingStoreRaw,
815
                                                         backingFormat,
E
Eric Blake 已提交
816 817 818
                                                         buf, len);
        if (store == BACKING_STORE_INVALID)
            goto done;
819

E
Eric Blake 已提交
820 821
        if (store == BACKING_STORE_ERROR)
            goto cleanup;
822 823
    }

824 825
    if (fileTypeInfo[meta->format].getFeatures != NULL &&
        fileTypeInfo[meta->format].getFeatures(&meta->features, meta->format, buf, len) < 0)
826 827
        goto cleanup;

828
    if (meta->format == VIR_STORAGE_FILE_QCOW2 && meta->features &&
829 830 831
        VIR_STRDUP(meta->compat, "1.1") < 0)
        goto cleanup;

832
 done:
833
    ret = 0;
E
Eric Blake 已提交
834

835
 cleanup:
E
Eric Blake 已提交
836
    return ret;
837 838 839 840
}


/**
841
 * virStorageFileProbeFormat:
842
 *
843 844
 * Probe for the format of 'path', returning the detected
 * disk format.
845 846 847
 *
 * Callers are advised never to trust the returned 'format'
 * unless it is listed as VIR_STORAGE_FILE_RAW, since a
848
 * malicious guest can turn a raw file into any other non-raw
849 850 851 852 853
 * format at will.
 *
 * Best option: Don't use this function
 */
int
854
virStorageFileProbeFormat(const char *path, uid_t uid, gid_t gid)
855
{
856
    int fd;
857
    int ret = -1;
858
    struct stat sb;
859 860
    ssize_t len = VIR_STORAGE_MAX_HEADER;
    char *header = NULL;
861

862 863
    if ((fd = virFileOpenAs(path, O_RDONLY, 0, uid, gid, 0)) < 0) {
        virReportSystemError(-fd, _("Failed to open file '%s'"), path);
864 865 866
        return -1;
    }

867 868 869 870 871
    if (fstat(fd, &sb) < 0) {
        virReportSystemError(errno, _("cannot stat file '%s'"), path);
        goto cleanup;
    }

872 873
    /* No header to probe for directories */
    if (S_ISDIR(sb.st_mode)) {
874 875
        ret = VIR_STORAGE_FILE_DIR;
        goto cleanup;
876
    }
877 878 879 880 881 882

    if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
        virReportSystemError(errno, _("cannot set to start of '%s'"), path);
        goto cleanup;
    }

883
    if ((len = virFileReadHeaderFD(fd, len, &header)) < 0) {
884 885 886 887
        virReportSystemError(errno, _("cannot read header '%s'"), path);
        goto cleanup;
    }

888
    ret = virStorageFileProbeFormatFromBuf(path, header, len);
889

890
 cleanup:
891
    VIR_FREE(header);
892
    VIR_FORCE_CLOSE(fd);
893 894 895 896

    return ret;
}

897

898
static virStorageSourcePtr
899 900 901
virStorageFileMetadataNew(const char *path,
                          int format)
{
902
    virStorageSourcePtr ret = NULL;
903 904 905 906 907 908 909

    if (VIR_ALLOC(ret) < 0)
        return NULL;

    ret->format = format;
    ret->type = VIR_STORAGE_TYPE_FILE;

910
    if (VIR_STRDUP(ret->relPath, path) < 0)
911 912
        goto error;

913 914
    if (VIR_STRDUP(ret->path, path) < 0)
        goto error;
915 916 917 918

    return ret;

 error:
919
    virStorageSourceFree(ret);
920 921 922 923
    return NULL;
}


924 925 926 927 928
/**
 * virStorageFileGetMetadataFromBuf:
 * @path: name of file, for error messages
 * @buf: header bytes from @path
 * @len: length of @buf
929 930
 * @backing: output malloc'd name of backing image, if any
 * @backingFormat: format of @backing
931
 *
E
Eric Blake 已提交
932 933 934 935
 * Extract metadata about the storage volume, including probing its
 * format.  Does not recurse.  Callers are advised not to trust the
 * learned format if a guest has ever used the volume when it was
 * raw, since a malicious guest can turn a raw file into any
936 937
 * other non-raw format at will.
 *
938
 * If the returned @backingFormat is VIR_STORAGE_FILE_AUTO
939 940 941 942
 * it indicates the image didn't specify an explicit format for its
 * backing store. Callers are advised against probing for the
 * backing store format in this case.
 *
943
 * Caller MUST free the result after use via virStorageSourceFree.
944
 */
945
virStorageSourcePtr
946 947 948
virStorageFileGetMetadataFromBuf(const char *path,
                                 char *buf,
                                 size_t len,
949 950
                                 char **backing,
                                 int *backingFormat)
951
{
952
    virStorageSourcePtr ret = NULL;
953
    virStorageSourcePtr meta = NULL;
954

E
Eric Blake 已提交
955
    if (!(meta = virStorageFileMetadataNew(path, VIR_STORAGE_FILE_AUTO)))
956
        return NULL;
957

958 959 960 961 962
    if (virStorageFileGetMetadataInternal(meta, buf, len,
                                          backingFormat) < 0)
        goto cleanup;
    if (VIR_STRDUP(*backing, meta->backingStoreRaw) < 0)
        goto cleanup;
963

964 965 966 967
    ret = meta;
    meta = NULL;
 cleanup:
    virStorageSourceFree(meta);
968
    return ret;
969 970 971 972
}


/* Internal version that also supports a containing directory name.  */
973
static int
974
virStorageFileGetMetadataFromFDInternal(virStorageSourcePtr meta,
975
                                        int fd,
976
                                        int *backingFormat)
977 978
{
    char *buf = NULL;
979
    ssize_t len = VIR_STORAGE_MAX_HEADER;
980
    struct stat sb;
981
    int ret = -1;
982
    int dummy;
983

984 985
    if (!backingFormat)
        backingFormat = &dummy;
986

987
    *backingFormat = VIR_STORAGE_FILE_NONE;
988

989 990 991
    if (fstat(fd, &sb) < 0) {
        virReportSystemError(errno,
                             _("cannot stat file '%s'"),
992
                             meta->relPath);
993
        return -1;
994 995 996
    }

    if (S_ISDIR(sb.st_mode)) {
997 998
        /* No header to probe for directories, but also no backing file. Just
         * update the metadata.*/
999 1000
        meta->type = VIR_STORAGE_TYPE_DIR;
        meta->format = VIR_STORAGE_FILE_DIR;
1001
        ret = 0;
1002 1003 1004 1005
        goto cleanup;
    }

    if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
1006
        virReportSystemError(errno, _("cannot seek to start of '%s'"), meta->relPath);
1007 1008 1009 1010
        goto cleanup;
    }

    if ((len = virFileReadHeaderFD(fd, len, &buf)) < 0) {
1011
        virReportSystemError(errno, _("cannot read header '%s'"), meta->relPath);
1012 1013 1014
        goto cleanup;
    }

1015
    ret = virStorageFileGetMetadataInternal(meta, buf, len, backingFormat);
1016

1017 1018 1019 1020 1021 1022
    if (ret == 0) {
        if (S_ISREG(sb.st_mode))
            meta->type = VIR_STORAGE_TYPE_FILE;
        else if (S_ISBLK(sb.st_mode))
            meta->type = VIR_STORAGE_TYPE_BLOCK;
    }
1023
 cleanup:
1024 1025 1026 1027 1028
    VIR_FREE(buf);
    return ret;
}


1029 1030 1031
/**
 * virStorageFileGetMetadataFromFD:
 *
1032 1033
 * Extract metadata about the storage volume with the specified
 * image format. If image format is VIR_STORAGE_FILE_AUTO, it
1034
 * will probe to automatically identify the format.  Does not recurse.
1035
 *
1036 1037 1038 1039
 * Callers are advised never to use VIR_STORAGE_FILE_AUTO as a
 * format, since a malicious guest can turn a raw file into any
 * other non-raw format at will.
 *
1040
 * Caller MUST free the result after use via virStorageSourceFree.
1041
 */
1042
virStorageSourcePtr
1043 1044
virStorageFileGetMetadataFromFD(const char *path,
                                int fd,
1045 1046 1047
                                int format,
                                int *backingFormat)

1048
{
1049
    virStorageSourcePtr ret = NULL;
1050
    char *canonPath = NULL;
1051

1052 1053
    if (!(canonPath = canonicalize_file_name(path))) {
        virReportSystemError(errno, _("unable to resolve '%s'"), path);
1054
        goto cleanup;
1055 1056 1057 1058 1059
    }

    if (!(ret = virStorageFileMetadataNew(canonPath, format)))
        goto cleanup;

1060

1061
    if (virStorageFileGetMetadataFromFDInternal(ret, fd, backingFormat) < 0) {
1062
        virStorageSourceFree(ret);
1063 1064 1065 1066
        ret = NULL;
    }

 cleanup:
1067
    VIR_FREE(canonPath);
1068
    return ret;
1069 1070
}

1071

1072 1073 1074 1075 1076
/**
 * virStorageFileChainCheckBroken
 *
 * If CHAIN is broken, set *brokenFile to the broken file name,
 * otherwise set it to NULL. Caller MUST free *brokenFile after use.
1077 1078
 * Return 0 on success (including when brokenFile is set), negative on
 * error (allocation failure).
1079 1080
 */
int
1081
virStorageFileChainGetBroken(virStorageSourcePtr chain,
1082 1083
                             char **brokenFile)
{
1084
    virStorageSourcePtr tmp;
1085

1086 1087
    *brokenFile = NULL;

1088 1089 1090
    if (!chain)
        return 0;

1091
    for (tmp = chain; tmp; tmp = tmp->backingStore) {
1092 1093
        /* Break when we hit end of chain; report error if we detected
         * a missing backing file, infinite loop, or other error */
1094
        if (!tmp->backingStore && tmp->backingStoreRaw) {
1095 1096
            if (VIR_STRDUP(*brokenFile, tmp->backingStoreRaw) < 0)
                return -1;
1097

1098 1099 1100
           return 0;
        }
    }
1101

1102
    return 0;
1103 1104 1105
}


1106 1107 1108 1109 1110 1111
/**
 * virStorageFileResize:
 *
 * Change the capacity of the raw storage file at 'path'.
 */
int
1112 1113 1114 1115
virStorageFileResize(const char *path,
                     unsigned long long capacity,
                     unsigned long long orig_capacity,
                     bool pre_allocate)
1116
{
1117 1118
    int fd = -1;
    int ret = -1;
1119 1120 1121 1122 1123 1124
    int rc ATTRIBUTE_UNUSED;
    off_t offset ATTRIBUTE_UNUSED;
    off_t len ATTRIBUTE_UNUSED;

    offset = orig_capacity;
    len = capacity - orig_capacity;
1125 1126 1127 1128 1129 1130

    if ((fd = open(path, O_RDWR)) < 0) {
        virReportSystemError(errno, _("Unable to open '%s'"), path);
        goto cleanup;
    }

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    if (pre_allocate) {
#if HAVE_POSIX_FALLOCATE
        if ((rc = posix_fallocate(fd, offset, len)) != 0) {
            virReportSystemError(rc,
                                 _("Failed to pre-allocate space for "
                                   "file '%s'"), path);
            goto cleanup;
        }
#elif HAVE_SYS_SYSCALL_H && defined(SYS_fallocate)
        if (syscall(SYS_fallocate, fd, 0, offset, len) != 0) {
            virReportSystemError(errno,
1142
                                 _("Failed to pre-allocate space for "
1143 1144 1145 1146 1147
                                   "file '%s'"), path);
            goto cleanup;
        }
#else
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
1148
                       _("preallocate is not supported on this platform"));
1149
        goto cleanup;
1150 1151 1152 1153 1154 1155 1156
#endif
    } else {
        if (ftruncate(fd, capacity) < 0) {
            virReportSystemError(errno,
                                 _("Failed to truncate file '%s'"), path);
            goto cleanup;
        }
1157 1158
    }

1159 1160 1161 1162 1163 1164 1165
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno, _("Unable to save '%s'"), path);
        goto cleanup;
    }

    ret = 0;

1166
 cleanup:
1167 1168
    VIR_FORCE_CLOSE(fd);
    return ret;
1169 1170
}

1171 1172 1173 1174 1175 1176

int virStorageFileIsClusterFS(const char *path)
{
    /* These are coherent cluster filesystems known to be safe for
     * migration with cache != none
     */
1177 1178 1179
    return virFileIsSharedFSType(path,
                                 VIR_FILE_SHFS_GFS2 |
                                 VIR_FILE_SHFS_OCFS);
1180
}
1181 1182

#ifdef LVS
1183 1184
int virStorageFileGetLVMKey(const char *path,
                            char **key)
1185 1186 1187 1188 1189
{
    /*
     *  # lvs --noheadings --unbuffered --nosuffix --options "uuid" LVNAME
     *    06UgP5-2rhb-w3Bo-3mdR-WeoL-pytO-SAa2ky
     */
1190
    int status;
1191 1192 1193 1194 1195 1196
    virCommandPtr cmd = virCommandNewArgList(
        LVS,
        "--noheadings", "--unbuffered", "--nosuffix",
        "--options", "uuid", path,
        NULL
        );
1197 1198 1199
    int ret = -1;

    *key = NULL;
1200 1201

    /* Run the program and capture its output */
1202 1203
    virCommandSetOutputBuffer(cmd, key);
    if (virCommandRun(cmd, &status) < 0)
1204 1205
        goto cleanup;

1206 1207 1208 1209 1210 1211
    /* Explicitly check status == 0, rather than passing NULL
     * to virCommandRun because we don't want to raise an actual
     * error in this scenario, just return a NULL key.
     */

    if (status == 0 && *key) {
1212
        char *nl;
1213
        char *tmp = *key;
1214 1215 1216 1217 1218 1219

        /* Find first non-space character */
        while (*tmp && c_isspace(*tmp)) {
            tmp++;
        }
        /* Kill leading spaces */
1220 1221
        if (tmp != *key)
            memmove(*key, tmp, strlen(tmp)+1);
1222 1223

        /* Kill trailing newline */
1224
        if ((nl = strchr(*key, '\n')))
1225 1226 1227
            *nl = '\0';
    }

1228
    ret = 0;
1229

1230
 cleanup:
1231 1232 1233
    if (*key && STREQ(*key, ""))
        VIR_FREE(*key);

1234 1235
    virCommandFree(cmd);

1236
    return ret;
1237 1238
}
#else
1239 1240
int virStorageFileGetLVMKey(const char *path,
                            char **key ATTRIBUTE_UNUSED)
1241 1242
{
    virReportSystemError(ENOSYS, _("Unable to get LVM key for %s"), path);
1243
    return -1;
1244 1245 1246
}
#endif

1247
#ifdef WITH_UDEV
1248 1249
int virStorageFileGetSCSIKey(const char *path,
                             char **key)
1250
{
1251
    int status;
1252 1253 1254 1255 1256 1257 1258
    virCommandPtr cmd = virCommandNewArgList(
        "/lib/udev/scsi_id",
        "--replace-whitespace",
        "--whitelisted",
        "--device", path,
        NULL
        );
1259 1260 1261
    int ret = -1;

    *key = NULL;
1262 1263

    /* Run the program and capture its output */
1264 1265
    virCommandSetOutputBuffer(cmd, key);
    if (virCommandRun(cmd, &status) < 0)
1266 1267
        goto cleanup;

1268 1269 1270 1271 1272 1273
    /* Explicitly check status == 0, rather than passing NULL
     * to virCommandRun because we don't want to raise an actual
     * error in this scenario, just return a NULL key.
     */
    if (status == 0 && *key) {
        char *nl = strchr(*key, '\n');
1274 1275 1276 1277
        if (nl)
            *nl = '\0';
    }

1278 1279
    ret = 0;

1280
 cleanup:
1281 1282 1283
    if (*key && STREQ(*key, ""))
        VIR_FREE(*key);

1284 1285
    virCommandFree(cmd);

1286
    return ret;
1287 1288
}
#else
1289 1290
int virStorageFileGetSCSIKey(const char *path,
                             char **key ATTRIBUTE_UNUSED)
1291 1292
{
    virReportSystemError(ENOSYS, _("Unable to get SCSI key for %s"), path);
1293
    return -1;
1294 1295
}
#endif
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
int
virStorageFileParseChainIndex(const char *diskTarget,
                              const char *name,
                              unsigned int *chainIndex)
{
    char **strings = NULL;
    unsigned int idx = 0;
    char *suffix;
    int ret = 0;

    *chainIndex = 0;

    if (name && diskTarget)
        strings = virStringSplit(name, "[", 2);

    if (virStringListLength(strings) != 2)
        goto cleanup;

E
Eric Blake 已提交
1315
    if (virStrToLong_uip(strings[1], &suffix, 10, &idx) < 0 ||
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
        STRNEQ(suffix, "]"))
        goto cleanup;

    if (STRNEQ(diskTarget, strings[0])) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested target '%s' does not match target '%s'"),
                       strings[0], diskTarget);
        ret = -1;
        goto cleanup;
    }

    *chainIndex = idx;

 cleanup:
    virStringFreeList(strings);
    return ret;
}

E
Eric Blake 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
/* Given a @chain, look for the backing store @name that is a backing file
 * of @startFrom (or any member of @chain if @startFrom is NULL) and return
 * that location within the chain.  @chain must always point to the top of
 * the chain.  Pass NULL for @name and 0 for @idx to find the base of the
 * chain.  Pass nonzero @idx to find the backing source according to its
 * position in the backing chain.  If @parent is not NULL, set *@parent to
 * the preferred name of the parent (or to NULL if @name matches the start
 * of the chain).  Since the results point within @chain, they must not be
 * independently freed. Reports an error and returns NULL if @name is not
 * found.
1344
 */
1345
virStorageSourcePtr
1346
virStorageFileChainLookup(virStorageSourcePtr chain,
1347
                          virStorageSourcePtr startFrom,
1348
                          const char *name,
1349
                          unsigned int idx,
1350 1351
                          const char **parent)
{
1352
    const char *start = chain->path;
1353
    const char *tmp;
E
Eric Blake 已提交
1354 1355
    const char *parentDir = ".";
    bool nameIsFile = virStorageIsFile(name);
1356
    size_t i;
1357 1358 1359 1360

    if (!parent)
        parent = &tmp;
    *parent = NULL;
1361 1362 1363

    i = 0;
    if (startFrom) {
E
Eric Blake 已提交
1364
        while (chain && chain != startFrom->backingStore) {
1365 1366 1367
            chain = chain->backingStore;
            i++;
        }
E
Eric Blake 已提交
1368
        *parent = startFrom->path;
1369 1370
    }

E
Eric Blake 已提交
1371
    while (chain) {
1372
        if (!name && !idx) {
1373
            if (!chain->backingStore)
1374
                break;
1375 1376 1377 1378
        } else if (idx) {
            VIR_DEBUG("%zu: %s", i, chain->path);
            if (idx == i)
                break;
E
Eric Blake 已提交
1379
        } else {
1380
            if (STREQ_NULLABLE(name, chain->relPath))
1381
                break;
E
Eric Blake 已提交
1382 1383 1384
            if (nameIsFile && (chain->type == VIR_STORAGE_TYPE_FILE ||
                               chain->type == VIR_STORAGE_TYPE_BLOCK)) {
                int result = virFileRelLinkPointsTo(parentDir, name,
1385
                                                    chain->path);
E
Eric Blake 已提交
1386 1387 1388 1389 1390
                if (result < 0)
                    goto error;
                if (result > 0)
                    break;
            }
1391
        }
1392
        *parent = chain->path;
E
Eric Blake 已提交
1393
        parentDir = chain->relDir;
1394
        chain = chain->backingStore;
1395
        i++;
1396
    }
1397

E
Eric Blake 已提交
1398
    if (!chain)
1399
        goto error;
1400
    return chain;
1401

1402
 error:
1403 1404 1405 1406 1407
    if (idx) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("could not find backing store %u in chain for '%s'"),
                       idx, start);
    } else if (name) {
E
Eric Blake 已提交
1408 1409 1410 1411 1412 1413 1414 1415
        if (startFrom)
            virReportError(VIR_ERR_INVALID_ARG,
                           _("could not find image '%s' beneath '%s' in "
                             "chain for '%s'"), name, startFrom->path, start);
        else
            virReportError(VIR_ERR_INVALID_ARG,
                           _("could not find image '%s' in chain for '%s'"),
                           name, start);
1416
    } else {
1417 1418 1419
        virReportError(VIR_ERR_INVALID_ARG,
                       _("could not find base image in chain for '%s'"),
                       start);
1420
    }
1421 1422 1423
    *parent = NULL;
    return NULL;
}
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485


void
virStorageNetHostDefClear(virStorageNetHostDefPtr def)
{
    if (!def)
        return;

    VIR_FREE(def->name);
    VIR_FREE(def->port);
    VIR_FREE(def->socket);
}


void
virStorageNetHostDefFree(size_t nhosts,
                         virStorageNetHostDefPtr hosts)
{
    size_t i;

    if (!hosts)
        return;

    for (i = 0; i < nhosts; i++)
        virStorageNetHostDefClear(&hosts[i]);

    VIR_FREE(hosts);
}


virStorageNetHostDefPtr
virStorageNetHostDefCopy(size_t nhosts,
                         virStorageNetHostDefPtr hosts)
{
    virStorageNetHostDefPtr ret = NULL;
    size_t i;

    if (VIR_ALLOC_N(ret, nhosts) < 0)
        goto error;

    for (i = 0; i < nhosts; i++) {
        virStorageNetHostDefPtr src = &hosts[i];
        virStorageNetHostDefPtr dst = &ret[i];

        dst->transport = src->transport;

        if (VIR_STRDUP(dst->name, src->name) < 0)
            goto error;

        if (VIR_STRDUP(dst->port, src->port) < 0)
            goto error;

        if (VIR_STRDUP(dst->socket, src->socket) < 0)
            goto error;
    }

    return ret;

 error:
    virStorageNetHostDefFree(nhosts, ret);
    return NULL;
}
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512


void
virStorageSourcePoolDefFree(virStorageSourcePoolDefPtr def)
{
    if (!def)
        return;

    VIR_FREE(def->pool);
    VIR_FREE(def->volume);

    VIR_FREE(def);
}


void
virStorageSourceAuthClear(virStorageSourcePtr def)
{
    VIR_FREE(def->auth.username);

    if (def->auth.secretType == VIR_STORAGE_SECRET_TYPE_USAGE)
        VIR_FREE(def->auth.secret.usage);

    def->auth.secretType = VIR_STORAGE_SECRET_TYPE_NONE;
}


1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
int
virStorageSourceGetActualType(virStorageSourcePtr def)
{
    if (def->type == VIR_STORAGE_TYPE_VOLUME && def->srcpool)
        return def->srcpool->actualtype;

    return def->type;
}


1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
/**
 * virStorageSourceClearBackingStore:
 *
 * @src: disk source to clear
 *
 * Clears information about backing store of the current storage file.
 */
void
virStorageSourceClearBackingStore(virStorageSourcePtr def)
{
    if (!def)
        return;

    VIR_FREE(def->relPath);
    VIR_FREE(def->relDir);
    VIR_FREE(def->backingStoreRaw);

    /* recursively free backing chain */
    virStorageSourceFree(def->backingStore);
    def->backingStore = NULL;
}


1546 1547 1548 1549 1550 1551 1552 1553 1554
void
virStorageSourceClear(virStorageSourcePtr def)
{
    size_t i;

    if (!def)
        return;

    VIR_FREE(def->path);
1555
    VIR_FREE(def->volume);
1556 1557
    virStorageSourcePoolDefFree(def->srcpool);
    VIR_FREE(def->driverName);
E
Eric Blake 已提交
1558 1559
    virBitmapFree(def->features);
    VIR_FREE(def->compat);
1560 1561 1562 1563 1564 1565 1566
    virStorageEncryptionFree(def->encryption);

    if (def->seclabels) {
        for (i = 0; i < def->nseclabels; i++)
            virSecurityDeviceLabelDefFree(def->seclabels[i]);
        VIR_FREE(def->seclabels);
    }
E
Eric Blake 已提交
1567 1568 1569 1570 1571
    if (def->perms) {
        VIR_FREE(def->perms->label);
        VIR_FREE(def->perms);
    }
    VIR_FREE(def->timestamps);
1572 1573 1574

    virStorageNetHostDefFree(def->nhosts, def->hosts);
    virStorageSourceAuthClear(def);
1575

1576
    virStorageSourceClearBackingStore(def);
1577
}
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588


void
virStorageSourceFree(virStorageSourcePtr def)
{
    if (!def)
        return;

    virStorageSourceClear(def);
    VIR_FREE(def);
}
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 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 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937


static virStorageSourcePtr
virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent,
                                       const char *rel)
{
    char *dirname = NULL;
    const char *parentdir = "";
    virStorageSourcePtr ret;

    if (VIR_ALLOC(ret) < 0)
        return NULL;

    ret->backingRelative = true;

    /* XXX Once we get rid of the need to use canonical names in path, we will be
     * able to use mdir_name on parent->path instead of using parent->relDir */
    if (STRNEQ(parent->relDir, "/"))
        parentdir = parent->relDir;

    if (virAsprintf(&ret->path, "%s/%s", parentdir, rel) < 0)
        goto error;

    if (virStorageSourceGetActualType(parent) != VIR_STORAGE_TYPE_NETWORK) {
        ret->type = VIR_STORAGE_TYPE_FILE;

        /* XXX store the relative directory name for test's sake */
        if (!(ret->relDir = mdir_name(ret->path))) {
            virReportOOMError();
            goto error;
        }

        /* XXX we don't currently need to store the canonical path but the
         * change would break the test suite. Get rid of this later */
        char *tmp = ret->path;
        if (!(ret->path = canonicalize_file_name(tmp))) {
            ret->path = tmp;
            tmp = NULL;
        }
        VIR_FREE(tmp);
    } else {
        ret->type = VIR_STORAGE_TYPE_NETWORK;

        /* copy the host network part */
        ret->protocol = parent->protocol;
        if (!(ret->hosts = virStorageNetHostDefCopy(parent->nhosts, parent->hosts)))
            goto error;
        ret->nhosts = parent->nhosts;

        if (VIR_STRDUP(ret->volume, parent->volume) < 0)
            goto error;

        /* XXX store the relative directory name for test's sake */
        if (!(ret->relDir = mdir_name(ret->path))) {
            virReportOOMError();
            goto error;
        }
    }

 cleanup:
    VIR_FREE(dirname);
    return ret;

 error:
    virStorageSourceFree(ret);
    ret = NULL;
    goto cleanup;
}


static int
virStorageSourceParseBackingURI(virStorageSourcePtr src,
                                const char *path)
{
    virURIPtr uri = NULL;
    char **scheme = NULL;
    int ret = -1;

    if (!(uri = virURIParse(path))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to parse backing file location '%s'"),
                       path);
        goto cleanup;
    }

    if (!(scheme = virStringSplit(uri->scheme, "+", 2)))
        goto cleanup;

    if (!scheme[0] ||
        (src->protocol = virStorageNetProtocolTypeFromString(scheme[0])) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("invalid backing protocol '%s'"),
                       NULLSTR(scheme[0]));
        goto cleanup;
    }

    if (scheme[1] &&
        (src->hosts->transport = virStorageNetHostTransportTypeFromString(scheme[1])) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("invalid protocol transport type '%s'"),
                       scheme[1]);
        goto cleanup;
    }

    /* handle socket stored as a query */
    if (uri->query) {
        if (VIR_STRDUP(src->hosts->socket, STRSKIP(uri->query, "socket=")) < 0)
            goto cleanup;
    }

    /* XXX We currently don't support auth, so don't bother parsing it */

    /* possibly skip the leading slash */
    if (VIR_STRDUP(src->path,
                   *uri->path == '/' ? uri->path + 1 : uri->path) < 0)
        goto cleanup;

    if (src->protocol == VIR_STORAGE_NET_PROTOCOL_GLUSTER) {
        char *tmp;
        if (!(tmp = strchr(src->path, '/')) ||
            tmp == src->path) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("missing volume name or file name in "
                             "gluster source path '%s'"), src->path);
            goto cleanup;
        }

        src->volume = src->path;

        if (VIR_STRDUP(src->path, tmp) < 0)
            goto cleanup;

        tmp[0] = '\0';
    }

    if (VIR_ALLOC(src->hosts) < 0)
        goto cleanup;

    src->nhosts = 1;

    if (uri->port > 0) {
        if (virAsprintf(&src->hosts->port, "%d", uri->port) < 0)
            goto cleanup;
    }

    if (VIR_STRDUP(src->hosts->name, uri->server) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    virURIFree(uri);
    virStringFreeList(scheme);
    return ret;
}


static int
virStorageSourceParseBackingColon(virStorageSourcePtr src,
                                  const char *path)
{
    char **backing = NULL;
    int ret = -1;

    if (!(backing = virStringSplit(path, ":", 0)))
        goto cleanup;

    if (!backing[0] ||
        (src->protocol = virStorageNetProtocolTypeFromString(backing[0])) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("invalid backing protocol '%s'"),
                       NULLSTR(backing[0]));
        goto cleanup;
    }

    switch ((virStorageNetProtocol) src->protocol) {
    case VIR_STORAGE_NET_PROTOCOL_NBD:
        if (VIR_ALLOC_N(src->hosts, 1) < 0)
            goto cleanup;
        src->nhosts = 1;
        src->hosts->transport = VIR_STORAGE_NET_HOST_TRANS_TCP;

        /* format: [] denotes optional sections, uppercase are variable strings
         * nbd:unix:/PATH/TO/SOCKET[:exportname=EXPORTNAME]
         * nbd:HOSTNAME:PORT[:exportname=EXPORTNAME]
         */
        if (!backing[1]) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing remote information in '%s' for protocol nbd"),
                           path);
            goto cleanup;
        } else if (STREQ(backing[1], "unix")) {
            if (!backing[2]) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("missing unix socket path in nbd backing string %s"),
                               path);
                goto cleanup;
            }

            if (VIR_STRDUP(src->hosts->socket, backing[2]) < 0)
                goto cleanup;

       } else {
            if (!backing[1]) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("missing host name in nbd string '%s'"),
                               path);
                goto cleanup;
            }

            if (VIR_STRDUP(src->hosts->name, backing[1]) < 0)
                goto cleanup;

            if (!backing[2]) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("missing port in nbd string '%s'"),
                               path);
                goto cleanup;
            }

            if (VIR_STRDUP(src->hosts->port, backing[2]) < 0)
                goto cleanup;
        }

        if (backing[3] && STRPREFIX(backing[3], "exportname=")) {
            if (VIR_STRDUP(src->path, backing[3] + strlen("exportname=")) < 0)
                goto cleanup;
        }
     break;

    case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
    case VIR_STORAGE_NET_PROTOCOL_RBD:
    case VIR_STORAGE_NET_PROTOCOL_LAST:
    case VIR_STORAGE_NET_PROTOCOL_NONE:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("backing store parser is not implemented for protocol %s"),
                       backing[0]);
        goto cleanup;

    case VIR_STORAGE_NET_PROTOCOL_HTTP:
    case VIR_STORAGE_NET_PROTOCOL_HTTPS:
    case VIR_STORAGE_NET_PROTOCOL_FTP:
    case VIR_STORAGE_NET_PROTOCOL_FTPS:
    case VIR_STORAGE_NET_PROTOCOL_TFTP:
    case VIR_STORAGE_NET_PROTOCOL_ISCSI:
    case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("malformed backing store path for protocol %s"),
                       backing[0]);
        goto cleanup;
    }

    ret = 0;

 cleanup:
    virStringFreeList(backing);
    return ret;

}


static virStorageSourcePtr
virStorageSourceNewFromBackingAbsolute(const char *path)
{
    virStorageSourcePtr ret;

    if (VIR_ALLOC(ret) < 0)
        return NULL;

    if (virStorageIsFile(path)) {
        ret->type = VIR_STORAGE_TYPE_FILE;

        /* XXX store the relative directory name for test's sake */
        if (!(ret->relDir = mdir_name(path))) {
            virReportOOMError();
            goto error;
        }

        /* XXX we don't currently need to store the canonical path but the
         * change would break the test suite. Get rid of this later */
        if (!(ret->path = canonicalize_file_name(path))) {
            if (VIR_STRDUP(ret->path, path) < 0)
                goto error;
        }
    } else {
        ret->type = VIR_STORAGE_TYPE_NETWORK;

        /* handle URI formatted backing stores */
        if (strstr(path, "://")) {
            if (virStorageSourceParseBackingURI(ret, path) < 0)
                goto error;
        } else {
            if (virStorageSourceParseBackingColon(ret, path) < 0)
                goto error;
        }

        /* XXX fill relative path so that relative names work with network storage too */
        if (ret->path) {
            if (!(ret->relDir = mdir_name(ret->path))) {
                virReportOOMError();
                goto error;
            }
        } else {
            if (VIR_STRDUP(ret->relDir, "") < 0)
                goto error;
        }
    }

    return ret;

 error:
    virStorageSourceFree(ret);
    return NULL;
}


virStorageSourcePtr
virStorageSourceNewFromBacking(virStorageSourcePtr parent)
{
    struct stat st;
    virStorageSourcePtr ret;

    if (virStorageIsRelative(parent->backingStoreRaw))
        ret = virStorageSourceNewFromBackingRelative(parent,
                                                     parent->backingStoreRaw);
    else
        ret = virStorageSourceNewFromBackingAbsolute(parent->backingStoreRaw);

    if (ret) {
        if (VIR_STRDUP(ret->relPath, parent->backingStoreRaw) < 0) {
            virStorageSourceFree(ret);
            return NULL;
        }

        /* possibly update local type */
        if (ret->type == VIR_STORAGE_TYPE_FILE) {
            if (stat(ret->path, &st) == 0) {
                if (S_ISDIR(st.st_mode)) {
                    ret->type = VIR_STORAGE_TYPE_DIR;
                    ret->format = VIR_STORAGE_FILE_DIR;
                } else if (S_ISBLK(st.st_mode)) {
                    ret->type = VIR_STORAGE_TYPE_BLOCK;
                }
            }
        }
    }

    return ret;
}