1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox storage devices:
|
---|
4 | * VBox HDD container implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_DRV_VBOXHDD
|
---|
27 | #include <VBox/VBoxHDD.h>
|
---|
28 | #include <VBox/pdm.h>
|
---|
29 | #include <VBox/mm.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 |
|
---|
32 | #include <VBox/log.h>
|
---|
33 | #include <iprt/alloc.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/uuid.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 | #include "Builtins.h"
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Constants And Macros, Structures and Typedefs *
|
---|
43 | *******************************************************************************/
|
---|
44 | /** The Sector size.
|
---|
45 | * Currently we support only 512 bytes sectors.
|
---|
46 | */
|
---|
47 | #define VDI_GEOMETRY_SECTOR_SIZE (512)
|
---|
48 | /** 512 = 2^^9 */
|
---|
49 | #define VDI_GEOMETRY_SECTOR_SHIFT (9)
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Harddisk geometry.
|
---|
53 | */
|
---|
54 | #pragma pack(1)
|
---|
55 | typedef struct VDIDISKGEOMETRY
|
---|
56 | {
|
---|
57 | /** Cylinders. */
|
---|
58 | uint32_t cCylinders;
|
---|
59 | /** Heads. */
|
---|
60 | uint32_t cHeads;
|
---|
61 | /** Sectors per track. */
|
---|
62 | uint32_t cSectors;
|
---|
63 | /** Sector size. (bytes per sector) */
|
---|
64 | uint32_t cbSector;
|
---|
65 | } VDIDISKGEOMETRY, *PVDIDISKGEOMETRY;
|
---|
66 | #pragma pack()
|
---|
67 |
|
---|
68 | /** Image signature. */
|
---|
69 | #define VDI_IMAGE_SIGNATURE (0xbeda107f)
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Pre-Header to be stored in image file - used for version control.
|
---|
73 | */
|
---|
74 | #pragma pack(1)
|
---|
75 | typedef struct VDIPREHEADER
|
---|
76 | {
|
---|
77 | /** Just text info about image type, for eyes only. */
|
---|
78 | char szFileInfo[64];
|
---|
79 | /** The image signature (VDI_IMAGE_SIGNATURE). */
|
---|
80 | uint32_t u32Signature;
|
---|
81 | /** The image version (VDI_IMAGE_VERSION). */
|
---|
82 | uint32_t u32Version;
|
---|
83 | } VDIPREHEADER, *PVDIPREHEADER;
|
---|
84 | #pragma pack()
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Size of szComment field of HDD image header.
|
---|
88 | */
|
---|
89 | #define VDI_IMAGE_COMMENT_SIZE 256
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 0.
|
---|
93 | * Prepended by VDIPREHEADER.
|
---|
94 | */
|
---|
95 | #pragma pack(1)
|
---|
96 | typedef struct VDIHEADER0
|
---|
97 | {
|
---|
98 | /** The image type (VDI_IMAGE_TYPE_*). */
|
---|
99 | uint32_t u32Type;
|
---|
100 | /** Image flags (VDI_IMAGE_FLAGS_*). */
|
---|
101 | uint32_t fFlags;
|
---|
102 | /** Image comment. (UTF-8) */
|
---|
103 | char szComment[VDI_IMAGE_COMMENT_SIZE];
|
---|
104 | /** Image geometry. */
|
---|
105 | VDIDISKGEOMETRY Geometry;
|
---|
106 | /** Size of disk (in bytes). */
|
---|
107 | uint64_t cbDisk;
|
---|
108 | /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) */
|
---|
109 | uint32_t cbBlock;
|
---|
110 | /** Number of blocks. */
|
---|
111 | uint32_t cBlocks;
|
---|
112 | /** Number of allocated blocks. */
|
---|
113 | uint32_t cBlocksAllocated;
|
---|
114 | /** UUID of image. */
|
---|
115 | RTUUID uuidCreate;
|
---|
116 | /** UUID of image's last modification. */
|
---|
117 | RTUUID uuidModify;
|
---|
118 | /** Only for secondary images - UUID of primary image. */
|
---|
119 | RTUUID uuidLinkage;
|
---|
120 | } VDIHEADER0, *PVDIHEADER0;
|
---|
121 | #pragma pack()
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 1.
|
---|
125 | * Prepended by VDIPREHEADER.
|
---|
126 | */
|
---|
127 | #pragma pack(1)
|
---|
128 | typedef struct VDIHEADER1
|
---|
129 | {
|
---|
130 | /** Size of this structure in bytes. */
|
---|
131 | uint32_t cbHeader;
|
---|
132 | /** The image type (VDI_IMAGE_TYPE_*). */
|
---|
133 | uint32_t u32Type;
|
---|
134 | /** Image flags (VDI_IMAGE_FLAGS_*). */
|
---|
135 | uint32_t fFlags;
|
---|
136 | /** Image comment. (UTF-8) */
|
---|
137 | char szComment[VDI_IMAGE_COMMENT_SIZE];
|
---|
138 | /** Offset of Blocks array from the begining of image file.
|
---|
139 | * Should be sector-aligned for HDD access optimization. */
|
---|
140 | uint32_t offBlocks;
|
---|
141 | /** Offset of image data from the begining of image file.
|
---|
142 | * Should be sector-aligned for HDD access optimization. */
|
---|
143 | uint32_t offData;
|
---|
144 | /** Image geometry. */
|
---|
145 | VDIDISKGEOMETRY Geometry;
|
---|
146 | /** BIOS HDD translation mode, see PDMBIOSTRANSLATION. */
|
---|
147 | uint32_t u32Translation;
|
---|
148 | /** Size of disk (in bytes). */
|
---|
149 | uint64_t cbDisk;
|
---|
150 | /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) Should be a power of 2! */
|
---|
151 | uint32_t cbBlock;
|
---|
152 | /** Size of additional service information of every data block.
|
---|
153 | * Prepended before block data. May be 0.
|
---|
154 | * Should be a power of 2 and sector-aligned for optimization reasons. */
|
---|
155 | uint32_t cbBlockExtra;
|
---|
156 | /** Number of blocks. */
|
---|
157 | uint32_t cBlocks;
|
---|
158 | /** Number of allocated blocks. */
|
---|
159 | uint32_t cBlocksAllocated;
|
---|
160 | /** UUID of image. */
|
---|
161 | RTUUID uuidCreate;
|
---|
162 | /** UUID of image's last modification. */
|
---|
163 | RTUUID uuidModify;
|
---|
164 | /** Only for secondary images - UUID of previous image. */
|
---|
165 | RTUUID uuidLinkage;
|
---|
166 | /** Only for secondary images - UUID of previous image's last modification. */
|
---|
167 | RTUUID uuidParentModify;
|
---|
168 | } VDIHEADER1, *PVDIHEADER1;
|
---|
169 | #pragma pack()
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Header structure for all versions.
|
---|
173 | */
|
---|
174 | typedef struct VDIHEADER
|
---|
175 | {
|
---|
176 | unsigned uVersion;
|
---|
177 | union
|
---|
178 | {
|
---|
179 | VDIHEADER0 v0;
|
---|
180 | VDIHEADER1 v1;
|
---|
181 | } u;
|
---|
182 | } VDIHEADER, *PVDIHEADER;
|
---|
183 |
|
---|
184 | /** Block 'pointer'. */
|
---|
185 | typedef uint32_t VDIIMAGEBLOCKPOINTER;
|
---|
186 | /** Pointer to a block 'pointer'. */
|
---|
187 | typedef VDIIMAGEBLOCKPOINTER *PVDIIMAGEBLOCKPOINTER;
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Block marked as free is not allocated in image file, read from this
|
---|
191 | * block may returns any random data.
|
---|
192 | */
|
---|
193 | #define VDI_IMAGE_BLOCK_FREE ((VDIIMAGEBLOCKPOINTER)~0)
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Block marked as zero is not allocated in image file, read from this
|
---|
197 | * block returns zeroes.
|
---|
198 | */
|
---|
199 | #define VDI_IMAGE_BLOCK_ZERO ((VDIIMAGEBLOCKPOINTER)~1)
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Block 'pointer' >= VDI_IMAGE_BLOCK_UNALLOCATED indicates block is not
|
---|
203 | * allocated in image file.
|
---|
204 | */
|
---|
205 | #define VDI_IMAGE_BLOCK_UNALLOCATED (VDI_IMAGE_BLOCK_ZERO)
|
---|
206 | #define IS_VDI_IMAGE_BLOCK_ALLOCATED(bp) (bp < VDI_IMAGE_BLOCK_UNALLOCATED)
|
---|
207 |
|
---|
208 | #define GET_MAJOR_HEADER_VERSION(ph) (VDI_GET_VERSION_MAJOR((ph)->uVersion))
|
---|
209 | #define GET_MINOR_HEADER_VERSION(ph) (VDI_GET_VERSION_MINOR((ph)->uVersion))
|
---|
210 |
|
---|
211 | /*******************************************************************************
|
---|
212 | * Internal Functions for header access *
|
---|
213 | *******************************************************************************/
|
---|
214 | static inline VDIIMAGETYPE getImageType(PVDIHEADER ph)
|
---|
215 | {
|
---|
216 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
217 | {
|
---|
218 | case 0: return (VDIIMAGETYPE)ph->u.v0.u32Type;
|
---|
219 | case 1: return (VDIIMAGETYPE)ph->u.v1.u32Type;
|
---|
220 | }
|
---|
221 | AssertFailed();
|
---|
222 | return (VDIIMAGETYPE)0;
|
---|
223 | }
|
---|
224 |
|
---|
225 | static inline unsigned getImageFlags(PVDIHEADER ph)
|
---|
226 | {
|
---|
227 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
228 | {
|
---|
229 | case 0: return ph->u.v0.fFlags;
|
---|
230 | case 1: return ph->u.v1.fFlags;
|
---|
231 | }
|
---|
232 | AssertFailed();
|
---|
233 | return 0;
|
---|
234 | }
|
---|
235 |
|
---|
236 | static inline char *getImageComment(PVDIHEADER ph)
|
---|
237 | {
|
---|
238 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
239 | {
|
---|
240 | case 0: return &ph->u.v0.szComment[0];
|
---|
241 | case 1: return &ph->u.v1.szComment[0];
|
---|
242 | }
|
---|
243 | AssertFailed();
|
---|
244 | return NULL;
|
---|
245 | }
|
---|
246 |
|
---|
247 | static inline unsigned getImageBlocksOffset(PVDIHEADER ph)
|
---|
248 | {
|
---|
249 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
250 | {
|
---|
251 | case 0: return (sizeof(VDIPREHEADER) + sizeof(VDIHEADER0));
|
---|
252 | case 1: return ph->u.v1.offBlocks;
|
---|
253 | }
|
---|
254 | AssertFailed();
|
---|
255 | return 0;
|
---|
256 | }
|
---|
257 |
|
---|
258 | static inline unsigned getImageDataOffset(PVDIHEADER ph)
|
---|
259 | {
|
---|
260 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
261 | {
|
---|
262 | case 0: return sizeof(VDIPREHEADER) + sizeof(VDIHEADER0) + \
|
---|
263 | (ph->u.v0.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
264 | case 1: return ph->u.v1.offData;
|
---|
265 | }
|
---|
266 | AssertFailed();
|
---|
267 | return 0;
|
---|
268 | }
|
---|
269 |
|
---|
270 | static inline PVDIDISKGEOMETRY getImageGeometry(PVDIHEADER ph)
|
---|
271 | {
|
---|
272 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
273 | {
|
---|
274 | case 0: return &ph->u.v0.Geometry;
|
---|
275 | case 1: return &ph->u.v1.Geometry;
|
---|
276 | }
|
---|
277 | AssertFailed();
|
---|
278 | return NULL;
|
---|
279 | }
|
---|
280 |
|
---|
281 | static inline PDMBIOSTRANSLATION getImageTranslation(PVDIHEADER ph)
|
---|
282 | {
|
---|
283 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
284 | {
|
---|
285 | case 0: return PDMBIOSTRANSLATION_AUTO;
|
---|
286 | case 1: return (PDMBIOSTRANSLATION)ph->u.v1.u32Translation;
|
---|
287 | }
|
---|
288 | AssertFailed();
|
---|
289 | return PDMBIOSTRANSLATION_NONE;
|
---|
290 | }
|
---|
291 |
|
---|
292 | static inline void setImageTranslation(PVDIHEADER ph, PDMBIOSTRANSLATION enmTranslation)
|
---|
293 | {
|
---|
294 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
295 | {
|
---|
296 | case 0: return;
|
---|
297 | case 1: ph->u.v1.u32Translation = (uint32_t)enmTranslation; return;
|
---|
298 | }
|
---|
299 | AssertFailed();
|
---|
300 | }
|
---|
301 |
|
---|
302 | static inline uint64_t getImageDiskSize(PVDIHEADER ph)
|
---|
303 | {
|
---|
304 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
305 | {
|
---|
306 | case 0: return ph->u.v0.cbDisk;
|
---|
307 | case 1: return ph->u.v1.cbDisk;
|
---|
308 | }
|
---|
309 | AssertFailed();
|
---|
310 | return 0;
|
---|
311 | }
|
---|
312 |
|
---|
313 | static inline unsigned getImageBlockSize(PVDIHEADER ph)
|
---|
314 | {
|
---|
315 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
316 | {
|
---|
317 | case 0: return ph->u.v0.cbBlock;
|
---|
318 | case 1: return ph->u.v1.cbBlock;
|
---|
319 | }
|
---|
320 | AssertFailed();
|
---|
321 | return 0;
|
---|
322 | }
|
---|
323 |
|
---|
324 | static inline unsigned getImageExtraBlockSize(PVDIHEADER ph)
|
---|
325 | {
|
---|
326 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
327 | {
|
---|
328 | case 0: return 0;
|
---|
329 | case 1: return ph->u.v1.cbBlockExtra;
|
---|
330 | }
|
---|
331 | AssertFailed();
|
---|
332 | return 0;
|
---|
333 | }
|
---|
334 |
|
---|
335 | static inline unsigned getImageBlocks(PVDIHEADER ph)
|
---|
336 | {
|
---|
337 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
338 | {
|
---|
339 | case 0: return ph->u.v0.cBlocks;
|
---|
340 | case 1: return ph->u.v1.cBlocks;
|
---|
341 | }
|
---|
342 | AssertFailed();
|
---|
343 | return 0;
|
---|
344 | }
|
---|
345 |
|
---|
346 | static inline unsigned getImageBlocksAllocated(PVDIHEADER ph)
|
---|
347 | {
|
---|
348 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
349 | {
|
---|
350 | case 0: return ph->u.v0.cBlocksAllocated;
|
---|
351 | case 1: return ph->u.v1.cBlocksAllocated;
|
---|
352 | }
|
---|
353 | AssertFailed();
|
---|
354 | return 0;
|
---|
355 | }
|
---|
356 |
|
---|
357 | static inline void setImageBlocksAllocated(PVDIHEADER ph, unsigned cBlocks)
|
---|
358 | {
|
---|
359 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
360 | {
|
---|
361 | case 0: ph->u.v0.cBlocksAllocated = cBlocks; return;
|
---|
362 | case 1: ph->u.v1.cBlocksAllocated = cBlocks; return;
|
---|
363 | }
|
---|
364 | AssertFailed();
|
---|
365 | }
|
---|
366 |
|
---|
367 | static inline PRTUUID getImageCreationUUID(PVDIHEADER ph)
|
---|
368 | {
|
---|
369 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
370 | {
|
---|
371 | case 0: return &ph->u.v0.uuidCreate;
|
---|
372 | case 1: return &ph->u.v1.uuidCreate;
|
---|
373 | }
|
---|
374 | AssertFailed();
|
---|
375 | return NULL;
|
---|
376 | }
|
---|
377 |
|
---|
378 | static inline PRTUUID getImageModificationUUID(PVDIHEADER ph)
|
---|
379 | {
|
---|
380 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
381 | {
|
---|
382 | case 0: return &ph->u.v0.uuidModify;
|
---|
383 | case 1: return &ph->u.v1.uuidModify;
|
---|
384 | }
|
---|
385 | AssertFailed();
|
---|
386 | return NULL;
|
---|
387 | }
|
---|
388 |
|
---|
389 | static inline PRTUUID getImageParentUUID(PVDIHEADER ph)
|
---|
390 | {
|
---|
391 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
392 | {
|
---|
393 | case 0: return &ph->u.v0.uuidLinkage;
|
---|
394 | case 1: return &ph->u.v1.uuidLinkage;
|
---|
395 | }
|
---|
396 | AssertFailed();
|
---|
397 | return NULL;
|
---|
398 | }
|
---|
399 |
|
---|
400 | static inline PRTUUID getImageParentModificationUUID(PVDIHEADER ph)
|
---|
401 | {
|
---|
402 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
403 | {
|
---|
404 | case 1: return &ph->u.v1.uuidParentModify;
|
---|
405 | }
|
---|
406 | AssertFailed();
|
---|
407 | return NULL;
|
---|
408 | }
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * Default image block size, may be changed by setBlockSize/getBlockSize.
|
---|
412 | *
|
---|
413 | * Note: for speed reasons block size should be a power of 2 !
|
---|
414 | */
|
---|
415 | #define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * fModified bit flags.
|
---|
419 | */
|
---|
420 | #define VDI_IMAGE_MODIFIED_FLAG BIT(0)
|
---|
421 | #define VDI_IMAGE_MODIFIED_FIRST BIT(1)
|
---|
422 | #define VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE BIT(2)
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * Image structure
|
---|
426 | */
|
---|
427 | typedef struct VDIIMAGEDESC
|
---|
428 | {
|
---|
429 | /** Link to parent image descriptor, if any. */
|
---|
430 | struct VDIIMAGEDESC *pPrev;
|
---|
431 | /** Link to child image descriptor, if any. */
|
---|
432 | struct VDIIMAGEDESC *pNext;
|
---|
433 | /** File handle. */
|
---|
434 | RTFILE File;
|
---|
435 | /** True if the image is operating in readonly mode. */
|
---|
436 | bool fReadOnly;
|
---|
437 | /** Image open flags, VDI_OPEN_FLAGS_*. */
|
---|
438 | unsigned fOpen;
|
---|
439 | /** Image pre-header. */
|
---|
440 | VDIPREHEADER PreHeader;
|
---|
441 | /** Image header. */
|
---|
442 | VDIHEADER Header;
|
---|
443 | /** Pointer to a block array. */
|
---|
444 | PVDIIMAGEBLOCKPOINTER paBlocks;
|
---|
445 | /** fFlags copy from image header, for speed optimization. */
|
---|
446 | unsigned fFlags;
|
---|
447 | /** Start offset of block array in image file, here for speed optimization. */
|
---|
448 | unsigned offStartBlocks;
|
---|
449 | /** Start offset of data in image file, here for speed optimization. */
|
---|
450 | unsigned offStartData;
|
---|
451 | /** Block mask for getting the offset into a block from a byte hdd offset. */
|
---|
452 | unsigned uBlockMask;
|
---|
453 | /** Block shift value for converting byte hdd offset into paBlock index. */
|
---|
454 | unsigned uShiftOffset2Index;
|
---|
455 | /** Block shift value for converting block index into offset in image. */
|
---|
456 | unsigned uShiftIndex2Offset;
|
---|
457 | /** Offset of data from the beginning of block. */
|
---|
458 | unsigned offStartBlockData;
|
---|
459 | /** Image is modified flags (VDI_IMAGE_MODIFIED*). */
|
---|
460 | unsigned fModified;
|
---|
461 | /** Container filename. (UTF-8)
|
---|
462 | * @todo Make this variable length to save a bunch of bytes. (low prio) */
|
---|
463 | char szFilename[RTPATH_MAX];
|
---|
464 | } VDIIMAGEDESC, *PVDIIMAGEDESC;
|
---|
465 |
|
---|
466 | /**
|
---|
467 | * Default work buffer size, may be changed by setBufferSize() method.
|
---|
468 | *
|
---|
469 | * For best speed performance it must be equal to image block size.
|
---|
470 | */
|
---|
471 | #define VDIDISK_DEFAULT_BUFFER_SIZE (VDI_IMAGE_DEFAULT_BLOCK_SIZE)
|
---|
472 |
|
---|
473 | /** VDIDISK Signature. */
|
---|
474 | #define VDIDISK_SIGNATURE (0xbedafeda)
|
---|
475 |
|
---|
476 | /**
|
---|
477 | * VBox HDD Container main structure, private part.
|
---|
478 | */
|
---|
479 | struct VDIDISK
|
---|
480 | {
|
---|
481 | /** Structure signature (VDIDISK_SIGNATURE). */
|
---|
482 | uint32_t u32Signature;
|
---|
483 |
|
---|
484 | /** Number of opened images. */
|
---|
485 | unsigned cImages;
|
---|
486 |
|
---|
487 | /** Base image. */
|
---|
488 | PVDIIMAGEDESC pBase;
|
---|
489 |
|
---|
490 | /** Last opened image in the chain.
|
---|
491 | * The same as pBase if only one image is used or the last opened diff image. */
|
---|
492 | PVDIIMAGEDESC pLast;
|
---|
493 |
|
---|
494 | /** Default block size for newly created images. */
|
---|
495 | unsigned cbBlock;
|
---|
496 |
|
---|
497 | /** Working buffer size, allocated only while committing data,
|
---|
498 | * copying block from primary image to secondary and saving previously
|
---|
499 | * zero block. Buffer deallocated after operation complete.
|
---|
500 | * @remark For best performance buffer size must be equal to image's
|
---|
501 | * block size, however it may be decreased for memory saving.
|
---|
502 | */
|
---|
503 | unsigned cbBuf;
|
---|
504 |
|
---|
505 | /** The media interface. */
|
---|
506 | PDMIMEDIA IMedia;
|
---|
507 | /** Pointer to the driver instance. */
|
---|
508 | PPDMDRVINS pDrvIns;
|
---|
509 | };
|
---|
510 |
|
---|
511 |
|
---|
512 | /** Converts a pointer to VDIDISK::IMedia to a PVDIDISK. */
|
---|
513 | #define PDMIMEDIA_2_VDIDISK(pInterface) ( (PVDIDISK)((uintptr_t)pInterface - RT_OFFSETOF(VDIDISK, IMedia)) )
|
---|
514 |
|
---|
515 | /** Converts a pointer to PDMDRVINS::IBase to a PPDMDRVINS. */
|
---|
516 | #define PDMIBASE_2_DRVINS(pInterface) ( (PPDMDRVINS)((uintptr_t)pInterface - RT_OFFSETOF(PDMDRVINS, IBase)) )
|
---|
517 |
|
---|
518 | /** Converts a pointer to PDMDRVINS::IBase to a PVDIDISK. */
|
---|
519 | #define PDMIBASE_2_VDIDISK(pInterface) ( PDMINS2DATA(PDMIBASE_2_DRVINS(pInterface), PVDIDISK) )
|
---|
520 |
|
---|
521 |
|
---|
522 | /*******************************************************************************
|
---|
523 | * Internal Functions *
|
---|
524 | *******************************************************************************/
|
---|
525 | static unsigned getPowerOfTwo(unsigned uNumber);
|
---|
526 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
|
---|
527 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
|
---|
528 | static void vdiInitHeader(PVDIHEADER pHeader, VDIIMAGETYPE enmType, uint32_t fFlags,
|
---|
529 | const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
|
---|
530 | uint32_t cbBlockExtra);
|
---|
531 | static int vdiValidateHeader(PVDIHEADER pHeader);
|
---|
532 | static int vdiCreateImage(const char *pszFilename, VDIIMAGETYPE enmType, unsigned fFlags,
|
---|
533 | uint64_t cbSize, const char *pszComment, PVDIIMAGEDESC pParent,
|
---|
534 | PFNVMPROGRESS pfnProgress, void *pvUser);
|
---|
535 | static void vdiInitImageDesc(PVDIIMAGEDESC pImage);
|
---|
536 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
|
---|
537 | static int vdiOpenImage(PVDIIMAGEDESC *ppImage, const char *pszFilename, unsigned fOpen,
|
---|
538 | PVDIIMAGEDESC pParent);
|
---|
539 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
|
---|
540 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
541 | static int vdiUpdateBlocks(PVDIIMAGEDESC pImage);
|
---|
542 | static void vdiSetModifiedFlag(PVDIIMAGEDESC pImage);
|
---|
543 | static void vdiResetModifiedFlag(PVDIIMAGEDESC pImage);
|
---|
544 | static void vdiDisableLastModifiedUpdate(PVDIIMAGEDESC pImage);
|
---|
545 | #if 0 /* unused */
|
---|
546 | static void vdiEnableLastModifiedUpdate(PVDIIMAGEDESC pImage);
|
---|
547 | #endif
|
---|
548 | static void vdiFlushImage(PVDIIMAGEDESC pImage);
|
---|
549 | static void vdiCloseImage(PVDIIMAGEDESC pImage);
|
---|
550 | static int vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead,
|
---|
551 | unsigned cbToRead, void *pvBuf);
|
---|
552 | static int vdiFillBlockByZeroes(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
553 | static int vdiWriteInBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock,
|
---|
554 | unsigned offWrite, unsigned cbToWrite, const void *pvBuf);
|
---|
555 | static int vdiCopyBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
556 | static int vdiMergeImages(PVDIIMAGEDESC pImageFrom, PVDIIMAGEDESC pImageTo, bool fParentToChild,
|
---|
557 | PFNVMPROGRESS pfnProgress, void *pvUser);
|
---|
558 | static void vdiInitVDIDisk(PVDIDISK pDisk);
|
---|
559 | static void vdiAddImageToList(PVDIDISK pDisk, PVDIIMAGEDESC pImage);
|
---|
560 | static void vdiRemoveImageFromList(PVDIDISK pDisk, PVDIIMAGEDESC pImage);
|
---|
561 | static PVDIIMAGEDESC vdiGetImageByNumber(PVDIDISK pDisk, int nImage);
|
---|
562 | static int vdiChangeImageMode(PVDIIMAGEDESC pImage, bool fReadOnly);
|
---|
563 | static int vdiUpdateReadOnlyHeader(PVDIIMAGEDESC pImage);
|
---|
564 |
|
---|
565 | static int vdiCommitToImage(PVDIDISK pDisk, PVDIIMAGEDESC pDstImage,
|
---|
566 | PFNVMPROGRESS pfnProgress, void *pvUser);
|
---|
567 | static void vdiDumpImage(PVDIIMAGEDESC pImage);
|
---|
568 |
|
---|
569 | static DECLCALLBACK(int) vdiConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle);
|
---|
570 | static DECLCALLBACK(void) vdiDestruct(PPDMDRVINS pDrvIns);
|
---|
571 | static DECLCALLBACK(int) vdiRead(PPDMIMEDIA pInterface,
|
---|
572 | uint64_t off, void *pvBuf, size_t cbRead);
|
---|
573 | static DECLCALLBACK(int) vdiWrite(PPDMIMEDIA pInterface,
|
---|
574 | uint64_t off, const void *pvBuf, size_t cbWrite);
|
---|
575 | static DECLCALLBACK(int) vdiFlush(PPDMIMEDIA pInterface);
|
---|
576 | static DECLCALLBACK(uint64_t) vdiGetSize(PPDMIMEDIA pInterface);
|
---|
577 | static DECLCALLBACK(int) vdiBiosGetGeometry(PPDMIMEDIA pInterface, uint32_t *pcCylinders,
|
---|
578 | uint32_t *pcHeads, uint32_t *pcSectors);
|
---|
579 | static DECLCALLBACK(int) vdiBiosSetGeometry(PPDMIMEDIA pInterface, uint32_t cCylinders,
|
---|
580 | uint32_t cHeads, uint32_t cSectors);
|
---|
581 | static DECLCALLBACK(int) vdiGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid);
|
---|
582 | static DECLCALLBACK(bool) vdiIsReadOnly(PPDMIMEDIA pInterface);
|
---|
583 | static DECLCALLBACK(int) vdiBiosGetTranslation(PPDMIMEDIA pInterface,
|
---|
584 | PPDMBIOSTRANSLATION penmTranslation);
|
---|
585 | static DECLCALLBACK(int) vdiBiosSetTranslation(PPDMIMEDIA pInterface,
|
---|
586 | PDMBIOSTRANSLATION enmTranslation);
|
---|
587 | static DECLCALLBACK(void *) vdiQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface);
|
---|
588 |
|
---|
589 |
|
---|
590 | /**
|
---|
591 | * internal: return power of 2 or 0 if num error.
|
---|
592 | */
|
---|
593 | static unsigned getPowerOfTwo(unsigned uNumber)
|
---|
594 | {
|
---|
595 | if (uNumber == 0)
|
---|
596 | return 0;
|
---|
597 | unsigned uPower2 = 0;
|
---|
598 | while ((uNumber & 1) == 0)
|
---|
599 | {
|
---|
600 | uNumber >>= 1;
|
---|
601 | uPower2++;
|
---|
602 | }
|
---|
603 | return uNumber == 1 ? uPower2 : 0;
|
---|
604 | }
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * internal: init HDD preheader.
|
---|
608 | */
|
---|
609 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
|
---|
610 | {
|
---|
611 | pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
|
---|
612 | pPreHdr->u32Version = VDI_IMAGE_VERSION;
|
---|
613 | memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
|
---|
614 | strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo));
|
---|
615 | }
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * internal: check HDD preheader.
|
---|
619 | */
|
---|
620 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
|
---|
621 | {
|
---|
622 | if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
|
---|
623 | return VERR_VDI_INVALID_SIGNATURE;
|
---|
624 |
|
---|
625 | if ( pPreHdr->u32Version != VDI_IMAGE_VERSION
|
---|
626 | && pPreHdr->u32Version != 0x00000002) /* old version. */
|
---|
627 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
628 |
|
---|
629 | return VINF_SUCCESS;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /**
|
---|
633 | * internal: init HDD header. Always use latest header version.
|
---|
634 | * @param pHeader Assumes it was initially initialized to all zeros.
|
---|
635 | */
|
---|
636 | static void vdiInitHeader(PVDIHEADER pHeader, VDIIMAGETYPE enmType, uint32_t fFlags,
|
---|
637 | const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
|
---|
638 | uint32_t cbBlockExtra)
|
---|
639 | {
|
---|
640 | pHeader->uVersion = VDI_IMAGE_VERSION;
|
---|
641 | pHeader->u.v1.cbHeader = sizeof(VDIHEADER1);
|
---|
642 | pHeader->u.v1.u32Type = (uint32_t)enmType;
|
---|
643 | pHeader->u.v1.fFlags = fFlags;
|
---|
644 | #ifdef VBOX_STRICT
|
---|
645 | char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
|
---|
646 | Assert(!memcmp(pHeader->u.v1.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
|
---|
647 | #endif
|
---|
648 | pHeader->u.v1.szComment[0] = '\0';
|
---|
649 | if (pszComment)
|
---|
650 | {
|
---|
651 | AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1.szComment),
|
---|
652 | ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
|
---|
653 | strncat(pHeader->u.v1.szComment, pszComment, sizeof(pHeader->u.v1.szComment));
|
---|
654 | }
|
---|
655 |
|
---|
656 | /* Mark the geometry not-calculated. */
|
---|
657 | pHeader->u.v1.Geometry.cCylinders = 0;
|
---|
658 | pHeader->u.v1.Geometry.cHeads = 0;
|
---|
659 | pHeader->u.v1.Geometry.cSectors = 0;
|
---|
660 | pHeader->u.v1.Geometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
661 | pHeader->u.v1.u32Translation = PDMBIOSTRANSLATION_AUTO;
|
---|
662 |
|
---|
663 | pHeader->u.v1.cbDisk = cbDisk;
|
---|
664 | pHeader->u.v1.cbBlock = cbBlock;
|
---|
665 | pHeader->u.v1.cBlocks = (uint32_t)(cbDisk / cbBlock);
|
---|
666 | if (cbDisk % cbBlock)
|
---|
667 | pHeader->u.v1.cBlocks++;
|
---|
668 | pHeader->u.v1.cbBlockExtra = cbBlockExtra;
|
---|
669 | pHeader->u.v1.cBlocksAllocated = 0;
|
---|
670 |
|
---|
671 | /* Init offsets. */
|
---|
672 | pHeader->u.v1.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
673 | pHeader->u.v1.offData = RT_ALIGN_32(pHeader->u.v1.offBlocks + (pHeader->u.v1.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
674 |
|
---|
675 | /* Init uuids. */
|
---|
676 | RTUuidCreate(&pHeader->u.v1.uuidCreate);
|
---|
677 | RTUuidClear(&pHeader->u.v1.uuidModify);
|
---|
678 | RTUuidClear(&pHeader->u.v1.uuidLinkage);
|
---|
679 | RTUuidClear(&pHeader->u.v1.uuidParentModify);
|
---|
680 | }
|
---|
681 |
|
---|
682 | /**
|
---|
683 | * internal: check HDD header.
|
---|
684 | */
|
---|
685 | static int vdiValidateHeader(PVDIHEADER pHeader)
|
---|
686 | {
|
---|
687 | /* Check verion-dependend header parameters. */
|
---|
688 | switch (GET_MAJOR_HEADER_VERSION(pHeader))
|
---|
689 | {
|
---|
690 | case 0:
|
---|
691 | {
|
---|
692 | /* Old header version. */
|
---|
693 | break;
|
---|
694 | }
|
---|
695 | case 1:
|
---|
696 | {
|
---|
697 | /* Current header version. */
|
---|
698 |
|
---|
699 | if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
|
---|
700 | {
|
---|
701 | LogRel(("VDI: v1 header size wrong (%d < %d)\n",
|
---|
702 | pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
|
---|
703 | return VERR_VDI_INVALID_HEADER;
|
---|
704 | }
|
---|
705 |
|
---|
706 | if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
|
---|
707 | {
|
---|
708 | LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
|
---|
709 | getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
|
---|
710 | return VERR_VDI_INVALID_HEADER;
|
---|
711 | }
|
---|
712 |
|
---|
713 | if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
|
---|
714 | {
|
---|
715 | LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
|
---|
716 | getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
|
---|
717 | return VERR_VDI_INVALID_HEADER;
|
---|
718 | }
|
---|
719 |
|
---|
720 | if ( getImageType(pHeader) == VDI_IMAGE_TYPE_UNDO
|
---|
721 | || getImageType(pHeader) == VDI_IMAGE_TYPE_DIFF)
|
---|
722 | {
|
---|
723 | if (RTUuidIsNull(getImageParentUUID(pHeader)))
|
---|
724 | {
|
---|
725 | LogRel(("VDI: v1 uuid of parent is 0)\n"));
|
---|
726 | return VERR_VDI_INVALID_HEADER;
|
---|
727 | }
|
---|
728 | if (RTUuidIsNull(getImageParentModificationUUID(pHeader)))
|
---|
729 | {
|
---|
730 | LogRel(("VDI: v1 uuid of parent modification is 0\n"));
|
---|
731 | return VERR_VDI_INVALID_HEADER;
|
---|
732 | }
|
---|
733 | }
|
---|
734 |
|
---|
735 | break;
|
---|
736 | }
|
---|
737 | default:
|
---|
738 | /* Unsupported. */
|
---|
739 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
740 | }
|
---|
741 |
|
---|
742 | /* Check common header parameters. */
|
---|
743 |
|
---|
744 | bool fFailed = false;
|
---|
745 |
|
---|
746 | if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
|
---|
747 | || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
|
---|
748 | {
|
---|
749 | LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
|
---|
750 | fFailed = true;
|
---|
751 | }
|
---|
752 |
|
---|
753 | if (getImageFlags(pHeader) & ~VDI_IMAGE_FLAGS_MASK)
|
---|
754 | {
|
---|
755 | LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
|
---|
756 | fFailed = true;
|
---|
757 | }
|
---|
758 |
|
---|
759 | if ((getImageGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
|
---|
760 | {
|
---|
761 | LogRel(("VDI: wrong sector size (%d != %d)\n",
|
---|
762 | (getImageGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
|
---|
763 | fFailed = true;
|
---|
764 | }
|
---|
765 |
|
---|
766 | if ( getImageDiskSize(pHeader) == 0
|
---|
767 | || getImageBlockSize(pHeader) == 0
|
---|
768 | || getImageBlocks(pHeader) == 0
|
---|
769 | || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
|
---|
770 | {
|
---|
771 | LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
|
---|
772 | getImageDiskSize(pHeader), getImageBlockSize(pHeader),
|
---|
773 | getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
|
---|
774 | fFailed = true;
|
---|
775 | }
|
---|
776 |
|
---|
777 | if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
|
---|
778 | {
|
---|
779 | LogRel(("VDI: too many blocks allocated (%d > %d)\n"
|
---|
780 | " blocksize=%d disksize=%lld\n",
|
---|
781 | getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
|
---|
782 | getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
|
---|
783 | fFailed = true;
|
---|
784 | }
|
---|
785 |
|
---|
786 | if ( getImageExtraBlockSize(pHeader) != 0
|
---|
787 | && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
|
---|
788 | {
|
---|
789 | LogRel(("VDI: wrong extra size (%d, %d)\n",
|
---|
790 | getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
|
---|
791 | fFailed = true;
|
---|
792 | }
|
---|
793 |
|
---|
794 | if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
|
---|
795 | {
|
---|
796 | LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
|
---|
797 | getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
|
---|
798 | fFailed = true;
|
---|
799 | }
|
---|
800 |
|
---|
801 | if (RTUuidIsNull(getImageCreationUUID(pHeader)))
|
---|
802 | {
|
---|
803 | LogRel(("VDI: uuid of creator is 0\n"));
|
---|
804 | fFailed = true;
|
---|
805 | }
|
---|
806 |
|
---|
807 | if (RTUuidIsNull(getImageModificationUUID(pHeader)))
|
---|
808 | {
|
---|
809 | LogRel(("VDI: uuid of modificator is 0\n"));
|
---|
810 | fFailed = true;
|
---|
811 | }
|
---|
812 |
|
---|
813 | return fFailed ? VERR_VDI_INVALID_HEADER : VINF_SUCCESS;
|
---|
814 | }
|
---|
815 |
|
---|
816 | /**
|
---|
817 | * internal: init VDIIMAGEDESC structure.
|
---|
818 | */
|
---|
819 | static void vdiInitImageDesc(PVDIIMAGEDESC pImage)
|
---|
820 | {
|
---|
821 | pImage->pPrev = NULL;
|
---|
822 | pImage->pNext = NULL;
|
---|
823 | pImage->File = NIL_RTFILE;
|
---|
824 | pImage->paBlocks = NULL;
|
---|
825 | }
|
---|
826 |
|
---|
827 | /**
|
---|
828 | * internal: setup VDIIMAGEDESC structure by image header.
|
---|
829 | */
|
---|
830 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
|
---|
831 | {
|
---|
832 | pImage->fFlags = getImageFlags(&pImage->Header);
|
---|
833 | pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
|
---|
834 | pImage->offStartData = getImageDataOffset(&pImage->Header);
|
---|
835 | pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
|
---|
836 | pImage->uShiftIndex2Offset =
|
---|
837 | pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
|
---|
838 | pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
|
---|
839 | if (pImage->offStartBlockData != 0)
|
---|
840 | pImage->uShiftIndex2Offset += getPowerOfTwo(pImage->offStartBlockData);
|
---|
841 | }
|
---|
842 |
|
---|
843 | /**
|
---|
844 | * internal: create image.
|
---|
845 | */
|
---|
846 | static int vdiCreateImage(const char *pszFilename, VDIIMAGETYPE enmType, unsigned fFlags,
|
---|
847 | uint64_t cbSize, const char *pszComment, PVDIIMAGEDESC pParent,
|
---|
848 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
849 | {
|
---|
850 | /* Check args. */
|
---|
851 | Assert(pszFilename);
|
---|
852 | Assert(enmType >= VDI_IMAGE_TYPE_FIRST && enmType <= VDI_IMAGE_TYPE_LAST);
|
---|
853 | Assert(!(fFlags & ~VDI_IMAGE_FLAGS_MASK));
|
---|
854 | Assert(cbSize);
|
---|
855 |
|
---|
856 | /* Special check for comment length. */
|
---|
857 | if ( pszComment
|
---|
858 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
859 | {
|
---|
860 | Log(("vdiCreateImage: pszComment is too long, cb=%d\n", strlen(pszComment)));
|
---|
861 | return VERR_VDI_COMMENT_TOO_LONG;
|
---|
862 | }
|
---|
863 |
|
---|
864 | if ( enmType == VDI_IMAGE_TYPE_UNDO
|
---|
865 | || enmType == VDI_IMAGE_TYPE_DIFF)
|
---|
866 | {
|
---|
867 | Assert(pParent);
|
---|
868 | if ((pParent->PreHeader.u32Version >> 16) != VDI_IMAGE_VERSION_MAJOR)
|
---|
869 | {
|
---|
870 | /* Invalid parent image version. */
|
---|
871 | Log(("vdiCreateImage: unsupported parent version=%08X\n", pParent->PreHeader.u32Version));
|
---|
872 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
873 | }
|
---|
874 |
|
---|
875 | /* get image params from the parent image. */
|
---|
876 | fFlags = getImageFlags(&pParent->Header);
|
---|
877 | cbSize = getImageDiskSize(&pParent->Header);
|
---|
878 | }
|
---|
879 |
|
---|
880 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
881 | if (!pImage)
|
---|
882 | return VERR_NO_MEMORY;
|
---|
883 | vdiInitImageDesc(pImage);
|
---|
884 |
|
---|
885 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
886 | vdiInitHeader(&pImage->Header, enmType, fFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0);
|
---|
887 |
|
---|
888 | if ( enmType == VDI_IMAGE_TYPE_UNDO
|
---|
889 | || enmType == VDI_IMAGE_TYPE_DIFF)
|
---|
890 | {
|
---|
891 | /* Set up linkage information. */
|
---|
892 | pImage->Header.u.v1.uuidLinkage = *getImageCreationUUID(&pParent->Header);
|
---|
893 | pImage->Header.u.v1.uuidParentModify = *getImageModificationUUID(&pParent->Header);
|
---|
894 | }
|
---|
895 |
|
---|
896 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
897 | if (!pImage->paBlocks)
|
---|
898 | {
|
---|
899 | RTMemFree(pImage);
|
---|
900 | return VERR_NO_MEMORY;
|
---|
901 | }
|
---|
902 |
|
---|
903 | if (enmType != VDI_IMAGE_TYPE_FIXED)
|
---|
904 | {
|
---|
905 | /* for growing images mark all blocks in paBlocks as free. */
|
---|
906 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
907 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
908 | }
|
---|
909 | else
|
---|
910 | {
|
---|
911 | /* for fixed images mark all blocks in paBlocks as allocated */
|
---|
912 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
913 | pImage->paBlocks[i] = i;
|
---|
914 | pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
|
---|
915 | }
|
---|
916 |
|
---|
917 | /* Setup image parameters. */
|
---|
918 | vdiSetupImageDesc(pImage);
|
---|
919 |
|
---|
920 | /* create file */
|
---|
921 | int rc = RTFileOpen(&pImage->File,
|
---|
922 | pszFilename,
|
---|
923 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
|
---|
924 | if (VBOX_SUCCESS(rc))
|
---|
925 | {
|
---|
926 | /* Lock image exclusively to close any wrong access by VDI API calls. */
|
---|
927 | uint64_t cbLock = pImage->offStartData
|
---|
928 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
929 | rc = RTFileLock(pImage->File,
|
---|
930 | RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, cbLock);
|
---|
931 | if (VBOX_FAILURE(rc))
|
---|
932 | {
|
---|
933 | cbLock = 0; /* Not locked. */
|
---|
934 | goto l_create_failed;
|
---|
935 | }
|
---|
936 |
|
---|
937 | if (enmType == VDI_IMAGE_TYPE_FIXED)
|
---|
938 | {
|
---|
939 | /*
|
---|
940 | * Allocate & commit whole file if fixed image, it must be more
|
---|
941 | * effective than expanding file by write operations.
|
---|
942 | */
|
---|
943 | rc = RTFileSetSize(pImage->File,
|
---|
944 | pImage->offStartData
|
---|
945 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset));
|
---|
946 | }
|
---|
947 | else
|
---|
948 | {
|
---|
949 | /* Set file size to hold header and blocks array. */
|
---|
950 | rc = RTFileSetSize(pImage->File, pImage->offStartData);
|
---|
951 | }
|
---|
952 | if (VBOX_FAILURE(rc))
|
---|
953 | goto l_create_failed;
|
---|
954 |
|
---|
955 | /* Generate image last-modify uuid */
|
---|
956 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
957 |
|
---|
958 | /* Write pre-header. */
|
---|
959 | rc = RTFileWrite(pImage->File, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
960 | if (VBOX_FAILURE(rc))
|
---|
961 | goto l_create_failed;
|
---|
962 |
|
---|
963 | /* Write header. */
|
---|
964 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
965 | if (VBOX_FAILURE(rc))
|
---|
966 | goto l_create_failed;
|
---|
967 |
|
---|
968 | /* Write blocks array. */
|
---|
969 | rc = RTFileSeek(pImage->File, pImage->offStartBlocks, RTFILE_SEEK_BEGIN, NULL);
|
---|
970 | if (VBOX_FAILURE(rc))
|
---|
971 | goto l_create_failed;
|
---|
972 | rc = RTFileWrite(pImage->File,
|
---|
973 | pImage->paBlocks,
|
---|
974 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
975 | NULL);
|
---|
976 | if (VBOX_FAILURE(rc))
|
---|
977 | goto l_create_failed;
|
---|
978 |
|
---|
979 | if ( (enmType == VDI_IMAGE_TYPE_FIXED)
|
---|
980 | && (fFlags & VDI_IMAGE_FLAGS_ZERO_EXPAND))
|
---|
981 | {
|
---|
982 | /* Fill image with zeroes. */
|
---|
983 |
|
---|
984 | rc = RTFileSeek(pImage->File, pImage->offStartData, RTFILE_SEEK_BEGIN, NULL);
|
---|
985 | if (VBOX_FAILURE(rc))
|
---|
986 | goto l_create_failed;
|
---|
987 |
|
---|
988 | /* alloc tmp zero-filled buffer */
|
---|
989 | void *pvBuf = RTMemTmpAllocZ(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
990 | if (pvBuf)
|
---|
991 | {
|
---|
992 | uint64_t cbFill = (uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset;
|
---|
993 | uint64_t cbDisk = cbFill;
|
---|
994 |
|
---|
995 | /* do loop to fill all image. */
|
---|
996 | while (cbFill > 0)
|
---|
997 | {
|
---|
998 | unsigned to_fill = (unsigned)RT_MIN(cbFill, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
999 |
|
---|
1000 | rc = RTFileWrite(pImage->File, pvBuf, to_fill, NULL);
|
---|
1001 | if (VBOX_FAILURE(rc))
|
---|
1002 | break;
|
---|
1003 |
|
---|
1004 | cbFill -= to_fill;
|
---|
1005 |
|
---|
1006 | if (pfnProgress)
|
---|
1007 | {
|
---|
1008 | rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1009 | (unsigned)(((cbDisk - cbFill) * 100) / cbDisk),
|
---|
1010 | pvUser);
|
---|
1011 | if (VBOX_FAILURE(rc))
|
---|
1012 | break;
|
---|
1013 | }
|
---|
1014 | }
|
---|
1015 | RTMemTmpFree(pvBuf);
|
---|
1016 | }
|
---|
1017 | else
|
---|
1018 | {
|
---|
1019 | /* alloc error */
|
---|
1020 | rc = VERR_NO_MEMORY;
|
---|
1021 | }
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | l_create_failed:
|
---|
1025 |
|
---|
1026 | if (cbLock)
|
---|
1027 | RTFileUnlock(pImage->File, 0, cbLock);
|
---|
1028 |
|
---|
1029 | RTFileClose(pImage->File);
|
---|
1030 |
|
---|
1031 | /* Delete image file if error occured while creating */
|
---|
1032 | if (VBOX_FAILURE(rc))
|
---|
1033 | RTFileDelete(pszFilename);
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | RTMemFree(pImage->paBlocks);
|
---|
1037 | RTMemFree(pImage);
|
---|
1038 |
|
---|
1039 | if ( VBOX_SUCCESS(rc)
|
---|
1040 | && pfnProgress)
|
---|
1041 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
1042 |
|
---|
1043 | Log(("vdiCreateImage: done, filename=\"%s\", rc=%Vrc\n", pszFilename, rc));
|
---|
1044 |
|
---|
1045 | return rc;
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | /**
|
---|
1049 | * Open an image.
|
---|
1050 | * @internal
|
---|
1051 | */
|
---|
1052 | static int vdiOpenImage(PVDIIMAGEDESC *ppImage, const char *pszFilename,
|
---|
1053 | unsigned fOpen, PVDIIMAGEDESC pParent)
|
---|
1054 | {
|
---|
1055 | /*
|
---|
1056 | * Validate input.
|
---|
1057 | */
|
---|
1058 | Assert(ppImage);
|
---|
1059 | Assert(pszFilename);
|
---|
1060 | Assert(!(fOpen & ~VDI_OPEN_FLAGS_MASK));
|
---|
1061 |
|
---|
1062 | PVDIIMAGEDESC pImage;
|
---|
1063 | size_t cchFilename = strlen(pszFilename);
|
---|
1064 | if (cchFilename >= sizeof(pImage->szFilename))
|
---|
1065 | {
|
---|
1066 | AssertMsgFailed(("filename=\"%s\" is too long (%d bytes)!\n", pszFilename, cchFilename));
|
---|
1067 | return VERR_FILENAME_TOO_LONG;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
1071 | if (!pImage)
|
---|
1072 | return VERR_NO_MEMORY;
|
---|
1073 | vdiInitImageDesc(pImage);
|
---|
1074 |
|
---|
1075 | memcpy(pImage->szFilename, pszFilename, cchFilename);
|
---|
1076 | pImage->fOpen = fOpen;
|
---|
1077 |
|
---|
1078 | /*
|
---|
1079 | * Open the image.
|
---|
1080 | */
|
---|
1081 | int rc = RTFileOpen(&pImage->File,
|
---|
1082 | pImage->szFilename,
|
---|
1083 | fOpen & VDI_OPEN_FLAGS_READONLY
|
---|
1084 | ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
|
---|
1085 | : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
1086 | if (VBOX_FAILURE(rc))
|
---|
1087 | {
|
---|
1088 | if (!(fOpen & VDI_OPEN_FLAGS_READONLY))
|
---|
1089 | {
|
---|
1090 | /* Try to open image for reading only. */
|
---|
1091 | rc = RTFileOpen(&pImage->File,
|
---|
1092 | pImage->szFilename,
|
---|
1093 | RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
1094 | if (VBOX_SUCCESS(rc))
|
---|
1095 | pImage->fOpen |= VDI_OPEN_FLAGS_READONLY;
|
---|
1096 | }
|
---|
1097 | if (VBOX_FAILURE(rc))
|
---|
1098 | {
|
---|
1099 | RTMemFree(pImage);
|
---|
1100 | return rc;
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | /* Set up current image r/w state. */
|
---|
1104 | pImage->fReadOnly = !!(pImage->fOpen & VDI_OPEN_FLAGS_READONLY);
|
---|
1105 |
|
---|
1106 | /*
|
---|
1107 | * Set initial file lock for reading header only.
|
---|
1108 | * Length of lock doesn't matter, it just must include image header.
|
---|
1109 | */
|
---|
1110 | uint64_t cbLock = _1M;
|
---|
1111 | rc = RTFileLock(pImage->File, RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY, 0, cbLock);
|
---|
1112 | if (VBOX_FAILURE(rc))
|
---|
1113 | {
|
---|
1114 | cbLock = 0;
|
---|
1115 | goto l_open_failed;
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | /* Read pre-header. */
|
---|
1119 | rc = RTFileRead(pImage->File, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
1120 | if (VBOX_FAILURE(rc))
|
---|
1121 | goto l_open_failed;
|
---|
1122 | rc = vdiValidatePreHeader(&pImage->PreHeader);
|
---|
1123 | if (VBOX_FAILURE(rc))
|
---|
1124 | goto l_open_failed;
|
---|
1125 |
|
---|
1126 | /* Read header. */
|
---|
1127 | pImage->Header.uVersion = pImage->PreHeader.u32Version;
|
---|
1128 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
1129 | {
|
---|
1130 | case 0:
|
---|
1131 | rc = RTFileRead(pImage->File, &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
|
---|
1132 | break;
|
---|
1133 | case 1:
|
---|
1134 | rc = RTFileRead(pImage->File, &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
1135 | break;
|
---|
1136 | default:
|
---|
1137 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
1138 | break;
|
---|
1139 | }
|
---|
1140 | if (VBOX_FAILURE(rc))
|
---|
1141 | goto l_open_failed;
|
---|
1142 |
|
---|
1143 | rc = vdiValidateHeader(&pImage->Header);
|
---|
1144 | if (VBOX_FAILURE(rc))
|
---|
1145 | goto l_open_failed;
|
---|
1146 |
|
---|
1147 | /* Check diff image correctness. */
|
---|
1148 | if (pParent)
|
---|
1149 | {
|
---|
1150 | if (pImage->PreHeader.u32Version != pParent->PreHeader.u32Version)
|
---|
1151 | {
|
---|
1152 | rc = VERR_VDI_IMAGES_VERSION_MISMATCH;
|
---|
1153 | goto l_open_failed;
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | if ( getImageType(&pImage->Header) != VDI_IMAGE_TYPE_UNDO
|
---|
1157 | && getImageType(&pImage->Header) != VDI_IMAGE_TYPE_DIFF)
|
---|
1158 | {
|
---|
1159 | rc = VERR_VDI_WRONG_DIFF_IMAGE;
|
---|
1160 | goto l_open_failed;
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | if ( getImageDiskSize(&pImage->Header) != getImageDiskSize(&pParent->Header)
|
---|
1164 | || getImageBlockSize(&pImage->Header) != getImageBlockSize(&pParent->Header)
|
---|
1165 | || getImageBlocks(&pImage->Header) != getImageBlocks(&pParent->Header)
|
---|
1166 | || getImageExtraBlockSize(&pImage->Header) != getImageExtraBlockSize(&pParent->Header))
|
---|
1167 | {
|
---|
1168 | rc = VERR_VDI_WRONG_DIFF_IMAGE;
|
---|
1169 | goto l_open_failed;
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | /* Check linkage data. */
|
---|
1173 | if ( RTUuidCompare(getImageParentUUID(&pImage->Header),
|
---|
1174 | getImageCreationUUID(&pParent->Header))
|
---|
1175 | || RTUuidCompare(getImageParentModificationUUID(&pImage->Header),
|
---|
1176 | getImageModificationUUID(&pParent->Header)))
|
---|
1177 | {
|
---|
1178 | rc = VERR_VDI_IMAGES_UUID_MISMATCH;
|
---|
1179 | goto l_open_failed;
|
---|
1180 | }
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | /* Setup image parameters by header. */
|
---|
1184 | vdiSetupImageDesc(pImage);
|
---|
1185 |
|
---|
1186 | /* reset modified flag into first-modified state. */
|
---|
1187 | pImage->fModified = VDI_IMAGE_MODIFIED_FIRST;
|
---|
1188 |
|
---|
1189 | /* Image is validated, set working file lock on it. */
|
---|
1190 | rc = RTFileUnlock(pImage->File, 0, cbLock);
|
---|
1191 | AssertRC(rc);
|
---|
1192 | cbLock = pImage->offStartData
|
---|
1193 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
1194 | rc = RTFileLock(pImage->File,
|
---|
1195 | (pImage->fReadOnly) ?
|
---|
1196 | RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY :
|
---|
1197 | RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY,
|
---|
1198 | 0,
|
---|
1199 | cbLock);
|
---|
1200 | if ( VBOX_FAILURE(rc)
|
---|
1201 | && !pImage->fReadOnly)
|
---|
1202 | {
|
---|
1203 | /* Failed to lock image for writing, try read-only lock. */
|
---|
1204 | rc = RTFileLock(pImage->File,
|
---|
1205 | RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY, 0, cbLock);
|
---|
1206 | if (VBOX_SUCCESS(rc))
|
---|
1207 | pImage->fReadOnly = true;
|
---|
1208 | }
|
---|
1209 | if (VBOX_FAILURE(rc))
|
---|
1210 | {
|
---|
1211 | cbLock = 0; /* Not locked. */
|
---|
1212 | goto l_open_failed;
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | /* Allocate memory for blocks array. */
|
---|
1216 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
1217 | if (!pImage->paBlocks)
|
---|
1218 | {
|
---|
1219 | rc = VERR_NO_MEMORY;
|
---|
1220 | goto l_open_failed;
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | /* Read blocks array. */
|
---|
1224 | rc = RTFileSeek(pImage->File, pImage->offStartBlocks, RTFILE_SEEK_BEGIN, NULL);
|
---|
1225 | if (VBOX_FAILURE(rc))
|
---|
1226 | goto l_open_failed;
|
---|
1227 | rc = RTFileRead(pImage->File, pImage->paBlocks,
|
---|
1228 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER), NULL);
|
---|
1229 | if (VBOX_FAILURE(rc))
|
---|
1230 | goto l_open_failed;
|
---|
1231 |
|
---|
1232 | /* all done. */
|
---|
1233 | *ppImage = pImage;
|
---|
1234 | return VINF_SUCCESS;
|
---|
1235 |
|
---|
1236 | l_open_failed:
|
---|
1237 | /* Clean up. */
|
---|
1238 | if (pImage->paBlocks)
|
---|
1239 | RTMemFree(pImage->paBlocks);
|
---|
1240 | if (cbLock)
|
---|
1241 | RTFileUnlock(pImage->File, 0, cbLock);
|
---|
1242 | RTFileClose(pImage->File);
|
---|
1243 | RTMemFree(pImage);
|
---|
1244 | Log(("vdiOpenImage: failed, filename=\"%s\", rc=%Vrc\n", pszFilename, rc));
|
---|
1245 | return rc;
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | /**
|
---|
1249 | * internal: save header to file.
|
---|
1250 | */
|
---|
1251 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
|
---|
1252 | {
|
---|
1253 | /* Seek to header start. */
|
---|
1254 | int rc = RTFileSeek(pImage->File, sizeof(VDIPREHEADER), RTFILE_SEEK_BEGIN, NULL);
|
---|
1255 | if (VBOX_SUCCESS(rc))
|
---|
1256 | {
|
---|
1257 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
1258 | {
|
---|
1259 | case 0:
|
---|
1260 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
|
---|
1261 | break;
|
---|
1262 | case 1:
|
---|
1263 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
1264 | break;
|
---|
1265 | default:
|
---|
1266 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
1267 | break;
|
---|
1268 | }
|
---|
1269 | }
|
---|
1270 | AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Vrc\n", pImage->szFilename, rc));
|
---|
1271 | return rc;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /**
|
---|
1275 | * internal: save block pointer to file, save header to file.
|
---|
1276 | */
|
---|
1277 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
1278 | {
|
---|
1279 | /* Update image header. */
|
---|
1280 | int rc = vdiUpdateHeader(pImage);
|
---|
1281 | if (VBOX_SUCCESS(rc))
|
---|
1282 | {
|
---|
1283 | /* write only one block pointer. */
|
---|
1284 | rc = RTFileSeek(pImage->File,
|
---|
1285 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
1286 | RTFILE_SEEK_BEGIN,
|
---|
1287 | NULL);
|
---|
1288 | if (VBOX_SUCCESS(rc))
|
---|
1289 | rc = RTFileWrite(pImage->File,
|
---|
1290 | &pImage->paBlocks[uBlock],
|
---|
1291 | sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
1292 | NULL);
|
---|
1293 | AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Vrc\n",
|
---|
1294 | uBlock, pImage->szFilename, rc));
|
---|
1295 | }
|
---|
1296 | return rc;
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | /**
|
---|
1300 | * internal: save blocks array to file, save header to file.
|
---|
1301 | */
|
---|
1302 | static int vdiUpdateBlocks(PVDIIMAGEDESC pImage)
|
---|
1303 | {
|
---|
1304 | /* Update image header. */
|
---|
1305 | int rc = vdiUpdateHeader(pImage);
|
---|
1306 | if (VBOX_SUCCESS(rc))
|
---|
1307 | {
|
---|
1308 | /* write the block pointers array. */
|
---|
1309 | rc = RTFileSeek(pImage->File, pImage->offStartBlocks, RTFILE_SEEK_BEGIN, NULL);
|
---|
1310 | if (VBOX_SUCCESS(rc))
|
---|
1311 | rc = RTFileWrite(pImage->File,
|
---|
1312 | pImage->paBlocks,
|
---|
1313 | sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header),
|
---|
1314 | NULL);
|
---|
1315 | AssertMsgRC(rc, ("vdiUpdateBlocks failed, filename=\"%s\", rc=%Vrc\n",
|
---|
1316 | pImage->szFilename, rc));
|
---|
1317 | }
|
---|
1318 | return rc;
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /**
|
---|
1322 | * internal: mark image as modified, if this is the first change - update image header
|
---|
1323 | * on disk with a new uuidModify value.
|
---|
1324 | */
|
---|
1325 | static void vdiSetModifiedFlag(PVDIIMAGEDESC pImage)
|
---|
1326 | {
|
---|
1327 | pImage->fModified |= VDI_IMAGE_MODIFIED_FLAG;
|
---|
1328 | if (pImage->fModified & VDI_IMAGE_MODIFIED_FIRST)
|
---|
1329 | {
|
---|
1330 | pImage->fModified &= ~VDI_IMAGE_MODIFIED_FIRST;
|
---|
1331 |
|
---|
1332 | /* first modify - generate uuidModify and save to file. */
|
---|
1333 | vdiResetModifiedFlag(pImage);
|
---|
1334 |
|
---|
1335 | if (!(pImage->fModified | VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
1336 | {
|
---|
1337 | /* save header to file,
|
---|
1338 | * note: no rc checking.
|
---|
1339 | */
|
---|
1340 | vdiUpdateHeader(pImage);
|
---|
1341 | }
|
---|
1342 | }
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | /**
|
---|
1346 | * internal: generate new uuidModify if the image was changed.
|
---|
1347 | */
|
---|
1348 | static void vdiResetModifiedFlag(PVDIIMAGEDESC pImage)
|
---|
1349 | {
|
---|
1350 | if (pImage->fModified & VDI_IMAGE_MODIFIED_FLAG)
|
---|
1351 | {
|
---|
1352 | /* generate new last-modified uuid */
|
---|
1353 | if (!(pImage->fModified | VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
1354 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
1355 |
|
---|
1356 | pImage->fModified &= ~VDI_IMAGE_MODIFIED_FLAG;
|
---|
1357 | }
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 | /**
|
---|
1361 | * internal: disables updates of the last-modified UUID
|
---|
1362 | * when performing image writes.
|
---|
1363 | */
|
---|
1364 | static void vdiDisableLastModifiedUpdate(PVDIIMAGEDESC pImage)
|
---|
1365 | {
|
---|
1366 | pImage->fModified |= VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE;
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | #if 0 /* unused */
|
---|
1370 | /**
|
---|
1371 | * internal: enables updates of the last-modified UUID
|
---|
1372 | * when performing image writes.
|
---|
1373 | */
|
---|
1374 | static void vdiEnableLastModifiedUpdate(PVDIIMAGEDESC pImage)
|
---|
1375 | {
|
---|
1376 | pImage->fModified &= ~VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE;
|
---|
1377 | }
|
---|
1378 | #endif
|
---|
1379 |
|
---|
1380 | /**
|
---|
1381 | * internal: flush image file to disk.
|
---|
1382 | */
|
---|
1383 | static void vdiFlushImage(PVDIIMAGEDESC pImage)
|
---|
1384 | {
|
---|
1385 | if (!pImage->fReadOnly)
|
---|
1386 | {
|
---|
1387 | /* Update last-modified uuid if need. */
|
---|
1388 | vdiResetModifiedFlag(pImage);
|
---|
1389 |
|
---|
1390 | /* Save header. */
|
---|
1391 | int rc = vdiUpdateHeader(pImage);
|
---|
1392 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
|
---|
1393 | pImage->szFilename, rc));
|
---|
1394 | RTFileFlush(pImage->File);
|
---|
1395 | }
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | /**
|
---|
1399 | * internal: close image file.
|
---|
1400 | */
|
---|
1401 | static void vdiCloseImage(PVDIIMAGEDESC pImage)
|
---|
1402 | {
|
---|
1403 | /* Params checking. */
|
---|
1404 | Assert(pImage);
|
---|
1405 | Assert(pImage->File != NIL_RTFILE);
|
---|
1406 |
|
---|
1407 | vdiFlushImage(pImage);
|
---|
1408 | RTFileUnlock(pImage->File,
|
---|
1409 | 0,
|
---|
1410 | pImage->offStartData
|
---|
1411 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset));
|
---|
1412 | RTFileClose(pImage->File);
|
---|
1413 |
|
---|
1414 | /* free image resources */
|
---|
1415 | RTMemFree(pImage->paBlocks);
|
---|
1416 | RTMemFree(pImage);
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | /**
|
---|
1420 | * internal: read data inside image block.
|
---|
1421 | *
|
---|
1422 | * note: uBlock must be valid, readed data must not overlap block bounds.
|
---|
1423 | */
|
---|
1424 | static int vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead,
|
---|
1425 | unsigned cbToRead, void *pvBuf)
|
---|
1426 | {
|
---|
1427 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
1428 | {
|
---|
1429 | /* block present in image file */
|
---|
1430 | uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
|
---|
1431 | + (pImage->offStartData + pImage->offStartBlockData + offRead);
|
---|
1432 | int rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
1433 | if (VBOX_SUCCESS(rc))
|
---|
1434 | rc = RTFileRead(pImage->File, pvBuf, cbToRead, NULL);
|
---|
1435 | if (VBOX_FAILURE(rc))
|
---|
1436 | Log(("vdiReadInBlock: rc=%Vrc filename=\"%s\" uBlock=%u offRead=%u cbToRead=%u u64Offset=%llu\n",
|
---|
1437 | rc, pImage->szFilename, uBlock, offRead, cbToRead, u64Offset));
|
---|
1438 | return rc;
|
---|
1439 | }
|
---|
1440 |
|
---|
1441 | /* Returns zeroes for both free and zero block types. */
|
---|
1442 | memset(pvBuf, 0, cbToRead);
|
---|
1443 | return VINF_SUCCESS;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | /**
|
---|
1447 | * Read data from virtual HDD.
|
---|
1448 | *
|
---|
1449 | * @returns VBox status code.
|
---|
1450 | * @param pDisk Pointer to VDI HDD container.
|
---|
1451 | * @param offStart Offset of first reading byte from start of disk.
|
---|
1452 | * @param pvBuf Pointer to buffer for reading data.
|
---|
1453 | * @param cbToRead Number of bytes to read.
|
---|
1454 | */
|
---|
1455 | IDER3DECL(int) VDIDiskRead(PVDIDISK pDisk, uint64_t offStart, void *pvBuf, unsigned cbToRead)
|
---|
1456 | {
|
---|
1457 | /* sanity check */
|
---|
1458 | Assert(pDisk);
|
---|
1459 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
1460 |
|
---|
1461 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
1462 | Assert(pImage);
|
---|
1463 |
|
---|
1464 | /* Check params. */
|
---|
1465 | if ( offStart + cbToRead > getImageDiskSize(&pImage->Header)
|
---|
1466 | || cbToRead == 0)
|
---|
1467 | {
|
---|
1468 | AssertMsgFailed(("offStart=%llu cbToRead=%u\n", offStart, cbToRead));
|
---|
1469 | return VERR_INVALID_PARAMETER;
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | /* Calculate starting block number and offset inside it. */
|
---|
1473 | unsigned uBlock = (unsigned)(offStart >> pImage->uShiftOffset2Index);
|
---|
1474 | unsigned offRead = (unsigned)offStart & pImage->uBlockMask;
|
---|
1475 |
|
---|
1476 | /* Save block size here for speed optimization. */
|
---|
1477 | unsigned cbBlock = getImageBlockSize(&pImage->Header);
|
---|
1478 |
|
---|
1479 | /* loop through blocks */
|
---|
1480 | int rc;
|
---|
1481 | for (;;)
|
---|
1482 | {
|
---|
1483 | unsigned to_read;
|
---|
1484 | if ((offRead + cbToRead) <= cbBlock)
|
---|
1485 | to_read = cbToRead;
|
---|
1486 | else
|
---|
1487 | to_read = cbBlock - offRead;
|
---|
1488 |
|
---|
1489 | if (pDisk->cImages > 1)
|
---|
1490 | {
|
---|
1491 | /* Differencing images are used, handle them. */
|
---|
1492 | pImage = pDisk->pLast;
|
---|
1493 |
|
---|
1494 | /* Search for image with allocated block. */
|
---|
1495 | while (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1496 | {
|
---|
1497 | pImage = pImage->pPrev;
|
---|
1498 | if (!pImage)
|
---|
1499 | {
|
---|
1500 | /* Block is not allocated in all images of chain. */
|
---|
1501 | pImage = pDisk->pLast;
|
---|
1502 | break;
|
---|
1503 | }
|
---|
1504 | }
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | rc = vdiReadInBlock(pImage, uBlock, offRead, to_read, pvBuf);
|
---|
1508 |
|
---|
1509 | cbToRead -= to_read;
|
---|
1510 | if ( cbToRead == 0
|
---|
1511 | || VBOX_FAILURE(rc))
|
---|
1512 | break;
|
---|
1513 |
|
---|
1514 | /* goto next block */
|
---|
1515 | uBlock++;
|
---|
1516 | offRead = 0;
|
---|
1517 | pvBuf = (char *)pvBuf + to_read;
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | return rc;
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | /**
|
---|
1524 | * internal: fill the whole block with zeroes.
|
---|
1525 | *
|
---|
1526 | * note: block id must be valid, block must be already allocated in file.
|
---|
1527 | * note: if pDisk is NULL, the default buffer size is used
|
---|
1528 | */
|
---|
1529 | static int vdiFillBlockByZeroes(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
1530 | {
|
---|
1531 | int rc;
|
---|
1532 |
|
---|
1533 | /* seek to start of block in file. */
|
---|
1534 | uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
|
---|
1535 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
1536 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
1537 | if (VBOX_FAILURE(rc))
|
---|
1538 | {
|
---|
1539 | Log(("vdiFillBlockByZeroes: seek rc=%Vrc filename=\"%s\" uBlock=%u u64Offset=%llu\n",
|
---|
1540 | rc, pImage->szFilename, uBlock, u64Offset));
|
---|
1541 | return rc;
|
---|
1542 | }
|
---|
1543 |
|
---|
1544 | /* alloc tmp zero-filled buffer */
|
---|
1545 | void *pvBuf = RTMemTmpAllocZ(pDisk ? pDisk->cbBuf : VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1546 | if (!pvBuf)
|
---|
1547 | return VERR_NO_MEMORY;
|
---|
1548 |
|
---|
1549 | unsigned cbFill = getImageBlockSize(&pImage->Header);
|
---|
1550 |
|
---|
1551 | /* do loop, because buffer size may be less then block size */
|
---|
1552 | while (cbFill > 0)
|
---|
1553 | {
|
---|
1554 | unsigned to_fill = RT_MIN(cbFill, pDisk ? pDisk->cbBuf : VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1555 | rc = RTFileWrite(pImage->File, pvBuf, to_fill, NULL);
|
---|
1556 | if (VBOX_FAILURE(rc))
|
---|
1557 | {
|
---|
1558 | Log(("vdiFillBlockByZeroes: write rc=%Vrc filename=\"%s\" uBlock=%u u64Offset=%llu cbFill=%u to_fill=%u\n",
|
---|
1559 | rc, pImage->szFilename, uBlock, u64Offset, cbFill, to_fill));
|
---|
1560 | break;
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | cbFill -= to_fill;
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | RTMemTmpFree(pvBuf);
|
---|
1567 | return rc;
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | /**
|
---|
1571 | * internal: write data inside image block.
|
---|
1572 | *
|
---|
1573 | * note: uBlock must be valid, written data must not overlap block bounds.
|
---|
1574 | */
|
---|
1575 | static int vdiWriteInBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offWrite, unsigned cbToWrite, const void *pvBuf)
|
---|
1576 | {
|
---|
1577 | int rc;
|
---|
1578 |
|
---|
1579 | /* Check if we can write into file. */
|
---|
1580 | if (pImage->fReadOnly)
|
---|
1581 | {
|
---|
1582 | Log(("vdiWriteInBlock: failed, image \"%s\" is read-only!\n", pImage->szFilename));
|
---|
1583 | return VERR_WRITE_PROTECT;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | vdiSetModifiedFlag(pImage);
|
---|
1587 |
|
---|
1588 | if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
1589 | {
|
---|
1590 | /* need to allocate a new block in image file */
|
---|
1591 |
|
---|
1592 | /* expand file by one block */
|
---|
1593 | uint64_t u64Size = (((uint64_t)(getImageBlocksAllocated(&pImage->Header) + 1)) << pImage->uShiftIndex2Offset)
|
---|
1594 | + pImage->offStartData;
|
---|
1595 | rc = RTFileSetSize(pImage->File, u64Size);
|
---|
1596 | if (VBOX_FAILURE(rc))
|
---|
1597 | {
|
---|
1598 | Log(("vdiWriteInBlock: set size rc=%Vrc filename=\"%s\" uBlock=%u u64Size=%llu\n",
|
---|
1599 | rc, pImage->szFilename, uBlock, u64Size));
|
---|
1600 | return rc;
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
1604 | pImage->paBlocks[uBlock] = cBlocksAllocated;
|
---|
1605 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
|
---|
1606 |
|
---|
1607 | if ( pImage->fFlags & VDI_IMAGE_FLAGS_ZERO_EXPAND
|
---|
1608 | || pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1609 | {
|
---|
1610 | /* Fill newly allocated block by zeroes. */
|
---|
1611 |
|
---|
1612 | if (offWrite || cbToWrite != getImageBlockSize(&pImage->Header))
|
---|
1613 | {
|
---|
1614 | rc = vdiFillBlockByZeroes(pDisk, pImage, uBlock);
|
---|
1615 | if (VBOX_FAILURE(rc))
|
---|
1616 | return rc;
|
---|
1617 | }
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | rc = vdiUpdateBlockInfo(pImage, uBlock);
|
---|
1621 | if (VBOX_FAILURE(rc))
|
---|
1622 | return rc;
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | /* Now block present in image file, write data inside it. */
|
---|
1626 | uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
|
---|
1627 | + (pImage->offStartData + pImage->offStartBlockData + offWrite);
|
---|
1628 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
1629 | if (VBOX_SUCCESS(rc))
|
---|
1630 | {
|
---|
1631 | rc = RTFileWrite(pImage->File, pvBuf, cbToWrite, NULL);
|
---|
1632 | if (VBOX_FAILURE(rc))
|
---|
1633 | Log(("vdiWriteInBlock: write rc=%Vrc filename=\"%s\" uBlock=%u offWrite=%u u64Offset=%llu cbToWrite=%u\n",
|
---|
1634 | rc, pImage->szFilename, uBlock, offWrite, u64Offset, cbToWrite));
|
---|
1635 | }
|
---|
1636 | else
|
---|
1637 | Log(("vdiWriteInBlock: seek rc=%Vrc filename=\"%s\" uBlock=%u offWrite=%u u64Offset=%llu\n",
|
---|
1638 | rc, pImage->szFilename, uBlock, offWrite, u64Offset));
|
---|
1639 |
|
---|
1640 | return rc;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | /**
|
---|
1644 | * internal: copy data block from one (parent) image to last image.
|
---|
1645 | */
|
---|
1646 | static int vdiCopyBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
1647 | {
|
---|
1648 | Assert(pImage != pDisk->pLast);
|
---|
1649 |
|
---|
1650 | if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1651 | {
|
---|
1652 | /*
|
---|
1653 | * if src block is zero, set dst block to zero too.
|
---|
1654 | */
|
---|
1655 | pDisk->pLast->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1656 | return VINF_SUCCESS;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | /* alloc tmp buffer */
|
---|
1660 | void *pvBuf = RTMemTmpAlloc(pDisk->cbBuf);
|
---|
1661 | if (!pvBuf)
|
---|
1662 | return VERR_NO_MEMORY;
|
---|
1663 |
|
---|
1664 | int rc = VINF_SUCCESS;
|
---|
1665 |
|
---|
1666 | unsigned cbCopy = getImageBlockSize(&pImage->Header);
|
---|
1667 | unsigned offCopy = 0;
|
---|
1668 |
|
---|
1669 | /* do loop, because buffer size may be less then block size */
|
---|
1670 | while (cbCopy > 0)
|
---|
1671 | {
|
---|
1672 | unsigned to_copy = RT_MIN(cbCopy, pDisk->cbBuf);
|
---|
1673 | rc = vdiReadInBlock(pImage, uBlock, offCopy, to_copy, pvBuf);
|
---|
1674 | if (VBOX_FAILURE(rc))
|
---|
1675 | break;
|
---|
1676 |
|
---|
1677 | rc = vdiWriteInBlock(pDisk, pDisk->pLast, uBlock, offCopy, to_copy, pvBuf);
|
---|
1678 | if (VBOX_FAILURE(rc))
|
---|
1679 | break;
|
---|
1680 |
|
---|
1681 | cbCopy -= to_copy;
|
---|
1682 | offCopy += to_copy;
|
---|
1683 | }
|
---|
1684 |
|
---|
1685 | RTMemTmpFree(pvBuf);
|
---|
1686 | return rc;
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | /**
|
---|
1690 | * Write data to virtual HDD.
|
---|
1691 | *
|
---|
1692 | * @returns VBox status code.
|
---|
1693 | * @param pDisk Pointer to VDI HDD container.
|
---|
1694 | * @param offStart Offset of first writing byte from start of HDD.
|
---|
1695 | * @param pvBuf Pointer to buffer of writing data.
|
---|
1696 | * @param cbToWrite Number of bytes to write.
|
---|
1697 | */
|
---|
1698 | IDER3DECL(int) VDIDiskWrite(PVDIDISK pDisk, uint64_t offStart, const void *pvBuf, unsigned cbToWrite)
|
---|
1699 | {
|
---|
1700 | /* sanity check */
|
---|
1701 | Assert(pDisk);
|
---|
1702 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
1703 |
|
---|
1704 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
1705 | Assert(pImage);
|
---|
1706 |
|
---|
1707 | /* Check params. */
|
---|
1708 | if ( offStart + cbToWrite > getImageDiskSize(&pImage->Header)
|
---|
1709 | || cbToWrite == 0)
|
---|
1710 | {
|
---|
1711 | AssertMsgFailed(("offStart=%llu cbToWrite=%u\n", offStart, cbToWrite));
|
---|
1712 | return VERR_INVALID_PARAMETER;
|
---|
1713 | }
|
---|
1714 |
|
---|
1715 | /* Calculate starting block number and offset inside it. */
|
---|
1716 | unsigned uBlock = (unsigned)(offStart >> pImage->uShiftOffset2Index);
|
---|
1717 | unsigned offWrite = (unsigned)offStart & pImage->uBlockMask;
|
---|
1718 | unsigned cbBlock = getImageBlockSize(&pImage->Header);
|
---|
1719 |
|
---|
1720 | /* loop through blocks */
|
---|
1721 | int rc = VINF_SUCCESS;
|
---|
1722 | for (;;)
|
---|
1723 | {
|
---|
1724 | unsigned to_write;
|
---|
1725 | if (offWrite + cbToWrite <= cbBlock)
|
---|
1726 | to_write = cbToWrite;
|
---|
1727 | else
|
---|
1728 | to_write = cbBlock - offWrite;
|
---|
1729 |
|
---|
1730 | /* All callers write less than a VDI block right now (assuming
|
---|
1731 | * default VDI block size). So not worth optimizing for the case
|
---|
1732 | * where a full block is overwritten (no copying required).
|
---|
1733 | * Checking whether a block is all zeroes after the write is too
|
---|
1734 | * expensive (would require reading the rest of the block). */
|
---|
1735 |
|
---|
1736 | if (pDisk->cImages > 1)
|
---|
1737 | {
|
---|
1738 | /* Differencing images are used, handle them. */
|
---|
1739 |
|
---|
1740 | /* Search for image with allocated block. */
|
---|
1741 | while (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1742 | {
|
---|
1743 | pImage = pImage->pPrev;
|
---|
1744 | if (!pImage)
|
---|
1745 | {
|
---|
1746 | /* Block is not allocated in all images of chain. */
|
---|
1747 | pImage = pDisk->pLast;
|
---|
1748 | break;
|
---|
1749 | }
|
---|
1750 | }
|
---|
1751 |
|
---|
1752 | if (pImage != pDisk->pLast)
|
---|
1753 | {
|
---|
1754 | /* One of parent image has a block data, copy it into last image. */
|
---|
1755 | rc = vdiCopyBlock(pDisk, pImage, uBlock);
|
---|
1756 | if (VBOX_FAILURE(rc))
|
---|
1757 | break;
|
---|
1758 | pImage = pDisk->pLast;
|
---|
1759 | }
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 | /* If the destination block is unallocated at this point, it's either
|
---|
1763 | * a zero block or a block which hasn't been used so far (which also
|
---|
1764 | * means that it's a zero block. Don't need to write anything to this
|
---|
1765 | * block if the data consists of just zeroes. */
|
---|
1766 | bool fBlockZeroed = false; /* assume data, for blocks already with data */
|
---|
1767 | if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
1768 | {
|
---|
1769 | /* Check block for data. */
|
---|
1770 | fBlockZeroed = true; /* Block is zeroed flag. */
|
---|
1771 | for (unsigned i = 0; i < (to_write >> 2); i++)
|
---|
1772 | if (((uint32_t *)pvBuf)[i] != 0)
|
---|
1773 | {
|
---|
1774 | /* Block is not zeroed! */
|
---|
1775 | fBlockZeroed = false;
|
---|
1776 | break;
|
---|
1777 | }
|
---|
1778 | }
|
---|
1779 |
|
---|
1780 | /* Actually write the data into block. */
|
---|
1781 | if (!fBlockZeroed)
|
---|
1782 | rc = vdiWriteInBlock(pDisk, pImage, uBlock, offWrite, to_write, pvBuf);
|
---|
1783 |
|
---|
1784 | cbToWrite -= to_write;
|
---|
1785 | if ( cbToWrite == 0
|
---|
1786 | || VBOX_FAILURE(rc))
|
---|
1787 | break;
|
---|
1788 |
|
---|
1789 | /* goto next block */
|
---|
1790 | uBlock++;
|
---|
1791 | offWrite = 0;
|
---|
1792 | pvBuf = (char *)pvBuf + to_write;
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | return rc;
|
---|
1796 | }
|
---|
1797 |
|
---|
1798 | /**
|
---|
1799 | * internal: commit one image to another, no changes to header, just
|
---|
1800 | * plain copy operation. Blocks that are not allocated in the source
|
---|
1801 | * image (i.e. inherited by its parent(s)) are not merged.
|
---|
1802 | *
|
---|
1803 | * @param pImageFrom source image
|
---|
1804 | * @param pImageTo target image (will receive all the modifications)
|
---|
1805 | * @param fParentToChild true if the source image is parent of the target one,
|
---|
1806 | * false of the target image is the parent of the source.
|
---|
1807 | * @param pfnProgress progress callback (NULL if not to be used)
|
---|
1808 | * @param pvUser user argument for the progress callback
|
---|
1809 | *
|
---|
1810 | * @note the target image has to be opened read/write
|
---|
1811 | * @note this method does not check whether merging is possible!
|
---|
1812 | */
|
---|
1813 | static int vdiMergeImages(PVDIIMAGEDESC pImageFrom, PVDIIMAGEDESC pImageTo, bool fParentToChild,
|
---|
1814 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1815 | {
|
---|
1816 | Assert(pImageFrom);
|
---|
1817 | Assert(pImageTo);
|
---|
1818 |
|
---|
1819 | Log(("vdiMergeImages: merging from image \"%s\" to image \"%s\" (fParentToChild=%d)\n",
|
---|
1820 | pImageFrom->szFilename, pImageTo->szFilename, fParentToChild));
|
---|
1821 |
|
---|
1822 | /* alloc tmp buffer */
|
---|
1823 | void *pvBuf = RTMemTmpAlloc(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1824 | if (!pvBuf)
|
---|
1825 | return VERR_NO_MEMORY;
|
---|
1826 |
|
---|
1827 | int rc = VINF_SUCCESS;
|
---|
1828 |
|
---|
1829 | if (!fParentToChild)
|
---|
1830 | {
|
---|
1831 | /*
|
---|
1832 | * Commit the child image to the parent image.
|
---|
1833 | * Child is the source (from), parent is the target (to).
|
---|
1834 | */
|
---|
1835 |
|
---|
1836 | unsigned cBlocks = getImageBlocks(&pImageFrom->Header);
|
---|
1837 |
|
---|
1838 | for (unsigned uBlock = 0; uBlock < cBlocks; uBlock++)
|
---|
1839 | {
|
---|
1840 | /* only process blocks that are allocated in the source image */
|
---|
1841 | if (pImageFrom->paBlocks[uBlock] != VDI_IMAGE_BLOCK_FREE)
|
---|
1842 | {
|
---|
1843 | /* Found used block in source image, commit it. */
|
---|
1844 | if ( pImageFrom->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
1845 | && !IS_VDI_IMAGE_BLOCK_ALLOCATED(pImageTo->paBlocks[uBlock]))
|
---|
1846 | {
|
---|
1847 | /* Block is zero in the source image and not allocated in the target image. */
|
---|
1848 | pImageTo->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1849 | vdiSetModifiedFlag(pImageTo);
|
---|
1850 | }
|
---|
1851 | else
|
---|
1852 | {
|
---|
1853 | /* Block is not zero / allocated in source image. */
|
---|
1854 | unsigned cbCommit = getImageBlockSize(&pImageFrom->Header);
|
---|
1855 | unsigned offCommit = 0;
|
---|
1856 |
|
---|
1857 | /* do loop, because buffer size may be less then block size */
|
---|
1858 | while (cbCommit > 0)
|
---|
1859 | {
|
---|
1860 | unsigned cbToCopy = RT_MIN(cbCommit, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1861 |
|
---|
1862 | rc = vdiReadInBlock(pImageFrom, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1863 | if (VBOX_FAILURE(rc))
|
---|
1864 | break;
|
---|
1865 |
|
---|
1866 | rc = vdiWriteInBlock(NULL, pImageTo, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1867 | if (VBOX_FAILURE(rc))
|
---|
1868 | break;
|
---|
1869 |
|
---|
1870 | cbCommit -= cbToCopy;
|
---|
1871 | offCommit += cbToCopy;
|
---|
1872 | }
|
---|
1873 | if (VBOX_FAILURE(rc))
|
---|
1874 | break;
|
---|
1875 | }
|
---|
1876 | }
|
---|
1877 |
|
---|
1878 | if (pfnProgress)
|
---|
1879 | {
|
---|
1880 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1881 | (uBlock * 100) / cBlocks,
|
---|
1882 | pvUser);
|
---|
1883 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1884 | }
|
---|
1885 | }
|
---|
1886 | }
|
---|
1887 | else
|
---|
1888 | {
|
---|
1889 | /*
|
---|
1890 | * Commit the parent image to the child image.
|
---|
1891 | * Parent is the source (from), child is the target (to).
|
---|
1892 | */
|
---|
1893 |
|
---|
1894 | unsigned cBlocks = getImageBlocks(&pImageFrom->Header);
|
---|
1895 |
|
---|
1896 | for (unsigned uBlock = 0; uBlock < cBlocks; uBlock++)
|
---|
1897 | {
|
---|
1898 | /*
|
---|
1899 | * only process blocks that are allocated or zero in the source image
|
---|
1900 | * and NEITHER allocated NOR zero in the target image
|
---|
1901 | */
|
---|
1902 | if (pImageFrom->paBlocks[uBlock] != VDI_IMAGE_BLOCK_FREE &&
|
---|
1903 | pImageTo->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1904 | {
|
---|
1905 | /* Found used block in source image (but unused in target), commit it. */
|
---|
1906 | if ( pImageFrom->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1907 | {
|
---|
1908 | /* Block is zero in the source image and not allocated in the target image. */
|
---|
1909 | pImageTo->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1910 | vdiSetModifiedFlag(pImageTo);
|
---|
1911 | }
|
---|
1912 | else
|
---|
1913 | {
|
---|
1914 | /* Block is not zero / allocated in source image. */
|
---|
1915 | unsigned cbCommit = getImageBlockSize(&pImageFrom->Header);
|
---|
1916 | unsigned offCommit = 0;
|
---|
1917 |
|
---|
1918 | /* do loop, because buffer size may be less then block size */
|
---|
1919 | while (cbCommit > 0)
|
---|
1920 | {
|
---|
1921 | unsigned cbToCopy = RT_MIN(cbCommit, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1922 |
|
---|
1923 | rc = vdiReadInBlock(pImageFrom, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1924 | if (VBOX_FAILURE(rc))
|
---|
1925 | break;
|
---|
1926 |
|
---|
1927 | rc = vdiWriteInBlock(NULL, pImageTo, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1928 | if (VBOX_FAILURE(rc))
|
---|
1929 | break;
|
---|
1930 |
|
---|
1931 | cbCommit -= cbToCopy;
|
---|
1932 | offCommit += cbToCopy;
|
---|
1933 | }
|
---|
1934 | if (VBOX_FAILURE(rc))
|
---|
1935 | break;
|
---|
1936 | }
|
---|
1937 | }
|
---|
1938 |
|
---|
1939 | if (pfnProgress)
|
---|
1940 | {
|
---|
1941 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1942 | (uBlock * 100) / cBlocks,
|
---|
1943 | pvUser);
|
---|
1944 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1945 | }
|
---|
1946 | }
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | RTMemTmpFree(pvBuf);
|
---|
1950 | return rc;
|
---|
1951 | }
|
---|
1952 |
|
---|
1953 | /**
|
---|
1954 | * internal: commit last image(s) to selected previous image.
|
---|
1955 | * note: all images accessed across this call must be opened in R/W mode.
|
---|
1956 | * @remark Only used by tstVDI.
|
---|
1957 | */
|
---|
1958 | static int vdiCommitToImage(PVDIDISK pDisk, PVDIIMAGEDESC pDstImage,
|
---|
1959 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1960 | {
|
---|
1961 | /* sanity check */
|
---|
1962 | Assert(pDisk);
|
---|
1963 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
1964 | Assert(pDstImage);
|
---|
1965 |
|
---|
1966 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
1967 | Assert(pImage);
|
---|
1968 | Log(("vdiCommitToImage: commiting from image \"%s\" to image \"%s\"\n",
|
---|
1969 | pImage->szFilename, pDstImage->szFilename));
|
---|
1970 | if (pDstImage == pImage)
|
---|
1971 | {
|
---|
1972 | Log(("vdiCommitToImage: attempt to commit to the same image!\n"));
|
---|
1973 | return VERR_VDI_NO_DIFF_IMAGES;
|
---|
1974 | }
|
---|
1975 |
|
---|
1976 | /* Scan images for pDstImage. */
|
---|
1977 | while (pImage && pImage != pDstImage)
|
---|
1978 | pImage = pImage->pPrev;
|
---|
1979 | if (!pImage)
|
---|
1980 | {
|
---|
1981 | AssertMsgFailed(("Invalid arguments: pDstImage is not in images chain\n"));
|
---|
1982 | return VERR_INVALID_PARAMETER;
|
---|
1983 | }
|
---|
1984 | pImage = pDisk->pLast;
|
---|
1985 |
|
---|
1986 | /* alloc tmp buffer */
|
---|
1987 | void *pvBuf = RTMemTmpAlloc(pDisk->cbBuf);
|
---|
1988 | if (!pvBuf)
|
---|
1989 | return VERR_NO_MEMORY;
|
---|
1990 |
|
---|
1991 | int rc = VINF_SUCCESS;
|
---|
1992 | unsigned cBlocks = getImageBlocks(&pImage->Header);
|
---|
1993 |
|
---|
1994 | for (unsigned uBlock = 0; uBlock < cBlocks; uBlock++)
|
---|
1995 | {
|
---|
1996 | pImage = pDisk->pLast;
|
---|
1997 |
|
---|
1998 | /* Find allocated block to commit. */
|
---|
1999 | while ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE
|
---|
2000 | && pImage != pDstImage)
|
---|
2001 | pImage = pImage->pPrev;
|
---|
2002 |
|
---|
2003 | if (pImage != pDstImage)
|
---|
2004 | {
|
---|
2005 | /* Found used block in diff image (pImage), commit it. */
|
---|
2006 | if ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
2007 | && !IS_VDI_IMAGE_BLOCK_ALLOCATED(pDstImage->paBlocks[uBlock]))
|
---|
2008 | {
|
---|
2009 | /* Block is zero in difference image and not allocated in primary image. */
|
---|
2010 | pDstImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
2011 | vdiSetModifiedFlag(pDstImage);
|
---|
2012 | }
|
---|
2013 | else
|
---|
2014 | {
|
---|
2015 | /* Block is not zero / allocated in primary image. */
|
---|
2016 | unsigned cbCommit = getImageBlockSize(&pImage->Header);
|
---|
2017 | unsigned offCommit = 0;
|
---|
2018 |
|
---|
2019 | /* do loop, because buffer size may be less then block size */
|
---|
2020 | while (cbCommit > 0)
|
---|
2021 | {
|
---|
2022 | unsigned cbToCopy = RT_MIN(cbCommit, pDisk->cbBuf);
|
---|
2023 |
|
---|
2024 | rc = vdiReadInBlock(pImage, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
2025 | if (VBOX_FAILURE(rc))
|
---|
2026 | break;
|
---|
2027 |
|
---|
2028 | rc = vdiWriteInBlock(pDisk, pDstImage, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
2029 | if (VBOX_FAILURE(rc))
|
---|
2030 | break;
|
---|
2031 |
|
---|
2032 | cbCommit -= cbToCopy;
|
---|
2033 | offCommit += cbToCopy;
|
---|
2034 | }
|
---|
2035 | if (VBOX_FAILURE(rc))
|
---|
2036 | break;
|
---|
2037 | }
|
---|
2038 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
2039 | }
|
---|
2040 |
|
---|
2041 | if (pfnProgress)
|
---|
2042 | {
|
---|
2043 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
2044 | (uBlock * 100) / cBlocks,
|
---|
2045 | pvUser);
|
---|
2046 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
2047 | }
|
---|
2048 | }
|
---|
2049 |
|
---|
2050 | RTMemTmpFree(pvBuf);
|
---|
2051 |
|
---|
2052 | /* Go forward and update linkage information. */
|
---|
2053 | for (pImage = pDstImage; pImage; pImage = pImage->pNext)
|
---|
2054 | {
|
---|
2055 | /* generate new last-modified uuid. */
|
---|
2056 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
2057 |
|
---|
2058 | /* fix up linkage. */
|
---|
2059 | if (pImage != pDstImage)
|
---|
2060 | *getImageParentModificationUUID(&pImage->Header) = *getImageModificationUUID(&pImage->pPrev->Header);
|
---|
2061 |
|
---|
2062 | /* reset modified flag. */
|
---|
2063 | pImage->fModified = 0;
|
---|
2064 | }
|
---|
2065 |
|
---|
2066 | /* Process committed images - truncate them. */
|
---|
2067 | for (pImage = pDisk->pLast; pImage != pDstImage; pImage = pImage->pPrev)
|
---|
2068 | {
|
---|
2069 | /* note: can't understand how to do error works here? */
|
---|
2070 |
|
---|
2071 | setImageBlocksAllocated(&pImage->Header, 0);
|
---|
2072 |
|
---|
2073 | /* Truncate file. */
|
---|
2074 | int rc2 = RTFileSetSize(pImage->File, pImage->offStartData);
|
---|
2075 | if (VBOX_FAILURE(rc2))
|
---|
2076 | {
|
---|
2077 | rc = rc2;
|
---|
2078 | Log(("vdiCommitToImage: set size (truncate) rc=%Vrc filename=\"%s\"\n",
|
---|
2079 | rc, pImage->szFilename));
|
---|
2080 | }
|
---|
2081 |
|
---|
2082 | /* Save header and blocks array. */
|
---|
2083 | rc2 = vdiUpdateBlocks(pImage);
|
---|
2084 | if (VBOX_FAILURE(rc2))
|
---|
2085 | {
|
---|
2086 | rc = rc2;
|
---|
2087 | Log(("vdiCommitToImage: update blocks and header rc=%Vrc filename=\"%s\"\n",
|
---|
2088 | rc, pImage->szFilename));
|
---|
2089 | }
|
---|
2090 | }
|
---|
2091 |
|
---|
2092 | if (pfnProgress)
|
---|
2093 | {
|
---|
2094 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2095 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
2096 | }
|
---|
2097 |
|
---|
2098 | Log(("vdiCommitToImage: done, rc=%Vrc\n", rc));
|
---|
2099 |
|
---|
2100 | return rc;
|
---|
2101 | }
|
---|
2102 |
|
---|
2103 | /**
|
---|
2104 | * Checks if image is available and not broken, returns some useful image parameters if requested.
|
---|
2105 | *
|
---|
2106 | * @returns VBox status code.
|
---|
2107 | * @param pszFilename Name of the image file to check.
|
---|
2108 | * @param puVersion Where to store the version of image. NULL is ok.
|
---|
2109 | * @param penmType Where to store the type of image. NULL is ok.
|
---|
2110 | * @param pcbSize Where to store the size of image in bytes. NULL is ok.
|
---|
2111 | * @param pUuid Where to store the uuid of image creation. NULL is ok.
|
---|
2112 | * @param pParentUuid Where to store the UUID of the parent image. NULL is ok.
|
---|
2113 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
2114 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
2115 | */
|
---|
2116 | IDER3DECL(int) VDICheckImage(const char *pszFilename, unsigned *puVersion, PVDIIMAGETYPE penmType,
|
---|
2117 | uint64_t *pcbSize, PRTUUID pUuid, PRTUUID pParentUuid,
|
---|
2118 | char *pszComment, unsigned cbComment)
|
---|
2119 | {
|
---|
2120 | LogFlow(("VDICheckImage:\n"));
|
---|
2121 |
|
---|
2122 | /* Check arguments. */
|
---|
2123 | if ( !pszFilename
|
---|
2124 | || *pszFilename == '\0')
|
---|
2125 | {
|
---|
2126 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2127 | return VERR_INVALID_PARAMETER;
|
---|
2128 | }
|
---|
2129 |
|
---|
2130 | PVDIIMAGEDESC pImage;
|
---|
2131 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
2132 | if (VBOX_SUCCESS(rc))
|
---|
2133 | {
|
---|
2134 | Log(("VDICheckImage: filename=\"%s\" version=%08X type=%X cbDisk=%llu uuid={%Vuuid}\n",
|
---|
2135 | pszFilename,
|
---|
2136 | pImage->PreHeader.u32Version,
|
---|
2137 | getImageType(&pImage->Header),
|
---|
2138 | getImageDiskSize(&pImage->Header),
|
---|
2139 | getImageCreationUUID(&pImage->Header)));
|
---|
2140 |
|
---|
2141 | if ( pszComment
|
---|
2142 | && cbComment > 0)
|
---|
2143 | {
|
---|
2144 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
2145 | unsigned cb = strlen(pszTmp);
|
---|
2146 | if (cbComment > cb)
|
---|
2147 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
2148 | else
|
---|
2149 | rc = VERR_BUFFER_OVERFLOW;
|
---|
2150 | }
|
---|
2151 | if (VBOX_SUCCESS(rc))
|
---|
2152 | {
|
---|
2153 | if (puVersion)
|
---|
2154 | *puVersion = pImage->PreHeader.u32Version;
|
---|
2155 | if (penmType)
|
---|
2156 | *penmType = getImageType(&pImage->Header);
|
---|
2157 | if (pcbSize)
|
---|
2158 | *pcbSize = getImageDiskSize(&pImage->Header);
|
---|
2159 | if (pUuid)
|
---|
2160 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
2161 | if (pParentUuid)
|
---|
2162 | *pParentUuid = *getImageParentUUID(&pImage->Header);
|
---|
2163 | }
|
---|
2164 | vdiCloseImage(pImage);
|
---|
2165 | }
|
---|
2166 |
|
---|
2167 | LogFlow(("VDICheckImage: returns %Vrc\n", rc));
|
---|
2168 | return rc;
|
---|
2169 | }
|
---|
2170 |
|
---|
2171 | /**
|
---|
2172 | * Changes an image's comment string.
|
---|
2173 | *
|
---|
2174 | * @returns VBox status code.
|
---|
2175 | * @param pszFilename Name of the image file to operate on.
|
---|
2176 | * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
|
---|
2177 | */
|
---|
2178 | IDER3DECL(int) VDISetImageComment(const char *pszFilename, const char *pszComment)
|
---|
2179 | {
|
---|
2180 | LogFlow(("VDISetImageComment:\n"));
|
---|
2181 |
|
---|
2182 | /*
|
---|
2183 | * Validate arguments.
|
---|
2184 | */
|
---|
2185 | if ( !pszFilename
|
---|
2186 | || *pszFilename == '\0')
|
---|
2187 | {
|
---|
2188 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2189 | return VERR_INVALID_PARAMETER;
|
---|
2190 | }
|
---|
2191 |
|
---|
2192 | const size_t cchComment = pszComment ? strlen(pszComment) : 0;
|
---|
2193 | if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
|
---|
2194 | {
|
---|
2195 | Log(("VDISetImageComment: pszComment is too long, %d bytes!\n", cchComment));
|
---|
2196 | return VERR_VDI_COMMENT_TOO_LONG;
|
---|
2197 | }
|
---|
2198 |
|
---|
2199 | /*
|
---|
2200 | * Open the image for updating.
|
---|
2201 | */
|
---|
2202 | PVDIIMAGEDESC pImage;
|
---|
2203 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2204 | if (VBOX_FAILURE(rc))
|
---|
2205 | {
|
---|
2206 | Log(("VDISetImageComment: vdiOpenImage rc=%Vrc filename=\"%s\"!\n", rc, pszFilename));
|
---|
2207 | return rc;
|
---|
2208 | }
|
---|
2209 | if (!pImage->fReadOnly)
|
---|
2210 | {
|
---|
2211 | /* we don't support old style images */
|
---|
2212 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
2213 | {
|
---|
2214 | /*
|
---|
2215 | * Update the comment field, making sure to zero out all of the previous comment.
|
---|
2216 | */
|
---|
2217 | memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
|
---|
2218 | memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
|
---|
2219 |
|
---|
2220 | /* write out new the header */
|
---|
2221 | rc = vdiUpdateHeader(pImage);
|
---|
2222 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
|
---|
2223 | pImage->szFilename, rc));
|
---|
2224 | }
|
---|
2225 | else
|
---|
2226 | {
|
---|
2227 | Log(("VDISetImageComment: Unsupported version!\n"));
|
---|
2228 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
2229 | }
|
---|
2230 | }
|
---|
2231 | else
|
---|
2232 | {
|
---|
2233 | Log(("VDISetImageComment: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
2234 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
2235 | }
|
---|
2236 |
|
---|
2237 | vdiCloseImage(pImage);
|
---|
2238 | return rc;
|
---|
2239 | }
|
---|
2240 |
|
---|
2241 | /**
|
---|
2242 | * Creates a new base image file.
|
---|
2243 | *
|
---|
2244 | * @returns VBox status code.
|
---|
2245 | * @param pszFilename Name of the creating image file.
|
---|
2246 | * @param enmType Image type, only base image types are acceptable.
|
---|
2247 | * @param cbSize Image size in bytes.
|
---|
2248 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
2249 | * @param pfnProgress Progress callback. Optional.
|
---|
2250 | * @param pvUser User argument for the progress callback.
|
---|
2251 | */
|
---|
2252 | IDER3DECL(int) VDICreateBaseImage(const char *pszFilename, VDIIMAGETYPE enmType, uint64_t cbSize,
|
---|
2253 | const char *pszComment, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
2254 | {
|
---|
2255 | LogFlow(("VDICreateBaseImage:\n"));
|
---|
2256 |
|
---|
2257 | /* Check arguments. */
|
---|
2258 | if ( !pszFilename
|
---|
2259 | || *pszFilename == '\0'
|
---|
2260 | || (enmType != VDI_IMAGE_TYPE_NORMAL && enmType != VDI_IMAGE_TYPE_FIXED)
|
---|
2261 | || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE)
|
---|
2262 | {
|
---|
2263 | AssertMsgFailed(("Invalid arguments: pszFilename=%p enmType=%x cbSize=%llu\n",
|
---|
2264 | pszFilename, enmType, cbSize));
|
---|
2265 | return VERR_INVALID_PARAMETER;
|
---|
2266 | }
|
---|
2267 |
|
---|
2268 | int rc = vdiCreateImage(pszFilename, enmType, VDI_IMAGE_FLAGS_DEFAULT, cbSize, pszComment, NULL,
|
---|
2269 | pfnProgress, pvUser);
|
---|
2270 | LogFlow(("VDICreateBaseImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
2271 | return rc;
|
---|
2272 | }
|
---|
2273 |
|
---|
2274 | /**
|
---|
2275 | * Creates a differencing dynamically growing image file for specified parent image.
|
---|
2276 | *
|
---|
2277 | * @returns VBox status code.
|
---|
2278 | * @param pszFilename Name of the creating differencing image file.
|
---|
2279 | * @param pszParent Name of the parent image file. May be base or diff image type.
|
---|
2280 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
2281 | * @param pfnProgress Progress callback. Optional.
|
---|
2282 | * @param pvUser User argument for the progress callback.
|
---|
2283 | */
|
---|
2284 | IDER3DECL(int) VDICreateDifferenceImage(const char *pszFilename, const char *pszParent,
|
---|
2285 | const char *pszComment, PFNVMPROGRESS pfnProgress,
|
---|
2286 | void *pvUser)
|
---|
2287 | {
|
---|
2288 | LogFlow(("VDICreateDifferenceImage:\n"));
|
---|
2289 |
|
---|
2290 | /* Check arguments. */
|
---|
2291 | if ( !pszFilename
|
---|
2292 | || *pszFilename == '\0'
|
---|
2293 | || !pszParent
|
---|
2294 | || *pszParent == '\0')
|
---|
2295 | {
|
---|
2296 | AssertMsgFailed(("Invalid arguments: pszFilename=%p pszParent=%p\n",
|
---|
2297 | pszFilename, pszParent));
|
---|
2298 | return VERR_INVALID_PARAMETER;
|
---|
2299 | }
|
---|
2300 |
|
---|
2301 | PVDIIMAGEDESC pParent;
|
---|
2302 | int rc = vdiOpenImage(&pParent, pszParent, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
2303 | if (VBOX_SUCCESS(rc))
|
---|
2304 | {
|
---|
2305 | rc = vdiCreateImage(pszFilename, VDI_IMAGE_TYPE_DIFF, VDI_IMAGE_FLAGS_DEFAULT,
|
---|
2306 | getImageDiskSize(&pParent->Header), pszComment, pParent,
|
---|
2307 | pfnProgress, pvUser);
|
---|
2308 | vdiCloseImage(pParent);
|
---|
2309 | }
|
---|
2310 | LogFlow(("VDICreateDifferenceImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
2311 | return rc;
|
---|
2312 | }
|
---|
2313 |
|
---|
2314 | /**
|
---|
2315 | * Deletes an image. Only valid image files can be deleted by this call.
|
---|
2316 | *
|
---|
2317 | * @returns VBox status code.
|
---|
2318 | * @param pszFilename Name of the image file to check.
|
---|
2319 | */
|
---|
2320 | IDER3DECL(int) VDIDeleteImage(const char *pszFilename)
|
---|
2321 | {
|
---|
2322 | LogFlow(("VDIDeleteImage:\n"));
|
---|
2323 | /* Check arguments. */
|
---|
2324 | if ( !pszFilename
|
---|
2325 | || *pszFilename == '\0')
|
---|
2326 | {
|
---|
2327 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2328 | return VERR_INVALID_PARAMETER;
|
---|
2329 | }
|
---|
2330 |
|
---|
2331 | int rc = VDICheckImage(pszFilename, NULL, NULL, NULL, NULL, NULL, NULL, 0);
|
---|
2332 | if (VBOX_SUCCESS(rc))
|
---|
2333 | rc = RTFileDelete(pszFilename);
|
---|
2334 |
|
---|
2335 | LogFlow(("VDIDeleteImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
2336 | return rc;
|
---|
2337 | }
|
---|
2338 |
|
---|
2339 | /**
|
---|
2340 | * Makes a copy of image file with a new (other) creation uuid.
|
---|
2341 | *
|
---|
2342 | * @returns VBox status code.
|
---|
2343 | * @param pszDstFilename Name of the image file to create.
|
---|
2344 | * @param pszSrcFilename Name of the image file to copy from.
|
---|
2345 | * @param pszComment Pointer to image comment. If NULL specified comment
|
---|
2346 | * will be copied from source image.
|
---|
2347 | * @param pfnProgress Progress callback. Optional.
|
---|
2348 | * @param pvUser User argument for the progress callback.
|
---|
2349 | */
|
---|
2350 | IDER3DECL(int) VDICopyImage(const char *pszDstFilename, const char *pszSrcFilename,
|
---|
2351 | const char *pszComment, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
2352 | {
|
---|
2353 | LogFlow(("VDICopyImage:\n"));
|
---|
2354 |
|
---|
2355 | /* Check arguments. */
|
---|
2356 | if ( !pszDstFilename
|
---|
2357 | || *pszDstFilename == '\0'
|
---|
2358 | || !pszSrcFilename
|
---|
2359 | || *pszSrcFilename == '\0')
|
---|
2360 | {
|
---|
2361 | AssertMsgFailed(("Invalid arguments: pszDstFilename=%p pszSrcFilename=%p\n",
|
---|
2362 | pszDstFilename, pszSrcFilename));
|
---|
2363 | return VERR_INVALID_PARAMETER;
|
---|
2364 | }
|
---|
2365 |
|
---|
2366 | /* Special check for comment length. */
|
---|
2367 | if ( pszComment
|
---|
2368 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
2369 | {
|
---|
2370 | Log(("VDICopyImage: pszComment is too long, cb=%d\n", strlen(pszComment)));
|
---|
2371 | return VERR_VDI_COMMENT_TOO_LONG;
|
---|
2372 | }
|
---|
2373 |
|
---|
2374 | PVDIIMAGEDESC pImage;
|
---|
2375 | int rc = vdiOpenImage(&pImage, pszSrcFilename, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
2376 | if (VBOX_FAILURE(rc))
|
---|
2377 | {
|
---|
2378 | Log(("VDICopyImage: src image \"%s\" open failed rc=%Vrc\n", pszSrcFilename, rc));
|
---|
2379 | return rc;
|
---|
2380 | }
|
---|
2381 |
|
---|
2382 | uint64_t cbFile = pImage->offStartData
|
---|
2383 | + ((uint64_t)getImageBlocksAllocated(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
2384 |
|
---|
2385 | /* create file */
|
---|
2386 | RTFILE File;
|
---|
2387 | rc = RTFileOpen(&File,
|
---|
2388 | pszDstFilename,
|
---|
2389 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
|
---|
2390 | if (VBOX_SUCCESS(rc))
|
---|
2391 | {
|
---|
2392 | /* lock new image exclusively to close any wrong access by VDI API calls. */
|
---|
2393 | rc = RTFileLock(File, RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, cbFile);
|
---|
2394 | if (VBOX_SUCCESS(rc))
|
---|
2395 | {
|
---|
2396 | /* Set the size of a new file. */
|
---|
2397 | rc = RTFileSetSize(File, cbFile);
|
---|
2398 | if (VBOX_SUCCESS(rc))
|
---|
2399 | {
|
---|
2400 | /* A dirty trick - use original image data to fill the new image. */
|
---|
2401 | RTFILE oldFileHandle = pImage->File;
|
---|
2402 | pImage->File = File;
|
---|
2403 | pImage->fReadOnly = false;
|
---|
2404 |
|
---|
2405 | /* generate a new image creation uuid. */
|
---|
2406 | RTUuidCreate(getImageCreationUUID(&pImage->Header));
|
---|
2407 | /* generate a new image last-modified uuid. */
|
---|
2408 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
2409 | /* set image comment, if present. */
|
---|
2410 | if (pszComment)
|
---|
2411 | strncpy(getImageComment(&pImage->Header), pszComment, VDI_IMAGE_COMMENT_SIZE);
|
---|
2412 |
|
---|
2413 | /* Write the pre-header to new image. */
|
---|
2414 | rc = RTFileSeek(pImage->File, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
2415 | if (VBOX_SUCCESS(rc))
|
---|
2416 | rc = RTFileWrite(pImage->File,
|
---|
2417 | &pImage->PreHeader,
|
---|
2418 | sizeof(pImage->PreHeader),
|
---|
2419 | NULL);
|
---|
2420 |
|
---|
2421 | /* Write the header and the blocks array to new image. */
|
---|
2422 | if (VBOX_SUCCESS(rc))
|
---|
2423 | rc = vdiUpdateBlocks(pImage);
|
---|
2424 |
|
---|
2425 | pImage->File = oldFileHandle;
|
---|
2426 | pImage->fReadOnly = true;
|
---|
2427 |
|
---|
2428 | /* Seek to the data start in both images. */
|
---|
2429 | if (VBOX_SUCCESS(rc))
|
---|
2430 | rc = RTFileSeek(pImage->File,
|
---|
2431 | pImage->offStartData,
|
---|
2432 | RTFILE_SEEK_BEGIN,
|
---|
2433 | NULL);
|
---|
2434 | if (VBOX_SUCCESS(rc))
|
---|
2435 | rc = RTFileSeek(File,
|
---|
2436 | pImage->offStartData,
|
---|
2437 | RTFILE_SEEK_BEGIN,
|
---|
2438 | NULL);
|
---|
2439 |
|
---|
2440 | if (VBOX_SUCCESS(rc))
|
---|
2441 | {
|
---|
2442 | /* alloc tmp buffer */
|
---|
2443 | void *pvBuf = RTMemTmpAlloc(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2444 | if (pvBuf)
|
---|
2445 | {
|
---|
2446 | /* Main copy loop. */
|
---|
2447 | uint64_t cbData = cbFile - pImage->offStartData;
|
---|
2448 | unsigned cBlocks = (unsigned)(cbData / VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2449 | unsigned c = 0;
|
---|
2450 |
|
---|
2451 | while (cbData)
|
---|
2452 | {
|
---|
2453 | unsigned cbToCopy = (unsigned)RT_MIN(cbData, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2454 |
|
---|
2455 | /* Read. */
|
---|
2456 | rc = RTFileRead(pImage->File, pvBuf, cbToCopy, NULL);
|
---|
2457 | if (VBOX_FAILURE(rc))
|
---|
2458 | break;
|
---|
2459 |
|
---|
2460 | /* Write. */
|
---|
2461 | rc = RTFileWrite(File, pvBuf, cbToCopy, NULL);
|
---|
2462 | if (VBOX_FAILURE(rc))
|
---|
2463 | break;
|
---|
2464 |
|
---|
2465 | if (pfnProgress)
|
---|
2466 | {
|
---|
2467 | c++;
|
---|
2468 | rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
2469 | (c * 100) / cBlocks,
|
---|
2470 | pvUser);
|
---|
2471 | if (VBOX_FAILURE(rc))
|
---|
2472 | break;
|
---|
2473 | }
|
---|
2474 | cbData -= cbToCopy;
|
---|
2475 | }
|
---|
2476 |
|
---|
2477 | RTMemTmpFree(pvBuf);
|
---|
2478 | }
|
---|
2479 | else
|
---|
2480 | rc = VERR_NO_MEMORY;
|
---|
2481 | }
|
---|
2482 | }
|
---|
2483 |
|
---|
2484 | RTFileUnlock(File, 0, cbFile);
|
---|
2485 | }
|
---|
2486 |
|
---|
2487 | RTFileClose(File);
|
---|
2488 |
|
---|
2489 | if (VBOX_FAILURE(rc))
|
---|
2490 | RTFileDelete(pszDstFilename);
|
---|
2491 |
|
---|
2492 | if (pfnProgress)
|
---|
2493 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2494 | }
|
---|
2495 |
|
---|
2496 | vdiCloseImage(pImage);
|
---|
2497 |
|
---|
2498 | LogFlow(("VDICopyImage: returns %Vrc for pszSrcFilename=\"%s\" pszDstFilename=\"%s\"\n",
|
---|
2499 | rc, pszSrcFilename, pszDstFilename));
|
---|
2500 | return rc;
|
---|
2501 | }
|
---|
2502 |
|
---|
2503 | /**
|
---|
2504 | * Shrinks growing image file by removing zeroed data blocks.
|
---|
2505 | *
|
---|
2506 | * @returns VBox status code.
|
---|
2507 | * @param pszFilename Name of the image file to shrink.
|
---|
2508 | * @param pfnProgress Progress callback. Optional.
|
---|
2509 | * @param pvUser User argument for the progress callback.
|
---|
2510 | */
|
---|
2511 | IDER3DECL(int) VDIShrinkImage(const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
2512 | {
|
---|
2513 | LogFlow(("VDIShrinkImage:\n"));
|
---|
2514 |
|
---|
2515 | /* Check arguments. */
|
---|
2516 | if ( !pszFilename
|
---|
2517 | || *pszFilename == '\0')
|
---|
2518 | {
|
---|
2519 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2520 | return VERR_INVALID_PARAMETER;
|
---|
2521 | }
|
---|
2522 |
|
---|
2523 | PVDIIMAGEDESC pImage;
|
---|
2524 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2525 | if (VBOX_FAILURE(rc))
|
---|
2526 | {
|
---|
2527 | Log(("VDIShrinkImage: vdiOpenImage rc=%Vrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2528 | return rc;
|
---|
2529 | }
|
---|
2530 | if (pImage->fReadOnly)
|
---|
2531 | {
|
---|
2532 | Log(("VDIShrinkImage: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
2533 | vdiCloseImage(pImage);
|
---|
2534 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
2535 | }
|
---|
2536 |
|
---|
2537 | /* Do debug dump. */
|
---|
2538 | vdiDumpImage(pImage);
|
---|
2539 |
|
---|
2540 | /* Working data. */
|
---|
2541 | unsigned cbBlock = getImageBlockSize(&pImage->Header);
|
---|
2542 | unsigned cBlocks = getImageBlocks(&pImage->Header);
|
---|
2543 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
2544 |
|
---|
2545 | uint64_t cbFile;
|
---|
2546 | rc = RTFileGetSize(pImage->File, &cbFile);
|
---|
2547 | if (VBOX_FAILURE(rc))
|
---|
2548 | {
|
---|
2549 | Log(("VDIShrinkImage: RTFileGetSize rc=%Vrc for file=\"%s\"\n", rc, pszFilename));
|
---|
2550 | vdiCloseImage(pImage);
|
---|
2551 | return rc;
|
---|
2552 | }
|
---|
2553 |
|
---|
2554 | uint64_t cbData = cbFile - pImage->offStartData;
|
---|
2555 | unsigned cBlocksAllocated2 = (unsigned)(cbData >> pImage->uShiftIndex2Offset);
|
---|
2556 | if (cbData != (uint64_t)cBlocksAllocated << pImage->uShiftIndex2Offset)
|
---|
2557 | Log(("VDIShrinkImage: invalid image file length, cbBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2558 | cbBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2559 |
|
---|
2560 | /* Allocate second blocks array for back resolving. */
|
---|
2561 | PVDIIMAGEBLOCKPOINTER paBlocks2 =
|
---|
2562 | (PVDIIMAGEBLOCKPOINTER)RTMemTmpAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * cBlocks);
|
---|
2563 | if (!paBlocks2)
|
---|
2564 | {
|
---|
2565 | Log(("VDIShrinkImage: failed to allocate paBlocks2 buffer (%u bytes)\n", sizeof(VDIIMAGEBLOCKPOINTER) * cBlocks));
|
---|
2566 | vdiCloseImage(pImage);
|
---|
2567 | return VERR_NO_MEMORY;
|
---|
2568 | }
|
---|
2569 |
|
---|
2570 | /* Init second blocks array. */
|
---|
2571 | for (unsigned n = 0; n < cBlocks; n++)
|
---|
2572 | paBlocks2[n] = VDI_IMAGE_BLOCK_FREE;
|
---|
2573 |
|
---|
2574 | /* Fill second blocks array, check for allocational errors. */
|
---|
2575 | for (unsigned n = 0; n < cBlocks; n++)
|
---|
2576 | {
|
---|
2577 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[n]))
|
---|
2578 | {
|
---|
2579 | unsigned uBlock = pImage->paBlocks[n];
|
---|
2580 | if (uBlock < cBlocksAllocated2)
|
---|
2581 | {
|
---|
2582 | if (paBlocks2[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
2583 | paBlocks2[uBlock] = n;
|
---|
2584 | else
|
---|
2585 | {
|
---|
2586 | Log(("VDIShrinkImage: block n=%u -> uBlock=%u is already in use!\n", n, uBlock));
|
---|
2587 | /* free second link to block. */
|
---|
2588 | pImage->paBlocks[n] = VDI_IMAGE_BLOCK_FREE;
|
---|
2589 | }
|
---|
2590 | }
|
---|
2591 | else
|
---|
2592 | {
|
---|
2593 | Log(("VDIShrinkImage: block n=%u -> uBlock=%u is out of blocks range! (cbBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu)\n",
|
---|
2594 | n, uBlock, cbBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2595 | /* free link to invalid block. */
|
---|
2596 | pImage->paBlocks[n] = VDI_IMAGE_BLOCK_FREE;
|
---|
2597 | }
|
---|
2598 | }
|
---|
2599 | }
|
---|
2600 |
|
---|
2601 | /* Allocate a working buffer for one block. */
|
---|
2602 | void *pvBuf = RTMemTmpAlloc(cbBlock);
|
---|
2603 | if (pvBuf)
|
---|
2604 | {
|
---|
2605 | /* Main voodoo loop, search holes and fill it. */
|
---|
2606 | unsigned uBlockWrite = 0;
|
---|
2607 | for (unsigned uBlock = 0; uBlock < cBlocksAllocated2; uBlock++)
|
---|
2608 | {
|
---|
2609 | if (paBlocks2[uBlock] != VDI_IMAGE_BLOCK_FREE)
|
---|
2610 | {
|
---|
2611 | /* Read the block from file and check for zeroes. */
|
---|
2612 | uint64_t u64Offset = ((uint64_t)uBlock << pImage->uShiftIndex2Offset)
|
---|
2613 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2614 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
2615 | if (VBOX_FAILURE(rc))
|
---|
2616 | {
|
---|
2617 | Log(("VDIShrinkImage: seek rc=%Vrc filename=\"%s\" uBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2618 | rc, pImage->szFilename, uBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2619 | break;
|
---|
2620 | }
|
---|
2621 | rc = RTFileRead(pImage->File, pvBuf, cbBlock, NULL);
|
---|
2622 | if (VBOX_FAILURE(rc))
|
---|
2623 | {
|
---|
2624 | Log(("VDIShrinkImage: read rc=%Vrc filename=\"%s\" cbBlock=%u uBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2625 | rc, pImage->szFilename, cbBlock, uBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2626 | break;
|
---|
2627 | }
|
---|
2628 |
|
---|
2629 | /* Check block for data. */
|
---|
2630 | bool fBlockZeroed = true; /* Block is zeroed flag. */
|
---|
2631 | for (unsigned i = 0; i < (cbBlock >> 2); i++)
|
---|
2632 | if (((uint32_t *)pvBuf)[i] != 0)
|
---|
2633 | {
|
---|
2634 | /* Block is not zeroed! */
|
---|
2635 | fBlockZeroed = false;
|
---|
2636 | break;
|
---|
2637 | }
|
---|
2638 |
|
---|
2639 | if (!fBlockZeroed)
|
---|
2640 | {
|
---|
2641 | /* Block has a data, may be it must be moved. */
|
---|
2642 | if (uBlockWrite < uBlock)
|
---|
2643 | {
|
---|
2644 | /* Move the block. */
|
---|
2645 | u64Offset = ((uint64_t)uBlockWrite << pImage->uShiftIndex2Offset)
|
---|
2646 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2647 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
2648 | if (VBOX_FAILURE(rc))
|
---|
2649 | {
|
---|
2650 | Log(("VDIShrinkImage: seek(2) rc=%Vrc filename=\"%s\" uBlockWrite=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2651 | rc, pImage->szFilename, uBlockWrite, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2652 | break;
|
---|
2653 | }
|
---|
2654 | rc = RTFileWrite(pImage->File, pvBuf, cbBlock, NULL);
|
---|
2655 | if (VBOX_FAILURE(rc))
|
---|
2656 | {
|
---|
2657 | Log(("VDIShrinkImage: write rc=%Vrc filename=\"%s\" cbBlock=%u uBlockWrite=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2658 | rc, pImage->szFilename, cbBlock, uBlockWrite, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2659 | break;
|
---|
2660 | }
|
---|
2661 | }
|
---|
2662 | /* Fix the block pointer. */
|
---|
2663 | pImage->paBlocks[paBlocks2[uBlock]] = uBlockWrite;
|
---|
2664 | uBlockWrite++;
|
---|
2665 | }
|
---|
2666 | else
|
---|
2667 | {
|
---|
2668 | Log(("VDIShrinkImage: found a zeroed block, uBlock=%u\n", uBlock));
|
---|
2669 |
|
---|
2670 | /* Fix the block pointer. */
|
---|
2671 | pImage->paBlocks[paBlocks2[uBlock]] = VDI_IMAGE_BLOCK_ZERO;
|
---|
2672 | }
|
---|
2673 | }
|
---|
2674 | else
|
---|
2675 | Log(("VDIShrinkImage: found an unused block, uBlock=%u\n", uBlock));
|
---|
2676 |
|
---|
2677 | if (pfnProgress)
|
---|
2678 | {
|
---|
2679 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
2680 | (uBlock * 100) / cBlocksAllocated2,
|
---|
2681 | pvUser);
|
---|
2682 | /* Shrink is unbreakable operation! */
|
---|
2683 | }
|
---|
2684 | }
|
---|
2685 |
|
---|
2686 | RTMemTmpFree(pvBuf);
|
---|
2687 |
|
---|
2688 | if ( VBOX_SUCCESS(rc)
|
---|
2689 | && uBlockWrite < cBlocksAllocated2)
|
---|
2690 | {
|
---|
2691 | /* File size must be shrinked. */
|
---|
2692 | Log(("VDIShrinkImage: shrinking file size from %llu to %llu bytes\n",
|
---|
2693 | cbFile,
|
---|
2694 | pImage->offStartData + ((uint64_t)uBlockWrite << pImage->uShiftIndex2Offset)));
|
---|
2695 | rc = RTFileSetSize(pImage->File,
|
---|
2696 | pImage->offStartData + ((uint64_t)uBlockWrite << pImage->uShiftIndex2Offset));
|
---|
2697 | if (VBOX_FAILURE(rc))
|
---|
2698 | Log(("VDIShrinkImage: RTFileSetSize rc=%Vrc\n", rc));
|
---|
2699 | }
|
---|
2700 | cBlocksAllocated2 = uBlockWrite;
|
---|
2701 | }
|
---|
2702 | else
|
---|
2703 | {
|
---|
2704 | Log(("VDIShrinkImage: failed to allocate working buffer (%u bytes)\n", cbBlock));
|
---|
2705 | rc = VERR_NO_MEMORY;
|
---|
2706 | }
|
---|
2707 |
|
---|
2708 | /* Save header and blocks array. */
|
---|
2709 | if (VBOX_SUCCESS(rc))
|
---|
2710 | {
|
---|
2711 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated2);
|
---|
2712 | rc = vdiUpdateBlocks(pImage);
|
---|
2713 | if (pfnProgress)
|
---|
2714 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2715 | }
|
---|
2716 |
|
---|
2717 | /* Do debug dump. */
|
---|
2718 | vdiDumpImage(pImage);
|
---|
2719 |
|
---|
2720 | /* Clean up. */
|
---|
2721 | RTMemTmpFree(paBlocks2);
|
---|
2722 | vdiCloseImage(pImage);
|
---|
2723 |
|
---|
2724 | LogFlow(("VDIShrinkImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
2725 | return rc;
|
---|
2726 | }
|
---|
2727 |
|
---|
2728 | /**
|
---|
2729 | * Converts image file from older VDI formats to current one.
|
---|
2730 | *
|
---|
2731 | * @returns VBox status code.
|
---|
2732 | * @param pszFilename Name of the image file to convert.
|
---|
2733 | * @param pfnProgress Progress callback. Optional.
|
---|
2734 | * @param pvUser User argument for the progress callback.
|
---|
2735 | * @remark Only used by vditool
|
---|
2736 | */
|
---|
2737 | IDER3DECL(int) VDIConvertImage(const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
2738 | {
|
---|
2739 | LogFlow(("VDIConvertImage:\n"));
|
---|
2740 |
|
---|
2741 | /* Check arguments. */
|
---|
2742 | if ( !pszFilename
|
---|
2743 | || *pszFilename == '\0')
|
---|
2744 | {
|
---|
2745 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2746 | return VERR_INVALID_PARAMETER;
|
---|
2747 | }
|
---|
2748 |
|
---|
2749 | PVDIIMAGEDESC pImage;
|
---|
2750 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2751 | if (VBOX_FAILURE(rc))
|
---|
2752 | {
|
---|
2753 | Log(("VDIConvertImage: vdiOpenImage rc=%Vrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2754 | return rc;
|
---|
2755 | }
|
---|
2756 |
|
---|
2757 | VDIHEADER Header = {0};
|
---|
2758 | int off;
|
---|
2759 | uint64_t cbFile;
|
---|
2760 | uint64_t cbData;
|
---|
2761 |
|
---|
2762 | if (pImage->fReadOnly)
|
---|
2763 | {
|
---|
2764 | Log(("VDIConvertImage: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
2765 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
2766 | goto l_conversion_failed;
|
---|
2767 | }
|
---|
2768 |
|
---|
2769 | if (pImage->PreHeader.u32Version != 0x00000002)
|
---|
2770 | {
|
---|
2771 | Log(("VDIConvertImage: unsupported version=%08X filename=\"%s\"\n",
|
---|
2772 | pImage->PreHeader.u32Version, pszFilename));
|
---|
2773 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
2774 | goto l_conversion_failed;
|
---|
2775 | }
|
---|
2776 |
|
---|
2777 | /* Build new version header from old one. */
|
---|
2778 | vdiInitHeader(&Header,
|
---|
2779 | getImageType(&pImage->Header),
|
---|
2780 | VDI_IMAGE_FLAGS_DEFAULT, /* Safety issue: Always use default flags. */
|
---|
2781 | getImageComment(&pImage->Header),
|
---|
2782 | getImageDiskSize(&pImage->Header),
|
---|
2783 | getImageBlockSize(&pImage->Header),
|
---|
2784 | 0);
|
---|
2785 | setImageBlocksAllocated(&Header, getImageBlocksAllocated(&pImage->Header));
|
---|
2786 | *getImageGeometry(&Header) = *getImageGeometry(&pImage->Header);
|
---|
2787 | setImageTranslation(&Header, getImageTranslation(&pImage->Header));
|
---|
2788 | *getImageCreationUUID(&Header) = *getImageCreationUUID(&pImage->Header);
|
---|
2789 | *getImageModificationUUID(&Header) = *getImageModificationUUID(&pImage->Header);
|
---|
2790 |
|
---|
2791 | /* Calc data offset. */
|
---|
2792 | off = getImageDataOffset(&Header) - getImageDataOffset(&pImage->Header);
|
---|
2793 | if (off <= 0)
|
---|
2794 | {
|
---|
2795 | rc = VERR_VDI_INVALID_HEADER;
|
---|
2796 | goto l_conversion_failed;
|
---|
2797 | }
|
---|
2798 |
|
---|
2799 | rc = RTFileGetSize(pImage->File, &cbFile);
|
---|
2800 | if (VBOX_FAILURE(rc))
|
---|
2801 | goto l_conversion_failed;
|
---|
2802 |
|
---|
2803 | /* Check file size. */
|
---|
2804 | cbData = cbFile - getImageDataOffset(&pImage->Header);
|
---|
2805 | if (cbData != (uint64_t)getImageBlocksAllocated(&pImage->Header) << pImage->uShiftIndex2Offset)
|
---|
2806 | {
|
---|
2807 | AssertMsgFailed(("Invalid file size, broken image?\n"));
|
---|
2808 | rc = VERR_VDI_INVALID_HEADER;
|
---|
2809 | goto l_conversion_failed;
|
---|
2810 | }
|
---|
2811 |
|
---|
2812 | /* Expand file. */
|
---|
2813 | rc = RTFileSetSize(pImage->File, cbFile + off);
|
---|
2814 | if (VBOX_FAILURE(rc))
|
---|
2815 | goto l_conversion_failed;
|
---|
2816 |
|
---|
2817 | if (cbData > 0)
|
---|
2818 | {
|
---|
2819 | /* Calc current file position to move data from. */
|
---|
2820 | uint64_t offFile;
|
---|
2821 | if (cbData > VDIDISK_DEFAULT_BUFFER_SIZE)
|
---|
2822 | offFile = cbFile - VDIDISK_DEFAULT_BUFFER_SIZE;
|
---|
2823 | else
|
---|
2824 | offFile = getImageDataOffset(&pImage->Header);
|
---|
2825 |
|
---|
2826 | unsigned cMoves = (unsigned)(cbData / VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2827 | unsigned c = 0;
|
---|
2828 |
|
---|
2829 | /* alloc tmp buffer */
|
---|
2830 | void *pvBuf = RTMemTmpAlloc(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2831 | if (pvBuf)
|
---|
2832 | {
|
---|
2833 | /* Move data. */
|
---|
2834 | for (;;)
|
---|
2835 | {
|
---|
2836 | unsigned cbToMove = (unsigned)RT_MIN(cbData, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2837 |
|
---|
2838 | /* Read. */
|
---|
2839 | rc = RTFileSeek(pImage->File, offFile, RTFILE_SEEK_BEGIN, NULL);
|
---|
2840 | if (VBOX_FAILURE(rc))
|
---|
2841 | break;
|
---|
2842 | rc = RTFileRead(pImage->File, pvBuf, cbToMove, NULL);
|
---|
2843 | if (VBOX_FAILURE(rc))
|
---|
2844 | break;
|
---|
2845 |
|
---|
2846 | /* Write. */
|
---|
2847 | rc = RTFileSeek(pImage->File, offFile + off, RTFILE_SEEK_BEGIN, NULL);
|
---|
2848 | if (VBOX_FAILURE(rc))
|
---|
2849 | break;
|
---|
2850 | rc = RTFileWrite(pImage->File, pvBuf, cbToMove, NULL);
|
---|
2851 | if (VBOX_FAILURE(rc))
|
---|
2852 | break;
|
---|
2853 |
|
---|
2854 | if (pfnProgress)
|
---|
2855 | {
|
---|
2856 | c++;
|
---|
2857 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
2858 | (c * 100) / cMoves,
|
---|
2859 | pvUser);
|
---|
2860 | /* Note: conversion is non breakable operation, skipping rc here. */
|
---|
2861 | }
|
---|
2862 |
|
---|
2863 | cbData -= cbToMove;
|
---|
2864 | if (cbData == 0)
|
---|
2865 | break;
|
---|
2866 |
|
---|
2867 | if (cbData > VDIDISK_DEFAULT_BUFFER_SIZE)
|
---|
2868 | offFile -= VDIDISK_DEFAULT_BUFFER_SIZE;
|
---|
2869 | else
|
---|
2870 | offFile = getImageDataOffset(&pImage->Header);
|
---|
2871 | }
|
---|
2872 |
|
---|
2873 | /* Fill the beginning of file with zeroes to wipe out old headers etc. */
|
---|
2874 | if (VBOX_SUCCESS(rc))
|
---|
2875 | {
|
---|
2876 | Assert(offFile + off <= VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2877 | rc = RTFileSeek(pImage->File, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
2878 | if (VBOX_SUCCESS(rc))
|
---|
2879 | {
|
---|
2880 | memset(pvBuf, 0, (unsigned)offFile + off);
|
---|
2881 | rc = RTFileWrite(pImage->File, pvBuf, (unsigned)offFile + off, NULL);
|
---|
2882 | }
|
---|
2883 | }
|
---|
2884 |
|
---|
2885 | RTMemTmpFree(pvBuf);
|
---|
2886 | }
|
---|
2887 | else
|
---|
2888 | rc = VERR_NO_MEMORY;
|
---|
2889 |
|
---|
2890 | if (VBOX_FAILURE(rc))
|
---|
2891 | goto l_conversion_failed;
|
---|
2892 | }
|
---|
2893 |
|
---|
2894 | if (pfnProgress)
|
---|
2895 | {
|
---|
2896 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2897 | /* Note: conversion is non breakable operation, skipping rc here. */
|
---|
2898 | }
|
---|
2899 |
|
---|
2900 | /* Data moved, now we need to save new pre header, header and blocks array. */
|
---|
2901 |
|
---|
2902 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
2903 | pImage->Header = Header;
|
---|
2904 |
|
---|
2905 | /* Setup image parameters by header. */
|
---|
2906 | vdiSetupImageDesc(pImage);
|
---|
2907 |
|
---|
2908 | /* Write pre-header. */
|
---|
2909 | rc = RTFileSeek(pImage->File, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
2910 | if (VBOX_FAILURE(rc))
|
---|
2911 | goto l_conversion_failed;
|
---|
2912 | rc = RTFileWrite(pImage->File, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
2913 | if (VBOX_FAILURE(rc))
|
---|
2914 | goto l_conversion_failed;
|
---|
2915 |
|
---|
2916 | /* Write header and blocks array. */
|
---|
2917 | rc = vdiUpdateBlocks(pImage);
|
---|
2918 |
|
---|
2919 | l_conversion_failed:
|
---|
2920 | vdiCloseImage(pImage);
|
---|
2921 |
|
---|
2922 | LogFlow(("VDIConvertImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
2923 | return rc;
|
---|
2924 | }
|
---|
2925 |
|
---|
2926 | /**
|
---|
2927 | * Queries the image's UUID and parent UUIDs.
|
---|
2928 | *
|
---|
2929 | * @returns VBox status code.
|
---|
2930 | * @param pszFilename Name of the image file to operate on.
|
---|
2931 | * @param pUuid Where to store image UUID (can be NULL).
|
---|
2932 | * @param pModificationUuid Where to store modification UUID (can be NULL).
|
---|
2933 | * @param pParentUuuid Where to store parent UUID (can be NULL).
|
---|
2934 | * @param pParentModificationUuid Where to store parent modification UUID (can be NULL).
|
---|
2935 | */
|
---|
2936 | IDER3DECL(int) VDIGetImageUUIDs(const char *pszFilename,
|
---|
2937 | PRTUUID pUuid, PRTUUID pModificationUuid,
|
---|
2938 | PRTUUID pParentUuid, PRTUUID pParentModificationUuid)
|
---|
2939 | {
|
---|
2940 | LogFlow(("VDIGetImageUUIDs:\n"));
|
---|
2941 |
|
---|
2942 | /* Check arguments. */
|
---|
2943 | if ( !pszFilename
|
---|
2944 | || *pszFilename == '\0')
|
---|
2945 | {
|
---|
2946 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2947 | return VERR_INVALID_PARAMETER;
|
---|
2948 | }
|
---|
2949 |
|
---|
2950 | /*
|
---|
2951 | * Try open the specified image.
|
---|
2952 | */
|
---|
2953 | PVDIIMAGEDESC pImage;
|
---|
2954 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2955 | if (VBOX_FAILURE(rc))
|
---|
2956 | {
|
---|
2957 | Log(("VDIGetImageUUIDs: vdiOpenImage rc=%Vrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2958 | return rc;
|
---|
2959 | }
|
---|
2960 |
|
---|
2961 | /*
|
---|
2962 | * Query data.
|
---|
2963 | */
|
---|
2964 | if (pUuid)
|
---|
2965 | {
|
---|
2966 | PCRTUUID pTmpUuid = getImageCreationUUID(&pImage->Header);
|
---|
2967 | if (pTmpUuid)
|
---|
2968 | *pUuid = *pTmpUuid;
|
---|
2969 | else
|
---|
2970 | RTUuidClear(pUuid);
|
---|
2971 | }
|
---|
2972 | if (pModificationUuid)
|
---|
2973 | {
|
---|
2974 | PCRTUUID pTmpUuid = getImageModificationUUID(&pImage->Header);
|
---|
2975 | if (pTmpUuid)
|
---|
2976 | *pModificationUuid = *pTmpUuid;
|
---|
2977 | else
|
---|
2978 | RTUuidClear(pModificationUuid);
|
---|
2979 | }
|
---|
2980 | if (pParentUuid)
|
---|
2981 | {
|
---|
2982 | PCRTUUID pTmpUuid = getImageParentUUID(&pImage->Header);
|
---|
2983 | if (pTmpUuid)
|
---|
2984 | *pParentUuid = *pTmpUuid;
|
---|
2985 | else
|
---|
2986 | RTUuidClear(pParentUuid);
|
---|
2987 | }
|
---|
2988 | if (pParentModificationUuid)
|
---|
2989 | {
|
---|
2990 | PCRTUUID pTmpUuid = getImageParentModificationUUID(&pImage->Header);
|
---|
2991 | if (pTmpUuid)
|
---|
2992 | *pParentModificationUuid = *pTmpUuid;
|
---|
2993 | else
|
---|
2994 | RTUuidClear(pParentModificationUuid);
|
---|
2995 | }
|
---|
2996 |
|
---|
2997 | /*
|
---|
2998 | * Close the image.
|
---|
2999 | */
|
---|
3000 | vdiCloseImage(pImage);
|
---|
3001 |
|
---|
3002 | return VINF_SUCCESS;
|
---|
3003 | }
|
---|
3004 |
|
---|
3005 | /**
|
---|
3006 | * Changes the image's UUID and parent UUIDs.
|
---|
3007 | *
|
---|
3008 | * @returns VBox status code.
|
---|
3009 | * @param pszFilename Name of the image file to operate on.
|
---|
3010 | * @param pUuid Optional parameter, new UUID of the image.
|
---|
3011 | * @param pModificationUuid Optional parameter, new modification UUID of the image.
|
---|
3012 | * @param pParentUuuid Optional parameter, new parent UUID of the image.
|
---|
3013 | * @param pParentModificationUuid Optional parameter, new parent modification UUID of the image.
|
---|
3014 | */
|
---|
3015 | IDER3DECL(int) VDISetImageUUIDs(const char *pszFilename,
|
---|
3016 | PCRTUUID pUuid, PCRTUUID pModificationUuid,
|
---|
3017 | PCRTUUID pParentUuid, PCRTUUID pParentModificationUuid)
|
---|
3018 | {
|
---|
3019 | LogFlow(("VDISetImageUUIDs:\n"));
|
---|
3020 |
|
---|
3021 | /* Check arguments. */
|
---|
3022 | if ( !pszFilename
|
---|
3023 | || *pszFilename == '\0')
|
---|
3024 | {
|
---|
3025 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
3026 | return VERR_INVALID_PARAMETER;
|
---|
3027 | }
|
---|
3028 |
|
---|
3029 | PVDIIMAGEDESC pImage;
|
---|
3030 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
3031 | if (VBOX_FAILURE(rc))
|
---|
3032 | {
|
---|
3033 | Log(("VDISetImageUUIDs: vdiOpenImage rc=%Vrc filename=\"%s\"\n", rc, pszFilename));
|
---|
3034 | return rc;
|
---|
3035 | }
|
---|
3036 | if (!pImage->fReadOnly)
|
---|
3037 | {
|
---|
3038 | /* we only support new images */
|
---|
3039 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
3040 | {
|
---|
3041 | if (pUuid)
|
---|
3042 | pImage->Header.u.v1.uuidCreate = *pUuid;
|
---|
3043 |
|
---|
3044 | if (pModificationUuid)
|
---|
3045 | pImage->Header.u.v1.uuidModify = *pModificationUuid;
|
---|
3046 |
|
---|
3047 | if (pParentUuid)
|
---|
3048 | pImage->Header.u.v1.uuidLinkage = *pParentUuid;
|
---|
3049 |
|
---|
3050 | if (pParentModificationUuid)
|
---|
3051 | pImage->Header.u.v1.uuidParentModify = *pParentModificationUuid;
|
---|
3052 |
|
---|
3053 | /* write out new header */
|
---|
3054 | rc = vdiUpdateHeader(pImage);
|
---|
3055 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
|
---|
3056 | pImage->szFilename, rc));
|
---|
3057 | }
|
---|
3058 | else
|
---|
3059 | {
|
---|
3060 | Log(("VDISetImageUUIDs: Version is not supported!\n"));
|
---|
3061 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
3062 | }
|
---|
3063 | }
|
---|
3064 | else
|
---|
3065 | {
|
---|
3066 | Log(("VDISetImageUUIDs: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
3067 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
3068 | }
|
---|
3069 |
|
---|
3070 | vdiCloseImage(pImage);
|
---|
3071 | return rc;
|
---|
3072 | }
|
---|
3073 |
|
---|
3074 | /**
|
---|
3075 | * Merges two images having a parent/child relationship (both directions).
|
---|
3076 | *
|
---|
3077 | * @returns VBox status code.
|
---|
3078 | * @param pszFilenameFrom Name of the image file to merge from.
|
---|
3079 | * @param pszFilenameTo Name of the image file to merge into.
|
---|
3080 | * @param pfnProgress Progress callback. Optional. NULL if not to be used.
|
---|
3081 | * @param pvUser User argument for the progress callback.
|
---|
3082 | */
|
---|
3083 | IDER3DECL(int) VDIMergeImage(const char *pszFilenameFrom, const char *pszFilenameTo,
|
---|
3084 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
3085 | {
|
---|
3086 | LogFlow(("VDIMergeImage:\n"));
|
---|
3087 |
|
---|
3088 | /* Check arguments. */
|
---|
3089 | if ( !pszFilenameFrom
|
---|
3090 | || *pszFilenameFrom == '\0'
|
---|
3091 | || !pszFilenameTo
|
---|
3092 | || *pszFilenameTo == '\0')
|
---|
3093 | {
|
---|
3094 | AssertMsgFailed(("Invalid arguments: pszFilenameFrom=%p, pszFilenameTo=%p\n", pszFilenameFrom, pszFilenameTo));
|
---|
3095 | return VERR_INVALID_PARAMETER;
|
---|
3096 | }
|
---|
3097 |
|
---|
3098 | PVDIIMAGEDESC pImageFrom;
|
---|
3099 | int rc = vdiOpenImage(&pImageFrom, pszFilenameFrom, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
3100 | if (VBOX_FAILURE(rc))
|
---|
3101 | {
|
---|
3102 | Log(("VDIMergeImage: vdiOpenImage rc=%Vrc pstFilenameFrom=\"%s\"\n", rc, pszFilenameFrom));
|
---|
3103 | return rc;
|
---|
3104 | }
|
---|
3105 |
|
---|
3106 | PVDIIMAGEDESC pImageTo;
|
---|
3107 | rc = vdiOpenImage(&pImageTo, pszFilenameTo, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
3108 | if (VBOX_FAILURE(rc))
|
---|
3109 | {
|
---|
3110 | Log(("VDIMergeImage: vdiOpenImage rc=%Vrc pszFilenameTo=\"%s\"\n", rc, pszFilenameTo));
|
---|
3111 | vdiCloseImage(pImageFrom);
|
---|
3112 | return rc;
|
---|
3113 | }
|
---|
3114 | if (pImageTo->fReadOnly)
|
---|
3115 | {
|
---|
3116 | Log(("VDIMergeImage: image \"%s\" is opened as read-only!\n", pszFilenameTo));
|
---|
3117 | vdiCloseImage(pImageFrom);
|
---|
3118 | vdiCloseImage(pImageTo);
|
---|
3119 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
3120 | }
|
---|
3121 |
|
---|
3122 | /*
|
---|
3123 | * when merging, we should not update the modification uuid of the target
|
---|
3124 | * image, because from the point of view of its children, it hasn't been
|
---|
3125 | * logically changed after the successful merge.
|
---|
3126 | */
|
---|
3127 | vdiDisableLastModifiedUpdate(pImageTo);
|
---|
3128 |
|
---|
3129 | /*
|
---|
3130 | * Check in which direction we merge
|
---|
3131 | */
|
---|
3132 |
|
---|
3133 | bool bParentToChild = false;
|
---|
3134 | if ( getImageParentUUID(&pImageFrom->Header)
|
---|
3135 | && !RTUuidCompare(getImageParentUUID(&pImageFrom->Header),
|
---|
3136 | getImageCreationUUID(&pImageTo->Header))
|
---|
3137 | && !RTUuidCompare(getImageParentModificationUUID(&pImageFrom->Header),
|
---|
3138 | getImageModificationUUID(&pImageTo->Header)))
|
---|
3139 | {
|
---|
3140 | /* we merge from a child to its parent */
|
---|
3141 | }
|
---|
3142 | else
|
---|
3143 | if ( getImageParentUUID(&pImageTo->Header)
|
---|
3144 | && !RTUuidCompare(getImageParentUUID(&pImageTo->Header),
|
---|
3145 | getImageCreationUUID(&pImageFrom->Header))
|
---|
3146 | && !RTUuidCompare(getImageParentModificationUUID(&pImageTo->Header),
|
---|
3147 | getImageModificationUUID(&pImageFrom->Header)))
|
---|
3148 | {
|
---|
3149 | /* we merge from a parent to its child */
|
---|
3150 | bParentToChild = true;
|
---|
3151 | }
|
---|
3152 | else
|
---|
3153 | {
|
---|
3154 | /* the images are not related, we can't merge! */
|
---|
3155 | Log(("VDIMergeImages: images do not have a parent/child or child/parent relationship!\n"));
|
---|
3156 | rc = VERR_VDI_IMAGES_UUID_MISMATCH;
|
---|
3157 | }
|
---|
3158 |
|
---|
3159 | rc = vdiMergeImages(pImageFrom, pImageTo, bParentToChild, pfnProgress, pvUser);
|
---|
3160 |
|
---|
3161 | if (pfnProgress)
|
---|
3162 | {
|
---|
3163 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
3164 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
3165 | }
|
---|
3166 |
|
---|
3167 | /* cleanup */
|
---|
3168 | vdiCloseImage(pImageFrom);
|
---|
3169 | vdiCloseImage(pImageTo);
|
---|
3170 |
|
---|
3171 | Log(("VDIMergeImage: done, returning with rc = %Vrc\n", rc));
|
---|
3172 | return rc;
|
---|
3173 | }
|
---|
3174 |
|
---|
3175 |
|
---|
3176 | /**
|
---|
3177 | * internal: initialize VDIDISK structure.
|
---|
3178 | */
|
---|
3179 | static void vdiInitVDIDisk(PVDIDISK pDisk)
|
---|
3180 | {
|
---|
3181 | Assert(pDisk);
|
---|
3182 | pDisk->u32Signature = VDIDISK_SIGNATURE;
|
---|
3183 | pDisk->cImages = 0;
|
---|
3184 | pDisk->pBase = NULL;
|
---|
3185 | pDisk->pLast = NULL;
|
---|
3186 | pDisk->cbBlock = VDI_IMAGE_DEFAULT_BLOCK_SIZE;
|
---|
3187 | pDisk->cbBuf = VDIDISK_DEFAULT_BUFFER_SIZE;
|
---|
3188 | }
|
---|
3189 |
|
---|
3190 | /**
|
---|
3191 | * internal: add image structure to the end of images list.
|
---|
3192 | */
|
---|
3193 | static void vdiAddImageToList(PVDIDISK pDisk, PVDIIMAGEDESC pImage)
|
---|
3194 | {
|
---|
3195 | pImage->pPrev = NULL;
|
---|
3196 | pImage->pNext = NULL;
|
---|
3197 |
|
---|
3198 | if (pDisk->pBase)
|
---|
3199 | {
|
---|
3200 | Assert(pDisk->cImages > 0);
|
---|
3201 | pImage->pPrev = pDisk->pLast;
|
---|
3202 | pDisk->pLast->pNext = pImage;
|
---|
3203 | pDisk->pLast = pImage;
|
---|
3204 | }
|
---|
3205 | else
|
---|
3206 | {
|
---|
3207 | Assert(pDisk->cImages == 0);
|
---|
3208 | pDisk->pBase = pImage;
|
---|
3209 | pDisk->pLast = pImage;
|
---|
3210 | }
|
---|
3211 |
|
---|
3212 | pDisk->cImages++;
|
---|
3213 | }
|
---|
3214 |
|
---|
3215 | /**
|
---|
3216 | * internal: remove image structure from the images list.
|
---|
3217 | */
|
---|
3218 | static void vdiRemoveImageFromList(PVDIDISK pDisk, PVDIIMAGEDESC pImage)
|
---|
3219 | {
|
---|
3220 | Assert(pDisk->cImages > 0);
|
---|
3221 |
|
---|
3222 | if (pImage->pPrev)
|
---|
3223 | pImage->pPrev->pNext = pImage->pNext;
|
---|
3224 | else
|
---|
3225 | pDisk->pBase = pImage->pNext;
|
---|
3226 |
|
---|
3227 | if (pImage->pNext)
|
---|
3228 | pImage->pNext->pPrev = pImage->pPrev;
|
---|
3229 | else
|
---|
3230 | pDisk->pLast = pImage->pPrev;
|
---|
3231 |
|
---|
3232 | pImage->pPrev = NULL;
|
---|
3233 | pImage->pNext = NULL;
|
---|
3234 |
|
---|
3235 | pDisk->cImages--;
|
---|
3236 | }
|
---|
3237 |
|
---|
3238 | /**
|
---|
3239 | * Allocates and initializes VDI HDD container.
|
---|
3240 | *
|
---|
3241 | * @returns Pointer to newly created HDD container with no one opened image file.
|
---|
3242 | * @returns NULL on failure, typically out of memory.
|
---|
3243 | */
|
---|
3244 | IDER3DECL(PVDIDISK) VDIDiskCreate(void)
|
---|
3245 | {
|
---|
3246 | PVDIDISK pDisk = (PVDIDISK)RTMemAllocZ(sizeof(VDIDISK));
|
---|
3247 | if (pDisk)
|
---|
3248 | vdiInitVDIDisk(pDisk);
|
---|
3249 | LogFlow(("VDIDiskCreate: returns pDisk=%X\n", pDisk));
|
---|
3250 | return pDisk;
|
---|
3251 | }
|
---|
3252 |
|
---|
3253 | /**
|
---|
3254 | * Destroys VDI HDD container. If container has opened image files they will be closed.
|
---|
3255 | *
|
---|
3256 | * @param pDisk Pointer to VDI HDD container.
|
---|
3257 | */
|
---|
3258 | IDER3DECL(void) VDIDiskDestroy(PVDIDISK pDisk)
|
---|
3259 | {
|
---|
3260 | LogFlow(("VDIDiskDestroy: pDisk=%X\n", pDisk));
|
---|
3261 | /* sanity check */
|
---|
3262 | Assert(pDisk);
|
---|
3263 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3264 |
|
---|
3265 | if (pDisk)
|
---|
3266 | {
|
---|
3267 | VDIDiskCloseAllImages(pDisk);
|
---|
3268 | RTMemFree(pDisk);
|
---|
3269 | }
|
---|
3270 | }
|
---|
3271 |
|
---|
3272 | /**
|
---|
3273 | * Get working buffer size of VDI HDD container.
|
---|
3274 | *
|
---|
3275 | * @returns Working buffer size in bytes.
|
---|
3276 | */
|
---|
3277 | IDER3DECL(unsigned) VDIDiskGetBufferSize(PVDIDISK pDisk)
|
---|
3278 | {
|
---|
3279 | /* sanity check */
|
---|
3280 | Assert(pDisk);
|
---|
3281 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3282 |
|
---|
3283 | LogFlow(("VDIDiskGetBufferSize: returns %u\n", pDisk->cbBuf));
|
---|
3284 | return pDisk->cbBuf;
|
---|
3285 | }
|
---|
3286 |
|
---|
3287 | /**
|
---|
3288 | * Get read/write mode of VDI HDD.
|
---|
3289 | *
|
---|
3290 | * @returns Disk ReadOnly status.
|
---|
3291 | * @returns true if no one VDI image is opened in HDD container.
|
---|
3292 | */
|
---|
3293 | IDER3DECL(bool) VDIDiskIsReadOnly(PVDIDISK pDisk)
|
---|
3294 | {
|
---|
3295 | /* sanity check */
|
---|
3296 | Assert(pDisk);
|
---|
3297 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3298 |
|
---|
3299 | if (pDisk->pLast)
|
---|
3300 | {
|
---|
3301 | LogFlow(("VDIDiskIsReadOnly: returns %u\n", pDisk->pLast->fReadOnly));
|
---|
3302 | return pDisk->pLast->fReadOnly;
|
---|
3303 | }
|
---|
3304 |
|
---|
3305 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
3306 | return true;
|
---|
3307 | }
|
---|
3308 |
|
---|
3309 | /**
|
---|
3310 | * Get disk size of VDI HDD container.
|
---|
3311 | *
|
---|
3312 | * @returns Virtual disk size in bytes.
|
---|
3313 | * @returns 0 if no one VDI image is opened in HDD container.
|
---|
3314 | */
|
---|
3315 | IDER3DECL(uint64_t) VDIDiskGetSize(PVDIDISK pDisk)
|
---|
3316 | {
|
---|
3317 | /* sanity check */
|
---|
3318 | Assert(pDisk);
|
---|
3319 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3320 |
|
---|
3321 | if (pDisk->pBase)
|
---|
3322 | {
|
---|
3323 | LogFlow(("VDIDiskGetSize: returns %llu\n", getImageDiskSize(&pDisk->pBase->Header)));
|
---|
3324 | return getImageDiskSize(&pDisk->pBase->Header);
|
---|
3325 | }
|
---|
3326 |
|
---|
3327 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
3328 | return 0;
|
---|
3329 | }
|
---|
3330 |
|
---|
3331 | /**
|
---|
3332 | * Get block size of VDI HDD container.
|
---|
3333 | *
|
---|
3334 | * @returns VDI image block size in bytes.
|
---|
3335 | * @returns 0 if no one VDI image is opened in HDD container.
|
---|
3336 | */
|
---|
3337 | IDER3DECL(unsigned) VDIDiskGetBlockSize(PVDIDISK pDisk)
|
---|
3338 | {
|
---|
3339 | /* sanity check */
|
---|
3340 | Assert(pDisk);
|
---|
3341 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3342 |
|
---|
3343 | if (pDisk->pBase)
|
---|
3344 | {
|
---|
3345 | LogFlow(("VDIDiskGetBlockSize: returns %u\n", getImageBlockSize(&pDisk->pBase->Header)));
|
---|
3346 | return getImageBlockSize(&pDisk->pBase->Header);
|
---|
3347 | }
|
---|
3348 |
|
---|
3349 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
3350 | return 0;
|
---|
3351 | }
|
---|
3352 |
|
---|
3353 | /**
|
---|
3354 | * Get virtual disk geometry stored in image file.
|
---|
3355 | *
|
---|
3356 | * @returns VBox status code.
|
---|
3357 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
3358 | * @returns VERR_VDI_GEOMETRY_NOT_SET if no geometry has been setted.
|
---|
3359 | * @param pDisk Pointer to VDI HDD container.
|
---|
3360 | * @param pcCylinders Where to store the number of cylinders. NULL is ok.
|
---|
3361 | * @param pcHeads Where to store the number of heads. NULL is ok.
|
---|
3362 | * @param pcSectors Where to store the number of sectors. NULL is ok.
|
---|
3363 | */
|
---|
3364 | IDER3DECL(int) VDIDiskGetGeometry(PVDIDISK pDisk, unsigned *pcCylinders, unsigned *pcHeads, unsigned *pcSectors)
|
---|
3365 | {
|
---|
3366 | /* sanity check */
|
---|
3367 | Assert(pDisk);
|
---|
3368 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3369 |
|
---|
3370 | if (pDisk->pBase)
|
---|
3371 | {
|
---|
3372 | int rc = VINF_SUCCESS;
|
---|
3373 | PVDIDISKGEOMETRY pGeometry = getImageGeometry(&pDisk->pBase->Header);
|
---|
3374 | LogFlow(("VDIDiskGetGeometry: C/H/S = %u/%u/%u\n",
|
---|
3375 | pGeometry->cCylinders, pGeometry->cHeads, pGeometry->cSectors));
|
---|
3376 | if ( pGeometry->cCylinders > 0
|
---|
3377 | && pGeometry->cHeads > 0
|
---|
3378 | && pGeometry->cSectors > 0)
|
---|
3379 | {
|
---|
3380 | if (pcCylinders)
|
---|
3381 | *pcCylinders = pGeometry->cCylinders;
|
---|
3382 | if (pcHeads)
|
---|
3383 | *pcHeads = pGeometry->cHeads;
|
---|
3384 | if (pcSectors)
|
---|
3385 | *pcSectors = pGeometry->cSectors;
|
---|
3386 | }
|
---|
3387 | else
|
---|
3388 | rc = VERR_VDI_GEOMETRY_NOT_SET;
|
---|
3389 |
|
---|
3390 | LogFlow(("VDIDiskGetGeometry: returns %Vrc\n", rc));
|
---|
3391 | return rc;
|
---|
3392 | }
|
---|
3393 |
|
---|
3394 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
3395 | return VERR_VDI_NOT_OPENED;
|
---|
3396 | }
|
---|
3397 |
|
---|
3398 | /**
|
---|
3399 | * Store virtual disk geometry into base image file of HDD container.
|
---|
3400 | *
|
---|
3401 | * Note that in case of unrecoverable error all images of HDD container will be closed.
|
---|
3402 | *
|
---|
3403 | * @returns VBox status code.
|
---|
3404 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
3405 | * @param pDisk Pointer to VDI HDD container.
|
---|
3406 | * @param cCylinders Number of cylinders.
|
---|
3407 | * @param cHeads Number of heads.
|
---|
3408 | * @param cSectors Number of sectors.
|
---|
3409 | */
|
---|
3410 | IDER3DECL(int) VDIDiskSetGeometry(PVDIDISK pDisk, unsigned cCylinders, unsigned cHeads, unsigned cSectors)
|
---|
3411 | {
|
---|
3412 | LogFlow(("VDIDiskSetGeometry: C/H/S = %u/%u/%u\n", cCylinders, cHeads, cSectors));
|
---|
3413 | /* sanity check */
|
---|
3414 | Assert(pDisk);
|
---|
3415 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3416 |
|
---|
3417 | if (pDisk->pBase)
|
---|
3418 | {
|
---|
3419 | PVDIDISKGEOMETRY pGeometry = getImageGeometry(&pDisk->pBase->Header);
|
---|
3420 | pGeometry->cCylinders = cCylinders;
|
---|
3421 | pGeometry->cHeads = cHeads;
|
---|
3422 | pGeometry->cSectors = cSectors;
|
---|
3423 | pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
3424 |
|
---|
3425 | /* Update header information in base image file. */
|
---|
3426 | int rc = vdiUpdateReadOnlyHeader(pDisk->pBase);
|
---|
3427 | LogFlow(("VDIDiskSetGeometry: returns %Vrc\n", rc));
|
---|
3428 | return rc;
|
---|
3429 | }
|
---|
3430 |
|
---|
3431 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
3432 | return VERR_VDI_NOT_OPENED;
|
---|
3433 | }
|
---|
3434 |
|
---|
3435 | /**
|
---|
3436 | * Get virtual disk translation mode stored in image file.
|
---|
3437 | *
|
---|
3438 | * @returns VBox status code.
|
---|
3439 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
3440 | * @param pDisk Pointer to VDI HDD container.
|
---|
3441 | * @param penmTranslation Where to store the translation mode (see pdm.h).
|
---|
3442 | */
|
---|
3443 | IDER3DECL(int) VDIDiskGetTranslation(PVDIDISK pDisk, PPDMBIOSTRANSLATION penmTranslation)
|
---|
3444 | {
|
---|
3445 | /* sanity check */
|
---|
3446 | Assert(pDisk);
|
---|
3447 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3448 | Assert(penmTranslation);
|
---|
3449 |
|
---|
3450 | if (pDisk->pBase)
|
---|
3451 | {
|
---|
3452 | *penmTranslation = getImageTranslation(&pDisk->pBase->Header);
|
---|
3453 | LogFlow(("VDIDiskGetTranslation: translation=%d\n", *penmTranslation));
|
---|
3454 | return VINF_SUCCESS;
|
---|
3455 | }
|
---|
3456 |
|
---|
3457 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
3458 | return VERR_VDI_NOT_OPENED;
|
---|
3459 | }
|
---|
3460 |
|
---|
3461 | /**
|
---|
3462 | * Store virtual disk translation mode into base image file of HDD container.
|
---|
3463 | *
|
---|
3464 | * Note that in case of unrecoverable error all images of HDD container will be closed.
|
---|
3465 | *
|
---|
3466 | * @returns VBox status code.
|
---|
3467 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
3468 | * @param pDisk Pointer to VDI HDD container.
|
---|
3469 | * @param enmTranslation Translation mode (see pdm.h).
|
---|
3470 | */
|
---|
3471 | IDER3DECL(int) VDIDiskSetTranslation(PVDIDISK pDisk, PDMBIOSTRANSLATION enmTranslation)
|
---|
3472 | {
|
---|
3473 | LogFlow(("VDIDiskSetTranslation: enmTranslation=%d\n", enmTranslation));
|
---|
3474 | /* sanity check */
|
---|
3475 | Assert(pDisk);
|
---|
3476 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3477 |
|
---|
3478 | if (pDisk->pBase)
|
---|
3479 | {
|
---|
3480 | setImageTranslation(&pDisk->pBase->Header, enmTranslation);
|
---|
3481 |
|
---|
3482 | /* Update header information in base image file. */
|
---|
3483 | int rc = vdiUpdateReadOnlyHeader(pDisk->pBase);
|
---|
3484 | LogFlow(("VDIDiskSetTranslation: returns %Vrc\n", rc));
|
---|
3485 | return rc;
|
---|
3486 | }
|
---|
3487 |
|
---|
3488 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
3489 | return VERR_VDI_NOT_OPENED;
|
---|
3490 | }
|
---|
3491 |
|
---|
3492 | /**
|
---|
3493 | * Get number of opened images in HDD container.
|
---|
3494 | *
|
---|
3495 | * @returns Number of opened images for HDD container. 0 if no images is opened.
|
---|
3496 | * @param pDisk Pointer to VDI HDD container.
|
---|
3497 | */
|
---|
3498 | IDER3DECL(int) VDIDiskGetImagesCount(PVDIDISK pDisk)
|
---|
3499 | {
|
---|
3500 | /* sanity check */
|
---|
3501 | Assert(pDisk);
|
---|
3502 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3503 |
|
---|
3504 | LogFlow(("VDIDiskGetImagesCount: returns %d\n", pDisk->cImages));
|
---|
3505 | return pDisk->cImages;
|
---|
3506 | }
|
---|
3507 |
|
---|
3508 | static PVDIIMAGEDESC vdiGetImageByNumber(PVDIDISK pDisk, int nImage)
|
---|
3509 | {
|
---|
3510 | PVDIIMAGEDESC pImage = pDisk->pBase;
|
---|
3511 | while (pImage && nImage)
|
---|
3512 | {
|
---|
3513 | pImage = pImage->pNext;
|
---|
3514 | nImage--;
|
---|
3515 | }
|
---|
3516 | return pImage;
|
---|
3517 | }
|
---|
3518 |
|
---|
3519 | /**
|
---|
3520 | * Get version of opened image of HDD container.
|
---|
3521 | *
|
---|
3522 | * @returns VBox status code.
|
---|
3523 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3524 | * @param pDisk Pointer to VDI HDD container.
|
---|
3525 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3526 | * @param puVersion Where to store the image version.
|
---|
3527 | */
|
---|
3528 | IDER3DECL(int) VDIDiskGetImageVersion(PVDIDISK pDisk, int nImage, unsigned *puVersion)
|
---|
3529 | {
|
---|
3530 | /* sanity check */
|
---|
3531 | Assert(pDisk);
|
---|
3532 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3533 | Assert(puVersion);
|
---|
3534 |
|
---|
3535 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3536 | Assert(pImage);
|
---|
3537 |
|
---|
3538 | if (pImage)
|
---|
3539 | {
|
---|
3540 | *puVersion = pImage->PreHeader.u32Version;
|
---|
3541 | LogFlow(("VDIDiskGetImageVersion: returns %08X\n", pImage->PreHeader.u32Version));
|
---|
3542 | return VINF_SUCCESS;
|
---|
3543 | }
|
---|
3544 |
|
---|
3545 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3546 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3547 | }
|
---|
3548 |
|
---|
3549 | /**
|
---|
3550 | * Get filename of opened image of HDD container.
|
---|
3551 | *
|
---|
3552 | * @returns VBox status code.
|
---|
3553 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3554 | * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
|
---|
3555 | * @param pDisk Pointer to VDI HDD container.
|
---|
3556 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3557 | * @param pszFilename Where to store the image file name.
|
---|
3558 | * @param cbFilename Size of buffer pszFilename points to.
|
---|
3559 | */
|
---|
3560 | IDER3DECL(int) VDIDiskGetImageFilename(PVDIDISK pDisk, int nImage, char *pszFilename, unsigned cbFilename)
|
---|
3561 | {
|
---|
3562 | /* sanity check */
|
---|
3563 | Assert(pDisk);
|
---|
3564 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3565 | Assert(pszFilename);
|
---|
3566 |
|
---|
3567 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3568 | Assert(pImage);
|
---|
3569 |
|
---|
3570 | if (pImage)
|
---|
3571 | {
|
---|
3572 | unsigned cb = strlen(pImage->szFilename);
|
---|
3573 | if (cb < cbFilename)
|
---|
3574 | {
|
---|
3575 | /* memcpy is much better than strncpy. */
|
---|
3576 | memcpy(pszFilename, pImage->szFilename, cb + 1);
|
---|
3577 | LogFlow(("VDIDiskGetImageFilename: returns VINF_SUCCESS, filename=\"%s\" nImage=%d\n",
|
---|
3578 | pszFilename, nImage));
|
---|
3579 | return VINF_SUCCESS;
|
---|
3580 | }
|
---|
3581 | else
|
---|
3582 | {
|
---|
3583 | AssertMsgFailed(("Out of buffer space, cbFilename=%d needed=%d\n", cbFilename, cb + 1));
|
---|
3584 | return VERR_BUFFER_OVERFLOW;
|
---|
3585 | }
|
---|
3586 | }
|
---|
3587 |
|
---|
3588 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3589 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3590 | }
|
---|
3591 |
|
---|
3592 | /**
|
---|
3593 | * Get the comment line of opened image of HDD container.
|
---|
3594 | *
|
---|
3595 | * @returns VBox status code.
|
---|
3596 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3597 | * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
|
---|
3598 | * @param pDisk Pointer to VDI HDD container.
|
---|
3599 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3600 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
3601 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
3602 | */
|
---|
3603 | IDER3DECL(int) VDIDiskGetImageComment(PVDIDISK pDisk, int nImage, char *pszComment, unsigned cbComment)
|
---|
3604 | {
|
---|
3605 | /* sanity check */
|
---|
3606 | Assert(pDisk);
|
---|
3607 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3608 | Assert(pszComment);
|
---|
3609 |
|
---|
3610 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3611 | Assert(pImage);
|
---|
3612 |
|
---|
3613 | if (pImage)
|
---|
3614 | {
|
---|
3615 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
3616 | unsigned cb = strlen(pszTmp);
|
---|
3617 | if (cb < cbComment)
|
---|
3618 | {
|
---|
3619 | /* memcpy is much better than strncpy. */
|
---|
3620 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
3621 | LogFlow(("VDIDiskGetImageComment: returns VINF_SUCCESS, comment=\"%s\" nImage=%d\n",
|
---|
3622 | pszTmp, nImage));
|
---|
3623 | return VINF_SUCCESS;
|
---|
3624 | }
|
---|
3625 | else
|
---|
3626 | {
|
---|
3627 | AssertMsgFailed(("Out of buffer space, cbComment=%d needed=%d\n", cbComment, cb + 1));
|
---|
3628 | return VERR_BUFFER_OVERFLOW;
|
---|
3629 | }
|
---|
3630 | }
|
---|
3631 |
|
---|
3632 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3633 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3634 | }
|
---|
3635 |
|
---|
3636 | /**
|
---|
3637 | * Get type of opened image of HDD container.
|
---|
3638 | *
|
---|
3639 | * @returns VBox status code.
|
---|
3640 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3641 | * @param pDisk Pointer to VDI HDD container.
|
---|
3642 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3643 | * @param penmType Where to store the image type.
|
---|
3644 | */
|
---|
3645 | IDER3DECL(int) VDIDiskGetImageType(PVDIDISK pDisk, int nImage, PVDIIMAGETYPE penmType)
|
---|
3646 | {
|
---|
3647 | /* sanity check */
|
---|
3648 | Assert(pDisk);
|
---|
3649 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3650 | Assert(penmType);
|
---|
3651 |
|
---|
3652 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3653 | Assert(pImage);
|
---|
3654 |
|
---|
3655 | if (pImage)
|
---|
3656 | {
|
---|
3657 | *penmType = getImageType(&pImage->Header);
|
---|
3658 | LogFlow(("VDIDiskGetImageType: returns VINF_SUCCESS, type=%X nImage=%d\n",
|
---|
3659 | *penmType, nImage));
|
---|
3660 | return VINF_SUCCESS;
|
---|
3661 | }
|
---|
3662 |
|
---|
3663 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3664 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3665 | }
|
---|
3666 |
|
---|
3667 | /**
|
---|
3668 | * Get flags of opened image of HDD container.
|
---|
3669 | *
|
---|
3670 | * @returns VBox status code.
|
---|
3671 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3672 | * @param pDisk Pointer to VDI HDD container.
|
---|
3673 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3674 | * @param pfFlags Where to store the image flags.
|
---|
3675 | */
|
---|
3676 | IDER3DECL(int) VDIDiskGetImageFlags(PVDIDISK pDisk, int nImage, unsigned *pfFlags)
|
---|
3677 | {
|
---|
3678 | /* sanity check */
|
---|
3679 | Assert(pDisk);
|
---|
3680 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3681 | Assert(pfFlags);
|
---|
3682 |
|
---|
3683 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3684 | Assert(pImage);
|
---|
3685 |
|
---|
3686 | if (pImage)
|
---|
3687 | {
|
---|
3688 | *pfFlags = getImageFlags(&pImage->Header);
|
---|
3689 | LogFlow(("VDIDiskGetImageFlags: returns VINF_SUCCESS, flags=%08X nImage=%d\n",
|
---|
3690 | *pfFlags, nImage));
|
---|
3691 | return VINF_SUCCESS;
|
---|
3692 | }
|
---|
3693 |
|
---|
3694 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3695 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3696 | }
|
---|
3697 |
|
---|
3698 | /**
|
---|
3699 | * Get Uuid of opened image of HDD container.
|
---|
3700 | *
|
---|
3701 | * @returns VBox status code.
|
---|
3702 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3703 | * @param pDisk Pointer to VDI HDD container.
|
---|
3704 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3705 | * @param pUuid Where to store the image creation uuid.
|
---|
3706 | */
|
---|
3707 | IDER3DECL(int) VDIDiskGetImageUuid(PVDIDISK pDisk, int nImage, PRTUUID pUuid)
|
---|
3708 | {
|
---|
3709 | /* sanity check */
|
---|
3710 | Assert(pDisk);
|
---|
3711 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3712 | Assert(pUuid);
|
---|
3713 |
|
---|
3714 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3715 | Assert(pImage);
|
---|
3716 |
|
---|
3717 | if (pImage)
|
---|
3718 | {
|
---|
3719 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
3720 | LogFlow(("VDIDiskGetImageUuid: returns VINF_SUCCESS, uuid={%Vuuid} nImage=%d\n",
|
---|
3721 | pUuid, nImage));
|
---|
3722 | return VINF_SUCCESS;
|
---|
3723 | }
|
---|
3724 |
|
---|
3725 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3726 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3727 | }
|
---|
3728 |
|
---|
3729 | /**
|
---|
3730 | * Get last modification Uuid of opened image of HDD container.
|
---|
3731 | *
|
---|
3732 | * @returns VBox status code.
|
---|
3733 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3734 | * @param pDisk Pointer to VDI HDD container.
|
---|
3735 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3736 | * @param pUuid Where to store the image modification uuid.
|
---|
3737 | */
|
---|
3738 | IDER3DECL(int) VDIDiskGetImageModificationUuid(PVDIDISK pDisk, int nImage, PRTUUID pUuid)
|
---|
3739 | {
|
---|
3740 | /* sanity check */
|
---|
3741 | Assert(pDisk);
|
---|
3742 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3743 | Assert(pUuid);
|
---|
3744 |
|
---|
3745 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3746 | Assert(pImage);
|
---|
3747 |
|
---|
3748 | if (pImage)
|
---|
3749 | {
|
---|
3750 | *pUuid = *getImageModificationUUID(&pImage->Header);
|
---|
3751 | LogFlow(("VDIDiskGetImageModificationUuid: returns VINF_SUCCESS, uuid={%Vuuid} nImage=%d\n",
|
---|
3752 | pUuid, nImage));
|
---|
3753 | return VINF_SUCCESS;
|
---|
3754 | }
|
---|
3755 |
|
---|
3756 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3757 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3758 | }
|
---|
3759 |
|
---|
3760 | /**
|
---|
3761 | * Get Uuid of opened image's parent image.
|
---|
3762 | *
|
---|
3763 | * @returns VBox status code.
|
---|
3764 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3765 | * @param pDisk Pointer to VDI HDD container.
|
---|
3766 | * @param nImage Image number, counts from 0. 0 is always base image of the container.
|
---|
3767 | * @param pUuid Where to store the image creation uuid.
|
---|
3768 | */
|
---|
3769 | IDER3DECL(int) VDIDiskGetParentImageUuid(PVDIDISK pDisk, int nImage, PRTUUID pUuid)
|
---|
3770 | {
|
---|
3771 | /* sanity check */
|
---|
3772 | Assert(pDisk);
|
---|
3773 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3774 | Assert(pUuid);
|
---|
3775 |
|
---|
3776 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3777 | if (pImage)
|
---|
3778 | {
|
---|
3779 | *pUuid = *getImageParentUUID(&pImage->Header);
|
---|
3780 | LogFlow(("VDIDiskGetParentImageUuid: returns VINF_SUCCESS, *pUuid={%Vuuid} nImage=%d\n",
|
---|
3781 | pUuid, nImage));
|
---|
3782 | return VINF_SUCCESS;
|
---|
3783 | }
|
---|
3784 |
|
---|
3785 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3786 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3787 | }
|
---|
3788 |
|
---|
3789 | /**
|
---|
3790 | * internal: Relock image as read/write or read-only.
|
---|
3791 | */
|
---|
3792 | static int vdiChangeImageMode(PVDIIMAGEDESC pImage, bool fReadOnly)
|
---|
3793 | {
|
---|
3794 | Assert(pImage);
|
---|
3795 |
|
---|
3796 | if ( !fReadOnly
|
---|
3797 | && pImage->fOpen & VDI_OPEN_FLAGS_READONLY)
|
---|
3798 | {
|
---|
3799 | /* Can't switch read-only opened image to read-write mode. */
|
---|
3800 | Log(("vdiChangeImageMode: can't switch r/o image to r/w mode, filename=\"%s\" fOpen=%X\n",
|
---|
3801 | pImage->szFilename, pImage->fOpen));
|
---|
3802 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
3803 | }
|
---|
3804 |
|
---|
3805 | /* Flush last image changes if was r/w mode. */
|
---|
3806 | vdiFlushImage(pImage);
|
---|
3807 |
|
---|
3808 | /* Change image locking. */
|
---|
3809 | uint64_t cbLock = pImage->offStartData
|
---|
3810 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
3811 | int rc = RTFileChangeLock(pImage->File,
|
---|
3812 | (fReadOnly) ?
|
---|
3813 | RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY :
|
---|
3814 | RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY,
|
---|
3815 | 0,
|
---|
3816 | cbLock);
|
---|
3817 | if (VBOX_SUCCESS(rc))
|
---|
3818 | {
|
---|
3819 | pImage->fReadOnly = fReadOnly;
|
---|
3820 | Log(("vdiChangeImageMode: Image \"%s\" mode changed to %s\n",
|
---|
3821 | pImage->szFilename, (pImage->fReadOnly) ? "read-only" : "read/write"));
|
---|
3822 | return VINF_SUCCESS;
|
---|
3823 | }
|
---|
3824 |
|
---|
3825 | /* Check for the most bad error in the world. Damn! It must never happens in real life! */
|
---|
3826 | if (rc == VERR_FILE_LOCK_LOST)
|
---|
3827 | {
|
---|
3828 | /* And what we can do now?! */
|
---|
3829 | AssertMsgFailed(("Image lock has been lost for file=\"%s\"", pImage->szFilename));
|
---|
3830 | Log(("vdiChangeImageMode: image lock has been lost for file=\"%s\", blocking on r/o lock wait",
|
---|
3831 | pImage->szFilename));
|
---|
3832 |
|
---|
3833 | /* Try to obtain read lock in blocking mode. Maybe it's a very bad method. */
|
---|
3834 | rc = RTFileLock(pImage->File, RTFILE_LOCK_READ | RTFILE_LOCK_WAIT, 0, cbLock);
|
---|
3835 | AssertReleaseRC(rc);
|
---|
3836 |
|
---|
3837 | pImage->fReadOnly = false;
|
---|
3838 | if (pImage->fReadOnly != fReadOnly)
|
---|
3839 | rc = VERR_FILE_LOCK_VIOLATION;
|
---|
3840 | }
|
---|
3841 |
|
---|
3842 | Log(("vdiChangeImageMode: Image \"%s\" mode change failed with rc=%Vrc, mode is %s\n",
|
---|
3843 | pImage->szFilename, rc, (pImage->fReadOnly) ? "read-only" : "read/write"));
|
---|
3844 |
|
---|
3845 | return rc;
|
---|
3846 | }
|
---|
3847 |
|
---|
3848 | /**
|
---|
3849 | * internal: try to save header in image file even if image is in read-only mode.
|
---|
3850 | */
|
---|
3851 | static int vdiUpdateReadOnlyHeader(PVDIIMAGEDESC pImage)
|
---|
3852 | {
|
---|
3853 | int rc = VINF_SUCCESS;
|
---|
3854 |
|
---|
3855 | if (pImage->fReadOnly)
|
---|
3856 | {
|
---|
3857 | rc = vdiChangeImageMode(pImage, false);
|
---|
3858 | if (VBOX_SUCCESS(rc))
|
---|
3859 | {
|
---|
3860 | vdiFlushImage(pImage);
|
---|
3861 | rc = vdiChangeImageMode(pImage, true);
|
---|
3862 | AssertReleaseRC(rc);
|
---|
3863 | }
|
---|
3864 | }
|
---|
3865 | else
|
---|
3866 | vdiFlushImage(pImage);
|
---|
3867 |
|
---|
3868 | return rc;
|
---|
3869 | }
|
---|
3870 |
|
---|
3871 | /**
|
---|
3872 | * Opens an image file.
|
---|
3873 | *
|
---|
3874 | * The first opened image file in a HDD container must have a base image type,
|
---|
3875 | * others (next opened images) must be a differencing or undo images.
|
---|
3876 | * Linkage is checked for differencing image to be in consistence with the previously opened image.
|
---|
3877 | * When a next differencing image is opened and the last image was opened in read/write access
|
---|
3878 | * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
|
---|
3879 | * other processes to use images in read-only mode too.
|
---|
3880 | *
|
---|
3881 | * Note that the image can be opened in read-only mode if a read/write open is not possible.
|
---|
3882 | * Use VDIDiskIsReadOnly to check open mode.
|
---|
3883 | *
|
---|
3884 | * @returns VBox status code.
|
---|
3885 | * @param pDisk Pointer to VDI HDD container.
|
---|
3886 | * @param pszFilename Name of the image file to open.
|
---|
3887 | * @param fOpen Image file open mode, see VDI_OPEN_FLAGS_* constants.
|
---|
3888 | */
|
---|
3889 | IDER3DECL(int) VDIDiskOpenImage(PVDIDISK pDisk, const char *pszFilename, unsigned fOpen)
|
---|
3890 | {
|
---|
3891 | /* sanity check */
|
---|
3892 | Assert(pDisk);
|
---|
3893 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3894 |
|
---|
3895 | /* Check arguments. */
|
---|
3896 | if ( !pszFilename
|
---|
3897 | || *pszFilename == '\0'
|
---|
3898 | || (fOpen & ~VDI_OPEN_FLAGS_MASK))
|
---|
3899 | {
|
---|
3900 | AssertMsgFailed(("Invalid arguments: pszFilename=%p fOpen=%x\n", pszFilename, fOpen));
|
---|
3901 | return VERR_INVALID_PARAMETER;
|
---|
3902 | }
|
---|
3903 | LogFlow(("VDIDiskOpenImage: pszFilename=\"%s\" fOpen=%X\n", pszFilename, fOpen));
|
---|
3904 |
|
---|
3905 | PVDIIMAGEDESC pImage;
|
---|
3906 | int rc = vdiOpenImage(&pImage, pszFilename, fOpen, pDisk->pLast);
|
---|
3907 | if (VBOX_SUCCESS(rc))
|
---|
3908 | {
|
---|
3909 | if (pDisk->pLast)
|
---|
3910 | {
|
---|
3911 | /* Opening differencing image. */
|
---|
3912 | if (!pDisk->pLast->fReadOnly)
|
---|
3913 | {
|
---|
3914 | /*
|
---|
3915 | * Previous image is opened in read/write mode -> switch it into read-only.
|
---|
3916 | */
|
---|
3917 | rc = vdiChangeImageMode(pDisk->pLast, true);
|
---|
3918 | }
|
---|
3919 | }
|
---|
3920 | else
|
---|
3921 | {
|
---|
3922 | /* Opening base image, check its type. */
|
---|
3923 | if ( getImageType(&pImage->Header) != VDI_IMAGE_TYPE_NORMAL
|
---|
3924 | && getImageType(&pImage->Header) != VDI_IMAGE_TYPE_FIXED)
|
---|
3925 | {
|
---|
3926 | rc = VERR_VDI_INVALID_TYPE;
|
---|
3927 | }
|
---|
3928 | }
|
---|
3929 |
|
---|
3930 | if (VBOX_SUCCESS(rc))
|
---|
3931 | vdiAddImageToList(pDisk, pImage);
|
---|
3932 | else
|
---|
3933 | vdiCloseImage(pImage);
|
---|
3934 | }
|
---|
3935 |
|
---|
3936 | LogFlow(("VDIDiskOpenImage: returns %Vrc\n", rc));
|
---|
3937 | return rc;
|
---|
3938 | }
|
---|
3939 |
|
---|
3940 | /**
|
---|
3941 | * Closes the last opened image file in the HDD container. Leaves all changes inside it.
|
---|
3942 | * If previous image file was opened in read-only mode (that is normal) and closing image
|
---|
3943 | * was opened in read-write mode (the whole disk was in read-write mode) - the previous image
|
---|
3944 | * will be reopened in read/write mode.
|
---|
3945 | *
|
---|
3946 | * @param pDisk Pointer to VDI HDD container.
|
---|
3947 | */
|
---|
3948 | IDER3DECL(void) VDIDiskCloseImage(PVDIDISK pDisk)
|
---|
3949 | {
|
---|
3950 | /* sanity check */
|
---|
3951 | Assert(pDisk);
|
---|
3952 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3953 |
|
---|
3954 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
3955 | if (pImage)
|
---|
3956 | {
|
---|
3957 | LogFlow(("VDIDiskCloseImage: closing image \"%s\"\n", pImage->szFilename));
|
---|
3958 |
|
---|
3959 | bool fWasReadOnly = pImage->fReadOnly;
|
---|
3960 | vdiRemoveImageFromList(pDisk, pImage);
|
---|
3961 | vdiCloseImage(pImage);
|
---|
3962 |
|
---|
3963 | if ( !fWasReadOnly
|
---|
3964 | && pDisk->pLast
|
---|
3965 | && pDisk->pLast->fReadOnly
|
---|
3966 | && !(pDisk->pLast->fOpen & VDI_OPEN_FLAGS_READONLY))
|
---|
3967 | {
|
---|
3968 | /*
|
---|
3969 | * Closed image was opened in read/write mode, previous image was opened
|
---|
3970 | * in read-only mode, try to switch it into read/write.
|
---|
3971 | */
|
---|
3972 | int rc = vdiChangeImageMode(pDisk->pLast, false);
|
---|
3973 | NOREF(rc); /* gcc still hates unused variables... */
|
---|
3974 | }
|
---|
3975 |
|
---|
3976 | return;
|
---|
3977 | }
|
---|
3978 | AssertMsgFailed(("No images to close\n"));
|
---|
3979 | }
|
---|
3980 |
|
---|
3981 | /**
|
---|
3982 | * Closes all opened image files in HDD container.
|
---|
3983 | *
|
---|
3984 | * @param pDisk Pointer to VDI HDD container.
|
---|
3985 | */
|
---|
3986 | IDER3DECL(void) VDIDiskCloseAllImages(PVDIDISK pDisk)
|
---|
3987 | {
|
---|
3988 | LogFlow(("VDIDiskCloseAllImages:\n"));
|
---|
3989 | /* sanity check */
|
---|
3990 | Assert(pDisk);
|
---|
3991 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3992 |
|
---|
3993 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
3994 | while (pImage)
|
---|
3995 | {
|
---|
3996 | PVDIIMAGEDESC pPrev = pImage->pPrev;
|
---|
3997 | vdiRemoveImageFromList(pDisk, pImage);
|
---|
3998 | vdiCloseImage(pImage);
|
---|
3999 | pImage = pPrev;
|
---|
4000 | }
|
---|
4001 | Assert(pDisk->pLast == NULL);
|
---|
4002 | }
|
---|
4003 |
|
---|
4004 | /**
|
---|
4005 | * Commits last opened differencing/undo image file of HDD container to previous one.
|
---|
4006 | * If previous image file was opened in read-only mode (that must be always so) it is reopened
|
---|
4007 | * as read/write to do commit operation.
|
---|
4008 | * After successfull commit the previous image file again reopened in read-only mode, last opened
|
---|
4009 | * image file is cleared of data and remains open and active in HDD container.
|
---|
4010 | * If you want to delete image after commit you must do it manually by VDIDiskCloseImage and
|
---|
4011 | * VDIDeleteImage calls.
|
---|
4012 | *
|
---|
4013 | * Note that in case of unrecoverable error all images of HDD container will be closed.
|
---|
4014 | *
|
---|
4015 | * @returns VBox status code.
|
---|
4016 | * @param pDisk Pointer to VDI HDD container.
|
---|
4017 | * @param pfnProgress Progress callback. Optional.
|
---|
4018 | * @param pvUser User argument for the progress callback.
|
---|
4019 | * @remark Only used by tstVDI.
|
---|
4020 | */
|
---|
4021 | IDER3DECL(int) VDIDiskCommitLastDiff(PVDIDISK pDisk, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
4022 | {
|
---|
4023 | LogFlow(("VDIDiskCommitLastDiff:\n"));
|
---|
4024 | /* sanity check */
|
---|
4025 | Assert(pDisk);
|
---|
4026 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
4027 |
|
---|
4028 | int rc = VINF_SUCCESS;
|
---|
4029 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
4030 | if (!pImage)
|
---|
4031 | {
|
---|
4032 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
4033 | return VERR_VDI_NOT_OPENED;
|
---|
4034 | }
|
---|
4035 |
|
---|
4036 | if (pImage->fReadOnly)
|
---|
4037 | {
|
---|
4038 | AssertMsgFailed(("Image \"%s\" is read-only!\n", pImage->szFilename));
|
---|
4039 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
4040 | }
|
---|
4041 |
|
---|
4042 | if (!pImage->pPrev)
|
---|
4043 | {
|
---|
4044 | AssertMsgFailed(("No images to commit to!\n"));
|
---|
4045 | return VERR_VDI_NO_DIFF_IMAGES;
|
---|
4046 | }
|
---|
4047 |
|
---|
4048 | bool fWasReadOnly = pImage->pPrev->fReadOnly;
|
---|
4049 | if (fWasReadOnly)
|
---|
4050 | {
|
---|
4051 | /* Change previous image mode to r/w. */
|
---|
4052 | rc = vdiChangeImageMode(pImage->pPrev, false);
|
---|
4053 | if (VBOX_FAILURE(rc))
|
---|
4054 | {
|
---|
4055 | Log(("VDIDiskCommitLastDiff: can't switch previous image into r/w mode, rc=%Vrc\n", rc));
|
---|
4056 | return rc;
|
---|
4057 | }
|
---|
4058 | }
|
---|
4059 |
|
---|
4060 | rc = vdiCommitToImage(pDisk, pImage->pPrev, pfnProgress, pvUser);
|
---|
4061 | if (VBOX_SUCCESS(rc) && fWasReadOnly)
|
---|
4062 | {
|
---|
4063 | /* Change previous image mode back to r/o. */
|
---|
4064 | rc = vdiChangeImageMode(pImage->pPrev, true);
|
---|
4065 | }
|
---|
4066 |
|
---|
4067 | if (VBOX_FAILURE(rc))
|
---|
4068 | {
|
---|
4069 | /* Failed! Close all images, can't work with VHDD at all. */
|
---|
4070 | VDIDiskCloseAllImages(pDisk);
|
---|
4071 | AssertMsgFailed(("Fatal: commit failed, rc=%Vrc\n", rc));
|
---|
4072 | }
|
---|
4073 |
|
---|
4074 | return rc;
|
---|
4075 | }
|
---|
4076 |
|
---|
4077 | /**
|
---|
4078 | * Creates and opens a new differencing image file in HDD container.
|
---|
4079 | * See comments for VDIDiskOpenImage function about differencing images.
|
---|
4080 | *
|
---|
4081 | * @returns VBox status code.
|
---|
4082 | * @param pDisk Pointer to VDI HDD container.
|
---|
4083 | * @param pszFilename Name of the image file to create and open.
|
---|
4084 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
4085 | * @param pfnProgress Progress callback. Optional.
|
---|
4086 | * @param pvUser User argument for the progress callback.
|
---|
4087 | * @remark Only used by tstVDI.
|
---|
4088 | */
|
---|
4089 | IDER3DECL(int) VDIDiskCreateOpenDifferenceImage(PVDIDISK pDisk, const char *pszFilename,
|
---|
4090 | const char *pszComment, PFNVMPROGRESS pfnProgress,
|
---|
4091 | void *pvUser)
|
---|
4092 | {
|
---|
4093 | LogFlow(("VDIDiskCreateOpenDifferenceImage:\n"));
|
---|
4094 | /* sanity check */
|
---|
4095 | Assert(pDisk);
|
---|
4096 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
4097 | Assert(pszFilename);
|
---|
4098 |
|
---|
4099 | if (!pDisk->pLast)
|
---|
4100 | {
|
---|
4101 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
4102 | return VERR_VDI_NOT_OPENED;
|
---|
4103 | }
|
---|
4104 |
|
---|
4105 | /* Flush last parent image changes if possible. */
|
---|
4106 | vdiFlushImage(pDisk->pLast);
|
---|
4107 |
|
---|
4108 | int rc = vdiCreateImage(pszFilename,
|
---|
4109 | VDI_IMAGE_TYPE_DIFF,
|
---|
4110 | VDI_IMAGE_FLAGS_DEFAULT,
|
---|
4111 | getImageDiskSize(&pDisk->pLast->Header),
|
---|
4112 | pszComment,
|
---|
4113 | pDisk->pLast,
|
---|
4114 | pfnProgress, pvUser);
|
---|
4115 | if (VBOX_SUCCESS(rc))
|
---|
4116 | {
|
---|
4117 | rc = VDIDiskOpenImage(pDisk, pszFilename, VDI_OPEN_FLAGS_NORMAL);
|
---|
4118 | if (VBOX_FAILURE(rc))
|
---|
4119 | VDIDeleteImage(pszFilename);
|
---|
4120 | }
|
---|
4121 | LogFlow(("VDIDiskCreateOpenDifferenceImage: returns %Vrc, filename=\"%s\"\n", rc, pszFilename));
|
---|
4122 | return rc;
|
---|
4123 | }
|
---|
4124 |
|
---|
4125 | /**
|
---|
4126 | * internal: debug image dump.
|
---|
4127 | *
|
---|
4128 | * @remark Only used by tstVDI.
|
---|
4129 | */
|
---|
4130 | static void vdiDumpImage(PVDIIMAGEDESC pImage)
|
---|
4131 | {
|
---|
4132 | RTLogPrintf("Dumping VDI image \"%s\" mode=%s fOpen=%X File=%08X\n",
|
---|
4133 | pImage->szFilename,
|
---|
4134 | (pImage->fReadOnly) ? "r/o" : "r/w",
|
---|
4135 | pImage->fOpen,
|
---|
4136 | pImage->File);
|
---|
4137 | RTLogPrintf("Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
|
---|
4138 | pImage->PreHeader.u32Version,
|
---|
4139 | getImageType(&pImage->Header),
|
---|
4140 | getImageFlags(&pImage->Header),
|
---|
4141 | getImageDiskSize(&pImage->Header));
|
---|
4142 | RTLogPrintf("Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
|
---|
4143 | getImageBlockSize(&pImage->Header),
|
---|
4144 | getImageExtraBlockSize(&pImage->Header),
|
---|
4145 | getImageBlocks(&pImage->Header),
|
---|
4146 | getImageBlocksAllocated(&pImage->Header));
|
---|
4147 | RTLogPrintf("Header: offBlocks=%u offData=%u\n",
|
---|
4148 | getImageBlocksOffset(&pImage->Header),
|
---|
4149 | getImageDataOffset(&pImage->Header));
|
---|
4150 | PVDIDISKGEOMETRY pg = getImageGeometry(&pImage->Header);
|
---|
4151 | RTLogPrintf("Header: Geometry: C/H/S=%u/%u/%u cbSector=%u Mode=%u\n",
|
---|
4152 | pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector,
|
---|
4153 | getImageTranslation(&pImage->Header));
|
---|
4154 | RTLogPrintf("Header: uuidCreation={%Vuuid}\n", getImageCreationUUID(&pImage->Header));
|
---|
4155 | RTLogPrintf("Header: uuidModification={%Vuuid}\n", getImageModificationUUID(&pImage->Header));
|
---|
4156 | RTLogPrintf("Header: uuidParent={%Vuuid}\n", getImageParentUUID(&pImage->Header));
|
---|
4157 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
|
---|
4158 | RTLogPrintf("Header: uuidParentModification={%Vuuid}\n", getImageParentModificationUUID(&pImage->Header));
|
---|
4159 | RTLogPrintf("Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
|
---|
4160 | pImage->fFlags, pImage->offStartBlocks, pImage->offStartData);
|
---|
4161 | RTLogPrintf("Image: uBlockMask=%08X uShiftIndex2Offset=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
|
---|
4162 | pImage->uBlockMask,
|
---|
4163 | pImage->uShiftIndex2Offset,
|
---|
4164 | pImage->uShiftOffset2Index,
|
---|
4165 | pImage->offStartBlockData);
|
---|
4166 |
|
---|
4167 | unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
|
---|
4168 | for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
|
---|
4169 | {
|
---|
4170 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
4171 | {
|
---|
4172 | cBlocksNotFree++;
|
---|
4173 | if (pImage->paBlocks[uBlock] >= cBlocks)
|
---|
4174 | cBadBlocks++;
|
---|
4175 | }
|
---|
4176 | }
|
---|
4177 | if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
|
---|
4178 | {
|
---|
4179 | RTLogPrintf("!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
|
---|
4180 | cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
|
---|
4181 | }
|
---|
4182 | if (cBadBlocks)
|
---|
4183 | {
|
---|
4184 | RTLogPrintf("!! WARNING: %u bad blocks found !!\n",
|
---|
4185 | cBadBlocks);
|
---|
4186 | }
|
---|
4187 | }
|
---|
4188 |
|
---|
4189 | /**
|
---|
4190 | * Debug helper - dumps all opened images of HDD container into the log file.
|
---|
4191 | *
|
---|
4192 | * @param pDisk Pointer to VDI HDD container.
|
---|
4193 | * @remark Only used by tstVDI and vditool
|
---|
4194 | */
|
---|
4195 | IDER3DECL(void) VDIDiskDumpImages(PVDIDISK pDisk)
|
---|
4196 | {
|
---|
4197 | /* sanity check */
|
---|
4198 | Assert(pDisk);
|
---|
4199 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
4200 |
|
---|
4201 | RTLogPrintf("--- Dumping VDI Disk, Images=%u\n", pDisk->cImages);
|
---|
4202 | for (PVDIIMAGEDESC pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
|
---|
4203 | vdiDumpImage(pImage);
|
---|
4204 | }
|
---|
4205 |
|
---|
4206 |
|
---|
4207 | /*******************************************************************************
|
---|
4208 | * PDM interface *
|
---|
4209 | *******************************************************************************/
|
---|
4210 |
|
---|
4211 | /**
|
---|
4212 | * Construct a VBox HDD media driver instance.
|
---|
4213 | *
|
---|
4214 | * @returns VBox status.
|
---|
4215 | * @param pDrvIns The driver instance data.
|
---|
4216 | * If the registration structure is needed, pDrvIns->pDrvReg points to it.
|
---|
4217 | * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
|
---|
4218 | * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
|
---|
4219 | * iInstance it's expected to be used a bit in this function.
|
---|
4220 | */
|
---|
4221 | static DECLCALLBACK(int) vdiConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
4222 | {
|
---|
4223 | LogFlow(("vdiConstruct:\n"));
|
---|
4224 | PVDIDISK pData = PDMINS2DATA(pDrvIns, PVDIDISK);
|
---|
4225 | char *pszName; /**< The path of the disk image file. */
|
---|
4226 | bool fReadOnly; /**< True if the media is readonly. */
|
---|
4227 |
|
---|
4228 | /*
|
---|
4229 | * Init the static parts.
|
---|
4230 | */
|
---|
4231 | pDrvIns->IBase.pfnQueryInterface = vdiQueryInterface;
|
---|
4232 | pData->pDrvIns = pDrvIns;
|
---|
4233 |
|
---|
4234 | vdiInitVDIDisk(pData);
|
---|
4235 |
|
---|
4236 | /* IMedia */
|
---|
4237 | pData->IMedia.pfnRead = vdiRead;
|
---|
4238 | pData->IMedia.pfnWrite = vdiWrite;
|
---|
4239 | pData->IMedia.pfnFlush = vdiFlush;
|
---|
4240 | pData->IMedia.pfnGetSize = vdiGetSize;
|
---|
4241 | pData->IMedia.pfnGetUuid = vdiGetUuid;
|
---|
4242 | pData->IMedia.pfnIsReadOnly = vdiIsReadOnly;
|
---|
4243 | pData->IMedia.pfnBiosGetGeometry = vdiBiosGetGeometry;
|
---|
4244 | pData->IMedia.pfnBiosSetGeometry = vdiBiosSetGeometry;
|
---|
4245 | pData->IMedia.pfnBiosGetTranslation = vdiBiosGetTranslation;
|
---|
4246 | pData->IMedia.pfnBiosSetTranslation = vdiBiosSetTranslation;
|
---|
4247 |
|
---|
4248 | #if 1 /** @todo someone review this! it's a shot in the dark from my side. */
|
---|
4249 | /*
|
---|
4250 | * Validate configuration and find the great to the level of umpteen grandparent.
|
---|
4251 | * The parents are found in the 'Parent' subtree, so it's sorta up side down
|
---|
4252 | * from the image dependency tree.
|
---|
4253 | */
|
---|
4254 | unsigned iLevel = 0;
|
---|
4255 | PCFGMNODE pCurNode = pCfgHandle;
|
---|
4256 | for (;;)
|
---|
4257 | {
|
---|
4258 | if (!CFGMR3AreValuesValid(pCfgHandle, "Path\0ReadOnly\0"))
|
---|
4259 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
4260 |
|
---|
4261 | PCFGMNODE pParent = CFGMR3GetChild(pCurNode, "Parent");
|
---|
4262 | if (!pParent)
|
---|
4263 | break;
|
---|
4264 | pCurNode = pParent;
|
---|
4265 | iLevel++;
|
---|
4266 | }
|
---|
4267 |
|
---|
4268 | /*
|
---|
4269 | * Open the images.
|
---|
4270 | */
|
---|
4271 | int rc = VINF_SUCCESS;
|
---|
4272 | while (pCurNode && VBOX_SUCCESS(rc))
|
---|
4273 | {
|
---|
4274 | /*
|
---|
4275 | * Read the image configuration.
|
---|
4276 | */
|
---|
4277 | int rc = CFGMR3QueryStringAlloc(pCurNode, "Path", &pszName);
|
---|
4278 | if (VBOX_FAILURE(rc))
|
---|
4279 | return PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
4280 | N_("VHDD: Configuration error: Querying \"Path\" as string failed"));
|
---|
4281 |
|
---|
4282 | rc = CFGMR3QueryBool(pCfgHandle, "ReadOnly", &fReadOnly);
|
---|
4283 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
4284 | fReadOnly = false;
|
---|
4285 | else if (VBOX_FAILURE(rc))
|
---|
4286 | {
|
---|
4287 | MMR3HeapFree(pszName);
|
---|
4288 | return PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
4289 | N_("VHDD: Configuration error: Querying \"ReadOnly\" as boolean failed"));
|
---|
4290 | }
|
---|
4291 |
|
---|
4292 | /*
|
---|
4293 | * Open the image.
|
---|
4294 | */
|
---|
4295 | rc = VDIDiskOpenImage(pData, pszName, fReadOnly ? VDI_OPEN_FLAGS_READONLY
|
---|
4296 | : VDI_OPEN_FLAGS_NORMAL);
|
---|
4297 | if (VBOX_SUCCESS(rc))
|
---|
4298 | Log(("vdiConstruct: %d - Opened '%s' in %s mode\n",
|
---|
4299 | iLevel, pszName, VDIDiskIsReadOnly(pData) ? "read-only" : "read-write"));
|
---|
4300 | else
|
---|
4301 | AssertMsgFailed(("Failed to open image '%s' rc=%Vrc\n", pszName, rc));
|
---|
4302 | MMR3HeapFree(pszName);
|
---|
4303 |
|
---|
4304 | /* next */
|
---|
4305 | iLevel--;
|
---|
4306 | pCurNode = CFGMR3GetParent(pCurNode);
|
---|
4307 | }
|
---|
4308 |
|
---|
4309 | /* On failure, vdiDestruct will be called, so no need to clean up here. */
|
---|
4310 |
|
---|
4311 | #else /* old */
|
---|
4312 | /*
|
---|
4313 | * Validate and read top level configuration.
|
---|
4314 | */
|
---|
4315 | int rc = CFGMR3QueryStringAlloc(pCfgHandle, "Path", &pszName);
|
---|
4316 | if (VBOX_FAILURE(rc))
|
---|
4317 | return PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
4318 | N_("VHDD: Configuration error: Querying \"Path\" as string failed"));
|
---|
4319 |
|
---|
4320 | rc = CFGMR3QueryBool(pCfgHandle, "ReadOnly", &fReadOnly);
|
---|
4321 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
4322 | fReadOnly = false;
|
---|
4323 | else if (VBOX_FAILURE(rc))
|
---|
4324 | {
|
---|
4325 | MMR3HeapFree(pszName);
|
---|
4326 | return PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
4327 | N_("VHDD: Configuration error: Querying \"ReadOnly\" as boolean failed"));
|
---|
4328 | }
|
---|
4329 |
|
---|
4330 | /*
|
---|
4331 | * Open the image.
|
---|
4332 | */
|
---|
4333 | rc = VDIDiskOpenImage(pData, pszName, fReadOnly ? VDI_OPEN_FLAGS_READONLY
|
---|
4334 | : VDI_OPEN_FLAGS_NORMAL);
|
---|
4335 | if (VBOX_SUCCESS(rc))
|
---|
4336 | Log(("vdiConstruct: Opened '%s' in %s mode\n", pszName, VDIDiskIsReadOnly(pData) ? "read-only" : "read-write"));
|
---|
4337 | else
|
---|
4338 | AssertMsgFailed(("Failed to open image '%s' rc=%Vrc\n", pszName, rc));
|
---|
4339 |
|
---|
4340 | MMR3HeapFree(pszName);
|
---|
4341 | pszName = NULL;
|
---|
4342 | #endif
|
---|
4343 |
|
---|
4344 | if (rc == VERR_ACCESS_DENIED)
|
---|
4345 | /* This should never happen here since this case is covered by Console::PowerUp */
|
---|
4346 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
4347 | N_("Cannot open virtual disk image '%s' for %s access"),
|
---|
4348 | pszName, fReadOnly ? "readonly" : "read/write");
|
---|
4349 |
|
---|
4350 | return rc;
|
---|
4351 | }
|
---|
4352 |
|
---|
4353 | /**
|
---|
4354 | * Destruct a driver instance.
|
---|
4355 | *
|
---|
4356 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
4357 | * resources can be freed correctly.
|
---|
4358 | *
|
---|
4359 | * @param pDrvIns The driver instance data.
|
---|
4360 | */
|
---|
4361 | static DECLCALLBACK(void) vdiDestruct(PPDMDRVINS pDrvIns)
|
---|
4362 | {
|
---|
4363 | LogFlow(("vdiDestruct:\n"));
|
---|
4364 | PVDIDISK pData = PDMINS2DATA(pDrvIns, PVDIDISK);
|
---|
4365 | VDIDiskCloseAllImages(pData);
|
---|
4366 | }
|
---|
4367 |
|
---|
4368 | /**
|
---|
4369 | * When the VM has been suspended we'll change the image mode to read-only
|
---|
4370 | * so that main and others can read the VDIs. This is important when
|
---|
4371 | * saving state and so forth.
|
---|
4372 | *
|
---|
4373 | * @param pDrvIns The driver instance data.
|
---|
4374 | */
|
---|
4375 | static DECLCALLBACK(void) vdiSuspend(PPDMDRVINS pDrvIns)
|
---|
4376 | {
|
---|
4377 | LogFlow(("vdiSuspend:\n"));
|
---|
4378 | #if 1 // #ifdef DEBUG_dmik
|
---|
4379 | PVDIDISK pData = PDMINS2DATA(pDrvIns, PVDIDISK);
|
---|
4380 | if (!(pData->pLast->fOpen & VDI_OPEN_FLAGS_READONLY))
|
---|
4381 | {
|
---|
4382 | int rc = vdiChangeImageMode(pData->pLast, true);
|
---|
4383 | AssertRC(rc);
|
---|
4384 | }
|
---|
4385 | #endif
|
---|
4386 | }
|
---|
4387 |
|
---|
4388 | /**
|
---|
4389 | * Before the VM resumes we'll have to undo the read-only mode change
|
---|
4390 | * done in vdiSuspend.
|
---|
4391 | *
|
---|
4392 | * @param pDrvIns The driver instance data.
|
---|
4393 | */
|
---|
4394 | static DECLCALLBACK(void) vdiResume(PPDMDRVINS pDrvIns)
|
---|
4395 | {
|
---|
4396 | LogFlow(("vdiSuspend:\n"));
|
---|
4397 | #if 1 //#ifdef DEBUG_dmik
|
---|
4398 | PVDIDISK pData = PDMINS2DATA(pDrvIns, PVDIDISK);
|
---|
4399 | if (!(pData->pLast->fOpen & VDI_OPEN_FLAGS_READONLY))
|
---|
4400 | {
|
---|
4401 | int rc = vdiChangeImageMode(pData->pLast, false);
|
---|
4402 | AssertRC(rc);
|
---|
4403 | }
|
---|
4404 | #endif
|
---|
4405 | }
|
---|
4406 |
|
---|
4407 |
|
---|
4408 | /** @copydoc PDMIMEDIA::pfnGetSize */
|
---|
4409 | static DECLCALLBACK(uint64_t) vdiGetSize(PPDMIMEDIA pInterface)
|
---|
4410 | {
|
---|
4411 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4412 | uint64_t cb = VDIDiskGetSize(pData);
|
---|
4413 | LogFlow(("vdiGetSize: returns %#llx (%llu)\n", cb, cb));
|
---|
4414 | return cb;
|
---|
4415 | }
|
---|
4416 |
|
---|
4417 | /**
|
---|
4418 | * Get stored media geometry - BIOS property.
|
---|
4419 | *
|
---|
4420 | * @see PDMIMEDIA::pfnBiosGetGeometry for details.
|
---|
4421 | */
|
---|
4422 | static DECLCALLBACK(int) vdiBiosGetGeometry(PPDMIMEDIA pInterface, uint32_t *pcCylinders, uint32_t *pcHeads, uint32_t *pcSectors)
|
---|
4423 | {
|
---|
4424 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4425 | int rc = VDIDiskGetGeometry(pData, pcCylinders, pcHeads, pcSectors);
|
---|
4426 | if (VBOX_SUCCESS(rc))
|
---|
4427 | {
|
---|
4428 | LogFlow(("vdiBiosGetGeometry: returns VINF_SUCCESS\n"));
|
---|
4429 | return VINF_SUCCESS;
|
---|
4430 | }
|
---|
4431 | Log(("vdiBiosGetGeometry: The Bios geometry data was not available.\n"));
|
---|
4432 | return VERR_PDM_GEOMETRY_NOT_SET;
|
---|
4433 | }
|
---|
4434 |
|
---|
4435 | /**
|
---|
4436 | * Set stored media geometry - BIOS property.
|
---|
4437 | *
|
---|
4438 | * @see PDMIMEDIA::pfnBiosSetGeometry for details.
|
---|
4439 | */
|
---|
4440 | static DECLCALLBACK(int) vdiBiosSetGeometry(PPDMIMEDIA pInterface, uint32_t cCylinders, uint32_t cHeads, uint32_t cSectors)
|
---|
4441 | {
|
---|
4442 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4443 | int rc = VDIDiskSetGeometry(pData, cCylinders, cHeads, cSectors);
|
---|
4444 | LogFlow(("vdiBiosSetGeometry: returns %Vrc (%d,%d,%d)\n", rc, cCylinders, cHeads, cSectors));
|
---|
4445 | return rc;
|
---|
4446 | }
|
---|
4447 |
|
---|
4448 | /**
|
---|
4449 | * Read bits.
|
---|
4450 | *
|
---|
4451 | * @see PDMIMEDIA::pfnRead for details.
|
---|
4452 | */
|
---|
4453 | static DECLCALLBACK(int) vdiRead(PPDMIMEDIA pInterface, uint64_t off, void *pvBuf, size_t cbRead)
|
---|
4454 | {
|
---|
4455 | LogFlow(("vdiRead: off=%#llx pvBuf=%p cbRead=%d\n", off, pvBuf, cbRead));
|
---|
4456 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4457 | int rc = VDIDiskRead(pData, off, pvBuf, cbRead);
|
---|
4458 | if (VBOX_SUCCESS(rc))
|
---|
4459 | Log2(("vdiRead: off=%#llx pvBuf=%p cbRead=%d\n"
|
---|
4460 | "%.*Vhxd\n",
|
---|
4461 | off, pvBuf, cbRead, cbRead, pvBuf));
|
---|
4462 | LogFlow(("vdiRead: returns %Vrc\n", rc));
|
---|
4463 | return rc;
|
---|
4464 | }
|
---|
4465 |
|
---|
4466 | /**
|
---|
4467 | * Write bits.
|
---|
4468 | *
|
---|
4469 | * @see PDMIMEDIA::pfnWrite for details.
|
---|
4470 | */
|
---|
4471 | static DECLCALLBACK(int) vdiWrite(PPDMIMEDIA pInterface, uint64_t off, const void *pvBuf, size_t cbWrite)
|
---|
4472 | {
|
---|
4473 | LogFlow(("vdiWrite: off=%#llx pvBuf=%p cbWrite=%d\n", off, pvBuf, cbWrite));
|
---|
4474 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4475 | Log2(("vdiWrite: off=%#llx pvBuf=%p cbWrite=%d\n"
|
---|
4476 | "%.*Vhxd\n",
|
---|
4477 | off, pvBuf, cbWrite, cbWrite, pvBuf));
|
---|
4478 | int rc = VDIDiskWrite(pData, off, pvBuf, cbWrite);
|
---|
4479 | LogFlow(("vdiWrite: returns %Vrc\n", rc));
|
---|
4480 | return rc;
|
---|
4481 | }
|
---|
4482 |
|
---|
4483 | /**
|
---|
4484 | * Flush bits to media.
|
---|
4485 | *
|
---|
4486 | * @see PDMIMEDIA::pfnFlush for details.
|
---|
4487 | */
|
---|
4488 | static DECLCALLBACK(int) vdiFlush(PPDMIMEDIA pInterface)
|
---|
4489 | {
|
---|
4490 | LogFlow(("vdiFlush:\n"));
|
---|
4491 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4492 | vdiFlushImage(pData->pLast);
|
---|
4493 | int rc = VINF_SUCCESS;
|
---|
4494 | LogFlow(("vdiFlush: returns %Vrc\n", rc));
|
---|
4495 | return rc;
|
---|
4496 | }
|
---|
4497 |
|
---|
4498 | /** @copydoc PDMIMEDIA::pfnGetUuid */
|
---|
4499 | static DECLCALLBACK(int) vdiGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
|
---|
4500 | {
|
---|
4501 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4502 | int rc = VDIDiskGetImageUuid(pData, 0, pUuid);
|
---|
4503 | LogFlow(("vdiGetUuid: returns %Vrc ({%Vuuid})\n", rc, pUuid));
|
---|
4504 | return rc;
|
---|
4505 | }
|
---|
4506 |
|
---|
4507 | /** @copydoc PDMIMEDIA::pfnIsReadOnly */
|
---|
4508 | static DECLCALLBACK(bool) vdiIsReadOnly(PPDMIMEDIA pInterface)
|
---|
4509 | {
|
---|
4510 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4511 | LogFlow(("vdiIsReadOnly: returns %d\n", VDIDiskIsReadOnly(pData)));
|
---|
4512 | return VDIDiskIsReadOnly(pData);
|
---|
4513 | }
|
---|
4514 |
|
---|
4515 | /** @copydoc PDMIMEDIA::pfnBiosGetTranslation */
|
---|
4516 | static DECLCALLBACK(int) vdiBiosGetTranslation(PPDMIMEDIA pInterface,
|
---|
4517 | PPDMBIOSTRANSLATION penmTranslation)
|
---|
4518 | {
|
---|
4519 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4520 | int rc = VDIDiskGetTranslation(pData, penmTranslation);
|
---|
4521 | LogFlow(("vdiBiosGetTranslation: returns %Vrc (%d)\n", rc, *penmTranslation));
|
---|
4522 | return rc;
|
---|
4523 | }
|
---|
4524 |
|
---|
4525 | /** @copydoc PDMIMEDIA::pfnBiosSetTranslation */
|
---|
4526 | static DECLCALLBACK(int) vdiBiosSetTranslation(PPDMIMEDIA pInterface,
|
---|
4527 | PDMBIOSTRANSLATION enmTranslation)
|
---|
4528 | {
|
---|
4529 | PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
|
---|
4530 | int rc = VDIDiskSetTranslation(pData, enmTranslation);
|
---|
4531 | LogFlow(("vdiBiosSetTranslation: returns %Vrc (%d)\n", rc, enmTranslation));
|
---|
4532 | return rc;
|
---|
4533 | }
|
---|
4534 |
|
---|
4535 |
|
---|
4536 | /**
|
---|
4537 | * Queries an interface to the driver.
|
---|
4538 | *
|
---|
4539 | * @returns Pointer to interface.
|
---|
4540 | * @returns NULL if the interface was not supported by the driver.
|
---|
4541 | * @param pInterface Pointer to this interface structure.
|
---|
4542 | * @param enmInterface The requested interface identification.
|
---|
4543 | * @thread Any thread.
|
---|
4544 | */
|
---|
4545 | static DECLCALLBACK(void *) vdiQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
|
---|
4546 | {
|
---|
4547 | PPDMDRVINS pDrvIns = PDMIBASE_2_DRVINS(pInterface);
|
---|
4548 | PVDIDISK pData = PDMINS2DATA(pDrvIns, PVDIDISK);
|
---|
4549 | switch (enmInterface)
|
---|
4550 | {
|
---|
4551 | case PDMINTERFACE_BASE:
|
---|
4552 | return &pDrvIns->IBase;
|
---|
4553 | case PDMINTERFACE_MEDIA:
|
---|
4554 | return &pData->IMedia;
|
---|
4555 | default:
|
---|
4556 | return NULL;
|
---|
4557 | }
|
---|
4558 | }
|
---|
4559 |
|
---|
4560 |
|
---|
4561 | /**
|
---|
4562 | * VBox HDD driver registration record.
|
---|
4563 | */
|
---|
4564 | const PDMDRVREG g_DrvVBoxHDD =
|
---|
4565 | {
|
---|
4566 | /* u32Version */
|
---|
4567 | PDM_DRVREG_VERSION,
|
---|
4568 | /* szDriverName */
|
---|
4569 | "VBoxHDD",
|
---|
4570 | /* pszDescription */
|
---|
4571 | "VBoxHDD media driver.",
|
---|
4572 | /* fFlags */
|
---|
4573 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
4574 | /* fClass. */
|
---|
4575 | PDM_DRVREG_CLASS_MEDIA,
|
---|
4576 | /* cMaxInstances */
|
---|
4577 | ~0,
|
---|
4578 | /* cbInstance */
|
---|
4579 | sizeof(VDIDISK),
|
---|
4580 | /* pfnConstruct */
|
---|
4581 | vdiConstruct,
|
---|
4582 | /* pfnDestruct */
|
---|
4583 | vdiDestruct,
|
---|
4584 | /* pfnIOCtl */
|
---|
4585 | NULL,
|
---|
4586 | /* pfnPowerOn */
|
---|
4587 | NULL,
|
---|
4588 | /* pfnReset */
|
---|
4589 | NULL,
|
---|
4590 | /* pfnSuspend */
|
---|
4591 | vdiSuspend,
|
---|
4592 | /* pfnResume */
|
---|
4593 | vdiResume,
|
---|
4594 | /* pfnDetach */
|
---|
4595 | NULL
|
---|
4596 | };
|
---|