1 | /* $Id: VMDK.cpp 41767 2012-06-15 18:40:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMDK disk image, core code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_VD_VMDK
|
---|
22 | #include <VBox/vd-plugin.h>
|
---|
23 | #include <VBox/err.h>
|
---|
24 |
|
---|
25 | #include <VBox/log.h>
|
---|
26 | #include <iprt/assert.h>
|
---|
27 | #include <iprt/alloc.h>
|
---|
28 | #include <iprt/uuid.h>
|
---|
29 | #include <iprt/path.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include <iprt/rand.h>
|
---|
32 | #include <iprt/zip.h>
|
---|
33 | #include <iprt/asm.h>
|
---|
34 |
|
---|
35 | /*******************************************************************************
|
---|
36 | * Constants And Macros, Structures and Typedefs *
|
---|
37 | *******************************************************************************/
|
---|
38 |
|
---|
39 | /** Maximum encoded string size (including NUL) we allow for VMDK images.
|
---|
40 | * Deliberately not set high to avoid running out of descriptor space. */
|
---|
41 | #define VMDK_ENCODED_COMMENT_MAX 1024
|
---|
42 |
|
---|
43 | /** VMDK descriptor DDB entry for PCHS cylinders. */
|
---|
44 | #define VMDK_DDB_GEO_PCHS_CYLINDERS "ddb.geometry.cylinders"
|
---|
45 |
|
---|
46 | /** VMDK descriptor DDB entry for PCHS heads. */
|
---|
47 | #define VMDK_DDB_GEO_PCHS_HEADS "ddb.geometry.heads"
|
---|
48 |
|
---|
49 | /** VMDK descriptor DDB entry for PCHS sectors. */
|
---|
50 | #define VMDK_DDB_GEO_PCHS_SECTORS "ddb.geometry.sectors"
|
---|
51 |
|
---|
52 | /** VMDK descriptor DDB entry for LCHS cylinders. */
|
---|
53 | #define VMDK_DDB_GEO_LCHS_CYLINDERS "ddb.geometry.biosCylinders"
|
---|
54 |
|
---|
55 | /** VMDK descriptor DDB entry for LCHS heads. */
|
---|
56 | #define VMDK_DDB_GEO_LCHS_HEADS "ddb.geometry.biosHeads"
|
---|
57 |
|
---|
58 | /** VMDK descriptor DDB entry for LCHS sectors. */
|
---|
59 | #define VMDK_DDB_GEO_LCHS_SECTORS "ddb.geometry.biosSectors"
|
---|
60 |
|
---|
61 | /** VMDK descriptor DDB entry for image UUID. */
|
---|
62 | #define VMDK_DDB_IMAGE_UUID "ddb.uuid.image"
|
---|
63 |
|
---|
64 | /** VMDK descriptor DDB entry for image modification UUID. */
|
---|
65 | #define VMDK_DDB_MODIFICATION_UUID "ddb.uuid.modification"
|
---|
66 |
|
---|
67 | /** VMDK descriptor DDB entry for parent image UUID. */
|
---|
68 | #define VMDK_DDB_PARENT_UUID "ddb.uuid.parent"
|
---|
69 |
|
---|
70 | /** VMDK descriptor DDB entry for parent image modification UUID. */
|
---|
71 | #define VMDK_DDB_PARENT_MODIFICATION_UUID "ddb.uuid.parentmodification"
|
---|
72 |
|
---|
73 | /** No compression for streamOptimized files. */
|
---|
74 | #define VMDK_COMPRESSION_NONE 0
|
---|
75 |
|
---|
76 | /** Deflate compression for streamOptimized files. */
|
---|
77 | #define VMDK_COMPRESSION_DEFLATE 1
|
---|
78 |
|
---|
79 | /** Marker that the actual GD value is stored in the footer. */
|
---|
80 | #define VMDK_GD_AT_END 0xffffffffffffffffULL
|
---|
81 |
|
---|
82 | /** Marker for end-of-stream in streamOptimized images. */
|
---|
83 | #define VMDK_MARKER_EOS 0
|
---|
84 |
|
---|
85 | /** Marker for grain table block in streamOptimized images. */
|
---|
86 | #define VMDK_MARKER_GT 1
|
---|
87 |
|
---|
88 | /** Marker for grain directory block in streamOptimized images. */
|
---|
89 | #define VMDK_MARKER_GD 2
|
---|
90 |
|
---|
91 | /** Marker for footer in streamOptimized images. */
|
---|
92 | #define VMDK_MARKER_FOOTER 3
|
---|
93 |
|
---|
94 | /** Marker for unknown purpose in streamOptimized images.
|
---|
95 | * Shows up in very recent images created by vSphere, but only sporadically.
|
---|
96 | * They "forgot" to document that one in the VMDK specification. */
|
---|
97 | #define VMDK_MARKER_UNSPECIFIED 4
|
---|
98 |
|
---|
99 | /** Dummy marker for "don't check the marker value". */
|
---|
100 | #define VMDK_MARKER_IGNORE 0xffffffffU
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Magic number for hosted images created by VMware Workstation 4, VMware
|
---|
104 | * Workstation 5, VMware Server or VMware Player. Not necessarily sparse.
|
---|
105 | */
|
---|
106 | #define VMDK_SPARSE_MAGICNUMBER 0x564d444b /* 'V' 'M' 'D' 'K' */
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * VMDK hosted binary extent header. The "Sparse" is a total misnomer, as
|
---|
110 | * this header is also used for monolithic flat images.
|
---|
111 | */
|
---|
112 | #pragma pack(1)
|
---|
113 | typedef struct SparseExtentHeader
|
---|
114 | {
|
---|
115 | uint32_t magicNumber;
|
---|
116 | uint32_t version;
|
---|
117 | uint32_t flags;
|
---|
118 | uint64_t capacity;
|
---|
119 | uint64_t grainSize;
|
---|
120 | uint64_t descriptorOffset;
|
---|
121 | uint64_t descriptorSize;
|
---|
122 | uint32_t numGTEsPerGT;
|
---|
123 | uint64_t rgdOffset;
|
---|
124 | uint64_t gdOffset;
|
---|
125 | uint64_t overHead;
|
---|
126 | bool uncleanShutdown;
|
---|
127 | char singleEndLineChar;
|
---|
128 | char nonEndLineChar;
|
---|
129 | char doubleEndLineChar1;
|
---|
130 | char doubleEndLineChar2;
|
---|
131 | uint16_t compressAlgorithm;
|
---|
132 | uint8_t pad[433];
|
---|
133 | } SparseExtentHeader;
|
---|
134 | #pragma pack()
|
---|
135 |
|
---|
136 | /** VMDK capacity for a single chunk when 2G splitting is turned on. Should be
|
---|
137 | * divisible by the default grain size (64K) */
|
---|
138 | #define VMDK_2G_SPLIT_SIZE (2047 * 1024 * 1024)
|
---|
139 |
|
---|
140 | /** VMDK streamOptimized file format marker. The type field may or may not
|
---|
141 | * be actually valid, but there's always data to read there. */
|
---|
142 | #pragma pack(1)
|
---|
143 | typedef struct VMDKMARKER
|
---|
144 | {
|
---|
145 | uint64_t uSector;
|
---|
146 | uint32_t cbSize;
|
---|
147 | uint32_t uType;
|
---|
148 | } VMDKMARKER, *PVMDKMARKER;
|
---|
149 | #pragma pack()
|
---|
150 |
|
---|
151 |
|
---|
152 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
153 |
|
---|
154 | /** @todo the ESX code is not tested, not used, and lacks error messages. */
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Magic number for images created by VMware GSX Server 3 or ESX Server 3.
|
---|
158 | */
|
---|
159 | #define VMDK_ESX_SPARSE_MAGICNUMBER 0x44574f43 /* 'C' 'O' 'W' 'D' */
|
---|
160 |
|
---|
161 | #pragma pack(1)
|
---|
162 | typedef struct COWDisk_Header
|
---|
163 | {
|
---|
164 | uint32_t magicNumber;
|
---|
165 | uint32_t version;
|
---|
166 | uint32_t flags;
|
---|
167 | uint32_t numSectors;
|
---|
168 | uint32_t grainSize;
|
---|
169 | uint32_t gdOffset;
|
---|
170 | uint32_t numGDEntries;
|
---|
171 | uint32_t freeSector;
|
---|
172 | /* The spec incompletely documents quite a few further fields, but states
|
---|
173 | * that they are unused by the current format. Replace them by padding. */
|
---|
174 | char reserved1[1604];
|
---|
175 | uint32_t savedGeneration;
|
---|
176 | char reserved2[8];
|
---|
177 | uint32_t uncleanShutdown;
|
---|
178 | char padding[396];
|
---|
179 | } COWDisk_Header;
|
---|
180 | #pragma pack()
|
---|
181 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
182 |
|
---|
183 |
|
---|
184 | /** Convert sector number/size to byte offset/size. */
|
---|
185 | #define VMDK_SECTOR2BYTE(u) ((uint64_t)(u) << 9)
|
---|
186 |
|
---|
187 | /** Convert byte offset/size to sector number/size. */
|
---|
188 | #define VMDK_BYTE2SECTOR(u) ((u) >> 9)
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * VMDK extent type.
|
---|
192 | */
|
---|
193 | typedef enum VMDKETYPE
|
---|
194 | {
|
---|
195 | /** Hosted sparse extent. */
|
---|
196 | VMDKETYPE_HOSTED_SPARSE = 1,
|
---|
197 | /** Flat extent. */
|
---|
198 | VMDKETYPE_FLAT,
|
---|
199 | /** Zero extent. */
|
---|
200 | VMDKETYPE_ZERO,
|
---|
201 | /** VMFS extent, used by ESX. */
|
---|
202 | VMDKETYPE_VMFS
|
---|
203 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
204 | ,
|
---|
205 | /** ESX sparse extent. */
|
---|
206 | VMDKETYPE_ESX_SPARSE
|
---|
207 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
208 | } VMDKETYPE, *PVMDKETYPE;
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * VMDK access type for a extent.
|
---|
212 | */
|
---|
213 | typedef enum VMDKACCESS
|
---|
214 | {
|
---|
215 | /** No access allowed. */
|
---|
216 | VMDKACCESS_NOACCESS = 0,
|
---|
217 | /** Read-only access. */
|
---|
218 | VMDKACCESS_READONLY,
|
---|
219 | /** Read-write access. */
|
---|
220 | VMDKACCESS_READWRITE
|
---|
221 | } VMDKACCESS, *PVMDKACCESS;
|
---|
222 |
|
---|
223 | /** Forward declaration for PVMDKIMAGE. */
|
---|
224 | typedef struct VMDKIMAGE *PVMDKIMAGE;
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Extents files entry. Used for opening a particular file only once.
|
---|
228 | */
|
---|
229 | typedef struct VMDKFILE
|
---|
230 | {
|
---|
231 | /** Pointer to filename. Local copy. */
|
---|
232 | const char *pszFilename;
|
---|
233 | /** File open flags for consistency checking. */
|
---|
234 | unsigned fOpen;
|
---|
235 | /** Flag whether this file has been opened for async I/O. */
|
---|
236 | bool fAsyncIO;
|
---|
237 | /** Handle for sync/async file abstraction.*/
|
---|
238 | PVDIOSTORAGE pStorage;
|
---|
239 | /** Reference counter. */
|
---|
240 | unsigned uReferences;
|
---|
241 | /** Flag whether the file should be deleted on last close. */
|
---|
242 | bool fDelete;
|
---|
243 | /** Pointer to the image we belong to (for debugging purposes). */
|
---|
244 | PVMDKIMAGE pImage;
|
---|
245 | /** Pointer to next file descriptor. */
|
---|
246 | struct VMDKFILE *pNext;
|
---|
247 | /** Pointer to the previous file descriptor. */
|
---|
248 | struct VMDKFILE *pPrev;
|
---|
249 | } VMDKFILE, *PVMDKFILE;
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * VMDK extent data structure.
|
---|
253 | */
|
---|
254 | typedef struct VMDKEXTENT
|
---|
255 | {
|
---|
256 | /** File handle. */
|
---|
257 | PVMDKFILE pFile;
|
---|
258 | /** Base name of the image extent. */
|
---|
259 | const char *pszBasename;
|
---|
260 | /** Full name of the image extent. */
|
---|
261 | const char *pszFullname;
|
---|
262 | /** Number of sectors in this extent. */
|
---|
263 | uint64_t cSectors;
|
---|
264 | /** Number of sectors per block (grain in VMDK speak). */
|
---|
265 | uint64_t cSectorsPerGrain;
|
---|
266 | /** Starting sector number of descriptor. */
|
---|
267 | uint64_t uDescriptorSector;
|
---|
268 | /** Size of descriptor in sectors. */
|
---|
269 | uint64_t cDescriptorSectors;
|
---|
270 | /** Starting sector number of grain directory. */
|
---|
271 | uint64_t uSectorGD;
|
---|
272 | /** Starting sector number of redundant grain directory. */
|
---|
273 | uint64_t uSectorRGD;
|
---|
274 | /** Total number of metadata sectors. */
|
---|
275 | uint64_t cOverheadSectors;
|
---|
276 | /** Nominal size (i.e. as described by the descriptor) of this extent. */
|
---|
277 | uint64_t cNominalSectors;
|
---|
278 | /** Sector offset (i.e. as described by the descriptor) of this extent. */
|
---|
279 | uint64_t uSectorOffset;
|
---|
280 | /** Number of entries in a grain table. */
|
---|
281 | uint32_t cGTEntries;
|
---|
282 | /** Number of sectors reachable via a grain directory entry. */
|
---|
283 | uint32_t cSectorsPerGDE;
|
---|
284 | /** Number of entries in the grain directory. */
|
---|
285 | uint32_t cGDEntries;
|
---|
286 | /** Pointer to the next free sector. Legacy information. Do not use. */
|
---|
287 | uint32_t uFreeSector;
|
---|
288 | /** Number of this extent in the list of images. */
|
---|
289 | uint32_t uExtent;
|
---|
290 | /** Pointer to the descriptor (NULL if no descriptor in this extent). */
|
---|
291 | char *pDescData;
|
---|
292 | /** Pointer to the grain directory. */
|
---|
293 | uint32_t *pGD;
|
---|
294 | /** Pointer to the redundant grain directory. */
|
---|
295 | uint32_t *pRGD;
|
---|
296 | /** VMDK version of this extent. 1=1.0/1.1 */
|
---|
297 | uint32_t uVersion;
|
---|
298 | /** Type of this extent. */
|
---|
299 | VMDKETYPE enmType;
|
---|
300 | /** Access to this extent. */
|
---|
301 | VMDKACCESS enmAccess;
|
---|
302 | /** Flag whether this extent is marked as unclean. */
|
---|
303 | bool fUncleanShutdown;
|
---|
304 | /** Flag whether the metadata in the extent header needs to be updated. */
|
---|
305 | bool fMetaDirty;
|
---|
306 | /** Flag whether there is a footer in this extent. */
|
---|
307 | bool fFooter;
|
---|
308 | /** Compression type for this extent. */
|
---|
309 | uint16_t uCompression;
|
---|
310 | /** Append position for writing new grain. Only for sparse extents. */
|
---|
311 | uint64_t uAppendPosition;
|
---|
312 | /** Last grain which was accessed. Only for streamOptimized extents. */
|
---|
313 | uint32_t uLastGrainAccess;
|
---|
314 | /** Starting sector corresponding to the grain buffer. */
|
---|
315 | uint32_t uGrainSectorAbs;
|
---|
316 | /** Grain number corresponding to the grain buffer. */
|
---|
317 | uint32_t uGrain;
|
---|
318 | /** Actual size of the compressed data, only valid for reading. */
|
---|
319 | uint32_t cbGrainStreamRead;
|
---|
320 | /** Size of compressed grain buffer for streamOptimized extents. */
|
---|
321 | size_t cbCompGrain;
|
---|
322 | /** Compressed grain buffer for streamOptimized extents, with marker. */
|
---|
323 | void *pvCompGrain;
|
---|
324 | /** Decompressed grain buffer for streamOptimized extents. */
|
---|
325 | void *pvGrain;
|
---|
326 | /** Reference to the image in which this extent is used. Do not use this
|
---|
327 | * on a regular basis to avoid passing pImage references to functions
|
---|
328 | * explicitly. */
|
---|
329 | struct VMDKIMAGE *pImage;
|
---|
330 | } VMDKEXTENT, *PVMDKEXTENT;
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Grain table cache size. Allocated per image.
|
---|
334 | */
|
---|
335 | #define VMDK_GT_CACHE_SIZE 256
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Grain table block size. Smaller than an actual grain table block to allow
|
---|
339 | * more grain table blocks to be cached without having to allocate excessive
|
---|
340 | * amounts of memory for the cache.
|
---|
341 | */
|
---|
342 | #define VMDK_GT_CACHELINE_SIZE 128
|
---|
343 |
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Maximum number of lines in a descriptor file. Not worth the effort of
|
---|
347 | * making it variable. Descriptor files are generally very short (~20 lines),
|
---|
348 | * with the exception of sparse files split in 2G chunks, which need for the
|
---|
349 | * maximum size (almost 2T) exactly 1025 lines for the disk database.
|
---|
350 | */
|
---|
351 | #define VMDK_DESCRIPTOR_LINES_MAX 1100U
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Parsed descriptor information. Allows easy access and update of the
|
---|
355 | * descriptor (whether separate file or not). Free form text files suck.
|
---|
356 | */
|
---|
357 | typedef struct VMDKDESCRIPTOR
|
---|
358 | {
|
---|
359 | /** Line number of first entry of the disk descriptor. */
|
---|
360 | unsigned uFirstDesc;
|
---|
361 | /** Line number of first entry in the extent description. */
|
---|
362 | unsigned uFirstExtent;
|
---|
363 | /** Line number of first disk database entry. */
|
---|
364 | unsigned uFirstDDB;
|
---|
365 | /** Total number of lines. */
|
---|
366 | unsigned cLines;
|
---|
367 | /** Total amount of memory available for the descriptor. */
|
---|
368 | size_t cbDescAlloc;
|
---|
369 | /** Set if descriptor has been changed and not yet written to disk. */
|
---|
370 | bool fDirty;
|
---|
371 | /** Array of pointers to the data in the descriptor. */
|
---|
372 | char *aLines[VMDK_DESCRIPTOR_LINES_MAX];
|
---|
373 | /** Array of line indices pointing to the next non-comment line. */
|
---|
374 | unsigned aNextLines[VMDK_DESCRIPTOR_LINES_MAX];
|
---|
375 | } VMDKDESCRIPTOR, *PVMDKDESCRIPTOR;
|
---|
376 |
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Cache entry for translating extent/sector to a sector number in that
|
---|
380 | * extent.
|
---|
381 | */
|
---|
382 | typedef struct VMDKGTCACHEENTRY
|
---|
383 | {
|
---|
384 | /** Extent number for which this entry is valid. */
|
---|
385 | uint32_t uExtent;
|
---|
386 | /** GT data block number. */
|
---|
387 | uint64_t uGTBlock;
|
---|
388 | /** Data part of the cache entry. */
|
---|
389 | uint32_t aGTData[VMDK_GT_CACHELINE_SIZE];
|
---|
390 | } VMDKGTCACHEENTRY, *PVMDKGTCACHEENTRY;
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Cache data structure for blocks of grain table entries. For now this is a
|
---|
394 | * fixed size direct mapping cache, but this should be adapted to the size of
|
---|
395 | * the sparse image and maybe converted to a set-associative cache. The
|
---|
396 | * implementation below implements a write-through cache with write allocate.
|
---|
397 | */
|
---|
398 | typedef struct VMDKGTCACHE
|
---|
399 | {
|
---|
400 | /** Cache entries. */
|
---|
401 | VMDKGTCACHEENTRY aGTCache[VMDK_GT_CACHE_SIZE];
|
---|
402 | /** Number of cache entries (currently unused). */
|
---|
403 | unsigned cEntries;
|
---|
404 | } VMDKGTCACHE, *PVMDKGTCACHE;
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Complete VMDK image data structure. Mainly a collection of extents and a few
|
---|
408 | * extra global data fields.
|
---|
409 | */
|
---|
410 | typedef struct VMDKIMAGE
|
---|
411 | {
|
---|
412 | /** Image name. */
|
---|
413 | const char *pszFilename;
|
---|
414 | /** Descriptor file if applicable. */
|
---|
415 | PVMDKFILE pFile;
|
---|
416 |
|
---|
417 | /** Pointer to the per-disk VD interface list. */
|
---|
418 | PVDINTERFACE pVDIfsDisk;
|
---|
419 | /** Pointer to the per-image VD interface list. */
|
---|
420 | PVDINTERFACE pVDIfsImage;
|
---|
421 |
|
---|
422 | /** Error interface. */
|
---|
423 | PVDINTERFACEERROR pIfError;
|
---|
424 | /** I/O interface. */
|
---|
425 | PVDINTERFACEIOINT pIfIo;
|
---|
426 |
|
---|
427 |
|
---|
428 | /** Pointer to the image extents. */
|
---|
429 | PVMDKEXTENT pExtents;
|
---|
430 | /** Number of image extents. */
|
---|
431 | unsigned cExtents;
|
---|
432 | /** Pointer to the files list, for opening a file referenced multiple
|
---|
433 | * times only once (happens mainly with raw partition access). */
|
---|
434 | PVMDKFILE pFiles;
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Pointer to an array of segment entries for async I/O.
|
---|
438 | * This is an optimization because the task number to submit is not known
|
---|
439 | * and allocating/freeing an array in the read/write functions every time
|
---|
440 | * is too expensive.
|
---|
441 | */
|
---|
442 | PPDMDATASEG paSegments;
|
---|
443 | /** Entries available in the segments array. */
|
---|
444 | unsigned cSegments;
|
---|
445 |
|
---|
446 | /** Open flags passed by VBoxHD layer. */
|
---|
447 | unsigned uOpenFlags;
|
---|
448 | /** Image flags defined during creation or determined during open. */
|
---|
449 | unsigned uImageFlags;
|
---|
450 | /** Total size of the image. */
|
---|
451 | uint64_t cbSize;
|
---|
452 | /** Physical geometry of this image. */
|
---|
453 | VDGEOMETRY PCHSGeometry;
|
---|
454 | /** Logical geometry of this image. */
|
---|
455 | VDGEOMETRY LCHSGeometry;
|
---|
456 | /** Image UUID. */
|
---|
457 | RTUUID ImageUuid;
|
---|
458 | /** Image modification UUID. */
|
---|
459 | RTUUID ModificationUuid;
|
---|
460 | /** Parent image UUID. */
|
---|
461 | RTUUID ParentUuid;
|
---|
462 | /** Parent image modification UUID. */
|
---|
463 | RTUUID ParentModificationUuid;
|
---|
464 |
|
---|
465 | /** Pointer to grain table cache, if this image contains sparse extents. */
|
---|
466 | PVMDKGTCACHE pGTCache;
|
---|
467 | /** Pointer to the descriptor (NULL if no separate descriptor file). */
|
---|
468 | char *pDescData;
|
---|
469 | /** Allocation size of the descriptor file. */
|
---|
470 | size_t cbDescAlloc;
|
---|
471 | /** Parsed descriptor file content. */
|
---|
472 | VMDKDESCRIPTOR Descriptor;
|
---|
473 | } VMDKIMAGE;
|
---|
474 |
|
---|
475 |
|
---|
476 | /** State for the input/output callout of the inflate reader/deflate writer. */
|
---|
477 | typedef struct VMDKCOMPRESSIO
|
---|
478 | {
|
---|
479 | /* Image this operation relates to. */
|
---|
480 | PVMDKIMAGE pImage;
|
---|
481 | /* Current read position. */
|
---|
482 | ssize_t iOffset;
|
---|
483 | /* Size of the compressed grain buffer (available data). */
|
---|
484 | size_t cbCompGrain;
|
---|
485 | /* Pointer to the compressed grain buffer. */
|
---|
486 | void *pvCompGrain;
|
---|
487 | } VMDKCOMPRESSIO;
|
---|
488 |
|
---|
489 |
|
---|
490 | /** Tracks async grain allocation. */
|
---|
491 | typedef struct VMDKGRAINALLOCASYNC
|
---|
492 | {
|
---|
493 | /** Flag whether the allocation failed. */
|
---|
494 | bool fIoErr;
|
---|
495 | /** Current number of transfers pending.
|
---|
496 | * If reached 0 and there is an error the old state is restored. */
|
---|
497 | unsigned cIoXfersPending;
|
---|
498 | /** Sector number */
|
---|
499 | uint64_t uSector;
|
---|
500 | /** Flag whether the grain table needs to be updated. */
|
---|
501 | bool fGTUpdateNeeded;
|
---|
502 | /** Extent the allocation happens. */
|
---|
503 | PVMDKEXTENT pExtent;
|
---|
504 | /** Position of the new grain, required for the grain table update. */
|
---|
505 | uint64_t uGrainOffset;
|
---|
506 | /** Grain table sector. */
|
---|
507 | uint64_t uGTSector;
|
---|
508 | /** Backup grain table sector. */
|
---|
509 | uint64_t uRGTSector;
|
---|
510 | } VMDKGRAINALLOCASYNC, *PVMDKGRAINALLOCASYNC;
|
---|
511 |
|
---|
512 | /*******************************************************************************
|
---|
513 | * Static Variables *
|
---|
514 | *******************************************************************************/
|
---|
515 |
|
---|
516 | /** NULL-terminated array of supported file extensions. */
|
---|
517 | static const VDFILEEXTENSION s_aVmdkFileExtensions[] =
|
---|
518 | {
|
---|
519 | {"vmdk", VDTYPE_HDD},
|
---|
520 | {NULL, VDTYPE_INVALID}
|
---|
521 | };
|
---|
522 |
|
---|
523 | /*******************************************************************************
|
---|
524 | * Internal Functions *
|
---|
525 | *******************************************************************************/
|
---|
526 |
|
---|
527 | static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent);
|
---|
528 | static void vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
529 | bool fDelete);
|
---|
530 |
|
---|
531 | static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents);
|
---|
532 | static int vmdkFlushImage(PVMDKIMAGE pImage);
|
---|
533 | static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment);
|
---|
534 | static int vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete);
|
---|
535 |
|
---|
536 | static int vmdkAllocGrainAsyncComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq);
|
---|
537 |
|
---|
538 | /**
|
---|
539 | * Internal: open a file (using a file descriptor cache to ensure each file
|
---|
540 | * is only opened once - anything else can cause locking problems).
|
---|
541 | */
|
---|
542 | static int vmdkFileOpen(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile,
|
---|
543 | const char *pszFilename, uint32_t fOpen, bool fAsyncIO)
|
---|
544 | {
|
---|
545 | int rc = VINF_SUCCESS;
|
---|
546 | PVMDKFILE pVmdkFile;
|
---|
547 |
|
---|
548 | for (pVmdkFile = pImage->pFiles;
|
---|
549 | pVmdkFile != NULL;
|
---|
550 | pVmdkFile = pVmdkFile->pNext)
|
---|
551 | {
|
---|
552 | if (!strcmp(pszFilename, pVmdkFile->pszFilename))
|
---|
553 | {
|
---|
554 | Assert(fOpen == pVmdkFile->fOpen);
|
---|
555 | pVmdkFile->uReferences++;
|
---|
556 |
|
---|
557 | *ppVmdkFile = pVmdkFile;
|
---|
558 |
|
---|
559 | return rc;
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|
563 | /* If we get here, there's no matching entry in the cache. */
|
---|
564 | pVmdkFile = (PVMDKFILE)RTMemAllocZ(sizeof(VMDKFILE));
|
---|
565 | if (!VALID_PTR(pVmdkFile))
|
---|
566 | {
|
---|
567 | *ppVmdkFile = NULL;
|
---|
568 | return VERR_NO_MEMORY;
|
---|
569 | }
|
---|
570 |
|
---|
571 | pVmdkFile->pszFilename = RTStrDup(pszFilename);
|
---|
572 | if (!VALID_PTR(pVmdkFile->pszFilename))
|
---|
573 | {
|
---|
574 | RTMemFree(pVmdkFile);
|
---|
575 | *ppVmdkFile = NULL;
|
---|
576 | return VERR_NO_MEMORY;
|
---|
577 | }
|
---|
578 | pVmdkFile->fOpen = fOpen;
|
---|
579 | pVmdkFile->fAsyncIO = fAsyncIO;
|
---|
580 |
|
---|
581 | rc = vdIfIoIntFileOpen(pImage->pIfIo, pszFilename, fOpen,
|
---|
582 | &pVmdkFile->pStorage);
|
---|
583 | if (RT_SUCCESS(rc))
|
---|
584 | {
|
---|
585 | pVmdkFile->uReferences = 1;
|
---|
586 | pVmdkFile->pImage = pImage;
|
---|
587 | pVmdkFile->pNext = pImage->pFiles;
|
---|
588 | if (pImage->pFiles)
|
---|
589 | pImage->pFiles->pPrev = pVmdkFile;
|
---|
590 | pImage->pFiles = pVmdkFile;
|
---|
591 | *ppVmdkFile = pVmdkFile;
|
---|
592 | }
|
---|
593 | else
|
---|
594 | {
|
---|
595 | RTStrFree((char *)(void *)pVmdkFile->pszFilename);
|
---|
596 | RTMemFree(pVmdkFile);
|
---|
597 | *ppVmdkFile = NULL;
|
---|
598 | }
|
---|
599 |
|
---|
600 | return rc;
|
---|
601 | }
|
---|
602 |
|
---|
603 | /**
|
---|
604 | * Internal: close a file, updating the file descriptor cache.
|
---|
605 | */
|
---|
606 | static int vmdkFileClose(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile, bool fDelete)
|
---|
607 | {
|
---|
608 | int rc = VINF_SUCCESS;
|
---|
609 | PVMDKFILE pVmdkFile = *ppVmdkFile;
|
---|
610 |
|
---|
611 | AssertPtr(pVmdkFile);
|
---|
612 |
|
---|
613 | pVmdkFile->fDelete |= fDelete;
|
---|
614 | Assert(pVmdkFile->uReferences);
|
---|
615 | pVmdkFile->uReferences--;
|
---|
616 | if (pVmdkFile->uReferences == 0)
|
---|
617 | {
|
---|
618 | PVMDKFILE pPrev;
|
---|
619 | PVMDKFILE pNext;
|
---|
620 |
|
---|
621 | /* Unchain the element from the list. */
|
---|
622 | pPrev = pVmdkFile->pPrev;
|
---|
623 | pNext = pVmdkFile->pNext;
|
---|
624 |
|
---|
625 | if (pNext)
|
---|
626 | pNext->pPrev = pPrev;
|
---|
627 | if (pPrev)
|
---|
628 | pPrev->pNext = pNext;
|
---|
629 | else
|
---|
630 | pImage->pFiles = pNext;
|
---|
631 |
|
---|
632 | rc = vdIfIoIntFileClose(pImage->pIfIo, pVmdkFile->pStorage);
|
---|
633 | if (RT_SUCCESS(rc) && pVmdkFile->fDelete)
|
---|
634 | rc = vdIfIoIntFileDelete(pImage->pIfIo, pVmdkFile->pszFilename);
|
---|
635 | RTStrFree((char *)(void *)pVmdkFile->pszFilename);
|
---|
636 | RTMemFree(pVmdkFile);
|
---|
637 | }
|
---|
638 |
|
---|
639 | *ppVmdkFile = NULL;
|
---|
640 | return rc;
|
---|
641 | }
|
---|
642 |
|
---|
643 | static DECLCALLBACK(int) vmdkFileInflateHelper(void *pvUser, void *pvBuf, size_t cbBuf, size_t *pcbBuf)
|
---|
644 | {
|
---|
645 | VMDKCOMPRESSIO *pInflateState = (VMDKCOMPRESSIO *)pvUser;
|
---|
646 | size_t cbInjected = 0;
|
---|
647 |
|
---|
648 | Assert(cbBuf);
|
---|
649 | if (pInflateState->iOffset < 0)
|
---|
650 | {
|
---|
651 | *(uint8_t *)pvBuf = RTZIPTYPE_ZLIB;
|
---|
652 | pvBuf = (uint8_t *)pvBuf + 1;
|
---|
653 | cbBuf--;
|
---|
654 | cbInjected = 1;
|
---|
655 | pInflateState->iOffset = RT_OFFSETOF(VMDKMARKER, uType);
|
---|
656 | }
|
---|
657 | if (!cbBuf)
|
---|
658 | {
|
---|
659 | if (pcbBuf)
|
---|
660 | *pcbBuf = cbInjected;
|
---|
661 | return VINF_SUCCESS;
|
---|
662 | }
|
---|
663 | cbBuf = RT_MIN(cbBuf, pInflateState->cbCompGrain - pInflateState->iOffset);
|
---|
664 | memcpy(pvBuf,
|
---|
665 | (uint8_t *)pInflateState->pvCompGrain + pInflateState->iOffset,
|
---|
666 | cbBuf);
|
---|
667 | pInflateState->iOffset += cbBuf;
|
---|
668 | Assert(pcbBuf);
|
---|
669 | *pcbBuf = cbBuf + cbInjected;
|
---|
670 | return VINF_SUCCESS;
|
---|
671 | }
|
---|
672 |
|
---|
673 | /**
|
---|
674 | * Internal: read from a file and inflate the compressed data,
|
---|
675 | * distinguishing between async and normal operation
|
---|
676 | */
|
---|
677 | DECLINLINE(int) vmdkFileInflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
678 | uint64_t uOffset, void *pvBuf,
|
---|
679 | size_t cbToRead, const void *pcvMarker,
|
---|
680 | uint64_t *puLBA, uint32_t *pcbMarkerData)
|
---|
681 | {
|
---|
682 | if (pExtent->pFile->fAsyncIO)
|
---|
683 | {
|
---|
684 | AssertMsgFailed(("TODO\n"));
|
---|
685 | return VERR_NOT_SUPPORTED;
|
---|
686 | }
|
---|
687 | else
|
---|
688 | {
|
---|
689 | int rc;
|
---|
690 | PRTZIPDECOMP pZip = NULL;
|
---|
691 | VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
|
---|
692 | size_t cbCompSize, cbActuallyRead;
|
---|
693 |
|
---|
694 | if (!pcvMarker)
|
---|
695 | {
|
---|
696 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
697 | uOffset, pMarker, RT_OFFSETOF(VMDKMARKER, uType),
|
---|
698 | NULL);
|
---|
699 | if (RT_FAILURE(rc))
|
---|
700 | return rc;
|
---|
701 | }
|
---|
702 | else
|
---|
703 | memcpy(pMarker, pcvMarker, RT_OFFSETOF(VMDKMARKER, uType));
|
---|
704 |
|
---|
705 | cbCompSize = RT_LE2H_U32(pMarker->cbSize);
|
---|
706 | if (cbCompSize == 0)
|
---|
707 | {
|
---|
708 | AssertMsgFailed(("VMDK: corrupted marker\n"));
|
---|
709 | return VERR_VD_VMDK_INVALID_FORMAT;
|
---|
710 | }
|
---|
711 |
|
---|
712 | /* Sanity check - the expansion ratio should be much less than 2. */
|
---|
713 | Assert(cbCompSize < 2 * cbToRead);
|
---|
714 | if (cbCompSize >= 2 * cbToRead)
|
---|
715 | return VERR_VD_VMDK_INVALID_FORMAT;
|
---|
716 |
|
---|
717 | /* Compressed grain marker. Data follows immediately. */
|
---|
718 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
719 | uOffset + RT_OFFSETOF(VMDKMARKER, uType),
|
---|
720 | (uint8_t *)pExtent->pvCompGrain
|
---|
721 | + RT_OFFSETOF(VMDKMARKER, uType),
|
---|
722 | RT_ALIGN_Z( cbCompSize
|
---|
723 | + RT_OFFSETOF(VMDKMARKER, uType),
|
---|
724 | 512)
|
---|
725 | - RT_OFFSETOF(VMDKMARKER, uType), NULL);
|
---|
726 |
|
---|
727 | if (puLBA)
|
---|
728 | *puLBA = RT_LE2H_U64(pMarker->uSector);
|
---|
729 | if (pcbMarkerData)
|
---|
730 | *pcbMarkerData = RT_ALIGN( cbCompSize
|
---|
731 | + RT_OFFSETOF(VMDKMARKER, uType),
|
---|
732 | 512);
|
---|
733 |
|
---|
734 | VMDKCOMPRESSIO InflateState;
|
---|
735 | InflateState.pImage = pImage;
|
---|
736 | InflateState.iOffset = -1;
|
---|
737 | InflateState.cbCompGrain = cbCompSize + RT_OFFSETOF(VMDKMARKER, uType);
|
---|
738 | InflateState.pvCompGrain = pExtent->pvCompGrain;
|
---|
739 |
|
---|
740 | rc = RTZipDecompCreate(&pZip, &InflateState, vmdkFileInflateHelper);
|
---|
741 | if (RT_FAILURE(rc))
|
---|
742 | return rc;
|
---|
743 | rc = RTZipDecompress(pZip, pvBuf, cbToRead, &cbActuallyRead);
|
---|
744 | RTZipDecompDestroy(pZip);
|
---|
745 | if (RT_FAILURE(rc))
|
---|
746 | {
|
---|
747 | if (rc == VERR_ZIP_CORRUPTED)
|
---|
748 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: Compressed image is corrupted '%s'"), pExtent->pszFullname);
|
---|
749 | return rc;
|
---|
750 | }
|
---|
751 | if (cbActuallyRead != cbToRead)
|
---|
752 | rc = VERR_VD_VMDK_INVALID_FORMAT;
|
---|
753 | return rc;
|
---|
754 | }
|
---|
755 | }
|
---|
756 |
|
---|
757 | static DECLCALLBACK(int) vmdkFileDeflateHelper(void *pvUser, const void *pvBuf, size_t cbBuf)
|
---|
758 | {
|
---|
759 | VMDKCOMPRESSIO *pDeflateState = (VMDKCOMPRESSIO *)pvUser;
|
---|
760 |
|
---|
761 | Assert(cbBuf);
|
---|
762 | if (pDeflateState->iOffset < 0)
|
---|
763 | {
|
---|
764 | pvBuf = (const uint8_t *)pvBuf + 1;
|
---|
765 | cbBuf--;
|
---|
766 | pDeflateState->iOffset = RT_OFFSETOF(VMDKMARKER, uType);
|
---|
767 | }
|
---|
768 | if (!cbBuf)
|
---|
769 | return VINF_SUCCESS;
|
---|
770 | if (pDeflateState->iOffset + cbBuf > pDeflateState->cbCompGrain)
|
---|
771 | return VERR_BUFFER_OVERFLOW;
|
---|
772 | memcpy((uint8_t *)pDeflateState->pvCompGrain + pDeflateState->iOffset,
|
---|
773 | pvBuf, cbBuf);
|
---|
774 | pDeflateState->iOffset += cbBuf;
|
---|
775 | return VINF_SUCCESS;
|
---|
776 | }
|
---|
777 |
|
---|
778 | /**
|
---|
779 | * Internal: deflate the uncompressed data and write to a file,
|
---|
780 | * distinguishing between async and normal operation
|
---|
781 | */
|
---|
782 | DECLINLINE(int) vmdkFileDeflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
783 | uint64_t uOffset, const void *pvBuf,
|
---|
784 | size_t cbToWrite, uint64_t uLBA,
|
---|
785 | uint32_t *pcbMarkerData)
|
---|
786 | {
|
---|
787 | if (pExtent->pFile->fAsyncIO)
|
---|
788 | {
|
---|
789 | AssertMsgFailed(("TODO\n"));
|
---|
790 | return VERR_NOT_SUPPORTED;
|
---|
791 | }
|
---|
792 | else
|
---|
793 | {
|
---|
794 | int rc;
|
---|
795 | PRTZIPCOMP pZip = NULL;
|
---|
796 | VMDKCOMPRESSIO DeflateState;
|
---|
797 |
|
---|
798 | DeflateState.pImage = pImage;
|
---|
799 | DeflateState.iOffset = -1;
|
---|
800 | DeflateState.cbCompGrain = pExtent->cbCompGrain;
|
---|
801 | DeflateState.pvCompGrain = pExtent->pvCompGrain;
|
---|
802 |
|
---|
803 | rc = RTZipCompCreate(&pZip, &DeflateState, vmdkFileDeflateHelper,
|
---|
804 | RTZIPTYPE_ZLIB, RTZIPLEVEL_DEFAULT);
|
---|
805 | if (RT_FAILURE(rc))
|
---|
806 | return rc;
|
---|
807 | rc = RTZipCompress(pZip, pvBuf, cbToWrite);
|
---|
808 | if (RT_SUCCESS(rc))
|
---|
809 | rc = RTZipCompFinish(pZip);
|
---|
810 | RTZipCompDestroy(pZip);
|
---|
811 | if (RT_SUCCESS(rc))
|
---|
812 | {
|
---|
813 | Assert( DeflateState.iOffset > 0
|
---|
814 | && (size_t)DeflateState.iOffset <= DeflateState.cbCompGrain);
|
---|
815 |
|
---|
816 | /* pad with zeroes to get to a full sector size */
|
---|
817 | uint32_t uSize = DeflateState.iOffset;
|
---|
818 | if (uSize % 512)
|
---|
819 | {
|
---|
820 | uint32_t uSizeAlign = RT_ALIGN(uSize, 512);
|
---|
821 | memset((uint8_t *)pExtent->pvCompGrain + uSize, '\0',
|
---|
822 | uSizeAlign - uSize);
|
---|
823 | uSize = uSizeAlign;
|
---|
824 | }
|
---|
825 |
|
---|
826 | if (pcbMarkerData)
|
---|
827 | *pcbMarkerData = uSize;
|
---|
828 |
|
---|
829 | /* Compressed grain marker. Data follows immediately. */
|
---|
830 | VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
|
---|
831 | pMarker->uSector = RT_H2LE_U64(uLBA);
|
---|
832 | pMarker->cbSize = RT_H2LE_U32( DeflateState.iOffset
|
---|
833 | - RT_OFFSETOF(VMDKMARKER, uType));
|
---|
834 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
835 | uOffset, pMarker, uSize, NULL);
|
---|
836 | if (RT_FAILURE(rc))
|
---|
837 | return rc;
|
---|
838 | }
|
---|
839 | return rc;
|
---|
840 | }
|
---|
841 | }
|
---|
842 |
|
---|
843 |
|
---|
844 | /**
|
---|
845 | * Internal: check if all files are closed, prevent leaking resources.
|
---|
846 | */
|
---|
847 | static int vmdkFileCheckAllClose(PVMDKIMAGE pImage)
|
---|
848 | {
|
---|
849 | int rc = VINF_SUCCESS, rc2;
|
---|
850 | PVMDKFILE pVmdkFile;
|
---|
851 |
|
---|
852 | Assert(pImage->pFiles == NULL);
|
---|
853 | for (pVmdkFile = pImage->pFiles;
|
---|
854 | pVmdkFile != NULL;
|
---|
855 | pVmdkFile = pVmdkFile->pNext)
|
---|
856 | {
|
---|
857 | LogRel(("VMDK: leaking reference to file \"%s\"\n",
|
---|
858 | pVmdkFile->pszFilename));
|
---|
859 | pImage->pFiles = pVmdkFile->pNext;
|
---|
860 |
|
---|
861 | rc2 = vmdkFileClose(pImage, &pVmdkFile, pVmdkFile->fDelete);
|
---|
862 |
|
---|
863 | if (RT_SUCCESS(rc))
|
---|
864 | rc = rc2;
|
---|
865 | }
|
---|
866 | return rc;
|
---|
867 | }
|
---|
868 |
|
---|
869 | /**
|
---|
870 | * Internal: truncate a string (at a UTF8 code point boundary) and encode the
|
---|
871 | * critical non-ASCII characters.
|
---|
872 | */
|
---|
873 | static char *vmdkEncodeString(const char *psz)
|
---|
874 | {
|
---|
875 | char szEnc[VMDK_ENCODED_COMMENT_MAX + 3];
|
---|
876 | char *pszDst = szEnc;
|
---|
877 |
|
---|
878 | AssertPtr(psz);
|
---|
879 |
|
---|
880 | for (; *psz; psz = RTStrNextCp(psz))
|
---|
881 | {
|
---|
882 | char *pszDstPrev = pszDst;
|
---|
883 | RTUNICP Cp = RTStrGetCp(psz);
|
---|
884 | if (Cp == '\\')
|
---|
885 | {
|
---|
886 | pszDst = RTStrPutCp(pszDst, Cp);
|
---|
887 | pszDst = RTStrPutCp(pszDst, Cp);
|
---|
888 | }
|
---|
889 | else if (Cp == '\n')
|
---|
890 | {
|
---|
891 | pszDst = RTStrPutCp(pszDst, '\\');
|
---|
892 | pszDst = RTStrPutCp(pszDst, 'n');
|
---|
893 | }
|
---|
894 | else if (Cp == '\r')
|
---|
895 | {
|
---|
896 | pszDst = RTStrPutCp(pszDst, '\\');
|
---|
897 | pszDst = RTStrPutCp(pszDst, 'r');
|
---|
898 | }
|
---|
899 | else
|
---|
900 | pszDst = RTStrPutCp(pszDst, Cp);
|
---|
901 | if (pszDst - szEnc >= VMDK_ENCODED_COMMENT_MAX - 1)
|
---|
902 | {
|
---|
903 | pszDst = pszDstPrev;
|
---|
904 | break;
|
---|
905 | }
|
---|
906 | }
|
---|
907 | *pszDst = '\0';
|
---|
908 | return RTStrDup(szEnc);
|
---|
909 | }
|
---|
910 |
|
---|
911 | /**
|
---|
912 | * Internal: decode a string and store it into the specified string.
|
---|
913 | */
|
---|
914 | static int vmdkDecodeString(const char *pszEncoded, char *psz, size_t cb)
|
---|
915 | {
|
---|
916 | int rc = VINF_SUCCESS;
|
---|
917 | char szBuf[4];
|
---|
918 |
|
---|
919 | if (!cb)
|
---|
920 | return VERR_BUFFER_OVERFLOW;
|
---|
921 |
|
---|
922 | AssertPtr(psz);
|
---|
923 |
|
---|
924 | for (; *pszEncoded; pszEncoded = RTStrNextCp(pszEncoded))
|
---|
925 | {
|
---|
926 | char *pszDst = szBuf;
|
---|
927 | RTUNICP Cp = RTStrGetCp(pszEncoded);
|
---|
928 | if (Cp == '\\')
|
---|
929 | {
|
---|
930 | pszEncoded = RTStrNextCp(pszEncoded);
|
---|
931 | RTUNICP CpQ = RTStrGetCp(pszEncoded);
|
---|
932 | if (CpQ == 'n')
|
---|
933 | RTStrPutCp(pszDst, '\n');
|
---|
934 | else if (CpQ == 'r')
|
---|
935 | RTStrPutCp(pszDst, '\r');
|
---|
936 | else if (CpQ == '\0')
|
---|
937 | {
|
---|
938 | rc = VERR_VD_VMDK_INVALID_HEADER;
|
---|
939 | break;
|
---|
940 | }
|
---|
941 | else
|
---|
942 | RTStrPutCp(pszDst, CpQ);
|
---|
943 | }
|
---|
944 | else
|
---|
945 | pszDst = RTStrPutCp(pszDst, Cp);
|
---|
946 |
|
---|
947 | /* Need to leave space for terminating NUL. */
|
---|
948 | if ((size_t)(pszDst - szBuf) + 1 >= cb)
|
---|
949 | {
|
---|
950 | rc = VERR_BUFFER_OVERFLOW;
|
---|
951 | break;
|
---|
952 | }
|
---|
953 | memcpy(psz, szBuf, pszDst - szBuf);
|
---|
954 | psz += pszDst - szBuf;
|
---|
955 | }
|
---|
956 | *psz = '\0';
|
---|
957 | return rc;
|
---|
958 | }
|
---|
959 |
|
---|
960 | /**
|
---|
961 | * Internal: free all buffers associated with grain directories.
|
---|
962 | */
|
---|
963 | static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent)
|
---|
964 | {
|
---|
965 | if (pExtent->pGD)
|
---|
966 | {
|
---|
967 | RTMemFree(pExtent->pGD);
|
---|
968 | pExtent->pGD = NULL;
|
---|
969 | }
|
---|
970 | if (pExtent->pRGD)
|
---|
971 | {
|
---|
972 | RTMemFree(pExtent->pRGD);
|
---|
973 | pExtent->pRGD = NULL;
|
---|
974 | }
|
---|
975 | }
|
---|
976 |
|
---|
977 | /**
|
---|
978 | * Internal: allocate the compressed/uncompressed buffers for streamOptimized
|
---|
979 | * images.
|
---|
980 | */
|
---|
981 | static int vmdkAllocStreamBuffers(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
|
---|
982 | {
|
---|
983 | int rc = VINF_SUCCESS;
|
---|
984 |
|
---|
985 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
986 | {
|
---|
987 | /* streamOptimized extents need a compressed grain buffer, which must
|
---|
988 | * be big enough to hold uncompressible data (which needs ~8 bytes
|
---|
989 | * more than the uncompressed data), the marker and padding. */
|
---|
990 | pExtent->cbCompGrain = RT_ALIGN_Z( VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
|
---|
991 | + 8 + sizeof(VMDKMARKER), 512);
|
---|
992 | pExtent->pvCompGrain = RTMemAlloc(pExtent->cbCompGrain);
|
---|
993 | if (!pExtent->pvCompGrain)
|
---|
994 | {
|
---|
995 | rc = VERR_NO_MEMORY;
|
---|
996 | goto out;
|
---|
997 | }
|
---|
998 |
|
---|
999 | /* streamOptimized extents need a decompressed grain buffer. */
|
---|
1000 | pExtent->pvGrain = RTMemAlloc(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
|
---|
1001 | if (!pExtent->pvGrain)
|
---|
1002 | {
|
---|
1003 | rc = VERR_NO_MEMORY;
|
---|
1004 | goto out;
|
---|
1005 | }
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | out:
|
---|
1009 | if (RT_FAILURE(rc))
|
---|
1010 | vmdkFreeStreamBuffers(pExtent);
|
---|
1011 | return rc;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | /**
|
---|
1015 | * Internal: allocate all buffers associated with grain directories.
|
---|
1016 | */
|
---|
1017 | static int vmdkAllocGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
|
---|
1018 | {
|
---|
1019 | int rc = VINF_SUCCESS;
|
---|
1020 | size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
|
---|
1021 | uint32_t *pGD = NULL, *pRGD = NULL;
|
---|
1022 |
|
---|
1023 | pGD = (uint32_t *)RTMemAllocZ(cbGD);
|
---|
1024 | if (!pGD)
|
---|
1025 | {
|
---|
1026 | rc = VERR_NO_MEMORY;
|
---|
1027 | goto out;
|
---|
1028 | }
|
---|
1029 | pExtent->pGD = pGD;
|
---|
1030 |
|
---|
1031 | if (pExtent->uSectorRGD)
|
---|
1032 | {
|
---|
1033 | pRGD = (uint32_t *)RTMemAllocZ(cbGD);
|
---|
1034 | if (!pRGD)
|
---|
1035 | {
|
---|
1036 | rc = VERR_NO_MEMORY;
|
---|
1037 | goto out;
|
---|
1038 | }
|
---|
1039 | pExtent->pRGD = pRGD;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | out:
|
---|
1043 | if (RT_FAILURE(rc))
|
---|
1044 | vmdkFreeGrainDirectory(pExtent);
|
---|
1045 | return rc;
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | static int vmdkReadGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
|
---|
1049 | {
|
---|
1050 | int rc = VINF_SUCCESS;
|
---|
1051 | unsigned i;
|
---|
1052 | uint32_t *pGDTmp, *pRGDTmp;
|
---|
1053 | size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
|
---|
1054 |
|
---|
1055 | if (pExtent->enmType != VMDKETYPE_HOSTED_SPARSE)
|
---|
1056 | goto out;
|
---|
1057 |
|
---|
1058 | if ( pExtent->uSectorGD == VMDK_GD_AT_END
|
---|
1059 | || pExtent->uSectorRGD == VMDK_GD_AT_END)
|
---|
1060 | {
|
---|
1061 | rc = VERR_INTERNAL_ERROR;
|
---|
1062 | goto out;
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | rc = vmdkAllocGrainDirectory(pImage, pExtent);
|
---|
1066 | if (RT_FAILURE(rc))
|
---|
1067 | goto out;
|
---|
1068 |
|
---|
1069 | /* The VMDK 1.1 spec seems to talk about compressed grain directories,
|
---|
1070 | * but in reality they are not compressed. */
|
---|
1071 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
1072 | VMDK_SECTOR2BYTE(pExtent->uSectorGD),
|
---|
1073 | pExtent->pGD, cbGD, NULL);
|
---|
1074 | AssertRC(rc);
|
---|
1075 | if (RT_FAILURE(rc))
|
---|
1076 | {
|
---|
1077 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not read grain directory in '%s': %Rrc"), pExtent->pszFullname);
|
---|
1078 | goto out;
|
---|
1079 | }
|
---|
1080 | for (i = 0, pGDTmp = pExtent->pGD; i < pExtent->cGDEntries; i++, pGDTmp++)
|
---|
1081 | *pGDTmp = RT_LE2H_U32(*pGDTmp);
|
---|
1082 |
|
---|
1083 | if (pExtent->uSectorRGD)
|
---|
1084 | {
|
---|
1085 | /* The VMDK 1.1 spec seems to talk about compressed grain directories,
|
---|
1086 | * but in reality they are not compressed. */
|
---|
1087 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
1088 | VMDK_SECTOR2BYTE(pExtent->uSectorRGD),
|
---|
1089 | pExtent->pRGD, cbGD, NULL);
|
---|
1090 | AssertRC(rc);
|
---|
1091 | if (RT_FAILURE(rc))
|
---|
1092 | {
|
---|
1093 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not read redundant grain directory in '%s'"), pExtent->pszFullname);
|
---|
1094 | goto out;
|
---|
1095 | }
|
---|
1096 | for (i = 0, pRGDTmp = pExtent->pRGD; i < pExtent->cGDEntries; i++, pRGDTmp++)
|
---|
1097 | *pRGDTmp = RT_LE2H_U32(*pRGDTmp);
|
---|
1098 |
|
---|
1099 | /* Check grain table and redundant grain table for consistency. */
|
---|
1100 | size_t cbGT = pExtent->cGTEntries * sizeof(uint32_t);
|
---|
1101 | size_t cbGTBuffers = cbGT; /* Start with space for one GT. */
|
---|
1102 | size_t cbGTBuffersMax = _1M;
|
---|
1103 |
|
---|
1104 | uint32_t *pTmpGT1 = (uint32_t *)RTMemAlloc(cbGTBuffers);
|
---|
1105 | uint32_t *pTmpGT2 = (uint32_t *)RTMemAlloc(cbGTBuffers);
|
---|
1106 |
|
---|
1107 | if ( !pTmpGT1
|
---|
1108 | || !pTmpGT2)
|
---|
1109 | rc = VERR_NO_MEMORY;
|
---|
1110 |
|
---|
1111 | i = 0;
|
---|
1112 | pGDTmp = pExtent->pGD;
|
---|
1113 | pRGDTmp = pExtent->pRGD;
|
---|
1114 |
|
---|
1115 | /* Loop through all entries. */
|
---|
1116 | while (i < pExtent->cGDEntries)
|
---|
1117 | {
|
---|
1118 | uint32_t uGTStart = *pGDTmp;
|
---|
1119 | uint32_t uRGTStart = *pRGDTmp;
|
---|
1120 | uint32_t cbGTRead = cbGT;
|
---|
1121 |
|
---|
1122 | /* If no grain table is allocated skip the entry. */
|
---|
1123 | if (*pGDTmp == 0 && *pRGDTmp == 0)
|
---|
1124 | {
|
---|
1125 | i++;
|
---|
1126 | continue;
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | if (*pGDTmp == 0 || *pRGDTmp == 0 || *pGDTmp == *pRGDTmp)
|
---|
1130 | {
|
---|
1131 | /* Just one grain directory entry refers to a not yet allocated
|
---|
1132 | * grain table or both grain directory copies refer to the same
|
---|
1133 | * grain table. Not allowed. */
|
---|
1134 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: inconsistent references to grain directory in '%s'"), pExtent->pszFullname);
|
---|
1135 | break;
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | i++;
|
---|
1139 | pGDTmp++;
|
---|
1140 | pRGDTmp++;
|
---|
1141 |
|
---|
1142 | /*
|
---|
1143 | * Read a few tables at once if adjacent to decrease the number
|
---|
1144 | * of I/O requests. Read at maximum 1MB at once.
|
---|
1145 | */
|
---|
1146 | while ( i < pExtent->cGDEntries
|
---|
1147 | && cbGTRead < cbGTBuffersMax)
|
---|
1148 | {
|
---|
1149 | /* If no grain table is allocated skip the entry. */
|
---|
1150 | if (*pGDTmp == 0 && *pRGDTmp == 0)
|
---|
1151 | continue;
|
---|
1152 |
|
---|
1153 | if (*pGDTmp == 0 || *pRGDTmp == 0 || *pGDTmp == *pRGDTmp)
|
---|
1154 | {
|
---|
1155 | /* Just one grain directory entry refers to a not yet allocated
|
---|
1156 | * grain table or both grain directory copies refer to the same
|
---|
1157 | * grain table. Not allowed. */
|
---|
1158 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: inconsistent references to grain directory in '%s'"), pExtent->pszFullname);
|
---|
1159 | break;
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | /* Check that the start offsets are adjacent.*/
|
---|
1163 | if ( VMDK_SECTOR2BYTE(uGTStart) + cbGTRead != VMDK_SECTOR2BYTE(*pGDTmp)
|
---|
1164 | || VMDK_SECTOR2BYTE(uRGTStart) + cbGTRead != VMDK_SECTOR2BYTE(*pRGDTmp))
|
---|
1165 | break;
|
---|
1166 |
|
---|
1167 | i++;
|
---|
1168 | pGDTmp++;
|
---|
1169 | pRGDTmp++;
|
---|
1170 | cbGTRead += cbGT;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | /* Increase buffers if required. */
|
---|
1174 | if ( RT_SUCCESS(rc)
|
---|
1175 | && cbGTBuffers < cbGTRead)
|
---|
1176 | {
|
---|
1177 | uint32_t *pTmp;
|
---|
1178 | pTmp = (uint32_t *)RTMemRealloc(pTmpGT1, cbGTRead);
|
---|
1179 | if (pTmp)
|
---|
1180 | {
|
---|
1181 | pTmpGT1 = pTmp;
|
---|
1182 | pTmp = (uint32_t *)RTMemRealloc(pTmpGT2, cbGTRead);
|
---|
1183 | if (pTmp)
|
---|
1184 | pTmpGT2 = pTmp;
|
---|
1185 | else
|
---|
1186 | rc = VERR_NO_MEMORY;
|
---|
1187 | }
|
---|
1188 | else
|
---|
1189 | rc = VERR_NO_MEMORY;
|
---|
1190 |
|
---|
1191 | if (rc == VERR_NO_MEMORY)
|
---|
1192 | {
|
---|
1193 | /* Reset to the old values. */
|
---|
1194 | rc = VINF_SUCCESS;
|
---|
1195 | i -= cbGTRead / cbGT;
|
---|
1196 | cbGTRead = cbGT;
|
---|
1197 |
|
---|
1198 | /* Don't try to increase the buffer again in the next run. */
|
---|
1199 | cbGTBuffersMax = cbGTBuffers;
|
---|
1200 | }
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | if (RT_SUCCESS(rc))
|
---|
1204 | {
|
---|
1205 | /* The VMDK 1.1 spec seems to talk about compressed grain tables,
|
---|
1206 | * but in reality they are not compressed. */
|
---|
1207 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
1208 | VMDK_SECTOR2BYTE(uGTStart),
|
---|
1209 | pTmpGT1, cbGTRead, NULL);
|
---|
1210 | if (RT_FAILURE(rc))
|
---|
1211 | {
|
---|
1212 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
|
---|
1213 | N_("VMDK: error reading grain table in '%s'"), pExtent->pszFullname);
|
---|
1214 | break;
|
---|
1215 | }
|
---|
1216 | /* The VMDK 1.1 spec seems to talk about compressed grain tables,
|
---|
1217 | * but in reality they are not compressed. */
|
---|
1218 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
1219 | VMDK_SECTOR2BYTE(uRGTStart),
|
---|
1220 | pTmpGT2, cbGTRead, NULL);
|
---|
1221 | if (RT_FAILURE(rc))
|
---|
1222 | {
|
---|
1223 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
|
---|
1224 | N_("VMDK: error reading backup grain table in '%s'"), pExtent->pszFullname);
|
---|
1225 | break;
|
---|
1226 | }
|
---|
1227 | if (memcmp(pTmpGT1, pTmpGT2, cbGTRead))
|
---|
1228 | {
|
---|
1229 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
|
---|
1230 | N_("VMDK: inconsistency between grain table and backup grain table in '%s'"), pExtent->pszFullname);
|
---|
1231 | break;
|
---|
1232 | }
|
---|
1233 | }
|
---|
1234 | } /* while (i < pExtent->cGDEntries) */
|
---|
1235 |
|
---|
1236 | /** @todo figure out what to do for unclean VMDKs. */
|
---|
1237 | if (pTmpGT1)
|
---|
1238 | RTMemFree(pTmpGT1);
|
---|
1239 | if (pTmpGT2)
|
---|
1240 | RTMemFree(pTmpGT2);
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | out:
|
---|
1244 | if (RT_FAILURE(rc))
|
---|
1245 | vmdkFreeGrainDirectory(pExtent);
|
---|
1246 | return rc;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | static int vmdkCreateGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
1250 | uint64_t uStartSector, bool fPreAlloc)
|
---|
1251 | {
|
---|
1252 | int rc = VINF_SUCCESS;
|
---|
1253 | unsigned i;
|
---|
1254 | size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
|
---|
1255 | size_t cbGDRounded = RT_ALIGN_64(cbGD, 512);
|
---|
1256 | size_t cbGTRounded;
|
---|
1257 | uint64_t cbOverhead;
|
---|
1258 |
|
---|
1259 | if (fPreAlloc)
|
---|
1260 | {
|
---|
1261 | cbGTRounded = RT_ALIGN_64(pExtent->cGDEntries * pExtent->cGTEntries * sizeof(uint32_t), 512);
|
---|
1262 | cbOverhead = VMDK_SECTOR2BYTE(uStartSector) + cbGDRounded
|
---|
1263 | + cbGTRounded;
|
---|
1264 | }
|
---|
1265 | else
|
---|
1266 | {
|
---|
1267 | /* Use a dummy start sector for layout computation. */
|
---|
1268 | if (uStartSector == VMDK_GD_AT_END)
|
---|
1269 | uStartSector = 1;
|
---|
1270 | cbGTRounded = 0;
|
---|
1271 | cbOverhead = VMDK_SECTOR2BYTE(uStartSector) + cbGDRounded;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /* For streamOptimized extents there is only one grain directory,
|
---|
1275 | * and for all others take redundant grain directory into account. */
|
---|
1276 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
1277 | {
|
---|
1278 | cbOverhead = RT_ALIGN_64(cbOverhead,
|
---|
1279 | VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
|
---|
1280 | }
|
---|
1281 | else
|
---|
1282 | {
|
---|
1283 | cbOverhead += cbGDRounded + cbGTRounded;
|
---|
1284 | cbOverhead = RT_ALIGN_64(cbOverhead,
|
---|
1285 | VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
|
---|
1286 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pExtent->pFile->pStorage, cbOverhead);
|
---|
1287 | }
|
---|
1288 | if (RT_FAILURE(rc))
|
---|
1289 | goto out;
|
---|
1290 | pExtent->uAppendPosition = cbOverhead;
|
---|
1291 | pExtent->cOverheadSectors = VMDK_BYTE2SECTOR(cbOverhead);
|
---|
1292 |
|
---|
1293 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
1294 | {
|
---|
1295 | pExtent->uSectorRGD = 0;
|
---|
1296 | pExtent->uSectorGD = uStartSector;
|
---|
1297 | }
|
---|
1298 | else
|
---|
1299 | {
|
---|
1300 | pExtent->uSectorRGD = uStartSector;
|
---|
1301 | pExtent->uSectorGD = uStartSector + VMDK_BYTE2SECTOR(cbGDRounded + cbGTRounded);
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | rc = vmdkAllocStreamBuffers(pImage, pExtent);
|
---|
1305 | if (RT_FAILURE(rc))
|
---|
1306 | goto out;
|
---|
1307 |
|
---|
1308 | rc = vmdkAllocGrainDirectory(pImage, pExtent);
|
---|
1309 | if (RT_FAILURE(rc))
|
---|
1310 | goto out;
|
---|
1311 |
|
---|
1312 | if (fPreAlloc)
|
---|
1313 | {
|
---|
1314 | uint32_t uGTSectorLE;
|
---|
1315 | uint64_t uOffsetSectors;
|
---|
1316 |
|
---|
1317 | if (pExtent->pRGD)
|
---|
1318 | {
|
---|
1319 | uOffsetSectors = pExtent->uSectorRGD + VMDK_BYTE2SECTOR(cbGDRounded);
|
---|
1320 | for (i = 0; i < pExtent->cGDEntries; i++)
|
---|
1321 | {
|
---|
1322 | pExtent->pRGD[i] = uOffsetSectors;
|
---|
1323 | uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
|
---|
1324 | /* Write the redundant grain directory entry to disk. */
|
---|
1325 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
1326 | VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + i * sizeof(uGTSectorLE),
|
---|
1327 | &uGTSectorLE, sizeof(uGTSectorLE), NULL);
|
---|
1328 | if (RT_FAILURE(rc))
|
---|
1329 | {
|
---|
1330 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write new redundant grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
1331 | goto out;
|
---|
1332 | }
|
---|
1333 | uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
|
---|
1334 | }
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | uOffsetSectors = pExtent->uSectorGD + VMDK_BYTE2SECTOR(cbGDRounded);
|
---|
1338 | for (i = 0; i < pExtent->cGDEntries; i++)
|
---|
1339 | {
|
---|
1340 | pExtent->pGD[i] = uOffsetSectors;
|
---|
1341 | uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
|
---|
1342 | /* Write the grain directory entry to disk. */
|
---|
1343 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
1344 | VMDK_SECTOR2BYTE(pExtent->uSectorGD) + i * sizeof(uGTSectorLE),
|
---|
1345 | &uGTSectorLE, sizeof(uGTSectorLE), NULL);
|
---|
1346 | if (RT_FAILURE(rc))
|
---|
1347 | {
|
---|
1348 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write new grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
1349 | goto out;
|
---|
1350 | }
|
---|
1351 | uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
|
---|
1352 | }
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | out:
|
---|
1356 | if (RT_FAILURE(rc))
|
---|
1357 | vmdkFreeGrainDirectory(pExtent);
|
---|
1358 | return rc;
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | static int vmdkStringUnquote(PVMDKIMAGE pImage, const char *pszStr,
|
---|
1362 | char **ppszUnquoted, char **ppszNext)
|
---|
1363 | {
|
---|
1364 | char *pszQ;
|
---|
1365 | char *pszUnquoted;
|
---|
1366 |
|
---|
1367 | /* Skip over whitespace. */
|
---|
1368 | while (*pszStr == ' ' || *pszStr == '\t')
|
---|
1369 | pszStr++;
|
---|
1370 |
|
---|
1371 | if (*pszStr != '"')
|
---|
1372 | {
|
---|
1373 | pszQ = (char *)pszStr;
|
---|
1374 | while (*pszQ && *pszQ != ' ' && *pszQ != '\t')
|
---|
1375 | pszQ++;
|
---|
1376 | }
|
---|
1377 | else
|
---|
1378 | {
|
---|
1379 | pszStr++;
|
---|
1380 | pszQ = (char *)strchr(pszStr, '"');
|
---|
1381 | if (pszQ == NULL)
|
---|
1382 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrectly quoted value in descriptor in '%s'"), pImage->pszFilename);
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | pszUnquoted = (char *)RTMemTmpAlloc(pszQ - pszStr + 1);
|
---|
1386 | if (!pszUnquoted)
|
---|
1387 | return VERR_NO_MEMORY;
|
---|
1388 | memcpy(pszUnquoted, pszStr, pszQ - pszStr);
|
---|
1389 | pszUnquoted[pszQ - pszStr] = '\0';
|
---|
1390 | *ppszUnquoted = pszUnquoted;
|
---|
1391 | if (ppszNext)
|
---|
1392 | *ppszNext = pszQ + 1;
|
---|
1393 | return VINF_SUCCESS;
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | static int vmdkDescInitStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1397 | const char *pszLine)
|
---|
1398 | {
|
---|
1399 | char *pEnd = pDescriptor->aLines[pDescriptor->cLines];
|
---|
1400 | ssize_t cbDiff = strlen(pszLine) + 1;
|
---|
1401 |
|
---|
1402 | if ( pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1
|
---|
1403 | && pEnd - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
|
---|
1404 | return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
1405 |
|
---|
1406 | memcpy(pEnd, pszLine, cbDiff);
|
---|
1407 | pDescriptor->cLines++;
|
---|
1408 | pDescriptor->aLines[pDescriptor->cLines] = pEnd + cbDiff;
|
---|
1409 | pDescriptor->fDirty = true;
|
---|
1410 |
|
---|
1411 | return VINF_SUCCESS;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | static bool vmdkDescGetStr(PVMDKDESCRIPTOR pDescriptor, unsigned uStart,
|
---|
1415 | const char *pszKey, const char **ppszValue)
|
---|
1416 | {
|
---|
1417 | size_t cbKey = strlen(pszKey);
|
---|
1418 | const char *pszValue;
|
---|
1419 |
|
---|
1420 | while (uStart != 0)
|
---|
1421 | {
|
---|
1422 | if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
|
---|
1423 | {
|
---|
1424 | /* Key matches, check for a '=' (preceded by whitespace). */
|
---|
1425 | pszValue = pDescriptor->aLines[uStart] + cbKey;
|
---|
1426 | while (*pszValue == ' ' || *pszValue == '\t')
|
---|
1427 | pszValue++;
|
---|
1428 | if (*pszValue == '=')
|
---|
1429 | {
|
---|
1430 | *ppszValue = pszValue + 1;
|
---|
1431 | break;
|
---|
1432 | }
|
---|
1433 | }
|
---|
1434 | uStart = pDescriptor->aNextLines[uStart];
|
---|
1435 | }
|
---|
1436 | return !!uStart;
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | static int vmdkDescSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1440 | unsigned uStart,
|
---|
1441 | const char *pszKey, const char *pszValue)
|
---|
1442 | {
|
---|
1443 | char *pszTmp;
|
---|
1444 | size_t cbKey = strlen(pszKey);
|
---|
1445 | unsigned uLast = 0;
|
---|
1446 |
|
---|
1447 | while (uStart != 0)
|
---|
1448 | {
|
---|
1449 | if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
|
---|
1450 | {
|
---|
1451 | /* Key matches, check for a '=' (preceded by whitespace). */
|
---|
1452 | pszTmp = pDescriptor->aLines[uStart] + cbKey;
|
---|
1453 | while (*pszTmp == ' ' || *pszTmp == '\t')
|
---|
1454 | pszTmp++;
|
---|
1455 | if (*pszTmp == '=')
|
---|
1456 | {
|
---|
1457 | pszTmp++;
|
---|
1458 | while (*pszTmp == ' ' || *pszTmp == '\t')
|
---|
1459 | pszTmp++;
|
---|
1460 | break;
|
---|
1461 | }
|
---|
1462 | }
|
---|
1463 | if (!pDescriptor->aNextLines[uStart])
|
---|
1464 | uLast = uStart;
|
---|
1465 | uStart = pDescriptor->aNextLines[uStart];
|
---|
1466 | }
|
---|
1467 | if (uStart)
|
---|
1468 | {
|
---|
1469 | if (pszValue)
|
---|
1470 | {
|
---|
1471 | /* Key already exists, replace existing value. */
|
---|
1472 | size_t cbOldVal = strlen(pszTmp);
|
---|
1473 | size_t cbNewVal = strlen(pszValue);
|
---|
1474 | ssize_t cbDiff = cbNewVal - cbOldVal;
|
---|
1475 | /* Check for buffer overflow. */
|
---|
1476 | if ( pDescriptor->aLines[pDescriptor->cLines]
|
---|
1477 | - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
|
---|
1478 | return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
1479 |
|
---|
1480 | memmove(pszTmp + cbNewVal, pszTmp + cbOldVal,
|
---|
1481 | pDescriptor->aLines[pDescriptor->cLines] - pszTmp - cbOldVal);
|
---|
1482 | memcpy(pszTmp, pszValue, cbNewVal + 1);
|
---|
1483 | for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
|
---|
1484 | pDescriptor->aLines[i] += cbDiff;
|
---|
1485 | }
|
---|
1486 | else
|
---|
1487 | {
|
---|
1488 | memmove(pDescriptor->aLines[uStart], pDescriptor->aLines[uStart+1],
|
---|
1489 | pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uStart+1] + 1);
|
---|
1490 | for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
|
---|
1491 | {
|
---|
1492 | pDescriptor->aLines[i-1] = pDescriptor->aLines[i];
|
---|
1493 | if (pDescriptor->aNextLines[i])
|
---|
1494 | pDescriptor->aNextLines[i-1] = pDescriptor->aNextLines[i] - 1;
|
---|
1495 | else
|
---|
1496 | pDescriptor->aNextLines[i-1] = 0;
|
---|
1497 | }
|
---|
1498 | pDescriptor->cLines--;
|
---|
1499 | /* Adjust starting line numbers of following descriptor sections. */
|
---|
1500 | if (uStart < pDescriptor->uFirstExtent)
|
---|
1501 | pDescriptor->uFirstExtent--;
|
---|
1502 | if (uStart < pDescriptor->uFirstDDB)
|
---|
1503 | pDescriptor->uFirstDDB--;
|
---|
1504 | }
|
---|
1505 | }
|
---|
1506 | else
|
---|
1507 | {
|
---|
1508 | /* Key doesn't exist, append after the last entry in this category. */
|
---|
1509 | if (!pszValue)
|
---|
1510 | {
|
---|
1511 | /* Key doesn't exist, and it should be removed. Simply a no-op. */
|
---|
1512 | return VINF_SUCCESS;
|
---|
1513 | }
|
---|
1514 | cbKey = strlen(pszKey);
|
---|
1515 | size_t cbValue = strlen(pszValue);
|
---|
1516 | ssize_t cbDiff = cbKey + 1 + cbValue + 1;
|
---|
1517 | /* Check for buffer overflow. */
|
---|
1518 | if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
|
---|
1519 | || ( pDescriptor->aLines[pDescriptor->cLines]
|
---|
1520 | - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
|
---|
1521 | return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
1522 | for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
|
---|
1523 | {
|
---|
1524 | pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
|
---|
1525 | if (pDescriptor->aNextLines[i - 1])
|
---|
1526 | pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
|
---|
1527 | else
|
---|
1528 | pDescriptor->aNextLines[i] = 0;
|
---|
1529 | }
|
---|
1530 | uStart = uLast + 1;
|
---|
1531 | pDescriptor->aNextLines[uLast] = uStart;
|
---|
1532 | pDescriptor->aNextLines[uStart] = 0;
|
---|
1533 | pDescriptor->cLines++;
|
---|
1534 | pszTmp = pDescriptor->aLines[uStart];
|
---|
1535 | memmove(pszTmp + cbDiff, pszTmp,
|
---|
1536 | pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
|
---|
1537 | memcpy(pDescriptor->aLines[uStart], pszKey, cbKey);
|
---|
1538 | pDescriptor->aLines[uStart][cbKey] = '=';
|
---|
1539 | memcpy(pDescriptor->aLines[uStart] + cbKey + 1, pszValue, cbValue + 1);
|
---|
1540 | for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
|
---|
1541 | pDescriptor->aLines[i] += cbDiff;
|
---|
1542 |
|
---|
1543 | /* Adjust starting line numbers of following descriptor sections. */
|
---|
1544 | if (uStart <= pDescriptor->uFirstExtent)
|
---|
1545 | pDescriptor->uFirstExtent++;
|
---|
1546 | if (uStart <= pDescriptor->uFirstDDB)
|
---|
1547 | pDescriptor->uFirstDDB++;
|
---|
1548 | }
|
---|
1549 | pDescriptor->fDirty = true;
|
---|
1550 | return VINF_SUCCESS;
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | static int vmdkDescBaseGetU32(PVMDKDESCRIPTOR pDescriptor, const char *pszKey,
|
---|
1554 | uint32_t *puValue)
|
---|
1555 | {
|
---|
1556 | const char *pszValue;
|
---|
1557 |
|
---|
1558 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey,
|
---|
1559 | &pszValue))
|
---|
1560 | return VERR_VD_VMDK_VALUE_NOT_FOUND;
|
---|
1561 | return RTStrToUInt32Ex(pszValue, NULL, 10, puValue);
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | static int vmdkDescBaseGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1565 | const char *pszKey, const char **ppszValue)
|
---|
1566 | {
|
---|
1567 | const char *pszValue;
|
---|
1568 | char *pszValueUnquoted;
|
---|
1569 |
|
---|
1570 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey,
|
---|
1571 | &pszValue))
|
---|
1572 | return VERR_VD_VMDK_VALUE_NOT_FOUND;
|
---|
1573 | int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
|
---|
1574 | if (RT_FAILURE(rc))
|
---|
1575 | return rc;
|
---|
1576 | *ppszValue = pszValueUnquoted;
|
---|
1577 | return rc;
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | static int vmdkDescBaseSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1581 | const char *pszKey, const char *pszValue)
|
---|
1582 | {
|
---|
1583 | char *pszValueQuoted;
|
---|
1584 |
|
---|
1585 | RTStrAPrintf(&pszValueQuoted, "\"%s\"", pszValue);
|
---|
1586 | if (!pszValueQuoted)
|
---|
1587 | return VERR_NO_STR_MEMORY;
|
---|
1588 | int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc, pszKey,
|
---|
1589 | pszValueQuoted);
|
---|
1590 | RTStrFree(pszValueQuoted);
|
---|
1591 | return rc;
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | static void vmdkDescExtRemoveDummy(PVMDKIMAGE pImage,
|
---|
1595 | PVMDKDESCRIPTOR pDescriptor)
|
---|
1596 | {
|
---|
1597 | unsigned uEntry = pDescriptor->uFirstExtent;
|
---|
1598 | ssize_t cbDiff;
|
---|
1599 |
|
---|
1600 | if (!uEntry)
|
---|
1601 | return;
|
---|
1602 |
|
---|
1603 | cbDiff = strlen(pDescriptor->aLines[uEntry]) + 1;
|
---|
1604 | /* Move everything including \0 in the entry marking the end of buffer. */
|
---|
1605 | memmove(pDescriptor->aLines[uEntry], pDescriptor->aLines[uEntry + 1],
|
---|
1606 | pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uEntry + 1] + 1);
|
---|
1607 | for (unsigned i = uEntry + 1; i <= pDescriptor->cLines; i++)
|
---|
1608 | {
|
---|
1609 | pDescriptor->aLines[i - 1] = pDescriptor->aLines[i] - cbDiff;
|
---|
1610 | if (pDescriptor->aNextLines[i])
|
---|
1611 | pDescriptor->aNextLines[i - 1] = pDescriptor->aNextLines[i] - 1;
|
---|
1612 | else
|
---|
1613 | pDescriptor->aNextLines[i - 1] = 0;
|
---|
1614 | }
|
---|
1615 | pDescriptor->cLines--;
|
---|
1616 | if (pDescriptor->uFirstDDB)
|
---|
1617 | pDescriptor->uFirstDDB--;
|
---|
1618 |
|
---|
1619 | return;
|
---|
1620 | }
|
---|
1621 |
|
---|
1622 | static int vmdkDescExtInsert(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1623 | VMDKACCESS enmAccess, uint64_t cNominalSectors,
|
---|
1624 | VMDKETYPE enmType, const char *pszBasename,
|
---|
1625 | uint64_t uSectorOffset)
|
---|
1626 | {
|
---|
1627 | static const char *apszAccess[] = { "NOACCESS", "RDONLY", "RW" };
|
---|
1628 | static const char *apszType[] = { "", "SPARSE", "FLAT", "ZERO", "VMFS" };
|
---|
1629 | char *pszTmp;
|
---|
1630 | unsigned uStart = pDescriptor->uFirstExtent, uLast = 0;
|
---|
1631 | char szExt[1024];
|
---|
1632 | ssize_t cbDiff;
|
---|
1633 |
|
---|
1634 | Assert((unsigned)enmAccess < RT_ELEMENTS(apszAccess));
|
---|
1635 | Assert((unsigned)enmType < RT_ELEMENTS(apszType));
|
---|
1636 |
|
---|
1637 | /* Find last entry in extent description. */
|
---|
1638 | while (uStart)
|
---|
1639 | {
|
---|
1640 | if (!pDescriptor->aNextLines[uStart])
|
---|
1641 | uLast = uStart;
|
---|
1642 | uStart = pDescriptor->aNextLines[uStart];
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | if (enmType == VMDKETYPE_ZERO)
|
---|
1646 | {
|
---|
1647 | RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s ", apszAccess[enmAccess],
|
---|
1648 | cNominalSectors, apszType[enmType]);
|
---|
1649 | }
|
---|
1650 | else if (enmType == VMDKETYPE_FLAT)
|
---|
1651 | {
|
---|
1652 | RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\" %llu",
|
---|
1653 | apszAccess[enmAccess], cNominalSectors,
|
---|
1654 | apszType[enmType], pszBasename, uSectorOffset);
|
---|
1655 | }
|
---|
1656 | else
|
---|
1657 | {
|
---|
1658 | RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\"",
|
---|
1659 | apszAccess[enmAccess], cNominalSectors,
|
---|
1660 | apszType[enmType], pszBasename);
|
---|
1661 | }
|
---|
1662 | cbDiff = strlen(szExt) + 1;
|
---|
1663 |
|
---|
1664 | /* Check for buffer overflow. */
|
---|
1665 | if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
|
---|
1666 | || ( pDescriptor->aLines[pDescriptor->cLines]
|
---|
1667 | - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
|
---|
1668 | return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
1669 |
|
---|
1670 | for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
|
---|
1671 | {
|
---|
1672 | pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
|
---|
1673 | if (pDescriptor->aNextLines[i - 1])
|
---|
1674 | pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
|
---|
1675 | else
|
---|
1676 | pDescriptor->aNextLines[i] = 0;
|
---|
1677 | }
|
---|
1678 | uStart = uLast + 1;
|
---|
1679 | pDescriptor->aNextLines[uLast] = uStart;
|
---|
1680 | pDescriptor->aNextLines[uStart] = 0;
|
---|
1681 | pDescriptor->cLines++;
|
---|
1682 | pszTmp = pDescriptor->aLines[uStart];
|
---|
1683 | memmove(pszTmp + cbDiff, pszTmp,
|
---|
1684 | pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
|
---|
1685 | memcpy(pDescriptor->aLines[uStart], szExt, cbDiff);
|
---|
1686 | for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
|
---|
1687 | pDescriptor->aLines[i] += cbDiff;
|
---|
1688 |
|
---|
1689 | /* Adjust starting line numbers of following descriptor sections. */
|
---|
1690 | if (uStart <= pDescriptor->uFirstDDB)
|
---|
1691 | pDescriptor->uFirstDDB++;
|
---|
1692 |
|
---|
1693 | pDescriptor->fDirty = true;
|
---|
1694 | return VINF_SUCCESS;
|
---|
1695 | }
|
---|
1696 |
|
---|
1697 | static int vmdkDescDDBGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1698 | const char *pszKey, const char **ppszValue)
|
---|
1699 | {
|
---|
1700 | const char *pszValue;
|
---|
1701 | char *pszValueUnquoted;
|
---|
1702 |
|
---|
1703 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
|
---|
1704 | &pszValue))
|
---|
1705 | return VERR_VD_VMDK_VALUE_NOT_FOUND;
|
---|
1706 | int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
|
---|
1707 | if (RT_FAILURE(rc))
|
---|
1708 | return rc;
|
---|
1709 | *ppszValue = pszValueUnquoted;
|
---|
1710 | return rc;
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | static int vmdkDescDDBGetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1714 | const char *pszKey, uint32_t *puValue)
|
---|
1715 | {
|
---|
1716 | const char *pszValue;
|
---|
1717 | char *pszValueUnquoted;
|
---|
1718 |
|
---|
1719 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
|
---|
1720 | &pszValue))
|
---|
1721 | return VERR_VD_VMDK_VALUE_NOT_FOUND;
|
---|
1722 | int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
|
---|
1723 | if (RT_FAILURE(rc))
|
---|
1724 | return rc;
|
---|
1725 | rc = RTStrToUInt32Ex(pszValueUnquoted, NULL, 10, puValue);
|
---|
1726 | RTMemTmpFree(pszValueUnquoted);
|
---|
1727 | return rc;
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | static int vmdkDescDDBGetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1731 | const char *pszKey, PRTUUID pUuid)
|
---|
1732 | {
|
---|
1733 | const char *pszValue;
|
---|
1734 | char *pszValueUnquoted;
|
---|
1735 |
|
---|
1736 | if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
|
---|
1737 | &pszValue))
|
---|
1738 | return VERR_VD_VMDK_VALUE_NOT_FOUND;
|
---|
1739 | int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
|
---|
1740 | if (RT_FAILURE(rc))
|
---|
1741 | return rc;
|
---|
1742 | rc = RTUuidFromStr(pUuid, pszValueUnquoted);
|
---|
1743 | RTMemTmpFree(pszValueUnquoted);
|
---|
1744 | return rc;
|
---|
1745 | }
|
---|
1746 |
|
---|
1747 | static int vmdkDescDDBSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1748 | const char *pszKey, const char *pszVal)
|
---|
1749 | {
|
---|
1750 | int rc;
|
---|
1751 | char *pszValQuoted;
|
---|
1752 |
|
---|
1753 | if (pszVal)
|
---|
1754 | {
|
---|
1755 | RTStrAPrintf(&pszValQuoted, "\"%s\"", pszVal);
|
---|
1756 | if (!pszValQuoted)
|
---|
1757 | return VERR_NO_STR_MEMORY;
|
---|
1758 | }
|
---|
1759 | else
|
---|
1760 | pszValQuoted = NULL;
|
---|
1761 | rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
|
---|
1762 | pszValQuoted);
|
---|
1763 | if (pszValQuoted)
|
---|
1764 | RTStrFree(pszValQuoted);
|
---|
1765 | return rc;
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | static int vmdkDescDDBSetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1769 | const char *pszKey, PCRTUUID pUuid)
|
---|
1770 | {
|
---|
1771 | char *pszUuid;
|
---|
1772 |
|
---|
1773 | RTStrAPrintf(&pszUuid, "\"%RTuuid\"", pUuid);
|
---|
1774 | if (!pszUuid)
|
---|
1775 | return VERR_NO_STR_MEMORY;
|
---|
1776 | int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
|
---|
1777 | pszUuid);
|
---|
1778 | RTStrFree(pszUuid);
|
---|
1779 | return rc;
|
---|
1780 | }
|
---|
1781 |
|
---|
1782 | static int vmdkDescDDBSetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
|
---|
1783 | const char *pszKey, uint32_t uValue)
|
---|
1784 | {
|
---|
1785 | char *pszValue;
|
---|
1786 |
|
---|
1787 | RTStrAPrintf(&pszValue, "\"%d\"", uValue);
|
---|
1788 | if (!pszValue)
|
---|
1789 | return VERR_NO_STR_MEMORY;
|
---|
1790 | int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
|
---|
1791 | pszValue);
|
---|
1792 | RTStrFree(pszValue);
|
---|
1793 | return rc;
|
---|
1794 | }
|
---|
1795 |
|
---|
1796 | static int vmdkPreprocessDescriptor(PVMDKIMAGE pImage, char *pDescData,
|
---|
1797 | size_t cbDescData,
|
---|
1798 | PVMDKDESCRIPTOR pDescriptor)
|
---|
1799 | {
|
---|
1800 | int rc = VINF_SUCCESS;
|
---|
1801 | unsigned cLine = 0, uLastNonEmptyLine = 0;
|
---|
1802 | char *pTmp = pDescData;
|
---|
1803 |
|
---|
1804 | pDescriptor->cbDescAlloc = cbDescData;
|
---|
1805 | while (*pTmp != '\0')
|
---|
1806 | {
|
---|
1807 | pDescriptor->aLines[cLine++] = pTmp;
|
---|
1808 | if (cLine >= VMDK_DESCRIPTOR_LINES_MAX)
|
---|
1809 | {
|
---|
1810 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
|
---|
1811 | goto out;
|
---|
1812 | }
|
---|
1813 |
|
---|
1814 | while (*pTmp != '\0' && *pTmp != '\n')
|
---|
1815 | {
|
---|
1816 | if (*pTmp == '\r')
|
---|
1817 | {
|
---|
1818 | if (*(pTmp + 1) != '\n')
|
---|
1819 | {
|
---|
1820 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: unsupported end of line in descriptor in '%s'"), pImage->pszFilename);
|
---|
1821 | goto out;
|
---|
1822 | }
|
---|
1823 | else
|
---|
1824 | {
|
---|
1825 | /* Get rid of CR character. */
|
---|
1826 | *pTmp = '\0';
|
---|
1827 | }
|
---|
1828 | }
|
---|
1829 | pTmp++;
|
---|
1830 | }
|
---|
1831 | /* Get rid of LF character. */
|
---|
1832 | if (*pTmp == '\n')
|
---|
1833 | {
|
---|
1834 | *pTmp = '\0';
|
---|
1835 | pTmp++;
|
---|
1836 | }
|
---|
1837 | }
|
---|
1838 | pDescriptor->cLines = cLine;
|
---|
1839 | /* Pointer right after the end of the used part of the buffer. */
|
---|
1840 | pDescriptor->aLines[cLine] = pTmp;
|
---|
1841 |
|
---|
1842 | if ( strcmp(pDescriptor->aLines[0], "# Disk DescriptorFile")
|
---|
1843 | && strcmp(pDescriptor->aLines[0], "# Disk Descriptor File"))
|
---|
1844 | {
|
---|
1845 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor does not start as expected in '%s'"), pImage->pszFilename);
|
---|
1846 | goto out;
|
---|
1847 | }
|
---|
1848 |
|
---|
1849 | /* Initialize those, because we need to be able to reopen an image. */
|
---|
1850 | pDescriptor->uFirstDesc = 0;
|
---|
1851 | pDescriptor->uFirstExtent = 0;
|
---|
1852 | pDescriptor->uFirstDDB = 0;
|
---|
1853 | for (unsigned i = 0; i < cLine; i++)
|
---|
1854 | {
|
---|
1855 | if (*pDescriptor->aLines[i] != '#' && *pDescriptor->aLines[i] != '\0')
|
---|
1856 | {
|
---|
1857 | if ( !strncmp(pDescriptor->aLines[i], "RW", 2)
|
---|
1858 | || !strncmp(pDescriptor->aLines[i], "RDONLY", 6)
|
---|
1859 | || !strncmp(pDescriptor->aLines[i], "NOACCESS", 8) )
|
---|
1860 | {
|
---|
1861 | /* An extent descriptor. */
|
---|
1862 | if (!pDescriptor->uFirstDesc || pDescriptor->uFirstDDB)
|
---|
1863 | {
|
---|
1864 | /* Incorrect ordering of entries. */
|
---|
1865 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
|
---|
1866 | goto out;
|
---|
1867 | }
|
---|
1868 | if (!pDescriptor->uFirstExtent)
|
---|
1869 | {
|
---|
1870 | pDescriptor->uFirstExtent = i;
|
---|
1871 | uLastNonEmptyLine = 0;
|
---|
1872 | }
|
---|
1873 | }
|
---|
1874 | else if (!strncmp(pDescriptor->aLines[i], "ddb.", 4))
|
---|
1875 | {
|
---|
1876 | /* A disk database entry. */
|
---|
1877 | if (!pDescriptor->uFirstDesc || !pDescriptor->uFirstExtent)
|
---|
1878 | {
|
---|
1879 | /* Incorrect ordering of entries. */
|
---|
1880 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
|
---|
1881 | goto out;
|
---|
1882 | }
|
---|
1883 | if (!pDescriptor->uFirstDDB)
|
---|
1884 | {
|
---|
1885 | pDescriptor->uFirstDDB = i;
|
---|
1886 | uLastNonEmptyLine = 0;
|
---|
1887 | }
|
---|
1888 | }
|
---|
1889 | else
|
---|
1890 | {
|
---|
1891 | /* A normal entry. */
|
---|
1892 | if (pDescriptor->uFirstExtent || pDescriptor->uFirstDDB)
|
---|
1893 | {
|
---|
1894 | /* Incorrect ordering of entries. */
|
---|
1895 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
|
---|
1896 | goto out;
|
---|
1897 | }
|
---|
1898 | if (!pDescriptor->uFirstDesc)
|
---|
1899 | {
|
---|
1900 | pDescriptor->uFirstDesc = i;
|
---|
1901 | uLastNonEmptyLine = 0;
|
---|
1902 | }
|
---|
1903 | }
|
---|
1904 | if (uLastNonEmptyLine)
|
---|
1905 | pDescriptor->aNextLines[uLastNonEmptyLine] = i;
|
---|
1906 | uLastNonEmptyLine = i;
|
---|
1907 | }
|
---|
1908 | }
|
---|
1909 |
|
---|
1910 | out:
|
---|
1911 | return rc;
|
---|
1912 | }
|
---|
1913 |
|
---|
1914 | static int vmdkDescSetPCHSGeometry(PVMDKIMAGE pImage,
|
---|
1915 | PCVDGEOMETRY pPCHSGeometry)
|
---|
1916 | {
|
---|
1917 | int rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
1918 | VMDK_DDB_GEO_PCHS_CYLINDERS,
|
---|
1919 | pPCHSGeometry->cCylinders);
|
---|
1920 | if (RT_FAILURE(rc))
|
---|
1921 | return rc;
|
---|
1922 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
1923 | VMDK_DDB_GEO_PCHS_HEADS,
|
---|
1924 | pPCHSGeometry->cHeads);
|
---|
1925 | if (RT_FAILURE(rc))
|
---|
1926 | return rc;
|
---|
1927 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
1928 | VMDK_DDB_GEO_PCHS_SECTORS,
|
---|
1929 | pPCHSGeometry->cSectors);
|
---|
1930 | return rc;
|
---|
1931 | }
|
---|
1932 |
|
---|
1933 | static int vmdkDescSetLCHSGeometry(PVMDKIMAGE pImage,
|
---|
1934 | PCVDGEOMETRY pLCHSGeometry)
|
---|
1935 | {
|
---|
1936 | int rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
1937 | VMDK_DDB_GEO_LCHS_CYLINDERS,
|
---|
1938 | pLCHSGeometry->cCylinders);
|
---|
1939 | if (RT_FAILURE(rc))
|
---|
1940 | return rc;
|
---|
1941 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
1942 | VMDK_DDB_GEO_LCHS_HEADS,
|
---|
1943 |
|
---|
1944 | pLCHSGeometry->cHeads);
|
---|
1945 | if (RT_FAILURE(rc))
|
---|
1946 | return rc;
|
---|
1947 | rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
|
---|
1948 | VMDK_DDB_GEO_LCHS_SECTORS,
|
---|
1949 | pLCHSGeometry->cSectors);
|
---|
1950 | return rc;
|
---|
1951 | }
|
---|
1952 |
|
---|
1953 | static int vmdkCreateDescriptor(PVMDKIMAGE pImage, char *pDescData,
|
---|
1954 | size_t cbDescData, PVMDKDESCRIPTOR pDescriptor)
|
---|
1955 | {
|
---|
1956 | int rc;
|
---|
1957 |
|
---|
1958 | pDescriptor->uFirstDesc = 0;
|
---|
1959 | pDescriptor->uFirstExtent = 0;
|
---|
1960 | pDescriptor->uFirstDDB = 0;
|
---|
1961 | pDescriptor->cLines = 0;
|
---|
1962 | pDescriptor->cbDescAlloc = cbDescData;
|
---|
1963 | pDescriptor->fDirty = false;
|
---|
1964 | pDescriptor->aLines[pDescriptor->cLines] = pDescData;
|
---|
1965 | memset(pDescriptor->aNextLines, '\0', sizeof(pDescriptor->aNextLines));
|
---|
1966 |
|
---|
1967 | rc = vmdkDescInitStr(pImage, pDescriptor, "# Disk DescriptorFile");
|
---|
1968 | if (RT_FAILURE(rc))
|
---|
1969 | goto out;
|
---|
1970 | rc = vmdkDescInitStr(pImage, pDescriptor, "version=1");
|
---|
1971 | if (RT_FAILURE(rc))
|
---|
1972 | goto out;
|
---|
1973 | pDescriptor->uFirstDesc = pDescriptor->cLines - 1;
|
---|
1974 | rc = vmdkDescInitStr(pImage, pDescriptor, "");
|
---|
1975 | if (RT_FAILURE(rc))
|
---|
1976 | goto out;
|
---|
1977 | rc = vmdkDescInitStr(pImage, pDescriptor, "# Extent description");
|
---|
1978 | if (RT_FAILURE(rc))
|
---|
1979 | goto out;
|
---|
1980 | rc = vmdkDescInitStr(pImage, pDescriptor, "NOACCESS 0 ZERO ");
|
---|
1981 | if (RT_FAILURE(rc))
|
---|
1982 | goto out;
|
---|
1983 | pDescriptor->uFirstExtent = pDescriptor->cLines - 1;
|
---|
1984 | rc = vmdkDescInitStr(pImage, pDescriptor, "");
|
---|
1985 | if (RT_FAILURE(rc))
|
---|
1986 | goto out;
|
---|
1987 | /* The trailing space is created by VMware, too. */
|
---|
1988 | rc = vmdkDescInitStr(pImage, pDescriptor, "# The disk Data Base ");
|
---|
1989 | if (RT_FAILURE(rc))
|
---|
1990 | goto out;
|
---|
1991 | rc = vmdkDescInitStr(pImage, pDescriptor, "#DDB");
|
---|
1992 | if (RT_FAILURE(rc))
|
---|
1993 | goto out;
|
---|
1994 | rc = vmdkDescInitStr(pImage, pDescriptor, "");
|
---|
1995 | if (RT_FAILURE(rc))
|
---|
1996 | goto out;
|
---|
1997 | rc = vmdkDescInitStr(pImage, pDescriptor, "ddb.virtualHWVersion = \"4\"");
|
---|
1998 | if (RT_FAILURE(rc))
|
---|
1999 | goto out;
|
---|
2000 | pDescriptor->uFirstDDB = pDescriptor->cLines - 1;
|
---|
2001 |
|
---|
2002 | /* Now that the framework is in place, use the normal functions to insert
|
---|
2003 | * the remaining keys. */
|
---|
2004 | char szBuf[9];
|
---|
2005 | RTStrPrintf(szBuf, sizeof(szBuf), "%08x", RTRandU32());
|
---|
2006 | rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc,
|
---|
2007 | "CID", szBuf);
|
---|
2008 | if (RT_FAILURE(rc))
|
---|
2009 | goto out;
|
---|
2010 | rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc,
|
---|
2011 | "parentCID", "ffffffff");
|
---|
2012 | if (RT_FAILURE(rc))
|
---|
2013 | goto out;
|
---|
2014 |
|
---|
2015 | rc = vmdkDescDDBSetStr(pImage, pDescriptor, "ddb.adapterType", "ide");
|
---|
2016 | if (RT_FAILURE(rc))
|
---|
2017 | goto out;
|
---|
2018 |
|
---|
2019 | out:
|
---|
2020 | return rc;
|
---|
2021 | }
|
---|
2022 |
|
---|
2023 | static int vmdkParseDescriptor(PVMDKIMAGE pImage, char *pDescData,
|
---|
2024 | size_t cbDescData)
|
---|
2025 | {
|
---|
2026 | int rc;
|
---|
2027 | unsigned cExtents;
|
---|
2028 | unsigned uLine;
|
---|
2029 | unsigned i;
|
---|
2030 |
|
---|
2031 | rc = vmdkPreprocessDescriptor(pImage, pDescData, cbDescData,
|
---|
2032 | &pImage->Descriptor);
|
---|
2033 | if (RT_FAILURE(rc))
|
---|
2034 | return rc;
|
---|
2035 |
|
---|
2036 | /* Check version, must be 1. */
|
---|
2037 | uint32_t uVersion;
|
---|
2038 | rc = vmdkDescBaseGetU32(&pImage->Descriptor, "version", &uVersion);
|
---|
2039 | if (RT_FAILURE(rc))
|
---|
2040 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error finding key 'version' in descriptor in '%s'"), pImage->pszFilename);
|
---|
2041 | if (uVersion != 1)
|
---|
2042 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VMDK: unsupported format version in descriptor in '%s'"), pImage->pszFilename);
|
---|
2043 |
|
---|
2044 | /* Get image creation type and determine image flags. */
|
---|
2045 | const char *pszCreateType = NULL; /* initialized to make gcc shut up */
|
---|
2046 | rc = vmdkDescBaseGetStr(pImage, &pImage->Descriptor, "createType",
|
---|
2047 | &pszCreateType);
|
---|
2048 | if (RT_FAILURE(rc))
|
---|
2049 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot get image type from descriptor in '%s'"), pImage->pszFilename);
|
---|
2050 | if ( !strcmp(pszCreateType, "twoGbMaxExtentSparse")
|
---|
2051 | || !strcmp(pszCreateType, "twoGbMaxExtentFlat"))
|
---|
2052 | pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_SPLIT_2G;
|
---|
2053 | else if ( !strcmp(pszCreateType, "partitionedDevice")
|
---|
2054 | || !strcmp(pszCreateType, "fullDevice"))
|
---|
2055 | pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_RAWDISK;
|
---|
2056 | else if (!strcmp(pszCreateType, "streamOptimized"))
|
---|
2057 | pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED;
|
---|
2058 | else if (!strcmp(pszCreateType, "vmfs"))
|
---|
2059 | pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED | VD_VMDK_IMAGE_FLAGS_ESX;
|
---|
2060 | RTStrFree((char *)(void *)pszCreateType);
|
---|
2061 |
|
---|
2062 | /* Count the number of extent config entries. */
|
---|
2063 | for (uLine = pImage->Descriptor.uFirstExtent, cExtents = 0;
|
---|
2064 | uLine != 0;
|
---|
2065 | uLine = pImage->Descriptor.aNextLines[uLine], cExtents++)
|
---|
2066 | /* nothing */;
|
---|
2067 |
|
---|
2068 | if (!pImage->pDescData && cExtents != 1)
|
---|
2069 | {
|
---|
2070 | /* Monolithic image, must have only one extent (already opened). */
|
---|
2071 | 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);
|
---|
2072 | }
|
---|
2073 |
|
---|
2074 | if (pImage->pDescData)
|
---|
2075 | {
|
---|
2076 | /* Non-monolithic image, extents need to be allocated. */
|
---|
2077 | rc = vmdkCreateExtents(pImage, cExtents);
|
---|
2078 | if (RT_FAILURE(rc))
|
---|
2079 | return rc;
|
---|
2080 | }
|
---|
2081 |
|
---|
2082 | for (i = 0, uLine = pImage->Descriptor.uFirstExtent;
|
---|
2083 | i < cExtents; i++, uLine = pImage->Descriptor.aNextLines[uLine])
|
---|
2084 | {
|
---|
2085 | char *pszLine = pImage->Descriptor.aLines[uLine];
|
---|
2086 |
|
---|
2087 | /* Access type of the extent. */
|
---|
2088 | if (!strncmp(pszLine, "RW", 2))
|
---|
2089 | {
|
---|
2090 | pImage->pExtents[i].enmAccess = VMDKACCESS_READWRITE;
|
---|
2091 | pszLine += 2;
|
---|
2092 | }
|
---|
2093 | else if (!strncmp(pszLine, "RDONLY", 6))
|
---|
2094 | {
|
---|
2095 | pImage->pExtents[i].enmAccess = VMDKACCESS_READONLY;
|
---|
2096 | pszLine += 6;
|
---|
2097 | }
|
---|
2098 | else if (!strncmp(pszLine, "NOACCESS", 8))
|
---|
2099 | {
|
---|
2100 | pImage->pExtents[i].enmAccess = VMDKACCESS_NOACCESS;
|
---|
2101 | pszLine += 8;
|
---|
2102 | }
|
---|
2103 | else
|
---|
2104 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
2105 | if (*pszLine++ != ' ')
|
---|
2106 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
2107 |
|
---|
2108 | /* Nominal size of the extent. */
|
---|
2109 | rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
|
---|
2110 | &pImage->pExtents[i].cNominalSectors);
|
---|
2111 | if (RT_FAILURE(rc))
|
---|
2112 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
2113 | if (*pszLine++ != ' ')
|
---|
2114 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
2115 |
|
---|
2116 | /* Type of the extent. */
|
---|
2117 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
2118 | /** @todo Add the ESX extent types. Not necessary for now because
|
---|
2119 | * the ESX extent types are only used inside an ESX server. They are
|
---|
2120 | * automatically converted if the VMDK is exported. */
|
---|
2121 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
2122 | if (!strncmp(pszLine, "SPARSE", 6))
|
---|
2123 | {
|
---|
2124 | pImage->pExtents[i].enmType = VMDKETYPE_HOSTED_SPARSE;
|
---|
2125 | pszLine += 6;
|
---|
2126 | }
|
---|
2127 | else if (!strncmp(pszLine, "FLAT", 4))
|
---|
2128 | {
|
---|
2129 | pImage->pExtents[i].enmType = VMDKETYPE_FLAT;
|
---|
2130 | pszLine += 4;
|
---|
2131 | }
|
---|
2132 | else if (!strncmp(pszLine, "ZERO", 4))
|
---|
2133 | {
|
---|
2134 | pImage->pExtents[i].enmType = VMDKETYPE_ZERO;
|
---|
2135 | pszLine += 4;
|
---|
2136 | }
|
---|
2137 | else if (!strncmp(pszLine, "VMFS", 4))
|
---|
2138 | {
|
---|
2139 | pImage->pExtents[i].enmType = VMDKETYPE_VMFS;
|
---|
2140 | pszLine += 4;
|
---|
2141 | }
|
---|
2142 | else
|
---|
2143 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
2144 |
|
---|
2145 | if (pImage->pExtents[i].enmType == VMDKETYPE_ZERO)
|
---|
2146 | {
|
---|
2147 | /* This one has no basename or offset. */
|
---|
2148 | if (*pszLine == ' ')
|
---|
2149 | pszLine++;
|
---|
2150 | if (*pszLine != '\0')
|
---|
2151 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
2152 | pImage->pExtents[i].pszBasename = NULL;
|
---|
2153 | }
|
---|
2154 | else
|
---|
2155 | {
|
---|
2156 | /* All other extent types have basename and optional offset. */
|
---|
2157 | if (*pszLine++ != ' ')
|
---|
2158 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
2159 |
|
---|
2160 | /* Basename of the image. Surrounded by quotes. */
|
---|
2161 | char *pszBasename;
|
---|
2162 | rc = vmdkStringUnquote(pImage, pszLine, &pszBasename, &pszLine);
|
---|
2163 | if (RT_FAILURE(rc))
|
---|
2164 | return rc;
|
---|
2165 | pImage->pExtents[i].pszBasename = pszBasename;
|
---|
2166 | if (*pszLine == ' ')
|
---|
2167 | {
|
---|
2168 | pszLine++;
|
---|
2169 | if (*pszLine != '\0')
|
---|
2170 | {
|
---|
2171 | /* Optional offset in extent specified. */
|
---|
2172 | rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
|
---|
2173 | &pImage->pExtents[i].uSectorOffset);
|
---|
2174 | if (RT_FAILURE(rc))
|
---|
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 | }
|
---|
2177 | }
|
---|
2178 |
|
---|
2179 | if (*pszLine != '\0')
|
---|
2180 | return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
|
---|
2181 | }
|
---|
2182 | }
|
---|
2183 |
|
---|
2184 | /* Determine PCHS geometry (autogenerate if necessary). */
|
---|
2185 | rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
|
---|
2186 | VMDK_DDB_GEO_PCHS_CYLINDERS,
|
---|
2187 | &pImage->PCHSGeometry.cCylinders);
|
---|
2188 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2189 | pImage->PCHSGeometry.cCylinders = 0;
|
---|
2190 | else if (RT_FAILURE(rc))
|
---|
2191 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
|
---|
2192 | rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
|
---|
2193 | VMDK_DDB_GEO_PCHS_HEADS,
|
---|
2194 | &pImage->PCHSGeometry.cHeads);
|
---|
2195 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2196 | pImage->PCHSGeometry.cHeads = 0;
|
---|
2197 | else if (RT_FAILURE(rc))
|
---|
2198 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
|
---|
2199 | rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
|
---|
2200 | VMDK_DDB_GEO_PCHS_SECTORS,
|
---|
2201 | &pImage->PCHSGeometry.cSectors);
|
---|
2202 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2203 | pImage->PCHSGeometry.cSectors = 0;
|
---|
2204 | else if (RT_FAILURE(rc))
|
---|
2205 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
|
---|
2206 | if ( pImage->PCHSGeometry.cCylinders == 0
|
---|
2207 | || pImage->PCHSGeometry.cHeads == 0
|
---|
2208 | || pImage->PCHSGeometry.cHeads > 16
|
---|
2209 | || pImage->PCHSGeometry.cSectors == 0
|
---|
2210 | || pImage->PCHSGeometry.cSectors > 63)
|
---|
2211 | {
|
---|
2212 | /* Mark PCHS geometry as not yet valid (can't do the calculation here
|
---|
2213 | * as the total image size isn't known yet). */
|
---|
2214 | pImage->PCHSGeometry.cCylinders = 0;
|
---|
2215 | pImage->PCHSGeometry.cHeads = 16;
|
---|
2216 | pImage->PCHSGeometry.cSectors = 63;
|
---|
2217 | }
|
---|
2218 |
|
---|
2219 | /* Determine LCHS geometry (set to 0 if not specified). */
|
---|
2220 | rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
|
---|
2221 | VMDK_DDB_GEO_LCHS_CYLINDERS,
|
---|
2222 | &pImage->LCHSGeometry.cCylinders);
|
---|
2223 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2224 | pImage->LCHSGeometry.cCylinders = 0;
|
---|
2225 | else if (RT_FAILURE(rc))
|
---|
2226 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
|
---|
2227 | rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
|
---|
2228 | VMDK_DDB_GEO_LCHS_HEADS,
|
---|
2229 | &pImage->LCHSGeometry.cHeads);
|
---|
2230 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2231 | pImage->LCHSGeometry.cHeads = 0;
|
---|
2232 | else if (RT_FAILURE(rc))
|
---|
2233 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
|
---|
2234 | rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
|
---|
2235 | VMDK_DDB_GEO_LCHS_SECTORS,
|
---|
2236 | &pImage->LCHSGeometry.cSectors);
|
---|
2237 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2238 | pImage->LCHSGeometry.cSectors = 0;
|
---|
2239 | else if (RT_FAILURE(rc))
|
---|
2240 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
|
---|
2241 | if ( pImage->LCHSGeometry.cCylinders == 0
|
---|
2242 | || pImage->LCHSGeometry.cHeads == 0
|
---|
2243 | || pImage->LCHSGeometry.cSectors == 0)
|
---|
2244 | {
|
---|
2245 | pImage->LCHSGeometry.cCylinders = 0;
|
---|
2246 | pImage->LCHSGeometry.cHeads = 0;
|
---|
2247 | pImage->LCHSGeometry.cSectors = 0;
|
---|
2248 | }
|
---|
2249 |
|
---|
2250 | /* Get image UUID. */
|
---|
2251 | rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, VMDK_DDB_IMAGE_UUID,
|
---|
2252 | &pImage->ImageUuid);
|
---|
2253 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2254 | {
|
---|
2255 | /* Image without UUID. Probably created by VMware and not yet used
|
---|
2256 | * by VirtualBox. Can only be added for images opened in read/write
|
---|
2257 | * mode, so don't bother producing a sensible UUID otherwise. */
|
---|
2258 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2259 | RTUuidClear(&pImage->ImageUuid);
|
---|
2260 | else
|
---|
2261 | {
|
---|
2262 | rc = RTUuidCreate(&pImage->ImageUuid);
|
---|
2263 | if (RT_FAILURE(rc))
|
---|
2264 | return rc;
|
---|
2265 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
2266 | VMDK_DDB_IMAGE_UUID, &pImage->ImageUuid);
|
---|
2267 | if (RT_FAILURE(rc))
|
---|
2268 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
2269 | }
|
---|
2270 | }
|
---|
2271 | else if (RT_FAILURE(rc))
|
---|
2272 | return rc;
|
---|
2273 |
|
---|
2274 | /* Get image modification UUID. */
|
---|
2275 | rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor,
|
---|
2276 | VMDK_DDB_MODIFICATION_UUID,
|
---|
2277 | &pImage->ModificationUuid);
|
---|
2278 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2279 | {
|
---|
2280 | /* Image without UUID. Probably created by VMware and not yet used
|
---|
2281 | * by VirtualBox. Can only be added for images opened in read/write
|
---|
2282 | * mode, so don't bother producing a sensible UUID otherwise. */
|
---|
2283 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2284 | RTUuidClear(&pImage->ModificationUuid);
|
---|
2285 | else
|
---|
2286 | {
|
---|
2287 | rc = RTUuidCreate(&pImage->ModificationUuid);
|
---|
2288 | if (RT_FAILURE(rc))
|
---|
2289 | return rc;
|
---|
2290 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
2291 | VMDK_DDB_MODIFICATION_UUID,
|
---|
2292 | &pImage->ModificationUuid);
|
---|
2293 | if (RT_FAILURE(rc))
|
---|
2294 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image modification UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
2295 | }
|
---|
2296 | }
|
---|
2297 | else if (RT_FAILURE(rc))
|
---|
2298 | return rc;
|
---|
2299 |
|
---|
2300 | /* Get UUID of parent image. */
|
---|
2301 | rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, VMDK_DDB_PARENT_UUID,
|
---|
2302 | &pImage->ParentUuid);
|
---|
2303 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2304 | {
|
---|
2305 | /* Image without UUID. Probably created by VMware and not yet used
|
---|
2306 | * by VirtualBox. Can only be added for images opened in read/write
|
---|
2307 | * mode, so don't bother producing a sensible UUID otherwise. */
|
---|
2308 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2309 | RTUuidClear(&pImage->ParentUuid);
|
---|
2310 | else
|
---|
2311 | {
|
---|
2312 | rc = RTUuidClear(&pImage->ParentUuid);
|
---|
2313 | if (RT_FAILURE(rc))
|
---|
2314 | return rc;
|
---|
2315 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
2316 | VMDK_DDB_PARENT_UUID, &pImage->ParentUuid);
|
---|
2317 | if (RT_FAILURE(rc))
|
---|
2318 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
2319 | }
|
---|
2320 | }
|
---|
2321 | else if (RT_FAILURE(rc))
|
---|
2322 | return rc;
|
---|
2323 |
|
---|
2324 | /* Get parent image modification UUID. */
|
---|
2325 | rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor,
|
---|
2326 | VMDK_DDB_PARENT_MODIFICATION_UUID,
|
---|
2327 | &pImage->ParentModificationUuid);
|
---|
2328 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
2329 | {
|
---|
2330 | /* Image without UUID. Probably created by VMware and not yet used
|
---|
2331 | * by VirtualBox. Can only be added for images opened in read/write
|
---|
2332 | * mode, so don't bother producing a sensible UUID otherwise. */
|
---|
2333 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2334 | RTUuidClear(&pImage->ParentModificationUuid);
|
---|
2335 | else
|
---|
2336 | {
|
---|
2337 | RTUuidClear(&pImage->ParentModificationUuid);
|
---|
2338 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
2339 | VMDK_DDB_PARENT_MODIFICATION_UUID,
|
---|
2340 | &pImage->ParentModificationUuid);
|
---|
2341 | if (RT_FAILURE(rc))
|
---|
2342 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent modification UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
2343 | }
|
---|
2344 | }
|
---|
2345 | else if (RT_FAILURE(rc))
|
---|
2346 | return rc;
|
---|
2347 |
|
---|
2348 | return VINF_SUCCESS;
|
---|
2349 | }
|
---|
2350 |
|
---|
2351 | /**
|
---|
2352 | * Internal : Prepares the descriptor to write to the image.
|
---|
2353 | */
|
---|
2354 | static int vmdkDescriptorPrepare(PVMDKIMAGE pImage, uint64_t cbLimit,
|
---|
2355 | void **ppvData, size_t *pcbData)
|
---|
2356 | {
|
---|
2357 | int rc = VINF_SUCCESS;
|
---|
2358 |
|
---|
2359 | /*
|
---|
2360 | * Allocate temporary descriptor buffer.
|
---|
2361 | * In case there is no limit allocate a default
|
---|
2362 | * and increase if required.
|
---|
2363 | */
|
---|
2364 | size_t cbDescriptor = cbLimit ? cbLimit : 4 * _1K;
|
---|
2365 | char *pszDescriptor = (char *)RTMemAllocZ(cbDescriptor);
|
---|
2366 | unsigned offDescriptor = 0;
|
---|
2367 |
|
---|
2368 | if (!pszDescriptor)
|
---|
2369 | return VERR_NO_MEMORY;
|
---|
2370 |
|
---|
2371 | for (unsigned i = 0; i < pImage->Descriptor.cLines; i++)
|
---|
2372 | {
|
---|
2373 | const char *psz = pImage->Descriptor.aLines[i];
|
---|
2374 | size_t cb = strlen(psz);
|
---|
2375 |
|
---|
2376 | /*
|
---|
2377 | * Increase the descriptor if there is no limit and
|
---|
2378 | * there is not enough room left for this line.
|
---|
2379 | */
|
---|
2380 | if (offDescriptor + cb + 1 > cbDescriptor)
|
---|
2381 | {
|
---|
2382 | if (cbLimit)
|
---|
2383 | {
|
---|
2384 | rc = vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too long in '%s'"), pImage->pszFilename);
|
---|
2385 | break;
|
---|
2386 | }
|
---|
2387 | else
|
---|
2388 | {
|
---|
2389 | char *pszDescriptorNew = NULL;
|
---|
2390 | LogFlow(("Increasing descriptor cache\n"));
|
---|
2391 |
|
---|
2392 | pszDescriptorNew = (char *)RTMemRealloc(pszDescriptor, cbDescriptor + cb + 4 * _1K);
|
---|
2393 | if (!pszDescriptorNew)
|
---|
2394 | {
|
---|
2395 | rc = VERR_NO_MEMORY;
|
---|
2396 | break;
|
---|
2397 | }
|
---|
2398 | pszDescriptor = pszDescriptorNew;
|
---|
2399 | cbDescriptor += cb + 4 * _1K;
|
---|
2400 | }
|
---|
2401 | }
|
---|
2402 |
|
---|
2403 | if (cb > 0)
|
---|
2404 | {
|
---|
2405 | memcpy(pszDescriptor + offDescriptor, psz, cb);
|
---|
2406 | offDescriptor += cb;
|
---|
2407 | }
|
---|
2408 |
|
---|
2409 | memcpy(pszDescriptor + offDescriptor, "\n", 1);
|
---|
2410 | offDescriptor++;
|
---|
2411 | }
|
---|
2412 |
|
---|
2413 | if (RT_SUCCESS(rc))
|
---|
2414 | {
|
---|
2415 | *ppvData = pszDescriptor;
|
---|
2416 | *pcbData = offDescriptor;
|
---|
2417 | }
|
---|
2418 | else if (pszDescriptor)
|
---|
2419 | RTMemFree(pszDescriptor);
|
---|
2420 |
|
---|
2421 | return rc;
|
---|
2422 | }
|
---|
2423 |
|
---|
2424 | /**
|
---|
2425 | * Internal: write/update the descriptor part of the image.
|
---|
2426 | */
|
---|
2427 | static int vmdkWriteDescriptor(PVMDKIMAGE pImage)
|
---|
2428 | {
|
---|
2429 | int rc = VINF_SUCCESS;
|
---|
2430 | uint64_t cbLimit;
|
---|
2431 | uint64_t uOffset;
|
---|
2432 | PVMDKFILE pDescFile;
|
---|
2433 | void *pvDescriptor;
|
---|
2434 | size_t cbDescriptor;
|
---|
2435 |
|
---|
2436 | if (pImage->pDescData)
|
---|
2437 | {
|
---|
2438 | /* Separate descriptor file. */
|
---|
2439 | uOffset = 0;
|
---|
2440 | cbLimit = 0;
|
---|
2441 | pDescFile = pImage->pFile;
|
---|
2442 | }
|
---|
2443 | else
|
---|
2444 | {
|
---|
2445 | /* Embedded descriptor file. */
|
---|
2446 | uOffset = VMDK_SECTOR2BYTE(pImage->pExtents[0].uDescriptorSector);
|
---|
2447 | cbLimit = VMDK_SECTOR2BYTE(pImage->pExtents[0].cDescriptorSectors);
|
---|
2448 | pDescFile = pImage->pExtents[0].pFile;
|
---|
2449 | }
|
---|
2450 | /* Bail out if there is no file to write to. */
|
---|
2451 | if (pDescFile == NULL)
|
---|
2452 | return VERR_INVALID_PARAMETER;
|
---|
2453 |
|
---|
2454 | rc = vmdkDescriptorPrepare(pImage, cbLimit, &pvDescriptor, &cbDescriptor);
|
---|
2455 | if (RT_SUCCESS(rc))
|
---|
2456 | {
|
---|
2457 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pDescFile->pStorage, uOffset,
|
---|
2458 | pvDescriptor, cbLimit ? cbLimit : cbDescriptor, NULL);
|
---|
2459 | if (RT_FAILURE(rc))
|
---|
2460 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing descriptor in '%s'"), pImage->pszFilename);
|
---|
2461 |
|
---|
2462 | if (RT_SUCCESS(rc) && !cbLimit)
|
---|
2463 | {
|
---|
2464 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pDescFile->pStorage, cbDescriptor);
|
---|
2465 | if (RT_FAILURE(rc))
|
---|
2466 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error truncating descriptor in '%s'"), pImage->pszFilename);
|
---|
2467 | }
|
---|
2468 |
|
---|
2469 | if (RT_SUCCESS(rc))
|
---|
2470 | pImage->Descriptor.fDirty = false;
|
---|
2471 |
|
---|
2472 | RTMemFree(pvDescriptor);
|
---|
2473 | }
|
---|
2474 |
|
---|
2475 | return rc;
|
---|
2476 | }
|
---|
2477 |
|
---|
2478 | /**
|
---|
2479 | * Internal: write/update the descriptor part of the image - async version.
|
---|
2480 | */
|
---|
2481 | static int vmdkWriteDescriptorAsync(PVMDKIMAGE pImage, PVDIOCTX pIoCtx)
|
---|
2482 | {
|
---|
2483 | int rc = VINF_SUCCESS;
|
---|
2484 | uint64_t cbLimit;
|
---|
2485 | uint64_t uOffset;
|
---|
2486 | PVMDKFILE pDescFile;
|
---|
2487 | void *pvDescriptor;
|
---|
2488 | size_t cbDescriptor;
|
---|
2489 |
|
---|
2490 | if (pImage->pDescData)
|
---|
2491 | {
|
---|
2492 | /* Separate descriptor file. */
|
---|
2493 | uOffset = 0;
|
---|
2494 | cbLimit = 0;
|
---|
2495 | pDescFile = pImage->pFile;
|
---|
2496 | }
|
---|
2497 | else
|
---|
2498 | {
|
---|
2499 | /* Embedded descriptor file. */
|
---|
2500 | uOffset = VMDK_SECTOR2BYTE(pImage->pExtents[0].uDescriptorSector);
|
---|
2501 | cbLimit = VMDK_SECTOR2BYTE(pImage->pExtents[0].cDescriptorSectors);
|
---|
2502 | pDescFile = pImage->pExtents[0].pFile;
|
---|
2503 | }
|
---|
2504 | /* Bail out if there is no file to write to. */
|
---|
2505 | if (pDescFile == NULL)
|
---|
2506 | return VERR_INVALID_PARAMETER;
|
---|
2507 |
|
---|
2508 | rc = vmdkDescriptorPrepare(pImage, cbLimit, &pvDescriptor, &cbDescriptor);
|
---|
2509 | if (RT_SUCCESS(rc))
|
---|
2510 | {
|
---|
2511 | rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pDescFile->pStorage,
|
---|
2512 | uOffset, pvDescriptor,
|
---|
2513 | cbLimit ? cbLimit : cbDescriptor,
|
---|
2514 | pIoCtx, NULL, NULL);
|
---|
2515 | if ( RT_FAILURE(rc)
|
---|
2516 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2517 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing descriptor in '%s'"), pImage->pszFilename);
|
---|
2518 | }
|
---|
2519 |
|
---|
2520 | if (RT_SUCCESS(rc) && !cbLimit)
|
---|
2521 | {
|
---|
2522 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pDescFile->pStorage, cbDescriptor);
|
---|
2523 | if (RT_FAILURE(rc))
|
---|
2524 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error truncating descriptor in '%s'"), pImage->pszFilename);
|
---|
2525 | }
|
---|
2526 |
|
---|
2527 | if (RT_SUCCESS(rc))
|
---|
2528 | pImage->Descriptor.fDirty = false;
|
---|
2529 |
|
---|
2530 | RTMemFree(pvDescriptor);
|
---|
2531 | return rc;
|
---|
2532 |
|
---|
2533 | }
|
---|
2534 |
|
---|
2535 | /**
|
---|
2536 | * Internal: validate the consistency check values in a binary header.
|
---|
2537 | */
|
---|
2538 | static int vmdkValidateHeader(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, const SparseExtentHeader *pHeader)
|
---|
2539 | {
|
---|
2540 | int rc = VINF_SUCCESS;
|
---|
2541 | if (RT_LE2H_U32(pHeader->magicNumber) != VMDK_SPARSE_MAGICNUMBER)
|
---|
2542 | {
|
---|
2543 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect magic in sparse extent header in '%s'"), pExtent->pszFullname);
|
---|
2544 | return rc;
|
---|
2545 | }
|
---|
2546 | if (RT_LE2H_U32(pHeader->version) != 1 && RT_LE2H_U32(pHeader->version) != 3)
|
---|
2547 | {
|
---|
2548 | 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);
|
---|
2549 | return rc;
|
---|
2550 | }
|
---|
2551 | if ( (RT_LE2H_U32(pHeader->flags) & 1)
|
---|
2552 | && ( pHeader->singleEndLineChar != '\n'
|
---|
2553 | || pHeader->nonEndLineChar != ' '
|
---|
2554 | || pHeader->doubleEndLineChar1 != '\r'
|
---|
2555 | || pHeader->doubleEndLineChar2 != '\n') )
|
---|
2556 | {
|
---|
2557 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: corrupted by CR/LF translation in '%s'"), pExtent->pszFullname);
|
---|
2558 | return rc;
|
---|
2559 | }
|
---|
2560 | return rc;
|
---|
2561 | }
|
---|
2562 |
|
---|
2563 | /**
|
---|
2564 | * Internal: read metadata belonging to an extent with binary header, i.e.
|
---|
2565 | * as found in monolithic files.
|
---|
2566 | */
|
---|
2567 | static int vmdkReadBinaryMetaExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
2568 | bool fMagicAlreadyRead)
|
---|
2569 | {
|
---|
2570 | SparseExtentHeader Header;
|
---|
2571 | uint64_t cSectorsPerGDE;
|
---|
2572 | uint64_t cbFile = 0;
|
---|
2573 | int rc;
|
---|
2574 |
|
---|
2575 | if (!fMagicAlreadyRead)
|
---|
2576 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage, 0,
|
---|
2577 | &Header, sizeof(Header), NULL);
|
---|
2578 | else
|
---|
2579 | {
|
---|
2580 | Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
|
---|
2581 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
2582 | RT_OFFSETOF(SparseExtentHeader, version),
|
---|
2583 | &Header.version,
|
---|
2584 | sizeof(Header)
|
---|
2585 | - RT_OFFSETOF(SparseExtentHeader, version),
|
---|
2586 | NULL);
|
---|
2587 | }
|
---|
2588 | AssertRC(rc);
|
---|
2589 | if (RT_FAILURE(rc))
|
---|
2590 | {
|
---|
2591 | vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading extent header in '%s'"), pExtent->pszFullname);
|
---|
2592 | rc = VERR_VD_VMDK_INVALID_HEADER;
|
---|
2593 | goto out;
|
---|
2594 | }
|
---|
2595 | rc = vmdkValidateHeader(pImage, pExtent, &Header);
|
---|
2596 | if (RT_FAILURE(rc))
|
---|
2597 | goto out;
|
---|
2598 |
|
---|
2599 | if ( (RT_LE2H_U32(Header.flags) & RT_BIT(17))
|
---|
2600 | && RT_LE2H_U64(Header.gdOffset) == VMDK_GD_AT_END)
|
---|
2601 | pExtent->fFooter = true;
|
---|
2602 |
|
---|
2603 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2604 | || ( pExtent->fFooter
|
---|
2605 | && !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
|
---|
2606 | {
|
---|
2607 | rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbFile);
|
---|
2608 | AssertRC(rc);
|
---|
2609 | if (RT_FAILURE(rc))
|
---|
2610 | {
|
---|
2611 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot get size of '%s'"), pExtent->pszFullname);
|
---|
2612 | goto out;
|
---|
2613 | }
|
---|
2614 | }
|
---|
2615 |
|
---|
2616 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
2617 | pExtent->uAppendPosition = RT_ALIGN_64(cbFile, 512);
|
---|
2618 |
|
---|
2619 | if ( pExtent->fFooter
|
---|
2620 | && ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2621 | || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
|
---|
2622 | {
|
---|
2623 | /* Read the footer, which comes before the end-of-stream marker. */
|
---|
2624 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
2625 | cbFile - 2*512, &Header,
|
---|
2626 | sizeof(Header), NULL);
|
---|
2627 | AssertRC(rc);
|
---|
2628 | if (RT_FAILURE(rc))
|
---|
2629 | {
|
---|
2630 | vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading extent footer in '%s'"), pExtent->pszFullname);
|
---|
2631 | rc = VERR_VD_VMDK_INVALID_HEADER;
|
---|
2632 | goto out;
|
---|
2633 | }
|
---|
2634 | rc = vmdkValidateHeader(pImage, pExtent, &Header);
|
---|
2635 | if (RT_FAILURE(rc))
|
---|
2636 | goto out;
|
---|
2637 | /* Prohibit any writes to this extent. */
|
---|
2638 | pExtent->uAppendPosition = 0;
|
---|
2639 | }
|
---|
2640 |
|
---|
2641 | pExtent->uVersion = RT_LE2H_U32(Header.version);
|
---|
2642 | pExtent->enmType = VMDKETYPE_HOSTED_SPARSE; /* Just dummy value, changed later. */
|
---|
2643 | pExtent->cSectors = RT_LE2H_U64(Header.capacity);
|
---|
2644 | pExtent->cSectorsPerGrain = RT_LE2H_U64(Header.grainSize);
|
---|
2645 | pExtent->uDescriptorSector = RT_LE2H_U64(Header.descriptorOffset);
|
---|
2646 | pExtent->cDescriptorSectors = RT_LE2H_U64(Header.descriptorSize);
|
---|
2647 | if (pExtent->uDescriptorSector && !pExtent->cDescriptorSectors)
|
---|
2648 | {
|
---|
2649 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: inconsistent embedded descriptor config in '%s'"), pExtent->pszFullname);
|
---|
2650 | goto out;
|
---|
2651 | }
|
---|
2652 | pExtent->cGTEntries = RT_LE2H_U32(Header.numGTEsPerGT);
|
---|
2653 | if (RT_LE2H_U32(Header.flags) & RT_BIT(1))
|
---|
2654 | {
|
---|
2655 | pExtent->uSectorRGD = RT_LE2H_U64(Header.rgdOffset);
|
---|
2656 | pExtent->uSectorGD = RT_LE2H_U64(Header.gdOffset);
|
---|
2657 | }
|
---|
2658 | else
|
---|
2659 | {
|
---|
2660 | pExtent->uSectorGD = RT_LE2H_U64(Header.gdOffset);
|
---|
2661 | pExtent->uSectorRGD = 0;
|
---|
2662 | }
|
---|
2663 | if ( ( pExtent->uSectorGD == VMDK_GD_AT_END
|
---|
2664 | || pExtent->uSectorRGD == VMDK_GD_AT_END)
|
---|
2665 | && ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2666 | || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
|
---|
2667 | {
|
---|
2668 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: cannot resolve grain directory offset in '%s'"), pExtent->pszFullname);
|
---|
2669 | goto out;
|
---|
2670 | }
|
---|
2671 | pExtent->cOverheadSectors = RT_LE2H_U64(Header.overHead);
|
---|
2672 | pExtent->fUncleanShutdown = !!Header.uncleanShutdown;
|
---|
2673 | pExtent->uCompression = RT_LE2H_U16(Header.compressAlgorithm);
|
---|
2674 | cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
|
---|
2675 | if (!cSectorsPerGDE || cSectorsPerGDE > UINT32_MAX)
|
---|
2676 | {
|
---|
2677 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect grain directory size in '%s'"), pExtent->pszFullname);
|
---|
2678 | goto out;
|
---|
2679 | }
|
---|
2680 | pExtent->cSectorsPerGDE = cSectorsPerGDE;
|
---|
2681 | pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
|
---|
2682 |
|
---|
2683 | /* Fix up the number of descriptor sectors, as some flat images have
|
---|
2684 | * really just one, and this causes failures when inserting the UUID
|
---|
2685 | * values and other extra information. */
|
---|
2686 | if (pExtent->cDescriptorSectors != 0 && pExtent->cDescriptorSectors < 4)
|
---|
2687 | {
|
---|
2688 | /* Do it the easy way - just fix it for flat images which have no
|
---|
2689 | * other complicated metadata which needs space too. */
|
---|
2690 | if ( pExtent->uDescriptorSector + 4 < pExtent->cOverheadSectors
|
---|
2691 | && pExtent->cGTEntries * pExtent->cGDEntries == 0)
|
---|
2692 | pExtent->cDescriptorSectors = 4;
|
---|
2693 | }
|
---|
2694 |
|
---|
2695 | out:
|
---|
2696 | if (RT_FAILURE(rc))
|
---|
2697 | vmdkFreeExtentData(pImage, pExtent, false);
|
---|
2698 |
|
---|
2699 | return rc;
|
---|
2700 | }
|
---|
2701 |
|
---|
2702 | /**
|
---|
2703 | * Internal: read additional metadata belonging to an extent. For those
|
---|
2704 | * extents which have no additional metadata just verify the information.
|
---|
2705 | */
|
---|
2706 | static int vmdkReadMetaExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
|
---|
2707 | {
|
---|
2708 | int rc = VINF_SUCCESS;
|
---|
2709 |
|
---|
2710 | /* disabled the check as there are too many truncated vmdk images out there */
|
---|
2711 | #ifdef VBOX_WITH_VMDK_STRICT_SIZE_CHECK
|
---|
2712 | uint64_t cbExtentSize;
|
---|
2713 | /* The image must be a multiple of a sector in size and contain the data
|
---|
2714 | * area (flat images only). If not, it means the image is at least
|
---|
2715 | * truncated, or even seriously garbled. */
|
---|
2716 | rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbExtentSize);
|
---|
2717 | if (RT_FAILURE(rc))
|
---|
2718 | {
|
---|
2719 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting size in '%s'"), pExtent->pszFullname);
|
---|
2720 | goto out;
|
---|
2721 | }
|
---|
2722 | if ( cbExtentSize != RT_ALIGN_64(cbExtentSize, 512)
|
---|
2723 | && (pExtent->enmType != VMDKETYPE_FLAT || pExtent->cNominalSectors + pExtent->uSectorOffset > VMDK_BYTE2SECTOR(cbExtentSize)))
|
---|
2724 | {
|
---|
2725 | 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);
|
---|
2726 | goto out;
|
---|
2727 | }
|
---|
2728 | #endif /* VBOX_WITH_VMDK_STRICT_SIZE_CHECK */
|
---|
2729 | if (pExtent->enmType != VMDKETYPE_HOSTED_SPARSE)
|
---|
2730 | goto out;
|
---|
2731 |
|
---|
2732 | /* The spec says that this must be a power of two and greater than 8,
|
---|
2733 | * but probably they meant not less than 8. */
|
---|
2734 | if ( (pExtent->cSectorsPerGrain & (pExtent->cSectorsPerGrain - 1))
|
---|
2735 | || pExtent->cSectorsPerGrain < 8)
|
---|
2736 | {
|
---|
2737 | 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);
|
---|
2738 | goto out;
|
---|
2739 | }
|
---|
2740 |
|
---|
2741 | /* This code requires that a grain table must hold a power of two multiple
|
---|
2742 | * of the number of entries per GT cache entry. */
|
---|
2743 | if ( (pExtent->cGTEntries & (pExtent->cGTEntries - 1))
|
---|
2744 | || pExtent->cGTEntries < VMDK_GT_CACHELINE_SIZE)
|
---|
2745 | {
|
---|
2746 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: grain table cache size problem in '%s'"), pExtent->pszFullname);
|
---|
2747 | goto out;
|
---|
2748 | }
|
---|
2749 |
|
---|
2750 | rc = vmdkAllocStreamBuffers(pImage, pExtent);
|
---|
2751 | if (RT_FAILURE(rc))
|
---|
2752 | goto out;
|
---|
2753 |
|
---|
2754 | /* Prohibit any writes to this streamOptimized extent. */
|
---|
2755 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
2756 | pExtent->uAppendPosition = 0;
|
---|
2757 |
|
---|
2758 | if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
2759 | || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2760 | || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
|
---|
2761 | rc = vmdkReadGrainDirectory(pImage, pExtent);
|
---|
2762 | else
|
---|
2763 | {
|
---|
2764 | pExtent->uGrainSectorAbs = pExtent->cOverheadSectors;
|
---|
2765 | pExtent->cbGrainStreamRead = 0;
|
---|
2766 | }
|
---|
2767 |
|
---|
2768 | out:
|
---|
2769 | if (RT_FAILURE(rc))
|
---|
2770 | vmdkFreeExtentData(pImage, pExtent, false);
|
---|
2771 |
|
---|
2772 | return rc;
|
---|
2773 | }
|
---|
2774 |
|
---|
2775 | /**
|
---|
2776 | * Internal: write/update the metadata for a sparse extent.
|
---|
2777 | */
|
---|
2778 | static int vmdkWriteMetaSparseExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
2779 | uint64_t uOffset)
|
---|
2780 | {
|
---|
2781 | SparseExtentHeader Header;
|
---|
2782 |
|
---|
2783 | memset(&Header, '\0', sizeof(Header));
|
---|
2784 | Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
|
---|
2785 | Header.version = RT_H2LE_U32(pExtent->uVersion);
|
---|
2786 | Header.flags = RT_H2LE_U32(RT_BIT(0));
|
---|
2787 | if (pExtent->pRGD)
|
---|
2788 | Header.flags |= RT_H2LE_U32(RT_BIT(1));
|
---|
2789 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
2790 | Header.flags |= RT_H2LE_U32(RT_BIT(16) | RT_BIT(17));
|
---|
2791 | Header.capacity = RT_H2LE_U64(pExtent->cSectors);
|
---|
2792 | Header.grainSize = RT_H2LE_U64(pExtent->cSectorsPerGrain);
|
---|
2793 | Header.descriptorOffset = RT_H2LE_U64(pExtent->uDescriptorSector);
|
---|
2794 | Header.descriptorSize = RT_H2LE_U64(pExtent->cDescriptorSectors);
|
---|
2795 | Header.numGTEsPerGT = RT_H2LE_U32(pExtent->cGTEntries);
|
---|
2796 | if (pExtent->fFooter && uOffset == 0)
|
---|
2797 | {
|
---|
2798 | if (pExtent->pRGD)
|
---|
2799 | {
|
---|
2800 | Assert(pExtent->uSectorRGD);
|
---|
2801 | Header.rgdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
|
---|
2802 | Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
|
---|
2803 | }
|
---|
2804 | else
|
---|
2805 | {
|
---|
2806 | Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
|
---|
2807 | }
|
---|
2808 | }
|
---|
2809 | else
|
---|
2810 | {
|
---|
2811 | if (pExtent->pRGD)
|
---|
2812 | {
|
---|
2813 | Assert(pExtent->uSectorRGD);
|
---|
2814 | Header.rgdOffset = RT_H2LE_U64(pExtent->uSectorRGD);
|
---|
2815 | Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
|
---|
2816 | }
|
---|
2817 | else
|
---|
2818 | {
|
---|
2819 | Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
|
---|
2820 | }
|
---|
2821 | }
|
---|
2822 | Header.overHead = RT_H2LE_U64(pExtent->cOverheadSectors);
|
---|
2823 | Header.uncleanShutdown = pExtent->fUncleanShutdown;
|
---|
2824 | Header.singleEndLineChar = '\n';
|
---|
2825 | Header.nonEndLineChar = ' ';
|
---|
2826 | Header.doubleEndLineChar1 = '\r';
|
---|
2827 | Header.doubleEndLineChar2 = '\n';
|
---|
2828 | Header.compressAlgorithm = RT_H2LE_U16(pExtent->uCompression);
|
---|
2829 |
|
---|
2830 | int rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
2831 | uOffset, &Header, sizeof(Header), NULL);
|
---|
2832 | AssertRC(rc);
|
---|
2833 | if (RT_FAILURE(rc))
|
---|
2834 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing extent header in '%s'"), pExtent->pszFullname);
|
---|
2835 | return rc;
|
---|
2836 | }
|
---|
2837 |
|
---|
2838 | /**
|
---|
2839 | * Internal: write/update the metadata for a sparse extent - async version.
|
---|
2840 | */
|
---|
2841 | static int vmdkWriteMetaSparseExtentAsync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
2842 | uint64_t uOffset, PVDIOCTX pIoCtx)
|
---|
2843 | {
|
---|
2844 | SparseExtentHeader Header;
|
---|
2845 |
|
---|
2846 | memset(&Header, '\0', sizeof(Header));
|
---|
2847 | Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
|
---|
2848 | Header.version = RT_H2LE_U32(pExtent->uVersion);
|
---|
2849 | Header.flags = RT_H2LE_U32(RT_BIT(0));
|
---|
2850 | if (pExtent->pRGD)
|
---|
2851 | Header.flags |= RT_H2LE_U32(RT_BIT(1));
|
---|
2852 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
2853 | Header.flags |= RT_H2LE_U32(RT_BIT(16) | RT_BIT(17));
|
---|
2854 | Header.capacity = RT_H2LE_U64(pExtent->cSectors);
|
---|
2855 | Header.grainSize = RT_H2LE_U64(pExtent->cSectorsPerGrain);
|
---|
2856 | Header.descriptorOffset = RT_H2LE_U64(pExtent->uDescriptorSector);
|
---|
2857 | Header.descriptorSize = RT_H2LE_U64(pExtent->cDescriptorSectors);
|
---|
2858 | Header.numGTEsPerGT = RT_H2LE_U32(pExtent->cGTEntries);
|
---|
2859 | if (pExtent->fFooter && uOffset == 0)
|
---|
2860 | {
|
---|
2861 | if (pExtent->pRGD)
|
---|
2862 | {
|
---|
2863 | Assert(pExtent->uSectorRGD);
|
---|
2864 | Header.rgdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
|
---|
2865 | Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
|
---|
2866 | }
|
---|
2867 | else
|
---|
2868 | {
|
---|
2869 | Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
|
---|
2870 | }
|
---|
2871 | }
|
---|
2872 | else
|
---|
2873 | {
|
---|
2874 | if (pExtent->pRGD)
|
---|
2875 | {
|
---|
2876 | Assert(pExtent->uSectorRGD);
|
---|
2877 | Header.rgdOffset = RT_H2LE_U64(pExtent->uSectorRGD);
|
---|
2878 | Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
|
---|
2879 | }
|
---|
2880 | else
|
---|
2881 | {
|
---|
2882 | Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
|
---|
2883 | }
|
---|
2884 | }
|
---|
2885 | Header.overHead = RT_H2LE_U64(pExtent->cOverheadSectors);
|
---|
2886 | Header.uncleanShutdown = pExtent->fUncleanShutdown;
|
---|
2887 | Header.singleEndLineChar = '\n';
|
---|
2888 | Header.nonEndLineChar = ' ';
|
---|
2889 | Header.doubleEndLineChar1 = '\r';
|
---|
2890 | Header.doubleEndLineChar2 = '\n';
|
---|
2891 | Header.compressAlgorithm = RT_H2LE_U16(pExtent->uCompression);
|
---|
2892 |
|
---|
2893 | int rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
2894 | uOffset, &Header, sizeof(Header),
|
---|
2895 | pIoCtx, NULL, NULL);
|
---|
2896 | if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
2897 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing extent header in '%s'"), pExtent->pszFullname);
|
---|
2898 | return rc;
|
---|
2899 | }
|
---|
2900 |
|
---|
2901 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
2902 | /**
|
---|
2903 | * Internal: unused code to read the metadata of a sparse ESX extent.
|
---|
2904 | *
|
---|
2905 | * Such extents never leave ESX server, so this isn't ever used.
|
---|
2906 | */
|
---|
2907 | static int vmdkReadMetaESXSparseExtent(PVMDKEXTENT pExtent)
|
---|
2908 | {
|
---|
2909 | COWDisk_Header Header;
|
---|
2910 | uint64_t cSectorsPerGDE;
|
---|
2911 |
|
---|
2912 | int rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage, 0,
|
---|
2913 | &Header, sizeof(Header), NULL);
|
---|
2914 | AssertRC(rc);
|
---|
2915 | if (RT_FAILURE(rc))
|
---|
2916 | {
|
---|
2917 | vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading ESX sparse extent header in '%s'"), pExtent->pszFullname);
|
---|
2918 | rc = VERR_VD_VMDK_INVALID_HEADER;
|
---|
2919 | goto out;
|
---|
2920 | }
|
---|
2921 | if ( RT_LE2H_U32(Header.magicNumber) != VMDK_ESX_SPARSE_MAGICNUMBER
|
---|
2922 | || RT_LE2H_U32(Header.version) != 1
|
---|
2923 | || RT_LE2H_U32(Header.flags) != 3)
|
---|
2924 | {
|
---|
2925 | rc = VERR_VD_VMDK_INVALID_HEADER;
|
---|
2926 | goto out;
|
---|
2927 | }
|
---|
2928 | pExtent->enmType = VMDKETYPE_ESX_SPARSE;
|
---|
2929 | pExtent->cSectors = RT_LE2H_U32(Header.numSectors);
|
---|
2930 | pExtent->cSectorsPerGrain = RT_LE2H_U32(Header.grainSize);
|
---|
2931 | /* The spec says that this must be between 1 sector and 1MB. This code
|
---|
2932 | * assumes it's a power of two, so check that requirement, too. */
|
---|
2933 | if ( (pExtent->cSectorsPerGrain & (pExtent->cSectorsPerGrain - 1))
|
---|
2934 | || pExtent->cSectorsPerGrain == 0
|
---|
2935 | || pExtent->cSectorsPerGrain > 2048)
|
---|
2936 | {
|
---|
2937 | rc = VERR_VD_VMDK_INVALID_HEADER;
|
---|
2938 | goto out;
|
---|
2939 | }
|
---|
2940 | pExtent->uDescriptorSector = 0;
|
---|
2941 | pExtent->cDescriptorSectors = 0;
|
---|
2942 | pExtent->uSectorGD = RT_LE2H_U32(Header.gdOffset);
|
---|
2943 | pExtent->uSectorRGD = 0;
|
---|
2944 | pExtent->cOverheadSectors = 0;
|
---|
2945 | pExtent->cGTEntries = 4096;
|
---|
2946 | cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
|
---|
2947 | if (!cSectorsPerGDE || cSectorsPerGDE > UINT32_MAX)
|
---|
2948 | {
|
---|
2949 | rc = VERR_VD_VMDK_INVALID_HEADER;
|
---|
2950 | goto out;
|
---|
2951 | }
|
---|
2952 | pExtent->cSectorsPerGDE = cSectorsPerGDE;
|
---|
2953 | pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
|
---|
2954 | if (pExtent->cGDEntries != RT_LE2H_U32(Header.numGDEntries))
|
---|
2955 | {
|
---|
2956 | /* Inconsistency detected. Computed number of GD entries doesn't match
|
---|
2957 | * stored value. Better be safe than sorry. */
|
---|
2958 | rc = VERR_VD_VMDK_INVALID_HEADER;
|
---|
2959 | goto out;
|
---|
2960 | }
|
---|
2961 | pExtent->uFreeSector = RT_LE2H_U32(Header.freeSector);
|
---|
2962 | pExtent->fUncleanShutdown = !!Header.uncleanShutdown;
|
---|
2963 |
|
---|
2964 | rc = vmdkReadGrainDirectory(pImage, pExtent);
|
---|
2965 |
|
---|
2966 | out:
|
---|
2967 | if (RT_FAILURE(rc))
|
---|
2968 | vmdkFreeExtentData(pImage, pExtent, false);
|
---|
2969 |
|
---|
2970 | return rc;
|
---|
2971 | }
|
---|
2972 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
2973 |
|
---|
2974 | /**
|
---|
2975 | * Internal: free the buffers used for streamOptimized images.
|
---|
2976 | */
|
---|
2977 | static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent)
|
---|
2978 | {
|
---|
2979 | if (pExtent->pvCompGrain)
|
---|
2980 | {
|
---|
2981 | RTMemFree(pExtent->pvCompGrain);
|
---|
2982 | pExtent->pvCompGrain = NULL;
|
---|
2983 | }
|
---|
2984 | if (pExtent->pvGrain)
|
---|
2985 | {
|
---|
2986 | RTMemFree(pExtent->pvGrain);
|
---|
2987 | pExtent->pvGrain = NULL;
|
---|
2988 | }
|
---|
2989 | }
|
---|
2990 |
|
---|
2991 | /**
|
---|
2992 | * Internal: free the memory used by the extent data structure, optionally
|
---|
2993 | * deleting the referenced files.
|
---|
2994 | */
|
---|
2995 | static void vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
2996 | bool fDelete)
|
---|
2997 | {
|
---|
2998 | vmdkFreeGrainDirectory(pExtent);
|
---|
2999 | if (pExtent->pDescData)
|
---|
3000 | {
|
---|
3001 | RTMemFree(pExtent->pDescData);
|
---|
3002 | pExtent->pDescData = NULL;
|
---|
3003 | }
|
---|
3004 | if (pExtent->pFile != NULL)
|
---|
3005 | {
|
---|
3006 | /* Do not delete raw extents, these have full and base names equal. */
|
---|
3007 | vmdkFileClose(pImage, &pExtent->pFile,
|
---|
3008 | fDelete
|
---|
3009 | && pExtent->pszFullname
|
---|
3010 | && strcmp(pExtent->pszFullname, pExtent->pszBasename));
|
---|
3011 | }
|
---|
3012 | if (pExtent->pszBasename)
|
---|
3013 | {
|
---|
3014 | RTMemTmpFree((void *)pExtent->pszBasename);
|
---|
3015 | pExtent->pszBasename = NULL;
|
---|
3016 | }
|
---|
3017 | if (pExtent->pszFullname)
|
---|
3018 | {
|
---|
3019 | RTStrFree((char *)(void *)pExtent->pszFullname);
|
---|
3020 | pExtent->pszFullname = NULL;
|
---|
3021 | }
|
---|
3022 | vmdkFreeStreamBuffers(pExtent);
|
---|
3023 | }
|
---|
3024 |
|
---|
3025 | /**
|
---|
3026 | * Internal: allocate grain table cache if necessary for this image.
|
---|
3027 | */
|
---|
3028 | static int vmdkAllocateGrainTableCache(PVMDKIMAGE pImage)
|
---|
3029 | {
|
---|
3030 | PVMDKEXTENT pExtent;
|
---|
3031 |
|
---|
3032 | /* Allocate grain table cache if any sparse extent is present. */
|
---|
3033 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
3034 | {
|
---|
3035 | pExtent = &pImage->pExtents[i];
|
---|
3036 | if ( pExtent->enmType == VMDKETYPE_HOSTED_SPARSE
|
---|
3037 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
3038 | || pExtent->enmType == VMDKETYPE_ESX_SPARSE
|
---|
3039 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
3040 | )
|
---|
3041 | {
|
---|
3042 | /* Allocate grain table cache. */
|
---|
3043 | pImage->pGTCache = (PVMDKGTCACHE)RTMemAllocZ(sizeof(VMDKGTCACHE));
|
---|
3044 | if (!pImage->pGTCache)
|
---|
3045 | return VERR_NO_MEMORY;
|
---|
3046 | for (unsigned j = 0; j < VMDK_GT_CACHE_SIZE; j++)
|
---|
3047 | {
|
---|
3048 | PVMDKGTCACHEENTRY pGCE = &pImage->pGTCache->aGTCache[j];
|
---|
3049 | pGCE->uExtent = UINT32_MAX;
|
---|
3050 | }
|
---|
3051 | pImage->pGTCache->cEntries = VMDK_GT_CACHE_SIZE;
|
---|
3052 | break;
|
---|
3053 | }
|
---|
3054 | }
|
---|
3055 |
|
---|
3056 | return VINF_SUCCESS;
|
---|
3057 | }
|
---|
3058 |
|
---|
3059 | /**
|
---|
3060 | * Internal: allocate the given number of extents.
|
---|
3061 | */
|
---|
3062 | static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents)
|
---|
3063 | {
|
---|
3064 | int rc = VINF_SUCCESS;
|
---|
3065 | PVMDKEXTENT pExtents = (PVMDKEXTENT)RTMemAllocZ(cExtents * sizeof(VMDKEXTENT));
|
---|
3066 | if (pExtents)
|
---|
3067 | {
|
---|
3068 | for (unsigned i = 0; i < cExtents; i++)
|
---|
3069 | {
|
---|
3070 | pExtents[i].pFile = NULL;
|
---|
3071 | pExtents[i].pszBasename = NULL;
|
---|
3072 | pExtents[i].pszFullname = NULL;
|
---|
3073 | pExtents[i].pGD = NULL;
|
---|
3074 | pExtents[i].pRGD = NULL;
|
---|
3075 | pExtents[i].pDescData = NULL;
|
---|
3076 | pExtents[i].uVersion = 1;
|
---|
3077 | pExtents[i].uCompression = VMDK_COMPRESSION_NONE;
|
---|
3078 | pExtents[i].uExtent = i;
|
---|
3079 | pExtents[i].pImage = pImage;
|
---|
3080 | }
|
---|
3081 | pImage->pExtents = pExtents;
|
---|
3082 | pImage->cExtents = cExtents;
|
---|
3083 | }
|
---|
3084 | else
|
---|
3085 | rc = VERR_NO_MEMORY;
|
---|
3086 |
|
---|
3087 | return rc;
|
---|
3088 | }
|
---|
3089 |
|
---|
3090 | /**
|
---|
3091 | * Internal: Open an image, constructing all necessary data structures.
|
---|
3092 | */
|
---|
3093 | static int vmdkOpenImage(PVMDKIMAGE pImage, unsigned uOpenFlags)
|
---|
3094 | {
|
---|
3095 | int rc;
|
---|
3096 | uint32_t u32Magic;
|
---|
3097 | PVMDKFILE pFile;
|
---|
3098 | PVMDKEXTENT pExtent;
|
---|
3099 |
|
---|
3100 | pImage->uOpenFlags = uOpenFlags;
|
---|
3101 |
|
---|
3102 | pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
|
---|
3103 | pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
|
---|
3104 | AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
|
---|
3105 |
|
---|
3106 | /*
|
---|
3107 | * Open the image.
|
---|
3108 | * We don't have to check for asynchronous access because
|
---|
3109 | * we only support raw access and the opened file is a description
|
---|
3110 | * file were no data is stored.
|
---|
3111 | */
|
---|
3112 |
|
---|
3113 | rc = vmdkFileOpen(pImage, &pFile, pImage->pszFilename,
|
---|
3114 | VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */),
|
---|
3115 | false /* fAsyncIO */);
|
---|
3116 | if (RT_FAILURE(rc))
|
---|
3117 | {
|
---|
3118 | /* Do NOT signal an appropriate error here, as the VD layer has the
|
---|
3119 | * choice of retrying the open if it failed. */
|
---|
3120 | goto out;
|
---|
3121 | }
|
---|
3122 | pImage->pFile = pFile;
|
---|
3123 |
|
---|
3124 | /* Read magic (if present). */
|
---|
3125 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pFile->pStorage, 0,
|
---|
3126 | &u32Magic, sizeof(u32Magic), NULL);
|
---|
3127 | if (RT_FAILURE(rc))
|
---|
3128 | {
|
---|
3129 | vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading the magic number in '%s'"), pImage->pszFilename);
|
---|
3130 | rc = VERR_VD_VMDK_INVALID_HEADER;
|
---|
3131 | goto out;
|
---|
3132 | }
|
---|
3133 |
|
---|
3134 | /* Handle the file according to its magic number. */
|
---|
3135 | if (RT_LE2H_U32(u32Magic) == VMDK_SPARSE_MAGICNUMBER)
|
---|
3136 | {
|
---|
3137 | /* It's a hosted single-extent image. */
|
---|
3138 | rc = vmdkCreateExtents(pImage, 1);
|
---|
3139 | if (RT_FAILURE(rc))
|
---|
3140 | goto out;
|
---|
3141 | /* The opened file is passed to the extent. No separate descriptor
|
---|
3142 | * file, so no need to keep anything open for the image. */
|
---|
3143 | pExtent = &pImage->pExtents[0];
|
---|
3144 | pExtent->pFile = pFile;
|
---|
3145 | pImage->pFile = NULL;
|
---|
3146 | pExtent->pszFullname = RTPathAbsDup(pImage->pszFilename);
|
---|
3147 | if (!pExtent->pszFullname)
|
---|
3148 | {
|
---|
3149 | rc = VERR_NO_MEMORY;
|
---|
3150 | goto out;
|
---|
3151 | }
|
---|
3152 | rc = vmdkReadBinaryMetaExtent(pImage, pExtent, true /* fMagicAlreadyRead */);
|
---|
3153 | if (RT_FAILURE(rc))
|
---|
3154 | goto out;
|
---|
3155 |
|
---|
3156 | /* As we're dealing with a monolithic image here, there must
|
---|
3157 | * be a descriptor embedded in the image file. */
|
---|
3158 | if (!pExtent->uDescriptorSector || !pExtent->cDescriptorSectors)
|
---|
3159 | {
|
---|
3160 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: monolithic image without descriptor in '%s'"), pImage->pszFilename);
|
---|
3161 | goto out;
|
---|
3162 | }
|
---|
3163 | /* HACK: extend the descriptor if it is unusually small and it fits in
|
---|
3164 | * the unused space after the image header. Allows opening VMDK files
|
---|
3165 | * with extremely small descriptor in read/write mode. */
|
---|
3166 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
3167 | && pExtent->cDescriptorSectors < 3
|
---|
3168 | && (int64_t)pExtent->uSectorGD - pExtent->uDescriptorSector >= 4
|
---|
3169 | && (!pExtent->uSectorRGD || (int64_t)pExtent->uSectorRGD - pExtent->uDescriptorSector >= 4))
|
---|
3170 | {
|
---|
3171 | pExtent->cDescriptorSectors = 4;
|
---|
3172 | pExtent->fMetaDirty = true;
|
---|
3173 | }
|
---|
3174 | /* Read the descriptor from the extent. */
|
---|
3175 | pExtent->pDescData = (char *)RTMemAllocZ(VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
|
---|
3176 | if (!pExtent->pDescData)
|
---|
3177 | {
|
---|
3178 | rc = VERR_NO_MEMORY;
|
---|
3179 | goto out;
|
---|
3180 | }
|
---|
3181 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
3182 | VMDK_SECTOR2BYTE(pExtent->uDescriptorSector),
|
---|
3183 | pExtent->pDescData,
|
---|
3184 | VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors), NULL);
|
---|
3185 | AssertRC(rc);
|
---|
3186 | if (RT_FAILURE(rc))
|
---|
3187 | {
|
---|
3188 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pExtent->pszFullname);
|
---|
3189 | goto out;
|
---|
3190 | }
|
---|
3191 |
|
---|
3192 | rc = vmdkParseDescriptor(pImage, pExtent->pDescData,
|
---|
3193 | VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
|
---|
3194 | if (RT_FAILURE(rc))
|
---|
3195 | goto out;
|
---|
3196 |
|
---|
3197 | if ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
|
---|
3198 | && uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
|
---|
3199 | {
|
---|
3200 | rc = VERR_NOT_SUPPORTED;
|
---|
3201 | goto out;
|
---|
3202 | }
|
---|
3203 |
|
---|
3204 | rc = vmdkReadMetaExtent(pImage, pExtent);
|
---|
3205 | if (RT_FAILURE(rc))
|
---|
3206 | goto out;
|
---|
3207 |
|
---|
3208 | /* Mark the extent as unclean if opened in read-write mode. */
|
---|
3209 | if ( !(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
3210 | && !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
3211 | {
|
---|
3212 | pExtent->fUncleanShutdown = true;
|
---|
3213 | pExtent->fMetaDirty = true;
|
---|
3214 | }
|
---|
3215 | }
|
---|
3216 | else
|
---|
3217 | {
|
---|
3218 | /* Allocate at least 10K, and make sure that there is 5K free space
|
---|
3219 | * in case new entries need to be added to the descriptor. Never
|
---|
3220 | * allocate more than 128K, because that's no valid descriptor file
|
---|
3221 | * and will result in the correct "truncated read" error handling. */
|
---|
3222 | uint64_t cbFileSize;
|
---|
3223 | rc = vdIfIoIntFileGetSize(pImage->pIfIo, pFile->pStorage, &cbFileSize);
|
---|
3224 | if (RT_FAILURE(rc))
|
---|
3225 | goto out;
|
---|
3226 |
|
---|
3227 | /* If the descriptor file is shorter than 50 bytes it can't be valid. */
|
---|
3228 | if (cbFileSize < 50)
|
---|
3229 | {
|
---|
3230 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor in '%s' is too short"), pImage->pszFilename);
|
---|
3231 | goto out;
|
---|
3232 | }
|
---|
3233 |
|
---|
3234 | uint64_t cbSize = cbFileSize;
|
---|
3235 | if (cbSize % VMDK_SECTOR2BYTE(10))
|
---|
3236 | cbSize += VMDK_SECTOR2BYTE(20) - cbSize % VMDK_SECTOR2BYTE(10);
|
---|
3237 | else
|
---|
3238 | cbSize += VMDK_SECTOR2BYTE(10);
|
---|
3239 | cbSize = RT_MIN(cbSize, _128K);
|
---|
3240 | pImage->cbDescAlloc = RT_MAX(VMDK_SECTOR2BYTE(20), cbSize);
|
---|
3241 | pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
|
---|
3242 | if (!pImage->pDescData)
|
---|
3243 | {
|
---|
3244 | rc = VERR_NO_MEMORY;
|
---|
3245 | goto out;
|
---|
3246 | }
|
---|
3247 |
|
---|
3248 | /* Don't reread the place where the magic would live in a sparse
|
---|
3249 | * image if it's a descriptor based one. */
|
---|
3250 | memcpy(pImage->pDescData, &u32Magic, sizeof(u32Magic));
|
---|
3251 | size_t cbRead;
|
---|
3252 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pFile->pStorage, sizeof(u32Magic),
|
---|
3253 | pImage->pDescData + sizeof(u32Magic),
|
---|
3254 | RT_MIN(pImage->cbDescAlloc - sizeof(u32Magic),
|
---|
3255 | cbFileSize - sizeof(u32Magic)),
|
---|
3256 | &cbRead);
|
---|
3257 | if (RT_FAILURE(rc))
|
---|
3258 | {
|
---|
3259 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pImage->pszFilename);
|
---|
3260 | goto out;
|
---|
3261 | }
|
---|
3262 | cbRead += sizeof(u32Magic);
|
---|
3263 | if (cbRead == pImage->cbDescAlloc)
|
---|
3264 | {
|
---|
3265 | /* Likely the read is truncated. Better fail a bit too early
|
---|
3266 | * (normally the descriptor is much smaller than our buffer). */
|
---|
3267 | rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: cannot read descriptor in '%s'"), pImage->pszFilename);
|
---|
3268 | goto out;
|
---|
3269 | }
|
---|
3270 |
|
---|
3271 | rc = vmdkParseDescriptor(pImage, pImage->pDescData,
|
---|
3272 | pImage->cbDescAlloc);
|
---|
3273 | if (RT_FAILURE(rc))
|
---|
3274 | goto out;
|
---|
3275 |
|
---|
3276 | /*
|
---|
3277 | * We have to check for the asynchronous open flag. The
|
---|
3278 | * extents are parsed and the type of all are known now.
|
---|
3279 | * Check if every extent is either FLAT or ZERO.
|
---|
3280 | */
|
---|
3281 | if (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
|
---|
3282 | {
|
---|
3283 | unsigned cFlatExtents = 0;
|
---|
3284 |
|
---|
3285 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
3286 | {
|
---|
3287 | pExtent = &pImage->pExtents[i];
|
---|
3288 |
|
---|
3289 | if (( pExtent->enmType != VMDKETYPE_FLAT
|
---|
3290 | && pExtent->enmType != VMDKETYPE_ZERO
|
---|
3291 | && pExtent->enmType != VMDKETYPE_VMFS)
|
---|
3292 | || ((pImage->pExtents[i].enmType == VMDKETYPE_FLAT) && (cFlatExtents > 0)))
|
---|
3293 | {
|
---|
3294 | /*
|
---|
3295 | * Opened image contains at least one none flat or zero extent.
|
---|
3296 | * Return error but don't set error message as the caller
|
---|
3297 | * has the chance to open in non async I/O mode.
|
---|
3298 | */
|
---|
3299 | rc = VERR_NOT_SUPPORTED;
|
---|
3300 | goto out;
|
---|
3301 | }
|
---|
3302 | if (pExtent->enmType == VMDKETYPE_FLAT)
|
---|
3303 | cFlatExtents++;
|
---|
3304 | }
|
---|
3305 | }
|
---|
3306 |
|
---|
3307 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
3308 | {
|
---|
3309 | pExtent = &pImage->pExtents[i];
|
---|
3310 |
|
---|
3311 | if (pExtent->pszBasename)
|
---|
3312 | {
|
---|
3313 | /* Hack to figure out whether the specified name in the
|
---|
3314 | * extent descriptor is absolute. Doesn't always work, but
|
---|
3315 | * should be good enough for now. */
|
---|
3316 | char *pszFullname;
|
---|
3317 | /** @todo implement proper path absolute check. */
|
---|
3318 | if (pExtent->pszBasename[0] == RTPATH_SLASH)
|
---|
3319 | {
|
---|
3320 | pszFullname = RTStrDup(pExtent->pszBasename);
|
---|
3321 | if (!pszFullname)
|
---|
3322 | {
|
---|
3323 | rc = VERR_NO_MEMORY;
|
---|
3324 | goto out;
|
---|
3325 | }
|
---|
3326 | }
|
---|
3327 | else
|
---|
3328 | {
|
---|
3329 | char *pszDirname = RTStrDup(pImage->pszFilename);
|
---|
3330 | if (!pszDirname)
|
---|
3331 | {
|
---|
3332 | rc = VERR_NO_MEMORY;
|
---|
3333 | goto out;
|
---|
3334 | }
|
---|
3335 | RTPathStripFilename(pszDirname);
|
---|
3336 | pszFullname = RTPathJoinA(pszDirname, pExtent->pszBasename);
|
---|
3337 | RTStrFree(pszDirname);
|
---|
3338 | if (!pszFullname)
|
---|
3339 | {
|
---|
3340 | rc = VERR_NO_STR_MEMORY;
|
---|
3341 | goto out;
|
---|
3342 | }
|
---|
3343 | }
|
---|
3344 | pExtent->pszFullname = pszFullname;
|
---|
3345 | }
|
---|
3346 | else
|
---|
3347 | pExtent->pszFullname = NULL;
|
---|
3348 |
|
---|
3349 | switch (pExtent->enmType)
|
---|
3350 | {
|
---|
3351 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
3352 | rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
|
---|
3353 | VDOpenFlagsToFileOpenFlags(uOpenFlags,
|
---|
3354 | false /* fCreate */),
|
---|
3355 | false /* fAsyncIO */);
|
---|
3356 | if (RT_FAILURE(rc))
|
---|
3357 | {
|
---|
3358 | /* Do NOT signal an appropriate error here, as the VD
|
---|
3359 | * layer has the choice of retrying the open if it
|
---|
3360 | * failed. */
|
---|
3361 | goto out;
|
---|
3362 | }
|
---|
3363 | rc = vmdkReadBinaryMetaExtent(pImage, pExtent,
|
---|
3364 | false /* fMagicAlreadyRead */);
|
---|
3365 | if (RT_FAILURE(rc))
|
---|
3366 | goto out;
|
---|
3367 | rc = vmdkReadMetaExtent(pImage, pExtent);
|
---|
3368 | if (RT_FAILURE(rc))
|
---|
3369 | goto out;
|
---|
3370 |
|
---|
3371 | /* Mark extent as unclean if opened in read-write mode. */
|
---|
3372 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
3373 | {
|
---|
3374 | pExtent->fUncleanShutdown = true;
|
---|
3375 | pExtent->fMetaDirty = true;
|
---|
3376 | }
|
---|
3377 | break;
|
---|
3378 | case VMDKETYPE_VMFS:
|
---|
3379 | case VMDKETYPE_FLAT:
|
---|
3380 | rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
|
---|
3381 | VDOpenFlagsToFileOpenFlags(uOpenFlags,
|
---|
3382 | false /* fCreate */),
|
---|
3383 | true /* fAsyncIO */);
|
---|
3384 | if (RT_FAILURE(rc))
|
---|
3385 | {
|
---|
3386 | /* Do NOT signal an appropriate error here, as the VD
|
---|
3387 | * layer has the choice of retrying the open if it
|
---|
3388 | * failed. */
|
---|
3389 | goto out;
|
---|
3390 | }
|
---|
3391 | break;
|
---|
3392 | case VMDKETYPE_ZERO:
|
---|
3393 | /* Nothing to do. */
|
---|
3394 | break;
|
---|
3395 | default:
|
---|
3396 | AssertMsgFailed(("unknown vmdk extent type %d\n", pExtent->enmType));
|
---|
3397 | }
|
---|
3398 | }
|
---|
3399 | }
|
---|
3400 |
|
---|
3401 | /* Make sure this is not reached accidentally with an error status. */
|
---|
3402 | AssertRC(rc);
|
---|
3403 |
|
---|
3404 | /* Determine PCHS geometry if not set. */
|
---|
3405 | if (pImage->PCHSGeometry.cCylinders == 0)
|
---|
3406 | {
|
---|
3407 | uint64_t cCylinders = VMDK_BYTE2SECTOR(pImage->cbSize)
|
---|
3408 | / pImage->PCHSGeometry.cHeads
|
---|
3409 | / pImage->PCHSGeometry.cSectors;
|
---|
3410 | pImage->PCHSGeometry.cCylinders = (unsigned)RT_MIN(cCylinders, 16383);
|
---|
3411 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
3412 | && !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
3413 | {
|
---|
3414 | rc = vmdkDescSetPCHSGeometry(pImage, &pImage->PCHSGeometry);
|
---|
3415 | AssertRC(rc);
|
---|
3416 | }
|
---|
3417 | }
|
---|
3418 |
|
---|
3419 | /* Update the image metadata now in case has changed. */
|
---|
3420 | rc = vmdkFlushImage(pImage);
|
---|
3421 | if (RT_FAILURE(rc))
|
---|
3422 | goto out;
|
---|
3423 |
|
---|
3424 | /* Figure out a few per-image constants from the extents. */
|
---|
3425 | pImage->cbSize = 0;
|
---|
3426 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
3427 | {
|
---|
3428 | pExtent = &pImage->pExtents[i];
|
---|
3429 | if ( pExtent->enmType == VMDKETYPE_HOSTED_SPARSE
|
---|
3430 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
3431 | || pExtent->enmType == VMDKETYPE_ESX_SPARSE
|
---|
3432 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
3433 | )
|
---|
3434 | {
|
---|
3435 | /* Here used to be a check whether the nominal size of an extent
|
---|
3436 | * is a multiple of the grain size. The spec says that this is
|
---|
3437 | * always the case, but unfortunately some files out there in the
|
---|
3438 | * wild violate the spec (e.g. ReactOS 0.3.1). */
|
---|
3439 | }
|
---|
3440 | pImage->cbSize += VMDK_SECTOR2BYTE(pExtent->cNominalSectors);
|
---|
3441 | }
|
---|
3442 |
|
---|
3443 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
3444 | {
|
---|
3445 | pExtent = &pImage->pExtents[i];
|
---|
3446 | if ( pImage->pExtents[i].enmType == VMDKETYPE_FLAT
|
---|
3447 | || pImage->pExtents[i].enmType == VMDKETYPE_ZERO)
|
---|
3448 | {
|
---|
3449 | pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED;
|
---|
3450 | break;
|
---|
3451 | }
|
---|
3452 | }
|
---|
3453 |
|
---|
3454 | if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
3455 | || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
3456 | || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
|
---|
3457 | rc = vmdkAllocateGrainTableCache(pImage);
|
---|
3458 |
|
---|
3459 | out:
|
---|
3460 | if (RT_FAILURE(rc))
|
---|
3461 | vmdkFreeImage(pImage, false);
|
---|
3462 | return rc;
|
---|
3463 | }
|
---|
3464 |
|
---|
3465 | /**
|
---|
3466 | * Internal: create VMDK images for raw disk/partition access.
|
---|
3467 | */
|
---|
3468 | static int vmdkCreateRawImage(PVMDKIMAGE pImage, const PVBOXHDDRAW pRaw,
|
---|
3469 | uint64_t cbSize)
|
---|
3470 | {
|
---|
3471 | int rc = VINF_SUCCESS;
|
---|
3472 | PVMDKEXTENT pExtent;
|
---|
3473 |
|
---|
3474 | if (pRaw->fRawDisk)
|
---|
3475 | {
|
---|
3476 | /* Full raw disk access. This requires setting up a descriptor
|
---|
3477 | * file and open the (flat) raw disk. */
|
---|
3478 | rc = vmdkCreateExtents(pImage, 1);
|
---|
3479 | if (RT_FAILURE(rc))
|
---|
3480 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
|
---|
3481 | pExtent = &pImage->pExtents[0];
|
---|
3482 | /* Create raw disk descriptor file. */
|
---|
3483 | rc = vmdkFileOpen(pImage, &pImage->pFile, pImage->pszFilename,
|
---|
3484 | VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
|
---|
3485 | true /* fCreate */),
|
---|
3486 | false /* fAsyncIO */);
|
---|
3487 | if (RT_FAILURE(rc))
|
---|
3488 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pImage->pszFilename);
|
---|
3489 |
|
---|
3490 | /* Set up basename for extent description. Cannot use StrDup. */
|
---|
3491 | size_t cbBasename = strlen(pRaw->pszRawDisk) + 1;
|
---|
3492 | char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
|
---|
3493 | if (!pszBasename)
|
---|
3494 | return VERR_NO_MEMORY;
|
---|
3495 | memcpy(pszBasename, pRaw->pszRawDisk, cbBasename);
|
---|
3496 | pExtent->pszBasename = pszBasename;
|
---|
3497 | /* For raw disks the full name is identical to the base name. */
|
---|
3498 | pExtent->pszFullname = RTStrDup(pszBasename);
|
---|
3499 | if (!pExtent->pszFullname)
|
---|
3500 | return VERR_NO_MEMORY;
|
---|
3501 | pExtent->enmType = VMDKETYPE_FLAT;
|
---|
3502 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
|
---|
3503 | pExtent->uSectorOffset = 0;
|
---|
3504 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
3505 | pExtent->fMetaDirty = false;
|
---|
3506 |
|
---|
3507 | /* Open flat image, the raw disk. */
|
---|
3508 | rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
|
---|
3509 | VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
3510 | false /* fCreate */),
|
---|
3511 | false /* fAsyncIO */);
|
---|
3512 | if (RT_FAILURE(rc))
|
---|
3513 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not open raw disk file '%s'"), pExtent->pszFullname);
|
---|
3514 | }
|
---|
3515 | else
|
---|
3516 | {
|
---|
3517 | /* Raw partition access. This requires setting up a descriptor
|
---|
3518 | * file, write the partition information to a flat extent and
|
---|
3519 | * open all the (flat) raw disk partitions. */
|
---|
3520 |
|
---|
3521 | /* First pass over the partition data areas to determine how many
|
---|
3522 | * extents we need. One data area can require up to 2 extents, as
|
---|
3523 | * it might be necessary to skip over unpartitioned space. */
|
---|
3524 | unsigned cExtents = 0;
|
---|
3525 | uint64_t uStart = 0;
|
---|
3526 | for (unsigned i = 0; i < pRaw->cPartDescs; i++)
|
---|
3527 | {
|
---|
3528 | PVBOXHDDRAWPARTDESC pPart = &pRaw->pPartDescs[i];
|
---|
3529 | if (uStart > pPart->uStart)
|
---|
3530 | 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);
|
---|
3531 |
|
---|
3532 | if (uStart < pPart->uStart)
|
---|
3533 | cExtents++;
|
---|
3534 | uStart = pPart->uStart + pPart->cbData;
|
---|
3535 | cExtents++;
|
---|
3536 | }
|
---|
3537 | /* Another extent for filling up the rest of the image. */
|
---|
3538 | if (uStart != cbSize)
|
---|
3539 | cExtents++;
|
---|
3540 |
|
---|
3541 | rc = vmdkCreateExtents(pImage, cExtents);
|
---|
3542 | if (RT_FAILURE(rc))
|
---|
3543 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
|
---|
3544 |
|
---|
3545 | /* Create raw partition descriptor file. */
|
---|
3546 | rc = vmdkFileOpen(pImage, &pImage->pFile, pImage->pszFilename,
|
---|
3547 | VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
|
---|
3548 | true /* fCreate */),
|
---|
3549 | false /* fAsyncIO */);
|
---|
3550 | if (RT_FAILURE(rc))
|
---|
3551 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pImage->pszFilename);
|
---|
3552 |
|
---|
3553 | /* Create base filename for the partition table extent. */
|
---|
3554 | /** @todo remove fixed buffer without creating memory leaks. */
|
---|
3555 | char pszPartition[1024];
|
---|
3556 | const char *pszBase = RTPathFilename(pImage->pszFilename);
|
---|
3557 | const char *pszExt = RTPathExt(pszBase);
|
---|
3558 | if (pszExt == NULL)
|
---|
3559 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: invalid filename '%s'"), pImage->pszFilename);
|
---|
3560 | char *pszBaseBase = RTStrDup(pszBase);
|
---|
3561 | if (!pszBaseBase)
|
---|
3562 | return VERR_NO_MEMORY;
|
---|
3563 | RTPathStripExt(pszBaseBase);
|
---|
3564 | RTStrPrintf(pszPartition, sizeof(pszPartition), "%s-pt%s",
|
---|
3565 | pszBaseBase, pszExt);
|
---|
3566 | RTStrFree(pszBaseBase);
|
---|
3567 |
|
---|
3568 | /* Second pass over the partitions, now define all extents. */
|
---|
3569 | uint64_t uPartOffset = 0;
|
---|
3570 | cExtents = 0;
|
---|
3571 | uStart = 0;
|
---|
3572 | for (unsigned i = 0; i < pRaw->cPartDescs; i++)
|
---|
3573 | {
|
---|
3574 | PVBOXHDDRAWPARTDESC pPart = &pRaw->pPartDescs[i];
|
---|
3575 | pExtent = &pImage->pExtents[cExtents++];
|
---|
3576 |
|
---|
3577 | if (uStart < pPart->uStart)
|
---|
3578 | {
|
---|
3579 | pExtent->pszBasename = NULL;
|
---|
3580 | pExtent->pszFullname = NULL;
|
---|
3581 | pExtent->enmType = VMDKETYPE_ZERO;
|
---|
3582 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->uStart - uStart);
|
---|
3583 | pExtent->uSectorOffset = 0;
|
---|
3584 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
3585 | pExtent->fMetaDirty = false;
|
---|
3586 | /* go to next extent */
|
---|
3587 | pExtent = &pImage->pExtents[cExtents++];
|
---|
3588 | }
|
---|
3589 | uStart = pPart->uStart + pPart->cbData;
|
---|
3590 |
|
---|
3591 | if (pPart->pvPartitionData)
|
---|
3592 | {
|
---|
3593 | /* Set up basename for extent description. Can't use StrDup. */
|
---|
3594 | size_t cbBasename = strlen(pszPartition) + 1;
|
---|
3595 | char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
|
---|
3596 | if (!pszBasename)
|
---|
3597 | return VERR_NO_MEMORY;
|
---|
3598 | memcpy(pszBasename, pszPartition, cbBasename);
|
---|
3599 | pExtent->pszBasename = pszBasename;
|
---|
3600 |
|
---|
3601 | /* Set up full name for partition extent. */
|
---|
3602 | char *pszDirname = RTStrDup(pImage->pszFilename);
|
---|
3603 | if (!pszDirname)
|
---|
3604 | return VERR_NO_STR_MEMORY;
|
---|
3605 | RTPathStripFilename(pszDirname);
|
---|
3606 | char *pszFullname = RTPathJoinA(pszDirname, pExtent->pszBasename);
|
---|
3607 | RTStrFree(pszDirname);
|
---|
3608 | if (!pszDirname)
|
---|
3609 | return VERR_NO_STR_MEMORY;
|
---|
3610 | pExtent->pszFullname = pszFullname;
|
---|
3611 | pExtent->enmType = VMDKETYPE_FLAT;
|
---|
3612 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbData);
|
---|
3613 | pExtent->uSectorOffset = uPartOffset;
|
---|
3614 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
3615 | pExtent->fMetaDirty = false;
|
---|
3616 |
|
---|
3617 | /* Create partition table flat image. */
|
---|
3618 | rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
|
---|
3619 | VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
|
---|
3620 | true /* fCreate */),
|
---|
3621 | false /* fAsyncIO */);
|
---|
3622 | if (RT_FAILURE(rc))
|
---|
3623 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new partition data file '%s'"), pExtent->pszFullname);
|
---|
3624 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
3625 | VMDK_SECTOR2BYTE(uPartOffset),
|
---|
3626 | pPart->pvPartitionData,
|
---|
3627 | pPart->cbData, NULL);
|
---|
3628 | if (RT_FAILURE(rc))
|
---|
3629 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not write partition data to '%s'"), pExtent->pszFullname);
|
---|
3630 | uPartOffset += VMDK_BYTE2SECTOR(pPart->cbData);
|
---|
3631 | }
|
---|
3632 | else
|
---|
3633 | {
|
---|
3634 | if (pPart->pszRawDevice)
|
---|
3635 | {
|
---|
3636 | /* Set up basename for extent descr. Can't use StrDup. */
|
---|
3637 | size_t cbBasename = strlen(pPart->pszRawDevice) + 1;
|
---|
3638 | char *pszBasename = (char *)RTMemTmpAlloc(cbBasename);
|
---|
3639 | if (!pszBasename)
|
---|
3640 | return VERR_NO_MEMORY;
|
---|
3641 | memcpy(pszBasename, pPart->pszRawDevice, cbBasename);
|
---|
3642 | pExtent->pszBasename = pszBasename;
|
---|
3643 | /* For raw disks full name is identical to base name. */
|
---|
3644 | pExtent->pszFullname = RTStrDup(pszBasename);
|
---|
3645 | if (!pExtent->pszFullname)
|
---|
3646 | return VERR_NO_MEMORY;
|
---|
3647 | pExtent->enmType = VMDKETYPE_FLAT;
|
---|
3648 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbData);
|
---|
3649 | pExtent->uSectorOffset = VMDK_BYTE2SECTOR(pPart->uStartOffset);
|
---|
3650 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
3651 | pExtent->fMetaDirty = false;
|
---|
3652 |
|
---|
3653 | /* Open flat image, the raw partition. */
|
---|
3654 | rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
|
---|
3655 | VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
3656 | false /* fCreate */),
|
---|
3657 | false /* fAsyncIO */);
|
---|
3658 | if (RT_FAILURE(rc))
|
---|
3659 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not open raw partition file '%s'"), pExtent->pszFullname);
|
---|
3660 | }
|
---|
3661 | else
|
---|
3662 | {
|
---|
3663 | pExtent->pszBasename = NULL;
|
---|
3664 | pExtent->pszFullname = NULL;
|
---|
3665 | pExtent->enmType = VMDKETYPE_ZERO;
|
---|
3666 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(pPart->cbData);
|
---|
3667 | pExtent->uSectorOffset = 0;
|
---|
3668 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
3669 | pExtent->fMetaDirty = false;
|
---|
3670 | }
|
---|
3671 | }
|
---|
3672 | }
|
---|
3673 | /* Another extent for filling up the rest of the image. */
|
---|
3674 | if (uStart != cbSize)
|
---|
3675 | {
|
---|
3676 | pExtent = &pImage->pExtents[cExtents++];
|
---|
3677 | pExtent->pszBasename = NULL;
|
---|
3678 | pExtent->pszFullname = NULL;
|
---|
3679 | pExtent->enmType = VMDKETYPE_ZERO;
|
---|
3680 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize - uStart);
|
---|
3681 | pExtent->uSectorOffset = 0;
|
---|
3682 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
3683 | pExtent->fMetaDirty = false;
|
---|
3684 | }
|
---|
3685 | }
|
---|
3686 |
|
---|
3687 | rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType",
|
---|
3688 | pRaw->fRawDisk ?
|
---|
3689 | "fullDevice" : "partitionedDevice");
|
---|
3690 | if (RT_FAILURE(rc))
|
---|
3691 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pImage->pszFilename);
|
---|
3692 | return rc;
|
---|
3693 | }
|
---|
3694 |
|
---|
3695 | /**
|
---|
3696 | * Internal: create a regular (i.e. file-backed) VMDK image.
|
---|
3697 | */
|
---|
3698 | static int vmdkCreateRegularImage(PVMDKIMAGE pImage, uint64_t cbSize,
|
---|
3699 | unsigned uImageFlags,
|
---|
3700 | PFNVDPROGRESS pfnProgress, void *pvUser,
|
---|
3701 | unsigned uPercentStart, unsigned uPercentSpan)
|
---|
3702 | {
|
---|
3703 | int rc = VINF_SUCCESS;
|
---|
3704 | unsigned cExtents = 1;
|
---|
3705 | uint64_t cbOffset = 0;
|
---|
3706 | uint64_t cbRemaining = cbSize;
|
---|
3707 |
|
---|
3708 | if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
|
---|
3709 | {
|
---|
3710 | cExtents = cbSize / VMDK_2G_SPLIT_SIZE;
|
---|
3711 | /* Do proper extent computation: need one smaller extent if the total
|
---|
3712 | * size isn't evenly divisible by the split size. */
|
---|
3713 | if (cbSize % VMDK_2G_SPLIT_SIZE)
|
---|
3714 | cExtents++;
|
---|
3715 | }
|
---|
3716 | rc = vmdkCreateExtents(pImage, cExtents);
|
---|
3717 | if (RT_FAILURE(rc))
|
---|
3718 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
|
---|
3719 |
|
---|
3720 | /* Basename strings needed for constructing the extent names. */
|
---|
3721 | char *pszBasenameSubstr = RTPathFilename(pImage->pszFilename);
|
---|
3722 | AssertPtr(pszBasenameSubstr);
|
---|
3723 | size_t cbBasenameSubstr = strlen(pszBasenameSubstr) + 1;
|
---|
3724 |
|
---|
3725 | /* Create separate descriptor file if necessary. */
|
---|
3726 | if (cExtents != 1 || (uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
3727 | {
|
---|
3728 | rc = vmdkFileOpen(pImage, &pImage->pFile, pImage->pszFilename,
|
---|
3729 | VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
|
---|
3730 | true /* fCreate */),
|
---|
3731 | false /* fAsyncIO */);
|
---|
3732 | if (RT_FAILURE(rc))
|
---|
3733 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new sparse descriptor file '%s'"), pImage->pszFilename);
|
---|
3734 | }
|
---|
3735 | else
|
---|
3736 | pImage->pFile = NULL;
|
---|
3737 |
|
---|
3738 | /* Set up all extents. */
|
---|
3739 | for (unsigned i = 0; i < cExtents; i++)
|
---|
3740 | {
|
---|
3741 | PVMDKEXTENT pExtent = &pImage->pExtents[i];
|
---|
3742 | uint64_t cbExtent = cbRemaining;
|
---|
3743 |
|
---|
3744 | /* Set up fullname/basename for extent description. Cannot use StrDup
|
---|
3745 | * for basename, as it is not guaranteed that the memory can be freed
|
---|
3746 | * with RTMemTmpFree, which must be used as in other code paths
|
---|
3747 | * StrDup is not usable. */
|
---|
3748 | if (cExtents == 1 && !(uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
3749 | {
|
---|
3750 | char *pszBasename = (char *)RTMemTmpAlloc(cbBasenameSubstr);
|
---|
3751 | if (!pszBasename)
|
---|
3752 | return VERR_NO_MEMORY;
|
---|
3753 | memcpy(pszBasename, pszBasenameSubstr, cbBasenameSubstr);
|
---|
3754 | pExtent->pszBasename = pszBasename;
|
---|
3755 | }
|
---|
3756 | else
|
---|
3757 | {
|
---|
3758 | char *pszBasenameExt = RTPathExt(pszBasenameSubstr);
|
---|
3759 | char *pszBasenameBase = RTStrDup(pszBasenameSubstr);
|
---|
3760 | RTPathStripExt(pszBasenameBase);
|
---|
3761 | char *pszTmp;
|
---|
3762 | size_t cbTmp;
|
---|
3763 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
3764 | {
|
---|
3765 | if (cExtents == 1)
|
---|
3766 | RTStrAPrintf(&pszTmp, "%s-flat%s", pszBasenameBase,
|
---|
3767 | pszBasenameExt);
|
---|
3768 | else
|
---|
3769 | RTStrAPrintf(&pszTmp, "%s-f%03d%s", pszBasenameBase,
|
---|
3770 | i+1, pszBasenameExt);
|
---|
3771 | }
|
---|
3772 | else
|
---|
3773 | RTStrAPrintf(&pszTmp, "%s-s%03d%s", pszBasenameBase, i+1,
|
---|
3774 | pszBasenameExt);
|
---|
3775 | RTStrFree(pszBasenameBase);
|
---|
3776 | if (!pszTmp)
|
---|
3777 | return VERR_NO_STR_MEMORY;
|
---|
3778 | cbTmp = strlen(pszTmp) + 1;
|
---|
3779 | char *pszBasename = (char *)RTMemTmpAlloc(cbTmp);
|
---|
3780 | if (!pszBasename)
|
---|
3781 | return VERR_NO_MEMORY;
|
---|
3782 | memcpy(pszBasename, pszTmp, cbTmp);
|
---|
3783 | RTStrFree(pszTmp);
|
---|
3784 | pExtent->pszBasename = pszBasename;
|
---|
3785 | if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
|
---|
3786 | cbExtent = RT_MIN(cbRemaining, VMDK_2G_SPLIT_SIZE);
|
---|
3787 | }
|
---|
3788 | char *pszBasedirectory = RTStrDup(pImage->pszFilename);
|
---|
3789 | if (!pszBasedirectory)
|
---|
3790 | return VERR_NO_STR_MEMORY;
|
---|
3791 | RTPathStripFilename(pszBasedirectory);
|
---|
3792 | char *pszFullname = RTPathJoinA(pszBasedirectory, pExtent->pszBasename);
|
---|
3793 | RTStrFree(pszBasedirectory);
|
---|
3794 | if (!pszFullname)
|
---|
3795 | return VERR_NO_STR_MEMORY;
|
---|
3796 | pExtent->pszFullname = pszFullname;
|
---|
3797 |
|
---|
3798 | /* Create file for extent. */
|
---|
3799 | rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
|
---|
3800 | VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
|
---|
3801 | true /* fCreate */),
|
---|
3802 | false /* fAsyncIO */);
|
---|
3803 | if (RT_FAILURE(rc))
|
---|
3804 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pExtent->pszFullname);
|
---|
3805 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
3806 | {
|
---|
3807 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pExtent->pFile->pStorage, cbExtent);
|
---|
3808 | if (RT_FAILURE(rc))
|
---|
3809 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
|
---|
3810 |
|
---|
3811 | /* Fill image with zeroes. We do this for every fixed-size image since on some systems
|
---|
3812 | * (for example Windows Vista), it takes ages to write a block near the end of a sparse
|
---|
3813 | * file and the guest could complain about an ATA timeout. */
|
---|
3814 |
|
---|
3815 | /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
|
---|
3816 | * Currently supported file systems are ext4 and ocfs2. */
|
---|
3817 |
|
---|
3818 | /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
|
---|
3819 | const size_t cbBuf = 128 * _1K;
|
---|
3820 | void *pvBuf = RTMemTmpAllocZ(cbBuf);
|
---|
3821 | if (!pvBuf)
|
---|
3822 | return VERR_NO_MEMORY;
|
---|
3823 |
|
---|
3824 | uint64_t uOff = 0;
|
---|
3825 | /* Write data to all image blocks. */
|
---|
3826 | while (uOff < cbExtent)
|
---|
3827 | {
|
---|
3828 | unsigned cbChunk = (unsigned)RT_MIN(cbExtent, cbBuf);
|
---|
3829 |
|
---|
3830 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
3831 | uOff, pvBuf, cbChunk, NULL);
|
---|
3832 | if (RT_FAILURE(rc))
|
---|
3833 | {
|
---|
3834 | RTMemFree(pvBuf);
|
---|
3835 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: writing block failed for '%s'"), pImage->pszFilename);
|
---|
3836 | }
|
---|
3837 |
|
---|
3838 | uOff += cbChunk;
|
---|
3839 |
|
---|
3840 | if (pfnProgress)
|
---|
3841 | {
|
---|
3842 | rc = pfnProgress(pvUser,
|
---|
3843 | uPercentStart + (cbOffset + uOff) * uPercentSpan / cbSize);
|
---|
3844 | if (RT_FAILURE(rc))
|
---|
3845 | {
|
---|
3846 | RTMemFree(pvBuf);
|
---|
3847 | return rc;
|
---|
3848 | }
|
---|
3849 | }
|
---|
3850 | }
|
---|
3851 | RTMemTmpFree(pvBuf);
|
---|
3852 | }
|
---|
3853 |
|
---|
3854 | /* Place descriptor file information (where integrated). */
|
---|
3855 | if (cExtents == 1 && !(uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
3856 | {
|
---|
3857 | pExtent->uDescriptorSector = 1;
|
---|
3858 | pExtent->cDescriptorSectors = VMDK_BYTE2SECTOR(pImage->cbDescAlloc);
|
---|
3859 | /* The descriptor is part of the (only) extent. */
|
---|
3860 | pExtent->pDescData = pImage->pDescData;
|
---|
3861 | pImage->pDescData = NULL;
|
---|
3862 | }
|
---|
3863 |
|
---|
3864 | if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
3865 | {
|
---|
3866 | uint64_t cSectorsPerGDE, cSectorsPerGD;
|
---|
3867 | pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
|
---|
3868 | pExtent->cSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64(cbExtent, _64K));
|
---|
3869 | pExtent->cSectorsPerGrain = VMDK_BYTE2SECTOR(_64K);
|
---|
3870 | pExtent->cGTEntries = 512;
|
---|
3871 | cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
|
---|
3872 | pExtent->cSectorsPerGDE = cSectorsPerGDE;
|
---|
3873 | pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
|
---|
3874 | cSectorsPerGD = (pExtent->cGDEntries + (512 / sizeof(uint32_t) - 1)) / (512 / sizeof(uint32_t));
|
---|
3875 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
3876 | {
|
---|
3877 | /* The spec says version is 1 for all VMDKs, but the vast
|
---|
3878 | * majority of streamOptimized VMDKs actually contain
|
---|
3879 | * version 3 - so go with the majority. Both are accepted. */
|
---|
3880 | pExtent->uVersion = 3;
|
---|
3881 | pExtent->uCompression = VMDK_COMPRESSION_DEFLATE;
|
---|
3882 | }
|
---|
3883 | }
|
---|
3884 | else
|
---|
3885 | {
|
---|
3886 | if (uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
|
---|
3887 | pExtent->enmType = VMDKETYPE_VMFS;
|
---|
3888 | else
|
---|
3889 | pExtent->enmType = VMDKETYPE_FLAT;
|
---|
3890 | }
|
---|
3891 |
|
---|
3892 | pExtent->enmAccess = VMDKACCESS_READWRITE;
|
---|
3893 | pExtent->fUncleanShutdown = true;
|
---|
3894 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbExtent);
|
---|
3895 | pExtent->uSectorOffset = 0;
|
---|
3896 | pExtent->fMetaDirty = true;
|
---|
3897 |
|
---|
3898 | if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
3899 | {
|
---|
3900 | /* fPreAlloc should never be false because VMware can't use such images. */
|
---|
3901 | rc = vmdkCreateGrainDirectory(pImage, pExtent,
|
---|
3902 | RT_MAX( pExtent->uDescriptorSector
|
---|
3903 | + pExtent->cDescriptorSectors,
|
---|
3904 | 1),
|
---|
3905 | true /* fPreAlloc */);
|
---|
3906 | if (RT_FAILURE(rc))
|
---|
3907 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new grain directory in '%s'"), pExtent->pszFullname);
|
---|
3908 | }
|
---|
3909 |
|
---|
3910 | cbOffset += cbExtent;
|
---|
3911 |
|
---|
3912 | if (RT_SUCCESS(rc) && pfnProgress)
|
---|
3913 | pfnProgress(pvUser, uPercentStart + cbOffset * uPercentSpan / cbSize);
|
---|
3914 |
|
---|
3915 | cbRemaining -= cbExtent;
|
---|
3916 | }
|
---|
3917 |
|
---|
3918 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
|
---|
3919 | {
|
---|
3920 | /* VirtualBox doesn't care, but VMWare ESX freaks out if the wrong
|
---|
3921 | * controller type is set in an image. */
|
---|
3922 | rc = vmdkDescDDBSetStr(pImage, &pImage->Descriptor, "ddb.adapterType", "lsilogic");
|
---|
3923 | if (RT_FAILURE(rc))
|
---|
3924 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set controller type to lsilogic in '%s'"), pImage->pszFilename);
|
---|
3925 | }
|
---|
3926 |
|
---|
3927 | const char *pszDescType = NULL;
|
---|
3928 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
3929 | {
|
---|
3930 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
|
---|
3931 | pszDescType = "vmfs";
|
---|
3932 | else
|
---|
3933 | pszDescType = (cExtents == 1)
|
---|
3934 | ? "monolithicFlat" : "twoGbMaxExtentFlat";
|
---|
3935 | }
|
---|
3936 | else
|
---|
3937 | {
|
---|
3938 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
3939 | pszDescType = "streamOptimized";
|
---|
3940 | else
|
---|
3941 | {
|
---|
3942 | pszDescType = (cExtents == 1)
|
---|
3943 | ? "monolithicSparse" : "twoGbMaxExtentSparse";
|
---|
3944 | }
|
---|
3945 | }
|
---|
3946 | rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType",
|
---|
3947 | pszDescType);
|
---|
3948 | if (RT_FAILURE(rc))
|
---|
3949 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pImage->pszFilename);
|
---|
3950 | return rc;
|
---|
3951 | }
|
---|
3952 |
|
---|
3953 | /**
|
---|
3954 | * Internal: Create a real stream optimized VMDK using only linear writes.
|
---|
3955 | */
|
---|
3956 | static int vmdkCreateStreamImage(PVMDKIMAGE pImage, uint64_t cbSize,
|
---|
3957 | unsigned uImageFlags,
|
---|
3958 | PFNVDPROGRESS pfnProgress, void *pvUser,
|
---|
3959 | unsigned uPercentStart, unsigned uPercentSpan)
|
---|
3960 | {
|
---|
3961 | int rc;
|
---|
3962 |
|
---|
3963 | rc = vmdkCreateExtents(pImage, 1);
|
---|
3964 | if (RT_FAILURE(rc))
|
---|
3965 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new extent list in '%s'"), pImage->pszFilename);
|
---|
3966 |
|
---|
3967 | /* Basename strings needed for constructing the extent names. */
|
---|
3968 | const char *pszBasenameSubstr = RTPathFilename(pImage->pszFilename);
|
---|
3969 | AssertPtr(pszBasenameSubstr);
|
---|
3970 | size_t cbBasenameSubstr = strlen(pszBasenameSubstr) + 1;
|
---|
3971 |
|
---|
3972 | /* No separate descriptor file. */
|
---|
3973 | pImage->pFile = NULL;
|
---|
3974 |
|
---|
3975 | /* Set up all extents. */
|
---|
3976 | PVMDKEXTENT pExtent = &pImage->pExtents[0];
|
---|
3977 |
|
---|
3978 | /* Set up fullname/basename for extent description. Cannot use StrDup
|
---|
3979 | * for basename, as it is not guaranteed that the memory can be freed
|
---|
3980 | * with RTMemTmpFree, which must be used as in other code paths
|
---|
3981 | * StrDup is not usable. */
|
---|
3982 | char *pszBasename = (char *)RTMemTmpAlloc(cbBasenameSubstr);
|
---|
3983 | if (!pszBasename)
|
---|
3984 | return VERR_NO_MEMORY;
|
---|
3985 | memcpy(pszBasename, pszBasenameSubstr, cbBasenameSubstr);
|
---|
3986 | pExtent->pszBasename = pszBasename;
|
---|
3987 |
|
---|
3988 | char *pszBasedirectory = RTStrDup(pImage->pszFilename);
|
---|
3989 | RTPathStripFilename(pszBasedirectory);
|
---|
3990 | char *pszFullname = RTPathJoinA(pszBasedirectory, pExtent->pszBasename);
|
---|
3991 | RTStrFree(pszBasedirectory);
|
---|
3992 | if (!pszFullname)
|
---|
3993 | return VERR_NO_STR_MEMORY;
|
---|
3994 | pExtent->pszFullname = pszFullname;
|
---|
3995 |
|
---|
3996 | /* Create file for extent. Make it write only, no reading allowed. */
|
---|
3997 | rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszFullname,
|
---|
3998 | VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
|
---|
3999 | true /* fCreate */)
|
---|
4000 | & ~RTFILE_O_READ,
|
---|
4001 | false /* fAsyncIO */);
|
---|
4002 | if (RT_FAILURE(rc))
|
---|
4003 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pExtent->pszFullname);
|
---|
4004 |
|
---|
4005 | /* Place descriptor file information. */
|
---|
4006 | pExtent->uDescriptorSector = 1;
|
---|
4007 | pExtent->cDescriptorSectors = VMDK_BYTE2SECTOR(pImage->cbDescAlloc);
|
---|
4008 | /* The descriptor is part of the (only) extent. */
|
---|
4009 | pExtent->pDescData = pImage->pDescData;
|
---|
4010 | pImage->pDescData = NULL;
|
---|
4011 |
|
---|
4012 | uint64_t cSectorsPerGDE, cSectorsPerGD;
|
---|
4013 | pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
|
---|
4014 | pExtent->cSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64(cbSize, _64K));
|
---|
4015 | pExtent->cSectorsPerGrain = VMDK_BYTE2SECTOR(_64K);
|
---|
4016 | pExtent->cGTEntries = 512;
|
---|
4017 | cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
|
---|
4018 | pExtent->cSectorsPerGDE = cSectorsPerGDE;
|
---|
4019 | pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
|
---|
4020 | cSectorsPerGD = (pExtent->cGDEntries + (512 / sizeof(uint32_t) - 1)) / (512 / sizeof(uint32_t));
|
---|
4021 |
|
---|
4022 | /* The spec says version is 1 for all VMDKs, but the vast
|
---|
4023 | * majority of streamOptimized VMDKs actually contain
|
---|
4024 | * version 3 - so go with the majority. Both are accepted. */
|
---|
4025 | pExtent->uVersion = 3;
|
---|
4026 | pExtent->uCompression = VMDK_COMPRESSION_DEFLATE;
|
---|
4027 | pExtent->fFooter = true;
|
---|
4028 |
|
---|
4029 | pExtent->enmAccess = VMDKACCESS_READONLY;
|
---|
4030 | pExtent->fUncleanShutdown = false;
|
---|
4031 | pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
|
---|
4032 | pExtent->uSectorOffset = 0;
|
---|
4033 | pExtent->fMetaDirty = true;
|
---|
4034 |
|
---|
4035 | /* Create grain directory, without preallocating it straight away. It will
|
---|
4036 | * be constructed on the fly when writing out the data and written when
|
---|
4037 | * closing the image. The end effect is that the full grain directory is
|
---|
4038 | * allocated, which is a requirement of the VMDK specs. */
|
---|
4039 | rc = vmdkCreateGrainDirectory(pImage, pExtent, VMDK_GD_AT_END,
|
---|
4040 | false /* fPreAlloc */);
|
---|
4041 | if (RT_FAILURE(rc))
|
---|
4042 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new grain directory in '%s'"), pExtent->pszFullname);
|
---|
4043 |
|
---|
4044 | rc = vmdkDescBaseSetStr(pImage, &pImage->Descriptor, "createType",
|
---|
4045 | "streamOptimized");
|
---|
4046 | if (RT_FAILURE(rc))
|
---|
4047 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set the image type in '%s'"), pImage->pszFilename);
|
---|
4048 |
|
---|
4049 | return rc;
|
---|
4050 | }
|
---|
4051 |
|
---|
4052 | /**
|
---|
4053 | * Internal: The actual code for creating any VMDK variant currently in
|
---|
4054 | * existence on hosted environments.
|
---|
4055 | */
|
---|
4056 | static int vmdkCreateImage(PVMDKIMAGE pImage, uint64_t cbSize,
|
---|
4057 | unsigned uImageFlags, const char *pszComment,
|
---|
4058 | PCVDGEOMETRY pPCHSGeometry,
|
---|
4059 | PCVDGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
|
---|
4060 | PFNVDPROGRESS pfnProgress, void *pvUser,
|
---|
4061 | unsigned uPercentStart, unsigned uPercentSpan)
|
---|
4062 | {
|
---|
4063 | int rc;
|
---|
4064 |
|
---|
4065 | pImage->uImageFlags = uImageFlags;
|
---|
4066 |
|
---|
4067 | pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
|
---|
4068 | pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
|
---|
4069 | AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
|
---|
4070 |
|
---|
4071 | rc = vmdkCreateDescriptor(pImage, pImage->pDescData, pImage->cbDescAlloc,
|
---|
4072 | &pImage->Descriptor);
|
---|
4073 | if (RT_FAILURE(rc))
|
---|
4074 | {
|
---|
4075 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new descriptor in '%s'"), pImage->pszFilename);
|
---|
4076 | goto out;
|
---|
4077 | }
|
---|
4078 |
|
---|
4079 | if ( (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
4080 | && (uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK))
|
---|
4081 | {
|
---|
4082 | /* Raw disk image (includes raw partition). */
|
---|
4083 | const PVBOXHDDRAW pRaw = (const PVBOXHDDRAW)pszComment;
|
---|
4084 | /* As the comment is misused, zap it so that no garbage comment
|
---|
4085 | * is set below. */
|
---|
4086 | pszComment = NULL;
|
---|
4087 | rc = vmdkCreateRawImage(pImage, pRaw, cbSize);
|
---|
4088 | }
|
---|
4089 | else
|
---|
4090 | {
|
---|
4091 | if (uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
4092 | {
|
---|
4093 | /* Stream optimized sparse image (monolithic). */
|
---|
4094 | rc = vmdkCreateStreamImage(pImage, cbSize, uImageFlags,
|
---|
4095 | pfnProgress, pvUser, uPercentStart,
|
---|
4096 | uPercentSpan * 95 / 100);
|
---|
4097 | }
|
---|
4098 | else
|
---|
4099 | {
|
---|
4100 | /* Regular fixed or sparse image (monolithic or split). */
|
---|
4101 | rc = vmdkCreateRegularImage(pImage, cbSize, uImageFlags,
|
---|
4102 | pfnProgress, pvUser, uPercentStart,
|
---|
4103 | uPercentSpan * 95 / 100);
|
---|
4104 | }
|
---|
4105 | }
|
---|
4106 |
|
---|
4107 | if (RT_FAILURE(rc))
|
---|
4108 | goto out;
|
---|
4109 |
|
---|
4110 | if (RT_SUCCESS(rc) && pfnProgress)
|
---|
4111 | pfnProgress(pvUser, uPercentStart + uPercentSpan * 98 / 100);
|
---|
4112 |
|
---|
4113 | pImage->cbSize = cbSize;
|
---|
4114 |
|
---|
4115 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
4116 | {
|
---|
4117 | PVMDKEXTENT pExtent = &pImage->pExtents[i];
|
---|
4118 |
|
---|
4119 | rc = vmdkDescExtInsert(pImage, &pImage->Descriptor, pExtent->enmAccess,
|
---|
4120 | pExtent->cNominalSectors, pExtent->enmType,
|
---|
4121 | pExtent->pszBasename, pExtent->uSectorOffset);
|
---|
4122 | if (RT_FAILURE(rc))
|
---|
4123 | {
|
---|
4124 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not insert the extent list into descriptor in '%s'"), pImage->pszFilename);
|
---|
4125 | goto out;
|
---|
4126 | }
|
---|
4127 | }
|
---|
4128 | vmdkDescExtRemoveDummy(pImage, &pImage->Descriptor);
|
---|
4129 |
|
---|
4130 | if ( pPCHSGeometry->cCylinders != 0
|
---|
4131 | && pPCHSGeometry->cHeads != 0
|
---|
4132 | && pPCHSGeometry->cSectors != 0)
|
---|
4133 | {
|
---|
4134 | rc = vmdkDescSetPCHSGeometry(pImage, pPCHSGeometry);
|
---|
4135 | if (RT_FAILURE(rc))
|
---|
4136 | goto out;
|
---|
4137 | }
|
---|
4138 | if ( pLCHSGeometry->cCylinders != 0
|
---|
4139 | && pLCHSGeometry->cHeads != 0
|
---|
4140 | && pLCHSGeometry->cSectors != 0)
|
---|
4141 | {
|
---|
4142 | rc = vmdkDescSetLCHSGeometry(pImage, pLCHSGeometry);
|
---|
4143 | if (RT_FAILURE(rc))
|
---|
4144 | goto out;
|
---|
4145 | }
|
---|
4146 |
|
---|
4147 | pImage->LCHSGeometry = *pLCHSGeometry;
|
---|
4148 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
4149 |
|
---|
4150 | pImage->ImageUuid = *pUuid;
|
---|
4151 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
4152 | VMDK_DDB_IMAGE_UUID, &pImage->ImageUuid);
|
---|
4153 | if (RT_FAILURE(rc))
|
---|
4154 | {
|
---|
4155 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image UUID in new descriptor in '%s'"), pImage->pszFilename);
|
---|
4156 | goto out;
|
---|
4157 | }
|
---|
4158 | RTUuidClear(&pImage->ParentUuid);
|
---|
4159 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
4160 | VMDK_DDB_PARENT_UUID, &pImage->ParentUuid);
|
---|
4161 | if (RT_FAILURE(rc))
|
---|
4162 | {
|
---|
4163 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent image UUID in new descriptor in '%s'"), pImage->pszFilename);
|
---|
4164 | goto out;
|
---|
4165 | }
|
---|
4166 | RTUuidClear(&pImage->ModificationUuid);
|
---|
4167 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
4168 | VMDK_DDB_MODIFICATION_UUID,
|
---|
4169 | &pImage->ModificationUuid);
|
---|
4170 | if (RT_FAILURE(rc))
|
---|
4171 | {
|
---|
4172 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing modification UUID in new descriptor in '%s'"), pImage->pszFilename);
|
---|
4173 | goto out;
|
---|
4174 | }
|
---|
4175 | RTUuidClear(&pImage->ParentModificationUuid);
|
---|
4176 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
4177 | VMDK_DDB_PARENT_MODIFICATION_UUID,
|
---|
4178 | &pImage->ParentModificationUuid);
|
---|
4179 | if (RT_FAILURE(rc))
|
---|
4180 | {
|
---|
4181 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent modification UUID in new descriptor in '%s'"), pImage->pszFilename);
|
---|
4182 | goto out;
|
---|
4183 | }
|
---|
4184 |
|
---|
4185 | rc = vmdkAllocateGrainTableCache(pImage);
|
---|
4186 | if (RT_FAILURE(rc))
|
---|
4187 | goto out;
|
---|
4188 |
|
---|
4189 | rc = vmdkSetImageComment(pImage, pszComment);
|
---|
4190 | if (RT_FAILURE(rc))
|
---|
4191 | {
|
---|
4192 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot set image comment in '%s'"), pImage->pszFilename);
|
---|
4193 | goto out;
|
---|
4194 | }
|
---|
4195 |
|
---|
4196 | if (RT_SUCCESS(rc) && pfnProgress)
|
---|
4197 | pfnProgress(pvUser, uPercentStart + uPercentSpan * 99 / 100);
|
---|
4198 |
|
---|
4199 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
4200 | {
|
---|
4201 | /* streamOptimized is a bit special, we cannot trigger the flush
|
---|
4202 | * until all data has been written. So we write the necessary
|
---|
4203 | * information explicitly. */
|
---|
4204 | pImage->pExtents[0].cDescriptorSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64( pImage->Descriptor.aLines[pImage->Descriptor.cLines]
|
---|
4205 | - pImage->Descriptor.aLines[0], 512));
|
---|
4206 | rc = vmdkWriteMetaSparseExtent(pImage, &pImage->pExtents[0], 0);
|
---|
4207 | if (RT_FAILURE(rc))
|
---|
4208 | {
|
---|
4209 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write VMDK header in '%s'"), pImage->pszFilename);
|
---|
4210 | goto out;
|
---|
4211 | }
|
---|
4212 |
|
---|
4213 | rc = vmdkWriteDescriptor(pImage);
|
---|
4214 | if (RT_FAILURE(rc))
|
---|
4215 | {
|
---|
4216 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write VMDK descriptor in '%s'"), pImage->pszFilename);
|
---|
4217 | goto out;
|
---|
4218 | }
|
---|
4219 | }
|
---|
4220 | else
|
---|
4221 | rc = vmdkFlushImage(pImage);
|
---|
4222 |
|
---|
4223 | out:
|
---|
4224 | if (RT_SUCCESS(rc) && pfnProgress)
|
---|
4225 | pfnProgress(pvUser, uPercentStart + uPercentSpan);
|
---|
4226 |
|
---|
4227 | if (RT_FAILURE(rc))
|
---|
4228 | vmdkFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
|
---|
4229 | return rc;
|
---|
4230 | }
|
---|
4231 |
|
---|
4232 | /**
|
---|
4233 | * Internal: Update image comment.
|
---|
4234 | */
|
---|
4235 | static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment)
|
---|
4236 | {
|
---|
4237 | char *pszCommentEncoded;
|
---|
4238 | if (pszComment)
|
---|
4239 | {
|
---|
4240 | pszCommentEncoded = vmdkEncodeString(pszComment);
|
---|
4241 | if (!pszCommentEncoded)
|
---|
4242 | return VERR_NO_MEMORY;
|
---|
4243 | }
|
---|
4244 | else
|
---|
4245 | pszCommentEncoded = NULL;
|
---|
4246 | int rc = vmdkDescDDBSetStr(pImage, &pImage->Descriptor,
|
---|
4247 | "ddb.comment", pszCommentEncoded);
|
---|
4248 | if (pszComment)
|
---|
4249 | RTStrFree(pszCommentEncoded);
|
---|
4250 | if (RT_FAILURE(rc))
|
---|
4251 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image comment in descriptor in '%s'"), pImage->pszFilename);
|
---|
4252 | return VINF_SUCCESS;
|
---|
4253 | }
|
---|
4254 |
|
---|
4255 | /**
|
---|
4256 | * Internal. Clear the grain table buffer for real stream optimized writing.
|
---|
4257 | */
|
---|
4258 | static void vmdkStreamClearGT(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
|
---|
4259 | {
|
---|
4260 | uint32_t cCacheLines = RT_ALIGN(pExtent->cGTEntries, VMDK_GT_CACHELINE_SIZE) / VMDK_GT_CACHELINE_SIZE;
|
---|
4261 | for (uint32_t i = 0; i < cCacheLines; i++)
|
---|
4262 | memset(&pImage->pGTCache->aGTCache[i].aGTData[0], '\0',
|
---|
4263 | VMDK_GT_CACHELINE_SIZE * sizeof(uint32_t));
|
---|
4264 | }
|
---|
4265 |
|
---|
4266 | /**
|
---|
4267 | * Internal. Flush the grain table buffer for real stream optimized writing.
|
---|
4268 | */
|
---|
4269 | static int vmdkStreamFlushGT(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
4270 | uint32_t uGDEntry)
|
---|
4271 | {
|
---|
4272 | int rc = VINF_SUCCESS;
|
---|
4273 | uint32_t cCacheLines = RT_ALIGN(pExtent->cGTEntries, VMDK_GT_CACHELINE_SIZE) / VMDK_GT_CACHELINE_SIZE;
|
---|
4274 |
|
---|
4275 | /* VMware does not write out completely empty grain tables in the case
|
---|
4276 | * of streamOptimized images, which according to my interpretation of
|
---|
4277 | * the VMDK 1.1 spec is bending the rules. Since they do it and we can
|
---|
4278 | * handle it without problems do it the same way and save some bytes. */
|
---|
4279 | bool fAllZero = true;
|
---|
4280 | for (uint32_t i = 0; i < cCacheLines; i++)
|
---|
4281 | {
|
---|
4282 | /* Convert the grain table to little endian in place, as it will not
|
---|
4283 | * be used at all after this function has been called. */
|
---|
4284 | uint32_t *pGTTmp = &pImage->pGTCache->aGTCache[i].aGTData[0];
|
---|
4285 | for (uint32_t j = 0; j < VMDK_GT_CACHELINE_SIZE; j++, pGTTmp++)
|
---|
4286 | if (*pGTTmp)
|
---|
4287 | {
|
---|
4288 | fAllZero = false;
|
---|
4289 | break;
|
---|
4290 | }
|
---|
4291 | if (!fAllZero)
|
---|
4292 | break;
|
---|
4293 | }
|
---|
4294 | if (fAllZero)
|
---|
4295 | return VINF_SUCCESS;
|
---|
4296 |
|
---|
4297 | uint64_t uFileOffset = pExtent->uAppendPosition;
|
---|
4298 | if (!uFileOffset)
|
---|
4299 | return VERR_INTERNAL_ERROR;
|
---|
4300 | /* Align to sector, as the previous write could have been any size. */
|
---|
4301 | uFileOffset = RT_ALIGN_64(uFileOffset, 512);
|
---|
4302 |
|
---|
4303 | /* Grain table marker. */
|
---|
4304 | uint8_t aMarker[512];
|
---|
4305 | PVMDKMARKER pMarker = (PVMDKMARKER)&aMarker[0];
|
---|
4306 | memset(pMarker, '\0', sizeof(aMarker));
|
---|
4307 | pMarker->uSector = RT_H2LE_U64(VMDK_BYTE2SECTOR((uint64_t)pExtent->cGTEntries * sizeof(uint32_t)));
|
---|
4308 | pMarker->uType = RT_H2LE_U32(VMDK_MARKER_GT);
|
---|
4309 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage, uFileOffset,
|
---|
4310 | aMarker, sizeof(aMarker), NULL);
|
---|
4311 | AssertRC(rc);
|
---|
4312 | uFileOffset += 512;
|
---|
4313 |
|
---|
4314 | if (!pExtent->pGD || pExtent->pGD[uGDEntry])
|
---|
4315 | return VERR_INTERNAL_ERROR;
|
---|
4316 |
|
---|
4317 | pExtent->pGD[uGDEntry] = VMDK_BYTE2SECTOR(uFileOffset);
|
---|
4318 |
|
---|
4319 | for (uint32_t i = 0; i < cCacheLines; i++)
|
---|
4320 | {
|
---|
4321 | /* Convert the grain table to little endian in place, as it will not
|
---|
4322 | * be used at all after this function has been called. */
|
---|
4323 | uint32_t *pGTTmp = &pImage->pGTCache->aGTCache[i].aGTData[0];
|
---|
4324 | for (uint32_t j = 0; j < VMDK_GT_CACHELINE_SIZE; j++, pGTTmp++)
|
---|
4325 | *pGTTmp = RT_H2LE_U32(*pGTTmp);
|
---|
4326 |
|
---|
4327 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage, uFileOffset,
|
---|
4328 | &pImage->pGTCache->aGTCache[i].aGTData[0],
|
---|
4329 | VMDK_GT_CACHELINE_SIZE * sizeof(uint32_t),
|
---|
4330 | NULL);
|
---|
4331 | uFileOffset += VMDK_GT_CACHELINE_SIZE * sizeof(uint32_t);
|
---|
4332 | if (RT_FAILURE(rc))
|
---|
4333 | break;
|
---|
4334 | }
|
---|
4335 | Assert(!(uFileOffset % 512));
|
---|
4336 | pExtent->uAppendPosition = RT_ALIGN_64(uFileOffset, 512);
|
---|
4337 | return rc;
|
---|
4338 | }
|
---|
4339 |
|
---|
4340 | /**
|
---|
4341 | * Internal. Free all allocated space for representing an image, and optionally
|
---|
4342 | * delete the image from disk.
|
---|
4343 | */
|
---|
4344 | static int vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete)
|
---|
4345 | {
|
---|
4346 | int rc = VINF_SUCCESS;
|
---|
4347 |
|
---|
4348 | /* Freeing a never allocated image (e.g. because the open failed) is
|
---|
4349 | * not signalled as an error. After all nothing bad happens. */
|
---|
4350 | if (pImage)
|
---|
4351 | {
|
---|
4352 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
4353 | {
|
---|
4354 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
4355 | {
|
---|
4356 | /* Check if all extents are clean. */
|
---|
4357 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
4358 | {
|
---|
4359 | Assert(!pImage->pExtents[i].fUncleanShutdown);
|
---|
4360 | }
|
---|
4361 | }
|
---|
4362 | else
|
---|
4363 | {
|
---|
4364 | /* Mark all extents as clean. */
|
---|
4365 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
4366 | {
|
---|
4367 | if ( ( pImage->pExtents[i].enmType == VMDKETYPE_HOSTED_SPARSE
|
---|
4368 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
4369 | || pImage->pExtents[i].enmType == VMDKETYPE_ESX_SPARSE
|
---|
4370 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
4371 | )
|
---|
4372 | && pImage->pExtents[i].fUncleanShutdown)
|
---|
4373 | {
|
---|
4374 | pImage->pExtents[i].fUncleanShutdown = false;
|
---|
4375 | pImage->pExtents[i].fMetaDirty = true;
|
---|
4376 | }
|
---|
4377 |
|
---|
4378 | /* From now on it's not safe to append any more data. */
|
---|
4379 | pImage->pExtents[i].uAppendPosition = 0;
|
---|
4380 | }
|
---|
4381 | }
|
---|
4382 | }
|
---|
4383 |
|
---|
4384 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
4385 | {
|
---|
4386 | /* No need to write any pending data if the file will be deleted
|
---|
4387 | * or if the new file wasn't successfully created. */
|
---|
4388 | if ( !fDelete && pImage->pExtents
|
---|
4389 | && pImage->pExtents[0].cGTEntries
|
---|
4390 | && pImage->pExtents[0].uAppendPosition)
|
---|
4391 | {
|
---|
4392 | PVMDKEXTENT pExtent = &pImage->pExtents[0];
|
---|
4393 | uint32_t uLastGDEntry = pExtent->uLastGrainAccess / pExtent->cGTEntries;
|
---|
4394 | rc = vmdkStreamFlushGT(pImage, pExtent, uLastGDEntry);
|
---|
4395 | AssertRC(rc);
|
---|
4396 | vmdkStreamClearGT(pImage, pExtent);
|
---|
4397 | for (uint32_t i = uLastGDEntry + 1; i < pExtent->cGDEntries; i++)
|
---|
4398 | {
|
---|
4399 | rc = vmdkStreamFlushGT(pImage, pExtent, i);
|
---|
4400 | AssertRC(rc);
|
---|
4401 | }
|
---|
4402 |
|
---|
4403 | uint64_t uFileOffset = pExtent->uAppendPosition;
|
---|
4404 | if (!uFileOffset)
|
---|
4405 | return VERR_INTERNAL_ERROR;
|
---|
4406 | uFileOffset = RT_ALIGN_64(uFileOffset, 512);
|
---|
4407 |
|
---|
4408 | /* From now on it's not safe to append any more data. */
|
---|
4409 | pExtent->uAppendPosition = 0;
|
---|
4410 |
|
---|
4411 | /* Grain directory marker. */
|
---|
4412 | uint8_t aMarker[512];
|
---|
4413 | PVMDKMARKER pMarker = (PVMDKMARKER)&aMarker[0];
|
---|
4414 | memset(pMarker, '\0', sizeof(aMarker));
|
---|
4415 | pMarker->uSector = VMDK_BYTE2SECTOR(RT_ALIGN_64(RT_H2LE_U64((uint64_t)pExtent->cGDEntries * sizeof(uint32_t)), 512));
|
---|
4416 | pMarker->uType = RT_H2LE_U32(VMDK_MARKER_GD);
|
---|
4417 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage, uFileOffset,
|
---|
4418 | aMarker, sizeof(aMarker), NULL);
|
---|
4419 | AssertRC(rc);
|
---|
4420 | uFileOffset += 512;
|
---|
4421 |
|
---|
4422 | /* Write grain directory in little endian style. The array will
|
---|
4423 | * not be used after this, so convert in place. */
|
---|
4424 | uint32_t *pGDTmp = pExtent->pGD;
|
---|
4425 | for (uint32_t i = 0; i < pExtent->cGDEntries; i++, pGDTmp++)
|
---|
4426 | *pGDTmp = RT_H2LE_U32(*pGDTmp);
|
---|
4427 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4428 | uFileOffset, pExtent->pGD,
|
---|
4429 | pExtent->cGDEntries * sizeof(uint32_t),
|
---|
4430 | NULL);
|
---|
4431 | AssertRC(rc);
|
---|
4432 |
|
---|
4433 | pExtent->uSectorGD = VMDK_BYTE2SECTOR(uFileOffset);
|
---|
4434 | pExtent->uSectorRGD = VMDK_BYTE2SECTOR(uFileOffset);
|
---|
4435 | uFileOffset = RT_ALIGN_64( uFileOffset
|
---|
4436 | + pExtent->cGDEntries * sizeof(uint32_t),
|
---|
4437 | 512);
|
---|
4438 |
|
---|
4439 | /* Footer marker. */
|
---|
4440 | memset(pMarker, '\0', sizeof(aMarker));
|
---|
4441 | pMarker->uSector = VMDK_BYTE2SECTOR(512);
|
---|
4442 | pMarker->uType = RT_H2LE_U32(VMDK_MARKER_FOOTER);
|
---|
4443 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4444 | uFileOffset, aMarker, sizeof(aMarker), NULL);
|
---|
4445 | AssertRC(rc);
|
---|
4446 |
|
---|
4447 | uFileOffset += 512;
|
---|
4448 | rc = vmdkWriteMetaSparseExtent(pImage, pExtent, uFileOffset);
|
---|
4449 | AssertRC(rc);
|
---|
4450 |
|
---|
4451 | uFileOffset += 512;
|
---|
4452 | /* End-of-stream marker. */
|
---|
4453 | memset(pMarker, '\0', sizeof(aMarker));
|
---|
4454 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4455 | uFileOffset, aMarker, sizeof(aMarker), NULL);
|
---|
4456 | AssertRC(rc);
|
---|
4457 | }
|
---|
4458 | }
|
---|
4459 | else
|
---|
4460 | vmdkFlushImage(pImage);
|
---|
4461 |
|
---|
4462 | if (pImage->pExtents != NULL)
|
---|
4463 | {
|
---|
4464 | for (unsigned i = 0 ; i < pImage->cExtents; i++)
|
---|
4465 | vmdkFreeExtentData(pImage, &pImage->pExtents[i], fDelete);
|
---|
4466 | RTMemFree(pImage->pExtents);
|
---|
4467 | pImage->pExtents = NULL;
|
---|
4468 | }
|
---|
4469 | pImage->cExtents = 0;
|
---|
4470 | if (pImage->pFile != NULL)
|
---|
4471 | vmdkFileClose(pImage, &pImage->pFile, fDelete);
|
---|
4472 | vmdkFileCheckAllClose(pImage);
|
---|
4473 |
|
---|
4474 | if (pImage->pGTCache)
|
---|
4475 | {
|
---|
4476 | RTMemFree(pImage->pGTCache);
|
---|
4477 | pImage->pGTCache = NULL;
|
---|
4478 | }
|
---|
4479 | if (pImage->pDescData)
|
---|
4480 | {
|
---|
4481 | RTMemFree(pImage->pDescData);
|
---|
4482 | pImage->pDescData = NULL;
|
---|
4483 | }
|
---|
4484 | }
|
---|
4485 |
|
---|
4486 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
4487 | return rc;
|
---|
4488 | }
|
---|
4489 |
|
---|
4490 | /**
|
---|
4491 | * Internal. Flush image data (and metadata) to disk.
|
---|
4492 | */
|
---|
4493 | static int vmdkFlushImage(PVMDKIMAGE pImage)
|
---|
4494 | {
|
---|
4495 | PVMDKEXTENT pExtent;
|
---|
4496 | int rc = VINF_SUCCESS;
|
---|
4497 |
|
---|
4498 | /* Update descriptor if changed. */
|
---|
4499 | if (pImage->Descriptor.fDirty)
|
---|
4500 | {
|
---|
4501 | rc = vmdkWriteDescriptor(pImage);
|
---|
4502 | if (RT_FAILURE(rc))
|
---|
4503 | goto out;
|
---|
4504 | }
|
---|
4505 |
|
---|
4506 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
4507 | {
|
---|
4508 | pExtent = &pImage->pExtents[i];
|
---|
4509 | if (pExtent->pFile != NULL && pExtent->fMetaDirty)
|
---|
4510 | {
|
---|
4511 | switch (pExtent->enmType)
|
---|
4512 | {
|
---|
4513 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
4514 | if (!pExtent->fFooter)
|
---|
4515 | {
|
---|
4516 | rc = vmdkWriteMetaSparseExtent(pImage, pExtent, 0);
|
---|
4517 | if (RT_FAILURE(rc))
|
---|
4518 | goto out;
|
---|
4519 | }
|
---|
4520 | else
|
---|
4521 | {
|
---|
4522 | uint64_t uFileOffset = pExtent->uAppendPosition;
|
---|
4523 | /* Simply skip writing anything if the streamOptimized
|
---|
4524 | * image hasn't been just created. */
|
---|
4525 | if (!uFileOffset)
|
---|
4526 | break;
|
---|
4527 | uFileOffset = RT_ALIGN_64(uFileOffset, 512);
|
---|
4528 | rc = vmdkWriteMetaSparseExtent(pImage, pExtent,
|
---|
4529 | uFileOffset);
|
---|
4530 | if (RT_FAILURE(rc))
|
---|
4531 | goto out;
|
---|
4532 | }
|
---|
4533 | break;
|
---|
4534 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
4535 | case VMDKETYPE_ESX_SPARSE:
|
---|
4536 | /** @todo update the header. */
|
---|
4537 | break;
|
---|
4538 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
4539 | case VMDKETYPE_VMFS:
|
---|
4540 | case VMDKETYPE_FLAT:
|
---|
4541 | /* Nothing to do. */
|
---|
4542 | break;
|
---|
4543 | case VMDKETYPE_ZERO:
|
---|
4544 | default:
|
---|
4545 | AssertMsgFailed(("extent with type %d marked as dirty\n",
|
---|
4546 | pExtent->enmType));
|
---|
4547 | break;
|
---|
4548 | }
|
---|
4549 | }
|
---|
4550 | switch (pExtent->enmType)
|
---|
4551 | {
|
---|
4552 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
4553 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
4554 | case VMDKETYPE_ESX_SPARSE:
|
---|
4555 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
4556 | case VMDKETYPE_VMFS:
|
---|
4557 | case VMDKETYPE_FLAT:
|
---|
4558 | /** @todo implement proper path absolute check. */
|
---|
4559 | if ( pExtent->pFile != NULL
|
---|
4560 | && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
4561 | && !(pExtent->pszBasename[0] == RTPATH_SLASH))
|
---|
4562 | rc = vdIfIoIntFileFlushSync(pImage->pIfIo, pExtent->pFile->pStorage);
|
---|
4563 | break;
|
---|
4564 | case VMDKETYPE_ZERO:
|
---|
4565 | /* No need to do anything for this extent. */
|
---|
4566 | break;
|
---|
4567 | default:
|
---|
4568 | AssertMsgFailed(("unknown extent type %d\n", pExtent->enmType));
|
---|
4569 | break;
|
---|
4570 | }
|
---|
4571 | }
|
---|
4572 |
|
---|
4573 | out:
|
---|
4574 | return rc;
|
---|
4575 | }
|
---|
4576 |
|
---|
4577 | /**
|
---|
4578 | * Internal. Find extent corresponding to the sector number in the disk.
|
---|
4579 | */
|
---|
4580 | static int vmdkFindExtent(PVMDKIMAGE pImage, uint64_t offSector,
|
---|
4581 | PVMDKEXTENT *ppExtent, uint64_t *puSectorInExtent)
|
---|
4582 | {
|
---|
4583 | PVMDKEXTENT pExtent = NULL;
|
---|
4584 | int rc = VINF_SUCCESS;
|
---|
4585 |
|
---|
4586 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
4587 | {
|
---|
4588 | if (offSector < pImage->pExtents[i].cNominalSectors)
|
---|
4589 | {
|
---|
4590 | pExtent = &pImage->pExtents[i];
|
---|
4591 | *puSectorInExtent = offSector + pImage->pExtents[i].uSectorOffset;
|
---|
4592 | break;
|
---|
4593 | }
|
---|
4594 | offSector -= pImage->pExtents[i].cNominalSectors;
|
---|
4595 | }
|
---|
4596 |
|
---|
4597 | if (pExtent)
|
---|
4598 | *ppExtent = pExtent;
|
---|
4599 | else
|
---|
4600 | rc = VERR_IO_SECTOR_NOT_FOUND;
|
---|
4601 |
|
---|
4602 | return rc;
|
---|
4603 | }
|
---|
4604 |
|
---|
4605 | /**
|
---|
4606 | * Internal. Hash function for placing the grain table hash entries.
|
---|
4607 | */
|
---|
4608 | static uint32_t vmdkGTCacheHash(PVMDKGTCACHE pCache, uint64_t uSector,
|
---|
4609 | unsigned uExtent)
|
---|
4610 | {
|
---|
4611 | /** @todo this hash function is quite simple, maybe use a better one which
|
---|
4612 | * scrambles the bits better. */
|
---|
4613 | return (uSector + uExtent) % pCache->cEntries;
|
---|
4614 | }
|
---|
4615 |
|
---|
4616 | /**
|
---|
4617 | * Internal. Get sector number in the extent file from the relative sector
|
---|
4618 | * number in the extent.
|
---|
4619 | */
|
---|
4620 | static int vmdkGetSector(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
4621 | uint64_t uSector, uint64_t *puExtentSector)
|
---|
4622 | {
|
---|
4623 | PVMDKGTCACHE pCache = pImage->pGTCache;
|
---|
4624 | uint64_t uGDIndex, uGTSector, uGTBlock;
|
---|
4625 | uint32_t uGTHash, uGTBlockIndex;
|
---|
4626 | PVMDKGTCACHEENTRY pGTCacheEntry;
|
---|
4627 | uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
|
---|
4628 | int rc;
|
---|
4629 |
|
---|
4630 | /* For newly created and readonly/sequentially opened streamOptimized
|
---|
4631 | * images this must be a no-op, as the grain directory is not there. */
|
---|
4632 | if ( ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
|
---|
4633 | && pExtent->uAppendPosition)
|
---|
4634 | || ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
|
---|
4635 | && pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY
|
---|
4636 | && pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
|
---|
4637 | {
|
---|
4638 | *puExtentSector = 0;
|
---|
4639 | return VINF_SUCCESS;
|
---|
4640 | }
|
---|
4641 |
|
---|
4642 | uGDIndex = uSector / pExtent->cSectorsPerGDE;
|
---|
4643 | if (uGDIndex >= pExtent->cGDEntries)
|
---|
4644 | return VERR_OUT_OF_RANGE;
|
---|
4645 | uGTSector = pExtent->pGD[uGDIndex];
|
---|
4646 | if (!uGTSector)
|
---|
4647 | {
|
---|
4648 | /* There is no grain table referenced by this grain directory
|
---|
4649 | * entry. So there is absolutely no data in this area. */
|
---|
4650 | *puExtentSector = 0;
|
---|
4651 | return VINF_SUCCESS;
|
---|
4652 | }
|
---|
4653 |
|
---|
4654 | uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
|
---|
4655 | uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
|
---|
4656 | pGTCacheEntry = &pCache->aGTCache[uGTHash];
|
---|
4657 | if ( pGTCacheEntry->uExtent != pExtent->uExtent
|
---|
4658 | || pGTCacheEntry->uGTBlock != uGTBlock)
|
---|
4659 | {
|
---|
4660 | /* Cache miss, fetch data from disk. */
|
---|
4661 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4662 | VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
4663 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
4664 | if (RT_FAILURE(rc))
|
---|
4665 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot read grain table entry in '%s'"), pExtent->pszFullname);
|
---|
4666 | pGTCacheEntry->uExtent = pExtent->uExtent;
|
---|
4667 | pGTCacheEntry->uGTBlock = uGTBlock;
|
---|
4668 | for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
|
---|
4669 | pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
|
---|
4670 | }
|
---|
4671 | uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
|
---|
4672 | uint32_t uGrainSector = pGTCacheEntry->aGTData[uGTBlockIndex];
|
---|
4673 | if (uGrainSector)
|
---|
4674 | *puExtentSector = uGrainSector + uSector % pExtent->cSectorsPerGrain;
|
---|
4675 | else
|
---|
4676 | *puExtentSector = 0;
|
---|
4677 | return VINF_SUCCESS;
|
---|
4678 | }
|
---|
4679 |
|
---|
4680 | /**
|
---|
4681 | * Internal. Get sector number in the extent file from the relative sector
|
---|
4682 | * number in the extent - version for async access.
|
---|
4683 | */
|
---|
4684 | static int vmdkGetSectorAsync(PVMDKIMAGE pImage, PVDIOCTX pIoCtx,
|
---|
4685 | PVMDKEXTENT pExtent, uint64_t uSector,
|
---|
4686 | uint64_t *puExtentSector)
|
---|
4687 | {
|
---|
4688 | PVMDKGTCACHE pCache = pImage->pGTCache;
|
---|
4689 | uint64_t uGDIndex, uGTSector, uGTBlock;
|
---|
4690 | uint32_t uGTHash, uGTBlockIndex;
|
---|
4691 | PVMDKGTCACHEENTRY pGTCacheEntry;
|
---|
4692 | uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
|
---|
4693 | int rc;
|
---|
4694 |
|
---|
4695 | uGDIndex = uSector / pExtent->cSectorsPerGDE;
|
---|
4696 | if (uGDIndex >= pExtent->cGDEntries)
|
---|
4697 | return VERR_OUT_OF_RANGE;
|
---|
4698 | uGTSector = pExtent->pGD[uGDIndex];
|
---|
4699 | if (!uGTSector)
|
---|
4700 | {
|
---|
4701 | /* There is no grain table referenced by this grain directory
|
---|
4702 | * entry. So there is absolutely no data in this area. */
|
---|
4703 | *puExtentSector = 0;
|
---|
4704 | return VINF_SUCCESS;
|
---|
4705 | }
|
---|
4706 |
|
---|
4707 | uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
|
---|
4708 | uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
|
---|
4709 | pGTCacheEntry = &pCache->aGTCache[uGTHash];
|
---|
4710 | if ( pGTCacheEntry->uExtent != pExtent->uExtent
|
---|
4711 | || pGTCacheEntry->uGTBlock != uGTBlock)
|
---|
4712 | {
|
---|
4713 | /* Cache miss, fetch data from disk. */
|
---|
4714 | PVDMETAXFER pMetaXfer;
|
---|
4715 | rc = vdIfIoIntFileReadMetaAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4716 | VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
4717 | aGTDataTmp, sizeof(aGTDataTmp), pIoCtx, &pMetaXfer, NULL, NULL);
|
---|
4718 | if (RT_FAILURE(rc))
|
---|
4719 | return rc;
|
---|
4720 | /* We can release the metadata transfer immediately. */
|
---|
4721 | vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
|
---|
4722 | pGTCacheEntry->uExtent = pExtent->uExtent;
|
---|
4723 | pGTCacheEntry->uGTBlock = uGTBlock;
|
---|
4724 | for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
|
---|
4725 | pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
|
---|
4726 | }
|
---|
4727 | uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
|
---|
4728 | uint32_t uGrainSector = pGTCacheEntry->aGTData[uGTBlockIndex];
|
---|
4729 | if (uGrainSector)
|
---|
4730 | *puExtentSector = uGrainSector + uSector % pExtent->cSectorsPerGrain;
|
---|
4731 | else
|
---|
4732 | *puExtentSector = 0;
|
---|
4733 | return VINF_SUCCESS;
|
---|
4734 | }
|
---|
4735 |
|
---|
4736 | /**
|
---|
4737 | * Internal. Allocates a new grain table (if necessary), writes the grain
|
---|
4738 | * and updates the grain table. The cache is also updated by this operation.
|
---|
4739 | * This is separate from vmdkGetSector, because that should be as fast as
|
---|
4740 | * possible. Most code from vmdkGetSector also appears here.
|
---|
4741 | */
|
---|
4742 | static int vmdkAllocGrain(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
4743 | uint64_t uSector, const void *pvBuf,
|
---|
4744 | uint64_t cbWrite)
|
---|
4745 | {
|
---|
4746 | PVMDKGTCACHE pCache = pImage->pGTCache;
|
---|
4747 | uint64_t uGDIndex, uGTSector, uRGTSector, uGTBlock;
|
---|
4748 | uint64_t uFileOffset;
|
---|
4749 | uint32_t uGTHash, uGTBlockIndex;
|
---|
4750 | PVMDKGTCACHEENTRY pGTCacheEntry;
|
---|
4751 | uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
|
---|
4752 | int rc;
|
---|
4753 |
|
---|
4754 | uGDIndex = uSector / pExtent->cSectorsPerGDE;
|
---|
4755 | if (uGDIndex >= pExtent->cGDEntries)
|
---|
4756 | return VERR_OUT_OF_RANGE;
|
---|
4757 | uGTSector = pExtent->pGD[uGDIndex];
|
---|
4758 | if (pExtent->pRGD)
|
---|
4759 | uRGTSector = pExtent->pRGD[uGDIndex];
|
---|
4760 | else
|
---|
4761 | uRGTSector = 0; /**< avoid compiler warning */
|
---|
4762 | if (!uGTSector)
|
---|
4763 | {
|
---|
4764 | /* There is no grain table referenced by this grain directory
|
---|
4765 | * entry. So there is absolutely no data in this area. Allocate
|
---|
4766 | * a new grain table and put the reference to it in the GDs. */
|
---|
4767 | uFileOffset = pExtent->uAppendPosition;
|
---|
4768 | if (!uFileOffset)
|
---|
4769 | return VERR_INTERNAL_ERROR;
|
---|
4770 | Assert(!(uFileOffset % 512));
|
---|
4771 | uFileOffset = RT_ALIGN_64(uFileOffset, 512);
|
---|
4772 | uGTSector = VMDK_BYTE2SECTOR(uFileOffset);
|
---|
4773 |
|
---|
4774 | pExtent->uAppendPosition += pExtent->cGTEntries * sizeof(uint32_t);
|
---|
4775 |
|
---|
4776 | /* Normally the grain table is preallocated for hosted sparse extents
|
---|
4777 | * that support more than 32 bit sector numbers. So this shouldn't
|
---|
4778 | * ever happen on a valid extent. */
|
---|
4779 | if (uGTSector > UINT32_MAX)
|
---|
4780 | return VERR_VD_VMDK_INVALID_HEADER;
|
---|
4781 |
|
---|
4782 | /* Write grain table by writing the required number of grain table
|
---|
4783 | * cache chunks. Avoids dynamic memory allocation, but is a bit
|
---|
4784 | * slower. But as this is a pretty infrequently occurring case it
|
---|
4785 | * should be acceptable. */
|
---|
4786 | memset(aGTDataTmp, '\0', sizeof(aGTDataTmp));
|
---|
4787 | for (unsigned i = 0;
|
---|
4788 | i < pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE;
|
---|
4789 | i++)
|
---|
4790 | {
|
---|
4791 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4792 | VMDK_SECTOR2BYTE(uGTSector) + i * sizeof(aGTDataTmp),
|
---|
4793 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
4794 | if (RT_FAILURE(rc))
|
---|
4795 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain table allocation in '%s'"), pExtent->pszFullname);
|
---|
4796 | }
|
---|
4797 | pExtent->uAppendPosition = RT_ALIGN_64( pExtent->uAppendPosition
|
---|
4798 | + pExtent->cGTEntries * sizeof(uint32_t),
|
---|
4799 | 512);
|
---|
4800 |
|
---|
4801 | if (pExtent->pRGD)
|
---|
4802 | {
|
---|
4803 | AssertReturn(!uRGTSector, VERR_VD_VMDK_INVALID_HEADER);
|
---|
4804 | uFileOffset = pExtent->uAppendPosition;
|
---|
4805 | if (!uFileOffset)
|
---|
4806 | return VERR_INTERNAL_ERROR;
|
---|
4807 | Assert(!(uFileOffset % 512));
|
---|
4808 | uRGTSector = VMDK_BYTE2SECTOR(uFileOffset);
|
---|
4809 |
|
---|
4810 | pExtent->uAppendPosition += pExtent->cGTEntries * sizeof(uint32_t);
|
---|
4811 |
|
---|
4812 | /* Normally the redundant grain table is preallocated for hosted
|
---|
4813 | * sparse extents that support more than 32 bit sector numbers. So
|
---|
4814 | * this shouldn't ever happen on a valid extent. */
|
---|
4815 | if (uRGTSector > UINT32_MAX)
|
---|
4816 | return VERR_VD_VMDK_INVALID_HEADER;
|
---|
4817 |
|
---|
4818 | /* Write backup grain table by writing the required number of grain
|
---|
4819 | * table cache chunks. Avoids dynamic memory allocation, but is a
|
---|
4820 | * bit slower. But as this is a pretty infrequently occurring case
|
---|
4821 | * it should be acceptable. */
|
---|
4822 | for (unsigned i = 0;
|
---|
4823 | i < pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE;
|
---|
4824 | i++)
|
---|
4825 | {
|
---|
4826 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4827 | VMDK_SECTOR2BYTE(uRGTSector) + i * sizeof(aGTDataTmp),
|
---|
4828 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
4829 | if (RT_FAILURE(rc))
|
---|
4830 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain table allocation in '%s'"), pExtent->pszFullname);
|
---|
4831 | }
|
---|
4832 |
|
---|
4833 | pExtent->uAppendPosition = pExtent->uAppendPosition
|
---|
4834 | + pExtent->cGTEntries * sizeof(uint32_t);
|
---|
4835 | }
|
---|
4836 |
|
---|
4837 | /* Update the grain directory on disk (doing it before writing the
|
---|
4838 | * grain table will result in a garbled extent if the operation is
|
---|
4839 | * aborted for some reason. Otherwise the worst that can happen is
|
---|
4840 | * some unused sectors in the extent. */
|
---|
4841 | uint32_t uGTSectorLE = RT_H2LE_U64(uGTSector);
|
---|
4842 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4843 | VMDK_SECTOR2BYTE(pExtent->uSectorGD) + uGDIndex * sizeof(uGTSectorLE),
|
---|
4844 | &uGTSectorLE, sizeof(uGTSectorLE), NULL);
|
---|
4845 | if (RT_FAILURE(rc))
|
---|
4846 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
4847 | if (pExtent->pRGD)
|
---|
4848 | {
|
---|
4849 | uint32_t uRGTSectorLE = RT_H2LE_U64(uRGTSector);
|
---|
4850 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4851 | VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + uGDIndex * sizeof(uRGTSectorLE),
|
---|
4852 | &uRGTSectorLE, sizeof(uRGTSectorLE), NULL);
|
---|
4853 | if (RT_FAILURE(rc))
|
---|
4854 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
4855 | }
|
---|
4856 |
|
---|
4857 | /* As the final step update the in-memory copy of the GDs. */
|
---|
4858 | pExtent->pGD[uGDIndex] = uGTSector;
|
---|
4859 | if (pExtent->pRGD)
|
---|
4860 | pExtent->pRGD[uGDIndex] = uRGTSector;
|
---|
4861 | }
|
---|
4862 |
|
---|
4863 | uFileOffset = pExtent->uAppendPosition;
|
---|
4864 | if (!uFileOffset)
|
---|
4865 | return VERR_INTERNAL_ERROR;
|
---|
4866 | Assert(!(uFileOffset % 512));
|
---|
4867 |
|
---|
4868 | /* Write the data. Always a full grain, or we're in big trouble. */
|
---|
4869 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
4870 | {
|
---|
4871 | if (cbWrite != VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
|
---|
4872 | return vdIfError(pImage->pIfError, VERR_INTERNAL_ERROR, RT_SRC_POS, N_("VMDK: not enough data for a compressed data block in '%s'"), pExtent->pszFullname);
|
---|
4873 |
|
---|
4874 | /* Invalidate cache, just in case some code incorrectly allows mixing
|
---|
4875 | * of reads and writes. Normally shouldn't be needed. */
|
---|
4876 | pExtent->uGrainSectorAbs = 0;
|
---|
4877 |
|
---|
4878 | /* Write compressed data block and the markers. */
|
---|
4879 | uint32_t cbGrain = 0;
|
---|
4880 | rc = vmdkFileDeflateSync(pImage, pExtent, uFileOffset,
|
---|
4881 | pvBuf, cbWrite, uSector, &cbGrain);
|
---|
4882 | if (RT_FAILURE(rc))
|
---|
4883 | {
|
---|
4884 | AssertRC(rc);
|
---|
4885 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write allocated compressed data block in '%s'"), pExtent->pszFullname);
|
---|
4886 | }
|
---|
4887 | pExtent->uLastGrainAccess = uSector / pExtent->cSectorsPerGrain;
|
---|
4888 | pExtent->uAppendPosition += cbGrain;
|
---|
4889 | }
|
---|
4890 | else
|
---|
4891 | {
|
---|
4892 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4893 | uFileOffset, pvBuf, cbWrite, NULL);
|
---|
4894 | if (RT_FAILURE(rc))
|
---|
4895 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write allocated data block in '%s'"), pExtent->pszFullname);
|
---|
4896 | pExtent->uAppendPosition += cbWrite;
|
---|
4897 | }
|
---|
4898 |
|
---|
4899 | /* Update the grain table (and the cache). */
|
---|
4900 | uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
|
---|
4901 | uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
|
---|
4902 | pGTCacheEntry = &pCache->aGTCache[uGTHash];
|
---|
4903 | if ( pGTCacheEntry->uExtent != pExtent->uExtent
|
---|
4904 | || pGTCacheEntry->uGTBlock != uGTBlock)
|
---|
4905 | {
|
---|
4906 | /* Cache miss, fetch data from disk. */
|
---|
4907 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4908 | VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
4909 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
4910 | if (RT_FAILURE(rc))
|
---|
4911 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot read allocated grain table entry in '%s'"), pExtent->pszFullname);
|
---|
4912 | pGTCacheEntry->uExtent = pExtent->uExtent;
|
---|
4913 | pGTCacheEntry->uGTBlock = uGTBlock;
|
---|
4914 | for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
|
---|
4915 | pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
|
---|
4916 | }
|
---|
4917 | else
|
---|
4918 | {
|
---|
4919 | /* Cache hit. Convert grain table block back to disk format, otherwise
|
---|
4920 | * the code below will write garbage for all but the updated entry. */
|
---|
4921 | for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
|
---|
4922 | aGTDataTmp[i] = RT_H2LE_U32(pGTCacheEntry->aGTData[i]);
|
---|
4923 | }
|
---|
4924 | uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
|
---|
4925 | aGTDataTmp[uGTBlockIndex] = RT_H2LE_U32(VMDK_BYTE2SECTOR(uFileOffset));
|
---|
4926 | pGTCacheEntry->aGTData[uGTBlockIndex] = VMDK_BYTE2SECTOR(uFileOffset);
|
---|
4927 | /* Update grain table on disk. */
|
---|
4928 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4929 | VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
4930 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
4931 | if (RT_FAILURE(rc))
|
---|
4932 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated grain table in '%s'"), pExtent->pszFullname);
|
---|
4933 | if (pExtent->pRGD)
|
---|
4934 | {
|
---|
4935 | /* Update backup grain table on disk. */
|
---|
4936 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
4937 | VMDK_SECTOR2BYTE(uRGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
4938 | aGTDataTmp, sizeof(aGTDataTmp), NULL);
|
---|
4939 | if (RT_FAILURE(rc))
|
---|
4940 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated backup grain table in '%s'"), pExtent->pszFullname);
|
---|
4941 | }
|
---|
4942 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
4943 | if (RT_SUCCESS(rc) && pExtent->enmType == VMDKETYPE_ESX_SPARSE)
|
---|
4944 | {
|
---|
4945 | pExtent->uFreeSector = uGTSector + VMDK_BYTE2SECTOR(cbWrite);
|
---|
4946 | pExtent->fMetaDirty = true;
|
---|
4947 | }
|
---|
4948 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
4949 | return rc;
|
---|
4950 | }
|
---|
4951 |
|
---|
4952 | /**
|
---|
4953 | * Internal. Writes the grain and also if necessary the grain tables.
|
---|
4954 | * Uses the grain table cache as a true grain table.
|
---|
4955 | */
|
---|
4956 | static int vmdkStreamAllocGrain(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
4957 | uint64_t uSector, const void *pvBuf,
|
---|
4958 | uint64_t cbWrite)
|
---|
4959 | {
|
---|
4960 | uint32_t uGrain;
|
---|
4961 | uint32_t uGDEntry, uLastGDEntry;
|
---|
4962 | uint32_t cbGrain = 0;
|
---|
4963 | uint32_t uCacheLine, uCacheEntry;
|
---|
4964 | const void *pData = pvBuf;
|
---|
4965 | int rc;
|
---|
4966 |
|
---|
4967 | /* Very strict requirements: always write at least one full grain, with
|
---|
4968 | * proper alignment. Everything else would require reading of already
|
---|
4969 | * written data, which we don't support for obvious reasons. The only
|
---|
4970 | * exception is the last grain, and only if the image size specifies
|
---|
4971 | * that only some portion holds data. In any case the write must be
|
---|
4972 | * within the image limits, no "overshoot" allowed. */
|
---|
4973 | if ( cbWrite == 0
|
---|
4974 | || ( cbWrite < VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
|
---|
4975 | && pExtent->cNominalSectors - uSector >= pExtent->cSectorsPerGrain)
|
---|
4976 | || uSector % pExtent->cSectorsPerGrain
|
---|
4977 | || uSector + VMDK_BYTE2SECTOR(cbWrite) > pExtent->cNominalSectors)
|
---|
4978 | return VERR_INVALID_PARAMETER;
|
---|
4979 |
|
---|
4980 | /* Clip write range to at most the rest of the grain. */
|
---|
4981 | cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSector % pExtent->cSectorsPerGrain));
|
---|
4982 |
|
---|
4983 | /* Do not allow to go back. */
|
---|
4984 | uGrain = uSector / pExtent->cSectorsPerGrain;
|
---|
4985 | uCacheLine = uGrain % pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE;
|
---|
4986 | uCacheEntry = uGrain % VMDK_GT_CACHELINE_SIZE;
|
---|
4987 | uGDEntry = uGrain / pExtent->cGTEntries;
|
---|
4988 | uLastGDEntry = pExtent->uLastGrainAccess / pExtent->cGTEntries;
|
---|
4989 | if (uGrain < pExtent->uLastGrainAccess)
|
---|
4990 | return VERR_VD_VMDK_INVALID_WRITE;
|
---|
4991 |
|
---|
4992 | /* Zero byte write optimization. Since we don't tell VBoxHDD that we need
|
---|
4993 | * to allocate something, we also need to detect the situation ourself. */
|
---|
4994 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
|
---|
4995 | && ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbWrite * 8) == -1)
|
---|
4996 | return VINF_SUCCESS;
|
---|
4997 |
|
---|
4998 | if (uGDEntry != uLastGDEntry)
|
---|
4999 | {
|
---|
5000 | rc = vmdkStreamFlushGT(pImage, pExtent, uLastGDEntry);
|
---|
5001 | if (RT_FAILURE(rc))
|
---|
5002 | return rc;
|
---|
5003 | vmdkStreamClearGT(pImage, pExtent);
|
---|
5004 | for (uint32_t i = uLastGDEntry + 1; i < uGDEntry; i++)
|
---|
5005 | {
|
---|
5006 | rc = vmdkStreamFlushGT(pImage, pExtent, i);
|
---|
5007 | if (RT_FAILURE(rc))
|
---|
5008 | return rc;
|
---|
5009 | }
|
---|
5010 | }
|
---|
5011 |
|
---|
5012 | uint64_t uFileOffset;
|
---|
5013 | uFileOffset = pExtent->uAppendPosition;
|
---|
5014 | if (!uFileOffset)
|
---|
5015 | return VERR_INTERNAL_ERROR;
|
---|
5016 | /* Align to sector, as the previous write could have been any size. */
|
---|
5017 | uFileOffset = RT_ALIGN_64(uFileOffset, 512);
|
---|
5018 |
|
---|
5019 | /* Paranoia check: extent type, grain table buffer presence and
|
---|
5020 | * grain table buffer space. Also grain table entry must be clear. */
|
---|
5021 | if ( pExtent->enmType != VMDKETYPE_HOSTED_SPARSE
|
---|
5022 | || !pImage->pGTCache
|
---|
5023 | || pExtent->cGTEntries > VMDK_GT_CACHE_SIZE * VMDK_GT_CACHELINE_SIZE
|
---|
5024 | || pImage->pGTCache->aGTCache[uCacheLine].aGTData[uCacheEntry])
|
---|
5025 | return VERR_INTERNAL_ERROR;
|
---|
5026 |
|
---|
5027 | /* Update grain table entry. */
|
---|
5028 | pImage->pGTCache->aGTCache[uCacheLine].aGTData[uCacheEntry] = VMDK_BYTE2SECTOR(uFileOffset);
|
---|
5029 |
|
---|
5030 | if (cbWrite != VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
|
---|
5031 | {
|
---|
5032 | memcpy(pExtent->pvGrain, pvBuf, cbWrite);
|
---|
5033 | memset((char *)pExtent->pvGrain + cbWrite, '\0',
|
---|
5034 | VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain) - cbWrite);
|
---|
5035 | pData = pExtent->pvGrain;
|
---|
5036 | }
|
---|
5037 | rc = vmdkFileDeflateSync(pImage, pExtent, uFileOffset, pData,
|
---|
5038 | VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
|
---|
5039 | uSector, &cbGrain);
|
---|
5040 | if (RT_FAILURE(rc))
|
---|
5041 | {
|
---|
5042 | pExtent->uGrainSectorAbs = 0;
|
---|
5043 | AssertRC(rc);
|
---|
5044 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write compressed data block in '%s'"), pExtent->pszFullname);
|
---|
5045 | }
|
---|
5046 | pExtent->uLastGrainAccess = uGrain;
|
---|
5047 | pExtent->uAppendPosition += cbGrain;
|
---|
5048 |
|
---|
5049 | return rc;
|
---|
5050 | }
|
---|
5051 |
|
---|
5052 | /**
|
---|
5053 | * Internal: Updates the grain table during a async grain allocation.
|
---|
5054 | */
|
---|
5055 | static int vmdkAllocGrainAsyncGTUpdate(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
5056 | PVDIOCTX pIoCtx,
|
---|
5057 | PVMDKGRAINALLOCASYNC pGrainAlloc)
|
---|
5058 | {
|
---|
5059 | int rc = VINF_SUCCESS;
|
---|
5060 | PVMDKGTCACHE pCache = pImage->pGTCache;
|
---|
5061 | uint32_t aGTDataTmp[VMDK_GT_CACHELINE_SIZE];
|
---|
5062 | uint32_t uGTHash, uGTBlockIndex;
|
---|
5063 | uint64_t uGTSector, uRGTSector, uGTBlock;
|
---|
5064 | uint64_t uSector = pGrainAlloc->uSector;
|
---|
5065 | PVMDKGTCACHEENTRY pGTCacheEntry;
|
---|
5066 |
|
---|
5067 | LogFlowFunc(("pImage=%#p pExtent=%#p pCache=%#p pIoCtx=%#p pGrainAlloc=%#p\n",
|
---|
5068 | pImage, pExtent, pCache, pIoCtx, pGrainAlloc));
|
---|
5069 |
|
---|
5070 | uGTSector = pGrainAlloc->uGTSector;
|
---|
5071 | uRGTSector = pGrainAlloc->uRGTSector;
|
---|
5072 | LogFlow(("uGTSector=%llu uRGTSector=%llu\n", uGTSector, uRGTSector));
|
---|
5073 |
|
---|
5074 | /* Update the grain table (and the cache). */
|
---|
5075 | uGTBlock = uSector / (pExtent->cSectorsPerGrain * VMDK_GT_CACHELINE_SIZE);
|
---|
5076 | uGTHash = vmdkGTCacheHash(pCache, uGTBlock, pExtent->uExtent);
|
---|
5077 | pGTCacheEntry = &pCache->aGTCache[uGTHash];
|
---|
5078 | if ( pGTCacheEntry->uExtent != pExtent->uExtent
|
---|
5079 | || pGTCacheEntry->uGTBlock != uGTBlock)
|
---|
5080 | {
|
---|
5081 | /* Cache miss, fetch data from disk. */
|
---|
5082 | LogFlow(("Cache miss, fetch data from disk\n"));
|
---|
5083 | PVDMETAXFER pMetaXfer = NULL;
|
---|
5084 | rc = vdIfIoIntFileReadMetaAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5085 | VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
5086 | aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
|
---|
5087 | &pMetaXfer, vmdkAllocGrainAsyncComplete, pGrainAlloc);
|
---|
5088 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5089 | {
|
---|
5090 | pGrainAlloc->cIoXfersPending++;
|
---|
5091 | pGrainAlloc->fGTUpdateNeeded = true;
|
---|
5092 | /* Leave early, we will be called again after the read completed. */
|
---|
5093 | LogFlowFunc(("Metadata read in progress, leaving\n"));
|
---|
5094 | return rc;
|
---|
5095 | }
|
---|
5096 | else if (RT_FAILURE(rc))
|
---|
5097 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot read allocated grain table entry in '%s'"), pExtent->pszFullname);
|
---|
5098 | vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
|
---|
5099 | pGTCacheEntry->uExtent = pExtent->uExtent;
|
---|
5100 | pGTCacheEntry->uGTBlock = uGTBlock;
|
---|
5101 | for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
|
---|
5102 | pGTCacheEntry->aGTData[i] = RT_LE2H_U32(aGTDataTmp[i]);
|
---|
5103 | }
|
---|
5104 | else
|
---|
5105 | {
|
---|
5106 | /* Cache hit. Convert grain table block back to disk format, otherwise
|
---|
5107 | * the code below will write garbage for all but the updated entry. */
|
---|
5108 | for (unsigned i = 0; i < VMDK_GT_CACHELINE_SIZE; i++)
|
---|
5109 | aGTDataTmp[i] = RT_H2LE_U32(pGTCacheEntry->aGTData[i]);
|
---|
5110 | }
|
---|
5111 | pGrainAlloc->fGTUpdateNeeded = false;
|
---|
5112 | uGTBlockIndex = (uSector / pExtent->cSectorsPerGrain) % VMDK_GT_CACHELINE_SIZE;
|
---|
5113 | aGTDataTmp[uGTBlockIndex] = RT_H2LE_U32(VMDK_BYTE2SECTOR(pGrainAlloc->uGrainOffset));
|
---|
5114 | pGTCacheEntry->aGTData[uGTBlockIndex] = VMDK_BYTE2SECTOR(pGrainAlloc->uGrainOffset);
|
---|
5115 | /* Update grain table on disk. */
|
---|
5116 | rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5117 | VMDK_SECTOR2BYTE(uGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
5118 | aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
|
---|
5119 | vmdkAllocGrainAsyncComplete, pGrainAlloc);
|
---|
5120 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5121 | pGrainAlloc->cIoXfersPending++;
|
---|
5122 | else if (RT_FAILURE(rc))
|
---|
5123 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated grain table in '%s'"), pExtent->pszFullname);
|
---|
5124 | if (pExtent->pRGD)
|
---|
5125 | {
|
---|
5126 | /* Update backup grain table on disk. */
|
---|
5127 | rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5128 | VMDK_SECTOR2BYTE(uRGTSector) + (uGTBlock % (pExtent->cGTEntries / VMDK_GT_CACHELINE_SIZE)) * sizeof(aGTDataTmp),
|
---|
5129 | aGTDataTmp, sizeof(aGTDataTmp), pIoCtx,
|
---|
5130 | vmdkAllocGrainAsyncComplete, pGrainAlloc);
|
---|
5131 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5132 | pGrainAlloc->cIoXfersPending++;
|
---|
5133 | else if (RT_FAILURE(rc))
|
---|
5134 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write updated backup grain table in '%s'"), pExtent->pszFullname);
|
---|
5135 | }
|
---|
5136 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
5137 | if (RT_SUCCESS(rc) && pExtent->enmType == VMDKETYPE_ESX_SPARSE)
|
---|
5138 | {
|
---|
5139 | pExtent->uFreeSector = uGTSector + VMDK_BYTE2SECTOR(cbWrite);
|
---|
5140 | pExtent->fMetaDirty = true;
|
---|
5141 | }
|
---|
5142 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
5143 |
|
---|
5144 | LogFlowFunc(("leaving rc=%Rrc\n", rc));
|
---|
5145 |
|
---|
5146 | return rc;
|
---|
5147 | }
|
---|
5148 |
|
---|
5149 | /**
|
---|
5150 | * Internal - complete the grain allocation by updating disk grain table if required.
|
---|
5151 | */
|
---|
5152 | static int vmdkAllocGrainAsyncComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
|
---|
5153 | {
|
---|
5154 | int rc = VINF_SUCCESS;
|
---|
5155 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
5156 | PVMDKGRAINALLOCASYNC pGrainAlloc = (PVMDKGRAINALLOCASYNC)pvUser;
|
---|
5157 | PVMDKEXTENT pExtent = pGrainAlloc->pExtent;
|
---|
5158 |
|
---|
5159 | LogFlowFunc(("pBackendData=%#p pIoCtx=%#p pvUser=%#p rcReq=%Rrc\n",
|
---|
5160 | pBackendData, pIoCtx, pvUser, rcReq));
|
---|
5161 |
|
---|
5162 | pGrainAlloc->cIoXfersPending--;
|
---|
5163 | if (!pGrainAlloc->cIoXfersPending && pGrainAlloc->fGTUpdateNeeded)
|
---|
5164 | rc = vmdkAllocGrainAsyncGTUpdate(pImage, pGrainAlloc->pExtent,
|
---|
5165 | pIoCtx, pGrainAlloc);
|
---|
5166 |
|
---|
5167 | if (!pGrainAlloc->cIoXfersPending)
|
---|
5168 | {
|
---|
5169 | /* Grain allocation completed. */
|
---|
5170 | RTMemFree(pGrainAlloc);
|
---|
5171 | }
|
---|
5172 |
|
---|
5173 | LogFlowFunc(("Leaving rc=%Rrc\n", rc));
|
---|
5174 | return rc;
|
---|
5175 | }
|
---|
5176 |
|
---|
5177 | /**
|
---|
5178 | * Internal. Allocates a new grain table (if necessary) - async version.
|
---|
5179 | */
|
---|
5180 | static int vmdkAllocGrainAsync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
5181 | PVDIOCTX pIoCtx, uint64_t uSector,
|
---|
5182 | uint64_t cbWrite)
|
---|
5183 | {
|
---|
5184 | PVMDKGTCACHE pCache = pImage->pGTCache;
|
---|
5185 | uint64_t uGDIndex, uGTSector, uRGTSector;
|
---|
5186 | uint64_t uFileOffset;
|
---|
5187 | PVMDKGRAINALLOCASYNC pGrainAlloc = NULL;
|
---|
5188 | int rc;
|
---|
5189 |
|
---|
5190 | LogFlowFunc(("pCache=%#p pExtent=%#p pIoCtx=%#p uSector=%llu cbWrite=%llu\n",
|
---|
5191 | pCache, pExtent, pIoCtx, uSector, cbWrite));
|
---|
5192 |
|
---|
5193 | AssertReturn(!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED), VERR_NOT_SUPPORTED);
|
---|
5194 |
|
---|
5195 | pGrainAlloc = (PVMDKGRAINALLOCASYNC)RTMemAllocZ(sizeof(VMDKGRAINALLOCASYNC));
|
---|
5196 | if (!pGrainAlloc)
|
---|
5197 | return VERR_NO_MEMORY;
|
---|
5198 |
|
---|
5199 | pGrainAlloc->pExtent = pExtent;
|
---|
5200 | pGrainAlloc->uSector = uSector;
|
---|
5201 |
|
---|
5202 | uGDIndex = uSector / pExtent->cSectorsPerGDE;
|
---|
5203 | if (uGDIndex >= pExtent->cGDEntries)
|
---|
5204 | {
|
---|
5205 | RTMemFree(pGrainAlloc);
|
---|
5206 | return VERR_OUT_OF_RANGE;
|
---|
5207 | }
|
---|
5208 | uGTSector = pExtent->pGD[uGDIndex];
|
---|
5209 | if (pExtent->pRGD)
|
---|
5210 | uRGTSector = pExtent->pRGD[uGDIndex];
|
---|
5211 | else
|
---|
5212 | uRGTSector = 0; /**< avoid compiler warning */
|
---|
5213 | if (!uGTSector)
|
---|
5214 | {
|
---|
5215 | LogFlow(("Allocating new grain table\n"));
|
---|
5216 |
|
---|
5217 | /* There is no grain table referenced by this grain directory
|
---|
5218 | * entry. So there is absolutely no data in this area. Allocate
|
---|
5219 | * a new grain table and put the reference to it in the GDs. */
|
---|
5220 | uFileOffset = pExtent->uAppendPosition;
|
---|
5221 | if (!uFileOffset)
|
---|
5222 | return VERR_INTERNAL_ERROR;
|
---|
5223 | Assert(!(uFileOffset % 512));
|
---|
5224 |
|
---|
5225 | uFileOffset = RT_ALIGN_64(uFileOffset, 512);
|
---|
5226 | uGTSector = VMDK_BYTE2SECTOR(uFileOffset);
|
---|
5227 |
|
---|
5228 | /* Normally the grain table is preallocated for hosted sparse extents
|
---|
5229 | * that support more than 32 bit sector numbers. So this shouldn't
|
---|
5230 | * ever happen on a valid extent. */
|
---|
5231 | if (uGTSector > UINT32_MAX)
|
---|
5232 | return VERR_VD_VMDK_INVALID_HEADER;
|
---|
5233 |
|
---|
5234 | /* Write grain table by writing the required number of grain table
|
---|
5235 | * cache chunks. Allocate memory dynamically here or we flood the
|
---|
5236 | * metadata cache with very small entries. */
|
---|
5237 | size_t cbGTDataTmp = pExtent->cGTEntries * sizeof(uint32_t);
|
---|
5238 | uint32_t *paGTDataTmp = (uint32_t *)RTMemTmpAllocZ(cbGTDataTmp);
|
---|
5239 |
|
---|
5240 | if (!paGTDataTmp)
|
---|
5241 | return VERR_NO_MEMORY;
|
---|
5242 |
|
---|
5243 | memset(paGTDataTmp, '\0', cbGTDataTmp);
|
---|
5244 | rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5245 | VMDK_SECTOR2BYTE(uGTSector),
|
---|
5246 | paGTDataTmp, cbGTDataTmp, pIoCtx,
|
---|
5247 | vmdkAllocGrainAsyncComplete, pGrainAlloc);
|
---|
5248 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5249 | pGrainAlloc->cIoXfersPending++;
|
---|
5250 | else if (RT_FAILURE(rc))
|
---|
5251 | {
|
---|
5252 | RTMemTmpFree(paGTDataTmp);
|
---|
5253 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain table allocation in '%s'"), pExtent->pszFullname);
|
---|
5254 | }
|
---|
5255 | pExtent->uAppendPosition = RT_ALIGN_64( pExtent->uAppendPosition
|
---|
5256 | + cbGTDataTmp, 512);
|
---|
5257 |
|
---|
5258 | if (pExtent->pRGD)
|
---|
5259 | {
|
---|
5260 | AssertReturn(!uRGTSector, VERR_VD_VMDK_INVALID_HEADER);
|
---|
5261 | uFileOffset = pExtent->uAppendPosition;
|
---|
5262 | if (!uFileOffset)
|
---|
5263 | return VERR_INTERNAL_ERROR;
|
---|
5264 | Assert(!(uFileOffset % 512));
|
---|
5265 | uRGTSector = VMDK_BYTE2SECTOR(uFileOffset);
|
---|
5266 |
|
---|
5267 | /* Normally the redundant grain table is preallocated for hosted
|
---|
5268 | * sparse extents that support more than 32 bit sector numbers. So
|
---|
5269 | * this shouldn't ever happen on a valid extent. */
|
---|
5270 | if (uRGTSector > UINT32_MAX)
|
---|
5271 | {
|
---|
5272 | RTMemTmpFree(paGTDataTmp);
|
---|
5273 | return VERR_VD_VMDK_INVALID_HEADER;
|
---|
5274 | }
|
---|
5275 |
|
---|
5276 | /* Write grain table by writing the required number of grain table
|
---|
5277 | * cache chunks. Allocate memory dynamically here or we flood the
|
---|
5278 | * metadata cache with very small entries. */
|
---|
5279 | rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5280 | VMDK_SECTOR2BYTE(uRGTSector),
|
---|
5281 | paGTDataTmp, cbGTDataTmp, pIoCtx,
|
---|
5282 | vmdkAllocGrainAsyncComplete, pGrainAlloc);
|
---|
5283 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5284 | pGrainAlloc->cIoXfersPending++;
|
---|
5285 | else if (RT_FAILURE(rc))
|
---|
5286 | {
|
---|
5287 | RTMemTmpFree(paGTDataTmp);
|
---|
5288 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain table allocation in '%s'"), pExtent->pszFullname);
|
---|
5289 | }
|
---|
5290 |
|
---|
5291 | pExtent->uAppendPosition = pExtent->uAppendPosition + cbGTDataTmp;
|
---|
5292 | }
|
---|
5293 |
|
---|
5294 | RTMemTmpFree(paGTDataTmp);
|
---|
5295 |
|
---|
5296 | /* Update the grain directory on disk (doing it before writing the
|
---|
5297 | * grain table will result in a garbled extent if the operation is
|
---|
5298 | * aborted for some reason. Otherwise the worst that can happen is
|
---|
5299 | * some unused sectors in the extent. */
|
---|
5300 | uint32_t uGTSectorLE = RT_H2LE_U64(uGTSector);
|
---|
5301 | rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5302 | VMDK_SECTOR2BYTE(pExtent->uSectorGD) + uGDIndex * sizeof(uGTSectorLE),
|
---|
5303 | &uGTSectorLE, sizeof(uGTSectorLE), pIoCtx,
|
---|
5304 | vmdkAllocGrainAsyncComplete, pGrainAlloc);
|
---|
5305 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5306 | pGrainAlloc->cIoXfersPending++;
|
---|
5307 | else if (RT_FAILURE(rc))
|
---|
5308 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
5309 | if (pExtent->pRGD)
|
---|
5310 | {
|
---|
5311 | uint32_t uRGTSectorLE = RT_H2LE_U64(uRGTSector);
|
---|
5312 | rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5313 | VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + uGDIndex * sizeof(uGTSectorLE),
|
---|
5314 | &uRGTSectorLE, sizeof(uRGTSectorLE), pIoCtx,
|
---|
5315 | vmdkAllocGrainAsyncComplete, pGrainAlloc);
|
---|
5316 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5317 | pGrainAlloc->cIoXfersPending++;
|
---|
5318 | else if (RT_FAILURE(rc))
|
---|
5319 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write backup grain directory entry in '%s'"), pExtent->pszFullname);
|
---|
5320 | }
|
---|
5321 |
|
---|
5322 | /* As the final step update the in-memory copy of the GDs. */
|
---|
5323 | pExtent->pGD[uGDIndex] = uGTSector;
|
---|
5324 | if (pExtent->pRGD)
|
---|
5325 | pExtent->pRGD[uGDIndex] = uRGTSector;
|
---|
5326 | }
|
---|
5327 |
|
---|
5328 | LogFlow(("uGTSector=%llu uRGTSector=%llu\n", uGTSector, uRGTSector));
|
---|
5329 | pGrainAlloc->uGTSector = uGTSector;
|
---|
5330 | pGrainAlloc->uRGTSector = uRGTSector;
|
---|
5331 |
|
---|
5332 | uFileOffset = pExtent->uAppendPosition;
|
---|
5333 | if (!uFileOffset)
|
---|
5334 | return VERR_INTERNAL_ERROR;
|
---|
5335 | Assert(!(uFileOffset % 512));
|
---|
5336 |
|
---|
5337 | pGrainAlloc->uGrainOffset = uFileOffset;
|
---|
5338 |
|
---|
5339 | /* Write the data. Always a full grain, or we're in big trouble. */
|
---|
5340 | rc = vdIfIoIntFileWriteUserAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5341 | uFileOffset, pIoCtx, cbWrite,
|
---|
5342 | vmdkAllocGrainAsyncComplete, pGrainAlloc);
|
---|
5343 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5344 | pGrainAlloc->cIoXfersPending++;
|
---|
5345 | else if (RT_FAILURE(rc))
|
---|
5346 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write allocated data block in '%s'"), pExtent->pszFullname);
|
---|
5347 |
|
---|
5348 | pExtent->uAppendPosition += cbWrite;
|
---|
5349 |
|
---|
5350 | rc = vmdkAllocGrainAsyncGTUpdate(pImage, pExtent, pIoCtx, pGrainAlloc);
|
---|
5351 |
|
---|
5352 | if (!pGrainAlloc->cIoXfersPending)
|
---|
5353 | {
|
---|
5354 | /* Grain allocation completed. */
|
---|
5355 | RTMemFree(pGrainAlloc);
|
---|
5356 | }
|
---|
5357 |
|
---|
5358 | LogFlowFunc(("leaving rc=%Rrc\n", rc));
|
---|
5359 |
|
---|
5360 | return rc;
|
---|
5361 | }
|
---|
5362 |
|
---|
5363 | /**
|
---|
5364 | * Internal. Reads the contents by sequentially going over the compressed
|
---|
5365 | * grains (hoping that they are in sequence).
|
---|
5366 | */
|
---|
5367 | static int vmdkStreamReadSequential(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
|
---|
5368 | uint64_t uSector, void *pvBuf,
|
---|
5369 | uint64_t cbRead)
|
---|
5370 | {
|
---|
5371 | int rc;
|
---|
5372 |
|
---|
5373 | LogFlowFunc(("pImage=%#p pExtent=%#p uSector=%llu pvBuf=%#p cbRead=%llu\n",
|
---|
5374 | pImage, pExtent, uSector, pvBuf, cbRead));
|
---|
5375 |
|
---|
5376 | /* Do not allow to go back. */
|
---|
5377 | uint32_t uGrain = uSector / pExtent->cSectorsPerGrain;
|
---|
5378 | if (uGrain < pExtent->uLastGrainAccess)
|
---|
5379 | return VERR_VD_VMDK_INVALID_STATE;
|
---|
5380 | pExtent->uLastGrainAccess = uGrain;
|
---|
5381 |
|
---|
5382 | /* After a previous error do not attempt to recover, as it would need
|
---|
5383 | * seeking (in the general case backwards which is forbidden). */
|
---|
5384 | if (!pExtent->uGrainSectorAbs)
|
---|
5385 | return VERR_VD_VMDK_INVALID_STATE;
|
---|
5386 |
|
---|
5387 | /* Check if we need to read something from the image or if what we have
|
---|
5388 | * in the buffer is good to fulfill the request. */
|
---|
5389 | if (!pExtent->cbGrainStreamRead || uGrain > pExtent->uGrain)
|
---|
5390 | {
|
---|
5391 | uint32_t uGrainSectorAbs = pExtent->uGrainSectorAbs
|
---|
5392 | + VMDK_BYTE2SECTOR(pExtent->cbGrainStreamRead);
|
---|
5393 |
|
---|
5394 | /* Get the marker from the next data block - and skip everything which
|
---|
5395 | * is not a compressed grain. If it's a compressed grain which is for
|
---|
5396 | * the requested sector (or after), read it. */
|
---|
5397 | VMDKMARKER Marker;
|
---|
5398 | do
|
---|
5399 | {
|
---|
5400 | RT_ZERO(Marker);
|
---|
5401 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5402 | VMDK_SECTOR2BYTE(uGrainSectorAbs),
|
---|
5403 | &Marker, RT_OFFSETOF(VMDKMARKER, uType),
|
---|
5404 | NULL);
|
---|
5405 | if (RT_FAILURE(rc))
|
---|
5406 | return rc;
|
---|
5407 | Marker.uSector = RT_LE2H_U64(Marker.uSector);
|
---|
5408 | Marker.cbSize = RT_LE2H_U32(Marker.cbSize);
|
---|
5409 |
|
---|
5410 | if (Marker.cbSize == 0)
|
---|
5411 | {
|
---|
5412 | /* A marker for something else than a compressed grain. */
|
---|
5413 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5414 | VMDK_SECTOR2BYTE(uGrainSectorAbs)
|
---|
5415 | + RT_OFFSETOF(VMDKMARKER, uType),
|
---|
5416 | &Marker.uType, sizeof(Marker.uType),
|
---|
5417 | NULL);
|
---|
5418 | if (RT_FAILURE(rc))
|
---|
5419 | return rc;
|
---|
5420 | Marker.uType = RT_LE2H_U32(Marker.uType);
|
---|
5421 | switch (Marker.uType)
|
---|
5422 | {
|
---|
5423 | case VMDK_MARKER_EOS:
|
---|
5424 | uGrainSectorAbs++;
|
---|
5425 | /* Read (or mostly skip) to the end of file. Uses the
|
---|
5426 | * Marker (LBA sector) as it is unused anyway. This
|
---|
5427 | * makes sure that really everything is read in the
|
---|
5428 | * success case. If this read fails it means the image
|
---|
5429 | * is truncated, but this is harmless so ignore. */
|
---|
5430 | vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
5431 | VMDK_SECTOR2BYTE(uGrainSectorAbs)
|
---|
5432 | + 511,
|
---|
5433 | &Marker.uSector, 1, NULL);
|
---|
5434 | break;
|
---|
5435 | case VMDK_MARKER_GT:
|
---|
5436 | uGrainSectorAbs += 1 + VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
|
---|
5437 | break;
|
---|
5438 | case VMDK_MARKER_GD:
|
---|
5439 | uGrainSectorAbs += 1 + VMDK_BYTE2SECTOR(RT_ALIGN(pExtent->cGDEntries * sizeof(uint32_t), 512));
|
---|
5440 | break;
|
---|
5441 | case VMDK_MARKER_FOOTER:
|
---|
5442 | uGrainSectorAbs += 2;
|
---|
5443 | break;
|
---|
5444 | case VMDK_MARKER_UNSPECIFIED:
|
---|
5445 | /* Skip over the contents of the unspecified marker
|
---|
5446 | * type 4 which exists in some vSphere created files. */
|
---|
5447 | /** @todo figure out what the payload means. */
|
---|
5448 | uGrainSectorAbs += 1;
|
---|
5449 | break;
|
---|
5450 | default:
|
---|
5451 | AssertMsgFailed(("VMDK: corrupted marker, type=%#x\n", Marker.uType));
|
---|
5452 | pExtent->uGrainSectorAbs = 0;
|
---|
5453 | return VERR_VD_VMDK_INVALID_STATE;
|
---|
5454 | }
|
---|
5455 | pExtent->cbGrainStreamRead = 0;
|
---|
5456 | }
|
---|
5457 | else
|
---|
5458 | {
|
---|
5459 | /* A compressed grain marker. If it is at/after what we're
|
---|
5460 | * interested in read and decompress data. */
|
---|
5461 | if (uSector > Marker.uSector + pExtent->cSectorsPerGrain)
|
---|
5462 | {
|
---|
5463 | uGrainSectorAbs += VMDK_BYTE2SECTOR(RT_ALIGN(Marker.cbSize + RT_OFFSETOF(VMDKMARKER, uType), 512));
|
---|
5464 | continue;
|
---|
5465 | }
|
---|
5466 | uint64_t uLBA = 0;
|
---|
5467 | uint32_t cbGrainStreamRead = 0;
|
---|
5468 | rc = vmdkFileInflateSync(pImage, pExtent,
|
---|
5469 | VMDK_SECTOR2BYTE(uGrainSectorAbs),
|
---|
5470 | pExtent->pvGrain,
|
---|
5471 | VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
|
---|
5472 | &Marker, &uLBA, &cbGrainStreamRead);
|
---|
5473 | if (RT_FAILURE(rc))
|
---|
5474 | {
|
---|
5475 | pExtent->uGrainSectorAbs = 0;
|
---|
5476 | return rc;
|
---|
5477 | }
|
---|
5478 | if ( pExtent->uGrain
|
---|
5479 | && uLBA / pExtent->cSectorsPerGrain <= pExtent->uGrain)
|
---|
5480 | {
|
---|
5481 | pExtent->uGrainSectorAbs = 0;
|
---|
5482 | return VERR_VD_VMDK_INVALID_STATE;
|
---|
5483 | }
|
---|
5484 | pExtent->uGrain = uLBA / pExtent->cSectorsPerGrain;
|
---|
5485 | pExtent->cbGrainStreamRead = cbGrainStreamRead;
|
---|
5486 | break;
|
---|
5487 | }
|
---|
5488 | } while (Marker.uType != VMDK_MARKER_EOS);
|
---|
5489 |
|
---|
5490 | pExtent->uGrainSectorAbs = uGrainSectorAbs;
|
---|
5491 |
|
---|
5492 | if (!pExtent->cbGrainStreamRead && Marker.uType == VMDK_MARKER_EOS)
|
---|
5493 | {
|
---|
5494 | pExtent->uGrain = UINT32_MAX;
|
---|
5495 | /* Must set a non-zero value for pExtent->cbGrainStreamRead or
|
---|
5496 | * the next read would try to get more data, and we're at EOF. */
|
---|
5497 | pExtent->cbGrainStreamRead = 1;
|
---|
5498 | }
|
---|
5499 | }
|
---|
5500 |
|
---|
5501 | if (pExtent->uGrain > uSector / pExtent->cSectorsPerGrain)
|
---|
5502 | {
|
---|
5503 | /* The next data block we have is not for this area, so just return
|
---|
5504 | * that there is no data. */
|
---|
5505 | LogFlowFunc(("returns VERR_VD_BLOCK_FREE\n"));
|
---|
5506 | return VERR_VD_BLOCK_FREE;
|
---|
5507 | }
|
---|
5508 |
|
---|
5509 | uint32_t uSectorInGrain = uSector % pExtent->cSectorsPerGrain;
|
---|
5510 | memcpy(pvBuf,
|
---|
5511 | (uint8_t *)pExtent->pvGrain + VMDK_SECTOR2BYTE(uSectorInGrain),
|
---|
5512 | cbRead);
|
---|
5513 | LogFlowFunc(("returns VINF_SUCCESS\n"));
|
---|
5514 | return VINF_SUCCESS;
|
---|
5515 | }
|
---|
5516 |
|
---|
5517 | /**
|
---|
5518 | * Replaces a fragment of a string with the specified string.
|
---|
5519 | *
|
---|
5520 | * @returns Pointer to the allocated UTF-8 string.
|
---|
5521 | * @param pszWhere UTF-8 string to search in.
|
---|
5522 | * @param pszWhat UTF-8 string to search for.
|
---|
5523 | * @param pszByWhat UTF-8 string to replace the found string with.
|
---|
5524 | */
|
---|
5525 | static char *vmdkStrReplace(const char *pszWhere, const char *pszWhat,
|
---|
5526 | const char *pszByWhat)
|
---|
5527 | {
|
---|
5528 | AssertPtr(pszWhere);
|
---|
5529 | AssertPtr(pszWhat);
|
---|
5530 | AssertPtr(pszByWhat);
|
---|
5531 | const char *pszFoundStr = strstr(pszWhere, pszWhat);
|
---|
5532 | if (!pszFoundStr)
|
---|
5533 | return NULL;
|
---|
5534 | size_t cFinal = strlen(pszWhere) + 1 + strlen(pszByWhat) - strlen(pszWhat);
|
---|
5535 | char *pszNewStr = (char *)RTMemAlloc(cFinal);
|
---|
5536 | if (pszNewStr)
|
---|
5537 | {
|
---|
5538 | char *pszTmp = pszNewStr;
|
---|
5539 | memcpy(pszTmp, pszWhere, pszFoundStr - pszWhere);
|
---|
5540 | pszTmp += pszFoundStr - pszWhere;
|
---|
5541 | memcpy(pszTmp, pszByWhat, strlen(pszByWhat));
|
---|
5542 | pszTmp += strlen(pszByWhat);
|
---|
5543 | strcpy(pszTmp, pszFoundStr + strlen(pszWhat));
|
---|
5544 | }
|
---|
5545 | return pszNewStr;
|
---|
5546 | }
|
---|
5547 |
|
---|
5548 |
|
---|
5549 | /** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
|
---|
5550 | static int vmdkCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
|
---|
5551 | PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
|
---|
5552 | {
|
---|
5553 | LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p penmType=%#p\n",
|
---|
5554 | pszFilename, pVDIfsDisk, pVDIfsImage, penmType));
|
---|
5555 | int rc = VINF_SUCCESS;
|
---|
5556 | PVMDKIMAGE pImage;
|
---|
5557 |
|
---|
5558 | if ( !pszFilename
|
---|
5559 | || !*pszFilename
|
---|
5560 | || strchr(pszFilename, '"'))
|
---|
5561 | {
|
---|
5562 | rc = VERR_INVALID_PARAMETER;
|
---|
5563 | goto out;
|
---|
5564 | }
|
---|
5565 |
|
---|
5566 | pImage = (PVMDKIMAGE)RTMemAllocZ(sizeof(VMDKIMAGE));
|
---|
5567 | if (!pImage)
|
---|
5568 | {
|
---|
5569 | rc = VERR_NO_MEMORY;
|
---|
5570 | goto out;
|
---|
5571 | }
|
---|
5572 | pImage->pszFilename = pszFilename;
|
---|
5573 | pImage->pFile = NULL;
|
---|
5574 | pImage->pExtents = NULL;
|
---|
5575 | pImage->pFiles = NULL;
|
---|
5576 | pImage->pGTCache = NULL;
|
---|
5577 | pImage->pDescData = NULL;
|
---|
5578 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
5579 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
5580 | /** @todo speed up this test open (VD_OPEN_FLAGS_INFO) by skipping as
|
---|
5581 | * much as possible in vmdkOpenImage. */
|
---|
5582 | rc = vmdkOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
|
---|
5583 | vmdkFreeImage(pImage, false);
|
---|
5584 | RTMemFree(pImage);
|
---|
5585 |
|
---|
5586 | if (RT_SUCCESS(rc))
|
---|
5587 | *penmType = VDTYPE_HDD;
|
---|
5588 |
|
---|
5589 | out:
|
---|
5590 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
5591 | return rc;
|
---|
5592 | }
|
---|
5593 |
|
---|
5594 | /** @copydoc VBOXHDDBACKEND::pfnOpen */
|
---|
5595 | static int vmdkOpen(const char *pszFilename, unsigned uOpenFlags,
|
---|
5596 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
5597 | VDTYPE enmType, void **ppBackendData)
|
---|
5598 | {
|
---|
5599 | LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
|
---|
5600 | int rc;
|
---|
5601 | PVMDKIMAGE pImage;
|
---|
5602 |
|
---|
5603 | /* Check open flags. All valid flags are supported. */
|
---|
5604 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
5605 | {
|
---|
5606 | rc = VERR_INVALID_PARAMETER;
|
---|
5607 | goto out;
|
---|
5608 | }
|
---|
5609 |
|
---|
5610 | /* Check remaining arguments. */
|
---|
5611 | if ( !VALID_PTR(pszFilename)
|
---|
5612 | || !*pszFilename
|
---|
5613 | || strchr(pszFilename, '"'))
|
---|
5614 | {
|
---|
5615 | rc = VERR_INVALID_PARAMETER;
|
---|
5616 | goto out;
|
---|
5617 | }
|
---|
5618 |
|
---|
5619 | pImage = (PVMDKIMAGE)RTMemAllocZ(sizeof(VMDKIMAGE));
|
---|
5620 | if (!pImage)
|
---|
5621 | {
|
---|
5622 | rc = VERR_NO_MEMORY;
|
---|
5623 | goto out;
|
---|
5624 | }
|
---|
5625 | pImage->pszFilename = pszFilename;
|
---|
5626 | pImage->pFile = NULL;
|
---|
5627 | pImage->pExtents = NULL;
|
---|
5628 | pImage->pFiles = NULL;
|
---|
5629 | pImage->pGTCache = NULL;
|
---|
5630 | pImage->pDescData = NULL;
|
---|
5631 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
5632 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
5633 |
|
---|
5634 | rc = vmdkOpenImage(pImage, uOpenFlags);
|
---|
5635 | if (RT_SUCCESS(rc))
|
---|
5636 | *ppBackendData = pImage;
|
---|
5637 | else
|
---|
5638 | RTMemFree(pImage);
|
---|
5639 |
|
---|
5640 | out:
|
---|
5641 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
5642 | return rc;
|
---|
5643 | }
|
---|
5644 |
|
---|
5645 | /** @copydoc VBOXHDDBACKEND::pfnCreate */
|
---|
5646 | static int vmdkCreate(const char *pszFilename, uint64_t cbSize,
|
---|
5647 | unsigned uImageFlags, const char *pszComment,
|
---|
5648 | PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
|
---|
5649 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
5650 | unsigned uPercentStart, unsigned uPercentSpan,
|
---|
5651 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
5652 | PVDINTERFACE pVDIfsOperation, void **ppBackendData)
|
---|
5653 | {
|
---|
5654 | LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p ppBackendData=%#p\n", pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
|
---|
5655 | int rc;
|
---|
5656 | PVMDKIMAGE pImage;
|
---|
5657 |
|
---|
5658 | PFNVDPROGRESS pfnProgress = NULL;
|
---|
5659 | void *pvUser = NULL;
|
---|
5660 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
5661 | if (pIfProgress)
|
---|
5662 | {
|
---|
5663 | pfnProgress = pIfProgress->pfnProgress;
|
---|
5664 | pvUser = pIfProgress->Core.pvUser;
|
---|
5665 | }
|
---|
5666 |
|
---|
5667 | /* Check the image flags. */
|
---|
5668 | if ((uImageFlags & ~VD_VMDK_IMAGE_FLAGS_MASK) != 0)
|
---|
5669 | {
|
---|
5670 | rc = VERR_VD_INVALID_TYPE;
|
---|
5671 | goto out;
|
---|
5672 | }
|
---|
5673 |
|
---|
5674 | /* Check open flags. All valid flags are supported. */
|
---|
5675 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
5676 | {
|
---|
5677 | rc = VERR_INVALID_PARAMETER;
|
---|
5678 | goto out;
|
---|
5679 | }
|
---|
5680 |
|
---|
5681 | /* Check size. Maximum 2TB-64K for sparse images, otherwise unlimited. */
|
---|
5682 | if ( !cbSize
|
---|
5683 | || (!(uImageFlags & VD_IMAGE_FLAGS_FIXED) && cbSize >= _1T * 2 - _64K))
|
---|
5684 | {
|
---|
5685 | rc = VERR_VD_INVALID_SIZE;
|
---|
5686 | goto out;
|
---|
5687 | }
|
---|
5688 |
|
---|
5689 | /* Check remaining arguments. */
|
---|
5690 | if ( !VALID_PTR(pszFilename)
|
---|
5691 | || !*pszFilename
|
---|
5692 | || strchr(pszFilename, '"')
|
---|
5693 | || !VALID_PTR(pPCHSGeometry)
|
---|
5694 | || !VALID_PTR(pLCHSGeometry)
|
---|
5695 | #ifndef VBOX_WITH_VMDK_ESX
|
---|
5696 | || ( uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX
|
---|
5697 | && !(uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
5698 | #endif
|
---|
5699 | || ( (uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
5700 | && (uImageFlags & ~(VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED | VD_IMAGE_FLAGS_DIFF))))
|
---|
5701 | {
|
---|
5702 | rc = VERR_INVALID_PARAMETER;
|
---|
5703 | goto out;
|
---|
5704 | }
|
---|
5705 |
|
---|
5706 | pImage = (PVMDKIMAGE)RTMemAllocZ(sizeof(VMDKIMAGE));
|
---|
5707 | if (!pImage)
|
---|
5708 | {
|
---|
5709 | rc = VERR_NO_MEMORY;
|
---|
5710 | goto out;
|
---|
5711 | }
|
---|
5712 | pImage->pszFilename = pszFilename;
|
---|
5713 | pImage->pFile = NULL;
|
---|
5714 | pImage->pExtents = NULL;
|
---|
5715 | pImage->pFiles = NULL;
|
---|
5716 | pImage->pGTCache = NULL;
|
---|
5717 | pImage->pDescData = NULL;
|
---|
5718 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
5719 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
5720 | /* Descriptors for split images can be pretty large, especially if the
|
---|
5721 | * filename is long. So prepare for the worst, and allocate quite some
|
---|
5722 | * memory for the descriptor in this case. */
|
---|
5723 | if (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
|
---|
5724 | pImage->cbDescAlloc = VMDK_SECTOR2BYTE(200);
|
---|
5725 | else
|
---|
5726 | pImage->cbDescAlloc = VMDK_SECTOR2BYTE(20);
|
---|
5727 | pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
|
---|
5728 | if (!pImage->pDescData)
|
---|
5729 | {
|
---|
5730 | RTMemFree(pImage);
|
---|
5731 | rc = VERR_NO_MEMORY;
|
---|
5732 | goto out;
|
---|
5733 | }
|
---|
5734 |
|
---|
5735 | rc = vmdkCreateImage(pImage, cbSize, uImageFlags, pszComment,
|
---|
5736 | pPCHSGeometry, pLCHSGeometry, pUuid,
|
---|
5737 | pfnProgress, pvUser, uPercentStart, uPercentSpan);
|
---|
5738 | if (RT_SUCCESS(rc))
|
---|
5739 | {
|
---|
5740 | /* So far the image is opened in read/write mode. Make sure the
|
---|
5741 | * image is opened in read-only mode if the caller requested that. */
|
---|
5742 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
5743 | {
|
---|
5744 | vmdkFreeImage(pImage, false);
|
---|
5745 | rc = vmdkOpenImage(pImage, uOpenFlags);
|
---|
5746 | if (RT_FAILURE(rc))
|
---|
5747 | goto out;
|
---|
5748 | }
|
---|
5749 | *ppBackendData = pImage;
|
---|
5750 | }
|
---|
5751 | else
|
---|
5752 | {
|
---|
5753 | RTMemFree(pImage->pDescData);
|
---|
5754 | RTMemFree(pImage);
|
---|
5755 | }
|
---|
5756 |
|
---|
5757 | out:
|
---|
5758 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
5759 | return rc;
|
---|
5760 | }
|
---|
5761 |
|
---|
5762 | /** @copydoc VBOXHDDBACKEND::pfnRename */
|
---|
5763 | static int vmdkRename(void *pBackendData, const char *pszFilename)
|
---|
5764 | {
|
---|
5765 | LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
|
---|
5766 |
|
---|
5767 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
5768 | int rc = VINF_SUCCESS;
|
---|
5769 | char **apszOldName = NULL;
|
---|
5770 | char **apszNewName = NULL;
|
---|
5771 | char **apszNewLines = NULL;
|
---|
5772 | char *pszOldDescName = NULL;
|
---|
5773 | bool fImageFreed = false;
|
---|
5774 | bool fEmbeddedDesc = false;
|
---|
5775 | unsigned cExtents = 0;
|
---|
5776 | char *pszNewBaseName = NULL;
|
---|
5777 | char *pszOldBaseName = NULL;
|
---|
5778 | char *pszNewFullName = NULL;
|
---|
5779 | char *pszOldFullName = NULL;
|
---|
5780 | const char *pszOldImageName;
|
---|
5781 | unsigned i, line;
|
---|
5782 | VMDKDESCRIPTOR DescriptorCopy;
|
---|
5783 | VMDKEXTENT ExtentCopy;
|
---|
5784 |
|
---|
5785 | memset(&DescriptorCopy, 0, sizeof(DescriptorCopy));
|
---|
5786 |
|
---|
5787 | /* Check arguments. */
|
---|
5788 | if ( !pImage
|
---|
5789 | || (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK)
|
---|
5790 | || !VALID_PTR(pszFilename)
|
---|
5791 | || !*pszFilename)
|
---|
5792 | {
|
---|
5793 | rc = VERR_INVALID_PARAMETER;
|
---|
5794 | goto out;
|
---|
5795 | }
|
---|
5796 |
|
---|
5797 | cExtents = pImage->cExtents;
|
---|
5798 |
|
---|
5799 | /*
|
---|
5800 | * Allocate an array to store both old and new names of renamed files
|
---|
5801 | * in case we have to roll back the changes. Arrays are initialized
|
---|
5802 | * with zeros. We actually save stuff when and if we change it.
|
---|
5803 | */
|
---|
5804 | apszOldName = (char **)RTMemTmpAllocZ((cExtents + 1) * sizeof(char*));
|
---|
5805 | apszNewName = (char **)RTMemTmpAllocZ((cExtents + 1) * sizeof(char*));
|
---|
5806 | apszNewLines = (char **)RTMemTmpAllocZ((cExtents) * sizeof(char*));
|
---|
5807 | if (!apszOldName || !apszNewName || !apszNewLines)
|
---|
5808 | {
|
---|
5809 | rc = VERR_NO_MEMORY;
|
---|
5810 | goto out;
|
---|
5811 | }
|
---|
5812 |
|
---|
5813 | /* Save the descriptor size and position. */
|
---|
5814 | if (pImage->pDescData)
|
---|
5815 | {
|
---|
5816 | /* Separate descriptor file. */
|
---|
5817 | fEmbeddedDesc = false;
|
---|
5818 | }
|
---|
5819 | else
|
---|
5820 | {
|
---|
5821 | /* Embedded descriptor file. */
|
---|
5822 | ExtentCopy = pImage->pExtents[0];
|
---|
5823 | fEmbeddedDesc = true;
|
---|
5824 | }
|
---|
5825 | /* Save the descriptor content. */
|
---|
5826 | DescriptorCopy.cLines = pImage->Descriptor.cLines;
|
---|
5827 | for (i = 0; i < DescriptorCopy.cLines; i++)
|
---|
5828 | {
|
---|
5829 | DescriptorCopy.aLines[i] = RTStrDup(pImage->Descriptor.aLines[i]);
|
---|
5830 | if (!DescriptorCopy.aLines[i])
|
---|
5831 | {
|
---|
5832 | rc = VERR_NO_MEMORY;
|
---|
5833 | goto out;
|
---|
5834 | }
|
---|
5835 | }
|
---|
5836 |
|
---|
5837 | /* Prepare both old and new base names used for string replacement. */
|
---|
5838 | pszNewBaseName = RTStrDup(RTPathFilename(pszFilename));
|
---|
5839 | RTPathStripExt(pszNewBaseName);
|
---|
5840 | pszOldBaseName = RTStrDup(RTPathFilename(pImage->pszFilename));
|
---|
5841 | RTPathStripExt(pszOldBaseName);
|
---|
5842 | /* Prepare both old and new full names used for string replacement. */
|
---|
5843 | pszNewFullName = RTStrDup(pszFilename);
|
---|
5844 | RTPathStripExt(pszNewFullName);
|
---|
5845 | pszOldFullName = RTStrDup(pImage->pszFilename);
|
---|
5846 | RTPathStripExt(pszOldFullName);
|
---|
5847 |
|
---|
5848 | /* --- Up to this point we have not done any damage yet. --- */
|
---|
5849 |
|
---|
5850 | /* Save the old name for easy access to the old descriptor file. */
|
---|
5851 | pszOldDescName = RTStrDup(pImage->pszFilename);
|
---|
5852 | /* Save old image name. */
|
---|
5853 | pszOldImageName = pImage->pszFilename;
|
---|
5854 |
|
---|
5855 | /* Update the descriptor with modified extent names. */
|
---|
5856 | for (i = 0, line = pImage->Descriptor.uFirstExtent;
|
---|
5857 | i < cExtents;
|
---|
5858 | i++, line = pImage->Descriptor.aNextLines[line])
|
---|
5859 | {
|
---|
5860 | /* Assume that vmdkStrReplace will fail. */
|
---|
5861 | rc = VERR_NO_MEMORY;
|
---|
5862 | /* Update the descriptor. */
|
---|
5863 | apszNewLines[i] = vmdkStrReplace(pImage->Descriptor.aLines[line],
|
---|
5864 | pszOldBaseName, pszNewBaseName);
|
---|
5865 | if (!apszNewLines[i])
|
---|
5866 | goto rollback;
|
---|
5867 | pImage->Descriptor.aLines[line] = apszNewLines[i];
|
---|
5868 | }
|
---|
5869 | /* Make sure the descriptor gets written back. */
|
---|
5870 | pImage->Descriptor.fDirty = true;
|
---|
5871 | /* Flush the descriptor now, in case it is embedded. */
|
---|
5872 | vmdkFlushImage(pImage);
|
---|
5873 |
|
---|
5874 | /* Close and rename/move extents. */
|
---|
5875 | for (i = 0; i < cExtents; i++)
|
---|
5876 | {
|
---|
5877 | PVMDKEXTENT pExtent = &pImage->pExtents[i];
|
---|
5878 | /* Compose new name for the extent. */
|
---|
5879 | apszNewName[i] = vmdkStrReplace(pExtent->pszFullname,
|
---|
5880 | pszOldFullName, pszNewFullName);
|
---|
5881 | if (!apszNewName[i])
|
---|
5882 | goto rollback;
|
---|
5883 | /* Close the extent file. */
|
---|
5884 | vmdkFileClose(pImage, &pExtent->pFile, false);
|
---|
5885 | /* Rename the extent file. */
|
---|
5886 | rc = vdIfIoIntFileMove(pImage->pIfIo, pExtent->pszFullname, apszNewName[i], 0);
|
---|
5887 | if (RT_FAILURE(rc))
|
---|
5888 | goto rollback;
|
---|
5889 | /* Remember the old name. */
|
---|
5890 | apszOldName[i] = RTStrDup(pExtent->pszFullname);
|
---|
5891 | }
|
---|
5892 | /* Release all old stuff. */
|
---|
5893 | vmdkFreeImage(pImage, false);
|
---|
5894 |
|
---|
5895 | fImageFreed = true;
|
---|
5896 |
|
---|
5897 | /* Last elements of new/old name arrays are intended for
|
---|
5898 | * storing descriptor's names.
|
---|
5899 | */
|
---|
5900 | apszNewName[cExtents] = RTStrDup(pszFilename);
|
---|
5901 | /* Rename the descriptor file if it's separate. */
|
---|
5902 | if (!fEmbeddedDesc)
|
---|
5903 | {
|
---|
5904 | rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, apszNewName[cExtents], 0);
|
---|
5905 | if (RT_FAILURE(rc))
|
---|
5906 | goto rollback;
|
---|
5907 | /* Save old name only if we may need to change it back. */
|
---|
5908 | apszOldName[cExtents] = RTStrDup(pszFilename);
|
---|
5909 | }
|
---|
5910 |
|
---|
5911 | /* Update pImage with the new information. */
|
---|
5912 | pImage->pszFilename = pszFilename;
|
---|
5913 |
|
---|
5914 | /* Open the new image. */
|
---|
5915 | rc = vmdkOpenImage(pImage, pImage->uOpenFlags);
|
---|
5916 | if (RT_SUCCESS(rc))
|
---|
5917 | goto out;
|
---|
5918 |
|
---|
5919 | rollback:
|
---|
5920 | /* Roll back all changes in case of failure. */
|
---|
5921 | if (RT_FAILURE(rc))
|
---|
5922 | {
|
---|
5923 | int rrc;
|
---|
5924 | if (!fImageFreed)
|
---|
5925 | {
|
---|
5926 | /*
|
---|
5927 | * Some extents may have been closed, close the rest. We will
|
---|
5928 | * re-open the whole thing later.
|
---|
5929 | */
|
---|
5930 | vmdkFreeImage(pImage, false);
|
---|
5931 | }
|
---|
5932 | /* Rename files back. */
|
---|
5933 | for (i = 0; i <= cExtents; i++)
|
---|
5934 | {
|
---|
5935 | if (apszOldName[i])
|
---|
5936 | {
|
---|
5937 | rrc = vdIfIoIntFileMove(pImage->pIfIo, apszNewName[i], apszOldName[i], 0);
|
---|
5938 | AssertRC(rrc);
|
---|
5939 | }
|
---|
5940 | }
|
---|
5941 | /* Restore the old descriptor. */
|
---|
5942 | PVMDKFILE pFile;
|
---|
5943 | rrc = vmdkFileOpen(pImage, &pFile, pszOldDescName,
|
---|
5944 | VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_NORMAL,
|
---|
5945 | false /* fCreate */),
|
---|
5946 | false /* fAsyncIO */);
|
---|
5947 | AssertRC(rrc);
|
---|
5948 | if (fEmbeddedDesc)
|
---|
5949 | {
|
---|
5950 | ExtentCopy.pFile = pFile;
|
---|
5951 | pImage->pExtents = &ExtentCopy;
|
---|
5952 | }
|
---|
5953 | else
|
---|
5954 | {
|
---|
5955 | /* Shouldn't be null for separate descriptor.
|
---|
5956 | * There will be no access to the actual content.
|
---|
5957 | */
|
---|
5958 | pImage->pDescData = pszOldDescName;
|
---|
5959 | pImage->pFile = pFile;
|
---|
5960 | }
|
---|
5961 | pImage->Descriptor = DescriptorCopy;
|
---|
5962 | vmdkWriteDescriptor(pImage);
|
---|
5963 | vmdkFileClose(pImage, &pFile, false);
|
---|
5964 | /* Get rid of the stuff we implanted. */
|
---|
5965 | pImage->pExtents = NULL;
|
---|
5966 | pImage->pFile = NULL;
|
---|
5967 | pImage->pDescData = NULL;
|
---|
5968 | /* Re-open the image back. */
|
---|
5969 | pImage->pszFilename = pszOldImageName;
|
---|
5970 | rrc = vmdkOpenImage(pImage, pImage->uOpenFlags);
|
---|
5971 | AssertRC(rrc);
|
---|
5972 | }
|
---|
5973 |
|
---|
5974 | out:
|
---|
5975 | for (i = 0; i < DescriptorCopy.cLines; i++)
|
---|
5976 | if (DescriptorCopy.aLines[i])
|
---|
5977 | RTStrFree(DescriptorCopy.aLines[i]);
|
---|
5978 | if (apszOldName)
|
---|
5979 | {
|
---|
5980 | for (i = 0; i <= cExtents; i++)
|
---|
5981 | if (apszOldName[i])
|
---|
5982 | RTStrFree(apszOldName[i]);
|
---|
5983 | RTMemTmpFree(apszOldName);
|
---|
5984 | }
|
---|
5985 | if (apszNewName)
|
---|
5986 | {
|
---|
5987 | for (i = 0; i <= cExtents; i++)
|
---|
5988 | if (apszNewName[i])
|
---|
5989 | RTStrFree(apszNewName[i]);
|
---|
5990 | RTMemTmpFree(apszNewName);
|
---|
5991 | }
|
---|
5992 | if (apszNewLines)
|
---|
5993 | {
|
---|
5994 | for (i = 0; i < cExtents; i++)
|
---|
5995 | if (apszNewLines[i])
|
---|
5996 | RTStrFree(apszNewLines[i]);
|
---|
5997 | RTMemTmpFree(apszNewLines);
|
---|
5998 | }
|
---|
5999 | if (pszOldDescName)
|
---|
6000 | RTStrFree(pszOldDescName);
|
---|
6001 | if (pszOldBaseName)
|
---|
6002 | RTStrFree(pszOldBaseName);
|
---|
6003 | if (pszNewBaseName)
|
---|
6004 | RTStrFree(pszNewBaseName);
|
---|
6005 | if (pszOldFullName)
|
---|
6006 | RTStrFree(pszOldFullName);
|
---|
6007 | if (pszNewFullName)
|
---|
6008 | RTStrFree(pszNewFullName);
|
---|
6009 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6010 | return rc;
|
---|
6011 | }
|
---|
6012 |
|
---|
6013 | /** @copydoc VBOXHDDBACKEND::pfnClose */
|
---|
6014 | static int vmdkClose(void *pBackendData, bool fDelete)
|
---|
6015 | {
|
---|
6016 | LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
|
---|
6017 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6018 | int rc;
|
---|
6019 |
|
---|
6020 | rc = vmdkFreeImage(pImage, fDelete);
|
---|
6021 | RTMemFree(pImage);
|
---|
6022 |
|
---|
6023 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6024 | return rc;
|
---|
6025 | }
|
---|
6026 |
|
---|
6027 | /** @copydoc VBOXHDDBACKEND::pfnRead */
|
---|
6028 | static int vmdkRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
|
---|
6029 | size_t cbToRead, size_t *pcbActuallyRead)
|
---|
6030 | {
|
---|
6031 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
|
---|
6032 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6033 | PVMDKEXTENT pExtent;
|
---|
6034 | uint64_t uSectorExtentRel;
|
---|
6035 | uint64_t uSectorExtentAbs;
|
---|
6036 | int rc;
|
---|
6037 |
|
---|
6038 | AssertPtr(pImage);
|
---|
6039 | Assert(uOffset % 512 == 0);
|
---|
6040 | Assert(cbToRead % 512 == 0);
|
---|
6041 |
|
---|
6042 | if ( uOffset + cbToRead > pImage->cbSize
|
---|
6043 | || cbToRead == 0)
|
---|
6044 | {
|
---|
6045 | rc = VERR_INVALID_PARAMETER;
|
---|
6046 | goto out;
|
---|
6047 | }
|
---|
6048 |
|
---|
6049 | rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
|
---|
6050 | &pExtent, &uSectorExtentRel);
|
---|
6051 | if (RT_FAILURE(rc))
|
---|
6052 | goto out;
|
---|
6053 |
|
---|
6054 | /* Check access permissions as defined in the extent descriptor. */
|
---|
6055 | if (pExtent->enmAccess == VMDKACCESS_NOACCESS)
|
---|
6056 | {
|
---|
6057 | rc = VERR_VD_VMDK_INVALID_STATE;
|
---|
6058 | goto out;
|
---|
6059 | }
|
---|
6060 |
|
---|
6061 | /* Clip read range to remain in this extent. */
|
---|
6062 | cbToRead = RT_MIN(cbToRead, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
6063 |
|
---|
6064 | /* Handle the read according to the current extent type. */
|
---|
6065 | switch (pExtent->enmType)
|
---|
6066 | {
|
---|
6067 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
6068 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
6069 | case VMDKETYPE_ESX_SPARSE:
|
---|
6070 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
6071 | rc = vmdkGetSector(pImage, pExtent, uSectorExtentRel,
|
---|
6072 | &uSectorExtentAbs);
|
---|
6073 | if (RT_FAILURE(rc))
|
---|
6074 | goto out;
|
---|
6075 | /* Clip read range to at most the rest of the grain. */
|
---|
6076 | cbToRead = RT_MIN(cbToRead, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSectorExtentRel % pExtent->cSectorsPerGrain));
|
---|
6077 | Assert(!(cbToRead % 512));
|
---|
6078 | if (uSectorExtentAbs == 0)
|
---|
6079 | {
|
---|
6080 | if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
6081 | || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6082 | || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
|
---|
6083 | rc = VERR_VD_BLOCK_FREE;
|
---|
6084 | else
|
---|
6085 | rc = vmdkStreamReadSequential(pImage, pExtent,
|
---|
6086 | uSectorExtentRel,
|
---|
6087 | pvBuf, cbToRead);
|
---|
6088 | }
|
---|
6089 | else
|
---|
6090 | {
|
---|
6091 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
6092 | {
|
---|
6093 | uint32_t uSectorInGrain = uSectorExtentRel % pExtent->cSectorsPerGrain;
|
---|
6094 | uSectorExtentAbs -= uSectorInGrain;
|
---|
6095 | uint64_t uLBA;
|
---|
6096 | if (pExtent->uGrainSectorAbs != uSectorExtentAbs)
|
---|
6097 | {
|
---|
6098 | rc = vmdkFileInflateSync(pImage, pExtent,
|
---|
6099 | VMDK_SECTOR2BYTE(uSectorExtentAbs),
|
---|
6100 | pExtent->pvGrain,
|
---|
6101 | VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain),
|
---|
6102 | NULL, &uLBA, NULL);
|
---|
6103 | if (RT_FAILURE(rc))
|
---|
6104 | {
|
---|
6105 | pExtent->uGrainSectorAbs = 0;
|
---|
6106 | AssertRC(rc);
|
---|
6107 | goto out;
|
---|
6108 | }
|
---|
6109 | pExtent->uGrainSectorAbs = uSectorExtentAbs;
|
---|
6110 | pExtent->uGrain = uSectorExtentRel / pExtent->cSectorsPerGrain;
|
---|
6111 | Assert(uLBA == uSectorExtentRel);
|
---|
6112 | }
|
---|
6113 | memcpy(pvBuf, (uint8_t *)pExtent->pvGrain + VMDK_SECTOR2BYTE(uSectorInGrain), cbToRead);
|
---|
6114 | }
|
---|
6115 | else
|
---|
6116 | {
|
---|
6117 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
6118 | VMDK_SECTOR2BYTE(uSectorExtentAbs),
|
---|
6119 | pvBuf, cbToRead, NULL);
|
---|
6120 | }
|
---|
6121 | }
|
---|
6122 | break;
|
---|
6123 | case VMDKETYPE_VMFS:
|
---|
6124 | case VMDKETYPE_FLAT:
|
---|
6125 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
6126 | VMDK_SECTOR2BYTE(uSectorExtentRel),
|
---|
6127 | pvBuf, cbToRead, NULL);
|
---|
6128 | break;
|
---|
6129 | case VMDKETYPE_ZERO:
|
---|
6130 | memset(pvBuf, '\0', cbToRead);
|
---|
6131 | break;
|
---|
6132 | }
|
---|
6133 | if (pcbActuallyRead)
|
---|
6134 | *pcbActuallyRead = cbToRead;
|
---|
6135 |
|
---|
6136 | out:
|
---|
6137 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6138 | return rc;
|
---|
6139 | }
|
---|
6140 |
|
---|
6141 | /** @copydoc VBOXHDDBACKEND::pfnWrite */
|
---|
6142 | static int vmdkWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
|
---|
6143 | size_t cbToWrite, size_t *pcbWriteProcess,
|
---|
6144 | size_t *pcbPreRead, size_t *pcbPostRead, unsigned fWrite)
|
---|
6145 | {
|
---|
6146 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
|
---|
6147 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6148 | PVMDKEXTENT pExtent;
|
---|
6149 | uint64_t uSectorExtentRel;
|
---|
6150 | uint64_t uSectorExtentAbs;
|
---|
6151 | int rc;
|
---|
6152 |
|
---|
6153 | AssertPtr(pImage);
|
---|
6154 | Assert(uOffset % 512 == 0);
|
---|
6155 | Assert(cbToWrite % 512 == 0);
|
---|
6156 |
|
---|
6157 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6158 | {
|
---|
6159 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
6160 | goto out;
|
---|
6161 | }
|
---|
6162 |
|
---|
6163 | if (cbToWrite == 0)
|
---|
6164 | {
|
---|
6165 | rc = VERR_INVALID_PARAMETER;
|
---|
6166 | goto out;
|
---|
6167 | }
|
---|
6168 |
|
---|
6169 | /* No size check here, will do that later when the extent is located.
|
---|
6170 | * There are sparse images out there which according to the spec are
|
---|
6171 | * invalid, because the total size is not a multiple of the grain size.
|
---|
6172 | * Also for sparse images which are stitched together in odd ways (not at
|
---|
6173 | * grain boundaries, and with the nominal size not being a multiple of the
|
---|
6174 | * grain size), this would prevent writing to the last grain. */
|
---|
6175 |
|
---|
6176 | rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
|
---|
6177 | &pExtent, &uSectorExtentRel);
|
---|
6178 | if (RT_FAILURE(rc))
|
---|
6179 | goto out;
|
---|
6180 |
|
---|
6181 | /* Check access permissions as defined in the extent descriptor. */
|
---|
6182 | if ( pExtent->enmAccess != VMDKACCESS_READWRITE
|
---|
6183 | && ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
6184 | && !pImage->pExtents[0].uAppendPosition
|
---|
6185 | && pExtent->enmAccess != VMDKACCESS_READONLY))
|
---|
6186 | {
|
---|
6187 | rc = VERR_VD_VMDK_INVALID_STATE;
|
---|
6188 | goto out;
|
---|
6189 | }
|
---|
6190 |
|
---|
6191 | /* Handle the write according to the current extent type. */
|
---|
6192 | switch (pExtent->enmType)
|
---|
6193 | {
|
---|
6194 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
6195 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
6196 | case VMDKETYPE_ESX_SPARSE:
|
---|
6197 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
6198 | rc = vmdkGetSector(pImage, pExtent, uSectorExtentRel,
|
---|
6199 | &uSectorExtentAbs);
|
---|
6200 | if (RT_FAILURE(rc))
|
---|
6201 | goto out;
|
---|
6202 | /* Clip write range to at most the rest of the grain. */
|
---|
6203 | cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSectorExtentRel % pExtent->cSectorsPerGrain));
|
---|
6204 | if ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
|
---|
6205 | && uSectorExtentRel < (uint64_t)pExtent->uLastGrainAccess * pExtent->cSectorsPerGrain)
|
---|
6206 | {
|
---|
6207 | rc = VERR_VD_VMDK_INVALID_WRITE;
|
---|
6208 | goto out;
|
---|
6209 | }
|
---|
6210 | if (uSectorExtentAbs == 0)
|
---|
6211 | {
|
---|
6212 | if (!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6213 | {
|
---|
6214 | if (cbToWrite == VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
|
---|
6215 | {
|
---|
6216 | /* Full block write to a previously unallocated block.
|
---|
6217 | * Check if the caller wants feedback. */
|
---|
6218 | if (!(fWrite & VD_WRITE_NO_ALLOC))
|
---|
6219 | {
|
---|
6220 | /* Allocate GT and store the grain. */
|
---|
6221 | rc = vmdkAllocGrain(pImage, pExtent,
|
---|
6222 | uSectorExtentRel,
|
---|
6223 | pvBuf, cbToWrite);
|
---|
6224 | }
|
---|
6225 | else
|
---|
6226 | rc = VERR_VD_BLOCK_FREE;
|
---|
6227 | *pcbPreRead = 0;
|
---|
6228 | *pcbPostRead = 0;
|
---|
6229 | }
|
---|
6230 | else
|
---|
6231 | {
|
---|
6232 | /* Clip write range to remain in this extent. */
|
---|
6233 | cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
6234 | *pcbPreRead = VMDK_SECTOR2BYTE(uSectorExtentRel % pExtent->cSectorsPerGrain);
|
---|
6235 | *pcbPostRead = VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain) - cbToWrite - *pcbPreRead;
|
---|
6236 | rc = VERR_VD_BLOCK_FREE;
|
---|
6237 | }
|
---|
6238 | }
|
---|
6239 | else
|
---|
6240 | {
|
---|
6241 | rc = vmdkStreamAllocGrain(pImage, pExtent,
|
---|
6242 | uSectorExtentRel,
|
---|
6243 | pvBuf, cbToWrite);
|
---|
6244 | }
|
---|
6245 | }
|
---|
6246 | else
|
---|
6247 | {
|
---|
6248 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
6249 | {
|
---|
6250 | /* A partial write to a streamOptimized image is simply
|
---|
6251 | * invalid. It requires rewriting already compressed data
|
---|
6252 | * which is somewhere between expensive and impossible. */
|
---|
6253 | rc = VERR_VD_VMDK_INVALID_STATE;
|
---|
6254 | pExtent->uGrainSectorAbs = 0;
|
---|
6255 | AssertRC(rc);
|
---|
6256 | }
|
---|
6257 | else
|
---|
6258 | {
|
---|
6259 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
6260 | VMDK_SECTOR2BYTE(uSectorExtentAbs),
|
---|
6261 | pvBuf, cbToWrite, NULL);
|
---|
6262 | }
|
---|
6263 | }
|
---|
6264 | break;
|
---|
6265 | case VMDKETYPE_VMFS:
|
---|
6266 | case VMDKETYPE_FLAT:
|
---|
6267 | /* Clip write range to remain in this extent. */
|
---|
6268 | cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
6269 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
6270 | VMDK_SECTOR2BYTE(uSectorExtentRel),
|
---|
6271 | pvBuf, cbToWrite, NULL);
|
---|
6272 | break;
|
---|
6273 | case VMDKETYPE_ZERO:
|
---|
6274 | /* Clip write range to remain in this extent. */
|
---|
6275 | cbToWrite = RT_MIN(cbToWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
6276 | break;
|
---|
6277 | }
|
---|
6278 |
|
---|
6279 | if (pcbWriteProcess)
|
---|
6280 | *pcbWriteProcess = cbToWrite;
|
---|
6281 |
|
---|
6282 | out:
|
---|
6283 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6284 | return rc;
|
---|
6285 | }
|
---|
6286 |
|
---|
6287 | /** @copydoc VBOXHDDBACKEND::pfnFlush */
|
---|
6288 | static int vmdkFlush(void *pBackendData)
|
---|
6289 | {
|
---|
6290 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
6291 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6292 | int rc = VINF_SUCCESS;
|
---|
6293 |
|
---|
6294 | AssertPtr(pImage);
|
---|
6295 |
|
---|
6296 | if (!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6297 | rc = vmdkFlushImage(pImage);
|
---|
6298 |
|
---|
6299 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6300 | return rc;
|
---|
6301 | }
|
---|
6302 |
|
---|
6303 | /** @copydoc VBOXHDDBACKEND::pfnGetVersion */
|
---|
6304 | static unsigned vmdkGetVersion(void *pBackendData)
|
---|
6305 | {
|
---|
6306 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
6307 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6308 |
|
---|
6309 | AssertPtr(pImage);
|
---|
6310 |
|
---|
6311 | if (pImage)
|
---|
6312 | return VMDK_IMAGE_VERSION;
|
---|
6313 | else
|
---|
6314 | return 0;
|
---|
6315 | }
|
---|
6316 |
|
---|
6317 | /** @copydoc VBOXHDDBACKEND::pfnGetSize */
|
---|
6318 | static uint64_t vmdkGetSize(void *pBackendData)
|
---|
6319 | {
|
---|
6320 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
6321 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6322 |
|
---|
6323 | AssertPtr(pImage);
|
---|
6324 |
|
---|
6325 | if (pImage)
|
---|
6326 | return pImage->cbSize;
|
---|
6327 | else
|
---|
6328 | return 0;
|
---|
6329 | }
|
---|
6330 |
|
---|
6331 | /** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
|
---|
6332 | static uint64_t vmdkGetFileSize(void *pBackendData)
|
---|
6333 | {
|
---|
6334 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
6335 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6336 | uint64_t cb = 0;
|
---|
6337 |
|
---|
6338 | AssertPtr(pImage);
|
---|
6339 |
|
---|
6340 | if (pImage)
|
---|
6341 | {
|
---|
6342 | uint64_t cbFile;
|
---|
6343 | if (pImage->pFile != NULL)
|
---|
6344 | {
|
---|
6345 | int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pFile->pStorage, &cbFile);
|
---|
6346 | if (RT_SUCCESS(rc))
|
---|
6347 | cb += cbFile;
|
---|
6348 | }
|
---|
6349 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
6350 | {
|
---|
6351 | if (pImage->pExtents[i].pFile != NULL)
|
---|
6352 | {
|
---|
6353 | int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pExtents[i].pFile->pStorage, &cbFile);
|
---|
6354 | if (RT_SUCCESS(rc))
|
---|
6355 | cb += cbFile;
|
---|
6356 | }
|
---|
6357 | }
|
---|
6358 | }
|
---|
6359 |
|
---|
6360 | LogFlowFunc(("returns %lld\n", cb));
|
---|
6361 | return cb;
|
---|
6362 | }
|
---|
6363 |
|
---|
6364 | /** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
|
---|
6365 | static int vmdkGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
|
---|
6366 | {
|
---|
6367 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
|
---|
6368 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6369 | int rc;
|
---|
6370 |
|
---|
6371 | AssertPtr(pImage);
|
---|
6372 |
|
---|
6373 | if (pImage)
|
---|
6374 | {
|
---|
6375 | if (pImage->PCHSGeometry.cCylinders)
|
---|
6376 | {
|
---|
6377 | *pPCHSGeometry = pImage->PCHSGeometry;
|
---|
6378 | rc = VINF_SUCCESS;
|
---|
6379 | }
|
---|
6380 | else
|
---|
6381 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
6382 | }
|
---|
6383 | else
|
---|
6384 | rc = VERR_VD_NOT_OPENED;
|
---|
6385 |
|
---|
6386 | LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
6387 | return rc;
|
---|
6388 | }
|
---|
6389 |
|
---|
6390 | /** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
|
---|
6391 | static int vmdkSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
|
---|
6392 | {
|
---|
6393 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
6394 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6395 | int rc;
|
---|
6396 |
|
---|
6397 | AssertPtr(pImage);
|
---|
6398 |
|
---|
6399 | if (pImage)
|
---|
6400 | {
|
---|
6401 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6402 | {
|
---|
6403 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
6404 | goto out;
|
---|
6405 | }
|
---|
6406 | if (pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
6407 | {
|
---|
6408 | rc = VERR_NOT_SUPPORTED;
|
---|
6409 | goto out;
|
---|
6410 | }
|
---|
6411 | rc = vmdkDescSetPCHSGeometry(pImage, pPCHSGeometry);
|
---|
6412 | if (RT_FAILURE(rc))
|
---|
6413 | goto out;
|
---|
6414 |
|
---|
6415 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
6416 | rc = VINF_SUCCESS;
|
---|
6417 | }
|
---|
6418 | else
|
---|
6419 | rc = VERR_VD_NOT_OPENED;
|
---|
6420 |
|
---|
6421 | out:
|
---|
6422 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6423 | return rc;
|
---|
6424 | }
|
---|
6425 |
|
---|
6426 | /** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
|
---|
6427 | static int vmdkGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
|
---|
6428 | {
|
---|
6429 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
|
---|
6430 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6431 | int rc;
|
---|
6432 |
|
---|
6433 | AssertPtr(pImage);
|
---|
6434 |
|
---|
6435 | if (pImage)
|
---|
6436 | {
|
---|
6437 | if (pImage->LCHSGeometry.cCylinders)
|
---|
6438 | {
|
---|
6439 | *pLCHSGeometry = pImage->LCHSGeometry;
|
---|
6440 | rc = VINF_SUCCESS;
|
---|
6441 | }
|
---|
6442 | else
|
---|
6443 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
6444 | }
|
---|
6445 | else
|
---|
6446 | rc = VERR_VD_NOT_OPENED;
|
---|
6447 |
|
---|
6448 | LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
6449 | return rc;
|
---|
6450 | }
|
---|
6451 |
|
---|
6452 | /** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
|
---|
6453 | static int vmdkSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
|
---|
6454 | {
|
---|
6455 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
6456 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6457 | int rc;
|
---|
6458 |
|
---|
6459 | AssertPtr(pImage);
|
---|
6460 |
|
---|
6461 | if (pImage)
|
---|
6462 | {
|
---|
6463 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6464 | {
|
---|
6465 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
6466 | goto out;
|
---|
6467 | }
|
---|
6468 | if (pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
6469 | {
|
---|
6470 | rc = VERR_NOT_SUPPORTED;
|
---|
6471 | goto out;
|
---|
6472 | }
|
---|
6473 | rc = vmdkDescSetLCHSGeometry(pImage, pLCHSGeometry);
|
---|
6474 | if (RT_FAILURE(rc))
|
---|
6475 | goto out;
|
---|
6476 |
|
---|
6477 | pImage->LCHSGeometry = *pLCHSGeometry;
|
---|
6478 | rc = VINF_SUCCESS;
|
---|
6479 | }
|
---|
6480 | else
|
---|
6481 | rc = VERR_VD_NOT_OPENED;
|
---|
6482 |
|
---|
6483 | out:
|
---|
6484 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6485 | return rc;
|
---|
6486 | }
|
---|
6487 |
|
---|
6488 | /** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
|
---|
6489 | static unsigned vmdkGetImageFlags(void *pBackendData)
|
---|
6490 | {
|
---|
6491 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
6492 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6493 | unsigned uImageFlags;
|
---|
6494 |
|
---|
6495 | AssertPtr(pImage);
|
---|
6496 |
|
---|
6497 | if (pImage)
|
---|
6498 | uImageFlags = pImage->uImageFlags;
|
---|
6499 | else
|
---|
6500 | uImageFlags = 0;
|
---|
6501 |
|
---|
6502 | LogFlowFunc(("returns %#x\n", uImageFlags));
|
---|
6503 | return uImageFlags;
|
---|
6504 | }
|
---|
6505 |
|
---|
6506 | /** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
|
---|
6507 | static unsigned vmdkGetOpenFlags(void *pBackendData)
|
---|
6508 | {
|
---|
6509 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
6510 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6511 | unsigned uOpenFlags;
|
---|
6512 |
|
---|
6513 | AssertPtr(pImage);
|
---|
6514 |
|
---|
6515 | if (pImage)
|
---|
6516 | uOpenFlags = pImage->uOpenFlags;
|
---|
6517 | else
|
---|
6518 | uOpenFlags = 0;
|
---|
6519 |
|
---|
6520 | LogFlowFunc(("returns %#x\n", uOpenFlags));
|
---|
6521 | return uOpenFlags;
|
---|
6522 | }
|
---|
6523 |
|
---|
6524 | /** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
|
---|
6525 | static int vmdkSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
|
---|
6526 | {
|
---|
6527 | LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
|
---|
6528 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6529 | int rc;
|
---|
6530 |
|
---|
6531 | /* Image must be opened and the new flags must be valid. */
|
---|
6532 | if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE | VD_OPEN_FLAGS_SEQUENTIAL)))
|
---|
6533 | {
|
---|
6534 | rc = VERR_INVALID_PARAMETER;
|
---|
6535 | goto out;
|
---|
6536 | }
|
---|
6537 |
|
---|
6538 | /* StreamOptimized images need special treatment: reopen is prohibited. */
|
---|
6539 | if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
6540 | {
|
---|
6541 | if (pImage->uOpenFlags == uOpenFlags)
|
---|
6542 | rc = VINF_SUCCESS;
|
---|
6543 | else
|
---|
6544 | rc = VERR_INVALID_PARAMETER;
|
---|
6545 | goto out;
|
---|
6546 | }
|
---|
6547 |
|
---|
6548 | /* Implement this operation via reopening the image. */
|
---|
6549 | vmdkFreeImage(pImage, false);
|
---|
6550 | rc = vmdkOpenImage(pImage, uOpenFlags);
|
---|
6551 |
|
---|
6552 | out:
|
---|
6553 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6554 | return rc;
|
---|
6555 | }
|
---|
6556 |
|
---|
6557 | /** @copydoc VBOXHDDBACKEND::pfnGetComment */
|
---|
6558 | static int vmdkGetComment(void *pBackendData, char *pszComment,
|
---|
6559 | size_t cbComment)
|
---|
6560 | {
|
---|
6561 | LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
|
---|
6562 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6563 | int rc;
|
---|
6564 |
|
---|
6565 | AssertPtr(pImage);
|
---|
6566 |
|
---|
6567 | if (pImage)
|
---|
6568 | {
|
---|
6569 | const char *pszCommentEncoded = NULL;
|
---|
6570 | rc = vmdkDescDDBGetStr(pImage, &pImage->Descriptor,
|
---|
6571 | "ddb.comment", &pszCommentEncoded);
|
---|
6572 | if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
|
---|
6573 | pszCommentEncoded = NULL;
|
---|
6574 | else if (RT_FAILURE(rc))
|
---|
6575 | goto out;
|
---|
6576 |
|
---|
6577 | if (pszComment && pszCommentEncoded)
|
---|
6578 | rc = vmdkDecodeString(pszCommentEncoded, pszComment, cbComment);
|
---|
6579 | else
|
---|
6580 | {
|
---|
6581 | if (pszComment)
|
---|
6582 | *pszComment = '\0';
|
---|
6583 | rc = VINF_SUCCESS;
|
---|
6584 | }
|
---|
6585 | if (pszCommentEncoded)
|
---|
6586 | RTStrFree((char *)(void *)pszCommentEncoded);
|
---|
6587 | }
|
---|
6588 | else
|
---|
6589 | rc = VERR_VD_NOT_OPENED;
|
---|
6590 |
|
---|
6591 | out:
|
---|
6592 | LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
|
---|
6593 | return rc;
|
---|
6594 | }
|
---|
6595 |
|
---|
6596 | /** @copydoc VBOXHDDBACKEND::pfnSetComment */
|
---|
6597 | static int vmdkSetComment(void *pBackendData, const char *pszComment)
|
---|
6598 | {
|
---|
6599 | LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
|
---|
6600 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6601 | int rc;
|
---|
6602 |
|
---|
6603 | AssertPtr(pImage);
|
---|
6604 |
|
---|
6605 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6606 | {
|
---|
6607 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
6608 | goto out;
|
---|
6609 | }
|
---|
6610 | if (pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
|
---|
6611 | {
|
---|
6612 | rc = VERR_NOT_SUPPORTED;
|
---|
6613 | goto out;
|
---|
6614 | }
|
---|
6615 |
|
---|
6616 | if (pImage)
|
---|
6617 | rc = vmdkSetImageComment(pImage, pszComment);
|
---|
6618 | else
|
---|
6619 | rc = VERR_VD_NOT_OPENED;
|
---|
6620 |
|
---|
6621 | out:
|
---|
6622 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6623 | return rc;
|
---|
6624 | }
|
---|
6625 |
|
---|
6626 | /** @copydoc VBOXHDDBACKEND::pfnGetUuid */
|
---|
6627 | static int vmdkGetUuid(void *pBackendData, PRTUUID pUuid)
|
---|
6628 | {
|
---|
6629 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
6630 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6631 | int rc;
|
---|
6632 |
|
---|
6633 | AssertPtr(pImage);
|
---|
6634 |
|
---|
6635 | if (pImage)
|
---|
6636 | {
|
---|
6637 | *pUuid = pImage->ImageUuid;
|
---|
6638 | rc = VINF_SUCCESS;
|
---|
6639 | }
|
---|
6640 | else
|
---|
6641 | rc = VERR_VD_NOT_OPENED;
|
---|
6642 |
|
---|
6643 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
6644 | return rc;
|
---|
6645 | }
|
---|
6646 |
|
---|
6647 | /** @copydoc VBOXHDDBACKEND::pfnSetUuid */
|
---|
6648 | static int vmdkSetUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
6649 | {
|
---|
6650 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
6651 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6652 | int rc;
|
---|
6653 |
|
---|
6654 | LogFlowFunc(("%RTuuid\n", pUuid));
|
---|
6655 | AssertPtr(pImage);
|
---|
6656 |
|
---|
6657 | if (pImage)
|
---|
6658 | {
|
---|
6659 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6660 | {
|
---|
6661 | if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6662 | {
|
---|
6663 | pImage->ImageUuid = *pUuid;
|
---|
6664 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
6665 | VMDK_DDB_IMAGE_UUID, pUuid);
|
---|
6666 | if (RT_FAILURE(rc))
|
---|
6667 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
6668 | rc = VINF_SUCCESS;
|
---|
6669 | }
|
---|
6670 | else
|
---|
6671 | rc = VERR_NOT_SUPPORTED;
|
---|
6672 | }
|
---|
6673 | else
|
---|
6674 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
6675 | }
|
---|
6676 | else
|
---|
6677 | rc = VERR_VD_NOT_OPENED;
|
---|
6678 |
|
---|
6679 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6680 | return rc;
|
---|
6681 | }
|
---|
6682 |
|
---|
6683 | /** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
|
---|
6684 | static int vmdkGetModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
6685 | {
|
---|
6686 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
6687 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6688 | int rc;
|
---|
6689 |
|
---|
6690 | AssertPtr(pImage);
|
---|
6691 |
|
---|
6692 | if (pImage)
|
---|
6693 | {
|
---|
6694 | *pUuid = pImage->ModificationUuid;
|
---|
6695 | rc = VINF_SUCCESS;
|
---|
6696 | }
|
---|
6697 | else
|
---|
6698 | rc = VERR_VD_NOT_OPENED;
|
---|
6699 |
|
---|
6700 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
6701 | return rc;
|
---|
6702 | }
|
---|
6703 |
|
---|
6704 | /** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
|
---|
6705 | static int vmdkSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
6706 | {
|
---|
6707 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
6708 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6709 | int rc;
|
---|
6710 |
|
---|
6711 | AssertPtr(pImage);
|
---|
6712 |
|
---|
6713 | if (pImage)
|
---|
6714 | {
|
---|
6715 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6716 | {
|
---|
6717 | if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6718 | {
|
---|
6719 | /* Only touch the modification uuid if it changed. */
|
---|
6720 | if (RTUuidCompare(&pImage->ModificationUuid, pUuid))
|
---|
6721 | {
|
---|
6722 | pImage->ModificationUuid = *pUuid;
|
---|
6723 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
6724 | VMDK_DDB_MODIFICATION_UUID, pUuid);
|
---|
6725 | if (RT_FAILURE(rc))
|
---|
6726 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing modification UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
6727 | }
|
---|
6728 | rc = VINF_SUCCESS;
|
---|
6729 | }
|
---|
6730 | else
|
---|
6731 | rc = VERR_NOT_SUPPORTED;
|
---|
6732 | }
|
---|
6733 | else
|
---|
6734 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
6735 | }
|
---|
6736 | else
|
---|
6737 | rc = VERR_VD_NOT_OPENED;
|
---|
6738 |
|
---|
6739 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6740 | return rc;
|
---|
6741 | }
|
---|
6742 |
|
---|
6743 | /** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
|
---|
6744 | static int vmdkGetParentUuid(void *pBackendData, PRTUUID pUuid)
|
---|
6745 | {
|
---|
6746 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
6747 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6748 | int rc;
|
---|
6749 |
|
---|
6750 | AssertPtr(pImage);
|
---|
6751 |
|
---|
6752 | if (pImage)
|
---|
6753 | {
|
---|
6754 | *pUuid = pImage->ParentUuid;
|
---|
6755 | rc = VINF_SUCCESS;
|
---|
6756 | }
|
---|
6757 | else
|
---|
6758 | rc = VERR_VD_NOT_OPENED;
|
---|
6759 |
|
---|
6760 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
6761 | return rc;
|
---|
6762 | }
|
---|
6763 |
|
---|
6764 | /** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
|
---|
6765 | static int vmdkSetParentUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
6766 | {
|
---|
6767 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
6768 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6769 | int rc;
|
---|
6770 |
|
---|
6771 | AssertPtr(pImage);
|
---|
6772 |
|
---|
6773 | if (pImage)
|
---|
6774 | {
|
---|
6775 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6776 | {
|
---|
6777 | if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6778 | {
|
---|
6779 | pImage->ParentUuid = *pUuid;
|
---|
6780 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
6781 | VMDK_DDB_PARENT_UUID, pUuid);
|
---|
6782 | if (RT_FAILURE(rc))
|
---|
6783 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent image UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
6784 | rc = VINF_SUCCESS;
|
---|
6785 | }
|
---|
6786 | else
|
---|
6787 | rc = VERR_NOT_SUPPORTED;
|
---|
6788 | }
|
---|
6789 | else
|
---|
6790 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
6791 | }
|
---|
6792 | else
|
---|
6793 | rc = VERR_VD_NOT_OPENED;
|
---|
6794 |
|
---|
6795 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6796 | return rc;
|
---|
6797 | }
|
---|
6798 |
|
---|
6799 | /** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
|
---|
6800 | static int vmdkGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
6801 | {
|
---|
6802 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
6803 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6804 | int rc;
|
---|
6805 |
|
---|
6806 | AssertPtr(pImage);
|
---|
6807 |
|
---|
6808 | if (pImage)
|
---|
6809 | {
|
---|
6810 | *pUuid = pImage->ParentModificationUuid;
|
---|
6811 | rc = VINF_SUCCESS;
|
---|
6812 | }
|
---|
6813 | else
|
---|
6814 | rc = VERR_VD_NOT_OPENED;
|
---|
6815 |
|
---|
6816 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
6817 | return rc;
|
---|
6818 | }
|
---|
6819 |
|
---|
6820 | /** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
|
---|
6821 | static int vmdkSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
6822 | {
|
---|
6823 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
6824 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6825 | int rc;
|
---|
6826 |
|
---|
6827 | AssertPtr(pImage);
|
---|
6828 |
|
---|
6829 | if (pImage)
|
---|
6830 | {
|
---|
6831 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6832 | {
|
---|
6833 | if (!(pImage->uOpenFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6834 | {
|
---|
6835 | pImage->ParentModificationUuid = *pUuid;
|
---|
6836 | rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
|
---|
6837 | VMDK_DDB_PARENT_MODIFICATION_UUID, pUuid);
|
---|
6838 | if (RT_FAILURE(rc))
|
---|
6839 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent image UUID in descriptor in '%s'"), pImage->pszFilename);
|
---|
6840 | rc = VINF_SUCCESS;
|
---|
6841 | }
|
---|
6842 | else
|
---|
6843 | rc = VERR_NOT_SUPPORTED;
|
---|
6844 | }
|
---|
6845 | else
|
---|
6846 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
6847 | }
|
---|
6848 | else
|
---|
6849 | rc = VERR_VD_NOT_OPENED;
|
---|
6850 |
|
---|
6851 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6852 | return rc;
|
---|
6853 | }
|
---|
6854 |
|
---|
6855 | /** @copydoc VBOXHDDBACKEND::pfnDump */
|
---|
6856 | static void vmdkDump(void *pBackendData)
|
---|
6857 | {
|
---|
6858 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6859 |
|
---|
6860 | AssertPtr(pImage);
|
---|
6861 | if (pImage)
|
---|
6862 | {
|
---|
6863 | vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
|
---|
6864 | pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
|
---|
6865 | pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
|
---|
6866 | VMDK_BYTE2SECTOR(pImage->cbSize));
|
---|
6867 | vdIfErrorMessage(pImage->pIfError, "Header: uuidCreation={%RTuuid}\n", &pImage->ImageUuid);
|
---|
6868 | vdIfErrorMessage(pImage->pIfError, "Header: uuidModification={%RTuuid}\n", &pImage->ModificationUuid);
|
---|
6869 | vdIfErrorMessage(pImage->pIfError, "Header: uuidParent={%RTuuid}\n", &pImage->ParentUuid);
|
---|
6870 | vdIfErrorMessage(pImage->pIfError, "Header: uuidParentModification={%RTuuid}\n", &pImage->ParentModificationUuid);
|
---|
6871 | }
|
---|
6872 | }
|
---|
6873 |
|
---|
6874 | /** @copydoc VBOXHDDBACKEND::pfnAsyncRead */
|
---|
6875 | static int vmdkAsyncRead(void *pBackendData, uint64_t uOffset, size_t cbRead,
|
---|
6876 | PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
|
---|
6877 | {
|
---|
6878 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
|
---|
6879 | pBackendData, uOffset, pIoCtx, cbRead, pcbActuallyRead));
|
---|
6880 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6881 | PVMDKEXTENT pExtent;
|
---|
6882 | uint64_t uSectorExtentRel;
|
---|
6883 | uint64_t uSectorExtentAbs;
|
---|
6884 | int rc;
|
---|
6885 |
|
---|
6886 | AssertPtr(pImage);
|
---|
6887 | Assert(uOffset % 512 == 0);
|
---|
6888 | Assert(cbRead % 512 == 0);
|
---|
6889 |
|
---|
6890 | if ( uOffset + cbRead > pImage->cbSize
|
---|
6891 | || cbRead == 0)
|
---|
6892 | {
|
---|
6893 | rc = VERR_INVALID_PARAMETER;
|
---|
6894 | goto out;
|
---|
6895 | }
|
---|
6896 |
|
---|
6897 | rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
|
---|
6898 | &pExtent, &uSectorExtentRel);
|
---|
6899 | if (RT_FAILURE(rc))
|
---|
6900 | goto out;
|
---|
6901 |
|
---|
6902 | /* Check access permissions as defined in the extent descriptor. */
|
---|
6903 | if (pExtent->enmAccess == VMDKACCESS_NOACCESS)
|
---|
6904 | {
|
---|
6905 | rc = VERR_VD_VMDK_INVALID_STATE;
|
---|
6906 | goto out;
|
---|
6907 | }
|
---|
6908 |
|
---|
6909 | /* Clip read range to remain in this extent. */
|
---|
6910 | cbRead = RT_MIN(cbRead, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
6911 |
|
---|
6912 | /* Handle the read according to the current extent type. */
|
---|
6913 | switch (pExtent->enmType)
|
---|
6914 | {
|
---|
6915 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
6916 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
6917 | case VMDKETYPE_ESX_SPARSE:
|
---|
6918 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
6919 | rc = vmdkGetSectorAsync(pImage, pIoCtx, pExtent,
|
---|
6920 | uSectorExtentRel, &uSectorExtentAbs);
|
---|
6921 | if (RT_FAILURE(rc))
|
---|
6922 | goto out;
|
---|
6923 | /* Clip read range to at most the rest of the grain. */
|
---|
6924 | cbRead = RT_MIN(cbRead, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSectorExtentRel % pExtent->cSectorsPerGrain));
|
---|
6925 | Assert(!(cbRead % 512));
|
---|
6926 | if (uSectorExtentAbs == 0)
|
---|
6927 | rc = VERR_VD_BLOCK_FREE;
|
---|
6928 | else
|
---|
6929 | {
|
---|
6930 | AssertMsg(!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED), ("Async I/O is not supported for stream optimized VMDK's\n"));
|
---|
6931 | rc = vdIfIoIntFileReadUserAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
6932 | VMDK_SECTOR2BYTE(uSectorExtentAbs),
|
---|
6933 | pIoCtx, cbRead);
|
---|
6934 | }
|
---|
6935 | break;
|
---|
6936 | case VMDKETYPE_VMFS:
|
---|
6937 | case VMDKETYPE_FLAT:
|
---|
6938 | rc = vdIfIoIntFileReadUserAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
6939 | VMDK_SECTOR2BYTE(uSectorExtentRel),
|
---|
6940 | pIoCtx, cbRead);
|
---|
6941 | break;
|
---|
6942 | case VMDKETYPE_ZERO:
|
---|
6943 | size_t cbSet;
|
---|
6944 |
|
---|
6945 | cbSet = vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbRead);
|
---|
6946 | Assert(cbSet == cbRead);
|
---|
6947 |
|
---|
6948 | rc = VINF_SUCCESS;
|
---|
6949 | break;
|
---|
6950 | }
|
---|
6951 | if (pcbActuallyRead)
|
---|
6952 | *pcbActuallyRead = cbRead;
|
---|
6953 |
|
---|
6954 | out:
|
---|
6955 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6956 | return rc;
|
---|
6957 | }
|
---|
6958 |
|
---|
6959 | /** @copydoc VBOXHDDBACKEND::pfnAsyncWrite */
|
---|
6960 | static int vmdkAsyncWrite(void *pBackendData, uint64_t uOffset, size_t cbWrite,
|
---|
6961 | PVDIOCTX pIoCtx,
|
---|
6962 | size_t *pcbWriteProcess, size_t *pcbPreRead,
|
---|
6963 | size_t *pcbPostRead, unsigned fWrite)
|
---|
6964 | {
|
---|
6965 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
|
---|
6966 | pBackendData, uOffset, pIoCtx, cbWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
|
---|
6967 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
6968 | PVMDKEXTENT pExtent;
|
---|
6969 | uint64_t uSectorExtentRel;
|
---|
6970 | uint64_t uSectorExtentAbs;
|
---|
6971 | int rc;
|
---|
6972 |
|
---|
6973 | AssertPtr(pImage);
|
---|
6974 | Assert(uOffset % 512 == 0);
|
---|
6975 | Assert(cbWrite % 512 == 0);
|
---|
6976 |
|
---|
6977 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6978 | {
|
---|
6979 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
6980 | goto out;
|
---|
6981 | }
|
---|
6982 |
|
---|
6983 | if (cbWrite == 0)
|
---|
6984 | {
|
---|
6985 | rc = VERR_INVALID_PARAMETER;
|
---|
6986 | goto out;
|
---|
6987 | }
|
---|
6988 |
|
---|
6989 | /* No size check here, will do that later when the extent is located.
|
---|
6990 | * There are sparse images out there which according to the spec are
|
---|
6991 | * invalid, because the total size is not a multiple of the grain size.
|
---|
6992 | * Also for sparse images which are stitched together in odd ways (not at
|
---|
6993 | * grain boundaries, and with the nominal size not being a multiple of the
|
---|
6994 | * grain size), this would prevent writing to the last grain. */
|
---|
6995 |
|
---|
6996 | rc = vmdkFindExtent(pImage, VMDK_BYTE2SECTOR(uOffset),
|
---|
6997 | &pExtent, &uSectorExtentRel);
|
---|
6998 | if (RT_FAILURE(rc))
|
---|
6999 | goto out;
|
---|
7000 |
|
---|
7001 | /* Check access permissions as defined in the extent descriptor. */
|
---|
7002 | if (pExtent->enmAccess != VMDKACCESS_READWRITE)
|
---|
7003 | {
|
---|
7004 | rc = VERR_VD_VMDK_INVALID_STATE;
|
---|
7005 | goto out;
|
---|
7006 | }
|
---|
7007 |
|
---|
7008 | /* Handle the write according to the current extent type. */
|
---|
7009 | switch (pExtent->enmType)
|
---|
7010 | {
|
---|
7011 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
7012 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
7013 | case VMDKETYPE_ESX_SPARSE:
|
---|
7014 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
7015 | rc = vmdkGetSectorAsync(pImage, pIoCtx, pExtent, uSectorExtentRel,
|
---|
7016 | &uSectorExtentAbs);
|
---|
7017 | if (RT_FAILURE(rc))
|
---|
7018 | goto out;
|
---|
7019 | /* Clip write range to at most the rest of the grain. */
|
---|
7020 | cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain - uSectorExtentRel % pExtent->cSectorsPerGrain));
|
---|
7021 | if ( pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
|
---|
7022 | && uSectorExtentRel < (uint64_t)pExtent->uLastGrainAccess * pExtent->cSectorsPerGrain)
|
---|
7023 | {
|
---|
7024 | rc = VERR_VD_VMDK_INVALID_WRITE;
|
---|
7025 | goto out;
|
---|
7026 | }
|
---|
7027 | if (uSectorExtentAbs == 0)
|
---|
7028 | {
|
---|
7029 | if (cbWrite == VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain))
|
---|
7030 | {
|
---|
7031 | /* Full block write to a previously unallocated block.
|
---|
7032 | * Check if the caller wants to avoid the automatic alloc. */
|
---|
7033 | if (!(fWrite & VD_WRITE_NO_ALLOC))
|
---|
7034 | {
|
---|
7035 | /* Allocate GT and find out where to store the grain. */
|
---|
7036 | rc = vmdkAllocGrainAsync(pImage, pExtent, pIoCtx,
|
---|
7037 | uSectorExtentRel, cbWrite);
|
---|
7038 | }
|
---|
7039 | else
|
---|
7040 | rc = VERR_VD_BLOCK_FREE;
|
---|
7041 | *pcbPreRead = 0;
|
---|
7042 | *pcbPostRead = 0;
|
---|
7043 | }
|
---|
7044 | else
|
---|
7045 | {
|
---|
7046 | /* Clip write range to remain in this extent. */
|
---|
7047 | cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
7048 | *pcbPreRead = VMDK_SECTOR2BYTE(uSectorExtentRel % pExtent->cSectorsPerGrain);
|
---|
7049 | *pcbPostRead = VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain) - cbWrite - *pcbPreRead;
|
---|
7050 | rc = VERR_VD_BLOCK_FREE;
|
---|
7051 | }
|
---|
7052 | }
|
---|
7053 | else
|
---|
7054 | {
|
---|
7055 | Assert(!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED));
|
---|
7056 | rc = vdIfIoIntFileWriteUserAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
7057 | VMDK_SECTOR2BYTE(uSectorExtentAbs),
|
---|
7058 | pIoCtx, cbWrite, NULL, NULL);
|
---|
7059 | }
|
---|
7060 | break;
|
---|
7061 | case VMDKETYPE_VMFS:
|
---|
7062 | case VMDKETYPE_FLAT:
|
---|
7063 | /* Clip write range to remain in this extent. */
|
---|
7064 | cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
7065 | rc = vdIfIoIntFileWriteUserAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
7066 | VMDK_SECTOR2BYTE(uSectorExtentRel),
|
---|
7067 | pIoCtx, cbWrite, NULL, NULL);
|
---|
7068 | break;
|
---|
7069 | case VMDKETYPE_ZERO:
|
---|
7070 | /* Clip write range to remain in this extent. */
|
---|
7071 | cbWrite = RT_MIN(cbWrite, VMDK_SECTOR2BYTE(pExtent->uSectorOffset + pExtent->cNominalSectors - uSectorExtentRel));
|
---|
7072 | break;
|
---|
7073 | }
|
---|
7074 |
|
---|
7075 | if (pcbWriteProcess)
|
---|
7076 | *pcbWriteProcess = cbWrite;
|
---|
7077 |
|
---|
7078 | out:
|
---|
7079 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7080 | return rc;
|
---|
7081 | }
|
---|
7082 |
|
---|
7083 | /** @copydoc VBOXHDDBACKEND::pfnAsyncFlush */
|
---|
7084 | static int vmdkAsyncFlush(void *pBackendData, PVDIOCTX pIoCtx)
|
---|
7085 | {
|
---|
7086 | PVMDKIMAGE pImage = (PVMDKIMAGE)pBackendData;
|
---|
7087 | PVMDKEXTENT pExtent;
|
---|
7088 | int rc = VINF_SUCCESS;
|
---|
7089 |
|
---|
7090 | /* Update descriptor if changed. */
|
---|
7091 | /** @todo: The descriptor is never updated because
|
---|
7092 | * it remains unchanged during normal operation (only vmdkRename updates it).
|
---|
7093 | * So this part is actually not tested so far and requires testing as soon
|
---|
7094 | * as the descriptor might change during async I/O.
|
---|
7095 | */
|
---|
7096 | if (pImage->Descriptor.fDirty)
|
---|
7097 | {
|
---|
7098 | rc = vmdkWriteDescriptorAsync(pImage, pIoCtx);
|
---|
7099 | if ( RT_FAILURE(rc)
|
---|
7100 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
7101 | goto out;
|
---|
7102 | }
|
---|
7103 |
|
---|
7104 | for (unsigned i = 0; i < pImage->cExtents; i++)
|
---|
7105 | {
|
---|
7106 | pExtent = &pImage->pExtents[i];
|
---|
7107 | if (pExtent->pFile != NULL && pExtent->fMetaDirty)
|
---|
7108 | {
|
---|
7109 | switch (pExtent->enmType)
|
---|
7110 | {
|
---|
7111 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
7112 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
7113 | case VMDKETYPE_ESX_SPARSE:
|
---|
7114 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
7115 | rc = vmdkWriteMetaSparseExtentAsync(pImage, pExtent, 0, pIoCtx);
|
---|
7116 | if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
7117 | goto out;
|
---|
7118 | if (pExtent->fFooter)
|
---|
7119 | {
|
---|
7120 | uint64_t uFileOffset = pExtent->uAppendPosition;
|
---|
7121 | if (!uFileOffset)
|
---|
7122 | {
|
---|
7123 | rc = VERR_INTERNAL_ERROR;
|
---|
7124 | goto out;
|
---|
7125 | }
|
---|
7126 | uFileOffset = RT_ALIGN_64(uFileOffset, 512);
|
---|
7127 | rc = vmdkWriteMetaSparseExtent(pImage, pExtent, uFileOffset);
|
---|
7128 | if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
7129 | goto out;
|
---|
7130 | }
|
---|
7131 | break;
|
---|
7132 | case VMDKETYPE_VMFS:
|
---|
7133 | case VMDKETYPE_FLAT:
|
---|
7134 | /* Nothing to do. */
|
---|
7135 | break;
|
---|
7136 | case VMDKETYPE_ZERO:
|
---|
7137 | default:
|
---|
7138 | AssertMsgFailed(("extent with type %d marked as dirty\n",
|
---|
7139 | pExtent->enmType));
|
---|
7140 | break;
|
---|
7141 | }
|
---|
7142 | }
|
---|
7143 | switch (pExtent->enmType)
|
---|
7144 | {
|
---|
7145 | case VMDKETYPE_HOSTED_SPARSE:
|
---|
7146 | #ifdef VBOX_WITH_VMDK_ESX
|
---|
7147 | case VMDKETYPE_ESX_SPARSE:
|
---|
7148 | #endif /* VBOX_WITH_VMDK_ESX */
|
---|
7149 | case VMDKETYPE_VMFS:
|
---|
7150 | case VMDKETYPE_FLAT:
|
---|
7151 | /*
|
---|
7152 | * Don't ignore block devices like in the sync case
|
---|
7153 | * (they have an absolute path).
|
---|
7154 | * We might have unwritten data in the writeback cache and
|
---|
7155 | * the async I/O manager will handle these requests properly
|
---|
7156 | * even if the block device doesn't support these requests.
|
---|
7157 | */
|
---|
7158 | if ( pExtent->pFile != NULL
|
---|
7159 | && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
7160 | rc = vdIfIoIntFileFlushAsync(pImage->pIfIo, pExtent->pFile->pStorage,
|
---|
7161 | pIoCtx, NULL, NULL);
|
---|
7162 | break;
|
---|
7163 | case VMDKETYPE_ZERO:
|
---|
7164 | /* No need to do anything for this extent. */
|
---|
7165 | break;
|
---|
7166 | default:
|
---|
7167 | AssertMsgFailed(("unknown extent type %d\n", pExtent->enmType));
|
---|
7168 | break;
|
---|
7169 | }
|
---|
7170 | }
|
---|
7171 |
|
---|
7172 | out:
|
---|
7173 | return rc;
|
---|
7174 | }
|
---|
7175 |
|
---|
7176 |
|
---|
7177 | VBOXHDDBACKEND g_VmdkBackend =
|
---|
7178 | {
|
---|
7179 | /* pszBackendName */
|
---|
7180 | "VMDK",
|
---|
7181 | /* cbSize */
|
---|
7182 | sizeof(VBOXHDDBACKEND),
|
---|
7183 | /* uBackendCaps */
|
---|
7184 | VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
|
---|
7185 | | VD_CAP_CREATE_SPLIT_2G | VD_CAP_DIFF | VD_CAP_FILE | VD_CAP_ASYNC
|
---|
7186 | | VD_CAP_VFS,
|
---|
7187 | /* paFileExtensions */
|
---|
7188 | s_aVmdkFileExtensions,
|
---|
7189 | /* paConfigInfo */
|
---|
7190 | NULL,
|
---|
7191 | /* hPlugin */
|
---|
7192 | NIL_RTLDRMOD,
|
---|
7193 | /* pfnCheckIfValid */
|
---|
7194 | vmdkCheckIfValid,
|
---|
7195 | /* pfnOpen */
|
---|
7196 | vmdkOpen,
|
---|
7197 | /* pfnCreate */
|
---|
7198 | vmdkCreate,
|
---|
7199 | /* pfnRename */
|
---|
7200 | vmdkRename,
|
---|
7201 | /* pfnClose */
|
---|
7202 | vmdkClose,
|
---|
7203 | /* pfnRead */
|
---|
7204 | vmdkRead,
|
---|
7205 | /* pfnWrite */
|
---|
7206 | vmdkWrite,
|
---|
7207 | /* pfnFlush */
|
---|
7208 | vmdkFlush,
|
---|
7209 | /* pfnGetVersion */
|
---|
7210 | vmdkGetVersion,
|
---|
7211 | /* pfnGetSize */
|
---|
7212 | vmdkGetSize,
|
---|
7213 | /* pfnGetFileSize */
|
---|
7214 | vmdkGetFileSize,
|
---|
7215 | /* pfnGetPCHSGeometry */
|
---|
7216 | vmdkGetPCHSGeometry,
|
---|
7217 | /* pfnSetPCHSGeometry */
|
---|
7218 | vmdkSetPCHSGeometry,
|
---|
7219 | /* pfnGetLCHSGeometry */
|
---|
7220 | vmdkGetLCHSGeometry,
|
---|
7221 | /* pfnSetLCHSGeometry */
|
---|
7222 | vmdkSetLCHSGeometry,
|
---|
7223 | /* pfnGetImageFlags */
|
---|
7224 | vmdkGetImageFlags,
|
---|
7225 | /* pfnGetOpenFlags */
|
---|
7226 | vmdkGetOpenFlags,
|
---|
7227 | /* pfnSetOpenFlags */
|
---|
7228 | vmdkSetOpenFlags,
|
---|
7229 | /* pfnGetComment */
|
---|
7230 | vmdkGetComment,
|
---|
7231 | /* pfnSetComment */
|
---|
7232 | vmdkSetComment,
|
---|
7233 | /* pfnGetUuid */
|
---|
7234 | vmdkGetUuid,
|
---|
7235 | /* pfnSetUuid */
|
---|
7236 | vmdkSetUuid,
|
---|
7237 | /* pfnGetModificationUuid */
|
---|
7238 | vmdkGetModificationUuid,
|
---|
7239 | /* pfnSetModificationUuid */
|
---|
7240 | vmdkSetModificationUuid,
|
---|
7241 | /* pfnGetParentUuid */
|
---|
7242 | vmdkGetParentUuid,
|
---|
7243 | /* pfnSetParentUuid */
|
---|
7244 | vmdkSetParentUuid,
|
---|
7245 | /* pfnGetParentModificationUuid */
|
---|
7246 | vmdkGetParentModificationUuid,
|
---|
7247 | /* pfnSetParentModificationUuid */
|
---|
7248 | vmdkSetParentModificationUuid,
|
---|
7249 | /* pfnDump */
|
---|
7250 | vmdkDump,
|
---|
7251 | /* pfnGetTimeStamp */
|
---|
7252 | NULL,
|
---|
7253 | /* pfnGetParentTimeStamp */
|
---|
7254 | NULL,
|
---|
7255 | /* pfnSetParentTimeStamp */
|
---|
7256 | NULL,
|
---|
7257 | /* pfnGetParentFilename */
|
---|
7258 | NULL,
|
---|
7259 | /* pfnSetParentFilename */
|
---|
7260 | NULL,
|
---|
7261 | /* pfnAsyncRead */
|
---|
7262 | vmdkAsyncRead,
|
---|
7263 | /* pfnAsyncWrite */
|
---|
7264 | vmdkAsyncWrite,
|
---|
7265 | /* pfnAsyncFlush */
|
---|
7266 | vmdkAsyncFlush,
|
---|
7267 | /* pfnComposeLocation */
|
---|
7268 | genericFileComposeLocation,
|
---|
7269 | /* pfnComposeName */
|
---|
7270 | genericFileComposeName,
|
---|
7271 | /* pfnCompact */
|
---|
7272 | NULL,
|
---|
7273 | /* pfnResize */
|
---|
7274 | NULL,
|
---|
7275 | /* pfnDiscard */
|
---|
7276 | NULL,
|
---|
7277 | /* pfnAsyncDiscard */
|
---|
7278 | NULL,
|
---|
7279 | /* pfnRepair */
|
---|
7280 | NULL
|
---|
7281 | };
|
---|