VirtualBox

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

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

Devices: %Vuuid -> %RTuuid (just preferred, not mandatory (yet))

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