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