VirtualBox

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

Last change on this file since 38462 was 38030, checked in by vboxsync, 13 years ago

VMDK: Remove debug logging and assertions from release builds

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