VirtualBox

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

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

Storage: Convert the backends to use the interface for I/O rather than using the RTFile* API directly.

For a VM the PDMAsyncCompletion is used now as the I/O layer (fixing the I/O load problems).
Still disabled by default until it was tested on all supported platforms to not risk corruption
of images. To enable compile with VBOX_WITH_NEW_IO_CODE set.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 76.7 KB
Line 
1/** @file
2 * Virtual Disk Image (VDI), Core Code.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
17 * Clara, CA 95054 USA or visit http://www.sun.com if you need
18 * additional information or have any questions.
19 */
20
21/*******************************************************************************
22* Header Files *
23*******************************************************************************/
24#define LOG_GROUP LOG_GROUP_VD_VDI
25#include "VBoxHDD-Internal.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, uint32_t uImageFlags,
58 const char *pszComment, uint64_t cbDisk,
59 uint32_t cbBlock, uint32_t cbBlockExtra);
60static int vdiValidateHeader(PVDIHEADER pHeader);
61static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
62static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
63static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
64static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete);
65
66
67/**
68 * Internal: signal an error to the frontend.
69 */
70DECLINLINE(int) vdiError(PVDIIMAGEDESC pImage, int rc, RT_SRC_POS_DECL,
71 const char *pszFormat, ...)
72{
73 va_list va;
74 va_start(va, pszFormat);
75 if (pImage->pInterfaceError && pImage->pInterfaceErrorCallbacks)
76 pImage->pInterfaceErrorCallbacks->pfnError(pImage->pInterfaceError->pvUser,
77 rc, RT_SRC_POS_ARGS,
78 pszFormat, va);
79 va_end(va);
80 return rc;
81}
82
83
84static int vdiFileOpen(PVDIIMAGEDESC pImage, bool fReadonly, bool fCreate)
85{
86 int rc = VINF_SUCCESS;
87
88 AssertMsg(!(fReadonly && fCreate), ("Image can't be opened readonly whilebeing created\n"));
89
90#ifndef VBOX_WITH_NEW_IO_CODE
91 unsigned uFileFlags = fReadonly ? RTFILE_O_READ | RTFILE_O_DENY_NONE
92 : RTFILE_O_READWRITE | RTFILE_O_DENY_WRITE;
93
94 if (fCreate)
95 uFileFlags |= RTFILE_O_CREATE;
96 else
97 uFileFlags |= RTFILE_O_OPEN;
98
99 rc = RTFileOpen(&pImage->File, pImage->pszFilename, uFileFlags);
100#else
101
102 unsigned uOpenFlags = fReadonly ? VD_INTERFACEASYNCIO_OPEN_FLAGS_READONLY : 0;
103
104 if (fCreate)
105 uOpenFlags |= VD_INTERFACEASYNCIO_OPEN_FLAGS_CREATE;
106
107 rc = pImage->pInterfaceAsyncIOCallbacks->pfnOpen(pImage->pInterfaceAsyncIO->pvUser,
108 pImage->pszFilename,
109 uOpenFlags,
110 NULL, &pImage->pvStorage);
111#endif
112
113 return rc;
114}
115
116static int vdiFileClose(PVDIIMAGEDESC pImage)
117{
118 int rc = VINF_SUCCESS;
119
120#ifndef VBOX_WITH_NEW_IO_CODE
121 if (pImage->File != NIL_RTFILE)
122 rc = RTFileClose(pImage->File);
123
124 pImage->File = NIL_RTFILE;
125#else
126 if (pImage->pvStorage)
127 rc = pImage->pInterfaceAsyncIOCallbacks->pfnClose(pImage->pInterfaceAsyncIO->pvUser,
128 pImage->pvStorage);
129
130 pImage->pvStorage = NULL;
131#endif
132
133 return rc;
134}
135
136static int vdiFileFlushSync(PVDIIMAGEDESC pImage)
137{
138 int rc = VINF_SUCCESS;
139
140#ifndef VBOX_WITH_NEW_IO_CODE
141 rc = RTFileFlush(pImage->File);
142#else
143 if (pImage->pvStorage)
144 rc = pImage->pInterfaceAsyncIOCallbacks->pfnFlushSync(pImage->pInterfaceAsyncIO->pvUser,
145 pImage->pvStorage);
146#endif
147
148 return rc;
149}
150
151static int vdiFileGetSize(PVDIIMAGEDESC pImage, uint64_t *pcbSize)
152{
153 int rc = VINF_SUCCESS;
154
155#ifndef VBOX_WITH_NEW_IO_CODE
156 rc = RTFileGetSize(pImage->File, pcbSize);
157#else
158 if (pImage->pvStorage)
159 rc = pImage->pInterfaceAsyncIOCallbacks->pfnGetSize(pImage->pInterfaceAsyncIO->pvUser,
160 pImage->pvStorage,
161 pcbSize);
162#endif
163
164 return rc;
165}
166
167static int vdiFileSetSize(PVDIIMAGEDESC pImage, uint64_t cbSize)
168{
169 int rc = VINF_SUCCESS;
170
171#ifndef VBOX_WITH_NEW_IO_CODE
172 rc = RTFileSetSize(pImage->File, cbSize);
173#else
174 if (pImage->pvStorage)
175 rc = pImage->pInterfaceAsyncIOCallbacks->pfnSetSize(pImage->pInterfaceAsyncIO->pvUser,
176 pImage->pvStorage,
177 cbSize);
178#endif
179
180 return rc;
181}
182
183static int vdiFileWriteSync(PVDIIMAGEDESC pImage, uint64_t off, const void *pcvBuf, size_t cbWrite, size_t *pcbWritten)
184{
185 int rc = VINF_SUCCESS;
186
187#ifndef VBOX_WITH_NEW_IO_CODE
188 rc = RTFileWriteAt(pImage->File, off, pcvBuf, cbWrite, pcbWritten);
189#else
190 if (pImage->pvStorage)
191 rc = pImage->pInterfaceAsyncIOCallbacks->pfnWriteSync(pImage->pInterfaceAsyncIO->pvUser,
192 pImage->pvStorage,
193 off, cbWrite, pcvBuf,
194 pcbWritten);
195#endif
196
197 return rc;
198}
199
200static int vdiFileReadSync(PVDIIMAGEDESC pImage, uint64_t off, void *pvBuf, size_t cbRead, size_t *pcbRead)
201{
202 int rc = VINF_SUCCESS;
203
204#ifndef VBOX_WITH_NEW_IO_CODE
205 rc = RTFileReadAt(pImage->File, off, pvBuf, cbRead, pcbRead);
206#else
207 if (pImage->pvStorage)
208 rc = pImage->pInterfaceAsyncIOCallbacks->pfnReadSync(pImage->pInterfaceAsyncIO->pvUser,
209 pImage->pvStorage,
210 off, cbRead, pvBuf,
211 pcbRead);
212#endif
213
214 return rc;
215}
216
217static bool vdiFileOpened(PVDIIMAGEDESC pImage)
218{
219#ifndef VBOX_WITH_NEW_IO_CODE
220 return pImage->File != NIL_RTFILE;
221#else
222 return pImage->pvStorage != NULL;
223#endif
224}
225
226
227/**
228 * internal: return power of 2 or 0 if num error.
229 */
230static unsigned getPowerOfTwo(unsigned uNumber)
231{
232 if (uNumber == 0)
233 return 0;
234 unsigned uPower2 = 0;
235 while ((uNumber & 1) == 0)
236 {
237 uNumber >>= 1;
238 uPower2++;
239 }
240 return uNumber == 1 ? uPower2 : 0;
241}
242
243
244/**
245 * Internal: Init VDI preheader.
246 */
247static void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
248{
249 pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
250 pPreHdr->u32Version = VDI_IMAGE_VERSION;
251 memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
252 strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo));
253}
254
255/**
256 * Internal: check VDI preheader.
257 */
258static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
259{
260 if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
261 return VERR_VD_VDI_INVALID_HEADER;
262
263 if ( VDI_GET_VERSION_MAJOR(pPreHdr->u32Version) != VDI_IMAGE_VERSION_MAJOR
264 && pPreHdr->u32Version != 0x00000002) /* old version. */
265 return VERR_VD_VDI_UNSUPPORTED_VERSION;
266
267 return VINF_SUCCESS;
268}
269
270/**
271 * Internal: translate VD image flags to VDI image type enum.
272 */
273static VDIIMAGETYPE vdiTranslateImageFlags2VDI(unsigned uImageFlags)
274{
275 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
276 return VDI_IMAGE_TYPE_FIXED;
277 else if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
278 return VDI_IMAGE_TYPE_DIFF;
279 else
280 return VDI_IMAGE_TYPE_NORMAL;
281}
282
283/**
284 * Internal: translate VDI image type enum to VD image type enum.
285 */
286static unsigned vdiTranslateVDI2ImageFlags(VDIIMAGETYPE enmType)
287{
288 switch (enmType)
289 {
290 case VDI_IMAGE_TYPE_NORMAL:
291 return VD_IMAGE_FLAGS_NONE;
292 case VDI_IMAGE_TYPE_FIXED:
293 return VD_IMAGE_FLAGS_FIXED;
294 case VDI_IMAGE_TYPE_DIFF:
295 return VD_IMAGE_FLAGS_DIFF;
296 default:
297 AssertMsgFailed(("invalid VDIIMAGETYPE enmType=%d\n", (int)enmType));
298 return VD_IMAGE_FLAGS_NONE;
299 }
300}
301
302/**
303 * Internal: Init VDI header. Always use latest header version.
304 * @param pHeader Assumes it was initially initialized to all zeros.
305 */
306static void vdiInitHeader(PVDIHEADER pHeader, uint32_t uImageFlags,
307 const char *pszComment, uint64_t cbDisk,
308 uint32_t cbBlock, uint32_t cbBlockExtra)
309{
310 pHeader->uVersion = VDI_IMAGE_VERSION;
311 pHeader->u.v1.cbHeader = sizeof(VDIHEADER1);
312 pHeader->u.v1.u32Type = (uint32_t)vdiTranslateImageFlags2VDI(uImageFlags);
313 pHeader->u.v1.fFlags = (uImageFlags & VD_VDI_IMAGE_FLAGS_ZERO_EXPAND) ? 1 : 0;
314#ifdef VBOX_STRICT
315 char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
316 Assert(!memcmp(pHeader->u.v1.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
317#endif
318 pHeader->u.v1.szComment[0] = '\0';
319 if (pszComment)
320 {
321 AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1.szComment),
322 ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
323 strncat(pHeader->u.v1.szComment, pszComment, sizeof(pHeader->u.v1.szComment));
324 }
325
326 /* Mark the legacy geometry not-calculated. */
327 pHeader->u.v1.LegacyGeometry.cCylinders = 0;
328 pHeader->u.v1.LegacyGeometry.cHeads = 0;
329 pHeader->u.v1.LegacyGeometry.cSectors = 0;
330 pHeader->u.v1.LegacyGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
331 pHeader->u.v1.u32Dummy = 0; /* used to be the translation value */
332
333 pHeader->u.v1.cbDisk = cbDisk;
334 pHeader->u.v1.cbBlock = cbBlock;
335 pHeader->u.v1.cBlocks = (uint32_t)(cbDisk / cbBlock);
336 if (cbDisk % cbBlock)
337 pHeader->u.v1.cBlocks++;
338 pHeader->u.v1.cbBlockExtra = cbBlockExtra;
339 pHeader->u.v1.cBlocksAllocated = 0;
340
341 /* Init offsets. */
342 pHeader->u.v1.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1), VDI_GEOMETRY_SECTOR_SIZE);
343 pHeader->u.v1.offData = RT_ALIGN_32(pHeader->u.v1.offBlocks + (pHeader->u.v1.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), VDI_GEOMETRY_SECTOR_SIZE);
344
345 /* Init uuids. */
346 RTUuidCreate(&pHeader->u.v1.uuidCreate);
347 RTUuidClear(&pHeader->u.v1.uuidModify);
348 RTUuidClear(&pHeader->u.v1.uuidLinkage);
349 RTUuidClear(&pHeader->u.v1.uuidParentModify);
350
351 /* Mark LCHS geometry not-calculated. */
352 pHeader->u.v1plus.LCHSGeometry.cCylinders = 0;
353 pHeader->u.v1plus.LCHSGeometry.cHeads = 0;
354 pHeader->u.v1plus.LCHSGeometry.cSectors = 0;
355 pHeader->u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
356}
357
358/**
359 * Internal: Check VDI header.
360 */
361static int vdiValidateHeader(PVDIHEADER pHeader)
362{
363 /* Check version-dependend header parameters. */
364 switch (GET_MAJOR_HEADER_VERSION(pHeader))
365 {
366 case 0:
367 {
368 /* Old header version. */
369 break;
370 }
371 case 1:
372 {
373 /* Current header version. */
374
375 if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
376 {
377 LogRel(("VDI: v1 header size wrong (%d < %d)\n",
378 pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
379 return VERR_VD_VDI_INVALID_HEADER;
380 }
381
382 if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
383 {
384 LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
385 getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
386 return VERR_VD_VDI_INVALID_HEADER;
387 }
388
389 if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
390 {
391 LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
392 getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
393 return VERR_VD_VDI_INVALID_HEADER;
394 }
395
396 break;
397 }
398 default:
399 /* Unsupported. */
400 return VERR_VD_VDI_UNSUPPORTED_VERSION;
401 }
402
403 /* Check common header parameters. */
404
405 bool fFailed = false;
406
407 if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
408 || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
409 {
410 LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
411 fFailed = true;
412 }
413
414 if (getImageFlags(pHeader) & ~VD_VDI_IMAGE_FLAGS_MASK)
415 {
416 LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
417 fFailed = true;
418 }
419
420 if ( getImageLCHSGeometry(pHeader)
421 && (getImageLCHSGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
422 {
423 LogRel(("VDI: wrong sector size (%d != %d)\n",
424 (getImageLCHSGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
425 fFailed = true;
426 }
427
428 if ( getImageDiskSize(pHeader) == 0
429 || getImageBlockSize(pHeader) == 0
430 || getImageBlocks(pHeader) == 0
431 || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
432 {
433 LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
434 getImageDiskSize(pHeader), getImageBlockSize(pHeader),
435 getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
436 fFailed = true;
437 }
438
439 if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
440 {
441 LogRel(("VDI: too many blocks allocated (%d > %d)\n"
442 " blocksize=%d disksize=%lld\n",
443 getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
444 getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
445 fFailed = true;
446 }
447
448 if ( getImageExtraBlockSize(pHeader) != 0
449 && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
450 {
451 LogRel(("VDI: wrong extra size (%d, %d)\n",
452 getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
453 fFailed = true;
454 }
455
456 if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
457 {
458 LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
459 getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
460 fFailed = true;
461 }
462
463 if (RTUuidIsNull(getImageCreationUUID(pHeader)))
464 {
465 LogRel(("VDI: uuid of creator is 0\n"));
466 fFailed = true;
467 }
468
469 if (RTUuidIsNull(getImageModificationUUID(pHeader)))
470 {
471 LogRel(("VDI: uuid of modificator is 0\n"));
472 fFailed = true;
473 }
474
475 return fFailed ? VERR_VD_VDI_INVALID_HEADER : VINF_SUCCESS;
476}
477
478/**
479 * Internal: Set up VDIIMAGEDESC structure by image header.
480 */
481static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
482{
483 pImage->uImageFlags = getImageFlags(&pImage->Header);
484 pImage->uImageFlags |= vdiTranslateVDI2ImageFlags(getImageType(&pImage->Header));
485 pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
486 pImage->offStartData = getImageDataOffset(&pImage->Header);
487 pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
488 pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
489 pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
490 pImage->cbTotalBlockData = pImage->offStartBlockData
491 + getImageBlockSize(&pImage->Header);
492}
493
494/**
495 * Internal: Create VDI image file.
496 */
497static int vdiCreateImage(PVDIIMAGEDESC pImage, uint64_t cbSize,
498 unsigned uImageFlags, const char *pszComment,
499 PCPDMMEDIAGEOMETRY pPCHSGeometry,
500 PCPDMMEDIAGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
501 PFNVMPROGRESS pfnProgress, void *pvUser,
502 unsigned uPercentStart, unsigned uPercentSpan)
503{
504 int rc;
505 RTFILE File;
506 uint64_t cbTotal;
507 uint64_t cbFill;
508 uint64_t uOff;
509
510 /* Special check for comment length. */
511 if ( VALID_PTR(pszComment)
512 && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
513 {
514 rc = vdiError(pImage, VERR_VD_VDI_COMMENT_TOO_LONG, RT_SRC_POS, N_("VDI: comment is too long for '%s'"), pImage->pszFilename);
515 goto out;
516 }
517 AssertPtr(pPCHSGeometry);
518 AssertPtr(pLCHSGeometry);
519
520 pImage->pInterfaceError = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_ERROR);
521 if (pImage->pInterfaceError)
522 pImage->pInterfaceErrorCallbacks = VDGetInterfaceError(pImage->pInterfaceError);
523
524 vdiInitPreHeader(&pImage->PreHeader);
525 vdiInitHeader(&pImage->Header, uImageFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0);
526 /* Save PCHS geometry. Not much work, and makes the flow of information
527 * quite a bit clearer - relying on the higher level isn't obvious. */
528 pImage->PCHSGeometry = *pPCHSGeometry;
529 /* Set LCHS geometry (legacy geometry is ignored for the current 1.1+). */
530 pImage->Header.u.v1plus.LCHSGeometry.cCylinders = pLCHSGeometry->cCylinders;
531 pImage->Header.u.v1plus.LCHSGeometry.cHeads = pLCHSGeometry->cHeads;
532 pImage->Header.u.v1plus.LCHSGeometry.cSectors = pLCHSGeometry->cSectors;
533 pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
534
535 pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
536 if (!pImage->paBlocks)
537 {
538 rc = VERR_NO_MEMORY;
539 goto out;
540 }
541
542 if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
543 {
544 /* for growing images mark all blocks in paBlocks as free. */
545 for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
546 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
547 }
548 else
549 {
550 /* for fixed images mark all blocks in paBlocks as allocated */
551 for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
552 pImage->paBlocks[i] = i;
553 pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
554 }
555
556 /* Setup image parameters. */
557 vdiSetupImageDesc(pImage);
558
559 /* Create image file. */
560 rc = vdiFileOpen(pImage, false /* fReadonly */, true /* fCreate */);
561 if (RT_FAILURE(rc))
562 {
563 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: cannot create image '%s'"), pImage->pszFilename);
564 goto out;
565 }
566
567 cbTotal = pImage->offStartData
568 + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
569
570 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
571 {
572 /* Check the free space on the disk and leave early if there is not
573 * sufficient space available. */
574 RTFOFF cbFree = 0;
575 rc = RTFsQuerySizes(pImage->pszFilename, NULL, &cbFree, NULL, NULL);
576 if (RT_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbTotal))
577 {
578 rc = vdiError(pImage, VERR_DISK_FULL, RT_SRC_POS, N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
579 goto out;
580 }
581 }
582
583 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
584 {
585 /*
586 * Allocate & commit whole file if fixed image, it must be more
587 * effective than expanding file by write operations.
588 */
589 rc = vdiFileSetSize(pImage, cbTotal);
590 }
591 else
592 {
593 /* Set file size to hold header and blocks array. */
594 rc = vdiFileSetSize(pImage, pImage->offStartData);
595 }
596 if (RT_FAILURE(rc))
597 {
598 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"), pImage->pszFilename);
599 goto out;
600 }
601
602 /* Use specified image uuid */
603 *getImageCreationUUID(&pImage->Header) = *pUuid;
604
605 /* Generate image last-modify uuid */
606 RTUuidCreate(getImageModificationUUID(&pImage->Header));
607
608 /* Write pre-header. */
609 rc = vdiFileWriteSync(pImage, 0, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
610 if (RT_FAILURE(rc))
611 {
612 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"), pImage->pszFilename);
613 goto out;
614 }
615
616 /* Write header. */
617 rc = vdiFileWriteSync(pImage, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
618 if (RT_FAILURE(rc))
619 {
620 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"), pImage->pszFilename);
621 goto out;
622 }
623
624 rc = vdiFileWriteSync(pImage, pImage->offStartBlocks,
625 pImage->paBlocks,
626 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
627 NULL);
628 if (RT_FAILURE(rc))
629 {
630 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"), pImage->pszFilename);
631 goto out;
632 }
633
634 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
635 {
636 /* Fill image with zeroes. We do this for every fixed-size image since on some systems
637 * (for example Windows Vista), it takes ages to write a block near the end of a sparse
638 * file and the guest could complain about an ATA timeout. */
639
640 /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
641 * Currently supported file systems are ext4 and ocfs2. */
642
643 /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
644 const size_t cbBuf = 128 * _1K;
645 void *pvBuf = RTMemTmpAllocZ(cbBuf);
646 if (!pvBuf)
647 {
648 rc = VERR_NO_MEMORY;
649 goto out;
650 }
651
652 cbFill = (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
653 uOff = 0;
654 /* Write data to all image blocks. */
655 while (uOff < cbFill)
656 {
657 unsigned cbChunk = (unsigned)RT_MIN(cbFill, cbBuf);
658
659 rc = vdiFileWriteSync(pImage, pImage->offStartData + uOff,
660 pvBuf, cbChunk, NULL);
661 if (RT_FAILURE(rc))
662 {
663 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block failed for '%s'"), pImage->pszFilename);
664 goto out;
665 }
666
667 uOff += cbChunk;
668
669 if (pfnProgress)
670 {
671 rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
672 uPercentStart + uOff * uPercentSpan / cbFill,
673 pvUser);
674 if (RT_FAILURE(rc))
675 goto out;
676 }
677 }
678 RTMemTmpFree(pvBuf);
679 }
680
681out:
682 if (RT_SUCCESS(rc) && pfnProgress)
683 pfnProgress(NULL /* WARNING! pVM=NULL */,
684 uPercentStart + uPercentSpan, pvUser);
685
686 if (RT_FAILURE(rc))
687 vdiFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
688 return rc;
689}
690
691/**
692 * Internal: Open a VDI image.
693 */
694static int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
695{
696 int rc;
697
698 if (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
699 return VERR_NOT_SUPPORTED;
700
701 pImage->uOpenFlags = uOpenFlags;
702
703 pImage->pInterfaceError = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_ERROR);
704 if (pImage->pInterfaceError)
705 pImage->pInterfaceErrorCallbacks = VDGetInterfaceError(pImage->pInterfaceError);
706
707#ifdef VBOX_WITH_NEW_IO_CODE
708 /* Try to get async I/O interface. */
709 pImage->pInterfaceAsyncIO = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_ASYNCIO);
710 AssertPtr(pImage->pInterfaceAsyncIO);
711 pImage->pInterfaceAsyncIOCallbacks = VDGetInterfaceAsyncIO(pImage->pInterfaceAsyncIO);
712 AssertPtr(pImage->pInterfaceAsyncIOCallbacks);
713#endif
714
715 /*
716 * Open the image.
717 */
718 rc = vdiFileOpen(pImage, !!(uOpenFlags & VD_OPEN_FLAGS_READONLY), false /* fCreate */);
719 if (RT_FAILURE(rc))
720 {
721 /* Do NOT signal an appropriate error here, as the VD layer has the
722 * choice of retrying the open if it failed. */
723 goto out;
724 }
725
726 /* Read pre-header. */
727 rc = vdiFileReadSync(pImage, 0, &pImage->PreHeader, sizeof(pImage->PreHeader),
728 NULL);
729 if (RT_FAILURE(rc))
730 {
731 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
732 goto out;
733 }
734 rc = vdiValidatePreHeader(&pImage->PreHeader);
735 if (RT_FAILURE(rc))
736 {
737 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
738 goto out;
739 }
740
741 /* Read header. */
742 pImage->Header.uVersion = pImage->PreHeader.u32Version;
743 switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
744 {
745 case 0:
746 rc = vdiFileReadSync(pImage, sizeof(pImage->PreHeader),
747 &pImage->Header.u.v0, sizeof(pImage->Header.u.v0),
748 NULL);
749 if (RT_FAILURE(rc))
750 {
751 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
752 goto out;
753 }
754 break;
755 case 1:
756 rc = vdiFileReadSync(pImage, sizeof(pImage->PreHeader),
757 &pImage->Header.u.v1, sizeof(pImage->Header.u.v1),
758 NULL);
759 if (RT_FAILURE(rc))
760 {
761 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
762 goto out;
763 }
764 /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write mode.
765 * Conversion is harmless, as any VirtualBox version supporting VDI
766 * 1.1 doesn't touch fields it doesn't know about. */
767 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
768 && GET_MINOR_HEADER_VERSION(&pImage->Header) == 1
769 && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
770 {
771 pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
772 /* Mark LCHS geometry not-calculated. */
773 pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
774 pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
775 pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
776 pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
777 }
778 else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
779 {
780 /* Read the actual VDI 1.1+ header completely. */
781 rc = vdiFileReadSync(pImage, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
782 if (RT_FAILURE(rc))
783 {
784 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
785 goto out;
786 }
787 }
788 break;
789 default:
790 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);
791 goto out;
792 }
793
794 rc = vdiValidateHeader(&pImage->Header);
795 if (RT_FAILURE(rc))
796 {
797 rc = vdiError(pImage, VERR_VD_VDI_INVALID_HEADER, RT_SRC_POS, N_("VDI: invalid header in '%s'"), pImage->pszFilename);
798 goto out;
799 }
800
801 /* Setup image parameters by header. */
802 vdiSetupImageDesc(pImage);
803
804 /* Allocate memory for blocks array. */
805 pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
806 if (!pImage->paBlocks)
807 {
808 rc = VERR_NO_MEMORY;
809 goto out;
810 }
811
812 /* Read blocks array. */
813 rc = vdiFileReadSync(pImage, pImage->offStartBlocks, pImage->paBlocks,
814 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
815 NULL);
816
817out:
818 if (RT_FAILURE(rc))
819 vdiFreeImage(pImage, false);
820 return rc;
821}
822
823/**
824 * Internal: Save header to file.
825 */
826static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
827{
828 int rc;
829 switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
830 {
831 case 0:
832 rc = vdiFileWriteSync(pImage, sizeof(VDIPREHEADER), &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
833 break;
834 case 1:
835 if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
836 rc = vdiFileWriteSync(pImage, sizeof(VDIPREHEADER), &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
837 else
838 rc = vdiFileWriteSync(pImage, sizeof(VDIPREHEADER), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
839 break;
840 default:
841 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
842 break;
843 }
844 AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
845 return rc;
846}
847
848/**
849 * Internal: Save block pointer to file, save header to file.
850 */
851static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
852{
853 /* Update image header. */
854 int rc = vdiUpdateHeader(pImage);
855 if (RT_SUCCESS(rc))
856 {
857 /* write only one block pointer. */
858 rc = vdiFileWriteSync(pImage,
859 pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
860 &pImage->paBlocks[uBlock],
861 sizeof(VDIIMAGEBLOCKPOINTER),
862 NULL);
863 AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n",
864 uBlock, pImage->pszFilename, rc));
865 }
866 return rc;
867}
868
869/**
870 * Internal: Flush the image file to disk.
871 */
872static void vdiFlushImage(PVDIIMAGEDESC pImage)
873{
874 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
875 {
876 /* Save header. */
877 int rc = vdiUpdateHeader(pImage);
878 AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Rrc\n",
879 pImage->pszFilename, rc));
880 vdiFileFlushSync(pImage);
881 }
882}
883
884/**
885 * Internal: Free all allocated space for representing an image, and optionally
886 * delete the image from disk.
887 */
888static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
889{
890 AssertPtr(pImage);
891
892 if (vdiFileOpened(pImage))
893 {
894 vdiFlushImage(pImage);
895 vdiFileClose(pImage);
896 }
897 if (pImage->paBlocks)
898 {
899 RTMemFree(pImage->paBlocks);
900 pImage->paBlocks = NULL;
901 }
902 if (fDelete && pImage->pszFilename)
903 RTFileDelete(pImage->pszFilename);
904}
905
906
907/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
908static int vdiCheckIfValid(const char *pszFilename)
909{
910 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
911 int rc = VINF_SUCCESS;
912 PVDIIMAGEDESC pImage;
913
914 if ( !VALID_PTR(pszFilename)
915 || !*pszFilename)
916 {
917 rc = VERR_INVALID_PARAMETER;
918 goto out;
919 }
920
921 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
922 if (!pImage)
923 {
924 rc = VERR_NO_MEMORY;
925 goto out;
926 }
927 pImage->pszFilename = pszFilename;
928#ifndef VBOX_WITH_NEW_IO_CODE
929 pImage->File = NIL_RTFILE;
930#else
931 pImage->pvStorage = NULL;
932#endif
933 pImage->paBlocks = NULL;
934 pImage->pVDIfsDisk = NULL;
935
936 rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
937 vdiFreeImage(pImage, false);
938 RTMemFree(pImage);
939
940out:
941 LogFlowFunc(("returns %Rrc\n", rc));
942 return rc;
943}
944
945/** @copydoc VBOXHDDBACKEND::pfnOpen */
946static int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
947 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
948 void **ppBackendData)
949{
950 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
951 int rc;
952 PVDIIMAGEDESC pImage;
953
954 /* Check open flags. All valid flags are supported. */
955 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
956 {
957 rc = VERR_INVALID_PARAMETER;
958 goto out;
959 }
960
961 /* Check remaining arguments. */
962 if ( !VALID_PTR(pszFilename)
963 || !*pszFilename)
964 {
965 rc = VERR_INVALID_PARAMETER;
966 goto out;
967 }
968
969 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
970 if (!pImage)
971 {
972 rc = VERR_NO_MEMORY;
973 goto out;
974 }
975 pImage->pszFilename = pszFilename;
976#ifndef VBOX_WITH_NEW_IO_CODE
977 pImage->File = NIL_RTFILE;
978#else
979 pImage->pvStorage = NULL;
980#endif
981 pImage->paBlocks = NULL;
982 pImage->pVDIfsDisk = pVDIfsDisk;
983
984 rc = vdiOpenImage(pImage, uOpenFlags);
985 if (RT_SUCCESS(rc))
986 *ppBackendData = pImage;
987
988out:
989 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
990 return rc;
991}
992
993/** @copydoc VBOXHDDBACKEND::pfnCreate */
994static int vdiCreate(const char *pszFilename, uint64_t cbSize,
995 unsigned uImageFlags, const char *pszComment,
996 PCPDMMEDIAGEOMETRY pPCHSGeometry,
997 PCPDMMEDIAGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
998 unsigned uOpenFlags, unsigned uPercentStart,
999 unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
1000 PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation,
1001 void **ppBackendData)
1002{
1003 LogFlowFunc(("pszFilename=\"%s\" 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, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
1004 int rc;
1005 PVDIIMAGEDESC pImage;
1006
1007 PFNVMPROGRESS pfnProgress = NULL;
1008 void *pvUser = NULL;
1009 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
1010 VDINTERFACETYPE_PROGRESS);
1011 PVDINTERFACEPROGRESS pCbProgress = NULL;
1012 if (pIfProgress)
1013 {
1014 pCbProgress = VDGetInterfaceProgress(pIfProgress);
1015 if (pCbProgress)
1016 pfnProgress = pCbProgress->pfnProgress;
1017 pvUser = pIfProgress->pvUser;
1018 }
1019
1020 /* Check open flags. All valid flags are supported. */
1021 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1022 {
1023 rc = VERR_INVALID_PARAMETER;
1024 goto out;
1025 }
1026
1027 /* Check size. Maximum 2PB-3M (to be on the safe side). No tricks with
1028 * adjusting the 1M block size so far, which would extend the size. */
1029 if ( !cbSize
1030 || cbSize >= _1P * 2 - _1M * 3)
1031 {
1032 rc = VERR_VD_INVALID_SIZE;
1033 goto out;
1034 }
1035
1036 /* Check remaining arguments. */
1037 if ( !VALID_PTR(pszFilename)
1038 || !*pszFilename
1039 || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
1040 || !VALID_PTR(pPCHSGeometry)
1041 || !VALID_PTR(pLCHSGeometry))
1042 {
1043 rc = VERR_INVALID_PARAMETER;
1044 goto out;
1045 }
1046
1047 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
1048 if (!pImage)
1049 {
1050 rc = VERR_NO_MEMORY;
1051 goto out;
1052 }
1053 pImage->pszFilename = pszFilename;
1054#ifndef VBOX_WITH_NEW_IO_CODE
1055 pImage->File = NIL_RTFILE;
1056#else
1057 pImage->pvStorage = NULL;
1058#endif
1059 pImage->paBlocks = NULL;
1060 pImage->pVDIfsDisk = pVDIfsDisk;
1061
1062 rc = vdiCreateImage(pImage, cbSize, uImageFlags, pszComment,
1063 pPCHSGeometry, pLCHSGeometry, pUuid,
1064 pfnProgress, pvUser, uPercentStart, uPercentSpan);
1065 if (RT_SUCCESS(rc))
1066 {
1067 /* So far the image is opened in read/write mode. Make sure the
1068 * image is opened in read-only mode if the caller requested that. */
1069 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1070 {
1071 vdiFreeImage(pImage, false);
1072 rc = vdiOpenImage(pImage, uOpenFlags);
1073 if (RT_FAILURE(rc))
1074 goto out;
1075 }
1076 *ppBackendData = pImage;
1077 }
1078 else
1079 RTMemFree(pImage);
1080
1081out:
1082 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1083 return rc;
1084}
1085
1086/** @copydoc VBOXHDDBACKEND::pfnRename */
1087static int vdiRename(void *pBackendData, const char *pszFilename)
1088{
1089 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1090
1091 int rc = VINF_SUCCESS;
1092 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1093
1094 /* Check arguments. */
1095 if ( !pImage
1096 || !pszFilename
1097 || !*pszFilename)
1098 {
1099 rc = VERR_INVALID_PARAMETER;
1100 goto out;
1101 }
1102
1103 /* Close the image. */
1104 vdiFreeImage(pImage, false);
1105
1106 /* Rename the file. */
1107 rc = RTFileMove(pImage->pszFilename, pszFilename, 0);
1108 if (RT_FAILURE(rc))
1109 {
1110 /* The move failed, try to reopen the original image. */
1111 int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
1112 if (RT_FAILURE(rc2))
1113 rc = rc2;
1114
1115 goto out;
1116 }
1117
1118 /* Update pImage with the new information. */
1119 pImage->pszFilename = pszFilename;
1120
1121 /* Open the new image. */
1122 rc = vdiOpenImage(pImage, pImage->uOpenFlags);
1123 if (RT_FAILURE(rc))
1124 goto out;
1125
1126out:
1127 LogFlowFunc(("returns %Rrc\n", rc));
1128 return rc;
1129}
1130
1131/** @copydoc VBOXHDDBACKEND::pfnClose */
1132static int vdiClose(void *pBackendData, bool fDelete)
1133{
1134 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1135 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1136 int rc = VINF_SUCCESS;
1137
1138 /* Freeing a never allocated image (e.g. because the open failed) is
1139 * not signalled as an error. After all nothing bad happens. */
1140 if (pImage)
1141 {
1142 vdiFreeImage(pImage, fDelete);
1143 RTMemFree(pImage);
1144 }
1145
1146 LogFlowFunc(("returns %Rrc\n", rc));
1147 return rc;
1148}
1149
1150/** @copydoc VBOXHDDBACKEND::pfnRead */
1151static int vdiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
1152 size_t cbToRead, size_t *pcbActuallyRead)
1153{
1154 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
1155 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1156 unsigned uBlock;
1157 unsigned offRead;
1158 int rc;
1159
1160 AssertPtr(pImage);
1161 Assert(!(uOffset % 512));
1162 Assert(!(cbToRead % 512));
1163
1164 if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
1165 || !VALID_PTR(pvBuf)
1166 || !cbToRead)
1167 {
1168 rc = VERR_INVALID_PARAMETER;
1169 goto out;
1170 }
1171
1172 /* Calculate starting block number and offset inside it. */
1173 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
1174 offRead = (unsigned)uOffset & pImage->uBlockMask;
1175
1176 /* Clip read range to at most the rest of the block. */
1177 cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
1178 Assert(!(cbToRead % 512));
1179
1180 if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
1181 rc = VERR_VD_BLOCK_FREE;
1182 else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
1183 {
1184 memset(pvBuf, 0, cbToRead);
1185 rc = VINF_SUCCESS;
1186 }
1187 else
1188 {
1189 /* Block present in image file, read relevant data. */
1190 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
1191 + (pImage->offStartData + pImage->offStartBlockData + offRead);
1192 rc = vdiFileReadSync(pImage, u64Offset, pvBuf, cbToRead, NULL);
1193 }
1194
1195 if (pcbActuallyRead)
1196 *pcbActuallyRead = cbToRead;
1197
1198out:
1199 LogFlowFunc(("returns %Rrc\n", rc));
1200 return rc;
1201}
1202
1203/**@copydoc VBOXHDDBACKEND::pfnWrite */
1204static int vdiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
1205 size_t cbToWrite, size_t *pcbWriteProcess,
1206 size_t *pcbPreRead, size_t *pcbPostRead, unsigned fWrite)
1207{
1208 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1209 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1210 unsigned uBlock;
1211 unsigned offWrite;
1212 int rc = VINF_SUCCESS;
1213
1214 AssertPtr(pImage);
1215 Assert(!(uOffset % 512));
1216 Assert(!(cbToWrite % 512));
1217
1218 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1219 {
1220 rc = VERR_VD_IMAGE_READ_ONLY;
1221 goto out;
1222 }
1223
1224 if (!VALID_PTR(pvBuf) || !cbToWrite)
1225 {
1226 rc = VERR_INVALID_PARAMETER;
1227 goto out;
1228 }
1229
1230 /* No size check here, will do that later. For dynamic images which are
1231 * not multiples of the block size in length, this would prevent writing to
1232 * the last grain. */
1233
1234 /* Calculate starting block number and offset inside it. */
1235 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
1236 offWrite = (unsigned)uOffset & pImage->uBlockMask;
1237
1238 /* Clip write range to at most the rest of the block. */
1239 cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
1240 Assert(!(cbToWrite % 512));
1241
1242 do
1243 {
1244 if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
1245 {
1246 /* Block is either free or zero. */
1247 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
1248 && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
1249 || cbToWrite == getImageBlockSize(&pImage->Header)))
1250 {
1251 /* If the destination block is unallocated at this point, it's
1252 * either a zero block or a block which hasn't been used so far
1253 * (which also means that it's a zero block. Don't need to write
1254 * anything to this block if the data consists of just zeroes. */
1255 Assert(!(cbToWrite % 4));
1256 Assert(cbToWrite * 8 <= UINT32_MAX);
1257 if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
1258 {
1259 pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
1260 break;
1261 }
1262 }
1263
1264 if (cbToWrite == getImageBlockSize(&pImage->Header))
1265 {
1266 /* Full block write to previously unallocated block.
1267 * Allocate block and write data. */
1268 Assert(!offWrite);
1269 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
1270 uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
1271 + (pImage->offStartData + pImage->offStartBlockData);
1272 rc = vdiFileWriteSync(pImage, u64Offset, pvBuf, cbToWrite, NULL);
1273 if (RT_FAILURE(rc))
1274 goto out;
1275 pImage->paBlocks[uBlock] = cBlocksAllocated;
1276 setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
1277
1278 rc = vdiUpdateBlockInfo(pImage, uBlock);
1279 if (RT_FAILURE(rc))
1280 goto out;
1281
1282 *pcbPreRead = 0;
1283 *pcbPostRead = 0;
1284 }
1285 else
1286 {
1287 /* Trying to do a partial write to an unallocated block. Don't do
1288 * anything except letting the upper layer know what to do. */
1289 *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
1290 *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
1291 rc = VERR_VD_BLOCK_FREE;
1292 }
1293 }
1294 else
1295 {
1296 /* Block present in image file, write relevant data. */
1297 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
1298 + (pImage->offStartData + pImage->offStartBlockData + offWrite);
1299 rc = vdiFileWriteSync(pImage, u64Offset, pvBuf, cbToWrite, NULL);
1300 }
1301 } while (0);
1302
1303 if (pcbWriteProcess)
1304 *pcbWriteProcess = cbToWrite;
1305
1306out:
1307 LogFlowFunc(("returns %Rrc\n", rc));
1308 return rc;
1309}
1310
1311/** @copydoc VBOXHDDBACKEND::pfnFlush */
1312static int vdiFlush(void *pBackendData)
1313{
1314 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1315 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1316 int rc = VINF_SUCCESS;
1317
1318 Assert(pImage);
1319
1320 vdiFlushImage(pImage);
1321 LogFlowFunc(("returns %Rrc\n", rc));
1322 return rc;
1323}
1324
1325/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
1326static unsigned vdiGetVersion(void *pBackendData)
1327{
1328 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1329 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1330 unsigned uVersion;
1331
1332 AssertPtr(pImage);
1333
1334 if (pImage)
1335 uVersion = pImage->PreHeader.u32Version;
1336 else
1337 uVersion = 0;
1338
1339 LogFlowFunc(("returns %#x\n", uVersion));
1340 return uVersion;
1341}
1342
1343/** @copydoc VBOXHDDBACKEND::pfnGetSize */
1344static uint64_t vdiGetSize(void *pBackendData)
1345{
1346 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1347 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1348 uint64_t cbSize;
1349
1350 AssertPtr(pImage);
1351
1352 if (pImage)
1353 cbSize = getImageDiskSize(&pImage->Header);
1354 else
1355 cbSize = 0;
1356
1357 LogFlowFunc(("returns %llu\n", cbSize));
1358 return cbSize;
1359}
1360
1361/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
1362static uint64_t vdiGetFileSize(void *pBackendData)
1363{
1364 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1365 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1366 uint64_t cb = 0;
1367
1368 AssertPtr(pImage);
1369
1370 if (pImage)
1371 {
1372 uint64_t cbFile;
1373 if (vdiFileOpened(pImage))
1374 {
1375 int rc = vdiFileGetSize(pImage, &cbFile);
1376 if (RT_SUCCESS(rc))
1377 cb += cbFile;
1378 }
1379 }
1380
1381 LogFlowFunc(("returns %lld\n", cb));
1382 return cb;
1383}
1384
1385/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
1386static int vdiGetPCHSGeometry(void *pBackendData,
1387 PPDMMEDIAGEOMETRY pPCHSGeometry)
1388{
1389 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
1390 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1391 int rc;
1392
1393 AssertPtr(pImage);
1394
1395 if (pImage)
1396 {
1397 if (pImage->PCHSGeometry.cCylinders)
1398 {
1399 *pPCHSGeometry = pImage->PCHSGeometry;
1400 rc = VINF_SUCCESS;
1401 }
1402 else
1403 rc = VERR_VD_GEOMETRY_NOT_SET;
1404 }
1405 else
1406 rc = VERR_VD_NOT_OPENED;
1407
1408 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1409 return rc;
1410}
1411
1412/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
1413static int vdiSetPCHSGeometry(void *pBackendData,
1414 PCPDMMEDIAGEOMETRY pPCHSGeometry)
1415{
1416 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1417 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1418 int rc;
1419
1420 AssertPtr(pImage);
1421
1422 if (pImage)
1423 {
1424 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1425 {
1426 rc = VERR_VD_IMAGE_READ_ONLY;
1427 goto out;
1428 }
1429
1430 pImage->PCHSGeometry = *pPCHSGeometry;
1431 rc = VINF_SUCCESS;
1432 }
1433 else
1434 rc = VERR_VD_NOT_OPENED;
1435
1436out:
1437 LogFlowFunc(("returns %Rrc\n", rc));
1438 return rc;
1439}
1440
1441/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
1442static int vdiGetLCHSGeometry(void *pBackendData,
1443 PPDMMEDIAGEOMETRY pLCHSGeometry)
1444{
1445 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
1446 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1447 int rc;
1448
1449 AssertPtr(pImage);
1450
1451 if (pImage)
1452 {
1453 VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
1454 PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
1455 if (!pGeometry)
1456 pGeometry = &DummyGeo;
1457
1458 if ( pGeometry->cCylinders > 0
1459 && pGeometry->cHeads > 0
1460 && pGeometry->cSectors > 0)
1461 {
1462 pLCHSGeometry->cCylinders = pGeometry->cCylinders;
1463 pLCHSGeometry->cHeads = pGeometry->cHeads;
1464 pLCHSGeometry->cSectors = pGeometry->cSectors;
1465 rc = VINF_SUCCESS;
1466 }
1467 else
1468 rc = VERR_VD_GEOMETRY_NOT_SET;
1469 }
1470 else
1471 rc = VERR_VD_NOT_OPENED;
1472
1473 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1474 return rc;
1475}
1476
1477/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
1478static int vdiSetLCHSGeometry(void *pBackendData,
1479 PCPDMMEDIAGEOMETRY pLCHSGeometry)
1480{
1481 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1482 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1483 PVDIDISKGEOMETRY pGeometry;
1484 int rc;
1485
1486 AssertPtr(pImage);
1487
1488 if (pImage)
1489 {
1490 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1491 {
1492 rc = VERR_VD_IMAGE_READ_ONLY;
1493 goto out;
1494 }
1495
1496 pGeometry = getImageLCHSGeometry(&pImage->Header);
1497 if (pGeometry)
1498 {
1499 pGeometry->cCylinders = pLCHSGeometry->cCylinders;
1500 pGeometry->cHeads = pLCHSGeometry->cHeads;
1501 pGeometry->cSectors = pLCHSGeometry->cSectors;
1502 pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
1503
1504 /* Update header information in base image file. */
1505 vdiFlushImage(pImage);
1506 }
1507 rc = VINF_SUCCESS;
1508 }
1509 else
1510 rc = VERR_VD_NOT_OPENED;
1511
1512out:
1513 LogFlowFunc(("returns %Rrc\n", rc));
1514 return rc;
1515}
1516
1517/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
1518static unsigned vdiGetImageFlags(void *pBackendData)
1519{
1520 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1521 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1522 unsigned uImageFlags;
1523
1524 AssertPtr(pImage);
1525
1526 if (pImage)
1527 uImageFlags = pImage->uImageFlags;
1528 else
1529 uImageFlags = 0;
1530
1531 LogFlowFunc(("returns %#x\n", uImageFlags));
1532 return uImageFlags;
1533}
1534
1535/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
1536static unsigned vdiGetOpenFlags(void *pBackendData)
1537{
1538 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1539 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1540 unsigned uOpenFlags;
1541
1542 AssertPtr(pImage);
1543
1544 if (pImage)
1545 uOpenFlags = pImage->uOpenFlags;
1546 else
1547 uOpenFlags = 0;
1548
1549 LogFlowFunc(("returns %#x\n", uOpenFlags));
1550 return uOpenFlags;
1551}
1552
1553/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
1554static int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
1555{
1556 LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
1557 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1558 int rc;
1559 const char *pszFilename;
1560
1561 /* Image must be opened and the new flags must be valid. Just readonly and
1562 * info flags are supported. */
1563 if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO)))
1564 {
1565 rc = VERR_INVALID_PARAMETER;
1566 goto out;
1567 }
1568
1569 /* Implement this operation via reopening the image. */
1570 pszFilename = pImage->pszFilename;
1571 vdiFreeImage(pImage, false);
1572 rc = vdiOpenImage(pImage, uOpenFlags);
1573
1574out:
1575 LogFlowFunc(("returns %Rrc\n", rc));
1576 return rc;
1577}
1578
1579/** @copydoc VBOXHDDBACKEND::pfnGetComment */
1580static int vdiGetComment(void *pBackendData, char *pszComment,
1581 size_t cbComment)
1582{
1583 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
1584 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1585 int rc = VINF_SUCCESS;
1586
1587 AssertPtr(pImage);
1588
1589 if (pImage)
1590 {
1591 char *pszTmp = getImageComment(&pImage->Header);
1592 size_t cb = strlen(pszTmp);
1593 if (cb < cbComment)
1594 {
1595 /* memcpy is much better than strncpy. */
1596 memcpy(pszComment, pszTmp, cb + 1);
1597 }
1598 else
1599 rc = VERR_BUFFER_OVERFLOW;
1600 }
1601 else
1602 rc = VERR_VD_NOT_OPENED;
1603
1604 LogFlowFunc(("returns %Rrc comment=\"%s\"\n", rc, pszComment));
1605 return rc;
1606}
1607
1608/** @copydoc VBOXHDDBACKEND::pfnGetComment */
1609static int vdiSetComment(void *pBackendData, const char *pszComment)
1610{
1611 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
1612 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1613 int rc;
1614
1615 AssertPtr(pImage);
1616
1617 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1618 {
1619 rc = VERR_VD_IMAGE_READ_ONLY;
1620 goto out;
1621 }
1622
1623 if (pImage)
1624 {
1625 size_t cchComment = pszComment ? strlen(pszComment) : 0;
1626 if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
1627 {
1628 LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
1629 rc = VERR_VD_VDI_COMMENT_TOO_LONG;
1630 goto out;
1631 }
1632
1633 /* we don't support old style images */
1634 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1635 {
1636 /*
1637 * Update the comment field, making sure to zero out all of the previous comment.
1638 */
1639 memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
1640 memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
1641
1642 /* write out new the header */
1643 rc = vdiUpdateHeader(pImage);
1644 }
1645 else
1646 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
1647 }
1648 else
1649 rc = VERR_VD_NOT_OPENED;
1650
1651out:
1652 LogFlowFunc(("returns %Rrc\n", rc));
1653 return rc;
1654}
1655
1656/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
1657static int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
1658{
1659 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1660 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1661 int rc;
1662
1663 AssertPtr(pImage);
1664
1665 if (pImage)
1666 {
1667 *pUuid = *getImageCreationUUID(&pImage->Header);
1668 rc = VINF_SUCCESS;
1669 }
1670 else
1671 rc = VERR_VD_NOT_OPENED;
1672
1673 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1674 return rc;
1675}
1676
1677/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
1678static int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
1679{
1680 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1681 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1682 int rc = VINF_SUCCESS;
1683
1684 AssertPtr(pImage);
1685
1686 if (pImage)
1687 {
1688 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1689 {
1690 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1691 pImage->Header.u.v1.uuidCreate = *pUuid;
1692 /* Make it possible to clone old VDIs. */
1693 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1694 pImage->Header.u.v0.uuidCreate = *pUuid;
1695 else
1696 {
1697 LogFunc(("Version is not supported!\n"));
1698 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
1699 }
1700 }
1701 else
1702 rc = VERR_VD_IMAGE_READ_ONLY;
1703 }
1704 else
1705 rc = VERR_VD_NOT_OPENED;
1706
1707 LogFlowFunc(("returns %Rrc\n", rc));
1708 return rc;
1709}
1710
1711/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
1712static int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
1713{
1714 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1715 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1716 int rc;
1717
1718 AssertPtr(pImage);
1719
1720 if (pImage)
1721 {
1722 *pUuid = *getImageModificationUUID(&pImage->Header);
1723 rc = VINF_SUCCESS;
1724 }
1725 else
1726 rc = VERR_VD_NOT_OPENED;
1727
1728 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1729 return rc;
1730}
1731
1732/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
1733static int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
1734{
1735 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1736 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1737 int rc = VINF_SUCCESS;
1738
1739 AssertPtr(pImage);
1740
1741 if (pImage)
1742 {
1743 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1744 {
1745 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1746 pImage->Header.u.v1.uuidModify = *pUuid;
1747 /* Make it possible to clone old VDIs. */
1748 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1749 pImage->Header.u.v0.uuidModify = *pUuid;
1750 else
1751 {
1752 LogFunc(("Version is not supported!\n"));
1753 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
1754 }
1755 }
1756 else
1757 rc = VERR_VD_IMAGE_READ_ONLY;
1758 }
1759 else
1760 rc = VERR_VD_NOT_OPENED;
1761
1762 LogFlowFunc(("returns %Rrc\n", rc));
1763 return rc;
1764}
1765
1766/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
1767static int vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
1768{
1769 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1770 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1771 int rc;
1772
1773 AssertPtr(pImage);
1774
1775 if (pImage)
1776 {
1777 *pUuid = *getImageParentUUID(&pImage->Header);
1778 rc = VINF_SUCCESS;
1779 }
1780 else
1781 rc = VERR_VD_NOT_OPENED;
1782
1783 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1784 return rc;
1785}
1786
1787/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
1788static int vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
1789{
1790 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1791 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1792 int rc = VINF_SUCCESS;
1793
1794 AssertPtr(pImage);
1795
1796 if (pImage)
1797 {
1798 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1799 {
1800 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1801 pImage->Header.u.v1.uuidLinkage = *pUuid;
1802 /* Make it possible to clone old VDIs. */
1803 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
1804 pImage->Header.u.v0.uuidLinkage = *pUuid;
1805 else
1806 {
1807 LogFunc(("Version is not supported!\n"));
1808 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
1809 }
1810 }
1811 else
1812 rc = VERR_VD_IMAGE_READ_ONLY;
1813 }
1814 else
1815 rc = VERR_VD_NOT_OPENED;
1816
1817 LogFlowFunc(("returns %Rrc\n", rc));
1818 return rc;
1819}
1820
1821/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
1822static int vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
1823{
1824 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1825 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1826 int rc;
1827
1828 AssertPtr(pImage);
1829
1830 if (pImage)
1831 {
1832 *pUuid = *getImageParentModificationUUID(&pImage->Header);
1833 rc = VINF_SUCCESS;
1834 }
1835 else
1836 rc = VERR_VD_NOT_OPENED;
1837
1838 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1839 return rc;
1840}
1841
1842/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
1843static int vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
1844{
1845 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1846 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1847 int rc = VINF_SUCCESS;
1848
1849 AssertPtr(pImage);
1850
1851 if (pImage)
1852 {
1853 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1854 {
1855 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
1856 pImage->Header.u.v1.uuidParentModify = *pUuid;
1857 else
1858 {
1859 LogFunc(("Version is not supported!\n"));
1860 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
1861 }
1862 }
1863 else
1864 rc = VERR_VD_IMAGE_READ_ONLY;
1865 }
1866 else
1867 rc = VERR_VD_NOT_OPENED;
1868
1869 LogFlowFunc(("returns %Rrc\n", rc));
1870 return rc;
1871}
1872
1873/** @copydoc VBOXHDDBACKEND::pfnDump */
1874static void vdiDump(void *pBackendData)
1875{
1876 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1877
1878#ifndef VBOX_WITH_NEW_IO_CODE
1879 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%08X\n",
1880 pImage->pszFilename,
1881 (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
1882 pImage->uOpenFlags,
1883 pImage->File);
1884#else
1885 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%#p\n",
1886 pImage->pszFilename,
1887 (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
1888 pImage->uOpenFlags,
1889 pImage->pvStorage);
1890#endif
1891 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
1892 pImage->PreHeader.u32Version,
1893 getImageType(&pImage->Header),
1894 getImageFlags(&pImage->Header),
1895 getImageDiskSize(&pImage->Header));
1896 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
1897 getImageBlockSize(&pImage->Header),
1898 getImageExtraBlockSize(&pImage->Header),
1899 getImageBlocks(&pImage->Header),
1900 getImageBlocksAllocated(&pImage->Header));
1901 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: offBlocks=%u offData=%u\n",
1902 getImageBlocksOffset(&pImage->Header),
1903 getImageDataOffset(&pImage->Header));
1904 PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
1905 if (pg)
1906 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
1907 pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
1908 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: uuidCreation={%RTuuid}\n", getImageCreationUUID(&pImage->Header));
1909 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: uuidModification={%RTuuid}\n", getImageModificationUUID(&pImage->Header));
1910 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: uuidParent={%RTuuid}\n", getImageParentUUID(&pImage->Header));
1911 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
1912 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: uuidParentModification={%RTuuid}\n", getImageParentModificationUUID(&pImage->Header));
1913 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
1914 pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
1915 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
1916 pImage->uBlockMask,
1917 pImage->cbTotalBlockData,
1918 pImage->uShiftOffset2Index,
1919 pImage->offStartBlockData);
1920
1921 unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
1922 for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
1923 {
1924 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
1925 {
1926 cBlocksNotFree++;
1927 if (pImage->paBlocks[uBlock] >= cBlocks)
1928 cBadBlocks++;
1929 }
1930 }
1931 if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
1932 {
1933 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
1934 cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
1935 }
1936 if (cBadBlocks)
1937 {
1938 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "!! WARNING: %u bad blocks found !!\n",
1939 cBadBlocks);
1940 }
1941}
1942
1943static int vdiGetTimeStamp(void *pBackendData, PRTTIMESPEC pTimeStamp)
1944{
1945 int rc = VERR_NOT_IMPLEMENTED;
1946 LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
1947 return rc;
1948}
1949
1950static int vdiGetParentTimeStamp(void *pBackendData, PRTTIMESPEC pTimeStamp)
1951{
1952 int rc = VERR_NOT_IMPLEMENTED;
1953 LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
1954 return rc;
1955}
1956
1957static int vdiSetParentTimeStamp(void *pBackendData, PCRTTIMESPEC pTimeStamp)
1958{
1959 int rc = VERR_NOT_IMPLEMENTED;
1960 LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
1961 return rc;
1962}
1963
1964static int vdiGetParentFilename(void *pBackendData, char **ppszParentFilename)
1965{
1966 int rc = VERR_NOT_IMPLEMENTED;
1967 LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
1968 return rc;
1969}
1970
1971static int vdiSetParentFilename(void *pBackendData, const char *pszParentFilename)
1972{
1973 int rc = VERR_NOT_IMPLEMENTED;
1974 LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
1975 return rc;
1976}
1977
1978static bool vdiIsAsyncIOSupported(void *pBackendData)
1979{
1980 return false;
1981}
1982
1983static int vdiAsyncRead(void *pBackendData, uint64_t uOffset, size_t cbRead,
1984 PPDMDATASEG paSeg, unsigned cSeg, void *pvUser)
1985{
1986 int rc = VERR_NOT_IMPLEMENTED;
1987 LogFlowFunc(("returns %Rrc\n", rc));
1988 return rc;
1989}
1990
1991static int vdiAsyncWrite(void *pBackendData, uint64_t uOffset, size_t cbWrite,
1992 PPDMDATASEG paSeg, unsigned cSeg, void *pvUser)
1993{
1994 int rc = VERR_NOT_IMPLEMENTED;
1995 LogFlowFunc(("returns %Rrc\n", rc));
1996 return rc;
1997}
1998
1999/** @copydoc VBOXHDDBACKEND::pfnCompact */
2000static int vdiCompact(void *pBackendData, unsigned uPercentStart,
2001 unsigned uPercentSpan, PVDINTERFACE pVDIfsOperation)
2002{
2003 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2004 int rc = VINF_SUCCESS;
2005 void *pvBuf = NULL, *pvTmp = NULL;
2006 unsigned *paBlocks2 = NULL;
2007
2008 int (*pfnParentRead)(void *, uint64_t, void *, size_t) = NULL;
2009 void *pvParent = NULL;
2010 PVDINTERFACE pIfParentState = VDInterfaceGet(pVDIfsOperation,
2011 VDINTERFACETYPE_PARENTSTATE);
2012 PVDINTERFACEPARENTSTATE pCbParentState = NULL;
2013 if (pIfParentState)
2014 {
2015 pCbParentState = VDGetInterfaceParentState(pIfParentState);
2016 if (pCbParentState)
2017 pfnParentRead = pCbParentState->pfnParentRead;
2018 pvParent = pIfParentState->pvUser;
2019 }
2020
2021 PFNVMPROGRESS pfnProgress = NULL;
2022 void *pvUser = NULL;
2023 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
2024 VDINTERFACETYPE_PROGRESS);
2025 PVDINTERFACEPROGRESS pCbProgress = NULL;
2026 if (pIfProgress)
2027 {
2028 pCbProgress = VDGetInterfaceProgress(pIfProgress);
2029 if (pCbProgress)
2030 pfnProgress = pCbProgress->pfnProgress;
2031 pvUser = pIfProgress->pvUser;
2032 }
2033
2034 do {
2035 AssertBreakStmt(pImage, rc = VERR_INVALID_PARAMETER);
2036
2037 AssertBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
2038 rc = VERR_VD_IMAGE_READ_ONLY);
2039
2040 unsigned cBlocks;
2041 unsigned cBlocksToMove = 0;
2042 size_t cbBlock;
2043 cBlocks = getImageBlocks(&pImage->Header);
2044 cbBlock = getImageBlockSize(&pImage->Header);
2045 if (pfnParentRead)
2046 {
2047 pvBuf = RTMemTmpAlloc(cbBlock);
2048 AssertBreakStmt(VALID_PTR(pvBuf), rc = VERR_NO_MEMORY);
2049 }
2050 pvTmp = RTMemTmpAlloc(cbBlock);
2051 AssertBreakStmt(VALID_PTR(pvTmp), rc = VERR_NO_MEMORY);
2052
2053 uint64_t cbFile;
2054 rc = vdiFileGetSize(pImage, &cbFile);
2055 AssertRCBreak(rc);
2056 unsigned cBlocksAllocated = (unsigned)((cbFile - pImage->offStartData - pImage->offStartBlockData) >> pImage->uShiftOffset2Index);
2057 if (cBlocksAllocated == 0)
2058 {
2059 /* No data blocks in this image, no need to compact. */
2060 rc = VINF_SUCCESS;
2061 break;
2062 }
2063
2064 /* Allocate block array for back resolving. */
2065 paBlocks2 = (unsigned *)RTMemAlloc(sizeof(unsigned *) * cBlocksAllocated);
2066 AssertBreakStmt(VALID_PTR(paBlocks2), rc = VERR_NO_MEMORY);
2067 /* Fill out back resolving, check/fix allocation errors before
2068 * compacting the image, just to be on the safe side. Update the
2069 * image contents straight away, as this enables cancelling. */
2070 for (unsigned i = 0; i < cBlocksAllocated; i++)
2071 paBlocks2[i] = VDI_IMAGE_BLOCK_FREE;
2072 rc = VINF_SUCCESS;
2073 for (unsigned i = 0; i < cBlocks; i++)
2074 {
2075 VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
2076 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
2077 {
2078 if (ptrBlock < cBlocksAllocated)
2079 {
2080 if (paBlocks2[ptrBlock] == VDI_IMAGE_BLOCK_FREE)
2081 paBlocks2[ptrBlock] = i;
2082 else
2083 {
2084 LogFunc(("Freed cross-linked block %u in file \"%s\"\n",
2085 i, pImage->pszFilename));
2086 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
2087 rc = vdiUpdateBlockInfo(pImage, i);
2088 if (RT_FAILURE(rc))
2089 break;
2090 }
2091 }
2092 else
2093 {
2094 LogFunc(("Freed out of bounds reference for block %u in file \"%s\"\n",
2095 i, pImage->pszFilename));
2096 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
2097 rc = vdiUpdateBlockInfo(pImage, i);
2098 if (RT_FAILURE(rc))
2099 break;
2100 }
2101 }
2102 }
2103 if (RT_FAILURE(rc))
2104 break;
2105
2106 /* Find redundant information and update the block pointers
2107 * accordingly, creating bubbles. Keep disk up to date, as this
2108 * enables cancelling. */
2109 for (unsigned i = 0; i < cBlocks; i++)
2110 {
2111 VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
2112 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
2113 {
2114 /* Block present in image file, read relevant data. */
2115 uint64_t u64Offset = (uint64_t)ptrBlock * pImage->cbTotalBlockData
2116 + (pImage->offStartData + pImage->offStartBlockData);
2117 rc = vdiFileReadSync(pImage, u64Offset, pvTmp, cbBlock, NULL);
2118 if (RT_FAILURE(rc))
2119 break;
2120
2121 if (ASMBitFirstSet((volatile void *)pvTmp, (uint32_t)cbBlock * 8) == -1)
2122 {
2123 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
2124 rc = vdiUpdateBlockInfo(pImage, i);
2125 if (RT_FAILURE(rc))
2126 break;
2127 paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
2128 /* Adjust progress info, one block to be relocated. */
2129 cBlocksToMove++;
2130 }
2131 else if (pfnParentRead)
2132 {
2133 rc = pfnParentRead(pvParent, i * cbBlock, pvBuf, cbBlock);
2134 if (RT_FAILURE(rc))
2135 break;
2136 if (!memcmp(pvTmp, pvBuf, cbBlock))
2137 {
2138 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
2139 rc = vdiUpdateBlockInfo(pImage, i);
2140 if (RT_FAILURE(rc))
2141 break;
2142 paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
2143 /* Adjust progress info, one block to be relocated. */
2144 cBlocksToMove++;
2145 }
2146 }
2147 }
2148
2149 if (pCbProgress && pCbProgress->pfnProgress)
2150 {
2151 rc = pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
2152 (uint64_t)i * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart,
2153 pIfProgress->pvUser);
2154 if (RT_FAILURE(rc))
2155 break;
2156 }
2157 }
2158 if (RT_FAILURE(rc))
2159 break;
2160
2161 /* Fill bubbles with other data (if available). */
2162 unsigned cBlocksMoved = 0;
2163 unsigned uBlockUsedPos = cBlocksAllocated;
2164 for (unsigned i = 0; i < cBlocksAllocated; i++)
2165 {
2166 unsigned uBlock = paBlocks2[i];
2167 if (uBlock == VDI_IMAGE_BLOCK_FREE)
2168 {
2169 unsigned uBlockData = VDI_IMAGE_BLOCK_FREE;
2170 while (uBlockUsedPos > i && uBlockData == VDI_IMAGE_BLOCK_FREE)
2171 {
2172 uBlockUsedPos--;
2173 uBlockData = paBlocks2[uBlockUsedPos];
2174 }
2175 /* Terminate early if there is no block which needs copying. */
2176 if (uBlockUsedPos == i)
2177 break;
2178 uint64_t u64Offset = (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
2179 + (pImage->offStartData + pImage->offStartBlockData);
2180 rc = vdiFileReadSync(pImage, u64Offset, pvTmp, cbBlock, NULL);
2181 u64Offset = (uint64_t)i * pImage->cbTotalBlockData
2182 + (pImage->offStartData + pImage->offStartBlockData);
2183 rc = vdiFileWriteSync(pImage, u64Offset, pvTmp, cbBlock, NULL);
2184 pImage->paBlocks[uBlockData] = i;
2185 setImageBlocksAllocated(&pImage->Header, cBlocksAllocated - cBlocksMoved);
2186 rc = vdiUpdateBlockInfo(pImage, uBlockData);
2187 if (RT_FAILURE(rc))
2188 break;
2189 paBlocks2[i] = uBlockData;
2190 paBlocks2[uBlockUsedPos] = VDI_IMAGE_BLOCK_FREE;
2191 cBlocksMoved++;
2192 }
2193
2194 if (pCbProgress && pCbProgress->pfnProgress)
2195 {
2196 rc = pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
2197 (uint64_t)(cBlocks + cBlocksMoved) * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart,
2198 pIfProgress->pvUser);
2199 if (RT_FAILURE(rc))
2200 break;
2201 }
2202 }
2203 if (RT_FAILURE(rc))
2204 break;
2205
2206 /* Update image header. */
2207 setImageBlocksAllocated(&pImage->Header, uBlockUsedPos);
2208 vdiUpdateHeader(pImage);
2209
2210 /* Truncate the image to the proper size to finish compacting. */
2211 rc = vdiFileSetSize(pImage,
2212 (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
2213 + (pImage->offStartData + pImage->offStartBlockData));
2214 } while (0);
2215
2216 if (paBlocks2)
2217 RTMemTmpFree(paBlocks2);
2218 if (pvTmp)
2219 RTMemTmpFree(pvTmp);
2220 if (pvBuf)
2221 RTMemTmpFree(pvBuf);
2222
2223 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
2224 {
2225 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
2226 uPercentStart + uPercentSpan,
2227 pIfProgress->pvUser);
2228 }
2229
2230 LogFlowFunc(("returns %Rrc\n", rc));
2231 return rc;
2232}
2233
2234
2235VBOXHDDBACKEND g_VDIBackend =
2236{
2237 /* pszBackendName */
2238 "VDI",
2239 /* cbSize */
2240 sizeof(VBOXHDDBACKEND),
2241 /* uBackendCaps */
2242 VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
2243 | VD_CAP_CREATE_SPLIT_2G | VD_CAP_DIFF | VD_CAP_FILE,
2244 /* papszFileExtensions */
2245 s_apszVdiFileExtensions,
2246 /* paConfigInfo */
2247 NULL,
2248 /* hPlugin */
2249 NIL_RTLDRMOD,
2250 /* pfnCheckIfValid */
2251 vdiCheckIfValid,
2252 /* pfnOpen */
2253 vdiOpen,
2254 /* pfnCreate */
2255 vdiCreate,
2256 /* pfnRename */
2257 vdiRename,
2258 /* pfnClose */
2259 vdiClose,
2260 /* pfnRead */
2261 vdiRead,
2262 /* pfnWrite */
2263 vdiWrite,
2264 /* pfnFlush */
2265 vdiFlush,
2266 /* pfnGetVersion */
2267 vdiGetVersion,
2268 /* pfnGetSize */
2269 vdiGetSize,
2270 /* pfnGetFileSize */
2271 vdiGetFileSize,
2272 /* pfnGetPCHSGeometry */
2273 vdiGetPCHSGeometry,
2274 /* pfnSetPCHSGeometry */
2275 vdiSetPCHSGeometry,
2276 /* pfnGetLCHSGeometry */
2277 vdiGetLCHSGeometry,
2278 /* pfnSetLCHSGeometry */
2279 vdiSetLCHSGeometry,
2280 /* pfnGetImageFlags */
2281 vdiGetImageFlags,
2282 /* pfnGetOpenFlags */
2283 vdiGetOpenFlags,
2284 /* pfnSetOpenFlags */
2285 vdiSetOpenFlags,
2286 /* pfnGetComment */
2287 vdiGetComment,
2288 /* pfnSetComment */
2289 vdiSetComment,
2290 /* pfnGetUuid */
2291 vdiGetUuid,
2292 /* pfnSetUuid */
2293 vdiSetUuid,
2294 /* pfnGetModificationUuid */
2295 vdiGetModificationUuid,
2296 /* pfnSetModificationUuid */
2297 vdiSetModificationUuid,
2298 /* pfnGetParentUuid */
2299 vdiGetParentUuid,
2300 /* pfnSetParentUuid */
2301 vdiSetParentUuid,
2302 /* pfnGetParentModificationUuid */
2303 vdiGetParentModificationUuid,
2304 /* pfnSetParentModificationUuid */
2305 vdiSetParentModificationUuid,
2306 /* pfnDump */
2307 vdiDump,
2308 /* pfnGetTimeStamp */
2309 vdiGetTimeStamp,
2310 /* pfnGetParentTimeStamp */
2311 vdiGetParentTimeStamp,
2312 /* pfnSetParentTimeStamp */
2313 vdiSetParentTimeStamp,
2314 /* pfnGetParentFilename */
2315 vdiGetParentFilename,
2316 /* pfnSetParentFilename */
2317 vdiSetParentFilename,
2318 /* pfnIsAsyncIOSupported */
2319 vdiIsAsyncIOSupported,
2320 /* pfnAsyncRead */
2321 vdiAsyncRead,
2322 /* pfnAsyncWrite */
2323 vdiAsyncWrite,
2324 /* pfnComposeLocation */
2325 genericFileComposeLocation,
2326 /* pfnComposeName */
2327 genericFileComposeName,
2328 /* pfnCompact */
2329 vdiCompact
2330};
2331
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