VirtualBox

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

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

Storage/{VDI,VMDK}: Refuse to create an image with an invalid image type (e.g. Split2G for VDI images)

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