2 * Block driver for the VMDK format
4 * Copyright (c) 2004 Fabrice Bellard
5 * Copyright (c) 2005 Filip Navara
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #include "block_int.h"
28 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
29 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
34 uint32_t disk_sectors;
36 uint32_t l1dir_offset;
38 uint32_t file_sectors;
41 uint32_t sectors_per_track;
51 int32_t num_gtes_per_gte;
57 } __attribute__((packed)) VMDK4Header;
59 #define L2_CACHE_SIZE 16
61 typedef struct BDRVVmdkState {
63 int64_t l1_table_offset;
64 int64_t l1_backup_table_offset;
66 uint32_t *l1_backup_table;
68 uint32_t l1_entry_sectors;
72 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
73 uint32_t l2_cache_counts[L2_CACHE_SIZE];
75 unsigned int cluster_sectors;
78 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
84 magic = be32_to_cpu(*(uint32_t *)buf);
85 if (magic == VMDK3_MAGIC ||
92 static int vmdk_open(BlockDriverState *bs, const char *filename)
94 BDRVVmdkState *s = bs->opaque;
99 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
101 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
106 if (read(fd, &magic, sizeof(magic)) != sizeof(magic))
108 magic = be32_to_cpu(magic);
109 if (magic == VMDK3_MAGIC) {
111 if (read(fd, &header, sizeof(header)) !=
114 s->cluster_sectors = le32_to_cpu(header.granularity);
117 bs->total_sectors = le32_to_cpu(header.disk_sectors);
118 s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
119 s->l1_backup_table_offset = 0;
120 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
121 } else if (magic == VMDK4_MAGIC) {
124 if (read(fd, &header, sizeof(header)) != sizeof(header))
126 bs->total_sectors = le32_to_cpu(header.capacity);
127 s->cluster_sectors = le32_to_cpu(header.granularity);
128 s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
129 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
130 if (s->l1_entry_sectors <= 0)
132 s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
133 / s->l1_entry_sectors;
134 s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
135 s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
139 /* read the L1 table */
140 l1_size = s->l1_size * sizeof(uint32_t);
141 s->l1_table = qemu_malloc(l1_size);
144 if (lseek(fd, s->l1_table_offset, SEEK_SET) == -1)
146 if (read(fd, s->l1_table, l1_size) != l1_size)
148 for(i = 0; i < s->l1_size; i++) {
149 le32_to_cpus(&s->l1_table[i]);
152 if (s->l1_backup_table_offset) {
153 s->l1_backup_table = qemu_malloc(l1_size);
154 if (!s->l1_backup_table)
156 if (lseek(fd, s->l1_backup_table_offset, SEEK_SET) == -1)
158 if (read(fd, s->l1_backup_table, l1_size) != l1_size)
160 for(i = 0; i < s->l1_size; i++) {
161 le32_to_cpus(&s->l1_backup_table[i]);
165 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
171 qemu_free(s->l1_backup_table);
172 qemu_free(s->l1_table);
173 qemu_free(s->l2_cache);
178 static uint64_t get_cluster_offset(BlockDriverState *bs,
179 uint64_t offset, int allocate)
181 BDRVVmdkState *s = bs->opaque;
182 unsigned int l1_index, l2_offset, l2_index;
184 uint32_t min_count, *l2_table, tmp;
185 uint64_t cluster_offset;
187 l1_index = (offset >> 9) / s->l1_entry_sectors;
188 if (l1_index >= s->l1_size)
190 l2_offset = s->l1_table[l1_index];
193 for(i = 0; i < L2_CACHE_SIZE; i++) {
194 if (l2_offset == s->l2_cache_offsets[i]) {
195 /* increment the hit count */
196 if (++s->l2_cache_counts[i] == 0xffffffff) {
197 for(j = 0; j < L2_CACHE_SIZE; j++) {
198 s->l2_cache_counts[j] >>= 1;
201 l2_table = s->l2_cache + (i * s->l2_size);
205 /* not found: load a new entry in the least used one */
207 min_count = 0xffffffff;
208 for(i = 0; i < L2_CACHE_SIZE; i++) {
209 if (s->l2_cache_counts[i] < min_count) {
210 min_count = s->l2_cache_counts[i];
214 l2_table = s->l2_cache + (min_index * s->l2_size);
215 lseek(s->fd, (int64_t)l2_offset * 512, SEEK_SET);
216 if (read(s->fd, l2_table, s->l2_size * sizeof(uint32_t)) !=
217 s->l2_size * sizeof(uint32_t))
219 s->l2_cache_offsets[min_index] = l2_offset;
220 s->l2_cache_counts[min_index] = 1;
222 l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
223 cluster_offset = le32_to_cpu(l2_table[l2_index]);
224 if (!cluster_offset) {
227 cluster_offset = lseek(s->fd, 0, SEEK_END);
228 ftruncate(s->fd, cluster_offset + (s->cluster_sectors << 9));
229 cluster_offset >>= 9;
230 /* update L2 table */
231 tmp = cpu_to_le32(cluster_offset);
232 l2_table[l2_index] = tmp;
233 lseek(s->fd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), SEEK_SET);
234 if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp))
236 /* update backup L2 table */
237 if (s->l1_backup_table_offset != 0) {
238 l2_offset = s->l1_backup_table[l1_index];
239 lseek(s->fd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), SEEK_SET);
240 if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp))
244 cluster_offset <<= 9;
245 return cluster_offset;
248 static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
249 int nb_sectors, int *pnum)
251 BDRVVmdkState *s = bs->opaque;
252 int index_in_cluster, n;
253 uint64_t cluster_offset;
255 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0);
256 index_in_cluster = sector_num % s->cluster_sectors;
257 n = s->cluster_sectors - index_in_cluster;
261 return (cluster_offset != 0);
264 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
265 uint8_t *buf, int nb_sectors)
267 BDRVVmdkState *s = bs->opaque;
268 int ret, index_in_cluster, n;
269 uint64_t cluster_offset;
271 while (nb_sectors > 0) {
272 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0);
273 index_in_cluster = sector_num % s->cluster_sectors;
274 n = s->cluster_sectors - index_in_cluster;
277 if (!cluster_offset) {
278 memset(buf, 0, 512 * n);
280 lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
281 ret = read(s->fd, buf, n * 512);
292 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
293 const uint8_t *buf, int nb_sectors)
295 BDRVVmdkState *s = bs->opaque;
296 int ret, index_in_cluster, n;
297 uint64_t cluster_offset;
299 while (nb_sectors > 0) {
300 index_in_cluster = sector_num & (s->cluster_sectors - 1);
301 n = s->cluster_sectors - index_in_cluster;
304 cluster_offset = get_cluster_offset(bs, sector_num << 9, 1);
307 lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
308 ret = write(s->fd, buf, n * 512);
318 static void vmdk_close(BlockDriverState *bs)
320 BDRVVmdkState *s = bs->opaque;
321 qemu_free(s->l1_table);
322 qemu_free(s->l2_cache);
326 BlockDriver bdrv_vmdk = {
328 sizeof(BDRVVmdkState),
334 NULL, /* no create yet */