blob: 0dfab6e94130246a2dbc8d0646513f90487fbb6d [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
2 * Block driver for the VMDK format
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardea2384d2004-08-01 21:59:26 +00004 * Copyright (c) 2004 Fabrice Bellard
bellardff1afc72005-04-26 21:08:00 +00005 * Copyright (c) 2005 Filip Navara
ths5fafdf22007-09-16 21:08:06 +00006 *
bellardea2384d2004-08-01 21:59:26 +00007 * 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 */
ths5f4da8c2007-01-24 21:05:24 +000025
Peter Maydell80c71a22016-01-18 18:01:42 +000026#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010027#include "qapi/error.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010028#include "block/block_int.h"
Kevin Wolfc4bea162016-03-08 15:57:05 +010029#include "sysemu/block-backend.h"
Max Reitzabc521a2019-02-01 20:29:26 +010030#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010031#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010032#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010034#include "qemu/option.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010035#include "qemu/bswap.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020036#include "migration/blocker.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020037#include "qemu/cutils.h"
Stefan Weil2923d342011-11-20 12:34:30 +010038#include <zlib.h>
bellardea2384d2004-08-01 21:59:26 +000039
bellardea2384d2004-08-01 21:59:26 +000040#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
41#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
Fam Zheng432bb172011-08-12 23:19:30 +080042#define VMDK4_COMPRESSION_DEFLATE 1
Fam Zheng95b0aa42013-05-02 10:25:25 +080043#define VMDK4_FLAG_NL_DETECT (1 << 0)
Fam Zhengbb45ded2011-08-12 23:19:34 +080044#define VMDK4_FLAG_RGD (1 << 1)
Fam Zheng14ead642013-05-02 10:25:23 +080045/* Zeroed-grain enable bit */
46#define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
Fam Zheng432bb172011-08-12 23:19:30 +080047#define VMDK4_FLAG_COMPRESS (1 << 16)
48#define VMDK4_FLAG_MARKER (1 << 17)
Kevin Wolf65bd1552012-08-16 10:39:33 +020049#define VMDK4_GD_AT_END 0xffffffffffffffffULL
bellardea2384d2004-08-01 21:59:26 +000050
yuchenlina77672e2018-03-22 21:33:37 +080051#define VMDK_EXTENT_MAX_SECTORS (1ULL << 32)
52
Fam Zheng14ead642013-05-02 10:25:23 +080053#define VMDK_GTE_ZEROED 0x1
Fam Zheng65f74722013-05-02 10:25:22 +080054
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 Zheng69e0b6d2013-05-02 10:25:24 +080062#define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
Thomas Weißschuhf3d43df2021-09-13 15:04:19 +020063#define BLOCK_OPT_TOOLSVERSION "toolsversion"
Fam Zheng69e0b6d2013-05-02 10:25:24 +080064
bellardea2384d2004-08-01 21:59:26 +000065typedef 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 Zheng5d8caa52013-08-06 15:44:47 +080076} QEMU_PACKED VMDK3Header;
bellardea2384d2004-08-01 21:59:26 +000077
78typedef struct {
79 uint32_t version;
80 uint32_t flags;
Fam Zhenge98768d2013-08-06 15:44:48 +080081 uint64_t capacity;
82 uint64_t granularity;
83 uint64_t desc_offset;
84 uint64_t desc_size;
Fam Zhengca8804c2013-08-06 15:44:55 +080085 /* Number of GrainTableEntries per GrainTable */
86 uint32_t num_gtes_per_gt;
Fam Zhenge98768d2013-08-06 15:44:48 +080087 uint64_t rgd_offset;
88 uint64_t gd_offset;
89 uint64_t grain_offset;
bellardea2384d2004-08-01 21:59:26 +000090 char filler[1];
91 char check_bytes[4];
Fam Zheng432bb172011-08-12 23:19:30 +080092 uint16_t compressAlgorithm;
Stefan Weil541dc0d2011-08-31 12:38:01 +020093} QEMU_PACKED VMDK4Header;
bellardea2384d2004-08-01 21:59:26 +000094
Sam Eiderman98eb9732019-06-20 12:10:57 +030095typedef 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
125typedef 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
bellardea2384d2004-08-01 21:59:26 +0000133#define L2_CACHE_SIZE 16
134
Fam Zhengb3976d32011-07-12 19:56:28 +0800135typedef struct VmdkExtent {
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200136 BdrvChild *file;
Fam Zhengb3976d32011-07-12 19:56:28 +0800137 bool flat;
Fam Zheng432bb172011-08-12 23:19:30 +0800138 bool compressed;
139 bool has_marker;
Fam Zheng14ead642013-05-02 10:25:23 +0800140 bool has_zero_grain;
Sam Eiderman98eb9732019-06-20 12:10:57 +0300141 bool sesparse;
142 uint64_t sesparse_l2_tables_offset;
143 uint64_t sesparse_clusters_offset;
144 int32_t entry_size;
Fam Zheng14ead642013-05-02 10:25:23 +0800145 int version;
Fam Zhengb3976d32011-07-12 19:56:28 +0800146 int64_t sectors;
147 int64_t end_sector;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800148 int64_t flat_start_offset;
bellardea2384d2004-08-01 21:59:26 +0000149 int64_t l1_table_offset;
bellardff1afc72005-04-26 21:08:00 +0000150 int64_t l1_backup_table_offset;
Sam Eiderman98eb9732019-06-20 12:10:57 +0300151 void *l1_table;
bellardff1afc72005-04-26 21:08:00 +0000152 uint32_t *l1_backup_table;
bellardea2384d2004-08-01 21:59:26 +0000153 unsigned int l1_size;
154 uint32_t l1_entry_sectors;
155
156 unsigned int l2_size;
Sam Eiderman98eb9732019-06-20 12:10:57 +0300157 void *l2_cache;
bellardea2384d2004-08-01 21:59:26 +0000158 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
159 uint32_t l2_cache_counts[L2_CACHE_SIZE];
160
Fam Zheng301c7d32013-09-23 17:18:29 +0800161 int64_t cluster_sectors;
Fam Zhengc6ac36e2014-07-30 14:39:10 +0800162 int64_t next_cluster_sector;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800163 char *type;
Fam Zhengb3976d32011-07-12 19:56:28 +0800164} VmdkExtent;
165
166typedef struct BDRVVmdkState {
Paolo Bonzini848c66e2011-10-20 13:16:21 +0200167 CoMutex lock;
Fam Zhenge98768d2013-08-06 15:44:48 +0800168 uint64_t desc_offset;
Fam Zheng69b4d862011-07-12 19:56:34 +0800169 bool cid_updated;
Fam Zhengc338b6a2013-10-18 13:17:19 +0800170 bool cid_checked;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800171 uint32_t cid;
ths5f4da8c2007-01-24 21:05:24 +0000172 uint32_t parent_cid;
Fam Zhengb3976d32011-07-12 19:56:28 +0800173 int num_extents;
174 /* Extent array with num_extents entries, ascend ordered by address */
175 VmdkExtent *extents;
Kevin Wolf2bc31662011-11-22 16:50:27 +0100176 Error *migration_blocker;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800177 char *create_type;
bellardea2384d2004-08-01 21:59:26 +0000178} BDRVVmdkState;
179
ths630530a2007-06-18 15:01:30 +0000180typedef struct VmdkMetaData {
ths630530a2007-06-18 15:01:30 +0000181 unsigned int l1_index;
182 unsigned int l2_index;
183 unsigned int l2_offset;
Kevin Wolf4dc20e62020-04-30 15:30:02 +0200184 bool new_allocation;
Fam Zhengcdeaf1f2013-05-02 10:25:27 +0800185 uint32_t *l2_cache_entry;
ths630530a2007-06-18 15:01:30 +0000186} VmdkMetaData;
187
Fam Zheng432bb172011-08-12 23:19:30 +0800188typedef struct VmdkGrainMarker {
189 uint64_t lba;
190 uint32_t size;
Philippe Mathieu-Daudé880a7812020-03-04 16:38:16 +0100191 uint8_t data[];
Fam Zheng5d8caa52013-08-06 15:44:47 +0800192} QEMU_PACKED VmdkGrainMarker;
Fam Zheng432bb172011-08-12 23:19:30 +0800193
Kevin Wolf65bd1552012-08-16 10:39:33 +0200194enum {
195 MARKER_END_OF_STREAM = 0,
196 MARKER_GRAIN_TABLE = 1,
197 MARKER_GRAIN_DIRECTORY = 2,
198 MARKER_FOOTER = 3,
199};
200
bellardea2384d2004-08-01 21:59:26 +0000201static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
202{
203 uint32_t magic;
204
Fam Zhengae261c82011-07-12 19:56:38 +0800205 if (buf_size < 4) {
bellardea2384d2004-08-01 21:59:26 +0000206 return 0;
Fam Zhengae261c82011-07-12 19:56:38 +0800207 }
bellardea2384d2004-08-01 21:59:26 +0000208 magic = be32_to_cpu(*(uint32_t *)buf);
209 if (magic == VMDK3_MAGIC ||
Fam Zheng01fc99d2011-07-12 19:56:30 +0800210 magic == VMDK4_MAGIC) {
bellardea2384d2004-08-01 21:59:26 +0000211 return 100;
Fam Zheng01fc99d2011-07-12 19:56:30 +0800212 } 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 Eidermanb69864e2019-03-14 16:14:37 +0200241 strncmp("version=2\n", p, strlen("version=2\n")) == 0 ||
242 strncmp("version=3\n", p, strlen("version=3\n")) == 0) {
Fam Zheng01fc99d2011-07-12 19:56:30 +0800243 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 Eidermanb69864e2019-03-14 16:14:37 +0200248 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 Zheng01fc99d2011-07-12 19:56:30 +0800250 return 100;
251 }
252 }
253 return 0;
254 }
bellardea2384d2004-08-01 21:59:26 +0000255 return 0;
Fam Zheng01fc99d2011-07-12 19:56:30 +0800256 }
bellardea2384d2004-08-01 21:59:26 +0000257}
258
ths3b46e622007-09-17 08:09:54 +0000259#define SECTOR_SIZE 512
Fam Zhengf66fd6c2011-07-19 08:45:23 +0800260#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 */
ths5f4da8c2007-01-24 21:05:24 +0000263
Fam Zhengb3976d32011-07-12 19:56:28 +0800264static void vmdk_free_extents(BlockDriverState *bs)
265{
266 int i;
267 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800268 VmdkExtent *e;
Fam Zhengb3976d32011-07-12 19:56:28 +0800269
270 for (i = 0; i < s->num_extents; i++) {
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800271 e = &s->extents[i];
272 g_free(e->l1_table);
273 g_free(e->l2_cache);
274 g_free(e->l1_backup_table);
Fam Zhengf4c129a2013-10-31 10:06:23 +0800275 g_free(e->type);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200276 if (e->file != bs->file) {
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200277 bdrv_unref_child(bs, e->file);
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800278 }
Fam Zhengb3976d32011-07-12 19:56:28 +0800279 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500280 g_free(s->extents);
Fam Zhengb3976d32011-07-12 19:56:28 +0800281}
282
Fam Zheng86c6b422011-08-12 23:19:28 +0800283static 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 Armbruster5839e532014-08-19 10:31:08 +0200291 s->extents = g_renew(VmdkExtent, s->extents, s->num_extents);
Fam Zheng86c6b422011-08-12 23:19:28 +0800292}
293
Peter Maydell98778602017-07-09 18:06:14 +0100294/* Return -ve errno, or 0 on success and write CID into *pcid. */
295static int vmdk_read_cid(BlockDriverState *bs, int parent, uint32_t *pcid)
ths5f4da8c2007-01-24 21:05:24 +0000296{
Fam Zheng5997c212016-03-08 16:24:35 +0800297 char *desc;
Peter Maydell98778602017-07-09 18:06:14 +0100298 uint32_t cid;
blueswir17ccfb2e2008-09-14 06:45:34 +0000299 const char *p_name, *cid_str;
ths5f4da8c2007-01-24 21:05:24 +0000300 size_t cid_str_size;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800301 BDRVVmdkState *s = bs->opaque;
Kevin Wolf99f18352011-10-26 12:25:25 +0200302 int ret;
ths5f4da8c2007-01-24 21:05:24 +0000303
Fam Zheng5997c212016-03-08 16:24:35 +0800304 desc = g_malloc0(DESC_SIZE);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200305 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
Kevin Wolf99f18352011-10-26 12:25:25 +0200306 if (ret < 0) {
Peter Maydell98778602017-07-09 18:06:14 +0100307 goto out;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800308 }
ths5f4da8c2007-01-24 21:05:24 +0000309
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 Wolf93897b92011-10-26 12:25:52 +0200318 desc[DESC_SIZE - 1] = '\0';
Fam Zhengae261c82011-07-12 19:56:38 +0800319 p_name = strstr(desc, cid_str);
Peter Maydell98778602017-07-09 18:06:14 +0100320 if (p_name == NULL) {
321 ret = -EINVAL;
322 goto out;
ths5f4da8c2007-01-24 21:05:24 +0000323 }
Peter Maydell98778602017-07-09 18:06:14 +0100324 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;
ths5f4da8c2007-01-24 21:05:24 +0000331
Peter Maydell98778602017-07-09 18:06:14 +0100332out:
Fam Zheng5997c212016-03-08 16:24:35 +0800333 g_free(desc);
Peter Maydell98778602017-07-09 18:06:14 +0100334 return ret;
ths5f4da8c2007-01-24 21:05:24 +0000335}
336
337static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
338{
Fam Zheng965415e2016-03-08 16:24:34 +0800339 char *desc, *tmp_desc;
ths5f4da8c2007-01-24 21:05:24 +0000340 char *p_name, *tmp_str;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800341 BDRVVmdkState *s = bs->opaque;
Fam Zheng965415e2016-03-08 16:24:34 +0800342 int ret = 0;
ths5f4da8c2007-01-24 21:05:24 +0000343
Fam Zheng965415e2016-03-08 16:24:34 +0800344 desc = g_malloc0(DESC_SIZE);
345 tmp_desc = g_malloc0(DESC_SIZE);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200346 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
Kevin Wolf99f18352011-10-26 12:25:25 +0200347 if (ret < 0) {
Fam Zheng965415e2016-03-08 16:24:34 +0800348 goto out;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800349 }
ths5f4da8c2007-01-24 21:05:24 +0000350
Kevin Wolf93897b92011-10-26 12:25:52 +0200351 desc[DESC_SIZE - 1] = '\0';
Fam Zhengae261c82011-07-12 19:56:38 +0800352 tmp_str = strstr(desc, "parentCID");
Kevin Wolf93897b92011-10-26 12:25:52 +0200353 if (tmp_str == NULL) {
Fam Zheng965415e2016-03-08 16:24:34 +0800354 ret = -EINVAL;
355 goto out;
Kevin Wolf93897b92011-10-26 12:25:52 +0200356 }
357
Fam Zheng965415e2016-03-08 16:24:34 +0800358 pstrcpy(tmp_desc, DESC_SIZE, tmp_str);
Fam Zhengae261c82011-07-12 19:56:38 +0800359 p_name = strstr(desc, "CID");
360 if (p_name != NULL) {
ths5f4da8c2007-01-24 21:05:24 +0000361 p_name += sizeof("CID");
Fam Zheng965415e2016-03-08 16:24:34 +0800362 snprintf(p_name, DESC_SIZE - (p_name - desc), "%" PRIx32 "\n", cid);
363 pstrcat(desc, DESC_SIZE, tmp_desc);
ths5f4da8c2007-01-24 21:05:24 +0000364 }
365
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200366 ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
Kevin Wolf99f18352011-10-26 12:25:25 +0200367
Fam Zheng965415e2016-03-08 16:24:34 +0800368out:
369 g_free(desc);
370 g_free(tmp_desc);
371 return ret;
ths5f4da8c2007-01-24 21:05:24 +0000372}
373
374static int vmdk_is_cid_valid(BlockDriverState *bs)
375{
ths5f4da8c2007-01-24 21:05:24 +0000376 BDRVVmdkState *s = bs->opaque;
ths5f4da8c2007-01-24 21:05:24 +0000377 uint32_t cur_pcid;
378
Kevin Wolf760e0062015-06-17 14:55:21 +0200379 if (!s->cid_checked && bs->backing) {
380 BlockDriverState *p_bs = bs->backing->bs;
381
Max Reitz439e89f2018-07-02 23:07:20 +0200382 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 Maydell98778602017-07-09 18:06:14 +0100388 if (vmdk_read_cid(p_bs, 0, &cur_pcid) != 0) {
389 /* read failure: report as not valid */
390 return 0;
391 }
Fam Zhengae261c82011-07-12 19:56:38 +0800392 if (s->parent_cid != cur_pcid) {
393 /* CID not valid */
ths5f4da8c2007-01-24 21:05:24 +0000394 return 0;
Fam Zhengae261c82011-07-12 19:56:38 +0800395 }
ths5f4da8c2007-01-24 21:05:24 +0000396 }
Fam Zhengc338b6a2013-10-18 13:17:19 +0800397 s->cid_checked = true;
Fam Zhengae261c82011-07-12 19:56:38 +0800398 /* CID valid */
ths5f4da8c2007-01-24 21:05:24 +0000399 return 1;
400}
401
Kevin Wolf67251a32015-04-09 18:54:04 +0200402/* We have nothing to do for VMDK reopen, stubs just return success */
Jeff Cody38975752012-09-20 15:13:30 -0400403static int vmdk_reopen_prepare(BDRVReopenState *state,
404 BlockReopenQueue *queue, Error **errp)
405{
Jeff Cody38975752012-09-20 15:13:30 -0400406 assert(state != NULL);
407 assert(state->bs != NULL);
Kevin Wolf67251a32015-04-09 18:54:04 +0200408 return 0;
Jeff Cody38975752012-09-20 15:13:30 -0400409}
410
Kevin Wolf9949f972010-04-16 21:07:19 +0200411static int vmdk_parent_open(BlockDriverState *bs)
ths5f4da8c2007-01-24 21:05:24 +0000412{
ths5fafdf22007-09-16 21:08:06 +0000413 char *p_name;
Fam Zheng71968db2016-03-08 16:24:36 +0800414 char *desc;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800415 BDRVVmdkState *s = bs->opaque;
Paolo Bonzini588b65a2011-10-20 13:16:19 +0200416 int ret;
ths5f4da8c2007-01-24 21:05:24 +0000417
Fam Zheng71968db2016-03-08 16:24:36 +0800418 desc = g_malloc0(DESC_SIZE + 1);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200419 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
Paolo Bonzini588b65a2011-10-20 13:16:19 +0200420 if (ret < 0) {
Fam Zheng71968db2016-03-08 16:24:36 +0800421 goto out;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800422 }
Fam Zheng71968db2016-03-08 16:24:36 +0800423 ret = 0;
ths5f4da8c2007-01-24 21:05:24 +0000424
Fam Zhengae261c82011-07-12 19:56:38 +0800425 p_name = strstr(desc, "parentFileNameHint");
426 if (p_name != NULL) {
ths5f4da8c2007-01-24 21:05:24 +0000427 char *end_name;
ths5f4da8c2007-01-24 21:05:24 +0000428
429 p_name += sizeof("parentFileNameHint") + 1;
Fam Zhengae261c82011-07-12 19:56:38 +0800430 end_name = strchr(p_name, '\"');
431 if (end_name == NULL) {
Fam Zheng71968db2016-03-08 16:24:36 +0800432 ret = -EINVAL;
433 goto out;
Fam Zhengae261c82011-07-12 19:56:38 +0800434 }
Max Reitz998c2012019-02-01 20:29:08 +0100435 if ((end_name - p_name) > sizeof(bs->auto_backing_file) - 1) {
Fam Zheng71968db2016-03-08 16:24:36 +0800436 ret = -EINVAL;
437 goto out;
Fam Zhengae261c82011-07-12 19:56:38 +0800438 }
ths3b46e622007-09-17 08:09:54 +0000439
Max Reitz998c2012019-02-01 20:29:08 +0100440 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 Eiderman7502be82019-03-26 21:58:37 +0200443 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
444 "vmdk");
ths5f4da8c2007-01-24 21:05:24 +0000445 }
446
Fam Zheng71968db2016-03-08 16:24:36 +0800447out:
448 g_free(desc);
449 return ret;
ths5f4da8c2007-01-24 21:05:24 +0000450}
451
Fam Zhengb3976d32011-07-12 19:56:28 +0800452/* Create and append extent to the extent array. Return the added VmdkExtent
453 * address. return NULL if allocation failed. */
Fam Zheng8aa13312013-08-06 15:44:51 +0800454static int vmdk_add_extent(BlockDriverState *bs,
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200455 BdrvChild *file, bool flat, int64_t sectors,
Fam Zhengb3976d32011-07-12 19:56:28 +0800456 int64_t l1_offset, int64_t l1_backup_offset,
457 uint32_t l1_size,
Fam Zheng8aa13312013-08-06 15:44:51 +0800458 int l2_size, uint64_t cluster_sectors,
Fam Zheng48239702013-10-11 15:43:22 +0800459 VmdkExtent **new_extent,
460 Error **errp)
Fam Zhengb3976d32011-07-12 19:56:28 +0800461{
462 VmdkExtent *extent;
463 BDRVVmdkState *s = bs->opaque;
Markus Armbruster0a156f72014-08-21 14:36:19 +0200464 int64_t nb_sectors;
Fam Zhengb3976d32011-07-12 19:56:28 +0800465
Fam Zheng8aa13312013-08-06 15:44:51 +0800466 if (cluster_sectors > 0x200000) {
467 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
Fam Zheng48239702013-10-11 15:43:22 +0800468 error_setg(errp, "Invalid granularity, image may be corrupt");
469 return -EFBIG;
Fam Zheng8aa13312013-08-06 15:44:51 +0800470 }
Sam Eiderman59d6ee42019-06-20 12:10:56 +0300471 if (l1_size > 32 * 1024 * 1024) {
Sam Eiderman940a2cd2019-06-20 12:10:55 +0300472 /*
473 * Although with big capacity and small l1_entry_sectors, we can get a
Fam Zhengb0651b82013-08-19 18:54:25 +0800474 * big l1_size, we don't want unbounded value to allocate the table.
Sam Eiderman59d6ee42019-06-20 12:10:56 +0300475 * 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 Eiderman98eb9732019-06-20 12:10:57 +0300481 * 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 Eiderman940a2cd2019-06-20 12:10:55 +0300486 */
Fam Zheng48239702013-10-11 15:43:22 +0800487 error_setg(errp, "L1 size too big");
Fam Zhengb0651b82013-08-19 18:54:25 +0800488 return -EFBIG;
489 }
Fam Zheng8aa13312013-08-06 15:44:51 +0800490
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200491 nb_sectors = bdrv_nb_sectors(file->bs);
Markus Armbruster0a156f72014-08-21 14:36:19 +0200492 if (nb_sectors < 0) {
493 return nb_sectors;
Fam Zhengc6ac36e2014-07-30 14:39:10 +0800494 }
495
Markus Armbruster5839e532014-08-19 10:31:08 +0200496 s->extents = g_renew(VmdkExtent, s->extents, s->num_extents + 1);
Fam Zhengb3976d32011-07-12 19:56:28 +0800497 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 Zheng301c7d32013-09-23 17:18:29 +0800509 extent->cluster_sectors = flat ? sectors : cluster_sectors;
Markus Armbruster0a156f72014-08-21 14:36:19 +0200510 extent->next_cluster_sector = ROUND_UP(nb_sectors, cluster_sectors);
Sam Eiderman98eb9732019-06-20 12:10:57 +0300511 extent->entry_size = sizeof(uint32_t);
Fam Zhengb3976d32011-07-12 19:56:28 +0800512
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 Zheng8aa13312013-08-06 15:44:51 +0800519 if (new_extent) {
520 *new_extent = extent;
521 }
522 return 0;
Fam Zhengb3976d32011-07-12 19:56:28 +0800523}
524
Fam Zheng48239702013-10-11 15:43:22 +0800525static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
526 Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000527{
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800528 int ret;
Fam Zheng13c49412015-05-05 17:28:13 +0800529 size_t l1_size;
530 int i;
ths5f4da8c2007-01-24 21:05:24 +0000531
bellardea2384d2004-08-01 21:59:26 +0000532 /* read the L1 table */
Sam Eiderman98eb9732019-06-20 12:10:57 +0300533 l1_size = extent->l1_size * extent->entry_size;
Kevin Wolfd6e59932014-05-20 13:56:27 +0200534 extent->l1_table = g_try_malloc(l1_size);
535 if (l1_size && extent->l1_table == NULL) {
536 return -ENOMEM;
537 }
538
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200539 ret = bdrv_pread(extent->file,
Fam Zheng48239702013-10-11 15:43:22 +0800540 extent->l1_table_offset,
541 extent->l1_table,
542 l1_size);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800543 if (ret < 0) {
Max Reitzf30c66b2019-02-01 20:29:05 +0100544 bdrv_refresh_filename(extent->file->bs);
Fam Zheng48239702013-10-11 15:43:22 +0800545 error_setg_errno(errp, -ret,
546 "Could not read l1 table from extent '%s'",
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200547 extent->file->bs->filename);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800548 goto fail_l1;
Fam Zhengb3976d32011-07-12 19:56:28 +0800549 }
550 for (i = 0; i < extent->l1_size; i++) {
Sam Eiderman98eb9732019-06-20 12:10:57 +0300551 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 }
bellardea2384d2004-08-01 21:59:26 +0000557 }
558
Fam Zhengb3976d32011-07-12 19:56:28 +0800559 if (extent->l1_backup_table_offset) {
Sam Eiderman98eb9732019-06-20 12:10:57 +0300560 assert(!extent->sesparse);
Kevin Wolfd6e59932014-05-20 13:56:27 +0200561 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 Wolfcf2ab8f2016-06-20 18:24:02 +0200566 ret = bdrv_pread(extent->file,
Fam Zheng48239702013-10-11 15:43:22 +0800567 extent->l1_backup_table_offset,
568 extent->l1_backup_table,
569 l1_size);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800570 if (ret < 0) {
Max Reitzf30c66b2019-02-01 20:29:05 +0100571 bdrv_refresh_filename(extent->file->bs);
Fam Zheng48239702013-10-11 15:43:22 +0800572 error_setg_errno(errp, -ret,
573 "Could not read l1 backup table from extent '%s'",
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200574 extent->file->bs->filename);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800575 goto fail_l1b;
Fam Zhengb3976d32011-07-12 19:56:28 +0800576 }
577 for (i = 0; i < extent->l1_size; i++) {
578 le32_to_cpus(&extent->l1_backup_table[i]);
bellardff1afc72005-04-26 21:08:00 +0000579 }
580 }
581
Fam Zhengb3976d32011-07-12 19:56:28 +0800582 extent->l2_cache =
Sam Eiderman98eb9732019-06-20 12:10:57 +0300583 g_malloc(extent->entry_size * extent->l2_size * L2_CACHE_SIZE);
bellardea2384d2004-08-01 21:59:26 +0000584 return 0;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800585 fail_l1b:
Anthony Liguori7267c092011-08-20 22:09:37 -0500586 g_free(extent->l1_backup_table);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800587 fail_l1:
Anthony Liguori7267c092011-08-20 22:09:37 -0500588 g_free(extent->l1_table);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800589 return ret;
590}
591
Fam Zhengdaac8fd2013-08-19 18:54:27 +0800592static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200593 BdrvChild *file,
Fam Zheng48239702013-10-11 15:43:22 +0800594 int flags, Error **errp)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800595{
596 int ret;
597 uint32_t magic;
598 VMDK3Header header;
Christian Borntraegercd466702020-09-30 17:58:56 +0200599 VmdkExtent *extent = NULL;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800600
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200601 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800602 if (ret < 0) {
Max Reitzf30c66b2019-02-01 20:29:05 +0100603 bdrv_refresh_filename(file->bs);
Fam Zheng48239702013-10-11 15:43:22 +0800604 error_setg_errno(errp, -ret,
605 "Could not read header from file '%s'",
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200606 file->bs->filename);
Fam Zheng86c6b422011-08-12 23:19:28 +0800607 return ret;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800608 }
Fam Zhengf6b61e52013-08-19 18:54:26 +0800609 ret = vmdk_add_extent(bs, file, false,
610 le32_to_cpu(header.disk_sectors),
Fam Zheng7237aec2015-04-27 22:23:01 +0800611 (int64_t)le32_to_cpu(header.l1dir_offset) << 9,
Fam Zhengf6b61e52013-08-19 18:54:26 +0800612 0,
613 le32_to_cpu(header.l1dir_size),
614 4096,
615 le32_to_cpu(header.granularity),
Fam Zheng48239702013-10-11 15:43:22 +0800616 &extent,
617 errp);
Fam Zheng8aa13312013-08-06 15:44:51 +0800618 if (ret < 0) {
619 return ret;
620 }
Fam Zheng48239702013-10-11 15:43:22 +0800621 ret = vmdk_init_tables(bs, extent, errp);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800622 if (ret) {
Fam Zheng86c6b422011-08-12 23:19:28 +0800623 /* free extent allocated by vmdk_add_extent */
624 vmdk_free_last_extent(bs);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800625 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800626 return ret;
627}
628
Sam Eiderman98eb9732019-06-20 12:10:57 +0300629#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 */
633static 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
720static 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
748static 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 Borntraegercd466702020-09-30 17:58:56 +0200755 VmdkExtent *extent = NULL;
Sam Eiderman98eb9732019-06-20 12:10:57 +0300756
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 Bonzinid1833ef2014-02-17 14:44:03 +0100828static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
Kevin Wolfa6468362015-04-07 15:35:59 +0200829 QDict *options, Error **errp);
Fam Zhengf16f5092011-08-12 23:19:33 +0800830
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200831static char *vmdk_read_desc(BdrvChild *file, uint64_t desc_offset, Error **errp)
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100832{
833 int64_t size;
834 char *buf;
835 int ret;
836
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200837 size = bdrv_getlength(file->bs);
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100838 if (size < 0) {
839 error_setg_errno(errp, -size, "Could not access file");
840 return NULL;
841 }
842
Fam Zheng03c33592014-12-04 07:28:32 +0800843 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 Zheng73b7bca2014-12-04 07:28:31 +0800851 size = MIN(size, (1 << 20) - 1); /* avoid unbounded allocation */
852 buf = g_malloc(size + 1);
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100853
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 Zheng73b7bca2014-12-04 07:28:31 +0800860 buf[ret] = 0;
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100861
862 return buf;
863}
864
Fam Zheng86c6b422011-08-12 23:19:28 +0800865static int vmdk_open_vmdk4(BlockDriverState *bs,
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200866 BdrvChild *file,
Kevin Wolfa6468362015-04-07 15:35:59 +0200867 int flags, QDict *options, Error **errp)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800868{
869 int ret;
870 uint32_t magic;
871 uint32_t l1_size, l1_entry_sectors;
872 VMDK4Header header;
Christian Borntraegercd466702020-09-30 17:58:56 +0200873 VmdkExtent *extent = NULL;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800874 BDRVVmdkState *s = bs->opaque;
Fam Zhengbb45ded2011-08-12 23:19:34 +0800875 int64_t l1_backup_offset = 0;
Fam Zheng3db1d982016-01-25 10:26:23 +0800876 bool compressed;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800877
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200878 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800879 if (ret < 0) {
Max Reitzf30c66b2019-02-01 20:29:05 +0100880 bdrv_refresh_filename(file->bs);
Fam Zheng48239702013-10-11 15:43:22 +0800881 error_setg_errno(errp, -ret,
882 "Could not read header from file '%s'",
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200883 file->bs->filename);
Paolo Bonzini89ac8482014-02-17 14:44:05 +0100884 return -EINVAL;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800885 }
Stefan Hajnoczi5a394b92013-06-10 11:07:33 +0200886 if (header.capacity == 0) {
Fam Zhenge98768d2013-08-06 15:44:48 +0800887 uint64_t desc_offset = le64_to_cpu(header.desc_offset);
Stefan Hajnoczi5a394b92013-06-10 11:07:33 +0200888 if (desc_offset) {
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200889 char *buf = vmdk_read_desc(file, desc_offset << 9, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100890 if (!buf) {
891 return -EINVAL;
892 }
Kevin Wolfa6468362015-04-07 15:35:59 +0200893 ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100894 g_free(buf);
895 return ret;
Stefan Hajnoczi5a394b92013-06-10 11:07:33 +0200896 }
Fam Zhengf16f5092011-08-12 23:19:33 +0800897 }
Kevin Wolf65bd1552012-08-16 10:39:33 +0200898
Fam Zhengf4c129a2013-10-31 10:06:23 +0800899 if (!s->create_type) {
900 s->create_type = g_strdup("monolithicSparse");
901 }
902
Kevin Wolf65bd1552012-08-16 10:39:33 +0200903 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 Wolfcf2ab8f2016-06-20 18:24:02 +0200929 ret = bdrv_pread(file,
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200930 bs->file->bs->total_sectors * 512 - 1536,
Kevin Wolf65bd1552012-08-16 10:39:33 +0200931 &footer, sizeof(footer));
932 if (ret < 0) {
Fam Zhengd899d2e2014-12-04 07:28:34 +0800933 error_setg_errno(errp, -ret, "Failed to read footer");
Kevin Wolf65bd1552012-08-16 10:39:33 +0200934 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 Zhengd899d2e2014-12-04 07:28:34 +0800945 error_setg(errp, "Invalid footer");
Kevin Wolf65bd1552012-08-16 10:39:33 +0200946 return -EINVAL;
947 }
948
949 header = footer.header;
950 }
951
Fam Zheng3db1d982016-01-25 10:26:23 +0800952 compressed =
953 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
Fam Zheng509d39a2013-11-28 09:48:03 +0800954 if (le32_to_cpu(header.version) > 3) {
Max Reitza55448b2016-03-16 19:54:33 +0100955 error_setg(errp, "Unsupported VMDK version %" PRIu32,
956 le32_to_cpu(header.version));
Fam Zheng96c51eb2013-06-13 11:21:29 +0800957 return -ENOTSUP;
Fam Zheng3db1d982016-01-25 10:26:23 +0800958 } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR) &&
959 !compressed) {
Fam Zheng509d39a2013-11-28 09:48:03 +0800960 /* 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 Zheng96c51eb2013-06-13 11:21:29 +0800966 }
967
Fam Zhengca8804c2013-08-06 15:44:55 +0800968 if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
Paolo Bonzini89ac8482014-02-17 14:44:05 +0100969 error_setg(errp, "L2 table size too big");
Fam Zhengf8ce0402013-08-06 15:44:52 +0800970 return -EINVAL;
971 }
972
Fam Zhengca8804c2013-08-06 15:44:55 +0800973 l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800974 * le64_to_cpu(header.granularity);
Stefan Weil75d12342012-02-25 14:01:42 +0100975 if (l1_entry_sectors == 0) {
Fam Zhengd899d2e2014-12-04 07:28:34 +0800976 error_setg(errp, "L1 entry size is invalid");
Fam Zheng86c6b422011-08-12 23:19:28 +0800977 return -EINVAL;
978 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800979 l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
980 / l1_entry_sectors;
Fam Zhengbb45ded2011-08-12 23:19:34 +0800981 if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
982 l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
983 }
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200984 if (bdrv_nb_sectors(file->bs) < le64_to_cpu(header.grain_offset)) {
Fam Zheng4ab9dab2014-04-17 11:34:37 +0800985 error_setg(errp, "File truncated, expecting at least %" PRId64 " bytes",
986 (int64_t)(le64_to_cpu(header.grain_offset)
987 * BDRV_SECTOR_SIZE));
Fam Zheng34ceed82014-01-21 15:07:43 +0800988 return -EINVAL;
989 }
990
Fam Zheng8aa13312013-08-06 15:44:51 +0800991 ret = vmdk_add_extent(bs, file, false,
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800992 le64_to_cpu(header.capacity),
993 le64_to_cpu(header.gd_offset) << 9,
Fam Zhengbb45ded2011-08-12 23:19:34 +0800994 l1_backup_offset,
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800995 l1_size,
Fam Zhengca8804c2013-08-06 15:44:55 +0800996 le32_to_cpu(header.num_gtes_per_gt),
Fam Zheng8aa13312013-08-06 15:44:51 +0800997 le64_to_cpu(header.granularity),
Fam Zheng48239702013-10-11 15:43:22 +0800998 &extent,
999 errp);
Fam Zheng8aa13312013-08-06 15:44:51 +08001000 if (ret < 0) {
1001 return ret;
1002 }
Fam Zheng432bb172011-08-12 23:19:30 +08001003 extent->compressed =
1004 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
Fam Zhengd8a7b062014-01-23 15:10:52 +08001005 if (extent->compressed) {
1006 g_free(s->create_type);
1007 s->create_type = g_strdup("streamOptimized");
1008 }
Fam Zheng432bb172011-08-12 23:19:30 +08001009 extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
Fam Zheng14ead642013-05-02 10:25:23 +08001010 extent->version = le32_to_cpu(header.version);
1011 extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
Fam Zheng48239702013-10-11 15:43:22 +08001012 ret = vmdk_init_tables(bs, extent, errp);
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001013 if (ret) {
Fam Zheng86c6b422011-08-12 23:19:28 +08001014 /* free extent allocated by vmdk_add_extent */
1015 vmdk_free_last_extent(bs);
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001016 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001017 return ret;
1018}
1019
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001020/* find an option value out of descriptor file */
1021static 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 Zheng65f74722013-05-02 10:25:22 +08001029 return VMDK_ERROR;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001030 }
1031 /* Skip "=\"" following opt_name */
1032 opt_pos += strlen(opt_name) + 2;
1033 if (opt_pos >= end) {
Fam Zheng65f74722013-05-02 10:25:22 +08001034 return VMDK_ERROR;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001035 }
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 Zheng65f74722013-05-02 10:25:22 +08001041 return VMDK_ERROR;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001042 }
1043 pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
Fam Zheng65f74722013-05-02 10:25:22 +08001044 return VMDK_OK;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001045}
1046
Fam Zheng86c6b422011-08-12 23:19:28 +08001047/* Open an extent file and append to bs array */
Kevin Wolf24bc15d2015-06-15 13:50:20 +02001048static int vmdk_open_sparse(BlockDriverState *bs, BdrvChild *file, int flags,
Kevin Wolfa6468362015-04-07 15:35:59 +02001049 char *buf, QDict *options, Error **errp)
Fam Zheng86c6b422011-08-12 23:19:28 +08001050{
1051 uint32_t magic;
1052
Paolo Bonzinid1833ef2014-02-17 14:44:03 +01001053 magic = ldl_be_p(buf);
Fam Zheng86c6b422011-08-12 23:19:28 +08001054 switch (magic) {
1055 case VMDK3_MAGIC:
Fam Zheng48239702013-10-11 15:43:22 +08001056 return vmdk_open_vmfs_sparse(bs, file, flags, errp);
Fam Zheng86c6b422011-08-12 23:19:28 +08001057 case VMDK4_MAGIC:
Kevin Wolfa6468362015-04-07 15:35:59 +02001058 return vmdk_open_vmdk4(bs, file, flags, options, errp);
Fam Zheng86c6b422011-08-12 23:19:28 +08001059 default:
Paolo Bonzini76abe402014-02-17 14:44:06 +01001060 error_setg(errp, "Image not in VMDK format");
1061 return -EINVAL;
Fam Zheng86c6b422011-08-12 23:19:28 +08001062 }
1063}
1064
Markus Armbrustere4937692015-12-18 16:35:20 +01001065static 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 Zheng7fa60fa2011-07-19 08:38:22 +08001076static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
Max Reitzcdc0dd22019-08-15 17:36:33 +02001077 QDict *options, Error **errp)
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001078{
1079 int ret;
Jeff Cody395a22f2015-01-22 08:03:25 -05001080 int matches;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001081 char access[11];
1082 char type[11];
1083 char fname[512];
Markus Armbrusterd28d7372015-12-18 16:35:21 +01001084 const char *p, *np;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001085 int64_t sectors = 0;
1086 int64_t flat_offset;
Max Reitzcdc0dd22019-08-15 17:36:33 +02001087 char *desc_file_dir = NULL;
Jeff Codyfe206562015-01-22 08:03:26 -05001088 char *extent_path;
Kevin Wolf24bc15d2015-06-15 13:50:20 +02001089 BdrvChild *extent_file;
Max Reitz8b1869d2020-05-13 13:05:35 +02001090 BdrvChildRole extent_role;
Fam Zhengf4c129a2013-10-31 10:06:23 +08001091 BDRVVmdkState *s = bs->opaque;
Christian Borntraegercd466702020-09-30 17:58:56 +02001092 VmdkExtent *extent = NULL;
Kevin Wolfa6468362015-04-07 15:35:59 +02001093 char extent_opt_prefix[32];
Kevin Wolf24bc15d2015-06-15 13:50:20 +02001094 Error *local_err = NULL;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001095
Markus Armbrustere4937692015-12-18 16:35:20 +01001096 for (p = desc; *p; p = next_line(p)) {
Fam Zheng8a3e0bc2014-12-04 07:28:30 +08001097 /* parse extent line in one of below formats:
1098 *
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001099 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001100 * RW [size in sectors] SPARSE "file-name.vmdk"
Fam Zheng8a3e0bc2014-12-04 07:28:30 +08001101 * RW [size in sectors] VMFS "file-name.vmdk"
1102 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
Sam Eiderman98eb9732019-06-20 12:10:57 +03001103 * RW [size in sectors] SESPARSE "file-name.vmdk"
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001104 */
1105 flat_offset = -1;
Jeff Cody395a22f2015-01-22 08:03:25 -05001106 matches = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
1107 access, &sectors, type, fname, &flat_offset);
1108 if (matches < 4 || strcmp(access, "RW")) {
Markus Armbrustere4937692015-12-18 16:35:20 +01001109 continue;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001110 } else if (!strcmp(type, "FLAT")) {
Jeff Cody395a22f2015-01-22 08:03:25 -05001111 if (matches != 5 || flat_offset < 0) {
Markus Armbrusterd28d7372015-12-18 16:35:21 +01001112 goto invalid;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001113 }
Fam Zhengdbbcaa82013-10-18 15:07:33 +08001114 } else if (!strcmp(type, "VMFS")) {
Jeff Cody395a22f2015-01-22 08:03:25 -05001115 if (matches == 4) {
Fam Zhengb47053b2013-12-09 13:24:36 +08001116 flat_offset = 0;
1117 } else {
Markus Armbrusterd28d7372015-12-18 16:35:21 +01001118 goto invalid;
Fam Zhengb47053b2013-12-09 13:24:36 +08001119 }
Jeff Cody395a22f2015-01-22 08:03:25 -05001120 } else if (matches != 4) {
Markus Armbrusterd28d7372015-12-18 16:35:21 +01001121 goto invalid;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001122 }
1123
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001124 if (sectors <= 0 ||
Fam Zhengdaac8fd2013-08-19 18:54:27 +08001125 (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
Sam Eiderman98eb9732019-06-20 12:10:57 +03001126 strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE") &&
1127 strcmp(type, "SESPARSE")) ||
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001128 (strcmp(access, "RW"))) {
Markus Armbrustere4937692015-12-18 16:35:20 +01001129 continue;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001130 }
1131
Max Reitzcdc0dd22019-08-15 17:36:33 +02001132 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 Reitz5c984152014-12-03 14:57:22 +01001146
Max Reitzcdc0dd22019-08-15 17:36:33 +02001147 extent_path = g_strconcat(desc_file_dir, fname, NULL);
1148 }
Kevin Wolfa6468362015-04-07 15:35:59 +02001149
1150 ret = snprintf(extent_opt_prefix, 32, "extents.%d", s->num_extents);
1151 assert(ret < 32);
1152
Max Reitz8b1869d2020-05-13 13:05:35 +02001153 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 Wolf24bc15d2015-06-15 13:50:20 +02001159 extent_file = bdrv_open_child(extent_path, options, extent_opt_prefix,
Max Reitz8b1869d2020-05-13 13:05:35 +02001160 bs, &child_of_bds, extent_role, false,
1161 &local_err);
Jeff Codyfe206562015-01-22 08:03:26 -05001162 g_free(extent_path);
Kevin Wolf24bc15d2015-06-15 13:50:20 +02001163 if (local_err) {
1164 error_propagate(errp, local_err);
Max Reitzcdc0dd22019-08-15 17:36:33 +02001165 ret = -EINVAL;
1166 goto out;
Fam Zheng86c6b422011-08-12 23:19:28 +08001167 }
1168
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001169 /* save to extents array */
Paolo Bonzini04d542c2013-08-19 18:54:28 +08001170 if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001171 /* FLAT extent */
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001172
Fam Zheng8aa13312013-08-06 15:44:51 +08001173 ret = vmdk_add_extent(bs, extent_file, true, sectors,
Fam Zheng48239702013-10-11 15:43:22 +08001174 0, 0, 0, 0, 0, &extent, errp);
Fam Zheng8aa13312013-08-06 15:44:51 +08001175 if (ret < 0) {
Kevin Wolf24bc15d2015-06-15 13:50:20 +02001176 bdrv_unref_child(bs, extent_file);
Max Reitzcdc0dd22019-08-15 17:36:33 +02001177 goto out;
Fam Zheng8aa13312013-08-06 15:44:51 +08001178 }
Fam Zhengf16f5092011-08-12 23:19:33 +08001179 extent->flat_start_offset = flat_offset << 9;
Fam Zhengdaac8fd2013-08-19 18:54:27 +08001180 } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
1181 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001182 char *buf = vmdk_read_desc(extent_file, 0, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +01001183 if (!buf) {
1184 ret = -EINVAL;
1185 } else {
Kevin Wolfa6468362015-04-07 15:35:59 +02001186 ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, buf,
1187 options, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +01001188 }
Stefan Hajnoczib6b1d312014-09-04 21:04:43 +01001189 g_free(buf);
Fam Zheng86c6b422011-08-12 23:19:28 +08001190 if (ret) {
Kevin Wolf24bc15d2015-06-15 13:50:20 +02001191 bdrv_unref_child(bs, extent_file);
Max Reitzcdc0dd22019-08-15 17:36:33 +02001192 goto out;
Fam Zheng86c6b422011-08-12 23:19:28 +08001193 }
Fam Zhengf4c129a2013-10-31 10:06:23 +08001194 extent = &s->extents[s->num_extents - 1];
Sam Eiderman98eb9732019-06-20 12:10:57 +03001195 } 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 Reitzcdc0dd22019-08-15 17:36:33 +02001199 goto out;
Sam Eiderman98eb9732019-06-20 12:10:57 +03001200 }
1201 extent = &s->extents[s->num_extents - 1];
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001202 } else {
Fam Zheng48239702013-10-11 15:43:22 +08001203 error_setg(errp, "Unsupported extent type '%s'", type);
Kevin Wolf24bc15d2015-06-15 13:50:20 +02001204 bdrv_unref_child(bs, extent_file);
Max Reitzcdc0dd22019-08-15 17:36:33 +02001205 ret = -ENOTSUP;
1206 goto out;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001207 }
Fam Zhengf4c129a2013-10-31 10:06:23 +08001208 extent->type = g_strdup(type);
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001209 }
Max Reitzcdc0dd22019-08-15 17:36:33 +02001210
1211 ret = 0;
1212 goto out;
Markus Armbrusterd28d7372015-12-18 16:35:21 +01001213
1214invalid:
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 Reitzcdc0dd22019-08-15 17:36:33 +02001221 ret = -EINVAL;
1222
1223out:
1224 g_free(desc_file_dir);
1225 return ret;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001226}
1227
Paolo Bonzinid1833ef2014-02-17 14:44:03 +01001228static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
Kevin Wolfa6468362015-04-07 15:35:59 +02001229 QDict *options, Error **errp)
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001230{
1231 int ret;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001232 char ct[128];
1233 BDRVVmdkState *s = bs->opaque;
1234
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001235 if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
Paolo Bonzini76abe402014-02-17 14:44:06 +01001236 error_setg(errp, "invalid VMDK image descriptor");
1237 ret = -EINVAL;
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +03001238 goto exit;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001239 }
Fam Zheng6398de52011-08-12 23:19:27 +08001240 if (strcmp(ct, "monolithicFlat") &&
Paolo Bonzini04d542c2013-08-19 18:54:28 +08001241 strcmp(ct, "vmfs") &&
Fam Zhengdaac8fd2013-08-19 18:54:27 +08001242 strcmp(ct, "vmfsSparse") &&
Sam Eiderman98eb9732019-06-20 12:10:57 +03001243 strcmp(ct, "seSparse") &&
Fam Zheng86c6b422011-08-12 23:19:28 +08001244 strcmp(ct, "twoGbMaxExtentSparse") &&
Fam Zheng6398de52011-08-12 23:19:27 +08001245 strcmp(ct, "twoGbMaxExtentFlat")) {
Fam Zheng48239702013-10-11 15:43:22 +08001246 error_setg(errp, "Unsupported image type '%s'", ct);
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +03001247 ret = -ENOTSUP;
1248 goto exit;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001249 }
Fam Zhengf4c129a2013-10-31 10:06:23 +08001250 s->create_type = g_strdup(ct);
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001251 s->desc_offset = 0;
Max Reitzcdc0dd22019-08-15 17:36:33 +02001252 ret = vmdk_parse_extents(buf, bs, options, errp);
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +03001253exit:
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +03001254 return ret;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001255}
1256
Max Reitz015a1032013-09-05 14:22:29 +02001257static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
1258 Error **errp)
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001259{
Fam Zheng9aeecbb2014-12-04 07:28:33 +08001260 char *buf;
Fam Zheng86c6b422011-08-12 23:19:28 +08001261 int ret;
1262 BDRVVmdkState *s = bs->opaque;
Paolo Bonzini37f09e52014-02-17 14:44:04 +01001263 uint32_t magic;
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001264
Max Reitz8b1869d2020-05-13 13:05:35 +02001265 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
1266 BDRV_CHILD_IMAGE, false, errp);
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01001267 if (!bs->file) {
1268 return -EINVAL;
1269 }
1270
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001271 buf = vmdk_read_desc(bs->file, 0, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +01001272 if (!buf) {
1273 return -EINVAL;
1274 }
1275
Paolo Bonzini37f09e52014-02-17 14:44:04 +01001276 magic = ldl_be_p(buf);
1277 switch (magic) {
1278 case VMDK3_MAGIC:
1279 case VMDK4_MAGIC:
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001280 ret = vmdk_open_sparse(bs, bs->file, flags, buf, options,
Kevin Wolf24bc15d2015-06-15 13:50:20 +02001281 errp);
Paolo Bonzini37f09e52014-02-17 14:44:04 +01001282 s->desc_offset = 0x200;
1283 break;
1284 default:
Max Reitz8b1869d2020-05-13 13:05:35 +02001285 /* 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 Wolfa6468362015-04-07 15:35:59 +02001291 ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
Paolo Bonzini37f09e52014-02-17 14:44:04 +01001292 break;
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001293 }
Paolo Bonzini37f09e52014-02-17 14:44:04 +01001294 if (ret) {
1295 goto fail;
1296 }
1297
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +02001298 /* try to open parent images, if exist */
1299 ret = vmdk_parent_open(bs);
1300 if (ret) {
1301 goto fail;
1302 }
Peter Maydell98778602017-07-09 18:06:14 +01001303 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 Bonzini848c66e2011-10-20 13:16:21 +02001311 qemu_co_mutex_init(&s->lock);
Kevin Wolf2bc31662011-11-22 16:50:27 +01001312
1313 /* Disable migration when VMDK images are used */
Alberto Garcia81e5f782015-04-08 12:29:19 +03001314 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 Armbruster386f6c02020-07-07 18:06:10 +02001317 ret = migrate_add_blocker(s->migration_blocker, errp);
1318 if (ret < 0) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301319 error_free(s->migration_blocker);
1320 goto fail;
1321 }
1322
Paolo Bonzinid1833ef2014-02-17 14:44:03 +01001323 g_free(buf);
Kevin Wolf2bc31662011-11-22 16:50:27 +01001324 return 0;
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +02001325
1326fail:
Paolo Bonzinid1833ef2014-02-17 14:44:03 +01001327 g_free(buf);
Fam Zhengf4c129a2013-10-31 10:06:23 +08001328 g_free(s->create_type);
1329 s->create_type = NULL;
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +02001330 vmdk_free_extents(bs);
1331 return ret;
bellardea2384d2004-08-01 21:59:26 +00001332}
1333
Kevin Wolfd34682c2013-12-11 19:26:16 +01001334
Kevin Wolf3baca892014-07-16 17:48:16 +02001335static void vmdk_refresh_limits(BlockDriverState *bs, Error **errp)
Kevin Wolfd34682c2013-12-11 19:26:16 +01001336{
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 Blakecf081fc2016-06-01 15:10:02 -06001342 bs->bl.pwrite_zeroes_alignment =
1343 MAX(bs->bl.pwrite_zeroes_alignment,
1344 s->extents[i].cluster_sectors << BDRV_SECTOR_BITS);
Kevin Wolfd34682c2013-12-11 19:26:16 +01001345 }
1346 }
Kevin Wolfd34682c2013-12-11 19:26:16 +01001347}
1348
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001349/**
1350 * get_whole_cluster
1351 *
1352 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
Kevin Wolf4823cde2020-04-30 15:30:04 +02001353 * 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 Zhengc6ac36e2014-07-30 14:39:10 +08001356 *
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 Zhengb3976d32011-07-12 19:56:28 +08001361static int get_whole_cluster(BlockDriverState *bs,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001362 VmdkExtent *extent,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001363 uint64_t cluster_offset,
1364 uint64_t offset,
1365 uint64_t skip_start_bytes,
Kevin Wolf4823cde2020-04-30 15:30:04 +02001366 uint64_t skip_end_bytes,
1367 bool zeroed)
ths5f4da8c2007-01-24 21:05:24 +00001368{
Fam Zhengbf815072013-08-06 15:44:54 +08001369 int ret = VMDK_OK;
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001370 int64_t cluster_bytes;
1371 uint8_t *whole_grain;
Kevin Wolf4823cde2020-04-30 15:30:04 +02001372 bool copy_from_backing;
ths5f4da8c2007-01-24 21:05:24 +00001373
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001374 /* For COW, align request sector_num to cluster start */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001375 cluster_bytes = extent->cluster_sectors << BDRV_SECTOR_BITS;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001376 offset = QEMU_ALIGN_DOWN(offset, cluster_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001377 whole_grain = qemu_blockalign(bs, cluster_bytes);
Kevin Wolf4823cde2020-04-30 15:30:04 +02001378 copy_from_backing = bs->backing && !zeroed;
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001379
Kevin Wolf4823cde2020-04-30 15:30:04 +02001380 if (!copy_from_backing) {
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001381 memset(whole_grain, 0, skip_start_bytes);
1382 memset(whole_grain + skip_end_bytes, 0, cluster_bytes - skip_end_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001383 }
1384
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001385 assert(skip_end_bytes <= cluster_bytes);
Fam Zheng0e69c542011-07-12 19:56:29 +08001386 /* we will be here if it's first write on non-exist grain(cluster).
1387 * try to read from parent image, if exist */
Kevin Wolf760e0062015-06-17 14:55:21 +02001388 if (bs->backing && !vmdk_is_cid_valid(bs)) {
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001389 ret = VMDK_ERROR;
1390 goto exit;
1391 }
ths5f4da8c2007-01-24 21:05:24 +00001392
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001393 /* Read backing data before skip range */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001394 if (skip_start_bytes > 0) {
Kevin Wolf4823cde2020-04-30 15:30:04 +02001395 if (copy_from_backing) {
Max Reitz23c4b2a2017-11-23 03:08:19 +01001396 /* qcow2 emits this on bs->file instead of bs->backing */
1397 BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001398 ret = bdrv_pread(bs->backing, offset, whole_grain,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001399 skip_start_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001400 if (ret < 0) {
1401 ret = VMDK_ERROR;
1402 goto exit;
1403 }
Kevin Wolfc3365002010-04-16 19:28:14 +02001404 }
Max Reitz23c4b2a2017-11-23 03:08:19 +01001405 BLKDBG_EVENT(extent->file, BLKDBG_COW_WRITE);
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02001406 ret = bdrv_pwrite(extent->file, cluster_offset, whole_grain,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001407 skip_start_bytes);
Kevin Wolfc3365002010-04-16 19:28:14 +02001408 if (ret < 0) {
Fam Zhengbf815072013-08-06 15:44:54 +08001409 ret = VMDK_ERROR;
1410 goto exit;
ths630530a2007-06-18 15:01:30 +00001411 }
ths5f4da8c2007-01-24 21:05:24 +00001412 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001413 /* Read backing data after skip range */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001414 if (skip_end_bytes < cluster_bytes) {
Kevin Wolf4823cde2020-04-30 15:30:04 +02001415 if (copy_from_backing) {
Max Reitz23c4b2a2017-11-23 03:08:19 +01001416 /* qcow2 emits this on bs->file instead of bs->backing */
1417 BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001418 ret = bdrv_pread(bs->backing, offset + skip_end_bytes,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001419 whole_grain + skip_end_bytes,
1420 cluster_bytes - skip_end_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001421 if (ret < 0) {
1422 ret = VMDK_ERROR;
1423 goto exit;
1424 }
1425 }
Max Reitz23c4b2a2017-11-23 03:08:19 +01001426 BLKDBG_EVENT(extent->file, BLKDBG_COW_WRITE);
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02001427 ret = bdrv_pwrite(extent->file, cluster_offset + skip_end_bytes,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001428 whole_grain + skip_end_bytes,
1429 cluster_bytes - skip_end_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001430 if (ret < 0) {
1431 ret = VMDK_ERROR;
1432 goto exit;
1433 }
1434 }
1435
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001436 ret = VMDK_OK;
Fam Zhengbf815072013-08-06 15:44:54 +08001437exit:
1438 qemu_vfree(whole_grain);
1439 return ret;
ths5f4da8c2007-01-24 21:05:24 +00001440}
1441
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001442static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data,
1443 uint32_t offset)
ths630530a2007-06-18 15:01:30 +00001444{
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001445 offset = cpu_to_le32(offset);
ths630530a2007-06-18 15:01:30 +00001446 /* update L2 table */
Max Reitz23c4b2a2017-11-23 03:08:19 +01001447 BLKDBG_EVENT(extent->file, BLKDBG_L2_UPDATE);
Kevin Wolf2758be02020-04-30 15:30:06 +02001448 if (bdrv_pwrite(extent->file,
Fam Zhengb3976d32011-07-12 19:56:28 +08001449 ((int64_t)m_data->l2_offset * 512)
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001450 + (m_data->l2_index * sizeof(offset)),
Fam Zhenge304e8e2013-05-02 10:25:26 +08001451 &offset, sizeof(offset)) < 0) {
Fam Zheng65f74722013-05-02 10:25:22 +08001452 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001453 }
ths630530a2007-06-18 15:01:30 +00001454 /* update backup L2 table */
Fam Zhengb3976d32011-07-12 19:56:28 +08001455 if (extent->l1_backup_table_offset != 0) {
1456 m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
Kevin Wolf2758be02020-04-30 15:30:06 +02001457 if (bdrv_pwrite(extent->file,
Fam Zhengb3976d32011-07-12 19:56:28 +08001458 ((int64_t)m_data->l2_offset * 512)
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001459 + (m_data->l2_index * sizeof(offset)),
Fam Zhenge304e8e2013-05-02 10:25:26 +08001460 &offset, sizeof(offset)) < 0) {
Fam Zheng65f74722013-05-02 10:25:22 +08001461 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001462 }
ths630530a2007-06-18 15:01:30 +00001463 }
Kevin Wolf2758be02020-04-30 15:30:06 +02001464 if (bdrv_flush(extent->file->bs) < 0) {
1465 return VMDK_ERROR;
1466 }
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001467 if (m_data->l2_cache_entry) {
1468 *m_data->l2_cache_entry = offset;
1469 }
ths630530a2007-06-18 15:01:30 +00001470
Fam Zheng65f74722013-05-02 10:25:22 +08001471 return VMDK_OK;
ths630530a2007-06-18 15:01:30 +00001472}
1473
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001474/**
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 Zheng91b85bd2011-07-12 19:56:35 +08001494static int get_cluster_offset(BlockDriverState *bs,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001495 VmdkExtent *extent,
1496 VmdkMetaData *m_data,
1497 uint64_t offset,
1498 bool allocate,
1499 uint64_t *cluster_offset,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001500 uint64_t skip_start_bytes,
1501 uint64_t skip_end_bytes)
bellardea2384d2004-08-01 21:59:26 +00001502{
bellardea2384d2004-08-01 21:59:26 +00001503 unsigned int l1_index, l2_offset, l2_index;
1504 int min_index, i, j;
Sam Eiderman98eb9732019-06-20 12:10:57 +03001505 uint32_t min_count;
1506 void *l2_table;
Fam Zheng14ead642013-05-02 10:25:23 +08001507 bool zeroed = false;
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001508 int64_t ret;
Fam Zhengd1319b02014-09-23 09:56:21 +08001509 int64_t cluster_sector;
Sam Eiderman98eb9732019-06-20 12:10:57 +03001510 unsigned int l2_size_bytes = extent->l2_size * extent->entry_size;
ths630530a2007-06-18 15:01:30 +00001511
Fam Zhengae261c82011-07-12 19:56:38 +08001512 if (m_data) {
Kevin Wolf4dc20e62020-04-30 15:30:02 +02001513 m_data->new_allocation = false;
Fam Zhengae261c82011-07-12 19:56:38 +08001514 }
Fam Zheng91b85bd2011-07-12 19:56:35 +08001515 if (extent->flat) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001516 *cluster_offset = extent->flat_start_offset;
Fam Zheng65f74722013-05-02 10:25:22 +08001517 return VMDK_OK;
Fam Zheng91b85bd2011-07-12 19:56:35 +08001518 }
ths630530a2007-06-18 15:01:30 +00001519
Fam Zheng6398de52011-08-12 23:19:27 +08001520 offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
Fam Zhengb3976d32011-07-12 19:56:28 +08001521 l1_index = (offset >> 9) / extent->l1_entry_sectors;
1522 if (l1_index >= extent->l1_size) {
Fam Zheng65f74722013-05-02 10:25:22 +08001523 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001524 }
Sam Eiderman98eb9732019-06-20 12:10:57 +03001525 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 Zhengb3976d32011-07-12 19:56:28 +08001555 if (!l2_offset) {
Fam Zheng65f74722013-05-02 10:25:22 +08001556 return VMDK_UNALLOC;
Fam Zhengb3976d32011-07-12 19:56:28 +08001557 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001558 for (i = 0; i < L2_CACHE_SIZE; i++) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001559 if (l2_offset == extent->l2_cache_offsets[i]) {
bellardea2384d2004-08-01 21:59:26 +00001560 /* increment the hit count */
Fam Zhengb3976d32011-07-12 19:56:28 +08001561 if (++extent->l2_cache_counts[i] == 0xffffffff) {
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001562 for (j = 0; j < L2_CACHE_SIZE; j++) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001563 extent->l2_cache_counts[j] >>= 1;
bellardea2384d2004-08-01 21:59:26 +00001564 }
1565 }
Sam Eiderman98eb9732019-06-20 12:10:57 +03001566 l2_table = (char *)extent->l2_cache + (i * l2_size_bytes);
bellardea2384d2004-08-01 21:59:26 +00001567 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 Zhengb4b3ab12011-07-12 19:56:31 +08001573 for (i = 0; i < L2_CACHE_SIZE; i++) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001574 if (extent->l2_cache_counts[i] < min_count) {
1575 min_count = extent->l2_cache_counts[i];
bellardea2384d2004-08-01 21:59:26 +00001576 min_index = i;
1577 }
1578 }
Sam Eiderman98eb9732019-06-20 12:10:57 +03001579 l2_table = (char *)extent->l2_cache + (min_index * l2_size_bytes);
Max Reitz23c4b2a2017-11-23 03:08:19 +01001580 BLKDBG_EVENT(extent->file, BLKDBG_L2_LOAD);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001581 if (bdrv_pread(extent->file,
Fam Zhengb3976d32011-07-12 19:56:28 +08001582 (int64_t)l2_offset * 512,
1583 l2_table,
Sam Eiderman98eb9732019-06-20 12:10:57 +03001584 l2_size_bytes
1585 ) != l2_size_bytes) {
Fam Zheng65f74722013-05-02 10:25:22 +08001586 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001587 }
ths5f4da8c2007-01-24 21:05:24 +00001588
Fam Zhengb3976d32011-07-12 19:56:28 +08001589 extent->l2_cache_offsets[min_index] = l2_offset;
1590 extent->l2_cache_counts[min_index] = 1;
bellardea2384d2004-08-01 21:59:26 +00001591 found:
Fam Zhengb3976d32011-07-12 19:56:28 +08001592 l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
Kevin Wolf2821c1c2020-04-30 15:30:03 +02001593 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 }
ths630530a2007-06-18 15:01:30 +00001599
Sam Eiderman98eb9732019-06-20 12:10:57 +03001600 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 Zheng14ead642013-05-02 10:25:23 +08001631 }
1632
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001633 if (!cluster_sector || zeroed) {
Fam Zheng91b85bd2011-07-12 19:56:35 +08001634 if (!allocate) {
Fam Zheng14ead642013-05-02 10:25:23 +08001635 return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
Fam Zheng91b85bd2011-07-12 19:56:35 +08001636 }
Sam Eiderman98eb9732019-06-20 12:10:57 +03001637 assert(!extent->sesparse);
ths5f4da8c2007-01-24 21:05:24 +00001638
yuchenlina77672e2018-03-22 21:33:37 +08001639 if (extent->next_cluster_sector >= VMDK_EXTENT_MAX_SECTORS) {
1640 return VMDK_ERROR;
1641 }
1642
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001643 cluster_sector = extent->next_cluster_sector;
1644 extent->next_cluster_sector += extent->cluster_sectors;
Kevin Wolf9949f972010-04-16 21:07:19 +02001645
ths630530a2007-06-18 15:01:30 +00001646 /* 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 Wolf37b1d7d2016-04-26 13:39:11 +02001651 ret = get_whole_cluster(bs, extent, cluster_sector * BDRV_SECTOR_SIZE,
Kevin Wolf4823cde2020-04-30 15:30:04 +02001652 offset, skip_start_bytes, skip_end_bytes,
1653 zeroed);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001654 if (ret) {
1655 return ret;
ths630530a2007-06-18 15:01:30 +00001656 }
Reda Sallahi524089b2016-07-07 10:42:49 +02001657 if (m_data) {
Kevin Wolf4dc20e62020-04-30 15:30:02 +02001658 m_data->new_allocation = true;
Reda Sallahi524089b2016-07-07 10:42:49 +02001659 }
bellardff1afc72005-04-26 21:08:00 +00001660 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001661 *cluster_offset = cluster_sector << BDRV_SECTOR_BITS;
Fam Zheng65f74722013-05-02 10:25:22 +08001662 return VMDK_OK;
bellardea2384d2004-08-01 21:59:26 +00001663}
1664
Fam Zhengb3976d32011-07-12 19:56:28 +08001665static 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 Wolfa844a2b2016-04-25 17:14:48 +02001682static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent *extent,
1683 int64_t offset)
1684{
Eduardo Habkost9be38592016-06-13 18:57:58 -03001685 uint64_t extent_begin_offset, extent_relative_offset;
Kevin Wolfa844a2b2016-04-25 17:14:48 +02001686 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 Habkost9be38592016-06-13 18:57:58 -03001691 return extent_relative_offset % cluster_size;
Kevin Wolfa844a2b2016-04-25 17:14:48 +02001692}
1693
Eric Blakec72080b2018-02-13 14:26:58 -06001694static 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)
bellardea2384d2004-08-01 21:59:26 +00001699{
1700 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3976d32011-07-12 19:56:28 +08001701 int64_t index_in_cluster, n, ret;
Eric Blakec72080b2018-02-13 14:26:58 -06001702 uint64_t cluster_offset;
Fam Zhengb3976d32011-07-12 19:56:28 +08001703 VmdkExtent *extent;
1704
Eric Blakec72080b2018-02-13 14:26:58 -06001705 extent = find_extent(s, offset >> BDRV_SECTOR_BITS, NULL);
Fam Zhengb3976d32011-07-12 19:56:28 +08001706 if (!extent) {
Eric Blakec72080b2018-02-13 14:26:58 -06001707 return -EIO;
Fam Zhengb3976d32011-07-12 19:56:28 +08001708 }
Stefan Hajnoczif8a2e5e2011-11-14 12:44:21 +00001709 qemu_co_mutex_lock(&s->lock);
Eric Blakec72080b2018-02-13 14:26:58 -06001710 ret = get_cluster_offset(bs, extent, NULL, offset, false, &cluster_offset,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001711 0, 0);
Stefan Hajnoczif8a2e5e2011-11-14 12:44:21 +00001712 qemu_co_mutex_unlock(&s->lock);
Fam Zheng14ead642013-05-02 10:25:23 +08001713
Eric Blakec72080b2018-02-13 14:26:58 -06001714 index_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02001715 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 Zhenge0f100f2016-01-26 11:58:59 +08001727 if (!extent->compressed) {
Fam Zhengd0a18f12016-01-26 11:58:58 +08001728 ret |= BDRV_BLOCK_OFFSET_VALID;
Eric Blakec72080b2018-02-13 14:26:58 -06001729 *map = cluster_offset + index_in_cluster;
Max Reitz4dd84ac2019-07-25 17:55:11 +02001730 if (extent->flat) {
1731 ret |= BDRV_BLOCK_RECURSE;
1732 }
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02001733 }
Fam Zhenge0f100f2016-01-26 11:58:59 +08001734 *file = extent->file->bs;
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02001735 break;
1736 }
Fam Zheng91b85bd2011-07-12 19:56:35 +08001737
Eric Blakec72080b2018-02-13 14:26:58 -06001738 n = extent->cluster_sectors * BDRV_SECTOR_SIZE - index_in_cluster;
1739 *pnum = MIN(n, bytes);
Fam Zhengb3976d32011-07-12 19:56:28 +08001740 return ret;
bellardea2384d2004-08-01 21:59:26 +00001741}
1742
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001743static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001744 int64_t offset_in_cluster, QEMUIOVector *qiov,
1745 uint64_t qiov_offset, uint64_t n_bytes,
1746 uint64_t offset)
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001747{
1748 int ret;
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001749 VmdkGrainMarker *data = NULL;
1750 uLongf buf_len;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001751 QEMUIOVector local_qiov;
Fam Zheng5e82a312015-05-06 20:23:46 +08001752 int64_t write_offset;
1753 int64_t write_end_sector;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001754
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001755 if (extent->compressed) {
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001756 void *compressed_data;
1757
Max Reitzbedb8bb2019-08-15 17:36:35 +02001758 /* 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 Zheng2b2c8c52011-08-12 23:19:31 +08001768 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 Wolf37b1d7d2016-04-26 13:39:11 +02001774
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 Zheng2b2c8c52011-08-12 23:19:31 +08001781 ret = -EINVAL;
1782 goto out;
1783 }
Fam Zheng5e82a312015-05-06 20:23:46 +08001784
QingFeng Hao4545d4f2016-12-16 06:20:40 +01001785 data->lba = cpu_to_le64(offset >> BDRV_SECTOR_BITS);
1786 data->size = cpu_to_le32(buf_len);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001787
1788 n_bytes = buf_len + sizeof(VmdkGrainMarker);
Vladimir Sementsov-Ogievskiy199d95b2019-02-18 17:09:20 +03001789 qemu_iovec_init_buf(&local_qiov, data, n_bytes);
Max Reitz23c4b2a2017-11-23 03:08:19 +01001790
1791 BLKDBG_EVENT(extent->file, BLKDBG_WRITE_COMPRESSED);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001792 } else {
1793 qemu_iovec_init(&local_qiov, qiov->niov);
1794 qemu_iovec_concat(&local_qiov, qiov, qiov_offset, n_bytes);
Max Reitz23c4b2a2017-11-23 03:08:19 +01001795
1796 BLKDBG_EVENT(extent->file, BLKDBG_WRITE_AIO);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001797 }
1798
Max Reitz3c363572017-11-23 03:08:16 +01001799 write_offset = cluster_offset + offset_in_cluster;
Kevin Wolfa03ef882016-06-20 21:31:46 +02001800 ret = bdrv_co_pwritev(extent->file, write_offset, n_bytes,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001801 &local_qiov, 0);
1802
1803 write_end_sector = DIV_ROUND_UP(write_offset + n_bytes, BDRV_SECTOR_SIZE);
Fam Zheng5e82a312015-05-06 20:23:46 +08001804
Radoslav Gerganov3efffc32015-09-10 10:53:14 +03001805 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 Zheng5e82a312015-05-06 20:23:46 +08001811
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001812 if (ret < 0) {
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001813 goto out;
1814 }
1815 ret = 0;
1816 out:
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001817 g_free(data);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001818 if (!extent->compressed) {
1819 qemu_iovec_destroy(&local_qiov);
1820 }
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001821 return ret;
1822}
1823
1824static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
Kevin Wolff10cc242016-04-25 17:34:41 +02001825 int64_t offset_in_cluster, QEMUIOVector *qiov,
1826 int bytes)
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001827{
1828 int ret;
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001829 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 Zhengdd3f6ee2011-08-12 23:19:29 +08001835
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001836
1837 if (!extent->compressed) {
Max Reitz23c4b2a2017-11-23 03:08:19 +01001838 BLKDBG_EVENT(extent->file, BLKDBG_READ_AIO);
Kevin Wolfa03ef882016-06-20 21:31:46 +02001839 ret = bdrv_co_preadv(extent->file,
Kevin Wolff10cc242016-04-25 17:34:41 +02001840 cluster_offset + offset_in_cluster, bytes,
1841 qiov, 0);
1842 if (ret < 0) {
1843 return ret;
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001844 }
Kevin Wolff10cc242016-04-25 17:34:41 +02001845 return 0;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001846 }
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001847 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 Reitz23c4b2a2017-11-23 03:08:19 +01001852 BLKDBG_EVENT(extent->file, BLKDBG_READ_COMPRESSED);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001853 ret = bdrv_pread(extent->file,
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001854 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 Wolff10cc242016-04-25 17:34:41 +02001878 offset_in_cluster + bytes > buf_len) {
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001879 ret = -EINVAL;
1880 goto out;
1881 }
Kevin Wolff10cc242016-04-25 17:34:41 +02001882 qemu_iovec_from_buf(qiov, 0, uncomp_buf + offset_in_cluster, bytes);
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001883 ret = 0;
1884
1885 out:
1886 g_free(uncomp_buf);
1887 g_free(cluster_buf);
1888 return ret;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001889}
1890
Kevin Wolff10cc242016-04-25 17:34:41 +02001891static int coroutine_fn
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +03001892vmdk_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
1893 QEMUIOVector *qiov, BdrvRequestFlags flags)
bellardea2384d2004-08-01 21:59:26 +00001894{
1895 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3976d32011-07-12 19:56:28 +08001896 int ret;
Kevin Wolff10cc242016-04-25 17:34:41 +02001897 uint64_t n_bytes, offset_in_cluster;
Fam Zhengb3976d32011-07-12 19:56:28 +08001898 VmdkExtent *extent = NULL;
Kevin Wolff10cc242016-04-25 17:34:41 +02001899 QEMUIOVector local_qiov;
bellardea2384d2004-08-01 21:59:26 +00001900 uint64_t cluster_offset;
Kevin Wolff10cc242016-04-25 17:34:41 +02001901 uint64_t bytes_done = 0;
ths5f4da8c2007-01-24 21:05:24 +00001902
Kevin Wolff10cc242016-04-25 17:34:41 +02001903 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 Zhengb3976d32011-07-12 19:56:28 +08001908 if (!extent) {
Kevin Wolff10cc242016-04-25 17:34:41 +02001909 ret = -EIO;
1910 goto fail;
Fam Zhengb3976d32011-07-12 19:56:28 +08001911 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001912 ret = get_cluster_offset(bs, extent, NULL,
Kevin Wolff10cc242016-04-25 17:34:41 +02001913 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 Zheng14ead642013-05-02 10:25:23 +08001919 if (ret != VMDK_OK) {
Fam Zheng91b85bd2011-07-12 19:56:35 +08001920 /* if not allocated, try to read from parent image, if exist */
Kevin Wolf760e0062015-06-17 14:55:21 +02001921 if (bs->backing && ret != VMDK_ZEROED) {
Fam Zhengae261c82011-07-12 19:56:38 +08001922 if (!vmdk_is_cid_valid(bs)) {
Kevin Wolff10cc242016-04-25 17:34:41 +02001923 ret = -EINVAL;
1924 goto fail;
Fam Zhengae261c82011-07-12 19:56:38 +08001925 }
Kevin Wolff10cc242016-04-25 17:34:41 +02001926
1927 qemu_iovec_reset(&local_qiov);
1928 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
1929
Max Reitz23c4b2a2017-11-23 03:08:19 +01001930 /* qcow2 emits this on bs->file instead of bs->backing */
1931 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
Kevin Wolfa03ef882016-06-20 21:31:46 +02001932 ret = bdrv_co_preadv(bs->backing, offset, n_bytes,
Kevin Wolff10cc242016-04-25 17:34:41 +02001933 &local_qiov, 0);
Fam Zhengae261c82011-07-12 19:56:38 +08001934 if (ret < 0) {
Kevin Wolff10cc242016-04-25 17:34:41 +02001935 goto fail;
Fam Zhengae261c82011-07-12 19:56:38 +08001936 }
ths5f4da8c2007-01-24 21:05:24 +00001937 } else {
Kevin Wolff10cc242016-04-25 17:34:41 +02001938 qemu_iovec_memset(qiov, bytes_done, 0, n_bytes);
ths5f4da8c2007-01-24 21:05:24 +00001939 }
bellardea2384d2004-08-01 21:59:26 +00001940 } else {
Kevin Wolff10cc242016-04-25 17:34:41 +02001941 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 Zhengdd3f6ee2011-08-12 23:19:29 +08001946 if (ret) {
Kevin Wolff10cc242016-04-25 17:34:41 +02001947 goto fail;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001948 }
bellardea2384d2004-08-01 21:59:26 +00001949 }
Kevin Wolff10cc242016-04-25 17:34:41 +02001950 bytes -= n_bytes;
1951 offset += n_bytes;
1952 bytes_done += n_bytes;
bellardea2384d2004-08-01 21:59:26 +00001953 }
bellardea2384d2004-08-01 21:59:26 +00001954
Kevin Wolff10cc242016-04-25 17:34:41 +02001955 ret = 0;
1956fail:
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001957 qemu_co_mutex_unlock(&s->lock);
Kevin Wolff10cc242016-04-25 17:34:41 +02001958 qemu_iovec_destroy(&local_qiov);
1959
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001960 return ret;
1961}
1962
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001963/**
1964 * vmdk_write:
1965 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
Fam Zheng8e507242013-08-01 18:12:17 +08001966 * 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 Zhengcdeaf1f2013-05-02 10:25:27 +08001970 *
1971 * Returns: error code with 0 for success.
1972 */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001973static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset,
1974 uint64_t bytes, QEMUIOVector *qiov,
1975 bool zeroed, bool zero_dry_run)
bellardea2384d2004-08-01 21:59:26 +00001976{
bellardff1afc72005-04-26 21:08:00 +00001977 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3976d32011-07-12 19:56:28 +08001978 VmdkExtent *extent = NULL;
Fam Zheng585ea0c2014-01-08 09:42:07 +08001979 int ret;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001980 int64_t offset_in_cluster, n_bytes;
bellardff1afc72005-04-26 21:08:00 +00001981 uint64_t cluster_offset;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001982 uint64_t bytes_done = 0;
Fam Zhengb3976d32011-07-12 19:56:28 +08001983 VmdkMetaData m_data;
bellardff1afc72005-04-26 21:08:00 +00001984
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001985 if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) {
1986 error_report("Wrong offset: offset=0x%" PRIx64
Markus Armbruster9af9e0f2015-12-18 16:35:19 +01001987 " total_sectors=0x%" PRIx64,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001988 offset, bs->total_sectors);
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001989 return -EIO;
ths630530a2007-06-18 15:01:30 +00001990 }
1991
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001992 while (bytes > 0) {
1993 extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent);
Fam Zhengb3976d32011-07-12 19:56:28 +08001994 if (!extent) {
1995 return -EIO;
1996 }
Sam Eiderman98eb9732019-06-20 12:10:57 +03001997 if (extent->sesparse) {
1998 return -ENOTSUP;
1999 }
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002000 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 Zhengc6ac36e2014-07-30 14:39:10 +08002005 !(extent->compressed || zeroed),
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002006 &cluster_offset, offset_in_cluster,
2007 offset_in_cluster + n_bytes);
Fam Zheng2b2c8c52011-08-12 23:19:31 +08002008 if (extent->compressed) {
Fam Zheng65f74722013-05-02 10:25:22 +08002009 if (ret == VMDK_OK) {
Fam Zheng2b2c8c52011-08-12 23:19:31 +08002010 /* Refuse write to allocated cluster for streamOptimized */
Fam Zheng48239702013-10-11 15:43:22 +08002011 error_report("Could not write to allocated cluster"
2012 " for streamOptimized");
Fam Zheng2b2c8c52011-08-12 23:19:31 +08002013 return -EIO;
Kevin Wolf2821c1c2020-04-30 15:30:03 +02002014 } else if (!zeroed) {
Fam Zheng2b2c8c52011-08-12 23:19:31 +08002015 /* allocate */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002016 ret = get_cluster_offset(bs, extent, &m_data, offset,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08002017 true, &cluster_offset, 0, 0);
Fam Zheng2b2c8c52011-08-12 23:19:31 +08002018 }
2019 }
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002020 if (ret == VMDK_ERROR) {
Fam Zheng91b85bd2011-07-12 19:56:35 +08002021 return -EINVAL;
Fam Zhengb3976d32011-07-12 19:56:28 +08002022 }
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002023 if (zeroed) {
2024 /* Do zeroed write, buf is ignored */
2025 if (extent->has_zero_grain &&
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002026 offset_in_cluster == 0 &&
2027 n_bytes >= extent->cluster_sectors * BDRV_SECTOR_SIZE) {
2028 n_bytes = extent->cluster_sectors * BDRV_SECTOR_SIZE;
Kevin Wolf78cae782020-04-30 15:30:05 +02002029 if (!zero_dry_run && ret != VMDK_ZEROED) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002030 /* update L2 tables */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08002031 if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED)
2032 != VMDK_OK) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002033 return -EIO;
2034 }
2035 }
2036 } else {
2037 return -ENOTSUP;
2038 }
2039 } else {
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002040 ret = vmdk_write_extent(extent, cluster_offset, offset_in_cluster,
2041 qiov, bytes_done, n_bytes, offset);
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002042 if (ret) {
2043 return ret;
2044 }
Kevin Wolf4dc20e62020-04-30 15:30:02 +02002045 if (m_data.new_allocation) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002046 /* update L2 tables */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08002047 if (vmdk_L2update(extent, &m_data,
2048 cluster_offset >> BDRV_SECTOR_BITS)
2049 != VMDK_OK) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002050 return -EIO;
2051 }
Fam Zhengb3976d32011-07-12 19:56:28 +08002052 }
ths630530a2007-06-18 15:01:30 +00002053 }
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002054 bytes -= n_bytes;
2055 offset += n_bytes;
2056 bytes_done += n_bytes;
ths5f4da8c2007-01-24 21:05:24 +00002057
Fam Zhengae261c82011-07-12 19:56:38 +08002058 /* update CID on the first write every time the virtual disk is
2059 * opened */
Fam Zheng69b4d862011-07-12 19:56:34 +08002060 if (!s->cid_updated) {
Fam Zhenge5dc64b2014-12-04 07:28:29 +08002061 ret = vmdk_write_cid(bs, g_random_int());
Kevin Wolf99f18352011-10-26 12:25:25 +02002062 if (ret < 0) {
2063 return ret;
2064 }
Fam Zheng69b4d862011-07-12 19:56:34 +08002065 s->cid_updated = true;
ths5f4da8c2007-01-24 21:05:24 +00002066 }
bellardff1afc72005-04-26 21:08:00 +00002067 }
2068 return 0;
bellardea2384d2004-08-01 21:59:26 +00002069}
2070
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002071static int coroutine_fn
Vladimir Sementsov-Ogievskiye75abed2021-09-03 13:28:00 +03002072vmdk_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
2073 QEMUIOVector *qiov, BdrvRequestFlags flags)
Paolo Bonzinie183ef72011-10-20 13:16:23 +02002074{
2075 int ret;
2076 BDRVVmdkState *s = bs->opaque;
2077 qemu_co_mutex_lock(&s->lock);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002078 ret = vmdk_pwritev(bs, offset, bytes, qiov, false, false);
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002079 qemu_co_mutex_unlock(&s->lock);
2080 return ret;
2081}
2082
Pavel Butsykinb2c622d2016-07-22 11:17:45 +03002083static int coroutine_fn
Vladimir Sementsov-Ogievskiye75abed2021-09-03 13:28:00 +03002084vmdk_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, int64_t bytes,
2085 QEMUIOVector *qiov)
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002086{
yuchenlin51b3c6b2018-09-13 16:29:52 +08002087 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 Reitzc80d8b02019-09-18 11:51:40 +02002100 ret = bdrv_truncate(s->extents[i].file, length, false,
Kevin Wolf7b8e4852020-04-24 14:54:40 +02002101 PREALLOC_MODE_OFF, 0, NULL);
yuchenlin51b3c6b2018-09-13 16:29:52 +08002102 if (ret < 0) {
2103 return ret;
2104 }
2105 }
2106 return 0;
2107 }
Pavel Butsykinb2c622d2016-07-22 11:17:45 +03002108 return vmdk_co_pwritev(bs, offset, bytes, qiov, 0);
Fam Zhengba0ad892014-05-06 21:08:44 +08002109}
2110
Eric Blakea620f2a2016-06-01 15:10:12 -06002111static int coroutine_fn vmdk_co_pwrite_zeroes(BlockDriverState *bs,
2112 int64_t offset,
Vladimir Sementsov-Ogievskiyf34b2bc2021-09-03 13:28:03 +03002113 int64_t bytes,
Eric Blakea620f2a2016-06-01 15:10:12 -06002114 BdrvRequestFlags flags)
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002115{
2116 int ret;
2117 BDRVVmdkState *s = bs->opaque;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002118
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002119 qemu_co_mutex_lock(&s->lock);
Fam Zheng8e507242013-08-01 18:12:17 +08002120 /* write zeroes could fail if sectors not aligned to cluster, test it with
2121 * dry_run == true before really updating image */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002122 ret = vmdk_pwritev(bs, offset, bytes, NULL, true, true);
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002123 if (!ret) {
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002124 ret = vmdk_pwritev(bs, offset, bytes, NULL, true, false);
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08002125 }
Paolo Bonzinie183ef72011-10-20 13:16:23 +02002126 qemu_co_mutex_unlock(&s->lock);
2127 return ret;
2128}
2129
Fam Zheng5be28492018-05-15 23:36:31 +08002130static int vmdk_init_extent(BlockBackend *blk,
2131 int64_t filesize, bool flat,
2132 bool compress, bool zeroed_grain,
2133 Error **errp)
bellard8979b222005-07-02 14:02:54 +00002134{
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002135 int ret, i;
bellard8979b222005-07-02 14:02:54 +00002136 VMDK4Header header;
Fam Zheng917703c2013-12-20 09:48:48 +08002137 uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
2138 uint32_t *gd_buf = NULL;
2139 int gd_buf_size;
Kevin Wolf0e7e1982009-05-18 16:42:10 +02002140
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002141 if (flat) {
Kevin Wolf8c6242b2020-04-24 14:54:41 +02002142 ret = blk_truncate(blk, filesize, false, PREALLOC_MODE_OFF, 0, errp);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002143 goto exit;
2144 }
bellard8979b222005-07-02 14:02:54 +00002145 magic = cpu_to_be32(VMDK4_MAGIC);
2146 memset(&header, 0, sizeof(header));
Fam Zhengd62d9dc2015-09-17 13:04:10 +08002147 if (compress) {
2148 header.version = 3;
2149 } else if (zeroed_grain) {
2150 header.version = 2;
2151 } else {
2152 header.version = 1;
2153 }
Fam Zheng95b0aa42013-05-02 10:25:25 +08002154 header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
Fam Zheng69e0b6d2013-05-02 10:25:24 +08002155 | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
2156 | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
Fam Zheng6c031aa2011-08-12 23:19:32 +08002157 header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
Fam Zheng917703c2013-12-20 09:48:48 +08002158 header.capacity = filesize / BDRV_SECTOR_SIZE;
Alexander Graf16372ff2011-05-25 00:46:55 +02002159 header.granularity = 128;
Fam Zheng917703c2013-12-20 09:48:48 +08002160 header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
bellard8979b222005-07-02 14:02:54 +00002161
Fam Zheng917703c2013-12-20 09:48:48 +08002162 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);
bellard8979b222005-07-02 14:02:54 +00002167
2168 header.desc_offset = 1;
2169 header.desc_size = 20;
2170 header.rgd_offset = header.desc_offset + header.desc_size;
Fam Zheng917703c2013-12-20 09:48:48 +08002171 header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
bellard8979b222005-07-02 14:02:54 +00002172 header.grain_offset =
Fam Zheng917703c2013-12-20 09:48:48 +08002173 ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
2174 header.granularity);
Alexander Graf16372ff2011-05-25 00:46:55 +02002175 /* 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 Zhengca8804c2013-08-06 15:44:55 +08002180 header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
bellard8979b222005-07-02 14:02:54 +00002181 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 Zheng6c031aa2011-08-12 23:19:32 +08002186 header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
bellard8979b222005-07-02 14:02:54 +00002187
2188 header.check_bytes[0] = 0xa;
2189 header.check_bytes[1] = 0x20;
2190 header.check_bytes[2] = 0xd;
2191 header.check_bytes[3] = 0xa;
ths3b46e622007-09-17 08:09:54 +00002192
2193 /* write all the data */
Eric Blake8341f002016-05-06 10:26:27 -06002194 ret = blk_pwrite(blk, 0, &magic, sizeof(magic), 0);
Fam Zheng917703c2013-12-20 09:48:48 +08002195 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002196 error_setg(errp, QERR_IO_ERROR);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002197 goto exit;
2198 }
Eric Blake8341f002016-05-06 10:26:27 -06002199 ret = blk_pwrite(blk, sizeof(magic), &header, sizeof(header), 0);
Fam Zheng917703c2013-12-20 09:48:48 +08002200 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002201 error_setg(errp, QERR_IO_ERROR);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002202 goto exit;
2203 }
bellard8979b222005-07-02 14:02:54 +00002204
Max Reitzc80d8b02019-09-18 11:51:40 +02002205 ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9, false,
Kevin Wolf8c6242b2020-04-24 14:54:41 +02002206 PREALLOC_MODE_OFF, 0, errp);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002207 if (ret < 0) {
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002208 goto exit;
2209 }
bellard8979b222005-07-02 14:02:54 +00002210
2211 /* write grain directory */
Fam Zheng917703c2013-12-20 09:48:48 +08002212 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. Shutemov16403662010-01-20 00:56:13 +01002215 i < gt_count; i++, tmp += gt_size) {
Fam Zheng917703c2013-12-20 09:48:48 +08002216 gd_buf[i] = cpu_to_le32(tmp);
2217 }
Kevin Wolfc4bea162016-03-08 15:57:05 +01002218 ret = blk_pwrite(blk, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
Eric Blake8341f002016-05-06 10:26:27 -06002219 gd_buf, gd_buf_size, 0);
Fam Zheng917703c2013-12-20 09:48:48 +08002220 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002221 error_setg(errp, QERR_IO_ERROR);
Fam Zheng917703c2013-12-20 09:48:48 +08002222 goto exit;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002223 }
ths3b46e622007-09-17 08:09:54 +00002224
bellard8979b222005-07-02 14:02:54 +00002225 /* write backup grain directory */
Fam Zheng917703c2013-12-20 09:48:48 +08002226 for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002227 i < gt_count; i++, tmp += gt_size) {
Fam Zheng917703c2013-12-20 09:48:48 +08002228 gd_buf[i] = cpu_to_le32(tmp);
2229 }
Kevin Wolfc4bea162016-03-08 15:57:05 +01002230 ret = blk_pwrite(blk, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
Eric Blake8341f002016-05-06 10:26:27 -06002231 gd_buf, gd_buf_size, 0);
Fam Zheng917703c2013-12-20 09:48:48 +08002232 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002233 error_setg(errp, QERR_IO_ERROR);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002234 }
bellard8979b222005-07-02 14:02:54 +00002235
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002236 ret = 0;
Fam Zheng917703c2013-12-20 09:48:48 +08002237exit:
Fam Zheng917703c2013-12-20 09:48:48 +08002238 g_free(gd_buf);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002239 return ret;
2240}
bellard8979b222005-07-02 14:02:54 +00002241
Fam Zheng5be28492018-05-15 23:36:31 +08002242static 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 Zheng5be28492018-05-15 23:36:31 +08002249
Markus Armbruster668f62e2020-07-07 18:06:02 +02002250 ret = bdrv_create_file(filename, opts, errp);
Fam Zheng5be28492018-05-15 23:36:31 +08002251 if (ret < 0) {
Fam Zheng5be28492018-05-15 23:36:31 +08002252 goto exit;
2253 }
2254
2255 blk = blk_new_open(filename, NULL, NULL,
2256 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
Markus Armbrusteraf175e82020-07-07 18:06:03 +02002257 errp);
Fam Zheng5be28492018-05-15 23:36:31 +08002258 if (blk == NULL) {
Fam Zheng5be28492018-05-15 23:36:31 +08002259 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);
2266exit:
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 Zhengf66fd6c2011-07-19 08:45:23 +08002278static int filename_decompose(const char *filename, char *path, char *prefix,
Fam Zheng48239702013-10-11 15:43:22 +08002279 char *postfix, size_t buf_len, Error **errp)
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002280{
2281 const char *p, *q;
2282
2283 if (filename == NULL || !strlen(filename)) {
Fam Zheng48239702013-10-11 15:43:22 +08002284 error_setg(errp, "No filename provided");
Fam Zheng65f74722013-05-02 10:25:22 +08002285 return VMDK_ERROR;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002286 }
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 Zheng65f74722013-05-02 10:25:22 +08002297 return VMDK_ERROR;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002298 }
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 Zheng65f74722013-05-02 10:25:22 +08002310 return VMDK_ERROR;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002311 }
2312 pstrcpy(prefix, q - p + 1, p);
2313 pstrcpy(postfix, buf_len, q);
2314 }
Fam Zheng65f74722013-05-02 10:25:22 +08002315 return VMDK_OK;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002316}
2317
Fam Zheng30153722018-05-15 23:36:32 +08002318/*
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 */
2323typedef 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
2332static void vmdk_desc_add_extent(GString *desc,
2333 const char *extent_line_fmt,
2334 int64_t size, const char *filename)
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002335{
Fam Zheng30153722018-05-15 23:36:32 +08002336 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
2343static 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ßschuhf3d43df2021-09-13 15:04:19 +02002348 const char *toolsversion,
Fam Zheng30153722018-05-15 23:36:32 +08002349 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 Wolf4a960ec2018-12-07 12:42:19 +01002357 BlockBackend *extent_blk;
Fam Zhengc13959c2014-05-28 11:38:58 +08002358 Error *local_err = NULL;
Fam Zhengaf057fe2013-12-03 10:41:05 +08002359 char *desc = NULL;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002360 int ret = 0;
Fam Zheng6c031aa2011-08-12 23:19:32 +08002361 bool flat, split, compress;
Fam Zhengaf057fe2013-12-03 10:41:05 +08002362 GString *ext_desc_lines;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002363 const int64_t split_size = 0x80000000; /* VMDK has constant split size */
Fam Zheng30153722018-05-15 23:36:32 +08002364 int64_t extent_size;
2365 int64_t created_size = 0;
2366 const char *extent_line_fmt;
Jeff Codyfe206562015-01-22 08:03:26 -05002367 char *parent_desc_line = g_malloc0(BUF_SIZE);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002368 uint32_t parent_cid = 0xffffffff;
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01002369 uint32_t number_heads = 16;
Fam Zheng917703c2013-12-20 09:48:48 +08002370 uint32_t desc_offset = 0, desc_len;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002371 const char desc_template[] =
2372 "# Disk DescriptorFile\n"
2373 "version=1\n"
Fam Zheng9b170312014-04-17 18:43:53 +08002374 "CID=%" PRIx32 "\n"
2375 "parentCID=%" PRIx32 "\n"
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002376 "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 Karhunenf2499242016-05-03 02:43:30 -07002385 "ddb.virtualHWVersion = \"%s\"\n"
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002386 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
Fam Zheng4ab9dab2014-04-17 11:34:37 +08002387 "ddb.geometry.heads = \"%" PRIu32 "\"\n"
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002388 "ddb.geometry.sectors = \"63\"\n"
Thomas Weißschuhf3d43df2021-09-13 15:04:19 +02002389 "ddb.adapterType = \"%s\"\n"
2390 "ddb.toolsVersion = \"%s\"\n";
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002391
Fam Zhengaf057fe2013-12-03 10:41:05 +08002392 ext_desc_lines = g_string_new(NULL);
2393
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002394 /* Read out options */
Fam Zheng30153722018-05-15 23:36:32 +08002395 if (compat6) {
2396 if (hw_version) {
Janne Karhunenf2499242016-05-03 02:43:30 -07002397 error_setg(errp,
2398 "compat6 cannot be enabled with hwversion set");
2399 ret = -EINVAL;
2400 goto exit;
2401 }
Fam Zheng30153722018-05-15 23:36:32 +08002402 hw_version = "6";
Janne Karhunenf2499242016-05-03 02:43:30 -07002403 }
Fam Zheng30153722018-05-15 23:36:32 +08002404 if (!hw_version) {
2405 hw_version = "4";
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002406 }
Thomas Weißschuhf3d43df2021-09-13 15:04:19 +02002407 if (!toolsversion) {
2408 toolsversion = "2147483647";
2409 }
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002410
Fam Zheng30153722018-05-15 23:36:32 +08002411 if (adapter_type != BLOCKDEV_VMDK_ADAPTER_TYPE_IDE) {
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01002412 /* 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 Zheng30153722018-05-15 23:36:32 +08002416 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 Zhengf66fd6c2011-07-19 08:45:23 +08002422 if (flat) {
Fam Zheng30153722018-05-15 23:36:32 +08002423 extent_line_fmt = "RW %" PRId64 " FLAT \"%s\" 0\n";
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002424 } else {
Fam Zheng30153722018-05-15 23:36:32 +08002425 extent_line_fmt = "RW %" PRId64 " SPARSE \"%s\"\n";
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002426 }
2427 if (flat && backing_file) {
Fam Zheng48239702013-10-11 15:43:22 +08002428 error_setg(errp, "Flat image can't have backing file");
Fam Zhengaf057fe2013-12-03 10:41:05 +08002429 ret = -ENOTSUP;
2430 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002431 }
Fam Zheng52c8d622013-10-11 15:43:23 +08002432 if (flat && zeroed_grain) {
2433 error_setg(errp, "Flat image can't enable zeroed grain");
Fam Zhengaf057fe2013-12-03 10:41:05 +08002434 ret = -ENOTSUP;
2435 goto exit;
Fam Zheng52c8d622013-10-11 15:43:23 +08002436 }
Fam Zheng30153722018-05-15 23:36:32 +08002437
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 Zhengf66fd6c2011-07-19 08:45:23 +08002461 if (backing_file) {
Fam Zheng30153722018-05-15 23:36:32 +08002462 BlockBackend *backing;
Max Reitz645ae7d2019-02-01 20:29:14 +01002463 char *full_backing =
2464 bdrv_get_full_backing_filename_from_filename(blk_bs(blk)->filename,
2465 backing_file,
2466 &local_err);
Max Reitz1085daf2014-11-26 17:20:28 +01002467 if (local_err) {
Max Reitz1085daf2014-11-26 17:20:28 +01002468 error_propagate(errp, local_err);
2469 ret = -ENOENT;
2470 goto exit;
2471 }
Max Reitz645ae7d2019-02-01 20:29:14 +01002472 assert(full_backing);
Kevin Wolfc4bea162016-03-08 15:57:05 +01002473
Fam Zheng30153722018-05-15 23:36:32 +08002474 backing = blk_new_open(full_backing, NULL, NULL,
2475 BDRV_O_NO_BACKING, errp);
Max Reitz1085daf2014-11-26 17:20:28 +01002476 g_free(full_backing);
Fam Zheng30153722018-05-15 23:36:32 +08002477 if (backing == NULL) {
Kevin Wolfc4bea162016-03-08 15:57:05 +01002478 ret = -EIO;
Fam Zhengaf057fe2013-12-03 10:41:05 +08002479 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002480 }
Fam Zheng30153722018-05-15 23:36:32 +08002481 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 Zhengaf057fe2013-12-03 10:41:05 +08002485 ret = -EINVAL;
2486 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002487 }
Fam Zheng30153722018-05-15 23:36:32 +08002488 ret = vmdk_read_cid(blk_bs(backing), 0, &parent_cid);
2489 blk_unref(backing);
Peter Maydell98778602017-07-09 18:06:14 +01002490 if (ret) {
Fam Zheng30153722018-05-15 23:36:32 +08002491 error_setg(errp, "Failed to read parent CID");
Peter Maydell98778602017-07-09 18:06:14 +01002492 goto exit;
2493 }
Jeff Codyfe206562015-01-22 08:03:26 -05002494 snprintf(parent_desc_line, BUF_SIZE,
Fam Zheng8ed610a2013-06-26 17:24:32 +08002495 "parentFileNameHint=\"%s\"", backing_file);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002496 }
Fam Zheng30153722018-05-15 23:36:32 +08002497 extent_idx = 1;
2498 while (created_size < size) {
Fam Zheng30153722018-05-15 23:36:32 +08002499 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 Zhengaf057fe2013-12-03 10:41:05 +08002503 ret = -EINVAL;
2504 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002505 }
Fam Zheng30153722018-05-15 23:36:32 +08002506 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 Zhengf66fd6c2011-07-19 08:45:23 +08002511 }
Kevin Wolf4a960ec2018-12-07 12:42:19 +01002512
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 Zhengf66fd6c2011-07-19 08:45:23 +08002523 /* generate descriptor file */
Fam Zhengaf057fe2013-12-03 10:41:05 +08002524 desc = g_strdup_printf(desc_template,
Fam Zhenge5dc64b2014-12-04 07:28:29 +08002525 g_random_int(),
Fam Zhengaf057fe2013-12-03 10:41:05 +08002526 parent_cid,
Fam Zheng30153722018-05-15 23:36:32 +08002527 BlockdevVmdkSubformat_str(subformat),
Fam Zhengaf057fe2013-12-03 10:41:05 +08002528 parent_desc_line,
2529 ext_desc_lines->str,
Janne Karhunenf2499242016-05-03 02:43:30 -07002530 hw_version,
Fam Zheng30153722018-05-15 23:36:32 +08002531 size /
Fam Zheng917703c2013-12-20 09:48:48 +08002532 (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
Fam Zhengaf057fe2013-12-03 10:41:05 +08002533 number_heads,
Thomas Weißschuhf3d43df2021-09-13 15:04:19 +02002534 BlockdevVmdkAdapterType_str(adapter_type),
2535 toolsversion);
Fam Zheng917703c2013-12-20 09:48:48 +08002536 desc_len = strlen(desc);
2537 /* the descriptor offset = 0x200 */
2538 if (!split && !flat) {
2539 desc_offset = 0x200;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002540 }
Kevin Wolfc4bea162016-03-08 15:57:05 +01002541
Fam Zheng30153722018-05-15 23:36:32 +08002542 ret = blk_pwrite(blk, desc_offset, desc, desc_len, 0);
Fam Zheng917703c2013-12-20 09:48:48 +08002543 if (ret < 0) {
2544 error_setg_errno(errp, -ret, "Could not write description");
2545 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002546 }
Fam Zheng917703c2013-12-20 09:48:48 +08002547 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2548 * for description file */
2549 if (desc_offset == 0) {
Kevin Wolf8c6242b2020-04-24 14:54:41 +02002550 ret = blk_truncate(blk, desc_len, false, PREALLOC_MODE_OFF, 0, errp);
Fam Zheng30153722018-05-15 23:36:32 +08002551 if (ret < 0) {
2552 goto exit;
2553 }
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002554 }
Fam Zheng30153722018-05-15 23:36:32 +08002555 ret = 0;
Fam Zhengaf057fe2013-12-03 10:41:05 +08002556exit:
Fam Zheng30153722018-05-15 23:36:32 +08002557 if (blk) {
2558 blk_unref(blk);
Fam Zheng917703c2013-12-20 09:48:48 +08002559 }
Fam Zheng30153722018-05-15 23:36:32 +08002560 g_free(desc);
2561 g_free(parent_desc_line);
2562 g_string_free(ext_desc_lines, true);
2563 return ret;
2564}
2565
2566typedef struct {
2567 char *path;
2568 char *prefix;
2569 char *postfix;
2570 QemuOpts *opts;
2571} VMDKCreateOptsData;
2572
2573static 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 Wolf4a960ec2018-12-07 12:42:19 +01002584 /* We're done, don't create excess extents. */
2585 if (size == -1) {
2586 assert(errp == NULL);
2587 return NULL;
2588 }
2589
Fam Zheng30153722018-05-15 23:36:32 +08002590 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);
2610exit:
2611 g_free(ext_filename);
2612 return blk;
2613}
2614
Maxim Levitskyb92902d2020-03-26 03:12:17 +02002615static int coroutine_fn vmdk_co_create_opts(BlockDriver *drv,
2616 const char *filename,
2617 QemuOpts *opts,
Fam Zheng30153722018-05-15 23:36:32 +08002618 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ßschuhf3d43df2021-09-13 15:04:19 +02002627 char *toolsversion = NULL;
Fam Zheng30153722018-05-15 23:36:32 +08002628 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 Blaked51a8142020-07-06 15:39:48 -05002641 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 Zheng30153722018-05-15 23:36:32 +08002649
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ßschuhf3d43df2021-09-13 15:04:19 +02002660 toolsversion = qemu_opt_get_del(opts, BLOCK_OPT_TOOLSVERSION);
Fam Zheng30153722018-05-15 23:36:32 +08002661 compat6 = qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false);
2662 if (strcmp(hw_version, "undefined") == 0) {
2663 g_free(hw_version);
yuchenlin26c92962019-02-21 11:08:05 +00002664 hw_version = NULL;
Fam Zheng30153722018-05-15 23:36:32 +08002665 }
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ßschuhf3d43df2021-09-13 15:04:19 +02002704 backing_file, hw_version, toolsversion, compat6,
2705 zeroed_grain, vmdk_co_create_opts_cb, &data, errp);
Fam Zheng30153722018-05-15 23:36:32 +08002706
2707exit:
Eric Blaked51a8142020-07-06 15:39:48 -05002708 g_free(backing_fmt);
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002709 g_free(adapter_type);
2710 g_free(backing_file);
Janne Karhunenf2499242016-05-03 02:43:30 -07002711 g_free(hw_version);
Thomas Weißschuhf3d43df2021-09-13 15:04:19 +02002712 g_free(toolsversion);
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002713 g_free(fmt);
Fam Zhengaf057fe2013-12-03 10:41:05 +08002714 g_free(desc);
Jeff Codyfe206562015-01-22 08:03:26 -05002715 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 Zheng30153722018-05-15 23:36:32 +08002722 return ret;
2723}
2724
2725static 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 Blakea3aeeab2020-04-28 14:26:46 -05002756 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 Zheng30153722018-05-15 23:36:32 +08002760 return NULL;
2761 }
2762 blk_set_allow_write_beyond_eof(blk, true);
2763 bdrv_unref(bs);
2764
Kevin Wolf4a960ec2018-12-07 12:42:19 +01002765 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 Zheng30153722018-05-15 23:36:32 +08002771 }
2772 return blk;
2773}
2774
2775static 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ßschuhf3d43df2021-09-13 15:04:19 +02002795 opts->toolsversion,
Fam Zheng30153722018-05-15 23:36:32 +08002796 false,
2797 opts->zeroed_grain,
2798 vmdk_co_create_cb,
2799 opts, errp);
2800 return ret;
2801
2802out:
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002803 return ret;
bellard8979b222005-07-02 14:02:54 +00002804}
2805
bellarde2731ad2004-09-18 19:32:11 +00002806static void vmdk_close(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002807{
Kevin Wolf2bc31662011-11-22 16:50:27 +01002808 BDRVVmdkState *s = bs->opaque;
2809
Fam Zhengb3976d32011-07-12 19:56:28 +08002810 vmdk_free_extents(bs);
Fam Zhengf4c129a2013-10-31 10:06:23 +08002811 g_free(s->create_type);
Kevin Wolf2bc31662011-11-22 16:50:27 +01002812
2813 migrate_del_blocker(s->migration_blocker);
2814 error_free(s->migration_blocker);
bellardea2384d2004-08-01 21:59:26 +00002815}
2816
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002817static 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 Wolf9a4f4c32015-06-16 14:19:22 +02002824 ret = bdrv_get_allocated_file_size(bs->file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002825 if (ret < 0) {
2826 return ret;
2827 }
2828 for (i = 0; i < s->num_extents; i++) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002829 if (s->extents[i].file == bs->file) {
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002830 continue;
2831 }
Kevin Wolf24bc15d2015-06-15 13:50:20 +02002832 r = bdrv_get_allocated_file_size(s->extents[i].file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002833 if (r < 0) {
2834 return r;
2835 }
2836 ret += r;
2837 }
2838 return ret;
2839}
Kevin Wolf0e7e1982009-05-18 16:42:10 +02002840
Fam Zhengda7a50f2013-07-01 11:33:17 +08002841static 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 Wolf24bc15d2015-06-15 13:50:20 +02002850 if (!bdrv_has_zero_init(s->extents[i].file->bs)) {
Fam Zhengda7a50f2013-07-01 11:33:17 +08002851 return 0;
2852 }
2853 }
2854 }
2855 return 1;
2856}
2857
Fam Zhengf4c129a2013-10-31 10:06:23 +08002858static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
2859{
2860 ImageInfo *info = g_new0(ImageInfo, 1);
2861
Max Reitzf30c66b2019-02-01 20:29:05 +01002862 bdrv_refresh_filename(extent->file->bs);
Fam Zhengf4c129a2013-10-31 10:06:23 +08002863 *info = (ImageInfo){
Kevin Wolf24bc15d2015-06-15 13:50:20 +02002864 .filename = g_strdup(extent->file->bs->filename),
Fam Zhengf4c129a2013-10-31 10:06:23 +08002865 .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 Bonzini2fd61632018-03-01 17:36:19 +01002876static int coroutine_fn vmdk_co_check(BlockDriverState *bs,
2877 BdrvCheckResult *result,
2878 BdrvCheckMode fix)
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002879{
2880 BDRVVmdkState *s = bs->opaque;
2881 VmdkExtent *extent = NULL;
2882 int64_t sector_num = 0;
Markus Armbruster57322b72014-06-26 13:23:22 +02002883 int64_t total_sectors = bdrv_nb_sectors(bs);
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002884 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 Zheng0e51b9b2017-08-04 22:09:42 +08002900 ret = -EINVAL;
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002901 break;
2902 }
2903 ret = get_cluster_offset(bs, extent, NULL,
2904 sector_num << BDRV_SECTOR_BITS,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08002905 false, &cluster_offset, 0, 0);
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002906 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 Zheng0e51b9b2017-08-04 22:09:42 +08002912 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 Lievenf43aa8e2014-01-29 09:34:16 +01002928 }
2929 sector_num += extent->cluster_sectors;
2930 }
2931
2932 result->corruptions++;
Fam Zheng0e51b9b2017-08-04 22:09:42 +08002933 return ret;
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002934}
2935
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03002936static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs,
2937 Error **errp)
Fam Zhengf4c129a2013-10-31 10:06:23 +08002938{
2939 int i;
2940 BDRVVmdkState *s = bs->opaque;
2941 ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
Eric Blakec3033fd2021-01-13 16:10:12 -06002942 ImageInfoList **tail;
Fam Zhengf4c129a2013-10-31 10:06:23 +08002943
2944 *spec_info = (ImageInfoSpecific){
Eric Blake6a8f9662015-10-26 16:34:54 -06002945 .type = IMAGE_INFO_SPECIFIC_KIND_VMDK,
Eric Blake32bafa82016-03-17 16:48:37 -06002946 .u = {
2947 .vmdk.data = g_new0(ImageInfoSpecificVmdk, 1),
Fam Zhengf4c129a2013-10-31 10:06:23 +08002948 },
2949 };
2950
Eric Blake32bafa82016-03-17 16:48:37 -06002951 *spec_info->u.vmdk.data = (ImageInfoSpecificVmdk) {
Fam Zhengf4c129a2013-10-31 10:06:23 +08002952 .create_type = g_strdup(s->create_type),
2953 .cid = s->cid,
2954 .parent_cid = s->parent_cid,
2955 };
2956
Eric Blakec3033fd2021-01-13 16:10:12 -06002957 tail = &spec_info->u.vmdk.data->extents;
Fam Zhengf4c129a2013-10-31 10:06:23 +08002958 for (i = 0; i < s->num_extents; i++) {
Eric Blakec3033fd2021-01-13 16:10:12 -06002959 QAPI_LIST_APPEND(tail, vmdk_get_extent_info(&s->extents[i]));
Fam Zhengf4c129a2013-10-31 10:06:23 +08002960 }
2961
2962 return spec_info;
2963}
2964
Fam Zheng5f583302014-11-14 12:09:21 +08002965static 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 Zheng74fe1882014-05-06 21:08:45 +08002972static int vmdk_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2973{
2974 int i;
2975 BDRVVmdkState *s = bs->opaque;
2976 assert(s->num_extents);
Fam Zheng5f583302014-11-14 12:09:21 +08002977
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 Zheng74fe1882014-05-06 21:08:45 +08002984 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 Zheng74fe1882014-05-06 21:08:45 +08002988 return 0;
2989}
2990
Max Reitzabc521a2019-02-01 20:29:26 +01002991static 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 Liu5820f1d2014-06-05 17:21:09 +08003008static 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 Blaked51a8142020-07-06 15:39:48 -05003029 .name = BLOCK_OPT_BACKING_FMT,
3030 .type = QEMU_OPT_STRING,
3031 .help = "Must be 'vmdk' if present",
3032 },
3033 {
Chunyan Liu5820f1d2014-06-05 17:21:09 +08003034 .name = BLOCK_OPT_COMPAT6,
3035 .type = QEMU_OPT_BOOL,
3036 .help = "VMDK version 6 image",
3037 .def_value_str = "off"
3038 },
3039 {
Janne Karhunenf2499242016-05-03 02:43:30 -07003040 .name = BLOCK_OPT_HWVERSION,
3041 .type = QEMU_OPT_STRING,
3042 .help = "VMDK hardware version",
3043 .def_value_str = "undefined"
3044 },
3045 {
Thomas Weißschuhf3d43df2021-09-13 15:04:19 +02003046 .name = BLOCK_OPT_TOOLSVERSION,
3047 .type = QEMU_OPT_STRING,
3048 .help = "VMware guest tools version",
3049 },
3050 {
Chunyan Liu5820f1d2014-06-05 17:21:09 +08003051 .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 Wolf0e7e1982009-05-18 16:42:10 +02003065};
3066
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003067static BlockDriver bdrv_vmdk = {
Fam Zhengda7a50f2013-07-01 11:33:17 +08003068 .format_name = "vmdk",
3069 .instance_size = sizeof(BDRVVmdkState),
3070 .bdrv_probe = vmdk_probe,
3071 .bdrv_open = vmdk_open,
Paolo Bonzini2fd61632018-03-01 17:36:19 +01003072 .bdrv_co_check = vmdk_co_check,
Fam Zhengda7a50f2013-07-01 11:33:17 +08003073 .bdrv_reopen_prepare = vmdk_reopen_prepare,
Max Reitz69dca432020-05-13 13:05:39 +02003074 .bdrv_child_perm = bdrv_default_perms,
Kevin Wolff10cc242016-04-25 17:34:41 +02003075 .bdrv_co_preadv = vmdk_co_preadv,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02003076 .bdrv_co_pwritev = vmdk_co_pwritev,
Pavel Butsykinb2c622d2016-07-22 11:17:45 +03003077 .bdrv_co_pwritev_compressed = vmdk_co_pwritev_compressed,
Eric Blakea620f2a2016-06-01 15:10:12 -06003078 .bdrv_co_pwrite_zeroes = vmdk_co_pwrite_zeroes,
Fam Zhengda7a50f2013-07-01 11:33:17 +08003079 .bdrv_close = vmdk_close,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01003080 .bdrv_co_create_opts = vmdk_co_create_opts,
Fam Zheng30153722018-05-15 23:36:32 +08003081 .bdrv_co_create = vmdk_co_create,
Eric Blakec72080b2018-02-13 14:26:58 -06003082 .bdrv_co_block_status = vmdk_co_block_status,
Fam Zhengda7a50f2013-07-01 11:33:17 +08003083 .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
3084 .bdrv_has_zero_init = vmdk_has_zero_init,
Fam Zhengf4c129a2013-10-31 10:06:23 +08003085 .bdrv_get_specific_info = vmdk_get_specific_info,
Kevin Wolfd34682c2013-12-11 19:26:16 +01003086 .bdrv_refresh_limits = vmdk_refresh_limits,
Fam Zheng74fe1882014-05-06 21:08:45 +08003087 .bdrv_get_info = vmdk_get_info,
Max Reitzabc521a2019-02-01 20:29:26 +01003088 .bdrv_gather_child_options = vmdk_gather_child_options,
Kevin Wolf0e7e1982009-05-18 16:42:10 +02003089
Max Reitzd67066d2020-05-13 13:05:12 +02003090 .is_format = true,
Kevin Wolf8ee79e72014-06-04 15:09:35 +02003091 .supports_backing = true,
Chunyan Liu5820f1d2014-06-05 17:21:09 +08003092 .create_opts = &vmdk_create_opts,
bellardea2384d2004-08-01 21:59:26 +00003093};
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003094
3095static void bdrv_vmdk_init(void)
3096{
3097 bdrv_register(&bdrv_vmdk);
3098}
3099
3100block_init(bdrv_vmdk_init);