root / block / vmdk.c @ 34b5d2c6
History | View | Annotate | Download (55.2 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 | 737e150e | Paolo Bonzini | #include "block/block_int.h" |
28 | 1de7afc9 | Paolo Bonzini | #include "qemu/module.h" |
29 | caf71f86 | Paolo Bonzini | #include "migration/migration.h" |
30 | 2923d34f | Stefan Weil | #include <zlib.h> |
31 | ea2384d3 | bellard | |
32 | ea2384d3 | bellard | #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D') |
33 | ea2384d3 | bellard | #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V') |
34 | 432bb170 | Fam Zheng | #define VMDK4_COMPRESSION_DEFLATE 1 |
35 | 95b0aa42 | Fam Zheng | #define VMDK4_FLAG_NL_DETECT (1 << 0) |
36 | bb45ded9 | Fam Zheng | #define VMDK4_FLAG_RGD (1 << 1) |
37 | 14ead646 | Fam Zheng | /* Zeroed-grain enable bit */
|
38 | 14ead646 | Fam Zheng | #define VMDK4_FLAG_ZERO_GRAIN (1 << 2) |
39 | 432bb170 | Fam Zheng | #define VMDK4_FLAG_COMPRESS (1 << 16) |
40 | 432bb170 | Fam Zheng | #define VMDK4_FLAG_MARKER (1 << 17) |
41 | 65bd155c | Kevin Wolf | #define VMDK4_GD_AT_END 0xffffffffffffffffULL |
42 | ea2384d3 | bellard | |
43 | 14ead646 | Fam Zheng | #define VMDK_GTE_ZEROED 0x1 |
44 | 65f74725 | Fam Zheng | |
45 | 65f74725 | Fam Zheng | /* VMDK internal error codes */
|
46 | 65f74725 | Fam Zheng | #define VMDK_OK 0 |
47 | 65f74725 | Fam Zheng | #define VMDK_ERROR (-1) |
48 | 65f74725 | Fam Zheng | /* Cluster not allocated */
|
49 | 65f74725 | Fam Zheng | #define VMDK_UNALLOC (-2) |
50 | 65f74725 | Fam Zheng | #define VMDK_ZEROED (-3) |
51 | 65f74725 | Fam Zheng | |
52 | 69e0b6df | Fam Zheng | #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain" |
53 | 69e0b6df | Fam Zheng | |
54 | ea2384d3 | bellard | typedef struct { |
55 | ea2384d3 | bellard | uint32_t version; |
56 | ea2384d3 | bellard | uint32_t flags; |
57 | ea2384d3 | bellard | uint32_t disk_sectors; |
58 | ea2384d3 | bellard | uint32_t granularity; |
59 | ea2384d3 | bellard | uint32_t l1dir_offset; |
60 | ea2384d3 | bellard | uint32_t l1dir_size; |
61 | ea2384d3 | bellard | uint32_t file_sectors; |
62 | ea2384d3 | bellard | uint32_t cylinders; |
63 | ea2384d3 | bellard | uint32_t heads; |
64 | ea2384d3 | bellard | uint32_t sectors_per_track; |
65 | 5d8caa54 | Fam Zheng | } QEMU_PACKED VMDK3Header; |
66 | ea2384d3 | bellard | |
67 | ea2384d3 | bellard | typedef struct { |
68 | ea2384d3 | bellard | uint32_t version; |
69 | ea2384d3 | bellard | uint32_t flags; |
70 | e98768d4 | Fam Zheng | uint64_t capacity; |
71 | e98768d4 | Fam Zheng | uint64_t granularity; |
72 | e98768d4 | Fam Zheng | uint64_t desc_offset; |
73 | e98768d4 | Fam Zheng | uint64_t desc_size; |
74 | ca8804ce | Fam Zheng | /* Number of GrainTableEntries per GrainTable */
|
75 | ca8804ce | Fam Zheng | uint32_t num_gtes_per_gt; |
76 | e98768d4 | Fam Zheng | uint64_t rgd_offset; |
77 | e98768d4 | Fam Zheng | uint64_t gd_offset; |
78 | e98768d4 | Fam Zheng | uint64_t grain_offset; |
79 | ea2384d3 | bellard | char filler[1]; |
80 | ea2384d3 | bellard | char check_bytes[4]; |
81 | 432bb170 | Fam Zheng | uint16_t compressAlgorithm; |
82 | 541dc0d4 | Stefan Weil | } QEMU_PACKED VMDK4Header; |
83 | ea2384d3 | bellard | |
84 | ea2384d3 | bellard | #define L2_CACHE_SIZE 16 |
85 | ea2384d3 | bellard | |
86 | b3976d3c | Fam Zheng | typedef struct VmdkExtent { |
87 | b3976d3c | Fam Zheng | BlockDriverState *file; |
88 | b3976d3c | Fam Zheng | bool flat;
|
89 | 432bb170 | Fam Zheng | bool compressed;
|
90 | 432bb170 | Fam Zheng | bool has_marker;
|
91 | 14ead646 | Fam Zheng | bool has_zero_grain;
|
92 | 14ead646 | Fam Zheng | int version;
|
93 | b3976d3c | Fam Zheng | int64_t sectors; |
94 | b3976d3c | Fam Zheng | int64_t end_sector; |
95 | 7fa60fa3 | Fam Zheng | int64_t flat_start_offset; |
96 | ea2384d3 | bellard | int64_t l1_table_offset; |
97 | ff1afc72 | bellard | int64_t l1_backup_table_offset; |
98 | ea2384d3 | bellard | uint32_t *l1_table; |
99 | ff1afc72 | bellard | uint32_t *l1_backup_table; |
100 | ea2384d3 | bellard | unsigned int l1_size; |
101 | ea2384d3 | bellard | uint32_t l1_entry_sectors; |
102 | ea2384d3 | bellard | |
103 | ea2384d3 | bellard | unsigned int l2_size; |
104 | ea2384d3 | bellard | uint32_t *l2_cache; |
105 | ea2384d3 | bellard | uint32_t l2_cache_offsets[L2_CACHE_SIZE]; |
106 | ea2384d3 | bellard | uint32_t l2_cache_counts[L2_CACHE_SIZE]; |
107 | ea2384d3 | bellard | |
108 | ea2384d3 | bellard | unsigned int cluster_sectors; |
109 | b3976d3c | Fam Zheng | } VmdkExtent; |
110 | b3976d3c | Fam Zheng | |
111 | b3976d3c | Fam Zheng | typedef struct BDRVVmdkState { |
112 | 848c66e8 | Paolo Bonzini | CoMutex lock; |
113 | e98768d4 | Fam Zheng | uint64_t desc_offset; |
114 | 69b4d86d | Fam Zheng | bool cid_updated;
|
115 | 5f4da8c0 | ths | uint32_t parent_cid; |
116 | b3976d3c | Fam Zheng | int num_extents;
|
117 | b3976d3c | Fam Zheng | /* Extent array with num_extents entries, ascend ordered by address */
|
118 | b3976d3c | Fam Zheng | VmdkExtent *extents; |
119 | 2bc3166c | Kevin Wolf | Error *migration_blocker; |
120 | ea2384d3 | bellard | } BDRVVmdkState; |
121 | ea2384d3 | bellard | |
122 | 630530a6 | ths | typedef struct VmdkMetaData { |
123 | 630530a6 | ths | uint32_t offset; |
124 | 630530a6 | ths | unsigned int l1_index; |
125 | 630530a6 | ths | unsigned int l2_index; |
126 | 630530a6 | ths | unsigned int l2_offset; |
127 | 630530a6 | ths | int valid;
|
128 | cdeaf1f1 | Fam Zheng | uint32_t *l2_cache_entry; |
129 | 630530a6 | ths | } VmdkMetaData; |
130 | 630530a6 | ths | |
131 | 432bb170 | Fam Zheng | typedef struct VmdkGrainMarker { |
132 | 432bb170 | Fam Zheng | uint64_t lba; |
133 | 432bb170 | Fam Zheng | uint32_t size; |
134 | 432bb170 | Fam Zheng | uint8_t data[0];
|
135 | 5d8caa54 | Fam Zheng | } QEMU_PACKED VmdkGrainMarker; |
136 | 432bb170 | Fam Zheng | |
137 | 65bd155c | Kevin Wolf | enum {
|
138 | 65bd155c | Kevin Wolf | MARKER_END_OF_STREAM = 0,
|
139 | 65bd155c | Kevin Wolf | MARKER_GRAIN_TABLE = 1,
|
140 | 65bd155c | Kevin Wolf | MARKER_GRAIN_DIRECTORY = 2,
|
141 | 65bd155c | Kevin Wolf | MARKER_FOOTER = 3,
|
142 | 65bd155c | Kevin Wolf | }; |
143 | 65bd155c | Kevin Wolf | |
144 | ea2384d3 | bellard | static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) |
145 | ea2384d3 | bellard | { |
146 | ea2384d3 | bellard | uint32_t magic; |
147 | ea2384d3 | bellard | |
148 | ae261c86 | Fam Zheng | if (buf_size < 4) { |
149 | ea2384d3 | bellard | return 0; |
150 | ae261c86 | Fam Zheng | } |
151 | ea2384d3 | bellard | magic = be32_to_cpu(*(uint32_t *)buf); |
152 | ea2384d3 | bellard | if (magic == VMDK3_MAGIC ||
|
153 | 01fc99d6 | Fam Zheng | magic == VMDK4_MAGIC) { |
154 | ea2384d3 | bellard | return 100; |
155 | 01fc99d6 | Fam Zheng | } else {
|
156 | 01fc99d6 | Fam Zheng | const char *p = (const char *)buf; |
157 | 01fc99d6 | Fam Zheng | const char *end = p + buf_size; |
158 | 01fc99d6 | Fam Zheng | while (p < end) {
|
159 | 01fc99d6 | Fam Zheng | if (*p == '#') { |
160 | 01fc99d6 | Fam Zheng | /* skip comment line */
|
161 | 01fc99d6 | Fam Zheng | while (p < end && *p != '\n') { |
162 | 01fc99d6 | Fam Zheng | p++; |
163 | 01fc99d6 | Fam Zheng | } |
164 | 01fc99d6 | Fam Zheng | p++; |
165 | 01fc99d6 | Fam Zheng | continue;
|
166 | 01fc99d6 | Fam Zheng | } |
167 | 01fc99d6 | Fam Zheng | if (*p == ' ') { |
168 | 01fc99d6 | Fam Zheng | while (p < end && *p == ' ') { |
169 | 01fc99d6 | Fam Zheng | p++; |
170 | 01fc99d6 | Fam Zheng | } |
171 | 01fc99d6 | Fam Zheng | /* skip '\r' if windows line endings used. */
|
172 | 01fc99d6 | Fam Zheng | if (p < end && *p == '\r') { |
173 | 01fc99d6 | Fam Zheng | p++; |
174 | 01fc99d6 | Fam Zheng | } |
175 | 01fc99d6 | Fam Zheng | /* only accept blank lines before 'version=' line */
|
176 | 01fc99d6 | Fam Zheng | if (p == end || *p != '\n') { |
177 | 01fc99d6 | Fam Zheng | return 0; |
178 | 01fc99d6 | Fam Zheng | } |
179 | 01fc99d6 | Fam Zheng | p++; |
180 | 01fc99d6 | Fam Zheng | continue;
|
181 | 01fc99d6 | Fam Zheng | } |
182 | 01fc99d6 | Fam Zheng | if (end - p >= strlen("version=X\n")) { |
183 | 01fc99d6 | Fam Zheng | if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 || |
184 | 01fc99d6 | Fam Zheng | strncmp("version=2\n", p, strlen("version=2\n")) == 0) { |
185 | 01fc99d6 | Fam Zheng | return 100; |
186 | 01fc99d6 | Fam Zheng | } |
187 | 01fc99d6 | Fam Zheng | } |
188 | 01fc99d6 | Fam Zheng | if (end - p >= strlen("version=X\r\n")) { |
189 | 01fc99d6 | Fam Zheng | if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 || |
190 | 01fc99d6 | Fam Zheng | strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) { |
191 | 01fc99d6 | Fam Zheng | return 100; |
192 | 01fc99d6 | Fam Zheng | } |
193 | 01fc99d6 | Fam Zheng | } |
194 | 01fc99d6 | Fam Zheng | return 0; |
195 | 01fc99d6 | Fam Zheng | } |
196 | ea2384d3 | bellard | return 0; |
197 | 01fc99d6 | Fam Zheng | } |
198 | ea2384d3 | bellard | } |
199 | ea2384d3 | bellard | |
200 | 5f4da8c0 | ths | #define CHECK_CID 1 |
201 | 5f4da8c0 | ths | |
202 | 3b46e624 | ths | #define SECTOR_SIZE 512 |
203 | f66fd6c3 | Fam Zheng | #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */ |
204 | f66fd6c3 | Fam Zheng | #define BUF_SIZE 4096 |
205 | f66fd6c3 | Fam Zheng | #define HEADER_SIZE 512 /* first sector of 512 bytes */ |
206 | 5f4da8c0 | ths | |
207 | b3976d3c | Fam Zheng | static void vmdk_free_extents(BlockDriverState *bs) |
208 | b3976d3c | Fam Zheng | { |
209 | b3976d3c | Fam Zheng | int i;
|
210 | b3976d3c | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
211 | b3c0bfb6 | Fam Zheng | VmdkExtent *e; |
212 | b3976d3c | Fam Zheng | |
213 | b3976d3c | Fam Zheng | for (i = 0; i < s->num_extents; i++) { |
214 | b3c0bfb6 | Fam Zheng | e = &s->extents[i]; |
215 | b3c0bfb6 | Fam Zheng | g_free(e->l1_table); |
216 | b3c0bfb6 | Fam Zheng | g_free(e->l2_cache); |
217 | b3c0bfb6 | Fam Zheng | g_free(e->l1_backup_table); |
218 | b3c0bfb6 | Fam Zheng | if (e->file != bs->file) {
|
219 | 4f6fd349 | Fam Zheng | bdrv_unref(e->file); |
220 | b3c0bfb6 | Fam Zheng | } |
221 | b3976d3c | Fam Zheng | } |
222 | 7267c094 | Anthony Liguori | g_free(s->extents); |
223 | b3976d3c | Fam Zheng | } |
224 | b3976d3c | Fam Zheng | |
225 | 86c6b429 | Fam Zheng | static void vmdk_free_last_extent(BlockDriverState *bs) |
226 | 86c6b429 | Fam Zheng | { |
227 | 86c6b429 | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
228 | 86c6b429 | Fam Zheng | |
229 | 86c6b429 | Fam Zheng | if (s->num_extents == 0) { |
230 | 86c6b429 | Fam Zheng | return;
|
231 | 86c6b429 | Fam Zheng | } |
232 | 86c6b429 | Fam Zheng | s->num_extents--; |
233 | 86c6b429 | Fam Zheng | s->extents = g_realloc(s->extents, s->num_extents * sizeof(VmdkExtent));
|
234 | 86c6b429 | Fam Zheng | } |
235 | 86c6b429 | Fam Zheng | |
236 | 5f4da8c0 | ths | static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) |
237 | ea2384d3 | bellard | { |
238 | 5f4da8c0 | ths | char desc[DESC_SIZE];
|
239 | 8379e46d | Pavel Borzenkov | uint32_t cid = 0xffffffff;
|
240 | 7ccfb2eb | blueswir1 | const char *p_name, *cid_str; |
241 | 5f4da8c0 | ths | size_t cid_str_size; |
242 | e1da9b24 | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
243 | 99f1835d | Kevin Wolf | int ret;
|
244 | 5f4da8c0 | ths | |
245 | 99f1835d | Kevin Wolf | ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE); |
246 | 99f1835d | Kevin Wolf | if (ret < 0) { |
247 | 5f4da8c0 | ths | return 0; |
248 | e1da9b24 | Fam Zheng | } |
249 | 5f4da8c0 | ths | |
250 | 5f4da8c0 | ths | if (parent) {
|
251 | 5f4da8c0 | ths | cid_str = "parentCID";
|
252 | 5f4da8c0 | ths | cid_str_size = sizeof("parentCID"); |
253 | 5f4da8c0 | ths | } else {
|
254 | 5f4da8c0 | ths | cid_str = "CID";
|
255 | 5f4da8c0 | ths | cid_str_size = sizeof("CID"); |
256 | 5f4da8c0 | ths | } |
257 | 5f4da8c0 | ths | |
258 | 93897b9f | Kevin Wolf | desc[DESC_SIZE - 1] = '\0'; |
259 | ae261c86 | Fam Zheng | p_name = strstr(desc, cid_str); |
260 | ae261c86 | Fam Zheng | if (p_name != NULL) { |
261 | 5f4da8c0 | ths | p_name += cid_str_size; |
262 | ae261c86 | Fam Zheng | sscanf(p_name, "%x", &cid);
|
263 | 5f4da8c0 | ths | } |
264 | 5f4da8c0 | ths | |
265 | 5f4da8c0 | ths | return cid;
|
266 | 5f4da8c0 | ths | } |
267 | 5f4da8c0 | ths | |
268 | 5f4da8c0 | ths | static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid) |
269 | 5f4da8c0 | ths | { |
270 | 5f4da8c0 | ths | char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
|
271 | 5f4da8c0 | ths | char *p_name, *tmp_str;
|
272 | e1da9b24 | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
273 | 99f1835d | Kevin Wolf | int ret;
|
274 | 5f4da8c0 | ths | |
275 | 99f1835d | Kevin Wolf | ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE); |
276 | 99f1835d | Kevin Wolf | if (ret < 0) { |
277 | 99f1835d | Kevin Wolf | return ret;
|
278 | e1da9b24 | Fam Zheng | } |
279 | 5f4da8c0 | ths | |
280 | 93897b9f | Kevin Wolf | desc[DESC_SIZE - 1] = '\0'; |
281 | ae261c86 | Fam Zheng | tmp_str = strstr(desc, "parentCID");
|
282 | 93897b9f | Kevin Wolf | if (tmp_str == NULL) { |
283 | 93897b9f | Kevin Wolf | return -EINVAL;
|
284 | 93897b9f | Kevin Wolf | } |
285 | 93897b9f | Kevin Wolf | |
286 | 363a37d5 | blueswir1 | pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
|
287 | ae261c86 | Fam Zheng | p_name = strstr(desc, "CID");
|
288 | ae261c86 | Fam Zheng | if (p_name != NULL) { |
289 | 5f4da8c0 | ths | p_name += sizeof("CID"); |
290 | 363a37d5 | blueswir1 | snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid); |
291 | 363a37d5 | blueswir1 | pstrcat(desc, sizeof(desc), tmp_desc);
|
292 | 5f4da8c0 | ths | } |
293 | 5f4da8c0 | ths | |
294 | 99f1835d | Kevin Wolf | ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE); |
295 | 99f1835d | Kevin Wolf | if (ret < 0) { |
296 | 99f1835d | Kevin Wolf | return ret;
|
297 | e1da9b24 | Fam Zheng | } |
298 | 99f1835d | Kevin Wolf | |
299 | 5f4da8c0 | ths | return 0; |
300 | 5f4da8c0 | ths | } |
301 | 5f4da8c0 | ths | |
302 | 5f4da8c0 | ths | static int vmdk_is_cid_valid(BlockDriverState *bs) |
303 | 5f4da8c0 | ths | { |
304 | 5f4da8c0 | ths | #ifdef CHECK_CID
|
305 | 5f4da8c0 | ths | BDRVVmdkState *s = bs->opaque; |
306 | b171271a | Kevin Wolf | BlockDriverState *p_bs = bs->backing_hd; |
307 | 5f4da8c0 | ths | uint32_t cur_pcid; |
308 | 5f4da8c0 | ths | |
309 | 5f4da8c0 | ths | if (p_bs) {
|
310 | ae261c86 | Fam Zheng | cur_pcid = vmdk_read_cid(p_bs, 0);
|
311 | ae261c86 | Fam Zheng | if (s->parent_cid != cur_pcid) {
|
312 | ae261c86 | Fam Zheng | /* CID not valid */
|
313 | 5f4da8c0 | ths | return 0; |
314 | ae261c86 | Fam Zheng | } |
315 | 5f4da8c0 | ths | } |
316 | 5f4da8c0 | ths | #endif
|
317 | ae261c86 | Fam Zheng | /* CID valid */
|
318 | 5f4da8c0 | ths | return 1; |
319 | 5f4da8c0 | ths | } |
320 | 5f4da8c0 | ths | |
321 | 3897575f | Jeff Cody | /* Queue extents, if any, for reopen() */
|
322 | 3897575f | Jeff Cody | static int vmdk_reopen_prepare(BDRVReopenState *state, |
323 | 3897575f | Jeff Cody | BlockReopenQueue *queue, Error **errp) |
324 | 3897575f | Jeff Cody | { |
325 | 3897575f | Jeff Cody | BDRVVmdkState *s; |
326 | 3897575f | Jeff Cody | int ret = -1; |
327 | 3897575f | Jeff Cody | int i;
|
328 | 3897575f | Jeff Cody | VmdkExtent *e; |
329 | 3897575f | Jeff Cody | |
330 | 3897575f | Jeff Cody | assert(state != NULL);
|
331 | 3897575f | Jeff Cody | assert(state->bs != NULL);
|
332 | 3897575f | Jeff Cody | |
333 | 3897575f | Jeff Cody | if (queue == NULL) { |
334 | 3897575f | Jeff Cody | error_set(errp, ERROR_CLASS_GENERIC_ERROR, |
335 | 3897575f | Jeff Cody | "No reopen queue for VMDK extents");
|
336 | 3897575f | Jeff Cody | goto exit;
|
337 | 3897575f | Jeff Cody | } |
338 | 3897575f | Jeff Cody | |
339 | 3897575f | Jeff Cody | s = state->bs->opaque; |
340 | 3897575f | Jeff Cody | |
341 | 3897575f | Jeff Cody | assert(s != NULL);
|
342 | 3897575f | Jeff Cody | |
343 | 3897575f | Jeff Cody | for (i = 0; i < s->num_extents; i++) { |
344 | 3897575f | Jeff Cody | e = &s->extents[i]; |
345 | 3897575f | Jeff Cody | if (e->file != state->bs->file) {
|
346 | 3897575f | Jeff Cody | bdrv_reopen_queue(queue, e->file, state->flags); |
347 | 3897575f | Jeff Cody | } |
348 | 3897575f | Jeff Cody | } |
349 | 3897575f | Jeff Cody | ret = 0;
|
350 | 3897575f | Jeff Cody | |
351 | 3897575f | Jeff Cody | exit:
|
352 | 3897575f | Jeff Cody | return ret;
|
353 | 3897575f | Jeff Cody | } |
354 | 3897575f | Jeff Cody | |
355 | 9949f97e | Kevin Wolf | static int vmdk_parent_open(BlockDriverState *bs) |
356 | 5f4da8c0 | ths | { |
357 | 5fafdf24 | ths | char *p_name;
|
358 | 7fa60fa3 | Fam Zheng | char desc[DESC_SIZE + 1]; |
359 | e1da9b24 | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
360 | 588b65a3 | Paolo Bonzini | int ret;
|
361 | 5f4da8c0 | ths | |
362 | 7fa60fa3 | Fam Zheng | desc[DESC_SIZE] = '\0';
|
363 | 588b65a3 | Paolo Bonzini | ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE); |
364 | 588b65a3 | Paolo Bonzini | if (ret < 0) { |
365 | 588b65a3 | Paolo Bonzini | return ret;
|
366 | e1da9b24 | Fam Zheng | } |
367 | 5f4da8c0 | ths | |
368 | ae261c86 | Fam Zheng | p_name = strstr(desc, "parentFileNameHint");
|
369 | ae261c86 | Fam Zheng | if (p_name != NULL) { |
370 | 5f4da8c0 | ths | char *end_name;
|
371 | 5f4da8c0 | ths | |
372 | 5f4da8c0 | ths | p_name += sizeof("parentFileNameHint") + 1; |
373 | ae261c86 | Fam Zheng | end_name = strchr(p_name, '\"');
|
374 | ae261c86 | Fam Zheng | if (end_name == NULL) { |
375 | 588b65a3 | Paolo Bonzini | return -EINVAL;
|
376 | ae261c86 | Fam Zheng | } |
377 | ae261c86 | Fam Zheng | if ((end_name - p_name) > sizeof(bs->backing_file) - 1) { |
378 | 588b65a3 | Paolo Bonzini | return -EINVAL;
|
379 | ae261c86 | Fam Zheng | } |
380 | 3b46e624 | ths | |
381 | b171271a | Kevin Wolf | pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
|
382 | ff1afc72 | bellard | } |
383 | 5f4da8c0 | ths | |
384 | 5f4da8c0 | ths | return 0; |
385 | 5f4da8c0 | ths | } |
386 | 5f4da8c0 | ths | |
387 | b3976d3c | Fam Zheng | /* Create and append extent to the extent array. Return the added VmdkExtent
|
388 | b3976d3c | Fam Zheng | * address. return NULL if allocation failed. */
|
389 | 8aa1331c | Fam Zheng | static int vmdk_add_extent(BlockDriverState *bs, |
390 | b3976d3c | Fam Zheng | BlockDriverState *file, bool flat, int64_t sectors,
|
391 | b3976d3c | Fam Zheng | int64_t l1_offset, int64_t l1_backup_offset, |
392 | b3976d3c | Fam Zheng | uint32_t l1_size, |
393 | 8aa1331c | Fam Zheng | int l2_size, uint64_t cluster_sectors,
|
394 | 8aa1331c | Fam Zheng | VmdkExtent **new_extent) |
395 | b3976d3c | Fam Zheng | { |
396 | b3976d3c | Fam Zheng | VmdkExtent *extent; |
397 | b3976d3c | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
398 | b3976d3c | Fam Zheng | |
399 | 8aa1331c | Fam Zheng | if (cluster_sectors > 0x200000) { |
400 | 8aa1331c | Fam Zheng | /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
|
401 | 8aa1331c | Fam Zheng | error_report("invalid granularity, image may be corrupt");
|
402 | 8aa1331c | Fam Zheng | return -EINVAL;
|
403 | 8aa1331c | Fam Zheng | } |
404 | b0651b8c | Fam Zheng | if (l1_size > 512 * 1024 * 1024) { |
405 | b0651b8c | Fam Zheng | /* Although with big capacity and small l1_entry_sectors, we can get a
|
406 | b0651b8c | Fam Zheng | * big l1_size, we don't want unbounded value to allocate the table.
|
407 | b0651b8c | Fam Zheng | * Limit it to 512M, which is 16PB for default cluster and L2 table
|
408 | b0651b8c | Fam Zheng | * size */
|
409 | b0651b8c | Fam Zheng | error_report("L1 size too big");
|
410 | b0651b8c | Fam Zheng | return -EFBIG;
|
411 | b0651b8c | Fam Zheng | } |
412 | 8aa1331c | Fam Zheng | |
413 | 7267c094 | Anthony Liguori | s->extents = g_realloc(s->extents, |
414 | b3976d3c | Fam Zheng | (s->num_extents + 1) * sizeof(VmdkExtent)); |
415 | b3976d3c | Fam Zheng | extent = &s->extents[s->num_extents]; |
416 | b3976d3c | Fam Zheng | s->num_extents++; |
417 | b3976d3c | Fam Zheng | |
418 | b3976d3c | Fam Zheng | memset(extent, 0, sizeof(VmdkExtent)); |
419 | b3976d3c | Fam Zheng | extent->file = file; |
420 | b3976d3c | Fam Zheng | extent->flat = flat; |
421 | b3976d3c | Fam Zheng | extent->sectors = sectors; |
422 | b3976d3c | Fam Zheng | extent->l1_table_offset = l1_offset; |
423 | b3976d3c | Fam Zheng | extent->l1_backup_table_offset = l1_backup_offset; |
424 | b3976d3c | Fam Zheng | extent->l1_size = l1_size; |
425 | b3976d3c | Fam Zheng | extent->l1_entry_sectors = l2_size * cluster_sectors; |
426 | b3976d3c | Fam Zheng | extent->l2_size = l2_size; |
427 | b3976d3c | Fam Zheng | extent->cluster_sectors = cluster_sectors; |
428 | b3976d3c | Fam Zheng | |
429 | b3976d3c | Fam Zheng | if (s->num_extents > 1) { |
430 | b3976d3c | Fam Zheng | extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
|
431 | b3976d3c | Fam Zheng | } else {
|
432 | b3976d3c | Fam Zheng | extent->end_sector = extent->sectors; |
433 | b3976d3c | Fam Zheng | } |
434 | b3976d3c | Fam Zheng | bs->total_sectors = extent->end_sector; |
435 | 8aa1331c | Fam Zheng | if (new_extent) {
|
436 | 8aa1331c | Fam Zheng | *new_extent = extent; |
437 | 8aa1331c | Fam Zheng | } |
438 | 8aa1331c | Fam Zheng | return 0; |
439 | b3976d3c | Fam Zheng | } |
440 | b3976d3c | Fam Zheng | |
441 | b4b3ab14 | Fam Zheng | static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent) |
442 | 5f4da8c0 | ths | { |
443 | b4b3ab14 | Fam Zheng | int ret;
|
444 | b4b3ab14 | Fam Zheng | int l1_size, i;
|
445 | 5f4da8c0 | ths | |
446 | ea2384d3 | bellard | /* read the L1 table */
|
447 | b3976d3c | Fam Zheng | l1_size = extent->l1_size * sizeof(uint32_t);
|
448 | 7267c094 | Anthony Liguori | extent->l1_table = g_malloc(l1_size); |
449 | b4b3ab14 | Fam Zheng | ret = bdrv_pread(extent->file, |
450 | b4b3ab14 | Fam Zheng | extent->l1_table_offset, |
451 | b4b3ab14 | Fam Zheng | extent->l1_table, |
452 | b4b3ab14 | Fam Zheng | l1_size); |
453 | b4b3ab14 | Fam Zheng | if (ret < 0) { |
454 | b4b3ab14 | Fam Zheng | goto fail_l1;
|
455 | b3976d3c | Fam Zheng | } |
456 | b3976d3c | Fam Zheng | for (i = 0; i < extent->l1_size; i++) { |
457 | b3976d3c | Fam Zheng | le32_to_cpus(&extent->l1_table[i]); |
458 | ea2384d3 | bellard | } |
459 | ea2384d3 | bellard | |
460 | b3976d3c | Fam Zheng | if (extent->l1_backup_table_offset) {
|
461 | 7267c094 | Anthony Liguori | extent->l1_backup_table = g_malloc(l1_size); |
462 | b4b3ab14 | Fam Zheng | ret = bdrv_pread(extent->file, |
463 | b4b3ab14 | Fam Zheng | extent->l1_backup_table_offset, |
464 | b4b3ab14 | Fam Zheng | extent->l1_backup_table, |
465 | b4b3ab14 | Fam Zheng | l1_size); |
466 | b4b3ab14 | Fam Zheng | if (ret < 0) { |
467 | b4b3ab14 | Fam Zheng | goto fail_l1b;
|
468 | b3976d3c | Fam Zheng | } |
469 | b3976d3c | Fam Zheng | for (i = 0; i < extent->l1_size; i++) { |
470 | b3976d3c | Fam Zheng | le32_to_cpus(&extent->l1_backup_table[i]); |
471 | ff1afc72 | bellard | } |
472 | ff1afc72 | bellard | } |
473 | ff1afc72 | bellard | |
474 | b3976d3c | Fam Zheng | extent->l2_cache = |
475 | 7267c094 | Anthony Liguori | g_malloc(extent->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
|
476 | ea2384d3 | bellard | return 0; |
477 | b4b3ab14 | Fam Zheng | fail_l1b:
|
478 | 7267c094 | Anthony Liguori | g_free(extent->l1_backup_table); |
479 | b4b3ab14 | Fam Zheng | fail_l1:
|
480 | 7267c094 | Anthony Liguori | g_free(extent->l1_table); |
481 | b4b3ab14 | Fam Zheng | return ret;
|
482 | b4b3ab14 | Fam Zheng | } |
483 | b4b3ab14 | Fam Zheng | |
484 | daac8fdc | Fam Zheng | static int vmdk_open_vmfs_sparse(BlockDriverState *bs, |
485 | daac8fdc | Fam Zheng | BlockDriverState *file, |
486 | daac8fdc | Fam Zheng | int flags)
|
487 | b4b3ab14 | Fam Zheng | { |
488 | b4b3ab14 | Fam Zheng | int ret;
|
489 | b4b3ab14 | Fam Zheng | uint32_t magic; |
490 | b4b3ab14 | Fam Zheng | VMDK3Header header; |
491 | b4b3ab14 | Fam Zheng | VmdkExtent *extent; |
492 | b4b3ab14 | Fam Zheng | |
493 | 86c6b429 | Fam Zheng | ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header)); |
494 | b4b3ab14 | Fam Zheng | if (ret < 0) { |
495 | 86c6b429 | Fam Zheng | return ret;
|
496 | b4b3ab14 | Fam Zheng | } |
497 | f6b61e54 | Fam Zheng | ret = vmdk_add_extent(bs, file, false,
|
498 | f6b61e54 | Fam Zheng | le32_to_cpu(header.disk_sectors), |
499 | f6b61e54 | Fam Zheng | le32_to_cpu(header.l1dir_offset) << 9,
|
500 | f6b61e54 | Fam Zheng | 0,
|
501 | f6b61e54 | Fam Zheng | le32_to_cpu(header.l1dir_size), |
502 | f6b61e54 | Fam Zheng | 4096,
|
503 | f6b61e54 | Fam Zheng | le32_to_cpu(header.granularity), |
504 | f6b61e54 | Fam Zheng | &extent); |
505 | 8aa1331c | Fam Zheng | if (ret < 0) { |
506 | 8aa1331c | Fam Zheng | return ret;
|
507 | 8aa1331c | Fam Zheng | } |
508 | b4b3ab14 | Fam Zheng | ret = vmdk_init_tables(bs, extent); |
509 | b4b3ab14 | Fam Zheng | if (ret) {
|
510 | 86c6b429 | Fam Zheng | /* free extent allocated by vmdk_add_extent */
|
511 | 86c6b429 | Fam Zheng | vmdk_free_last_extent(bs); |
512 | b4b3ab14 | Fam Zheng | } |
513 | b4b3ab14 | Fam Zheng | return ret;
|
514 | b4b3ab14 | Fam Zheng | } |
515 | b4b3ab14 | Fam Zheng | |
516 | f16f509d | Fam Zheng | static int vmdk_open_desc_file(BlockDriverState *bs, int flags, |
517 | e98768d4 | Fam Zheng | uint64_t desc_offset); |
518 | f16f509d | Fam Zheng | |
519 | 86c6b429 | Fam Zheng | static int vmdk_open_vmdk4(BlockDriverState *bs, |
520 | 86c6b429 | Fam Zheng | BlockDriverState *file, |
521 | 86c6b429 | Fam Zheng | int flags)
|
522 | b4b3ab14 | Fam Zheng | { |
523 | b4b3ab14 | Fam Zheng | int ret;
|
524 | b4b3ab14 | Fam Zheng | uint32_t magic; |
525 | b4b3ab14 | Fam Zheng | uint32_t l1_size, l1_entry_sectors; |
526 | b4b3ab14 | Fam Zheng | VMDK4Header header; |
527 | b4b3ab14 | Fam Zheng | VmdkExtent *extent; |
528 | bb45ded9 | Fam Zheng | int64_t l1_backup_offset = 0;
|
529 | b4b3ab14 | Fam Zheng | |
530 | 86c6b429 | Fam Zheng | ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header)); |
531 | b4b3ab14 | Fam Zheng | if (ret < 0) { |
532 | 86c6b429 | Fam Zheng | return ret;
|
533 | b4b3ab14 | Fam Zheng | } |
534 | 5a394b9e | Stefan Hajnoczi | if (header.capacity == 0) { |
535 | e98768d4 | Fam Zheng | uint64_t desc_offset = le64_to_cpu(header.desc_offset); |
536 | 5a394b9e | Stefan Hajnoczi | if (desc_offset) {
|
537 | 5a394b9e | Stefan Hajnoczi | return vmdk_open_desc_file(bs, flags, desc_offset << 9); |
538 | 5a394b9e | Stefan Hajnoczi | } |
539 | f16f509d | Fam Zheng | } |
540 | 65bd155c | Kevin Wolf | |
541 | 65bd155c | Kevin Wolf | if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
|
542 | 65bd155c | Kevin Wolf | /*
|
543 | 65bd155c | Kevin Wolf | * The footer takes precedence over the header, so read it in. The
|
544 | 65bd155c | Kevin Wolf | * footer starts at offset -1024 from the end: One sector for the
|
545 | 65bd155c | Kevin Wolf | * footer, and another one for the end-of-stream marker.
|
546 | 65bd155c | Kevin Wolf | */
|
547 | 65bd155c | Kevin Wolf | struct {
|
548 | 65bd155c | Kevin Wolf | struct {
|
549 | 65bd155c | Kevin Wolf | uint64_t val; |
550 | 65bd155c | Kevin Wolf | uint32_t size; |
551 | 65bd155c | Kevin Wolf | uint32_t type; |
552 | 65bd155c | Kevin Wolf | uint8_t pad[512 - 16]; |
553 | 65bd155c | Kevin Wolf | } QEMU_PACKED footer_marker; |
554 | 65bd155c | Kevin Wolf | |
555 | 65bd155c | Kevin Wolf | uint32_t magic; |
556 | 65bd155c | Kevin Wolf | VMDK4Header header; |
557 | 65bd155c | Kevin Wolf | uint8_t pad[512 - 4 - sizeof(VMDK4Header)]; |
558 | 65bd155c | Kevin Wolf | |
559 | 65bd155c | Kevin Wolf | struct {
|
560 | 65bd155c | Kevin Wolf | uint64_t val; |
561 | 65bd155c | Kevin Wolf | uint32_t size; |
562 | 65bd155c | Kevin Wolf | uint32_t type; |
563 | 65bd155c | Kevin Wolf | uint8_t pad[512 - 16]; |
564 | 65bd155c | Kevin Wolf | } QEMU_PACKED eos_marker; |
565 | 65bd155c | Kevin Wolf | } QEMU_PACKED footer; |
566 | 65bd155c | Kevin Wolf | |
567 | 65bd155c | Kevin Wolf | ret = bdrv_pread(file, |
568 | 65bd155c | Kevin Wolf | bs->file->total_sectors * 512 - 1536, |
569 | 65bd155c | Kevin Wolf | &footer, sizeof(footer));
|
570 | 65bd155c | Kevin Wolf | if (ret < 0) { |
571 | 65bd155c | Kevin Wolf | return ret;
|
572 | 65bd155c | Kevin Wolf | } |
573 | 65bd155c | Kevin Wolf | |
574 | 65bd155c | Kevin Wolf | /* Some sanity checks for the footer */
|
575 | 65bd155c | Kevin Wolf | if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
|
576 | 65bd155c | Kevin Wolf | le32_to_cpu(footer.footer_marker.size) != 0 ||
|
577 | 65bd155c | Kevin Wolf | le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER || |
578 | 65bd155c | Kevin Wolf | le64_to_cpu(footer.eos_marker.val) != 0 ||
|
579 | 65bd155c | Kevin Wolf | le32_to_cpu(footer.eos_marker.size) != 0 ||
|
580 | 65bd155c | Kevin Wolf | le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM) |
581 | 65bd155c | Kevin Wolf | { |
582 | 65bd155c | Kevin Wolf | return -EINVAL;
|
583 | 65bd155c | Kevin Wolf | } |
584 | 65bd155c | Kevin Wolf | |
585 | 65bd155c | Kevin Wolf | header = footer.header; |
586 | 65bd155c | Kevin Wolf | } |
587 | 65bd155c | Kevin Wolf | |
588 | 96c51eb5 | Fam Zheng | if (le32_to_cpu(header.version) >= 3) { |
589 | 96c51eb5 | Fam Zheng | char buf[64]; |
590 | 96c51eb5 | Fam Zheng | snprintf(buf, sizeof(buf), "VMDK version %d", |
591 | 96c51eb5 | Fam Zheng | le32_to_cpu(header.version)); |
592 | 96c51eb5 | Fam Zheng | qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, |
593 | 96c51eb5 | Fam Zheng | bs->device_name, "vmdk", buf);
|
594 | 96c51eb5 | Fam Zheng | return -ENOTSUP;
|
595 | 96c51eb5 | Fam Zheng | } |
596 | 96c51eb5 | Fam Zheng | |
597 | ca8804ce | Fam Zheng | if (le32_to_cpu(header.num_gtes_per_gt) > 512) { |
598 | f8ce0403 | Fam Zheng | error_report("L2 table size too big");
|
599 | f8ce0403 | Fam Zheng | return -EINVAL;
|
600 | f8ce0403 | Fam Zheng | } |
601 | f8ce0403 | Fam Zheng | |
602 | ca8804ce | Fam Zheng | l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt) |
603 | b4b3ab14 | Fam Zheng | * le64_to_cpu(header.granularity); |
604 | 75d12341 | Stefan Weil | if (l1_entry_sectors == 0) { |
605 | 86c6b429 | Fam Zheng | return -EINVAL;
|
606 | 86c6b429 | Fam Zheng | } |
607 | b4b3ab14 | Fam Zheng | l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
|
608 | b4b3ab14 | Fam Zheng | / l1_entry_sectors; |
609 | bb45ded9 | Fam Zheng | if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
|
610 | bb45ded9 | Fam Zheng | l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
|
611 | bb45ded9 | Fam Zheng | } |
612 | 8aa1331c | Fam Zheng | ret = vmdk_add_extent(bs, file, false,
|
613 | b4b3ab14 | Fam Zheng | le64_to_cpu(header.capacity), |
614 | b4b3ab14 | Fam Zheng | le64_to_cpu(header.gd_offset) << 9,
|
615 | bb45ded9 | Fam Zheng | l1_backup_offset, |
616 | b4b3ab14 | Fam Zheng | l1_size, |
617 | ca8804ce | Fam Zheng | le32_to_cpu(header.num_gtes_per_gt), |
618 | 8aa1331c | Fam Zheng | le64_to_cpu(header.granularity), |
619 | 8aa1331c | Fam Zheng | &extent); |
620 | 8aa1331c | Fam Zheng | if (ret < 0) { |
621 | 8aa1331c | Fam Zheng | return ret;
|
622 | 8aa1331c | Fam Zheng | } |
623 | 432bb170 | Fam Zheng | extent->compressed = |
624 | 432bb170 | Fam Zheng | le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE; |
625 | 432bb170 | Fam Zheng | extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER; |
626 | 14ead646 | Fam Zheng | extent->version = le32_to_cpu(header.version); |
627 | 14ead646 | Fam Zheng | extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN; |
628 | b4b3ab14 | Fam Zheng | ret = vmdk_init_tables(bs, extent); |
629 | b4b3ab14 | Fam Zheng | if (ret) {
|
630 | 86c6b429 | Fam Zheng | /* free extent allocated by vmdk_add_extent */
|
631 | 86c6b429 | Fam Zheng | vmdk_free_last_extent(bs); |
632 | b4b3ab14 | Fam Zheng | } |
633 | b4b3ab14 | Fam Zheng | return ret;
|
634 | b4b3ab14 | Fam Zheng | } |
635 | b4b3ab14 | Fam Zheng | |
636 | 7fa60fa3 | Fam Zheng | /* find an option value out of descriptor file */
|
637 | 7fa60fa3 | Fam Zheng | static int vmdk_parse_description(const char *desc, const char *opt_name, |
638 | 7fa60fa3 | Fam Zheng | char *buf, int buf_size) |
639 | 7fa60fa3 | Fam Zheng | { |
640 | 7fa60fa3 | Fam Zheng | char *opt_pos, *opt_end;
|
641 | 7fa60fa3 | Fam Zheng | const char *end = desc + strlen(desc); |
642 | 7fa60fa3 | Fam Zheng | |
643 | 7fa60fa3 | Fam Zheng | opt_pos = strstr(desc, opt_name); |
644 | 7fa60fa3 | Fam Zheng | if (!opt_pos) {
|
645 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
646 | 7fa60fa3 | Fam Zheng | } |
647 | 7fa60fa3 | Fam Zheng | /* Skip "=\"" following opt_name */
|
648 | 7fa60fa3 | Fam Zheng | opt_pos += strlen(opt_name) + 2;
|
649 | 7fa60fa3 | Fam Zheng | if (opt_pos >= end) {
|
650 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
651 | 7fa60fa3 | Fam Zheng | } |
652 | 7fa60fa3 | Fam Zheng | opt_end = opt_pos; |
653 | 7fa60fa3 | Fam Zheng | while (opt_end < end && *opt_end != '"') { |
654 | 7fa60fa3 | Fam Zheng | opt_end++; |
655 | 7fa60fa3 | Fam Zheng | } |
656 | 7fa60fa3 | Fam Zheng | if (opt_end == end || buf_size < opt_end - opt_pos + 1) { |
657 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
658 | 7fa60fa3 | Fam Zheng | } |
659 | 7fa60fa3 | Fam Zheng | pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
|
660 | 65f74725 | Fam Zheng | return VMDK_OK;
|
661 | 7fa60fa3 | Fam Zheng | } |
662 | 7fa60fa3 | Fam Zheng | |
663 | 86c6b429 | Fam Zheng | /* Open an extent file and append to bs array */
|
664 | 86c6b429 | Fam Zheng | static int vmdk_open_sparse(BlockDriverState *bs, |
665 | 86c6b429 | Fam Zheng | BlockDriverState *file, |
666 | 86c6b429 | Fam Zheng | int flags)
|
667 | 86c6b429 | Fam Zheng | { |
668 | 86c6b429 | Fam Zheng | uint32_t magic; |
669 | 86c6b429 | Fam Zheng | |
670 | 86c6b429 | Fam Zheng | if (bdrv_pread(file, 0, &magic, sizeof(magic)) != sizeof(magic)) { |
671 | 86c6b429 | Fam Zheng | return -EIO;
|
672 | 86c6b429 | Fam Zheng | } |
673 | 86c6b429 | Fam Zheng | |
674 | 86c6b429 | Fam Zheng | magic = be32_to_cpu(magic); |
675 | 86c6b429 | Fam Zheng | switch (magic) {
|
676 | 86c6b429 | Fam Zheng | case VMDK3_MAGIC:
|
677 | daac8fdc | Fam Zheng | return vmdk_open_vmfs_sparse(bs, file, flags);
|
678 | 86c6b429 | Fam Zheng | break;
|
679 | 86c6b429 | Fam Zheng | case VMDK4_MAGIC:
|
680 | 86c6b429 | Fam Zheng | return vmdk_open_vmdk4(bs, file, flags);
|
681 | 86c6b429 | Fam Zheng | break;
|
682 | 86c6b429 | Fam Zheng | default:
|
683 | 15bac0d5 | Stefan Weil | return -EMEDIUMTYPE;
|
684 | 86c6b429 | Fam Zheng | break;
|
685 | 86c6b429 | Fam Zheng | } |
686 | 86c6b429 | Fam Zheng | } |
687 | 86c6b429 | Fam Zheng | |
688 | 7fa60fa3 | Fam Zheng | static int vmdk_parse_extents(const char *desc, BlockDriverState *bs, |
689 | 7fa60fa3 | Fam Zheng | const char *desc_file_path) |
690 | 7fa60fa3 | Fam Zheng | { |
691 | 7fa60fa3 | Fam Zheng | int ret;
|
692 | 7fa60fa3 | Fam Zheng | char access[11]; |
693 | 7fa60fa3 | Fam Zheng | char type[11]; |
694 | 7fa60fa3 | Fam Zheng | char fname[512]; |
695 | 7fa60fa3 | Fam Zheng | const char *p = desc; |
696 | 7fa60fa3 | Fam Zheng | int64_t sectors = 0;
|
697 | 7fa60fa3 | Fam Zheng | int64_t flat_offset; |
698 | 86c6b429 | Fam Zheng | char extent_path[PATH_MAX];
|
699 | 86c6b429 | Fam Zheng | BlockDriverState *extent_file; |
700 | 34b5d2c6 | Max Reitz | Error *local_err = NULL;
|
701 | 7fa60fa3 | Fam Zheng | |
702 | 7fa60fa3 | Fam Zheng | while (*p) {
|
703 | 7fa60fa3 | Fam Zheng | /* parse extent line:
|
704 | 7fa60fa3 | Fam Zheng | * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
|
705 | 7fa60fa3 | Fam Zheng | * or
|
706 | 7fa60fa3 | Fam Zheng | * RW [size in sectors] SPARSE "file-name.vmdk"
|
707 | 7fa60fa3 | Fam Zheng | */
|
708 | 7fa60fa3 | Fam Zheng | flat_offset = -1;
|
709 | cd923475 | Philipp Hahn | ret = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64, |
710 | 7fa60fa3 | Fam Zheng | access, §ors, type, fname, &flat_offset); |
711 | 7fa60fa3 | Fam Zheng | if (ret < 4 || strcmp(access, "RW")) { |
712 | 7fa60fa3 | Fam Zheng | goto next_line;
|
713 | 7fa60fa3 | Fam Zheng | } else if (!strcmp(type, "FLAT")) { |
714 | 7fa60fa3 | Fam Zheng | if (ret != 5 || flat_offset < 0) { |
715 | 7fa60fa3 | Fam Zheng | return -EINVAL;
|
716 | 7fa60fa3 | Fam Zheng | } |
717 | 7fa60fa3 | Fam Zheng | } else if (ret != 4) { |
718 | 7fa60fa3 | Fam Zheng | return -EINVAL;
|
719 | 7fa60fa3 | Fam Zheng | } |
720 | 7fa60fa3 | Fam Zheng | |
721 | 7fa60fa3 | Fam Zheng | if (sectors <= 0 || |
722 | daac8fdc | Fam Zheng | (strcmp(type, "FLAT") && strcmp(type, "SPARSE") && |
723 | 04d542c8 | Paolo Bonzini | strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) || |
724 | 7fa60fa3 | Fam Zheng | (strcmp(access, "RW"))) {
|
725 | 7fa60fa3 | Fam Zheng | goto next_line;
|
726 | 7fa60fa3 | Fam Zheng | } |
727 | 7fa60fa3 | Fam Zheng | |
728 | 86c6b429 | Fam Zheng | path_combine(extent_path, sizeof(extent_path),
|
729 | 86c6b429 | Fam Zheng | desc_file_path, fname); |
730 | 34b5d2c6 | Max Reitz | ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
|
731 | 34b5d2c6 | Max Reitz | &local_err); |
732 | 86c6b429 | Fam Zheng | if (ret) {
|
733 | 34b5d2c6 | Max Reitz | qerror_report_err(local_err); |
734 | 34b5d2c6 | Max Reitz | error_free(local_err); |
735 | 86c6b429 | Fam Zheng | return ret;
|
736 | 86c6b429 | Fam Zheng | } |
737 | 86c6b429 | Fam Zheng | |
738 | 7fa60fa3 | Fam Zheng | /* save to extents array */
|
739 | 04d542c8 | Paolo Bonzini | if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) { |
740 | 7fa60fa3 | Fam Zheng | /* FLAT extent */
|
741 | 7fa60fa3 | Fam Zheng | VmdkExtent *extent; |
742 | 7fa60fa3 | Fam Zheng | |
743 | 8aa1331c | Fam Zheng | ret = vmdk_add_extent(bs, extent_file, true, sectors,
|
744 | 8aa1331c | Fam Zheng | 0, 0, 0, 0, sectors, &extent); |
745 | 8aa1331c | Fam Zheng | if (ret < 0) { |
746 | 8aa1331c | Fam Zheng | return ret;
|
747 | 8aa1331c | Fam Zheng | } |
748 | f16f509d | Fam Zheng | extent->flat_start_offset = flat_offset << 9;
|
749 | daac8fdc | Fam Zheng | } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) { |
750 | daac8fdc | Fam Zheng | /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
|
751 | 86c6b429 | Fam Zheng | ret = vmdk_open_sparse(bs, extent_file, bs->open_flags); |
752 | 86c6b429 | Fam Zheng | if (ret) {
|
753 | 4f6fd349 | Fam Zheng | bdrv_unref(extent_file); |
754 | 86c6b429 | Fam Zheng | return ret;
|
755 | 86c6b429 | Fam Zheng | } |
756 | 7fa60fa3 | Fam Zheng | } else {
|
757 | 7fa60fa3 | Fam Zheng | fprintf(stderr, |
758 | 7fa60fa3 | Fam Zheng | "VMDK: Not supported extent type \"%s\""".\n", type); |
759 | 7fa60fa3 | Fam Zheng | return -ENOTSUP;
|
760 | 7fa60fa3 | Fam Zheng | } |
761 | 7fa60fa3 | Fam Zheng | next_line:
|
762 | 7fa60fa3 | Fam Zheng | /* move to next line */
|
763 | 7fa60fa3 | Fam Zheng | while (*p && *p != '\n') { |
764 | 7fa60fa3 | Fam Zheng | p++; |
765 | 7fa60fa3 | Fam Zheng | } |
766 | 7fa60fa3 | Fam Zheng | p++; |
767 | 7fa60fa3 | Fam Zheng | } |
768 | 7fa60fa3 | Fam Zheng | return 0; |
769 | 7fa60fa3 | Fam Zheng | } |
770 | 7fa60fa3 | Fam Zheng | |
771 | f16f509d | Fam Zheng | static int vmdk_open_desc_file(BlockDriverState *bs, int flags, |
772 | e98768d4 | Fam Zheng | uint64_t desc_offset) |
773 | 7fa60fa3 | Fam Zheng | { |
774 | 7fa60fa3 | Fam Zheng | int ret;
|
775 | 0bed087d | Evgeny Budilovsky | char *buf = NULL; |
776 | 7fa60fa3 | Fam Zheng | char ct[128]; |
777 | 7fa60fa3 | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
778 | 0bed087d | Evgeny Budilovsky | int64_t size; |
779 | 7fa60fa3 | Fam Zheng | |
780 | 0bed087d | Evgeny Budilovsky | size = bdrv_getlength(bs->file); |
781 | 0bed087d | Evgeny Budilovsky | if (size < 0) { |
782 | 0bed087d | Evgeny Budilovsky | return -EINVAL;
|
783 | 0bed087d | Evgeny Budilovsky | } |
784 | 0bed087d | Evgeny Budilovsky | |
785 | 0bed087d | Evgeny Budilovsky | size = MIN(size, 1 << 20); /* avoid unbounded allocation */ |
786 | 0bed087d | Evgeny Budilovsky | buf = g_malloc0(size + 1);
|
787 | 0bed087d | Evgeny Budilovsky | |
788 | 0bed087d | Evgeny Budilovsky | ret = bdrv_pread(bs->file, desc_offset, buf, size); |
789 | 7fa60fa3 | Fam Zheng | if (ret < 0) { |
790 | 0bed087d | Evgeny Budilovsky | goto exit;
|
791 | 7fa60fa3 | Fam Zheng | } |
792 | 7fa60fa3 | Fam Zheng | if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) { |
793 | 0bed087d | Evgeny Budilovsky | ret = -EMEDIUMTYPE; |
794 | 0bed087d | Evgeny Budilovsky | goto exit;
|
795 | 7fa60fa3 | Fam Zheng | } |
796 | 6398de51 | Fam Zheng | if (strcmp(ct, "monolithicFlat") && |
797 | 04d542c8 | Paolo Bonzini | strcmp(ct, "vmfs") &&
|
798 | daac8fdc | Fam Zheng | strcmp(ct, "vmfsSparse") &&
|
799 | 86c6b429 | Fam Zheng | strcmp(ct, "twoGbMaxExtentSparse") &&
|
800 | 6398de51 | Fam Zheng | strcmp(ct, "twoGbMaxExtentFlat")) {
|
801 | 7fa60fa3 | Fam Zheng | fprintf(stderr, |
802 | 7fa60fa3 | Fam Zheng | "VMDK: Not supported image type \"%s\""".\n", ct); |
803 | 0bed087d | Evgeny Budilovsky | ret = -ENOTSUP; |
804 | 0bed087d | Evgeny Budilovsky | goto exit;
|
805 | 7fa60fa3 | Fam Zheng | } |
806 | 7fa60fa3 | Fam Zheng | s->desc_offset = 0;
|
807 | 0bed087d | Evgeny Budilovsky | ret = vmdk_parse_extents(buf, bs, bs->file->filename); |
808 | 0bed087d | Evgeny Budilovsky | exit:
|
809 | 0bed087d | Evgeny Budilovsky | g_free(buf); |
810 | 0bed087d | Evgeny Budilovsky | return ret;
|
811 | 7fa60fa3 | Fam Zheng | } |
812 | 7fa60fa3 | Fam Zheng | |
813 | 015a1036 | Max Reitz | static int vmdk_open(BlockDriverState *bs, QDict *options, int flags, |
814 | 015a1036 | Max Reitz | Error **errp) |
815 | b4b3ab14 | Fam Zheng | { |
816 | 86c6b429 | Fam Zheng | int ret;
|
817 | 86c6b429 | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
818 | b4b3ab14 | Fam Zheng | |
819 | 86c6b429 | Fam Zheng | if (vmdk_open_sparse(bs, bs->file, flags) == 0) { |
820 | 86c6b429 | Fam Zheng | s->desc_offset = 0x200;
|
821 | bae0a0cc | Paolo Bonzini | } else {
|
822 | bae0a0cc | Paolo Bonzini | ret = vmdk_open_desc_file(bs, flags, 0);
|
823 | 86c6b429 | Fam Zheng | if (ret) {
|
824 | bae0a0cc | Paolo Bonzini | goto fail;
|
825 | 86c6b429 | Fam Zheng | } |
826 | b4b3ab14 | Fam Zheng | } |
827 | bae0a0cc | Paolo Bonzini | /* try to open parent images, if exist */
|
828 | bae0a0cc | Paolo Bonzini | ret = vmdk_parent_open(bs); |
829 | bae0a0cc | Paolo Bonzini | if (ret) {
|
830 | bae0a0cc | Paolo Bonzini | goto fail;
|
831 | bae0a0cc | Paolo Bonzini | } |
832 | bae0a0cc | Paolo Bonzini | s->parent_cid = vmdk_read_cid(bs, 1);
|
833 | 848c66e8 | Paolo Bonzini | qemu_co_mutex_init(&s->lock); |
834 | 2bc3166c | Kevin Wolf | |
835 | 2bc3166c | Kevin Wolf | /* Disable migration when VMDK images are used */
|
836 | 2bc3166c | Kevin Wolf | error_set(&s->migration_blocker, |
837 | 2bc3166c | Kevin Wolf | QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, |
838 | 2bc3166c | Kevin Wolf | "vmdk", bs->device_name, "live migration"); |
839 | 2bc3166c | Kevin Wolf | migrate_add_blocker(s->migration_blocker); |
840 | 2bc3166c | Kevin Wolf | |
841 | 2bc3166c | Kevin Wolf | return 0; |
842 | bae0a0cc | Paolo Bonzini | |
843 | bae0a0cc | Paolo Bonzini | fail:
|
844 | bae0a0cc | Paolo Bonzini | vmdk_free_extents(bs); |
845 | bae0a0cc | Paolo Bonzini | return ret;
|
846 | ea2384d3 | bellard | } |
847 | ea2384d3 | bellard | |
848 | b3976d3c | Fam Zheng | static int get_whole_cluster(BlockDriverState *bs, |
849 | b3976d3c | Fam Zheng | VmdkExtent *extent, |
850 | b3976d3c | Fam Zheng | uint64_t cluster_offset, |
851 | b3976d3c | Fam Zheng | uint64_t offset, |
852 | b3976d3c | Fam Zheng | bool allocate)
|
853 | 5f4da8c0 | ths | { |
854 | bf81507d | Fam Zheng | int ret = VMDK_OK;
|
855 | bf81507d | Fam Zheng | uint8_t *whole_grain = NULL;
|
856 | 5f4da8c0 | ths | |
857 | 0e69c543 | Fam Zheng | /* we will be here if it's first write on non-exist grain(cluster).
|
858 | 0e69c543 | Fam Zheng | * try to read from parent image, if exist */
|
859 | b171271a | Kevin Wolf | if (bs->backing_hd) {
|
860 | bf81507d | Fam Zheng | whole_grain = |
861 | bf81507d | Fam Zheng | qemu_blockalign(bs, extent->cluster_sectors << BDRV_SECTOR_BITS); |
862 | ae261c86 | Fam Zheng | if (!vmdk_is_cid_valid(bs)) {
|
863 | bf81507d | Fam Zheng | ret = VMDK_ERROR; |
864 | bf81507d | Fam Zheng | goto exit;
|
865 | ae261c86 | Fam Zheng | } |
866 | 5f4da8c0 | ths | |
867 | 0e69c543 | Fam Zheng | /* floor offset to cluster */
|
868 | 0e69c543 | Fam Zheng | offset -= offset % (extent->cluster_sectors * 512);
|
869 | c336500d | Kevin Wolf | ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain,
|
870 | b3976d3c | Fam Zheng | extent->cluster_sectors); |
871 | c336500d | Kevin Wolf | if (ret < 0) { |
872 | bf81507d | Fam Zheng | ret = VMDK_ERROR; |
873 | bf81507d | Fam Zheng | goto exit;
|
874 | c336500d | Kevin Wolf | } |
875 | 630530a6 | ths | |
876 | 0e69c543 | Fam Zheng | /* Write grain only into the active image */
|
877 | b3976d3c | Fam Zheng | ret = bdrv_write(extent->file, cluster_offset, whole_grain, |
878 | b3976d3c | Fam Zheng | extent->cluster_sectors); |
879 | c336500d | Kevin Wolf | if (ret < 0) { |
880 | bf81507d | Fam Zheng | ret = VMDK_ERROR; |
881 | bf81507d | Fam Zheng | goto exit;
|
882 | 630530a6 | ths | } |
883 | 630530a6 | ths | } |
884 | bf81507d | Fam Zheng | exit:
|
885 | bf81507d | Fam Zheng | qemu_vfree(whole_grain); |
886 | bf81507d | Fam Zheng | return ret;
|
887 | 630530a6 | ths | } |
888 | 630530a6 | ths | |
889 | b3976d3c | Fam Zheng | static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) |
890 | 630530a6 | ths | { |
891 | e304e8e5 | Fam Zheng | uint32_t offset; |
892 | e304e8e5 | Fam Zheng | QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset)); |
893 | e304e8e5 | Fam Zheng | offset = cpu_to_le32(m_data->offset); |
894 | 630530a6 | ths | /* update L2 table */
|
895 | b3976d3c | Fam Zheng | if (bdrv_pwrite_sync(
|
896 | b3976d3c | Fam Zheng | extent->file, |
897 | b3976d3c | Fam Zheng | ((int64_t)m_data->l2_offset * 512)
|
898 | b3976d3c | Fam Zheng | + (m_data->l2_index * sizeof(m_data->offset)),
|
899 | e304e8e5 | Fam Zheng | &offset, sizeof(offset)) < 0) { |
900 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
901 | b3976d3c | Fam Zheng | } |
902 | 630530a6 | ths | /* update backup L2 table */
|
903 | b3976d3c | Fam Zheng | if (extent->l1_backup_table_offset != 0) { |
904 | b3976d3c | Fam Zheng | m_data->l2_offset = extent->l1_backup_table[m_data->l1_index]; |
905 | b3976d3c | Fam Zheng | if (bdrv_pwrite_sync(
|
906 | b3976d3c | Fam Zheng | extent->file, |
907 | b3976d3c | Fam Zheng | ((int64_t)m_data->l2_offset * 512)
|
908 | b3976d3c | Fam Zheng | + (m_data->l2_index * sizeof(m_data->offset)),
|
909 | e304e8e5 | Fam Zheng | &offset, sizeof(offset)) < 0) { |
910 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
911 | b3976d3c | Fam Zheng | } |
912 | 5f4da8c0 | ths | } |
913 | cdeaf1f1 | Fam Zheng | if (m_data->l2_cache_entry) {
|
914 | cdeaf1f1 | Fam Zheng | *m_data->l2_cache_entry = offset; |
915 | cdeaf1f1 | Fam Zheng | } |
916 | 630530a6 | ths | |
917 | 65f74725 | Fam Zheng | return VMDK_OK;
|
918 | 5f4da8c0 | ths | } |
919 | 5f4da8c0 | ths | |
920 | 91b85bd3 | Fam Zheng | static int get_cluster_offset(BlockDriverState *bs, |
921 | b3976d3c | Fam Zheng | VmdkExtent *extent, |
922 | b3976d3c | Fam Zheng | VmdkMetaData *m_data, |
923 | 91b85bd3 | Fam Zheng | uint64_t offset, |
924 | 91b85bd3 | Fam Zheng | int allocate,
|
925 | 91b85bd3 | Fam Zheng | uint64_t *cluster_offset) |
926 | ea2384d3 | bellard | { |
927 | ea2384d3 | bellard | unsigned int l1_index, l2_offset, l2_index; |
928 | ea2384d3 | bellard | int min_index, i, j;
|
929 | e304e8e5 | Fam Zheng | uint32_t min_count, *l2_table; |
930 | 14ead646 | Fam Zheng | bool zeroed = false; |
931 | 630530a6 | ths | |
932 | ae261c86 | Fam Zheng | if (m_data) {
|
933 | 630530a6 | ths | m_data->valid = 0;
|
934 | ae261c86 | Fam Zheng | } |
935 | 91b85bd3 | Fam Zheng | if (extent->flat) {
|
936 | 7fa60fa3 | Fam Zheng | *cluster_offset = extent->flat_start_offset; |
937 | 65f74725 | Fam Zheng | return VMDK_OK;
|
938 | 91b85bd3 | Fam Zheng | } |
939 | 630530a6 | ths | |
940 | 6398de51 | Fam Zheng | offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE; |
941 | b3976d3c | Fam Zheng | l1_index = (offset >> 9) / extent->l1_entry_sectors;
|
942 | b3976d3c | Fam Zheng | if (l1_index >= extent->l1_size) {
|
943 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
944 | b3976d3c | Fam Zheng | } |
945 | b3976d3c | Fam Zheng | l2_offset = extent->l1_table[l1_index]; |
946 | b3976d3c | Fam Zheng | if (!l2_offset) {
|
947 | 65f74725 | Fam Zheng | return VMDK_UNALLOC;
|
948 | b3976d3c | Fam Zheng | } |
949 | b4b3ab14 | Fam Zheng | for (i = 0; i < L2_CACHE_SIZE; i++) { |
950 | b3976d3c | Fam Zheng | if (l2_offset == extent->l2_cache_offsets[i]) {
|
951 | ea2384d3 | bellard | /* increment the hit count */
|
952 | b3976d3c | Fam Zheng | if (++extent->l2_cache_counts[i] == 0xffffffff) { |
953 | b4b3ab14 | Fam Zheng | for (j = 0; j < L2_CACHE_SIZE; j++) { |
954 | b3976d3c | Fam Zheng | extent->l2_cache_counts[j] >>= 1;
|
955 | ea2384d3 | bellard | } |
956 | ea2384d3 | bellard | } |
957 | b3976d3c | Fam Zheng | l2_table = extent->l2_cache + (i * extent->l2_size); |
958 | ea2384d3 | bellard | goto found;
|
959 | ea2384d3 | bellard | } |
960 | ea2384d3 | bellard | } |
961 | ea2384d3 | bellard | /* not found: load a new entry in the least used one */
|
962 | ea2384d3 | bellard | min_index = 0;
|
963 | ea2384d3 | bellard | min_count = 0xffffffff;
|
964 | b4b3ab14 | Fam Zheng | for (i = 0; i < L2_CACHE_SIZE; i++) { |
965 | b3976d3c | Fam Zheng | if (extent->l2_cache_counts[i] < min_count) {
|
966 | b3976d3c | Fam Zheng | min_count = extent->l2_cache_counts[i]; |
967 | ea2384d3 | bellard | min_index = i; |
968 | ea2384d3 | bellard | } |
969 | ea2384d3 | bellard | } |
970 | b3976d3c | Fam Zheng | l2_table = extent->l2_cache + (min_index * extent->l2_size); |
971 | b3976d3c | Fam Zheng | if (bdrv_pread(
|
972 | b3976d3c | Fam Zheng | extent->file, |
973 | b3976d3c | Fam Zheng | (int64_t)l2_offset * 512,
|
974 | b3976d3c | Fam Zheng | l2_table, |
975 | b3976d3c | Fam Zheng | extent->l2_size * sizeof(uint32_t)
|
976 | b3976d3c | Fam Zheng | ) != extent->l2_size * sizeof(uint32_t)) {
|
977 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
978 | b3976d3c | Fam Zheng | } |
979 | 5f4da8c0 | ths | |
980 | b3976d3c | Fam Zheng | extent->l2_cache_offsets[min_index] = l2_offset; |
981 | b3976d3c | Fam Zheng | extent->l2_cache_counts[min_index] = 1;
|
982 | ea2384d3 | bellard | found:
|
983 | b3976d3c | Fam Zheng | l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
|
984 | 91b85bd3 | Fam Zheng | *cluster_offset = le32_to_cpu(l2_table[l2_index]); |
985 | 630530a6 | ths | |
986 | cdeaf1f1 | Fam Zheng | if (m_data) {
|
987 | cdeaf1f1 | Fam Zheng | m_data->valid = 1;
|
988 | cdeaf1f1 | Fam Zheng | m_data->l1_index = l1_index; |
989 | cdeaf1f1 | Fam Zheng | m_data->l2_index = l2_index; |
990 | cdeaf1f1 | Fam Zheng | m_data->offset = *cluster_offset; |
991 | cdeaf1f1 | Fam Zheng | m_data->l2_offset = l2_offset; |
992 | cdeaf1f1 | Fam Zheng | m_data->l2_cache_entry = &l2_table[l2_index]; |
993 | cdeaf1f1 | Fam Zheng | } |
994 | 14ead646 | Fam Zheng | if (extent->has_zero_grain && *cluster_offset == VMDK_GTE_ZEROED) {
|
995 | 14ead646 | Fam Zheng | zeroed = true;
|
996 | 14ead646 | Fam Zheng | } |
997 | 14ead646 | Fam Zheng | |
998 | 14ead646 | Fam Zheng | if (!*cluster_offset || zeroed) {
|
999 | 91b85bd3 | Fam Zheng | if (!allocate) {
|
1000 | 14ead646 | Fam Zheng | return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
|
1001 | 91b85bd3 | Fam Zheng | } |
1002 | 9949f97e | Kevin Wolf | |
1003 | ae261c86 | Fam Zheng | /* Avoid the L2 tables update for the images that have snapshots. */
|
1004 | 91b85bd3 | Fam Zheng | *cluster_offset = bdrv_getlength(extent->file); |
1005 | 2b2c8c5d | Fam Zheng | if (!extent->compressed) {
|
1006 | 2b2c8c5d | Fam Zheng | bdrv_truncate( |
1007 | 2b2c8c5d | Fam Zheng | extent->file, |
1008 | 2b2c8c5d | Fam Zheng | *cluster_offset + (extent->cluster_sectors << 9)
|
1009 | 2b2c8c5d | Fam Zheng | ); |
1010 | 2b2c8c5d | Fam Zheng | } |
1011 | 9949f97e | Kevin Wolf | |
1012 | 91b85bd3 | Fam Zheng | *cluster_offset >>= 9;
|
1013 | e304e8e5 | Fam Zheng | l2_table[l2_index] = cpu_to_le32(*cluster_offset); |
1014 | 630530a6 | ths | |
1015 | 630530a6 | ths | /* First of all we write grain itself, to avoid race condition
|
1016 | 630530a6 | ths | * that may to corrupt the image.
|
1017 | 630530a6 | ths | * This problem may occur because of insufficient space on host disk
|
1018 | 630530a6 | ths | * or inappropriate VM shutdown.
|
1019 | 630530a6 | ths | */
|
1020 | b3976d3c | Fam Zheng | if (get_whole_cluster(
|
1021 | ae261c86 | Fam Zheng | bs, extent, *cluster_offset, offset, allocate) == -1) {
|
1022 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
1023 | ae261c86 | Fam Zheng | } |
1024 | 630530a6 | ths | |
1025 | 630530a6 | ths | if (m_data) {
|
1026 | e304e8e5 | Fam Zheng | m_data->offset = *cluster_offset; |
1027 | 630530a6 | ths | } |
1028 | ff1afc72 | bellard | } |
1029 | 91b85bd3 | Fam Zheng | *cluster_offset <<= 9;
|
1030 | 65f74725 | Fam Zheng | return VMDK_OK;
|
1031 | ea2384d3 | bellard | } |
1032 | ea2384d3 | bellard | |
1033 | b3976d3c | Fam Zheng | static VmdkExtent *find_extent(BDRVVmdkState *s,
|
1034 | b3976d3c | Fam Zheng | int64_t sector_num, VmdkExtent *start_hint) |
1035 | b3976d3c | Fam Zheng | { |
1036 | b3976d3c | Fam Zheng | VmdkExtent *extent = start_hint; |
1037 | b3976d3c | Fam Zheng | |
1038 | b3976d3c | Fam Zheng | if (!extent) {
|
1039 | b3976d3c | Fam Zheng | extent = &s->extents[0];
|
1040 | b3976d3c | Fam Zheng | } |
1041 | b3976d3c | Fam Zheng | while (extent < &s->extents[s->num_extents]) {
|
1042 | b3976d3c | Fam Zheng | if (sector_num < extent->end_sector) {
|
1043 | b3976d3c | Fam Zheng | return extent;
|
1044 | b3976d3c | Fam Zheng | } |
1045 | b3976d3c | Fam Zheng | extent++; |
1046 | b3976d3c | Fam Zheng | } |
1047 | b3976d3c | Fam Zheng | return NULL; |
1048 | b3976d3c | Fam Zheng | } |
1049 | b3976d3c | Fam Zheng | |
1050 | b6b8a333 | Paolo Bonzini | static int64_t coroutine_fn vmdk_co_get_block_status(BlockDriverState *bs,
|
1051 | f8a2e5e3 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, int *pnum) |
1052 | ea2384d3 | bellard | { |
1053 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
1054 | b3976d3c | Fam Zheng | int64_t index_in_cluster, n, ret; |
1055 | b3976d3c | Fam Zheng | uint64_t offset; |
1056 | b3976d3c | Fam Zheng | VmdkExtent *extent; |
1057 | b3976d3c | Fam Zheng | |
1058 | b3976d3c | Fam Zheng | extent = find_extent(s, sector_num, NULL);
|
1059 | b3976d3c | Fam Zheng | if (!extent) {
|
1060 | b3976d3c | Fam Zheng | return 0; |
1061 | b3976d3c | Fam Zheng | } |
1062 | f8a2e5e3 | Stefan Hajnoczi | qemu_co_mutex_lock(&s->lock); |
1063 | 91b85bd3 | Fam Zheng | ret = get_cluster_offset(bs, extent, NULL,
|
1064 | 91b85bd3 | Fam Zheng | sector_num * 512, 0, &offset); |
1065 | f8a2e5e3 | Stefan Hajnoczi | qemu_co_mutex_unlock(&s->lock); |
1066 | 14ead646 | Fam Zheng | |
1067 | 4bc74be9 | Paolo Bonzini | switch (ret) {
|
1068 | 4bc74be9 | Paolo Bonzini | case VMDK_ERROR:
|
1069 | 4bc74be9 | Paolo Bonzini | ret = -EIO; |
1070 | 4bc74be9 | Paolo Bonzini | break;
|
1071 | 4bc74be9 | Paolo Bonzini | case VMDK_UNALLOC:
|
1072 | 4bc74be9 | Paolo Bonzini | ret = 0;
|
1073 | 4bc74be9 | Paolo Bonzini | break;
|
1074 | 4bc74be9 | Paolo Bonzini | case VMDK_ZEROED:
|
1075 | 4bc74be9 | Paolo Bonzini | ret = BDRV_BLOCK_ZERO; |
1076 | 4bc74be9 | Paolo Bonzini | break;
|
1077 | 4bc74be9 | Paolo Bonzini | case VMDK_OK:
|
1078 | 4bc74be9 | Paolo Bonzini | ret = BDRV_BLOCK_DATA; |
1079 | 4bc74be9 | Paolo Bonzini | if (extent->file == bs->file) {
|
1080 | 4bc74be9 | Paolo Bonzini | ret |= BDRV_BLOCK_OFFSET_VALID | offset; |
1081 | 4bc74be9 | Paolo Bonzini | } |
1082 | 4bc74be9 | Paolo Bonzini | |
1083 | 4bc74be9 | Paolo Bonzini | break;
|
1084 | 4bc74be9 | Paolo Bonzini | } |
1085 | 91b85bd3 | Fam Zheng | |
1086 | 91b85bd3 | Fam Zheng | index_in_cluster = sector_num % extent->cluster_sectors; |
1087 | 91b85bd3 | Fam Zheng | n = extent->cluster_sectors - index_in_cluster; |
1088 | ae261c86 | Fam Zheng | if (n > nb_sectors) {
|
1089 | ea2384d3 | bellard | n = nb_sectors; |
1090 | ae261c86 | Fam Zheng | } |
1091 | ea2384d3 | bellard | *pnum = n; |
1092 | b3976d3c | Fam Zheng | return ret;
|
1093 | ea2384d3 | bellard | } |
1094 | ea2384d3 | bellard | |
1095 | dd3f6ee2 | Fam Zheng | static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset, |
1096 | dd3f6ee2 | Fam Zheng | int64_t offset_in_cluster, const uint8_t *buf,
|
1097 | dd3f6ee2 | Fam Zheng | int nb_sectors, int64_t sector_num)
|
1098 | dd3f6ee2 | Fam Zheng | { |
1099 | dd3f6ee2 | Fam Zheng | int ret;
|
1100 | 2b2c8c5d | Fam Zheng | VmdkGrainMarker *data = NULL;
|
1101 | 2b2c8c5d | Fam Zheng | uLongf buf_len; |
1102 | dd3f6ee2 | Fam Zheng | const uint8_t *write_buf = buf;
|
1103 | dd3f6ee2 | Fam Zheng | int write_len = nb_sectors * 512; |
1104 | dd3f6ee2 | Fam Zheng | |
1105 | 2b2c8c5d | Fam Zheng | if (extent->compressed) {
|
1106 | 2b2c8c5d | Fam Zheng | if (!extent->has_marker) {
|
1107 | 2b2c8c5d | Fam Zheng | ret = -EINVAL; |
1108 | 2b2c8c5d | Fam Zheng | goto out;
|
1109 | 2b2c8c5d | Fam Zheng | } |
1110 | 2b2c8c5d | Fam Zheng | buf_len = (extent->cluster_sectors << 9) * 2; |
1111 | 2b2c8c5d | Fam Zheng | data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
|
1112 | 2b2c8c5d | Fam Zheng | if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK || |
1113 | 2b2c8c5d | Fam Zheng | buf_len == 0) {
|
1114 | 2b2c8c5d | Fam Zheng | ret = -EINVAL; |
1115 | 2b2c8c5d | Fam Zheng | goto out;
|
1116 | 2b2c8c5d | Fam Zheng | } |
1117 | 2b2c8c5d | Fam Zheng | data->lba = sector_num; |
1118 | 2b2c8c5d | Fam Zheng | data->size = buf_len; |
1119 | 2b2c8c5d | Fam Zheng | write_buf = (uint8_t *)data; |
1120 | 2b2c8c5d | Fam Zheng | write_len = buf_len + sizeof(VmdkGrainMarker);
|
1121 | 2b2c8c5d | Fam Zheng | } |
1122 | dd3f6ee2 | Fam Zheng | ret = bdrv_pwrite(extent->file, |
1123 | dd3f6ee2 | Fam Zheng | cluster_offset + offset_in_cluster, |
1124 | dd3f6ee2 | Fam Zheng | write_buf, |
1125 | dd3f6ee2 | Fam Zheng | write_len); |
1126 | dd3f6ee2 | Fam Zheng | if (ret != write_len) {
|
1127 | dd3f6ee2 | Fam Zheng | ret = ret < 0 ? ret : -EIO;
|
1128 | dd3f6ee2 | Fam Zheng | goto out;
|
1129 | dd3f6ee2 | Fam Zheng | } |
1130 | dd3f6ee2 | Fam Zheng | ret = 0;
|
1131 | dd3f6ee2 | Fam Zheng | out:
|
1132 | 2b2c8c5d | Fam Zheng | g_free(data); |
1133 | dd3f6ee2 | Fam Zheng | return ret;
|
1134 | dd3f6ee2 | Fam Zheng | } |
1135 | dd3f6ee2 | Fam Zheng | |
1136 | dd3f6ee2 | Fam Zheng | static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset, |
1137 | dd3f6ee2 | Fam Zheng | int64_t offset_in_cluster, uint8_t *buf, |
1138 | dd3f6ee2 | Fam Zheng | int nb_sectors)
|
1139 | dd3f6ee2 | Fam Zheng | { |
1140 | dd3f6ee2 | Fam Zheng | int ret;
|
1141 | 2b2c8c5d | Fam Zheng | int cluster_bytes, buf_bytes;
|
1142 | 2b2c8c5d | Fam Zheng | uint8_t *cluster_buf, *compressed_data; |
1143 | 2b2c8c5d | Fam Zheng | uint8_t *uncomp_buf; |
1144 | 2b2c8c5d | Fam Zheng | uint32_t data_len; |
1145 | 2b2c8c5d | Fam Zheng | VmdkGrainMarker *marker; |
1146 | 2b2c8c5d | Fam Zheng | uLongf buf_len; |
1147 | 2b2c8c5d | Fam Zheng | |
1148 | dd3f6ee2 | Fam Zheng | |
1149 | 2b2c8c5d | Fam Zheng | if (!extent->compressed) {
|
1150 | 2b2c8c5d | Fam Zheng | ret = bdrv_pread(extent->file, |
1151 | 2b2c8c5d | Fam Zheng | cluster_offset + offset_in_cluster, |
1152 | 2b2c8c5d | Fam Zheng | buf, nb_sectors * 512);
|
1153 | 2b2c8c5d | Fam Zheng | if (ret == nb_sectors * 512) { |
1154 | 2b2c8c5d | Fam Zheng | return 0; |
1155 | 2b2c8c5d | Fam Zheng | } else {
|
1156 | 2b2c8c5d | Fam Zheng | return -EIO;
|
1157 | 2b2c8c5d | Fam Zheng | } |
1158 | 2b2c8c5d | Fam Zheng | } |
1159 | 2b2c8c5d | Fam Zheng | cluster_bytes = extent->cluster_sectors * 512;
|
1160 | 2b2c8c5d | Fam Zheng | /* Read two clusters in case GrainMarker + compressed data > one cluster */
|
1161 | 2b2c8c5d | Fam Zheng | buf_bytes = cluster_bytes * 2;
|
1162 | 2b2c8c5d | Fam Zheng | cluster_buf = g_malloc(buf_bytes); |
1163 | 2b2c8c5d | Fam Zheng | uncomp_buf = g_malloc(cluster_bytes); |
1164 | dd3f6ee2 | Fam Zheng | ret = bdrv_pread(extent->file, |
1165 | 2b2c8c5d | Fam Zheng | cluster_offset, |
1166 | 2b2c8c5d | Fam Zheng | cluster_buf, buf_bytes); |
1167 | 2b2c8c5d | Fam Zheng | if (ret < 0) { |
1168 | 2b2c8c5d | Fam Zheng | goto out;
|
1169 | 2b2c8c5d | Fam Zheng | } |
1170 | 2b2c8c5d | Fam Zheng | compressed_data = cluster_buf; |
1171 | 2b2c8c5d | Fam Zheng | buf_len = cluster_bytes; |
1172 | 2b2c8c5d | Fam Zheng | data_len = cluster_bytes; |
1173 | 2b2c8c5d | Fam Zheng | if (extent->has_marker) {
|
1174 | 2b2c8c5d | Fam Zheng | marker = (VmdkGrainMarker *)cluster_buf; |
1175 | 2b2c8c5d | Fam Zheng | compressed_data = marker->data; |
1176 | 2b2c8c5d | Fam Zheng | data_len = le32_to_cpu(marker->size); |
1177 | 2b2c8c5d | Fam Zheng | } |
1178 | 2b2c8c5d | Fam Zheng | if (!data_len || data_len > buf_bytes) {
|
1179 | 2b2c8c5d | Fam Zheng | ret = -EINVAL; |
1180 | 2b2c8c5d | Fam Zheng | goto out;
|
1181 | 2b2c8c5d | Fam Zheng | } |
1182 | 2b2c8c5d | Fam Zheng | ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len); |
1183 | 2b2c8c5d | Fam Zheng | if (ret != Z_OK) {
|
1184 | 2b2c8c5d | Fam Zheng | ret = -EINVAL; |
1185 | 2b2c8c5d | Fam Zheng | goto out;
|
1186 | 2b2c8c5d | Fam Zheng | |
1187 | 2b2c8c5d | Fam Zheng | } |
1188 | 2b2c8c5d | Fam Zheng | if (offset_in_cluster < 0 || |
1189 | 2b2c8c5d | Fam Zheng | offset_in_cluster + nb_sectors * 512 > buf_len) {
|
1190 | 2b2c8c5d | Fam Zheng | ret = -EINVAL; |
1191 | 2b2c8c5d | Fam Zheng | goto out;
|
1192 | dd3f6ee2 | Fam Zheng | } |
1193 | 2b2c8c5d | Fam Zheng | memcpy(buf, uncomp_buf + offset_in_cluster, nb_sectors * 512);
|
1194 | 2b2c8c5d | Fam Zheng | ret = 0;
|
1195 | 2b2c8c5d | Fam Zheng | |
1196 | 2b2c8c5d | Fam Zheng | out:
|
1197 | 2b2c8c5d | Fam Zheng | g_free(uncomp_buf); |
1198 | 2b2c8c5d | Fam Zheng | g_free(cluster_buf); |
1199 | 2b2c8c5d | Fam Zheng | return ret;
|
1200 | dd3f6ee2 | Fam Zheng | } |
1201 | dd3f6ee2 | Fam Zheng | |
1202 | 5fafdf24 | ths | static int vmdk_read(BlockDriverState *bs, int64_t sector_num, |
1203 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
1204 | ea2384d3 | bellard | { |
1205 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
1206 | b3976d3c | Fam Zheng | int ret;
|
1207 | b3976d3c | Fam Zheng | uint64_t n, index_in_cluster; |
1208 | b1649fae | Gerhard Wiesinger | uint64_t extent_begin_sector, extent_relative_sector_num; |
1209 | b3976d3c | Fam Zheng | VmdkExtent *extent = NULL;
|
1210 | ea2384d3 | bellard | uint64_t cluster_offset; |
1211 | 5f4da8c0 | ths | |
1212 | ea2384d3 | bellard | while (nb_sectors > 0) { |
1213 | b3976d3c | Fam Zheng | extent = find_extent(s, sector_num, extent); |
1214 | b3976d3c | Fam Zheng | if (!extent) {
|
1215 | b3976d3c | Fam Zheng | return -EIO;
|
1216 | b3976d3c | Fam Zheng | } |
1217 | 91b85bd3 | Fam Zheng | ret = get_cluster_offset( |
1218 | 91b85bd3 | Fam Zheng | bs, extent, NULL,
|
1219 | 91b85bd3 | Fam Zheng | sector_num << 9, 0, &cluster_offset); |
1220 | b1649fae | Gerhard Wiesinger | extent_begin_sector = extent->end_sector - extent->sectors; |
1221 | b1649fae | Gerhard Wiesinger | extent_relative_sector_num = sector_num - extent_begin_sector; |
1222 | b1649fae | Gerhard Wiesinger | index_in_cluster = extent_relative_sector_num % extent->cluster_sectors; |
1223 | b3976d3c | Fam Zheng | n = extent->cluster_sectors - index_in_cluster; |
1224 | ae261c86 | Fam Zheng | if (n > nb_sectors) {
|
1225 | ea2384d3 | bellard | n = nb_sectors; |
1226 | ae261c86 | Fam Zheng | } |
1227 | 14ead646 | Fam Zheng | if (ret != VMDK_OK) {
|
1228 | 91b85bd3 | Fam Zheng | /* if not allocated, try to read from parent image, if exist */
|
1229 | 14ead646 | Fam Zheng | if (bs->backing_hd && ret != VMDK_ZEROED) {
|
1230 | ae261c86 | Fam Zheng | if (!vmdk_is_cid_valid(bs)) {
|
1231 | 7fa60fa3 | Fam Zheng | return -EINVAL;
|
1232 | ae261c86 | Fam Zheng | } |
1233 | b171271a | Kevin Wolf | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); |
1234 | ae261c86 | Fam Zheng | if (ret < 0) { |
1235 | 7fa60fa3 | Fam Zheng | return ret;
|
1236 | ae261c86 | Fam Zheng | } |
1237 | 5f4da8c0 | ths | } else {
|
1238 | 5f4da8c0 | ths | memset(buf, 0, 512 * n); |
1239 | 5f4da8c0 | ths | } |
1240 | ea2384d3 | bellard | } else {
|
1241 | dd3f6ee2 | Fam Zheng | ret = vmdk_read_extent(extent, |
1242 | dd3f6ee2 | Fam Zheng | cluster_offset, index_in_cluster * 512,
|
1243 | dd3f6ee2 | Fam Zheng | buf, n); |
1244 | dd3f6ee2 | Fam Zheng | if (ret) {
|
1245 | 7fa60fa3 | Fam Zheng | return ret;
|
1246 | 7fa60fa3 | Fam Zheng | } |
1247 | ea2384d3 | bellard | } |
1248 | ea2384d3 | bellard | nb_sectors -= n; |
1249 | ea2384d3 | bellard | sector_num += n; |
1250 | ea2384d3 | bellard | buf += n * 512;
|
1251 | ea2384d3 | bellard | } |
1252 | ea2384d3 | bellard | return 0; |
1253 | ea2384d3 | bellard | } |
1254 | ea2384d3 | bellard | |
1255 | 2914caa0 | Paolo Bonzini | static coroutine_fn int vmdk_co_read(BlockDriverState *bs, int64_t sector_num, |
1256 | 2914caa0 | Paolo Bonzini | uint8_t *buf, int nb_sectors)
|
1257 | 2914caa0 | Paolo Bonzini | { |
1258 | 2914caa0 | Paolo Bonzini | int ret;
|
1259 | 2914caa0 | Paolo Bonzini | BDRVVmdkState *s = bs->opaque; |
1260 | 2914caa0 | Paolo Bonzini | qemu_co_mutex_lock(&s->lock); |
1261 | 2914caa0 | Paolo Bonzini | ret = vmdk_read(bs, sector_num, buf, nb_sectors); |
1262 | 2914caa0 | Paolo Bonzini | qemu_co_mutex_unlock(&s->lock); |
1263 | 2914caa0 | Paolo Bonzini | return ret;
|
1264 | 2914caa0 | Paolo Bonzini | } |
1265 | 2914caa0 | Paolo Bonzini | |
1266 | cdeaf1f1 | Fam Zheng | /**
|
1267 | cdeaf1f1 | Fam Zheng | * vmdk_write:
|
1268 | cdeaf1f1 | Fam Zheng | * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
|
1269 | 8e507243 | Fam Zheng | * if possible, otherwise return -ENOTSUP.
|
1270 | 8e507243 | Fam Zheng | * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
|
1271 | 8e507243 | Fam Zheng | * with each cluster. By dry run we can find if the zero write
|
1272 | 8e507243 | Fam Zheng | * is possible without modifying image data.
|
1273 | cdeaf1f1 | Fam Zheng | *
|
1274 | cdeaf1f1 | Fam Zheng | * Returns: error code with 0 for success.
|
1275 | cdeaf1f1 | Fam Zheng | */
|
1276 | 5fafdf24 | ths | static int vmdk_write(BlockDriverState *bs, int64_t sector_num, |
1277 | cdeaf1f1 | Fam Zheng | const uint8_t *buf, int nb_sectors, |
1278 | cdeaf1f1 | Fam Zheng | bool zeroed, bool zero_dry_run) |
1279 | ea2384d3 | bellard | { |
1280 | ff1afc72 | bellard | BDRVVmdkState *s = bs->opaque; |
1281 | b3976d3c | Fam Zheng | VmdkExtent *extent = NULL;
|
1282 | 91b85bd3 | Fam Zheng | int n, ret;
|
1283 | b3976d3c | Fam Zheng | int64_t index_in_cluster; |
1284 | b1649fae | Gerhard Wiesinger | uint64_t extent_begin_sector, extent_relative_sector_num; |
1285 | ff1afc72 | bellard | uint64_t cluster_offset; |
1286 | b3976d3c | Fam Zheng | VmdkMetaData m_data; |
1287 | ff1afc72 | bellard | |
1288 | 630530a6 | ths | if (sector_num > bs->total_sectors) {
|
1289 | 630530a6 | ths | fprintf(stderr, |
1290 | 92868412 | j_mayer | "(VMDK) Wrong offset: sector_num=0x%" PRIx64
|
1291 | 92868412 | j_mayer | " total_sectors=0x%" PRIx64 "\n", |
1292 | 630530a6 | ths | sector_num, bs->total_sectors); |
1293 | 7fa60fa3 | Fam Zheng | return -EIO;
|
1294 | 630530a6 | ths | } |
1295 | 630530a6 | ths | |
1296 | ff1afc72 | bellard | while (nb_sectors > 0) { |
1297 | b3976d3c | Fam Zheng | extent = find_extent(s, sector_num, extent); |
1298 | b3976d3c | Fam Zheng | if (!extent) {
|
1299 | b3976d3c | Fam Zheng | return -EIO;
|
1300 | b3976d3c | Fam Zheng | } |
1301 | 91b85bd3 | Fam Zheng | ret = get_cluster_offset( |
1302 | b3976d3c | Fam Zheng | bs, |
1303 | b3976d3c | Fam Zheng | extent, |
1304 | b3976d3c | Fam Zheng | &m_data, |
1305 | 2b2c8c5d | Fam Zheng | sector_num << 9, !extent->compressed,
|
1306 | 91b85bd3 | Fam Zheng | &cluster_offset); |
1307 | 2b2c8c5d | Fam Zheng | if (extent->compressed) {
|
1308 | 65f74725 | Fam Zheng | if (ret == VMDK_OK) {
|
1309 | 2b2c8c5d | Fam Zheng | /* Refuse write to allocated cluster for streamOptimized */
|
1310 | 2b2c8c5d | Fam Zheng | fprintf(stderr, |
1311 | 2b2c8c5d | Fam Zheng | "VMDK: can't write to allocated cluster"
|
1312 | 2b2c8c5d | Fam Zheng | " for streamOptimized\n");
|
1313 | 2b2c8c5d | Fam Zheng | return -EIO;
|
1314 | 2b2c8c5d | Fam Zheng | } else {
|
1315 | 2b2c8c5d | Fam Zheng | /* allocate */
|
1316 | 2b2c8c5d | Fam Zheng | ret = get_cluster_offset( |
1317 | 2b2c8c5d | Fam Zheng | bs, |
1318 | 2b2c8c5d | Fam Zheng | extent, |
1319 | 2b2c8c5d | Fam Zheng | &m_data, |
1320 | 2b2c8c5d | Fam Zheng | sector_num << 9, 1, |
1321 | 2b2c8c5d | Fam Zheng | &cluster_offset); |
1322 | 2b2c8c5d | Fam Zheng | } |
1323 | 2b2c8c5d | Fam Zheng | } |
1324 | cdeaf1f1 | Fam Zheng | if (ret == VMDK_ERROR) {
|
1325 | 91b85bd3 | Fam Zheng | return -EINVAL;
|
1326 | b3976d3c | Fam Zheng | } |
1327 | b1649fae | Gerhard Wiesinger | extent_begin_sector = extent->end_sector - extent->sectors; |
1328 | b1649fae | Gerhard Wiesinger | extent_relative_sector_num = sector_num - extent_begin_sector; |
1329 | b1649fae | Gerhard Wiesinger | index_in_cluster = extent_relative_sector_num % extent->cluster_sectors; |
1330 | b3976d3c | Fam Zheng | n = extent->cluster_sectors - index_in_cluster; |
1331 | b3976d3c | Fam Zheng | if (n > nb_sectors) {
|
1332 | b3976d3c | Fam Zheng | n = nb_sectors; |
1333 | b3976d3c | Fam Zheng | } |
1334 | cdeaf1f1 | Fam Zheng | if (zeroed) {
|
1335 | cdeaf1f1 | Fam Zheng | /* Do zeroed write, buf is ignored */
|
1336 | cdeaf1f1 | Fam Zheng | if (extent->has_zero_grain &&
|
1337 | cdeaf1f1 | Fam Zheng | index_in_cluster == 0 &&
|
1338 | cdeaf1f1 | Fam Zheng | n >= extent->cluster_sectors) { |
1339 | cdeaf1f1 | Fam Zheng | n = extent->cluster_sectors; |
1340 | cdeaf1f1 | Fam Zheng | if (!zero_dry_run) {
|
1341 | cdeaf1f1 | Fam Zheng | m_data.offset = VMDK_GTE_ZEROED; |
1342 | cdeaf1f1 | Fam Zheng | /* update L2 tables */
|
1343 | cdeaf1f1 | Fam Zheng | if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
|
1344 | cdeaf1f1 | Fam Zheng | return -EIO;
|
1345 | cdeaf1f1 | Fam Zheng | } |
1346 | cdeaf1f1 | Fam Zheng | } |
1347 | cdeaf1f1 | Fam Zheng | } else {
|
1348 | cdeaf1f1 | Fam Zheng | return -ENOTSUP;
|
1349 | cdeaf1f1 | Fam Zheng | } |
1350 | cdeaf1f1 | Fam Zheng | } else {
|
1351 | cdeaf1f1 | Fam Zheng | ret = vmdk_write_extent(extent, |
1352 | cdeaf1f1 | Fam Zheng | cluster_offset, index_in_cluster * 512,
|
1353 | cdeaf1f1 | Fam Zheng | buf, n, sector_num); |
1354 | cdeaf1f1 | Fam Zheng | if (ret) {
|
1355 | cdeaf1f1 | Fam Zheng | return ret;
|
1356 | cdeaf1f1 | Fam Zheng | } |
1357 | cdeaf1f1 | Fam Zheng | if (m_data.valid) {
|
1358 | cdeaf1f1 | Fam Zheng | /* update L2 tables */
|
1359 | cdeaf1f1 | Fam Zheng | if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
|
1360 | cdeaf1f1 | Fam Zheng | return -EIO;
|
1361 | cdeaf1f1 | Fam Zheng | } |
1362 | b3976d3c | Fam Zheng | } |
1363 | 630530a6 | ths | } |
1364 | ff1afc72 | bellard | nb_sectors -= n; |
1365 | ff1afc72 | bellard | sector_num += n; |
1366 | ff1afc72 | bellard | buf += n * 512;
|
1367 | 5f4da8c0 | ths | |
1368 | ae261c86 | Fam Zheng | /* update CID on the first write every time the virtual disk is
|
1369 | ae261c86 | Fam Zheng | * opened */
|
1370 | 69b4d86d | Fam Zheng | if (!s->cid_updated) {
|
1371 | 99f1835d | Kevin Wolf | ret = vmdk_write_cid(bs, time(NULL));
|
1372 | 99f1835d | Kevin Wolf | if (ret < 0) { |
1373 | 99f1835d | Kevin Wolf | return ret;
|
1374 | 99f1835d | Kevin Wolf | } |
1375 | 69b4d86d | Fam Zheng | s->cid_updated = true;
|
1376 | 5f4da8c0 | ths | } |
1377 | ff1afc72 | bellard | } |
1378 | ff1afc72 | bellard | return 0; |
1379 | ea2384d3 | bellard | } |
1380 | ea2384d3 | bellard | |
1381 | e183ef75 | Paolo Bonzini | static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num, |
1382 | e183ef75 | Paolo Bonzini | const uint8_t *buf, int nb_sectors) |
1383 | e183ef75 | Paolo Bonzini | { |
1384 | e183ef75 | Paolo Bonzini | int ret;
|
1385 | e183ef75 | Paolo Bonzini | BDRVVmdkState *s = bs->opaque; |
1386 | e183ef75 | Paolo Bonzini | qemu_co_mutex_lock(&s->lock); |
1387 | cdeaf1f1 | Fam Zheng | ret = vmdk_write(bs, sector_num, buf, nb_sectors, false, false); |
1388 | cdeaf1f1 | Fam Zheng | qemu_co_mutex_unlock(&s->lock); |
1389 | cdeaf1f1 | Fam Zheng | return ret;
|
1390 | cdeaf1f1 | Fam Zheng | } |
1391 | cdeaf1f1 | Fam Zheng | |
1392 | cdeaf1f1 | Fam Zheng | static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs, |
1393 | cdeaf1f1 | Fam Zheng | int64_t sector_num, |
1394 | cdeaf1f1 | Fam Zheng | int nb_sectors)
|
1395 | cdeaf1f1 | Fam Zheng | { |
1396 | cdeaf1f1 | Fam Zheng | int ret;
|
1397 | cdeaf1f1 | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
1398 | cdeaf1f1 | Fam Zheng | qemu_co_mutex_lock(&s->lock); |
1399 | 8e507243 | Fam Zheng | /* write zeroes could fail if sectors not aligned to cluster, test it with
|
1400 | 8e507243 | Fam Zheng | * dry_run == true before really updating image */
|
1401 | cdeaf1f1 | Fam Zheng | ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, true); |
1402 | cdeaf1f1 | Fam Zheng | if (!ret) {
|
1403 | cdeaf1f1 | Fam Zheng | ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, false); |
1404 | cdeaf1f1 | Fam Zheng | } |
1405 | e183ef75 | Paolo Bonzini | qemu_co_mutex_unlock(&s->lock); |
1406 | e183ef75 | Paolo Bonzini | return ret;
|
1407 | e183ef75 | Paolo Bonzini | } |
1408 | e183ef75 | Paolo Bonzini | |
1409 | 6c031aac | Fam Zheng | static int vmdk_create_extent(const char *filename, int64_t filesize, |
1410 | 69e0b6df | Fam Zheng | bool flat, bool compress, bool zeroed_grain) |
1411 | 8979b227 | bellard | { |
1412 | f66fd6c3 | Fam Zheng | int ret, i;
|
1413 | f66fd6c3 | Fam Zheng | int fd = 0; |
1414 | 8979b227 | bellard | VMDK4Header header; |
1415 | 8979b227 | bellard | uint32_t tmp, magic, grains, gd_size, gt_size, gt_count; |
1416 | 0e7e1989 | Kevin Wolf | |
1417 | 6165f4d8 | Corey Bryant | fd = qemu_open(filename, |
1418 | 6165f4d8 | Corey Bryant | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
1419 | 6165f4d8 | Corey Bryant | 0644);
|
1420 | f66fd6c3 | Fam Zheng | if (fd < 0) { |
1421 | f66fd6c3 | Fam Zheng | return -errno;
|
1422 | 0e7e1989 | Kevin Wolf | } |
1423 | f66fd6c3 | Fam Zheng | if (flat) {
|
1424 | f66fd6c3 | Fam Zheng | ret = ftruncate(fd, filesize); |
1425 | f66fd6c3 | Fam Zheng | if (ret < 0) { |
1426 | f66fd6c3 | Fam Zheng | ret = -errno; |
1427 | f66fd6c3 | Fam Zheng | } |
1428 | f66fd6c3 | Fam Zheng | goto exit;
|
1429 | 5f4da8c0 | ths | } |
1430 | 8979b227 | bellard | magic = cpu_to_be32(VMDK4_MAGIC); |
1431 | 8979b227 | bellard | memset(&header, 0, sizeof(header)); |
1432 | 69e0b6df | Fam Zheng | header.version = zeroed_grain ? 2 : 1; |
1433 | 95b0aa42 | Fam Zheng | header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT |
1434 | 69e0b6df | Fam Zheng | | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
|
1435 | 69e0b6df | Fam Zheng | | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
|
1436 | 6c031aac | Fam Zheng | header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
|
1437 | f66fd6c3 | Fam Zheng | header.capacity = filesize / 512;
|
1438 | 16372ff0 | Alexander Graf | header.granularity = 128;
|
1439 | ca8804ce | Fam Zheng | header.num_gtes_per_gt = 512;
|
1440 | 8979b227 | bellard | |
1441 | f66fd6c3 | Fam Zheng | grains = (filesize / 512 + header.granularity - 1) / header.granularity; |
1442 | ca8804ce | Fam Zheng | gt_size = ((header.num_gtes_per_gt * sizeof(uint32_t)) + 511) >> 9; |
1443 | f66fd6c3 | Fam Zheng | gt_count = |
1444 | ca8804ce | Fam Zheng | (grains + header.num_gtes_per_gt - 1) / header.num_gtes_per_gt;
|
1445 | 8979b227 | bellard | gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9; |
1446 | 8979b227 | bellard | |
1447 | 8979b227 | bellard | header.desc_offset = 1;
|
1448 | 8979b227 | bellard | header.desc_size = 20;
|
1449 | 8979b227 | bellard | header.rgd_offset = header.desc_offset + header.desc_size; |
1450 | 8979b227 | bellard | header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count); |
1451 | 8979b227 | bellard | header.grain_offset = |
1452 | 8979b227 | bellard | ((header.gd_offset + gd_size + (gt_size * gt_count) + |
1453 | 8979b227 | bellard | header.granularity - 1) / header.granularity) *
|
1454 | 8979b227 | bellard | header.granularity; |
1455 | 16372ff0 | Alexander Graf | /* swap endianness for all header fields */
|
1456 | 16372ff0 | Alexander Graf | header.version = cpu_to_le32(header.version); |
1457 | 16372ff0 | Alexander Graf | header.flags = cpu_to_le32(header.flags); |
1458 | 16372ff0 | Alexander Graf | header.capacity = cpu_to_le64(header.capacity); |
1459 | 16372ff0 | Alexander Graf | header.granularity = cpu_to_le64(header.granularity); |
1460 | ca8804ce | Fam Zheng | header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt); |
1461 | 8979b227 | bellard | header.desc_offset = cpu_to_le64(header.desc_offset); |
1462 | 8979b227 | bellard | header.desc_size = cpu_to_le64(header.desc_size); |
1463 | 8979b227 | bellard | header.rgd_offset = cpu_to_le64(header.rgd_offset); |
1464 | 8979b227 | bellard | header.gd_offset = cpu_to_le64(header.gd_offset); |
1465 | 8979b227 | bellard | header.grain_offset = cpu_to_le64(header.grain_offset); |
1466 | 6c031aac | Fam Zheng | header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm); |
1467 | 8979b227 | bellard | |
1468 | 8979b227 | bellard | header.check_bytes[0] = 0xa; |
1469 | 8979b227 | bellard | header.check_bytes[1] = 0x20; |
1470 | 8979b227 | bellard | header.check_bytes[2] = 0xd; |
1471 | 8979b227 | bellard | header.check_bytes[3] = 0xa; |
1472 | 3b46e624 | ths | |
1473 | 3b46e624 | ths | /* write all the data */
|
1474 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, &magic, sizeof(magic));
|
1475 | 1640366c | Kirill A. Shutemov | if (ret != sizeof(magic)) { |
1476 | b781cce5 | Juan Quintela | ret = -errno; |
1477 | 1640366c | Kirill A. Shutemov | goto exit;
|
1478 | 1640366c | Kirill A. Shutemov | } |
1479 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, &header, sizeof(header));
|
1480 | 1640366c | Kirill A. Shutemov | if (ret != sizeof(header)) { |
1481 | b781cce5 | Juan Quintela | ret = -errno; |
1482 | 1640366c | Kirill A. Shutemov | goto exit;
|
1483 | 1640366c | Kirill A. Shutemov | } |
1484 | 8979b227 | bellard | |
1485 | 16372ff0 | Alexander Graf | ret = ftruncate(fd, le64_to_cpu(header.grain_offset) << 9);
|
1486 | 1640366c | Kirill A. Shutemov | if (ret < 0) { |
1487 | b781cce5 | Juan Quintela | ret = -errno; |
1488 | 1640366c | Kirill A. Shutemov | goto exit;
|
1489 | 1640366c | Kirill A. Shutemov | } |
1490 | 8979b227 | bellard | |
1491 | 8979b227 | bellard | /* write grain directory */
|
1492 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
|
1493 | 16372ff0 | Alexander Graf | for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_size; |
1494 | 1640366c | Kirill A. Shutemov | i < gt_count; i++, tmp += gt_size) { |
1495 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, &tmp, sizeof(tmp));
|
1496 | 1640366c | Kirill A. Shutemov | if (ret != sizeof(tmp)) { |
1497 | b781cce5 | Juan Quintela | ret = -errno; |
1498 | 1640366c | Kirill A. Shutemov | goto exit;
|
1499 | 1640366c | Kirill A. Shutemov | } |
1500 | 1640366c | Kirill A. Shutemov | } |
1501 | 3b46e624 | ths | |
1502 | 8979b227 | bellard | /* write backup grain directory */
|
1503 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
|
1504 | 16372ff0 | Alexander Graf | for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_size; |
1505 | 1640366c | Kirill A. Shutemov | i < gt_count; i++, tmp += gt_size) { |
1506 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, &tmp, sizeof(tmp));
|
1507 | 1640366c | Kirill A. Shutemov | if (ret != sizeof(tmp)) { |
1508 | b781cce5 | Juan Quintela | ret = -errno; |
1509 | 1640366c | Kirill A. Shutemov | goto exit;
|
1510 | 1640366c | Kirill A. Shutemov | } |
1511 | 1640366c | Kirill A. Shutemov | } |
1512 | 8979b227 | bellard | |
1513 | f66fd6c3 | Fam Zheng | ret = 0;
|
1514 | f66fd6c3 | Fam Zheng | exit:
|
1515 | 2e1e79da | Corey Bryant | qemu_close(fd); |
1516 | f66fd6c3 | Fam Zheng | return ret;
|
1517 | f66fd6c3 | Fam Zheng | } |
1518 | f66fd6c3 | Fam Zheng | |
1519 | f66fd6c3 | Fam Zheng | static int filename_decompose(const char *filename, char *path, char *prefix, |
1520 | f66fd6c3 | Fam Zheng | char *postfix, size_t buf_len)
|
1521 | f66fd6c3 | Fam Zheng | { |
1522 | f66fd6c3 | Fam Zheng | const char *p, *q; |
1523 | f66fd6c3 | Fam Zheng | |
1524 | f66fd6c3 | Fam Zheng | if (filename == NULL || !strlen(filename)) { |
1525 | f66fd6c3 | Fam Zheng | fprintf(stderr, "Vmdk: no filename provided.\n");
|
1526 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
1527 | f66fd6c3 | Fam Zheng | } |
1528 | f66fd6c3 | Fam Zheng | p = strrchr(filename, '/');
|
1529 | f66fd6c3 | Fam Zheng | if (p == NULL) { |
1530 | f66fd6c3 | Fam Zheng | p = strrchr(filename, '\\');
|
1531 | f66fd6c3 | Fam Zheng | } |
1532 | f66fd6c3 | Fam Zheng | if (p == NULL) { |
1533 | f66fd6c3 | Fam Zheng | p = strrchr(filename, ':');
|
1534 | f66fd6c3 | Fam Zheng | } |
1535 | f66fd6c3 | Fam Zheng | if (p != NULL) { |
1536 | f66fd6c3 | Fam Zheng | p++; |
1537 | f66fd6c3 | Fam Zheng | if (p - filename >= buf_len) {
|
1538 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
1539 | f66fd6c3 | Fam Zheng | } |
1540 | f66fd6c3 | Fam Zheng | pstrcpy(path, p - filename + 1, filename);
|
1541 | f66fd6c3 | Fam Zheng | } else {
|
1542 | f66fd6c3 | Fam Zheng | p = filename; |
1543 | f66fd6c3 | Fam Zheng | path[0] = '\0'; |
1544 | f66fd6c3 | Fam Zheng | } |
1545 | f66fd6c3 | Fam Zheng | q = strrchr(p, '.');
|
1546 | f66fd6c3 | Fam Zheng | if (q == NULL) { |
1547 | f66fd6c3 | Fam Zheng | pstrcpy(prefix, buf_len, p); |
1548 | f66fd6c3 | Fam Zheng | postfix[0] = '\0'; |
1549 | f66fd6c3 | Fam Zheng | } else {
|
1550 | f66fd6c3 | Fam Zheng | if (q - p >= buf_len) {
|
1551 | 65f74725 | Fam Zheng | return VMDK_ERROR;
|
1552 | f66fd6c3 | Fam Zheng | } |
1553 | f66fd6c3 | Fam Zheng | pstrcpy(prefix, q - p + 1, p);
|
1554 | f66fd6c3 | Fam Zheng | pstrcpy(postfix, buf_len, q); |
1555 | f66fd6c3 | Fam Zheng | } |
1556 | 65f74725 | Fam Zheng | return VMDK_OK;
|
1557 | f66fd6c3 | Fam Zheng | } |
1558 | f66fd6c3 | Fam Zheng | |
1559 | d5124c00 | Max Reitz | static int vmdk_create(const char *filename, QEMUOptionParameter *options, |
1560 | d5124c00 | Max Reitz | Error **errp) |
1561 | f66fd6c3 | Fam Zheng | { |
1562 | f66fd6c3 | Fam Zheng | int fd, idx = 0; |
1563 | f66fd6c3 | Fam Zheng | char desc[BUF_SIZE];
|
1564 | f66fd6c3 | Fam Zheng | int64_t total_size = 0, filesize;
|
1565 | 7f2039f6 | Othmar Pasteka | const char *adapter_type = NULL; |
1566 | f66fd6c3 | Fam Zheng | const char *backing_file = NULL; |
1567 | f66fd6c3 | Fam Zheng | const char *fmt = NULL; |
1568 | f66fd6c3 | Fam Zheng | int flags = 0; |
1569 | f66fd6c3 | Fam Zheng | int ret = 0; |
1570 | 6c031aac | Fam Zheng | bool flat, split, compress;
|
1571 | f66fd6c3 | Fam Zheng | char ext_desc_lines[BUF_SIZE] = ""; |
1572 | f66fd6c3 | Fam Zheng | char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];
|
1573 | f66fd6c3 | Fam Zheng | const int64_t split_size = 0x80000000; /* VMDK has constant split size */ |
1574 | f66fd6c3 | Fam Zheng | const char *desc_extent_line; |
1575 | f66fd6c3 | Fam Zheng | char parent_desc_line[BUF_SIZE] = ""; |
1576 | f66fd6c3 | Fam Zheng | uint32_t parent_cid = 0xffffffff;
|
1577 | 7f2039f6 | Othmar Pasteka | uint32_t number_heads = 16;
|
1578 | 69e0b6df | Fam Zheng | bool zeroed_grain = false; |
1579 | f66fd6c3 | Fam Zheng | const char desc_template[] = |
1580 | f66fd6c3 | Fam Zheng | "# Disk DescriptorFile\n"
|
1581 | f66fd6c3 | Fam Zheng | "version=1\n"
|
1582 | f66fd6c3 | Fam Zheng | "CID=%x\n"
|
1583 | f66fd6c3 | Fam Zheng | "parentCID=%x\n"
|
1584 | f66fd6c3 | Fam Zheng | "createType=\"%s\"\n"
|
1585 | f66fd6c3 | Fam Zheng | "%s"
|
1586 | f66fd6c3 | Fam Zheng | "\n"
|
1587 | f66fd6c3 | Fam Zheng | "# Extent description\n"
|
1588 | f66fd6c3 | Fam Zheng | "%s"
|
1589 | f66fd6c3 | Fam Zheng | "\n"
|
1590 | f66fd6c3 | Fam Zheng | "# The Disk Data Base\n"
|
1591 | f66fd6c3 | Fam Zheng | "#DDB\n"
|
1592 | f66fd6c3 | Fam Zheng | "\n"
|
1593 | f66fd6c3 | Fam Zheng | "ddb.virtualHWVersion = \"%d\"\n"
|
1594 | f66fd6c3 | Fam Zheng | "ddb.geometry.cylinders = \"%" PRId64 "\"\n" |
1595 | 7f2039f6 | Othmar Pasteka | "ddb.geometry.heads = \"%d\"\n"
|
1596 | f66fd6c3 | Fam Zheng | "ddb.geometry.sectors = \"63\"\n"
|
1597 | 7f2039f6 | Othmar Pasteka | "ddb.adapterType = \"%s\"\n";
|
1598 | 34b5d2c6 | Max Reitz | Error *local_err = NULL;
|
1599 | f66fd6c3 | Fam Zheng | |
1600 | f66fd6c3 | Fam Zheng | if (filename_decompose(filename, path, prefix, postfix, PATH_MAX)) {
|
1601 | f66fd6c3 | Fam Zheng | return -EINVAL;
|
1602 | f66fd6c3 | Fam Zheng | } |
1603 | f66fd6c3 | Fam Zheng | /* Read out options */
|
1604 | f66fd6c3 | Fam Zheng | while (options && options->name) {
|
1605 | f66fd6c3 | Fam Zheng | if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
1606 | f66fd6c3 | Fam Zheng | total_size = options->value.n; |
1607 | 7f2039f6 | Othmar Pasteka | } else if (!strcmp(options->name, BLOCK_OPT_ADAPTER_TYPE)) { |
1608 | 7f2039f6 | Othmar Pasteka | adapter_type = options->value.s; |
1609 | f66fd6c3 | Fam Zheng | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { |
1610 | f66fd6c3 | Fam Zheng | backing_file = options->value.s; |
1611 | f66fd6c3 | Fam Zheng | } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) { |
1612 | f66fd6c3 | Fam Zheng | flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;
|
1613 | f66fd6c3 | Fam Zheng | } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) { |
1614 | f66fd6c3 | Fam Zheng | fmt = options->value.s; |
1615 | 69e0b6df | Fam Zheng | } else if (!strcmp(options->name, BLOCK_OPT_ZEROED_GRAIN)) { |
1616 | 69e0b6df | Fam Zheng | zeroed_grain |= options->value.n; |
1617 | f66fd6c3 | Fam Zheng | } |
1618 | f66fd6c3 | Fam Zheng | options++; |
1619 | f66fd6c3 | Fam Zheng | } |
1620 | 7f2039f6 | Othmar Pasteka | if (!adapter_type) {
|
1621 | 7f2039f6 | Othmar Pasteka | adapter_type = "ide";
|
1622 | 7f2039f6 | Othmar Pasteka | } else if (strcmp(adapter_type, "ide") && |
1623 | 7f2039f6 | Othmar Pasteka | strcmp(adapter_type, "buslogic") &&
|
1624 | 7f2039f6 | Othmar Pasteka | strcmp(adapter_type, "lsilogic") &&
|
1625 | 7f2039f6 | Othmar Pasteka | strcmp(adapter_type, "legacyESX")) {
|
1626 | 7f2039f6 | Othmar Pasteka | fprintf(stderr, "VMDK: Unknown adapter type: '%s'.\n", adapter_type);
|
1627 | 7f2039f6 | Othmar Pasteka | return -EINVAL;
|
1628 | 7f2039f6 | Othmar Pasteka | } |
1629 | 7f2039f6 | Othmar Pasteka | if (strcmp(adapter_type, "ide") != 0) { |
1630 | 7f2039f6 | Othmar Pasteka | /* that's the number of heads with which vmware operates when
|
1631 | 7f2039f6 | Othmar Pasteka | creating, exporting, etc. vmdk files with a non-ide adapter type */
|
1632 | 7f2039f6 | Othmar Pasteka | number_heads = 255;
|
1633 | 7f2039f6 | Othmar Pasteka | } |
1634 | f66fd6c3 | Fam Zheng | if (!fmt) {
|
1635 | f66fd6c3 | Fam Zheng | /* Default format to monolithicSparse */
|
1636 | f66fd6c3 | Fam Zheng | fmt = "monolithicSparse";
|
1637 | f66fd6c3 | Fam Zheng | } else if (strcmp(fmt, "monolithicFlat") && |
1638 | f66fd6c3 | Fam Zheng | strcmp(fmt, "monolithicSparse") &&
|
1639 | f66fd6c3 | Fam Zheng | strcmp(fmt, "twoGbMaxExtentSparse") &&
|
1640 | 6c031aac | Fam Zheng | strcmp(fmt, "twoGbMaxExtentFlat") &&
|
1641 | 6c031aac | Fam Zheng | strcmp(fmt, "streamOptimized")) {
|
1642 | f66fd6c3 | Fam Zheng | fprintf(stderr, "VMDK: Unknown subformat: %s\n", fmt);
|
1643 | f66fd6c3 | Fam Zheng | return -EINVAL;
|
1644 | f66fd6c3 | Fam Zheng | } |
1645 | f66fd6c3 | Fam Zheng | split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
|
1646 | f66fd6c3 | Fam Zheng | strcmp(fmt, "twoGbMaxExtentSparse"));
|
1647 | f66fd6c3 | Fam Zheng | flat = !(strcmp(fmt, "monolithicFlat") &&
|
1648 | f66fd6c3 | Fam Zheng | strcmp(fmt, "twoGbMaxExtentFlat"));
|
1649 | 6c031aac | Fam Zheng | compress = !strcmp(fmt, "streamOptimized");
|
1650 | f66fd6c3 | Fam Zheng | if (flat) {
|
1651 | f66fd6c3 | Fam Zheng | desc_extent_line = "RW %lld FLAT \"%s\" 0\n";
|
1652 | f66fd6c3 | Fam Zheng | } else {
|
1653 | f66fd6c3 | Fam Zheng | desc_extent_line = "RW %lld SPARSE \"%s\"\n";
|
1654 | f66fd6c3 | Fam Zheng | } |
1655 | f66fd6c3 | Fam Zheng | if (flat && backing_file) {
|
1656 | f66fd6c3 | Fam Zheng | /* not supporting backing file for flat image */
|
1657 | f66fd6c3 | Fam Zheng | return -ENOTSUP;
|
1658 | f66fd6c3 | Fam Zheng | } |
1659 | f66fd6c3 | Fam Zheng | if (backing_file) {
|
1660 | f66fd6c3 | Fam Zheng | BlockDriverState *bs = bdrv_new("");
|
1661 | 34b5d2c6 | Max Reitz | ret = bdrv_open(bs, backing_file, NULL, 0, NULL, &local_err); |
1662 | f66fd6c3 | Fam Zheng | if (ret != 0) { |
1663 | 34b5d2c6 | Max Reitz | qerror_report_err(local_err); |
1664 | 34b5d2c6 | Max Reitz | error_free(local_err); |
1665 | 4f6fd349 | Fam Zheng | bdrv_unref(bs); |
1666 | f66fd6c3 | Fam Zheng | return ret;
|
1667 | f66fd6c3 | Fam Zheng | } |
1668 | f66fd6c3 | Fam Zheng | if (strcmp(bs->drv->format_name, "vmdk")) { |
1669 | 4f6fd349 | Fam Zheng | bdrv_unref(bs); |
1670 | f66fd6c3 | Fam Zheng | return -EINVAL;
|
1671 | f66fd6c3 | Fam Zheng | } |
1672 | f66fd6c3 | Fam Zheng | parent_cid = vmdk_read_cid(bs, 0);
|
1673 | 4f6fd349 | Fam Zheng | bdrv_unref(bs); |
1674 | f66fd6c3 | Fam Zheng | snprintf(parent_desc_line, sizeof(parent_desc_line),
|
1675 | 8ed610a1 | Fam Zheng | "parentFileNameHint=\"%s\"", backing_file);
|
1676 | f66fd6c3 | Fam Zheng | } |
1677 | f66fd6c3 | Fam Zheng | |
1678 | f66fd6c3 | Fam Zheng | /* Create extents */
|
1679 | f66fd6c3 | Fam Zheng | filesize = total_size; |
1680 | f66fd6c3 | Fam Zheng | while (filesize > 0) { |
1681 | f66fd6c3 | Fam Zheng | char desc_line[BUF_SIZE];
|
1682 | f66fd6c3 | Fam Zheng | char ext_filename[PATH_MAX];
|
1683 | f66fd6c3 | Fam Zheng | char desc_filename[PATH_MAX];
|
1684 | f66fd6c3 | Fam Zheng | int64_t size = filesize; |
1685 | f66fd6c3 | Fam Zheng | |
1686 | f66fd6c3 | Fam Zheng | if (split && size > split_size) {
|
1687 | f66fd6c3 | Fam Zheng | size = split_size; |
1688 | f66fd6c3 | Fam Zheng | } |
1689 | f66fd6c3 | Fam Zheng | if (split) {
|
1690 | f66fd6c3 | Fam Zheng | snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s", |
1691 | f66fd6c3 | Fam Zheng | prefix, flat ? 'f' : 's', ++idx, postfix); |
1692 | f66fd6c3 | Fam Zheng | } else if (flat) { |
1693 | f66fd6c3 | Fam Zheng | snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s", |
1694 | f66fd6c3 | Fam Zheng | prefix, postfix); |
1695 | f66fd6c3 | Fam Zheng | } else {
|
1696 | f66fd6c3 | Fam Zheng | snprintf(desc_filename, sizeof(desc_filename), "%s%s", |
1697 | f66fd6c3 | Fam Zheng | prefix, postfix); |
1698 | f66fd6c3 | Fam Zheng | } |
1699 | f66fd6c3 | Fam Zheng | snprintf(ext_filename, sizeof(ext_filename), "%s%s", |
1700 | f66fd6c3 | Fam Zheng | path, desc_filename); |
1701 | f66fd6c3 | Fam Zheng | |
1702 | 69e0b6df | Fam Zheng | if (vmdk_create_extent(ext_filename, size,
|
1703 | 69e0b6df | Fam Zheng | flat, compress, zeroed_grain)) { |
1704 | f66fd6c3 | Fam Zheng | return -EINVAL;
|
1705 | f66fd6c3 | Fam Zheng | } |
1706 | f66fd6c3 | Fam Zheng | filesize -= size; |
1707 | f66fd6c3 | Fam Zheng | |
1708 | f66fd6c3 | Fam Zheng | /* Format description line */
|
1709 | f66fd6c3 | Fam Zheng | snprintf(desc_line, sizeof(desc_line),
|
1710 | f66fd6c3 | Fam Zheng | desc_extent_line, size / 512, desc_filename);
|
1711 | f66fd6c3 | Fam Zheng | pstrcat(ext_desc_lines, sizeof(ext_desc_lines), desc_line);
|
1712 | f66fd6c3 | Fam Zheng | } |
1713 | f66fd6c3 | Fam Zheng | /* generate descriptor file */
|
1714 | f66fd6c3 | Fam Zheng | snprintf(desc, sizeof(desc), desc_template,
|
1715 | f66fd6c3 | Fam Zheng | (unsigned int)time(NULL), |
1716 | f66fd6c3 | Fam Zheng | parent_cid, |
1717 | f66fd6c3 | Fam Zheng | fmt, |
1718 | f66fd6c3 | Fam Zheng | parent_desc_line, |
1719 | f66fd6c3 | Fam Zheng | ext_desc_lines, |
1720 | f66fd6c3 | Fam Zheng | (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4), |
1721 | 7f2039f6 | Othmar Pasteka | total_size / (int64_t)(63 * number_heads * 512), number_heads, |
1722 | 7f2039f6 | Othmar Pasteka | adapter_type); |
1723 | f66fd6c3 | Fam Zheng | if (split || flat) {
|
1724 | 6165f4d8 | Corey Bryant | fd = qemu_open(filename, |
1725 | 6165f4d8 | Corey Bryant | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
1726 | 6165f4d8 | Corey Bryant | 0644);
|
1727 | f66fd6c3 | Fam Zheng | } else {
|
1728 | 6165f4d8 | Corey Bryant | fd = qemu_open(filename, |
1729 | 6165f4d8 | Corey Bryant | O_WRONLY | O_BINARY | O_LARGEFILE, |
1730 | 6165f4d8 | Corey Bryant | 0644);
|
1731 | f66fd6c3 | Fam Zheng | } |
1732 | f66fd6c3 | Fam Zheng | if (fd < 0) { |
1733 | f66fd6c3 | Fam Zheng | return -errno;
|
1734 | f66fd6c3 | Fam Zheng | } |
1735 | f66fd6c3 | Fam Zheng | /* the descriptor offset = 0x200 */
|
1736 | f66fd6c3 | Fam Zheng | if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) { |
1737 | f66fd6c3 | Fam Zheng | ret = -errno; |
1738 | f66fd6c3 | Fam Zheng | goto exit;
|
1739 | f66fd6c3 | Fam Zheng | } |
1740 | 1640366c | Kirill A. Shutemov | ret = qemu_write_full(fd, desc, strlen(desc)); |
1741 | 1640366c | Kirill A. Shutemov | if (ret != strlen(desc)) {
|
1742 | b781cce5 | Juan Quintela | ret = -errno; |
1743 | 1640366c | Kirill A. Shutemov | goto exit;
|
1744 | 1640366c | Kirill A. Shutemov | } |
1745 | 1640366c | Kirill A. Shutemov | ret = 0;
|
1746 | 1640366c | Kirill A. Shutemov | exit:
|
1747 | 2e1e79da | Corey Bryant | qemu_close(fd); |
1748 | 1640366c | Kirill A. Shutemov | return ret;
|
1749 | 8979b227 | bellard | } |
1750 | 8979b227 | bellard | |
1751 | e2731add | bellard | static void vmdk_close(BlockDriverState *bs) |
1752 | ea2384d3 | bellard | { |
1753 | 2bc3166c | Kevin Wolf | BDRVVmdkState *s = bs->opaque; |
1754 | 2bc3166c | Kevin Wolf | |
1755 | b3976d3c | Fam Zheng | vmdk_free_extents(bs); |
1756 | 2bc3166c | Kevin Wolf | |
1757 | 2bc3166c | Kevin Wolf | migrate_del_blocker(s->migration_blocker); |
1758 | 2bc3166c | Kevin Wolf | error_free(s->migration_blocker); |
1759 | ea2384d3 | bellard | } |
1760 | ea2384d3 | bellard | |
1761 | 8b94ff85 | Paolo Bonzini | static coroutine_fn int vmdk_co_flush(BlockDriverState *bs) |
1762 | 7a6cba61 | pbrook | { |
1763 | 333c574d | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
1764 | 29cdb251 | Paolo Bonzini | int i, err;
|
1765 | 29cdb251 | Paolo Bonzini | int ret = 0; |
1766 | 333c574d | Fam Zheng | |
1767 | 333c574d | Fam Zheng | for (i = 0; i < s->num_extents; i++) { |
1768 | 8b94ff85 | Paolo Bonzini | err = bdrv_co_flush(s->extents[i].file); |
1769 | 333c574d | Fam Zheng | if (err < 0) { |
1770 | 333c574d | Fam Zheng | ret = err; |
1771 | 333c574d | Fam Zheng | } |
1772 | 333c574d | Fam Zheng | } |
1773 | 333c574d | Fam Zheng | return ret;
|
1774 | 7a6cba61 | pbrook | } |
1775 | 7a6cba61 | pbrook | |
1776 | 4a1d5e1f | Fam Zheng | static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
|
1777 | 4a1d5e1f | Fam Zheng | { |
1778 | 4a1d5e1f | Fam Zheng | int i;
|
1779 | 4a1d5e1f | Fam Zheng | int64_t ret = 0;
|
1780 | 4a1d5e1f | Fam Zheng | int64_t r; |
1781 | 4a1d5e1f | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
1782 | 4a1d5e1f | Fam Zheng | |
1783 | 4a1d5e1f | Fam Zheng | ret = bdrv_get_allocated_file_size(bs->file); |
1784 | 4a1d5e1f | Fam Zheng | if (ret < 0) { |
1785 | 4a1d5e1f | Fam Zheng | return ret;
|
1786 | 4a1d5e1f | Fam Zheng | } |
1787 | 4a1d5e1f | Fam Zheng | for (i = 0; i < s->num_extents; i++) { |
1788 | 4a1d5e1f | Fam Zheng | if (s->extents[i].file == bs->file) {
|
1789 | 4a1d5e1f | Fam Zheng | continue;
|
1790 | 4a1d5e1f | Fam Zheng | } |
1791 | 4a1d5e1f | Fam Zheng | r = bdrv_get_allocated_file_size(s->extents[i].file); |
1792 | 4a1d5e1f | Fam Zheng | if (r < 0) { |
1793 | 4a1d5e1f | Fam Zheng | return r;
|
1794 | 4a1d5e1f | Fam Zheng | } |
1795 | 4a1d5e1f | Fam Zheng | ret += r; |
1796 | 4a1d5e1f | Fam Zheng | } |
1797 | 4a1d5e1f | Fam Zheng | return ret;
|
1798 | 4a1d5e1f | Fam Zheng | } |
1799 | 0e7e1989 | Kevin Wolf | |
1800 | da7a50f9 | Fam Zheng | static int vmdk_has_zero_init(BlockDriverState *bs) |
1801 | da7a50f9 | Fam Zheng | { |
1802 | da7a50f9 | Fam Zheng | int i;
|
1803 | da7a50f9 | Fam Zheng | BDRVVmdkState *s = bs->opaque; |
1804 | da7a50f9 | Fam Zheng | |
1805 | da7a50f9 | Fam Zheng | /* If has a flat extent and its underlying storage doesn't have zero init,
|
1806 | da7a50f9 | Fam Zheng | * return 0. */
|
1807 | da7a50f9 | Fam Zheng | for (i = 0; i < s->num_extents; i++) { |
1808 | da7a50f9 | Fam Zheng | if (s->extents[i].flat) {
|
1809 | da7a50f9 | Fam Zheng | if (!bdrv_has_zero_init(s->extents[i].file)) {
|
1810 | da7a50f9 | Fam Zheng | return 0; |
1811 | da7a50f9 | Fam Zheng | } |
1812 | da7a50f9 | Fam Zheng | } |
1813 | da7a50f9 | Fam Zheng | } |
1814 | da7a50f9 | Fam Zheng | return 1; |
1815 | da7a50f9 | Fam Zheng | } |
1816 | da7a50f9 | Fam Zheng | |
1817 | 0e7e1989 | Kevin Wolf | static QEMUOptionParameter vmdk_create_options[] = {
|
1818 | db08adf5 | Kevin Wolf | { |
1819 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_SIZE, |
1820 | db08adf5 | Kevin Wolf | .type = OPT_SIZE, |
1821 | db08adf5 | Kevin Wolf | .help = "Virtual disk size"
|
1822 | db08adf5 | Kevin Wolf | }, |
1823 | db08adf5 | Kevin Wolf | { |
1824 | 7f2039f6 | Othmar Pasteka | .name = BLOCK_OPT_ADAPTER_TYPE, |
1825 | 7f2039f6 | Othmar Pasteka | .type = OPT_STRING, |
1826 | 7f2039f6 | Othmar Pasteka | .help = "Virtual adapter type, can be one of "
|
1827 | 7f2039f6 | Othmar Pasteka | "ide (default), lsilogic, buslogic or legacyESX"
|
1828 | 7f2039f6 | Othmar Pasteka | }, |
1829 | 7f2039f6 | Othmar Pasteka | { |
1830 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_BACKING_FILE, |
1831 | db08adf5 | Kevin Wolf | .type = OPT_STRING, |
1832 | db08adf5 | Kevin Wolf | .help = "File name of a base image"
|
1833 | db08adf5 | Kevin Wolf | }, |
1834 | db08adf5 | Kevin Wolf | { |
1835 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_COMPAT6, |
1836 | db08adf5 | Kevin Wolf | .type = OPT_FLAG, |
1837 | db08adf5 | Kevin Wolf | .help = "VMDK version 6 image"
|
1838 | db08adf5 | Kevin Wolf | }, |
1839 | f66fd6c3 | Fam Zheng | { |
1840 | f66fd6c3 | Fam Zheng | .name = BLOCK_OPT_SUBFMT, |
1841 | f66fd6c3 | Fam Zheng | .type = OPT_STRING, |
1842 | f66fd6c3 | Fam Zheng | .help = |
1843 | f66fd6c3 | Fam Zheng | "VMDK flat extent format, can be one of "
|
1844 | 6c031aac | Fam Zheng | "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
|
1845 | f66fd6c3 | Fam Zheng | }, |
1846 | 69e0b6df | Fam Zheng | { |
1847 | 69e0b6df | Fam Zheng | .name = BLOCK_OPT_ZEROED_GRAIN, |
1848 | 69e0b6df | Fam Zheng | .type = OPT_FLAG, |
1849 | 69e0b6df | Fam Zheng | .help = "Enable efficient zero writes using the zeroed-grain GTE feature"
|
1850 | 69e0b6df | Fam Zheng | }, |
1851 | 0e7e1989 | Kevin Wolf | { NULL }
|
1852 | 0e7e1989 | Kevin Wolf | }; |
1853 | 0e7e1989 | Kevin Wolf | |
1854 | 5efa9d5a | Anthony Liguori | static BlockDriver bdrv_vmdk = {
|
1855 | da7a50f9 | Fam Zheng | .format_name = "vmdk",
|
1856 | da7a50f9 | Fam Zheng | .instance_size = sizeof(BDRVVmdkState),
|
1857 | da7a50f9 | Fam Zheng | .bdrv_probe = vmdk_probe, |
1858 | da7a50f9 | Fam Zheng | .bdrv_open = vmdk_open, |
1859 | da7a50f9 | Fam Zheng | .bdrv_reopen_prepare = vmdk_reopen_prepare, |
1860 | da7a50f9 | Fam Zheng | .bdrv_read = vmdk_co_read, |
1861 | da7a50f9 | Fam Zheng | .bdrv_write = vmdk_co_write, |
1862 | da7a50f9 | Fam Zheng | .bdrv_co_write_zeroes = vmdk_co_write_zeroes, |
1863 | da7a50f9 | Fam Zheng | .bdrv_close = vmdk_close, |
1864 | da7a50f9 | Fam Zheng | .bdrv_create = vmdk_create, |
1865 | da7a50f9 | Fam Zheng | .bdrv_co_flush_to_disk = vmdk_co_flush, |
1866 | b6b8a333 | Paolo Bonzini | .bdrv_co_get_block_status = vmdk_co_get_block_status, |
1867 | da7a50f9 | Fam Zheng | .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size, |
1868 | da7a50f9 | Fam Zheng | .bdrv_has_zero_init = vmdk_has_zero_init, |
1869 | da7a50f9 | Fam Zheng | |
1870 | da7a50f9 | Fam Zheng | .create_options = vmdk_create_options, |
1871 | ea2384d3 | bellard | }; |
1872 | 5efa9d5a | Anthony Liguori | |
1873 | 5efa9d5a | Anthony Liguori | static void bdrv_vmdk_init(void) |
1874 | 5efa9d5a | Anthony Liguori | { |
1875 | 5efa9d5a | Anthony Liguori | bdrv_register(&bdrv_vmdk); |
1876 | 5efa9d5a | Anthony Liguori | } |
1877 | 5efa9d5a | Anthony Liguori | |
1878 | 5efa9d5a | Anthony Liguori | block_init(bdrv_vmdk_init); |