1 | /** $Id: VmdkHDDCore.cpp 2829 2007-05-23 16:21:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMDK Disk image, Core Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #define LOG_GROUP LOG_GROUP_VD_VMDK
|
---|
26 | #include "VBoxHDD-newInternal.h"
|
---|
27 | #include <VBox/err.h>
|
---|
28 |
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/alloc.h>
|
---|
32 | #include <iprt/uuid.h>
|
---|
33 | #include <iprt/file.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/rand.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Constants And Macros, Structures and Typedefs *
|
---|
41 | *******************************************************************************/
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Magic number for hosted images created by VMware Workstation 4, VMware
|
---|
45 | * Workstation 5, VMware Server or VMware Player.
|
---|
46 | */
|
---|
47 | #define VMDK_SPARSE_MAGICNUMBER 0x564d444b /* 'V' 'M' 'D' 'K' */
|
---|
48 |
|
---|
49 | /** VMDK hosted sparse extent header. */
|
---|
50 | #pragma pack(1)
|
---|
51 | typedef struct SparseExtentHeader
|
---|
52 | {
|
---|
53 | uint32_t magicNumber;
|
---|
54 | uint32_t version;
|
---|
55 | uint32_t flags;
|
---|
56 | uint64_t capacity;
|
---|
57 | uint64_t grainSize;
|
---|
58 | uint64_t descriptorOffset;
|
---|
59 | uint64_t descriptorSize;
|
---|
60 | uint32_t numGTEsPerGT;
|
---|
61 | uint64_t rgdOffset;
|
---|
62 | uint64_t gdOffset;
|
---|
63 | uint64_t overHead;
|
---|
64 | bool uncleanShutdown;
|
---|
65 | char singleEndLineChar;
|
---|
66 | char nonEndLineChar;
|
---|
67 | char doubleEndLineChar1;
|
---|
68 | char doubleEndLineChar2;
|
---|
69 | uint8_t pad[435];
|
---|
70 | } SparseExtentHeader;
|
---|
71 | #pragma pack()
|
---|
72 |
|
---|
73 |
|
---|
74 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
75 |
|
---|
76 | /** @todo the ESX code is not tested, not used, and lacks error messages. */
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Magic number for images created by VMware GSX Server 3 or ESX Server 3.
|
---|
80 | */
|
---|
81 | #define VMDK_ESX_SPARSE_MAGICNUMBER 0x44574f43 /* 'C' 'O' 'W' 'D' */
|
---|
82 |
|
---|
83 | #pragma pack(1)
|
---|
84 | typedef struct COWDisk_Header
|
---|
85 | {
|
---|
86 | uint32_t magicNumber;
|
---|
87 | uint32_t version;
|
---|
88 | uint32_t flags;
|
---|
89 | uint32_t numSectors;
|
---|
90 | uint32_t grainSize;
|
---|
91 | uint32_t gdOffset;
|
---|
92 | uint32_t numGDEntries;
|
---|
93 | uint32_t freeSector;
|
---|
94 | /* The spec incompletely documents quite a few further fields, but states
|
---|
95 | * that they are not used by the current format. Replace them by padding. */
|
---|
96 | char reserved1[1604];
|
---|
97 | uint32_t savedGeneration;
|
---|
98 | char reserved2[8];
|
---|
99 | uint32_t uncleanShutdown;
|
---|
100 | char padding[396];
|
---|
101 | } COWDisk_Header;
|
---|
102 | #pragma pack()
|
---|
103 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
104 |
|
---|
105 |
|
---|
106 | /** Convert sector number/size to byte offset/size. */
|
---|
107 | #define VMDK_SECTOR2BYTE(u) ((u) << 9)
|
---|
108 |
|
---|
109 | /** Convert byte offset/size to sector number/size. */
|
---|
110 | #define VMDK_BYTE2SECTOR(u) ((u) >> 9)
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * VMDK extent type.
|
---|
114 | */
|
---|
115 | typedef enum VMDKETYPE
|
---|
116 | {
|
---|
117 | /** Hosted sparse extent. */
|
---|
118 | VMDKETYPE_HOSTED_SPARSE = 1,
|
---|
119 | /** Flat extent. */
|
---|
120 | VMDKETYPE_FLAT,
|
---|
121 | /** Zero extent. */
|
---|
122 | VMDKETYPE_ZERO
|
---|
123 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
124 | ,
|
---|
125 | /** ESX sparse extent. */
|
---|
126 | VMDKETYPE_ESX_SPARSE
|
---|
127 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
128 | } VMDKETYPE, *PVMDKETYPE;
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * VMDK access type for a extent.
|
---|
132 | */
|
---|
133 | typedef enum VMDKACCESS
|
---|
134 | {
|
---|
135 | /** No access allowed. */
|
---|
136 | VMDKACCESS_NOACCESS = 0,
|
---|
137 | /** Read-only access. */
|
---|
138 | VMDKACCESS_READONLY,
|
---|
139 | /** Read-write access. */
|
---|
140 | VMDKACCESS_READWRITE
|
---|
141 | } VMDKACCESS, *PVMDKACCESS;
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * VMDK extent data structure.
|
---|
145 | */
|
---|
146 | typedef struct VMDKEXTENT
|
---|
147 | {
|
---|
148 | /** File handle. */
|
---|
149 | RTFILE File;
|
---|
150 | /** Base name of the image extent. */
|
---|
151 | const char *pszBasename;
|
---|
152 | /** Full name of the image extent. */
|
---|
153 | const char *pszFullname;
|
---|
154 | /** Number of sectors in this extent. */
|
---|
155 | uint64_t cSectors;
|
---|
156 | /** Number of sectors per block (grain in VMDK speak). */
|
---|
157 | uint64_t cSectorsPerGrain;
|
---|
158 | /** Starting sector number of descriptor. */
|
---|
159 | uint64_t uDescriptorSector;
|
---|
160 | /** Size of descriptor in sectors. */
|
---|
161 | uint64_t cDescriptorSectors;
|
---|
162 | /** Starting sector number of grain directory. */
|
---|
163 | uint64_t uSectorGD;
|
---|
164 | /** Starting sector number of redundant grain directory. */
|
---|
165 | uint64_t uSectorRGD;
|
---|
166 | /** Total number of metadata sectors. */
|
---|
167 | uint64_t cOverheadSectors;
|
---|
168 | /** Nominal size (i.e. as described by the descriptor) of this extent. */
|
---|
169 | uint64_t cNominalSectors;
|
---|
170 | /** Sector offset (i.e. as described by the descriptor) of this extent. */
|
---|
171 | uint64_t uSectorOffset;
|
---|
172 | /** Number of entries in a grain table. */
|
---|
173 | uint32_t cGTEntries;
|
---|
174 | /** Number of sectors reachable via a grain directory entry. */
|
---|
175 | uint32_t cSectorsPerGDE;
|
---|
176 | /** Number of entries in the grain directory. */
|
---|
177 | uint32_t cGDEntries;
|
---|
178 | /** Pointer to the next free sector. Legacy information. Do not use. */
|
---|
179 | uint32_t uFreeSector;
|
---|
180 | /** Number of this extent in the list of images. */
|
---|
181 | uint32_t uExtent;
|
---|
182 | /** Pointer to the descriptor (NULL if no descriptor in this extent). */
|
---|
183 | char *pDescData;
|
---|
184 | /** Pointer to the grain directory. */
|
---|
185 | uint32_t *pGD;
|
---|
186 | /** Pointer to the redundant grain directory. */
|
---|
187 | uint32_t *pRGD;
|
---|
188 | /** Type of this extent. */
|
---|
189 | VMDKETYPE enmType;
|
---|
190 | /** Access to this extent. */
|
---|
191 | VMDKACCESS enmAccess;
|
---|
192 | /** Flag whether this extent is marked as unclean. */
|
---|
193 | bool fUncleanShutdown;
|
---|
194 | /** Flag whether the metadata in the extent header needs to be updated. */
|
---|
195 | bool fMetaDirty;
|
---|
196 | /** Reference to the image in which this extent is used. Do not use this
|
---|
197 | * on a regular basis to avoid passing pImage references to functions
|
---|
198 | * explicitly. */
|
---|
199 | struct VMDKIMAGE *pImage;
|
---|
200 | } VMDKEXTENT, *PVMDKEXTENT;
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Grain table cache size. Allocated per image.
|
---|
204 | */
|
---|
205 | #define VMDK_GT_CACHE_SIZE 256
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Grain table block size. Smaller than an actual grain table block to allow
|
---|
209 | * more grain table blocks to be cached without having to allocate excessive
|
---|
210 | * amounts of memory for the cache.
|
---|
211 | */
|
---|
212 | #define VMDK_GT_CACHELINE_SIZE 128
|
---|
213 |
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Maximum number of lines in a descriptor file. Not worth the effort of
|
---|
217 | * making it variable. Descriptor files are generally very short (~20 lines).
|
---|
218 | */
|
---|
219 | #define VMDK_DESCRIPTOR_LINES_MAX 100U
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Parsed descriptor information. Allows easy access and update of the
|
---|
223 | * descriptor (whether separate file or not). Free form text files suck.
|
---|
224 | */
|
---|
225 | typedef struct VMDKDESCRIPTOR
|
---|
226 | {
|
---|
227 | /** Line number of first entry of the disk descriptor. */
|
---|
228 | unsigned uFirstDesc;
|
---|
229 | /** Line number of first entry in the extent description. */
|
---|
230 | unsigned uFirstExtent;
|
---|
231 | /** Line number of first disk database entry. */
|
---|
232 | unsigned uFirstDDB;
|
---|
233 | /** Total number of lines. */
|
---|
234 | unsigned cLines;
|
---|
235 | /** Total amount of memory available for the descriptor. */
|
---|
236 | size_t cbDescAlloc;
|
---|
237 | /** Set if descriptor has been changed and not yet written to disk. */
|
---|
238 | bool fDirty;
|
---|
239 | /** Array of pointers to the data in the descriptor. */
|
---|
240 | char *aLines[VMDK_DESCRIPTOR_LINES_MAX];
|
---|
241 | /** Array of line indices pointing to the next non-comment line. */
|
---|
242 | unsigned aNextLines[VMDK_DESCRIPTOR_LINES_MAX];
|
---|
243 | } VMDKDESCRIPTOR, *PVMDKDESCRIPTOR;
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Cache entry for translating extent/sector to a sector number in that
|
---|
248 | * extent.
|
---|
249 | */
|
---|
250 | typedef struct VMDKGTCACHEENTRY
|
---|
251 | {
|
---|
252 | /** Extent number for which this entry is valid. */
|
---|
253 | uint32_t uExtent;
|
---|
254 | /** GT data block number. */
|
---|
255 | uint64_t uGTBlock;
|
---|
256 | /** Data part of the cache entry. */
|
---|
257 | uint32_t aGTData[VMDK_GT_CACHELINE_SIZE];
|
---|
258 | } VMDKGTCACHEENTRY, *PVMDKGTCACHEENTRY;
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Cache data structure for blocks of grain table entries. For now this is a
|
---|
262 | * fixed size direct mapping cache, but this should be adapted to the size of
|
---|
263 | * the sparse image and maybe converted to a set-associative cache. The
|
---|
264 | * implementation below implements a write-through cache with write allocate.
|
---|
265 | */
|
---|
266 | typedef struct VMDKGTCACHE
|
---|
267 | {
|
---|
268 | /** Cache entries. */
|
---|
269 | VMDKGTCACHEENTRY aGTCache[VMDK_GT_CACHE_SIZE];
|
---|
270 | /** Number of cache entries (currently unused). */
|
---|
271 | unsigned cEntries;
|
---|
272 | } VMDKGTCACHE, *PVMDKGTCACHE;
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Complete VMDK image data structure. Mainly a collection of extents and a few
|
---|
276 | * extra global data fields.
|
---|
277 | */
|
---|
278 | typedef struct VMDKIMAGE
|
---|
279 | {
|
---|
280 | PVMDKEXTENT pExtents;
|
---|
281 | unsigned cExtents;
|
---|
282 |
|
---|
283 | /** Base image name. */
|
---|
284 | const char *pszFilename;
|
---|
285 | /** Descriptor file if applicable. */
|
---|
286 | RTFILE File;
|
---|
287 |
|
---|
288 | /** Error callback. */
|
---|
289 | PFNVDERROR pfnError;
|
---|
290 | /** Opaque data for error callback. */
|
---|
291 | void *pvErrorUser;
|
---|
292 |
|
---|
293 | /** Open flags passed by VBoxHD layer. */
|
---|
294 | unsigned uOpenFlags;
|
---|
295 | /** Image type. */
|
---|
296 | VDIMAGETYPE enmImageType;
|
---|
297 | /** Image flags defined during creation or determined during open. */
|
---|
298 | unsigned uImageFlags;
|
---|
299 | /** Total size of the image. */
|
---|
300 | uint64_t cbSize;
|
---|
301 | /** BIOS translation mode. */
|
---|
302 | PDMBIOSTRANSLATION enmTranslation;
|
---|
303 | /** Physical geometry of this image, cylinders. */
|
---|
304 | uint32_t cCylinders;
|
---|
305 | /** Physical geometry of this image, heads. */
|
---|
306 | uint32_t cHeads;
|
---|
307 | /** Physical geometry of this image, sectors. */
|
---|
308 | uint32_t cSectors;
|
---|
309 | /** Image UUID. */
|
---|
310 | RTUUID ImageUuid;
|
---|
311 | /** Image modification UUID. */
|
---|
312 | RTUUID ModificationUuid;
|
---|
313 | /** Parent image UUID. */
|
---|
314 | RTUUID ParentUuid;
|
---|
315 |
|
---|
316 | /** Pointer to the grain table cache, if this image contains sparse extents. */
|
---|
317 | PVMDKGTCACHE pGTCache;
|
---|
318 | /** Pointer to the descriptor (NULL if no separate descriptor file). */
|
---|
319 | char *pDescData;
|
---|
320 | /** Allocation size of the descriptor file. */
|
---|
321 | size_t cbDescAlloc;
|
---|
322 | /** Parsed descriptor file content. */
|
---|
323 | VMDKDESCRIPTOR Descriptor;
|
---|
324 | } VMDKIMAGE, *PVMDKIMAGE;
|
---|
325 |
|
---|
326 |
|
---|
327 | /*******************************************************************************
|
---|
328 | * Internal Functions *
|
---|
329 | *******************************************************************************/
|
---|
330 |
|
---|
331 | static int vmdkReadGrainDirectory(PVMDKEXTENT pExtent);
|
---|
332 | static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent);
|
---|
333 |
|
---|
334 | static int vmdkPreprocessDescriptor(PVMDKIMAGE pImage, char *pDescData, size_t cbDescData, PVMDKDESCRIPTOR pDescriptor);
|
---|
335 | static int vmdkReadMetaSparseExtent(PVMDKEXTENT pExtent);
|
---|
336 | static int vmdkWriteMetaSparseExtent(PVMDKEXTENT pExtent);
|
---|
337 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
338 | static int vmdkReadMetaESXSparseExtent(PVMDKEXTENT pExtent);
|
---|
339 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
340 | static void vmdkFreeExtentData(PVMDKEXTENT pExtent, bool fDelete);
|
---|
341 |
|
---|
342 | static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents);
|
---|
343 | static int vmdkOpenImage(PVMDKIMAGE pImage, const char *pszFilename, unsigned uOpenFlags);
|
---|
344 | static int vmdkFlushImage(PVMDKIMAGE pImage);
|
---|
345 | static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment);
|
---|
346 | static void vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete);
|
---|
347 |
|
---|
348 | static int vmdkOpen(const char *pszFilename, unsigned uOpenFlags, PFNVDERROR pfnError, void *pvErrorUser, void **ppvBackendData);
|
---|
349 | static int vmdkClose(void *pBackendData, bool fDelete);
|
---|
350 |
|
---|
351 |
|
---|
352 | DECLINLINE(int) vmdkError(PVMDKIMAGE pImage, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
|
---|
353 | {
|
---|
354 | va_list va;
|
---|
355 | va_start(va, pszFormat);
|
---|
356 | pImage->pfnError(pImage->pvErrorUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
|
---|
357 | va_end(va);
|
---|
358 | return rc;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Internal: truncate a string (at a UTF8 code point boundary) and encode the
|
---|
363 | * critical non-ASCII characters.
|
---|
364 | */
|
---|
365 | static char *vmdkEncodeString(const char *psz)
|
---|
366 | {
|
---|
367 | /** @todo implement me. */
|
---|
368 | return RTStrDup(psz);
|
---|
369 | }
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Internal: decode a string and store it into the specified string.
|
---|
373 | */
|
---|
374 | static int vmdkDecodeString(const char *pszEncoded, char *psz, size_t cb)
|
---|
375 | {
|
---|
376 | /** @todo implement me. */
|
---|
377 | if (!cb)
|
---|
378 | return VINF_SUCCESS;
|
---|
379 | strncpy(psz, pszEncoded, cb);
|
---|
380 | psz[cb - 1] = '\0';
|
---|
381 | return VINF_SUCCESS;
|
---|
382 | }
|
---|
383 |
|
---|
384 | static int vmdkReadGrainDirectory(PVMDKEXTENT pExtent)
|
---|
385 | {
|
---|
386 | int rc = VINF_SUCCESS;
|
---|
387 | unsigned i;
|
---|
388 | uint32_t *pGD = NULL, *pRGD = NULL, *pGDTmp, *pRGDTmp;
|
---|
389 | size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
|
---|
390 |
|
---|
391 | pGD = (uint32_t *)RTMemAllocZ(cbGD);
|
---|
392 | if (!pGD)
|
---|
393 | {
|
---|
394 | rc = VERR_NO_MEMORY;
|
---|
395 | goto out;
|
---|
396 | }
|
---|
397 | pExtent->pGD = pGD;
|
---|
398 | rc = RTFileReadAt(pExtent->File, VMDK_SECTOR2BYTE(pExtent->uSectorGD),
|
---|
399 | pGD, cbGD, NULL);
|
---|
400 | AssertRC(rc);
|
---|
401 | if (VBOX_FAILURE(rc))
|
---|
402 | {
|
---|
403 | rc = vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: could not read grain directory in '%s'"), pExtent->pszFullname);
|
---|
404 | goto out;
|
---|
405 | }
|
---|
406 | for (i = 0, pGDTmp = pGD; i < pExtent->cGDEntries; i++, pGDTmp++)
|
---|
407 | *pGDTmp = RT_LE2H_U32(*pGDTmp);
|
---|
408 |
|
---|
409 | if (pExtent->uSectorRGD)
|
---|
410 | {
|
---|
411 | pRGD = (uint32_t *)RTMemAllocZ(cbGD);
|
---|
412 | if (!pRGD)
|
---|
413 | {
|
---|
414 | rc = VERR_NO_MEMORY;
|
---|
415 | goto out;
|
---|
416 | }
|
---|
417 | pExtent->pRGD = pRGD;
|
---|
418 | rc = RTFileReadAt(pExtent->File, VMDK_SECTOR2BYTE(pExtent->uSectorRGD),
|
---|
419 | pRGD, cbGD, NULL);
|
---|
420 | AssertRC(rc);
|
---|
421 | if (VBOX_FAILURE(rc))
|
---|
422 | {
|
---|
423 | rc = vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: could not read redundant grain directory in '%s'"), pExtent->pszFullname);
|
---|
424 | goto out;
|
---|
425 | }
|
---|
426 | for (i = 0, pRGDTmp = pRGD; i < pExtent->cGDEntries; i++, pRGDTmp++)
|
---|
427 | *pRGDTmp = RT_LE2H_U32(*pRGDTmp);
|
---|
428 |
|
---|
429 | /* Check grain table and redundant grain table for consistency. */
|
---|
430 | size_t cbGT = pExtent->cGTEntries;
|
---|
431 | uint32_t *pTmpGT1 = (uint32_t *)RTMemTmpAlloc(cbGT);
|
---|
432 | if (!pTmpGT1)
|
---|
433 | {
|
---|
434 | rc = VERR_NO_MEMORY;
|
---|
435 | goto out;
|
---|
436 | }
|
---|
437 | uint32_t *pTmpGT2 = (uint32_t *)RTMemTmpAlloc(cbGT);
|
---|
438 | if (!pTmpGT2)
|
---|
439 | {
|
---|
440 | RTMemTmpFree(pTmpGT1);
|
---|
441 | rc = VERR_NO_MEMORY;
|
---|
442 | goto out;
|
---|
443 | }
|
---|
444 |
|
---|
445 | for (i = 0, pGDTmp = pGD, pRGDTmp = pRGD; i < pExtent->cGDEntries; i++, pGDTmp++, pRGDTmp++)
|
---|
446 | {
|
---|
447 | /* If no grain table is allocated skip the entry. */
|
---|
448 | if (*pGDTmp == 0 && *pRGDTmp == 0)
|
---|
449 | continue;
|
---|
450 |
|
---|
451 | if (*pGDTmp == 0 || *pRGDTmp == 0 || *pGDTmp == *pRGDTmp)
|
---|
452 | {
|
---|
453 | /* Just one grain directory entry refers to a not yet allocated
|
---|
454 | * grain table or both grain directory copies refer to the same
|
---|
455 | * grain table. Not allowed. */
|
---|
456 | RTMemTmpFree(pTmpGT1);
|
---|
457 | RTMemTmpFree(pTmpGT2);
|
---|
458 | rc = vmdkError(pExtent->pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: inconsistent references to grain directory in '%s'"), pExtent->pszFullname);
|
---|
459 | goto out;
|
---|
460 | }
|
---|
461 | rc = RTFileReadAt(pExtent->File, VMDK_SECTOR2BYTE(*pGDTmp),
|
---|
462 | pTmpGT1, cbGT, NULL);
|
---|
463 | if (VBOX_FAILURE(rc))
|
---|
464 | {
|
---|
465 | rc = vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: error reading grain table in '%s'"), pExtent->pszFullname);
|
---|
466 | RTMemTmpFree(pTmpGT1);
|
---|
467 | RTMemTmpFree(pTmpGT2);
|
---|
468 | goto out;
|
---|
469 | }
|
---|
470 | rc = RTFileReadAt(pExtent->File, VMDK_SECTOR2BYTE(*pRGDTmp),
|
---|
471 | pTmpGT2, cbGT, NULL);
|
---|
472 | if (VBOX_FAILURE(rc))
|
---|
473 | {
|
---|
474 | rc = vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: error reading backup grain table in '%s'"), pExtent->pszFullname);
|
---|
475 | RTMemTmpFree(pTmpGT1);
|
---|
476 | RTMemTmpFree(pTmpGT2);
|
---|
477 | goto out;
|
---|
478 | }
|
---|
479 | if (memcmp(pTmpGT1, pTmpGT2, cbGT))
|
---|
480 | {
|
---|
481 | RTMemTmpFree(pTmpGT1);
|
---|
482 | RTMemTmpFree(pTmpGT2);
|
---|
483 | rc = vmdkError(pExtent->pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: inconsistency between grain table and backup grain table in '%s'"), pExtent->pszFullname);
|
---|
484 | goto out;
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | /** @todo figure out what to do for unclean VMDKs. */
|
---|
489 | }
|
---|
490 |
|
---|
491 | out:
|
---|
492 | if (VBOX_FAILURE(rc))
|
---|
493 | vmdkFreeGrainDirectory(pExtent);
|
---|
494 | return rc;
|
---|
495 | }
|
---|
496 |
|
---|
497 | static int vmdkCreateGrainDirectory(PVMDKEXTENT pExtent, uint64_t uStartSector, bool fPreAlloc)
|
---|
498 | {
|
---|
499 | int rc = VINF_SUCCESS;
|
---|
500 | unsigned i;
|
---|
501 | uint32_t *pGD = NULL, *pRGD = NULL;
|
---|
502 | size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
|
---|
503 | size_t cbGDRounded = RT_ALIGN_64(pExtent->cGDEntries * sizeof(uint32_t), 512);
|
---|
504 | size_t cbGTRounded;
|
---|
505 | uint64_t cbOverhead;
|
---|
506 |
|
---|
507 | if (fPreAlloc)
|
---|
508 | cbGTRounded = RT_ALIGN_64(pExtent->cGDEntries * pExtent->cGTEntries * sizeof(uint32_t), 512);
|
---|
509 | else
|
---|
510 | cbGTRounded = 0;
|
---|
511 |
|
---|
512 | pGD = (uint32_t *)RTMemAllocZ(cbGD);
|
---|
513 | if (!pGD)
|
---|
514 | {
|
---|
515 | rc = VERR_NO_MEMORY;
|
---|
516 | goto out;
|
---|
517 | }
|
---|
518 | pExtent->pGD = pGD;
|
---|
519 | pRGD = (uint32_t *)RTMemAllocZ(cbGD);
|
---|
520 | if (!pRGD)
|
---|
521 | {
|
---|
522 | rc = VERR_NO_MEMORY;
|
---|
523 | goto out;
|
---|
524 | }
|
---|
525 | pExtent->pRGD = pRGD;
|
---|
526 |
|
---|
527 | cbOverhead = RT_ALIGN_64(VMDK_SECTOR2BYTE(uStartSector) + 2 * (cbGDRounded + cbGTRounded), VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
|
---|
528 | rc = RTFileSetSize(pExtent->File, cbOverhead);
|
---|
529 | if (VBOX_FAILURE(rc))
|
---|
530 | goto out;
|
---|
531 | pExtent->uSectorRGD = uStartSector;
|
---|
532 | pExtent->uSectorGD = uStartSector + VMDK_BYTE2SECTOR(cbGDRounded + cbGTRounded);
|
---|
533 |
|
---|
534 | if (fPreAlloc)
|
---|
535 | {
|
---|
536 | uint32_t uGTSectorLE;
|
---|
537 | uint32_t uOffsetSectors;
|
---|
538 |
|
---|
539 | uOffsetSectors = pExtent->uSectorRGD + VMDK_BYTE2SECTOR(cbGDRounded);
|
---|
540 | for (i = 0; i < pExtent->cGDEntries; i++)
|
---|
541 | {
|
---|
542 | pRGD[i] = uOffsetSectors;
|
---|
543 | uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
|
---|
544 | /* Write the redundant grain directory entry to disk. */
|
---|
545 | rc = RTFileWriteAt(pExtent->File, VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + i * sizeof(uGTSectorLE), &uGTSectorLE, sizeof(uGTSectorLE), NULL);
|
---|
546 | if (VBOX_FAILURE(rc))
|
---|
547 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot write new redundant grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
548 | uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
|
---|
549 | }
|
---|
550 |
|
---|
551 | uOffsetSectors = pExtent->uSectorGD + VMDK_BYTE2SECTOR(cbGDRounded);
|
---|
552 | for (i = 0; i < pExtent->cGDEntries; i++)
|
---|
553 | {
|
---|
554 | pGD[i] = uOffsetSectors;
|
---|
555 | uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
|
---|
556 | /* Write the grain directory entry to disk. */
|
---|
557 | rc = RTFileWriteAt(pExtent->File, VMDK_SECTOR2BYTE(pExtent->uSectorGD) + i * sizeof(uGTSectorLE), &uGTSectorLE, sizeof(uGTSectorLE), NULL);
|
---|
558 | if (VBOX_FAILURE(rc))
|
---|
559 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot write new grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
560 | uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
|
---|
561 | }
|
---|
562 | }
|
---|
563 | pExtent->cOverheadSectors = VMDK_BYTE2SECTOR(cbOverhead);
|
---|
564 |
|
---|
565 | out:
|
---|
566 | if (VBOX_FAILURE(rc))
|
---|
567 | vmdkFreeGrainDirectory(pExtent);
|
---|
568 | return rc;
|
---|
569 | }
|
---|
570 |
|
---|
571 | static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent)
|
---|
572 | {
|
---|
573 | if (pExtent->pGD)
|
---|
574 | {
|
---|
575 | RTMemFree(pExtent->pGD);
|
---|
576 | pExtent->pGD = NULL;
|
---|
577 | }
|
---|
578 | if (pExtent->pRGD)
|
---|
579 | {
|
---|
580 | RTMemFree(pExtent->pRGD);
|
---|
581 | pExtent->pRGD = NULL;
|
---|
582 | }
|
---|
583 | }
|
---|
584 |
|
---|
585 | static int vmdkStringUnquote(PVMDKIMAGE pImage, const char *pszStr, char **ppszUnquoted, char **ppszNext)
|
---|
586 | {
|
---|
587 | char *pszQ;
|
---|
588 | char *pszUnquoted;
|
---|
589 |
|
---|
590 | /* Skip over whitespace. */
|
---|
591 | while (*pszStr == ' ' || *pszStr == '\t')
|
---|
592 | pszStr++;
|
---|
593 | if (*pszStr++ != '"')
|
---|
594 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrectly quoted value in descriptor in '%s'"), pImage->pszFilename);
|
---|
595 |
|
---|
596 | pszQ = (char*)strchr(pszStr, '"');
|
---|
597 | if (pszQ == NULL)
|
---|
598 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrectly quoted value in descriptor in '%s'"), pImage->pszFilename);
|
---|
599 | pszUnquoted = (char *)RTMemTmpAlloc(pszQ - pszStr + 1);
|
---|
600 | if (!pszUnquoted)
|
---|
601 | return VERR_NO_MEMORY;
|
---|
602 | memcpy(pszUnquoted, pszStr, pszQ - pszStr);
|
---|
603 | pszUnquoted[pszQ - pszStr] = '\0';
|
---|
604 | *ppszUnquoted = pszUnquoted;
|
---|
605 | if (ppszNext)
|
---|
606 | *ppszNext = pszQ + 1;
|
---|
607 | return VINF_SUCCESS;
|
---|
608 | }
|
---|
609 |
|
---|
610 | static int vmdkDescInitStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
611 | const char *pszLine)
|
---|
612 | {
|
---|
613 | char *pEnd = pDescriptor->aLines[pDescriptor->cLines];
|
---|
614 | ssize_t cbDiff = strlen(pszLine) + 1;
|
---|
615 |
|
---|
616 | if ( pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1
|
---|
617 | && pEnd - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
|
---|
618 | return vmdkError(pImage, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
619 |
|
---|
620 | memcpy(pEnd, pszLine, cbDiff);
|
---|
621 | pDescriptor->cLines++;
|
---|
622 | pDescriptor->aLines[pDescriptor->cLines] = pEnd + cbDiff;
|
---|
623 | pDescriptor->fDirty = true;
|
---|
624 |
|
---|
625 | return VINF_SUCCESS;
|
---|
626 | }
|
---|
627 |
|
---|
628 | static bool vmdkDescGetStr(PVMDKDESCRIPTOR pDescriptor, unsigned uStart,
|
---|
629 | const char *pszKey, const char **ppszValue)
|
---|
630 | {
|
---|
631 | size_t cbKey = strlen(pszKey);
|
---|
632 | const char *pszValue;
|
---|
633 |
|
---|
634 | while (uStart != 0)
|
---|
635 | {
|
---|
636 | if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
|
---|
637 | {
|
---|
638 | /* Key matches, check if there is a '=' (preceded by whitespace). */
|
---|
639 | pszValue = pDescriptor->aLines[uStart] + cbKey;
|
---|
640 | while (*pszValue == ' ' || *pszValue == '\t')
|
---|
641 | pszValue++;
|
---|
642 | if (*pszValue == '=')
|
---|
643 | {
|
---|
644 | *ppszValue = pszValue + 1;
|
---|
645 | break;
|
---|
646 | }
|
---|
647 | }
|
---|
648 | uStart = pDescriptor->aNextLines[uStart];
|
---|
649 | }
|
---|
650 | return !!uStart;
|
---|
651 | }
|
---|
652 |
|
---|
653 | static int vmdkDescSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
654 | unsigned uStart,
|
---|
655 | const char *pszKey, const char *pszValue)
|
---|
656 | {
|
---|
657 | char *pszTmp;
|
---|
658 | size_t cbKey = strlen(pszKey);
|
---|
659 | unsigned uLast = 0;
|
---|
660 |
|
---|
661 | while (uStart != 0)
|
---|
662 | {
|
---|
663 | if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
|
---|
664 | {
|
---|
665 | /* Key matches, check if there is a '=' (preceded by whitespace). */
|
---|
666 | pszTmp = pDescriptor->aLines[uStart] + cbKey;
|
---|
667 | while (*pszTmp == ' ' || *pszTmp == '\t')
|
---|
668 | pszTmp++;
|
---|
669 | if (*pszTmp == '=')
|
---|
670 | {
|
---|
671 | while (*pszTmp == ' ' || *pszTmp == '\t')
|
---|
672 | pszTmp++;
|
---|
673 | break;
|
---|
674 | }
|
---|
675 | }
|
---|
676 | if (!pDescriptor->aNextLines[uStart])
|
---|
677 | uLast = uStart;
|
---|
678 | uStart = pDescriptor->aNextLines[uStart];
|
---|
679 | }
|
---|
680 | if (uStart)
|
---|
681 | {
|
---|
682 | if (pszValue)
|
---|
683 | {
|
---|
684 | /* Key already exists, replace existing value. */
|
---|
685 | size_t cbOldVal = strlen(pszTmp);
|
---|
686 | size_t cbNewVal = strlen(pszValue);
|
---|
687 | ssize_t cbDiff = cbNewVal - cbOldVal;
|
---|
688 | /* Check for buffer overflow. */
|
---|
689 | if ( pDescriptor->aLines[pDescriptor->cLines]
|
---|
690 | - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
|
---|
691 | return vmdkError(pImage, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
692 |
|
---|
693 | memmove(pszTmp + cbNewVal, pszTmp + cbOldVal,
|
---|
694 | pDescriptor->aLines[pDescriptor->cLines] - pszTmp - cbOldVal);
|
---|
695 | memcpy(pszTmp, pszValue, cbNewVal + 1);
|
---|
696 | for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
|
---|
697 | pDescriptor->aLines[i] += cbDiff;
|
---|
698 | }
|
---|
699 | else
|
---|
700 | {
|
---|
701 | memmove(pDescriptor->aLines[uStart], pDescriptor->aLines[uStart+1],
|
---|
702 | pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uStart+1] + 1);
|
---|
703 | for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
|
---|
704 | {
|
---|
705 | pDescriptor->aLines[i-1] = pDescriptor->aLines[i];
|
---|
706 | if (pDescriptor->aNextLines[i])
|
---|
707 | pDescriptor->aNextLines[i-1] = pDescriptor->aNextLines[i] - 1;
|
---|
708 | else
|
---|
709 | pDescriptor->aNextLines[i-1] = 0;
|
---|
710 | }
|
---|
711 | pDescriptor->cLines--;
|
---|
712 | /* Adjust starting line numbers of following descriptor sections. */
|
---|
713 | if (uStart < pDescriptor->uFirstExtent)
|
---|
714 | pDescriptor->uFirstExtent--;
|
---|
715 | if (uStart < pDescriptor->uFirstDDB)
|
---|
716 | pDescriptor->uFirstDDB--;
|
---|
717 | }
|
---|
718 | }
|
---|
719 | else
|
---|
720 | {
|
---|
721 | /* Key doesn't exist, append it after the last entry in this category. */
|
---|
722 | if (!pszValue)
|
---|
723 | {
|
---|
724 | /* Key doesn't exist, and it should be removed. Simply a no-op. */
|
---|
725 | return VINF_SUCCESS;
|
---|
726 | }
|
---|
727 | size_t cbKey = strlen(pszKey);
|
---|
728 | size_t cbValue = strlen(pszValue);
|
---|
729 | ssize_t cbDiff = cbKey + 1 + cbValue + 1;
|
---|
730 | /* Check for buffer overflow. */
|
---|
731 | if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
|
---|
732 | || ( pDescriptor->aLines[pDescriptor->cLines]
|
---|
733 | - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
|
---|
734 | return vmdkError(pImage, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
735 | for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
|
---|
736 | {
|
---|
737 | pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
|
---|
738 | if (pDescriptor->aNextLines[i - 1])
|
---|
739 | pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
|
---|
740 | else
|
---|
741 | pDescriptor->aNextLines[i] = 0;
|
---|
742 | }
|
---|
743 | uStart = uLast + 1;
|
---|
744 | pDescriptor->aNextLines[uLast] = uStart;
|
---|
745 | pDescriptor->aNextLines[uStart] = 0;
|
---|
746 | pDescriptor->cLines++;
|
---|
747 | pszTmp = pDescriptor->aLines[uStart];
|
---|
748 | memmove(pszTmp + cbDiff, pszTmp,
|
---|
749 | pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
|
---|
750 | memcpy(pDescriptor->aLines[uStart], pszKey, cbKey);
|
---|
751 | pDescriptor->aLines[uStart][cbKey] = '=';
|
---|
752 | memcpy(pDescriptor->aLines[uStart] + cbKey + 1, pszValue, cbValue + 1);
|
---|
753 | for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
|
---|
754 | pDescriptor->aLines[i] += cbDiff;
|
---|
755 |
|
---|
756 | /* Adjust starting line numbers of following descriptor sections. */
|
---|
757 | if (uStart <= pDescriptor->uFirstExtent)
|
---|
758 | pDescriptor->uFirstExtent++;
|
---|
759 | if (uStart <= pDescriptor->uFirstDDB)
|
---|
760 | pDescriptor->uFirstDDB++;
|
---|
761 | }
|
---|
762 | pDescriptor->fDirty = true;
|
---|
763 | return VINF_SUCCESS;
|
---|
764 | }
|
---|
765 |
|
---|
766 | static int vmdkDescBaseGetU32(PVMDKDESCRIPTOR pDescriptor, const char *pszKey,
|
---|
767 | uint32_t *puValue)
|
---|
768 | {
|
---|
769 | const char *pszValue;
|
---|
770 |
|
---|
771 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey, &pszValue))
|
---|
772 | return VERR_VDI_VALUE_NOT_FOUND;
|
---|
773 | return RTStrToUInt32Ex(pszValue, NULL, 10, puValue);
|
---|
774 | }
|
---|
775 |
|
---|
776 | static int vmdkDescBaseGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
777 | const char *pszKey, const char **ppszValue)
|
---|
778 | {
|
---|
779 | const char *pszValue;
|
---|
780 | char *pszValueUnquoted;
|
---|
781 |
|
---|
782 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey, &pszValue))
|
---|
783 | return VERR_VDI_VALUE_NOT_FOUND;
|
---|
784 | int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
|
---|
785 | if (VBOX_FAILURE(rc))
|
---|
786 | return rc;
|
---|
787 | *ppszValue = pszValueUnquoted;
|
---|
788 | return rc;
|
---|
789 | }
|
---|
790 |
|
---|
791 | static int vmdkDescBaseSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
792 | const char *pszKey, const char *pszValue)
|
---|
793 | {
|
---|
794 | char *pszValueQuoted;
|
---|
795 |
|
---|
796 | int rc = RTStrAPrintf(&pszValueQuoted, "\"%s\"", pszValue);
|
---|
797 | if (VBOX_FAILURE(rc))
|
---|
798 | return rc;
|
---|
799 | rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc, pszKey, pszValueQuoted);
|
---|
800 | RTStrFree(pszValueQuoted);
|
---|
801 | return rc;
|
---|
802 | }
|
---|
803 |
|
---|
804 | static void vmdkDescExtRemoveDummy(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor)
|
---|
805 | {
|
---|
806 | unsigned uEntry = pDescriptor->uFirstExtent;
|
---|
807 | ssize_t cbDiff;
|
---|
808 |
|
---|
809 | if (!uEntry)
|
---|
810 | return;
|
---|
811 |
|
---|
812 | cbDiff = strlen(pDescriptor->aLines[uEntry]) + 1;
|
---|
813 | /* Move everything including the \0 in the entry marking the end of buffer. */
|
---|
814 | memmove(pDescriptor->aLines[uEntry], pDescriptor->aLines[uEntry + 1],
|
---|
815 | pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uEntry + 1] + 1);
|
---|
816 | for (unsigned i = uEntry + 1; i <= pDescriptor->cLines; i++)
|
---|
817 | {
|
---|
818 | pDescriptor->aLines[i - 1] = pDescriptor->aLines[i] - cbDiff;
|
---|
819 | if (pDescriptor->aNextLines[i])
|
---|
820 | pDescriptor->aNextLines[i - 1] = pDescriptor->aNextLines[i] - 1;
|
---|
821 | else
|
---|
822 | pDescriptor->aNextLines[i - 1] = 0;
|
---|
823 | }
|
---|
824 | pDescriptor->cLines--;
|
---|
825 | if (pDescriptor->uFirstDDB)
|
---|
826 | pDescriptor->uFirstDDB--;
|
---|
827 |
|
---|
828 | return;
|
---|
829 | }
|
---|
830 |
|
---|
831 | static int vmdkDescExtInsert(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
832 | VMDKACCESS enmAccess, uint64_t cNominalSectors,
|
---|
833 | VMDKETYPE enmType, const char *pszBasename,
|
---|
834 | uint64_t uSectorOffset)
|
---|
835 | {
|
---|
836 | static const char *apszAccess[] = { "NOACCESS", "RDONLY", "RW" };
|
---|
837 | static const char *apszType[] = { "", "SPARSE", "FLAT", "ZERO" };
|
---|
838 | char *pszTmp;
|
---|
839 | unsigned uStart = pDescriptor->uFirstExtent, uLast = 0;
|
---|
840 | char szExt[1024];
|
---|
841 | ssize_t cbDiff;
|
---|
842 |
|
---|
843 | /* Find last entry in extent description. */
|
---|
844 | while (uStart)
|
---|
845 | {
|
---|
846 | if (!pDescriptor->aNextLines[uStart])
|
---|
847 | uLast = uStart;
|
---|
848 | uStart = pDescriptor->aNextLines[uStart];
|
---|
849 | }
|
---|
850 |
|
---|
851 | if (enmType == VMDKETYPE_ZERO)
|
---|
852 | {
|
---|
853 | RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s ", apszAccess[enmAccess],
|
---|
854 | cNominalSectors, apszType[enmType]);
|
---|
855 | }
|
---|
856 | else
|
---|
857 | {
|
---|
858 | if (!uSectorOffset)
|
---|
859 | RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\"",
|
---|
860 | apszAccess[enmAccess], cNominalSectors,
|
---|
861 | apszType[enmType], pszBasename);
|
---|
862 | else
|
---|
863 | RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\" %llu",
|
---|
864 | apszAccess[enmAccess], cNominalSectors,
|
---|
865 | apszType[enmType], pszBasename, uSectorOffset);
|
---|
866 | }
|
---|
867 | cbDiff = strlen(szExt) + 1;
|
---|
868 |
|
---|
869 | /* Check for buffer overflow. */
|
---|
870 | if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
|
---|
871 | || ( pDescriptor->aLines[pDescriptor->cLines]
|
---|
872 | - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
|
---|
873 | return vmdkError(pImage, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
874 |
|
---|
875 | for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
|
---|
876 | {
|
---|
877 | pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
|
---|
878 | if (pDescriptor->aNextLines[i - 1])
|
---|
879 | pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
|
---|
880 | else
|
---|
881 | pDescriptor->aNextLines[i] = 0;
|
---|
882 | }
|
---|
883 | uStart = uLast + 1;
|
---|
884 | pDescriptor->aNextLines[uLast] = uStart;
|
---|
885 | pDescriptor->aNextLines[uStart] = 0;
|
---|
886 | pDescriptor->cLines++;
|
---|
887 | pszTmp = pDescriptor->aLines[uStart];
|
---|
888 | memmove(pszTmp + cbDiff, pszTmp,
|
---|
889 | pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
|
---|
890 | memcpy(pDescriptor->aLines[uStart], szExt, cbDiff);
|
---|
891 | for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
|
---|
892 | pDescriptor->aLines[i] += cbDiff;
|
---|
893 |
|
---|
894 | /* Adjust starting line numbers of following descriptor sections. */
|
---|
895 | if (uStart <= pDescriptor->uFirstDDB)
|
---|
896 | pDescriptor->uFirstDDB++;
|
---|
897 |
|
---|
898 | pDescriptor->fDirty = true;
|
---|
899 | return VINF_SUCCESS;
|
---|
900 | }
|
---|
901 |
|
---|
902 | static int vmdkDescDDBGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
903 | const char *pszKey, const char **ppszValue)
|
---|
904 | {
|
---|
905 | const char *pszValue;
|
---|
906 | char *pszValueUnquoted;
|
---|
907 |
|
---|
908 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey, &pszValue))
|
---|
909 | return VERR_VDI_VALUE_NOT_FOUND;
|
---|
910 | int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
|
---|
911 | if (VBOX_FAILURE(rc))
|
---|
912 | return rc;
|
---|
913 | *ppszValue = pszValueUnquoted;
|
---|
914 | return rc;
|
---|
915 | }
|
---|
916 |
|
---|
917 | static int vmdkDescDDBGetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
918 | const char *pszKey, uint32_t *puValue)
|
---|
919 | {
|
---|
920 | const char *pszValue;
|
---|
921 | char *pszValueUnquoted;
|
---|
922 |
|
---|
923 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey, &pszValue))
|
---|
924 | return VERR_VDI_VALUE_NOT_FOUND;
|
---|
925 | int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
|
---|
926 | if (VBOX_FAILURE(rc))
|
---|
927 | return rc;
|
---|
928 | rc = RTStrToUInt32Ex(pszValueUnquoted, NULL, 10, puValue);
|
---|
929 | RTMemTmpFree(pszValueUnquoted);
|
---|
930 | return rc;
|
---|
931 | }
|
---|
932 |
|
---|
933 | static int vmdkDescDDBGetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
934 | const char *pszKey, PRTUUID pUuid)
|
---|
935 | {
|
---|
936 | const char *pszValue;
|
---|
937 | char *pszValueUnquoted;
|
---|
938 |
|
---|
939 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey, &pszValue))
|
---|
940 | return VERR_VDI_VALUE_NOT_FOUND;
|
---|
941 | int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
|
---|
942 | if (VBOX_FAILURE(rc))
|
---|
943 | return rc;
|
---|
944 | rc = RTUuidFromStr(pUuid, pszValueUnquoted);
|
---|
945 | RTMemTmpFree(pszValueUnquoted);
|
---|
946 | return rc;
|
---|
947 | }
|
---|
948 |
|
---|
949 | static int vmdkDescDDBSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor, const char *pszKey, const char *pszVal)
|
---|
950 | {
|
---|
951 | int rc;
|
---|
952 | char *pszValQuoted;
|
---|
953 |
|
---|
954 | if (pszVal)
|
---|
955 | {
|
---|
956 | rc = RTStrAPrintf(&pszValQuoted, "\"%s\"", pszVal);
|
---|
957 | if (VBOX_FAILURE(rc))
|
---|
958 | return rc;
|
---|
959 | }
|
---|
960 | else
|
---|
961 | pszValQuoted = NULL;
|
---|
962 | rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey, pszValQuoted);
|
---|
963 | if (pszValQuoted)
|
---|
964 | RTStrFree(pszValQuoted);
|
---|
965 | return rc;
|
---|
966 | }
|
---|
967 |
|
---|
968 | static int vmdkDescDDBSetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor, const char *pszKey, PCRTUUID pUuid)
|
---|
969 | {
|
---|
970 | char *pszUuid;
|
---|
971 |
|
---|
972 | int rc = RTStrAPrintf(&pszUuid, "\"%Vuuid\"", pUuid);
|
---|
973 | if (VBOX_FAILURE(rc))
|
---|
974 | return rc;
|
---|
975 | rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey, pszUuid);
|
---|
976 | RTStrFree(pszUuid);
|
---|
977 | return rc;
|
---|
978 | }
|
---|
979 |
|
---|
980 | int vmdkDescDDBSetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor, const char *pszKey, uint32_t uValue)
|
---|
981 | {
|
---|
982 | char *pszValue;
|
---|
983 |
|
---|
984 | int rc = RTStrAPrintf(&pszValue, "\"%d\"", uValue);
|
---|
985 | if (VBOX_FAILURE(rc))
|
---|
986 | return rc;
|
---|
987 | rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey, pszValue);
|
---|
988 | RTStrFree(pszValue);
|
---|
989 | return rc;
|
---|
990 | }
|
---|
991 |
|
---|
992 | static int vmdkPreprocessDescriptor(PVMDKIMAGE pImage, char *pDescData, size_t cbDescData, PVMDKDESCRIPTOR pDescriptor)
|
---|
993 | {
|
---|
994 | int rc = VINF_SUCCESS;
|
---|
995 | unsigned cLine = 0, uLastNonEmptyLine = 0;
|
---|
996 | char *pTmp = pDescData;
|
---|
997 |
|
---|
998 | pDescriptor->cbDescAlloc = cbDescData;
|
---|
999 | while (*pTmp != '\0')
|
---|
1000 | {
|
---|
1001 | pDescriptor->aLines[cLine++] = pTmp;
|
---|
1002 | if (cLine >= VMDK_DESCRIPTOR_LINES_MAX)
|
---|
1003 | {
|
---|
1004 | rc = vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
1005 | goto out;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | while (*pTmp != '\0' && *pTmp != '\n')
|
---|
1009 | {
|
---|
1010 | if (*pTmp == '\r')
|
---|
1011 | {
|
---|
1012 | if (*(pTmp + 1) != '\n')
|
---|
1013 | {
|
---|
1014 | rc = vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: unsupported end of line in descriptor in '%s'"), pImage->pszFilename);
|
---|
1015 | goto out;
|
---|
1016 | }
|
---|
1017 | else
|
---|
1018 | {
|
---|
1019 | /* Get rid of CR character. */
|
---|
1020 | *pTmp = '\0';
|
---|
1021 | }
|
---|
1022 | }
|
---|
1023 | pTmp++;
|
---|
1024 | }
|
---|
1025 | /* Get rid of LF character. */
|
---|
1026 | if (*pTmp == '\n')
|
---|
1027 | {
|
---|
1028 | *pTmp = '\0';
|
---|
1029 | pTmp++;
|
---|
1030 | }
|
---|
1031 | }
|
---|
1032 | pDescriptor->cLines = cLine;
|
---|
1033 | /* Pointer right after the end of the used part of the buffer. */
|
---|
1034 | pDescriptor->aLines[cLine] = pTmp;
|
---|
1035 |
|
---|
1036 | if (strcmp(pDescriptor->aLines[0], "# Disk DescriptorFile"))
|
---|
1037 | {
|
---|
1038 | rc = vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor does not start as expected in '%s'"), pImage->pszFilename);
|
---|
1039 | goto out;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | /* Initialize those, because we need to be able to reopen an image. */
|
---|
1043 | pDescriptor->uFirstDesc = 0;
|
---|
1044 | pDescriptor->uFirstExtent = 0;
|
---|
1045 | pDescriptor->uFirstDDB = 0;
|
---|
1046 | for (unsigned i = 0; i < cLine; i++)
|
---|
1047 | {
|
---|
1048 | if (*pDescriptor->aLines[i] != '#' && *pDescriptor->aLines[i] != '\0')
|
---|
1049 | {
|
---|
1050 | if ( !strncmp(pDescriptor->aLines[i], "RW", 2)
|
---|
1051 | || !strncmp(pDescriptor->aLines[i], "RDONLY", 6)
|
---|
1052 | || !strncmp(pDescriptor->aLines[i], "NOACCESS", 8) )
|
---|
1053 | {
|
---|
1054 | /* An extent descriptor. */
|
---|
1055 | if (!pDescriptor->uFirstDesc || pDescriptor->uFirstDDB)
|
---|
1056 | {
|
---|
1057 | /* Incorrect ordering of entries. */
|
---|
1058 | rc = vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
|
---|
1059 | goto out;
|
---|
1060 | }
|
---|
1061 | if (!pDescriptor->uFirstExtent)
|
---|
1062 | {
|
---|
1063 | pDescriptor->uFirstExtent = i;
|
---|
1064 | uLastNonEmptyLine = 0;
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 | else if (!strncmp(pDescriptor->aLines[i], "ddb.", 4))
|
---|
1068 | {
|
---|
1069 | /* A disk database entry. */
|
---|
1070 | if (!pDescriptor->uFirstDesc || !pDescriptor->uFirstExtent)
|
---|
1071 | {
|
---|
1072 | /* Incorrect ordering of entries. */
|
---|
1073 | rc = vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
|
---|
1074 | goto out;
|
---|
1075 | }
|
---|
1076 | if (!pDescriptor->uFirstDDB)
|
---|
1077 | {
|
---|
1078 | pDescriptor->uFirstDDB = i;
|
---|
1079 | uLastNonEmptyLine = 0;
|
---|
1080 | }
|
---|
1081 | }
|
---|
1082 | else
|
---|
1083 | {
|
---|
1084 | /* A normal entry. */
|
---|
1085 | if (pDescriptor->uFirstExtent || pDescriptor->uFirstDDB)
|
---|
1086 | {
|
---|
1087 | /* Incorrect ordering of entries. */
|
---|
1088 | rc = vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
|
---|
1089 | goto out;
|
---|
1090 | }
|
---|
1091 | if (!pDescriptor->uFirstDesc)
|
---|
1092 | {
|
---|
1093 | pDescriptor->uFirstDesc = i;
|
---|
1094 | uLastNonEmptyLine = 0;
|
---|
1095 | }
|
---|
1096 | }
|
---|
1097 | if (uLastNonEmptyLine)
|
---|
1098 | pDescriptor->aNextLines[uLastNonEmptyLine] = i;
|
---|
1099 | uLastNonEmptyLine = i;
|
---|
1100 | }
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | out:
|
---|
1104 | return rc;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | static int vmdkCreateDescriptor(PVMDKIMAGE pImage, char *pDescData, size_t cbDescData, PVMDKDESCRIPTOR pDescriptor)
|
---|
1108 | {
|
---|
1109 | int rc;
|
---|
1110 |
|
---|
1111 | pDescriptor->uFirstDesc = 0;
|
---|
1112 | pDescriptor->uFirstExtent = 0;
|
---|
1113 | pDescriptor->uFirstDDB = 0;
|
---|
1114 | pDescriptor->cLines = 0;
|
---|
1115 | pDescriptor->cbDescAlloc = cbDescData;
|
---|
1116 | pDescriptor->fDirty = false;
|
---|
1117 | pDescriptor->aLines[pDescriptor->cLines] = pDescData;
|
---|
1118 | memset(pDescriptor->aNextLines, '\0', sizeof(pDescriptor->aNextLines));
|
---|
1119 |
|
---|
1120 | rc = vmdkDescInitStr(pImage, pDescriptor, "# Disk DescriptorFile");
|
---|
1121 | if (VBOX_FAILURE(rc))
|
---|
1122 | goto out;
|
---|
1123 | rc = vmdkDescInitStr(pImage, pDescriptor, "version=1");
|
---|
1124 | if (VBOX_FAILURE(rc))
|
---|
1125 | goto out;
|
---|
1126 | pDescriptor->uFirstDesc = pDescriptor->cLines - 1;
|
---|
1127 | rc = vmdkDescInitStr(pImage, pDescriptor, "");
|
---|
1128 | if (VBOX_FAILURE(rc))
|
---|
1129 | goto out;
|
---|
1130 | rc = vmdkDescInitStr(pImage, pDescriptor, "# Extent description");
|
---|
1131 | if (VBOX_FAILURE(rc))
|
---|
1132 | goto out;
|
---|
1133 | rc = vmdkDescInitStr(pImage, pDescriptor, "NOACCESS 0 ZERO ");
|
---|
1134 | if (VBOX_FAILURE(rc))
|
---|
1135 | goto out;
|
---|
1136 | pDescriptor->uFirstExtent = pDescriptor->cLines - 1;
|
---|
1137 | rc = vmdkDescInitStr(pImage, pDescriptor, "");
|
---|
1138 | if (VBOX_FAILURE(rc))
|
---|
1139 | goto out;
|
---|
1140 | /* The trailing space is created by VMware, too. */
|
---|
1141 | rc = vmdkDescInitStr(pImage, pDescriptor, "# The disk Data Base ");
|
---|
1142 | if (VBOX_FAILURE(rc))
|
---|
1143 | goto out;
|
---|
1144 | rc = vmdkDescInitStr(pImage, pDescriptor, "#DDB");
|
---|
1145 | if (VBOX_FAILURE(rc))
|
---|
1146 | goto out;
|
---|
1147 | rc = vmdkDescInitStr(pImage, pDescriptor, "");
|
---|
1148 | if (VBOX_FAILURE(rc))
|
---|
1149 | goto out;
|
---|
1150 | rc = vmdkDescInitStr(pImage, pDescriptor, "ddb.virtualHWVersion = \"4\"");
|
---|
1151 | if (VBOX_FAILURE(rc))
|
---|
1152 | goto out;
|
---|
1153 | pDescriptor->uFirstDDB = pDescriptor->cLines - 1;
|
---|
1154 |
|
---|
1155 | /* Now that the framework is in place, use the normal functions to insert
|
---|
1156 | * the remaining keys. */
|
---|
1157 | char szBuf[9];
|
---|
1158 | RTStrPrintf(szBuf, sizeof(szBuf), "%08x", RTRandU32());
|
---|
1159 | rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc, "CID", szBuf);
|
---|
1160 | if (VBOX_FAILURE(rc))
|
---|
1161 | goto out;
|
---|
1162 | rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc, "parentCID", "ffffffff");
|
---|
1163 | if (VBOX_FAILURE(rc))
|
---|
1164 | goto out;
|
---|
1165 |
|
---|
1166 | rc = vmdkDescDDBSetStr(pImage, pDescriptor, "ddb.adapterType", "ide");
|
---|
1167 | if (VBOX_FAILURE(rc))
|
---|
1168 | goto out;
|
---|
1169 |
|
---|
1170 | out:
|
---|
1171 | return rc;
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | static int vmdkParseDescriptor(PVMDKIMAGE pImage, char *pDescData, size_t cbDescData)
|
---|
1175 | {
|
---|
1176 | int rc;
|
---|
1177 | unsigned cExtents;
|
---|
1178 | unsigned uLine;
|
---|
1179 |
|
---|
1180 | rc = vmdkPreprocessDescriptor(pImage, pDescData, cbDescData, &pImage->Descriptor);
|
---|
1181 | if (VBOX_FAILURE(rc))
|
---|
1182 | return rc;
|
---|
1183 |
|
---|
1184 | /* Check version, must be 1. */
|
---|
1185 | uint32_t uVersion;
|
---|
1186 | rc = vmdkDescBaseGetU32(&pImage->Descriptor, "version", &uVersion);
|
---|
1187 | if (VBOX_FAILURE(rc))
|
---|
1188 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error finding key 'version' in descriptor in '%s'"), pImage->pszFilename);
|
---|
1189 | if (uVersion != 1)
|
---|
1190 | return vmdkError(pImage, VERR_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VMDK: unsupported format version in descriptor in '%s'"), pImage->pszFilename);
|
---|
1191 |
|
---|
1192 | /* Get image creation type and determine image flags. */
|
---|
1193 | const char *pszCreateType;
|
---|
1194 | rc = vmdkDescBaseGetStr(pImage, &pImage->Descriptor, "createType",
|
---|
1195 | &pszCreateType);
|
---|
1196 | if (VBOX_FAILURE(rc))
|
---|
1197 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: cannot get image type from descriptor in '%s'"), pImage->pszFilename);
|
---|
1198 | if ( !strcmp(pszCreateType, "twoGbMaxExtentSparse")
|
---|
1199 | || !strcmp(pszCreateType, "twoGbMaxExtentFlat"))
|
---|
1200 | pImage->uImageFlags = VD_VMDK_IMAGE_FLAGS_SPLIT_2G;
|
---|
1201 | if ( !strcmp(pszCreateType, "partitionedDevice")
|
---|
1202 | || !strcmp(pszCreateType, "fullDevice"))
|
---|
1203 | pImage->uImageFlags = VD_VMDK_IMAGE_FLAGS_RAWDISK;
|
---|
1204 | else
|
---|
1205 | pImage->uImageFlags = 0;
|
---|
1206 | RTStrFree((char *)(void *)pszCreateType);
|
---|
1207 |
|
---|
1208 | /* Count the number of extent config entries. */
|
---|
1209 | for (uLine = pImage->Descriptor.uFirstExtent, cExtents = 0;
|
---|
1210 | uLine != 0;
|
---|
1211 | uLine = pImage->Descriptor.aNextLines[uLine], cExtents++)
|
---|
1212 | /* nothing */;
|
---|
1213 |
|
---|
1214 | if (!pImage->pDescData && cExtents != 1)
|
---|
1215 | {
|
---|
1216 | /* Monolithic image, must have only one extent (already opened). */
|
---|
1217 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: monolithic image may only have one extent in '%s'"), pImage->pszFilename);
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | if (pImage->pDescData)
|
---|
1221 | {
|
---|
1222 | /* Non-monolithic image, extents need to be allocated. */
|
---|
1223 | rc = vmdkCreateExtents(pImage, cExtents);
|
---|
1224 | if (VBOX_FAILURE(rc))
|
---|
1225 | return rc;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | for (unsigned i = 0, uLine = pImage->Descriptor.uFirstExtent;
|
---|
1229 | i < cExtents; i++, uLine = pImage->Descriptor.aNextLines[uLine])
|
---|
1230 | {
|
---|
1231 | char *pszLine = pImage->Descriptor.aLines[uLine];
|
---|
1232 |
|
---|
1233 | /* Access type of the extent. */
|
---|
1234 | if (!strncmp(pszLine, "RW", 2))
|
---|
1235 | {
|
---|
1236 | pImage->pExtents[i].enmAccess = VMDKACCESS_READWRITE;
|
---|
1237 | pszLine += 2;
|
---|
1238 | }
|
---|
1239 | else if (!strncmp(pszLine, "RDONLY", 6))
|
---|
1240 | {
|
---|
1241 | pImage->pExtents[i].enmAccess = VMDKACCESS_READONLY;
|
---|
1242 | pszLine += 6;
|
---|
1243 | }
|
---|
1244 | else if (!strncmp(pszLine, "NOACCESS", 8))
|
---|
1245 | {
|
---|
1246 | pImage->pExtents[i].enmAccess = VMDKACCESS_NOACCESS;
|
---|
1247 | pszLine += 8;
|
---|
1248 | }
|
---|
1249 | else
|
---|
1250 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
1251 | if (*pszLine++ != ' ')
|
---|
1252 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
1253 |
|
---|
1254 | /* Nominal size of the extent. */
|
---|
1255 | rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
|
---|
1256 | &pImage->pExtents[i].cNominalSectors);
|
---|
1257 | if (VBOX_FAILURE(rc))
|
---|
1258 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
1259 | if (*pszLine++ != ' ')
|
---|
1260 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
1261 |
|
---|
1262 | /* Type of the extent. */
|
---|
1263 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
1264 | /** @todo Add the ESX extent types. Not necessary for now because
|
---|
1265 | * the ESX extent types are only used inside an ESX server. They are
|
---|
1266 | * automatically converted if the VMDK is exported. */
|
---|
1267 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
1268 | if (!strncmp(pszLine, "SPARSE", 6))
|
---|
1269 | {
|
---|
1270 | pImage->pExtents[i].enmType = VMDKETYPE_HOSTED_SPARSE;
|
---|
1271 | pszLine += 6;
|
---|
1272 | }
|
---|
1273 | else if (!strncmp(pszLine, "FLAT", 4))
|
---|
1274 | {
|
---|
1275 | pImage->pExtents[i].enmType = VMDKETYPE_FLAT;
|
---|
1276 | pszLine += 4;
|
---|
1277 | }
|
---|
1278 | else if (!strncmp(pszLine, "ZERO", 4))
|
---|
1279 | {
|
---|
1280 | pImage->pExtents[i].enmType = VMDKETYPE_ZERO;
|
---|
1281 | pszLine += 4;
|
---|
1282 | }
|
---|
1283 | else
|
---|
1284 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
1285 | if (pImage->pExtents[i].enmType == VMDKETYPE_ZERO)
|
---|
1286 | {
|
---|
1287 | /* This one has no basename or offset. */
|
---|
1288 | if (*pszLine == ' ')
|
---|
1289 | pszLine++;
|
---|
1290 | if (*pszLine != '\0')
|
---|
1291 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
1292 | pImage->pExtents[i].pszBasename = NULL;
|
---|
1293 | }
|
---|
1294 | else
|
---|
1295 | {
|
---|
1296 | /* All other extent types have basename and optional offset. */
|
---|
1297 | if (*pszLine++ != ' ')
|
---|
1298 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
1299 |
|
---|
1300 | /* Basename of the image. Surrounded by quotes. */
|
---|
1301 | char *pszBasename;
|
---|
1302 | rc = vmdkStringUnquote(pImage, pszLine, &pszBasename, &pszLine);
|
---|
1303 | if (VBOX_FAILURE(rc))
|
---|
1304 | return rc;
|
---|
1305 | pImage->pExtents[i].pszBasename = pszBasename;
|
---|
1306 | if (*pszLine == ' ')
|
---|
1307 | {
|
---|
1308 | pszLine++;
|
---|
1309 | if (*pszLine != '\0')
|
---|
1310 | {
|
---|
1311 | /* Optional offset in extent specified. */
|
---|
1312 | rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
|
---|
1313 | &pImage->pExtents[i].uSectorOffset);
|
---|
1314 | if (VBOX_FAILURE(rc))
|
---|
1315 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
1316 | }
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | if (*pszLine != '\0')
|
---|
1320 | return vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
1321 | }
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
|
---|
1325 | "ddb.geometry.cylinders", &pImage->cCylinders);
|
---|
1326 | if (VBOX_FAILURE(rc))
|
---|
1327 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error getting CHS geometry from extent description in '%s'"), pImage->pszFilename);
|
---|
1328 | rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
|
---|
1329 | "ddb.geometry.heads", &pImage->cHeads);
|
---|
1330 | if (VBOX_FAILURE(rc))
|
---|
1331 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error getting CHS geometry from extent description in '%s'"), pImage->pszFilename);
|
---|
1332 | rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
|
---|
1333 | "ddb.geometry.sectors", &pImage->cSectors);
|
---|
1334 | if (VBOX_FAILURE(rc))
|
---|
1335 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error getting CHS geometry from extent description in '%s'"), pImage->pszFilename);
|
---|
1336 | if (pImage->cCylinders >= 1024 || pImage->cHeads != 16)
|
---|
1337 | pImage->enmTranslation = PDMBIOSTRANSLATION_LBA;
|
---|
1338 | else
|
---|
1339 | pImage->enmTranslation = PDMBIOSTRANSLATION_NONE;
|
---|
1340 |
|
---|
1341 | /* Get image UUID. */
|
---|
1342 | rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, "ddb.uuid.image",
|
---|
1343 | &pImage->ImageUuid);
|
---|
1344 | if (rc == VERR_VDI_VALUE_NOT_FOUND)
|
---|
1345 | {
|
---|
1346 | /* Image without UUID. Probably created by VMware and not yet used
|
---|
1347 | * by VirtualBox. Can only be added for images opened in read/write
|
---|
1348 | * mode, so don't bother producing a sensible UUID otherwise. */
|
---|
1349 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1350 | RTUuidClear(&pImage->ImageUuid);
|
---|
1351 | else
|
---|
1352 | {
|
---|
1353 | rc = RTUuidCreate(&pImage->ImageUuid);
|
---|
1354 | if (VBOX_FAILURE(rc))
|
---|
1355 | return rc;
|
---|
1356 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
1357 | "ddb.uuid.image", &pImage->ImageUuid);
|
---|
1358 | if (VBOX_FAILURE(rc))
|
---|
1359 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
1360 | }
|
---|
1361 | }
|
---|
1362 | else if (VBOX_FAILURE(rc))
|
---|
1363 | return rc;
|
---|
1364 |
|
---|
1365 | /* Get image modification UUID. */
|
---|
1366 | rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, "ddb.uuid.modification",
|
---|
1367 | &pImage->ModificationUuid);
|
---|
1368 | if (rc == VERR_VDI_VALUE_NOT_FOUND)
|
---|
1369 | {
|
---|
1370 | /* Image without UUID. Probably created by VMware and not yet used
|
---|
1371 | * by VirtualBox. Can only be added for images opened in read/write
|
---|
1372 | * mode, so don't bother producing a sensible UUID otherwise. */
|
---|
1373 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1374 | RTUuidClear(&pImage->ModificationUuid);
|
---|
1375 | else
|
---|
1376 | {
|
---|
1377 | rc = RTUuidCreate(&pImage->ModificationUuid);
|
---|
1378 | if (VBOX_FAILURE(rc))
|
---|
1379 | return rc;
|
---|
1380 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
1381 | "ddb.uuid.modification", &pImage->ModificationUuid);
|
---|
1382 | if (VBOX_FAILURE(rc))
|
---|
1383 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing image modification UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
1384 | }
|
---|
1385 | }
|
---|
1386 | else if (VBOX_FAILURE(rc))
|
---|
1387 | return rc;
|
---|
1388 |
|
---|
1389 | /* Get UUID of parent image. */
|
---|
1390 | rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, "ddb.uuid.parent",
|
---|
1391 | &pImage->ParentUuid);
|
---|
1392 | if (rc == VERR_VDI_VALUE_NOT_FOUND)
|
---|
1393 | {
|
---|
1394 | /* Image without UUID. Probably created by VMware and not yet used
|
---|
1395 | * by VirtualBox. Can only be added for images opened in read/write
|
---|
1396 | * mode, so don't bother producing a sensible UUID otherwise. */
|
---|
1397 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1398 | RTUuidClear(&pImage->ParentUuid);
|
---|
1399 | else
|
---|
1400 | {
|
---|
1401 | rc = RTUuidClear(&pImage->ParentUuid);
|
---|
1402 | if (VBOX_FAILURE(rc))
|
---|
1403 | return rc;
|
---|
1404 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
1405 | "ddb.uuid.parent", &pImage->ParentUuid);
|
---|
1406 | if (VBOX_FAILURE(rc))
|
---|
1407 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing parent UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
1408 | }
|
---|
1409 | }
|
---|
1410 | else if (VBOX_FAILURE(rc))
|
---|
1411 | return rc;
|
---|
1412 |
|
---|
1413 | return VINF_SUCCESS;
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | static int vmdkWriteDescriptor(PVMDKIMAGE pImage)
|
---|
1417 | {
|
---|
1418 | int rc = VINF_SUCCESS;
|
---|
1419 | uint64_t cbLimit;
|
---|
1420 | uint64_t uOffset;
|
---|
1421 | RTFILE DescFile;
|
---|
1422 |
|
---|
1423 | if (pImage->pDescData)
|
---|
1424 | {
|
---|
1425 | /* Separate descriptor file. */
|
---|
1426 | uOffset = 0;
|
---|
1427 | cbLimit = 0;
|
---|
1428 | DescFile = pImage->File;
|
---|
1429 | }
|
---|
1430 | else
|
---|
1431 | {
|
---|
1432 | /* Embedded descriptor file. */
|
---|
1433 | uOffset = VMDK_SECTOR2BYTE(pImage->pExtents[0].uDescriptorSector);
|
---|
1434 | cbLimit = VMDK_SECTOR2BYTE(pImage->pExtents[0].cDescriptorSectors);
|
---|
1435 | cbLimit += uOffset;
|
---|
1436 | DescFile = pImage->pExtents[0].File;
|
---|
1437 | }
|
---|
1438 | for (unsigned i = 0; i < pImage->Descriptor.cLines; i++)
|
---|
1439 | {
|
---|
1440 | const char *psz = pImage->Descriptor.aLines[i];
|
---|
1441 | size_t cb = strlen(psz);
|
---|
1442 |
|
---|
1443 | if (cbLimit && uOffset + cb + 1 > cbLimit)
|
---|
1444 | return vmdkError(pImage, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too long in '%s'"), pImage->pszFilename);
|
---|
1445 | rc = RTFileWriteAt(DescFile, uOffset, psz, cb, NULL);
|
---|
1446 | if (VBOX_FAILURE(rc))
|
---|
1447 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error writing descriptor in '%s'"), pImage->pszFilename);
|
---|
1448 | uOffset += cb;
|
---|
1449 | rc = RTFileWriteAt(DescFile, uOffset, "\n", 1, NULL);
|
---|
1450 | if (VBOX_FAILURE(rc))
|
---|
1451 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error writing descriptor in '%s'"), pImage->pszFilename);
|
---|
1452 | uOffset++;
|
---|
1453 | }
|
---|
1454 | if (cbLimit)
|
---|
1455 | {
|
---|
1456 | /* Inefficient, but simple. */
|
---|
1457 | while (uOffset < cbLimit)
|
---|
1458 | {
|
---|
1459 | rc = RTFileWriteAt(DescFile, uOffset, "", 1, NULL);
|
---|
1460 | if (VBOX_FAILURE(rc))
|
---|
1461 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error writing descriptor in '%s'"), pImage->pszFilename);
|
---|
1462 | uOffset++;
|
---|
1463 | }
|
---|
1464 | }
|
---|
1465 | else
|
---|
1466 | {
|
---|
1467 | rc = RTFileSetSize(DescFile, uOffset);
|
---|
1468 | if (VBOX_FAILURE(rc))
|
---|
1469 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error truncating descriptor in '%s'"), pImage->pszFilename);
|
---|
1470 | }
|
---|
1471 | pImage->Descriptor.fDirty = false;
|
---|
1472 | return rc;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | static int vmdkReadMetaSparseExtent(PVMDKEXTENT pExtent)
|
---|
1476 | {
|
---|
1477 | SparseExtentHeader Header;
|
---|
1478 | uint64_t cbExtentSize, cSectorsPerGDE;
|
---|
1479 |
|
---|
1480 | int rc = RTFileReadAt(pExtent->File, 0, &Header, sizeof(Header), NULL);
|
---|
1481 | AssertRC(rc);
|
---|
1482 | if (VBOX_FAILURE(rc))
|
---|
1483 | {
|
---|
1484 | rc = vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: error reading extent header in '%s'"), pExtent->pszFullname);
|
---|
1485 | goto out;
|
---|
1486 | }
|
---|
1487 | if ( RT_LE2H_U32(Header.magicNumber) != VMDK_SPARSE_MAGICNUMBER
|
---|
1488 | || RT_LE2H_U32(Header.version) != 1)
|
---|
1489 | {
|
---|
1490 | rc = vmdkError(pExtent->pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect magic/version in extent header in '%s'"), pExtent->pszFullname);
|
---|
1491 | goto out;
|
---|
1492 | }
|
---|
1493 | /* The image must be a multiple of a sector in size. If not, it means the
|
---|
1494 | * image is at least truncated, or even seriously garbled. */
|
---|
1495 | rc = RTFileGetSize(pExtent->File, &cbExtentSize);
|
---|
1496 | if (VBOX_FAILURE(rc))
|
---|
1497 | {
|
---|
1498 | rc = vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: error getting size in '%s'"), pExtent->pszFullname);
|
---|
1499 | goto out;
|
---|
1500 | }
|
---|
1501 | if ( (RT_LE2H_U32(Header.flags) & 1)
|
---|
1502 | && ( Header.singleEndLineChar != '\n'
|
---|
1503 | || Header.nonEndLineChar != ' '
|
---|
1504 | || Header.doubleEndLineChar1 != '\r'
|
---|
1505 | || Header.doubleEndLineChar2 != '\n') )
|
---|
1506 | {
|
---|
1507 | rc = vmdkError(pExtent->pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: corrupted by CR/LF translation in '%s'"), pExtent->pszFullname);
|
---|
1508 | goto out;
|
---|
1509 | }
|
---|
1510 | pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
|
---|
1511 | pExtent->cSectors = RT_LE2H_U64(Header.capacity);
|
---|
1512 | pExtent->cSectorsPerGrain = RT_LE2H_U64(Header.grainSize);
|
---|
1513 | /* The spec says that this must be a power of two and greater than 8,
|
---|
1514 | * but probably they meant not less than 8. */
|
---|
1515 | if ( (pExtent->cSectorsPerGrain & (pExtent->cSectorsPerGrain - 1))
|
---|
1516 | || pExtent->cSectorsPerGrain < 8)
|
---|
1517 | {
|
---|
1518 | rc = vmdkError(pExtent->pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: invalid extent grain size %u in '%s'"), pExtent->cSectorsPerGrain, pExtent->pszFullname);
|
---|
1519 | goto out;
|
---|
1520 | }
|
---|
1521 | pExtent->uDescriptorSector = RT_LE2H_U64(Header.descriptorOffset);
|
---|
1522 | pExtent->cDescriptorSectors = RT_LE2H_U64(Header.descriptorSize);
|
---|
1523 | if (pExtent->uDescriptorSector && !pExtent->cDescriptorSectors)
|
---|
1524 | {
|
---|
1525 | rc = vmdkError(pExtent->pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: inconsistent embedded descriptor config in '%s'"), pExtent->pszFullname);
|
---|
1526 | goto out;
|
---|
1527 | }
|
---|
1528 | pExtent->cGTEntries = RT_LE2H_U32(Header.numGTEsPerGT);
|
---|
1529 | /* This code requires that a grain table must hold a power of two multiple
|
---|
1530 | * of the number of entries per GT cache entry. */
|
---|
1531 | if ( (pExtent->cGTEntries & (pExtent->cGTEntries - 1))
|
---|
1532 | || pExtent->cGTEntries < VMDK_GT_CACHELINE_SIZE)
|
---|
1533 | {
|
---|
1534 | rc = vmdkError(pExtent->pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: grain table cache size problem in '%s'"), pExtent->pszFullname);
|
---|
1535 | goto out;
|
---|
1536 | }
|
---|
1537 | if (RT_LE2H_U32(Header.flags) & 2)
|
---|
1538 | {
|
---|
1539 | pExtent->uSectorRGD = RT_LE2H_U64(Header.rgdOffset);
|
---|
1540 | pExtent->uSectorGD = RT_LE2H_U64(Header.gdOffset);
|
---|
1541 | }
|
---|
1542 | else
|
---|
1543 | {
|
---|
1544 | /** @todo this is just guesswork, the spec doesn't document this
|
---|
1545 | * properly and I don't have a vmdk without RGD. */
|
---|
1546 | pExtent->uSectorGD = RT_LE2H_U64(Header.rgdOffset);
|
---|
1547 | pExtent->uSectorRGD = 0;
|
---|
1548 | }
|
---|
1549 | pExtent->cOverheadSectors = RT_LE2H_U64(Header.overHead);
|
---|
1550 | pExtent->fUncleanShutdown = !!Header.uncleanShutdown;
|
---|
1551 | cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
|
---|
1552 | if (!cSectorsPerGDE || cSectorsPerGDE > UINT32_MAX)
|
---|
1553 | {
|
---|
1554 | rc = vmdkError(pExtent->pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect grain directory size in '%s'"), pExtent->pszFullname);
|
---|
1555 | goto out;
|
---|
1556 | }
|
---|
1557 | pExtent->cSectorsPerGDE = cSectorsPerGDE;
|
---|
1558 | pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
|
---|
1559 |
|
---|
1560 | rc = vmdkReadGrainDirectory(pExtent);
|
---|
1561 |
|
---|
1562 | out:
|
---|
1563 | if (VBOX_FAILURE(rc))
|
---|
1564 | vmdkFreeExtentData(pExtent, false);
|
---|
1565 |
|
---|
1566 | return rc;
|
---|
1567 | }
|
---|
1568 |
|
---|
1569 | static int vmdkWriteMetaSparseExtent(PVMDKEXTENT pExtent)
|
---|
1570 | {
|
---|
1571 | SparseExtentHeader Header;
|
---|
1572 |
|
---|
1573 | memset(&Header, '\0', sizeof(Header));
|
---|
1574 | Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
|
---|
1575 | Header.version = RT_H2LE_U32(1);
|
---|
1576 | Header.flags = RT_H2LE_U32(1 | ((pExtent->pRGD) ? 2 : 0));
|
---|
1577 | Header.capacity = RT_H2LE_U64(pExtent->cSectors);
|
---|
1578 | Header.grainSize = RT_H2LE_U64(pExtent->cSectorsPerGrain);
|
---|
1579 | Header.descriptorOffset = RT_H2LE_U64(pExtent->uDescriptorSector);
|
---|
1580 | Header.descriptorSize = RT_H2LE_U64(pExtent->cDescriptorSectors);
|
---|
1581 | Header.numGTEsPerGT = RT_H2LE_U32(pExtent->cGTEntries);
|
---|
1582 | if (pExtent->pRGD)
|
---|
1583 | {
|
---|
1584 | Header.rgdOffset = RT_H2LE_U64(pExtent->uSectorRGD);
|
---|
1585 | Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
|
---|
1586 | }
|
---|
1587 | else
|
---|
1588 | {
|
---|
1589 | /** @todo this is just guesswork, the spec doesn't document this
|
---|
1590 | * properly and I don't have a vmdk without RGD. */
|
---|
1591 | Header.rgdOffset = RT_H2LE_U64(pExtent->uSectorGD);
|
---|
1592 | }
|
---|
1593 | Header.overHead = RT_H2LE_U64(pExtent->cOverheadSectors);
|
---|
1594 | Header.uncleanShutdown = pExtent->fUncleanShutdown;
|
---|
1595 | Header.singleEndLineChar = '\n';
|
---|
1596 | Header.nonEndLineChar = ' ';
|
---|
1597 | Header.doubleEndLineChar1 = '\r';
|
---|
1598 | Header.doubleEndLineChar2 = '\n';
|
---|
1599 |
|
---|
1600 | int rc = RTFileWriteAt(pExtent->File, 0, &Header, sizeof(Header), NULL);
|
---|
1601 | AssertRC(rc);
|
---|
1602 | if (VBOX_FAILURE(rc))
|
---|
1603 | rc = vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: error writing extent header in '%s'"), pExtent->pszFullname);
|
---|
1604 | return rc;
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
1608 | static int vmdkReadMetaESXSparseExtent(PVMDKEXTENT pExtent)
|
---|
1609 | {
|
---|
1610 | COWDisk_Header Header;
|
---|
1611 | uint64_t cSectorsPerGDE;
|
---|
1612 |
|
---|
1613 | int rc = RTFileReadAt(pExtent->File, 0, &Header, sizeof(Header), NULL);
|
---|
1614 | AssertRC(rc);
|
---|
1615 | if (VBOX_FAILURE(rc))
|
---|
1616 | goto out;
|
---|
1617 | if ( RT_LE2H_U32(Header.magicNumber) != VMDK_ESX_SPARSE_MAGICNUMBER
|
---|
1618 | || RT_LE2H_U32(Header.version) != 1
|
---|
1619 | || RT_LE2H_U32(Header.flags) != 3)
|
---|
1620 | {
|
---|
1621 | rc = VERR_VDI_INVALID_HEADER;
|
---|
1622 | goto out;
|
---|
1623 | }
|
---|
1624 | pExtent->enmType = VMDKETYPE_ESX_SPARSE;
|
---|
1625 | pExtent->cSectors = RT_LE2H_U32(Header.numSectors);
|
---|
1626 | pExtent->cSectorsPerGrain = RT_LE2H_U32(Header.grainSize);
|
---|
1627 | /* The spec says that this must be between 1 sector and 1MB. This code
|
---|
1628 | * assumes it's a power of two, so check that requirement, too. */
|
---|
1629 | if ( (pExtent->cSectorsPerGrain & (pExtent->cSectorsPerGrain - 1))
|
---|
1630 | || pExtent->cSectorsPerGrain == 0
|
---|
1631 | || pExtent->cSectorsPerGrain > 2048)
|
---|
1632 | {
|
---|
1633 | rc = VERR_VDI_INVALID_HEADER;
|
---|
1634 | goto out;
|
---|
1635 | }
|
---|
1636 | pExtent->uDescriptorSector = 0;
|
---|
1637 | pExtent->cDescriptorSectors = 0;
|
---|
1638 | pExtent->uSectorGD = RT_LE2H_U32(Header.gdOffset);
|
---|
1639 | pExtent->uSectorRGD = 0;
|
---|
1640 | pExtent->cOverheadSectors = 0;
|
---|
1641 | pExtent->cGTEntries = 4096;
|
---|
1642 | cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
|
---|
1643 | if (!cSectorsPerGDE || cSectorsPerGDE > UINT32_MAX)
|
---|
1644 | {
|
---|
1645 | rc = VERR_VDI_INVALID_HEADER;
|
---|
1646 | goto out;
|
---|
1647 | }
|
---|
1648 | pExtent->cSectorsPerGDE = cSectorsPerGDE;
|
---|
1649 | pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
|
---|
1650 | if (pExtent->cGDEntries != RT_LE2H_U32(Header.numGDEntries))
|
---|
1651 | {
|
---|
1652 | /* Inconsistency detected. Computed number of GD entries doesn't match
|
---|
1653 | * stored value. Better be safe than sorry. */
|
---|
1654 | rc = VERR_VDI_INVALID_HEADER;
|
---|
1655 | goto out;
|
---|
1656 | }
|
---|
1657 | pExtent->uFreeSector = RT_LE2H_U32(Header.freeSector);
|
---|
1658 | pExtent->fUncleanShutdown = !!Header.uncleanShutdown;
|
---|
1659 |
|
---|
1660 | rc = vmdkReadGrainDirectory(pExtent);
|
---|
1661 |
|
---|
1662 | out:
|
---|
1663 | if (VBOX_FAILURE(rc))
|
---|
1664 | vmdkFreeExtentData(pExtent, false);
|
---|
1665 |
|
---|
1666 | return rc;
|
---|
1667 | }
|
---|
1668 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
1669 |
|
---|
1670 | static void vmdkFreeExtentData(PVMDKEXTENT pExtent, bool fDelete)
|
---|
1671 | {
|
---|
1672 | vmdkFreeGrainDirectory(pExtent);
|
---|
1673 | if (pExtent->pDescData)
|
---|
1674 | {
|
---|
1675 | RTMemFree(pExtent->pDescData);
|
---|
1676 | pExtent->pDescData = NULL;
|
---|
1677 | }
|
---|
1678 | if (pExtent->File != NIL_RTFILE)
|
---|
1679 | {
|
---|
1680 | RTFileClose(pExtent->File);
|
---|
1681 | pExtent->File = NIL_RTFILE;
|
---|
1682 | if ( fDelete
|
---|
1683 | && strcmp(pExtent->pszFullname, pExtent->pszBasename) != 0
|
---|
1684 | && pExtent->pszFullname)
|
---|
1685 | RTFileDelete(pExtent->pszFullname);
|
---|
1686 | }
|
---|
1687 | if (pExtent->pszBasename)
|
---|
1688 | {
|
---|
1689 | RTMemTmpFree((void *)pExtent->pszBasename);
|
---|
1690 | pExtent->pszBasename = NULL;
|
---|
1691 | }
|
---|
1692 | if (pExtent->pszFullname)
|
---|
1693 | {
|
---|
1694 | RTStrFree((char *)(void *)pExtent->pszFullname);
|
---|
1695 | pExtent->pszFullname = NULL;
|
---|
1696 | }
|
---|
1697 | }
|
---|
1698 |
|
---|
1699 | static int vmdkAllocateGrainTableCache(PVMDKIMAGE pImage)
|
---|
1700 | {
|
---|
1701 | PVMDKEXTENT pExtent;
|
---|
1702 |
|
---|
1703 | /* Allocate grain table cache if any sparse extent is present. */
|
---|
1704 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
1705 | {
|
---|
1706 | pExtent = &pImage->pExtents[i];
|
---|
1707 | if ( pExtent->enmType == VMDKETYPE_HOSTED_SPARSE
|
---|
1708 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
1709 | || pExtent->enmType == VMDKETYPE_ESX_SPARSE
|
---|
1710 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
1711 | )
|
---|
1712 | {
|
---|
1713 | /* Allocate grain table cache. */
|
---|
1714 | pImage->pGTCache = (PVMDKGTCACHE)RTMemAllocZ(sizeof(VMDKGTCACHE));
|
---|
1715 | if (!pImage->pGTCache)
|
---|
1716 | return VERR_NO_MEMORY;
|
---|
1717 | for (unsigned i = 0; i < VMDK_GT_CACHE_SIZE; i++)
|
---|
1718 | {
|
---|
1719 | PVMDKGTCACHEENTRY pGCE = &pImage->pGTCache->aGTCache[i];
|
---|
1720 | pGCE->uExtent = UINT32_MAX;
|
---|
1721 | }
|
---|
1722 | pImage->pGTCache->cEntries = VMDK_GT_CACHE_SIZE;
|
---|
1723 | break;
|
---|
1724 | }
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | return VINF_SUCCESS;
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents)
|
---|
1731 | {
|
---|
1732 | int rc = VINF_SUCCESS;
|
---|
1733 | PVMDKEXTENT pExtents = (PVMDKEXTENT)RTMemAllocZ(cExtents * sizeof(VMDKEXTENT));
|
---|
1734 | if (pImage)
|
---|
1735 | {
|
---|
1736 | for (unsigned i = 0; i < cExtents; i++)
|
---|
1737 | {
|
---|
1738 | pExtents[i].File = NIL_RTFILE;
|
---|
1739 | pExtents[i].pszBasename = NULL;
|
---|
1740 | pExtents[i].pszFullname = NULL;
|
---|
1741 | pExtents[i].pGD = NULL;
|
---|
1742 | pExtents[i].pRGD = NULL;
|
---|
1743 | pExtents[i].pDescData = NULL;
|
---|
1744 | pExtents[i].uExtent = i;
|
---|
1745 | pExtents[i].pImage = pImage;
|
---|
1746 | }
|
---|
1747 | pImage->pExtents = pExtents;
|
---|
1748 | pImage->cExtents = cExtents;
|
---|
1749 | }
|
---|
1750 | else
|
---|
1751 | rc = VERR_NO_MEMORY;
|
---|
1752 |
|
---|
1753 | return rc;
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 | static int vmdkOpenImage(PVMDKIMAGE pImage, const char *pszFilename, unsigned uOpenFlags)
|
---|
1757 | {
|
---|
1758 | int rc = VINF_SUCCESS;
|
---|
1759 | uint32_t u32Magic;
|
---|
1760 | RTFILE File;
|
---|
1761 | PVMDKEXTENT pExtent;
|
---|
1762 |
|
---|
1763 | pImage->uOpenFlags = uOpenFlags;
|
---|
1764 |
|
---|
1765 | /** @todo check whether the same file is used somewhere else. don't open any file twice, leads to locking problems and can cause trouble with file caching. */
|
---|
1766 |
|
---|
1767 | /*
|
---|
1768 | * Open the image.
|
---|
1769 | */
|
---|
1770 | rc = RTFileOpen(&File, pszFilename, uOpenFlags & VD_OPEN_FLAGS_READONLY
|
---|
1771 | ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
|
---|
1772 | : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
1773 | if (VBOX_FAILURE(rc))
|
---|
1774 | {
|
---|
1775 | /* Do NOT signal an appropriate error here, as the VD layer has the
|
---|
1776 | * choice of retrying the open if it failed. */
|
---|
1777 | goto out;
|
---|
1778 | }
|
---|
1779 | pImage->File = File;
|
---|
1780 | rc = RTFileReadAt(File, 0, &u32Magic, sizeof(u32Magic), NULL);
|
---|
1781 | AssertRC(rc);
|
---|
1782 | if (VBOX_FAILURE(rc))
|
---|
1783 | {
|
---|
1784 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error reading the magic number in '%s'"), pszFilename);
|
---|
1785 | goto out;
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | /* Handle the file according to its magic number. */
|
---|
1789 | if (RT_LE2H_U32(u32Magic) == VMDK_SPARSE_MAGICNUMBER)
|
---|
1790 | {
|
---|
1791 | /* It's a hosted sparse single-extent image. */
|
---|
1792 | rc = vmdkCreateExtents(pImage, 1);
|
---|
1793 | if (VBOX_FAILURE(rc))
|
---|
1794 | goto out;
|
---|
1795 | /* The opened file is passed to the extent. No separate descriptor
|
---|
1796 | * file, so no need to keep anything open for the image. */
|
---|
1797 | pExtent = &pImage->pExtents[0];
|
---|
1798 | pExtent->File = File;
|
---|
1799 | pImage->File = NIL_RTFILE;
|
---|
1800 | rc = vmdkReadMetaSparseExtent(pExtent);
|
---|
1801 | if (VBOX_FAILURE(rc))
|
---|
1802 | goto out;
|
---|
1803 | /* As we're dealing with a monolithic sparse image here, there must
|
---|
1804 | * be a descriptor embedded in the image file. */
|
---|
1805 | if (!pExtent->uDescriptorSector || !pExtent->cDescriptorSectors)
|
---|
1806 | {
|
---|
1807 | rc = vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: monolithic image without descriptor in '%s'"), pszFilename);
|
---|
1808 | goto out;
|
---|
1809 | }
|
---|
1810 | /* Read the descriptor from the extent. */
|
---|
1811 | pExtent->pDescData = (char *)RTMemAllocZ(VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
|
---|
1812 | if (!pExtent->pDescData)
|
---|
1813 | {
|
---|
1814 | rc = VERR_NO_MEMORY;
|
---|
1815 | goto out;
|
---|
1816 | }
|
---|
1817 | rc = RTFileReadAt(pExtent->File,
|
---|
1818 | VMDK_SECTOR2BYTE(pExtent->uDescriptorSector),
|
---|
1819 | pExtent->pDescData,
|
---|
1820 | VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors), NULL);
|
---|
1821 | AssertRC(rc);
|
---|
1822 | if (VBOX_FAILURE(rc))
|
---|
1823 | {
|
---|
1824 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pExtent->pszFullname);
|
---|
1825 | goto out;
|
---|
1826 | }
|
---|
1827 |
|
---|
1828 | rc = vmdkParseDescriptor(pImage, pExtent->pDescData,
|
---|
1829 | VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
|
---|
1830 | if (VBOX_FAILURE(rc))
|
---|
1831 | goto out;
|
---|
1832 |
|
---|
1833 | /* Mark the extent as unclean if opened in read-write mode. */
|
---|
1834 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1835 | {
|
---|
1836 | pExtent->fUncleanShutdown = true;
|
---|
1837 | pExtent->fMetaDirty = true;
|
---|
1838 | }
|
---|
1839 | }
|
---|
1840 | else
|
---|
1841 | {
|
---|
1842 | pImage->cbDescAlloc = VMDK_SECTOR2BYTE(20);
|
---|
1843 | pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
|
---|
1844 | if (!pImage->pDescData)
|
---|
1845 | {
|
---|
1846 | rc = VERR_NO_MEMORY;
|
---|
1847 | goto out;
|
---|
1848 | }
|
---|
1849 |
|
---|
1850 | /*size_t*/unsigned cbRead;
|
---|
1851 | rc = RTFileReadAt(pImage->File, 0, pImage->pDescData,
|
---|
1852 | pImage->cbDescAlloc, &cbRead);
|
---|
1853 | if (VBOX_FAILURE(rc))
|
---|
1854 | {
|
---|
1855 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pszFilename);
|
---|
1856 | goto out;
|
---|
1857 | }
|
---|
1858 | if (cbRead == pImage->cbDescAlloc)
|
---|
1859 | {
|
---|
1860 | /* Likely the read is truncated. Better fail a bit too early
|
---|
1861 | * (normally the descriptor is much smaller than our buffer). */
|
---|
1862 | rc = vmdkError(pImage, VERR_VDI_INVALID_HEADER, RT_SRC_POS, N_("VMDK: cannot read descriptor in '%s'"), pszFilename);
|
---|
1863 | goto out;
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | rc = vmdkParseDescriptor(pImage, pImage->pDescData, pImage->cbDescAlloc);
|
---|
1867 | if (VBOX_FAILURE(rc))
|
---|
1868 | goto out;
|
---|
1869 |
|
---|
1870 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
1871 | {
|
---|
1872 | PVMDKEXTENT pExtent = &pImage->pExtents[i];
|
---|
1873 |
|
---|
1874 | if (pExtent->pszBasename)
|
---|
1875 | {
|
---|
1876 | /* Hack to figure out whether the specified name in the
|
---|
1877 | * extent descriptor is absolute. Doesn't always work, but
|
---|
1878 | * should be good enough for now. */
|
---|
1879 | char *pszFullname;
|
---|
1880 | /** @todo implement proper path absolute check. */
|
---|
1881 | if (pExtent->pszBasename[0] == RTPATH_SLASH)
|
---|
1882 | {
|
---|
1883 | pszFullname = RTStrDup(pExtent->pszBasename);
|
---|
1884 | if (!pszFullname)
|
---|
1885 | {
|
---|
1886 | rc = VERR_NO_MEMORY;
|
---|
1887 | goto out;
|
---|
1888 | }
|
---|
1889 | }
|
---|
1890 | else
|
---|
1891 | {
|
---|
1892 | size_t cbDirname;
|
---|
1893 | char *pszDirname = RTStrDup(pImage->pszFilename);
|
---|
1894 | if (!pszDirname)
|
---|
1895 | {
|
---|
1896 | rc = VERR_NO_MEMORY;
|
---|
1897 | goto out;
|
---|
1898 | }
|
---|
1899 | RTPathStripFilename(pszDirname);
|
---|
1900 | cbDirname = strlen(pszDirname);
|
---|
1901 | rc = RTStrAPrintf(&pszFullname, "%s%c%s", pszDirname,
|
---|
1902 | RTPATH_SLASH, pExtent->pszBasename);
|
---|
1903 | RTStrFree(pszDirname);
|
---|
1904 | if (VBOX_FAILURE(rc))
|
---|
1905 | goto out;
|
---|
1906 | }
|
---|
1907 | pExtent->pszFullname = pszFullname;
|
---|
1908 | }
|
---|
1909 | else
|
---|
1910 | pExtent->pszFullname = NULL;
|
---|
1911 |
|
---|
1912 | switch (pExtent->enmType)
|
---|
1913 | {
|
---|
1914 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
1915 | rc = RTFileOpen(&pExtent->File, pExtent->pszFullname,
|
---|
1916 | uOpenFlags & VD_OPEN_FLAGS_READONLY
|
---|
1917 | ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
|
---|
1918 | : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
1919 | if (VBOX_FAILURE(rc))
|
---|
1920 | {
|
---|
1921 | /* Do NOT signal an appropriate error here, as the VD
|
---|
1922 | * layer has the choice of retrying the open if it
|
---|
1923 | * failed. */
|
---|
1924 | goto out;
|
---|
1925 | }
|
---|
1926 | rc = vmdkReadMetaSparseExtent(pExtent);
|
---|
1927 | if (VBOX_FAILURE(rc))
|
---|
1928 | goto out;
|
---|
1929 |
|
---|
1930 | /* Mark the extent as unclean if opened in read-write mode. */
|
---|
1931 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1932 | {
|
---|
1933 | pExtent->fUncleanShutdown = true;
|
---|
1934 | pExtent->fMetaDirty = true;
|
---|
1935 | }
|
---|
1936 | break;
|
---|
1937 | case VMDKETYPE_FLAT:
|
---|
1938 | rc = RTFileOpen(&pExtent->File, pExtent->pszFullname,
|
---|
1939 | uOpenFlags & VD_OPEN_FLAGS_READONLY
|
---|
1940 | ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
|
---|
1941 | : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
1942 | if (VBOX_FAILURE(rc))
|
---|
1943 | {
|
---|
1944 | /* Do NOT signal an appropriate error here, as the VD
|
---|
1945 | * layer has the choice of retrying the open if it
|
---|
1946 | * failed. */
|
---|
1947 | goto out;
|
---|
1948 | }
|
---|
1949 | break;
|
---|
1950 | case VMDKETYPE_ZERO:
|
---|
1951 | /* Nothing to do. */
|
---|
1952 | break;
|
---|
1953 | default:
|
---|
1954 | AssertMsgFailed(("unknown vmdk extent type %d\n", pExtent->enmType));
|
---|
1955 | }
|
---|
1956 | }
|
---|
1957 | }
|
---|
1958 |
|
---|
1959 | /* Make sure this is not reached accidentally with an error status. */
|
---|
1960 | AssertRC(rc);
|
---|
1961 |
|
---|
1962 | /* Update the image metadata now in case has changed. */
|
---|
1963 | rc = vmdkFlushImage(pImage);
|
---|
1964 | if (VBOX_FAILURE(rc))
|
---|
1965 | goto out;
|
---|
1966 |
|
---|
1967 | /* Figure out a few per-image constants from the extents. */
|
---|
1968 | pImage->cbSize = 0;
|
---|
1969 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
1970 | {
|
---|
1971 | pExtent = &pImage->pExtents[i];
|
---|
1972 | if ( pExtent->enmType == VMDKETYPE_HOSTED_SPARSE
|
---|
1973 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
1974 | || pExtent->enmType == VMDKETYPE_ESX_SPARSE
|
---|
1975 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
1976 | )
|
---|
1977 | {
|
---|
1978 | /* Here used to be a check whether the nominal size of an extent
|
---|
1979 | * is a multiple of the grain size. The spec says that this is
|
---|
1980 | * always the case, but unfortunately some files out there in the
|
---|
1981 | * wild violate the spec (e.g. ReactOS 0.3.1). */
|
---|
1982 | }
|
---|
1983 | pImage->cbSize += VMDK_SECTOR2BYTE(pExtent->cNominalSectors);
|
---|
1984 | }
|
---|
1985 |
|
---|
1986 | pImage->enmImageType = VD_IMAGE_TYPE_NORMAL;
|
---|
1987 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
1988 | {
|
---|
1989 | pExtent = &pImage->pExtents[i];
|
---|
1990 | if ( pImage->pExtents[i].enmType == VMDKETYPE_FLAT
|
---|
1991 | || pImage->pExtents[i].enmType == VMDKETYPE_ZERO)
|
---|
1992 | {
|
---|
1993 | pImage->enmImageType = VD_IMAGE_TYPE_FIXED;
|
---|
1994 | break;
|
---|
1995 | }
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | rc = vmdkAllocateGrainTableCache(pImage);
|
---|
1999 | if (VBOX_FAILURE(rc))
|
---|
2000 | goto out;
|
---|
2001 |
|
---|
2002 | out:
|
---|
2003 | if (VBOX_FAILURE(rc))
|
---|
2004 | vmdkFreeImage(pImage, false);
|
---|
2005 | return rc;
|
---|
2006 | }
|
---|
2007 |
|
---|
2008 | static int vmdkCreateImage(PVMDKIMAGE pImage, const char *pszFilename, VDIMAGETYPE enmType, uint64_t cbSize, unsigned uImageFlags, const char *pszComment, uint32_t cCylinders, uint32_t cHeads, uint32_t cSectors)
|
---|
2009 | {
|
---|
2010 | int rc;
|
---|
2011 | uint64_t cSectorsPerGDE, cSectorsPerGD;
|
---|
2012 | PVMDKEXTENT pExtent;
|
---|
2013 |
|
---|
2014 | pImage->uImageFlags = uImageFlags;
|
---|
2015 | rc = vmdkCreateDescriptor(pImage, pImage->pDescData, pImage->cbDescAlloc, &pImage->Descriptor);
|
---|
2016 | if (VBOX_FAILURE(rc))
|
---|
2017 | {
|
---|
2018 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not create new descriptor in '%s'"), pszFilename);
|
---|
2019 | goto out;
|
---|
2020 | }
|
---|
2021 |
|
---|
2022 | if ( enmType == VD_IMAGE_TYPE_FIXED
|
---|
2023 | || (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
|
---|
2024 | || (uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK))
|
---|
2025 | {
|
---|
2026 | /* Fixed images and split images in general have a separate descriptor
|
---|
2027 | * file. This is the more complicated case, as it requires setting up
|
---|
2028 | * potentially more than one extent, including filename generation. */
|
---|
2029 |
|
---|
2030 | if ( enmType == VD_IMAGE_TYPE_FIXED
|
---|
2031 | && (uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK))
|
---|
2032 | {
|
---|
2033 | PVBOXHDDRAW pRaw = (PVBOXHDDRAW)(void *)pszComment;
|
---|
2034 | /* As the comment is misused, zap it so that no garbage comment
|
---|
2035 | * is set below. */
|
---|
2036 | pszComment = NULL;
|
---|
2037 | if (pRaw->fRawDisk)
|
---|
2038 | {
|
---|
2039 | /* Full raw disk access. This requires setting up a descriptor
|
---|
2040 | * file and open the (flat) raw disk. */
|
---|
2041 | rc = vmdkCreateExtents(pImage, 1);
|
---|
2042 | if (VBOX_FAILURE(rc))
|
---|
2043 | {
|
---|
2044 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pszFilename);
|
---|
2045 | goto out;
|
---|
2046 | }
|
---|
2047 | pExtent = &pImage->pExtents[0];
|
---|
2048 | rc = RTFileOpen(&pImage->File, pszFilename,
|
---|
2049 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
|
---|
2050 | if (VBOX_FAILURE(rc))
|
---|
2051 | {
|
---|
2052 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pszFilename);
|
---|
2053 | goto out;
|
---|
2054 | }
|
---|
2055 |
|
---|
2056 | /* Set up basename for extent description. Cannot use StrDup. */
|
---|
2057 | size_t cbBasename = strlen(pRaw->pszRawDisk) + 1;
|
---|
2058 | char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
|
---|
2059 | if (!pszBasename)
|
---|
2060 | {
|
---|
2061 | rc = VERR_NO_MEMORY;
|
---|
2062 | goto out;
|
---|
2063 | }
|
---|
2064 | memcpy(pszBasename, pRaw->pszRawDisk, cbBasename);
|
---|
2065 | pExtent->pszBasename = pszBasename;
|
---|
2066 | /* For raw disks the full name is identical to the base name. */
|
---|
2067 | pExtent->pszFullname = RTStrDup(pszBasename);
|
---|
2068 | if (!pExtent->pszFullname)
|
---|
2069 | {
|
---|
2070 | rc = VERR_NO_MEMORY;
|
---|
2071 | goto out;
|
---|
2072 | }
|
---|
2073 | pExtent->enmType = VMDKETYPE_FLAT;
|
---|
2074 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
|
---|
2075 | pExtent->uSectorOffset = 0;
|
---|
2076 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
2077 | pExtent->fMetaDirty = false;
|
---|
2078 |
|
---|
2079 | pImage->enmImageType = enmType;
|
---|
2080 | rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType", "fullDevice");
|
---|
2081 | if (VBOX_FAILURE(rc))
|
---|
2082 | {
|
---|
2083 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pszFilename);
|
---|
2084 | goto out;
|
---|
2085 | }
|
---|
2086 |
|
---|
2087 | /* Open flat image, the raw disk. */
|
---|
2088 | rc = RTFileOpen(&pExtent->File, pExtent->pszFullname,
|
---|
2089 | RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
2090 | if (VBOX_FAILURE(rc))
|
---|
2091 | {
|
---|
2092 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not open raw disk file '%s'"), pExtent->pszFullname);
|
---|
2093 | goto out;
|
---|
2094 | }
|
---|
2095 | }
|
---|
2096 | else
|
---|
2097 | {
|
---|
2098 | /* Raw partition access. This requires setting up a descriptor
|
---|
2099 | * file, write the partition information to a flat extent and
|
---|
2100 | * open all the (flat) raw disk partitions. */
|
---|
2101 |
|
---|
2102 | /* First pass over the partitions to determine how many
|
---|
2103 | * extents we need. One partition can require up to 4 extents.
|
---|
2104 | * One to skip over unpartitioned space, one for the
|
---|
2105 | * partitioning data, one to skip over unpartitioned space
|
---|
2106 | * and one for the partition data. */
|
---|
2107 | unsigned cExtents = 0;
|
---|
2108 | uint64_t uStart = 0;
|
---|
2109 | for (unsigned i = 0; i < pRaw->cPartitions; i++)
|
---|
2110 | {
|
---|
2111 | PVBOXHDDRAWPART pPart = &pRaw->pPartitions[i];
|
---|
2112 | if (pPart->cbPartitionData)
|
---|
2113 | {
|
---|
2114 | if (uStart > pPart->uPartitionDataStart)
|
---|
2115 | {
|
---|
2116 | rc = vmdkError(pImage, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("VMDK: cannot go backwards for partitioning information in '%s'"), pszFilename);
|
---|
2117 | goto out;
|
---|
2118 | } else if (uStart != pPart->uPartitionDataStart)
|
---|
2119 | cExtents++;
|
---|
2120 | uStart = pPart->uPartitionDataStart + pPart->cbPartitionData;
|
---|
2121 | cExtents++;
|
---|
2122 | }
|
---|
2123 | if (pPart->cbPartition)
|
---|
2124 | {
|
---|
2125 | if (uStart > pPart->uPartitionStart)
|
---|
2126 | {
|
---|
2127 | rc = vmdkError(pImage, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("VMDK: cannot go backwards for partition data in '%s'"), pszFilename);
|
---|
2128 | goto out;
|
---|
2129 | } else if (uStart != pPart->uPartitionStart)
|
---|
2130 | cExtents++;
|
---|
2131 | uStart = pPart->uPartitionStart + pPart->cbPartition;
|
---|
2132 | cExtents++;
|
---|
2133 | }
|
---|
2134 | }
|
---|
2135 | /* Another extent for filling up the rest of the image. */
|
---|
2136 | if (uStart != cbSize)
|
---|
2137 | cExtents++;
|
---|
2138 |
|
---|
2139 | rc = vmdkCreateExtents(pImage, cExtents);
|
---|
2140 | if (VBOX_FAILURE(rc))
|
---|
2141 | {
|
---|
2142 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pszFilename);
|
---|
2143 | goto out;
|
---|
2144 | }
|
---|
2145 |
|
---|
2146 | rc = RTFileOpen(&pImage->File, pszFilename,
|
---|
2147 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
|
---|
2148 | if (VBOX_FAILURE(rc))
|
---|
2149 | {
|
---|
2150 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pszFilename);
|
---|
2151 | goto out;
|
---|
2152 | }
|
---|
2153 |
|
---|
2154 | /* Create base filename for the partition table extent. */
|
---|
2155 | /** @todo remove fixed buffer. */
|
---|
2156 | char pszPartition[1024];
|
---|
2157 | const char *pszBase = RTPathFilename(pszFilename);
|
---|
2158 | const char *pszExt = RTPathExt(pszBase);
|
---|
2159 | if (pszExt == NULL)
|
---|
2160 | {
|
---|
2161 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: invalid filename '%s'"), pszFilename);
|
---|
2162 | goto out;
|
---|
2163 | }
|
---|
2164 | memcpy(pszPartition, pszBase, pszExt - pszBase);
|
---|
2165 | memcpy(pszPartition + (pszExt - pszBase), "-pt", 3);
|
---|
2166 | memcpy(pszPartition + (pszExt - pszBase) + 3, pszExt, strlen(pszExt) + 1);
|
---|
2167 |
|
---|
2168 | /* Second pass over the partitions, now define all extents. */
|
---|
2169 | uint64_t uPartOffset = 0;
|
---|
2170 | cExtents = 0;
|
---|
2171 | uStart = 0;
|
---|
2172 | for (unsigned i = 0; i < pRaw->cPartitions; i++)
|
---|
2173 | {
|
---|
2174 | PVBOXHDDRAWPART pPart = &pRaw->pPartitions[i];
|
---|
2175 | if (pPart->cbPartitionData)
|
---|
2176 | {
|
---|
2177 | if (uStart != pPart->uPartitionDataStart)
|
---|
2178 | {
|
---|
2179 | pExtent = &pImage->pExtents[cExtents++];
|
---|
2180 | pExtent->pszBasename = NULL;
|
---|
2181 | pExtent->pszFullname = NULL;
|
---|
2182 | pExtent->enmType = VMDKETYPE_ZERO;
|
---|
2183 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->uPartitionDataStart - uStart);
|
---|
2184 | pExtent->uSectorOffset = 0;
|
---|
2185 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
2186 | pExtent->fMetaDirty = false;
|
---|
2187 | }
|
---|
2188 | uStart = pPart->uPartitionDataStart + pPart->cbPartitionData;
|
---|
2189 | pExtent = &pImage->pExtents[cExtents++];
|
---|
2190 | /* Set up basename for extent description. Cannot use StrDup. */
|
---|
2191 | size_t cbBasename = strlen(pszPartition) + 1;
|
---|
2192 | char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
|
---|
2193 | if (!pszBasename)
|
---|
2194 | {
|
---|
2195 | rc = VERR_NO_MEMORY;
|
---|
2196 | goto out;
|
---|
2197 | }
|
---|
2198 | memcpy(pszBasename, pszPartition, cbBasename);
|
---|
2199 | pExtent->pszBasename = pszBasename;
|
---|
2200 |
|
---|
2201 | /* Set up full name for partition extent. */
|
---|
2202 | size_t cbDirname;
|
---|
2203 | char *pszDirname = RTStrDup(pImage->pszFilename);
|
---|
2204 | if (!pszDirname)
|
---|
2205 | {
|
---|
2206 | rc = VERR_NO_MEMORY;
|
---|
2207 | goto out;
|
---|
2208 | }
|
---|
2209 | RTPathStripFilename(pszDirname);
|
---|
2210 | cbDirname = strlen(pszDirname);
|
---|
2211 | char *pszFullname;
|
---|
2212 | rc = RTStrAPrintf(&pszFullname, "%s%c%s", pszDirname,
|
---|
2213 | RTPATH_SLASH, pExtent->pszBasename);
|
---|
2214 | RTStrFree(pszDirname);
|
---|
2215 | if (VBOX_FAILURE(rc))
|
---|
2216 | goto out;
|
---|
2217 | pExtent->pszFullname = pszFullname;
|
---|
2218 | pExtent->enmType = VMDKETYPE_FLAT;
|
---|
2219 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbPartitionData);
|
---|
2220 | pExtent->uSectorOffset = uPartOffset;
|
---|
2221 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
2222 | pExtent->fMetaDirty = false;
|
---|
2223 |
|
---|
2224 | rc = RTFileOpen(&pExtent->File, pExtent->pszFullname,
|
---|
2225 | RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE);
|
---|
2226 | if (VBOX_FAILURE(rc))
|
---|
2227 | {
|
---|
2228 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not create new partition data file '%s'"), pExtent->pszFullname);
|
---|
2229 | goto out;
|
---|
2230 | }
|
---|
2231 | rc = RTFileWriteAt(pExtent->File, VMDK_SECTOR2BYTE(uPartOffset), pPart->pvPartitionData, pPart->cbPartitionData, NULL);
|
---|
2232 | if (VBOX_FAILURE(rc))
|
---|
2233 | {
|
---|
2234 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not write partition data to '%s'"), pExtent->pszFullname);
|
---|
2235 | goto out;
|
---|
2236 | }
|
---|
2237 | uPartOffset += VMDK_BYTE2SECTOR(pPart->cbPartitionData);
|
---|
2238 | }
|
---|
2239 | if (pPart->cbPartition)
|
---|
2240 | {
|
---|
2241 | if (uStart != pPart->uPartitionStart)
|
---|
2242 | {
|
---|
2243 | pExtent = &pImage->pExtents[cExtents++];
|
---|
2244 | pExtent->pszBasename = NULL;
|
---|
2245 | pExtent->pszFullname = NULL;
|
---|
2246 | pExtent->enmType = VMDKETYPE_ZERO;
|
---|
2247 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->uPartitionStart - uStart);
|
---|
2248 | pExtent->uSectorOffset = 0;
|
---|
2249 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
2250 | pExtent->fMetaDirty = false;
|
---|
2251 | }
|
---|
2252 | uStart = pPart->uPartitionStart + pPart->cbPartition;
|
---|
2253 | pExtent = &pImage->pExtents[cExtents++];
|
---|
2254 | if (pPart->pszRawDevice)
|
---|
2255 | {
|
---|
2256 | /* Set up basename for extent description. Cannot use StrDup. */
|
---|
2257 | size_t cbBasename = strlen(pPart->pszRawDevice) + 1;
|
---|
2258 | char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
|
---|
2259 | if (!pszBasename)
|
---|
2260 | {
|
---|
2261 | rc = VERR_NO_MEMORY;
|
---|
2262 | goto out;
|
---|
2263 | }
|
---|
2264 | memcpy(pszBasename, pPart->pszRawDevice, cbBasename);
|
---|
2265 | pExtent->pszBasename = pszBasename;
|
---|
2266 | /* For raw disks the full name is identical to the base name. */
|
---|
2267 | pExtent->pszFullname = RTStrDup(pszBasename);
|
---|
2268 | if (!pExtent->pszFullname)
|
---|
2269 | {
|
---|
2270 | rc = VERR_NO_MEMORY;
|
---|
2271 | goto out;
|
---|
2272 | }
|
---|
2273 | pExtent->enmType = VMDKETYPE_FLAT;
|
---|
2274 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbPartition);
|
---|
2275 | pExtent->uSectorOffset = VMDK_BYTE2SECTOR(pPart->uPartitionStartOffset);
|
---|
2276 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
2277 | pExtent->fMetaDirty = false;
|
---|
2278 |
|
---|
2279 | rc = RTFileOpen(&pExtent->File, pExtent->pszFullname,
|
---|
2280 | RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
2281 | if (VBOX_FAILURE(rc))
|
---|
2282 | {
|
---|
2283 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not open raw partition file '%s'"), pExtent->pszFullname);
|
---|
2284 | goto out;
|
---|
2285 | }
|
---|
2286 | }
|
---|
2287 | else
|
---|
2288 | {
|
---|
2289 | pExtent->pszBasename = NULL;
|
---|
2290 | pExtent->pszFullname = NULL;
|
---|
2291 | pExtent->enmType = VMDKETYPE_ZERO;
|
---|
2292 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbPartition);
|
---|
2293 | pExtent->uSectorOffset = 0;
|
---|
2294 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
2295 | pExtent->fMetaDirty = false;
|
---|
2296 | }
|
---|
2297 | }
|
---|
2298 | }
|
---|
2299 | /* Another extent for filling up the rest of the image. */
|
---|
2300 | if (uStart != cbSize)
|
---|
2301 | {
|
---|
2302 | pExtent = &pImage->pExtents[cExtents++];
|
---|
2303 | pExtent->pszBasename = NULL;
|
---|
2304 | pExtent->pszFullname = NULL;
|
---|
2305 | pExtent->enmType = VMDKETYPE_ZERO;
|
---|
2306 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize - uStart);
|
---|
2307 | pExtent->uSectorOffset = 0;
|
---|
2308 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
2309 | pExtent->fMetaDirty = false;
|
---|
2310 | }
|
---|
2311 |
|
---|
2312 | pImage->enmImageType = enmType;
|
---|
2313 | rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType", "partitionedDevice");
|
---|
2314 | if (VBOX_FAILURE(rc))
|
---|
2315 | {
|
---|
2316 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pszFilename);
|
---|
2317 | goto out;
|
---|
2318 | }
|
---|
2319 | }
|
---|
2320 | }
|
---|
2321 | else
|
---|
2322 | {
|
---|
2323 | rc = VERR_NOT_IMPLEMENTED;
|
---|
2324 | goto out;
|
---|
2325 | }
|
---|
2326 | }
|
---|
2327 | else
|
---|
2328 | {
|
---|
2329 | /* Normal (growing) image which is not split into pieces. */
|
---|
2330 | rc = vmdkCreateExtents(pImage, 1);
|
---|
2331 | if (VBOX_FAILURE(rc))
|
---|
2332 | {
|
---|
2333 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pszFilename);
|
---|
2334 | goto out;
|
---|
2335 | }
|
---|
2336 | pExtent = &pImage->pExtents[0];
|
---|
2337 | pImage->File = NIL_RTFILE;
|
---|
2338 | rc = RTFileOpen(&pExtent->File, pszFilename,
|
---|
2339 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
|
---|
2340 | if (VBOX_FAILURE(rc))
|
---|
2341 | {
|
---|
2342 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pszFilename);
|
---|
2343 | goto out;
|
---|
2344 | }
|
---|
2345 |
|
---|
2346 | /* Set up basename for extent description. Cannot use StrDup, as it is
|
---|
2347 | * not guaranteed that the memory can be freed with RTMemTmpFree, which
|
---|
2348 | * must be used as in other code paths StrDup is not usable. */
|
---|
2349 | char *pszBasenameSubstr = RTPathFilename(pszFilename);
|
---|
2350 | Assert(pszBasenameSubstr);
|
---|
2351 | size_t cbBasenameSubstr = strlen(pszBasenameSubstr) + 1;
|
---|
2352 | char *pszBasename = (char *)RTMemTmpAlloc(cbBasenameSubstr);
|
---|
2353 | if (!pszBasename)
|
---|
2354 | {
|
---|
2355 | rc = VERR_NO_MEMORY;
|
---|
2356 | goto out;
|
---|
2357 | }
|
---|
2358 | memcpy(pszBasename, pszBasenameSubstr, cbBasenameSubstr);
|
---|
2359 | pExtent->pszBasename = pszBasename;
|
---|
2360 | pExtent->pszFullname = RTStrDup(pszFilename);
|
---|
2361 | if (!pExtent->pszFullname)
|
---|
2362 | {
|
---|
2363 | rc = VERR_NO_MEMORY;
|
---|
2364 | goto out;
|
---|
2365 | }
|
---|
2366 | pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
|
---|
2367 | pExtent->cSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64(cbSize, 65536));
|
---|
2368 | pExtent->cSectorsPerGrain = VMDK_BYTE2SECTOR(65536);
|
---|
2369 | pExtent->uDescriptorSector = 1;
|
---|
2370 | pExtent->cDescriptorSectors = VMDK_BYTE2SECTOR(pImage->cbDescAlloc);
|
---|
2371 | pExtent->cGTEntries = 512;
|
---|
2372 | cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
|
---|
2373 | pExtent->cSectorsPerGDE = cSectorsPerGDE;
|
---|
2374 | pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
|
---|
2375 | cSectorsPerGD = (pExtent->cGDEntries + (512 / sizeof(uint32_t) - 1)) / (512 / sizeof(uint32_t));
|
---|
2376 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
2377 | pExtent->fUncleanShutdown = true;
|
---|
2378 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
|
---|
2379 | pExtent->uSectorOffset = 0;
|
---|
2380 | pExtent->fMetaDirty = true;
|
---|
2381 |
|
---|
2382 | rc = vmdkCreateGrainDirectory(pExtent, pExtent->uDescriptorSector + pExtent->cDescriptorSectors, true);
|
---|
2383 | if (VBOX_FAILURE(rc))
|
---|
2384 | {
|
---|
2385 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not create new grain directory in '%s'"), pszFilename);
|
---|
2386 | goto out;
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | pImage->enmImageType = enmType;
|
---|
2390 | rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType", "monolithicSparse");
|
---|
2391 | if (VBOX_FAILURE(rc))
|
---|
2392 | {
|
---|
2393 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pszFilename);
|
---|
2394 | goto out;
|
---|
2395 | }
|
---|
2396 |
|
---|
2397 | /* The descriptor is part of the extent, move info to extent. */
|
---|
2398 | pExtent->pDescData = pImage->pDescData;
|
---|
2399 | pImage->pDescData = NULL;
|
---|
2400 | }
|
---|
2401 |
|
---|
2402 | pImage->cbSize = cbSize;
|
---|
2403 | if (pImage->cCylinders >= 1024 || pImage->cHeads != 16)
|
---|
2404 | pImage->enmTranslation = PDMBIOSTRANSLATION_LBA;
|
---|
2405 | else
|
---|
2406 | pImage->enmTranslation = PDMBIOSTRANSLATION_NONE;
|
---|
2407 |
|
---|
2408 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
2409 | {
|
---|
2410 | pExtent = &pImage->pExtents[i];
|
---|
2411 |
|
---|
2412 | rc = vmdkDescExtInsert(pImage, &pImage->Descriptor, pExtent->enmAccess,
|
---|
2413 | pExtent->cNominalSectors, pExtent->enmType,
|
---|
2414 | pExtent->pszBasename, pExtent->uSectorOffset);
|
---|
2415 | if (VBOX_FAILURE(rc))
|
---|
2416 | {
|
---|
2417 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: could not insert the extent list into descriptor in '%s'"), pszFilename);
|
---|
2418 | goto out;
|
---|
2419 | }
|
---|
2420 | }
|
---|
2421 | vmdkDescExtRemoveDummy(pImage, &pImage->Descriptor);
|
---|
2422 |
|
---|
2423 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
2424 | "ddb.geometry.cylinders", cCylinders);
|
---|
2425 | if (VBOX_FAILURE(rc))
|
---|
2426 | goto out;
|
---|
2427 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
2428 | "ddb.geometry.heads", cHeads);
|
---|
2429 | if (VBOX_FAILURE(rc))
|
---|
2430 | goto out;
|
---|
2431 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
2432 | "ddb.geometry.sectors", cSectors);
|
---|
2433 | if (VBOX_FAILURE(rc))
|
---|
2434 | goto out;
|
---|
2435 |
|
---|
2436 | pImage->cCylinders = cCylinders;
|
---|
2437 | pImage->cHeads = cHeads;
|
---|
2438 | pImage->cSectors = cSectors;
|
---|
2439 |
|
---|
2440 | rc = RTUuidCreate(&pImage->ImageUuid);
|
---|
2441 | if (VBOX_FAILURE(rc))
|
---|
2442 | goto out;
|
---|
2443 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
2444 | "ddb.uuid.image", &pImage->ImageUuid);
|
---|
2445 | if (VBOX_FAILURE(rc))
|
---|
2446 | {
|
---|
2447 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing image UUID in new descriptor in '%s'"), pszFilename);
|
---|
2448 | goto out;
|
---|
2449 | }
|
---|
2450 | RTUuidClear(&pImage->ParentUuid);
|
---|
2451 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
2452 | "ddb.uuid.parent", &pImage->ParentUuid);
|
---|
2453 | if (VBOX_FAILURE(rc))
|
---|
2454 | {
|
---|
2455 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing parent image UUID in new descriptor in '%s'"), pszFilename);
|
---|
2456 | goto out;
|
---|
2457 | }
|
---|
2458 | RTUuidClear(&pImage->ModificationUuid);
|
---|
2459 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
2460 | "ddb.uuid.modification", &pImage->ModificationUuid);
|
---|
2461 | if (VBOX_FAILURE(rc))
|
---|
2462 | {
|
---|
2463 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing modification UUID in new descriptor in '%s'"), pszFilename);
|
---|
2464 | goto out;
|
---|
2465 | }
|
---|
2466 |
|
---|
2467 | rc = vmdkAllocateGrainTableCache(pImage);
|
---|
2468 | if (VBOX_FAILURE(rc))
|
---|
2469 | goto out;
|
---|
2470 |
|
---|
2471 | rc = vmdkSetImageComment(pImage, pszComment);
|
---|
2472 | if (VBOX_FAILURE(rc))
|
---|
2473 | {
|
---|
2474 | rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: cannot set image comment in '%s'"), pszFilename);
|
---|
2475 | goto out;
|
---|
2476 | }
|
---|
2477 |
|
---|
2478 | rc = vmdkFlushImage(pImage);
|
---|
2479 |
|
---|
2480 | out:
|
---|
2481 | if (VBOX_FAILURE(rc))
|
---|
2482 | vmdkFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
|
---|
2483 | return rc;
|
---|
2484 | }
|
---|
2485 |
|
---|
2486 | static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment)
|
---|
2487 | {
|
---|
2488 | char *pszCommentEncoded;
|
---|
2489 | if (pszComment)
|
---|
2490 | {
|
---|
2491 | pszCommentEncoded = vmdkEncodeString(pszComment);
|
---|
2492 | if (!pszCommentEncoded)
|
---|
2493 | return VERR_NO_MEMORY;
|
---|
2494 | }
|
---|
2495 | else
|
---|
2496 | pszCommentEncoded = NULL;
|
---|
2497 | int rc = vmdkDescDDBSetStr(pImage, &pImage->Descriptor,
|
---|
2498 | "ddb.comment", pszCommentEncoded);
|
---|
2499 | if (pszComment)
|
---|
2500 | RTStrFree(pszCommentEncoded);
|
---|
2501 | if (VBOX_FAILURE(rc))
|
---|
2502 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing image comment in descriptor in '%s'"), pImage->pszFilename);
|
---|
2503 | return VINF_SUCCESS;
|
---|
2504 | }
|
---|
2505 |
|
---|
2506 | static void vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete)
|
---|
2507 | {
|
---|
2508 | if (pImage->enmImageType)
|
---|
2509 | {
|
---|
2510 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
2511 | {
|
---|
2512 | /* Mark all extents as clean. */
|
---|
2513 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
2514 | {
|
---|
2515 | if (( pImage->pExtents[i].enmType == VMDKETYPE_HOSTED_SPARSE
|
---|
2516 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
2517 | || pImage->pExtents[i].enmType == VMDKETYPE_ESX_SPARSE
|
---|
2518 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
2519 | )
|
---|
2520 | && pImage->pExtents[i].fUncleanShutdown)
|
---|
2521 | {
|
---|
2522 | pImage->pExtents[i].fUncleanShutdown = false;
|
---|
2523 | pImage->pExtents[i].fMetaDirty = true;
|
---|
2524 | }
|
---|
2525 | }
|
---|
2526 | }
|
---|
2527 | (void)vmdkFlushImage(pImage);
|
---|
2528 | }
|
---|
2529 | if (pImage->pExtents != NULL)
|
---|
2530 | {
|
---|
2531 | for (unsigned i = 0 ; i < pImage->cExtents; i++)
|
---|
2532 | vmdkFreeExtentData(&pImage->pExtents[i], fDelete);
|
---|
2533 | RTMemFree(pImage->pExtents);
|
---|
2534 | pImage->pExtents = NULL;
|
---|
2535 | }
|
---|
2536 | if (pImage->File != NIL_RTFILE)
|
---|
2537 | {
|
---|
2538 | RTFileClose(pImage->File);
|
---|
2539 | pImage->File = NIL_RTFILE;
|
---|
2540 | }
|
---|
2541 | if (fDelete && pImage->pszFilename)
|
---|
2542 | RTFileDelete(pImage->pszFilename);
|
---|
2543 | }
|
---|
2544 |
|
---|
2545 | static int vmdkFlushImage(PVMDKIMAGE pImage)
|
---|
2546 | {
|
---|
2547 | PVMDKEXTENT pExtent;
|
---|
2548 | int rc = VINF_SUCCESS;
|
---|
2549 |
|
---|
2550 | /* Update descriptor if changed. */
|
---|
2551 | if (pImage->Descriptor.fDirty)
|
---|
2552 | {
|
---|
2553 | rc = vmdkWriteDescriptor(pImage);
|
---|
2554 | if (VBOX_FAILURE(rc))
|
---|
2555 | goto out;
|
---|
2556 | }
|
---|
2557 |
|
---|
2558 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
2559 | {
|
---|
2560 | pExtent = &pImage->pExtents[i];
|
---|
2561 | if (pExtent->File != NIL_RTFILE && pExtent->fMetaDirty)
|
---|
2562 | {
|
---|
2563 | switch (pExtent->enmType)
|
---|
2564 | {
|
---|
2565 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
2566 | rc = vmdkWriteMetaSparseExtent(pExtent);
|
---|
2567 | if (VBOX_FAILURE(rc))
|
---|
2568 | goto out;
|
---|
2569 | break;
|
---|
2570 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
2571 | case VMDKETYPE_ESX_SPARSE:
|
---|
2572 | /** @todo update the header. */
|
---|
2573 | break;
|
---|
2574 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
2575 | case VMDKETYPE_FLAT:
|
---|
2576 | /* Nothing to do. */
|
---|
2577 | break;
|
---|
2578 | case VMDKETYPE_ZERO:
|
---|
2579 | default:
|
---|
2580 | AssertMsgFailed(("extent with type %d marked as dirty\n",
|
---|
2581 | pExtent->enmType));
|
---|
2582 | break;
|
---|
2583 | }
|
---|
2584 | }
|
---|
2585 | switch (pExtent->enmType)
|
---|
2586 | {
|
---|
2587 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
2588 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
2589 | case VMDKETYPE_ESX_SPARSE:
|
---|
2590 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
2591 | case VMDKETYPE_FLAT:
|
---|
2592 | /** @todo implement proper path absolute check. */
|
---|
2593 | if (pExtent->File != NIL_RTFILE && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) && !(pExtent->pszBasename[0] == RTPATH_SLASH))
|
---|
2594 | rc = RTFileFlush(pExtent->File);
|
---|
2595 | break;
|
---|
2596 | case VMDKETYPE_ZERO:
|
---|
2597 | /* No need to do anything for this extent. */
|
---|
2598 | break;
|
---|
2599 | default:
|
---|
2600 | AssertMsgFailed(("unknown extent type %d\n", pExtent->enmType));
|
---|
2601 | break;
|
---|
2602 | }
|
---|
2603 | }
|
---|
2604 |
|
---|
2605 | out:
|
---|
2606 | return rc;
|
---|
2607 | }
|
---|
2608 |
|
---|
2609 | static int vmdkFindExtent(PVMDKIMAGE pImage, uint64_t offSector, PVMDKEXTENT *ppExtent, uint64_t *puSectorInExtent)
|
---|
2610 | {
|
---|
2611 | PVMDKEXTENT pExtent = NULL;
|
---|
2612 | int rc = VINF_SUCCESS;
|
---|
2613 |
|
---|
2614 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
2615 | {
|
---|
2616 | if (offSector < pImage->pExtents[i].cNominalSectors)
|
---|
2617 | {
|
---|
2618 | pExtent = &pImage->pExtents[i];
|
---|
2619 | *puSectorInExtent = offSector + pImage->pExtents[i].uSectorOffset;
|
---|
2620 | break;
|
---|
2621 | }
|
---|
2622 | offSector -= pImage->pExtents[i].cNominalSectors;
|
---|
2623 | }
|
---|
2624 |
|
---|
2625 | if (pExtent)
|
---|
2626 | *ppExtent = pExtent;
|
---|
2627 | else
|
---|
2628 | rc = VERR_IO_SECTOR_NOT_FOUND;
|
---|
2629 |
|
---|
2630 | return rc;
|
---|
2631 | }
|
---|
2632 |
|
---|
2633 | static uint32_t vmdkGTCacheHash(PVMDKGTCACHE pCache, uint64_t uSector, unsigned uExtent)
|
---|
2634 | {
|
---|
2635 | /** @todo this hash function is quite simple, maybe use a better one which
|
---|
2636 | * scrambles the bits better. */
|
---|
2637 | return (uSector + uExtent) % pCache->cEntries;
|
---|
2638 | }
|
---|
2639 |
|
---|
2640 | static int vmdkGetSector(PVMDKGTCACHE pCache, PVMDKEXTENT pExtent,
|
---|
2641 | uint64_t uSector, uint64_t *puExtentSector)
|
---|
2642 | {
|
---|
2643 | uint64_t uGDIndex, uGTSector, uGTBlock;
|
---|
2644 | uint32_t uGTHash, uGTBlockIndex;
|
---|
2645 | PVMDKGTCACHEENTRY pGTCacheEntry;
|
---|
2646 | uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
|
---|
2647 | int rc;
|
---|
2648 |
|
---|
2649 | uGDIndex = uSector / pExtent->cSectorsPerGDE;
|
---|
2650 | if (uGDIndex >= pExtent->cGDEntries)
|
---|
2651 | return VERR_OUT_OF_RANGE;
|
---|
2652 | uGTSector = pExtent->pGD[uGDIndex];
|
---|
2653 | if (!uGTSector)
|
---|
2654 | {
|
---|
2655 | /* There is no grain table referenced by this grain directory
|
---|
2656 | * entry. So there is absolutely no data in this area. */
|
---|
2657 | *puExtentSector = 0;
|
---|
2658 | return VINF_SUCCESS;
|
---|
2659 | }
|
---|
2660 |
|
---|
2661 | uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
|
---|
2662 | uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
|
---|
2663 | pGTCacheEntry = &pCache->aGTCache[uGTHash];
|
---|
2664 | if ( pGTCacheEntry->uExtent != pExtent->uExtent
|
---|
2665 | || pGTCacheEntry->uGTBlock != uGTBlock)
|
---|
2666 | {
|
---|
2667 | /* Cache miss, fetch data from disk. */
|
---|
2668 | rc = RTFileReadAt(pExtent->File,
|
---|
2669 | VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
2670 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
2671 | if (VBOX_FAILURE(rc))
|
---|
2672 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot read grain table entry in '%s'"), pExtent->pszFullname);
|
---|
2673 | pGTCacheEntry->uExtent = pExtent->uExtent;
|
---|
2674 | pGTCacheEntry->uGTBlock = uGTBlock;
|
---|
2675 | for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
|
---|
2676 | pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
|
---|
2677 | }
|
---|
2678 | uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
|
---|
2679 | uint64_t uGrainSector = pGTCacheEntry->aGTData[uGTBlockIndex];
|
---|
2680 | if (uGrainSector)
|
---|
2681 | *puExtentSector = uGrainSector + uSector % pExtent->cSectorsPerGrain;
|
---|
2682 | else
|
---|
2683 | *puExtentSector = 0;
|
---|
2684 | return VINF_SUCCESS;
|
---|
2685 | }
|
---|
2686 |
|
---|
2687 | /**
|
---|
2688 | * Internal. Allocates a new grain table (if necessary), writes the grain
|
---|
2689 | * and updates the grain table. The cache is also updated by this operation.
|
---|
2690 | * This is separate from vmdkGetSector, because that should be as fast as
|
---|
2691 | * possible. Most code from vmdkGetSector also appears here.
|
---|
2692 | */
|
---|
2693 | static int vmdkAllocGrain(PVMDKGTCACHE pCache, PVMDKEXTENT pExtent,
|
---|
2694 | uint64_t uSector, const void *pvBuf, uint64_t cbWrite)
|
---|
2695 | {
|
---|
2696 | uint64_t uGDIndex, uGTSector, uRGTSector, uGTBlock;
|
---|
2697 | uint64_t cbExtentSize;
|
---|
2698 | uint32_t uGTHash, uGTBlockIndex;
|
---|
2699 | PVMDKGTCACHEENTRY pGTCacheEntry;
|
---|
2700 | uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
|
---|
2701 | int rc;
|
---|
2702 |
|
---|
2703 | uGDIndex = uSector / pExtent->cSectorsPerGDE;
|
---|
2704 | if (uGDIndex >= pExtent->cGDEntries)
|
---|
2705 | return VERR_OUT_OF_RANGE;
|
---|
2706 | uGTSector = pExtent->pGD[uGDIndex];
|
---|
2707 | uRGTSector = pExtent->pRGD[uGDIndex];
|
---|
2708 | if (!uGTSector)
|
---|
2709 | {
|
---|
2710 | /* There is no grain table referenced by this grain directory
|
---|
2711 | * entry. So there is absolutely no data in this area. Allocate
|
---|
2712 | * a new grain table and put the reference to it in the GDs. */
|
---|
2713 | rc = RTFileGetSize(pExtent->File, &cbExtentSize);
|
---|
2714 | if (VBOX_FAILURE(rc))
|
---|
2715 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: error getting size in '%s'"), pExtent->pszFullname);
|
---|
2716 | Assert(!(cbExtentSize % 512));
|
---|
2717 | uGTSector = VMDK_BYTE2SECTOR(cbExtentSize);
|
---|
2718 | /* Normally the grain table is preallocated for hosted sparse extents
|
---|
2719 | * that support more than 32 bit sector numbers. So this shouldn't
|
---|
2720 | * ever happen on a valid extent. */
|
---|
2721 | if (uGTSector > UINT32_MAX)
|
---|
2722 | return VERR_VDI_INVALID_HEADER;
|
---|
2723 | /* Write grain table by writing the required number of grain table
|
---|
2724 | * cache chunks. Avoids dynamic memory allocation, but is a bit
|
---|
2725 | * slower. But as this is a pretty infrequently occurring case it
|
---|
2726 | * should be acceptable. */
|
---|
2727 | memset(aGTDataTmp, '\0', sizeof(aGTDataTmp));
|
---|
2728 | for (unsigned i = 0; i < pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE; i++)
|
---|
2729 | {
|
---|
2730 | rc = RTFileWriteAt(pExtent->File, VMDK_SECTOR2BYTE(uGTSector) + i * sizeof(aGTDataTmp), aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
2731 | if (VBOX_FAILURE(rc))
|
---|
2732 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot write grain table allocation in '%s'"), pExtent->pszFullname);
|
---|
2733 | }
|
---|
2734 | if (pExtent->pRGD)
|
---|
2735 | {
|
---|
2736 | rc = RTFileGetSize(pExtent->File, &cbExtentSize);
|
---|
2737 | if (VBOX_FAILURE(rc))
|
---|
2738 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: error getting size in '%s'"), pExtent->pszFullname);
|
---|
2739 | Assert(!(cbExtentSize % 512));
|
---|
2740 | uRGTSector = VMDK_BYTE2SECTOR(cbExtentSize);
|
---|
2741 | /* Write backup grain table by writing the required number of grain
|
---|
2742 | * table cache chunks. Avoids dynamic memory allocation, but is a
|
---|
2743 | * bit slower. But as this is a pretty infrequently occurring case
|
---|
2744 | * it should be acceptable. */
|
---|
2745 | for (unsigned i = 0; i < pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE; i++)
|
---|
2746 | {
|
---|
2747 | rc = RTFileWriteAt(pExtent->File, VMDK_SECTOR2BYTE(uRGTSector) + i * sizeof(aGTDataTmp), aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
2748 | if (VBOX_FAILURE(rc))
|
---|
2749 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain table allocation in '%s'"), pExtent->pszFullname);
|
---|
2750 | }
|
---|
2751 | }
|
---|
2752 |
|
---|
2753 | /* Update the grain directory on disk (doing it before writing the
|
---|
2754 | * grain table will result in a garbled extent if the operation is
|
---|
2755 | * aborted for some reason. Otherwise the worst that can happen is
|
---|
2756 | * some unused sectors in the extent. */
|
---|
2757 | uint32_t uGTSectorLE = RT_H2LE_U64(uGTSector);
|
---|
2758 | rc = RTFileWriteAt(pExtent->File, VMDK_SECTOR2BYTE(pExtent->uSectorGD) + uGDIndex * sizeof(uGTSectorLE), &uGTSectorLE, sizeof(uGTSectorLE), NULL);
|
---|
2759 | if (VBOX_FAILURE(rc))
|
---|
2760 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot write grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
2761 | if (pExtent->pRGD)
|
---|
2762 | {
|
---|
2763 | uint32_t uRGTSectorLE = RT_H2LE_U64(uRGTSector);
|
---|
2764 | rc = RTFileWriteAt(pExtent->File, VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + uGDIndex * sizeof(uRGTSectorLE), &uRGTSectorLE, sizeof(uRGTSectorLE), NULL);
|
---|
2765 | if (VBOX_FAILURE(rc))
|
---|
2766 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
2767 | }
|
---|
2768 |
|
---|
2769 | /* As the final step update the in-memory copy of the GDs. */
|
---|
2770 | pExtent->pGD[uGDIndex] = uGTSector;
|
---|
2771 | if (pExtent->pRGD)
|
---|
2772 | pExtent->pRGD[uGDIndex] = uRGTSector;
|
---|
2773 | }
|
---|
2774 |
|
---|
2775 | rc = RTFileGetSize(pExtent->File, &cbExtentSize);
|
---|
2776 | if (VBOX_FAILURE(rc))
|
---|
2777 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: error getting size in '%s'"), pExtent->pszFullname);
|
---|
2778 | Assert(!(cbExtentSize % 512));
|
---|
2779 |
|
---|
2780 | /* Write the data. */
|
---|
2781 | rc = RTFileWriteAt(pExtent->File, cbExtentSize, pvBuf, cbWrite, NULL);
|
---|
2782 | if (VBOX_FAILURE(rc))
|
---|
2783 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot write allocated data block in '%s'"), pExtent->pszFullname);
|
---|
2784 |
|
---|
2785 | /* Update the grain table (and the cache). */
|
---|
2786 | uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
|
---|
2787 | uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
|
---|
2788 | pGTCacheEntry = &pCache->aGTCache[uGTHash];
|
---|
2789 | if ( pGTCacheEntry->uExtent != pExtent->uExtent
|
---|
2790 | || pGTCacheEntry->uGTBlock != uGTBlock)
|
---|
2791 | {
|
---|
2792 | /* Cache miss, fetch data from disk. */
|
---|
2793 | rc = RTFileReadAt(pExtent->File,
|
---|
2794 | VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
2795 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
2796 | if (VBOX_FAILURE(rc))
|
---|
2797 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot read allocated grain table entry in '%s'"), pExtent->pszFullname);
|
---|
2798 | pGTCacheEntry->uExtent = pExtent->uExtent;
|
---|
2799 | pGTCacheEntry->uGTBlock = uGTBlock;
|
---|
2800 | for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
|
---|
2801 | pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
|
---|
2802 | }
|
---|
2803 | else
|
---|
2804 | {
|
---|
2805 | /* Cache hit. Convert grain table block back to disk format, otherwise
|
---|
2806 | * the code below will write garbage for all but the updated entry. */
|
---|
2807 | for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
|
---|
2808 | aGTDataTmp[i] = RT_H2LE_U32(pGTCacheEntry->aGTData[i]);
|
---|
2809 | }
|
---|
2810 | uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
|
---|
2811 | aGTDataTmp[uGTBlockIndex] = RT_H2LE_U32(VMDK_BYTE2SECTOR(cbExtentSize));
|
---|
2812 | pGTCacheEntry->aGTData[uGTBlockIndex] = VMDK_BYTE2SECTOR(cbExtentSize);
|
---|
2813 | /* Update grain table on disk. */
|
---|
2814 | rc = RTFileWriteAt(pExtent->File,
|
---|
2815 | VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
2816 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
2817 | if (VBOX_FAILURE(rc))
|
---|
2818 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot write updated grain table in '%s'"), pExtent->pszFullname);
|
---|
2819 | if (pExtent->pRGD)
|
---|
2820 | {
|
---|
2821 | /* Update backup grain table on disk. */
|
---|
2822 | rc = RTFileWriteAt(pExtent->File,
|
---|
2823 | VMDK_SECTOR2BYTE(uRGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
2824 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
2825 | if (VBOX_FAILURE(rc))
|
---|
2826 | return vmdkError(pExtent->pImage, rc, RT_SRC_POS, N_("VMDK: cannot write updated backup grain table in '%s'"), pExtent->pszFullname);
|
---|
2827 | }
|
---|
2828 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
2829 | if (VBOX_SUCCESS(rc) && pExtent->enmType == VMDKETYPE_ESX_SPARSE)
|
---|
2830 | {
|
---|
2831 | pExtent->uFreeSector = uGTSector + VMDK_BYTE2SECTOR(cbWrite);
|
---|
2832 | pExtent->fMetaDirty = true;
|
---|
2833 | }
|
---|
2834 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
2835 | return rc;
|
---|
2836 | }
|
---|
2837 |
|
---|
2838 | static int vmdkOpen(const char *pszFilename, unsigned uOpenFlags, PFNVDERROR pfnError, void *pvErrorUser, void **ppvBackendData)
|
---|
2839 | {
|
---|
2840 | int rc;
|
---|
2841 | PVMDKIMAGE pImage;
|
---|
2842 |
|
---|
2843 | /** @todo check the image file name for invalid characters, especially double quotes. */
|
---|
2844 |
|
---|
2845 | /* Check open flags. All valid flags are supported. */
|
---|
2846 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
2847 | {
|
---|
2848 | rc = VERR_INVALID_PARAMETER;
|
---|
2849 | goto out;
|
---|
2850 | }
|
---|
2851 |
|
---|
2852 | pImage = (PVMDKIMAGE)RTMemAllocZ(sizeof(VMDKIMAGE));
|
---|
2853 | if (!pImage)
|
---|
2854 | {
|
---|
2855 | rc = VERR_NO_MEMORY;
|
---|
2856 | goto out;
|
---|
2857 | }
|
---|
2858 | pImage->pszFilename = pszFilename;
|
---|
2859 | pImage->File = NIL_RTFILE;
|
---|
2860 | pImage->pExtents = NULL;
|
---|
2861 | pImage->pGTCache = NULL;
|
---|
2862 | pImage->pDescData = NULL;
|
---|
2863 | pImage->pfnError = pfnError;
|
---|
2864 | pImage->pvErrorUser = pvErrorUser;
|
---|
2865 |
|
---|
2866 | rc = vmdkOpenImage(pImage, pszFilename, uOpenFlags);
|
---|
2867 | if (VBOX_SUCCESS(rc))
|
---|
2868 | *ppvBackendData = pImage;
|
---|
2869 |
|
---|
2870 | out:
|
---|
2871 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
2872 | return rc;
|
---|
2873 | }
|
---|
2874 |
|
---|
2875 | static int vmdkCreate(const char *pszFilename, VDIMAGETYPE enmType,
|
---|
2876 | uint64_t cbSize, unsigned uImageFlags,
|
---|
2877 | const char *pszComment, uint32_t cCylinders,
|
---|
2878 | uint32_t cHeads, uint32_t cSectors, unsigned uOpenFlags,
|
---|
2879 | PFNVMPROGRESS pfnProgress, void *pvUser,
|
---|
2880 | PFNVDERROR pfnError, void *pvErrorUser,
|
---|
2881 | void **ppvBackendData)
|
---|
2882 | {
|
---|
2883 | int rc;
|
---|
2884 | PVMDKIMAGE pImage;
|
---|
2885 |
|
---|
2886 | /** @todo check the image file name for invalid characters, especially double quotes. */
|
---|
2887 |
|
---|
2888 | /* Check open flags. All valid flags are supported. */
|
---|
2889 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
2890 | {
|
---|
2891 | rc = VERR_INVALID_PARAMETER;
|
---|
2892 | goto out;
|
---|
2893 | }
|
---|
2894 |
|
---|
2895 | pImage = (PVMDKIMAGE)RTMemAllocZ(sizeof(VMDKIMAGE));
|
---|
2896 | if (!pImage)
|
---|
2897 | {
|
---|
2898 | rc = VERR_NO_MEMORY;
|
---|
2899 | goto out;
|
---|
2900 | }
|
---|
2901 | pImage->pszFilename = pszFilename;
|
---|
2902 | pImage->File = NIL_RTFILE;
|
---|
2903 | pImage->pExtents = NULL;
|
---|
2904 | pImage->pGTCache = NULL;
|
---|
2905 | pImage->pDescData = NULL;
|
---|
2906 | pImage->pfnError = pfnError;
|
---|
2907 | pImage->pvErrorUser = pvErrorUser;
|
---|
2908 | pImage->cbDescAlloc = VMDK_SECTOR2BYTE(20);
|
---|
2909 | pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
|
---|
2910 | if (!pImage->pDescData)
|
---|
2911 | {
|
---|
2912 | rc = VERR_NO_MEMORY;
|
---|
2913 | goto out;
|
---|
2914 | }
|
---|
2915 |
|
---|
2916 | rc = vmdkCreateImage(pImage, pszFilename, enmType, cbSize, uImageFlags,
|
---|
2917 | pszComment, cCylinders, cHeads, cSectors);
|
---|
2918 | if (VBOX_SUCCESS(rc))
|
---|
2919 | {
|
---|
2920 | /* So far the image is opened in read/write mode. Make sure the
|
---|
2921 | * image is opened in read-only mode if the caller requested that. */
|
---|
2922 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2923 | {
|
---|
2924 | vmdkFreeImage(pImage, false);
|
---|
2925 | rc = vmdkOpenImage(pImage, pszFilename, uOpenFlags);
|
---|
2926 | if (VBOX_FAILURE(rc))
|
---|
2927 | goto out;
|
---|
2928 | }
|
---|
2929 | *ppvBackendData = pImage;
|
---|
2930 | }
|
---|
2931 |
|
---|
2932 | out:
|
---|
2933 | /** @todo implement meaningful progress stuff (especially for fixed images). */
|
---|
2934 | if ( VBOX_SUCCESS(rc)
|
---|
2935 | && pfnProgress)
|
---|
2936 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2937 |
|
---|
2938 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
2939 | return rc;
|
---|
2940 | }
|
---|
2941 |
|
---|
2942 | static int vmdkClose(void *pBackendData, bool fDelete)
|
---|
2943 | {
|
---|
2944 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
2945 | int rc = VINF_SUCCESS;
|
---|
2946 |
|
---|
2947 | /* Freeing a never allocated image (e.g. because the open failed) is
|
---|
2948 | * not signalled as an error. After all nothing bad happens. */
|
---|
2949 | if (pImage)
|
---|
2950 | vmdkFreeImage(pImage, fDelete);
|
---|
2951 |
|
---|
2952 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
2953 | return rc;
|
---|
2954 | }
|
---|
2955 |
|
---|
2956 | static int vmdkRead(void *pBackendData, uint64_t uOffset, void *pvBuf, size_t cbRead, size_t *pcbActuallyRead)
|
---|
2957 | {
|
---|
2958 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
2959 | PVMDKEXTENT pExtent;
|
---|
2960 | uint64_t uSectorExtentRel;
|
---|
2961 | uint64_t uSectorExtentAbs;
|
---|
2962 | int rc;
|
---|
2963 |
|
---|
2964 | Assert(uOffset % 512 == 0);
|
---|
2965 | Assert(cbRead % 512 == 0);
|
---|
2966 |
|
---|
2967 | if (uOffset + cbRead > pImage->cbSize)
|
---|
2968 | {
|
---|
2969 | rc = VERR_INVALID_PARAMETER;
|
---|
2970 | goto out;
|
---|
2971 | }
|
---|
2972 |
|
---|
2973 | rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
|
---|
2974 | &pExtent, &uSectorExtentRel);
|
---|
2975 | if (VBOX_FAILURE(rc))
|
---|
2976 | goto out;
|
---|
2977 |
|
---|
2978 | /* Check access permissions as defined in the extent descriptor. */
|
---|
2979 | if (pExtent->enmAccess == VMDKACCESS_NOACCESS)
|
---|
2980 | {
|
---|
2981 | rc = VERR_VDI_INVALID_STATE;
|
---|
2982 | goto out;
|
---|
2983 | }
|
---|
2984 |
|
---|
2985 | /* Clip read range to remain in this extent. */
|
---|
2986 | cbRead = RT_MIN(cbRead, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
2987 |
|
---|
2988 | /* Handle the read according to the current extent type. */
|
---|
2989 | switch (pExtent->enmType)
|
---|
2990 | {
|
---|
2991 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
2992 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
2993 | case VMDKETYPE_ESX_SPARSE:
|
---|
2994 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
2995 | rc = vmdkGetSector(pImage->pGTCache, pExtent, uSectorExtentRel,
|
---|
2996 | &uSectorExtentAbs);
|
---|
2997 | if (VBOX_FAILURE(rc))
|
---|
2998 | goto out;
|
---|
2999 | /* Clip read range to at most the rest of the grain. */
|
---|
3000 | cbRead = RT_MIN(cbRead, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSectorExtentRel % pExtent->cSectorsPerGrain));
|
---|
3001 | Assert(!(cbRead % 512));
|
---|
3002 | if (uSectorExtentAbs == 0)
|
---|
3003 | rc = VINF_VDI_BLOCK_FREE;
|
---|
3004 | else
|
---|
3005 | rc = RTFileReadAt(pExtent->File,
|
---|
3006 | VMDK_SECTOR2BYTE(uSectorExtentAbs),
|
---|
3007 | pvBuf, cbRead, NULL);
|
---|
3008 | break;
|
---|
3009 | case VMDKETYPE_FLAT:
|
---|
3010 | rc = RTFileReadAt(pExtent->File, VMDK_SECTOR2BYTE(uSectorExtentRel),
|
---|
3011 | pvBuf, cbRead, NULL);
|
---|
3012 | break;
|
---|
3013 | case VMDKETYPE_ZERO:
|
---|
3014 | memset(pvBuf, '\0', cbRead);
|
---|
3015 | break;
|
---|
3016 | }
|
---|
3017 | *pcbActuallyRead = cbRead;
|
---|
3018 |
|
---|
3019 | out:
|
---|
3020 | return rc;
|
---|
3021 | }
|
---|
3022 |
|
---|
3023 | static int vmdkWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf, size_t cbWrite, size_t *pcbWriteProcess, size_t *pcbPreRead, size_t *pcbPostRead)
|
---|
3024 | {
|
---|
3025 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3026 | PVMDKEXTENT pExtent;
|
---|
3027 | uint64_t uSectorExtentRel;
|
---|
3028 | uint64_t uSectorExtentAbs;
|
---|
3029 | int rc;
|
---|
3030 |
|
---|
3031 | Assert(uOffset % 512 == 0);
|
---|
3032 | Assert(cbWrite % 512 == 0);
|
---|
3033 |
|
---|
3034 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
3035 | {
|
---|
3036 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
3037 | goto out;
|
---|
3038 | }
|
---|
3039 |
|
---|
3040 | /* No size check here, will do that later when the extent is located.
|
---|
3041 | * There are sparse images out there which according to the spec are
|
---|
3042 | * invalid, because the total size is not a multiple of the grain size.
|
---|
3043 | * Also for sparse images which are stitched together in odd ways (not at
|
---|
3044 | * grain boundaries, and with the nominal size not being a multiple of the
|
---|
3045 | * grain size), this would prevent writing to the last grain. */
|
---|
3046 |
|
---|
3047 | rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
|
---|
3048 | &pExtent, &uSectorExtentRel);
|
---|
3049 | if (VBOX_FAILURE(rc))
|
---|
3050 | goto out;
|
---|
3051 |
|
---|
3052 | /* Check access permissions as defined in the extent descriptor. */
|
---|
3053 | if (pExtent->enmAccess != VMDKACCESS_READWRITE)
|
---|
3054 | {
|
---|
3055 | rc = VERR_VDI_INVALID_STATE;
|
---|
3056 | goto out;
|
---|
3057 | }
|
---|
3058 |
|
---|
3059 | /** @todo implement suppressing of zero data writes (a bit tricky in this
|
---|
3060 | * case, as VMDK has no marker for zero blocks). We somehow need to get the
|
---|
3061 | * information whether the information in this area is all zeroes as of the
|
---|
3062 | * parent image. Then (based on the assumption that parent images are
|
---|
3063 | * immutable) the write can be ignored. */
|
---|
3064 |
|
---|
3065 | /* Handle the write according to the current extent type. */
|
---|
3066 | switch (pExtent->enmType)
|
---|
3067 | {
|
---|
3068 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
3069 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
3070 | case VMDKETYPE_ESX_SPARSE:
|
---|
3071 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
3072 | rc = vmdkGetSector(pImage->pGTCache, pExtent, uSectorExtentRel,
|
---|
3073 | &uSectorExtentAbs);
|
---|
3074 | if (VBOX_FAILURE(rc))
|
---|
3075 | goto out;
|
---|
3076 | /* Clip write range to at most the rest of the grain. */
|
---|
3077 | cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSectorExtentRel % pExtent->cSectorsPerGrain));
|
---|
3078 | if (uSectorExtentAbs == 0)
|
---|
3079 | {
|
---|
3080 | if (cbWrite == VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
|
---|
3081 | {
|
---|
3082 | /* Full block write to a previously unallocated block.
|
---|
3083 | * Allocate GT and find out where to store the grain. */
|
---|
3084 | rc = vmdkAllocGrain(pImage->pGTCache, pExtent,
|
---|
3085 | uSectorExtentRel, pvBuf, cbWrite);
|
---|
3086 | *pcbPreRead = 0;
|
---|
3087 | *pcbPostRead = 0;
|
---|
3088 | }
|
---|
3089 | else
|
---|
3090 | {
|
---|
3091 | /* Clip write range to remain in this extent. */
|
---|
3092 | cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
3093 | *pcbPreRead = VMDK_SECTOR2BYTE(uSectorExtentRel % pExtent->cSectorsPerGrain);
|
---|
3094 | *pcbPostRead = VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain) - cbWrite - *pcbPreRead;
|
---|
3095 | rc = VINF_VDI_BLOCK_FREE;
|
---|
3096 | }
|
---|
3097 | }
|
---|
3098 | else
|
---|
3099 | rc = RTFileWriteAt(pExtent->File,
|
---|
3100 | VMDK_SECTOR2BYTE(uSectorExtentAbs),
|
---|
3101 | pvBuf, cbWrite, NULL);
|
---|
3102 | break;
|
---|
3103 | case VMDKETYPE_FLAT:
|
---|
3104 | /* Clip write range to remain in this extent. */
|
---|
3105 | cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
3106 | rc = RTFileWriteAt(pExtent->File, VMDK_SECTOR2BYTE(uSectorExtentRel), pvBuf, cbWrite, NULL);
|
---|
3107 | break;
|
---|
3108 | case VMDKETYPE_ZERO:
|
---|
3109 | /* Clip write range to remain in this extent. */
|
---|
3110 | cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
3111 | break;
|
---|
3112 | }
|
---|
3113 | if (pcbWriteProcess)
|
---|
3114 | *pcbWriteProcess = cbWrite;
|
---|
3115 |
|
---|
3116 | out:
|
---|
3117 | return rc;
|
---|
3118 | }
|
---|
3119 |
|
---|
3120 | static int vmdkFlush(void *pBackendData)
|
---|
3121 | {
|
---|
3122 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3123 |
|
---|
3124 | int rc = vmdkFlushImage(pImage);
|
---|
3125 |
|
---|
3126 | return rc;
|
---|
3127 | }
|
---|
3128 |
|
---|
3129 | static int vmdkGetImageType(void *pBackendData, PVDIMAGETYPE penmImageType)
|
---|
3130 | {
|
---|
3131 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3132 | int rc = VINF_SUCCESS;
|
---|
3133 |
|
---|
3134 | Assert(pImage);
|
---|
3135 | Assert(penmImageType);
|
---|
3136 |
|
---|
3137 | if (pImage && pImage->cExtents != 0)
|
---|
3138 | *penmImageType = pImage->enmImageType;
|
---|
3139 | else
|
---|
3140 | rc = VERR_VDI_NOT_OPENED;
|
---|
3141 |
|
---|
3142 | return rc;
|
---|
3143 | }
|
---|
3144 |
|
---|
3145 | static uint64_t vmdkGetSize(void *pBackendData)
|
---|
3146 | {
|
---|
3147 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3148 |
|
---|
3149 | Assert(pImage);
|
---|
3150 |
|
---|
3151 | if (pImage)
|
---|
3152 | return pImage->cbSize;
|
---|
3153 | else
|
---|
3154 | return 0;
|
---|
3155 | }
|
---|
3156 |
|
---|
3157 | static int vmdkGetGeometry(void *pBackendData, unsigned *pcCylinders, unsigned *pcHeads, unsigned *pcSectors)
|
---|
3158 | {
|
---|
3159 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3160 | int rc;
|
---|
3161 |
|
---|
3162 | Assert(pImage);
|
---|
3163 |
|
---|
3164 | if (pImage)
|
---|
3165 | {
|
---|
3166 | if (pImage->cCylinders)
|
---|
3167 | {
|
---|
3168 | *pcCylinders = pImage->cCylinders;
|
---|
3169 | *pcHeads = pImage->cHeads;
|
---|
3170 | *pcSectors = pImage->cSectors;
|
---|
3171 | rc = VINF_SUCCESS;
|
---|
3172 | }
|
---|
3173 | else
|
---|
3174 | rc = VERR_VDI_GEOMETRY_NOT_SET;
|
---|
3175 | }
|
---|
3176 | else
|
---|
3177 | rc = VERR_VDI_NOT_OPENED;
|
---|
3178 | LogFlow(("%s: returned %Vrc (CHS=%u/%u/%u)\n", __FUNCTION__, rc,
|
---|
3179 | pImage->cCylinders, pImage->cHeads, pImage->cSectors));
|
---|
3180 | return rc;
|
---|
3181 | }
|
---|
3182 |
|
---|
3183 | static int vmdkSetGeometry(void *pBackendData, unsigned cCylinders, unsigned cHeads, unsigned cSectors)
|
---|
3184 | {
|
---|
3185 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3186 | int rc;
|
---|
3187 |
|
---|
3188 | Assert(pImage);
|
---|
3189 |
|
---|
3190 | if (pImage)
|
---|
3191 | {
|
---|
3192 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
3193 | {
|
---|
3194 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
3195 | goto out;
|
---|
3196 | }
|
---|
3197 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
3198 | "ddb.geometry.cylinders", cCylinders);
|
---|
3199 | if (VBOX_FAILURE(rc))
|
---|
3200 | goto out;
|
---|
3201 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
3202 | "ddb.geometry.heads", cHeads);
|
---|
3203 | if (VBOX_FAILURE(rc))
|
---|
3204 | goto out;
|
---|
3205 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
3206 | "ddb.geometry.sectors", cSectors);
|
---|
3207 | if (VBOX_FAILURE(rc))
|
---|
3208 | goto out;
|
---|
3209 |
|
---|
3210 | pImage->cCylinders = cCylinders;
|
---|
3211 | pImage->cHeads = cHeads;
|
---|
3212 | pImage->cSectors = cSectors;
|
---|
3213 | rc = VINF_SUCCESS;
|
---|
3214 | }
|
---|
3215 | else
|
---|
3216 | rc = VERR_VDI_NOT_OPENED;
|
---|
3217 |
|
---|
3218 | out:
|
---|
3219 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
3220 | return rc;
|
---|
3221 | }
|
---|
3222 |
|
---|
3223 | static int vmdkGetTranslation(void *pBackendData, PPDMBIOSTRANSLATION penmTranslation)
|
---|
3224 | {
|
---|
3225 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3226 | int rc;
|
---|
3227 |
|
---|
3228 | Assert(pImage);
|
---|
3229 |
|
---|
3230 | if (pImage)
|
---|
3231 | {
|
---|
3232 | if (pImage->enmTranslation)
|
---|
3233 | {
|
---|
3234 | *penmTranslation = pImage->enmTranslation;
|
---|
3235 | rc = VINF_SUCCESS;
|
---|
3236 | }
|
---|
3237 | else
|
---|
3238 | rc = VERR_VDI_GEOMETRY_NOT_SET;
|
---|
3239 | }
|
---|
3240 | else
|
---|
3241 | rc = VERR_VDI_NOT_OPENED;
|
---|
3242 | LogFlow(("%s: returned %Vrc (%d)\n", __FUNCTION__, rc,
|
---|
3243 | pImage->enmTranslation));
|
---|
3244 | return rc;
|
---|
3245 | }
|
---|
3246 |
|
---|
3247 | static int vmdkSetTranslation(void *pBackendData, PDMBIOSTRANSLATION enmTranslation)
|
---|
3248 | {
|
---|
3249 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3250 | int rc;
|
---|
3251 |
|
---|
3252 | Assert(pImage);
|
---|
3253 |
|
---|
3254 | if (pImage)
|
---|
3255 | {
|
---|
3256 | /** @todo maybe store this in the image descriptor */
|
---|
3257 | pImage->enmTranslation = enmTranslation;
|
---|
3258 | rc = VINF_SUCCESS;
|
---|
3259 | }
|
---|
3260 | else
|
---|
3261 | rc = VERR_VDI_NOT_OPENED;
|
---|
3262 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
3263 | return rc;
|
---|
3264 | }
|
---|
3265 |
|
---|
3266 | static unsigned vmdkGetOpenFlags(void *pBackendData)
|
---|
3267 | {
|
---|
3268 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3269 | unsigned uOpenFlags;
|
---|
3270 |
|
---|
3271 | Assert(pImage);
|
---|
3272 |
|
---|
3273 | if (pImage)
|
---|
3274 | uOpenFlags = pImage->uOpenFlags;
|
---|
3275 | else
|
---|
3276 | uOpenFlags = 0;
|
---|
3277 |
|
---|
3278 | LogFlow(("%s: returned %d\n", __FUNCTION__, uOpenFlags));
|
---|
3279 | return uOpenFlags;
|
---|
3280 | }
|
---|
3281 |
|
---|
3282 | static int vmdkSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
|
---|
3283 | {
|
---|
3284 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3285 | int rc;
|
---|
3286 | const char *pszFilename;
|
---|
3287 |
|
---|
3288 | /* Image must be opened and the new flags must be valid. Just readonly flag
|
---|
3289 | * is supported. */
|
---|
3290 | if (!pImage || uOpenFlags & ~VD_OPEN_FLAGS_READONLY)
|
---|
3291 | {
|
---|
3292 | rc = VERR_INVALID_PARAMETER;
|
---|
3293 | goto out;
|
---|
3294 | }
|
---|
3295 |
|
---|
3296 | /* Implement this operation via reopening the image. */
|
---|
3297 | pszFilename = pImage->pszFilename;
|
---|
3298 | vmdkFreeImage(pImage, false);
|
---|
3299 | rc = vmdkOpenImage(pImage, pszFilename, uOpenFlags);
|
---|
3300 |
|
---|
3301 | out:
|
---|
3302 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
3303 | return rc;
|
---|
3304 | }
|
---|
3305 |
|
---|
3306 | static int vmdkGetComment(void *pBackendData, char *pszComment, size_t cbComment)
|
---|
3307 | {
|
---|
3308 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3309 | int rc;
|
---|
3310 |
|
---|
3311 | Assert(pImage);
|
---|
3312 |
|
---|
3313 | if (pImage)
|
---|
3314 | {
|
---|
3315 | const char *pszCommentEncoded = NULL;
|
---|
3316 | rc = vmdkDescDDBGetStr(pImage, &pImage->Descriptor,
|
---|
3317 | "ddb.comment", &pszCommentEncoded);
|
---|
3318 | if (rc == VERR_VDI_VALUE_NOT_FOUND)
|
---|
3319 | pszCommentEncoded = NULL;
|
---|
3320 | else if (VBOX_FAILURE(rc))
|
---|
3321 | goto out;
|
---|
3322 |
|
---|
3323 | if (pszComment)
|
---|
3324 | rc = vmdkDecodeString(pszCommentEncoded, pszComment, cbComment);
|
---|
3325 | else
|
---|
3326 | {
|
---|
3327 | *pszComment = '\0';
|
---|
3328 | rc = VINF_SUCCESS;
|
---|
3329 | }
|
---|
3330 | if (pszCommentEncoded)
|
---|
3331 | RTStrFree((char *)(void *)pszCommentEncoded);
|
---|
3332 | }
|
---|
3333 | else
|
---|
3334 | rc = VERR_VDI_NOT_OPENED;
|
---|
3335 |
|
---|
3336 | out:
|
---|
3337 | LogFlow(("%s: returned %Vrc comment='%s'\n", __FUNCTION__, rc, pszComment));
|
---|
3338 | return rc;
|
---|
3339 | }
|
---|
3340 |
|
---|
3341 | static int vmdkSetComment(void *pBackendData, const char *pszComment)
|
---|
3342 | {
|
---|
3343 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3344 | int rc;
|
---|
3345 |
|
---|
3346 | LogFlow(("%s: comment '%s'\n", pszComment));
|
---|
3347 | Assert(pImage);
|
---|
3348 |
|
---|
3349 | if (pImage)
|
---|
3350 | {
|
---|
3351 | rc = vmdkSetImageComment(pImage, pszComment);
|
---|
3352 | }
|
---|
3353 | else
|
---|
3354 | rc = VERR_VDI_NOT_OPENED;
|
---|
3355 |
|
---|
3356 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
3357 | return rc;
|
---|
3358 | }
|
---|
3359 |
|
---|
3360 | static int vmdkGetUuid(void *pBackendData, PRTUUID pUuid)
|
---|
3361 | {
|
---|
3362 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3363 | int rc;
|
---|
3364 |
|
---|
3365 | Assert(pImage);
|
---|
3366 |
|
---|
3367 | if (pImage)
|
---|
3368 | {
|
---|
3369 | *pUuid = pImage->ImageUuid;
|
---|
3370 | rc = VINF_SUCCESS;
|
---|
3371 | }
|
---|
3372 | else
|
---|
3373 | rc = VERR_VDI_NOT_OPENED;
|
---|
3374 | LogFlow(("%s: returned %Vrc (%Vuuid)\n", __FUNCTION__, rc, pUuid));
|
---|
3375 | return rc;
|
---|
3376 | }
|
---|
3377 |
|
---|
3378 | static int vmdkSetUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
3379 | {
|
---|
3380 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3381 | int rc;
|
---|
3382 |
|
---|
3383 | LogFlow(("%s: %Vuuid\n", pUuid));
|
---|
3384 | Assert(pImage);
|
---|
3385 |
|
---|
3386 | if (pImage)
|
---|
3387 | {
|
---|
3388 | pImage->ImageUuid = *pUuid;
|
---|
3389 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
3390 | "ddb.uuid.image", pUuid);
|
---|
3391 | if (VBOX_FAILURE(rc))
|
---|
3392 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
3393 | rc = VINF_SUCCESS;
|
---|
3394 | }
|
---|
3395 | else
|
---|
3396 | rc = VERR_VDI_NOT_OPENED;
|
---|
3397 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
3398 | return rc;
|
---|
3399 | }
|
---|
3400 |
|
---|
3401 | static int vmdkGetModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
3402 | {
|
---|
3403 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3404 | int rc;
|
---|
3405 |
|
---|
3406 | Assert(pImage);
|
---|
3407 |
|
---|
3408 | if (pImage)
|
---|
3409 | {
|
---|
3410 | *pUuid = pImage->ModificationUuid;
|
---|
3411 | rc = VINF_SUCCESS;
|
---|
3412 | }
|
---|
3413 | else
|
---|
3414 | rc = VERR_VDI_NOT_OPENED;
|
---|
3415 | LogFlow(("%s: returned %Vrc (%Vuuid)\n", __FUNCTION__, rc, pUuid));
|
---|
3416 | return rc;
|
---|
3417 | }
|
---|
3418 |
|
---|
3419 | static int vmdkSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
3420 | {
|
---|
3421 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3422 | int rc;
|
---|
3423 |
|
---|
3424 | LogFlow(("%s: %Vuuid\n", pUuid));
|
---|
3425 | Assert(pImage);
|
---|
3426 |
|
---|
3427 | if (pImage)
|
---|
3428 | {
|
---|
3429 | pImage->ModificationUuid = *pUuid;
|
---|
3430 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
3431 | "ddb.uuid.modification", pUuid);
|
---|
3432 | if (VBOX_FAILURE(rc))
|
---|
3433 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing modification UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
3434 | rc = VINF_SUCCESS;
|
---|
3435 | }
|
---|
3436 | else
|
---|
3437 | rc = VERR_VDI_NOT_OPENED;
|
---|
3438 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
3439 | return rc;
|
---|
3440 | }
|
---|
3441 |
|
---|
3442 | static int vmdkGetParentUuid(void *pBackendData, PRTUUID pUuid)
|
---|
3443 | {
|
---|
3444 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3445 | int rc;
|
---|
3446 |
|
---|
3447 | Assert(pImage);
|
---|
3448 |
|
---|
3449 | if (pImage)
|
---|
3450 | {
|
---|
3451 | *pUuid = pImage->ParentUuid;
|
---|
3452 | rc = VINF_SUCCESS;
|
---|
3453 | }
|
---|
3454 | else
|
---|
3455 | rc = VERR_VDI_NOT_OPENED;
|
---|
3456 | LogFlow(("%s: returned %Vrc (%Vuuid)\n", __FUNCTION__, rc, pUuid));
|
---|
3457 | return rc;
|
---|
3458 | }
|
---|
3459 |
|
---|
3460 | static int vmdkSetParentUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
3461 | {
|
---|
3462 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
3463 | int rc;
|
---|
3464 |
|
---|
3465 | LogFlow(("%s: %Vuuid\n", pUuid));
|
---|
3466 | Assert(pImage);
|
---|
3467 |
|
---|
3468 | if (pImage)
|
---|
3469 | {
|
---|
3470 | pImage->ParentUuid = *pUuid;
|
---|
3471 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
3472 | "ddb.uuid.parent", pUuid);
|
---|
3473 | if (VBOX_FAILURE(rc))
|
---|
3474 | return vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: error storing parent image UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
3475 | rc = VINF_SUCCESS;
|
---|
3476 | }
|
---|
3477 | else
|
---|
3478 | rc = VERR_VDI_NOT_OPENED;
|
---|
3479 | LogFlow(("%s: returned %Vrc\n", __FUNCTION__, rc));
|
---|
3480 | return rc;
|
---|
3481 | }
|
---|
3482 |
|
---|
3483 |
|
---|
3484 | VBOXHDDBACKEND g_VmdkBackend =
|
---|
3485 | {
|
---|
3486 | /* pfnOpen */
|
---|
3487 | vmdkOpen,
|
---|
3488 | /* pfnCreate */
|
---|
3489 | vmdkCreate,
|
---|
3490 | /* pfnClose */
|
---|
3491 | vmdkClose,
|
---|
3492 | /* pfnRead */
|
---|
3493 | vmdkRead,
|
---|
3494 | /* pfnWrite */
|
---|
3495 | vmdkWrite,
|
---|
3496 | /* pfnFlush */
|
---|
3497 | vmdkFlush,
|
---|
3498 | /* pfnGetImageType */
|
---|
3499 | vmdkGetImageType,
|
---|
3500 | /* pfnGetSize */
|
---|
3501 | vmdkGetSize,
|
---|
3502 | /* pfnGetGeometry */
|
---|
3503 | vmdkGetGeometry,
|
---|
3504 | /* pfnSetGeometry */
|
---|
3505 | vmdkSetGeometry,
|
---|
3506 | /* pfnGetTranslation */
|
---|
3507 | vmdkGetTranslation,
|
---|
3508 | /* pfnSetTranslation */
|
---|
3509 | vmdkSetTranslation,
|
---|
3510 | /* pfnGetOpenFlags */
|
---|
3511 | vmdkGetOpenFlags,
|
---|
3512 | /* pfnSetOpenFlags */
|
---|
3513 | vmdkSetOpenFlags,
|
---|
3514 | /* pfnGetComment */
|
---|
3515 | vmdkGetComment,
|
---|
3516 | /* pfnSetComment */
|
---|
3517 | vmdkSetComment,
|
---|
3518 | /* pfnGetUuid */
|
---|
3519 | vmdkGetUuid,
|
---|
3520 | /* pfnSetUuid */
|
---|
3521 | vmdkSetUuid,
|
---|
3522 | /* pfnGetModificationUuid */
|
---|
3523 | vmdkGetModificationUuid,
|
---|
3524 | /* pfnSetModificationUuid */
|
---|
3525 | vmdkSetModificationUuid,
|
---|
3526 | /* pfnGetParentUuid */
|
---|
3527 | vmdkGetParentUuid,
|
---|
3528 | /* pfnSetParentUuid */
|
---|
3529 | vmdkSetParentUuid
|
---|
3530 | };
|
---|