root / block-vmdk.c @ e9df014c
History | View | Annotate | Download (22.9 kB)
1 | ea2384d3 | bellard | /*
|
---|---|---|---|
2 | ea2384d3 | bellard | * Block driver for the VMDK format
|
3 | ea2384d3 | bellard | *
|
4 | ea2384d3 | bellard | * Copyright (c) 2004 Fabrice Bellard
|
5 | ff1afc72 | bellard | * Copyright (c) 2005 Filip Navara
|
6 | ea2384d3 | bellard | *
|
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 | ea2384d3 | bellard | #include "vl.h" |
27 | ea2384d3 | bellard | #include "block_int.h" |
28 | ea2384d3 | bellard | |
29 | ea2384d3 | bellard | #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D') |
30 | ea2384d3 | bellard | #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V') |
31 | ea2384d3 | bellard | |
32 | ea2384d3 | bellard | typedef struct { |
33 | ea2384d3 | bellard | uint32_t version; |
34 | ea2384d3 | bellard | uint32_t flags; |
35 | ea2384d3 | bellard | uint32_t disk_sectors; |
36 | ea2384d3 | bellard | uint32_t granularity; |
37 | ea2384d3 | bellard | uint32_t l1dir_offset; |
38 | ea2384d3 | bellard | uint32_t l1dir_size; |
39 | ea2384d3 | bellard | uint32_t file_sectors; |
40 | ea2384d3 | bellard | uint32_t cylinders; |
41 | ea2384d3 | bellard | uint32_t heads; |
42 | ea2384d3 | bellard | uint32_t sectors_per_track; |
43 | ea2384d3 | bellard | } VMDK3Header; |
44 | ea2384d3 | bellard | |
45 | ea2384d3 | bellard | typedef struct { |
46 | ea2384d3 | bellard | uint32_t version; |
47 | ea2384d3 | bellard | uint32_t flags; |
48 | ea2384d3 | bellard | int64_t capacity; |
49 | ea2384d3 | bellard | int64_t granularity; |
50 | ea2384d3 | bellard | int64_t desc_offset; |
51 | ea2384d3 | bellard | int64_t desc_size; |
52 | ea2384d3 | bellard | int32_t num_gtes_per_gte; |
53 | ea2384d3 | bellard | int64_t rgd_offset; |
54 | ea2384d3 | bellard | int64_t gd_offset; |
55 | ea2384d3 | bellard | int64_t grain_offset; |
56 | ea2384d3 | bellard | char filler[1]; |
57 | ea2384d3 | bellard | char check_bytes[4]; |
58 | ff1afc72 | bellard | } __attribute__((packed)) VMDK4Header; |
59 | ea2384d3 | bellard | |
60 | ea2384d3 | bellard | #define L2_CACHE_SIZE 16 |
61 | ea2384d3 | bellard | |
62 | ea2384d3 | bellard | typedef struct BDRVVmdkState { |
63 | 5f4da8c0 | ths | BlockDriverState *hd; |
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 | ea2384d3 | bellard | static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) |
81 | ea2384d3 | bellard | { |
82 | ea2384d3 | bellard | uint32_t magic; |
83 | ea2384d3 | bellard | |
84 | ea2384d3 | bellard | if (buf_size < 4) |
85 | ea2384d3 | bellard | return 0; |
86 | ea2384d3 | bellard | magic = be32_to_cpu(*(uint32_t *)buf); |
87 | ea2384d3 | bellard | if (magic == VMDK3_MAGIC ||
|
88 | ea2384d3 | bellard | magic == VMDK4_MAGIC) |
89 | ea2384d3 | bellard | return 100; |
90 | ea2384d3 | bellard | else
|
91 | ea2384d3 | bellard | return 0; |
92 | ea2384d3 | bellard | } |
93 | ea2384d3 | bellard | |
94 | 5f4da8c0 | ths | #define CHECK_CID 1 |
95 | 5f4da8c0 | ths | |
96 | 5f4da8c0 | ths | #define SECTOR_SIZE 512 |
97 | 5f4da8c0 | ths | #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each |
98 | 5f4da8c0 | ths | #define HEADER_SIZE 512 // first sector of 512 bytes |
99 | 5f4da8c0 | ths | |
100 | 5f4da8c0 | ths | static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) |
101 | ea2384d3 | bellard | { |
102 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
103 | 5f4da8c0 | ths | char desc[DESC_SIZE];
|
104 | 5f4da8c0 | ths | uint32_t cid; |
105 | 5f4da8c0 | ths | char *p_name, *cid_str;
|
106 | 5f4da8c0 | ths | size_t cid_str_size; |
107 | 5f4da8c0 | ths | |
108 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
109 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) |
110 | 5f4da8c0 | ths | return 0; |
111 | 5f4da8c0 | ths | |
112 | 5f4da8c0 | ths | if (parent) {
|
113 | 5f4da8c0 | ths | cid_str = "parentCID";
|
114 | 5f4da8c0 | ths | cid_str_size = sizeof("parentCID"); |
115 | 5f4da8c0 | ths | } else {
|
116 | 5f4da8c0 | ths | cid_str = "CID";
|
117 | 5f4da8c0 | ths | cid_str_size = sizeof("CID"); |
118 | 5f4da8c0 | ths | } |
119 | 5f4da8c0 | ths | |
120 | 5f4da8c0 | ths | if ((p_name = strstr(desc,cid_str)) != 0) { |
121 | 5f4da8c0 | ths | p_name += cid_str_size; |
122 | 5f4da8c0 | ths | sscanf(p_name,"%x",&cid);
|
123 | 5f4da8c0 | ths | } |
124 | 5f4da8c0 | ths | |
125 | 5f4da8c0 | ths | return cid;
|
126 | 5f4da8c0 | ths | } |
127 | 5f4da8c0 | ths | |
128 | 5f4da8c0 | ths | static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid) |
129 | 5f4da8c0 | ths | { |
130 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
131 | 5f4da8c0 | ths | char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
|
132 | 5f4da8c0 | ths | char *p_name, *tmp_str;
|
133 | 5f4da8c0 | ths | |
134 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
135 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) |
136 | 5f4da8c0 | ths | return -1; |
137 | 5f4da8c0 | ths | |
138 | 5f4da8c0 | ths | tmp_str = strstr(desc,"parentCID");
|
139 | 5f4da8c0 | ths | strcpy(tmp_desc, tmp_str); |
140 | 5f4da8c0 | ths | if ((p_name = strstr(desc,"CID")) != 0) { |
141 | 5f4da8c0 | ths | p_name += sizeof("CID"); |
142 | 5f4da8c0 | ths | sprintf(p_name,"%x\n",cid);
|
143 | 5f4da8c0 | ths | strcat(desc,tmp_desc); |
144 | 5f4da8c0 | ths | } |
145 | 5f4da8c0 | ths | |
146 | 5f4da8c0 | ths | if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) |
147 | 5f4da8c0 | ths | return -1; |
148 | 5f4da8c0 | ths | return 0; |
149 | 5f4da8c0 | ths | } |
150 | 5f4da8c0 | ths | |
151 | 5f4da8c0 | ths | static int vmdk_is_cid_valid(BlockDriverState *bs) |
152 | 5f4da8c0 | ths | { |
153 | 5f4da8c0 | ths | #ifdef CHECK_CID
|
154 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
155 | 5f4da8c0 | ths | BlockDriverState *p_bs = s->hd->backing_hd; |
156 | 5f4da8c0 | ths | uint32_t cur_pcid; |
157 | 5f4da8c0 | ths | |
158 | 5f4da8c0 | ths | if (p_bs) {
|
159 | 5f4da8c0 | ths | cur_pcid = vmdk_read_cid(p_bs,0);
|
160 | 5f4da8c0 | ths | if (s->parent_cid != cur_pcid)
|
161 | 5f4da8c0 | ths | // CID not valid
|
162 | 5f4da8c0 | ths | return 0; |
163 | 5f4da8c0 | ths | } |
164 | 5f4da8c0 | ths | #endif
|
165 | 5f4da8c0 | ths | // CID valid
|
166 | 5f4da8c0 | ths | return 1; |
167 | 5f4da8c0 | ths | } |
168 | 5f4da8c0 | ths | |
169 | 5f4da8c0 | ths | static int vmdk_snapshot_create(const char *filename, const char *backing_file) |
170 | 5f4da8c0 | ths | { |
171 | 5f4da8c0 | ths | int snp_fd, p_fd;
|
172 | 5f4da8c0 | ths | uint32_t p_cid; |
173 | 5f4da8c0 | ths | char *p_name, *gd_buf, *rgd_buf;
|
174 | 5f4da8c0 | ths | const char *real_filename, *temp_str; |
175 | 5f4da8c0 | ths | VMDK4Header header; |
176 | 5f4da8c0 | ths | uint32_t gde_entries, gd_size; |
177 | 5f4da8c0 | ths | int64_t gd_offset, rgd_offset, capacity, gt_size; |
178 | 5f4da8c0 | ths | char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
|
179 | 5f4da8c0 | ths | char *desc_template =
|
180 | 5f4da8c0 | ths | "# Disk DescriptorFile\n"
|
181 | 5f4da8c0 | ths | "version=1\n"
|
182 | 5f4da8c0 | ths | "CID=%x\n"
|
183 | 5f4da8c0 | ths | "parentCID=%x\n"
|
184 | 5f4da8c0 | ths | "createType=\"monolithicSparse\"\n"
|
185 | 5f4da8c0 | ths | "parentFileNameHint=\"%s\"\n"
|
186 | 5f4da8c0 | ths | "\n"
|
187 | 5f4da8c0 | ths | "# Extent description\n"
|
188 | 5f4da8c0 | ths | "RW %lu SPARSE \"%s\"\n"
|
189 | 5f4da8c0 | ths | "\n"
|
190 | 5f4da8c0 | ths | "# The Disk Data Base \n"
|
191 | 5f4da8c0 | ths | "#DDB\n"
|
192 | 5f4da8c0 | ths | "\n";
|
193 | 5f4da8c0 | ths | |
194 | 5f4da8c0 | ths | snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
|
195 | 5f4da8c0 | ths | if (snp_fd < 0) |
196 | 5f4da8c0 | ths | return -1; |
197 | 5f4da8c0 | ths | p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE); |
198 | 5f4da8c0 | ths | if (p_fd < 0) { |
199 | 5f4da8c0 | ths | close(snp_fd); |
200 | 5f4da8c0 | ths | return -1; |
201 | 5f4da8c0 | ths | } |
202 | 5f4da8c0 | ths | |
203 | 5f4da8c0 | ths | /* read the header */
|
204 | 5f4da8c0 | ths | if (lseek(p_fd, 0x0, SEEK_SET) == -1) |
205 | 5f4da8c0 | ths | goto fail;
|
206 | 5f4da8c0 | ths | if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
|
207 | 5f4da8c0 | ths | goto fail;
|
208 | 5f4da8c0 | ths | |
209 | 5f4da8c0 | ths | /* write the header */
|
210 | 5f4da8c0 | ths | if (lseek(snp_fd, 0x0, SEEK_SET) == -1) |
211 | 5f4da8c0 | ths | goto fail;
|
212 | 5f4da8c0 | ths | if (write(snp_fd, hdr, HEADER_SIZE) == -1) |
213 | 5f4da8c0 | ths | goto fail;
|
214 | 5f4da8c0 | ths | |
215 | 5f4da8c0 | ths | memset(&header, 0, sizeof(header)); |
216 | 5f4da8c0 | ths | memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC |
217 | 5f4da8c0 | ths | |
218 | 5f4da8c0 | ths | ftruncate(snp_fd, header.grain_offset << 9);
|
219 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
220 | 5f4da8c0 | ths | if (lseek(p_fd, 0x200, SEEK_SET) == -1) |
221 | 5f4da8c0 | ths | goto fail;
|
222 | 5f4da8c0 | ths | if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
|
223 | 5f4da8c0 | ths | goto fail;
|
224 | 5f4da8c0 | ths | |
225 | 5f4da8c0 | ths | if ((p_name = strstr(p_desc,"CID")) != 0) { |
226 | 5f4da8c0 | ths | p_name += sizeof("CID"); |
227 | 5f4da8c0 | ths | sscanf(p_name,"%x",&p_cid);
|
228 | 5f4da8c0 | ths | } |
229 | 5f4da8c0 | ths | |
230 | 5f4da8c0 | ths | real_filename = filename; |
231 | 5f4da8c0 | ths | if ((temp_str = strrchr(real_filename, '\\')) != NULL) |
232 | 5f4da8c0 | ths | real_filename = temp_str + 1;
|
233 | 5f4da8c0 | ths | if ((temp_str = strrchr(real_filename, '/')) != NULL) |
234 | 5f4da8c0 | ths | real_filename = temp_str + 1;
|
235 | 5f4da8c0 | ths | if ((temp_str = strrchr(real_filename, ':')) != NULL) |
236 | 5f4da8c0 | ths | real_filename = temp_str + 1;
|
237 | 5f4da8c0 | ths | |
238 | 5f4da8c0 | ths | sprintf(s_desc, desc_template, p_cid, p_cid, backing_file |
239 | 5f4da8c0 | ths | , (uint32_t)header.capacity, real_filename); |
240 | 5f4da8c0 | ths | |
241 | 5f4da8c0 | ths | /* write the descriptor */
|
242 | 5f4da8c0 | ths | if (lseek(snp_fd, 0x200, SEEK_SET) == -1) |
243 | 5f4da8c0 | ths | goto fail;
|
244 | 5f4da8c0 | ths | if (write(snp_fd, s_desc, strlen(s_desc)) == -1) |
245 | 5f4da8c0 | ths | goto fail;
|
246 | ea2384d3 | bellard | |
247 | 5f4da8c0 | ths | gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
|
248 | 5f4da8c0 | ths | rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
|
249 | 5f4da8c0 | ths | capacity = header.capacity * SECTOR_SIZE; // Extent size
|
250 | 5f4da8c0 | ths | /*
|
251 | 5f4da8c0 | ths | * Each GDE span 32M disk, means:
|
252 | 5f4da8c0 | ths | * 512 GTE per GT, each GTE points to grain
|
253 | 5f4da8c0 | ths | */
|
254 | 5f4da8c0 | ths | gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE; |
255 | 5f4da8c0 | ths | if (!gt_size)
|
256 | 5f4da8c0 | ths | goto fail;
|
257 | 5f4da8c0 | ths | gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
|
258 | 5f4da8c0 | ths | gd_size = gde_entries * sizeof(uint32_t);
|
259 | 5f4da8c0 | ths | |
260 | 5f4da8c0 | ths | /* write RGD */
|
261 | 5f4da8c0 | ths | rgd_buf = qemu_malloc(gd_size); |
262 | 5f4da8c0 | ths | if (!rgd_buf)
|
263 | 5f4da8c0 | ths | goto fail;
|
264 | 5f4da8c0 | ths | if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) |
265 | 5f4da8c0 | ths | goto fail_rgd;
|
266 | 5f4da8c0 | ths | if (read(p_fd, rgd_buf, gd_size) != gd_size)
|
267 | 5f4da8c0 | ths | goto fail_rgd;
|
268 | 5f4da8c0 | ths | if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) |
269 | 5f4da8c0 | ths | goto fail_rgd;
|
270 | 5f4da8c0 | ths | if (write(snp_fd, rgd_buf, gd_size) == -1) |
271 | 5f4da8c0 | ths | goto fail_rgd;
|
272 | 5f4da8c0 | ths | qemu_free(rgd_buf); |
273 | 5f4da8c0 | ths | |
274 | 5f4da8c0 | ths | /* write GD */
|
275 | 5f4da8c0 | ths | gd_buf = qemu_malloc(gd_size); |
276 | 5f4da8c0 | ths | if (!gd_buf)
|
277 | 5f4da8c0 | ths | goto fail_rgd;
|
278 | 5f4da8c0 | ths | if (lseek(p_fd, gd_offset, SEEK_SET) == -1) |
279 | 5f4da8c0 | ths | goto fail_gd;
|
280 | 5f4da8c0 | ths | if (read(p_fd, gd_buf, gd_size) != gd_size)
|
281 | 5f4da8c0 | ths | goto fail_gd;
|
282 | 5f4da8c0 | ths | if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) |
283 | 5f4da8c0 | ths | goto fail_gd;
|
284 | 5f4da8c0 | ths | if (write(snp_fd, gd_buf, gd_size) == -1) |
285 | 5f4da8c0 | ths | goto fail_gd;
|
286 | 5f4da8c0 | ths | qemu_free(gd_buf); |
287 | 5f4da8c0 | ths | |
288 | 5f4da8c0 | ths | close(p_fd); |
289 | 5f4da8c0 | ths | close(snp_fd); |
290 | 5f4da8c0 | ths | return 0; |
291 | 5f4da8c0 | ths | |
292 | 5f4da8c0 | ths | fail_gd:
|
293 | 5f4da8c0 | ths | qemu_free(gd_buf); |
294 | 5f4da8c0 | ths | fail_rgd:
|
295 | 5f4da8c0 | ths | qemu_free(rgd_buf); |
296 | 5f4da8c0 | ths | fail:
|
297 | 5f4da8c0 | ths | close(p_fd); |
298 | 5f4da8c0 | ths | close(snp_fd); |
299 | 5f4da8c0 | ths | return -1; |
300 | 5f4da8c0 | ths | } |
301 | 5f4da8c0 | ths | |
302 | 5f4da8c0 | ths | static void vmdk_parent_close(BlockDriverState *bs) |
303 | 5f4da8c0 | ths | { |
304 | 5f4da8c0 | ths | if (bs->backing_hd)
|
305 | 5f4da8c0 | ths | bdrv_close(bs->backing_hd); |
306 | 5f4da8c0 | ths | } |
307 | 5f4da8c0 | ths | |
308 | 5f4da8c0 | ths | |
309 | 5f4da8c0 | ths | static int vmdk_parent_open(BlockDriverState *bs, const char * filename) |
310 | 5f4da8c0 | ths | { |
311 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
312 | 5f4da8c0 | ths | char *p_name;
|
313 | 5f4da8c0 | ths | char desc[DESC_SIZE];
|
314 | 5f4da8c0 | ths | char parent_img_name[1024]; |
315 | 5f4da8c0 | ths | |
316 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
317 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) |
318 | 5f4da8c0 | ths | return -1; |
319 | 5f4da8c0 | ths | |
320 | 5f4da8c0 | ths | if ((p_name = strstr(desc,"parentFileNameHint")) != 0) { |
321 | 5f4da8c0 | ths | char *end_name;
|
322 | 5f4da8c0 | ths | struct stat file_buf;
|
323 | 5f4da8c0 | ths | |
324 | 5f4da8c0 | ths | p_name += sizeof("parentFileNameHint") + 1; |
325 | 5f4da8c0 | ths | if ((end_name = strchr(p_name,'\"')) == 0) |
326 | 5f4da8c0 | ths | return -1; |
327 | 5f4da8c0 | ths | |
328 | 5f4da8c0 | ths | strncpy(s->hd->backing_file, p_name, end_name - p_name); |
329 | 5f4da8c0 | ths | if (stat(s->hd->backing_file, &file_buf) != 0) { |
330 | 5f4da8c0 | ths | path_combine(parent_img_name, sizeof(parent_img_name),
|
331 | 5f4da8c0 | ths | filename, s->hd->backing_file); |
332 | 5f4da8c0 | ths | } else {
|
333 | 5f4da8c0 | ths | strcpy(parent_img_name, s->hd->backing_file); |
334 | 5f4da8c0 | ths | } |
335 | 5f4da8c0 | ths | |
336 | 5f4da8c0 | ths | s->hd->backing_hd = bdrv_new("");
|
337 | 5f4da8c0 | ths | if (!s->hd->backing_hd) {
|
338 | 5f4da8c0 | ths | failure:
|
339 | 5f4da8c0 | ths | bdrv_close(s->hd); |
340 | ff1afc72 | bellard | return -1; |
341 | 5f4da8c0 | ths | } |
342 | 5f4da8c0 | ths | if (bdrv_open(s->hd->backing_hd, parent_img_name, 0) < 0) |
343 | 5f4da8c0 | ths | goto failure;
|
344 | ff1afc72 | bellard | } |
345 | 5f4da8c0 | ths | |
346 | 5f4da8c0 | ths | return 0; |
347 | 5f4da8c0 | ths | } |
348 | 5f4da8c0 | ths | |
349 | 5f4da8c0 | ths | static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) |
350 | 5f4da8c0 | ths | { |
351 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
352 | 5f4da8c0 | ths | uint32_t magic; |
353 | 5f4da8c0 | ths | int l1_size, i, ret;
|
354 | 5f4da8c0 | ths | |
355 | 5f4da8c0 | ths | ret = bdrv_file_open(&s->hd, filename, flags); |
356 | 5f4da8c0 | ths | if (ret < 0) |
357 | 5f4da8c0 | ths | return ret;
|
358 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic)) |
359 | ea2384d3 | bellard | goto fail;
|
360 | 5f4da8c0 | ths | |
361 | 7143c62c | bellard | magic = be32_to_cpu(magic); |
362 | ea2384d3 | bellard | if (magic == VMDK3_MAGIC) {
|
363 | ea2384d3 | bellard | VMDK3Header header; |
364 | 5f4da8c0 | ths | |
365 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) |
366 | ea2384d3 | bellard | goto fail;
|
367 | ea2384d3 | bellard | s->cluster_sectors = le32_to_cpu(header.granularity); |
368 | ea2384d3 | bellard | s->l2_size = 1 << 9; |
369 | ea2384d3 | bellard | s->l1_size = 1 << 6; |
370 | ea2384d3 | bellard | bs->total_sectors = le32_to_cpu(header.disk_sectors); |
371 | ff1afc72 | bellard | s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
|
372 | ff1afc72 | bellard | s->l1_backup_table_offset = 0;
|
373 | ea2384d3 | bellard | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
374 | ea2384d3 | bellard | } else if (magic == VMDK4_MAGIC) { |
375 | ea2384d3 | bellard | VMDK4Header header; |
376 | 5f4da8c0 | ths | |
377 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) |
378 | ea2384d3 | bellard | goto fail;
|
379 | bd6ea3c8 | bellard | bs->total_sectors = le64_to_cpu(header.capacity); |
380 | bd6ea3c8 | bellard | s->cluster_sectors = le64_to_cpu(header.granularity); |
381 | ea2384d3 | bellard | s->l2_size = le32_to_cpu(header.num_gtes_per_gte); |
382 | ea2384d3 | bellard | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
383 | ea2384d3 | bellard | if (s->l1_entry_sectors <= 0) |
384 | ea2384d3 | bellard | goto fail;
|
385 | ea2384d3 | bellard | s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
|
386 | ea2384d3 | bellard | / s->l1_entry_sectors; |
387 | ff1afc72 | bellard | s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
|
388 | ff1afc72 | bellard | s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
|
389 | 5f4da8c0 | ths | |
390 | 5f4da8c0 | ths | // try to open parent images, if exist
|
391 | 5f4da8c0 | ths | if (vmdk_parent_open(bs, filename) != 0) |
392 | 5f4da8c0 | ths | goto fail;
|
393 | 5f4da8c0 | ths | // write the CID once after the image creation
|
394 | 5f4da8c0 | ths | s->parent_cid = vmdk_read_cid(bs,1);
|
395 | ea2384d3 | bellard | } else {
|
396 | ea2384d3 | bellard | goto fail;
|
397 | ea2384d3 | bellard | } |
398 | 5f4da8c0 | ths | |
399 | ea2384d3 | bellard | /* read the L1 table */
|
400 | ea2384d3 | bellard | l1_size = s->l1_size * sizeof(uint32_t);
|
401 | ea2384d3 | bellard | s->l1_table = qemu_malloc(l1_size); |
402 | ea2384d3 | bellard | if (!s->l1_table)
|
403 | ea2384d3 | bellard | goto fail;
|
404 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
|
405 | ea2384d3 | bellard | goto fail;
|
406 | ea2384d3 | bellard | for(i = 0; i < s->l1_size; i++) { |
407 | ea2384d3 | bellard | le32_to_cpus(&s->l1_table[i]); |
408 | ea2384d3 | bellard | } |
409 | ea2384d3 | bellard | |
410 | ff1afc72 | bellard | if (s->l1_backup_table_offset) {
|
411 | ff1afc72 | bellard | s->l1_backup_table = qemu_malloc(l1_size); |
412 | ff1afc72 | bellard | if (!s->l1_backup_table)
|
413 | ff1afc72 | bellard | goto fail;
|
414 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
|
415 | ff1afc72 | bellard | goto fail;
|
416 | ff1afc72 | bellard | for(i = 0; i < s->l1_size; i++) { |
417 | ff1afc72 | bellard | le32_to_cpus(&s->l1_backup_table[i]); |
418 | ff1afc72 | bellard | } |
419 | ff1afc72 | bellard | } |
420 | ff1afc72 | bellard | |
421 | ea2384d3 | bellard | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
|
422 | ea2384d3 | bellard | if (!s->l2_cache)
|
423 | ea2384d3 | bellard | goto fail;
|
424 | ea2384d3 | bellard | return 0; |
425 | ea2384d3 | bellard | fail:
|
426 | ff1afc72 | bellard | qemu_free(s->l1_backup_table); |
427 | ea2384d3 | bellard | qemu_free(s->l1_table); |
428 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
429 | 5f4da8c0 | ths | bdrv_delete(s->hd); |
430 | ea2384d3 | bellard | return -1; |
431 | ea2384d3 | bellard | } |
432 | ea2384d3 | bellard | |
433 | 5f4da8c0 | ths | static uint64_t get_cluster_offset(BlockDriverState *bs, uint64_t offset, int allocate); |
434 | 5f4da8c0 | ths | |
435 | 5f4da8c0 | ths | static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset, |
436 | 5f4da8c0 | ths | uint64_t offset, int allocate)
|
437 | 5f4da8c0 | ths | { |
438 | 5f4da8c0 | ths | uint64_t parent_cluster_offset; |
439 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
440 | 5f4da8c0 | ths | uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB |
441 | 5f4da8c0 | ths | |
442 | 5f4da8c0 | ths | // we will be here if it's first write on non-exist grain(cluster).
|
443 | 5f4da8c0 | ths | // try to read from parent image, if exist
|
444 | 5f4da8c0 | ths | if (s->hd->backing_hd) {
|
445 | 5f4da8c0 | ths | BDRVVmdkState *ps = s->hd->backing_hd->opaque; |
446 | 5f4da8c0 | ths | |
447 | 5f4da8c0 | ths | if (!vmdk_is_cid_valid(bs))
|
448 | 5f4da8c0 | ths | return -1; |
449 | 5f4da8c0 | ths | parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, offset, allocate); |
450 | 5f4da8c0 | ths | if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != |
451 | 5f4da8c0 | ths | ps->cluster_sectors*512)
|
452 | 5f4da8c0 | ths | return -1; |
453 | 5f4da8c0 | ths | |
454 | 5f4da8c0 | ths | if (bdrv_pwrite(s->hd, cluster_offset << 9, whole_grain, sizeof(whole_grain)) != |
455 | 5f4da8c0 | ths | sizeof(whole_grain))
|
456 | 5f4da8c0 | ths | return -1; |
457 | 5f4da8c0 | ths | } |
458 | 5f4da8c0 | ths | return 0; |
459 | 5f4da8c0 | ths | } |
460 | 5f4da8c0 | ths | |
461 | ea2384d3 | bellard | static uint64_t get_cluster_offset(BlockDriverState *bs,
|
462 | ff1afc72 | bellard | uint64_t offset, int allocate)
|
463 | ea2384d3 | bellard | { |
464 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
465 | ea2384d3 | bellard | unsigned int l1_index, l2_offset, l2_index; |
466 | ea2384d3 | bellard | int min_index, i, j;
|
467 | ff1afc72 | bellard | uint32_t min_count, *l2_table, tmp; |
468 | ea2384d3 | bellard | uint64_t cluster_offset; |
469 | ea2384d3 | bellard | |
470 | ea2384d3 | bellard | l1_index = (offset >> 9) / s->l1_entry_sectors;
|
471 | ea2384d3 | bellard | if (l1_index >= s->l1_size)
|
472 | ea2384d3 | bellard | return 0; |
473 | ea2384d3 | bellard | l2_offset = s->l1_table[l1_index]; |
474 | ea2384d3 | bellard | if (!l2_offset)
|
475 | ea2384d3 | bellard | return 0; |
476 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
477 | ea2384d3 | bellard | if (l2_offset == s->l2_cache_offsets[i]) {
|
478 | ea2384d3 | bellard | /* increment the hit count */
|
479 | ea2384d3 | bellard | if (++s->l2_cache_counts[i] == 0xffffffff) { |
480 | ea2384d3 | bellard | for(j = 0; j < L2_CACHE_SIZE; j++) { |
481 | ea2384d3 | bellard | s->l2_cache_counts[j] >>= 1;
|
482 | ea2384d3 | bellard | } |
483 | ea2384d3 | bellard | } |
484 | ea2384d3 | bellard | l2_table = s->l2_cache + (i * s->l2_size); |
485 | ea2384d3 | bellard | goto found;
|
486 | ea2384d3 | bellard | } |
487 | ea2384d3 | bellard | } |
488 | ea2384d3 | bellard | /* not found: load a new entry in the least used one */
|
489 | ea2384d3 | bellard | min_index = 0;
|
490 | ea2384d3 | bellard | min_count = 0xffffffff;
|
491 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
492 | ea2384d3 | bellard | if (s->l2_cache_counts[i] < min_count) {
|
493 | ea2384d3 | bellard | min_count = s->l2_cache_counts[i]; |
494 | ea2384d3 | bellard | min_index = i; |
495 | ea2384d3 | bellard | } |
496 | ea2384d3 | bellard | } |
497 | ea2384d3 | bellard | l2_table = s->l2_cache + (min_index * s->l2_size); |
498 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) != |
499 | 5f4da8c0 | ths | s->l2_size * sizeof(uint32_t))
|
500 | ea2384d3 | bellard | return 0; |
501 | 5f4da8c0 | ths | |
502 | ea2384d3 | bellard | s->l2_cache_offsets[min_index] = l2_offset; |
503 | ea2384d3 | bellard | s->l2_cache_counts[min_index] = 1;
|
504 | ea2384d3 | bellard | found:
|
505 | ea2384d3 | bellard | l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
|
506 | ea2384d3 | bellard | cluster_offset = le32_to_cpu(l2_table[l2_index]); |
507 | ff1afc72 | bellard | if (!cluster_offset) {
|
508 | 5f4da8c0 | ths | struct stat file_buf;
|
509 | 5f4da8c0 | ths | |
510 | ff1afc72 | bellard | if (!allocate)
|
511 | ff1afc72 | bellard | return 0; |
512 | 5f4da8c0 | ths | stat(s->hd->filename, &file_buf); |
513 | 5f4da8c0 | ths | cluster_offset = file_buf.st_size; |
514 | 5f4da8c0 | ths | bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9));
|
515 | 5f4da8c0 | ths | |
516 | ff1afc72 | bellard | cluster_offset >>= 9;
|
517 | ff1afc72 | bellard | /* update L2 table */
|
518 | ff1afc72 | bellard | tmp = cpu_to_le32(cluster_offset); |
519 | ff1afc72 | bellard | l2_table[l2_index] = tmp; |
520 | 5f4da8c0 | ths | if (bdrv_pwrite(s->hd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), |
521 | 5f4da8c0 | ths | &tmp, sizeof(tmp)) != sizeof(tmp)) |
522 | ff1afc72 | bellard | return 0; |
523 | ff1afc72 | bellard | /* update backup L2 table */
|
524 | ff1afc72 | bellard | if (s->l1_backup_table_offset != 0) { |
525 | ff1afc72 | bellard | l2_offset = s->l1_backup_table[l1_index]; |
526 | 5f4da8c0 | ths | if (bdrv_pwrite(s->hd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), |
527 | 5f4da8c0 | ths | &tmp, sizeof(tmp)) != sizeof(tmp)) |
528 | ff1afc72 | bellard | return 0; |
529 | ff1afc72 | bellard | } |
530 | 5f4da8c0 | ths | |
531 | 5f4da8c0 | ths | if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1) |
532 | 5f4da8c0 | ths | return 0; |
533 | ff1afc72 | bellard | } |
534 | ea2384d3 | bellard | cluster_offset <<= 9;
|
535 | ea2384d3 | bellard | return cluster_offset;
|
536 | ea2384d3 | bellard | } |
537 | ea2384d3 | bellard | |
538 | ea2384d3 | bellard | static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num, |
539 | ea2384d3 | bellard | int nb_sectors, int *pnum) |
540 | ea2384d3 | bellard | { |
541 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
542 | ea2384d3 | bellard | int index_in_cluster, n;
|
543 | ea2384d3 | bellard | uint64_t cluster_offset; |
544 | ea2384d3 | bellard | |
545 | ff1afc72 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0); |
546 | ea2384d3 | bellard | index_in_cluster = sector_num % s->cluster_sectors; |
547 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
548 | ea2384d3 | bellard | if (n > nb_sectors)
|
549 | ea2384d3 | bellard | n = nb_sectors; |
550 | ea2384d3 | bellard | *pnum = n; |
551 | ea2384d3 | bellard | return (cluster_offset != 0); |
552 | ea2384d3 | bellard | } |
553 | ea2384d3 | bellard | |
554 | ea2384d3 | bellard | static int vmdk_read(BlockDriverState *bs, int64_t sector_num, |
555 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
556 | ea2384d3 | bellard | { |
557 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
558 | 5f4da8c0 | ths | int index_in_cluster, n, ret;
|
559 | ea2384d3 | bellard | uint64_t cluster_offset; |
560 | 5f4da8c0 | ths | |
561 | ea2384d3 | bellard | while (nb_sectors > 0) { |
562 | ff1afc72 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0); |
563 | ea2384d3 | bellard | index_in_cluster = sector_num % s->cluster_sectors; |
564 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
565 | ea2384d3 | bellard | if (n > nb_sectors)
|
566 | ea2384d3 | bellard | n = nb_sectors; |
567 | ea2384d3 | bellard | if (!cluster_offset) {
|
568 | 5f4da8c0 | ths | // try to read from parent image, if exist
|
569 | 5f4da8c0 | ths | if (s->hd->backing_hd) {
|
570 | 5f4da8c0 | ths | if (!vmdk_is_cid_valid(bs))
|
571 | 5f4da8c0 | ths | return -1; |
572 | 5f4da8c0 | ths | ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n); |
573 | 5f4da8c0 | ths | if (ret < 0) |
574 | 5f4da8c0 | ths | return -1; |
575 | 5f4da8c0 | ths | } else {
|
576 | 5f4da8c0 | ths | memset(buf, 0, 512 * n); |
577 | 5f4da8c0 | ths | } |
578 | ea2384d3 | bellard | } else {
|
579 | 5f4da8c0 | ths | if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) |
580 | ea2384d3 | bellard | return -1; |
581 | ea2384d3 | bellard | } |
582 | ea2384d3 | bellard | nb_sectors -= n; |
583 | ea2384d3 | bellard | sector_num += n; |
584 | ea2384d3 | bellard | buf += n * 512;
|
585 | ea2384d3 | bellard | } |
586 | ea2384d3 | bellard | return 0; |
587 | ea2384d3 | bellard | } |
588 | ea2384d3 | bellard | |
589 | ea2384d3 | bellard | static int vmdk_write(BlockDriverState *bs, int64_t sector_num, |
590 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
591 | ea2384d3 | bellard | { |
592 | ff1afc72 | bellard | BDRVVmdkState *s = bs->opaque; |
593 | 5f4da8c0 | ths | int index_in_cluster, n;
|
594 | ff1afc72 | bellard | uint64_t cluster_offset; |
595 | 5f4da8c0 | ths | static int cid_update = 0; |
596 | ff1afc72 | bellard | |
597 | ff1afc72 | bellard | while (nb_sectors > 0) { |
598 | ff1afc72 | bellard | index_in_cluster = sector_num & (s->cluster_sectors - 1);
|
599 | ff1afc72 | bellard | n = s->cluster_sectors - index_in_cluster; |
600 | ff1afc72 | bellard | if (n > nb_sectors)
|
601 | ff1afc72 | bellard | n = nb_sectors; |
602 | ff1afc72 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 1); |
603 | ff1afc72 | bellard | if (!cluster_offset)
|
604 | ff1afc72 | bellard | return -1; |
605 | 5f4da8c0 | ths | if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) |
606 | ff1afc72 | bellard | return -1; |
607 | ff1afc72 | bellard | nb_sectors -= n; |
608 | ff1afc72 | bellard | sector_num += n; |
609 | ff1afc72 | bellard | buf += n * 512;
|
610 | 5f4da8c0 | ths | |
611 | 5f4da8c0 | ths | // update CID on the first write every time the virtual disk is opened
|
612 | 5f4da8c0 | ths | if (!cid_update) {
|
613 | 5f4da8c0 | ths | vmdk_write_cid(bs, time(NULL));
|
614 | 5f4da8c0 | ths | cid_update++; |
615 | 5f4da8c0 | ths | } |
616 | ff1afc72 | bellard | } |
617 | ff1afc72 | bellard | return 0; |
618 | ea2384d3 | bellard | } |
619 | ea2384d3 | bellard | |
620 | 8979b227 | bellard | static int vmdk_create(const char *filename, int64_t total_size, |
621 | 8979b227 | bellard | const char *backing_file, int flags) |
622 | 8979b227 | bellard | { |
623 | 8979b227 | bellard | int fd, i;
|
624 | 8979b227 | bellard | VMDK4Header header; |
625 | 8979b227 | bellard | uint32_t tmp, magic, grains, gd_size, gt_size, gt_count; |
626 | 8979b227 | bellard | char *desc_template =
|
627 | 8979b227 | bellard | "# Disk DescriptorFile\n"
|
628 | 8979b227 | bellard | "version=1\n"
|
629 | 8979b227 | bellard | "CID=%x\n"
|
630 | 8979b227 | bellard | "parentCID=ffffffff\n"
|
631 | 8979b227 | bellard | "createType=\"monolithicSparse\"\n"
|
632 | 8979b227 | bellard | "\n"
|
633 | 8979b227 | bellard | "# Extent description\n"
|
634 | 8979b227 | bellard | "RW %lu SPARSE \"%s\"\n"
|
635 | 8979b227 | bellard | "\n"
|
636 | 8979b227 | bellard | "# The Disk Data Base \n"
|
637 | 8979b227 | bellard | "#DDB\n"
|
638 | 8979b227 | bellard | "\n"
|
639 | 5f4da8c0 | ths | "ddb.virtualHWVersion = \"4\"\n"
|
640 | 8979b227 | bellard | "ddb.geometry.cylinders = \"%lu\"\n"
|
641 | 8979b227 | bellard | "ddb.geometry.heads = \"16\"\n"
|
642 | 8979b227 | bellard | "ddb.geometry.sectors = \"63\"\n"
|
643 | 8979b227 | bellard | "ddb.adapterType = \"ide\"\n";
|
644 | 8979b227 | bellard | char desc[1024]; |
645 | 8979b227 | bellard | const char *real_filename, *temp_str; |
646 | 8979b227 | bellard | |
647 | 8979b227 | bellard | /* XXX: add support for backing file */
|
648 | 5f4da8c0 | ths | if (backing_file) {
|
649 | 5f4da8c0 | ths | return vmdk_snapshot_create(filename, backing_file);
|
650 | 5f4da8c0 | ths | } |
651 | 8979b227 | bellard | |
652 | 8979b227 | bellard | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
653 | 8979b227 | bellard | 0644);
|
654 | 8979b227 | bellard | if (fd < 0) |
655 | 8979b227 | bellard | return -1; |
656 | 8979b227 | bellard | magic = cpu_to_be32(VMDK4_MAGIC); |
657 | 8979b227 | bellard | memset(&header, 0, sizeof(header)); |
658 | 8979b227 | bellard | header.version = cpu_to_le32(1);
|
659 | 8979b227 | bellard | header.flags = cpu_to_le32(3); /* ?? */ |
660 | 8979b227 | bellard | header.capacity = cpu_to_le64(total_size); |
661 | 8979b227 | bellard | header.granularity = cpu_to_le64(128);
|
662 | 8979b227 | bellard | header.num_gtes_per_gte = cpu_to_le32(512);
|
663 | 8979b227 | bellard | |
664 | 8979b227 | bellard | grains = (total_size + header.granularity - 1) / header.granularity;
|
665 | 8979b227 | bellard | gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9; |
666 | 8979b227 | bellard | gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
|
667 | 8979b227 | bellard | gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9; |
668 | 8979b227 | bellard | |
669 | 8979b227 | bellard | header.desc_offset = 1;
|
670 | 8979b227 | bellard | header.desc_size = 20;
|
671 | 8979b227 | bellard | header.rgd_offset = header.desc_offset + header.desc_size; |
672 | 8979b227 | bellard | header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count); |
673 | 8979b227 | bellard | header.grain_offset = |
674 | 8979b227 | bellard | ((header.gd_offset + gd_size + (gt_size * gt_count) + |
675 | 8979b227 | bellard | header.granularity - 1) / header.granularity) *
|
676 | 8979b227 | bellard | header.granularity; |
677 | 8979b227 | bellard | |
678 | 8979b227 | bellard | header.desc_offset = cpu_to_le64(header.desc_offset); |
679 | 8979b227 | bellard | header.desc_size = cpu_to_le64(header.desc_size); |
680 | 8979b227 | bellard | header.rgd_offset = cpu_to_le64(header.rgd_offset); |
681 | 8979b227 | bellard | header.gd_offset = cpu_to_le64(header.gd_offset); |
682 | 8979b227 | bellard | header.grain_offset = cpu_to_le64(header.grain_offset); |
683 | 8979b227 | bellard | |
684 | 8979b227 | bellard | header.check_bytes[0] = 0xa; |
685 | 8979b227 | bellard | header.check_bytes[1] = 0x20; |
686 | 8979b227 | bellard | header.check_bytes[2] = 0xd; |
687 | 8979b227 | bellard | header.check_bytes[3] = 0xa; |
688 | 8979b227 | bellard | |
689 | 8979b227 | bellard | /* write all the data */
|
690 | 8979b227 | bellard | write(fd, &magic, sizeof(magic));
|
691 | 8979b227 | bellard | write(fd, &header, sizeof(header));
|
692 | 8979b227 | bellard | |
693 | 8979b227 | bellard | ftruncate(fd, header.grain_offset << 9);
|
694 | 8979b227 | bellard | |
695 | 8979b227 | bellard | /* write grain directory */
|
696 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
|
697 | 8979b227 | bellard | for (i = 0, tmp = header.rgd_offset + gd_size; |
698 | 8979b227 | bellard | i < gt_count; i++, tmp += gt_size) |
699 | 8979b227 | bellard | write(fd, &tmp, sizeof(tmp));
|
700 | 8979b227 | bellard | |
701 | 8979b227 | bellard | /* write backup grain directory */
|
702 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
|
703 | 8979b227 | bellard | for (i = 0, tmp = header.gd_offset + gd_size; |
704 | 8979b227 | bellard | i < gt_count; i++, tmp += gt_size) |
705 | 8979b227 | bellard | write(fd, &tmp, sizeof(tmp));
|
706 | 8979b227 | bellard | |
707 | 8979b227 | bellard | /* compose the descriptor */
|
708 | 8979b227 | bellard | real_filename = filename; |
709 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, '\\')) != NULL) |
710 | 8979b227 | bellard | real_filename = temp_str + 1;
|
711 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, '/')) != NULL) |
712 | 8979b227 | bellard | real_filename = temp_str + 1;
|
713 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, ':')) != NULL) |
714 | 8979b227 | bellard | real_filename = temp_str + 1;
|
715 | 8979b227 | bellard | sprintf(desc, desc_template, time(NULL), (unsigned long)total_size, |
716 | 8979b227 | bellard | real_filename, total_size / (63 * 16)); |
717 | 8979b227 | bellard | |
718 | 8979b227 | bellard | /* write the descriptor */
|
719 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
|
720 | 8979b227 | bellard | write(fd, desc, strlen(desc)); |
721 | 8979b227 | bellard | |
722 | 8979b227 | bellard | close(fd); |
723 | 8979b227 | bellard | return 0; |
724 | 8979b227 | bellard | } |
725 | 8979b227 | bellard | |
726 | e2731add | bellard | static void vmdk_close(BlockDriverState *bs) |
727 | ea2384d3 | bellard | { |
728 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
729 | 5f4da8c0 | ths | |
730 | ea2384d3 | bellard | qemu_free(s->l1_table); |
731 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
732 | 5f4da8c0 | ths | bdrv_delete(s->hd); |
733 | 5f4da8c0 | ths | // try to close parent image, if exist
|
734 | 5f4da8c0 | ths | vmdk_parent_close(s->hd); |
735 | ea2384d3 | bellard | } |
736 | ea2384d3 | bellard | |
737 | 7a6cba61 | pbrook | static void vmdk_flush(BlockDriverState *bs) |
738 | 7a6cba61 | pbrook | { |
739 | 7a6cba61 | pbrook | BDRVVmdkState *s = bs->opaque; |
740 | 5f4da8c0 | ths | bdrv_flush(s->hd); |
741 | 7a6cba61 | pbrook | } |
742 | 7a6cba61 | pbrook | |
743 | ea2384d3 | bellard | BlockDriver bdrv_vmdk = { |
744 | ea2384d3 | bellard | "vmdk",
|
745 | ea2384d3 | bellard | sizeof(BDRVVmdkState),
|
746 | ea2384d3 | bellard | vmdk_probe, |
747 | ea2384d3 | bellard | vmdk_open, |
748 | ea2384d3 | bellard | vmdk_read, |
749 | ea2384d3 | bellard | vmdk_write, |
750 | ea2384d3 | bellard | vmdk_close, |
751 | 8979b227 | bellard | vmdk_create, |
752 | 7a6cba61 | pbrook | vmdk_flush, |
753 | ea2384d3 | bellard | vmdk_is_allocated, |
754 | ea2384d3 | bellard | }; |