VirtualBox

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

Last change on this file since 44233 was 44233, checked in by vboxsync, 12 years ago

Storage: Preparations for the sync/async I/O unification

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