xref: /qemu/block/vpc.c (revision 95de6d70)
1019d6b8fSAnthony Liguori /*
2cc2040f8SStefan Weil  * Block driver for Connectix / Microsoft Virtual PC images
3019d6b8fSAnthony Liguori  *
4019d6b8fSAnthony Liguori  * Copyright (c) 2005 Alex Beregszaszi
5019d6b8fSAnthony Liguori  * Copyright (c) 2009 Kevin Wolf <kwolf@suse.de>
6019d6b8fSAnthony Liguori  *
7019d6b8fSAnthony Liguori  * Permission is hereby granted, free of charge, to any person obtaining a copy
8019d6b8fSAnthony Liguori  * of this software and associated documentation files (the "Software"), to deal
9019d6b8fSAnthony Liguori  * in the Software without restriction, including without limitation the rights
10019d6b8fSAnthony Liguori  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11019d6b8fSAnthony Liguori  * copies of the Software, and to permit persons to whom the Software is
12019d6b8fSAnthony Liguori  * furnished to do so, subject to the following conditions:
13019d6b8fSAnthony Liguori  *
14019d6b8fSAnthony Liguori  * The above copyright notice and this permission notice shall be included in
15019d6b8fSAnthony Liguori  * all copies or substantial portions of the Software.
16019d6b8fSAnthony Liguori  *
17019d6b8fSAnthony Liguori  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18019d6b8fSAnthony Liguori  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19019d6b8fSAnthony Liguori  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20019d6b8fSAnthony Liguori  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21019d6b8fSAnthony Liguori  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22019d6b8fSAnthony Liguori  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23019d6b8fSAnthony Liguori  * THE SOFTWARE.
24019d6b8fSAnthony Liguori  */
25019d6b8fSAnthony Liguori #include "qemu-common.h"
26737e150eSPaolo Bonzini #include "block/block_int.h"
271de7afc9SPaolo Bonzini #include "qemu/module.h"
28caf71f86SPaolo Bonzini #include "migration/migration.h"
291fe1fa51SCharles Arnold #if defined(CONFIG_UUID)
301fe1fa51SCharles Arnold #include <uuid/uuid.h>
311fe1fa51SCharles Arnold #endif
32019d6b8fSAnthony Liguori 
33019d6b8fSAnthony Liguori /**************************************************************/
34019d6b8fSAnthony Liguori 
35019d6b8fSAnthony Liguori #define HEADER_SIZE 512
36019d6b8fSAnthony Liguori 
37019d6b8fSAnthony Liguori //#define CACHE
38019d6b8fSAnthony Liguori 
39019d6b8fSAnthony Liguori enum vhd_type {
40019d6b8fSAnthony Liguori     VHD_FIXED           = 2,
41019d6b8fSAnthony Liguori     VHD_DYNAMIC         = 3,
42019d6b8fSAnthony Liguori     VHD_DIFFERENCING    = 4,
43019d6b8fSAnthony Liguori };
44019d6b8fSAnthony Liguori 
45019d6b8fSAnthony Liguori // Seconds since Jan 1, 2000 0:00:00 (UTC)
46019d6b8fSAnthony Liguori #define VHD_TIMESTAMP_BASE 946684800
47019d6b8fSAnthony Liguori 
48019d6b8fSAnthony Liguori // always big-endian
49e54835c0SJeff Cody typedef struct vhd_footer {
50019d6b8fSAnthony Liguori     char        creator[8]; // "conectix"
51019d6b8fSAnthony Liguori     uint32_t    features;
52019d6b8fSAnthony Liguori     uint32_t    version;
53019d6b8fSAnthony Liguori 
54019d6b8fSAnthony Liguori     // Offset of next header structure, 0xFFFFFFFF if none
55019d6b8fSAnthony Liguori     uint64_t    data_offset;
56019d6b8fSAnthony Liguori 
57019d6b8fSAnthony Liguori     // Seconds since Jan 1, 2000 0:00:00 (UTC)
58019d6b8fSAnthony Liguori     uint32_t    timestamp;
59019d6b8fSAnthony Liguori 
60019d6b8fSAnthony Liguori     char        creator_app[4]; // "vpc "
61019d6b8fSAnthony Liguori     uint16_t    major;
62019d6b8fSAnthony Liguori     uint16_t    minor;
63019d6b8fSAnthony Liguori     char        creator_os[4]; // "Wi2k"
64019d6b8fSAnthony Liguori 
65019d6b8fSAnthony Liguori     uint64_t    orig_size;
66019d6b8fSAnthony Liguori     uint64_t    size;
67019d6b8fSAnthony Liguori 
68019d6b8fSAnthony Liguori     uint16_t    cyls;
69019d6b8fSAnthony Liguori     uint8_t     heads;
70019d6b8fSAnthony Liguori     uint8_t     secs_per_cyl;
71019d6b8fSAnthony Liguori 
72019d6b8fSAnthony Liguori     uint32_t    type;
73019d6b8fSAnthony Liguori 
74019d6b8fSAnthony Liguori     // Checksum of the Hard Disk Footer ("one's complement of the sum of all
75019d6b8fSAnthony Liguori     // the bytes in the footer without the checksum field")
76019d6b8fSAnthony Liguori     uint32_t    checksum;
77019d6b8fSAnthony Liguori 
78019d6b8fSAnthony Liguori     // UUID used to identify a parent hard disk (backing file)
79019d6b8fSAnthony Liguori     uint8_t     uuid[16];
80019d6b8fSAnthony Liguori 
81019d6b8fSAnthony Liguori     uint8_t     in_saved_state;
82e54835c0SJeff Cody } QEMU_PACKED VHDFooter;
83019d6b8fSAnthony Liguori 
84e54835c0SJeff Cody typedef struct vhd_dyndisk_header {
85019d6b8fSAnthony Liguori     char        magic[8]; // "cxsparse"
86019d6b8fSAnthony Liguori 
87019d6b8fSAnthony Liguori     // Offset of next header structure, 0xFFFFFFFF if none
88019d6b8fSAnthony Liguori     uint64_t    data_offset;
89019d6b8fSAnthony Liguori 
90019d6b8fSAnthony Liguori     // Offset of the Block Allocation Table (BAT)
91019d6b8fSAnthony Liguori     uint64_t    table_offset;
92019d6b8fSAnthony Liguori 
93019d6b8fSAnthony Liguori     uint32_t    version;
94019d6b8fSAnthony Liguori     uint32_t    max_table_entries; // 32bit/entry
95019d6b8fSAnthony Liguori 
96019d6b8fSAnthony Liguori     // 2 MB by default, must be a power of two
97019d6b8fSAnthony Liguori     uint32_t    block_size;
98019d6b8fSAnthony Liguori 
99019d6b8fSAnthony Liguori     uint32_t    checksum;
100019d6b8fSAnthony Liguori     uint8_t     parent_uuid[16];
101019d6b8fSAnthony Liguori     uint32_t    parent_timestamp;
102019d6b8fSAnthony Liguori     uint32_t    reserved;
103019d6b8fSAnthony Liguori 
104019d6b8fSAnthony Liguori     // Backing file name (in UTF-16)
105019d6b8fSAnthony Liguori     uint8_t     parent_name[512];
106019d6b8fSAnthony Liguori 
107019d6b8fSAnthony Liguori     struct {
108019d6b8fSAnthony Liguori         uint32_t    platform;
109019d6b8fSAnthony Liguori         uint32_t    data_space;
110019d6b8fSAnthony Liguori         uint32_t    data_length;
111019d6b8fSAnthony Liguori         uint32_t    reserved;
112019d6b8fSAnthony Liguori         uint64_t    data_offset;
113019d6b8fSAnthony Liguori     } parent_locator[8];
114e54835c0SJeff Cody } QEMU_PACKED VHDDynDiskHeader;
115019d6b8fSAnthony Liguori 
116019d6b8fSAnthony Liguori typedef struct BDRVVPCState {
117848c66e8SPaolo Bonzini     CoMutex lock;
118019d6b8fSAnthony Liguori     uint8_t footer_buf[HEADER_SIZE];
119019d6b8fSAnthony Liguori     uint64_t free_data_block_offset;
120019d6b8fSAnthony Liguori     int max_table_entries;
121019d6b8fSAnthony Liguori     uint32_t *pagetable;
122019d6b8fSAnthony Liguori     uint64_t bat_offset;
123019d6b8fSAnthony Liguori     uint64_t last_bitmap_offset;
124019d6b8fSAnthony Liguori 
125019d6b8fSAnthony Liguori     uint32_t block_size;
126019d6b8fSAnthony Liguori     uint32_t bitmap_size;
127019d6b8fSAnthony Liguori 
128019d6b8fSAnthony Liguori #ifdef CACHE
129019d6b8fSAnthony Liguori     uint8_t *pageentry_u8;
130019d6b8fSAnthony Liguori     uint32_t *pageentry_u32;
131019d6b8fSAnthony Liguori     uint16_t *pageentry_u16;
132019d6b8fSAnthony Liguori 
133019d6b8fSAnthony Liguori     uint64_t last_bitmap;
134019d6b8fSAnthony Liguori #endif
135612ff3d8SKevin Wolf 
136612ff3d8SKevin Wolf     Error *migration_blocker;
137019d6b8fSAnthony Liguori } BDRVVPCState;
138019d6b8fSAnthony Liguori 
139019d6b8fSAnthony Liguori static uint32_t vpc_checksum(uint8_t* buf, size_t size)
140019d6b8fSAnthony Liguori {
141019d6b8fSAnthony Liguori     uint32_t res = 0;
142019d6b8fSAnthony Liguori     int i;
143019d6b8fSAnthony Liguori 
144019d6b8fSAnthony Liguori     for (i = 0; i < size; i++)
145019d6b8fSAnthony Liguori         res += buf[i];
146019d6b8fSAnthony Liguori 
147019d6b8fSAnthony Liguori     return ~res;
148019d6b8fSAnthony Liguori }
149019d6b8fSAnthony Liguori 
150019d6b8fSAnthony Liguori 
151019d6b8fSAnthony Liguori static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename)
152019d6b8fSAnthony Liguori {
153019d6b8fSAnthony Liguori     if (buf_size >= 8 && !strncmp((char *)buf, "conectix", 8))
154019d6b8fSAnthony Liguori 	return 100;
155019d6b8fSAnthony Liguori     return 0;
156019d6b8fSAnthony Liguori }
157019d6b8fSAnthony Liguori 
158015a1036SMax Reitz static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
159015a1036SMax Reitz                     Error **errp)
160019d6b8fSAnthony Liguori {
161019d6b8fSAnthony Liguori     BDRVVPCState *s = bs->opaque;
16266f82ceeSKevin Wolf     int i;
163e54835c0SJeff Cody     VHDFooter *footer;
164e54835c0SJeff Cody     VHDDynDiskHeader *dyndisk_header;
165019d6b8fSAnthony Liguori     uint8_t buf[HEADER_SIZE];
166019d6b8fSAnthony Liguori     uint32_t checksum;
16724da78dbSCharles Arnold     int disk_type = VHD_DYNAMIC;
16859294e46SKevin Wolf     int ret;
169019d6b8fSAnthony Liguori 
17059294e46SKevin Wolf     ret = bdrv_pread(bs->file, 0, s->footer_buf, HEADER_SIZE);
17159294e46SKevin Wolf     if (ret < 0) {
172019d6b8fSAnthony Liguori         goto fail;
17359294e46SKevin Wolf     }
174019d6b8fSAnthony Liguori 
175e54835c0SJeff Cody     footer = (VHDFooter *) s->footer_buf;
17624da78dbSCharles Arnold     if (strncmp(footer->creator, "conectix", 8)) {
17724da78dbSCharles Arnold         int64_t offset = bdrv_getlength(bs->file);
17859294e46SKevin Wolf         if (offset < 0) {
17959294e46SKevin Wolf             ret = offset;
18059294e46SKevin Wolf             goto fail;
18159294e46SKevin Wolf         } else if (offset < HEADER_SIZE) {
18259294e46SKevin Wolf             ret = -EINVAL;
183019d6b8fSAnthony Liguori             goto fail;
18424da78dbSCharles Arnold         }
18559294e46SKevin Wolf 
18624da78dbSCharles Arnold         /* If a fixed disk, the footer is found only at the end of the file */
18759294e46SKevin Wolf         ret = bdrv_pread(bs->file, offset-HEADER_SIZE, s->footer_buf,
18859294e46SKevin Wolf                          HEADER_SIZE);
18959294e46SKevin Wolf         if (ret < 0) {
19024da78dbSCharles Arnold             goto fail;
19124da78dbSCharles Arnold         }
19224da78dbSCharles Arnold         if (strncmp(footer->creator, "conectix", 8)) {
19359294e46SKevin Wolf             ret = -EMEDIUMTYPE;
19424da78dbSCharles Arnold             goto fail;
19524da78dbSCharles Arnold         }
19624da78dbSCharles Arnold         disk_type = VHD_FIXED;
19724da78dbSCharles Arnold     }
198019d6b8fSAnthony Liguori 
199019d6b8fSAnthony Liguori     checksum = be32_to_cpu(footer->checksum);
200019d6b8fSAnthony Liguori     footer->checksum = 0;
201019d6b8fSAnthony Liguori     if (vpc_checksum(s->footer_buf, HEADER_SIZE) != checksum)
202019d6b8fSAnthony Liguori         fprintf(stderr, "block-vpc: The header checksum of '%s' is "
20366f82ceeSKevin Wolf             "incorrect.\n", bs->filename);
204019d6b8fSAnthony Liguori 
205c088b691SZhang Shengju     /* Write 'checksum' back to footer, or else will leave it with zero. */
206c088b691SZhang Shengju     footer->checksum = be32_to_cpu(checksum);
207c088b691SZhang Shengju 
20833ccf667SStefan Hajnoczi     // The visible size of a image in Virtual PC depends on the geometry
20933ccf667SStefan Hajnoczi     // rather than on the size stored in the footer (the size in the footer
21033ccf667SStefan Hajnoczi     // is too large usually)
21133ccf667SStefan Hajnoczi     bs->total_sectors = (int64_t)
21233ccf667SStefan Hajnoczi         be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl;
213019d6b8fSAnthony Liguori 
2140173e7bbSPeter Lieven     /* images created with disk2vhd report a far higher virtual size
2150173e7bbSPeter Lieven      * than expected with the cyls * heads * sectors_per_cyl formula.
2160173e7bbSPeter Lieven      * use the footer->size instead if the image was created with
2170173e7bbSPeter Lieven      * disk2vhd.
2180173e7bbSPeter Lieven      */
2190173e7bbSPeter Lieven     if (!strncmp(footer->creator_app, "d2v", 4)) {
2200173e7bbSPeter Lieven         bs->total_sectors = be64_to_cpu(footer->size) / BDRV_SECTOR_SIZE;
2210173e7bbSPeter Lieven     }
2220173e7bbSPeter Lieven 
223258d2edbSCharles Arnold     /* Allow a maximum disk size of approximately 2 TB */
224258d2edbSCharles Arnold     if (bs->total_sectors >= 65535LL * 255 * 255) {
22559294e46SKevin Wolf         ret = -EFBIG;
226efc8243dSSerge E. Hallyn         goto fail;
227efc8243dSSerge E. Hallyn     }
228efc8243dSSerge E. Hallyn 
22924da78dbSCharles Arnold     if (disk_type == VHD_DYNAMIC) {
23059294e46SKevin Wolf         ret = bdrv_pread(bs->file, be64_to_cpu(footer->data_offset), buf,
23159294e46SKevin Wolf                          HEADER_SIZE);
23259294e46SKevin Wolf         if (ret < 0) {
233019d6b8fSAnthony Liguori             goto fail;
23424da78dbSCharles Arnold         }
235019d6b8fSAnthony Liguori 
236e54835c0SJeff Cody         dyndisk_header = (VHDDynDiskHeader *) buf;
237019d6b8fSAnthony Liguori 
23824da78dbSCharles Arnold         if (strncmp(dyndisk_header->magic, "cxsparse", 8)) {
23959294e46SKevin Wolf             ret = -EINVAL;
240019d6b8fSAnthony Liguori             goto fail;
24124da78dbSCharles Arnold         }
242019d6b8fSAnthony Liguori 
243019d6b8fSAnthony Liguori         s->block_size = be32_to_cpu(dyndisk_header->block_size);
244019d6b8fSAnthony Liguori         s->bitmap_size = ((s->block_size / (8 * 512)) + 511) & ~511;
245019d6b8fSAnthony Liguori 
246019d6b8fSAnthony Liguori         s->max_table_entries = be32_to_cpu(dyndisk_header->max_table_entries);
2477267c094SAnthony Liguori         s->pagetable = g_malloc(s->max_table_entries * 4);
248019d6b8fSAnthony Liguori 
249019d6b8fSAnthony Liguori         s->bat_offset = be64_to_cpu(dyndisk_header->table_offset);
25059294e46SKevin Wolf 
25159294e46SKevin Wolf         ret = bdrv_pread(bs->file, s->bat_offset, s->pagetable,
25259294e46SKevin Wolf                          s->max_table_entries * 4);
25359294e46SKevin Wolf         if (ret < 0) {
254019d6b8fSAnthony Liguori             goto fail;
25524da78dbSCharles Arnold         }
256019d6b8fSAnthony Liguori 
257019d6b8fSAnthony Liguori         s->free_data_block_offset =
258019d6b8fSAnthony Liguori             (s->bat_offset + (s->max_table_entries * 4) + 511) & ~511;
259019d6b8fSAnthony Liguori 
260019d6b8fSAnthony Liguori         for (i = 0; i < s->max_table_entries; i++) {
261019d6b8fSAnthony Liguori             be32_to_cpus(&s->pagetable[i]);
262019d6b8fSAnthony Liguori             if (s->pagetable[i] != 0xFFFFFFFF) {
263019d6b8fSAnthony Liguori                 int64_t next = (512 * (int64_t) s->pagetable[i]) +
264019d6b8fSAnthony Liguori                     s->bitmap_size + s->block_size;
265019d6b8fSAnthony Liguori 
26624da78dbSCharles Arnold                 if (next > s->free_data_block_offset) {
267019d6b8fSAnthony Liguori                     s->free_data_block_offset = next;
268019d6b8fSAnthony Liguori                 }
269019d6b8fSAnthony Liguori             }
27024da78dbSCharles Arnold         }
271019d6b8fSAnthony Liguori 
272fb8fe35fSPeter Lieven         if (s->free_data_block_offset > bdrv_getlength(bs->file)) {
273fb8fe35fSPeter Lieven             error_setg(errp, "block-vpc: free_data_block_offset points after "
274fb8fe35fSPeter Lieven                              "the end of file. The image has been truncated.");
275fb8fe35fSPeter Lieven             ret = -EINVAL;
276fb8fe35fSPeter Lieven             goto fail;
277fb8fe35fSPeter Lieven         }
278fb8fe35fSPeter Lieven 
279019d6b8fSAnthony Liguori         s->last_bitmap_offset = (int64_t) -1;
280019d6b8fSAnthony Liguori 
281019d6b8fSAnthony Liguori #ifdef CACHE
2827267c094SAnthony Liguori         s->pageentry_u8 = g_malloc(512);
283019d6b8fSAnthony Liguori         s->pageentry_u32 = s->pageentry_u8;
284019d6b8fSAnthony Liguori         s->pageentry_u16 = s->pageentry_u8;
285019d6b8fSAnthony Liguori         s->last_pagetable = -1;
286019d6b8fSAnthony Liguori #endif
28724da78dbSCharles Arnold     }
288019d6b8fSAnthony Liguori 
289848c66e8SPaolo Bonzini     qemu_co_mutex_init(&s->lock);
290612ff3d8SKevin Wolf 
291612ff3d8SKevin Wolf     /* Disable migration when VHD images are used */
292612ff3d8SKevin Wolf     error_set(&s->migration_blocker,
293612ff3d8SKevin Wolf               QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
294612ff3d8SKevin Wolf               "vpc", bs->device_name, "live migration");
295612ff3d8SKevin Wolf     migrate_add_blocker(s->migration_blocker);
296612ff3d8SKevin Wolf 
297019d6b8fSAnthony Liguori     return 0;
29859294e46SKevin Wolf 
299019d6b8fSAnthony Liguori fail:
30059294e46SKevin Wolf     g_free(s->pagetable);
30159294e46SKevin Wolf #ifdef CACHE
30259294e46SKevin Wolf     g_free(s->pageentry_u8);
30359294e46SKevin Wolf #endif
30459294e46SKevin Wolf     return ret;
305019d6b8fSAnthony Liguori }
306019d6b8fSAnthony Liguori 
3073fe4b700SJeff Cody static int vpc_reopen_prepare(BDRVReopenState *state,
3083fe4b700SJeff Cody                               BlockReopenQueue *queue, Error **errp)
3093fe4b700SJeff Cody {
3103fe4b700SJeff Cody     return 0;
3113fe4b700SJeff Cody }
3123fe4b700SJeff Cody 
313019d6b8fSAnthony Liguori /*
314019d6b8fSAnthony Liguori  * Returns the absolute byte offset of the given sector in the image file.
315019d6b8fSAnthony Liguori  * If the sector is not allocated, -1 is returned instead.
316019d6b8fSAnthony Liguori  *
317019d6b8fSAnthony Liguori  * The parameter write must be 1 if the offset will be used for a write
318019d6b8fSAnthony Liguori  * operation (the block bitmaps is updated then), 0 otherwise.
319019d6b8fSAnthony Liguori  */
320019d6b8fSAnthony Liguori static inline int64_t get_sector_offset(BlockDriverState *bs,
321019d6b8fSAnthony Liguori     int64_t sector_num, int write)
322019d6b8fSAnthony Liguori {
323019d6b8fSAnthony Liguori     BDRVVPCState *s = bs->opaque;
324019d6b8fSAnthony Liguori     uint64_t offset = sector_num * 512;
325019d6b8fSAnthony Liguori     uint64_t bitmap_offset, block_offset;
326019d6b8fSAnthony Liguori     uint32_t pagetable_index, pageentry_index;
327019d6b8fSAnthony Liguori 
328019d6b8fSAnthony Liguori     pagetable_index = offset / s->block_size;
329019d6b8fSAnthony Liguori     pageentry_index = (offset % s->block_size) / 512;
330019d6b8fSAnthony Liguori 
331019d6b8fSAnthony Liguori     if (pagetable_index >= s->max_table_entries || s->pagetable[pagetable_index] == 0xffffffff)
332019d6b8fSAnthony Liguori         return -1; // not allocated
333019d6b8fSAnthony Liguori 
334019d6b8fSAnthony Liguori     bitmap_offset = 512 * (uint64_t) s->pagetable[pagetable_index];
335019d6b8fSAnthony Liguori     block_offset = bitmap_offset + s->bitmap_size + (512 * pageentry_index);
336019d6b8fSAnthony Liguori 
337019d6b8fSAnthony Liguori     // We must ensure that we don't write to any sectors which are marked as
338019d6b8fSAnthony Liguori     // unused in the bitmap. We get away with setting all bits in the block
339019d6b8fSAnthony Liguori     // bitmap each time we write to a new block. This might cause Virtual PC to
340019d6b8fSAnthony Liguori     // miss sparse read optimization, but it's not a problem in terms of
341019d6b8fSAnthony Liguori     // correctness.
342019d6b8fSAnthony Liguori     if (write && (s->last_bitmap_offset != bitmap_offset)) {
343019d6b8fSAnthony Liguori         uint8_t bitmap[s->bitmap_size];
344019d6b8fSAnthony Liguori 
345019d6b8fSAnthony Liguori         s->last_bitmap_offset = bitmap_offset;
346019d6b8fSAnthony Liguori         memset(bitmap, 0xff, s->bitmap_size);
347078a458eSKevin Wolf         bdrv_pwrite_sync(bs->file, bitmap_offset, bitmap, s->bitmap_size);
348019d6b8fSAnthony Liguori     }
349019d6b8fSAnthony Liguori 
350019d6b8fSAnthony Liguori //    printf("sector: %" PRIx64 ", index: %x, offset: %x, bioff: %" PRIx64 ", bloff: %" PRIx64 "\n",
351019d6b8fSAnthony Liguori //	sector_num, pagetable_index, pageentry_index,
352019d6b8fSAnthony Liguori //	bitmap_offset, block_offset);
353019d6b8fSAnthony Liguori 
354019d6b8fSAnthony Liguori // disabled by reason
355019d6b8fSAnthony Liguori #if 0
356019d6b8fSAnthony Liguori #ifdef CACHE
357019d6b8fSAnthony Liguori     if (bitmap_offset != s->last_bitmap)
358019d6b8fSAnthony Liguori     {
359019d6b8fSAnthony Liguori 	lseek(s->fd, bitmap_offset, SEEK_SET);
360019d6b8fSAnthony Liguori 
361019d6b8fSAnthony Liguori 	s->last_bitmap = bitmap_offset;
362019d6b8fSAnthony Liguori 
363019d6b8fSAnthony Liguori 	// Scary! Bitmap is stored as big endian 32bit entries,
364019d6b8fSAnthony Liguori 	// while we used to look it up byte by byte
365019d6b8fSAnthony Liguori 	read(s->fd, s->pageentry_u8, 512);
366019d6b8fSAnthony Liguori 	for (i = 0; i < 128; i++)
367019d6b8fSAnthony Liguori 	    be32_to_cpus(&s->pageentry_u32[i]);
368019d6b8fSAnthony Liguori     }
369019d6b8fSAnthony Liguori 
370019d6b8fSAnthony Liguori     if ((s->pageentry_u8[pageentry_index / 8] >> (pageentry_index % 8)) & 1)
371019d6b8fSAnthony Liguori 	return -1;
372019d6b8fSAnthony Liguori #else
373019d6b8fSAnthony Liguori     lseek(s->fd, bitmap_offset + (pageentry_index / 8), SEEK_SET);
374019d6b8fSAnthony Liguori 
375019d6b8fSAnthony Liguori     read(s->fd, &bitmap_entry, 1);
376019d6b8fSAnthony Liguori 
377019d6b8fSAnthony Liguori     if ((bitmap_entry >> (pageentry_index % 8)) & 1)
378019d6b8fSAnthony Liguori 	return -1; // not allocated
379019d6b8fSAnthony Liguori #endif
380019d6b8fSAnthony Liguori #endif
381019d6b8fSAnthony Liguori 
382019d6b8fSAnthony Liguori     return block_offset;
383019d6b8fSAnthony Liguori }
384019d6b8fSAnthony Liguori 
385019d6b8fSAnthony Liguori /*
386019d6b8fSAnthony Liguori  * Writes the footer to the end of the image file. This is needed when the
387019d6b8fSAnthony Liguori  * file grows as it overwrites the old footer
388019d6b8fSAnthony Liguori  *
389019d6b8fSAnthony Liguori  * Returns 0 on success and < 0 on error
390019d6b8fSAnthony Liguori  */
391019d6b8fSAnthony Liguori static int rewrite_footer(BlockDriverState* bs)
392019d6b8fSAnthony Liguori {
393019d6b8fSAnthony Liguori     int ret;
394019d6b8fSAnthony Liguori     BDRVVPCState *s = bs->opaque;
395019d6b8fSAnthony Liguori     int64_t offset = s->free_data_block_offset;
396019d6b8fSAnthony Liguori 
397078a458eSKevin Wolf     ret = bdrv_pwrite_sync(bs->file, offset, s->footer_buf, HEADER_SIZE);
398019d6b8fSAnthony Liguori     if (ret < 0)
399019d6b8fSAnthony Liguori         return ret;
400019d6b8fSAnthony Liguori 
401019d6b8fSAnthony Liguori     return 0;
402019d6b8fSAnthony Liguori }
403019d6b8fSAnthony Liguori 
404019d6b8fSAnthony Liguori /*
405019d6b8fSAnthony Liguori  * Allocates a new block. This involves writing a new footer and updating
406019d6b8fSAnthony Liguori  * the Block Allocation Table to use the space at the old end of the image
407019d6b8fSAnthony Liguori  * file (overwriting the old footer)
408019d6b8fSAnthony Liguori  *
409019d6b8fSAnthony Liguori  * Returns the sectors' offset in the image file on success and < 0 on error
410019d6b8fSAnthony Liguori  */
411019d6b8fSAnthony Liguori static int64_t alloc_block(BlockDriverState* bs, int64_t sector_num)
412019d6b8fSAnthony Liguori {
413019d6b8fSAnthony Liguori     BDRVVPCState *s = bs->opaque;
414019d6b8fSAnthony Liguori     int64_t bat_offset;
415019d6b8fSAnthony Liguori     uint32_t index, bat_value;
416019d6b8fSAnthony Liguori     int ret;
417019d6b8fSAnthony Liguori     uint8_t bitmap[s->bitmap_size];
418019d6b8fSAnthony Liguori 
419019d6b8fSAnthony Liguori     // Check if sector_num is valid
420019d6b8fSAnthony Liguori     if ((sector_num < 0) || (sector_num > bs->total_sectors))
421019d6b8fSAnthony Liguori         return -1;
422019d6b8fSAnthony Liguori 
423019d6b8fSAnthony Liguori     // Write entry into in-memory BAT
424019d6b8fSAnthony Liguori     index = (sector_num * 512) / s->block_size;
425019d6b8fSAnthony Liguori     if (s->pagetable[index] != 0xFFFFFFFF)
426019d6b8fSAnthony Liguori         return -1;
427019d6b8fSAnthony Liguori 
428019d6b8fSAnthony Liguori     s->pagetable[index] = s->free_data_block_offset / 512;
429019d6b8fSAnthony Liguori 
430019d6b8fSAnthony Liguori     // Initialize the block's bitmap
431019d6b8fSAnthony Liguori     memset(bitmap, 0xff, s->bitmap_size);
4325bb1cbacSKevin Wolf     ret = bdrv_pwrite_sync(bs->file, s->free_data_block_offset, bitmap,
433078a458eSKevin Wolf         s->bitmap_size);
4345bb1cbacSKevin Wolf     if (ret < 0) {
4355bb1cbacSKevin Wolf         return ret;
4365bb1cbacSKevin Wolf     }
437019d6b8fSAnthony Liguori 
438019d6b8fSAnthony Liguori     // Write new footer (the old one will be overwritten)
439019d6b8fSAnthony Liguori     s->free_data_block_offset += s->block_size + s->bitmap_size;
440019d6b8fSAnthony Liguori     ret = rewrite_footer(bs);
441019d6b8fSAnthony Liguori     if (ret < 0)
442019d6b8fSAnthony Liguori         goto fail;
443019d6b8fSAnthony Liguori 
444019d6b8fSAnthony Liguori     // Write BAT entry to disk
445019d6b8fSAnthony Liguori     bat_offset = s->bat_offset + (4 * index);
446019d6b8fSAnthony Liguori     bat_value = be32_to_cpu(s->pagetable[index]);
447078a458eSKevin Wolf     ret = bdrv_pwrite_sync(bs->file, bat_offset, &bat_value, 4);
448019d6b8fSAnthony Liguori     if (ret < 0)
449019d6b8fSAnthony Liguori         goto fail;
450019d6b8fSAnthony Liguori 
451019d6b8fSAnthony Liguori     return get_sector_offset(bs, sector_num, 0);
452019d6b8fSAnthony Liguori 
453019d6b8fSAnthony Liguori fail:
454019d6b8fSAnthony Liguori     s->free_data_block_offset -= (s->block_size + s->bitmap_size);
455019d6b8fSAnthony Liguori     return -1;
456019d6b8fSAnthony Liguori }
457019d6b8fSAnthony Liguori 
45897b00e28SPaolo Bonzini static int vpc_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
45997b00e28SPaolo Bonzini {
46097b00e28SPaolo Bonzini     BDRVVPCState *s = (BDRVVPCState *)bs->opaque;
46197b00e28SPaolo Bonzini     VHDFooter *footer = (VHDFooter *) s->footer_buf;
46297b00e28SPaolo Bonzini 
46397b00e28SPaolo Bonzini     if (cpu_to_be32(footer->type) != VHD_FIXED) {
46497b00e28SPaolo Bonzini         bdi->cluster_size = s->block_size;
46597b00e28SPaolo Bonzini     }
46697b00e28SPaolo Bonzini 
467*95de6d70SPaolo Bonzini     bdi->unallocated_blocks_are_zero = true;
46897b00e28SPaolo Bonzini     return 0;
46997b00e28SPaolo Bonzini }
47097b00e28SPaolo Bonzini 
471019d6b8fSAnthony Liguori static int vpc_read(BlockDriverState *bs, int64_t sector_num,
472019d6b8fSAnthony Liguori                     uint8_t *buf, int nb_sectors)
473019d6b8fSAnthony Liguori {
4746c6ea921SKevin Wolf     BDRVVPCState *s = bs->opaque;
475019d6b8fSAnthony Liguori     int ret;
476019d6b8fSAnthony Liguori     int64_t offset;
4776c6ea921SKevin Wolf     int64_t sectors, sectors_per_block;
478e54835c0SJeff Cody     VHDFooter *footer = (VHDFooter *) s->footer_buf;
479019d6b8fSAnthony Liguori 
48024da78dbSCharles Arnold     if (cpu_to_be32(footer->type) == VHD_FIXED) {
48124da78dbSCharles Arnold         return bdrv_read(bs->file, sector_num, buf, nb_sectors);
48224da78dbSCharles Arnold     }
483019d6b8fSAnthony Liguori     while (nb_sectors > 0) {
484019d6b8fSAnthony Liguori         offset = get_sector_offset(bs, sector_num, 0);
485019d6b8fSAnthony Liguori 
4866c6ea921SKevin Wolf         sectors_per_block = s->block_size >> BDRV_SECTOR_BITS;
4876c6ea921SKevin Wolf         sectors = sectors_per_block - (sector_num % sectors_per_block);
4886c6ea921SKevin Wolf         if (sectors > nb_sectors) {
4896c6ea921SKevin Wolf             sectors = nb_sectors;
490019d6b8fSAnthony Liguori         }
491019d6b8fSAnthony Liguori 
4926c6ea921SKevin Wolf         if (offset == -1) {
4936c6ea921SKevin Wolf             memset(buf, 0, sectors * BDRV_SECTOR_SIZE);
4946c6ea921SKevin Wolf         } else {
4956c6ea921SKevin Wolf             ret = bdrv_pread(bs->file, offset, buf,
4966c6ea921SKevin Wolf                 sectors * BDRV_SECTOR_SIZE);
4976c6ea921SKevin Wolf             if (ret != sectors * BDRV_SECTOR_SIZE) {
4986c6ea921SKevin Wolf                 return -1;
4996c6ea921SKevin Wolf             }
5006c6ea921SKevin Wolf         }
5016c6ea921SKevin Wolf 
5026c6ea921SKevin Wolf         nb_sectors -= sectors;
5036c6ea921SKevin Wolf         sector_num += sectors;
5046c6ea921SKevin Wolf         buf += sectors * BDRV_SECTOR_SIZE;
505019d6b8fSAnthony Liguori     }
506019d6b8fSAnthony Liguori     return 0;
507019d6b8fSAnthony Liguori }
508019d6b8fSAnthony Liguori 
5092914caa0SPaolo Bonzini static coroutine_fn int vpc_co_read(BlockDriverState *bs, int64_t sector_num,
5102914caa0SPaolo Bonzini                                     uint8_t *buf, int nb_sectors)
5112914caa0SPaolo Bonzini {
5122914caa0SPaolo Bonzini     int ret;
5132914caa0SPaolo Bonzini     BDRVVPCState *s = bs->opaque;
5142914caa0SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
5152914caa0SPaolo Bonzini     ret = vpc_read(bs, sector_num, buf, nb_sectors);
5162914caa0SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
5172914caa0SPaolo Bonzini     return ret;
5182914caa0SPaolo Bonzini }
5192914caa0SPaolo Bonzini 
520019d6b8fSAnthony Liguori static int vpc_write(BlockDriverState *bs, int64_t sector_num,
521019d6b8fSAnthony Liguori     const uint8_t *buf, int nb_sectors)
522019d6b8fSAnthony Liguori {
5236c6ea921SKevin Wolf     BDRVVPCState *s = bs->opaque;
524019d6b8fSAnthony Liguori     int64_t offset;
5256c6ea921SKevin Wolf     int64_t sectors, sectors_per_block;
526019d6b8fSAnthony Liguori     int ret;
527e54835c0SJeff Cody     VHDFooter *footer =  (VHDFooter *) s->footer_buf;
528019d6b8fSAnthony Liguori 
52924da78dbSCharles Arnold     if (cpu_to_be32(footer->type) == VHD_FIXED) {
53024da78dbSCharles Arnold         return bdrv_write(bs->file, sector_num, buf, nb_sectors);
53124da78dbSCharles Arnold     }
532019d6b8fSAnthony Liguori     while (nb_sectors > 0) {
533019d6b8fSAnthony Liguori         offset = get_sector_offset(bs, sector_num, 1);
534019d6b8fSAnthony Liguori 
5356c6ea921SKevin Wolf         sectors_per_block = s->block_size >> BDRV_SECTOR_BITS;
5366c6ea921SKevin Wolf         sectors = sectors_per_block - (sector_num % sectors_per_block);
5376c6ea921SKevin Wolf         if (sectors > nb_sectors) {
5386c6ea921SKevin Wolf             sectors = nb_sectors;
5396c6ea921SKevin Wolf         }
5406c6ea921SKevin Wolf 
541019d6b8fSAnthony Liguori         if (offset == -1) {
542019d6b8fSAnthony Liguori             offset = alloc_block(bs, sector_num);
543019d6b8fSAnthony Liguori             if (offset < 0)
544019d6b8fSAnthony Liguori                 return -1;
545019d6b8fSAnthony Liguori         }
546019d6b8fSAnthony Liguori 
5476c6ea921SKevin Wolf         ret = bdrv_pwrite(bs->file, offset, buf, sectors * BDRV_SECTOR_SIZE);
5486c6ea921SKevin Wolf         if (ret != sectors * BDRV_SECTOR_SIZE) {
549019d6b8fSAnthony Liguori             return -1;
5506c6ea921SKevin Wolf         }
551019d6b8fSAnthony Liguori 
5526c6ea921SKevin Wolf         nb_sectors -= sectors;
5536c6ea921SKevin Wolf         sector_num += sectors;
5546c6ea921SKevin Wolf         buf += sectors * BDRV_SECTOR_SIZE;
555019d6b8fSAnthony Liguori     }
556019d6b8fSAnthony Liguori 
557019d6b8fSAnthony Liguori     return 0;
558019d6b8fSAnthony Liguori }
559019d6b8fSAnthony Liguori 
560e183ef75SPaolo Bonzini static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num,
561e183ef75SPaolo Bonzini                                      const uint8_t *buf, int nb_sectors)
562e183ef75SPaolo Bonzini {
563e183ef75SPaolo Bonzini     int ret;
564e183ef75SPaolo Bonzini     BDRVVPCState *s = bs->opaque;
565e183ef75SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
566e183ef75SPaolo Bonzini     ret = vpc_write(bs, sector_num, buf, nb_sectors);
567e183ef75SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
568e183ef75SPaolo Bonzini     return ret;
569e183ef75SPaolo Bonzini }
570e183ef75SPaolo Bonzini 
571019d6b8fSAnthony Liguori /*
572019d6b8fSAnthony Liguori  * Calculates the number of cylinders, heads and sectors per cylinder
573019d6b8fSAnthony Liguori  * based on a given number of sectors. This is the algorithm described
574019d6b8fSAnthony Liguori  * in the VHD specification.
575019d6b8fSAnthony Liguori  *
576019d6b8fSAnthony Liguori  * Note that the geometry doesn't always exactly match total_sectors but
577019d6b8fSAnthony Liguori  * may round it down.
578019d6b8fSAnthony Liguori  *
579258d2edbSCharles Arnold  * Returns 0 on success, -EFBIG if the size is larger than ~2 TB. Override
580258d2edbSCharles Arnold  * the hardware EIDE and ATA-2 limit of 16 heads (max disk size of 127 GB)
581258d2edbSCharles Arnold  * and instead allow up to 255 heads.
582019d6b8fSAnthony Liguori  */
583019d6b8fSAnthony Liguori static int calculate_geometry(int64_t total_sectors, uint16_t* cyls,
584019d6b8fSAnthony Liguori     uint8_t* heads, uint8_t* secs_per_cyl)
585019d6b8fSAnthony Liguori {
586019d6b8fSAnthony Liguori     uint32_t cyls_times_heads;
587019d6b8fSAnthony Liguori 
588258d2edbSCharles Arnold     /* Allow a maximum disk size of approximately 2 TB */
589258d2edbSCharles Arnold     if (total_sectors > 65535LL * 255 * 255) {
590019d6b8fSAnthony Liguori         return -EFBIG;
591258d2edbSCharles Arnold     }
592019d6b8fSAnthony Liguori 
593019d6b8fSAnthony Liguori     if (total_sectors > 65535 * 16 * 63) {
594019d6b8fSAnthony Liguori         *secs_per_cyl = 255;
595258d2edbSCharles Arnold         if (total_sectors > 65535 * 16 * 255) {
596258d2edbSCharles Arnold             *heads = 255;
597258d2edbSCharles Arnold         } else {
598019d6b8fSAnthony Liguori             *heads = 16;
599258d2edbSCharles Arnold         }
600019d6b8fSAnthony Liguori         cyls_times_heads = total_sectors / *secs_per_cyl;
601019d6b8fSAnthony Liguori     } else {
602019d6b8fSAnthony Liguori         *secs_per_cyl = 17;
603019d6b8fSAnthony Liguori         cyls_times_heads = total_sectors / *secs_per_cyl;
604019d6b8fSAnthony Liguori         *heads = (cyls_times_heads + 1023) / 1024;
605019d6b8fSAnthony Liguori 
606019d6b8fSAnthony Liguori         if (*heads < 4)
607019d6b8fSAnthony Liguori             *heads = 4;
608019d6b8fSAnthony Liguori 
609019d6b8fSAnthony Liguori         if (cyls_times_heads >= (*heads * 1024) || *heads > 16) {
610019d6b8fSAnthony Liguori             *secs_per_cyl = 31;
611019d6b8fSAnthony Liguori             *heads = 16;
612019d6b8fSAnthony Liguori             cyls_times_heads = total_sectors / *secs_per_cyl;
613019d6b8fSAnthony Liguori         }
614019d6b8fSAnthony Liguori 
615019d6b8fSAnthony Liguori         if (cyls_times_heads >= (*heads * 1024)) {
616019d6b8fSAnthony Liguori             *secs_per_cyl = 63;
617019d6b8fSAnthony Liguori             *heads = 16;
618019d6b8fSAnthony Liguori             cyls_times_heads = total_sectors / *secs_per_cyl;
619019d6b8fSAnthony Liguori         }
620019d6b8fSAnthony Liguori     }
621019d6b8fSAnthony Liguori 
622dede4188SStefan Weil     *cyls = cyls_times_heads / *heads;
623019d6b8fSAnthony Liguori 
624019d6b8fSAnthony Liguori     return 0;
625019d6b8fSAnthony Liguori }
626019d6b8fSAnthony Liguori 
62724da78dbSCharles Arnold static int create_dynamic_disk(int fd, uint8_t *buf, int64_t total_sectors)
628019d6b8fSAnthony Liguori {
629e54835c0SJeff Cody     VHDDynDiskHeader *dyndisk_header =
630e54835c0SJeff Cody         (VHDDynDiskHeader *) buf;
631019d6b8fSAnthony Liguori     size_t block_size, num_bat_entries;
63224da78dbSCharles Arnold     int i;
633f0ff243aSBlue Swirl     int ret = -EIO;
634019d6b8fSAnthony Liguori 
635019d6b8fSAnthony Liguori     // Write the footer (twice: at the beginning and at the end)
636019d6b8fSAnthony Liguori     block_size = 0x200000;
637019d6b8fSAnthony Liguori     num_bat_entries = (total_sectors + block_size / 512) / (block_size / 512);
638019d6b8fSAnthony Liguori 
639f0ff243aSBlue Swirl     if (write(fd, buf, HEADER_SIZE) != HEADER_SIZE) {
640f0ff243aSBlue Swirl         goto fail;
641f0ff243aSBlue Swirl     }
642019d6b8fSAnthony Liguori 
643f0ff243aSBlue Swirl     if (lseek(fd, 1536 + ((num_bat_entries * 4 + 511) & ~511), SEEK_SET) < 0) {
644f0ff243aSBlue Swirl         goto fail;
645f0ff243aSBlue Swirl     }
646f0ff243aSBlue Swirl     if (write(fd, buf, HEADER_SIZE) != HEADER_SIZE) {
647f0ff243aSBlue Swirl         goto fail;
648f0ff243aSBlue Swirl     }
649019d6b8fSAnthony Liguori 
650019d6b8fSAnthony Liguori     // Write the initial BAT
651f0ff243aSBlue Swirl     if (lseek(fd, 3 * 512, SEEK_SET) < 0) {
652f0ff243aSBlue Swirl         goto fail;
653f0ff243aSBlue Swirl     }
654019d6b8fSAnthony Liguori 
655019d6b8fSAnthony Liguori     memset(buf, 0xFF, 512);
656f0ff243aSBlue Swirl     for (i = 0; i < (num_bat_entries * 4 + 511) / 512; i++) {
657f0ff243aSBlue Swirl         if (write(fd, buf, 512) != 512) {
658f0ff243aSBlue Swirl             goto fail;
659f0ff243aSBlue Swirl         }
660f0ff243aSBlue Swirl     }
661019d6b8fSAnthony Liguori 
662019d6b8fSAnthony Liguori     // Prepare the Dynamic Disk Header
663019d6b8fSAnthony Liguori     memset(buf, 0, 1024);
664019d6b8fSAnthony Liguori 
6655ec4d682SNathan Froyd     memcpy(dyndisk_header->magic, "cxsparse", 8);
666019d6b8fSAnthony Liguori 
66778439f6aSCharles Arnold     /*
66878439f6aSCharles Arnold      * Note: The spec is actually wrong here for data_offset, it says
66978439f6aSCharles Arnold      * 0xFFFFFFFF, but MS tools expect all 64 bits to be set.
67078439f6aSCharles Arnold      */
67178439f6aSCharles Arnold     dyndisk_header->data_offset = be64_to_cpu(0xFFFFFFFFFFFFFFFFULL);
672019d6b8fSAnthony Liguori     dyndisk_header->table_offset = be64_to_cpu(3 * 512);
673019d6b8fSAnthony Liguori     dyndisk_header->version = be32_to_cpu(0x00010000);
674019d6b8fSAnthony Liguori     dyndisk_header->block_size = be32_to_cpu(block_size);
675019d6b8fSAnthony Liguori     dyndisk_header->max_table_entries = be32_to_cpu(num_bat_entries);
676019d6b8fSAnthony Liguori 
677019d6b8fSAnthony Liguori     dyndisk_header->checksum = be32_to_cpu(vpc_checksum(buf, 1024));
678019d6b8fSAnthony Liguori 
679019d6b8fSAnthony Liguori     // Write the header
680f0ff243aSBlue Swirl     if (lseek(fd, 512, SEEK_SET) < 0) {
681f0ff243aSBlue Swirl         goto fail;
682f0ff243aSBlue Swirl     }
683019d6b8fSAnthony Liguori 
684f0ff243aSBlue Swirl     if (write(fd, buf, 1024) != 1024) {
685f0ff243aSBlue Swirl         goto fail;
686f0ff243aSBlue Swirl     }
687f0ff243aSBlue Swirl     ret = 0;
688f0ff243aSBlue Swirl 
689f0ff243aSBlue Swirl  fail:
69024da78dbSCharles Arnold     return ret;
69124da78dbSCharles Arnold }
69224da78dbSCharles Arnold 
69324da78dbSCharles Arnold static int create_fixed_disk(int fd, uint8_t *buf, int64_t total_size)
69424da78dbSCharles Arnold {
69524da78dbSCharles Arnold     int ret = -EIO;
69624da78dbSCharles Arnold 
69724da78dbSCharles Arnold     /* Add footer to total size */
69824da78dbSCharles Arnold     total_size += 512;
69924da78dbSCharles Arnold     if (ftruncate(fd, total_size) != 0) {
70024da78dbSCharles Arnold         ret = -errno;
70124da78dbSCharles Arnold         goto fail;
70224da78dbSCharles Arnold     }
70324da78dbSCharles Arnold     if (lseek(fd, -512, SEEK_END) < 0) {
70424da78dbSCharles Arnold         goto fail;
70524da78dbSCharles Arnold     }
70624da78dbSCharles Arnold     if (write(fd, buf, HEADER_SIZE) != HEADER_SIZE) {
70724da78dbSCharles Arnold         goto fail;
70824da78dbSCharles Arnold     }
70924da78dbSCharles Arnold 
71024da78dbSCharles Arnold     ret = 0;
71124da78dbSCharles Arnold 
71224da78dbSCharles Arnold  fail:
71324da78dbSCharles Arnold     return ret;
71424da78dbSCharles Arnold }
71524da78dbSCharles Arnold 
716d5124c00SMax Reitz static int vpc_create(const char *filename, QEMUOptionParameter *options,
717d5124c00SMax Reitz                       Error **errp)
71824da78dbSCharles Arnold {
71924da78dbSCharles Arnold     uint8_t buf[1024];
720e54835c0SJeff Cody     VHDFooter *footer = (VHDFooter *) buf;
72124da78dbSCharles Arnold     QEMUOptionParameter *disk_type_param;
72224da78dbSCharles Arnold     int fd, i;
72324da78dbSCharles Arnold     uint16_t cyls = 0;
72424da78dbSCharles Arnold     uint8_t heads = 0;
72524da78dbSCharles Arnold     uint8_t secs_per_cyl = 0;
72624da78dbSCharles Arnold     int64_t total_sectors;
72724da78dbSCharles Arnold     int64_t total_size;
72824da78dbSCharles Arnold     int disk_type;
72924da78dbSCharles Arnold     int ret = -EIO;
73024da78dbSCharles Arnold 
73124da78dbSCharles Arnold     /* Read out options */
73224da78dbSCharles Arnold     total_size = get_option_parameter(options, BLOCK_OPT_SIZE)->value.n;
73324da78dbSCharles Arnold 
73424da78dbSCharles Arnold     disk_type_param = get_option_parameter(options, BLOCK_OPT_SUBFMT);
73524da78dbSCharles Arnold     if (disk_type_param && disk_type_param->value.s) {
73624da78dbSCharles Arnold         if (!strcmp(disk_type_param->value.s, "dynamic")) {
73724da78dbSCharles Arnold             disk_type = VHD_DYNAMIC;
73824da78dbSCharles Arnold         } else if (!strcmp(disk_type_param->value.s, "fixed")) {
73924da78dbSCharles Arnold             disk_type = VHD_FIXED;
74024da78dbSCharles Arnold         } else {
74124da78dbSCharles Arnold             return -EINVAL;
74224da78dbSCharles Arnold         }
74324da78dbSCharles Arnold     } else {
74424da78dbSCharles Arnold         disk_type = VHD_DYNAMIC;
74524da78dbSCharles Arnold     }
74624da78dbSCharles Arnold 
74724da78dbSCharles Arnold     /* Create the file */
7486165f4d8SCorey Bryant     fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
74924da78dbSCharles Arnold     if (fd < 0) {
75024da78dbSCharles Arnold         return -EIO;
75124da78dbSCharles Arnold     }
75224da78dbSCharles Arnold 
753ecd880d9SKevin Wolf     /*
754ecd880d9SKevin Wolf      * Calculate matching total_size and geometry. Increase the number of
755ecd880d9SKevin Wolf      * sectors requested until we get enough (or fail). This ensures that
756ecd880d9SKevin Wolf      * qemu-img convert doesn't truncate images, but rather rounds up.
757ecd880d9SKevin Wolf      */
75824da78dbSCharles Arnold     total_sectors = total_size / BDRV_SECTOR_SIZE;
759ecd880d9SKevin Wolf     for (i = 0; total_sectors > (int64_t)cyls * heads * secs_per_cyl; i++) {
760ecd880d9SKevin Wolf         if (calculate_geometry(total_sectors + i, &cyls, &heads,
761ecd880d9SKevin Wolf                                &secs_per_cyl))
762ecd880d9SKevin Wolf         {
76324da78dbSCharles Arnold             ret = -EFBIG;
76424da78dbSCharles Arnold             goto fail;
76524da78dbSCharles Arnold         }
76624da78dbSCharles Arnold     }
767ecd880d9SKevin Wolf 
76824da78dbSCharles Arnold     total_sectors = (int64_t) cyls * heads * secs_per_cyl;
76924da78dbSCharles Arnold 
77024da78dbSCharles Arnold     /* Prepare the Hard Disk Footer */
77124da78dbSCharles Arnold     memset(buf, 0, 1024);
77224da78dbSCharles Arnold 
77324da78dbSCharles Arnold     memcpy(footer->creator, "conectix", 8);
77424da78dbSCharles Arnold     /* TODO Check if "qemu" creator_app is ok for VPC */
77524da78dbSCharles Arnold     memcpy(footer->creator_app, "qemu", 4);
77624da78dbSCharles Arnold     memcpy(footer->creator_os, "Wi2k", 4);
77724da78dbSCharles Arnold 
77824da78dbSCharles Arnold     footer->features = be32_to_cpu(0x02);
77924da78dbSCharles Arnold     footer->version = be32_to_cpu(0x00010000);
78024da78dbSCharles Arnold     if (disk_type == VHD_DYNAMIC) {
78124da78dbSCharles Arnold         footer->data_offset = be64_to_cpu(HEADER_SIZE);
78224da78dbSCharles Arnold     } else {
78324da78dbSCharles Arnold         footer->data_offset = be64_to_cpu(0xFFFFFFFFFFFFFFFFULL);
78424da78dbSCharles Arnold     }
78524da78dbSCharles Arnold     footer->timestamp = be32_to_cpu(time(NULL) - VHD_TIMESTAMP_BASE);
78624da78dbSCharles Arnold 
78724da78dbSCharles Arnold     /* Version of Virtual PC 2007 */
78824da78dbSCharles Arnold     footer->major = be16_to_cpu(0x0005);
78924da78dbSCharles Arnold     footer->minor = be16_to_cpu(0x0003);
79024da78dbSCharles Arnold     if (disk_type == VHD_DYNAMIC) {
79124da78dbSCharles Arnold         footer->orig_size = be64_to_cpu(total_sectors * 512);
79224da78dbSCharles Arnold         footer->size = be64_to_cpu(total_sectors * 512);
79324da78dbSCharles Arnold     } else {
79424da78dbSCharles Arnold         footer->orig_size = be64_to_cpu(total_size);
79524da78dbSCharles Arnold         footer->size = be64_to_cpu(total_size);
79624da78dbSCharles Arnold     }
79724da78dbSCharles Arnold     footer->cyls = be16_to_cpu(cyls);
79824da78dbSCharles Arnold     footer->heads = heads;
79924da78dbSCharles Arnold     footer->secs_per_cyl = secs_per_cyl;
80024da78dbSCharles Arnold 
80124da78dbSCharles Arnold     footer->type = be32_to_cpu(disk_type);
80224da78dbSCharles Arnold 
8031fe1fa51SCharles Arnold #if defined(CONFIG_UUID)
8041fe1fa51SCharles Arnold     uuid_generate(footer->uuid);
8051fe1fa51SCharles Arnold #endif
80624da78dbSCharles Arnold 
80724da78dbSCharles Arnold     footer->checksum = be32_to_cpu(vpc_checksum(buf, HEADER_SIZE));
80824da78dbSCharles Arnold 
80924da78dbSCharles Arnold     if (disk_type == VHD_DYNAMIC) {
81024da78dbSCharles Arnold         ret = create_dynamic_disk(fd, buf, total_sectors);
81124da78dbSCharles Arnold     } else {
81224da78dbSCharles Arnold         ret = create_fixed_disk(fd, buf, total_size);
81324da78dbSCharles Arnold     }
81424da78dbSCharles Arnold 
81524da78dbSCharles Arnold  fail:
8162e1e79daSCorey Bryant     qemu_close(fd);
817f0ff243aSBlue Swirl     return ret;
818019d6b8fSAnthony Liguori }
819019d6b8fSAnthony Liguori 
82072c6cc94SKevin Wolf static int vpc_has_zero_init(BlockDriverState *bs)
82172c6cc94SKevin Wolf {
82272c6cc94SKevin Wolf     BDRVVPCState *s = bs->opaque;
823e54835c0SJeff Cody     VHDFooter *footer =  (VHDFooter *) s->footer_buf;
82472c6cc94SKevin Wolf 
82572c6cc94SKevin Wolf     if (cpu_to_be32(footer->type) == VHD_FIXED) {
82672c6cc94SKevin Wolf         return bdrv_has_zero_init(bs->file);
82772c6cc94SKevin Wolf     } else {
82872c6cc94SKevin Wolf         return 1;
82972c6cc94SKevin Wolf     }
83072c6cc94SKevin Wolf }
83172c6cc94SKevin Wolf 
832019d6b8fSAnthony Liguori static void vpc_close(BlockDriverState *bs)
833019d6b8fSAnthony Liguori {
834019d6b8fSAnthony Liguori     BDRVVPCState *s = bs->opaque;
8357267c094SAnthony Liguori     g_free(s->pagetable);
836019d6b8fSAnthony Liguori #ifdef CACHE
8377267c094SAnthony Liguori     g_free(s->pageentry_u8);
838019d6b8fSAnthony Liguori #endif
839612ff3d8SKevin Wolf 
840612ff3d8SKevin Wolf     migrate_del_blocker(s->migration_blocker);
841612ff3d8SKevin Wolf     error_free(s->migration_blocker);
842019d6b8fSAnthony Liguori }
843019d6b8fSAnthony Liguori 
8440e7e1989SKevin Wolf static QEMUOptionParameter vpc_create_options[] = {
845db08adf5SKevin Wolf     {
846db08adf5SKevin Wolf         .name = BLOCK_OPT_SIZE,
847db08adf5SKevin Wolf         .type = OPT_SIZE,
848db08adf5SKevin Wolf         .help = "Virtual disk size"
849db08adf5SKevin Wolf     },
85024da78dbSCharles Arnold     {
85124da78dbSCharles Arnold         .name = BLOCK_OPT_SUBFMT,
85224da78dbSCharles Arnold         .type = OPT_STRING,
85324da78dbSCharles Arnold         .help =
85424da78dbSCharles Arnold             "Type of virtual hard disk format. Supported formats are "
85524da78dbSCharles Arnold             "{dynamic (default) | fixed} "
85624da78dbSCharles Arnold     },
8570e7e1989SKevin Wolf     { NULL }
8580e7e1989SKevin Wolf };
8590e7e1989SKevin Wolf 
860019d6b8fSAnthony Liguori static BlockDriver bdrv_vpc = {
861019d6b8fSAnthony Liguori     .format_name    = "vpc",
862019d6b8fSAnthony Liguori     .instance_size  = sizeof(BDRVVPCState),
863c68b89acSKevin Wolf 
864019d6b8fSAnthony Liguori     .bdrv_probe             = vpc_probe,
865019d6b8fSAnthony Liguori     .bdrv_open              = vpc_open,
866019d6b8fSAnthony Liguori     .bdrv_close             = vpc_close,
8673fe4b700SJeff Cody     .bdrv_reopen_prepare    = vpc_reopen_prepare,
868019d6b8fSAnthony Liguori     .bdrv_create            = vpc_create,
8690e7e1989SKevin Wolf 
870c68b89acSKevin Wolf     .bdrv_read              = vpc_co_read,
871c68b89acSKevin Wolf     .bdrv_write             = vpc_co_write,
872c68b89acSKevin Wolf 
87397b00e28SPaolo Bonzini     .bdrv_get_info          = vpc_get_info,
87497b00e28SPaolo Bonzini 
8750e7e1989SKevin Wolf     .create_options         = vpc_create_options,
87672c6cc94SKevin Wolf     .bdrv_has_zero_init     = vpc_has_zero_init,
877019d6b8fSAnthony Liguori };
878019d6b8fSAnthony Liguori 
879019d6b8fSAnthony Liguori static void bdrv_vpc_init(void)
880019d6b8fSAnthony Liguori {
881019d6b8fSAnthony Liguori     bdrv_register(&bdrv_vpc);
882019d6b8fSAnthony Liguori }
883019d6b8fSAnthony Liguori 
884019d6b8fSAnthony Liguori block_init(bdrv_vpc_init);
885