root / block / vmdk.c @ 76ae317f
History | View | Annotate | Download (25.4 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 | 5f4da8c0 | ths | BlockDriverState *hd; |
65 | ea2384d3 | bellard | int64_t l1_table_offset; |
66 | ff1afc72 | bellard | int64_t l1_backup_table_offset; |
67 | ea2384d3 | bellard | uint32_t *l1_table; |
68 | ff1afc72 | bellard | uint32_t *l1_backup_table; |
69 | ea2384d3 | bellard | unsigned int l1_size; |
70 | ea2384d3 | bellard | uint32_t l1_entry_sectors; |
71 | ea2384d3 | bellard | |
72 | ea2384d3 | bellard | unsigned int l2_size; |
73 | ea2384d3 | bellard | uint32_t *l2_cache; |
74 | ea2384d3 | bellard | uint32_t l2_cache_offsets[L2_CACHE_SIZE]; |
75 | ea2384d3 | bellard | uint32_t l2_cache_counts[L2_CACHE_SIZE]; |
76 | ea2384d3 | bellard | |
77 | ea2384d3 | bellard | unsigned int cluster_sectors; |
78 | 5f4da8c0 | ths | uint32_t parent_cid; |
79 | 630530a6 | ths | int is_parent;
|
80 | ea2384d3 | bellard | } BDRVVmdkState; |
81 | ea2384d3 | bellard | |
82 | 630530a6 | ths | typedef struct VmdkMetaData { |
83 | 630530a6 | ths | uint32_t offset; |
84 | 630530a6 | ths | unsigned int l1_index; |
85 | 630530a6 | ths | unsigned int l2_index; |
86 | 630530a6 | ths | unsigned int l2_offset; |
87 | 630530a6 | ths | int valid;
|
88 | 630530a6 | ths | } VmdkMetaData; |
89 | 630530a6 | ths | |
90 | 630530a6 | ths | typedef struct ActiveBDRVState{ |
91 | 630530a6 | ths | BlockDriverState *hd; // active image handler
|
92 | 630530a6 | ths | uint64_t cluster_offset; // current write offset
|
93 | 630530a6 | ths | }ActiveBDRVState; |
94 | 630530a6 | ths | |
95 | 630530a6 | ths | static ActiveBDRVState activeBDRV;
|
96 | 630530a6 | ths | |
97 | 630530a6 | ths | |
98 | ea2384d3 | bellard | static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) |
99 | ea2384d3 | bellard | { |
100 | ea2384d3 | bellard | uint32_t magic; |
101 | ea2384d3 | bellard | |
102 | ea2384d3 | bellard | if (buf_size < 4) |
103 | ea2384d3 | bellard | return 0; |
104 | ea2384d3 | bellard | magic = be32_to_cpu(*(uint32_t *)buf); |
105 | ea2384d3 | bellard | if (magic == VMDK3_MAGIC ||
|
106 | ea2384d3 | bellard | magic == VMDK4_MAGIC) |
107 | ea2384d3 | bellard | return 100; |
108 | ea2384d3 | bellard | else
|
109 | ea2384d3 | bellard | return 0; |
110 | ea2384d3 | bellard | } |
111 | ea2384d3 | bellard | |
112 | 5f4da8c0 | ths | #define CHECK_CID 1 |
113 | 5f4da8c0 | ths | |
114 | 3b46e624 | ths | #define SECTOR_SIZE 512 |
115 | 5f4da8c0 | ths | #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each |
116 | 5fafdf24 | ths | #define HEADER_SIZE 512 // first sector of 512 bytes |
117 | 5f4da8c0 | ths | |
118 | 5f4da8c0 | ths | static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) |
119 | ea2384d3 | bellard | { |
120 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
121 | 5f4da8c0 | ths | char desc[DESC_SIZE];
|
122 | 5f4da8c0 | ths | uint32_t cid; |
123 | 7ccfb2eb | blueswir1 | const char *p_name, *cid_str; |
124 | 5f4da8c0 | ths | size_t cid_str_size; |
125 | 5f4da8c0 | ths | |
126 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
127 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) |
128 | 5f4da8c0 | ths | return 0; |
129 | 5f4da8c0 | ths | |
130 | 5f4da8c0 | ths | if (parent) {
|
131 | 5f4da8c0 | ths | cid_str = "parentCID";
|
132 | 5f4da8c0 | ths | cid_str_size = sizeof("parentCID"); |
133 | 5f4da8c0 | ths | } else {
|
134 | 5f4da8c0 | ths | cid_str = "CID";
|
135 | 5f4da8c0 | ths | cid_str_size = sizeof("CID"); |
136 | 5f4da8c0 | ths | } |
137 | 5f4da8c0 | ths | |
138 | 511d2b14 | blueswir1 | if ((p_name = strstr(desc,cid_str)) != NULL) { |
139 | 5f4da8c0 | ths | p_name += cid_str_size; |
140 | 5f4da8c0 | ths | sscanf(p_name,"%x",&cid);
|
141 | 5f4da8c0 | ths | } |
142 | 5f4da8c0 | ths | |
143 | 5f4da8c0 | ths | return cid;
|
144 | 5f4da8c0 | ths | } |
145 | 5f4da8c0 | ths | |
146 | 5f4da8c0 | ths | static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid) |
147 | 5f4da8c0 | ths | { |
148 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
149 | 5f4da8c0 | ths | char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
|
150 | 5f4da8c0 | ths | char *p_name, *tmp_str;
|
151 | 5f4da8c0 | ths | |
152 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
153 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) |
154 | 5f4da8c0 | ths | return -1; |
155 | 5f4da8c0 | ths | |
156 | 5f4da8c0 | ths | tmp_str = strstr(desc,"parentCID");
|
157 | 363a37d5 | blueswir1 | pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
|
158 | 511d2b14 | blueswir1 | if ((p_name = strstr(desc,"CID")) != NULL) { |
159 | 5f4da8c0 | ths | p_name += sizeof("CID"); |
160 | 363a37d5 | blueswir1 | snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid); |
161 | 363a37d5 | blueswir1 | pstrcat(desc, sizeof(desc), tmp_desc);
|
162 | 5f4da8c0 | ths | } |
163 | 5f4da8c0 | ths | |
164 | 5f4da8c0 | ths | if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) |
165 | 5f4da8c0 | ths | return -1; |
166 | 5f4da8c0 | ths | return 0; |
167 | 5f4da8c0 | ths | } |
168 | 5f4da8c0 | ths | |
169 | 5f4da8c0 | ths | static int vmdk_is_cid_valid(BlockDriverState *bs) |
170 | 5f4da8c0 | ths | { |
171 | 5f4da8c0 | ths | #ifdef CHECK_CID
|
172 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
173 | 5f4da8c0 | ths | BlockDriverState *p_bs = s->hd->backing_hd; |
174 | 5f4da8c0 | ths | uint32_t cur_pcid; |
175 | 5f4da8c0 | ths | |
176 | 5f4da8c0 | ths | if (p_bs) {
|
177 | 5f4da8c0 | ths | cur_pcid = vmdk_read_cid(p_bs,0);
|
178 | 5f4da8c0 | ths | if (s->parent_cid != cur_pcid)
|
179 | 5f4da8c0 | ths | // CID not valid
|
180 | 5f4da8c0 | ths | return 0; |
181 | 5f4da8c0 | ths | } |
182 | 5f4da8c0 | ths | #endif
|
183 | 5f4da8c0 | ths | // CID valid
|
184 | 5f4da8c0 | ths | return 1; |
185 | 5f4da8c0 | ths | } |
186 | 5f4da8c0 | ths | |
187 | 5f4da8c0 | ths | static int vmdk_snapshot_create(const char *filename, const char *backing_file) |
188 | 5f4da8c0 | ths | { |
189 | 5f4da8c0 | ths | int snp_fd, p_fd;
|
190 | 5f4da8c0 | ths | uint32_t p_cid; |
191 | 5fafdf24 | ths | char *p_name, *gd_buf, *rgd_buf;
|
192 | 5f4da8c0 | ths | const char *real_filename, *temp_str; |
193 | 5f4da8c0 | ths | VMDK4Header header; |
194 | 5f4da8c0 | ths | uint32_t gde_entries, gd_size; |
195 | 5f4da8c0 | ths | int64_t gd_offset, rgd_offset, capacity, gt_size; |
196 | 5f4da8c0 | ths | char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
|
197 | 7ccfb2eb | blueswir1 | static const char desc_template[] = |
198 | 5f4da8c0 | ths | "# Disk DescriptorFile\n"
|
199 | 5f4da8c0 | ths | "version=1\n"
|
200 | 5f4da8c0 | ths | "CID=%x\n"
|
201 | 5f4da8c0 | ths | "parentCID=%x\n"
|
202 | 5f4da8c0 | ths | "createType=\"monolithicSparse\"\n"
|
203 | 5f4da8c0 | ths | "parentFileNameHint=\"%s\"\n"
|
204 | 5f4da8c0 | ths | "\n"
|
205 | 5f4da8c0 | ths | "# Extent description\n"
|
206 | 7ccfb2eb | blueswir1 | "RW %u SPARSE \"%s\"\n"
|
207 | 5f4da8c0 | ths | "\n"
|
208 | 5f4da8c0 | ths | "# The Disk Data Base \n"
|
209 | 5f4da8c0 | ths | "#DDB\n"
|
210 | 5f4da8c0 | ths | "\n";
|
211 | 5f4da8c0 | ths | |
212 | 5f4da8c0 | ths | snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
|
213 | 5f4da8c0 | ths | if (snp_fd < 0) |
214 | 5f4da8c0 | ths | return -1; |
215 | 5f4da8c0 | ths | p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE); |
216 | 5f4da8c0 | ths | if (p_fd < 0) { |
217 | 5f4da8c0 | ths | close(snp_fd); |
218 | 5f4da8c0 | ths | return -1; |
219 | 5f4da8c0 | ths | } |
220 | 5f4da8c0 | ths | |
221 | 5f4da8c0 | ths | /* read the header */
|
222 | 5f4da8c0 | ths | if (lseek(p_fd, 0x0, SEEK_SET) == -1) |
223 | 5f4da8c0 | ths | goto fail;
|
224 | 5f4da8c0 | ths | if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
|
225 | 5f4da8c0 | ths | goto fail;
|
226 | 5f4da8c0 | ths | |
227 | 5f4da8c0 | ths | /* write the header */
|
228 | 5f4da8c0 | ths | if (lseek(snp_fd, 0x0, SEEK_SET) == -1) |
229 | 5f4da8c0 | ths | goto fail;
|
230 | 5f4da8c0 | ths | if (write(snp_fd, hdr, HEADER_SIZE) == -1) |
231 | 5f4da8c0 | ths | goto fail;
|
232 | 5f4da8c0 | ths | |
233 | 5f4da8c0 | ths | memset(&header, 0, sizeof(header)); |
234 | 5f4da8c0 | ths | memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC |
235 | 5f4da8c0 | ths | |
236 | 5f4da8c0 | ths | ftruncate(snp_fd, header.grain_offset << 9);
|
237 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
238 | 5f4da8c0 | ths | if (lseek(p_fd, 0x200, SEEK_SET) == -1) |
239 | 5f4da8c0 | ths | goto fail;
|
240 | 5f4da8c0 | ths | if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
|
241 | 5f4da8c0 | ths | goto fail;
|
242 | 5f4da8c0 | ths | |
243 | 511d2b14 | blueswir1 | if ((p_name = strstr(p_desc,"CID")) != NULL) { |
244 | 5f4da8c0 | ths | p_name += sizeof("CID"); |
245 | 5f4da8c0 | ths | sscanf(p_name,"%x",&p_cid);
|
246 | 5f4da8c0 | ths | } |
247 | 5f4da8c0 | ths | |
248 | 5f4da8c0 | ths | real_filename = filename; |
249 | 5f4da8c0 | ths | if ((temp_str = strrchr(real_filename, '\\')) != NULL) |
250 | 5f4da8c0 | ths | real_filename = temp_str + 1;
|
251 | 5f4da8c0 | ths | if ((temp_str = strrchr(real_filename, '/')) != NULL) |
252 | 5f4da8c0 | ths | real_filename = temp_str + 1;
|
253 | 5f4da8c0 | ths | if ((temp_str = strrchr(real_filename, ':')) != NULL) |
254 | 5f4da8c0 | ths | real_filename = temp_str + 1;
|
255 | 5f4da8c0 | ths | |
256 | 363a37d5 | blueswir1 | snprintf(s_desc, sizeof(s_desc), desc_template, p_cid, p_cid, backing_file,
|
257 | 363a37d5 | blueswir1 | (uint32_t)header.capacity, real_filename); |
258 | 5f4da8c0 | ths | |
259 | 5f4da8c0 | ths | /* write the descriptor */
|
260 | 5f4da8c0 | ths | if (lseek(snp_fd, 0x200, SEEK_SET) == -1) |
261 | 5f4da8c0 | ths | goto fail;
|
262 | 5f4da8c0 | ths | if (write(snp_fd, s_desc, strlen(s_desc)) == -1) |
263 | 5f4da8c0 | ths | goto fail;
|
264 | ea2384d3 | bellard | |
265 | 5f4da8c0 | ths | gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
|
266 | 5f4da8c0 | ths | rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
|
267 | 5f4da8c0 | ths | capacity = header.capacity * SECTOR_SIZE; // Extent size
|
268 | 5f4da8c0 | ths | /*
|
269 | 5f4da8c0 | ths | * Each GDE span 32M disk, means:
|
270 | 5f4da8c0 | ths | * 512 GTE per GT, each GTE points to grain
|
271 | 5f4da8c0 | ths | */
|
272 | 5f4da8c0 | ths | gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE; |
273 | 5f4da8c0 | ths | if (!gt_size)
|
274 | 5f4da8c0 | ths | goto fail;
|
275 | 5fafdf24 | ths | gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
|
276 | 5f4da8c0 | ths | gd_size = gde_entries * sizeof(uint32_t);
|
277 | 5f4da8c0 | ths | |
278 | 5f4da8c0 | ths | /* write RGD */
|
279 | 5f4da8c0 | ths | rgd_buf = qemu_malloc(gd_size); |
280 | 5f4da8c0 | ths | if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) |
281 | 5f4da8c0 | ths | goto fail_rgd;
|
282 | 5f4da8c0 | ths | if (read(p_fd, rgd_buf, gd_size) != gd_size)
|
283 | 5f4da8c0 | ths | goto fail_rgd;
|
284 | 5f4da8c0 | ths | if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) |
285 | 5f4da8c0 | ths | goto fail_rgd;
|
286 | 5f4da8c0 | ths | if (write(snp_fd, rgd_buf, gd_size) == -1) |
287 | 5f4da8c0 | ths | goto fail_rgd;
|
288 | 5f4da8c0 | ths | qemu_free(rgd_buf); |
289 | 5f4da8c0 | ths | |
290 | 5f4da8c0 | ths | /* write GD */
|
291 | 5f4da8c0 | ths | gd_buf = qemu_malloc(gd_size); |
292 | 5f4da8c0 | ths | if (lseek(p_fd, gd_offset, SEEK_SET) == -1) |
293 | 5f4da8c0 | ths | goto fail_gd;
|
294 | 5f4da8c0 | ths | if (read(p_fd, gd_buf, gd_size) != gd_size)
|
295 | 5f4da8c0 | ths | goto fail_gd;
|
296 | 5f4da8c0 | ths | if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) |
297 | 5f4da8c0 | ths | goto fail_gd;
|
298 | 5f4da8c0 | ths | if (write(snp_fd, gd_buf, gd_size) == -1) |
299 | 5f4da8c0 | ths | goto fail_gd;
|
300 | 5f4da8c0 | ths | qemu_free(gd_buf); |
301 | 5f4da8c0 | ths | |
302 | 5f4da8c0 | ths | close(p_fd); |
303 | 5f4da8c0 | ths | close(snp_fd); |
304 | 5f4da8c0 | ths | return 0; |
305 | 5f4da8c0 | ths | |
306 | 5f4da8c0 | ths | fail_gd:
|
307 | 5f4da8c0 | ths | qemu_free(gd_buf); |
308 | 3b46e624 | ths | fail_rgd:
|
309 | 5f4da8c0 | ths | qemu_free(rgd_buf); |
310 | 5f4da8c0 | ths | fail:
|
311 | 5f4da8c0 | ths | close(p_fd); |
312 | 5f4da8c0 | ths | close(snp_fd); |
313 | 5f4da8c0 | ths | return -1; |
314 | 5f4da8c0 | ths | } |
315 | 5f4da8c0 | ths | |
316 | 5f4da8c0 | ths | static void vmdk_parent_close(BlockDriverState *bs) |
317 | 5f4da8c0 | ths | { |
318 | 5f4da8c0 | ths | if (bs->backing_hd)
|
319 | 5f4da8c0 | ths | bdrv_close(bs->backing_hd); |
320 | 5f4da8c0 | ths | } |
321 | 5f4da8c0 | ths | |
322 | b1d8e52e | blueswir1 | static int parent_open = 0; |
323 | 5f4da8c0 | ths | static int vmdk_parent_open(BlockDriverState *bs, const char * filename) |
324 | 5f4da8c0 | ths | { |
325 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
326 | 5fafdf24 | ths | char *p_name;
|
327 | 5f4da8c0 | ths | char desc[DESC_SIZE];
|
328 | 5f4da8c0 | ths | char parent_img_name[1024]; |
329 | 5f4da8c0 | ths | |
330 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
331 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) |
332 | 5f4da8c0 | ths | return -1; |
333 | 5f4da8c0 | ths | |
334 | 511d2b14 | blueswir1 | if ((p_name = strstr(desc,"parentFileNameHint")) != NULL) { |
335 | 5f4da8c0 | ths | char *end_name;
|
336 | 5f4da8c0 | ths | struct stat file_buf;
|
337 | 5f4da8c0 | ths | |
338 | 5f4da8c0 | ths | p_name += sizeof("parentFileNameHint") + 1; |
339 | 511d2b14 | blueswir1 | if ((end_name = strchr(p_name,'\"')) == NULL) |
340 | 5f4da8c0 | ths | return -1; |
341 | b34d259a | balrog | if ((end_name - p_name) > sizeof (s->hd->backing_file) - 1) |
342 | b34d259a | balrog | return -1; |
343 | 3b46e624 | ths | |
344 | 9b8114a8 | aliguori | pstrcpy(s->hd->backing_file, end_name - p_name + 1, p_name);
|
345 | 5f4da8c0 | ths | if (stat(s->hd->backing_file, &file_buf) != 0) { |
346 | 5f4da8c0 | ths | path_combine(parent_img_name, sizeof(parent_img_name),
|
347 | 5f4da8c0 | ths | filename, s->hd->backing_file); |
348 | 5f4da8c0 | ths | } else {
|
349 | 363a37d5 | blueswir1 | pstrcpy(parent_img_name, sizeof(parent_img_name),
|
350 | 363a37d5 | blueswir1 | s->hd->backing_file); |
351 | 5f4da8c0 | ths | } |
352 | 5f4da8c0 | ths | |
353 | 5f4da8c0 | ths | s->hd->backing_hd = bdrv_new("");
|
354 | 5f4da8c0 | ths | if (!s->hd->backing_hd) {
|
355 | 5f4da8c0 | ths | failure:
|
356 | 5f4da8c0 | ths | bdrv_close(s->hd); |
357 | ff1afc72 | bellard | return -1; |
358 | 5f4da8c0 | ths | } |
359 | 630530a6 | ths | parent_open = 1;
|
360 | 630530a6 | ths | if (bdrv_open(s->hd->backing_hd, parent_img_name, BDRV_O_RDONLY) < 0) |
361 | 5f4da8c0 | ths | goto failure;
|
362 | 630530a6 | ths | parent_open = 0;
|
363 | ff1afc72 | bellard | } |
364 | 5f4da8c0 | ths | |
365 | 5f4da8c0 | ths | return 0; |
366 | 5f4da8c0 | ths | } |
367 | 5f4da8c0 | ths | |
368 | 5f4da8c0 | ths | static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) |
369 | 5f4da8c0 | ths | { |
370 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
371 | 5f4da8c0 | ths | uint32_t magic; |
372 | 5f4da8c0 | ths | int l1_size, i, ret;
|
373 | 5f4da8c0 | ths | |
374 | 630530a6 | ths | if (parent_open)
|
375 | 630530a6 | ths | // Parent must be opened as RO.
|
376 | 630530a6 | ths | flags = BDRV_O_RDONLY; |
377 | 630530a6 | ths | |
378 | b5eff355 | aurel32 | ret = bdrv_file_open(&s->hd, filename, flags); |
379 | 5f4da8c0 | ths | if (ret < 0) |
380 | 5f4da8c0 | ths | return ret;
|
381 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic)) |
382 | ea2384d3 | bellard | goto fail;
|
383 | 5f4da8c0 | ths | |
384 | 7143c62c | bellard | magic = be32_to_cpu(magic); |
385 | ea2384d3 | bellard | if (magic == VMDK3_MAGIC) {
|
386 | ea2384d3 | bellard | VMDK3Header header; |
387 | 5f4da8c0 | ths | |
388 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) |
389 | ea2384d3 | bellard | goto fail;
|
390 | ea2384d3 | bellard | s->cluster_sectors = le32_to_cpu(header.granularity); |
391 | ea2384d3 | bellard | s->l2_size = 1 << 9; |
392 | ea2384d3 | bellard | s->l1_size = 1 << 6; |
393 | ea2384d3 | bellard | bs->total_sectors = le32_to_cpu(header.disk_sectors); |
394 | ff1afc72 | bellard | s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
|
395 | ff1afc72 | bellard | s->l1_backup_table_offset = 0;
|
396 | ea2384d3 | bellard | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
397 | ea2384d3 | bellard | } else if (magic == VMDK4_MAGIC) { |
398 | ea2384d3 | bellard | VMDK4Header header; |
399 | 5f4da8c0 | ths | |
400 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) |
401 | ea2384d3 | bellard | goto fail;
|
402 | bd6ea3c8 | bellard | bs->total_sectors = le64_to_cpu(header.capacity); |
403 | bd6ea3c8 | bellard | s->cluster_sectors = le64_to_cpu(header.granularity); |
404 | ea2384d3 | bellard | s->l2_size = le32_to_cpu(header.num_gtes_per_gte); |
405 | ea2384d3 | bellard | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
406 | ea2384d3 | bellard | if (s->l1_entry_sectors <= 0) |
407 | ea2384d3 | bellard | goto fail;
|
408 | 5fafdf24 | ths | s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
|
409 | ea2384d3 | bellard | / s->l1_entry_sectors; |
410 | ff1afc72 | bellard | s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
|
411 | ff1afc72 | bellard | s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
|
412 | 5f4da8c0 | ths | |
413 | 630530a6 | ths | if (parent_open)
|
414 | 630530a6 | ths | s->is_parent = 1;
|
415 | 630530a6 | ths | else
|
416 | 630530a6 | ths | s->is_parent = 0;
|
417 | 630530a6 | ths | |
418 | 5f4da8c0 | ths | // try to open parent images, if exist
|
419 | 5f4da8c0 | ths | if (vmdk_parent_open(bs, filename) != 0) |
420 | 5f4da8c0 | ths | goto fail;
|
421 | 5f4da8c0 | ths | // write the CID once after the image creation
|
422 | 5f4da8c0 | ths | s->parent_cid = vmdk_read_cid(bs,1);
|
423 | ea2384d3 | bellard | } else {
|
424 | ea2384d3 | bellard | goto fail;
|
425 | ea2384d3 | bellard | } |
426 | 5f4da8c0 | ths | |
427 | ea2384d3 | bellard | /* read the L1 table */
|
428 | ea2384d3 | bellard | l1_size = s->l1_size * sizeof(uint32_t);
|
429 | ea2384d3 | bellard | s->l1_table = qemu_malloc(l1_size); |
430 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
|
431 | ea2384d3 | bellard | goto fail;
|
432 | ea2384d3 | bellard | for(i = 0; i < s->l1_size; i++) { |
433 | ea2384d3 | bellard | le32_to_cpus(&s->l1_table[i]); |
434 | ea2384d3 | bellard | } |
435 | ea2384d3 | bellard | |
436 | ff1afc72 | bellard | if (s->l1_backup_table_offset) {
|
437 | ff1afc72 | bellard | s->l1_backup_table = qemu_malloc(l1_size); |
438 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
|
439 | ff1afc72 | bellard | goto fail;
|
440 | ff1afc72 | bellard | for(i = 0; i < s->l1_size; i++) { |
441 | ff1afc72 | bellard | le32_to_cpus(&s->l1_backup_table[i]); |
442 | ff1afc72 | bellard | } |
443 | ff1afc72 | bellard | } |
444 | ff1afc72 | bellard | |
445 | ea2384d3 | bellard | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
|
446 | ea2384d3 | bellard | return 0; |
447 | ea2384d3 | bellard | fail:
|
448 | ff1afc72 | bellard | qemu_free(s->l1_backup_table); |
449 | ea2384d3 | bellard | qemu_free(s->l1_table); |
450 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
451 | 5f4da8c0 | ths | bdrv_delete(s->hd); |
452 | ea2384d3 | bellard | return -1; |
453 | ea2384d3 | bellard | } |
454 | ea2384d3 | bellard | |
455 | 630530a6 | ths | static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
|
456 | 630530a6 | ths | uint64_t offset, int allocate);
|
457 | 5f4da8c0 | ths | |
458 | 5f4da8c0 | ths | static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset, |
459 | 5f4da8c0 | ths | uint64_t offset, int allocate)
|
460 | 5f4da8c0 | ths | { |
461 | 5f4da8c0 | ths | uint64_t parent_cluster_offset; |
462 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
463 | 5f4da8c0 | ths | uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB |
464 | 5f4da8c0 | ths | |
465 | 5f4da8c0 | ths | // we will be here if it's first write on non-exist grain(cluster).
|
466 | 5f4da8c0 | ths | // try to read from parent image, if exist
|
467 | 5f4da8c0 | ths | if (s->hd->backing_hd) {
|
468 | 5f4da8c0 | ths | BDRVVmdkState *ps = s->hd->backing_hd->opaque; |
469 | 5f4da8c0 | ths | |
470 | 5f4da8c0 | ths | if (!vmdk_is_cid_valid(bs))
|
471 | 5f4da8c0 | ths | return -1; |
472 | 5f4da8c0 | ths | |
473 | 630530a6 | ths | parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, NULL, offset, allocate);
|
474 | 630530a6 | ths | |
475 | 630530a6 | ths | if (parent_cluster_offset) {
|
476 | 630530a6 | ths | BDRVVmdkState *act_s = activeBDRV.hd->opaque; |
477 | 630530a6 | ths | |
478 | 630530a6 | ths | if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != ps->cluster_sectors*512) |
479 | 630530a6 | ths | return -1; |
480 | 630530a6 | ths | |
481 | 630530a6 | ths | //Write grain only into the active image
|
482 | 630530a6 | ths | if (bdrv_pwrite(act_s->hd, activeBDRV.cluster_offset << 9, whole_grain, sizeof(whole_grain)) != sizeof(whole_grain)) |
483 | 630530a6 | ths | return -1; |
484 | 630530a6 | ths | } |
485 | 630530a6 | ths | } |
486 | 630530a6 | ths | return 0; |
487 | 630530a6 | ths | } |
488 | 630530a6 | ths | |
489 | 630530a6 | ths | static int vmdk_L2update(BlockDriverState *bs, VmdkMetaData *m_data) |
490 | 630530a6 | ths | { |
491 | 630530a6 | ths | BDRVVmdkState *s = bs->opaque; |
492 | 630530a6 | ths | |
493 | 630530a6 | ths | /* update L2 table */
|
494 | 630530a6 | ths | if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)), |
495 | 630530a6 | ths | &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset)) |
496 | 630530a6 | ths | return -1; |
497 | 630530a6 | ths | /* update backup L2 table */
|
498 | 630530a6 | ths | if (s->l1_backup_table_offset != 0) { |
499 | 630530a6 | ths | m_data->l2_offset = s->l1_backup_table[m_data->l1_index]; |
500 | 630530a6 | ths | if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)), |
501 | 630530a6 | ths | &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset)) |
502 | 5f4da8c0 | ths | return -1; |
503 | 5f4da8c0 | ths | } |
504 | 630530a6 | ths | |
505 | 5f4da8c0 | ths | return 0; |
506 | 5f4da8c0 | ths | } |
507 | 5f4da8c0 | ths | |
508 | 630530a6 | ths | static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
|
509 | ff1afc72 | bellard | uint64_t offset, int allocate)
|
510 | ea2384d3 | bellard | { |
511 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
512 | ea2384d3 | bellard | unsigned int l1_index, l2_offset, l2_index; |
513 | ea2384d3 | bellard | int min_index, i, j;
|
514 | 630530a6 | ths | uint32_t min_count, *l2_table, tmp = 0;
|
515 | ea2384d3 | bellard | uint64_t cluster_offset; |
516 | 630530a6 | ths | |
517 | 630530a6 | ths | if (m_data)
|
518 | 630530a6 | ths | m_data->valid = 0;
|
519 | 630530a6 | ths | |
520 | ea2384d3 | bellard | l1_index = (offset >> 9) / s->l1_entry_sectors;
|
521 | ea2384d3 | bellard | if (l1_index >= s->l1_size)
|
522 | ea2384d3 | bellard | return 0; |
523 | ea2384d3 | bellard | l2_offset = s->l1_table[l1_index]; |
524 | ea2384d3 | bellard | if (!l2_offset)
|
525 | ea2384d3 | bellard | return 0; |
526 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
527 | ea2384d3 | bellard | if (l2_offset == s->l2_cache_offsets[i]) {
|
528 | ea2384d3 | bellard | /* increment the hit count */
|
529 | ea2384d3 | bellard | if (++s->l2_cache_counts[i] == 0xffffffff) { |
530 | ea2384d3 | bellard | for(j = 0; j < L2_CACHE_SIZE; j++) { |
531 | ea2384d3 | bellard | s->l2_cache_counts[j] >>= 1;
|
532 | ea2384d3 | bellard | } |
533 | ea2384d3 | bellard | } |
534 | ea2384d3 | bellard | l2_table = s->l2_cache + (i * s->l2_size); |
535 | ea2384d3 | bellard | goto found;
|
536 | ea2384d3 | bellard | } |
537 | ea2384d3 | bellard | } |
538 | ea2384d3 | bellard | /* not found: load a new entry in the least used one */
|
539 | ea2384d3 | bellard | min_index = 0;
|
540 | ea2384d3 | bellard | min_count = 0xffffffff;
|
541 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
542 | ea2384d3 | bellard | if (s->l2_cache_counts[i] < min_count) {
|
543 | ea2384d3 | bellard | min_count = s->l2_cache_counts[i]; |
544 | ea2384d3 | bellard | min_index = i; |
545 | ea2384d3 | bellard | } |
546 | ea2384d3 | bellard | } |
547 | ea2384d3 | bellard | l2_table = s->l2_cache + (min_index * s->l2_size); |
548 | 5fafdf24 | ths | if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) != |
549 | 5f4da8c0 | ths | s->l2_size * sizeof(uint32_t))
|
550 | ea2384d3 | bellard | return 0; |
551 | 5f4da8c0 | ths | |
552 | ea2384d3 | bellard | s->l2_cache_offsets[min_index] = l2_offset; |
553 | ea2384d3 | bellard | s->l2_cache_counts[min_index] = 1;
|
554 | ea2384d3 | bellard | found:
|
555 | ea2384d3 | bellard | l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
|
556 | ea2384d3 | bellard | cluster_offset = le32_to_cpu(l2_table[l2_index]); |
557 | 630530a6 | ths | |
558 | ff1afc72 | bellard | if (!cluster_offset) {
|
559 | ff1afc72 | bellard | if (!allocate)
|
560 | ff1afc72 | bellard | return 0; |
561 | 630530a6 | ths | // Avoid the L2 tables update for the images that have snapshots.
|
562 | 630530a6 | ths | if (!s->is_parent) {
|
563 | 49140a77 | ths | cluster_offset = bdrv_getlength(s->hd); |
564 | 630530a6 | ths | bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9));
|
565 | 630530a6 | ths | |
566 | 630530a6 | ths | cluster_offset >>= 9;
|
567 | 630530a6 | ths | tmp = cpu_to_le32(cluster_offset); |
568 | 630530a6 | ths | l2_table[l2_index] = tmp; |
569 | 630530a6 | ths | // Save the active image state
|
570 | 630530a6 | ths | activeBDRV.cluster_offset = cluster_offset; |
571 | 630530a6 | ths | activeBDRV.hd = bs; |
572 | ff1afc72 | bellard | } |
573 | 630530a6 | ths | /* First of all we write grain itself, to avoid race condition
|
574 | 630530a6 | ths | * that may to corrupt the image.
|
575 | 630530a6 | ths | * This problem may occur because of insufficient space on host disk
|
576 | 630530a6 | ths | * or inappropriate VM shutdown.
|
577 | 630530a6 | ths | */
|
578 | 5f4da8c0 | ths | if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1) |
579 | 5f4da8c0 | ths | return 0; |
580 | 630530a6 | ths | |
581 | 630530a6 | ths | if (m_data) {
|
582 | 630530a6 | ths | m_data->offset = tmp; |
583 | 630530a6 | ths | m_data->l1_index = l1_index; |
584 | 630530a6 | ths | m_data->l2_index = l2_index; |
585 | 630530a6 | ths | m_data->l2_offset = l2_offset; |
586 | 630530a6 | ths | m_data->valid = 1;
|
587 | 630530a6 | ths | } |
588 | ff1afc72 | bellard | } |
589 | ea2384d3 | bellard | cluster_offset <<= 9;
|
590 | ea2384d3 | bellard | return cluster_offset;
|
591 | ea2384d3 | bellard | } |
592 | ea2384d3 | bellard | |
593 | 5fafdf24 | ths | static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num, |
594 | ea2384d3 | bellard | int nb_sectors, int *pnum) |
595 | ea2384d3 | bellard | { |
596 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
597 | ea2384d3 | bellard | int index_in_cluster, n;
|
598 | ea2384d3 | bellard | uint64_t cluster_offset; |
599 | ea2384d3 | bellard | |
600 | 630530a6 | ths | cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0); |
601 | ea2384d3 | bellard | index_in_cluster = sector_num % s->cluster_sectors; |
602 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
603 | ea2384d3 | bellard | if (n > nb_sectors)
|
604 | ea2384d3 | bellard | n = nb_sectors; |
605 | ea2384d3 | bellard | *pnum = n; |
606 | ea2384d3 | bellard | return (cluster_offset != 0); |
607 | ea2384d3 | bellard | } |
608 | ea2384d3 | bellard | |
609 | 5fafdf24 | ths | static int vmdk_read(BlockDriverState *bs, int64_t sector_num, |
610 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
611 | ea2384d3 | bellard | { |
612 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
613 | 5f4da8c0 | ths | int index_in_cluster, n, ret;
|
614 | ea2384d3 | bellard | uint64_t cluster_offset; |
615 | 5f4da8c0 | ths | |
616 | ea2384d3 | bellard | while (nb_sectors > 0) { |
617 | 630530a6 | ths | cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0); |
618 | ea2384d3 | bellard | index_in_cluster = sector_num % s->cluster_sectors; |
619 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
620 | ea2384d3 | bellard | if (n > nb_sectors)
|
621 | ea2384d3 | bellard | n = nb_sectors; |
622 | ea2384d3 | bellard | if (!cluster_offset) {
|
623 | 5f4da8c0 | ths | // try to read from parent image, if exist
|
624 | 5f4da8c0 | ths | if (s->hd->backing_hd) {
|
625 | 5f4da8c0 | ths | if (!vmdk_is_cid_valid(bs))
|
626 | 5f4da8c0 | ths | return -1; |
627 | 5f4da8c0 | ths | ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n); |
628 | 5f4da8c0 | ths | if (ret < 0) |
629 | 5f4da8c0 | ths | return -1; |
630 | 5f4da8c0 | ths | } else {
|
631 | 5f4da8c0 | ths | memset(buf, 0, 512 * n); |
632 | 5f4da8c0 | ths | } |
633 | ea2384d3 | bellard | } else {
|
634 | 5f4da8c0 | ths | if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) |
635 | ea2384d3 | bellard | return -1; |
636 | ea2384d3 | bellard | } |
637 | ea2384d3 | bellard | nb_sectors -= n; |
638 | ea2384d3 | bellard | sector_num += n; |
639 | ea2384d3 | bellard | buf += n * 512;
|
640 | ea2384d3 | bellard | } |
641 | ea2384d3 | bellard | return 0; |
642 | ea2384d3 | bellard | } |
643 | ea2384d3 | bellard | |
644 | 5fafdf24 | ths | static int vmdk_write(BlockDriverState *bs, int64_t sector_num, |
645 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
646 | ea2384d3 | bellard | { |
647 | ff1afc72 | bellard | BDRVVmdkState *s = bs->opaque; |
648 | 630530a6 | ths | VmdkMetaData m_data; |
649 | 5f4da8c0 | ths | int index_in_cluster, n;
|
650 | ff1afc72 | bellard | uint64_t cluster_offset; |
651 | 5f4da8c0 | ths | static int cid_update = 0; |
652 | ff1afc72 | bellard | |
653 | 630530a6 | ths | if (sector_num > bs->total_sectors) {
|
654 | 630530a6 | ths | fprintf(stderr, |
655 | 92868412 | j_mayer | "(VMDK) Wrong offset: sector_num=0x%" PRIx64
|
656 | 92868412 | j_mayer | " total_sectors=0x%" PRIx64 "\n", |
657 | 630530a6 | ths | sector_num, bs->total_sectors); |
658 | 630530a6 | ths | return -1; |
659 | 630530a6 | ths | } |
660 | 630530a6 | ths | |
661 | ff1afc72 | bellard | while (nb_sectors > 0) { |
662 | ff1afc72 | bellard | index_in_cluster = sector_num & (s->cluster_sectors - 1);
|
663 | ff1afc72 | bellard | n = s->cluster_sectors - index_in_cluster; |
664 | ff1afc72 | bellard | if (n > nb_sectors)
|
665 | ff1afc72 | bellard | n = nb_sectors; |
666 | 630530a6 | ths | cluster_offset = get_cluster_offset(bs, &m_data, sector_num << 9, 1); |
667 | ff1afc72 | bellard | if (!cluster_offset)
|
668 | ff1afc72 | bellard | return -1; |
669 | 630530a6 | ths | |
670 | 5f4da8c0 | ths | if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) |
671 | ff1afc72 | bellard | return -1; |
672 | 630530a6 | ths | if (m_data.valid) {
|
673 | 630530a6 | ths | /* update L2 tables */
|
674 | 630530a6 | ths | if (vmdk_L2update(bs, &m_data) == -1) |
675 | 630530a6 | ths | return -1; |
676 | 630530a6 | ths | } |
677 | ff1afc72 | bellard | nb_sectors -= n; |
678 | ff1afc72 | bellard | sector_num += n; |
679 | ff1afc72 | bellard | buf += n * 512;
|
680 | 5f4da8c0 | ths | |
681 | 5f4da8c0 | ths | // update CID on the first write every time the virtual disk is opened
|
682 | 5f4da8c0 | ths | if (!cid_update) {
|
683 | 5f4da8c0 | ths | vmdk_write_cid(bs, time(NULL));
|
684 | 5f4da8c0 | ths | cid_update++; |
685 | 5f4da8c0 | ths | } |
686 | ff1afc72 | bellard | } |
687 | ff1afc72 | bellard | return 0; |
688 | ea2384d3 | bellard | } |
689 | ea2384d3 | bellard | |
690 | 8979b227 | bellard | static int vmdk_create(const char *filename, int64_t total_size, |
691 | 8979b227 | bellard | const char *backing_file, int flags) |
692 | 8979b227 | bellard | { |
693 | 8979b227 | bellard | int fd, i;
|
694 | 8979b227 | bellard | VMDK4Header header; |
695 | 8979b227 | bellard | uint32_t tmp, magic, grains, gd_size, gt_size, gt_count; |
696 | 7ccfb2eb | blueswir1 | static const char desc_template[] = |
697 | 8979b227 | bellard | "# Disk DescriptorFile\n"
|
698 | 8979b227 | bellard | "version=1\n"
|
699 | 8979b227 | bellard | "CID=%x\n"
|
700 | 8979b227 | bellard | "parentCID=ffffffff\n"
|
701 | 8979b227 | bellard | "createType=\"monolithicSparse\"\n"
|
702 | 8979b227 | bellard | "\n"
|
703 | 8979b227 | bellard | "# Extent description\n"
|
704 | 7fd6d9fc | blueswir1 | "RW %" PRId64 " SPARSE \"%s\"\n" |
705 | 8979b227 | bellard | "\n"
|
706 | 8979b227 | bellard | "# The Disk Data Base \n"
|
707 | 8979b227 | bellard | "#DDB\n"
|
708 | 8979b227 | bellard | "\n"
|
709 | ec36ba14 | ths | "ddb.virtualHWVersion = \"%d\"\n"
|
710 | 7fd6d9fc | blueswir1 | "ddb.geometry.cylinders = \"%" PRId64 "\"\n" |
711 | 8979b227 | bellard | "ddb.geometry.heads = \"16\"\n"
|
712 | 8979b227 | bellard | "ddb.geometry.sectors = \"63\"\n"
|
713 | 8979b227 | bellard | "ddb.adapterType = \"ide\"\n";
|
714 | 8979b227 | bellard | char desc[1024]; |
715 | 8979b227 | bellard | const char *real_filename, *temp_str; |
716 | 8979b227 | bellard | |
717 | 8979b227 | bellard | /* XXX: add support for backing file */
|
718 | 5f4da8c0 | ths | if (backing_file) {
|
719 | 5f4da8c0 | ths | return vmdk_snapshot_create(filename, backing_file);
|
720 | 5f4da8c0 | ths | } |
721 | 8979b227 | bellard | |
722 | 8979b227 | bellard | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
723 | 8979b227 | bellard | 0644);
|
724 | 8979b227 | bellard | if (fd < 0) |
725 | 8979b227 | bellard | return -1; |
726 | 8979b227 | bellard | magic = cpu_to_be32(VMDK4_MAGIC); |
727 | 8979b227 | bellard | memset(&header, 0, sizeof(header)); |
728 | 8979b227 | bellard | header.version = cpu_to_le32(1);
|
729 | 8979b227 | bellard | header.flags = cpu_to_le32(3); /* ?? */ |
730 | 8979b227 | bellard | header.capacity = cpu_to_le64(total_size); |
731 | 8979b227 | bellard | header.granularity = cpu_to_le64(128);
|
732 | 8979b227 | bellard | header.num_gtes_per_gte = cpu_to_le32(512);
|
733 | 8979b227 | bellard | |
734 | 8979b227 | bellard | grains = (total_size + header.granularity - 1) / header.granularity;
|
735 | 8979b227 | bellard | gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9; |
736 | 8979b227 | bellard | gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
|
737 | 8979b227 | bellard | gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9; |
738 | 8979b227 | bellard | |
739 | 8979b227 | bellard | header.desc_offset = 1;
|
740 | 8979b227 | bellard | header.desc_size = 20;
|
741 | 8979b227 | bellard | header.rgd_offset = header.desc_offset + header.desc_size; |
742 | 8979b227 | bellard | header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count); |
743 | 8979b227 | bellard | header.grain_offset = |
744 | 8979b227 | bellard | ((header.gd_offset + gd_size + (gt_size * gt_count) + |
745 | 8979b227 | bellard | header.granularity - 1) / header.granularity) *
|
746 | 8979b227 | bellard | header.granularity; |
747 | 8979b227 | bellard | |
748 | 8979b227 | bellard | header.desc_offset = cpu_to_le64(header.desc_offset); |
749 | 8979b227 | bellard | header.desc_size = cpu_to_le64(header.desc_size); |
750 | 8979b227 | bellard | header.rgd_offset = cpu_to_le64(header.rgd_offset); |
751 | 8979b227 | bellard | header.gd_offset = cpu_to_le64(header.gd_offset); |
752 | 8979b227 | bellard | header.grain_offset = cpu_to_le64(header.grain_offset); |
753 | 8979b227 | bellard | |
754 | 8979b227 | bellard | header.check_bytes[0] = 0xa; |
755 | 8979b227 | bellard | header.check_bytes[1] = 0x20; |
756 | 8979b227 | bellard | header.check_bytes[2] = 0xd; |
757 | 8979b227 | bellard | header.check_bytes[3] = 0xa; |
758 | 3b46e624 | ths | |
759 | 3b46e624 | ths | /* write all the data */
|
760 | 8979b227 | bellard | write(fd, &magic, sizeof(magic));
|
761 | 8979b227 | bellard | write(fd, &header, sizeof(header));
|
762 | 8979b227 | bellard | |
763 | 8979b227 | bellard | ftruncate(fd, header.grain_offset << 9);
|
764 | 8979b227 | bellard | |
765 | 8979b227 | bellard | /* write grain directory */
|
766 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
|
767 | 8979b227 | bellard | for (i = 0, tmp = header.rgd_offset + gd_size; |
768 | 8979b227 | bellard | i < gt_count; i++, tmp += gt_size) |
769 | 8979b227 | bellard | write(fd, &tmp, sizeof(tmp));
|
770 | 3b46e624 | ths | |
771 | 8979b227 | bellard | /* write backup grain directory */
|
772 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
|
773 | 8979b227 | bellard | for (i = 0, tmp = header.gd_offset + gd_size; |
774 | 8979b227 | bellard | i < gt_count; i++, tmp += gt_size) |
775 | 8979b227 | bellard | write(fd, &tmp, sizeof(tmp));
|
776 | 8979b227 | bellard | |
777 | 8979b227 | bellard | /* compose the descriptor */
|
778 | 8979b227 | bellard | real_filename = filename; |
779 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, '\\')) != NULL) |
780 | 8979b227 | bellard | real_filename = temp_str + 1;
|
781 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, '/')) != NULL) |
782 | 8979b227 | bellard | real_filename = temp_str + 1;
|
783 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, ':')) != NULL) |
784 | 8979b227 | bellard | real_filename = temp_str + 1;
|
785 | 7ccfb2eb | blueswir1 | snprintf(desc, sizeof(desc), desc_template, (unsigned int)time(NULL), |
786 | 7fd6d9fc | blueswir1 | total_size, real_filename, |
787 | 7fd6d9fc | blueswir1 | (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4), |
788 | 7fd6d9fc | blueswir1 | total_size / (int64_t)(63 * 16)); |
789 | 8979b227 | bellard | |
790 | 8979b227 | bellard | /* write the descriptor */
|
791 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
|
792 | 8979b227 | bellard | write(fd, desc, strlen(desc)); |
793 | 8979b227 | bellard | |
794 | 8979b227 | bellard | close(fd); |
795 | 8979b227 | bellard | return 0; |
796 | 8979b227 | bellard | } |
797 | 8979b227 | bellard | |
798 | e2731add | bellard | static void vmdk_close(BlockDriverState *bs) |
799 | ea2384d3 | bellard | { |
800 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
801 | 5f4da8c0 | ths | |
802 | ea2384d3 | bellard | qemu_free(s->l1_table); |
803 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
804 | 5f4da8c0 | ths | // try to close parent image, if exist
|
805 | 5f4da8c0 | ths | vmdk_parent_close(s->hd); |
806 | 5cbdd273 | ths | bdrv_delete(s->hd); |
807 | ea2384d3 | bellard | } |
808 | ea2384d3 | bellard | |
809 | 7a6cba61 | pbrook | static void vmdk_flush(BlockDriverState *bs) |
810 | 7a6cba61 | pbrook | { |
811 | 7a6cba61 | pbrook | BDRVVmdkState *s = bs->opaque; |
812 | 5f4da8c0 | ths | bdrv_flush(s->hd); |
813 | 7a6cba61 | pbrook | } |
814 | 7a6cba61 | pbrook | |
815 | 5efa9d5a | Anthony Liguori | static BlockDriver bdrv_vmdk = {
|
816 | e60f469c | aurel32 | .format_name = "vmdk",
|
817 | e60f469c | aurel32 | .instance_size = sizeof(BDRVVmdkState),
|
818 | e60f469c | aurel32 | .bdrv_probe = vmdk_probe, |
819 | e60f469c | aurel32 | .bdrv_open = vmdk_open, |
820 | e60f469c | aurel32 | .bdrv_read = vmdk_read, |
821 | e60f469c | aurel32 | .bdrv_write = vmdk_write, |
822 | e60f469c | aurel32 | .bdrv_close = vmdk_close, |
823 | e60f469c | aurel32 | .bdrv_create = vmdk_create, |
824 | e60f469c | aurel32 | .bdrv_flush = vmdk_flush, |
825 | e60f469c | aurel32 | .bdrv_is_allocated = vmdk_is_allocated, |
826 | ea2384d3 | bellard | }; |
827 | 5efa9d5a | Anthony Liguori | |
828 | 5efa9d5a | Anthony Liguori | static void bdrv_vmdk_init(void) |
829 | 5efa9d5a | Anthony Liguori | { |
830 | 5efa9d5a | Anthony Liguori | bdrv_register(&bdrv_vmdk); |
831 | 5efa9d5a | Anthony Liguori | } |
832 | 5efa9d5a | Anthony Liguori | |
833 | 5efa9d5a | Anthony Liguori | block_init(bdrv_vmdk_init); |