Statistics
| Branch: | Revision:

root / block / vmdk.c @ 72893756

History | View | Annotate | Download (25.4 kB)

1 ea2384d3 bellard
/*
2 ea2384d3 bellard
 * Block driver for the VMDK format
3 5fafdf24 ths
 *
4 ea2384d3 bellard
 * Copyright (c) 2004 Fabrice Bellard
5 ff1afc72 bellard
 * Copyright (c) 2005 Filip Navara
6 5fafdf24 ths
 *
7 ea2384d3 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 ea2384d3 bellard
 * of this software and associated documentation files (the "Software"), to deal
9 ea2384d3 bellard
 * in the Software without restriction, including without limitation the rights
10 ea2384d3 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 ea2384d3 bellard
 * copies of the Software, and to permit persons to whom the Software is
12 ea2384d3 bellard
 * furnished to do so, subject to the following conditions:
13 ea2384d3 bellard
 *
14 ea2384d3 bellard
 * The above copyright notice and this permission notice shall be included in
15 ea2384d3 bellard
 * all copies or substantial portions of the Software.
16 ea2384d3 bellard
 *
17 ea2384d3 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 ea2384d3 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 ea2384d3 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 ea2384d3 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 ea2384d3 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 ea2384d3 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 ea2384d3 bellard
 * THE SOFTWARE.
24 ea2384d3 bellard
 */
25 5f4da8c0 ths
26 faf07963 pbrook
#include "qemu-common.h"
27 ea2384d3 bellard
#include "block_int.h"
28 5efa9d5a Anthony Liguori
#include "module.h"
29 ea2384d3 bellard
30 ea2384d3 bellard
#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
31 ea2384d3 bellard
#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
32 ea2384d3 bellard
33 ea2384d3 bellard
typedef struct {
34 ea2384d3 bellard
    uint32_t version;
35 ea2384d3 bellard
    uint32_t flags;
36 ea2384d3 bellard
    uint32_t disk_sectors;
37 ea2384d3 bellard
    uint32_t granularity;
38 ea2384d3 bellard
    uint32_t l1dir_offset;
39 ea2384d3 bellard
    uint32_t l1dir_size;
40 ea2384d3 bellard
    uint32_t file_sectors;
41 ea2384d3 bellard
    uint32_t cylinders;
42 ea2384d3 bellard
    uint32_t heads;
43 ea2384d3 bellard
    uint32_t sectors_per_track;
44 ea2384d3 bellard
} VMDK3Header;
45 ea2384d3 bellard
46 ea2384d3 bellard
typedef struct {
47 ea2384d3 bellard
    uint32_t version;
48 ea2384d3 bellard
    uint32_t flags;
49 ea2384d3 bellard
    int64_t capacity;
50 ea2384d3 bellard
    int64_t granularity;
51 ea2384d3 bellard
    int64_t desc_offset;
52 ea2384d3 bellard
    int64_t desc_size;
53 ea2384d3 bellard
    int32_t num_gtes_per_gte;
54 ea2384d3 bellard
    int64_t rgd_offset;
55 ea2384d3 bellard
    int64_t gd_offset;
56 ea2384d3 bellard
    int64_t grain_offset;
57 ea2384d3 bellard
    char filler[1];
58 ea2384d3 bellard
    char check_bytes[4];
59 ff1afc72 bellard
} __attribute__((packed)) VMDK4Header;
60 ea2384d3 bellard
61 ea2384d3 bellard
#define L2_CACHE_SIZE 16
62 ea2384d3 bellard
63 ea2384d3 bellard
typedef struct BDRVVmdkState {
64 5f4da8c0 ths
    BlockDriverState *hd;
65 ea2384d3 bellard
    int64_t l1_table_offset;
66 ff1afc72 bellard
    int64_t l1_backup_table_offset;
67 ea2384d3 bellard
    uint32_t *l1_table;
68 ff1afc72 bellard
    uint32_t *l1_backup_table;
69 ea2384d3 bellard
    unsigned int l1_size;
70 ea2384d3 bellard
    uint32_t l1_entry_sectors;
71 ea2384d3 bellard
72 ea2384d3 bellard
    unsigned int l2_size;
73 ea2384d3 bellard
    uint32_t *l2_cache;
74 ea2384d3 bellard
    uint32_t l2_cache_offsets[L2_CACHE_SIZE];
75 ea2384d3 bellard
    uint32_t l2_cache_counts[L2_CACHE_SIZE];
76 ea2384d3 bellard
77 ea2384d3 bellard
    unsigned int cluster_sectors;
78 5f4da8c0 ths
    uint32_t parent_cid;
79 ea2384d3 bellard
} BDRVVmdkState;
80 ea2384d3 bellard
81 630530a6 ths
typedef struct VmdkMetaData {
82 630530a6 ths
    uint32_t offset;
83 630530a6 ths
    unsigned int l1_index;
84 630530a6 ths
    unsigned int l2_index;
85 630530a6 ths
    unsigned int l2_offset;
86 630530a6 ths
    int valid;
87 630530a6 ths
} VmdkMetaData;
88 630530a6 ths
89 ea2384d3 bellard
static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
90 ea2384d3 bellard
{
91 ea2384d3 bellard
    uint32_t magic;
92 ea2384d3 bellard
93 ea2384d3 bellard
    if (buf_size < 4)
94 ea2384d3 bellard
        return 0;
95 ea2384d3 bellard
    magic = be32_to_cpu(*(uint32_t *)buf);
96 ea2384d3 bellard
    if (magic == VMDK3_MAGIC ||
97 ea2384d3 bellard
        magic == VMDK4_MAGIC)
98 ea2384d3 bellard
        return 100;
99 ea2384d3 bellard
    else
100 ea2384d3 bellard
        return 0;
101 ea2384d3 bellard
}
102 ea2384d3 bellard
103 5f4da8c0 ths
#define CHECK_CID 1
104 5f4da8c0 ths
105 3b46e624 ths
#define SECTOR_SIZE 512
106 5f4da8c0 ths
#define DESC_SIZE 20*SECTOR_SIZE        // 20 sectors of 512 bytes each
107 5fafdf24 ths
#define HEADER_SIZE 512                           // first sector of 512 bytes
108 5f4da8c0 ths
109 5f4da8c0 ths
static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
110 ea2384d3 bellard
{
111 5f4da8c0 ths
    char desc[DESC_SIZE];
112 5f4da8c0 ths
    uint32_t cid;
113 7ccfb2eb blueswir1
    const char *p_name, *cid_str;
114 5f4da8c0 ths
    size_t cid_str_size;
115 5f4da8c0 ths
116 5f4da8c0 ths
    /* the descriptor offset = 0x200 */
117 6511ef77 Kevin Wolf
    if (bdrv_pread(bs->file, 0x200, desc, DESC_SIZE) != DESC_SIZE)
118 5f4da8c0 ths
        return 0;
119 5f4da8c0 ths
120 5f4da8c0 ths
    if (parent) {
121 5f4da8c0 ths
        cid_str = "parentCID";
122 5f4da8c0 ths
        cid_str_size = sizeof("parentCID");
123 5f4da8c0 ths
    } else {
124 5f4da8c0 ths
        cid_str = "CID";
125 5f4da8c0 ths
        cid_str_size = sizeof("CID");
126 5f4da8c0 ths
    }
127 5f4da8c0 ths
128 511d2b14 blueswir1
    if ((p_name = strstr(desc,cid_str)) != NULL) {
129 5f4da8c0 ths
        p_name += cid_str_size;
130 5f4da8c0 ths
        sscanf(p_name,"%x",&cid);
131 5f4da8c0 ths
    }
132 5f4da8c0 ths
133 5f4da8c0 ths
    return cid;
134 5f4da8c0 ths
}
135 5f4da8c0 ths
136 5f4da8c0 ths
static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
137 5f4da8c0 ths
{
138 5f4da8c0 ths
    char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
139 5f4da8c0 ths
    char *p_name, *tmp_str;
140 5f4da8c0 ths
141 5f4da8c0 ths
    /* the descriptor offset = 0x200 */
142 6511ef77 Kevin Wolf
    if (bdrv_pread(bs->file, 0x200, desc, DESC_SIZE) != DESC_SIZE)
143 5f4da8c0 ths
        return -1;
144 5f4da8c0 ths
145 5f4da8c0 ths
    tmp_str = strstr(desc,"parentCID");
146 363a37d5 blueswir1
    pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
147 511d2b14 blueswir1
    if ((p_name = strstr(desc,"CID")) != NULL) {
148 5f4da8c0 ths
        p_name += sizeof("CID");
149 363a37d5 blueswir1
        snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid);
150 363a37d5 blueswir1
        pstrcat(desc, sizeof(desc), tmp_desc);
151 5f4da8c0 ths
    }
152 5f4da8c0 ths
153 b8852e87 Kevin Wolf
    if (bdrv_pwrite_sync(bs->file, 0x200, desc, DESC_SIZE) < 0)
154 5f4da8c0 ths
        return -1;
155 5f4da8c0 ths
    return 0;
156 5f4da8c0 ths
}
157 5f4da8c0 ths
158 5f4da8c0 ths
static int vmdk_is_cid_valid(BlockDriverState *bs)
159 5f4da8c0 ths
{
160 5f4da8c0 ths
#ifdef CHECK_CID
161 5f4da8c0 ths
    BDRVVmdkState *s = bs->opaque;
162 b171271a Kevin Wolf
    BlockDriverState *p_bs = bs->backing_hd;
163 5f4da8c0 ths
    uint32_t cur_pcid;
164 5f4da8c0 ths
165 5f4da8c0 ths
    if (p_bs) {
166 5f4da8c0 ths
        cur_pcid = vmdk_read_cid(p_bs,0);
167 5f4da8c0 ths
        if (s->parent_cid != cur_pcid)
168 5f4da8c0 ths
            // CID not valid
169 5f4da8c0 ths
            return 0;
170 5f4da8c0 ths
    }
171 5f4da8c0 ths
#endif
172 5f4da8c0 ths
    // CID valid
173 5f4da8c0 ths
    return 1;
174 5f4da8c0 ths
}
175 5f4da8c0 ths
176 5f4da8c0 ths
static int vmdk_snapshot_create(const char *filename, const char *backing_file)
177 5f4da8c0 ths
{
178 5f4da8c0 ths
    int snp_fd, p_fd;
179 53c2e716 Juan Quintela
    int ret;
180 5f4da8c0 ths
    uint32_t p_cid;
181 5fafdf24 ths
    char *p_name, *gd_buf, *rgd_buf;
182 5f4da8c0 ths
    const char *real_filename, *temp_str;
183 5f4da8c0 ths
    VMDK4Header header;
184 5f4da8c0 ths
    uint32_t gde_entries, gd_size;
185 5f4da8c0 ths
    int64_t gd_offset, rgd_offset, capacity, gt_size;
186 5f4da8c0 ths
    char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
187 7ccfb2eb blueswir1
    static const char desc_template[] =
188 5f4da8c0 ths
    "# Disk DescriptorFile\n"
189 5f4da8c0 ths
    "version=1\n"
190 5f4da8c0 ths
    "CID=%x\n"
191 5f4da8c0 ths
    "parentCID=%x\n"
192 5f4da8c0 ths
    "createType=\"monolithicSparse\"\n"
193 5f4da8c0 ths
    "parentFileNameHint=\"%s\"\n"
194 5f4da8c0 ths
    "\n"
195 5f4da8c0 ths
    "# Extent description\n"
196 7ccfb2eb blueswir1
    "RW %u SPARSE \"%s\"\n"
197 5f4da8c0 ths
    "\n"
198 5f4da8c0 ths
    "# The Disk Data Base \n"
199 5f4da8c0 ths
    "#DDB\n"
200 5f4da8c0 ths
    "\n";
201 5f4da8c0 ths
202 5f4da8c0 ths
    snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
203 5f4da8c0 ths
    if (snp_fd < 0)
204 53c2e716 Juan Quintela
        return -errno;
205 5f4da8c0 ths
    p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
206 5f4da8c0 ths
    if (p_fd < 0) {
207 5f4da8c0 ths
        close(snp_fd);
208 53c2e716 Juan Quintela
        return -errno;
209 5f4da8c0 ths
    }
210 5f4da8c0 ths
211 5f4da8c0 ths
    /* read the header */
212 53c2e716 Juan Quintela
    if (lseek(p_fd, 0x0, SEEK_SET) == -1) {
213 53c2e716 Juan Quintela
        ret = -errno;
214 5f4da8c0 ths
        goto fail;
215 53c2e716 Juan Quintela
    }
216 53c2e716 Juan Quintela
    if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) {
217 53c2e716 Juan Quintela
        ret = -errno;
218 5f4da8c0 ths
        goto fail;
219 53c2e716 Juan Quintela
    }
220 5f4da8c0 ths
221 5f4da8c0 ths
    /* write the header */
222 53c2e716 Juan Quintela
    if (lseek(snp_fd, 0x0, SEEK_SET) == -1) {
223 53c2e716 Juan Quintela
        ret = -errno;
224 5f4da8c0 ths
        goto fail;
225 53c2e716 Juan Quintela
    }
226 53c2e716 Juan Quintela
    if (write(snp_fd, hdr, HEADER_SIZE) == -1) {
227 53c2e716 Juan Quintela
        ret = -errno;
228 5f4da8c0 ths
        goto fail;
229 53c2e716 Juan Quintela
    }
230 5f4da8c0 ths
231 5f4da8c0 ths
    memset(&header, 0, sizeof(header));
232 5f4da8c0 ths
    memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
233 5f4da8c0 ths
234 53c2e716 Juan Quintela
    if (ftruncate(snp_fd, header.grain_offset << 9)) {
235 53c2e716 Juan Quintela
        ret = -errno;
236 1640366c Kirill A. Shutemov
        goto fail;
237 53c2e716 Juan Quintela
    }
238 5f4da8c0 ths
    /* the descriptor offset = 0x200 */
239 53c2e716 Juan Quintela
    if (lseek(p_fd, 0x200, SEEK_SET) == -1) {
240 53c2e716 Juan Quintela
        ret = -errno;
241 5f4da8c0 ths
        goto fail;
242 53c2e716 Juan Quintela
    }
243 53c2e716 Juan Quintela
    if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE) {
244 53c2e716 Juan Quintela
        ret = -errno;
245 5f4da8c0 ths
        goto fail;
246 53c2e716 Juan Quintela
    }
247 5f4da8c0 ths
248 511d2b14 blueswir1
    if ((p_name = strstr(p_desc,"CID")) != NULL) {
249 5f4da8c0 ths
        p_name += sizeof("CID");
250 5f4da8c0 ths
        sscanf(p_name,"%x",&p_cid);
251 5f4da8c0 ths
    }
252 5f4da8c0 ths
253 5f4da8c0 ths
    real_filename = filename;
254 5f4da8c0 ths
    if ((temp_str = strrchr(real_filename, '\\')) != NULL)
255 5f4da8c0 ths
        real_filename = temp_str + 1;
256 5f4da8c0 ths
    if ((temp_str = strrchr(real_filename, '/')) != NULL)
257 5f4da8c0 ths
        real_filename = temp_str + 1;
258 5f4da8c0 ths
    if ((temp_str = strrchr(real_filename, ':')) != NULL)
259 5f4da8c0 ths
        real_filename = temp_str + 1;
260 5f4da8c0 ths
261 363a37d5 blueswir1
    snprintf(s_desc, sizeof(s_desc), desc_template, p_cid, p_cid, backing_file,
262 363a37d5 blueswir1
             (uint32_t)header.capacity, real_filename);
263 5f4da8c0 ths
264 5f4da8c0 ths
    /* write the descriptor */
265 53c2e716 Juan Quintela
    if (lseek(snp_fd, 0x200, SEEK_SET) == -1) {
266 53c2e716 Juan Quintela
        ret = -errno;
267 5f4da8c0 ths
        goto fail;
268 53c2e716 Juan Quintela
    }
269 53c2e716 Juan Quintela
    if (write(snp_fd, s_desc, strlen(s_desc)) == -1) {
270 53c2e716 Juan Quintela
        ret = -errno;
271 5f4da8c0 ths
        goto fail;
272 53c2e716 Juan Quintela
    }
273 ea2384d3 bellard
274 5f4da8c0 ths
    gd_offset = header.gd_offset * SECTOR_SIZE;     // offset of GD table
275 5f4da8c0 ths
    rgd_offset = header.rgd_offset * SECTOR_SIZE;   // offset of RGD table
276 5f4da8c0 ths
    capacity = header.capacity * SECTOR_SIZE;       // Extent size
277 5f4da8c0 ths
    /*
278 5f4da8c0 ths
     * Each GDE span 32M disk, means:
279 5f4da8c0 ths
     * 512 GTE per GT, each GTE points to grain
280 5f4da8c0 ths
     */
281 5f4da8c0 ths
    gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
282 53c2e716 Juan Quintela
    if (!gt_size) {
283 53c2e716 Juan Quintela
        ret = -EINVAL;
284 5f4da8c0 ths
        goto fail;
285 53c2e716 Juan Quintela
    }
286 5fafdf24 ths
    gde_entries = (uint32_t)(capacity / gt_size);  // number of gde/rgde
287 5f4da8c0 ths
    gd_size = gde_entries * sizeof(uint32_t);
288 5f4da8c0 ths
289 5f4da8c0 ths
    /* write RGD */
290 5f4da8c0 ths
    rgd_buf = qemu_malloc(gd_size);
291 53c2e716 Juan Quintela
    if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) {
292 53c2e716 Juan Quintela
        ret = -errno;
293 5f4da8c0 ths
        goto fail_rgd;
294 53c2e716 Juan Quintela
    }
295 53c2e716 Juan Quintela
    if (read(p_fd, rgd_buf, gd_size) != gd_size) {
296 53c2e716 Juan Quintela
        ret = -errno;
297 5f4da8c0 ths
        goto fail_rgd;
298 53c2e716 Juan Quintela
    }
299 53c2e716 Juan Quintela
    if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) {
300 53c2e716 Juan Quintela
        ret = -errno;
301 5f4da8c0 ths
        goto fail_rgd;
302 53c2e716 Juan Quintela
    }
303 53c2e716 Juan Quintela
    if (write(snp_fd, rgd_buf, gd_size) == -1) {
304 53c2e716 Juan Quintela
        ret = -errno;
305 5f4da8c0 ths
        goto fail_rgd;
306 53c2e716 Juan Quintela
    }
307 5f4da8c0 ths
308 5f4da8c0 ths
    /* write GD */
309 5f4da8c0 ths
    gd_buf = qemu_malloc(gd_size);
310 53c2e716 Juan Quintela
    if (lseek(p_fd, gd_offset, SEEK_SET) == -1) {
311 53c2e716 Juan Quintela
        ret = -errno;
312 5f4da8c0 ths
        goto fail_gd;
313 53c2e716 Juan Quintela
    }
314 53c2e716 Juan Quintela
    if (read(p_fd, gd_buf, gd_size) != gd_size) {
315 53c2e716 Juan Quintela
        ret = -errno;
316 5f4da8c0 ths
        goto fail_gd;
317 53c2e716 Juan Quintela
    }
318 53c2e716 Juan Quintela
    if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) {
319 53c2e716 Juan Quintela
        ret = -errno;
320 5f4da8c0 ths
        goto fail_gd;
321 53c2e716 Juan Quintela
    }
322 53c2e716 Juan Quintela
    if (write(snp_fd, gd_buf, gd_size) == -1) {
323 53c2e716 Juan Quintela
        ret = -errno;
324 5f4da8c0 ths
        goto fail_gd;
325 53c2e716 Juan Quintela
    }
326 3829cb46 Juan Quintela
    ret = 0;
327 5f4da8c0 ths
328 3829cb46 Juan Quintela
fail_gd:
329 5f4da8c0 ths
    qemu_free(gd_buf);
330 3829cb46 Juan Quintela
fail_rgd:
331 5f4da8c0 ths
    qemu_free(rgd_buf);
332 3829cb46 Juan Quintela
fail:
333 5f4da8c0 ths
    close(p_fd);
334 5f4da8c0 ths
    close(snp_fd);
335 53c2e716 Juan Quintela
    return ret;
336 5f4da8c0 ths
}
337 5f4da8c0 ths
338 9949f97e Kevin Wolf
static int vmdk_parent_open(BlockDriverState *bs)
339 5f4da8c0 ths
{
340 5fafdf24 ths
    char *p_name;
341 5f4da8c0 ths
    char desc[DESC_SIZE];
342 5f4da8c0 ths
343 5f4da8c0 ths
    /* the descriptor offset = 0x200 */
344 6511ef77 Kevin Wolf
    if (bdrv_pread(bs->file, 0x200, desc, DESC_SIZE) != DESC_SIZE)
345 5f4da8c0 ths
        return -1;
346 5f4da8c0 ths
347 511d2b14 blueswir1
    if ((p_name = strstr(desc,"parentFileNameHint")) != NULL) {
348 5f4da8c0 ths
        char *end_name;
349 5f4da8c0 ths
350 5f4da8c0 ths
        p_name += sizeof("parentFileNameHint") + 1;
351 511d2b14 blueswir1
        if ((end_name = strchr(p_name,'\"')) == NULL)
352 5f4da8c0 ths
            return -1;
353 b171271a Kevin Wolf
        if ((end_name - p_name) > sizeof (bs->backing_file) - 1)
354 b34d259a balrog
            return -1;
355 3b46e624 ths
356 b171271a Kevin Wolf
        pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
357 ff1afc72 bellard
    }
358 5f4da8c0 ths
359 5f4da8c0 ths
    return 0;
360 5f4da8c0 ths
}
361 5f4da8c0 ths
362 6511ef77 Kevin Wolf
static int vmdk_open(BlockDriverState *bs, int flags)
363 5f4da8c0 ths
{
364 5f4da8c0 ths
    BDRVVmdkState *s = bs->opaque;
365 5f4da8c0 ths
    uint32_t magic;
366 6511ef77 Kevin Wolf
    int l1_size, i;
367 5f4da8c0 ths
368 6511ef77 Kevin Wolf
    if (bdrv_pread(bs->file, 0, &magic, sizeof(magic)) != sizeof(magic))
369 ea2384d3 bellard
        goto fail;
370 5f4da8c0 ths
371 7143c62c bellard
    magic = be32_to_cpu(magic);
372 ea2384d3 bellard
    if (magic == VMDK3_MAGIC) {
373 ea2384d3 bellard
        VMDK3Header header;
374 5f4da8c0 ths
375 6511ef77 Kevin Wolf
        if (bdrv_pread(bs->file, sizeof(magic), &header, sizeof(header)) != sizeof(header))
376 ea2384d3 bellard
            goto fail;
377 ea2384d3 bellard
        s->cluster_sectors = le32_to_cpu(header.granularity);
378 ea2384d3 bellard
        s->l2_size = 1 << 9;
379 ea2384d3 bellard
        s->l1_size = 1 << 6;
380 ea2384d3 bellard
        bs->total_sectors = le32_to_cpu(header.disk_sectors);
381 ff1afc72 bellard
        s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
382 ff1afc72 bellard
        s->l1_backup_table_offset = 0;
383 ea2384d3 bellard
        s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
384 ea2384d3 bellard
    } else if (magic == VMDK4_MAGIC) {
385 ea2384d3 bellard
        VMDK4Header header;
386 5f4da8c0 ths
387 6511ef77 Kevin Wolf
        if (bdrv_pread(bs->file, sizeof(magic), &header, sizeof(header)) != sizeof(header))
388 ea2384d3 bellard
            goto fail;
389 bd6ea3c8 bellard
        bs->total_sectors = le64_to_cpu(header.capacity);
390 bd6ea3c8 bellard
        s->cluster_sectors = le64_to_cpu(header.granularity);
391 ea2384d3 bellard
        s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
392 ea2384d3 bellard
        s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
393 ea2384d3 bellard
        if (s->l1_entry_sectors <= 0)
394 ea2384d3 bellard
            goto fail;
395 5fafdf24 ths
        s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
396 ea2384d3 bellard
            / s->l1_entry_sectors;
397 ff1afc72 bellard
        s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
398 ff1afc72 bellard
        s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
399 5f4da8c0 ths
400 5f4da8c0 ths
        // try to open parent images, if exist
401 9949f97e Kevin Wolf
        if (vmdk_parent_open(bs) != 0)
402 5f4da8c0 ths
            goto fail;
403 5f4da8c0 ths
        // write the CID once after the image creation
404 5f4da8c0 ths
        s->parent_cid = vmdk_read_cid(bs,1);
405 ea2384d3 bellard
    } else {
406 ea2384d3 bellard
        goto fail;
407 ea2384d3 bellard
    }
408 5f4da8c0 ths
409 ea2384d3 bellard
    /* read the L1 table */
410 ea2384d3 bellard
    l1_size = s->l1_size * sizeof(uint32_t);
411 ea2384d3 bellard
    s->l1_table = qemu_malloc(l1_size);
412 6511ef77 Kevin Wolf
    if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
413 ea2384d3 bellard
        goto fail;
414 ea2384d3 bellard
    for(i = 0; i < s->l1_size; i++) {
415 ea2384d3 bellard
        le32_to_cpus(&s->l1_table[i]);
416 ea2384d3 bellard
    }
417 ea2384d3 bellard
418 ff1afc72 bellard
    if (s->l1_backup_table_offset) {
419 ff1afc72 bellard
        s->l1_backup_table = qemu_malloc(l1_size);
420 6511ef77 Kevin Wolf
        if (bdrv_pread(bs->file, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
421 ff1afc72 bellard
            goto fail;
422 ff1afc72 bellard
        for(i = 0; i < s->l1_size; i++) {
423 ff1afc72 bellard
            le32_to_cpus(&s->l1_backup_table[i]);
424 ff1afc72 bellard
        }
425 ff1afc72 bellard
    }
426 ff1afc72 bellard
427 ea2384d3 bellard
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
428 ea2384d3 bellard
    return 0;
429 ea2384d3 bellard
 fail:
430 ff1afc72 bellard
    qemu_free(s->l1_backup_table);
431 ea2384d3 bellard
    qemu_free(s->l1_table);
432 ea2384d3 bellard
    qemu_free(s->l2_cache);
433 ea2384d3 bellard
    return -1;
434 ea2384d3 bellard
}
435 ea2384d3 bellard
436 630530a6 ths
static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
437 630530a6 ths
                                   uint64_t offset, int allocate);
438 5f4da8c0 ths
439 5f4da8c0 ths
static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset,
440 5f4da8c0 ths
                             uint64_t offset, int allocate)
441 5f4da8c0 ths
{
442 5f4da8c0 ths
    BDRVVmdkState *s = bs->opaque;
443 5f4da8c0 ths
    uint8_t  whole_grain[s->cluster_sectors*512];        // 128 sectors * 512 bytes each = grain size 64KB
444 5f4da8c0 ths
445 5f4da8c0 ths
    // we will be here if it's first write on non-exist grain(cluster).
446 5f4da8c0 ths
    // try to read from parent image, if exist
447 b171271a Kevin Wolf
    if (bs->backing_hd) {
448 c336500d Kevin Wolf
        int ret;
449 5f4da8c0 ths
450 5f4da8c0 ths
        if (!vmdk_is_cid_valid(bs))
451 5f4da8c0 ths
            return -1;
452 5f4da8c0 ths
453 c336500d Kevin Wolf
        ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain,
454 c336500d Kevin Wolf
            s->cluster_sectors);
455 c336500d Kevin Wolf
        if (ret < 0) {
456 c336500d Kevin Wolf
            return -1;
457 c336500d Kevin Wolf
        }
458 630530a6 ths
459 c336500d Kevin Wolf
        //Write grain only into the active image
460 6511ef77 Kevin Wolf
        ret = bdrv_write(bs->file, cluster_offset, whole_grain,
461 c336500d Kevin Wolf
            s->cluster_sectors);
462 c336500d Kevin Wolf
        if (ret < 0) {
463 c336500d Kevin Wolf
            return -1;
464 630530a6 ths
        }
465 630530a6 ths
    }
466 630530a6 ths
    return 0;
467 630530a6 ths
}
468 630530a6 ths
469 630530a6 ths
static int vmdk_L2update(BlockDriverState *bs, VmdkMetaData *m_data)
470 630530a6 ths
{
471 630530a6 ths
    BDRVVmdkState *s = bs->opaque;
472 630530a6 ths
473 630530a6 ths
    /* update L2 table */
474 b8852e87 Kevin Wolf
    if (bdrv_pwrite_sync(bs->file, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
475 b8852e87 Kevin Wolf
                    &(m_data->offset), sizeof(m_data->offset)) < 0)
476 630530a6 ths
        return -1;
477 630530a6 ths
    /* update backup L2 table */
478 630530a6 ths
    if (s->l1_backup_table_offset != 0) {
479 630530a6 ths
        m_data->l2_offset = s->l1_backup_table[m_data->l1_index];
480 b8852e87 Kevin Wolf
        if (bdrv_pwrite_sync(bs->file, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
481 b8852e87 Kevin Wolf
                        &(m_data->offset), sizeof(m_data->offset)) < 0)
482 5f4da8c0 ths
            return -1;
483 5f4da8c0 ths
    }
484 630530a6 ths
485 5f4da8c0 ths
    return 0;
486 5f4da8c0 ths
}
487 5f4da8c0 ths
488 630530a6 ths
static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
489 ff1afc72 bellard
                                   uint64_t offset, int allocate)
490 ea2384d3 bellard
{
491 ea2384d3 bellard
    BDRVVmdkState *s = bs->opaque;
492 ea2384d3 bellard
    unsigned int l1_index, l2_offset, l2_index;
493 ea2384d3 bellard
    int min_index, i, j;
494 630530a6 ths
    uint32_t min_count, *l2_table, tmp = 0;
495 ea2384d3 bellard
    uint64_t cluster_offset;
496 630530a6 ths
497 630530a6 ths
    if (m_data)
498 630530a6 ths
        m_data->valid = 0;
499 630530a6 ths
500 ea2384d3 bellard
    l1_index = (offset >> 9) / s->l1_entry_sectors;
501 ea2384d3 bellard
    if (l1_index >= s->l1_size)
502 ea2384d3 bellard
        return 0;
503 ea2384d3 bellard
    l2_offset = s->l1_table[l1_index];
504 ea2384d3 bellard
    if (!l2_offset)
505 ea2384d3 bellard
        return 0;
506 ea2384d3 bellard
    for(i = 0; i < L2_CACHE_SIZE; i++) {
507 ea2384d3 bellard
        if (l2_offset == s->l2_cache_offsets[i]) {
508 ea2384d3 bellard
            /* increment the hit count */
509 ea2384d3 bellard
            if (++s->l2_cache_counts[i] == 0xffffffff) {
510 ea2384d3 bellard
                for(j = 0; j < L2_CACHE_SIZE; j++) {
511 ea2384d3 bellard
                    s->l2_cache_counts[j] >>= 1;
512 ea2384d3 bellard
                }
513 ea2384d3 bellard
            }
514 ea2384d3 bellard
            l2_table = s->l2_cache + (i * s->l2_size);
515 ea2384d3 bellard
            goto found;
516 ea2384d3 bellard
        }
517 ea2384d3 bellard
    }
518 ea2384d3 bellard
    /* not found: load a new entry in the least used one */
519 ea2384d3 bellard
    min_index = 0;
520 ea2384d3 bellard
    min_count = 0xffffffff;
521 ea2384d3 bellard
    for(i = 0; i < L2_CACHE_SIZE; i++) {
522 ea2384d3 bellard
        if (s->l2_cache_counts[i] < min_count) {
523 ea2384d3 bellard
            min_count = s->l2_cache_counts[i];
524 ea2384d3 bellard
            min_index = i;
525 ea2384d3 bellard
        }
526 ea2384d3 bellard
    }
527 ea2384d3 bellard
    l2_table = s->l2_cache + (min_index * s->l2_size);
528 6511ef77 Kevin Wolf
    if (bdrv_pread(bs->file, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) !=
529 5f4da8c0 ths
                                                                        s->l2_size * sizeof(uint32_t))
530 ea2384d3 bellard
        return 0;
531 5f4da8c0 ths
532 ea2384d3 bellard
    s->l2_cache_offsets[min_index] = l2_offset;
533 ea2384d3 bellard
    s->l2_cache_counts[min_index] = 1;
534 ea2384d3 bellard
 found:
535 ea2384d3 bellard
    l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
536 ea2384d3 bellard
    cluster_offset = le32_to_cpu(l2_table[l2_index]);
537 630530a6 ths
538 ff1afc72 bellard
    if (!cluster_offset) {
539 ff1afc72 bellard
        if (!allocate)
540 ff1afc72 bellard
            return 0;
541 9949f97e Kevin Wolf
542 630530a6 ths
        // Avoid the L2 tables update for the images that have snapshots.
543 6511ef77 Kevin Wolf
        cluster_offset = bdrv_getlength(bs->file);
544 6511ef77 Kevin Wolf
        bdrv_truncate(bs->file, cluster_offset + (s->cluster_sectors << 9));
545 9949f97e Kevin Wolf
546 9949f97e Kevin Wolf
        cluster_offset >>= 9;
547 9949f97e Kevin Wolf
        tmp = cpu_to_le32(cluster_offset);
548 9949f97e Kevin Wolf
        l2_table[l2_index] = tmp;
549 630530a6 ths
550 630530a6 ths
        /* First of all we write grain itself, to avoid race condition
551 630530a6 ths
         * that may to corrupt the image.
552 630530a6 ths
         * This problem may occur because of insufficient space on host disk
553 630530a6 ths
         * or inappropriate VM shutdown.
554 630530a6 ths
         */
555 5f4da8c0 ths
        if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1)
556 5f4da8c0 ths
            return 0;
557 630530a6 ths
558 630530a6 ths
        if (m_data) {
559 630530a6 ths
            m_data->offset = tmp;
560 630530a6 ths
            m_data->l1_index = l1_index;
561 630530a6 ths
            m_data->l2_index = l2_index;
562 630530a6 ths
            m_data->l2_offset = l2_offset;
563 630530a6 ths
            m_data->valid = 1;
564 630530a6 ths
        }
565 ff1afc72 bellard
    }
566 ea2384d3 bellard
    cluster_offset <<= 9;
567 ea2384d3 bellard
    return cluster_offset;
568 ea2384d3 bellard
}
569 ea2384d3 bellard
570 5fafdf24 ths
static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
571 ea2384d3 bellard
                             int nb_sectors, int *pnum)
572 ea2384d3 bellard
{
573 ea2384d3 bellard
    BDRVVmdkState *s = bs->opaque;
574 ea2384d3 bellard
    int index_in_cluster, n;
575 ea2384d3 bellard
    uint64_t cluster_offset;
576 ea2384d3 bellard
577 630530a6 ths
    cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
578 ea2384d3 bellard
    index_in_cluster = sector_num % s->cluster_sectors;
579 ea2384d3 bellard
    n = s->cluster_sectors - index_in_cluster;
580 ea2384d3 bellard
    if (n > nb_sectors)
581 ea2384d3 bellard
        n = nb_sectors;
582 ea2384d3 bellard
    *pnum = n;
583 ea2384d3 bellard
    return (cluster_offset != 0);
584 ea2384d3 bellard
}
585 ea2384d3 bellard
586 5fafdf24 ths
static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
587 ea2384d3 bellard
                    uint8_t *buf, int nb_sectors)
588 ea2384d3 bellard
{
589 ea2384d3 bellard
    BDRVVmdkState *s = bs->opaque;
590 5f4da8c0 ths
    int index_in_cluster, n, ret;
591 ea2384d3 bellard
    uint64_t cluster_offset;
592 5f4da8c0 ths
593 ea2384d3 bellard
    while (nb_sectors > 0) {
594 630530a6 ths
        cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
595 ea2384d3 bellard
        index_in_cluster = sector_num % s->cluster_sectors;
596 ea2384d3 bellard
        n = s->cluster_sectors - index_in_cluster;
597 ea2384d3 bellard
        if (n > nb_sectors)
598 ea2384d3 bellard
            n = nb_sectors;
599 ea2384d3 bellard
        if (!cluster_offset) {
600 5f4da8c0 ths
            // try to read from parent image, if exist
601 b171271a Kevin Wolf
            if (bs->backing_hd) {
602 5f4da8c0 ths
                if (!vmdk_is_cid_valid(bs))
603 5f4da8c0 ths
                    return -1;
604 b171271a Kevin Wolf
                ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
605 5f4da8c0 ths
                if (ret < 0)
606 5f4da8c0 ths
                    return -1;
607 5f4da8c0 ths
            } else {
608 5f4da8c0 ths
                memset(buf, 0, 512 * n);
609 5f4da8c0 ths
            }
610 ea2384d3 bellard
        } else {
611 6511ef77 Kevin Wolf
            if(bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
612 ea2384d3 bellard
                return -1;
613 ea2384d3 bellard
        }
614 ea2384d3 bellard
        nb_sectors -= n;
615 ea2384d3 bellard
        sector_num += n;
616 ea2384d3 bellard
        buf += n * 512;
617 ea2384d3 bellard
    }
618 ea2384d3 bellard
    return 0;
619 ea2384d3 bellard
}
620 ea2384d3 bellard
621 5fafdf24 ths
static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
622 ea2384d3 bellard
                     const uint8_t *buf, int nb_sectors)
623 ea2384d3 bellard
{
624 ff1afc72 bellard
    BDRVVmdkState *s = bs->opaque;
625 630530a6 ths
    VmdkMetaData m_data;
626 5f4da8c0 ths
    int index_in_cluster, n;
627 ff1afc72 bellard
    uint64_t cluster_offset;
628 5f4da8c0 ths
    static int cid_update = 0;
629 ff1afc72 bellard
630 630530a6 ths
    if (sector_num > bs->total_sectors) {
631 630530a6 ths
        fprintf(stderr,
632 92868412 j_mayer
                "(VMDK) Wrong offset: sector_num=0x%" PRIx64
633 92868412 j_mayer
                " total_sectors=0x%" PRIx64 "\n",
634 630530a6 ths
                sector_num, bs->total_sectors);
635 630530a6 ths
        return -1;
636 630530a6 ths
    }
637 630530a6 ths
638 ff1afc72 bellard
    while (nb_sectors > 0) {
639 ff1afc72 bellard
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
640 ff1afc72 bellard
        n = s->cluster_sectors - index_in_cluster;
641 ff1afc72 bellard
        if (n > nb_sectors)
642 ff1afc72 bellard
            n = nb_sectors;
643 630530a6 ths
        cluster_offset = get_cluster_offset(bs, &m_data, sector_num << 9, 1);
644 ff1afc72 bellard
        if (!cluster_offset)
645 ff1afc72 bellard
            return -1;
646 630530a6 ths
647 6511ef77 Kevin Wolf
        if (bdrv_pwrite(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
648 ff1afc72 bellard
            return -1;
649 630530a6 ths
        if (m_data.valid) {
650 630530a6 ths
            /* update L2 tables */
651 630530a6 ths
            if (vmdk_L2update(bs, &m_data) == -1)
652 630530a6 ths
                return -1;
653 630530a6 ths
        }
654 ff1afc72 bellard
        nb_sectors -= n;
655 ff1afc72 bellard
        sector_num += n;
656 ff1afc72 bellard
        buf += n * 512;
657 5f4da8c0 ths
658 5f4da8c0 ths
        // update CID on the first write every time the virtual disk is opened
659 5f4da8c0 ths
        if (!cid_update) {
660 5f4da8c0 ths
            vmdk_write_cid(bs, time(NULL));
661 5f4da8c0 ths
            cid_update++;
662 5f4da8c0 ths
        }
663 ff1afc72 bellard
    }
664 ff1afc72 bellard
    return 0;
665 ea2384d3 bellard
}
666 ea2384d3 bellard
667 0e7e1989 Kevin Wolf
static int vmdk_create(const char *filename, QEMUOptionParameter *options)
668 8979b227 bellard
{
669 8979b227 bellard
    int fd, i;
670 8979b227 bellard
    VMDK4Header header;
671 8979b227 bellard
    uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
672 7ccfb2eb blueswir1
    static const char desc_template[] =
673 8979b227 bellard
        "# Disk DescriptorFile\n"
674 8979b227 bellard
        "version=1\n"
675 8979b227 bellard
        "CID=%x\n"
676 8979b227 bellard
        "parentCID=ffffffff\n"
677 8979b227 bellard
        "createType=\"monolithicSparse\"\n"
678 8979b227 bellard
        "\n"
679 8979b227 bellard
        "# Extent description\n"
680 7fd6d9fc blueswir1
        "RW %" PRId64 " SPARSE \"%s\"\n"
681 8979b227 bellard
        "\n"
682 8979b227 bellard
        "# The Disk Data Base \n"
683 8979b227 bellard
        "#DDB\n"
684 8979b227 bellard
        "\n"
685 ec36ba14 ths
        "ddb.virtualHWVersion = \"%d\"\n"
686 7fd6d9fc blueswir1
        "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
687 8979b227 bellard
        "ddb.geometry.heads = \"16\"\n"
688 8979b227 bellard
        "ddb.geometry.sectors = \"63\"\n"
689 8979b227 bellard
        "ddb.adapterType = \"ide\"\n";
690 8979b227 bellard
    char desc[1024];
691 8979b227 bellard
    const char *real_filename, *temp_str;
692 0e7e1989 Kevin Wolf
    int64_t total_size = 0;
693 0e7e1989 Kevin Wolf
    const char *backing_file = NULL;
694 0e7e1989 Kevin Wolf
    int flags = 0;
695 1640366c Kirill A. Shutemov
    int ret;
696 0e7e1989 Kevin Wolf
697 0e7e1989 Kevin Wolf
    // Read out options
698 0e7e1989 Kevin Wolf
    while (options && options->name) {
699 0e7e1989 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
700 0e7e1989 Kevin Wolf
            total_size = options->value.n / 512;
701 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
702 0e7e1989 Kevin Wolf
            backing_file = options->value.s;
703 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
704 0e7e1989 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_COMPAT6: 0;
705 0e7e1989 Kevin Wolf
        }
706 0e7e1989 Kevin Wolf
        options++;
707 0e7e1989 Kevin Wolf
    }
708 8979b227 bellard
709 8979b227 bellard
    /* XXX: add support for backing file */
710 5f4da8c0 ths
    if (backing_file) {
711 5f4da8c0 ths
        return vmdk_snapshot_create(filename, backing_file);
712 5f4da8c0 ths
    }
713 8979b227 bellard
714 8979b227 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
715 8979b227 bellard
              0644);
716 8979b227 bellard
    if (fd < 0)
717 b781cce5 Juan Quintela
        return -errno;
718 8979b227 bellard
    magic = cpu_to_be32(VMDK4_MAGIC);
719 8979b227 bellard
    memset(&header, 0, sizeof(header));
720 8979b227 bellard
    header.version = cpu_to_le32(1);
721 8979b227 bellard
    header.flags = cpu_to_le32(3); /* ?? */
722 8979b227 bellard
    header.capacity = cpu_to_le64(total_size);
723 8979b227 bellard
    header.granularity = cpu_to_le64(128);
724 8979b227 bellard
    header.num_gtes_per_gte = cpu_to_le32(512);
725 8979b227 bellard
726 8979b227 bellard
    grains = (total_size + header.granularity - 1) / header.granularity;
727 8979b227 bellard
    gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9;
728 8979b227 bellard
    gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
729 8979b227 bellard
    gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
730 8979b227 bellard
731 8979b227 bellard
    header.desc_offset = 1;
732 8979b227 bellard
    header.desc_size = 20;
733 8979b227 bellard
    header.rgd_offset = header.desc_offset + header.desc_size;
734 8979b227 bellard
    header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
735 8979b227 bellard
    header.grain_offset =
736 8979b227 bellard
       ((header.gd_offset + gd_size + (gt_size * gt_count) +
737 8979b227 bellard
         header.granularity - 1) / header.granularity) *
738 8979b227 bellard
        header.granularity;
739 8979b227 bellard
740 8979b227 bellard
    header.desc_offset = cpu_to_le64(header.desc_offset);
741 8979b227 bellard
    header.desc_size = cpu_to_le64(header.desc_size);
742 8979b227 bellard
    header.rgd_offset = cpu_to_le64(header.rgd_offset);
743 8979b227 bellard
    header.gd_offset = cpu_to_le64(header.gd_offset);
744 8979b227 bellard
    header.grain_offset = cpu_to_le64(header.grain_offset);
745 8979b227 bellard
746 8979b227 bellard
    header.check_bytes[0] = 0xa;
747 8979b227 bellard
    header.check_bytes[1] = 0x20;
748 8979b227 bellard
    header.check_bytes[2] = 0xd;
749 8979b227 bellard
    header.check_bytes[3] = 0xa;
750 3b46e624 ths
751 3b46e624 ths
    /* write all the data */
752 1640366c Kirill A. Shutemov
    ret = qemu_write_full(fd, &magic, sizeof(magic));
753 1640366c Kirill A. Shutemov
    if (ret != sizeof(magic)) {
754 b781cce5 Juan Quintela
        ret = -errno;
755 1640366c Kirill A. Shutemov
        goto exit;
756 1640366c Kirill A. Shutemov
    }
757 1640366c Kirill A. Shutemov
    ret = qemu_write_full(fd, &header, sizeof(header));
758 1640366c Kirill A. Shutemov
    if (ret != sizeof(header)) {
759 b781cce5 Juan Quintela
        ret = -errno;
760 1640366c Kirill A. Shutemov
        goto exit;
761 1640366c Kirill A. Shutemov
    }
762 8979b227 bellard
763 1640366c Kirill A. Shutemov
    ret = ftruncate(fd, header.grain_offset << 9);
764 1640366c Kirill A. Shutemov
    if (ret < 0) {
765 b781cce5 Juan Quintela
        ret = -errno;
766 1640366c Kirill A. Shutemov
        goto exit;
767 1640366c Kirill A. Shutemov
    }
768 8979b227 bellard
769 8979b227 bellard
    /* write grain directory */
770 8979b227 bellard
    lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
771 8979b227 bellard
    for (i = 0, tmp = header.rgd_offset + gd_size;
772 1640366c Kirill A. Shutemov
         i < gt_count; i++, tmp += gt_size) {
773 1640366c Kirill A. Shutemov
        ret = qemu_write_full(fd, &tmp, sizeof(tmp));
774 1640366c Kirill A. Shutemov
        if (ret != sizeof(tmp)) {
775 b781cce5 Juan Quintela
            ret = -errno;
776 1640366c Kirill A. Shutemov
            goto exit;
777 1640366c Kirill A. Shutemov
        }
778 1640366c Kirill A. Shutemov
    }
779 3b46e624 ths
780 8979b227 bellard
    /* write backup grain directory */
781 8979b227 bellard
    lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
782 8979b227 bellard
    for (i = 0, tmp = header.gd_offset + gd_size;
783 1640366c Kirill A. Shutemov
         i < gt_count; i++, tmp += gt_size) {
784 1640366c Kirill A. Shutemov
        ret = qemu_write_full(fd, &tmp, sizeof(tmp));
785 1640366c Kirill A. Shutemov
        if (ret != sizeof(tmp)) {
786 b781cce5 Juan Quintela
            ret = -errno;
787 1640366c Kirill A. Shutemov
            goto exit;
788 1640366c Kirill A. Shutemov
        }
789 1640366c Kirill A. Shutemov
    }
790 8979b227 bellard
791 8979b227 bellard
    /* compose the descriptor */
792 8979b227 bellard
    real_filename = filename;
793 8979b227 bellard
    if ((temp_str = strrchr(real_filename, '\\')) != NULL)
794 8979b227 bellard
        real_filename = temp_str + 1;
795 8979b227 bellard
    if ((temp_str = strrchr(real_filename, '/')) != NULL)
796 8979b227 bellard
        real_filename = temp_str + 1;
797 8979b227 bellard
    if ((temp_str = strrchr(real_filename, ':')) != NULL)
798 8979b227 bellard
        real_filename = temp_str + 1;
799 7ccfb2eb blueswir1
    snprintf(desc, sizeof(desc), desc_template, (unsigned int)time(NULL),
800 7fd6d9fc blueswir1
             total_size, real_filename,
801 7fd6d9fc blueswir1
             (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
802 7fd6d9fc blueswir1
             total_size / (int64_t)(63 * 16));
803 8979b227 bellard
804 8979b227 bellard
    /* write the descriptor */
805 8979b227 bellard
    lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
806 1640366c Kirill A. Shutemov
    ret = qemu_write_full(fd, desc, strlen(desc));
807 1640366c Kirill A. Shutemov
    if (ret != strlen(desc)) {
808 b781cce5 Juan Quintela
        ret = -errno;
809 1640366c Kirill A. Shutemov
        goto exit;
810 1640366c Kirill A. Shutemov
    }
811 8979b227 bellard
812 1640366c Kirill A. Shutemov
    ret = 0;
813 1640366c Kirill A. Shutemov
exit:
814 8979b227 bellard
    close(fd);
815 1640366c Kirill A. Shutemov
    return ret;
816 8979b227 bellard
}
817 8979b227 bellard
818 e2731add bellard
static void vmdk_close(BlockDriverState *bs)
819 ea2384d3 bellard
{
820 ea2384d3 bellard
    BDRVVmdkState *s = bs->opaque;
821 5f4da8c0 ths
822 ea2384d3 bellard
    qemu_free(s->l1_table);
823 ea2384d3 bellard
    qemu_free(s->l2_cache);
824 ea2384d3 bellard
}
825 ea2384d3 bellard
826 7a6cba61 pbrook
static void vmdk_flush(BlockDriverState *bs)
827 7a6cba61 pbrook
{
828 6511ef77 Kevin Wolf
    bdrv_flush(bs->file);
829 7a6cba61 pbrook
}
830 7a6cba61 pbrook
831 0e7e1989 Kevin Wolf
832 0e7e1989 Kevin Wolf
static QEMUOptionParameter vmdk_create_options[] = {
833 db08adf5 Kevin Wolf
    {
834 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_SIZE,
835 db08adf5 Kevin Wolf
        .type = OPT_SIZE,
836 db08adf5 Kevin Wolf
        .help = "Virtual disk size"
837 db08adf5 Kevin Wolf
    },
838 db08adf5 Kevin Wolf
    {
839 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_BACKING_FILE,
840 db08adf5 Kevin Wolf
        .type = OPT_STRING,
841 db08adf5 Kevin Wolf
        .help = "File name of a base image"
842 db08adf5 Kevin Wolf
    },
843 db08adf5 Kevin Wolf
    {
844 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_COMPAT6,
845 db08adf5 Kevin Wolf
        .type = OPT_FLAG,
846 db08adf5 Kevin Wolf
        .help = "VMDK version 6 image"
847 db08adf5 Kevin Wolf
    },
848 0e7e1989 Kevin Wolf
    { NULL }
849 0e7e1989 Kevin Wolf
};
850 0e7e1989 Kevin Wolf
851 5efa9d5a Anthony Liguori
static BlockDriver bdrv_vmdk = {
852 e60f469c aurel32
    .format_name        = "vmdk",
853 e60f469c aurel32
    .instance_size        = sizeof(BDRVVmdkState),
854 e60f469c aurel32
    .bdrv_probe                = vmdk_probe,
855 6511ef77 Kevin Wolf
    .bdrv_open      = vmdk_open,
856 e60f469c aurel32
    .bdrv_read                = vmdk_read,
857 e60f469c aurel32
    .bdrv_write                = vmdk_write,
858 e60f469c aurel32
    .bdrv_close                = vmdk_close,
859 e60f469c aurel32
    .bdrv_create        = vmdk_create,
860 e60f469c aurel32
    .bdrv_flush                = vmdk_flush,
861 e60f469c aurel32
    .bdrv_is_allocated        = vmdk_is_allocated,
862 0e7e1989 Kevin Wolf
863 0e7e1989 Kevin Wolf
    .create_options = vmdk_create_options,
864 ea2384d3 bellard
};
865 5efa9d5a Anthony Liguori
866 5efa9d5a Anthony Liguori
static void bdrv_vmdk_init(void)
867 5efa9d5a Anthony Liguori
{
868 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_vmdk);
869 5efa9d5a Anthony Liguori
}
870 5efa9d5a Anthony Liguori
871 5efa9d5a Anthony Liguori
block_init(bdrv_vmdk_init);