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