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