VirtualBox

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

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

Storage: big cleanup of the VD interfaces, especially hide the linked list better.

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