1 /** $Id: VDICore.h 25896 2007-11-01 16:13:17Z frank $ */
3 * Virtual Disk Image (VDI), Core Code Header (internal).
7 * Copyright (C) 2006-2007 innotek GmbH
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
21 /*******************************************************************************
23 *******************************************************************************/
28 #include "func_defines.h"
29 #include "const_defines.h"
33 /*******************************************************************************
34 * Constants And Macros, Structures and Typedefs *
35 *******************************************************************************/
38 * Currently we support only 512 bytes sectors.
40 #define VDI_GEOMETRY_SECTOR_SIZE (512)
42 #define VDI_GEOMETRY_SECTOR_SHIFT (9)
48 typedef struct VDIDISKGEOMETRY
54 /** Sectors per track. */
56 /** Sector size. (bytes per sector) */
58 } VDIDISKGEOMETRY
, *PVDIDISKGEOMETRY
;
61 /** Image signature. */
62 #define VDI_IMAGE_SIGNATURE (0xbeda107f)
65 * Pre-Header to be stored in image file - used for version control.
68 typedef struct VDIPREHEADER
70 /** Just text info about image type, for eyes only. */
72 /** The image signature (VDI_IMAGE_SIGNATURE). */
73 uint32_t u32Signature
;
74 /** The image version (VDI_IMAGE_VERSION). */
76 } VDIPREHEADER
, *PVDIPREHEADER
;
80 * Size of szComment field of HDD image header.
82 #define VDI_IMAGE_COMMENT_SIZE 256
85 * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 0.
86 * Prepended by VDIPREHEADER.
89 typedef struct VDIHEADER0
91 /** The image type (VDI_IMAGE_TYPE_*). */
93 /** Image flags (VDI_IMAGE_FLAGS_*). */
95 /** Image comment. (UTF-8) */
96 char szComment
[VDI_IMAGE_COMMENT_SIZE
];
97 /** Image geometry. */
98 VDIDISKGEOMETRY Geometry
;
99 /** Size of disk (in bytes). */
101 /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) */
103 /** Number of blocks. */
105 /** Number of allocated blocks. */
106 uint32_t cBlocksAllocated
;
107 /** UUID of image. */
109 /** UUID of image's last modification. */
111 /** Only for secondary images - UUID of primary image. */
113 } VDIHEADER0
, *PVDIHEADER0
;
117 * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 1.
118 * Prepended by VDIPREHEADER.
121 typedef struct VDIHEADER1
123 /** Size of this structure in bytes. */
125 /** The image type (VDI_IMAGE_TYPE_*). */
127 /** Image flags (VDI_IMAGE_FLAGS_*). */
129 /** Image comment. (UTF-8) */
130 char szComment
[VDI_IMAGE_COMMENT_SIZE
];
131 /** Offset of Blocks array from the begining of image file.
132 * Should be sector-aligned for HDD access optimization. */
134 /** Offset of image data from the begining of image file.
135 * Should be sector-aligned for HDD access optimization. */
137 /** Image geometry. */
138 VDIDISKGEOMETRY Geometry
;
139 /** BIOS HDD translation mode, see PDMBIOSTRANSLATION. */
140 uint32_t u32Translation
;
141 /** Size of disk (in bytes). */
143 /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) Should be a power of 2! */
145 /** Size of additional service information of every data block.
146 * Prepended before block data. May be 0.
147 * Should be a power of 2 and sector-aligned for optimization reasons. */
148 uint32_t cbBlockExtra
;
149 /** Number of blocks. */
151 /** Number of allocated blocks. */
152 uint32_t cBlocksAllocated
;
153 /** UUID of image. */
155 /** UUID of image's last modification. */
157 /** Only for secondary images - UUID of previous image. */
159 /** Only for secondary images - UUID of previous image's last modification. */
160 RTUUID uuidParentModify
;
161 } VDIHEADER1
, *PVDIHEADER1
;
165 * Header structure for all versions.
167 typedef struct VDIHEADER
175 } VDIHEADER
, *PVDIHEADER
;
177 /** Block 'pointer'. */
178 typedef uint32_t VDIIMAGEBLOCKPOINTER
;
179 /** Pointer to a block 'pointer'. */
180 typedef VDIIMAGEBLOCKPOINTER
*PVDIIMAGEBLOCKPOINTER
;
183 * Block marked as free is not allocated in image file, read from this
184 * block may returns any random data.
186 #define VDI_IMAGE_BLOCK_FREE ((VDIIMAGEBLOCKPOINTER)~0)
189 * Block marked as zero is not allocated in image file, read from this
190 * block returns zeroes.
192 #define VDI_IMAGE_BLOCK_ZERO ((VDIIMAGEBLOCKPOINTER)~1)
195 * Block 'pointer' >= VDI_IMAGE_BLOCK_UNALLOCATED indicates block is not
196 * allocated in image file.
198 #define VDI_IMAGE_BLOCK_UNALLOCATED (VDI_IMAGE_BLOCK_ZERO)
199 #define IS_VDI_IMAGE_BLOCK_ALLOCATED(bp) (bp < VDI_IMAGE_BLOCK_UNALLOCATED)
201 #define GET_MAJOR_HEADER_VERSION(ph) (VDI_GET_VERSION_MAJOR((ph)->uVersion))
202 #define GET_MINOR_HEADER_VERSION(ph) (VDI_GET_VERSION_MINOR((ph)->uVersion))
205 /*******************************************************************************
206 * Internal Functions for header access *
207 *******************************************************************************/
208 DECLINLINE(VDIIMAGETYPE
) getImageType(PVDIHEADER ph
)
210 switch (GET_MAJOR_HEADER_VERSION(ph
))
212 case 0: return (VDIIMAGETYPE
)ph
->u
.v0
.u32Type
;
213 case 1: return (VDIIMAGETYPE
)ph
->u
.v1
.u32Type
;
216 return (VDIIMAGETYPE
)0;
219 DECLINLINE(unsigned) getImageFlags(PVDIHEADER ph
)
221 switch (GET_MAJOR_HEADER_VERSION(ph
))
223 case 0: return ph
->u
.v0
.fFlags
;
224 case 1: return ph
->u
.v1
.fFlags
;
230 DECLINLINE(char *) getImageComment(PVDIHEADER ph
)
232 switch (GET_MAJOR_HEADER_VERSION(ph
))
234 case 0: return &ph
->u
.v0
.szComment
[0];
235 case 1: return &ph
->u
.v1
.szComment
[0];
241 DECLINLINE(unsigned) getImageBlocksOffset(PVDIHEADER ph
)
243 switch (GET_MAJOR_HEADER_VERSION(ph
))
245 case 0: return (sizeof(VDIPREHEADER
) + sizeof(VDIHEADER0
));
246 case 1: return ph
->u
.v1
.offBlocks
;
252 DECLINLINE(unsigned) getImageDataOffset(PVDIHEADER ph
)
254 switch (GET_MAJOR_HEADER_VERSION(ph
))
256 case 0: return sizeof(VDIPREHEADER
) + sizeof(VDIHEADER0
) + \
257 (ph
->u
.v0
.cBlocks
* sizeof(VDIIMAGEBLOCKPOINTER
));
258 case 1: return ph
->u
.v1
.offData
;
264 DECLINLINE(PVDIDISKGEOMETRY
) getImageGeometry(PVDIHEADER ph
)
266 switch (GET_MAJOR_HEADER_VERSION(ph
))
268 case 0: return &ph
->u
.v0
.Geometry
;
269 case 1: return &ph
->u
.v1
.Geometry
;
275 DECLINLINE(PDMBIOSTRANSLATION
) getImageTranslation(PVDIHEADER ph
)
277 switch (GET_MAJOR_HEADER_VERSION(ph
))
279 case 0: return PDMBIOSTRANSLATION_AUTO
;
280 case 1: return (PDMBIOSTRANSLATION
)ph
->u
.v1
.u32Translation
;
283 return PDMBIOSTRANSLATION_NONE
;
286 DECLINLINE(void) setImageTranslation(PVDIHEADER ph
, PDMBIOSTRANSLATION enmTranslation
)
288 switch (GET_MAJOR_HEADER_VERSION(ph
))
291 case 1: ph
->u
.v1
.u32Translation
= (uint32_t)enmTranslation
; return;
296 DECLINLINE(uint64_t) getImageDiskSize(PVDIHEADER ph
)
298 switch (GET_MAJOR_HEADER_VERSION(ph
))
300 case 0: return ph
->u
.v0
.cbDisk
;
301 case 1: return ph
->u
.v1
.cbDisk
;
307 DECLINLINE(unsigned) getImageBlockSize(PVDIHEADER ph
)
309 switch (GET_MAJOR_HEADER_VERSION(ph
))
311 case 0: return ph
->u
.v0
.cbBlock
;
312 case 1: return ph
->u
.v1
.cbBlock
;
318 DECLINLINE(unsigned) getImageExtraBlockSize(PVDIHEADER ph
)
320 switch (GET_MAJOR_HEADER_VERSION(ph
))
323 case 1: return ph
->u
.v1
.cbBlockExtra
;
329 DECLINLINE(unsigned) getImageBlocks(PVDIHEADER ph
)
331 switch (GET_MAJOR_HEADER_VERSION(ph
))
333 case 0: return ph
->u
.v0
.cBlocks
;
334 case 1: return ph
->u
.v1
.cBlocks
;
340 DECLINLINE(unsigned) getImageBlocksAllocated(PVDIHEADER ph
)
342 switch (GET_MAJOR_HEADER_VERSION(ph
))
344 case 0: return ph
->u
.v0
.cBlocksAllocated
;
345 case 1: return ph
->u
.v1
.cBlocksAllocated
;
351 DECLINLINE(void) setImageBlocksAllocated(PVDIHEADER ph
, unsigned cBlocks
)
353 switch (GET_MAJOR_HEADER_VERSION(ph
))
355 case 0: ph
->u
.v0
.cBlocksAllocated
= cBlocks
; return;
356 case 1: ph
->u
.v1
.cBlocksAllocated
= cBlocks
; return;
361 DECLINLINE(PRTUUID
) getImageCreationUUID(PVDIHEADER ph
)
363 switch (GET_MAJOR_HEADER_VERSION(ph
))
365 case 0: return &ph
->u
.v0
.uuidCreate
;
366 case 1: return &ph
->u
.v1
.uuidCreate
;
372 DECLINLINE(PRTUUID
) getImageModificationUUID(PVDIHEADER ph
)
374 switch (GET_MAJOR_HEADER_VERSION(ph
))
376 case 0: return &ph
->u
.v0
.uuidModify
;
377 case 1: return &ph
->u
.v1
.uuidModify
;
383 DECLINLINE(PRTUUID
) getImageParentUUID(PVDIHEADER ph
)
385 switch (GET_MAJOR_HEADER_VERSION(ph
))
387 case 0: return &ph
->u
.v0
.uuidLinkage
;
388 case 1: return &ph
->u
.v1
.uuidLinkage
;
394 DECLINLINE(PRTUUID
) getImageParentModificationUUID(PVDIHEADER ph
)
396 switch (GET_MAJOR_HEADER_VERSION(ph
))
398 case 1: return &ph
->u
.v1
.uuidParentModify
;
405 * Default image block size, may be changed by setBlockSize/getBlockSize.
407 * Note: for speed reasons block size should be a power of 2 !
409 #define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
412 * fModified bit flags.
414 #define VDI_IMAGE_MODIFIED_FLAG RT_BIT(0)
415 #define VDI_IMAGE_MODIFIED_FIRST RT_BIT(1)
416 #define VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE RT_BIT(2)
421 typedef struct VDIIMAGEDESC
423 /** Link to parent image descriptor, if any. */
424 struct VDIIMAGEDESC
*pPrev
;
425 /** Link to child image descriptor, if any. */
426 struct VDIIMAGEDESC
*pNext
;
429 /** True if the image is operating in readonly mode. */
431 /** Image open flags, VDI_OPEN_FLAGS_*. */
433 /** Image pre-header. */
434 VDIPREHEADER PreHeader
;
437 /** Pointer to a block array. */
438 PVDIIMAGEBLOCKPOINTER paBlocks
;
439 /** fFlags copy from image header, for speed optimization. */
441 /** Start offset of block array in image file, here for speed optimization. */
442 unsigned offStartBlocks
;
443 /** Start offset of data in image file, here for speed optimization. */
444 unsigned offStartData
;
445 /** Block mask for getting the offset into a block from a byte hdd offset. */
447 /** Block shift value for converting byte hdd offset into paBlock index. */
448 unsigned uShiftOffset2Index
;
449 /** Block shift value for converting block index into offset in image. */
450 unsigned uShiftIndex2Offset
;
451 /** Offset of data from the beginning of block. */
452 unsigned offStartBlockData
;
453 /** Image is modified flags (VDI_IMAGE_MODIFIED*). */
455 /** Container filename. (UTF-8)
456 * @todo Make this variable length to save a bunch of bytes. (low prio) */
457 char szFilename
[RTPATH_MAX
];
458 } VDIIMAGEDESC
, *PVDIIMAGEDESC
;
461 * Default work buffer size, may be changed by setBufferSize() method.
463 * For best speed performance it must be equal to image block size.
465 #define VDIDISK_DEFAULT_BUFFER_SIZE (VDI_IMAGE_DEFAULT_BLOCK_SIZE)
467 /** VDIDISK Signature. */
468 #define VDIDISK_SIGNATURE (0xbedafeda)
471 * VBox HDD Container main structure, private part.
475 /** Structure signature (VDIDISK_SIGNATURE). */
476 uint32_t u32Signature
;
478 /** Number of opened images. */
484 /** Last opened image in the chain.
485 * The same as pBase if only one image is used or the last opened diff image. */
488 /** Default block size for newly created images. */
491 /** Working buffer size, allocated only while committing data,
492 * copying block from primary image to secondary and saving previously
493 * zero block. Buffer deallocated after operation complete.
494 * @remark For best performance buffer size must be equal to image's
495 * block size, however it may be decreased for memory saving.
499 /** Flag whether zero writes should be handled normally or optimized
500 * away if possible. */
501 bool fHonorZeroWrites
;
503 /** The media interface. */
505 /** Pointer to the driver instance. */
509 * VBox VDI disk Container main structure.
511 /* Forward declaration, VDIDISK structure is visible only inside VDI module. */
513 typedef struct VDIDISK VDIDISK
;
514 typedef VDIDISK
*PVDIDISK
;
517 /*******************************************************************************
518 * Internal Functions *
519 *******************************************************************************/
522 void vdiInitVDIDisk(PVDIDISK pDisk
);
523 void vdiFlushImage(PVDIIMAGEDESC pImage
);
524 int vdiChangeImageMode(PVDIIMAGEDESC pImage
, bool fReadOnly
);