Statistics
| Branch: | Revision:

root / block / vmdk.c @ 34b5d2c6

History | View | Annotate | Download (55.2 kB)

1
/*
2
 * Block driver for the VMDK format
3
 *
4
 * Copyright (c) 2004 Fabrice Bellard
5
 * Copyright (c) 2005 Filip Navara
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25

    
26
#include "qemu-common.h"
27
#include "block/block_int.h"
28
#include "qemu/module.h"
29
#include "migration/migration.h"
30
#include <zlib.h>
31

    
32
#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
33
#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
34
#define VMDK4_COMPRESSION_DEFLATE 1
35
#define VMDK4_FLAG_NL_DETECT (1 << 0)
36
#define VMDK4_FLAG_RGD (1 << 1)
37
/* Zeroed-grain enable bit */
38
#define VMDK4_FLAG_ZERO_GRAIN   (1 << 2)
39
#define VMDK4_FLAG_COMPRESS (1 << 16)
40
#define VMDK4_FLAG_MARKER (1 << 17)
41
#define VMDK4_GD_AT_END 0xffffffffffffffffULL
42

    
43
#define VMDK_GTE_ZEROED 0x1
44

    
45
/* VMDK internal error codes */
46
#define VMDK_OK      0
47
#define VMDK_ERROR   (-1)
48
/* Cluster not allocated */
49
#define VMDK_UNALLOC (-2)
50
#define VMDK_ZEROED  (-3)
51

    
52
#define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
53

    
54
typedef struct {
55
    uint32_t version;
56
    uint32_t flags;
57
    uint32_t disk_sectors;
58
    uint32_t granularity;
59
    uint32_t l1dir_offset;
60
    uint32_t l1dir_size;
61
    uint32_t file_sectors;
62
    uint32_t cylinders;
63
    uint32_t heads;
64
    uint32_t sectors_per_track;
65
} QEMU_PACKED VMDK3Header;
66

    
67
typedef struct {
68
    uint32_t version;
69
    uint32_t flags;
70
    uint64_t capacity;
71
    uint64_t granularity;
72
    uint64_t desc_offset;
73
    uint64_t desc_size;
74
    /* Number of GrainTableEntries per GrainTable */
75
    uint32_t num_gtes_per_gt;
76
    uint64_t rgd_offset;
77
    uint64_t gd_offset;
78
    uint64_t grain_offset;
79
    char filler[1];
80
    char check_bytes[4];
81
    uint16_t compressAlgorithm;
82
} QEMU_PACKED VMDK4Header;
83

    
84
#define L2_CACHE_SIZE 16
85

    
86
typedef struct VmdkExtent {
87
    BlockDriverState *file;
88
    bool flat;
89
    bool compressed;
90
    bool has_marker;
91
    bool has_zero_grain;
92
    int version;
93
    int64_t sectors;
94
    int64_t end_sector;
95
    int64_t flat_start_offset;
96
    int64_t l1_table_offset;
97
    int64_t l1_backup_table_offset;
98
    uint32_t *l1_table;
99
    uint32_t *l1_backup_table;
100
    unsigned int l1_size;
101
    uint32_t l1_entry_sectors;
102

    
103
    unsigned int l2_size;
104
    uint32_t *l2_cache;
105
    uint32_t l2_cache_offsets[L2_CACHE_SIZE];
106
    uint32_t l2_cache_counts[L2_CACHE_SIZE];
107

    
108
    unsigned int cluster_sectors;
109
} VmdkExtent;
110

    
111
typedef struct BDRVVmdkState {
112
    CoMutex lock;
113
    uint64_t desc_offset;
114
    bool cid_updated;
115
    uint32_t parent_cid;
116
    int num_extents;
117
    /* Extent array with num_extents entries, ascend ordered by address */
118
    VmdkExtent *extents;
119
    Error *migration_blocker;
120
} BDRVVmdkState;
121

    
122
typedef struct VmdkMetaData {
123
    uint32_t offset;
124
    unsigned int l1_index;
125
    unsigned int l2_index;
126
    unsigned int l2_offset;
127
    int valid;
128
    uint32_t *l2_cache_entry;
129
} VmdkMetaData;
130

    
131
typedef struct VmdkGrainMarker {
132
    uint64_t lba;
133
    uint32_t size;
134
    uint8_t  data[0];
135
} QEMU_PACKED VmdkGrainMarker;
136

    
137
enum {
138
    MARKER_END_OF_STREAM    = 0,
139
    MARKER_GRAIN_TABLE      = 1,
140
    MARKER_GRAIN_DIRECTORY  = 2,
141
    MARKER_FOOTER           = 3,
142
};
143

    
144
static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
145
{
146
    uint32_t magic;
147

    
148
    if (buf_size < 4) {
149
        return 0;
150
    }
151
    magic = be32_to_cpu(*(uint32_t *)buf);
152
    if (magic == VMDK3_MAGIC ||
153
        magic == VMDK4_MAGIC) {
154
        return 100;
155
    } else {
156
        const char *p = (const char *)buf;
157
        const char *end = p + buf_size;
158
        while (p < end) {
159
            if (*p == '#') {
160
                /* skip comment line */
161
                while (p < end && *p != '\n') {
162
                    p++;
163
                }
164
                p++;
165
                continue;
166
            }
167
            if (*p == ' ') {
168
                while (p < end && *p == ' ') {
169
                    p++;
170
                }
171
                /* skip '\r' if windows line endings used. */
172
                if (p < end && *p == '\r') {
173
                    p++;
174
                }
175
                /* only accept blank lines before 'version=' line */
176
                if (p == end || *p != '\n') {
177
                    return 0;
178
                }
179
                p++;
180
                continue;
181
            }
182
            if (end - p >= strlen("version=X\n")) {
183
                if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
184
                    strncmp("version=2\n", p, strlen("version=2\n")) == 0) {
185
                    return 100;
186
                }
187
            }
188
            if (end - p >= strlen("version=X\r\n")) {
189
                if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
190
                    strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) {
191
                    return 100;
192
                }
193
            }
194
            return 0;
195
        }
196
        return 0;
197
    }
198
}
199

    
200
#define CHECK_CID 1
201

    
202
#define SECTOR_SIZE 512
203
#define DESC_SIZE (20 * SECTOR_SIZE)    /* 20 sectors of 512 bytes each */
204
#define BUF_SIZE 4096
205
#define HEADER_SIZE 512                 /* first sector of 512 bytes */
206

    
207
static void vmdk_free_extents(BlockDriverState *bs)
208
{
209
    int i;
210
    BDRVVmdkState *s = bs->opaque;
211
    VmdkExtent *e;
212

    
213
    for (i = 0; i < s->num_extents; i++) {
214
        e = &s->extents[i];
215
        g_free(e->l1_table);
216
        g_free(e->l2_cache);
217
        g_free(e->l1_backup_table);
218
        if (e->file != bs->file) {
219
            bdrv_unref(e->file);
220
        }
221
    }
222
    g_free(s->extents);
223
}
224

    
225
static void vmdk_free_last_extent(BlockDriverState *bs)
226
{
227
    BDRVVmdkState *s = bs->opaque;
228

    
229
    if (s->num_extents == 0) {
230
        return;
231
    }
232
    s->num_extents--;
233
    s->extents = g_realloc(s->extents, s->num_extents * sizeof(VmdkExtent));
234
}
235

    
236
static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
237
{
238
    char desc[DESC_SIZE];
239
    uint32_t cid = 0xffffffff;
240
    const char *p_name, *cid_str;
241
    size_t cid_str_size;
242
    BDRVVmdkState *s = bs->opaque;
243
    int ret;
244

    
245
    ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
246
    if (ret < 0) {
247
        return 0;
248
    }
249

    
250
    if (parent) {
251
        cid_str = "parentCID";
252
        cid_str_size = sizeof("parentCID");
253
    } else {
254
        cid_str = "CID";
255
        cid_str_size = sizeof("CID");
256
    }
257

    
258
    desc[DESC_SIZE - 1] = '\0';
259
    p_name = strstr(desc, cid_str);
260
    if (p_name != NULL) {
261
        p_name += cid_str_size;
262
        sscanf(p_name, "%x", &cid);
263
    }
264

    
265
    return cid;
266
}
267

    
268
static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
269
{
270
    char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
271
    char *p_name, *tmp_str;
272
    BDRVVmdkState *s = bs->opaque;
273
    int ret;
274

    
275
    ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
276
    if (ret < 0) {
277
        return ret;
278
    }
279

    
280
    desc[DESC_SIZE - 1] = '\0';
281
    tmp_str = strstr(desc, "parentCID");
282
    if (tmp_str == NULL) {
283
        return -EINVAL;
284
    }
285

    
286
    pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
287
    p_name = strstr(desc, "CID");
288
    if (p_name != NULL) {
289
        p_name += sizeof("CID");
290
        snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid);
291
        pstrcat(desc, sizeof(desc), tmp_desc);
292
    }
293

    
294
    ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
295
    if (ret < 0) {
296
        return ret;
297
    }
298

    
299
    return 0;
300
}
301

    
302
static int vmdk_is_cid_valid(BlockDriverState *bs)
303
{
304
#ifdef CHECK_CID
305
    BDRVVmdkState *s = bs->opaque;
306
    BlockDriverState *p_bs = bs->backing_hd;
307
    uint32_t cur_pcid;
308

    
309
    if (p_bs) {
310
        cur_pcid = vmdk_read_cid(p_bs, 0);
311
        if (s->parent_cid != cur_pcid) {
312
            /* CID not valid */
313
            return 0;
314
        }
315
    }
316
#endif
317
    /* CID valid */
318
    return 1;
319
}
320

    
321
/* Queue extents, if any, for reopen() */
322
static int vmdk_reopen_prepare(BDRVReopenState *state,
323
                               BlockReopenQueue *queue, Error **errp)
324
{
325
    BDRVVmdkState *s;
326
    int ret = -1;
327
    int i;
328
    VmdkExtent *e;
329

    
330
    assert(state != NULL);
331
    assert(state->bs != NULL);
332

    
333
    if (queue == NULL) {
334
        error_set(errp, ERROR_CLASS_GENERIC_ERROR,
335
                 "No reopen queue for VMDK extents");
336
        goto exit;
337
    }
338

    
339
    s = state->bs->opaque;
340

    
341
    assert(s != NULL);
342

    
343
    for (i = 0; i < s->num_extents; i++) {
344
        e = &s->extents[i];
345
        if (e->file != state->bs->file) {
346
            bdrv_reopen_queue(queue, e->file, state->flags);
347
        }
348
    }
349
    ret = 0;
350

    
351
exit:
352
    return ret;
353
}
354

    
355
static int vmdk_parent_open(BlockDriverState *bs)
356
{
357
    char *p_name;
358
    char desc[DESC_SIZE + 1];
359
    BDRVVmdkState *s = bs->opaque;
360
    int ret;
361

    
362
    desc[DESC_SIZE] = '\0';
363
    ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
364
    if (ret < 0) {
365
        return ret;
366
    }
367

    
368
    p_name = strstr(desc, "parentFileNameHint");
369
    if (p_name != NULL) {
370
        char *end_name;
371

    
372
        p_name += sizeof("parentFileNameHint") + 1;
373
        end_name = strchr(p_name, '\"');
374
        if (end_name == NULL) {
375
            return -EINVAL;
376
        }
377
        if ((end_name - p_name) > sizeof(bs->backing_file) - 1) {
378
            return -EINVAL;
379
        }
380

    
381
        pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
382
    }
383

    
384
    return 0;
385
}
386

    
387
/* Create and append extent to the extent array. Return the added VmdkExtent
388
 * address. return NULL if allocation failed. */
389
static int vmdk_add_extent(BlockDriverState *bs,
390
                           BlockDriverState *file, bool flat, int64_t sectors,
391
                           int64_t l1_offset, int64_t l1_backup_offset,
392
                           uint32_t l1_size,
393
                           int l2_size, uint64_t cluster_sectors,
394
                           VmdkExtent **new_extent)
395
{
396
    VmdkExtent *extent;
397
    BDRVVmdkState *s = bs->opaque;
398

    
399
    if (cluster_sectors > 0x200000) {
400
        /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
401
        error_report("invalid granularity, image may be corrupt");
402
        return -EINVAL;
403
    }
404
    if (l1_size > 512 * 1024 * 1024) {
405
        /* Although with big capacity and small l1_entry_sectors, we can get a
406
         * big l1_size, we don't want unbounded value to allocate the table.
407
         * Limit it to 512M, which is 16PB for default cluster and L2 table
408
         * size */
409
        error_report("L1 size too big");
410
        return -EFBIG;
411
    }
412

    
413
    s->extents = g_realloc(s->extents,
414
                              (s->num_extents + 1) * sizeof(VmdkExtent));
415
    extent = &s->extents[s->num_extents];
416
    s->num_extents++;
417

    
418
    memset(extent, 0, sizeof(VmdkExtent));
419
    extent->file = file;
420
    extent->flat = flat;
421
    extent->sectors = sectors;
422
    extent->l1_table_offset = l1_offset;
423
    extent->l1_backup_table_offset = l1_backup_offset;
424
    extent->l1_size = l1_size;
425
    extent->l1_entry_sectors = l2_size * cluster_sectors;
426
    extent->l2_size = l2_size;
427
    extent->cluster_sectors = cluster_sectors;
428

    
429
    if (s->num_extents > 1) {
430
        extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
431
    } else {
432
        extent->end_sector = extent->sectors;
433
    }
434
    bs->total_sectors = extent->end_sector;
435
    if (new_extent) {
436
        *new_extent = extent;
437
    }
438
    return 0;
439
}
440

    
441
static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent)
442
{
443
    int ret;
444
    int l1_size, i;
445

    
446
    /* read the L1 table */
447
    l1_size = extent->l1_size * sizeof(uint32_t);
448
    extent->l1_table = g_malloc(l1_size);
449
    ret = bdrv_pread(extent->file,
450
                    extent->l1_table_offset,
451
                    extent->l1_table,
452
                    l1_size);
453
    if (ret < 0) {
454
        goto fail_l1;
455
    }
456
    for (i = 0; i < extent->l1_size; i++) {
457
        le32_to_cpus(&extent->l1_table[i]);
458
    }
459

    
460
    if (extent->l1_backup_table_offset) {
461
        extent->l1_backup_table = g_malloc(l1_size);
462
        ret = bdrv_pread(extent->file,
463
                        extent->l1_backup_table_offset,
464
                        extent->l1_backup_table,
465
                        l1_size);
466
        if (ret < 0) {
467
            goto fail_l1b;
468
        }
469
        for (i = 0; i < extent->l1_size; i++) {
470
            le32_to_cpus(&extent->l1_backup_table[i]);
471
        }
472
    }
473

    
474
    extent->l2_cache =
475
        g_malloc(extent->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
476
    return 0;
477
 fail_l1b:
478
    g_free(extent->l1_backup_table);
479
 fail_l1:
480
    g_free(extent->l1_table);
481
    return ret;
482
}
483

    
484
static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
485
                                 BlockDriverState *file,
486
                                 int flags)
487
{
488
    int ret;
489
    uint32_t magic;
490
    VMDK3Header header;
491
    VmdkExtent *extent;
492

    
493
    ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
494
    if (ret < 0) {
495
        return ret;
496
    }
497
    ret = vmdk_add_extent(bs, file, false,
498
                          le32_to_cpu(header.disk_sectors),
499
                          le32_to_cpu(header.l1dir_offset) << 9,
500
                          0,
501
                          le32_to_cpu(header.l1dir_size),
502
                          4096,
503
                          le32_to_cpu(header.granularity),
504
                          &extent);
505
    if (ret < 0) {
506
        return ret;
507
    }
508
    ret = vmdk_init_tables(bs, extent);
509
    if (ret) {
510
        /* free extent allocated by vmdk_add_extent */
511
        vmdk_free_last_extent(bs);
512
    }
513
    return ret;
514
}
515

    
516
static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
517
                               uint64_t desc_offset);
518

    
519
static int vmdk_open_vmdk4(BlockDriverState *bs,
520
                           BlockDriverState *file,
521
                           int flags)
522
{
523
    int ret;
524
    uint32_t magic;
525
    uint32_t l1_size, l1_entry_sectors;
526
    VMDK4Header header;
527
    VmdkExtent *extent;
528
    int64_t l1_backup_offset = 0;
529

    
530
    ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
531
    if (ret < 0) {
532
        return ret;
533
    }
534
    if (header.capacity == 0) {
535
        uint64_t desc_offset = le64_to_cpu(header.desc_offset);
536
        if (desc_offset) {
537
            return vmdk_open_desc_file(bs, flags, desc_offset << 9);
538
        }
539
    }
540

    
541
    if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
542
        /*
543
         * The footer takes precedence over the header, so read it in. The
544
         * footer starts at offset -1024 from the end: One sector for the
545
         * footer, and another one for the end-of-stream marker.
546
         */
547
        struct {
548
            struct {
549
                uint64_t val;
550
                uint32_t size;
551
                uint32_t type;
552
                uint8_t pad[512 - 16];
553
            } QEMU_PACKED footer_marker;
554

    
555
            uint32_t magic;
556
            VMDK4Header header;
557
            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
558

    
559
            struct {
560
                uint64_t val;
561
                uint32_t size;
562
                uint32_t type;
563
                uint8_t pad[512 - 16];
564
            } QEMU_PACKED eos_marker;
565
        } QEMU_PACKED footer;
566

    
567
        ret = bdrv_pread(file,
568
            bs->file->total_sectors * 512 - 1536,
569
            &footer, sizeof(footer));
570
        if (ret < 0) {
571
            return ret;
572
        }
573

    
574
        /* Some sanity checks for the footer */
575
        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
576
            le32_to_cpu(footer.footer_marker.size) != 0  ||
577
            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
578
            le64_to_cpu(footer.eos_marker.val) != 0  ||
579
            le32_to_cpu(footer.eos_marker.size) != 0  ||
580
            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
581
        {
582
            return -EINVAL;
583
        }
584

    
585
        header = footer.header;
586
    }
587

    
588
    if (le32_to_cpu(header.version) >= 3) {
589
        char buf[64];
590
        snprintf(buf, sizeof(buf), "VMDK version %d",
591
                 le32_to_cpu(header.version));
592
        qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
593
                bs->device_name, "vmdk", buf);
594
        return -ENOTSUP;
595
    }
596

    
597
    if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
598
        error_report("L2 table size too big");
599
        return -EINVAL;
600
    }
601

    
602
    l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
603
                        * le64_to_cpu(header.granularity);
604
    if (l1_entry_sectors == 0) {
605
        return -EINVAL;
606
    }
607
    l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
608
                / l1_entry_sectors;
609
    if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
610
        l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
611
    }
612
    ret = vmdk_add_extent(bs, file, false,
613
                          le64_to_cpu(header.capacity),
614
                          le64_to_cpu(header.gd_offset) << 9,
615
                          l1_backup_offset,
616
                          l1_size,
617
                          le32_to_cpu(header.num_gtes_per_gt),
618
                          le64_to_cpu(header.granularity),
619
                          &extent);
620
    if (ret < 0) {
621
        return ret;
622
    }
623
    extent->compressed =
624
        le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
625
    extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
626
    extent->version = le32_to_cpu(header.version);
627
    extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
628
    ret = vmdk_init_tables(bs, extent);
629
    if (ret) {
630
        /* free extent allocated by vmdk_add_extent */
631
        vmdk_free_last_extent(bs);
632
    }
633
    return ret;
634
}
635

    
636
/* find an option value out of descriptor file */
637
static int vmdk_parse_description(const char *desc, const char *opt_name,
638
        char *buf, int buf_size)
639
{
640
    char *opt_pos, *opt_end;
641
    const char *end = desc + strlen(desc);
642

    
643
    opt_pos = strstr(desc, opt_name);
644
    if (!opt_pos) {
645
        return VMDK_ERROR;
646
    }
647
    /* Skip "=\"" following opt_name */
648
    opt_pos += strlen(opt_name) + 2;
649
    if (opt_pos >= end) {
650
        return VMDK_ERROR;
651
    }
652
    opt_end = opt_pos;
653
    while (opt_end < end && *opt_end != '"') {
654
        opt_end++;
655
    }
656
    if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
657
        return VMDK_ERROR;
658
    }
659
    pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
660
    return VMDK_OK;
661
}
662

    
663
/* Open an extent file and append to bs array */
664
static int vmdk_open_sparse(BlockDriverState *bs,
665
                            BlockDriverState *file,
666
                            int flags)
667
{
668
    uint32_t magic;
669

    
670
    if (bdrv_pread(file, 0, &magic, sizeof(magic)) != sizeof(magic)) {
671
        return -EIO;
672
    }
673

    
674
    magic = be32_to_cpu(magic);
675
    switch (magic) {
676
        case VMDK3_MAGIC:
677
            return vmdk_open_vmfs_sparse(bs, file, flags);
678
            break;
679
        case VMDK4_MAGIC:
680
            return vmdk_open_vmdk4(bs, file, flags);
681
            break;
682
        default:
683
            return -EMEDIUMTYPE;
684
            break;
685
    }
686
}
687

    
688
static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
689
        const char *desc_file_path)
690
{
691
    int ret;
692
    char access[11];
693
    char type[11];
694
    char fname[512];
695
    const char *p = desc;
696
    int64_t sectors = 0;
697
    int64_t flat_offset;
698
    char extent_path[PATH_MAX];
699
    BlockDriverState *extent_file;
700
    Error *local_err = NULL;
701

    
702
    while (*p) {
703
        /* parse extent line:
704
         * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
705
         * or
706
         * RW [size in sectors] SPARSE "file-name.vmdk"
707
         */
708
        flat_offset = -1;
709
        ret = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
710
                access, &sectors, type, fname, &flat_offset);
711
        if (ret < 4 || strcmp(access, "RW")) {
712
            goto next_line;
713
        } else if (!strcmp(type, "FLAT")) {
714
            if (ret != 5 || flat_offset < 0) {
715
                return -EINVAL;
716
            }
717
        } else if (ret != 4) {
718
            return -EINVAL;
719
        }
720

    
721
        if (sectors <= 0 ||
722
            (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
723
             strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) ||
724
            (strcmp(access, "RW"))) {
725
            goto next_line;
726
        }
727

    
728
        path_combine(extent_path, sizeof(extent_path),
729
                desc_file_path, fname);
730
        ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
731
                             &local_err);
732
        if (ret) {
733
            qerror_report_err(local_err);
734
            error_free(local_err);
735
            return ret;
736
        }
737

    
738
        /* save to extents array */
739
        if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
740
            /* FLAT extent */
741
            VmdkExtent *extent;
742

    
743
            ret = vmdk_add_extent(bs, extent_file, true, sectors,
744
                            0, 0, 0, 0, sectors, &extent);
745
            if (ret < 0) {
746
                return ret;
747
            }
748
            extent->flat_start_offset = flat_offset << 9;
749
        } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
750
            /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
751
            ret = vmdk_open_sparse(bs, extent_file, bs->open_flags);
752
            if (ret) {
753
                bdrv_unref(extent_file);
754
                return ret;
755
            }
756
        } else {
757
            fprintf(stderr,
758
                "VMDK: Not supported extent type \"%s\""".\n", type);
759
            return -ENOTSUP;
760
        }
761
next_line:
762
        /* move to next line */
763
        while (*p && *p != '\n') {
764
            p++;
765
        }
766
        p++;
767
    }
768
    return 0;
769
}
770

    
771
static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
772
                               uint64_t desc_offset)
773
{
774
    int ret;
775
    char *buf = NULL;
776
    char ct[128];
777
    BDRVVmdkState *s = bs->opaque;
778
    int64_t size;
779

    
780
    size = bdrv_getlength(bs->file);
781
    if (size < 0) {
782
        return -EINVAL;
783
    }
784

    
785
    size = MIN(size, 1 << 20);  /* avoid unbounded allocation */
786
    buf = g_malloc0(size + 1);
787

    
788
    ret = bdrv_pread(bs->file, desc_offset, buf, size);
789
    if (ret < 0) {
790
        goto exit;
791
    }
792
    if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
793
        ret = -EMEDIUMTYPE;
794
        goto exit;
795
    }
796
    if (strcmp(ct, "monolithicFlat") &&
797
        strcmp(ct, "vmfs") &&
798
        strcmp(ct, "vmfsSparse") &&
799
        strcmp(ct, "twoGbMaxExtentSparse") &&
800
        strcmp(ct, "twoGbMaxExtentFlat")) {
801
        fprintf(stderr,
802
                "VMDK: Not supported image type \"%s\""".\n", ct);
803
        ret = -ENOTSUP;
804
        goto exit;
805
    }
806
    s->desc_offset = 0;
807
    ret = vmdk_parse_extents(buf, bs, bs->file->filename);
808
exit:
809
    g_free(buf);
810
    return ret;
811
}
812

    
813
static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
814
                     Error **errp)
815
{
816
    int ret;
817
    BDRVVmdkState *s = bs->opaque;
818

    
819
    if (vmdk_open_sparse(bs, bs->file, flags) == 0) {
820
        s->desc_offset = 0x200;
821
    } else {
822
        ret = vmdk_open_desc_file(bs, flags, 0);
823
        if (ret) {
824
            goto fail;
825
        }
826
    }
827
    /* try to open parent images, if exist */
828
    ret = vmdk_parent_open(bs);
829
    if (ret) {
830
        goto fail;
831
    }
832
    s->parent_cid = vmdk_read_cid(bs, 1);
833
    qemu_co_mutex_init(&s->lock);
834

    
835
    /* Disable migration when VMDK images are used */
836
    error_set(&s->migration_blocker,
837
              QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
838
              "vmdk", bs->device_name, "live migration");
839
    migrate_add_blocker(s->migration_blocker);
840

    
841
    return 0;
842

    
843
fail:
844
    vmdk_free_extents(bs);
845
    return ret;
846
}
847

    
848
static int get_whole_cluster(BlockDriverState *bs,
849
                VmdkExtent *extent,
850
                uint64_t cluster_offset,
851
                uint64_t offset,
852
                bool allocate)
853
{
854
    int ret = VMDK_OK;
855
    uint8_t *whole_grain = NULL;
856

    
857
    /* we will be here if it's first write on non-exist grain(cluster).
858
     * try to read from parent image, if exist */
859
    if (bs->backing_hd) {
860
        whole_grain =
861
            qemu_blockalign(bs, extent->cluster_sectors << BDRV_SECTOR_BITS);
862
        if (!vmdk_is_cid_valid(bs)) {
863
            ret = VMDK_ERROR;
864
            goto exit;
865
        }
866

    
867
        /* floor offset to cluster */
868
        offset -= offset % (extent->cluster_sectors * 512);
869
        ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain,
870
                extent->cluster_sectors);
871
        if (ret < 0) {
872
            ret = VMDK_ERROR;
873
            goto exit;
874
        }
875

    
876
        /* Write grain only into the active image */
877
        ret = bdrv_write(extent->file, cluster_offset, whole_grain,
878
                extent->cluster_sectors);
879
        if (ret < 0) {
880
            ret = VMDK_ERROR;
881
            goto exit;
882
        }
883
    }
884
exit:
885
    qemu_vfree(whole_grain);
886
    return ret;
887
}
888

    
889
static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
890
{
891
    uint32_t offset;
892
    QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
893
    offset = cpu_to_le32(m_data->offset);
894
    /* update L2 table */
895
    if (bdrv_pwrite_sync(
896
                extent->file,
897
                ((int64_t)m_data->l2_offset * 512)
898
                    + (m_data->l2_index * sizeof(m_data->offset)),
899
                &offset, sizeof(offset)) < 0) {
900
        return VMDK_ERROR;
901
    }
902
    /* update backup L2 table */
903
    if (extent->l1_backup_table_offset != 0) {
904
        m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
905
        if (bdrv_pwrite_sync(
906
                    extent->file,
907
                    ((int64_t)m_data->l2_offset * 512)
908
                        + (m_data->l2_index * sizeof(m_data->offset)),
909
                    &offset, sizeof(offset)) < 0) {
910
            return VMDK_ERROR;
911
        }
912
    }
913
    if (m_data->l2_cache_entry) {
914
        *m_data->l2_cache_entry = offset;
915
    }
916

    
917
    return VMDK_OK;
918
}
919

    
920
static int get_cluster_offset(BlockDriverState *bs,
921
                                    VmdkExtent *extent,
922
                                    VmdkMetaData *m_data,
923
                                    uint64_t offset,
924
                                    int allocate,
925
                                    uint64_t *cluster_offset)
926
{
927
    unsigned int l1_index, l2_offset, l2_index;
928
    int min_index, i, j;
929
    uint32_t min_count, *l2_table;
930
    bool zeroed = false;
931

    
932
    if (m_data) {
933
        m_data->valid = 0;
934
    }
935
    if (extent->flat) {
936
        *cluster_offset = extent->flat_start_offset;
937
        return VMDK_OK;
938
    }
939

    
940
    offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
941
    l1_index = (offset >> 9) / extent->l1_entry_sectors;
942
    if (l1_index >= extent->l1_size) {
943
        return VMDK_ERROR;
944
    }
945
    l2_offset = extent->l1_table[l1_index];
946
    if (!l2_offset) {
947
        return VMDK_UNALLOC;
948
    }
949
    for (i = 0; i < L2_CACHE_SIZE; i++) {
950
        if (l2_offset == extent->l2_cache_offsets[i]) {
951
            /* increment the hit count */
952
            if (++extent->l2_cache_counts[i] == 0xffffffff) {
953
                for (j = 0; j < L2_CACHE_SIZE; j++) {
954
                    extent->l2_cache_counts[j] >>= 1;
955
                }
956
            }
957
            l2_table = extent->l2_cache + (i * extent->l2_size);
958
            goto found;
959
        }
960
    }
961
    /* not found: load a new entry in the least used one */
962
    min_index = 0;
963
    min_count = 0xffffffff;
964
    for (i = 0; i < L2_CACHE_SIZE; i++) {
965
        if (extent->l2_cache_counts[i] < min_count) {
966
            min_count = extent->l2_cache_counts[i];
967
            min_index = i;
968
        }
969
    }
970
    l2_table = extent->l2_cache + (min_index * extent->l2_size);
971
    if (bdrv_pread(
972
                extent->file,
973
                (int64_t)l2_offset * 512,
974
                l2_table,
975
                extent->l2_size * sizeof(uint32_t)
976
            ) != extent->l2_size * sizeof(uint32_t)) {
977
        return VMDK_ERROR;
978
    }
979

    
980
    extent->l2_cache_offsets[min_index] = l2_offset;
981
    extent->l2_cache_counts[min_index] = 1;
982
 found:
983
    l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
984
    *cluster_offset = le32_to_cpu(l2_table[l2_index]);
985

    
986
    if (m_data) {
987
        m_data->valid = 1;
988
        m_data->l1_index = l1_index;
989
        m_data->l2_index = l2_index;
990
        m_data->offset = *cluster_offset;
991
        m_data->l2_offset = l2_offset;
992
        m_data->l2_cache_entry = &l2_table[l2_index];
993
    }
994
    if (extent->has_zero_grain && *cluster_offset == VMDK_GTE_ZEROED) {
995
        zeroed = true;
996
    }
997

    
998
    if (!*cluster_offset || zeroed) {
999
        if (!allocate) {
1000
            return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
1001
        }
1002

    
1003
        /* Avoid the L2 tables update for the images that have snapshots. */
1004
        *cluster_offset = bdrv_getlength(extent->file);
1005
        if (!extent->compressed) {
1006
            bdrv_truncate(
1007
                extent->file,
1008
                *cluster_offset + (extent->cluster_sectors << 9)
1009
            );
1010
        }
1011

    
1012
        *cluster_offset >>= 9;
1013
        l2_table[l2_index] = cpu_to_le32(*cluster_offset);
1014

    
1015
        /* First of all we write grain itself, to avoid race condition
1016
         * that may to corrupt the image.
1017
         * This problem may occur because of insufficient space on host disk
1018
         * or inappropriate VM shutdown.
1019
         */
1020
        if (get_whole_cluster(
1021
                bs, extent, *cluster_offset, offset, allocate) == -1) {
1022
            return VMDK_ERROR;
1023
        }
1024

    
1025
        if (m_data) {
1026
            m_data->offset = *cluster_offset;
1027
        }
1028
    }
1029
    *cluster_offset <<= 9;
1030
    return VMDK_OK;
1031
}
1032

    
1033
static VmdkExtent *find_extent(BDRVVmdkState *s,
1034
                                int64_t sector_num, VmdkExtent *start_hint)
1035
{
1036
    VmdkExtent *extent = start_hint;
1037

    
1038
    if (!extent) {
1039
        extent = &s->extents[0];
1040
    }
1041
    while (extent < &s->extents[s->num_extents]) {
1042
        if (sector_num < extent->end_sector) {
1043
            return extent;
1044
        }
1045
        extent++;
1046
    }
1047
    return NULL;
1048
}
1049

    
1050
static int64_t coroutine_fn vmdk_co_get_block_status(BlockDriverState *bs,
1051
        int64_t sector_num, int nb_sectors, int *pnum)
1052
{
1053
    BDRVVmdkState *s = bs->opaque;
1054
    int64_t index_in_cluster, n, ret;
1055
    uint64_t offset;
1056
    VmdkExtent *extent;
1057

    
1058
    extent = find_extent(s, sector_num, NULL);
1059
    if (!extent) {
1060
        return 0;
1061
    }
1062
    qemu_co_mutex_lock(&s->lock);
1063
    ret = get_cluster_offset(bs, extent, NULL,
1064
                            sector_num * 512, 0, &offset);
1065
    qemu_co_mutex_unlock(&s->lock);
1066

    
1067
    switch (ret) {
1068
    case VMDK_ERROR:
1069
        ret = -EIO;
1070
        break;
1071
    case VMDK_UNALLOC:
1072
        ret = 0;
1073
        break;
1074
    case VMDK_ZEROED:
1075
        ret = BDRV_BLOCK_ZERO;
1076
        break;
1077
    case VMDK_OK:
1078
        ret = BDRV_BLOCK_DATA;
1079
        if (extent->file == bs->file) {
1080
            ret |= BDRV_BLOCK_OFFSET_VALID | offset;
1081
        }
1082

    
1083
        break;
1084
    }
1085

    
1086
    index_in_cluster = sector_num % extent->cluster_sectors;
1087
    n = extent->cluster_sectors - index_in_cluster;
1088
    if (n > nb_sectors) {
1089
        n = nb_sectors;
1090
    }
1091
    *pnum = n;
1092
    return ret;
1093
}
1094

    
1095
static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
1096
                            int64_t offset_in_cluster, const uint8_t *buf,
1097
                            int nb_sectors, int64_t sector_num)
1098
{
1099
    int ret;
1100
    VmdkGrainMarker *data = NULL;
1101
    uLongf buf_len;
1102
    const uint8_t *write_buf = buf;
1103
    int write_len = nb_sectors * 512;
1104

    
1105
    if (extent->compressed) {
1106
        if (!extent->has_marker) {
1107
            ret = -EINVAL;
1108
            goto out;
1109
        }
1110
        buf_len = (extent->cluster_sectors << 9) * 2;
1111
        data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
1112
        if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK ||
1113
                buf_len == 0) {
1114
            ret = -EINVAL;
1115
            goto out;
1116
        }
1117
        data->lba = sector_num;
1118
        data->size = buf_len;
1119
        write_buf = (uint8_t *)data;
1120
        write_len = buf_len + sizeof(VmdkGrainMarker);
1121
    }
1122
    ret = bdrv_pwrite(extent->file,
1123
                        cluster_offset + offset_in_cluster,
1124
                        write_buf,
1125
                        write_len);
1126
    if (ret != write_len) {
1127
        ret = ret < 0 ? ret : -EIO;
1128
        goto out;
1129
    }
1130
    ret = 0;
1131
 out:
1132
    g_free(data);
1133
    return ret;
1134
}
1135

    
1136
static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
1137
                            int64_t offset_in_cluster, uint8_t *buf,
1138
                            int nb_sectors)
1139
{
1140
    int ret;
1141
    int cluster_bytes, buf_bytes;
1142
    uint8_t *cluster_buf, *compressed_data;
1143
    uint8_t *uncomp_buf;
1144
    uint32_t data_len;
1145
    VmdkGrainMarker *marker;
1146
    uLongf buf_len;
1147

    
1148

    
1149
    if (!extent->compressed) {
1150
        ret = bdrv_pread(extent->file,
1151
                          cluster_offset + offset_in_cluster,
1152
                          buf, nb_sectors * 512);
1153
        if (ret == nb_sectors * 512) {
1154
            return 0;
1155
        } else {
1156
            return -EIO;
1157
        }
1158
    }
1159
    cluster_bytes = extent->cluster_sectors * 512;
1160
    /* Read two clusters in case GrainMarker + compressed data > one cluster */
1161
    buf_bytes = cluster_bytes * 2;
1162
    cluster_buf = g_malloc(buf_bytes);
1163
    uncomp_buf = g_malloc(cluster_bytes);
1164
    ret = bdrv_pread(extent->file,
1165
                cluster_offset,
1166
                cluster_buf, buf_bytes);
1167
    if (ret < 0) {
1168
        goto out;
1169
    }
1170
    compressed_data = cluster_buf;
1171
    buf_len = cluster_bytes;
1172
    data_len = cluster_bytes;
1173
    if (extent->has_marker) {
1174
        marker = (VmdkGrainMarker *)cluster_buf;
1175
        compressed_data = marker->data;
1176
        data_len = le32_to_cpu(marker->size);
1177
    }
1178
    if (!data_len || data_len > buf_bytes) {
1179
        ret = -EINVAL;
1180
        goto out;
1181
    }
1182
    ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1183
    if (ret != Z_OK) {
1184
        ret = -EINVAL;
1185
        goto out;
1186

    
1187
    }
1188
    if (offset_in_cluster < 0 ||
1189
            offset_in_cluster + nb_sectors * 512 > buf_len) {
1190
        ret = -EINVAL;
1191
        goto out;
1192
    }
1193
    memcpy(buf, uncomp_buf + offset_in_cluster, nb_sectors * 512);
1194
    ret = 0;
1195

    
1196
 out:
1197
    g_free(uncomp_buf);
1198
    g_free(cluster_buf);
1199
    return ret;
1200
}
1201

    
1202
static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
1203
                    uint8_t *buf, int nb_sectors)
1204
{
1205
    BDRVVmdkState *s = bs->opaque;
1206
    int ret;
1207
    uint64_t n, index_in_cluster;
1208
    uint64_t extent_begin_sector, extent_relative_sector_num;
1209
    VmdkExtent *extent = NULL;
1210
    uint64_t cluster_offset;
1211

    
1212
    while (nb_sectors > 0) {
1213
        extent = find_extent(s, sector_num, extent);
1214
        if (!extent) {
1215
            return -EIO;
1216
        }
1217
        ret = get_cluster_offset(
1218
                            bs, extent, NULL,
1219
                            sector_num << 9, 0, &cluster_offset);
1220
        extent_begin_sector = extent->end_sector - extent->sectors;
1221
        extent_relative_sector_num = sector_num - extent_begin_sector;
1222
        index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
1223
        n = extent->cluster_sectors - index_in_cluster;
1224
        if (n > nb_sectors) {
1225
            n = nb_sectors;
1226
        }
1227
        if (ret != VMDK_OK) {
1228
            /* if not allocated, try to read from parent image, if exist */
1229
            if (bs->backing_hd && ret != VMDK_ZEROED) {
1230
                if (!vmdk_is_cid_valid(bs)) {
1231
                    return -EINVAL;
1232
                }
1233
                ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
1234
                if (ret < 0) {
1235
                    return ret;
1236
                }
1237
            } else {
1238
                memset(buf, 0, 512 * n);
1239
            }
1240
        } else {
1241
            ret = vmdk_read_extent(extent,
1242
                            cluster_offset, index_in_cluster * 512,
1243
                            buf, n);
1244
            if (ret) {
1245
                return ret;
1246
            }
1247
        }
1248
        nb_sectors -= n;
1249
        sector_num += n;
1250
        buf += n * 512;
1251
    }
1252
    return 0;
1253
}
1254

    
1255
static coroutine_fn int vmdk_co_read(BlockDriverState *bs, int64_t sector_num,
1256
                                     uint8_t *buf, int nb_sectors)
1257
{
1258
    int ret;
1259
    BDRVVmdkState *s = bs->opaque;
1260
    qemu_co_mutex_lock(&s->lock);
1261
    ret = vmdk_read(bs, sector_num, buf, nb_sectors);
1262
    qemu_co_mutex_unlock(&s->lock);
1263
    return ret;
1264
}
1265

    
1266
/**
1267
 * vmdk_write:
1268
 * @zeroed:       buf is ignored (data is zero), use zeroed_grain GTE feature
1269
 *                if possible, otherwise return -ENOTSUP.
1270
 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1271
 *                with each cluster. By dry run we can find if the zero write
1272
 *                is possible without modifying image data.
1273
 *
1274
 * Returns: error code with 0 for success.
1275
 */
1276
static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
1277
                      const uint8_t *buf, int nb_sectors,
1278
                      bool zeroed, bool zero_dry_run)
1279
{
1280
    BDRVVmdkState *s = bs->opaque;
1281
    VmdkExtent *extent = NULL;
1282
    int n, ret;
1283
    int64_t index_in_cluster;
1284
    uint64_t extent_begin_sector, extent_relative_sector_num;
1285
    uint64_t cluster_offset;
1286
    VmdkMetaData m_data;
1287

    
1288
    if (sector_num > bs->total_sectors) {
1289
        fprintf(stderr,
1290
                "(VMDK) Wrong offset: sector_num=0x%" PRIx64
1291
                " total_sectors=0x%" PRIx64 "\n",
1292
                sector_num, bs->total_sectors);
1293
        return -EIO;
1294
    }
1295

    
1296
    while (nb_sectors > 0) {
1297
        extent = find_extent(s, sector_num, extent);
1298
        if (!extent) {
1299
            return -EIO;
1300
        }
1301
        ret = get_cluster_offset(
1302
                                bs,
1303
                                extent,
1304
                                &m_data,
1305
                                sector_num << 9, !extent->compressed,
1306
                                &cluster_offset);
1307
        if (extent->compressed) {
1308
            if (ret == VMDK_OK) {
1309
                /* Refuse write to allocated cluster for streamOptimized */
1310
                fprintf(stderr,
1311
                        "VMDK: can't write to allocated cluster"
1312
                        " for streamOptimized\n");
1313
                return -EIO;
1314
            } else {
1315
                /* allocate */
1316
                ret = get_cluster_offset(
1317
                                        bs,
1318
                                        extent,
1319
                                        &m_data,
1320
                                        sector_num << 9, 1,
1321
                                        &cluster_offset);
1322
            }
1323
        }
1324
        if (ret == VMDK_ERROR) {
1325
            return -EINVAL;
1326
        }
1327
        extent_begin_sector = extent->end_sector - extent->sectors;
1328
        extent_relative_sector_num = sector_num - extent_begin_sector;
1329
        index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
1330
        n = extent->cluster_sectors - index_in_cluster;
1331
        if (n > nb_sectors) {
1332
            n = nb_sectors;
1333
        }
1334
        if (zeroed) {
1335
            /* Do zeroed write, buf is ignored */
1336
            if (extent->has_zero_grain &&
1337
                    index_in_cluster == 0 &&
1338
                    n >= extent->cluster_sectors) {
1339
                n = extent->cluster_sectors;
1340
                if (!zero_dry_run) {
1341
                    m_data.offset = VMDK_GTE_ZEROED;
1342
                    /* update L2 tables */
1343
                    if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1344
                        return -EIO;
1345
                    }
1346
                }
1347
            } else {
1348
                return -ENOTSUP;
1349
            }
1350
        } else {
1351
            ret = vmdk_write_extent(extent,
1352
                            cluster_offset, index_in_cluster * 512,
1353
                            buf, n, sector_num);
1354
            if (ret) {
1355
                return ret;
1356
            }
1357
            if (m_data.valid) {
1358
                /* update L2 tables */
1359
                if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1360
                    return -EIO;
1361
                }
1362
            }
1363
        }
1364
        nb_sectors -= n;
1365
        sector_num += n;
1366
        buf += n * 512;
1367

    
1368
        /* update CID on the first write every time the virtual disk is
1369
         * opened */
1370
        if (!s->cid_updated) {
1371
            ret = vmdk_write_cid(bs, time(NULL));
1372
            if (ret < 0) {
1373
                return ret;
1374
            }
1375
            s->cid_updated = true;
1376
        }
1377
    }
1378
    return 0;
1379
}
1380

    
1381
static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num,
1382
                                      const uint8_t *buf, int nb_sectors)
1383
{
1384
    int ret;
1385
    BDRVVmdkState *s = bs->opaque;
1386
    qemu_co_mutex_lock(&s->lock);
1387
    ret = vmdk_write(bs, sector_num, buf, nb_sectors, false, false);
1388
    qemu_co_mutex_unlock(&s->lock);
1389
    return ret;
1390
}
1391

    
1392
static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
1393
                                             int64_t sector_num,
1394
                                             int nb_sectors)
1395
{
1396
    int ret;
1397
    BDRVVmdkState *s = bs->opaque;
1398
    qemu_co_mutex_lock(&s->lock);
1399
    /* write zeroes could fail if sectors not aligned to cluster, test it with
1400
     * dry_run == true before really updating image */
1401
    ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, true);
1402
    if (!ret) {
1403
        ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, false);
1404
    }
1405
    qemu_co_mutex_unlock(&s->lock);
1406
    return ret;
1407
}
1408

    
1409
static int vmdk_create_extent(const char *filename, int64_t filesize,
1410
                              bool flat, bool compress, bool zeroed_grain)
1411
{
1412
    int ret, i;
1413
    int fd = 0;
1414
    VMDK4Header header;
1415
    uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
1416

    
1417
    fd = qemu_open(filename,
1418
                   O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
1419
                   0644);
1420
    if (fd < 0) {
1421
        return -errno;
1422
    }
1423
    if (flat) {
1424
        ret = ftruncate(fd, filesize);
1425
        if (ret < 0) {
1426
            ret = -errno;
1427
        }
1428
        goto exit;
1429
    }
1430
    magic = cpu_to_be32(VMDK4_MAGIC);
1431
    memset(&header, 0, sizeof(header));
1432
    header.version = zeroed_grain ? 2 : 1;
1433
    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
1434
                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
1435
                   | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
1436
    header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
1437
    header.capacity = filesize / 512;
1438
    header.granularity = 128;
1439
    header.num_gtes_per_gt = 512;
1440

    
1441
    grains = (filesize / 512 + header.granularity - 1) / header.granularity;
1442
    gt_size = ((header.num_gtes_per_gt * sizeof(uint32_t)) + 511) >> 9;
1443
    gt_count =
1444
        (grains + header.num_gtes_per_gt - 1) / header.num_gtes_per_gt;
1445
    gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
1446

    
1447
    header.desc_offset = 1;
1448
    header.desc_size = 20;
1449
    header.rgd_offset = header.desc_offset + header.desc_size;
1450
    header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
1451
    header.grain_offset =
1452
       ((header.gd_offset + gd_size + (gt_size * gt_count) +
1453
         header.granularity - 1) / header.granularity) *
1454
        header.granularity;
1455
    /* swap endianness for all header fields */
1456
    header.version = cpu_to_le32(header.version);
1457
    header.flags = cpu_to_le32(header.flags);
1458
    header.capacity = cpu_to_le64(header.capacity);
1459
    header.granularity = cpu_to_le64(header.granularity);
1460
    header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
1461
    header.desc_offset = cpu_to_le64(header.desc_offset);
1462
    header.desc_size = cpu_to_le64(header.desc_size);
1463
    header.rgd_offset = cpu_to_le64(header.rgd_offset);
1464
    header.gd_offset = cpu_to_le64(header.gd_offset);
1465
    header.grain_offset = cpu_to_le64(header.grain_offset);
1466
    header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
1467

    
1468
    header.check_bytes[0] = 0xa;
1469
    header.check_bytes[1] = 0x20;
1470
    header.check_bytes[2] = 0xd;
1471
    header.check_bytes[3] = 0xa;
1472

    
1473
    /* write all the data */
1474
    ret = qemu_write_full(fd, &magic, sizeof(magic));
1475
    if (ret != sizeof(magic)) {
1476
        ret = -errno;
1477
        goto exit;
1478
    }
1479
    ret = qemu_write_full(fd, &header, sizeof(header));
1480
    if (ret != sizeof(header)) {
1481
        ret = -errno;
1482
        goto exit;
1483
    }
1484

    
1485
    ret = ftruncate(fd, le64_to_cpu(header.grain_offset) << 9);
1486
    if (ret < 0) {
1487
        ret = -errno;
1488
        goto exit;
1489
    }
1490

    
1491
    /* write grain directory */
1492
    lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
1493
    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_size;
1494
         i < gt_count; i++, tmp += gt_size) {
1495
        ret = qemu_write_full(fd, &tmp, sizeof(tmp));
1496
        if (ret != sizeof(tmp)) {
1497
            ret = -errno;
1498
            goto exit;
1499
        }
1500
    }
1501

    
1502
    /* write backup grain directory */
1503
    lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
1504
    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_size;
1505
         i < gt_count; i++, tmp += gt_size) {
1506
        ret = qemu_write_full(fd, &tmp, sizeof(tmp));
1507
        if (ret != sizeof(tmp)) {
1508
            ret = -errno;
1509
            goto exit;
1510
        }
1511
    }
1512

    
1513
    ret = 0;
1514
 exit:
1515
    qemu_close(fd);
1516
    return ret;
1517
}
1518

    
1519
static int filename_decompose(const char *filename, char *path, char *prefix,
1520
        char *postfix, size_t buf_len)
1521
{
1522
    const char *p, *q;
1523

    
1524
    if (filename == NULL || !strlen(filename)) {
1525
        fprintf(stderr, "Vmdk: no filename provided.\n");
1526
        return VMDK_ERROR;
1527
    }
1528
    p = strrchr(filename, '/');
1529
    if (p == NULL) {
1530
        p = strrchr(filename, '\\');
1531
    }
1532
    if (p == NULL) {
1533
        p = strrchr(filename, ':');
1534
    }
1535
    if (p != NULL) {
1536
        p++;
1537
        if (p - filename >= buf_len) {
1538
            return VMDK_ERROR;
1539
        }
1540
        pstrcpy(path, p - filename + 1, filename);
1541
    } else {
1542
        p = filename;
1543
        path[0] = '\0';
1544
    }
1545
    q = strrchr(p, '.');
1546
    if (q == NULL) {
1547
        pstrcpy(prefix, buf_len, p);
1548
        postfix[0] = '\0';
1549
    } else {
1550
        if (q - p >= buf_len) {
1551
            return VMDK_ERROR;
1552
        }
1553
        pstrcpy(prefix, q - p + 1, p);
1554
        pstrcpy(postfix, buf_len, q);
1555
    }
1556
    return VMDK_OK;
1557
}
1558

    
1559
static int vmdk_create(const char *filename, QEMUOptionParameter *options,
1560
                       Error **errp)
1561
{
1562
    int fd, idx = 0;
1563
    char desc[BUF_SIZE];
1564
    int64_t total_size = 0, filesize;
1565
    const char *adapter_type = NULL;
1566
    const char *backing_file = NULL;
1567
    const char *fmt = NULL;
1568
    int flags = 0;
1569
    int ret = 0;
1570
    bool flat, split, compress;
1571
    char ext_desc_lines[BUF_SIZE] = "";
1572
    char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];
1573
    const int64_t split_size = 0x80000000;  /* VMDK has constant split size */
1574
    const char *desc_extent_line;
1575
    char parent_desc_line[BUF_SIZE] = "";
1576
    uint32_t parent_cid = 0xffffffff;
1577
    uint32_t number_heads = 16;
1578
    bool zeroed_grain = false;
1579
    const char desc_template[] =
1580
        "# Disk DescriptorFile\n"
1581
        "version=1\n"
1582
        "CID=%x\n"
1583
        "parentCID=%x\n"
1584
        "createType=\"%s\"\n"
1585
        "%s"
1586
        "\n"
1587
        "# Extent description\n"
1588
        "%s"
1589
        "\n"
1590
        "# The Disk Data Base\n"
1591
        "#DDB\n"
1592
        "\n"
1593
        "ddb.virtualHWVersion = \"%d\"\n"
1594
        "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
1595
        "ddb.geometry.heads = \"%d\"\n"
1596
        "ddb.geometry.sectors = \"63\"\n"
1597
        "ddb.adapterType = \"%s\"\n";
1598
    Error *local_err = NULL;
1599

    
1600
    if (filename_decompose(filename, path, prefix, postfix, PATH_MAX)) {
1601
        return -EINVAL;
1602
    }
1603
    /* Read out options */
1604
    while (options && options->name) {
1605
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1606
            total_size = options->value.n;
1607
        } else if (!strcmp(options->name, BLOCK_OPT_ADAPTER_TYPE)) {
1608
            adapter_type = options->value.s;
1609
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1610
            backing_file = options->value.s;
1611
        } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
1612
            flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;
1613
        } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) {
1614
            fmt = options->value.s;
1615
        } else if (!strcmp(options->name, BLOCK_OPT_ZEROED_GRAIN)) {
1616
            zeroed_grain |= options->value.n;
1617
        }
1618
        options++;
1619
    }
1620
    if (!adapter_type) {
1621
        adapter_type = "ide";
1622
    } else if (strcmp(adapter_type, "ide") &&
1623
               strcmp(adapter_type, "buslogic") &&
1624
               strcmp(adapter_type, "lsilogic") &&
1625
               strcmp(adapter_type, "legacyESX")) {
1626
        fprintf(stderr, "VMDK: Unknown adapter type: '%s'.\n", adapter_type);
1627
        return -EINVAL;
1628
    }
1629
    if (strcmp(adapter_type, "ide") != 0) {
1630
        /* that's the number of heads with which vmware operates when
1631
           creating, exporting, etc. vmdk files with a non-ide adapter type */
1632
        number_heads = 255;
1633
    }
1634
    if (!fmt) {
1635
        /* Default format to monolithicSparse */
1636
        fmt = "monolithicSparse";
1637
    } else if (strcmp(fmt, "monolithicFlat") &&
1638
               strcmp(fmt, "monolithicSparse") &&
1639
               strcmp(fmt, "twoGbMaxExtentSparse") &&
1640
               strcmp(fmt, "twoGbMaxExtentFlat") &&
1641
               strcmp(fmt, "streamOptimized")) {
1642
        fprintf(stderr, "VMDK: Unknown subformat: %s\n", fmt);
1643
        return -EINVAL;
1644
    }
1645
    split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
1646
              strcmp(fmt, "twoGbMaxExtentSparse"));
1647
    flat = !(strcmp(fmt, "monolithicFlat") &&
1648
             strcmp(fmt, "twoGbMaxExtentFlat"));
1649
    compress = !strcmp(fmt, "streamOptimized");
1650
    if (flat) {
1651
        desc_extent_line = "RW %lld FLAT \"%s\" 0\n";
1652
    } else {
1653
        desc_extent_line = "RW %lld SPARSE \"%s\"\n";
1654
    }
1655
    if (flat && backing_file) {
1656
        /* not supporting backing file for flat image */
1657
        return -ENOTSUP;
1658
    }
1659
    if (backing_file) {
1660
        BlockDriverState *bs = bdrv_new("");
1661
        ret = bdrv_open(bs, backing_file, NULL, 0, NULL, &local_err);
1662
        if (ret != 0) {
1663
            qerror_report_err(local_err);
1664
            error_free(local_err);
1665
            bdrv_unref(bs);
1666
            return ret;
1667
        }
1668
        if (strcmp(bs->drv->format_name, "vmdk")) {
1669
            bdrv_unref(bs);
1670
            return -EINVAL;
1671
        }
1672
        parent_cid = vmdk_read_cid(bs, 0);
1673
        bdrv_unref(bs);
1674
        snprintf(parent_desc_line, sizeof(parent_desc_line),
1675
                "parentFileNameHint=\"%s\"", backing_file);
1676
    }
1677

    
1678
    /* Create extents */
1679
    filesize = total_size;
1680
    while (filesize > 0) {
1681
        char desc_line[BUF_SIZE];
1682
        char ext_filename[PATH_MAX];
1683
        char desc_filename[PATH_MAX];
1684
        int64_t size = filesize;
1685

    
1686
        if (split && size > split_size) {
1687
            size = split_size;
1688
        }
1689
        if (split) {
1690
            snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",
1691
                    prefix, flat ? 'f' : 's', ++idx, postfix);
1692
        } else if (flat) {
1693
            snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",
1694
                    prefix, postfix);
1695
        } else {
1696
            snprintf(desc_filename, sizeof(desc_filename), "%s%s",
1697
                    prefix, postfix);
1698
        }
1699
        snprintf(ext_filename, sizeof(ext_filename), "%s%s",
1700
                path, desc_filename);
1701

    
1702
        if (vmdk_create_extent(ext_filename, size,
1703
                               flat, compress, zeroed_grain)) {
1704
            return -EINVAL;
1705
        }
1706
        filesize -= size;
1707

    
1708
        /* Format description line */
1709
        snprintf(desc_line, sizeof(desc_line),
1710
                    desc_extent_line, size / 512, desc_filename);
1711
        pstrcat(ext_desc_lines, sizeof(ext_desc_lines), desc_line);
1712
    }
1713
    /* generate descriptor file */
1714
    snprintf(desc, sizeof(desc), desc_template,
1715
            (unsigned int)time(NULL),
1716
            parent_cid,
1717
            fmt,
1718
            parent_desc_line,
1719
            ext_desc_lines,
1720
            (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
1721
            total_size / (int64_t)(63 * number_heads * 512), number_heads,
1722
                adapter_type);
1723
    if (split || flat) {
1724
        fd = qemu_open(filename,
1725
                       O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
1726
                       0644);
1727
    } else {
1728
        fd = qemu_open(filename,
1729
                       O_WRONLY | O_BINARY | O_LARGEFILE,
1730
                       0644);
1731
    }
1732
    if (fd < 0) {
1733
        return -errno;
1734
    }
1735
    /* the descriptor offset = 0x200 */
1736
    if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) {
1737
        ret = -errno;
1738
        goto exit;
1739
    }
1740
    ret = qemu_write_full(fd, desc, strlen(desc));
1741
    if (ret != strlen(desc)) {
1742
        ret = -errno;
1743
        goto exit;
1744
    }
1745
    ret = 0;
1746
exit:
1747
    qemu_close(fd);
1748
    return ret;
1749
}
1750

    
1751
static void vmdk_close(BlockDriverState *bs)
1752
{
1753
    BDRVVmdkState *s = bs->opaque;
1754

    
1755
    vmdk_free_extents(bs);
1756

    
1757
    migrate_del_blocker(s->migration_blocker);
1758
    error_free(s->migration_blocker);
1759
}
1760

    
1761
static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
1762
{
1763
    BDRVVmdkState *s = bs->opaque;
1764
    int i, err;
1765
    int ret = 0;
1766

    
1767
    for (i = 0; i < s->num_extents; i++) {
1768
        err = bdrv_co_flush(s->extents[i].file);
1769
        if (err < 0) {
1770
            ret = err;
1771
        }
1772
    }
1773
    return ret;
1774
}
1775

    
1776
static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
1777
{
1778
    int i;
1779
    int64_t ret = 0;
1780
    int64_t r;
1781
    BDRVVmdkState *s = bs->opaque;
1782

    
1783
    ret = bdrv_get_allocated_file_size(bs->file);
1784
    if (ret < 0) {
1785
        return ret;
1786
    }
1787
    for (i = 0; i < s->num_extents; i++) {
1788
        if (s->extents[i].file == bs->file) {
1789
            continue;
1790
        }
1791
        r = bdrv_get_allocated_file_size(s->extents[i].file);
1792
        if (r < 0) {
1793
            return r;
1794
        }
1795
        ret += r;
1796
    }
1797
    return ret;
1798
}
1799

    
1800
static int vmdk_has_zero_init(BlockDriverState *bs)
1801
{
1802
    int i;
1803
    BDRVVmdkState *s = bs->opaque;
1804

    
1805
    /* If has a flat extent and its underlying storage doesn't have zero init,
1806
     * return 0. */
1807
    for (i = 0; i < s->num_extents; i++) {
1808
        if (s->extents[i].flat) {
1809
            if (!bdrv_has_zero_init(s->extents[i].file)) {
1810
                return 0;
1811
            }
1812
        }
1813
    }
1814
    return 1;
1815
}
1816

    
1817
static QEMUOptionParameter vmdk_create_options[] = {
1818
    {
1819
        .name = BLOCK_OPT_SIZE,
1820
        .type = OPT_SIZE,
1821
        .help = "Virtual disk size"
1822
    },
1823
    {
1824
        .name = BLOCK_OPT_ADAPTER_TYPE,
1825
        .type = OPT_STRING,
1826
        .help = "Virtual adapter type, can be one of "
1827
                "ide (default), lsilogic, buslogic or legacyESX"
1828
    },
1829
    {
1830
        .name = BLOCK_OPT_BACKING_FILE,
1831
        .type = OPT_STRING,
1832
        .help = "File name of a base image"
1833
    },
1834
    {
1835
        .name = BLOCK_OPT_COMPAT6,
1836
        .type = OPT_FLAG,
1837
        .help = "VMDK version 6 image"
1838
    },
1839
    {
1840
        .name = BLOCK_OPT_SUBFMT,
1841
        .type = OPT_STRING,
1842
        .help =
1843
            "VMDK flat extent format, can be one of "
1844
            "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
1845
    },
1846
    {
1847
        .name = BLOCK_OPT_ZEROED_GRAIN,
1848
        .type = OPT_FLAG,
1849
        .help = "Enable efficient zero writes using the zeroed-grain GTE feature"
1850
    },
1851
    { NULL }
1852
};
1853

    
1854
static BlockDriver bdrv_vmdk = {
1855
    .format_name                  = "vmdk",
1856
    .instance_size                = sizeof(BDRVVmdkState),
1857
    .bdrv_probe                   = vmdk_probe,
1858
    .bdrv_open                    = vmdk_open,
1859
    .bdrv_reopen_prepare          = vmdk_reopen_prepare,
1860
    .bdrv_read                    = vmdk_co_read,
1861
    .bdrv_write                   = vmdk_co_write,
1862
    .bdrv_co_write_zeroes         = vmdk_co_write_zeroes,
1863
    .bdrv_close                   = vmdk_close,
1864
    .bdrv_create                  = vmdk_create,
1865
    .bdrv_co_flush_to_disk        = vmdk_co_flush,
1866
    .bdrv_co_get_block_status     = vmdk_co_get_block_status,
1867
    .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
1868
    .bdrv_has_zero_init           = vmdk_has_zero_init,
1869

    
1870
    .create_options               = vmdk_create_options,
1871
};
1872

    
1873
static void bdrv_vmdk_init(void)
1874
{
1875
    bdrv_register(&bdrv_vmdk);
1876
}
1877

    
1878
block_init(bdrv_vmdk_init);