]>
Commit | Line | Data |
---|---|---|
ea2384d3 FB |
1 | /* |
2 | * Block driver for the VMDK format | |
3 | * | |
4 | * Copyright (c) 2004 Fabrice Bellard | |
ff1afc72 | 5 | * Copyright (c) 2005 Filip Navara |
ea2384d3 FB |
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 | #include "vl.h" | |
26 | #include "block_int.h" | |
27 | ||
ea2384d3 FB |
28 | #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D') |
29 | #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V') | |
30 | ||
31 | typedef struct { | |
32 | uint32_t version; | |
33 | uint32_t flags; | |
34 | uint32_t disk_sectors; | |
35 | uint32_t granularity; | |
36 | uint32_t l1dir_offset; | |
37 | uint32_t l1dir_size; | |
38 | uint32_t file_sectors; | |
39 | uint32_t cylinders; | |
40 | uint32_t heads; | |
41 | uint32_t sectors_per_track; | |
42 | } VMDK3Header; | |
43 | ||
44 | typedef struct { | |
45 | uint32_t version; | |
46 | uint32_t flags; | |
47 | int64_t capacity; | |
48 | int64_t granularity; | |
49 | int64_t desc_offset; | |
50 | int64_t desc_size; | |
51 | int32_t num_gtes_per_gte; | |
52 | int64_t rgd_offset; | |
53 | int64_t gd_offset; | |
54 | int64_t grain_offset; | |
55 | char filler[1]; | |
56 | char check_bytes[4]; | |
ff1afc72 | 57 | } __attribute__((packed)) VMDK4Header; |
ea2384d3 FB |
58 | |
59 | #define L2_CACHE_SIZE 16 | |
60 | ||
61 | typedef struct BDRVVmdkState { | |
62 | int fd; | |
63 | int64_t l1_table_offset; | |
ff1afc72 | 64 | int64_t l1_backup_table_offset; |
ea2384d3 | 65 | uint32_t *l1_table; |
ff1afc72 | 66 | uint32_t *l1_backup_table; |
ea2384d3 FB |
67 | unsigned int l1_size; |
68 | uint32_t l1_entry_sectors; | |
69 | ||
70 | unsigned int l2_size; | |
71 | uint32_t *l2_cache; | |
72 | uint32_t l2_cache_offsets[L2_CACHE_SIZE]; | |
73 | uint32_t l2_cache_counts[L2_CACHE_SIZE]; | |
74 | ||
75 | unsigned int cluster_sectors; | |
76 | } BDRVVmdkState; | |
77 | ||
78 | static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) | |
79 | { | |
80 | uint32_t magic; | |
81 | ||
82 | if (buf_size < 4) | |
83 | return 0; | |
84 | magic = be32_to_cpu(*(uint32_t *)buf); | |
85 | if (magic == VMDK3_MAGIC || | |
86 | magic == VMDK4_MAGIC) | |
87 | return 100; | |
88 | else | |
89 | return 0; | |
90 | } | |
91 | ||
92 | static int vmdk_open(BlockDriverState *bs, const char *filename) | |
93 | { | |
94 | BDRVVmdkState *s = bs->opaque; | |
95 | int fd, i; | |
96 | uint32_t magic; | |
97 | int l1_size; | |
98 | ||
ff1afc72 FB |
99 | fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
100 | if (fd < 0) { | |
101 | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); | |
102 | if (fd < 0) | |
103 | return -1; | |
104 | bs->read_only = 1; | |
105 | } | |
ea2384d3 FB |
106 | if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) |
107 | goto fail; | |
7143c62c | 108 | magic = be32_to_cpu(magic); |
ea2384d3 FB |
109 | if (magic == VMDK3_MAGIC) { |
110 | VMDK3Header header; | |
111 | if (read(fd, &header, sizeof(header)) != | |
112 | sizeof(header)) | |
113 | goto fail; | |
114 | s->cluster_sectors = le32_to_cpu(header.granularity); | |
115 | s->l2_size = 1 << 9; | |
116 | s->l1_size = 1 << 6; | |
117 | bs->total_sectors = le32_to_cpu(header.disk_sectors); | |
ff1afc72 FB |
118 | s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9; |
119 | s->l1_backup_table_offset = 0; | |
ea2384d3 FB |
120 | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
121 | } else if (magic == VMDK4_MAGIC) { | |
122 | VMDK4Header header; | |
123 | ||
124 | if (read(fd, &header, sizeof(header)) != sizeof(header)) | |
125 | goto fail; | |
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) | |
131 | goto fail; | |
132 | s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1) | |
133 | / s->l1_entry_sectors; | |
ff1afc72 FB |
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; | |
ea2384d3 FB |
136 | } else { |
137 | goto fail; | |
138 | } | |
139 | /* read the L1 table */ | |
140 | l1_size = s->l1_size * sizeof(uint32_t); | |
141 | s->l1_table = qemu_malloc(l1_size); | |
142 | if (!s->l1_table) | |
143 | goto fail; | |
7143c62c FB |
144 | if (lseek(fd, s->l1_table_offset, SEEK_SET) == -1) |
145 | goto fail; | |
146 | if (read(fd, s->l1_table, l1_size) != l1_size) | |
ea2384d3 FB |
147 | goto fail; |
148 | for(i = 0; i < s->l1_size; i++) { | |
149 | le32_to_cpus(&s->l1_table[i]); | |
150 | } | |
151 | ||
ff1afc72 FB |
152 | if (s->l1_backup_table_offset) { |
153 | s->l1_backup_table = qemu_malloc(l1_size); | |
154 | if (!s->l1_backup_table) | |
155 | goto fail; | |
156 | if (lseek(fd, s->l1_backup_table_offset, SEEK_SET) == -1) | |
157 | goto fail; | |
158 | if (read(fd, s->l1_backup_table, l1_size) != l1_size) | |
159 | goto fail; | |
160 | for(i = 0; i < s->l1_size; i++) { | |
161 | le32_to_cpus(&s->l1_backup_table[i]); | |
162 | } | |
163 | } | |
164 | ||
ea2384d3 FB |
165 | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t)); |
166 | if (!s->l2_cache) | |
167 | goto fail; | |
168 | s->fd = fd; | |
ea2384d3 FB |
169 | return 0; |
170 | fail: | |
ff1afc72 | 171 | qemu_free(s->l1_backup_table); |
ea2384d3 FB |
172 | qemu_free(s->l1_table); |
173 | qemu_free(s->l2_cache); | |
174 | close(fd); | |
175 | return -1; | |
176 | } | |
177 | ||
178 | static uint64_t get_cluster_offset(BlockDriverState *bs, | |
ff1afc72 | 179 | uint64_t offset, int allocate) |
ea2384d3 FB |
180 | { |
181 | BDRVVmdkState *s = bs->opaque; | |
182 | unsigned int l1_index, l2_offset, l2_index; | |
183 | int min_index, i, j; | |
ff1afc72 | 184 | uint32_t min_count, *l2_table, tmp; |
ea2384d3 FB |
185 | uint64_t cluster_offset; |
186 | ||
187 | l1_index = (offset >> 9) / s->l1_entry_sectors; | |
188 | if (l1_index >= s->l1_size) | |
189 | return 0; | |
190 | l2_offset = s->l1_table[l1_index]; | |
191 | if (!l2_offset) | |
192 | return 0; | |
ea2384d3 FB |
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; | |
199 | } | |
200 | } | |
201 | l2_table = s->l2_cache + (i * s->l2_size); | |
202 | goto found; | |
203 | } | |
204 | } | |
205 | /* not found: load a new entry in the least used one */ | |
206 | min_index = 0; | |
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]; | |
211 | min_index = i; | |
212 | } | |
213 | } | |
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)) | |
218 | return 0; | |
219 | s->l2_cache_offsets[min_index] = l2_offset; | |
220 | s->l2_cache_counts[min_index] = 1; | |
221 | found: | |
222 | l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size; | |
223 | cluster_offset = le32_to_cpu(l2_table[l2_index]); | |
ff1afc72 FB |
224 | if (!cluster_offset) { |
225 | if (!allocate) | |
226 | return 0; | |
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)) | |
235 | return 0; | |
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)) | |
241 | return 0; | |
242 | } | |
243 | } | |
ea2384d3 FB |
244 | cluster_offset <<= 9; |
245 | return cluster_offset; | |
246 | } | |
247 | ||
248 | static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num, | |
249 | int nb_sectors, int *pnum) | |
250 | { | |
251 | BDRVVmdkState *s = bs->opaque; | |
252 | int index_in_cluster, n; | |
253 | uint64_t cluster_offset; | |
254 | ||
ff1afc72 | 255 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0); |
ea2384d3 FB |
256 | index_in_cluster = sector_num % s->cluster_sectors; |
257 | n = s->cluster_sectors - index_in_cluster; | |
258 | if (n > nb_sectors) | |
259 | n = nb_sectors; | |
260 | *pnum = n; | |
261 | return (cluster_offset != 0); | |
262 | } | |
263 | ||
264 | static int vmdk_read(BlockDriverState *bs, int64_t sector_num, | |
265 | uint8_t *buf, int nb_sectors) | |
266 | { | |
267 | BDRVVmdkState *s = bs->opaque; | |
268 | int ret, index_in_cluster, n; | |
269 | uint64_t cluster_offset; | |
270 | ||
271 | while (nb_sectors > 0) { | |
ff1afc72 | 272 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0); |
ea2384d3 FB |
273 | index_in_cluster = sector_num % s->cluster_sectors; |
274 | n = s->cluster_sectors - index_in_cluster; | |
275 | if (n > nb_sectors) | |
276 | n = nb_sectors; | |
277 | if (!cluster_offset) { | |
278 | memset(buf, 0, 512 * n); | |
279 | } else { | |
d5249393 | 280 | lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET); |
ea2384d3 FB |
281 | ret = read(s->fd, buf, n * 512); |
282 | if (ret != n * 512) | |
283 | return -1; | |
284 | } | |
285 | nb_sectors -= n; | |
286 | sector_num += n; | |
287 | buf += n * 512; | |
288 | } | |
289 | return 0; | |
290 | } | |
291 | ||
292 | static int vmdk_write(BlockDriverState *bs, int64_t sector_num, | |
293 | const uint8_t *buf, int nb_sectors) | |
294 | { | |
ff1afc72 FB |
295 | BDRVVmdkState *s = bs->opaque; |
296 | int ret, index_in_cluster, n; | |
297 | uint64_t cluster_offset; | |
298 | ||
299 | while (nb_sectors > 0) { | |
300 | index_in_cluster = sector_num & (s->cluster_sectors - 1); | |
301 | n = s->cluster_sectors - index_in_cluster; | |
302 | if (n > nb_sectors) | |
303 | n = nb_sectors; | |
304 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 1); | |
305 | if (!cluster_offset) | |
306 | return -1; | |
307 | lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET); | |
308 | ret = write(s->fd, buf, n * 512); | |
309 | if (ret != n * 512) | |
310 | return -1; | |
311 | nb_sectors -= n; | |
312 | sector_num += n; | |
313 | buf += n * 512; | |
314 | } | |
315 | return 0; | |
ea2384d3 FB |
316 | } |
317 | ||
e2731add | 318 | static void vmdk_close(BlockDriverState *bs) |
ea2384d3 FB |
319 | { |
320 | BDRVVmdkState *s = bs->opaque; | |
321 | qemu_free(s->l1_table); | |
322 | qemu_free(s->l2_cache); | |
323 | close(s->fd); | |
324 | } | |
325 | ||
326 | BlockDriver bdrv_vmdk = { | |
327 | "vmdk", | |
328 | sizeof(BDRVVmdkState), | |
329 | vmdk_probe, | |
330 | vmdk_open, | |
331 | vmdk_read, | |
332 | vmdk_write, | |
333 | vmdk_close, | |
334 | NULL, /* no create yet */ | |
335 | vmdk_is_allocated, | |
336 | }; |