VirtualBox

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

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

VDI: Replace second label with do {} while(0)

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