vmdk 3 fixes
[qemu] / block-vmdk.c
1 /*
2  * Block driver for the VMDK format
3  * 
4  * Copyright (c) 2004 Fabrice Bellard
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "vl.h"
25 #include "block_int.h"
26
27 /* XXX: this code is untested */
28 /* XXX: add write support */
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 } VMDK4Header;
60
61 #define L2_CACHE_SIZE 16
62
63 typedef struct BDRVVmdkState {
64     int fd;
65     int64_t l1_table_offset;
66     uint32_t *l1_table;
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
99     fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
100     if (fd < 0)
101         return -1;
102     if (read(fd, &magic, sizeof(magic)) != sizeof(magic))
103         goto fail;
104     magic = be32_to_cpu(magic);
105     if (magic == VMDK3_MAGIC) {
106         VMDK3Header header;
107         if (read(fd, &header, sizeof(header)) != 
108             sizeof(header))
109             goto fail;
110         s->cluster_sectors = le32_to_cpu(header.granularity);
111         s->l2_size = 1 << 9;
112         s->l1_size = 1 << 6;
113         bs->total_sectors = le32_to_cpu(header.disk_sectors);
114         s->l1_table_offset = le32_to_cpu(header.l1dir_offset) * 512;
115         s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
116     } else if (magic == VMDK4_MAGIC) {
117         VMDK4Header header;
118         
119         if (read(fd, &header, sizeof(header)) != sizeof(header))
120             goto fail;
121         bs->total_sectors = le32_to_cpu(header.capacity);
122         s->cluster_sectors = le32_to_cpu(header.granularity);
123         s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
124         s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
125         if (s->l1_entry_sectors <= 0)
126             goto fail;
127         s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1) 
128             / s->l1_entry_sectors;
129         s->l1_table_offset = le64_to_cpu(header.rgd_offset) * 512;
130     } else {
131         goto fail;
132     }
133     /* read the L1 table */
134     l1_size = s->l1_size * sizeof(uint32_t);
135     s->l1_table = qemu_malloc(l1_size);
136     if (!s->l1_table)
137         goto fail;
138     if (lseek(fd, s->l1_table_offset, SEEK_SET) == -1)
139         goto fail;
140     if (read(fd, s->l1_table, l1_size) != l1_size)
141         goto fail;
142     for(i = 0; i < s->l1_size; i++) {
143         le32_to_cpus(&s->l1_table[i]);
144     }
145
146     s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
147     if (!s->l2_cache)
148         goto fail;
149     s->fd = fd;
150     /* XXX: currently only read only */
151     bs->read_only = 1;
152     return 0;
153  fail:
154     qemu_free(s->l1_table);
155     qemu_free(s->l2_cache);
156     close(fd);
157     return -1;
158 }
159
160 static uint64_t get_cluster_offset(BlockDriverState *bs,
161                                    uint64_t offset)
162 {
163     BDRVVmdkState *s = bs->opaque;
164     unsigned int l1_index, l2_offset, l2_index;
165     int min_index, i, j;
166     uint32_t min_count, *l2_table;
167     uint64_t cluster_offset;
168     
169     l1_index = (offset >> 9) / s->l1_entry_sectors;
170     if (l1_index >= s->l1_size)
171         return 0;
172     l2_offset = s->l1_table[l1_index];
173     if (!l2_offset)
174         return 0;
175     
176     for(i = 0; i < L2_CACHE_SIZE; i++) {
177         if (l2_offset == s->l2_cache_offsets[i]) {
178             /* increment the hit count */
179             if (++s->l2_cache_counts[i] == 0xffffffff) {
180                 for(j = 0; j < L2_CACHE_SIZE; j++) {
181                     s->l2_cache_counts[j] >>= 1;
182                 }
183             }
184             l2_table = s->l2_cache + (i * s->l2_size);
185             goto found;
186         }
187     }
188     /* not found: load a new entry in the least used one */
189     min_index = 0;
190     min_count = 0xffffffff;
191     for(i = 0; i < L2_CACHE_SIZE; i++) {
192         if (s->l2_cache_counts[i] < min_count) {
193             min_count = s->l2_cache_counts[i];
194             min_index = i;
195         }
196     }
197     l2_table = s->l2_cache + (min_index * s->l2_size);
198     lseek(s->fd, (int64_t)l2_offset * 512, SEEK_SET);
199     if (read(s->fd, l2_table, s->l2_size * sizeof(uint32_t)) != 
200         s->l2_size * sizeof(uint32_t))
201         return 0;
202     s->l2_cache_offsets[min_index] = l2_offset;
203     s->l2_cache_counts[min_index] = 1;
204  found:
205     l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
206     cluster_offset = le32_to_cpu(l2_table[l2_index]);
207     cluster_offset <<= 9;
208     return cluster_offset;
209 }
210
211 static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num, 
212                              int nb_sectors, int *pnum)
213 {
214     BDRVVmdkState *s = bs->opaque;
215     int index_in_cluster, n;
216     uint64_t cluster_offset;
217
218     cluster_offset = get_cluster_offset(bs, sector_num << 9);
219     index_in_cluster = sector_num % s->cluster_sectors;
220     n = s->cluster_sectors - index_in_cluster;
221     if (n > nb_sectors)
222         n = nb_sectors;
223     *pnum = n;
224     return (cluster_offset != 0);
225 }
226
227 static int vmdk_read(BlockDriverState *bs, int64_t sector_num, 
228                     uint8_t *buf, int nb_sectors)
229 {
230     BDRVVmdkState *s = bs->opaque;
231     int ret, index_in_cluster, n;
232     uint64_t cluster_offset;
233     
234     while (nb_sectors > 0) {
235         cluster_offset = get_cluster_offset(bs, sector_num << 9);
236         index_in_cluster = sector_num % s->cluster_sectors;
237         n = s->cluster_sectors - index_in_cluster;
238         if (n > nb_sectors)
239             n = nb_sectors;
240         if (!cluster_offset) {
241             memset(buf, 0, 512 * n);
242         } else {
243             lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
244             ret = read(s->fd, buf, n * 512);
245             if (ret != n * 512) 
246                 return -1;
247         }
248         nb_sectors -= n;
249         sector_num += n;
250         buf += n * 512;
251     }
252     return 0;
253 }
254
255 static int vmdk_write(BlockDriverState *bs, int64_t sector_num, 
256                      const uint8_t *buf, int nb_sectors)
257 {
258     return -1;
259 }
260
261 static int vmdk_close(BlockDriverState *bs)
262 {
263     BDRVVmdkState *s = bs->opaque;
264     qemu_free(s->l1_table);
265     qemu_free(s->l2_cache);
266     close(s->fd);
267 }
268
269 BlockDriver bdrv_vmdk = {
270     "vmdk",
271     sizeof(BDRVVmdkState),
272     vmdk_probe,
273     vmdk_open,
274     vmdk_read,
275     vmdk_write,
276     vmdk_close,
277     NULL, /* no create yet */
278     vmdk_is_allocated,
279 };