VirtualBox

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

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

Storage: Miscellaneous bug fixes to make the library work on big endian machines

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