VirtualBox

source: vbox/trunk/src/VBox/Storage/VMDK.cpp@ 73441

Last change on this file since 73441 was 73097, checked in by vboxsync, 6 years ago

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 255.3 KB
Line 
1/* $Id: VMDK.cpp 73097 2018-07-12 21:06:33Z vboxsync $ */
2/** @file
3 * VMDK disk image, core code.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD_VMDK
23#include <VBox/vd-plugin.h>
24#include <VBox/err.h>
25
26#include <VBox/log.h>
27#include <iprt/assert.h>
28#include <iprt/alloc.h>
29#include <iprt/uuid.h>
30#include <iprt/path.h>
31#include <iprt/string.h>
32#include <iprt/rand.h>
33#include <iprt/zip.h>
34#include <iprt/asm.h>
35
36#include "VDBackends.h"
37
38
39/*********************************************************************************************************************************
40* Constants And Macros, Structures and Typedefs *
41*********************************************************************************************************************************/
42
43/** Maximum encoded string size (including NUL) we allow for VMDK images.
44 * Deliberately not set high to avoid running out of descriptor space. */
45#define VMDK_ENCODED_COMMENT_MAX 1024
46
47/** VMDK descriptor DDB entry for PCHS cylinders. */
48#define VMDK_DDB_GEO_PCHS_CYLINDERS "ddb.geometry.cylinders"
49
50/** VMDK descriptor DDB entry for PCHS heads. */
51#define VMDK_DDB_GEO_PCHS_HEADS "ddb.geometry.heads"
52
53/** VMDK descriptor DDB entry for PCHS sectors. */
54#define VMDK_DDB_GEO_PCHS_SECTORS "ddb.geometry.sectors"
55
56/** VMDK descriptor DDB entry for LCHS cylinders. */
57#define VMDK_DDB_GEO_LCHS_CYLINDERS "ddb.geometry.biosCylinders"
58
59/** VMDK descriptor DDB entry for LCHS heads. */
60#define VMDK_DDB_GEO_LCHS_HEADS "ddb.geometry.biosHeads"
61
62/** VMDK descriptor DDB entry for LCHS sectors. */
63#define VMDK_DDB_GEO_LCHS_SECTORS "ddb.geometry.biosSectors"
64
65/** VMDK descriptor DDB entry for image UUID. */
66#define VMDK_DDB_IMAGE_UUID "ddb.uuid.image"
67
68/** VMDK descriptor DDB entry for image modification UUID. */
69#define VMDK_DDB_MODIFICATION_UUID "ddb.uuid.modification"
70
71/** VMDK descriptor DDB entry for parent image UUID. */
72#define VMDK_DDB_PARENT_UUID "ddb.uuid.parent"
73
74/** VMDK descriptor DDB entry for parent image modification UUID. */
75#define VMDK_DDB_PARENT_MODIFICATION_UUID "ddb.uuid.parentmodification"
76
77/** No compression for streamOptimized files. */
78#define VMDK_COMPRESSION_NONE 0
79
80/** Deflate compression for streamOptimized files. */
81#define VMDK_COMPRESSION_DEFLATE 1
82
83/** Marker that the actual GD value is stored in the footer. */
84#define VMDK_GD_AT_END 0xffffffffffffffffULL
85
86/** Marker for end-of-stream in streamOptimized images. */
87#define VMDK_MARKER_EOS 0
88
89/** Marker for grain table block in streamOptimized images. */
90#define VMDK_MARKER_GT 1
91
92/** Marker for grain directory block in streamOptimized images. */
93#define VMDK_MARKER_GD 2
94
95/** Marker for footer in streamOptimized images. */
96#define VMDK_MARKER_FOOTER 3
97
98/** Marker for unknown purpose in streamOptimized images.
99 * Shows up in very recent images created by vSphere, but only sporadically.
100 * They "forgot" to document that one in the VMDK specification. */
101#define VMDK_MARKER_UNSPECIFIED 4
102
103/** Dummy marker for "don't check the marker value". */
104#define VMDK_MARKER_IGNORE 0xffffffffU
105
106/**
107 * Magic number for hosted images created by VMware Workstation 4, VMware
108 * Workstation 5, VMware Server or VMware Player. Not necessarily sparse.
109 */
110#define VMDK_SPARSE_MAGICNUMBER 0x564d444b /* 'V' 'M' 'D' 'K' */
111
112/**
113 * VMDK hosted binary extent header. The "Sparse" is a total misnomer, as
114 * this header is also used for monolithic flat images.
115 */
116#pragma pack(1)
117typedef struct SparseExtentHeader
118{
119 uint32_t magicNumber;
120 uint32_t version;
121 uint32_t flags;
122 uint64_t capacity;
123 uint64_t grainSize;
124 uint64_t descriptorOffset;
125 uint64_t descriptorSize;
126 uint32_t numGTEsPerGT;
127 uint64_t rgdOffset;
128 uint64_t gdOffset;
129 uint64_t overHead;
130 bool uncleanShutdown;
131 char singleEndLineChar;
132 char nonEndLineChar;
133 char doubleEndLineChar1;
134 char doubleEndLineChar2;
135 uint16_t compressAlgorithm;
136 uint8_t pad[433];
137} SparseExtentHeader;
138#pragma pack()
139
140/** VMDK capacity for a single chunk when 2G splitting is turned on. Should be
141 * divisible by the default grain size (64K) */
142#define VMDK_2G_SPLIT_SIZE (2047 * 1024 * 1024)
143
144/** VMDK streamOptimized file format marker. The type field may or may not
145 * be actually valid, but there's always data to read there. */
146#pragma pack(1)
147typedef struct VMDKMARKER
148{
149 uint64_t uSector;
150 uint32_t cbSize;
151 uint32_t uType;
152} VMDKMARKER, *PVMDKMARKER;
153#pragma pack()
154
155
156/** Convert sector number/size to byte offset/size. */
157#define VMDK_SECTOR2BYTE(u) ((uint64_t)(u) << 9)
158
159/** Convert byte offset/size to sector number/size. */
160#define VMDK_BYTE2SECTOR(u) ((u) >> 9)
161
162/**
163 * VMDK extent type.
164 */
165typedef enum VMDKETYPE
166{
167 /** Hosted sparse extent. */
168 VMDKETYPE_HOSTED_SPARSE = 1,
169 /** Flat extent. */
170 VMDKETYPE_FLAT,
171 /** Zero extent. */
172 VMDKETYPE_ZERO,
173 /** VMFS extent, used by ESX. */
174 VMDKETYPE_VMFS
175} VMDKETYPE, *PVMDKETYPE;
176
177/**
178 * VMDK access type for a extent.
179 */
180typedef enum VMDKACCESS
181{
182 /** No access allowed. */
183 VMDKACCESS_NOACCESS = 0,
184 /** Read-only access. */
185 VMDKACCESS_READONLY,
186 /** Read-write access. */
187 VMDKACCESS_READWRITE
188} VMDKACCESS, *PVMDKACCESS;
189
190/** Forward declaration for PVMDKIMAGE. */
191typedef struct VMDKIMAGE *PVMDKIMAGE;
192
193/**
194 * Extents files entry. Used for opening a particular file only once.
195 */
196typedef struct VMDKFILE
197{
198 /** Pointer to filename. Local copy. */
199 const char *pszFilename;
200 /** File open flags for consistency checking. */
201 unsigned fOpen;
202 /** Handle for sync/async file abstraction.*/
203 PVDIOSTORAGE pStorage;
204 /** Reference counter. */
205 unsigned uReferences;
206 /** Flag whether the file should be deleted on last close. */
207 bool fDelete;
208 /** Pointer to the image we belong to (for debugging purposes). */
209 PVMDKIMAGE pImage;
210 /** Pointer to next file descriptor. */
211 struct VMDKFILE *pNext;
212 /** Pointer to the previous file descriptor. */
213 struct VMDKFILE *pPrev;
214} VMDKFILE, *PVMDKFILE;
215
216/**
217 * VMDK extent data structure.
218 */
219typedef struct VMDKEXTENT
220{
221 /** File handle. */
222 PVMDKFILE pFile;
223 /** Base name of the image extent. */
224 const char *pszBasename;
225 /** Full name of the image extent. */
226 const char *pszFullname;
227 /** Number of sectors in this extent. */
228 uint64_t cSectors;
229 /** Number of sectors per block (grain in VMDK speak). */
230 uint64_t cSectorsPerGrain;
231 /** Starting sector number of descriptor. */
232 uint64_t uDescriptorSector;
233 /** Size of descriptor in sectors. */
234 uint64_t cDescriptorSectors;
235 /** Starting sector number of grain directory. */
236 uint64_t uSectorGD;
237 /** Starting sector number of redundant grain directory. */
238 uint64_t uSectorRGD;
239 /** Total number of metadata sectors. */
240 uint64_t cOverheadSectors;
241 /** Nominal size (i.e. as described by the descriptor) of this extent. */
242 uint64_t cNominalSectors;
243 /** Sector offset (i.e. as described by the descriptor) of this extent. */
244 uint64_t uSectorOffset;
245 /** Number of entries in a grain table. */
246 uint32_t cGTEntries;
247 /** Number of sectors reachable via a grain directory entry. */
248 uint32_t cSectorsPerGDE;
249 /** Number of entries in the grain directory. */
250 uint32_t cGDEntries;
251 /** Pointer to the next free sector. Legacy information. Do not use. */
252 uint32_t uFreeSector;
253 /** Number of this extent in the list of images. */
254 uint32_t uExtent;
255 /** Pointer to the descriptor (NULL if no descriptor in this extent). */
256 char *pDescData;
257 /** Pointer to the grain directory. */
258 uint32_t *pGD;
259 /** Pointer to the redundant grain directory. */
260 uint32_t *pRGD;
261 /** VMDK version of this extent. 1=1.0/1.1 */
262 uint32_t uVersion;
263 /** Type of this extent. */
264 VMDKETYPE enmType;
265 /** Access to this extent. */
266 VMDKACCESS enmAccess;
267 /** Flag whether this extent is marked as unclean. */
268 bool fUncleanShutdown;
269 /** Flag whether the metadata in the extent header needs to be updated. */
270 bool fMetaDirty;
271 /** Flag whether there is a footer in this extent. */
272 bool fFooter;
273 /** Compression type for this extent. */
274 uint16_t uCompression;
275 /** Append position for writing new grain. Only for sparse extents. */
276 uint64_t uAppendPosition;
277 /** Last grain which was accessed. Only for streamOptimized extents. */
278 uint32_t uLastGrainAccess;
279 /** Starting sector corresponding to the grain buffer. */
280 uint32_t uGrainSectorAbs;
281 /** Grain number corresponding to the grain buffer. */
282 uint32_t uGrain;
283 /** Actual size of the compressed data, only valid for reading. */
284 uint32_t cbGrainStreamRead;
285 /** Size of compressed grain buffer for streamOptimized extents. */
286 size_t cbCompGrain;
287 /** Compressed grain buffer for streamOptimized extents, with marker. */
288 void *pvCompGrain;
289 /** Decompressed grain buffer for streamOptimized extents. */
290 void *pvGrain;
291 /** Reference to the image in which this extent is used. Do not use this
292 * on a regular basis to avoid passing pImage references to functions
293 * explicitly. */
294 struct VMDKIMAGE *pImage;
295} VMDKEXTENT, *PVMDKEXTENT;
296
297/**
298 * Grain table cache size. Allocated per image.
299 */
300#define VMDK_GT_CACHE_SIZE 256
301
302/**
303 * Grain table block size. Smaller than an actual grain table block to allow
304 * more grain table blocks to be cached without having to allocate excessive
305 * amounts of memory for the cache.
306 */
307#define VMDK_GT_CACHELINE_SIZE 128
308
309
310/**
311 * Maximum number of lines in a descriptor file. Not worth the effort of
312 * making it variable. Descriptor files are generally very short (~20 lines),
313 * with the exception of sparse files split in 2G chunks, which need for the
314 * maximum size (almost 2T) exactly 1025 lines for the disk database.
315 */
316#define VMDK_DESCRIPTOR_LINES_MAX 1100U
317
318/**
319 * Parsed descriptor information. Allows easy access and update of the
320 * descriptor (whether separate file or not). Free form text files suck.
321 */
322typedef struct VMDKDESCRIPTOR
323{
324 /** Line number of first entry of the disk descriptor. */
325 unsigned uFirstDesc;
326 /** Line number of first entry in the extent description. */
327 unsigned uFirstExtent;
328 /** Line number of first disk database entry. */
329 unsigned uFirstDDB;
330 /** Total number of lines. */
331 unsigned cLines;
332 /** Total amount of memory available for the descriptor. */
333 size_t cbDescAlloc;
334 /** Set if descriptor has been changed and not yet written to disk. */
335 bool fDirty;
336 /** Array of pointers to the data in the descriptor. */
337 char *aLines[VMDK_DESCRIPTOR_LINES_MAX];
338 /** Array of line indices pointing to the next non-comment line. */
339 unsigned aNextLines[VMDK_DESCRIPTOR_LINES_MAX];
340} VMDKDESCRIPTOR, *PVMDKDESCRIPTOR;
341
342
343/**
344 * Cache entry for translating extent/sector to a sector number in that
345 * extent.
346 */
347typedef struct VMDKGTCACHEENTRY
348{
349 /** Extent number for which this entry is valid. */
350 uint32_t uExtent;
351 /** GT data block number. */
352 uint64_t uGTBlock;
353 /** Data part of the cache entry. */
354 uint32_t aGTData[VMDK_GT_CACHELINE_SIZE];
355} VMDKGTCACHEENTRY, *PVMDKGTCACHEENTRY;
356
357/**
358 * Cache data structure for blocks of grain table entries. For now this is a
359 * fixed size direct mapping cache, but this should be adapted to the size of
360 * the sparse image and maybe converted to a set-associative cache. The
361 * implementation below implements a write-through cache with write allocate.
362 */
363typedef struct VMDKGTCACHE
364{
365 /** Cache entries. */
366 VMDKGTCACHEENTRY aGTCache[VMDK_GT_CACHE_SIZE];
367 /** Number of cache entries (currently unused). */
368 unsigned cEntries;
369} VMDKGTCACHE, *PVMDKGTCACHE;
370
371/**
372 * Complete VMDK image data structure. Mainly a collection of extents and a few
373 * extra global data fields.
374 */
375typedef struct VMDKIMAGE
376{
377 /** Image name. */
378 const char *pszFilename;
379 /** Descriptor file if applicable. */
380 PVMDKFILE pFile;
381
382 /** Pointer to the per-disk VD interface list. */
383 PVDINTERFACE pVDIfsDisk;
384 /** Pointer to the per-image VD interface list. */
385 PVDINTERFACE pVDIfsImage;
386
387 /** Error interface. */
388 PVDINTERFACEERROR pIfError;
389 /** I/O interface. */
390 PVDINTERFACEIOINT pIfIo;
391
392
393 /** Pointer to the image extents. */
394 PVMDKEXTENT pExtents;
395 /** Number of image extents. */
396 unsigned cExtents;
397 /** Pointer to the files list, for opening a file referenced multiple
398 * times only once (happens mainly with raw partition access). */
399 PVMDKFILE pFiles;
400
401 /**
402 * Pointer to an array of segment entries for async I/O.
403 * This is an optimization because the task number to submit is not known
404 * and allocating/freeing an array in the read/write functions every time
405 * is too expensive.
406 */
407 PPDMDATASEG paSegments;
408 /** Entries available in the segments array. */
409 unsigned cSegments;
410
411 /** Open flags passed by VBoxHD layer. */
412 unsigned uOpenFlags;
413 /** Image flags defined during creation or determined during open. */
414 unsigned uImageFlags;
415 /** Total size of the image. */
416 uint64_t cbSize;
417 /** Physical geometry of this image. */
418 VDGEOMETRY PCHSGeometry;
419 /** Logical geometry of this image. */
420 VDGEOMETRY LCHSGeometry;
421 /** Image UUID. */
422 RTUUID ImageUuid;
423 /** Image modification UUID. */
424 RTUUID ModificationUuid;
425 /** Parent image UUID. */
426 RTUUID ParentUuid;
427 /** Parent image modification UUID. */
428 RTUUID ParentModificationUuid;
429
430 /** Pointer to grain table cache, if this image contains sparse extents. */
431 PVMDKGTCACHE pGTCache;
432 /** Pointer to the descriptor (NULL if no separate descriptor file). */
433 char *pDescData;
434 /** Allocation size of the descriptor file. */
435 size_t cbDescAlloc;
436 /** Parsed descriptor file content. */
437 VMDKDESCRIPTOR Descriptor;
438 /** The static region list. */
439 VDREGIONLIST RegionList;
440} VMDKIMAGE;
441
442
443/** State for the input/output callout of the inflate reader/deflate writer. */
444typedef struct VMDKCOMPRESSIO
445{
446 /* Image this operation relates to. */
447 PVMDKIMAGE pImage;
448 /* Current read position. */
449 ssize_t iOffset;
450 /* Size of the compressed grain buffer (available data). */
451 size_t cbCompGrain;
452 /* Pointer to the compressed grain buffer. */
453 void *pvCompGrain;
454} VMDKCOMPRESSIO;
455
456
457/** Tracks async grain allocation. */
458typedef struct VMDKGRAINALLOCASYNC
459{
460 /** Flag whether the allocation failed. */
461 bool fIoErr;
462 /** Current number of transfers pending.
463 * If reached 0 and there is an error the old state is restored. */
464 unsigned cIoXfersPending;
465 /** Sector number */
466 uint64_t uSector;
467 /** Flag whether the grain table needs to be updated. */
468 bool fGTUpdateNeeded;
469 /** Extent the allocation happens. */
470 PVMDKEXTENT pExtent;
471 /** Position of the new grain, required for the grain table update. */
472 uint64_t uGrainOffset;
473 /** Grain table sector. */
474 uint64_t uGTSector;
475 /** Backup grain table sector. */
476 uint64_t uRGTSector;
477} VMDKGRAINALLOCASYNC, *PVMDKGRAINALLOCASYNC;
478
479/**
480 * State information for vmdkRename() and helpers.
481 */
482typedef struct VMDKRENAMESTATE
483{
484 /** Array of old filenames. */
485 char **apszOldName;
486 /** Array of new filenames. */
487 char **apszNewName;
488 /** Array of new lines in the extent descriptor. */
489 char **apszNewLines;
490 /** Name of the old descriptor file if not a sparse image. */
491 char *pszOldDescName;
492 /** Flag whether we called vmdkFreeImage(). */
493 bool fImageFreed;
494 /** Flag whther the descriptor is embedded in the image (sparse) or
495 * in a separate file. */
496 bool fEmbeddedDesc;
497 /** Number of extents in the image. */
498 unsigned cExtents;
499 /** New base filename. */
500 char *pszNewBaseName;
501 /** The old base filename. */
502 char *pszOldBaseName;
503 /** New full filename. */
504 char *pszNewFullName;
505 /** Old full filename. */
506 char *pszOldFullName;
507 /** The old image name. */
508 const char *pszOldImageName;
509 /** Copy of the original VMDK descriptor. */
510 VMDKDESCRIPTOR DescriptorCopy;
511 /** Copy of the extent state for sparse images. */
512 VMDKEXTENT ExtentCopy;
513} VMDKRENAMESTATE;
514/** Pointer to a VMDK rename state. */
515typedef VMDKRENAMESTATE *PVMDKRENAMESTATE;
516
517
518/*********************************************************************************************************************************
519* Static Variables *
520*********************************************************************************************************************************/
521
522/** NULL-terminated array of supported file extensions. */
523static const VDFILEEXTENSION s_aVmdkFileExtensions[] =
524{
525 {"vmdk", VDTYPE_HDD},
526 {NULL, VDTYPE_INVALID}
527};
528
529
530/*********************************************************************************************************************************
531* Internal Functions *
532*********************************************************************************************************************************/
533
534static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent);
535static int vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
536 bool fDelete);
537
538static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents);
539static int vmdkFlushImage(PVMDKIMAGE pImage, PVDIOCTX pIoCtx);
540static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment);
541static int vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete);
542
543static DECLCALLBACK(int) vmdkAllocGrainComplete(void *pBackendData, PVDIOCTX pIoCtx,
544 void *pvUser, int rcReq);
545
546/**
547 * Internal: open a file (using a file descriptor cache to ensure each file
548 * is only opened once - anything else can cause locking problems).
549 */
550static int vmdkFileOpen(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile,
551 const char *pszFilename, uint32_t fOpen)
552{
553 int rc = VINF_SUCCESS;
554 PVMDKFILE pVmdkFile;
555
556 for (pVmdkFile = pImage->pFiles;
557 pVmdkFile != NULL;
558 pVmdkFile = pVmdkFile->pNext)
559 {
560 if (!strcmp(pszFilename, pVmdkFile->pszFilename))
561 {
562 Assert(fOpen == pVmdkFile->fOpen);
563 pVmdkFile->uReferences++;
564
565 *ppVmdkFile = pVmdkFile;
566
567 return rc;
568 }
569 }
570
571 /* If we get here, there's no matching entry in the cache. */
572 pVmdkFile = (PVMDKFILE)RTMemAllocZ(sizeof(VMDKFILE));
573 if (!pVmdkFile)
574 {
575 *ppVmdkFile = NULL;
576 return VERR_NO_MEMORY;
577 }
578
579 pVmdkFile->pszFilename = RTStrDup(pszFilename);
580 if (!pVmdkFile->pszFilename)
581 {
582 RTMemFree(pVmdkFile);
583 *ppVmdkFile = NULL;
584 return VERR_NO_MEMORY;
585 }
586 pVmdkFile->fOpen = fOpen;
587
588 rc = vdIfIoIntFileOpen(pImage->pIfIo, pszFilename, fOpen,
589 &pVmdkFile->pStorage);
590 if (RT_SUCCESS(rc))
591 {
592 pVmdkFile->uReferences = 1;
593 pVmdkFile->pImage = pImage;
594 pVmdkFile->pNext = pImage->pFiles;
595 if (pImage->pFiles)
596 pImage->pFiles->pPrev = pVmdkFile;
597 pImage->pFiles = pVmdkFile;
598 *ppVmdkFile = pVmdkFile;
599 }
600 else
601 {
602 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
603 RTMemFree(pVmdkFile);
604 *ppVmdkFile = NULL;
605 }
606
607 return rc;
608}
609
610/**
611 * Internal: close a file, updating the file descriptor cache.
612 */
613static int vmdkFileClose(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile, bool fDelete)
614{
615 int rc = VINF_SUCCESS;
616 PVMDKFILE pVmdkFile = *ppVmdkFile;
617
618 AssertPtr(pVmdkFile);
619
620 pVmdkFile->fDelete |= fDelete;
621 Assert(pVmdkFile->uReferences);
622 pVmdkFile->uReferences--;
623 if (pVmdkFile->uReferences == 0)
624 {
625 PVMDKFILE pPrev;
626 PVMDKFILE pNext;
627
628 /* Unchain the element from the list. */
629 pPrev = pVmdkFile->pPrev;
630 pNext = pVmdkFile->pNext;
631
632 if (pNext)
633 pNext->pPrev = pPrev;
634 if (pPrev)
635 pPrev->pNext = pNext;
636 else
637 pImage->pFiles = pNext;
638
639 rc = vdIfIoIntFileClose(pImage->pIfIo, pVmdkFile->pStorage);
640 if (pVmdkFile->fDelete)
641 {
642 int rc2 = vdIfIoIntFileDelete(pImage->pIfIo, pVmdkFile->pszFilename);
643 if (RT_SUCCESS(rc))
644 rc = rc2;
645 }
646 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
647 RTMemFree(pVmdkFile);
648 }
649
650 *ppVmdkFile = NULL;
651 return rc;
652}
653
654/*#define VMDK_USE_BLOCK_DECOMP_API - test and enable */
655#ifndef VMDK_USE_BLOCK_DECOMP_API
656static DECLCALLBACK(int) vmdkFileInflateHelper(void *pvUser, void *pvBuf, size_t cbBuf, size_t *pcbBuf)
657{
658 VMDKCOMPRESSIO *pInflateState = (VMDKCOMPRESSIO *)pvUser;
659 size_t cbInjected = 0;
660
661 Assert(cbBuf);
662 if (pInflateState->iOffset < 0)
663 {
664 *(uint8_t *)pvBuf = RTZIPTYPE_ZLIB;
665 pvBuf = (uint8_t *)pvBuf + 1;
666 cbBuf--;
667 cbInjected = 1;
668 pInflateState->iOffset = RT_UOFFSETOF(VMDKMARKER, uType);
669 }
670 if (!cbBuf)
671 {
672 if (pcbBuf)
673 *pcbBuf = cbInjected;
674 return VINF_SUCCESS;
675 }
676 cbBuf = RT_MIN(cbBuf, pInflateState->cbCompGrain - pInflateState->iOffset);
677 memcpy(pvBuf,
678 (uint8_t *)pInflateState->pvCompGrain + pInflateState->iOffset,
679 cbBuf);
680 pInflateState->iOffset += cbBuf;
681 Assert(pcbBuf);
682 *pcbBuf = cbBuf + cbInjected;
683 return VINF_SUCCESS;
684}
685#endif
686
687/**
688 * Internal: read from a file and inflate the compressed data,
689 * distinguishing between async and normal operation
690 */
691DECLINLINE(int) vmdkFileInflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
692 uint64_t uOffset, void *pvBuf,
693 size_t cbToRead, const void *pcvMarker,
694 uint64_t *puLBA, uint32_t *pcbMarkerData)
695{
696 int rc;
697#ifndef VMDK_USE_BLOCK_DECOMP_API
698 PRTZIPDECOMP pZip = NULL;
699#endif
700 VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
701 size_t cbCompSize, cbActuallyRead;
702
703 if (!pcvMarker)
704 {
705 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
706 uOffset, pMarker, RT_UOFFSETOF(VMDKMARKER, uType));
707 if (RT_FAILURE(rc))
708 return rc;
709 }
710 else
711 {
712 memcpy(pMarker, pcvMarker, RT_UOFFSETOF(VMDKMARKER, uType));
713 /* pcvMarker endianness has already been partially transformed, fix it */
714 pMarker->uSector = RT_H2LE_U64(pMarker->uSector);
715 pMarker->cbSize = RT_H2LE_U32(pMarker->cbSize);
716 }
717
718 cbCompSize = RT_LE2H_U32(pMarker->cbSize);
719 if (cbCompSize == 0)
720 {
721 AssertMsgFailed(("VMDK: corrupted marker\n"));
722 return VERR_VD_VMDK_INVALID_FORMAT;
723 }
724
725 /* Sanity check - the expansion ratio should be much less than 2. */
726 Assert(cbCompSize < 2 * cbToRead);
727 if (cbCompSize >= 2 * cbToRead)
728 return VERR_VD_VMDK_INVALID_FORMAT;
729
730 /* Compressed grain marker. Data follows immediately. */
731 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
732 uOffset + RT_UOFFSETOF(VMDKMARKER, uType),
733 (uint8_t *)pExtent->pvCompGrain
734 + RT_UOFFSETOF(VMDKMARKER, uType),
735 RT_ALIGN_Z( cbCompSize
736 + RT_UOFFSETOF(VMDKMARKER, uType),
737 512)
738 - RT_UOFFSETOF(VMDKMARKER, uType));
739
740 if (puLBA)
741 *puLBA = RT_LE2H_U64(pMarker->uSector);
742 if (pcbMarkerData)
743 *pcbMarkerData = RT_ALIGN( cbCompSize
744 + RT_UOFFSETOF(VMDKMARKER, uType),
745 512);
746
747#ifdef VMDK_USE_BLOCK_DECOMP_API
748 rc = RTZipBlockDecompress(RTZIPTYPE_ZLIB, 0 /*fFlags*/,
749 pExtent->pvCompGrain, cbCompSize + RT_UOFFSETOF(VMDKMARKER, uType), NULL,
750 pvBuf, cbToRead, &cbActuallyRead);
751#else
752 VMDKCOMPRESSIO InflateState;
753 InflateState.pImage = pImage;
754 InflateState.iOffset = -1;
755 InflateState.cbCompGrain = cbCompSize + RT_UOFFSETOF(VMDKMARKER, uType);
756 InflateState.pvCompGrain = pExtent->pvCompGrain;
757
758 rc = RTZipDecompCreate(&pZip, &InflateState, vmdkFileInflateHelper);
759 if (RT_FAILURE(rc))
760 return rc;
761 rc = RTZipDecompress(pZip, pvBuf, cbToRead, &cbActuallyRead);
762 RTZipDecompDestroy(pZip);
763#endif /* !VMDK_USE_BLOCK_DECOMP_API */
764 if (RT_FAILURE(rc))
765 {
766 if (rc == VERR_ZIP_CORRUPTED)
767 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: Compressed image is corrupted '%s'"), pExtent->pszFullname);
768 return rc;
769 }
770 if (cbActuallyRead != cbToRead)
771 rc = VERR_VD_VMDK_INVALID_FORMAT;
772 return rc;
773}
774
775static DECLCALLBACK(int) vmdkFileDeflateHelper(void *pvUser, const void *pvBuf, size_t cbBuf)
776{
777 VMDKCOMPRESSIO *pDeflateState = (VMDKCOMPRESSIO *)pvUser;
778
779 Assert(cbBuf);
780 if (pDeflateState->iOffset < 0)
781 {
782 pvBuf = (const uint8_t *)pvBuf + 1;
783 cbBuf--;
784 pDeflateState->iOffset = RT_UOFFSETOF(VMDKMARKER, uType);
785 }
786 if (!cbBuf)
787 return VINF_SUCCESS;
788 if (pDeflateState->iOffset + cbBuf > pDeflateState->cbCompGrain)
789 return VERR_BUFFER_OVERFLOW;
790 memcpy((uint8_t *)pDeflateState->pvCompGrain + pDeflateState->iOffset,
791 pvBuf, cbBuf);
792 pDeflateState->iOffset += cbBuf;
793 return VINF_SUCCESS;
794}
795
796/**
797 * Internal: deflate the uncompressed data and write to a file,
798 * distinguishing between async and normal operation
799 */
800DECLINLINE(int) vmdkFileDeflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
801 uint64_t uOffset, const void *pvBuf,
802 size_t cbToWrite, uint64_t uLBA,
803 uint32_t *pcbMarkerData)
804{
805 int rc;
806 PRTZIPCOMP pZip = NULL;
807 VMDKCOMPRESSIO DeflateState;
808
809 DeflateState.pImage = pImage;
810 DeflateState.iOffset = -1;
811 DeflateState.cbCompGrain = pExtent->cbCompGrain;
812 DeflateState.pvCompGrain = pExtent->pvCompGrain;
813
814 rc = RTZipCompCreate(&pZip, &DeflateState, vmdkFileDeflateHelper,
815 RTZIPTYPE_ZLIB, RTZIPLEVEL_DEFAULT);
816 if (RT_FAILURE(rc))
817 return rc;
818 rc = RTZipCompress(pZip, pvBuf, cbToWrite);
819 if (RT_SUCCESS(rc))
820 rc = RTZipCompFinish(pZip);
821 RTZipCompDestroy(pZip);
822 if (RT_SUCCESS(rc))
823 {
824 Assert( DeflateState.iOffset > 0
825 && (size_t)DeflateState.iOffset <= DeflateState.cbCompGrain);
826
827 /* pad with zeroes to get to a full sector size */
828 uint32_t uSize = DeflateState.iOffset;
829 if (uSize % 512)
830 {
831 uint32_t uSizeAlign = RT_ALIGN(uSize, 512);
832 memset((uint8_t *)pExtent->pvCompGrain + uSize, '\0',
833 uSizeAlign - uSize);
834 uSize = uSizeAlign;
835 }
836
837 if (pcbMarkerData)
838 *pcbMarkerData = uSize;
839
840 /* Compressed grain marker. Data follows immediately. */
841 VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
842 pMarker->uSector = RT_H2LE_U64(uLBA);
843 pMarker->cbSize = RT_H2LE_U32( DeflateState.iOffset
844 - RT_UOFFSETOF(VMDKMARKER, uType));
845 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
846 uOffset, pMarker, uSize);
847 if (RT_FAILURE(rc))
848 return rc;
849 }
850 return rc;
851}
852
853
854/**
855 * Internal: check if all files are closed, prevent leaking resources.
856 */
857static int vmdkFileCheckAllClose(PVMDKIMAGE pImage)
858{
859 int rc = VINF_SUCCESS, rc2;
860 PVMDKFILE pVmdkFile;
861
862 Assert(pImage->pFiles == NULL);
863 for (pVmdkFile = pImage->pFiles;
864 pVmdkFile != NULL;
865 pVmdkFile = pVmdkFile->pNext)
866 {
867 LogRel(("VMDK: leaking reference to file \"%s\"\n",
868 pVmdkFile->pszFilename));
869 pImage->pFiles = pVmdkFile->pNext;
870
871 rc2 = vmdkFileClose(pImage, &pVmdkFile, pVmdkFile->fDelete);
872
873 if (RT_SUCCESS(rc))
874 rc = rc2;
875 }
876 return rc;
877}
878
879/**
880 * Internal: truncate a string (at a UTF8 code point boundary) and encode the
881 * critical non-ASCII characters.
882 */
883static char *vmdkEncodeString(const char *psz)
884{
885 char szEnc[VMDK_ENCODED_COMMENT_MAX + 3];
886 char *pszDst = szEnc;
887
888 AssertPtr(psz);
889
890 for (; *psz; psz = RTStrNextCp(psz))
891 {
892 char *pszDstPrev = pszDst;
893 RTUNICP Cp = RTStrGetCp(psz);
894 if (Cp == '\\')
895 {
896 pszDst = RTStrPutCp(pszDst, Cp);
897 pszDst = RTStrPutCp(pszDst, Cp);
898 }
899 else if (Cp == '\n')
900 {
901 pszDst = RTStrPutCp(pszDst, '\\');
902 pszDst = RTStrPutCp(pszDst, 'n');
903 }
904 else if (Cp == '\r')
905 {
906 pszDst = RTStrPutCp(pszDst, '\\');
907 pszDst = RTStrPutCp(pszDst, 'r');
908 }
909 else
910 pszDst = RTStrPutCp(pszDst, Cp);
911 if (pszDst - szEnc >= VMDK_ENCODED_COMMENT_MAX - 1)
912 {
913 pszDst = pszDstPrev;
914 break;
915 }
916 }
917 *pszDst = '\0';
918 return RTStrDup(szEnc);
919}
920
921/**
922 * Internal: decode a string and store it into the specified string.
923 */
924static int vmdkDecodeString(const char *pszEncoded, char *psz, size_t cb)
925{
926 int rc = VINF_SUCCESS;
927 char szBuf[4];
928
929 if (!cb)
930 return VERR_BUFFER_OVERFLOW;
931
932 AssertPtr(psz);
933
934 for (; *pszEncoded; pszEncoded = RTStrNextCp(pszEncoded))
935 {
936 char *pszDst = szBuf;
937 RTUNICP Cp = RTStrGetCp(pszEncoded);
938 if (Cp == '\\')
939 {
940 pszEncoded = RTStrNextCp(pszEncoded);
941 RTUNICP CpQ = RTStrGetCp(pszEncoded);
942 if (CpQ == 'n')
943 RTStrPutCp(pszDst, '\n');
944 else if (CpQ == 'r')
945 RTStrPutCp(pszDst, '\r');
946 else if (CpQ == '\0')
947 {
948 rc = VERR_VD_VMDK_INVALID_HEADER;
949 break;
950 }
951 else
952 RTStrPutCp(pszDst, CpQ);
953 }
954 else
955 pszDst = RTStrPutCp(pszDst, Cp);
956
957 /* Need to leave space for terminating NUL. */
958 if ((size_t)(pszDst - szBuf) + 1 >= cb)
959 {
960 rc = VERR_BUFFER_OVERFLOW;
961 break;
962 }
963 memcpy(psz, szBuf, pszDst - szBuf);
964 psz += pszDst - szBuf;
965 }
966 *psz = '\0';
967 return rc;
968}
969
970/**
971 * Internal: free all buffers associated with grain directories.
972 */
973static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent)
974{
975 if (pExtent->pGD)
976 {
977 RTMemFree(pExtent->pGD);
978 pExtent->pGD = NULL;
979 }
980 if (pExtent->pRGD)
981 {
982 RTMemFree(pExtent->pRGD);
983 pExtent->pRGD = NULL;
984 }
985}
986
987/**
988 * Internal: allocate the compressed/uncompressed buffers for streamOptimized
989 * images.
990 */
991static int vmdkAllocStreamBuffers(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
992{
993 int rc = VINF_SUCCESS;
994
995 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
996 {
997 /* streamOptimized extents need a compressed grain buffer, which must
998 * be big enough to hold uncompressible data (which needs ~8 bytes
999 * more than the uncompressed data), the marker and padding. */
1000 pExtent->cbCompGrain = RT_ALIGN_Z( VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
1001 + 8 + sizeof(VMDKMARKER), 512);
1002 pExtent->pvCompGrain = RTMemAlloc(pExtent->cbCompGrain);
1003 if (RT_LIKELY(pExtent->pvCompGrain))
1004 {
1005 /* streamOptimized extents need a decompressed grain buffer. */
1006 pExtent->pvGrain = RTMemAlloc(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1007 if (!pExtent->pvGrain)
1008 rc = VERR_NO_MEMORY;
1009 }
1010 else
1011 rc = VERR_NO_MEMORY;
1012 }
1013
1014 if (RT_FAILURE(rc))
1015 vmdkFreeStreamBuffers(pExtent);
1016 return rc;
1017}
1018
1019/**
1020 * Internal: allocate all buffers associated with grain directories.
1021 */
1022static int vmdkAllocGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1023{
1024 RT_NOREF1(pImage);
1025 int rc = VINF_SUCCESS;
1026 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1027
1028 pExtent->pGD = (uint32_t *)RTMemAllocZ(cbGD);
1029 if (RT_LIKELY(pExtent->pGD))
1030 {
1031 if (pExtent->uSectorRGD)
1032 {
1033 pExtent->pRGD = (uint32_t *)RTMemAllocZ(cbGD);
1034 if (RT_UNLIKELY(!pExtent->pRGD))
1035 rc = VERR_NO_MEMORY;
1036 }
1037 }
1038 else
1039 rc = VERR_NO_MEMORY;
1040
1041 if (RT_FAILURE(rc))
1042 vmdkFreeGrainDirectory(pExtent);
1043 return rc;
1044}
1045
1046/**
1047 * Converts the grain directory from little to host endianess.
1048 *
1049 * @returns nothing.
1050 * @param pGD The grain directory.
1051 * @param cGDEntries Number of entries in the grain directory to convert.
1052 */
1053DECLINLINE(void) vmdkGrainDirectoryConvToHost(uint32_t *pGD, uint32_t cGDEntries)
1054{
1055 uint32_t *pGDTmp = pGD;
1056
1057 for (uint32_t i = 0; i < cGDEntries; i++, pGDTmp++)
1058 *pGDTmp = RT_LE2H_U32(*pGDTmp);
1059}
1060
1061/**
1062 * Read the grain directory and allocated grain tables verifying them against
1063 * their back up copies if available.
1064 *
1065 * @returns VBox status code.
1066 * @param pImage Image instance data.
1067 * @param pExtent The VMDK extent.
1068 */
1069static int vmdkReadGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1070{
1071 int rc = VINF_SUCCESS;
1072 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1073
1074 AssertReturn(( pExtent->enmType == VMDKETYPE_HOSTED_SPARSE
1075 && pExtent->uSectorGD != VMDK_GD_AT_END
1076 && pExtent->uSectorRGD != VMDK_GD_AT_END), VERR_INTERNAL_ERROR);
1077
1078 rc = vmdkAllocGrainDirectory(pImage, pExtent);
1079 if (RT_SUCCESS(rc))
1080 {
1081 /* The VMDK 1.1 spec seems to talk about compressed grain directories,
1082 * but in reality they are not compressed. */
1083 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1084 VMDK_SECTOR2BYTE(pExtent->uSectorGD),
1085 pExtent->pGD, cbGD);
1086 if (RT_SUCCESS(rc))
1087 {
1088 vmdkGrainDirectoryConvToHost(pExtent->pGD, pExtent->cGDEntries);
1089
1090 if ( pExtent->uSectorRGD
1091 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS))
1092 {
1093 /* The VMDK 1.1 spec seems to talk about compressed grain directories,
1094 * but in reality they are not compressed. */
1095 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1096 VMDK_SECTOR2BYTE(pExtent->uSectorRGD),
1097 pExtent->pRGD, cbGD);
1098 if (RT_SUCCESS(rc))
1099 {
1100 vmdkGrainDirectoryConvToHost(pExtent->pRGD, pExtent->cGDEntries);
1101
1102 /* Check grain table and redundant grain table for consistency. */
1103 size_t cbGT = pExtent->cGTEntries * sizeof(uint32_t);
1104 size_t cbGTBuffers = cbGT; /* Start with space for one GT. */
1105 size_t cbGTBuffersMax = _1M;
1106
1107 uint32_t *pTmpGT1 = (uint32_t *)RTMemAlloc(cbGTBuffers);
1108 uint32_t *pTmpGT2 = (uint32_t *)RTMemAlloc(cbGTBuffers);
1109
1110 if ( !pTmpGT1
1111 || !pTmpGT2)
1112 rc = VERR_NO_MEMORY;
1113
1114 size_t i = 0;
1115 uint32_t *pGDTmp = pExtent->pGD;
1116 uint32_t *pRGDTmp = pExtent->pRGD;
1117
1118 /* Loop through all entries. */
1119 while (i < pExtent->cGDEntries)
1120 {
1121 uint32_t uGTStart = *pGDTmp;
1122 uint32_t uRGTStart = *pRGDTmp;
1123 size_t cbGTRead = cbGT;
1124
1125 /* If no grain table is allocated skip the entry. */
1126 if (*pGDTmp == 0 && *pRGDTmp == 0)
1127 {
1128 i++;
1129 continue;
1130 }
1131
1132 if (*pGDTmp == 0 || *pRGDTmp == 0 || *pGDTmp == *pRGDTmp)
1133 {
1134 /* Just one grain directory entry refers to a not yet allocated
1135 * grain table or both grain directory copies refer to the same
1136 * grain table. Not allowed. */
1137 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1138 N_("VMDK: inconsistent references to grain directory in '%s'"), pExtent->pszFullname);
1139 break;
1140 }
1141
1142 i++;
1143 pGDTmp++;
1144 pRGDTmp++;
1145
1146 /*
1147 * Read a few tables at once if adjacent to decrease the number
1148 * of I/O requests. Read at maximum 1MB at once.
1149 */
1150 while ( i < pExtent->cGDEntries
1151 && cbGTRead < cbGTBuffersMax)
1152 {
1153 /* If no grain table is allocated skip the entry. */
1154 if (*pGDTmp == 0 && *pRGDTmp == 0)
1155 {
1156 i++;
1157 continue;
1158 }
1159
1160 if (*pGDTmp == 0 || *pRGDTmp == 0 || *pGDTmp == *pRGDTmp)
1161 {
1162 /* Just one grain directory entry refers to a not yet allocated
1163 * grain table or both grain directory copies refer to the same
1164 * grain table. Not allowed. */
1165 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1166 N_("VMDK: inconsistent references to grain directory in '%s'"), pExtent->pszFullname);
1167 break;
1168 }
1169
1170 /* Check that the start offsets are adjacent.*/
1171 if ( VMDK_SECTOR2BYTE(uGTStart) + cbGTRead != VMDK_SECTOR2BYTE(*pGDTmp)
1172 || VMDK_SECTOR2BYTE(uRGTStart) + cbGTRead != VMDK_SECTOR2BYTE(*pRGDTmp))
1173 break;
1174
1175 i++;
1176 pGDTmp++;
1177 pRGDTmp++;
1178 cbGTRead += cbGT;
1179 }
1180
1181 /* Increase buffers if required. */
1182 if ( RT_SUCCESS(rc)
1183 && cbGTBuffers < cbGTRead)
1184 {
1185 uint32_t *pTmp;
1186 pTmp = (uint32_t *)RTMemRealloc(pTmpGT1, cbGTRead);
1187 if (pTmp)
1188 {
1189 pTmpGT1 = pTmp;
1190 pTmp = (uint32_t *)RTMemRealloc(pTmpGT2, cbGTRead);
1191 if (pTmp)
1192 pTmpGT2 = pTmp;
1193 else
1194 rc = VERR_NO_MEMORY;
1195 }
1196 else
1197 rc = VERR_NO_MEMORY;
1198
1199 if (rc == VERR_NO_MEMORY)
1200 {
1201 /* Reset to the old values. */
1202 rc = VINF_SUCCESS;
1203 i -= cbGTRead / cbGT;
1204 cbGTRead = cbGT;
1205
1206 /* Don't try to increase the buffer again in the next run. */
1207 cbGTBuffersMax = cbGTBuffers;
1208 }
1209 }
1210
1211 if (RT_SUCCESS(rc))
1212 {
1213 /* The VMDK 1.1 spec seems to talk about compressed grain tables,
1214 * but in reality they are not compressed. */
1215 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1216 VMDK_SECTOR2BYTE(uGTStart),
1217 pTmpGT1, cbGTRead);
1218 if (RT_FAILURE(rc))
1219 {
1220 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1221 N_("VMDK: error reading grain table in '%s'"), pExtent->pszFullname);
1222 break;
1223 }
1224 /* The VMDK 1.1 spec seems to talk about compressed grain tables,
1225 * but in reality they are not compressed. */
1226 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1227 VMDK_SECTOR2BYTE(uRGTStart),
1228 pTmpGT2, cbGTRead);
1229 if (RT_FAILURE(rc))
1230 {
1231 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1232 N_("VMDK: error reading backup grain table in '%s'"), pExtent->pszFullname);
1233 break;
1234 }
1235 if (memcmp(pTmpGT1, pTmpGT2, cbGTRead))
1236 {
1237 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1238 N_("VMDK: inconsistency between grain table and backup grain table in '%s'"), pExtent->pszFullname);
1239 break;
1240 }
1241 }
1242 } /* while (i < pExtent->cGDEntries) */
1243
1244 /** @todo figure out what to do for unclean VMDKs. */
1245 if (pTmpGT1)
1246 RTMemFree(pTmpGT1);
1247 if (pTmpGT2)
1248 RTMemFree(pTmpGT2);
1249 }
1250 else
1251 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1252 N_("VMDK: could not read redundant grain directory in '%s'"), pExtent->pszFullname);
1253 }
1254 }
1255 else
1256 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1257 N_("VMDK: could not read grain directory in '%s': %Rrc"), pExtent->pszFullname, rc);
1258 }
1259
1260 if (RT_FAILURE(rc))
1261 vmdkFreeGrainDirectory(pExtent);
1262 return rc;
1263}
1264
1265/**
1266 * Creates a new grain directory for the given extent at the given start sector.
1267 *
1268 * @returns VBox status code.
1269 * @param pImage Image instance data.
1270 * @param pExtent The VMDK extent.
1271 * @param uStartSector Where the grain directory should be stored in the image.
1272 * @param fPreAlloc Flag whether to pre allocate the grain tables at this point.
1273 */
1274static int vmdkCreateGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
1275 uint64_t uStartSector, bool fPreAlloc)
1276{
1277 int rc = VINF_SUCCESS;
1278 unsigned i;
1279 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1280 size_t cbGDRounded = RT_ALIGN_64(cbGD, 512);
1281 size_t cbGTRounded;
1282 uint64_t cbOverhead;
1283
1284 if (fPreAlloc)
1285 {
1286 cbGTRounded = RT_ALIGN_64(pExtent->cGDEntries * pExtent->cGTEntries * sizeof(uint32_t), 512);
1287 cbOverhead = VMDK_SECTOR2BYTE(uStartSector) + cbGDRounded + cbGTRounded;
1288 }
1289 else
1290 {
1291 /* Use a dummy start sector for layout computation. */
1292 if (uStartSector == VMDK_GD_AT_END)
1293 uStartSector = 1;
1294 cbGTRounded = 0;
1295 cbOverhead = VMDK_SECTOR2BYTE(uStartSector) + cbGDRounded;
1296 }
1297
1298 /* For streamOptimized extents there is only one grain directory,
1299 * and for all others take redundant grain directory into account. */
1300 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
1301 {
1302 cbOverhead = RT_ALIGN_64(cbOverhead,
1303 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1304 }
1305 else
1306 {
1307 cbOverhead += cbGDRounded + cbGTRounded;
1308 cbOverhead = RT_ALIGN_64(cbOverhead,
1309 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1310 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pExtent->pFile->pStorage, cbOverhead);
1311 }
1312
1313 if (RT_SUCCESS(rc))
1314 {
1315 pExtent->uAppendPosition = cbOverhead;
1316 pExtent->cOverheadSectors = VMDK_BYTE2SECTOR(cbOverhead);
1317
1318 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
1319 {
1320 pExtent->uSectorRGD = 0;
1321 pExtent->uSectorGD = uStartSector;
1322 }
1323 else
1324 {
1325 pExtent->uSectorRGD = uStartSector;
1326 pExtent->uSectorGD = uStartSector + VMDK_BYTE2SECTOR(cbGDRounded + cbGTRounded);
1327 }
1328
1329 rc = vmdkAllocStreamBuffers(pImage, pExtent);
1330 if (RT_SUCCESS(rc))
1331 {
1332 rc = vmdkAllocGrainDirectory(pImage, pExtent);
1333 if ( RT_SUCCESS(rc)
1334 && fPreAlloc)
1335 {
1336 uint32_t uGTSectorLE;
1337 uint64_t uOffsetSectors;
1338
1339 if (pExtent->pRGD)
1340 {
1341 uOffsetSectors = pExtent->uSectorRGD + VMDK_BYTE2SECTOR(cbGDRounded);
1342 for (i = 0; i < pExtent->cGDEntries; i++)
1343 {
1344 pExtent->pRGD[i] = uOffsetSectors;
1345 uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
1346 /* Write the redundant grain directory entry to disk. */
1347 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
1348 VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + i * sizeof(uGTSectorLE),
1349 &uGTSectorLE, sizeof(uGTSectorLE));
1350 if (RT_FAILURE(rc))
1351 {
1352 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write new redundant grain directory entry in '%s'"), pExtent->pszFullname);
1353 break;
1354 }
1355 uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
1356 }
1357 }
1358
1359 if (RT_SUCCESS(rc))
1360 {
1361 uOffsetSectors = pExtent->uSectorGD + VMDK_BYTE2SECTOR(cbGDRounded);
1362 for (i = 0; i < pExtent->cGDEntries; i++)
1363 {
1364 pExtent->pGD[i] = uOffsetSectors;
1365 uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
1366 /* Write the grain directory entry to disk. */
1367 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
1368 VMDK_SECTOR2BYTE(pExtent->uSectorGD) + i * sizeof(uGTSectorLE),
1369 &uGTSectorLE, sizeof(uGTSectorLE));
1370 if (RT_FAILURE(rc))
1371 {
1372 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write new grain directory entry in '%s'"), pExtent->pszFullname);
1373 break;
1374 }
1375 uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
1376 }
1377 }
1378 }
1379 }
1380 }
1381
1382 if (RT_FAILURE(rc))
1383 vmdkFreeGrainDirectory(pExtent);
1384 return rc;
1385}
1386
1387/**
1388 * Unquotes the given string returning the result in a separate buffer.
1389 *
1390 * @returns VBox status code.
1391 * @param pImage The VMDK image state.
1392 * @param pszStr The string to unquote.
1393 * @param ppszUnquoted Where to store the return value, use RTMemTmpFree to
1394 * free.
1395 * @param ppszNext Where to store the pointer to any character following
1396 * the quoted value, optional.
1397 */
1398static int vmdkStringUnquote(PVMDKIMAGE pImage, const char *pszStr,
1399 char **ppszUnquoted, char **ppszNext)
1400{
1401 const char *pszStart = pszStr;
1402 char *pszQ;
1403 char *pszUnquoted;
1404
1405 /* Skip over whitespace. */
1406 while (*pszStr == ' ' || *pszStr == '\t')
1407 pszStr++;
1408
1409 if (*pszStr != '"')
1410 {
1411 pszQ = (char *)pszStr;
1412 while (*pszQ && *pszQ != ' ' && *pszQ != '\t')
1413 pszQ++;
1414 }
1415 else
1416 {
1417 pszStr++;
1418 pszQ = (char *)strchr(pszStr, '"');
1419 if (pszQ == NULL)
1420 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrectly quoted value in descriptor in '%s' (raw value %s)"),
1421 pImage->pszFilename, pszStart);
1422 }
1423
1424 pszUnquoted = (char *)RTMemTmpAlloc(pszQ - pszStr + 1);
1425 if (!pszUnquoted)
1426 return VERR_NO_MEMORY;
1427 memcpy(pszUnquoted, pszStr, pszQ - pszStr);
1428 pszUnquoted[pszQ - pszStr] = '\0';
1429 *ppszUnquoted = pszUnquoted;
1430 if (ppszNext)
1431 *ppszNext = pszQ + 1;
1432 return VINF_SUCCESS;
1433}
1434
1435static int vmdkDescInitStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1436 const char *pszLine)
1437{
1438 char *pEnd = pDescriptor->aLines[pDescriptor->cLines];
1439 ssize_t cbDiff = strlen(pszLine) + 1;
1440
1441 if ( pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1
1442 && pEnd - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
1443 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1444
1445 memcpy(pEnd, pszLine, cbDiff);
1446 pDescriptor->cLines++;
1447 pDescriptor->aLines[pDescriptor->cLines] = pEnd + cbDiff;
1448 pDescriptor->fDirty = true;
1449
1450 return VINF_SUCCESS;
1451}
1452
1453static bool vmdkDescGetStr(PVMDKDESCRIPTOR pDescriptor, unsigned uStart,
1454 const char *pszKey, const char **ppszValue)
1455{
1456 size_t cbKey = strlen(pszKey);
1457 const char *pszValue;
1458
1459 while (uStart != 0)
1460 {
1461 if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
1462 {
1463 /* Key matches, check for a '=' (preceded by whitespace). */
1464 pszValue = pDescriptor->aLines[uStart] + cbKey;
1465 while (*pszValue == ' ' || *pszValue == '\t')
1466 pszValue++;
1467 if (*pszValue == '=')
1468 {
1469 *ppszValue = pszValue + 1;
1470 break;
1471 }
1472 }
1473 uStart = pDescriptor->aNextLines[uStart];
1474 }
1475 return !!uStart;
1476}
1477
1478static int vmdkDescSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1479 unsigned uStart,
1480 const char *pszKey, const char *pszValue)
1481{
1482 char *pszTmp = NULL; /* (MSC naturally cannot figure this isn't used uninitialized) */
1483 size_t cbKey = strlen(pszKey);
1484 unsigned uLast = 0;
1485
1486 while (uStart != 0)
1487 {
1488 if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
1489 {
1490 /* Key matches, check for a '=' (preceded by whitespace). */
1491 pszTmp = pDescriptor->aLines[uStart] + cbKey;
1492 while (*pszTmp == ' ' || *pszTmp == '\t')
1493 pszTmp++;
1494 if (*pszTmp == '=')
1495 {
1496 pszTmp++;
1497 /** @todo r=bird: Doesn't skipping trailing blanks here just cause unecessary
1498 * bloat and potentially out of space error? */
1499 while (*pszTmp == ' ' || *pszTmp == '\t')
1500 pszTmp++;
1501 break;
1502 }
1503 }
1504 if (!pDescriptor->aNextLines[uStart])
1505 uLast = uStart;
1506 uStart = pDescriptor->aNextLines[uStart];
1507 }
1508 if (uStart)
1509 {
1510 if (pszValue)
1511 {
1512 /* Key already exists, replace existing value. */
1513 size_t cbOldVal = strlen(pszTmp);
1514 size_t cbNewVal = strlen(pszValue);
1515 ssize_t cbDiff = cbNewVal - cbOldVal;
1516 /* Check for buffer overflow. */
1517 if ( pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[0]
1518 > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
1519 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1520
1521 memmove(pszTmp + cbNewVal, pszTmp + cbOldVal,
1522 pDescriptor->aLines[pDescriptor->cLines] - pszTmp - cbOldVal);
1523 memcpy(pszTmp, pszValue, cbNewVal + 1);
1524 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1525 pDescriptor->aLines[i] += cbDiff;
1526 }
1527 else
1528 {
1529 memmove(pDescriptor->aLines[uStart], pDescriptor->aLines[uStart+1],
1530 pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uStart+1] + 1);
1531 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1532 {
1533 pDescriptor->aLines[i-1] = pDescriptor->aLines[i];
1534 if (pDescriptor->aNextLines[i])
1535 pDescriptor->aNextLines[i-1] = pDescriptor->aNextLines[i] - 1;
1536 else
1537 pDescriptor->aNextLines[i-1] = 0;
1538 }
1539 pDescriptor->cLines--;
1540 /* Adjust starting line numbers of following descriptor sections. */
1541 if (uStart < pDescriptor->uFirstExtent)
1542 pDescriptor->uFirstExtent--;
1543 if (uStart < pDescriptor->uFirstDDB)
1544 pDescriptor->uFirstDDB--;
1545 }
1546 }
1547 else
1548 {
1549 /* Key doesn't exist, append after the last entry in this category. */
1550 if (!pszValue)
1551 {
1552 /* Key doesn't exist, and it should be removed. Simply a no-op. */
1553 return VINF_SUCCESS;
1554 }
1555 cbKey = strlen(pszKey);
1556 size_t cbValue = strlen(pszValue);
1557 ssize_t cbDiff = cbKey + 1 + cbValue + 1;
1558 /* Check for buffer overflow. */
1559 if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
1560 || ( pDescriptor->aLines[pDescriptor->cLines]
1561 - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
1562 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1563 for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
1564 {
1565 pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
1566 if (pDescriptor->aNextLines[i - 1])
1567 pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
1568 else
1569 pDescriptor->aNextLines[i] = 0;
1570 }
1571 uStart = uLast + 1;
1572 pDescriptor->aNextLines[uLast] = uStart;
1573 pDescriptor->aNextLines[uStart] = 0;
1574 pDescriptor->cLines++;
1575 pszTmp = pDescriptor->aLines[uStart];
1576 memmove(pszTmp + cbDiff, pszTmp,
1577 pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
1578 memcpy(pDescriptor->aLines[uStart], pszKey, cbKey);
1579 pDescriptor->aLines[uStart][cbKey] = '=';
1580 memcpy(pDescriptor->aLines[uStart] + cbKey + 1, pszValue, cbValue + 1);
1581 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1582 pDescriptor->aLines[i] += cbDiff;
1583
1584 /* Adjust starting line numbers of following descriptor sections. */
1585 if (uStart <= pDescriptor->uFirstExtent)
1586 pDescriptor->uFirstExtent++;
1587 if (uStart <= pDescriptor->uFirstDDB)
1588 pDescriptor->uFirstDDB++;
1589 }
1590 pDescriptor->fDirty = true;
1591 return VINF_SUCCESS;
1592}
1593
1594static int vmdkDescBaseGetU32(PVMDKDESCRIPTOR pDescriptor, const char *pszKey,
1595 uint32_t *puValue)
1596{
1597 const char *pszValue;
1598
1599 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey,
1600 &pszValue))
1601 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1602 return RTStrToUInt32Ex(pszValue, NULL, 10, puValue);
1603}
1604
1605/**
1606 * Returns the value of the given key as a string allocating the necessary memory.
1607 *
1608 * @returns VBox status code.
1609 * @retval VERR_VD_VMDK_VALUE_NOT_FOUND if the value could not be found.
1610 * @param pImage The VMDK image state.
1611 * @param pDescriptor The descriptor to fetch the value from.
1612 * @param pszKey The key to get the value from.
1613 * @param ppszValue Where to store the return value, use RTMemTmpFree to
1614 * free.
1615 */
1616static int vmdkDescBaseGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1617 const char *pszKey, char **ppszValue)
1618{
1619 const char *pszValue;
1620 char *pszValueUnquoted;
1621
1622 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey,
1623 &pszValue))
1624 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1625 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1626 if (RT_FAILURE(rc))
1627 return rc;
1628 *ppszValue = pszValueUnquoted;
1629 return rc;
1630}
1631
1632static int vmdkDescBaseSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1633 const char *pszKey, const char *pszValue)
1634{
1635 char *pszValueQuoted;
1636
1637 RTStrAPrintf(&pszValueQuoted, "\"%s\"", pszValue);
1638 if (!pszValueQuoted)
1639 return VERR_NO_STR_MEMORY;
1640 int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc, pszKey,
1641 pszValueQuoted);
1642 RTStrFree(pszValueQuoted);
1643 return rc;
1644}
1645
1646static void vmdkDescExtRemoveDummy(PVMDKIMAGE pImage,
1647 PVMDKDESCRIPTOR pDescriptor)
1648{
1649 RT_NOREF1(pImage);
1650 unsigned uEntry = pDescriptor->uFirstExtent;
1651 ssize_t cbDiff;
1652
1653 if (!uEntry)
1654 return;
1655
1656 cbDiff = strlen(pDescriptor->aLines[uEntry]) + 1;
1657 /* Move everything including \0 in the entry marking the end of buffer. */
1658 memmove(pDescriptor->aLines[uEntry], pDescriptor->aLines[uEntry + 1],
1659 pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uEntry + 1] + 1);
1660 for (unsigned i = uEntry + 1; i <= pDescriptor->cLines; i++)
1661 {
1662 pDescriptor->aLines[i - 1] = pDescriptor->aLines[i] - cbDiff;
1663 if (pDescriptor->aNextLines[i])
1664 pDescriptor->aNextLines[i - 1] = pDescriptor->aNextLines[i] - 1;
1665 else
1666 pDescriptor->aNextLines[i - 1] = 0;
1667 }
1668 pDescriptor->cLines--;
1669 if (pDescriptor->uFirstDDB)
1670 pDescriptor->uFirstDDB--;
1671
1672 return;
1673}
1674
1675static int vmdkDescExtInsert(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1676 VMDKACCESS enmAccess, uint64_t cNominalSectors,
1677 VMDKETYPE enmType, const char *pszBasename,
1678 uint64_t uSectorOffset)
1679{
1680 static const char *apszAccess[] = { "NOACCESS", "RDONLY", "RW" };
1681 static const char *apszType[] = { "", "SPARSE", "FLAT", "ZERO", "VMFS" };
1682 char *pszTmp;
1683 unsigned uStart = pDescriptor->uFirstExtent, uLast = 0;
1684 char szExt[1024];
1685 ssize_t cbDiff;
1686
1687 Assert((unsigned)enmAccess < RT_ELEMENTS(apszAccess));
1688 Assert((unsigned)enmType < RT_ELEMENTS(apszType));
1689
1690 /* Find last entry in extent description. */
1691 while (uStart)
1692 {
1693 if (!pDescriptor->aNextLines[uStart])
1694 uLast = uStart;
1695 uStart = pDescriptor->aNextLines[uStart];
1696 }
1697
1698 if (enmType == VMDKETYPE_ZERO)
1699 {
1700 RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s ", apszAccess[enmAccess],
1701 cNominalSectors, apszType[enmType]);
1702 }
1703 else if (enmType == VMDKETYPE_FLAT)
1704 {
1705 RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\" %llu",
1706 apszAccess[enmAccess], cNominalSectors,
1707 apszType[enmType], pszBasename, uSectorOffset);
1708 }
1709 else
1710 {
1711 RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\"",
1712 apszAccess[enmAccess], cNominalSectors,
1713 apszType[enmType], pszBasename);
1714 }
1715 cbDiff = strlen(szExt) + 1;
1716
1717 /* Check for buffer overflow. */
1718 if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
1719 || ( pDescriptor->aLines[pDescriptor->cLines]
1720 - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
1721 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1722
1723 for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
1724 {
1725 pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
1726 if (pDescriptor->aNextLines[i - 1])
1727 pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
1728 else
1729 pDescriptor->aNextLines[i] = 0;
1730 }
1731 uStart = uLast + 1;
1732 pDescriptor->aNextLines[uLast] = uStart;
1733 pDescriptor->aNextLines[uStart] = 0;
1734 pDescriptor->cLines++;
1735 pszTmp = pDescriptor->aLines[uStart];
1736 memmove(pszTmp + cbDiff, pszTmp,
1737 pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
1738 memcpy(pDescriptor->aLines[uStart], szExt, cbDiff);
1739 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1740 pDescriptor->aLines[i] += cbDiff;
1741
1742 /* Adjust starting line numbers of following descriptor sections. */
1743 if (uStart <= pDescriptor->uFirstDDB)
1744 pDescriptor->uFirstDDB++;
1745
1746 pDescriptor->fDirty = true;
1747 return VINF_SUCCESS;
1748}
1749
1750/**
1751 * Returns the value of the given key from the DDB as a string allocating
1752 * the necessary memory.
1753 *
1754 * @returns VBox status code.
1755 * @retval VERR_VD_VMDK_VALUE_NOT_FOUND if the value could not be found.
1756 * @param pImage The VMDK image state.
1757 * @param pDescriptor The descriptor to fetch the value from.
1758 * @param pszKey The key to get the value from.
1759 * @param ppszValue Where to store the return value, use RTMemTmpFree to
1760 * free.
1761 */
1762static int vmdkDescDDBGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1763 const char *pszKey, char **ppszValue)
1764{
1765 const char *pszValue;
1766 char *pszValueUnquoted;
1767
1768 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
1769 &pszValue))
1770 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1771 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1772 if (RT_FAILURE(rc))
1773 return rc;
1774 *ppszValue = pszValueUnquoted;
1775 return rc;
1776}
1777
1778static int vmdkDescDDBGetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1779 const char *pszKey, uint32_t *puValue)
1780{
1781 const char *pszValue;
1782 char *pszValueUnquoted;
1783
1784 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
1785 &pszValue))
1786 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1787 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1788 if (RT_FAILURE(rc))
1789 return rc;
1790 rc = RTStrToUInt32Ex(pszValueUnquoted, NULL, 10, puValue);
1791 RTMemTmpFree(pszValueUnquoted);
1792 return rc;
1793}
1794
1795static int vmdkDescDDBGetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1796 const char *pszKey, PRTUUID pUuid)
1797{
1798 const char *pszValue;
1799 char *pszValueUnquoted;
1800
1801 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
1802 &pszValue))
1803 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1804 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1805 if (RT_FAILURE(rc))
1806 return rc;
1807 rc = RTUuidFromStr(pUuid, pszValueUnquoted);
1808 RTMemTmpFree(pszValueUnquoted);
1809 return rc;
1810}
1811
1812static int vmdkDescDDBSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1813 const char *pszKey, const char *pszVal)
1814{
1815 int rc;
1816 char *pszValQuoted;
1817
1818 if (pszVal)
1819 {
1820 RTStrAPrintf(&pszValQuoted, "\"%s\"", pszVal);
1821 if (!pszValQuoted)
1822 return VERR_NO_STR_MEMORY;
1823 }
1824 else
1825 pszValQuoted = NULL;
1826 rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
1827 pszValQuoted);
1828 if (pszValQuoted)
1829 RTStrFree(pszValQuoted);
1830 return rc;
1831}
1832
1833static int vmdkDescDDBSetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1834 const char *pszKey, PCRTUUID pUuid)
1835{
1836 char *pszUuid;
1837
1838 RTStrAPrintf(&pszUuid, "\"%RTuuid\"", pUuid);
1839 if (!pszUuid)
1840 return VERR_NO_STR_MEMORY;
1841 int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
1842 pszUuid);
1843 RTStrFree(pszUuid);
1844 return rc;
1845}
1846
1847static int vmdkDescDDBSetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1848 const char *pszKey, uint32_t uValue)
1849{
1850 char *pszValue;
1851
1852 RTStrAPrintf(&pszValue, "\"%d\"", uValue);
1853 if (!pszValue)
1854 return VERR_NO_STR_MEMORY;
1855 int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
1856 pszValue);
1857 RTStrFree(pszValue);
1858 return rc;
1859}
1860
1861/**
1862 * Splits the descriptor data into individual lines checking for correct line
1863 * endings and descriptor size.
1864 *
1865 * @returns VBox status code.
1866 * @param pImage The image instance.
1867 * @param pDesc The descriptor.
1868 * @param pszTmp The raw descriptor data from the image.
1869 */
1870static int vmdkDescSplitLines(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDesc, char *pszTmp)
1871{
1872 unsigned cLine = 0;
1873 int rc = VINF_SUCCESS;
1874
1875 while ( RT_SUCCESS(rc)
1876 && *pszTmp != '\0')
1877 {
1878 pDesc->aLines[cLine++] = pszTmp;
1879 if (cLine >= VMDK_DESCRIPTOR_LINES_MAX)
1880 {
1881 vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1882 rc = VERR_VD_VMDK_INVALID_HEADER;
1883 break;
1884 }
1885
1886 while (*pszTmp != '\0' && *pszTmp != '\n')
1887 {
1888 if (*pszTmp == '\r')
1889 {
1890 if (*(pszTmp + 1) != '\n')
1891 {
1892 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: unsupported end of line in descriptor in '%s'"), pImage->pszFilename);
1893 break;
1894 }
1895 else
1896 {
1897 /* Get rid of CR character. */
1898 *pszTmp = '\0';
1899 }
1900 }
1901 pszTmp++;
1902 }
1903
1904 if (RT_FAILURE(rc))
1905 break;
1906
1907 /* Get rid of LF character. */
1908 if (*pszTmp == '\n')
1909 {
1910 *pszTmp = '\0';
1911 pszTmp++;
1912 }
1913 }
1914
1915 if (RT_SUCCESS(rc))
1916 {
1917 pDesc->cLines = cLine;
1918 /* Pointer right after the end of the used part of the buffer. */
1919 pDesc->aLines[cLine] = pszTmp;
1920 }
1921
1922 return rc;
1923}
1924
1925static int vmdkPreprocessDescriptor(PVMDKIMAGE pImage, char *pDescData,
1926 size_t cbDescData, PVMDKDESCRIPTOR pDescriptor)
1927{
1928 pDescriptor->cbDescAlloc = cbDescData;
1929 int rc = vmdkDescSplitLines(pImage, pDescriptor, pDescData);
1930 if (RT_SUCCESS(rc))
1931 {
1932 if ( strcmp(pDescriptor->aLines[0], "# Disk DescriptorFile")
1933 && strcmp(pDescriptor->aLines[0], "# Disk Descriptor File")
1934 && strcmp(pDescriptor->aLines[0], "#Disk Descriptor File")
1935 && strcmp(pDescriptor->aLines[0], "#Disk DescriptorFile"))
1936 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1937 N_("VMDK: descriptor does not start as expected in '%s'"), pImage->pszFilename);
1938 else
1939 {
1940 unsigned uLastNonEmptyLine = 0;
1941
1942 /* Initialize those, because we need to be able to reopen an image. */
1943 pDescriptor->uFirstDesc = 0;
1944 pDescriptor->uFirstExtent = 0;
1945 pDescriptor->uFirstDDB = 0;
1946 for (unsigned i = 0; i < pDescriptor->cLines; i++)
1947 {
1948 if (*pDescriptor->aLines[i] != '#' && *pDescriptor->aLines[i] != '\0')
1949 {
1950 if ( !strncmp(pDescriptor->aLines[i], "RW", 2)
1951 || !strncmp(pDescriptor->aLines[i], "RDONLY", 6)
1952 || !strncmp(pDescriptor->aLines[i], "NOACCESS", 8) )
1953 {
1954 /* An extent descriptor. */
1955 if (!pDescriptor->uFirstDesc || pDescriptor->uFirstDDB)
1956 {
1957 /* Incorrect ordering of entries. */
1958 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1959 N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
1960 break;
1961 }
1962 if (!pDescriptor->uFirstExtent)
1963 {
1964 pDescriptor->uFirstExtent = i;
1965 uLastNonEmptyLine = 0;
1966 }
1967 }
1968 else if (!strncmp(pDescriptor->aLines[i], "ddb.", 4))
1969 {
1970 /* A disk database entry. */
1971 if (!pDescriptor->uFirstDesc || !pDescriptor->uFirstExtent)
1972 {
1973 /* Incorrect ordering of entries. */
1974 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1975 N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
1976 break;
1977 }
1978 if (!pDescriptor->uFirstDDB)
1979 {
1980 pDescriptor->uFirstDDB = i;
1981 uLastNonEmptyLine = 0;
1982 }
1983 }
1984 else
1985 {
1986 /* A normal entry. */
1987 if (pDescriptor->uFirstExtent || pDescriptor->uFirstDDB)
1988 {
1989 /* Incorrect ordering of entries. */
1990 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1991 N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
1992 break;
1993 }
1994 if (!pDescriptor->uFirstDesc)
1995 {
1996 pDescriptor->uFirstDesc = i;
1997 uLastNonEmptyLine = 0;
1998 }
1999 }
2000 if (uLastNonEmptyLine)
2001 pDescriptor->aNextLines[uLastNonEmptyLine] = i;
2002 uLastNonEmptyLine = i;
2003 }
2004 }
2005 }
2006 }
2007
2008 return rc;
2009}
2010
2011static int vmdkDescSetPCHSGeometry(PVMDKIMAGE pImage,
2012 PCVDGEOMETRY pPCHSGeometry)
2013{
2014 int rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2015 VMDK_DDB_GEO_PCHS_CYLINDERS,
2016 pPCHSGeometry->cCylinders);
2017 if (RT_FAILURE(rc))
2018 return rc;
2019 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2020 VMDK_DDB_GEO_PCHS_HEADS,
2021 pPCHSGeometry->cHeads);
2022 if (RT_FAILURE(rc))
2023 return rc;
2024 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2025 VMDK_DDB_GEO_PCHS_SECTORS,
2026 pPCHSGeometry->cSectors);
2027 return rc;
2028}
2029
2030static int vmdkDescSetLCHSGeometry(PVMDKIMAGE pImage,
2031 PCVDGEOMETRY pLCHSGeometry)
2032{
2033 int rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2034 VMDK_DDB_GEO_LCHS_CYLINDERS,
2035 pLCHSGeometry->cCylinders);
2036 if (RT_FAILURE(rc))
2037 return rc;
2038 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2039 VMDK_DDB_GEO_LCHS_HEADS,
2040
2041 pLCHSGeometry->cHeads);
2042 if (RT_FAILURE(rc))
2043 return rc;
2044 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2045 VMDK_DDB_GEO_LCHS_SECTORS,
2046 pLCHSGeometry->cSectors);
2047 return rc;
2048}
2049
2050static int vmdkCreateDescriptor(PVMDKIMAGE pImage, char *pDescData,
2051 size_t cbDescData, PVMDKDESCRIPTOR pDescriptor)
2052{
2053 pDescriptor->uFirstDesc = 0;
2054 pDescriptor->uFirstExtent = 0;
2055 pDescriptor->uFirstDDB = 0;
2056 pDescriptor->cLines = 0;
2057 pDescriptor->cbDescAlloc = cbDescData;
2058 pDescriptor->fDirty = false;
2059 pDescriptor->aLines[pDescriptor->cLines] = pDescData;
2060 memset(pDescriptor->aNextLines, '\0', sizeof(pDescriptor->aNextLines));
2061
2062 int rc = vmdkDescInitStr(pImage, pDescriptor, "# Disk DescriptorFile");
2063 if (RT_SUCCESS(rc))
2064 rc = vmdkDescInitStr(pImage, pDescriptor, "version=1");
2065 if (RT_SUCCESS(rc))
2066 {
2067 pDescriptor->uFirstDesc = pDescriptor->cLines - 1;
2068 rc = vmdkDescInitStr(pImage, pDescriptor, "");
2069 }
2070 if (RT_SUCCESS(rc))
2071 rc = vmdkDescInitStr(pImage, pDescriptor, "# Extent description");
2072 if (RT_SUCCESS(rc))
2073 rc = vmdkDescInitStr(pImage, pDescriptor, "NOACCESS 0 ZERO ");
2074 if (RT_SUCCESS(rc))
2075 {
2076 pDescriptor->uFirstExtent = pDescriptor->cLines - 1;
2077 rc = vmdkDescInitStr(pImage, pDescriptor, "");
2078 }
2079 if (RT_SUCCESS(rc))
2080 {
2081 /* The trailing space is created by VMware, too. */
2082 rc = vmdkDescInitStr(pImage, pDescriptor, "# The disk Data Base ");
2083 }
2084 if (RT_SUCCESS(rc))
2085 rc = vmdkDescInitStr(pImage, pDescriptor, "#DDB");
2086 if (RT_SUCCESS(rc))
2087 rc = vmdkDescInitStr(pImage, pDescriptor, "");
2088 if (RT_SUCCESS(rc))
2089 rc = vmdkDescInitStr(pImage, pDescriptor, "ddb.virtualHWVersion = \"4\"");
2090 if (RT_SUCCESS(rc))
2091 {
2092 pDescriptor->uFirstDDB = pDescriptor->cLines - 1;
2093
2094 /* Now that the framework is in place, use the normal functions to insert
2095 * the remaining keys. */
2096 char szBuf[9];
2097 RTStrPrintf(szBuf, sizeof(szBuf), "%08x", RTRandU32());
2098 rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc,
2099 "CID", szBuf);
2100 }
2101 if (RT_SUCCESS(rc))
2102 rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc,
2103 "parentCID", "ffffffff");
2104 if (RT_SUCCESS(rc))
2105 rc = vmdkDescDDBSetStr(pImage, pDescriptor, "ddb.adapterType", "ide");
2106
2107 return rc;
2108}
2109
2110static int vmdkParseDescriptor(PVMDKIMAGE pImage, char *pDescData, size_t cbDescData)
2111{
2112 int rc;
2113 unsigned cExtents;
2114 unsigned uLine;
2115 unsigned i;
2116
2117 rc = vmdkPreprocessDescriptor(pImage, pDescData, cbDescData,
2118 &pImage->Descriptor);
2119 if (RT_FAILURE(rc))
2120 return rc;
2121
2122 /* Check version, must be 1. */
2123 uint32_t uVersion;
2124 rc = vmdkDescBaseGetU32(&pImage->Descriptor, "version", &uVersion);
2125 if (RT_FAILURE(rc))
2126 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error finding key 'version' in descriptor in '%s'"), pImage->pszFilename);
2127 if (uVersion != 1)
2128 return vdIfError(pImage->pIfError, VERR_VD_VMDK_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VMDK: unsupported format version in descriptor in '%s'"), pImage->pszFilename);
2129
2130 /* Get image creation type and determine image flags. */
2131 char *pszCreateType = NULL; /* initialized to make gcc shut up */
2132 rc = vmdkDescBaseGetStr(pImage, &pImage->Descriptor, "createType",
2133 &pszCreateType);
2134 if (RT_FAILURE(rc))
2135 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot get image type from descriptor in '%s'"), pImage->pszFilename);
2136 if ( !strcmp(pszCreateType, "twoGbMaxExtentSparse")
2137 || !strcmp(pszCreateType, "twoGbMaxExtentFlat"))
2138 pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_SPLIT_2G;
2139 else if ( !strcmp(pszCreateType, "partitionedDevice")
2140 || !strcmp(pszCreateType, "fullDevice"))
2141 pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_RAWDISK;
2142 else if (!strcmp(pszCreateType, "streamOptimized"))
2143 pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED;
2144 else if (!strcmp(pszCreateType, "vmfs"))
2145 pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED | VD_VMDK_IMAGE_FLAGS_ESX;
2146 RTMemTmpFree(pszCreateType);
2147
2148 /* Count the number of extent config entries. */
2149 for (uLine = pImage->Descriptor.uFirstExtent, cExtents = 0;
2150 uLine != 0;
2151 uLine = pImage->Descriptor.aNextLines[uLine], cExtents++)
2152 /* nothing */;
2153
2154 if (!pImage->pDescData && cExtents != 1)
2155 {
2156 /* Monolithic image, must have only one extent (already opened). */
2157 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: monolithic image may only have one extent in '%s'"), pImage->pszFilename);
2158 }
2159
2160 if (pImage->pDescData)
2161 {
2162 /* Non-monolithic image, extents need to be allocated. */
2163 rc = vmdkCreateExtents(pImage, cExtents);
2164 if (RT_FAILURE(rc))
2165 return rc;
2166 }
2167
2168 for (i = 0, uLine = pImage->Descriptor.uFirstExtent;
2169 i < cExtents; i++, uLine = pImage->Descriptor.aNextLines[uLine])
2170 {
2171 char *pszLine = pImage->Descriptor.aLines[uLine];
2172
2173 /* Access type of the extent. */
2174 if (!strncmp(pszLine, "RW", 2))
2175 {
2176 pImage->pExtents[i].enmAccess = VMDKACCESS_READWRITE;
2177 pszLine += 2;
2178 }
2179 else if (!strncmp(pszLine, "RDONLY", 6))
2180 {
2181 pImage->pExtents[i].enmAccess = VMDKACCESS_READONLY;
2182 pszLine += 6;
2183 }
2184 else if (!strncmp(pszLine, "NOACCESS", 8))
2185 {
2186 pImage->pExtents[i].enmAccess = VMDKACCESS_NOACCESS;
2187 pszLine += 8;
2188 }
2189 else
2190 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2191 if (*pszLine++ != ' ')
2192 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2193
2194 /* Nominal size of the extent. */
2195 rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
2196 &pImage->pExtents[i].cNominalSectors);
2197 if (RT_FAILURE(rc))
2198 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2199 if (*pszLine++ != ' ')
2200 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2201
2202 /* Type of the extent. */
2203 if (!strncmp(pszLine, "SPARSE", 6))
2204 {
2205 pImage->pExtents[i].enmType = VMDKETYPE_HOSTED_SPARSE;
2206 pszLine += 6;
2207 }
2208 else if (!strncmp(pszLine, "FLAT", 4))
2209 {
2210 pImage->pExtents[i].enmType = VMDKETYPE_FLAT;
2211 pszLine += 4;
2212 }
2213 else if (!strncmp(pszLine, "ZERO", 4))
2214 {
2215 pImage->pExtents[i].enmType = VMDKETYPE_ZERO;
2216 pszLine += 4;
2217 }
2218 else if (!strncmp(pszLine, "VMFS", 4))
2219 {
2220 pImage->pExtents[i].enmType = VMDKETYPE_VMFS;
2221 pszLine += 4;
2222 }
2223 else
2224 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2225
2226 if (pImage->pExtents[i].enmType == VMDKETYPE_ZERO)
2227 {
2228 /* This one has no basename or offset. */
2229 if (*pszLine == ' ')
2230 pszLine++;
2231 if (*pszLine != '\0')
2232 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2233 pImage->pExtents[i].pszBasename = NULL;
2234 }
2235 else
2236 {
2237 /* All other extent types have basename and optional offset. */
2238 if (*pszLine++ != ' ')
2239 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2240
2241 /* Basename of the image. Surrounded by quotes. */
2242 char *pszBasename;
2243 rc = vmdkStringUnquote(pImage, pszLine, &pszBasename, &pszLine);
2244 if (RT_FAILURE(rc))
2245 return rc;
2246 pImage->pExtents[i].pszBasename = pszBasename;
2247 if (*pszLine == ' ')
2248 {
2249 pszLine++;
2250 if (*pszLine != '\0')
2251 {
2252 /* Optional offset in extent specified. */
2253 rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
2254 &pImage->pExtents[i].uSectorOffset);
2255 if (RT_FAILURE(rc))
2256 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2257 }
2258 }
2259
2260 if (*pszLine != '\0')
2261 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2262 }
2263 }
2264
2265 /* Determine PCHS geometry (autogenerate if necessary). */
2266 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2267 VMDK_DDB_GEO_PCHS_CYLINDERS,
2268 &pImage->PCHSGeometry.cCylinders);
2269 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2270 pImage->PCHSGeometry.cCylinders = 0;
2271 else if (RT_FAILURE(rc))
2272 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
2273 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2274 VMDK_DDB_GEO_PCHS_HEADS,
2275 &pImage->PCHSGeometry.cHeads);
2276 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2277 pImage->PCHSGeometry.cHeads = 0;
2278 else if (RT_FAILURE(rc))
2279 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
2280 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2281 VMDK_DDB_GEO_PCHS_SECTORS,
2282 &pImage->PCHSGeometry.cSectors);
2283 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2284 pImage->PCHSGeometry.cSectors = 0;
2285 else if (RT_FAILURE(rc))
2286 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
2287 if ( pImage->PCHSGeometry.cCylinders == 0
2288 || pImage->PCHSGeometry.cHeads == 0
2289 || pImage->PCHSGeometry.cHeads > 16
2290 || pImage->PCHSGeometry.cSectors == 0
2291 || pImage->PCHSGeometry.cSectors > 63)
2292 {
2293 /* Mark PCHS geometry as not yet valid (can't do the calculation here
2294 * as the total image size isn't known yet). */
2295 pImage->PCHSGeometry.cCylinders = 0;
2296 pImage->PCHSGeometry.cHeads = 16;
2297 pImage->PCHSGeometry.cSectors = 63;
2298 }
2299
2300 /* Determine LCHS geometry (set to 0 if not specified). */
2301 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2302 VMDK_DDB_GEO_LCHS_CYLINDERS,
2303 &pImage->LCHSGeometry.cCylinders);
2304 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2305 pImage->LCHSGeometry.cCylinders = 0;
2306 else if (RT_FAILURE(rc))
2307 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
2308 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2309 VMDK_DDB_GEO_LCHS_HEADS,
2310 &pImage->LCHSGeometry.cHeads);
2311 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2312 pImage->LCHSGeometry.cHeads = 0;
2313 else if (RT_FAILURE(rc))
2314 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
2315 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2316 VMDK_DDB_GEO_LCHS_SECTORS,
2317 &pImage->LCHSGeometry.cSectors);
2318 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2319 pImage->LCHSGeometry.cSectors = 0;
2320 else if (RT_FAILURE(rc))
2321 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
2322 if ( pImage->LCHSGeometry.cCylinders == 0
2323 || pImage->LCHSGeometry.cHeads == 0
2324 || pImage->LCHSGeometry.cSectors == 0)
2325 {
2326 pImage->LCHSGeometry.cCylinders = 0;
2327 pImage->LCHSGeometry.cHeads = 0;
2328 pImage->LCHSGeometry.cSectors = 0;
2329 }
2330
2331 /* Get image UUID. */
2332 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, VMDK_DDB_IMAGE_UUID,
2333 &pImage->ImageUuid);
2334 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2335 {
2336 /* Image without UUID. Probably created by VMware and not yet used
2337 * by VirtualBox. Can only be added for images opened in read/write
2338 * mode, so don't bother producing a sensible UUID otherwise. */
2339 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2340 RTUuidClear(&pImage->ImageUuid);
2341 else
2342 {
2343 rc = RTUuidCreate(&pImage->ImageUuid);
2344 if (RT_FAILURE(rc))
2345 return rc;
2346 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2347 VMDK_DDB_IMAGE_UUID, &pImage->ImageUuid);
2348 if (RT_FAILURE(rc))
2349 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
2350 }
2351 }
2352 else if (RT_FAILURE(rc))
2353 return rc;
2354
2355 /* Get image modification UUID. */
2356 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor,
2357 VMDK_DDB_MODIFICATION_UUID,
2358 &pImage->ModificationUuid);
2359 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2360 {
2361 /* Image without UUID. Probably created by VMware and not yet used
2362 * by VirtualBox. Can only be added for images opened in read/write
2363 * mode, so don't bother producing a sensible UUID otherwise. */
2364 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2365 RTUuidClear(&pImage->ModificationUuid);
2366 else
2367 {
2368 rc = RTUuidCreate(&pImage->ModificationUuid);
2369 if (RT_FAILURE(rc))
2370 return rc;
2371 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2372 VMDK_DDB_MODIFICATION_UUID,
2373 &pImage->ModificationUuid);
2374 if (RT_FAILURE(rc))
2375 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image modification UUID in descriptor in '%s'"), pImage->pszFilename);
2376 }
2377 }
2378 else if (RT_FAILURE(rc))
2379 return rc;
2380
2381 /* Get UUID of parent image. */
2382 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, VMDK_DDB_PARENT_UUID,
2383 &pImage->ParentUuid);
2384 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2385 {
2386 /* Image without UUID. Probably created by VMware and not yet used
2387 * by VirtualBox. Can only be added for images opened in read/write
2388 * mode, so don't bother producing a sensible UUID otherwise. */
2389 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2390 RTUuidClear(&pImage->ParentUuid);
2391 else
2392 {
2393 rc = RTUuidClear(&pImage->ParentUuid);
2394 if (RT_FAILURE(rc))
2395 return rc;
2396 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2397 VMDK_DDB_PARENT_UUID, &pImage->ParentUuid);
2398 if (RT_FAILURE(rc))
2399 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent UUID in descriptor in '%s'"), pImage->pszFilename);
2400 }
2401 }
2402 else if (RT_FAILURE(rc))
2403 return rc;
2404
2405 /* Get parent image modification UUID. */
2406 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor,
2407 VMDK_DDB_PARENT_MODIFICATION_UUID,
2408 &pImage->ParentModificationUuid);
2409 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2410 {
2411 /* Image without UUID. Probably created by VMware and not yet used
2412 * by VirtualBox. Can only be added for images opened in read/write
2413 * mode, so don't bother producing a sensible UUID otherwise. */
2414 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2415 RTUuidClear(&pImage->ParentModificationUuid);
2416 else
2417 {
2418 RTUuidClear(&pImage->ParentModificationUuid);
2419 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2420 VMDK_DDB_PARENT_MODIFICATION_UUID,
2421 &pImage->ParentModificationUuid);
2422 if (RT_FAILURE(rc))
2423 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent modification UUID in descriptor in '%s'"), pImage->pszFilename);
2424 }
2425 }
2426 else if (RT_FAILURE(rc))
2427 return rc;
2428
2429 return VINF_SUCCESS;
2430}
2431
2432/**
2433 * Internal : Prepares the descriptor to write to the image.
2434 */
2435static int vmdkDescriptorPrepare(PVMDKIMAGE pImage, uint64_t cbLimit,
2436 void **ppvData, size_t *pcbData)
2437{
2438 int rc = VINF_SUCCESS;
2439
2440 /*
2441 * Allocate temporary descriptor buffer.
2442 * In case there is no limit allocate a default
2443 * and increase if required.
2444 */
2445 size_t cbDescriptor = cbLimit ? cbLimit : 4 * _1K;
2446 char *pszDescriptor = (char *)RTMemAllocZ(cbDescriptor);
2447 size_t offDescriptor = 0;
2448
2449 if (!pszDescriptor)
2450 return VERR_NO_MEMORY;
2451
2452 for (unsigned i = 0; i < pImage->Descriptor.cLines; i++)
2453 {
2454 const char *psz = pImage->Descriptor.aLines[i];
2455 size_t cb = strlen(psz);
2456
2457 /*
2458 * Increase the descriptor if there is no limit and
2459 * there is not enough room left for this line.
2460 */
2461 if (offDescriptor + cb + 1 > cbDescriptor)
2462 {
2463 if (cbLimit)
2464 {
2465 rc = vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too long in '%s'"), pImage->pszFilename);
2466 break;
2467 }
2468 else
2469 {
2470 char *pszDescriptorNew = NULL;
2471 LogFlow(("Increasing descriptor cache\n"));
2472
2473 pszDescriptorNew = (char *)RTMemRealloc(pszDescriptor, cbDescriptor + cb + 4 * _1K);
2474 if (!pszDescriptorNew)
2475 {
2476 rc = VERR_NO_MEMORY;
2477 break;
2478 }
2479 pszDescriptor = pszDescriptorNew;
2480 cbDescriptor += cb + 4 * _1K;
2481 }
2482 }
2483
2484 if (cb > 0)
2485 {
2486 memcpy(pszDescriptor + offDescriptor, psz, cb);
2487 offDescriptor += cb;
2488 }
2489
2490 memcpy(pszDescriptor + offDescriptor, "\n", 1);
2491 offDescriptor++;
2492 }
2493
2494 if (RT_SUCCESS(rc))
2495 {
2496 *ppvData = pszDescriptor;
2497 *pcbData = offDescriptor;
2498 }
2499 else if (pszDescriptor)
2500 RTMemFree(pszDescriptor);
2501
2502 return rc;
2503}
2504
2505/**
2506 * Internal: write/update the descriptor part of the image.
2507 */
2508static int vmdkWriteDescriptor(PVMDKIMAGE pImage, PVDIOCTX pIoCtx)
2509{
2510 int rc = VINF_SUCCESS;
2511 uint64_t cbLimit;
2512 uint64_t uOffset;
2513 PVMDKFILE pDescFile;
2514 void *pvDescriptor = NULL;
2515 size_t cbDescriptor;
2516
2517 if (pImage->pDescData)
2518 {
2519 /* Separate descriptor file. */
2520 uOffset = 0;
2521 cbLimit = 0;
2522 pDescFile = pImage->pFile;
2523 }
2524 else
2525 {
2526 /* Embedded descriptor file. */
2527 uOffset = VMDK_SECTOR2BYTE(pImage->pExtents[0].uDescriptorSector);
2528 cbLimit = VMDK_SECTOR2BYTE(pImage->pExtents[0].cDescriptorSectors);
2529 pDescFile = pImage->pExtents[0].pFile;
2530 }
2531 /* Bail out if there is no file to write to. */
2532 if (pDescFile == NULL)
2533 return VERR_INVALID_PARAMETER;
2534
2535 rc = vmdkDescriptorPrepare(pImage, cbLimit, &pvDescriptor, &cbDescriptor);
2536 if (RT_SUCCESS(rc))
2537 {
2538 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pDescFile->pStorage,
2539 uOffset, pvDescriptor,
2540 cbLimit ? cbLimit : cbDescriptor,
2541 pIoCtx, NULL, NULL);
2542 if ( RT_FAILURE(rc)
2543 && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
2544 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing descriptor in '%s'"), pImage->pszFilename);
2545 }
2546
2547 if (RT_SUCCESS(rc) && !cbLimit)
2548 {
2549 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pDescFile->pStorage, cbDescriptor);
2550 if (RT_FAILURE(rc))
2551 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error truncating descriptor in '%s'"), pImage->pszFilename);
2552 }
2553
2554 if (RT_SUCCESS(rc))
2555 pImage->Descriptor.fDirty = false;
2556
2557 if (pvDescriptor)
2558 RTMemFree(pvDescriptor);
2559 return rc;
2560
2561}
2562
2563/**
2564 * Internal: validate the consistency check values in a binary header.
2565 */
2566static int vmdkValidateHeader(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, const SparseExtentHeader *pHeader)
2567{
2568 int rc = VINF_SUCCESS;
2569 if (RT_LE2H_U32(pHeader->magicNumber) != VMDK_SPARSE_MAGICNUMBER)
2570 {
2571 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect magic in sparse extent header in '%s'"), pExtent->pszFullname);
2572 return rc;
2573 }
2574 if (RT_LE2H_U32(pHeader->version) != 1 && RT_LE2H_U32(pHeader->version) != 3)
2575 {
2576 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VMDK: incorrect version in sparse extent header in '%s', not a VMDK 1.0/1.1 conforming file"), pExtent->pszFullname);
2577 return rc;
2578 }
2579 if ( (RT_LE2H_U32(pHeader->flags) & 1)
2580 && ( pHeader->singleEndLineChar != '\n'
2581 || pHeader->nonEndLineChar != ' '
2582 || pHeader->doubleEndLineChar1 != '\r'
2583 || pHeader->doubleEndLineChar2 != '\n') )
2584 {
2585 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: corrupted by CR/LF translation in '%s'"), pExtent->pszFullname);
2586 return rc;
2587 }
2588 return rc;
2589}
2590
2591/**
2592 * Internal: read metadata belonging to an extent with binary header, i.e.
2593 * as found in monolithic files.
2594 */
2595static int vmdkReadBinaryMetaExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
2596 bool fMagicAlreadyRead)
2597{
2598 SparseExtentHeader Header;
2599 int rc;
2600
2601 if (!fMagicAlreadyRead)
2602 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage, 0,
2603 &Header, sizeof(Header));
2604 else
2605 {
2606 Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
2607 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
2608 RT_UOFFSETOF(SparseExtentHeader, version),
2609 &Header.version,
2610 sizeof(Header)
2611 - RT_UOFFSETOF(SparseExtentHeader, version));
2612 }
2613
2614 if (RT_SUCCESS(rc))
2615 {
2616 rc = vmdkValidateHeader(pImage, pExtent, &Header);
2617 if (RT_SUCCESS(rc))
2618 {
2619 uint64_t cbFile = 0;
2620
2621 if ( (RT_LE2H_U32(Header.flags) & RT_BIT(17))
2622 && RT_LE2H_U64(Header.gdOffset) == VMDK_GD_AT_END)
2623 pExtent->fFooter = true;
2624
2625 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2626 || ( pExtent->fFooter
2627 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
2628 {
2629 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbFile);
2630 if (RT_FAILURE(rc))
2631 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot get size of '%s'"), pExtent->pszFullname);
2632 }
2633
2634 if (RT_SUCCESS(rc))
2635 {
2636 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2637 pExtent->uAppendPosition = RT_ALIGN_64(cbFile, 512);
2638
2639 if ( pExtent->fFooter
2640 && ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2641 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
2642 {
2643 /* Read the footer, which comes before the end-of-stream marker. */
2644 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
2645 cbFile - 2*512, &Header,
2646 sizeof(Header));
2647 if (RT_FAILURE(rc))
2648 {
2649 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading extent footer in '%s'"), pExtent->pszFullname);
2650 rc = VERR_VD_VMDK_INVALID_HEADER;
2651 }
2652
2653 if (RT_SUCCESS(rc))
2654 rc = vmdkValidateHeader(pImage, pExtent, &Header);
2655 /* Prohibit any writes to this extent. */
2656 pExtent->uAppendPosition = 0;
2657 }
2658
2659 if (RT_SUCCESS(rc))
2660 {
2661 pExtent->uVersion = RT_LE2H_U32(Header.version);
2662 pExtent->enmType = VMDKETYPE_HOSTED_SPARSE; /* Just dummy value, changed later. */
2663 pExtent->cSectors = RT_LE2H_U64(Header.capacity);
2664 pExtent->cSectorsPerGrain = RT_LE2H_U64(Header.grainSize);
2665 pExtent->uDescriptorSector = RT_LE2H_U64(Header.descriptorOffset);
2666 pExtent->cDescriptorSectors = RT_LE2H_U64(Header.descriptorSize);
2667 pExtent->cGTEntries = RT_LE2H_U32(Header.numGTEsPerGT);
2668 pExtent->cOverheadSectors = RT_LE2H_U64(Header.overHead);
2669 pExtent->fUncleanShutdown = !!Header.uncleanShutdown;
2670 pExtent->uCompression = RT_LE2H_U16(Header.compressAlgorithm);
2671 if (RT_LE2H_U32(Header.flags) & RT_BIT(1))
2672 {
2673 pExtent->uSectorRGD = RT_LE2H_U64(Header.rgdOffset);
2674 pExtent->uSectorGD = RT_LE2H_U64(Header.gdOffset);
2675 }
2676 else
2677 {
2678 pExtent->uSectorGD = RT_LE2H_U64(Header.gdOffset);
2679 pExtent->uSectorRGD = 0;
2680 }
2681
2682 if (pExtent->uDescriptorSector && !pExtent->cDescriptorSectors)
2683 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2684 N_("VMDK: inconsistent embedded descriptor config in '%s'"), pExtent->pszFullname);
2685
2686 if ( RT_SUCCESS(rc)
2687 && ( pExtent->uSectorGD == VMDK_GD_AT_END
2688 || pExtent->uSectorRGD == VMDK_GD_AT_END)
2689 && ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2690 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
2691 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2692 N_("VMDK: cannot resolve grain directory offset in '%s'"), pExtent->pszFullname);
2693
2694 if (RT_SUCCESS(rc))
2695 {
2696 uint64_t cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
2697 if (!cSectorsPerGDE || cSectorsPerGDE > UINT32_MAX)
2698 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2699 N_("VMDK: incorrect grain directory size in '%s'"), pExtent->pszFullname);
2700 else
2701 {
2702 pExtent->cSectorsPerGDE = cSectorsPerGDE;
2703 pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
2704
2705 /* Fix up the number of descriptor sectors, as some flat images have
2706 * really just one, and this causes failures when inserting the UUID
2707 * values and other extra information. */
2708 if (pExtent->cDescriptorSectors != 0 && pExtent->cDescriptorSectors < 4)
2709 {
2710 /* Do it the easy way - just fix it for flat images which have no
2711 * other complicated metadata which needs space too. */
2712 if ( pExtent->uDescriptorSector + 4 < pExtent->cOverheadSectors
2713 && pExtent->cGTEntries * pExtent->cGDEntries == 0)
2714 pExtent->cDescriptorSectors = 4;
2715 }
2716 }
2717 }
2718 }
2719 }
2720 }
2721 }
2722 else
2723 {
2724 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading extent header in '%s'"), pExtent->pszFullname);
2725 rc = VERR_VD_VMDK_INVALID_HEADER;
2726 }
2727
2728 if (RT_FAILURE(rc))
2729 vmdkFreeExtentData(pImage, pExtent, false);
2730
2731 return rc;
2732}
2733
2734/**
2735 * Internal: read additional metadata belonging to an extent. For those
2736 * extents which have no additional metadata just verify the information.
2737 */
2738static int vmdkReadMetaExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
2739{
2740 int rc = VINF_SUCCESS;
2741
2742/* disabled the check as there are too many truncated vmdk images out there */
2743#ifdef VBOX_WITH_VMDK_STRICT_SIZE_CHECK
2744 uint64_t cbExtentSize;
2745 /* The image must be a multiple of a sector in size and contain the data
2746 * area (flat images only). If not, it means the image is at least
2747 * truncated, or even seriously garbled. */
2748 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbExtentSize);
2749 if (RT_FAILURE(rc))
2750 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting size in '%s'"), pExtent->pszFullname);
2751 else if ( cbExtentSize != RT_ALIGN_64(cbExtentSize, 512)
2752 && (pExtent->enmType != VMDKETYPE_FLAT || pExtent->cNominalSectors + pExtent->uSectorOffset > VMDK_BYTE2SECTOR(cbExtentSize)))
2753 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2754 N_("VMDK: file size is not a multiple of 512 in '%s', file is truncated or otherwise garbled"), pExtent->pszFullname);
2755#endif /* VBOX_WITH_VMDK_STRICT_SIZE_CHECK */
2756 if ( RT_SUCCESS(rc)
2757 && pExtent->enmType == VMDKETYPE_HOSTED_SPARSE)
2758 {
2759 /* The spec says that this must be a power of two and greater than 8,
2760 * but probably they meant not less than 8. */
2761 if ( (pExtent->cSectorsPerGrain & (pExtent->cSectorsPerGrain - 1))
2762 || pExtent->cSectorsPerGrain < 8)
2763 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2764 N_("VMDK: invalid extent grain size %u in '%s'"), pExtent->cSectorsPerGrain, pExtent->pszFullname);
2765 else
2766 {
2767 /* This code requires that a grain table must hold a power of two multiple
2768 * of the number of entries per GT cache entry. */
2769 if ( (pExtent->cGTEntries & (pExtent->cGTEntries - 1))
2770 || pExtent->cGTEntries < VMDK_GT_CACHELINE_SIZE)
2771 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2772 N_("VMDK: grain table cache size problem in '%s'"), pExtent->pszFullname);
2773 else
2774 {
2775 rc = vmdkAllocStreamBuffers(pImage, pExtent);
2776 if (RT_SUCCESS(rc))
2777 {
2778 /* Prohibit any writes to this streamOptimized extent. */
2779 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
2780 pExtent->uAppendPosition = 0;
2781
2782 if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
2783 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2784 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
2785 rc = vmdkReadGrainDirectory(pImage, pExtent);
2786 else
2787 {
2788 pExtent->uGrainSectorAbs = pExtent->cOverheadSectors;
2789 pExtent->cbGrainStreamRead = 0;
2790 }
2791 }
2792 }
2793 }
2794 }
2795
2796 if (RT_FAILURE(rc))
2797 vmdkFreeExtentData(pImage, pExtent, false);
2798
2799 return rc;
2800}
2801
2802/**
2803 * Internal: write/update the metadata for a sparse extent.
2804 */
2805static int vmdkWriteMetaSparseExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
2806 uint64_t uOffset, PVDIOCTX pIoCtx)
2807{
2808 SparseExtentHeader Header;
2809
2810 memset(&Header, '\0', sizeof(Header));
2811 Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
2812 Header.version = RT_H2LE_U32(pExtent->uVersion);
2813 Header.flags = RT_H2LE_U32(RT_BIT(0));
2814 if (pExtent->pRGD)
2815 Header.flags |= RT_H2LE_U32(RT_BIT(1));
2816 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
2817 Header.flags |= RT_H2LE_U32(RT_BIT(16) | RT_BIT(17));
2818 Header.capacity = RT_H2LE_U64(pExtent->cSectors);
2819 Header.grainSize = RT_H2LE_U64(pExtent->cSectorsPerGrain);
2820 Header.descriptorOffset = RT_H2LE_U64(pExtent->uDescriptorSector);
2821 Header.descriptorSize = RT_H2LE_U64(pExtent->cDescriptorSectors);
2822 Header.numGTEsPerGT = RT_H2LE_U32(pExtent->cGTEntries);
2823 if (pExtent->fFooter && uOffset == 0)
2824 {
2825 if (pExtent->pRGD)
2826 {
2827 Assert(pExtent->uSectorRGD);
2828 Header.rgdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
2829 Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
2830 }
2831 else
2832 Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
2833 }
2834 else
2835 {
2836 if (pExtent->pRGD)
2837 {
2838 Assert(pExtent->uSectorRGD);
2839 Header.rgdOffset = RT_H2LE_U64(pExtent->uSectorRGD);
2840 Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
2841 }
2842 else
2843 Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
2844 }
2845 Header.overHead = RT_H2LE_U64(pExtent->cOverheadSectors);
2846 Header.uncleanShutdown = pExtent->fUncleanShutdown;
2847 Header.singleEndLineChar = '\n';
2848 Header.nonEndLineChar = ' ';
2849 Header.doubleEndLineChar1 = '\r';
2850 Header.doubleEndLineChar2 = '\n';
2851 Header.compressAlgorithm = RT_H2LE_U16(pExtent->uCompression);
2852
2853 int rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
2854 uOffset, &Header, sizeof(Header),
2855 pIoCtx, NULL, NULL);
2856 if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
2857 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing extent header in '%s'"), pExtent->pszFullname);
2858 return rc;
2859}
2860
2861/**
2862 * Internal: free the buffers used for streamOptimized images.
2863 */
2864static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent)
2865{
2866 if (pExtent->pvCompGrain)
2867 {
2868 RTMemFree(pExtent->pvCompGrain);
2869 pExtent->pvCompGrain = NULL;
2870 }
2871 if (pExtent->pvGrain)
2872 {
2873 RTMemFree(pExtent->pvGrain);
2874 pExtent->pvGrain = NULL;
2875 }
2876}
2877
2878/**
2879 * Internal: free the memory used by the extent data structure, optionally
2880 * deleting the referenced files.
2881 *
2882 * @returns VBox status code.
2883 * @param pImage Pointer to the image instance data.
2884 * @param pExtent The extent to free.
2885 * @param fDelete Flag whether to delete the backing storage.
2886 */
2887static int vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
2888 bool fDelete)
2889{
2890 int rc = VINF_SUCCESS;
2891
2892 vmdkFreeGrainDirectory(pExtent);
2893 if (pExtent->pDescData)
2894 {
2895 RTMemFree(pExtent->pDescData);
2896 pExtent->pDescData = NULL;
2897 }
2898 if (pExtent->pFile != NULL)
2899 {
2900 /* Do not delete raw extents, these have full and base names equal. */
2901 rc = vmdkFileClose(pImage, &pExtent->pFile,
2902 fDelete
2903 && pExtent->pszFullname
2904 && pExtent->pszBasename
2905 && strcmp(pExtent->pszFullname, pExtent->pszBasename));
2906 }
2907 if (pExtent->pszBasename)
2908 {
2909 RTMemTmpFree((void *)pExtent->pszBasename);
2910 pExtent->pszBasename = NULL;
2911 }
2912 if (pExtent->pszFullname)
2913 {
2914 RTStrFree((char *)(void *)pExtent->pszFullname);
2915 pExtent->pszFullname = NULL;
2916 }
2917 vmdkFreeStreamBuffers(pExtent);
2918
2919 return rc;
2920}
2921
2922/**
2923 * Internal: allocate grain table cache if necessary for this image.
2924 */
2925static int vmdkAllocateGrainTableCache(PVMDKIMAGE pImage)
2926{
2927 PVMDKEXTENT pExtent;
2928
2929 /* Allocate grain table cache if any sparse extent is present. */
2930 for (unsigned i = 0; i < pImage->cExtents; i++)
2931 {
2932 pExtent = &pImage->pExtents[i];
2933 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE)
2934 {
2935 /* Allocate grain table cache. */
2936 pImage->pGTCache = (PVMDKGTCACHE)RTMemAllocZ(sizeof(VMDKGTCACHE));
2937 if (!pImage->pGTCache)
2938 return VERR_NO_MEMORY;
2939 for (unsigned j = 0; j < VMDK_GT_CACHE_SIZE; j++)
2940 {
2941 PVMDKGTCACHEENTRY pGCE = &pImage->pGTCache->aGTCache[j];
2942 pGCE->uExtent = UINT32_MAX;
2943 }
2944 pImage->pGTCache->cEntries = VMDK_GT_CACHE_SIZE;
2945 break;
2946 }
2947 }
2948
2949 return VINF_SUCCESS;
2950}
2951
2952/**
2953 * Internal: allocate the given number of extents.
2954 */
2955static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents)
2956{
2957 int rc = VINF_SUCCESS;
2958 PVMDKEXTENT pExtents = (PVMDKEXTENT)RTMemAllocZ(cExtents * sizeof(VMDKEXTENT));
2959 if (pExtents)
2960 {
2961 for (unsigned i = 0; i < cExtents; i++)
2962 {
2963 pExtents[i].pFile = NULL;
2964 pExtents[i].pszBasename = NULL;
2965 pExtents[i].pszFullname = NULL;
2966 pExtents[i].pGD = NULL;
2967 pExtents[i].pRGD = NULL;
2968 pExtents[i].pDescData = NULL;
2969 pExtents[i].uVersion = 1;
2970 pExtents[i].uCompression = VMDK_COMPRESSION_NONE;
2971 pExtents[i].uExtent = i;
2972 pExtents[i].pImage = pImage;
2973 }
2974 pImage->pExtents = pExtents;
2975 pImage->cExtents = cExtents;
2976 }
2977 else
2978 rc = VERR_NO_MEMORY;
2979
2980 return rc;
2981}
2982
2983/**
2984 * Reads and processes the descriptor embedded in sparse images.
2985 *
2986 * @returns VBox status code.
2987 * @param pImage VMDK image instance.
2988 * @param pFile The sparse file handle.
2989 */
2990static int vmdkDescriptorReadSparse(PVMDKIMAGE pImage, PVMDKFILE pFile)
2991{
2992 /* It's a hosted single-extent image. */
2993 int rc = vmdkCreateExtents(pImage, 1);
2994 if (RT_SUCCESS(rc))
2995 {
2996 /* The opened file is passed to the extent. No separate descriptor
2997 * file, so no need to keep anything open for the image. */
2998 PVMDKEXTENT pExtent = &pImage->pExtents[0];
2999 pExtent->pFile = pFile;
3000 pImage->pFile = NULL;
3001 pExtent->pszFullname = RTPathAbsDup(pImage->pszFilename);
3002 if (RT_LIKELY(pExtent->pszFullname))
3003 {
3004 /* As we're dealing with a monolithic image here, there must
3005 * be a descriptor embedded in the image file. */
3006 rc = vmdkReadBinaryMetaExtent(pImage, pExtent, true /* fMagicAlreadyRead */);
3007 if ( RT_SUCCESS(rc)
3008 && pExtent->uDescriptorSector
3009 && pExtent->cDescriptorSectors)
3010 {
3011 /* HACK: extend the descriptor if it is unusually small and it fits in
3012 * the unused space after the image header. Allows opening VMDK files
3013 * with extremely small descriptor in read/write mode.
3014 *
3015 * The previous version introduced a possible regression for VMDK stream
3016 * optimized images from VMware which tend to have only a single sector sized
3017 * descriptor. Increasing the descriptor size resulted in adding the various uuid
3018 * entries required to make it work with VBox but for stream optimized images
3019 * the updated binary header wasn't written to the disk creating a mismatch
3020 * between advertised and real descriptor size.
3021 *
3022 * The descriptor size will be increased even if opened readonly now if there
3023 * enough room but the new value will not be written back to the image.
3024 */
3025 if ( pExtent->cDescriptorSectors < 3
3026 && (int64_t)pExtent->uSectorGD - pExtent->uDescriptorSector >= 4
3027 && (!pExtent->uSectorRGD || (int64_t)pExtent->uSectorRGD - pExtent->uDescriptorSector >= 4))
3028 {
3029 uint64_t cDescriptorSectorsOld = pExtent->cDescriptorSectors;
3030
3031 pExtent->cDescriptorSectors = 4;
3032 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3033 {
3034 /*
3035 * Update the on disk number now to make sure we don't introduce inconsistencies
3036 * in case of stream optimized images from VMware where the descriptor is just
3037 * one sector big (the binary header is not written to disk for complete
3038 * stream optimized images in vmdkFlushImage()).
3039 */
3040 uint64_t u64DescSizeNew = RT_H2LE_U64(pExtent->cDescriptorSectors);
3041 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pFile->pStorage,
3042 RT_UOFFSETOF(SparseExtentHeader, descriptorSize),
3043 &u64DescSizeNew, sizeof(u64DescSizeNew));
3044 if (RT_FAILURE(rc))
3045 {
3046 LogFlowFunc(("Increasing the descriptor size failed with %Rrc\n", rc));
3047 /* Restore the old size and carry on. */
3048 pExtent->cDescriptorSectors = cDescriptorSectorsOld;
3049 }
3050 }
3051 }
3052 /* Read the descriptor from the extent. */
3053 pExtent->pDescData = (char *)RTMemAllocZ(VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
3054 if (RT_LIKELY(pExtent->pDescData))
3055 {
3056 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
3057 VMDK_SECTOR2BYTE(pExtent->uDescriptorSector),
3058 pExtent->pDescData,
3059 VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
3060 if (RT_SUCCESS(rc))
3061 {
3062 rc = vmdkParseDescriptor(pImage, pExtent->pDescData,
3063 VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
3064 if ( RT_SUCCESS(rc)
3065 && ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3066 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)))
3067 {
3068 rc = vmdkReadMetaExtent(pImage, pExtent);
3069 if (RT_SUCCESS(rc))
3070 {
3071 /* Mark the extent as unclean if opened in read-write mode. */
3072 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3073 && !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
3074 {
3075 pExtent->fUncleanShutdown = true;
3076 pExtent->fMetaDirty = true;
3077 }
3078 }
3079 }
3080 else if (RT_SUCCESS(rc))
3081 rc = VERR_NOT_SUPPORTED;
3082 }
3083 else
3084 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pExtent->pszFullname);
3085 }
3086 else
3087 rc = VERR_NO_MEMORY;
3088 }
3089 else if (RT_SUCCESS(rc))
3090 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: monolithic image without descriptor in '%s'"), pImage->pszFilename);
3091 }
3092 else
3093 rc = VERR_NO_MEMORY;
3094 }
3095
3096 return rc;
3097}
3098
3099/**
3100 * Reads the descriptor from a pure text file.
3101 *
3102 * @returns VBox status code.
3103 * @param pImage VMDK image instance.
3104 * @param pFile The descriptor file handle.
3105 */
3106static int vmdkDescriptorReadAscii(PVMDKIMAGE pImage, PVMDKFILE pFile)
3107{
3108 /* Allocate at least 10K, and make sure that there is 5K free space
3109 * in case new entries need to be added to the descriptor. Never
3110 * allocate more than 128K, because that's no valid descriptor file
3111 * and will result in the correct "truncated read" error handling. */
3112 uint64_t cbFileSize;
3113 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pFile->pStorage, &cbFileSize);
3114 if ( RT_SUCCESS(rc)
3115 && cbFileSize >= 50)
3116 {
3117 uint64_t cbSize = cbFileSize;
3118 if (cbSize % VMDK_SECTOR2BYTE(10))
3119 cbSize += VMDK_SECTOR2BYTE(20) - cbSize % VMDK_SECTOR2BYTE(10);
3120 else
3121 cbSize += VMDK_SECTOR2BYTE(10);
3122 cbSize = RT_MIN(cbSize, _128K);
3123 pImage->cbDescAlloc = RT_MAX(VMDK_SECTOR2BYTE(20), cbSize);
3124 pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
3125 if (RT_LIKELY(pImage->pDescData))
3126 {
3127 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pFile->pStorage, 0, pImage->pDescData,
3128 RT_MIN(pImage->cbDescAlloc, cbFileSize));
3129 if (RT_SUCCESS(rc))
3130 {
3131#if 0 /** @todo Revisit */
3132 cbRead += sizeof(u32Magic);
3133 if (cbRead == pImage->cbDescAlloc)
3134 {
3135 /* Likely the read is truncated. Better fail a bit too early
3136 * (normally the descriptor is much smaller than our buffer). */
3137 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: cannot read descriptor in '%s'"), pImage->pszFilename);
3138 goto out;
3139 }
3140#endif
3141 rc = vmdkParseDescriptor(pImage, pImage->pDescData,
3142 pImage->cbDescAlloc);
3143 if (RT_SUCCESS(rc))
3144 {
3145 for (unsigned i = 0; i < pImage->cExtents && RT_SUCCESS(rc); i++)
3146 {
3147 PVMDKEXTENT pExtent = &pImage->pExtents[i];
3148 if (pExtent->pszBasename)
3149 {
3150 /* Hack to figure out whether the specified name in the
3151 * extent descriptor is absolute. Doesn't always work, but
3152 * should be good enough for now. */
3153 char *pszFullname;
3154 /** @todo implement proper path absolute check. */
3155 if (pExtent->pszBasename[0] == RTPATH_SLASH)
3156 {
3157 pszFullname = RTStrDup(pExtent->pszBasename);
3158 if (!pszFullname)
3159 {
3160 rc = VERR_NO_MEMORY;
3161 break;
3162 }
3163 }
3164 else
3165 {
3166 char *pszDirname = RTStrDup(pImage->pszFilename);
3167 if (!pszDirname)
3168 {
3169 rc = VERR_NO_MEMORY;
3170 break;
3171 }
3172 RTPathStripFilename(pszDirname);
3173 pszFullname = RTPathJoinA(pszDirname, pExtent->pszBasename);
3174 RTStrFree(pszDirname);
3175 if (!pszFullname)
3176 {
3177 rc = VERR_NO_STR_MEMORY;
3178 break;
3179 }
3180 }
3181 pExtent->pszFullname = pszFullname;
3182 }
3183 else
3184 pExtent->pszFullname = NULL;
3185
3186 unsigned uOpenFlags = pImage->uOpenFlags | ((pExtent->enmAccess == VMDKACCESS_READONLY) ? VD_OPEN_FLAGS_READONLY : 0);
3187 switch (pExtent->enmType)
3188 {
3189 case VMDKETYPE_HOSTED_SPARSE:
3190 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
3191 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */));
3192 if (RT_FAILURE(rc))
3193 {
3194 /* Do NOT signal an appropriate error here, as the VD
3195 * layer has the choice of retrying the open if it
3196 * failed. */
3197 break;
3198 }
3199 rc = vmdkReadBinaryMetaExtent(pImage, pExtent,
3200 false /* fMagicAlreadyRead */);
3201 if (RT_FAILURE(rc))
3202 break;
3203 rc = vmdkReadMetaExtent(pImage, pExtent);
3204 if (RT_FAILURE(rc))
3205 break;
3206
3207 /* Mark extent as unclean if opened in read-write mode. */
3208 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3209 {
3210 pExtent->fUncleanShutdown = true;
3211 pExtent->fMetaDirty = true;
3212 }
3213 break;
3214 case VMDKETYPE_VMFS:
3215 case VMDKETYPE_FLAT:
3216 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
3217 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */));
3218 if (RT_FAILURE(rc))
3219 {
3220 /* Do NOT signal an appropriate error here, as the VD
3221 * layer has the choice of retrying the open if it
3222 * failed. */
3223 break;
3224 }
3225 break;
3226 case VMDKETYPE_ZERO:
3227 /* Nothing to do. */
3228 break;
3229 default:
3230 AssertMsgFailed(("unknown vmdk extent type %d\n", pExtent->enmType));
3231 }
3232 }
3233 }
3234 }
3235 else
3236 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pImage->pszFilename);
3237 }
3238 else
3239 rc = VERR_NO_MEMORY;
3240 }
3241 else if (RT_SUCCESS(rc))
3242 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor in '%s' is too short"), pImage->pszFilename);
3243
3244 return rc;
3245}
3246
3247/**
3248 * Read and process the descriptor based on the image type.
3249 *
3250 * @returns VBox status code.
3251 * @param pImage VMDK image instance.
3252 * @param pFile VMDK file handle.
3253 */
3254static int vmdkDescriptorRead(PVMDKIMAGE pImage, PVMDKFILE pFile)
3255{
3256 uint32_t u32Magic;
3257
3258 /* Read magic (if present). */
3259 int rc = vdIfIoIntFileReadSync(pImage->pIfIo, pFile->pStorage, 0,
3260 &u32Magic, sizeof(u32Magic));
3261 if (RT_SUCCESS(rc))
3262 {
3263 /* Handle the file according to its magic number. */
3264 if (RT_LE2H_U32(u32Magic) == VMDK_SPARSE_MAGICNUMBER)
3265 rc = vmdkDescriptorReadSparse(pImage, pFile);
3266 else
3267 rc = vmdkDescriptorReadAscii(pImage, pFile);
3268 }
3269 else
3270 {
3271 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading the magic number in '%s'"), pImage->pszFilename);
3272 rc = VERR_VD_VMDK_INVALID_HEADER;
3273 }
3274
3275 return rc;
3276}
3277
3278/**
3279 * Internal: Open an image, constructing all necessary data structures.
3280 */
3281static int vmdkOpenImage(PVMDKIMAGE pImage, unsigned uOpenFlags)
3282{
3283 pImage->uOpenFlags = uOpenFlags;
3284 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
3285 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
3286 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
3287
3288 /*
3289 * Open the image.
3290 * We don't have to check for asynchronous access because
3291 * we only support raw access and the opened file is a description
3292 * file were no data is stored.
3293 */
3294 PVMDKFILE pFile;
3295 int rc = vmdkFileOpen(pImage, &pFile, pImage->pszFilename,
3296 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */));
3297 if (RT_SUCCESS(rc))
3298 {
3299 pImage->pFile = pFile;
3300
3301 rc = vmdkDescriptorRead(pImage, pFile);
3302 if (RT_SUCCESS(rc))
3303 {
3304 /* Determine PCHS geometry if not set. */
3305 if (pImage->PCHSGeometry.cCylinders == 0)
3306 {
3307 uint64_t cCylinders = VMDK_BYTE2SECTOR(pImage->cbSize)
3308 / pImage->PCHSGeometry.cHeads
3309 / pImage->PCHSGeometry.cSectors;
3310 pImage->PCHSGeometry.cCylinders = (unsigned)RT_MIN(cCylinders, 16383);
3311 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3312 && !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
3313 {
3314 rc = vmdkDescSetPCHSGeometry(pImage, &pImage->PCHSGeometry);
3315 AssertRC(rc);
3316 }
3317 }
3318
3319 /* Update the image metadata now in case has changed. */
3320 rc = vmdkFlushImage(pImage, NULL);
3321 if (RT_SUCCESS(rc))
3322 {
3323 /* Figure out a few per-image constants from the extents. */
3324 pImage->cbSize = 0;
3325 for (unsigned i = 0; i < pImage->cExtents; i++)
3326 {
3327 PVMDKEXTENT pExtent = &pImage->pExtents[i];
3328 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE)
3329 {
3330 /* Here used to be a check whether the nominal size of an extent
3331 * is a multiple of the grain size. The spec says that this is
3332 * always the case, but unfortunately some files out there in the
3333 * wild violate the spec (e.g. ReactOS 0.3.1). */
3334 }
3335 else if ( pExtent->enmType == VMDKETYPE_FLAT
3336 || pExtent->enmType == VMDKETYPE_ZERO)
3337 pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED;
3338
3339 pImage->cbSize += VMDK_SECTOR2BYTE(pExtent->cNominalSectors);
3340 }
3341
3342 if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3343 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3344 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
3345 rc = vmdkAllocateGrainTableCache(pImage);
3346 }
3347 }
3348 }
3349 /* else: Do NOT signal an appropriate error here, as the VD layer has the
3350 * choice of retrying the open if it failed. */
3351
3352 if (RT_SUCCESS(rc))
3353 {
3354 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
3355 pImage->RegionList.fFlags = 0;
3356 pImage->RegionList.cRegions = 1;
3357
3358 pRegion->offRegion = 0; /* Disk start. */
3359 pRegion->cbBlock = 512;
3360 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
3361 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
3362 pRegion->cbData = 512;
3363 pRegion->cbMetadata = 0;
3364 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
3365 }
3366 else
3367 vmdkFreeImage(pImage, false);
3368 return rc;
3369}
3370
3371/**
3372 * Internal: create VMDK images for raw disk/partition access.
3373 */
3374static int vmdkCreateRawImage(PVMDKIMAGE pImage, const PVDISKRAW pRaw,
3375 uint64_t cbSize)
3376{
3377 int rc = VINF_SUCCESS;
3378 PVMDKEXTENT pExtent;
3379
3380 if (pRaw->uFlags & VDISKRAW_DISK)
3381 {
3382 /* Full raw disk access. This requires setting up a descriptor
3383 * file and open the (flat) raw disk. */
3384 rc = vmdkCreateExtents(pImage, 1);
3385 if (RT_FAILURE(rc))
3386 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
3387 pExtent = &pImage->pExtents[0];
3388 /* Create raw disk descriptor file. */
3389 rc = vmdkFileOpen(pImage, &pImage->pFile, pImage->pszFilename,
3390 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
3391 true /* fCreate */));
3392 if (RT_FAILURE(rc))
3393 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pImage->pszFilename);
3394
3395 /* Set up basename for extent description. Cannot use StrDup. */
3396 size_t cbBasename = strlen(pRaw->pszRawDisk) + 1;
3397 char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
3398 if (!pszBasename)
3399 return VERR_NO_MEMORY;
3400 memcpy(pszBasename, pRaw->pszRawDisk, cbBasename);
3401 pExtent->pszBasename = pszBasename;
3402 /* For raw disks the full name is identical to the base name. */
3403 pExtent->pszFullname = RTStrDup(pszBasename);
3404 if (!pExtent->pszFullname)
3405 return VERR_NO_MEMORY;
3406 pExtent->enmType = VMDKETYPE_FLAT;
3407 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
3408 pExtent->uSectorOffset = 0;
3409 pExtent->enmAccess = (pRaw->uFlags & VDISKRAW_READONLY) ? VMDKACCESS_READONLY : VMDKACCESS_READWRITE;
3410 pExtent->fMetaDirty = false;
3411
3412 /* Open flat image, the raw disk. */
3413 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
3414 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags | ((pExtent->enmAccess == VMDKACCESS_READONLY) ? VD_OPEN_FLAGS_READONLY : 0),
3415 false /* fCreate */));
3416 if (RT_FAILURE(rc))
3417 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not open raw disk file '%s'"), pExtent->pszFullname);
3418 }
3419 else
3420 {
3421 /* Raw partition access. This requires setting up a descriptor
3422 * file, write the partition information to a flat extent and
3423 * open all the (flat) raw disk partitions. */
3424
3425 /* First pass over the partition data areas to determine how many
3426 * extents we need. One data area can require up to 2 extents, as
3427 * it might be necessary to skip over unpartitioned space. */
3428 unsigned cExtents = 0;
3429 uint64_t uStart = 0;
3430 for (unsigned i = 0; i < pRaw->cPartDescs; i++)
3431 {
3432 PVDISKRAWPARTDESC pPart = &pRaw->pPartDescs[i];
3433 if (uStart > pPart->uStart)
3434 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
3435 N_("VMDK: incorrect partition data area ordering set up by the caller in '%s'"), pImage->pszFilename);
3436
3437 if (uStart < pPart->uStart)
3438 cExtents++;
3439 uStart = pPart->uStart + pPart->cbData;
3440 cExtents++;
3441 }
3442 /* Another extent for filling up the rest of the image. */
3443 if (uStart != cbSize)
3444 cExtents++;
3445
3446 rc = vmdkCreateExtents(pImage, cExtents);
3447 if (RT_FAILURE(rc))
3448 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
3449
3450 /* Create raw partition descriptor file. */
3451 rc = vmdkFileOpen(pImage, &pImage->pFile, pImage->pszFilename,
3452 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
3453 true /* fCreate */));
3454 if (RT_FAILURE(rc))
3455 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pImage->pszFilename);
3456
3457 /* Create base filename for the partition table extent. */
3458 /** @todo remove fixed buffer without creating memory leaks. */
3459 char pszPartition[1024];
3460 const char *pszBase = RTPathFilename(pImage->pszFilename);
3461 const char *pszSuff = RTPathSuffix(pszBase);
3462 if (pszSuff == NULL)
3463 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: invalid filename '%s'"), pImage->pszFilename);
3464 char *pszBaseBase = RTStrDup(pszBase);
3465 if (!pszBaseBase)
3466 return VERR_NO_MEMORY;
3467 RTPathStripSuffix(pszBaseBase);
3468 RTStrPrintf(pszPartition, sizeof(pszPartition), "%s-pt%s",
3469 pszBaseBase, pszSuff);
3470 RTStrFree(pszBaseBase);
3471
3472 /* Second pass over the partitions, now define all extents. */
3473 uint64_t uPartOffset = 0;
3474 cExtents = 0;
3475 uStart = 0;
3476 for (unsigned i = 0; i < pRaw->cPartDescs; i++)
3477 {
3478 PVDISKRAWPARTDESC pPart = &pRaw->pPartDescs[i];
3479 pExtent = &pImage->pExtents[cExtents++];
3480
3481 if (uStart < pPart->uStart)
3482 {
3483 pExtent->pszBasename = NULL;
3484 pExtent->pszFullname = NULL;
3485 pExtent->enmType = VMDKETYPE_ZERO;
3486 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->uStart - uStart);
3487 pExtent->uSectorOffset = 0;
3488 pExtent->enmAccess = VMDKACCESS_READWRITE;
3489 pExtent->fMetaDirty = false;
3490 /* go to next extent */
3491 pExtent = &pImage->pExtents[cExtents++];
3492 }
3493 uStart = pPart->uStart + pPart->cbData;
3494
3495 if (pPart->pvPartitionData)
3496 {
3497 /* Set up basename for extent description. Can't use StrDup. */
3498 size_t cbBasename = strlen(pszPartition) + 1;
3499 char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
3500 if (!pszBasename)
3501 return VERR_NO_MEMORY;
3502 memcpy(pszBasename, pszPartition, cbBasename);
3503 pExtent->pszBasename = pszBasename;
3504
3505 /* Set up full name for partition extent. */
3506 char *pszDirname = RTStrDup(pImage->pszFilename);
3507 if (!pszDirname)
3508 return VERR_NO_STR_MEMORY;
3509 RTPathStripFilename(pszDirname);
3510 char *pszFullname = RTPathJoinA(pszDirname, pExtent->pszBasename);
3511 RTStrFree(pszDirname);
3512 if (!pszFullname)
3513 return VERR_NO_STR_MEMORY;
3514 pExtent->pszFullname = pszFullname;
3515 pExtent->enmType = VMDKETYPE_FLAT;
3516 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbData);
3517 pExtent->uSectorOffset = uPartOffset;
3518 pExtent->enmAccess = VMDKACCESS_READWRITE;
3519 pExtent->fMetaDirty = false;
3520
3521 /* Create partition table flat image. */
3522 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
3523 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags | ((pExtent->enmAccess == VMDKACCESS_READONLY) ? VD_OPEN_FLAGS_READONLY : 0),
3524 true /* fCreate */));
3525 if (RT_FAILURE(rc))
3526 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new partition data file '%s'"), pExtent->pszFullname);
3527 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
3528 VMDK_SECTOR2BYTE(uPartOffset),
3529 pPart->pvPartitionData,
3530 pPart->cbData);
3531 if (RT_FAILURE(rc))
3532 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not write partition data to '%s'"), pExtent->pszFullname);
3533 uPartOffset += VMDK_BYTE2SECTOR(pPart->cbData);
3534 }
3535 else
3536 {
3537 if (pPart->pszRawDevice)
3538 {
3539 /* Set up basename for extent descr. Can't use StrDup. */
3540 size_t cbBasename = strlen(pPart->pszRawDevice) + 1;
3541 char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
3542 if (!pszBasename)
3543 return VERR_NO_MEMORY;
3544 memcpy(pszBasename, pPart->pszRawDevice, cbBasename);
3545 pExtent->pszBasename = pszBasename;
3546 /* For raw disks full name is identical to base name. */
3547 pExtent->pszFullname = RTStrDup(pszBasename);
3548 if (!pExtent->pszFullname)
3549 return VERR_NO_MEMORY;
3550 pExtent->enmType = VMDKETYPE_FLAT;
3551 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbData);
3552 pExtent->uSectorOffset = VMDK_BYTE2SECTOR(pPart->uStartOffset);
3553 pExtent->enmAccess = (pPart->uFlags & VDISKRAW_READONLY) ? VMDKACCESS_READONLY : VMDKACCESS_READWRITE;
3554 pExtent->fMetaDirty = false;
3555
3556 /* Open flat image, the raw partition. */
3557 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
3558 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags | ((pExtent->enmAccess == VMDKACCESS_READONLY) ? VD_OPEN_FLAGS_READONLY : 0),
3559 false /* fCreate */));
3560 if (RT_FAILURE(rc))
3561 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not open raw partition file '%s'"), pExtent->pszFullname);
3562 }
3563 else
3564 {
3565 pExtent->pszBasename = NULL;
3566 pExtent->pszFullname = NULL;
3567 pExtent->enmType = VMDKETYPE_ZERO;
3568 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbData);
3569 pExtent->uSectorOffset = 0;
3570 pExtent->enmAccess = VMDKACCESS_READWRITE;
3571 pExtent->fMetaDirty = false;
3572 }
3573 }
3574 }
3575 /* Another extent for filling up the rest of the image. */
3576 if (uStart != cbSize)
3577 {
3578 pExtent = &pImage->pExtents[cExtents++];
3579 pExtent->pszBasename = NULL;
3580 pExtent->pszFullname = NULL;
3581 pExtent->enmType = VMDKETYPE_ZERO;
3582 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize - uStart);
3583 pExtent->uSectorOffset = 0;
3584 pExtent->enmAccess = VMDKACCESS_READWRITE;
3585 pExtent->fMetaDirty = false;
3586 }
3587 }
3588
3589 rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType",
3590 (pRaw->uFlags & VDISKRAW_DISK) ?
3591 "fullDevice" : "partitionedDevice");
3592 if (RT_FAILURE(rc))
3593 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pImage->pszFilename);
3594 return rc;
3595}
3596
3597/**
3598 * Internal: create a regular (i.e. file-backed) VMDK image.
3599 */
3600static int vmdkCreateRegularImage(PVMDKIMAGE pImage, uint64_t cbSize,
3601 unsigned uImageFlags, PVDINTERFACEPROGRESS pIfProgress,
3602 unsigned uPercentStart, unsigned uPercentSpan)
3603{
3604 int rc = VINF_SUCCESS;
3605 unsigned cExtents = 1;
3606 uint64_t cbOffset = 0;
3607 uint64_t cbRemaining = cbSize;
3608
3609 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
3610 {
3611 cExtents = cbSize / VMDK_2G_SPLIT_SIZE;
3612 /* Do proper extent computation: need one smaller extent if the total
3613 * size isn't evenly divisible by the split size. */
3614 if (cbSize % VMDK_2G_SPLIT_SIZE)
3615 cExtents++;
3616 }
3617 rc = vmdkCreateExtents(pImage, cExtents);
3618 if (RT_FAILURE(rc))
3619 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
3620
3621 /* Basename strings needed for constructing the extent names. */
3622 char *pszBasenameSubstr = RTPathFilename(pImage->pszFilename);
3623 AssertPtr(pszBasenameSubstr);
3624 size_t cbBasenameSubstr = strlen(pszBasenameSubstr) + 1;
3625
3626 /* Create separate descriptor file if necessary. */
3627 if (cExtents != 1 || (uImageFlags & VD_IMAGE_FLAGS_FIXED))
3628 {
3629 rc = vmdkFileOpen(pImage, &pImage->pFile, pImage->pszFilename,
3630 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
3631 true /* fCreate */));
3632 if (RT_FAILURE(rc))
3633 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new sparse descriptor file '%s'"), pImage->pszFilename);
3634 }
3635 else
3636 pImage->pFile = NULL;
3637
3638 /* Set up all extents. */
3639 for (unsigned i = 0; i < cExtents; i++)
3640 {
3641 PVMDKEXTENT pExtent = &pImage->pExtents[i];
3642 uint64_t cbExtent = cbRemaining;
3643
3644 /* Set up fullname/basename for extent description. Cannot use StrDup
3645 * for basename, as it is not guaranteed that the memory can be freed
3646 * with RTMemTmpFree, which must be used as in other code paths
3647 * StrDup is not usable. */
3648 if (cExtents == 1 && !(uImageFlags & VD_IMAGE_FLAGS_FIXED))
3649 {
3650 char *pszBasename = (char *)RTMemTmpAlloc(cbBasenameSubstr);
3651 if (!pszBasename)
3652 return VERR_NO_MEMORY;
3653 memcpy(pszBasename, pszBasenameSubstr, cbBasenameSubstr);
3654 pExtent->pszBasename = pszBasename;
3655 }
3656 else
3657 {
3658 char *pszBasenameSuff = RTPathSuffix(pszBasenameSubstr);
3659 char *pszBasenameBase = RTStrDup(pszBasenameSubstr);
3660 RTPathStripSuffix(pszBasenameBase);
3661 char *pszTmp;
3662 size_t cbTmp;
3663 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
3664 {
3665 if (cExtents == 1)
3666 RTStrAPrintf(&pszTmp, "%s-flat%s", pszBasenameBase,
3667 pszBasenameSuff);
3668 else
3669 RTStrAPrintf(&pszTmp, "%s-f%03d%s", pszBasenameBase,
3670 i+1, pszBasenameSuff);
3671 }
3672 else
3673 RTStrAPrintf(&pszTmp, "%s-s%03d%s", pszBasenameBase, i+1,
3674 pszBasenameSuff);
3675 RTStrFree(pszBasenameBase);
3676 if (!pszTmp)
3677 return VERR_NO_STR_MEMORY;
3678 cbTmp = strlen(pszTmp) + 1;
3679 char *pszBasename = (char *)RTMemTmpAlloc(cbTmp);
3680 if (!pszBasename)
3681 {
3682 RTStrFree(pszTmp);
3683 return VERR_NO_MEMORY;
3684 }
3685 memcpy(pszBasename, pszTmp, cbTmp);
3686 RTStrFree(pszTmp);
3687 pExtent->pszBasename = pszBasename;
3688 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
3689 cbExtent = RT_MIN(cbRemaining, VMDK_2G_SPLIT_SIZE);
3690 }
3691 char *pszBasedirectory = RTStrDup(pImage->pszFilename);
3692 if (!pszBasedirectory)
3693 return VERR_NO_STR_MEMORY;
3694 RTPathStripFilename(pszBasedirectory);
3695 char *pszFullname = RTPathJoinA(pszBasedirectory, pExtent->pszBasename);
3696 RTStrFree(pszBasedirectory);
3697 if (!pszFullname)
3698 return VERR_NO_STR_MEMORY;
3699 pExtent->pszFullname = pszFullname;
3700
3701 /* Create file for extent. */
3702 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
3703 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
3704 true /* fCreate */));
3705 if (RT_FAILURE(rc))
3706 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pExtent->pszFullname);
3707 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
3708 {
3709 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage, cbExtent,
3710 0 /* fFlags */, pIfProgress,
3711 uPercentStart + cbOffset * uPercentSpan / cbSize,
3712 cbExtent * uPercentSpan / cbSize);
3713 if (RT_FAILURE(rc))
3714 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
3715 }
3716
3717 /* Place descriptor file information (where integrated). */
3718 if (cExtents == 1 && !(uImageFlags & VD_IMAGE_FLAGS_FIXED))
3719 {
3720 pExtent->uDescriptorSector = 1;
3721 pExtent->cDescriptorSectors = VMDK_BYTE2SECTOR(pImage->cbDescAlloc);
3722 /* The descriptor is part of the (only) extent. */
3723 pExtent->pDescData = pImage->pDescData;
3724 pImage->pDescData = NULL;
3725 }
3726
3727 if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
3728 {
3729 uint64_t cSectorsPerGDE, cSectorsPerGD;
3730 pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
3731 pExtent->cSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64(cbExtent, _64K));
3732 pExtent->cSectorsPerGrain = VMDK_BYTE2SECTOR(_64K);
3733 pExtent->cGTEntries = 512;
3734 cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
3735 pExtent->cSectorsPerGDE = cSectorsPerGDE;
3736 pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
3737 cSectorsPerGD = (pExtent->cGDEntries + (512 / sizeof(uint32_t) - 1)) / (512 / sizeof(uint32_t));
3738 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3739 {
3740 /* The spec says version is 1 for all VMDKs, but the vast
3741 * majority of streamOptimized VMDKs actually contain
3742 * version 3 - so go with the majority. Both are accepted. */
3743 pExtent->uVersion = 3;
3744 pExtent->uCompression = VMDK_COMPRESSION_DEFLATE;
3745 }
3746 }
3747 else
3748 {
3749 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
3750 pExtent->enmType = VMDKETYPE_VMFS;
3751 else
3752 pExtent->enmType = VMDKETYPE_FLAT;
3753 }
3754
3755 pExtent->enmAccess = VMDKACCESS_READWRITE;
3756 pExtent->fUncleanShutdown = true;
3757 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbExtent);
3758 pExtent->uSectorOffset = 0;
3759 pExtent->fMetaDirty = true;
3760
3761 if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
3762 {
3763 /* fPreAlloc should never be false because VMware can't use such images. */
3764 rc = vmdkCreateGrainDirectory(pImage, pExtent,
3765 RT_MAX( pExtent->uDescriptorSector
3766 + pExtent->cDescriptorSectors,
3767 1),
3768 true /* fPreAlloc */);
3769 if (RT_FAILURE(rc))
3770 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new grain directory in '%s'"), pExtent->pszFullname);
3771 }
3772
3773 cbOffset += cbExtent;
3774
3775 if (RT_SUCCESS(rc))
3776 vdIfProgress(pIfProgress, uPercentStart + cbOffset * uPercentSpan / cbSize);
3777
3778 cbRemaining -= cbExtent;
3779 }
3780
3781 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
3782 {
3783 /* VirtualBox doesn't care, but VMWare ESX freaks out if the wrong
3784 * controller type is set in an image. */
3785 rc = vmdkDescDDBSetStr(pImage, &pImage->Descriptor, "ddb.adapterType", "lsilogic");
3786 if (RT_FAILURE(rc))
3787 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set controller type to lsilogic in '%s'"), pImage->pszFilename);
3788 }
3789
3790 const char *pszDescType = NULL;
3791 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
3792 {
3793 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
3794 pszDescType = "vmfs";
3795 else
3796 pszDescType = (cExtents == 1)
3797 ? "monolithicFlat" : "twoGbMaxExtentFlat";
3798 }
3799 else
3800 {
3801 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3802 pszDescType = "streamOptimized";
3803 else
3804 {
3805 pszDescType = (cExtents == 1)
3806 ? "monolithicSparse" : "twoGbMaxExtentSparse";
3807 }
3808 }
3809 rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType",
3810 pszDescType);
3811 if (RT_FAILURE(rc))
3812 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pImage->pszFilename);
3813 return rc;
3814}
3815
3816/**
3817 * Internal: Create a real stream optimized VMDK using only linear writes.
3818 */
3819static int vmdkCreateStreamImage(PVMDKIMAGE pImage, uint64_t cbSize)
3820{
3821 int rc = vmdkCreateExtents(pImage, 1);
3822 if (RT_FAILURE(rc))
3823 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
3824
3825 /* Basename strings needed for constructing the extent names. */
3826 const char *pszBasenameSubstr = RTPathFilename(pImage->pszFilename);
3827 AssertPtr(pszBasenameSubstr);
3828 size_t cbBasenameSubstr = strlen(pszBasenameSubstr) + 1;
3829
3830 /* No separate descriptor file. */
3831 pImage->pFile = NULL;
3832
3833 /* Set up all extents. */
3834 PVMDKEXTENT pExtent = &pImage->pExtents[0];
3835
3836 /* Set up fullname/basename for extent description. Cannot use StrDup
3837 * for basename, as it is not guaranteed that the memory can be freed
3838 * with RTMemTmpFree, which must be used as in other code paths
3839 * StrDup is not usable. */
3840 char *pszBasename = (char *)RTMemTmpAlloc(cbBasenameSubstr);
3841 if (!pszBasename)
3842 return VERR_NO_MEMORY;
3843 memcpy(pszBasename, pszBasenameSubstr, cbBasenameSubstr);
3844 pExtent->pszBasename = pszBasename;
3845
3846 char *pszBasedirectory = RTStrDup(pImage->pszFilename);
3847 RTPathStripFilename(pszBasedirectory);
3848 char *pszFullname = RTPathJoinA(pszBasedirectory, pExtent->pszBasename);
3849 RTStrFree(pszBasedirectory);
3850 if (!pszFullname)
3851 return VERR_NO_STR_MEMORY;
3852 pExtent->pszFullname = pszFullname;
3853
3854 /* Create file for extent. Make it write only, no reading allowed. */
3855 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
3856 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
3857 true /* fCreate */)
3858 & ~RTFILE_O_READ);
3859 if (RT_FAILURE(rc))
3860 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pExtent->pszFullname);
3861
3862 /* Place descriptor file information. */
3863 pExtent->uDescriptorSector = 1;
3864 pExtent->cDescriptorSectors = VMDK_BYTE2SECTOR(pImage->cbDescAlloc);
3865 /* The descriptor is part of the (only) extent. */
3866 pExtent->pDescData = pImage->pDescData;
3867 pImage->pDescData = NULL;
3868
3869 uint64_t cSectorsPerGDE, cSectorsPerGD;
3870 pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
3871 pExtent->cSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64(cbSize, _64K));
3872 pExtent->cSectorsPerGrain = VMDK_BYTE2SECTOR(_64K);
3873 pExtent->cGTEntries = 512;
3874 cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
3875 pExtent->cSectorsPerGDE = cSectorsPerGDE;
3876 pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
3877 cSectorsPerGD = (pExtent->cGDEntries + (512 / sizeof(uint32_t) - 1)) / (512 / sizeof(uint32_t));
3878
3879 /* The spec says version is 1 for all VMDKs, but the vast
3880 * majority of streamOptimized VMDKs actually contain
3881 * version 3 - so go with the majority. Both are accepted. */
3882 pExtent->uVersion = 3;
3883 pExtent->uCompression = VMDK_COMPRESSION_DEFLATE;
3884 pExtent->fFooter = true;
3885
3886 pExtent->enmAccess = VMDKACCESS_READONLY;
3887 pExtent->fUncleanShutdown = false;
3888 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
3889 pExtent->uSectorOffset = 0;
3890 pExtent->fMetaDirty = true;
3891
3892 /* Create grain directory, without preallocating it straight away. It will
3893 * be constructed on the fly when writing out the data and written when
3894 * closing the image. The end effect is that the full grain directory is
3895 * allocated, which is a requirement of the VMDK specs. */
3896 rc = vmdkCreateGrainDirectory(pImage, pExtent, VMDK_GD_AT_END,
3897 false /* fPreAlloc */);
3898 if (RT_FAILURE(rc))
3899 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new grain directory in '%s'"), pExtent->pszFullname);
3900
3901 rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType",
3902 "streamOptimized");
3903 if (RT_FAILURE(rc))
3904 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pImage->pszFilename);
3905
3906 return rc;
3907}
3908
3909/**
3910 * Initializes the UUID fields in the DDB.
3911 *
3912 * @returns VBox status code.
3913 * @param pImage The VMDK image instance.
3914 */
3915static int vmdkCreateImageDdbUuidsInit(PVMDKIMAGE pImage)
3916{
3917 int rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor, VMDK_DDB_IMAGE_UUID, &pImage->ImageUuid);
3918 if (RT_SUCCESS(rc))
3919 {
3920 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor, VMDK_DDB_PARENT_UUID, &pImage->ParentUuid);
3921 if (RT_SUCCESS(rc))
3922 {
3923 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor, VMDK_DDB_MODIFICATION_UUID,
3924 &pImage->ModificationUuid);
3925 if (RT_SUCCESS(rc))
3926 {
3927 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor, VMDK_DDB_PARENT_MODIFICATION_UUID,
3928 &pImage->ParentModificationUuid);
3929 if (RT_FAILURE(rc))
3930 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
3931 N_("VMDK: error storing parent modification UUID in new descriptor in '%s'"), pImage->pszFilename);
3932 }
3933 else
3934 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
3935 N_("VMDK: error storing modification UUID in new descriptor in '%s'"), pImage->pszFilename);
3936 }
3937 else
3938 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
3939 N_("VMDK: error storing parent image UUID in new descriptor in '%s'"), pImage->pszFilename);
3940 }
3941 else
3942 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
3943 N_("VMDK: error storing image UUID in new descriptor in '%s'"), pImage->pszFilename);
3944
3945 return rc;
3946}
3947
3948/**
3949 * Internal: The actual code for creating any VMDK variant currently in
3950 * existence on hosted environments.
3951 */
3952static int vmdkCreateImage(PVMDKIMAGE pImage, uint64_t cbSize,
3953 unsigned uImageFlags, const char *pszComment,
3954 PCVDGEOMETRY pPCHSGeometry,
3955 PCVDGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
3956 PVDINTERFACEPROGRESS pIfProgress,
3957 unsigned uPercentStart, unsigned uPercentSpan)
3958{
3959 pImage->uImageFlags = uImageFlags;
3960
3961 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
3962 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
3963 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
3964
3965 int rc = vmdkCreateDescriptor(pImage, pImage->pDescData, pImage->cbDescAlloc,
3966 &pImage->Descriptor);
3967 if (RT_SUCCESS(rc))
3968 {
3969 if ( (uImageFlags & VD_IMAGE_FLAGS_FIXED)
3970 && (uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK))
3971 {
3972 /* Raw disk image (includes raw partition). */
3973 const PVDISKRAW pRaw = (const PVDISKRAW)pszComment;
3974 /* As the comment is misused, zap it so that no garbage comment
3975 * is set below. */
3976 pszComment = NULL;
3977 rc = vmdkCreateRawImage(pImage, pRaw, cbSize);
3978 }
3979 else if (uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3980 {
3981 /* Stream optimized sparse image (monolithic). */
3982 rc = vmdkCreateStreamImage(pImage, cbSize);
3983 }
3984 else
3985 {
3986 /* Regular fixed or sparse image (monolithic or split). */
3987 rc = vmdkCreateRegularImage(pImage, cbSize, uImageFlags,
3988 pIfProgress, uPercentStart,
3989 uPercentSpan * 95 / 100);
3990 }
3991
3992 if (RT_SUCCESS(rc))
3993 {
3994 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan * 98 / 100);
3995
3996 pImage->cbSize = cbSize;
3997
3998 for (unsigned i = 0; i < pImage->cExtents; i++)
3999 {
4000 PVMDKEXTENT pExtent = &pImage->pExtents[i];
4001
4002 rc = vmdkDescExtInsert(pImage, &pImage->Descriptor, pExtent->enmAccess,
4003 pExtent->cNominalSectors, pExtent->enmType,
4004 pExtent->pszBasename, pExtent->uSectorOffset);
4005 if (RT_FAILURE(rc))
4006 {
4007 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not insert the extent list into descriptor in '%s'"), pImage->pszFilename);
4008 break;
4009 }
4010 }
4011
4012 if (RT_SUCCESS(rc))
4013 vmdkDescExtRemoveDummy(pImage, &pImage->Descriptor);
4014
4015 if ( RT_SUCCESS(rc)
4016 && pPCHSGeometry->cCylinders != 0
4017 && pPCHSGeometry->cHeads != 0
4018 && pPCHSGeometry->cSectors != 0)
4019 rc = vmdkDescSetPCHSGeometry(pImage, pPCHSGeometry);
4020
4021 if ( RT_SUCCESS(rc)
4022 && pLCHSGeometry->cCylinders != 0
4023 && pLCHSGeometry->cHeads != 0
4024 && pLCHSGeometry->cSectors != 0)
4025 rc = vmdkDescSetLCHSGeometry(pImage, pLCHSGeometry);
4026
4027 pImage->LCHSGeometry = *pLCHSGeometry;
4028 pImage->PCHSGeometry = *pPCHSGeometry;
4029
4030 pImage->ImageUuid = *pUuid;
4031 RTUuidClear(&pImage->ParentUuid);
4032 RTUuidClear(&pImage->ModificationUuid);
4033 RTUuidClear(&pImage->ParentModificationUuid);
4034
4035 if (RT_SUCCESS(rc))
4036 rc = vmdkCreateImageDdbUuidsInit(pImage);
4037
4038 if (RT_SUCCESS(rc))
4039 rc = vmdkAllocateGrainTableCache(pImage);
4040
4041 if (RT_SUCCESS(rc))
4042 {
4043 rc = vmdkSetImageComment(pImage, pszComment);
4044 if (RT_FAILURE(rc))
4045 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot set image comment in '%s'"), pImage->pszFilename);
4046 }
4047
4048 if (RT_SUCCESS(rc))
4049 {
4050 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan * 99 / 100);
4051
4052 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
4053 {
4054 /* streamOptimized is a bit special, we cannot trigger the flush
4055 * until all data has been written. So we write the necessary
4056 * information explicitly. */
4057 pImage->pExtents[0].cDescriptorSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64( pImage->Descriptor.aLines[pImage->Descriptor.cLines]
4058 - pImage->Descriptor.aLines[0], 512));
4059 rc = vmdkWriteMetaSparseExtent(pImage, &pImage->pExtents[0], 0, NULL);
4060 if (RT_SUCCESS(rc))
4061 {
4062 rc = vmdkWriteDescriptor(pImage, NULL);
4063 if (RT_FAILURE(rc))
4064 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write VMDK descriptor in '%s'"), pImage->pszFilename);
4065 }
4066 else
4067 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write VMDK header in '%s'"), pImage->pszFilename);
4068 }
4069 else
4070 rc = vmdkFlushImage(pImage, NULL);
4071 }
4072 }
4073 }
4074 else
4075 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new descriptor in '%s'"), pImage->pszFilename);
4076
4077
4078 if (RT_SUCCESS(rc))
4079 {
4080 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
4081 pImage->RegionList.fFlags = 0;
4082 pImage->RegionList.cRegions = 1;
4083
4084 pRegion->offRegion = 0; /* Disk start. */
4085 pRegion->cbBlock = 512;
4086 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
4087 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
4088 pRegion->cbData = 512;
4089 pRegion->cbMetadata = 0;
4090 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
4091
4092 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
4093 }
4094 else
4095 vmdkFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
4096 return rc;
4097}
4098
4099/**
4100 * Internal: Update image comment.
4101 */
4102static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment)
4103{
4104 char *pszCommentEncoded = NULL;
4105 if (pszComment)
4106 {
4107 pszCommentEncoded = vmdkEncodeString(pszComment);
4108 if (!pszCommentEncoded)
4109 return VERR_NO_MEMORY;
4110 }
4111
4112 int rc = vmdkDescDDBSetStr(pImage, &pImage->Descriptor,
4113 "ddb.comment", pszCommentEncoded);
4114 if (pszCommentEncoded)
4115 RTStrFree(pszCommentEncoded);
4116 if (RT_FAILURE(rc))
4117 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image comment in descriptor in '%s'"), pImage->pszFilename);
4118 return VINF_SUCCESS;
4119}
4120
4121/**
4122 * Internal. Clear the grain table buffer for real stream optimized writing.
4123 */
4124static void vmdkStreamClearGT(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
4125{
4126 uint32_t cCacheLines = RT_ALIGN(pExtent->cGTEntries, VMDK_GT_CACHELINE_SIZE) / VMDK_GT_CACHELINE_SIZE;
4127 for (uint32_t i = 0; i < cCacheLines; i++)
4128 memset(&pImage->pGTCache->aGTCache[i].aGTData[0], '\0',
4129 VMDK_GT_CACHELINE_SIZE * sizeof(uint32_t));
4130}
4131
4132/**
4133 * Internal. Flush the grain table buffer for real stream optimized writing.
4134 */
4135static int vmdkStreamFlushGT(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
4136 uint32_t uGDEntry)
4137{
4138 int rc = VINF_SUCCESS;
4139 uint32_t cCacheLines = RT_ALIGN(pExtent->cGTEntries, VMDK_GT_CACHELINE_SIZE) / VMDK_GT_CACHELINE_SIZE;
4140
4141 /* VMware does not write out completely empty grain tables in the case
4142 * of streamOptimized images, which according to my interpretation of
4143 * the VMDK 1.1 spec is bending the rules. Since they do it and we can
4144 * handle it without problems do it the same way and save some bytes. */
4145 bool fAllZero = true;
4146 for (uint32_t i = 0; i < cCacheLines; i++)
4147 {
4148 /* Convert the grain table to little endian in place, as it will not
4149 * be used at all after this function has been called. */
4150 uint32_t *pGTTmp = &pImage->pGTCache->aGTCache[i].aGTData[0];
4151 for (uint32_t j = 0; j < VMDK_GT_CACHELINE_SIZE; j++, pGTTmp++)
4152 if (*pGTTmp)
4153 {
4154 fAllZero = false;
4155 break;
4156 }
4157 if (!fAllZero)
4158 break;
4159 }
4160 if (fAllZero)
4161 return VINF_SUCCESS;
4162
4163 uint64_t uFileOffset = pExtent->uAppendPosition;
4164 if (!uFileOffset)
4165 return VERR_INTERNAL_ERROR;
4166 /* Align to sector, as the previous write could have been any size. */
4167 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
4168
4169 /* Grain table marker. */
4170 uint8_t aMarker[512];
4171 PVMDKMARKER pMarker = (PVMDKMARKER)&aMarker[0];
4172 memset(pMarker, '\0', sizeof(aMarker));
4173 pMarker->uSector = RT_H2LE_U64(VMDK_BYTE2SECTOR((uint64_t)pExtent->cGTEntries * sizeof(uint32_t)));
4174 pMarker->uType = RT_H2LE_U32(VMDK_MARKER_GT);
4175 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage, uFileOffset,
4176 aMarker, sizeof(aMarker));
4177 AssertRC(rc);
4178 uFileOffset += 512;
4179
4180 if (!pExtent->pGD || pExtent->pGD[uGDEntry])
4181 return VERR_INTERNAL_ERROR;
4182
4183 pExtent->pGD[uGDEntry] = VMDK_BYTE2SECTOR(uFileOffset);
4184
4185 for (uint32_t i = 0; i < cCacheLines; i++)
4186 {
4187 /* Convert the grain table to little endian in place, as it will not
4188 * be used at all after this function has been called. */
4189 uint32_t *pGTTmp = &pImage->pGTCache->aGTCache[i].aGTData[0];
4190 for (uint32_t j = 0; j < VMDK_GT_CACHELINE_SIZE; j++, pGTTmp++)
4191 *pGTTmp = RT_H2LE_U32(*pGTTmp);
4192
4193 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage, uFileOffset,
4194 &pImage->pGTCache->aGTCache[i].aGTData[0],
4195 VMDK_GT_CACHELINE_SIZE * sizeof(uint32_t));
4196 uFileOffset += VMDK_GT_CACHELINE_SIZE * sizeof(uint32_t);
4197 if (RT_FAILURE(rc))
4198 break;
4199 }
4200 Assert(!(uFileOffset % 512));
4201 pExtent->uAppendPosition = RT_ALIGN_64(uFileOffset, 512);
4202 return rc;
4203}
4204
4205/**
4206 * Internal. Free all allocated space for representing an image, and optionally
4207 * delete the image from disk.
4208 */
4209static int vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete)
4210{
4211 int rc = VINF_SUCCESS;
4212
4213 /* Freeing a never allocated image (e.g. because the open failed) is
4214 * not signalled as an error. After all nothing bad happens. */
4215 if (pImage)
4216 {
4217 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
4218 {
4219 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
4220 {
4221 /* Check if all extents are clean. */
4222 for (unsigned i = 0; i < pImage->cExtents; i++)
4223 {
4224 Assert(!pImage->pExtents[i].fUncleanShutdown);
4225 }
4226 }
4227 else
4228 {
4229 /* Mark all extents as clean. */
4230 for (unsigned i = 0; i < pImage->cExtents; i++)
4231 {
4232 if ( pImage->pExtents[i].enmType == VMDKETYPE_HOSTED_SPARSE
4233 && pImage->pExtents[i].fUncleanShutdown)
4234 {
4235 pImage->pExtents[i].fUncleanShutdown = false;
4236 pImage->pExtents[i].fMetaDirty = true;
4237 }
4238
4239 /* From now on it's not safe to append any more data. */
4240 pImage->pExtents[i].uAppendPosition = 0;
4241 }
4242 }
4243 }
4244
4245 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
4246 {
4247 /* No need to write any pending data if the file will be deleted
4248 * or if the new file wasn't successfully created. */
4249 if ( !fDelete && pImage->pExtents
4250 && pImage->pExtents[0].cGTEntries
4251 && pImage->pExtents[0].uAppendPosition)
4252 {
4253 PVMDKEXTENT pExtent = &pImage->pExtents[0];
4254 uint32_t uLastGDEntry = pExtent->uLastGrainAccess / pExtent->cGTEntries;
4255 rc = vmdkStreamFlushGT(pImage, pExtent, uLastGDEntry);
4256 AssertRC(rc);
4257 vmdkStreamClearGT(pImage, pExtent);
4258 for (uint32_t i = uLastGDEntry + 1; i < pExtent->cGDEntries; i++)
4259 {
4260 rc = vmdkStreamFlushGT(pImage, pExtent, i);
4261 AssertRC(rc);
4262 }
4263
4264 uint64_t uFileOffset = pExtent->uAppendPosition;
4265 if (!uFileOffset)
4266 return VERR_INTERNAL_ERROR;
4267 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
4268
4269 /* From now on it's not safe to append any more data. */
4270 pExtent->uAppendPosition = 0;
4271
4272 /* Grain directory marker. */
4273 uint8_t aMarker[512];
4274 PVMDKMARKER pMarker = (PVMDKMARKER)&aMarker[0];
4275 memset(pMarker, '\0', sizeof(aMarker));
4276 pMarker->uSector = VMDK_BYTE2SECTOR(RT_ALIGN_64(RT_H2LE_U64((uint64_t)pExtent->cGDEntries * sizeof(uint32_t)), 512));
4277 pMarker->uType = RT_H2LE_U32(VMDK_MARKER_GD);
4278 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage, uFileOffset,
4279 aMarker, sizeof(aMarker));
4280 AssertRC(rc);
4281 uFileOffset += 512;
4282
4283 /* Write grain directory in little endian style. The array will
4284 * not be used after this, so convert in place. */
4285 uint32_t *pGDTmp = pExtent->pGD;
4286 for (uint32_t i = 0; i < pExtent->cGDEntries; i++, pGDTmp++)
4287 *pGDTmp = RT_H2LE_U32(*pGDTmp);
4288 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
4289 uFileOffset, pExtent->pGD,
4290 pExtent->cGDEntries * sizeof(uint32_t));
4291 AssertRC(rc);
4292
4293 pExtent->uSectorGD = VMDK_BYTE2SECTOR(uFileOffset);
4294 pExtent->uSectorRGD = VMDK_BYTE2SECTOR(uFileOffset);
4295 uFileOffset = RT_ALIGN_64( uFileOffset
4296 + pExtent->cGDEntries * sizeof(uint32_t),
4297 512);
4298
4299 /* Footer marker. */
4300 memset(pMarker, '\0', sizeof(aMarker));
4301 pMarker->uSector = VMDK_BYTE2SECTOR(512);
4302 pMarker->uType = RT_H2LE_U32(VMDK_MARKER_FOOTER);
4303 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
4304 uFileOffset, aMarker, sizeof(aMarker));
4305 AssertRC(rc);
4306
4307 uFileOffset += 512;
4308 rc = vmdkWriteMetaSparseExtent(pImage, pExtent, uFileOffset, NULL);
4309 AssertRC(rc);
4310
4311 uFileOffset += 512;
4312 /* End-of-stream marker. */
4313 memset(pMarker, '\0', sizeof(aMarker));
4314 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
4315 uFileOffset, aMarker, sizeof(aMarker));
4316 AssertRC(rc);
4317 }
4318 }
4319 else if (!fDelete)
4320 vmdkFlushImage(pImage, NULL);
4321
4322 if (pImage->pExtents != NULL)
4323 {
4324 for (unsigned i = 0 ; i < pImage->cExtents; i++)
4325 {
4326 int rc2 = vmdkFreeExtentData(pImage, &pImage->pExtents[i], fDelete);
4327 if (RT_SUCCESS(rc))
4328 rc = rc2; /* Propogate any error when closing the file. */
4329 }
4330 RTMemFree(pImage->pExtents);
4331 pImage->pExtents = NULL;
4332 }
4333 pImage->cExtents = 0;
4334 if (pImage->pFile != NULL)
4335 {
4336 int rc2 = vmdkFileClose(pImage, &pImage->pFile, fDelete);
4337 if (RT_SUCCESS(rc))
4338 rc = rc2; /* Propogate any error when closing the file. */
4339 }
4340 int rc2 = vmdkFileCheckAllClose(pImage);
4341 if (RT_SUCCESS(rc))
4342 rc = rc2; /* Propogate any error when closing the file. */
4343
4344 if (pImage->pGTCache)
4345 {
4346 RTMemFree(pImage->pGTCache);
4347 pImage->pGTCache = NULL;
4348 }
4349 if (pImage->pDescData)
4350 {
4351 RTMemFree(pImage->pDescData);
4352 pImage->pDescData = NULL;
4353 }
4354 }
4355
4356 LogFlowFunc(("returns %Rrc\n", rc));
4357 return rc;
4358}
4359
4360/**
4361 * Internal. Flush image data (and metadata) to disk.
4362 */
4363static int vmdkFlushImage(PVMDKIMAGE pImage, PVDIOCTX pIoCtx)
4364{
4365 PVMDKEXTENT pExtent;
4366 int rc = VINF_SUCCESS;
4367
4368 /* Update descriptor if changed. */
4369 if (pImage->Descriptor.fDirty)
4370 rc = vmdkWriteDescriptor(pImage, pIoCtx);
4371
4372 if (RT_SUCCESS(rc))
4373 {
4374 for (unsigned i = 0; i < pImage->cExtents; i++)
4375 {
4376 pExtent = &pImage->pExtents[i];
4377 if (pExtent->pFile != NULL && pExtent->fMetaDirty)
4378 {
4379 switch (pExtent->enmType)
4380 {
4381 case VMDKETYPE_HOSTED_SPARSE:
4382 if (!pExtent->fFooter)
4383 rc = vmdkWriteMetaSparseExtent(pImage, pExtent, 0, pIoCtx);
4384 else
4385 {
4386 uint64_t uFileOffset = pExtent->uAppendPosition;
4387 /* Simply skip writing anything if the streamOptimized
4388 * image hasn't been just created. */
4389 if (!uFileOffset)
4390 break;
4391 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
4392 rc = vmdkWriteMetaSparseExtent(pImage, pExtent,
4393 uFileOffset, pIoCtx);
4394 }
4395 break;
4396 case VMDKETYPE_VMFS:
4397 case VMDKETYPE_FLAT:
4398 /* Nothing to do. */
4399 break;
4400 case VMDKETYPE_ZERO:
4401 default:
4402 AssertMsgFailed(("extent with type %d marked as dirty\n",
4403 pExtent->enmType));
4404 break;
4405 }
4406 }
4407
4408 if (RT_FAILURE(rc))
4409 break;
4410
4411 switch (pExtent->enmType)
4412 {
4413 case VMDKETYPE_HOSTED_SPARSE:
4414 case VMDKETYPE_VMFS:
4415 case VMDKETYPE_FLAT:
4416 /** @todo implement proper path absolute check. */
4417 if ( pExtent->pFile != NULL
4418 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
4419 && !(pExtent->pszBasename[0] == RTPATH_SLASH))
4420 rc = vdIfIoIntFileFlush(pImage->pIfIo, pExtent->pFile->pStorage, pIoCtx,
4421 NULL, NULL);
4422 break;
4423 case VMDKETYPE_ZERO:
4424 /* No need to do anything for this extent. */
4425 break;
4426 default:
4427 AssertMsgFailed(("unknown extent type %d\n", pExtent->enmType));
4428 break;
4429 }
4430 }
4431 }
4432
4433 return rc;
4434}
4435
4436/**
4437 * Internal. Find extent corresponding to the sector number in the disk.
4438 */
4439static int vmdkFindExtent(PVMDKIMAGE pImage, uint64_t offSector,
4440 PVMDKEXTENT *ppExtent, uint64_t *puSectorInExtent)
4441{
4442 PVMDKEXTENT pExtent = NULL;
4443 int rc = VINF_SUCCESS;
4444
4445 for (unsigned i = 0; i < pImage->cExtents; i++)
4446 {
4447 if (offSector < pImage->pExtents[i].cNominalSectors)
4448 {
4449 pExtent = &pImage->pExtents[i];
4450 *puSectorInExtent = offSector + pImage->pExtents[i].uSectorOffset;
4451 break;
4452 }
4453 offSector -= pImage->pExtents[i].cNominalSectors;
4454 }
4455
4456 if (pExtent)
4457 *ppExtent = pExtent;
4458 else
4459 rc = VERR_IO_SECTOR_NOT_FOUND;
4460
4461 return rc;
4462}
4463
4464/**
4465 * Internal. Hash function for placing the grain table hash entries.
4466 */
4467static uint32_t vmdkGTCacheHash(PVMDKGTCACHE pCache, uint64_t uSector,
4468 unsigned uExtent)
4469{
4470 /** @todo this hash function is quite simple, maybe use a better one which
4471 * scrambles the bits better. */
4472 return (uSector + uExtent) % pCache->cEntries;
4473}
4474
4475/**
4476 * Internal. Get sector number in the extent file from the relative sector
4477 * number in the extent.
4478 */
4479static int vmdkGetSector(PVMDKIMAGE pImage, PVDIOCTX pIoCtx,
4480 PVMDKEXTENT pExtent, uint64_t uSector,
4481 uint64_t *puExtentSector)
4482{
4483 PVMDKGTCACHE pCache = pImage->pGTCache;
4484 uint64_t uGDIndex, uGTSector, uGTBlock;
4485 uint32_t uGTHash, uGTBlockIndex;
4486 PVMDKGTCACHEENTRY pGTCacheEntry;
4487 uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
4488 int rc;
4489
4490 /* For newly created and readonly/sequentially opened streamOptimized
4491 * images this must be a no-op, as the grain directory is not there. */
4492 if ( ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
4493 && pExtent->uAppendPosition)
4494 || ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
4495 && pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY
4496 && pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
4497 {
4498 *puExtentSector = 0;
4499 return VINF_SUCCESS;
4500 }
4501
4502 uGDIndex = uSector / pExtent->cSectorsPerGDE;
4503 if (uGDIndex >= pExtent->cGDEntries)
4504 return VERR_OUT_OF_RANGE;
4505 uGTSector = pExtent->pGD[uGDIndex];
4506 if (!uGTSector)
4507 {
4508 /* There is no grain table referenced by this grain directory
4509 * entry. So there is absolutely no data in this area. */
4510 *puExtentSector = 0;
4511 return VINF_SUCCESS;
4512 }
4513
4514 uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
4515 uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
4516 pGTCacheEntry = &pCache->aGTCache[uGTHash];
4517 if ( pGTCacheEntry->uExtent != pExtent->uExtent
4518 || pGTCacheEntry->uGTBlock != uGTBlock)
4519 {
4520 /* Cache miss, fetch data from disk. */
4521 PVDMETAXFER pMetaXfer;
4522 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pExtent->pFile->pStorage,
4523 VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
4524 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx, &pMetaXfer, NULL, NULL);
4525 if (RT_FAILURE(rc))
4526 return rc;
4527 /* We can release the metadata transfer immediately. */
4528 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
4529 pGTCacheEntry->uExtent = pExtent->uExtent;
4530 pGTCacheEntry->uGTBlock = uGTBlock;
4531 for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
4532 pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
4533 }
4534 uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
4535 uint32_t uGrainSector = pGTCacheEntry->aGTData[uGTBlockIndex];
4536 if (uGrainSector)
4537 *puExtentSector = uGrainSector + uSector % pExtent->cSectorsPerGrain;
4538 else
4539 *puExtentSector = 0;
4540 return VINF_SUCCESS;
4541}
4542
4543/**
4544 * Internal. Writes the grain and also if necessary the grain tables.
4545 * Uses the grain table cache as a true grain table.
4546 */
4547static int vmdkStreamAllocGrain(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
4548 uint64_t uSector, PVDIOCTX pIoCtx,
4549 uint64_t cbWrite)
4550{
4551 uint32_t uGrain;
4552 uint32_t uGDEntry, uLastGDEntry;
4553 uint32_t cbGrain = 0;
4554 uint32_t uCacheLine, uCacheEntry;
4555 const void *pData;
4556 int rc;
4557
4558 /* Very strict requirements: always write at least one full grain, with
4559 * proper alignment. Everything else would require reading of already
4560 * written data, which we don't support for obvious reasons. The only
4561 * exception is the last grain, and only if the image size specifies
4562 * that only some portion holds data. In any case the write must be
4563 * within the image limits, no "overshoot" allowed. */
4564 if ( cbWrite == 0
4565 || ( cbWrite < VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
4566 && pExtent->cNominalSectors - uSector >= pExtent->cSectorsPerGrain)
4567 || uSector % pExtent->cSectorsPerGrain
4568 || uSector + VMDK_BYTE2SECTOR(cbWrite) > pExtent->cNominalSectors)
4569 return VERR_INVALID_PARAMETER;
4570
4571 /* Clip write range to at most the rest of the grain. */
4572 cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSector % pExtent->cSectorsPerGrain));
4573
4574 /* Do not allow to go back. */
4575 uGrain = uSector / pExtent->cSectorsPerGrain;
4576 uCacheLine = uGrain % pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE;
4577 uCacheEntry = uGrain % VMDK_GT_CACHELINE_SIZE;
4578 uGDEntry = uGrain / pExtent->cGTEntries;
4579 uLastGDEntry = pExtent->uLastGrainAccess / pExtent->cGTEntries;
4580 if (uGrain < pExtent->uLastGrainAccess)
4581 return VERR_VD_VMDK_INVALID_WRITE;
4582
4583 /* Zero byte write optimization. Since we don't tell VBoxHDD that we need
4584 * to allocate something, we also need to detect the situation ourself. */
4585 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
4586 && vdIfIoIntIoCtxIsZero(pImage->pIfIo, pIoCtx, cbWrite, true /* fAdvance */))
4587 return VINF_SUCCESS;
4588
4589 if (uGDEntry != uLastGDEntry)
4590 {
4591 rc = vmdkStreamFlushGT(pImage, pExtent, uLastGDEntry);
4592 if (RT_FAILURE(rc))
4593 return rc;
4594 vmdkStreamClearGT(pImage, pExtent);
4595 for (uint32_t i = uLastGDEntry + 1; i < uGDEntry; i++)
4596 {
4597 rc = vmdkStreamFlushGT(pImage, pExtent, i);
4598 if (RT_FAILURE(rc))
4599 return rc;
4600 }
4601 }
4602
4603 uint64_t uFileOffset;
4604 uFileOffset = pExtent->uAppendPosition;
4605 if (!uFileOffset)
4606 return VERR_INTERNAL_ERROR;
4607 /* Align to sector, as the previous write could have been any size. */
4608 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
4609
4610 /* Paranoia check: extent type, grain table buffer presence and
4611 * grain table buffer space. Also grain table entry must be clear. */
4612 if ( pExtent->enmType != VMDKETYPE_HOSTED_SPARSE
4613 || !pImage->pGTCache
4614 || pExtent->cGTEntries > VMDK_GT_CACHE_SIZE * VMDK_GT_CACHELINE_SIZE
4615 || pImage->pGTCache->aGTCache[uCacheLine].aGTData[uCacheEntry])
4616 return VERR_INTERNAL_ERROR;
4617
4618 /* Update grain table entry. */
4619 pImage->pGTCache->aGTCache[uCacheLine].aGTData[uCacheEntry] = VMDK_BYTE2SECTOR(uFileOffset);
4620
4621 if (cbWrite != VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
4622 {
4623 vdIfIoIntIoCtxCopyFrom(pImage->pIfIo, pIoCtx, pExtent->pvGrain, cbWrite);
4624 memset((char *)pExtent->pvGrain + cbWrite, '\0',
4625 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain) - cbWrite);
4626 pData = pExtent->pvGrain;
4627 }
4628 else
4629 {
4630 RTSGSEG Segment;
4631 unsigned cSegments = 1;
4632 size_t cbSeg = 0;
4633
4634 cbSeg = vdIfIoIntIoCtxSegArrayCreate(pImage->pIfIo, pIoCtx, &Segment,
4635 &cSegments, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
4636 Assert(cbSeg == VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
4637 pData = Segment.pvSeg;
4638 }
4639 rc = vmdkFileDeflateSync(pImage, pExtent, uFileOffset, pData,
4640 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
4641 uSector, &cbGrain);
4642 if (RT_FAILURE(rc))
4643 {
4644 pExtent->uGrainSectorAbs = 0;
4645 AssertRC(rc);
4646 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write compressed data block in '%s'"), pExtent->pszFullname);
4647 }
4648 pExtent->uLastGrainAccess = uGrain;
4649 pExtent->uAppendPosition += cbGrain;
4650
4651 return rc;
4652}
4653
4654/**
4655 * Internal: Updates the grain table during grain allocation.
4656 */
4657static int vmdkAllocGrainGTUpdate(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, PVDIOCTX pIoCtx,
4658 PVMDKGRAINALLOCASYNC pGrainAlloc)
4659{
4660 int rc = VINF_SUCCESS;
4661 PVMDKGTCACHE pCache = pImage->pGTCache;
4662 uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
4663 uint32_t uGTHash, uGTBlockIndex;
4664 uint64_t uGTSector, uRGTSector, uGTBlock;
4665 uint64_t uSector = pGrainAlloc->uSector;
4666 PVMDKGTCACHEENTRY pGTCacheEntry;
4667
4668 LogFlowFunc(("pImage=%#p pExtent=%#p pCache=%#p pIoCtx=%#p pGrainAlloc=%#p\n",
4669 pImage, pExtent, pCache, pIoCtx, pGrainAlloc));
4670
4671 uGTSector = pGrainAlloc->uGTSector;
4672 uRGTSector = pGrainAlloc->uRGTSector;
4673 LogFlow(("uGTSector=%llu uRGTSector=%llu\n", uGTSector, uRGTSector));
4674
4675 /* Update the grain table (and the cache). */
4676 uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
4677 uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
4678 pGTCacheEntry = &pCache->aGTCache[uGTHash];
4679 if ( pGTCacheEntry->uExtent != pExtent->uExtent
4680 || pGTCacheEntry->uGTBlock != uGTBlock)
4681 {
4682 /* Cache miss, fetch data from disk. */
4683 LogFlow(("Cache miss, fetch data from disk\n"));
4684 PVDMETAXFER pMetaXfer = NULL;
4685 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pExtent->pFile->pStorage,
4686 VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
4687 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
4688 &pMetaXfer, vmdkAllocGrainComplete, pGrainAlloc);
4689 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4690 {
4691 pGrainAlloc->cIoXfersPending++;
4692 pGrainAlloc->fGTUpdateNeeded = true;
4693 /* Leave early, we will be called again after the read completed. */
4694 LogFlowFunc(("Metadata read in progress, leaving\n"));
4695 return rc;
4696 }
4697 else if (RT_FAILURE(rc))
4698 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot read allocated grain table entry in '%s'"), pExtent->pszFullname);
4699 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
4700 pGTCacheEntry->uExtent = pExtent->uExtent;
4701 pGTCacheEntry->uGTBlock = uGTBlock;
4702 for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
4703 pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
4704 }
4705 else
4706 {
4707 /* Cache hit. Convert grain table block back to disk format, otherwise
4708 * the code below will write garbage for all but the updated entry. */
4709 for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
4710 aGTDataTmp[i] = RT_H2LE_U32(pGTCacheEntry->aGTData[i]);
4711 }
4712 pGrainAlloc->fGTUpdateNeeded = false;
4713 uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
4714 aGTDataTmp[uGTBlockIndex] = RT_H2LE_U32(VMDK_BYTE2SECTOR(pGrainAlloc->uGrainOffset));
4715 pGTCacheEntry->aGTData[uGTBlockIndex] = VMDK_BYTE2SECTOR(pGrainAlloc->uGrainOffset);
4716 /* Update grain table on disk. */
4717 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
4718 VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
4719 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
4720 vmdkAllocGrainComplete, pGrainAlloc);
4721 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4722 pGrainAlloc->cIoXfersPending++;
4723 else if (RT_FAILURE(rc))
4724 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated grain table in '%s'"), pExtent->pszFullname);
4725 if (pExtent->pRGD)
4726 {
4727 /* Update backup grain table on disk. */
4728 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
4729 VMDK_SECTOR2BYTE(uRGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
4730 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
4731 vmdkAllocGrainComplete, pGrainAlloc);
4732 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4733 pGrainAlloc->cIoXfersPending++;
4734 else if (RT_FAILURE(rc))
4735 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated backup grain table in '%s'"), pExtent->pszFullname);
4736 }
4737
4738 LogFlowFunc(("leaving rc=%Rrc\n", rc));
4739 return rc;
4740}
4741
4742/**
4743 * Internal - complete the grain allocation by updating disk grain table if required.
4744 */
4745static int vmdkAllocGrainComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
4746{
4747 RT_NOREF1(rcReq);
4748 int rc = VINF_SUCCESS;
4749 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
4750 PVMDKGRAINALLOCASYNC pGrainAlloc = (PVMDKGRAINALLOCASYNC)pvUser;
4751
4752 LogFlowFunc(("pBackendData=%#p pIoCtx=%#p pvUser=%#p rcReq=%Rrc\n",
4753 pBackendData, pIoCtx, pvUser, rcReq));
4754
4755 pGrainAlloc->cIoXfersPending--;
4756 if (!pGrainAlloc->cIoXfersPending && pGrainAlloc->fGTUpdateNeeded)
4757 rc = vmdkAllocGrainGTUpdate(pImage, pGrainAlloc->pExtent, pIoCtx, pGrainAlloc);
4758
4759 if (!pGrainAlloc->cIoXfersPending)
4760 {
4761 /* Grain allocation completed. */
4762 RTMemFree(pGrainAlloc);
4763 }
4764
4765 LogFlowFunc(("Leaving rc=%Rrc\n", rc));
4766 return rc;
4767}
4768
4769/**
4770 * Internal. Allocates a new grain table (if necessary).
4771 */
4772static int vmdkAllocGrain(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, PVDIOCTX pIoCtx,
4773 uint64_t uSector, uint64_t cbWrite)
4774{
4775 PVMDKGTCACHE pCache = pImage->pGTCache; NOREF(pCache);
4776 uint64_t uGDIndex, uGTSector, uRGTSector;
4777 uint64_t uFileOffset;
4778 PVMDKGRAINALLOCASYNC pGrainAlloc = NULL;
4779 int rc;
4780
4781 LogFlowFunc(("pCache=%#p pExtent=%#p pIoCtx=%#p uSector=%llu cbWrite=%llu\n",
4782 pCache, pExtent, pIoCtx, uSector, cbWrite));
4783
4784 pGrainAlloc = (PVMDKGRAINALLOCASYNC)RTMemAllocZ(sizeof(VMDKGRAINALLOCASYNC));
4785 if (!pGrainAlloc)
4786 return VERR_NO_MEMORY;
4787
4788 pGrainAlloc->pExtent = pExtent;
4789 pGrainAlloc->uSector = uSector;
4790
4791 uGDIndex = uSector / pExtent->cSectorsPerGDE;
4792 if (uGDIndex >= pExtent->cGDEntries)
4793 {
4794 RTMemFree(pGrainAlloc);
4795 return VERR_OUT_OF_RANGE;
4796 }
4797 uGTSector = pExtent->pGD[uGDIndex];
4798 if (pExtent->pRGD)
4799 uRGTSector = pExtent->pRGD[uGDIndex];
4800 else
4801 uRGTSector = 0; /**< avoid compiler warning */
4802 if (!uGTSector)
4803 {
4804 LogFlow(("Allocating new grain table\n"));
4805
4806 /* There is no grain table referenced by this grain directory
4807 * entry. So there is absolutely no data in this area. Allocate
4808 * a new grain table and put the reference to it in the GDs. */
4809 uFileOffset = pExtent->uAppendPosition;
4810 if (!uFileOffset)
4811 {
4812 RTMemFree(pGrainAlloc);
4813 return VERR_INTERNAL_ERROR;
4814 }
4815 Assert(!(uFileOffset % 512));
4816
4817 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
4818 uGTSector = VMDK_BYTE2SECTOR(uFileOffset);
4819
4820 /* Normally the grain table is preallocated for hosted sparse extents
4821 * that support more than 32 bit sector numbers. So this shouldn't
4822 * ever happen on a valid extent. */
4823 if (uGTSector > UINT32_MAX)
4824 {
4825 RTMemFree(pGrainAlloc);
4826 return VERR_VD_VMDK_INVALID_HEADER;
4827 }
4828
4829 /* Write grain table by writing the required number of grain table
4830 * cache chunks. Allocate memory dynamically here or we flood the
4831 * metadata cache with very small entries. */
4832 size_t cbGTDataTmp = pExtent->cGTEntries * sizeof(uint32_t);
4833 uint32_t *paGTDataTmp = (uint32_t *)RTMemTmpAllocZ(cbGTDataTmp);
4834
4835 if (!paGTDataTmp)
4836 {
4837 RTMemFree(pGrainAlloc);
4838 return VERR_NO_MEMORY;
4839 }
4840
4841 memset(paGTDataTmp, '\0', cbGTDataTmp);
4842 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
4843 VMDK_SECTOR2BYTE(uGTSector),
4844 paGTDataTmp, cbGTDataTmp, pIoCtx,
4845 vmdkAllocGrainComplete, pGrainAlloc);
4846 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4847 pGrainAlloc->cIoXfersPending++;
4848 else if (RT_FAILURE(rc))
4849 {
4850 RTMemTmpFree(paGTDataTmp);
4851 RTMemFree(pGrainAlloc);
4852 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain table allocation in '%s'"), pExtent->pszFullname);
4853 }
4854 pExtent->uAppendPosition = RT_ALIGN_64( pExtent->uAppendPosition
4855 + cbGTDataTmp, 512);
4856
4857 if (pExtent->pRGD)
4858 {
4859 AssertReturn(!uRGTSector, VERR_VD_VMDK_INVALID_HEADER);
4860 uFileOffset = pExtent->uAppendPosition;
4861 if (!uFileOffset)
4862 return VERR_INTERNAL_ERROR;
4863 Assert(!(uFileOffset % 512));
4864 uRGTSector = VMDK_BYTE2SECTOR(uFileOffset);
4865
4866 /* Normally the redundant grain table is preallocated for hosted
4867 * sparse extents that support more than 32 bit sector numbers. So
4868 * this shouldn't ever happen on a valid extent. */
4869 if (uRGTSector > UINT32_MAX)
4870 {
4871 RTMemTmpFree(paGTDataTmp);
4872 return VERR_VD_VMDK_INVALID_HEADER;
4873 }
4874
4875 /* Write grain table by writing the required number of grain table
4876 * cache chunks. Allocate memory dynamically here or we flood the
4877 * metadata cache with very small entries. */
4878 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
4879 VMDK_SECTOR2BYTE(uRGTSector),
4880 paGTDataTmp, cbGTDataTmp, pIoCtx,
4881 vmdkAllocGrainComplete, pGrainAlloc);
4882 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4883 pGrainAlloc->cIoXfersPending++;
4884 else if (RT_FAILURE(rc))
4885 {
4886 RTMemTmpFree(paGTDataTmp);
4887 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain table allocation in '%s'"), pExtent->pszFullname);
4888 }
4889
4890 pExtent->uAppendPosition = pExtent->uAppendPosition + cbGTDataTmp;
4891 }
4892
4893 RTMemTmpFree(paGTDataTmp);
4894
4895 /* Update the grain directory on disk (doing it before writing the
4896 * grain table will result in a garbled extent if the operation is
4897 * aborted for some reason. Otherwise the worst that can happen is
4898 * some unused sectors in the extent. */
4899 uint32_t uGTSectorLE = RT_H2LE_U64(uGTSector);
4900 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
4901 VMDK_SECTOR2BYTE(pExtent->uSectorGD) + uGDIndex * sizeof(uGTSectorLE),
4902 &uGTSectorLE, sizeof(uGTSectorLE), pIoCtx,
4903 vmdkAllocGrainComplete, pGrainAlloc);
4904 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4905 pGrainAlloc->cIoXfersPending++;
4906 else if (RT_FAILURE(rc))
4907 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain directory entry in '%s'"), pExtent->pszFullname);
4908 if (pExtent->pRGD)
4909 {
4910 uint32_t uRGTSectorLE = RT_H2LE_U64(uRGTSector);
4911 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
4912 VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + uGDIndex * sizeof(uGTSectorLE),
4913 &uRGTSectorLE, sizeof(uRGTSectorLE), pIoCtx,
4914 vmdkAllocGrainComplete, pGrainAlloc);
4915 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4916 pGrainAlloc->cIoXfersPending++;
4917 else if (RT_FAILURE(rc))
4918 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain directory entry in '%s'"), pExtent->pszFullname);
4919 }
4920
4921 /* As the final step update the in-memory copy of the GDs. */
4922 pExtent->pGD[uGDIndex] = uGTSector;
4923 if (pExtent->pRGD)
4924 pExtent->pRGD[uGDIndex] = uRGTSector;
4925 }
4926
4927 LogFlow(("uGTSector=%llu uRGTSector=%llu\n", uGTSector, uRGTSector));
4928 pGrainAlloc->uGTSector = uGTSector;
4929 pGrainAlloc->uRGTSector = uRGTSector;
4930
4931 uFileOffset = pExtent->uAppendPosition;
4932 if (!uFileOffset)
4933 return VERR_INTERNAL_ERROR;
4934 Assert(!(uFileOffset % 512));
4935
4936 pGrainAlloc->uGrainOffset = uFileOffset;
4937
4938 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
4939 {
4940 AssertMsgReturn(vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx),
4941 ("Accesses to stream optimized images must be synchronous\n"),
4942 VERR_INVALID_STATE);
4943
4944 if (cbWrite != VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
4945 return vdIfError(pImage->pIfError, VERR_INTERNAL_ERROR, RT_SRC_POS, N_("VMDK: not enough data for a compressed data block in '%s'"), pExtent->pszFullname);
4946
4947 /* Invalidate cache, just in case some code incorrectly allows mixing
4948 * of reads and writes. Normally shouldn't be needed. */
4949 pExtent->uGrainSectorAbs = 0;
4950
4951 /* Write compressed data block and the markers. */
4952 uint32_t cbGrain = 0;
4953 size_t cbSeg = 0;
4954 RTSGSEG Segment;
4955 unsigned cSegments = 1;
4956
4957 cbSeg = vdIfIoIntIoCtxSegArrayCreate(pImage->pIfIo, pIoCtx, &Segment,
4958 &cSegments, cbWrite);
4959 Assert(cbSeg == cbWrite);
4960
4961 rc = vmdkFileDeflateSync(pImage, pExtent, uFileOffset,
4962 Segment.pvSeg, cbWrite, uSector, &cbGrain);
4963 if (RT_FAILURE(rc))
4964 {
4965 AssertRC(rc);
4966 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write allocated compressed data block in '%s'"), pExtent->pszFullname);
4967 }
4968 pExtent->uLastGrainAccess = uSector / pExtent->cSectorsPerGrain;
4969 pExtent->uAppendPosition += cbGrain;
4970 }
4971 else
4972 {
4973 /* Write the data. Always a full grain, or we're in big trouble. */
4974 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pExtent->pFile->pStorage,
4975 uFileOffset, pIoCtx, cbWrite,
4976 vmdkAllocGrainComplete, pGrainAlloc);
4977 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4978 pGrainAlloc->cIoXfersPending++;
4979 else if (RT_FAILURE(rc))
4980 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write allocated data block in '%s'"), pExtent->pszFullname);
4981
4982 pExtent->uAppendPosition += cbWrite;
4983 }
4984
4985 rc = vmdkAllocGrainGTUpdate(pImage, pExtent, pIoCtx, pGrainAlloc);
4986
4987 if (!pGrainAlloc->cIoXfersPending)
4988 {
4989 /* Grain allocation completed. */
4990 RTMemFree(pGrainAlloc);
4991 }
4992
4993 LogFlowFunc(("leaving rc=%Rrc\n", rc));
4994
4995 return rc;
4996}
4997
4998/**
4999 * Internal. Reads the contents by sequentially going over the compressed
5000 * grains (hoping that they are in sequence).
5001 */
5002static int vmdkStreamReadSequential(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
5003 uint64_t uSector, PVDIOCTX pIoCtx,
5004 uint64_t cbRead)
5005{
5006 int rc;
5007
5008 LogFlowFunc(("pImage=%#p pExtent=%#p uSector=%llu pIoCtx=%#p cbRead=%llu\n",
5009 pImage, pExtent, uSector, pIoCtx, cbRead));
5010
5011 AssertMsgReturn(vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx),
5012 ("Async I/O not supported for sequential stream optimized images\n"),
5013 VERR_INVALID_STATE);
5014
5015 /* Do not allow to go back. */
5016 uint32_t uGrain = uSector / pExtent->cSectorsPerGrain;
5017 if (uGrain < pExtent->uLastGrainAccess)
5018 return VERR_VD_VMDK_INVALID_STATE;
5019 pExtent->uLastGrainAccess = uGrain;
5020
5021 /* After a previous error do not attempt to recover, as it would need
5022 * seeking (in the general case backwards which is forbidden). */
5023 if (!pExtent->uGrainSectorAbs)
5024 return VERR_VD_VMDK_INVALID_STATE;
5025
5026 /* Check if we need to read something from the image or if what we have
5027 * in the buffer is good to fulfill the request. */
5028 if (!pExtent->cbGrainStreamRead || uGrain > pExtent->uGrain)
5029 {
5030 uint32_t uGrainSectorAbs = pExtent->uGrainSectorAbs
5031 + VMDK_BYTE2SECTOR(pExtent->cbGrainStreamRead);
5032
5033 /* Get the marker from the next data block - and skip everything which
5034 * is not a compressed grain. If it's a compressed grain which is for
5035 * the requested sector (or after), read it. */
5036 VMDKMARKER Marker;
5037 do
5038 {
5039 RT_ZERO(Marker);
5040 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
5041 VMDK_SECTOR2BYTE(uGrainSectorAbs),
5042 &Marker, RT_UOFFSETOF(VMDKMARKER, uType));
5043 if (RT_FAILURE(rc))
5044 return rc;
5045 Marker.uSector = RT_LE2H_U64(Marker.uSector);
5046 Marker.cbSize = RT_LE2H_U32(Marker.cbSize);
5047
5048 if (Marker.cbSize == 0)
5049 {
5050 /* A marker for something else than a compressed grain. */
5051 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
5052 VMDK_SECTOR2BYTE(uGrainSectorAbs)
5053 + RT_UOFFSETOF(VMDKMARKER, uType),
5054 &Marker.uType, sizeof(Marker.uType));
5055 if (RT_FAILURE(rc))
5056 return rc;
5057 Marker.uType = RT_LE2H_U32(Marker.uType);
5058 switch (Marker.uType)
5059 {
5060 case VMDK_MARKER_EOS:
5061 uGrainSectorAbs++;
5062 /* Read (or mostly skip) to the end of file. Uses the
5063 * Marker (LBA sector) as it is unused anyway. This
5064 * makes sure that really everything is read in the
5065 * success case. If this read fails it means the image
5066 * is truncated, but this is harmless so ignore. */
5067 vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
5068 VMDK_SECTOR2BYTE(uGrainSectorAbs)
5069 + 511,
5070 &Marker.uSector, 1);
5071 break;
5072 case VMDK_MARKER_GT:
5073 uGrainSectorAbs += 1 + VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
5074 break;
5075 case VMDK_MARKER_GD:
5076 uGrainSectorAbs += 1 + VMDK_BYTE2SECTOR(RT_ALIGN(pExtent->cGDEntries * sizeof(uint32_t), 512));
5077 break;
5078 case VMDK_MARKER_FOOTER:
5079 uGrainSectorAbs += 2;
5080 break;
5081 case VMDK_MARKER_UNSPECIFIED:
5082 /* Skip over the contents of the unspecified marker
5083 * type 4 which exists in some vSphere created files. */
5084 /** @todo figure out what the payload means. */
5085 uGrainSectorAbs += 1;
5086 break;
5087 default:
5088 AssertMsgFailed(("VMDK: corrupted marker, type=%#x\n", Marker.uType));
5089 pExtent->uGrainSectorAbs = 0;
5090 return VERR_VD_VMDK_INVALID_STATE;
5091 }
5092 pExtent->cbGrainStreamRead = 0;
5093 }
5094 else
5095 {
5096 /* A compressed grain marker. If it is at/after what we're
5097 * interested in read and decompress data. */
5098 if (uSector > Marker.uSector + pExtent->cSectorsPerGrain)
5099 {
5100 uGrainSectorAbs += VMDK_BYTE2SECTOR(RT_ALIGN(Marker.cbSize + RT_UOFFSETOF(VMDKMARKER, uType), 512));
5101 continue;
5102 }
5103 uint64_t uLBA = 0;
5104 uint32_t cbGrainStreamRead = 0;
5105 rc = vmdkFileInflateSync(pImage, pExtent,
5106 VMDK_SECTOR2BYTE(uGrainSectorAbs),
5107 pExtent->pvGrain,
5108 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
5109 &Marker, &uLBA, &cbGrainStreamRead);
5110 if (RT_FAILURE(rc))
5111 {
5112 pExtent->uGrainSectorAbs = 0;
5113 return rc;
5114 }
5115 if ( pExtent->uGrain
5116 && uLBA / pExtent->cSectorsPerGrain <= pExtent->uGrain)
5117 {
5118 pExtent->uGrainSectorAbs = 0;
5119 return VERR_VD_VMDK_INVALID_STATE;
5120 }
5121 pExtent->uGrain = uLBA / pExtent->cSectorsPerGrain;
5122 pExtent->cbGrainStreamRead = cbGrainStreamRead;
5123 break;
5124 }
5125 } while (Marker.uType != VMDK_MARKER_EOS);
5126
5127 pExtent->uGrainSectorAbs = uGrainSectorAbs;
5128
5129 if (!pExtent->cbGrainStreamRead && Marker.uType == VMDK_MARKER_EOS)
5130 {
5131 pExtent->uGrain = UINT32_MAX;
5132 /* Must set a non-zero value for pExtent->cbGrainStreamRead or
5133 * the next read would try to get more data, and we're at EOF. */
5134 pExtent->cbGrainStreamRead = 1;
5135 }
5136 }
5137
5138 if (pExtent->uGrain > uSector / pExtent->cSectorsPerGrain)
5139 {
5140 /* The next data block we have is not for this area, so just return
5141 * that there is no data. */
5142 LogFlowFunc(("returns VERR_VD_BLOCK_FREE\n"));
5143 return VERR_VD_BLOCK_FREE;
5144 }
5145
5146 uint32_t uSectorInGrain = uSector % pExtent->cSectorsPerGrain;
5147 vdIfIoIntIoCtxCopyTo(pImage->pIfIo, pIoCtx,
5148 (uint8_t *)pExtent->pvGrain + VMDK_SECTOR2BYTE(uSectorInGrain),
5149 cbRead);
5150 LogFlowFunc(("returns VINF_SUCCESS\n"));
5151 return VINF_SUCCESS;
5152}
5153
5154/**
5155 * Replaces a fragment of a string with the specified string.
5156 *
5157 * @returns Pointer to the allocated UTF-8 string.
5158 * @param pszWhere UTF-8 string to search in.
5159 * @param pszWhat UTF-8 string to search for.
5160 * @param pszByWhat UTF-8 string to replace the found string with.
5161 */
5162static char *vmdkStrReplace(const char *pszWhere, const char *pszWhat,
5163 const char *pszByWhat)
5164{
5165 AssertPtr(pszWhere);
5166 AssertPtr(pszWhat);
5167 AssertPtr(pszByWhat);
5168 const char *pszFoundStr = strstr(pszWhere, pszWhat);
5169 if (!pszFoundStr)
5170 return NULL;
5171 size_t cFinal = strlen(pszWhere) + 1 + strlen(pszByWhat) - strlen(pszWhat);
5172 char *pszNewStr = (char *)RTMemAlloc(cFinal);
5173 if (pszNewStr)
5174 {
5175 char *pszTmp = pszNewStr;
5176 memcpy(pszTmp, pszWhere, pszFoundStr - pszWhere);
5177 pszTmp += pszFoundStr - pszWhere;
5178 memcpy(pszTmp, pszByWhat, strlen(pszByWhat));
5179 pszTmp += strlen(pszByWhat);
5180 strcpy(pszTmp, pszFoundStr + strlen(pszWhat));
5181 }
5182 return pszNewStr;
5183}
5184
5185
5186/** @copydoc VDIMAGEBACKEND::pfnProbe */
5187static DECLCALLBACK(int) vmdkProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
5188 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
5189{
5190 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p penmType=%#p\n",
5191 pszFilename, pVDIfsDisk, pVDIfsImage, penmType));
5192
5193 AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
5194
5195 int rc = VINF_SUCCESS;
5196 PVMDKIMAGE pImage = (PVMDKIMAGE)RTMemAllocZ(RT_UOFFSETOF(VMDKIMAGE, RegionList.aRegions[1]));
5197 if (RT_LIKELY(pImage))
5198 {
5199 pImage->pszFilename = pszFilename;
5200 pImage->pFile = NULL;
5201 pImage->pExtents = NULL;
5202 pImage->pFiles = NULL;
5203 pImage->pGTCache = NULL;
5204 pImage->pDescData = NULL;
5205 pImage->pVDIfsDisk = pVDIfsDisk;
5206 pImage->pVDIfsImage = pVDIfsImage;
5207 /** @todo speed up this test open (VD_OPEN_FLAGS_INFO) by skipping as
5208 * much as possible in vmdkOpenImage. */
5209 rc = vmdkOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
5210 vmdkFreeImage(pImage, false);
5211 RTMemFree(pImage);
5212
5213 if (RT_SUCCESS(rc))
5214 *penmType = VDTYPE_HDD;
5215 }
5216 else
5217 rc = VERR_NO_MEMORY;
5218
5219 LogFlowFunc(("returns %Rrc\n", rc));
5220 return rc;
5221}
5222
5223/** @copydoc VDIMAGEBACKEND::pfnOpen */
5224static DECLCALLBACK(int) vmdkOpen(const char *pszFilename, unsigned uOpenFlags,
5225 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
5226 VDTYPE enmType, void **ppBackendData)
5227{
5228 RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
5229
5230 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
5231 pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
5232 int rc;
5233
5234 /* Check open flags. All valid flags are supported. */
5235 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
5236 AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
5237
5238 PVMDKIMAGE pImage = (PVMDKIMAGE)RTMemAllocZ(RT_UOFFSETOF(VMDKIMAGE, RegionList.aRegions[1]));
5239 if (RT_LIKELY(pImage))
5240 {
5241 pImage->pszFilename = pszFilename;
5242 pImage->pFile = NULL;
5243 pImage->pExtents = NULL;
5244 pImage->pFiles = NULL;
5245 pImage->pGTCache = NULL;
5246 pImage->pDescData = NULL;
5247 pImage->pVDIfsDisk = pVDIfsDisk;
5248 pImage->pVDIfsImage = pVDIfsImage;
5249
5250 rc = vmdkOpenImage(pImage, uOpenFlags);
5251 if (RT_SUCCESS(rc))
5252 *ppBackendData = pImage;
5253 else
5254 RTMemFree(pImage);
5255 }
5256 else
5257 rc = VERR_NO_MEMORY;
5258
5259 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
5260 return rc;
5261}
5262
5263/** @copydoc VDIMAGEBACKEND::pfnCreate */
5264static DECLCALLBACK(int) vmdkCreate(const char *pszFilename, uint64_t cbSize,
5265 unsigned uImageFlags, const char *pszComment,
5266 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
5267 PCRTUUID pUuid, unsigned uOpenFlags,
5268 unsigned uPercentStart, unsigned uPercentSpan,
5269 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
5270 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
5271 void **ppBackendData)
5272{
5273 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p enmType=%u ppBackendData=%#p\n",
5274 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
5275 int rc;
5276
5277 /* Check the VD container type and image flags. */
5278 if ( enmType != VDTYPE_HDD
5279 || (uImageFlags & ~VD_VMDK_IMAGE_FLAGS_MASK) != 0)
5280 return VERR_VD_INVALID_TYPE;
5281
5282 /* Check size. Maximum 256TB-64K for sparse images, otherwise unlimited. */
5283 if ( !cbSize
5284 || (!(uImageFlags & VD_IMAGE_FLAGS_FIXED) && cbSize >= _1T * 256 - _64K))
5285 return VERR_VD_INVALID_SIZE;
5286
5287 /* Check image flags for invalid combinations. */
5288 if ( (uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
5289 && (uImageFlags & ~(VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED | VD_IMAGE_FLAGS_DIFF)))
5290 return VERR_INVALID_PARAMETER;
5291
5292 /* Check open flags. All valid flags are supported. */
5293 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
5294 AssertReturn( VALID_PTR(pszFilename)
5295 && *pszFilename
5296 && VALID_PTR(pPCHSGeometry)
5297 && VALID_PTR(pLCHSGeometry)
5298 && !( uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX
5299 && !(uImageFlags & VD_IMAGE_FLAGS_FIXED)),
5300 VERR_INVALID_PARAMETER);
5301
5302 PVMDKIMAGE pImage = (PVMDKIMAGE)RTMemAllocZ(RT_UOFFSETOF(VMDKIMAGE, RegionList.aRegions[1]));
5303 if (RT_LIKELY(pImage))
5304 {
5305 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
5306
5307 pImage->pszFilename = pszFilename;
5308 pImage->pFile = NULL;
5309 pImage->pExtents = NULL;
5310 pImage->pFiles = NULL;
5311 pImage->pGTCache = NULL;
5312 pImage->pDescData = NULL;
5313 pImage->pVDIfsDisk = pVDIfsDisk;
5314 pImage->pVDIfsImage = pVDIfsImage;
5315 /* Descriptors for split images can be pretty large, especially if the
5316 * filename is long. So prepare for the worst, and allocate quite some
5317 * memory for the descriptor in this case. */
5318 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
5319 pImage->cbDescAlloc = VMDK_SECTOR2BYTE(200);
5320 else
5321 pImage->cbDescAlloc = VMDK_SECTOR2BYTE(20);
5322 pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
5323 if (RT_LIKELY(pImage->pDescData))
5324 {
5325 rc = vmdkCreateImage(pImage, cbSize, uImageFlags, pszComment,
5326 pPCHSGeometry, pLCHSGeometry, pUuid,
5327 pIfProgress, uPercentStart, uPercentSpan);
5328 if (RT_SUCCESS(rc))
5329 {
5330 /* So far the image is opened in read/write mode. Make sure the
5331 * image is opened in read-only mode if the caller requested that. */
5332 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
5333 {
5334 vmdkFreeImage(pImage, false);
5335 rc = vmdkOpenImage(pImage, uOpenFlags);
5336 }
5337
5338 if (RT_SUCCESS(rc))
5339 *ppBackendData = pImage;
5340 }
5341
5342 if (RT_FAILURE(rc))
5343 RTMemFree(pImage->pDescData);
5344 }
5345 else
5346 rc = VERR_NO_MEMORY;
5347
5348 if (RT_FAILURE(rc))
5349 RTMemFree(pImage);
5350 }
5351 else
5352 rc = VERR_NO_MEMORY;
5353
5354 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
5355 return rc;
5356}
5357
5358/**
5359 * Prepares the state for renaming a VMDK image, setting up the state and allocating
5360 * memory.
5361 *
5362 * @returns VBox status code.
5363 * @param pImage VMDK image instance.
5364 * @param pRenameState The state to initialize.
5365 * @param pszFilename The new filename.
5366 */
5367static int vmdkRenameStatePrepare(PVMDKIMAGE pImage, PVMDKRENAMESTATE pRenameState, const char *pszFilename)
5368{
5369 int rc = VINF_SUCCESS;
5370
5371 memset(&pRenameState->DescriptorCopy, 0, sizeof(pRenameState->DescriptorCopy));
5372
5373 /*
5374 * Allocate an array to store both old and new names of renamed files
5375 * in case we have to roll back the changes. Arrays are initialized
5376 * with zeros. We actually save stuff when and if we change it.
5377 */
5378 pRenameState->cExtents = pImage->cExtents;
5379 pRenameState->apszOldName = (char **)RTMemTmpAllocZ((pRenameState->cExtents + 1) * sizeof(char*));
5380 pRenameState->apszNewName = (char **)RTMemTmpAllocZ((pRenameState->cExtents + 1) * sizeof(char*));
5381 pRenameState->apszNewLines = (char **)RTMemTmpAllocZ(pRenameState->cExtents * sizeof(char*));
5382 if ( pRenameState->apszOldName
5383 && pRenameState->apszNewName
5384 && pRenameState->apszNewLines)
5385 {
5386 /* Save the descriptor size and position. */
5387 if (pImage->pDescData)
5388 {
5389 /* Separate descriptor file. */
5390 pRenameState->fEmbeddedDesc = false;
5391 }
5392 else
5393 {
5394 /* Embedded descriptor file. */
5395 pRenameState->ExtentCopy = pImage->pExtents[0];
5396 pRenameState->fEmbeddedDesc = true;
5397 }
5398
5399 /* Save the descriptor content. */
5400 pRenameState->DescriptorCopy.cLines = pImage->Descriptor.cLines;
5401 for (unsigned i = 0; i < pRenameState->DescriptorCopy.cLines; i++)
5402 {
5403 pRenameState->DescriptorCopy.aLines[i] = RTStrDup(pImage->Descriptor.aLines[i]);
5404 if (!pRenameState->DescriptorCopy.aLines[i])
5405 {
5406 rc = VERR_NO_MEMORY;
5407 break;
5408 }
5409 }
5410
5411 if (RT_SUCCESS(rc))
5412 {
5413 /* Prepare both old and new base names used for string replacement. */
5414 pRenameState->pszNewBaseName = RTStrDup(RTPathFilename(pszFilename));
5415 RTPathStripSuffix(pRenameState->pszNewBaseName);
5416 pRenameState->pszOldBaseName = RTStrDup(RTPathFilename(pImage->pszFilename));
5417 RTPathStripSuffix(pRenameState->pszOldBaseName);
5418 /* Prepare both old and new full names used for string replacement. */
5419 pRenameState->pszNewFullName = RTStrDup(pszFilename);
5420 RTPathStripSuffix(pRenameState->pszNewFullName);
5421 pRenameState->pszOldFullName = RTStrDup(pImage->pszFilename);
5422 RTPathStripSuffix(pRenameState->pszOldFullName);
5423
5424 /* Save the old name for easy access to the old descriptor file. */
5425 pRenameState->pszOldDescName = RTStrDup(pImage->pszFilename);
5426 /* Save old image name. */
5427 pRenameState->pszOldImageName = pImage->pszFilename;
5428 }
5429 }
5430 else
5431 rc = VERR_NO_MEMORY;
5432
5433 return rc;
5434}
5435
5436/**
5437 * Destroys the given rename state, freeing all allocated memory.
5438 *
5439 * @returns nothing.
5440 * @param pRenameState The rename state to destroy.
5441 */
5442static void vmdkRenameStateDestroy(PVMDKRENAMESTATE pRenameState)
5443{
5444 for (unsigned i = 0; i < pRenameState->DescriptorCopy.cLines; i++)
5445 if (pRenameState->DescriptorCopy.aLines[i])
5446 RTStrFree(pRenameState->DescriptorCopy.aLines[i]);
5447 if (pRenameState->apszOldName)
5448 {
5449 for (unsigned i = 0; i <= pRenameState->cExtents; i++)
5450 if (pRenameState->apszOldName[i])
5451 RTStrFree(pRenameState->apszOldName[i]);
5452 RTMemTmpFree(pRenameState->apszOldName);
5453 }
5454 if (pRenameState->apszNewName)
5455 {
5456 for (unsigned i = 0; i <= pRenameState->cExtents; i++)
5457 if (pRenameState->apszNewName[i])
5458 RTStrFree(pRenameState->apszNewName[i]);
5459 RTMemTmpFree(pRenameState->apszNewName);
5460 }
5461 if (pRenameState->apszNewLines)
5462 {
5463 for (unsigned i = 0; i < pRenameState->cExtents; i++)
5464 if (pRenameState->apszNewLines[i])
5465 RTStrFree(pRenameState->apszNewLines[i]);
5466 RTMemTmpFree(pRenameState->apszNewLines);
5467 }
5468 if (pRenameState->pszOldDescName)
5469 RTStrFree(pRenameState->pszOldDescName);
5470 if (pRenameState->pszOldBaseName)
5471 RTStrFree(pRenameState->pszOldBaseName);
5472 if (pRenameState->pszNewBaseName)
5473 RTStrFree(pRenameState->pszNewBaseName);
5474 if (pRenameState->pszOldFullName)
5475 RTStrFree(pRenameState->pszOldFullName);
5476 if (pRenameState->pszNewFullName)
5477 RTStrFree(pRenameState->pszNewFullName);
5478}
5479
5480/**
5481 * Rolls back the rename operation to the original state.
5482 *
5483 * @returns VBox status code.
5484 * @param pImage VMDK image instance.
5485 * @param pRenameState The rename state.
5486 */
5487static int vmdkRenameRollback(PVMDKIMAGE pImage, PVMDKRENAMESTATE pRenameState)
5488{
5489 int rc = VINF_SUCCESS;
5490
5491 if (!pRenameState->fImageFreed)
5492 {
5493 /*
5494 * Some extents may have been closed, close the rest. We will
5495 * re-open the whole thing later.
5496 */
5497 vmdkFreeImage(pImage, false);
5498 }
5499
5500 /* Rename files back. */
5501 for (unsigned i = 0; i <= pRenameState->cExtents; i++)
5502 {
5503 if (pRenameState->apszOldName[i])
5504 {
5505 rc = vdIfIoIntFileMove(pImage->pIfIo, pRenameState->apszNewName[i], pRenameState->apszOldName[i], 0);
5506 AssertRC(rc);
5507 }
5508 }
5509 /* Restore the old descriptor. */
5510 PVMDKFILE pFile;
5511 rc = vmdkFileOpen(pImage, &pFile, pRenameState->pszOldDescName,
5512 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_NORMAL,
5513 false /* fCreate */));
5514 AssertRC(rc);
5515 if (pRenameState->fEmbeddedDesc)
5516 {
5517 pRenameState->ExtentCopy.pFile = pFile;
5518 pImage->pExtents = &pRenameState->ExtentCopy;
5519 }
5520 else
5521 {
5522 /* Shouldn't be null for separate descriptor.
5523 * There will be no access to the actual content.
5524 */
5525 pImage->pDescData = pRenameState->pszOldDescName;
5526 pImage->pFile = pFile;
5527 }
5528 pImage->Descriptor = pRenameState->DescriptorCopy;
5529 vmdkWriteDescriptor(pImage, NULL);
5530 vmdkFileClose(pImage, &pFile, false);
5531 /* Get rid of the stuff we implanted. */
5532 pImage->pExtents = NULL;
5533 pImage->pFile = NULL;
5534 pImage->pDescData = NULL;
5535 /* Re-open the image back. */
5536 pImage->pszFilename = pRenameState->pszOldImageName;
5537 rc = vmdkOpenImage(pImage, pImage->uOpenFlags);
5538
5539 return rc;
5540}
5541
5542/**
5543 * Rename worker doing the real work.
5544 *
5545 * @returns VBox status code.
5546 * @param pImage VMDK image instance.
5547 * @param pRenameState The rename state.
5548 * @param pszFilename The new filename.
5549 */
5550static int vmdkRenameWorker(PVMDKIMAGE pImage, PVMDKRENAMESTATE pRenameState, const char *pszFilename)
5551{
5552 int rc = VINF_SUCCESS;
5553 unsigned i, line;
5554
5555 /* Update the descriptor with modified extent names. */
5556 for (i = 0, line = pImage->Descriptor.uFirstExtent;
5557 i < pRenameState->cExtents;
5558 i++, line = pImage->Descriptor.aNextLines[line])
5559 {
5560 /* Update the descriptor. */
5561 pRenameState->apszNewLines[i] = vmdkStrReplace(pImage->Descriptor.aLines[line],
5562 pRenameState->pszOldBaseName,
5563 pRenameState->pszNewBaseName);
5564 if (!pRenameState->apszNewLines[i])
5565 {
5566 rc = VERR_NO_MEMORY;
5567 break;
5568 }
5569 pImage->Descriptor.aLines[line] = pRenameState->apszNewLines[i];
5570 }
5571
5572 if (RT_SUCCESS(rc))
5573 {
5574 /* Make sure the descriptor gets written back. */
5575 pImage->Descriptor.fDirty = true;
5576 /* Flush the descriptor now, in case it is embedded. */
5577 vmdkFlushImage(pImage, NULL);
5578
5579 /* Close and rename/move extents. */
5580 for (i = 0; i < pRenameState->cExtents; i++)
5581 {
5582 PVMDKEXTENT pExtent = &pImage->pExtents[i];
5583 /* Compose new name for the extent. */
5584 pRenameState->apszNewName[i] = vmdkStrReplace(pExtent->pszFullname,
5585 pRenameState->pszOldFullName,
5586 pRenameState->pszNewFullName);
5587 if (!pRenameState->apszNewName[i])
5588 {
5589 rc = VERR_NO_MEMORY;
5590 break;
5591 }
5592 /* Close the extent file. */
5593 rc = vmdkFileClose(pImage, &pExtent->pFile, false);
5594 if (RT_FAILURE(rc))
5595 break;;
5596
5597 /* Rename the extent file. */
5598 rc = vdIfIoIntFileMove(pImage->pIfIo, pExtent->pszFullname, pRenameState->apszNewName[i], 0);
5599 if (RT_FAILURE(rc))
5600 break;
5601 /* Remember the old name. */
5602 pRenameState->apszOldName[i] = RTStrDup(pExtent->pszFullname);
5603 }
5604
5605 if (RT_SUCCESS(rc))
5606 {
5607 /* Release all old stuff. */
5608 rc = vmdkFreeImage(pImage, false);
5609 if (RT_SUCCESS(rc))
5610 {
5611 pRenameState->fImageFreed = true;
5612
5613 /* Last elements of new/old name arrays are intended for
5614 * storing descriptor's names.
5615 */
5616 pRenameState->apszNewName[pRenameState->cExtents] = RTStrDup(pszFilename);
5617 /* Rename the descriptor file if it's separate. */
5618 if (!pRenameState->fEmbeddedDesc)
5619 {
5620 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pRenameState->apszNewName[pRenameState->cExtents], 0);
5621 if (RT_SUCCESS(rc))
5622 {
5623 /* Save old name only if we may need to change it back. */
5624 pRenameState->apszOldName[pRenameState->cExtents] = RTStrDup(pszFilename);
5625 }
5626 }
5627
5628 /* Update pImage with the new information. */
5629 pImage->pszFilename = pszFilename;
5630
5631 /* Open the new image. */
5632 rc = vmdkOpenImage(pImage, pImage->uOpenFlags);
5633 }
5634 }
5635 }
5636
5637 return rc;
5638}
5639
5640/** @copydoc VDIMAGEBACKEND::pfnRename */
5641static DECLCALLBACK(int) vmdkRename(void *pBackendData, const char *pszFilename)
5642{
5643 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
5644
5645 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
5646 VMDKRENAMESTATE RenameState;
5647
5648 memset(&RenameState, 0, sizeof(RenameState));
5649
5650 /* Check arguments. */
5651 AssertReturn(( pImage
5652 && VALID_PTR(pszFilename)
5653 && *pszFilename
5654 && !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK)), VERR_INVALID_PARAMETER);
5655
5656 int rc = vmdkRenameStatePrepare(pImage, &RenameState, pszFilename);
5657 if (RT_SUCCESS(rc))
5658 {
5659 /* --- Up to this point we have not done any damage yet. --- */
5660
5661 rc = vmdkRenameWorker(pImage, &RenameState, pszFilename);
5662 /* Roll back all changes in case of failure. */
5663 if (RT_FAILURE(rc))
5664 {
5665 int rrc = vmdkRenameRollback(pImage, &RenameState);
5666 AssertRC(rrc);
5667 }
5668 }
5669
5670 vmdkRenameStateDestroy(&RenameState);
5671 LogFlowFunc(("returns %Rrc\n", rc));
5672 return rc;
5673}
5674
5675/** @copydoc VDIMAGEBACKEND::pfnClose */
5676static DECLCALLBACK(int) vmdkClose(void *pBackendData, bool fDelete)
5677{
5678 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
5679 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
5680
5681 int rc = vmdkFreeImage(pImage, fDelete);
5682 RTMemFree(pImage);
5683
5684 LogFlowFunc(("returns %Rrc\n", rc));
5685 return rc;
5686}
5687
5688/** @copydoc VDIMAGEBACKEND::pfnRead */
5689static DECLCALLBACK(int) vmdkRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
5690 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
5691{
5692 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
5693 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
5694 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
5695
5696 AssertPtr(pImage);
5697 Assert(uOffset % 512 == 0);
5698 Assert(cbToRead % 512 == 0);
5699 AssertReturn((VALID_PTR(pIoCtx) && cbToRead), VERR_INVALID_PARAMETER);
5700 AssertReturn(uOffset + cbToRead <= pImage->cbSize, VERR_INVALID_PARAMETER);
5701
5702 /* Find the extent and check access permissions as defined in the extent descriptor. */
5703 PVMDKEXTENT pExtent;
5704 uint64_t uSectorExtentRel;
5705 int rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
5706 &pExtent, &uSectorExtentRel);
5707 if ( RT_SUCCESS(rc)
5708 && pExtent->enmAccess != VMDKACCESS_NOACCESS)
5709 {
5710 /* Clip read range to remain in this extent. */
5711 cbToRead = RT_MIN(cbToRead, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
5712
5713 /* Handle the read according to the current extent type. */
5714 switch (pExtent->enmType)
5715 {
5716 case VMDKETYPE_HOSTED_SPARSE:
5717 {
5718 uint64_t uSectorExtentAbs;
5719
5720 rc = vmdkGetSector(pImage, pIoCtx, pExtent, uSectorExtentRel, &uSectorExtentAbs);
5721 if (RT_FAILURE(rc))
5722 break;
5723 /* Clip read range to at most the rest of the grain. */
5724 cbToRead = RT_MIN(cbToRead, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSectorExtentRel % pExtent->cSectorsPerGrain));
5725 Assert(!(cbToRead % 512));
5726 if (uSectorExtentAbs == 0)
5727 {
5728 if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
5729 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
5730 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
5731 rc = VERR_VD_BLOCK_FREE;
5732 else
5733 rc = vmdkStreamReadSequential(pImage, pExtent,
5734 uSectorExtentRel,
5735 pIoCtx, cbToRead);
5736 }
5737 else
5738 {
5739 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
5740 {
5741 AssertMsg(vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx),
5742 ("Async I/O is not supported for stream optimized VMDK's\n"));
5743
5744 uint32_t uSectorInGrain = uSectorExtentRel % pExtent->cSectorsPerGrain;
5745 uSectorExtentAbs -= uSectorInGrain;
5746 if (pExtent->uGrainSectorAbs != uSectorExtentAbs)
5747 {
5748 uint64_t uLBA = 0; /* gcc maybe uninitialized */
5749 rc = vmdkFileInflateSync(pImage, pExtent,
5750 VMDK_SECTOR2BYTE(uSectorExtentAbs),
5751 pExtent->pvGrain,
5752 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
5753 NULL, &uLBA, NULL);
5754 if (RT_FAILURE(rc))
5755 {
5756 pExtent->uGrainSectorAbs = 0;
5757 break;
5758 }
5759 pExtent->uGrainSectorAbs = uSectorExtentAbs;
5760 pExtent->uGrain = uSectorExtentRel / pExtent->cSectorsPerGrain;
5761 Assert(uLBA == uSectorExtentRel);
5762 }
5763 vdIfIoIntIoCtxCopyTo(pImage->pIfIo, pIoCtx,
5764 (uint8_t *)pExtent->pvGrain
5765 + VMDK_SECTOR2BYTE(uSectorInGrain),
5766 cbToRead);
5767 }
5768 else
5769 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pExtent->pFile->pStorage,
5770 VMDK_SECTOR2BYTE(uSectorExtentAbs),
5771 pIoCtx, cbToRead);
5772 }
5773 break;
5774 }
5775 case VMDKETYPE_VMFS:
5776 case VMDKETYPE_FLAT:
5777 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pExtent->pFile->pStorage,
5778 VMDK_SECTOR2BYTE(uSectorExtentRel),
5779 pIoCtx, cbToRead);
5780 break;
5781 case VMDKETYPE_ZERO:
5782 {
5783 size_t cbSet;
5784
5785 cbSet = vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
5786 Assert(cbSet == cbToRead);
5787 break;
5788 }
5789 }
5790 if (pcbActuallyRead)
5791 *pcbActuallyRead = cbToRead;
5792 }
5793 else if (RT_SUCCESS(rc))
5794 rc = VERR_VD_VMDK_INVALID_STATE;
5795
5796 LogFlowFunc(("returns %Rrc\n", rc));
5797 return rc;
5798}
5799
5800/** @copydoc VDIMAGEBACKEND::pfnWrite */
5801static DECLCALLBACK(int) vmdkWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
5802 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
5803 size_t *pcbPostRead, unsigned fWrite)
5804{
5805 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
5806 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
5807 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
5808 int rc;
5809
5810 AssertPtr(pImage);
5811 Assert(uOffset % 512 == 0);
5812 Assert(cbToWrite % 512 == 0);
5813 AssertReturn((VALID_PTR(pIoCtx) && cbToWrite), VERR_INVALID_PARAMETER);
5814
5815 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
5816 {
5817 PVMDKEXTENT pExtent;
5818 uint64_t uSectorExtentRel;
5819 uint64_t uSectorExtentAbs;
5820
5821 /* No size check here, will do that later when the extent is located.
5822 * There are sparse images out there which according to the spec are
5823 * invalid, because the total size is not a multiple of the grain size.
5824 * Also for sparse images which are stitched together in odd ways (not at
5825 * grain boundaries, and with the nominal size not being a multiple of the
5826 * grain size), this would prevent writing to the last grain. */
5827
5828 rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
5829 &pExtent, &uSectorExtentRel);
5830 if (RT_SUCCESS(rc))
5831 {
5832 if ( pExtent->enmAccess != VMDKACCESS_READWRITE
5833 && ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
5834 && !pImage->pExtents[0].uAppendPosition
5835 && pExtent->enmAccess != VMDKACCESS_READONLY))
5836 rc = VERR_VD_VMDK_INVALID_STATE;
5837 else
5838 {
5839 /* Handle the write according to the current extent type. */
5840 switch (pExtent->enmType)
5841 {
5842 case VMDKETYPE_HOSTED_SPARSE:
5843 rc = vmdkGetSector(pImage, pIoCtx, pExtent, uSectorExtentRel, &uSectorExtentAbs);
5844 if (RT_SUCCESS(rc))
5845 {
5846 if ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
5847 && uSectorExtentRel < (uint64_t)pExtent->uLastGrainAccess * pExtent->cSectorsPerGrain)
5848 rc = VERR_VD_VMDK_INVALID_WRITE;
5849 else
5850 {
5851 /* Clip write range to at most the rest of the grain. */
5852 cbToWrite = RT_MIN(cbToWrite,
5853 VMDK_SECTOR2BYTE( pExtent->cSectorsPerGrain
5854 - uSectorExtentRel % pExtent->cSectorsPerGrain));
5855 if (uSectorExtentAbs == 0)
5856 {
5857 if (!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
5858 {
5859 if (cbToWrite == VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
5860 {
5861 /* Full block write to a previously unallocated block.
5862 * Check if the caller wants to avoid the automatic alloc. */
5863 if (!(fWrite & VD_WRITE_NO_ALLOC))
5864 {
5865 /* Allocate GT and find out where to store the grain. */
5866 rc = vmdkAllocGrain(pImage, pExtent, pIoCtx,
5867 uSectorExtentRel, cbToWrite);
5868 }
5869 else
5870 rc = VERR_VD_BLOCK_FREE;
5871 *pcbPreRead = 0;
5872 *pcbPostRead = 0;
5873 }
5874 else
5875 {
5876 /* Clip write range to remain in this extent. */
5877 cbToWrite = RT_MIN(cbToWrite,
5878 VMDK_SECTOR2BYTE( pExtent->uSectorOffset
5879 + pExtent->cNominalSectors - uSectorExtentRel));
5880 *pcbPreRead = VMDK_SECTOR2BYTE(uSectorExtentRel % pExtent->cSectorsPerGrain);
5881 *pcbPostRead = VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain) - cbToWrite - *pcbPreRead;
5882 rc = VERR_VD_BLOCK_FREE;
5883 }
5884 }
5885 else
5886 rc = vmdkStreamAllocGrain(pImage, pExtent, uSectorExtentRel,
5887 pIoCtx, cbToWrite);
5888 }
5889 else
5890 {
5891 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
5892 {
5893 /* A partial write to a streamOptimized image is simply
5894 * invalid. It requires rewriting already compressed data
5895 * which is somewhere between expensive and impossible. */
5896 rc = VERR_VD_VMDK_INVALID_STATE;
5897 pExtent->uGrainSectorAbs = 0;
5898 AssertRC(rc);
5899 }
5900 else
5901 {
5902 Assert(!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED));
5903 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pExtent->pFile->pStorage,
5904 VMDK_SECTOR2BYTE(uSectorExtentAbs),
5905 pIoCtx, cbToWrite, NULL, NULL);
5906 }
5907 }
5908 }
5909 }
5910 break;
5911 case VMDKETYPE_VMFS:
5912 case VMDKETYPE_FLAT:
5913 /* Clip write range to remain in this extent. */
5914 cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
5915 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pExtent->pFile->pStorage,
5916 VMDK_SECTOR2BYTE(uSectorExtentRel),
5917 pIoCtx, cbToWrite, NULL, NULL);
5918 break;
5919 case VMDKETYPE_ZERO:
5920 /* Clip write range to remain in this extent. */
5921 cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
5922 break;
5923 }
5924 }
5925
5926 if (pcbWriteProcess)
5927 *pcbWriteProcess = cbToWrite;
5928 }
5929 }
5930 else
5931 rc = VERR_VD_IMAGE_READ_ONLY;
5932
5933 LogFlowFunc(("returns %Rrc\n", rc));
5934 return rc;
5935}
5936
5937/** @copydoc VDIMAGEBACKEND::pfnFlush */
5938static DECLCALLBACK(int) vmdkFlush(void *pBackendData, PVDIOCTX pIoCtx)
5939{
5940 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
5941
5942 return vmdkFlushImage(pImage, pIoCtx);
5943}
5944
5945/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
5946static DECLCALLBACK(unsigned) vmdkGetVersion(void *pBackendData)
5947{
5948 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5949 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
5950
5951 AssertPtrReturn(pImage, 0);
5952
5953 return VMDK_IMAGE_VERSION;
5954}
5955
5956/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
5957static DECLCALLBACK(uint64_t) vmdkGetFileSize(void *pBackendData)
5958{
5959 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5960 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
5961 uint64_t cb = 0;
5962
5963 AssertPtrReturn(pImage, 0);
5964
5965 if (pImage->pFile != NULL)
5966 {
5967 uint64_t cbFile;
5968 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pFile->pStorage, &cbFile);
5969 if (RT_SUCCESS(rc))
5970 cb += cbFile;
5971 }
5972 for (unsigned i = 0; i < pImage->cExtents; i++)
5973 {
5974 if (pImage->pExtents[i].pFile != NULL)
5975 {
5976 uint64_t cbFile;
5977 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pExtents[i].pFile->pStorage, &cbFile);
5978 if (RT_SUCCESS(rc))
5979 cb += cbFile;
5980 }
5981 }
5982
5983 LogFlowFunc(("returns %lld\n", cb));
5984 return cb;
5985}
5986
5987/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
5988static DECLCALLBACK(int) vmdkGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
5989{
5990 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
5991 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
5992 int rc = VINF_SUCCESS;
5993
5994 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5995
5996 if (pImage->PCHSGeometry.cCylinders)
5997 *pPCHSGeometry = pImage->PCHSGeometry;
5998 else
5999 rc = VERR_VD_GEOMETRY_NOT_SET;
6000
6001 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
6002 return rc;
6003}
6004
6005/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
6006static DECLCALLBACK(int) vmdkSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
6007{
6008 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
6009 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
6010 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6011 int rc = VINF_SUCCESS;
6012
6013 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6014
6015 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
6016 {
6017 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
6018 {
6019 rc = vmdkDescSetPCHSGeometry(pImage, pPCHSGeometry);
6020 if (RT_SUCCESS(rc))
6021 pImage->PCHSGeometry = *pPCHSGeometry;
6022 }
6023 else
6024 rc = VERR_NOT_SUPPORTED;
6025 }
6026 else
6027 rc = VERR_VD_IMAGE_READ_ONLY;
6028
6029 LogFlowFunc(("returns %Rrc\n", rc));
6030 return rc;
6031}
6032
6033/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
6034static DECLCALLBACK(int) vmdkGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
6035{
6036 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
6037 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6038 int rc = VINF_SUCCESS;
6039
6040 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6041
6042 if (pImage->LCHSGeometry.cCylinders)
6043 *pLCHSGeometry = pImage->LCHSGeometry;
6044 else
6045 rc = VERR_VD_GEOMETRY_NOT_SET;
6046
6047 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
6048 return rc;
6049}
6050
6051/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
6052static DECLCALLBACK(int) vmdkSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
6053{
6054 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
6055 pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
6056 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6057 int rc = VINF_SUCCESS;
6058
6059 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6060
6061 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
6062 {
6063 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
6064 {
6065 rc = vmdkDescSetLCHSGeometry(pImage, pLCHSGeometry);
6066 if (RT_SUCCESS(rc))
6067 pImage->LCHSGeometry = *pLCHSGeometry;
6068 }
6069 else
6070 rc = VERR_NOT_SUPPORTED;
6071 }
6072 else
6073 rc = VERR_VD_IMAGE_READ_ONLY;
6074
6075 LogFlowFunc(("returns %Rrc\n", rc));
6076 return rc;
6077}
6078
6079/** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
6080static DECLCALLBACK(int) vmdkQueryRegions(void *pBackendData, PCVDREGIONLIST *ppRegionList)
6081{
6082 LogFlowFunc(("pBackendData=%#p ppRegionList=%#p\n", pBackendData, ppRegionList));
6083 PVMDKIMAGE pThis = (PVMDKIMAGE)pBackendData;
6084
6085 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
6086
6087 *ppRegionList = &pThis->RegionList;
6088 LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
6089 return VINF_SUCCESS;
6090}
6091
6092/** @copydoc VDIMAGEBACKEND::pfnRegionListRelease */
6093static DECLCALLBACK(void) vmdkRegionListRelease(void *pBackendData, PCVDREGIONLIST pRegionList)
6094{
6095 RT_NOREF1(pRegionList);
6096 LogFlowFunc(("pBackendData=%#p pRegionList=%#p\n", pBackendData, pRegionList));
6097 PVMDKIMAGE pThis = (PVMDKIMAGE)pBackendData;
6098 AssertPtr(pThis); RT_NOREF(pThis);
6099
6100 /* Nothing to do here. */
6101}
6102
6103/** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
6104static DECLCALLBACK(unsigned) vmdkGetImageFlags(void *pBackendData)
6105{
6106 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
6107 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6108
6109 AssertPtrReturn(pImage, 0);
6110
6111 LogFlowFunc(("returns %#x\n", pImage->uImageFlags));
6112 return pImage->uImageFlags;
6113}
6114
6115/** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
6116static DECLCALLBACK(unsigned) vmdkGetOpenFlags(void *pBackendData)
6117{
6118 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
6119 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6120
6121 AssertPtrReturn(pImage, 0);
6122
6123 LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
6124 return pImage->uOpenFlags;
6125}
6126
6127/** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
6128static DECLCALLBACK(int) vmdkSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
6129{
6130 LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
6131 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6132 int rc;
6133
6134 /* Image must be opened and the new flags must be valid. */
6135 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
6136 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
6137 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
6138 rc = VERR_INVALID_PARAMETER;
6139 else
6140 {
6141 /* StreamOptimized images need special treatment: reopen is prohibited. */
6142 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
6143 {
6144 if (pImage->uOpenFlags == uOpenFlags)
6145 rc = VINF_SUCCESS;
6146 else
6147 rc = VERR_INVALID_PARAMETER;
6148 }
6149 else
6150 {
6151 /* Implement this operation via reopening the image. */
6152 vmdkFreeImage(pImage, false);
6153 rc = vmdkOpenImage(pImage, uOpenFlags);
6154 }
6155 }
6156
6157 LogFlowFunc(("returns %Rrc\n", rc));
6158 return rc;
6159}
6160
6161/** @copydoc VDIMAGEBACKEND::pfnGetComment */
6162static DECLCALLBACK(int) vmdkGetComment(void *pBackendData, char *pszComment, size_t cbComment)
6163{
6164 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
6165 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6166
6167 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6168
6169 char *pszCommentEncoded = NULL;
6170 int rc = vmdkDescDDBGetStr(pImage, &pImage->Descriptor,
6171 "ddb.comment", &pszCommentEncoded);
6172 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
6173 {
6174 pszCommentEncoded = NULL;
6175 rc = VINF_SUCCESS;
6176 }
6177
6178 if (RT_SUCCESS(rc))
6179 {
6180 if (pszComment && pszCommentEncoded)
6181 rc = vmdkDecodeString(pszCommentEncoded, pszComment, cbComment);
6182 else if (pszComment)
6183 *pszComment = '\0';
6184
6185 if (pszCommentEncoded)
6186 RTMemTmpFree(pszCommentEncoded);
6187 }
6188
6189 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
6190 return rc;
6191}
6192
6193/** @copydoc VDIMAGEBACKEND::pfnSetComment */
6194static DECLCALLBACK(int) vmdkSetComment(void *pBackendData, const char *pszComment)
6195{
6196 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
6197 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6198 int rc;
6199
6200 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6201
6202 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
6203 {
6204 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
6205 rc = vmdkSetImageComment(pImage, pszComment);
6206 else
6207 rc = VERR_NOT_SUPPORTED;
6208 }
6209 else
6210 rc = VERR_VD_IMAGE_READ_ONLY;
6211
6212 LogFlowFunc(("returns %Rrc\n", rc));
6213 return rc;
6214}
6215
6216/** @copydoc VDIMAGEBACKEND::pfnGetUuid */
6217static DECLCALLBACK(int) vmdkGetUuid(void *pBackendData, PRTUUID pUuid)
6218{
6219 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
6220 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6221
6222 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6223
6224 *pUuid = pImage->ImageUuid;
6225
6226 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
6227 return VINF_SUCCESS;
6228}
6229
6230/** @copydoc VDIMAGEBACKEND::pfnSetUuid */
6231static DECLCALLBACK(int) vmdkSetUuid(void *pBackendData, PCRTUUID pUuid)
6232{
6233 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
6234 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6235 int rc = VINF_SUCCESS;
6236
6237 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6238
6239 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
6240 {
6241 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
6242 {
6243 pImage->ImageUuid = *pUuid;
6244 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
6245 VMDK_DDB_IMAGE_UUID, pUuid);
6246 if (RT_FAILURE(rc))
6247 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
6248 N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
6249 }
6250 else
6251 rc = VERR_NOT_SUPPORTED;
6252 }
6253 else
6254 rc = VERR_VD_IMAGE_READ_ONLY;
6255
6256 LogFlowFunc(("returns %Rrc\n", rc));
6257 return rc;
6258}
6259
6260/** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
6261static DECLCALLBACK(int) vmdkGetModificationUuid(void *pBackendData, PRTUUID pUuid)
6262{
6263 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
6264 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6265
6266 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6267
6268 *pUuid = pImage->ModificationUuid;
6269
6270 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
6271 return VINF_SUCCESS;
6272}
6273
6274/** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
6275static DECLCALLBACK(int) vmdkSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
6276{
6277 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
6278 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6279 int rc = VINF_SUCCESS;
6280
6281 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6282
6283 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
6284 {
6285 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
6286 {
6287 /* Only touch the modification uuid if it changed. */
6288 if (RTUuidCompare(&pImage->ModificationUuid, pUuid))
6289 {
6290 pImage->ModificationUuid = *pUuid;
6291 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
6292 VMDK_DDB_MODIFICATION_UUID, pUuid);
6293 if (RT_FAILURE(rc))
6294 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing modification UUID in descriptor in '%s'"), pImage->pszFilename);
6295 }
6296 }
6297 else
6298 rc = VERR_NOT_SUPPORTED;
6299 }
6300 else
6301 rc = VERR_VD_IMAGE_READ_ONLY;
6302
6303 LogFlowFunc(("returns %Rrc\n", rc));
6304 return rc;
6305}
6306
6307/** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
6308static DECLCALLBACK(int) vmdkGetParentUuid(void *pBackendData, PRTUUID pUuid)
6309{
6310 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
6311 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6312
6313 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6314
6315 *pUuid = pImage->ParentUuid;
6316
6317 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
6318 return VINF_SUCCESS;
6319}
6320
6321/** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
6322static DECLCALLBACK(int) vmdkSetParentUuid(void *pBackendData, PCRTUUID pUuid)
6323{
6324 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
6325 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6326 int rc = VINF_SUCCESS;
6327
6328 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6329
6330 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
6331 {
6332 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
6333 {
6334 pImage->ParentUuid = *pUuid;
6335 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
6336 VMDK_DDB_PARENT_UUID, pUuid);
6337 if (RT_FAILURE(rc))
6338 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
6339 N_("VMDK: error storing parent image UUID in descriptor in '%s'"), pImage->pszFilename);
6340 }
6341 else
6342 rc = VERR_NOT_SUPPORTED;
6343 }
6344 else
6345 rc = VERR_VD_IMAGE_READ_ONLY;
6346
6347 LogFlowFunc(("returns %Rrc\n", rc));
6348 return rc;
6349}
6350
6351/** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
6352static DECLCALLBACK(int) vmdkGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
6353{
6354 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
6355 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6356
6357 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6358
6359 *pUuid = pImage->ParentModificationUuid;
6360
6361 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
6362 return VINF_SUCCESS;
6363}
6364
6365/** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
6366static DECLCALLBACK(int) vmdkSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
6367{
6368 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
6369 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6370 int rc = VINF_SUCCESS;
6371
6372 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
6373
6374 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
6375 {
6376 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
6377 {
6378 pImage->ParentModificationUuid = *pUuid;
6379 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
6380 VMDK_DDB_PARENT_MODIFICATION_UUID, pUuid);
6381 if (RT_FAILURE(rc))
6382 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent image UUID in descriptor in '%s'"), pImage->pszFilename);
6383 }
6384 else
6385 rc = VERR_NOT_SUPPORTED;
6386 }
6387 else
6388 rc = VERR_VD_IMAGE_READ_ONLY;
6389
6390 LogFlowFunc(("returns %Rrc\n", rc));
6391 return rc;
6392}
6393
6394/** @copydoc VDIMAGEBACKEND::pfnDump */
6395static DECLCALLBACK(void) vmdkDump(void *pBackendData)
6396{
6397 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6398
6399 AssertPtrReturnVoid(pImage);
6400 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
6401 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
6402 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
6403 VMDK_BYTE2SECTOR(pImage->cbSize));
6404 vdIfErrorMessage(pImage->pIfError, "Header: uuidCreation={%RTuuid}\n", &pImage->ImageUuid);
6405 vdIfErrorMessage(pImage->pIfError, "Header: uuidModification={%RTuuid}\n", &pImage->ModificationUuid);
6406 vdIfErrorMessage(pImage->pIfError, "Header: uuidParent={%RTuuid}\n", &pImage->ParentUuid);
6407 vdIfErrorMessage(pImage->pIfError, "Header: uuidParentModification={%RTuuid}\n", &pImage->ParentModificationUuid);
6408}
6409
6410
6411
6412const VDIMAGEBACKEND g_VmdkBackend =
6413{
6414 /* u32Version */
6415 VD_IMGBACKEND_VERSION,
6416 /* pszBackendName */
6417 "VMDK",
6418 /* uBackendCaps */
6419 VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
6420 | VD_CAP_CREATE_SPLIT_2G | VD_CAP_DIFF | VD_CAP_FILE | VD_CAP_ASYNC
6421 | VD_CAP_VFS | VD_CAP_PREFERRED,
6422 /* paFileExtensions */
6423 s_aVmdkFileExtensions,
6424 /* paConfigInfo */
6425 NULL,
6426 /* pfnProbe */
6427 vmdkProbe,
6428 /* pfnOpen */
6429 vmdkOpen,
6430 /* pfnCreate */
6431 vmdkCreate,
6432 /* pfnRename */
6433 vmdkRename,
6434 /* pfnClose */
6435 vmdkClose,
6436 /* pfnRead */
6437 vmdkRead,
6438 /* pfnWrite */
6439 vmdkWrite,
6440 /* pfnFlush */
6441 vmdkFlush,
6442 /* pfnDiscard */
6443 NULL,
6444 /* pfnGetVersion */
6445 vmdkGetVersion,
6446 /* pfnGetFileSize */
6447 vmdkGetFileSize,
6448 /* pfnGetPCHSGeometry */
6449 vmdkGetPCHSGeometry,
6450 /* pfnSetPCHSGeometry */
6451 vmdkSetPCHSGeometry,
6452 /* pfnGetLCHSGeometry */
6453 vmdkGetLCHSGeometry,
6454 /* pfnSetLCHSGeometry */
6455 vmdkSetLCHSGeometry,
6456 /* pfnQueryRegions */
6457 vmdkQueryRegions,
6458 /* pfnRegionListRelease */
6459 vmdkRegionListRelease,
6460 /* pfnGetImageFlags */
6461 vmdkGetImageFlags,
6462 /* pfnGetOpenFlags */
6463 vmdkGetOpenFlags,
6464 /* pfnSetOpenFlags */
6465 vmdkSetOpenFlags,
6466 /* pfnGetComment */
6467 vmdkGetComment,
6468 /* pfnSetComment */
6469 vmdkSetComment,
6470 /* pfnGetUuid */
6471 vmdkGetUuid,
6472 /* pfnSetUuid */
6473 vmdkSetUuid,
6474 /* pfnGetModificationUuid */
6475 vmdkGetModificationUuid,
6476 /* pfnSetModificationUuid */
6477 vmdkSetModificationUuid,
6478 /* pfnGetParentUuid */
6479 vmdkGetParentUuid,
6480 /* pfnSetParentUuid */
6481 vmdkSetParentUuid,
6482 /* pfnGetParentModificationUuid */
6483 vmdkGetParentModificationUuid,
6484 /* pfnSetParentModificationUuid */
6485 vmdkSetParentModificationUuid,
6486 /* pfnDump */
6487 vmdkDump,
6488 /* pfnGetTimestamp */
6489 NULL,
6490 /* pfnGetParentTimestamp */
6491 NULL,
6492 /* pfnSetParentTimestamp */
6493 NULL,
6494 /* pfnGetParentFilename */
6495 NULL,
6496 /* pfnSetParentFilename */
6497 NULL,
6498 /* pfnComposeLocation */
6499 genericFileComposeLocation,
6500 /* pfnComposeName */
6501 genericFileComposeName,
6502 /* pfnCompact */
6503 NULL,
6504 /* pfnResize */
6505 NULL,
6506 /* pfnRepair */
6507 NULL,
6508 /* pfnTraverseMetadata */
6509 NULL,
6510 /* u32VersionEnd */
6511 VD_IMGBACKEND_VERSION
6512};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette