root / block / vmdk.c @ 57e69b7d
History | View | Annotate | Download (27.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 | b171271a | Kevin Wolf | BlockDriverState *p_bs = bs->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 | 53c2e716 | Juan Quintela | int ret;
|
191 | 5f4da8c0 | ths | uint32_t p_cid; |
192 | 5fafdf24 | ths | char *p_name, *gd_buf, *rgd_buf;
|
193 | 5f4da8c0 | ths | const char *real_filename, *temp_str; |
194 | 5f4da8c0 | ths | VMDK4Header header; |
195 | 5f4da8c0 | ths | uint32_t gde_entries, gd_size; |
196 | 5f4da8c0 | ths | int64_t gd_offset, rgd_offset, capacity, gt_size; |
197 | 5f4da8c0 | ths | char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
|
198 | 7ccfb2eb | blueswir1 | static const char desc_template[] = |
199 | 5f4da8c0 | ths | "# Disk DescriptorFile\n"
|
200 | 5f4da8c0 | ths | "version=1\n"
|
201 | 5f4da8c0 | ths | "CID=%x\n"
|
202 | 5f4da8c0 | ths | "parentCID=%x\n"
|
203 | 5f4da8c0 | ths | "createType=\"monolithicSparse\"\n"
|
204 | 5f4da8c0 | ths | "parentFileNameHint=\"%s\"\n"
|
205 | 5f4da8c0 | ths | "\n"
|
206 | 5f4da8c0 | ths | "# Extent description\n"
|
207 | 7ccfb2eb | blueswir1 | "RW %u SPARSE \"%s\"\n"
|
208 | 5f4da8c0 | ths | "\n"
|
209 | 5f4da8c0 | ths | "# The Disk Data Base \n"
|
210 | 5f4da8c0 | ths | "#DDB\n"
|
211 | 5f4da8c0 | ths | "\n";
|
212 | 5f4da8c0 | ths | |
213 | 5f4da8c0 | ths | snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
|
214 | 5f4da8c0 | ths | if (snp_fd < 0) |
215 | 53c2e716 | Juan Quintela | return -errno;
|
216 | 5f4da8c0 | ths | p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE); |
217 | 5f4da8c0 | ths | if (p_fd < 0) { |
218 | 5f4da8c0 | ths | close(snp_fd); |
219 | 53c2e716 | Juan Quintela | return -errno;
|
220 | 5f4da8c0 | ths | } |
221 | 5f4da8c0 | ths | |
222 | 5f4da8c0 | ths | /* read the header */
|
223 | 53c2e716 | Juan Quintela | if (lseek(p_fd, 0x0, SEEK_SET) == -1) { |
224 | 53c2e716 | Juan Quintela | ret = -errno; |
225 | 5f4da8c0 | ths | goto fail;
|
226 | 53c2e716 | Juan Quintela | } |
227 | 53c2e716 | Juan Quintela | if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) {
|
228 | 53c2e716 | Juan Quintela | ret = -errno; |
229 | 5f4da8c0 | ths | goto fail;
|
230 | 53c2e716 | Juan Quintela | } |
231 | 5f4da8c0 | ths | |
232 | 5f4da8c0 | ths | /* write the header */
|
233 | 53c2e716 | Juan Quintela | if (lseek(snp_fd, 0x0, SEEK_SET) == -1) { |
234 | 53c2e716 | Juan Quintela | ret = -errno; |
235 | 5f4da8c0 | ths | goto fail;
|
236 | 53c2e716 | Juan Quintela | } |
237 | 53c2e716 | Juan Quintela | if (write(snp_fd, hdr, HEADER_SIZE) == -1) { |
238 | 53c2e716 | Juan Quintela | ret = -errno; |
239 | 5f4da8c0 | ths | goto fail;
|
240 | 53c2e716 | Juan Quintela | } |
241 | 5f4da8c0 | ths | |
242 | 5f4da8c0 | ths | memset(&header, 0, sizeof(header)); |
243 | 5f4da8c0 | ths | memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC |
244 | 5f4da8c0 | ths | |
245 | 53c2e716 | Juan Quintela | if (ftruncate(snp_fd, header.grain_offset << 9)) { |
246 | 53c2e716 | Juan Quintela | ret = -errno; |
247 | 1640366c | Kirill A. Shutemov | goto fail;
|
248 | 53c2e716 | Juan Quintela | } |
249 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
250 | 53c2e716 | Juan Quintela | if (lseek(p_fd, 0x200, SEEK_SET) == -1) { |
251 | 53c2e716 | Juan Quintela | ret = -errno; |
252 | 5f4da8c0 | ths | goto fail;
|
253 | 53c2e716 | Juan Quintela | } |
254 | 53c2e716 | Juan Quintela | if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE) {
|
255 | 53c2e716 | Juan Quintela | ret = -errno; |
256 | 5f4da8c0 | ths | goto fail;
|
257 | 53c2e716 | Juan Quintela | } |
258 | 5f4da8c0 | ths | |
259 | 511d2b14 | blueswir1 | if ((p_name = strstr(p_desc,"CID")) != NULL) { |
260 | 5f4da8c0 | ths | p_name += sizeof("CID"); |
261 | 5f4da8c0 | ths | sscanf(p_name,"%x",&p_cid);
|
262 | 5f4da8c0 | ths | } |
263 | 5f4da8c0 | ths | |
264 | 5f4da8c0 | ths | real_filename = filename; |
265 | 5f4da8c0 | ths | if ((temp_str = strrchr(real_filename, '\\')) != NULL) |
266 | 5f4da8c0 | ths | real_filename = temp_str + 1;
|
267 | 5f4da8c0 | ths | if ((temp_str = strrchr(real_filename, '/')) != NULL) |
268 | 5f4da8c0 | ths | real_filename = temp_str + 1;
|
269 | 5f4da8c0 | ths | if ((temp_str = strrchr(real_filename, ':')) != NULL) |
270 | 5f4da8c0 | ths | real_filename = temp_str + 1;
|
271 | 5f4da8c0 | ths | |
272 | 363a37d5 | blueswir1 | snprintf(s_desc, sizeof(s_desc), desc_template, p_cid, p_cid, backing_file,
|
273 | 363a37d5 | blueswir1 | (uint32_t)header.capacity, real_filename); |
274 | 5f4da8c0 | ths | |
275 | 5f4da8c0 | ths | /* write the descriptor */
|
276 | 53c2e716 | Juan Quintela | if (lseek(snp_fd, 0x200, SEEK_SET) == -1) { |
277 | 53c2e716 | Juan Quintela | ret = -errno; |
278 | 5f4da8c0 | ths | goto fail;
|
279 | 53c2e716 | Juan Quintela | } |
280 | 53c2e716 | Juan Quintela | if (write(snp_fd, s_desc, strlen(s_desc)) == -1) { |
281 | 53c2e716 | Juan Quintela | ret = -errno; |
282 | 5f4da8c0 | ths | goto fail;
|
283 | 53c2e716 | Juan Quintela | } |
284 | ea2384d3 | bellard | |
285 | 5f4da8c0 | ths | gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
|
286 | 5f4da8c0 | ths | rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
|
287 | 5f4da8c0 | ths | capacity = header.capacity * SECTOR_SIZE; // Extent size
|
288 | 5f4da8c0 | ths | /*
|
289 | 5f4da8c0 | ths | * Each GDE span 32M disk, means:
|
290 | 5f4da8c0 | ths | * 512 GTE per GT, each GTE points to grain
|
291 | 5f4da8c0 | ths | */
|
292 | 5f4da8c0 | ths | gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE; |
293 | 53c2e716 | Juan Quintela | if (!gt_size) {
|
294 | 53c2e716 | Juan Quintela | ret = -EINVAL; |
295 | 5f4da8c0 | ths | goto fail;
|
296 | 53c2e716 | Juan Quintela | } |
297 | 5fafdf24 | ths | gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
|
298 | 5f4da8c0 | ths | gd_size = gde_entries * sizeof(uint32_t);
|
299 | 5f4da8c0 | ths | |
300 | 5f4da8c0 | ths | /* write RGD */
|
301 | 5f4da8c0 | ths | rgd_buf = qemu_malloc(gd_size); |
302 | 53c2e716 | Juan Quintela | if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) { |
303 | 53c2e716 | Juan Quintela | ret = -errno; |
304 | 5f4da8c0 | ths | goto fail_rgd;
|
305 | 53c2e716 | Juan Quintela | } |
306 | 53c2e716 | Juan Quintela | if (read(p_fd, rgd_buf, gd_size) != gd_size) {
|
307 | 53c2e716 | Juan Quintela | ret = -errno; |
308 | 5f4da8c0 | ths | goto fail_rgd;
|
309 | 53c2e716 | Juan Quintela | } |
310 | 53c2e716 | Juan Quintela | if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) { |
311 | 53c2e716 | Juan Quintela | ret = -errno; |
312 | 5f4da8c0 | ths | goto fail_rgd;
|
313 | 53c2e716 | Juan Quintela | } |
314 | 53c2e716 | Juan Quintela | if (write(snp_fd, rgd_buf, gd_size) == -1) { |
315 | 53c2e716 | Juan Quintela | ret = -errno; |
316 | 5f4da8c0 | ths | goto fail_rgd;
|
317 | 53c2e716 | Juan Quintela | } |
318 | 5f4da8c0 | ths | |
319 | 5f4da8c0 | ths | /* write GD */
|
320 | 5f4da8c0 | ths | gd_buf = qemu_malloc(gd_size); |
321 | 53c2e716 | Juan Quintela | if (lseek(p_fd, gd_offset, SEEK_SET) == -1) { |
322 | 53c2e716 | Juan Quintela | ret = -errno; |
323 | 5f4da8c0 | ths | goto fail_gd;
|
324 | 53c2e716 | Juan Quintela | } |
325 | 53c2e716 | Juan Quintela | if (read(p_fd, gd_buf, gd_size) != gd_size) {
|
326 | 53c2e716 | Juan Quintela | ret = -errno; |
327 | 5f4da8c0 | ths | goto fail_gd;
|
328 | 53c2e716 | Juan Quintela | } |
329 | 53c2e716 | Juan Quintela | if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) { |
330 | 53c2e716 | Juan Quintela | ret = -errno; |
331 | 5f4da8c0 | ths | goto fail_gd;
|
332 | 53c2e716 | Juan Quintela | } |
333 | 53c2e716 | Juan Quintela | if (write(snp_fd, gd_buf, gd_size) == -1) { |
334 | 53c2e716 | Juan Quintela | ret = -errno; |
335 | 5f4da8c0 | ths | goto fail_gd;
|
336 | 53c2e716 | Juan Quintela | } |
337 | 3829cb46 | Juan Quintela | ret = 0;
|
338 | 5f4da8c0 | ths | |
339 | 3829cb46 | Juan Quintela | fail_gd:
|
340 | 5f4da8c0 | ths | qemu_free(gd_buf); |
341 | 3829cb46 | Juan Quintela | fail_rgd:
|
342 | 5f4da8c0 | ths | qemu_free(rgd_buf); |
343 | 3829cb46 | Juan Quintela | fail:
|
344 | 5f4da8c0 | ths | close(p_fd); |
345 | 5f4da8c0 | ths | close(snp_fd); |
346 | 53c2e716 | Juan Quintela | return ret;
|
347 | 5f4da8c0 | ths | } |
348 | 5f4da8c0 | ths | |
349 | 5f4da8c0 | ths | static void vmdk_parent_close(BlockDriverState *bs) |
350 | 5f4da8c0 | ths | { |
351 | 5f4da8c0 | ths | if (bs->backing_hd)
|
352 | 5f4da8c0 | ths | bdrv_close(bs->backing_hd); |
353 | 5f4da8c0 | ths | } |
354 | 5f4da8c0 | ths | |
355 | b1d8e52e | blueswir1 | static int parent_open = 0; |
356 | 5f4da8c0 | ths | static int vmdk_parent_open(BlockDriverState *bs, const char * filename) |
357 | 5f4da8c0 | ths | { |
358 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
359 | 5fafdf24 | ths | char *p_name;
|
360 | 5f4da8c0 | ths | char desc[DESC_SIZE];
|
361 | 5f4da8c0 | ths | char parent_img_name[1024]; |
362 | 5f4da8c0 | ths | |
363 | 5f4da8c0 | ths | /* the descriptor offset = 0x200 */
|
364 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) |
365 | 5f4da8c0 | ths | return -1; |
366 | 5f4da8c0 | ths | |
367 | 511d2b14 | blueswir1 | if ((p_name = strstr(desc,"parentFileNameHint")) != NULL) { |
368 | 5f4da8c0 | ths | char *end_name;
|
369 | 5f4da8c0 | ths | struct stat file_buf;
|
370 | 5f4da8c0 | ths | |
371 | 5f4da8c0 | ths | p_name += sizeof("parentFileNameHint") + 1; |
372 | 511d2b14 | blueswir1 | if ((end_name = strchr(p_name,'\"')) == NULL) |
373 | 5f4da8c0 | ths | return -1; |
374 | b171271a | Kevin Wolf | if ((end_name - p_name) > sizeof (bs->backing_file) - 1) |
375 | b34d259a | balrog | return -1; |
376 | 3b46e624 | ths | |
377 | b171271a | Kevin Wolf | pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
|
378 | b171271a | Kevin Wolf | if (stat(bs->backing_file, &file_buf) != 0) { |
379 | 5f4da8c0 | ths | path_combine(parent_img_name, sizeof(parent_img_name),
|
380 | b171271a | Kevin Wolf | filename, bs->backing_file); |
381 | 5f4da8c0 | ths | } else {
|
382 | 363a37d5 | blueswir1 | pstrcpy(parent_img_name, sizeof(parent_img_name),
|
383 | b171271a | Kevin Wolf | bs->backing_file); |
384 | 5f4da8c0 | ths | } |
385 | 5f4da8c0 | ths | |
386 | b171271a | Kevin Wolf | bs->backing_hd = bdrv_new("");
|
387 | b171271a | Kevin Wolf | if (!bs->backing_hd) {
|
388 | 5f4da8c0 | ths | failure:
|
389 | 5f4da8c0 | ths | bdrv_close(s->hd); |
390 | ff1afc72 | bellard | return -1; |
391 | 5f4da8c0 | ths | } |
392 | 630530a6 | ths | parent_open = 1;
|
393 | f5edb014 | Naphtali Sprei | if (bdrv_open(bs->backing_hd, parent_img_name, 0) < 0) |
394 | 5f4da8c0 | ths | goto failure;
|
395 | 630530a6 | ths | parent_open = 0;
|
396 | ff1afc72 | bellard | } |
397 | 5f4da8c0 | ths | |
398 | 5f4da8c0 | ths | return 0; |
399 | 5f4da8c0 | ths | } |
400 | 5f4da8c0 | ths | |
401 | 5f4da8c0 | ths | static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) |
402 | 5f4da8c0 | ths | { |
403 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
404 | 5f4da8c0 | ths | uint32_t magic; |
405 | 5f4da8c0 | ths | int l1_size, i, ret;
|
406 | 5f4da8c0 | ths | |
407 | f5edb014 | Naphtali Sprei | if (parent_open) {
|
408 | f5edb014 | Naphtali Sprei | /* Parent must be opened as RO, no RDWR. */
|
409 | f5edb014 | Naphtali Sprei | flags = 0;
|
410 | f5edb014 | Naphtali Sprei | } |
411 | 630530a6 | ths | |
412 | b5eff355 | aurel32 | ret = bdrv_file_open(&s->hd, filename, flags); |
413 | 5f4da8c0 | ths | if (ret < 0) |
414 | 5f4da8c0 | ths | return ret;
|
415 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic)) |
416 | ea2384d3 | bellard | goto fail;
|
417 | 5f4da8c0 | ths | |
418 | 7143c62c | bellard | magic = be32_to_cpu(magic); |
419 | ea2384d3 | bellard | if (magic == VMDK3_MAGIC) {
|
420 | ea2384d3 | bellard | VMDK3Header header; |
421 | 5f4da8c0 | ths | |
422 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) |
423 | ea2384d3 | bellard | goto fail;
|
424 | ea2384d3 | bellard | s->cluster_sectors = le32_to_cpu(header.granularity); |
425 | ea2384d3 | bellard | s->l2_size = 1 << 9; |
426 | ea2384d3 | bellard | s->l1_size = 1 << 6; |
427 | ea2384d3 | bellard | bs->total_sectors = le32_to_cpu(header.disk_sectors); |
428 | ff1afc72 | bellard | s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
|
429 | ff1afc72 | bellard | s->l1_backup_table_offset = 0;
|
430 | ea2384d3 | bellard | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
431 | ea2384d3 | bellard | } else if (magic == VMDK4_MAGIC) { |
432 | ea2384d3 | bellard | VMDK4Header header; |
433 | 5f4da8c0 | ths | |
434 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) |
435 | ea2384d3 | bellard | goto fail;
|
436 | bd6ea3c8 | bellard | bs->total_sectors = le64_to_cpu(header.capacity); |
437 | bd6ea3c8 | bellard | s->cluster_sectors = le64_to_cpu(header.granularity); |
438 | ea2384d3 | bellard | s->l2_size = le32_to_cpu(header.num_gtes_per_gte); |
439 | ea2384d3 | bellard | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
440 | ea2384d3 | bellard | if (s->l1_entry_sectors <= 0) |
441 | ea2384d3 | bellard | goto fail;
|
442 | 5fafdf24 | ths | s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
|
443 | ea2384d3 | bellard | / s->l1_entry_sectors; |
444 | ff1afc72 | bellard | s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
|
445 | ff1afc72 | bellard | s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
|
446 | 5f4da8c0 | ths | |
447 | 630530a6 | ths | if (parent_open)
|
448 | 630530a6 | ths | s->is_parent = 1;
|
449 | 630530a6 | ths | else
|
450 | 630530a6 | ths | s->is_parent = 0;
|
451 | 630530a6 | ths | |
452 | 5f4da8c0 | ths | // try to open parent images, if exist
|
453 | 5f4da8c0 | ths | if (vmdk_parent_open(bs, filename) != 0) |
454 | 5f4da8c0 | ths | goto fail;
|
455 | 5f4da8c0 | ths | // write the CID once after the image creation
|
456 | 5f4da8c0 | ths | s->parent_cid = vmdk_read_cid(bs,1);
|
457 | ea2384d3 | bellard | } else {
|
458 | ea2384d3 | bellard | goto fail;
|
459 | ea2384d3 | bellard | } |
460 | 5f4da8c0 | ths | |
461 | ea2384d3 | bellard | /* read the L1 table */
|
462 | ea2384d3 | bellard | l1_size = s->l1_size * sizeof(uint32_t);
|
463 | ea2384d3 | bellard | s->l1_table = qemu_malloc(l1_size); |
464 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
|
465 | ea2384d3 | bellard | goto fail;
|
466 | ea2384d3 | bellard | for(i = 0; i < s->l1_size; i++) { |
467 | ea2384d3 | bellard | le32_to_cpus(&s->l1_table[i]); |
468 | ea2384d3 | bellard | } |
469 | ea2384d3 | bellard | |
470 | ff1afc72 | bellard | if (s->l1_backup_table_offset) {
|
471 | ff1afc72 | bellard | s->l1_backup_table = qemu_malloc(l1_size); |
472 | 5f4da8c0 | ths | if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
|
473 | ff1afc72 | bellard | goto fail;
|
474 | ff1afc72 | bellard | for(i = 0; i < s->l1_size; i++) { |
475 | ff1afc72 | bellard | le32_to_cpus(&s->l1_backup_table[i]); |
476 | ff1afc72 | bellard | } |
477 | ff1afc72 | bellard | } |
478 | ff1afc72 | bellard | |
479 | ea2384d3 | bellard | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
|
480 | ea2384d3 | bellard | return 0; |
481 | ea2384d3 | bellard | fail:
|
482 | ff1afc72 | bellard | qemu_free(s->l1_backup_table); |
483 | ea2384d3 | bellard | qemu_free(s->l1_table); |
484 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
485 | 5f4da8c0 | ths | bdrv_delete(s->hd); |
486 | ea2384d3 | bellard | return -1; |
487 | ea2384d3 | bellard | } |
488 | ea2384d3 | bellard | |
489 | 630530a6 | ths | static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
|
490 | 630530a6 | ths | uint64_t offset, int allocate);
|
491 | 5f4da8c0 | ths | |
492 | 5f4da8c0 | ths | static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset, |
493 | 5f4da8c0 | ths | uint64_t offset, int allocate)
|
494 | 5f4da8c0 | ths | { |
495 | 5f4da8c0 | ths | uint64_t parent_cluster_offset; |
496 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
497 | 5f4da8c0 | ths | uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB |
498 | 5f4da8c0 | ths | |
499 | 5f4da8c0 | ths | // we will be here if it's first write on non-exist grain(cluster).
|
500 | 5f4da8c0 | ths | // try to read from parent image, if exist
|
501 | b171271a | Kevin Wolf | if (bs->backing_hd) {
|
502 | b171271a | Kevin Wolf | BDRVVmdkState *ps = bs->backing_hd->opaque; |
503 | 5f4da8c0 | ths | |
504 | 5f4da8c0 | ths | if (!vmdk_is_cid_valid(bs))
|
505 | 5f4da8c0 | ths | return -1; |
506 | 5f4da8c0 | ths | |
507 | b171271a | Kevin Wolf | parent_cluster_offset = get_cluster_offset(bs->backing_hd, NULL,
|
508 | b171271a | Kevin Wolf | offset, allocate); |
509 | 630530a6 | ths | |
510 | 630530a6 | ths | if (parent_cluster_offset) {
|
511 | 630530a6 | ths | BDRVVmdkState *act_s = activeBDRV.hd->opaque; |
512 | 630530a6 | ths | |
513 | 630530a6 | ths | if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != ps->cluster_sectors*512) |
514 | 630530a6 | ths | return -1; |
515 | 630530a6 | ths | |
516 | 630530a6 | ths | //Write grain only into the active image
|
517 | 630530a6 | ths | if (bdrv_pwrite(act_s->hd, activeBDRV.cluster_offset << 9, whole_grain, sizeof(whole_grain)) != sizeof(whole_grain)) |
518 | 630530a6 | ths | return -1; |
519 | 630530a6 | ths | } |
520 | 630530a6 | ths | } |
521 | 630530a6 | ths | return 0; |
522 | 630530a6 | ths | } |
523 | 630530a6 | ths | |
524 | 630530a6 | ths | static int vmdk_L2update(BlockDriverState *bs, VmdkMetaData *m_data) |
525 | 630530a6 | ths | { |
526 | 630530a6 | ths | BDRVVmdkState *s = bs->opaque; |
527 | 630530a6 | ths | |
528 | 630530a6 | ths | /* update L2 table */
|
529 | 630530a6 | ths | if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)), |
530 | 630530a6 | ths | &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset)) |
531 | 630530a6 | ths | return -1; |
532 | 630530a6 | ths | /* update backup L2 table */
|
533 | 630530a6 | ths | if (s->l1_backup_table_offset != 0) { |
534 | 630530a6 | ths | m_data->l2_offset = s->l1_backup_table[m_data->l1_index]; |
535 | 630530a6 | ths | if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)), |
536 | 630530a6 | ths | &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset)) |
537 | 5f4da8c0 | ths | return -1; |
538 | 5f4da8c0 | ths | } |
539 | 630530a6 | ths | |
540 | 5f4da8c0 | ths | return 0; |
541 | 5f4da8c0 | ths | } |
542 | 5f4da8c0 | ths | |
543 | 630530a6 | ths | static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
|
544 | ff1afc72 | bellard | uint64_t offset, int allocate)
|
545 | ea2384d3 | bellard | { |
546 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
547 | ea2384d3 | bellard | unsigned int l1_index, l2_offset, l2_index; |
548 | ea2384d3 | bellard | int min_index, i, j;
|
549 | 630530a6 | ths | uint32_t min_count, *l2_table, tmp = 0;
|
550 | ea2384d3 | bellard | uint64_t cluster_offset; |
551 | 630530a6 | ths | |
552 | 630530a6 | ths | if (m_data)
|
553 | 630530a6 | ths | m_data->valid = 0;
|
554 | 630530a6 | ths | |
555 | ea2384d3 | bellard | l1_index = (offset >> 9) / s->l1_entry_sectors;
|
556 | ea2384d3 | bellard | if (l1_index >= s->l1_size)
|
557 | ea2384d3 | bellard | return 0; |
558 | ea2384d3 | bellard | l2_offset = s->l1_table[l1_index]; |
559 | ea2384d3 | bellard | if (!l2_offset)
|
560 | ea2384d3 | bellard | return 0; |
561 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
562 | ea2384d3 | bellard | if (l2_offset == s->l2_cache_offsets[i]) {
|
563 | ea2384d3 | bellard | /* increment the hit count */
|
564 | ea2384d3 | bellard | if (++s->l2_cache_counts[i] == 0xffffffff) { |
565 | ea2384d3 | bellard | for(j = 0; j < L2_CACHE_SIZE; j++) { |
566 | ea2384d3 | bellard | s->l2_cache_counts[j] >>= 1;
|
567 | ea2384d3 | bellard | } |
568 | ea2384d3 | bellard | } |
569 | ea2384d3 | bellard | l2_table = s->l2_cache + (i * s->l2_size); |
570 | ea2384d3 | bellard | goto found;
|
571 | ea2384d3 | bellard | } |
572 | ea2384d3 | bellard | } |
573 | ea2384d3 | bellard | /* not found: load a new entry in the least used one */
|
574 | ea2384d3 | bellard | min_index = 0;
|
575 | ea2384d3 | bellard | min_count = 0xffffffff;
|
576 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
577 | ea2384d3 | bellard | if (s->l2_cache_counts[i] < min_count) {
|
578 | ea2384d3 | bellard | min_count = s->l2_cache_counts[i]; |
579 | ea2384d3 | bellard | min_index = i; |
580 | ea2384d3 | bellard | } |
581 | ea2384d3 | bellard | } |
582 | ea2384d3 | bellard | l2_table = s->l2_cache + (min_index * s->l2_size); |
583 | 5fafdf24 | ths | if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) != |
584 | 5f4da8c0 | ths | s->l2_size * sizeof(uint32_t))
|
585 | ea2384d3 | bellard | return 0; |
586 | 5f4da8c0 | ths | |
587 | ea2384d3 | bellard | s->l2_cache_offsets[min_index] = l2_offset; |
588 | ea2384d3 | bellard | s->l2_cache_counts[min_index] = 1;
|
589 | ea2384d3 | bellard | found:
|
590 | ea2384d3 | bellard | l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
|
591 | ea2384d3 | bellard | cluster_offset = le32_to_cpu(l2_table[l2_index]); |
592 | 630530a6 | ths | |
593 | ff1afc72 | bellard | if (!cluster_offset) {
|
594 | ff1afc72 | bellard | if (!allocate)
|
595 | ff1afc72 | bellard | return 0; |
596 | 630530a6 | ths | // Avoid the L2 tables update for the images that have snapshots.
|
597 | 630530a6 | ths | if (!s->is_parent) {
|
598 | 49140a77 | ths | cluster_offset = bdrv_getlength(s->hd); |
599 | 630530a6 | ths | bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9));
|
600 | 630530a6 | ths | |
601 | 630530a6 | ths | cluster_offset >>= 9;
|
602 | 630530a6 | ths | tmp = cpu_to_le32(cluster_offset); |
603 | 630530a6 | ths | l2_table[l2_index] = tmp; |
604 | 630530a6 | ths | // Save the active image state
|
605 | 630530a6 | ths | activeBDRV.cluster_offset = cluster_offset; |
606 | 630530a6 | ths | activeBDRV.hd = bs; |
607 | ff1afc72 | bellard | } |
608 | 630530a6 | ths | /* First of all we write grain itself, to avoid race condition
|
609 | 630530a6 | ths | * that may to corrupt the image.
|
610 | 630530a6 | ths | * This problem may occur because of insufficient space on host disk
|
611 | 630530a6 | ths | * or inappropriate VM shutdown.
|
612 | 630530a6 | ths | */
|
613 | 5f4da8c0 | ths | if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1) |
614 | 5f4da8c0 | ths | return 0; |
615 | 630530a6 | ths | |
616 | 630530a6 | ths | if (m_data) {
|
617 | 630530a6 | ths | m_data->offset = tmp; |
618 | 630530a6 | ths | m_data->l1_index = l1_index; |
619 | 630530a6 | ths | m_data->l2_index = l2_index; |
620 | 630530a6 | ths | m_data->l2_offset = l2_offset; |
621 | 630530a6 | ths | m_data->valid = 1;
|
622 | 630530a6 | ths | } |
623 | ff1afc72 | bellard | } |
624 | ea2384d3 | bellard | cluster_offset <<= 9;
|
625 | ea2384d3 | bellard | return cluster_offset;
|
626 | ea2384d3 | bellard | } |
627 | ea2384d3 | bellard | |
628 | 5fafdf24 | ths | static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num, |
629 | ea2384d3 | bellard | int nb_sectors, int *pnum) |
630 | ea2384d3 | bellard | { |
631 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
632 | ea2384d3 | bellard | int index_in_cluster, n;
|
633 | ea2384d3 | bellard | uint64_t cluster_offset; |
634 | ea2384d3 | bellard | |
635 | 630530a6 | ths | cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0); |
636 | ea2384d3 | bellard | index_in_cluster = sector_num % s->cluster_sectors; |
637 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
638 | ea2384d3 | bellard | if (n > nb_sectors)
|
639 | ea2384d3 | bellard | n = nb_sectors; |
640 | ea2384d3 | bellard | *pnum = n; |
641 | ea2384d3 | bellard | return (cluster_offset != 0); |
642 | ea2384d3 | bellard | } |
643 | ea2384d3 | bellard | |
644 | 5fafdf24 | ths | static int vmdk_read(BlockDriverState *bs, int64_t sector_num, |
645 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
646 | ea2384d3 | bellard | { |
647 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
648 | 5f4da8c0 | ths | int index_in_cluster, n, ret;
|
649 | ea2384d3 | bellard | uint64_t cluster_offset; |
650 | 5f4da8c0 | ths | |
651 | ea2384d3 | bellard | while (nb_sectors > 0) { |
652 | 630530a6 | ths | cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0); |
653 | ea2384d3 | bellard | index_in_cluster = sector_num % s->cluster_sectors; |
654 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
655 | ea2384d3 | bellard | if (n > nb_sectors)
|
656 | ea2384d3 | bellard | n = nb_sectors; |
657 | ea2384d3 | bellard | if (!cluster_offset) {
|
658 | 5f4da8c0 | ths | // try to read from parent image, if exist
|
659 | b171271a | Kevin Wolf | if (bs->backing_hd) {
|
660 | 5f4da8c0 | ths | if (!vmdk_is_cid_valid(bs))
|
661 | 5f4da8c0 | ths | return -1; |
662 | b171271a | Kevin Wolf | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); |
663 | 5f4da8c0 | ths | if (ret < 0) |
664 | 5f4da8c0 | ths | return -1; |
665 | 5f4da8c0 | ths | } else {
|
666 | 5f4da8c0 | ths | memset(buf, 0, 512 * n); |
667 | 5f4da8c0 | ths | } |
668 | ea2384d3 | bellard | } else {
|
669 | 5f4da8c0 | ths | if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) |
670 | ea2384d3 | bellard | return -1; |
671 | ea2384d3 | bellard | } |
672 | ea2384d3 | bellard | nb_sectors -= n; |
673 | ea2384d3 | bellard | sector_num += n; |
674 | ea2384d3 | bellard | buf += n * 512;
|
675 | ea2384d3 | bellard | } |
676 | ea2384d3 | bellard | return 0; |
677 | ea2384d3 | bellard | } |
678 | ea2384d3 | bellard | |
679 | 5fafdf24 | ths | static int vmdk_write(BlockDriverState *bs, int64_t sector_num, |
680 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
681 | ea2384d3 | bellard | { |
682 | ff1afc72 | bellard | BDRVVmdkState *s = bs->opaque; |
683 | 630530a6 | ths | VmdkMetaData m_data; |
684 | 5f4da8c0 | ths | int index_in_cluster, n;
|
685 | ff1afc72 | bellard | uint64_t cluster_offset; |
686 | 5f4da8c0 | ths | static int cid_update = 0; |
687 | ff1afc72 | bellard | |
688 | 630530a6 | ths | if (sector_num > bs->total_sectors) {
|
689 | 630530a6 | ths | fprintf(stderr, |
690 | 92868412 | j_mayer | "(VMDK) Wrong offset: sector_num=0x%" PRIx64
|
691 | 92868412 | j_mayer | " total_sectors=0x%" PRIx64 "\n", |
692 | 630530a6 | ths | sector_num, bs->total_sectors); |
693 | 630530a6 | ths | return -1; |
694 | 630530a6 | ths | } |
695 | 630530a6 | ths | |
696 | ff1afc72 | bellard | while (nb_sectors > 0) { |
697 | ff1afc72 | bellard | index_in_cluster = sector_num & (s->cluster_sectors - 1);
|
698 | ff1afc72 | bellard | n = s->cluster_sectors - index_in_cluster; |
699 | ff1afc72 | bellard | if (n > nb_sectors)
|
700 | ff1afc72 | bellard | n = nb_sectors; |
701 | 630530a6 | ths | cluster_offset = get_cluster_offset(bs, &m_data, sector_num << 9, 1); |
702 | ff1afc72 | bellard | if (!cluster_offset)
|
703 | ff1afc72 | bellard | return -1; |
704 | 630530a6 | ths | |
705 | 5f4da8c0 | ths | if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) |
706 | ff1afc72 | bellard | return -1; |
707 | 630530a6 | ths | if (m_data.valid) {
|
708 | 630530a6 | ths | /* update L2 tables */
|
709 | 630530a6 | ths | if (vmdk_L2update(bs, &m_data) == -1) |
710 | 630530a6 | ths | return -1; |
711 | 630530a6 | ths | } |
712 | ff1afc72 | bellard | nb_sectors -= n; |
713 | ff1afc72 | bellard | sector_num += n; |
714 | ff1afc72 | bellard | buf += n * 512;
|
715 | 5f4da8c0 | ths | |
716 | 5f4da8c0 | ths | // update CID on the first write every time the virtual disk is opened
|
717 | 5f4da8c0 | ths | if (!cid_update) {
|
718 | 5f4da8c0 | ths | vmdk_write_cid(bs, time(NULL));
|
719 | 5f4da8c0 | ths | cid_update++; |
720 | 5f4da8c0 | ths | } |
721 | ff1afc72 | bellard | } |
722 | ff1afc72 | bellard | return 0; |
723 | ea2384d3 | bellard | } |
724 | ea2384d3 | bellard | |
725 | 0e7e1989 | Kevin Wolf | static int vmdk_create(const char *filename, QEMUOptionParameter *options) |
726 | 8979b227 | bellard | { |
727 | 8979b227 | bellard | int fd, i;
|
728 | 8979b227 | bellard | VMDK4Header header; |
729 | 8979b227 | bellard | uint32_t tmp, magic, grains, gd_size, gt_size, gt_count; |
730 | 7ccfb2eb | blueswir1 | static const char desc_template[] = |
731 | 8979b227 | bellard | "# Disk DescriptorFile\n"
|
732 | 8979b227 | bellard | "version=1\n"
|
733 | 8979b227 | bellard | "CID=%x\n"
|
734 | 8979b227 | bellard | "parentCID=ffffffff\n"
|
735 | 8979b227 | bellard | "createType=\"monolithicSparse\"\n"
|
736 | 8979b227 | bellard | "\n"
|
737 | 8979b227 | bellard | "# Extent description\n"
|
738 | 7fd6d9fc | blueswir1 | "RW %" PRId64 " SPARSE \"%s\"\n" |
739 | 8979b227 | bellard | "\n"
|
740 | 8979b227 | bellard | "# The Disk Data Base \n"
|
741 | 8979b227 | bellard | "#DDB\n"
|
742 | 8979b227 | bellard | "\n"
|
743 | ec36ba14 | ths | "ddb.virtualHWVersion = \"%d\"\n"
|
744 | 7fd6d9fc | blueswir1 | "ddb.geometry.cylinders = \"%" PRId64 "\"\n" |
745 | 8979b227 | bellard | "ddb.geometry.heads = \"16\"\n"
|
746 | 8979b227 | bellard | "ddb.geometry.sectors = \"63\"\n"
|
747 | 8979b227 | bellard | "ddb.adapterType = \"ide\"\n";
|
748 | 8979b227 | bellard | char desc[1024]; |
749 | 8979b227 | bellard | const char *real_filename, *temp_str; |
750 | 0e7e1989 | Kevin Wolf | int64_t total_size = 0;
|
751 | 0e7e1989 | Kevin Wolf | const char *backing_file = NULL; |
752 | 0e7e1989 | Kevin Wolf | int flags = 0; |
753 | 1640366c | Kirill A. Shutemov | int ret;
|
754 | 0e7e1989 | Kevin Wolf | |
755 | 0e7e1989 | Kevin Wolf | // Read out options
|
756 | 0e7e1989 | Kevin Wolf | while (options && options->name) {
|
757 | 0e7e1989 | Kevin Wolf | if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
758 | 0e7e1989 | Kevin Wolf | total_size = options->value.n / 512;
|
759 | 0e7e1989 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { |
760 | 0e7e1989 | Kevin Wolf | backing_file = options->value.s; |
761 | 0e7e1989 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) { |
762 | 0e7e1989 | Kevin Wolf | flags |= options->value.n ? BLOCK_FLAG_COMPAT6: 0;
|
763 | 0e7e1989 | Kevin Wolf | } |
764 | 0e7e1989 | Kevin Wolf | options++; |
765 | 0e7e1989 | Kevin Wolf | } |
766 | 8979b227 | bellard | |
767 | 8979b227 | bellard | /* XXX: add support for backing file */
|
768 | 5f4da8c0 | ths | if (backing_file) {
|
769 | 5f4da8c0 | ths | return vmdk_snapshot_create(filename, backing_file);
|
770 | 5f4da8c0 | ths | } |
771 | 8979b227 | bellard | |
772 | 8979b227 | bellard | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
773 | 8979b227 | bellard | 0644);
|
774 | 8979b227 | bellard | if (fd < 0) |
775 | b781cce5 | Juan Quintela | return -errno;
|
776 | 8979b227 | bellard | magic = cpu_to_be32(VMDK4_MAGIC); |
777 | 8979b227 | bellard | memset(&header, 0, sizeof(header)); |
778 | 8979b227 | bellard | header.version = cpu_to_le32(1);
|
779 | 8979b227 | bellard | header.flags = cpu_to_le32(3); /* ?? */ |
780 | 8979b227 | bellard | header.capacity = cpu_to_le64(total_size); |
781 | 8979b227 | bellard | header.granularity = cpu_to_le64(128);
|
782 | 8979b227 | bellard | header.num_gtes_per_gte = cpu_to_le32(512);
|
783 | 8979b227 | bellard | |
784 | 8979b227 | bellard | grains = (total_size + header.granularity - 1) / header.granularity;
|
785 | 8979b227 | bellard | gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9; |
786 | 8979b227 | bellard | gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
|
787 | 8979b227 | bellard | gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9; |
788 | 8979b227 | bellard | |
789 | 8979b227 | bellard | header.desc_offset = 1;
|
790 | 8979b227 | bellard | header.desc_size = 20;
|
791 | 8979b227 | bellard | header.rgd_offset = header.desc_offset + header.desc_size; |
792 | 8979b227 | bellard | header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count); |
793 | 8979b227 | bellard | header.grain_offset = |
794 | 8979b227 | bellard | ((header.gd_offset + gd_size + (gt_size * gt_count) + |
795 | 8979b227 | bellard | header.granularity - 1) / header.granularity) *
|
796 | 8979b227 | bellard | header.granularity; |
797 | 8979b227 | bellard | |
798 | 8979b227 | bellard | header.desc_offset = cpu_to_le64(header.desc_offset); |
799 | 8979b227 | bellard | header.desc_size = cpu_to_le64(header.desc_size); |
800 | 8979b227 | bellard | header.rgd_offset = cpu_to_le64(header.rgd_offset); |
801 | 8979b227 | bellard | header.gd_offset = cpu_to_le64(header.gd_offset); |
802 | 8979b227 | bellard | header.grain_offset = cpu_to_le64(header.grain_offset); |
803 | 8979b227 | bellard | |
804 | 8979b227 | bellard | header.check_bytes[0] = 0xa; |
805 | 8979b227 | bellard | header.check_bytes[1] = 0x20; |
806 | 8979b227 | bellard | header.check_bytes[2] = 0xd; |
807 | 8979b227 | bellard | header.check_bytes[3] = 0xa; |
808 | 3b46e624 | ths | |
809 | 3b46e624 | ths | /* write all the data */
|
810 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, &magic, sizeof(magic));
|
811 | 1640366c | Kirill A. Shutemov | if (ret != sizeof(magic)) { |
812 | b781cce5 | Juan Quintela | ret = -errno; |
813 | 1640366c | Kirill A. Shutemov | goto exit;
|
814 | 1640366c | Kirill A. Shutemov | } |
815 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, &header, sizeof(header));
|
816 | 1640366c | Kirill A. Shutemov | if (ret != sizeof(header)) { |
817 | b781cce5 | Juan Quintela | ret = -errno; |
818 | 1640366c | Kirill A. Shutemov | goto exit;
|
819 | 1640366c | Kirill A. Shutemov | } |
820 | 8979b227 | bellard | |
821 | 1640366c | Kirill A. Shutemov | ret = ftruncate(fd, header.grain_offset << 9);
|
822 | 1640366c | Kirill A. Shutemov | if (ret < 0) { |
823 | b781cce5 | Juan Quintela | ret = -errno; |
824 | 1640366c | Kirill A. Shutemov | goto exit;
|
825 | 1640366c | Kirill A. Shutemov | } |
826 | 8979b227 | bellard | |
827 | 8979b227 | bellard | /* write grain directory */
|
828 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
|
829 | 8979b227 | bellard | for (i = 0, tmp = header.rgd_offset + gd_size; |
830 | 1640366c | Kirill A. Shutemov | i < gt_count; i++, tmp += gt_size) { |
831 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, &tmp, sizeof(tmp));
|
832 | 1640366c | Kirill A. Shutemov | if (ret != sizeof(tmp)) { |
833 | b781cce5 | Juan Quintela | ret = -errno; |
834 | 1640366c | Kirill A. Shutemov | goto exit;
|
835 | 1640366c | Kirill A. Shutemov | } |
836 | 1640366c | Kirill A. Shutemov | } |
837 | 3b46e624 | ths | |
838 | 8979b227 | bellard | /* write backup grain directory */
|
839 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
|
840 | 8979b227 | bellard | for (i = 0, tmp = header.gd_offset + gd_size; |
841 | 1640366c | Kirill A. Shutemov | i < gt_count; i++, tmp += gt_size) { |
842 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, &tmp, sizeof(tmp));
|
843 | 1640366c | Kirill A. Shutemov | if (ret != sizeof(tmp)) { |
844 | b781cce5 | Juan Quintela | ret = -errno; |
845 | 1640366c | Kirill A. Shutemov | goto exit;
|
846 | 1640366c | Kirill A. Shutemov | } |
847 | 1640366c | Kirill A. Shutemov | } |
848 | 8979b227 | bellard | |
849 | 8979b227 | bellard | /* compose the descriptor */
|
850 | 8979b227 | bellard | real_filename = filename; |
851 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, '\\')) != NULL) |
852 | 8979b227 | bellard | real_filename = temp_str + 1;
|
853 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, '/')) != NULL) |
854 | 8979b227 | bellard | real_filename = temp_str + 1;
|
855 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, ':')) != NULL) |
856 | 8979b227 | bellard | real_filename = temp_str + 1;
|
857 | 7ccfb2eb | blueswir1 | snprintf(desc, sizeof(desc), desc_template, (unsigned int)time(NULL), |
858 | 7fd6d9fc | blueswir1 | total_size, real_filename, |
859 | 7fd6d9fc | blueswir1 | (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4), |
860 | 7fd6d9fc | blueswir1 | total_size / (int64_t)(63 * 16)); |
861 | 8979b227 | bellard | |
862 | 8979b227 | bellard | /* write the descriptor */
|
863 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
|
864 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, desc, strlen(desc)); |
865 | 1640366c | Kirill A. Shutemov | if (ret != strlen(desc)) {
|
866 | b781cce5 | Juan Quintela | ret = -errno; |
867 | 1640366c | Kirill A. Shutemov | goto exit;
|
868 | 1640366c | Kirill A. Shutemov | } |
869 | 8979b227 | bellard | |
870 | 1640366c | Kirill A. Shutemov | ret = 0;
|
871 | 1640366c | Kirill A. Shutemov | exit:
|
872 | 8979b227 | bellard | close(fd); |
873 | 1640366c | Kirill A. Shutemov | return ret;
|
874 | 8979b227 | bellard | } |
875 | 8979b227 | bellard | |
876 | e2731add | bellard | static void vmdk_close(BlockDriverState *bs) |
877 | ea2384d3 | bellard | { |
878 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
879 | 5f4da8c0 | ths | |
880 | ea2384d3 | bellard | qemu_free(s->l1_table); |
881 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
882 | 5f4da8c0 | ths | // try to close parent image, if exist
|
883 | 5f4da8c0 | ths | vmdk_parent_close(s->hd); |
884 | 5cbdd273 | ths | bdrv_delete(s->hd); |
885 | ea2384d3 | bellard | } |
886 | ea2384d3 | bellard | |
887 | 7a6cba61 | pbrook | static void vmdk_flush(BlockDriverState *bs) |
888 | 7a6cba61 | pbrook | { |
889 | 7a6cba61 | pbrook | BDRVVmdkState *s = bs->opaque; |
890 | 5f4da8c0 | ths | bdrv_flush(s->hd); |
891 | 7a6cba61 | pbrook | } |
892 | 7a6cba61 | pbrook | |
893 | 0e7e1989 | Kevin Wolf | |
894 | 0e7e1989 | Kevin Wolf | static QEMUOptionParameter vmdk_create_options[] = {
|
895 | db08adf5 | Kevin Wolf | { |
896 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_SIZE, |
897 | db08adf5 | Kevin Wolf | .type = OPT_SIZE, |
898 | db08adf5 | Kevin Wolf | .help = "Virtual disk size"
|
899 | db08adf5 | Kevin Wolf | }, |
900 | db08adf5 | Kevin Wolf | { |
901 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_BACKING_FILE, |
902 | db08adf5 | Kevin Wolf | .type = OPT_STRING, |
903 | db08adf5 | Kevin Wolf | .help = "File name of a base image"
|
904 | db08adf5 | Kevin Wolf | }, |
905 | db08adf5 | Kevin Wolf | { |
906 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_COMPAT6, |
907 | db08adf5 | Kevin Wolf | .type = OPT_FLAG, |
908 | db08adf5 | Kevin Wolf | .help = "VMDK version 6 image"
|
909 | db08adf5 | Kevin Wolf | }, |
910 | 0e7e1989 | Kevin Wolf | { NULL }
|
911 | 0e7e1989 | Kevin Wolf | }; |
912 | 0e7e1989 | Kevin Wolf | |
913 | 5efa9d5a | Anthony Liguori | static BlockDriver bdrv_vmdk = {
|
914 | e60f469c | aurel32 | .format_name = "vmdk",
|
915 | e60f469c | aurel32 | .instance_size = sizeof(BDRVVmdkState),
|
916 | e60f469c | aurel32 | .bdrv_probe = vmdk_probe, |
917 | e60f469c | aurel32 | .bdrv_open = vmdk_open, |
918 | e60f469c | aurel32 | .bdrv_read = vmdk_read, |
919 | e60f469c | aurel32 | .bdrv_write = vmdk_write, |
920 | e60f469c | aurel32 | .bdrv_close = vmdk_close, |
921 | e60f469c | aurel32 | .bdrv_create = vmdk_create, |
922 | e60f469c | aurel32 | .bdrv_flush = vmdk_flush, |
923 | e60f469c | aurel32 | .bdrv_is_allocated = vmdk_is_allocated, |
924 | 0e7e1989 | Kevin Wolf | |
925 | 0e7e1989 | Kevin Wolf | .create_options = vmdk_create_options, |
926 | ea2384d3 | bellard | }; |
927 | 5efa9d5a | Anthony Liguori | |
928 | 5efa9d5a | Anthony Liguori | static void bdrv_vmdk_init(void) |
929 | 5efa9d5a | Anthony Liguori | { |
930 | 5efa9d5a | Anthony Liguori | bdrv_register(&bdrv_vmdk); |
931 | 5efa9d5a | Anthony Liguori | } |
932 | 5efa9d5a | Anthony Liguori | |
933 | 5efa9d5a | Anthony Liguori | block_init(bdrv_vmdk_init); |