VirtualBox

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

Last change on this file since 27808 was 27808, checked in by vboxsync, 15 years ago

VBoxHDD: First part of the async I/O support

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

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