blob: 84f8bbe480cbf944e87145af0b3bbd0617d52a80 [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"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010030#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010031#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010033#include "qemu/option.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010034#include "qemu/bswap.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020035#include "migration/blocker.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020036#include "qemu/cutils.h"
Stefan Weil2923d342011-11-20 12:34:30 +010037#include <zlib.h>
bellardea2384d2004-08-01 21:59:26 +000038
bellardea2384d2004-08-01 21:59:26 +000039#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
40#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
Fam Zheng432bb172011-08-12 23:19:30 +080041#define VMDK4_COMPRESSION_DEFLATE 1
Fam Zheng95b0aa42013-05-02 10:25:25 +080042#define VMDK4_FLAG_NL_DETECT (1 << 0)
Fam Zhengbb45ded2011-08-12 23:19:34 +080043#define VMDK4_FLAG_RGD (1 << 1)
Fam Zheng14ead642013-05-02 10:25:23 +080044/* Zeroed-grain enable bit */
45#define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
Fam Zheng432bb172011-08-12 23:19:30 +080046#define VMDK4_FLAG_COMPRESS (1 << 16)
47#define VMDK4_FLAG_MARKER (1 << 17)
Kevin Wolf65bd1552012-08-16 10:39:33 +020048#define VMDK4_GD_AT_END 0xffffffffffffffffULL
bellardea2384d2004-08-01 21:59:26 +000049
yuchenlina77672e2018-03-22 21:33:37 +080050#define VMDK_EXTENT_MAX_SECTORS (1ULL << 32)
51
Fam Zheng14ead642013-05-02 10:25:23 +080052#define VMDK_GTE_ZEROED 0x1
Fam Zheng65f74722013-05-02 10:25:22 +080053
54/* VMDK internal error codes */
55#define VMDK_OK 0
56#define VMDK_ERROR (-1)
57/* Cluster not allocated */
58#define VMDK_UNALLOC (-2)
59#define VMDK_ZEROED (-3)
60
Fam Zheng69e0b6d2013-05-02 10:25:24 +080061#define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
62
bellardea2384d2004-08-01 21:59:26 +000063typedef struct {
64 uint32_t version;
65 uint32_t flags;
66 uint32_t disk_sectors;
67 uint32_t granularity;
68 uint32_t l1dir_offset;
69 uint32_t l1dir_size;
70 uint32_t file_sectors;
71 uint32_t cylinders;
72 uint32_t heads;
73 uint32_t sectors_per_track;
Fam Zheng5d8caa52013-08-06 15:44:47 +080074} QEMU_PACKED VMDK3Header;
bellardea2384d2004-08-01 21:59:26 +000075
76typedef struct {
77 uint32_t version;
78 uint32_t flags;
Fam Zhenge98768d2013-08-06 15:44:48 +080079 uint64_t capacity;
80 uint64_t granularity;
81 uint64_t desc_offset;
82 uint64_t desc_size;
Fam Zhengca8804c2013-08-06 15:44:55 +080083 /* Number of GrainTableEntries per GrainTable */
84 uint32_t num_gtes_per_gt;
Fam Zhenge98768d2013-08-06 15:44:48 +080085 uint64_t rgd_offset;
86 uint64_t gd_offset;
87 uint64_t grain_offset;
bellardea2384d2004-08-01 21:59:26 +000088 char filler[1];
89 char check_bytes[4];
Fam Zheng432bb172011-08-12 23:19:30 +080090 uint16_t compressAlgorithm;
Stefan Weil541dc0d2011-08-31 12:38:01 +020091} QEMU_PACKED VMDK4Header;
bellardea2384d2004-08-01 21:59:26 +000092
93#define L2_CACHE_SIZE 16
94
Fam Zhengb3976d32011-07-12 19:56:28 +080095typedef struct VmdkExtent {
Kevin Wolf24bc15d2015-06-15 13:50:20 +020096 BdrvChild *file;
Fam Zhengb3976d32011-07-12 19:56:28 +080097 bool flat;
Fam Zheng432bb172011-08-12 23:19:30 +080098 bool compressed;
99 bool has_marker;
Fam Zheng14ead642013-05-02 10:25:23 +0800100 bool has_zero_grain;
101 int version;
Fam Zhengb3976d32011-07-12 19:56:28 +0800102 int64_t sectors;
103 int64_t end_sector;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800104 int64_t flat_start_offset;
bellardea2384d2004-08-01 21:59:26 +0000105 int64_t l1_table_offset;
bellardff1afc72005-04-26 21:08:00 +0000106 int64_t l1_backup_table_offset;
bellardea2384d2004-08-01 21:59:26 +0000107 uint32_t *l1_table;
bellardff1afc72005-04-26 21:08:00 +0000108 uint32_t *l1_backup_table;
bellardea2384d2004-08-01 21:59:26 +0000109 unsigned int l1_size;
110 uint32_t l1_entry_sectors;
111
112 unsigned int l2_size;
113 uint32_t *l2_cache;
114 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
115 uint32_t l2_cache_counts[L2_CACHE_SIZE];
116
Fam Zheng301c7d32013-09-23 17:18:29 +0800117 int64_t cluster_sectors;
Fam Zhengc6ac36e2014-07-30 14:39:10 +0800118 int64_t next_cluster_sector;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800119 char *type;
Fam Zhengb3976d32011-07-12 19:56:28 +0800120} VmdkExtent;
121
122typedef struct BDRVVmdkState {
Paolo Bonzini848c66e2011-10-20 13:16:21 +0200123 CoMutex lock;
Fam Zhenge98768d2013-08-06 15:44:48 +0800124 uint64_t desc_offset;
Fam Zheng69b4d862011-07-12 19:56:34 +0800125 bool cid_updated;
Fam Zhengc338b6a2013-10-18 13:17:19 +0800126 bool cid_checked;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800127 uint32_t cid;
ths5f4da8c2007-01-24 21:05:24 +0000128 uint32_t parent_cid;
Fam Zhengb3976d32011-07-12 19:56:28 +0800129 int num_extents;
130 /* Extent array with num_extents entries, ascend ordered by address */
131 VmdkExtent *extents;
Kevin Wolf2bc31662011-11-22 16:50:27 +0100132 Error *migration_blocker;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800133 char *create_type;
bellardea2384d2004-08-01 21:59:26 +0000134} BDRVVmdkState;
135
ths630530a2007-06-18 15:01:30 +0000136typedef struct VmdkMetaData {
ths630530a2007-06-18 15:01:30 +0000137 unsigned int l1_index;
138 unsigned int l2_index;
139 unsigned int l2_offset;
140 int valid;
Fam Zhengcdeaf1f2013-05-02 10:25:27 +0800141 uint32_t *l2_cache_entry;
ths630530a2007-06-18 15:01:30 +0000142} VmdkMetaData;
143
Fam Zheng432bb172011-08-12 23:19:30 +0800144typedef struct VmdkGrainMarker {
145 uint64_t lba;
146 uint32_t size;
147 uint8_t data[0];
Fam Zheng5d8caa52013-08-06 15:44:47 +0800148} QEMU_PACKED VmdkGrainMarker;
Fam Zheng432bb172011-08-12 23:19:30 +0800149
Kevin Wolf65bd1552012-08-16 10:39:33 +0200150enum {
151 MARKER_END_OF_STREAM = 0,
152 MARKER_GRAIN_TABLE = 1,
153 MARKER_GRAIN_DIRECTORY = 2,
154 MARKER_FOOTER = 3,
155};
156
bellardea2384d2004-08-01 21:59:26 +0000157static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
158{
159 uint32_t magic;
160
Fam Zhengae261c82011-07-12 19:56:38 +0800161 if (buf_size < 4) {
bellardea2384d2004-08-01 21:59:26 +0000162 return 0;
Fam Zhengae261c82011-07-12 19:56:38 +0800163 }
bellardea2384d2004-08-01 21:59:26 +0000164 magic = be32_to_cpu(*(uint32_t *)buf);
165 if (magic == VMDK3_MAGIC ||
Fam Zheng01fc99d2011-07-12 19:56:30 +0800166 magic == VMDK4_MAGIC) {
bellardea2384d2004-08-01 21:59:26 +0000167 return 100;
Fam Zheng01fc99d2011-07-12 19:56:30 +0800168 } else {
169 const char *p = (const char *)buf;
170 const char *end = p + buf_size;
171 while (p < end) {
172 if (*p == '#') {
173 /* skip comment line */
174 while (p < end && *p != '\n') {
175 p++;
176 }
177 p++;
178 continue;
179 }
180 if (*p == ' ') {
181 while (p < end && *p == ' ') {
182 p++;
183 }
184 /* skip '\r' if windows line endings used. */
185 if (p < end && *p == '\r') {
186 p++;
187 }
188 /* only accept blank lines before 'version=' line */
189 if (p == end || *p != '\n') {
190 return 0;
191 }
192 p++;
193 continue;
194 }
195 if (end - p >= strlen("version=X\n")) {
196 if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
197 strncmp("version=2\n", p, strlen("version=2\n")) == 0) {
198 return 100;
199 }
200 }
201 if (end - p >= strlen("version=X\r\n")) {
202 if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
203 strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) {
204 return 100;
205 }
206 }
207 return 0;
208 }
bellardea2384d2004-08-01 21:59:26 +0000209 return 0;
Fam Zheng01fc99d2011-07-12 19:56:30 +0800210 }
bellardea2384d2004-08-01 21:59:26 +0000211}
212
ths3b46e622007-09-17 08:09:54 +0000213#define SECTOR_SIZE 512
Fam Zhengf66fd6c2011-07-19 08:45:23 +0800214#define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
215#define BUF_SIZE 4096
216#define HEADER_SIZE 512 /* first sector of 512 bytes */
ths5f4da8c2007-01-24 21:05:24 +0000217
Fam Zhengb3976d32011-07-12 19:56:28 +0800218static void vmdk_free_extents(BlockDriverState *bs)
219{
220 int i;
221 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800222 VmdkExtent *e;
Fam Zhengb3976d32011-07-12 19:56:28 +0800223
224 for (i = 0; i < s->num_extents; i++) {
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800225 e = &s->extents[i];
226 g_free(e->l1_table);
227 g_free(e->l2_cache);
228 g_free(e->l1_backup_table);
Fam Zhengf4c129a2013-10-31 10:06:23 +0800229 g_free(e->type);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200230 if (e->file != bs->file) {
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200231 bdrv_unref_child(bs, e->file);
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800232 }
Fam Zhengb3976d32011-07-12 19:56:28 +0800233 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500234 g_free(s->extents);
Fam Zhengb3976d32011-07-12 19:56:28 +0800235}
236
Fam Zheng86c6b422011-08-12 23:19:28 +0800237static void vmdk_free_last_extent(BlockDriverState *bs)
238{
239 BDRVVmdkState *s = bs->opaque;
240
241 if (s->num_extents == 0) {
242 return;
243 }
244 s->num_extents--;
Markus Armbruster5839e532014-08-19 10:31:08 +0200245 s->extents = g_renew(VmdkExtent, s->extents, s->num_extents);
Fam Zheng86c6b422011-08-12 23:19:28 +0800246}
247
Peter Maydell98778602017-07-09 18:06:14 +0100248/* Return -ve errno, or 0 on success and write CID into *pcid. */
249static int vmdk_read_cid(BlockDriverState *bs, int parent, uint32_t *pcid)
ths5f4da8c2007-01-24 21:05:24 +0000250{
Fam Zheng5997c212016-03-08 16:24:35 +0800251 char *desc;
Peter Maydell98778602017-07-09 18:06:14 +0100252 uint32_t cid;
blueswir17ccfb2e2008-09-14 06:45:34 +0000253 const char *p_name, *cid_str;
ths5f4da8c2007-01-24 21:05:24 +0000254 size_t cid_str_size;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800255 BDRVVmdkState *s = bs->opaque;
Kevin Wolf99f18352011-10-26 12:25:25 +0200256 int ret;
ths5f4da8c2007-01-24 21:05:24 +0000257
Fam Zheng5997c212016-03-08 16:24:35 +0800258 desc = g_malloc0(DESC_SIZE);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200259 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
Kevin Wolf99f18352011-10-26 12:25:25 +0200260 if (ret < 0) {
Peter Maydell98778602017-07-09 18:06:14 +0100261 goto out;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800262 }
ths5f4da8c2007-01-24 21:05:24 +0000263
264 if (parent) {
265 cid_str = "parentCID";
266 cid_str_size = sizeof("parentCID");
267 } else {
268 cid_str = "CID";
269 cid_str_size = sizeof("CID");
270 }
271
Kevin Wolf93897b92011-10-26 12:25:52 +0200272 desc[DESC_SIZE - 1] = '\0';
Fam Zhengae261c82011-07-12 19:56:38 +0800273 p_name = strstr(desc, cid_str);
Peter Maydell98778602017-07-09 18:06:14 +0100274 if (p_name == NULL) {
275 ret = -EINVAL;
276 goto out;
ths5f4da8c2007-01-24 21:05:24 +0000277 }
Peter Maydell98778602017-07-09 18:06:14 +0100278 p_name += cid_str_size;
279 if (sscanf(p_name, "%" SCNx32, &cid) != 1) {
280 ret = -EINVAL;
281 goto out;
282 }
283 *pcid = cid;
284 ret = 0;
ths5f4da8c2007-01-24 21:05:24 +0000285
Peter Maydell98778602017-07-09 18:06:14 +0100286out:
Fam Zheng5997c212016-03-08 16:24:35 +0800287 g_free(desc);
Peter Maydell98778602017-07-09 18:06:14 +0100288 return ret;
ths5f4da8c2007-01-24 21:05:24 +0000289}
290
291static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
292{
Fam Zheng965415e2016-03-08 16:24:34 +0800293 char *desc, *tmp_desc;
ths5f4da8c2007-01-24 21:05:24 +0000294 char *p_name, *tmp_str;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800295 BDRVVmdkState *s = bs->opaque;
Fam Zheng965415e2016-03-08 16:24:34 +0800296 int ret = 0;
ths5f4da8c2007-01-24 21:05:24 +0000297
Fam Zheng965415e2016-03-08 16:24:34 +0800298 desc = g_malloc0(DESC_SIZE);
299 tmp_desc = g_malloc0(DESC_SIZE);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200300 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
Kevin Wolf99f18352011-10-26 12:25:25 +0200301 if (ret < 0) {
Fam Zheng965415e2016-03-08 16:24:34 +0800302 goto out;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800303 }
ths5f4da8c2007-01-24 21:05:24 +0000304
Kevin Wolf93897b92011-10-26 12:25:52 +0200305 desc[DESC_SIZE - 1] = '\0';
Fam Zhengae261c82011-07-12 19:56:38 +0800306 tmp_str = strstr(desc, "parentCID");
Kevin Wolf93897b92011-10-26 12:25:52 +0200307 if (tmp_str == NULL) {
Fam Zheng965415e2016-03-08 16:24:34 +0800308 ret = -EINVAL;
309 goto out;
Kevin Wolf93897b92011-10-26 12:25:52 +0200310 }
311
Fam Zheng965415e2016-03-08 16:24:34 +0800312 pstrcpy(tmp_desc, DESC_SIZE, tmp_str);
Fam Zhengae261c82011-07-12 19:56:38 +0800313 p_name = strstr(desc, "CID");
314 if (p_name != NULL) {
ths5f4da8c2007-01-24 21:05:24 +0000315 p_name += sizeof("CID");
Fam Zheng965415e2016-03-08 16:24:34 +0800316 snprintf(p_name, DESC_SIZE - (p_name - desc), "%" PRIx32 "\n", cid);
317 pstrcat(desc, DESC_SIZE, tmp_desc);
ths5f4da8c2007-01-24 21:05:24 +0000318 }
319
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200320 ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
Kevin Wolf99f18352011-10-26 12:25:25 +0200321
Fam Zheng965415e2016-03-08 16:24:34 +0800322out:
323 g_free(desc);
324 g_free(tmp_desc);
325 return ret;
ths5f4da8c2007-01-24 21:05:24 +0000326}
327
328static int vmdk_is_cid_valid(BlockDriverState *bs)
329{
ths5f4da8c2007-01-24 21:05:24 +0000330 BDRVVmdkState *s = bs->opaque;
ths5f4da8c2007-01-24 21:05:24 +0000331 uint32_t cur_pcid;
332
Kevin Wolf760e0062015-06-17 14:55:21 +0200333 if (!s->cid_checked && bs->backing) {
334 BlockDriverState *p_bs = bs->backing->bs;
335
Peter Maydell98778602017-07-09 18:06:14 +0100336 if (vmdk_read_cid(p_bs, 0, &cur_pcid) != 0) {
337 /* read failure: report as not valid */
338 return 0;
339 }
Fam Zhengae261c82011-07-12 19:56:38 +0800340 if (s->parent_cid != cur_pcid) {
341 /* CID not valid */
ths5f4da8c2007-01-24 21:05:24 +0000342 return 0;
Fam Zhengae261c82011-07-12 19:56:38 +0800343 }
ths5f4da8c2007-01-24 21:05:24 +0000344 }
Fam Zhengc338b6a2013-10-18 13:17:19 +0800345 s->cid_checked = true;
Fam Zhengae261c82011-07-12 19:56:38 +0800346 /* CID valid */
ths5f4da8c2007-01-24 21:05:24 +0000347 return 1;
348}
349
Kevin Wolf67251a32015-04-09 18:54:04 +0200350/* We have nothing to do for VMDK reopen, stubs just return success */
Jeff Cody38975752012-09-20 15:13:30 -0400351static int vmdk_reopen_prepare(BDRVReopenState *state,
352 BlockReopenQueue *queue, Error **errp)
353{
Jeff Cody38975752012-09-20 15:13:30 -0400354 assert(state != NULL);
355 assert(state->bs != NULL);
Kevin Wolf67251a32015-04-09 18:54:04 +0200356 return 0;
Jeff Cody38975752012-09-20 15:13:30 -0400357}
358
Kevin Wolf9949f972010-04-16 21:07:19 +0200359static int vmdk_parent_open(BlockDriverState *bs)
ths5f4da8c2007-01-24 21:05:24 +0000360{
ths5fafdf22007-09-16 21:08:06 +0000361 char *p_name;
Fam Zheng71968db2016-03-08 16:24:36 +0800362 char *desc;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800363 BDRVVmdkState *s = bs->opaque;
Paolo Bonzini588b65a2011-10-20 13:16:19 +0200364 int ret;
ths5f4da8c2007-01-24 21:05:24 +0000365
Fam Zheng71968db2016-03-08 16:24:36 +0800366 desc = g_malloc0(DESC_SIZE + 1);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200367 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
Paolo Bonzini588b65a2011-10-20 13:16:19 +0200368 if (ret < 0) {
Fam Zheng71968db2016-03-08 16:24:36 +0800369 goto out;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800370 }
Fam Zheng71968db2016-03-08 16:24:36 +0800371 ret = 0;
ths5f4da8c2007-01-24 21:05:24 +0000372
Fam Zhengae261c82011-07-12 19:56:38 +0800373 p_name = strstr(desc, "parentFileNameHint");
374 if (p_name != NULL) {
ths5f4da8c2007-01-24 21:05:24 +0000375 char *end_name;
ths5f4da8c2007-01-24 21:05:24 +0000376
377 p_name += sizeof("parentFileNameHint") + 1;
Fam Zhengae261c82011-07-12 19:56:38 +0800378 end_name = strchr(p_name, '\"');
379 if (end_name == NULL) {
Fam Zheng71968db2016-03-08 16:24:36 +0800380 ret = -EINVAL;
381 goto out;
Fam Zhengae261c82011-07-12 19:56:38 +0800382 }
383 if ((end_name - p_name) > sizeof(bs->backing_file) - 1) {
Fam Zheng71968db2016-03-08 16:24:36 +0800384 ret = -EINVAL;
385 goto out;
Fam Zhengae261c82011-07-12 19:56:38 +0800386 }
ths3b46e622007-09-17 08:09:54 +0000387
Kevin Wolfb1712712009-07-17 08:20:41 +0200388 pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
ths5f4da8c2007-01-24 21:05:24 +0000389 }
390
Fam Zheng71968db2016-03-08 16:24:36 +0800391out:
392 g_free(desc);
393 return ret;
ths5f4da8c2007-01-24 21:05:24 +0000394}
395
Fam Zhengb3976d32011-07-12 19:56:28 +0800396/* Create and append extent to the extent array. Return the added VmdkExtent
397 * address. return NULL if allocation failed. */
Fam Zheng8aa13312013-08-06 15:44:51 +0800398static int vmdk_add_extent(BlockDriverState *bs,
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200399 BdrvChild *file, bool flat, int64_t sectors,
Fam Zhengb3976d32011-07-12 19:56:28 +0800400 int64_t l1_offset, int64_t l1_backup_offset,
401 uint32_t l1_size,
Fam Zheng8aa13312013-08-06 15:44:51 +0800402 int l2_size, uint64_t cluster_sectors,
Fam Zheng48239702013-10-11 15:43:22 +0800403 VmdkExtent **new_extent,
404 Error **errp)
Fam Zhengb3976d32011-07-12 19:56:28 +0800405{
406 VmdkExtent *extent;
407 BDRVVmdkState *s = bs->opaque;
Markus Armbruster0a156f72014-08-21 14:36:19 +0200408 int64_t nb_sectors;
Fam Zhengb3976d32011-07-12 19:56:28 +0800409
Fam Zheng8aa13312013-08-06 15:44:51 +0800410 if (cluster_sectors > 0x200000) {
411 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
Fam Zheng48239702013-10-11 15:43:22 +0800412 error_setg(errp, "Invalid granularity, image may be corrupt");
413 return -EFBIG;
Fam Zheng8aa13312013-08-06 15:44:51 +0800414 }
Fam Zhengb0651b82013-08-19 18:54:25 +0800415 if (l1_size > 512 * 1024 * 1024) {
416 /* Although with big capacity and small l1_entry_sectors, we can get a
417 * big l1_size, we don't want unbounded value to allocate the table.
418 * Limit it to 512M, which is 16PB for default cluster and L2 table
419 * size */
Fam Zheng48239702013-10-11 15:43:22 +0800420 error_setg(errp, "L1 size too big");
Fam Zhengb0651b82013-08-19 18:54:25 +0800421 return -EFBIG;
422 }
Fam Zheng8aa13312013-08-06 15:44:51 +0800423
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200424 nb_sectors = bdrv_nb_sectors(file->bs);
Markus Armbruster0a156f72014-08-21 14:36:19 +0200425 if (nb_sectors < 0) {
426 return nb_sectors;
Fam Zhengc6ac36e2014-07-30 14:39:10 +0800427 }
428
Markus Armbruster5839e532014-08-19 10:31:08 +0200429 s->extents = g_renew(VmdkExtent, s->extents, s->num_extents + 1);
Fam Zhengb3976d32011-07-12 19:56:28 +0800430 extent = &s->extents[s->num_extents];
431 s->num_extents++;
432
433 memset(extent, 0, sizeof(VmdkExtent));
434 extent->file = file;
435 extent->flat = flat;
436 extent->sectors = sectors;
437 extent->l1_table_offset = l1_offset;
438 extent->l1_backup_table_offset = l1_backup_offset;
439 extent->l1_size = l1_size;
440 extent->l1_entry_sectors = l2_size * cluster_sectors;
441 extent->l2_size = l2_size;
Fam Zheng301c7d32013-09-23 17:18:29 +0800442 extent->cluster_sectors = flat ? sectors : cluster_sectors;
Markus Armbruster0a156f72014-08-21 14:36:19 +0200443 extent->next_cluster_sector = ROUND_UP(nb_sectors, cluster_sectors);
Fam Zhengb3976d32011-07-12 19:56:28 +0800444
445 if (s->num_extents > 1) {
446 extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
447 } else {
448 extent->end_sector = extent->sectors;
449 }
450 bs->total_sectors = extent->end_sector;
Fam Zheng8aa13312013-08-06 15:44:51 +0800451 if (new_extent) {
452 *new_extent = extent;
453 }
454 return 0;
Fam Zhengb3976d32011-07-12 19:56:28 +0800455}
456
Fam Zheng48239702013-10-11 15:43:22 +0800457static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
458 Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000459{
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800460 int ret;
Fam Zheng13c49412015-05-05 17:28:13 +0800461 size_t l1_size;
462 int i;
ths5f4da8c2007-01-24 21:05:24 +0000463
bellardea2384d2004-08-01 21:59:26 +0000464 /* read the L1 table */
Fam Zhengb3976d32011-07-12 19:56:28 +0800465 l1_size = extent->l1_size * sizeof(uint32_t);
Kevin Wolfd6e59932014-05-20 13:56:27 +0200466 extent->l1_table = g_try_malloc(l1_size);
467 if (l1_size && extent->l1_table == NULL) {
468 return -ENOMEM;
469 }
470
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200471 ret = bdrv_pread(extent->file,
Fam Zheng48239702013-10-11 15:43:22 +0800472 extent->l1_table_offset,
473 extent->l1_table,
474 l1_size);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800475 if (ret < 0) {
Fam Zheng48239702013-10-11 15:43:22 +0800476 error_setg_errno(errp, -ret,
477 "Could not read l1 table from extent '%s'",
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200478 extent->file->bs->filename);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800479 goto fail_l1;
Fam Zhengb3976d32011-07-12 19:56:28 +0800480 }
481 for (i = 0; i < extent->l1_size; i++) {
482 le32_to_cpus(&extent->l1_table[i]);
bellardea2384d2004-08-01 21:59:26 +0000483 }
484
Fam Zhengb3976d32011-07-12 19:56:28 +0800485 if (extent->l1_backup_table_offset) {
Kevin Wolfd6e59932014-05-20 13:56:27 +0200486 extent->l1_backup_table = g_try_malloc(l1_size);
487 if (l1_size && extent->l1_backup_table == NULL) {
488 ret = -ENOMEM;
489 goto fail_l1;
490 }
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200491 ret = bdrv_pread(extent->file,
Fam Zheng48239702013-10-11 15:43:22 +0800492 extent->l1_backup_table_offset,
493 extent->l1_backup_table,
494 l1_size);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800495 if (ret < 0) {
Fam Zheng48239702013-10-11 15:43:22 +0800496 error_setg_errno(errp, -ret,
497 "Could not read l1 backup table from extent '%s'",
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200498 extent->file->bs->filename);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800499 goto fail_l1b;
Fam Zhengb3976d32011-07-12 19:56:28 +0800500 }
501 for (i = 0; i < extent->l1_size; i++) {
502 le32_to_cpus(&extent->l1_backup_table[i]);
bellardff1afc72005-04-26 21:08:00 +0000503 }
504 }
505
Fam Zhengb3976d32011-07-12 19:56:28 +0800506 extent->l2_cache =
Markus Armbruster5839e532014-08-19 10:31:08 +0200507 g_new(uint32_t, extent->l2_size * L2_CACHE_SIZE);
bellardea2384d2004-08-01 21:59:26 +0000508 return 0;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800509 fail_l1b:
Anthony Liguori7267c092011-08-20 22:09:37 -0500510 g_free(extent->l1_backup_table);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800511 fail_l1:
Anthony Liguori7267c092011-08-20 22:09:37 -0500512 g_free(extent->l1_table);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800513 return ret;
514}
515
Fam Zhengdaac8fd2013-08-19 18:54:27 +0800516static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200517 BdrvChild *file,
Fam Zheng48239702013-10-11 15:43:22 +0800518 int flags, Error **errp)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800519{
520 int ret;
521 uint32_t magic;
522 VMDK3Header header;
523 VmdkExtent *extent;
524
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200525 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800526 if (ret < 0) {
Fam Zheng48239702013-10-11 15:43:22 +0800527 error_setg_errno(errp, -ret,
528 "Could not read header from file '%s'",
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200529 file->bs->filename);
Fam Zheng86c6b422011-08-12 23:19:28 +0800530 return ret;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800531 }
Fam Zhengf6b61e52013-08-19 18:54:26 +0800532 ret = vmdk_add_extent(bs, file, false,
533 le32_to_cpu(header.disk_sectors),
Fam Zheng7237aec2015-04-27 22:23:01 +0800534 (int64_t)le32_to_cpu(header.l1dir_offset) << 9,
Fam Zhengf6b61e52013-08-19 18:54:26 +0800535 0,
536 le32_to_cpu(header.l1dir_size),
537 4096,
538 le32_to_cpu(header.granularity),
Fam Zheng48239702013-10-11 15:43:22 +0800539 &extent,
540 errp);
Fam Zheng8aa13312013-08-06 15:44:51 +0800541 if (ret < 0) {
542 return ret;
543 }
Fam Zheng48239702013-10-11 15:43:22 +0800544 ret = vmdk_init_tables(bs, extent, errp);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800545 if (ret) {
Fam Zheng86c6b422011-08-12 23:19:28 +0800546 /* free extent allocated by vmdk_add_extent */
547 vmdk_free_last_extent(bs);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800548 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800549 return ret;
550}
551
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100552static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
Kevin Wolfa6468362015-04-07 15:35:59 +0200553 QDict *options, Error **errp);
Fam Zhengf16f5092011-08-12 23:19:33 +0800554
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200555static char *vmdk_read_desc(BdrvChild *file, uint64_t desc_offset, Error **errp)
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100556{
557 int64_t size;
558 char *buf;
559 int ret;
560
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200561 size = bdrv_getlength(file->bs);
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100562 if (size < 0) {
563 error_setg_errno(errp, -size, "Could not access file");
564 return NULL;
565 }
566
Fam Zheng03c33592014-12-04 07:28:32 +0800567 if (size < 4) {
568 /* Both descriptor file and sparse image must be much larger than 4
569 * bytes, also callers of vmdk_read_desc want to compare the first 4
570 * bytes with VMDK4_MAGIC, let's error out if less is read. */
571 error_setg(errp, "File is too small, not a valid image");
572 return NULL;
573 }
574
Fam Zheng73b7bca2014-12-04 07:28:31 +0800575 size = MIN(size, (1 << 20) - 1); /* avoid unbounded allocation */
576 buf = g_malloc(size + 1);
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100577
578 ret = bdrv_pread(file, desc_offset, buf, size);
579 if (ret < 0) {
580 error_setg_errno(errp, -ret, "Could not read from file");
581 g_free(buf);
582 return NULL;
583 }
Fam Zheng73b7bca2014-12-04 07:28:31 +0800584 buf[ret] = 0;
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100585
586 return buf;
587}
588
Fam Zheng86c6b422011-08-12 23:19:28 +0800589static int vmdk_open_vmdk4(BlockDriverState *bs,
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200590 BdrvChild *file,
Kevin Wolfa6468362015-04-07 15:35:59 +0200591 int flags, QDict *options, Error **errp)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800592{
593 int ret;
594 uint32_t magic;
595 uint32_t l1_size, l1_entry_sectors;
596 VMDK4Header header;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800597 VmdkExtent *extent;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800598 BDRVVmdkState *s = bs->opaque;
Fam Zhengbb45ded2011-08-12 23:19:34 +0800599 int64_t l1_backup_offset = 0;
Fam Zheng3db1d982016-01-25 10:26:23 +0800600 bool compressed;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800601
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200602 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800603 if (ret < 0) {
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);
Paolo Bonzini89ac8482014-02-17 14:44:05 +0100607 return -EINVAL;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800608 }
Stefan Hajnoczi5a394b92013-06-10 11:07:33 +0200609 if (header.capacity == 0) {
Fam Zhenge98768d2013-08-06 15:44:48 +0800610 uint64_t desc_offset = le64_to_cpu(header.desc_offset);
Stefan Hajnoczi5a394b92013-06-10 11:07:33 +0200611 if (desc_offset) {
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200612 char *buf = vmdk_read_desc(file, desc_offset << 9, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100613 if (!buf) {
614 return -EINVAL;
615 }
Kevin Wolfa6468362015-04-07 15:35:59 +0200616 ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100617 g_free(buf);
618 return ret;
Stefan Hajnoczi5a394b92013-06-10 11:07:33 +0200619 }
Fam Zhengf16f5092011-08-12 23:19:33 +0800620 }
Kevin Wolf65bd1552012-08-16 10:39:33 +0200621
Fam Zhengf4c129a2013-10-31 10:06:23 +0800622 if (!s->create_type) {
623 s->create_type = g_strdup("monolithicSparse");
624 }
625
Kevin Wolf65bd1552012-08-16 10:39:33 +0200626 if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
627 /*
628 * The footer takes precedence over the header, so read it in. The
629 * footer starts at offset -1024 from the end: One sector for the
630 * footer, and another one for the end-of-stream marker.
631 */
632 struct {
633 struct {
634 uint64_t val;
635 uint32_t size;
636 uint32_t type;
637 uint8_t pad[512 - 16];
638 } QEMU_PACKED footer_marker;
639
640 uint32_t magic;
641 VMDK4Header header;
642 uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
643
644 struct {
645 uint64_t val;
646 uint32_t size;
647 uint32_t type;
648 uint8_t pad[512 - 16];
649 } QEMU_PACKED eos_marker;
650 } QEMU_PACKED footer;
651
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200652 ret = bdrv_pread(file,
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200653 bs->file->bs->total_sectors * 512 - 1536,
Kevin Wolf65bd1552012-08-16 10:39:33 +0200654 &footer, sizeof(footer));
655 if (ret < 0) {
Fam Zhengd899d2e2014-12-04 07:28:34 +0800656 error_setg_errno(errp, -ret, "Failed to read footer");
Kevin Wolf65bd1552012-08-16 10:39:33 +0200657 return ret;
658 }
659
660 /* Some sanity checks for the footer */
661 if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
662 le32_to_cpu(footer.footer_marker.size) != 0 ||
663 le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
664 le64_to_cpu(footer.eos_marker.val) != 0 ||
665 le32_to_cpu(footer.eos_marker.size) != 0 ||
666 le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
667 {
Fam Zhengd899d2e2014-12-04 07:28:34 +0800668 error_setg(errp, "Invalid footer");
Kevin Wolf65bd1552012-08-16 10:39:33 +0200669 return -EINVAL;
670 }
671
672 header = footer.header;
673 }
674
Fam Zheng3db1d982016-01-25 10:26:23 +0800675 compressed =
676 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
Fam Zheng509d39a2013-11-28 09:48:03 +0800677 if (le32_to_cpu(header.version) > 3) {
Max Reitza55448b2016-03-16 19:54:33 +0100678 error_setg(errp, "Unsupported VMDK version %" PRIu32,
679 le32_to_cpu(header.version));
Fam Zheng96c51eb2013-06-13 11:21:29 +0800680 return -ENOTSUP;
Fam Zheng3db1d982016-01-25 10:26:23 +0800681 } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR) &&
682 !compressed) {
Fam Zheng509d39a2013-11-28 09:48:03 +0800683 /* VMware KB 2064959 explains that version 3 added support for
684 * persistent changed block tracking (CBT), and backup software can
685 * read it as version=1 if it doesn't care about the changed area
686 * information. So we are safe to enable read only. */
687 error_setg(errp, "VMDK version 3 must be read only");
688 return -EINVAL;
Fam Zheng96c51eb2013-06-13 11:21:29 +0800689 }
690
Fam Zhengca8804c2013-08-06 15:44:55 +0800691 if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
Paolo Bonzini89ac8482014-02-17 14:44:05 +0100692 error_setg(errp, "L2 table size too big");
Fam Zhengf8ce0402013-08-06 15:44:52 +0800693 return -EINVAL;
694 }
695
Fam Zhengca8804c2013-08-06 15:44:55 +0800696 l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800697 * le64_to_cpu(header.granularity);
Stefan Weil75d12342012-02-25 14:01:42 +0100698 if (l1_entry_sectors == 0) {
Fam Zhengd899d2e2014-12-04 07:28:34 +0800699 error_setg(errp, "L1 entry size is invalid");
Fam Zheng86c6b422011-08-12 23:19:28 +0800700 return -EINVAL;
701 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800702 l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
703 / l1_entry_sectors;
Fam Zhengbb45ded2011-08-12 23:19:34 +0800704 if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
705 l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
706 }
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200707 if (bdrv_nb_sectors(file->bs) < le64_to_cpu(header.grain_offset)) {
Fam Zheng4ab9dab2014-04-17 11:34:37 +0800708 error_setg(errp, "File truncated, expecting at least %" PRId64 " bytes",
709 (int64_t)(le64_to_cpu(header.grain_offset)
710 * BDRV_SECTOR_SIZE));
Fam Zheng34ceed82014-01-21 15:07:43 +0800711 return -EINVAL;
712 }
713
Fam Zheng8aa13312013-08-06 15:44:51 +0800714 ret = vmdk_add_extent(bs, file, false,
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800715 le64_to_cpu(header.capacity),
716 le64_to_cpu(header.gd_offset) << 9,
Fam Zhengbb45ded2011-08-12 23:19:34 +0800717 l1_backup_offset,
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800718 l1_size,
Fam Zhengca8804c2013-08-06 15:44:55 +0800719 le32_to_cpu(header.num_gtes_per_gt),
Fam Zheng8aa13312013-08-06 15:44:51 +0800720 le64_to_cpu(header.granularity),
Fam Zheng48239702013-10-11 15:43:22 +0800721 &extent,
722 errp);
Fam Zheng8aa13312013-08-06 15:44:51 +0800723 if (ret < 0) {
724 return ret;
725 }
Fam Zheng432bb172011-08-12 23:19:30 +0800726 extent->compressed =
727 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
Fam Zhengd8a7b062014-01-23 15:10:52 +0800728 if (extent->compressed) {
729 g_free(s->create_type);
730 s->create_type = g_strdup("streamOptimized");
731 }
Fam Zheng432bb172011-08-12 23:19:30 +0800732 extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
Fam Zheng14ead642013-05-02 10:25:23 +0800733 extent->version = le32_to_cpu(header.version);
734 extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
Fam Zheng48239702013-10-11 15:43:22 +0800735 ret = vmdk_init_tables(bs, extent, errp);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800736 if (ret) {
Fam Zheng86c6b422011-08-12 23:19:28 +0800737 /* free extent allocated by vmdk_add_extent */
738 vmdk_free_last_extent(bs);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800739 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800740 return ret;
741}
742
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800743/* find an option value out of descriptor file */
744static int vmdk_parse_description(const char *desc, const char *opt_name,
745 char *buf, int buf_size)
746{
747 char *opt_pos, *opt_end;
748 const char *end = desc + strlen(desc);
749
750 opt_pos = strstr(desc, opt_name);
751 if (!opt_pos) {
Fam Zheng65f74722013-05-02 10:25:22 +0800752 return VMDK_ERROR;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800753 }
754 /* Skip "=\"" following opt_name */
755 opt_pos += strlen(opt_name) + 2;
756 if (opt_pos >= end) {
Fam Zheng65f74722013-05-02 10:25:22 +0800757 return VMDK_ERROR;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800758 }
759 opt_end = opt_pos;
760 while (opt_end < end && *opt_end != '"') {
761 opt_end++;
762 }
763 if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
Fam Zheng65f74722013-05-02 10:25:22 +0800764 return VMDK_ERROR;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800765 }
766 pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
Fam Zheng65f74722013-05-02 10:25:22 +0800767 return VMDK_OK;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800768}
769
Fam Zheng86c6b422011-08-12 23:19:28 +0800770/* Open an extent file and append to bs array */
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200771static int vmdk_open_sparse(BlockDriverState *bs, BdrvChild *file, int flags,
Kevin Wolfa6468362015-04-07 15:35:59 +0200772 char *buf, QDict *options, Error **errp)
Fam Zheng86c6b422011-08-12 23:19:28 +0800773{
774 uint32_t magic;
775
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100776 magic = ldl_be_p(buf);
Fam Zheng86c6b422011-08-12 23:19:28 +0800777 switch (magic) {
778 case VMDK3_MAGIC:
Fam Zheng48239702013-10-11 15:43:22 +0800779 return vmdk_open_vmfs_sparse(bs, file, flags, errp);
Fam Zheng86c6b422011-08-12 23:19:28 +0800780 break;
781 case VMDK4_MAGIC:
Kevin Wolfa6468362015-04-07 15:35:59 +0200782 return vmdk_open_vmdk4(bs, file, flags, options, errp);
Fam Zheng86c6b422011-08-12 23:19:28 +0800783 break;
784 default:
Paolo Bonzini76abe402014-02-17 14:44:06 +0100785 error_setg(errp, "Image not in VMDK format");
786 return -EINVAL;
Fam Zheng86c6b422011-08-12 23:19:28 +0800787 break;
788 }
789}
790
Markus Armbrustere4937692015-12-18 16:35:20 +0100791static const char *next_line(const char *s)
792{
793 while (*s) {
794 if (*s == '\n') {
795 return s + 1;
796 }
797 s++;
798 }
799 return s;
800}
801
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800802static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
Kevin Wolfa6468362015-04-07 15:35:59 +0200803 const char *desc_file_path, QDict *options,
804 Error **errp)
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800805{
806 int ret;
Jeff Cody395a22f2015-01-22 08:03:25 -0500807 int matches;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800808 char access[11];
809 char type[11];
810 char fname[512];
Markus Armbrusterd28d7372015-12-18 16:35:21 +0100811 const char *p, *np;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800812 int64_t sectors = 0;
813 int64_t flat_offset;
Jeff Codyfe206562015-01-22 08:03:26 -0500814 char *extent_path;
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200815 BdrvChild *extent_file;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800816 BDRVVmdkState *s = bs->opaque;
817 VmdkExtent *extent;
Kevin Wolfa6468362015-04-07 15:35:59 +0200818 char extent_opt_prefix[32];
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200819 Error *local_err = NULL;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800820
Markus Armbrustere4937692015-12-18 16:35:20 +0100821 for (p = desc; *p; p = next_line(p)) {
Fam Zheng8a3e0bc2014-12-04 07:28:30 +0800822 /* parse extent line in one of below formats:
823 *
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800824 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800825 * RW [size in sectors] SPARSE "file-name.vmdk"
Fam Zheng8a3e0bc2014-12-04 07:28:30 +0800826 * RW [size in sectors] VMFS "file-name.vmdk"
827 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800828 */
829 flat_offset = -1;
Jeff Cody395a22f2015-01-22 08:03:25 -0500830 matches = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
831 access, &sectors, type, fname, &flat_offset);
832 if (matches < 4 || strcmp(access, "RW")) {
Markus Armbrustere4937692015-12-18 16:35:20 +0100833 continue;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800834 } else if (!strcmp(type, "FLAT")) {
Jeff Cody395a22f2015-01-22 08:03:25 -0500835 if (matches != 5 || flat_offset < 0) {
Markus Armbrusterd28d7372015-12-18 16:35:21 +0100836 goto invalid;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800837 }
Fam Zhengdbbcaa82013-10-18 15:07:33 +0800838 } else if (!strcmp(type, "VMFS")) {
Jeff Cody395a22f2015-01-22 08:03:25 -0500839 if (matches == 4) {
Fam Zhengb47053b2013-12-09 13:24:36 +0800840 flat_offset = 0;
841 } else {
Markus Armbrusterd28d7372015-12-18 16:35:21 +0100842 goto invalid;
Fam Zhengb47053b2013-12-09 13:24:36 +0800843 }
Jeff Cody395a22f2015-01-22 08:03:25 -0500844 } else if (matches != 4) {
Markus Armbrusterd28d7372015-12-18 16:35:21 +0100845 goto invalid;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800846 }
847
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800848 if (sectors <= 0 ||
Fam Zhengdaac8fd2013-08-19 18:54:27 +0800849 (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
Paolo Bonzini04d542c2013-08-19 18:54:28 +0800850 strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) ||
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800851 (strcmp(access, "RW"))) {
Markus Armbrustere4937692015-12-18 16:35:20 +0100852 continue;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800853 }
854
Max Reitz5c984152014-12-03 14:57:22 +0100855 if (!path_is_absolute(fname) && !path_has_protocol(fname) &&
856 !desc_file_path[0])
857 {
858 error_setg(errp, "Cannot use relative extent paths with VMDK "
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200859 "descriptor file '%s'", bs->file->bs->filename);
Max Reitz5c984152014-12-03 14:57:22 +0100860 return -EINVAL;
861 }
862
Jeff Codyfe206562015-01-22 08:03:26 -0500863 extent_path = g_malloc0(PATH_MAX);
Jeff Codya7be17b2015-02-10 13:22:56 -0500864 path_combine(extent_path, PATH_MAX, desc_file_path, fname);
Kevin Wolfa6468362015-04-07 15:35:59 +0200865
866 ret = snprintf(extent_opt_prefix, 32, "extents.%d", s->num_extents);
867 assert(ret < 32);
868
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200869 extent_file = bdrv_open_child(extent_path, options, extent_opt_prefix,
870 bs, &child_file, false, &local_err);
Jeff Codyfe206562015-01-22 08:03:26 -0500871 g_free(extent_path);
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200872 if (local_err) {
873 error_propagate(errp, local_err);
874 return -EINVAL;
Fam Zheng86c6b422011-08-12 23:19:28 +0800875 }
876
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800877 /* save to extents array */
Paolo Bonzini04d542c2013-08-19 18:54:28 +0800878 if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800879 /* FLAT extent */
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800880
Fam Zheng8aa13312013-08-06 15:44:51 +0800881 ret = vmdk_add_extent(bs, extent_file, true, sectors,
Fam Zheng48239702013-10-11 15:43:22 +0800882 0, 0, 0, 0, 0, &extent, errp);
Fam Zheng8aa13312013-08-06 15:44:51 +0800883 if (ret < 0) {
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200884 bdrv_unref_child(bs, extent_file);
Fam Zheng8aa13312013-08-06 15:44:51 +0800885 return ret;
886 }
Fam Zhengf16f5092011-08-12 23:19:33 +0800887 extent->flat_start_offset = flat_offset << 9;
Fam Zhengdaac8fd2013-08-19 18:54:27 +0800888 } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
889 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200890 char *buf = vmdk_read_desc(extent_file, 0, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100891 if (!buf) {
892 ret = -EINVAL;
893 } else {
Kevin Wolfa6468362015-04-07 15:35:59 +0200894 ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, buf,
895 options, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100896 }
Stefan Hajnoczib6b1d312014-09-04 21:04:43 +0100897 g_free(buf);
Fam Zheng86c6b422011-08-12 23:19:28 +0800898 if (ret) {
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200899 bdrv_unref_child(bs, extent_file);
Fam Zheng86c6b422011-08-12 23:19:28 +0800900 return ret;
901 }
Fam Zhengf4c129a2013-10-31 10:06:23 +0800902 extent = &s->extents[s->num_extents - 1];
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800903 } else {
Fam Zheng48239702013-10-11 15:43:22 +0800904 error_setg(errp, "Unsupported extent type '%s'", type);
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200905 bdrv_unref_child(bs, extent_file);
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800906 return -ENOTSUP;
907 }
Fam Zhengf4c129a2013-10-31 10:06:23 +0800908 extent->type = g_strdup(type);
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800909 }
910 return 0;
Markus Armbrusterd28d7372015-12-18 16:35:21 +0100911
912invalid:
913 np = next_line(p);
914 assert(np != p);
915 if (np[-1] == '\n') {
916 np--;
917 }
918 error_setg(errp, "Invalid extent line: %.*s", (int)(np - p), p);
919 return -EINVAL;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800920}
921
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100922static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
Kevin Wolfa6468362015-04-07 15:35:59 +0200923 QDict *options, Error **errp)
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800924{
925 int ret;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800926 char ct[128];
927 BDRVVmdkState *s = bs->opaque;
928
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800929 if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
Paolo Bonzini76abe402014-02-17 14:44:06 +0100930 error_setg(errp, "invalid VMDK image descriptor");
931 ret = -EINVAL;
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +0300932 goto exit;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800933 }
Fam Zheng6398de52011-08-12 23:19:27 +0800934 if (strcmp(ct, "monolithicFlat") &&
Paolo Bonzini04d542c2013-08-19 18:54:28 +0800935 strcmp(ct, "vmfs") &&
Fam Zhengdaac8fd2013-08-19 18:54:27 +0800936 strcmp(ct, "vmfsSparse") &&
Fam Zheng86c6b422011-08-12 23:19:28 +0800937 strcmp(ct, "twoGbMaxExtentSparse") &&
Fam Zheng6398de52011-08-12 23:19:27 +0800938 strcmp(ct, "twoGbMaxExtentFlat")) {
Fam Zheng48239702013-10-11 15:43:22 +0800939 error_setg(errp, "Unsupported image type '%s'", ct);
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +0300940 ret = -ENOTSUP;
941 goto exit;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800942 }
Fam Zhengf4c129a2013-10-31 10:06:23 +0800943 s->create_type = g_strdup(ct);
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800944 s->desc_offset = 0;
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200945 ret = vmdk_parse_extents(buf, bs, bs->file->bs->exact_filename, options,
946 errp);
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +0300947exit:
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +0300948 return ret;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800949}
950
Max Reitz015a1032013-09-05 14:22:29 +0200951static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
952 Error **errp)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800953{
Fam Zheng9aeecbb2014-12-04 07:28:33 +0800954 char *buf;
Fam Zheng86c6b422011-08-12 23:19:28 +0800955 int ret;
956 BDRVVmdkState *s = bs->opaque;
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100957 uint32_t magic;
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530958 Error *local_err = NULL;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800959
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +0100960 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
961 false, errp);
962 if (!bs->file) {
963 return -EINVAL;
964 }
965
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200966 buf = vmdk_read_desc(bs->file, 0, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100967 if (!buf) {
968 return -EINVAL;
969 }
970
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100971 magic = ldl_be_p(buf);
972 switch (magic) {
973 case VMDK3_MAGIC:
974 case VMDK4_MAGIC:
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200975 ret = vmdk_open_sparse(bs, bs->file, flags, buf, options,
Kevin Wolf24bc15d2015-06-15 13:50:20 +0200976 errp);
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100977 s->desc_offset = 0x200;
978 break;
979 default:
Kevin Wolfa6468362015-04-07 15:35:59 +0200980 ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100981 break;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800982 }
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100983 if (ret) {
984 goto fail;
985 }
986
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +0200987 /* try to open parent images, if exist */
988 ret = vmdk_parent_open(bs);
989 if (ret) {
990 goto fail;
991 }
Peter Maydell98778602017-07-09 18:06:14 +0100992 ret = vmdk_read_cid(bs, 0, &s->cid);
993 if (ret) {
994 goto fail;
995 }
996 ret = vmdk_read_cid(bs, 1, &s->parent_cid);
997 if (ret) {
998 goto fail;
999 }
Paolo Bonzini848c66e2011-10-20 13:16:21 +02001000 qemu_co_mutex_init(&s->lock);
Kevin Wolf2bc31662011-11-22 16:50:27 +01001001
1002 /* Disable migration when VMDK images are used */
Alberto Garcia81e5f782015-04-08 12:29:19 +03001003 error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
1004 "does not support live migration",
1005 bdrv_get_device_or_node_name(bs));
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301006 ret = migrate_add_blocker(s->migration_blocker, &local_err);
1007 if (local_err) {
1008 error_propagate(errp, local_err);
1009 error_free(s->migration_blocker);
1010 goto fail;
1011 }
1012
Paolo Bonzinid1833ef2014-02-17 14:44:03 +01001013 g_free(buf);
Kevin Wolf2bc31662011-11-22 16:50:27 +01001014 return 0;
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +02001015
1016fail:
Paolo Bonzinid1833ef2014-02-17 14:44:03 +01001017 g_free(buf);
Fam Zhengf4c129a2013-10-31 10:06:23 +08001018 g_free(s->create_type);
1019 s->create_type = NULL;
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +02001020 vmdk_free_extents(bs);
1021 return ret;
bellardea2384d2004-08-01 21:59:26 +00001022}
1023
Kevin Wolfd34682c2013-12-11 19:26:16 +01001024
Kevin Wolf3baca892014-07-16 17:48:16 +02001025static void vmdk_refresh_limits(BlockDriverState *bs, Error **errp)
Kevin Wolfd34682c2013-12-11 19:26:16 +01001026{
1027 BDRVVmdkState *s = bs->opaque;
1028 int i;
1029
1030 for (i = 0; i < s->num_extents; i++) {
1031 if (!s->extents[i].flat) {
Eric Blakecf081fc2016-06-01 15:10:02 -06001032 bs->bl.pwrite_zeroes_alignment =
1033 MAX(bs->bl.pwrite_zeroes_alignment,
1034 s->extents[i].cluster_sectors << BDRV_SECTOR_BITS);
Kevin Wolfd34682c2013-12-11 19:26:16 +01001035 }
1036 }
Kevin Wolfd34682c2013-12-11 19:26:16 +01001037}
1038
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001039/**
1040 * get_whole_cluster
1041 *
1042 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1043 * to the cluster at @cluster_sector_num.
1044 *
1045 * If @skip_start_sector < @skip_end_sector, the relative range
1046 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1047 * it for call to write user data in the request.
1048 */
Fam Zhengb3976d32011-07-12 19:56:28 +08001049static int get_whole_cluster(BlockDriverState *bs,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001050 VmdkExtent *extent,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001051 uint64_t cluster_offset,
1052 uint64_t offset,
1053 uint64_t skip_start_bytes,
1054 uint64_t skip_end_bytes)
ths5f4da8c2007-01-24 21:05:24 +00001055{
Fam Zhengbf815072013-08-06 15:44:54 +08001056 int ret = VMDK_OK;
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001057 int64_t cluster_bytes;
1058 uint8_t *whole_grain;
ths5f4da8c2007-01-24 21:05:24 +00001059
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001060 /* For COW, align request sector_num to cluster start */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001061 cluster_bytes = extent->cluster_sectors << BDRV_SECTOR_BITS;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001062 offset = QEMU_ALIGN_DOWN(offset, cluster_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001063 whole_grain = qemu_blockalign(bs, cluster_bytes);
1064
Kevin Wolf760e0062015-06-17 14:55:21 +02001065 if (!bs->backing) {
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001066 memset(whole_grain, 0, skip_start_bytes);
1067 memset(whole_grain + skip_end_bytes, 0, cluster_bytes - skip_end_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001068 }
1069
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001070 assert(skip_end_bytes <= cluster_bytes);
Fam Zheng0e69c542011-07-12 19:56:29 +08001071 /* we will be here if it's first write on non-exist grain(cluster).
1072 * try to read from parent image, if exist */
Kevin Wolf760e0062015-06-17 14:55:21 +02001073 if (bs->backing && !vmdk_is_cid_valid(bs)) {
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001074 ret = VMDK_ERROR;
1075 goto exit;
1076 }
ths5f4da8c2007-01-24 21:05:24 +00001077
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001078 /* Read backing data before skip range */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001079 if (skip_start_bytes > 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02001080 if (bs->backing) {
Max Reitz23c4b2a2017-11-23 03:08:19 +01001081 /* qcow2 emits this on bs->file instead of bs->backing */
1082 BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001083 ret = bdrv_pread(bs->backing, offset, whole_grain,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001084 skip_start_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001085 if (ret < 0) {
1086 ret = VMDK_ERROR;
1087 goto exit;
1088 }
Kevin Wolfc3365002010-04-16 19:28:14 +02001089 }
Max Reitz23c4b2a2017-11-23 03:08:19 +01001090 BLKDBG_EVENT(extent->file, BLKDBG_COW_WRITE);
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02001091 ret = bdrv_pwrite(extent->file, cluster_offset, whole_grain,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001092 skip_start_bytes);
Kevin Wolfc3365002010-04-16 19:28:14 +02001093 if (ret < 0) {
Fam Zhengbf815072013-08-06 15:44:54 +08001094 ret = VMDK_ERROR;
1095 goto exit;
ths630530a2007-06-18 15:01:30 +00001096 }
ths5f4da8c2007-01-24 21:05:24 +00001097 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001098 /* Read backing data after skip range */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001099 if (skip_end_bytes < cluster_bytes) {
Kevin Wolf760e0062015-06-17 14:55:21 +02001100 if (bs->backing) {
Max Reitz23c4b2a2017-11-23 03:08:19 +01001101 /* qcow2 emits this on bs->file instead of bs->backing */
1102 BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001103 ret = bdrv_pread(bs->backing, offset + skip_end_bytes,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001104 whole_grain + skip_end_bytes,
1105 cluster_bytes - skip_end_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001106 if (ret < 0) {
1107 ret = VMDK_ERROR;
1108 goto exit;
1109 }
1110 }
Max Reitz23c4b2a2017-11-23 03:08:19 +01001111 BLKDBG_EVENT(extent->file, BLKDBG_COW_WRITE);
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02001112 ret = bdrv_pwrite(extent->file, cluster_offset + skip_end_bytes,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001113 whole_grain + skip_end_bytes,
1114 cluster_bytes - skip_end_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001115 if (ret < 0) {
1116 ret = VMDK_ERROR;
1117 goto exit;
1118 }
1119 }
1120
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001121 ret = VMDK_OK;
Fam Zhengbf815072013-08-06 15:44:54 +08001122exit:
1123 qemu_vfree(whole_grain);
1124 return ret;
ths5f4da8c2007-01-24 21:05:24 +00001125}
1126
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001127static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data,
1128 uint32_t offset)
ths630530a2007-06-18 15:01:30 +00001129{
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001130 offset = cpu_to_le32(offset);
ths630530a2007-06-18 15:01:30 +00001131 /* update L2 table */
Max Reitz23c4b2a2017-11-23 03:08:19 +01001132 BLKDBG_EVENT(extent->file, BLKDBG_L2_UPDATE);
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02001133 if (bdrv_pwrite_sync(extent->file,
Fam Zhengb3976d32011-07-12 19:56:28 +08001134 ((int64_t)m_data->l2_offset * 512)
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001135 + (m_data->l2_index * sizeof(offset)),
Fam Zhenge304e8e2013-05-02 10:25:26 +08001136 &offset, sizeof(offset)) < 0) {
Fam Zheng65f74722013-05-02 10:25:22 +08001137 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001138 }
ths630530a2007-06-18 15:01:30 +00001139 /* update backup L2 table */
Fam Zhengb3976d32011-07-12 19:56:28 +08001140 if (extent->l1_backup_table_offset != 0) {
1141 m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02001142 if (bdrv_pwrite_sync(extent->file,
Fam Zhengb3976d32011-07-12 19:56:28 +08001143 ((int64_t)m_data->l2_offset * 512)
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001144 + (m_data->l2_index * sizeof(offset)),
Fam Zhenge304e8e2013-05-02 10:25:26 +08001145 &offset, sizeof(offset)) < 0) {
Fam Zheng65f74722013-05-02 10:25:22 +08001146 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001147 }
ths630530a2007-06-18 15:01:30 +00001148 }
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001149 if (m_data->l2_cache_entry) {
1150 *m_data->l2_cache_entry = offset;
1151 }
ths630530a2007-06-18 15:01:30 +00001152
Fam Zheng65f74722013-05-02 10:25:22 +08001153 return VMDK_OK;
ths630530a2007-06-18 15:01:30 +00001154}
1155
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001156/**
1157 * get_cluster_offset
1158 *
1159 * Look up cluster offset in extent file by sector number, and store in
1160 * @cluster_offset.
1161 *
1162 * For flat extents, the start offset as parsed from the description file is
1163 * returned.
1164 *
1165 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1166 * offset for a new cluster and update L2 cache. If there is a backing file,
1167 * COW is done before returning; otherwise, zeroes are written to the allocated
1168 * cluster. Both COW and zero writing skips the sector range
1169 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1170 * has new data to write there.
1171 *
1172 * Returns: VMDK_OK if cluster exists and mapped in the image.
1173 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1174 * VMDK_ERROR if failed.
1175 */
Fam Zheng91b85bd2011-07-12 19:56:35 +08001176static int get_cluster_offset(BlockDriverState *bs,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001177 VmdkExtent *extent,
1178 VmdkMetaData *m_data,
1179 uint64_t offset,
1180 bool allocate,
1181 uint64_t *cluster_offset,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001182 uint64_t skip_start_bytes,
1183 uint64_t skip_end_bytes)
bellardea2384d2004-08-01 21:59:26 +00001184{
bellardea2384d2004-08-01 21:59:26 +00001185 unsigned int l1_index, l2_offset, l2_index;
1186 int min_index, i, j;
Fam Zhenge304e8e2013-05-02 10:25:26 +08001187 uint32_t min_count, *l2_table;
Fam Zheng14ead642013-05-02 10:25:23 +08001188 bool zeroed = false;
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001189 int64_t ret;
Fam Zhengd1319b02014-09-23 09:56:21 +08001190 int64_t cluster_sector;
ths630530a2007-06-18 15:01:30 +00001191
Fam Zhengae261c82011-07-12 19:56:38 +08001192 if (m_data) {
ths630530a2007-06-18 15:01:30 +00001193 m_data->valid = 0;
Fam Zhengae261c82011-07-12 19:56:38 +08001194 }
Fam Zheng91b85bd2011-07-12 19:56:35 +08001195 if (extent->flat) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001196 *cluster_offset = extent->flat_start_offset;
Fam Zheng65f74722013-05-02 10:25:22 +08001197 return VMDK_OK;
Fam Zheng91b85bd2011-07-12 19:56:35 +08001198 }
ths630530a2007-06-18 15:01:30 +00001199
Fam Zheng6398de52011-08-12 23:19:27 +08001200 offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
Fam Zhengb3976d32011-07-12 19:56:28 +08001201 l1_index = (offset >> 9) / extent->l1_entry_sectors;
1202 if (l1_index >= extent->l1_size) {
Fam Zheng65f74722013-05-02 10:25:22 +08001203 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001204 }
1205 l2_offset = extent->l1_table[l1_index];
1206 if (!l2_offset) {
Fam Zheng65f74722013-05-02 10:25:22 +08001207 return VMDK_UNALLOC;
Fam Zhengb3976d32011-07-12 19:56:28 +08001208 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001209 for (i = 0; i < L2_CACHE_SIZE; i++) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001210 if (l2_offset == extent->l2_cache_offsets[i]) {
bellardea2384d2004-08-01 21:59:26 +00001211 /* increment the hit count */
Fam Zhengb3976d32011-07-12 19:56:28 +08001212 if (++extent->l2_cache_counts[i] == 0xffffffff) {
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001213 for (j = 0; j < L2_CACHE_SIZE; j++) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001214 extent->l2_cache_counts[j] >>= 1;
bellardea2384d2004-08-01 21:59:26 +00001215 }
1216 }
Fam Zhengb3976d32011-07-12 19:56:28 +08001217 l2_table = extent->l2_cache + (i * extent->l2_size);
bellardea2384d2004-08-01 21:59:26 +00001218 goto found;
1219 }
1220 }
1221 /* not found: load a new entry in the least used one */
1222 min_index = 0;
1223 min_count = 0xffffffff;
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001224 for (i = 0; i < L2_CACHE_SIZE; i++) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001225 if (extent->l2_cache_counts[i] < min_count) {
1226 min_count = extent->l2_cache_counts[i];
bellardea2384d2004-08-01 21:59:26 +00001227 min_index = i;
1228 }
1229 }
Fam Zhengb3976d32011-07-12 19:56:28 +08001230 l2_table = extent->l2_cache + (min_index * extent->l2_size);
Max Reitz23c4b2a2017-11-23 03:08:19 +01001231 BLKDBG_EVENT(extent->file, BLKDBG_L2_LOAD);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001232 if (bdrv_pread(extent->file,
Fam Zhengb3976d32011-07-12 19:56:28 +08001233 (int64_t)l2_offset * 512,
1234 l2_table,
1235 extent->l2_size * sizeof(uint32_t)
1236 ) != extent->l2_size * sizeof(uint32_t)) {
Fam Zheng65f74722013-05-02 10:25:22 +08001237 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001238 }
ths5f4da8c2007-01-24 21:05:24 +00001239
Fam Zhengb3976d32011-07-12 19:56:28 +08001240 extent->l2_cache_offsets[min_index] = l2_offset;
1241 extent->l2_cache_counts[min_index] = 1;
bellardea2384d2004-08-01 21:59:26 +00001242 found:
Fam Zhengb3976d32011-07-12 19:56:28 +08001243 l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001244 cluster_sector = le32_to_cpu(l2_table[l2_index]);
ths630530a2007-06-18 15:01:30 +00001245
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001246 if (extent->has_zero_grain && cluster_sector == VMDK_GTE_ZEROED) {
Fam Zheng14ead642013-05-02 10:25:23 +08001247 zeroed = true;
1248 }
1249
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001250 if (!cluster_sector || zeroed) {
Fam Zheng91b85bd2011-07-12 19:56:35 +08001251 if (!allocate) {
Fam Zheng14ead642013-05-02 10:25:23 +08001252 return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
Fam Zheng91b85bd2011-07-12 19:56:35 +08001253 }
ths5f4da8c2007-01-24 21:05:24 +00001254
yuchenlina77672e2018-03-22 21:33:37 +08001255 if (extent->next_cluster_sector >= VMDK_EXTENT_MAX_SECTORS) {
1256 return VMDK_ERROR;
1257 }
1258
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001259 cluster_sector = extent->next_cluster_sector;
1260 extent->next_cluster_sector += extent->cluster_sectors;
Kevin Wolf9949f972010-04-16 21:07:19 +02001261
ths630530a2007-06-18 15:01:30 +00001262 /* First of all we write grain itself, to avoid race condition
1263 * that may to corrupt the image.
1264 * This problem may occur because of insufficient space on host disk
1265 * or inappropriate VM shutdown.
1266 */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001267 ret = get_whole_cluster(bs, extent, cluster_sector * BDRV_SECTOR_SIZE,
1268 offset, skip_start_bytes, skip_end_bytes);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001269 if (ret) {
1270 return ret;
ths630530a2007-06-18 15:01:30 +00001271 }
Reda Sallahi524089b2016-07-07 10:42:49 +02001272 if (m_data) {
1273 m_data->valid = 1;
1274 m_data->l1_index = l1_index;
1275 m_data->l2_index = l2_index;
1276 m_data->l2_offset = l2_offset;
1277 m_data->l2_cache_entry = &l2_table[l2_index];
1278 }
bellardff1afc72005-04-26 21:08:00 +00001279 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001280 *cluster_offset = cluster_sector << BDRV_SECTOR_BITS;
Fam Zheng65f74722013-05-02 10:25:22 +08001281 return VMDK_OK;
bellardea2384d2004-08-01 21:59:26 +00001282}
1283
Fam Zhengb3976d32011-07-12 19:56:28 +08001284static VmdkExtent *find_extent(BDRVVmdkState *s,
1285 int64_t sector_num, VmdkExtent *start_hint)
1286{
1287 VmdkExtent *extent = start_hint;
1288
1289 if (!extent) {
1290 extent = &s->extents[0];
1291 }
1292 while (extent < &s->extents[s->num_extents]) {
1293 if (sector_num < extent->end_sector) {
1294 return extent;
1295 }
1296 extent++;
1297 }
1298 return NULL;
1299}
1300
Kevin Wolfa844a2b2016-04-25 17:14:48 +02001301static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent *extent,
1302 int64_t offset)
1303{
Eduardo Habkost9be38592016-06-13 18:57:58 -03001304 uint64_t extent_begin_offset, extent_relative_offset;
Kevin Wolfa844a2b2016-04-25 17:14:48 +02001305 uint64_t cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE;
1306
1307 extent_begin_offset =
1308 (extent->end_sector - extent->sectors) * BDRV_SECTOR_SIZE;
1309 extent_relative_offset = offset - extent_begin_offset;
Eduardo Habkost9be38592016-06-13 18:57:58 -03001310 return extent_relative_offset % cluster_size;
Kevin Wolfa844a2b2016-04-25 17:14:48 +02001311}
1312
Eric Blakec72080b2018-02-13 14:26:58 -06001313static int coroutine_fn vmdk_co_block_status(BlockDriverState *bs,
1314 bool want_zero,
1315 int64_t offset, int64_t bytes,
1316 int64_t *pnum, int64_t *map,
1317 BlockDriverState **file)
bellardea2384d2004-08-01 21:59:26 +00001318{
1319 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3976d32011-07-12 19:56:28 +08001320 int64_t index_in_cluster, n, ret;
Eric Blakec72080b2018-02-13 14:26:58 -06001321 uint64_t cluster_offset;
Fam Zhengb3976d32011-07-12 19:56:28 +08001322 VmdkExtent *extent;
1323
Eric Blakec72080b2018-02-13 14:26:58 -06001324 extent = find_extent(s, offset >> BDRV_SECTOR_BITS, NULL);
Fam Zhengb3976d32011-07-12 19:56:28 +08001325 if (!extent) {
Eric Blakec72080b2018-02-13 14:26:58 -06001326 return -EIO;
Fam Zhengb3976d32011-07-12 19:56:28 +08001327 }
Stefan Hajnoczif8a2e5e2011-11-14 12:44:21 +00001328 qemu_co_mutex_lock(&s->lock);
Eric Blakec72080b2018-02-13 14:26:58 -06001329 ret = get_cluster_offset(bs, extent, NULL, offset, false, &cluster_offset,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001330 0, 0);
Stefan Hajnoczif8a2e5e2011-11-14 12:44:21 +00001331 qemu_co_mutex_unlock(&s->lock);
Fam Zheng14ead642013-05-02 10:25:23 +08001332
Eric Blakec72080b2018-02-13 14:26:58 -06001333 index_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02001334 switch (ret) {
1335 case VMDK_ERROR:
1336 ret = -EIO;
1337 break;
1338 case VMDK_UNALLOC:
1339 ret = 0;
1340 break;
1341 case VMDK_ZEROED:
1342 ret = BDRV_BLOCK_ZERO;
1343 break;
1344 case VMDK_OK:
1345 ret = BDRV_BLOCK_DATA;
Fam Zhenge0f100f2016-01-26 11:58:59 +08001346 if (!extent->compressed) {
Fam Zhengd0a18f12016-01-26 11:58:58 +08001347 ret |= BDRV_BLOCK_OFFSET_VALID;
Eric Blakec72080b2018-02-13 14:26:58 -06001348 *map = cluster_offset + index_in_cluster;
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02001349 }
Fam Zhenge0f100f2016-01-26 11:58:59 +08001350 *file = extent->file->bs;
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02001351 break;
1352 }
Fam Zheng91b85bd2011-07-12 19:56:35 +08001353
Eric Blakec72080b2018-02-13 14:26:58 -06001354 n = extent->cluster_sectors * BDRV_SECTOR_SIZE - index_in_cluster;
1355 *pnum = MIN(n, bytes);
Fam Zhengb3976d32011-07-12 19:56:28 +08001356 return ret;
bellardea2384d2004-08-01 21:59:26 +00001357}
1358
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001359static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001360 int64_t offset_in_cluster, QEMUIOVector *qiov,
1361 uint64_t qiov_offset, uint64_t n_bytes,
1362 uint64_t offset)
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001363{
1364 int ret;
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001365 VmdkGrainMarker *data = NULL;
1366 uLongf buf_len;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001367 QEMUIOVector local_qiov;
1368 struct iovec iov;
Fam Zheng5e82a312015-05-06 20:23:46 +08001369 int64_t write_offset;
1370 int64_t write_end_sector;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001371
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001372 if (extent->compressed) {
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001373 void *compressed_data;
1374
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001375 if (!extent->has_marker) {
1376 ret = -EINVAL;
1377 goto out;
1378 }
1379 buf_len = (extent->cluster_sectors << 9) * 2;
1380 data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001381
1382 compressed_data = g_malloc(n_bytes);
1383 qemu_iovec_to_buf(qiov, qiov_offset, compressed_data, n_bytes);
1384 ret = compress(data->data, &buf_len, compressed_data, n_bytes);
1385 g_free(compressed_data);
1386
1387 if (ret != Z_OK || buf_len == 0) {
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001388 ret = -EINVAL;
1389 goto out;
1390 }
Fam Zheng5e82a312015-05-06 20:23:46 +08001391
QingFeng Hao4545d4f2016-12-16 06:20:40 +01001392 data->lba = cpu_to_le64(offset >> BDRV_SECTOR_BITS);
1393 data->size = cpu_to_le32(buf_len);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001394
1395 n_bytes = buf_len + sizeof(VmdkGrainMarker);
1396 iov = (struct iovec) {
1397 .iov_base = data,
1398 .iov_len = n_bytes,
1399 };
1400 qemu_iovec_init_external(&local_qiov, &iov, 1);
Max Reitz23c4b2a2017-11-23 03:08:19 +01001401
1402 BLKDBG_EVENT(extent->file, BLKDBG_WRITE_COMPRESSED);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001403 } else {
1404 qemu_iovec_init(&local_qiov, qiov->niov);
1405 qemu_iovec_concat(&local_qiov, qiov, qiov_offset, n_bytes);
Max Reitz23c4b2a2017-11-23 03:08:19 +01001406
1407 BLKDBG_EVENT(extent->file, BLKDBG_WRITE_AIO);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001408 }
1409
Max Reitz3c363572017-11-23 03:08:16 +01001410 write_offset = cluster_offset + offset_in_cluster;
Kevin Wolfa03ef882016-06-20 21:31:46 +02001411 ret = bdrv_co_pwritev(extent->file, write_offset, n_bytes,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001412 &local_qiov, 0);
1413
1414 write_end_sector = DIV_ROUND_UP(write_offset + n_bytes, BDRV_SECTOR_SIZE);
Fam Zheng5e82a312015-05-06 20:23:46 +08001415
Radoslav Gerganov3efffc32015-09-10 10:53:14 +03001416 if (extent->compressed) {
1417 extent->next_cluster_sector = write_end_sector;
1418 } else {
1419 extent->next_cluster_sector = MAX(extent->next_cluster_sector,
1420 write_end_sector);
1421 }
Fam Zheng5e82a312015-05-06 20:23:46 +08001422
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001423 if (ret < 0) {
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001424 goto out;
1425 }
1426 ret = 0;
1427 out:
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001428 g_free(data);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001429 if (!extent->compressed) {
1430 qemu_iovec_destroy(&local_qiov);
1431 }
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001432 return ret;
1433}
1434
1435static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
Kevin Wolff10cc242016-04-25 17:34:41 +02001436 int64_t offset_in_cluster, QEMUIOVector *qiov,
1437 int bytes)
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001438{
1439 int ret;
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001440 int cluster_bytes, buf_bytes;
1441 uint8_t *cluster_buf, *compressed_data;
1442 uint8_t *uncomp_buf;
1443 uint32_t data_len;
1444 VmdkGrainMarker *marker;
1445 uLongf buf_len;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001446
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001447
1448 if (!extent->compressed) {
Max Reitz23c4b2a2017-11-23 03:08:19 +01001449 BLKDBG_EVENT(extent->file, BLKDBG_READ_AIO);
Kevin Wolfa03ef882016-06-20 21:31:46 +02001450 ret = bdrv_co_preadv(extent->file,
Kevin Wolff10cc242016-04-25 17:34:41 +02001451 cluster_offset + offset_in_cluster, bytes,
1452 qiov, 0);
1453 if (ret < 0) {
1454 return ret;
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001455 }
Kevin Wolff10cc242016-04-25 17:34:41 +02001456 return 0;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001457 }
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001458 cluster_bytes = extent->cluster_sectors * 512;
1459 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1460 buf_bytes = cluster_bytes * 2;
1461 cluster_buf = g_malloc(buf_bytes);
1462 uncomp_buf = g_malloc(cluster_bytes);
Max Reitz23c4b2a2017-11-23 03:08:19 +01001463 BLKDBG_EVENT(extent->file, BLKDBG_READ_COMPRESSED);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001464 ret = bdrv_pread(extent->file,
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001465 cluster_offset,
1466 cluster_buf, buf_bytes);
1467 if (ret < 0) {
1468 goto out;
1469 }
1470 compressed_data = cluster_buf;
1471 buf_len = cluster_bytes;
1472 data_len = cluster_bytes;
1473 if (extent->has_marker) {
1474 marker = (VmdkGrainMarker *)cluster_buf;
1475 compressed_data = marker->data;
1476 data_len = le32_to_cpu(marker->size);
1477 }
1478 if (!data_len || data_len > buf_bytes) {
1479 ret = -EINVAL;
1480 goto out;
1481 }
1482 ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1483 if (ret != Z_OK) {
1484 ret = -EINVAL;
1485 goto out;
1486
1487 }
1488 if (offset_in_cluster < 0 ||
Kevin Wolff10cc242016-04-25 17:34:41 +02001489 offset_in_cluster + bytes > buf_len) {
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001490 ret = -EINVAL;
1491 goto out;
1492 }
Kevin Wolff10cc242016-04-25 17:34:41 +02001493 qemu_iovec_from_buf(qiov, 0, uncomp_buf + offset_in_cluster, bytes);
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001494 ret = 0;
1495
1496 out:
1497 g_free(uncomp_buf);
1498 g_free(cluster_buf);
1499 return ret;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001500}
1501
Kevin Wolff10cc242016-04-25 17:34:41 +02001502static int coroutine_fn
1503vmdk_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1504 QEMUIOVector *qiov, int flags)
bellardea2384d2004-08-01 21:59:26 +00001505{
1506 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3976d32011-07-12 19:56:28 +08001507 int ret;
Kevin Wolff10cc242016-04-25 17:34:41 +02001508 uint64_t n_bytes, offset_in_cluster;
Fam Zhengb3976d32011-07-12 19:56:28 +08001509 VmdkExtent *extent = NULL;
Kevin Wolff10cc242016-04-25 17:34:41 +02001510 QEMUIOVector local_qiov;
bellardea2384d2004-08-01 21:59:26 +00001511 uint64_t cluster_offset;
Kevin Wolff10cc242016-04-25 17:34:41 +02001512 uint64_t bytes_done = 0;
ths5f4da8c2007-01-24 21:05:24 +00001513
Kevin Wolff10cc242016-04-25 17:34:41 +02001514 qemu_iovec_init(&local_qiov, qiov->niov);
1515 qemu_co_mutex_lock(&s->lock);
1516
1517 while (bytes > 0) {
1518 extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent);
Fam Zhengb3976d32011-07-12 19:56:28 +08001519 if (!extent) {
Kevin Wolff10cc242016-04-25 17:34:41 +02001520 ret = -EIO;
1521 goto fail;
Fam Zhengb3976d32011-07-12 19:56:28 +08001522 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001523 ret = get_cluster_offset(bs, extent, NULL,
Kevin Wolff10cc242016-04-25 17:34:41 +02001524 offset, false, &cluster_offset, 0, 0);
1525 offset_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
1526
1527 n_bytes = MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE
1528 - offset_in_cluster);
1529
Fam Zheng14ead642013-05-02 10:25:23 +08001530 if (ret != VMDK_OK) {
Fam Zheng91b85bd2011-07-12 19:56:35 +08001531 /* if not allocated, try to read from parent image, if exist */
Kevin Wolf760e0062015-06-17 14:55:21 +02001532 if (bs->backing && ret != VMDK_ZEROED) {
Fam Zhengae261c82011-07-12 19:56:38 +08001533 if (!vmdk_is_cid_valid(bs)) {
Kevin Wolff10cc242016-04-25 17:34:41 +02001534 ret = -EINVAL;
1535 goto fail;
Fam Zhengae261c82011-07-12 19:56:38 +08001536 }
Kevin Wolff10cc242016-04-25 17:34:41 +02001537
1538 qemu_iovec_reset(&local_qiov);
1539 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
1540
Max Reitz23c4b2a2017-11-23 03:08:19 +01001541 /* qcow2 emits this on bs->file instead of bs->backing */
1542 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
Kevin Wolfa03ef882016-06-20 21:31:46 +02001543 ret = bdrv_co_preadv(bs->backing, offset, n_bytes,
Kevin Wolff10cc242016-04-25 17:34:41 +02001544 &local_qiov, 0);
Fam Zhengae261c82011-07-12 19:56:38 +08001545 if (ret < 0) {
Kevin Wolff10cc242016-04-25 17:34:41 +02001546 goto fail;
Fam Zhengae261c82011-07-12 19:56:38 +08001547 }
ths5f4da8c2007-01-24 21:05:24 +00001548 } else {
Kevin Wolff10cc242016-04-25 17:34:41 +02001549 qemu_iovec_memset(qiov, bytes_done, 0, n_bytes);
ths5f4da8c2007-01-24 21:05:24 +00001550 }
bellardea2384d2004-08-01 21:59:26 +00001551 } else {
Kevin Wolff10cc242016-04-25 17:34:41 +02001552 qemu_iovec_reset(&local_qiov);
1553 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
1554
1555 ret = vmdk_read_extent(extent, cluster_offset, offset_in_cluster,
1556 &local_qiov, n_bytes);
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001557 if (ret) {
Kevin Wolff10cc242016-04-25 17:34:41 +02001558 goto fail;
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001559 }
bellardea2384d2004-08-01 21:59:26 +00001560 }
Kevin Wolff10cc242016-04-25 17:34:41 +02001561 bytes -= n_bytes;
1562 offset += n_bytes;
1563 bytes_done += n_bytes;
bellardea2384d2004-08-01 21:59:26 +00001564 }
bellardea2384d2004-08-01 21:59:26 +00001565
Kevin Wolff10cc242016-04-25 17:34:41 +02001566 ret = 0;
1567fail:
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001568 qemu_co_mutex_unlock(&s->lock);
Kevin Wolff10cc242016-04-25 17:34:41 +02001569 qemu_iovec_destroy(&local_qiov);
1570
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001571 return ret;
1572}
1573
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001574/**
1575 * vmdk_write:
1576 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
Fam Zheng8e507242013-08-01 18:12:17 +08001577 * if possible, otherwise return -ENOTSUP.
1578 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1579 * with each cluster. By dry run we can find if the zero write
1580 * is possible without modifying image data.
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001581 *
1582 * Returns: error code with 0 for success.
1583 */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001584static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset,
1585 uint64_t bytes, QEMUIOVector *qiov,
1586 bool zeroed, bool zero_dry_run)
bellardea2384d2004-08-01 21:59:26 +00001587{
bellardff1afc72005-04-26 21:08:00 +00001588 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3976d32011-07-12 19:56:28 +08001589 VmdkExtent *extent = NULL;
Fam Zheng585ea0c2014-01-08 09:42:07 +08001590 int ret;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001591 int64_t offset_in_cluster, n_bytes;
bellardff1afc72005-04-26 21:08:00 +00001592 uint64_t cluster_offset;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001593 uint64_t bytes_done = 0;
Fam Zhengb3976d32011-07-12 19:56:28 +08001594 VmdkMetaData m_data;
bellardff1afc72005-04-26 21:08:00 +00001595
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001596 if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) {
1597 error_report("Wrong offset: offset=0x%" PRIx64
Markus Armbruster9af9e0f2015-12-18 16:35:19 +01001598 " total_sectors=0x%" PRIx64,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001599 offset, bs->total_sectors);
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001600 return -EIO;
ths630530a2007-06-18 15:01:30 +00001601 }
1602
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001603 while (bytes > 0) {
1604 extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent);
Fam Zhengb3976d32011-07-12 19:56:28 +08001605 if (!extent) {
1606 return -EIO;
1607 }
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001608 offset_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
1609 n_bytes = MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE
1610 - offset_in_cluster);
1611
1612 ret = get_cluster_offset(bs, extent, &m_data, offset,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001613 !(extent->compressed || zeroed),
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001614 &cluster_offset, offset_in_cluster,
1615 offset_in_cluster + n_bytes);
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001616 if (extent->compressed) {
Fam Zheng65f74722013-05-02 10:25:22 +08001617 if (ret == VMDK_OK) {
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001618 /* Refuse write to allocated cluster for streamOptimized */
Fam Zheng48239702013-10-11 15:43:22 +08001619 error_report("Could not write to allocated cluster"
1620 " for streamOptimized");
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001621 return -EIO;
1622 } else {
1623 /* allocate */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001624 ret = get_cluster_offset(bs, extent, &m_data, offset,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001625 true, &cluster_offset, 0, 0);
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001626 }
1627 }
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001628 if (ret == VMDK_ERROR) {
Fam Zheng91b85bd2011-07-12 19:56:35 +08001629 return -EINVAL;
Fam Zhengb3976d32011-07-12 19:56:28 +08001630 }
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001631 if (zeroed) {
1632 /* Do zeroed write, buf is ignored */
1633 if (extent->has_zero_grain &&
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001634 offset_in_cluster == 0 &&
1635 n_bytes >= extent->cluster_sectors * BDRV_SECTOR_SIZE) {
1636 n_bytes = extent->cluster_sectors * BDRV_SECTOR_SIZE;
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001637 if (!zero_dry_run) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001638 /* update L2 tables */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001639 if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED)
1640 != VMDK_OK) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001641 return -EIO;
1642 }
1643 }
1644 } else {
1645 return -ENOTSUP;
1646 }
1647 } else {
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001648 ret = vmdk_write_extent(extent, cluster_offset, offset_in_cluster,
1649 qiov, bytes_done, n_bytes, offset);
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001650 if (ret) {
1651 return ret;
1652 }
1653 if (m_data.valid) {
1654 /* update L2 tables */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001655 if (vmdk_L2update(extent, &m_data,
1656 cluster_offset >> BDRV_SECTOR_BITS)
1657 != VMDK_OK) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001658 return -EIO;
1659 }
Fam Zhengb3976d32011-07-12 19:56:28 +08001660 }
ths630530a2007-06-18 15:01:30 +00001661 }
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001662 bytes -= n_bytes;
1663 offset += n_bytes;
1664 bytes_done += n_bytes;
ths5f4da8c2007-01-24 21:05:24 +00001665
Fam Zhengae261c82011-07-12 19:56:38 +08001666 /* update CID on the first write every time the virtual disk is
1667 * opened */
Fam Zheng69b4d862011-07-12 19:56:34 +08001668 if (!s->cid_updated) {
Fam Zhenge5dc64b2014-12-04 07:28:29 +08001669 ret = vmdk_write_cid(bs, g_random_int());
Kevin Wolf99f18352011-10-26 12:25:25 +02001670 if (ret < 0) {
1671 return ret;
1672 }
Fam Zheng69b4d862011-07-12 19:56:34 +08001673 s->cid_updated = true;
ths5f4da8c2007-01-24 21:05:24 +00001674 }
bellardff1afc72005-04-26 21:08:00 +00001675 }
1676 return 0;
bellardea2384d2004-08-01 21:59:26 +00001677}
1678
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001679static int coroutine_fn
1680vmdk_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1681 QEMUIOVector *qiov, int flags)
Paolo Bonzinie183ef72011-10-20 13:16:23 +02001682{
1683 int ret;
1684 BDRVVmdkState *s = bs->opaque;
1685 qemu_co_mutex_lock(&s->lock);
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001686 ret = vmdk_pwritev(bs, offset, bytes, qiov, false, false);
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001687 qemu_co_mutex_unlock(&s->lock);
1688 return ret;
1689}
1690
Pavel Butsykinb2c622d2016-07-22 11:17:45 +03001691static int coroutine_fn
1692vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
1693 uint64_t bytes, QEMUIOVector *qiov)
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001694{
Pavel Butsykinb2c622d2016-07-22 11:17:45 +03001695 return vmdk_co_pwritev(bs, offset, bytes, qiov, 0);
Fam Zhengba0ad892014-05-06 21:08:44 +08001696}
1697
Eric Blakea620f2a2016-06-01 15:10:12 -06001698static int coroutine_fn vmdk_co_pwrite_zeroes(BlockDriverState *bs,
1699 int64_t offset,
1700 int bytes,
1701 BdrvRequestFlags flags)
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001702{
1703 int ret;
1704 BDRVVmdkState *s = bs->opaque;
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001705
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001706 qemu_co_mutex_lock(&s->lock);
Fam Zheng8e507242013-08-01 18:12:17 +08001707 /* write zeroes could fail if sectors not aligned to cluster, test it with
1708 * dry_run == true before really updating image */
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001709 ret = vmdk_pwritev(bs, offset, bytes, NULL, true, true);
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001710 if (!ret) {
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02001711 ret = vmdk_pwritev(bs, offset, bytes, NULL, true, false);
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001712 }
Paolo Bonzinie183ef72011-10-20 13:16:23 +02001713 qemu_co_mutex_unlock(&s->lock);
1714 return ret;
1715}
1716
Fam Zheng6c031aa2011-08-12 23:19:32 +08001717static int vmdk_create_extent(const char *filename, int64_t filesize,
Fam Zheng917703c2013-12-20 09:48:48 +08001718 bool flat, bool compress, bool zeroed_grain,
Chunyan Liu4ab15592014-06-30 14:29:58 +08001719 QemuOpts *opts, Error **errp)
bellard8979b222005-07-02 14:02:54 +00001720{
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001721 int ret, i;
Kevin Wolfc4bea162016-03-08 15:57:05 +01001722 BlockBackend *blk = NULL;
bellard8979b222005-07-02 14:02:54 +00001723 VMDK4Header header;
Fam Zhengc13959c2014-05-28 11:38:58 +08001724 Error *local_err = NULL;
Fam Zheng917703c2013-12-20 09:48:48 +08001725 uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
1726 uint32_t *gd_buf = NULL;
1727 int gd_buf_size;
Kevin Wolf0e7e1982009-05-18 16:42:10 +02001728
Chunyan Liu4ab15592014-06-30 14:29:58 +08001729 ret = bdrv_create_file(filename, opts, &local_err);
Fam Zheng917703c2013-12-20 09:48:48 +08001730 if (ret < 0) {
1731 error_propagate(errp, local_err);
1732 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001733 }
Fam Zheng917703c2013-12-20 09:48:48 +08001734
Max Reitzefaa7c42016-03-16 19:54:38 +01001735 blk = blk_new_open(filename, NULL, NULL,
Kevin Wolf55880602017-02-17 15:07:38 +01001736 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
1737 &local_err);
Kevin Wolfc4bea162016-03-08 15:57:05 +01001738 if (blk == NULL) {
Fam Zheng917703c2013-12-20 09:48:48 +08001739 error_propagate(errp, local_err);
Kevin Wolfc4bea162016-03-08 15:57:05 +01001740 ret = -EIO;
Fam Zheng917703c2013-12-20 09:48:48 +08001741 goto exit;
1742 }
1743
Kevin Wolfc4bea162016-03-08 15:57:05 +01001744 blk_set_allow_write_beyond_eof(blk, true);
1745
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001746 if (flat) {
Max Reitz3a691c52017-06-13 22:20:54 +02001747 ret = blk_truncate(blk, filesize, PREALLOC_MODE_OFF, errp);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001748 goto exit;
1749 }
bellard8979b222005-07-02 14:02:54 +00001750 magic = cpu_to_be32(VMDK4_MAGIC);
1751 memset(&header, 0, sizeof(header));
Fam Zhengd62d9dc2015-09-17 13:04:10 +08001752 if (compress) {
1753 header.version = 3;
1754 } else if (zeroed_grain) {
1755 header.version = 2;
1756 } else {
1757 header.version = 1;
1758 }
Fam Zheng95b0aa42013-05-02 10:25:25 +08001759 header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
Fam Zheng69e0b6d2013-05-02 10:25:24 +08001760 | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
1761 | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
Fam Zheng6c031aa2011-08-12 23:19:32 +08001762 header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
Fam Zheng917703c2013-12-20 09:48:48 +08001763 header.capacity = filesize / BDRV_SECTOR_SIZE;
Alexander Graf16372ff2011-05-25 00:46:55 +02001764 header.granularity = 128;
Fam Zheng917703c2013-12-20 09:48:48 +08001765 header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
bellard8979b222005-07-02 14:02:54 +00001766
Fam Zheng917703c2013-12-20 09:48:48 +08001767 grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
1768 gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
1769 BDRV_SECTOR_SIZE);
1770 gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
1771 gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
bellard8979b222005-07-02 14:02:54 +00001772
1773 header.desc_offset = 1;
1774 header.desc_size = 20;
1775 header.rgd_offset = header.desc_offset + header.desc_size;
Fam Zheng917703c2013-12-20 09:48:48 +08001776 header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
bellard8979b222005-07-02 14:02:54 +00001777 header.grain_offset =
Fam Zheng917703c2013-12-20 09:48:48 +08001778 ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
1779 header.granularity);
Alexander Graf16372ff2011-05-25 00:46:55 +02001780 /* swap endianness for all header fields */
1781 header.version = cpu_to_le32(header.version);
1782 header.flags = cpu_to_le32(header.flags);
1783 header.capacity = cpu_to_le64(header.capacity);
1784 header.granularity = cpu_to_le64(header.granularity);
Fam Zhengca8804c2013-08-06 15:44:55 +08001785 header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
bellard8979b222005-07-02 14:02:54 +00001786 header.desc_offset = cpu_to_le64(header.desc_offset);
1787 header.desc_size = cpu_to_le64(header.desc_size);
1788 header.rgd_offset = cpu_to_le64(header.rgd_offset);
1789 header.gd_offset = cpu_to_le64(header.gd_offset);
1790 header.grain_offset = cpu_to_le64(header.grain_offset);
Fam Zheng6c031aa2011-08-12 23:19:32 +08001791 header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
bellard8979b222005-07-02 14:02:54 +00001792
1793 header.check_bytes[0] = 0xa;
1794 header.check_bytes[1] = 0x20;
1795 header.check_bytes[2] = 0xd;
1796 header.check_bytes[3] = 0xa;
ths3b46e622007-09-17 08:09:54 +00001797
1798 /* write all the data */
Eric Blake8341f002016-05-06 10:26:27 -06001799 ret = blk_pwrite(blk, 0, &magic, sizeof(magic), 0);
Fam Zheng917703c2013-12-20 09:48:48 +08001800 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001801 error_setg(errp, QERR_IO_ERROR);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001802 goto exit;
1803 }
Eric Blake8341f002016-05-06 10:26:27 -06001804 ret = blk_pwrite(blk, sizeof(magic), &header, sizeof(header), 0);
Fam Zheng917703c2013-12-20 09:48:48 +08001805 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001806 error_setg(errp, QERR_IO_ERROR);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001807 goto exit;
1808 }
bellard8979b222005-07-02 14:02:54 +00001809
Max Reitz3a691c52017-06-13 22:20:54 +02001810 ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9,
1811 PREALLOC_MODE_OFF, errp);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001812 if (ret < 0) {
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001813 goto exit;
1814 }
bellard8979b222005-07-02 14:02:54 +00001815
1816 /* write grain directory */
Fam Zheng917703c2013-12-20 09:48:48 +08001817 gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
1818 gd_buf = g_malloc0(gd_buf_size);
1819 for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001820 i < gt_count; i++, tmp += gt_size) {
Fam Zheng917703c2013-12-20 09:48:48 +08001821 gd_buf[i] = cpu_to_le32(tmp);
1822 }
Kevin Wolfc4bea162016-03-08 15:57:05 +01001823 ret = blk_pwrite(blk, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
Eric Blake8341f002016-05-06 10:26:27 -06001824 gd_buf, gd_buf_size, 0);
Fam Zheng917703c2013-12-20 09:48:48 +08001825 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001826 error_setg(errp, QERR_IO_ERROR);
Fam Zheng917703c2013-12-20 09:48:48 +08001827 goto exit;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001828 }
ths3b46e622007-09-17 08:09:54 +00001829
bellard8979b222005-07-02 14:02:54 +00001830 /* write backup grain directory */
Fam Zheng917703c2013-12-20 09:48:48 +08001831 for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001832 i < gt_count; i++, tmp += gt_size) {
Fam Zheng917703c2013-12-20 09:48:48 +08001833 gd_buf[i] = cpu_to_le32(tmp);
1834 }
Kevin Wolfc4bea162016-03-08 15:57:05 +01001835 ret = blk_pwrite(blk, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
Eric Blake8341f002016-05-06 10:26:27 -06001836 gd_buf, gd_buf_size, 0);
Fam Zheng917703c2013-12-20 09:48:48 +08001837 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001838 error_setg(errp, QERR_IO_ERROR);
Fam Zheng917703c2013-12-20 09:48:48 +08001839 goto exit;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001840 }
bellard8979b222005-07-02 14:02:54 +00001841
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001842 ret = 0;
Fam Zheng917703c2013-12-20 09:48:48 +08001843exit:
Kevin Wolfc4bea162016-03-08 15:57:05 +01001844 if (blk) {
1845 blk_unref(blk);
Fam Zheng917703c2013-12-20 09:48:48 +08001846 }
1847 g_free(gd_buf);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001848 return ret;
1849}
bellard8979b222005-07-02 14:02:54 +00001850
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001851static int filename_decompose(const char *filename, char *path, char *prefix,
Fam Zheng48239702013-10-11 15:43:22 +08001852 char *postfix, size_t buf_len, Error **errp)
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001853{
1854 const char *p, *q;
1855
1856 if (filename == NULL || !strlen(filename)) {
Fam Zheng48239702013-10-11 15:43:22 +08001857 error_setg(errp, "No filename provided");
Fam Zheng65f74722013-05-02 10:25:22 +08001858 return VMDK_ERROR;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001859 }
1860 p = strrchr(filename, '/');
1861 if (p == NULL) {
1862 p = strrchr(filename, '\\');
1863 }
1864 if (p == NULL) {
1865 p = strrchr(filename, ':');
1866 }
1867 if (p != NULL) {
1868 p++;
1869 if (p - filename >= buf_len) {
Fam Zheng65f74722013-05-02 10:25:22 +08001870 return VMDK_ERROR;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001871 }
1872 pstrcpy(path, p - filename + 1, filename);
1873 } else {
1874 p = filename;
1875 path[0] = '\0';
1876 }
1877 q = strrchr(p, '.');
1878 if (q == NULL) {
1879 pstrcpy(prefix, buf_len, p);
1880 postfix[0] = '\0';
1881 } else {
1882 if (q - p >= buf_len) {
Fam Zheng65f74722013-05-02 10:25:22 +08001883 return VMDK_ERROR;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001884 }
1885 pstrcpy(prefix, q - p + 1, p);
1886 pstrcpy(postfix, buf_len, q);
1887 }
Fam Zheng65f74722013-05-02 10:25:22 +08001888 return VMDK_OK;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001889}
1890
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01001891static int coroutine_fn vmdk_co_create_opts(const char *filename, QemuOpts *opts,
1892 Error **errp)
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001893{
Fam Zheng917703c2013-12-20 09:48:48 +08001894 int idx = 0;
Kevin Wolfc4bea162016-03-08 15:57:05 +01001895 BlockBackend *new_blk = NULL;
Fam Zhengc13959c2014-05-28 11:38:58 +08001896 Error *local_err = NULL;
Fam Zhengaf057fe2013-12-03 10:41:05 +08001897 char *desc = NULL;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001898 int64_t total_size = 0, filesize;
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001899 char *adapter_type = NULL;
1900 char *backing_file = NULL;
Janne Karhunenf2499242016-05-03 02:43:30 -07001901 char *hw_version = NULL;
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001902 char *fmt = NULL;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001903 int ret = 0;
Fam Zheng6c031aa2011-08-12 23:19:32 +08001904 bool flat, split, compress;
Fam Zhengaf057fe2013-12-03 10:41:05 +08001905 GString *ext_desc_lines;
Jeff Codyfe206562015-01-22 08:03:26 -05001906 char *path = g_malloc0(PATH_MAX);
1907 char *prefix = g_malloc0(PATH_MAX);
1908 char *postfix = g_malloc0(PATH_MAX);
1909 char *desc_line = g_malloc0(BUF_SIZE);
1910 char *ext_filename = g_malloc0(PATH_MAX);
1911 char *desc_filename = g_malloc0(PATH_MAX);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001912 const int64_t split_size = 0x80000000; /* VMDK has constant split size */
1913 const char *desc_extent_line;
Jeff Codyfe206562015-01-22 08:03:26 -05001914 char *parent_desc_line = g_malloc0(BUF_SIZE);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001915 uint32_t parent_cid = 0xffffffff;
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001916 uint32_t number_heads = 16;
Fam Zheng69e0b6d2013-05-02 10:25:24 +08001917 bool zeroed_grain = false;
Fam Zheng917703c2013-12-20 09:48:48 +08001918 uint32_t desc_offset = 0, desc_len;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001919 const char desc_template[] =
1920 "# Disk DescriptorFile\n"
1921 "version=1\n"
Fam Zheng9b170312014-04-17 18:43:53 +08001922 "CID=%" PRIx32 "\n"
1923 "parentCID=%" PRIx32 "\n"
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001924 "createType=\"%s\"\n"
1925 "%s"
1926 "\n"
1927 "# Extent description\n"
1928 "%s"
1929 "\n"
1930 "# The Disk Data Base\n"
1931 "#DDB\n"
1932 "\n"
Janne Karhunenf2499242016-05-03 02:43:30 -07001933 "ddb.virtualHWVersion = \"%s\"\n"
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001934 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
Fam Zheng4ab9dab2014-04-17 11:34:37 +08001935 "ddb.geometry.heads = \"%" PRIu32 "\"\n"
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001936 "ddb.geometry.sectors = \"63\"\n"
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001937 "ddb.adapterType = \"%s\"\n";
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001938
Fam Zhengaf057fe2013-12-03 10:41:05 +08001939 ext_desc_lines = g_string_new(NULL);
1940
Fam Zheng48239702013-10-11 15:43:22 +08001941 if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {
Fam Zhengaf057fe2013-12-03 10:41:05 +08001942 ret = -EINVAL;
1943 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001944 }
1945 /* Read out options */
Hu Taoc2eb9182014-09-10 17:05:45 +08001946 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1947 BDRV_SECTOR_SIZE);
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001948 adapter_type = qemu_opt_get_del(opts, BLOCK_OPT_ADAPTER_TYPE);
1949 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
Janne Karhunenf2499242016-05-03 02:43:30 -07001950 hw_version = qemu_opt_get_del(opts, BLOCK_OPT_HWVERSION);
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001951 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false)) {
Janne Karhunenf2499242016-05-03 02:43:30 -07001952 if (strcmp(hw_version, "undefined")) {
1953 error_setg(errp,
1954 "compat6 cannot be enabled with hwversion set");
1955 ret = -EINVAL;
1956 goto exit;
1957 }
1958 g_free(hw_version);
1959 hw_version = g_strdup("6");
1960 }
1961 if (strcmp(hw_version, "undefined") == 0) {
1962 g_free(hw_version);
1963 hw_version = g_strdup("4");
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001964 }
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001965 fmt = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT);
1966 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ZEROED_GRAIN, false)) {
1967 zeroed_grain = true;
1968 }
1969
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001970 if (!adapter_type) {
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001971 adapter_type = g_strdup("ide");
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001972 } else if (strcmp(adapter_type, "ide") &&
1973 strcmp(adapter_type, "buslogic") &&
1974 strcmp(adapter_type, "lsilogic") &&
1975 strcmp(adapter_type, "legacyESX")) {
Fam Zheng48239702013-10-11 15:43:22 +08001976 error_setg(errp, "Unknown adapter type: '%s'", adapter_type);
Fam Zhengaf057fe2013-12-03 10:41:05 +08001977 ret = -EINVAL;
1978 goto exit;
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001979 }
1980 if (strcmp(adapter_type, "ide") != 0) {
1981 /* that's the number of heads with which vmware operates when
1982 creating, exporting, etc. vmdk files with a non-ide adapter type */
1983 number_heads = 255;
1984 }
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001985 if (!fmt) {
1986 /* Default format to monolithicSparse */
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001987 fmt = g_strdup("monolithicSparse");
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001988 } else if (strcmp(fmt, "monolithicFlat") &&
1989 strcmp(fmt, "monolithicSparse") &&
1990 strcmp(fmt, "twoGbMaxExtentSparse") &&
Fam Zheng6c031aa2011-08-12 23:19:32 +08001991 strcmp(fmt, "twoGbMaxExtentFlat") &&
1992 strcmp(fmt, "streamOptimized")) {
Fam Zheng48239702013-10-11 15:43:22 +08001993 error_setg(errp, "Unknown subformat: '%s'", fmt);
Fam Zhengaf057fe2013-12-03 10:41:05 +08001994 ret = -EINVAL;
1995 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001996 }
1997 split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
1998 strcmp(fmt, "twoGbMaxExtentSparse"));
1999 flat = !(strcmp(fmt, "monolithicFlat") &&
2000 strcmp(fmt, "twoGbMaxExtentFlat"));
Fam Zheng6c031aa2011-08-12 23:19:32 +08002001 compress = !strcmp(fmt, "streamOptimized");
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002002 if (flat) {
Fam Zheng4ab9dab2014-04-17 11:34:37 +08002003 desc_extent_line = "RW %" PRId64 " FLAT \"%s\" 0\n";
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002004 } else {
Fam Zheng4ab9dab2014-04-17 11:34:37 +08002005 desc_extent_line = "RW %" PRId64 " SPARSE \"%s\"\n";
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002006 }
2007 if (flat && backing_file) {
Fam Zheng48239702013-10-11 15:43:22 +08002008 error_setg(errp, "Flat image can't have backing file");
Fam Zhengaf057fe2013-12-03 10:41:05 +08002009 ret = -ENOTSUP;
2010 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002011 }
Fam Zheng52c8d622013-10-11 15:43:23 +08002012 if (flat && zeroed_grain) {
2013 error_setg(errp, "Flat image can't enable zeroed grain");
Fam Zhengaf057fe2013-12-03 10:41:05 +08002014 ret = -ENOTSUP;
2015 goto exit;
Fam Zheng52c8d622013-10-11 15:43:23 +08002016 }
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002017 if (backing_file) {
Kevin Wolfc4bea162016-03-08 15:57:05 +01002018 BlockBackend *blk;
Max Reitz1085daf2014-11-26 17:20:28 +01002019 char *full_backing = g_new0(char, PATH_MAX);
2020 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
2021 full_backing, PATH_MAX,
2022 &local_err);
2023 if (local_err) {
2024 g_free(full_backing);
2025 error_propagate(errp, local_err);
2026 ret = -ENOENT;
2027 goto exit;
2028 }
Kevin Wolfc4bea162016-03-08 15:57:05 +01002029
Max Reitzefaa7c42016-03-16 19:54:38 +01002030 blk = blk_new_open(full_backing, NULL, NULL,
Kevin Wolf72e775c2016-03-15 14:34:37 +01002031 BDRV_O_NO_BACKING, errp);
Max Reitz1085daf2014-11-26 17:20:28 +01002032 g_free(full_backing);
Kevin Wolfc4bea162016-03-08 15:57:05 +01002033 if (blk == NULL) {
2034 ret = -EIO;
Fam Zhengaf057fe2013-12-03 10:41:05 +08002035 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002036 }
Kevin Wolfc4bea162016-03-08 15:57:05 +01002037 if (strcmp(blk_bs(blk)->drv->format_name, "vmdk")) {
2038 blk_unref(blk);
Fam Zhengaf057fe2013-12-03 10:41:05 +08002039 ret = -EINVAL;
2040 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002041 }
Peter Maydell98778602017-07-09 18:06:14 +01002042 ret = vmdk_read_cid(blk_bs(blk), 0, &parent_cid);
Kevin Wolfc4bea162016-03-08 15:57:05 +01002043 blk_unref(blk);
Peter Maydell98778602017-07-09 18:06:14 +01002044 if (ret) {
2045 goto exit;
2046 }
Jeff Codyfe206562015-01-22 08:03:26 -05002047 snprintf(parent_desc_line, BUF_SIZE,
Fam Zheng8ed610a2013-06-26 17:24:32 +08002048 "parentFileNameHint=\"%s\"", backing_file);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002049 }
2050
2051 /* Create extents */
2052 filesize = total_size;
2053 while (filesize > 0) {
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002054 int64_t size = filesize;
2055
2056 if (split && size > split_size) {
2057 size = split_size;
2058 }
2059 if (split) {
Jeff Codyfe206562015-01-22 08:03:26 -05002060 snprintf(desc_filename, PATH_MAX, "%s-%c%03d%s",
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002061 prefix, flat ? 'f' : 's', ++idx, postfix);
2062 } else if (flat) {
Jeff Codyfe206562015-01-22 08:03:26 -05002063 snprintf(desc_filename, PATH_MAX, "%s-flat%s", prefix, postfix);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002064 } else {
Jeff Codyfe206562015-01-22 08:03:26 -05002065 snprintf(desc_filename, PATH_MAX, "%s%s", prefix, postfix);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002066 }
Jeff Codyfe206562015-01-22 08:03:26 -05002067 snprintf(ext_filename, PATH_MAX, "%s%s", path, desc_filename);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002068
Fam Zheng69e0b6d2013-05-02 10:25:24 +08002069 if (vmdk_create_extent(ext_filename, size,
Chunyan Liu4ab15592014-06-30 14:29:58 +08002070 flat, compress, zeroed_grain, opts, errp)) {
Fam Zhengaf057fe2013-12-03 10:41:05 +08002071 ret = -EINVAL;
2072 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002073 }
2074 filesize -= size;
2075
2076 /* Format description line */
Jeff Codyfe206562015-01-22 08:03:26 -05002077 snprintf(desc_line, BUF_SIZE,
Fam Zheng917703c2013-12-20 09:48:48 +08002078 desc_extent_line, size / BDRV_SECTOR_SIZE, desc_filename);
Fam Zhengaf057fe2013-12-03 10:41:05 +08002079 g_string_append(ext_desc_lines, desc_line);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002080 }
2081 /* generate descriptor file */
Fam Zhengaf057fe2013-12-03 10:41:05 +08002082 desc = g_strdup_printf(desc_template,
Fam Zhenge5dc64b2014-12-04 07:28:29 +08002083 g_random_int(),
Fam Zhengaf057fe2013-12-03 10:41:05 +08002084 parent_cid,
2085 fmt,
2086 parent_desc_line,
2087 ext_desc_lines->str,
Janne Karhunenf2499242016-05-03 02:43:30 -07002088 hw_version,
Fam Zheng917703c2013-12-20 09:48:48 +08002089 total_size /
2090 (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
Fam Zhengaf057fe2013-12-03 10:41:05 +08002091 number_heads,
2092 adapter_type);
Fam Zheng917703c2013-12-20 09:48:48 +08002093 desc_len = strlen(desc);
2094 /* the descriptor offset = 0x200 */
2095 if (!split && !flat) {
2096 desc_offset = 0x200;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002097 } else {
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002098 ret = bdrv_create_file(filename, opts, &local_err);
Fam Zheng917703c2013-12-20 09:48:48 +08002099 if (ret < 0) {
Fam Zhengc13959c2014-05-28 11:38:58 +08002100 error_propagate(errp, local_err);
Fam Zheng917703c2013-12-20 09:48:48 +08002101 goto exit;
2102 }
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002103 }
Kevin Wolfc4bea162016-03-08 15:57:05 +01002104
Max Reitzefaa7c42016-03-16 19:54:38 +01002105 new_blk = blk_new_open(filename, NULL, NULL,
Kevin Wolf55880602017-02-17 15:07:38 +01002106 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
2107 &local_err);
Kevin Wolfc4bea162016-03-08 15:57:05 +01002108 if (new_blk == NULL) {
Fam Zhengc13959c2014-05-28 11:38:58 +08002109 error_propagate(errp, local_err);
Kevin Wolfc4bea162016-03-08 15:57:05 +01002110 ret = -EIO;
Fam Zhengaf057fe2013-12-03 10:41:05 +08002111 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002112 }
Kevin Wolfc4bea162016-03-08 15:57:05 +01002113
2114 blk_set_allow_write_beyond_eof(new_blk, true);
2115
Eric Blake8341f002016-05-06 10:26:27 -06002116 ret = blk_pwrite(new_blk, desc_offset, desc, desc_len, 0);
Fam Zheng917703c2013-12-20 09:48:48 +08002117 if (ret < 0) {
2118 error_setg_errno(errp, -ret, "Could not write description");
2119 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08002120 }
Fam Zheng917703c2013-12-20 09:48:48 +08002121 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2122 * for description file */
2123 if (desc_offset == 0) {
Max Reitz3a691c52017-06-13 22:20:54 +02002124 ret = blk_truncate(new_blk, desc_len, PREALLOC_MODE_OFF, errp);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002125 }
Fam Zhengaf057fe2013-12-03 10:41:05 +08002126exit:
Kevin Wolfc4bea162016-03-08 15:57:05 +01002127 if (new_blk) {
2128 blk_unref(new_blk);
Fam Zheng917703c2013-12-20 09:48:48 +08002129 }
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002130 g_free(adapter_type);
2131 g_free(backing_file);
Janne Karhunenf2499242016-05-03 02:43:30 -07002132 g_free(hw_version);
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002133 g_free(fmt);
Fam Zhengaf057fe2013-12-03 10:41:05 +08002134 g_free(desc);
Jeff Codyfe206562015-01-22 08:03:26 -05002135 g_free(path);
2136 g_free(prefix);
2137 g_free(postfix);
2138 g_free(desc_line);
2139 g_free(ext_filename);
2140 g_free(desc_filename);
2141 g_free(parent_desc_line);
Fam Zhengaf057fe2013-12-03 10:41:05 +08002142 g_string_free(ext_desc_lines, true);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002143 return ret;
bellard8979b222005-07-02 14:02:54 +00002144}
2145
bellarde2731ad2004-09-18 19:32:11 +00002146static void vmdk_close(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002147{
Kevin Wolf2bc31662011-11-22 16:50:27 +01002148 BDRVVmdkState *s = bs->opaque;
2149
Fam Zhengb3976d32011-07-12 19:56:28 +08002150 vmdk_free_extents(bs);
Fam Zhengf4c129a2013-10-31 10:06:23 +08002151 g_free(s->create_type);
Kevin Wolf2bc31662011-11-22 16:50:27 +01002152
2153 migrate_del_blocker(s->migration_blocker);
2154 error_free(s->migration_blocker);
bellardea2384d2004-08-01 21:59:26 +00002155}
2156
Paolo Bonzini8b94ff82011-10-20 13:16:24 +02002157static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
pbrook7a6cba62006-06-04 11:39:07 +00002158{
Fam Zheng333c5742011-07-12 19:56:33 +08002159 BDRVVmdkState *s = bs->opaque;
Paolo Bonzini29cdb252012-03-12 18:26:01 +01002160 int i, err;
2161 int ret = 0;
Fam Zheng333c5742011-07-12 19:56:33 +08002162
Fam Zheng333c5742011-07-12 19:56:33 +08002163 for (i = 0; i < s->num_extents; i++) {
Kevin Wolf24bc15d2015-06-15 13:50:20 +02002164 err = bdrv_co_flush(s->extents[i].file->bs);
Fam Zheng333c5742011-07-12 19:56:33 +08002165 if (err < 0) {
2166 ret = err;
2167 }
2168 }
2169 return ret;
pbrook7a6cba62006-06-04 11:39:07 +00002170}
2171
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002172static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
2173{
2174 int i;
2175 int64_t ret = 0;
2176 int64_t r;
2177 BDRVVmdkState *s = bs->opaque;
2178
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002179 ret = bdrv_get_allocated_file_size(bs->file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002180 if (ret < 0) {
2181 return ret;
2182 }
2183 for (i = 0; i < s->num_extents; i++) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002184 if (s->extents[i].file == bs->file) {
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002185 continue;
2186 }
Kevin Wolf24bc15d2015-06-15 13:50:20 +02002187 r = bdrv_get_allocated_file_size(s->extents[i].file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002188 if (r < 0) {
2189 return r;
2190 }
2191 ret += r;
2192 }
2193 return ret;
2194}
Kevin Wolf0e7e1982009-05-18 16:42:10 +02002195
Fam Zhengda7a50f2013-07-01 11:33:17 +08002196static int vmdk_has_zero_init(BlockDriverState *bs)
2197{
2198 int i;
2199 BDRVVmdkState *s = bs->opaque;
2200
2201 /* If has a flat extent and its underlying storage doesn't have zero init,
2202 * return 0. */
2203 for (i = 0; i < s->num_extents; i++) {
2204 if (s->extents[i].flat) {
Kevin Wolf24bc15d2015-06-15 13:50:20 +02002205 if (!bdrv_has_zero_init(s->extents[i].file->bs)) {
Fam Zhengda7a50f2013-07-01 11:33:17 +08002206 return 0;
2207 }
2208 }
2209 }
2210 return 1;
2211}
2212
Fam Zhengf4c129a2013-10-31 10:06:23 +08002213static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
2214{
2215 ImageInfo *info = g_new0(ImageInfo, 1);
2216
2217 *info = (ImageInfo){
Kevin Wolf24bc15d2015-06-15 13:50:20 +02002218 .filename = g_strdup(extent->file->bs->filename),
Fam Zhengf4c129a2013-10-31 10:06:23 +08002219 .format = g_strdup(extent->type),
2220 .virtual_size = extent->sectors * BDRV_SECTOR_SIZE,
2221 .compressed = extent->compressed,
2222 .has_compressed = extent->compressed,
2223 .cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE,
2224 .has_cluster_size = !extent->flat,
2225 };
2226
2227 return info;
2228}
2229
Paolo Bonzini2fd61632018-03-01 17:36:19 +01002230static int coroutine_fn vmdk_co_check(BlockDriverState *bs,
2231 BdrvCheckResult *result,
2232 BdrvCheckMode fix)
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002233{
2234 BDRVVmdkState *s = bs->opaque;
2235 VmdkExtent *extent = NULL;
2236 int64_t sector_num = 0;
Markus Armbruster57322b72014-06-26 13:23:22 +02002237 int64_t total_sectors = bdrv_nb_sectors(bs);
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002238 int ret;
2239 uint64_t cluster_offset;
2240
2241 if (fix) {
2242 return -ENOTSUP;
2243 }
2244
2245 for (;;) {
2246 if (sector_num >= total_sectors) {
2247 return 0;
2248 }
2249 extent = find_extent(s, sector_num, extent);
2250 if (!extent) {
2251 fprintf(stderr,
2252 "ERROR: could not find extent for sector %" PRId64 "\n",
2253 sector_num);
Fam Zheng0e51b9b2017-08-04 22:09:42 +08002254 ret = -EINVAL;
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002255 break;
2256 }
2257 ret = get_cluster_offset(bs, extent, NULL,
2258 sector_num << BDRV_SECTOR_BITS,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08002259 false, &cluster_offset, 0, 0);
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002260 if (ret == VMDK_ERROR) {
2261 fprintf(stderr,
2262 "ERROR: could not get cluster_offset for sector %"
2263 PRId64 "\n", sector_num);
2264 break;
2265 }
Fam Zheng0e51b9b2017-08-04 22:09:42 +08002266 if (ret == VMDK_OK) {
2267 int64_t extent_len = bdrv_getlength(extent->file->bs);
2268 if (extent_len < 0) {
2269 fprintf(stderr,
2270 "ERROR: could not get extent file length for sector %"
2271 PRId64 "\n", sector_num);
2272 ret = extent_len;
2273 break;
2274 }
2275 if (cluster_offset >= extent_len) {
2276 fprintf(stderr,
2277 "ERROR: cluster offset for sector %"
2278 PRId64 " points after EOF\n", sector_num);
2279 ret = -EINVAL;
2280 break;
2281 }
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002282 }
2283 sector_num += extent->cluster_sectors;
2284 }
2285
2286 result->corruptions++;
Fam Zheng0e51b9b2017-08-04 22:09:42 +08002287 return ret;
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002288}
2289
Fam Zhengf4c129a2013-10-31 10:06:23 +08002290static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs)
2291{
2292 int i;
2293 BDRVVmdkState *s = bs->opaque;
2294 ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
2295 ImageInfoList **next;
2296
2297 *spec_info = (ImageInfoSpecific){
Eric Blake6a8f9662015-10-26 16:34:54 -06002298 .type = IMAGE_INFO_SPECIFIC_KIND_VMDK,
Eric Blake32bafa82016-03-17 16:48:37 -06002299 .u = {
2300 .vmdk.data = g_new0(ImageInfoSpecificVmdk, 1),
Fam Zhengf4c129a2013-10-31 10:06:23 +08002301 },
2302 };
2303
Eric Blake32bafa82016-03-17 16:48:37 -06002304 *spec_info->u.vmdk.data = (ImageInfoSpecificVmdk) {
Fam Zhengf4c129a2013-10-31 10:06:23 +08002305 .create_type = g_strdup(s->create_type),
2306 .cid = s->cid,
2307 .parent_cid = s->parent_cid,
2308 };
2309
Eric Blake32bafa82016-03-17 16:48:37 -06002310 next = &spec_info->u.vmdk.data->extents;
Fam Zhengf4c129a2013-10-31 10:06:23 +08002311 for (i = 0; i < s->num_extents; i++) {
2312 *next = g_new0(ImageInfoList, 1);
2313 (*next)->value = vmdk_get_extent_info(&s->extents[i]);
2314 (*next)->next = NULL;
2315 next = &(*next)->next;
2316 }
2317
2318 return spec_info;
2319}
2320
Fam Zheng5f583302014-11-14 12:09:21 +08002321static bool vmdk_extents_type_eq(const VmdkExtent *a, const VmdkExtent *b)
2322{
2323 return a->flat == b->flat &&
2324 a->compressed == b->compressed &&
2325 (a->flat || a->cluster_sectors == b->cluster_sectors);
2326}
2327
Fam Zheng74fe1882014-05-06 21:08:45 +08002328static int vmdk_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2329{
2330 int i;
2331 BDRVVmdkState *s = bs->opaque;
2332 assert(s->num_extents);
Fam Zheng5f583302014-11-14 12:09:21 +08002333
2334 /* See if we have multiple extents but they have different cases */
2335 for (i = 1; i < s->num_extents; i++) {
2336 if (!vmdk_extents_type_eq(&s->extents[0], &s->extents[i])) {
2337 return -ENOTSUP;
2338 }
2339 }
Fam Zheng74fe1882014-05-06 21:08:45 +08002340 bdi->needs_compressed_writes = s->extents[0].compressed;
2341 if (!s->extents[0].flat) {
2342 bdi->cluster_size = s->extents[0].cluster_sectors << BDRV_SECTOR_BITS;
2343 }
Fam Zheng74fe1882014-05-06 21:08:45 +08002344 return 0;
2345}
2346
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002347static QemuOptsList vmdk_create_opts = {
2348 .name = "vmdk-create-opts",
2349 .head = QTAILQ_HEAD_INITIALIZER(vmdk_create_opts.head),
2350 .desc = {
2351 {
2352 .name = BLOCK_OPT_SIZE,
2353 .type = QEMU_OPT_SIZE,
2354 .help = "Virtual disk size"
2355 },
2356 {
2357 .name = BLOCK_OPT_ADAPTER_TYPE,
2358 .type = QEMU_OPT_STRING,
2359 .help = "Virtual adapter type, can be one of "
2360 "ide (default), lsilogic, buslogic or legacyESX"
2361 },
2362 {
2363 .name = BLOCK_OPT_BACKING_FILE,
2364 .type = QEMU_OPT_STRING,
2365 .help = "File name of a base image"
2366 },
2367 {
2368 .name = BLOCK_OPT_COMPAT6,
2369 .type = QEMU_OPT_BOOL,
2370 .help = "VMDK version 6 image",
2371 .def_value_str = "off"
2372 },
2373 {
Janne Karhunenf2499242016-05-03 02:43:30 -07002374 .name = BLOCK_OPT_HWVERSION,
2375 .type = QEMU_OPT_STRING,
2376 .help = "VMDK hardware version",
2377 .def_value_str = "undefined"
2378 },
2379 {
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002380 .name = BLOCK_OPT_SUBFMT,
2381 .type = QEMU_OPT_STRING,
2382 .help =
2383 "VMDK flat extent format, can be one of "
2384 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2385 },
2386 {
2387 .name = BLOCK_OPT_ZEROED_GRAIN,
2388 .type = QEMU_OPT_BOOL,
2389 .help = "Enable efficient zero writes "
2390 "using the zeroed-grain GTE feature"
2391 },
2392 { /* end of list */ }
2393 }
Kevin Wolf0e7e1982009-05-18 16:42:10 +02002394};
2395
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002396static BlockDriver bdrv_vmdk = {
Fam Zhengda7a50f2013-07-01 11:33:17 +08002397 .format_name = "vmdk",
2398 .instance_size = sizeof(BDRVVmdkState),
2399 .bdrv_probe = vmdk_probe,
2400 .bdrv_open = vmdk_open,
Paolo Bonzini2fd61632018-03-01 17:36:19 +01002401 .bdrv_co_check = vmdk_co_check,
Fam Zhengda7a50f2013-07-01 11:33:17 +08002402 .bdrv_reopen_prepare = vmdk_reopen_prepare,
Kevin Wolf862f2152016-12-19 16:36:02 +01002403 .bdrv_child_perm = bdrv_format_default_perms,
Kevin Wolff10cc242016-04-25 17:34:41 +02002404 .bdrv_co_preadv = vmdk_co_preadv,
Kevin Wolf37b1d7d2016-04-26 13:39:11 +02002405 .bdrv_co_pwritev = vmdk_co_pwritev,
Pavel Butsykinb2c622d2016-07-22 11:17:45 +03002406 .bdrv_co_pwritev_compressed = vmdk_co_pwritev_compressed,
Eric Blakea620f2a2016-06-01 15:10:12 -06002407 .bdrv_co_pwrite_zeroes = vmdk_co_pwrite_zeroes,
Fam Zhengda7a50f2013-07-01 11:33:17 +08002408 .bdrv_close = vmdk_close,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01002409 .bdrv_co_create_opts = vmdk_co_create_opts,
Fam Zhengda7a50f2013-07-01 11:33:17 +08002410 .bdrv_co_flush_to_disk = vmdk_co_flush,
Eric Blakec72080b2018-02-13 14:26:58 -06002411 .bdrv_co_block_status = vmdk_co_block_status,
Fam Zhengda7a50f2013-07-01 11:33:17 +08002412 .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
2413 .bdrv_has_zero_init = vmdk_has_zero_init,
Fam Zhengf4c129a2013-10-31 10:06:23 +08002414 .bdrv_get_specific_info = vmdk_get_specific_info,
Kevin Wolfd34682c2013-12-11 19:26:16 +01002415 .bdrv_refresh_limits = vmdk_refresh_limits,
Fam Zheng74fe1882014-05-06 21:08:45 +08002416 .bdrv_get_info = vmdk_get_info,
Kevin Wolf0e7e1982009-05-18 16:42:10 +02002417
Kevin Wolf8ee79e72014-06-04 15:09:35 +02002418 .supports_backing = true,
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002419 .create_opts = &vmdk_create_opts,
bellardea2384d2004-08-01 21:59:26 +00002420};
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002421
2422static void bdrv_vmdk_init(void)
2423{
2424 bdrv_register(&bdrv_vmdk);
2425}
2426
2427block_init(bdrv_vmdk_init);