VirtualBox

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

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

disk: ixed a bunch of NULL pointer accesses

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