VirtualBox

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

Last change on this file since 7640 was 7275, checked in by vboxsync, 17 years ago

Silly bug in the new VDI creation code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 56.1 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 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 /* do loop to fill all image. */
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 vdiFlushImage(pImage);
691 if (pImage->File != NIL_RTFILE)
692 {
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 ( !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
728 rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
729 vdiFreeImage(pImage, false);
730
731out:
732 LogFlowFunc(("returns %Vrc\n", rc));
733 return rc;
734}
735
736/** @copydoc VBOXHDDBACKEND::pfnOpen */
737static int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
738 PFNVDERROR pfnError, void *pvErrorUser,
739 void **ppBackendData)
740{
741 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x ppBackendData=%#p\n", pszFilename, uOpenFlags, ppBackendData));
742 int rc;
743 PVDIIMAGEDESC pImage;
744
745 /* Check open flags. All valid flags are supported. */
746 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
747 {
748 rc = VERR_INVALID_PARAMETER;
749 goto out;
750 }
751
752 /* Check remaining arguments. */
753 if ( !VALID_PTR(pszFilename)
754 || !*pszFilename)
755 {
756 rc = VERR_INVALID_PARAMETER;
757 goto out;
758 }
759
760 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
761 if (!pImage)
762 {
763 rc = VERR_NO_MEMORY;
764 goto out;
765 }
766 pImage->pszFilename = pszFilename;
767 pImage->paBlocks = NULL;
768 pImage->pfnError = pfnError;
769 pImage->pvErrorUser = pvErrorUser;
770
771 rc = vdiOpenImage(pImage, uOpenFlags);
772 if (VBOX_SUCCESS(rc))
773 *ppBackendData = pImage;
774
775out:
776 LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
777 return rc;
778}
779
780/** @copydoc VBOXHDDBACKEND::pfnCreate */
781static int vdiCreate(const char *pszFilename, VDIMAGETYPE enmType,
782 uint64_t cbSize, unsigned uImageFlags,
783 const char *pszComment,
784 PCPDMMEDIAGEOMETRY pPCHSGeometry,
785 PCPDMMEDIAGEOMETRY pLCHSGeometry,
786 unsigned uOpenFlags, PFNVMPROGRESS pfnProgress,
787 void *pvUser, unsigned uPercentStart,
788 unsigned uPercentSpan, PFNVDERROR pfnError,
789 void *pvErrorUser, void **ppBackendData)
790{
791 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));
792 int rc;
793 PVDIIMAGEDESC pImage;
794
795 /* Check open flags. All valid flags are supported. */
796 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
797 {
798 rc = VERR_INVALID_PARAMETER;
799 goto out;
800 }
801
802 /* Check remaining arguments. */
803 if ( !pszFilename
804 || !*pszFilename
805 || (enmType != VD_IMAGE_TYPE_NORMAL && enmType != VD_IMAGE_TYPE_FIXED)
806 || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
807 || !VALID_PTR(pPCHSGeometry)
808 || !VALID_PTR(pLCHSGeometry))
809 {
810 rc = VERR_INVALID_PARAMETER;
811 goto out;
812 }
813
814 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
815 if (!pImage)
816 {
817 rc = VERR_NO_MEMORY;
818 goto out;
819 }
820 pImage->pszFilename = pszFilename;
821 pImage->paBlocks = NULL;
822 pImage->pfnError = pfnError;
823 pImage->pvErrorUser = pvErrorUser;
824
825 rc = vdiCreateImage(pImage, enmType, cbSize, uImageFlags, pszComment,
826 pPCHSGeometry, pLCHSGeometry,
827 pfnProgress, pvUser, uPercentStart, uPercentSpan);
828 if (VBOX_SUCCESS(rc))
829 {
830 /* So far the image is opened in read/write mode. Make sure the
831 * image is opened in read-only mode if the caller requested that. */
832 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
833 {
834 vdiFreeImage(pImage, false);
835 rc = vdiOpenImage(pImage, uOpenFlags);
836 if (VBOX_FAILURE(rc))
837 goto out;
838 }
839 *ppBackendData = pImage;
840 }
841
842out:
843 LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
844 return rc;
845}
846
847/** @copydoc VBOXHDDBACKEND::pfnRename */
848static int vdiRename(void *pBackendData, const char *pszFilename)
849{
850 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
851 int rc = VERR_NOT_IMPLEMENTED;
852
853 LogFlowFunc(("returns %Vrc\n", rc));
854 return rc;
855}
856
857/** @copydoc VBOXHDDBACKEND::pfnClose */
858static int vdiClose(void *pBackendData, bool fDelete)
859{
860 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
861 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
862 int rc = VINF_SUCCESS;
863
864 /* Freeing a never allocated image (e.g. because the open failed) is
865 * not signalled as an error. After all nothing bad happens. */
866 if (pImage)
867 vdiFreeImage(pImage, fDelete);
868
869 LogFlowFunc(("returns %Vrc\n", rc));
870 return rc;
871}
872
873/** @copydoc VBOXHDDBACKEND::pfnRead */
874static int vdiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
875 size_t cbToRead, size_t *pcbActuallyRead)
876{
877 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
878 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
879 unsigned uBlock;
880 unsigned offRead;
881 int rc;
882
883 Assert(VALID_PTR(pImage));
884 Assert(!(uOffset % 512));
885 Assert(!(cbToRead % 512));
886
887 if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
888 || !VALID_PTR(pvBuf)
889 || !cbToRead)
890 {
891 rc = VERR_INVALID_PARAMETER;
892 goto out;
893 }
894
895 /* Calculate starting block number and offset inside it. */
896 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
897 offRead = (unsigned)uOffset & pImage->uBlockMask;
898
899 /* Clip read range to at most the rest of the block. */
900 cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
901 Assert(!(cbToRead % 512));
902
903 if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
904 rc = VERR_VDI_BLOCK_FREE;
905 else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
906 {
907 memset(pvBuf, 0, cbToRead);
908 rc = VINF_SUCCESS;
909 }
910 else
911 {
912 /* Block present in image file, read relevant data. */
913 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
914 + (pImage->offStartData + pImage->offStartBlockData + offRead);
915 rc = RTFileReadAt(pImage->File, u64Offset, pvBuf, cbToRead, NULL);
916 }
917
918 if (VBOX_SUCCESS(rc))
919 *pcbActuallyRead = cbToRead;
920
921out:
922 LogFlowFunc(("returns %Vrc\n", rc));
923 return rc;
924}
925
926/**@copydoc VBOXHDDBACKEND::pfnWrite */
927static int vdiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
928 size_t cbToWrite, size_t *pcbWriteProcess,
929 size_t *pcbPreRead, size_t *pcbPostRead)
930{
931 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
932 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
933 unsigned uBlock;
934 unsigned offWrite;
935 int rc = VINF_SUCCESS;
936
937 Assert(VALID_PTR(pImage));
938 Assert(!(uOffset % 512));
939 Assert(!(cbToWrite % 512));
940
941 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
942 {
943 rc = VERR_VDI_IMAGE_READ_ONLY;
944 goto out;
945 }
946
947 if (!VALID_PTR(pvBuf) || !cbToWrite)
948 {
949 rc = VERR_INVALID_PARAMETER;
950 goto out;
951 }
952
953 /* No size check here, will do that later. For dynamic images which are
954 * not multiples of the block size in length, this would prevent writing to
955 * the last grain. */
956
957 /* Calculate starting block number and offset inside it. */
958 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
959 offWrite = (unsigned)uOffset & pImage->uBlockMask;
960
961 /* Clip write range to at most the rest of the block. */
962 cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
963 Assert(!(cbToWrite % 512));
964
965 if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
966 {
967 /* Block is either free or zero. */
968 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
969 && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
970 || cbToWrite == getImageBlockSize(&pImage->Header)))
971 {
972 /* If the destination block is unallocated at this point, it's
973 * either a zero block or a block which hasn't been used so far
974 * (which also means that it's a zero block. Don't need to write
975 * anything to this block if the data consists of just zeroes. */
976 Assert(!(cbToWrite % 4));
977 Assert(cbToWrite * 8 <= UINT32_MAX);
978 if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
979 {
980 pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
981 goto out;
982 }
983 }
984
985 if (cbToWrite == getImageBlockSize(&pImage->Header))
986 {
987 /* Full block write to previously unallocated block.
988 * Allocate block and write data. */
989 Assert(!offWrite);
990 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
991 uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
992 + (pImage->offStartData + pImage->offStartBlockData);
993 rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
994 if (VBOX_FAILURE(rc))
995 goto out;
996 pImage->paBlocks[uBlock] = cBlocksAllocated;
997 setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
998
999 rc = vdiUpdateBlockInfo(pImage, uBlock);
1000 if (VBOX_FAILURE(rc))
1001 goto out;
1002
1003 *pcbPreRead = 0;
1004 *pcbPostRead = 0;
1005 }
1006 else
1007 {
1008 /* Trying to do a partial write to an unallocated block. Don't do
1009 * anything except letting the upper layer know what to do. */
1010 *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
1011 *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
1012 rc = VERR_VDI_BLOCK_FREE;
1013 }
1014 }
1015 else
1016 {
1017 /* Block present in image file, write relevant data. */
1018 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
1019 + (pImage->offStartData + pImage->offStartBlockData + offWrite);
1020 rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
1021 }
1022
1023out:
1024 LogFlowFunc(("returns %Vrc\n", rc));
1025 return rc;
1026}
1027
1028/** @copydoc VBOXHDDBACKEND::pfnFlush */
1029static int vdiFlush(void *pBackendData)
1030{
1031 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1032 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1033 int rc = VINF_SUCCESS;
1034
1035 vdiFlushImage(pImage);
1036 LogFlowFunc(("returns %Vrc\n", rc));
1037 return rc;
1038}
1039
1040/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
1041static unsigned vdiGetVersion(void *pBackendData)
1042{
1043 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1044 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1045 unsigned uVersion;
1046
1047 Assert(VALID_PTR(pImage));
1048
1049 if (pImage)
1050 uVersion = pImage->PreHeader.u32Version;
1051 else
1052 uVersion = 0;
1053
1054 LogFlowFunc(("returns %#x\n", uVersion));
1055 return uVersion;
1056}
1057
1058/** @copydoc VBOXHDDBACKEND::pfnGetImageType */
1059static int vdiGetImageType(void *pBackendData, PVDIMAGETYPE penmImageType)
1060{
1061 LogFlowFunc(("pBackendData=%#p penmImageType=%#p\n", pBackendData, penmImageType));
1062 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1063 int rc = VINF_SUCCESS;
1064
1065 Assert(VALID_PTR(pImage));
1066 Assert(VALID_PTR(penmImageType));
1067
1068 if (pImage)
1069 *penmImageType = getImageType(&pImage->Header) == VDI_IMAGE_TYPE_NORMAL
1070 ? VD_IMAGE_TYPE_NORMAL
1071 : VD_IMAGE_TYPE_DIFF;
1072 else
1073 rc = VERR_VDI_NOT_OPENED;
1074
1075 LogFlowFunc(("returns %Vrc enmImageType=%u\n", rc, *penmImageType));
1076 return rc;
1077}
1078
1079/** @copydoc VBOXHDDBACKEND::pfnGetSize */
1080static uint64_t vdiGetSize(void *pBackendData)
1081{
1082 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1083 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1084 uint64_t cbSize;
1085
1086 Assert(VALID_PTR(pImage));
1087
1088 if (pImage)
1089 cbSize = getImageDiskSize(&pImage->Header);
1090 else
1091 cbSize = 0;
1092
1093 LogFlowFunc(("returns %llu\n", cbSize));
1094 return cbSize;
1095}
1096
1097/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
1098static uint64_t vdiGetFileSize(void *pBackendData)
1099{
1100 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1101 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1102 uint64_t cb = 0;
1103
1104 Assert(VALID_PTR(pImage));
1105
1106 if (pImage)
1107 {
1108 uint64_t cbFile;
1109 if (pImage->File != NIL_RTFILE)
1110 {
1111 int rc = RTFileGetSize(pImage->File, &cbFile);
1112 if (VBOX_SUCCESS(rc))
1113 cb += cbFile;
1114 }
1115 }
1116
1117 LogFlowFunc(("returns %lld\n", cb));
1118 return cb;
1119}
1120
1121/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
1122static int vdiGetPCHSGeometry(void *pBackendData,
1123 PPDMMEDIAGEOMETRY pPCHSGeometry)
1124{
1125 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
1126 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1127 int rc;
1128
1129 Assert(VALID_PTR(pImage));
1130
1131 if (pImage)
1132 {
1133 if (pImage->PCHSGeometry.cCylinders)
1134 {
1135 *pPCHSGeometry = pImage->PCHSGeometry;
1136 rc = VINF_SUCCESS;
1137 }
1138 else
1139 rc = VERR_VDI_GEOMETRY_NOT_SET;
1140 }
1141 else
1142 rc = VERR_VDI_NOT_OPENED;
1143
1144 LogFlowFunc(("returns %Vrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1145 return rc;
1146}
1147
1148/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
1149static int vdiSetPCHSGeometry(void *pBackendData,
1150 PCPDMMEDIAGEOMETRY pPCHSGeometry)
1151{
1152 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1153 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1154 int rc;
1155
1156 Assert(VALID_PTR(pImage));
1157
1158 if (pImage)
1159 {
1160 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1161 {
1162 rc = VERR_VDI_IMAGE_READ_ONLY;
1163 goto out;
1164 }
1165
1166 pImage->PCHSGeometry = *pPCHSGeometry;
1167 rc = VINF_SUCCESS;
1168 }
1169 else
1170 rc = VERR_VDI_NOT_OPENED;
1171
1172out:
1173 LogFlowFunc(("returns %Vrc\n", rc));
1174 return rc;
1175}
1176
1177/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
1178static int vdiGetLCHSGeometry(void *pBackendData,
1179 PPDMMEDIAGEOMETRY pLCHSGeometry)
1180{
1181 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
1182 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1183 int rc;
1184
1185 Assert(VALID_PTR(pImage));
1186
1187 if (pImage)
1188 {
1189 VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
1190 PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
1191 if (!pGeometry)
1192 pGeometry = &DummyGeo;
1193
1194 if ( pGeometry->cCylinders > 0
1195 && pGeometry->cHeads > 0
1196 && pGeometry->cSectors > 0)
1197 {
1198 pLCHSGeometry->cCylinders = pGeometry->cCylinders;
1199 pLCHSGeometry->cHeads = pGeometry->cHeads;
1200 pLCHSGeometry->cSectors = pGeometry->cSectors;
1201 rc = VINF_SUCCESS;
1202 }
1203 else
1204 rc = VERR_VDI_GEOMETRY_NOT_SET;
1205 }
1206 else
1207 rc = VERR_VDI_NOT_OPENED;
1208
1209 LogFlowFunc(("returns %Vrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1210 return rc;
1211}
1212
1213/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
1214static int vdiSetLCHSGeometry(void *pBackendData,
1215 PCPDMMEDIAGEOMETRY pLCHSGeometry)
1216{
1217 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1218 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1219 PVDIDISKGEOMETRY pGeometry;
1220 int rc;
1221
1222 Assert(VALID_PTR(pImage));
1223
1224 if (pImage)
1225 {
1226 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1227 {
1228 rc = VERR_VDI_IMAGE_READ_ONLY;
1229 goto out;
1230 }
1231
1232 pGeometry = getImageLCHSGeometry(&pImage->Header);
1233 if (pGeometry)
1234 {
1235 pGeometry->cCylinders = pLCHSGeometry->cCylinders;
1236 pGeometry->cHeads = pLCHSGeometry->cHeads;
1237 pGeometry->cSectors = pLCHSGeometry->cSectors;
1238 pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
1239
1240 /* Update header information in base image file. */
1241 vdiFlushImage(pImage);
1242 }
1243 rc = VINF_SUCCESS;
1244 }
1245 else
1246 rc = VERR_VDI_NOT_OPENED;
1247
1248out:
1249 LogFlowFunc(("returns %Vrc\n", rc));
1250 return rc;
1251}
1252
1253/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
1254static unsigned vdiGetImageFlags(void *pBackendData)
1255{
1256 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1257 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1258 unsigned uImageFlags;
1259
1260 Assert(VALID_PTR(pImage));
1261
1262 if (pImage)
1263 uImageFlags = getImageFlags(&pImage->Header);
1264 else
1265 uImageFlags = 0;
1266
1267 LogFlowFunc(("returns %#x\n", uImageFlags));
1268 return uImageFlags;
1269}
1270
1271/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
1272static unsigned vdiGetOpenFlags(void *pBackendData)
1273{
1274 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1275 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1276 unsigned uOpenFlags;
1277
1278 Assert(VALID_PTR(pImage));
1279
1280 if (pImage)
1281 uOpenFlags = pImage->uOpenFlags;
1282 else
1283 uOpenFlags = 0;
1284
1285 LogFlowFunc(("returns %#x\n", uOpenFlags));
1286 return uOpenFlags;
1287}
1288
1289/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
1290static int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
1291{
1292 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
1293 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1294 int rc;
1295 const char *pszFilename;
1296
1297 /* Image must be opened and the new flags must be valid. Just readonly flag
1298 * is supported. */
1299 if (!pImage || uOpenFlags & ~VD_OPEN_FLAGS_READONLY)
1300 {
1301 rc = VERR_INVALID_PARAMETER;
1302 goto out;
1303 }
1304
1305 /* Implement this operation via reopening the image. */
1306 pszFilename = pImage->pszFilename;
1307 vdiFreeImage(pImage, false);
1308 rc = vdiOpenImage(pImage, uOpenFlags);
1309
1310out:
1311 LogFlowFunc(("returns %Vrc\n", rc));
1312 return rc;
1313}
1314
1315/** @copydoc VBOXHDDBACKEND::pfnGetComment */
1316static int vdiGetComment(void *pBackendData, char *pszComment,
1317 size_t cbComment)
1318{
1319 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
1320 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1321 int rc = VINF_SUCCESS;
1322
1323 Assert(VALID_PTR(pImage));
1324
1325 if (pImage)
1326 {
1327 char *pszTmp = getImageComment(&pImage->Header);
1328 unsigned cb = strlen(pszTmp);
1329 if (cb < cbComment)
1330 {
1331 /* memcpy is much better than strncpy. */
1332 memcpy(pszComment, pszTmp, cb + 1);
1333 }
1334 else
1335 rc = VERR_BUFFER_OVERFLOW;
1336 }
1337 else
1338 rc = VERR_VDI_NOT_OPENED;
1339
1340 LogFlowFunc(("returns %Vrc comment=\"%s\"\n", rc, pszComment));
1341 return rc;
1342}
1343
1344/** @copydoc VBOXHDDBACKEND::pfnGetComment */
1345static int vdiSetComment(void *pBackendData, const char *pszComment)
1346{
1347 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
1348 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1349 int rc;
1350
1351 Assert(VALID_PTR(pImage));
1352
1353 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1354 {
1355 rc = VERR_VDI_IMAGE_READ_ONLY;
1356 goto out;
1357 }
1358
1359 if (pImage)
1360 {
1361 size_t cchComment = pszComment ? strlen(pszComment) : 0;
1362 if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
1363 {
1364 LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
1365 rc = VERR_VDI_COMMENT_TOO_LONG;
1366 goto out;
1367 }
1368
1369 /* we don't support old style images */
1370 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1371 {
1372 /*
1373 * Update the comment field, making sure to zero out all of the previous comment.
1374 */
1375 memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
1376 memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
1377
1378 /* write out new the header */
1379 rc = vdiUpdateHeader(pImage);
1380 }
1381 else
1382 rc = VERR_VDI_UNSUPPORTED_VERSION;
1383 }
1384 else
1385 rc = VERR_VDI_NOT_OPENED;
1386
1387out:
1388 LogFlowFunc(("returns %Vrc\n", rc));
1389 return rc;
1390}
1391
1392/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
1393static int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
1394{
1395 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1396 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1397 int rc;
1398
1399 Assert(VALID_PTR(pImage));
1400
1401 if (pImage)
1402 {
1403 *pUuid = *getImageCreationUUID(&pImage->Header);
1404 rc = VINF_SUCCESS;
1405 }
1406 else
1407 rc = VERR_VDI_NOT_OPENED;
1408
1409 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1410 return rc;
1411}
1412
1413/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
1414static int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
1415{
1416 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1417 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1418 int rc = VINF_SUCCESS;
1419
1420 Assert(VALID_PTR(pImage));
1421
1422 if (pImage)
1423 {
1424 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1425 {
1426 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1427 pImage->Header.u.v1.uuidCreate = *pUuid;
1428 /* Make it possible to clone old VDIs. */
1429 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1430 pImage->Header.u.v0.uuidCreate = *pUuid;
1431 else
1432 {
1433 LogFunc(("Version is not supported!\n"));
1434 rc = VERR_VDI_UNSUPPORTED_VERSION;
1435 }
1436 }
1437 else
1438 rc = VERR_VDI_IMAGE_READ_ONLY;
1439 }
1440 else
1441 rc = VERR_VDI_NOT_OPENED;
1442
1443 LogFlowFunc(("returns %Vrc\n", rc));
1444 return rc;
1445}
1446
1447/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
1448static int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
1449{
1450 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1451 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1452 int rc;
1453
1454 Assert(VALID_PTR(pImage));
1455
1456 if (pImage)
1457 {
1458 *pUuid = *getImageModificationUUID(&pImage->Header);
1459 rc = VINF_SUCCESS;
1460 }
1461 else
1462 rc = VERR_VDI_NOT_OPENED;
1463
1464 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1465 return rc;
1466}
1467
1468/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
1469static int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
1470{
1471 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1472 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1473 int rc = VINF_SUCCESS;
1474
1475 Assert(VALID_PTR(pImage));
1476
1477 if (pImage)
1478 {
1479 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1480 {
1481 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1482 pImage->Header.u.v1.uuidModify = *pUuid;
1483 /* Make it possible to clone old VDIs. */
1484 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1485 pImage->Header.u.v0.uuidModify = *pUuid;
1486 else
1487 {
1488 LogFunc(("Version is not supported!\n"));
1489 rc = VERR_VDI_UNSUPPORTED_VERSION;
1490 }
1491 }
1492 else
1493 rc = VERR_VDI_IMAGE_READ_ONLY;
1494 }
1495 else
1496 rc = VERR_VDI_NOT_OPENED;
1497
1498 LogFlowFunc(("returns %Vrc\n", rc));
1499 return rc;
1500}
1501
1502/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
1503static int vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
1504{
1505 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1506 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1507 int rc;
1508
1509 Assert(VALID_PTR(pImage));
1510
1511 if (pImage)
1512 {
1513 *pUuid = *getImageParentUUID(&pImage->Header);
1514 rc = VINF_SUCCESS;
1515 }
1516 else
1517 rc = VERR_VDI_NOT_OPENED;
1518
1519 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1520 return rc;
1521}
1522
1523/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
1524static int vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
1525{
1526 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1527 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1528 int rc = VINF_SUCCESS;
1529
1530 Assert(VALID_PTR(pImage));
1531
1532 if (pImage)
1533 {
1534 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1535 {
1536 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1537 pImage->Header.u.v1.uuidLinkage = *pUuid;
1538 /* Make it possible to clone old VDIs. */
1539 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1540 pImage->Header.u.v0.uuidLinkage = *pUuid;
1541 else
1542 {
1543 LogFunc(("Version is not supported!\n"));
1544 rc = VERR_VDI_UNSUPPORTED_VERSION;
1545 }
1546 }
1547 else
1548 rc = VERR_VDI_IMAGE_READ_ONLY;
1549 }
1550 else
1551 rc = VERR_VDI_NOT_OPENED;
1552
1553 LogFlowFunc(("returns %Vrc\n", rc));
1554 return rc;
1555}
1556
1557/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
1558static int vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
1559{
1560 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1561 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1562 int rc;
1563
1564 Assert(VALID_PTR(pImage));
1565
1566 if (pImage)
1567 {
1568 *pUuid = *getImageParentModificationUUID(&pImage->Header);
1569 rc = VINF_SUCCESS;
1570 }
1571 else
1572 rc = VERR_VDI_NOT_OPENED;
1573
1574 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1575 return rc;
1576}
1577
1578/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
1579static int vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
1580{
1581 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1582 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1583 int rc = VINF_SUCCESS;
1584
1585 Assert(VALID_PTR(pImage));
1586
1587 if (pImage)
1588 {
1589 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1590 {
1591 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1592 pImage->Header.u.v1.uuidParentModify = *pUuid;
1593 else
1594 {
1595 LogFunc(("Version is not supported!\n"));
1596 rc = VERR_VDI_UNSUPPORTED_VERSION;
1597 }
1598 }
1599 else
1600 rc = VERR_VDI_IMAGE_READ_ONLY;
1601 }
1602 else
1603 rc = VERR_VDI_NOT_OPENED;
1604
1605 LogFlowFunc(("returns %Vrc\n", rc));
1606 return rc;
1607}
1608
1609/** @copydoc VBOXHDDBACKEND::pfnDump */
1610static void vdiDump(void *pBackendData)
1611{
1612 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1613
1614 RTLogPrintf("Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%08X\n",
1615 pImage->pszFilename,
1616 (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
1617 pImage->uOpenFlags,
1618 pImage->File);
1619 RTLogPrintf("Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
1620 pImage->PreHeader.u32Version,
1621 getImageType(&pImage->Header),
1622 getImageFlags(&pImage->Header),
1623 getImageDiskSize(&pImage->Header));
1624 RTLogPrintf("Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
1625 getImageBlockSize(&pImage->Header),
1626 getImageExtraBlockSize(&pImage->Header),
1627 getImageBlocks(&pImage->Header),
1628 getImageBlocksAllocated(&pImage->Header));
1629 RTLogPrintf("Header: offBlocks=%u offData=%u\n",
1630 getImageBlocksOffset(&pImage->Header),
1631 getImageDataOffset(&pImage->Header));
1632 PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
1633 if (pg)
1634 RTLogPrintf("Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
1635 pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
1636 RTLogPrintf("Header: uuidCreation={%Vuuid}\n", getImageCreationUUID(&pImage->Header));
1637 RTLogPrintf("Header: uuidModification={%Vuuid}\n", getImageModificationUUID(&pImage->Header));
1638 RTLogPrintf("Header: uuidParent={%Vuuid}\n", getImageParentUUID(&pImage->Header));
1639 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
1640 RTLogPrintf("Header: uuidParentModification={%Vuuid}\n", getImageParentModificationUUID(&pImage->Header));
1641 RTLogPrintf("Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
1642 pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
1643 RTLogPrintf("Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
1644 pImage->uBlockMask,
1645 pImage->cbTotalBlockData,
1646 pImage->uShiftOffset2Index,
1647 pImage->offStartBlockData);
1648
1649 unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
1650 for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
1651 {
1652 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
1653 {
1654 cBlocksNotFree++;
1655 if (pImage->paBlocks[uBlock] >= cBlocks)
1656 cBadBlocks++;
1657 }
1658 }
1659 if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
1660 {
1661 RTLogPrintf("!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
1662 cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
1663 }
1664 if (cBadBlocks)
1665 {
1666 RTLogPrintf("!! WARNING: %u bad blocks found !!\n",
1667 cBadBlocks);
1668 }
1669}
1670
1671VBOXHDDBACKEND g_VDIBackend =
1672{
1673 /* pszBackendName */
1674 "VDI",
1675 /* cbSize */
1676 sizeof(VBOXHDDBACKEND),
1677 /* pfnCheckIfValid */
1678 vdiCheckIfValid,
1679 /* pfnOpen */
1680 vdiOpen,
1681 /* pfnCreate */
1682 vdiCreate,
1683 /* pfnRename */
1684 vdiRename,
1685 /* pfnClose */
1686 vdiClose,
1687 /* pfnRead */
1688 vdiRead,
1689 /* pfnWrite */
1690 vdiWrite,
1691 /* pfnFlush */
1692 vdiFlush,
1693 /* pfnGetVersion */
1694 vdiGetVersion,
1695 /* pfnGetImageType */
1696 vdiGetImageType,
1697 /* pfnGetSize */
1698 vdiGetSize,
1699 /* pfnGetFileSize */
1700 vdiGetFileSize,
1701 /* pfnGetPCHSGeometry */
1702 vdiGetPCHSGeometry,
1703 /* pfnSetPCHSGeometry */
1704 vdiSetPCHSGeometry,
1705 /* pfnGetLCHSGeometry */
1706 vdiGetLCHSGeometry,
1707 /* pfnSetLCHSGeometry */
1708 vdiSetLCHSGeometry,
1709 /* pfnGetImageFlags */
1710 vdiGetImageFlags,
1711 /* pfnGetOpenFlags */
1712 vdiGetOpenFlags,
1713 /* pfnSetOpenFlags */
1714 vdiSetOpenFlags,
1715 /* pfnGetComment */
1716 vdiGetComment,
1717 /* pfnSetComment */
1718 vdiSetComment,
1719 /* pfnGetUuid */
1720 vdiGetUuid,
1721 /* pfnSetUuid */
1722 vdiSetUuid,
1723 /* pfnGetModificationUuid */
1724 vdiGetModificationUuid,
1725 /* pfnSetModificationUuid */
1726 vdiSetModificationUuid,
1727 /* pfnGetParentUuid */
1728 vdiGetParentUuid,
1729 /* pfnSetParentUuid */
1730 vdiSetParentUuid,
1731 /* pfnGetParentModificationUuid */
1732 vdiGetParentModificationUuid,
1733 /* pfnSetParentModificationUuid */
1734 vdiSetParentModificationUuid,
1735 /* pfnDump */
1736 vdiDump
1737};
1738
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