VirtualBox

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

Last change on this file since 107664 was 107654, checked in by vboxsync, 4 months ago

Storage/VMDK.cpp: Small cleanup, bugref:3409 [build fix]

  • 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 107654 2025-01-10 13:57:48Z vboxsync $ */
2/** @file
3 * VMDK disk image, core code.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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/sg.h>
47#include <iprt/sort.h>
48#include <iprt/string.h>
49#include <iprt/zip.h>
50#include <iprt/asm.h>
51#ifdef RT_OS_WINDOWS
52# include <iprt/utf16.h>
53# include <iprt/uni.h>
54# include <iprt/uni.h>
55# include <iprt/nt/nt-and-windows.h>
56# include <winioctl.h>
57#endif
58#ifdef RT_OS_LINUX
59# include <errno.h>
60# include <sys/stat.h>
61# include <iprt/dir.h>
62# include <iprt/symlink.h>
63# include <iprt/linux/sysfs.h>
64#endif
65#ifdef RT_OS_FREEBSD
66#include <libgeom.h>
67#include <sys/stat.h>
68#include <stdlib.h>
69#endif
70#ifdef RT_OS_SOLARIS
71#include <sys/dkio.h>
72#include <sys/vtoc.h>
73#include <sys/efi_partition.h>
74#include <unistd.h>
75#include <errno.h>
76#endif
77#ifdef RT_OS_DARWIN
78# include <sys/stat.h>
79# include <sys/disk.h>
80# include <errno.h>
81/* The following structure and IOCTLs are defined in znu bsd/sys/disk.h but
82 inside KERNEL ifdefs and thus stripped from the SDK edition of the header.
83 While we could try include the header from the Kernel.framework, it's a lot
84 easier to just add the structure and 4 defines here. */
85typedef struct
86{
87 uint64_t offset;
88 uint64_t length;
89 uint8_t reserved0128[12];
90 dev_t dev;
91} dk_physical_extent_t;
92# define DKIOCGETBASE _IOR( 'd', 73, uint64_t)
93# define DKIOCLOCKPHYSICALEXTENTS _IO( 'd', 81)
94# define DKIOCGETPHYSICALEXTENT _IOWR('d', 82, dk_physical_extent_t)
95# define DKIOCUNLOCKPHYSICALEXTENTS _IO( 'd', 83)
96#endif /* RT_OS_DARWIN */
97
98#include "VDBackends.h"
99
100
101/*********************************************************************************************************************************
102* Constants And Macros, Structures and Typedefs *
103*********************************************************************************************************************************/
104
105/** Maximum encoded string size (including NUL) we allow for VMDK images.
106 * Deliberately not set high to avoid running out of descriptor space. */
107#define VMDK_ENCODED_COMMENT_MAX 1024
108
109/** VMDK descriptor DDB entry for PCHS cylinders. */
110#define VMDK_DDB_GEO_PCHS_CYLINDERS "ddb.geometry.cylinders"
111
112/** VMDK descriptor DDB entry for PCHS heads. */
113#define VMDK_DDB_GEO_PCHS_HEADS "ddb.geometry.heads"
114
115/** VMDK descriptor DDB entry for PCHS sectors. */
116#define VMDK_DDB_GEO_PCHS_SECTORS "ddb.geometry.sectors"
117
118/** VMDK descriptor DDB entry for LCHS cylinders. */
119#define VMDK_DDB_GEO_LCHS_CYLINDERS "ddb.geometry.biosCylinders"
120
121/** VMDK descriptor DDB entry for LCHS heads. */
122#define VMDK_DDB_GEO_LCHS_HEADS "ddb.geometry.biosHeads"
123
124/** VMDK descriptor DDB entry for LCHS sectors. */
125#define VMDK_DDB_GEO_LCHS_SECTORS "ddb.geometry.biosSectors"
126
127/** VMDK descriptor DDB entry for image UUID. */
128#define VMDK_DDB_IMAGE_UUID "ddb.uuid.image"
129
130/** VMDK descriptor DDB entry for image modification UUID. */
131#define VMDK_DDB_MODIFICATION_UUID "ddb.uuid.modification"
132
133/** VMDK descriptor DDB entry for parent image UUID. */
134#define VMDK_DDB_PARENT_UUID "ddb.uuid.parent"
135
136/** VMDK descriptor DDB entry for parent image modification UUID. */
137#define VMDK_DDB_PARENT_MODIFICATION_UUID "ddb.uuid.parentmodification"
138
139/** No compression for streamOptimized files. */
140#define VMDK_COMPRESSION_NONE 0
141
142/** Deflate compression for streamOptimized files. */
143#define VMDK_COMPRESSION_DEFLATE 1
144
145/** Marker that the actual GD value is stored in the footer. */
146#define VMDK_GD_AT_END 0xffffffffffffffffULL
147
148/** Marker for end-of-stream in streamOptimized images. */
149#define VMDK_MARKER_EOS 0
150
151/** Marker for grain table block in streamOptimized images. */
152#define VMDK_MARKER_GT 1
153
154/** Marker for grain directory block in streamOptimized images. */
155#define VMDK_MARKER_GD 2
156
157/** Marker for footer in streamOptimized images. */
158#define VMDK_MARKER_FOOTER 3
159
160/** Marker for unknown purpose in streamOptimized images.
161 * Shows up in very recent images created by vSphere, but only sporadically.
162 * They "forgot" to document that one in the VMDK specification. */
163#define VMDK_MARKER_UNSPECIFIED 4
164
165/** Dummy marker for "don't check the marker value". */
166#define VMDK_MARKER_IGNORE 0xffffffffU
167
168/**
169 * Magic number for hosted images created by VMware Workstation 4, VMware
170 * Workstation 5, VMware Server or VMware Player. Not necessarily sparse.
171 */
172#define VMDK_SPARSE_MAGICNUMBER 0x564d444b /* 'V' 'M' 'D' 'K' */
173
174/** VMDK sector size in bytes. */
175#define VMDK_SECTOR_SIZE 512
176/** Max string buffer size for uint64_t with null term */
177#define UINT64_MAX_BUFF_SIZE 21
178/** Grain directory entry size in bytes */
179#define VMDK_GRAIN_DIR_ENTRY_SIZE 4
180/** Grain table size in bytes */
181#define VMDK_GRAIN_TABLE_SIZE 2048
182
183/**
184 * VMDK hosted binary extent header. The "Sparse" is a total misnomer, as
185 * this header is also used for monolithic flat images.
186 */
187#pragma pack(1)
188typedef struct SparseExtentHeader
189{
190 uint32_t magicNumber;
191 uint32_t version;
192 uint32_t flags;
193 uint64_t capacity;
194 uint64_t grainSize;
195 uint64_t descriptorOffset;
196 uint64_t descriptorSize;
197 uint32_t numGTEsPerGT;
198 uint64_t rgdOffset;
199 uint64_t gdOffset;
200 uint64_t overHead;
201 bool uncleanShutdown;
202 char singleEndLineChar;
203 char nonEndLineChar;
204 char doubleEndLineChar1;
205 char doubleEndLineChar2;
206 uint16_t compressAlgorithm;
207 uint8_t pad[433];
208} SparseExtentHeader;
209#pragma pack()
210
211/** The maximum allowed descriptor size in the extent header in sectors. */
212#define VMDK_SPARSE_DESCRIPTOR_SIZE_MAX UINT64_C(20480) /* 10MB */
213
214/** VMDK capacity for a single chunk when 2G splitting is turned on. Should be
215 * divisible by the default grain size (64K) */
216#define VMDK_2G_SPLIT_SIZE (2047 * 1024 * 1024)
217
218/** VMDK streamOptimized file format marker. The type field may or may not
219 * be actually valid, but there's always data to read there. */
220#pragma pack(1)
221typedef struct VMDKMARKER
222{
223 uint64_t uSector;
224 uint32_t cbSize;
225 uint32_t uType;
226} VMDKMARKER, *PVMDKMARKER;
227#pragma pack()
228
229
230/** Convert sector number/size to byte offset/size. */
231#define VMDK_SECTOR2BYTE(u) ((uint64_t)(u) << 9)
232
233/** Convert byte offset/size to sector number/size. */
234#define VMDK_BYTE2SECTOR(u) ((u) >> 9)
235
236/**
237 * VMDK extent type.
238 */
239typedef enum VMDKETYPE
240{
241 /** Hosted sparse extent. */
242 VMDKETYPE_HOSTED_SPARSE = 1,
243 /** Flat extent. */
244 VMDKETYPE_FLAT,
245 /** Zero extent. */
246 VMDKETYPE_ZERO,
247 /** VMFS extent, used by ESX. */
248 VMDKETYPE_VMFS
249} VMDKETYPE, *PVMDKETYPE;
250
251/**
252 * VMDK access type for a extent.
253 */
254typedef enum VMDKACCESS
255{
256 /** No access allowed. */
257 VMDKACCESS_NOACCESS = 0,
258 /** Read-only access. */
259 VMDKACCESS_READONLY,
260 /** Read-write access. */
261 VMDKACCESS_READWRITE
262} VMDKACCESS, *PVMDKACCESS;
263
264/** Forward declaration for PVMDKIMAGE. */
265typedef struct VMDKIMAGE *PVMDKIMAGE;
266
267/**
268 * Extents files entry. Used for opening a particular file only once.
269 */
270typedef struct VMDKFILE
271{
272 /** Pointer to file path. Local copy. */
273 const char *pszFilename;
274 /** Pointer to base name. Local copy. */
275 const char *pszBasename;
276 /** File open flags for consistency checking. */
277 unsigned fOpen;
278 /** Handle for sync/async file abstraction.*/
279 PVDIOSTORAGE pStorage;
280 /** Reference counter. */
281 unsigned uReferences;
282 /** Flag whether the file should be deleted on last close. */
283 bool fDelete;
284 /** Pointer to the image we belong to (for debugging purposes). */
285 PVMDKIMAGE pImage;
286 /** Pointer to next file descriptor. */
287 struct VMDKFILE *pNext;
288 /** Pointer to the previous file descriptor. */
289 struct VMDKFILE *pPrev;
290} VMDKFILE, *PVMDKFILE;
291
292/**
293 * VMDK extent data structure.
294 */
295typedef struct VMDKEXTENT
296{
297 /** File handle. */
298 PVMDKFILE pFile;
299 /** Base name of the image extent. */
300 const char *pszBasename;
301 /** Full name of the image extent. */
302 const char *pszFullname;
303 /** Number of sectors in this extent. */
304 uint64_t cSectors;
305 /** Number of sectors per block (grain in VMDK speak). */
306 uint64_t cSectorsPerGrain;
307 /** Starting sector number of descriptor. */
308 uint64_t uDescriptorSector;
309 /** Size of descriptor in sectors. */
310 uint64_t cDescriptorSectors;
311 /** Starting sector number of grain directory. */
312 uint64_t uSectorGD;
313 /** Starting sector number of redundant grain directory. */
314 uint64_t uSectorRGD;
315 /** Total number of metadata sectors. */
316 uint64_t cOverheadSectors;
317 /** Nominal size (i.e. as described by the descriptor) of this extent. */
318 uint64_t cNominalSectors;
319 /** Sector offset (i.e. as described by the descriptor) of this extent. */
320 uint64_t uSectorOffset;
321 /** Number of entries in a grain table. */
322 uint32_t cGTEntries;
323 /** Number of sectors reachable via a grain directory entry. */
324 uint32_t cSectorsPerGDE;
325 /** Number of entries in the grain directory. */
326 uint32_t cGDEntries;
327 /** Pointer to the next free sector. Legacy information. Do not use. */
328 uint32_t uFreeSector;
329 /** Number of this extent in the list of images. */
330 uint32_t uExtent;
331 /** Pointer to the descriptor (NULL if no descriptor in this extent). */
332 char *pDescData;
333 /** Pointer to the grain directory. */
334 uint32_t *pGD;
335 /** Pointer to the redundant grain directory. */
336 uint32_t *pRGD;
337 /** VMDK version of this extent. 1=1.0/1.1 */
338 uint32_t uVersion;
339 /** Type of this extent. */
340 VMDKETYPE enmType;
341 /** Access to this extent. */
342 VMDKACCESS enmAccess;
343 /** Flag whether this extent is marked as unclean. */
344 bool fUncleanShutdown;
345 /** Flag whether the metadata in the extent header needs to be updated. */
346 bool fMetaDirty;
347 /** Flag whether there is a footer in this extent. */
348 bool fFooter;
349 /** Compression type for this extent. */
350 uint16_t uCompression;
351 /** Append position for writing new grain. Only for sparse extents. */
352 uint64_t uAppendPosition;
353 /** Last grain which was accessed. Only for streamOptimized extents. */
354 uint32_t uLastGrainAccess;
355 /** Starting sector corresponding to the grain buffer. */
356 uint32_t uGrainSectorAbs;
357 /** Grain number corresponding to the grain buffer. */
358 uint32_t uGrain;
359 /** Actual size of the compressed data, only valid for reading. */
360 uint32_t cbGrainStreamRead;
361 /** Size of compressed grain buffer for streamOptimized extents. */
362 size_t cbCompGrain;
363 /** Compressed grain buffer for streamOptimized extents, with marker. */
364 void *pvCompGrain;
365 /** Decompressed grain buffer for streamOptimized extents. */
366 void *pvGrain;
367 /** Reference to the image in which this extent is used. Do not use this
368 * on a regular basis to avoid passing pImage references to functions
369 * explicitly. */
370 struct VMDKIMAGE *pImage;
371} VMDKEXTENT, *PVMDKEXTENT;
372
373/**
374 * Grain table cache size. Allocated per image.
375 */
376#define VMDK_GT_CACHE_SIZE 256
377
378/**
379 * Grain table block size. Smaller than an actual grain table block to allow
380 * more grain table blocks to be cached without having to allocate excessive
381 * amounts of memory for the cache.
382 */
383#define VMDK_GT_CACHELINE_SIZE 128
384
385
386/**
387 * Maximum number of lines in a descriptor file. Not worth the effort of
388 * making it variable. Descriptor files are generally very short (~20 lines),
389 * with the exception of sparse files split in 2G chunks, which need for the
390 * maximum size (almost 2T) exactly 1025 lines for the disk database.
391 */
392#define VMDK_DESCRIPTOR_LINES_MAX 1100U
393
394/**
395 * Parsed descriptor information. Allows easy access and update of the
396 * descriptor (whether separate file or not). Free form text files suck.
397 */
398typedef struct VMDKDESCRIPTOR
399{
400 /** Line number of first entry of the disk descriptor. */
401 unsigned uFirstDesc;
402 /** Line number of first entry in the extent description. */
403 unsigned uFirstExtent;
404 /** Line number of first disk database entry. */
405 unsigned uFirstDDB;
406 /** Total number of lines. */
407 unsigned cLines;
408 /** Total amount of memory available for the descriptor. */
409 size_t cbDescAlloc;
410 /** Set if descriptor has been changed and not yet written to disk. */
411 bool fDirty;
412 /** Array of pointers to the data in the descriptor. */
413 char *aLines[VMDK_DESCRIPTOR_LINES_MAX];
414 /** Array of line indices pointing to the next non-comment line. */
415 unsigned aNextLines[VMDK_DESCRIPTOR_LINES_MAX];
416} VMDKDESCRIPTOR, *PVMDKDESCRIPTOR;
417
418
419/**
420 * Cache entry for translating extent/sector to a sector number in that
421 * extent.
422 */
423typedef struct VMDKGTCACHEENTRY
424{
425 /** Extent number for which this entry is valid. */
426 uint32_t uExtent;
427 /** GT data block number. */
428 uint64_t uGTBlock;
429 /** Data part of the cache entry. */
430 uint32_t aGTData[VMDK_GT_CACHELINE_SIZE];
431} VMDKGTCACHEENTRY, *PVMDKGTCACHEENTRY;
432
433/**
434 * Cache data structure for blocks of grain table entries. For now this is a
435 * fixed size direct mapping cache, but this should be adapted to the size of
436 * the sparse image and maybe converted to a set-associative cache. The
437 * implementation below implements a write-through cache with write allocate.
438 */
439typedef struct VMDKGTCACHE
440{
441 /** Cache entries. */
442 VMDKGTCACHEENTRY aGTCache[VMDK_GT_CACHE_SIZE];
443 /** Number of cache entries (currently unused). */
444 unsigned cEntries;
445} VMDKGTCACHE, *PVMDKGTCACHE;
446
447/**
448 * Complete VMDK image data structure. Mainly a collection of extents and a few
449 * extra global data fields.
450 */
451typedef struct VMDKIMAGE
452{
453 /** Image name. */
454 const char *pszFilename;
455 /** Descriptor file if applicable. */
456 PVMDKFILE pFile;
457
458 /** Pointer to the per-disk VD interface list. */
459 PVDINTERFACE pVDIfsDisk;
460 /** Pointer to the per-image VD interface list. */
461 PVDINTERFACE pVDIfsImage;
462
463 /** Error interface. */
464 PVDINTERFACEERROR pIfError;
465 /** I/O interface. */
466 PVDINTERFACEIOINT pIfIo;
467
468
469 /** Pointer to the image extents. */
470 PVMDKEXTENT pExtents;
471 /** Number of image extents. */
472 unsigned cExtents;
473 /** Pointer to the files list, for opening a file referenced multiple
474 * times only once (happens mainly with raw partition access). */
475 PVMDKFILE pFiles;
476
477 /**
478 * Pointer to an array of segment entries for async I/O.
479 * This is an optimization because the task number to submit is not known
480 * and allocating/freeing an array in the read/write functions every time
481 * is too expensive.
482 */
483 PRTSGSEG paSegments;
484 /** Entries available in the segments array. */
485 unsigned cSegments;
486
487 /** Open flags passed by VBoxHD layer. */
488 unsigned uOpenFlags;
489 /** Image flags defined during creation or determined during open. */
490 unsigned uImageFlags;
491 /** Total size of the image. */
492 uint64_t cbSize;
493 /** Physical geometry of this image. */
494 VDGEOMETRY PCHSGeometry;
495 /** Logical geometry of this image. */
496 VDGEOMETRY LCHSGeometry;
497 /** Image UUID. */
498 RTUUID ImageUuid;
499 /** Image modification UUID. */
500 RTUUID ModificationUuid;
501 /** Parent image UUID. */
502 RTUUID ParentUuid;
503 /** Parent image modification UUID. */
504 RTUUID ParentModificationUuid;
505
506 /** Pointer to grain table cache, if this image contains sparse extents. */
507 PVMDKGTCACHE pGTCache;
508 /** Pointer to the descriptor (NULL if no separate descriptor file). */
509 char *pDescData;
510 /** Allocation size of the descriptor file. */
511 size_t cbDescAlloc;
512 /** Parsed descriptor file content. */
513 VMDKDESCRIPTOR Descriptor;
514 /** The static region list. */
515 VDREGIONLIST RegionList;
516} VMDKIMAGE;
517
518
519/** State for the input/output callout of the inflate reader/deflate writer. */
520typedef struct VMDKCOMPRESSIO
521{
522 /* Image this operation relates to. */
523 PVMDKIMAGE pImage;
524 /* Current read position. */
525 ssize_t iOffset;
526 /* Size of the compressed grain buffer (available data). */
527 size_t cbCompGrain;
528 /* Pointer to the compressed grain buffer. */
529 void *pvCompGrain;
530} VMDKCOMPRESSIO;
531
532
533/** Tracks async grain allocation. */
534typedef struct VMDKGRAINALLOCASYNC
535{
536 /** Flag whether the allocation failed. */
537 bool fIoErr;
538 /** Current number of transfers pending.
539 * If reached 0 and there is an error the old state is restored. */
540 unsigned cIoXfersPending;
541 /** Sector number */
542 uint64_t uSector;
543 /** Flag whether the grain table needs to be updated. */
544 bool fGTUpdateNeeded;
545 /** Extent the allocation happens. */
546 PVMDKEXTENT pExtent;
547 /** Position of the new grain, required for the grain table update. */
548 uint64_t uGrainOffset;
549 /** Grain table sector. */
550 uint64_t uGTSector;
551 /** Backup grain table sector. */
552 uint64_t uRGTSector;
553} VMDKGRAINALLOCASYNC, *PVMDKGRAINALLOCASYNC;
554
555/**
556 * State information for vmdkRename() and helpers.
557 */
558typedef struct VMDKRENAMESTATE
559{
560 /** Array of old filenames. */
561 char **apszOldName;
562 /** Array of new filenames. */
563 char **apszNewName;
564 /** Array of new lines in the extent descriptor. */
565 char **apszNewLines;
566 /** Name of the old descriptor file if not a sparse image. */
567 char *pszOldDescName;
568 /** Flag whether we called vmdkFreeImage(). */
569 bool fImageFreed;
570 /** Flag whther the descriptor is embedded in the image (sparse) or
571 * in a separate file. */
572 bool fEmbeddedDesc;
573 /** Number of extents in the image. */
574 unsigned cExtents;
575 /** New base filename. */
576 char *pszNewBaseName;
577 /** The old base filename. */
578 char *pszOldBaseName;
579 /** New full filename. */
580 char *pszNewFullName;
581 /** Old full filename. */
582 char *pszOldFullName;
583 /** The old image name. */
584 const char *pszOldImageName;
585 /** Copy of the original VMDK descriptor. */
586 VMDKDESCRIPTOR DescriptorCopy;
587 /** Copy of the extent state for sparse images. */
588 VMDKEXTENT ExtentCopy;
589} VMDKRENAMESTATE;
590/** Pointer to a VMDK rename state. */
591typedef VMDKRENAMESTATE *PVMDKRENAMESTATE;
592
593
594/*********************************************************************************************************************************
595* Static Variables *
596*********************************************************************************************************************************/
597
598/** NULL-terminated array of supported file extensions. */
599static const VDFILEEXTENSION s_aVmdkFileExtensions[] =
600{
601 {"vmdk", VDTYPE_HDD},
602 {NULL, VDTYPE_INVALID}
603};
604
605/** NULL-terminated array of configuration option. */
606static const VDCONFIGINFO s_aVmdkConfigInfo[] =
607{
608 /* Options for VMDK raw disks */
609 { "RawDrive", NULL, VDCFGVALUETYPE_STRING, 0 },
610 { "Partitions", NULL, VDCFGVALUETYPE_STRING, 0 },
611 { "BootSector", NULL, VDCFGVALUETYPE_BYTES, 0 },
612 { "Relative", NULL, VDCFGVALUETYPE_INTEGER, 0 },
613
614 /* End of options list */
615 { NULL, NULL, VDCFGVALUETYPE_INTEGER, 0 }
616};
617
618
619/*********************************************************************************************************************************
620* Internal Functions *
621*********************************************************************************************************************************/
622
623static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent);
624static int vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
625 bool fDelete);
626
627static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents);
628static int vmdkFlushImage(PVMDKIMAGE pImage, PVDIOCTX pIoCtx);
629static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment);
630static int vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete, bool fFlush);
631
632static DECLCALLBACK(int) vmdkAllocGrainComplete(void *pBackendData, PVDIOCTX pIoCtx,
633 void *pvUser, int rcReq);
634
635/**
636 * Internal: open a file (using a file descriptor cache to ensure each file
637 * is only opened once - anything else can cause locking problems).
638 */
639static int vmdkFileOpen(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile,
640 const char *pszBasename, const char *pszFilename, uint32_t fOpen)
641{
642 int rc = VINF_SUCCESS;
643 PVMDKFILE pVmdkFile;
644
645 for (pVmdkFile = pImage->pFiles;
646 pVmdkFile != NULL;
647 pVmdkFile = pVmdkFile->pNext)
648 {
649 if (!strcmp(pszFilename, pVmdkFile->pszFilename))
650 {
651 Assert(fOpen == pVmdkFile->fOpen);
652 pVmdkFile->uReferences++;
653
654 *ppVmdkFile = pVmdkFile;
655
656 return rc;
657 }
658 }
659
660 /* If we get here, there's no matching entry in the cache. */
661 pVmdkFile = (PVMDKFILE)RTMemAllocZ(sizeof(VMDKFILE));
662 if (!pVmdkFile)
663 {
664 *ppVmdkFile = NULL;
665 return VERR_NO_MEMORY;
666 }
667
668 pVmdkFile->pszFilename = RTStrDup(pszFilename);
669 if (!pVmdkFile->pszFilename)
670 {
671 RTMemFree(pVmdkFile);
672 *ppVmdkFile = NULL;
673 return VERR_NO_MEMORY;
674 }
675
676 if (pszBasename)
677 {
678 pVmdkFile->pszBasename = RTStrDup(pszBasename);
679 if (!pVmdkFile->pszBasename)
680 {
681 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
682 RTMemFree(pVmdkFile);
683 *ppVmdkFile = NULL;
684 return VERR_NO_MEMORY;
685 }
686 }
687
688 pVmdkFile->fOpen = fOpen;
689
690 rc = vdIfIoIntFileOpen(pImage->pIfIo, pszFilename, fOpen,
691 &pVmdkFile->pStorage);
692 if (RT_SUCCESS(rc))
693 {
694 pVmdkFile->uReferences = 1;
695 pVmdkFile->pImage = pImage;
696 pVmdkFile->pNext = pImage->pFiles;
697 if (pImage->pFiles)
698 pImage->pFiles->pPrev = pVmdkFile;
699 pImage->pFiles = pVmdkFile;
700 *ppVmdkFile = pVmdkFile;
701 }
702 else
703 {
704 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
705 RTMemFree(pVmdkFile);
706 *ppVmdkFile = NULL;
707 }
708
709 return rc;
710}
711
712/**
713 * Internal: close a file, updating the file descriptor cache.
714 */
715static int vmdkFileClose(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile, bool fDelete)
716{
717 int rc = VINF_SUCCESS;
718 PVMDKFILE pVmdkFile = *ppVmdkFile;
719
720 AssertPtr(pVmdkFile);
721
722 pVmdkFile->fDelete |= fDelete;
723 Assert(pVmdkFile->uReferences);
724 pVmdkFile->uReferences--;
725 if (pVmdkFile->uReferences == 0)
726 {
727 PVMDKFILE pPrev;
728 PVMDKFILE pNext;
729
730 /* Unchain the element from the list. */
731 pPrev = pVmdkFile->pPrev;
732 pNext = pVmdkFile->pNext;
733
734 if (pNext)
735 pNext->pPrev = pPrev;
736 if (pPrev)
737 pPrev->pNext = pNext;
738 else
739 pImage->pFiles = pNext;
740
741 rc = vdIfIoIntFileClose(pImage->pIfIo, pVmdkFile->pStorage);
742
743 bool fFileDel = pVmdkFile->fDelete;
744 if ( pVmdkFile->pszBasename
745 && fFileDel)
746 {
747 const char *pszSuffix = RTPathSuffix(pVmdkFile->pszBasename);
748 if ( RTPathHasPath(pVmdkFile->pszBasename)
749 || !pszSuffix
750 || ( strcmp(pszSuffix, ".vmdk")
751 && strcmp(pszSuffix, ".bin")
752 && strcmp(pszSuffix, ".img")))
753 fFileDel = false;
754 }
755
756 if (fFileDel)
757 {
758 int rc2 = vdIfIoIntFileDelete(pImage->pIfIo, pVmdkFile->pszFilename);
759 if (RT_SUCCESS(rc))
760 rc = rc2;
761 }
762 else if (pVmdkFile->fDelete)
763 LogRel(("VMDK: Denying deletion of %s\n", pVmdkFile->pszBasename));
764 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
765 if (pVmdkFile->pszBasename)
766 RTStrFree((char *)(void *)pVmdkFile->pszBasename);
767 RTMemFree(pVmdkFile);
768 }
769
770 *ppVmdkFile = NULL;
771 return rc;
772}
773
774/*#define VMDK_USE_BLOCK_DECOMP_API - test and enable */
775#ifndef VMDK_USE_BLOCK_DECOMP_API
776static DECLCALLBACK(int) vmdkFileInflateHelper(void *pvUser, void *pvBuf, size_t cbBuf, size_t *pcbBuf)
777{
778 VMDKCOMPRESSIO *pInflateState = (VMDKCOMPRESSIO *)pvUser;
779 size_t cbInjected = 0;
780
781 Assert(cbBuf);
782 if (pInflateState->iOffset < 0)
783 {
784 *(uint8_t *)pvBuf = RTZIPTYPE_ZLIB;
785 pvBuf = (uint8_t *)pvBuf + 1;
786 cbBuf--;
787 cbInjected = 1;
788 pInflateState->iOffset = RT_UOFFSETOF(VMDKMARKER, uType);
789 }
790 if (!cbBuf)
791 {
792 if (pcbBuf)
793 *pcbBuf = cbInjected;
794 return VINF_SUCCESS;
795 }
796 cbBuf = RT_MIN(cbBuf, pInflateState->cbCompGrain - pInflateState->iOffset);
797 memcpy(pvBuf,
798 (uint8_t *)pInflateState->pvCompGrain + pInflateState->iOffset,
799 cbBuf);
800 pInflateState->iOffset += cbBuf;
801 Assert(pcbBuf);
802 *pcbBuf = cbBuf + cbInjected;
803 return VINF_SUCCESS;
804}
805#endif
806
807/**
808 * Internal: read from a file and inflate the compressed data,
809 * distinguishing between async and normal operation
810 */
811DECLINLINE(int) vmdkFileInflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
812 uint64_t uOffset, void *pvBuf,
813 size_t cbToRead, const void *pcvMarker,
814 uint64_t *puLBA, uint32_t *pcbMarkerData)
815{
816 int rc;
817#ifndef VMDK_USE_BLOCK_DECOMP_API
818 PRTZIPDECOMP pZip = NULL;
819#endif
820 VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
821 size_t cbCompSize, cbActuallyRead;
822
823 if (!pcvMarker)
824 {
825 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
826 uOffset, pMarker, RT_UOFFSETOF(VMDKMARKER, uType));
827 if (RT_FAILURE(rc))
828 return rc;
829 }
830 else
831 {
832 memcpy(pMarker, pcvMarker, RT_UOFFSETOF(VMDKMARKER, uType));
833 /* pcvMarker endianness has already been partially transformed, fix it */
834 pMarker->uSector = RT_H2LE_U64(pMarker->uSector);
835 pMarker->cbSize = RT_H2LE_U32(pMarker->cbSize);
836 }
837
838 cbCompSize = RT_LE2H_U32(pMarker->cbSize);
839 if (cbCompSize == 0)
840 {
841 AssertMsgFailed(("VMDK: corrupted marker\n"));
842 return VERR_VD_VMDK_INVALID_FORMAT;
843 }
844
845 /* Sanity check - the expansion ratio should be much less than 2. */
846 Assert(cbCompSize < 2 * cbToRead);
847 if (cbCompSize >= 2 * cbToRead)
848 return VERR_VD_VMDK_INVALID_FORMAT;
849
850 /* Compressed grain marker. Data follows immediately. */
851 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
852 uOffset + RT_UOFFSETOF(VMDKMARKER, uType),
853 (uint8_t *)pExtent->pvCompGrain
854 + RT_UOFFSETOF(VMDKMARKER, uType),
855 RT_ALIGN_Z( cbCompSize
856 + RT_UOFFSETOF(VMDKMARKER, uType),
857 512)
858 - RT_UOFFSETOF(VMDKMARKER, uType));
859
860 if (puLBA)
861 *puLBA = RT_LE2H_U64(pMarker->uSector);
862 if (pcbMarkerData)
863 *pcbMarkerData = RT_ALIGN( cbCompSize
864 + RT_UOFFSETOF(VMDKMARKER, uType),
865 512);
866
867#ifdef VMDK_USE_BLOCK_DECOMP_API
868 rc = RTZipBlockDecompress(RTZIPTYPE_ZLIB, 0 /*fFlags*/,
869 pExtent->pvCompGrain, cbCompSize + RT_UOFFSETOF(VMDKMARKER, uType), NULL,
870 pvBuf, cbToRead, &cbActuallyRead);
871#else
872 VMDKCOMPRESSIO InflateState;
873 InflateState.pImage = pImage;
874 InflateState.iOffset = -1;
875 InflateState.cbCompGrain = cbCompSize + RT_UOFFSETOF(VMDKMARKER, uType);
876 InflateState.pvCompGrain = pExtent->pvCompGrain;
877
878 rc = RTZipDecompCreate(&pZip, &InflateState, vmdkFileInflateHelper);
879 if (RT_FAILURE(rc))
880 return rc;
881 rc = RTZipDecompress(pZip, pvBuf, cbToRead, &cbActuallyRead);
882 RTZipDecompDestroy(pZip);
883#endif /* !VMDK_USE_BLOCK_DECOMP_API */
884 if (RT_FAILURE(rc))
885 {
886 if (rc == VERR_ZIP_CORRUPTED)
887 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: Compressed image is corrupted '%s'"), pExtent->pszFullname);
888 return rc;
889 }
890 if (cbActuallyRead != cbToRead)
891 rc = VERR_VD_VMDK_INVALID_FORMAT;
892 return rc;
893}
894
895static DECLCALLBACK(int) vmdkFileDeflateHelper(void *pvUser, const void *pvBuf, size_t cbBuf)
896{
897 VMDKCOMPRESSIO *pDeflateState = (VMDKCOMPRESSIO *)pvUser;
898
899 Assert(cbBuf);
900 if (pDeflateState->iOffset < 0)
901 {
902 pvBuf = (const uint8_t *)pvBuf + 1;
903 cbBuf--;
904 pDeflateState->iOffset = RT_UOFFSETOF(VMDKMARKER, uType);
905 }
906 if (!cbBuf)
907 return VINF_SUCCESS;
908 if (pDeflateState->iOffset + cbBuf > pDeflateState->cbCompGrain)
909 return VERR_BUFFER_OVERFLOW;
910 memcpy((uint8_t *)pDeflateState->pvCompGrain + pDeflateState->iOffset,
911 pvBuf, cbBuf);
912 pDeflateState->iOffset += cbBuf;
913 return VINF_SUCCESS;
914}
915
916/**
917 * Internal: deflate the uncompressed data and write to a file,
918 * distinguishing between async and normal operation
919 */
920DECLINLINE(int) vmdkFileDeflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
921 uint64_t uOffset, const void *pvBuf,
922 size_t cbToWrite, uint64_t uLBA,
923 uint32_t *pcbMarkerData)
924{
925 int rc;
926 PRTZIPCOMP pZip = NULL;
927 VMDKCOMPRESSIO DeflateState;
928
929 DeflateState.pImage = pImage;
930 DeflateState.iOffset = -1;
931 DeflateState.cbCompGrain = pExtent->cbCompGrain;
932 DeflateState.pvCompGrain = pExtent->pvCompGrain;
933
934 rc = RTZipCompCreate(&pZip, &DeflateState, vmdkFileDeflateHelper,
935 RTZIPTYPE_ZLIB, RTZIPLEVEL_DEFAULT);
936 if (RT_FAILURE(rc))
937 return rc;
938 rc = RTZipCompress(pZip, pvBuf, cbToWrite);
939 if (RT_SUCCESS(rc))
940 rc = RTZipCompFinish(pZip);
941 RTZipCompDestroy(pZip);
942 if (RT_SUCCESS(rc))
943 {
944 Assert( DeflateState.iOffset > 0
945 && (size_t)DeflateState.iOffset <= DeflateState.cbCompGrain);
946
947 /* pad with zeroes to get to a full sector size */
948 uint32_t uSize = DeflateState.iOffset;
949 if (uSize % 512)
950 {
951 uint32_t uSizeAlign = RT_ALIGN(uSize, 512);
952 memset((uint8_t *)pExtent->pvCompGrain + uSize, '\0',
953 uSizeAlign - uSize);
954 uSize = uSizeAlign;
955 }
956
957 if (pcbMarkerData)
958 *pcbMarkerData = uSize;
959
960 /* Compressed grain marker. Data follows immediately. */
961 VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
962 pMarker->uSector = RT_H2LE_U64(uLBA);
963 pMarker->cbSize = RT_H2LE_U32( DeflateState.iOffset
964 - RT_UOFFSETOF(VMDKMARKER, uType));
965 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
966 uOffset, pMarker, uSize);
967 if (RT_FAILURE(rc))
968 return rc;
969 }
970 return rc;
971}
972
973
974/**
975 * Internal: check if all files are closed, prevent leaking resources.
976 */
977static int vmdkFileCheckAllClose(PVMDKIMAGE pImage)
978{
979 int rc = VINF_SUCCESS, rc2;
980 PVMDKFILE pVmdkFile;
981
982 Assert(pImage->pFiles == NULL);
983 for (pVmdkFile = pImage->pFiles;
984 pVmdkFile != NULL;
985 pVmdkFile = pVmdkFile->pNext)
986 {
987 LogRel(("VMDK: leaking reference to file \"%s\"\n",
988 pVmdkFile->pszFilename));
989 pImage->pFiles = pVmdkFile->pNext;
990
991 rc2 = vmdkFileClose(pImage, &pVmdkFile, pVmdkFile->fDelete);
992
993 if (RT_SUCCESS(rc))
994 rc = rc2;
995 }
996 return rc;
997}
998
999/**
1000 * Internal: truncate a string (at a UTF8 code point boundary) and encode the
1001 * critical non-ASCII characters.
1002 */
1003static char *vmdkEncodeString(const char *psz)
1004{
1005 char szEnc[VMDK_ENCODED_COMMENT_MAX + 3];
1006 char *pszDst = szEnc;
1007
1008 AssertPtr(psz);
1009
1010 for (; *psz; psz = RTStrNextCp(psz))
1011 {
1012 char *pszDstPrev = pszDst;
1013 RTUNICP Cp = RTStrGetCp(psz);
1014 if (Cp == '\\')
1015 {
1016 pszDst = RTStrPutCp(pszDst, Cp);
1017 pszDst = RTStrPutCp(pszDst, Cp);
1018 }
1019 else if (Cp == '\n')
1020 {
1021 pszDst = RTStrPutCp(pszDst, '\\');
1022 pszDst = RTStrPutCp(pszDst, 'n');
1023 }
1024 else if (Cp == '\r')
1025 {
1026 pszDst = RTStrPutCp(pszDst, '\\');
1027 pszDst = RTStrPutCp(pszDst, 'r');
1028 }
1029 else
1030 pszDst = RTStrPutCp(pszDst, Cp);
1031 if (pszDst - szEnc >= VMDK_ENCODED_COMMENT_MAX - 1)
1032 {
1033 pszDst = pszDstPrev;
1034 break;
1035 }
1036 }
1037 *pszDst = '\0';
1038 return RTStrDup(szEnc);
1039}
1040
1041/**
1042 * Internal: decode a string and store it into the specified string.
1043 */
1044static int vmdkDecodeString(const char *pszEncoded, char *psz, size_t cb)
1045{
1046 int rc = VINF_SUCCESS;
1047 char szBuf[4];
1048
1049 if (!cb)
1050 return VERR_BUFFER_OVERFLOW;
1051
1052 AssertPtr(psz);
1053
1054 for (; *pszEncoded; pszEncoded = RTStrNextCp(pszEncoded))
1055 {
1056 char *pszDst = szBuf;
1057 RTUNICP Cp = RTStrGetCp(pszEncoded);
1058 if (Cp == '\\')
1059 {
1060 pszEncoded = RTStrNextCp(pszEncoded);
1061 RTUNICP CpQ = RTStrGetCp(pszEncoded);
1062 if (CpQ == 'n')
1063 RTStrPutCp(pszDst, '\n');
1064 else if (CpQ == 'r')
1065 RTStrPutCp(pszDst, '\r');
1066 else if (CpQ == '\0')
1067 {
1068 rc = VERR_VD_VMDK_INVALID_HEADER;
1069 break;
1070 }
1071 else
1072 RTStrPutCp(pszDst, CpQ);
1073 }
1074 else
1075 pszDst = RTStrPutCp(pszDst, Cp);
1076
1077 /* Need to leave space for terminating NUL. */
1078 if ((size_t)(pszDst - szBuf) + 1 >= cb)
1079 {
1080 rc = VERR_BUFFER_OVERFLOW;
1081 break;
1082 }
1083 memcpy(psz, szBuf, pszDst - szBuf);
1084 psz += pszDst - szBuf;
1085 }
1086 *psz = '\0';
1087 return rc;
1088}
1089
1090/**
1091 * Internal: free all buffers associated with grain directories.
1092 */
1093static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent)
1094{
1095 if (pExtent->pGD)
1096 {
1097 RTMemFree(pExtent->pGD);
1098 pExtent->pGD = NULL;
1099 }
1100 if (pExtent->pRGD)
1101 {
1102 RTMemFree(pExtent->pRGD);
1103 pExtent->pRGD = NULL;
1104 }
1105}
1106
1107/**
1108 * Internal: allocate the compressed/uncompressed buffers for streamOptimized
1109 * images.
1110 */
1111static int vmdkAllocStreamBuffers(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1112{
1113 int rc = VINF_SUCCESS;
1114
1115 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
1116 {
1117 /* streamOptimized extents need a compressed grain buffer, which must
1118 * be big enough to hold uncompressible data (which needs ~8 bytes
1119 * more than the uncompressed data), the marker and padding. */
1120 pExtent->cbCompGrain = RT_ALIGN_Z( VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
1121 + 8 + sizeof(VMDKMARKER), 512);
1122 pExtent->pvCompGrain = RTMemAlloc(pExtent->cbCompGrain);
1123 if (RT_LIKELY(pExtent->pvCompGrain))
1124 {
1125 /* streamOptimized extents need a decompressed grain buffer. */
1126 pExtent->pvGrain = RTMemAlloc(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1127 if (!pExtent->pvGrain)
1128 rc = VERR_NO_MEMORY;
1129 }
1130 else
1131 rc = VERR_NO_MEMORY;
1132 }
1133
1134 if (RT_FAILURE(rc))
1135 vmdkFreeStreamBuffers(pExtent);
1136 return rc;
1137}
1138
1139/**
1140 * Internal: allocate all buffers associated with grain directories.
1141 */
1142static int vmdkAllocGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1143{
1144 RT_NOREF1(pImage);
1145 int rc = VINF_SUCCESS;
1146 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1147
1148 pExtent->pGD = (uint32_t *)RTMemAllocZ(cbGD);
1149 if (RT_LIKELY(pExtent->pGD))
1150 {
1151 if (pExtent->uSectorRGD)
1152 {
1153 pExtent->pRGD = (uint32_t *)RTMemAllocZ(cbGD);
1154 if (RT_UNLIKELY(!pExtent->pRGD))
1155 rc = VERR_NO_MEMORY;
1156 }
1157 }
1158 else
1159 rc = VERR_NO_MEMORY;
1160
1161 if (RT_FAILURE(rc))
1162 vmdkFreeGrainDirectory(pExtent);
1163 return rc;
1164}
1165
1166/**
1167 * Converts the grain directory from little to host endianess.
1168 *
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
6511 size_t cbSeg = vdIfIoIntIoCtxSegArrayCreate(pImage->pIfIo, pIoCtx, &Segment,
6512 &cSegments, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
6513 Assert(cbSeg == VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)); RT_NOREF(cbSeg);
6514 pData = Segment.pvSeg;
6515 }
6516 rc = vmdkFileDeflateSync(pImage, pExtent, uFileOffset, pData,
6517 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
6518 uSector, &cbGrain);
6519 if (RT_FAILURE(rc))
6520 {
6521 pExtent->uGrainSectorAbs = 0;
6522 AssertRC(rc);
6523 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write compressed data block in '%s'"), pExtent->pszFullname);
6524 }
6525 pExtent->uLastGrainAccess = uGrain;
6526 pExtent->uAppendPosition += cbGrain;
6527
6528 return rc;
6529}
6530
6531/**
6532 * Internal: Updates the grain table during grain allocation.
6533 */
6534static int vmdkAllocGrainGTUpdate(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, PVDIOCTX pIoCtx,
6535 PVMDKGRAINALLOCASYNC pGrainAlloc)
6536{
6537 int rc = VINF_SUCCESS;
6538 PVMDKGTCACHE pCache = pImage->pGTCache;
6539 uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
6540 uint32_t uGTHash, uGTBlockIndex;
6541 uint64_t uGTSector, uRGTSector, uGTBlock;
6542 uint64_t uSector = pGrainAlloc->uSector;
6543 PVMDKGTCACHEENTRY pGTCacheEntry;
6544
6545 LogFlowFunc(("pImage=%#p pExtent=%#p pCache=%#p pIoCtx=%#p pGrainAlloc=%#p\n",
6546 pImage, pExtent, pCache, pIoCtx, pGrainAlloc));
6547
6548 uGTSector = pGrainAlloc->uGTSector;
6549 uRGTSector = pGrainAlloc->uRGTSector;
6550 LogFlow(("uGTSector=%llu uRGTSector=%llu\n", uGTSector, uRGTSector));
6551
6552 /* Update the grain table (and the cache). */
6553 uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
6554 uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
6555 pGTCacheEntry = &pCache->aGTCache[uGTHash];
6556 if ( pGTCacheEntry->uExtent != pExtent->uExtent
6557 || pGTCacheEntry->uGTBlock != uGTBlock)
6558 {
6559 /* Cache miss, fetch data from disk. */
6560 LogFlow(("Cache miss, fetch data from disk\n"));
6561 PVDMETAXFER pMetaXfer = NULL;
6562 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6563 VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
6564 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
6565 &pMetaXfer, vmdkAllocGrainComplete, pGrainAlloc);
6566 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6567 {
6568 pGrainAlloc->cIoXfersPending++;
6569 pGrainAlloc->fGTUpdateNeeded = true;
6570 /* Leave early, we will be called again after the read completed. */
6571 LogFlowFunc(("Metadata read in progress, leaving\n"));
6572 return rc;
6573 }
6574 else if (RT_FAILURE(rc))
6575 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot read allocated grain table entry in '%s'"), pExtent->pszFullname);
6576 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
6577 pGTCacheEntry->uExtent = pExtent->uExtent;
6578 pGTCacheEntry->uGTBlock = uGTBlock;
6579 for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
6580 pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
6581 }
6582 else
6583 {
6584 /* Cache hit. Convert grain table block back to disk format, otherwise
6585 * the code below will write garbage for all but the updated entry. */
6586 for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
6587 aGTDataTmp[i] = RT_H2LE_U32(pGTCacheEntry->aGTData[i]);
6588 }
6589 pGrainAlloc->fGTUpdateNeeded = false;
6590 uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
6591 aGTDataTmp[uGTBlockIndex] = RT_H2LE_U32(VMDK_BYTE2SECTOR(pGrainAlloc->uGrainOffset));
6592 pGTCacheEntry->aGTData[uGTBlockIndex] = VMDK_BYTE2SECTOR(pGrainAlloc->uGrainOffset);
6593 /* Update grain table on disk. */
6594 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6595 VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
6596 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
6597 vmdkAllocGrainComplete, pGrainAlloc);
6598 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6599 pGrainAlloc->cIoXfersPending++;
6600 else if (RT_FAILURE(rc))
6601 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated grain table in '%s'"), pExtent->pszFullname);
6602 if (pExtent->pRGD)
6603 {
6604 /* Update backup grain table on disk. */
6605 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6606 VMDK_SECTOR2BYTE(uRGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
6607 aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
6608 vmdkAllocGrainComplete, pGrainAlloc);
6609 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6610 pGrainAlloc->cIoXfersPending++;
6611 else if (RT_FAILURE(rc))
6612 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated backup grain table in '%s'"), pExtent->pszFullname);
6613 }
6614
6615 LogFlowFunc(("leaving rc=%Rrc\n", rc));
6616 return rc;
6617}
6618
6619/**
6620 * Internal - complete the grain allocation by updating disk grain table if required.
6621 */
6622static DECLCALLBACK(int) vmdkAllocGrainComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
6623{
6624 RT_NOREF1(rcReq);
6625 int rc = VINF_SUCCESS;
6626 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
6627 PVMDKGRAINALLOCASYNC pGrainAlloc = (PVMDKGRAINALLOCASYNC)pvUser;
6628
6629 LogFlowFunc(("pBackendData=%#p pIoCtx=%#p pvUser=%#p rcReq=%Rrc\n",
6630 pBackendData, pIoCtx, pvUser, rcReq));
6631
6632 pGrainAlloc->cIoXfersPending--;
6633 if (!pGrainAlloc->cIoXfersPending && pGrainAlloc->fGTUpdateNeeded)
6634 rc = vmdkAllocGrainGTUpdate(pImage, pGrainAlloc->pExtent, pIoCtx, pGrainAlloc);
6635
6636 if (!pGrainAlloc->cIoXfersPending)
6637 {
6638 /* Grain allocation completed. */
6639 RTMemFree(pGrainAlloc);
6640 }
6641
6642 LogFlowFunc(("Leaving rc=%Rrc\n", rc));
6643 return rc;
6644}
6645
6646/**
6647 * Internal. Allocates a new grain table (if necessary).
6648 */
6649static int vmdkAllocGrain(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, PVDIOCTX pIoCtx,
6650 uint64_t uSector, uint64_t cbWrite)
6651{
6652 PVMDKGTCACHE pCache = pImage->pGTCache; NOREF(pCache);
6653 uint64_t uGDIndex, uGTSector, uRGTSector;
6654 uint64_t uFileOffset;
6655 PVMDKGRAINALLOCASYNC pGrainAlloc = NULL;
6656 int rc;
6657
6658 LogFlowFunc(("pCache=%#p pExtent=%#p pIoCtx=%#p uSector=%llu cbWrite=%llu\n",
6659 pCache, pExtent, pIoCtx, uSector, cbWrite));
6660
6661 pGrainAlloc = (PVMDKGRAINALLOCASYNC)RTMemAllocZ(sizeof(VMDKGRAINALLOCASYNC));
6662 if (!pGrainAlloc)
6663 return VERR_NO_MEMORY;
6664
6665 pGrainAlloc->pExtent = pExtent;
6666 pGrainAlloc->uSector = uSector;
6667
6668 uGDIndex = uSector / pExtent->cSectorsPerGDE;
6669 if (uGDIndex >= pExtent->cGDEntries)
6670 {
6671 RTMemFree(pGrainAlloc);
6672 return VERR_OUT_OF_RANGE;
6673 }
6674 uGTSector = pExtent->pGD[uGDIndex];
6675 if (pExtent->pRGD)
6676 uRGTSector = pExtent->pRGD[uGDIndex];
6677 else
6678 uRGTSector = 0; /**< avoid compiler warning */
6679 if (!uGTSector)
6680 {
6681 LogFlow(("Allocating new grain table\n"));
6682
6683 /* There is no grain table referenced by this grain directory
6684 * entry. So there is absolutely no data in this area. Allocate
6685 * a new grain table and put the reference to it in the GDs. */
6686 uFileOffset = pExtent->uAppendPosition;
6687 if (!uFileOffset)
6688 {
6689 RTMemFree(pGrainAlloc);
6690 return VERR_INTERNAL_ERROR;
6691 }
6692 Assert(!(uFileOffset % 512));
6693
6694 uFileOffset = RT_ALIGN_64(uFileOffset, 512);
6695 uGTSector = VMDK_BYTE2SECTOR(uFileOffset);
6696
6697 /* Normally the grain table is preallocated for hosted sparse extents
6698 * that support more than 32 bit sector numbers. So this shouldn't
6699 * ever happen on a valid extent. */
6700 if (uGTSector > UINT32_MAX)
6701 {
6702 RTMemFree(pGrainAlloc);
6703 return VERR_VD_VMDK_INVALID_HEADER;
6704 }
6705
6706 /* Write grain table by writing the required number of grain table
6707 * cache chunks. Allocate memory dynamically here or we flood the
6708 * metadata cache with very small entries. */
6709 size_t cbGTDataTmp = pExtent->cGTEntries * sizeof(uint32_t);
6710 uint32_t *paGTDataTmp = (uint32_t *)RTMemTmpAllocZ(cbGTDataTmp);
6711
6712 if (!paGTDataTmp)
6713 {
6714 RTMemFree(pGrainAlloc);
6715 return VERR_NO_MEMORY;
6716 }
6717
6718 memset(paGTDataTmp, '\0', cbGTDataTmp);
6719 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6720 VMDK_SECTOR2BYTE(uGTSector),
6721 paGTDataTmp, cbGTDataTmp, pIoCtx,
6722 vmdkAllocGrainComplete, pGrainAlloc);
6723 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6724 pGrainAlloc->cIoXfersPending++;
6725 else if (RT_FAILURE(rc))
6726 {
6727 RTMemTmpFree(paGTDataTmp);
6728 RTMemFree(pGrainAlloc);
6729 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain table allocation in '%s'"), pExtent->pszFullname);
6730 }
6731 pExtent->uAppendPosition = RT_ALIGN_64( pExtent->uAppendPosition
6732 + cbGTDataTmp, 512);
6733
6734 if (pExtent->pRGD)
6735 {
6736 AssertReturn(!uRGTSector, VERR_VD_VMDK_INVALID_HEADER);
6737 uFileOffset = pExtent->uAppendPosition;
6738 if (!uFileOffset)
6739 return VERR_INTERNAL_ERROR;
6740 Assert(!(uFileOffset % 512));
6741 uRGTSector = VMDK_BYTE2SECTOR(uFileOffset);
6742
6743 /* Normally the redundant grain table is preallocated for hosted
6744 * sparse extents that support more than 32 bit sector numbers. So
6745 * this shouldn't ever happen on a valid extent. */
6746 if (uRGTSector > UINT32_MAX)
6747 {
6748 RTMemTmpFree(paGTDataTmp);
6749 return VERR_VD_VMDK_INVALID_HEADER;
6750 }
6751
6752 /* Write grain table by writing the required number of grain table
6753 * cache chunks. Allocate memory dynamically here or we flood the
6754 * metadata cache with very small entries. */
6755 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6756 VMDK_SECTOR2BYTE(uRGTSector),
6757 paGTDataTmp, cbGTDataTmp, pIoCtx,
6758 vmdkAllocGrainComplete, pGrainAlloc);
6759 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6760 pGrainAlloc->cIoXfersPending++;
6761 else if (RT_FAILURE(rc))
6762 {
6763 RTMemTmpFree(paGTDataTmp);
6764 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain table allocation in '%s'"), pExtent->pszFullname);
6765 }
6766
6767 pExtent->uAppendPosition = pExtent->uAppendPosition + cbGTDataTmp;
6768 }
6769
6770 RTMemTmpFree(paGTDataTmp);
6771
6772 /* Update the grain directory on disk (doing it before writing the
6773 * grain table will result in a garbled extent if the operation is
6774 * aborted for some reason. Otherwise the worst that can happen is
6775 * some unused sectors in the extent. */
6776 uint32_t uGTSectorLE = RT_H2LE_U64(uGTSector);
6777 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6778 VMDK_SECTOR2BYTE(pExtent->uSectorGD) + uGDIndex * sizeof(uGTSectorLE),
6779 &uGTSectorLE, sizeof(uGTSectorLE), pIoCtx,
6780 vmdkAllocGrainComplete, pGrainAlloc);
6781 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6782 pGrainAlloc->cIoXfersPending++;
6783 else if (RT_FAILURE(rc))
6784 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain directory entry in '%s'"), pExtent->pszFullname);
6785 if (pExtent->pRGD)
6786 {
6787 uint32_t uRGTSectorLE = RT_H2LE_U64(uRGTSector);
6788 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
6789 VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + uGDIndex * sizeof(uGTSectorLE),
6790 &uRGTSectorLE, sizeof(uRGTSectorLE), pIoCtx,
6791 vmdkAllocGrainComplete, pGrainAlloc);
6792 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6793 pGrainAlloc->cIoXfersPending++;
6794 else if (RT_FAILURE(rc))
6795 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain directory entry in '%s'"), pExtent->pszFullname);
6796 }
6797
6798 /* As the final step update the in-memory copy of the GDs. */
6799 pExtent->pGD[uGDIndex] = uGTSector;
6800 if (pExtent->pRGD)
6801 pExtent->pRGD[uGDIndex] = uRGTSector;
6802 }
6803
6804 LogFlow(("uGTSector=%llu uRGTSector=%llu\n", uGTSector, uRGTSector));
6805 pGrainAlloc->uGTSector = uGTSector;
6806 pGrainAlloc->uRGTSector = uRGTSector;
6807
6808 uFileOffset = pExtent->uAppendPosition;
6809 if (!uFileOffset)
6810 return VERR_INTERNAL_ERROR;
6811 Assert(!(uFileOffset % 512));
6812
6813 pGrainAlloc->uGrainOffset = uFileOffset;
6814
6815 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
6816 {
6817 AssertMsgReturn(vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx),
6818 ("Accesses to stream optimized images must be synchronous\n"),
6819 VERR_INVALID_STATE);
6820
6821 if (cbWrite != VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
6822 return vdIfError(pImage->pIfError, VERR_INTERNAL_ERROR, RT_SRC_POS, N_("VMDK: not enough data for a compressed data block in '%s'"), pExtent->pszFullname);
6823
6824 /* Invalidate cache, just in case some code incorrectly allows mixing
6825 * of reads and writes. Normally shouldn't be needed. */
6826 pExtent->uGrainSectorAbs = 0;
6827
6828 /* Write compressed data block and the markers. */
6829 uint32_t cbGrain = 0;
6830 RTSGSEG Segment;
6831 unsigned cSegments = 1;
6832
6833 size_t cbSeg = vdIfIoIntIoCtxSegArrayCreate(pImage->pIfIo, pIoCtx, &Segment,
6834 &cSegments, cbWrite);
6835 Assert(cbSeg == cbWrite); RT_NOREF(cbSeg);
6836
6837 rc = vmdkFileDeflateSync(pImage, pExtent, uFileOffset,
6838 Segment.pvSeg, cbWrite, uSector, &cbGrain);
6839 if (RT_FAILURE(rc))
6840 {
6841 AssertRC(rc);
6842 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write allocated compressed data block in '%s'"), pExtent->pszFullname);
6843 }
6844 pExtent->uLastGrainAccess = uSector / pExtent->cSectorsPerGrain;
6845 pExtent->uAppendPosition += cbGrain;
6846 }
6847 else
6848 {
6849 /* Write the data. Always a full grain, or we're in big trouble. */
6850 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pExtent->pFile->pStorage,
6851 uFileOffset, pIoCtx, cbWrite,
6852 vmdkAllocGrainComplete, pGrainAlloc);
6853 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
6854 pGrainAlloc->cIoXfersPending++;
6855 else if (RT_FAILURE(rc))
6856 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write allocated data block in '%s'"), pExtent->pszFullname);
6857
6858 pExtent->uAppendPosition += cbWrite;
6859 }
6860
6861 rc = vmdkAllocGrainGTUpdate(pImage, pExtent, pIoCtx, pGrainAlloc);
6862
6863 if (!pGrainAlloc->cIoXfersPending)
6864 {
6865 /* Grain allocation completed. */
6866 RTMemFree(pGrainAlloc);
6867 }
6868
6869 LogFlowFunc(("leaving rc=%Rrc\n", rc));
6870
6871 return rc;
6872}
6873
6874/**
6875 * Internal. Reads the contents by sequentially going over the compressed
6876 * grains (hoping that they are in sequence).
6877 */
6878static int vmdkStreamReadSequential(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
6879 uint64_t uSector, PVDIOCTX pIoCtx,
6880 uint64_t cbRead)
6881{
6882 int rc;
6883
6884 LogFlowFunc(("pImage=%#p pExtent=%#p uSector=%llu pIoCtx=%#p cbRead=%llu\n",
6885 pImage, pExtent, uSector, pIoCtx, cbRead));
6886
6887 AssertMsgReturn(vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx),
6888 ("Async I/O not supported for sequential stream optimized images\n"),
6889 VERR_INVALID_STATE);
6890
6891 /* Do not allow to go back. */
6892 uint32_t uGrain = uSector / pExtent->cSectorsPerGrain;
6893 if (uGrain < pExtent->uLastGrainAccess)
6894 return VERR_VD_VMDK_INVALID_STATE;
6895 pExtent->uLastGrainAccess = uGrain;
6896
6897 /* After a previous error do not attempt to recover, as it would need
6898 * seeking (in the general case backwards which is forbidden). */
6899 if (!pExtent->uGrainSectorAbs)
6900 return VERR_VD_VMDK_INVALID_STATE;
6901
6902 /* Check if we need to read something from the image or if what we have
6903 * in the buffer is good to fulfill the request. */
6904 if (!pExtent->cbGrainStreamRead || uGrain > pExtent->uGrain)
6905 {
6906 uint32_t uGrainSectorAbs = pExtent->uGrainSectorAbs
6907 + VMDK_BYTE2SECTOR(pExtent->cbGrainStreamRead);
6908
6909 /* Get the marker from the next data block - and skip everything which
6910 * is not a compressed grain. If it's a compressed grain which is for
6911 * the requested sector (or after), read it. */
6912 VMDKMARKER Marker;
6913 do
6914 {
6915 RT_ZERO(Marker);
6916 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
6917 VMDK_SECTOR2BYTE(uGrainSectorAbs),
6918 &Marker, RT_UOFFSETOF(VMDKMARKER, uType));
6919 if (RT_FAILURE(rc))
6920 return rc;
6921 Marker.uSector = RT_LE2H_U64(Marker.uSector);
6922 Marker.cbSize = RT_LE2H_U32(Marker.cbSize);
6923
6924 if (Marker.cbSize == 0)
6925 {
6926 /* A marker for something else than a compressed grain. */
6927 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
6928 VMDK_SECTOR2BYTE(uGrainSectorAbs)
6929 + RT_UOFFSETOF(VMDKMARKER, uType),
6930 &Marker.uType, sizeof(Marker.uType));
6931 if (RT_FAILURE(rc))
6932 return rc;
6933 Marker.uType = RT_LE2H_U32(Marker.uType);
6934 switch (Marker.uType)
6935 {
6936 case VMDK_MARKER_EOS:
6937 uGrainSectorAbs++;
6938 /* Read (or mostly skip) to the end of file. Uses the
6939 * Marker (LBA sector) as it is unused anyway. This
6940 * makes sure that really everything is read in the
6941 * success case. If this read fails it means the image
6942 * is truncated, but this is harmless so ignore. */
6943 vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
6944 VMDK_SECTOR2BYTE(uGrainSectorAbs)
6945 + 511,
6946 &Marker.uSector, 1);
6947 break;
6948 case VMDK_MARKER_GT:
6949 uGrainSectorAbs += 1 + VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
6950 break;
6951 case VMDK_MARKER_GD:
6952 uGrainSectorAbs += 1 + VMDK_BYTE2SECTOR(RT_ALIGN(pExtent->cGDEntries * sizeof(uint32_t), 512));
6953 break;
6954 case VMDK_MARKER_FOOTER:
6955 uGrainSectorAbs += 2;
6956 break;
6957 case VMDK_MARKER_UNSPECIFIED:
6958 /* Skip over the contents of the unspecified marker
6959 * type 4 which exists in some vSphere created files. */
6960 /** @todo figure out what the payload means. */
6961 uGrainSectorAbs += 1;
6962 break;
6963 default:
6964 AssertMsgFailed(("VMDK: corrupted marker, type=%#x\n", Marker.uType));
6965 pExtent->uGrainSectorAbs = 0;
6966 return VERR_VD_VMDK_INVALID_STATE;
6967 }
6968 pExtent->cbGrainStreamRead = 0;
6969 }
6970 else
6971 {
6972 /* A compressed grain marker. If it is at/after what we're
6973 * interested in read and decompress data. */
6974 if (uSector > Marker.uSector + pExtent->cSectorsPerGrain)
6975 {
6976 uGrainSectorAbs += VMDK_BYTE2SECTOR(RT_ALIGN(Marker.cbSize + RT_UOFFSETOF(VMDKMARKER, uType), 512));
6977 continue;
6978 }
6979 uint64_t uLBA = 0;
6980 uint32_t cbGrainStreamRead = 0;
6981 rc = vmdkFileInflateSync(pImage, pExtent,
6982 VMDK_SECTOR2BYTE(uGrainSectorAbs),
6983 pExtent->pvGrain,
6984 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
6985 &Marker, &uLBA, &cbGrainStreamRead);
6986 if (RT_FAILURE(rc))
6987 {
6988 pExtent->uGrainSectorAbs = 0;
6989 return rc;
6990 }
6991 if ( pExtent->uGrain
6992 && uLBA / pExtent->cSectorsPerGrain <= pExtent->uGrain)
6993 {
6994 pExtent->uGrainSectorAbs = 0;
6995 return VERR_VD_VMDK_INVALID_STATE;
6996 }
6997 pExtent->uGrain = uLBA / pExtent->cSectorsPerGrain;
6998 pExtent->cbGrainStreamRead = cbGrainStreamRead;
6999 break;
7000 }
7001 } while (Marker.uType != VMDK_MARKER_EOS);
7002
7003 pExtent->uGrainSectorAbs = uGrainSectorAbs;
7004
7005 if (!pExtent->cbGrainStreamRead && Marker.uType == VMDK_MARKER_EOS)
7006 {
7007 pExtent->uGrain = UINT32_MAX;
7008 /* Must set a non-zero value for pExtent->cbGrainStreamRead or
7009 * the next read would try to get more data, and we're at EOF. */
7010 pExtent->cbGrainStreamRead = 1;
7011 }
7012 }
7013
7014 if (pExtent->uGrain > uSector / pExtent->cSectorsPerGrain)
7015 {
7016 /* The next data block we have is not for this area, so just return
7017 * that there is no data. */
7018 LogFlowFunc(("returns VERR_VD_BLOCK_FREE\n"));
7019 return VERR_VD_BLOCK_FREE;
7020 }
7021
7022 uint32_t uSectorInGrain = uSector % pExtent->cSectorsPerGrain;
7023 vdIfIoIntIoCtxCopyTo(pImage->pIfIo, pIoCtx,
7024 (uint8_t *)pExtent->pvGrain + VMDK_SECTOR2BYTE(uSectorInGrain),
7025 cbRead);
7026 LogFlowFunc(("returns VINF_SUCCESS\n"));
7027 return VINF_SUCCESS;
7028}
7029
7030/**
7031 * Replaces a fragment of a string with the specified string.
7032 *
7033 * @returns Pointer to the allocated UTF-8 string.
7034 * @param pszWhere UTF-8 string to search in.
7035 * @param pszWhat UTF-8 string to search for.
7036 * @param pszByWhat UTF-8 string to replace the found string with.
7037 *
7038 * @note r=bird: This is only used by vmdkRenameWorker(). The first use is
7039 * for updating the base name in the descriptor, the second is for
7040 * generating new filenames for extents. This code borked when
7041 * RTPathAbs started correcting the driver letter case on windows,
7042 * when strstr failed because the pExtent->pszFullname was not
7043 * subjected to RTPathAbs but while pExtent->pszFullname was. I fixed
7044 * this by apply RTPathAbs to the places it wasn't applied.
7045 *
7046 * However, this highlights some undocumented ASSUMPTIONS as well as
7047 * terrible short commings of the approach.
7048 *
7049 * Given the right filename, it may also screw up the descriptor. Take
7050 * the descriptor text 'RW 2048 SPARSE "Test0.vmdk"' for instance,
7051 * we'll be asked to replace "Test0" with something, no problem. No,
7052 * imagine 'RW 2048 SPARSE "SPARSE.vmdk"', 'RW 2048 SPARSE "RW.vmdk"'
7053 * or 'RW 2048 SPARSE "2048.vmdk"', and the strstr approach falls on
7054 * its bum. The descriptor string must be parsed and reconstructed,
7055 * the lazy strstr approach doesn't cut it.
7056 *
7057 * I'm also curious as to what would be the correct escaping of '"' in
7058 * the file name and how that is supposed to be handled, because it
7059 * needs to be or such names must be rejected in several places (maybe
7060 * they are, I didn't check).
7061 *
7062 * When this function is used to replace the start of a path, I think
7063 * the assumption from the prep/setup code is that we kind of knows
7064 * what we're working on (I could be wrong). However, using strstr
7065 * instead of strncmp/RTStrNICmp makes no sense and isn't future proof.
7066 * Especially on unix systems, weird stuff could happen if someone
7067 * unwittingly tinkers with the prep/setup code. What should really be
7068 * done here is using a new RTPathStartEx function that (via flags)
7069 * allows matching partial final component and returns the length of
7070 * what it matched up (in case it skipped slashes and '.' components).
7071 *
7072 */
7073static char *vmdkStrReplace(const char *pszWhere, const char *pszWhat,
7074 const char *pszByWhat)
7075{
7076 AssertPtr(pszWhere);
7077 AssertPtr(pszWhat);
7078 AssertPtr(pszByWhat);
7079 const char *pszFoundStr = strstr(pszWhere, pszWhat);
7080 if (!pszFoundStr)
7081 {
7082 LogFlowFunc(("Failed to find '%s' in '%s'!\n", pszWhat, pszWhere));
7083 return NULL;
7084 }
7085 size_t cbFinal = strlen(pszWhere) + 1 + strlen(pszByWhat) - strlen(pszWhat);
7086 char *pszNewStr = RTStrAlloc(cbFinal);
7087 if (pszNewStr)
7088 {
7089 char *pszTmp = pszNewStr;
7090 memcpy(pszTmp, pszWhere, pszFoundStr - pszWhere);
7091 pszTmp += pszFoundStr - pszWhere;
7092 memcpy(pszTmp, pszByWhat, strlen(pszByWhat));
7093 pszTmp += strlen(pszByWhat);
7094 strcpy(pszTmp, pszFoundStr + strlen(pszWhat));
7095 }
7096 return pszNewStr;
7097}
7098
7099
7100/** @copydoc VDIMAGEBACKEND::pfnProbe */
7101static DECLCALLBACK(int) vmdkProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
7102 PVDINTERFACE pVDIfsImage, VDTYPE enmDesiredType, VDTYPE *penmType)
7103{
7104 RT_NOREF(enmDesiredType);
7105 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p penmType=%#p\n",
7106 pszFilename, pVDIfsDisk, pVDIfsImage, penmType));
7107 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
7108 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
7109
7110 int rc = VINF_SUCCESS;
7111 PVMDKIMAGE pImage = (PVMDKIMAGE)RTMemAllocZ(RT_UOFFSETOF(VMDKIMAGE, RegionList.aRegions[1]));
7112 if (RT_LIKELY(pImage))
7113 {
7114 pImage->pszFilename = pszFilename;
7115 pImage->pFile = NULL;
7116 pImage->pExtents = NULL;
7117 pImage->pFiles = NULL;
7118 pImage->pGTCache = NULL;
7119 pImage->pDescData = NULL;
7120 pImage->pVDIfsDisk = pVDIfsDisk;
7121 pImage->pVDIfsImage = pVDIfsImage;
7122 /** @todo speed up this test open (VD_OPEN_FLAGS_INFO) by skipping as
7123 * much as possible in vmdkOpenImage. */
7124 rc = vmdkOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
7125 vmdkFreeImage(pImage, false, false /*fFlush*/);
7126 RTMemFree(pImage);
7127
7128 if (RT_SUCCESS(rc))
7129 *penmType = VDTYPE_HDD;
7130 }
7131 else
7132 rc = VERR_NO_MEMORY;
7133
7134 LogFlowFunc(("returns %Rrc\n", rc));
7135 return rc;
7136}
7137
7138/** @copydoc VDIMAGEBACKEND::pfnOpen */
7139static DECLCALLBACK(int) vmdkOpen(const char *pszFilename, unsigned uOpenFlags,
7140 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
7141 VDTYPE enmType, void **ppBackendData)
7142{
7143 RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
7144
7145 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
7146 pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
7147 int rc;
7148
7149 /* Check open flags. All valid flags are supported. */
7150 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
7151 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
7152 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
7153
7154
7155 PVMDKIMAGE pImage = (PVMDKIMAGE)RTMemAllocZ(RT_UOFFSETOF(VMDKIMAGE, RegionList.aRegions[1]));
7156 if (RT_LIKELY(pImage))
7157 {
7158 pImage->pszFilename = pszFilename;
7159 pImage->pFile = NULL;
7160 pImage->pExtents = NULL;
7161 pImage->pFiles = NULL;
7162 pImage->pGTCache = NULL;
7163 pImage->pDescData = NULL;
7164 pImage->pVDIfsDisk = pVDIfsDisk;
7165 pImage->pVDIfsImage = pVDIfsImage;
7166
7167 rc = vmdkOpenImage(pImage, uOpenFlags);
7168 if (RT_SUCCESS(rc))
7169 *ppBackendData = pImage;
7170 else
7171 RTMemFree(pImage);
7172 }
7173 else
7174 rc = VERR_NO_MEMORY;
7175
7176 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
7177 return rc;
7178}
7179
7180/** @copydoc VDIMAGEBACKEND::pfnCreate */
7181static DECLCALLBACK(int) vmdkCreate(const char *pszFilename, uint64_t cbSize,
7182 unsigned uImageFlags, const char *pszComment,
7183 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
7184 PCRTUUID pUuid, unsigned uOpenFlags,
7185 unsigned uPercentStart, unsigned uPercentSpan,
7186 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
7187 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
7188 void **ppBackendData)
7189{
7190 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",
7191 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
7192 int rc;
7193
7194 /* Check the VD container type and image flags. */
7195 if ( enmType != VDTYPE_HDD
7196 || (uImageFlags & ~VD_VMDK_IMAGE_FLAGS_MASK) != 0)
7197 return VERR_VD_INVALID_TYPE;
7198
7199 /* Check size. Maximum 256TB-64K for sparse images, otherwise unlimited. */
7200 if ( !(uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK)
7201 && ( !cbSize
7202 || (!(uImageFlags & VD_IMAGE_FLAGS_FIXED) && cbSize >= _1T * 256 - _64K)))
7203 return VERR_VD_INVALID_SIZE;
7204
7205 /* Check image flags for invalid combinations. */
7206 if ( (uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7207 && (uImageFlags & ~(VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED | VD_IMAGE_FLAGS_DIFF)))
7208 return VERR_INVALID_PARAMETER;
7209
7210 /* Check open flags. All valid flags are supported. */
7211 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
7212 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
7213 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
7214 AssertPtrReturn(pPCHSGeometry, VERR_INVALID_POINTER);
7215 AssertPtrReturn(pLCHSGeometry, VERR_INVALID_POINTER);
7216 AssertReturn(!( uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX
7217 && !(uImageFlags & VD_IMAGE_FLAGS_FIXED)),
7218 VERR_INVALID_PARAMETER);
7219
7220 PVMDKIMAGE pImage = (PVMDKIMAGE)RTMemAllocZ(RT_UOFFSETOF(VMDKIMAGE, RegionList.aRegions[1]));
7221 if (RT_LIKELY(pImage))
7222 {
7223 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
7224
7225 pImage->pszFilename = pszFilename;
7226 pImage->pFile = NULL;
7227 pImage->pExtents = NULL;
7228 pImage->pFiles = NULL;
7229 pImage->pGTCache = NULL;
7230 pImage->pDescData = NULL;
7231 pImage->pVDIfsDisk = pVDIfsDisk;
7232 pImage->pVDIfsImage = pVDIfsImage;
7233 /* Descriptors for split images can be pretty large, especially if the
7234 * filename is long. So prepare for the worst, and allocate quite some
7235 * memory for the descriptor in this case. */
7236 if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
7237 pImage->cbDescAlloc = VMDK_SECTOR2BYTE(200);
7238 else
7239 pImage->cbDescAlloc = VMDK_SECTOR2BYTE(20);
7240 pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
7241 if (RT_LIKELY(pImage->pDescData))
7242 {
7243 rc = vmdkCreateImage(pImage, cbSize, uImageFlags, pszComment,
7244 pPCHSGeometry, pLCHSGeometry, pUuid,
7245 pIfProgress, uPercentStart, uPercentSpan);
7246 if (RT_SUCCESS(rc))
7247 {
7248 /* So far the image is opened in read/write mode. Make sure the
7249 * image is opened in read-only mode if the caller requested that. */
7250 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
7251 {
7252 vmdkFreeImage(pImage, false, true /*fFlush*/);
7253 rc = vmdkOpenImage(pImage, uOpenFlags);
7254 }
7255
7256 if (RT_SUCCESS(rc))
7257 *ppBackendData = pImage;
7258 }
7259
7260 if (RT_FAILURE(rc))
7261 RTMemFree(pImage->pDescData);
7262 }
7263 else
7264 rc = VERR_NO_MEMORY;
7265
7266 if (RT_FAILURE(rc))
7267 RTMemFree(pImage);
7268 }
7269 else
7270 rc = VERR_NO_MEMORY;
7271
7272 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
7273 return rc;
7274}
7275
7276/**
7277 * Prepares the state for renaming a VMDK image, setting up the state and allocating
7278 * memory.
7279 *
7280 * @returns VBox status code.
7281 * @param pImage VMDK image instance.
7282 * @param pRenameState The state to initialize.
7283 * @param pszFilename The new filename.
7284 */
7285static int vmdkRenameStatePrepare(PVMDKIMAGE pImage, PVMDKRENAMESTATE pRenameState, const char *pszFilename)
7286{
7287 AssertReturn(RTPathFilename(pszFilename) != NULL, VERR_INVALID_PARAMETER);
7288
7289 int rc = VINF_SUCCESS;
7290
7291 memset(&pRenameState->DescriptorCopy, 0, sizeof(pRenameState->DescriptorCopy));
7292
7293 /*
7294 * Allocate an array to store both old and new names of renamed files
7295 * in case we have to roll back the changes. Arrays are initialized
7296 * with zeros. We actually save stuff when and if we change it.
7297 */
7298 pRenameState->cExtents = pImage->cExtents;
7299 pRenameState->apszOldName = (char **)RTMemTmpAllocZ((pRenameState->cExtents + 1) * sizeof(char *));
7300 pRenameState->apszNewName = (char **)RTMemTmpAllocZ((pRenameState->cExtents + 1) * sizeof(char *));
7301 pRenameState->apszNewLines = (char **)RTMemTmpAllocZ(pRenameState->cExtents * sizeof(char *));
7302 if ( pRenameState->apszOldName
7303 && pRenameState->apszNewName
7304 && pRenameState->apszNewLines)
7305 {
7306 /* Save the descriptor size and position. */
7307 if (pImage->pDescData)
7308 {
7309 /* Separate descriptor file. */
7310 pRenameState->fEmbeddedDesc = false;
7311 }
7312 else
7313 {
7314 /* Embedded descriptor file. */
7315 pRenameState->ExtentCopy = pImage->pExtents[0];
7316 pRenameState->fEmbeddedDesc = true;
7317 }
7318
7319 /* Save the descriptor content. */
7320 pRenameState->DescriptorCopy.cLines = pImage->Descriptor.cLines;
7321 for (unsigned i = 0; i < pRenameState->DescriptorCopy.cLines; i++)
7322 {
7323 pRenameState->DescriptorCopy.aLines[i] = RTStrDup(pImage->Descriptor.aLines[i]);
7324 if (!pRenameState->DescriptorCopy.aLines[i])
7325 {
7326 rc = VERR_NO_MEMORY;
7327 break;
7328 }
7329 }
7330
7331 if (RT_SUCCESS(rc))
7332 {
7333 /* Prepare both old and new base names used for string replacement. */
7334 pRenameState->pszNewBaseName = RTStrDup(RTPathFilename(pszFilename));
7335 AssertReturn(pRenameState->pszNewBaseName, VERR_NO_STR_MEMORY);
7336 RTPathStripSuffix(pRenameState->pszNewBaseName);
7337
7338 pRenameState->pszOldBaseName = RTStrDup(RTPathFilename(pImage->pszFilename));
7339 AssertReturn(pRenameState->pszOldBaseName, VERR_NO_STR_MEMORY);
7340 RTPathStripSuffix(pRenameState->pszOldBaseName);
7341
7342 /* Prepare both old and new full names used for string replacement.
7343 Note! Must abspath the stuff here, so the strstr weirdness later in
7344 the renaming process get a match against abspath'ed extent paths.
7345 See RTPathAbsDup call in vmdkDescriptorReadSparse(). */
7346 pRenameState->pszNewFullName = RTPathAbsDup(pszFilename);
7347 AssertReturn(pRenameState->pszNewFullName, VERR_NO_STR_MEMORY);
7348 RTPathStripSuffix(pRenameState->pszNewFullName);
7349
7350 pRenameState->pszOldFullName = RTPathAbsDup(pImage->pszFilename);
7351 AssertReturn(pRenameState->pszOldFullName, VERR_NO_STR_MEMORY);
7352 RTPathStripSuffix(pRenameState->pszOldFullName);
7353
7354 /* Save the old name for easy access to the old descriptor file. */
7355 pRenameState->pszOldDescName = RTStrDup(pImage->pszFilename);
7356 AssertReturn(pRenameState->pszOldDescName, VERR_NO_STR_MEMORY);
7357
7358 /* Save old image name. */
7359 pRenameState->pszOldImageName = pImage->pszFilename;
7360 }
7361 }
7362 else
7363 rc = VERR_NO_TMP_MEMORY;
7364
7365 return rc;
7366}
7367
7368/**
7369 * Destroys the given rename state, freeing all allocated memory.
7370 *
7371 * @param pRenameState The rename state to destroy.
7372 */
7373static void vmdkRenameStateDestroy(PVMDKRENAMESTATE pRenameState)
7374{
7375 for (unsigned i = 0; i < pRenameState->DescriptorCopy.cLines; i++)
7376 if (pRenameState->DescriptorCopy.aLines[i])
7377 RTStrFree(pRenameState->DescriptorCopy.aLines[i]);
7378 if (pRenameState->apszOldName)
7379 {
7380 for (unsigned i = 0; i <= pRenameState->cExtents; i++)
7381 if (pRenameState->apszOldName[i])
7382 RTStrFree(pRenameState->apszOldName[i]);
7383 RTMemTmpFree(pRenameState->apszOldName);
7384 }
7385 if (pRenameState->apszNewName)
7386 {
7387 for (unsigned i = 0; i <= pRenameState->cExtents; i++)
7388 if (pRenameState->apszNewName[i])
7389 RTStrFree(pRenameState->apszNewName[i]);
7390 RTMemTmpFree(pRenameState->apszNewName);
7391 }
7392 if (pRenameState->apszNewLines)
7393 {
7394 for (unsigned i = 0; i < pRenameState->cExtents; i++)
7395 if (pRenameState->apszNewLines[i])
7396 RTStrFree(pRenameState->apszNewLines[i]);
7397 RTMemTmpFree(pRenameState->apszNewLines);
7398 }
7399 if (pRenameState->pszOldDescName)
7400 RTStrFree(pRenameState->pszOldDescName);
7401 if (pRenameState->pszOldBaseName)
7402 RTStrFree(pRenameState->pszOldBaseName);
7403 if (pRenameState->pszNewBaseName)
7404 RTStrFree(pRenameState->pszNewBaseName);
7405 if (pRenameState->pszOldFullName)
7406 RTStrFree(pRenameState->pszOldFullName);
7407 if (pRenameState->pszNewFullName)
7408 RTStrFree(pRenameState->pszNewFullName);
7409}
7410
7411/**
7412 * Rolls back the rename operation to the original state.
7413 *
7414 * @returns VBox status code.
7415 * @param pImage VMDK image instance.
7416 * @param pRenameState The rename state.
7417 */
7418static int vmdkRenameRollback(PVMDKIMAGE pImage, PVMDKRENAMESTATE pRenameState)
7419{
7420 int rc = VINF_SUCCESS;
7421
7422 if (!pRenameState->fImageFreed)
7423 {
7424 /*
7425 * Some extents may have been closed, close the rest. We will
7426 * re-open the whole thing later.
7427 */
7428 vmdkFreeImage(pImage, false, true /*fFlush*/);
7429 }
7430
7431 /* Rename files back. */
7432 for (unsigned i = 0; i <= pRenameState->cExtents; i++)
7433 {
7434 if (pRenameState->apszOldName[i])
7435 {
7436 rc = vdIfIoIntFileMove(pImage->pIfIo, pRenameState->apszNewName[i], pRenameState->apszOldName[i], 0);
7437 AssertRC(rc);
7438 }
7439 }
7440 /* Restore the old descriptor. */
7441 PVMDKFILE pFile;
7442 rc = vmdkFileOpen(pImage, &pFile, NULL, pRenameState->pszOldDescName,
7443 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_NORMAL,
7444 false /* fCreate */));
7445 AssertRC(rc);
7446 if (pRenameState->fEmbeddedDesc)
7447 {
7448 pRenameState->ExtentCopy.pFile = pFile;
7449 pImage->pExtents = &pRenameState->ExtentCopy;
7450 }
7451 else
7452 {
7453 /* Shouldn't be null for separate descriptor.
7454 * There will be no access to the actual content.
7455 */
7456 pImage->pDescData = pRenameState->pszOldDescName;
7457 pImage->pFile = pFile;
7458 }
7459 pImage->Descriptor = pRenameState->DescriptorCopy;
7460 vmdkWriteDescriptor(pImage, NULL);
7461 vmdkFileClose(pImage, &pFile, false);
7462 /* Get rid of the stuff we implanted. */
7463 pImage->pExtents = NULL;
7464 pImage->pFile = NULL;
7465 pImage->pDescData = NULL;
7466 /* Re-open the image back. */
7467 pImage->pszFilename = pRenameState->pszOldImageName;
7468 rc = vmdkOpenImage(pImage, pImage->uOpenFlags);
7469
7470 return rc;
7471}
7472
7473/**
7474 * Rename worker doing the real work.
7475 *
7476 * @returns VBox status code.
7477 * @param pImage VMDK image instance.
7478 * @param pRenameState The rename state.
7479 * @param pszFilename The new filename.
7480 */
7481static int vmdkRenameWorker(PVMDKIMAGE pImage, PVMDKRENAMESTATE pRenameState, const char *pszFilename)
7482{
7483 int rc = VINF_SUCCESS;
7484 unsigned i, line;
7485
7486 /* Update the descriptor with modified extent names. */
7487 for (i = 0, line = pImage->Descriptor.uFirstExtent;
7488 i < pRenameState->cExtents;
7489 i++, line = pImage->Descriptor.aNextLines[line])
7490 {
7491 /* Update the descriptor. */
7492 pRenameState->apszNewLines[i] = vmdkStrReplace(pImage->Descriptor.aLines[line],
7493 pRenameState->pszOldBaseName,
7494 pRenameState->pszNewBaseName);
7495 if (!pRenameState->apszNewLines[i])
7496 {
7497 rc = VERR_NO_MEMORY;
7498 break;
7499 }
7500 pImage->Descriptor.aLines[line] = pRenameState->apszNewLines[i];
7501 }
7502
7503 if (RT_SUCCESS(rc))
7504 {
7505 /* Make sure the descriptor gets written back. */
7506 pImage->Descriptor.fDirty = true;
7507 /* Flush the descriptor now, in case it is embedded. */
7508 vmdkFlushImage(pImage, NULL);
7509
7510 /* Close and rename/move extents. */
7511 for (i = 0; i < pRenameState->cExtents; i++)
7512 {
7513 PVMDKEXTENT pExtent = &pImage->pExtents[i];
7514 /* Compose new name for the extent. */
7515 pRenameState->apszNewName[i] = vmdkStrReplace(pExtent->pszFullname,
7516 pRenameState->pszOldFullName,
7517 pRenameState->pszNewFullName);
7518 if (!pRenameState->apszNewName[i])
7519 {
7520 rc = VERR_NO_MEMORY;
7521 break;
7522 }
7523 /* Close the extent file. */
7524 rc = vmdkFileClose(pImage, &pExtent->pFile, false);
7525 if (RT_FAILURE(rc))
7526 break;;
7527
7528 /* Rename the extent file. */
7529 rc = vdIfIoIntFileMove(pImage->pIfIo, pExtent->pszFullname, pRenameState->apszNewName[i], 0);
7530 if (RT_FAILURE(rc))
7531 break;
7532 /* Remember the old name. */
7533 pRenameState->apszOldName[i] = RTStrDup(pExtent->pszFullname);
7534 }
7535
7536 if (RT_SUCCESS(rc))
7537 {
7538 /* Release all old stuff. */
7539 rc = vmdkFreeImage(pImage, false, true /*fFlush*/);
7540 if (RT_SUCCESS(rc))
7541 {
7542 pRenameState->fImageFreed = true;
7543
7544 /* Last elements of new/old name arrays are intended for
7545 * storing descriptor's names.
7546 */
7547 pRenameState->apszNewName[pRenameState->cExtents] = RTStrDup(pszFilename);
7548 /* Rename the descriptor file if it's separate. */
7549 if (!pRenameState->fEmbeddedDesc)
7550 {
7551 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pRenameState->apszNewName[pRenameState->cExtents], 0);
7552 if (RT_SUCCESS(rc))
7553 {
7554 /* Save old name only if we may need to change it back. */
7555 pRenameState->apszOldName[pRenameState->cExtents] = RTStrDup(pszFilename);
7556 }
7557 }
7558
7559 /* Update pImage with the new information. */
7560 pImage->pszFilename = pszFilename;
7561
7562 /* Open the new image. */
7563 rc = vmdkOpenImage(pImage, pImage->uOpenFlags);
7564 }
7565 }
7566 }
7567
7568 return rc;
7569}
7570
7571/** @copydoc VDIMAGEBACKEND::pfnRename */
7572static DECLCALLBACK(int) vmdkRename(void *pBackendData, const char *pszFilename)
7573{
7574 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
7575
7576 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7577 VMDKRENAMESTATE RenameState;
7578
7579 memset(&RenameState, 0, sizeof(RenameState));
7580
7581 /* Check arguments. */
7582 AssertPtrReturn(pImage, VERR_INVALID_POINTER);
7583 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
7584 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
7585 AssertReturn(!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK), VERR_INVALID_PARAMETER);
7586
7587 int rc = vmdkRenameStatePrepare(pImage, &RenameState, pszFilename);
7588 if (RT_SUCCESS(rc))
7589 {
7590 /* --- Up to this point we have not done any damage yet. --- */
7591
7592 rc = vmdkRenameWorker(pImage, &RenameState, pszFilename);
7593 /* Roll back all changes in case of failure. */
7594 if (RT_FAILURE(rc))
7595 {
7596 int rrc = vmdkRenameRollback(pImage, &RenameState);
7597 AssertRC(rrc);
7598 }
7599 }
7600
7601 vmdkRenameStateDestroy(&RenameState);
7602 LogFlowFunc(("returns %Rrc\n", rc));
7603 return rc;
7604}
7605
7606/** @copydoc VDIMAGEBACKEND::pfnClose */
7607static DECLCALLBACK(int) vmdkClose(void *pBackendData, bool fDelete)
7608{
7609 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
7610 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7611
7612 int rc = vmdkFreeImage(pImage, fDelete, true /*fFlush*/);
7613 RTMemFree(pImage);
7614
7615 LogFlowFunc(("returns %Rrc\n", rc));
7616 return rc;
7617}
7618
7619/** @copydoc VDIMAGEBACKEND::pfnRead */
7620static DECLCALLBACK(int) vmdkRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
7621 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
7622{
7623 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
7624 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
7625 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7626
7627 AssertPtr(pImage);
7628 Assert(uOffset % 512 == 0);
7629 Assert(cbToRead % 512 == 0);
7630 AssertPtrReturn(pIoCtx, VERR_INVALID_POINTER);
7631 AssertReturn(cbToRead, VERR_INVALID_PARAMETER);
7632 AssertReturn(uOffset + cbToRead <= pImage->cbSize, VERR_INVALID_PARAMETER);
7633
7634 /* Find the extent and check access permissions as defined in the extent descriptor. */
7635 PVMDKEXTENT pExtent;
7636 uint64_t uSectorExtentRel;
7637 int rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
7638 &pExtent, &uSectorExtentRel);
7639 if ( RT_SUCCESS(rc)
7640 && pExtent->enmAccess != VMDKACCESS_NOACCESS)
7641 {
7642 /* Clip read range to remain in this extent. */
7643 cbToRead = RT_MIN(cbToRead, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
7644
7645 /* Handle the read according to the current extent type. */
7646 switch (pExtent->enmType)
7647 {
7648 case VMDKETYPE_HOSTED_SPARSE:
7649 {
7650 uint64_t uSectorExtentAbs;
7651
7652 rc = vmdkGetSector(pImage, pIoCtx, pExtent, uSectorExtentRel, &uSectorExtentAbs);
7653 if (RT_FAILURE(rc))
7654 break;
7655 /* Clip read range to at most the rest of the grain. */
7656 cbToRead = RT_MIN(cbToRead, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSectorExtentRel % pExtent->cSectorsPerGrain));
7657 Assert(!(cbToRead % 512));
7658 if (uSectorExtentAbs == 0)
7659 {
7660 if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7661 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
7662 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
7663 rc = VERR_VD_BLOCK_FREE;
7664 else
7665 rc = vmdkStreamReadSequential(pImage, pExtent,
7666 uSectorExtentRel,
7667 pIoCtx, cbToRead);
7668 }
7669 else
7670 {
7671 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7672 {
7673 AssertMsg(vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx),
7674 ("Async I/O is not supported for stream optimized VMDK's\n"));
7675
7676 uint32_t uSectorInGrain = uSectorExtentRel % pExtent->cSectorsPerGrain;
7677 uSectorExtentAbs -= uSectorInGrain;
7678 if (pExtent->uGrainSectorAbs != uSectorExtentAbs)
7679 {
7680 uint64_t uLBA = 0; /* gcc maybe uninitialized */
7681 rc = vmdkFileInflateSync(pImage, pExtent,
7682 VMDK_SECTOR2BYTE(uSectorExtentAbs),
7683 pExtent->pvGrain,
7684 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
7685 NULL, &uLBA, NULL);
7686 if (RT_FAILURE(rc))
7687 {
7688 pExtent->uGrainSectorAbs = 0;
7689 break;
7690 }
7691 pExtent->uGrainSectorAbs = uSectorExtentAbs;
7692 pExtent->uGrain = uSectorExtentRel / pExtent->cSectorsPerGrain;
7693 Assert(uLBA == uSectorExtentRel);
7694 }
7695 vdIfIoIntIoCtxCopyTo(pImage->pIfIo, pIoCtx,
7696 (uint8_t *)pExtent->pvGrain
7697 + VMDK_SECTOR2BYTE(uSectorInGrain),
7698 cbToRead);
7699 }
7700 else
7701 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pExtent->pFile->pStorage,
7702 VMDK_SECTOR2BYTE(uSectorExtentAbs),
7703 pIoCtx, cbToRead);
7704 }
7705 break;
7706 }
7707 case VMDKETYPE_VMFS:
7708 case VMDKETYPE_FLAT:
7709 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pExtent->pFile->pStorage,
7710 VMDK_SECTOR2BYTE(uSectorExtentRel),
7711 pIoCtx, cbToRead);
7712 break;
7713 case VMDKETYPE_ZERO:
7714 {
7715 size_t cbSet = vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
7716 Assert(cbSet == cbToRead); RT_NOREF(cbSet);
7717 break;
7718 }
7719 }
7720 if (pcbActuallyRead)
7721 *pcbActuallyRead = cbToRead;
7722 }
7723 else if (RT_SUCCESS(rc))
7724 rc = VERR_VD_VMDK_INVALID_STATE;
7725
7726 LogFlowFunc(("returns %Rrc\n", rc));
7727 return rc;
7728}
7729
7730/** @copydoc VDIMAGEBACKEND::pfnWrite */
7731static DECLCALLBACK(int) vmdkWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
7732 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
7733 size_t *pcbPostRead, unsigned fWrite)
7734{
7735 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
7736 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
7737 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7738 int rc;
7739
7740 AssertPtr(pImage);
7741 Assert(uOffset % 512 == 0);
7742 Assert(cbToWrite % 512 == 0);
7743 AssertPtrReturn(pIoCtx, VERR_INVALID_POINTER);
7744 AssertReturn(cbToWrite, VERR_INVALID_PARAMETER);
7745
7746 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
7747 {
7748 PVMDKEXTENT pExtent;
7749 uint64_t uSectorExtentRel;
7750 uint64_t uSectorExtentAbs;
7751
7752 /* No size check here, will do that later when the extent is located.
7753 * There are sparse images out there which according to the spec are
7754 * invalid, because the total size is not a multiple of the grain size.
7755 * Also for sparse images which are stitched together in odd ways (not at
7756 * grain boundaries, and with the nominal size not being a multiple of the
7757 * grain size), this would prevent writing to the last grain. */
7758
7759 rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
7760 &pExtent, &uSectorExtentRel);
7761 if (RT_SUCCESS(rc))
7762 {
7763 if ( pExtent->enmAccess != VMDKACCESS_READWRITE
7764 && ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7765 && !pImage->pExtents[0].uAppendPosition
7766 && pExtent->enmAccess != VMDKACCESS_READONLY))
7767 rc = VERR_VD_VMDK_INVALID_STATE;
7768 else
7769 {
7770 /* Handle the write according to the current extent type. */
7771 switch (pExtent->enmType)
7772 {
7773 case VMDKETYPE_HOSTED_SPARSE:
7774 rc = vmdkGetSector(pImage, pIoCtx, pExtent, uSectorExtentRel, &uSectorExtentAbs);
7775 if (RT_SUCCESS(rc))
7776 {
7777 if ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
7778 && uSectorExtentRel < (uint64_t)pExtent->uLastGrainAccess * pExtent->cSectorsPerGrain)
7779 rc = VERR_VD_VMDK_INVALID_WRITE;
7780 else
7781 {
7782 /* Clip write range to at most the rest of the grain. */
7783 cbToWrite = RT_MIN(cbToWrite,
7784 VMDK_SECTOR2BYTE( pExtent->cSectorsPerGrain
7785 - uSectorExtentRel % pExtent->cSectorsPerGrain));
7786 if (uSectorExtentAbs == 0)
7787 {
7788 if (!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
7789 {
7790 if (cbToWrite == VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
7791 {
7792 /* Full block write to a previously unallocated block.
7793 * Check if the caller wants to avoid the automatic alloc. */
7794 if (!(fWrite & VD_WRITE_NO_ALLOC))
7795 {
7796 /* Allocate GT and find out where to store the grain. */
7797 rc = vmdkAllocGrain(pImage, pExtent, pIoCtx,
7798 uSectorExtentRel, cbToWrite);
7799 }
7800 else
7801 rc = VERR_VD_BLOCK_FREE;
7802 *pcbPreRead = 0;
7803 *pcbPostRead = 0;
7804 }
7805 else
7806 {
7807 /* Clip write range to remain in this extent. */
7808 cbToWrite = RT_MIN(cbToWrite,
7809 VMDK_SECTOR2BYTE( pExtent->uSectorOffset
7810 + pExtent->cNominalSectors - uSectorExtentRel));
7811 *pcbPreRead = VMDK_SECTOR2BYTE(uSectorExtentRel % pExtent->cSectorsPerGrain);
7812 *pcbPostRead = VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain) - cbToWrite - *pcbPreRead;
7813 rc = VERR_VD_BLOCK_FREE;
7814 }
7815 }
7816 else
7817 rc = vmdkStreamAllocGrain(pImage, pExtent, uSectorExtentRel,
7818 pIoCtx, cbToWrite);
7819 }
7820 else
7821 {
7822 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
7823 {
7824 /* A partial write to a streamOptimized image is simply
7825 * invalid. It requires rewriting already compressed data
7826 * which is somewhere between expensive and impossible. */
7827 rc = VERR_VD_VMDK_INVALID_STATE;
7828 pExtent->uGrainSectorAbs = 0;
7829 AssertRC(rc);
7830 }
7831 else
7832 {
7833 Assert(!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED));
7834 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pExtent->pFile->pStorage,
7835 VMDK_SECTOR2BYTE(uSectorExtentAbs),
7836 pIoCtx, cbToWrite, NULL, NULL);
7837 }
7838 }
7839 }
7840 }
7841 break;
7842 case VMDKETYPE_VMFS:
7843 case VMDKETYPE_FLAT:
7844 /* Clip write range to remain in this extent. */
7845 cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
7846 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pExtent->pFile->pStorage,
7847 VMDK_SECTOR2BYTE(uSectorExtentRel),
7848 pIoCtx, cbToWrite, NULL, NULL);
7849 break;
7850 case VMDKETYPE_ZERO:
7851 /* Clip write range to remain in this extent. */
7852 cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
7853 break;
7854 }
7855 }
7856
7857 if (pcbWriteProcess)
7858 *pcbWriteProcess = cbToWrite;
7859 }
7860 }
7861 else
7862 rc = VERR_VD_IMAGE_READ_ONLY;
7863
7864 LogFlowFunc(("returns %Rrc\n", rc));
7865 return rc;
7866}
7867
7868/** @copydoc VDIMAGEBACKEND::pfnFlush */
7869static DECLCALLBACK(int) vmdkFlush(void *pBackendData, PVDIOCTX pIoCtx)
7870{
7871 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7872
7873 return vmdkFlushImage(pImage, pIoCtx);
7874}
7875
7876/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
7877static DECLCALLBACK(unsigned) vmdkGetVersion(void *pBackendData)
7878{
7879 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
7880 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7881
7882 AssertPtrReturn(pImage, 0);
7883
7884 return VMDK_IMAGE_VERSION;
7885}
7886
7887/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
7888static DECLCALLBACK(uint64_t) vmdkGetFileSize(void *pBackendData)
7889{
7890 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
7891 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7892 uint64_t cb = 0;
7893
7894 AssertPtrReturn(pImage, 0);
7895
7896 if (pImage->pFile != NULL)
7897 {
7898 uint64_t cbFile;
7899 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pFile->pStorage, &cbFile);
7900 if (RT_SUCCESS(rc))
7901 cb += cbFile;
7902 }
7903 for (unsigned i = 0; i < pImage->cExtents; i++)
7904 {
7905 if (pImage->pExtents[i].pFile != NULL)
7906 {
7907 uint64_t cbFile;
7908 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pExtents[i].pFile->pStorage, &cbFile);
7909 if (RT_SUCCESS(rc))
7910 cb += cbFile;
7911 }
7912 }
7913
7914 LogFlowFunc(("returns %lld\n", cb));
7915 return cb;
7916}
7917
7918/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
7919static DECLCALLBACK(int) vmdkGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
7920{
7921 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
7922 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7923 int rc = VINF_SUCCESS;
7924
7925 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
7926
7927 if (pImage->PCHSGeometry.cCylinders)
7928 *pPCHSGeometry = pImage->PCHSGeometry;
7929 else
7930 rc = VERR_VD_GEOMETRY_NOT_SET;
7931
7932 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
7933 return rc;
7934}
7935
7936/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
7937static DECLCALLBACK(int) vmdkSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
7938{
7939 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
7940 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
7941 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7942 int rc = VINF_SUCCESS;
7943
7944 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
7945
7946 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
7947 {
7948 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
7949 {
7950 rc = vmdkDescSetPCHSGeometry(pImage, pPCHSGeometry);
7951 if (RT_SUCCESS(rc))
7952 pImage->PCHSGeometry = *pPCHSGeometry;
7953 }
7954 else
7955 rc = VERR_NOT_SUPPORTED;
7956 }
7957 else
7958 rc = VERR_VD_IMAGE_READ_ONLY;
7959
7960 LogFlowFunc(("returns %Rrc\n", rc));
7961 return rc;
7962}
7963
7964/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
7965static DECLCALLBACK(int) vmdkGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
7966{
7967 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
7968 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7969 int rc = VINF_SUCCESS;
7970
7971 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
7972
7973 if (pImage->LCHSGeometry.cCylinders)
7974 *pLCHSGeometry = pImage->LCHSGeometry;
7975 else
7976 rc = VERR_VD_GEOMETRY_NOT_SET;
7977
7978 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
7979 return rc;
7980}
7981
7982/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
7983static DECLCALLBACK(int) vmdkSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
7984{
7985 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
7986 pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
7987 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
7988 int rc = VINF_SUCCESS;
7989
7990 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
7991
7992 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
7993 {
7994 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
7995 {
7996 rc = vmdkDescSetLCHSGeometry(pImage, pLCHSGeometry);
7997 if (RT_SUCCESS(rc))
7998 pImage->LCHSGeometry = *pLCHSGeometry;
7999 }
8000 else
8001 rc = VERR_NOT_SUPPORTED;
8002 }
8003 else
8004 rc = VERR_VD_IMAGE_READ_ONLY;
8005
8006 LogFlowFunc(("returns %Rrc\n", rc));
8007 return rc;
8008}
8009
8010/** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
8011static DECLCALLBACK(int) vmdkQueryRegions(void *pBackendData, PCVDREGIONLIST *ppRegionList)
8012{
8013 LogFlowFunc(("pBackendData=%#p ppRegionList=%#p\n", pBackendData, ppRegionList));
8014 PVMDKIMAGE pThis = (PVMDKIMAGE)pBackendData;
8015
8016 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
8017
8018 *ppRegionList = &pThis->RegionList;
8019 LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
8020 return VINF_SUCCESS;
8021}
8022
8023/** @copydoc VDIMAGEBACKEND::pfnRegionListRelease */
8024static DECLCALLBACK(void) vmdkRegionListRelease(void *pBackendData, PCVDREGIONLIST pRegionList)
8025{
8026 RT_NOREF1(pRegionList);
8027 LogFlowFunc(("pBackendData=%#p pRegionList=%#p\n", pBackendData, pRegionList));
8028 PVMDKIMAGE pThis = (PVMDKIMAGE)pBackendData;
8029 AssertPtr(pThis); RT_NOREF(pThis);
8030
8031 /* Nothing to do here. */
8032}
8033
8034/** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
8035static DECLCALLBACK(unsigned) vmdkGetImageFlags(void *pBackendData)
8036{
8037 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
8038 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8039
8040 AssertPtrReturn(pImage, 0);
8041
8042 LogFlowFunc(("returns %#x\n", pImage->uImageFlags));
8043 return pImage->uImageFlags;
8044}
8045
8046/** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
8047static DECLCALLBACK(unsigned) vmdkGetOpenFlags(void *pBackendData)
8048{
8049 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
8050 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8051
8052 AssertPtrReturn(pImage, 0);
8053
8054 LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
8055 return pImage->uOpenFlags;
8056}
8057
8058/** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
8059static DECLCALLBACK(int) vmdkSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
8060{
8061 LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
8062 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8063 int rc;
8064
8065 /* Image must be opened and the new flags must be valid. */
8066 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
8067 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
8068 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
8069 rc = VERR_INVALID_PARAMETER;
8070 else
8071 {
8072 /* StreamOptimized images need special treatment: reopen is prohibited. */
8073 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
8074 {
8075 if (pImage->uOpenFlags == uOpenFlags)
8076 rc = VINF_SUCCESS;
8077 else
8078 rc = VERR_INVALID_PARAMETER;
8079 }
8080 else
8081 {
8082 /* Implement this operation via reopening the image. */
8083 vmdkFreeImage(pImage, false, true /*fFlush*/);
8084 rc = vmdkOpenImage(pImage, uOpenFlags);
8085 }
8086 }
8087
8088 LogFlowFunc(("returns %Rrc\n", rc));
8089 return rc;
8090}
8091
8092/** @copydoc VDIMAGEBACKEND::pfnGetComment */
8093static DECLCALLBACK(int) vmdkGetComment(void *pBackendData, char *pszComment, size_t cbComment)
8094{
8095 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
8096 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8097
8098 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8099
8100 char *pszCommentEncoded = NULL;
8101 int rc = vmdkDescDDBGetStr(pImage, &pImage->Descriptor,
8102 "ddb.comment", &pszCommentEncoded);
8103 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
8104 {
8105 pszCommentEncoded = NULL;
8106 rc = VINF_SUCCESS;
8107 }
8108
8109 if (RT_SUCCESS(rc))
8110 {
8111 if (pszComment && pszCommentEncoded)
8112 rc = vmdkDecodeString(pszCommentEncoded, pszComment, cbComment);
8113 else if (pszComment)
8114 *pszComment = '\0';
8115
8116 if (pszCommentEncoded)
8117 RTMemTmpFree(pszCommentEncoded);
8118 }
8119
8120 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
8121 return rc;
8122}
8123
8124/** @copydoc VDIMAGEBACKEND::pfnSetComment */
8125static DECLCALLBACK(int) vmdkSetComment(void *pBackendData, const char *pszComment)
8126{
8127 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
8128 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8129 int rc;
8130
8131 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8132
8133 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8134 {
8135 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8136 rc = vmdkSetImageComment(pImage, pszComment);
8137 else
8138 rc = VERR_NOT_SUPPORTED;
8139 }
8140 else
8141 rc = VERR_VD_IMAGE_READ_ONLY;
8142
8143 LogFlowFunc(("returns %Rrc\n", rc));
8144 return rc;
8145}
8146
8147/** @copydoc VDIMAGEBACKEND::pfnGetUuid */
8148static DECLCALLBACK(int) vmdkGetUuid(void *pBackendData, PRTUUID pUuid)
8149{
8150 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
8151 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8152
8153 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8154
8155 *pUuid = pImage->ImageUuid;
8156
8157 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
8158 return VINF_SUCCESS;
8159}
8160
8161/** @copydoc VDIMAGEBACKEND::pfnSetUuid */
8162static DECLCALLBACK(int) vmdkSetUuid(void *pBackendData, PCRTUUID pUuid)
8163{
8164 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
8165 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8166 int rc = VINF_SUCCESS;
8167
8168 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8169
8170 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8171 {
8172 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8173 {
8174 pImage->ImageUuid = *pUuid;
8175 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
8176 VMDK_DDB_IMAGE_UUID, pUuid);
8177 if (RT_FAILURE(rc))
8178 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
8179 N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
8180 }
8181 else
8182 rc = VERR_NOT_SUPPORTED;
8183 }
8184 else
8185 rc = VERR_VD_IMAGE_READ_ONLY;
8186
8187 LogFlowFunc(("returns %Rrc\n", rc));
8188 return rc;
8189}
8190
8191/** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
8192static DECLCALLBACK(int) vmdkGetModificationUuid(void *pBackendData, PRTUUID pUuid)
8193{
8194 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
8195 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8196
8197 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8198
8199 *pUuid = pImage->ModificationUuid;
8200
8201 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
8202 return VINF_SUCCESS;
8203}
8204
8205/** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
8206static DECLCALLBACK(int) vmdkSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
8207{
8208 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
8209 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8210 int rc = VINF_SUCCESS;
8211
8212 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8213
8214 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8215 {
8216 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8217 {
8218 /* Only touch the modification uuid if it changed. */
8219 if (RTUuidCompare(&pImage->ModificationUuid, pUuid))
8220 {
8221 pImage->ModificationUuid = *pUuid;
8222 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
8223 VMDK_DDB_MODIFICATION_UUID, pUuid);
8224 if (RT_FAILURE(rc))
8225 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing modification UUID in descriptor in '%s'"), pImage->pszFilename);
8226 }
8227 }
8228 else
8229 rc = VERR_NOT_SUPPORTED;
8230 }
8231 else
8232 rc = VERR_VD_IMAGE_READ_ONLY;
8233
8234 LogFlowFunc(("returns %Rrc\n", rc));
8235 return rc;
8236}
8237
8238/** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
8239static DECLCALLBACK(int) vmdkGetParentUuid(void *pBackendData, PRTUUID pUuid)
8240{
8241 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
8242 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8243
8244 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8245
8246 *pUuid = pImage->ParentUuid;
8247
8248 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
8249 return VINF_SUCCESS;
8250}
8251
8252/** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
8253static DECLCALLBACK(int) vmdkSetParentUuid(void *pBackendData, PCRTUUID pUuid)
8254{
8255 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
8256 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8257 int rc = VINF_SUCCESS;
8258
8259 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8260
8261 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8262 {
8263 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8264 {
8265 pImage->ParentUuid = *pUuid;
8266 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
8267 VMDK_DDB_PARENT_UUID, pUuid);
8268 if (RT_FAILURE(rc))
8269 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
8270 N_("VMDK: error storing parent image UUID in descriptor in '%s'"), pImage->pszFilename);
8271 }
8272 else
8273 rc = VERR_NOT_SUPPORTED;
8274 }
8275 else
8276 rc = VERR_VD_IMAGE_READ_ONLY;
8277
8278 LogFlowFunc(("returns %Rrc\n", rc));
8279 return rc;
8280}
8281
8282/** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
8283static DECLCALLBACK(int) vmdkGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
8284{
8285 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
8286 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8287
8288 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8289
8290 *pUuid = pImage->ParentModificationUuid;
8291
8292 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
8293 return VINF_SUCCESS;
8294}
8295
8296/** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
8297static DECLCALLBACK(int) vmdkSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
8298{
8299 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
8300 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8301 int rc = VINF_SUCCESS;
8302
8303 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
8304
8305 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
8306 {
8307 if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
8308 {
8309 pImage->ParentModificationUuid = *pUuid;
8310 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
8311 VMDK_DDB_PARENT_MODIFICATION_UUID, pUuid);
8312 if (RT_FAILURE(rc))
8313 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent image UUID in descriptor in '%s'"), pImage->pszFilename);
8314 }
8315 else
8316 rc = VERR_NOT_SUPPORTED;
8317 }
8318 else
8319 rc = VERR_VD_IMAGE_READ_ONLY;
8320
8321 LogFlowFunc(("returns %Rrc\n", rc));
8322 return rc;
8323}
8324
8325/** @copydoc VDIMAGEBACKEND::pfnDump */
8326static DECLCALLBACK(void) vmdkDump(void *pBackendData)
8327{
8328 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8329
8330 AssertPtrReturnVoid(pImage);
8331 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
8332 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
8333 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
8334 VMDK_BYTE2SECTOR(pImage->cbSize));
8335 vdIfErrorMessage(pImage->pIfError, "Header: uuidCreation={%RTuuid}\n", &pImage->ImageUuid);
8336 vdIfErrorMessage(pImage->pIfError, "Header: uuidModification={%RTuuid}\n", &pImage->ModificationUuid);
8337 vdIfErrorMessage(pImage->pIfError, "Header: uuidParent={%RTuuid}\n", &pImage->ParentUuid);
8338 vdIfErrorMessage(pImage->pIfError, "Header: uuidParentModification={%RTuuid}\n", &pImage->ParentModificationUuid);
8339}
8340
8341
8342/**
8343 * Returns the size, in bytes, of the sparse extent overhead for
8344 * the number of desired total sectors and based on the current
8345 * sectors of the extent.
8346 *
8347 * @returns uint64_t size of new overhead in bytes.
8348 * @param pExtent VMDK extent instance.
8349 * @param cSectorsNew Number of desired total sectors.
8350 */
8351static uint64_t vmdkGetNewOverhead(PVMDKEXTENT pExtent, uint64_t cSectorsNew)
8352{
8353 uint64_t cNewDirEntries = cSectorsNew / pExtent->cSectorsPerGDE;
8354 if (cSectorsNew % pExtent->cSectorsPerGDE)
8355 cNewDirEntries++;
8356
8357 size_t cbNewGD = cNewDirEntries * sizeof(uint32_t);
8358 uint64_t cbNewDirSize = RT_ALIGN_64(cbNewGD, 512);
8359 uint64_t cbNewAllTablesSize = RT_ALIGN_64(cNewDirEntries * pExtent->cGTEntries * sizeof(uint32_t), 512);
8360 uint64_t cbNewOverhead = RT_ALIGN_Z(RT_MAX(pExtent->uDescriptorSector
8361 + pExtent->cDescriptorSectors, 1)
8362 + cbNewDirSize + cbNewAllTablesSize, 512);
8363 cbNewOverhead += cbNewDirSize + cbNewAllTablesSize;
8364 cbNewOverhead = RT_ALIGN_64(cbNewOverhead,
8365 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8366
8367 return cbNewOverhead;
8368}
8369
8370/**
8371 * Internal: Replaces the size (in sectors) of an extent in the descriptor file.
8372 *
8373 * @returns VBox status code.
8374 * @param pImage VMDK image instance.
8375 * @param pExtent VMDK extent instance.
8376 * @param uLine Line number of descriptor to change.
8377 * @param cSectorsOld Existing number of sectors.
8378 * @param cSectorsNew New number of sectors.
8379 */
8380static int vmdkReplaceExtentSize(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, unsigned uLine, uint64_t cSectorsOld,
8381 uint64_t cSectorsNew)
8382{
8383 char szOldExtentSectors[UINT64_MAX_BUFF_SIZE];
8384 char szNewExtentSectors[UINT64_MAX_BUFF_SIZE];
8385
8386 ssize_t cbWritten = RTStrPrintf2(szOldExtentSectors, sizeof(szOldExtentSectors), "%llu", cSectorsOld);
8387 if (cbWritten <= 0 || cbWritten > (ssize_t)sizeof(szOldExtentSectors))
8388 return VERR_BUFFER_OVERFLOW;
8389
8390 cbWritten = RTStrPrintf2(szNewExtentSectors, sizeof(szNewExtentSectors), "%llu", cSectorsNew);
8391 if (cbWritten <= 0 || cbWritten > (ssize_t)sizeof(szNewExtentSectors))
8392 return VERR_BUFFER_OVERFLOW;
8393
8394 char *pszNewExtentLine = vmdkStrReplace(pImage->Descriptor.aLines[uLine],
8395 szOldExtentSectors,
8396 szNewExtentSectors);
8397
8398 if (RT_UNLIKELY(!pszNewExtentLine))
8399 return VERR_INVALID_PARAMETER;
8400
8401 vmdkDescExtRemoveByLine(pImage, &pImage->Descriptor, uLine);
8402 vmdkDescExtInsert(pImage, &pImage->Descriptor,
8403 pExtent->enmAccess, cSectorsNew,
8404 pExtent->enmType, pExtent->pszBasename, pExtent->uSectorOffset);
8405
8406 RTStrFree(pszNewExtentLine);
8407 pszNewExtentLine = NULL;
8408
8409 pImage->Descriptor.fDirty = true;
8410
8411 return VINF_SUCCESS;
8412}
8413
8414/**
8415 * Moves sectors down to make room for new overhead.
8416 * Used for sparse extent resize.
8417 *
8418 * @returns VBox status code.
8419 * @param pImage VMDK image instance.
8420 * @param pExtent VMDK extent instance.
8421 * @param cSectorsNew Number of sectors after resize.
8422 */
8423static int vmdkRelocateSectorsForSparseResize(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
8424 uint64_t cSectorsNew)
8425{
8426 int rc = VINF_SUCCESS;
8427
8428 uint64_t cbNewOverhead = vmdkGetNewOverhead(pExtent, cSectorsNew);
8429
8430 uint64_t cNewOverheadSectors = VMDK_BYTE2SECTOR(cbNewOverhead);
8431 uint64_t cOverheadSectorDiff = cNewOverheadSectors - pExtent->cOverheadSectors;
8432
8433 uint64_t cbFile = 0;
8434 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbFile);
8435
8436 uint64_t uNewAppendPosition;
8437
8438 /* Calculate how many sectors need to be relocated. */
8439 unsigned cSectorsReloc = cOverheadSectorDiff;
8440 if (cbNewOverhead % VMDK_SECTOR_SIZE)
8441 cSectorsReloc++;
8442
8443 if (cSectorsReloc < pExtent->cSectors)
8444 uNewAppendPosition = RT_ALIGN_Z(cbFile + VMDK_SECTOR2BYTE(cOverheadSectorDiff), 512);
8445 else
8446 uNewAppendPosition = cbFile;
8447
8448 /*
8449 * Get the blocks we need to relocate first, they are appended to the end
8450 * of the image.
8451 */
8452 void *pvBuf = NULL, *pvZero = NULL;
8453 do
8454 {
8455 /* Allocate data buffer. */
8456 pvBuf = RTMemAllocZ(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8457 if (!pvBuf)
8458 {
8459 rc = VERR_NO_MEMORY;
8460 break;
8461 }
8462
8463 /* Allocate buffer for overwriting with zeroes. */
8464 pvZero = RTMemAllocZ(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8465 if (!pvZero)
8466 {
8467 RTMemFree(pvBuf);
8468 pvBuf = NULL;
8469
8470 rc = VERR_NO_MEMORY;
8471 break;
8472 }
8473
8474 uint32_t *aGTDataTmp = (uint32_t *)RTMemAllocZ(sizeof(uint32_t) * pExtent->cGTEntries);
8475 if(!aGTDataTmp)
8476 {
8477 RTMemFree(pvBuf);
8478 pvBuf = NULL;
8479
8480 RTMemFree(pvZero);
8481 pvZero = NULL;
8482
8483 rc = VERR_NO_MEMORY;
8484 break;
8485 }
8486
8487 uint32_t *aRGTDataTmp = (uint32_t *)RTMemAllocZ(sizeof(uint32_t) * pExtent->cGTEntries);
8488 if(!aRGTDataTmp)
8489 {
8490 RTMemFree(pvBuf);
8491 pvBuf = NULL;
8492
8493 RTMemFree(pvZero);
8494 pvZero = NULL;
8495
8496 RTMemFree(aGTDataTmp);
8497 aGTDataTmp = NULL;
8498
8499 rc = VERR_NO_MEMORY;
8500 break;
8501 }
8502
8503 /* Search for overlap sector in the grain table. */
8504 for (uint32_t idxGD = 0; idxGD < pExtent->cGDEntries; idxGD++)
8505 {
8506 uint64_t uGTSector = pExtent->pGD[idxGD];
8507 uint64_t uRGTSector = pExtent->pRGD[idxGD];
8508
8509 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8510 VMDK_SECTOR2BYTE(uGTSector),
8511 aGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries);
8512
8513 if (RT_FAILURE(rc))
8514 break;
8515
8516 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8517 VMDK_SECTOR2BYTE(uRGTSector),
8518 aRGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries);
8519
8520 if (RT_FAILURE(rc))
8521 break;
8522
8523 for (uint32_t idxGT = 0; idxGT < pExtent->cGTEntries; idxGT++)
8524 {
8525 uint64_t aGTEntryLE = RT_LE2H_U64(aGTDataTmp[idxGT]);
8526 uint64_t aRGTEntryLE = RT_LE2H_U64(aRGTDataTmp[idxGT]);
8527
8528 /**
8529 * Check if grain table is valid. If not dump out with an error.
8530 * Shoudln't ever get here (given other checks) but good sanity check.
8531 */
8532 if (aGTEntryLE != aRGTEntryLE)
8533 {
8534 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
8535 N_("VMDK: inconsistent references within grain table in '%s'"), pExtent->pszFullname);
8536 break;
8537 }
8538
8539 if (aGTEntryLE < cNewOverheadSectors
8540 && aGTEntryLE != 0)
8541 {
8542 /* Read data and append grain to the end of the image. */
8543 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8544 VMDK_SECTOR2BYTE(aGTEntryLE), pvBuf,
8545 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8546 if (RT_FAILURE(rc))
8547 break;
8548
8549 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8550 uNewAppendPosition, pvBuf,
8551 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8552 if (RT_FAILURE(rc))
8553 break;
8554
8555 /* Zero out the old block area. */
8556 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8557 VMDK_SECTOR2BYTE(aGTEntryLE), pvZero,
8558 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
8559 if (RT_FAILURE(rc))
8560 break;
8561
8562 /* Write updated grain tables to file */
8563 aGTDataTmp[idxGT] = VMDK_BYTE2SECTOR(uNewAppendPosition);
8564 aRGTDataTmp[idxGT] = VMDK_BYTE2SECTOR(uNewAppendPosition);
8565
8566 if (memcmp(aGTDataTmp, aRGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries))
8567 {
8568 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
8569 N_("VMDK: inconsistency between grain table and backup grain table in '%s'"), pExtent->pszFullname);
8570 break;
8571 }
8572
8573 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8574 VMDK_SECTOR2BYTE(uGTSector),
8575 aGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries);
8576
8577 if (RT_FAILURE(rc))
8578 break;
8579
8580 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8581 VMDK_SECTOR2BYTE(uRGTSector),
8582 aRGTDataTmp, sizeof(uint32_t) * pExtent->cGTEntries);
8583
8584 break;
8585 }
8586 }
8587 }
8588
8589 RTMemFree(aGTDataTmp);
8590 aGTDataTmp = NULL;
8591
8592 RTMemFree(aRGTDataTmp);
8593 aRGTDataTmp = NULL;
8594
8595 if (RT_FAILURE(rc))
8596 break;
8597
8598 uNewAppendPosition += VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain);
8599 } while (0);
8600
8601 if (pvBuf)
8602 {
8603 RTMemFree(pvBuf);
8604 pvBuf = NULL;
8605 }
8606
8607 if (pvZero)
8608 {
8609 RTMemFree(pvZero);
8610 pvZero = NULL;
8611 }
8612
8613 // Update append position for extent
8614 pExtent->uAppendPosition = uNewAppendPosition;
8615
8616 return rc;
8617}
8618
8619/**
8620 * Resizes meta/overhead for sparse extent resize.
8621 *
8622 * @returns VBox status code.
8623 * @param pImage VMDK image instance.
8624 * @param pExtent VMDK extent instance.
8625 * @param cSectorsNew Number of sectors after resize.
8626 */
8627static int vmdkResizeSparseMeta(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
8628 uint64_t cSectorsNew)
8629{
8630 int rc = VINF_SUCCESS;
8631 uint32_t cOldGDEntries = pExtent->cGDEntries;
8632
8633 uint64_t cNewDirEntries = cSectorsNew / pExtent->cSectorsPerGDE;
8634 if (cSectorsNew % pExtent->cSectorsPerGDE)
8635 cNewDirEntries++;
8636
8637 size_t cbNewGD = cNewDirEntries * sizeof(uint32_t);
8638
8639 uint64_t cbNewDirSize = RT_ALIGN_64(cbNewGD, 512);
8640 uint64_t cbCurrDirSize = RT_ALIGN_64(pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE, 512);
8641 uint64_t cDirSectorDiff = VMDK_BYTE2SECTOR(cbNewDirSize - cbCurrDirSize);
8642
8643 uint64_t cbNewAllTablesSize = RT_ALIGN_64(cNewDirEntries * pExtent->cGTEntries * sizeof(uint32_t), 512);
8644 uint64_t cbCurrAllTablesSize = RT_ALIGN_64(pExtent->cGDEntries * VMDK_GRAIN_TABLE_SIZE, 512);
8645 uint64_t cTableSectorDiff = VMDK_BYTE2SECTOR(cbNewAllTablesSize - cbCurrAllTablesSize);
8646
8647 uint64_t cbNewOverhead = vmdkGetNewOverhead(pExtent, cSectorsNew);
8648 uint64_t cNewOverheadSectors = VMDK_BYTE2SECTOR(cbNewOverhead);
8649 uint64_t cOverheadSectorDiff = cNewOverheadSectors - pExtent->cOverheadSectors;
8650
8651 /*
8652 * Get the blocks we need to relocate first, they are appended to the end
8653 * of the image.
8654 */
8655 void *pvBuf = NULL, *pvZero = NULL;
8656
8657 do
8658 {
8659 /* Allocate data buffer. */
8660 pvBuf = RTMemAllocZ(VMDK_GRAIN_TABLE_SIZE);
8661 if (!pvBuf)
8662 {
8663 rc = VERR_NO_MEMORY;
8664 break;
8665 }
8666
8667 /* Allocate buffer for overwriting with zeroes. */
8668 pvZero = RTMemAllocZ(VMDK_GRAIN_TABLE_SIZE);
8669 if (!pvZero)
8670 {
8671 RTMemFree(pvBuf);
8672 pvBuf = NULL;
8673
8674 rc = VERR_NO_MEMORY;
8675 break;
8676 }
8677
8678 uint32_t uGTStart = VMDK_SECTOR2BYTE(pExtent->uSectorGD) + (cOldGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8679
8680 // points to last element in the grain table
8681 uint32_t uGTTail = uGTStart + (pExtent->cGDEntries * VMDK_GRAIN_TABLE_SIZE) - VMDK_GRAIN_TABLE_SIZE;
8682 uint32_t cbGTOff = RT_ALIGN_Z(VMDK_SECTOR2BYTE(cDirSectorDiff + cTableSectorDiff + cDirSectorDiff), 512);
8683
8684 for (int i = pExtent->cGDEntries - 1; i >= 0; i--)
8685 {
8686 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8687 uGTTail, pvBuf,
8688 VMDK_GRAIN_TABLE_SIZE);
8689 if (RT_FAILURE(rc))
8690 break;
8691
8692 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8693 RT_ALIGN_Z(uGTTail + cbGTOff, 512), pvBuf,
8694 VMDK_GRAIN_TABLE_SIZE);
8695 if (RT_FAILURE(rc))
8696 break;
8697
8698 // This overshoots when i == 0, but we don't need it anymore.
8699 uGTTail -= VMDK_GRAIN_TABLE_SIZE;
8700 }
8701
8702
8703 /* Find the end of the grain directory and start bumping everything down. Update locations of GT entries. */
8704 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8705 VMDK_SECTOR2BYTE(pExtent->uSectorGD), pvBuf,
8706 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8707 if (RT_FAILURE(rc))
8708 break;
8709
8710 int * tmpBuf = (int *)pvBuf;
8711
8712 for (uint32_t i = 0; i < pExtent->cGDEntries; i++)
8713 {
8714 tmpBuf[i] = tmpBuf[i] + VMDK_BYTE2SECTOR(cbGTOff);
8715 pExtent->pGD[i] = pExtent->pGD[i] + VMDK_BYTE2SECTOR(cbGTOff);
8716 }
8717
8718 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8719 RT_ALIGN_Z(VMDK_SECTOR2BYTE(pExtent->uSectorGD + cTableSectorDiff + cDirSectorDiff), 512), pvBuf,
8720 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8721 if (RT_FAILURE(rc))
8722 break;
8723
8724 pExtent->uSectorGD = pExtent->uSectorGD + cDirSectorDiff + cTableSectorDiff;
8725
8726 /* Repeat both steps with the redundant grain table/directory. */
8727
8728 uint32_t uRGTStart = VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + (cOldGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8729
8730 // points to last element in the grain table
8731 uint32_t uRGTTail = uRGTStart + (pExtent->cGDEntries * VMDK_GRAIN_TABLE_SIZE) - VMDK_GRAIN_TABLE_SIZE;
8732 uint32_t cbRGTOff = RT_ALIGN_Z(VMDK_SECTOR2BYTE(cDirSectorDiff), 512);
8733
8734 for (int i = pExtent->cGDEntries - 1; i >= 0; i--)
8735 {
8736 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8737 uRGTTail, pvBuf,
8738 VMDK_GRAIN_TABLE_SIZE);
8739 if (RT_FAILURE(rc))
8740 break;
8741
8742 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8743 RT_ALIGN_Z(uRGTTail + cbRGTOff, 512), pvBuf,
8744 VMDK_GRAIN_TABLE_SIZE);
8745 if (RT_FAILURE(rc))
8746 break;
8747
8748 // This overshoots when i == 0, but we don't need it anymore.
8749 uRGTTail -= VMDK_GRAIN_TABLE_SIZE;
8750 }
8751
8752 /* Update locations of GT entries. */
8753 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
8754 VMDK_SECTOR2BYTE(pExtent->uSectorRGD), pvBuf,
8755 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8756 if (RT_FAILURE(rc))
8757 break;
8758
8759 tmpBuf = (int *)pvBuf;
8760
8761 for (uint32_t i = 0; i < pExtent->cGDEntries; i++)
8762 {
8763 tmpBuf[i] = tmpBuf[i] + cDirSectorDiff;
8764 pExtent->pRGD[i] = pExtent->pRGD[i] + cDirSectorDiff;
8765 }
8766
8767 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8768 VMDK_SECTOR2BYTE(pExtent->uSectorRGD), pvBuf,
8769 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8770 if (RT_FAILURE(rc))
8771 break;
8772
8773 pExtent->uSectorRGD = pExtent->uSectorRGD;
8774 pExtent->cOverheadSectors += cOverheadSectorDiff;
8775
8776 } while (0);
8777
8778 if (pvBuf)
8779 {
8780 RTMemFree(pvBuf);
8781 pvBuf = NULL;
8782 }
8783
8784 if (pvZero)
8785 {
8786 RTMemFree(pvZero);
8787 pvZero = NULL;
8788 }
8789
8790 pExtent->cGDEntries = cNewDirEntries;
8791
8792 /* Allocate buffer for overwriting with zeroes. */
8793 pvZero = RTMemAllocZ(VMDK_GRAIN_TABLE_SIZE);
8794 if (!pvZero)
8795 return VERR_NO_MEMORY;
8796
8797 // Allocate additional grain dir
8798 pExtent->pGD = (uint32_t *) RTMemReallocZ(pExtent->pGD, pExtent->cGDEntries * sizeof(uint32_t), cbNewGD);
8799 if (RT_LIKELY(pExtent->pGD))
8800 {
8801 if (pExtent->uSectorRGD)
8802 {
8803 pExtent->pRGD = (uint32_t *)RTMemReallocZ(pExtent->pRGD, pExtent->cGDEntries * sizeof(uint32_t), cbNewGD);
8804 if (RT_UNLIKELY(!pExtent->pRGD))
8805 rc = VERR_NO_MEMORY;
8806 }
8807 }
8808 else
8809 return VERR_NO_MEMORY;
8810
8811
8812 uint32_t uTmpDirVal = pExtent->pGD[cOldGDEntries - 1] + VMDK_GRAIN_DIR_ENTRY_SIZE;
8813 for (uint32_t i = cOldGDEntries; i < pExtent->cGDEntries; i++)
8814 {
8815 pExtent->pGD[i] = uTmpDirVal;
8816
8817 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8818 VMDK_SECTOR2BYTE(uTmpDirVal), pvZero,
8819 VMDK_GRAIN_TABLE_SIZE);
8820
8821 if (RT_FAILURE(rc))
8822 return rc;
8823
8824 uTmpDirVal += VMDK_GRAIN_DIR_ENTRY_SIZE;
8825 }
8826
8827 uint32_t uRTmpDirVal = pExtent->pRGD[cOldGDEntries - 1] + VMDK_GRAIN_DIR_ENTRY_SIZE;
8828 for (uint32_t i = cOldGDEntries; i < pExtent->cGDEntries; i++)
8829 {
8830 pExtent->pRGD[i] = uRTmpDirVal;
8831
8832 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8833 VMDK_SECTOR2BYTE(uRTmpDirVal), pvZero,
8834 VMDK_GRAIN_TABLE_SIZE);
8835
8836 if (RT_FAILURE(rc))
8837 return rc;
8838
8839 uRTmpDirVal += VMDK_GRAIN_DIR_ENTRY_SIZE;
8840 }
8841
8842 RTMemFree(pvZero);
8843 pvZero = NULL;
8844
8845 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8846 VMDK_SECTOR2BYTE(pExtent->uSectorGD), pExtent->pGD,
8847 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8848 if (RT_FAILURE(rc))
8849 return rc;
8850
8851 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
8852 VMDK_SECTOR2BYTE(pExtent->uSectorRGD), pExtent->pRGD,
8853 pExtent->cGDEntries * VMDK_GRAIN_DIR_ENTRY_SIZE);
8854 if (RT_FAILURE(rc))
8855 return rc;
8856
8857 rc = vmdkReplaceExtentSize(pImage, pExtent, pImage->Descriptor.uFirstExtent + pExtent->uExtent,
8858 pExtent->cNominalSectors, cSectorsNew);
8859 if (RT_FAILURE(rc))
8860 return rc;
8861
8862 return rc;
8863}
8864
8865/** @copydoc VDIMAGEBACKEND::pfnResize */
8866static DECLCALLBACK(int) vmdkResize(void *pBackendData, uint64_t cbSize,
8867 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
8868 unsigned uPercentStart, unsigned uPercentSpan,
8869 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
8870 PVDINTERFACE pVDIfsOperation)
8871{
8872 RT_NOREF5(uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation);
8873
8874 // Establish variables and objects needed
8875 int rc = VINF_SUCCESS;
8876 PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
8877 unsigned uImageFlags = pImage->uImageFlags;
8878 PVMDKEXTENT pExtent = &pImage->pExtents[0];
8879 pExtent->fMetaDirty = true;
8880
8881 uint64_t cSectorsNew = cbSize / VMDK_SECTOR_SIZE; /** < New number of sectors in the image after the resize */
8882 if (cbSize % VMDK_SECTOR_SIZE)
8883 cSectorsNew++;
8884
8885 uint64_t cSectorsOld = pImage->cbSize / VMDK_SECTOR_SIZE; /** < Number of sectors before the resize. Only for FLAT images. */
8886 if (pImage->cbSize % VMDK_SECTOR_SIZE)
8887 cSectorsOld++;
8888 unsigned cExtents = pImage->cExtents;
8889
8890 /* Check size is within min/max bounds. */
8891 if ( !(uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK)
8892 && ( !cbSize
8893 || (!(uImageFlags & VD_IMAGE_FLAGS_FIXED) && cbSize >= _1T * 256 - _64K)) )
8894 return VERR_VD_INVALID_SIZE;
8895
8896 /*
8897 * Making the image smaller is not supported at the moment.
8898 */
8899 /** @todo implement making the image smaller, it is the responsibility of
8900 * the user to know what they're doing. */
8901 if (cbSize < pImage->cbSize)
8902 rc = VERR_VD_SHRINK_NOT_SUPPORTED;
8903 else if (cbSize > pImage->cbSize)
8904 {
8905 /**
8906 * monolithicFlat. FIXED flag and not split up into 2 GB parts.
8907 */
8908 if ((uImageFlags & VD_IMAGE_FLAGS_FIXED) && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G))
8909 {
8910 /** Required space in bytes for the extent after the resize. */
8911 uint64_t cbSectorSpaceNew = cSectorsNew * VMDK_SECTOR_SIZE;
8912 pExtent = &pImage->pExtents[0];
8913
8914 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage, cbSectorSpaceNew,
8915 0 /* fFlags */, NULL,
8916 uPercentStart, uPercentSpan);
8917 if (RT_FAILURE(rc))
8918 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
8919
8920 rc = vmdkReplaceExtentSize(pImage, pExtent, pImage->Descriptor.uFirstExtent, cSectorsOld, cSectorsNew);
8921 if (RT_FAILURE(rc))
8922 return rc;
8923 }
8924
8925 /**
8926 * twoGbMaxExtentFlat. FIXED flag and SPLIT into 2 GB parts.
8927 */
8928 if ((uImageFlags & VD_IMAGE_FLAGS_FIXED) && (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G))
8929 {
8930 /* Check to see how much space remains in last extent */
8931 bool fSpaceAvailible = false;
8932 uint64_t cLastExtentRemSectors = cSectorsOld % VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
8933 if (cLastExtentRemSectors)
8934 fSpaceAvailible = true;
8935
8936 uint64_t cSectorsNeeded = cSectorsNew - cSectorsOld;
8937
8938 /** Space remaining in current last extent file that we don't need to create another one. */
8939 if (fSpaceAvailible && cSectorsNeeded + cLastExtentRemSectors <= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE))
8940 {
8941 pExtent = &pImage->pExtents[cExtents - 1];
8942 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage,
8943 VMDK_SECTOR2BYTE(cSectorsNeeded + cLastExtentRemSectors),
8944 0 /* fFlags */, NULL, uPercentStart, uPercentSpan);
8945 if (RT_FAILURE(rc))
8946 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
8947
8948 rc = vmdkReplaceExtentSize(pImage, pExtent, pImage->Descriptor.uFirstExtent + cExtents - 1,
8949 pExtent->cNominalSectors, cSectorsNeeded + cLastExtentRemSectors);
8950 if (RT_FAILURE(rc))
8951 return rc;
8952 }
8953 //** Need more extent files to handle all the requested space. */
8954 else
8955 {
8956 if (fSpaceAvailible)
8957 {
8958 pExtent = &pImage->pExtents[cExtents - 1];
8959 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage, VMDK_2G_SPLIT_SIZE,
8960 0 /* fFlags */, NULL,
8961 uPercentStart, uPercentSpan);
8962 if (RT_FAILURE(rc))
8963 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
8964
8965 cSectorsNeeded = cSectorsNeeded - VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE) + cLastExtentRemSectors;
8966
8967 rc = vmdkReplaceExtentSize(pImage, pExtent, pImage->Descriptor.uFirstExtent + cExtents - 1,
8968 pExtent->cNominalSectors, VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE));
8969 if (RT_FAILURE(rc))
8970 return rc;
8971 }
8972
8973 unsigned cNewExtents = VMDK_SECTOR2BYTE(cSectorsNeeded) / VMDK_2G_SPLIT_SIZE;
8974 if (cNewExtents % VMDK_2G_SPLIT_SIZE || cNewExtents < VMDK_2G_SPLIT_SIZE)
8975 cNewExtents++;
8976
8977 for (unsigned i = cExtents;
8978 i < cExtents + cNewExtents && cSectorsNeeded >= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
8979 i++)
8980 {
8981 rc = vmdkAddFileBackedExtent(pImage, VMDK_2G_SPLIT_SIZE);
8982 if (RT_FAILURE(rc))
8983 return rc;
8984
8985 pExtent = &pImage->pExtents[i];
8986
8987 pExtent->cSectors = VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
8988 cSectorsNeeded -= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
8989 }
8990
8991 if (cSectorsNeeded)
8992 {
8993 rc = vmdkAddFileBackedExtent(pImage, VMDK_SECTOR2BYTE(cSectorsNeeded));
8994 if (RT_FAILURE(rc))
8995 return rc;
8996 }
8997 }
8998 }
8999
9000 /**
9001 * monolithicSparse.
9002 */
9003 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G))
9004 {
9005 // 1. Calculate sectors needed for new overhead.
9006
9007 uint64_t cbNewOverhead = vmdkGetNewOverhead(pExtent, cSectorsNew);
9008 uint64_t cNewOverheadSectors = VMDK_BYTE2SECTOR(cbNewOverhead);
9009 uint64_t cOverheadSectorDiff = cNewOverheadSectors - pExtent->cOverheadSectors;
9010
9011 // 2. Relocate sectors to make room for new GD/GT, update entries in GD/GT
9012 if (cOverheadSectorDiff > 0)
9013 {
9014 if (pExtent->cSectors > 0)
9015 {
9016 /* Do the relocation. */
9017 LogFlow(("Relocating VMDK sectors\n"));
9018 rc = vmdkRelocateSectorsForSparseResize(pImage, pExtent, cSectorsNew);
9019 if (RT_FAILURE(rc))
9020 return rc;
9021
9022 rc = vmdkFlushImage(pImage, NULL);
9023 if (RT_FAILURE(rc))
9024 return rc;
9025 }
9026
9027 rc = vmdkResizeSparseMeta(pImage, pExtent, cSectorsNew);
9028 if (RT_FAILURE(rc))
9029 return rc;
9030 }
9031 }
9032
9033 /**
9034 * twoGbSparseExtent
9035 */
9036 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE && (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G))
9037 {
9038 /* Check to see how much space remains in last extent */
9039 bool fSpaceAvailible = false;
9040 uint64_t cLastExtentRemSectors = cSectorsOld % VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
9041 if (cLastExtentRemSectors)
9042 fSpaceAvailible = true;
9043
9044 uint64_t cSectorsNeeded = cSectorsNew - cSectorsOld;
9045
9046 if (fSpaceAvailible && cSectorsNeeded + cLastExtentRemSectors <= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE))
9047 {
9048 pExtent = &pImage->pExtents[cExtents - 1];
9049 rc = vmdkRelocateSectorsForSparseResize(pImage, pExtent, cSectorsNeeded + cLastExtentRemSectors);
9050 if (RT_FAILURE(rc))
9051 return rc;
9052
9053 rc = vmdkFlushImage(pImage, NULL);
9054 if (RT_FAILURE(rc))
9055 return rc;
9056
9057 rc = vmdkResizeSparseMeta(pImage, pExtent, cSectorsNeeded + cLastExtentRemSectors);
9058 if (RT_FAILURE(rc))
9059 return rc;
9060 }
9061 else
9062 {
9063 if (fSpaceAvailible)
9064 {
9065 pExtent = &pImage->pExtents[cExtents - 1];
9066 rc = vmdkRelocateSectorsForSparseResize(pImage, pExtent, VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE));
9067 if (RT_FAILURE(rc))
9068 return rc;
9069
9070 rc = vmdkFlushImage(pImage, NULL);
9071 if (RT_FAILURE(rc))
9072 return rc;
9073
9074 rc = vmdkResizeSparseMeta(pImage, pExtent, VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE));
9075 if (RT_FAILURE(rc))
9076 return rc;
9077
9078 cSectorsNeeded = cSectorsNeeded - VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE) + cLastExtentRemSectors;
9079 }
9080
9081 unsigned cNewExtents = VMDK_SECTOR2BYTE(cSectorsNeeded) / VMDK_2G_SPLIT_SIZE;
9082 if (cNewExtents % VMDK_2G_SPLIT_SIZE || cNewExtents < VMDK_2G_SPLIT_SIZE)
9083 cNewExtents++;
9084
9085 for (unsigned i = cExtents;
9086 i < cExtents + cNewExtents && cSectorsNeeded >= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
9087 i++)
9088 {
9089 rc = vmdkAddFileBackedExtent(pImage, VMDK_2G_SPLIT_SIZE);
9090 if (RT_FAILURE(rc))
9091 return rc;
9092
9093 pExtent = &pImage->pExtents[i];
9094
9095 rc = vmdkFlushImage(pImage, NULL);
9096 if (RT_FAILURE(rc))
9097 return rc;
9098
9099 pExtent->cSectors = VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
9100 cSectorsNeeded -= VMDK_BYTE2SECTOR(VMDK_2G_SPLIT_SIZE);
9101 }
9102
9103 if (cSectorsNeeded)
9104 {
9105 rc = vmdkAddFileBackedExtent(pImage, VMDK_SECTOR2BYTE(cSectorsNeeded));
9106 if (RT_FAILURE(rc))
9107 return rc;
9108
9109 pExtent = &pImage->pExtents[pImage->cExtents];
9110
9111 rc = vmdkFlushImage(pImage, NULL);
9112 if (RT_FAILURE(rc))
9113 return rc;
9114 }
9115 }
9116 }
9117
9118 /* Successful resize. Update metadata */
9119 if (RT_SUCCESS(rc))
9120 {
9121 /* Update size and new block count. */
9122 pImage->cbSize = cbSize;
9123 pExtent->cNominalSectors = cSectorsNew;
9124 pExtent->cSectors = cSectorsNew;
9125
9126 /* Update geometry. */
9127 pImage->PCHSGeometry = *pPCHSGeometry;
9128 pImage->LCHSGeometry = *pLCHSGeometry;
9129 }
9130
9131 /* Update header information in base image file. */
9132 pImage->Descriptor.fDirty = true;
9133 rc = vmdkWriteDescriptor(pImage, NULL);
9134
9135 if (RT_SUCCESS(rc))
9136 rc = vmdkFlushImage(pImage, NULL);
9137 }
9138 /* Same size doesn't change the image at all. */
9139
9140 LogFlowFunc(("returns %Rrc\n", rc));
9141 return rc;
9142}
9143
9144const VDIMAGEBACKEND g_VmdkBackend =
9145{
9146 /* u32Version */
9147 VD_IMGBACKEND_VERSION,
9148 /* pszBackendName */
9149 "VMDK",
9150 /* uBackendCaps */
9151 VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
9152 | VD_CAP_CREATE_SPLIT_2G | VD_CAP_DIFF | VD_CAP_FILE | VD_CAP_ASYNC
9153 | VD_CAP_VFS | VD_CAP_PREFERRED,
9154 /* paFileExtensions */
9155 s_aVmdkFileExtensions,
9156 /* paConfigInfo */
9157 s_aVmdkConfigInfo,
9158 /* pfnProbe */
9159 vmdkProbe,
9160 /* pfnOpen */
9161 vmdkOpen,
9162 /* pfnCreate */
9163 vmdkCreate,
9164 /* pfnRename */
9165 vmdkRename,
9166 /* pfnClose */
9167 vmdkClose,
9168 /* pfnRead */
9169 vmdkRead,
9170 /* pfnWrite */
9171 vmdkWrite,
9172 /* pfnFlush */
9173 vmdkFlush,
9174 /* pfnDiscard */
9175 NULL,
9176 /* pfnGetVersion */
9177 vmdkGetVersion,
9178 /* pfnGetFileSize */
9179 vmdkGetFileSize,
9180 /* pfnGetPCHSGeometry */
9181 vmdkGetPCHSGeometry,
9182 /* pfnSetPCHSGeometry */
9183 vmdkSetPCHSGeometry,
9184 /* pfnGetLCHSGeometry */
9185 vmdkGetLCHSGeometry,
9186 /* pfnSetLCHSGeometry */
9187 vmdkSetLCHSGeometry,
9188 /* pfnQueryRegions */
9189 vmdkQueryRegions,
9190 /* pfnRegionListRelease */
9191 vmdkRegionListRelease,
9192 /* pfnGetImageFlags */
9193 vmdkGetImageFlags,
9194 /* pfnGetOpenFlags */
9195 vmdkGetOpenFlags,
9196 /* pfnSetOpenFlags */
9197 vmdkSetOpenFlags,
9198 /* pfnGetComment */
9199 vmdkGetComment,
9200 /* pfnSetComment */
9201 vmdkSetComment,
9202 /* pfnGetUuid */
9203 vmdkGetUuid,
9204 /* pfnSetUuid */
9205 vmdkSetUuid,
9206 /* pfnGetModificationUuid */
9207 vmdkGetModificationUuid,
9208 /* pfnSetModificationUuid */
9209 vmdkSetModificationUuid,
9210 /* pfnGetParentUuid */
9211 vmdkGetParentUuid,
9212 /* pfnSetParentUuid */
9213 vmdkSetParentUuid,
9214 /* pfnGetParentModificationUuid */
9215 vmdkGetParentModificationUuid,
9216 /* pfnSetParentModificationUuid */
9217 vmdkSetParentModificationUuid,
9218 /* pfnDump */
9219 vmdkDump,
9220 /* pfnGetTimestamp */
9221 NULL,
9222 /* pfnGetParentTimestamp */
9223 NULL,
9224 /* pfnSetParentTimestamp */
9225 NULL,
9226 /* pfnGetParentFilename */
9227 NULL,
9228 /* pfnSetParentFilename */
9229 NULL,
9230 /* pfnComposeLocation */
9231 genericFileComposeLocation,
9232 /* pfnComposeName */
9233 genericFileComposeName,
9234 /* pfnCompact */
9235 NULL,
9236 /* pfnResize */
9237 vmdkResize,
9238 /* pfnRepair */
9239 NULL,
9240 /* pfnTraverseMetadata */
9241 NULL,
9242 /* u32VersionEnd */
9243 VD_IMGBACKEND_VERSION
9244};
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette