VirtualBox

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

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

API/HardDisk, Storage/VBoxHDD, Frontend/VBoxManage: eliminated base image type, which led to much unnecessary code duplication. Was triggered by VBoxManage finally being able to create all image variants the backends can support.

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