VirtualBox

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

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

VD: API to repair corrupted images

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