VirtualBox

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

Last change on this file since 68155 was 67744, checked in by vboxsync, 7 years ago

Storage/VMDK: Missing check (regression from 110625)

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