VirtualBox

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

Last change on this file since 8110 was 7894, checked in by vboxsync, 17 years ago

VDI: Skip flushing the image if the open wasn't successful.

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

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