VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/VDIHDDCore.cpp@ 9113

Last change on this file since 9113 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.4 KB
Line 
1/** @file
2 * Virtual Disk Image (VDI), Core Code.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
17 * Clara, CA 95054 USA or visit http://www.sun.com if you need
18 * additional information or have any questions.
19 */
20
21/*******************************************************************************
22* Header Files *
23*******************************************************************************/
24#define LOG_GROUP LOG_GROUP_VD_VDI
25#include "VBoxHDD-newInternal.h"
26#define VBOX_VDICORE_VD /* Signal that the header is included from here. */
27#include "VDICore.h"
28#include <VBox/err.h>
29
30#include <VBox/log.h>
31#include <iprt/alloc.h>
32#include <iprt/assert.h>
33#include <iprt/uuid.h>
34#include <iprt/file.h>
35#include <iprt/string.h>
36#include <iprt/asm.h>
37
38#define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
39
40/*******************************************************************************
41* Internal Functions *
42*******************************************************************************/
43static unsigned getPowerOfTwo(unsigned uNumber);
44static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
45static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
46static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType,
47 uint32_t uImageFlags, const char *pszComment,
48 uint64_t cbDisk, uint32_t cbBlock,
49 uint32_t cbBlockExtra);
50static int vdiValidateHeader(PVDIHEADER pHeader);
51static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
52static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
53static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
54static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete);
55
56
57/**
58 * Internal: signal an error to the frontend.
59 */
60DECLINLINE(int) vdiError(PVDIIMAGEDESC pImage, int rc, RT_SRC_POS_DECL,
61 const char *pszFormat, ...)
62{
63 va_list va;
64 va_start(va, pszFormat);
65 if (pImage->pfnError)
66 pImage->pfnError(pImage->pvErrorUser, rc, RT_SRC_POS_ARGS,
67 pszFormat, va);
68 va_end(va);
69 return rc;
70}
71
72
73/**
74 * internal: return power of 2 or 0 if num error.
75 */
76static unsigned getPowerOfTwo(unsigned uNumber)
77{
78 if (uNumber == 0)
79 return 0;
80 unsigned uPower2 = 0;
81 while ((uNumber & 1) == 0)
82 {
83 uNumber >>= 1;
84 uPower2++;
85 }
86 return uNumber == 1 ? uPower2 : 0;
87}
88
89
90/**
91 * Internal: Init VDI preheader.
92 */
93static void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
94{
95 pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
96 pPreHdr->u32Version = VDI_IMAGE_VERSION;
97 memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
98 strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo));
99}
100
101/**
102 * Internal: check VDI preheader.
103 */
104static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
105{
106 if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
107 return VERR_VDI_INVALID_SIGNATURE;
108
109 if ( VDI_GET_VERSION_MAJOR(pPreHdr->u32Version) != VDI_IMAGE_VERSION_MAJOR
110 && pPreHdr->u32Version != 0x00000002) /* old version. */
111 return VERR_VDI_UNSUPPORTED_VERSION;
112
113 return VINF_SUCCESS;
114}
115
116/**
117 * Internal: Init VDI header. Always use latest header version.
118 * @param pHeader Assumes it was initially initialized to all zeros.
119 */
120static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType,
121 uint32_t uImageFlags, const char *pszComment,
122 uint64_t cbDisk, uint32_t cbBlock,
123 uint32_t cbBlockExtra)
124{
125 pHeader->uVersion = VDI_IMAGE_VERSION;
126 pHeader->u.v1.cbHeader = sizeof(VDIHEADER1);
127 pHeader->u.v1.u32Type = (uint32_t)( enmType == VD_IMAGE_TYPE_NORMAL
128 ? VDI_IMAGE_TYPE_NORMAL
129 : VDI_IMAGE_TYPE_DIFF);
130 pHeader->u.v1.fFlags = uImageFlags;
131#ifdef VBOX_STRICT
132 char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
133 Assert(!memcmp(pHeader->u.v1.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
134#endif
135 pHeader->u.v1.szComment[0] = '\0';
136 if (pszComment)
137 {
138 AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1.szComment),
139 ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
140 strncat(pHeader->u.v1.szComment, pszComment, sizeof(pHeader->u.v1.szComment));
141 }
142
143 /* Mark the legacy geometry not-calculated. */
144 pHeader->u.v1.LegacyGeometry.cCylinders = 0;
145 pHeader->u.v1.LegacyGeometry.cHeads = 0;
146 pHeader->u.v1.LegacyGeometry.cSectors = 0;
147 pHeader->u.v1.LegacyGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
148 pHeader->u.v1.u32Dummy = 0; /* used to be the translation value */
149
150 pHeader->u.v1.cbDisk = cbDisk;
151 pHeader->u.v1.cbBlock = cbBlock;
152 pHeader->u.v1.cBlocks = (uint32_t)(cbDisk / cbBlock);
153 if (cbDisk % cbBlock)
154 pHeader->u.v1.cBlocks++;
155 pHeader->u.v1.cbBlockExtra = cbBlockExtra;
156 pHeader->u.v1.cBlocksAllocated = 0;
157
158 /* Init offsets. */
159 pHeader->u.v1.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1), VDI_GEOMETRY_SECTOR_SIZE);
160 pHeader->u.v1.offData = RT_ALIGN_32(pHeader->u.v1.offBlocks + (pHeader->u.v1.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), VDI_GEOMETRY_SECTOR_SIZE);
161
162 /* Init uuids. */
163 RTUuidCreate(&pHeader->u.v1.uuidCreate);
164 RTUuidClear(&pHeader->u.v1.uuidModify);
165 RTUuidClear(&pHeader->u.v1.uuidLinkage);
166 RTUuidClear(&pHeader->u.v1.uuidParentModify);
167
168 /* Mark LCHS geometry not-calculated. */
169 pHeader->u.v1plus.LCHSGeometry.cCylinders = 0;
170 pHeader->u.v1plus.LCHSGeometry.cHeads = 0;
171 pHeader->u.v1plus.LCHSGeometry.cSectors = 0;
172 pHeader->u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
173}
174
175/**
176 * Internal: Check VDI header.
177 */
178static int vdiValidateHeader(PVDIHEADER pHeader)
179{
180 /* Check version-dependend header parameters. */
181 switch (GET_MAJOR_HEADER_VERSION(pHeader))
182 {
183 case 0:
184 {
185 /* Old header version. */
186 break;
187 }
188 case 1:
189 {
190 /* Current header version. */
191
192 if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
193 {
194 LogRel(("VDI: v1 header size wrong (%d < %d)\n",
195 pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
196 return VERR_VDI_INVALID_HEADER;
197 }
198
199 if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
200 {
201 LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
202 getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
203 return VERR_VDI_INVALID_HEADER;
204 }
205
206 if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
207 {
208 LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
209 getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
210 return VERR_VDI_INVALID_HEADER;
211 }
212
213 break;
214 }
215 default:
216 /* Unsupported. */
217 return VERR_VDI_UNSUPPORTED_VERSION;
218 }
219
220 /* Check common header parameters. */
221
222 bool fFailed = false;
223
224 if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
225 || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
226 {
227 LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
228 fFailed = true;
229 }
230
231 if (getImageFlags(pHeader) & ~VD_VDI_IMAGE_FLAGS_MASK)
232 {
233 LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
234 fFailed = true;
235 }
236
237 if ( getImageLCHSGeometry(pHeader)
238 && (getImageLCHSGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
239 {
240 LogRel(("VDI: wrong sector size (%d != %d)\n",
241 (getImageLCHSGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
242 fFailed = true;
243 }
244
245 if ( getImageDiskSize(pHeader) == 0
246 || getImageBlockSize(pHeader) == 0
247 || getImageBlocks(pHeader) == 0
248 || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
249 {
250 LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
251 getImageDiskSize(pHeader), getImageBlockSize(pHeader),
252 getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
253 fFailed = true;
254 }
255
256 if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
257 {
258 LogRel(("VDI: too many blocks allocated (%d > %d)\n"
259 " blocksize=%d disksize=%lld\n",
260 getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
261 getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
262 fFailed = true;
263 }
264
265 if ( getImageExtraBlockSize(pHeader) != 0
266 && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
267 {
268 LogRel(("VDI: wrong extra size (%d, %d)\n",
269 getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
270 fFailed = true;
271 }
272
273 if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
274 {
275 LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
276 getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
277 fFailed = true;
278 }
279
280 if (RTUuidIsNull(getImageCreationUUID(pHeader)))
281 {
282 LogRel(("VDI: uuid of creator is 0\n"));
283 fFailed = true;
284 }
285
286 if (RTUuidIsNull(getImageModificationUUID(pHeader)))
287 {
288 LogRel(("VDI: uuid of modificator is 0\n"));
289 fFailed = true;
290 }
291
292 return fFailed ? VERR_VDI_INVALID_HEADER : VINF_SUCCESS;
293}
294
295/**
296 * Internal: Set up VDIIMAGEDESC structure by image header.
297 */
298static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
299{
300 pImage->uImageFlags = getImageFlags(&pImage->Header);
301 pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
302 pImage->offStartData = getImageDataOffset(&pImage->Header);
303 pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
304 pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
305 pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
306 pImage->cbTotalBlockData = pImage->offStartBlockData
307 + getImageBlockSize(&pImage->Header);
308}
309
310/**
311 * Internal: Create VDI image file.
312 */
313static int vdiCreateImage(PVDIIMAGEDESC pImage, VDIMAGETYPE enmType,
314 uint64_t cbSize, unsigned uImageFlags,
315 const char *pszComment,
316 PCPDMMEDIAGEOMETRY pPCHSGeometry,
317 PCPDMMEDIAGEOMETRY pLCHSGeometry,
318 PFNVMPROGRESS pfnProgress, void *pvUser,
319 unsigned uPercentStart, unsigned uPercentSpan)
320{
321 int rc;
322 RTFILE File;
323 uint64_t cbTotal;
324 uint64_t cbFill;
325 uint64_t uOff;
326
327 /* Special check for comment length. */
328 if ( VALID_PTR(pszComment)
329 && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
330 {
331 rc = vdiError(pImage, VERR_VDI_COMMENT_TOO_LONG, RT_SRC_POS, N_("VDI: comment is too long for '%s'"), pImage->pszFilename);
332 goto out;
333 }
334 Assert(VALID_PTR(pPCHSGeometry));
335 Assert(VALID_PTR(pLCHSGeometry));
336
337 vdiInitPreHeader(&pImage->PreHeader);
338 vdiInitHeader(&pImage->Header, enmType, uImageFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0);
339 /* Save PCHS geometry. Not much work, and makes the flow of information
340 * quite a bit clearer - relying on the higher level isn't obvious. */
341 pImage->PCHSGeometry = *pPCHSGeometry;
342 /* Set LCHS geometry (legacy geometry is ignored for the current 1.1+). */
343 pImage->Header.u.v1plus.LCHSGeometry.cCylinders = pLCHSGeometry->cCylinders;
344 pImage->Header.u.v1plus.LCHSGeometry.cHeads = pLCHSGeometry->cHeads;
345 pImage->Header.u.v1plus.LCHSGeometry.cSectors = pLCHSGeometry->cSectors;
346 pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
347
348 pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
349 if (!pImage->paBlocks)
350 {
351 rc = VERR_NO_MEMORY;
352 goto out;
353 }
354
355 if (enmType != VD_IMAGE_TYPE_FIXED)
356 {
357 /* for growing images mark all blocks in paBlocks as free. */
358 for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
359 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
360 }
361 else
362 {
363 /* for fixed images mark all blocks in paBlocks as allocated */
364 for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
365 pImage->paBlocks[i] = i;
366 pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
367 }
368
369 /* Setup image parameters. */
370 vdiSetupImageDesc(pImage);
371
372 /* Create image file. */
373 rc = RTFileOpen(&File, pImage->pszFilename,
374 RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
375 if (VBOX_FAILURE(rc))
376 {
377 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: cannot create image '%s'"), pImage->pszFilename);
378 goto out;
379 }
380 pImage->File = File;
381
382 cbTotal = pImage->offStartData
383 + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
384
385 if (enmType == VD_IMAGE_TYPE_FIXED)
386 {
387 /* Check the free space on the disk and leave early if there is not
388 * sufficient space available. */
389 RTFOFF cbFree = 0;
390 rc = RTFsQuerySizes(pImage->pszFilename, NULL, &cbFree, NULL, NULL);
391 if (VBOX_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbTotal))
392 {
393 rc = vdiError(pImage, VERR_DISK_FULL, RT_SRC_POS, N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
394 goto out;
395 }
396 }
397
398 if (enmType == VD_IMAGE_TYPE_FIXED)
399 {
400 /*
401 * Allocate & commit whole file if fixed image, it must be more
402 * effective than expanding file by write operations.
403 */
404 rc = RTFileSetSize(File, cbTotal);
405 }
406 else
407 {
408 /* Set file size to hold header and blocks array. */
409 rc = RTFileSetSize(pImage->File, pImage->offStartData);
410 }
411 if (VBOX_FAILURE(rc))
412 {
413 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"), pImage->pszFilename);
414 goto out;
415 }
416
417 /* Generate image last-modify uuid */
418 RTUuidCreate(getImageModificationUUID(&pImage->Header));
419
420 /* Write pre-header. */
421 rc = RTFileWriteAt(File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
422 if (VBOX_FAILURE(rc))
423 {
424 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"), pImage->pszFilename);
425 goto out;
426 }
427
428 /* Write header. */
429 rc = RTFileWriteAt(File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
430 if (VBOX_FAILURE(rc))
431 {
432 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"), pImage->pszFilename);
433 goto out;
434 }
435
436 rc = RTFileWriteAt(File, pImage->offStartBlocks,
437 pImage->paBlocks,
438 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
439 NULL);
440 if (VBOX_FAILURE(rc))
441 {
442 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"), pImage->pszFilename);
443 goto out;
444 }
445
446 if (enmType == VD_IMAGE_TYPE_FIXED)
447 {
448 /* Fill image with zeroes. We do this for every fixed-size image since on some systems
449 * (for example Windows Vista), it takes ages to write a block near the end of a sparse
450 * file and the guest could complain about an ATA timeout. */
451
452 /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
453 * Currently supported file systems are ext4 and ocfs2. */
454
455 /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
456 const size_t cbBuf = 128 * _1K;
457 void *pvBuf = RTMemTmpAllocZ(cbBuf);
458 if (!pvBuf)
459 {
460 rc = VERR_NO_MEMORY;
461 goto out;
462 }
463
464 cbFill = (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
465 uOff = 0;
466 /* Write data to all image blocks. */
467 while (uOff < cbFill)
468 {
469 unsigned cbChunk = (unsigned)RT_MIN(cbFill, cbBuf);
470
471 rc = RTFileWriteAt(File, pImage->offStartData + uOff,
472 pvBuf, cbChunk, NULL);
473 if (VBOX_FAILURE(rc))
474 {
475 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block failed for '%s'"), pImage->pszFilename);
476 goto out;
477 }
478
479 uOff += cbChunk;
480
481 if (pfnProgress)
482 {
483 rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
484 uPercentStart + uOff * uPercentSpan / cbFill,
485 pvUser);
486 if (VBOX_FAILURE(rc))
487 goto out;
488 }
489 }
490 RTMemTmpFree(pvBuf);
491 }
492
493out:
494 if (VBOX_SUCCESS(rc) && pfnProgress)
495 pfnProgress(NULL /* WARNING! pVM=NULL */,
496 uPercentStart + uPercentSpan, pvUser);
497
498 if (VBOX_FAILURE(rc))
499 vdiFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
500 return rc;
501}
502
503/**
504 * Internal: Open a VDI image.
505 */
506static int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
507{
508 int rc;
509 RTFILE File;
510
511 pImage->uOpenFlags = uOpenFlags;
512
513 /*
514 * Open the image.
515 */
516 rc = RTFileOpen(&File, pImage->pszFilename,
517 uOpenFlags & VD_OPEN_FLAGS_READONLY
518 ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
519 : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
520 if (VBOX_FAILURE(rc))
521 {
522 /* Do NOT signal an appropriate error here, as the VD layer has the
523 * choice of retrying the open if it failed. */
524 goto out;
525 }
526 pImage->File = File;
527
528 /* Read pre-header. */
529 rc = RTFileReadAt(File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader),
530 NULL);
531 if (VBOX_FAILURE(rc))
532 {
533 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
534 goto out;
535 }
536 rc = vdiValidatePreHeader(&pImage->PreHeader);
537 if (VBOX_FAILURE(rc))
538 {
539 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
540 goto out;
541 }
542
543 /* Read header. */
544 pImage->Header.uVersion = pImage->PreHeader.u32Version;
545 switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
546 {
547 case 0:
548 rc = RTFileReadAt(File, sizeof(pImage->PreHeader),
549 &pImage->Header.u.v0, sizeof(pImage->Header.u.v0),
550 NULL);
551 if (VBOX_FAILURE(rc))
552 {
553 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
554 goto out;
555 }
556 break;
557 case 1:
558 rc = RTFileReadAt(File, sizeof(pImage->PreHeader),
559 &pImage->Header.u.v1, sizeof(pImage->Header.u.v1),
560 NULL);
561 if (VBOX_FAILURE(rc))
562 {
563 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
564 goto out;
565 }
566 /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write mode.
567 * Conversion is harmless, as any VirtualBox version supporting VDI
568 * 1.1 doesn't touch fields it doesn't know about. */
569 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
570 && GET_MINOR_HEADER_VERSION(&pImage->Header) == 1
571 && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
572 {
573 pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
574 /* Mark LCHS geometry not-calculated. */
575 pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
576 pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
577 pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
578 pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
579 }
580 else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
581 {
582 /* Read the actual VDI 1.1+ header completely. */
583 rc = RTFileReadAt(File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
584 if (VBOX_FAILURE(rc))
585 {
586 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
587 goto out;
588 }
589 }
590 break;
591 default:
592 rc = vdiError(pImage, VERR_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: unsupported major version %u in '%s'"), GET_MAJOR_HEADER_VERSION(&pImage->Header), pImage->pszFilename);
593 goto out;
594 }
595
596 rc = vdiValidateHeader(&pImage->Header);
597 if (VBOX_FAILURE(rc))
598 {
599 rc = vdiError(pImage, VERR_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: invalid header in '%s'"), pImage->pszFilename);
600 goto out;
601 }
602
603 /* Setup image parameters by header. */
604 vdiSetupImageDesc(pImage);
605
606 /* Allocate memory for blocks array. */
607 pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
608 if (!pImage->paBlocks)
609 {
610 rc = VERR_NO_MEMORY;
611 goto out;
612 }
613
614 /* Read blocks array. */
615 rc = RTFileReadAt(pImage->File, pImage->offStartBlocks, pImage->paBlocks,
616 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
617 NULL);
618
619out:
620 if (VBOX_FAILURE(rc))
621 vdiFreeImage(pImage, false);
622 return rc;
623}
624
625/**
626 * Internal: Save header to file.
627 */
628static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
629{
630 int rc;
631 switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
632 {
633 case 0:
634 rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
635 break;
636 case 1:
637 if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
638 rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
639 else
640 rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
641 break;
642 default:
643 rc = VERR_VDI_UNSUPPORTED_VERSION;
644 break;
645 }
646 AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Vrc\n", pImage->pszFilename, rc));
647 return rc;
648}
649
650/**
651 * Internal: Save block pointer to file, save header to file.
652 */
653static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
654{
655 /* Update image header. */
656 int rc = vdiUpdateHeader(pImage);
657 if (VBOX_SUCCESS(rc))
658 {
659 /* write only one block pointer. */
660 rc = RTFileWriteAt(pImage->File,
661 pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
662 &pImage->paBlocks[uBlock],
663 sizeof(VDIIMAGEBLOCKPOINTER),
664 NULL);
665 AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Vrc\n",
666 uBlock, pImage->pszFilename, rc));
667 }
668 return rc;
669}
670
671/**
672 * Internal: Flush the image file to disk.
673 */
674static void vdiFlushImage(PVDIIMAGEDESC pImage)
675{
676 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
677 {
678 /* Save header. */
679 int rc = vdiUpdateHeader(pImage);
680 AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
681 pImage->pszFilename, rc));
682 RTFileFlush(pImage->File);
683 }
684}
685
686/**
687 * Internal: Free all allocated space for representing an image, and optionally
688 * delete the image from disk.
689 */
690static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
691{
692 Assert(VALID_PTR(pImage));
693
694 if (pImage->File != NIL_RTFILE)
695 {
696 vdiFlushImage(pImage);
697 RTFileClose(pImage->File);
698 pImage->File = NIL_RTFILE;
699 }
700 if (pImage->paBlocks)
701 {
702 RTMemFree(pImage->paBlocks);
703 pImage->paBlocks = NULL;
704 }
705 if (fDelete && pImage->pszFilename)
706 RTFileDelete(pImage->pszFilename);
707}
708
709
710/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
711static int vdiCheckIfValid(const char *pszFilename)
712{
713 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
714 int rc = VINF_SUCCESS;
715 PVDIIMAGEDESC pImage;
716
717 if ( !VALID_PTR(pszFilename)
718 || !*pszFilename)
719 {
720 rc = VERR_INVALID_PARAMETER;
721 goto out;
722 }
723
724 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
725 if (!pImage)
726 {
727 rc = VERR_NO_MEMORY;
728 goto out;
729 }
730 pImage->pszFilename = pszFilename;
731 pImage->File = NIL_RTFILE;
732 pImage->paBlocks = NULL;
733 pImage->pfnError = NULL;
734 pImage->pvErrorUser = NULL;
735
736 rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
737 vdiFreeImage(pImage, false);
738
739out:
740 LogFlowFunc(("returns %Vrc\n", rc));
741 return rc;
742}
743
744/** @copydoc VBOXHDDBACKEND::pfnOpen */
745static int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
746 PFNVDERROR pfnError, void *pvErrorUser,
747 void **ppBackendData)
748{
749 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x ppBackendData=%#p\n", pszFilename, uOpenFlags, ppBackendData));
750 int rc;
751 PVDIIMAGEDESC pImage;
752
753 /* Check open flags. All valid flags are supported. */
754 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
755 {
756 rc = VERR_INVALID_PARAMETER;
757 goto out;
758 }
759
760 /* Check remaining arguments. */
761 if ( !VALID_PTR(pszFilename)
762 || !*pszFilename)
763 {
764 rc = VERR_INVALID_PARAMETER;
765 goto out;
766 }
767
768 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
769 if (!pImage)
770 {
771 rc = VERR_NO_MEMORY;
772 goto out;
773 }
774 pImage->pszFilename = pszFilename;
775 pImage->File = NIL_RTFILE;
776 pImage->paBlocks = NULL;
777 pImage->pfnError = pfnError;
778 pImage->pvErrorUser = pvErrorUser;
779
780 rc = vdiOpenImage(pImage, uOpenFlags);
781 if (VBOX_SUCCESS(rc))
782 *ppBackendData = pImage;
783
784out:
785 LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
786 return rc;
787}
788
789/** @copydoc VBOXHDDBACKEND::pfnCreate */
790static int vdiCreate(const char *pszFilename, VDIMAGETYPE enmType,
791 uint64_t cbSize, unsigned uImageFlags,
792 const char *pszComment,
793 PCPDMMEDIAGEOMETRY pPCHSGeometry,
794 PCPDMMEDIAGEOMETRY pLCHSGeometry,
795 unsigned uOpenFlags, PFNVMPROGRESS pfnProgress,
796 void *pvUser, unsigned uPercentStart,
797 unsigned uPercentSpan, PFNVDERROR pfnError,
798 void *pvErrorUser, void **ppBackendData)
799{
800 LogFlowFunc(("pszFilename=\"%s\" enmType=%d cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p uOpenFlags=%#x pfnProgress=%#p pvUser=%#p uPercentStart=%u uPercentSpan=%u pfnError=%#p pvErrorUser=%#p ppBackendData=%#p", pszFilename, enmType, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, uOpenFlags, pfnProgress, pvUser, uPercentStart, uPercentSpan, pfnError, pvErrorUser, ppBackendData));
801 int rc;
802 PVDIIMAGEDESC pImage;
803
804 /* Check open flags. All valid flags are supported. */
805 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
806 {
807 rc = VERR_INVALID_PARAMETER;
808 goto out;
809 }
810
811 /* Check remaining arguments. */
812 if ( !VALID_PTR(pszFilename)
813 || !*pszFilename
814 || (enmType != VD_IMAGE_TYPE_NORMAL && enmType != VD_IMAGE_TYPE_FIXED)
815 || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
816 || !VALID_PTR(pPCHSGeometry)
817 || !VALID_PTR(pLCHSGeometry))
818 {
819 rc = VERR_INVALID_PARAMETER;
820 goto out;
821 }
822
823 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
824 if (!pImage)
825 {
826 rc = VERR_NO_MEMORY;
827 goto out;
828 }
829 pImage->pszFilename = pszFilename;
830 pImage->File = NIL_RTFILE;
831 pImage->paBlocks = NULL;
832 pImage->pfnError = pfnError;
833 pImage->pvErrorUser = pvErrorUser;
834
835 rc = vdiCreateImage(pImage, enmType, cbSize, uImageFlags, pszComment,
836 pPCHSGeometry, pLCHSGeometry,
837 pfnProgress, pvUser, uPercentStart, uPercentSpan);
838 if (VBOX_SUCCESS(rc))
839 {
840 /* So far the image is opened in read/write mode. Make sure the
841 * image is opened in read-only mode if the caller requested that. */
842 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
843 {
844 vdiFreeImage(pImage, false);
845 rc = vdiOpenImage(pImage, uOpenFlags);
846 if (VBOX_FAILURE(rc))
847 goto out;
848 }
849 *ppBackendData = pImage;
850 }
851
852out:
853 LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
854 return rc;
855}
856
857/** @copydoc VBOXHDDBACKEND::pfnRename */
858static int vdiRename(void *pBackendData, const char *pszFilename)
859{
860 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
861
862 int rc = VINF_SUCCESS;
863 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
864
865 /* Check arguments. */
866 if ( !pImage
867 || !pszFilename
868 || !*pszFilename)
869 {
870 rc = VERR_INVALID_PARAMETER;
871 goto out;
872 }
873
874 /* Close the image. */
875 vdiFreeImage(pImage, false);
876
877 /* Rename the file. */
878 rc = RTFileMove(pImage->pszFilename, pszFilename, 0);
879 if (VBOX_FAILURE(rc))
880 {
881 /* The move failed, try to reopen the original image. */
882 int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
883 if (VBOX_FAILURE(rc2))
884 rc = rc2;
885
886 goto out;
887 }
888
889 /* Update pImage with the new information. */
890 pImage->pszFilename = pszFilename;
891
892 /* Open the new image. */
893 rc = vdiOpenImage(pImage, pImage->uOpenFlags);
894 if (VBOX_FAILURE(rc))
895 goto out;
896
897out:
898 LogFlowFunc(("returns %Vrc\n", rc));
899 return rc;
900}
901
902/** @copydoc VBOXHDDBACKEND::pfnClose */
903static int vdiClose(void *pBackendData, bool fDelete)
904{
905 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
906 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
907 int rc = VINF_SUCCESS;
908
909 /* Freeing a never allocated image (e.g. because the open failed) is
910 * not signalled as an error. After all nothing bad happens. */
911 if (pImage)
912 vdiFreeImage(pImage, fDelete);
913
914 LogFlowFunc(("returns %Vrc\n", rc));
915 return rc;
916}
917
918/** @copydoc VBOXHDDBACKEND::pfnRead */
919static int vdiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
920 size_t cbToRead, size_t *pcbActuallyRead)
921{
922 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
923 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
924 unsigned uBlock;
925 unsigned offRead;
926 int rc;
927
928 Assert(VALID_PTR(pImage));
929 Assert(!(uOffset % 512));
930 Assert(!(cbToRead % 512));
931
932 if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
933 || !VALID_PTR(pvBuf)
934 || !cbToRead)
935 {
936 rc = VERR_INVALID_PARAMETER;
937 goto out;
938 }
939
940 /* Calculate starting block number and offset inside it. */
941 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
942 offRead = (unsigned)uOffset & pImage->uBlockMask;
943
944 /* Clip read range to at most the rest of the block. */
945 cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
946 Assert(!(cbToRead % 512));
947
948 if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
949 rc = VERR_VDI_BLOCK_FREE;
950 else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
951 {
952 memset(pvBuf, 0, cbToRead);
953 rc = VINF_SUCCESS;
954 }
955 else
956 {
957 /* Block present in image file, read relevant data. */
958 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
959 + (pImage->offStartData + pImage->offStartBlockData + offRead);
960 rc = RTFileReadAt(pImage->File, u64Offset, pvBuf, cbToRead, NULL);
961 }
962
963 if (VBOX_SUCCESS(rc))
964 *pcbActuallyRead = cbToRead;
965
966out:
967 LogFlowFunc(("returns %Vrc\n", rc));
968 return rc;
969}
970
971/**@copydoc VBOXHDDBACKEND::pfnWrite */
972static int vdiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
973 size_t cbToWrite, size_t *pcbWriteProcess,
974 size_t *pcbPreRead, size_t *pcbPostRead, unsigned fWrite)
975{
976 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
977 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
978 unsigned uBlock;
979 unsigned offWrite;
980 int rc = VINF_SUCCESS;
981
982 Assert(VALID_PTR(pImage));
983 Assert(!(uOffset % 512));
984 Assert(!(cbToWrite % 512));
985
986 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
987 {
988 rc = VERR_VDI_IMAGE_READ_ONLY;
989 goto out;
990 }
991
992 if (!VALID_PTR(pvBuf) || !cbToWrite)
993 {
994 rc = VERR_INVALID_PARAMETER;
995 goto out;
996 }
997
998 /* No size check here, will do that later. For dynamic images which are
999 * not multiples of the block size in length, this would prevent writing to
1000 * the last grain. */
1001
1002 /* Calculate starting block number and offset inside it. */
1003 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
1004 offWrite = (unsigned)uOffset & pImage->uBlockMask;
1005
1006 /* Clip write range to at most the rest of the block. */
1007 cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
1008 Assert(!(cbToWrite % 512));
1009
1010 if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
1011 {
1012 /* Block is either free or zero. */
1013 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
1014 && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
1015 || cbToWrite == getImageBlockSize(&pImage->Header)))
1016 {
1017 /* If the destination block is unallocated at this point, it's
1018 * either a zero block or a block which hasn't been used so far
1019 * (which also means that it's a zero block. Don't need to write
1020 * anything to this block if the data consists of just zeroes. */
1021 Assert(!(cbToWrite % 4));
1022 Assert(cbToWrite * 8 <= UINT32_MAX);
1023 if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
1024 {
1025 pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
1026 goto out;
1027 }
1028 }
1029
1030 if (cbToWrite == getImageBlockSize(&pImage->Header))
1031 {
1032 /* Full block write to previously unallocated block.
1033 * Allocate block and write data. */
1034 Assert(!offWrite);
1035 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
1036 uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
1037 + (pImage->offStartData + pImage->offStartBlockData);
1038 rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
1039 if (VBOX_FAILURE(rc))
1040 goto out;
1041 pImage->paBlocks[uBlock] = cBlocksAllocated;
1042 setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
1043
1044 rc = vdiUpdateBlockInfo(pImage, uBlock);
1045 if (VBOX_FAILURE(rc))
1046 goto out;
1047
1048 *pcbPreRead = 0;
1049 *pcbPostRead = 0;
1050 }
1051 else
1052 {
1053 /* Trying to do a partial write to an unallocated block. Don't do
1054 * anything except letting the upper layer know what to do. */
1055 *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
1056 *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
1057 rc = VERR_VDI_BLOCK_FREE;
1058 }
1059 }
1060 else
1061 {
1062 /* Block present in image file, write relevant data. */
1063 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
1064 + (pImage->offStartData + pImage->offStartBlockData + offWrite);
1065 rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
1066 }
1067
1068out:
1069 LogFlowFunc(("returns %Vrc\n", rc));
1070 return rc;
1071}
1072
1073/** @copydoc VBOXHDDBACKEND::pfnFlush */
1074static int vdiFlush(void *pBackendData)
1075{
1076 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1077 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1078 int rc = VINF_SUCCESS;
1079
1080 vdiFlushImage(pImage);
1081 LogFlowFunc(("returns %Vrc\n", rc));
1082 return rc;
1083}
1084
1085/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
1086static unsigned vdiGetVersion(void *pBackendData)
1087{
1088 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1089 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1090 unsigned uVersion;
1091
1092 Assert(VALID_PTR(pImage));
1093
1094 if (pImage)
1095 uVersion = pImage->PreHeader.u32Version;
1096 else
1097 uVersion = 0;
1098
1099 LogFlowFunc(("returns %#x\n", uVersion));
1100 return uVersion;
1101}
1102
1103/** @copydoc VBOXHDDBACKEND::pfnGetImageType */
1104static int vdiGetImageType(void *pBackendData, PVDIMAGETYPE penmImageType)
1105{
1106 LogFlowFunc(("pBackendData=%#p penmImageType=%#p\n", pBackendData, penmImageType));
1107 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1108 int rc = VINF_SUCCESS;
1109
1110 Assert(VALID_PTR(pImage));
1111 Assert(VALID_PTR(penmImageType));
1112
1113 if (pImage)
1114 *penmImageType = getImageType(&pImage->Header) == VDI_IMAGE_TYPE_NORMAL
1115 ? VD_IMAGE_TYPE_NORMAL
1116 : VD_IMAGE_TYPE_DIFF;
1117 else
1118 rc = VERR_VDI_NOT_OPENED;
1119
1120 LogFlowFunc(("returns %Vrc enmImageType=%u\n", rc, *penmImageType));
1121 return rc;
1122}
1123
1124/** @copydoc VBOXHDDBACKEND::pfnGetSize */
1125static uint64_t vdiGetSize(void *pBackendData)
1126{
1127 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1128 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1129 uint64_t cbSize;
1130
1131 Assert(VALID_PTR(pImage));
1132
1133 if (pImage)
1134 cbSize = getImageDiskSize(&pImage->Header);
1135 else
1136 cbSize = 0;
1137
1138 LogFlowFunc(("returns %llu\n", cbSize));
1139 return cbSize;
1140}
1141
1142/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
1143static uint64_t vdiGetFileSize(void *pBackendData)
1144{
1145 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1146 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1147 uint64_t cb = 0;
1148
1149 Assert(VALID_PTR(pImage));
1150
1151 if (pImage)
1152 {
1153 uint64_t cbFile;
1154 if (pImage->File != NIL_RTFILE)
1155 {
1156 int rc = RTFileGetSize(pImage->File, &cbFile);
1157 if (VBOX_SUCCESS(rc))
1158 cb += cbFile;
1159 }
1160 }
1161
1162 LogFlowFunc(("returns %lld\n", cb));
1163 return cb;
1164}
1165
1166/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
1167static int vdiGetPCHSGeometry(void *pBackendData,
1168 PPDMMEDIAGEOMETRY pPCHSGeometry)
1169{
1170 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
1171 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1172 int rc;
1173
1174 Assert(VALID_PTR(pImage));
1175
1176 if (pImage)
1177 {
1178 if (pImage->PCHSGeometry.cCylinders)
1179 {
1180 *pPCHSGeometry = pImage->PCHSGeometry;
1181 rc = VINF_SUCCESS;
1182 }
1183 else
1184 rc = VERR_VDI_GEOMETRY_NOT_SET;
1185 }
1186 else
1187 rc = VERR_VDI_NOT_OPENED;
1188
1189 LogFlowFunc(("returns %Vrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1190 return rc;
1191}
1192
1193/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
1194static int vdiSetPCHSGeometry(void *pBackendData,
1195 PCPDMMEDIAGEOMETRY pPCHSGeometry)
1196{
1197 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1198 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1199 int rc;
1200
1201 Assert(VALID_PTR(pImage));
1202
1203 if (pImage)
1204 {
1205 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1206 {
1207 rc = VERR_VDI_IMAGE_READ_ONLY;
1208 goto out;
1209 }
1210
1211 pImage->PCHSGeometry = *pPCHSGeometry;
1212 rc = VINF_SUCCESS;
1213 }
1214 else
1215 rc = VERR_VDI_NOT_OPENED;
1216
1217out:
1218 LogFlowFunc(("returns %Vrc\n", rc));
1219 return rc;
1220}
1221
1222/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
1223static int vdiGetLCHSGeometry(void *pBackendData,
1224 PPDMMEDIAGEOMETRY pLCHSGeometry)
1225{
1226 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
1227 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1228 int rc;
1229
1230 Assert(VALID_PTR(pImage));
1231
1232 if (pImage)
1233 {
1234 VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
1235 PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
1236 if (!pGeometry)
1237 pGeometry = &DummyGeo;
1238
1239 if ( pGeometry->cCylinders > 0
1240 && pGeometry->cHeads > 0
1241 && pGeometry->cSectors > 0)
1242 {
1243 pLCHSGeometry->cCylinders = pGeometry->cCylinders;
1244 pLCHSGeometry->cHeads = pGeometry->cHeads;
1245 pLCHSGeometry->cSectors = pGeometry->cSectors;
1246 rc = VINF_SUCCESS;
1247 }
1248 else
1249 rc = VERR_VDI_GEOMETRY_NOT_SET;
1250 }
1251 else
1252 rc = VERR_VDI_NOT_OPENED;
1253
1254 LogFlowFunc(("returns %Vrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1255 return rc;
1256}
1257
1258/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
1259static int vdiSetLCHSGeometry(void *pBackendData,
1260 PCPDMMEDIAGEOMETRY pLCHSGeometry)
1261{
1262 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1263 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1264 PVDIDISKGEOMETRY pGeometry;
1265 int rc;
1266
1267 Assert(VALID_PTR(pImage));
1268
1269 if (pImage)
1270 {
1271 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1272 {
1273 rc = VERR_VDI_IMAGE_READ_ONLY;
1274 goto out;
1275 }
1276
1277 pGeometry = getImageLCHSGeometry(&pImage->Header);
1278 if (pGeometry)
1279 {
1280 pGeometry->cCylinders = pLCHSGeometry->cCylinders;
1281 pGeometry->cHeads = pLCHSGeometry->cHeads;
1282 pGeometry->cSectors = pLCHSGeometry->cSectors;
1283 pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
1284
1285 /* Update header information in base image file. */
1286 vdiFlushImage(pImage);
1287 }
1288 rc = VINF_SUCCESS;
1289 }
1290 else
1291 rc = VERR_VDI_NOT_OPENED;
1292
1293out:
1294 LogFlowFunc(("returns %Vrc\n", rc));
1295 return rc;
1296}
1297
1298/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
1299static unsigned vdiGetImageFlags(void *pBackendData)
1300{
1301 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1302 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1303 unsigned uImageFlags;
1304
1305 Assert(VALID_PTR(pImage));
1306
1307 if (pImage)
1308 uImageFlags = getImageFlags(&pImage->Header);
1309 else
1310 uImageFlags = 0;
1311
1312 LogFlowFunc(("returns %#x\n", uImageFlags));
1313 return uImageFlags;
1314}
1315
1316/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
1317static unsigned vdiGetOpenFlags(void *pBackendData)
1318{
1319 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1320 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1321 unsigned uOpenFlags;
1322
1323 Assert(VALID_PTR(pImage));
1324
1325 if (pImage)
1326 uOpenFlags = pImage->uOpenFlags;
1327 else
1328 uOpenFlags = 0;
1329
1330 LogFlowFunc(("returns %#x\n", uOpenFlags));
1331 return uOpenFlags;
1332}
1333
1334/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
1335static int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
1336{
1337 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
1338 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1339 int rc;
1340 const char *pszFilename;
1341
1342 /* Image must be opened and the new flags must be valid. Just readonly flag
1343 * is supported. */
1344 if (!pImage || uOpenFlags & ~VD_OPEN_FLAGS_READONLY)
1345 {
1346 rc = VERR_INVALID_PARAMETER;
1347 goto out;
1348 }
1349
1350 /* Implement this operation via reopening the image. */
1351 pszFilename = pImage->pszFilename;
1352 vdiFreeImage(pImage, false);
1353 rc = vdiOpenImage(pImage, uOpenFlags);
1354
1355out:
1356 LogFlowFunc(("returns %Vrc\n", rc));
1357 return rc;
1358}
1359
1360/** @copydoc VBOXHDDBACKEND::pfnGetComment */
1361static int vdiGetComment(void *pBackendData, char *pszComment,
1362 size_t cbComment)
1363{
1364 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
1365 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1366 int rc = VINF_SUCCESS;
1367
1368 Assert(VALID_PTR(pImage));
1369
1370 if (pImage)
1371 {
1372 char *pszTmp = getImageComment(&pImage->Header);
1373 unsigned cb = strlen(pszTmp);
1374 if (cb < cbComment)
1375 {
1376 /* memcpy is much better than strncpy. */
1377 memcpy(pszComment, pszTmp, cb + 1);
1378 }
1379 else
1380 rc = VERR_BUFFER_OVERFLOW;
1381 }
1382 else
1383 rc = VERR_VDI_NOT_OPENED;
1384
1385 LogFlowFunc(("returns %Vrc comment=\"%s\"\n", rc, pszComment));
1386 return rc;
1387}
1388
1389/** @copydoc VBOXHDDBACKEND::pfnGetComment */
1390static int vdiSetComment(void *pBackendData, const char *pszComment)
1391{
1392 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
1393 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1394 int rc;
1395
1396 Assert(VALID_PTR(pImage));
1397
1398 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1399 {
1400 rc = VERR_VDI_IMAGE_READ_ONLY;
1401 goto out;
1402 }
1403
1404 if (pImage)
1405 {
1406 size_t cchComment = pszComment ? strlen(pszComment) : 0;
1407 if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
1408 {
1409 LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
1410 rc = VERR_VDI_COMMENT_TOO_LONG;
1411 goto out;
1412 }
1413
1414 /* we don't support old style images */
1415 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1416 {
1417 /*
1418 * Update the comment field, making sure to zero out all of the previous comment.
1419 */
1420 memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
1421 memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
1422
1423 /* write out new the header */
1424 rc = vdiUpdateHeader(pImage);
1425 }
1426 else
1427 rc = VERR_VDI_UNSUPPORTED_VERSION;
1428 }
1429 else
1430 rc = VERR_VDI_NOT_OPENED;
1431
1432out:
1433 LogFlowFunc(("returns %Vrc\n", rc));
1434 return rc;
1435}
1436
1437/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
1438static int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
1439{
1440 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1441 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1442 int rc;
1443
1444 Assert(VALID_PTR(pImage));
1445
1446 if (pImage)
1447 {
1448 *pUuid = *getImageCreationUUID(&pImage->Header);
1449 rc = VINF_SUCCESS;
1450 }
1451 else
1452 rc = VERR_VDI_NOT_OPENED;
1453
1454 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1455 return rc;
1456}
1457
1458/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
1459static int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
1460{
1461 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1462 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1463 int rc = VINF_SUCCESS;
1464
1465 Assert(VALID_PTR(pImage));
1466
1467 if (pImage)
1468 {
1469 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1470 {
1471 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1472 pImage->Header.u.v1.uuidCreate = *pUuid;
1473 /* Make it possible to clone old VDIs. */
1474 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1475 pImage->Header.u.v0.uuidCreate = *pUuid;
1476 else
1477 {
1478 LogFunc(("Version is not supported!\n"));
1479 rc = VERR_VDI_UNSUPPORTED_VERSION;
1480 }
1481 }
1482 else
1483 rc = VERR_VDI_IMAGE_READ_ONLY;
1484 }
1485 else
1486 rc = VERR_VDI_NOT_OPENED;
1487
1488 LogFlowFunc(("returns %Vrc\n", rc));
1489 return rc;
1490}
1491
1492/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
1493static int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
1494{
1495 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1496 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1497 int rc;
1498
1499 Assert(VALID_PTR(pImage));
1500
1501 if (pImage)
1502 {
1503 *pUuid = *getImageModificationUUID(&pImage->Header);
1504 rc = VINF_SUCCESS;
1505 }
1506 else
1507 rc = VERR_VDI_NOT_OPENED;
1508
1509 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1510 return rc;
1511}
1512
1513/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
1514static int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
1515{
1516 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1517 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1518 int rc = VINF_SUCCESS;
1519
1520 Assert(VALID_PTR(pImage));
1521
1522 if (pImage)
1523 {
1524 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1525 {
1526 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1527 pImage->Header.u.v1.uuidModify = *pUuid;
1528 /* Make it possible to clone old VDIs. */
1529 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1530 pImage->Header.u.v0.uuidModify = *pUuid;
1531 else
1532 {
1533 LogFunc(("Version is not supported!\n"));
1534 rc = VERR_VDI_UNSUPPORTED_VERSION;
1535 }
1536 }
1537 else
1538 rc = VERR_VDI_IMAGE_READ_ONLY;
1539 }
1540 else
1541 rc = VERR_VDI_NOT_OPENED;
1542
1543 LogFlowFunc(("returns %Vrc\n", rc));
1544 return rc;
1545}
1546
1547/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
1548static int vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
1549{
1550 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1551 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1552 int rc;
1553
1554 Assert(VALID_PTR(pImage));
1555
1556 if (pImage)
1557 {
1558 *pUuid = *getImageParentUUID(&pImage->Header);
1559 rc = VINF_SUCCESS;
1560 }
1561 else
1562 rc = VERR_VDI_NOT_OPENED;
1563
1564 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1565 return rc;
1566}
1567
1568/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
1569static int vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
1570{
1571 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1572 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1573 int rc = VINF_SUCCESS;
1574
1575 Assert(VALID_PTR(pImage));
1576
1577 if (pImage)
1578 {
1579 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1580 {
1581 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1582 pImage->Header.u.v1.uuidLinkage = *pUuid;
1583 /* Make it possible to clone old VDIs. */
1584 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1585 pImage->Header.u.v0.uuidLinkage = *pUuid;
1586 else
1587 {
1588 LogFunc(("Version is not supported!\n"));
1589 rc = VERR_VDI_UNSUPPORTED_VERSION;
1590 }
1591 }
1592 else
1593 rc = VERR_VDI_IMAGE_READ_ONLY;
1594 }
1595 else
1596 rc = VERR_VDI_NOT_OPENED;
1597
1598 LogFlowFunc(("returns %Vrc\n", rc));
1599 return rc;
1600}
1601
1602/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
1603static int vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
1604{
1605 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1606 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1607 int rc;
1608
1609 Assert(VALID_PTR(pImage));
1610
1611 if (pImage)
1612 {
1613 *pUuid = *getImageParentModificationUUID(&pImage->Header);
1614 rc = VINF_SUCCESS;
1615 }
1616 else
1617 rc = VERR_VDI_NOT_OPENED;
1618
1619 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1620 return rc;
1621}
1622
1623/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
1624static int vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
1625{
1626 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1627 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1628 int rc = VINF_SUCCESS;
1629
1630 Assert(VALID_PTR(pImage));
1631
1632 if (pImage)
1633 {
1634 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1635 {
1636 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1637 pImage->Header.u.v1.uuidParentModify = *pUuid;
1638 else
1639 {
1640 LogFunc(("Version is not supported!\n"));
1641 rc = VERR_VDI_UNSUPPORTED_VERSION;
1642 }
1643 }
1644 else
1645 rc = VERR_VDI_IMAGE_READ_ONLY;
1646 }
1647 else
1648 rc = VERR_VDI_NOT_OPENED;
1649
1650 LogFlowFunc(("returns %Vrc\n", rc));
1651 return rc;
1652}
1653
1654/** @copydoc VBOXHDDBACKEND::pfnDump */
1655static void vdiDump(void *pBackendData)
1656{
1657 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1658
1659 RTLogPrintf("Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%08X\n",
1660 pImage->pszFilename,
1661 (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
1662 pImage->uOpenFlags,
1663 pImage->File);
1664 RTLogPrintf("Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
1665 pImage->PreHeader.u32Version,
1666 getImageType(&pImage->Header),
1667 getImageFlags(&pImage->Header),
1668 getImageDiskSize(&pImage->Header));
1669 RTLogPrintf("Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
1670 getImageBlockSize(&pImage->Header),
1671 getImageExtraBlockSize(&pImage->Header),
1672 getImageBlocks(&pImage->Header),
1673 getImageBlocksAllocated(&pImage->Header));
1674 RTLogPrintf("Header: offBlocks=%u offData=%u\n",
1675 getImageBlocksOffset(&pImage->Header),
1676 getImageDataOffset(&pImage->Header));
1677 PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
1678 if (pg)
1679 RTLogPrintf("Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
1680 pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
1681 RTLogPrintf("Header: uuidCreation={%Vuuid}\n", getImageCreationUUID(&pImage->Header));
1682 RTLogPrintf("Header: uuidModification={%Vuuid}\n", getImageModificationUUID(&pImage->Header));
1683 RTLogPrintf("Header: uuidParent={%Vuuid}\n", getImageParentUUID(&pImage->Header));
1684 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
1685 RTLogPrintf("Header: uuidParentModification={%Vuuid}\n", getImageParentModificationUUID(&pImage->Header));
1686 RTLogPrintf("Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
1687 pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
1688 RTLogPrintf("Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
1689 pImage->uBlockMask,
1690 pImage->cbTotalBlockData,
1691 pImage->uShiftOffset2Index,
1692 pImage->offStartBlockData);
1693
1694 unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
1695 for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
1696 {
1697 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
1698 {
1699 cBlocksNotFree++;
1700 if (pImage->paBlocks[uBlock] >= cBlocks)
1701 cBadBlocks++;
1702 }
1703 }
1704 if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
1705 {
1706 RTLogPrintf("!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
1707 cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
1708 }
1709 if (cBadBlocks)
1710 {
1711 RTLogPrintf("!! WARNING: %u bad blocks found !!\n",
1712 cBadBlocks);
1713 }
1714}
1715
1716VBOXHDDBACKEND g_VDIBackend =
1717{
1718 /* pszBackendName */
1719 "VDI",
1720 /* cbSize */
1721 sizeof(VBOXHDDBACKEND),
1722 /* uBackendCaps */
1723 VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
1724 | VD_CAP_CREATE_SPLIT_2G | VD_CAP_DIFF,
1725 /* pfnCheckIfValid */
1726 vdiCheckIfValid,
1727 /* pfnOpen */
1728 vdiOpen,
1729 /* pfnCreate */
1730 vdiCreate,
1731 /* pfnRename */
1732 vdiRename,
1733 /* pfnClose */
1734 vdiClose,
1735 /* pfnRead */
1736 vdiRead,
1737 /* pfnWrite */
1738 vdiWrite,
1739 /* pfnFlush */
1740 vdiFlush,
1741 /* pfnGetVersion */
1742 vdiGetVersion,
1743 /* pfnGetImageType */
1744 vdiGetImageType,
1745 /* pfnGetSize */
1746 vdiGetSize,
1747 /* pfnGetFileSize */
1748 vdiGetFileSize,
1749 /* pfnGetPCHSGeometry */
1750 vdiGetPCHSGeometry,
1751 /* pfnSetPCHSGeometry */
1752 vdiSetPCHSGeometry,
1753 /* pfnGetLCHSGeometry */
1754 vdiGetLCHSGeometry,
1755 /* pfnSetLCHSGeometry */
1756 vdiSetLCHSGeometry,
1757 /* pfnGetImageFlags */
1758 vdiGetImageFlags,
1759 /* pfnGetOpenFlags */
1760 vdiGetOpenFlags,
1761 /* pfnSetOpenFlags */
1762 vdiSetOpenFlags,
1763 /* pfnGetComment */
1764 vdiGetComment,
1765 /* pfnSetComment */
1766 vdiSetComment,
1767 /* pfnGetUuid */
1768 vdiGetUuid,
1769 /* pfnSetUuid */
1770 vdiSetUuid,
1771 /* pfnGetModificationUuid */
1772 vdiGetModificationUuid,
1773 /* pfnSetModificationUuid */
1774 vdiSetModificationUuid,
1775 /* pfnGetParentUuid */
1776 vdiGetParentUuid,
1777 /* pfnSetParentUuid */
1778 vdiSetParentUuid,
1779 /* pfnGetParentModificationUuid */
1780 vdiGetParentModificationUuid,
1781 /* pfnSetParentModificationUuid */
1782 vdiSetParentModificationUuid,
1783 /* pfnDump */
1784 vdiDump
1785};
1786
Note: See TracBrowser for help on using the repository browser.

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