Line | Count | Source |
1 | | /* |
2 | | * Block driver for the VMDK format |
3 | | * |
4 | | * Copyright (c) 2004 Fabrice Bellard |
5 | | * Copyright (c) 2005 Filip Navara |
6 | | * |
7 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | | * of this software and associated documentation files (the "Software"), to deal |
9 | | * in the Software without restriction, including without limitation the rights |
10 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | | * copies of the Software, and to permit persons to whom the Software is |
12 | | * furnished to do so, subject to the following conditions: |
13 | | * |
14 | | * The above copyright notice and this permission notice shall be included in |
15 | | * all copies or substantial portions of the Software. |
16 | | * |
17 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
20 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23 | | * THE SOFTWARE. |
24 | | */ |
25 | | |
26 | | #include "qemu/osdep.h" |
27 | | #include "qapi/error.h" |
28 | | #include "block/block_int.h" |
29 | | #include "system/block-backend.h" |
30 | | #include "qobject/qdict.h" |
31 | | #include "qemu/error-report.h" |
32 | | #include "qemu/module.h" |
33 | | #include "qemu/option.h" |
34 | | #include "qemu/bswap.h" |
35 | | #include "qemu/memalign.h" |
36 | | #include "migration/blocker.h" |
37 | | #include "qemu/cutils.h" |
38 | | #include <zlib.h> |
39 | | |
40 | 0 | #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D') |
41 | 0 | #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V') |
42 | 0 | #define VMDK4_COMPRESSION_DEFLATE 1 |
43 | 0 | #define VMDK4_FLAG_NL_DETECT (1 << 0) |
44 | 0 | #define VMDK4_FLAG_RGD (1 << 1) |
45 | | /* Zeroed-grain enable bit */ |
46 | 0 | #define VMDK4_FLAG_ZERO_GRAIN (1 << 2) |
47 | 0 | #define VMDK4_FLAG_COMPRESS (1 << 16) |
48 | 0 | #define VMDK4_FLAG_MARKER (1 << 17) |
49 | 0 | #define VMDK4_GD_AT_END 0xffffffffffffffffULL |
50 | | |
51 | 0 | #define VMDK_EXTENT_MAX_SECTORS (1ULL << 32) |
52 | | |
53 | 0 | #define VMDK_GTE_ZEROED 0x1 |
54 | | |
55 | | /* VMDK internal error codes */ |
56 | 0 | #define VMDK_OK 0 |
57 | 0 | #define VMDK_ERROR (-1) |
58 | | /* Cluster not allocated */ |
59 | 0 | #define VMDK_UNALLOC (-2) |
60 | 0 | #define VMDK_ZEROED (-3) |
61 | | |
62 | 0 | #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain" |
63 | 0 | #define BLOCK_OPT_TOOLSVERSION "toolsversion" |
64 | | |
65 | | typedef struct { |
66 | | uint32_t version; |
67 | | uint32_t flags; |
68 | | uint32_t disk_sectors; |
69 | | uint32_t granularity; |
70 | | uint32_t l1dir_offset; |
71 | | uint32_t l1dir_size; |
72 | | uint32_t file_sectors; |
73 | | uint32_t cylinders; |
74 | | uint32_t heads; |
75 | | uint32_t sectors_per_track; |
76 | | } QEMU_PACKED VMDK3Header; |
77 | | |
78 | | typedef struct { |
79 | | uint32_t version; |
80 | | uint32_t flags; |
81 | | uint64_t capacity; |
82 | | uint64_t granularity; |
83 | | uint64_t desc_offset; |
84 | | uint64_t desc_size; |
85 | | /* Number of GrainTableEntries per GrainTable */ |
86 | | uint32_t num_gtes_per_gt; |
87 | | uint64_t rgd_offset; |
88 | | uint64_t gd_offset; |
89 | | uint64_t grain_offset; |
90 | | char filler[1]; |
91 | | char check_bytes[4]; |
92 | | uint16_t compressAlgorithm; |
93 | | } QEMU_PACKED VMDK4Header; |
94 | | |
95 | | typedef struct VMDKSESparseConstHeader { |
96 | | uint64_t magic; |
97 | | uint64_t version; |
98 | | uint64_t capacity; |
99 | | uint64_t grain_size; |
100 | | uint64_t grain_table_size; |
101 | | uint64_t flags; |
102 | | uint64_t reserved1; |
103 | | uint64_t reserved2; |
104 | | uint64_t reserved3; |
105 | | uint64_t reserved4; |
106 | | uint64_t volatile_header_offset; |
107 | | uint64_t volatile_header_size; |
108 | | uint64_t journal_header_offset; |
109 | | uint64_t journal_header_size; |
110 | | uint64_t journal_offset; |
111 | | uint64_t journal_size; |
112 | | uint64_t grain_dir_offset; |
113 | | uint64_t grain_dir_size; |
114 | | uint64_t grain_tables_offset; |
115 | | uint64_t grain_tables_size; |
116 | | uint64_t free_bitmap_offset; |
117 | | uint64_t free_bitmap_size; |
118 | | uint64_t backmap_offset; |
119 | | uint64_t backmap_size; |
120 | | uint64_t grains_offset; |
121 | | uint64_t grains_size; |
122 | | uint8_t pad[304]; |
123 | | } QEMU_PACKED VMDKSESparseConstHeader; |
124 | | |
125 | | typedef struct VMDKSESparseVolatileHeader { |
126 | | uint64_t magic; |
127 | | uint64_t free_gt_number; |
128 | | uint64_t next_txn_seq_number; |
129 | | uint64_t replay_journal; |
130 | | uint8_t pad[480]; |
131 | | } QEMU_PACKED VMDKSESparseVolatileHeader; |
132 | | |
133 | 0 | #define L2_CACHE_SIZE 16 |
134 | | |
135 | | typedef struct VmdkExtent { |
136 | | BdrvChild *file; |
137 | | bool flat; |
138 | | bool compressed; |
139 | | bool has_marker; |
140 | | bool has_zero_grain; |
141 | | bool sesparse; |
142 | | uint64_t sesparse_l2_tables_offset; |
143 | | uint64_t sesparse_clusters_offset; |
144 | | int32_t entry_size; |
145 | | int version; |
146 | | int64_t sectors; |
147 | | int64_t end_sector; |
148 | | int64_t flat_start_offset; |
149 | | int64_t l1_table_offset; |
150 | | int64_t l1_backup_table_offset; |
151 | | void *l1_table; |
152 | | uint32_t *l1_backup_table; |
153 | | unsigned int l1_size; |
154 | | uint32_t l1_entry_sectors; |
155 | | |
156 | | unsigned int l2_size; |
157 | | void *l2_cache; |
158 | | uint32_t l2_cache_offsets[L2_CACHE_SIZE]; |
159 | | uint32_t l2_cache_counts[L2_CACHE_SIZE]; |
160 | | |
161 | | int64_t cluster_sectors; |
162 | | int64_t next_cluster_sector; |
163 | | char *type; |
164 | | } VmdkExtent; |
165 | | |
166 | | typedef struct BDRVVmdkState { |
167 | | CoMutex lock; |
168 | | uint64_t desc_offset; |
169 | | bool cid_updated; |
170 | | bool cid_checked; |
171 | | uint32_t cid; |
172 | | uint32_t parent_cid; |
173 | | int num_extents; |
174 | | /* Extent array with num_extents entries, ascend ordered by address */ |
175 | | VmdkExtent *extents; |
176 | | Error *migration_blocker; |
177 | | char *create_type; |
178 | | } BDRVVmdkState; |
179 | | |
180 | | typedef struct BDRVVmdkReopenState { |
181 | | bool *extents_using_bs_file; |
182 | | } BDRVVmdkReopenState; |
183 | | |
184 | | typedef struct VmdkMetaData { |
185 | | unsigned int l1_index; |
186 | | unsigned int l2_index; |
187 | | unsigned int l2_offset; |
188 | | bool new_allocation; |
189 | | uint32_t *l2_cache_entry; |
190 | | } VmdkMetaData; |
191 | | |
192 | | typedef struct VmdkGrainMarker { |
193 | | uint64_t lba; |
194 | | uint32_t size; |
195 | | uint8_t data[]; |
196 | | } QEMU_PACKED VmdkGrainMarker; |
197 | | |
198 | | typedef struct EosMarker { |
199 | | uint64_t val; |
200 | | uint32_t size; |
201 | | uint32_t type; |
202 | | uint8_t pad[512 - 16]; |
203 | | } QEMU_PACKED EosMarker; |
204 | | |
205 | | enum { |
206 | | MARKER_END_OF_STREAM = 0, |
207 | | MARKER_GRAIN_TABLE = 1, |
208 | | MARKER_GRAIN_DIRECTORY = 2, |
209 | | MARKER_FOOTER = 3, |
210 | | }; |
211 | | |
212 | | static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) |
213 | 0 | { |
214 | 0 | uint32_t magic; |
215 | |
|
216 | 0 | if (buf_size < 4) { |
217 | 0 | return 0; |
218 | 0 | } |
219 | 0 | magic = be32_to_cpu(*(uint32_t *)buf); |
220 | 0 | if (magic == VMDK3_MAGIC || |
221 | 0 | magic == VMDK4_MAGIC) { |
222 | 0 | return 100; |
223 | 0 | } else { |
224 | 0 | const char *p = (const char *)buf; |
225 | 0 | const char *end = p + buf_size; |
226 | 0 | while (p < end) { |
227 | 0 | if (*p == '#') { |
228 | | /* skip comment line */ |
229 | 0 | while (p < end && *p != '\n') { |
230 | 0 | p++; |
231 | 0 | } |
232 | 0 | p++; |
233 | 0 | continue; |
234 | 0 | } |
235 | 0 | if (*p == ' ') { |
236 | 0 | while (p < end && *p == ' ') { |
237 | 0 | p++; |
238 | 0 | } |
239 | | /* skip '\r' if windows line endings used. */ |
240 | 0 | if (p < end && *p == '\r') { |
241 | 0 | p++; |
242 | 0 | } |
243 | | /* only accept blank lines before 'version=' line */ |
244 | 0 | if (p == end || *p != '\n') { |
245 | 0 | return 0; |
246 | 0 | } |
247 | 0 | p++; |
248 | 0 | continue; |
249 | 0 | } |
250 | 0 | if (end - p >= strlen("version=X\n")) { |
251 | 0 | if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 || |
252 | 0 | strncmp("version=2\n", p, strlen("version=2\n")) == 0 || |
253 | 0 | strncmp("version=3\n", p, strlen("version=3\n")) == 0) { |
254 | 0 | return 100; |
255 | 0 | } |
256 | 0 | } |
257 | 0 | if (end - p >= strlen("version=X\r\n")) { |
258 | 0 | if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 || |
259 | 0 | strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0 || |
260 | 0 | strncmp("version=3\r\n", p, strlen("version=3\r\n")) == 0) { |
261 | 0 | return 100; |
262 | 0 | } |
263 | 0 | } |
264 | 0 | return 0; |
265 | 0 | } |
266 | 0 | return 0; |
267 | 0 | } |
268 | 0 | } |
269 | | |
270 | 0 | #define SECTOR_SIZE 512 |
271 | 0 | #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */ |
272 | 0 | #define BUF_SIZE 4096 |
273 | | #define HEADER_SIZE 512 /* first sector of 512 bytes */ |
274 | | |
275 | | static void vmdk_free_extents(BlockDriverState *bs) |
276 | 0 | { |
277 | 0 | int i; |
278 | 0 | BDRVVmdkState *s = bs->opaque; |
279 | 0 | VmdkExtent *e; |
280 | |
|
281 | 0 | bdrv_graph_wrlock_drained(); |
282 | 0 | for (i = 0; i < s->num_extents; i++) { |
283 | 0 | e = &s->extents[i]; |
284 | 0 | g_free(e->l1_table); |
285 | 0 | g_free(e->l2_cache); |
286 | 0 | g_free(e->l1_backup_table); |
287 | 0 | g_free(e->type); |
288 | 0 | if (e->file != bs->file) { |
289 | 0 | bdrv_unref_child(bs, e->file); |
290 | 0 | } |
291 | 0 | } |
292 | 0 | bdrv_graph_wrunlock(); |
293 | |
|
294 | 0 | g_free(s->extents); |
295 | 0 | } |
296 | | |
297 | | static void vmdk_free_last_extent(BlockDriverState *bs) |
298 | 0 | { |
299 | 0 | BDRVVmdkState *s = bs->opaque; |
300 | |
|
301 | 0 | if (s->num_extents == 0) { |
302 | 0 | return; |
303 | 0 | } |
304 | 0 | s->num_extents--; |
305 | 0 | s->extents = g_renew(VmdkExtent, s->extents, s->num_extents); |
306 | 0 | } |
307 | | |
308 | | /* Return -ve errno, or 0 on success and write CID into *pcid. */ |
309 | | static int GRAPH_RDLOCK |
310 | | vmdk_read_cid(BlockDriverState *bs, int parent, uint32_t *pcid) |
311 | 0 | { |
312 | 0 | char *desc; |
313 | 0 | uint32_t cid; |
314 | 0 | const char *p_name, *cid_str; |
315 | 0 | size_t cid_str_size; |
316 | 0 | BDRVVmdkState *s = bs->opaque; |
317 | 0 | int ret; |
318 | |
|
319 | 0 | desc = g_malloc0(DESC_SIZE); |
320 | 0 | ret = bdrv_pread(bs->file, s->desc_offset, DESC_SIZE, desc, 0); |
321 | 0 | if (ret < 0) { |
322 | 0 | goto out; |
323 | 0 | } |
324 | | |
325 | 0 | if (parent) { |
326 | 0 | cid_str = "parentCID"; |
327 | 0 | cid_str_size = sizeof("parentCID"); |
328 | 0 | } else { |
329 | 0 | cid_str = "CID"; |
330 | 0 | cid_str_size = sizeof("CID"); |
331 | 0 | } |
332 | |
|
333 | 0 | desc[DESC_SIZE - 1] = '\0'; |
334 | 0 | p_name = strstr(desc, cid_str); |
335 | 0 | if (p_name == NULL) { |
336 | 0 | ret = -EINVAL; |
337 | 0 | goto out; |
338 | 0 | } |
339 | 0 | p_name += cid_str_size; |
340 | 0 | if (p_name >= desc + DESC_SIZE) { |
341 | 0 | ret = -EINVAL; |
342 | 0 | goto out; |
343 | 0 | } |
344 | 0 | if (sscanf(p_name, "%" SCNx32, &cid) != 1) { |
345 | 0 | ret = -EINVAL; |
346 | 0 | goto out; |
347 | 0 | } |
348 | 0 | *pcid = cid; |
349 | 0 | ret = 0; |
350 | |
|
351 | 0 | out: |
352 | 0 | g_free(desc); |
353 | 0 | return ret; |
354 | 0 | } |
355 | | |
356 | | static int coroutine_fn GRAPH_RDLOCK |
357 | | vmdk_write_cid(BlockDriverState *bs, uint32_t cid) |
358 | 0 | { |
359 | 0 | char *desc, *tmp_desc; |
360 | 0 | char *p_name, *tmp_str; |
361 | 0 | BDRVVmdkState *s = bs->opaque; |
362 | 0 | int ret = 0; |
363 | |
|
364 | 0 | size_t desc_buf_size; |
365 | |
|
366 | 0 | if (s->desc_offset == 0) { |
367 | 0 | desc_buf_size = bdrv_getlength(bs->file->bs); |
368 | 0 | if (desc_buf_size > 16ULL << 20) { |
369 | 0 | error_report("VMDK description file too big"); |
370 | 0 | return -EFBIG; |
371 | 0 | } |
372 | 0 | } else { |
373 | 0 | desc_buf_size = DESC_SIZE; |
374 | 0 | } |
375 | | |
376 | 0 | desc = g_malloc0(desc_buf_size); |
377 | 0 | tmp_desc = g_malloc0(desc_buf_size); |
378 | 0 | ret = bdrv_co_pread(bs->file, s->desc_offset, desc_buf_size, desc, 0); |
379 | 0 | if (ret < 0) { |
380 | 0 | goto out; |
381 | 0 | } |
382 | | |
383 | 0 | desc[desc_buf_size - 1] = '\0'; |
384 | 0 | tmp_str = strstr(desc, "parentCID"); |
385 | 0 | if (tmp_str == NULL) { |
386 | 0 | ret = -EINVAL; |
387 | 0 | goto out; |
388 | 0 | } |
389 | | |
390 | 0 | pstrcpy(tmp_desc, desc_buf_size, tmp_str); |
391 | 0 | p_name = strstr(desc, "CID"); |
392 | 0 | if (p_name != NULL) { |
393 | 0 | p_name += sizeof("CID"); |
394 | 0 | snprintf(p_name, desc_buf_size - (p_name - desc), "%" PRIx32 "\n", cid); |
395 | 0 | pstrcat(desc, desc_buf_size, tmp_desc); |
396 | 0 | } |
397 | |
|
398 | 0 | ret = bdrv_co_pwrite_sync(bs->file, s->desc_offset, desc_buf_size, desc, 0); |
399 | |
|
400 | 0 | out: |
401 | 0 | g_free(desc); |
402 | 0 | g_free(tmp_desc); |
403 | 0 | return ret; |
404 | 0 | } |
405 | | |
406 | | static int coroutine_fn GRAPH_RDLOCK vmdk_is_cid_valid(BlockDriverState *bs) |
407 | 0 | { |
408 | 0 | BDRVVmdkState *s = bs->opaque; |
409 | 0 | uint32_t cur_pcid; |
410 | |
|
411 | 0 | if (!s->cid_checked && bs->backing) { |
412 | 0 | BlockDriverState *p_bs = bs->backing->bs; |
413 | |
|
414 | 0 | if (strcmp(p_bs->drv->format_name, "vmdk")) { |
415 | | /* Backing file is not in vmdk format, so it does not have |
416 | | * a CID, which makes the overlay's parent CID invalid */ |
417 | 0 | return 0; |
418 | 0 | } |
419 | | |
420 | 0 | if (vmdk_read_cid(p_bs, 0, &cur_pcid) != 0) { |
421 | | /* read failure: report as not valid */ |
422 | 0 | return 0; |
423 | 0 | } |
424 | 0 | if (s->parent_cid != cur_pcid) { |
425 | | /* CID not valid */ |
426 | 0 | return 0; |
427 | 0 | } |
428 | 0 | } |
429 | 0 | s->cid_checked = true; |
430 | | /* CID valid */ |
431 | 0 | return 1; |
432 | 0 | } |
433 | | |
434 | | static int vmdk_reopen_prepare(BDRVReopenState *state, |
435 | | BlockReopenQueue *queue, Error **errp) |
436 | 0 | { |
437 | 0 | BDRVVmdkState *s; |
438 | 0 | BDRVVmdkReopenState *rs; |
439 | 0 | int i; |
440 | |
|
441 | 0 | GLOBAL_STATE_CODE(); |
442 | 0 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
443 | |
|
444 | 0 | assert(state != NULL); |
445 | 0 | assert(state->bs != NULL); |
446 | 0 | assert(state->opaque == NULL); |
447 | |
|
448 | 0 | s = state->bs->opaque; |
449 | |
|
450 | 0 | rs = g_new0(BDRVVmdkReopenState, 1); |
451 | 0 | state->opaque = rs; |
452 | | |
453 | | /* |
454 | | * Check whether there are any extents stored in bs->file; if bs->file |
455 | | * changes, we will need to update their .file pointers to follow suit |
456 | | */ |
457 | 0 | rs->extents_using_bs_file = g_new(bool, s->num_extents); |
458 | 0 | for (i = 0; i < s->num_extents; i++) { |
459 | 0 | rs->extents_using_bs_file[i] = s->extents[i].file == state->bs->file; |
460 | 0 | } |
461 | |
|
462 | 0 | return 0; |
463 | 0 | } |
464 | | |
465 | | static void vmdk_reopen_clean(BDRVReopenState *state) |
466 | 0 | { |
467 | 0 | BDRVVmdkReopenState *rs = state->opaque; |
468 | |
|
469 | 0 | g_free(rs->extents_using_bs_file); |
470 | 0 | g_free(rs); |
471 | 0 | state->opaque = NULL; |
472 | 0 | } |
473 | | |
474 | | static void vmdk_reopen_commit(BDRVReopenState *state) |
475 | 0 | { |
476 | 0 | BDRVVmdkState *s = state->bs->opaque; |
477 | 0 | BDRVVmdkReopenState *rs = state->opaque; |
478 | 0 | int i; |
479 | |
|
480 | 0 | GLOBAL_STATE_CODE(); |
481 | 0 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
482 | |
|
483 | 0 | for (i = 0; i < s->num_extents; i++) { |
484 | 0 | if (rs->extents_using_bs_file[i]) { |
485 | 0 | s->extents[i].file = state->bs->file; |
486 | 0 | } |
487 | 0 | } |
488 | |
|
489 | 0 | vmdk_reopen_clean(state); |
490 | 0 | } |
491 | | |
492 | | static void vmdk_reopen_abort(BDRVReopenState *state) |
493 | 0 | { |
494 | 0 | vmdk_reopen_clean(state); |
495 | 0 | } |
496 | | |
497 | | static int GRAPH_RDLOCK vmdk_parent_open(BlockDriverState *bs) |
498 | 0 | { |
499 | 0 | char *p_name; |
500 | 0 | char *desc; |
501 | 0 | BDRVVmdkState *s = bs->opaque; |
502 | 0 | int ret; |
503 | |
|
504 | 0 | desc = g_malloc0(DESC_SIZE + 1); |
505 | 0 | ret = bdrv_pread(bs->file, s->desc_offset, DESC_SIZE, desc, 0); |
506 | 0 | if (ret < 0) { |
507 | 0 | goto out; |
508 | 0 | } |
509 | | |
510 | 0 | p_name = strstr(desc, "parentFileNameHint"); |
511 | 0 | if (p_name != NULL) { |
512 | 0 | char *end_name; |
513 | |
|
514 | 0 | p_name += sizeof("parentFileNameHint") + 1; |
515 | 0 | if (p_name >= desc + DESC_SIZE + 1) { |
516 | 0 | ret = -EINVAL; |
517 | 0 | goto out; |
518 | 0 | } |
519 | 0 | end_name = strchr(p_name, '\"'); |
520 | 0 | if (end_name == NULL) { |
521 | 0 | ret = -EINVAL; |
522 | 0 | goto out; |
523 | 0 | } |
524 | 0 | if ((end_name - p_name) > sizeof(bs->auto_backing_file) - 1) { |
525 | 0 | ret = -EINVAL; |
526 | 0 | goto out; |
527 | 0 | } |
528 | | |
529 | 0 | pstrcpy(bs->auto_backing_file, end_name - p_name + 1, p_name); |
530 | 0 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), |
531 | 0 | bs->auto_backing_file); |
532 | 0 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), |
533 | 0 | "vmdk"); |
534 | 0 | } |
535 | | |
536 | 0 | out: |
537 | 0 | g_free(desc); |
538 | 0 | return ret; |
539 | 0 | } |
540 | | |
541 | | /* Create and append extent to the extent array. Return the added VmdkExtent |
542 | | * address. return NULL if allocation failed. */ |
543 | | static int vmdk_add_extent(BlockDriverState *bs, |
544 | | BdrvChild *file, bool flat, int64_t sectors, |
545 | | int64_t l1_offset, int64_t l1_backup_offset, |
546 | | uint32_t l1_size, |
547 | | int l2_size, uint64_t cluster_sectors, |
548 | | VmdkExtent **new_extent, |
549 | | Error **errp) |
550 | 0 | { |
551 | 0 | VmdkExtent *extent; |
552 | 0 | BDRVVmdkState *s = bs->opaque; |
553 | 0 | int64_t nb_sectors; |
554 | |
|
555 | 0 | if (!flat && cluster_sectors == 0) { |
556 | 0 | error_setg(errp, "Invalid zero granularity, image may be corrupt"); |
557 | 0 | return -EINVAL; |
558 | 0 | } |
559 | | |
560 | 0 | if (cluster_sectors > 0x200000) { |
561 | | /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */ |
562 | 0 | error_setg(errp, "Invalid granularity, image may be corrupt"); |
563 | 0 | return -EFBIG; |
564 | 0 | } |
565 | 0 | if (l1_size > 32 * 1024 * 1024) { |
566 | | /* |
567 | | * Although with big capacity and small l1_entry_sectors, we can get a |
568 | | * big l1_size, we don't want unbounded value to allocate the table. |
569 | | * Limit it to 32M, which is enough to store: |
570 | | * 8TB - for both VMDK3 & VMDK4 with |
571 | | * minimal cluster size: 512B |
572 | | * minimal L2 table size: 512 entries |
573 | | * 8 TB is still more than the maximal value supported for |
574 | | * VMDK3 & VMDK4 which is 2TB. |
575 | | * 64TB - for "ESXi seSparse Extent" |
576 | | * minimal cluster size: 512B (default is 4KB) |
577 | | * L2 table size: 4096 entries (const). |
578 | | * 64TB is more than the maximal value supported for |
579 | | * seSparse VMDKs (which is slightly less than 64TB) |
580 | | */ |
581 | 0 | error_setg(errp, "L1 size too big"); |
582 | 0 | return -EFBIG; |
583 | 0 | } |
584 | | |
585 | 0 | nb_sectors = bdrv_nb_sectors(file->bs); |
586 | 0 | if (nb_sectors < 0) { |
587 | 0 | return nb_sectors; |
588 | 0 | } |
589 | | |
590 | 0 | s->extents = g_renew(VmdkExtent, s->extents, s->num_extents + 1); |
591 | 0 | extent = &s->extents[s->num_extents]; |
592 | 0 | s->num_extents++; |
593 | |
|
594 | 0 | memset(extent, 0, sizeof(VmdkExtent)); |
595 | 0 | extent->file = file; |
596 | 0 | extent->flat = flat; |
597 | 0 | extent->sectors = sectors; |
598 | 0 | extent->l1_table_offset = l1_offset; |
599 | 0 | extent->l1_backup_table_offset = l1_backup_offset; |
600 | 0 | extent->l1_size = l1_size; |
601 | 0 | extent->l1_entry_sectors = l2_size * cluster_sectors; |
602 | 0 | extent->l2_size = l2_size; |
603 | 0 | extent->cluster_sectors = flat ? sectors : cluster_sectors; |
604 | 0 | extent->next_cluster_sector = ROUND_UP(nb_sectors, cluster_sectors); |
605 | 0 | extent->entry_size = sizeof(uint32_t); |
606 | |
|
607 | 0 | if (s->num_extents > 1) { |
608 | 0 | extent->end_sector = (*(extent - 1)).end_sector + extent->sectors; |
609 | 0 | } else { |
610 | 0 | extent->end_sector = extent->sectors; |
611 | 0 | } |
612 | 0 | bs->total_sectors = extent->end_sector; |
613 | 0 | if (new_extent) { |
614 | 0 | *new_extent = extent; |
615 | 0 | } |
616 | 0 | return 0; |
617 | 0 | } |
618 | | |
619 | | static int GRAPH_RDLOCK |
620 | | vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent, Error **errp) |
621 | 0 | { |
622 | 0 | int ret; |
623 | 0 | size_t l1_size; |
624 | 0 | int i; |
625 | | |
626 | | /* read the L1 table */ |
627 | 0 | l1_size = extent->l1_size * extent->entry_size; |
628 | 0 | extent->l1_table = g_try_malloc(l1_size); |
629 | 0 | if (l1_size && extent->l1_table == NULL) { |
630 | 0 | return -ENOMEM; |
631 | 0 | } |
632 | | |
633 | 0 | ret = bdrv_pread(extent->file, extent->l1_table_offset, l1_size, |
634 | 0 | extent->l1_table, 0); |
635 | 0 | if (ret < 0) { |
636 | 0 | bdrv_refresh_filename(extent->file->bs); |
637 | 0 | error_setg_errno(errp, -ret, |
638 | 0 | "Could not read l1 table from extent '%s'", |
639 | 0 | extent->file->bs->filename); |
640 | 0 | goto fail_l1; |
641 | 0 | } |
642 | 0 | for (i = 0; i < extent->l1_size; i++) { |
643 | 0 | if (extent->entry_size == sizeof(uint64_t)) { |
644 | 0 | le64_to_cpus((uint64_t *)extent->l1_table + i); |
645 | 0 | } else { |
646 | 0 | assert(extent->entry_size == sizeof(uint32_t)); |
647 | 0 | le32_to_cpus((uint32_t *)extent->l1_table + i); |
648 | 0 | } |
649 | 0 | } |
650 | |
|
651 | 0 | if (extent->l1_backup_table_offset) { |
652 | 0 | assert(!extent->sesparse); |
653 | 0 | extent->l1_backup_table = g_try_malloc(l1_size); |
654 | 0 | if (l1_size && extent->l1_backup_table == NULL) { |
655 | 0 | ret = -ENOMEM; |
656 | 0 | goto fail_l1; |
657 | 0 | } |
658 | 0 | ret = bdrv_pread(extent->file, extent->l1_backup_table_offset, |
659 | 0 | l1_size, extent->l1_backup_table, 0); |
660 | 0 | if (ret < 0) { |
661 | 0 | bdrv_refresh_filename(extent->file->bs); |
662 | 0 | error_setg_errno(errp, -ret, |
663 | 0 | "Could not read l1 backup table from extent '%s'", |
664 | 0 | extent->file->bs->filename); |
665 | 0 | goto fail_l1b; |
666 | 0 | } |
667 | 0 | for (i = 0; i < extent->l1_size; i++) { |
668 | 0 | le32_to_cpus(&extent->l1_backup_table[i]); |
669 | 0 | } |
670 | 0 | } |
671 | | |
672 | 0 | extent->l2_cache = |
673 | 0 | g_malloc(extent->entry_size * extent->l2_size * L2_CACHE_SIZE); |
674 | 0 | return 0; |
675 | 0 | fail_l1b: |
676 | 0 | g_free(extent->l1_backup_table); |
677 | 0 | fail_l1: |
678 | 0 | g_free(extent->l1_table); |
679 | 0 | return ret; |
680 | 0 | } |
681 | | |
682 | | static int GRAPH_RDLOCK |
683 | | vmdk_open_vmfs_sparse(BlockDriverState *bs, BdrvChild *file, int flags, |
684 | | Error **errp) |
685 | 0 | { |
686 | 0 | int ret; |
687 | 0 | uint32_t magic; |
688 | 0 | VMDK3Header header; |
689 | 0 | VmdkExtent *extent = NULL; |
690 | |
|
691 | 0 | ret = bdrv_pread(file, sizeof(magic), sizeof(header), &header, 0); |
692 | 0 | if (ret < 0) { |
693 | 0 | bdrv_refresh_filename(file->bs); |
694 | 0 | error_setg_errno(errp, -ret, |
695 | 0 | "Could not read header from file '%s'", |
696 | 0 | file->bs->filename); |
697 | 0 | return ret; |
698 | 0 | } |
699 | 0 | ret = vmdk_add_extent(bs, file, false, |
700 | 0 | le32_to_cpu(header.disk_sectors), |
701 | 0 | (int64_t)le32_to_cpu(header.l1dir_offset) << 9, |
702 | 0 | 0, |
703 | 0 | le32_to_cpu(header.l1dir_size), |
704 | 0 | 4096, |
705 | 0 | le32_to_cpu(header.granularity), |
706 | 0 | &extent, |
707 | 0 | errp); |
708 | 0 | if (ret < 0) { |
709 | 0 | return ret; |
710 | 0 | } |
711 | 0 | ret = vmdk_init_tables(bs, extent, errp); |
712 | 0 | if (ret) { |
713 | | /* free extent allocated by vmdk_add_extent */ |
714 | 0 | vmdk_free_last_extent(bs); |
715 | 0 | } |
716 | 0 | return ret; |
717 | 0 | } |
718 | | |
719 | 0 | #define SESPARSE_CONST_HEADER_MAGIC UINT64_C(0x00000000cafebabe) |
720 | 0 | #define SESPARSE_VOLATILE_HEADER_MAGIC UINT64_C(0x00000000cafecafe) |
721 | | |
722 | | /* Strict checks - format not officially documented */ |
723 | | static int check_se_sparse_const_header(VMDKSESparseConstHeader *header, |
724 | | Error **errp) |
725 | 0 | { |
726 | 0 | header->magic = le64_to_cpu(header->magic); |
727 | 0 | header->version = le64_to_cpu(header->version); |
728 | 0 | header->grain_size = le64_to_cpu(header->grain_size); |
729 | 0 | header->grain_table_size = le64_to_cpu(header->grain_table_size); |
730 | 0 | header->flags = le64_to_cpu(header->flags); |
731 | 0 | header->reserved1 = le64_to_cpu(header->reserved1); |
732 | 0 | header->reserved2 = le64_to_cpu(header->reserved2); |
733 | 0 | header->reserved3 = le64_to_cpu(header->reserved3); |
734 | 0 | header->reserved4 = le64_to_cpu(header->reserved4); |
735 | |
|
736 | 0 | header->volatile_header_offset = |
737 | 0 | le64_to_cpu(header->volatile_header_offset); |
738 | 0 | header->volatile_header_size = le64_to_cpu(header->volatile_header_size); |
739 | |
|
740 | 0 | header->journal_header_offset = le64_to_cpu(header->journal_header_offset); |
741 | 0 | header->journal_header_size = le64_to_cpu(header->journal_header_size); |
742 | |
|
743 | 0 | header->journal_offset = le64_to_cpu(header->journal_offset); |
744 | 0 | header->journal_size = le64_to_cpu(header->journal_size); |
745 | |
|
746 | 0 | header->grain_dir_offset = le64_to_cpu(header->grain_dir_offset); |
747 | 0 | header->grain_dir_size = le64_to_cpu(header->grain_dir_size); |
748 | |
|
749 | 0 | header->grain_tables_offset = le64_to_cpu(header->grain_tables_offset); |
750 | 0 | header->grain_tables_size = le64_to_cpu(header->grain_tables_size); |
751 | |
|
752 | 0 | header->free_bitmap_offset = le64_to_cpu(header->free_bitmap_offset); |
753 | 0 | header->free_bitmap_size = le64_to_cpu(header->free_bitmap_size); |
754 | |
|
755 | 0 | header->backmap_offset = le64_to_cpu(header->backmap_offset); |
756 | 0 | header->backmap_size = le64_to_cpu(header->backmap_size); |
757 | |
|
758 | 0 | header->grains_offset = le64_to_cpu(header->grains_offset); |
759 | 0 | header->grains_size = le64_to_cpu(header->grains_size); |
760 | |
|
761 | 0 | if (header->magic != SESPARSE_CONST_HEADER_MAGIC) { |
762 | 0 | error_setg(errp, "Bad const header magic: 0x%016" PRIx64, |
763 | 0 | header->magic); |
764 | 0 | return -EINVAL; |
765 | 0 | } |
766 | | |
767 | 0 | if (header->version != 0x0000000200000001) { |
768 | 0 | error_setg(errp, "Unsupported version: 0x%016" PRIx64, |
769 | 0 | header->version); |
770 | 0 | return -ENOTSUP; |
771 | 0 | } |
772 | | |
773 | 0 | if (header->grain_size != 8) { |
774 | 0 | error_setg(errp, "Unsupported grain size: %" PRIu64, |
775 | 0 | header->grain_size); |
776 | 0 | return -ENOTSUP; |
777 | 0 | } |
778 | | |
779 | 0 | if (header->grain_table_size != 64) { |
780 | 0 | error_setg(errp, "Unsupported grain table size: %" PRIu64, |
781 | 0 | header->grain_table_size); |
782 | 0 | return -ENOTSUP; |
783 | 0 | } |
784 | | |
785 | 0 | if (header->flags != 0) { |
786 | 0 | error_setg(errp, "Unsupported flags: 0x%016" PRIx64, |
787 | 0 | header->flags); |
788 | 0 | return -ENOTSUP; |
789 | 0 | } |
790 | | |
791 | 0 | if (header->reserved1 != 0 || header->reserved2 != 0 || |
792 | 0 | header->reserved3 != 0 || header->reserved4 != 0) { |
793 | 0 | error_setg(errp, "Unsupported reserved bits:" |
794 | 0 | " 0x%016" PRIx64 " 0x%016" PRIx64 |
795 | 0 | " 0x%016" PRIx64 " 0x%016" PRIx64, |
796 | 0 | header->reserved1, header->reserved2, |
797 | 0 | header->reserved3, header->reserved4); |
798 | 0 | return -ENOTSUP; |
799 | 0 | } |
800 | | |
801 | | /* check that padding is 0 */ |
802 | 0 | if (!buffer_is_zero(header->pad, sizeof(header->pad))) { |
803 | 0 | error_setg(errp, "Unsupported non-zero const header padding"); |
804 | 0 | return -ENOTSUP; |
805 | 0 | } |
806 | | |
807 | 0 | return 0; |
808 | 0 | } |
809 | | |
810 | | static int check_se_sparse_volatile_header(VMDKSESparseVolatileHeader *header, |
811 | | Error **errp) |
812 | 0 | { |
813 | 0 | header->magic = le64_to_cpu(header->magic); |
814 | 0 | header->free_gt_number = le64_to_cpu(header->free_gt_number); |
815 | 0 | header->next_txn_seq_number = le64_to_cpu(header->next_txn_seq_number); |
816 | 0 | header->replay_journal = le64_to_cpu(header->replay_journal); |
817 | |
|
818 | 0 | if (header->magic != SESPARSE_VOLATILE_HEADER_MAGIC) { |
819 | 0 | error_setg(errp, "Bad volatile header magic: 0x%016" PRIx64, |
820 | 0 | header->magic); |
821 | 0 | return -EINVAL; |
822 | 0 | } |
823 | | |
824 | 0 | if (header->replay_journal) { |
825 | 0 | error_setg(errp, "Image is dirty, Replaying journal not supported"); |
826 | 0 | return -ENOTSUP; |
827 | 0 | } |
828 | | |
829 | | /* check that padding is 0 */ |
830 | 0 | if (!buffer_is_zero(header->pad, sizeof(header->pad))) { |
831 | 0 | error_setg(errp, "Unsupported non-zero volatile header padding"); |
832 | 0 | return -ENOTSUP; |
833 | 0 | } |
834 | | |
835 | 0 | return 0; |
836 | 0 | } |
837 | | |
838 | | static int GRAPH_RDLOCK |
839 | | vmdk_open_se_sparse(BlockDriverState *bs, BdrvChild *file, int flags, |
840 | | Error **errp) |
841 | 0 | { |
842 | 0 | int ret; |
843 | 0 | VMDKSESparseConstHeader const_header; |
844 | 0 | VMDKSESparseVolatileHeader volatile_header; |
845 | 0 | VmdkExtent *extent = NULL; |
846 | |
|
847 | 0 | ret = bdrv_apply_auto_read_only(bs, |
848 | 0 | "No write support for seSparse images available", errp); |
849 | 0 | if (ret < 0) { |
850 | 0 | return ret; |
851 | 0 | } |
852 | | |
853 | 0 | assert(sizeof(const_header) == SECTOR_SIZE); |
854 | |
|
855 | 0 | ret = bdrv_pread(file, 0, sizeof(const_header), &const_header, 0); |
856 | 0 | if (ret < 0) { |
857 | 0 | bdrv_refresh_filename(file->bs); |
858 | 0 | error_setg_errno(errp, -ret, |
859 | 0 | "Could not read const header from file '%s'", |
860 | 0 | file->bs->filename); |
861 | 0 | return ret; |
862 | 0 | } |
863 | | |
864 | | /* check const header */ |
865 | 0 | ret = check_se_sparse_const_header(&const_header, errp); |
866 | 0 | if (ret < 0) { |
867 | 0 | return ret; |
868 | 0 | } |
869 | | |
870 | 0 | assert(sizeof(volatile_header) == SECTOR_SIZE); |
871 | |
|
872 | 0 | ret = bdrv_pread(file, const_header.volatile_header_offset * SECTOR_SIZE, |
873 | 0 | sizeof(volatile_header), &volatile_header, 0); |
874 | 0 | if (ret < 0) { |
875 | 0 | bdrv_refresh_filename(file->bs); |
876 | 0 | error_setg_errno(errp, -ret, |
877 | 0 | "Could not read volatile header from file '%s'", |
878 | 0 | file->bs->filename); |
879 | 0 | return ret; |
880 | 0 | } |
881 | | |
882 | | /* check volatile header */ |
883 | 0 | ret = check_se_sparse_volatile_header(&volatile_header, errp); |
884 | 0 | if (ret < 0) { |
885 | 0 | return ret; |
886 | 0 | } |
887 | | |
888 | 0 | ret = vmdk_add_extent(bs, file, false, |
889 | 0 | const_header.capacity, |
890 | 0 | const_header.grain_dir_offset * SECTOR_SIZE, |
891 | 0 | 0, |
892 | 0 | const_header.grain_dir_size * |
893 | 0 | SECTOR_SIZE / sizeof(uint64_t), |
894 | 0 | const_header.grain_table_size * |
895 | 0 | SECTOR_SIZE / sizeof(uint64_t), |
896 | 0 | const_header.grain_size, |
897 | 0 | &extent, |
898 | 0 | errp); |
899 | 0 | if (ret < 0) { |
900 | 0 | return ret; |
901 | 0 | } |
902 | | |
903 | 0 | extent->sesparse = true; |
904 | 0 | extent->sesparse_l2_tables_offset = const_header.grain_tables_offset; |
905 | 0 | extent->sesparse_clusters_offset = const_header.grains_offset; |
906 | 0 | extent->entry_size = sizeof(uint64_t); |
907 | |
|
908 | 0 | ret = vmdk_init_tables(bs, extent, errp); |
909 | 0 | if (ret) { |
910 | | /* free extent allocated by vmdk_add_extent */ |
911 | 0 | vmdk_free_last_extent(bs); |
912 | 0 | } |
913 | |
|
914 | 0 | return ret; |
915 | 0 | } |
916 | | |
917 | | static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf, |
918 | | QDict *options, Error **errp); |
919 | | |
920 | | static char *vmdk_read_desc(BdrvChild *file, uint64_t desc_offset, Error **errp) |
921 | 0 | { |
922 | 0 | int64_t size; |
923 | 0 | char *buf; |
924 | 0 | int ret; |
925 | |
|
926 | 0 | size = bdrv_getlength(file->bs); |
927 | 0 | if (size < 0) { |
928 | 0 | error_setg_errno(errp, -size, "Could not access file"); |
929 | 0 | return NULL; |
930 | 0 | } |
931 | | |
932 | 0 | if (size < 4) { |
933 | | /* Both descriptor file and sparse image must be much larger than 4 |
934 | | * bytes, also callers of vmdk_read_desc want to compare the first 4 |
935 | | * bytes with VMDK4_MAGIC, let's error out if less is read. */ |
936 | 0 | error_setg(errp, "File is too small, not a valid image"); |
937 | 0 | return NULL; |
938 | 0 | } |
939 | | |
940 | 0 | size = MIN(size, (1 << 20) - 1); /* avoid unbounded allocation */ |
941 | 0 | buf = g_malloc(size + 1); |
942 | |
|
943 | 0 | ret = bdrv_pread(file, desc_offset, size, buf, 0); |
944 | 0 | if (ret < 0) { |
945 | 0 | error_setg_errno(errp, -ret, "Could not read from file"); |
946 | 0 | g_free(buf); |
947 | 0 | return NULL; |
948 | 0 | } |
949 | 0 | buf[size] = 0; |
950 | |
|
951 | 0 | return buf; |
952 | 0 | } |
953 | | |
954 | | static int GRAPH_RDLOCK |
955 | | vmdk_open_vmdk4(BlockDriverState *bs, BdrvChild *file, int flags, |
956 | | QDict *options, Error **errp) |
957 | 0 | { |
958 | 0 | int ret; |
959 | 0 | uint32_t magic; |
960 | 0 | uint32_t l1_size, l1_entry_sectors; |
961 | 0 | VMDK4Header header; |
962 | 0 | VmdkExtent *extent = NULL; |
963 | 0 | BDRVVmdkState *s = bs->opaque; |
964 | 0 | int64_t l1_backup_offset = 0; |
965 | 0 | bool compressed; |
966 | |
|
967 | 0 | ret = bdrv_pread(file, sizeof(magic), sizeof(header), &header, 0); |
968 | 0 | if (ret < 0) { |
969 | 0 | bdrv_refresh_filename(file->bs); |
970 | 0 | error_setg_errno(errp, -ret, |
971 | 0 | "Could not read header from file '%s'", |
972 | 0 | file->bs->filename); |
973 | 0 | return -EINVAL; |
974 | 0 | } |
975 | 0 | if (header.capacity == 0) { |
976 | 0 | uint64_t desc_offset = le64_to_cpu(header.desc_offset); |
977 | 0 | if (desc_offset) { |
978 | 0 | char *buf = vmdk_read_desc(file, desc_offset << 9, errp); |
979 | 0 | if (!buf) { |
980 | 0 | return -EINVAL; |
981 | 0 | } |
982 | 0 | ret = vmdk_open_desc_file(bs, flags, buf, options, errp); |
983 | 0 | g_free(buf); |
984 | 0 | return ret; |
985 | 0 | } |
986 | 0 | } |
987 | | |
988 | 0 | if (!s->create_type) { |
989 | 0 | s->create_type = g_strdup("monolithicSparse"); |
990 | 0 | } |
991 | |
|
992 | 0 | if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) { |
993 | | /* |
994 | | * The footer takes precedence over the header, so read it in. The |
995 | | * footer starts at offset -1024 from the end: One sector for the |
996 | | * footer, and another one for the end-of-stream marker. |
997 | | */ |
998 | 0 | struct { |
999 | 0 | struct { |
1000 | 0 | uint64_t val; |
1001 | 0 | uint32_t size; |
1002 | 0 | uint32_t type; |
1003 | 0 | uint8_t pad[512 - 16]; |
1004 | 0 | } QEMU_PACKED footer_marker; |
1005 | |
|
1006 | 0 | uint32_t magic; |
1007 | 0 | VMDK4Header header; |
1008 | 0 | uint8_t pad[512 - 4 - sizeof(VMDK4Header)]; |
1009 | |
|
1010 | 0 | EosMarker eos_marker; |
1011 | 0 | } QEMU_PACKED footer; |
1012 | |
|
1013 | 0 | ret = bdrv_pread(file, bs->file->bs->total_sectors * 512 - 1536, |
1014 | 0 | sizeof(footer), &footer, 0); |
1015 | 0 | if (ret < 0) { |
1016 | 0 | error_setg_errno(errp, -ret, "Failed to read footer"); |
1017 | 0 | return ret; |
1018 | 0 | } |
1019 | | |
1020 | | /* Some sanity checks for the footer */ |
1021 | 0 | if (be32_to_cpu(footer.magic) != VMDK4_MAGIC || |
1022 | 0 | le32_to_cpu(footer.footer_marker.size) != 0 || |
1023 | 0 | le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER || |
1024 | 0 | le64_to_cpu(footer.eos_marker.val) != 0 || |
1025 | 0 | le32_to_cpu(footer.eos_marker.size) != 0 || |
1026 | 0 | le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM) |
1027 | 0 | { |
1028 | 0 | error_setg(errp, "Invalid footer"); |
1029 | 0 | return -EINVAL; |
1030 | 0 | } |
1031 | | |
1032 | 0 | header = footer.header; |
1033 | 0 | } |
1034 | | |
1035 | 0 | compressed = |
1036 | 0 | le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE; |
1037 | 0 | if (le32_to_cpu(header.version) > 3) { |
1038 | 0 | error_setg(errp, "Unsupported VMDK version %" PRIu32, |
1039 | 0 | le32_to_cpu(header.version)); |
1040 | 0 | return -ENOTSUP; |
1041 | 0 | } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR) && |
1042 | 0 | !compressed) { |
1043 | | /* VMware KB 2064959 explains that version 3 added support for |
1044 | | * persistent changed block tracking (CBT), and backup software can |
1045 | | * read it as version=1 if it doesn't care about the changed area |
1046 | | * information. So we are safe to enable read only. */ |
1047 | 0 | error_setg(errp, "VMDK version 3 must be read only"); |
1048 | 0 | return -EINVAL; |
1049 | 0 | } |
1050 | | |
1051 | 0 | if (le32_to_cpu(header.num_gtes_per_gt) > 512) { |
1052 | 0 | error_setg(errp, "L2 table size too big"); |
1053 | 0 | return -EINVAL; |
1054 | 0 | } |
1055 | 0 | if (le64_to_cpu(header.granularity) > 0x200000) { |
1056 | 0 | error_setg(errp, "Invalid granularity, image may be corrupt"); |
1057 | 0 | return -EFBIG; |
1058 | 0 | } |
1059 | 0 | l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt) |
1060 | 0 | * le64_to_cpu(header.granularity); |
1061 | 0 | if (l1_entry_sectors == 0) { |
1062 | 0 | error_setg(errp, "L1 entry size is invalid"); |
1063 | 0 | return -EINVAL; |
1064 | 0 | } |
1065 | 0 | l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1) |
1066 | 0 | / l1_entry_sectors; |
1067 | 0 | if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) { |
1068 | 0 | l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9; |
1069 | 0 | } |
1070 | 0 | if (bdrv_nb_sectors(file->bs) < le64_to_cpu(header.grain_offset)) { |
1071 | 0 | error_setg(errp, "File truncated, expecting at least %" PRId64 " bytes", |
1072 | 0 | (int64_t)(le64_to_cpu(header.grain_offset) |
1073 | 0 | * BDRV_SECTOR_SIZE)); |
1074 | 0 | return -EINVAL; |
1075 | 0 | } |
1076 | | |
1077 | 0 | ret = vmdk_add_extent(bs, file, false, |
1078 | 0 | le64_to_cpu(header.capacity), |
1079 | 0 | le64_to_cpu(header.gd_offset) << 9, |
1080 | 0 | l1_backup_offset, |
1081 | 0 | l1_size, |
1082 | 0 | le32_to_cpu(header.num_gtes_per_gt), |
1083 | 0 | le64_to_cpu(header.granularity), |
1084 | 0 | &extent, |
1085 | 0 | errp); |
1086 | 0 | if (ret < 0) { |
1087 | 0 | return ret; |
1088 | 0 | } |
1089 | 0 | extent->compressed = |
1090 | 0 | le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE; |
1091 | 0 | if (extent->compressed) { |
1092 | 0 | g_free(s->create_type); |
1093 | 0 | s->create_type = g_strdup("streamOptimized"); |
1094 | 0 | } |
1095 | 0 | extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER; |
1096 | 0 | extent->version = le32_to_cpu(header.version); |
1097 | 0 | extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN; |
1098 | 0 | ret = vmdk_init_tables(bs, extent, errp); |
1099 | 0 | if (ret) { |
1100 | | /* free extent allocated by vmdk_add_extent */ |
1101 | 0 | vmdk_free_last_extent(bs); |
1102 | 0 | } |
1103 | 0 | return ret; |
1104 | 0 | } |
1105 | | |
1106 | | /* find an option value out of descriptor file */ |
1107 | | static int vmdk_parse_description(const char *desc, const char *opt_name, |
1108 | | char *buf, int buf_size) |
1109 | 0 | { |
1110 | 0 | const char *opt_pos, *opt_end; |
1111 | 0 | const char *end = desc + strlen(desc); |
1112 | |
|
1113 | 0 | opt_pos = strstr(desc, opt_name); |
1114 | 0 | if (!opt_pos) { |
1115 | 0 | return VMDK_ERROR; |
1116 | 0 | } |
1117 | | /* Skip "=\"" following opt_name */ |
1118 | 0 | opt_pos += strlen(opt_name) + 2; |
1119 | 0 | if (opt_pos >= end) { |
1120 | 0 | return VMDK_ERROR; |
1121 | 0 | } |
1122 | 0 | opt_end = opt_pos; |
1123 | 0 | while (opt_end < end && *opt_end != '"') { |
1124 | 0 | opt_end++; |
1125 | 0 | } |
1126 | 0 | if (opt_end == end || buf_size < opt_end - opt_pos + 1) { |
1127 | 0 | return VMDK_ERROR; |
1128 | 0 | } |
1129 | 0 | pstrcpy(buf, opt_end - opt_pos + 1, opt_pos); |
1130 | 0 | return VMDK_OK; |
1131 | 0 | } |
1132 | | |
1133 | | /* Open an extent file and append to bs array */ |
1134 | | static int GRAPH_RDLOCK |
1135 | | vmdk_open_sparse(BlockDriverState *bs, BdrvChild *file, int flags, |
1136 | | char *buf, QDict *options, Error **errp) |
1137 | 0 | { |
1138 | 0 | uint32_t magic; |
1139 | |
|
1140 | 0 | magic = ldl_be_p(buf); |
1141 | 0 | switch (magic) { |
1142 | 0 | case VMDK3_MAGIC: |
1143 | 0 | return vmdk_open_vmfs_sparse(bs, file, flags, errp); |
1144 | 0 | case VMDK4_MAGIC: |
1145 | 0 | return vmdk_open_vmdk4(bs, file, flags, options, errp); |
1146 | 0 | default: |
1147 | 0 | error_setg(errp, "Image not in VMDK format"); |
1148 | 0 | return -EINVAL; |
1149 | 0 | } |
1150 | 0 | } |
1151 | | |
1152 | | static const char *next_line(const char *s) |
1153 | 0 | { |
1154 | 0 | while (*s) { |
1155 | 0 | if (*s == '\n') { |
1156 | 0 | return s + 1; |
1157 | 0 | } |
1158 | 0 | s++; |
1159 | 0 | } |
1160 | 0 | return s; |
1161 | 0 | } |
1162 | | |
1163 | | static int GRAPH_RDLOCK |
1164 | | vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, |
1165 | | Error **errp) |
1166 | 0 | { |
1167 | 0 | ERRP_GUARD(); |
1168 | 0 | int ret; |
1169 | 0 | int matches; |
1170 | 0 | char access[11]; |
1171 | 0 | char type[11]; |
1172 | 0 | char fname[512]; |
1173 | 0 | const char *p, *np; |
1174 | 0 | int64_t sectors = 0; |
1175 | 0 | int64_t flat_offset; |
1176 | 0 | char *desc_file_dir = NULL; |
1177 | 0 | char *extent_path; |
1178 | 0 | BdrvChild *extent_file; |
1179 | 0 | BdrvChildRole extent_role; |
1180 | 0 | BDRVVmdkState *s = bs->opaque; |
1181 | 0 | VmdkExtent *extent = NULL; |
1182 | 0 | char extent_opt_prefix[32]; |
1183 | 0 | Error *local_err = NULL; |
1184 | |
|
1185 | 0 | GLOBAL_STATE_CODE(); |
1186 | |
|
1187 | 0 | for (p = desc; *p; p = next_line(p)) { |
1188 | | /* parse extent line in one of below formats: |
1189 | | * |
1190 | | * RW [size in sectors] FLAT "file-name.vmdk" OFFSET |
1191 | | * RW [size in sectors] SPARSE "file-name.vmdk" |
1192 | | * RW [size in sectors] VMFS "file-name.vmdk" |
1193 | | * RW [size in sectors] VMFSSPARSE "file-name.vmdk" |
1194 | | * RW [size in sectors] SESPARSE "file-name.vmdk" |
1195 | | */ |
1196 | 0 | flat_offset = -1; |
1197 | 0 | matches = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64, |
1198 | 0 | access, §ors, type, fname, &flat_offset); |
1199 | 0 | if (matches < 4 || strcmp(access, "RW")) { |
1200 | 0 | continue; |
1201 | 0 | } else if (!strcmp(type, "FLAT")) { |
1202 | 0 | if (matches != 5 || flat_offset < 0) { |
1203 | 0 | goto invalid; |
1204 | 0 | } |
1205 | 0 | } else if (!strcmp(type, "VMFS")) { |
1206 | 0 | if (matches == 4) { |
1207 | 0 | flat_offset = 0; |
1208 | 0 | } else { |
1209 | 0 | goto invalid; |
1210 | 0 | } |
1211 | 0 | } else if (matches != 4) { |
1212 | 0 | goto invalid; |
1213 | 0 | } |
1214 | | |
1215 | 0 | if (sectors <= 0 || |
1216 | 0 | (strcmp(type, "FLAT") && strcmp(type, "SPARSE") && |
1217 | 0 | strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE") && |
1218 | 0 | strcmp(type, "SESPARSE")) || |
1219 | 0 | (strcmp(access, "RW"))) { |
1220 | 0 | continue; |
1221 | 0 | } |
1222 | | |
1223 | 0 | if (path_is_absolute(fname)) { |
1224 | 0 | extent_path = g_strdup(fname); |
1225 | 0 | } else { |
1226 | 0 | if (!desc_file_dir) { |
1227 | 0 | desc_file_dir = bdrv_dirname(bs->file->bs, errp); |
1228 | 0 | if (!desc_file_dir) { |
1229 | 0 | bdrv_refresh_filename(bs->file->bs); |
1230 | 0 | error_prepend(errp, "Cannot use relative paths with VMDK " |
1231 | 0 | "descriptor file '%s': ", |
1232 | 0 | bs->file->bs->filename); |
1233 | 0 | ret = -EINVAL; |
1234 | 0 | goto out; |
1235 | 0 | } |
1236 | 0 | } |
1237 | | |
1238 | 0 | extent_path = g_strconcat(desc_file_dir, fname, NULL); |
1239 | 0 | } |
1240 | | |
1241 | 0 | ret = snprintf(extent_opt_prefix, 32, "extents.%d", s->num_extents); |
1242 | 0 | assert(ret < 32); |
1243 | |
|
1244 | 0 | extent_role = BDRV_CHILD_DATA; |
1245 | 0 | if (strcmp(type, "FLAT") != 0 && strcmp(type, "VMFS") != 0) { |
1246 | | /* non-flat extents have metadata */ |
1247 | 0 | extent_role |= BDRV_CHILD_METADATA; |
1248 | 0 | } |
1249 | |
|
1250 | 0 | bdrv_graph_rdunlock_main_loop(); |
1251 | 0 | extent_file = bdrv_open_child(extent_path, options, extent_opt_prefix, |
1252 | 0 | bs, &child_of_bds, extent_role, false, |
1253 | 0 | &local_err); |
1254 | 0 | bdrv_graph_rdlock_main_loop(); |
1255 | 0 | g_free(extent_path); |
1256 | 0 | if (!extent_file) { |
1257 | 0 | error_propagate(errp, local_err); |
1258 | 0 | ret = -EINVAL; |
1259 | 0 | goto out; |
1260 | 0 | } |
1261 | | |
1262 | | /* save to extents array */ |
1263 | 0 | if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) { |
1264 | | /* FLAT extent */ |
1265 | |
|
1266 | 0 | ret = vmdk_add_extent(bs, extent_file, true, sectors, |
1267 | 0 | 0, 0, 0, 0, 0, &extent, errp); |
1268 | 0 | if (ret < 0) { |
1269 | 0 | bdrv_graph_rdunlock_main_loop(); |
1270 | 0 | bdrv_graph_wrlock_drained(); |
1271 | 0 | bdrv_unref_child(bs, extent_file); |
1272 | 0 | bdrv_graph_wrunlock(); |
1273 | 0 | bdrv_graph_rdlock_main_loop(); |
1274 | 0 | goto out; |
1275 | 0 | } |
1276 | 0 | extent->flat_start_offset = flat_offset << 9; |
1277 | 0 | } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) { |
1278 | | /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/ |
1279 | 0 | char *buf = vmdk_read_desc(extent_file, 0, errp); |
1280 | 0 | if (!buf) { |
1281 | 0 | ret = -EINVAL; |
1282 | 0 | } else { |
1283 | 0 | ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, buf, |
1284 | 0 | options, errp); |
1285 | 0 | } |
1286 | 0 | g_free(buf); |
1287 | 0 | if (ret) { |
1288 | 0 | bdrv_graph_rdunlock_main_loop(); |
1289 | 0 | bdrv_graph_wrlock_drained(); |
1290 | 0 | bdrv_unref_child(bs, extent_file); |
1291 | 0 | bdrv_graph_wrunlock(); |
1292 | 0 | bdrv_graph_rdlock_main_loop(); |
1293 | 0 | goto out; |
1294 | 0 | } |
1295 | 0 | extent = &s->extents[s->num_extents - 1]; |
1296 | 0 | } else if (!strcmp(type, "SESPARSE")) { |
1297 | 0 | ret = vmdk_open_se_sparse(bs, extent_file, bs->open_flags, errp); |
1298 | 0 | if (ret) { |
1299 | 0 | bdrv_graph_rdunlock_main_loop(); |
1300 | 0 | bdrv_graph_wrlock_drained(); |
1301 | 0 | bdrv_unref_child(bs, extent_file); |
1302 | 0 | bdrv_graph_wrunlock(); |
1303 | 0 | bdrv_graph_rdlock_main_loop(); |
1304 | 0 | goto out; |
1305 | 0 | } |
1306 | 0 | extent = &s->extents[s->num_extents - 1]; |
1307 | 0 | } else { |
1308 | 0 | error_setg(errp, "Unsupported extent type '%s'", type); |
1309 | 0 | bdrv_graph_rdunlock_main_loop(); |
1310 | 0 | bdrv_graph_wrlock_drained(); |
1311 | 0 | bdrv_unref_child(bs, extent_file); |
1312 | 0 | bdrv_graph_wrunlock(); |
1313 | 0 | bdrv_graph_rdlock_main_loop(); |
1314 | 0 | ret = -ENOTSUP; |
1315 | 0 | goto out; |
1316 | 0 | } |
1317 | 0 | extent->type = g_strdup(type); |
1318 | 0 | } |
1319 | | |
1320 | 0 | if (s->num_extents == 0) { |
1321 | 0 | error_setg(errp, "Invalid file: no extent lines present"); |
1322 | 0 | ret = -EINVAL; |
1323 | 0 | goto out; |
1324 | 0 | } |
1325 | | |
1326 | 0 | ret = 0; |
1327 | 0 | goto out; |
1328 | | |
1329 | 0 | invalid: |
1330 | 0 | np = next_line(p); |
1331 | 0 | assert(np != p); |
1332 | 0 | if (np[-1] == '\n') { |
1333 | 0 | np--; |
1334 | 0 | } |
1335 | 0 | error_setg(errp, "Invalid extent line: %.*s", (int)(np - p), p); |
1336 | 0 | ret = -EINVAL; |
1337 | |
|
1338 | 0 | out: |
1339 | 0 | g_free(desc_file_dir); |
1340 | 0 | return ret; |
1341 | 0 | } |
1342 | | |
1343 | | static int GRAPH_RDLOCK |
1344 | | vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf, QDict *options, |
1345 | | Error **errp) |
1346 | 0 | { |
1347 | 0 | int ret; |
1348 | 0 | char ct[128]; |
1349 | 0 | BDRVVmdkState *s = bs->opaque; |
1350 | |
|
1351 | 0 | if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) { |
1352 | 0 | error_setg(errp, "invalid VMDK image descriptor"); |
1353 | 0 | ret = -EINVAL; |
1354 | 0 | goto exit; |
1355 | 0 | } |
1356 | 0 | if (strcmp(ct, "monolithicFlat") && |
1357 | 0 | strcmp(ct, "vmfs") && |
1358 | 0 | strcmp(ct, "vmfsSparse") && |
1359 | 0 | strcmp(ct, "seSparse") && |
1360 | 0 | strcmp(ct, "twoGbMaxExtentSparse") && |
1361 | 0 | strcmp(ct, "twoGbMaxExtentFlat")) { |
1362 | 0 | error_setg(errp, "Unsupported image type '%s'", ct); |
1363 | 0 | ret = -ENOTSUP; |
1364 | 0 | goto exit; |
1365 | 0 | } |
1366 | 0 | s->create_type = g_strdup(ct); |
1367 | 0 | s->desc_offset = 0; |
1368 | 0 | ret = vmdk_parse_extents(buf, bs, options, errp); |
1369 | 0 | exit: |
1370 | 0 | return ret; |
1371 | 0 | } |
1372 | | |
1373 | | static int vmdk_open(BlockDriverState *bs, QDict *options, int flags, |
1374 | | Error **errp) |
1375 | 0 | { |
1376 | 0 | char *buf; |
1377 | 0 | int ret; |
1378 | 0 | BDRVVmdkState *s = bs->opaque; |
1379 | 0 | uint32_t magic; |
1380 | |
|
1381 | 0 | ret = bdrv_open_file_child(NULL, options, "file", bs, errp); |
1382 | 0 | if (ret < 0) { |
1383 | 0 | return ret; |
1384 | 0 | } |
1385 | | |
1386 | 0 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
1387 | |
|
1388 | 0 | buf = vmdk_read_desc(bs->file, 0, errp); |
1389 | 0 | if (!buf) { |
1390 | 0 | return -EINVAL; |
1391 | 0 | } |
1392 | | |
1393 | 0 | magic = ldl_be_p(buf); |
1394 | 0 | switch (magic) { |
1395 | 0 | case VMDK3_MAGIC: |
1396 | 0 | case VMDK4_MAGIC: |
1397 | 0 | ret = vmdk_open_sparse(bs, bs->file, flags, buf, options, |
1398 | 0 | errp); |
1399 | 0 | s->desc_offset = 0x200; |
1400 | 0 | break; |
1401 | 0 | default: |
1402 | | /* No data in the descriptor file */ |
1403 | 0 | bs->file->role &= ~BDRV_CHILD_DATA; |
1404 | | |
1405 | | /* Must succeed because we have given up permissions if anything */ |
1406 | 0 | bdrv_child_refresh_perms(bs, bs->file, &error_abort); |
1407 | |
|
1408 | 0 | ret = vmdk_open_desc_file(bs, flags, buf, options, errp); |
1409 | 0 | break; |
1410 | 0 | } |
1411 | 0 | if (ret) { |
1412 | 0 | goto fail; |
1413 | 0 | } |
1414 | | |
1415 | | /* try to open parent images, if exist */ |
1416 | 0 | ret = vmdk_parent_open(bs); |
1417 | 0 | if (ret) { |
1418 | 0 | goto fail; |
1419 | 0 | } |
1420 | 0 | ret = vmdk_read_cid(bs, 0, &s->cid); |
1421 | 0 | if (ret) { |
1422 | 0 | goto fail; |
1423 | 0 | } |
1424 | 0 | ret = vmdk_read_cid(bs, 1, &s->parent_cid); |
1425 | 0 | if (ret) { |
1426 | 0 | goto fail; |
1427 | 0 | } |
1428 | 0 | qemu_co_mutex_init(&s->lock); |
1429 | | |
1430 | | /* Disable migration when VMDK images are used */ |
1431 | 0 | error_setg(&s->migration_blocker, "The vmdk format used by node '%s' " |
1432 | 0 | "does not support live migration", |
1433 | 0 | bdrv_get_device_or_node_name(bs)); |
1434 | 0 | ret = migrate_add_blocker_normal(&s->migration_blocker, errp); |
1435 | 0 | if (ret < 0) { |
1436 | 0 | goto fail; |
1437 | 0 | } |
1438 | | |
1439 | 0 | g_free(buf); |
1440 | 0 | return 0; |
1441 | | |
1442 | 0 | fail: |
1443 | 0 | g_free(buf); |
1444 | 0 | g_free(s->create_type); |
1445 | 0 | s->create_type = NULL; |
1446 | 0 | vmdk_free_extents(bs); |
1447 | 0 | return ret; |
1448 | 0 | } |
1449 | | |
1450 | | |
1451 | | static void vmdk_refresh_limits(BlockDriverState *bs, Error **errp) |
1452 | 0 | { |
1453 | 0 | BDRVVmdkState *s = bs->opaque; |
1454 | 0 | int i; |
1455 | |
|
1456 | 0 | for (i = 0; i < s->num_extents; i++) { |
1457 | 0 | if (!s->extents[i].flat) { |
1458 | 0 | bs->bl.pwrite_zeroes_alignment = |
1459 | 0 | MAX(bs->bl.pwrite_zeroes_alignment, |
1460 | 0 | s->extents[i].cluster_sectors << BDRV_SECTOR_BITS); |
1461 | 0 | } |
1462 | 0 | } |
1463 | 0 | } |
1464 | | |
1465 | | /** |
1466 | | * get_whole_cluster |
1467 | | * |
1468 | | * Copy backing file's cluster that covers @sector_num, otherwise write zero, |
1469 | | * to the cluster at @cluster_sector_num. If @zeroed is true, we're overwriting |
1470 | | * a zeroed cluster in the current layer and must not copy data from the |
1471 | | * backing file. |
1472 | | * |
1473 | | * If @skip_start_sector < @skip_end_sector, the relative range |
1474 | | * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave |
1475 | | * it for call to write user data in the request. |
1476 | | */ |
1477 | | static int coroutine_fn GRAPH_RDLOCK |
1478 | | get_whole_cluster(BlockDriverState *bs, VmdkExtent *extent, |
1479 | | uint64_t cluster_offset, uint64_t offset, |
1480 | | uint64_t skip_start_bytes, uint64_t skip_end_bytes, |
1481 | | bool zeroed) |
1482 | 0 | { |
1483 | 0 | int ret = VMDK_OK; |
1484 | 0 | int64_t cluster_bytes; |
1485 | 0 | uint8_t *whole_grain; |
1486 | 0 | bool copy_from_backing; |
1487 | | |
1488 | | /* For COW, align request sector_num to cluster start */ |
1489 | 0 | cluster_bytes = extent->cluster_sectors << BDRV_SECTOR_BITS; |
1490 | 0 | offset = QEMU_ALIGN_DOWN(offset, cluster_bytes); |
1491 | 0 | whole_grain = qemu_blockalign(bs, cluster_bytes); |
1492 | 0 | copy_from_backing = bs->backing && !zeroed; |
1493 | |
|
1494 | 0 | if (!copy_from_backing) { |
1495 | 0 | memset(whole_grain, 0, skip_start_bytes); |
1496 | 0 | memset(whole_grain + skip_end_bytes, 0, cluster_bytes - skip_end_bytes); |
1497 | 0 | } |
1498 | |
|
1499 | 0 | assert(skip_end_bytes <= cluster_bytes); |
1500 | | /* we will be here if it's first write on non-exist grain(cluster). |
1501 | | * try to read from parent image, if exist */ |
1502 | 0 | if (bs->backing && !vmdk_is_cid_valid(bs)) { |
1503 | 0 | ret = VMDK_ERROR; |
1504 | 0 | goto exit; |
1505 | 0 | } |
1506 | | |
1507 | | /* Read backing data before skip range */ |
1508 | 0 | if (skip_start_bytes > 0) { |
1509 | 0 | if (copy_from_backing) { |
1510 | | /* qcow2 emits this on bs->file instead of bs->backing */ |
1511 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_COW_READ); |
1512 | 0 | ret = bdrv_co_pread(bs->backing, offset, skip_start_bytes, |
1513 | 0 | whole_grain, 0); |
1514 | 0 | if (ret < 0) { |
1515 | 0 | ret = VMDK_ERROR; |
1516 | 0 | goto exit; |
1517 | 0 | } |
1518 | 0 | } |
1519 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_COW_WRITE); |
1520 | 0 | ret = bdrv_co_pwrite(extent->file, cluster_offset, skip_start_bytes, |
1521 | 0 | whole_grain, 0); |
1522 | 0 | if (ret < 0) { |
1523 | 0 | ret = VMDK_ERROR; |
1524 | 0 | goto exit; |
1525 | 0 | } |
1526 | 0 | } |
1527 | | /* Read backing data after skip range */ |
1528 | 0 | if (skip_end_bytes < cluster_bytes) { |
1529 | 0 | if (copy_from_backing) { |
1530 | | /* qcow2 emits this on bs->file instead of bs->backing */ |
1531 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_COW_READ); |
1532 | 0 | ret = bdrv_co_pread(bs->backing, offset + skip_end_bytes, |
1533 | 0 | cluster_bytes - skip_end_bytes, |
1534 | 0 | whole_grain + skip_end_bytes, 0); |
1535 | 0 | if (ret < 0) { |
1536 | 0 | ret = VMDK_ERROR; |
1537 | 0 | goto exit; |
1538 | 0 | } |
1539 | 0 | } |
1540 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_COW_WRITE); |
1541 | 0 | ret = bdrv_co_pwrite(extent->file, cluster_offset + skip_end_bytes, |
1542 | 0 | cluster_bytes - skip_end_bytes, |
1543 | 0 | whole_grain + skip_end_bytes, 0); |
1544 | 0 | if (ret < 0) { |
1545 | 0 | ret = VMDK_ERROR; |
1546 | 0 | goto exit; |
1547 | 0 | } |
1548 | 0 | } |
1549 | | |
1550 | 0 | ret = VMDK_OK; |
1551 | 0 | exit: |
1552 | 0 | qemu_vfree(whole_grain); |
1553 | 0 | return ret; |
1554 | 0 | } |
1555 | | |
1556 | | static int coroutine_fn GRAPH_RDLOCK |
1557 | | vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data, uint32_t offset) |
1558 | 0 | { |
1559 | 0 | offset = cpu_to_le32(offset); |
1560 | | /* update L2 table */ |
1561 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_L2_UPDATE); |
1562 | 0 | if (bdrv_co_pwrite(extent->file, |
1563 | 0 | ((int64_t)m_data->l2_offset * 512) |
1564 | 0 | + (m_data->l2_index * sizeof(offset)), |
1565 | 0 | sizeof(offset), &offset, 0) < 0) { |
1566 | 0 | return VMDK_ERROR; |
1567 | 0 | } |
1568 | | /* update backup L2 table */ |
1569 | 0 | if (extent->l1_backup_table_offset != 0) { |
1570 | 0 | m_data->l2_offset = extent->l1_backup_table[m_data->l1_index]; |
1571 | 0 | if (bdrv_co_pwrite(extent->file, |
1572 | 0 | ((int64_t)m_data->l2_offset * 512) |
1573 | 0 | + (m_data->l2_index * sizeof(offset)), |
1574 | 0 | sizeof(offset), &offset, 0) < 0) { |
1575 | 0 | return VMDK_ERROR; |
1576 | 0 | } |
1577 | 0 | } |
1578 | 0 | if (bdrv_co_flush(extent->file->bs) < 0) { |
1579 | 0 | return VMDK_ERROR; |
1580 | 0 | } |
1581 | 0 | if (m_data->l2_cache_entry) { |
1582 | 0 | *m_data->l2_cache_entry = offset; |
1583 | 0 | } |
1584 | |
|
1585 | 0 | return VMDK_OK; |
1586 | 0 | } |
1587 | | |
1588 | | /** |
1589 | | * get_cluster_offset |
1590 | | * |
1591 | | * Look up cluster offset in extent file by sector number, and store in |
1592 | | * @cluster_offset. |
1593 | | * |
1594 | | * For flat extents, the start offset as parsed from the description file is |
1595 | | * returned. |
1596 | | * |
1597 | | * For sparse extents, look up in L1, L2 table. If allocate is true, return an |
1598 | | * offset for a new cluster and update L2 cache. If there is a backing file, |
1599 | | * COW is done before returning; otherwise, zeroes are written to the allocated |
1600 | | * cluster. Both COW and zero writing skips the sector range |
1601 | | * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller |
1602 | | * has new data to write there. |
1603 | | * |
1604 | | * Returns: VMDK_OK if cluster exists and mapped in the image. |
1605 | | * VMDK_UNALLOC if cluster is not mapped and @allocate is false. |
1606 | | * VMDK_ERROR if failed. |
1607 | | */ |
1608 | | static int coroutine_fn GRAPH_RDLOCK |
1609 | | get_cluster_offset(BlockDriverState *bs, VmdkExtent *extent, |
1610 | | VmdkMetaData *m_data, uint64_t offset, bool allocate, |
1611 | | uint64_t *cluster_offset, uint64_t skip_start_bytes, |
1612 | | uint64_t skip_end_bytes) |
1613 | 0 | { |
1614 | 0 | unsigned int l1_index, l2_offset, l2_index; |
1615 | 0 | int min_index, i, j; |
1616 | 0 | uint32_t min_count; |
1617 | 0 | void *l2_table; |
1618 | 0 | bool zeroed = false; |
1619 | 0 | int64_t ret; |
1620 | 0 | int64_t cluster_sector; |
1621 | 0 | unsigned int l2_size_bytes = extent->l2_size * extent->entry_size; |
1622 | |
|
1623 | 0 | if (m_data) { |
1624 | 0 | m_data->new_allocation = false; |
1625 | 0 | } |
1626 | 0 | if (extent->flat) { |
1627 | 0 | *cluster_offset = extent->flat_start_offset; |
1628 | 0 | return VMDK_OK; |
1629 | 0 | } |
1630 | | |
1631 | 0 | offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE; |
1632 | 0 | l1_index = (offset >> 9) / extent->l1_entry_sectors; |
1633 | 0 | if (l1_index >= extent->l1_size) { |
1634 | 0 | return VMDK_ERROR; |
1635 | 0 | } |
1636 | 0 | if (extent->sesparse) { |
1637 | 0 | uint64_t l2_offset_u64; |
1638 | |
|
1639 | 0 | assert(extent->entry_size == sizeof(uint64_t)); |
1640 | |
|
1641 | 0 | l2_offset_u64 = ((uint64_t *)extent->l1_table)[l1_index]; |
1642 | 0 | if (l2_offset_u64 == 0) { |
1643 | 0 | l2_offset = 0; |
1644 | 0 | } else if ((l2_offset_u64 & 0xffffffff00000000) != 0x1000000000000000) { |
1645 | | /* |
1646 | | * Top most nibble is 0x1 if grain table is allocated. |
1647 | | * strict check - top most 4 bytes must be 0x10000000 since max |
1648 | | * supported size is 64TB for disk - so no more than 64TB / 16MB |
1649 | | * grain directories which is smaller than uint32, |
1650 | | * where 16MB is the only supported default grain table coverage. |
1651 | | */ |
1652 | 0 | return VMDK_ERROR; |
1653 | 0 | } else { |
1654 | 0 | l2_offset_u64 = l2_offset_u64 & 0x00000000ffffffff; |
1655 | 0 | l2_offset_u64 = extent->sesparse_l2_tables_offset + |
1656 | 0 | l2_offset_u64 * l2_size_bytes / SECTOR_SIZE; |
1657 | 0 | if (l2_offset_u64 > 0x00000000ffffffff) { |
1658 | 0 | return VMDK_ERROR; |
1659 | 0 | } |
1660 | 0 | l2_offset = (unsigned int)(l2_offset_u64); |
1661 | 0 | } |
1662 | 0 | } else { |
1663 | 0 | assert(extent->entry_size == sizeof(uint32_t)); |
1664 | 0 | l2_offset = ((uint32_t *)extent->l1_table)[l1_index]; |
1665 | 0 | } |
1666 | 0 | if (!l2_offset) { |
1667 | 0 | return VMDK_UNALLOC; |
1668 | 0 | } |
1669 | 0 | for (i = 0; i < L2_CACHE_SIZE; i++) { |
1670 | 0 | if (l2_offset == extent->l2_cache_offsets[i]) { |
1671 | | /* increment the hit count */ |
1672 | 0 | if (++extent->l2_cache_counts[i] == 0xffffffff) { |
1673 | 0 | for (j = 0; j < L2_CACHE_SIZE; j++) { |
1674 | 0 | extent->l2_cache_counts[j] >>= 1; |
1675 | 0 | } |
1676 | 0 | } |
1677 | 0 | l2_table = (char *)extent->l2_cache + (i * l2_size_bytes); |
1678 | 0 | goto found; |
1679 | 0 | } |
1680 | 0 | } |
1681 | | /* not found: load a new entry in the least used one */ |
1682 | 0 | min_index = 0; |
1683 | 0 | min_count = 0xffffffff; |
1684 | 0 | for (i = 0; i < L2_CACHE_SIZE; i++) { |
1685 | 0 | if (extent->l2_cache_counts[i] < min_count) { |
1686 | 0 | min_count = extent->l2_cache_counts[i]; |
1687 | 0 | min_index = i; |
1688 | 0 | } |
1689 | 0 | } |
1690 | 0 | l2_table = (char *)extent->l2_cache + (min_index * l2_size_bytes); |
1691 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_L2_LOAD); |
1692 | 0 | if (bdrv_co_pread(extent->file, |
1693 | 0 | (int64_t)l2_offset * 512, |
1694 | 0 | l2_size_bytes, |
1695 | 0 | l2_table, 0 |
1696 | 0 | ) < 0) { |
1697 | 0 | return VMDK_ERROR; |
1698 | 0 | } |
1699 | | |
1700 | 0 | extent->l2_cache_offsets[min_index] = l2_offset; |
1701 | 0 | extent->l2_cache_counts[min_index] = 1; |
1702 | 0 | found: |
1703 | 0 | l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size; |
1704 | 0 | if (m_data) { |
1705 | 0 | m_data->l1_index = l1_index; |
1706 | 0 | m_data->l2_index = l2_index; |
1707 | 0 | m_data->l2_offset = l2_offset; |
1708 | 0 | m_data->l2_cache_entry = ((uint32_t *)l2_table) + l2_index; |
1709 | 0 | } |
1710 | |
|
1711 | 0 | if (extent->sesparse) { |
1712 | 0 | cluster_sector = le64_to_cpu(((uint64_t *)l2_table)[l2_index]); |
1713 | 0 | switch (cluster_sector & 0xf000000000000000) { |
1714 | 0 | case 0x0000000000000000: |
1715 | | /* unallocated grain */ |
1716 | 0 | if (cluster_sector != 0) { |
1717 | 0 | return VMDK_ERROR; |
1718 | 0 | } |
1719 | 0 | break; |
1720 | 0 | case 0x1000000000000000: |
1721 | | /* scsi-unmapped grain - fallthrough */ |
1722 | 0 | case 0x2000000000000000: |
1723 | | /* zero grain */ |
1724 | 0 | zeroed = true; |
1725 | 0 | break; |
1726 | 0 | case 0x3000000000000000: |
1727 | | /* allocated grain */ |
1728 | 0 | cluster_sector = (((cluster_sector & 0x0fff000000000000) >> 48) | |
1729 | 0 | ((cluster_sector & 0x0000ffffffffffff) << 12)); |
1730 | 0 | cluster_sector = extent->sesparse_clusters_offset + |
1731 | 0 | cluster_sector * extent->cluster_sectors; |
1732 | 0 | break; |
1733 | 0 | default: |
1734 | 0 | return VMDK_ERROR; |
1735 | 0 | } |
1736 | 0 | } else { |
1737 | 0 | cluster_sector = le32_to_cpu(((uint32_t *)l2_table)[l2_index]); |
1738 | |
|
1739 | 0 | if (extent->has_zero_grain && cluster_sector == VMDK_GTE_ZEROED) { |
1740 | 0 | zeroed = true; |
1741 | 0 | } |
1742 | 0 | } |
1743 | | |
1744 | 0 | if (!cluster_sector || zeroed) { |
1745 | 0 | if (!allocate) { |
1746 | 0 | return zeroed ? VMDK_ZEROED : VMDK_UNALLOC; |
1747 | 0 | } |
1748 | 0 | assert(!extent->sesparse); |
1749 | |
|
1750 | 0 | if (extent->next_cluster_sector >= VMDK_EXTENT_MAX_SECTORS) { |
1751 | 0 | return VMDK_ERROR; |
1752 | 0 | } |
1753 | | |
1754 | 0 | cluster_sector = extent->next_cluster_sector; |
1755 | 0 | extent->next_cluster_sector += extent->cluster_sectors; |
1756 | | |
1757 | | /* |
1758 | | * Stream optimized (compressed) extents do not have to be |
1759 | | * pre-allocated, as compressed writes always cover the whole |
1760 | | * cluster, and will align on 512 byte chunks. |
1761 | | */ |
1762 | 0 | if (!extent->compressed) { |
1763 | | /* |
1764 | | * First of all we write grain itself, to avoid race condition |
1765 | | * that may to corrupt the image. |
1766 | | * This problem may occur because of insufficient space on host disk |
1767 | | * or inappropriate VM shutdown. |
1768 | | */ |
1769 | 0 | ret = get_whole_cluster(bs, extent, |
1770 | 0 | cluster_sector * BDRV_SECTOR_SIZE, |
1771 | 0 | offset, skip_start_bytes, skip_end_bytes, |
1772 | 0 | zeroed); |
1773 | 0 | if (ret) { |
1774 | 0 | return ret; |
1775 | 0 | } |
1776 | 0 | } |
1777 | 0 | if (m_data) { |
1778 | 0 | m_data->new_allocation = true; |
1779 | 0 | } |
1780 | 0 | } |
1781 | 0 | *cluster_offset = cluster_sector << BDRV_SECTOR_BITS; |
1782 | 0 | return VMDK_OK; |
1783 | 0 | } |
1784 | | |
1785 | | static VmdkExtent *find_extent(BDRVVmdkState *s, |
1786 | | int64_t sector_num, VmdkExtent *start_hint) |
1787 | 0 | { |
1788 | 0 | VmdkExtent *extent = start_hint; |
1789 | |
|
1790 | 0 | if (!extent) { |
1791 | 0 | extent = &s->extents[0]; |
1792 | 0 | } |
1793 | 0 | while (extent < &s->extents[s->num_extents]) { |
1794 | 0 | if (sector_num < extent->end_sector) { |
1795 | 0 | return extent; |
1796 | 0 | } |
1797 | 0 | extent++; |
1798 | 0 | } |
1799 | 0 | return NULL; |
1800 | 0 | } |
1801 | | |
1802 | | static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent *extent, |
1803 | | int64_t offset) |
1804 | 0 | { |
1805 | 0 | uint64_t extent_begin_offset, extent_relative_offset; |
1806 | 0 | uint64_t cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE; |
1807 | |
|
1808 | 0 | extent_begin_offset = |
1809 | 0 | (extent->end_sector - extent->sectors) * BDRV_SECTOR_SIZE; |
1810 | 0 | extent_relative_offset = offset - extent_begin_offset; |
1811 | 0 | return extent_relative_offset % cluster_size; |
1812 | 0 | } |
1813 | | |
1814 | | static int coroutine_fn GRAPH_RDLOCK |
1815 | | vmdk_co_block_status(BlockDriverState *bs, unsigned int mode, |
1816 | | int64_t offset, int64_t bytes, int64_t *pnum, |
1817 | | int64_t *map, BlockDriverState **file) |
1818 | 0 | { |
1819 | 0 | BDRVVmdkState *s = bs->opaque; |
1820 | 0 | int64_t index_in_cluster, n, ret; |
1821 | 0 | uint64_t cluster_offset; |
1822 | 0 | VmdkExtent *extent; |
1823 | |
|
1824 | 0 | extent = find_extent(s, offset >> BDRV_SECTOR_BITS, NULL); |
1825 | 0 | if (!extent) { |
1826 | 0 | return -EIO; |
1827 | 0 | } |
1828 | 0 | qemu_co_mutex_lock(&s->lock); |
1829 | 0 | ret = get_cluster_offset(bs, extent, NULL, offset, false, &cluster_offset, |
1830 | 0 | 0, 0); |
1831 | 0 | qemu_co_mutex_unlock(&s->lock); |
1832 | |
|
1833 | 0 | index_in_cluster = vmdk_find_offset_in_cluster(extent, offset); |
1834 | 0 | switch (ret) { |
1835 | 0 | case VMDK_ERROR: |
1836 | 0 | ret = -EIO; |
1837 | 0 | break; |
1838 | 0 | case VMDK_UNALLOC: |
1839 | 0 | ret = 0; |
1840 | 0 | break; |
1841 | 0 | case VMDK_ZEROED: |
1842 | 0 | ret = BDRV_BLOCK_ZERO; |
1843 | 0 | break; |
1844 | 0 | case VMDK_OK: |
1845 | 0 | ret = BDRV_BLOCK_DATA; |
1846 | 0 | if (!extent->compressed) { |
1847 | 0 | ret |= BDRV_BLOCK_OFFSET_VALID; |
1848 | 0 | *map = cluster_offset + index_in_cluster; |
1849 | 0 | if (extent->flat) { |
1850 | 0 | ret |= BDRV_BLOCK_RECURSE; |
1851 | 0 | } |
1852 | 0 | } else { |
1853 | 0 | ret |= BDRV_BLOCK_COMPRESSED; |
1854 | 0 | } |
1855 | 0 | *file = extent->file->bs; |
1856 | 0 | break; |
1857 | 0 | } |
1858 | | |
1859 | 0 | n = extent->cluster_sectors * BDRV_SECTOR_SIZE - index_in_cluster; |
1860 | 0 | *pnum = MIN(n, bytes); |
1861 | 0 | return ret; |
1862 | 0 | } |
1863 | | |
1864 | | static int coroutine_fn GRAPH_RDLOCK |
1865 | | vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset, |
1866 | | int64_t offset_in_cluster, QEMUIOVector *qiov, |
1867 | | uint64_t qiov_offset, uint64_t n_bytes, |
1868 | | uint64_t offset) |
1869 | 0 | { |
1870 | 0 | int ret; |
1871 | 0 | VmdkGrainMarker *data = NULL; |
1872 | 0 | uLongf buf_len; |
1873 | 0 | QEMUIOVector local_qiov; |
1874 | 0 | int64_t write_offset; |
1875 | 0 | int64_t write_end_sector; |
1876 | |
|
1877 | 0 | if (extent->compressed) { |
1878 | 0 | void *compressed_data; |
1879 | | |
1880 | | /* Only whole clusters */ |
1881 | 0 | if (offset_in_cluster || |
1882 | 0 | n_bytes > (extent->cluster_sectors * SECTOR_SIZE) || |
1883 | 0 | (n_bytes < (extent->cluster_sectors * SECTOR_SIZE) && |
1884 | 0 | offset + n_bytes != extent->end_sector * SECTOR_SIZE)) |
1885 | 0 | { |
1886 | 0 | ret = -EINVAL; |
1887 | 0 | goto out; |
1888 | 0 | } |
1889 | | |
1890 | 0 | if (!extent->has_marker) { |
1891 | 0 | ret = -EINVAL; |
1892 | 0 | goto out; |
1893 | 0 | } |
1894 | 0 | buf_len = (extent->cluster_sectors << 9) * 2; |
1895 | 0 | data = g_malloc(buf_len + sizeof(VmdkGrainMarker)); |
1896 | |
|
1897 | 0 | compressed_data = g_malloc(n_bytes); |
1898 | 0 | qemu_iovec_to_buf(qiov, qiov_offset, compressed_data, n_bytes); |
1899 | 0 | ret = compress(data->data, &buf_len, compressed_data, n_bytes); |
1900 | 0 | g_free(compressed_data); |
1901 | |
|
1902 | 0 | if (ret != Z_OK || buf_len == 0) { |
1903 | 0 | ret = -EINVAL; |
1904 | 0 | goto out; |
1905 | 0 | } |
1906 | | |
1907 | 0 | data->lba = cpu_to_le64(offset >> BDRV_SECTOR_BITS); |
1908 | 0 | data->size = cpu_to_le32(buf_len); |
1909 | |
|
1910 | 0 | n_bytes = buf_len + sizeof(VmdkGrainMarker); |
1911 | 0 | qemu_iovec_init_buf(&local_qiov, data, n_bytes); |
1912 | |
|
1913 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_WRITE_COMPRESSED); |
1914 | 0 | } else { |
1915 | 0 | qemu_iovec_init(&local_qiov, qiov->niov); |
1916 | 0 | qemu_iovec_concat(&local_qiov, qiov, qiov_offset, n_bytes); |
1917 | |
|
1918 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_WRITE_AIO); |
1919 | 0 | } |
1920 | | |
1921 | 0 | write_offset = cluster_offset + offset_in_cluster; |
1922 | 0 | ret = bdrv_co_pwritev(extent->file, write_offset, n_bytes, |
1923 | 0 | &local_qiov, 0); |
1924 | |
|
1925 | 0 | write_end_sector = DIV_ROUND_UP(write_offset + n_bytes, BDRV_SECTOR_SIZE); |
1926 | |
|
1927 | 0 | if (extent->compressed) { |
1928 | 0 | extent->next_cluster_sector = write_end_sector; |
1929 | 0 | } else { |
1930 | 0 | extent->next_cluster_sector = MAX(extent->next_cluster_sector, |
1931 | 0 | write_end_sector); |
1932 | 0 | } |
1933 | |
|
1934 | 0 | if (ret < 0) { |
1935 | 0 | goto out; |
1936 | 0 | } |
1937 | 0 | ret = 0; |
1938 | 0 | out: |
1939 | 0 | g_free(data); |
1940 | 0 | if (!extent->compressed) { |
1941 | 0 | qemu_iovec_destroy(&local_qiov); |
1942 | 0 | } |
1943 | 0 | return ret; |
1944 | 0 | } |
1945 | | |
1946 | | static int coroutine_fn GRAPH_RDLOCK |
1947 | | vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset, |
1948 | | int64_t offset_in_cluster, QEMUIOVector *qiov, int bytes) |
1949 | 0 | { |
1950 | 0 | int ret; |
1951 | 0 | int64_t cluster_bytes, buf_bytes; |
1952 | 0 | uint8_t *cluster_buf, *compressed_data; |
1953 | 0 | uint8_t *uncomp_buf; |
1954 | 0 | uint32_t data_len; |
1955 | 0 | VmdkGrainMarker *marker; |
1956 | 0 | uLongf buf_len; |
1957 | | |
1958 | |
|
1959 | 0 | if (!extent->compressed) { |
1960 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_READ_AIO); |
1961 | 0 | ret = bdrv_co_preadv(extent->file, |
1962 | 0 | cluster_offset + offset_in_cluster, bytes, |
1963 | 0 | qiov, 0); |
1964 | 0 | if (ret < 0) { |
1965 | 0 | return ret; |
1966 | 0 | } |
1967 | 0 | return 0; |
1968 | 0 | } |
1969 | 0 | cluster_bytes = extent->cluster_sectors * 512; |
1970 | | /* Read two clusters in case GrainMarker + compressed data > one cluster */ |
1971 | 0 | buf_bytes = cluster_bytes * 2; |
1972 | 0 | cluster_buf = g_malloc(buf_bytes); |
1973 | 0 | uncomp_buf = g_malloc(cluster_bytes); |
1974 | 0 | BLKDBG_CO_EVENT(extent->file, BLKDBG_READ_COMPRESSED); |
1975 | 0 | ret = bdrv_co_pread(extent->file, cluster_offset, buf_bytes, cluster_buf, |
1976 | 0 | 0); |
1977 | 0 | if (ret < 0) { |
1978 | 0 | goto out; |
1979 | 0 | } |
1980 | 0 | compressed_data = cluster_buf; |
1981 | 0 | buf_len = cluster_bytes; |
1982 | 0 | data_len = cluster_bytes; |
1983 | 0 | if (extent->has_marker) { |
1984 | 0 | marker = (VmdkGrainMarker *)cluster_buf; |
1985 | 0 | compressed_data = marker->data; |
1986 | 0 | data_len = le32_to_cpu(marker->size); |
1987 | 0 | if (!data_len || data_len > buf_bytes - sizeof(VmdkGrainMarker)) { |
1988 | 0 | ret = -EINVAL; |
1989 | 0 | goto out; |
1990 | 0 | } |
1991 | 0 | } |
1992 | 0 | ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len); |
1993 | 0 | if (ret != Z_OK) { |
1994 | 0 | ret = -EINVAL; |
1995 | 0 | goto out; |
1996 | |
|
1997 | 0 | } |
1998 | 0 | if (offset_in_cluster < 0 || |
1999 | 0 | offset_in_cluster + bytes > buf_len) { |
2000 | 0 | ret = -EINVAL; |
2001 | 0 | goto out; |
2002 | 0 | } |
2003 | 0 | qemu_iovec_from_buf(qiov, 0, uncomp_buf + offset_in_cluster, bytes); |
2004 | 0 | ret = 0; |
2005 | |
|
2006 | 0 | out: |
2007 | 0 | g_free(uncomp_buf); |
2008 | 0 | g_free(cluster_buf); |
2009 | 0 | return ret; |
2010 | 0 | } |
2011 | | |
2012 | | static int coroutine_fn GRAPH_RDLOCK |
2013 | | vmdk_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, |
2014 | | QEMUIOVector *qiov, BdrvRequestFlags flags) |
2015 | 0 | { |
2016 | 0 | BDRVVmdkState *s = bs->opaque; |
2017 | 0 | int ret; |
2018 | 0 | uint64_t n_bytes, offset_in_cluster; |
2019 | 0 | VmdkExtent *extent = NULL; |
2020 | 0 | QEMUIOVector local_qiov; |
2021 | 0 | uint64_t cluster_offset; |
2022 | 0 | uint64_t bytes_done = 0; |
2023 | |
|
2024 | 0 | qemu_iovec_init(&local_qiov, qiov->niov); |
2025 | 0 | qemu_co_mutex_lock(&s->lock); |
2026 | |
|
2027 | 0 | while (bytes > 0) { |
2028 | 0 | extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent); |
2029 | 0 | if (!extent) { |
2030 | 0 | ret = -EIO; |
2031 | 0 | goto fail; |
2032 | 0 | } |
2033 | 0 | ret = get_cluster_offset(bs, extent, NULL, |
2034 | 0 | offset, false, &cluster_offset, 0, 0); |
2035 | 0 | offset_in_cluster = vmdk_find_offset_in_cluster(extent, offset); |
2036 | |
|
2037 | 0 | n_bytes = MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE |
2038 | 0 | - offset_in_cluster); |
2039 | |
|
2040 | 0 | if (ret != VMDK_OK) { |
2041 | | /* if not allocated, try to read from parent image, if exist */ |
2042 | 0 | if (bs->backing && ret != VMDK_ZEROED) { |
2043 | 0 | if (!vmdk_is_cid_valid(bs)) { |
2044 | 0 | ret = -EINVAL; |
2045 | 0 | goto fail; |
2046 | 0 | } |
2047 | | |
2048 | 0 | qemu_iovec_reset(&local_qiov); |
2049 | 0 | qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes); |
2050 | | |
2051 | | /* qcow2 emits this on bs->file instead of bs->backing */ |
2052 | 0 | BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); |
2053 | 0 | ret = bdrv_co_preadv(bs->backing, offset, n_bytes, |
2054 | 0 | &local_qiov, 0); |
2055 | 0 | if (ret < 0) { |
2056 | 0 | goto fail; |
2057 | 0 | } |
2058 | 0 | } else { |
2059 | 0 | qemu_iovec_memset(qiov, bytes_done, 0, n_bytes); |
2060 | 0 | } |
2061 | 0 | } else { |
2062 | 0 | qemu_iovec_reset(&local_qiov); |
2063 | 0 | qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes); |
2064 | |
|
2065 | 0 | ret = vmdk_read_extent(extent, cluster_offset, offset_in_cluster, |
2066 | 0 | &local_qiov, n_bytes); |
2067 | 0 | if (ret) { |
2068 | 0 | goto fail; |
2069 | 0 | } |
2070 | 0 | } |
2071 | 0 | bytes -= n_bytes; |
2072 | 0 | offset += n_bytes; |
2073 | 0 | bytes_done += n_bytes; |
2074 | 0 | } |
2075 | | |
2076 | 0 | ret = 0; |
2077 | 0 | fail: |
2078 | 0 | qemu_co_mutex_unlock(&s->lock); |
2079 | 0 | qemu_iovec_destroy(&local_qiov); |
2080 | |
|
2081 | 0 | return ret; |
2082 | 0 | } |
2083 | | |
2084 | | /** |
2085 | | * vmdk_write: |
2086 | | * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature |
2087 | | * if possible, otherwise return -ENOTSUP. |
2088 | | * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try |
2089 | | * with each cluster. By dry run we can find if the zero write |
2090 | | * is possible without modifying image data. |
2091 | | * |
2092 | | * Returns: error code with 0 for success. |
2093 | | */ |
2094 | | static int coroutine_fn GRAPH_RDLOCK |
2095 | | vmdk_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
2096 | | QEMUIOVector *qiov, bool zeroed, bool zero_dry_run) |
2097 | 0 | { |
2098 | 0 | BDRVVmdkState *s = bs->opaque; |
2099 | 0 | VmdkExtent *extent = NULL; |
2100 | 0 | int ret; |
2101 | 0 | int64_t offset_in_cluster, n_bytes; |
2102 | 0 | uint64_t cluster_offset; |
2103 | 0 | uint64_t bytes_done = 0; |
2104 | 0 | VmdkMetaData m_data; |
2105 | |
|
2106 | 0 | if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) { |
2107 | 0 | error_report("Wrong offset: offset=0x%" PRIx64 |
2108 | 0 | " total_sectors=0x%" PRIx64, |
2109 | 0 | offset, bs->total_sectors); |
2110 | 0 | return -EIO; |
2111 | 0 | } |
2112 | | |
2113 | 0 | while (bytes > 0) { |
2114 | 0 | extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent); |
2115 | 0 | if (!extent) { |
2116 | 0 | return -EIO; |
2117 | 0 | } |
2118 | 0 | if (extent->sesparse) { |
2119 | 0 | return -ENOTSUP; |
2120 | 0 | } |
2121 | 0 | offset_in_cluster = vmdk_find_offset_in_cluster(extent, offset); |
2122 | 0 | n_bytes = MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE |
2123 | 0 | - offset_in_cluster); |
2124 | |
|
2125 | 0 | ret = get_cluster_offset(bs, extent, &m_data, offset, |
2126 | 0 | !(extent->compressed || zeroed), |
2127 | 0 | &cluster_offset, offset_in_cluster, |
2128 | 0 | offset_in_cluster + n_bytes); |
2129 | 0 | if (extent->compressed) { |
2130 | 0 | if (ret == VMDK_OK) { |
2131 | | /* Refuse write to allocated cluster for streamOptimized */ |
2132 | 0 | error_report("Could not write to allocated cluster" |
2133 | 0 | " for streamOptimized"); |
2134 | 0 | return -EIO; |
2135 | 0 | } else if (!zeroed) { |
2136 | | /* allocate */ |
2137 | 0 | ret = get_cluster_offset(bs, extent, &m_data, offset, |
2138 | 0 | true, &cluster_offset, 0, 0); |
2139 | 0 | } |
2140 | 0 | } |
2141 | 0 | if (ret == VMDK_ERROR) { |
2142 | 0 | return -EINVAL; |
2143 | 0 | } |
2144 | 0 | if (zeroed) { |
2145 | | /* Do zeroed write, buf is ignored */ |
2146 | 0 | if (extent->has_zero_grain && |
2147 | 0 | offset_in_cluster == 0 && |
2148 | 0 | n_bytes >= extent->cluster_sectors * BDRV_SECTOR_SIZE) { |
2149 | 0 | n_bytes = extent->cluster_sectors * BDRV_SECTOR_SIZE; |
2150 | 0 | if (!zero_dry_run && ret != VMDK_ZEROED) { |
2151 | | /* update L2 tables */ |
2152 | 0 | if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED) |
2153 | 0 | != VMDK_OK) { |
2154 | 0 | return -EIO; |
2155 | 0 | } |
2156 | 0 | } |
2157 | 0 | } else { |
2158 | 0 | return -ENOTSUP; |
2159 | 0 | } |
2160 | 0 | } else { |
2161 | 0 | ret = vmdk_write_extent(extent, cluster_offset, offset_in_cluster, |
2162 | 0 | qiov, bytes_done, n_bytes, offset); |
2163 | 0 | if (ret) { |
2164 | 0 | return ret; |
2165 | 0 | } |
2166 | 0 | if (m_data.new_allocation) { |
2167 | | /* update L2 tables */ |
2168 | 0 | if (vmdk_L2update(extent, &m_data, |
2169 | 0 | cluster_offset >> BDRV_SECTOR_BITS) |
2170 | 0 | != VMDK_OK) { |
2171 | 0 | return -EIO; |
2172 | 0 | } |
2173 | 0 | } |
2174 | 0 | } |
2175 | 0 | bytes -= n_bytes; |
2176 | 0 | offset += n_bytes; |
2177 | 0 | bytes_done += n_bytes; |
2178 | | |
2179 | | /* update CID on the first write every time the virtual disk is |
2180 | | * opened */ |
2181 | 0 | if (!s->cid_updated) { |
2182 | 0 | ret = vmdk_write_cid(bs, g_random_int()); |
2183 | 0 | if (ret < 0) { |
2184 | 0 | return ret; |
2185 | 0 | } |
2186 | 0 | s->cid_updated = true; |
2187 | 0 | } |
2188 | 0 | } |
2189 | 0 | return 0; |
2190 | 0 | } |
2191 | | |
2192 | | static int coroutine_fn GRAPH_RDLOCK |
2193 | | vmdk_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, |
2194 | | QEMUIOVector *qiov, BdrvRequestFlags flags) |
2195 | 0 | { |
2196 | 0 | int ret; |
2197 | 0 | BDRVVmdkState *s = bs->opaque; |
2198 | 0 | qemu_co_mutex_lock(&s->lock); |
2199 | 0 | ret = vmdk_pwritev(bs, offset, bytes, qiov, false, false); |
2200 | 0 | qemu_co_mutex_unlock(&s->lock); |
2201 | 0 | return ret; |
2202 | 0 | } |
2203 | | |
2204 | | static int coroutine_fn GRAPH_RDLOCK |
2205 | | vmdk_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, int64_t bytes, |
2206 | | QEMUIOVector *qiov) |
2207 | 0 | { |
2208 | 0 | if (bytes == 0) { |
2209 | | /* The caller will write bytes 0 to signal EOF. */ |
2210 | 0 | BDRVVmdkState *s = bs->opaque; |
2211 | 0 | int ret; |
2212 | 0 | int64_t length; |
2213 | 0 | QEMU_AUTO_VFREE EosMarker *eos_marker = NULL; |
2214 | | |
2215 | | /* Stream optimized disks should only contain a single extent. */ |
2216 | 0 | assert(s->num_extents == 1); |
2217 | | |
2218 | | /* |
2219 | | * Stream optimized disks end with an EOS marker, |
2220 | | * consisting of 512 null bytes. |
2221 | | */ |
2222 | 0 | eos_marker = qemu_blockalign0(bs, sizeof(EosMarker)); |
2223 | 0 | length = bdrv_co_getlength(s->extents[0].file->bs); |
2224 | 0 | if (length < 0) { |
2225 | 0 | return length; |
2226 | 0 | } |
2227 | 0 | length = QEMU_ALIGN_UP(length, BDRV_SECTOR_SIZE); |
2228 | 0 | ret = bdrv_co_pwrite(s->extents[0].file, length, sizeof(EosMarker), |
2229 | 0 | eos_marker, 0); |
2230 | 0 | if (ret < 0) { |
2231 | 0 | return ret; |
2232 | 0 | } |
2233 | 0 | return 0; |
2234 | 0 | } |
2235 | 0 | return vmdk_co_pwritev(bs, offset, bytes, qiov, 0); |
2236 | 0 | } |
2237 | | |
2238 | | static int coroutine_fn GRAPH_RDLOCK |
2239 | | vmdk_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, |
2240 | | BdrvRequestFlags flags) |
2241 | 0 | { |
2242 | 0 | int ret; |
2243 | 0 | BDRVVmdkState *s = bs->opaque; |
2244 | |
|
2245 | 0 | qemu_co_mutex_lock(&s->lock); |
2246 | | /* write zeroes could fail if sectors not aligned to cluster, test it with |
2247 | | * dry_run == true before really updating image */ |
2248 | 0 | ret = vmdk_pwritev(bs, offset, bytes, NULL, true, true); |
2249 | 0 | if (!ret) { |
2250 | 0 | ret = vmdk_pwritev(bs, offset, bytes, NULL, true, false); |
2251 | 0 | } |
2252 | 0 | qemu_co_mutex_unlock(&s->lock); |
2253 | 0 | return ret; |
2254 | 0 | } |
2255 | | |
2256 | | static int coroutine_fn GRAPH_UNLOCKED |
2257 | | vmdk_init_extent(BlockBackend *blk, int64_t filesize, bool flat, bool compress, |
2258 | | bool zeroed_grain, Error **errp) |
2259 | 0 | { |
2260 | 0 | int ret, i; |
2261 | 0 | VMDK4Header header; |
2262 | 0 | uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count; |
2263 | 0 | uint32_t *gd_buf = NULL; |
2264 | 0 | int gd_buf_size; |
2265 | |
|
2266 | 0 | if (flat) { |
2267 | 0 | ret = blk_co_truncate(blk, filesize, false, PREALLOC_MODE_OFF, 0, errp); |
2268 | 0 | goto exit; |
2269 | 0 | } |
2270 | 0 | magic = cpu_to_be32(VMDK4_MAGIC); |
2271 | 0 | memset(&header, 0, sizeof(header)); |
2272 | 0 | if (compress) { |
2273 | 0 | header.version = 3; |
2274 | 0 | } else if (zeroed_grain) { |
2275 | 0 | header.version = 2; |
2276 | 0 | } else { |
2277 | 0 | header.version = 1; |
2278 | 0 | } |
2279 | 0 | header.flags = VMDK4_FLAG_NL_DETECT |
2280 | 0 | | (compress ? |
2281 | 0 | VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : VMDK4_FLAG_RGD) |
2282 | 0 | | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0); |
2283 | 0 | header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0; |
2284 | 0 | header.capacity = filesize / BDRV_SECTOR_SIZE; |
2285 | 0 | header.granularity = 128; |
2286 | 0 | header.num_gtes_per_gt = BDRV_SECTOR_SIZE; |
2287 | |
|
2288 | 0 | grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity); |
2289 | 0 | gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t), |
2290 | 0 | BDRV_SECTOR_SIZE); |
2291 | 0 | gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt); |
2292 | 0 | gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE); |
2293 | |
|
2294 | 0 | header.desc_offset = 1; |
2295 | 0 | header.desc_size = 20; |
2296 | |
|
2297 | 0 | if (compress) { |
2298 | 0 | header.rgd_offset = 0; |
2299 | 0 | header.gd_offset = header.desc_offset + header.desc_size; |
2300 | 0 | } else { |
2301 | 0 | header.rgd_offset = header.desc_offset + header.desc_size; |
2302 | 0 | header.gd_offset = |
2303 | 0 | header.rgd_offset + gd_sectors + (gt_size * gt_count); |
2304 | 0 | } |
2305 | |
|
2306 | 0 | header.grain_offset = |
2307 | 0 | ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count), |
2308 | 0 | header.granularity); |
2309 | | /* swap endianness for all header fields */ |
2310 | 0 | header.version = cpu_to_le32(header.version); |
2311 | 0 | header.flags = cpu_to_le32(header.flags); |
2312 | 0 | header.capacity = cpu_to_le64(header.capacity); |
2313 | 0 | header.granularity = cpu_to_le64(header.granularity); |
2314 | 0 | header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt); |
2315 | 0 | header.desc_offset = cpu_to_le64(header.desc_offset); |
2316 | 0 | header.desc_size = cpu_to_le64(header.desc_size); |
2317 | 0 | header.rgd_offset = cpu_to_le64(header.rgd_offset); |
2318 | 0 | header.gd_offset = cpu_to_le64(header.gd_offset); |
2319 | 0 | header.grain_offset = cpu_to_le64(header.grain_offset); |
2320 | 0 | header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm); |
2321 | |
|
2322 | 0 | header.check_bytes[0] = 0xa; |
2323 | 0 | header.check_bytes[1] = 0x20; |
2324 | 0 | header.check_bytes[2] = 0xd; |
2325 | 0 | header.check_bytes[3] = 0xa; |
2326 | | |
2327 | | /* write all the data */ |
2328 | 0 | ret = blk_co_pwrite(blk, 0, sizeof(magic), &magic, 0); |
2329 | 0 | if (ret < 0) { |
2330 | 0 | error_setg_errno(errp, -ret, "failed to write VMDK magic"); |
2331 | 0 | goto exit; |
2332 | 0 | } |
2333 | 0 | ret = blk_co_pwrite(blk, sizeof(magic), sizeof(header), &header, 0); |
2334 | 0 | if (ret < 0) { |
2335 | 0 | error_setg_errno(errp, -ret, "failed to write VMDK header"); |
2336 | 0 | goto exit; |
2337 | 0 | } |
2338 | | |
2339 | 0 | ret = blk_co_truncate(blk, le64_to_cpu(header.grain_offset) << 9, false, |
2340 | 0 | PREALLOC_MODE_OFF, 0, errp); |
2341 | 0 | if (ret < 0) { |
2342 | 0 | goto exit; |
2343 | 0 | } |
2344 | | |
2345 | | /* write backup grain directory */ |
2346 | 0 | gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE; |
2347 | 0 | gd_buf = g_malloc0(gd_buf_size); |
2348 | |
|
2349 | 0 | if (!compress) { |
2350 | 0 | for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors; |
2351 | 0 | i < gt_count; i++, tmp += gt_size) { |
2352 | 0 | gd_buf[i] = cpu_to_le32(tmp); |
2353 | 0 | } |
2354 | 0 | ret = blk_co_pwrite(blk, |
2355 | 0 | le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE, |
2356 | 0 | gd_buf_size, gd_buf, 0); |
2357 | 0 | if (ret < 0) { |
2358 | 0 | error_setg_errno(errp, -ret, |
2359 | 0 | "failed to write VMDK backup grain directory"); |
2360 | 0 | goto exit; |
2361 | 0 | } |
2362 | 0 | } |
2363 | | |
2364 | | /* write grain directory */ |
2365 | 0 | for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors; |
2366 | 0 | i < gt_count; i++, tmp += gt_size) { |
2367 | 0 | gd_buf[i] = cpu_to_le32(tmp); |
2368 | 0 | } |
2369 | 0 | ret = blk_co_pwrite(blk, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE, |
2370 | 0 | gd_buf_size, gd_buf, 0); |
2371 | 0 | if (ret < 0) { |
2372 | 0 | error_setg_errno(errp, -ret, |
2373 | 0 | "failed to write VMDK grain directory"); |
2374 | 0 | } |
2375 | |
|
2376 | 0 | ret = 0; |
2377 | 0 | exit: |
2378 | 0 | g_free(gd_buf); |
2379 | 0 | return ret; |
2380 | 0 | } |
2381 | | |
2382 | | static int coroutine_fn GRAPH_UNLOCKED |
2383 | | vmdk_create_extent(const char *filename, int64_t filesize, bool flat, |
2384 | | bool compress, bool zeroed_grain, BlockBackend **pbb, |
2385 | | QemuOpts *opts, Error **errp) |
2386 | 0 | { |
2387 | 0 | int ret; |
2388 | 0 | BlockBackend *blk = NULL; |
2389 | |
|
2390 | 0 | ret = bdrv_co_create_file(filename, opts, false, errp); |
2391 | 0 | if (ret < 0) { |
2392 | 0 | goto exit; |
2393 | 0 | } |
2394 | | |
2395 | 0 | blk = blk_co_new_open(filename, NULL, NULL, |
2396 | 0 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, |
2397 | 0 | errp); |
2398 | 0 | if (blk == NULL) { |
2399 | 0 | ret = -EIO; |
2400 | 0 | goto exit; |
2401 | 0 | } |
2402 | | |
2403 | 0 | blk_set_allow_write_beyond_eof(blk, true); |
2404 | |
|
2405 | 0 | ret = vmdk_init_extent(blk, filesize, flat, compress, zeroed_grain, errp); |
2406 | 0 | exit: |
2407 | 0 | if (blk) { |
2408 | 0 | if (pbb) { |
2409 | 0 | *pbb = blk; |
2410 | 0 | } else { |
2411 | 0 | blk_co_unref(blk); |
2412 | 0 | blk = NULL; |
2413 | 0 | } |
2414 | 0 | } |
2415 | 0 | return ret; |
2416 | 0 | } |
2417 | | |
2418 | | static int filename_decompose(const char *filename, char *path, char *prefix, |
2419 | | char *postfix, size_t buf_len, Error **errp) |
2420 | 0 | { |
2421 | 0 | const char *p, *q; |
2422 | |
|
2423 | 0 | if (filename == NULL || !strlen(filename)) { |
2424 | 0 | error_setg(errp, "No filename provided"); |
2425 | 0 | return VMDK_ERROR; |
2426 | 0 | } |
2427 | 0 | p = strrchr(filename, '/'); |
2428 | 0 | if (p == NULL) { |
2429 | 0 | p = strrchr(filename, '\\'); |
2430 | 0 | } |
2431 | 0 | if (p == NULL) { |
2432 | 0 | p = strrchr(filename, ':'); |
2433 | 0 | } |
2434 | 0 | if (p != NULL) { |
2435 | 0 | p++; |
2436 | 0 | if (p - filename >= buf_len) { |
2437 | 0 | return VMDK_ERROR; |
2438 | 0 | } |
2439 | 0 | pstrcpy(path, p - filename + 1, filename); |
2440 | 0 | } else { |
2441 | 0 | p = filename; |
2442 | 0 | path[0] = '\0'; |
2443 | 0 | } |
2444 | 0 | q = strrchr(p, '.'); |
2445 | 0 | if (q == NULL) { |
2446 | 0 | pstrcpy(prefix, buf_len, p); |
2447 | 0 | postfix[0] = '\0'; |
2448 | 0 | } else { |
2449 | 0 | if (q - p >= buf_len) { |
2450 | 0 | return VMDK_ERROR; |
2451 | 0 | } |
2452 | 0 | pstrcpy(prefix, q - p + 1, p); |
2453 | 0 | pstrcpy(postfix, buf_len, q); |
2454 | 0 | } |
2455 | 0 | return VMDK_OK; |
2456 | 0 | } |
2457 | | |
2458 | | /* |
2459 | | * idx == 0: get or create the descriptor file (also the image file if in a |
2460 | | * non-split format. |
2461 | | * idx >= 1: get the n-th extent if in a split subformat |
2462 | | */ |
2463 | | typedef BlockBackend * coroutine_fn GRAPH_UNLOCKED_PTR |
2464 | | (*vmdk_create_extent_fn)(int64_t size, int idx, bool flat, bool split, |
2465 | | bool compress, bool zeroed_grain, void *opaque, |
2466 | | Error **errp); |
2467 | | |
2468 | | static void vmdk_desc_add_extent(GString *desc, |
2469 | | const char *extent_line_fmt, |
2470 | | int64_t size, const char *filename) |
2471 | 0 | { |
2472 | 0 | char *basename = g_path_get_basename(filename); |
2473 | |
|
2474 | 0 | g_string_append_printf(desc, extent_line_fmt, |
2475 | 0 | DIV_ROUND_UP(size, BDRV_SECTOR_SIZE), basename); |
2476 | 0 | g_free(basename); |
2477 | 0 | } |
2478 | | |
2479 | | static int coroutine_fn GRAPH_UNLOCKED |
2480 | | vmdk_co_do_create(int64_t size, |
2481 | | BlockdevVmdkSubformat subformat, |
2482 | | BlockdevVmdkAdapterType adapter_type, |
2483 | | const char *backing_file, |
2484 | | const char *hw_version, |
2485 | | const char *toolsversion, |
2486 | | bool compat6, |
2487 | | bool zeroed_grain, |
2488 | | vmdk_create_extent_fn extent_fn, |
2489 | | void *opaque, |
2490 | | Error **errp) |
2491 | 0 | { |
2492 | 0 | int extent_idx; |
2493 | 0 | BlockBackend *blk = NULL; |
2494 | 0 | BlockBackend *extent_blk; |
2495 | 0 | Error *local_err = NULL; |
2496 | 0 | char *desc = NULL; |
2497 | 0 | int ret = 0; |
2498 | 0 | bool flat, split, compress; |
2499 | 0 | GString *ext_desc_lines; |
2500 | 0 | const int64_t split_size = 0x80000000; /* VMDK has constant split size */ |
2501 | 0 | int64_t extent_size; |
2502 | 0 | int64_t created_size = 0; |
2503 | 0 | const char *extent_line_fmt; |
2504 | 0 | char *parent_desc_line = g_malloc0(BUF_SIZE); |
2505 | 0 | uint32_t parent_cid = 0xffffffff; |
2506 | 0 | uint32_t number_heads = 16; |
2507 | 0 | uint32_t desc_offset = 0, desc_len; |
2508 | 0 | const char desc_template[] = |
2509 | 0 | "# Disk DescriptorFile\n" |
2510 | 0 | "version=1\n" |
2511 | 0 | "CID=%" PRIx32 "\n" |
2512 | 0 | "parentCID=%" PRIx32 "\n" |
2513 | 0 | "createType=\"%s\"\n" |
2514 | 0 | "%s" |
2515 | 0 | "\n" |
2516 | 0 | "# Extent description\n" |
2517 | 0 | "%s" |
2518 | 0 | "\n" |
2519 | 0 | "# The Disk Data Base\n" |
2520 | 0 | "#DDB\n" |
2521 | 0 | "\n" |
2522 | 0 | "ddb.virtualHWVersion = \"%s\"\n" |
2523 | 0 | "ddb.geometry.cylinders = \"%" PRId64 "\"\n" |
2524 | 0 | "ddb.geometry.heads = \"%" PRIu32 "\"\n" |
2525 | 0 | "ddb.geometry.sectors = \"63\"\n" |
2526 | 0 | "ddb.adapterType = \"%s\"\n" |
2527 | 0 | "ddb.toolsVersion = \"%s\"\n"; |
2528 | |
|
2529 | 0 | ext_desc_lines = g_string_new(NULL); |
2530 | | |
2531 | | /* Read out options */ |
2532 | 0 | if (compat6) { |
2533 | 0 | if (hw_version) { |
2534 | 0 | error_setg(errp, |
2535 | 0 | "compat6 cannot be enabled with hwversion set"); |
2536 | 0 | ret = -EINVAL; |
2537 | 0 | goto exit; |
2538 | 0 | } |
2539 | 0 | hw_version = "6"; |
2540 | 0 | } |
2541 | 0 | if (!hw_version) { |
2542 | 0 | hw_version = "4"; |
2543 | 0 | } |
2544 | 0 | if (!toolsversion) { |
2545 | 0 | toolsversion = "2147483647"; |
2546 | 0 | } |
2547 | |
|
2548 | 0 | if (adapter_type != BLOCKDEV_VMDK_ADAPTER_TYPE_IDE) { |
2549 | | /* that's the number of heads with which vmware operates when |
2550 | | creating, exporting, etc. vmdk files with a non-ide adapter type */ |
2551 | 0 | number_heads = 255; |
2552 | 0 | } |
2553 | 0 | split = (subformat == BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT) || |
2554 | 0 | (subformat == BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTSPARSE); |
2555 | 0 | flat = (subformat == BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICFLAT) || |
2556 | 0 | (subformat == BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT); |
2557 | 0 | compress = subformat == BLOCKDEV_VMDK_SUBFORMAT_STREAMOPTIMIZED; |
2558 | |
|
2559 | 0 | if (flat) { |
2560 | 0 | extent_line_fmt = "RW %" PRId64 " FLAT \"%s\" 0\n"; |
2561 | 0 | } else { |
2562 | 0 | extent_line_fmt = "RW %" PRId64 " SPARSE \"%s\"\n"; |
2563 | 0 | } |
2564 | 0 | if (flat && backing_file) { |
2565 | 0 | error_setg(errp, "Flat image can't have backing file"); |
2566 | 0 | ret = -ENOTSUP; |
2567 | 0 | goto exit; |
2568 | 0 | } |
2569 | 0 | if (flat && zeroed_grain) { |
2570 | 0 | error_setg(errp, "Flat image can't enable zeroed grain"); |
2571 | 0 | ret = -ENOTSUP; |
2572 | 0 | goto exit; |
2573 | 0 | } |
2574 | | |
2575 | | /* Create extents */ |
2576 | 0 | if (split) { |
2577 | 0 | extent_size = split_size; |
2578 | 0 | } else { |
2579 | 0 | extent_size = size; |
2580 | 0 | } |
2581 | 0 | if (!split && !flat) { |
2582 | 0 | created_size = extent_size; |
2583 | 0 | } else { |
2584 | 0 | created_size = 0; |
2585 | 0 | } |
2586 | | /* Get the descriptor file BDS */ |
2587 | 0 | blk = extent_fn(created_size, 0, flat, split, compress, zeroed_grain, |
2588 | 0 | opaque, errp); |
2589 | 0 | if (!blk) { |
2590 | 0 | ret = -EIO; |
2591 | 0 | goto exit; |
2592 | 0 | } |
2593 | 0 | if (!split && !flat) { |
2594 | 0 | vmdk_desc_add_extent(ext_desc_lines, extent_line_fmt, created_size, |
2595 | 0 | blk_bs(blk)->filename); |
2596 | 0 | } |
2597 | |
|
2598 | 0 | if (backing_file) { |
2599 | 0 | BlockBackend *backing; |
2600 | 0 | char *full_backing = |
2601 | 0 | bdrv_get_full_backing_filename_from_filename(blk_bs(blk)->filename, |
2602 | 0 | backing_file, |
2603 | 0 | &local_err); |
2604 | 0 | if (local_err) { |
2605 | 0 | error_propagate(errp, local_err); |
2606 | 0 | ret = -ENOENT; |
2607 | 0 | goto exit; |
2608 | 0 | } |
2609 | 0 | assert(full_backing); |
2610 | |
|
2611 | 0 | backing = blk_co_new_open(full_backing, NULL, NULL, |
2612 | 0 | BDRV_O_NO_BACKING, errp); |
2613 | 0 | g_free(full_backing); |
2614 | 0 | if (backing == NULL) { |
2615 | 0 | ret = -EIO; |
2616 | 0 | goto exit; |
2617 | 0 | } |
2618 | 0 | if (strcmp(blk_bs(backing)->drv->format_name, "vmdk")) { |
2619 | 0 | error_setg(errp, "Invalid backing file format: %s. Must be vmdk", |
2620 | 0 | blk_bs(backing)->drv->format_name); |
2621 | 0 | blk_co_unref(backing); |
2622 | 0 | ret = -EINVAL; |
2623 | 0 | goto exit; |
2624 | 0 | } |
2625 | | |
2626 | 0 | bdrv_graph_co_rdlock(); |
2627 | 0 | ret = vmdk_read_cid(blk_bs(backing), 0, &parent_cid); |
2628 | 0 | bdrv_graph_co_rdunlock(); |
2629 | 0 | blk_co_unref(backing); |
2630 | 0 | if (ret) { |
2631 | 0 | error_setg(errp, "Failed to read parent CID"); |
2632 | 0 | goto exit; |
2633 | 0 | } |
2634 | 0 | snprintf(parent_desc_line, BUF_SIZE, |
2635 | 0 | "parentFileNameHint=\"%s\"", backing_file); |
2636 | 0 | } |
2637 | 0 | extent_idx = 1; |
2638 | 0 | while (created_size < size) { |
2639 | 0 | int64_t cur_size = MIN(size - created_size, extent_size); |
2640 | 0 | extent_blk = extent_fn(cur_size, extent_idx, flat, split, compress, |
2641 | 0 | zeroed_grain, opaque, errp); |
2642 | 0 | if (!extent_blk) { |
2643 | 0 | ret = -EINVAL; |
2644 | 0 | goto exit; |
2645 | 0 | } |
2646 | 0 | vmdk_desc_add_extent(ext_desc_lines, extent_line_fmt, cur_size, |
2647 | 0 | blk_bs(extent_blk)->filename); |
2648 | 0 | created_size += cur_size; |
2649 | 0 | extent_idx++; |
2650 | 0 | blk_co_unref(extent_blk); |
2651 | 0 | } |
2652 | | |
2653 | | /* Check whether we got excess extents */ |
2654 | 0 | extent_blk = extent_fn(-1, extent_idx, flat, split, compress, zeroed_grain, |
2655 | 0 | opaque, NULL); |
2656 | 0 | if (extent_blk) { |
2657 | 0 | blk_co_unref(extent_blk); |
2658 | 0 | error_setg(errp, "List of extents contains unused extents"); |
2659 | 0 | ret = -EINVAL; |
2660 | 0 | goto exit; |
2661 | 0 | } |
2662 | | |
2663 | | /* generate descriptor file */ |
2664 | 0 | desc = g_strdup_printf(desc_template, |
2665 | 0 | g_random_int(), |
2666 | 0 | parent_cid, |
2667 | 0 | BlockdevVmdkSubformat_str(subformat), |
2668 | 0 | parent_desc_line, |
2669 | 0 | ext_desc_lines->str, |
2670 | 0 | hw_version, |
2671 | 0 | size / |
2672 | 0 | (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE), |
2673 | 0 | number_heads, |
2674 | 0 | BlockdevVmdkAdapterType_str(adapter_type), |
2675 | 0 | toolsversion); |
2676 | 0 | desc_len = strlen(desc); |
2677 | | /* the descriptor offset = 0x200 */ |
2678 | 0 | if (!split && !flat) { |
2679 | 0 | desc_offset = 0x200; |
2680 | 0 | } |
2681 | |
|
2682 | 0 | ret = blk_co_pwrite(blk, desc_offset, desc_len, desc, 0); |
2683 | 0 | if (ret < 0) { |
2684 | 0 | error_setg_errno(errp, -ret, "Could not write description"); |
2685 | 0 | goto exit; |
2686 | 0 | } |
2687 | | /* bdrv_pwrite write padding zeros to align to sector, we don't need that |
2688 | | * for description file */ |
2689 | 0 | if (desc_offset == 0) { |
2690 | 0 | ret = blk_co_truncate(blk, desc_len, false, PREALLOC_MODE_OFF, 0, errp); |
2691 | 0 | if (ret < 0) { |
2692 | 0 | goto exit; |
2693 | 0 | } |
2694 | 0 | } |
2695 | 0 | ret = 0; |
2696 | 0 | exit: |
2697 | 0 | if (blk) { |
2698 | 0 | blk_co_unref(blk); |
2699 | 0 | } |
2700 | 0 | g_free(desc); |
2701 | 0 | g_free(parent_desc_line); |
2702 | 0 | g_string_free(ext_desc_lines, true); |
2703 | 0 | return ret; |
2704 | 0 | } |
2705 | | |
2706 | | typedef struct { |
2707 | | char *path; |
2708 | | char *prefix; |
2709 | | char *postfix; |
2710 | | QemuOpts *opts; |
2711 | | } VMDKCreateOptsData; |
2712 | | |
2713 | | static BlockBackend * coroutine_fn GRAPH_UNLOCKED |
2714 | | vmdk_co_create_opts_cb(int64_t size, int idx, bool flat, bool split, |
2715 | | bool compress, bool zeroed_grain, void *opaque, |
2716 | | Error **errp) |
2717 | 0 | { |
2718 | 0 | BlockBackend *blk = NULL; |
2719 | 0 | BlockDriverState *bs = NULL; |
2720 | 0 | VMDKCreateOptsData *data = opaque; |
2721 | 0 | char *ext_filename = NULL; |
2722 | 0 | char *rel_filename = NULL; |
2723 | | |
2724 | | /* We're done, don't create excess extents. */ |
2725 | 0 | if (size == -1) { |
2726 | 0 | assert(errp == NULL); |
2727 | 0 | return NULL; |
2728 | 0 | } |
2729 | | |
2730 | 0 | if (idx == 0) { |
2731 | 0 | rel_filename = g_strdup_printf("%s%s", data->prefix, data->postfix); |
2732 | 0 | } else if (split) { |
2733 | 0 | rel_filename = g_strdup_printf("%s-%c%03d%s", |
2734 | 0 | data->prefix, |
2735 | 0 | flat ? 'f' : 's', idx, data->postfix); |
2736 | 0 | } else { |
2737 | 0 | assert(idx == 1); |
2738 | 0 | rel_filename = g_strdup_printf("%s-flat%s", data->prefix, data->postfix); |
2739 | 0 | } |
2740 | |
|
2741 | 0 | ext_filename = g_strdup_printf("%s%s", data->path, rel_filename); |
2742 | 0 | g_free(rel_filename); |
2743 | |
|
2744 | 0 | if (vmdk_create_extent(ext_filename, size, |
2745 | 0 | flat, compress, zeroed_grain, &blk, data->opts, |
2746 | 0 | errp)) { |
2747 | 0 | goto exit; |
2748 | 0 | } |
2749 | 0 | bdrv_co_unref(bs); |
2750 | 0 | exit: |
2751 | 0 | g_free(ext_filename); |
2752 | 0 | return blk; |
2753 | 0 | } |
2754 | | |
2755 | | static int coroutine_fn GRAPH_UNLOCKED |
2756 | | vmdk_co_create_opts(BlockDriver *drv, const char *filename, |
2757 | | QemuOpts *opts, Error **errp) |
2758 | 0 | { |
2759 | 0 | Error *local_err = NULL; |
2760 | 0 | char *desc = NULL; |
2761 | 0 | int64_t total_size = 0; |
2762 | 0 | char *adapter_type = NULL; |
2763 | 0 | BlockdevVmdkAdapterType adapter_type_enum; |
2764 | 0 | char *backing_file = NULL; |
2765 | 0 | char *hw_version = NULL; |
2766 | 0 | char *toolsversion = NULL; |
2767 | 0 | char *fmt = NULL; |
2768 | 0 | BlockdevVmdkSubformat subformat; |
2769 | 0 | int ret = 0; |
2770 | 0 | char *path = g_malloc0(PATH_MAX); |
2771 | 0 | char *prefix = g_malloc0(PATH_MAX); |
2772 | 0 | char *postfix = g_malloc0(PATH_MAX); |
2773 | 0 | char *desc_line = g_malloc0(BUF_SIZE); |
2774 | 0 | char *ext_filename = g_malloc0(PATH_MAX); |
2775 | 0 | char *desc_filename = g_malloc0(PATH_MAX); |
2776 | 0 | char *parent_desc_line = g_malloc0(BUF_SIZE); |
2777 | 0 | bool zeroed_grain; |
2778 | 0 | bool compat6; |
2779 | 0 | VMDKCreateOptsData data; |
2780 | 0 | char *backing_fmt = NULL; |
2781 | |
|
2782 | 0 | backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); |
2783 | 0 | if (backing_fmt && strcmp(backing_fmt, "vmdk") != 0) { |
2784 | 0 | error_setg(errp, "backing_file must be a vmdk image"); |
2785 | 0 | ret = -EINVAL; |
2786 | 0 | goto exit; |
2787 | 0 | } |
2788 | | |
2789 | 0 | if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) { |
2790 | 0 | ret = -EINVAL; |
2791 | 0 | goto exit; |
2792 | 0 | } |
2793 | | /* Read out options */ |
2794 | 0 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
2795 | 0 | BDRV_SECTOR_SIZE); |
2796 | 0 | adapter_type = qemu_opt_get_del(opts, BLOCK_OPT_ADAPTER_TYPE); |
2797 | 0 | backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); |
2798 | 0 | hw_version = qemu_opt_get_del(opts, BLOCK_OPT_HWVERSION); |
2799 | 0 | toolsversion = qemu_opt_get_del(opts, BLOCK_OPT_TOOLSVERSION); |
2800 | 0 | compat6 = qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false); |
2801 | 0 | if (strcmp(hw_version, "undefined") == 0) { |
2802 | 0 | g_free(hw_version); |
2803 | 0 | hw_version = NULL; |
2804 | 0 | } |
2805 | 0 | fmt = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT); |
2806 | 0 | zeroed_grain = qemu_opt_get_bool_del(opts, BLOCK_OPT_ZEROED_GRAIN, false); |
2807 | |
|
2808 | 0 | if (adapter_type) { |
2809 | 0 | adapter_type_enum = qapi_enum_parse(&BlockdevVmdkAdapterType_lookup, |
2810 | 0 | adapter_type, |
2811 | 0 | BLOCKDEV_VMDK_ADAPTER_TYPE_IDE, |
2812 | 0 | &local_err); |
2813 | 0 | if (local_err) { |
2814 | 0 | error_propagate(errp, local_err); |
2815 | 0 | ret = -EINVAL; |
2816 | 0 | goto exit; |
2817 | 0 | } |
2818 | 0 | } else { |
2819 | 0 | adapter_type_enum = BLOCKDEV_VMDK_ADAPTER_TYPE_IDE; |
2820 | 0 | } |
2821 | | |
2822 | 0 | if (!fmt) { |
2823 | | /* Default format to monolithicSparse */ |
2824 | 0 | subformat = BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE; |
2825 | 0 | } else { |
2826 | 0 | subformat = qapi_enum_parse(&BlockdevVmdkSubformat_lookup, |
2827 | 0 | fmt, |
2828 | 0 | BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE, |
2829 | 0 | &local_err); |
2830 | 0 | if (local_err) { |
2831 | 0 | error_propagate(errp, local_err); |
2832 | 0 | ret = -EINVAL; |
2833 | 0 | goto exit; |
2834 | 0 | } |
2835 | 0 | } |
2836 | 0 | data = (VMDKCreateOptsData){ |
2837 | 0 | .prefix = prefix, |
2838 | 0 | .postfix = postfix, |
2839 | 0 | .path = path, |
2840 | 0 | .opts = opts, |
2841 | 0 | }; |
2842 | 0 | ret = vmdk_co_do_create(total_size, subformat, adapter_type_enum, |
2843 | 0 | backing_file, hw_version, toolsversion, compat6, |
2844 | 0 | zeroed_grain, vmdk_co_create_opts_cb, &data, errp); |
2845 | |
|
2846 | 0 | exit: |
2847 | 0 | g_free(backing_fmt); |
2848 | 0 | g_free(adapter_type); |
2849 | 0 | g_free(backing_file); |
2850 | 0 | g_free(hw_version); |
2851 | 0 | g_free(toolsversion); |
2852 | 0 | g_free(fmt); |
2853 | 0 | g_free(desc); |
2854 | 0 | g_free(path); |
2855 | 0 | g_free(prefix); |
2856 | 0 | g_free(postfix); |
2857 | 0 | g_free(desc_line); |
2858 | 0 | g_free(ext_filename); |
2859 | 0 | g_free(desc_filename); |
2860 | 0 | g_free(parent_desc_line); |
2861 | 0 | return ret; |
2862 | 0 | } |
2863 | | |
2864 | | static BlockBackend * coroutine_fn GRAPH_UNLOCKED |
2865 | | vmdk_co_create_cb(int64_t size, int idx, bool flat, bool split, bool compress, |
2866 | | bool zeroed_grain, void *opaque, Error **errp) |
2867 | 0 | { |
2868 | 0 | int ret; |
2869 | 0 | BlockDriverState *bs; |
2870 | 0 | BlockBackend *blk; |
2871 | 0 | BlockdevCreateOptionsVmdk *opts = opaque; |
2872 | |
|
2873 | 0 | if (idx == 0) { |
2874 | 0 | bs = bdrv_co_open_blockdev_ref(opts->file, errp); |
2875 | 0 | } else { |
2876 | 0 | int i; |
2877 | 0 | BlockdevRefList *list = opts->extents; |
2878 | 0 | for (i = 1; i < idx; i++) { |
2879 | 0 | if (!list || !list->next) { |
2880 | 0 | error_setg(errp, "Extent [%d] not specified", i); |
2881 | 0 | return NULL; |
2882 | 0 | } |
2883 | 0 | list = list->next; |
2884 | 0 | } |
2885 | 0 | if (!list) { |
2886 | 0 | error_setg(errp, "Extent [%d] not specified", idx - 1); |
2887 | 0 | return NULL; |
2888 | 0 | } |
2889 | 0 | bs = bdrv_co_open_blockdev_ref(list->value, errp); |
2890 | 0 | } |
2891 | 0 | if (!bs) { |
2892 | 0 | return NULL; |
2893 | 0 | } |
2894 | 0 | blk = blk_co_new_with_bs(bs, |
2895 | 0 | BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | |
2896 | 0 | BLK_PERM_RESIZE, |
2897 | 0 | BLK_PERM_ALL, |
2898 | 0 | errp); |
2899 | 0 | if (!blk) { |
2900 | 0 | return NULL; |
2901 | 0 | } |
2902 | 0 | blk_set_allow_write_beyond_eof(blk, true); |
2903 | 0 | bdrv_co_unref(bs); |
2904 | |
|
2905 | 0 | if (size != -1) { |
2906 | 0 | ret = vmdk_init_extent(blk, size, flat, compress, zeroed_grain, errp); |
2907 | 0 | if (ret) { |
2908 | 0 | blk_co_unref(blk); |
2909 | 0 | blk = NULL; |
2910 | 0 | } |
2911 | 0 | } |
2912 | 0 | return blk; |
2913 | 0 | } |
2914 | | |
2915 | | static int coroutine_fn GRAPH_UNLOCKED |
2916 | | vmdk_co_create(BlockdevCreateOptions *create_options, Error **errp) |
2917 | 0 | { |
2918 | 0 | BlockdevCreateOptionsVmdk *opts; |
2919 | |
|
2920 | 0 | opts = &create_options->u.vmdk; |
2921 | | |
2922 | | /* Validate options */ |
2923 | 0 | if (!QEMU_IS_ALIGNED(opts->size, BDRV_SECTOR_SIZE)) { |
2924 | 0 | error_setg(errp, "Image size must be a multiple of 512 bytes"); |
2925 | 0 | return -EINVAL; |
2926 | 0 | } |
2927 | | |
2928 | 0 | return vmdk_co_do_create(opts->size, |
2929 | 0 | opts->subformat, |
2930 | 0 | opts->adapter_type, |
2931 | 0 | opts->backing_file, |
2932 | 0 | opts->hwversion, |
2933 | 0 | opts->toolsversion, |
2934 | 0 | false, |
2935 | 0 | opts->zeroed_grain, |
2936 | 0 | vmdk_co_create_cb, |
2937 | 0 | opts, errp); |
2938 | 0 | } |
2939 | | |
2940 | | static void vmdk_close(BlockDriverState *bs) |
2941 | 0 | { |
2942 | 0 | BDRVVmdkState *s = bs->opaque; |
2943 | |
|
2944 | 0 | vmdk_free_extents(bs); |
2945 | 0 | g_free(s->create_type); |
2946 | |
|
2947 | 0 | migrate_del_blocker(&s->migration_blocker); |
2948 | 0 | } |
2949 | | |
2950 | | static int64_t coroutine_fn GRAPH_RDLOCK |
2951 | | vmdk_co_get_allocated_file_size(BlockDriverState *bs) |
2952 | 0 | { |
2953 | 0 | int i; |
2954 | 0 | int64_t ret = 0; |
2955 | 0 | int64_t r; |
2956 | 0 | BDRVVmdkState *s = bs->opaque; |
2957 | |
|
2958 | 0 | ret = bdrv_co_get_allocated_file_size(bs->file->bs); |
2959 | 0 | if (ret < 0) { |
2960 | 0 | return ret; |
2961 | 0 | } |
2962 | 0 | for (i = 0; i < s->num_extents; i++) { |
2963 | 0 | if (s->extents[i].file == bs->file) { |
2964 | 0 | continue; |
2965 | 0 | } |
2966 | 0 | r = bdrv_co_get_allocated_file_size(s->extents[i].file->bs); |
2967 | 0 | if (r < 0) { |
2968 | 0 | return r; |
2969 | 0 | } |
2970 | 0 | ret += r; |
2971 | 0 | } |
2972 | 0 | return ret; |
2973 | 0 | } |
2974 | | |
2975 | | static int GRAPH_RDLOCK vmdk_has_zero_init(BlockDriverState *bs) |
2976 | 0 | { |
2977 | 0 | int i; |
2978 | 0 | BDRVVmdkState *s = bs->opaque; |
2979 | | |
2980 | | /* If has a flat extent and its underlying storage doesn't have zero init, |
2981 | | * return 0. */ |
2982 | 0 | for (i = 0; i < s->num_extents; i++) { |
2983 | 0 | if (s->extents[i].flat) { |
2984 | 0 | if (!bdrv_has_zero_init(s->extents[i].file->bs)) { |
2985 | 0 | return 0; |
2986 | 0 | } |
2987 | 0 | } |
2988 | 0 | } |
2989 | 0 | return 1; |
2990 | 0 | } |
2991 | | |
2992 | | static VmdkExtentInfo * GRAPH_RDLOCK vmdk_get_extent_info(VmdkExtent *extent) |
2993 | 0 | { |
2994 | 0 | VmdkExtentInfo *info = g_new0(VmdkExtentInfo, 1); |
2995 | |
|
2996 | 0 | bdrv_refresh_filename(extent->file->bs); |
2997 | 0 | *info = (VmdkExtentInfo){ |
2998 | 0 | .filename = g_strdup(extent->file->bs->filename), |
2999 | 0 | .format = g_strdup(extent->type), |
3000 | 0 | .virtual_size = extent->sectors * BDRV_SECTOR_SIZE, |
3001 | 0 | .compressed = extent->compressed, |
3002 | 0 | .has_compressed = extent->compressed, |
3003 | 0 | .cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE, |
3004 | 0 | .has_cluster_size = !extent->flat, |
3005 | 0 | }; |
3006 | |
|
3007 | 0 | return info; |
3008 | 0 | } |
3009 | | |
3010 | | static int coroutine_fn GRAPH_RDLOCK |
3011 | | vmdk_co_check(BlockDriverState *bs, BdrvCheckResult *result, BdrvCheckMode fix) |
3012 | 0 | { |
3013 | 0 | BDRVVmdkState *s = bs->opaque; |
3014 | 0 | VmdkExtent *extent = NULL; |
3015 | 0 | int64_t sector_num = 0; |
3016 | 0 | int64_t total_sectors = bdrv_co_nb_sectors(bs); |
3017 | 0 | int ret; |
3018 | 0 | uint64_t cluster_offset; |
3019 | |
|
3020 | 0 | if (fix) { |
3021 | 0 | return -ENOTSUP; |
3022 | 0 | } |
3023 | | |
3024 | 0 | for (;;) { |
3025 | 0 | if (sector_num >= total_sectors) { |
3026 | 0 | return 0; |
3027 | 0 | } |
3028 | 0 | extent = find_extent(s, sector_num, extent); |
3029 | 0 | if (!extent) { |
3030 | 0 | fprintf(stderr, |
3031 | 0 | "ERROR: could not find extent for sector %" PRId64 "\n", |
3032 | 0 | sector_num); |
3033 | 0 | ret = -EINVAL; |
3034 | 0 | break; |
3035 | 0 | } |
3036 | 0 | ret = get_cluster_offset(bs, extent, NULL, |
3037 | 0 | sector_num << BDRV_SECTOR_BITS, |
3038 | 0 | false, &cluster_offset, 0, 0); |
3039 | 0 | if (ret == VMDK_ERROR) { |
3040 | 0 | fprintf(stderr, |
3041 | 0 | "ERROR: could not get cluster_offset for sector %" |
3042 | 0 | PRId64 "\n", sector_num); |
3043 | 0 | break; |
3044 | 0 | } |
3045 | 0 | if (ret == VMDK_OK) { |
3046 | 0 | int64_t extent_len = bdrv_co_getlength(extent->file->bs); |
3047 | 0 | if (extent_len < 0) { |
3048 | 0 | fprintf(stderr, |
3049 | 0 | "ERROR: could not get extent file length for sector %" |
3050 | 0 | PRId64 "\n", sector_num); |
3051 | 0 | ret = extent_len; |
3052 | 0 | break; |
3053 | 0 | } |
3054 | 0 | if (cluster_offset >= extent_len) { |
3055 | 0 | fprintf(stderr, |
3056 | 0 | "ERROR: cluster offset for sector %" |
3057 | 0 | PRId64 " points after EOF\n", sector_num); |
3058 | 0 | ret = -EINVAL; |
3059 | 0 | break; |
3060 | 0 | } |
3061 | 0 | } |
3062 | 0 | sector_num += extent->cluster_sectors; |
3063 | 0 | } |
3064 | | |
3065 | 0 | result->corruptions++; |
3066 | 0 | return ret; |
3067 | 0 | } |
3068 | | |
3069 | | static ImageInfoSpecific * GRAPH_RDLOCK |
3070 | | vmdk_get_specific_info(BlockDriverState *bs, Error **errp) |
3071 | 0 | { |
3072 | 0 | int i; |
3073 | 0 | BDRVVmdkState *s = bs->opaque; |
3074 | 0 | ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1); |
3075 | 0 | VmdkExtentInfoList **tail; |
3076 | |
|
3077 | 0 | *spec_info = (ImageInfoSpecific){ |
3078 | 0 | .type = IMAGE_INFO_SPECIFIC_KIND_VMDK, |
3079 | 0 | .u = { |
3080 | 0 | .vmdk.data = g_new0(ImageInfoSpecificVmdk, 1), |
3081 | 0 | }, |
3082 | 0 | }; |
3083 | |
|
3084 | 0 | *spec_info->u.vmdk.data = (ImageInfoSpecificVmdk) { |
3085 | 0 | .create_type = g_strdup(s->create_type), |
3086 | 0 | .cid = s->cid, |
3087 | 0 | .parent_cid = s->parent_cid, |
3088 | 0 | }; |
3089 | |
|
3090 | 0 | tail = &spec_info->u.vmdk.data->extents; |
3091 | 0 | for (i = 0; i < s->num_extents; i++) { |
3092 | 0 | QAPI_LIST_APPEND(tail, vmdk_get_extent_info(&s->extents[i])); |
3093 | 0 | } |
3094 | |
|
3095 | 0 | return spec_info; |
3096 | 0 | } |
3097 | | |
3098 | | static bool vmdk_extents_type_eq(const VmdkExtent *a, const VmdkExtent *b) |
3099 | 0 | { |
3100 | 0 | return a->flat == b->flat && |
3101 | 0 | a->compressed == b->compressed && |
3102 | 0 | (a->flat || a->cluster_sectors == b->cluster_sectors); |
3103 | 0 | } |
3104 | | |
3105 | | static int coroutine_fn |
3106 | | vmdk_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
3107 | 0 | { |
3108 | 0 | int i; |
3109 | 0 | BDRVVmdkState *s = bs->opaque; |
3110 | 0 | assert(s->num_extents); |
3111 | | |
3112 | | /* See if we have multiple extents but they have different cases */ |
3113 | 0 | for (i = 1; i < s->num_extents; i++) { |
3114 | 0 | if (!vmdk_extents_type_eq(&s->extents[0], &s->extents[i])) { |
3115 | 0 | return -ENOTSUP; |
3116 | 0 | } |
3117 | 0 | } |
3118 | 0 | bdi->needs_compressed_writes = s->extents[0].compressed; |
3119 | 0 | if (!s->extents[0].flat) { |
3120 | 0 | bdi->cluster_size = s->extents[0].cluster_sectors << BDRV_SECTOR_BITS; |
3121 | 0 | } |
3122 | 0 | return 0; |
3123 | 0 | } |
3124 | | |
3125 | | static void GRAPH_RDLOCK |
3126 | | vmdk_gather_child_options(BlockDriverState *bs, QDict *target, |
3127 | | bool backing_overridden) |
3128 | 0 | { |
3129 | | /* No children but file and backing can be explicitly specified (TODO) */ |
3130 | 0 | qdict_put(target, "file", |
3131 | 0 | qobject_ref(bs->file->bs->full_open_options)); |
3132 | |
|
3133 | 0 | if (backing_overridden) { |
3134 | 0 | if (bs->backing) { |
3135 | 0 | qdict_put(target, "backing", |
3136 | 0 | qobject_ref(bs->backing->bs->full_open_options)); |
3137 | 0 | } else { |
3138 | 0 | qdict_put_null(target, "backing"); |
3139 | 0 | } |
3140 | 0 | } |
3141 | 0 | } |
3142 | | |
3143 | | static QemuOptsList vmdk_create_opts = { |
3144 | | .name = "vmdk-create-opts", |
3145 | | .head = QTAILQ_HEAD_INITIALIZER(vmdk_create_opts.head), |
3146 | | .desc = { |
3147 | | { |
3148 | | .name = BLOCK_OPT_SIZE, |
3149 | | .type = QEMU_OPT_SIZE, |
3150 | | .help = "Virtual disk size" |
3151 | | }, |
3152 | | { |
3153 | | .name = BLOCK_OPT_ADAPTER_TYPE, |
3154 | | .type = QEMU_OPT_STRING, |
3155 | | .help = "Virtual adapter type, can be one of " |
3156 | | "ide (default), lsilogic, buslogic or legacyESX" |
3157 | | }, |
3158 | | { |
3159 | | .name = BLOCK_OPT_BACKING_FILE, |
3160 | | .type = QEMU_OPT_STRING, |
3161 | | .help = "File name of a base image" |
3162 | | }, |
3163 | | { |
3164 | | .name = BLOCK_OPT_BACKING_FMT, |
3165 | | .type = QEMU_OPT_STRING, |
3166 | | .help = "Must be 'vmdk' if present", |
3167 | | }, |
3168 | | { |
3169 | | .name = BLOCK_OPT_COMPAT6, |
3170 | | .type = QEMU_OPT_BOOL, |
3171 | | .help = "VMDK version 6 image", |
3172 | | .def_value_str = "off" |
3173 | | }, |
3174 | | { |
3175 | | .name = BLOCK_OPT_HWVERSION, |
3176 | | .type = QEMU_OPT_STRING, |
3177 | | .help = "VMDK hardware version", |
3178 | | .def_value_str = "undefined" |
3179 | | }, |
3180 | | { |
3181 | | .name = BLOCK_OPT_TOOLSVERSION, |
3182 | | .type = QEMU_OPT_STRING, |
3183 | | .help = "VMware guest tools version", |
3184 | | }, |
3185 | | { |
3186 | | .name = BLOCK_OPT_SUBFMT, |
3187 | | .type = QEMU_OPT_STRING, |
3188 | | .help = |
3189 | | "VMDK flat extent format, can be one of " |
3190 | | "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} " |
3191 | | }, |
3192 | | { |
3193 | | .name = BLOCK_OPT_ZEROED_GRAIN, |
3194 | | .type = QEMU_OPT_BOOL, |
3195 | | .help = "Enable efficient zero writes " |
3196 | | "using the zeroed-grain GTE feature" |
3197 | | }, |
3198 | | { /* end of list */ } |
3199 | | } |
3200 | | }; |
3201 | | |
3202 | | static BlockDriver bdrv_vmdk = { |
3203 | | .format_name = "vmdk", |
3204 | | .instance_size = sizeof(BDRVVmdkState), |
3205 | | .bdrv_probe = vmdk_probe, |
3206 | | .bdrv_open = vmdk_open, |
3207 | | .bdrv_co_check = vmdk_co_check, |
3208 | | .bdrv_reopen_prepare = vmdk_reopen_prepare, |
3209 | | .bdrv_reopen_commit = vmdk_reopen_commit, |
3210 | | .bdrv_reopen_abort = vmdk_reopen_abort, |
3211 | | .bdrv_child_perm = bdrv_default_perms, |
3212 | | .bdrv_co_preadv = vmdk_co_preadv, |
3213 | | .bdrv_co_pwritev = vmdk_co_pwritev, |
3214 | | .bdrv_co_pwritev_compressed = vmdk_co_pwritev_compressed, |
3215 | | .bdrv_co_pwrite_zeroes = vmdk_co_pwrite_zeroes, |
3216 | | .bdrv_close = vmdk_close, |
3217 | | .bdrv_co_create_opts = vmdk_co_create_opts, |
3218 | | .bdrv_co_create = vmdk_co_create, |
3219 | | .bdrv_co_block_status = vmdk_co_block_status, |
3220 | | .bdrv_co_get_allocated_file_size = vmdk_co_get_allocated_file_size, |
3221 | | .bdrv_has_zero_init = vmdk_has_zero_init, |
3222 | | .bdrv_get_specific_info = vmdk_get_specific_info, |
3223 | | .bdrv_refresh_limits = vmdk_refresh_limits, |
3224 | | .bdrv_co_get_info = vmdk_co_get_info, |
3225 | | .bdrv_gather_child_options = vmdk_gather_child_options, |
3226 | | |
3227 | | .is_format = true, |
3228 | | .supports_backing = true, |
3229 | | .create_opts = &vmdk_create_opts, |
3230 | | }; |
3231 | | |
3232 | | static void bdrv_vmdk_init(void) |
3233 | 63 | { |
3234 | 63 | bdrv_register(&bdrv_vmdk); |
3235 | 63 | } |
3236 | | |
3237 | | block_init(bdrv_vmdk_init); |