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