VirtualBox

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

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

Fixed a couple of long-standing thinkos (one can't just add shift values, fortunately never triggered as the per-block extra data was always 0 bytes). Cleaned up the code even more.

  • 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
377 cbTotal = pImage->offStartData
378 + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
379
380 if (enmType == VD_IMAGE_TYPE_FIXED)
381 {
382 /* check the free space on the disk and leave early if there is not
383 * sufficient space available */
384 RTFOFF cbFree = 0;
385 rc = RTFsQuerySizes(pImage->pszFilename, NULL, &cbFree, NULL, NULL);
386 if (VBOX_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbTotal))
387 {
388 rc = vdiError(pImage, VERR_DISK_FULL, RT_SRC_POS, N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
389 goto out;
390 }
391 }
392
393 if (enmType == VD_IMAGE_TYPE_FIXED)
394 {
395 /*
396 * Allocate & commit whole file if fixed image, it must be more
397 * effective than expanding file by write operations.
398 */
399 rc = RTFileSetSize(pImage->File, cbTotal);
400 }
401 else
402 {
403 /* Set file size to hold header and blocks array. */
404 rc = RTFileSetSize(pImage->File, pImage->offStartData);
405 }
406 if (VBOX_FAILURE(rc))
407 {
408 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"), pImage->pszFilename);
409 goto out;
410 }
411
412 /* Generate image last-modify uuid */
413 RTUuidCreate(getImageModificationUUID(&pImage->Header));
414
415 /* Write pre-header. */
416 rc = RTFileWriteAt(pImage->File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
417 if (VBOX_FAILURE(rc))
418 {
419 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"), pImage->pszFilename);
420 goto out;
421 }
422
423 /* Write header. */
424 rc = RTFileWriteAt(pImage->File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
425 if (VBOX_FAILURE(rc))
426 {
427 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"), pImage->pszFilename);
428 goto out;
429 }
430
431 rc = RTFileWriteAt(pImage->File, pImage->offStartBlocks,
432 pImage->paBlocks,
433 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
434 NULL);
435 if (VBOX_FAILURE(rc))
436 {
437 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"), pImage->pszFilename);
438 goto out;
439 }
440
441 if (enmType == VD_IMAGE_TYPE_FIXED)
442 {
443 /* Fill image with zeroes. We do this for every fixed-size image since on some systems
444 * (for example Windows Vista), it takes ages to write a block near the end of a sparse
445 * file and the guest could complain about an ATA timeout. */
446
447 /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
448 * Currently supported file systems are ext4 and ocfs2. */
449
450 /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
451 const size_t cbBuf = 128 * _1K;
452 void *pvBuf = RTMemTmpAllocZ(cbBuf);
453 if (!pvBuf)
454 {
455 rc = VERR_NO_MEMORY;
456 goto out;
457 }
458
459 cbFill = (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
460 uOff = 0;
461 /* do loop to fill all image. */
462 while (uOff < cbFill)
463 {
464 unsigned cbChunk = (unsigned)RT_MIN(cbFill, cbBuf);
465
466 rc = RTFileWriteAt(pImage->File, pImage->offStartData + uOff,
467 pvBuf, cbChunk, NULL);
468 if (VBOX_FAILURE(rc))
469 {
470 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block failed for '%s'"), pImage->pszFilename);
471 goto out;
472 }
473
474 uOff += cbChunk;
475
476 if (pfnProgress)
477 {
478 rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
479 uPercentStart + uOff * uPercentSpan / cbFill,
480 pvUser);
481 if (VBOX_FAILURE(rc))
482 goto out;
483 }
484 }
485 RTMemTmpFree(pvBuf);
486 }
487
488out:
489 if (VBOX_SUCCESS(rc) && pfnProgress)
490 pfnProgress(NULL /* WARNING! pVM=NULL */,
491 uPercentStart + uPercentSpan, pvUser);
492
493 if (VBOX_FAILURE(rc))
494 vdiFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
495 return rc;
496}
497
498/**
499 * Internal: Open a VDI image.
500 */
501static int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
502{
503 int rc;
504 RTFILE File;
505
506 pImage->uOpenFlags = uOpenFlags;
507
508 /*
509 * Open the image.
510 */
511 rc = RTFileOpen(&File, pImage->pszFilename,
512 uOpenFlags & VD_OPEN_FLAGS_READONLY
513 ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
514 : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
515 if (VBOX_FAILURE(rc))
516 {
517 /* Do NOT signal an appropriate error here, as the VD layer has the
518 * choice of retrying the open if it failed. */
519 goto out;
520 }
521 pImage->File = File;
522
523 /* Read pre-header. */
524 rc = RTFileReadAt(File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader),
525 NULL);
526 if (VBOX_FAILURE(rc))
527 {
528 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
529 goto out;
530 }
531 rc = vdiValidatePreHeader(&pImage->PreHeader);
532 if (VBOX_FAILURE(rc))
533 {
534 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
535 goto out;
536 }
537
538 /* Read header. */
539 pImage->Header.uVersion = pImage->PreHeader.u32Version;
540 switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
541 {
542 case 0:
543 rc = RTFileReadAt(File, sizeof(pImage->PreHeader),
544 &pImage->Header.u.v0, sizeof(pImage->Header.u.v0),
545 NULL);
546 if (VBOX_FAILURE(rc))
547 {
548 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
549 goto out;
550 }
551 break;
552 case 1:
553 rc = RTFileReadAt(File, sizeof(pImage->PreHeader),
554 &pImage->Header.u.v1, sizeof(pImage->Header.u.v1),
555 NULL);
556 if (VBOX_FAILURE(rc))
557 {
558 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
559 goto out;
560 }
561 /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write mode.
562 * Conversion is harmless, as any VirtualBox version supporting VDI
563 * 1.1 doesn't touch fields it doesn't know about. */
564 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
565 && GET_MINOR_HEADER_VERSION(&pImage->Header) == 1
566 && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
567 {
568 pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
569 /* Mark LCHS geometry not-calculated. */
570 pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
571 pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
572 pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
573 pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
574 }
575 else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
576 {
577 /* Read the actual VDI 1.1+ header completely. */
578 rc = RTFileReadAt(File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
579 if (VBOX_FAILURE(rc))
580 {
581 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
582 goto out;
583 }
584 }
585 break;
586 default:
587 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);
588 goto out;
589 }
590
591 rc = vdiValidateHeader(&pImage->Header);
592 if (VBOX_FAILURE(rc))
593 {
594 rc = vdiError(pImage, VERR_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: invalid header in '%s'"), pImage->pszFilename);
595 goto out;
596 }
597
598 /* Setup image parameters by header. */
599 vdiSetupImageDesc(pImage);
600
601 /* Allocate memory for blocks array. */
602 pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
603 if (!pImage->paBlocks)
604 {
605 rc = VERR_NO_MEMORY;
606 goto out;
607 }
608
609 /* Read blocks array. */
610 rc = RTFileReadAt(pImage->File, pImage->offStartBlocks, pImage->paBlocks,
611 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
612 NULL);
613
614out:
615 if (VBOX_FAILURE(rc))
616 vdiFreeImage(pImage, false);
617 return rc;
618}
619
620/**
621 * Internal: Save header to file.
622 */
623static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
624{
625 int rc;
626 switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
627 {
628 case 0:
629 rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
630 break;
631 case 1:
632 if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
633 rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
634 else
635 rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
636 break;
637 default:
638 rc = VERR_VDI_UNSUPPORTED_VERSION;
639 break;
640 }
641 AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Vrc\n", pImage->pszFilename, rc));
642 return rc;
643}
644
645/**
646 * Internal: Save block pointer to file, save header to file.
647 */
648static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
649{
650 /* Update image header. */
651 int rc = vdiUpdateHeader(pImage);
652 if (VBOX_SUCCESS(rc))
653 {
654 /* write only one block pointer. */
655 rc = RTFileWriteAt(pImage->File,
656 pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
657 &pImage->paBlocks[uBlock],
658 sizeof(VDIIMAGEBLOCKPOINTER),
659 NULL);
660 AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Vrc\n",
661 uBlock, pImage->pszFilename, rc));
662 }
663 return rc;
664}
665
666/**
667 * Internal: Flush the image file to disk.
668 */
669static void vdiFlushImage(PVDIIMAGEDESC pImage)
670{
671 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
672 {
673 /* Save header. */
674 int rc = vdiUpdateHeader(pImage);
675 AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
676 pImage->pszFilename, rc));
677 RTFileFlush(pImage->File);
678 }
679}
680
681/**
682 * Internal: Free all allocated space for representing an image, and optionally
683 * delete the image from disk.
684 */
685static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
686{
687 Assert(VALID_PTR(pImage));
688
689 vdiFlushImage(pImage);
690 if (pImage->File != NIL_RTFILE)
691 {
692 RTFileClose(pImage->File);
693 pImage->File = NIL_RTFILE;
694 }
695 if (pImage->paBlocks)
696 {
697 RTMemFree(pImage->paBlocks);
698 pImage->paBlocks = NULL;
699 }
700 if (fDelete && pImage->pszFilename)
701 RTFileDelete(pImage->pszFilename);
702}
703
704
705/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
706static int vdiCheckIfValid(const char *pszFilename)
707{
708 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
709 int rc = VINF_SUCCESS;
710 PVDIIMAGEDESC pImage;
711
712 if ( !pszFilename
713 || !*pszFilename)
714 {
715 rc = VERR_INVALID_PARAMETER;
716 goto out;
717 }
718
719 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
720 if (!pImage)
721 {
722 rc = VERR_NO_MEMORY;
723 goto out;
724 }
725 pImage->pszFilename = pszFilename;
726
727 rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
728 vdiFreeImage(pImage, false);
729
730out:
731 LogFlowFunc(("returns %Vrc\n", rc));
732 return rc;
733}
734
735/** @copydoc VBOXHDDBACKEND::pfnOpen */
736static int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
737 PFNVDERROR pfnError, void *pvErrorUser,
738 void **ppBackendData)
739{
740 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x ppBackendData=%#p\n", pszFilename, uOpenFlags, ppBackendData));
741 int rc;
742 PVDIIMAGEDESC pImage;
743
744 /* Check open flags. All valid flags are supported. */
745 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
746 {
747 rc = VERR_INVALID_PARAMETER;
748 goto out;
749 }
750
751 /* Check remaining arguments. */
752 if ( !VALID_PTR(pszFilename)
753 || !*pszFilename)
754 {
755 rc = VERR_INVALID_PARAMETER;
756 goto out;
757 }
758
759 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
760 if (!pImage)
761 {
762 rc = VERR_NO_MEMORY;
763 goto out;
764 }
765 pImage->pszFilename = pszFilename;
766 pImage->paBlocks = NULL;
767 pImage->pfnError = pfnError;
768 pImage->pvErrorUser = pvErrorUser;
769
770 rc = vdiOpenImage(pImage, uOpenFlags);
771 if (VBOX_SUCCESS(rc))
772 *ppBackendData = pImage;
773
774out:
775 LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
776 return rc;
777}
778
779/** @copydoc VBOXHDDBACKEND::pfnCreate */
780static int vdiCreate(const char *pszFilename, VDIMAGETYPE enmType,
781 uint64_t cbSize, unsigned uImageFlags,
782 const char *pszComment,
783 PCPDMMEDIAGEOMETRY pPCHSGeometry,
784 PCPDMMEDIAGEOMETRY pLCHSGeometry,
785 unsigned uOpenFlags, PFNVMPROGRESS pfnProgress,
786 void *pvUser, unsigned uPercentStart,
787 unsigned uPercentSpan, PFNVDERROR pfnError,
788 void *pvErrorUser, void **ppBackendData)
789{
790 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));
791 int rc;
792 PVDIIMAGEDESC pImage;
793
794 /* Check open flags. All valid flags are supported. */
795 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
796 {
797 rc = VERR_INVALID_PARAMETER;
798 goto out;
799 }
800
801 /* Check remaining arguments. */
802 if ( !pszFilename
803 || !*pszFilename
804 || (enmType != VD_IMAGE_TYPE_NORMAL && enmType != VD_IMAGE_TYPE_FIXED)
805 || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
806 || !VALID_PTR(pPCHSGeometry)
807 || !VALID_PTR(pLCHSGeometry))
808 {
809 rc = VERR_INVALID_PARAMETER;
810 goto out;
811 }
812
813 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
814 if (!pImage)
815 {
816 rc = VERR_NO_MEMORY;
817 goto out;
818 }
819 pImage->pszFilename = pszFilename;
820 pImage->paBlocks = NULL;
821 pImage->pfnError = pfnError;
822 pImage->pvErrorUser = pvErrorUser;
823
824 rc = vdiCreateImage(pImage, enmType, cbSize, uImageFlags, pszComment,
825 pPCHSGeometry, pLCHSGeometry,
826 pfnProgress, pvUser, uPercentStart, uPercentSpan);
827 if (VBOX_SUCCESS(rc))
828 {
829 /* So far the image is opened in read/write mode. Make sure the
830 * image is opened in read-only mode if the caller requested that. */
831 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
832 {
833 vdiFreeImage(pImage, false);
834 rc = vdiOpenImage(pImage, uOpenFlags);
835 if (VBOX_FAILURE(rc))
836 goto out;
837 }
838 *ppBackendData = pImage;
839 }
840
841out:
842 LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
843 return rc;
844}
845
846/** @copydoc VBOXHDDBACKEND::pfnRename */
847static int vdiRename(void *pBackendData, const char *pszFilename)
848{
849 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
850 int rc = VERR_NOT_IMPLEMENTED;
851
852 LogFlowFunc(("returns %Vrc\n", rc));
853 return rc;
854}
855
856/** @copydoc VBOXHDDBACKEND::pfnClose */
857static int vdiClose(void *pBackendData, bool fDelete)
858{
859 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
860 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
861 int rc = VINF_SUCCESS;
862
863 /* Freeing a never allocated image (e.g. because the open failed) is
864 * not signalled as an error. After all nothing bad happens. */
865 if (pImage)
866 vdiFreeImage(pImage, fDelete);
867
868 LogFlowFunc(("returns %Vrc\n", rc));
869 return rc;
870}
871
872/** @copydoc VBOXHDDBACKEND::pfnRead */
873static int vdiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
874 size_t cbToRead, size_t *pcbActuallyRead)
875{
876 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
877 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
878 unsigned uBlock;
879 unsigned offRead;
880 int rc;
881
882 Assert(VALID_PTR(pImage));
883 Assert(!(uOffset % 512));
884 Assert(!(cbToRead % 512));
885
886 if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
887 || !VALID_PTR(pvBuf)
888 || !cbToRead)
889 {
890 rc = VERR_INVALID_PARAMETER;
891 goto out;
892 }
893
894 /* Calculate starting block number and offset inside it. */
895 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
896 offRead = (unsigned)uOffset & pImage->uBlockMask;
897
898 /* Clip read range to at most the rest of the block. */
899 cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
900 Assert(!(cbToRead % 512));
901
902 if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
903 rc = VERR_VDI_BLOCK_FREE;
904 else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
905 {
906 memset(pvBuf, 0, cbToRead);
907 rc = VINF_SUCCESS;
908 }
909 else
910 {
911 /* Block present in image file, read relevant data. */
912 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
913 + (pImage->offStartData + pImage->offStartBlockData + offRead);
914 rc = RTFileReadAt(pImage->File, u64Offset, pvBuf, cbToRead, NULL);
915 }
916
917 if (VBOX_SUCCESS(rc))
918 *pcbActuallyRead = cbToRead;
919
920out:
921 LogFlowFunc(("returns %Vrc\n", rc));
922 return rc;
923}
924
925/**@copydoc VBOXHDDBACKEND::pfnWrite */
926static int vdiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
927 size_t cbToWrite, size_t *pcbWriteProcess,
928 size_t *pcbPreRead, size_t *pcbPostRead)
929{
930 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
931 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
932 unsigned uBlock;
933 unsigned offWrite;
934 int rc = VINF_SUCCESS;
935
936 Assert(VALID_PTR(pImage));
937 Assert(!(uOffset % 512));
938 Assert(!(cbToWrite % 512));
939
940 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
941 {
942 rc = VERR_VDI_IMAGE_READ_ONLY;
943 goto out;
944 }
945
946 if (!VALID_PTR(pvBuf) || !cbToWrite)
947 {
948 rc = VERR_INVALID_PARAMETER;
949 goto out;
950 }
951
952 /* No size check here, will do that later. For dynamic images which are
953 * not multiples of the block size in length, this would prevent writing to
954 * the last grain. */
955
956 /* Calculate starting block number and offset inside it. */
957 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
958 offWrite = (unsigned)uOffset & pImage->uBlockMask;
959
960 /* Clip write range to at most the rest of the block. */
961 cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
962 Assert(!(cbToWrite % 512));
963
964 if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
965 {
966 /* Block is either free or zero. */
967 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
968 && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
969 || cbToWrite == getImageBlockSize(&pImage->Header)))
970 {
971 /* If the destination block is unallocated at this point, it's
972 * either a zero block or a block which hasn't been used so far
973 * (which also means that it's a zero block. Don't need to write
974 * anything to this block if the data consists of just zeroes. */
975 Assert(!(cbToWrite % 4));
976 Assert(cbToWrite * 8 <= UINT32_MAX);
977 if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
978 {
979 pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
980 goto out;
981 }
982 }
983
984 if (cbToWrite == getImageBlockSize(&pImage->Header))
985 {
986 /* Full block write to previously unallocated block.
987 * Allocate block and write data. */
988 Assert(!offWrite);
989 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
990 uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
991 + (pImage->offStartData + pImage->offStartBlockData);
992 rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
993 if (VBOX_FAILURE(rc))
994 goto out;
995 pImage->paBlocks[uBlock] = cBlocksAllocated;
996 setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
997
998 rc = vdiUpdateBlockInfo(pImage, uBlock);
999 if (VBOX_FAILURE(rc))
1000 goto out;
1001
1002 *pcbPreRead = 0;
1003 *pcbPostRead = 0;
1004 }
1005 else
1006 {
1007 /* Trying to do a partial write to an unallocated block. Don't do
1008 * anything except letting the upper layer know what to do. */
1009 *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
1010 *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
1011 rc = VERR_VDI_BLOCK_FREE;
1012 }
1013 }
1014 else
1015 {
1016 /* Block present in image file, write relevant data. */
1017 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
1018 + (pImage->offStartData + pImage->offStartBlockData + offWrite);
1019 rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
1020 }
1021
1022out:
1023 LogFlowFunc(("returns %Vrc\n", rc));
1024 return rc;
1025}
1026
1027/** @copydoc VBOXHDDBACKEND::pfnFlush */
1028static int vdiFlush(void *pBackendData)
1029{
1030 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1031 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1032 int rc = VINF_SUCCESS;
1033
1034 vdiFlushImage(pImage);
1035 LogFlowFunc(("returns %Vrc\n", rc));
1036 return rc;
1037}
1038
1039/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
1040static unsigned vdiGetVersion(void *pBackendData)
1041{
1042 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1043 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1044 unsigned uVersion;
1045
1046 Assert(VALID_PTR(pImage));
1047
1048 if (pImage)
1049 uVersion = pImage->PreHeader.u32Version;
1050 else
1051 uVersion = 0;
1052
1053 LogFlowFunc(("returns %#x\n", uVersion));
1054 return uVersion;
1055}
1056
1057/** @copydoc VBOXHDDBACKEND::pfnGetImageType */
1058static int vdiGetImageType(void *pBackendData, PVDIMAGETYPE penmImageType)
1059{
1060 LogFlowFunc(("pBackendData=%#p penmImageType=%#p\n", pBackendData, penmImageType));
1061 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1062 int rc = VINF_SUCCESS;
1063
1064 Assert(VALID_PTR(pImage));
1065 Assert(VALID_PTR(penmImageType));
1066
1067 if (pImage)
1068 *penmImageType = getImageType(&pImage->Header) == VDI_IMAGE_TYPE_NORMAL
1069 ? VD_IMAGE_TYPE_NORMAL
1070 : VD_IMAGE_TYPE_DIFF;
1071 else
1072 rc = VERR_VDI_NOT_OPENED;
1073
1074 LogFlowFunc(("returns %Vrc enmImageType=%u\n", rc, *penmImageType));
1075 return rc;
1076}
1077
1078/** @copydoc VBOXHDDBACKEND::pfnGetSize */
1079static uint64_t vdiGetSize(void *pBackendData)
1080{
1081 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1082 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1083 uint64_t cbSize;
1084
1085 Assert(VALID_PTR(pImage));
1086
1087 if (pImage)
1088 cbSize = getImageDiskSize(&pImage->Header);
1089 else
1090 cbSize = 0;
1091
1092 LogFlowFunc(("returns %llu\n", cbSize));
1093 return cbSize;
1094}
1095
1096/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
1097static uint64_t vdiGetFileSize(void *pBackendData)
1098{
1099 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1100 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1101 uint64_t cb = 0;
1102
1103 Assert(VALID_PTR(pImage));
1104
1105 if (pImage)
1106 {
1107 uint64_t cbFile;
1108 if (pImage->File != NIL_RTFILE)
1109 {
1110 int rc = RTFileGetSize(pImage->File, &cbFile);
1111 if (VBOX_SUCCESS(rc))
1112 cb += cbFile;
1113 }
1114 }
1115
1116 LogFlowFunc(("returns %lld\n", cb));
1117 return cb;
1118}
1119
1120/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
1121static int vdiGetPCHSGeometry(void *pBackendData,
1122 PPDMMEDIAGEOMETRY pPCHSGeometry)
1123{
1124 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
1125 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1126 int rc;
1127
1128 Assert(VALID_PTR(pImage));
1129
1130 if (pImage)
1131 {
1132 if (pImage->PCHSGeometry.cCylinders)
1133 {
1134 *pPCHSGeometry = pImage->PCHSGeometry;
1135 rc = VINF_SUCCESS;
1136 }
1137 else
1138 rc = VERR_VDI_GEOMETRY_NOT_SET;
1139 }
1140 else
1141 rc = VERR_VDI_NOT_OPENED;
1142
1143 LogFlowFunc(("returns %Vrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1144 return rc;
1145}
1146
1147/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
1148static int vdiSetPCHSGeometry(void *pBackendData,
1149 PCPDMMEDIAGEOMETRY pPCHSGeometry)
1150{
1151 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1152 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1153 int rc;
1154
1155 Assert(VALID_PTR(pImage));
1156
1157 if (pImage)
1158 {
1159 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1160 {
1161 rc = VERR_VDI_IMAGE_READ_ONLY;
1162 goto out;
1163 }
1164
1165 pImage->PCHSGeometry = *pPCHSGeometry;
1166 rc = VINF_SUCCESS;
1167 }
1168 else
1169 rc = VERR_VDI_NOT_OPENED;
1170
1171out:
1172 LogFlowFunc(("returns %Vrc\n", rc));
1173 return rc;
1174}
1175
1176/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
1177static int vdiGetLCHSGeometry(void *pBackendData,
1178 PPDMMEDIAGEOMETRY pLCHSGeometry)
1179{
1180 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
1181 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1182 int rc;
1183
1184 Assert(VALID_PTR(pImage));
1185
1186 if (pImage)
1187 {
1188 VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
1189 PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
1190 if (!pGeometry)
1191 pGeometry = &DummyGeo;
1192
1193 if ( pGeometry->cCylinders > 0
1194 && pGeometry->cHeads > 0
1195 && pGeometry->cSectors > 0)
1196 {
1197 pLCHSGeometry->cCylinders = pGeometry->cCylinders;
1198 pLCHSGeometry->cHeads = pGeometry->cHeads;
1199 pLCHSGeometry->cSectors = pGeometry->cSectors;
1200 rc = VINF_SUCCESS;
1201 }
1202 else
1203 rc = VERR_VDI_GEOMETRY_NOT_SET;
1204 }
1205 else
1206 rc = VERR_VDI_NOT_OPENED;
1207
1208 LogFlowFunc(("returns %Vrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1209 return rc;
1210}
1211
1212/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
1213static int vdiSetLCHSGeometry(void *pBackendData,
1214 PCPDMMEDIAGEOMETRY pLCHSGeometry)
1215{
1216 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1217 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1218 PVDIDISKGEOMETRY pGeometry;
1219 int rc;
1220
1221 Assert(VALID_PTR(pImage));
1222
1223 if (pImage)
1224 {
1225 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1226 {
1227 rc = VERR_VDI_IMAGE_READ_ONLY;
1228 goto out;
1229 }
1230
1231 pGeometry = getImageLCHSGeometry(&pImage->Header);
1232 if (pGeometry)
1233 {
1234 pGeometry->cCylinders = pLCHSGeometry->cCylinders;
1235 pGeometry->cHeads = pLCHSGeometry->cHeads;
1236 pGeometry->cSectors = pLCHSGeometry->cSectors;
1237 pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
1238
1239 /* Update header information in base image file. */
1240 vdiFlushImage(pImage);
1241 }
1242 rc = VINF_SUCCESS;
1243 }
1244 else
1245 rc = VERR_VDI_NOT_OPENED;
1246
1247out:
1248 LogFlowFunc(("returns %Vrc\n", rc));
1249 return rc;
1250}
1251
1252/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
1253static unsigned vdiGetImageFlags(void *pBackendData)
1254{
1255 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1256 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1257 unsigned uImageFlags;
1258
1259 Assert(VALID_PTR(pImage));
1260
1261 if (pImage)
1262 uImageFlags = getImageFlags(&pImage->Header);
1263 else
1264 uImageFlags = 0;
1265
1266 LogFlowFunc(("returns %#x\n", uImageFlags));
1267 return uImageFlags;
1268}
1269
1270/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
1271static unsigned vdiGetOpenFlags(void *pBackendData)
1272{
1273 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1274 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1275 unsigned uOpenFlags;
1276
1277 Assert(VALID_PTR(pImage));
1278
1279 if (pImage)
1280 uOpenFlags = pImage->uOpenFlags;
1281 else
1282 uOpenFlags = 0;
1283
1284 LogFlowFunc(("returns %#x\n", uOpenFlags));
1285 return uOpenFlags;
1286}
1287
1288/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
1289static int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
1290{
1291 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
1292 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1293 int rc;
1294 const char *pszFilename;
1295
1296 /* Image must be opened and the new flags must be valid. Just readonly flag
1297 * is supported. */
1298 if (!pImage || uOpenFlags & ~VD_OPEN_FLAGS_READONLY)
1299 {
1300 rc = VERR_INVALID_PARAMETER;
1301 goto out;
1302 }
1303
1304 /* Implement this operation via reopening the image. */
1305 pszFilename = pImage->pszFilename;
1306 vdiFreeImage(pImage, false);
1307 rc = vdiOpenImage(pImage, uOpenFlags);
1308
1309out:
1310 LogFlowFunc(("returns %Vrc\n", rc));
1311 return rc;
1312}
1313
1314/** @copydoc VBOXHDDBACKEND::pfnGetComment */
1315static int vdiGetComment(void *pBackendData, char *pszComment,
1316 size_t cbComment)
1317{
1318 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
1319 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1320 int rc = VINF_SUCCESS;
1321
1322 Assert(VALID_PTR(pImage));
1323
1324 if (pImage)
1325 {
1326 char *pszTmp = getImageComment(&pImage->Header);
1327 unsigned cb = strlen(pszTmp);
1328 if (cb < cbComment)
1329 {
1330 /* memcpy is much better than strncpy. */
1331 memcpy(pszComment, pszTmp, cb + 1);
1332 }
1333 else
1334 rc = VERR_BUFFER_OVERFLOW;
1335 }
1336 else
1337 rc = VERR_VDI_NOT_OPENED;
1338
1339 LogFlowFunc(("returns %Vrc comment=\"%s\"\n", rc, pszComment));
1340 return rc;
1341}
1342
1343/** @copydoc VBOXHDDBACKEND::pfnGetComment */
1344static int vdiSetComment(void *pBackendData, const char *pszComment)
1345{
1346 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
1347 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1348 int rc;
1349
1350 Assert(VALID_PTR(pImage));
1351
1352 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1353 {
1354 rc = VERR_VDI_IMAGE_READ_ONLY;
1355 goto out;
1356 }
1357
1358 if (pImage)
1359 {
1360 size_t cchComment = pszComment ? strlen(pszComment) : 0;
1361 if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
1362 {
1363 LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
1364 rc = VERR_VDI_COMMENT_TOO_LONG;
1365 goto out;
1366 }
1367
1368 /* we don't support old style images */
1369 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1370 {
1371 /*
1372 * Update the comment field, making sure to zero out all of the previous comment.
1373 */
1374 memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
1375 memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
1376
1377 /* write out new the header */
1378 rc = vdiUpdateHeader(pImage);
1379 }
1380 else
1381 rc = VERR_VDI_UNSUPPORTED_VERSION;
1382 }
1383 else
1384 rc = VERR_VDI_NOT_OPENED;
1385
1386out:
1387 LogFlowFunc(("returns %Vrc\n", rc));
1388 return rc;
1389}
1390
1391/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
1392static int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
1393{
1394 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1395 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1396 int rc;
1397
1398 Assert(VALID_PTR(pImage));
1399
1400 if (pImage)
1401 {
1402 *pUuid = *getImageCreationUUID(&pImage->Header);
1403 rc = VINF_SUCCESS;
1404 }
1405 else
1406 rc = VERR_VDI_NOT_OPENED;
1407
1408 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1409 return rc;
1410}
1411
1412/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
1413static int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
1414{
1415 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1416 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1417 int rc = VINF_SUCCESS;
1418
1419 Assert(VALID_PTR(pImage));
1420
1421 if (pImage)
1422 {
1423 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1424 {
1425 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1426 pImage->Header.u.v1.uuidCreate = *pUuid;
1427 /* Make it possible to clone old VDIs. */
1428 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1429 pImage->Header.u.v0.uuidCreate = *pUuid;
1430 else
1431 {
1432 LogFunc(("Version is not supported!\n"));
1433 rc = VERR_VDI_UNSUPPORTED_VERSION;
1434 }
1435 }
1436 else
1437 rc = VERR_VDI_IMAGE_READ_ONLY;
1438 }
1439 else
1440 rc = VERR_VDI_NOT_OPENED;
1441
1442 LogFlowFunc(("returns %Vrc\n", rc));
1443 return rc;
1444}
1445
1446/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
1447static int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
1448{
1449 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1450 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1451 int rc;
1452
1453 Assert(VALID_PTR(pImage));
1454
1455 if (pImage)
1456 {
1457 *pUuid = *getImageModificationUUID(&pImage->Header);
1458 rc = VINF_SUCCESS;
1459 }
1460 else
1461 rc = VERR_VDI_NOT_OPENED;
1462
1463 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1464 return rc;
1465}
1466
1467/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
1468static int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
1469{
1470 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1471 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1472 int rc = VINF_SUCCESS;
1473
1474 Assert(VALID_PTR(pImage));
1475
1476 if (pImage)
1477 {
1478 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1479 {
1480 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1481 pImage->Header.u.v1.uuidModify = *pUuid;
1482 /* Make it possible to clone old VDIs. */
1483 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1484 pImage->Header.u.v0.uuidModify = *pUuid;
1485 else
1486 {
1487 LogFunc(("Version is not supported!\n"));
1488 rc = VERR_VDI_UNSUPPORTED_VERSION;
1489 }
1490 }
1491 else
1492 rc = VERR_VDI_IMAGE_READ_ONLY;
1493 }
1494 else
1495 rc = VERR_VDI_NOT_OPENED;
1496
1497 LogFlowFunc(("returns %Vrc\n", rc));
1498 return rc;
1499}
1500
1501/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
1502static int vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
1503{
1504 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1505 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1506 int rc;
1507
1508 Assert(VALID_PTR(pImage));
1509
1510 if (pImage)
1511 {
1512 *pUuid = *getImageParentUUID(&pImage->Header);
1513 rc = VINF_SUCCESS;
1514 }
1515 else
1516 rc = VERR_VDI_NOT_OPENED;
1517
1518 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1519 return rc;
1520}
1521
1522/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
1523static int vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
1524{
1525 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1526 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1527 int rc = VINF_SUCCESS;
1528
1529 Assert(VALID_PTR(pImage));
1530
1531 if (pImage)
1532 {
1533 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1534 {
1535 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1536 pImage->Header.u.v1.uuidLinkage = *pUuid;
1537 /* Make it possible to clone old VDIs. */
1538 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1539 pImage->Header.u.v0.uuidLinkage = *pUuid;
1540 else
1541 {
1542 LogFunc(("Version is not supported!\n"));
1543 rc = VERR_VDI_UNSUPPORTED_VERSION;
1544 }
1545 }
1546 else
1547 rc = VERR_VDI_IMAGE_READ_ONLY;
1548 }
1549 else
1550 rc = VERR_VDI_NOT_OPENED;
1551
1552 LogFlowFunc(("returns %Vrc\n", rc));
1553 return rc;
1554}
1555
1556/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
1557static int vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
1558{
1559 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1560 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1561 int rc;
1562
1563 Assert(VALID_PTR(pImage));
1564
1565 if (pImage)
1566 {
1567 *pUuid = *getImageParentModificationUUID(&pImage->Header);
1568 rc = VINF_SUCCESS;
1569 }
1570 else
1571 rc = VERR_VDI_NOT_OPENED;
1572
1573 LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
1574 return rc;
1575}
1576
1577/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
1578static int vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
1579{
1580 LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
1581 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1582 int rc = VINF_SUCCESS;
1583
1584 Assert(VALID_PTR(pImage));
1585
1586 if (pImage)
1587 {
1588 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1589 {
1590 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1591 pImage->Header.u.v1.uuidParentModify = *pUuid;
1592 else
1593 {
1594 LogFunc(("Version is not supported!\n"));
1595 rc = VERR_VDI_UNSUPPORTED_VERSION;
1596 }
1597 }
1598 else
1599 rc = VERR_VDI_IMAGE_READ_ONLY;
1600 }
1601 else
1602 rc = VERR_VDI_NOT_OPENED;
1603
1604 LogFlowFunc(("returns %Vrc\n", rc));
1605 return rc;
1606}
1607
1608/** @copydoc VBOXHDDBACKEND::pfnDump */
1609static void vdiDump(void *pBackendData)
1610{
1611 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1612
1613 RTLogPrintf("Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%08X\n",
1614 pImage->pszFilename,
1615 (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
1616 pImage->uOpenFlags,
1617 pImage->File);
1618 RTLogPrintf("Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
1619 pImage->PreHeader.u32Version,
1620 getImageType(&pImage->Header),
1621 getImageFlags(&pImage->Header),
1622 getImageDiskSize(&pImage->Header));
1623 RTLogPrintf("Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
1624 getImageBlockSize(&pImage->Header),
1625 getImageExtraBlockSize(&pImage->Header),
1626 getImageBlocks(&pImage->Header),
1627 getImageBlocksAllocated(&pImage->Header));
1628 RTLogPrintf("Header: offBlocks=%u offData=%u\n",
1629 getImageBlocksOffset(&pImage->Header),
1630 getImageDataOffset(&pImage->Header));
1631 PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
1632 if (pg)
1633 RTLogPrintf("Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
1634 pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
1635 RTLogPrintf("Header: uuidCreation={%Vuuid}\n", getImageCreationUUID(&pImage->Header));
1636 RTLogPrintf("Header: uuidModification={%Vuuid}\n", getImageModificationUUID(&pImage->Header));
1637 RTLogPrintf("Header: uuidParent={%Vuuid}\n", getImageParentUUID(&pImage->Header));
1638 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
1639 RTLogPrintf("Header: uuidParentModification={%Vuuid}\n", getImageParentModificationUUID(&pImage->Header));
1640 RTLogPrintf("Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
1641 pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
1642 RTLogPrintf("Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
1643 pImage->uBlockMask,
1644 pImage->cbTotalBlockData,
1645 pImage->uShiftOffset2Index,
1646 pImage->offStartBlockData);
1647
1648 unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
1649 for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
1650 {
1651 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
1652 {
1653 cBlocksNotFree++;
1654 if (pImage->paBlocks[uBlock] >= cBlocks)
1655 cBadBlocks++;
1656 }
1657 }
1658 if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
1659 {
1660 RTLogPrintf("!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
1661 cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
1662 }
1663 if (cBadBlocks)
1664 {
1665 RTLogPrintf("!! WARNING: %u bad blocks found !!\n",
1666 cBadBlocks);
1667 }
1668}
1669
1670VBOXHDDBACKEND g_VDIBackend =
1671{
1672 /* pszBackendName */
1673 "VDI",
1674 /* cbSize */
1675 sizeof(VBOXHDDBACKEND),
1676 /* pfnCheckIfValid */
1677 vdiCheckIfValid,
1678 /* pfnOpen */
1679 vdiOpen,
1680 /* pfnCreate */
1681 vdiCreate,
1682 /* pfnRename */
1683 vdiRename,
1684 /* pfnClose */
1685 vdiClose,
1686 /* pfnRead */
1687 vdiRead,
1688 /* pfnWrite */
1689 vdiWrite,
1690 /* pfnFlush */
1691 vdiFlush,
1692 /* pfnGetVersion */
1693 vdiGetVersion,
1694 /* pfnGetImageType */
1695 vdiGetImageType,
1696 /* pfnGetSize */
1697 vdiGetSize,
1698 /* pfnGetFileSize */
1699 vdiGetFileSize,
1700 /* pfnGetPCHSGeometry */
1701 vdiGetPCHSGeometry,
1702 /* pfnSetPCHSGeometry */
1703 vdiSetPCHSGeometry,
1704 /* pfnGetLCHSGeometry */
1705 vdiGetLCHSGeometry,
1706 /* pfnSetLCHSGeometry */
1707 vdiSetLCHSGeometry,
1708 /* pfnGetImageFlags */
1709 vdiGetImageFlags,
1710 /* pfnGetOpenFlags */
1711 vdiGetOpenFlags,
1712 /* pfnSetOpenFlags */
1713 vdiSetOpenFlags,
1714 /* pfnGetComment */
1715 vdiGetComment,
1716 /* pfnSetComment */
1717 vdiSetComment,
1718 /* pfnGetUuid */
1719 vdiGetUuid,
1720 /* pfnSetUuid */
1721 vdiSetUuid,
1722 /* pfnGetModificationUuid */
1723 vdiGetModificationUuid,
1724 /* pfnSetModificationUuid */
1725 vdiSetModificationUuid,
1726 /* pfnGetParentUuid */
1727 vdiGetParentUuid,
1728 /* pfnSetParentUuid */
1729 vdiSetParentUuid,
1730 /* pfnGetParentModificationUuid */
1731 vdiGetParentModificationUuid,
1732 /* pfnSetParentModificationUuid */
1733 vdiSetParentModificationUuid,
1734 /* pfnDump */
1735 vdiDump
1736};
1737
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