blob: 7bdc3d0f3dafe27a45a9728580d9c99a4ef7d9c0 [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
pbrookfaf07962007-11-11 02:51:17 +000026#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010027#include "block/block_int.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010028#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/module.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010031#include "migration/migration.h"
Stefan Weil2923d342011-11-20 12:34:30 +010032#include <zlib.h>
Fam Zhenge5dc64b2014-12-04 07:28:29 +080033#include <glib.h>
bellardea2384d2004-08-01 21:59:26 +000034
bellardea2384d2004-08-01 21:59:26 +000035#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
36#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
Fam Zheng432bb172011-08-12 23:19:30 +080037#define VMDK4_COMPRESSION_DEFLATE 1
Fam Zheng95b0aa42013-05-02 10:25:25 +080038#define VMDK4_FLAG_NL_DETECT (1 << 0)
Fam Zhengbb45ded2011-08-12 23:19:34 +080039#define VMDK4_FLAG_RGD (1 << 1)
Fam Zheng14ead642013-05-02 10:25:23 +080040/* Zeroed-grain enable bit */
41#define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
Fam Zheng432bb172011-08-12 23:19:30 +080042#define VMDK4_FLAG_COMPRESS (1 << 16)
43#define VMDK4_FLAG_MARKER (1 << 17)
Kevin Wolf65bd1552012-08-16 10:39:33 +020044#define VMDK4_GD_AT_END 0xffffffffffffffffULL
bellardea2384d2004-08-01 21:59:26 +000045
Fam Zheng14ead642013-05-02 10:25:23 +080046#define VMDK_GTE_ZEROED 0x1
Fam Zheng65f74722013-05-02 10:25:22 +080047
48/* VMDK internal error codes */
49#define VMDK_OK 0
50#define VMDK_ERROR (-1)
51/* Cluster not allocated */
52#define VMDK_UNALLOC (-2)
53#define VMDK_ZEROED (-3)
54
Fam Zheng69e0b6d2013-05-02 10:25:24 +080055#define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
56
bellardea2384d2004-08-01 21:59:26 +000057typedef struct {
58 uint32_t version;
59 uint32_t flags;
60 uint32_t disk_sectors;
61 uint32_t granularity;
62 uint32_t l1dir_offset;
63 uint32_t l1dir_size;
64 uint32_t file_sectors;
65 uint32_t cylinders;
66 uint32_t heads;
67 uint32_t sectors_per_track;
Fam Zheng5d8caa52013-08-06 15:44:47 +080068} QEMU_PACKED VMDK3Header;
bellardea2384d2004-08-01 21:59:26 +000069
70typedef struct {
71 uint32_t version;
72 uint32_t flags;
Fam Zhenge98768d2013-08-06 15:44:48 +080073 uint64_t capacity;
74 uint64_t granularity;
75 uint64_t desc_offset;
76 uint64_t desc_size;
Fam Zhengca8804c2013-08-06 15:44:55 +080077 /* Number of GrainTableEntries per GrainTable */
78 uint32_t num_gtes_per_gt;
Fam Zhenge98768d2013-08-06 15:44:48 +080079 uint64_t rgd_offset;
80 uint64_t gd_offset;
81 uint64_t grain_offset;
bellardea2384d2004-08-01 21:59:26 +000082 char filler[1];
83 char check_bytes[4];
Fam Zheng432bb172011-08-12 23:19:30 +080084 uint16_t compressAlgorithm;
Stefan Weil541dc0d2011-08-31 12:38:01 +020085} QEMU_PACKED VMDK4Header;
bellardea2384d2004-08-01 21:59:26 +000086
87#define L2_CACHE_SIZE 16
88
Fam Zhengb3976d32011-07-12 19:56:28 +080089typedef struct VmdkExtent {
90 BlockDriverState *file;
91 bool flat;
Fam Zheng432bb172011-08-12 23:19:30 +080092 bool compressed;
93 bool has_marker;
Fam Zheng14ead642013-05-02 10:25:23 +080094 bool has_zero_grain;
95 int version;
Fam Zhengb3976d32011-07-12 19:56:28 +080096 int64_t sectors;
97 int64_t end_sector;
Fam Zheng7fa60fa2011-07-19 08:38:22 +080098 int64_t flat_start_offset;
bellardea2384d2004-08-01 21:59:26 +000099 int64_t l1_table_offset;
bellardff1afc72005-04-26 21:08:00 +0000100 int64_t l1_backup_table_offset;
bellardea2384d2004-08-01 21:59:26 +0000101 uint32_t *l1_table;
bellardff1afc72005-04-26 21:08:00 +0000102 uint32_t *l1_backup_table;
bellardea2384d2004-08-01 21:59:26 +0000103 unsigned int l1_size;
104 uint32_t l1_entry_sectors;
105
106 unsigned int l2_size;
107 uint32_t *l2_cache;
108 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
109 uint32_t l2_cache_counts[L2_CACHE_SIZE];
110
Fam Zheng301c7d32013-09-23 17:18:29 +0800111 int64_t cluster_sectors;
Fam Zhengc6ac36e2014-07-30 14:39:10 +0800112 int64_t next_cluster_sector;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800113 char *type;
Fam Zhengb3976d32011-07-12 19:56:28 +0800114} VmdkExtent;
115
116typedef struct BDRVVmdkState {
Paolo Bonzini848c66e2011-10-20 13:16:21 +0200117 CoMutex lock;
Fam Zhenge98768d2013-08-06 15:44:48 +0800118 uint64_t desc_offset;
Fam Zheng69b4d862011-07-12 19:56:34 +0800119 bool cid_updated;
Fam Zhengc338b6a2013-10-18 13:17:19 +0800120 bool cid_checked;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800121 uint32_t cid;
ths5f4da8c2007-01-24 21:05:24 +0000122 uint32_t parent_cid;
Fam Zhengb3976d32011-07-12 19:56:28 +0800123 int num_extents;
124 /* Extent array with num_extents entries, ascend ordered by address */
125 VmdkExtent *extents;
Kevin Wolf2bc31662011-11-22 16:50:27 +0100126 Error *migration_blocker;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800127 char *create_type;
bellardea2384d2004-08-01 21:59:26 +0000128} BDRVVmdkState;
129
ths630530a2007-06-18 15:01:30 +0000130typedef struct VmdkMetaData {
ths630530a2007-06-18 15:01:30 +0000131 unsigned int l1_index;
132 unsigned int l2_index;
133 unsigned int l2_offset;
134 int valid;
Fam Zhengcdeaf1f2013-05-02 10:25:27 +0800135 uint32_t *l2_cache_entry;
ths630530a2007-06-18 15:01:30 +0000136} VmdkMetaData;
137
Fam Zheng432bb172011-08-12 23:19:30 +0800138typedef struct VmdkGrainMarker {
139 uint64_t lba;
140 uint32_t size;
141 uint8_t data[0];
Fam Zheng5d8caa52013-08-06 15:44:47 +0800142} QEMU_PACKED VmdkGrainMarker;
Fam Zheng432bb172011-08-12 23:19:30 +0800143
Kevin Wolf65bd1552012-08-16 10:39:33 +0200144enum {
145 MARKER_END_OF_STREAM = 0,
146 MARKER_GRAIN_TABLE = 1,
147 MARKER_GRAIN_DIRECTORY = 2,
148 MARKER_FOOTER = 3,
149};
150
bellardea2384d2004-08-01 21:59:26 +0000151static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
152{
153 uint32_t magic;
154
Fam Zhengae261c82011-07-12 19:56:38 +0800155 if (buf_size < 4) {
bellardea2384d2004-08-01 21:59:26 +0000156 return 0;
Fam Zhengae261c82011-07-12 19:56:38 +0800157 }
bellardea2384d2004-08-01 21:59:26 +0000158 magic = be32_to_cpu(*(uint32_t *)buf);
159 if (magic == VMDK3_MAGIC ||
Fam Zheng01fc99d2011-07-12 19:56:30 +0800160 magic == VMDK4_MAGIC) {
bellardea2384d2004-08-01 21:59:26 +0000161 return 100;
Fam Zheng01fc99d2011-07-12 19:56:30 +0800162 } else {
163 const char *p = (const char *)buf;
164 const char *end = p + buf_size;
165 while (p < end) {
166 if (*p == '#') {
167 /* skip comment line */
168 while (p < end && *p != '\n') {
169 p++;
170 }
171 p++;
172 continue;
173 }
174 if (*p == ' ') {
175 while (p < end && *p == ' ') {
176 p++;
177 }
178 /* skip '\r' if windows line endings used. */
179 if (p < end && *p == '\r') {
180 p++;
181 }
182 /* only accept blank lines before 'version=' line */
183 if (p == end || *p != '\n') {
184 return 0;
185 }
186 p++;
187 continue;
188 }
189 if (end - p >= strlen("version=X\n")) {
190 if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
191 strncmp("version=2\n", p, strlen("version=2\n")) == 0) {
192 return 100;
193 }
194 }
195 if (end - p >= strlen("version=X\r\n")) {
196 if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
197 strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) {
198 return 100;
199 }
200 }
201 return 0;
202 }
bellardea2384d2004-08-01 21:59:26 +0000203 return 0;
Fam Zheng01fc99d2011-07-12 19:56:30 +0800204 }
bellardea2384d2004-08-01 21:59:26 +0000205}
206
ths3b46e622007-09-17 08:09:54 +0000207#define SECTOR_SIZE 512
Fam Zhengf66fd6c2011-07-19 08:45:23 +0800208#define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
209#define BUF_SIZE 4096
210#define HEADER_SIZE 512 /* first sector of 512 bytes */
ths5f4da8c2007-01-24 21:05:24 +0000211
Fam Zhengb3976d32011-07-12 19:56:28 +0800212static void vmdk_free_extents(BlockDriverState *bs)
213{
214 int i;
215 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800216 VmdkExtent *e;
Fam Zhengb3976d32011-07-12 19:56:28 +0800217
218 for (i = 0; i < s->num_extents; i++) {
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800219 e = &s->extents[i];
220 g_free(e->l1_table);
221 g_free(e->l2_cache);
222 g_free(e->l1_backup_table);
Fam Zhengf4c129a2013-10-31 10:06:23 +0800223 g_free(e->type);
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800224 if (e->file != bs->file) {
Fam Zheng4f6fd342013-08-23 09:14:47 +0800225 bdrv_unref(e->file);
Fam Zhengb3c0bfb2011-09-19 10:26:42 +0800226 }
Fam Zhengb3976d32011-07-12 19:56:28 +0800227 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500228 g_free(s->extents);
Fam Zhengb3976d32011-07-12 19:56:28 +0800229}
230
Fam Zheng86c6b422011-08-12 23:19:28 +0800231static void vmdk_free_last_extent(BlockDriverState *bs)
232{
233 BDRVVmdkState *s = bs->opaque;
234
235 if (s->num_extents == 0) {
236 return;
237 }
238 s->num_extents--;
Markus Armbruster5839e532014-08-19 10:31:08 +0200239 s->extents = g_renew(VmdkExtent, s->extents, s->num_extents);
Fam Zheng86c6b422011-08-12 23:19:28 +0800240}
241
ths5f4da8c2007-01-24 21:05:24 +0000242static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
243{
ths5f4da8c2007-01-24 21:05:24 +0000244 char desc[DESC_SIZE];
Pavel Borzenkov8379e462011-10-18 21:19:03 +0400245 uint32_t cid = 0xffffffff;
blueswir17ccfb2e2008-09-14 06:45:34 +0000246 const char *p_name, *cid_str;
ths5f4da8c2007-01-24 21:05:24 +0000247 size_t cid_str_size;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800248 BDRVVmdkState *s = bs->opaque;
Kevin Wolf99f18352011-10-26 12:25:25 +0200249 int ret;
ths5f4da8c2007-01-24 21:05:24 +0000250
Kevin Wolf99f18352011-10-26 12:25:25 +0200251 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
252 if (ret < 0) {
ths5f4da8c2007-01-24 21:05:24 +0000253 return 0;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800254 }
ths5f4da8c2007-01-24 21:05:24 +0000255
256 if (parent) {
257 cid_str = "parentCID";
258 cid_str_size = sizeof("parentCID");
259 } else {
260 cid_str = "CID";
261 cid_str_size = sizeof("CID");
262 }
263
Kevin Wolf93897b92011-10-26 12:25:52 +0200264 desc[DESC_SIZE - 1] = '\0';
Fam Zhengae261c82011-07-12 19:56:38 +0800265 p_name = strstr(desc, cid_str);
266 if (p_name != NULL) {
ths5f4da8c2007-01-24 21:05:24 +0000267 p_name += cid_str_size;
Fam Zheng9b170312014-04-17 18:43:53 +0800268 sscanf(p_name, "%" SCNx32, &cid);
ths5f4da8c2007-01-24 21:05:24 +0000269 }
270
271 return cid;
272}
273
274static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
275{
ths5f4da8c2007-01-24 21:05:24 +0000276 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
277 char *p_name, *tmp_str;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800278 BDRVVmdkState *s = bs->opaque;
Kevin Wolf99f18352011-10-26 12:25:25 +0200279 int ret;
ths5f4da8c2007-01-24 21:05:24 +0000280
Kevin Wolf99f18352011-10-26 12:25:25 +0200281 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
282 if (ret < 0) {
283 return ret;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800284 }
ths5f4da8c2007-01-24 21:05:24 +0000285
Kevin Wolf93897b92011-10-26 12:25:52 +0200286 desc[DESC_SIZE - 1] = '\0';
Fam Zhengae261c82011-07-12 19:56:38 +0800287 tmp_str = strstr(desc, "parentCID");
Kevin Wolf93897b92011-10-26 12:25:52 +0200288 if (tmp_str == NULL) {
289 return -EINVAL;
290 }
291
blueswir1363a37d2008-08-21 17:58:08 +0000292 pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
Fam Zhengae261c82011-07-12 19:56:38 +0800293 p_name = strstr(desc, "CID");
294 if (p_name != NULL) {
ths5f4da8c2007-01-24 21:05:24 +0000295 p_name += sizeof("CID");
Fam Zheng9b170312014-04-17 18:43:53 +0800296 snprintf(p_name, sizeof(desc) - (p_name - desc), "%" PRIx32 "\n", cid);
blueswir1363a37d2008-08-21 17:58:08 +0000297 pstrcat(desc, sizeof(desc), tmp_desc);
ths5f4da8c2007-01-24 21:05:24 +0000298 }
299
Kevin Wolf99f18352011-10-26 12:25:25 +0200300 ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
301 if (ret < 0) {
302 return ret;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800303 }
Kevin Wolf99f18352011-10-26 12:25:25 +0200304
ths5f4da8c2007-01-24 21:05:24 +0000305 return 0;
306}
307
308static int vmdk_is_cid_valid(BlockDriverState *bs)
309{
ths5f4da8c2007-01-24 21:05:24 +0000310 BDRVVmdkState *s = bs->opaque;
Kevin Wolfb1712712009-07-17 08:20:41 +0200311 BlockDriverState *p_bs = bs->backing_hd;
ths5f4da8c2007-01-24 21:05:24 +0000312 uint32_t cur_pcid;
313
Fam Zhengc338b6a2013-10-18 13:17:19 +0800314 if (!s->cid_checked && p_bs) {
Fam Zhengae261c82011-07-12 19:56:38 +0800315 cur_pcid = vmdk_read_cid(p_bs, 0);
316 if (s->parent_cid != cur_pcid) {
317 /* CID not valid */
ths5f4da8c2007-01-24 21:05:24 +0000318 return 0;
Fam Zhengae261c82011-07-12 19:56:38 +0800319 }
ths5f4da8c2007-01-24 21:05:24 +0000320 }
Fam Zhengc338b6a2013-10-18 13:17:19 +0800321 s->cid_checked = true;
Fam Zhengae261c82011-07-12 19:56:38 +0800322 /* CID valid */
ths5f4da8c2007-01-24 21:05:24 +0000323 return 1;
324}
325
Kevin Wolf67251a32015-04-09 18:54:04 +0200326/* We have nothing to do for VMDK reopen, stubs just return success */
Jeff Cody38975752012-09-20 15:13:30 -0400327static int vmdk_reopen_prepare(BDRVReopenState *state,
328 BlockReopenQueue *queue, Error **errp)
329{
Jeff Cody38975752012-09-20 15:13:30 -0400330 assert(state != NULL);
331 assert(state->bs != NULL);
Kevin Wolf67251a32015-04-09 18:54:04 +0200332 return 0;
Jeff Cody38975752012-09-20 15:13:30 -0400333}
334
Kevin Wolf9949f972010-04-16 21:07:19 +0200335static int vmdk_parent_open(BlockDriverState *bs)
ths5f4da8c2007-01-24 21:05:24 +0000336{
ths5fafdf22007-09-16 21:08:06 +0000337 char *p_name;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800338 char desc[DESC_SIZE + 1];
Fam Zhenge1da9b22011-07-12 19:56:32 +0800339 BDRVVmdkState *s = bs->opaque;
Paolo Bonzini588b65a2011-10-20 13:16:19 +0200340 int ret;
ths5f4da8c2007-01-24 21:05:24 +0000341
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800342 desc[DESC_SIZE] = '\0';
Paolo Bonzini588b65a2011-10-20 13:16:19 +0200343 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
344 if (ret < 0) {
345 return ret;
Fam Zhenge1da9b22011-07-12 19:56:32 +0800346 }
ths5f4da8c2007-01-24 21:05:24 +0000347
Fam Zhengae261c82011-07-12 19:56:38 +0800348 p_name = strstr(desc, "parentFileNameHint");
349 if (p_name != NULL) {
ths5f4da8c2007-01-24 21:05:24 +0000350 char *end_name;
ths5f4da8c2007-01-24 21:05:24 +0000351
352 p_name += sizeof("parentFileNameHint") + 1;
Fam Zhengae261c82011-07-12 19:56:38 +0800353 end_name = strchr(p_name, '\"');
354 if (end_name == NULL) {
Paolo Bonzini588b65a2011-10-20 13:16:19 +0200355 return -EINVAL;
Fam Zhengae261c82011-07-12 19:56:38 +0800356 }
357 if ((end_name - p_name) > sizeof(bs->backing_file) - 1) {
Paolo Bonzini588b65a2011-10-20 13:16:19 +0200358 return -EINVAL;
Fam Zhengae261c82011-07-12 19:56:38 +0800359 }
ths3b46e622007-09-17 08:09:54 +0000360
Kevin Wolfb1712712009-07-17 08:20:41 +0200361 pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
ths5f4da8c2007-01-24 21:05:24 +0000362 }
363
364 return 0;
365}
366
Fam Zhengb3976d32011-07-12 19:56:28 +0800367/* Create and append extent to the extent array. Return the added VmdkExtent
368 * address. return NULL if allocation failed. */
Fam Zheng8aa13312013-08-06 15:44:51 +0800369static int vmdk_add_extent(BlockDriverState *bs,
Fam Zhengb3976d32011-07-12 19:56:28 +0800370 BlockDriverState *file, bool flat, int64_t sectors,
371 int64_t l1_offset, int64_t l1_backup_offset,
372 uint32_t l1_size,
Fam Zheng8aa13312013-08-06 15:44:51 +0800373 int l2_size, uint64_t cluster_sectors,
Fam Zheng48239702013-10-11 15:43:22 +0800374 VmdkExtent **new_extent,
375 Error **errp)
Fam Zhengb3976d32011-07-12 19:56:28 +0800376{
377 VmdkExtent *extent;
378 BDRVVmdkState *s = bs->opaque;
Markus Armbruster0a156f72014-08-21 14:36:19 +0200379 int64_t nb_sectors;
Fam Zhengb3976d32011-07-12 19:56:28 +0800380
Fam Zheng8aa13312013-08-06 15:44:51 +0800381 if (cluster_sectors > 0x200000) {
382 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
Fam Zheng48239702013-10-11 15:43:22 +0800383 error_setg(errp, "Invalid granularity, image may be corrupt");
384 return -EFBIG;
Fam Zheng8aa13312013-08-06 15:44:51 +0800385 }
Fam Zhengb0651b82013-08-19 18:54:25 +0800386 if (l1_size > 512 * 1024 * 1024) {
387 /* Although with big capacity and small l1_entry_sectors, we can get a
388 * big l1_size, we don't want unbounded value to allocate the table.
389 * Limit it to 512M, which is 16PB for default cluster and L2 table
390 * size */
Fam Zheng48239702013-10-11 15:43:22 +0800391 error_setg(errp, "L1 size too big");
Fam Zhengb0651b82013-08-19 18:54:25 +0800392 return -EFBIG;
393 }
Fam Zheng8aa13312013-08-06 15:44:51 +0800394
Markus Armbruster0a156f72014-08-21 14:36:19 +0200395 nb_sectors = bdrv_nb_sectors(file);
396 if (nb_sectors < 0) {
397 return nb_sectors;
Fam Zhengc6ac36e2014-07-30 14:39:10 +0800398 }
399
Markus Armbruster5839e532014-08-19 10:31:08 +0200400 s->extents = g_renew(VmdkExtent, s->extents, s->num_extents + 1);
Fam Zhengb3976d32011-07-12 19:56:28 +0800401 extent = &s->extents[s->num_extents];
402 s->num_extents++;
403
404 memset(extent, 0, sizeof(VmdkExtent));
405 extent->file = file;
406 extent->flat = flat;
407 extent->sectors = sectors;
408 extent->l1_table_offset = l1_offset;
409 extent->l1_backup_table_offset = l1_backup_offset;
410 extent->l1_size = l1_size;
411 extent->l1_entry_sectors = l2_size * cluster_sectors;
412 extent->l2_size = l2_size;
Fam Zheng301c7d32013-09-23 17:18:29 +0800413 extent->cluster_sectors = flat ? sectors : cluster_sectors;
Markus Armbruster0a156f72014-08-21 14:36:19 +0200414 extent->next_cluster_sector = ROUND_UP(nb_sectors, cluster_sectors);
Fam Zhengb3976d32011-07-12 19:56:28 +0800415
416 if (s->num_extents > 1) {
417 extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
418 } else {
419 extent->end_sector = extent->sectors;
420 }
421 bs->total_sectors = extent->end_sector;
Fam Zheng8aa13312013-08-06 15:44:51 +0800422 if (new_extent) {
423 *new_extent = extent;
424 }
425 return 0;
Fam Zhengb3976d32011-07-12 19:56:28 +0800426}
427
Fam Zheng48239702013-10-11 15:43:22 +0800428static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
429 Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000430{
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800431 int ret;
Fam Zheng13c49412015-05-05 17:28:13 +0800432 size_t l1_size;
433 int i;
ths5f4da8c2007-01-24 21:05:24 +0000434
bellardea2384d2004-08-01 21:59:26 +0000435 /* read the L1 table */
Fam Zhengb3976d32011-07-12 19:56:28 +0800436 l1_size = extent->l1_size * sizeof(uint32_t);
Kevin Wolfd6e59932014-05-20 13:56:27 +0200437 extent->l1_table = g_try_malloc(l1_size);
438 if (l1_size && extent->l1_table == NULL) {
439 return -ENOMEM;
440 }
441
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800442 ret = bdrv_pread(extent->file,
Fam Zheng48239702013-10-11 15:43:22 +0800443 extent->l1_table_offset,
444 extent->l1_table,
445 l1_size);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800446 if (ret < 0) {
Fam Zheng48239702013-10-11 15:43:22 +0800447 error_setg_errno(errp, -ret,
448 "Could not read l1 table from extent '%s'",
449 extent->file->filename);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800450 goto fail_l1;
Fam Zhengb3976d32011-07-12 19:56:28 +0800451 }
452 for (i = 0; i < extent->l1_size; i++) {
453 le32_to_cpus(&extent->l1_table[i]);
bellardea2384d2004-08-01 21:59:26 +0000454 }
455
Fam Zhengb3976d32011-07-12 19:56:28 +0800456 if (extent->l1_backup_table_offset) {
Kevin Wolfd6e59932014-05-20 13:56:27 +0200457 extent->l1_backup_table = g_try_malloc(l1_size);
458 if (l1_size && extent->l1_backup_table == NULL) {
459 ret = -ENOMEM;
460 goto fail_l1;
461 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800462 ret = bdrv_pread(extent->file,
Fam Zheng48239702013-10-11 15:43:22 +0800463 extent->l1_backup_table_offset,
464 extent->l1_backup_table,
465 l1_size);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800466 if (ret < 0) {
Fam Zheng48239702013-10-11 15:43:22 +0800467 error_setg_errno(errp, -ret,
468 "Could not read l1 backup table from extent '%s'",
469 extent->file->filename);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800470 goto fail_l1b;
Fam Zhengb3976d32011-07-12 19:56:28 +0800471 }
472 for (i = 0; i < extent->l1_size; i++) {
473 le32_to_cpus(&extent->l1_backup_table[i]);
bellardff1afc72005-04-26 21:08:00 +0000474 }
475 }
476
Fam Zhengb3976d32011-07-12 19:56:28 +0800477 extent->l2_cache =
Markus Armbruster5839e532014-08-19 10:31:08 +0200478 g_new(uint32_t, extent->l2_size * L2_CACHE_SIZE);
bellardea2384d2004-08-01 21:59:26 +0000479 return 0;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800480 fail_l1b:
Anthony Liguori7267c092011-08-20 22:09:37 -0500481 g_free(extent->l1_backup_table);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800482 fail_l1:
Anthony Liguori7267c092011-08-20 22:09:37 -0500483 g_free(extent->l1_table);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800484 return ret;
485}
486
Fam Zhengdaac8fd2013-08-19 18:54:27 +0800487static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
488 BlockDriverState *file,
Fam Zheng48239702013-10-11 15:43:22 +0800489 int flags, Error **errp)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800490{
491 int ret;
492 uint32_t magic;
493 VMDK3Header header;
494 VmdkExtent *extent;
495
Fam Zheng86c6b422011-08-12 23:19:28 +0800496 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800497 if (ret < 0) {
Fam Zheng48239702013-10-11 15:43:22 +0800498 error_setg_errno(errp, -ret,
499 "Could not read header from file '%s'",
500 file->filename);
Fam Zheng86c6b422011-08-12 23:19:28 +0800501 return ret;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800502 }
Fam Zhengf6b61e52013-08-19 18:54:26 +0800503 ret = vmdk_add_extent(bs, file, false,
504 le32_to_cpu(header.disk_sectors),
Fam Zheng7237aec2015-04-27 22:23:01 +0800505 (int64_t)le32_to_cpu(header.l1dir_offset) << 9,
Fam Zhengf6b61e52013-08-19 18:54:26 +0800506 0,
507 le32_to_cpu(header.l1dir_size),
508 4096,
509 le32_to_cpu(header.granularity),
Fam Zheng48239702013-10-11 15:43:22 +0800510 &extent,
511 errp);
Fam Zheng8aa13312013-08-06 15:44:51 +0800512 if (ret < 0) {
513 return ret;
514 }
Fam Zheng48239702013-10-11 15:43:22 +0800515 ret = vmdk_init_tables(bs, extent, errp);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800516 if (ret) {
Fam Zheng86c6b422011-08-12 23:19:28 +0800517 /* free extent allocated by vmdk_add_extent */
518 vmdk_free_last_extent(bs);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800519 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800520 return ret;
521}
522
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100523static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
Kevin Wolfa6468362015-04-07 15:35:59 +0200524 QDict *options, Error **errp);
Fam Zhengf16f5092011-08-12 23:19:33 +0800525
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100526static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
527 Error **errp)
528{
529 int64_t size;
530 char *buf;
531 int ret;
532
533 size = bdrv_getlength(file);
534 if (size < 0) {
535 error_setg_errno(errp, -size, "Could not access file");
536 return NULL;
537 }
538
Fam Zheng03c33592014-12-04 07:28:32 +0800539 if (size < 4) {
540 /* Both descriptor file and sparse image must be much larger than 4
541 * bytes, also callers of vmdk_read_desc want to compare the first 4
542 * bytes with VMDK4_MAGIC, let's error out if less is read. */
543 error_setg(errp, "File is too small, not a valid image");
544 return NULL;
545 }
546
Fam Zheng73b7bca2014-12-04 07:28:31 +0800547 size = MIN(size, (1 << 20) - 1); /* avoid unbounded allocation */
548 buf = g_malloc(size + 1);
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100549
550 ret = bdrv_pread(file, desc_offset, buf, size);
551 if (ret < 0) {
552 error_setg_errno(errp, -ret, "Could not read from file");
553 g_free(buf);
554 return NULL;
555 }
Fam Zheng73b7bca2014-12-04 07:28:31 +0800556 buf[ret] = 0;
Paolo Bonzinia8842e62014-02-17 14:44:02 +0100557
558 return buf;
559}
560
Fam Zheng86c6b422011-08-12 23:19:28 +0800561static int vmdk_open_vmdk4(BlockDriverState *bs,
562 BlockDriverState *file,
Kevin Wolfa6468362015-04-07 15:35:59 +0200563 int flags, QDict *options, Error **errp)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800564{
565 int ret;
566 uint32_t magic;
567 uint32_t l1_size, l1_entry_sectors;
568 VMDK4Header header;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800569 VmdkExtent *extent;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800570 BDRVVmdkState *s = bs->opaque;
Fam Zhengbb45ded2011-08-12 23:19:34 +0800571 int64_t l1_backup_offset = 0;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800572
Fam Zheng86c6b422011-08-12 23:19:28 +0800573 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800574 if (ret < 0) {
Fam Zheng48239702013-10-11 15:43:22 +0800575 error_setg_errno(errp, -ret,
576 "Could not read header from file '%s'",
577 file->filename);
Paolo Bonzini89ac8482014-02-17 14:44:05 +0100578 return -EINVAL;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800579 }
Stefan Hajnoczi5a394b92013-06-10 11:07:33 +0200580 if (header.capacity == 0) {
Fam Zhenge98768d2013-08-06 15:44:48 +0800581 uint64_t desc_offset = le64_to_cpu(header.desc_offset);
Stefan Hajnoczi5a394b92013-06-10 11:07:33 +0200582 if (desc_offset) {
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100583 char *buf = vmdk_read_desc(file, desc_offset << 9, errp);
584 if (!buf) {
585 return -EINVAL;
586 }
Kevin Wolfa6468362015-04-07 15:35:59 +0200587 ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100588 g_free(buf);
589 return ret;
Stefan Hajnoczi5a394b92013-06-10 11:07:33 +0200590 }
Fam Zhengf16f5092011-08-12 23:19:33 +0800591 }
Kevin Wolf65bd1552012-08-16 10:39:33 +0200592
Fam Zhengf4c129a2013-10-31 10:06:23 +0800593 if (!s->create_type) {
594 s->create_type = g_strdup("monolithicSparse");
595 }
596
Kevin Wolf65bd1552012-08-16 10:39:33 +0200597 if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
598 /*
599 * The footer takes precedence over the header, so read it in. The
600 * footer starts at offset -1024 from the end: One sector for the
601 * footer, and another one for the end-of-stream marker.
602 */
603 struct {
604 struct {
605 uint64_t val;
606 uint32_t size;
607 uint32_t type;
608 uint8_t pad[512 - 16];
609 } QEMU_PACKED footer_marker;
610
611 uint32_t magic;
612 VMDK4Header header;
613 uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
614
615 struct {
616 uint64_t val;
617 uint32_t size;
618 uint32_t type;
619 uint8_t pad[512 - 16];
620 } QEMU_PACKED eos_marker;
621 } QEMU_PACKED footer;
622
623 ret = bdrv_pread(file,
624 bs->file->total_sectors * 512 - 1536,
625 &footer, sizeof(footer));
626 if (ret < 0) {
Fam Zhengd899d2e2014-12-04 07:28:34 +0800627 error_setg_errno(errp, -ret, "Failed to read footer");
Kevin Wolf65bd1552012-08-16 10:39:33 +0200628 return ret;
629 }
630
631 /* Some sanity checks for the footer */
632 if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
633 le32_to_cpu(footer.footer_marker.size) != 0 ||
634 le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
635 le64_to_cpu(footer.eos_marker.val) != 0 ||
636 le32_to_cpu(footer.eos_marker.size) != 0 ||
637 le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
638 {
Fam Zhengd899d2e2014-12-04 07:28:34 +0800639 error_setg(errp, "Invalid footer");
Kevin Wolf65bd1552012-08-16 10:39:33 +0200640 return -EINVAL;
641 }
642
643 header = footer.header;
644 }
645
Fam Zheng509d39a2013-11-28 09:48:03 +0800646 if (le32_to_cpu(header.version) > 3) {
Fam Zheng96c51eb2013-06-13 11:21:29 +0800647 char buf[64];
Fam Zheng4ab9dab2014-04-17 11:34:37 +0800648 snprintf(buf, sizeof(buf), "VMDK version %" PRId32,
Fam Zheng96c51eb2013-06-13 11:21:29 +0800649 le32_to_cpu(header.version));
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100650 error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
651 bdrv_get_device_or_node_name(bs), "vmdk", buf);
Fam Zheng96c51eb2013-06-13 11:21:29 +0800652 return -ENOTSUP;
Fam Zheng509d39a2013-11-28 09:48:03 +0800653 } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
654 /* VMware KB 2064959 explains that version 3 added support for
655 * persistent changed block tracking (CBT), and backup software can
656 * read it as version=1 if it doesn't care about the changed area
657 * information. So we are safe to enable read only. */
658 error_setg(errp, "VMDK version 3 must be read only");
659 return -EINVAL;
Fam Zheng96c51eb2013-06-13 11:21:29 +0800660 }
661
Fam Zhengca8804c2013-08-06 15:44:55 +0800662 if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
Paolo Bonzini89ac8482014-02-17 14:44:05 +0100663 error_setg(errp, "L2 table size too big");
Fam Zhengf8ce0402013-08-06 15:44:52 +0800664 return -EINVAL;
665 }
666
Fam Zhengca8804c2013-08-06 15:44:55 +0800667 l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800668 * le64_to_cpu(header.granularity);
Stefan Weil75d12342012-02-25 14:01:42 +0100669 if (l1_entry_sectors == 0) {
Fam Zhengd899d2e2014-12-04 07:28:34 +0800670 error_setg(errp, "L1 entry size is invalid");
Fam Zheng86c6b422011-08-12 23:19:28 +0800671 return -EINVAL;
672 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800673 l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
674 / l1_entry_sectors;
Fam Zhengbb45ded2011-08-12 23:19:34 +0800675 if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
676 l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
677 }
Markus Armbruster57322b72014-06-26 13:23:22 +0200678 if (bdrv_nb_sectors(file) < le64_to_cpu(header.grain_offset)) {
Fam Zheng4ab9dab2014-04-17 11:34:37 +0800679 error_setg(errp, "File truncated, expecting at least %" PRId64 " bytes",
680 (int64_t)(le64_to_cpu(header.grain_offset)
681 * BDRV_SECTOR_SIZE));
Fam Zheng34ceed82014-01-21 15:07:43 +0800682 return -EINVAL;
683 }
684
Fam Zheng8aa13312013-08-06 15:44:51 +0800685 ret = vmdk_add_extent(bs, file, false,
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800686 le64_to_cpu(header.capacity),
687 le64_to_cpu(header.gd_offset) << 9,
Fam Zhengbb45ded2011-08-12 23:19:34 +0800688 l1_backup_offset,
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800689 l1_size,
Fam Zhengca8804c2013-08-06 15:44:55 +0800690 le32_to_cpu(header.num_gtes_per_gt),
Fam Zheng8aa13312013-08-06 15:44:51 +0800691 le64_to_cpu(header.granularity),
Fam Zheng48239702013-10-11 15:43:22 +0800692 &extent,
693 errp);
Fam Zheng8aa13312013-08-06 15:44:51 +0800694 if (ret < 0) {
695 return ret;
696 }
Fam Zheng432bb172011-08-12 23:19:30 +0800697 extent->compressed =
698 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
Fam Zhengd8a7b062014-01-23 15:10:52 +0800699 if (extent->compressed) {
700 g_free(s->create_type);
701 s->create_type = g_strdup("streamOptimized");
702 }
Fam Zheng432bb172011-08-12 23:19:30 +0800703 extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
Fam Zheng14ead642013-05-02 10:25:23 +0800704 extent->version = le32_to_cpu(header.version);
705 extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
Fam Zheng48239702013-10-11 15:43:22 +0800706 ret = vmdk_init_tables(bs, extent, errp);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800707 if (ret) {
Fam Zheng86c6b422011-08-12 23:19:28 +0800708 /* free extent allocated by vmdk_add_extent */
709 vmdk_free_last_extent(bs);
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800710 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800711 return ret;
712}
713
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800714/* find an option value out of descriptor file */
715static int vmdk_parse_description(const char *desc, const char *opt_name,
716 char *buf, int buf_size)
717{
718 char *opt_pos, *opt_end;
719 const char *end = desc + strlen(desc);
720
721 opt_pos = strstr(desc, opt_name);
722 if (!opt_pos) {
Fam Zheng65f74722013-05-02 10:25:22 +0800723 return VMDK_ERROR;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800724 }
725 /* Skip "=\"" following opt_name */
726 opt_pos += strlen(opt_name) + 2;
727 if (opt_pos >= end) {
Fam Zheng65f74722013-05-02 10:25:22 +0800728 return VMDK_ERROR;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800729 }
730 opt_end = opt_pos;
731 while (opt_end < end && *opt_end != '"') {
732 opt_end++;
733 }
734 if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
Fam Zheng65f74722013-05-02 10:25:22 +0800735 return VMDK_ERROR;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800736 }
737 pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
Fam Zheng65f74722013-05-02 10:25:22 +0800738 return VMDK_OK;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800739}
740
Fam Zheng86c6b422011-08-12 23:19:28 +0800741/* Open an extent file and append to bs array */
742static int vmdk_open_sparse(BlockDriverState *bs,
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100743 BlockDriverState *file, int flags,
Kevin Wolfa6468362015-04-07 15:35:59 +0200744 char *buf, QDict *options, Error **errp)
Fam Zheng86c6b422011-08-12 23:19:28 +0800745{
746 uint32_t magic;
747
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100748 magic = ldl_be_p(buf);
Fam Zheng86c6b422011-08-12 23:19:28 +0800749 switch (magic) {
750 case VMDK3_MAGIC:
Fam Zheng48239702013-10-11 15:43:22 +0800751 return vmdk_open_vmfs_sparse(bs, file, flags, errp);
Fam Zheng86c6b422011-08-12 23:19:28 +0800752 break;
753 case VMDK4_MAGIC:
Kevin Wolfa6468362015-04-07 15:35:59 +0200754 return vmdk_open_vmdk4(bs, file, flags, options, errp);
Fam Zheng86c6b422011-08-12 23:19:28 +0800755 break;
756 default:
Paolo Bonzini76abe402014-02-17 14:44:06 +0100757 error_setg(errp, "Image not in VMDK format");
758 return -EINVAL;
Fam Zheng86c6b422011-08-12 23:19:28 +0800759 break;
760 }
761}
762
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800763static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
Kevin Wolfa6468362015-04-07 15:35:59 +0200764 const char *desc_file_path, QDict *options,
765 Error **errp)
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800766{
767 int ret;
Jeff Cody395a22f2015-01-22 08:03:25 -0500768 int matches;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800769 char access[11];
770 char type[11];
771 char fname[512];
772 const char *p = desc;
773 int64_t sectors = 0;
774 int64_t flat_offset;
Jeff Codyfe206562015-01-22 08:03:26 -0500775 char *extent_path;
Fam Zheng86c6b422011-08-12 23:19:28 +0800776 BlockDriverState *extent_file;
Fam Zhengf4c129a2013-10-31 10:06:23 +0800777 BDRVVmdkState *s = bs->opaque;
778 VmdkExtent *extent;
Kevin Wolfa6468362015-04-07 15:35:59 +0200779 char extent_opt_prefix[32];
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800780
781 while (*p) {
Fam Zheng8a3e0bc2014-12-04 07:28:30 +0800782 /* parse extent line in one of below formats:
783 *
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800784 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800785 * RW [size in sectors] SPARSE "file-name.vmdk"
Fam Zheng8a3e0bc2014-12-04 07:28:30 +0800786 * RW [size in sectors] VMFS "file-name.vmdk"
787 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800788 */
789 flat_offset = -1;
Jeff Cody395a22f2015-01-22 08:03:25 -0500790 matches = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
791 access, &sectors, type, fname, &flat_offset);
792 if (matches < 4 || strcmp(access, "RW")) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800793 goto next_line;
794 } else if (!strcmp(type, "FLAT")) {
Jeff Cody395a22f2015-01-22 08:03:25 -0500795 if (matches != 5 || flat_offset < 0) {
Fam Zheng48239702013-10-11 15:43:22 +0800796 error_setg(errp, "Invalid extent lines: \n%s", p);
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800797 return -EINVAL;
798 }
Fam Zhengdbbcaa82013-10-18 15:07:33 +0800799 } else if (!strcmp(type, "VMFS")) {
Jeff Cody395a22f2015-01-22 08:03:25 -0500800 if (matches == 4) {
Fam Zhengb47053b2013-12-09 13:24:36 +0800801 flat_offset = 0;
802 } else {
803 error_setg(errp, "Invalid extent lines:\n%s", p);
804 return -EINVAL;
805 }
Jeff Cody395a22f2015-01-22 08:03:25 -0500806 } else if (matches != 4) {
Fam Zhengb47053b2013-12-09 13:24:36 +0800807 error_setg(errp, "Invalid extent lines:\n%s", p);
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800808 return -EINVAL;
809 }
810
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800811 if (sectors <= 0 ||
Fam Zhengdaac8fd2013-08-19 18:54:27 +0800812 (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
Paolo Bonzini04d542c2013-08-19 18:54:28 +0800813 strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) ||
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800814 (strcmp(access, "RW"))) {
815 goto next_line;
816 }
817
Max Reitz5c984152014-12-03 14:57:22 +0100818 if (!path_is_absolute(fname) && !path_has_protocol(fname) &&
819 !desc_file_path[0])
820 {
821 error_setg(errp, "Cannot use relative extent paths with VMDK "
822 "descriptor file '%s'", bs->file->filename);
823 return -EINVAL;
824 }
825
Jeff Codyfe206562015-01-22 08:03:26 -0500826 extent_path = g_malloc0(PATH_MAX);
Jeff Codya7be17b2015-02-10 13:22:56 -0500827 path_combine(extent_path, PATH_MAX, desc_file_path, fname);
Max Reitz2e401342014-02-18 18:33:07 +0100828 extent_file = NULL;
Kevin Wolfa6468362015-04-07 15:35:59 +0200829
830 ret = snprintf(extent_opt_prefix, 32, "extents.%d", s->num_extents);
831 assert(ret < 32);
832
Kevin Wolff3930ed2015-04-08 13:43:47 +0200833 ret = bdrv_open_image(&extent_file, extent_path, options,
834 extent_opt_prefix, bs, &child_file, false, errp);
Jeff Codyfe206562015-01-22 08:03:26 -0500835 g_free(extent_path);
Fam Zheng86c6b422011-08-12 23:19:28 +0800836 if (ret) {
837 return ret;
838 }
839
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800840 /* save to extents array */
Paolo Bonzini04d542c2013-08-19 18:54:28 +0800841 if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800842 /* FLAT extent */
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800843
Fam Zheng8aa13312013-08-06 15:44:51 +0800844 ret = vmdk_add_extent(bs, extent_file, true, sectors,
Fam Zheng48239702013-10-11 15:43:22 +0800845 0, 0, 0, 0, 0, &extent, errp);
Fam Zheng8aa13312013-08-06 15:44:51 +0800846 if (ret < 0) {
Stefan Hajnocziff74f332014-09-04 21:04:42 +0100847 bdrv_unref(extent_file);
Fam Zheng8aa13312013-08-06 15:44:51 +0800848 return ret;
849 }
Fam Zhengf16f5092011-08-12 23:19:33 +0800850 extent->flat_start_offset = flat_offset << 9;
Fam Zhengdaac8fd2013-08-19 18:54:27 +0800851 } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
852 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100853 char *buf = vmdk_read_desc(extent_file, 0, errp);
854 if (!buf) {
855 ret = -EINVAL;
856 } else {
Kevin Wolfa6468362015-04-07 15:35:59 +0200857 ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, buf,
858 options, errp);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100859 }
Stefan Hajnoczib6b1d312014-09-04 21:04:43 +0100860 g_free(buf);
Fam Zheng86c6b422011-08-12 23:19:28 +0800861 if (ret) {
Fam Zheng4f6fd342013-08-23 09:14:47 +0800862 bdrv_unref(extent_file);
Fam Zheng86c6b422011-08-12 23:19:28 +0800863 return ret;
864 }
Fam Zhengf4c129a2013-10-31 10:06:23 +0800865 extent = &s->extents[s->num_extents - 1];
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800866 } else {
Fam Zheng48239702013-10-11 15:43:22 +0800867 error_setg(errp, "Unsupported extent type '%s'", type);
Stefan Hajnocziff74f332014-09-04 21:04:42 +0100868 bdrv_unref(extent_file);
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800869 return -ENOTSUP;
870 }
Fam Zhengf4c129a2013-10-31 10:06:23 +0800871 extent->type = g_strdup(type);
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800872next_line:
873 /* move to next line */
Fam Zheng899f1ae2013-10-11 19:48:29 +0800874 while (*p) {
875 if (*p == '\n') {
876 p++;
877 break;
878 }
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800879 p++;
880 }
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800881 }
882 return 0;
883}
884
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100885static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
Kevin Wolfa6468362015-04-07 15:35:59 +0200886 QDict *options, Error **errp)
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800887{
888 int ret;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800889 char ct[128];
890 BDRVVmdkState *s = bs->opaque;
891
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800892 if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
Paolo Bonzini76abe402014-02-17 14:44:06 +0100893 error_setg(errp, "invalid VMDK image descriptor");
894 ret = -EINVAL;
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +0300895 goto exit;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800896 }
Fam Zheng6398de52011-08-12 23:19:27 +0800897 if (strcmp(ct, "monolithicFlat") &&
Paolo Bonzini04d542c2013-08-19 18:54:28 +0800898 strcmp(ct, "vmfs") &&
Fam Zhengdaac8fd2013-08-19 18:54:27 +0800899 strcmp(ct, "vmfsSparse") &&
Fam Zheng86c6b422011-08-12 23:19:28 +0800900 strcmp(ct, "twoGbMaxExtentSparse") &&
Fam Zheng6398de52011-08-12 23:19:27 +0800901 strcmp(ct, "twoGbMaxExtentFlat")) {
Fam Zheng48239702013-10-11 15:43:22 +0800902 error_setg(errp, "Unsupported image type '%s'", ct);
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +0300903 ret = -ENOTSUP;
904 goto exit;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800905 }
Fam Zhengf4c129a2013-10-31 10:06:23 +0800906 s->create_type = g_strdup(ct);
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800907 s->desc_offset = 0;
Kevin Wolfa6468362015-04-07 15:35:59 +0200908 ret = vmdk_parse_extents(buf, bs, bs->file->exact_filename, options, errp);
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +0300909exit:
Evgeny Budilovsky0bed0872013-06-12 14:06:30 +0300910 return ret;
Fam Zheng7fa60fa2011-07-19 08:38:22 +0800911}
912
Max Reitz015a1032013-09-05 14:22:29 +0200913static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
914 Error **errp)
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800915{
Fam Zheng9aeecbb2014-12-04 07:28:33 +0800916 char *buf;
Fam Zheng86c6b422011-08-12 23:19:28 +0800917 int ret;
918 BDRVVmdkState *s = bs->opaque;
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100919 uint32_t magic;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800920
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100921 buf = vmdk_read_desc(bs->file, 0, errp);
922 if (!buf) {
923 return -EINVAL;
924 }
925
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100926 magic = ldl_be_p(buf);
927 switch (magic) {
928 case VMDK3_MAGIC:
929 case VMDK4_MAGIC:
Kevin Wolfa6468362015-04-07 15:35:59 +0200930 ret = vmdk_open_sparse(bs, bs->file, flags, buf, options, errp);
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100931 s->desc_offset = 0x200;
932 break;
933 default:
Kevin Wolfa6468362015-04-07 15:35:59 +0200934 ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100935 break;
Fam Zhengb4b3ab12011-07-12 19:56:31 +0800936 }
Paolo Bonzini37f09e52014-02-17 14:44:04 +0100937 if (ret) {
938 goto fail;
939 }
940
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +0200941 /* try to open parent images, if exist */
942 ret = vmdk_parent_open(bs);
943 if (ret) {
944 goto fail;
945 }
Fam Zhengf4c129a2013-10-31 10:06:23 +0800946 s->cid = vmdk_read_cid(bs, 0);
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +0200947 s->parent_cid = vmdk_read_cid(bs, 1);
Paolo Bonzini848c66e2011-10-20 13:16:21 +0200948 qemu_co_mutex_init(&s->lock);
Kevin Wolf2bc31662011-11-22 16:50:27 +0100949
950 /* Disable migration when VMDK images are used */
Alberto Garcia81e5f782015-04-08 12:29:19 +0300951 error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
952 "does not support live migration",
953 bdrv_get_device_or_node_name(bs));
Kevin Wolf2bc31662011-11-22 16:50:27 +0100954 migrate_add_blocker(s->migration_blocker);
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100955 g_free(buf);
Kevin Wolf2bc31662011-11-22 16:50:27 +0100956 return 0;
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +0200957
958fail:
Paolo Bonzinid1833ef2014-02-17 14:44:03 +0100959 g_free(buf);
Fam Zhengf4c129a2013-10-31 10:06:23 +0800960 g_free(s->create_type);
961 s->create_type = NULL;
Paolo Bonzinibae0a0c2011-10-20 13:16:20 +0200962 vmdk_free_extents(bs);
963 return ret;
bellardea2384d2004-08-01 21:59:26 +0000964}
965
Kevin Wolfd34682c2013-12-11 19:26:16 +0100966
Kevin Wolf3baca892014-07-16 17:48:16 +0200967static void vmdk_refresh_limits(BlockDriverState *bs, Error **errp)
Kevin Wolfd34682c2013-12-11 19:26:16 +0100968{
969 BDRVVmdkState *s = bs->opaque;
970 int i;
971
972 for (i = 0; i < s->num_extents; i++) {
973 if (!s->extents[i].flat) {
974 bs->bl.write_zeroes_alignment =
975 MAX(bs->bl.write_zeroes_alignment,
976 s->extents[i].cluster_sectors);
977 }
978 }
Kevin Wolfd34682c2013-12-11 19:26:16 +0100979}
980
Fam Zhengc6ac36e2014-07-30 14:39:10 +0800981/**
982 * get_whole_cluster
983 *
984 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
985 * to the cluster at @cluster_sector_num.
986 *
987 * If @skip_start_sector < @skip_end_sector, the relative range
988 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
989 * it for call to write user data in the request.
990 */
Fam Zhengb3976d32011-07-12 19:56:28 +0800991static int get_whole_cluster(BlockDriverState *bs,
Fam Zhengc6ac36e2014-07-30 14:39:10 +0800992 VmdkExtent *extent,
993 uint64_t cluster_sector_num,
994 uint64_t sector_num,
995 uint64_t skip_start_sector,
996 uint64_t skip_end_sector)
ths5f4da8c2007-01-24 21:05:24 +0000997{
Fam Zhengbf815072013-08-06 15:44:54 +0800998 int ret = VMDK_OK;
Fam Zhengc6ac36e2014-07-30 14:39:10 +0800999 int64_t cluster_bytes;
1000 uint8_t *whole_grain;
ths5f4da8c2007-01-24 21:05:24 +00001001
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001002 /* For COW, align request sector_num to cluster start */
1003 sector_num = QEMU_ALIGN_DOWN(sector_num, extent->cluster_sectors);
1004 cluster_bytes = extent->cluster_sectors << BDRV_SECTOR_BITS;
1005 whole_grain = qemu_blockalign(bs, cluster_bytes);
1006
1007 if (!bs->backing_hd) {
1008 memset(whole_grain, 0, skip_start_sector << BDRV_SECTOR_BITS);
1009 memset(whole_grain + (skip_end_sector << BDRV_SECTOR_BITS), 0,
1010 cluster_bytes - (skip_end_sector << BDRV_SECTOR_BITS));
1011 }
1012
1013 assert(skip_end_sector <= extent->cluster_sectors);
Fam Zheng0e69c542011-07-12 19:56:29 +08001014 /* we will be here if it's first write on non-exist grain(cluster).
1015 * try to read from parent image, if exist */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001016 if (bs->backing_hd && !vmdk_is_cid_valid(bs)) {
1017 ret = VMDK_ERROR;
1018 goto exit;
1019 }
ths5f4da8c2007-01-24 21:05:24 +00001020
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001021 /* Read backing data before skip range */
1022 if (skip_start_sector > 0) {
1023 if (bs->backing_hd) {
1024 ret = bdrv_read(bs->backing_hd, sector_num,
1025 whole_grain, skip_start_sector);
1026 if (ret < 0) {
1027 ret = VMDK_ERROR;
1028 goto exit;
1029 }
Kevin Wolfc3365002010-04-16 19:28:14 +02001030 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001031 ret = bdrv_write(extent->file, cluster_sector_num, whole_grain,
1032 skip_start_sector);
Kevin Wolfc3365002010-04-16 19:28:14 +02001033 if (ret < 0) {
Fam Zhengbf815072013-08-06 15:44:54 +08001034 ret = VMDK_ERROR;
1035 goto exit;
ths630530a2007-06-18 15:01:30 +00001036 }
ths5f4da8c2007-01-24 21:05:24 +00001037 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001038 /* Read backing data after skip range */
1039 if (skip_end_sector < extent->cluster_sectors) {
1040 if (bs->backing_hd) {
1041 ret = bdrv_read(bs->backing_hd, sector_num + skip_end_sector,
1042 whole_grain + (skip_end_sector << BDRV_SECTOR_BITS),
1043 extent->cluster_sectors - skip_end_sector);
1044 if (ret < 0) {
1045 ret = VMDK_ERROR;
1046 goto exit;
1047 }
1048 }
1049 ret = bdrv_write(extent->file, cluster_sector_num + skip_end_sector,
1050 whole_grain + (skip_end_sector << BDRV_SECTOR_BITS),
1051 extent->cluster_sectors - skip_end_sector);
1052 if (ret < 0) {
1053 ret = VMDK_ERROR;
1054 goto exit;
1055 }
1056 }
1057
Fam Zhengbf815072013-08-06 15:44:54 +08001058exit:
1059 qemu_vfree(whole_grain);
1060 return ret;
ths5f4da8c2007-01-24 21:05:24 +00001061}
1062
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001063static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data,
1064 uint32_t offset)
ths630530a2007-06-18 15:01:30 +00001065{
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001066 offset = cpu_to_le32(offset);
ths630530a2007-06-18 15:01:30 +00001067 /* update L2 table */
Fam Zhengb3976d32011-07-12 19:56:28 +08001068 if (bdrv_pwrite_sync(
1069 extent->file,
1070 ((int64_t)m_data->l2_offset * 512)
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001071 + (m_data->l2_index * sizeof(offset)),
Fam Zhenge304e8e2013-05-02 10:25:26 +08001072 &offset, sizeof(offset)) < 0) {
Fam Zheng65f74722013-05-02 10:25:22 +08001073 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001074 }
ths630530a2007-06-18 15:01:30 +00001075 /* update backup L2 table */
Fam Zhengb3976d32011-07-12 19:56:28 +08001076 if (extent->l1_backup_table_offset != 0) {
1077 m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
1078 if (bdrv_pwrite_sync(
1079 extent->file,
1080 ((int64_t)m_data->l2_offset * 512)
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001081 + (m_data->l2_index * sizeof(offset)),
Fam Zhenge304e8e2013-05-02 10:25:26 +08001082 &offset, sizeof(offset)) < 0) {
Fam Zheng65f74722013-05-02 10:25:22 +08001083 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001084 }
ths630530a2007-06-18 15:01:30 +00001085 }
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001086 if (m_data->l2_cache_entry) {
1087 *m_data->l2_cache_entry = offset;
1088 }
ths630530a2007-06-18 15:01:30 +00001089
Fam Zheng65f74722013-05-02 10:25:22 +08001090 return VMDK_OK;
ths630530a2007-06-18 15:01:30 +00001091}
1092
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001093/**
1094 * get_cluster_offset
1095 *
1096 * Look up cluster offset in extent file by sector number, and store in
1097 * @cluster_offset.
1098 *
1099 * For flat extents, the start offset as parsed from the description file is
1100 * returned.
1101 *
1102 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1103 * offset for a new cluster and update L2 cache. If there is a backing file,
1104 * COW is done before returning; otherwise, zeroes are written to the allocated
1105 * cluster. Both COW and zero writing skips the sector range
1106 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1107 * has new data to write there.
1108 *
1109 * Returns: VMDK_OK if cluster exists and mapped in the image.
1110 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1111 * VMDK_ERROR if failed.
1112 */
Fam Zheng91b85bd2011-07-12 19:56:35 +08001113static int get_cluster_offset(BlockDriverState *bs,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001114 VmdkExtent *extent,
1115 VmdkMetaData *m_data,
1116 uint64_t offset,
1117 bool allocate,
1118 uint64_t *cluster_offset,
1119 uint64_t skip_start_sector,
1120 uint64_t skip_end_sector)
bellardea2384d2004-08-01 21:59:26 +00001121{
bellardea2384d2004-08-01 21:59:26 +00001122 unsigned int l1_index, l2_offset, l2_index;
1123 int min_index, i, j;
Fam Zhenge304e8e2013-05-02 10:25:26 +08001124 uint32_t min_count, *l2_table;
Fam Zheng14ead642013-05-02 10:25:23 +08001125 bool zeroed = false;
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001126 int64_t ret;
Fam Zhengd1319b02014-09-23 09:56:21 +08001127 int64_t cluster_sector;
ths630530a2007-06-18 15:01:30 +00001128
Fam Zhengae261c82011-07-12 19:56:38 +08001129 if (m_data) {
ths630530a2007-06-18 15:01:30 +00001130 m_data->valid = 0;
Fam Zhengae261c82011-07-12 19:56:38 +08001131 }
Fam Zheng91b85bd2011-07-12 19:56:35 +08001132 if (extent->flat) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001133 *cluster_offset = extent->flat_start_offset;
Fam Zheng65f74722013-05-02 10:25:22 +08001134 return VMDK_OK;
Fam Zheng91b85bd2011-07-12 19:56:35 +08001135 }
ths630530a2007-06-18 15:01:30 +00001136
Fam Zheng6398de52011-08-12 23:19:27 +08001137 offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
Fam Zhengb3976d32011-07-12 19:56:28 +08001138 l1_index = (offset >> 9) / extent->l1_entry_sectors;
1139 if (l1_index >= extent->l1_size) {
Fam Zheng65f74722013-05-02 10:25:22 +08001140 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001141 }
1142 l2_offset = extent->l1_table[l1_index];
1143 if (!l2_offset) {
Fam Zheng65f74722013-05-02 10:25:22 +08001144 return VMDK_UNALLOC;
Fam Zhengb3976d32011-07-12 19:56:28 +08001145 }
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001146 for (i = 0; i < L2_CACHE_SIZE; i++) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001147 if (l2_offset == extent->l2_cache_offsets[i]) {
bellardea2384d2004-08-01 21:59:26 +00001148 /* increment the hit count */
Fam Zhengb3976d32011-07-12 19:56:28 +08001149 if (++extent->l2_cache_counts[i] == 0xffffffff) {
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001150 for (j = 0; j < L2_CACHE_SIZE; j++) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001151 extent->l2_cache_counts[j] >>= 1;
bellardea2384d2004-08-01 21:59:26 +00001152 }
1153 }
Fam Zhengb3976d32011-07-12 19:56:28 +08001154 l2_table = extent->l2_cache + (i * extent->l2_size);
bellardea2384d2004-08-01 21:59:26 +00001155 goto found;
1156 }
1157 }
1158 /* not found: load a new entry in the least used one */
1159 min_index = 0;
1160 min_count = 0xffffffff;
Fam Zhengb4b3ab12011-07-12 19:56:31 +08001161 for (i = 0; i < L2_CACHE_SIZE; i++) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001162 if (extent->l2_cache_counts[i] < min_count) {
1163 min_count = extent->l2_cache_counts[i];
bellardea2384d2004-08-01 21:59:26 +00001164 min_index = i;
1165 }
1166 }
Fam Zhengb3976d32011-07-12 19:56:28 +08001167 l2_table = extent->l2_cache + (min_index * extent->l2_size);
1168 if (bdrv_pread(
1169 extent->file,
1170 (int64_t)l2_offset * 512,
1171 l2_table,
1172 extent->l2_size * sizeof(uint32_t)
1173 ) != extent->l2_size * sizeof(uint32_t)) {
Fam Zheng65f74722013-05-02 10:25:22 +08001174 return VMDK_ERROR;
Fam Zhengb3976d32011-07-12 19:56:28 +08001175 }
ths5f4da8c2007-01-24 21:05:24 +00001176
Fam Zhengb3976d32011-07-12 19:56:28 +08001177 extent->l2_cache_offsets[min_index] = l2_offset;
1178 extent->l2_cache_counts[min_index] = 1;
bellardea2384d2004-08-01 21:59:26 +00001179 found:
Fam Zhengb3976d32011-07-12 19:56:28 +08001180 l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001181 cluster_sector = le32_to_cpu(l2_table[l2_index]);
ths630530a2007-06-18 15:01:30 +00001182
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001183 if (m_data) {
1184 m_data->valid = 1;
1185 m_data->l1_index = l1_index;
1186 m_data->l2_index = l2_index;
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001187 m_data->l2_offset = l2_offset;
1188 m_data->l2_cache_entry = &l2_table[l2_index];
1189 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001190 if (extent->has_zero_grain && cluster_sector == VMDK_GTE_ZEROED) {
Fam Zheng14ead642013-05-02 10:25:23 +08001191 zeroed = true;
1192 }
1193
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001194 if (!cluster_sector || zeroed) {
Fam Zheng91b85bd2011-07-12 19:56:35 +08001195 if (!allocate) {
Fam Zheng14ead642013-05-02 10:25:23 +08001196 return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
Fam Zheng91b85bd2011-07-12 19:56:35 +08001197 }
ths5f4da8c2007-01-24 21:05:24 +00001198
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001199 cluster_sector = extent->next_cluster_sector;
1200 extent->next_cluster_sector += extent->cluster_sectors;
Kevin Wolf9949f972010-04-16 21:07:19 +02001201
ths630530a2007-06-18 15:01:30 +00001202 /* First of all we write grain itself, to avoid race condition
1203 * that may to corrupt the image.
1204 * This problem may occur because of insufficient space on host disk
1205 * or inappropriate VM shutdown.
1206 */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001207 ret = get_whole_cluster(bs, extent,
1208 cluster_sector,
1209 offset >> BDRV_SECTOR_BITS,
1210 skip_start_sector, skip_end_sector);
1211 if (ret) {
1212 return ret;
ths630530a2007-06-18 15:01:30 +00001213 }
bellardff1afc72005-04-26 21:08:00 +00001214 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001215 *cluster_offset = cluster_sector << BDRV_SECTOR_BITS;
Fam Zheng65f74722013-05-02 10:25:22 +08001216 return VMDK_OK;
bellardea2384d2004-08-01 21:59:26 +00001217}
1218
Fam Zhengb3976d32011-07-12 19:56:28 +08001219static VmdkExtent *find_extent(BDRVVmdkState *s,
1220 int64_t sector_num, VmdkExtent *start_hint)
1221{
1222 VmdkExtent *extent = start_hint;
1223
1224 if (!extent) {
1225 extent = &s->extents[0];
1226 }
1227 while (extent < &s->extents[s->num_extents]) {
1228 if (sector_num < extent->end_sector) {
1229 return extent;
1230 }
1231 extent++;
1232 }
1233 return NULL;
1234}
1235
Fam Zheng61f0ed12015-06-04 14:02:56 +08001236static inline uint64_t vmdk_find_index_in_cluster(VmdkExtent *extent,
1237 int64_t sector_num)
1238{
1239 uint64_t index_in_cluster, extent_begin_sector, extent_relative_sector_num;
1240
1241 extent_begin_sector = extent->end_sector - extent->sectors;
1242 extent_relative_sector_num = sector_num - extent_begin_sector;
1243 index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
1244 return index_in_cluster;
1245}
1246
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02001247static int64_t coroutine_fn vmdk_co_get_block_status(BlockDriverState *bs,
Stefan Hajnoczif8a2e5e2011-11-14 12:44:21 +00001248 int64_t sector_num, int nb_sectors, int *pnum)
bellardea2384d2004-08-01 21:59:26 +00001249{
1250 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3976d32011-07-12 19:56:28 +08001251 int64_t index_in_cluster, n, ret;
1252 uint64_t offset;
1253 VmdkExtent *extent;
1254
1255 extent = find_extent(s, sector_num, NULL);
1256 if (!extent) {
1257 return 0;
1258 }
Stefan Hajnoczif8a2e5e2011-11-14 12:44:21 +00001259 qemu_co_mutex_lock(&s->lock);
Fam Zheng91b85bd2011-07-12 19:56:35 +08001260 ret = get_cluster_offset(bs, extent, NULL,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001261 sector_num * 512, false, &offset,
1262 0, 0);
Stefan Hajnoczif8a2e5e2011-11-14 12:44:21 +00001263 qemu_co_mutex_unlock(&s->lock);
Fam Zheng14ead642013-05-02 10:25:23 +08001264
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02001265 switch (ret) {
1266 case VMDK_ERROR:
1267 ret = -EIO;
1268 break;
1269 case VMDK_UNALLOC:
1270 ret = 0;
1271 break;
1272 case VMDK_ZEROED:
1273 ret = BDRV_BLOCK_ZERO;
1274 break;
1275 case VMDK_OK:
1276 ret = BDRV_BLOCK_DATA;
Peter Lieven3eba13e2014-02-26 10:47:57 +01001277 if (extent->file == bs->file && !extent->compressed) {
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02001278 ret |= BDRV_BLOCK_OFFSET_VALID | offset;
1279 }
1280
1281 break;
1282 }
Fam Zheng91b85bd2011-07-12 19:56:35 +08001283
Fam Zheng61f0ed12015-06-04 14:02:56 +08001284 index_in_cluster = vmdk_find_index_in_cluster(extent, sector_num);
Fam Zheng91b85bd2011-07-12 19:56:35 +08001285 n = extent->cluster_sectors - index_in_cluster;
Fam Zhengae261c82011-07-12 19:56:38 +08001286 if (n > nb_sectors) {
bellardea2384d2004-08-01 21:59:26 +00001287 n = nb_sectors;
Fam Zhengae261c82011-07-12 19:56:38 +08001288 }
bellardea2384d2004-08-01 21:59:26 +00001289 *pnum = n;
Fam Zhengb3976d32011-07-12 19:56:28 +08001290 return ret;
bellardea2384d2004-08-01 21:59:26 +00001291}
1292
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001293static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
1294 int64_t offset_in_cluster, const uint8_t *buf,
1295 int nb_sectors, int64_t sector_num)
1296{
1297 int ret;
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001298 VmdkGrainMarker *data = NULL;
1299 uLongf buf_len;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001300 const uint8_t *write_buf = buf;
1301 int write_len = nb_sectors * 512;
Fam Zheng5e82a312015-05-06 20:23:46 +08001302 int64_t write_offset;
1303 int64_t write_end_sector;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001304
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001305 if (extent->compressed) {
1306 if (!extent->has_marker) {
1307 ret = -EINVAL;
1308 goto out;
1309 }
1310 buf_len = (extent->cluster_sectors << 9) * 2;
1311 data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
1312 if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK ||
1313 buf_len == 0) {
1314 ret = -EINVAL;
1315 goto out;
1316 }
1317 data->lba = sector_num;
1318 data->size = buf_len;
1319 write_buf = (uint8_t *)data;
1320 write_len = buf_len + sizeof(VmdkGrainMarker);
1321 }
Fam Zheng5e82a312015-05-06 20:23:46 +08001322 write_offset = cluster_offset + offset_in_cluster,
1323 ret = bdrv_pwrite(extent->file, write_offset, write_buf, write_len);
1324
1325 write_end_sector = DIV_ROUND_UP(write_offset + write_len, BDRV_SECTOR_SIZE);
1326
1327 extent->next_cluster_sector = MAX(extent->next_cluster_sector,
1328 write_end_sector);
1329
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001330 if (ret != write_len) {
1331 ret = ret < 0 ? ret : -EIO;
1332 goto out;
1333 }
1334 ret = 0;
1335 out:
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001336 g_free(data);
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001337 return ret;
1338}
1339
1340static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
1341 int64_t offset_in_cluster, uint8_t *buf,
1342 int nb_sectors)
1343{
1344 int ret;
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001345 int cluster_bytes, buf_bytes;
1346 uint8_t *cluster_buf, *compressed_data;
1347 uint8_t *uncomp_buf;
1348 uint32_t data_len;
1349 VmdkGrainMarker *marker;
1350 uLongf buf_len;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001351
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001352
1353 if (!extent->compressed) {
1354 ret = bdrv_pread(extent->file,
1355 cluster_offset + offset_in_cluster,
1356 buf, nb_sectors * 512);
1357 if (ret == nb_sectors * 512) {
1358 return 0;
1359 } else {
1360 return -EIO;
1361 }
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001362 }
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001363 cluster_bytes = extent->cluster_sectors * 512;
1364 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1365 buf_bytes = cluster_bytes * 2;
1366 cluster_buf = g_malloc(buf_bytes);
1367 uncomp_buf = g_malloc(cluster_bytes);
1368 ret = bdrv_pread(extent->file,
1369 cluster_offset,
1370 cluster_buf, buf_bytes);
1371 if (ret < 0) {
1372 goto out;
1373 }
1374 compressed_data = cluster_buf;
1375 buf_len = cluster_bytes;
1376 data_len = cluster_bytes;
1377 if (extent->has_marker) {
1378 marker = (VmdkGrainMarker *)cluster_buf;
1379 compressed_data = marker->data;
1380 data_len = le32_to_cpu(marker->size);
1381 }
1382 if (!data_len || data_len > buf_bytes) {
1383 ret = -EINVAL;
1384 goto out;
1385 }
1386 ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1387 if (ret != Z_OK) {
1388 ret = -EINVAL;
1389 goto out;
1390
1391 }
1392 if (offset_in_cluster < 0 ||
1393 offset_in_cluster + nb_sectors * 512 > buf_len) {
1394 ret = -EINVAL;
1395 goto out;
1396 }
1397 memcpy(buf, uncomp_buf + offset_in_cluster, nb_sectors * 512);
1398 ret = 0;
1399
1400 out:
1401 g_free(uncomp_buf);
1402 g_free(cluster_buf);
1403 return ret;
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001404}
1405
ths5fafdf22007-09-16 21:08:06 +00001406static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
bellardea2384d2004-08-01 21:59:26 +00001407 uint8_t *buf, int nb_sectors)
1408{
1409 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3976d32011-07-12 19:56:28 +08001410 int ret;
1411 uint64_t n, index_in_cluster;
1412 VmdkExtent *extent = NULL;
bellardea2384d2004-08-01 21:59:26 +00001413 uint64_t cluster_offset;
ths5f4da8c2007-01-24 21:05:24 +00001414
bellardea2384d2004-08-01 21:59:26 +00001415 while (nb_sectors > 0) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001416 extent = find_extent(s, sector_num, extent);
1417 if (!extent) {
1418 return -EIO;
1419 }
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001420 ret = get_cluster_offset(bs, extent, NULL,
1421 sector_num << 9, false, &cluster_offset,
1422 0, 0);
Fam Zheng90df6012015-06-04 14:02:57 +08001423 index_in_cluster = vmdk_find_index_in_cluster(extent, sector_num);
Fam Zhengb3976d32011-07-12 19:56:28 +08001424 n = extent->cluster_sectors - index_in_cluster;
Fam Zhengae261c82011-07-12 19:56:38 +08001425 if (n > nb_sectors) {
bellardea2384d2004-08-01 21:59:26 +00001426 n = nb_sectors;
Fam Zhengae261c82011-07-12 19:56:38 +08001427 }
Fam Zheng14ead642013-05-02 10:25:23 +08001428 if (ret != VMDK_OK) {
Fam Zheng91b85bd2011-07-12 19:56:35 +08001429 /* if not allocated, try to read from parent image, if exist */
Fam Zheng14ead642013-05-02 10:25:23 +08001430 if (bs->backing_hd && ret != VMDK_ZEROED) {
Fam Zhengae261c82011-07-12 19:56:38 +08001431 if (!vmdk_is_cid_valid(bs)) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001432 return -EINVAL;
Fam Zhengae261c82011-07-12 19:56:38 +08001433 }
Kevin Wolfb1712712009-07-17 08:20:41 +02001434 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
Fam Zhengae261c82011-07-12 19:56:38 +08001435 if (ret < 0) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001436 return ret;
Fam Zhengae261c82011-07-12 19:56:38 +08001437 }
ths5f4da8c2007-01-24 21:05:24 +00001438 } else {
1439 memset(buf, 0, 512 * n);
1440 }
bellardea2384d2004-08-01 21:59:26 +00001441 } else {
Fam Zhengdd3f6ee2011-08-12 23:19:29 +08001442 ret = vmdk_read_extent(extent,
1443 cluster_offset, index_in_cluster * 512,
1444 buf, n);
1445 if (ret) {
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001446 return ret;
1447 }
bellardea2384d2004-08-01 21:59:26 +00001448 }
1449 nb_sectors -= n;
1450 sector_num += n;
1451 buf += n * 512;
1452 }
1453 return 0;
1454}
1455
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001456static coroutine_fn int vmdk_co_read(BlockDriverState *bs, int64_t sector_num,
1457 uint8_t *buf, int nb_sectors)
1458{
1459 int ret;
1460 BDRVVmdkState *s = bs->opaque;
1461 qemu_co_mutex_lock(&s->lock);
1462 ret = vmdk_read(bs, sector_num, buf, nb_sectors);
1463 qemu_co_mutex_unlock(&s->lock);
1464 return ret;
1465}
1466
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001467/**
1468 * vmdk_write:
1469 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
Fam Zheng8e507242013-08-01 18:12:17 +08001470 * if possible, otherwise return -ENOTSUP.
1471 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1472 * with each cluster. By dry run we can find if the zero write
1473 * is possible without modifying image data.
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001474 *
1475 * Returns: error code with 0 for success.
1476 */
ths5fafdf22007-09-16 21:08:06 +00001477static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001478 const uint8_t *buf, int nb_sectors,
1479 bool zeroed, bool zero_dry_run)
bellardea2384d2004-08-01 21:59:26 +00001480{
bellardff1afc72005-04-26 21:08:00 +00001481 BDRVVmdkState *s = bs->opaque;
Fam Zhengb3976d32011-07-12 19:56:28 +08001482 VmdkExtent *extent = NULL;
Fam Zheng585ea0c2014-01-08 09:42:07 +08001483 int ret;
1484 int64_t index_in_cluster, n;
bellardff1afc72005-04-26 21:08:00 +00001485 uint64_t cluster_offset;
Fam Zhengb3976d32011-07-12 19:56:28 +08001486 VmdkMetaData m_data;
bellardff1afc72005-04-26 21:08:00 +00001487
ths630530a2007-06-18 15:01:30 +00001488 if (sector_num > bs->total_sectors) {
Fam Zheng48239702013-10-11 15:43:22 +08001489 error_report("Wrong offset: sector_num=0x%" PRIx64
j_mayer92868412007-09-21 06:09:39 +00001490 " total_sectors=0x%" PRIx64 "\n",
ths630530a2007-06-18 15:01:30 +00001491 sector_num, bs->total_sectors);
Fam Zheng7fa60fa2011-07-19 08:38:22 +08001492 return -EIO;
ths630530a2007-06-18 15:01:30 +00001493 }
1494
bellardff1afc72005-04-26 21:08:00 +00001495 while (nb_sectors > 0) {
Fam Zhengb3976d32011-07-12 19:56:28 +08001496 extent = find_extent(s, sector_num, extent);
1497 if (!extent) {
1498 return -EIO;
1499 }
Fam Zheng90df6012015-06-04 14:02:57 +08001500 index_in_cluster = vmdk_find_index_in_cluster(extent, sector_num);
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001501 n = extent->cluster_sectors - index_in_cluster;
1502 if (n > nb_sectors) {
1503 n = nb_sectors;
1504 }
1505 ret = get_cluster_offset(bs, extent, &m_data, sector_num << 9,
1506 !(extent->compressed || zeroed),
1507 &cluster_offset,
1508 index_in_cluster, index_in_cluster + n);
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001509 if (extent->compressed) {
Fam Zheng65f74722013-05-02 10:25:22 +08001510 if (ret == VMDK_OK) {
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001511 /* Refuse write to allocated cluster for streamOptimized */
Fam Zheng48239702013-10-11 15:43:22 +08001512 error_report("Could not write to allocated cluster"
1513 " for streamOptimized");
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001514 return -EIO;
1515 } else {
1516 /* allocate */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001517 ret = get_cluster_offset(bs, extent, &m_data, sector_num << 9,
1518 true, &cluster_offset, 0, 0);
Fam Zheng2b2c8c52011-08-12 23:19:31 +08001519 }
1520 }
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001521 if (ret == VMDK_ERROR) {
Fam Zheng91b85bd2011-07-12 19:56:35 +08001522 return -EINVAL;
Fam Zhengb3976d32011-07-12 19:56:28 +08001523 }
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001524 if (zeroed) {
1525 /* Do zeroed write, buf is ignored */
1526 if (extent->has_zero_grain &&
1527 index_in_cluster == 0 &&
1528 n >= extent->cluster_sectors) {
1529 n = extent->cluster_sectors;
1530 if (!zero_dry_run) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001531 /* update L2 tables */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001532 if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED)
1533 != VMDK_OK) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001534 return -EIO;
1535 }
1536 }
1537 } else {
1538 return -ENOTSUP;
1539 }
1540 } else {
1541 ret = vmdk_write_extent(extent,
1542 cluster_offset, index_in_cluster * 512,
1543 buf, n, sector_num);
1544 if (ret) {
1545 return ret;
1546 }
1547 if (m_data.valid) {
1548 /* update L2 tables */
Fam Zhengc6ac36e2014-07-30 14:39:10 +08001549 if (vmdk_L2update(extent, &m_data,
1550 cluster_offset >> BDRV_SECTOR_BITS)
1551 != VMDK_OK) {
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001552 return -EIO;
1553 }
Fam Zhengb3976d32011-07-12 19:56:28 +08001554 }
ths630530a2007-06-18 15:01:30 +00001555 }
bellardff1afc72005-04-26 21:08:00 +00001556 nb_sectors -= n;
1557 sector_num += n;
1558 buf += n * 512;
ths5f4da8c2007-01-24 21:05:24 +00001559
Fam Zhengae261c82011-07-12 19:56:38 +08001560 /* update CID on the first write every time the virtual disk is
1561 * opened */
Fam Zheng69b4d862011-07-12 19:56:34 +08001562 if (!s->cid_updated) {
Fam Zhenge5dc64b2014-12-04 07:28:29 +08001563 ret = vmdk_write_cid(bs, g_random_int());
Kevin Wolf99f18352011-10-26 12:25:25 +02001564 if (ret < 0) {
1565 return ret;
1566 }
Fam Zheng69b4d862011-07-12 19:56:34 +08001567 s->cid_updated = true;
ths5f4da8c2007-01-24 21:05:24 +00001568 }
bellardff1afc72005-04-26 21:08:00 +00001569 }
1570 return 0;
bellardea2384d2004-08-01 21:59:26 +00001571}
1572
Paolo Bonzinie183ef72011-10-20 13:16:23 +02001573static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num,
1574 const uint8_t *buf, int nb_sectors)
1575{
1576 int ret;
1577 BDRVVmdkState *s = bs->opaque;
1578 qemu_co_mutex_lock(&s->lock);
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001579 ret = vmdk_write(bs, sector_num, buf, nb_sectors, false, false);
1580 qemu_co_mutex_unlock(&s->lock);
1581 return ret;
1582}
1583
Fam Zhengba0ad892014-05-06 21:08:44 +08001584static int vmdk_write_compressed(BlockDriverState *bs,
1585 int64_t sector_num,
1586 const uint8_t *buf,
1587 int nb_sectors)
1588{
1589 BDRVVmdkState *s = bs->opaque;
1590 if (s->num_extents == 1 && s->extents[0].compressed) {
1591 return vmdk_write(bs, sector_num, buf, nb_sectors, false, false);
1592 } else {
1593 return -ENOTSUP;
1594 }
1595}
1596
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001597static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
1598 int64_t sector_num,
Peter Lievenaa7bfbf2013-10-24 12:06:51 +02001599 int nb_sectors,
1600 BdrvRequestFlags flags)
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001601{
1602 int ret;
1603 BDRVVmdkState *s = bs->opaque;
1604 qemu_co_mutex_lock(&s->lock);
Fam Zheng8e507242013-08-01 18:12:17 +08001605 /* write zeroes could fail if sectors not aligned to cluster, test it with
1606 * dry_run == true before really updating image */
Fam Zhengcdeaf1f2013-05-02 10:25:27 +08001607 ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, true);
1608 if (!ret) {
1609 ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, false);
1610 }
Paolo Bonzinie183ef72011-10-20 13:16:23 +02001611 qemu_co_mutex_unlock(&s->lock);
1612 return ret;
1613}
1614
Fam Zheng6c031aa2011-08-12 23:19:32 +08001615static int vmdk_create_extent(const char *filename, int64_t filesize,
Fam Zheng917703c2013-12-20 09:48:48 +08001616 bool flat, bool compress, bool zeroed_grain,
Chunyan Liu4ab15592014-06-30 14:29:58 +08001617 QemuOpts *opts, Error **errp)
bellard8979b222005-07-02 14:02:54 +00001618{
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001619 int ret, i;
Fam Zheng917703c2013-12-20 09:48:48 +08001620 BlockDriverState *bs = NULL;
bellard8979b222005-07-02 14:02:54 +00001621 VMDK4Header header;
Fam Zhengc13959c2014-05-28 11:38:58 +08001622 Error *local_err = NULL;
Fam Zheng917703c2013-12-20 09:48:48 +08001623 uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
1624 uint32_t *gd_buf = NULL;
1625 int gd_buf_size;
Kevin Wolf0e7e1982009-05-18 16:42:10 +02001626
Chunyan Liu4ab15592014-06-30 14:29:58 +08001627 ret = bdrv_create_file(filename, opts, &local_err);
Fam Zheng917703c2013-12-20 09:48:48 +08001628 if (ret < 0) {
1629 error_propagate(errp, local_err);
1630 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001631 }
Fam Zheng917703c2013-12-20 09:48:48 +08001632
Max Reitz2e401342014-02-18 18:33:07 +01001633 assert(bs == NULL);
1634 ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
Max Reitz6ebf9aa2015-08-26 19:47:49 +02001635 &local_err);
Fam Zheng917703c2013-12-20 09:48:48 +08001636 if (ret < 0) {
1637 error_propagate(errp, local_err);
1638 goto exit;
1639 }
1640
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001641 if (flat) {
Fam Zheng917703c2013-12-20 09:48:48 +08001642 ret = bdrv_truncate(bs, filesize);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001643 if (ret < 0) {
Jeff Cody39a611a2014-02-12 14:46:24 -05001644 error_setg_errno(errp, -ret, "Could not truncate file");
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001645 }
1646 goto exit;
1647 }
bellard8979b222005-07-02 14:02:54 +00001648 magic = cpu_to_be32(VMDK4_MAGIC);
1649 memset(&header, 0, sizeof(header));
Fam Zheng69e0b6d2013-05-02 10:25:24 +08001650 header.version = zeroed_grain ? 2 : 1;
Fam Zheng95b0aa42013-05-02 10:25:25 +08001651 header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
Fam Zheng69e0b6d2013-05-02 10:25:24 +08001652 | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
1653 | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
Fam Zheng6c031aa2011-08-12 23:19:32 +08001654 header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
Fam Zheng917703c2013-12-20 09:48:48 +08001655 header.capacity = filesize / BDRV_SECTOR_SIZE;
Alexander Graf16372ff2011-05-25 00:46:55 +02001656 header.granularity = 128;
Fam Zheng917703c2013-12-20 09:48:48 +08001657 header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
bellard8979b222005-07-02 14:02:54 +00001658
Fam Zheng917703c2013-12-20 09:48:48 +08001659 grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
1660 gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
1661 BDRV_SECTOR_SIZE);
1662 gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
1663 gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
bellard8979b222005-07-02 14:02:54 +00001664
1665 header.desc_offset = 1;
1666 header.desc_size = 20;
1667 header.rgd_offset = header.desc_offset + header.desc_size;
Fam Zheng917703c2013-12-20 09:48:48 +08001668 header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
bellard8979b222005-07-02 14:02:54 +00001669 header.grain_offset =
Fam Zheng917703c2013-12-20 09:48:48 +08001670 ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
1671 header.granularity);
Alexander Graf16372ff2011-05-25 00:46:55 +02001672 /* swap endianness for all header fields */
1673 header.version = cpu_to_le32(header.version);
1674 header.flags = cpu_to_le32(header.flags);
1675 header.capacity = cpu_to_le64(header.capacity);
1676 header.granularity = cpu_to_le64(header.granularity);
Fam Zhengca8804c2013-08-06 15:44:55 +08001677 header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
bellard8979b222005-07-02 14:02:54 +00001678 header.desc_offset = cpu_to_le64(header.desc_offset);
1679 header.desc_size = cpu_to_le64(header.desc_size);
1680 header.rgd_offset = cpu_to_le64(header.rgd_offset);
1681 header.gd_offset = cpu_to_le64(header.gd_offset);
1682 header.grain_offset = cpu_to_le64(header.grain_offset);
Fam Zheng6c031aa2011-08-12 23:19:32 +08001683 header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
bellard8979b222005-07-02 14:02:54 +00001684
1685 header.check_bytes[0] = 0xa;
1686 header.check_bytes[1] = 0x20;
1687 header.check_bytes[2] = 0xd;
1688 header.check_bytes[3] = 0xa;
ths3b46e622007-09-17 08:09:54 +00001689
1690 /* write all the data */
Fam Zheng917703c2013-12-20 09:48:48 +08001691 ret = bdrv_pwrite(bs, 0, &magic, sizeof(magic));
1692 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001693 error_setg(errp, QERR_IO_ERROR);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001694 goto exit;
1695 }
Fam Zheng917703c2013-12-20 09:48:48 +08001696 ret = bdrv_pwrite(bs, sizeof(magic), &header, sizeof(header));
1697 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001698 error_setg(errp, QERR_IO_ERROR);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001699 goto exit;
1700 }
bellard8979b222005-07-02 14:02:54 +00001701
Fam Zheng917703c2013-12-20 09:48:48 +08001702 ret = bdrv_truncate(bs, le64_to_cpu(header.grain_offset) << 9);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001703 if (ret < 0) {
Jeff Cody39a611a2014-02-12 14:46:24 -05001704 error_setg_errno(errp, -ret, "Could not truncate file");
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001705 goto exit;
1706 }
bellard8979b222005-07-02 14:02:54 +00001707
1708 /* write grain directory */
Fam Zheng917703c2013-12-20 09:48:48 +08001709 gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
1710 gd_buf = g_malloc0(gd_buf_size);
1711 for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001712 i < gt_count; i++, tmp += gt_size) {
Fam Zheng917703c2013-12-20 09:48:48 +08001713 gd_buf[i] = cpu_to_le32(tmp);
1714 }
1715 ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
1716 gd_buf, gd_buf_size);
1717 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001718 error_setg(errp, QERR_IO_ERROR);
Fam Zheng917703c2013-12-20 09:48:48 +08001719 goto exit;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001720 }
ths3b46e622007-09-17 08:09:54 +00001721
bellard8979b222005-07-02 14:02:54 +00001722 /* write backup grain directory */
Fam Zheng917703c2013-12-20 09:48:48 +08001723 for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001724 i < gt_count; i++, tmp += gt_size) {
Fam Zheng917703c2013-12-20 09:48:48 +08001725 gd_buf[i] = cpu_to_le32(tmp);
1726 }
1727 ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
1728 gd_buf, gd_buf_size);
1729 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001730 error_setg(errp, QERR_IO_ERROR);
Fam Zheng917703c2013-12-20 09:48:48 +08001731 goto exit;
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001732 }
bellard8979b222005-07-02 14:02:54 +00001733
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001734 ret = 0;
Fam Zheng917703c2013-12-20 09:48:48 +08001735exit:
1736 if (bs) {
1737 bdrv_unref(bs);
1738 }
1739 g_free(gd_buf);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001740 return ret;
1741}
bellard8979b222005-07-02 14:02:54 +00001742
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001743static int filename_decompose(const char *filename, char *path, char *prefix,
Fam Zheng48239702013-10-11 15:43:22 +08001744 char *postfix, size_t buf_len, Error **errp)
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001745{
1746 const char *p, *q;
1747
1748 if (filename == NULL || !strlen(filename)) {
Fam Zheng48239702013-10-11 15:43:22 +08001749 error_setg(errp, "No filename provided");
Fam Zheng65f74722013-05-02 10:25:22 +08001750 return VMDK_ERROR;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001751 }
1752 p = strrchr(filename, '/');
1753 if (p == NULL) {
1754 p = strrchr(filename, '\\');
1755 }
1756 if (p == NULL) {
1757 p = strrchr(filename, ':');
1758 }
1759 if (p != NULL) {
1760 p++;
1761 if (p - filename >= buf_len) {
Fam Zheng65f74722013-05-02 10:25:22 +08001762 return VMDK_ERROR;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001763 }
1764 pstrcpy(path, p - filename + 1, filename);
1765 } else {
1766 p = filename;
1767 path[0] = '\0';
1768 }
1769 q = strrchr(p, '.');
1770 if (q == NULL) {
1771 pstrcpy(prefix, buf_len, p);
1772 postfix[0] = '\0';
1773 } else {
1774 if (q - p >= buf_len) {
Fam Zheng65f74722013-05-02 10:25:22 +08001775 return VMDK_ERROR;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001776 }
1777 pstrcpy(prefix, q - p + 1, p);
1778 pstrcpy(postfix, buf_len, q);
1779 }
Fam Zheng65f74722013-05-02 10:25:22 +08001780 return VMDK_OK;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001781}
1782
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001783static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp)
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001784{
Fam Zheng917703c2013-12-20 09:48:48 +08001785 int idx = 0;
1786 BlockDriverState *new_bs = NULL;
Fam Zhengc13959c2014-05-28 11:38:58 +08001787 Error *local_err = NULL;
Fam Zhengaf057fe2013-12-03 10:41:05 +08001788 char *desc = NULL;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001789 int64_t total_size = 0, filesize;
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001790 char *adapter_type = NULL;
1791 char *backing_file = NULL;
1792 char *fmt = NULL;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001793 int flags = 0;
1794 int ret = 0;
Fam Zheng6c031aa2011-08-12 23:19:32 +08001795 bool flat, split, compress;
Fam Zhengaf057fe2013-12-03 10:41:05 +08001796 GString *ext_desc_lines;
Jeff Codyfe206562015-01-22 08:03:26 -05001797 char *path = g_malloc0(PATH_MAX);
1798 char *prefix = g_malloc0(PATH_MAX);
1799 char *postfix = g_malloc0(PATH_MAX);
1800 char *desc_line = g_malloc0(BUF_SIZE);
1801 char *ext_filename = g_malloc0(PATH_MAX);
1802 char *desc_filename = g_malloc0(PATH_MAX);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001803 const int64_t split_size = 0x80000000; /* VMDK has constant split size */
1804 const char *desc_extent_line;
Jeff Codyfe206562015-01-22 08:03:26 -05001805 char *parent_desc_line = g_malloc0(BUF_SIZE);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001806 uint32_t parent_cid = 0xffffffff;
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001807 uint32_t number_heads = 16;
Fam Zheng69e0b6d2013-05-02 10:25:24 +08001808 bool zeroed_grain = false;
Fam Zheng917703c2013-12-20 09:48:48 +08001809 uint32_t desc_offset = 0, desc_len;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001810 const char desc_template[] =
1811 "# Disk DescriptorFile\n"
1812 "version=1\n"
Fam Zheng9b170312014-04-17 18:43:53 +08001813 "CID=%" PRIx32 "\n"
1814 "parentCID=%" PRIx32 "\n"
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001815 "createType=\"%s\"\n"
1816 "%s"
1817 "\n"
1818 "# Extent description\n"
1819 "%s"
1820 "\n"
1821 "# The Disk Data Base\n"
1822 "#DDB\n"
1823 "\n"
1824 "ddb.virtualHWVersion = \"%d\"\n"
1825 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
Fam Zheng4ab9dab2014-04-17 11:34:37 +08001826 "ddb.geometry.heads = \"%" PRIu32 "\"\n"
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001827 "ddb.geometry.sectors = \"63\"\n"
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001828 "ddb.adapterType = \"%s\"\n";
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001829
Fam Zhengaf057fe2013-12-03 10:41:05 +08001830 ext_desc_lines = g_string_new(NULL);
1831
Fam Zheng48239702013-10-11 15:43:22 +08001832 if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {
Fam Zhengaf057fe2013-12-03 10:41:05 +08001833 ret = -EINVAL;
1834 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001835 }
1836 /* Read out options */
Hu Taoc2eb9182014-09-10 17:05:45 +08001837 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1838 BDRV_SECTOR_SIZE);
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001839 adapter_type = qemu_opt_get_del(opts, BLOCK_OPT_ADAPTER_TYPE);
1840 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
1841 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false)) {
1842 flags |= BLOCK_FLAG_COMPAT6;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001843 }
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001844 fmt = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT);
1845 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ZEROED_GRAIN, false)) {
1846 zeroed_grain = true;
1847 }
1848
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001849 if (!adapter_type) {
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001850 adapter_type = g_strdup("ide");
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001851 } else if (strcmp(adapter_type, "ide") &&
1852 strcmp(adapter_type, "buslogic") &&
1853 strcmp(adapter_type, "lsilogic") &&
1854 strcmp(adapter_type, "legacyESX")) {
Fam Zheng48239702013-10-11 15:43:22 +08001855 error_setg(errp, "Unknown adapter type: '%s'", adapter_type);
Fam Zhengaf057fe2013-12-03 10:41:05 +08001856 ret = -EINVAL;
1857 goto exit;
Othmar Pasteka7f2039f2013-01-30 00:26:52 +01001858 }
1859 if (strcmp(adapter_type, "ide") != 0) {
1860 /* that's the number of heads with which vmware operates when
1861 creating, exporting, etc. vmdk files with a non-ide adapter type */
1862 number_heads = 255;
1863 }
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001864 if (!fmt) {
1865 /* Default format to monolithicSparse */
Chunyan Liu5820f1d2014-06-05 17:21:09 +08001866 fmt = g_strdup("monolithicSparse");
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001867 } else if (strcmp(fmt, "monolithicFlat") &&
1868 strcmp(fmt, "monolithicSparse") &&
1869 strcmp(fmt, "twoGbMaxExtentSparse") &&
Fam Zheng6c031aa2011-08-12 23:19:32 +08001870 strcmp(fmt, "twoGbMaxExtentFlat") &&
1871 strcmp(fmt, "streamOptimized")) {
Fam Zheng48239702013-10-11 15:43:22 +08001872 error_setg(errp, "Unknown subformat: '%s'", fmt);
Fam Zhengaf057fe2013-12-03 10:41:05 +08001873 ret = -EINVAL;
1874 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001875 }
1876 split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
1877 strcmp(fmt, "twoGbMaxExtentSparse"));
1878 flat = !(strcmp(fmt, "monolithicFlat") &&
1879 strcmp(fmt, "twoGbMaxExtentFlat"));
Fam Zheng6c031aa2011-08-12 23:19:32 +08001880 compress = !strcmp(fmt, "streamOptimized");
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001881 if (flat) {
Fam Zheng4ab9dab2014-04-17 11:34:37 +08001882 desc_extent_line = "RW %" PRId64 " FLAT \"%s\" 0\n";
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001883 } else {
Fam Zheng4ab9dab2014-04-17 11:34:37 +08001884 desc_extent_line = "RW %" PRId64 " SPARSE \"%s\"\n";
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001885 }
1886 if (flat && backing_file) {
Fam Zheng48239702013-10-11 15:43:22 +08001887 error_setg(errp, "Flat image can't have backing file");
Fam Zhengaf057fe2013-12-03 10:41:05 +08001888 ret = -ENOTSUP;
1889 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001890 }
Fam Zheng52c8d622013-10-11 15:43:23 +08001891 if (flat && zeroed_grain) {
1892 error_setg(errp, "Flat image can't enable zeroed grain");
Fam Zhengaf057fe2013-12-03 10:41:05 +08001893 ret = -ENOTSUP;
1894 goto exit;
Fam Zheng52c8d622013-10-11 15:43:23 +08001895 }
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001896 if (backing_file) {
Max Reitzf67503e2014-02-18 18:33:05 +01001897 BlockDriverState *bs = NULL;
Max Reitz1085daf2014-11-26 17:20:28 +01001898 char *full_backing = g_new0(char, PATH_MAX);
1899 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
1900 full_backing, PATH_MAX,
1901 &local_err);
1902 if (local_err) {
1903 g_free(full_backing);
1904 error_propagate(errp, local_err);
1905 ret = -ENOENT;
1906 goto exit;
1907 }
Max Reitz6ebf9aa2015-08-26 19:47:49 +02001908 ret = bdrv_open(&bs, full_backing, NULL, NULL, BDRV_O_NO_BACKING, errp);
Max Reitz1085daf2014-11-26 17:20:28 +01001909 g_free(full_backing);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001910 if (ret != 0) {
Fam Zhengaf057fe2013-12-03 10:41:05 +08001911 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001912 }
1913 if (strcmp(bs->drv->format_name, "vmdk")) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001914 bdrv_unref(bs);
Fam Zhengaf057fe2013-12-03 10:41:05 +08001915 ret = -EINVAL;
1916 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001917 }
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001918 parent_cid = vmdk_read_cid(bs, 0);
Fam Zheng4f6fd342013-08-23 09:14:47 +08001919 bdrv_unref(bs);
Jeff Codyfe206562015-01-22 08:03:26 -05001920 snprintf(parent_desc_line, BUF_SIZE,
Fam Zheng8ed610a2013-06-26 17:24:32 +08001921 "parentFileNameHint=\"%s\"", backing_file);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001922 }
1923
1924 /* Create extents */
1925 filesize = total_size;
1926 while (filesize > 0) {
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001927 int64_t size = filesize;
1928
1929 if (split && size > split_size) {
1930 size = split_size;
1931 }
1932 if (split) {
Jeff Codyfe206562015-01-22 08:03:26 -05001933 snprintf(desc_filename, PATH_MAX, "%s-%c%03d%s",
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001934 prefix, flat ? 'f' : 's', ++idx, postfix);
1935 } else if (flat) {
Jeff Codyfe206562015-01-22 08:03:26 -05001936 snprintf(desc_filename, PATH_MAX, "%s-flat%s", prefix, postfix);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001937 } else {
Jeff Codyfe206562015-01-22 08:03:26 -05001938 snprintf(desc_filename, PATH_MAX, "%s%s", prefix, postfix);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001939 }
Jeff Codyfe206562015-01-22 08:03:26 -05001940 snprintf(ext_filename, PATH_MAX, "%s%s", path, desc_filename);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001941
Fam Zheng69e0b6d2013-05-02 10:25:24 +08001942 if (vmdk_create_extent(ext_filename, size,
Chunyan Liu4ab15592014-06-30 14:29:58 +08001943 flat, compress, zeroed_grain, opts, errp)) {
Fam Zhengaf057fe2013-12-03 10:41:05 +08001944 ret = -EINVAL;
1945 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001946 }
1947 filesize -= size;
1948
1949 /* Format description line */
Jeff Codyfe206562015-01-22 08:03:26 -05001950 snprintf(desc_line, BUF_SIZE,
Fam Zheng917703c2013-12-20 09:48:48 +08001951 desc_extent_line, size / BDRV_SECTOR_SIZE, desc_filename);
Fam Zhengaf057fe2013-12-03 10:41:05 +08001952 g_string_append(ext_desc_lines, desc_line);
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001953 }
1954 /* generate descriptor file */
Fam Zhengaf057fe2013-12-03 10:41:05 +08001955 desc = g_strdup_printf(desc_template,
Fam Zhenge5dc64b2014-12-04 07:28:29 +08001956 g_random_int(),
Fam Zhengaf057fe2013-12-03 10:41:05 +08001957 parent_cid,
1958 fmt,
1959 parent_desc_line,
1960 ext_desc_lines->str,
1961 (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
Fam Zheng917703c2013-12-20 09:48:48 +08001962 total_size /
1963 (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
Fam Zhengaf057fe2013-12-03 10:41:05 +08001964 number_heads,
1965 adapter_type);
Fam Zheng917703c2013-12-20 09:48:48 +08001966 desc_len = strlen(desc);
1967 /* the descriptor offset = 0x200 */
1968 if (!split && !flat) {
1969 desc_offset = 0x200;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001970 } else {
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001971 ret = bdrv_create_file(filename, opts, &local_err);
Fam Zheng917703c2013-12-20 09:48:48 +08001972 if (ret < 0) {
Fam Zhengc13959c2014-05-28 11:38:58 +08001973 error_propagate(errp, local_err);
Fam Zheng917703c2013-12-20 09:48:48 +08001974 goto exit;
1975 }
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001976 }
Max Reitz2e401342014-02-18 18:33:07 +01001977 assert(new_bs == NULL);
1978 ret = bdrv_open(&new_bs, filename, NULL, NULL,
Max Reitz6ebf9aa2015-08-26 19:47:49 +02001979 BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err);
Fam Zheng917703c2013-12-20 09:48:48 +08001980 if (ret < 0) {
Fam Zhengc13959c2014-05-28 11:38:58 +08001981 error_propagate(errp, local_err);
Fam Zhengaf057fe2013-12-03 10:41:05 +08001982 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001983 }
Fam Zheng917703c2013-12-20 09:48:48 +08001984 ret = bdrv_pwrite(new_bs, desc_offset, desc, desc_len);
1985 if (ret < 0) {
1986 error_setg_errno(errp, -ret, "Could not write description");
1987 goto exit;
Fam Zhengf66fd6c2011-07-19 08:45:23 +08001988 }
Fam Zheng917703c2013-12-20 09:48:48 +08001989 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
1990 * for description file */
1991 if (desc_offset == 0) {
1992 ret = bdrv_truncate(new_bs, desc_len);
1993 if (ret < 0) {
Jeff Cody39a611a2014-02-12 14:46:24 -05001994 error_setg_errno(errp, -ret, "Could not truncate file");
Fam Zheng917703c2013-12-20 09:48:48 +08001995 }
Kirill A. Shutemov16403662010-01-20 00:56:13 +01001996 }
Fam Zhengaf057fe2013-12-03 10:41:05 +08001997exit:
Fam Zheng917703c2013-12-20 09:48:48 +08001998 if (new_bs) {
1999 bdrv_unref(new_bs);
2000 }
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002001 g_free(adapter_type);
2002 g_free(backing_file);
2003 g_free(fmt);
Fam Zhengaf057fe2013-12-03 10:41:05 +08002004 g_free(desc);
Jeff Codyfe206562015-01-22 08:03:26 -05002005 g_free(path);
2006 g_free(prefix);
2007 g_free(postfix);
2008 g_free(desc_line);
2009 g_free(ext_filename);
2010 g_free(desc_filename);
2011 g_free(parent_desc_line);
Fam Zhengaf057fe2013-12-03 10:41:05 +08002012 g_string_free(ext_desc_lines, true);
Kirill A. Shutemov16403662010-01-20 00:56:13 +01002013 return ret;
bellard8979b222005-07-02 14:02:54 +00002014}
2015
bellarde2731ad2004-09-18 19:32:11 +00002016static void vmdk_close(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002017{
Kevin Wolf2bc31662011-11-22 16:50:27 +01002018 BDRVVmdkState *s = bs->opaque;
2019
Fam Zhengb3976d32011-07-12 19:56:28 +08002020 vmdk_free_extents(bs);
Fam Zhengf4c129a2013-10-31 10:06:23 +08002021 g_free(s->create_type);
Kevin Wolf2bc31662011-11-22 16:50:27 +01002022
2023 migrate_del_blocker(s->migration_blocker);
2024 error_free(s->migration_blocker);
bellardea2384d2004-08-01 21:59:26 +00002025}
2026
Paolo Bonzini8b94ff82011-10-20 13:16:24 +02002027static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
pbrook7a6cba62006-06-04 11:39:07 +00002028{
Fam Zheng333c5742011-07-12 19:56:33 +08002029 BDRVVmdkState *s = bs->opaque;
Paolo Bonzini29cdb252012-03-12 18:26:01 +01002030 int i, err;
2031 int ret = 0;
Fam Zheng333c5742011-07-12 19:56:33 +08002032
Fam Zheng333c5742011-07-12 19:56:33 +08002033 for (i = 0; i < s->num_extents; i++) {
Paolo Bonzini8b94ff82011-10-20 13:16:24 +02002034 err = bdrv_co_flush(s->extents[i].file);
Fam Zheng333c5742011-07-12 19:56:33 +08002035 if (err < 0) {
2036 ret = err;
2037 }
2038 }
2039 return ret;
pbrook7a6cba62006-06-04 11:39:07 +00002040}
2041
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002042static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
2043{
2044 int i;
2045 int64_t ret = 0;
2046 int64_t r;
2047 BDRVVmdkState *s = bs->opaque;
2048
2049 ret = bdrv_get_allocated_file_size(bs->file);
2050 if (ret < 0) {
2051 return ret;
2052 }
2053 for (i = 0; i < s->num_extents; i++) {
2054 if (s->extents[i].file == bs->file) {
2055 continue;
2056 }
2057 r = bdrv_get_allocated_file_size(s->extents[i].file);
2058 if (r < 0) {
2059 return r;
2060 }
2061 ret += r;
2062 }
2063 return ret;
2064}
Kevin Wolf0e7e1982009-05-18 16:42:10 +02002065
Fam Zhengda7a50f2013-07-01 11:33:17 +08002066static int vmdk_has_zero_init(BlockDriverState *bs)
2067{
2068 int i;
2069 BDRVVmdkState *s = bs->opaque;
2070
2071 /* If has a flat extent and its underlying storage doesn't have zero init,
2072 * return 0. */
2073 for (i = 0; i < s->num_extents; i++) {
2074 if (s->extents[i].flat) {
2075 if (!bdrv_has_zero_init(s->extents[i].file)) {
2076 return 0;
2077 }
2078 }
2079 }
2080 return 1;
2081}
2082
Fam Zhengf4c129a2013-10-31 10:06:23 +08002083static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
2084{
2085 ImageInfo *info = g_new0(ImageInfo, 1);
2086
2087 *info = (ImageInfo){
2088 .filename = g_strdup(extent->file->filename),
2089 .format = g_strdup(extent->type),
2090 .virtual_size = extent->sectors * BDRV_SECTOR_SIZE,
2091 .compressed = extent->compressed,
2092 .has_compressed = extent->compressed,
2093 .cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE,
2094 .has_cluster_size = !extent->flat,
2095 };
2096
2097 return info;
2098}
2099
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002100static int vmdk_check(BlockDriverState *bs, BdrvCheckResult *result,
2101 BdrvCheckMode fix)
2102{
2103 BDRVVmdkState *s = bs->opaque;
2104 VmdkExtent *extent = NULL;
2105 int64_t sector_num = 0;
Markus Armbruster57322b72014-06-26 13:23:22 +02002106 int64_t total_sectors = bdrv_nb_sectors(bs);
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002107 int ret;
2108 uint64_t cluster_offset;
2109
2110 if (fix) {
2111 return -ENOTSUP;
2112 }
2113
2114 for (;;) {
2115 if (sector_num >= total_sectors) {
2116 return 0;
2117 }
2118 extent = find_extent(s, sector_num, extent);
2119 if (!extent) {
2120 fprintf(stderr,
2121 "ERROR: could not find extent for sector %" PRId64 "\n",
2122 sector_num);
2123 break;
2124 }
2125 ret = get_cluster_offset(bs, extent, NULL,
2126 sector_num << BDRV_SECTOR_BITS,
Fam Zhengc6ac36e2014-07-30 14:39:10 +08002127 false, &cluster_offset, 0, 0);
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002128 if (ret == VMDK_ERROR) {
2129 fprintf(stderr,
2130 "ERROR: could not get cluster_offset for sector %"
2131 PRId64 "\n", sector_num);
2132 break;
2133 }
2134 if (ret == VMDK_OK && cluster_offset >= bdrv_getlength(extent->file)) {
2135 fprintf(stderr,
2136 "ERROR: cluster offset for sector %"
2137 PRId64 " points after EOF\n", sector_num);
2138 break;
2139 }
2140 sector_num += extent->cluster_sectors;
2141 }
2142
2143 result->corruptions++;
2144 return 0;
2145}
2146
Fam Zhengf4c129a2013-10-31 10:06:23 +08002147static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs)
2148{
2149 int i;
2150 BDRVVmdkState *s = bs->opaque;
2151 ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
2152 ImageInfoList **next;
2153
2154 *spec_info = (ImageInfoSpecific){
2155 .kind = IMAGE_INFO_SPECIFIC_KIND_VMDK,
2156 {
2157 .vmdk = g_new0(ImageInfoSpecificVmdk, 1),
2158 },
2159 };
2160
2161 *spec_info->vmdk = (ImageInfoSpecificVmdk) {
2162 .create_type = g_strdup(s->create_type),
2163 .cid = s->cid,
2164 .parent_cid = s->parent_cid,
2165 };
2166
2167 next = &spec_info->vmdk->extents;
2168 for (i = 0; i < s->num_extents; i++) {
2169 *next = g_new0(ImageInfoList, 1);
2170 (*next)->value = vmdk_get_extent_info(&s->extents[i]);
2171 (*next)->next = NULL;
2172 next = &(*next)->next;
2173 }
2174
2175 return spec_info;
2176}
2177
Fam Zheng5f583302014-11-14 12:09:21 +08002178static bool vmdk_extents_type_eq(const VmdkExtent *a, const VmdkExtent *b)
2179{
2180 return a->flat == b->flat &&
2181 a->compressed == b->compressed &&
2182 (a->flat || a->cluster_sectors == b->cluster_sectors);
2183}
2184
Fam Zheng74fe1882014-05-06 21:08:45 +08002185static int vmdk_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2186{
2187 int i;
2188 BDRVVmdkState *s = bs->opaque;
2189 assert(s->num_extents);
Fam Zheng5f583302014-11-14 12:09:21 +08002190
2191 /* See if we have multiple extents but they have different cases */
2192 for (i = 1; i < s->num_extents; i++) {
2193 if (!vmdk_extents_type_eq(&s->extents[0], &s->extents[i])) {
2194 return -ENOTSUP;
2195 }
2196 }
Fam Zheng74fe1882014-05-06 21:08:45 +08002197 bdi->needs_compressed_writes = s->extents[0].compressed;
2198 if (!s->extents[0].flat) {
2199 bdi->cluster_size = s->extents[0].cluster_sectors << BDRV_SECTOR_BITS;
2200 }
Fam Zheng74fe1882014-05-06 21:08:45 +08002201 return 0;
2202}
2203
Stefan Hajnoczic75f3bd2014-05-08 16:34:54 +02002204static void vmdk_detach_aio_context(BlockDriverState *bs)
2205{
2206 BDRVVmdkState *s = bs->opaque;
2207 int i;
2208
2209 for (i = 0; i < s->num_extents; i++) {
2210 bdrv_detach_aio_context(s->extents[i].file);
2211 }
2212}
2213
2214static void vmdk_attach_aio_context(BlockDriverState *bs,
2215 AioContext *new_context)
2216{
2217 BDRVVmdkState *s = bs->opaque;
2218 int i;
2219
2220 for (i = 0; i < s->num_extents; i++) {
2221 bdrv_attach_aio_context(s->extents[i].file, new_context);
2222 }
2223}
2224
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002225static QemuOptsList vmdk_create_opts = {
2226 .name = "vmdk-create-opts",
2227 .head = QTAILQ_HEAD_INITIALIZER(vmdk_create_opts.head),
2228 .desc = {
2229 {
2230 .name = BLOCK_OPT_SIZE,
2231 .type = QEMU_OPT_SIZE,
2232 .help = "Virtual disk size"
2233 },
2234 {
2235 .name = BLOCK_OPT_ADAPTER_TYPE,
2236 .type = QEMU_OPT_STRING,
2237 .help = "Virtual adapter type, can be one of "
2238 "ide (default), lsilogic, buslogic or legacyESX"
2239 },
2240 {
2241 .name = BLOCK_OPT_BACKING_FILE,
2242 .type = QEMU_OPT_STRING,
2243 .help = "File name of a base image"
2244 },
2245 {
2246 .name = BLOCK_OPT_COMPAT6,
2247 .type = QEMU_OPT_BOOL,
2248 .help = "VMDK version 6 image",
2249 .def_value_str = "off"
2250 },
2251 {
2252 .name = BLOCK_OPT_SUBFMT,
2253 .type = QEMU_OPT_STRING,
2254 .help =
2255 "VMDK flat extent format, can be one of "
2256 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2257 },
2258 {
2259 .name = BLOCK_OPT_ZEROED_GRAIN,
2260 .type = QEMU_OPT_BOOL,
2261 .help = "Enable efficient zero writes "
2262 "using the zeroed-grain GTE feature"
2263 },
2264 { /* end of list */ }
2265 }
Kevin Wolf0e7e1982009-05-18 16:42:10 +02002266};
2267
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002268static BlockDriver bdrv_vmdk = {
Fam Zhengda7a50f2013-07-01 11:33:17 +08002269 .format_name = "vmdk",
2270 .instance_size = sizeof(BDRVVmdkState),
2271 .bdrv_probe = vmdk_probe,
2272 .bdrv_open = vmdk_open,
Peter Lievenf43aa8e2014-01-29 09:34:16 +01002273 .bdrv_check = vmdk_check,
Fam Zhengda7a50f2013-07-01 11:33:17 +08002274 .bdrv_reopen_prepare = vmdk_reopen_prepare,
2275 .bdrv_read = vmdk_co_read,
2276 .bdrv_write = vmdk_co_write,
Fam Zhengba0ad892014-05-06 21:08:44 +08002277 .bdrv_write_compressed = vmdk_write_compressed,
Fam Zhengda7a50f2013-07-01 11:33:17 +08002278 .bdrv_co_write_zeroes = vmdk_co_write_zeroes,
2279 .bdrv_close = vmdk_close,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002280 .bdrv_create = vmdk_create,
Fam Zhengda7a50f2013-07-01 11:33:17 +08002281 .bdrv_co_flush_to_disk = vmdk_co_flush,
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02002282 .bdrv_co_get_block_status = vmdk_co_get_block_status,
Fam Zhengda7a50f2013-07-01 11:33:17 +08002283 .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
2284 .bdrv_has_zero_init = vmdk_has_zero_init,
Fam Zhengf4c129a2013-10-31 10:06:23 +08002285 .bdrv_get_specific_info = vmdk_get_specific_info,
Kevin Wolfd34682c2013-12-11 19:26:16 +01002286 .bdrv_refresh_limits = vmdk_refresh_limits,
Fam Zheng74fe1882014-05-06 21:08:45 +08002287 .bdrv_get_info = vmdk_get_info,
Stefan Hajnoczic75f3bd2014-05-08 16:34:54 +02002288 .bdrv_detach_aio_context = vmdk_detach_aio_context,
2289 .bdrv_attach_aio_context = vmdk_attach_aio_context,
Kevin Wolf0e7e1982009-05-18 16:42:10 +02002290
Kevin Wolf8ee79e72014-06-04 15:09:35 +02002291 .supports_backing = true,
Chunyan Liu5820f1d2014-06-05 17:21:09 +08002292 .create_opts = &vmdk_create_opts,
bellardea2384d2004-08-01 21:59:26 +00002293};
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002294
2295static void bdrv_vmdk_init(void)
2296{
2297 bdrv_register(&bdrv_vmdk);
2298}
2299
2300block_init(bdrv_vmdk_init);