VirtualBox

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

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

VDI: Update number of bytes actually written even if the block was completely zero. Fixes cloning of VDI's (public bug #2813)

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

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