VirtualBox

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

Last change on this file since 9734 was 9734, checked in by vboxsync, 16 years ago

Extended the interface with backends to support time stamps and parent's filename.

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

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