Statistics
| Branch: | Revision:

root / block-vmdk.c @ ea2384d3

History | View | Annotate | Download (8.1 kB)

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 = le32_to_cpu(magic);
105
    
106
    if (magic == VMDK3_MAGIC) {
107
        VMDK3Header header;
108
        if (read(fd, &header, sizeof(header)) != 
109
            sizeof(header))
110
            goto fail;
111
        s->cluster_sectors = le32_to_cpu(header.granularity);
112
        s->l2_size = 1 << 9;
113
        s->l1_size = 1 << 6;
114
        bs->total_sectors = le32_to_cpu(header.disk_sectors);
115
        s->l1_table_offset = le32_to_cpu(header.l1dir_offset) * 512;
116
        s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
117
    } else if (magic == VMDK4_MAGIC) {
118
        VMDK4Header header;
119
        
120
        if (read(fd, &header, sizeof(header)) != sizeof(header))
121
            goto fail;
122
        bs->total_sectors = le32_to_cpu(header.capacity);
123
        s->cluster_sectors = le32_to_cpu(header.granularity);
124
        s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
125
        s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
126
        if (s->l1_entry_sectors <= 0)
127
            goto fail;
128
        s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1) 
129
            / s->l1_entry_sectors;
130
        s->l1_table_offset = le64_to_cpu(header.rgd_offset) * 512;
131
    } else {
132
        goto fail;
133
    }
134
    /* read the L1 table */
135
    l1_size = s->l1_size * sizeof(uint32_t);
136
    s->l1_table = qemu_malloc(l1_size);
137
    if (!s->l1_table)
138
        goto fail;
139
    if (read(s->fd, s->l1_table, l1_size) != l1_size)
140
        goto fail;
141
    for(i = 0; i < s->l1_size; i++) {
142
        le32_to_cpus(&s->l1_table[i]);
143
    }
144

    
145
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
146
    if (!s->l2_cache)
147
        goto fail;
148
    s->fd = fd;
149
    /* XXX: currently only read only */
150
    bs->read_only = 1;
151
    return 0;
152
 fail:
153
    qemu_free(s->l1_table);
154
    qemu_free(s->l2_cache);
155
    close(fd);
156
    return -1;
157
}
158

    
159
static uint64_t get_cluster_offset(BlockDriverState *bs,
160
                                   uint64_t offset)
161
{
162
    BDRVVmdkState *s = bs->opaque;
163
    unsigned int l1_index, l2_offset, l2_index;
164
    int min_index, i, j;
165
    uint32_t min_count, *l2_table;
166
    uint64_t cluster_offset;
167
    
168
    l1_index = (offset >> 9) / s->l1_entry_sectors;
169
    if (l1_index >= s->l1_size)
170
        return 0;
171
    l2_offset = s->l1_table[l1_index];
172
    if (!l2_offset)
173
        return 0;
174
    
175
    for(i = 0; i < L2_CACHE_SIZE; i++) {
176
        if (l2_offset == s->l2_cache_offsets[i]) {
177
            /* increment the hit count */
178
            if (++s->l2_cache_counts[i] == 0xffffffff) {
179
                for(j = 0; j < L2_CACHE_SIZE; j++) {
180
                    s->l2_cache_counts[j] >>= 1;
181
                }
182
            }
183
            l2_table = s->l2_cache + (i * s->l2_size);
184
            goto found;
185
        }
186
    }
187
    /* not found: load a new entry in the least used one */
188
    min_index = 0;
189
    min_count = 0xffffffff;
190
    for(i = 0; i < L2_CACHE_SIZE; i++) {
191
        if (s->l2_cache_counts[i] < min_count) {
192
            min_count = s->l2_cache_counts[i];
193
            min_index = i;
194
        }
195
    }
196
    l2_table = s->l2_cache + (min_index * s->l2_size);
197
    lseek(s->fd, (int64_t)l2_offset * 512, SEEK_SET);
198
    if (read(s->fd, l2_table, s->l2_size * sizeof(uint32_t)) != 
199
        s->l2_size * sizeof(uint32_t))
200
        return 0;
201
    s->l2_cache_offsets[min_index] = l2_offset;
202
    s->l2_cache_counts[min_index] = 1;
203
 found:
204
    l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
205
    cluster_offset = le32_to_cpu(l2_table[l2_index]);
206
    cluster_offset <<= 9;
207
    return cluster_offset;
208
}
209

    
210
static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num, 
211
                             int nb_sectors, int *pnum)
212
{
213
    BDRVVmdkState *s = bs->opaque;
214
    int index_in_cluster, n;
215
    uint64_t cluster_offset;
216

    
217
    cluster_offset = get_cluster_offset(bs, sector_num << 9);
218
    index_in_cluster = sector_num % s->cluster_sectors;
219
    n = s->cluster_sectors - index_in_cluster;
220
    if (n > nb_sectors)
221
        n = nb_sectors;
222
    *pnum = n;
223
    return (cluster_offset != 0);
224
}
225

    
226
static int vmdk_read(BlockDriverState *bs, int64_t sector_num, 
227
                    uint8_t *buf, int nb_sectors)
228
{
229
    BDRVVmdkState *s = bs->opaque;
230
    int ret, index_in_cluster, n;
231
    uint64_t cluster_offset;
232
    
233
    while (nb_sectors > 0) {
234
        cluster_offset = get_cluster_offset(bs, sector_num << 9);
235
        index_in_cluster = sector_num % s->cluster_sectors;
236
        n = s->cluster_sectors - index_in_cluster;
237
        if (n > nb_sectors)
238
            n = nb_sectors;
239
        if (!cluster_offset) {
240
            memset(buf, 0, 512 * n);
241
        } else {
242
            lseek64(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
243
            ret = read(s->fd, buf, n * 512);
244
            if (ret != n * 512) 
245
                return -1;
246
        }
247
        nb_sectors -= n;
248
        sector_num += n;
249
        buf += n * 512;
250
    }
251
    return 0;
252
}
253

    
254
static int vmdk_write(BlockDriverState *bs, int64_t sector_num, 
255
                     const uint8_t *buf, int nb_sectors)
256
{
257
    return -1;
258
}
259

    
260
static int vmdk_close(BlockDriverState *bs)
261
{
262
    BDRVVmdkState *s = bs->opaque;
263
    qemu_free(s->l1_table);
264
    qemu_free(s->l2_cache);
265
    close(s->fd);
266
}
267

    
268
BlockDriver bdrv_vmdk = {
269
    "vmdk",
270
    sizeof(BDRVVmdkState),
271
    vmdk_probe,
272
    vmdk_open,
273
    vmdk_read,
274
    vmdk_write,
275
    vmdk_close,
276
    NULL, /* no create yet */
277
    vmdk_is_allocated,
278
};