VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 20 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 379.2 KB
Line 
1/* $Id: VMDK.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VMDK disk image, core code.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_VD_VMDK
33#include <VBox/log.h> /* before VBox/vd-ifs.h */
34#include <VBox/vd-plugin.h>
35#include <VBox/err.h>
36
37#include <iprt/assert.h>
38#include <iprt/alloc.h>
39#include <iprt/base64.h>
40#include <iprt/ctype.h>
41#include <iprt/crc.h>
42#include <iprt/dvm.h>
43#include <iprt/uuid.h>
44#include <iprt/path.h>
45#include <iprt/rand.h>
46#include <iprt/string.h>
47#include <iprt/sort.h>
48#include <iprt/zip.h>
49#include <iprt/asm.h>
50#ifdef RT_OS_WINDOWS
51# include <iprt/utf16.h>
52# include <iprt/uni.h>
53# include <iprt/uni.h>
54# include <iprt/nt/nt-and-windows.h>
55# include <winioctl.h>
56#endif
57#ifdef RT_OS_LINUX
58# include <errno.h>
59# include <sys/stat.h>
60# include <iprt/dir.h>
61# include <iprt/symlink.h>
62# include <iprt/linux/sysfs.h>
63#endif
64#ifdef RT_OS_FREEBSD
65#include <libgeom.h>
66#include <sys/stat.h>
67#include <stdlib.h>
68#endif
69#ifdef RT_OS_SOLARIS
70#include <sys/dkio.h>
71#include <sys/vtoc.h>
72#include <sys/efi_partition.h>
73#include <unistd.h>
74#include <errno.h>
75#endif
76#ifdef RT_OS_DARWIN
77# include <sys/stat.h>
78# include <sys/disk.h>
79# include <errno.h>
80/* The following structure and IOCTLs are defined in znu bsd/sys/disk.h but
81 inside KERNEL ifdefs and thus stripped from the SDK edition of the header.
82 While we could try include the header from the Kernel.framework, it's a lot
83 easier to just add the structure and 4 defines here. */
84typedef struct
85{
86 uint64_t offset;
87 uint64_t length;
88 uint8_t reserved0128[12];
89 dev_t dev;
90} dk_physical_extent_t;
91# define DKIOCGETBASE _IOR( 'd', 73, uint64_t)
92# define DKIOCLOCKPHYSICALEXTENTS _IO( 'd', 81)
93# define DKIOCGETPHYSICALEXTENT _IOWR('d', 82, dk_physical_extent_t)
94# define DKIOCUNLOCKPHYSICALEXTENTS _IO( 'd', 83)
95#endif /* RT_OS_DARWIN */
96
97#include "VDBackends.h"
98
99
100/*********************************************************************************************************************************
101* Constants And Macros, Structures and Typedefs *
102*********************************************************************************************************************************/
103
104/** Maximum encoded string size (including NUL) we allow for VMDK images.
105 * Deliberately not set high to avoid running out of descriptor space. */
106#define VMDK_ENCODED_COMMENT_MAX 1024
107
108/** VMDK descriptor DDB entry for PCHS cylinders. */
109#define VMDK_DDB_GEO_PCHS_CYLINDERS "ddb.geometry.cylinders"
110
111/** VMDK descriptor DDB entry for PCHS heads. */
112#define VMDK_DDB_GEO_PCHS_HEADS "ddb.geometry.heads"
113
114/** VMDK descriptor DDB entry for PCHS sectors. */
115#define VMDK_DDB_GEO_PCHS_SECTORS "ddb.geometry.sectors"
116
117/** VMDK descriptor DDB entry for LCHS cylinders. */
118#define VMDK_DDB_GEO_LCHS_CYLINDERS "ddb.geometry.biosCylinders"
119
120/** VMDK descriptor DDB entry for LCHS heads. */
121#define VMDK_DDB_GEO_LCHS_HEADS "ddb.geometry.biosHeads"
122
123/** VMDK descriptor DDB entry for LCHS sectors. */
124#define VMDK_DDB_GEO_LCHS_SECTORS "ddb.geometry.biosSectors"
125
126/** VMDK descriptor DDB entry for image UUID. */
127#define VMDK_DDB_IMAGE_UUID "ddb.uuid.image"
128
129/** VMDK descriptor DDB entry for image modification UUID. */
130#define VMDK_DDB_MODIFICATION_UUID "ddb.uuid.modification"
131
132/** VMDK descriptor DDB entry for parent image UUID. */
133#define VMDK_DDB_PARENT_UUID "ddb.uuid.parent"
134
135/** VMDK descriptor DDB entry for parent image modification UUID. */
136#define VMDK_DDB_PARENT_MODIFICATION_UUID "ddb.uuid.parentmodification"
137
138/** No compression for streamOptimized files. */
139#define VMDK_COMPRESSION_NONE 0
140
141/** Deflate compression for streamOptimized files. */
142#define VMDK_COMPRESSION_DEFLATE 1
143
144/** Marker that the actual GD value is stored in the footer. */
145#define VMDK_GD_AT_END 0xffffffffffffffffULL
146
147/** Marker for end-of-stream in streamOptimized images. */
148#define VMDK_MARKER_EOS 0
149
150/** Marker for grain table block in streamOptimized images. */
151#define VMDK_MARKER_GT 1
152
153/** Marker for grain directory block in streamOptimized images. */
154#define VMDK_MARKER_GD 2
155
156/** Marker for footer in streamOptimized images. */
157#define VMDK_MARKER_FOOTER 3
158
159/** Marker for unknown purpose in streamOptimized images.
160 * Shows up in very recent images created by vSphere, but only sporadically.
161 * They "forgot" to document that one in the VMDK specification. */
162#define VMDK_MARKER_UNSPECIFIED 4
163
164/** Dummy marker for "don't check the marker value". */
165#define VMDK_MARKER_IGNORE 0xffffffffU
166
167/**
168 * Magic number for hosted images created by VMware Workstation 4, VMware
169 * Workstation 5, VMware Server or VMware Player. Not necessarily sparse.
170 */
171#define VMDK_SPARSE_MAGICNUMBER 0x564d444b /* 'V' 'M' 'D' 'K' */
172
173/** VMDK sector size in bytes. */
174#define VMDK_SECTOR_SIZE 512
175/** Max string buffer size for uint64_t with null term */
176#define UINT64_MAX_BUFF_SIZE 21
177/** Grain directory entry size in bytes */
178#define VMDK_GRAIN_DIR_ENTRY_SIZE 4
179/** Grain table size in bytes */
180#define VMDK_GRAIN_TABLE_SIZE 2048
181
182/**
183 * VMDK hosted binary extent header. The "Sparse" is a total misnomer, as
184 * this header is also used for monolithic flat images.
185 */
186#pragma pack(1)
187typedef struct SparseExtentHeader
188{
189 uint32_t magicNumber;
190 uint32_t version;
191 uint32_t flags;
192 uint64_t capacity;
193 uint64_t grainSize;
194 uint64_t descriptorOffset;
195 uint64_t descriptorSize;
196 uint32_t numGTEsPerGT;
197 uint64_t rgdOffset;
198 uint64_t gdOffset;
199 uint64_t overHead;
200 bool uncleanShutdown;
201 char singleEndLineChar;
202 char nonEndLineChar;
203 char doubleEndLineChar1;
204 char doubleEndLineChar2;
205 uint16_t compressAlgorithm;
206 uint8_t pad[433];
207} SparseExtentHeader;
208#pragma pack()
209
210/** The maximum allowed descriptor size in the extent header in sectors. */
211#define VMDK_SPARSE_DESCRIPTOR_SIZE_MAX UINT64_C(20480) /* 10MB */
212
213/** VMDK capacity for a single chunk when 2G splitting is turned on. Should be
214 * divisible by the default grain size (64K) */
215#define VMDK_2G_SPLIT_SIZE (2047 * 1024 * 1024)
216
217/** VMDK streamOptimized file format marker. The type field may or may not
218 * be actually valid, but there's always data to read there. */
219#pragma pack(1)
220typedef struct VMDKMARKER
221{
222 uint64_t uSector;
223 uint32_t cbSize;
224 uint32_t uType;
225} VMDKMARKER, *PVMDKMARKER;
226#pragma pack()
227
228
229/** Convert sector number/size to byte offset/size. */
230#define VMDK_SECTOR2BYTE(u) ((uint64_t)(u) << 9)
231
232/** Convert byte offset/size to sector number/size. */
233#define VMDK_BYTE2SECTOR(u) ((u) >> 9)
234
235/**
236 * VMDK extent type.
237 */
238typedef enum VMDKETYPE
239{
240 /** Hosted sparse extent. */
241 VMDKETYPE_HOSTED_SPARSE = 1,
242 /** Flat extent. */
243 VMDKETYPE_FLAT,
244 /** Zero extent. */
245 VMDKETYPE_ZERO,
246 /** VMFS extent, used by ESX. */
247 VMDKETYPE_VMFS
248} VMDKETYPE, *PVMDKETYPE;
249
250/**
251 * VMDK access type for a extent.
252 */
253typedef enum VMDKACCESS
254{
255 /** No access allowed. */
256 VMDKACCESS_NOACCESS = 0,
257 /** Read-only access. */
258 VMDKACCESS_READONLY,
259 /** Read-write access. */
260 VMDKACCESS_READWRITE
261} VMDKACCESS, *PVMDKACCESS;
262
263/** Forward declaration for PVMDKIMAGE. */
264typedef struct VMDKIMAGE *PVMDKIMAGE;
265
266/**
267 * Extents files entry. Used for opening a particular file only once.
268 */
269typedef struct VMDKFILE
270{
271 /** Pointer to file path. Local copy. */
272 const char *pszFilename;
273 /** Pointer to base name. Local copy. */
274 const char *pszBasename;
275 /** File open flags for consistency checking. */
276 unsigned fOpen;
277 /** Handle for sync/async file abstraction.*/
278 PVDIOSTORAGE pStorage;
279 /** Reference counter. */
280 unsigned uReferences;
281 /** Flag whether the file should be deleted on last close. */
282 bool fDelete;
283 /** Pointer to the image we belong to (for debugging purposes). */
284 PVMDKIMAGE pImage;
285 /** Pointer to next file descriptor. */
286 struct VMDKFILE *pNext;
287 /** Pointer to the previous file descriptor. */
288 struct VMDKFILE *pPrev;
289} VMDKFILE, *PVMDKFILE;
290
291/**
292 * VMDK extent data structure.
293 */
294typedef struct VMDKEXTENT
295{
296 /** File handle. */
297 PVMDKFILE pFile;
298 /** Base name of the image extent. */
299 const char *pszBasename;
300 /** Full name of the image extent. */
301 const char *pszFullname;
302 /** Number of sectors in this extent. */
303 uint64_t cSectors;
304 /** Number of sectors per block (grain in VMDK speak). */
305 uint64_t cSectorsPerGrain;
306 /** Starting sector number of descriptor. */
307 uint64_t uDescriptorSector;
308 /** Size of descriptor in sectors. */
309 uint64_t cDescriptorSectors;
310 /** Starting sector number of grain directory. */
311 uint64_t uSectorGD;
312 /** Starting sector number of redundant grain directory. */
313 uint64_t uSectorRGD;
314 /** Total number of metadata sectors. */
315 uint64_t cOverheadSectors;
316 /** Nominal size (i.e. as described by the descriptor) of this extent. */
317 uint64_t cNominalSectors;
318 /** Sector offset (i.e. as described by the descriptor) of this extent. */
319 uint64_t uSectorOffset;
320 /** Number of entries in a grain table. */
321 uint32_t cGTEntries;
322 /** Number of sectors reachable via a grain directory entry. */
323 uint32_t cSectorsPerGDE;
324 /** Number of entries in the grain directory. */
325 uint32_t cGDEntries;
326 /** Pointer to the next free sector. Legacy information. Do not use. */
327 uint32_t uFreeSector;
328 /** Number of this extent in the list of images. */
329 uint32_t uExtent;
330 /** Pointer to the descriptor (NULL if no descriptor in this extent). */
331 char *pDescData;
332 /** Pointer to the grain directory. */
333 uint32_t *pGD;
334 /** Pointer to the redundant grain directory. */
335 uint32_t *pRGD;
336 /** VMDK version of this extent. 1=1.0/1.1 */
337 uint32_t uVersion;
338 /** Type of this extent. */
339 VMDKETYPE enmType;
340 /** Access to this extent. */
341 VMDKACCESS enmAccess;
342 /** Flag whether this extent is marked as unclean. */
343 bool fUncleanShutdown;
344 /** Flag whether the metadata in the extent header needs to be updated. */
345 bool fMetaDirty;
346 /** Flag whether there is a footer in this extent. */
347 bool fFooter;
348 /** Compression type for this extent. */
349 uint16_t uCompression;
350 /** Append position for writing new grain. Only for sparse extents. */
351 uint64_t uAppendPosition;
352 /** Last grain which was accessed. Only for streamOptimized extents. */
353 uint32_t uLastGrainAccess;
354 /** Starting sector corresponding to the grain buffer. */
355 uint32_t uGrainSectorAbs;
356 /** Grain number corresponding to the grain buffer. */
357 uint32_t uGrain;
358 /** Actual size of the compressed data, only valid for reading. */
359 uint32_t cbGrainStreamRead;
360 /** Size of compressed grain buffer for streamOptimized extents. */
361 size_t cbCompGrain;
362 /** Compressed grain buffer for streamOptimized extents, with marker. */
363 void *pvCompGrain;
364 /** Decompressed grain buffer for streamOptimized extents. */
365 void *pvGrain;
366 /** Reference to the image in which this extent is used. Do not use this
367 * on a regular basis to avoid passing pImage references to functions
368 * explicitly. */
369 struct VMDKIMAGE *pImage;
370} VMDKEXTENT, *PVMDKEXTENT;
371
372/**
373 * Grain table cache size. Allocated per image.
374 */
375#define VMDK_GT_CACHE_SIZE 256
376
377/**
378 * Grain table block size. Smaller than an actual grain table block to allow
379 * more grain table blocks to be cached without having to allocate excessive
380 * amounts of memory for the cache.
381 */
382#define VMDK_GT_CACHELINE_SIZE 128
383
384
385/**
386 * Maximum number of lines in a descriptor file. Not worth the effort of
387 * making it variable. Descriptor files are generally very short (~20 lines),
388 * with the exception of sparse files split in 2G chunks, which need for the
389 * maximum size (almost 2T) exactly 1025 lines for the disk database.
390 */
391#define VMDK_DESCRIPTOR_LINES_MAX 1100U
392
393/**
394 * Parsed descriptor information. Allows easy access and update of the
395 * descriptor (whether separate file or not). Free form text files suck.
396 */
397typedef struct VMDKDESCRIPTOR
398{
399 /** Line number of first entry of the disk descriptor. */
400 unsigned uFirstDesc;
401 /** Line number of first entry in the extent description. */
402 unsigned uFirstExtent;
403 /** Line number of first disk database entry. */
404 unsigned uFirstDDB;
405 /** Total number of lines. */
406 unsigned cLines;
407 /** Total amount of memory available for the descriptor. */
408 size_t cbDescAlloc;
409 /** Set if descriptor has been changed and not yet written to disk. */
410 bool fDirty;
411 /** Array of pointers to the data in the descriptor. */
412 char *aLines[VMDK_DESCRIPTOR_LINES_MAX];
413 /** Array of line indices pointing to the next non-comment line. */
414 unsigned aNextLines[VMDK_DESCRIPTOR_LINES_MAX];
415} VMDKDESCRIPTOR, *PVMDKDESCRIPTOR;
416
417
418/**
419 * Cache entry for translating extent/sector to a sector number in that
420 * extent.
421 */
422typedef struct VMDKGTCACHEENTRY
423{
424 /** Extent number for which this entry is valid. */
425 uint32_t uExtent;
426 /** GT data block number. */
427 uint64_t uGTBlock;
428 /** Data part of the cache entry. */
429 uint32_t aGTData[VMDK_GT_CACHELINE_SIZE];
430} VMDKGTCACHEENTRY, *PVMDKGTCACHEENTRY;
431
432/**
433 * Cache data structure for blocks of grain table entries. For now this is a
434 * fixed size direct mapping cache, but this should be adapted to the size of
435 * the sparse image and maybe converted to a set-associative cache. The
436 * implementation below implements a write-through cache with write allocate.
437 */
438typedef struct VMDKGTCACHE
439{
440 /** Cache entries. */
441 VMDKGTCACHEENTRY aGTCache[VMDK_GT_CACHE_SIZE];
442 /** Number of cache entries (currently unused). */
443 unsigned cEntries;
444} VMDKGTCACHE, *PVMDKGTCACHE;
445
446/**
447 * Complete VMDK image data structure. Mainly a collection of extents and a few
448 * extra global data fields.
449 */
450typedef struct VMDKIMAGE
451{
452 /** Image name. */
453 const char *pszFilename;
454 /** Descriptor file if applicable. */
455 PVMDKFILE pFile;
456
457 /** Pointer to the per-disk VD interface list. */
458 PVDINTERFACE pVDIfsDisk;
459 /** Pointer to the per-image VD interface list. */
460 PVDINTERFACE pVDIfsImage;
461
462 /** Error interface. */
463 PVDINTERFACEERROR pIfError;
464 /** I/O interface. */
465 PVDINTERFACEIOINT pIfIo;
466
467
468 /** Pointer to the image extents. */
469 PVMDKEXTENT pExtents;
470 /** Number of image extents. */
471 unsigned cExtents;
472 /** Pointer to the files list, for opening a file referenced multiple
473 * times only once (happens mainly with raw partition access). */
474 PVMDKFILE pFiles;
475
476 /**
477 * Pointer to an array of segment entries for async I/O.
478 * This is an optimization because the task number to submit is not known
479 * and allocating/freeing an array in the read/write functions every time
480 * is too expensive.
481 */
482 PPDMDATASEG paSegments;
483 /** Entries available in the segments array. */
484 unsigned cSegments;
485
486 /** Open flags passed by VBoxHD layer. */
487 unsigned uOpenFlags;
488 /** Image flags defined during creation or determined during open. */
489 unsigned uImageFlags;
490 /** Total size of the image. */
491 uint64_t cbSize;
492 /** Physical geometry of this image. */
493 VDGEOMETRY PCHSGeometry;
494 /** Logical geometry of this image. */
495 VDGEOMETRY LCHSGeometry;
496 /** Image UUID. */
497 RTUUID ImageUuid;
498 /** Image modification UUID. */
499 RTUUID ModificationUuid;
500 /** Parent image UUID. */
501 RTUUID ParentUuid;
502 /** Parent image modification UUID. */
503 RTUUID ParentModificationUuid;
504
505 /** Pointer to grain table cache, if this image contains sparse extents. */
506 PVMDKGTCACHE pGTCache;
507 /** Pointer to the descriptor (NULL if no separate descriptor file). */
508 char *pDescData;
509 /** Allocation size of the descriptor file. */
510 size_t cbDescAlloc;
511 /** Parsed descriptor file content. */
512 VMDKDESCRIPTOR Descriptor;
513 /** The static region list. */
514 VDREGIONLIST RegionList;
515} VMDKIMAGE;
516
517
518/** State for the input/output callout of the inflate reader/deflate writer. */
519typedef struct VMDKCOMPRESSIO
520{
521 /* Image this operation relates to. */
522 PVMDKIMAGE pImage;
523 /* Current read position. */
524 ssize_t iOffset;
525 /* Size of the compressed grain buffer (available data). */
526 size_t cbCompGrain;
527 /* Pointer to the compressed grain buffer. */
528 void *pvCompGrain;
529} VMDKCOMPRESSIO;
530
531
532/** Tracks async grain allocation. */
533typedef struct VMDKGRAINALLOCASYNC
534{
535 /** Flag whether the allocation failed. */
536 bool fIoErr;
537 /** Current number of transfers pending.
538 * If reached 0 and there is an error the old state is restored. */
539 unsigned cIoXfersPending;
540 /** Sector number */
541 uint64_t uSector;
542 /** Flag whether the grain table needs to be updated. */
543 bool fGTUpdateNeeded;
544 /** Extent the allocation happens. */
545 PVMDKEXTENT pExtent;
546 /** Position of the new grain, required for the grain table update. */
547 uint64_t uGrainOffset;
548 /** Grain table sector. */
549 uint64_t uGTSector;
550 /** Backup grain table sector. */
551 uint64_t uRGTSector;
552} VMDKGRAINALLOCASYNC, *PVMDKGRAINALLOCASYNC;
553
554/**
555 * State information for vmdkRename() and helpers.
556 */
557typedef struct VMDKRENAMESTATE
558{
559 /** Array of old filenames. */
560 char **apszOldName;
561 /** Array of new filenames. */
562 char **apszNewName;
563 /** Array of new lines in the extent descriptor. */
564 char **apszNewLines;
565 /** Name of the old descriptor file if not a sparse image. */
566 char *pszOldDescName;
567 /** Flag whether we called vmdkFreeImage(). */
568 bool fImageFreed;
569 /** Flag whther the descriptor is embedded in the image (sparse) or
570 * in a separate file. */
571 bool fEmbeddedDesc;
572 /** Number of extents in the image. */
573 unsigned cExtents;
574 /** New base filename. */
575 char *pszNewBaseName;
576 /** The old base filename. */
577 char *pszOldBaseName;
578 /** New full filename. */
579 char *pszNewFullName;
580 /** Old full filename. */
581 char *pszOldFullName;
582 /** The old image name. */
583 const char *pszOldImageName;
584 /** Copy of the original VMDK descriptor. */
585 VMDKDESCRIPTOR DescriptorCopy;
586 /** Copy of the extent state for sparse images. */
587 VMDKEXTENT ExtentCopy;
588} VMDKRENAMESTATE;
589/** Pointer to a VMDK rename state. */
590typedef VMDKRENAMESTATE *PVMDKRENAMESTATE;
591
592
593/*********************************************************************************************************************************
594* Static Variables *
595*********************************************************************************************************************************/
596
597/** NULL-terminated array of supported file extensions. */
598static const VDFILEEXTENSION s_aVmdkFileExtensions[] =
599{
600 {"vmdk", VDTYPE_HDD},
601 {NULL, VDTYPE_INVALID}
602};
603
604/** NULL-terminated array of configuration option. */
605static const VDCONFIGINFO s_aVmdkConfigInfo[] =
606{
607 /* Options for VMDK raw disks */
608 { "RawDrive", NULL, VDCFGVALUETYPE_STRING, 0 },
609 { "Partitions", NULL, VDCFGVALUETYPE_STRING, 0 },
610 { "BootSector", NULL, VDCFGVALUETYPE_BYTES, 0 },
611 { "Relative", NULL, VDCFGVALUETYPE_INTEGER, 0 },
612
613 /* End of options list */
614 { NULL, NULL, VDCFGVALUETYPE_INTEGER, 0 }
615};
616
617
618/*********************************************************************************************************************************
619* Internal Functions *
620*********************************************************************************************************************************/
621
622static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent);
623static int vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
624 bool fDelete);
625
626static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents);
627static int vmdkFlushImage(PVMDKIMAGE pImage, PVDIOCTX pIoCtx);
628static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment);
629static int vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete, bool fFlush);
630
631static DECLCALLBACK(int) vmdkAllocGrainComplete(void *pBackendData, PVDIOCTX pIoCtx,
632 void *pvUser, int rcReq);
633
634/**
635 * Internal: open a file (using a file descriptor cache to ensure each file
636 * is only opened once - anything else can cause locking problems).
637 */
638static int vmdkFileOpen(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile,
639 const char *pszBasename, const char *pszFilename, uint32_t fOpen)
640{
641 int rc = VINF_SUCCESS;
642 PVMDKFILE pVmdkFile;
643
644 for (pVmdkFile = pImage->pFiles;
645 pVmdkFile != NULL;
646 pVmdkFile = pVmdkFile->pNext)
647 {
648 if (!strcmp(pszFilename, pVmdkFile->pszFilename))
649 {
650 Assert(fOpen == pVmdkFile->fOpen);
651 pVmdkFile->uReferences++;
652
653 *ppVmdkFile = pVmdkFile;
654
655 return rc;
656 }
657 }
658
659 /* If we get here, there's no matching entry in the cache. */
660 pVmdkFile = (PVMDKFILE)RTMemAllocZ(sizeof(VMDKFILE));
661 if (!pVmdkFile)
662 {
663 *ppVmdkFile = NULL;
664 return VERR_NO_MEMORY;
665 }
666
667 pVmdkFile->pszFilename = RTStrDup(pszFilename);
668 if (!pVmdkFile->pszFilename)
669 {
670 RTMemFree(pVmdkFile);
671 *ppVmdkFile = NULL;
672 return VERR_NO_MEMORY;
673 }
674
675 if (pszBasename)
676 {
677 pVmdkFile->pszBasename = RTStrDup(pszBasename);
678 if (!pVmdkFile->pszBasename)
679 {
680 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
681 RTMemFree(pVmdkFile);
682 *ppVmdkFile = NULL;
683 return VERR_NO_MEMORY;
684 }
685 }
686
687 pVmdkFile->fOpen = fOpen;
688
689 rc = vdIfIoIntFileOpen(pImage->pIfIo, pszFilename, fOpen,
690 &pVmdkFile->pStorage);
691 if (RT_SUCCESS(rc))
692 {
693 pVmdkFile->uReferences = 1;
694 pVmdkFile->pImage = pImage;
695 pVmdkFile->pNext = pImage->pFiles;
696 if (pImage->pFiles)
697 pImage->pFiles->pPrev = pVmdkFile;
698 pImage->pFiles = pVmdkFile;
699 *ppVmdkFile = pVmdkFile;
700 }
701 else
702 {
703 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
704 RTMemFree(pVmdkFile);
705 *ppVmdkFile = NULL;
706 }
707
708 return rc;
709}
710
711/**
712 * Internal: close a file, updating the file descriptor cache.
713 */
714static int vmdkFileClose(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile, bool fDelete)
715{
716 int rc = VINF_SUCCESS;
717 PVMDKFILE pVmdkFile = *ppVmdkFile;
718
719 AssertPtr(pVmdkFile);
720
721 pVmdkFile->fDelete |= fDelete;
722 Assert(pVmdkFile->uReferences);
723 pVmdkFile->uReferences--;
724 if (pVmdkFile->uReferences == 0)
725 {
726 PVMDKFILE pPrev;
727 PVMDKFILE pNext;
728
729 /* Unchain the element from the list. */
730 pPrev = pVmdkFile->pPrev;
731 pNext = pVmdkFile->pNext;
732
733 if (pNext)
734 pNext->pPrev = pPrev;
735 if (pPrev)
736 pPrev->pNext = pNext;
737 else
738 pImage->pFiles = pNext;
739
740 rc = vdIfIoIntFileClose(pImage->pIfIo, pVmdkFile->pStorage);
741
742 bool fFileDel = pVmdkFile->fDelete;
743 if ( pVmdkFile->pszBasename
744 && fFileDel)
745 {
746 const char *pszSuffix = RTPathSuffix(pVmdkFile->pszBasename);
747 if ( RTPathHasPath(pVmdkFile->pszBasename)
748 || !pszSuffix
749 || ( strcmp(pszSuffix, ".vmdk")
750 && strcmp(pszSuffix, ".bin")
751 && strcmp(pszSuffix, ".img")))
752 fFileDel = false;
753 }
754
755 if (fFileDel)
756 {
757 int rc2 = vdIfIoIntFileDelete(pImage->pIfIo, pVmdkFile->pszFilename);
758 if (RT_SUCCESS(rc))
759 rc = rc2;
760 }
761 else if (pVmdkFile->fDelete)
762 LogRel(("VMDK: Denying deletion of %s\n", pVmdkFile->pszBasename));
763 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
764 if (pVmdkFile->pszBasename)
765 RTStrFree((char *)(void *)pVmdkFile->pszBasename);
766 RTMemFree(pVmdkFile);
767 }
768
769 *ppVmdkFile = NULL;
770 return rc;
771}
772
773/*#define VMDK_USE_BLOCK_DECOMP_API - test and enable */
774#ifndef VMDK_USE_BLOCK_DECOMP_API
775static DECLCALLBACK(int) vmdkFileInflateHelper(void *pvUser, void *pvBuf, size_t cbBuf, size_t *pcbBuf)
776{
777 VMDKCOMPRESSIO *pInflateState = (VMDKCOMPRESSIO *)pvUser;
778 size_t cbInjected = 0;
779
780 Assert(cbBuf);
781 if (pInflateState->iOffset < 0)
782 {
783 *(uint8_t *)pvBuf = RTZIPTYPE_ZLIB;
784 pvBuf = (uint8_t *)pvBuf + 1;
785 cbBuf--;
786 cbInjected = 1;
787 pInflateState->iOffset = RT_UOFFSETOF(VMDKMARKER, uType);
788 }
789 if (!cbBuf)
790 {
791 if (pcbBuf)
792 *pcbBuf = cbInjected;
793 return VINF_SUCCESS;
794 }
795 cbBuf = RT_MIN(cbBuf, pInflateState->cbCompGrain - pInflateState->iOffset);
796 memcpy(pvBuf,
797 (uint8_t *)pInflateState->pvCompGrain + pInflateState->iOffset,
798 cbBuf);
799 pInflateState->iOffset += cbBuf;
800 Assert(pcbBuf);
801 *pcbBuf = cbBuf + cbInjected;
802 return VINF_SUCCESS;
803}
804#endif
805
806/**
807 * Internal: read from a file and inflate the compressed data,
808 * distinguishing between async and normal operation
809 */
810DECLINLINE(int) vmdkFileInflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
811 uint64_t uOffset, void *pvBuf,
812 size_t cbToRead, const void *pcvMarker,
813 uint64_t *puLBA, uint32_t *pcbMarkerData)
814{
815 int rc;
816#ifndef VMDK_USE_BLOCK_DECOMP_API
817 PRTZIPDECOMP pZip = NULL;
818#endif
819 VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
820 size_t cbCompSize, cbActuallyRead;
821
822 if (!pcvMarker)
823 {
824 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
825 uOffset, pMarker, RT_UOFFSETOF(VMDKMARKER, uType));
826 if (RT_FAILURE(rc))
827 return rc;
828 }
829 else
830 {
831 memcpy(pMarker, pcvMarker, RT_UOFFSETOF(VMDKMARKER, uType));
832 /* pcvMarker endianness has already been partially transformed, fix it */
833 pMarker->uSector = RT_H2LE_U64(pMarker->uSector);
834 pMarker->cbSize = RT_H2LE_U32(pMarker->cbSize);
835 }
836
837 cbCompSize = RT_LE2H_U32(pMarker->cbSize);
838 if (cbCompSize == 0)
839 {
840 AssertMsgFailed(("VMDK: corrupted marker\n"));
841 return VERR_VD_VMDK_INVALID_FORMAT;
842 }
843
844 /* Sanity check - the expansion ratio should be much less than 2. */
845 Assert(cbCompSize < 2 * cbToRead);
846 if (cbCompSize >= 2 * cbToRead)
847 return VERR_VD_VMDK_INVALID_FORMAT;
848
849 /* Compressed grain marker. Data follows immediately. */
850 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
851 uOffset + RT_UOFFSETOF(VMDKMARKER, uType),
852 (uint8_t *)pExtent->pvCompGrain
853 + RT_UOFFSETOF(VMDKMARKER, uType),
854 RT_ALIGN_Z( cbCompSize
855 + RT_UOFFSETOF(VMDKMARKER, uType),
856 512)
857 - RT_UOFFSETOF(VMDKMARKER, uType));
858
859 if (puLBA)
860 *puLBA = RT_LE2H_U64(pMarker->uSector);
861 if (pcbMarkerData)
862 *pcbMarkerData = RT_ALIGN( cbCompSize
863 + RT_UOFFSETOF(VMDKMARKER, uType),
864 512);
865
866#ifdef VMDK_USE_BLOCK_DECOMP_API
867 rc = RTZipBlockDecompress(RTZIPTYPE_ZLIB, 0 /*fFlags*/,
868 pExtent->pvCompGrain, cbCompSize + RT_UOFFSETOF(VMDKMARKER, uType), NULL,
869 pvBuf, cbToRead, &cbActuallyRead);
870#else
871 VMDKCOMPRESSIO InflateState;
872 InflateState.pImage = pImage;
873 InflateState.iOffset = -1;
874 InflateState.cbCompGrain = cbCompSize + RT_UOFFSETOF(VMDKMARKER, uType);
875 InflateState.pvCompGrain = pExtent->pvCompGrain;
876
877 rc = RTZipDecompCreate(&pZip, &InflateState, vmdkFileInflateHelper);
878 if (RT_FAILURE(rc))
879 return rc;
880 rc = RTZipDecompress(pZip, pvBuf, cbToRead, &cbActuallyRead);
881 RTZipDecompDestroy(pZip);
882#endif /* !VMDK_USE_BLOCK_DECOMP_API */
883 if (RT_FAILURE(rc))
884 {
885 if (rc == VERR_ZIP_CORRUPTED)
886 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: Compressed image is corrupted '%s'"), pExtent->pszFullname);
887 return rc;
888 }
889 if (cbActuallyRead != cbToRead)
890 rc = VERR_VD_VMDK_INVALID_FORMAT;
891 return rc;
892}
893
894static DECLCALLBACK(int) vmdkFileDeflateHelper(void *pvUser, const void *pvBuf, size_t cbBuf)
895{
896 VMDKCOMPRESSIO *pDeflateState = (VMDKCOMPRESSIO *)pvUser;
897
898 Assert(cbBuf);
899 if (pDeflateState->iOffset < 0)
900 {
901 pvBuf = (const uint8_t *)pvBuf + 1;
902 cbBuf--;
903 pDeflateState->iOffset = RT_UOFFSETOF(VMDKMARKER, uType);
904 }
905 if (!cbBuf)
906 return VINF_SUCCESS;
907 if (pDeflateState->iOffset + cbBuf > pDeflateState->cbCompGrain)
908 return VERR_BUFFER_OVERFLOW;
909 memcpy((uint8_t *)pDeflateState->pvCompGrain + pDeflateState->iOffset,
910 pvBuf, cbBuf);
911 pDeflateState->iOffset += cbBuf;
912 return VINF_SUCCESS;
913}
914
915/**
916 * Internal: deflate the uncompressed data and write to a file,
917 * distinguishing between async and normal operation
918 */
919DECLINLINE(int) vmdkFileDeflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
920 uint64_t uOffset, const void *pvBuf,
921 size_t cbToWrite, uint64_t uLBA,
922 uint32_t *pcbMarkerData)
923{
924 int rc;
925 PRTZIPCOMP pZip = NULL;
926 VMDKCOMPRESSIO DeflateState;
927
928 DeflateState.pImage = pImage;
929 DeflateState.iOffset = -1;
930 DeflateState.cbCompGrain = pExtent->cbCompGrain;
931 DeflateState.pvCompGrain = pExtent->pvCompGrain;
932
933 rc = RTZipCompCreate(&pZip, &DeflateState, vmdkFileDeflateHelper,
934 RTZIPTYPE_ZLIB, RTZIPLEVEL_DEFAULT);
935 if (RT_FAILURE(rc))
936 return rc;
937 rc = RTZipCompress(pZip, pvBuf, cbToWrite);
938 if (RT_SUCCESS(rc))
939 rc = RTZipCompFinish(pZip);
940 RTZipCompDestroy(pZip);
941 if (RT_SUCCESS(rc))
942 {
943 Assert( DeflateState.iOffset > 0
944 && (size_t)DeflateState.iOffset <= DeflateState.cbCompGrain);
945
946 /* pad with zeroes to get to a full sector size */
947 uint32_t uSize = DeflateState.iOffset;
948 if (uSize % 512)
949 {
950 uint32_t uSizeAlign = RT_ALIGN(uSize, 512);
951 memset((uint8_t *)pExtent->pvCompGrain + uSize, '\0',
952 uSizeAlign - uSize);
953 uSize = uSizeAlign;
954 }
955
956 if (pcbMarkerData)
957 *pcbMarkerData = uSize;
958
959 /* Compressed grain marker. Data follows immediately. */
960 VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
961 pMarker->uSector = RT_H2LE_U64(uLBA);
962 pMarker->cbSize = RT_H2LE_U32( DeflateState.iOffset
963 - RT_UOFFSETOF(VMDKMARKER, uType));
964 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
965 uOffset, pMarker, uSize);
966 if (RT_FAILURE(rc))
967 return rc;
968 }
969 return rc;
970}
971
972
973/**
974 * Internal: check if all files are closed, prevent leaking resources.
975 */
976static int vmdkFileCheckAllClose(PVMDKIMAGE pImage)
977{
978 int rc = VINF_SUCCESS, rc2;
979 PVMDKFILE pVmdkFile;
980
981 Assert(pImage->pFiles == NULL);
982 for (pVmdkFile = pImage->pFiles;
983 pVmdkFile != NULL;
984 pVmdkFile = pVmdkFile->pNext)
985 {
986 LogRel(("VMDK: leaking reference to file \"%s\"\n",
987 pVmdkFile->pszFilename));
988 pImage->pFiles = pVmdkFile->pNext;
989
990 rc2 = vmdkFileClose(pImage, &pVmdkFile, pVmdkFile->fDelete);
991
992 if (RT_SUCCESS(rc))
993 rc = rc2;
994 }
995 return rc;
996}
997
998/**
999 * Internal: truncate a string (at a UTF8 code point boundary) and encode the
1000 * critical non-ASCII characters.
1001 */
1002static char *vmdkEncodeString(const char *psz)
1003{
1004 char szEnc[VMDK_ENCODED_COMMENT_MAX + 3];
1005 char *pszDst = szEnc;
1006
1007 AssertPtr(psz);
1008
1009 for (; *psz; psz = RTStrNextCp(psz))
1010 {
1011 char *pszDstPrev = pszDst;
1012 RTUNICP Cp = RTStrGetCp(psz);
1013 if (Cp == '\\')
1014 {
1015 pszDst = RTStrPutCp(pszDst, Cp);
1016 pszDst = RTStrPutCp(pszDst, Cp);
1017 }
1018 else if (Cp == '\n')
1019 {
1020 pszDst = RTStrPutCp(pszDst, '\\');
1021 pszDst = RTStrPutCp(pszDst, 'n');
1022 }
1023 else if (Cp == '\r')
1024 {
1025 pszDst = RTStrPutCp(pszDst, '\\');
1026 pszDst = RTStrPutCp(pszDst, 'r');
1027 }
1028 else
1029 pszDst = RTStrPutCp(pszDst, Cp);
1030 if (pszDst - szEnc >= VMDK_ENCODED_COMMENT_MAX - 1)
1031 {
1032 pszDst = pszDstPrev;
1033 break;
1034 }
1035 }
1036 *pszDst = '\0';
1037 return RTStrDup(szEnc);
1038}
1039
1040/**
1041 * Internal: decode a string and store it into the specified string.
1042 */
1043static int vmdkDecodeString(const char *pszEncoded, char *psz, size_t cb)
1044{
1045 int rc = VINF_SUCCESS;
1046 char szBuf[4];
1047
1048 if (!cb)
1049 return VERR_BUFFER_OVERFLOW;
1050
1051 AssertPtr(psz);
1052
1053 for (; *pszEncoded; pszEncoded = RTStrNextCp(pszEncoded))
1054 {
1055 char *pszDst = szBuf;
1056 RTUNICP Cp = RTStrGetCp(pszEncoded);
1057 if (Cp == '\\')
1058 {
1059 pszEncoded = RTStrNextCp(pszEncoded);
1060 RTUNICP CpQ = RTStrGetCp(pszEncoded);
1061 if (CpQ == 'n')
1062 RTStrPutCp(pszDst, '\n');
1063 else if (CpQ == 'r')
1064 RTStrPutCp(pszDst, '\r');
1065 else if (CpQ == '\0')
1066 {
1067 rc = VERR_VD_VMDK_INVALID_HEADER;
1068 break;
1069 }
1070 else
1071 RTStrPutCp(pszDst, CpQ);
1072 }
1073 else
1074 pszDst = RTStrPutCp(pszDst, Cp);
1075
1076 /* Need to leave space for terminating NUL. */
1077 if ((size_t)(pszDst - szBuf) + 1 >= cb)
1078 {
1079 rc = VERR_BUFFER_OVERFLOW;
1080 break;
1081 }
1082 memcpy(psz, szBuf, pszDst - szBuf);
1083 psz += pszDst - szBuf;
1084 }
1085 *psz = '\0';
1086 return rc;
1087}
1088
1089/**
1090 * Internal: free all buffers associated with grain directories.
1091 */
1092static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent)
1093{
1094 if (pExtent->pGD)
1095 {
1096 RTMemFree(pExtent->pGD);
1097 pExtent->pGD = NULL;
1098 }
1099 if (pExtent->pRGD)
1100 {
1101 RTMemFree(pExtent->pRGD);
1102 pExtent->pRGD = NULL;
1103 }
1104}
1105
1106/**
1107 * Internal: allocate the compressed/uncompressed buffers for streamOptimized
1108 * images.
1109 */
1110static int vmdkAllocStreamBuffers(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1111{
1112 int rc = VINF_SUCCESS;
1113
1114 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
1115 {
1116 /* streamOptimized extents need a compressed grain buffer, which must
1117 * be big enough to hold uncompressible data (which needs ~8 bytes
1118 * more than the uncompressed data), the marker and padding. */
1119 pExtent->cbCompGrain = RT_ALIGN_Z( VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
1120 + 8 + sizeof(VMDKMARKER), 512);
1121 pExtent->pvCompGrain = RTMemAlloc(pExtent->cbCompGrain);
1122 if (RT_LIKELY(pExtent->pvCompGrain))
1123 {
1124 /* streamOptimized extents need a decompressed grain buffer. */
1125 pExtent->pvGrain = RTMemAlloc(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1126 if (!pExtent->pvGrain)
1127 rc = VERR_NO_MEMORY;
1128 }
1129 else
1130 rc = VERR_NO_MEMORY;
1131 }
1132
1133 if (RT_FAILURE(rc))
1134 vmdkFreeStreamBuffers(pExtent);
1135 return rc;
1136}
1137
1138/**
1139 * Internal: allocate all buffers associated with grain directories.
1140 */
1141static int vmdkAllocGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1142{
1143 RT_NOREF1(pImage);
1144 int rc = VINF_SUCCESS;
1145 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1146
1147 pExtent->pGD = (uint32_t *)RTMemAllocZ(cbGD);
1148 if (RT_LIKELY(pExtent->pGD))
1149 {
1150 if (pExtent->uSectorRGD)
1151 {
1152 pExtent->pRGD = (uint32_t *)RTMemAllocZ(cbGD);
1153 if (RT_UNLIKELY(!pExtent->pRGD))
1154 rc = VERR_NO_MEMORY;
1155 }
1156 }
1157 else
1158 rc = VERR_NO_MEMORY;
1159
1160 if (RT_FAILURE(rc))
1161 vmdkFreeGrainDirectory(pExtent);
1162 return rc;
1163}
1164
1165/**
1166 * Converts the grain directory from little to host endianess.
1167 *
1168 * @returns nothing.
1169 * @param pGD The grain directory.
1170 * @param cGDEntries Number of entries in the grain directory to convert.
1171 */
1172DECLINLINE(void) vmdkGrainDirectoryConvToHost(uint32_t *pGD, uint32_t cGDEntries)
1173{
1174 uint32_t *pGDTmp = pGD;
1175
1176 for (uint32_t i = 0; i < cGDEntries; i++, pGDTmp++)
1177 *pGDTmp = RT_LE2H_U32(*pGDTmp);
1178}
1179
1180/**
1181 * Read the grain directory and allocated grain tables verifying them against
1182 * their back up copies if available.
1183 *
1184 * @returns VBox status code.
1185 * @param pImage Image instance data.
1186 * @param pExtent The VMDK extent.
1187 */
1188static int vmdkReadGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1189{
1190 int rc = VINF_SUCCESS;
1191 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1192
1193 AssertReturn(( pExtent->enmType == VMDKETYPE_HOSTED_SPARSE
1194 && pExtent->uSectorGD != VMDK_GD_AT_END
1195 && pExtent->uSectorRGD != VMDK_GD_AT_END), VERR_INTERNAL_ERROR);
1196
1197 rc = vmdkAllocGrainDirectory(pImage, pExtent);
1198 if (RT_SUCCESS(rc))
1199 {
1200 /* The VMDK 1.1 spec seems to talk about compressed grain directories,
1201 * but in reality they are not compressed. */
1202 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1203 VMDK_SECTOR2BYTE(pExtent->uSectorGD),
1204 pExtent->pGD, cbGD);
1205 if (RT_SUCCESS(rc))
1206 {
1207 vmdkGrainDirectoryConvToHost(pExtent->pGD, pExtent->cGDEntries);
1208
1209 if ( pExtent->uSectorRGD
1210 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS))
1211 {
1212 /* The VMDK 1.1 spec seems to talk about compressed grain directories,
1213 * but in reality they are not compressed. */
1214 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1215 VMDK_SECTOR2BYTE(pExtent->uSectorRGD),
1216 pExtent->pRGD, cbGD);
1217 if (RT_SUCCESS(rc))
1218 {
1219 vmdkGrainDirectoryConvToHost(pExtent->pRGD, pExtent->cGDEntries);
1220
1221 /* Check grain table and redundant grain table for consistency. */
1222 size_t cbGT = pExtent->cGTEntries * sizeof(uint32_t);
1223 size_t cbGTBuffers = cbGT; /* Start with space for one GT. */
1224 size_t cbGTBuffersMax = _1M;
1225
1226 uint32_t *pTmpGT1 = (uint32_t *)RTMemAlloc(cbGTBuffers);
1227 uint32_t *pTmpGT2 = (uint32_t *)RTMemAlloc(cbGTBuffers);
1228
1229 if ( !pTmpGT1
1230 || !pTmpGT2)
1231 rc = VERR_NO_MEMORY;
1232
1233 size_t i = 0;
1234 uint32_t *pGDTmp = pExtent->pGD;
1235 uint32_t *pRGDTmp = pExtent->pRGD;
1236
1237 /* Loop through all entries. */
1238 while (i < pExtent->cGDEntries)
1239 {
1240 uint32_t uGTStart = *pGDTmp;
1241 uint32_t uRGTStart = *pRGDTmp;
1242 size_t cbGTRead = cbGT;
1243
1244 /* If no grain table is allocated skip the entry. */
1245 if (*pGDTmp == 0 && *pRGDTmp == 0)
1246 {
1247 i++;
1248 continue;
1249 }
1250
1251 if (*pGDTmp == 0 || *pRGDTmp == 0 || *pGDTmp == *pRGDTmp)
1252 {
1253 /* Just one grain directory entry refers to a not yet allocated
1254 * grain table or both grain directory copies refer to the same
1255 * grain table. Not allowed. */
1256 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1257 N_("VMDK: inconsistent references to grain directory in '%s'"), pExtent->pszFullname);
1258 break;
1259 }
1260
1261 i++;
1262 pGDTmp++;
1263 pRGDTmp++;
1264
1265 /*
1266 * Read a few tables at once if adjacent to decrease the number
1267 * of I/O requests. Read at maximum 1MB at once.
1268 */
1269 while ( i < pExtent->cGDEntries
1270 && cbGTRead < cbGTBuffersMax)
1271 {
1272 /* If no grain table is allocated skip the entry. */
1273 if (*pGDTmp == 0 && *pRGDTmp == 0)
1274 {
1275 i++;
1276 continue;
1277 }
1278
1279 if (*pGDTmp == 0 || *pRGDTmp == 0 || *pGDTmp == *pRGDTmp)
1280 {
1281 /* Just one grain directory entry refers to a not yet allocated
1282 * grain table or both grain directory copies refer to the same
1283 * grain table. Not allowed. */
1284 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1285 N_("VMDK: inconsistent references to grain directory in '%s'"), pExtent->pszFullname);
1286 break;
1287 }
1288
1289 /* Check that the start offsets are adjacent.*/
1290 if ( VMDK_SECTOR2BYTE(uGTStart) + cbGTRead != VMDK_SECTOR2BYTE(*pGDTmp)
1291 || VMDK_SECTOR2BYTE(uRGTStart) + cbGTRead != VMDK_SECTOR2BYTE(*pRGDTmp))
1292 break;
1293
1294 i++;
1295 pGDTmp++;
1296 pRGDTmp++;
1297 cbGTRead += cbGT;
1298 }
1299
1300 /* Increase buffers if required. */
1301 if ( RT_SUCCESS(rc)
1302 && cbGTBuffers < cbGTRead)
1303 {
1304 uint32_t *pTmp;
1305 pTmp = (uint32_t *)RTMemRealloc(pTmpGT1, cbGTRead);
1306 if (pTmp)
1307 {
1308 pTmpGT1 = pTmp;
1309 pTmp = (uint32_t *)RTMemRealloc(pTmpGT2, cbGTRead);
1310 if (pTmp)
1311 pTmpGT2 = pTmp;
1312 else
1313 rc = VERR_NO_MEMORY;
1314 }
1315 else
1316 rc = VERR_NO_MEMORY;
1317
1318 if (rc == VERR_NO_MEMORY)
1319 {
1320 /* Reset to the old values. */
1321 rc = VINF_SUCCESS;
1322 i -= cbGTRead / cbGT;
1323 cbGTRead = cbGT;
1324
1325 /* Don't try to increase the buffer again in the next run. */
1326 cbGTBuffersMax = cbGTBuffers;
1327 }
1328 }
1329
1330 if (RT_SUCCESS(rc))
1331 {
1332 /* The VMDK 1.1 spec seems to talk about compressed grain tables,
1333 * but in reality they are not compressed. */
1334 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1335 VMDK_SECTOR2BYTE(uGTStart),
1336 pTmpGT1, cbGTRead);
1337 if (RT_FAILURE(rc))
1338 {
1339 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1340 N_("VMDK: error reading grain table in '%s'"), pExtent->pszFullname);
1341 break;
1342 }
1343 /* The VMDK 1.1 spec seems to talk about compressed grain tables,
1344 * but in reality they are not compressed. */
1345 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1346 VMDK_SECTOR2BYTE(uRGTStart),
1347 pTmpGT2, cbGTRead);
1348 if (RT_FAILURE(rc))
1349 {
1350 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1351 N_("VMDK: error reading backup grain table in '%s'"), pExtent->pszFullname);
1352 break;
1353 }
1354 if (memcmp(pTmpGT1, pTmpGT2, cbGTRead))
1355 {
1356 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1357 N_("VMDK: inconsistency between grain table and backup grain table in '%s'"), pExtent->pszFullname);
1358 break;
1359 }
1360 }
1361 } /* while (i < pExtent->cGDEntries) */
1362
1363 /** @todo figure out what to do for unclean VMDKs. */
1364 if (pTmpGT1)
1365 RTMemFree(pTmpGT1);
1366 if (pTmpGT2)
1367 RTMemFree(pTmpGT2);
1368 }
1369 else
1370 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1371 N_("VMDK: could not read redundant grain directory in '%s'"), pExtent->pszFullname);
1372 }
1373 }
1374 else
1375 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1376 N_("VMDK: could not read grain directory in '%s': %Rrc"), pExtent->pszFullname, rc);
1377 }
1378
1379 if (RT_FAILURE(rc))
1380 vmdkFreeGrainDirectory(pExtent);
1381 return rc;
1382}
1383
1384/**
1385 * Creates a new grain directory for the given extent at the given start sector.
1386 *
1387 * @returns VBox status code.
1388 * @param pImage Image instance data.
1389 * @param pExtent The VMDK extent.
1390 * @param uStartSector Where the grain directory should be stored in the image.
1391 * @param fPreAlloc Flag whether to pre allocate the grain tables at this point.
1392 */
1393static int vmdkCreateGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
1394 uint64_t uStartSector, bool fPreAlloc)
1395{
1396 int rc = VINF_SUCCESS;
1397 unsigned i;
1398 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1399 size_t cbGDRounded = RT_ALIGN_64(cbGD, 512);
1400 size_t cbGTRounded;
1401 uint64_t cbOverhead;
1402
1403 if (fPreAlloc)
1404 {
1405 cbGTRounded = RT_ALIGN_64(pExtent->cGDEntries * pExtent->cGTEntries * sizeof(uint32_t), 512);
1406 cbOverhead = VMDK_SECTOR2BYTE(uStartSector) + cbGDRounded + cbGTRounded;
1407 }
1408 else
1409 {
1410 /* Use a dummy start sector for layout computation. */
1411 if (uStartSector == VMDK_GD_AT_END)
1412 uStartSector = 1;
1413 cbGTRounded = 0;
1414 cbOverhead = VMDK_SECTOR2BYTE(uStartSector) + cbGDRounded;
1415 }
1416
1417 /* For streamOptimized extents there is only one grain directory,
1418 * and for all others take redundant grain directory into account. */
1419 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
1420 {
1421 cbOverhead = RT_ALIGN_64(cbOverhead,
1422 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1423 }
1424 else
1425 {
1426 cbOverhead += cbGDRounded + cbGTRounded;
1427 cbOverhead = RT_ALIGN_64(cbOverhead,
1428 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1429 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pExtent->pFile->pStorage, cbOverhead);
1430 }
1431
1432 if (RT_SUCCESS(rc))
1433 {
1434 pExtent->uAppendPosition = cbOverhead;
1435 pExtent->cOverheadSectors = VMDK_BYTE2SECTOR(cbOverhead);
1436
1437 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
1438 {
1439 pExtent->uSectorRGD = 0;
1440 pExtent->uSectorGD = uStartSector;
1441 }
1442 else
1443 {
1444 pExtent->uSectorRGD = uStartSector;
1445 pExtent->uSectorGD = uStartSector + VMDK_BYTE2SECTOR(cbGDRounded + cbGTRounded);
1446 }
1447
1448 rc = vmdkAllocStreamBuffers(pImage, pExtent);
1449 if (RT_SUCCESS(rc))
1450 {
1451 rc = vmdkAllocGrainDirectory(pImage, pExtent);
1452 if ( RT_SUCCESS(rc)
1453 && fPreAlloc)
1454 {
1455 uint32_t uGTSectorLE;
1456 uint64_t uOffsetSectors;
1457
1458 if (pExtent->pRGD)
1459 {
1460 uOffsetSectors = pExtent->uSectorRGD + VMDK_BYTE2SECTOR(cbGDRounded);
1461 for (i = 0; i < pExtent->cGDEntries; i++)
1462 {
1463 pExtent->pRGD[i] = uOffsetSectors;
1464 uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
1465 /* Write the redundant grain directory entry to disk. */
1466 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
1467 VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + i * sizeof(uGTSectorLE),
1468 &uGTSectorLE, sizeof(uGTSectorLE));
1469 if (RT_FAILURE(rc))
1470 {
1471 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write new redundant grain directory entry in '%s'"), pExtent->pszFullname);
1472 break;
1473 }
1474 uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
1475 }
1476 }
1477
1478 if (RT_SUCCESS(rc))
1479 {
1480 uOffsetSectors = pExtent->uSectorGD + VMDK_BYTE2SECTOR(cbGDRounded);
1481 for (i = 0; i < pExtent->cGDEntries; i++)
1482 {
1483 pExtent->pGD[i] = uOffsetSectors;
1484 uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
1485 /* Write the grain directory entry to disk. */
1486 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
1487 VMDK_SECTOR2BYTE(pExtent->uSectorGD) + i * sizeof(uGTSectorLE),
1488 &uGTSectorLE, sizeof(uGTSectorLE));
1489 if (RT_FAILURE(rc))
1490 {
1491 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write new grain directory entry in '%s'"), pExtent->pszFullname);
1492 break;
1493 }
1494 uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
1495 }
1496 }
1497 }
1498 }
1499 }
1500
1501 if (RT_FAILURE(rc))
1502 vmdkFreeGrainDirectory(pExtent);
1503 return rc;
1504}
1505
1506/**
1507 * Unquotes the given string returning the result in a separate buffer.
1508 *
1509 * @returns VBox status code.
1510 * @param pImage The VMDK image state.
1511 * @param pszStr The string to unquote.
1512 * @param ppszUnquoted Where to store the return value, use RTMemTmpFree to
1513 * free.
1514 * @param ppszNext Where to store the pointer to any character following
1515 * the quoted value, optional.
1516 */
1517static int vmdkStringUnquote(PVMDKIMAGE pImage, const char *pszStr,
1518 char **ppszUnquoted, char **ppszNext)
1519{
1520 const char *pszStart = pszStr;
1521 char *pszQ;
1522 char *pszUnquoted;
1523
1524 /* Skip over whitespace. */
1525 while (*pszStr == ' ' || *pszStr == '\t')
1526 pszStr++;
1527
1528 if (*pszStr != '"')
1529 {
1530 pszQ = (char *)pszStr;
1531 while (*pszQ && *pszQ != ' ' && *pszQ != '\t')
1532 pszQ++;
1533 }
1534 else
1535 {
1536 pszStr++;
1537 pszQ = (char *)strchr(pszStr, '"');
1538 if (pszQ == NULL)
1539 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrectly quoted value in descriptor in '%s' (raw value %s)"),
1540 pImage->pszFilename, pszStart);
1541 }
1542
1543 pszUnquoted = (char *)RTMemTmpAlloc(pszQ - pszStr + 1);
1544 if (!pszUnquoted)
1545 return VERR_NO_MEMORY;
1546 memcpy(pszUnquoted, pszStr, pszQ - pszStr);
1547 pszUnquoted[pszQ - pszStr] = '\0';
1548 *ppszUnquoted = pszUnquoted;
1549 if (ppszNext)
1550 *ppszNext = pszQ + 1;
1551 return VINF_SUCCESS;
1552}
1553
1554static int vmdkDescInitStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1555 const char *pszLine)
1556{
1557 char *pEnd = pDescriptor->aLines[pDescriptor->cLines];
1558 ssize_t cbDiff = strlen(pszLine) + 1;
1559
1560 if ( pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1
1561 && pEnd - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
1562 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1563
1564 memcpy(pEnd, pszLine, cbDiff);
1565 pDescriptor->cLines++;
1566 pDescriptor->aLines[pDescriptor->cLines] = pEnd + cbDiff;
1567 pDescriptor->fDirty = true;
1568
1569 return VINF_SUCCESS;
1570}
1571
1572static bool vmdkDescGetStr(PVMDKDESCRIPTOR pDescriptor, unsigned uStart,
1573 const char *pszKey, const char **ppszValue)
1574{
1575 size_t cbKey = strlen(pszKey);
1576 const char *pszValue;
1577
1578 while (uStart != 0)
1579 {
1580 if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
1581 {
1582 /* Key matches, check for a '=' (preceded by whitespace). */
1583 pszValue = pDescriptor->aLines[uStart] + cbKey;
1584 while (*pszValue == ' ' || *pszValue == '\t')
1585 pszValue++;
1586 if (*pszValue == '=')
1587 {
1588 *ppszValue = pszValue + 1;
1589 break;
1590 }
1591 }
1592 uStart = pDescriptor->aNextLines[uStart];
1593 }
1594 return !!uStart;
1595}
1596
1597static int vmdkDescSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1598 unsigned uStart,
1599 const char *pszKey, const char *pszValue)
1600{
1601 char *pszTmp = NULL; /* (MSC naturally cannot figure this isn't used uninitialized) */
1602 size_t cbKey = strlen(pszKey);
1603 unsigned uLast = 0;
1604
1605 while (uStart != 0)
1606 {
1607 if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
1608 {
1609 /* Key matches, check for a '=' (preceded by whitespace). */
1610 pszTmp = pDescriptor->aLines[uStart] + cbKey;
1611 while (*pszTmp == ' ' || *pszTmp == '\t')
1612 pszTmp++;
1613 if (*pszTmp == '=')
1614 {
1615 pszTmp++;
1616 /** @todo r=bird: Doesn't skipping trailing blanks here just cause unecessary
1617 * bloat and potentially out of space error? */
1618 while (*pszTmp == ' ' || *pszTmp == '\t')
1619 pszTmp++;
1620 break;
1621 }
1622 }
1623 if (!pDescriptor->aNextLines[uStart])
1624 uLast = uStart;
1625 uStart = pDescriptor->aNextLines[uStart];
1626 }
1627 if (uStart)
1628 {
1629 if (pszValue)
1630 {
1631 /* Key already exists, replace existing value. */
1632 size_t cbOldVal = strlen(pszTmp);
1633 size_t cbNewVal = strlen(pszValue);
1634 ssize_t cbDiff = cbNewVal - cbOldVal;
1635 /* Check for buffer overflow. */
1636 if ( pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[0]
1637 > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
1638 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1639
1640 memmove(pszTmp + cbNewVal, pszTmp + cbOldVal,
1641 pDescriptor->aLines[pDescriptor->cLines] - pszTmp - cbOldVal);
1642 memcpy(pszTmp, pszValue, cbNewVal + 1);
1643 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1644 pDescriptor->aLines[i] += cbDiff;
1645 }
1646 else
1647 {
1648 memmove(pDescriptor->aLines[uStart], pDescriptor->aLines[uStart+1],
1649 pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uStart+1] + 1);
1650 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1651 {
1652 pDescriptor->aLines[i-1] = pDescriptor->aLines[i];
1653 if (pDescriptor->aNextLines[i])
1654 pDescriptor->aNextLines[i-1] = pDescriptor->aNextLines[i] - 1;
1655 else
1656 pDescriptor->aNextLines[i-1] = 0;
1657 }
1658 pDescriptor->cLines--;
1659 /* Adjust starting line numbers of following descriptor sections. */
1660 if (uStart < pDescriptor->uFirstExtent)
1661 pDescriptor->uFirstExtent--;
1662 if (uStart < pDescriptor->uFirstDDB)
1663 pDescriptor->uFirstDDB--;
1664 }
1665 }
1666 else
1667 {
1668 /* Key doesn't exist, append after the last entry in this category. */
1669 if (!pszValue)
1670 {
1671 /* Key doesn't exist, and it should be removed. Simply a no-op. */
1672 return VINF_SUCCESS;
1673 }
1674 cbKey = strlen(pszKey);
1675 size_t cbValue = strlen(pszValue);
1676 ssize_t cbDiff = cbKey + 1 + cbValue + 1;
1677 /* Check for buffer overflow. */
1678 if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
1679 || ( pDescriptor->aLines[pDescriptor->cLines]
1680 - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
1681 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1682 for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
1683 {
1684 pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
1685 if (pDescriptor->aNextLines[i - 1])
1686 pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
1687 else
1688 pDescriptor->aNextLines[i] = 0;
1689 }
1690 uStart = uLast + 1;
1691 pDescriptor->aNextLines[uLast] = uStart;
1692 pDescriptor->aNextLines[uStart] = 0;
1693 pDescriptor->cLines++;
1694 pszTmp = pDescriptor->aLines[uStart];
1695 memmove(pszTmp + cbDiff, pszTmp,
1696 pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
1697 memcpy(pDescriptor->aLines[uStart], pszKey, cbKey);
1698 pDescriptor->aLines[uStart][cbKey] = '=';
1699 memcpy(pDescriptor->aLines[uStart] + cbKey + 1, pszValue, cbValue + 1);
1700 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1701 pDescriptor->aLines[i] += cbDiff;
1702
1703 /* Adjust starting line numbers of following descriptor sections. */
1704 if (uStart <= pDescriptor->uFirstExtent)
1705 pDescriptor->uFirstExtent++;
1706 if (uStart <= pDescriptor->uFirstDDB)
1707 pDescriptor->uFirstDDB++;
1708 }
1709 pDescriptor->fDirty = true;
1710 return VINF_SUCCESS;
1711}
1712
1713static int vmdkDescBaseGetU32(PVMDKDESCRIPTOR pDescriptor, const char *pszKey,
1714 uint32_t *puValue)
1715{
1716 const char *pszValue;
1717
1718 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey,
1719 &pszValue))
1720 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1721 return RTStrToUInt32Ex(pszValue, NULL, 10, puValue);
1722}
1723
1724/**
1725 * Returns the value of the given key as a string allocating the necessary memory.
1726 *
1727 * @returns VBox status code.
1728 * @retval VERR_VD_VMDK_VALUE_NOT_FOUND if the value could not be found.
1729 * @param pImage The VMDK image state.
1730 * @param pDescriptor The descriptor to fetch the value from.
1731 * @param pszKey The key to get the value from.
1732 * @param ppszValue Where to store the return value, use RTMemTmpFree to
1733 * free.
1734 */
1735static int vmdkDescBaseGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1736 const char *pszKey, char **ppszValue)
1737{
1738 const char *pszValue;
1739 char *pszValueUnquoted;
1740
1741 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey,
1742 &pszValue))
1743 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1744 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1745 if (RT_FAILURE(rc))
1746 return rc;
1747 *ppszValue = pszValueUnquoted;
1748 return rc;
1749}
1750
1751static int vmdkDescBaseSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1752 const char *pszKey, const char *pszValue)
1753{
1754 char *pszValueQuoted;
1755
1756 RTStrAPrintf(&pszValueQuoted, "\"%s\"", pszValue);
1757 if (!pszValueQuoted)
1758 return VERR_NO_STR_MEMORY;
1759 int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc, pszKey,
1760 pszValueQuoted);
1761 RTStrFree(pszValueQuoted);
1762 return rc;
1763}
1764
1765static void vmdkDescExtRemoveDummy(PVMDKIMAGE pImage,
1766 PVMDKDESCRIPTOR pDescriptor)
1767{
1768 RT_NOREF1(pImage);
1769 unsigned uEntry = pDescriptor->uFirstExtent;
1770 ssize_t cbDiff;
1771
1772 if (!uEntry)
1773 return;
1774
1775 cbDiff = strlen(pDescriptor->aLines[uEntry]) + 1;
1776 /* Move everything including \0 in the entry marking the end of buffer. */
1777 memmove(pDescriptor->aLines[uEntry], pDescriptor->aLines[uEntry + 1],
1778 pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uEntry + 1] + 1);
1779 for (unsigned i = uEntry + 1; i <= pDescriptor->cLines; i++)
1780 {
1781 pDescriptor->aLines[i - 1] = pDescriptor->aLines[i] - cbDiff;
1782 if (pDescriptor->aNextLines[i])
1783 pDescriptor->aNextLines[i - 1] = pDescriptor->aNextLines[i] - 1;
1784 else
1785 pDescriptor->aNextLines[i - 1] = 0;
1786 }
1787 pDescriptor->cLines--;
1788 if (pDescriptor->uFirstDDB)
1789 pDescriptor->uFirstDDB--;
1790
1791 return;
1792}
1793
1794static void vmdkDescExtRemoveByLine(PVMDKIMAGE pImage,
1795 PVMDKDESCRIPTOR pDescriptor, unsigned uLine)
1796{
1797 RT_NOREF1(pImage);
1798 unsigned uEntry = uLine;
1799 ssize_t cbDiff;
1800 if (!uEntry)
1801 return;
1802 cbDiff = strlen(pDescriptor->aLines[uEntry]) + 1;
1803 /* Move everything including \0 in the entry marking the end of buffer. */
1804 memmove(pDescriptor->aLines[uEntry], pDescriptor->aLines[uEntry + 1],
1805 pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uEntry + 1] + 1);
1806 for (unsigned i = uEntry; i <= pDescriptor->cLines; i++)
1807 {
1808 if (i != uEntry)
1809 pDescriptor->aLines[i - 1] = pDescriptor->aLines[i] - cbDiff;
1810 if (pDescriptor->aNextLines[i])
1811 pDescriptor->aNextLines[i - 1] = pDescriptor->aNextLines[i] - 1;
1812 else
1813 pDescriptor->aNextLines[i - 1] = 0;
1814 }
1815 pDescriptor->cLines--;
1816 if (pDescriptor->uFirstDDB)
1817 pDescriptor->uFirstDDB--;
1818 return;
1819}
1820
1821static int vmdkDescExtInsert(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1822 VMDKACCESS enmAccess, uint64_t cNominalSectors,
1823 VMDKETYPE enmType, const char *pszBasename,
1824 uint64_t uSectorOffset)
1825{
1826 static const char *apszAccess[] = { "NOACCESS", "RDONLY", "RW" };
1827 static const char *apszType[] = { "", "SPARSE", "FLAT", "ZERO", "VMFS" };
1828 char *pszTmp;
1829 unsigned uStart = pDescriptor->uFirstExtent, uLast = 0;
1830 char szExt[1024];
1831 ssize_t cbDiff;
1832
1833 Assert((unsigned)enmAccess < RT_ELEMENTS(apszAccess));
1834 Assert((unsigned)enmType < RT_ELEMENTS(apszType));
1835
1836 /* Find last entry in extent description. */
1837 while (uStart)
1838 {
1839 if (!pDescriptor->aNextLines[uStart])
1840 uLast = uStart;
1841 uStart = pDescriptor->aNextLines[uStart];
1842 }
1843
1844 if (enmType == VMDKETYPE_ZERO)
1845 {
1846 RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s ", apszAccess[enmAccess],
1847 cNominalSectors, apszType[enmType]);
1848 }
1849 else if (enmType == VMDKETYPE_FLAT)
1850 {
1851 RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\" %llu",
1852 apszAccess[enmAccess], cNominalSectors,
1853 apszType[enmType], pszBasename, uSectorOffset);
1854 }
1855 else
1856 {
1857 RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\"",
1858 apszAccess[enmAccess], cNominalSectors,
1859 apszType[enmType], pszBasename);
1860 }
1861 cbDiff = strlen(szExt) + 1;
1862
1863 /* Check for buffer overflow. */
1864 if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
1865 || ( pDescriptor->aLines[pDescriptor->cLines]
1866 - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
1867 {
1868 if ((pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
1869 && !(pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1))
1870 {
1871 pImage->cbDescAlloc *= 2;
1872 pDescriptor->cbDescAlloc *= 2;
1873 }
1874 else
1875 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1876 }
1877
1878 for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
1879 {
1880 pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
1881 if (pDescriptor->aNextLines[i - 1])
1882 pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
1883 else
1884 pDescriptor->aNextLines[i] = 0;
1885 }
1886 uStart = uLast + 1;
1887 pDescriptor->aNextLines[uLast] = uStart;
1888 pDescriptor->aNextLines[uStart] = 0;
1889 pDescriptor->cLines++;
1890 pszTmp = pDescriptor->aLines[uStart];
1891 memmove(pszTmp + cbDiff, pszTmp,
1892 pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
1893 memcpy(pDescriptor->aLines[uStart], szExt, cbDiff);
1894 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1895 pDescriptor->aLines[i] += cbDiff;
1896
1897 /* Adjust starting line numbers of following descriptor sections. */
1898 if (uStart <= pDescriptor->uFirstDDB)
1899 pDescriptor->uFirstDDB++;
1900
1901 pDescriptor->fDirty = true;
1902 return VINF_SUCCESS;
1903}
1904
1905/**
1906 * Returns the value of the given key from the DDB as a string allocating
1907 * the necessary memory.
1908 *
1909 * @returns VBox status code.
1910 * @retval VERR_VD_VMDK_VALUE_NOT_FOUND if the value could not be found.
1911 * @param pImage The VMDK image state.
1912 * @param pDescriptor The descriptor to fetch the value from.
1913 * @param pszKey The key to get the value from.
1914 * @param ppszValue Where to store the return value, use RTMemTmpFree to
1915 * free.
1916 */
1917static int vmdkDescDDBGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1918 const char *pszKey, char **ppszValue)
1919{
1920 const char *pszValue;
1921 char *pszValueUnquoted;
1922
1923 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
1924 &pszValue))
1925 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1926 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1927 if (RT_FAILURE(rc))
1928 return rc;
1929 *ppszValue = pszValueUnquoted;
1930 return rc;
1931}
1932
1933static int vmdkDescDDBGetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1934 const char *pszKey, uint32_t *puValue)
1935{
1936 const char *pszValue;
1937 char *pszValueUnquoted;
1938
1939 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
1940 &pszValue))
1941 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1942 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1943 if (RT_FAILURE(rc))
1944 return rc;
1945 rc = RTStrToUInt32Ex(pszValueUnquoted, NULL, 10, puValue);
1946 RTMemTmpFree(pszValueUnquoted);
1947 return rc;
1948}
1949
1950static int vmdkDescDDBGetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1951 const char *pszKey, PRTUUID pUuid)
1952{
1953 const char *pszValue;
1954 char *pszValueUnquoted;
1955
1956 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
1957 &pszValue))
1958 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1959 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1960 if (RT_FAILURE(rc))
1961 return rc;
1962 rc = RTUuidFromStr(pUuid, pszValueUnquoted);
1963 RTMemTmpFree(pszValueUnquoted);
1964 return rc;
1965}
1966
1967static int vmdkDescDDBSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1968 const char *pszKey, const char *pszVal)
1969{
1970 int rc;
1971 char *pszValQuoted;
1972
1973 if (pszVal)
1974 {
1975 RTStrAPrintf(&pszValQuoted, "\"%s\"", pszVal);
1976 if (!pszValQuoted)
1977 return VERR_NO_STR_MEMORY;
1978 }
1979 else
1980 pszValQuoted = NULL;
1981 rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
1982 pszValQuoted);
1983 if (pszValQuoted)
1984 RTStrFree(pszValQuoted);
1985 return rc;
1986}
1987
1988static int vmdkDescDDBSetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1989 const char *pszKey, PCRTUUID pUuid)
1990{
1991 char *pszUuid;
1992
1993 RTStrAPrintf(&pszUuid, "\"%RTuuid\"", pUuid);
1994 if (!pszUuid)
1995 return VERR_NO_STR_MEMORY;
1996 int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
1997 pszUuid);
1998 RTStrFree(pszUuid);
1999 return rc;
2000}
2001
2002static int vmdkDescDDBSetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
2003 const char *pszKey, uint32_t uValue)
2004{
2005 char *pszValue;
2006
2007 RTStrAPrintf(&pszValue, "\"%d\"", uValue);
2008 if (!pszValue)
2009 return VERR_NO_STR_MEMORY;
2010 int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
2011 pszValue);
2012 RTStrFree(pszValue);
2013 return rc;
2014}
2015
2016/**
2017 * Splits the descriptor data into individual lines checking for correct line
2018 * endings and descriptor size.
2019 *
2020 * @returns VBox status code.
2021 * @param pImage The image instance.
2022 * @param pDesc The descriptor.
2023 * @param pszTmp The raw descriptor data from the image.
2024 */
2025static int vmdkDescSplitLines(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDesc, char *pszTmp)
2026{
2027 unsigned cLine = 0;
2028 int rc = VINF_SUCCESS;
2029
2030 while ( RT_SUCCESS(rc)
2031 && *pszTmp != '\0')
2032 {
2033 pDesc->aLines[cLine++] = pszTmp;
2034 if (cLine >= VMDK_DESCRIPTOR_LINES_MAX)
2035 {
2036 vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
2037 rc = VERR_VD_VMDK_INVALID_HEADER;
2038 break;
2039 }
2040
2041 while (*pszTmp != '\0' && *pszTmp != '\n')
2042 {
2043 if (*pszTmp == '\r')
2044 {
2045 if (*(pszTmp + 1) != '\n')
2046 {
2047 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: unsupported end of line in descriptor in '%s'"), pImage->pszFilename);
2048 break;
2049 }
2050 else
2051 {
2052 /* Get rid of CR character. */
2053 *pszTmp = '\0';
2054 }
2055 }
2056 pszTmp++;
2057 }
2058
2059 if (RT_FAILURE(rc))
2060 break;
2061
2062 /* Get rid of LF character. */
2063 if (*pszTmp == '\n')
2064 {
2065 *pszTmp = '\0';
2066 pszTmp++;
2067 }
2068 }
2069
2070 if (RT_SUCCESS(rc))
2071 {
2072 pDesc->cLines = cLine;
2073 /* Pointer right after the end of the used part of the buffer. */
2074 pDesc->aLines[cLine] = pszTmp;
2075 }
2076
2077 return rc;
2078}
2079
2080static int vmdkPreprocessDescriptor(PVMDKIMAGE pImage, char *pDescData,
2081 size_t cbDescData, PVMDKDESCRIPTOR pDescriptor)
2082{
2083 pDescriptor->cbDescAlloc = cbDescData;
2084 int rc = vmdkDescSplitLines(pImage, pDescriptor, pDescData);
2085 if (RT_SUCCESS(rc))
2086 {
2087 if ( strcmp(pDescriptor->aLines[0], "# Disk DescriptorFile")
2088 && strcmp(pDescriptor->aLines[0], "# Disk Descriptor File")
2089 && strcmp(pDescriptor->aLines[0], "#Disk Descriptor File")
2090 && strcmp(pDescriptor->aLines[0], "#Disk DescriptorFile"))
2091 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2092 N_("VMDK: descriptor does not start as expected in '%s'"), pImage->pszFilename);
2093 else
2094 {
2095 unsigned uLastNonEmptyLine = 0;
2096
2097 /* Initialize those, because we need to be able to reopen an image. */
2098 pDescriptor->uFirstDesc = 0;
2099 pDescriptor->uFirstExtent = 0;
2100 pDescriptor->uFirstDDB = 0;
2101 for (unsigned i = 0; i < pDescriptor->cLines; i++)
2102 {
2103 if (*pDescriptor->aLines[i] != '#' && *pDescriptor->aLines[i] != '\0')
2104 {
2105 if ( !strncmp(pDescriptor->aLines[i], "RW", 2)
2106 || !strncmp(pDescriptor->aLines[i], "RDONLY", 6)
2107 || !strncmp(pDescriptor->aLines[i], "NOACCESS", 8) )
2108 {
2109 /* An extent descriptor. */
2110 if (!pDescriptor->uFirstDesc || pDescriptor->uFirstDDB)
2111 {
2112 /* Incorrect ordering of entries. */
2113 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2114 N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
2115 break;
2116 }
2117 if (!pDescriptor->uFirstExtent)
2118 {
2119 pDescriptor->uFirstExtent = i;
2120 uLastNonEmptyLine = 0;
2121 }
2122 }
2123 else if (!strncmp(pDescriptor->aLines[i], "ddb.", 4))
2124 {
2125 /* A disk database entry. */
2126 if (!pDescriptor->uFirstDesc || !pDescriptor->uFirstExtent)
2127 {
2128 /* Incorrect ordering of entries. */
2129 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2130 N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
2131 break;
2132 }
2133 if (!pDescriptor->uFirstDDB)
2134 {
2135 pDescriptor->uFirstDDB = i;
2136 uLastNonEmptyLine = 0;
2137 }
2138 }
2139 else
2140 {
2141 /* A normal entry. */
2142 if (pDescriptor->uFirstExtent || pDescriptor->uFirstDDB)
2143 {
2144 /* Incorrect ordering of entries. */
2145 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2146 N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
2147 break;
2148 }
2149 if (!pDescriptor->uFirstDesc)
2150 {
2151 pDescriptor->uFirstDesc = i;
2152 uLastNonEmptyLine = 0;
2153 }
2154 }
2155 if (uLastNonEmptyLine)
2156 pDescriptor->aNextLines[uLastNonEmptyLine] = i;
2157 uLastNonEmptyLine = i;
2158 }
2159 }
2160 }
2161 }
2162
2163 return rc;
2164}
2165
2166static int vmdkDescSetPCHSGeometry(PVMDKIMAGE pImage,
2167 PCVDGEOMETRY pPCHSGeometry)
2168{
2169 int rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2170 VMDK_DDB_GEO_PCHS_CYLINDERS,
2171 pPCHSGeometry->cCylinders);
2172 if (RT_FAILURE(rc))
2173 return rc;
2174 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2175 VMDK_DDB_GEO_PCHS_HEADS,
2176 pPCHSGeometry->cHeads);
2177 if (RT_FAILURE(rc))
2178 return rc;
2179 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2180 VMDK_DDB_GEO_PCHS_SECTORS,
2181 pPCHSGeometry->cSectors);
2182 return rc;
2183}
2184
2185static int vmdkDescSetLCHSGeometry(PVMDKIMAGE pImage,
2186 PCVDGEOMETRY pLCHSGeometry)
2187{
2188 int rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2189 VMDK_DDB_GEO_LCHS_CYLINDERS,
2190 pLCHSGeometry->cCylinders);
2191 if (RT_FAILURE(rc))
2192 return rc;
2193 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2194 VMDK_DDB_GEO_LCHS_HEADS,
2195
2196 pLCHSGeometry->cHeads);
2197 if (RT_FAILURE(rc))
2198 return rc;
2199 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2200 VMDK_DDB_GEO_LCHS_SECTORS,
2201 pLCHSGeometry->cSectors);
2202 return rc;
2203}
2204
2205static int vmdkCreateDescriptor(PVMDKIMAGE pImage, char *pDescData,
2206 size_t cbDescData, PVMDKDESCRIPTOR pDescriptor)
2207{
2208 pDescriptor->uFirstDesc = 0;
2209 pDescriptor->uFirstExtent = 0;
2210 pDescriptor->uFirstDDB = 0;
2211 pDescriptor->cLines = 0;
2212 pDescriptor->cbDescAlloc = cbDescData;
2213 pDescriptor->fDirty = false;
2214 pDescriptor->aLines[pDescriptor->cLines] = pDescData;
2215 memset(pDescriptor->aNextLines, '\0', sizeof(pDescriptor->aNextLines));
2216
2217 int rc = vmdkDescInitStr(pImage, pDescriptor, "# Disk DescriptorFile");
2218 if (RT_SUCCESS(rc))
2219 rc = vmdkDescInitStr(pImage, pDescriptor, "version=1");
2220 if (RT_SUCCESS(rc))
2221 {
2222 pDescriptor->uFirstDesc = pDescriptor->cLines - 1;
2223 rc = vmdkDescInitStr(pImage, pDescriptor, "");
2224 }
2225 if (RT_SUCCESS(rc))
2226 rc = vmdkDescInitStr(pImage, pDescriptor, "# Extent description");
2227 if (RT_SUCCESS(rc))
2228 rc = vmdkDescInitStr(pImage, pDescriptor, "NOACCESS 0 ZERO ");
2229 if (RT_SUCCESS(rc))
2230 {
2231 pDescriptor->uFirstExtent = pDescriptor->cLines - 1;
2232 rc = vmdkDescInitStr(pImage, pDescriptor, "");
2233 }
2234 if (RT_SUCCESS(rc))
2235 {
2236 /* The trailing space is created by VMware, too. */
2237 rc = vmdkDescInitStr(pImage, pDescriptor, "# The disk Data Base ");
2238 }
2239 if (RT_SUCCESS(rc))
2240 rc = vmdkDescInitStr(pImage, pDescriptor, "#DDB");
2241 if (RT_SUCCESS(rc))
2242 rc = vmdkDescInitStr(pImage, pDescriptor, "");
2243 if (RT_SUCCESS(rc))
2244 rc = vmdkDescInitStr(pImage, pDescriptor, "ddb.virtualHWVersion = \"4\"");
2245 if (RT_SUCCESS(rc))
2246 {
2247 pDescriptor->uFirstDDB = pDescriptor->cLines - 1;
2248
2249 /* Now that the framework is in place, use the normal functions to insert
2250 * the remaining keys. */
2251 char szBuf[9];
2252 RTStrPrintf(szBuf, sizeof(szBuf), "%08x", RTRandU32());
2253 rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc,
2254 "CID", szBuf);
2255 }
2256 if (RT_SUCCESS(rc))
2257 rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc,
2258 "parentCID", "ffffffff");
2259 if (RT_SUCCESS(rc))
2260 rc = vmdkDescDDBSetStr(pImage, pDescriptor, "ddb.adapterType", "ide");
2261
2262 return rc;
2263}
2264
2265static int vmdkParseDescriptor(PVMDKIMAGE pImage, char *pDescData, size_t cbDescData)
2266{
2267 int rc;
2268 unsigned cExtents;
2269 unsigned uLine;
2270 unsigned i;
2271
2272 rc = vmdkPreprocessDescriptor(pImage, pDescData, cbDescData,
2273 &pImage->Descriptor);
2274 if (RT_FAILURE(rc))
2275 return rc;
2276
2277 /* Check version, must be 1. */
2278 uint32_t uVersion;
2279 rc = vmdkDescBaseGetU32(&pImage->Descriptor, "version", &uVersion);
2280 if (RT_FAILURE(rc))
2281 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error finding key 'version' in descriptor in '%s'"), pImage->pszFilename);
2282 if (uVersion != 1)
2283 return vdIfError(pImage->pIfError, VERR_VD_VMDK_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VMDK: unsupported format version in descriptor in '%s'"), pImage->pszFilename);
2284
2285 /* Get image creation type and determine image flags. */
2286 char *pszCreateType = NULL; /* initialized to make gcc shut up */
2287 rc = vmdkDescBaseGetStr(pImage, &pImage->Descriptor, "createType",
2288 &pszCreateType);
2289 if (RT_FAILURE(rc))
2290 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot get image type from descriptor in '%s'"), pImage->pszFilename);
2291 if ( !strcmp(pszCreateType, "twoGbMaxExtentSparse")
2292 || !strcmp(pszCreateType, "twoGbMaxExtentFlat"))
2293 pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_SPLIT_2G;
2294 else if ( !strcmp(pszCreateType, "partitionedDevice")
2295 || !strcmp(pszCreateType, "fullDevice"))
2296 pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_RAWDISK;
2297 else if (!strcmp(pszCreateType, "streamOptimized"))
2298 pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED;
2299 else if (!strcmp(pszCreateType, "vmfs"))
2300 pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED | VD_VMDK_IMAGE_FLAGS_ESX;
2301 RTMemTmpFree(pszCreateType);
2302
2303 /* Count the number of extent config entries. */
2304 for (uLine = pImage->Descriptor.uFirstExtent, cExtents = 0;
2305 uLine != 0;
2306 uLine = pImage->Descriptor.aNextLines[uLine], cExtents++)
2307 /* nothing */;
2308
2309 if (!pImage->pDescData && cExtents != 1)
2310 {
2311 /* Monolithic image, must have only one extent (already opened). */
2312 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);
2313 }
2314
2315 if (pImage->pDescData)
2316 {
2317 /* Non-monolithic image, extents need to be allocated. */
2318 rc = vmdkCreateExtents(pImage, cExtents);
2319 if (RT_FAILURE(rc))
2320 return rc;
2321 }
2322
2323 for (i = 0, uLine = pImage->Descriptor.uFirstExtent;
2324 i < cExtents; i++, uLine = pImage->Descriptor.aNextLines[uLine])
2325 {
2326 char *pszLine = pImage->Descriptor.aLines[uLine];
2327
2328 /* Access type of the extent. */
2329 if (!strncmp(pszLine, "RW", 2))
2330 {
2331 pImage->pExtents[i].enmAccess = VMDKACCESS_READWRITE;
2332 pszLine += 2;
2333 }
2334 else if (!strncmp(pszLine, "RDONLY", 6))
2335 {
2336 pImage->pExtents[i].enmAccess = VMDKACCESS_READONLY;
2337 pszLine += 6;
2338 }
2339 else if (!strncmp(pszLine, "NOACCESS", 8))
2340 {
2341 pImage->pExtents[i].enmAccess = VMDKACCESS_NOACCESS;
2342 pszLine += 8;
2343 }
2344 else
2345 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2346 if (*pszLine++ != ' ')
2347 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2348
2349 /* Nominal size of the extent. */
2350 rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
2351 &pImage->pExtents[i].cNominalSectors);
2352 if (RT_FAILURE(rc))
2353 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2354 if (*pszLine++ != ' ')
2355 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2356
2357 /* Type of the extent. */
2358 if (!strncmp(pszLine, "SPARSE", 6))
2359 {
2360 pImage->pExtents[i].enmType = VMDKETYPE_HOSTED_SPARSE;
2361 pszLine += 6;
2362 }
2363 else if (!strncmp(pszLine, "FLAT", 4))
2364 {
2365 pImage->pExtents[i].enmType = VMDKETYPE_FLAT;
2366 pszLine += 4;
2367 }
2368 else if (!strncmp(pszLine, "ZERO", 4))
2369 {
2370 pImage->pExtents[i].enmType = VMDKETYPE_ZERO;
2371 pszLine += 4;
2372 }
2373 else if (!strncmp(pszLine, "VMFS", 4))
2374 {
2375 pImage->pExtents[i].enmType = VMDKETYPE_VMFS;
2376 pszLine += 4;
2377 }
2378 else
2379 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2380
2381 if (pImage->pExtents[i].enmType == VMDKETYPE_ZERO)
2382 {
2383 /* This one has no basename or offset. */
2384 if (*pszLine == ' ')
2385 pszLine++;
2386 if (*pszLine != '\0')
2387 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2388 pImage->pExtents[i].pszBasename = NULL;
2389 }
2390 else
2391 {
2392 /* All other extent types have basename and optional offset. */
2393 if (*pszLine++ != ' ')
2394 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2395
2396 /* Basename of the image. Surrounded by quotes. */
2397 char *pszBasename;
2398 rc = vmdkStringUnquote(pImage, pszLine, &pszBasename, &pszLine);
2399 if (RT_FAILURE(rc))
2400 return rc;
2401 pImage->pExtents[i].pszBasename = pszBasename;
2402 if (*pszLine == ' ')
2403 {
2404 pszLine++;
2405 if (*pszLine != '\0')
2406 {
2407 /* Optional offset in extent specified. */
2408 rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
2409 &pImage->pExtents[i].uSectorOffset);
2410 if (RT_FAILURE(rc))
2411 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2412 }
2413 }
2414
2415 if (*pszLine != '\0')
2416 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2417 }
2418 }
2419
2420 /* Determine PCHS geometry (autogenerate if necessary). */
2421 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2422 VMDK_DDB_GEO_PCHS_CYLINDERS,
2423 &pImage->PCHSGeometry.cCylinders);
2424 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2425 pImage->PCHSGeometry.cCylinders = 0;
2426 else if (RT_FAILURE(rc))
2427 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
2428 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2429 VMDK_DDB_GEO_PCHS_HEADS,
2430 &pImage->PCHSGeometry.cHeads);
2431 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2432 pImage->PCHSGeometry.cHeads = 0;
2433 else if (RT_FAILURE(rc))
2434 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
2435 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2436 VMDK_DDB_GEO_PCHS_SECTORS,
2437 &pImage->PCHSGeometry.cSectors);
2438 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2439 pImage->PCHSGeometry.cSectors = 0;
2440 else if (RT_FAILURE(rc))
2441 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
2442 if ( pImage->PCHSGeometry.cCylinders == 0
2443 || pImage->PCHSGeometry.cHeads == 0
2444 || pImage->PCHSGeometry.cHeads > 16
2445 || pImage->PCHSGeometry.cSectors == 0
2446 || pImage->PCHSGeometry.cSectors > 63)
2447 {
2448 /* Mark PCHS geometry as not yet valid (can't do the calculation here
2449 * as the total image size isn't known yet). */
2450 pImage->PCHSGeometry.cCylinders = 0;
2451 pImage->PCHSGeometry.cHeads = 16;
2452 pImage->PCHSGeometry.cSectors = 63;
2453 }
2454
2455 /* Determine LCHS geometry (set to 0 if not specified). */
2456 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2457 VMDK_DDB_GEO_LCHS_CYLINDERS,
2458 &pImage->LCHSGeometry.cCylinders);
2459 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2460 pImage->LCHSGeometry.cCylinders = 0;
2461 else if (RT_FAILURE(rc))
2462 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
2463 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2464 VMDK_DDB_GEO_LCHS_HEADS,
2465 &pImage->LCHSGeometry.cHeads);
2466 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2467 pImage->LCHSGeometry.cHeads = 0;
2468 else if (RT_FAILURE(rc))
2469 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
2470 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2471 VMDK_DDB_GEO_LCHS_SECTORS,
2472 &pImage->LCHSGeometry.cSectors);
2473 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2474 pImage->LCHSGeometry.cSectors = 0;
2475 else if (RT_FAILURE(rc))
2476 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
2477 if ( pImage->LCHSGeometry.cCylinders == 0
2478 || pImage->LCHSGeometry.cHeads == 0
2479 || pImage->LCHSGeometry.cSectors == 0)
2480 {
2481 pImage->LCHSGeometry.cCylinders = 0;
2482 pImage->LCHSGeometry.cHeads = 0;
2483 pImage->LCHSGeometry.cSectors = 0;
2484 }
2485
2486 /* Get image UUID. */
2487 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, VMDK_DDB_IMAGE_UUID,
2488 &pImage->ImageUuid);
2489 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2490 {
2491 /* Image without UUID. Probably created by VMware and not yet used
2492 * by VirtualBox. Can only be added for images opened in read/write
2493 * mode, so don't bother producing a sensible UUID otherwise. */
2494 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2495 RTUuidClear(&pImage->ImageUuid);
2496 else
2497 {
2498 rc = RTUuidCreate(&pImage->ImageUuid);
2499 if (RT_FAILURE(rc))
2500 return rc;
2501 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2502 VMDK_DDB_IMAGE_UUID, &pImage->ImageUuid);
2503 if (RT_FAILURE(rc))
2504 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
2505 }
2506 }
2507 else if (RT_FAILURE(rc))
2508 return rc;
2509
2510 /* Get image modification UUID. */
2511 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor,
2512 VMDK_DDB_MODIFICATION_UUID,
2513 &pImage->ModificationUuid);
2514 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2515 {
2516 /* Image without UUID. Probably created by VMware and not yet used
2517 * by VirtualBox. Can only be added for images opened in read/write
2518 * mode, so don't bother producing a sensible UUID otherwise. */
2519 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2520 RTUuidClear(&pImage->ModificationUuid);
2521 else
2522 {
2523 rc = RTUuidCreate(&pImage->ModificationUuid);
2524 if (RT_FAILURE(rc))
2525 return rc;
2526 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2527 VMDK_DDB_MODIFICATION_UUID,
2528 &pImage->ModificationUuid);
2529 if (RT_FAILURE(rc))
2530 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image modification UUID in descriptor in '%s'"), pImage->pszFilename);
2531 }
2532 }
2533 else if (RT_FAILURE(rc))
2534 return rc;
2535
2536 /* Get UUID of parent image. */
2537 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, VMDK_DDB_PARENT_UUID,
2538 &pImage->ParentUuid);
2539 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2540 {
2541 /* Image without UUID. Probably created by VMware and not yet used
2542 * by VirtualBox. Can only be added for images opened in read/write
2543 * mode, so don't bother producing a sensible UUID otherwise. */
2544 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2545 RTUuidClear(&pImage->ParentUuid);
2546 else
2547 {
2548 rc = RTUuidClear(&pImage->ParentUuid);
2549 if (RT_FAILURE(rc))
2550 return rc;
2551 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2552 VMDK_DDB_PARENT_UUID, &pImage->ParentUuid);
2553 if (RT_FAILURE(rc))
2554 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent UUID in descriptor in '%s'"), pImage->pszFilename);
2555 }
2556 }
2557 else if (RT_FAILURE(rc))
2558 return rc;
2559
2560 /* Get parent image modification UUID. */
2561 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor,
2562 VMDK_DDB_PARENT_MODIFICATION_UUID,
2563 &pImage->ParentModificationUuid);
2564 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2565 {
2566 /* Image without UUID. Probably created by VMware and not yet used
2567 * by VirtualBox. Can only be added for images opened in read/write
2568 * mode, so don't bother producing a sensible UUID otherwise. */
2569 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2570 RTUuidClear(&pImage->ParentModificationUuid);
2571 else
2572 {
2573 RTUuidClear(&pImage->ParentModificationUuid);
2574 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2575 VMDK_DDB_PARENT_MODIFICATION_UUID,
2576 &pImage->ParentModificationUuid);
2577 if (RT_FAILURE(rc))
2578 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent modification UUID in descriptor in '%s'"), pImage->pszFilename);
2579 }
2580 }
2581 else if (RT_FAILURE(rc))
2582 return rc;
2583
2584 return VINF_SUCCESS;
2585}
2586
2587/**
2588 * Internal : Prepares the descriptor to write to the image.
2589 */
2590static int vmdkDescriptorPrepare(PVMDKIMAGE pImage, uint64_t cbLimit,
2591 void **ppvData, size_t *pcbData)
2592{
2593 int rc = VINF_SUCCESS;
2594
2595 /*
2596 * Allocate temporary descriptor buffer.
2597 * In case there is no limit allocate a default
2598 * and increase if required.
2599 */
2600 size_t cbDescriptor = cbLimit ? cbLimit : 4 * _1K;
2601 char *pszDescriptor = (char *)RTMemAllocZ(cbDescriptor);
2602 size_t offDescriptor = 0;
2603
2604 if (!pszDescriptor)
2605 return VERR_NO_MEMORY;
2606
2607 for (unsigned i = 0; i < pImage->Descriptor.cLines; i++)
2608 {
2609 const char *psz = pImage->Descriptor.aLines[i];
2610 size_t cb = strlen(psz);
2611
2612 /*
2613 * Increase the descriptor if there is no limit and
2614 * there is not enough room left for this line.
2615 */
2616 if (offDescriptor + cb + 1 > cbDescriptor)
2617 {
2618 if (cbLimit)
2619 {
2620 rc = vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too long in '%s'"), pImage->pszFilename);
2621 break;
2622 }
2623 else
2624 {
2625 char *pszDescriptorNew = NULL;
2626 LogFlow(("Increasing descriptor cache\n"));
2627
2628 pszDescriptorNew = (char *)RTMemRealloc(pszDescriptor, cbDescriptor + cb + 4 * _1K);
2629 if (!pszDescriptorNew)
2630 {
2631 rc = VERR_NO_MEMORY;
2632 break;
2633 }
2634 pszDescriptor = pszDescriptorNew;
2635 cbDescriptor += cb + 4 * _1K;
2636 }
2637 }
2638
2639 if (cb > 0)
2640 {
2641 memcpy(pszDescriptor + offDescriptor, psz, cb);
2642 offDescriptor += cb;
2643 }
2644
2645 memcpy(pszDescriptor + offDescriptor, "\n", 1);
2646 offDescriptor++;
2647 }
2648
2649 if (RT_SUCCESS(rc))
2650 {
2651 *ppvData = pszDescriptor;
2652 *pcbData = offDescriptor;
2653 }
2654 else if (pszDescriptor)
2655 RTMemFree(pszDescriptor);
2656
2657 return rc;
2658}
2659
2660/**
2661 * Internal: write/update the descriptor part of the image.
2662 */
2663static int vmdkWriteDescriptor(PVMDKIMAGE pImage, PVDIOCTX pIoCtx)
2664{
2665 int rc = VINF_SUCCESS;
2666 uint64_t cbLimit;
2667 uint64_t uOffset;
2668 PVMDKFILE pDescFile;
2669 void *pvDescriptor = NULL;
2670 size_t cbDescriptor;
2671
2672 if (pImage->pDescData)
2673 {
2674 /* Separate descriptor file. */
2675 uOffset = 0;
2676 cbLimit = 0;
2677 pDescFile = pImage->pFile;
2678 }
2679 else
2680 {
2681 /* Embedded descriptor file. */
2682 uOffset = VMDK_SECTOR2BYTE(pImage->pExtents[0].uDescriptorSector);
2683 cbLimit = VMDK_SECTOR2BYTE(pImage->pExtents[0].cDescriptorSectors);
2684 pDescFile = pImage->pExtents[0].pFile;
2685 }
2686 /* Bail out if there is no file to write to. */
2687 if (pDescFile == NULL)
2688 return VERR_INVALID_PARAMETER;
2689
2690 rc = vmdkDescriptorPrepare(pImage, cbLimit, &pvDescriptor, &cbDescriptor);
2691 if (RT_SUCCESS(rc))
2692 {
2693 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pDescFile->pStorage,
2694 uOffset, pvDescriptor,
2695 cbLimit ? cbLimit : cbDescriptor,
2696 pIoCtx, NULL, NULL);
2697 if ( RT_FAILURE(rc)
2698 && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
2699 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing descriptor in '%s'"), pImage->pszFilename);
2700 }
2701
2702 if (RT_SUCCESS(rc) && !cbLimit)
2703 {
2704 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pDescFile->pStorage, cbDescriptor);
2705 if (RT_FAILURE(rc))
2706 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error truncating descriptor in '%s'"), pImage->pszFilename);
2707 }
2708
2709 if (RT_SUCCESS(rc))
2710 pImage->Descriptor.fDirty = false;
2711
2712 if (pvDescriptor)
2713 RTMemFree(pvDescriptor);
2714 return rc;
2715
2716}
2717
2718/**
2719 * Internal: validate the consistency check values in a binary header.
2720 */
2721static int vmdkValidateHeader(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, const SparseExtentHeader *pHeader)
2722{
2723 int rc = VINF_SUCCESS;
2724 if (RT_LE2H_U32(pHeader->magicNumber) != VMDK_SPARSE_MAGICNUMBER)
2725 {
2726 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect magic in sparse extent header in '%s'"), pExtent->pszFullname);
2727 return rc;
2728 }
2729 if (RT_LE2H_U32(pHeader->version) != 1 && RT_LE2H_U32(pHeader->version) != 3)
2730 {
2731 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);
2732 return rc;
2733 }
2734 if ( (RT_LE2H_U32(pHeader->flags) & 1)
2735 && ( pHeader->singleEndLineChar != '\n'
2736 || pHeader->nonEndLineChar != ' '
2737 || pHeader->doubleEndLineChar1 != '\r'
2738 || pHeader->doubleEndLineChar2 != '\n') )
2739 {
2740 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: corrupted by CR/LF translation in '%s'"), pExtent->pszFullname);
2741 return rc;
2742 }
2743 if (RT_LE2H_U64(pHeader->descriptorSize) > VMDK_SPARSE_DESCRIPTOR_SIZE_MAX)
2744 {
2745 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor size out of bounds (%llu vs %llu) '%s'"),
2746 pExtent->pszFullname, RT_LE2H_U64(pHeader->descriptorSize), VMDK_SPARSE_DESCRIPTOR_SIZE_MAX);
2747 return rc;
2748 }
2749 return rc;
2750}
2751
2752/**
2753 * Internal: read metadata belonging to an extent with binary header, i.e.
2754 * as found in monolithic files.
2755 */
2756static int vmdkReadBinaryMetaExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
2757 bool fMagicAlreadyRead)
2758{
2759 SparseExtentHeader Header;
2760 int rc;
2761
2762 if (!fMagicAlreadyRead)
2763 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage, 0,
2764 &Header, sizeof(Header));
2765 else
2766 {
2767 Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
2768 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
2769 RT_UOFFSETOF(SparseExtentHeader, version),
2770 &Header.version,
2771 sizeof(Header)
2772 - RT_UOFFSETOF(SparseExtentHeader, version));
2773 }
2774
2775 if (RT_SUCCESS(rc))
2776 {
2777 rc = vmdkValidateHeader(pImage, pExtent, &Header);
2778 if (RT_SUCCESS(rc))
2779 {
2780 uint64_t cbFile = 0;
2781
2782 if ( (RT_LE2H_U32(Header.flags) & RT_BIT(17))
2783 && RT_LE2H_U64(Header.gdOffset) == VMDK_GD_AT_END)
2784 pExtent->fFooter = true;
2785
2786 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2787 || ( pExtent->fFooter
2788 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
2789 {
2790 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbFile);
2791 if (RT_FAILURE(rc))
2792 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot get size of '%s'"), pExtent->pszFullname);
2793 }
2794
2795 if (RT_SUCCESS(rc))
2796 {
2797 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2798 pExtent->uAppendPosition = RT_ALIGN_64(cbFile, 512);
2799
2800 if ( pExtent->fFooter
2801 && ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2802 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
2803 {
2804 /* Read the footer, which comes before the end-of-stream marker. */
2805 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
2806 cbFile - 2*512, &Header,
2807 sizeof(Header));
2808 if (RT_FAILURE(rc))
2809 {
2810 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading extent footer in '%s'"), pExtent->pszFullname);
2811 rc = VERR_VD_VMDK_INVALID_HEADER;
2812 }
2813
2814 if (RT_SUCCESS(rc))
2815 rc = vmdkValidateHeader(pImage, pExtent, &Header);
2816 /* Prohibit any writes to this extent. */
2817 pExtent->uAppendPosition = 0;
2818 }
2819
2820 if (RT_SUCCESS(rc))
2821 {
2822 pExtent->uVersion = RT_LE2H_U32(Header.version);
2823 pExtent->enmType = VMDKETYPE_HOSTED_SPARSE; /* Just dummy value, changed later. */
2824 pExtent->cSectors = RT_LE2H_U64(Header.capacity);
2825 pExtent->cSectorsPerGrain = RT_LE2H_U64(Header.grainSize);
2826 pExtent->uDescriptorSector = RT_LE2H_U64(Header.descriptorOffset);
2827 pExtent->cDescriptorSectors = RT_LE2H_U64(Header.descriptorSize);
2828 pExtent->cGTEntries = RT_LE2H_U32(Header.numGTEsPerGT);
2829 pExtent->cOverheadSectors = RT_LE2H_U64(Header.overHead);
2830 pExtent->fUncleanShutdown = !!Header.uncleanShutdown;
2831 pExtent->uCompression = RT_LE2H_U16(Header.compressAlgorithm);
2832 if (RT_LE2H_U32(Header.flags) & RT_BIT(1))
2833 {
2834 pExtent->uSectorRGD = RT_LE2H_U64(Header.rgdOffset);
2835 pExtent->uSectorGD = RT_LE2H_U64(Header.gdOffset);
2836 }
2837 else
2838 {
2839 pExtent->uSectorGD = RT_LE2H_U64(Header.gdOffset);
2840 pExtent->uSectorRGD = 0;
2841 }
2842
2843 if (pExtent->uDescriptorSector && !pExtent->cDescriptorSectors)
2844 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2845 N_("VMDK: inconsistent embedded descriptor config in '%s'"), pExtent->pszFullname);
2846
2847 if ( RT_SUCCESS(rc)
2848 && ( pExtent->uSectorGD == VMDK_GD_AT_END
2849 || pExtent->uSectorRGD == VMDK_GD_AT_END)
2850 && ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2851 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
2852 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2853 N_("VMDK: cannot resolve grain directory offset in '%s'"), pExtent->pszFullname);
2854
2855 if (RT_SUCCESS(rc))
2856 {
2857 uint64_t cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
2858 if (!cSectorsPerGDE || cSectorsPerGDE > UINT32_MAX)
2859 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2860 N_("VMDK: incorrect grain directory size in '%s'"), pExtent->pszFullname);
2861 else
2862 {
2863 pExtent->cSectorsPerGDE = cSectorsPerGDE;
2864 pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
2865
2866 /* Fix up the number of descriptor sectors, as some flat images have
2867 * really just one, and this causes failures when inserting the UUID
2868 * values and other extra information. */
2869 if (pExtent->cDescriptorSectors != 0 && pExtent->cDescriptorSectors < 4)
2870 {
2871 /* Do it the easy way - just fix it for flat images which have no
2872 * other complicated metadata which needs space too. */
2873 if ( pExtent->uDescriptorSector + 4 < pExtent->cOverheadSectors
2874 && pExtent->cGTEntries * pExtent->cGDEntries == 0)
2875 pExtent->cDescriptorSectors = 4;
2876 }
2877 }
2878 }
2879 }
2880 }
2881 }
2882 }
2883 else
2884 {
2885 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading extent header in '%s'"), pExtent->pszFullname);
2886 rc = VERR_VD_VMDK_INVALID_HEADER;
2887 }
2888
2889 if (RT_FAILURE(rc))
2890 vmdkFreeExtentData(pImage, pExtent, false);
2891
2892 return rc;
2893}
2894
2895/**
2896 * Internal: read additional metadata belonging to an extent. For those
2897 * extents which have no additional metadata just verify the information.
2898 */
2899static int vmdkReadMetaExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
2900{
2901 int rc = VINF_SUCCESS;
2902
2903/* disabled the check as there are too many truncated vmdk images out there */
2904#ifdef VBOX_WITH_VMDK_STRICT_SIZE_CHECK
2905 uint64_t cbExtentSize;
2906 /* The image must be a multiple of a sector in size and contain the data
2907 * area (flat images only). If not, it means the image is at least
2908 * truncated, or even seriously garbled. */
2909 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbExtentSize);
2910 if (RT_FAILURE(rc))
2911 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting size in '%s'"), pExtent->pszFullname);
2912 else if ( cbExtentSize != RT_ALIGN_64(cbExtentSize, 512)
2913 && (pExtent->enmType != VMDKETYPE_FLAT || pExtent->cNominalSectors + pExtent->uSectorOffset > VMDK_BYTE2SECTOR(cbExtentSize)))
2914 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2915 N_("VMDK: file size is not a multiple of 512 in '%s', file is truncated or otherwise garbled"), pExtent->pszFullname);
2916#endif /* VBOX_WITH_VMDK_STRICT_SIZE_CHECK */
2917 if ( RT_SUCCESS(rc)
2918 && pExtent->enmType == VMDKETYPE_HOSTED_SPARSE)
2919 {
2920 /* The spec says that this must be a power of two and greater than 8,
2921 * but probably they meant not less than 8. */
2922 if ( (pExtent->cSectorsPerGrain & (pExtent->cSectorsPerGrain - 1))
2923 || pExtent->cSectorsPerGrain < 8)
2924 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2925 N_("VMDK: invalid extent grain size %u in '%s'"), pExtent->cSectorsPerGrain, pExtent->pszFullname);
2926 else
2927 {
2928 /* This code requires that a grain table must hold a power of two multiple
2929 * of the number of entries per GT cache entry. */
2930 if ( (pExtent->cGTEntries & (pExtent->cGTEntries - 1))
2931 || pExtent->cGTEntries < VMDK_GT_CACHELINE_SIZE)
2932 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2933 N_("VMDK: grain table cache size problem in '%s'"), pExtent->pszFullname);
2934 else
2935 {
2936 rc = vmdkAllocStreamBuffers(pImage, pExtent);
2937 if (RT_SUCCESS(rc))
2938 {
2939 /* Prohibit any writes to this streamOptimized extent. */
2940 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
2941 pExtent->uAppendPosition = 0;
2942
2943 if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
2944 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2945 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
2946 rc = vmdkReadGrainDirectory(pImage, pExtent);
2947 else
2948 {
2949 pExtent->uGrainSectorAbs = pExtent->cOverheadSectors;
2950 pExtent->cbGrainStreamRead = 0;
2951 }
2952 }
2953 }
2954 }
2955 }
2956
2957 if (RT_FAILURE(rc))
2958 vmdkFreeExtentData(pImage, pExtent, false);
2959
2960 return rc;
2961}
2962
2963/**
2964 * Internal: write/update the metadata for a sparse extent.
2965 */
2966static int vmdkWriteMetaSparseExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
2967 uint64_t uOffset, PVDIOCTX pIoCtx)
2968{
2969 SparseExtentHeader Header;
2970
2971 memset(&Header, '\0', sizeof(Header));
2972 Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
2973 Header.version = RT_H2LE_U32(pExtent->uVersion);
2974 Header.flags = RT_H2LE_U32(RT_BIT(0));
2975 if (pExtent->pRGD)
2976 Header.flags |= RT_H2LE_U32(RT_BIT(1));
2977 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
2978 Header.flags |= RT_H2LE_U32(RT_BIT(16) | RT_BIT(17));
2979 Header.capacity = RT_H2LE_U64(pExtent->cSectors);
2980 Header.grainSize = RT_H2LE_U64(pExtent->cSectorsPerGrain);
2981 Header.descriptorOffset = RT_H2LE_U64(pExtent->uDescriptorSector);
2982 Header.descriptorSize = RT_H2LE_U64(pExtent->cDescriptorSectors);
2983 Header.numGTEsPerGT = RT_H2LE_U32(pExtent->cGTEntries);
2984 if (pExtent->fFooter && uOffset == 0)
2985 {
2986 if (pExtent->pRGD)
2987 {
2988 Assert(pExtent->uSectorRGD);
2989 Header.rgdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
2990 Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
2991 }
2992 else
2993 Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
2994 }
2995 else
2996 {
2997 if (pExtent->pRGD)
2998 {
2999 Assert(pExtent->uSectorRGD);
3000 Header.rgdOffset = RT_H2LE_U64(pExtent->uSectorRGD);
3001 Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
3002 }
3003 else
3004 Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
3005 }
3006 Header.overHead = RT_H2LE_U64(pExtent->cOverheadSectors);
3007 Header.uncleanShutdown = pExtent->fUncleanShutdown;
3008 Header.singleEndLineChar = '\n';
3009 Header.nonEndLineChar = ' ';
3010 Header.doubleEndLineChar1 = '\r';
3011 Header.doubleEndLineChar2 = '\n';
3012 Header.compressAlgorithm = RT_H2LE_U16(pExtent->uCompression);
3013
3014 int rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
3015 uOffset, &Header, sizeof(Header),
3016 pIoCtx, NULL, NULL);
3017 if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
3018 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing extent header in '%s'"), pExtent->pszFullname);
3019 return rc;
3020}
3021
3022/**
3023 * Internal: free the buffers used for streamOptimized images.
3024 */
3025static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent)
3026{
3027 if (pExtent->pvCompGrain)
3028 {
3029 RTMemFree(pExtent->pvCompGrain);
3030 pExtent->pvCompGrain = NULL;
3031 }
3032 if (pExtent->pvGrain)
3033 {
3034 RTMemFree(pExtent->pvGrain);
3035 pExtent->pvGrain = NULL;
3036 }
3037}
3038
3039/**
3040 * Internal: free the memory used by the extent data structure, optionally
3041 * deleting the referenced files.
3042 *
3043 * @returns VBox status code.
3044 * @param pImage Pointer to the image instance data.
3045 * @param pExtent The extent to free.
3046 * @param fDelete Flag whether to delete the backing storage.
3047 */
3048static int vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
3049 bool fDelete)
3050{
3051 int rc = VINF_SUCCESS;
3052
3053 vmdkFreeGrainDirectory(pExtent);
3054 if (pExtent->pDescData)
3055 {
3056 RTMemFree(pExtent->pDescData);
3057 pExtent->pDescData = NULL;
3058 }
3059 if (pExtent->pFile != NULL)
3060 {
3061 /* Do not delete raw extents, these have full and base names equal. */
3062 rc = vmdkFileClose(pImage, &pExtent->pFile,
3063 fDelete
3064 && pExtent->pszFullname
3065 && pExtent->pszBasename
3066 && strcmp(pExtent->pszFullname, pExtent->pszBasename));
3067 }
3068 if (pExtent->pszBasename)
3069 {
3070 RTMemTmpFree((void *)pExtent->pszBasename);
3071 pExtent->pszBasename = NULL;
3072 }
3073 if (pExtent->pszFullname)
3074 {
3075 RTStrFree((char *)(void *)pExtent->pszFullname);
3076 pExtent->pszFullname = NULL;
3077 }
3078 vmdkFreeStreamBuffers(pExtent);
3079
3080 return rc;
3081}
3082
3083/**
3084 * Internal: allocate grain table cache if necessary for this image.
3085 */
3086static int vmdkAllocateGrainTableCache(PVMDKIMAGE pImage)
3087{
3088 PVMDKEXTENT pExtent;
3089
3090 /* Allocate grain table cache if any sparse extent is present. */
3091 for (unsigned i = 0; i < pImage->cExtents; i++)
3092 {
3093 pExtent = &pImage->pExtents[i];
3094 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE)
3095 {
3096 /* Allocate grain table cache. */
3097 pImage->pGTCache = (PVMDKGTCACHE)RTMemAllocZ(sizeof(VMDKGTCACHE));
3098 if (!pImage->pGTCache)
3099 return VERR_NO_MEMORY;
3100 for (unsigned j = 0; j < VMDK_GT_CACHE_SIZE; j++)
3101 {
3102 PVMDKGTCACHEENTRY pGCE = &pImage->pGTCache->aGTCache[j];
3103 pGCE->uExtent = UINT32_MAX;
3104 }
3105 pImage->pGTCache->cEntries = VMDK_GT_CACHE_SIZE;
3106 break;
3107 }
3108 }
3109
3110 return VINF_SUCCESS;
3111}
3112
3113/**
3114 * Internal: allocate the given number of extents.
3115 */
3116static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents)
3117{
3118 int rc = VINF_SUCCESS;
3119 PVMDKEXTENT pExtents = (PVMDKEXTENT)RTMemAllocZ(cExtents * sizeof(VMDKEXTENT));
3120 if (pExtents)
3121 {
3122 for (unsigned i = 0; i < cExtents; i++)
3123 {
3124 pExtents[i].pFile = NULL;
3125 pExtents[i].pszBasename = NULL;
3126 pExtents[i].pszFullname = NULL;
3127 pExtents[i].pGD = NULL;
3128 pExtents[i].pRGD = NULL;
3129 pExtents[i].pDescData = NULL;
3130 pExtents[i].uVersion = 1;
3131 pExtents[i].uCompression = VMDK_COMPRESSION_NONE;
3132 pExtents[i].uExtent = i;
3133 pExtents[i].pImage = pImage;
3134 }
3135 pImage->pExtents = pExtents;
3136 pImage->cExtents = cExtents;
3137 }
3138 else
3139 rc = VERR_NO_MEMORY;
3140
3141 return rc;
3142}
3143
3144/**
3145 * Internal: Create an additional file backed extent in split images.
3146 * Supports split sparse and flat images.
3147 *
3148 * @returns VBox status code.
3149 * @param pImage VMDK image instance.
3150 * @param cbSize Desiried size in bytes of new extent.
3151 */
3152static int vmdkAddFileBackedExtent(PVMDKIMAGE pImage, uint64_t cbSize)
3153{
3154 int rc = VINF_SUCCESS;
3155 unsigned uImageFlags = pImage->uImageFlags;
3156
3157 /* Check for unsupported image type. */
3158 if ((uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
3159 || (uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3160 || (uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK))
3161 {
3162 return VERR_NOT_SUPPORTED;
3163 }
3164
3165 /* Allocate array of extents and copy existing extents to it. */
3166 PVMDKEXTENT pNewExtents = (PVMDKEXTENT)RTMemAllocZ((pImage->cExtents + 1) * sizeof(VMDKEXTENT));
3167 if (!pNewExtents)
3168 {
3169 return VERR_NO_MEMORY;
3170 }
3171
3172 memcpy(pNewExtents, pImage->pExtents, pImage->cExtents * sizeof(VMDKEXTENT));
3173
3174 /* Locate newly created extent and populate default metadata. */
3175 PVMDKEXTENT pExtent = &pNewExtents[pImage->cExtents];
3176
3177 pExtent->pFile = NULL;
3178 pExtent->pszBasename = NULL;
3179 pExtent->pszFullname = NULL;
3180 pExtent->pGD = NULL;
3181 pExtent->pRGD = NULL;
3182 pExtent->pDescData = NULL;
3183 pExtent->uVersion = 1;
3184 pExtent->uCompression = VMDK_COMPRESSION_NONE;
3185 pExtent->uExtent = pImage->cExtents;
3186 pExtent->pImage = pImage;
3187 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
3188 pExtent->enmAccess = VMDKACCESS_READWRITE;
3189 pExtent->uSectorOffset = 0;
3190 pExtent->fMetaDirty = true;
3191
3192 /* Apply image type specific meta data. */
3193 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
3194 {
3195 pExtent->enmType = VMDKETYPE_FLAT;
3196 }
3197 else
3198 {
3199 uint64_t cSectorsPerGDE, cSectorsPerGD;
3200 pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
3201 pExtent->cSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64(cbSize, _64K));
3202 pExtent->cSectorsPerGrain = VMDK_BYTE2SECTOR(_64K);
3203 pExtent->cGTEntries = 512;
3204 cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
3205 pExtent->cSectorsPerGDE = cSectorsPerGDE;
3206 pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
3207 cSectorsPerGD = (pExtent->cGDEntries + (512 / sizeof(uint32_t) - 1)) / (512 / sizeof(uint32_t));
3208 }
3209
3210 /* Allocate and set file name for extent. */
3211 char *pszBasenameSubstr = RTPathFilename(pImage->pszFilename);
3212 AssertPtr(pszBasenameSubstr);
3213
3214 char *pszBasenameSuff = RTPathSuffix(pszBasenameSubstr);
3215 char *pszBasenameBase = RTStrDup(pszBasenameSubstr);
3216 RTPathStripSuffix(pszBasenameBase);
3217 char *pszTmp;
3218 size_t cbTmp;
3219
3220 if (pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
3221 RTStrAPrintf(&pszTmp, "%s-f%03d%s", pszBasenameBase,
3222 pExtent->uExtent + 1, pszBasenameSuff);
3223 else
3224 RTStrAPrintf(&pszTmp, "%s-s%03d%s", pszBasenameBase, pExtent->uExtent + 1,
3225 pszBasenameSuff);
3226
3227 RTStrFree(pszBasenameBase);
3228 if (!pszTmp)
3229 return VERR_NO_STR_MEMORY;
3230 cbTmp = strlen(pszTmp) + 1;
3231 char *pszBasename = (char *)RTMemTmpAlloc(cbTmp);
3232 if (!pszBasename)
3233 {
3234 RTStrFree(pszTmp);
3235 return VERR_NO_MEMORY;
3236 }
3237
3238 memcpy(pszBasename, pszTmp, cbTmp);
3239 RTStrFree(pszTmp);
3240
3241 pExtent->pszBasename = pszBasename;
3242
3243 char *pszBasedirectory = RTStrDup(pImage->pszFilename);
3244 if (!pszBasedirectory)
3245 return VERR_NO_STR_MEMORY;
3246 RTPathStripFilename(pszBasedirectory);
3247 char *pszFullname = RTPathJoinA(pszBasedirectory, pExtent->pszBasename);
3248 RTStrFree(pszBasedirectory);
3249 if (!pszFullname)
3250 return VERR_NO_STR_MEMORY;
3251 pExtent->pszFullname = pszFullname;
3252
3253 /* Create file for extent. */
3254 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
3255 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
3256 true /* fCreate */));
3257 if (RT_FAILURE(rc))
3258 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pExtent->pszFullname);
3259
3260 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
3261 {
3262 /* For flat images: Pre allocate file space. */
3263 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage, cbSize,
3264 0 /* fFlags */, NULL, 0, 0);
3265 if (RT_FAILURE(rc))
3266 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
3267 }
3268 else
3269 {
3270 /* For sparse images: Allocate new grain directories/tables. */
3271 /* fPreAlloc should never be false because VMware can't use such images. */
3272 rc = vmdkCreateGrainDirectory(pImage, pExtent,
3273 RT_MAX( pExtent->uDescriptorSector
3274 + pExtent->cDescriptorSectors,
3275 1),
3276 true /* fPreAlloc */);
3277 if (RT_FAILURE(rc))
3278 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new grain directory in '%s'"), pExtent->pszFullname);
3279 }
3280
3281 /* Insert new extent into descriptor file. */
3282 rc = vmdkDescExtInsert(pImage, &pImage->Descriptor, pExtent->enmAccess,
3283 pExtent->cNominalSectors, pExtent->enmType,
3284 pExtent->pszBasename, pExtent->uSectorOffset);
3285 if (RT_FAILURE(rc))
3286 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not insert the extent list into descriptor in '%s'"), pImage->pszFilename);
3287
3288 pImage->pExtents = pNewExtents;
3289 pImage->cExtents++;
3290
3291 return rc;
3292}
3293
3294/**
3295 * Reads and processes the descriptor embedded in sparse images.
3296 *
3297 * @returns VBox status code.
3298 * @param pImage VMDK image instance.
3299 * @param pFile The sparse file handle.
3300 */
3301static int vmdkDescriptorReadSparse(PVMDKIMAGE pImage, PVMDKFILE pFile)
3302{
3303 /* It's a hosted single-extent image. */
3304 int rc = vmdkCreateExtents(pImage, 1);
3305 if (RT_SUCCESS(rc))
3306 {
3307 /* The opened file is passed to the extent. No separate descriptor
3308 * file, so no need to keep anything open for the image. */
3309 PVMDKEXTENT pExtent = &pImage->pExtents[0];
3310 pExtent->pFile = pFile;
3311 pImage->pFile = NULL;
3312 pExtent->pszFullname = RTPathAbsDup(pImage->pszFilename);
3313 if (RT_LIKELY(pExtent->pszFullname))
3314 {
3315 /* As we're dealing with a monolithic image here, there must
3316 * be a descriptor embedded in the image file. */
3317 rc = vmdkReadBinaryMetaExtent(pImage, pExtent, true /* fMagicAlreadyRead */);
3318 if ( RT_SUCCESS(rc)
3319 && pExtent->uDescriptorSector
3320 && pExtent->cDescriptorSectors)
3321 {
3322 /* HACK: extend the descriptor if it is unusually small and it fits in
3323 * the unused space after the image header. Allows opening VMDK files
3324 * with extremely small descriptor in read/write mode.
3325 *
3326 * The previous version introduced a possible regression for VMDK stream
3327 * optimized images from VMware which tend to have only a single sector sized
3328 * descriptor. Increasing the descriptor size resulted in adding the various uuid
3329 * entries required to make it work with VBox but for stream optimized images
3330 * the updated binary header wasn't written to the disk creating a mismatch
3331 * between advertised and real descriptor size.
3332 *
3333 * The descriptor size will be increased even if opened readonly now if there
3334 * enough room but the new value will not be written back to the image.
3335 */
3336 if ( pExtent->cDescriptorSectors < 3
3337 && (int64_t)pExtent->uSectorGD - pExtent->uDescriptorSector >= 4
3338 && (!pExtent->uSectorRGD || (int64_t)pExtent->uSectorRGD - pExtent->uDescriptorSector >= 4))
3339 {
3340 uint64_t cDescriptorSectorsOld = pExtent->cDescriptorSectors;
3341
3342 pExtent->cDescriptorSectors = 4;
3343 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3344 {
3345 /*
3346 * Update the on disk number now to make sure we don't introduce inconsistencies
3347 * in case of stream optimized images from VMware where the descriptor is just
3348 * one sector big (the binary header is not written to disk for complete
3349 * stream optimized images in vmdkFlushImage()).
3350 */
3351 uint64_t u64DescSizeNew = RT_H2LE_U64(pExtent->cDescriptorSectors);
3352 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pFile->pStorage,
3353 RT_UOFFSETOF(SparseExtentHeader, descriptorSize),
3354 &u64DescSizeNew, sizeof(u64DescSizeNew));
3355 if (RT_FAILURE(rc))
3356 {
3357 LogFlowFunc(("Increasing the descriptor size failed with %Rrc\n", rc));
3358 /* Restore the old size and carry on. */
3359 pExtent->cDescriptorSectors = cDescriptorSectorsOld;
3360 }
3361 }
3362 }
3363 /* Read the descriptor from the extent. */
3364 pExtent->pDescData = (char *)RTMemAllocZ(VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
3365 if (RT_LIKELY(pExtent->pDescData))
3366 {
3367 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
3368 VMDK_SECTOR2BYTE(pExtent->uDescriptorSector),
3369 pExtent->pDescData,
3370 VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
3371 if (RT_SUCCESS(rc))
3372 {
3373 rc = vmdkParseDescriptor(pImage, pExtent->pDescData,
3374 VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
3375 if ( RT_SUCCESS(rc)
3376 && ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3377 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)))
3378 {
3379 rc = vmdkReadMetaExtent(pImage, pExtent);
3380 if (RT_SUCCESS(rc))
3381 {
3382 /* Mark the extent as unclean if opened in read-write mode. */
3383 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3384 && !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
3385 {
3386 pExtent->fUncleanShutdown = true;
3387 pExtent->fMetaDirty = true;
3388 }
3389 }
3390 }
3391 else if (RT_SUCCESS(rc))
3392 rc = VERR_NOT_SUPPORTED;
3393 }
3394 else
3395 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pExtent->pszFullname);
3396 }
3397 else
3398 rc = VERR_NO_MEMORY;
3399 }
3400 else if (RT_SUCCESS(rc))
3401 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: monolithic image without descriptor in '%s'"), pImage->pszFilename);
3402 }
3403 else
3404 rc = VERR_NO_MEMORY;
3405 }
3406
3407 return rc;
3408}
3409
3410/**
3411 * Reads the descriptor from a pure text file.
3412 *
3413 * @returns VBox status code.
3414 * @param pImage VMDK image instance.
3415 * @param pFile The descriptor file handle.
3416 */
3417static int vmdkDescriptorReadAscii(PVMDKIMAGE pImage, PVMDKFILE pFile)
3418{
3419 /* Allocate at least 10K, and make sure that there is 5K free space
3420 * in case new entries need to be added to the descriptor. Never
3421 * allocate more than 128K, because that's no valid descriptor file
3422 * and will result in the correct "truncated read" error handling. */
3423 uint64_t cbFileSize;
3424 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pFile->pStorage, &cbFileSize);
3425 if ( RT_SUCCESS(rc)
3426 && cbFileSize >= 50)
3427 {
3428 uint64_t cbSize = cbFileSize;
3429 if (cbSize % VMDK_SECTOR2BYTE(10))
3430 cbSize += VMDK_SECTOR2BYTE(20) - cbSize % VMDK_SECTOR2BYTE(10);
3431 else
3432 cbSize += VMDK_SECTOR2BYTE(10);
3433 cbSize = RT_MIN(cbSize, _128K);
3434 pImage->cbDescAlloc = RT_MAX(VMDK_SECTOR2BYTE(20), cbSize);
3435 pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
3436 if (RT_LIKELY(pImage->pDescData))
3437 {
3438 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pFile->pStorage, 0, pImage->pDescData,
3439 RT_MIN(pImage->cbDescAlloc, cbFileSize));
3440 if (RT_SUCCESS(rc))
3441 {
3442#if 0 /** @todo Revisit */
3443 cbRead += sizeof(u32Magic);
3444 if (cbRead == pImage->cbDescAlloc)
3445 {
3446 /* Likely the read is truncated. Better fail a bit too early
3447 * (normally the descriptor is much smaller than our buffer). */
3448 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: cannot read descriptor in '%s'"), pImage->pszFilename);
3449 goto out;
3450 }
3451#endif
3452 rc = vmdkParseDescriptor(pImage, pImage->pDescData,
3453 pImage->cbDescAlloc);
3454 if (RT_SUCCESS(rc))
3455 {
3456 for (unsigned i = 0; i < pImage->cExtents && RT_SUCCESS(rc); i++)
3457 {
3458 PVMDKEXTENT pExtent = &pImage->pExtents[i];
3459 if (pExtent->pszBasename)
3460 {
3461 /* Hack to figure out whether the specified name in the
3462 * extent descriptor is absolute. Doesn't always work, but
3463 * should be good enough for now. */
3464 char *pszFullname;
3465 /** @todo implement proper path absolute check. */
3466 if (pExtent->pszBasename[0] == RTPATH_SLASH)
3467 {
3468 pszFullname = RTStrDup(pExtent->pszBasename);
3469 if (!pszFullname)
3470 {
3471 rc = VERR_NO_MEMORY;
3472 break;
3473 }
3474 }
3475 else
3476 {
3477 char *pszDirname = RTStrDup(pImage->pszFilename);
3478 if (!pszDirname)
3479 {
3480 rc = VERR_NO_MEMORY;
3481 break;
3482 }
3483 RTPathStripFilename(pszDirname);
3484 pszFullname = RTPathJoinA(pszDirname, pExtent->pszBasename);
3485 RTStrFree(pszDirname);
3486 if (!pszFullname)
3487 {
3488 rc = VERR_NO_STR_MEMORY;
3489 break;
3490 }
3491 }
3492 pExtent->pszFullname = pszFullname;
3493 }
3494 else
3495 pExtent->pszFullname = NULL;
3496
3497 unsigned uOpenFlags = pImage->uOpenFlags | ((pExtent->enmAccess == VMDKACCESS_READONLY) ? VD_OPEN_FLAGS_READONLY : 0);
3498 switch (pExtent->enmType)
3499 {
3500 case VMDKETYPE_HOSTED_SPARSE:
3501 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
3502 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */));
3503 if (RT_FAILURE(rc))
3504 {
3505 /* Do NOT signal an appropriate error here, as the VD
3506 * layer has the choice of retrying the open if it
3507 * failed. */
3508 break;
3509 }
3510 rc = vmdkReadBinaryMetaExtent(pImage, pExtent,
3511 false /* fMagicAlreadyRead */);
3512 if (RT_FAILURE(rc))
3513 break;
3514 rc = vmdkReadMetaExtent(pImage, pExtent);
3515 if (RT_FAILURE(rc))
3516 break;
3517
3518 /* Mark extent as unclean if opened in read-write mode. */
3519 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3520 {
3521 pExtent->fUncleanShutdown = true;
3522 pExtent->fMetaDirty = true;
3523 }
3524 break;
3525 case VMDKETYPE_VMFS:
3526 case VMDKETYPE_FLAT:
3527 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
3528 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */));
3529 if (RT_FAILURE(rc))
3530 {
3531 /* Do NOT signal an appropriate error here, as the VD
3532 * layer has the choice of retrying the open if it
3533 * failed. */
3534 break;
3535 }
3536 break;
3537 case VMDKETYPE_ZERO:
3538 /* Nothing to do. */
3539 break;
3540 default:
3541 AssertMsgFailed(("unknown vmdk extent type %d\n", pExtent->enmType));
3542 }
3543 }
3544 }
3545 }
3546 else
3547 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pImage->pszFilename);
3548 }
3549 else
3550 rc = VERR_NO_MEMORY;
3551 }
3552 else if (RT_SUCCESS(rc))
3553 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor in '%s' is too short"), pImage->pszFilename);
3554
3555 return rc;
3556}
3557
3558/**
3559 * Read and process the descriptor based on the image type.
3560 *
3561 * @returns VBox status code.
3562 * @param pImage VMDK image instance.
3563 * @param pFile VMDK file handle.
3564 */
3565static int vmdkDescriptorRead(PVMDKIMAGE pImage, PVMDKFILE pFile)
3566{
3567 uint32_t u32Magic;
3568
3569 /* Read magic (if present). */
3570 int rc = vdIfIoIntFileReadSync(pImage->pIfIo, pFile->pStorage, 0,
3571 &u32Magic, sizeof(u32Magic));
3572 if (RT_SUCCESS(rc))
3573 {
3574 /* Handle the file according to its magic number. */
3575 if (RT_LE2H_U32(u32Magic) == VMDK_SPARSE_MAGICNUMBER)
3576 rc = vmdkDescriptorReadSparse(pImage, pFile);
3577 else
3578 rc = vmdkDescriptorReadAscii(pImage, pFile);
3579 }
3580 else
3581 {
3582 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading the magic number in '%s'"), pImage->pszFilename);
3583 rc = VERR_VD_VMDK_INVALID_HEADER;
3584 }
3585
3586 return rc;
3587}
3588
3589/**
3590 * Internal: Open an image, constructing all necessary data structures.
3591 */
3592static int vmdkOpenImage(PVMDKIMAGE pImage, unsigned uOpenFlags)
3593{
3594 pImage->uOpenFlags = uOpenFlags;
3595 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
3596 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
3597 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
3598
3599 /*
3600 * Open the image.
3601 * We don't have to check for asynchronous access because
3602 * we only support raw access and the opened file is a description
3603 * file were no data is stored.
3604 */
3605 PVMDKFILE pFile;
3606 int rc = vmdkFileOpen(pImage, &pFile, NULL, pImage->pszFilename,
3607 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */));
3608 if (RT_SUCCESS(rc))
3609 {
3610 pImage->pFile = pFile;
3611
3612 rc = vmdkDescriptorRead(pImage, pFile);
3613 if (RT_SUCCESS(rc))
3614 {
3615 /* Determine PCHS geometry if not set. */
3616 if (pImage->PCHSGeometry.cCylinders == 0)
3617 {
3618 uint64_t cCylinders = VMDK_BYTE2SECTOR(pImage->cbSize)
3619 / pImage->PCHSGeometry.cHeads
3620 / pImage->PCHSGeometry.cSectors;
3621 pImage->PCHSGeometry.cCylinders = (unsigned)RT_MIN(cCylinders, 16383);
3622 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3623 && !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
3624 {
3625 rc = vmdkDescSetPCHSGeometry(pImage, &pImage->PCHSGeometry);
3626 AssertRC(rc);
3627 }
3628 }
3629
3630 /* Update the image metadata now in case has changed. */
3631 rc = vmdkFlushImage(pImage, NULL);
3632 if (RT_SUCCESS(rc))
3633 {
3634 /* Figure out a few per-image constants from the extents. */
3635 pImage->cbSize = 0;
3636 for (unsigned i = 0; i < pImage->cExtents; i++)
3637 {
3638 PVMDKEXTENT pExtent = &pImage->pExtents[i];
3639 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE)
3640 {
3641 /* Here used to be a check whether the nominal size of an extent
3642 * is a multiple of the grain size. The spec says that this is
3643 * always the case, but unfortunately some files out there in the
3644 * wild violate the spec (e.g. ReactOS 0.3.1). */
3645 }
3646 else if ( pExtent->enmType == VMDKETYPE_FLAT
3647 || pExtent->enmType == VMDKETYPE_ZERO)
3648 pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED;
3649
3650 pImage->cbSize += VMDK_SECTOR2BYTE(pExtent->cNominalSectors);
3651 }
3652
3653 if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3654 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3655 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
3656 rc = vmdkAllocateGrainTableCache(pImage);
3657 }
3658 }
3659 }
3660 /* else: Do NOT signal an appropriate error here, as the VD layer has the
3661 * choice of retrying the open if it failed. */
3662
3663 if (RT_SUCCESS(rc))
3664 {
3665 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
3666 pImage->RegionList.fFlags = 0;
3667 pImage->RegionList.cRegions = 1;
3668
3669 pRegion->offRegion = 0; /* Disk start. */
3670 pRegion->cbBlock = 512;
3671 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
3672 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
3673 pRegion->cbData = 512;
3674 pRegion->cbMetadata = 0;
3675 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
3676 }
3677 else
3678 vmdkFreeImage(pImage, false, false /*fFlush*/); /* Don't try to flush anything if opening failed. */
3679 return rc;
3680}
3681
3682/**
3683 * Frees a raw descriptor.
3684 * @internal
3685 */
3686static int vmdkRawDescFree(PVDISKRAW pRawDesc)
3687{
3688 if (!pRawDesc)
3689 return VINF_SUCCESS;
3690
3691 RTStrFree(pRawDesc->pszRawDisk);
3692 pRawDesc->pszRawDisk = NULL;
3693
3694 /* Partitions: */
3695 for (unsigned i = 0; i < pRawDesc->cPartDescs; i++)
3696 {
3697 RTStrFree(pRawDesc->pPartDescs[i].pszRawDevice);
3698 pRawDesc->pPartDescs[i].pszRawDevice = NULL;
3699
3700 RTMemFree(pRawDesc->pPartDescs[i].pvPartitionData);
3701 pRawDesc->pPartDescs[i].pvPartitionData = NULL;
3702 }
3703
3704 RTMemFree(pRawDesc->pPartDescs);
3705 pRawDesc->pPartDescs = NULL;
3706
3707 RTMemFree(pRawDesc);
3708 return VINF_SUCCESS;
3709}
3710
3711/**
3712 * Helper that grows the raw partition descriptor table by @a cToAdd entries,
3713 * returning the pointer to the first new entry.
3714 * @internal
3715 */
3716static int vmdkRawDescAppendPartDesc(PVMDKIMAGE pImage, PVDISKRAW pRawDesc, uint32_t cToAdd, PVDISKRAWPARTDESC *ppRet)
3717{
3718 uint32_t const cOld = pRawDesc->cPartDescs;
3719 uint32_t const cNew = cOld + cToAdd;
3720 PVDISKRAWPARTDESC paNew = (PVDISKRAWPARTDESC)RTMemReallocZ(pRawDesc->pPartDescs,
3721 cOld * sizeof(pRawDesc->pPartDescs[0]),
3722 cNew * sizeof(pRawDesc->pPartDescs[0]));
3723 if (paNew)
3724 {
3725 pRawDesc->cPartDescs = cNew;
3726 pRawDesc->pPartDescs = paNew;
3727
3728 *ppRet = &paNew[cOld];
3729 return VINF_SUCCESS;
3730 }
3731 *ppRet = NULL;
3732 return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
3733 N_("VMDK: Image path: '%s'. Out of memory growing the partition descriptors (%u -> %u)."),
3734 pImage->pszFilename, cOld, cNew);
3735}
3736
3737/**
3738 * @callback_method_impl{FNRTSORTCMP}
3739 */
3740static DECLCALLBACK(int) vmdkRawDescPartComp(void const *pvElement1, void const *pvElement2, void *pvUser)
3741{
3742 RT_NOREF(pvUser);
3743 int64_t const iDelta = ((PVDISKRAWPARTDESC)pvElement1)->offStartInVDisk - ((PVDISKRAWPARTDESC)pvElement2)->offStartInVDisk;
3744 return iDelta < 0 ? -1 : iDelta > 0 ? 1 : 0;
3745}
3746
3747/**
3748 * Post processes the partition descriptors.
3749 *
3750 * Sorts them and check that they don't overlap.
3751 */
3752static int vmdkRawDescPostProcessPartitions(PVMDKIMAGE pImage, PVDISKRAW pRawDesc, uint64_t cbSize)
3753{
3754 /*
3755 * Sort data areas in ascending order of start.
3756 */
3757 RTSortShell(pRawDesc->pPartDescs, pRawDesc->cPartDescs, sizeof(pRawDesc->pPartDescs[0]), vmdkRawDescPartComp, NULL);
3758
3759 /*
3760 * Check that we don't have overlapping descriptors. If we do, that's an
3761 * indication that the drive is corrupt or that the RTDvm code is buggy.
3762 */
3763 VDISKRAWPARTDESC const *paPartDescs = pRawDesc->pPartDescs;
3764 for (uint32_t i = 0; i < pRawDesc->cPartDescs; i++)
3765 {
3766 uint64_t offLast = paPartDescs[i].offStartInVDisk + paPartDescs[i].cbData;
3767 if (offLast <= paPartDescs[i].offStartInVDisk)
3768 return vdIfError(pImage->pIfError, VERR_FILESYSTEM_CORRUPT /*?*/, RT_SRC_POS,
3769 N_("VMDK: Image path: '%s'. Bogus partition descriptor #%u (%#RX64 LB %#RX64%s): Wrap around or zero"),
3770 pImage->pszFilename, i, paPartDescs[i].offStartInVDisk, paPartDescs[i].cbData,
3771 paPartDescs[i].pvPartitionData ? " (data)" : "");
3772 offLast -= 1;
3773
3774 if (i + 1 < pRawDesc->cPartDescs && offLast >= paPartDescs[i + 1].offStartInVDisk)
3775 return vdIfError(pImage->pIfError, VERR_FILESYSTEM_CORRUPT /*?*/, RT_SRC_POS,
3776 N_("VMDK: Image path: '%s'. Partition descriptor #%u (%#RX64 LB %#RX64%s) overlaps with the next (%#RX64 LB %#RX64%s)"),
3777 pImage->pszFilename, i, paPartDescs[i].offStartInVDisk, paPartDescs[i].cbData,
3778 paPartDescs[i].pvPartitionData ? " (data)" : "", paPartDescs[i + 1].offStartInVDisk,
3779 paPartDescs[i + 1].cbData, paPartDescs[i + 1].pvPartitionData ? " (data)" : "");
3780 if (offLast >= cbSize)
3781 return vdIfError(pImage->pIfError, VERR_FILESYSTEM_CORRUPT /*?*/, RT_SRC_POS,
3782 N_("VMDK: Image path: '%s'. Partition descriptor #%u (%#RX64 LB %#RX64%s) goes beyond the end of the drive (%#RX64)"),
3783 pImage->pszFilename, i, paPartDescs[i].offStartInVDisk, paPartDescs[i].cbData,
3784 paPartDescs[i].pvPartitionData ? " (data)" : "", cbSize);
3785 }
3786
3787 return VINF_SUCCESS;
3788}
3789
3790
3791#ifdef RT_OS_LINUX
3792/**
3793 * Searches the dir specified in @a pszBlockDevDir for subdirectories with a
3794 * 'dev' file matching @a uDevToLocate.
3795 *
3796 * This is used both
3797 *
3798 * @returns IPRT status code, errors have been reported properly.
3799 * @param pImage For error reporting.
3800 * @param pszBlockDevDir Input: Path to the directory search under.
3801 * Output: Path to the directory containing information
3802 * for @a uDevToLocate.
3803 * @param cbBlockDevDir The size of the buffer @a pszBlockDevDir points to.
3804 * @param uDevToLocate The device number of the block device info dir to
3805 * locate.
3806 * @param pszDevToLocate For error reporting.
3807 */
3808static int vmdkFindSysBlockDevPath(PVMDKIMAGE pImage, char *pszBlockDevDir, size_t cbBlockDevDir,
3809 dev_t uDevToLocate, const char *pszDevToLocate)
3810{
3811 size_t const cchDir = RTPathEnsureTrailingSeparator(pszBlockDevDir, cbBlockDevDir);
3812 AssertReturn(cchDir > 0, VERR_BUFFER_OVERFLOW);
3813
3814 RTDIR hDir = NIL_RTDIR;
3815 int rc = RTDirOpen(&hDir, pszBlockDevDir);
3816 if (RT_SUCCESS(rc))
3817 {
3818 for (;;)
3819 {
3820 RTDIRENTRY Entry;
3821 rc = RTDirRead(hDir, &Entry, NULL);
3822 if (RT_SUCCESS(rc))
3823 {
3824 /* We're interested in directories and symlinks. */
3825 if ( Entry.enmType == RTDIRENTRYTYPE_DIRECTORY
3826 || Entry.enmType == RTDIRENTRYTYPE_SYMLINK
3827 || Entry.enmType == RTDIRENTRYTYPE_UNKNOWN)
3828 {
3829 rc = RTStrCopy(&pszBlockDevDir[cchDir], cbBlockDevDir - cchDir, Entry.szName);
3830 AssertContinue(RT_SUCCESS(rc)); /* should not happen! */
3831
3832 dev_t uThisDevNo = ~uDevToLocate;
3833 rc = RTLinuxSysFsReadDevNumFile(&uThisDevNo, "%s/dev", pszBlockDevDir);
3834 if (RT_SUCCESS(rc) && uThisDevNo == uDevToLocate)
3835 break;
3836 }
3837 }
3838 else
3839 {
3840 pszBlockDevDir[cchDir] = '\0';
3841 if (rc == VERR_NO_MORE_FILES)
3842 rc = vdIfError(pImage->pIfError, VERR_NOT_FOUND, RT_SRC_POS,
3843 N_("VMDK: Image path: '%s'. Failed to locate device corresponding to '%s' under '%s'"),
3844 pImage->pszFilename, pszDevToLocate, pszBlockDevDir);
3845 else
3846 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
3847 N_("VMDK: Image path: '%s'. RTDirRead failed enumerating '%s': %Rrc"),
3848 pImage->pszFilename, pszBlockDevDir, rc);
3849 break;
3850 }
3851 }
3852 RTDirClose(hDir);
3853 }
3854 else
3855 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
3856 N_("VMDK: Image path: '%s'. Failed to open dir '%s' for listing: %Rrc"),
3857 pImage->pszFilename, pszBlockDevDir, rc);
3858 return rc;
3859}
3860#endif /* RT_OS_LINUX */
3861
3862#ifdef RT_OS_FREEBSD
3863
3864
3865/**
3866 * Reads the config data from the provider and returns offset and size
3867 *
3868 * @return IPRT status code
3869 * @param pProvider GEOM provider representing partition
3870 * @param pcbOffset Placeholder for the offset of the partition
3871 * @param pcbSize Placeholder for the size of the partition
3872 */
3873static int vmdkReadPartitionsParamsFromProvider(gprovider *pProvider, uint64_t *pcbOffset, uint64_t *pcbSize)
3874{
3875 gconfig *pConfEntry;
3876 int rc = VERR_NOT_FOUND;
3877
3878 /*
3879 * Required parameters are located in the list containing key/value pairs.
3880 * Both key and value are in text form. Manuals tells nothing about the fact
3881 * that the both parameters should be present in the list. Thus, there are
3882 * cases when only one parameter is presented. To handle such cases we treat
3883 * absent params as zero allowing the caller decide the case is either correct
3884 * or an error.
3885 */
3886 uint64_t cbOffset = 0;
3887 uint64_t cbSize = 0;
3888 LIST_FOREACH(pConfEntry, &pProvider->lg_config, lg_config)
3889 {
3890 if (RTStrCmp(pConfEntry->lg_name, "offset") == 0)
3891 {
3892 cbOffset = RTStrToUInt64(pConfEntry->lg_val);
3893 rc = VINF_SUCCESS;
3894 }
3895 else if (RTStrCmp(pConfEntry->lg_name, "length") == 0)
3896 {
3897 cbSize = RTStrToUInt64(pConfEntry->lg_val);
3898 rc = VINF_SUCCESS;
3899 }
3900 }
3901 if (RT_SUCCESS(rc))
3902 {
3903 *pcbOffset = cbOffset;
3904 *pcbSize = cbSize;
3905 }
3906 return rc;
3907}
3908
3909
3910/**
3911 * Searches the partition specified by name and calculates its size and absolute offset.
3912 *
3913 * @return IPRT status code.
3914 * @param pParentClass Class containing pParentGeom
3915 * @param pszParentGeomName Name of the parent geom where we are looking for provider
3916 * @param pszProviderName Name of the provider we are looking for
3917 * @param pcbAbsoluteOffset Placeholder for the absolute offset of the partition, i.e. offset from the beginning of the disk
3918 * @param psbSize Placeholder for the size of the partition.
3919 */
3920static int vmdkFindPartitionParamsByName(gclass *pParentClass, const char *pszParentGeomName, const char *pszProviderName,
3921 uint64_t *pcbAbsoluteOffset, uint64_t *pcbSize)
3922{
3923 AssertReturn(pParentClass, VERR_INVALID_PARAMETER);
3924 AssertReturn(pszParentGeomName, VERR_INVALID_PARAMETER);
3925 AssertReturn(pszProviderName, VERR_INVALID_PARAMETER);
3926 AssertReturn(pcbAbsoluteOffset, VERR_INVALID_PARAMETER);
3927 AssertReturn(pcbSize, VERR_INVALID_PARAMETER);
3928
3929 ggeom *pParentGeom;
3930 int rc = VERR_NOT_FOUND;
3931 LIST_FOREACH(pParentGeom, &pParentClass->lg_geom, lg_geom)
3932 {
3933 if (RTStrCmp(pParentGeom->lg_name, pszParentGeomName) == 0)
3934 {
3935 rc = VINF_SUCCESS;
3936 break;
3937 }
3938 }
3939 if (RT_FAILURE(rc))
3940 return rc;
3941
3942 gprovider *pProvider;
3943 /*
3944 * First, go over providers without handling EBR or BSDLabel
3945 * partitions for case when looking provider is child
3946 * of the givng geom, to reduce searching time
3947 */
3948 LIST_FOREACH(pProvider, &pParentGeom->lg_provider, lg_provider)
3949 {
3950 if (RTStrCmp(pProvider->lg_name, pszProviderName) == 0)
3951 return vmdkReadPartitionsParamsFromProvider(pProvider, pcbAbsoluteOffset, pcbSize);
3952 }
3953
3954 /*
3955 * No provider found. Go over the parent geom again
3956 * and make recursions if geom represents EBR or BSDLabel.
3957 * In this case given parent geom contains only EBR or BSDLabel
3958 * partition itself and their own partitions are in the separate
3959 * geoms. Also, partition offsets are relative to geom, so
3960 * we have to add offset from child provider with parent geoms
3961 * provider
3962 */
3963
3964 LIST_FOREACH(pProvider, &pParentGeom->lg_provider, lg_provider)
3965 {
3966 uint64_t cbOffset = 0;
3967 uint64_t cbSize = 0;
3968 rc = vmdkReadPartitionsParamsFromProvider(pProvider, &cbOffset, &cbSize);
3969 if (RT_FAILURE(rc))
3970 return rc;
3971
3972 uint64_t cbProviderOffset = 0;
3973 uint64_t cbProviderSize = 0;
3974 rc = vmdkFindPartitionParamsByName(pParentClass, pProvider->lg_name, pszProviderName, &cbProviderOffset, &cbProviderSize);
3975 if (RT_SUCCESS(rc))
3976 {
3977 *pcbAbsoluteOffset = cbOffset + cbProviderOffset;
3978 *pcbSize = cbProviderSize;
3979 return rc;
3980 }
3981 }
3982
3983 return VERR_NOT_FOUND;
3984}
3985#endif
3986
3987
3988/**
3989 * Attempts to verify the raw partition path.
3990 *
3991 * We don't want to trust RTDvm and the partition device node morphing blindly.
3992 */
3993static int vmdkRawDescVerifyPartitionPath(PVMDKIMAGE pImage, PVDISKRAWPARTDESC pPartDesc, uint32_t idxPartition,
3994 const char *pszRawDrive, RTFILE hRawDrive, uint32_t cbSector, RTDVMVOLUME hVol)
3995{
3996 RT_NOREF(pImage, pPartDesc, idxPartition, pszRawDrive, hRawDrive, cbSector, hVol);
3997
3998 /*
3999 * Try open the raw partition device.
4000 */
4001 RTFILE hRawPart = NIL_RTFILE;
4002 int rc = RTFileOpen(&hRawPart, pPartDesc->pszRawDevice, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
4003 if (RT_FAILURE(rc))
4004 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4005 N_("VMDK: Image path: '%s'. Failed to open partition #%u on '%s' via '%s' (%Rrc)"),
4006 pImage->pszFilename, idxPartition, pszRawDrive, pPartDesc->pszRawDevice, rc);
4007
4008 /*
4009 * Compare the partition UUID if we can get it.
4010 */
4011#ifdef RT_OS_WINDOWS
4012 DWORD cbReturned;
4013
4014 /* 1. Get the device numbers for both handles, they should have the same disk. */
4015 STORAGE_DEVICE_NUMBER DevNum1;
4016 RT_ZERO(DevNum1);
4017 if (!DeviceIoControl((HANDLE)RTFileToNative(hRawDrive), IOCTL_STORAGE_GET_DEVICE_NUMBER,
4018 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, &DevNum1, sizeof(DevNum1), &cbReturned, NULL /*pOverlapped*/))
4019 rc = vdIfError(pImage->pIfError, RTErrConvertFromWin32(GetLastError()), RT_SRC_POS,
4020 N_("VMDK: Image path: '%s'. IOCTL_STORAGE_GET_DEVICE_NUMBER failed on '%s': %u"),
4021 pImage->pszFilename, pszRawDrive, GetLastError());
4022
4023 STORAGE_DEVICE_NUMBER DevNum2;
4024 RT_ZERO(DevNum2);
4025 if (!DeviceIoControl((HANDLE)RTFileToNative(hRawPart), IOCTL_STORAGE_GET_DEVICE_NUMBER,
4026 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, &DevNum2, sizeof(DevNum2), &cbReturned, NULL /*pOverlapped*/))
4027 rc = vdIfError(pImage->pIfError, RTErrConvertFromWin32(GetLastError()), RT_SRC_POS,
4028 N_("VMDK: Image path: '%s'. IOCTL_STORAGE_GET_DEVICE_NUMBER failed on '%s': %u"),
4029 pImage->pszFilename, pPartDesc->pszRawDevice, GetLastError());
4030 if ( RT_SUCCESS(rc)
4031 && ( DevNum1.DeviceNumber != DevNum2.DeviceNumber
4032 || DevNum1.DeviceType != DevNum2.DeviceType))
4033 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4034 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s' (%#x != %#x || %#x != %#x)"),
4035 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4036 DevNum1.DeviceNumber, DevNum2.DeviceNumber, DevNum1.DeviceType, DevNum2.DeviceType);
4037 if (RT_SUCCESS(rc))
4038 {
4039 /* Get the partitions from the raw drive and match up with the volume info
4040 from RTDvm. The partition number is found in DevNum2. */
4041 DWORD cbNeeded = 0;
4042 if ( DeviceIoControl((HANDLE)RTFileToNative(hRawDrive), IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
4043 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, NULL, 0, &cbNeeded, NULL /*pOverlapped*/)
4044 || cbNeeded < RT_UOFFSETOF_DYN(DRIVE_LAYOUT_INFORMATION_EX, PartitionEntry[1]))
4045 cbNeeded = RT_UOFFSETOF_DYN(DRIVE_LAYOUT_INFORMATION_EX, PartitionEntry[64]);
4046 cbNeeded += sizeof(PARTITION_INFORMATION_EX) * 2; /* just in case */
4047 DRIVE_LAYOUT_INFORMATION_EX *pLayout = (DRIVE_LAYOUT_INFORMATION_EX *)RTMemTmpAllocZ(cbNeeded);
4048 if (pLayout)
4049 {
4050 cbReturned = 0;
4051 if (DeviceIoControl((HANDLE)RTFileToNative(hRawDrive), IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
4052 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, pLayout, cbNeeded, &cbReturned, NULL /*pOverlapped*/))
4053 {
4054 /* Find the entry with the given partition number (it's not an index, array contains empty MBR entries ++). */
4055 unsigned iEntry = 0;
4056 while ( iEntry < pLayout->PartitionCount
4057 && pLayout->PartitionEntry[iEntry].PartitionNumber != DevNum2.PartitionNumber)
4058 iEntry++;
4059 if (iEntry < pLayout->PartitionCount)
4060 {
4061 /* Compare the basics */
4062 PARTITION_INFORMATION_EX const * const pLayoutEntry = &pLayout->PartitionEntry[iEntry];
4063 if (pLayoutEntry->StartingOffset.QuadPart != (int64_t)pPartDesc->offStartInVDisk)
4064 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4065 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': StartingOffset %RU64, expected %RU64"),
4066 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4067 pLayoutEntry->StartingOffset.QuadPart, pPartDesc->offStartInVDisk);
4068 else if (pLayoutEntry->PartitionLength.QuadPart != (int64_t)pPartDesc->cbData)
4069 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4070 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': PartitionLength %RU64, expected %RU64"),
4071 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4072 pLayoutEntry->PartitionLength.QuadPart, pPartDesc->cbData);
4073 /** @todo We could compare the MBR type, GPT type and ID. */
4074 RT_NOREF(hVol);
4075 }
4076 else
4077 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4078 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': PartitionCount (%#x vs %#x)"),
4079 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4080 DevNum2.PartitionNumber, pLayout->PartitionCount);
4081# ifndef LOG_ENABLED
4082 if (RT_FAILURE(rc))
4083# endif
4084 {
4085 LogRel(("VMDK: Windows reports %u partitions for '%s':\n", pLayout->PartitionCount, pszRawDrive));
4086 PARTITION_INFORMATION_EX const *pEntry = &pLayout->PartitionEntry[0];
4087 for (DWORD i = 0; i < pLayout->PartitionCount; i++, pEntry++)
4088 {
4089 LogRel(("VMDK: #%u/%u: %016RU64 LB %016RU64 style=%d rewrite=%d",
4090 i, pEntry->PartitionNumber, pEntry->StartingOffset.QuadPart, pEntry->PartitionLength.QuadPart,
4091 pEntry->PartitionStyle, pEntry->RewritePartition));
4092 if (pEntry->PartitionStyle == PARTITION_STYLE_MBR)
4093 LogRel((" type=%#x boot=%d rec=%d hidden=%u\n", pEntry->Mbr.PartitionType, pEntry->Mbr.BootIndicator,
4094 pEntry->Mbr.RecognizedPartition, pEntry->Mbr.HiddenSectors));
4095 else if (pEntry->PartitionStyle == PARTITION_STYLE_GPT)
4096 LogRel((" type=%RTuuid id=%RTuuid aatrib=%RX64 name=%.36ls\n", &pEntry->Gpt.PartitionType,
4097 &pEntry->Gpt.PartitionId, pEntry->Gpt.Attributes, &pEntry->Gpt.Name[0]));
4098 else
4099 LogRel(("\n"));
4100 }
4101 LogRel(("VMDK: Looked for partition #%u (%u, '%s') at %RU64 LB %RU64\n", DevNum2.PartitionNumber,
4102 idxPartition, pPartDesc->pszRawDevice, pPartDesc->offStartInVDisk, pPartDesc->cbData));
4103 }
4104 }
4105 else
4106 rc = vdIfError(pImage->pIfError, RTErrConvertFromWin32(GetLastError()), RT_SRC_POS,
4107 N_("VMDK: Image path: '%s'. IOCTL_DISK_GET_DRIVE_LAYOUT_EX failed on '%s': %u (cb %u, cbRet %u)"),
4108 pImage->pszFilename, pPartDesc->pszRawDevice, GetLastError(), cbNeeded, cbReturned);
4109 RTMemTmpFree(pLayout);
4110 }
4111 else
4112 rc = VERR_NO_TMP_MEMORY;
4113 }
4114
4115#elif defined(RT_OS_LINUX)
4116 RT_NOREF(hVol);
4117
4118 /* Stat the two devices first to get their device numbers. (We probably
4119 could make some assumptions here about the major & minor number assignments
4120 for legacy nodes, but it doesn't hold up for nvme, so we'll skip that.) */
4121 struct stat StDrive, StPart;
4122 if (fstat((int)RTFileToNative(hRawDrive), &StDrive) != 0)
4123 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4124 N_("VMDK: Image path: '%s'. fstat failed on '%s': %d"), pImage->pszFilename, pszRawDrive, errno);
4125 else if (fstat((int)RTFileToNative(hRawPart), &StPart) != 0)
4126 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4127 N_("VMDK: Image path: '%s'. fstat failed on '%s': %d"), pImage->pszFilename, pPartDesc->pszRawDevice, errno);
4128 else
4129 {
4130 /* Scan the directories immediately under /sys/block/ for one with a
4131 'dev' file matching the drive's device number: */
4132 char szSysPath[RTPATH_MAX];
4133 rc = RTLinuxConstructPath(szSysPath, sizeof(szSysPath), "block/");
4134 AssertRCReturn(rc, rc); /* this shall not fail */
4135 if (RTDirExists(szSysPath))
4136 {
4137 rc = vmdkFindSysBlockDevPath(pImage, szSysPath, sizeof(szSysPath), StDrive.st_rdev, pszRawDrive);
4138
4139 /* Now, scan the directories under that again for a partition device
4140 matching the hRawPart device's number: */
4141 if (RT_SUCCESS(rc))
4142 rc = vmdkFindSysBlockDevPath(pImage, szSysPath, sizeof(szSysPath), StPart.st_rdev, pPartDesc->pszRawDevice);
4143
4144 /* Having found the /sys/block/device/partition/ path, we can finally
4145 read the partition attributes and compare with hVol. */
4146 if (RT_SUCCESS(rc))
4147 {
4148 /* partition number: */
4149 int64_t iLnxPartition = 0;
4150 rc = RTLinuxSysFsReadIntFile(10, &iLnxPartition, "%s/partition", szSysPath);
4151 if (RT_SUCCESS(rc) && iLnxPartition != idxPartition)
4152 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4153 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Partition number %RI64, expected %RU32"),
4154 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, iLnxPartition, idxPartition);
4155 /* else: ignore failure? */
4156
4157 /* start offset: */
4158 uint32_t const cbLnxSector = 512; /* It's hardcoded in the Linux kernel */
4159 if (RT_SUCCESS(rc))
4160 {
4161 int64_t offLnxStart = -1;
4162 rc = RTLinuxSysFsReadIntFile(10, &offLnxStart, "%s/start", szSysPath);
4163 offLnxStart *= cbLnxSector;
4164 if (RT_SUCCESS(rc) && offLnxStart != (int64_t)pPartDesc->offStartInVDisk)
4165 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4166 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Start offset %RI64, expected %RU64"),
4167 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, offLnxStart, pPartDesc->offStartInVDisk);
4168 /* else: ignore failure? */
4169 }
4170
4171 /* the size: */
4172 if (RT_SUCCESS(rc))
4173 {
4174 int64_t cbLnxData = -1;
4175 rc = RTLinuxSysFsReadIntFile(10, &cbLnxData, "%s/size", szSysPath);
4176 cbLnxData *= cbLnxSector;
4177 if (RT_SUCCESS(rc) && cbLnxData != (int64_t)pPartDesc->cbData)
4178 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4179 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Size %RI64, expected %RU64"),
4180 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbLnxData, pPartDesc->cbData);
4181 /* else: ignore failure? */
4182 }
4183 }
4184 }
4185 /* else: We've got nothing to work on, so only do content comparison. */
4186 }
4187
4188#elif defined(RT_OS_FREEBSD)
4189 char szDriveDevName[256];
4190 char* pszDevName = fdevname_r(RTFileToNative(hRawDrive), szDriveDevName, 256);
4191 if (pszDevName == NULL)
4192 rc = vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4193 N_("VMDK: Image path: '%s'. '%s' is not a drive path"), pImage->pszFilename, pszRawDrive);
4194 char szPartDevName[256];
4195 if (RT_SUCCESS(rc))
4196 {
4197 pszDevName = fdevname_r(RTFileToNative(hRawPart), szPartDevName, 256);
4198 if (pszDevName == NULL)
4199 rc = vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4200 N_("VMDK: Image path: '%s'. '%s' is not a partition path"), pImage->pszFilename, pPartDesc->pszRawDevice);
4201 }
4202 if (RT_SUCCESS(rc))
4203 {
4204 gmesh geomMesh;
4205 int err = geom_gettree(&geomMesh);
4206 if (err == 0)
4207 {
4208 /* Find root class containg partitions info */
4209 gclass* pPartClass;
4210 LIST_FOREACH(pPartClass, &geomMesh.lg_class, lg_class)
4211 {
4212 if (RTStrCmp(pPartClass->lg_name, "PART") == 0)
4213 break;
4214 }
4215 if (pPartClass == NULL || RTStrCmp(pPartClass->lg_name, "PART") != 0)
4216 rc = vdIfError(pImage->pIfError, VERR_GENERAL_FAILURE, RT_SRC_POS,
4217 N_("VMDK: Image path: '%s'. 'PART' class not found in the GEOM tree"), pImage->pszFilename);
4218
4219
4220 if (RT_SUCCESS(rc))
4221 {
4222 /* Find provider representing partition device */
4223 uint64_t cbOffset;
4224 uint64_t cbSize;
4225 rc = vmdkFindPartitionParamsByName(pPartClass, szDriveDevName, szPartDevName, &cbOffset, &cbSize);
4226 if (RT_SUCCESS(rc))
4227 {
4228 if (cbOffset != pPartDesc->offStartInVDisk)
4229 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4230 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Start offset %RU64, expected %RU64"),
4231 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbOffset, pPartDesc->offStartInVDisk);
4232 if (cbSize != pPartDesc->cbData)
4233 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4234 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Size %RU64, expected %RU64"),
4235 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbSize, pPartDesc->cbData);
4236 }
4237 else
4238 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4239 N_("VMDK: Image path: '%s'. Error getting geom provider for the partition '%s' of the drive '%s' in the GEOM tree: %Rrc"),
4240 pImage->pszFilename, pPartDesc->pszRawDevice, pszRawDrive, rc);
4241 }
4242
4243 geom_deletetree(&geomMesh);
4244 }
4245 else
4246 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(err), RT_SRC_POS,
4247 N_("VMDK: Image path: '%s'. geom_gettree failed: %d"), pImage->pszFilename, err);
4248 }
4249
4250#elif defined(RT_OS_SOLARIS)
4251 RT_NOREF(hVol);
4252
4253 dk_cinfo dkiDriveInfo;
4254 dk_cinfo dkiPartInfo;
4255 if (ioctl(RTFileToNative(hRawDrive), DKIOCINFO, (caddr_t)&dkiDriveInfo) == -1)
4256 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4257 N_("VMDK: Image path: '%s'. DKIOCINFO failed on '%s': %d"), pImage->pszFilename, pszRawDrive, errno);
4258 else if (ioctl(RTFileToNative(hRawPart), DKIOCINFO, (caddr_t)&dkiPartInfo) == -1)
4259 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4260 N_("VMDK: Image path: '%s'. DKIOCINFO failed on '%s': %d"), pImage->pszFilename, pszRawDrive, errno);
4261 else if ( dkiDriveInfo.dki_ctype != dkiPartInfo.dki_ctype
4262 || dkiDriveInfo.dki_cnum != dkiPartInfo.dki_cnum
4263 || dkiDriveInfo.dki_addr != dkiPartInfo.dki_addr
4264 || dkiDriveInfo.dki_unit != dkiPartInfo.dki_unit
4265 || dkiDriveInfo.dki_slave != dkiPartInfo.dki_slave)
4266 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4267 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s' (%#x != %#x || %#x != %#x || %#x != %#x || %#x != %#x || %#x != %#x)"),
4268 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4269 dkiDriveInfo.dki_ctype, dkiPartInfo.dki_ctype, dkiDriveInfo.dki_cnum, dkiPartInfo.dki_cnum,
4270 dkiDriveInfo.dki_addr, dkiPartInfo.dki_addr, dkiDriveInfo.dki_unit, dkiPartInfo.dki_unit,
4271 dkiDriveInfo.dki_slave, dkiPartInfo.dki_slave);
4272 else
4273 {
4274 uint64_t cbOffset = 0;
4275 uint64_t cbSize = 0;
4276 dk_gpt *pEfi = NULL;
4277 int idxEfiPart = efi_alloc_and_read(RTFileToNative(hRawPart), &pEfi);
4278 if (idxEfiPart >= 0)
4279 {
4280 if ((uint32_t)dkiPartInfo.dki_partition + 1 == idxPartition)
4281 {
4282 cbOffset = pEfi->efi_parts[idxEfiPart].p_start * pEfi->efi_lbasize;
4283 cbSize = pEfi->efi_parts[idxEfiPart].p_size * pEfi->efi_lbasize;
4284 }
4285 else
4286 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4287 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s' (%#x != %#x)"),
4288 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4289 idxPartition, (uint32_t)dkiPartInfo.dki_partition + 1);
4290 efi_free(pEfi);
4291 }
4292 else
4293 {
4294 /*
4295 * Manual says the efi_alloc_and_read returns VT_EINVAL if no EFI partition table found.
4296 * Actually, the function returns any error, e.g. VT_ERROR. Thus, we are not sure, is it
4297 * real error or just no EFI table found. Therefore, let's try to obtain partition info
4298 * using another way. If there is an error, it returns errno which will be handled below.
4299 */
4300
4301 uint32_t numPartition = (uint32_t)dkiPartInfo.dki_partition;
4302 if (numPartition > NDKMAP)
4303 numPartition -= NDKMAP;
4304 if (numPartition != idxPartition)
4305 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4306 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s' (%#x != %#x)"),
4307 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4308 idxPartition, numPartition);
4309 else
4310 {
4311 dk_minfo_ext mediaInfo;
4312 if (ioctl(RTFileToNative(hRawPart), DKIOCGMEDIAINFOEXT, (caddr_t)&mediaInfo) == -1)
4313 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4314 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s'. Can not obtain partition info: %d"),
4315 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4316 else
4317 {
4318 extpart_info extPartInfo;
4319 if (ioctl(RTFileToNative(hRawPart), DKIOCEXTPARTINFO, (caddr_t)&extPartInfo) != -1)
4320 {
4321 cbOffset = (uint64_t)extPartInfo.p_start * mediaInfo.dki_lbsize;
4322 cbSize = (uint64_t)extPartInfo.p_length * mediaInfo.dki_lbsize;
4323 }
4324 else
4325 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4326 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s'. Can not obtain partition info: %d"),
4327 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4328 }
4329 }
4330 }
4331 if (RT_SUCCESS(rc) && cbOffset != pPartDesc->offStartInVDisk)
4332 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4333 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Start offset %RI64, expected %RU64"),
4334 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbOffset, pPartDesc->offStartInVDisk);
4335
4336 if (RT_SUCCESS(rc) && cbSize != pPartDesc->cbData)
4337 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4338 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Size %RI64, expected %RU64"),
4339 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbSize, pPartDesc->cbData);
4340 }
4341
4342#elif defined(RT_OS_DARWIN)
4343 /* Stat the drive get its device number. */
4344 struct stat StDrive;
4345 if (fstat((int)RTFileToNative(hRawDrive), &StDrive) != 0)
4346 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4347 N_("VMDK: Image path: '%s'. fstat failed on '%s' (errno=%d)"), pImage->pszFilename, pszRawDrive, errno);
4348 else
4349 {
4350 if (ioctl(RTFileToNative(hRawPart), DKIOCLOCKPHYSICALEXTENTS, NULL) == -1)
4351 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4352 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to lock the partition (errno=%d)"),
4353 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4354 else
4355 {
4356 uint32_t cbBlockSize = 0;
4357 uint64_t cbOffset = 0;
4358 uint64_t cbSize = 0;
4359 if (ioctl(RTFileToNative(hRawPart), DKIOCGETBLOCKSIZE, (caddr_t)&cbBlockSize) == -1)
4360 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4361 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to obtain the sector size of the partition (errno=%d)"),
4362 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4363 else if (ioctl(RTFileToNative(hRawPart), DKIOCGETBASE, (caddr_t)&cbOffset) == -1)
4364 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4365 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to obtain the start offset of the partition (errno=%d)"),
4366 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4367 else if (ioctl(RTFileToNative(hRawPart), DKIOCGETBLOCKCOUNT, (caddr_t)&cbSize) == -1)
4368 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4369 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to obtain the size of the partition (errno=%d)"),
4370 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4371 else
4372 {
4373 cbSize *= (uint64_t)cbBlockSize;
4374 dk_physical_extent_t dkPartExtent = {0};
4375 dkPartExtent.offset = 0;
4376 dkPartExtent.length = cbSize;
4377 if (ioctl(RTFileToNative(hRawPart), DKIOCGETPHYSICALEXTENT, (caddr_t)&dkPartExtent) == -1)
4378 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4379 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to obtain partition info (errno=%d)"),
4380 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4381 else
4382 {
4383 if (dkPartExtent.dev != StDrive.st_rdev)
4384 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4385 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Drive does not contain the partition"),
4386 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive);
4387 else if (cbOffset != pPartDesc->offStartInVDisk)
4388 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4389 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Start offset %RU64, expected %RU64"),
4390 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbOffset, pPartDesc->offStartInVDisk);
4391 else if (cbSize != pPartDesc->cbData)
4392 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4393 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Size %RU64, expected %RU64"),
4394 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbSize, pPartDesc->cbData);
4395 }
4396 }
4397
4398 if (ioctl(RTFileToNative(hRawPart), DKIOCUNLOCKPHYSICALEXTENTS, NULL) == -1)
4399 {
4400 int rc2 = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4401 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to unlock the partition (errno=%d)"),
4402 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4403 if (RT_SUCCESS(rc))
4404 rc = rc2;
4405 }
4406 }
4407 }
4408
4409#else
4410 RT_NOREF(hVol); /* PORTME */
4411 rc = VERR_NOT_SUPPORTED;
4412#endif
4413 if (RT_SUCCESS(rc))
4414 {
4415 /*
4416 * Compare the first 32 sectors of the partition.
4417 *
4418 * This might not be conclusive, but for partitions formatted with the more
4419 * common file systems it should be as they have a superblock copy at or near
4420 * the start of the partition (fat, fat32, ntfs, and ext4 does at least).
4421 */
4422 size_t const cbToCompare = (size_t)RT_MIN(pPartDesc->cbData / cbSector, 32) * cbSector;
4423 uint8_t *pbSector1 = (uint8_t *)RTMemTmpAlloc(cbToCompare * 2);
4424 if (pbSector1 != NULL)
4425 {
4426 uint8_t *pbSector2 = pbSector1 + cbToCompare;
4427
4428 /* Do the comparing, we repeat if it fails and the data might be volatile. */
4429 uint64_t uPrevCrc1 = 0;
4430 uint64_t uPrevCrc2 = 0;
4431 uint32_t cStable = 0;
4432 for (unsigned iTry = 0; iTry < 256; iTry++)
4433 {
4434 rc = RTFileReadAt(hRawDrive, pPartDesc->offStartInVDisk, pbSector1, cbToCompare, NULL);
4435 if (RT_SUCCESS(rc))
4436 {
4437 rc = RTFileReadAt(hRawPart, pPartDesc->offStartInDevice, pbSector2, cbToCompare, NULL);
4438 if (RT_SUCCESS(rc))
4439 {
4440 if (memcmp(pbSector1, pbSector2, cbToCompare) != 0)
4441 {
4442 rc = VERR_MISMATCH;
4443
4444 /* Do data stability checks before repeating: */
4445 uint64_t const uCrc1 = RTCrc64(pbSector1, cbToCompare);
4446 uint64_t const uCrc2 = RTCrc64(pbSector2, cbToCompare);
4447 if ( uPrevCrc1 != uCrc1
4448 || uPrevCrc2 != uCrc2)
4449 cStable = 0;
4450 else if (++cStable > 4)
4451 break;
4452 uPrevCrc1 = uCrc1;
4453 uPrevCrc2 = uCrc2;
4454 continue;
4455 }
4456 rc = VINF_SUCCESS;
4457 }
4458 else
4459 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4460 N_("VMDK: Image path: '%s'. Error reading %zu bytes from '%s' at offset %RU64 (%Rrc)"),
4461 pImage->pszFilename, cbToCompare, pPartDesc->pszRawDevice, pPartDesc->offStartInDevice, rc);
4462 }
4463 else
4464 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4465 N_("VMDK: Image path: '%s'. Error reading %zu bytes from '%s' at offset %RU64 (%Rrc)"),
4466 pImage->pszFilename, cbToCompare, pszRawDrive, pPartDesc->offStartInVDisk, rc);
4467 break;
4468 }
4469 if (rc == VERR_MISMATCH)
4470 {
4471 /* Find the first mismatching bytes: */
4472 size_t offMissmatch = 0;
4473 while (offMissmatch < cbToCompare && pbSector1[offMissmatch] == pbSector2[offMissmatch])
4474 offMissmatch++;
4475 int cbSample = (int)RT_MIN(cbToCompare - offMissmatch, 16);
4476
4477 if (cStable > 0)
4478 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4479 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s' (cStable=%d @%#zx: %.*Rhxs vs %.*Rhxs)"),
4480 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cStable,
4481 offMissmatch, cbSample, &pbSector1[offMissmatch], cbSample, &pbSector2[offMissmatch]);
4482 else
4483 {
4484 LogRel(("VMDK: Image path: '%s'. Partition #%u path ('%s') verification undecided on '%s' because of unstable data! (@%#zx: %.*Rhxs vs %.*Rhxs)\n",
4485 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4486 offMissmatch, cbSample, &pbSector1[offMissmatch], cbSample, &pbSector2[offMissmatch]));
4487 rc = -rc;
4488 }
4489 }
4490
4491 RTMemTmpFree(pbSector1);
4492 }
4493 else
4494 rc = vdIfError(pImage->pIfError, VERR_NO_TMP_MEMORY, RT_SRC_POS,
4495 N_("VMDK: Image path: '%s'. Failed to allocate %zu bytes for a temporary read buffer\n"),
4496 pImage->pszFilename, cbToCompare * 2);
4497 }
4498 RTFileClose(hRawPart);
4499 return rc;
4500}
4501
4502#ifdef RT_OS_WINDOWS
4503/**
4504 * Construct the device name for the given partition number.
4505 */
4506static int vmdkRawDescWinMakePartitionName(PVMDKIMAGE pImage, const char *pszRawDrive, RTFILE hRawDrive, uint32_t idxPartition,
4507 char **ppszRawPartition)
4508{
4509 int rc = VINF_SUCCESS;
4510 DWORD cbReturned = 0;
4511 STORAGE_DEVICE_NUMBER DevNum;
4512 RT_ZERO(DevNum);
4513 if (DeviceIoControl((HANDLE)RTFileToNative(hRawDrive), IOCTL_STORAGE_GET_DEVICE_NUMBER,
4514 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, &DevNum, sizeof(DevNum), &cbReturned, NULL /*pOverlapped*/))
4515 RTStrAPrintf(ppszRawPartition, "\\\\.\\Harddisk%uPartition%u", DevNum.DeviceNumber, idxPartition);
4516 else
4517 rc = vdIfError(pImage->pIfError, RTErrConvertFromWin32(GetLastError()), RT_SRC_POS,
4518 N_("VMDK: Image path: '%s'. IOCTL_STORAGE_GET_DEVICE_NUMBER failed on '%s': %u"),
4519 pImage->pszFilename, pszRawDrive, GetLastError());
4520 return rc;
4521}
4522#endif /* RT_OS_WINDOWS */
4523
4524/**
4525 * Worker for vmdkMakeRawDescriptor that adds partition descriptors when the
4526 * 'Partitions' configuration value is present.
4527 *
4528 * @returns VBox status code, error message has been set on failure.
4529 *
4530 * @note Caller is assumed to clean up @a pRawDesc and release
4531 * @a *phVolToRelease.
4532 * @internal
4533 */
4534static int vmdkRawDescDoPartitions(PVMDKIMAGE pImage, RTDVM hVolMgr, PVDISKRAW pRawDesc,
4535 RTFILE hRawDrive, const char *pszRawDrive, uint32_t cbSector,
4536 uint32_t fPartitions, uint32_t fPartitionsReadOnly, bool fRelative,
4537 PRTDVMVOLUME phVolToRelease)
4538{
4539 *phVolToRelease = NIL_RTDVMVOLUME;
4540
4541 /* Check sanity/understanding. */
4542 Assert(fPartitions);
4543 Assert((fPartitions & fPartitionsReadOnly) == fPartitionsReadOnly); /* RO should be a sub-set */
4544
4545 /*
4546 * Allocate on descriptor for each volume up front.
4547 */
4548 uint32_t const cVolumes = RTDvmMapGetValidVolumes(hVolMgr);
4549
4550 PVDISKRAWPARTDESC paPartDescs = NULL;
4551 int rc = vmdkRawDescAppendPartDesc(pImage, pRawDesc, cVolumes, &paPartDescs);
4552 AssertRCReturn(rc, rc);
4553
4554 /*
4555 * Enumerate the partitions (volumes) on the disk and create descriptors for each of them.
4556 */
4557 uint32_t fPartitionsLeft = fPartitions;
4558 RTDVMVOLUME hVol = NIL_RTDVMVOLUME; /* the current volume, needed for getting the next. */
4559 for (uint32_t i = 0; i < cVolumes; i++)
4560 {
4561 /*
4562 * Get the next/first volume and release the current.
4563 */
4564 RTDVMVOLUME hVolNext = NIL_RTDVMVOLUME;
4565 if (i == 0)
4566 rc = RTDvmMapQueryFirstVolume(hVolMgr, &hVolNext);
4567 else
4568 rc = RTDvmMapQueryNextVolume(hVolMgr, hVol, &hVolNext);
4569 if (RT_FAILURE(rc))
4570 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4571 N_("VMDK: Image path: '%s'. Volume enumeration failed at volume #%u on '%s' (%Rrc)"),
4572 pImage->pszFilename, i, pszRawDrive, rc);
4573 uint32_t cRefs = RTDvmVolumeRelease(hVol);
4574 Assert(cRefs != UINT32_MAX); RT_NOREF(cRefs);
4575 *phVolToRelease = hVol = hVolNext;
4576
4577 /*
4578 * Depending on the fPartitions selector and associated read-only mask,
4579 * the guest either gets read-write or read-only access (bits set)
4580 * or no access (selector bit clear, access directed to the VMDK).
4581 */
4582 paPartDescs[i].cbData = RTDvmVolumeGetSize(hVol);
4583
4584 uint64_t offVolumeEndIgnored = 0;
4585 rc = RTDvmVolumeQueryRange(hVol, &paPartDescs[i].offStartInVDisk, &offVolumeEndIgnored);
4586 if (RT_FAILURE(rc))
4587 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4588 N_("VMDK: Image path: '%s'. Failed to get location of volume #%u on '%s' (%Rrc)"),
4589 pImage->pszFilename, i, pszRawDrive, rc);
4590 Assert(paPartDescs[i].cbData == offVolumeEndIgnored + 1 - paPartDescs[i].offStartInVDisk);
4591
4592 /* Note! The index must match IHostDrivePartition::number. */
4593 uint32_t idxPartition = RTDvmVolumeGetIndex(hVol, RTDVMVOLIDX_HOST);
4594 if ( idxPartition < 32
4595 && (fPartitions & RT_BIT_32(idxPartition)))
4596 {
4597 fPartitionsLeft &= ~RT_BIT_32(idxPartition);
4598 if (fPartitionsReadOnly & RT_BIT_32(idxPartition))
4599 paPartDescs[i].uFlags |= VDISKRAW_READONLY;
4600
4601 if (!fRelative)
4602 {
4603 /*
4604 * Accessing the drive thru the main device node (pRawDesc->pszRawDisk).
4605 */
4606 paPartDescs[i].offStartInDevice = paPartDescs[i].offStartInVDisk;
4607 paPartDescs[i].pszRawDevice = RTStrDup(pszRawDrive);
4608 AssertPtrReturn(paPartDescs[i].pszRawDevice, VERR_NO_STR_MEMORY);
4609 }
4610 else
4611 {
4612 /*
4613 * Relative means access the partition data via the device node for that
4614 * partition, allowing the sysadmin/OS to allow a user access to individual
4615 * partitions without necessarily being able to compromise the host OS.
4616 * Obviously, the creation of the VMDK requires read access to the main
4617 * device node for the drive, but that's a one-time thing and can be done
4618 * by the sysadmin. Here data starts at offset zero in the device node.
4619 */
4620 paPartDescs[i].offStartInDevice = 0;
4621
4622#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
4623 /* /dev/rdisk1 -> /dev/rdisk1s2 (s=slice) */
4624 RTStrAPrintf(&paPartDescs[i].pszRawDevice, "%ss%u", pszRawDrive, idxPartition);
4625#elif defined(RT_OS_LINUX)
4626 /* Two naming schemes here: /dev/nvme0n1 -> /dev/nvme0n1p1; /dev/sda -> /dev/sda1 */
4627 RTStrAPrintf(&paPartDescs[i].pszRawDevice,
4628 RT_C_IS_DIGIT(pszRawDrive[strlen(pszRawDrive) - 1]) ? "%sp%u" : "%s%u", pszRawDrive, idxPartition);
4629#elif defined(RT_OS_WINDOWS)
4630 rc = vmdkRawDescWinMakePartitionName(pImage, pszRawDrive, hRawDrive, idxPartition, &paPartDescs[i].pszRawDevice);
4631 AssertRCReturn(rc, rc);
4632#elif defined(RT_OS_SOLARIS)
4633 if (pRawDesc->enmPartitioningType == VDISKPARTTYPE_MBR)
4634 {
4635 /*
4636 * MBR partitions have device nodes in form /dev/(r)dsk/cXtYdZpK
4637 * where X is the controller,
4638 * Y is target (SCSI device number),
4639 * Z is disk number,
4640 * K is partition number,
4641 * where p0 is the whole disk
4642 * p1-pN are the partitions of the disk
4643 */
4644 const char *pszRawDrivePath = pszRawDrive;
4645 char szDrivePath[RTPATH_MAX];
4646 size_t cbRawDrive = strlen(pszRawDrive);
4647 if ( cbRawDrive > 1 && strcmp(&pszRawDrive[cbRawDrive - 2], "p0") == 0)
4648 {
4649 memcpy(szDrivePath, pszRawDrive, cbRawDrive - 2);
4650 szDrivePath[cbRawDrive - 2] = '\0';
4651 pszRawDrivePath = szDrivePath;
4652 }
4653 RTStrAPrintf(&paPartDescs[i].pszRawDevice, "%sp%u", pszRawDrivePath, idxPartition);
4654 }
4655 else /* GPT */
4656 {
4657 /*
4658 * GPT partitions have device nodes in form /dev/(r)dsk/cXtYdZsK
4659 * where X is the controller,
4660 * Y is target (SCSI device number),
4661 * Z is disk number,
4662 * K is partition number, zero based. Can be only from 0 to 6.
4663 * Thus, only partitions numbered 0 through 6 have device nodes.
4664 */
4665 if (idxPartition > 7)
4666 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4667 N_("VMDK: Image path: '%s'. the partition #%u on '%s' has no device node and can not be specified with 'Relative' property"),
4668 pImage->pszFilename, idxPartition, pszRawDrive);
4669 RTStrAPrintf(&paPartDescs[i].pszRawDevice, "%ss%u", pszRawDrive, idxPartition - 1);
4670 }
4671#else
4672 AssertFailedReturn(VERR_INTERNAL_ERROR_4); /* The option parsing code should have prevented this - PORTME */
4673#endif
4674 AssertPtrReturn(paPartDescs[i].pszRawDevice, VERR_NO_STR_MEMORY);
4675
4676 rc = vmdkRawDescVerifyPartitionPath(pImage, &paPartDescs[i], idxPartition, pszRawDrive, hRawDrive, cbSector, hVol);
4677 AssertRCReturn(rc, rc);
4678 }
4679 }
4680 else
4681 {
4682 /* Not accessible to the guest. */
4683 paPartDescs[i].offStartInDevice = 0;
4684 paPartDescs[i].pszRawDevice = NULL;
4685 }
4686 } /* for each volume */
4687
4688 RTDvmVolumeRelease(hVol);
4689 *phVolToRelease = NIL_RTDVMVOLUME;
4690
4691 /*
4692 * Check that we found all the partitions the user selected.
4693 */
4694 if (fPartitionsLeft)
4695 {
4696 char szLeft[3 * sizeof(fPartitions) * 8];
4697 size_t cchLeft = 0;
4698 for (unsigned i = 0; i < sizeof(fPartitions) * 8; i++)
4699 if (fPartitionsLeft & RT_BIT_32(i))
4700 cchLeft += RTStrPrintf(&szLeft[cchLeft], sizeof(szLeft) - cchLeft, cchLeft ? "%u" : ",%u", i);
4701 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4702 N_("VMDK: Image path: '%s'. Not all the specified partitions for drive '%s' was found: %s"),
4703 pImage->pszFilename, pszRawDrive, szLeft);
4704 }
4705
4706 return VINF_SUCCESS;
4707}
4708
4709/**
4710 * Worker for vmdkMakeRawDescriptor that adds partition descriptors with copies
4711 * of the partition tables and associated padding areas when the 'Partitions'
4712 * configuration value is present.
4713 *
4714 * The guest is not allowed access to the partition tables, however it needs
4715 * them to be able to access the drive. So, create descriptors for each of the
4716 * tables and attach the current disk content. vmdkCreateRawImage() will later
4717 * write the content to the VMDK. Any changes the guest later makes to the
4718 * partition tables will then go to the VMDK copy, rather than the host drive.
4719 *
4720 * @returns VBox status code, error message has been set on failure.
4721 *
4722 * @note Caller is assumed to clean up @a pRawDesc
4723 * @internal
4724 */
4725static int vmdkRawDescDoCopyPartitionTables(PVMDKIMAGE pImage, RTDVM hVolMgr, PVDISKRAW pRawDesc,
4726 const char *pszRawDrive, RTFILE hRawDrive, void *pvBootSector, size_t cbBootSector)
4727{
4728 /*
4729 * Query the locations.
4730 */
4731 /* Determin how many locations there are: */
4732 size_t cLocations = 0;
4733 int rc = RTDvmMapQueryTableLocations(hVolMgr, RTDVMMAPQTABLOC_F_INCLUDE_LEGACY, NULL, 0, &cLocations);
4734 if (rc != VERR_BUFFER_OVERFLOW)
4735 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4736 N_("VMDK: Image path: '%s'. RTDvmMapQueryTableLocations failed on '%s' (%Rrc)"),
4737 pImage->pszFilename, pszRawDrive, rc);
4738 AssertReturn(cLocations > 0 && cLocations < _16M, VERR_INTERNAL_ERROR_5);
4739
4740 /* We can allocate the partition descriptors here to save an intentation level. */
4741 PVDISKRAWPARTDESC paPartDescs = NULL;
4742 rc = vmdkRawDescAppendPartDesc(pImage, pRawDesc, (uint32_t)cLocations, &paPartDescs);
4743 AssertRCReturn(rc, rc);
4744
4745 /* Allocate the result table and repeat the location table query: */
4746 PRTDVMTABLELOCATION paLocations = (PRTDVMTABLELOCATION)RTMemAllocZ(sizeof(paLocations[0]) * cLocations);
4747 if (!paLocations)
4748 return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("VMDK: Image path: '%s'. Failed to allocate %zu bytes"),
4749 pImage->pszFilename, sizeof(paLocations[0]) * cLocations);
4750 rc = RTDvmMapQueryTableLocations(hVolMgr, RTDVMMAPQTABLOC_F_INCLUDE_LEGACY, paLocations, cLocations, NULL);
4751 if (RT_SUCCESS(rc))
4752 {
4753 /*
4754 * Translate them into descriptors.
4755 *
4756 * We restrict the amount of partition alignment padding to 4MiB as more
4757 * will just be a waste of space. The use case for including the padding
4758 * are older boot loaders and boot manager (including one by a team member)
4759 * that put data and code in the 62 sectors between the MBR and the first
4760 * partition (total of 63). Later CHS was abandond and partition started
4761 * being aligned on power of two sector boundraries (typically 64KiB or
4762 * 1MiB depending on the media size).
4763 */
4764 for (size_t i = 0; i < cLocations && RT_SUCCESS(rc); i++)
4765 {
4766 Assert(paLocations[i].cb > 0);
4767 if (paLocations[i].cb <= _64M)
4768 {
4769 /* Create the partition descriptor entry: */
4770 //paPartDescs[i].pszRawDevice = NULL;
4771 //paPartDescs[i].offStartInDevice = 0;
4772 //paPartDescs[i].uFlags = 0;
4773 paPartDescs[i].offStartInVDisk = paLocations[i].off;
4774 paPartDescs[i].cbData = paLocations[i].cb;
4775 if (paPartDescs[i].cbData < _4M)
4776 paPartDescs[i].cbData = RT_MIN(paPartDescs[i].cbData + paLocations[i].cbPadding, _4M);
4777 paPartDescs[i].pvPartitionData = RTMemAllocZ((size_t)paPartDescs[i].cbData);
4778 if (paPartDescs[i].pvPartitionData)
4779 {
4780 /* Read the content from the drive: */
4781 rc = RTFileReadAt(hRawDrive, paPartDescs[i].offStartInVDisk, paPartDescs[i].pvPartitionData,
4782 (size_t)paPartDescs[i].cbData, NULL);
4783 if (RT_SUCCESS(rc))
4784 {
4785 /* Do we have custom boot sector code? */
4786 if (pvBootSector && cbBootSector && paPartDescs[i].offStartInVDisk == 0)
4787 {
4788 /* Note! Old code used to quietly drop the bootsector if it was considered too big.
4789 Instead we fail as we weren't able to do what the user requested us to do.
4790 Better if the user knows than starts questioning why the guest isn't
4791 booting as expected. */
4792 if (cbBootSector <= paPartDescs[i].cbData)
4793 memcpy(paPartDescs[i].pvPartitionData, pvBootSector, cbBootSector);
4794 else
4795 rc = vdIfError(pImage->pIfError, VERR_TOO_MUCH_DATA, RT_SRC_POS,
4796 N_("VMDK: Image path: '%s'. The custom boot sector is too big: %zu bytes, %RU64 bytes available"),
4797 pImage->pszFilename, cbBootSector, paPartDescs[i].cbData);
4798 }
4799 }
4800 else
4801 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4802 N_("VMDK: Image path: '%s'. Failed to read partition at off %RU64 length %zu from '%s' (%Rrc)"),
4803 pImage->pszFilename, paPartDescs[i].offStartInVDisk,
4804 (size_t)paPartDescs[i].cbData, pszRawDrive, rc);
4805 }
4806 else
4807 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4808 N_("VMDK: Image path: '%s'. Failed to allocate %zu bytes for copying the partition table at off %RU64"),
4809 pImage->pszFilename, (size_t)paPartDescs[i].cbData, paPartDescs[i].offStartInVDisk);
4810 }
4811 else
4812 rc = vdIfError(pImage->pIfError, VERR_TOO_MUCH_DATA, RT_SRC_POS,
4813 N_("VMDK: Image path: '%s'. Partition table #%u at offset %RU64 in '%s' is to big: %RU64 bytes"),
4814 pImage->pszFilename, i, paLocations[i].off, pszRawDrive, paLocations[i].cb);
4815 }
4816 }
4817 else
4818 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4819 N_("VMDK: Image path: '%s'. RTDvmMapQueryTableLocations failed on '%s' (%Rrc)"),
4820 pImage->pszFilename, pszRawDrive, rc);
4821 RTMemFree(paLocations);
4822 return rc;
4823}
4824
4825/**
4826 * Opens the volume manager for the raw drive when in selected-partition mode.
4827 *
4828 * @param pImage The VMDK image (for errors).
4829 * @param hRawDrive The raw drive handle.
4830 * @param pszRawDrive The raw drive device path (for errors).
4831 * @param cbSector The sector size.
4832 * @param phVolMgr Where to return the handle to the volume manager on
4833 * success.
4834 * @returns VBox status code, errors have been reported.
4835 * @internal
4836 */
4837static int vmdkRawDescOpenVolMgr(PVMDKIMAGE pImage, RTFILE hRawDrive, const char *pszRawDrive, uint32_t cbSector, PRTDVM phVolMgr)
4838{
4839 *phVolMgr = NIL_RTDVM;
4840
4841 RTVFSFILE hVfsFile = NIL_RTVFSFILE;
4842 int rc = RTVfsFileFromRTFile(hRawDrive, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE, true /*fLeaveOpen*/, &hVfsFile);
4843 if (RT_FAILURE(rc))
4844 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4845 N_("VMDK: Image path: '%s'. RTVfsFileFromRTFile failed for '%s' handle (%Rrc)"),
4846 pImage->pszFilename, pszRawDrive, rc);
4847
4848 RTDVM hVolMgr = NIL_RTDVM;
4849 rc = RTDvmCreate(&hVolMgr, hVfsFile, cbSector, 0 /*fFlags*/);
4850
4851 RTVfsFileRelease(hVfsFile);
4852
4853 if (RT_FAILURE(rc))
4854 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4855 N_("VMDK: Image path: '%s'. Failed to create volume manager instance for '%s' (%Rrc)"),
4856 pImage->pszFilename, pszRawDrive, rc);
4857
4858 rc = RTDvmMapOpen(hVolMgr);
4859 if (RT_SUCCESS(rc))
4860 {
4861 *phVolMgr = hVolMgr;
4862 return VINF_SUCCESS;
4863 }
4864 RTDvmRelease(hVolMgr);
4865 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: Image path: '%s'. RTDvmMapOpen failed for '%s' (%Rrc)"),
4866 pImage->pszFilename, pszRawDrive, rc);
4867}
4868
4869/**
4870 * Opens the raw drive device and get the sizes for it.
4871 *
4872 * @param pImage The image (for error reporting).
4873 * @param pszRawDrive The device/whatever to open.
4874 * @param phRawDrive Where to return the file handle.
4875 * @param pcbRawDrive Where to return the size.
4876 * @param pcbSector Where to return the sector size.
4877 * @returns IPRT status code, errors have been reported.
4878 * @internal
4879 */
4880static int vmkdRawDescOpenDevice(PVMDKIMAGE pImage, const char *pszRawDrive,
4881 PRTFILE phRawDrive, uint64_t *pcbRawDrive, uint32_t *pcbSector)
4882{
4883 /*
4884 * Open the device for the raw drive.
4885 */
4886 RTFILE hRawDrive = NIL_RTFILE;
4887 int rc = RTFileOpen(&hRawDrive, pszRawDrive, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
4888 if (RT_FAILURE(rc))
4889 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4890 N_("VMDK: Image path: '%s'. Failed to open the raw drive '%s' for reading (%Rrc)"),
4891 pImage->pszFilename, pszRawDrive, rc);
4892
4893 /*
4894 * Get the sector size.
4895 */
4896 uint32_t cbSector = 0;
4897 rc = RTFileQuerySectorSize(hRawDrive, &cbSector);
4898 if (RT_SUCCESS(rc))
4899 {
4900 /* sanity checks */
4901 if ( cbSector >= 512
4902 && cbSector <= _64K
4903 && RT_IS_POWER_OF_TWO(cbSector))
4904 {
4905 /*
4906 * Get the size.
4907 */
4908 uint64_t cbRawDrive = 0;
4909 rc = RTFileQuerySize(hRawDrive, &cbRawDrive);
4910 if (RT_SUCCESS(rc))
4911 {
4912 /* Check whether cbSize is actually sensible. */
4913 if (cbRawDrive > cbSector && (cbRawDrive % cbSector) == 0)
4914 {
4915 *phRawDrive = hRawDrive;
4916 *pcbRawDrive = cbRawDrive;
4917 *pcbSector = cbSector;
4918 return VINF_SUCCESS;
4919 }
4920 rc = vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4921 N_("VMDK: Image path: '%s'. Got a bogus size for the raw drive '%s': %RU64 (sector size %u)"),
4922 pImage->pszFilename, pszRawDrive, cbRawDrive, cbSector);
4923 }
4924 else
4925 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4926 N_("VMDK: Image path: '%s'. Failed to query size of the drive '%s' (%Rrc)"),
4927 pImage->pszFilename, pszRawDrive, rc);
4928 }
4929 else
4930 rc = vdIfError(pImage->pIfError, VERR_OUT_OF_RANGE, RT_SRC_POS,
4931 N_("VMDK: Image path: '%s'. Unsupported sector size for '%s': %u (%#x)"),
4932 pImage->pszFilename, pszRawDrive, cbSector, cbSector);
4933 }
4934 else
4935 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4936 N_("VMDK: Image path: '%s'. Failed to get the sector size for '%s' (%Rrc)"),
4937 pImage->pszFilename, pszRawDrive, rc);
4938 RTFileClose(hRawDrive);
4939 return rc;
4940}
4941
4942/**
4943 * Reads the raw disk configuration, leaving initalization and cleanup to the
4944 * caller (regardless of return status).
4945 *
4946 * @returns VBox status code, errors properly reported.
4947 * @internal
4948 */
4949static int vmdkRawDescParseConfig(PVMDKIMAGE pImage, char **ppszRawDrive,
4950 uint32_t *pfPartitions, uint32_t *pfPartitionsReadOnly,
4951 void **ppvBootSector, size_t *pcbBootSector, bool *pfRelative,
4952 char **ppszFreeMe)
4953{
4954 PVDINTERFACECONFIG pImgCfg = VDIfConfigGet(pImage->pVDIfsImage);
4955 if (!pImgCfg)
4956 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4957 N_("VMDK: Image path: '%s'. Getting config interface failed"), pImage->pszFilename);
4958
4959 /*
4960 * RawDrive = path
4961 */
4962 int rc = VDCFGQueryStringAlloc(pImgCfg, "RawDrive", ppszRawDrive);
4963 if (RT_FAILURE(rc))
4964 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4965 N_("VMDK: Image path: '%s'. Getting 'RawDrive' configuration failed (%Rrc)"), pImage->pszFilename, rc);
4966 AssertPtrReturn(*ppszRawDrive, VERR_INTERNAL_ERROR_3);
4967
4968 /*
4969 * Partitions=n[r][,...]
4970 */
4971 uint32_t const cMaxPartitionBits = sizeof(*pfPartitions) * 8 /* ASSUMES 8 bits per char */;
4972 *pfPartitions = *pfPartitionsReadOnly = 0;
4973
4974 rc = VDCFGQueryStringAlloc(pImgCfg, "Partitions", ppszFreeMe);
4975 if (RT_SUCCESS(rc))
4976 {
4977 char *psz = *ppszFreeMe;
4978 while (*psz != '\0')
4979 {
4980 char *pszNext;
4981 uint32_t u32;
4982 rc = RTStrToUInt32Ex(psz, &pszNext, 0, &u32);
4983 if (rc == VWRN_NUMBER_TOO_BIG || rc == VWRN_NEGATIVE_UNSIGNED)
4984 rc = -rc;
4985 if (RT_FAILURE(rc))
4986 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4987 N_("VMDK: Image path: '%s'. Parsing 'Partitions' config value failed. Incorrect value (%Rrc): %s"),
4988 pImage->pszFilename, rc, psz);
4989 if (u32 >= cMaxPartitionBits)
4990 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4991 N_("VMDK: Image path: '%s'. 'Partitions' config sub-value out of range: %RU32, max %RU32"),
4992 pImage->pszFilename, u32, cMaxPartitionBits);
4993 *pfPartitions |= RT_BIT_32(u32);
4994 psz = pszNext;
4995 if (*psz == 'r')
4996 {
4997 *pfPartitionsReadOnly |= RT_BIT_32(u32);
4998 psz++;
4999 }
5000 if (*psz == ',')
5001 psz++;
5002 else if (*psz != '\0')
5003 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
5004 N_("VMDK: Image path: '%s'. Malformed 'Partitions' config value, expected separator: %s"),
5005 pImage->pszFilename, psz);
5006 }
5007
5008 RTStrFree(*ppszFreeMe);
5009 *ppszFreeMe = NULL;
5010 }
5011 else if (rc != VERR_CFGM_VALUE_NOT_FOUND)
5012 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
5013 N_("VMDK: Image path: '%s'. Getting 'Partitions' configuration failed (%Rrc)"), pImage->pszFilename, rc);
5014
5015 /*
5016 * BootSector=base64
5017 */
5018 rc = VDCFGQueryStringAlloc(pImgCfg, "BootSector", ppszFreeMe);
5019 if (RT_SUCCESS(rc))
5020 {
5021 ssize_t cbBootSector = RTBase64DecodedSize(*ppszFreeMe, NULL);
5022 if (cbBootSector < 0)
5023 return vdIfError(pImage->pIfError, VERR_INVALID_BASE64_ENCODING, RT_SRC_POS,
5024 N_("VMDK: Image path: '%s'. BASE64 decoding failed on the custom bootsector for '%s'"),
5025 pImage->pszFilename, *ppszRawDrive);
5026 if (cbBootSector == 0)
5027 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
5028 N_("VMDK: Image path: '%s'. Custom bootsector for '%s' is zero bytes big"),
5029 pImage->pszFilename, *ppszRawDrive);
5030 if (cbBootSector > _4M) /* this is just a preliminary max */
5031 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
5032 N_("VMDK: Image path: '%s'. Custom bootsector for '%s' is way too big: %zu bytes, max 4MB"),
5033 pImage->pszFilename, *ppszRawDrive, cbBootSector);
5034
5035 /* Refuse the boot sector if whole-drive. This used to be done quietly,
5036 however, bird disagrees and thinks the user should be told that what
5037 he/she/it tries to do isn't possible. There should be less head
5038 scratching this way when the guest doesn't do the expected thing. */
5039 if (!*pfPartitions)
5040 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
5041 N_("VMDK: Image path: '%s'. Custom bootsector for '%s' is not supported for whole-drive configurations, only when selecting partitions"),
5042 pImage->pszFilename, *ppszRawDrive);
5043
5044 *pcbBootSector = (size_t)cbBootSector;
5045 *ppvBootSector = RTMemAlloc((size_t)cbBootSector);
5046 if (!*ppvBootSector)
5047 return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
5048 N_("VMDK: Image path: '%s'. Failed to allocate %zd bytes for the custom bootsector for '%s'"),
5049 pImage->pszFilename, cbBootSector, *ppszRawDrive);
5050
5051 rc = RTBase64Decode(*ppszFreeMe, *ppvBootSector, cbBootSector, NULL /*pcbActual*/, NULL /*ppszEnd*/);
5052 if (RT_FAILURE(rc))
5053 return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
5054 N_("VMDK: Image path: '%s'. Base64 decoding of the custom boot sector for '%s' failed (%Rrc)"),
5055 pImage->pszFilename, *ppszRawDrive, rc);
5056
5057 RTStrFree(*ppszFreeMe);
5058 *ppszFreeMe = NULL;
5059 }
5060 else if (rc != VERR_CFGM_VALUE_NOT_FOUND)
5061 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
5062 N_("VMDK: Image path: '%s'. Getting 'BootSector' configuration failed (%Rrc)"), pImage->pszFilename, rc);
5063
5064 /*
5065 * Relative=0/1
5066 */
5067 *pfRelative = false;
5068 rc = VDCFGQueryBool(pImgCfg, "Relative", pfRelative);
5069 if (RT_SUCCESS(rc))
5070 {
5071 if (!*pfPartitions && *pfRelative != false)
5072 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
5073 N_("VMDK: Image path: '%s'. The 'Relative' option is not supported for whole-drive configurations, only when selecting partitions"),
5074 pImage->pszFilename);
5075#if !defined(RT_OS_DARWIN) && !defined(RT_OS_LINUX) && !defined(RT_OS_FREEBSD) && !defined(RT_OS_WINDOWS) && !defined(RT_OS_SOLARIS) /* PORTME */
5076 if (*pfRelative == true)
5077 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
5078 N_("VMDK: Image path: '%s'. The 'Relative' option is not supported on this host OS"),
5079 pImage->pszFilename);
5080#endif
5081 }
5082 else if (rc != VERR_CFGM_VALUE_NOT_FOUND)
5083 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
5084 N_("VMDK: Image path: '%s'. Getting 'Relative' configuration failed (%Rrc)"), pImage->pszFilename, rc);
5085 else
5086#ifdef RT_OS_DARWIN /* different default on macOS, see ticketref:1461 (comment 20). */
5087 *pfRelative = true;
5088#else
5089 *pfRelative = false;
5090#endif
5091
5092 return VINF_SUCCESS;
5093}
5094
5095/**
5096 * Creates a raw drive (nee disk) descriptor.
5097 *
5098 * This was originally done in VBoxInternalManage.cpp, but was copied (not move)
5099 * here much later. That's one of the reasons why we produce a descriptor just
5100 * like it does, rather than mixing directly into the vmdkCreateRawImage code.
5101 *
5102 * @returns VBox status code.
5103 * @param pImage The image.
5104 * @param ppRaw Where to return the raw drive descriptor. Caller must
5105 * free it using vmdkRawDescFree regardless of the status
5106 * code.
5107 * @internal
5108 */
5109static int vmdkMakeRawDescriptor(PVMDKIMAGE pImage, PVDISKRAW *ppRaw)
5110{
5111 /* Make sure it's NULL. */
5112 *ppRaw = NULL;
5113
5114 /*
5115 * Read the configuration.
5116 */
5117 char *pszRawDrive = NULL;
5118 uint32_t fPartitions = 0; /* zero if whole-drive */
5119 uint32_t fPartitionsReadOnly = 0; /* (subset of fPartitions) */
5120 void *pvBootSector = NULL;
5121 size_t cbBootSector = 0;
5122 bool fRelative = false;
5123 char *pszFreeMe = NULL; /* lazy bird cleanup. */
5124 int rc = vmdkRawDescParseConfig(pImage, &pszRawDrive, &fPartitions, &fPartitionsReadOnly,
5125 &pvBootSector, &cbBootSector, &fRelative, &pszFreeMe);
5126 RTStrFree(pszFreeMe);
5127 if (RT_SUCCESS(rc))
5128 {
5129 /*
5130 * Open the device, getting the sector size and drive size.
5131 */
5132 uint64_t cbSize = 0;
5133 uint32_t cbSector = 0;
5134 RTFILE hRawDrive = NIL_RTFILE;
5135 rc = vmkdRawDescOpenDevice(pImage, pszRawDrive, &hRawDrive, &cbSize, &cbSector);
5136 if (RT_SUCCESS(rc))
5137 {
5138 pImage->cbSize = cbSize;
5139 /*
5140 * Create the raw-drive descriptor
5141 */
5142 PVDISKRAW pRawDesc = (PVDISKRAW)RTMemAllocZ(sizeof(*pRawDesc));
5143 if (pRawDesc)
5144 {
5145 pRawDesc->szSignature[0] = 'R';
5146 pRawDesc->szSignature[1] = 'A';
5147 pRawDesc->szSignature[2] = 'W';
5148 //pRawDesc->szSignature[3] = '\0';
5149 if (!fPartitions)
5150 {
5151 /*
5152 * It's simple for when doing the whole drive.
5153 */
5154 pRawDesc->uFlags = VDISKRAW_DISK;
5155 rc = RTStrDupEx(&pRawDesc->pszRawDisk, pszRawDrive);
5156 }
5157 else
5158 {
5159 /*
5160 * In selected partitions mode we've got a lot more work ahead of us.
5161 */
5162 pRawDesc->uFlags = VDISKRAW_NORMAL;
5163 //pRawDesc->pszRawDisk = NULL;
5164 //pRawDesc->cPartDescs = 0;
5165 //pRawDesc->pPartDescs = NULL;
5166
5167 /* We need to parse the partition map to complete the descriptor: */
5168 RTDVM hVolMgr = NIL_RTDVM;
5169 rc = vmdkRawDescOpenVolMgr(pImage, hRawDrive, pszRawDrive, cbSector, &hVolMgr);
5170 if (RT_SUCCESS(rc))
5171 {
5172 RTDVMFORMATTYPE enmFormatType = RTDvmMapGetFormatType(hVolMgr);
5173 if ( enmFormatType == RTDVMFORMATTYPE_MBR
5174 || enmFormatType == RTDVMFORMATTYPE_GPT)
5175 {
5176 pRawDesc->enmPartitioningType = enmFormatType == RTDVMFORMATTYPE_MBR
5177 ? VDISKPARTTYPE_MBR : VDISKPARTTYPE_GPT;
5178
5179 /* Add copies of the partition tables: */
5180 rc = vmdkRawDescDoCopyPartitionTables(pImage, hVolMgr, pRawDesc, pszRawDrive, hRawDrive,
5181 pvBootSector, cbBootSector);
5182 if (RT_SUCCESS(rc))
5183 {
5184 /* Add descriptors for the partitions/volumes, indicating which
5185 should be accessible and how to access them: */
5186 RTDVMVOLUME hVolRelease = NIL_RTDVMVOLUME;
5187 rc = vmdkRawDescDoPartitions(pImage, hVolMgr, pRawDesc, hRawDrive, pszRawDrive, cbSector,
5188 fPartitions, fPartitionsReadOnly, fRelative, &hVolRelease);
5189 RTDvmVolumeRelease(hVolRelease);
5190
5191 /* Finally, sort the partition and check consistency (overlaps, etc): */
5192 if (RT_SUCCESS(rc))
5193 rc = vmdkRawDescPostProcessPartitions(pImage, pRawDesc, cbSize);
5194 }
5195 }
5196 else
5197 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
5198 N_("VMDK: Image path: '%s'. Unsupported partitioning for the disk '%s': %s"),
5199 pImage->pszFilename, pszRawDrive, RTDvmMapGetFormatType(hVolMgr));
5200 RTDvmRelease(hVolMgr);
5201 }
5202 }
5203 if (RT_SUCCESS(rc))
5204 {
5205 /*
5206 * We succeeded.
5207 */
5208 *ppRaw = pRawDesc;
5209 Log(("vmdkMakeRawDescriptor: fFlags=%#x enmPartitioningType=%d cPartDescs=%u pszRawDisk=%s\n",
5210 pRawDesc->uFlags, pRawDesc->enmPartitioningType, pRawDesc->cPartDescs, pRawDesc->pszRawDisk));
5211 if (pRawDesc->cPartDescs)
5212 {
5213 Log(("# VMDK offset Length Device offset PartDataPtr Device\n"));
5214 for (uint32_t i = 0; i < pRawDesc->cPartDescs; i++)
5215 Log(("%2u %14RU64 %14RU64 %14RU64 %#18p %s\n", i, pRawDesc->pPartDescs[i].offStartInVDisk,
5216 pRawDesc->pPartDescs[i].cbData, pRawDesc->pPartDescs[i].offStartInDevice,
5217 pRawDesc->pPartDescs[i].pvPartitionData, pRawDesc->pPartDescs[i].pszRawDevice));
5218 }
5219 }
5220 else
5221 vmdkRawDescFree(pRawDesc);
5222 }
5223 else
5224 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
5225 N_("VMDK: Image path: '%s'. Failed to allocate %u bytes for the raw drive descriptor"),
5226 pImage->pszFilename, sizeof(*pRawDesc));
5227 RTFileClose(hRawDrive);
5228 }
5229 }
5230 RTStrFree(pszRawDrive);
5231 RTMemFree(pvBootSector);
5232 return rc;
5233}
5234
5235/**
5236 * Internal: create VMDK images for raw disk/partition access.
5237 */
5238static int vmdkCreateRawImage(PVMDKIMAGE pImage, const PVDISKRAW pRaw,
5239 uint64_t cbSize)
5240{
5241 int rc = VINF_SUCCESS;
5242 PVMDKEXTENT pExtent;
5243
5244 if (pRaw->uFlags & VDISKRAW_DISK)
5245 {
5246 /* Full raw disk access. This requires setting up a descriptor
5247 * file and open the (flat) raw disk. */
5248 rc = vmdkCreateExtents(pImage, 1);
5249 if (RT_FAILURE(rc))
5250 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
5251 pExtent = &pImage->pExtents[0];
5252 /* Create raw disk descriptor file. */
5253 rc = vmdkFileOpen(pImage, &pImage->pFile, NULL, pImage->pszFilename,
5254 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
5255 true /* fCreate */));
5256 if (RT_FAILURE(rc))
5257 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pImage->pszFilename);
5258
5259 /* Set up basename for extent description. Cannot use StrDup. */
5260 size_t cbBasename = strlen(pRaw->pszRawDisk) + 1;
5261 char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
5262 if (!pszBasename)
5263 return VERR_NO_MEMORY;
5264 memcpy(pszBasename, pRaw->pszRawDisk, cbBasename);
5265 pExtent->pszBasename = pszBasename;
5266 /* For raw disks the full name is identical to the base name. */
5267 pExtent->pszFullname = RTStrDup(pszBasename);
5268 if (!pExtent->pszFullname)
5269 return VERR_NO_MEMORY;
5270 pExtent->enmType = VMDKETYPE_FLAT;
5271 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
5272 pExtent->uSectorOffset = 0;
5273 pExtent->enmAccess = (pRaw->uFlags & VDISKRAW_READONLY) ? VMDKACCESS_READONLY : VMDKACCESS_READWRITE;
5274 pExtent->fMetaDirty = false;
5275
5276 /* Open flat image, the raw disk. */
5277 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
5278 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags | ((pExtent->enmAccess == VMDKACCESS_READONLY) ? VD_OPEN_FLAGS_READONLY : 0),
5279 false /* fCreate */));
5280 if (RT_FAILURE(rc))
5281 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not open raw disk file '%s'"), pExtent->pszFullname);
5282 }
5283 else
5284 {
5285 /* Raw partition access. This requires setting up a descriptor
5286 * file, write the partition information to a flat extent and
5287 * open all the (flat) raw disk partitions. */
5288
5289 /* First pass over the partition data areas to determine how many
5290 * extents we need. One data area can require up to 2 extents, as
5291 * it might be necessary to skip over unpartitioned space. */
5292 unsigned cExtents = 0;
5293 uint64_t uStart = 0;
5294 for (unsigned i = 0; i < pRaw->cPartDescs; i++)
5295 {
5296 PVDISKRAWPARTDESC pPart = &pRaw->pPartDescs[i];
5297 if (uStart > pPart->offStartInVDisk)
5298 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
5299 N_("VMDK: incorrect partition data area ordering set up by the caller in '%s'"), pImage->pszFilename);
5300
5301 if (uStart < pPart->offStartInVDisk)
5302 cExtents++;
5303 uStart = pPart->offStartInVDisk + pPart->cbData;
5304 cExtents++;
5305 }
5306 /* Another extent for filling up the rest of the image. */
5307 if (uStart != cbSize)
5308 cExtents++;
5309
5310 rc = vmdkCreateExtents(pImage, cExtents);
5311 if (RT_FAILURE(rc))
5312 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
5313
5314 /* Create raw partition descriptor file. */
5315 rc = vmdkFileOpen(pImage, &pImage->pFile, NULL, pImage->pszFilename,
5316 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
5317 true /* fCreate */));
5318 if (RT_FAILURE(rc))
5319 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pImage->pszFilename);
5320
5321 /* Create base filename for the partition table extent. */
5322 /** @todo remove fixed buffer without creating memory leaks. */
5323 char pszPartition[1024];
5324 const char *pszBase = RTPathFilename(pImage->pszFilename);
5325 const char *pszSuff = RTPathSuffix(pszBase);
5326 if (pszSuff == NULL)
5327 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: invalid filename '%s'"), pImage->pszFilename);
5328 char *pszBaseBase = RTStrDup(pszBase);
5329 if (!pszBaseBase)
5330 return VERR_NO_MEMORY;
5331 RTPathStripSuffix(pszBaseBase);
5332 RTStrPrintf(pszPartition, sizeof(pszPartition), "%s-pt%s",
5333 pszBaseBase, pszSuff);
5334 RTStrFree(pszBaseBase);
5335
5336 /* Second pass over the partitions, now define all extents. */
5337 uint64_t uPartOffset = 0;
5338 cExtents = 0;
5339 uStart = 0;
5340 for (unsigned i = 0; i < pRaw->cPartDescs; i++)
5341 {
5342 PVDISKRAWPARTDESC pPart = &pRaw->pPartDescs[i];
5343 pExtent = &pImage->pExtents[cExtents++];
5344
5345 if (uStart < pPart->offStartInVDisk)
5346 {
5347 pExtent->pszBasename = NULL;
5348 pExtent->pszFullname = NULL;
5349 pExtent->enmType = VMDKETYPE_ZERO;
5350 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->offStartInVDisk - uStart);
5351 pExtent->uSectorOffset = 0;
5352 pExtent->enmAccess = VMDKACCESS_READWRITE;
5353 pExtent->fMetaDirty = false;
5354 /* go to next extent */
5355 pExtent = &pImage->pExtents[cExtents++];
5356 }
5357 uStart = pPart->offStartInVDisk + pPart->cbData;
5358
5359 if (pPart->pvPartitionData)
5360 {
5361 /* Set up basename for extent description. Can't use StrDup. */
5362 size_t cbBasename = strlen(pszPartition) + 1;
5363 char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
5364 if (!pszBasename)
5365 return VERR_NO_MEMORY;
5366 memcpy(pszBasename, pszPartition, cbBasename);
5367 pExtent->pszBasename = pszBasename;
5368
5369 /* Set up full name for partition extent. */
5370 char *pszDirname = RTStrDup(pImage->pszFilename);
5371 if (!pszDirname)
5372 return VERR_NO_STR_MEMORY;
5373 RTPathStripFilename(pszDirname);
5374 char *pszFullname = RTPathJoinA(pszDirname, pExtent->pszBasename);
5375 RTStrFree(pszDirname);
5376 if (!pszFullname)
5377 return VERR_NO_STR_MEMORY;
5378 pExtent->pszFullname = pszFullname;
5379 pExtent->enmType = VMDKETYPE_FLAT;
5380 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbData);
5381 pExtent->uSectorOffset = uPartOffset;
5382 pExtent->enmAccess = VMDKACCESS_READWRITE;
5383 pExtent->fMetaDirty = false;
5384
5385 /* Create partition table flat image. */
5386 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
5387 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags | ((pExtent->enmAccess == VMDKACCESS_READONLY) ? VD_OPEN_FLAGS_READONLY : 0),
5388 true /* fCreate */));
5389 if (RT_FAILURE(rc))
5390 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new partition data file '%s'"), pExtent->pszFullname);
5391 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
5392 VMDK_SECTOR2BYTE(uPartOffset),
5393 pPart->pvPartitionData,
5394 pPart->cbData);
5395 if (RT_FAILURE(rc))
5396 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not write partition data to '%s'"), pExtent->pszFullname);
5397 uPartOffset += VMDK_BYTE2SECTOR(pPart->cbData);
5398 }
5399 else
5400 {
5401 if (pPart->pszRawDevice)
5402 {
5403 /* Set up basename for extent descr. Can't use StrDup. */
5404 size_t cbBasename = strlen(pPart->pszRawDevice) + 1;
5405 char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
5406 if (!pszBasename)
5407 return VERR_NO_MEMORY;
5408 memcpy(pszBasename, pPart->pszRawDevice, cbBasename);
5409 pExtent->pszBasename = pszBasename;
5410 /* For raw disks full name is identical to base name. */
5411 pExtent->pszFullname = RTStrDup(pszBasename);
5412 if (!pExtent->pszFullname)
5413 return VERR_NO_MEMORY;
5414 pExtent->enmType = VMDKETYPE_FLAT;
5415 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbData);
5416 pExtent->uSectorOffset = VMDK_BYTE2SECTOR(pPart->offStartInDevice);
5417 pExtent->enmAccess = (pPart->uFlags & VDISKRAW_READONLY) ? VMDKACCESS_READONLY : VMDKACCESS_READWRITE;
5418 pExtent->fMetaDirty = false;
5419
5420 /* Open flat image, the raw partition. */
5421 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
5422 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags | ((pExtent->enmAccess == VMDKACCESS_READONLY) ? VD_OPEN_FLAGS_READONLY : 0),
5423 false /* fCreate */));
5424 if (RT_FAILURE(rc))
5425 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not open raw partition file '%s'"), pExtent->pszFullname);
5426 }
5427 else
5428 {
5429 pExtent->pszBasename = NULL;
5430 pExtent->pszFullname = NULL;
5431 pExtent->enmType = VMDKETYPE_ZERO;
5432 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbData);
5433 pExtent->uSectorOffset = 0;
5434 pExtent->enmAccess = VMDKACCESS_READWRITE;
5435 pExtent->fMetaDirty = false;
5436 }
5437 }
5438 }
5439 /* Another extent for filling up the rest of the image. */
5440 if (uStart != cbSize)
5441 {
5442 pExtent = &pImage->pExtents[cExtents++];
5443 pExtent->pszBasename = NULL;
5444 pExtent->pszFullname = NULL;
5445 pExtent->enmType = VMDKETYPE_ZERO;
5446 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize - uStart);
5447 pExtent->uSectorOffset = 0;
5448 pExtent->enmAccess = VMDKACCESS_READWRITE;
5449 pExtent->fMetaDirty = false;
5450 }
5451 }
5452
5453 rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType",
5454 (pRaw->uFlags & VDISKRAW_DISK) ?
5455 "fullDevice" : "partitionedDevice");
5456 if (RT_FAILURE(rc))
5457 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pImage->pszFilename);
5458 return rc;
5459}
5460
5461/**
5462 * Internal: create a regular (i.e. file-backed) VMDK image.
5463 */
5464static int vmdkCreateRegularImage(PVMDKIMAGE pImage, uint64_t cbSize,
5465 unsigned uImageFlags, PVDINTERFACEPROGRESS pIfProgress,
5466 unsigned uPercentStart, unsigned uPercentSpan)
5467{
5468 int rc = VINF_SUCCESS;
5469 unsigned cExtents = 1;
5470 uint64_t cbOffset = 0;
5471 uint64_t cbRemaining = cbSize;
5472
5473 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
5474 {
5475 cExtents = cbSize / VMDK_2G_SPLIT_SIZE;
5476 /* Do proper extent computation: need one smaller extent if the total
5477 * size isn't evenly divisible by the split size. */
5478 if (cbSize % VMDK_2G_SPLIT_SIZE)
5479 cExtents++;
5480 }
5481 rc = vmdkCreateExtents(pImage, cExtents);
5482 if (RT_FAILURE(rc))
5483 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
5484
5485 /* Basename strings needed for constructing the extent names. */
5486 char *pszBasenameSubstr = RTPathFilename(pImage->pszFilename);
5487 AssertPtr(pszBasenameSubstr);
5488 size_t cbBasenameSubstr = strlen(pszBasenameSubstr) + 1;
5489
5490 /* Create separate descriptor file if necessary. */
5491 if (cExtents != 1 || (uImageFlags & VD_IMAGE_FLAGS_FIXED))
5492 {
5493 rc = vmdkFileOpen(pImage, &pImage->pFile, NULL, pImage->pszFilename,
5494 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
5495 true /* fCreate */));
5496 if (RT_FAILURE(rc))
5497 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new sparse descriptor file '%s'"), pImage->pszFilename);
5498 }
5499 else
5500 pImage->pFile = NULL;
5501
5502 /* Set up all extents. */
5503 for (unsigned i = 0; i < cExtents; i++)
5504 {
5505 PVMDKEXTENT pExtent = &pImage->pExtents[i];
5506 uint64_t cbExtent = cbRemaining;
5507
5508 /* Set up fullname/basename for extent description. Cannot use StrDup
5509 * for basename, as it is not guaranteed that the memory can be freed
5510 * with RTMemTmpFree, which must be used as in other code paths
5511 * StrDup is not usable. */
5512 if (cExtents == 1 && !(uImageFlags & VD_IMAGE_FLAGS_FIXED))
5513 {
5514 char *pszBasename = (char *)RTMemTmpAlloc(cbBasenameSubstr);
5515 if (!pszBasename)
5516 return VERR_NO_MEMORY;
5517 memcpy(pszBasename, pszBasenameSubstr, cbBasenameSubstr);
5518 pExtent->pszBasename = pszBasename;
5519 }
5520 else
5521 {
5522 char *pszBasenameSuff = RTPathSuffix(pszBasenameSubstr);
5523 char *pszBasenameBase = RTStrDup(pszBasenameSubstr);
5524 RTPathStripSuffix(pszBasenameBase);
5525 char *pszTmp;
5526 size_t cbTmp;
5527 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
5528 {
5529 if (cExtents == 1)
5530 RTStrAPrintf(&pszTmp, "%s-flat%s", pszBasenameBase,
5531 pszBasenameSuff);
5532 else
5533 RTStrAPrintf(&pszTmp, "%s-f%03d%s", pszBasenameBase,
5534 i+1, pszBasenameSuff);
5535 }
5536 else
5537 RTStrAPrintf(&pszTmp, "%s-s%03d%s", pszBasenameBase, i+1,
5538 pszBasenameSuff);
5539 RTStrFree(pszBasenameBase);
5540 if (!pszTmp)
5541 return VERR_NO_STR_MEMORY;
5542 cbTmp = strlen(pszTmp) + 1;
5543 char *pszBasename = (char *)RTMemTmpAlloc(cbTmp);
5544 if (!pszBasename)
5545 {
5546 RTStrFree(pszTmp);
5547 return VERR_NO_MEMORY;
5548 }
5549 memcpy(pszBasename, pszTmp, cbTmp);
5550 RTStrFree(pszTmp);
5551 pExtent->pszBasename = pszBasename;
5552 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
5553 cbExtent = RT_MIN(cbRemaining, VMDK_2G_SPLIT_SIZE);
5554 }
5555 char *pszBasedirectory = RTStrDup(pImage->pszFilename);
5556 if (!pszBasedirectory)
5557 return VERR_NO_STR_MEMORY;
5558 RTPathStripFilename(pszBasedirectory);
5559 char *pszFullname = RTPathJoinA(pszBasedirectory, pExtent->pszBasename);
5560 RTStrFree(pszBasedirectory);
5561 if (!pszFullname)
5562 return VERR_NO_STR_MEMORY;
5563 pExtent->pszFullname = pszFullname;
5564
5565 /* Create file for extent. */
5566 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
5567 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
5568 true /* fCreate */));
5569 if (RT_FAILURE(rc))
5570 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pExtent->pszFullname);
5571 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
5572 {
5573 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage, cbExtent,
5574 0 /* fFlags */, pIfProgress,
5575 uPercentStart + cbOffset * uPercentSpan / cbSize,
5576 cbExtent * uPercentSpan / cbSize);
5577 if (RT_FAILURE(rc))
5578 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
5579 }
5580
5581 /* Place descriptor file information (where integrated). */
5582 if (cExtents == 1 && !(uImageFlags & VD_IMAGE_FLAGS_FIXED))
5583 {
5584 pExtent->uDescriptorSector = 1;
5585 pExtent->cDescriptorSectors = VMDK_BYTE2SECTOR(pImage->cbDescAlloc);
5586 /* The descriptor is part of the (only) extent. */
5587 pExtent->pDescData = pImage->pDescData;
5588 pImage->pDescData = NULL;
5589 }
5590
5591 if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
5592 {
5593 uint64_t cSectorsPerGDE, cSectorsPerGD;
5594 pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
5595 pExtent->cSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64(cbExtent, _64K));
5596 pExtent->cSectorsPerGrain = VMDK_BYTE2SECTOR(_64K);
5597 pExtent->cGTEntries = 512;
5598 cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
5599 pExtent->cSectorsPerGDE = cSectorsPerGDE;
5600 pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
5601 cSectorsPerGD = (pExtent->cGDEntries + (512 / sizeof(uint32_t) - 1)) / (512 / sizeof(uint32_t));
5602 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
5603 {
5604 /* The spec says version is 1 for all VMDKs, but the vast
5605 * majority of streamOptimized VMDKs actually contain
5606 * version 3 - so go with the majority. Both are accepted. */
5607 pExtent->uVersion = 3;
5608 pExtent->uCompression = VMDK_COMPRESSION_DEFLATE;
5609 }
5610 }
5611 else
5612 {
5613 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
5614 pExtent->enmType = VMDKETYPE_VMFS;
5615 else
5616 pExtent->enmType = VMDKETYPE_FLAT;
5617 }
5618
5619 pExtent->enmAccess = VMDKACCESS_READWRITE;
5620 pExtent->fUncleanShutdown = true;
5621 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbExtent);
5622 pExtent->uSectorOffset = 0;
5623 pExtent->fMetaDirty = true;
5624
5625 if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
5626 {
5627 /* fPreAlloc should never be false because VMware can't use such images. */
5628 rc = vmdkCreateGrainDirectory(pImage, pExtent,
5629 RT_MAX( pExtent->uDescriptorSector
5630 + pExtent->cDescriptorSectors,
5631 1),
5632 true /* fPreAlloc */);
5633 if (RT_FAILURE(rc))
5634 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new grain directory in '%s'"), pExtent->pszFullname);
5635 }
5636
5637 cbOffset += cbExtent;
5638
5639 if (RT_SUCCESS(rc))
5640 vdIfProgress(pIfProgress, uPercentStart + cbOffset * uPercentSpan / cbSize);
5641
5642 cbRemaining -= cbExtent;
5643 }
5644
5645 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
5646 {
5647 /* VirtualBox doesn't care, but VMWare ESX freaks out if the wrong
5648 * controller type is set in an image. */
5649 rc = vmdkDescDDBSetStr(pImage, &pImage->Descriptor, "ddb.adapterType", "lsilogic");
5650 if (RT_FAILURE(rc))
5651 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set controller type to lsilogic in '%s'"), pImage->pszFilename);
5652 }
5653
5654 const char *pszDescType = NULL;
5655 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
5656 {
5657 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
5658 pszDescType = "vmfs";
5659 else
5660 pszDescType = (cExtents == 1)
5661 ? "monolithicFlat" : "twoGbMaxExtentFlat";
5662 }
5663 else
5664 {
5665 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
5666 pszDescType = "streamOptimized";
5667 else
5668 {
5669 pszDescType = (cExtents == 1)
5670 ? "monolithicSparse" : "twoGbMaxExtentSparse";
5671 }
5672 }
5673 rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType",
5674 pszDescType);
5675 if (RT_FAILURE(rc))
5676 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pImage->pszFilename);
5677 return rc;
5678}
5679
5680/**
5681 * Internal: Create a real stream optimized VMDK using only linear writes.
5682 */
5683static int vmdkCreateStreamImage(PVMDKIMAGE pImage, uint64_t cbSize)
5684{
5685 int rc = vmdkCreateExtents(pImage, 1);
5686 if (RT_FAILURE(rc))
5687 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
5688
5689 /* Basename strings needed for constructing the extent names. */
5690 const char *pszBasenameSubstr = RTPathFilename(pImage->pszFilename);
5691 AssertPtr(pszBasenameSubstr);
5692 size_t cbBasenameSubstr = strlen(pszBasenameSubstr) + 1;
5693
5694 /* No separate descriptor file. */
5695 pImage->pFile = NULL;
5696
5697 /* Set up all extents. */
5698 PVMDKEXTENT pExtent = &pImage->pExtents[0];
5699
5700 /* Set up fullname/basename for extent description. Cannot use StrDup
5701 * for basename, as it is not guaranteed that the memory can be freed
5702 * with RTMemTmpFree, which must be used as in other code paths
5703 * StrDup is not usable. */
5704 char *pszBasename = (char *)RTMemTmpAlloc(cbBasenameSubstr);
5705 if (!pszBasename)
5706 return VERR_NO_MEMORY;
5707 memcpy(pszBasename, pszBasenameSubstr, cbBasenameSubstr);
5708 pExtent->pszBasename = pszBasename;
5709
5710 char *pszBasedirectory = RTStrDup(pImage->pszFilename);
5711 RTPathStripFilename(pszBasedirectory);
5712 char *pszFullname = RTPathJoinA(pszBasedirectory, pExtent->pszBasename);
5713 RTStrFree(pszBasedirectory);
5714 if (!pszFullname)
5715 return VERR_NO_STR_MEMORY;
5716 pExtent->pszFullname = pszFullname;
5717
5718 /* Create file for extent. Make it write only, no reading allowed. */
5719 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
5720 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
5721 true /* fCreate */)
5722 & ~RTFILE_O_READ);
5723 if (RT_FAILURE(rc))
5724 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pExtent->pszFullname);
5725
5726 /* Place descriptor file information. */
5727 pExtent->uDescriptorSector = 1;
5728 pExtent->cDescriptorSectors = VMDK_BYTE2SECTOR(pImage->cbDescAlloc);
5729 /* The descriptor is part of the (only) extent. */
5730 pExtent->pDescData = pImage->pDescData;
5731 pImage->pDescData = NULL;
5732
5733 uint64_t cSectorsPerGDE, cSectorsPerGD;
5734 pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
5735 pExtent->cSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64(cbSize, _64K));
5736 pExtent->cSectorsPerGrain = VMDK_BYTE2SECTOR(_64K);
5737 pExtent->cGTEntries = 512;
5738 cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
5739 pExtent->cSectorsPerGDE = cSectorsPerGDE;
5740 pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
5741 cSectorsPerGD = (pExtent->cGDEntries + (512 / sizeof(uint32_t) - 1)) / (512 / sizeof(uint32_t));
5742
5743 /* The spec says version is 1 for all VMDKs, but the vast
5744 * majority of streamOptimized VMDKs actually contain
5745 * version 3 - so go with the majority. Both are accepted. */
5746 pExtent->uVersion = 3;
5747 pExtent->uCompression = VMDK_COMPRESSION_DEFLATE;
5748 pExtent->fFooter = true;
5749
5750 pExtent->enmAccess = VMDKACCESS_READONLY;
5751 pExtent->fUncleanShutdown = false;
5752 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
5753 pExtent->uSectorOffset = 0;
5754 pExtent->fMetaDirty = true;
5755
5756 /* Create grain directory, without preallocating it straight away. It will
5757 * be constructed on the fly when writing out the data and written when
5758 * closing the image. The end effect is that the full grain directory is
5759 * allocated, which is a requirement of the VMDK specs. */
5760 rc = vmdkCreateGrainDirectory(pImage, pExtent, VMDK_GD_AT_END,
5761 false /* fPreAlloc */);
5762 if (RT_FAILURE(rc))
5763 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new grain directory in '%s'"), pExtent->pszFullname);
5764
5765 rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType",
5766 "streamOptimized");
5767 if (RT_FAILURE(rc))
5768 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pImage->pszFilename);
5769
5770 return rc;
5771}
5772
5773/**
5774 * Initializes the UUID fields in the DDB.
5775 *
5776 * @returns VBox status code.
5777 * @param pImage The VMDK image instance.
5778 */
5779static int vmdkCreateImageDdbUuidsInit(PVMDKIMAGE pImage)
5780{
5781 int rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor, VMDK_DDB_IMAGE_UUID, &pImage->ImageUuid);
5782 if (RT_SUCCESS(rc))
5783 {
5784 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor, VMDK_DDB_PARENT_UUID, &pImage->ParentUuid);
5785 if (RT_SUCCESS(rc))
5786 {
5787 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor, VMDK_DDB_MODIFICATION_UUID,
5788 &pImage->ModificationUuid);
5789 if (RT_SUCCESS(rc))
5790 {
5791 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor, VMDK_DDB_PARENT_MODIFICATION_UUID,
5792 &pImage->ParentModificationUuid);
5793 if (RT_FAILURE(rc))
5794 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
5795 N_("VMDK: error storing parent modification UUID in new descriptor in '%s'"), pImage->pszFilename);
5796 }
5797 else
5798 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
5799 N_("VMDK: error storing modification UUID in new descriptor in '%s'"), pImage->pszFilename);
5800 }
5801 else
5802 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
5803 N_("VMDK: error storing parent image UUID in new descriptor in '%s'"), pImage->pszFilename);
5804 }
5805 else
5806 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
5807 N_("VMDK: error storing image UUID in new descriptor in '%s'"), pImage->pszFilename);
5808
5809 return rc;
5810}
5811
5812/**
5813 * Internal: The actual code for creating any VMDK variant currently in
5814 * existence on hosted environments.
5815 */
5816static int vmdkCreateImage(PVMDKIMAGE pImage, uint64_t cbSize,
5817 unsigned uImageFlags, const char *pszComment,
5818 PCVDGEOMETRY pPCHSGeometry,
5819 PCVDGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
5820 PVDINTERFACEPROGRESS pIfProgress,
5821 unsigned uPercentStart, unsigned uPercentSpan)
5822{
5823 pImage->uImageFlags = uImageFlags;
5824
5825 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
5826 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
5827 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
5828
5829 int rc = vmdkCreateDescriptor(pImage, pImage->pDescData, pImage->cbDescAlloc,
5830 &pImage->Descriptor);
5831 if (RT_SUCCESS(rc))
5832 {
5833 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK)
5834 {
5835 /* Raw disk image (includes raw partition). */
5836 PVDISKRAW pRaw = NULL;
5837 rc = vmdkMakeRawDescriptor(pImage, &pRaw);
5838 if (RT_FAILURE(rc))
5839 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create raw descriptor for '%s'"),
5840 pImage->pszFilename);
5841 if (!cbSize)
5842 cbSize = pImage->cbSize;
5843
5844 rc = vmdkCreateRawImage(pImage, pRaw, cbSize);
5845 vmdkRawDescFree(pRaw);
5846 }
5847 else if (uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
5848 {
5849 /* Stream optimized sparse image (monolithic). */
5850 rc = vmdkCreateStreamImage(pImage, cbSize);
5851 }
5852 else
5853 {
5854 /* Regular fixed or sparse image (monolithic or split). */
5855 rc = vmdkCreateRegularImage(pImage, cbSize, uImageFlags,
5856 pIfProgress, uPercentStart,
5857 uPercentSpan * 95 / 100);
5858 }
5859
5860 if (RT_SUCCESS(rc))
5861 {
5862 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan * 98 / 100);
5863
5864 pImage->cbSize = cbSize;
5865
5866 for (unsigned i = 0; i < pImage->cExtents; i++)
5867 {
5868 PVMDKEXTENT pExtent = &pImage->pExtents[i];
5869
5870 rc = vmdkDescExtInsert(pImage, &pImage->Descriptor, pExtent->enmAccess,
5871 pExtent->cNominalSectors, pExtent->enmType,
5872 pExtent->pszBasename, pExtent->uSectorOffset);
5873 if (RT_FAILURE(rc))
5874 {
5875 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not insert the extent list into descriptor in '%s'"), pImage->pszFilename);
5876 break;
5877 }
5878 }
5879
5880 if (RT_SUCCESS(rc))
5881 vmdkDescExtRemoveDummy(pImage, &pImage->Descriptor);
5882
5883 pImage->LCHSGeometry = *pLCHSGeometry;
5884 pImage->PCHSGeometry = *pPCHSGeometry;
5885
5886 if (RT_SUCCESS(rc))
5887 {
5888 if ( pPCHSGeometry->cCylinders != 0
5889 && pPCHSGeometry->cHeads != 0
5890 && pPCHSGeometry->cSectors != 0)
5891 rc = vmdkDescSetPCHSGeometry(pImage, pPCHSGeometry);
5892 else if (uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK)
5893 {
5894 VDGEOMETRY RawDiskPCHSGeometry;
5895 RawDiskPCHSGeometry.cCylinders = (uint32_t)RT_MIN(pImage->cbSize / 512 / 16 / 63, 16383);
5896 RawDiskPCHSGeometry.cHeads = 16;
5897 RawDiskPCHSGeometry.cSectors = 63;
5898 rc = vmdkDescSetPCHSGeometry(pImage, &RawDiskPCHSGeometry);
5899 }
5900 }
5901
5902 if ( RT_SUCCESS(rc)
5903 && pLCHSGeometry->cCylinders != 0
5904 && pLCHSGeometry->cHeads != 0
5905 && pLCHSGeometry->cSectors != 0)
5906 rc = vmdkDescSetLCHSGeometry(pImage, pLCHSGeometry);
5907
5908 pImage->ImageUuid = *pUuid;
5909 RTUuidClear(&pImage->ParentUuid);
5910 RTUuidClear(&pImage->ModificationUuid);
5911 RTUuidClear(&pImage->ParentModificationUuid);
5912
5913 if (RT_SUCCESS(rc))
5914 rc = vmdkCreateImageDdbUuidsInit(pImage);
5915
5916 if (RT_SUCCESS(rc))
5917 rc = vmdkAllocateGrainTableCache(pImage);
5918
5919 if (RT_SUCCESS(rc))
5920 {
5921 rc = vmdkSetImageComment(pImage, pszComment);
5922 if (RT_FAILURE(rc))
5923 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot set image comment in '%s'"), pImage->pszFilename);
5924 }
5925
5926 if (RT_SUCCESS(rc))
5927 {
5928 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan * 99 / 100);
5929
5930 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
5931 {
5932 /* streamOptimized is a bit special, we cannot trigger the flush
5933 * until all data has been written. So we write the necessary
5934 * information explicitly. */
5935 pImage->pExtents[0].cDescriptorSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64( pImage->Descriptor.aLines[pImage->Descriptor.cLines]
5936 - pImage->Descriptor.aLines[0], 512));
5937 rc = vmdkWriteMetaSparseExtent(pImage, &pImage->pExtents[0], 0, NULL);
5938 if (RT_SUCCESS(rc))
5939 {
5940 rc = vmdkWriteDescriptor(pImage, NULL);
5941 if (RT_FAILURE(rc))
5942 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write VMDK descriptor in '%s'"), pImage->pszFilename);
5943 }
5944 else
5945 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write VMDK header in '%s'"), pImage->pszFilename);
5946 }
5947 else
5948 rc = vmdkFlushImage(pImage, NULL);
5949 }
5950 }
5951 }
5952 else
5953 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new descriptor in '%s'"), pImage->pszFilename);
5954
5955
5956 if (RT_SUCCESS(rc))
5957 {
5958 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
5959 pImage->RegionList.fFlags = 0;
5960 pImage->RegionList.cRegions = 1;
5961
5962 pRegion->offRegion = 0; /* Disk start. */
5963 pRegion->cbBlock = 512;
5964 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
5965 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
5966 pRegion->cbData = 512;
5967 pRegion->cbMetadata = 0;
5968 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
5969
5970 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
5971 }
5972 else
5973 vmdkFreeImage(pImage, rc != VERR_ALREADY_EXISTS, false /*fFlush*/);
5974 return rc;
5975}
5976
5977/**
5978 * Internal: Update image comment.
5979 */
5980static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment)
5981{
5982 char *pszCommentEncoded = NULL;
5983 if (pszComment)
5984 {
5985 pszCommentEncoded = vmdkEncodeString(pszComment);
5986 if (!pszCommentEncoded)
5987 return VERR_NO_MEMORY;
5988 }
5989
5990 int rc = vmdkDescDDBSetStr(pImage, &pImage->Descriptor,
5991 "ddb.comment", pszCommentEncoded);
5992 if (pszCommentEncoded)
5993 RTStrFree(pszCommentEncoded);
5994 if (RT_FAILURE(rc))
5995 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image comment in descriptor in '%s'"), pImage->pszFilename);
5996 return VINF_SUCCESS;
5997}
5998
5999/**
6000 * Internal. Clear the grain table buffer for real stream optimized writing.
6001 */
6002static void vmdkStreamClearGT(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
6003{
6004 uint32_t cCacheLines = RT_ALIGN(pExtent->cGTEntries, VMDK_GT_CACHELINE_SIZE) / VMDK_GT_CACHELINE_SIZE;
6005 for (uint32_t i = 0; i < cCacheLines; i++)
6006 memset(&pImage->pGTCache->aGTCache[i].aGTData[0], '\0',
6007 VMDK_GT_CACHELINE_SIZE * sizeof(uint32_t));
6008}
6009
6010/**
6011 * Internal. Flush the grain table buffer for real stream optimized writing.
6012 */
6013static int vmdkStreamFlushGT(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
6014 uint32_t uGDEntry)
6015{
6016 int rc = VINF_SUCCESS;
6017 uint32_t cCacheLines = RT_ALIGN(pExtent->cGTEntries, VMDK_GT_CACHELINE_SIZE) / VMDK_GT_CACHELINE_SIZE;
6018
6019 /* VMware does not write out completely empty grain tables in the case
6020 * of streamOptimized images, which according to my interpretation of
6021 * the VMDK 1.1 spec is bending the rules. Since they do it and we can
6022 * handle it without problems do it the same way and save some bytes. */
6023 bool fAllZero = true;
6024 for (uint32_t i = 0; i < cCacheLines; i++)
6025 {
6026 /* Convert the grain table to little endian in place, as it will not
6027 * be used at all after this function has been called. */
6028 uint32_t *pGTTmp = &pImage->pGTCache->aGTCache[i].aGTData[0];
6029 for (uint32_t j = 0; j < VMDK_GT_CACHELINE_SIZE; j++, pGTTmp++)
6030 if (*pGTTmp)
6031 {
6032 fAllZero = false;
6033 break;
6034 }
6035 if (!fAllZero)
6036 break;
6037 }
6038 if (fAllZero)
6039 return VINF_SUCCESS;
6040
6041 uint64_t uFileOffset = pExtent->uAppendPosition;
6042 if (!uFileOffset)
6043 return VERR_INTERNAL_ERROR;
6044 /* Align to sector, as the previous write could have been any size. */
6045 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
6046
6047 /* Grain table marker. */
6048 uint8_t aMarker[512];
6049 PVMDKMARKER pMarker = (PVMDKMARKER)&aMarker[0];
6050 memset(pMarker, '\0', sizeof(aMarker));
6051 pMarker->uSector = RT_H2LE_U64(VMDK_BYTE2SECTOR((uint64_t)pExtent->cGTEntries * sizeof(uint32_t)));
6052 pMarker->uType = RT_H2LE_U32(VMDK_MARKER_GT);
6053 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage, uFileOffset,
6054 aMarker, sizeof(aMarker));
6055 AssertRC(rc);
6056 uFileOffset += 512;
6057
6058 if (!pExtent->pGD || pExtent->pGD[uGDEntry])
6059 return VERR_INTERNAL_ERROR;
6060
6061 pExtent->pGD[uGDEntry] = VMDK_BYTE2SECTOR(uFileOffset);
6062
6063 for (uint32_t i = 0; i < cCacheLines; i++)
6064 {
6065 /* Convert the grain table to little endian in place, as it will not
6066 * be used at all after this function has been called. */
6067 uint32_t *pGTTmp = &pImage->pGTCache->aGTCache[i].aGTData[0];
6068 for (uint32_t j = 0; j < VMDK_GT_CACHELINE_SIZE; j++, pGTTmp++)
6069 *pGTTmp = RT_H2LE_U32(*pGTTmp);
6070
6071 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage, uFileOffset,
6072 &pImage->pGTCache->aGTCache[i].aGTData[0],
6073 VMDK_GT_CACHELINE_SIZE * sizeof(uint32_t));
6074 uFileOffset += VMDK_GT_CACHELINE_SIZE * sizeof(uint32_t);
6075 if (RT_FAILURE(rc))
6076 break;
6077 }
6078 Assert(!(uFileOffset % 512));
6079 pExtent->uAppendPosition = RT_ALIGN_64(uFileOffset, 512);
6080 return rc;
6081}
6082
6083/**
6084 * Internal. Free all allocated space for representing an image, and optionally
6085 * delete the image from disk.
6086 */
6087static int vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete, bool fFlush)
6088{
6089 int rc = VINF_SUCCESS;
6090
6091 /* Freeing a never allocated image (e.g. because the open failed) is
6092 * not signalled as an error. After all nothing bad happens. */
6093 if (pImage)
6094 {
6095 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
6096 {
6097 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
6098 {
6099 /* Check if all extents are clean. */
6100 for (unsigned i = 0; i < pImage->cExtents; i++)
6101 {
6102 Assert(!pImage->pExtents[i].fUncleanShutdown);
6103 }
6104 }
6105 else
6106 {
6107 /* Mark all extents as clean. */
6108 for (unsigned i = 0; i < pImage->cExtents; i++)
6109 {
6110 if ( pImage->pExtents[i].enmType == VMDKETYPE_HOSTED_SPARSE
6111 && pImage->pExtents[i].fUncleanShutdown)
6112 {
6113 pImage->pExtents[i].fUncleanShutdown = false;
6114 pImage->pExtents[i].fMetaDirty = true;
6115 }
6116
6117 /* From now on it's not safe to append any more data. */
6118 pImage->pExtents[i].uAppendPosition = 0;
6119 }
6120 }
6121 }
6122
6123 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
6124 {
6125 /* No need to write any pending data if the file will be deleted
6126 * or if the new file wasn't successfully created. */
6127 if ( !fDelete && pImage->pExtents
6128 && pImage->pExtents[0].cGTEntries
6129 && pImage->pExtents[0].uAppendPosition)
6130 {
6131 PVMDKEXTENT pExtent = &pImage->pExtents[0];
6132 uint32_t uLastGDEntry = pExtent->uLastGrainAccess / pExtent->cGTEntries;
6133 rc = vmdkStreamFlushGT(pImage, pExtent, uLastGDEntry);
6134 AssertRC(rc);
6135 vmdkStreamClearGT(pImage, pExtent);
6136 for (uint32_t i = uLastGDEntry + 1; i < pExtent->cGDEntries; i++)
6137 {
6138 rc = vmdkStreamFlushGT(pImage, pExtent, i);
6139 AssertRC(rc);
6140 }
6141
6142 uint64_t uFileOffset = pExtent->uAppendPosition;
6143 if (!uFileOffset)
6144 return VERR_INTERNAL_ERROR;
6145 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
6146
6147 /* From now on it's not safe to append any more data. */
6148 pExtent->uAppendPosition = 0;
6149
6150 /* Grain directory marker. */
6151 uint8_t aMarker[512];
6152 PVMDKMARKER pMarker = (PVMDKMARKER)&aMarker[0];
6153 memset(pMarker, '\0', sizeof(aMarker));
6154 pMarker->uSector = VMDK_BYTE2SECTOR(RT_ALIGN_64(RT_H2LE_U64((uint64_t)pExtent->cGDEntries * sizeof(uint32_t)), 512));
6155 pMarker->uType = RT_H2LE_U32(VMDK_MARKER_GD);
6156 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage, uFileOffset,
6157 aMarker, sizeof(aMarker));
6158 AssertRC(rc);
6159 uFileOffset += 512;
6160
6161 /* Write grain directory in little endian style. The array will
6162 * not be used after this, so convert in place. */
6163 uint32_t *pGDTmp = pExtent->pGD;
6164 for (uint32_t i = 0; i < pExtent->cGDEntries; i++, pGDTmp++)
6165 *pGDTmp = RT_H2LE_U32(*pGDTmp);
6166 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
6167 uFileOffset, pExtent->pGD,
6168 pExtent->cGDEntries * sizeof(uint32_t));
6169 AssertRC(rc);
6170
6171 pExtent->uSectorGD = VMDK_BYTE2SECTOR(uFileOffset);
6172 pExtent->uSectorRGD = VMDK_BYTE2SECTOR(uFileOffset);
6173 uFileOffset = RT_ALIGN_64( uFileOffset
6174 + pExtent->cGDEntries * sizeof(uint32_t),
6175 512);
6176
6177 /* Footer marker. */
6178 memset(pMarker, '\0', sizeof(aMarker));
6179 pMarker->uSector = VMDK_BYTE2SECTOR(512);
6180 pMarker->uType = RT_H2LE_U32(VMDK_MARKER_FOOTER);
6181 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
6182 uFileOffset, aMarker, sizeof(aMarker));
6183 AssertRC(rc);
6184
6185 uFileOffset += 512;
6186 rc = vmdkWriteMetaSparseExtent(pImage, pExtent, uFileOffset, NULL);
6187 AssertRC(rc);
6188
6189 uFileOffset += 512;
6190 /* End-of-stream marker. */
6191 memset(pMarker, '\0', sizeof(aMarker));
6192 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
6193 uFileOffset, aMarker, sizeof(aMarker));
6194 AssertRC(rc);
6195 }
6196 }
6197 else if (!fDelete && fFlush)
6198 vmdkFlushImage(pImage, NULL);
6199
6200 if (pImage->pExtents != NULL)
6201 {
6202 for (unsigned i = 0 ; i < pImage->cExtents; i++)
6203 {
6204 int rc2 = vmdkFreeExtentData(pImage, &pImage->pExtents[i], fDelete);
6205 if (RT_SUCCESS(rc))
6206 rc = rc2; /* Propogate any error when closing the file. */
6207 }
6208 RTMemFree(pImage->pExtents);
6209 pImage->pExtents = NULL;
6210 }
6211 pImage->cExtents = 0;
6212 if (pImage->pFile != NULL)
6213 {
6214 int rc2 = vmdkFileClose(pImage, &pImage->pFile, fDelete);
6215 if (RT_SUCCESS(rc))
6216 rc = rc2; /* Propogate any error when closing the file. */
6217 }
6218 int rc2 = vmdkFileCheckAllClose(pImage);
6219 if (RT_SUCCESS(rc))
6220 rc = rc2; /* Propogate any error when closing the file. */
6221
6222 if (pImage->pGTCache)
6223 {
6224 RTMemFree(pImage->pGTCache);
6225 pImage->pGTCache = NULL;
6226 }
6227 if (pImage->pDescData)
6228 {
6229 RTMemFree(pImage->pDescData);
6230 pImage->pDescData = NULL;
6231 }
6232 }
6233
6234 LogFlowFunc(("returns %Rrc\n", rc));
6235 return rc;
6236}
6237
6238/**
6239 * Internal. Flush image data (and metadata) to disk.
6240 */
6241static int vmdkFlushImage(PVMDKIMAGE pImage, PVDIOCTX pIoCtx)
6242{
6243 PVMDKEXTENT pExtent;
6244 int rc = VINF_SUCCESS;
6245
6246 /* Update descriptor if changed. */
6247 if (pImage->Descriptor.fDirty)
6248 rc = vmdkWriteDescriptor(pImage, pIoCtx);
6249
6250 if (RT_SUCCESS(rc))
6251 {
6252 for (unsigned i = 0; i < pImage->cExtents; i++)
6253 {
6254 pExtent = &pImage->pExtents[i];
6255 if (pExtent->pFile != NULL && pExtent->fMetaDirty)
6256 {
6257 switch (pExtent->enmType)
6258 {
6259 case VMDKETYPE_HOSTED_SPARSE:
6260 if (!pExtent->fFooter)
6261 rc = vmdkWriteMetaSparseExtent(pImage, pExtent, 0, pIoCtx);
6262 else
6263 {
6264 uint64_t uFileOffset = pExtent->uAppendPosition;
6265 /* Simply skip writing anything if the streamOptimized
6266 * image hasn't been just created. */
6267 if (!uFileOffset)
6268 break;
6269 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
6270 rc = vmdkWriteMetaSparseExtent(pImage, pExtent,
6271 uFileOffset, pIoCtx);
6272 }
6273 break;
6274 case VMDKETYPE_VMFS:
6275 case VMDKETYPE_FLAT:
6276 /* Nothing to do. */
6277 break;
6278 case VMDKETYPE_ZERO:
6279 default:
6280 AssertMsgFailed(("extent with type %d marked as dirty\n",
6281 pExtent->enmType));
6282 break;
6283 }
6284 }
6285
6286 if (RT_FAILURE(rc))
6287 break;
6288
6289 switch (pExtent->enmType)
6290 {
6291 case VMDKETYPE_HOSTED_SPARSE:
6292 case VMDKETYPE_VMFS:
6293 case VMDKETYPE_FLAT:
6294 /** @todo implement proper path absolute check. */
6295 if ( pExtent->pFile != NULL
6296 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
6297 && !(pExtent->pszBasename[0] == RTPATH_SLASH))
6298 rc = vdIfIoIntFileFlush(pImage->pIfIo, pExtent->pFile->pStorage, pIoCtx,
6299 NULL, NULL);
6300 break;
6301 case VMDKETYPE_ZERO:
6302 /* No need to do anything for this extent. */
6303 break;
6304 default:
6305 AssertMsgFailed(("unknown extent type %d\n", pExtent->enmType));
6306 break;
6307 }
6308 }
6309 }
6310
6311 return rc;
6312}
6313
6314/**
6315 * Internal. Find extent corresponding to the sector number in the disk.
6316 */
6317static int vmdkFindExtent(PVMDKIMAGE pImage, uint64_t offSector,
6318 PVMDKEXTENT *ppExtent, uint64_t *puSectorInExtent)
6319{
6320 PVMDKEXTENT pExtent = NULL;
6321 int rc = VINF_SUCCESS;
6322
6323 for (unsigned i = 0; i < pImage->cExtents; i++)
6324 {
6325 if (offSector < pImage->pExtents[i].cNominalSectors)
6326 {
6327 pExtent = &pImage->pExtents[i];
6328 *puSectorInExtent = offSector + pImage->pExtents[i].uSectorOffset;
6329 break;
6330 }
6331 offSector -= pImage->pExtents[i].cNominalSectors;
6332 }
6333
6334 if (pExtent)
6335 *ppExtent = pExtent;
6336 else
6337 rc = VERR_IO_SECTOR_NOT_FOUND;
6338
6339 return rc;
6340}
6341
6342/**
6343 * Internal. Hash function for placing the grain table hash entries.
6344 */
6345static uint32_t vmdkGTCacheHash(PVMDKGTCACHE pCache, uint64_t uSector,
6346 unsigned uExtent)
6347{
6348 /** @todo this hash function is quite simple, maybe use a better one which
6349 * scrambles the bits better. */
6350 return (uSector + uExtent) % pCache->cEntries;
6351}
6352
6353/**
6354 * Internal. Get sector number in the extent file from the relative sector
6355 * number in the extent.
6356 */
6357static int vmdkGetSector(PVMDKIMAGE pImage, PVDIOCTX pIoCtx,
6358 PVMDKEXTENT pExtent, uint64_t uSector,
6359 uint64_t *puExtentSector)
6360{
6361 PVMDKGTCACHE pCache = pImage->pGTCache;
6362 uint64_t uGDIndex, uGTSector, uGTBlock;
6363 uint32_t uGTHash, uGTBlockIndex;
6364 PVMDKGTCACHEENTRY pGTCacheEntry;
6365 uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
6366 int rc;
6367
6368 /* For newly created and readonly/sequentially opened streamOptimized
6369 * images this must be a no-op, as the grain directory is not there. */
6370 if ( ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
6371 && pExtent->uAppendPosition)
6372 || ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
6373 && pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY
6374 && pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
6375 {
6376 *puExtentSector = 0;
6377 return VINF_SUCCESS;
6378 }
6379
6380 uGDIndex = uSector / pExtent->cSectorsPerGDE;
6381 if (uGDIndex >= pExtent->cGDEntries)
6382 return VERR_OUT_OF_RANGE;
6383 uGTSector = pExtent->pGD[uGDIndex];
6384 if (!uGTSector)
6385 {
6386 /* There is no grain table referenced by this grain directory
6387 * entry. So there is absolutely no data in this area. */
6388 *puExtentSector = 0;
6389 return VINF_SUCCESS;
6390 }
6391
6392 uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
6393 uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
6394 pGTCacheEntry = &pCache->aGTCache[uGTHash];
6395 if ( pGTCacheEntry->uExtent != pExtent->uExtent
6396 || pGTCacheEntry->uGTBlock != uGTBlock)
6397 {
6398 /* Cache miss, fetch data from disk. */
6399 PVDMETAXFER pMetaXfer;
6400 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6401 VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
6402 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx, &pMetaXfer, NULL, NULL);
6403 if (RT_FAILURE(rc))
6404 return rc;
6405 /* We can release the metadata transfer immediately. */
6406 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
6407 pGTCacheEntry->uExtent = pExtent->uExtent;
6408 pGTCacheEntry->uGTBlock = uGTBlock;
6409 for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
6410 pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
6411 }
6412 uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
6413 uint32_t uGrainSector = pGTCacheEntry->aGTData[uGTBlockIndex];
6414 if (uGrainSector)
6415 *puExtentSector = uGrainSector + uSector % pExtent->cSectorsPerGrain;
6416 else
6417 *puExtentSector = 0;
6418 return VINF_SUCCESS;
6419}
6420
6421/**
6422 * Internal. Writes the grain and also if necessary the grain tables.
6423 * Uses the grain table cache as a true grain table.
6424 */
6425static int vmdkStreamAllocGrain(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
6426 uint64_t uSector, PVDIOCTX pIoCtx,
6427 uint64_t cbWrite)
6428{
6429 uint32_t uGrain;
6430 uint32_t uGDEntry, uLastGDEntry;
6431 uint32_t cbGrain = 0;
6432 uint32_t uCacheLine, uCacheEntry;
6433 const void *pData;
6434 int rc;
6435
6436 /* Very strict requirements: always write at least one full grain, with
6437 * proper alignment. Everything else would require reading of already
6438 * written data, which we don't support for obvious reasons. The only
6439 * exception is the last grain, and only if the image size specifies
6440 * that only some portion holds data. In any case the write must be
6441 * within the image limits, no "overshoot" allowed. */
6442 if ( cbWrite == 0
6443 || ( cbWrite < VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
6444 && pExtent->cNominalSectors - uSector >= pExtent->cSectorsPerGrain)
6445 || uSector % pExtent->cSectorsPerGrain
6446 || uSector + VMDK_BYTE2SECTOR(cbWrite) > pExtent->cNominalSectors)
6447 return VERR_INVALID_PARAMETER;
6448
6449 /* Clip write range to at most the rest of the grain. */
6450 cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSector % pExtent->cSectorsPerGrain));
6451
6452 /* Do not allow to go back. */
6453 uGrain = uSector / pExtent->cSectorsPerGrain;
6454 uCacheLine = uGrain % pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE;
6455 uCacheEntry = uGrain % VMDK_GT_CACHELINE_SIZE;
6456 uGDEntry = uGrain / pExtent->cGTEntries;
6457 uLastGDEntry = pExtent->uLastGrainAccess / pExtent->cGTEntries;
6458 if (uGrain < pExtent->uLastGrainAccess)
6459 return VERR_VD_VMDK_INVALID_WRITE;
6460
6461 /* Zero byte write optimization. Since we don't tell VBoxHDD that we need
6462 * to allocate something, we also need to detect the situation ourself. */
6463 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
6464 && vdIfIoIntIoCtxIsZero(pImage->pIfIo, pIoCtx, cbWrite, true /* fAdvance */))
6465 return VINF_SUCCESS;
6466
6467 if (uGDEntry != uLastGDEntry)
6468 {
6469 rc = vmdkStreamFlushGT(pImage, pExtent, uLastGDEntry);
6470 if (RT_FAILURE(rc))
6471 return rc;
6472 vmdkStreamClearGT(pImage, pExtent);
6473 for (uint32_t i = uLastGDEntry + 1; i < uGDEntry; i++)
6474 {
6475 rc = vmdkStreamFlushGT(pImage, pExtent, i);
6476 if (RT_FAILURE(rc))
6477 return rc;
6478 }
6479 }
6480
6481 uint64_t uFileOffset;
6482 uFileOffset = pExtent->uAppendPosition;
6483 if (!uFileOffset)
6484 return VERR_INTERNAL_ERROR;
6485 /* Align to sector, as the previous write could have been any size. */
6486 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
6487
6488 /* Paranoia check: extent type, grain table buffer presence and
6489 * grain table buffer space. Also grain table entry must be clear. */
6490 if ( pExtent->enmType != VMDKETYPE_HOSTED_SPARSE
6491 || !pImage->pGTCache
6492 || pExtent->cGTEntries > VMDK_GT_CACHE_SIZE * VMDK_GT_CACHELINE_SIZE
6493 || pImage->pGTCache->aGTCache[uCacheLine].aGTData[uCacheEntry])
6494 return VERR_INTERNAL_ERROR;
6495
6496 /* Update grain table entry. */
6497 pImage->pGTCache->aGTCache[uCacheLine].aGTData[uCacheEntry] = VMDK_BYTE2SECTOR(uFileOffset);
6498
6499 if (cbWrite != VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
6500 {
6501 vdIfIoIntIoCtxCopyFrom(pImage->pIfIo, pIoCtx, pExtent->pvGrain, cbWrite);
6502 memset((char *)pExtent->pvGrain + cbWrite, '\0',
6503 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain) - cbWrite);
6504 pData = pExtent->pvGrain;
6505 }
6506 else
6507 {
6508 RTSGSEG Segment;
6509 unsigned cSegments = 1;
6510 size_t cbSeg = 0;
6511
6512 cbSeg = vdIfIoIntIoCtxSegArrayCreate(pImage->pIfIo, pIoCtx, &Segment,
6513 &cSegments, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
6514 Assert(cbSeg == VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
6515 pData = Segment.pvSeg;
6516 }
6517 rc = vmdkFileDeflateSync(pImage, pExtent, uFileOffset, pData,
6518 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
6519 uSector, &cbGrain);
6520 if (RT_FAILURE(rc))
6521 {
6522 pExtent->uGrainSectorAbs = 0;
6523 AssertRC(rc);
6524 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write compressed data block in '%s'"), pExtent->pszFullname);
6525 }
6526 pExtent->uLastGrainAccess = uGrain;
6527 pExtent->uAppendPosition += cbGrain;
6528
6529 return rc;
6530}
6531
6532/**
6533 * Internal: Updates the grain table during grain allocation.
6534 */
6535static int vmdkAllocGrainGTUpdate(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, PVDIOCTX pIoCtx,
6536 PVMDKGRAINALLOCASYNC pGrainAlloc)
6537{
6538 int rc = VINF_SUCCESS;
6539 PVMDKGTCACHE pCache = pImage->pGTCache;
6540 uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
6541 uint32_t uGTHash, uGTBlockIndex;
6542 uint64_t uGTSector, uRGTSector, uGTBlock;
6543 uint64_t uSector = pGrainAlloc->uSector;
6544 PVMDKGTCACHEENTRY pGTCacheEntry;
6545
6546 LogFlowFunc(("pImage=%#p pExtent=%#p pCache=%#p pIoCtx=%#p pGrainAlloc=%#p\n",
6547 pImage, pExtent, pCache, pIoCtx, pGrainAlloc));
6548
6549 uGTSector = pGrainAlloc->uGTSector;
6550 uRGTSector = pGrainAlloc->uRGTSector;
6551 LogFlow(("uGTSector=%llu uRGTSector=%llu\n", uGTSector, uRGTSector));
6552
6553 /* Update the grain table (and the cache). */
6554 uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
6555 uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
6556 pGTCacheEntry = &pCache->aGTCache[uGTHash];
6557 if ( pGTCacheEntry->uExtent != pExtent->uExtent
6558 || pGTCacheEntry->uGTBlock != uGTBlock)
6559 {
6560 /* Cache miss, fetch data from disk. */
6561 LogFlow(("Cache miss, fetch data from disk\n"));
6562 PVDMETAXFER pMetaXfer = NULL;
6563 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6564 VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
6565 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
6566 &pMetaXfer, vmdkAllocGrainComplete, pGrainAlloc);
6567 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6568 {
6569 pGrainAlloc->cIoXfersPending++;
6570 pGrainAlloc->fGTUpdateNeeded = true;
6571 /* Leave early, we will be called again after the read completed. */
6572 LogFlowFunc(("Metadata read in progress, leaving\n"));
6573 return rc;
6574 }
6575 else if (RT_FAILURE(rc))
6576 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot read allocated grain table entry in '%s'"), pExtent->pszFullname);
6577 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
6578 pGTCacheEntry->uExtent = pExtent->uExtent;
6579 pGTCacheEntry->uGTBlock = uGTBlock;
6580 for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
6581 pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
6582 }
6583 else
6584 {
6585 /* Cache hit. Convert grain table block back to disk format, otherwise
6586 * the code below will write garbage for all but the updated entry. */
6587 for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
6588 aGTDataTmp[i] = RT_H2LE_U32(pGTCacheEntry->aGTData[i]);
6589 }
6590 pGrainAlloc->fGTUpdateNeeded = false;
6591 uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
6592 aGTDataTmp[uGTBlockIndex] = RT_H2LE_U32(VMDK_BYTE2SECTOR(pGrainAlloc->uGrainOffset));
6593 pGTCacheEntry->aGTData[uGTBlockIndex] = VMDK_BYTE2SECTOR(pGrainAlloc->uGrainOffset);
6594 /* Update grain table on disk. */
6595 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6596 VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
6597 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
6598 vmdkAllocGrainComplete, pGrainAlloc);
6599 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6600 pGrainAlloc->cIoXfersPending++;
6601 else if (RT_FAILURE(rc))
6602 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated grain table in '%s'"), pExtent->pszFullname);
6603 if (pExtent->pRGD)
6604 {
6605 /* Update backup grain table on disk. */
6606 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6607 VMDK_SECTOR2BYTE(uRGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
6608 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
6609 vmdkAllocGrainComplete, pGrainAlloc);
6610 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6611 pGrainAlloc->cIoXfersPending++;
6612 else if (RT_FAILURE(rc))
6613 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated backup grain table in '%s'"), pExtent->pszFullname);
6614 }
6615
6616 LogFlowFunc(("leaving rc=%Rrc\n", rc));
6617 return rc;
6618}
6619
6620/**
6621 * Internal - complete the grain allocation by updating disk grain table if required.
6622 */
6623static DECLCALLBACK(int) vmdkAllocGrainComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
6624{
6625 RT_NOREF1(rcReq);
6626 int rc = VINF_SUCCESS;
6627 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6628 PVMDKGRAINALLOCASYNC pGrainAlloc = (PVMDKGRAINALLOCASYNC)pvUser;
6629
6630 LogFlowFunc(("pBackendData=%#p pIoCtx=%#p pvUser=%#p rcReq=%Rrc\n",
6631 pBackendData, pIoCtx, pvUser, rcReq));
6632
6633 pGrainAlloc->cIoXfersPending--;
6634 if (!pGrainAlloc->cIoXfersPending && pGrainAlloc->fGTUpdateNeeded)
6635 rc = vmdkAllocGrainGTUpdate(pImage, pGrainAlloc->pExtent, pIoCtx, pGrainAlloc);
6636
6637 if (!pGrainAlloc->cIoXfersPending)
6638 {
6639 /* Grain allocation completed. */
6640 RTMemFree(pGrainAlloc);
6641 }
6642
6643 LogFlowFunc(("Leaving rc=%Rrc\n", rc));
6644 return rc;
6645}
6646
6647/**
6648 * Internal. Allocates a new grain table (if necessary).
6649 */
6650static int vmdkAllocGrain(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, PVDIOCTX pIoCtx,
6651 uint64_t uSector, uint64_t cbWrite)
6652{
6653 PVMDKGTCACHE pCache = pImage->pGTCache; NOREF(pCache);
6654 uint64_t uGDIndex, uGTSector, uRGTSector;
6655 uint64_t uFileOffset;
6656 PVMDKGRAINALLOCASYNC pGrainAlloc = NULL;
6657 int rc;
6658
6659 LogFlowFunc(("pCache=%#p pExtent=%#p pIoCtx=%#p uSector=%llu cbWrite=%llu\n",
6660 pCache, pExtent, pIoCtx, uSector, cbWrite));
6661
6662 pGrainAlloc = (PVMDKGRAINALLOCASYNC)RTMemAllocZ(sizeof(VMDKGRAINALLOCASYNC));
6663 if (!pGrainAlloc)
6664 return VERR_NO_MEMORY;
6665
6666 pGrainAlloc->pExtent = pExtent;
6667 pGrainAlloc->uSector = uSector;
6668
6669 uGDIndex = uSector / pExtent->cSectorsPerGDE;
6670 if (uGDIndex >= pExtent->cGDEntries)
6671 {
6672 RTMemFree(pGrainAlloc);
6673 return VERR_OUT_OF_RANGE;
6674 }
6675 uGTSector = pExtent->pGD[uGDIndex];
6676 if (pExtent->pRGD)
6677 uRGTSector = pExtent->pRGD[uGDIndex];
6678 else
6679 uRGTSector = 0; /**< avoid compiler warning */
6680 if (!uGTSector)
6681 {
6682 LogFlow(("Allocating new grain table\n"));
6683
6684 /* There is no grain table referenced by this grain directory
6685 * entry. So there is absolutely no data in this area. Allocate
6686 * a new grain table and put the reference to it in the GDs. */
6687 uFileOffset = pExtent->uAppendPosition;
6688 if (!uFileOffset)
6689 {
6690 RTMemFree(pGrainAlloc);
6691 return VERR_INTERNAL_ERROR;
6692 }
6693 Assert(!(uFileOffset % 512));
6694
6695 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
6696 uGTSector = VMDK_BYTE2SECTOR(uFileOffset);
6697
6698 /* Normally the grain table is preallocated for hosted sparse extents
6699 * that support more than 32 bit sector numbers. So this shouldn't
6700 * ever happen on a valid extent. */
6701 if (uGTSector > UINT32_MAX)
6702 {
6703 RTMemFree(pGrainAlloc);
6704 return VERR_VD_VMDK_INVALID_HEADER;
6705 }
6706
6707 /* Write grain table by writing the required number of grain table
6708 * cache chunks. Allocate memory dynamically here or we flood the
6709 * metadata cache with very small entries. */
6710 size_t cbGTDataTmp = pExtent->cGTEntries * sizeof(uint32_t);
6711 uint32_t *paGTDataTmp = (uint32_t *)RTMemTmpAllocZ(cbGTDataTmp);
6712
6713 if (!paGTDataTmp)
6714 {
6715 RTMemFree(pGrainAlloc);
6716 return VERR_NO_MEMORY;
6717 }
6718
6719 memset(paGTDataTmp, '\0', cbGTDataTmp);
6720 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6721 VMDK_SECTOR2BYTE(uGTSector),
6722 paGTDataTmp, cbGTDataTmp, pIoCtx,
6723 vmdkAllocGrainComplete, pGrainAlloc);
6724 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6725 pGrainAlloc->cIoXfersPending++;
6726 else if (RT_FAILURE(rc))
6727 {
6728 RTMemTmpFree(paGTDataTmp);
6729 RTMemFree(pGrainAlloc);
6730 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain table allocation in '%s'"), pExtent->pszFullname);
6731 }
6732 pExtent->uAppendPosition = RT_ALIGN_64( pExtent->uAppendPosition
6733 + cbGTDataTmp, 512);
6734
6735 if (pExtent->pRGD)
6736 {
6737 AssertReturn(!uRGTSector, VERR_VD_VMDK_INVALID_HEADER);
6738 uFileOffset = pExtent->uAppendPosition;
6739 if (!uFileOffset)
6740 return VERR_INTERNAL_ERROR;
6741 Assert(!(uFileOffset % 512));
6742 uRGTSector = VMDK_BYTE2SECTOR(uFileOffset);
6743
6744 /* Normally the redundant grain table is preallocated for hosted
6745 * sparse extents that support more than 32 bit sector numbers. So
6746 * this shouldn't ever happen on a valid extent. */
6747 if (uRGTSector > UINT32_MAX)
6748 {
6749 RTMemTmpFree(paGTDataTmp);
6750 return VERR_VD_VMDK_INVALID_HEADER;
6751 }
6752
6753 /* Write grain table by writing the required number of grain table
6754 * cache chunks. Allocate memory dynamically here or we flood the
6755 * metadata cache with very small entries. */
6756 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6757 VMDK_SECTOR2BYTE(uRGTSector),
6758 paGTDataTmp, cbGTDataTmp, pIoCtx,
6759 vmdkAllocGrainComplete, pGrainAlloc);
6760 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6761 pGrainAlloc->cIoXfersPending++;
6762 else if (RT_FAILURE(rc))
6763 {
6764 RTMemTmpFree(paGTDataTmp);
6765 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain table allocation in '%s'"), pExtent->pszFullname);
6766 }
6767
6768 pExtent->uAppendPosition = pExtent->uAppendPosition + cbGTDataTmp;
6769 }
6770
6771 RTMemTmpFree(paGTDataTmp);
6772
6773 /* Update the grain directory on disk (doing it before writing the
6774 * grain table will result in a garbled extent if the operation is
6775 * aborted for some reason. Otherwise the worst that can happen is
6776 * some unused sectors in the extent. */
6777 uint32_t uGTSectorLE = RT_H2LE_U64(uGTSector);
6778 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6779 VMDK_SECTOR2BYTE(pExtent->uSectorGD) + uGDIndex * sizeof(uGTSectorLE),
6780 &uGTSectorLE, sizeof(uGTSectorLE), pIoCtx,
6781 vmdkAllocGrainComplete, pGrainAlloc);
6782 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6783 pGrainAlloc->cIoXfersPending++;
6784 else if (RT_FAILURE(rc))
6785 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain directory entry in '%s'"), pExtent->pszFullname);
6786 if (pExtent->pRGD)
6787 {
6788 uint32_t uRGTSectorLE = RT_H2LE_U64(uRGTSector);
6789 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6790 VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + uGDIndex * sizeof(uGTSectorLE),
6791 &uRGTSectorLE, sizeof(uRGTSectorLE), pIoCtx,
6792 vmdkAllocGrainComplete, pGrainAlloc);
6793 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6794 pGrainAlloc->cIoXfersPending++;
6795 else if (RT_FAILURE(rc))
6796 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain directory entry in '%s'"), pExtent->pszFullname);
6797 }
6798
6799 /* As the final step update the in-memory copy of the GDs. */
6800 pExtent->pGD[uGDIndex] = uGTSector;
6801 if (pExtent->pRGD)
6802 pExtent->pRGD[uGDIndex] = uRGTSector;
6803 }
6804
6805 LogFlow(("uGTSector=%llu uRGTSector=%llu\n", uGTSector, uRGTSector));
6806 pGrainAlloc->uGTSector = uGTSector;
6807 pGrainAlloc->uRGTSector = uRGTSector;
6808
6809 uFileOffset = pExtent->uAppendPosition;
6810 if (!uFileOffset)
6811 return VERR_INTERNAL_ERROR;
6812 Assert(!(uFileOffset % 512));
6813
6814 pGrainAlloc->uGrainOffset = uFileOffset;
6815
6816 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
6817 {
6818 AssertMsgReturn(vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx),
6819 ("Accesses to stream optimized images must be synchronous\n"),
6820 VERR_INVALID_STATE);
6821
6822 if (cbWrite != VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
6823 return vdIfError(pImage->pIfError, VERR_INTERNAL_ERROR, RT_SRC_POS, N_("VMDK: not enough data for a compressed data block in '%s'"), pExtent->pszFullname);
6824
6825 /* Invalidate cache, just in case some code incorrectly allows mixing
6826 * of reads and writes. Normally shouldn't be needed. */
6827 pExtent->uGrainSectorAbs = 0;
6828
6829 /* Write compressed data block and the markers. */
6830 uint32_t cbGrain = 0;
6831 size_t cbSeg = 0;
6832 RTSGSEG Segment;
6833 unsigned cSegments = 1;
6834
6835 cbSeg = vdIfIoIntIoCtxSegArrayCreate(pImage->pIfIo, pIoCtx, &Segment,
6836 &cSegments, cbWrite);
6837 Assert(cbSeg == cbWrite);
6838
6839 rc = vmdkFileDeflateSync(pImage, pExtent, uFileOffset,
6840 Segment.pvSeg, cbWrite, uSector, &cbGrain);
6841 if (RT_FAILURE(rc))
6842 {
6843 AssertRC(rc);
6844 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write allocated compressed data block in '%s'"), pExtent->pszFullname);
6845 }
6846 pExtent->uLastGrainAccess = uSector / pExtent->cSectorsPerGrain;
6847 pExtent->uAppendPosition += cbGrain;
6848 }
6849 else
6850 {
6851 /* Write the data. Always a full grain, or we're in big trouble. */
6852 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pExtent->pFile->pStorage,
6853 uFileOffset, pIoCtx, cbWrite,
6854 vmdkAllocGrainComplete, pGrainAlloc);
6855 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6856 pGrainAlloc->cIoXfersPending++;
6857 else if (RT_FAILURE(rc))
6858 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write allocated data block in '%s'"), pExtent->pszFullname);
6859
6860 pExtent->uAppendPosition += cbWrite;
6861 }
6862
6863 rc = vmdkAllocGrainGTUpdate(pImage, pExtent, pIoCtx, pGrainAlloc);
6864
6865 if (!pGrainAlloc->cIoXfersPending)
6866 {
6867 /* Grain allocation completed. */
6868 RTMemFree(pGrainAlloc);
6869 }
6870
6871 LogFlowFunc(("leaving rc=%Rrc\n", rc));
6872
6873 return rc;
6874}
6875
6876/**
6877 * Internal. Reads the contents by sequentially going over the compressed
6878 * grains (hoping that they are in sequence).
6879 */
6880static int vmdkStreamReadSequential(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
6881 uint64_t uSector, PVDIOCTX pIoCtx,
6882 uint64_t cbRead)
6883{
6884 int rc;
6885
6886 LogFlowFunc(("pImage=%#p pExtent=%#p uSector=%llu pIoCtx=%#p cbRead=%llu\n",
6887 pImage, pExtent, uSector, pIoCtx, cbRead));
6888
6889 AssertMsgReturn(vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx),
6890 ("Async I/O not supported for sequential stream optimized images\n"),
6891 VERR_INVALID_STATE);
6892
6893 /* Do not allow to go back. */
6894 uint32_t uGrain = uSector / pExtent->cSectorsPerGrain;
6895 if (uGrain < pExtent->uLastGrainAccess)
6896 return VERR_VD_VMDK_INVALID_STATE;
6897 pExtent->uLastGrainAccess = uGrain;
6898
6899 /* After a previous error do not attempt to recover, as it would need
6900 * seeking (in the general case backwards which is forbidden). */
6901 if (!pExtent->uGrainSectorAbs)
6902 return VERR_VD_VMDK_INVALID_STATE;
6903
6904 /* Check if we need to read something from the image or if what we have
6905 * in the buffer is good to fulfill the request. */
6906 if (!pExtent->cbGrainStreamRead || uGrain > pExtent->uGrain)
6907 {
6908 uint32_t uGrainSectorAbs = pExtent->uGrainSectorAbs
6909 + VMDK_BYTE2SECTOR(pExtent->cbGrainStreamRead);
6910
6911 /* Get the marker from the next data block - and skip everything which
6912 * is not a compressed grain. If it's a compressed grain which is for
6913 * the requested sector (or after), read it. */
6914 VMDKMARKER Marker;
6915 do
6916 {
6917 RT_ZERO(Marker);
6918 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
6919 VMDK_SECTOR2BYTE(uGrainSectorAbs),
6920 &Marker, RT_UOFFSETOF(VMDKMARKER, uType));
6921 if (RT_FAILURE(rc))
6922 return rc;
6923 Marker.uSector = RT_LE2H_U64(Marker.uSector);
6924 Marker.cbSize = RT_LE2H_U32(Marker.cbSize);
6925
6926 if (Marker.cbSize == 0)
6927 {
6928 /* A marker for something else than a compressed grain. */
6929 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
6930 VMDK_SECTOR2BYTE(uGrainSectorAbs)
6931 + RT_UOFFSETOF(VMDKMARKER, uType),
6932 &Marker.uType, sizeof(Marker.uType));
6933 if (RT_FAILURE(rc))
6934 return rc;
6935 Marker.uType = RT_LE2H_U32(Marker.uType);
6936 switch (Marker.uType)
6937 {
6938 case VMDK_MARKER_EOS:
6939 uGrainSectorAbs++;
6940 /* Read (or mostly skip) to the end of file. Uses the
6941 * Marker (LBA sector) as it is unused anyway. This
6942 * makes sure that really everything is read in the
6943 * success case. If this read fails it means the image
6944 * is truncated, but this is harmless so ignore. */
6945 vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
6946 VMDK_SECTOR2BYTE(uGrainSectorAbs)
6947 + 511,
6948 &Marker.uSector, 1);
6949 break;
6950 case VMDK_MARKER_GT:
6951 uGrainSectorAbs += 1 + VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
6952 break;
6953 case VMDK_MARKER_GD:
6954 uGrainSectorAbs += 1 + VMDK_BYTE2SECTOR(RT_ALIGN(pExtent->cGDEntries * sizeof(uint32_t), 512));
6955 break;
6956 case VMDK_MARKER_FOOTER:
6957 uGrainSectorAbs += 2;
6958 break;
6959 case VMDK_MARKER_UNSPECIFIED:
6960 /* Skip over the contents of the unspecified marker
6961 * type 4 which exists in some vSphere created files. */
6962 /** @todo figure out what the payload means. */
6963 uGrainSectorAbs += 1;
6964 break;
6965 default:
6966 AssertMsgFailed(("VMDK: corrupted marker, type=%#x\n", Marker.uType));
6967 pExtent->uGrainSectorAbs = 0;
6968 return VERR_VD_VMDK_INVALID_STATE;
6969 }
6970 pExtent->cbGrainStreamRead = 0;
6971 }
6972 else
6973 {
6974 /* A compressed grain marker. If it is at/after what we're
6975 * interested in read and decompress data. */
6976 if (uSector > Marker.uSector + pExtent->cSectorsPerGrain)
6977 {
6978 uGrainSectorAbs += VMDK_BYTE2SECTOR(RT_ALIGN(Marker.cbSize + RT_UOFFSETOF(VMDKMARKER, uType), 512));
6979 continue;
6980 }
6981 uint64_t uLBA = 0;
6982 uint32_t cbGrainStreamRead = 0;
6983 rc = vmdkFileInflateSync(pImage, pExtent,
6984 VMDK_SECTOR2BYTE(uGrainSectorAbs),
6985 pExtent->pvGrain,
6986 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
6987 &Marker, &uLBA, &cbGrainStreamRead);
6988 if (RT_FAILURE(rc))
6989 {
6990 pExtent->uGrainSectorAbs = 0;
6991 return rc;
6992 }
6993 if ( pExtent->uGrain
6994 && uLBA / pExtent->cSectorsPerGrain <= pExtent->uGrain)
6995 {
6996 pExtent->uGrainSectorAbs = 0;
6997 return VERR_VD_VMDK_INVALID_STATE;
6998 }
6999 pExtent->uGrain = uLBA / pExtent->cSectorsPerGrain;
7000 pExtent->cbGrainStreamRead = cbGrainStreamRead;
7001 break;
7002 }
7003 } while (Marker.uType != VMDK_MARKER_EOS);
7004
7005 pExtent->uGrainSectorAbs = uGrainSectorAbs;
7006
7007 if (!pExtent->cbGrainStreamRead && Marker.uType == VMDK_MARKER_EOS)
7008 {
7009 pExtent->uGrain = UINT32_MAX;
7010 /* Must set a non-zero value for pExtent->cbGrainStreamRead or
7011 * the next read would try to get more data, and we're at EOF. */
7012 pExtent->cbGrainStreamRead = 1;
7013 }
7014 }
7015
7016 if (pExtent->uGrain > uSector / pExtent->cSectorsPerGrain)
7017 {
7018 /* The next data block we have is not for this area, so just return
7019 * that there is no data. */
7020 LogFlowFunc(("returns VERR_VD_BLOCK_FREE\n"));
7021 return VERR_VD_BLOCK_FREE;
7022 }
7023
7024 uint32_t uSectorInGrain = uSector % pExtent->cSectorsPerGrain;
7025 vdIfIoIntIoCtxCopyTo(pImage->pIfIo, pIoCtx,
7026 (uint8_t *)pExtent->pvGrain + VMDK_SECTOR2BYTE(uSectorInGrain),
7027 cbRead);
7028 LogFlowFunc(("returns VINF_SUCCESS\n"));
7029 return VINF_SUCCESS;
7030}
7031
7032/**
7033 * Replaces a fragment of a string with the specified string.
7034 *
7035 * @returns Pointer to the allocated UTF-8 string.
7036 * @param pszWhere UTF-8 string to search in.
7037 * @param pszWhat UTF-8 string to search for.
7038 * @param pszByWhat UTF-8 string to replace the found string with.
7039 *
7040 * @note r=bird: This is only used by vmdkRenameWorker(). The first use is
7041 * for updating the base name in the descriptor, the second is for
7042 * generating new filenames for extents. This code borked when
7043 * RTPathAbs started correcting the driver letter case on windows,
7044 * when strstr failed because the pExtent->pszFullname was not
7045 * subjected to RTPathAbs but while pExtent->pszFullname was. I fixed
7046 * this by apply RTPathAbs to the places it wasn't applied.
7047 *
7048 * However, this highlights some undocumented ASSUMPTIONS as well as
7049 * terrible short commings of the approach.
7050 *
7051 * Given the right filename, it may also screw up the descriptor. Take
7052 * the descriptor text 'RW 2048 SPARSE "Test0.vmdk"' for instance,
7053 * we'll be asked to replace "Test0" with something, no problem. No,
7054 * imagine 'RW 2048 SPARSE "SPARSE.vmdk"', 'RW 2048 SPARSE "RW.vmdk"'
7055 * or 'RW 2048 SPARSE "2048.vmdk"', and the strstr approach falls on
7056 * its bum. The descriptor string must be parsed and reconstructed,
7057 * the lazy strstr approach doesn't cut it.
7058 *
7059 * I'm also curious as to what would be the correct escaping of '"' in
7060 * the file name and how that is supposed to be handled, because it
7061 * needs to be or such names must be rejected in several places (maybe
7062 * they are, I didn't check).
7063 *
7064 * When this function is used to replace the start of a path, I think
7065 * the assumption from the prep/setup code is that we kind of knows
7066 * what we're working on (I could be wrong). However, using strstr
7067 * instead of strncmp/RTStrNICmp makes no sense and isn't future proof.
7068 * Especially on unix systems, weird stuff could happen if someone
7069 * unwittingly tinkers with the prep/setup code. What should really be
7070 * done here is using a new RTPathStartEx function that (via flags)
7071 * allows matching partial final component and returns the length of
7072 * what it matched up (in case it skipped slashes and '.' components).
7073 *
7074 */
7075static char *vmdkStrReplace(const char *pszWhere, const char *pszWhat,
7076 const char *pszByWhat)
7077{
7078 AssertPtr(pszWhere);
7079 AssertPtr(pszWhat);
7080 AssertPtr(pszByWhat);
7081 const char *pszFoundStr = strstr(pszWhere, pszWhat);
7082 if (!pszFoundStr)
7083 {
7084 LogFlowFunc(("Failed to find '%s' in '%s'!\n", pszWhat, pszWhere));
7085 return NULL;
7086 }
7087 size_t cbFinal = strlen(pszWhere) + 1 + strlen(pszByWhat) - strlen(pszWhat);
7088 char *pszNewStr = RTStrAlloc(cbFinal);
7089 if (pszNewStr)
7090 {
7091 char *pszTmp = pszNewStr;
7092 memcpy(pszTmp, pszWhere, pszFoundStr - pszWhere);
7093 pszTmp += pszFoundStr - pszWhere;
7094 memcpy(pszTmp, pszByWhat, strlen(pszByWhat));
7095 pszTmp += strlen(pszByWhat);
7096 strcpy(pszTmp, pszFoundStr + strlen(pszWhat));
7097 }
7098 return pszNewStr;
7099}
7100
7101
7102/** @copydoc VDIMAGEBACKEND::pfnProbe */
7103static DECLCALLBACK(int) vmdkProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
7104 PVDINTERFACE pVDIfsImage, VDTYPE enmDesiredType, VDTYPE *penmType)
7105{
7106 RT_NOREF(enmDesiredType);
7107 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p penmType=%#p\n",
7108 pszFilename, pVDIfsDisk, pVDIfsImage, penmType));
7109 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
7110 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
7111
7112 int rc = VINF_SUCCESS;
7113 PVMDKIMAGE pImage = (PVMDKIMAGE)RTMemAllocZ(RT_UOFFSETOF(VMDKIMAGE, RegionList.aRegions[1]));
7114 if (RT_LIKELY(pImage))
7115 {
7116 pImage->pszFilename = pszFilename;
7117 pImage->pFile = NULL;
7118 pImage->pExtents = NULL;
7119 pImage->pFiles = NULL;
7120 pImage->pGTCache = NULL;
7121 pImage->pDescData = NULL;
7122 pImage->pVDIfsDisk = pVDIfsDisk;
7123 pImage->pVDIfsImage = pVDIfsImage;
7124 /** @todo speed up this test open (VD_OPEN_FLAGS_INFO) by skipping as
7125 * much as possible in vmdkOpenImage. */
7126 rc = vmdkOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
7127 vmdkFreeImage(pImage, false, false /*fFlush*/);
7128 RTMemFree(pImage);
7129
7130 if (RT_SUCCESS(rc))
7131 *penmType = VDTYPE_HDD;
7132 }
7133 else
7134 rc = VERR_NO_MEMORY;
7135
7136 LogFlowFunc(("returns %Rrc\n", rc));
7137 return rc;
7138}
7139
7140/** @copydoc VDIMAGEBACKEND::pfnOpen */
7141static DECLCALLBACK(int) vmdkOpen(const char *pszFilename, unsigned uOpenFlags,
7142 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
7143 VDTYPE enmType, void **ppBackendData)
7144{
7145 RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
7146
7147 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
7148 pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
7149 int rc;
7150
7151 /* Check open flags. All valid flags are supported. */
7152 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
7153 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
7154 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
7155
7156
7157 PVMDKIMAGE pImage = (PVMDKIMAGE)RTMemAllocZ(RT_UOFFSETOF(VMDKIMAGE, RegionList.aRegions[1]));
7158 if (RT_LIKELY(pImage))
7159 {
7160 pImage->pszFilename = pszFilename;
7161 pImage->pFile = NULL;
7162 pImage->pExtents = NULL;
7163 pImage->pFiles = NULL;
7164 pImage->pGTCache = NULL;
7165 pImage->pDescData = NULL;
7166 pImage->pVDIfsDisk = pVDIfsDisk;
7167 pImage->pVDIfsImage = pVDIfsImage;
7168
7169 rc = vmdkOpenImage(pImage, uOpenFlags);
7170 if (RT_SUCCESS(rc))
7171 *ppBackendData = pImage;
7172 else
7173 RTMemFree(pImage);
7174 }
7175 else
7176 rc = VERR_NO_MEMORY;
7177
7178 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
7179 return rc;
7180}
7181
7182/** @copydoc VDIMAGEBACKEND::pfnCreate */
7183static DECLCALLBACK(int) vmdkCreate(const char *pszFilename, uint64_t cbSize,
7184 unsigned uImageFlags, const char *pszComment,
7185 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
7186 PCRTUUID pUuid, unsigned uOpenFlags,
7187 unsigned uPercentStart, unsigned uPercentSpan,
7188 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
7189 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
7190 void **ppBackendData)
7191{
7192 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p enmType=%u ppBackendData=%#p\n",
7193 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
7194 int rc;
7195
7196 /* Check the VD container type and image flags. */
7197 if ( enmType != VDTYPE_HDD
7198 || (uImageFlags & ~VD_VMDK_IMAGE_FLAGS_MASK) != 0)
7199 return VERR_VD_INVALID_TYPE;
7200
7201 /* Check size. Maximum 256TB-64K for sparse images, otherwise unlimited. */
7202 if ( !(uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK)
7203 && ( !cbSize
7204 || (!(uImageFlags & VD_IMAGE_FLAGS_FIXED) && cbSize >= _1T * 256 - _64K)))
7205 return VERR_VD_INVALID_SIZE;
7206
7207 /* Check image flags for invalid combinations. */
7208 if ( (uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7209 && (uImageFlags & ~(VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED | VD_IMAGE_FLAGS_DIFF)))
7210 return VERR_INVALID_PARAMETER;
7211
7212 /* Check open flags. All valid flags are supported. */
7213 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
7214 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
7215 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
7216 AssertPtrReturn(pPCHSGeometry, VERR_INVALID_POINTER);
7217 AssertPtrReturn(pLCHSGeometry, VERR_INVALID_POINTER);
7218 AssertReturn(!( uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX
7219 && !(uImageFlags & VD_IMAGE_FLAGS_FIXED)),
7220 VERR_INVALID_PARAMETER);
7221
7222 PVMDKIMAGE pImage = (PVMDKIMAGE)RTMemAllocZ(RT_UOFFSETOF(VMDKIMAGE, RegionList.aRegions[1]));
7223 if (RT_LIKELY(pImage))
7224 {
7225 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
7226
7227 pImage->pszFilename = pszFilename;
7228 pImage->pFile = NULL;
7229 pImage->pExtents = NULL;
7230 pImage->pFiles = NULL;
7231 pImage->pGTCache = NULL;
7232 pImage->pDescData = NULL;
7233 pImage->pVDIfsDisk = pVDIfsDisk;
7234 pImage->pVDIfsImage = pVDIfsImage;
7235 /* Descriptors for split images can be pretty large, especially if the
7236 * filename is long. So prepare for the worst, and allocate quite some
7237 * memory for the descriptor in this case. */
7238 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
7239 pImage->cbDescAlloc = VMDK_SECTOR2BYTE(200);
7240 else
7241 pImage->cbDescAlloc = VMDK_SECTOR2BYTE(20);
7242 pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
7243 if (RT_LIKELY(pImage->pDescData))
7244 {
7245 rc = vmdkCreateImage(pImage, cbSize, uImageFlags, pszComment,
7246 pPCHSGeometry, pLCHSGeometry, pUuid,
7247 pIfProgress, uPercentStart, uPercentSpan);
7248 if (RT_SUCCESS(rc))
7249 {
7250 /* So far the image is opened in read/write mode. Make sure the
7251 * image is opened in read-only mode if the caller requested that. */
7252 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
7253 {
7254 vmdkFreeImage(pImage, false, true /*fFlush*/);
7255 rc = vmdkOpenImage(pImage, uOpenFlags);
7256 }
7257
7258 if (RT_SUCCESS(rc))
7259 *ppBackendData = pImage;
7260 }
7261
7262 if (RT_FAILURE(rc))
7263 RTMemFree(pImage->pDescData);
7264 }
7265 else
7266 rc = VERR_NO_MEMORY;
7267
7268 if (RT_FAILURE(rc))
7269 RTMemFree(pImage);
7270 }
7271 else
7272 rc = VERR_NO_MEMORY;
7273
7274 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
7275 return rc;
7276}
7277
7278/**
7279 * Prepares the state for renaming a VMDK image, setting up the state and allocating
7280 * memory.
7281 *
7282 * @returns VBox status code.
7283 * @param pImage VMDK image instance.
7284 * @param pRenameState The state to initialize.
7285 * @param pszFilename The new filename.
7286 */
7287static int vmdkRenameStatePrepare(PVMDKIMAGE pImage, PVMDKRENAMESTATE pRenameState, const char *pszFilename)
7288{
7289 AssertReturn(RTPathFilename(pszFilename) != NULL, VERR_INVALID_PARAMETER);
7290
7291 int rc = VINF_SUCCESS;
7292
7293 memset(&pRenameState->DescriptorCopy, 0, sizeof(pRenameState->DescriptorCopy));
7294
7295 /*
7296 * Allocate an array to store both old and new names of renamed files
7297 * in case we have to roll back the changes. Arrays are initialized
7298 * with zeros. We actually save stuff when and if we change it.
7299 */
7300 pRenameState->cExtents = pImage->cExtents;
7301 pRenameState->apszOldName = (char **)RTMemTmpAllocZ((pRenameState->cExtents + 1) * sizeof(char *));
7302 pRenameState->apszNewName = (char **)RTMemTmpAllocZ((pRenameState->cExtents + 1) * sizeof(char *));
7303 pRenameState->apszNewLines = (char **)RTMemTmpAllocZ(pRenameState->cExtents * sizeof(char *));
7304 if ( pRenameState->apszOldName
7305 && pRenameState->apszNewName
7306 && pRenameState->apszNewLines)
7307 {
7308 /* Save the descriptor size and position. */
7309 if (pImage->pDescData)
7310 {
7311 /* Separate descriptor file. */
7312 pRenameState->fEmbeddedDesc = false;
7313 }
7314 else
7315 {
7316 /* Embedded descriptor file. */
7317 pRenameState->ExtentCopy = pImage->pExtents[0];
7318 pRenameState->fEmbeddedDesc = true;
7319 }
7320
7321 /* Save the descriptor content. */
7322 pRenameState->DescriptorCopy.cLines = pImage->Descriptor.cLines;
7323 for (unsigned i = 0; i < pRenameState->DescriptorCopy.cLines; i++)
7324 {
7325 pRenameState->DescriptorCopy.aLines[i] = RTStrDup(pImage->Descriptor.aLines[i]);
7326 if (!pRenameState->DescriptorCopy.aLines[i])
7327 {
7328 rc = VERR_NO_MEMORY;
7329 break;
7330 }
7331 }
7332
7333 if (RT_SUCCESS(rc))
7334 {
7335 /* Prepare both old and new base names used for string replacement. */
7336 pRenameState->pszNewBaseName = RTStrDup(RTPathFilename(pszFilename));
7337 AssertReturn(pRenameState->pszNewBaseName, VERR_NO_STR_MEMORY);
7338 RTPathStripSuffix(pRenameState->pszNewBaseName);
7339
7340 pRenameState->pszOldBaseName = RTStrDup(RTPathFilename(pImage->pszFilename));
7341 AssertReturn(pRenameState->pszOldBaseName, VERR_NO_STR_MEMORY);
7342 RTPathStripSuffix(pRenameState->pszOldBaseName);
7343
7344 /* Prepare both old and new full names used for string replacement.
7345 Note! Must abspath the stuff here, so the strstr weirdness later in
7346 the renaming process get a match against abspath'ed extent paths.
7347 See RTPathAbsDup call in vmdkDescriptorReadSparse(). */
7348 pRenameState->pszNewFullName = RTPathAbsDup(pszFilename);
7349 AssertReturn(pRenameState->pszNewFullName, VERR_NO_STR_MEMORY);
7350 RTPathStripSuffix(pRenameState->pszNewFullName);
7351
7352 pRenameState->pszOldFullName = RTPathAbsDup(pImage->pszFilename);
7353 AssertReturn(pRenameState->pszOldFullName, VERR_NO_STR_MEMORY);
7354 RTPathStripSuffix(pRenameState->pszOldFullName);
7355
7356 /* Save the old name for easy access to the old descriptor file. */
7357 pRenameState->pszOldDescName = RTStrDup(pImage->pszFilename);
7358 AssertReturn(pRenameState->pszOldDescName, VERR_NO_STR_MEMORY);
7359
7360 /* Save old image name. */
7361 pRenameState->pszOldImageName = pImage->pszFilename;
7362 }
7363 }
7364 else
7365 rc = VERR_NO_TMP_MEMORY;
7366
7367 return rc;
7368}
7369
7370/**
7371 * Destroys the given rename state, freeing all allocated memory.
7372 *
7373 * @returns nothing.
7374 * @param pRenameState The rename state to destroy.
7375 */
7376static void vmdkRenameStateDestroy(PVMDKRENAMESTATE pRenameState)
7377{
7378 for (unsigned i = 0; i < pRenameState->DescriptorCopy.cLines; i++)
7379 if (pRenameState->DescriptorCopy.aLines[i])
7380 RTStrFree(pRenameState->DescriptorCopy.aLines[i]);
7381 if (pRenameState->apszOldName)
7382 {
7383 for (unsigned i = 0; i <= pRenameState->cExtents; i++)
7384 if (pRenameState->apszOldName[i])
7385 RTStrFree(pRenameState->apszOldName[i]);
7386 RTMemTmpFree(pRenameState->apszOldName);
7387 }
7388 if (pRenameState->apszNewName)
7389 {
7390 for (unsigned i = 0; i <= pRenameState->cExtents; i++)
7391 if (pRenameState->apszNewName[i])
7392 RTStrFree(pRenameState->apszNewName[i]);
7393 RTMemTmpFree(pRenameState->apszNewName);
7394 }
7395 if (pRenameState->apszNewLines)
7396 {
7397 for (unsigned i = 0; i < pRenameState->cExtents; i++)
7398 if (pRenameState->apszNewLines[i])
7399 RTStrFree(pRenameState->apszNewLines[i]);
7400 RTMemTmpFree(pRenameState->apszNewLines);
7401 }
7402 if (pRenameState->pszOldDescName)
7403 RTStrFree(pRenameState->pszOldDescName);
7404 if (pRenameState->pszOldBaseName)
7405 RTStrFree(pRenameState->pszOldBaseName);
7406 if (pRenameState->pszNewBaseName)
7407 RTStrFree(pRenameState->pszNewBaseName);
7408 if (pRenameState->pszOldFullName)
7409 RTStrFree(pRenameState->pszOldFullName);
7410 if (pRenameState->pszNewFullName)
7411 RTStrFree(pRenameState->pszNewFullName);
7412}
7413
7414/**
7415 * Rolls back the rename operation to the original state.
7416 *
7417 * @returns VBox status code.
7418 * @param pImage VMDK image instance.
7419 * @param pRenameState The rename state.
7420 */
7421static int vmdkRenameRollback(PVMDKIMAGE pImage, PVMDKRENAMESTATE pRenameState)
7422{
7423 int rc = VINF_SUCCESS;
7424
7425 if (!pRenameState->fImageFreed)
7426 {
7427 /*
7428 * Some extents may have been closed, close the rest. We will
7429 * re-open the whole thing later.
7430 */
7431 vmdkFreeImage(pImage, false, true /*fFlush*/);
7432 }
7433
7434 /* Rename files back. */
7435 for (unsigned i = 0; i <= pRenameState->cExtents; i++)
7436 {
7437 if (pRenameState->apszOldName[i])
7438 {
7439 rc = vdIfIoIntFileMove(pImage->pIfIo, pRenameState->apszNewName[i], pRenameState->apszOldName[i], 0);
7440 AssertRC(rc);
7441 }
7442 }
7443 /* Restore the old descriptor. */
7444 PVMDKFILE pFile;
7445 rc = vmdkFileOpen(pImage, &pFile, NULL, pRenameState->pszOldDescName,
7446 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_NORMAL,
7447 false /* fCreate */));
7448 AssertRC(rc);
7449 if (pRenameState->fEmbeddedDesc)
7450 {
7451 pRenameState->ExtentCopy.pFile = pFile;
7452 pImage->pExtents = &pRenameState->ExtentCopy;
7453 }
7454 else
7455 {
7456 /* Shouldn't be null for separate descriptor.
7457 * There will be no access to the actual content.
7458 */
7459 pImage->pDescData = pRenameState->pszOldDescName;
7460 pImage->pFile = pFile;
7461 }
7462 pImage->Descriptor = pRenameState->DescriptorCopy;
7463 vmdkWriteDescriptor(pImage, NULL);
7464 vmdkFileClose(pImage, &pFile, false);
7465 /* Get rid of the stuff we implanted. */
7466 pImage->pExtents = NULL;
7467 pImage->pFile = NULL;
7468 pImage->pDescData = NULL;
7469 /* Re-open the image back. */
7470 pImage->pszFilename = pRenameState->pszOldImageName;
7471 rc = vmdkOpenImage(pImage, pImage->uOpenFlags);
7472
7473 return rc;
7474}
7475
7476/**
7477 * Rename worker doing the real work.
7478 *
7479 * @returns VBox status code.
7480 * @param pImage VMDK image instance.
7481 * @param pRenameState The rename state.
7482 * @param pszFilename The new filename.
7483 */
7484static int vmdkRenameWorker(PVMDKIMAGE pImage, PVMDKRENAMESTATE pRenameState, const char *pszFilename)
7485{
7486 int rc = VINF_SUCCESS;
7487 unsigned i, line;
7488
7489 /* Update the descriptor with modified extent names. */
7490 for (i = 0, line = pImage->Descriptor.uFirstExtent;
7491 i < pRenameState->cExtents;
7492 i++, line = pImage->Descriptor.aNextLines[line])
7493 {
7494 /* Update the descriptor. */
7495 pRenameState->apszNewLines[i] = vmdkStrReplace(pImage->Descriptor.aLines[line],
7496 pRenameState->pszOldBaseName,
7497 pRenameState->pszNewBaseName);
7498 if (!pRenameState->apszNewLines[i])
7499 {
7500 rc = VERR_NO_MEMORY;
7501 break;
7502 }
7503 pImage->Descriptor.aLines[line] = pRenameState->apszNewLines[i];
7504 }
7505
7506 if (RT_SUCCESS(rc))
7507 {
7508 /* Make sure the descriptor gets written back. */
7509 pImage->Descriptor.fDirty = true;
7510 /* Flush the descriptor now, in case it is embedded. */
7511 vmdkFlushImage(pImage, NULL);
7512
7513 /* Close and rename/move extents. */
7514 for (i = 0; i < pRenameState->cExtents; i++)
7515 {
7516 PVMDKEXTENT pExtent = &pImage->pExtents[i];
7517 /* Compose new name for the extent. */
7518 pRenameState->apszNewName[i] = vmdkStrReplace(pExtent->pszFullname,
7519 pRenameState->pszOldFullName,
7520 pRenameState->pszNewFullName);
7521 if (!pRenameState->apszNewName[i])
7522 {
7523 rc = VERR_NO_MEMORY;
7524 break;
7525 }
7526 /* Close the extent file. */
7527 rc = vmdkFileClose(pImage, &pExtent->pFile, false);
7528 if (RT_FAILURE(rc))
7529 break;;
7530
7531 /* Rename the extent file. */
7532 rc = vdIfIoIntFileMove(pImage->pIfIo, pExtent->pszFullname, pRenameState->apszNewName[i], 0);
7533 if (RT_FAILURE(rc))
7534 break;
7535 /* Remember the old name. */
7536 pRenameState->apszOldName[i] = RTStrDup(pExtent->pszFullname);
7537 }
7538
7539 if (RT_SUCCESS(rc))
7540 {
7541 /* Release all old stuff. */
7542 rc = vmdkFreeImage(pImage, false, true /*fFlush*/);
7543 if (RT_SUCCESS(rc))
7544 {
7545 pRenameState->fImageFreed = true;
7546
7547 /* Last elements of new/old name arrays are intended for
7548 * storing descriptor's names.
7549 */
7550 pRenameState->apszNewName[pRenameState->cExtents] = RTStrDup(pszFilename);
7551 /* Rename the descriptor file if it's separate. */
7552 if (!pRenameState->fEmbeddedDesc)
7553 {
7554 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pRenameState->apszNewName[pRenameState->cExtents], 0);
7555 if (RT_SUCCESS(rc))
7556 {
7557 /* Save old name only if we may need to change it back. */
7558 pRenameState->apszOldName[pRenameState->cExtents] = RTStrDup(pszFilename);
7559 }
7560 }
7561
7562 /* Update pImage with the new information. */
7563 pImage->pszFilename = pszFilename;
7564
7565 /* Open the new image. */
7566 rc = vmdkOpenImage(pImage, pImage->uOpenFlags);
7567 }
7568 }
7569 }
7570
7571 return rc;
7572}
7573
7574/** @copydoc VDIMAGEBACKEND::pfnRename */
7575static DECLCALLBACK(int) vmdkRename(void *pBackendData, const char *pszFilename)
7576{
7577 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
7578
7579 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7580 VMDKRENAMESTATE RenameState;
7581
7582 memset(&RenameState, 0, sizeof(RenameState));
7583
7584 /* Check arguments. */
7585 AssertPtrReturn(pImage, VERR_INVALID_POINTER);
7586 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
7587 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
7588 AssertReturn(!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK), VERR_INVALID_PARAMETER);
7589
7590 int rc = vmdkRenameStatePrepare(pImage, &RenameState, pszFilename);
7591 if (RT_SUCCESS(rc))
7592 {
7593 /* --- Up to this point we have not done any damage yet. --- */
7594
7595 rc = vmdkRenameWorker(pImage, &RenameState, pszFilename);
7596 /* Roll back all changes in case of failure. */
7597 if (RT_FAILURE(rc))
7598 {
7599 int rrc = vmdkRenameRollback(pImage, &RenameState);
7600 AssertRC(rrc);
7601 }
7602 }
7603
7604 vmdkRenameStateDestroy(&RenameState);
7605 LogFlowFunc(("returns %Rrc\n", rc));
7606 return rc;
7607}
7608
7609/** @copydoc VDIMAGEBACKEND::pfnClose */
7610static DECLCALLBACK(int) vmdkClose(void *pBackendData, bool fDelete)
7611{
7612 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
7613 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7614
7615 int rc = vmdkFreeImage(pImage, fDelete, true /*fFlush*/);
7616 RTMemFree(pImage);
7617
7618 LogFlowFunc(("returns %Rrc\n", rc));
7619 return rc;
7620}
7621
7622/** @copydoc VDIMAGEBACKEND::pfnRead */
7623static DECLCALLBACK(int) vmdkRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
7624 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
7625{
7626 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
7627 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
7628 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7629
7630 AssertPtr(pImage);
7631 Assert(uOffset % 512 == 0);
7632 Assert(cbToRead % 512 == 0);
7633 AssertPtrReturn(pIoCtx, VERR_INVALID_POINTER);
7634 AssertReturn(cbToRead, VERR_INVALID_PARAMETER);
7635 AssertReturn(uOffset + cbToRead <= pImage->cbSize, VERR_INVALID_PARAMETER);
7636
7637 /* Find the extent and check access permissions as defined in the extent descriptor. */
7638 PVMDKEXTENT pExtent;
7639 uint64_t uSectorExtentRel;
7640 int rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
7641 &pExtent, &uSectorExtentRel);
7642 if ( RT_SUCCESS(rc)
7643 && pExtent->enmAccess != VMDKACCESS_NOACCESS)
7644 {
7645 /* Clip read range to remain in this extent. */
7646 cbToRead = RT_MIN(cbToRead, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
7647
7648 /* Handle the read according to the current extent type. */
7649 switch (pExtent->enmType)
7650 {
7651 case VMDKETYPE_HOSTED_SPARSE:
7652 {
7653 uint64_t uSectorExtentAbs;
7654
7655 rc = vmdkGetSector(pImage, pIoCtx, pExtent, uSectorExtentRel, &uSectorExtentAbs);
7656 if (RT_FAILURE(rc))
7657 break;
7658 /* Clip read range to at most the rest of the grain. */
7659 cbToRead = RT_MIN(cbToRead, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSectorExtentRel % pExtent->cSectorsPerGrain));
7660 Assert(!(cbToRead % 512));
7661 if (uSectorExtentAbs == 0)
7662 {
7663 if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7664 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
7665 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
7666 rc = VERR_VD_BLOCK_FREE;
7667 else
7668 rc = vmdkStreamReadSequential(pImage, pExtent,
7669 uSectorExtentRel,
7670 pIoCtx, cbToRead);
7671 }
7672 else
7673 {
7674 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7675 {
7676 AssertMsg(vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx),
7677 ("Async I/O is not supported for stream optimized VMDK's\n"));
7678
7679 uint32_t uSectorInGrain = uSectorExtentRel % pExtent->cSectorsPerGrain;
7680 uSectorExtentAbs -= uSectorInGrain;
7681 if (pExtent->uGrainSectorAbs != uSectorExtentAbs)
7682 {
7683 uint64_t uLBA = 0; /* gcc maybe uninitialized */
7684 rc = vmdkFileInflateSync(pImage, pExtent,
7685 VMDK_SECTOR2BYTE(uSectorExtentAbs),
7686 pExtent->pvGrain,
7687 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
7688 NULL, &uLBA, NULL);
7689 if (RT_FAILURE(rc))
7690 {
7691 pExtent->uGrainSectorAbs = 0;
7692 break;
7693 }
7694 pExtent->uGrainSectorAbs = uSectorExtentAbs;
7695 pExtent->uGrain = uSectorExtentRel / pExtent->cSectorsPerGrain;
7696 Assert(uLBA == uSectorExtentRel);
7697 }
7698 vdIfIoIntIoCtxCopyTo(pImage->pIfIo, pIoCtx,
7699 (uint8_t *)pExtent->pvGrain
7700 + VMDK_SECTOR2BYTE(uSectorInGrain),
7701 cbToRead);
7702 }
7703 else
7704 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pExtent->pFile->pStorage,
7705 VMDK_SECTOR2BYTE(uSectorExtentAbs),
7706 pIoCtx, cbToRead);
7707 }
7708 break;
7709 }
7710 case VMDKETYPE_VMFS:
7711 case VMDKETYPE_FLAT:
7712 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pExtent->pFile->pStorage,
7713 VMDK_SECTOR2BYTE(uSectorExtentRel),
7714 pIoCtx, cbToRead);
7715 break;
7716 case VMDKETYPE_ZERO:
7717 {
7718 size_t cbSet;
7719
7720 cbSet = vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
7721 Assert(cbSet == cbToRead);
7722 break;
7723 }
7724 }
7725 if (pcbActuallyRead)
7726 *pcbActuallyRead = cbToRead;
7727 }
7728 else if (RT_SUCCESS(rc))
7729 rc = VERR_VD_VMDK_INVALID_STATE;
7730
7731 LogFlowFunc(("returns %Rrc\n", rc));
7732 return rc;
7733}
7734
7735/** @copydoc VDIMAGEBACKEND::pfnWrite */
7736static DECLCALLBACK(int) vmdkWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
7737 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
7738 size_t *pcbPostRead, unsigned fWrite)
7739{
7740 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
7741 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
7742 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7743 int rc;
7744
7745 AssertPtr(pImage);
7746 Assert(uOffset % 512 == 0);
7747 Assert(cbToWrite % 512 == 0);
7748 AssertPtrReturn(pIoCtx, VERR_INVALID_POINTER);
7749 AssertReturn(cbToWrite, VERR_INVALID_PARAMETER);
7750
7751 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
7752 {
7753 PVMDKEXTENT pExtent;
7754 uint64_t uSectorExtentRel;
7755 uint64_t uSectorExtentAbs;
7756
7757 /* No size check here, will do that later when the extent is located.
7758 * There are sparse images out there which according to the spec are
7759 * invalid, because the total size is not a multiple of the grain size.
7760 * Also for sparse images which are stitched together in odd ways (not at
7761 * grain boundaries, and with the nominal size not being a multiple of the
7762 * grain size), this would prevent writing to the last grain. */
7763
7764 rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
7765 &pExtent, &uSectorExtentRel);
7766 if (RT_SUCCESS(rc))
7767 {
7768 if ( pExtent->enmAccess != VMDKACCESS_READWRITE
7769 && ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7770 && !pImage->pExtents[0].uAppendPosition
7771 && pExtent->enmAccess != VMDKACCESS_READONLY))
7772 rc = VERR_VD_VMDK_INVALID_STATE;
7773 else
7774 {
7775 /* Handle the write according to the current extent type. */
7776 switch (pExtent->enmType)
7777 {
7778 case VMDKETYPE_HOSTED_SPARSE:
7779 rc = vmdkGetSector(pImage, pIoCtx, pExtent, uSectorExtentRel, &uSectorExtentAbs);
7780 if (RT_SUCCESS(rc))
7781 {
7782 if ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
7783 && uSectorExtentRel < (uint64_t)pExtent->uLastGrainAccess * pExtent->cSectorsPerGrain)
7784 rc = VERR_VD_VMDK_INVALID_WRITE;
7785 else
7786 {
7787 /* Clip write range to at most the rest of the grain. */
7788 cbToWrite = RT_MIN(cbToWrite,
7789 VMDK_SECTOR2BYTE( pExtent->cSectorsPerGrain
7790 - uSectorExtentRel % pExtent->cSectorsPerGrain));
7791 if (uSectorExtentAbs == 0)
7792 {
7793 if (!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
7794 {
7795 if (cbToWrite == VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
7796 {
7797 /* Full block write to a previously unallocated block.
7798 * Check if the caller wants to avoid the automatic alloc. */
7799 if (!(fWrite & VD_WRITE_NO_ALLOC))
7800 {
7801 /* Allocate GT and find out where to store the grain. */
7802 rc = vmdkAllocGrain(pImage, pExtent, pIoCtx,
7803 uSectorExtentRel, cbToWrite);
7804 }
7805 else
7806 rc = VERR_VD_BLOCK_FREE;
7807 *pcbPreRead = 0;
7808 *pcbPostRead = 0;
7809 }
7810 else
7811 {
7812 /* Clip write range to remain in this extent. */
7813 cbToWrite = RT_MIN(cbToWrite,
7814 VMDK_SECTOR2BYTE( pExtent->uSectorOffset
7815 + pExtent->cNominalSectors - uSectorExtentRel));
7816 *pcbPreRead = VMDK_SECTOR2BYTE(uSectorExtentRel % pExtent->cSectorsPerGrain);
7817 *pcbPostRead = VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain) - cbToWrite - *pcbPreRead;
7818 rc = VERR_VD_BLOCK_FREE;
7819 }
7820 }
7821 else
7822 rc = vmdkStreamAllocGrain(pImage, pExtent, uSectorExtentRel,
7823 pIoCtx, cbToWrite);
7824 }
7825 else
7826 {
7827 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7828 {
7829 /* A partial write to a streamOptimized image is simply
7830 * invalid. It requires rewriting already compressed data
7831 * which is somewhere between expensive and impossible. */
7832 rc = VERR_VD_VMDK_INVALID_STATE;
7833 pExtent->uGrainSectorAbs = 0;
7834 AssertRC(rc);
7835 }
7836 else
7837 {
7838 Assert(!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED));
7839 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pExtent->pFile->pStorage,
7840 VMDK_SECTOR2BYTE(uSectorExtentAbs),
7841 pIoCtx, cbToWrite, NULL, NULL);
7842 }
7843 }
7844 }
7845 }
7846 break;
7847 case VMDKETYPE_VMFS:
7848 case VMDKETYPE_FLAT:
7849 /* Clip write range to remain in this extent. */
7850 cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
7851 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pExtent->pFile->pStorage,
7852 VMDK_SECTOR2BYTE(uSectorExtentRel),
7853 pIoCtx, cbToWrite, NULL, NULL);
7854 break;
7855 case VMDKETYPE_ZERO:
7856 /* Clip write range to remain in this extent. */
7857 cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
7858 break;
7859 }
7860 }
7861
7862 if (pcbWriteProcess)
7863 *pcbWriteProcess = cbToWrite;
7864 }
7865 }
7866 else
7867 rc = VERR_VD_IMAGE_READ_ONLY;
7868
7869 LogFlowFunc(("returns %Rrc\n", rc));
7870 return rc;
7871}
7872
7873/** @copydoc VDIMAGEBACKEND::pfnFlush */
7874static DECLCALLBACK(int) vmdkFlush(void *pBackendData, PVDIOCTX pIoCtx)
7875{
7876 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7877
7878 return vmdkFlushImage(pImage, pIoCtx);
7879}
7880
7881/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
7882static DECLCALLBACK(unsigned) vmdkGetVersion(void *pBackendData)
7883{
7884 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
7885 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7886
7887 AssertPtrReturn(pImage, 0);
7888
7889 return VMDK_IMAGE_VERSION;
7890}
7891
7892/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
7893static DECLCALLBACK(uint64_t) vmdkGetFileSize(void *pBackendData)
7894{
7895 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
7896 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7897 uint64_t cb = 0;
7898
7899 AssertPtrReturn(pImage, 0);
7900
7901 if (pImage->pFile != NULL)
7902 {
7903 uint64_t cbFile;
7904 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pFile->pStorage, &cbFile);
7905 if (RT_SUCCESS(rc))
7906 cb += cbFile;
7907 }
7908 for (unsigned i = 0; i < pImage->cExtents; i++)
7909 {
7910 if (pImage->pExtents[i].pFile != NULL)
7911 {
7912 uint64_t cbFile;
7913 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pExtents[i].pFile->pStorage, &cbFile);
7914 if (RT_SUCCESS(rc))
7915 cb += cbFile;
7916 }
7917 }
7918
7919 LogFlowFunc(("returns %lld\n", cb));
7920 return cb;
7921}
7922
7923/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
7924static DECLCALLBACK(int) vmdkGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
7925{
7926 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
7927 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7928 int rc = VINF_SUCCESS;
7929
7930 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
7931
7932 if (pImage->PCHSGeometry.cCylinders)
7933 *pPCHSGeometry = pImage->PCHSGeometry;
7934 else
7935 rc = VERR_VD_GEOMETRY_NOT_SET;
7936
7937 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
7938 return rc;
7939}
7940
7941/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
7942static DECLCALLBACK(int) vmdkSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
7943{
7944 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
7945 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
7946 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7947 int rc = VINF_SUCCESS;
7948
7949 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
7950
7951 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
7952 {
7953 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
7954 {
7955 rc = vmdkDescSetPCHSGeometry(pImage, pPCHSGeometry);
7956 if (RT_SUCCESS(rc))
7957 pImage->PCHSGeometry = *pPCHSGeometry;
7958 }
7959 else
7960 rc = VERR_NOT_SUPPORTED;
7961 }
7962 else
7963 rc = VERR_VD_IMAGE_READ_ONLY;
7964
7965 LogFlowFunc(("returns %Rrc\n", rc));
7966 return rc;
7967}
7968
7969/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
7970static DECLCALLBACK(int) vmdkGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
7971{
7972 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
7973 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7974 int rc = VINF_SUCCESS;
7975
7976 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
7977
7978 if (pImage->LCHSGeometry.cCylinders)
7979 *pLCHSGeometry = pImage->LCHSGeometry;
7980 else
7981 rc = VERR_VD_GEOMETRY_NOT_SET;
7982
7983 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
7984 return rc;
7985}
7986
7987/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
7988static DECLCALLBACK(int) vmdkSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
7989{
7990 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
7991 pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
7992 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7993 int rc = VINF_SUCCESS;
7994
7995 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
7996
7997 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
7998 {
7999 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8000 {
8001 rc = vmdkDescSetLCHSGeometry(pImage, pLCHSGeometry);
8002 if (RT_SUCCESS(rc))
8003 pImage->LCHSGeometry = *pLCHSGeometry;
8004 }
8005 else
8006 rc = VERR_NOT_SUPPORTED;
8007 }
8008 else
8009 rc = VERR_VD_IMAGE_READ_ONLY;
8010
8011 LogFlowFunc(("returns %Rrc\n", rc));
8012 return rc;
8013}
8014
8015/** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
8016static DECLCALLBACK(int) vmdkQueryRegions(void *pBackendData, PCVDREGIONLIST *ppRegionList)
8017{
8018 LogFlowFunc(("pBackendData=%#p ppRegionList=%#p\n", pBackendData, ppRegionList));
8019 PVMDKIMAGE pThis = (PVMDKIMAGE)pBackendData;
8020
8021 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
8022
8023 *ppRegionList = &pThis->RegionList;
8024 LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
8025 return VINF_SUCCESS;
8026}
8027
8028/** @copydoc VDIMAGEBACKEND::pfnRegionListRelease */
8029static DECLCALLBACK(void) vmdkRegionListRelease(void *pBackendData, PCVDREGIONLIST pRegionList)
8030{
8031 RT_NOREF1(pRegionList);
8032 LogFlowFunc(("pBackendData=%#p pRegionList=%#p\n", pBackendData, pRegionList));
8033 PVMDKIMAGE pThis = (PVMDKIMAGE)pBackendData;
8034 AssertPtr(pThis); RT_NOREF(pThis);
8035
8036 /* Nothing to do here. */
8037}
8038
8039/** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
8040static DECLCALLBACK(unsigned) vmdkGetImageFlags(void *pBackendData)
8041{
8042 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
8043 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8044
8045 AssertPtrReturn(pImage, 0);
8046
8047 LogFlowFunc(("returns %#x\n", pImage->uImageFlags));
8048 return pImage->uImageFlags;
8049}
8050
8051/** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
8052static DECLCALLBACK(unsigned) vmdkGetOpenFlags(void *pBackendData)
8053{
8054 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
8055 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8056
8057 AssertPtrReturn(pImage, 0);
8058
8059 LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
8060 return pImage->uOpenFlags;
8061}
8062
8063/** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
8064static DECLCALLBACK(int) vmdkSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
8065{
8066 LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
8067 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8068 int rc;
8069
8070 /* Image must be opened and the new flags must be valid. */
8071 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
8072 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
8073 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
8074 rc = VERR_INVALID_PARAMETER;
8075 else
8076 {
8077 /* StreamOptimized images need special treatment: reopen is prohibited. */
8078 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
8079 {
8080 if (pImage->uOpenFlags == uOpenFlags)
8081 rc = VINF_SUCCESS;
8082 else
8083 rc = VERR_INVALID_PARAMETER;
8084 }
8085 else
8086 {
8087 /* Implement this operation via reopening the image. */
8088 vmdkFreeImage(pImage, false, true /*fFlush*/);
8089 rc = vmdkOpenImage(pImage, uOpenFlags);
8090 }
8091 }
8092
8093 LogFlowFunc(("returns %Rrc\n", rc));
8094 return rc;
8095}
8096
8097/** @copydoc VDIMAGEBACKEND::pfnGetComment */
8098static DECLCALLBACK(int) vmdkGetComment(void *pBackendData, char *pszComment, size_t cbComment)
8099{
8100 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
8101 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8102
8103 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8104
8105 char *pszCommentEncoded = NULL;
8106 int rc = vmdkDescDDBGetStr(pImage, &pImage->Descriptor,
8107 "ddb.comment", &pszCommentEncoded);
8108 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
8109 {
8110 pszCommentEncoded = NULL;
8111 rc = VINF_SUCCESS;
8112 }
8113
8114 if (RT_SUCCESS(rc))
8115 {
8116 if (pszComment && pszCommentEncoded)
8117 rc = vmdkDecodeString(pszCommentEncoded, pszComment, cbComment);
8118 else if (pszComment)
8119 *pszComment = '\0';
8120
8121 if (pszCommentEncoded)
8122 RTMemTmpFree(pszCommentEncoded);
8123 }
8124
8125 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
8126 return rc;
8127}
8128
8129/** @copydoc VDIMAGEBACKEND::pfnSetComment */
8130static DECLCALLBACK(int) vmdkSetComment(void *pBackendData, const char *pszComment)
8131{
8132 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
8133 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8134 int rc;
8135
8136 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8137
8138 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8139 {
8140 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8141 rc = vmdkSetImageComment(pImage, pszComment);
8142 else
8143 rc = VERR_NOT_SUPPORTED;
8144 }
8145 else
8146 rc = VERR_VD_IMAGE_READ_ONLY;
8147
8148 LogFlowFunc(("returns %Rrc\n", rc));
8149 return rc;
8150}
8151
8152/** @copydoc VDIMAGEBACKEND::pfnGetUuid */
8153static DECLCALLBACK(int) vmdkGetUuid(void *pBackendData, PRTUUID pUuid)
8154{
8155 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
8156 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8157
8158 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8159
8160 *pUuid = pImage->ImageUuid;
8161
8162 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
8163 return VINF_SUCCESS;
8164}
8165
8166/** @copydoc VDIMAGEBACKEND::pfnSetUuid */
8167static DECLCALLBACK(int) vmdkSetUuid(void *pBackendData, PCRTUUID pUuid)
8168{
8169 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
8170 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8171 int rc = VINF_SUCCESS;
8172
8173 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8174
8175 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8176 {
8177 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8178 {
8179 pImage->ImageUuid = *pUuid;
8180 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
8181 VMDK_DDB_IMAGE_UUID, pUuid);
8182 if (RT_FAILURE(rc))
8183 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
8184 N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
8185 }
8186 else
8187 rc = VERR_NOT_SUPPORTED;
8188 }
8189 else
8190 rc = VERR_VD_IMAGE_READ_ONLY;
8191
8192 LogFlowFunc(("returns %Rrc\n", rc));
8193 return rc;
8194}
8195
8196/** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
8197static DECLCALLBACK(int) vmdkGetModificationUuid(void *pBackendData, PRTUUID pUuid)
8198{
8199 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
8200 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8201
8202 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8203
8204 *pUuid = pImage->ModificationUuid;
8205
8206 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
8207 return VINF_SUCCESS;
8208}
8209
8210/** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
8211static DECLCALLBACK(int) vmdkSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
8212{
8213 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
8214 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8215 int rc = VINF_SUCCESS;
8216
8217 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8218
8219 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8220 {
8221 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8222 {
8223 /* Only touch the modification uuid if it changed. */
8224 if (RTUuidCompare(&pImage->ModificationUuid, pUuid))
8225 {
8226 pImage->ModificationUuid = *pUuid;
8227 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
8228 VMDK_DDB_MODIFICATION_UUID, pUuid);
8229 if (RT_FAILURE(rc))
8230 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing modification UUID in descriptor in '%s'"), pImage->pszFilename);
8231 }
8232 }
8233 else
8234 rc = VERR_NOT_SUPPORTED;
8235 }
8236 else
8237 rc = VERR_VD_IMAGE_READ_ONLY;
8238
8239 LogFlowFunc(("returns %Rrc\n", rc));
8240 return rc;
8241}
8242
8243/** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
8244static DECLCALLBACK(int) vmdkGetParentUuid(void *pBackendData, PRTUUID pUuid)
8245{
8246 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
8247 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8248
8249 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8250
8251 *pUuid = pImage->ParentUuid;
8252
8253 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
8254 return VINF_SUCCESS;
8255}
8256
8257/** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
8258static DECLCALLBACK(int) vmdkSetParentUuid(void *pBackendData, PCRTUUID pUuid)
8259{
8260 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
8261 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8262 int rc = VINF_SUCCESS;
8263
8264 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8265
8266 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8267 {
8268 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8269 {
8270 pImage->ParentUuid = *pUuid;
8271 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
8272 VMDK_DDB_PARENT_UUID, pUuid);
8273 if (RT_FAILURE(rc))
8274 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
8275 N_("VMDK: error storing parent image UUID in descriptor in '%s'"), pImage->pszFilename);
8276 }
8277 else
8278 rc = VERR_NOT_SUPPORTED;
8279 }
8280 else
8281 rc = VERR_VD_IMAGE_READ_ONLY;
8282
8283 LogFlowFunc(("returns %Rrc\n", rc));
8284 return rc;
8285}
8286
8287/** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
8288static DECLCALLBACK(int) vmdkGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
8289{
8290 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
8291 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8292
8293 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8294
8295 *pUuid = pImage->ParentModificationUuid;
8296
8297 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
8298 return VINF_SUCCESS;
8299}
8300
8301/** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
8302static DECLCALLBACK(int) vmdkSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
8303{
8304 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
8305 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8306 int rc = VINF_SUCCESS;
8307
8308 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8309
8310 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8311 {
8312 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8313 {
8314 pImage->ParentModificationUuid = *pUuid;
8315 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
8316 VMDK_DDB_PARENT_MODIFICATION_UUID, pUuid);
8317 if (RT_FAILURE(rc))
8318 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent image UUID in descriptor in '%s'"), pImage->pszFilename);
8319 }
8320 else
8321 rc = VERR_NOT_SUPPORTED;
8322 }
8323 else
8324 rc = VERR_VD_IMAGE_READ_ONLY;
8325
8326 LogFlowFunc(("returns %Rrc\n", rc));
8327 return rc;
8328}
8329
8330/** @copydoc VDIMAGEBACKEND::pfnDump */
8331static DECLCALLBACK(void) vmdkDump(void *pBackendData)
8332{
8333 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8334
8335 AssertPtrReturnVoid(pImage);
8336 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
8337 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
8338 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
8339 VMDK_BYTE2SECTOR(pImage->cbSize));
8340 vdIfErrorMessage(pImage->pIfError, "Header: uuidCreation={%RTuuid}\n", &pImage->ImageUuid);
8341 vdIfErrorMessage(pImage->pIfError, "Header: uuidModification={%RTuuid}\n", &pImage->ModificationUuid);
8342 vdIfErrorMessage(pImage->pIfError, "Header: uuidParent={%RTuuid}\n", &pImage->ParentUuid);
8343 vdIfErrorMessage(pImage->pIfError, "Header: uuidParentModification={%RTuuid}\n", &pImage->ParentModificationUuid);
8344}
8345
8346
8347/**
8348 * Returns the size, in bytes, of the sparse extent overhead for
8349 * the number of desired total sectors and based on the current
8350 * sectors of the extent.
8351 *
8352 * @returns uint64_t size of new overhead in bytes.
8353 * @param pExtent VMDK extent instance.
8354 * @param cSectorsNew Number of desired total sectors.
8355 */
8356static uint64_t vmdkGetNewOverhead(PVMDKEXTENT pExtent, uint64_t cSectorsNew)
8357{
8358 uint64_t cNewDirEntries = cSectorsNew / pExtent->cSectorsPerGDE;
8359 if (cSectorsNew % pExtent->cSectorsPerGDE)
8360 cNewDirEntries++;
8361
8362 size_t cbNewGD = cNewDirEntries * sizeof(uint32_t);
8363 uint64_t cbNewDirSize = RT_ALIGN_64(cbNewGD, 512);
8364 uint64_t cbNewAllTablesSize = RT_ALIGN_64(cNewDirEntries * pExtent->cGTEntries * sizeof(uint32_t), 512);
8365 uint64_t cbNewOverhead = RT_ALIGN_Z(RT_MAX(pExtent->uDescriptorSector
8366 + pExtent->cDescriptorSectors, 1)
8367 + cbNewDirSize + cbNewAllTablesSize, 512);
8368 cbNewOverhead += cbNewDirSize + cbNewAllTablesSize;
8369 cbNewOverhead = RT_ALIGN_64(cbNewOverhead,
8370 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8371
8372 return cbNewOverhead;
8373}
8374
8375/**
8376 * Internal: Replaces the size (in sectors) of an extent in the descriptor file.
8377 *
8378 * @returns VBox status code.
8379 * @param pImage VMDK image instance.
8380 * @param pExtent VMDK extent instance.
8381 * @param uLine Line number of descriptor to change.
8382 * @param cSectorsOld Existing number of sectors.
8383 * @param cSectorsNew New number of sectors.
8384 */
8385static int vmdkReplaceExtentSize(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, unsigned uLine, uint64_t cSectorsOld,
8386 uint64_t cSectorsNew)
8387{
8388 char szOldExtentSectors[UINT64_MAX_BUFF_SIZE];
8389 char szNewExtentSectors[UINT64_MAX_BUFF_SIZE];
8390
8391 ssize_t cbWritten = RTStrPrintf2(szOldExtentSectors, sizeof(szOldExtentSectors), "%llu", cSectorsOld);
8392 if (cbWritten <= 0 || cbWritten > (ssize_t)sizeof(szOldExtentSectors))
8393 return VERR_BUFFER_OVERFLOW;
8394
8395 cbWritten = RTStrPrintf2(szNewExtentSectors, sizeof(szNewExtentSectors), "%llu", cSectorsNew);
8396 if (cbWritten <= 0 || cbWritten > (ssize_t)sizeof(szNewExtentSectors))
8397 return VERR_BUFFER_OVERFLOW;
8398
8399 char *pszNewExtentLine = vmdkStrReplace(pImage->Descriptor.aLines[uLine],
8400 szOldExtentSectors,
8401 szNewExtentSectors);
8402
8403 if (RT_UNLIKELY(!pszNewExtentLine))
8404 return VERR_INVALID_PARAMETER;
8405
8406 vmdkDescExtRemoveByLine(pImage, &pImage->Descriptor, uLine);
8407 vmdkDescExtInsert(pImage, &pImage->Descriptor,
8408 pExtent->enmAccess, cSectorsNew,
8409 pExtent->enmType, pExtent->pszBasename, pExtent->uSectorOffset);
8410
8411 RTStrFree(pszNewExtentLine);
8412 pszNewExtentLine = NULL;
8413
8414 pImage->Descriptor.fDirty = true;
8415
8416 return VINF_SUCCESS;
8417}
8418
8419/**
8420 * Moves sectors down to make room for new overhead.
8421 * Used for sparse extent resize.
8422 *
8423 * @returns VBox status code.
8424 * @param pImage VMDK image instance.
8425 * @param pExtent VMDK extent instance.
8426 * @param cSectorsNew Number of sectors after resize.
8427 */
8428static int vmdkRelocateSectorsForSparseResize(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
8429 uint64_t cSectorsNew)
8430{
8431 int rc = VINF_SUCCESS;
8432
8433 uint64_t cbNewOverhead = vmdkGetNewOverhead(pExtent, cSectorsNew);
8434
8435 uint64_t cNewOverheadSectors = VMDK_BYTE2SECTOR(cbNewOverhead);
8436 uint64_t cOverheadSectorDiff = cNewOverheadSectors - pExtent->cOverheadSectors;
8437
8438 uint64_t cbFile = 0;
8439 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbFile);
8440
8441 uint64_t uNewAppendPosition;
8442
8443 /* Calculate how many sectors need to be relocated. */
8444 unsigned cSectorsReloc = cOverheadSectorDiff;
8445 if (cbNewOverhead % VMDK_SECTOR_SIZE)
8446 cSectorsReloc++;
8447
8448 if (cSectorsReloc < pExtent->cSectors)
8449 uNewAppendPosition = RT_ALIGN_Z(cbFile + VMDK_SECTOR2BYTE(cOverheadSectorDiff), 512);
8450 else
8451 uNewAppendPosition = cbFile;
8452
8453 /*
8454 * Get the blocks we need to relocate first, they are appended to the end
8455 * of the image.
8456 */
8457 void *pvBuf = NULL, *pvZero = NULL;
8458 do
8459 {
8460 /* Allocate data buffer. */
8461 pvBuf = RTMemAllocZ(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8462 if (!pvBuf)
8463 {
8464 rc = VERR_NO_MEMORY;
8465 break;
8466 }
8467
8468 /* Allocate buffer for overwriting with zeroes. */
8469 pvZero = RTMemAllocZ(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8470 if (!pvZero)
8471 {
8472 RTMemFree(pvBuf);
8473 pvBuf = NULL;
8474
8475 rc = VERR_NO_MEMORY;
8476 break;
8477 }
8478
8479 uint32_t *aGTDataTmp = (uint32_t *)RTMemAllocZ(sizeof(uint32_t) * pExtent->cGTEntries);
8480 if(!aGTDataTmp)
8481 {
8482 RTMemFree(pvBuf);
8483 pvBuf = NULL;
8484
8485 RTMemFree(pvZero);
8486 pvZero = NULL;
8487
8488 rc = VERR_NO_MEMORY;
8489 break;
8490 }
8491
8492 uint32_t *aRGTDataTmp = (uint32_t *)RTMemAllocZ(sizeof(uint32_t) * pExtent->cGTEntries);
8493 if(!aRGTDataTmp)
8494 {
8495 RTMemFree(pvBuf);
8496 pvBuf = NULL;
8497
8498 RTMemFree(pvZero);
8499 pvZero = NULL;
8500
8501 RTMemFree(aGTDataTmp);
8502 aGTDataTmp = NULL;
8503
8504 rc = VERR_NO_MEMORY;
8505 break;
8506 }
8507
8508 /* Search for overlap sector in the grain table. */
8509 for (uint32_t idxGD = 0; idxGD < pExtent->cGDEntries; idxGD++)
8510 {
8511 uint64_t uGTSector = pExtent->pGD[idxGD];
8512 uint64_t uRGTSector = pExtent->pRGD[idxGD];
8513
8514 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8515 VMDK_SECTOR2BYTE(uGTSector),
8516 aGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries);
8517
8518 if (RT_FAILURE(rc))
8519 break;
8520
8521 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8522 VMDK_SECTOR2BYTE(uRGTSector),
8523 aRGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries);
8524
8525 if (RT_FAILURE(rc))
8526 break;
8527
8528 for (uint32_t idxGT = 0; idxGT < pExtent->cGTEntries; idxGT++)
8529 {
8530 uint64_t aGTEntryLE = RT_LE2H_U64(aGTDataTmp[idxGT]);
8531 uint64_t aRGTEntryLE = RT_LE2H_U64(aRGTDataTmp[idxGT]);
8532
8533 /**
8534 * Check if grain table is valid. If not dump out with an error.
8535 * Shoudln't ever get here (given other checks) but good sanity check.
8536 */
8537 if (aGTEntryLE != aRGTEntryLE)
8538 {
8539 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
8540 N_("VMDK: inconsistent references within grain table in '%s'"), pExtent->pszFullname);
8541 break;
8542 }
8543
8544 if (aGTEntryLE < cNewOverheadSectors
8545 && aGTEntryLE != 0)
8546 {
8547 /* Read data and append grain to the end of the image. */
8548 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8549 VMDK_SECTOR2BYTE(aGTEntryLE), pvBuf,
8550 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8551 if (RT_FAILURE(rc))
8552 break;
8553
8554 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8555 uNewAppendPosition, pvBuf,
8556 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8557 if (RT_FAILURE(rc))
8558 break;
8559
8560 /* Zero out the old block area. */
8561 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8562 VMDK_SECTOR2BYTE(aGTEntryLE), pvZero,
8563 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8564 if (RT_FAILURE(rc))
8565 break;
8566
8567 /* Write updated grain tables to file */
8568 aGTDataTmp[idxGT] = VMDK_BYTE2SECTOR(uNewAppendPosition);
8569 aRGTDataTmp[idxGT] = VMDK_BYTE2SECTOR(uNewAppendPosition);
8570
8571 if (memcmp(aGTDataTmp, aRGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries))
8572 {
8573 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
8574 N_("VMDK: inconsistency between grain table and backup grain table in '%s'"), pExtent->pszFullname);
8575 break;
8576 }
8577
8578 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8579 VMDK_SECTOR2BYTE(uGTSector),
8580 aGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries);
8581
8582 if (RT_FAILURE(rc))
8583 break;
8584
8585 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8586 VMDK_SECTOR2BYTE(uRGTSector),
8587 aRGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries);
8588
8589 break;
8590 }
8591 }
8592 }
8593
8594 RTMemFree(aGTDataTmp);
8595 aGTDataTmp = NULL;
8596
8597 RTMemFree(aRGTDataTmp);
8598 aRGTDataTmp = NULL;
8599
8600 if (RT_FAILURE(rc))
8601 break;
8602
8603 uNewAppendPosition += VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain);
8604 } while (0);
8605
8606 if (pvBuf)
8607 {
8608 RTMemFree(pvBuf);
8609 pvBuf = NULL;
8610 }
8611
8612 if (pvZero)
8613 {
8614 RTMemFree(pvZero);
8615 pvZero = NULL;
8616 }
8617
8618 // Update append position for extent
8619 pExtent->uAppendPosition = uNewAppendPosition;
8620
8621 return rc;
8622}
8623
8624/**
8625 * Resizes meta/overhead for sparse extent resize.
8626 *
8627 * @returns VBox status code.
8628 * @param pImage VMDK image instance.
8629 * @param pExtent VMDK extent instance.
8630 * @param cSectorsNew Number of sectors after resize.
8631 */
8632static int vmdkResizeSparseMeta(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
8633 uint64_t cSectorsNew)
8634{
8635 int rc = VINF_SUCCESS;
8636 uint32_t cOldGDEntries = pExtent->cGDEntries;
8637
8638 uint64_t cNewDirEntries = cSectorsNew / pExtent->cSectorsPerGDE;
8639 if (cSectorsNew % pExtent->cSectorsPerGDE)
8640 cNewDirEntries++;
8641
8642 size_t cbNewGD = cNewDirEntries * sizeof(uint32_t);
8643
8644 uint64_t cbNewDirSize = RT_ALIGN_64(cbNewGD, 512);
8645 uint64_t cbCurrDirSize = RT_ALIGN_64(pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE, 512);
8646 uint64_t cDirSectorDiff = VMDK_BYTE2SECTOR(cbNewDirSize - cbCurrDirSize);
8647
8648 uint64_t cbNewAllTablesSize = RT_ALIGN_64(cNewDirEntries * pExtent->cGTEntries * sizeof(uint32_t), 512);
8649 uint64_t cbCurrAllTablesSize = RT_ALIGN_64(pExtent->cGDEntries * VMDK_GRAIN_TABLE_SIZE, 512);
8650 uint64_t cTableSectorDiff = VMDK_BYTE2SECTOR(cbNewAllTablesSize - cbCurrAllTablesSize);
8651
8652 uint64_t cbNewOverhead = vmdkGetNewOverhead(pExtent, cSectorsNew);
8653 uint64_t cNewOverheadSectors = VMDK_BYTE2SECTOR(cbNewOverhead);
8654 uint64_t cOverheadSectorDiff = cNewOverheadSectors - pExtent->cOverheadSectors;
8655
8656 /*
8657 * Get the blocks we need to relocate first, they are appended to the end
8658 * of the image.
8659 */
8660 void *pvBuf = NULL, *pvZero = NULL;
8661
8662 do
8663 {
8664 /* Allocate data buffer. */
8665 pvBuf = RTMemAllocZ(VMDK_GRAIN_TABLE_SIZE);
8666 if (!pvBuf)
8667 {
8668 rc = VERR_NO_MEMORY;
8669 break;
8670 }
8671
8672 /* Allocate buffer for overwriting with zeroes. */
8673 pvZero = RTMemAllocZ(VMDK_GRAIN_TABLE_SIZE);
8674 if (!pvZero)
8675 {
8676 RTMemFree(pvBuf);
8677 pvBuf = NULL;
8678
8679 rc = VERR_NO_MEMORY;
8680 break;
8681 }
8682
8683 uint32_t uGTStart = VMDK_SECTOR2BYTE(pExtent->uSectorGD) + (cOldGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8684
8685 // points to last element in the grain table
8686 uint32_t uGTTail = uGTStart + (pExtent->cGDEntries * VMDK_GRAIN_TABLE_SIZE) - VMDK_GRAIN_TABLE_SIZE;
8687 uint32_t cbGTOff = RT_ALIGN_Z(VMDK_SECTOR2BYTE(cDirSectorDiff + cTableSectorDiff + cDirSectorDiff), 512);
8688
8689 for (int i = pExtent->cGDEntries - 1; i >= 0; i--)
8690 {
8691 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8692 uGTTail, pvBuf,
8693 VMDK_GRAIN_TABLE_SIZE);
8694 if (RT_FAILURE(rc))
8695 break;
8696
8697 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8698 RT_ALIGN_Z(uGTTail + cbGTOff, 512), pvBuf,
8699 VMDK_GRAIN_TABLE_SIZE);
8700 if (RT_FAILURE(rc))
8701 break;
8702
8703 // This overshoots when i == 0, but we don't need it anymore.
8704 uGTTail -= VMDK_GRAIN_TABLE_SIZE;
8705 }
8706
8707
8708 /* Find the end of the grain directory and start bumping everything down. Update locations of GT entries. */
8709 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8710 VMDK_SECTOR2BYTE(pExtent->uSectorGD), pvBuf,
8711 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8712 if (RT_FAILURE(rc))
8713 break;
8714
8715 int * tmpBuf = (int *)pvBuf;
8716
8717 for (uint32_t i = 0; i < pExtent->cGDEntries; i++)
8718 {
8719 tmpBuf[i] = tmpBuf[i] + VMDK_BYTE2SECTOR(cbGTOff);
8720 pExtent->pGD[i] = pExtent->pGD[i] + VMDK_BYTE2SECTOR(cbGTOff);
8721 }
8722
8723 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8724 RT_ALIGN_Z(VMDK_SECTOR2BYTE(pExtent->uSectorGD + cTableSectorDiff + cDirSectorDiff), 512), pvBuf,
8725 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8726 if (RT_FAILURE(rc))
8727 break;
8728
8729 pExtent->uSectorGD = pExtent->uSectorGD + cDirSectorDiff + cTableSectorDiff;
8730
8731 /* Repeat both steps with the redundant grain table/directory. */
8732
8733 uint32_t uRGTStart = VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + (cOldGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8734
8735 // points to last element in the grain table
8736 uint32_t uRGTTail = uRGTStart + (pExtent->cGDEntries * VMDK_GRAIN_TABLE_SIZE) - VMDK_GRAIN_TABLE_SIZE;
8737 uint32_t cbRGTOff = RT_ALIGN_Z(VMDK_SECTOR2BYTE(cDirSectorDiff), 512);
8738
8739 for (int i = pExtent->cGDEntries - 1; i >= 0; i--)
8740 {
8741 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8742 uRGTTail, pvBuf,
8743 VMDK_GRAIN_TABLE_SIZE);
8744 if (RT_FAILURE(rc))
8745 break;
8746
8747 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8748 RT_ALIGN_Z(uRGTTail + cbRGTOff, 512), pvBuf,
8749 VMDK_GRAIN_TABLE_SIZE);
8750 if (RT_FAILURE(rc))
8751 break;
8752
8753 // This overshoots when i == 0, but we don't need it anymore.
8754 uRGTTail -= VMDK_GRAIN_TABLE_SIZE;
8755 }
8756
8757 /* Update locations of GT entries. */
8758 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8759 VMDK_SECTOR2BYTE(pExtent->uSectorRGD), pvBuf,
8760 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8761 if (RT_FAILURE(rc))
8762 break;
8763
8764 tmpBuf = (int *)pvBuf;
8765
8766 for (uint32_t i = 0; i < pExtent->cGDEntries; i++)
8767 {
8768 tmpBuf[i] = tmpBuf[i] + cDirSectorDiff;
8769 pExtent->pRGD[i] = pExtent->pRGD[i] + cDirSectorDiff;
8770 }
8771
8772 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8773 VMDK_SECTOR2BYTE(pExtent->uSectorRGD), pvBuf,
8774 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8775 if (RT_FAILURE(rc))
8776 break;
8777
8778 pExtent->uSectorRGD = pExtent->uSectorRGD;
8779 pExtent->cOverheadSectors += cOverheadSectorDiff;
8780
8781 } while (0);
8782
8783 if (pvBuf)
8784 {
8785 RTMemFree(pvBuf);
8786 pvBuf = NULL;
8787 }
8788
8789 if (pvZero)
8790 {
8791 RTMemFree(pvZero);
8792 pvZero = NULL;
8793 }
8794
8795 pExtent->cGDEntries = cNewDirEntries;
8796
8797 /* Allocate buffer for overwriting with zeroes. */
8798 pvZero = RTMemAllocZ(VMDK_GRAIN_TABLE_SIZE);
8799 if (!pvZero)
8800 return VERR_NO_MEMORY;
8801
8802 // Allocate additional grain dir
8803 pExtent->pGD = (uint32_t *) RTMemReallocZ(pExtent->pGD, pExtent->cGDEntries * sizeof(uint32_t), cbNewGD);
8804 if (RT_LIKELY(pExtent->pGD))
8805 {
8806 if (pExtent->uSectorRGD)
8807 {
8808 pExtent->pRGD = (uint32_t *)RTMemReallocZ(pExtent->pRGD, pExtent->cGDEntries * sizeof(uint32_t), cbNewGD);
8809 if (RT_UNLIKELY(!pExtent->pRGD))
8810 rc = VERR_NO_MEMORY;
8811 }
8812 }
8813 else
8814 return VERR_NO_MEMORY;
8815
8816
8817 uint32_t uTmpDirVal = pExtent->pGD[cOldGDEntries - 1] + VMDK_GRAIN_DIR_ENTRY_SIZE;
8818 for (uint32_t i = cOldGDEntries; i < pExtent->cGDEntries; i++)
8819 {
8820 pExtent->pGD[i] = uTmpDirVal;
8821
8822 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8823 VMDK_SECTOR2BYTE(uTmpDirVal), pvZero,
8824 VMDK_GRAIN_TABLE_SIZE);
8825
8826 if (RT_FAILURE(rc))
8827 return rc;
8828
8829 uTmpDirVal += VMDK_GRAIN_DIR_ENTRY_SIZE;
8830 }
8831
8832 uint32_t uRTmpDirVal = pExtent->pRGD[cOldGDEntries - 1] + VMDK_GRAIN_DIR_ENTRY_SIZE;
8833 for (uint32_t i = cOldGDEntries; i < pExtent->cGDEntries; i++)
8834 {
8835 pExtent->pRGD[i] = uRTmpDirVal;
8836
8837 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8838 VMDK_SECTOR2BYTE(uRTmpDirVal), pvZero,
8839 VMDK_GRAIN_TABLE_SIZE);
8840
8841 if (RT_FAILURE(rc))
8842 return rc;
8843
8844 uRTmpDirVal += VMDK_GRAIN_DIR_ENTRY_SIZE;
8845 }
8846
8847 RTMemFree(pvZero);
8848 pvZero = NULL;
8849
8850 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8851 VMDK_SECTOR2BYTE(pExtent->uSectorGD), pExtent->pGD,
8852 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8853 if (RT_FAILURE(rc))
8854 return rc;
8855
8856 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8857 VMDK_SECTOR2BYTE(pExtent->uSectorRGD), pExtent->pRGD,
8858 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8859 if (RT_FAILURE(rc))
8860 return rc;
8861
8862 rc = vmdkReplaceExtentSize(pImage, pExtent, pImage->Descriptor.uFirstExtent + pExtent->uExtent,
8863 pExtent->cNominalSectors, cSectorsNew);
8864 if (RT_FAILURE(rc))
8865 return rc;
8866
8867 return rc;
8868}
8869
8870/** @copydoc VDIMAGEBACKEND::pfnResize */
8871static DECLCALLBACK(int) vmdkResize(void *pBackendData, uint64_t cbSize,
8872 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
8873 unsigned uPercentStart, unsigned uPercentSpan,
8874 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
8875 PVDINTERFACE pVDIfsOperation)
8876{
8877 RT_NOREF5(uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation);
8878
8879 // Establish variables and objects needed
8880 int rc = VINF_SUCCESS;
8881 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8882 unsigned uImageFlags = pImage->uImageFlags;
8883 PVMDKEXTENT pExtent = &pImage->pExtents[0];
8884 pExtent->fMetaDirty = true;
8885
8886 uint64_t cSectorsNew = cbSize / VMDK_SECTOR_SIZE; /** < New number of sectors in the image after the resize */
8887 if (cbSize % VMDK_SECTOR_SIZE)
8888 cSectorsNew++;
8889
8890 uint64_t cSectorsOld = pImage->cbSize / VMDK_SECTOR_SIZE; /** < Number of sectors before the resize. Only for FLAT images. */
8891 if (pImage->cbSize % VMDK_SECTOR_SIZE)
8892 cSectorsOld++;
8893 unsigned cExtents = pImage->cExtents;
8894
8895 /* Check size is within min/max bounds. */
8896 if ( !(uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK)
8897 && ( !cbSize
8898 || (!(uImageFlags & VD_IMAGE_FLAGS_FIXED) && cbSize >= _1T * 256 - _64K)) )
8899 return VERR_VD_INVALID_SIZE;
8900
8901 /*
8902 * Making the image smaller is not supported at the moment.
8903 */
8904 /** @todo implement making the image smaller, it is the responsibility of
8905 * the user to know what they're doing. */
8906 if (cbSize < pImage->cbSize)
8907 rc = VERR_VD_SHRINK_NOT_SUPPORTED;
8908 else if (cbSize > pImage->cbSize)
8909 {
8910 /**
8911 * monolithicFlat. FIXED flag and not split up into 2 GB parts.
8912 */
8913 if ((uImageFlags & VD_IMAGE_FLAGS_FIXED) && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G))
8914 {
8915 /** Required space in bytes for the extent after the resize. */
8916 uint64_t cbSectorSpaceNew = cSectorsNew * VMDK_SECTOR_SIZE;
8917 pExtent = &pImage->pExtents[0];
8918
8919 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage, cbSectorSpaceNew,
8920 0 /* fFlags */, NULL,
8921 uPercentStart, uPercentSpan);
8922 if (RT_FAILURE(rc))
8923 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
8924
8925 rc = vmdkReplaceExtentSize(pImage, pExtent, pImage->Descriptor.uFirstExtent, cSectorsOld, cSectorsNew);
8926 if (RT_FAILURE(rc))
8927 return rc;
8928 }
8929
8930 /**
8931 * twoGbMaxExtentFlat. FIXED flag and SPLIT into 2 GB parts.
8932 */
8933 if ((uImageFlags & VD_IMAGE_FLAGS_FIXED) && (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G))
8934 {
8935 /* Check to see how much space remains in last extent */
8936 bool fSpaceAvailible = false;
8937 uint64_t cLastExtentRemSectors = cSectorsOld % VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
8938 if (cLastExtentRemSectors)
8939 fSpaceAvailible = true;
8940
8941 uint64_t cSectorsNeeded = cSectorsNew - cSectorsOld;
8942
8943 /** Space remaining in current last extent file that we don't need to create another one. */
8944 if (fSpaceAvailible && cSectorsNeeded + cLastExtentRemSectors <= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE))
8945 {
8946 pExtent = &pImage->pExtents[cExtents - 1];
8947 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage,
8948 VMDK_SECTOR2BYTE(cSectorsNeeded + cLastExtentRemSectors),
8949 0 /* fFlags */, NULL, uPercentStart, uPercentSpan);
8950 if (RT_FAILURE(rc))
8951 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
8952
8953 rc = vmdkReplaceExtentSize(pImage, pExtent, pImage->Descriptor.uFirstExtent + cExtents - 1,
8954 pExtent->cNominalSectors, cSectorsNeeded + cLastExtentRemSectors);
8955 if (RT_FAILURE(rc))
8956 return rc;
8957 }
8958 //** Need more extent files to handle all the requested space. */
8959 else
8960 {
8961 if (fSpaceAvailible)
8962 {
8963 pExtent = &pImage->pExtents[cExtents - 1];
8964 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage, VMDK_2G_SPLIT_SIZE,
8965 0 /* fFlags */, NULL,
8966 uPercentStart, uPercentSpan);
8967 if (RT_FAILURE(rc))
8968 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
8969
8970 cSectorsNeeded = cSectorsNeeded - VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE) + cLastExtentRemSectors;
8971
8972 rc = vmdkReplaceExtentSize(pImage, pExtent, pImage->Descriptor.uFirstExtent + cExtents - 1,
8973 pExtent->cNominalSectors, VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE));
8974 if (RT_FAILURE(rc))
8975 return rc;
8976 }
8977
8978 unsigned cNewExtents = VMDK_SECTOR2BYTE(cSectorsNeeded) / VMDK_2G_SPLIT_SIZE;
8979 if (cNewExtents % VMDK_2G_SPLIT_SIZE || cNewExtents < VMDK_2G_SPLIT_SIZE)
8980 cNewExtents++;
8981
8982 for (unsigned i = cExtents;
8983 i < cExtents + cNewExtents && cSectorsNeeded >= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
8984 i++)
8985 {
8986 rc = vmdkAddFileBackedExtent(pImage, VMDK_2G_SPLIT_SIZE);
8987 if (RT_FAILURE(rc))
8988 return rc;
8989
8990 pExtent = &pImage->pExtents[i];
8991
8992 pExtent->cSectors = VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
8993 cSectorsNeeded -= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
8994 }
8995
8996 if (cSectorsNeeded)
8997 {
8998 rc = vmdkAddFileBackedExtent(pImage, VMDK_SECTOR2BYTE(cSectorsNeeded));
8999 if (RT_FAILURE(rc))
9000 return rc;
9001 }
9002 }
9003 }
9004
9005 /**
9006 * monolithicSparse.
9007 */
9008 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G))
9009 {
9010 // 1. Calculate sectors needed for new overhead.
9011
9012 uint64_t cbNewOverhead = vmdkGetNewOverhead(pExtent, cSectorsNew);
9013 uint64_t cNewOverheadSectors = VMDK_BYTE2SECTOR(cbNewOverhead);
9014 uint64_t cOverheadSectorDiff = cNewOverheadSectors - pExtent->cOverheadSectors;
9015
9016 // 2. Relocate sectors to make room for new GD/GT, update entries in GD/GT
9017 if (cOverheadSectorDiff > 0)
9018 {
9019 if (pExtent->cSectors > 0)
9020 {
9021 /* Do the relocation. */
9022 LogFlow(("Relocating VMDK sectors\n"));
9023 rc = vmdkRelocateSectorsForSparseResize(pImage, pExtent, cSectorsNew);
9024 if (RT_FAILURE(rc))
9025 return rc;
9026
9027 rc = vmdkFlushImage(pImage, NULL);
9028 if (RT_FAILURE(rc))
9029 return rc;
9030 }
9031
9032 rc = vmdkResizeSparseMeta(pImage, pExtent, cSectorsNew);
9033 if (RT_FAILURE(rc))
9034 return rc;
9035 }
9036 }
9037
9038 /**
9039 * twoGbSparseExtent
9040 */
9041 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE && (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G))
9042 {
9043 /* Check to see how much space remains in last extent */
9044 bool fSpaceAvailible = false;
9045 uint64_t cLastExtentRemSectors = cSectorsOld % VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
9046 if (cLastExtentRemSectors)
9047 fSpaceAvailible = true;
9048
9049 uint64_t cSectorsNeeded = cSectorsNew - cSectorsOld;
9050
9051 if (fSpaceAvailible && cSectorsNeeded + cLastExtentRemSectors <= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE))
9052 {
9053 pExtent = &pImage->pExtents[cExtents - 1];
9054 rc = vmdkRelocateSectorsForSparseResize(pImage, pExtent, cSectorsNeeded + cLastExtentRemSectors);
9055 if (RT_FAILURE(rc))
9056 return rc;
9057
9058 rc = vmdkFlushImage(pImage, NULL);
9059 if (RT_FAILURE(rc))
9060 return rc;
9061
9062 rc = vmdkResizeSparseMeta(pImage, pExtent, cSectorsNeeded + cLastExtentRemSectors);
9063 if (RT_FAILURE(rc))
9064 return rc;
9065 }
9066 else
9067 {
9068 if (fSpaceAvailible)
9069 {
9070 pExtent = &pImage->pExtents[cExtents - 1];
9071 rc = vmdkRelocateSectorsForSparseResize(pImage, pExtent, VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE));
9072 if (RT_FAILURE(rc))
9073 return rc;
9074
9075 rc = vmdkFlushImage(pImage, NULL);
9076 if (RT_FAILURE(rc))
9077 return rc;
9078
9079 rc = vmdkResizeSparseMeta(pImage, pExtent, VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE));
9080 if (RT_FAILURE(rc))
9081 return rc;
9082
9083 cSectorsNeeded = cSectorsNeeded - VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE) + cLastExtentRemSectors;
9084 }
9085
9086 unsigned cNewExtents = VMDK_SECTOR2BYTE(cSectorsNeeded) / VMDK_2G_SPLIT_SIZE;
9087 if (cNewExtents % VMDK_2G_SPLIT_SIZE || cNewExtents < VMDK_2G_SPLIT_SIZE)
9088 cNewExtents++;
9089
9090 for (unsigned i = cExtents;
9091 i < cExtents + cNewExtents && cSectorsNeeded >= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
9092 i++)
9093 {
9094 rc = vmdkAddFileBackedExtent(pImage, VMDK_2G_SPLIT_SIZE);
9095 if (RT_FAILURE(rc))
9096 return rc;
9097
9098 pExtent = &pImage->pExtents[i];
9099
9100 rc = vmdkFlushImage(pImage, NULL);
9101 if (RT_FAILURE(rc))
9102 return rc;
9103
9104 pExtent->cSectors = VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
9105 cSectorsNeeded -= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
9106 }
9107
9108 if (cSectorsNeeded)
9109 {
9110 rc = vmdkAddFileBackedExtent(pImage, VMDK_SECTOR2BYTE(cSectorsNeeded));
9111 if (RT_FAILURE(rc))
9112 return rc;
9113
9114 pExtent = &pImage->pExtents[pImage->cExtents];
9115
9116 rc = vmdkFlushImage(pImage, NULL);
9117 if (RT_FAILURE(rc))
9118 return rc;
9119 }
9120 }
9121 }
9122
9123 /* Successful resize. Update metadata */
9124 if (RT_SUCCESS(rc))
9125 {
9126 /* Update size and new block count. */
9127 pImage->cbSize = cbSize;
9128 pExtent->cNominalSectors = cSectorsNew;
9129 pExtent->cSectors = cSectorsNew;
9130
9131 /* Update geometry. */
9132 pImage->PCHSGeometry = *pPCHSGeometry;
9133 pImage->LCHSGeometry = *pLCHSGeometry;
9134 }
9135
9136 /* Update header information in base image file. */
9137 pImage->Descriptor.fDirty = true;
9138 rc = vmdkWriteDescriptor(pImage, NULL);
9139
9140 if (RT_SUCCESS(rc))
9141 rc = vmdkFlushImage(pImage, NULL);
9142 }
9143 /* Same size doesn't change the image at all. */
9144
9145 LogFlowFunc(("returns %Rrc\n", rc));
9146 return rc;
9147}
9148
9149const VDIMAGEBACKEND g_VmdkBackend =
9150{
9151 /* u32Version */
9152 VD_IMGBACKEND_VERSION,
9153 /* pszBackendName */
9154 "VMDK",
9155 /* uBackendCaps */
9156 VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
9157 | VD_CAP_CREATE_SPLIT_2G | VD_CAP_DIFF | VD_CAP_FILE | VD_CAP_ASYNC
9158 | VD_CAP_VFS | VD_CAP_PREFERRED,
9159 /* paFileExtensions */
9160 s_aVmdkFileExtensions,
9161 /* paConfigInfo */
9162 s_aVmdkConfigInfo,
9163 /* pfnProbe */
9164 vmdkProbe,
9165 /* pfnOpen */
9166 vmdkOpen,
9167 /* pfnCreate */
9168 vmdkCreate,
9169 /* pfnRename */
9170 vmdkRename,
9171 /* pfnClose */
9172 vmdkClose,
9173 /* pfnRead */
9174 vmdkRead,
9175 /* pfnWrite */
9176 vmdkWrite,
9177 /* pfnFlush */
9178 vmdkFlush,
9179 /* pfnDiscard */
9180 NULL,
9181 /* pfnGetVersion */
9182 vmdkGetVersion,
9183 /* pfnGetFileSize */
9184 vmdkGetFileSize,
9185 /* pfnGetPCHSGeometry */
9186 vmdkGetPCHSGeometry,
9187 /* pfnSetPCHSGeometry */
9188 vmdkSetPCHSGeometry,
9189 /* pfnGetLCHSGeometry */
9190 vmdkGetLCHSGeometry,
9191 /* pfnSetLCHSGeometry */
9192 vmdkSetLCHSGeometry,
9193 /* pfnQueryRegions */
9194 vmdkQueryRegions,
9195 /* pfnRegionListRelease */
9196 vmdkRegionListRelease,
9197 /* pfnGetImageFlags */
9198 vmdkGetImageFlags,
9199 /* pfnGetOpenFlags */
9200 vmdkGetOpenFlags,
9201 /* pfnSetOpenFlags */
9202 vmdkSetOpenFlags,
9203 /* pfnGetComment */
9204 vmdkGetComment,
9205 /* pfnSetComment */
9206 vmdkSetComment,
9207 /* pfnGetUuid */
9208 vmdkGetUuid,
9209 /* pfnSetUuid */
9210 vmdkSetUuid,
9211 /* pfnGetModificationUuid */
9212 vmdkGetModificationUuid,
9213 /* pfnSetModificationUuid */
9214 vmdkSetModificationUuid,
9215 /* pfnGetParentUuid */
9216 vmdkGetParentUuid,
9217 /* pfnSetParentUuid */
9218 vmdkSetParentUuid,
9219 /* pfnGetParentModificationUuid */
9220 vmdkGetParentModificationUuid,
9221 /* pfnSetParentModificationUuid */
9222 vmdkSetParentModificationUuid,
9223 /* pfnDump */
9224 vmdkDump,
9225 /* pfnGetTimestamp */
9226 NULL,
9227 /* pfnGetParentTimestamp */
9228 NULL,
9229 /* pfnSetParentTimestamp */
9230 NULL,
9231 /* pfnGetParentFilename */
9232 NULL,
9233 /* pfnSetParentFilename */
9234 NULL,
9235 /* pfnComposeLocation */
9236 genericFileComposeLocation,
9237 /* pfnComposeName */
9238 genericFileComposeName,
9239 /* pfnCompact */
9240 NULL,
9241 /* pfnResize */
9242 vmdkResize,
9243 /* pfnRepair */
9244 NULL,
9245 /* pfnTraverseMetadata */
9246 NULL,
9247 /* u32VersionEnd */
9248 VD_IMGBACKEND_VERSION
9249};
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