Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/block/vpc.c
Line
Count
Source
1
/*
2
 * Block driver for Connectix / Microsoft Virtual PC images
3
 *
4
 * Copyright (c) 2005 Alex Beregszaszi
5
 * Copyright (c) 2009 Kevin Wolf <kwolf@suse.de>
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/osdep.h"
27
#include "qapi/error.h"
28
#include "block/block_int.h"
29
#include "block/qdict.h"
30
#include "system/block-backend.h"
31
#include "qemu/module.h"
32
#include "qemu/option.h"
33
#include "migration/blocker.h"
34
#include "qemu/bswap.h"
35
#include "qemu/uuid.h"
36
#include "qemu/memalign.h"
37
#include "qobject/qdict.h"
38
#include "qapi/qobject-input-visitor.h"
39
#include "qapi/qapi-visit-block-core.h"
40
41
/**************************************************************/
42
43
//#define CACHE
44
45
enum vhd_type {
46
    VHD_FIXED           = 2,
47
    VHD_DYNAMIC         = 3,
48
    VHD_DIFFERENCING    = 4,
49
};
50
51
/* Seconds since Jan 1, 2000 0:00:00 (UTC) */
52
0
#define VHD_TIMESTAMP_BASE 946684800
53
54
0
#define VHD_CHS_MAX_C   65535LL
55
0
#define VHD_CHS_MAX_H   16
56
0
#define VHD_CHS_MAX_S   255
57
58
0
#define VHD_MAX_SECTORS       0xff000000    /* 2040 GiB max image size */
59
0
#define VHD_MAX_GEOMETRY      (VHD_CHS_MAX_C * VHD_CHS_MAX_H * VHD_CHS_MAX_S)
60
61
0
#define VPC_OPT_FORCE_SIZE "force_size"
62
63
/* always big-endian */
64
typedef struct vhd_footer {
65
    char        creator[8]; /* "conectix" */
66
    uint32_t    features;
67
    uint32_t    version;
68
69
    /* Offset of next header structure, 0xFFFFFFFF if none */
70
    uint64_t    data_offset;
71
72
    /* Seconds since Jan 1, 2000 0:00:00 (UTC) */
73
    uint32_t    timestamp;
74
75
    char        creator_app[4]; /*  e.g., "vpc " */
76
    uint16_t    major;
77
    uint16_t    minor;
78
    char        creator_os[4]; /* "Wi2k" */
79
80
    uint64_t    orig_size;
81
    uint64_t    current_size;
82
83
    uint16_t    cyls;
84
    uint8_t     heads;
85
    uint8_t     secs_per_cyl;
86
87
    uint32_t    type;
88
89
    /* Checksum of the Hard Disk Footer ("one's complement of the sum of all
90
       the bytes in the footer without the checksum field") */
91
    uint32_t    checksum;
92
93
    /* UUID used to identify a parent hard disk (backing file) */
94
    QemuUUID    uuid;
95
96
    uint8_t     in_saved_state;
97
    uint8_t     reserved[427];
98
} QEMU_PACKED VHDFooter;
99
100
QEMU_BUILD_BUG_ON(sizeof(VHDFooter) != 512);
101
102
typedef struct vhd_dyndisk_header {
103
    char        magic[8]; /* "cxsparse" */
104
105
    /* Offset of next header structure, 0xFFFFFFFF if none */
106
    uint64_t    data_offset;
107
108
    /* Offset of the Block Allocation Table (BAT) */
109
    uint64_t    table_offset;
110
111
    uint32_t    version;
112
    uint32_t    max_table_entries; /* 32bit/entry */
113
114
    /* 2 MB by default, must be a power of two */
115
    uint32_t    block_size;
116
117
    uint32_t    checksum;
118
    uint8_t     parent_uuid[16];
119
    uint32_t    parent_timestamp;
120
    uint32_t    reserved;
121
122
    /* Backing file name (in UTF-16) */
123
    uint8_t     parent_name[512];
124
125
    struct {
126
        uint32_t    platform;
127
        uint32_t    data_space;
128
        uint32_t    data_length;
129
        uint32_t    reserved;
130
        uint64_t    data_offset;
131
    } parent_locator[8];
132
    uint8_t     reserved2[256];
133
} QEMU_PACKED VHDDynDiskHeader;
134
135
QEMU_BUILD_BUG_ON(sizeof(VHDDynDiskHeader) != 1024);
136
137
typedef struct BDRVVPCState {
138
    CoMutex lock;
139
    VHDFooter footer;
140
    uint64_t free_data_block_offset;
141
    int max_table_entries;
142
    uint32_t *pagetable;
143
    uint64_t bat_offset;
144
    uint64_t last_bitmap_offset;
145
146
    uint32_t block_size;
147
    uint32_t bitmap_size;
148
    bool force_use_chs;
149
    bool force_use_sz;
150
151
#ifdef CACHE
152
    uint8_t *pageentry_u8;
153
    uint32_t *pageentry_u32;
154
    uint16_t *pageentry_u16;
155
156
    uint64_t last_bitmap;
157
#endif
158
159
    Error *migration_blocker;
160
} BDRVVPCState;
161
162
0
#define VPC_OPT_SIZE_CALC "force_size_calc"
163
static QemuOptsList vpc_runtime_opts = {
164
    .name = "vpc-runtime-opts",
165
    .head = QTAILQ_HEAD_INITIALIZER(vpc_runtime_opts.head),
166
    .desc = {
167
        {
168
            .name = VPC_OPT_SIZE_CALC,
169
            .type = QEMU_OPT_STRING,
170
            .help = "Force disk size calculation to use either CHS geometry, "
171
                    "or use the disk current_size specified in the VHD footer. "
172
                    "{chs, current_size}"
173
        },
174
        { /* end of list */ }
175
    }
176
};
177
178
static QemuOptsList vpc_create_opts;
179
180
static uint32_t vpc_checksum(void *p, size_t size)
181
0
{
182
0
    uint8_t *buf = p;
183
0
    uint32_t res = 0;
184
0
    int i;
185
186
0
    for (i = 0; i < size; i++)
187
0
        res += buf[i];
188
189
0
    return ~res;
190
0
}
191
192
193
static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename)
194
0
{
195
0
    if (buf_size >= 8 && !strncmp((char *)buf, "conectix", 8))
196
0
        return 100;
197
0
    return 0;
198
0
}
199
200
static void vpc_parse_options(BlockDriverState *bs, QemuOpts *opts,
201
                              Error **errp)
202
0
{
203
0
    BDRVVPCState *s = bs->opaque;
204
0
    const char *size_calc;
205
206
0
    size_calc = qemu_opt_get(opts, VPC_OPT_SIZE_CALC);
207
208
0
    if (!size_calc) {
209
       /* no override, use autodetect only */
210
0
    } else if (!strcmp(size_calc, "current_size")) {
211
0
        s->force_use_sz = true;
212
0
    } else if (!strcmp(size_calc, "chs")) {
213
0
        s->force_use_chs = true;
214
0
    } else {
215
0
        error_setg(errp, "Invalid size calculation mode: '%s'", size_calc);
216
0
    }
217
0
}
218
219
/*
220
 * Microsoft Virtual PC and Microsoft Hyper-V produce and read
221
 * VHD image sizes differently.  VPC will rely on CHS geometry,
222
 * while Hyper-V and disk2vhd use the size specified in the footer.
223
 *
224
 * We use a couple of approaches to try and determine the correct method:
225
 * look at the Creator App field, and look for images that have CHS
226
 * geometry that is the maximum value.
227
 *
228
 * If the CHS geometry is the maximum CHS geometry, then we assume that
229
 * the size is the footer->current_size to avoid truncation.  Otherwise,
230
 * we follow the table based on footer->creator_app:
231
 *
232
 *  Known creator apps:
233
 *      'vpc '  :  CHS              Virtual PC (uses disk geometry)
234
 *      'qemu'  :  CHS              QEMU (uses disk geometry)
235
 *      'qem2'  :  current_size     QEMU (uses current_size)
236
 *      'win '  :  current_size     Hyper-V
237
 *      'd2v '  :  current_size     Disk2vhd
238
 *      'tap\0' :  current_size     XenServer
239
 *      'CTXS'  :  current_size     XenConverter
240
 *      'wa\0\0':  current_size     Azure
241
 *
242
 *  The user can override the table values via drive options, however
243
 *  even with an override we will still use current_size for images
244
 *  that have CHS geometry of the maximum size.
245
 */
246
static bool vpc_ignore_current_size(VHDFooter *footer)
247
0
{
248
0
    return !strncmp(footer->creator_app, "vpc ", 4) ||
249
0
           !strncmp(footer->creator_app, "qemu", 4);
250
0
}
251
252
static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
253
                    Error **errp)
254
0
{
255
0
    BDRVVPCState *s = bs->opaque;
256
0
    int i;
257
0
    VHDFooter *footer;
258
0
    QemuOpts *opts = NULL;
259
0
    Error *local_err = NULL;
260
0
    bool use_chs;
261
0
    VHDDynDiskHeader dyndisk_header;
262
0
    uint32_t checksum;
263
0
    uint64_t computed_size;
264
0
    uint64_t pagetable_size;
265
0
    int disk_type = VHD_DYNAMIC;
266
0
    int ret;
267
0
    int64_t bs_size;
268
269
0
    ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
270
0
    if (ret < 0) {
271
0
        return ret;
272
0
    }
273
274
0
    GRAPH_RDLOCK_GUARD_MAINLOOP();
275
276
0
    opts = qemu_opts_create(&vpc_runtime_opts, NULL, 0, &error_abort);
277
0
    if (!qemu_opts_absorb_qdict(opts, options, errp)) {
278
0
        ret = -EINVAL;
279
0
        goto fail;
280
0
    }
281
282
0
    vpc_parse_options(bs, opts, &local_err);
283
0
    if (local_err) {
284
0
        error_propagate(errp, local_err);
285
0
        ret = -EINVAL;
286
0
        goto fail;
287
0
    }
288
289
0
    ret = bdrv_pread(bs->file, 0, sizeof(s->footer), &s->footer, 0);
290
0
    if (ret < 0) {
291
0
        error_setg(errp, "Unable to read VHD header");
292
0
        goto fail;
293
0
    }
294
295
0
    footer = &s->footer;
296
0
    if (strncmp(footer->creator, "conectix", 8)) {
297
0
        int64_t offset = bdrv_getlength(bs->file->bs);
298
0
        if (offset < 0) {
299
0
            ret = offset;
300
0
            error_setg(errp, "Invalid file size");
301
0
            goto fail;
302
0
        } else if (offset < sizeof(*footer)) {
303
0
            ret = -EINVAL;
304
0
            error_setg(errp, "File too small for a VHD header");
305
0
            goto fail;
306
0
        }
307
308
        /* If a fixed disk, the footer is found only at the end of the file */
309
0
        ret = bdrv_pread(bs->file, offset - sizeof(*footer), sizeof(*footer),
310
0
                         footer, 0);
311
0
        if (ret < 0) {
312
0
            goto fail;
313
0
        }
314
0
        if (strncmp(footer->creator, "conectix", 8) ||
315
0
            be32_to_cpu(footer->type) != VHD_FIXED) {
316
0
            error_setg(errp, "invalid VPC image");
317
0
            ret = -EINVAL;
318
0
            goto fail;
319
0
        }
320
0
        disk_type = VHD_FIXED;
321
0
    }
322
323
0
    checksum = be32_to_cpu(footer->checksum);
324
0
    footer->checksum = 0;
325
0
    if (vpc_checksum(footer, sizeof(*footer)) != checksum) {
326
0
        error_setg(errp, "Incorrect header checksum");
327
0
        ret = -EINVAL;
328
0
        goto fail;
329
0
    }
330
331
    /* Write 'checksum' back to footer, or else will leave it with zero. */
332
0
    footer->checksum = cpu_to_be32(checksum);
333
334
    /* The visible size of a image in Virtual PC depends on the geometry
335
       rather than on the size stored in the footer (the size in the footer
336
       is too large usually) */
337
0
    bs->total_sectors = (int64_t)
338
0
        be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl;
339
340
    /* Use CHS or current_size to determine the image size. */
341
0
    use_chs = vpc_ignore_current_size(footer) || s->force_use_chs;
342
343
0
    if (!use_chs || bs->total_sectors == VHD_MAX_GEOMETRY || s->force_use_sz) {
344
0
        bs->total_sectors = be64_to_cpu(footer->current_size) /
345
0
                                        BDRV_SECTOR_SIZE;
346
0
    }
347
348
    /* Allow a maximum disk size of 2040 GiB */
349
0
    if (bs->total_sectors > VHD_MAX_SECTORS) {
350
0
        ret = -EFBIG;
351
0
        goto fail;
352
0
    }
353
354
0
    if (disk_type == VHD_DYNAMIC) {
355
0
        ret = bdrv_pread(bs->file, be64_to_cpu(footer->data_offset),
356
0
                         sizeof(dyndisk_header), &dyndisk_header, 0);
357
0
        if (ret < 0) {
358
0
            error_setg(errp, "Error reading dynamic VHD header");
359
0
            goto fail;
360
0
        }
361
362
0
        if (strncmp(dyndisk_header.magic, "cxsparse", 8)) {
363
0
            error_setg(errp, "Invalid header magic");
364
0
            ret = -EINVAL;
365
0
            goto fail;
366
0
        }
367
368
0
        s->block_size = be32_to_cpu(dyndisk_header.block_size);
369
0
        if (!is_power_of_2(s->block_size) || s->block_size < BDRV_SECTOR_SIZE) {
370
0
            error_setg(errp, "Invalid block size %" PRIu32, s->block_size);
371
0
            ret = -EINVAL;
372
0
            goto fail;
373
0
        }
374
0
        s->bitmap_size = ((s->block_size / (8 * 512)) + 511) & ~511;
375
376
0
        s->max_table_entries = be32_to_cpu(dyndisk_header.max_table_entries);
377
378
0
        if ((bs->total_sectors * 512) / s->block_size > 0xffffffffU) {
379
0
            error_setg(errp, "Too many blocks");
380
0
            ret = -EINVAL;
381
0
            goto fail;
382
0
        }
383
384
0
        computed_size = (uint64_t) s->max_table_entries * s->block_size;
385
0
        if (computed_size < bs->total_sectors * 512) {
386
0
            error_setg(errp, "Page table too small");
387
0
            ret = -EINVAL;
388
0
            goto fail;
389
0
        }
390
391
0
        if (s->max_table_entries > SIZE_MAX / 4 ||
392
0
            s->max_table_entries > (int) INT_MAX / 4) {
393
0
            error_setg(errp, "Max Table Entries too large (%" PRId32 ")",
394
0
                        s->max_table_entries);
395
0
            ret = -EINVAL;
396
0
            goto fail;
397
0
        }
398
399
0
        pagetable_size = (uint64_t) s->max_table_entries * 4;
400
401
0
        s->pagetable = qemu_try_blockalign(bs->file->bs, pagetable_size);
402
0
        if (s->pagetable == NULL) {
403
0
            error_setg(errp, "Unable to allocate memory for page table");
404
0
            ret = -ENOMEM;
405
0
            goto fail;
406
0
        }
407
408
0
        s->bat_offset = be64_to_cpu(dyndisk_header.table_offset);
409
410
0
        ret = bdrv_pread(bs->file, s->bat_offset, pagetable_size,
411
0
                         s->pagetable, 0);
412
0
        if (ret < 0) {
413
0
            error_setg(errp, "Error reading pagetable");
414
0
            goto fail;
415
0
        }
416
417
0
        s->free_data_block_offset =
418
0
            ROUND_UP(s->bat_offset + pagetable_size, 512);
419
420
0
        for (i = 0; i < s->max_table_entries; i++) {
421
0
            be32_to_cpus(&s->pagetable[i]);
422
0
            if (s->pagetable[i] != 0xFFFFFFFF) {
423
0
                int64_t next = (512 * (int64_t) s->pagetable[i]) +
424
0
                    s->bitmap_size + s->block_size;
425
426
0
                if (next > s->free_data_block_offset) {
427
0
                    s->free_data_block_offset = next;
428
0
                }
429
0
            }
430
0
        }
431
432
0
        bs_size = bdrv_getlength(bs->file->bs);
433
0
        if (bs_size < 0) {
434
0
            error_setg_errno(errp, -bs_size, "Unable to learn image size");
435
0
            ret = bs_size;
436
0
            goto fail;
437
0
        }
438
0
        if (s->free_data_block_offset > bs_size) {
439
0
            error_setg(errp, "block-vpc: free_data_block_offset points after "
440
0
                             "the end of file. The image has been truncated.");
441
0
            ret = -EINVAL;
442
0
            goto fail;
443
0
        }
444
445
0
        s->last_bitmap_offset = (int64_t) -1;
446
447
#ifdef CACHE
448
        s->pageentry_u8 = g_malloc(512);
449
        s->pageentry_u32 = s->pageentry_u8;
450
        s->pageentry_u16 = s->pageentry_u8;
451
        s->last_pagetable = -1;
452
#endif
453
0
    }
454
455
    /* Disable migration when VHD images are used */
456
0
    error_setg(&s->migration_blocker, "The vpc format used by node '%s' "
457
0
               "does not support live migration",
458
0
               bdrv_get_device_or_node_name(bs));
459
460
0
    ret = migrate_add_blocker_normal(&s->migration_blocker, errp);
461
0
    if (ret < 0) {
462
0
        goto fail;
463
0
    }
464
465
0
    qemu_co_mutex_init(&s->lock);
466
0
    qemu_opts_del(opts);
467
468
0
    return 0;
469
470
0
fail:
471
0
    qemu_opts_del(opts);
472
0
    qemu_vfree(s->pagetable);
473
#ifdef CACHE
474
    g_free(s->pageentry_u8);
475
#endif
476
0
    return ret;
477
0
}
478
479
static int vpc_reopen_prepare(BDRVReopenState *state,
480
                              BlockReopenQueue *queue, Error **errp)
481
0
{
482
0
    return 0;
483
0
}
484
485
/*
486
 * Returns the absolute byte offset of the given sector in the image file.
487
 * If the sector is not allocated, -1 is returned instead.
488
 * If an error occurred trying to write an updated block bitmap back to
489
 * the file, -2 is returned, and the error value is written to *err.
490
 * This can only happen for a write operation.
491
 *
492
 * The parameter write must be 1 if the offset will be used for a write
493
 * operation (the block bitmaps is updated then), 0 otherwise.
494
 * If write is true then err must not be NULL.
495
 */
496
static int64_t coroutine_fn GRAPH_RDLOCK
497
get_image_offset(BlockDriverState *bs, uint64_t offset, bool write, int *err)
498
0
{
499
0
    BDRVVPCState *s = bs->opaque;
500
0
    uint64_t bitmap_offset, block_offset;
501
0
    uint32_t pagetable_index, offset_in_block;
502
503
0
    assert(!(write && err == NULL));
504
505
0
    pagetable_index = offset / s->block_size;
506
0
    offset_in_block = offset % s->block_size;
507
508
0
    if (pagetable_index >= s->max_table_entries || s->pagetable[pagetable_index] == 0xffffffff)
509
0
        return -1; /* not allocated */
510
511
0
    bitmap_offset = 512 * (uint64_t) s->pagetable[pagetable_index];
512
0
    block_offset = bitmap_offset + s->bitmap_size + offset_in_block;
513
514
    /* We must ensure that we don't write to any sectors which are marked as
515
       unused in the bitmap. We get away with setting all bits in the block
516
       bitmap each time we write to a new block. This might cause Virtual PC to
517
       miss sparse read optimization, but it's not a problem in terms of
518
       correctness. */
519
0
    if (write && (s->last_bitmap_offset != bitmap_offset)) {
520
0
        g_autofree uint8_t *bitmap = g_malloc(s->bitmap_size);
521
0
        int r;
522
523
0
        s->last_bitmap_offset = bitmap_offset;
524
0
        memset(bitmap, 0xff, s->bitmap_size);
525
0
        r = bdrv_co_pwrite_sync(bs->file, bitmap_offset, s->bitmap_size, bitmap, 0);
526
0
        if (r < 0) {
527
0
            *err = r;
528
0
            return -2;
529
0
        }
530
0
    }
531
532
0
    return block_offset;
533
0
}
534
535
/*
536
 * Writes the footer to the end of the image file. This is needed when the
537
 * file grows as it overwrites the old footer
538
 *
539
 * Returns 0 on success and < 0 on error
540
 */
541
static int coroutine_fn GRAPH_RDLOCK rewrite_footer(BlockDriverState *bs)
542
0
{
543
0
    int ret;
544
0
    BDRVVPCState *s = bs->opaque;
545
0
    int64_t offset = s->free_data_block_offset;
546
547
0
    ret = bdrv_co_pwrite_sync(bs->file, offset, sizeof(s->footer), &s->footer, 0);
548
0
    if (ret < 0)
549
0
        return ret;
550
551
0
    return 0;
552
0
}
553
554
/*
555
 * Allocates a new block. This involves writing a new footer and updating
556
 * the Block Allocation Table to use the space at the old end of the image
557
 * file (overwriting the old footer)
558
 *
559
 * Returns the sectors' offset in the image file on success and < 0 on error
560
 */
561
static int64_t coroutine_fn GRAPH_RDLOCK
562
alloc_block(BlockDriverState *bs, int64_t offset)
563
0
{
564
0
    BDRVVPCState *s = bs->opaque;
565
0
    int64_t bat_offset;
566
0
    uint32_t index, bat_value;
567
0
    int ret;
568
0
    g_autofree uint8_t *bitmap = g_malloc(s->bitmap_size);
569
570
    /* Check if sector_num is valid */
571
0
    if ((offset < 0) || (offset > bs->total_sectors * BDRV_SECTOR_SIZE)) {
572
0
        return -EINVAL;
573
0
    }
574
575
    /* Write entry into in-memory BAT */
576
0
    index = offset / s->block_size;
577
0
    assert(s->pagetable[index] == 0xFFFFFFFF);
578
0
    s->pagetable[index] = s->free_data_block_offset / 512;
579
580
    /* Initialize the block's bitmap */
581
0
    memset(bitmap, 0xff, s->bitmap_size);
582
0
    ret = bdrv_co_pwrite_sync(bs->file, s->free_data_block_offset,
583
0
                              s->bitmap_size, bitmap, 0);
584
0
    if (ret < 0) {
585
0
        return ret;
586
0
    }
587
588
    /* Write new footer (the old one will be overwritten) */
589
0
    s->free_data_block_offset += s->block_size + s->bitmap_size;
590
0
    ret = rewrite_footer(bs);
591
0
    if (ret < 0)
592
0
        goto fail;
593
594
    /* Write BAT entry to disk */
595
0
    bat_offset = s->bat_offset + (4 * index);
596
0
    bat_value = cpu_to_be32(s->pagetable[index]);
597
0
    ret = bdrv_co_pwrite_sync(bs->file, bat_offset, 4, &bat_value, 0);
598
0
    if (ret < 0)
599
0
        goto fail;
600
601
0
    return get_image_offset(bs, offset, false, NULL);
602
603
0
fail:
604
0
    s->free_data_block_offset -= (s->block_size + s->bitmap_size);
605
0
    return ret;
606
0
}
607
608
static int coroutine_fn
609
vpc_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
610
0
{
611
0
    BDRVVPCState *s = (BDRVVPCState *)bs->opaque;
612
613
0
    if (be32_to_cpu(s->footer.type) != VHD_FIXED) {
614
0
        bdi->cluster_size = s->block_size;
615
0
    }
616
617
0
    return 0;
618
0
}
619
620
static int coroutine_fn GRAPH_RDLOCK
621
vpc_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
622
              QEMUIOVector *qiov, BdrvRequestFlags flags)
623
0
{
624
0
    BDRVVPCState *s = bs->opaque;
625
0
    int ret;
626
0
    int64_t image_offset;
627
0
    int64_t n_bytes;
628
0
    int64_t bytes_done = 0;
629
0
    QEMUIOVector local_qiov;
630
631
0
    if (be32_to_cpu(s->footer.type) == VHD_FIXED) {
632
0
        return bdrv_co_preadv(bs->file, offset, bytes, qiov, 0);
633
0
    }
634
635
0
    qemu_co_mutex_lock(&s->lock);
636
0
    qemu_iovec_init(&local_qiov, qiov->niov);
637
638
0
    while (bytes > 0) {
639
0
        image_offset = get_image_offset(bs, offset, false, NULL);
640
0
        n_bytes = MIN(bytes, s->block_size - (offset % s->block_size));
641
642
0
        if (image_offset == -1) {
643
0
            qemu_iovec_memset(qiov, bytes_done, 0, n_bytes);
644
0
        } else {
645
0
            qemu_iovec_reset(&local_qiov);
646
0
            qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
647
648
0
            qemu_co_mutex_unlock(&s->lock);
649
0
            ret = bdrv_co_preadv(bs->file, image_offset, n_bytes,
650
0
                                 &local_qiov, 0);
651
0
            qemu_co_mutex_lock(&s->lock);
652
0
            if (ret < 0) {
653
0
                goto fail;
654
0
            }
655
0
        }
656
657
0
        bytes -= n_bytes;
658
0
        offset += n_bytes;
659
0
        bytes_done += n_bytes;
660
0
    }
661
662
0
    ret = 0;
663
0
fail:
664
0
    qemu_iovec_destroy(&local_qiov);
665
0
    qemu_co_mutex_unlock(&s->lock);
666
667
0
    return ret;
668
0
}
669
670
static int coroutine_fn GRAPH_RDLOCK
671
vpc_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
672
               QEMUIOVector *qiov, BdrvRequestFlags flags)
673
0
{
674
0
    BDRVVPCState *s = bs->opaque;
675
0
    int64_t image_offset;
676
0
    int64_t n_bytes;
677
0
    int64_t bytes_done = 0;
678
0
    int ret = 0;
679
0
    QEMUIOVector local_qiov;
680
681
0
    if (be32_to_cpu(s->footer.type) == VHD_FIXED) {
682
0
        return bdrv_co_pwritev(bs->file, offset, bytes, qiov, 0);
683
0
    }
684
685
0
    qemu_co_mutex_lock(&s->lock);
686
0
    qemu_iovec_init(&local_qiov, qiov->niov);
687
688
0
    while (bytes > 0) {
689
0
        image_offset = get_image_offset(bs, offset, true, &ret);
690
0
        if (image_offset == -2) {
691
            /* Failed to write block bitmap: can't proceed with write */
692
0
            goto fail;
693
0
        }
694
0
        n_bytes = MIN(bytes, s->block_size - (offset % s->block_size));
695
696
0
        if (image_offset == -1) {
697
0
            image_offset = alloc_block(bs, offset);
698
0
            if (image_offset < 0) {
699
0
                ret = image_offset;
700
0
                goto fail;
701
0
            }
702
0
        }
703
704
0
        qemu_iovec_reset(&local_qiov);
705
0
        qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
706
707
0
        qemu_co_mutex_unlock(&s->lock);
708
0
        ret = bdrv_co_pwritev(bs->file, image_offset, n_bytes,
709
0
                              &local_qiov, 0);
710
0
        qemu_co_mutex_lock(&s->lock);
711
0
        if (ret < 0) {
712
0
            goto fail;
713
0
        }
714
715
0
        bytes -= n_bytes;
716
0
        offset += n_bytes;
717
0
        bytes_done += n_bytes;
718
0
    }
719
720
0
    ret = 0;
721
0
fail:
722
0
    qemu_iovec_destroy(&local_qiov);
723
0
    qemu_co_mutex_unlock(&s->lock);
724
725
0
    return ret;
726
0
}
727
728
static int coroutine_fn GRAPH_RDLOCK
729
vpc_co_block_status(BlockDriverState *bs, unsigned int mode,
730
                    int64_t offset, int64_t bytes,
731
                    int64_t *pnum, int64_t *map,
732
                    BlockDriverState **file)
733
0
{
734
0
    BDRVVPCState *s = bs->opaque;
735
0
    int64_t image_offset;
736
0
    bool allocated;
737
0
    int ret;
738
0
    int64_t n;
739
740
0
    if (be32_to_cpu(s->footer.type) == VHD_FIXED) {
741
0
        *pnum = bytes;
742
0
        *map = offset;
743
0
        *file = bs->file->bs;
744
0
        return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_RECURSE;
745
0
    }
746
747
0
    qemu_co_mutex_lock(&s->lock);
748
749
0
    image_offset = get_image_offset(bs, offset, false, NULL);
750
0
    allocated = (image_offset != -1);
751
0
    *pnum = 0;
752
0
    ret = BDRV_BLOCK_ZERO;
753
754
0
    do {
755
        /* All sectors in a block are contiguous (without using the bitmap) */
756
0
        n = ROUND_UP(offset + 1, s->block_size) - offset;
757
0
        n = MIN(n, bytes);
758
759
0
        *pnum += n;
760
0
        offset += n;
761
0
        bytes -= n;
762
        /* *pnum can't be greater than one block for allocated
763
         * sectors since there is always a bitmap in between. */
764
0
        if (allocated) {
765
0
            *file = bs->file->bs;
766
0
            *map = image_offset;
767
0
            ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
768
0
            break;
769
0
        }
770
0
        if (bytes == 0) {
771
0
            break;
772
0
        }
773
0
        image_offset = get_image_offset(bs, offset, false, NULL);
774
0
    } while (image_offset == -1);
775
776
0
    qemu_co_mutex_unlock(&s->lock);
777
0
    return ret;
778
0
}
779
780
/*
781
 * Calculates the number of cylinders, heads and sectors per cylinder
782
 * based on a given number of sectors. This is the algorithm described
783
 * in the VHD specification.
784
 *
785
 * Note that the geometry doesn't always exactly match total_sectors but
786
 * may round it down.
787
 *
788
 * Returns 0 on success, -EFBIG if the size is larger than 2040 GiB. Override
789
 * the hardware EIDE and ATA-2 limit of 16 heads (max disk size of 127 GB)
790
 * and instead allow up to 255 heads.
791
 */
792
static int calculate_geometry(int64_t total_sectors, uint16_t *cyls,
793
    uint8_t *heads, uint8_t *secs_per_cyl)
794
0
{
795
0
    uint32_t cyls_times_heads;
796
797
0
    total_sectors = MIN(total_sectors, VHD_MAX_GEOMETRY);
798
799
0
    if (total_sectors >= 65535LL * 16 * 63) {
800
0
        *secs_per_cyl = 255;
801
0
        *heads = 16;
802
0
        cyls_times_heads = total_sectors / *secs_per_cyl;
803
0
    } else {
804
0
        *secs_per_cyl = 17;
805
0
        cyls_times_heads = total_sectors / *secs_per_cyl;
806
0
        *heads = DIV_ROUND_UP(cyls_times_heads, 1024);
807
808
0
        if (*heads < 4) {
809
0
            *heads = 4;
810
0
        }
811
812
0
        if (cyls_times_heads >= (*heads * 1024) || *heads > 16) {
813
0
            *secs_per_cyl = 31;
814
0
            *heads = 16;
815
0
            cyls_times_heads = total_sectors / *secs_per_cyl;
816
0
        }
817
818
0
        if (cyls_times_heads >= (*heads * 1024)) {
819
0
            *secs_per_cyl = 63;
820
0
            *heads = 16;
821
0
            cyls_times_heads = total_sectors / *secs_per_cyl;
822
0
        }
823
0
    }
824
825
0
    *cyls = cyls_times_heads / *heads;
826
827
0
    return 0;
828
0
}
829
830
static int coroutine_fn create_dynamic_disk(BlockBackend *blk, VHDFooter *footer,
831
                                            int64_t total_sectors)
832
0
{
833
0
    VHDDynDiskHeader dyndisk_header;
834
0
    uint8_t bat_sector[512];
835
0
    size_t block_size, num_bat_entries;
836
0
    int i;
837
0
    int ret;
838
0
    int64_t offset = 0;
839
840
    /* Write the footer (twice: at the beginning and at the end) */
841
0
    block_size = 0x200000;
842
0
    num_bat_entries = DIV_ROUND_UP(total_sectors, block_size / 512);
843
844
0
    ret = blk_co_pwrite(blk, offset, sizeof(*footer), footer, 0);
845
0
    if (ret < 0) {
846
0
        goto fail;
847
0
    }
848
849
0
    offset = 1536 + ((num_bat_entries * 4 + 511) & ~511);
850
0
    ret = blk_co_pwrite(blk, offset, sizeof(*footer), footer, 0);
851
0
    if (ret < 0) {
852
0
        goto fail;
853
0
    }
854
855
    /* Write the initial BAT */
856
0
    offset = 3 * 512;
857
858
0
    memset(bat_sector, 0xFF, 512);
859
0
    for (i = 0; i < DIV_ROUND_UP(num_bat_entries * 4, 512); i++) {
860
0
        ret = blk_co_pwrite(blk, offset, 512, bat_sector, 0);
861
0
        if (ret < 0) {
862
0
            goto fail;
863
0
        }
864
0
        offset += 512;
865
0
    }
866
867
    /* Prepare the Dynamic Disk Header */
868
0
    memset(&dyndisk_header, 0, sizeof(dyndisk_header));
869
870
0
    memcpy(dyndisk_header.magic, "cxsparse", 8);
871
872
    /*
873
     * Note: The spec is actually wrong here for data_offset, it says
874
     * 0xFFFFFFFF, but MS tools expect all 64 bits to be set.
875
     */
876
0
    dyndisk_header.data_offset = cpu_to_be64(0xFFFFFFFFFFFFFFFFULL);
877
0
    dyndisk_header.table_offset = cpu_to_be64(3 * 512);
878
0
    dyndisk_header.version = cpu_to_be32(0x00010000);
879
0
    dyndisk_header.block_size = cpu_to_be32(block_size);
880
0
    dyndisk_header.max_table_entries = cpu_to_be32(num_bat_entries);
881
882
0
    dyndisk_header.checksum = cpu_to_be32(
883
0
        vpc_checksum(&dyndisk_header, sizeof(dyndisk_header)));
884
885
    /* Write the header */
886
0
    offset = 512;
887
888
0
    ret = blk_co_pwrite(blk, offset, sizeof(dyndisk_header), &dyndisk_header, 0);
889
0
    if (ret < 0) {
890
0
        goto fail;
891
0
    }
892
893
0
    ret = 0;
894
0
 fail:
895
0
    return ret;
896
0
}
897
898
static int coroutine_fn create_fixed_disk(BlockBackend *blk, VHDFooter *footer,
899
                                          int64_t total_size, Error **errp)
900
0
{
901
0
    int ret;
902
903
    /* Add footer to total size */
904
0
    total_size += sizeof(*footer);
905
906
0
    ret = blk_co_truncate(blk, total_size, false, PREALLOC_MODE_OFF, 0, errp);
907
0
    if (ret < 0) {
908
0
        return ret;
909
0
    }
910
911
0
    ret = blk_co_pwrite(blk, total_size - sizeof(*footer), sizeof(*footer),
912
0
                        footer, 0);
913
0
    if (ret < 0) {
914
0
        error_setg_errno(errp, -ret, "Unable to write VHD header");
915
0
        return ret;
916
0
    }
917
918
0
    return 0;
919
0
}
920
921
static int calculate_rounded_image_size(BlockdevCreateOptionsVpc *vpc_opts,
922
                                        uint16_t *out_cyls,
923
                                        uint8_t *out_heads,
924
                                        uint8_t *out_secs_per_cyl,
925
                                        int64_t *out_total_sectors,
926
                                        Error **errp)
927
0
{
928
0
    int64_t total_size = vpc_opts->size;
929
0
    uint16_t cyls = 0;
930
0
    uint8_t heads = 0;
931
0
    uint8_t secs_per_cyl = 0;
932
0
    int64_t total_sectors;
933
0
    int i;
934
935
    /*
936
     * Calculate matching total_size and geometry. Increase the number of
937
     * sectors requested until we get enough (or fail). This ensures that
938
     * qemu-img convert doesn't truncate images, but rather rounds up.
939
     *
940
     * If the image size can't be represented by a spec conformant CHS geometry,
941
     * we set the geometry to 65535 x 16 x 255 (CxHxS) sectors and use
942
     * the image size from the VHD footer to calculate total_sectors.
943
     */
944
0
    if (vpc_opts->force_size) {
945
        /* This will force the use of total_size for sector count, below */
946
0
        cyls         = VHD_CHS_MAX_C;
947
0
        heads        = VHD_CHS_MAX_H;
948
0
        secs_per_cyl = VHD_CHS_MAX_S;
949
0
    } else {
950
0
        total_sectors = MIN(VHD_MAX_GEOMETRY, total_size / BDRV_SECTOR_SIZE);
951
0
        for (i = 0; total_sectors > (int64_t)cyls * heads * secs_per_cyl; i++) {
952
0
            calculate_geometry(total_sectors + i, &cyls, &heads, &secs_per_cyl);
953
0
        }
954
0
    }
955
956
0
    if ((int64_t)cyls * heads * secs_per_cyl == VHD_MAX_GEOMETRY) {
957
0
        total_sectors = total_size / BDRV_SECTOR_SIZE;
958
        /* Allow a maximum disk size of 2040 GiB */
959
0
        if (total_sectors > VHD_MAX_SECTORS) {
960
0
            error_setg(errp, "Disk size is too large, max size is 2040 GiB");
961
0
            return -EFBIG;
962
0
        }
963
0
    } else {
964
0
        total_sectors = (int64_t) cyls * heads * secs_per_cyl;
965
0
    }
966
967
0
    *out_total_sectors = total_sectors;
968
0
    if (out_cyls) {
969
0
        *out_cyls = cyls;
970
0
        *out_heads = heads;
971
0
        *out_secs_per_cyl = secs_per_cyl;
972
0
    }
973
974
0
    return 0;
975
0
}
976
977
static int coroutine_fn GRAPH_UNLOCKED
978
vpc_co_create(BlockdevCreateOptions *opts, Error **errp)
979
0
{
980
0
    BlockdevCreateOptionsVpc *vpc_opts;
981
0
    BlockBackend *blk = NULL;
982
0
    BlockDriverState *bs = NULL;
983
984
0
    VHDFooter footer;
985
0
    uint16_t cyls = 0;
986
0
    uint8_t heads = 0;
987
0
    uint8_t secs_per_cyl = 0;
988
0
    int64_t total_sectors;
989
0
    int64_t total_size;
990
0
    int disk_type;
991
0
    int ret = -EIO;
992
0
    QemuUUID uuid;
993
994
0
    assert(opts->driver == BLOCKDEV_DRIVER_VPC);
995
0
    vpc_opts = &opts->u.vpc;
996
997
    /* Validate options and set default values */
998
0
    total_size = vpc_opts->size;
999
1000
0
    if (!vpc_opts->has_subformat) {
1001
0
        vpc_opts->subformat = BLOCKDEV_VPC_SUBFORMAT_DYNAMIC;
1002
0
    }
1003
0
    switch (vpc_opts->subformat) {
1004
0
    case BLOCKDEV_VPC_SUBFORMAT_DYNAMIC:
1005
0
        disk_type = VHD_DYNAMIC;
1006
0
        break;
1007
0
    case BLOCKDEV_VPC_SUBFORMAT_FIXED:
1008
0
        disk_type = VHD_FIXED;
1009
0
        break;
1010
0
    default:
1011
0
        g_assert_not_reached();
1012
0
    }
1013
1014
    /* Create BlockBackend to write to the image */
1015
0
    bs = bdrv_co_open_blockdev_ref(vpc_opts->file, errp);
1016
0
    if (bs == NULL) {
1017
0
        return -EIO;
1018
0
    }
1019
1020
0
    blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
1021
0
                             errp);
1022
0
    if (!blk) {
1023
0
        ret = -EPERM;
1024
0
        goto out;
1025
0
    }
1026
0
    blk_set_allow_write_beyond_eof(blk, true);
1027
1028
    /* Get geometry and check that it matches the image size*/
1029
0
    ret = calculate_rounded_image_size(vpc_opts, &cyls, &heads, &secs_per_cyl,
1030
0
                                       &total_sectors, errp);
1031
0
    if (ret < 0) {
1032
0
        goto out;
1033
0
    }
1034
1035
0
    if (total_size != total_sectors * BDRV_SECTOR_SIZE) {
1036
0
        error_setg(errp, "The requested image size cannot be represented in "
1037
0
                         "CHS geometry");
1038
0
        error_append_hint(errp, "Try size=%llu or force-size=on (the "
1039
0
                                "latter makes the image incompatible with "
1040
0
                                "Virtual PC)",
1041
0
                          total_sectors * BDRV_SECTOR_SIZE);
1042
0
        ret = -EINVAL;
1043
0
        goto out;
1044
0
    }
1045
1046
    /* Prepare the Hard Disk Footer */
1047
0
    memset(&footer, 0, sizeof(footer));
1048
1049
0
    memcpy(footer.creator, "conectix", 8);
1050
0
    if (vpc_opts->force_size) {
1051
0
        memcpy(footer.creator_app, "qem2", 4);
1052
0
    } else {
1053
0
        memcpy(footer.creator_app, "qemu", 4);
1054
0
    }
1055
0
    memcpy(footer.creator_os, "Wi2k", 4);
1056
1057
0
    footer.features = cpu_to_be32(0x02);
1058
0
    footer.version = cpu_to_be32(0x00010000);
1059
0
    if (disk_type == VHD_DYNAMIC) {
1060
0
        footer.data_offset = cpu_to_be64(sizeof(footer));
1061
0
    } else {
1062
0
        footer.data_offset = cpu_to_be64(0xFFFFFFFFFFFFFFFFULL);
1063
0
    }
1064
0
    footer.timestamp = cpu_to_be32(time(NULL) - VHD_TIMESTAMP_BASE);
1065
1066
    /* Version of Virtual PC 2007 */
1067
0
    footer.major = cpu_to_be16(0x0005);
1068
0
    footer.minor = cpu_to_be16(0x0003);
1069
0
    footer.orig_size = cpu_to_be64(total_size);
1070
0
    footer.current_size = cpu_to_be64(total_size);
1071
0
    footer.cyls = cpu_to_be16(cyls);
1072
0
    footer.heads = heads;
1073
0
    footer.secs_per_cyl = secs_per_cyl;
1074
1075
0
    footer.type = cpu_to_be32(disk_type);
1076
1077
0
    qemu_uuid_generate(&uuid);
1078
0
    footer.uuid = uuid;
1079
1080
0
    footer.checksum = cpu_to_be32(vpc_checksum(&footer, sizeof(footer)));
1081
1082
0
    if (disk_type == VHD_DYNAMIC) {
1083
0
        ret = create_dynamic_disk(blk, &footer, total_sectors);
1084
0
        if (ret < 0) {
1085
0
            error_setg(errp, "Unable to create or write VHD header");
1086
0
        }
1087
0
    } else {
1088
0
        ret = create_fixed_disk(blk, &footer, total_size, errp);
1089
0
    }
1090
1091
0
out:
1092
0
    blk_co_unref(blk);
1093
0
    bdrv_co_unref(bs);
1094
0
    return ret;
1095
0
}
1096
1097
static int coroutine_fn GRAPH_UNLOCKED
1098
vpc_co_create_opts(BlockDriver *drv, const char *filename,
1099
                   QemuOpts *opts, Error **errp)
1100
0
{
1101
0
    BlockdevCreateOptions *create_options = NULL;
1102
0
    QDict *qdict;
1103
0
    Visitor *v;
1104
0
    BlockDriverState *bs = NULL;
1105
0
    int ret;
1106
1107
0
    static const QDictRenames opt_renames[] = {
1108
0
        { VPC_OPT_FORCE_SIZE,           "force-size" },
1109
0
        { NULL, NULL },
1110
0
    };
1111
1112
    /* Parse options and convert legacy syntax */
1113
0
    qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vpc_create_opts, true);
1114
1115
0
    if (!qdict_rename_keys(qdict, opt_renames, errp)) {
1116
0
        ret = -EINVAL;
1117
0
        goto fail;
1118
0
    }
1119
1120
    /* Create and open the file (protocol layer) */
1121
0
    ret = bdrv_co_create_file(filename, opts, true, errp);
1122
0
    if (ret < 0) {
1123
0
        goto fail;
1124
0
    }
1125
1126
0
    bs = bdrv_co_open(filename, NULL, NULL,
1127
0
                      BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
1128
0
    if (bs == NULL) {
1129
0
        ret = -EIO;
1130
0
        goto fail;
1131
0
    }
1132
1133
    /* Now get the QAPI type BlockdevCreateOptions */
1134
0
    qdict_put_str(qdict, "driver", "vpc");
1135
0
    qdict_put_str(qdict, "file", bs->node_name);
1136
1137
0
    v = qobject_input_visitor_new_flat_confused(qdict, errp);
1138
0
    if (!v) {
1139
0
        ret = -EINVAL;
1140
0
        goto fail;
1141
0
    }
1142
1143
0
    visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
1144
0
    visit_free(v);
1145
0
    if (!create_options) {
1146
0
        ret = -EINVAL;
1147
0
        goto fail;
1148
0
    }
1149
1150
    /* Silently round up size */
1151
0
    assert(create_options->driver == BLOCKDEV_DRIVER_VPC);
1152
0
    create_options->u.vpc.size =
1153
0
        ROUND_UP(create_options->u.vpc.size, BDRV_SECTOR_SIZE);
1154
1155
0
    if (!create_options->u.vpc.force_size) {
1156
0
        int64_t total_sectors;
1157
0
        ret = calculate_rounded_image_size(&create_options->u.vpc, NULL, NULL,
1158
0
                                           NULL, &total_sectors, errp);
1159
0
        if (ret < 0) {
1160
0
            goto fail;
1161
0
        }
1162
1163
0
        create_options->u.vpc.size = total_sectors * BDRV_SECTOR_SIZE;
1164
0
    }
1165
1166
1167
    /* Create the vpc image (format layer) */
1168
0
    ret = vpc_co_create(create_options, errp);
1169
1170
0
fail:
1171
0
    qobject_unref(qdict);
1172
0
    bdrv_co_unref(bs);
1173
0
    qapi_free_BlockdevCreateOptions(create_options);
1174
0
    return ret;
1175
0
}
1176
1177
1178
static int GRAPH_RDLOCK vpc_has_zero_init(BlockDriverState *bs)
1179
0
{
1180
0
    BDRVVPCState *s = bs->opaque;
1181
1182
0
    if (be32_to_cpu(s->footer.type) == VHD_FIXED) {
1183
0
        return bdrv_has_zero_init(bs->file->bs);
1184
0
    } else {
1185
0
        return 1;
1186
0
    }
1187
0
}
1188
1189
static void vpc_close(BlockDriverState *bs)
1190
0
{
1191
0
    BDRVVPCState *s = bs->opaque;
1192
0
    qemu_vfree(s->pagetable);
1193
#ifdef CACHE
1194
    g_free(s->pageentry_u8);
1195
#endif
1196
1197
0
    migrate_del_blocker(&s->migration_blocker);
1198
0
}
1199
1200
static QemuOptsList vpc_create_opts = {
1201
    .name = "vpc-create-opts",
1202
    .head = QTAILQ_HEAD_INITIALIZER(vpc_create_opts.head),
1203
    .desc = {
1204
        {
1205
            .name = BLOCK_OPT_SIZE,
1206
            .type = QEMU_OPT_SIZE,
1207
            .help = "Virtual disk size"
1208
        },
1209
        {
1210
            .name = BLOCK_OPT_SUBFMT,
1211
            .type = QEMU_OPT_STRING,
1212
            .help =
1213
                "Type of virtual hard disk format. Supported formats are "
1214
                "{dynamic (default) | fixed} "
1215
        },
1216
        {
1217
            .name = VPC_OPT_FORCE_SIZE,
1218
            .type = QEMU_OPT_BOOL,
1219
            .help = "Force disk size calculation to use the actual size "
1220
                    "specified, rather than using the nearest CHS-based "
1221
                    "calculation"
1222
        },
1223
        { /* end of list */ }
1224
    }
1225
};
1226
1227
static const char *const vpc_strong_runtime_opts[] = {
1228
    VPC_OPT_SIZE_CALC,
1229
1230
    NULL
1231
};
1232
1233
static BlockDriver bdrv_vpc = {
1234
    .format_name    = "vpc",
1235
    .instance_size  = sizeof(BDRVVPCState),
1236
1237
    .bdrv_probe             = vpc_probe,
1238
    .bdrv_open              = vpc_open,
1239
    .bdrv_close             = vpc_close,
1240
    .bdrv_reopen_prepare    = vpc_reopen_prepare,
1241
    .bdrv_child_perm        = bdrv_default_perms,
1242
    .bdrv_co_create         = vpc_co_create,
1243
    .bdrv_co_create_opts    = vpc_co_create_opts,
1244
1245
    .bdrv_co_preadv             = vpc_co_preadv,
1246
    .bdrv_co_pwritev            = vpc_co_pwritev,
1247
    .bdrv_co_block_status       = vpc_co_block_status,
1248
1249
    .bdrv_co_get_info       = vpc_co_get_info,
1250
1251
    .is_format              = true,
1252
    .create_opts            = &vpc_create_opts,
1253
    .bdrv_has_zero_init     = vpc_has_zero_init,
1254
    .strong_runtime_opts    = vpc_strong_runtime_opts,
1255
};
1256
1257
static void bdrv_vpc_init(void)
1258
61
{
1259
61
    bdrv_register(&bdrv_vpc);
1260
61
}
1261
1262
block_init(bdrv_vpc_init);