VirtualBox

source: vbox/trunk/src/VBox/Storage/VHD.cpp@ 46613

Last change on this file since 46613 was 46613, checked in by vboxsync, 11 years ago

Storage: Propagate errors when closing a file but free everything nevertheless (see @bugref{6791})

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 115.3 KB
Line 
1/* $Id: VHD.cpp 46613 2013-06-18 10:27:13Z vboxsync $ */
2/** @file
3 * VHD Disk image, Core Code.
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_VD_VHD
22#include <VBox/vd-plugin.h>
23#include <VBox/err.h>
24
25#include <VBox/log.h>
26#include <VBox/version.h>
27#include <iprt/asm.h>
28#include <iprt/assert.h>
29#include <iprt/mem.h>
30#include <iprt/uuid.h>
31#include <iprt/path.h>
32#include <iprt/string.h>
33
34#define VHD_RELATIVE_MAX_PATH 512
35#define VHD_ABSOLUTE_MAX_PATH 512
36
37#define VHD_SECTOR_SIZE 512
38#define VHD_BLOCK_SIZE (2 * _1M)
39
40/* This is common to all VHD disk types and is located at the end of the image */
41#pragma pack(1)
42typedef struct VHDFooter
43{
44 char Cookie[8];
45 uint32_t Features;
46 uint32_t Version;
47 uint64_t DataOffset;
48 uint32_t TimeStamp;
49 uint8_t CreatorApp[4];
50 uint32_t CreatorVer;
51 uint32_t CreatorOS;
52 uint64_t OrigSize;
53 uint64_t CurSize;
54 uint16_t DiskGeometryCylinder;
55 uint8_t DiskGeometryHeads;
56 uint8_t DiskGeometrySectors;
57 uint32_t DiskType;
58 uint32_t Checksum;
59 char UniqueID[16];
60 uint8_t SavedState;
61 uint8_t Reserved[427];
62} VHDFooter;
63#pragma pack()
64
65/* this really is spelled with only one n */
66#define VHD_FOOTER_COOKIE "conectix"
67#define VHD_FOOTER_COOKIE_SIZE 8
68
69#define VHD_FOOTER_FEATURES_NOT_ENABLED 0
70#define VHD_FOOTER_FEATURES_TEMPORARY 1
71#define VHD_FOOTER_FEATURES_RESERVED 2
72
73#define VHD_FOOTER_FILE_FORMAT_VERSION 0x00010000
74#define VHD_FOOTER_DATA_OFFSET_FIXED UINT64_C(0xffffffffffffffff)
75#define VHD_FOOTER_DISK_TYPE_FIXED 2
76#define VHD_FOOTER_DISK_TYPE_DYNAMIC 3
77#define VHD_FOOTER_DISK_TYPE_DIFFERENCING 4
78
79#define VHD_MAX_LOCATOR_ENTRIES 8
80#define VHD_PLATFORM_CODE_NONE 0
81#define VHD_PLATFORM_CODE_WI2R 0x57693272
82#define VHD_PLATFORM_CODE_WI2K 0x5769326B
83#define VHD_PLATFORM_CODE_W2RU 0x57327275
84#define VHD_PLATFORM_CODE_W2KU 0x57326B75
85#define VHD_PLATFORM_CODE_MAC 0x4D163220
86#define VHD_PLATFORM_CODE_MACX 0x4D163258
87
88/* Header for expanding disk images. */
89#pragma pack(1)
90typedef struct VHDParentLocatorEntry
91{
92 uint32_t u32Code;
93 uint32_t u32DataSpace;
94 uint32_t u32DataLength;
95 uint32_t u32Reserved;
96 uint64_t u64DataOffset;
97} VHDPLE, *PVHDPLE;
98
99typedef struct VHDDynamicDiskHeader
100{
101 char Cookie[8];
102 uint64_t DataOffset;
103 uint64_t TableOffset;
104 uint32_t HeaderVersion;
105 uint32_t MaxTableEntries;
106 uint32_t BlockSize;
107 uint32_t Checksum;
108 uint8_t ParentUuid[16];
109 uint32_t ParentTimeStamp;
110 uint32_t Reserved0;
111 uint16_t ParentUnicodeName[256];
112 VHDPLE ParentLocatorEntry[VHD_MAX_LOCATOR_ENTRIES];
113 uint8_t Reserved1[256];
114} VHDDynamicDiskHeader;
115#pragma pack()
116
117#define VHD_DYNAMIC_DISK_HEADER_COOKIE "cxsparse"
118#define VHD_DYNAMIC_DISK_HEADER_COOKIE_SIZE 8
119#define VHD_DYNAMIC_DISK_HEADER_VERSION 0x00010000
120
121/**
122 * Complete VHD image data structure.
123 */
124typedef struct VHDIMAGE
125{
126 /** Image file name. */
127 const char *pszFilename;
128 /** Opaque storage handle. */
129 PVDIOSTORAGE pStorage;
130
131 /** Pointer to the per-disk VD interface list. */
132 PVDINTERFACE pVDIfsDisk;
133 /** Pointer to the per-image VD interface list. */
134 PVDINTERFACE pVDIfsImage;
135 /** Error interface. */
136 PVDINTERFACEERROR pIfError;
137 /** I/O interface. */
138 PVDINTERFACEIOINT pIfIo;
139
140 /** Open flags passed by VBoxHDD layer. */
141 unsigned uOpenFlags;
142 /** Image flags defined during creation or determined during open. */
143 unsigned uImageFlags;
144 /** Total size of the image. */
145 uint64_t cbSize;
146
147 /** Physical geometry of this image. */
148 VDGEOMETRY PCHSGeometry;
149 /** Logical geometry of this image. */
150 VDGEOMETRY LCHSGeometry;
151
152 /** Image UUID. */
153 RTUUID ImageUuid;
154 /** Parent image UUID. */
155 RTUUID ParentUuid;
156
157 /** Parent's time stamp at the time of image creation. */
158 uint32_t u32ParentTimeStamp;
159 /** Relative path to the parent image. */
160 char *pszParentFilename;
161
162 /** The Block Allocation Table. */
163 uint32_t *pBlockAllocationTable;
164 /** Number of entries in the table. */
165 uint32_t cBlockAllocationTableEntries;
166
167 /** Size of one data block. */
168 uint32_t cbDataBlock;
169 /** Sectors per data block. */
170 uint32_t cSectorsPerDataBlock;
171 /** Length of the sector bitmap in bytes. */
172 uint32_t cbDataBlockBitmap;
173 /** A copy of the disk footer. */
174 VHDFooter vhdFooterCopy;
175 /** Current end offset of the file (without the disk footer). */
176 uint64_t uCurrentEndOfFile;
177 /** Size of the data block bitmap in sectors. */
178 uint32_t cDataBlockBitmapSectors;
179 /** Start of the block allocation table. */
180 uint64_t uBlockAllocationTableOffset;
181 /** Buffer to hold block's bitmap for bit search operations. */
182 uint8_t *pu8Bitmap;
183 /** Offset to the next data structure (dynamic disk header). */
184 uint64_t u64DataOffset;
185 /** Flag to force dynamic disk header update. */
186 bool fDynHdrNeedsUpdate;
187} VHDIMAGE, *PVHDIMAGE;
188
189/**
190 * Structure tracking the expansion process of the image
191 * for async access.
192 */
193typedef struct VHDIMAGEEXPAND
194{
195 /** Flag indicating the status of each step. */
196 volatile uint32_t fFlags;
197 /** The index in the block allocation table which is written. */
198 uint32_t idxBatAllocated;
199 /** Big endian representation of the block index
200 * which is written in the BAT. */
201 uint32_t idxBlockBe;
202 /** Old end of the file - used for rollback in case of an error. */
203 uint64_t cbEofOld;
204 /** Sector bitmap written to the new block - variable in size. */
205 uint8_t au8Bitmap[1];
206} VHDIMAGEEXPAND, *PVHDIMAGEEXPAND;
207
208/**
209 * Flag defines
210 */
211#define VHDIMAGEEXPAND_STEP_IN_PROGRESS (0x0)
212#define VHDIMAGEEXPAND_STEP_FAILED (0x2)
213#define VHDIMAGEEXPAND_STEP_SUCCESS (0x3)
214/** All steps completed successfully. */
215#define VHDIMAGEEXPAND_ALL_SUCCESS (0xff)
216/** All steps completed (no success indicator) */
217#define VHDIMAGEEXPAND_ALL_COMPLETE (0xaa)
218
219/** Every status field has 2 bits so we can encode 4 steps in one byte. */
220#define VHDIMAGEEXPAND_STATUS_MASK 0x03
221#define VHDIMAGEEXPAND_BLOCKBITMAP_STATUS_SHIFT 0x00
222#define VHDIMAGEEXPAND_USERBLOCK_STATUS_SHIFT 0x02
223#define VHDIMAGEEXPAND_FOOTER_STATUS_SHIFT 0x04
224#define VHDIMAGEEXPAND_BAT_STATUS_SHIFT 0x06
225
226/**
227 * Helper macros to get and set the status field.
228 */
229#define VHDIMAGEEXPAND_STATUS_GET(fFlags, cShift) \
230 (((fFlags) >> (cShift)) & VHDIMAGEEXPAND_STATUS_MASK)
231#define VHDIMAGEEXPAND_STATUS_SET(fFlags, cShift, uVal) \
232 ASMAtomicOrU32(&(fFlags), ((uVal) & VHDIMAGEEXPAND_STATUS_MASK) << (cShift))
233
234/*******************************************************************************
235* Static Variables *
236*******************************************************************************/
237
238/** NULL-terminated array of supported file extensions. */
239static const VDFILEEXTENSION s_aVhdFileExtensions[] =
240{
241 {"vhd", VDTYPE_HDD},
242 {NULL, VDTYPE_INVALID}
243};
244
245/*******************************************************************************
246* Internal Functions *
247*******************************************************************************/
248
249
250/**
251 * Internal: Compute and update header checksum.
252 */
253static uint32_t vhdChecksum(void *pHeader, uint32_t cbSize)
254{
255 uint32_t checksum = 0;
256 for (uint32_t i = 0; i < cbSize; i++)
257 checksum += ((unsigned char *)pHeader)[i];
258 return ~checksum;
259}
260
261/**
262 * Internal: Convert filename to UTF16 with appropriate endianness.
263 */
264static int vhdFilenameToUtf16(const char *pszFilename, uint16_t *pu16Buf,
265 uint32_t cbBufSize, uint32_t *pcbActualSize,
266 bool fBigEndian)
267{
268 int rc;
269 PRTUTF16 tmp16 = NULL;
270 size_t cTmp16Len;
271
272 rc = RTStrToUtf16(pszFilename, &tmp16);
273 if (RT_FAILURE(rc))
274 goto out;
275 cTmp16Len = RTUtf16Len(tmp16);
276 if (cTmp16Len * sizeof(*tmp16) > cbBufSize)
277 {
278 rc = VERR_FILENAME_TOO_LONG;
279 goto out;
280 }
281
282 if (fBigEndian)
283 for (unsigned i = 0; i < cTmp16Len; i++)
284 pu16Buf[i] = RT_H2BE_U16(tmp16[i]);
285 else
286 memcpy(pu16Buf, tmp16, cTmp16Len * sizeof(*tmp16));
287 if (pcbActualSize)
288 *pcbActualSize = (uint32_t)(cTmp16Len * sizeof(*tmp16));
289
290out:
291 if (tmp16)
292 RTUtf16Free(tmp16);
293 return rc;
294}
295
296/**
297 * Internal: Update one locator entry.
298 */
299static int vhdLocatorUpdate(PVHDIMAGE pImage, PVHDPLE pLocator, const char *pszFilename)
300{
301 int rc = VINF_SUCCESS;
302 uint32_t cb, cbMaxLen = RT_BE2H_U32(pLocator->u32DataSpace);
303 void *pvBuf = RTMemTmpAllocZ(cbMaxLen);
304 char *pszTmp;
305
306 if (!pvBuf)
307 return VERR_NO_MEMORY;
308
309 switch (RT_BE2H_U32(pLocator->u32Code))
310 {
311 case VHD_PLATFORM_CODE_WI2R:
312 {
313 if (RTPathStartsWithRoot(pszFilename))
314 {
315 /* Convert to relative path. */
316 char szPath[RTPATH_MAX];
317 rc = RTPathCalcRelative(szPath, sizeof(szPath), pImage->pszFilename,
318 pszFilename);
319 if (RT_SUCCESS(rc))
320 {
321 /* Update plain relative name. */
322 cb = (uint32_t)strlen(szPath);
323 if (cb > cbMaxLen)
324 {
325 rc = VERR_FILENAME_TOO_LONG;
326 break;
327 }
328 memcpy(pvBuf, szPath, cb);
329 }
330 }
331 else
332 {
333 /* Update plain relative name. */
334 cb = (uint32_t)strlen(pszFilename);
335 if (cb > cbMaxLen)
336 {
337 rc = VERR_FILENAME_TOO_LONG;
338 break;
339 }
340 memcpy(pvBuf, pszFilename, cb);
341 }
342 pLocator->u32DataLength = RT_H2BE_U32(cb);
343 break;
344 }
345 case VHD_PLATFORM_CODE_WI2K:
346 /* Update plain absolute name. */
347 rc = RTPathAbs(pszFilename, (char *)pvBuf, cbMaxLen);
348 if (RT_SUCCESS(rc))
349 pLocator->u32DataLength = RT_H2BE_U32((uint32_t)strlen((const char *)pvBuf));
350 break;
351 case VHD_PLATFORM_CODE_W2RU:
352 if (RTPathStartsWithRoot(pszFilename))
353 {
354 /* Convert to relative path. */
355 char szPath[RTPATH_MAX];
356 rc = RTPathCalcRelative(szPath, sizeof(szPath), pImage->pszFilename,
357 pszFilename);
358 if (RT_SUCCESS(rc))
359 rc = vhdFilenameToUtf16(szPath, (uint16_t *)pvBuf, cbMaxLen, &cb, false);
360 }
361 else
362 {
363 /* Update unicode relative name. */
364 rc = vhdFilenameToUtf16(pszFilename, (uint16_t *)pvBuf, cbMaxLen, &cb, false);
365 }
366
367 if (RT_SUCCESS(rc))
368 pLocator->u32DataLength = RT_H2BE_U32(cb);
369 break;
370 case VHD_PLATFORM_CODE_W2KU:
371 /* Update unicode absolute name. */
372 pszTmp = (char*)RTMemTmpAllocZ(cbMaxLen);
373 if (!pszTmp)
374 {
375 rc = VERR_NO_MEMORY;
376 break;
377 }
378 rc = RTPathAbs(pszFilename, pszTmp, cbMaxLen);
379 if (RT_FAILURE(rc))
380 {
381 RTMemTmpFree(pszTmp);
382 break;
383 }
384 rc = vhdFilenameToUtf16(pszTmp, (uint16_t *)pvBuf, cbMaxLen, &cb, false);
385 RTMemTmpFree(pszTmp);
386 if (RT_SUCCESS(rc))
387 pLocator->u32DataLength = RT_H2BE_U32(cb);
388 break;
389 default:
390 rc = VERR_NOT_IMPLEMENTED;
391 break;
392 }
393
394 if (RT_SUCCESS(rc))
395 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
396 RT_BE2H_U64(pLocator->u64DataOffset),
397 pvBuf, cb);
398
399 if (pvBuf)
400 RTMemTmpFree(pvBuf);
401 return rc;
402}
403
404/**
405 * Internal: Update dynamic disk header from VHDIMAGE.
406 */
407static int vhdDynamicHeaderUpdate(PVHDIMAGE pImage)
408{
409 VHDDynamicDiskHeader ddh;
410 int rc, i;
411
412 if (!pImage)
413 return VERR_VD_NOT_OPENED;
414
415 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
416 pImage->u64DataOffset, &ddh, sizeof(ddh));
417 if (RT_FAILURE(rc))
418 return rc;
419 if (memcmp(ddh.Cookie, VHD_DYNAMIC_DISK_HEADER_COOKIE, VHD_DYNAMIC_DISK_HEADER_COOKIE_SIZE) != 0)
420 return VERR_VD_VHD_INVALID_HEADER;
421
422 uint32_t u32Checksum = RT_BE2H_U32(ddh.Checksum);
423 ddh.Checksum = 0;
424 if (u32Checksum != vhdChecksum(&ddh, sizeof(ddh)))
425 return VERR_VD_VHD_INVALID_HEADER;
426
427 /* Update parent's timestamp. */
428 ddh.ParentTimeStamp = RT_H2BE_U32(pImage->u32ParentTimeStamp);
429 /* Update parent's filename. */
430 if (pImage->pszParentFilename)
431 {
432 rc = vhdFilenameToUtf16(RTPathFilename(pImage->pszParentFilename),
433 ddh.ParentUnicodeName, sizeof(ddh.ParentUnicodeName) - 1, NULL, true);
434 if (RT_FAILURE(rc))
435 return rc;
436 }
437
438 /* Update parent's locators. */
439 for (i = 0; i < VHD_MAX_LOCATOR_ENTRIES; i++)
440 {
441 /* Skip empty locators */
442 if (ddh.ParentLocatorEntry[i].u32Code != RT_H2BE_U32(VHD_PLATFORM_CODE_NONE))
443 {
444 if (pImage->pszParentFilename)
445 {
446 rc = vhdLocatorUpdate(pImage, &ddh.ParentLocatorEntry[i], pImage->pszParentFilename);
447 if (RT_FAILURE(rc))
448 return rc;
449 }
450 else
451 {
452 /* The parent was deleted. */
453 ddh.ParentLocatorEntry[i].u32Code = RT_H2BE_U32(VHD_PLATFORM_CODE_NONE);
454 }
455 }
456 }
457 /* Update parent's UUID */
458 memcpy(ddh.ParentUuid, pImage->ParentUuid.au8, sizeof(ddh.ParentUuid));
459
460 /* Update data offset and number of table entries. */
461 ddh.MaxTableEntries = RT_H2BE_U32(pImage->cBlockAllocationTableEntries);
462
463 ddh.Checksum = 0;
464 ddh.Checksum = RT_H2BE_U32(vhdChecksum(&ddh, sizeof(ddh)));
465 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
466 pImage->u64DataOffset, &ddh, sizeof(ddh));
467 return rc;
468}
469
470/**
471 * Internal: Update the VHD footer.
472 */
473static int vhdUpdateFooter(PVHDIMAGE pImage)
474{
475 int rc = VINF_SUCCESS;
476
477 /* Update fields which can change. */
478 pImage->vhdFooterCopy.CurSize = RT_H2BE_U64(pImage->cbSize);
479 pImage->vhdFooterCopy.DiskGeometryCylinder = RT_H2BE_U16(pImage->PCHSGeometry.cCylinders);
480 pImage->vhdFooterCopy.DiskGeometryHeads = pImage->PCHSGeometry.cHeads;
481 pImage->vhdFooterCopy.DiskGeometrySectors = pImage->PCHSGeometry.cSectors;
482
483 pImage->vhdFooterCopy.Checksum = 0;
484 pImage->vhdFooterCopy.Checksum = RT_H2BE_U32(vhdChecksum(&pImage->vhdFooterCopy, sizeof(VHDFooter)));
485
486 if (pImage->pBlockAllocationTable)
487 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0,
488 &pImage->vhdFooterCopy, sizeof(VHDFooter));
489
490 if (RT_SUCCESS(rc))
491 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
492 pImage->uCurrentEndOfFile, &pImage->vhdFooterCopy,
493 sizeof(VHDFooter));
494
495 return rc;
496}
497
498/**
499 * Internal. Flush image data to disk.
500 */
501static int vhdFlushImage(PVHDIMAGE pImage)
502{
503 int rc = VINF_SUCCESS;
504
505 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
506 return VINF_SUCCESS;
507
508 if (pImage->pBlockAllocationTable)
509 {
510 /*
511 * This is an expanding image. Write the BAT and copy of the disk footer.
512 */
513 size_t cbBlockAllocationTableToWrite = pImage->cBlockAllocationTableEntries * sizeof(uint32_t);
514 uint32_t *pBlockAllocationTableToWrite = (uint32_t *)RTMemAllocZ(cbBlockAllocationTableToWrite);
515
516 if (!pBlockAllocationTableToWrite)
517 return VERR_NO_MEMORY;
518
519 /*
520 * The BAT entries have to be stored in big endian format.
521 */
522 for (unsigned i = 0; i < pImage->cBlockAllocationTableEntries; i++)
523 pBlockAllocationTableToWrite[i] = RT_H2BE_U32(pImage->pBlockAllocationTable[i]);
524
525 /*
526 * Write the block allocation table after the copy of the disk footer and the dynamic disk header.
527 */
528 vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->uBlockAllocationTableOffset,
529 pBlockAllocationTableToWrite, cbBlockAllocationTableToWrite);
530 if (pImage->fDynHdrNeedsUpdate)
531 rc = vhdDynamicHeaderUpdate(pImage);
532 RTMemFree(pBlockAllocationTableToWrite);
533 }
534
535 if (RT_SUCCESS(rc))
536 rc = vhdUpdateFooter(pImage);
537
538 if (RT_SUCCESS(rc))
539 rc = vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
540
541 return rc;
542}
543
544/**
545 * Internal. Free all allocated space for representing an image except pImage,
546 * and optionally delete the image from disk.
547 */
548static int vhdFreeImage(PVHDIMAGE pImage, bool fDelete)
549{
550 int rc = VINF_SUCCESS;
551
552 /* Freeing a never allocated image (e.g. because the open failed) is
553 * not signalled as an error. After all nothing bad happens. */
554 if (pImage)
555 {
556 if (pImage->pStorage)
557 {
558 /* No point updating the file that is deleted anyway. */
559 if (!fDelete)
560 vhdFlushImage(pImage);
561
562 rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
563 pImage->pStorage = NULL;
564 }
565
566 if (pImage->pszParentFilename)
567 {
568 RTStrFree(pImage->pszParentFilename);
569 pImage->pszParentFilename = NULL;
570 }
571 if (pImage->pBlockAllocationTable)
572 {
573 RTMemFree(pImage->pBlockAllocationTable);
574 pImage->pBlockAllocationTable = NULL;
575 }
576 if (pImage->pu8Bitmap)
577 {
578 RTMemFree(pImage->pu8Bitmap);
579 pImage->pu8Bitmap = NULL;
580 }
581
582 if (fDelete && pImage->pszFilename)
583 vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
584 }
585
586 LogFlowFunc(("returns %Rrc\n", rc));
587 return rc;
588}
589
590/* 946684800 is the number of seconds between 1/1/1970 and 1/1/2000 */
591#define VHD_TO_UNIX_EPOCH_SECONDS UINT64_C(946684800)
592
593static uint32_t vhdRtTime2VhdTime(PCRTTIMESPEC pRtTimeStamp)
594{
595 uint64_t u64Seconds = RTTimeSpecGetSeconds(pRtTimeStamp);
596 return (uint32_t)(u64Seconds - VHD_TO_UNIX_EPOCH_SECONDS);
597}
598
599static void vhdTime2RtTime(PRTTIMESPEC pRtTimeStamp, uint32_t u32VhdTimeStamp)
600{
601 RTTimeSpecSetSeconds(pRtTimeStamp, VHD_TO_UNIX_EPOCH_SECONDS + u32VhdTimeStamp);
602}
603
604/**
605 * Internal: Allocates the block bitmap rounding up to the next 32bit or 64bit boundary.
606 * Can be freed with RTMemFree. The memory is zeroed.
607 */
608DECLINLINE(uint8_t *)vhdBlockBitmapAllocate(PVHDIMAGE pImage)
609{
610#ifdef RT_ARCH_AMD64
611 return (uint8_t *)RTMemAllocZ(pImage->cbDataBlockBitmap + 8);
612#else
613 return (uint8_t *)RTMemAllocZ(pImage->cbDataBlockBitmap + 4);
614#endif
615}
616
617/**
618 * Internal: called when the async expansion process completed (failure or success).
619 * Will do the necessary rollback if an error occurred.
620 */
621static int vhdAsyncExpansionComplete(PVHDIMAGE pImage, PVDIOCTX pIoCtx, PVHDIMAGEEXPAND pExpand)
622{
623 int rc = VINF_SUCCESS;
624 uint32_t fFlags = ASMAtomicReadU32(&pExpand->fFlags);
625 bool fIoInProgress = false;
626
627 /* Quick path, check if everything succeeded. */
628 if (fFlags == VHDIMAGEEXPAND_ALL_SUCCESS)
629 {
630 RTMemFree(pExpand);
631 }
632 else
633 {
634 uint32_t uStatus;
635
636 uStatus = VHDIMAGEEXPAND_STATUS_GET(pExpand->fFlags, VHDIMAGEEXPAND_BAT_STATUS_SHIFT);
637 if ( uStatus == VHDIMAGEEXPAND_STEP_FAILED
638 || uStatus == VHDIMAGEEXPAND_STEP_SUCCESS)
639 {
640 /* Undo and restore the old value. */
641 pImage->pBlockAllocationTable[pExpand->idxBatAllocated] = ~0U;
642
643 /* Restore the old value on the disk.
644 * No need for a completion callback because we can't
645 * do anything if this fails. */
646 if (uStatus == VHDIMAGEEXPAND_STEP_SUCCESS)
647 {
648 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
649 pImage->uBlockAllocationTableOffset
650 + pExpand->idxBatAllocated * sizeof(uint32_t),
651 &pImage->pBlockAllocationTable[pExpand->idxBatAllocated],
652 sizeof(uint32_t), pIoCtx, NULL, NULL);
653 fIoInProgress |= rc == VERR_VD_ASYNC_IO_IN_PROGRESS;
654 }
655 }
656
657 /* Restore old size (including the footer because another application might
658 * fill up the free space making it impossible to add the footer)
659 * and add the footer at the right place again. */
660 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
661 pExpand->cbEofOld + sizeof(VHDFooter));
662 AssertRC(rc);
663
664 pImage->uCurrentEndOfFile = pExpand->cbEofOld;
665 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
666 pImage->uCurrentEndOfFile,
667 &pImage->vhdFooterCopy, sizeof(VHDFooter),
668 pIoCtx, NULL, NULL);
669 fIoInProgress |= rc == VERR_VD_ASYNC_IO_IN_PROGRESS;
670 }
671
672 return fIoInProgress ? VERR_VD_ASYNC_IO_IN_PROGRESS : rc;
673}
674
675static int vhdAsyncExpansionStepCompleted(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq, unsigned iStep)
676{
677 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
678 PVHDIMAGEEXPAND pExpand = (PVHDIMAGEEXPAND)pvUser;
679
680 LogFlowFunc(("pBackendData=%#p pIoCtx=%#p pvUser=%#p rcReq=%Rrc iStep=%u\n",
681 pBackendData, pIoCtx, pvUser, rcReq, iStep));
682
683 if (RT_SUCCESS(rcReq))
684 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, iStep, VHDIMAGEEXPAND_STEP_SUCCESS);
685 else
686 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, iStep, VHDIMAGEEXPAND_STEP_FAILED);
687
688 if ((pExpand->fFlags & VHDIMAGEEXPAND_ALL_COMPLETE) == VHDIMAGEEXPAND_ALL_COMPLETE)
689 return vhdAsyncExpansionComplete(pImage, pIoCtx, pExpand);
690
691 return VERR_VD_ASYNC_IO_IN_PROGRESS;
692}
693
694static int vhdAsyncExpansionDataBlockBitmapComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
695{
696 return vhdAsyncExpansionStepCompleted(pBackendData, pIoCtx, pvUser, rcReq, VHDIMAGEEXPAND_BLOCKBITMAP_STATUS_SHIFT);
697}
698
699static int vhdAsyncExpansionDataComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
700{
701 return vhdAsyncExpansionStepCompleted(pBackendData, pIoCtx, pvUser, rcReq, VHDIMAGEEXPAND_USERBLOCK_STATUS_SHIFT);
702}
703
704static int vhdAsyncExpansionBatUpdateComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
705{
706 return vhdAsyncExpansionStepCompleted(pBackendData, pIoCtx, pvUser, rcReq, VHDIMAGEEXPAND_BAT_STATUS_SHIFT);
707}
708
709static int vhdAsyncExpansionFooterUpdateComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
710{
711 return vhdAsyncExpansionStepCompleted(pBackendData, pIoCtx, pvUser, rcReq, VHDIMAGEEXPAND_FOOTER_STATUS_SHIFT);
712}
713
714static int vhdLoadDynamicDisk(PVHDIMAGE pImage, uint64_t uDynamicDiskHeaderOffset)
715{
716 VHDDynamicDiskHeader vhdDynamicDiskHeader;
717 int rc = VINF_SUCCESS;
718 uint32_t *pBlockAllocationTable;
719 uint64_t uBlockAllocationTableOffset;
720 unsigned i = 0;
721
722 Log(("Open a dynamic disk.\n"));
723
724 /*
725 * Read the dynamic disk header.
726 */
727 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, uDynamicDiskHeaderOffset,
728 &vhdDynamicDiskHeader, sizeof(VHDDynamicDiskHeader));
729 if (memcmp(vhdDynamicDiskHeader.Cookie, VHD_DYNAMIC_DISK_HEADER_COOKIE, VHD_DYNAMIC_DISK_HEADER_COOKIE_SIZE))
730 return VERR_INVALID_PARAMETER;
731
732 pImage->cbDataBlock = RT_BE2H_U32(vhdDynamicDiskHeader.BlockSize);
733 LogFlowFunc(("BlockSize=%u\n", pImage->cbDataBlock));
734 pImage->cBlockAllocationTableEntries = RT_BE2H_U32(vhdDynamicDiskHeader.MaxTableEntries);
735 LogFlowFunc(("MaxTableEntries=%lu\n", pImage->cBlockAllocationTableEntries));
736 AssertMsg(!(pImage->cbDataBlock % VHD_SECTOR_SIZE), ("%s: Data block size is not a multiple of %!\n", __FUNCTION__, VHD_SECTOR_SIZE));
737
738 pImage->cSectorsPerDataBlock = pImage->cbDataBlock / VHD_SECTOR_SIZE;
739 LogFlowFunc(("SectorsPerDataBlock=%u\n", pImage->cSectorsPerDataBlock));
740
741 /*
742 * Every block starts with a bitmap indicating which sectors are valid and which are not.
743 * We store the size of it to be able to calculate the real offset.
744 */
745 pImage->cbDataBlockBitmap = pImage->cSectorsPerDataBlock / 8;
746 pImage->cDataBlockBitmapSectors = pImage->cbDataBlockBitmap / VHD_SECTOR_SIZE;
747 /* Round up to full sector size */
748 if (pImage->cbDataBlockBitmap % VHD_SECTOR_SIZE > 0)
749 pImage->cDataBlockBitmapSectors++;
750 LogFlowFunc(("cbDataBlockBitmap=%u\n", pImage->cbDataBlockBitmap));
751 LogFlowFunc(("cDataBlockBitmapSectors=%u\n", pImage->cDataBlockBitmapSectors));
752
753 pImage->pu8Bitmap = vhdBlockBitmapAllocate(pImage);
754 if (!pImage->pu8Bitmap)
755 return VERR_NO_MEMORY;
756
757 pBlockAllocationTable = (uint32_t *)RTMemAllocZ(pImage->cBlockAllocationTableEntries * sizeof(uint32_t));
758 if (!pBlockAllocationTable)
759 return VERR_NO_MEMORY;
760
761 /*
762 * Read the table.
763 */
764 uBlockAllocationTableOffset = RT_BE2H_U64(vhdDynamicDiskHeader.TableOffset);
765 LogFlowFunc(("uBlockAllocationTableOffset=%llu\n", uBlockAllocationTableOffset));
766 pImage->uBlockAllocationTableOffset = uBlockAllocationTableOffset;
767 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
768 uBlockAllocationTableOffset, pBlockAllocationTable,
769 pImage->cBlockAllocationTableEntries * sizeof(uint32_t));
770
771 /*
772 * Because the offset entries inside the allocation table are stored big endian
773 * we need to convert them into host endian.
774 */
775 pImage->pBlockAllocationTable = (uint32_t *)RTMemAllocZ(pImage->cBlockAllocationTableEntries * sizeof(uint32_t));
776 if (!pImage->pBlockAllocationTable)
777 {
778 RTMemFree(pBlockAllocationTable);
779 return VERR_NO_MEMORY;
780 }
781
782 for (i = 0; i < pImage->cBlockAllocationTableEntries; i++)
783 pImage->pBlockAllocationTable[i] = RT_BE2H_U32(pBlockAllocationTable[i]);
784
785 RTMemFree(pBlockAllocationTable);
786
787 if (pImage->uImageFlags & VD_IMAGE_FLAGS_DIFF)
788 memcpy(pImage->ParentUuid.au8, vhdDynamicDiskHeader.ParentUuid, sizeof(pImage->ParentUuid));
789
790 return rc;
791}
792
793static int vhdOpenImage(PVHDIMAGE pImage, unsigned uOpenFlags)
794{
795 uint64_t FileSize;
796 VHDFooter vhdFooter;
797
798 pImage->uOpenFlags = uOpenFlags;
799
800 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
801 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
802 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
803
804 /*
805 * Open the image.
806 */
807 int rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
808 VDOpenFlagsToFileOpenFlags(uOpenFlags,
809 false /* fCreate */),
810 &pImage->pStorage);
811 if (RT_FAILURE(rc))
812 {
813 /* Do NOT signal an appropriate error here, as the VD layer has the
814 * choice of retrying the open if it failed. */
815 return rc;
816 }
817
818 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &FileSize);
819 pImage->uCurrentEndOfFile = FileSize - sizeof(VHDFooter);
820
821 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, pImage->uCurrentEndOfFile,
822 &vhdFooter, sizeof(VHDFooter));
823 if (RT_SUCCESS(rc))
824 {
825 if (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0)
826 {
827 /*
828 * There is also a backup header at the beginning in case the image got corrupted.
829 * Such corrupted images are detected here to let the open handler repair it later.
830 */
831 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0,
832 &vhdFooter, sizeof(VHDFooter));
833 if (RT_SUCCESS(rc))
834 {
835 if (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0)
836 rc = VERR_VD_VHD_INVALID_HEADER;
837 else
838 rc = VERR_VD_IMAGE_CORRUPTED;
839 }
840 }
841 }
842
843 if (RT_FAILURE(rc))
844 return rc;
845
846 switch (RT_BE2H_U32(vhdFooter.DiskType))
847 {
848 case VHD_FOOTER_DISK_TYPE_FIXED:
849 pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED;
850 break;
851 case VHD_FOOTER_DISK_TYPE_DYNAMIC:
852 pImage->uImageFlags &= ~VD_IMAGE_FLAGS_FIXED;
853 break;
854 case VHD_FOOTER_DISK_TYPE_DIFFERENCING:
855 pImage->uImageFlags |= VD_IMAGE_FLAGS_DIFF;
856 pImage->uImageFlags &= ~VD_IMAGE_FLAGS_FIXED;
857 break;
858 default:
859 return VERR_NOT_IMPLEMENTED;
860 }
861
862 pImage->cbSize = RT_BE2H_U64(vhdFooter.CurSize);
863 pImage->LCHSGeometry.cCylinders = 0;
864 pImage->LCHSGeometry.cHeads = 0;
865 pImage->LCHSGeometry.cSectors = 0;
866 pImage->PCHSGeometry.cCylinders = RT_BE2H_U16(vhdFooter.DiskGeometryCylinder);
867 pImage->PCHSGeometry.cHeads = vhdFooter.DiskGeometryHeads;
868 pImage->PCHSGeometry.cSectors = vhdFooter.DiskGeometrySectors;
869
870 /*
871 * Copy of the disk footer.
872 * If we allocate new blocks in differencing disks on write access
873 * the footer is overwritten. We need to write it at the end of the file.
874 */
875 memcpy(&pImage->vhdFooterCopy, &vhdFooter, sizeof(VHDFooter));
876
877 /*
878 * Is there a better way?
879 */
880 memcpy(&pImage->ImageUuid, &vhdFooter.UniqueID, 16);
881
882 pImage->u64DataOffset = RT_BE2H_U64(vhdFooter.DataOffset);
883 LogFlowFunc(("DataOffset=%llu\n", pImage->u64DataOffset));
884
885 if (!(pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED))
886 rc = vhdLoadDynamicDisk(pImage, pImage->u64DataOffset);
887
888 if (RT_FAILURE(rc))
889 vhdFreeImage(pImage, false);
890 return rc;
891}
892
893/**
894 * Internal: Checks if a sector in the block bitmap is set
895 */
896DECLINLINE(bool) vhdBlockBitmapSectorContainsData(PVHDIMAGE pImage, uint32_t cBlockBitmapEntry)
897{
898 uint32_t iBitmap = (cBlockBitmapEntry / 8); /* Byte in the block bitmap. */
899
900 /*
901 * The index of the bit in the byte of the data block bitmap.
902 * The most significant bit stands for a lower sector number.
903 */
904 uint8_t iBitInByte = (8-1) - (cBlockBitmapEntry % 8);
905 uint8_t *puBitmap = pImage->pu8Bitmap + iBitmap;
906
907 AssertMsg(puBitmap < (pImage->pu8Bitmap + pImage->cbDataBlockBitmap),
908 ("VHD: Current bitmap position exceeds maximum size of the bitmap\n"));
909
910 return ASMBitTest(puBitmap, iBitInByte);
911}
912
913/**
914 * Internal: Sets the given sector in the sector bitmap.
915 */
916DECLINLINE(bool) vhdBlockBitmapSectorSet(PVHDIMAGE pImage, uint8_t *pu8Bitmap, uint32_t cBlockBitmapEntry)
917{
918 uint32_t iBitmap = (cBlockBitmapEntry / 8); /* Byte in the block bitmap. */
919
920 /*
921 * The index of the bit in the byte of the data block bitmap.
922 * The most significant bit stands for a lower sector number.
923 */
924 uint8_t iBitInByte = (8-1) - (cBlockBitmapEntry % 8);
925 uint8_t *puBitmap = pu8Bitmap + iBitmap;
926
927 AssertMsg(puBitmap < (pu8Bitmap + pImage->cbDataBlockBitmap),
928 ("VHD: Current bitmap position exceeds maximum size of the bitmap\n"));
929
930 return !ASMBitTestAndSet(puBitmap, iBitInByte);
931}
932
933/**
934 * Internal: Derive drive geometry from its size.
935 */
936static void vhdSetDiskGeometry(PVHDIMAGE pImage, uint64_t cbSize)
937{
938 uint64_t u64TotalSectors = cbSize / VHD_SECTOR_SIZE;
939 uint32_t u32CylinderTimesHeads, u32Heads, u32SectorsPerTrack;
940
941 if (u64TotalSectors > 65535 * 16 * 255)
942 {
943 /* ATA disks limited to 127 GB. */
944 u64TotalSectors = 65535 * 16 * 255;
945 }
946
947 if (u64TotalSectors >= 65535 * 16 * 63)
948 {
949 u32SectorsPerTrack = 255;
950 u32Heads = 16;
951 u32CylinderTimesHeads = u64TotalSectors / u32SectorsPerTrack;
952 }
953 else
954 {
955 u32SectorsPerTrack = 17;
956 u32CylinderTimesHeads = u64TotalSectors / u32SectorsPerTrack;
957
958 u32Heads = (u32CylinderTimesHeads + 1023) / 1024;
959
960 if (u32Heads < 4)
961 {
962 u32Heads = 4;
963 }
964 if (u32CylinderTimesHeads >= (u32Heads * 1024) || u32Heads > 16)
965 {
966 u32SectorsPerTrack = 31;
967 u32Heads = 16;
968 u32CylinderTimesHeads = u64TotalSectors / u32SectorsPerTrack;
969 }
970 if (u32CylinderTimesHeads >= (u32Heads * 1024))
971 {
972 u32SectorsPerTrack = 63;
973 u32Heads = 16;
974 u32CylinderTimesHeads = u64TotalSectors / u32SectorsPerTrack;
975 }
976 }
977 pImage->PCHSGeometry.cCylinders = u32CylinderTimesHeads / u32Heads;
978 pImage->PCHSGeometry.cHeads = u32Heads;
979 pImage->PCHSGeometry.cSectors = u32SectorsPerTrack;
980 pImage->LCHSGeometry.cCylinders = 0;
981 pImage->LCHSGeometry.cHeads = 0;
982 pImage->LCHSGeometry.cSectors = 0;
983}
984
985
986static uint32_t vhdAllocateParentLocators(PVHDIMAGE pImage, VHDDynamicDiskHeader *pDDH, uint64_t u64Offset)
987{
988 PVHDPLE pLocator = pDDH->ParentLocatorEntry;
989
990 /*
991 * The VHD spec states that the DataSpace field holds the number of sectors
992 * required to store the parent locator path.
993 * As it turned out VPC and Hyper-V store the amount of bytes reserved for the
994 * path and not the number of sectors.
995 */
996
997 /* Unicode absolute Windows path. */
998 pLocator->u32Code = RT_H2BE_U32(VHD_PLATFORM_CODE_W2KU);
999 pLocator->u32DataSpace = RT_H2BE_U32(VHD_ABSOLUTE_MAX_PATH * sizeof(RTUTF16));
1000 pLocator->u64DataOffset = RT_H2BE_U64(u64Offset);
1001 pLocator++;
1002 u64Offset += VHD_ABSOLUTE_MAX_PATH * sizeof(RTUTF16);
1003 /* Unicode relative Windows path. */
1004 pLocator->u32Code = RT_H2BE_U32(VHD_PLATFORM_CODE_W2RU);
1005 pLocator->u32DataSpace = RT_H2BE_U32(VHD_RELATIVE_MAX_PATH * sizeof(RTUTF16));
1006 pLocator->u64DataOffset = RT_H2BE_U64(u64Offset);
1007 u64Offset += VHD_RELATIVE_MAX_PATH * sizeof(RTUTF16);
1008 return u64Offset;
1009}
1010
1011/**
1012 * Internal: Additional code for dynamic VHD image creation.
1013 */
1014static int vhdCreateDynamicImage(PVHDIMAGE pImage, uint64_t cbSize)
1015{
1016 int rc;
1017 VHDDynamicDiskHeader DynamicDiskHeader;
1018 uint32_t u32BlockAllocationTableSectors;
1019 void *pvTmp = NULL;
1020
1021 memset(&DynamicDiskHeader, 0, sizeof(DynamicDiskHeader));
1022
1023 pImage->u64DataOffset = sizeof(VHDFooter);
1024 pImage->cbDataBlock = VHD_BLOCK_SIZE; /* 2 MB */
1025 pImage->cSectorsPerDataBlock = pImage->cbDataBlock / VHD_SECTOR_SIZE;
1026 pImage->cbDataBlockBitmap = pImage->cSectorsPerDataBlock / 8;
1027 pImage->cDataBlockBitmapSectors = pImage->cbDataBlockBitmap / VHD_SECTOR_SIZE;
1028 /* Align to sector boundary */
1029 if (pImage->cbDataBlockBitmap % VHD_SECTOR_SIZE > 0)
1030 pImage->cDataBlockBitmapSectors++;
1031 pImage->pu8Bitmap = vhdBlockBitmapAllocate(pImage);
1032 if (!pImage->pu8Bitmap)
1033 return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("VHD: cannot allocate memory for bitmap storage"));
1034
1035 /* Initialize BAT. */
1036 pImage->uBlockAllocationTableOffset = (uint64_t)sizeof(VHDFooter) + sizeof(VHDDynamicDiskHeader);
1037 pImage->cBlockAllocationTableEntries = (uint32_t)((cbSize + pImage->cbDataBlock - 1) / pImage->cbDataBlock); /* Align table to the block size. */
1038 u32BlockAllocationTableSectors = (pImage->cBlockAllocationTableEntries * sizeof(uint32_t) + VHD_SECTOR_SIZE - 1) / VHD_SECTOR_SIZE;
1039 pImage->pBlockAllocationTable = (uint32_t *)RTMemAllocZ(pImage->cBlockAllocationTableEntries * sizeof(uint32_t));
1040 if (!pImage->pBlockAllocationTable)
1041 return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("VHD: cannot allocate memory for BAT"));
1042
1043 for (unsigned i = 0; i < pImage->cBlockAllocationTableEntries; i++)
1044 {
1045 pImage->pBlockAllocationTable[i] = 0xFFFFFFFF; /* It is actually big endian. */
1046 }
1047
1048 /* Round up to the sector size. */
1049 if (pImage->uImageFlags & VD_IMAGE_FLAGS_DIFF) /* fix hyper-v unreadable error */
1050 pImage->uCurrentEndOfFile = vhdAllocateParentLocators(pImage, &DynamicDiskHeader,
1051 pImage->uBlockAllocationTableOffset + u32BlockAllocationTableSectors * VHD_SECTOR_SIZE);
1052 else
1053 pImage->uCurrentEndOfFile = pImage->uBlockAllocationTableOffset + u32BlockAllocationTableSectors * VHD_SECTOR_SIZE;
1054
1055 /* Set dynamic image size. */
1056 pvTmp = RTMemTmpAllocZ(pImage->uCurrentEndOfFile + sizeof(VHDFooter));
1057 if (!pvTmp)
1058 return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("VHD: cannot set the file size for '%s'"), pImage->pszFilename);
1059
1060 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0, pvTmp,
1061 pImage->uCurrentEndOfFile + sizeof(VHDFooter));
1062 if (RT_FAILURE(rc))
1063 {
1064 RTMemTmpFree(pvTmp);
1065 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot set the file size for '%s'"), pImage->pszFilename);
1066 }
1067
1068 RTMemTmpFree(pvTmp);
1069
1070 /* Initialize and write the dynamic disk header. */
1071 memcpy(DynamicDiskHeader.Cookie, VHD_DYNAMIC_DISK_HEADER_COOKIE, sizeof(DynamicDiskHeader.Cookie));
1072 DynamicDiskHeader.DataOffset = UINT64_C(0xFFFFFFFFFFFFFFFF); /* Initially the disk has no data. */
1073 DynamicDiskHeader.TableOffset = RT_H2BE_U64(pImage->uBlockAllocationTableOffset);
1074 DynamicDiskHeader.HeaderVersion = RT_H2BE_U32(VHD_DYNAMIC_DISK_HEADER_VERSION);
1075 DynamicDiskHeader.BlockSize = RT_H2BE_U32(pImage->cbDataBlock);
1076 DynamicDiskHeader.MaxTableEntries = RT_H2BE_U32(pImage->cBlockAllocationTableEntries);
1077 /* Compute and update checksum. */
1078 DynamicDiskHeader.Checksum = 0;
1079 DynamicDiskHeader.Checksum = RT_H2BE_U32(vhdChecksum(&DynamicDiskHeader, sizeof(DynamicDiskHeader)));
1080
1081 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VHDFooter),
1082 &DynamicDiskHeader, sizeof(DynamicDiskHeader));
1083 if (RT_FAILURE(rc))
1084 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot write dynamic disk header to image '%s'"), pImage->pszFilename);
1085
1086 /* Write BAT. */
1087 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->uBlockAllocationTableOffset,
1088 pImage->pBlockAllocationTable,
1089 pImage->cBlockAllocationTableEntries * sizeof(uint32_t));
1090 if (RT_FAILURE(rc))
1091 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot write BAT to image '%s'"), pImage->pszFilename);
1092
1093 return rc;
1094}
1095
1096/**
1097 * Internal: The actual code for VHD image creation, both fixed and dynamic.
1098 */
1099static int vhdCreateImage(PVHDIMAGE pImage, uint64_t cbSize,
1100 unsigned uImageFlags, const char *pszComment,
1101 PCVDGEOMETRY pPCHSGeometry,
1102 PCVDGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
1103 unsigned uOpenFlags,
1104 PFNVDPROGRESS pfnProgress, void *pvUser,
1105 unsigned uPercentStart, unsigned uPercentSpan)
1106{
1107 int rc;
1108 VHDFooter Footer;
1109 RTTIMESPEC now;
1110
1111 pImage->uOpenFlags = uOpenFlags;
1112 pImage->uImageFlags = uImageFlags;
1113
1114 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1115
1116 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
1117 VDOpenFlagsToFileOpenFlags(uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
1118 true /* fCreate */),
1119 &pImage->pStorage);
1120 if (RT_FAILURE(rc))
1121 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot create image '%s'"), pImage->pszFilename);
1122
1123
1124 pImage->cbSize = cbSize;
1125 pImage->ImageUuid = *pUuid;
1126 RTUuidClear(&pImage->ParentUuid);
1127 vhdSetDiskGeometry(pImage, cbSize);
1128
1129 /* Initialize the footer. */
1130 memset(&Footer, 0, sizeof(Footer));
1131 memcpy(Footer.Cookie, VHD_FOOTER_COOKIE, sizeof(Footer.Cookie));
1132 Footer.Features = RT_H2BE_U32(0x2);
1133 Footer.Version = RT_H2BE_U32(VHD_FOOTER_FILE_FORMAT_VERSION);
1134 Footer.TimeStamp = RT_H2BE_U32(vhdRtTime2VhdTime(RTTimeNow(&now)));
1135 memcpy(Footer.CreatorApp, "vbox", sizeof(Footer.CreatorApp));
1136 Footer.CreatorVer = RT_H2BE_U32(VBOX_VERSION);
1137#ifdef RT_OS_DARWIN
1138 Footer.CreatorOS = RT_H2BE_U32(0x4D616320); /* "Mac " */
1139#else /* Virtual PC supports only two platforms atm, so everything else will be Wi2k. */
1140 Footer.CreatorOS = RT_H2BE_U32(0x5769326B); /* "Wi2k" */
1141#endif
1142 Footer.OrigSize = RT_H2BE_U64(cbSize);
1143 Footer.CurSize = Footer.OrigSize;
1144 Footer.DiskGeometryCylinder = RT_H2BE_U16(pImage->PCHSGeometry.cCylinders);
1145 Footer.DiskGeometryHeads = pImage->PCHSGeometry.cHeads;
1146 Footer.DiskGeometrySectors = pImage->PCHSGeometry.cSectors;
1147 memcpy(Footer.UniqueID, pImage->ImageUuid.au8, sizeof(Footer.UniqueID));
1148 Footer.SavedState = 0;
1149
1150 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
1151 {
1152 Footer.DiskType = RT_H2BE_U32(VHD_FOOTER_DISK_TYPE_FIXED);
1153 /*
1154 * Initialize fixed image.
1155 * "The size of the entire file is the size of the hard disk in
1156 * the guest operating system plus the size of the footer."
1157 */
1158 pImage->u64DataOffset = VHD_FOOTER_DATA_OFFSET_FIXED;
1159 pImage->uCurrentEndOfFile = cbSize;
1160 /** @todo r=klaus replace this with actual data writes, see the experience
1161 * with VDI files on Windows, can cause long freezes when writing. */
1162 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
1163 pImage->uCurrentEndOfFile + sizeof(VHDFooter));
1164 if (RT_FAILURE(rc))
1165 {
1166 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot set the file size for '%s'"), pImage->pszFilename);
1167 goto out;
1168 }
1169 }
1170 else
1171 {
1172 /*
1173 * Initialize dynamic image.
1174 *
1175 * The overall structure of dynamic disk is:
1176 *
1177 * [Copy of hard disk footer (512 bytes)]
1178 * [Dynamic disk header (1024 bytes)]
1179 * [BAT (Block Allocation Table)]
1180 * [Parent Locators]
1181 * [Data block 1]
1182 * [Data block 2]
1183 * ...
1184 * [Data block N]
1185 * [Hard disk footer (512 bytes)]
1186 */
1187 Footer.DiskType = (uImageFlags & VD_IMAGE_FLAGS_DIFF)
1188 ? RT_H2BE_U32(VHD_FOOTER_DISK_TYPE_DIFFERENCING)
1189 : RT_H2BE_U32(VHD_FOOTER_DISK_TYPE_DYNAMIC);
1190 /* We are half way thorough with creation of image, let the caller know. */
1191 if (pfnProgress)
1192 pfnProgress(pvUser, (uPercentStart + uPercentSpan) / 2);
1193
1194 rc = vhdCreateDynamicImage(pImage, cbSize);
1195 if (RT_FAILURE(rc))
1196 goto out;
1197 }
1198
1199 Footer.DataOffset = RT_H2BE_U64(pImage->u64DataOffset);
1200
1201 /* Compute and update the footer checksum. */
1202 Footer.Checksum = 0;
1203 Footer.Checksum = RT_H2BE_U32(vhdChecksum(&Footer, sizeof(Footer)));
1204
1205 pImage->vhdFooterCopy = Footer;
1206
1207 /* Store the footer */
1208 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->uCurrentEndOfFile,
1209 &Footer, sizeof(Footer));
1210 if (RT_FAILURE(rc))
1211 {
1212 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot write footer to image '%s'"), pImage->pszFilename);
1213 goto out;
1214 }
1215
1216 /* Dynamic images contain a copy of the footer at the very beginning of the file. */
1217 if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
1218 {
1219 /* Write the copy of the footer. */
1220 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0, &Footer, sizeof(Footer));
1221 if (RT_FAILURE(rc))
1222 {
1223 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot write a copy of footer to image '%s'"), pImage->pszFilename);
1224 goto out;
1225 }
1226 }
1227
1228out:
1229 if (RT_SUCCESS(rc) && pfnProgress)
1230 pfnProgress(pvUser, uPercentStart + uPercentSpan);
1231
1232 if (RT_FAILURE(rc))
1233 vhdFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
1234 return rc;
1235}
1236
1237
1238/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
1239static int vhdCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1240 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
1241{
1242 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
1243 int rc;
1244 PVDIOSTORAGE pStorage;
1245 uint64_t cbFile;
1246 VHDFooter vhdFooter;
1247
1248 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1249 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1250
1251 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1252 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1253 false /* fCreate */),
1254 &pStorage);
1255 if (RT_FAILURE(rc))
1256 goto out;
1257
1258 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1259 if (RT_FAILURE(rc))
1260 {
1261 vdIfIoIntFileClose(pIfIo, pStorage);
1262 rc = VERR_VD_VHD_INVALID_HEADER;
1263 goto out;
1264 }
1265
1266 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, cbFile - sizeof(VHDFooter),
1267 &vhdFooter, sizeof(VHDFooter));
1268 if (RT_SUCCESS(rc))
1269 {
1270 if (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0)
1271 {
1272 /*
1273 * There is also a backup header at the beginning in case the image got corrupted.
1274 * Such corrupted images are detected here to let the open handler repair it later.
1275 */
1276 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &vhdFooter, sizeof(VHDFooter));
1277 if ( RT_FAILURE(rc)
1278 || (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0))
1279 rc = VERR_VD_VHD_INVALID_HEADER;
1280 }
1281
1282 if (RT_SUCCESS(rc))
1283 *penmType = VDTYPE_HDD;
1284 }
1285 else
1286 rc = VERR_VD_VHD_INVALID_HEADER;
1287
1288 vdIfIoIntFileClose(pIfIo, pStorage);
1289
1290out:
1291 LogFlowFunc(("returns %Rrc\n", rc));
1292 return rc;
1293}
1294
1295/** @copydoc VBOXHDDBACKEND::pfnOpen */
1296static int vhdOpen(const char *pszFilename, unsigned uOpenFlags,
1297 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1298 VDTYPE enmType, void **ppBackendData)
1299{
1300 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
1301 int rc = VINF_SUCCESS;
1302 PVHDIMAGE pImage;
1303
1304 /* Check open flags. All valid flags are supported. */
1305 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1306 {
1307 rc = VERR_INVALID_PARAMETER;
1308 goto out;
1309 }
1310
1311 /* Check remaining arguments. */
1312 if ( !VALID_PTR(pszFilename)
1313 || !*pszFilename)
1314 {
1315 rc = VERR_INVALID_PARAMETER;
1316 goto out;
1317 }
1318
1319 pImage = (PVHDIMAGE)RTMemAllocZ(sizeof(VHDIMAGE));
1320 if (!pImage)
1321 {
1322 rc = VERR_NO_MEMORY;
1323 goto out;
1324 }
1325
1326 pImage->pszFilename = pszFilename;
1327 pImage->pStorage = NULL;
1328 pImage->pVDIfsDisk = pVDIfsDisk;
1329 pImage->pVDIfsImage = pVDIfsImage;
1330
1331 rc = vhdOpenImage(pImage, uOpenFlags);
1332 if (RT_SUCCESS(rc))
1333 *ppBackendData = pImage;
1334 else
1335 RTMemFree(pImage);
1336
1337out:
1338 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1339 return rc;
1340}
1341
1342/** @copydoc VBOXHDDBACKEND::pfnCreate */
1343static int vhdCreate(const char *pszFilename, uint64_t cbSize,
1344 unsigned uImageFlags, const char *pszComment,
1345 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1346 PCRTUUID pUuid, unsigned uOpenFlags,
1347 unsigned uPercentStart, unsigned uPercentSpan,
1348 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1349 PVDINTERFACE pVDIfsOperation, void **ppBackendData)
1350{
1351 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));
1352 int rc = VINF_SUCCESS;
1353 PVHDIMAGE pImage;
1354
1355 PFNVDPROGRESS pfnProgress = NULL;
1356 void *pvUser = NULL;
1357 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
1358 if (pIfProgress)
1359 {
1360 pfnProgress = pIfProgress->pfnProgress;
1361 pvUser = pIfProgress->Core.pvUser;
1362 }
1363
1364 /* Check open flags. All valid flags are supported. */
1365 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1366 {
1367 rc = VERR_INVALID_PARAMETER;
1368 return rc;
1369 }
1370
1371 /* @todo Check the values of other params */
1372
1373 pImage = (PVHDIMAGE)RTMemAllocZ(sizeof(VHDIMAGE));
1374 if (!pImage)
1375 {
1376 rc = VERR_NO_MEMORY;
1377 return rc;
1378 }
1379 pImage->pszFilename = pszFilename;
1380 pImage->pStorage = NULL;
1381 pImage->pVDIfsDisk = pVDIfsDisk;
1382 pImage->pVDIfsImage = pVDIfsImage;
1383
1384 /* Get I/O interface. */
1385 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1386 if (RT_UNLIKELY(!VALID_PTR(pImage->pIfIo)))
1387 {
1388 RTMemFree(pImage);
1389 return VERR_INVALID_PARAMETER;
1390 }
1391
1392 rc = vhdCreateImage(pImage, cbSize, uImageFlags, pszComment,
1393 pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags,
1394 pfnProgress, pvUser, uPercentStart, uPercentSpan);
1395
1396 if (RT_SUCCESS(rc))
1397 {
1398 /* So far the image is opened in read/write mode. Make sure the
1399 * image is opened in read-only mode if the caller requested that. */
1400 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1401 {
1402 vhdFreeImage(pImage, false);
1403 rc = vhdOpenImage(pImage, uOpenFlags);
1404 if (RT_FAILURE(rc))
1405 {
1406 RTMemFree(pImage);
1407 goto out;
1408 }
1409 }
1410 *ppBackendData = pImage;
1411 }
1412 else
1413 RTMemFree(pImage);
1414
1415out:
1416 LogFlowFunc(("returns %Rrc\n", rc));
1417 return rc;
1418}
1419
1420/** @copydoc VBOXHDDBACKEND::pfnRename */
1421static int vhdRename(void *pBackendData, const char *pszFilename)
1422{
1423 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1424 int rc = VINF_SUCCESS;
1425 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1426
1427 /* Check arguments. */
1428 if ( !pImage
1429 || !pszFilename
1430 || !*pszFilename)
1431 {
1432 rc = VERR_INVALID_PARAMETER;
1433 goto out;
1434 }
1435
1436 /* Close the image. */
1437 rc = vhdFreeImage(pImage, false);
1438 if (RT_FAILURE(rc))
1439 goto out;
1440
1441 /* Rename the file. */
1442 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1443 if (RT_FAILURE(rc))
1444 {
1445 /* The move failed, try to reopen the original image. */
1446 int rc2 = vhdOpenImage(pImage, pImage->uOpenFlags);
1447 if (RT_FAILURE(rc2))
1448 rc = rc2;
1449
1450 goto out;
1451 }
1452
1453 /* Update pImage with the new information. */
1454 pImage->pszFilename = pszFilename;
1455
1456 /* Open the old file with new name. */
1457 rc = vhdOpenImage(pImage, pImage->uOpenFlags);
1458 if (RT_FAILURE(rc))
1459 goto out;
1460
1461out:
1462 LogFlowFunc(("returns %Rrc\n", rc));
1463 return rc;
1464}
1465
1466/** @copydoc VBOXHDDBACKEND::pfnClose */
1467static int vhdClose(void *pBackendData, bool fDelete)
1468{
1469 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1470 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1471 int rc;
1472
1473 rc = vhdFreeImage(pImage, fDelete);
1474 RTMemFree(pImage);
1475
1476 LogFlowFunc(("returns %Rrc\n", rc));
1477 return rc;
1478}
1479
1480/** @copydoc VBOXHDDBACKEND::pfnRead */
1481static int vhdRead(void *pBackendData, uint64_t uOffset, size_t cbRead,
1482 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1483{
1484 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1485 int rc = VINF_SUCCESS;
1486
1487 LogFlowFunc(("pBackendData=%p uOffset=%#llx pIoCtx=%#p cbRead=%u pcbActuallyRead=%p\n", pBackendData, uOffset, pIoCtx, cbRead, pcbActuallyRead));
1488
1489 if (uOffset + cbRead > pImage->cbSize)
1490 return VERR_INVALID_PARAMETER;
1491
1492 /*
1493 * If we have a dynamic disk image, we need to find the data block and sector to read.
1494 */
1495 if (pImage->pBlockAllocationTable)
1496 {
1497 /*
1498 * Get the data block first.
1499 */
1500 uint32_t cBlockAllocationTableEntry = (uOffset / VHD_SECTOR_SIZE) / pImage->cSectorsPerDataBlock;
1501 uint32_t cBATEntryIndex = (uOffset / VHD_SECTOR_SIZE) % pImage->cSectorsPerDataBlock;
1502 uint64_t uVhdOffset;
1503
1504 LogFlowFunc(("cBlockAllocationTableEntry=%u cBatEntryIndex=%u\n", cBlockAllocationTableEntry, cBATEntryIndex));
1505 LogFlowFunc(("BlockAllocationEntry=%u\n", pImage->pBlockAllocationTable[cBlockAllocationTableEntry]));
1506
1507 /*
1508 * Clip read range to remain in this data block.
1509 */
1510 cbRead = RT_MIN(cbRead, (pImage->cbDataBlock - (cBATEntryIndex * VHD_SECTOR_SIZE)));
1511
1512 /*
1513 * If the block is not allocated the content of the entry is ~0
1514 */
1515 if (pImage->pBlockAllocationTable[cBlockAllocationTableEntry] == ~0U)
1516 rc = VERR_VD_BLOCK_FREE;
1517 else
1518 {
1519 uVhdOffset = ((uint64_t)pImage->pBlockAllocationTable[cBlockAllocationTableEntry] + pImage->cDataBlockBitmapSectors + cBATEntryIndex) * VHD_SECTOR_SIZE;
1520 LogFlowFunc(("uVhdOffset=%llu cbRead=%u\n", uVhdOffset, cbRead));
1521
1522 /* Read in the block's bitmap. */
1523 PVDMETAXFER pMetaXfer;
1524 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage,
1525 ((uint64_t)pImage->pBlockAllocationTable[cBlockAllocationTableEntry]) * VHD_SECTOR_SIZE,
1526 pImage->pu8Bitmap, pImage->cbDataBlockBitmap,
1527 pIoCtx, &pMetaXfer, NULL, NULL);
1528
1529 if (RT_SUCCESS(rc))
1530 {
1531 uint32_t cSectors = 0;
1532
1533 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
1534 if (vhdBlockBitmapSectorContainsData(pImage, cBATEntryIndex))
1535 {
1536 cBATEntryIndex++;
1537 cSectors = 1;
1538
1539 /*
1540 * The first sector being read is marked dirty, read as much as we
1541 * can from child. Note that only sectors that are marked dirty
1542 * must be read from child.
1543 */
1544 while ( (cSectors < (cbRead / VHD_SECTOR_SIZE))
1545 && vhdBlockBitmapSectorContainsData(pImage, cBATEntryIndex))
1546 {
1547 cBATEntryIndex++;
1548 cSectors++;
1549 }
1550
1551 cbRead = cSectors * VHD_SECTOR_SIZE;
1552
1553 LogFlowFunc(("uVhdOffset=%llu cbRead=%u\n", uVhdOffset, cbRead));
1554 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage,
1555 uVhdOffset, pIoCtx, cbRead);
1556 }
1557 else
1558 {
1559 /*
1560 * The first sector being read is marked clean, so we should read from
1561 * our parent instead, but only as much as there are the following
1562 * clean sectors, because the block may still contain dirty sectors
1563 * further on. We just need to compute the number of clean sectors
1564 * and pass it to our caller along with the notification that they
1565 * should be read from the parent.
1566 */
1567 cBATEntryIndex++;
1568 cSectors = 1;
1569
1570 while ( (cSectors < (cbRead / VHD_SECTOR_SIZE))
1571 && !vhdBlockBitmapSectorContainsData(pImage, cBATEntryIndex))
1572 {
1573 cBATEntryIndex++;
1574 cSectors++;
1575 }
1576
1577 cbRead = cSectors * VHD_SECTOR_SIZE;
1578 LogFunc(("Sectors free: uVhdOffset=%llu cbRead=%u\n", uVhdOffset, cbRead));
1579 rc = VERR_VD_BLOCK_FREE;
1580 }
1581 }
1582 else
1583 AssertMsg(rc == VERR_VD_NOT_ENOUGH_METADATA, ("Reading block bitmap failed rc=%Rrc\n", rc));
1584 }
1585 }
1586 else
1587 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, uOffset, pIoCtx, cbRead);
1588
1589 if (pcbActuallyRead)
1590 *pcbActuallyRead = cbRead;
1591
1592 LogFlowFunc(("returns rc=%Rrc\n", rc));
1593 return rc;
1594}
1595
1596/** @copydoc VBOXHDDBACKEND::pfnWrite */
1597static int vhdWrite(void *pBackendData, uint64_t uOffset, size_t cbWrite,
1598 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
1599 size_t *pcbPostRead, unsigned fWrite)
1600{
1601 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1602 int rc = VINF_SUCCESS;
1603
1604 LogFlowFunc(("pBackendData=%p uOffset=%llu pIoCtx=%#p cbWrite=%u pcbWriteProcess=%p pcbPreRead=%p pcbPostRead=%p fWrite=%u\n",
1605 pBackendData, uOffset, pIoCtx, cbWrite, pcbWriteProcess, pcbPreRead, pcbPostRead, fWrite));
1606
1607 AssertPtr(pImage);
1608 Assert(uOffset % VHD_SECTOR_SIZE == 0);
1609 Assert(cbWrite % VHD_SECTOR_SIZE == 0);
1610
1611 if (pImage->pBlockAllocationTable)
1612 {
1613 /*
1614 * Get the data block first.
1615 */
1616 uint32_t cSector = uOffset / VHD_SECTOR_SIZE;
1617 uint32_t cBlockAllocationTableEntry = cSector / pImage->cSectorsPerDataBlock;
1618 uint32_t cBATEntryIndex = cSector % pImage->cSectorsPerDataBlock;
1619 uint64_t uVhdOffset;
1620
1621 /*
1622 * Clip write range.
1623 */
1624 cbWrite = RT_MIN(cbWrite, (pImage->cbDataBlock - (cBATEntryIndex * VHD_SECTOR_SIZE)));
1625
1626 /*
1627 * If the block is not allocated the content of the entry is ~0
1628 * and we need to allocate a new block. Note that while blocks are
1629 * allocated with a relatively big granularity, each sector has its
1630 * own bitmap entry, indicating whether it has been written or not.
1631 * So that means for the purposes of the higher level that the
1632 * granularity is invisible. This means there's no need to return
1633 * VERR_VD_BLOCK_FREE unless the block hasn't been allocated yet.
1634 */
1635 if (pImage->pBlockAllocationTable[cBlockAllocationTableEntry] == ~0U)
1636 {
1637 /* Check if the block allocation should be suppressed. */
1638 if (fWrite & VD_WRITE_NO_ALLOC)
1639 {
1640 *pcbPreRead = cBATEntryIndex * VHD_SECTOR_SIZE;
1641 *pcbPostRead = pImage->cSectorsPerDataBlock * VHD_SECTOR_SIZE - cbWrite - *pcbPreRead;
1642
1643 if (pcbWriteProcess)
1644 *pcbWriteProcess = cbWrite;
1645 return VERR_VD_BLOCK_FREE;
1646 }
1647
1648 PVHDIMAGEEXPAND pExpand = (PVHDIMAGEEXPAND)RTMemAllocZ(RT_OFFSETOF(VHDIMAGEEXPAND, au8Bitmap[pImage->cDataBlockBitmapSectors * VHD_SECTOR_SIZE]));
1649 bool fIoInProgress = false;
1650
1651 if (!pExpand)
1652 return VERR_NO_MEMORY;
1653
1654 pExpand->cbEofOld = pImage->uCurrentEndOfFile;
1655 pExpand->idxBatAllocated = cBlockAllocationTableEntry;
1656 pExpand->idxBlockBe = RT_H2BE_U32(pImage->uCurrentEndOfFile / VHD_SECTOR_SIZE);
1657
1658 /* Set the bits for all sectors having been written. */
1659 for (uint32_t iSector = 0; iSector < (cbWrite / VHD_SECTOR_SIZE); iSector++)
1660 {
1661 /* No need to check for a changed value because this is an initial write. */
1662 vhdBlockBitmapSectorSet(pImage, pExpand->au8Bitmap, cBATEntryIndex);
1663 cBATEntryIndex++;
1664 }
1665
1666 do
1667 {
1668 /*
1669 * Start with the sector bitmap.
1670 */
1671 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1672 pImage->uCurrentEndOfFile,
1673 pExpand->au8Bitmap,
1674 pImage->cDataBlockBitmapSectors * VHD_SECTOR_SIZE, pIoCtx,
1675 vhdAsyncExpansionDataBlockBitmapComplete,
1676 pExpand);
1677 if (RT_SUCCESS(rc))
1678 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_BLOCKBITMAP_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_SUCCESS);
1679 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1680 fIoInProgress = true;
1681 else
1682 {
1683 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_BLOCKBITMAP_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1684 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_USERBLOCK_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1685 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_BAT_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1686 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_FOOTER_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1687 break;
1688 }
1689
1690
1691 /*
1692 * Write the new block at the current end of the file.
1693 */
1694 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1695 pImage->uCurrentEndOfFile + pImage->cDataBlockBitmapSectors * VHD_SECTOR_SIZE,
1696 pIoCtx, cbWrite,
1697 vhdAsyncExpansionDataComplete,
1698 pExpand);
1699 if (RT_SUCCESS(rc))
1700 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_USERBLOCK_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_SUCCESS);
1701 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1702 fIoInProgress = true;
1703 else
1704 {
1705 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_USERBLOCK_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1706 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_BAT_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1707 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_FOOTER_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1708 break;
1709 }
1710
1711 /*
1712 * Write entry in the BAT.
1713 */
1714 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1715 pImage->uBlockAllocationTableOffset + cBlockAllocationTableEntry * sizeof(uint32_t),
1716 &pExpand->idxBlockBe, sizeof(uint32_t), pIoCtx,
1717 vhdAsyncExpansionBatUpdateComplete,
1718 pExpand);
1719 if (RT_SUCCESS(rc))
1720 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_BAT_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_SUCCESS);
1721 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1722 fIoInProgress = true;
1723 else
1724 {
1725 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_BAT_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1726 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_FOOTER_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1727 break;
1728 }
1729
1730 /*
1731 * Set the new end of the file and link the new block into the BAT.
1732 */
1733 pImage->pBlockAllocationTable[cBlockAllocationTableEntry] = pImage->uCurrentEndOfFile / VHD_SECTOR_SIZE;
1734 pImage->uCurrentEndOfFile += pImage->cDataBlockBitmapSectors * VHD_SECTOR_SIZE + pImage->cbDataBlock;
1735
1736 /* Update the footer. */
1737 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1738 pImage->uCurrentEndOfFile,
1739 &pImage->vhdFooterCopy,
1740 sizeof(VHDFooter), pIoCtx,
1741 vhdAsyncExpansionFooterUpdateComplete,
1742 pExpand);
1743 if (RT_SUCCESS(rc))
1744 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_FOOTER_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_SUCCESS);
1745 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1746 fIoInProgress = true;
1747 else
1748 {
1749 VHDIMAGEEXPAND_STATUS_SET(pExpand->fFlags, VHDIMAGEEXPAND_FOOTER_STATUS_SHIFT, VHDIMAGEEXPAND_STEP_FAILED);
1750 break;
1751 }
1752
1753 } while (0);
1754
1755 if (!fIoInProgress)
1756 vhdAsyncExpansionComplete(pImage, pIoCtx, pExpand);
1757 else
1758 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1759 }
1760 else
1761 {
1762 /*
1763 * Calculate the real offset in the file.
1764 */
1765 uVhdOffset = ((uint64_t)pImage->pBlockAllocationTable[cBlockAllocationTableEntry] + pImage->cDataBlockBitmapSectors + cBATEntryIndex) * VHD_SECTOR_SIZE;
1766
1767 /* Read in the block's bitmap. */
1768 PVDMETAXFER pMetaXfer;
1769 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage,
1770 ((uint64_t)pImage->pBlockAllocationTable[cBlockAllocationTableEntry]) * VHD_SECTOR_SIZE,
1771 pImage->pu8Bitmap,
1772 pImage->cbDataBlockBitmap, pIoCtx,
1773 &pMetaXfer, NULL, NULL);
1774 if (RT_SUCCESS(rc))
1775 {
1776 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
1777
1778 /* Write data. */
1779 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1780 uVhdOffset, pIoCtx, cbWrite,
1781 NULL, NULL);
1782 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1783 {
1784 bool fChanged = false;
1785
1786 /* Set the bits for all sectors having been written. */
1787 for (uint32_t iSector = 0; iSector < (cbWrite / VHD_SECTOR_SIZE); iSector++)
1788 {
1789 fChanged |= vhdBlockBitmapSectorSet(pImage, pImage->pu8Bitmap, cBATEntryIndex);
1790 cBATEntryIndex++;
1791 }
1792
1793 /* Only write the bitmap if it was changed. */
1794 if (fChanged)
1795 {
1796 /*
1797 * Write the bitmap back.
1798 *
1799 * @note We don't have a completion callback here because we
1800 * can't do anything if the write fails for some reason.
1801 * The error will propagated to the device/guest
1802 * by the generic VD layer already and we don't need
1803 * to rollback anything here.
1804 */
1805 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1806 ((uint64_t)pImage->pBlockAllocationTable[cBlockAllocationTableEntry]) * VHD_SECTOR_SIZE,
1807 pImage->pu8Bitmap,
1808 pImage->cbDataBlockBitmap,
1809 pIoCtx, NULL, NULL);
1810 }
1811 }
1812 }
1813 }
1814 }
1815 else
1816 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1817 uOffset, pIoCtx, cbWrite, NULL, NULL);
1818
1819 if (pcbWriteProcess)
1820 *pcbWriteProcess = cbWrite;
1821
1822 /* Stay on the safe side. Do not run the risk of confusing the higher
1823 * level, as that can be pretty lethal to image consistency. */
1824 *pcbPreRead = 0;
1825 *pcbPostRead = 0;
1826
1827 return rc;
1828}
1829
1830/** @copydoc VBOXHDDBACKEND::pfnFlush */
1831static int vhdFlush(void *pBackendData, PVDIOCTX pIoCtx)
1832{
1833 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1834
1835 /* No need to write anything here. Data is always updated on a write. */
1836 return vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage, pIoCtx, NULL, NULL);
1837}
1838
1839/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
1840static unsigned vhdGetVersion(void *pBackendData)
1841{
1842 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1843 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1844 unsigned ver = 0;
1845
1846 AssertPtr(pImage);
1847
1848 if (pImage)
1849 ver = 1; /**< @todo use correct version */
1850
1851 LogFlowFunc(("returns %u\n", ver));
1852 return ver;
1853}
1854
1855/** @copydoc VBOXHDDBACKEND::pfnGetSize */
1856static uint64_t vhdGetSize(void *pBackendData)
1857{
1858 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1859 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1860 uint64_t cb = 0;
1861
1862 AssertPtr(pImage);
1863
1864 if (pImage && pImage->pStorage)
1865 cb = pImage->cbSize;
1866
1867 LogFlowFunc(("returns %llu\n", cb));
1868 return cb;
1869}
1870
1871/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
1872static uint64_t vhdGetFileSize(void *pBackendData)
1873{
1874 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1875 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1876 uint64_t cb = 0;
1877
1878 AssertPtr(pImage);
1879
1880 if (pImage && pImage->pStorage)
1881 cb = pImage->uCurrentEndOfFile + sizeof(VHDFooter);
1882
1883 LogFlowFunc(("returns %lld\n", cb));
1884 return cb;
1885}
1886
1887/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
1888static int vhdGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
1889{
1890 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
1891 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1892 int rc;
1893
1894 AssertPtr(pImage);
1895
1896 if (pImage)
1897 {
1898 if (pImage->PCHSGeometry.cCylinders)
1899 {
1900 *pPCHSGeometry = pImage->PCHSGeometry;
1901 rc = VINF_SUCCESS;
1902 }
1903 else
1904 rc = VERR_VD_GEOMETRY_NOT_SET;
1905 }
1906 else
1907 rc = VERR_VD_NOT_OPENED;
1908
1909 LogFlowFunc(("returns %Rrc (CHS=%u/%u/%u)\n", rc, pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors));
1910 return rc;
1911}
1912
1913/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
1914static int vhdSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
1915{
1916 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1917 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1918 int rc;
1919
1920 AssertPtr(pImage);
1921
1922 if (pImage)
1923 {
1924 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1925 {
1926 rc = VERR_VD_IMAGE_READ_ONLY;
1927 goto out;
1928 }
1929
1930 pImage->PCHSGeometry = *pPCHSGeometry;
1931 rc = VINF_SUCCESS;
1932 }
1933 else
1934 rc = VERR_VD_NOT_OPENED;
1935
1936out:
1937 LogFlowFunc(("returns %Rrc\n", rc));
1938 return rc;
1939}
1940
1941/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
1942static int vhdGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
1943{
1944LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
1945 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1946 int rc;
1947
1948 AssertPtr(pImage);
1949
1950 if (pImage)
1951 {
1952 if (pImage->LCHSGeometry.cCylinders)
1953 {
1954 *pLCHSGeometry = pImage->LCHSGeometry;
1955 rc = VINF_SUCCESS;
1956 }
1957 else
1958 rc = VERR_VD_GEOMETRY_NOT_SET;
1959 }
1960 else
1961 rc = VERR_VD_NOT_OPENED;
1962
1963 LogFlowFunc(("returns %Rrc (CHS=%u/%u/%u)\n", rc, pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors));
1964 return rc;
1965}
1966
1967/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
1968static int vhdSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
1969{
1970 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1971 int rc;
1972
1973 AssertPtr(pImage);
1974
1975 if (pImage)
1976 {
1977 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1978 {
1979 rc = VERR_VD_IMAGE_READ_ONLY;
1980 goto out;
1981 }
1982
1983 pImage->LCHSGeometry = *pLCHSGeometry;
1984 rc = VINF_SUCCESS;
1985 }
1986 else
1987 rc = VERR_VD_NOT_OPENED;
1988
1989out:
1990 LogFlowFunc(("returns %Rrc\n", rc));
1991 return rc;
1992}
1993
1994/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
1995static unsigned vhdGetImageFlags(void *pBackendData)
1996{
1997 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1998 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
1999 unsigned uImageFlags;
2000
2001 AssertPtr(pImage);
2002
2003 if (pImage)
2004 uImageFlags = pImage->uImageFlags;
2005 else
2006 uImageFlags = 0;
2007
2008 LogFlowFunc(("returns %#x\n", uImageFlags));
2009 return uImageFlags;
2010}
2011
2012/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
2013static unsigned vhdGetOpenFlags(void *pBackendData)
2014{
2015 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2016 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2017 unsigned uOpenFlags;
2018
2019 AssertPtr(pImage);
2020
2021 if (pImage)
2022 uOpenFlags = pImage->uOpenFlags;
2023 else
2024 uOpenFlags = 0;
2025
2026 LogFlowFunc(("returns %#x\n", uOpenFlags));
2027 return uOpenFlags;
2028}
2029
2030/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
2031static int vhdSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2032{
2033 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2034 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2035 int rc;
2036
2037 /* Image must be opened and the new flags must be valid. */
2038 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2039 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
2040 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2041 {
2042 rc = VERR_INVALID_PARAMETER;
2043 goto out;
2044 }
2045
2046 /* Implement this operation via reopening the image. */
2047 rc = vhdFreeImage(pImage, false);
2048 if (RT_FAILURE(rc))
2049 goto out;
2050 rc = vhdOpenImage(pImage, uOpenFlags);
2051
2052out:
2053 LogFlowFunc(("returns %Rrc\n", rc));
2054 return rc;
2055}
2056
2057/** @copydoc VBOXHDDBACKEND::pfnGetComment */
2058static int vhdGetComment(void *pBackendData, char *pszComment,
2059 size_t cbComment)
2060{
2061 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
2062 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2063 int rc;
2064
2065 AssertPtr(pImage);
2066
2067 if (pImage)
2068 rc = VERR_NOT_SUPPORTED;
2069 else
2070 rc = VERR_VD_NOT_OPENED;
2071
2072 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
2073 return rc;
2074}
2075
2076/** @copydoc VBOXHDDBACKEND::pfnSetComment */
2077static int vhdSetComment(void *pBackendData, const char *pszComment)
2078{
2079 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
2080 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2081 int rc;
2082
2083 AssertPtr(pImage);
2084
2085 if (pImage)
2086 {
2087 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2088 rc = VERR_VD_IMAGE_READ_ONLY;
2089 else
2090 rc = VERR_NOT_SUPPORTED;
2091 }
2092 else
2093 rc = VERR_VD_NOT_OPENED;
2094
2095 LogFlowFunc(("returns %Rrc\n", rc));
2096 return rc;
2097}
2098
2099/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
2100static int vhdGetUuid(void *pBackendData, PRTUUID pUuid)
2101{
2102 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2103 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2104 int rc;
2105
2106 AssertPtr(pImage);
2107
2108 if (pImage)
2109 {
2110 *pUuid = pImage->ImageUuid;
2111 rc = VINF_SUCCESS;
2112 }
2113 else
2114 rc = VERR_VD_NOT_OPENED;
2115
2116 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2117 return rc;
2118}
2119
2120/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
2121static int vhdSetUuid(void *pBackendData, PCRTUUID pUuid)
2122{
2123 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2124 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2125 int rc;
2126
2127 AssertPtr(pImage);
2128
2129 if (pImage)
2130 {
2131 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2132 {
2133 pImage->ImageUuid = *pUuid;
2134 /* Update the footer copy. It will get written to disk when the image is closed. */
2135 memcpy(&pImage->vhdFooterCopy.UniqueID, pUuid, 16);
2136 /* Update checksum. */
2137 pImage->vhdFooterCopy.Checksum = 0;
2138 pImage->vhdFooterCopy.Checksum = RT_H2BE_U32(vhdChecksum(&pImage->vhdFooterCopy, sizeof(VHDFooter)));
2139
2140 /* Need to update the dynamic disk header to update the disk footer copy at the beginning. */
2141 if (!(pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED))
2142 pImage->fDynHdrNeedsUpdate = true;
2143 rc = VINF_SUCCESS;
2144 }
2145 else
2146 rc = VERR_VD_IMAGE_READ_ONLY;
2147 }
2148 else
2149 rc = VERR_VD_NOT_OPENED;
2150
2151 LogFlowFunc(("returns %Rrc\n", rc));
2152 return rc;
2153}
2154
2155/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
2156static int vhdGetModificationUuid(void *pBackendData, PRTUUID pUuid)
2157{
2158 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2159 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2160 int rc;
2161
2162 AssertPtr(pImage);
2163
2164 if (pImage)
2165 rc = VERR_NOT_SUPPORTED;
2166 else
2167 rc = VERR_VD_NOT_OPENED;
2168
2169 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2170 return rc;
2171}
2172
2173/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
2174static int vhdSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
2175{
2176 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2177 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2178 int rc;
2179
2180 AssertPtr(pImage);
2181
2182 if (pImage)
2183 {
2184 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2185 rc = VERR_NOT_SUPPORTED;
2186 else
2187 rc = VERR_VD_IMAGE_READ_ONLY;
2188 }
2189 else
2190 rc = VERR_VD_NOT_OPENED;
2191
2192 LogFlowFunc(("returns %Rrc\n", rc));
2193 return rc;
2194}
2195
2196/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
2197static int vhdGetParentUuid(void *pBackendData, PRTUUID pUuid)
2198{
2199 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2200 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2201 int rc;
2202
2203 AssertPtr(pImage);
2204
2205 if (pImage)
2206 {
2207 *pUuid = pImage->ParentUuid;
2208 rc = VINF_SUCCESS;
2209 }
2210 else
2211 rc = VERR_VD_NOT_OPENED;
2212
2213 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2214 return rc;
2215}
2216
2217/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
2218static int vhdSetParentUuid(void *pBackendData, PCRTUUID pUuid)
2219{
2220 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2221 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2222 int rc = VINF_SUCCESS;
2223
2224 AssertPtr(pImage);
2225
2226 if (pImage && pImage->pStorage)
2227 {
2228 if (!(pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED))
2229 {
2230 pImage->ParentUuid = *pUuid;
2231 pImage->fDynHdrNeedsUpdate = true;
2232 }
2233 else
2234 rc = VERR_VD_IMAGE_READ_ONLY;
2235 }
2236 else
2237 rc = VERR_VD_NOT_OPENED;
2238
2239 LogFlowFunc(("returns %Rrc\n", rc));
2240 return rc;
2241}
2242
2243/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
2244static int vhdGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
2245{
2246 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2247 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2248 int rc;
2249
2250 AssertPtr(pImage);
2251
2252 if (pImage)
2253 rc = VERR_NOT_SUPPORTED;
2254 else
2255 rc = VERR_VD_NOT_OPENED;
2256
2257 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2258 return rc;
2259}
2260
2261/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
2262static int vhdSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
2263{
2264 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2265 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2266 int rc;
2267
2268 AssertPtr(pImage);
2269
2270 if (pImage)
2271 {
2272 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2273 rc = VERR_NOT_SUPPORTED;
2274 else
2275 rc = VERR_VD_IMAGE_READ_ONLY;
2276 }
2277 else
2278 rc = VERR_VD_NOT_OPENED;
2279
2280 LogFlowFunc(("returns %Rrc\n", rc));
2281 return rc;
2282}
2283
2284/** @copydoc VBOXHDDBACKEND::pfnDump */
2285static void vhdDump(void *pBackendData)
2286{
2287 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2288
2289 AssertPtr(pImage);
2290 if (pImage)
2291 {
2292 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%u\n",
2293 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2294 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2295 VHD_SECTOR_SIZE);
2296 vdIfErrorMessage(pImage->pIfError, "Header: uuidCreation={%RTuuid}\n", &pImage->ImageUuid);
2297 vdIfErrorMessage(pImage->pIfError, "Header: uuidParent={%RTuuid}\n", &pImage->ParentUuid);
2298 }
2299}
2300
2301/** @copydoc VBOXHDDBACKEND::pfnGetTimestamp */
2302static int vhdGetTimeStamp(void *pBackendData, PRTTIMESPEC pTimeStamp)
2303{
2304 int rc = VINF_SUCCESS;
2305 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2306
2307 AssertPtr(pImage);
2308
2309 if (pImage)
2310 rc = vdIfIoIntFileGetModificationTime(pImage->pIfIo, pImage->pszFilename, pTimeStamp);
2311 else
2312 rc = VERR_VD_NOT_OPENED;
2313
2314 LogFlowFunc(("returns %Rrc\n", rc));
2315 return rc;
2316}
2317
2318/** @copydoc VBOXHDDBACKEND::pfnGetParentTimeStamp */
2319static int vhdGetParentTimeStamp(void *pBackendData, PRTTIMESPEC pTimeStamp)
2320{
2321 int rc = VINF_SUCCESS;
2322 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2323
2324 AssertPtr(pImage);
2325
2326 if (pImage)
2327 vhdTime2RtTime(pTimeStamp, pImage->u32ParentTimeStamp);
2328 else
2329 rc = VERR_VD_NOT_OPENED;
2330
2331 LogFlowFunc(("returns %Rrc\n", rc));
2332 return rc;
2333}
2334
2335/** @copydoc VBOXHDDBACKEND::pfnSetParentTimeStamp */
2336static int vhdSetParentTimeStamp(void *pBackendData, PCRTTIMESPEC pTimeStamp)
2337{
2338 int rc = VINF_SUCCESS;
2339 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2340
2341 AssertPtr(pImage);
2342 if (pImage)
2343 {
2344 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2345 rc = VERR_VD_IMAGE_READ_ONLY;
2346 else
2347 {
2348 pImage->u32ParentTimeStamp = vhdRtTime2VhdTime(pTimeStamp);
2349 pImage->fDynHdrNeedsUpdate = true;
2350 }
2351 }
2352 else
2353 rc = VERR_VD_NOT_OPENED;
2354
2355 LogFlowFunc(("returns %Rrc\n", rc));
2356 return rc;
2357}
2358
2359/** @copydoc VBOXHDDBACKEND::pfnGetParentFilename */
2360static int vhdGetParentFilename(void *pBackendData, char **ppszParentFilename)
2361{
2362 int rc = VINF_SUCCESS;
2363 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2364
2365 AssertPtr(pImage);
2366 if (pImage)
2367 *ppszParentFilename = RTStrDup(pImage->pszParentFilename);
2368 else
2369 rc = VERR_VD_NOT_OPENED;
2370
2371 LogFlowFunc(("returns %Rrc\n", rc));
2372 return rc;
2373}
2374
2375/** @copydoc VBOXHDDBACKEND::pfnSetParentFilename */
2376static int vhdSetParentFilename(void *pBackendData, const char *pszParentFilename)
2377{
2378 int rc = VINF_SUCCESS;
2379 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2380
2381 AssertPtr(pImage);
2382 if (pImage)
2383 {
2384 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2385 rc = VERR_VD_IMAGE_READ_ONLY;
2386 else
2387 {
2388 if (pImage->pszParentFilename)
2389 RTStrFree(pImage->pszParentFilename);
2390 pImage->pszParentFilename = RTStrDup(pszParentFilename);
2391 if (!pImage->pszParentFilename)
2392 rc = VERR_NO_MEMORY;
2393 else
2394 pImage->fDynHdrNeedsUpdate = true;
2395 }
2396 }
2397 else
2398 rc = VERR_VD_NOT_OPENED;
2399
2400 LogFlowFunc(("returns %Rrc\n", rc));
2401 return rc;
2402}
2403
2404/** @copydoc VBOXHDDBACKEND::pfnCompact */
2405static int vhdCompact(void *pBackendData, unsigned uPercentStart,
2406 unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
2407 PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation)
2408{
2409 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2410 int rc = VINF_SUCCESS;
2411 void *pvBuf = NULL, *pvReplace = NULL;
2412 uint32_t *paBlocks = NULL;
2413
2414 int (*pfnParentRead)(void *, uint64_t, void *, size_t) = NULL;
2415 void *pvParent = NULL;
2416 PVDINTERFACEPARENTSTATE pIfParentState = VDIfParentStateGet(pVDIfsOperation);
2417 if (pIfParentState)
2418 {
2419 pfnParentRead = pIfParentState->pfnParentRead;
2420 pvParent = pIfParentState->Core.pvUser;
2421 }
2422
2423 PFNVDPROGRESS pfnProgress = NULL;
2424 void *pvUser = NULL;
2425 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
2426 if (pIfProgress)
2427 {
2428 pfnProgress = pIfProgress->pfnProgress;
2429 pvUser = pIfProgress->Core.pvUser;
2430 }
2431
2432 do
2433 {
2434 AssertBreakStmt(pImage, rc = VERR_INVALID_PARAMETER);
2435
2436 AssertBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
2437 rc = VERR_VD_IMAGE_READ_ONLY);
2438
2439 /* Reject fixed images as they don't have a BAT. */
2440 if (pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
2441 {
2442 rc = VERR_NOT_SUPPORTED;
2443 break;
2444 }
2445
2446 if (pfnParentRead)
2447 {
2448 pvParent = RTMemTmpAlloc(pImage->cbDataBlock);
2449 AssertBreakStmt(VALID_PTR(pvBuf), rc = VERR_NO_MEMORY);
2450 }
2451 pvBuf = RTMemTmpAlloc(pImage->cbDataBlock);
2452 AssertBreakStmt(VALID_PTR(pvBuf), rc = VERR_NO_MEMORY);
2453
2454 unsigned cBlocksAllocated = 0;
2455 unsigned cBlocksToMove = 0;
2456 unsigned cBlocks = pImage->cBlockAllocationTableEntries;
2457 uint32_t offBlocksStart = ~0U; /* Start offset of data blocks in sectors. */
2458 uint32_t *paBat = pImage->pBlockAllocationTable;
2459
2460 /* Count the number of allocated blocks and find the start offset for the data blocks. */
2461 for (unsigned i = 0; i < cBlocks; i++)
2462 if (paBat[i] != ~0U)
2463 {
2464 cBlocksAllocated++;
2465 if (paBat[i] < offBlocksStart)
2466 offBlocksStart = paBat[i];
2467 }
2468
2469 if (!cBlocksAllocated)
2470 {
2471 /* Nothing to do. */
2472 rc = VINF_SUCCESS;
2473 break;
2474 }
2475
2476 paBlocks = (uint32_t *)RTMemTmpAllocZ(cBlocksAllocated * sizeof(uint32_t));
2477 AssertBreakStmt(VALID_PTR(paBlocks), rc = VERR_NO_MEMORY);
2478
2479 /* Invalidate the back resolving array. */
2480 for (unsigned i = 0; i < cBlocksAllocated; i++)
2481 paBlocks[i] = ~0U;
2482
2483 /* Fill the back resolving table. */
2484 for (unsigned i = 0; i < cBlocks; i++)
2485 if (paBat[i] != ~0U)
2486 {
2487 unsigned idxBlock = (paBat[i] - offBlocksStart) / pImage->cSectorsPerDataBlock;
2488 if ( idxBlock < cBlocksAllocated
2489 && paBlocks[idxBlock] == ~0U)
2490 paBlocks[idxBlock] = i;
2491 else
2492 {
2493 /* The image is in an inconsistent state. Don't go further. */
2494 rc = VERR_INVALID_STATE;
2495 break;
2496 }
2497 }
2498
2499 if (RT_FAILURE(rc))
2500 break;
2501
2502 /* Find redundant information and update the block pointers
2503 * accordingly, creating bubbles. Keep disk up to date, as this
2504 * enables cancelling. */
2505 for (unsigned i = 0; i < cBlocks; i++)
2506 {
2507 if (paBat[i] != ~0U)
2508 {
2509 unsigned idxBlock = (paBat[i] - offBlocksStart) / pImage->cSectorsPerDataBlock;
2510
2511 /* Block present in image file, read relevant data. */
2512 uint64_t u64Offset = ((uint64_t)paBat[i] + pImage->cDataBlockBitmapSectors) * VHD_SECTOR_SIZE;
2513 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
2514 u64Offset, pvBuf, pImage->cbDataBlock);
2515 if (RT_FAILURE(rc))
2516 break;
2517
2518 if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)pImage->cbDataBlock * 8) == -1)
2519 {
2520 paBat[i] = ~0;
2521 paBlocks[idxBlock] = ~0U;
2522 /* Adjust progress info, one block to be relocated. */
2523 cBlocksToMove++;
2524 }
2525 else if (pfnParentRead)
2526 {
2527 rc = pfnParentRead(pvParent, (uint64_t)i * pImage->cbDataBlock, pvParent, pImage->cbDataBlock);
2528 if (RT_FAILURE(rc))
2529 break;
2530 if (!memcmp(pvParent, pvBuf, pImage->cbDataBlock))
2531 {
2532 paBat[i] = ~0U;
2533 paBlocks[idxBlock] = ~0U;
2534 /* Adjust progress info, one block to be relocated. */
2535 cBlocksToMove++;
2536 }
2537 }
2538 }
2539
2540 if (pIfProgress && pIfProgress->pfnProgress)
2541 {
2542 rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
2543 (uint64_t)i * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
2544 if (RT_FAILURE(rc))
2545 break;
2546 }
2547 }
2548
2549 if (RT_SUCCESS(rc))
2550 {
2551 /* Fill bubbles with other data (if available). */
2552 unsigned cBlocksMoved = 0;
2553 unsigned uBlockUsedPos = cBlocksAllocated;
2554 size_t cbBlock = pImage->cbDataBlock + pImage->cbDataBlockBitmap; /** < Size of whole block containing the bitmap and the user data. */
2555
2556 /* Allocate data buffer to hold the data block and allocation bitmap in front of the actual data. */
2557 RTMemTmpFree(pvBuf);
2558 pvBuf = RTMemTmpAllocZ(cbBlock);
2559 AssertBreakStmt(VALID_PTR(pvBuf), rc = VERR_NO_MEMORY);
2560
2561 for (unsigned i = 0; i < cBlocksAllocated; i++)
2562 {
2563 unsigned uBlock = paBlocks[i];
2564 if (uBlock == ~0U)
2565 {
2566 unsigned uBlockData = ~0U;
2567 while (uBlockUsedPos > i && uBlockData == ~0U)
2568 {
2569 uBlockUsedPos--;
2570 uBlockData = paBlocks[uBlockUsedPos];
2571 }
2572 /* Terminate early if there is no block which needs copying. */
2573 if (uBlockUsedPos == i)
2574 break;
2575 uint64_t u64Offset = (uint64_t)uBlockUsedPos * cbBlock
2576 + (offBlocksStart * VHD_SECTOR_SIZE);
2577 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
2578 u64Offset, pvBuf, cbBlock);
2579 if (RT_FAILURE(rc))
2580 break;
2581
2582 u64Offset = (uint64_t)i * cbBlock
2583 + (offBlocksStart * VHD_SECTOR_SIZE);
2584 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2585 u64Offset, pvBuf, cbBlock);
2586 if (RT_FAILURE(rc))
2587 break;
2588
2589 paBat[uBlockData] = i*(pImage->cSectorsPerDataBlock + pImage->cDataBlockBitmapSectors) + offBlocksStart;
2590
2591 /* Truncate the file but leave enough room for the footer to avoid
2592 * races if other processes fill the whole harddisk. */
2593 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
2594 pImage->uCurrentEndOfFile - cbBlock + VHD_SECTOR_SIZE);
2595 if (RT_FAILURE(rc))
2596 break;
2597
2598 /* Update pointers and write footer. */
2599 pImage->uCurrentEndOfFile -= cbBlock;
2600
2601 /* We're kinda screwed if this failes. */
2602 rc = vhdUpdateFooter(pImage);
2603 if (RT_FAILURE(rc))
2604 break;
2605
2606 paBlocks[i] = uBlockData;
2607 paBlocks[uBlockUsedPos] = ~0U;
2608 cBlocksMoved++;
2609 }
2610
2611 if (pIfProgress && pIfProgress->pfnProgress)
2612 {
2613 rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
2614 (uint64_t)(cBlocks + cBlocksMoved) * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
2615
2616 if (RT_FAILURE(rc))
2617 break;
2618 }
2619 }
2620 }
2621
2622 /* Write the new BAT in any case. */
2623 rc = vhdFlushImage(pImage);
2624 } while (0);
2625
2626 if (paBlocks)
2627 RTMemTmpFree(paBlocks);
2628 if (pvParent)
2629 RTMemTmpFree(pvParent);
2630 if (pvBuf)
2631 RTMemTmpFree(pvBuf);
2632
2633 if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
2634 {
2635 pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
2636 uPercentStart + uPercentSpan);
2637 }
2638
2639 LogFlowFunc(("returns %Rrc\n", rc));
2640 return rc;
2641}
2642
2643/** @copydoc VBOXHDDBACKEND::pfnResize */
2644static int vhdResize(void *pBackendData, uint64_t cbSize,
2645 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
2646 unsigned uPercentStart, unsigned uPercentSpan,
2647 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
2648 PVDINTERFACE pVDIfsOperation)
2649{
2650 PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
2651 int rc = VINF_SUCCESS;
2652
2653 PFNVDPROGRESS pfnProgress = NULL;
2654 void *pvUser = NULL;
2655 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
2656 if (pIfProgress)
2657 {
2658 pfnProgress = pIfProgress->pfnProgress;
2659 pvUser = pIfProgress->Core.pvUser;
2660 }
2661
2662 /* Making the image smaller is not supported at the moment. */
2663 if ( cbSize < pImage->cbSize
2664 || pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
2665 rc = VERR_NOT_SUPPORTED;
2666 else if (cbSize > pImage->cbSize)
2667 {
2668 unsigned cBlocksAllocated = 0;
2669 size_t cbBlock = pImage->cbDataBlock + pImage->cbDataBlockBitmap; /** < Size of a block including the sector bitmap. */
2670 uint32_t cBlocksNew = cbSize / pImage->cbDataBlock; /** < New number of blocks in the image after the resize */
2671 if (cbSize % pImage->cbDataBlock)
2672 cBlocksNew++;
2673
2674 uint32_t cBlocksOld = pImage->cBlockAllocationTableEntries; /** < Number of blocks before the resize. */
2675 uint64_t cbBlockspaceNew = RT_ALIGN_32(cBlocksNew * sizeof(uint32_t), VHD_SECTOR_SIZE); /** < Required space for the block array after the resize. */
2676 uint64_t offStartDataNew = RT_ALIGN_32(pImage->uBlockAllocationTableOffset + cbBlockspaceNew, VHD_SECTOR_SIZE); /** < New start offset for block data after the resize */
2677 uint64_t offStartDataOld = ~0ULL;
2678
2679 /* Go through the BAT and find the data start offset. */
2680 for (unsigned idxBlock = 0; idxBlock < pImage->cBlockAllocationTableEntries; idxBlock++)
2681 {
2682 if (pImage->pBlockAllocationTable[idxBlock] != ~0U)
2683 {
2684 uint64_t offStartBlock = pImage->pBlockAllocationTable[idxBlock] * VHD_SECTOR_SIZE;
2685 if (offStartBlock < offStartDataOld)
2686 offStartDataOld = offStartBlock;
2687 cBlocksAllocated++;
2688 }
2689 }
2690
2691 if ( offStartDataOld != offStartDataNew
2692 && cBlocksAllocated > 0)
2693 {
2694 /* Calculate how many sectors nee to be relocated. */
2695 uint64_t cbOverlapping = offStartDataNew - offStartDataOld;
2696 unsigned cBlocksReloc = (unsigned)(cbOverlapping / cbBlock);
2697 if (cbOverlapping % cbBlock)
2698 cBlocksReloc++;
2699
2700 cBlocksReloc = RT_MIN(cBlocksReloc, cBlocksAllocated);
2701 offStartDataNew = offStartDataOld;
2702
2703 /* Do the relocation. */
2704 LogFlow(("Relocating %u blocks\n", cBlocksReloc));
2705
2706 /*
2707 * Get the blocks we need to relocate first, they are appended to the end
2708 * of the image.
2709 */
2710 void *pvBuf = NULL, *pvZero = NULL;
2711 do
2712 {
2713 /* Allocate data buffer. */
2714 pvBuf = RTMemAllocZ(cbBlock);
2715 if (!pvBuf)
2716 {
2717 rc = VERR_NO_MEMORY;
2718 break;
2719 }
2720
2721 /* Allocate buffer for overwriting with zeroes. */
2722 pvZero = RTMemAllocZ(cbBlock);
2723 if (!pvZero)
2724 {
2725 rc = VERR_NO_MEMORY;
2726 break;
2727 }
2728
2729 for (unsigned i = 0; i < cBlocksReloc; i++)
2730 {
2731 uint32_t uBlock = offStartDataNew / VHD_SECTOR_SIZE;
2732
2733 /* Search the index in the block table. */
2734 for (unsigned idxBlock = 0; idxBlock < cBlocksOld; idxBlock++)
2735 {
2736 if (pImage->pBlockAllocationTable[idxBlock] == uBlock)
2737 {
2738 /* Read data and append to the end of the image. */
2739 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
2740 offStartDataNew, pvBuf, cbBlock);
2741 if (RT_FAILURE(rc))
2742 break;
2743
2744 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2745 pImage->uCurrentEndOfFile, pvBuf, cbBlock);
2746 if (RT_FAILURE(rc))
2747 break;
2748
2749 /* Zero out the old block area. */
2750 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2751 offStartDataNew, pvZero, cbBlock);
2752 if (RT_FAILURE(rc))
2753 break;
2754
2755 /* Update block counter. */
2756 pImage->pBlockAllocationTable[idxBlock] = pImage->uCurrentEndOfFile / VHD_SECTOR_SIZE;
2757
2758 pImage->uCurrentEndOfFile += cbBlock;
2759
2760 /* Continue with the next block. */
2761 break;
2762 }
2763 }
2764
2765 if (RT_FAILURE(rc))
2766 break;
2767
2768 offStartDataNew += cbBlock;
2769 }
2770 } while (0);
2771
2772 if (pvBuf)
2773 RTMemFree(pvBuf);
2774 if (pvZero)
2775 RTMemFree(pvZero);
2776 }
2777
2778 /*
2779 * Relocation done, expand the block array and update the header with
2780 * the new data.
2781 */
2782 if (RT_SUCCESS(rc))
2783 {
2784 uint32_t *paBlocksNew = (uint32_t *)RTMemRealloc(pImage->pBlockAllocationTable, cBlocksNew * sizeof(uint32_t));
2785 if (paBlocksNew)
2786 {
2787 pImage->pBlockAllocationTable = paBlocksNew;
2788
2789 /* Mark the new blocks as unallocated. */
2790 for (unsigned idxBlock = cBlocksOld; idxBlock < cBlocksNew; idxBlock++)
2791 pImage->pBlockAllocationTable[idxBlock] = ~0U;
2792 }
2793 else
2794 rc = VERR_NO_MEMORY;
2795
2796 if (RT_SUCCESS(rc))
2797 {
2798 /* Write the block array before updating the rest. */
2799 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2800 pImage->uBlockAllocationTableOffset,
2801 pImage->pBlockAllocationTable,
2802 cBlocksNew * sizeof(uint32_t));
2803 }
2804
2805 if (RT_SUCCESS(rc))
2806 {
2807 /* Update size and new block count. */
2808 pImage->cBlockAllocationTableEntries = cBlocksNew;
2809 pImage->cbSize = cbSize;
2810
2811 /* Update geometry. */
2812 pImage->PCHSGeometry = *pPCHSGeometry;
2813 pImage->LCHSGeometry = *pLCHSGeometry;
2814 }
2815 }
2816
2817 /* Update header information in base image file. */
2818 pImage->fDynHdrNeedsUpdate = true;
2819 vhdFlushImage(pImage);
2820 }
2821 /* Same size doesn't change the image at all. */
2822
2823 LogFlowFunc(("returns %Rrc\n", rc));
2824 return rc;
2825}
2826
2827/** @copydoc VBOXHDDBACKEND::pfnRepair */
2828static DECLCALLBACK(int) vhdRepair(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
2829 PVDINTERFACE pVDIfsImage, uint32_t fFlags)
2830{
2831 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
2832 int rc;
2833 PVDINTERFACEERROR pIfError;
2834 PVDINTERFACEIOINT pIfIo;
2835 PVDIOSTORAGE pStorage;
2836 uint64_t cbFile;
2837 VHDFooter vhdFooter;
2838 VHDDynamicDiskHeader dynamicDiskHeader;
2839 uint32_t *paBat = NULL;
2840 uint32_t *pu32BlockBitmap = NULL;
2841
2842 pIfIo = VDIfIoIntGet(pVDIfsImage);
2843 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
2844
2845 pIfError = VDIfErrorGet(pVDIfsDisk);
2846
2847 do
2848 {
2849 uint64_t offDynamicDiskHeader = 0;
2850 uint64_t offBat = 0;
2851 uint64_t offFooter = 0;
2852 uint32_t cBatEntries = 0;
2853 bool fDynamic = false;
2854 bool fRepairFooter = false;
2855 bool fRepairBat = false;
2856 bool fRepairDynHeader = false;
2857
2858 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
2859 VDOpenFlagsToFileOpenFlags( fFlags & VD_REPAIR_DRY_RUN
2860 ? VD_OPEN_FLAGS_READONLY
2861 : 0,
2862 false /* fCreate */),
2863 &pStorage);
2864 if (RT_FAILURE(rc))
2865 {
2866 rc = vdIfError(pIfError, rc, RT_SRC_POS, "Failed to open image \"%s\"", pszFilename);
2867 break;
2868 }
2869
2870 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
2871 if (RT_FAILURE(rc))
2872 {
2873 rc = vdIfError(pIfError, rc, RT_SRC_POS, "Failed to query image size");
2874 break;
2875 }
2876
2877 if (cbFile < sizeof(VHDFooter))
2878 {
2879 rc = vdIfError(pIfError, VERR_VD_INVALID_SIZE, RT_SRC_POS,
2880 "Image must be at least %u bytes (got %llu)",
2881 sizeof(VHDFooter), cbFile);
2882 break;
2883 }
2884
2885 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, cbFile - sizeof(VHDFooter),
2886 &vhdFooter, sizeof(VHDFooter));
2887 if (RT_FAILURE(rc))
2888 {
2889 rc = vdIfError(pIfError, rc, RT_SRC_POS, "Failed to read footer of image");
2890 break;
2891 }
2892
2893 if (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0)
2894 {
2895 /* Dynamic images have a backup at the beginning of the image. */
2896 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0,
2897 &vhdFooter, sizeof(VHDFooter));
2898 if (RT_FAILURE(rc))
2899 {
2900 rc = vdIfError(pIfError, rc, RT_SRC_POS, "Failed to read header of image");
2901 break;
2902 }
2903
2904 /*
2905 * Check for the header, if this fails the image is either completely corrupted
2906 * and impossible to repair or in another format.
2907 */
2908 if (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0)
2909 {
2910 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
2911 "No valid VHD structures found");
2912 break;
2913 }
2914 else
2915 vdIfErrorMessage(pIfError, "Missing footer structure, using backup\n");
2916
2917 /* Remember to fix the footer structure. */
2918 fRepairFooter = true;
2919 }
2920
2921 offFooter = cbFile - sizeof(VHDFooter);
2922
2923 /* Verify that checksums match. */
2924 uint32_t u32ChkSumOld = RT_BE2H_U32(vhdFooter.Checksum);
2925 vhdFooter.Checksum = 0;
2926 uint32_t u32ChkSum = vhdChecksum(&vhdFooter, sizeof(VHDFooter));
2927
2928 vhdFooter.Checksum = RT_H2BE_U32(u32ChkSum);
2929
2930 if (u32ChkSumOld != u32ChkSum)
2931 {
2932 vdIfErrorMessage(pIfError, "Checksum is invalid (should be %u got %u), repairing\n",
2933 u32ChkSum, u32ChkSumOld);
2934 fRepairFooter = true;
2935 break;
2936 }
2937
2938 switch (RT_BE2H_U32(vhdFooter.DiskType))
2939 {
2940 case VHD_FOOTER_DISK_TYPE_FIXED:
2941 fDynamic = false;
2942 break;
2943 case VHD_FOOTER_DISK_TYPE_DYNAMIC:
2944 fDynamic = true;
2945 break;
2946 case VHD_FOOTER_DISK_TYPE_DIFFERENCING:
2947 fDynamic = true;
2948 break;
2949 default:
2950 {
2951 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
2952 "VHD image type %u is not supported",
2953 RT_BE2H_U32(vhdFooter.DiskType));
2954 break;
2955 }
2956 }
2957
2958 /* Load and check dynamic disk header if required. */
2959 if (fDynamic)
2960 {
2961 size_t cbBlock;
2962
2963 offDynamicDiskHeader = RT_BE2H_U64(vhdFooter.DataOffset);
2964 if (offDynamicDiskHeader + sizeof(VHDDynamicDiskHeader) > cbFile)
2965 {
2966 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
2967 "VHD image type is not supported");
2968 break;
2969 }
2970
2971 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, offDynamicDiskHeader,
2972 &dynamicDiskHeader, sizeof(VHDDynamicDiskHeader));
2973 if (RT_FAILURE(rc))
2974 {
2975 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
2976 "Failed to read dynamic disk header (at %llu), %Rrc",
2977 offDynamicDiskHeader, rc);
2978 break;
2979 }
2980
2981 /* Verify that checksums match. */
2982 u32ChkSumOld = RT_BE2H_U32(dynamicDiskHeader.Checksum);
2983 dynamicDiskHeader.Checksum = 0;
2984 u32ChkSum = vhdChecksum(&dynamicDiskHeader, sizeof(VHDDynamicDiskHeader));
2985
2986 dynamicDiskHeader.Checksum = RT_H2BE_U32(u32ChkSum);
2987
2988 if (u32ChkSumOld != u32ChkSum)
2989 {
2990 vdIfErrorMessage(pIfError, "Checksum of dynamic disk header is invalid (should be %u got %u), repairing\n",
2991 u32ChkSum, u32ChkSumOld);
2992 fRepairDynHeader = true;
2993 break;
2994 }
2995
2996 /* Read the block allocation table and fix any inconsistencies. */
2997 offBat = RT_BE2H_U64(dynamicDiskHeader.TableOffset);
2998 cBatEntries = RT_BE2H_U32(dynamicDiskHeader.MaxTableEntries);
2999 cbBlock = RT_BE2H_U32(dynamicDiskHeader.BlockSize);
3000 cbBlock += cbBlock / VHD_SECTOR_SIZE / 8;
3001
3002 if (offBat + cBatEntries * sizeof(uint32_t) > cbFile)
3003 {
3004 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3005 "Block allocation table is not inside the image");
3006 break;
3007 }
3008
3009 paBat = (uint32_t *)RTMemAllocZ(cBatEntries * sizeof(uint32_t));
3010 if (!paBat)
3011 {
3012 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3013 "Could not allocate memory for the block allocation table (%u bytes)",
3014 cBatEntries * sizeof(uint32_t));
3015 break;
3016 }
3017
3018 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, offBat, paBat,
3019 cBatEntries * sizeof(uint32_t));
3020 if (RT_FAILURE(rc))
3021 {
3022 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3023 "Could not read block allocation table (at %llu), %Rrc",
3024 offBat, rc);
3025 break;
3026 }
3027
3028 pu32BlockBitmap = (uint32_t *)RTMemAllocZ(RT_ALIGN_Z(cBatEntries / 8, 4));
3029 if (!pu32BlockBitmap)
3030 {
3031 rc = vdIfError(pIfError, VERR_NO_MEMORY, RT_SRC_POS,
3032 "Failed to allocate memory for block bitmap");
3033 break;
3034 }
3035
3036 uint32_t idxMinBlock = UINT32_C(0xffffffff);
3037 for (uint32_t i = 0; i < cBatEntries; i++)
3038 {
3039 paBat[i] = RT_BE2H_U32(paBat[i]);
3040 if (paBat[i] < idxMinBlock)
3041 idxMinBlock = paBat[i];
3042 }
3043
3044 vdIfErrorMessage(pIfError, "First data block at sector %u\n", idxMinBlock);
3045
3046 for (uint32_t i = 0; i < cBatEntries; i++)
3047 {
3048 if (paBat[i] != UINT32_C(0xffffffff))
3049 {
3050 uint64_t offBlock =(uint64_t)paBat[i] * VHD_SECTOR_SIZE;
3051
3052 /*
3053 * Check that the offsets are valid (inside of the image) and
3054 * that there are no double references.
3055 */
3056 if (offBlock + cbBlock > cbFile)
3057 {
3058 vdIfErrorMessage(pIfError, "Entry %u points to invalid offset %llu, clearing\n",
3059 i, offBlock);
3060 paBat[i] = UINT32_C(0xffffffff);
3061 fRepairBat = true;
3062 }
3063 else if (offBlock + cbBlock > offFooter)
3064 {
3065 vdIfErrorMessage(pIfError, "Entry %u intersects with footer, aligning footer\n",
3066 i);
3067 offFooter = offBlock + cbBlock;
3068 fRepairBat = true;
3069 }
3070
3071 if ( paBat[i] != UINT32_C(0xffffffff)
3072 && ASMBitTestAndSet(pu32BlockBitmap, (paBat[i] - idxMinBlock) / (cbBlock / VHD_SECTOR_SIZE)))
3073 {
3074 vdIfErrorMessage(pIfError, "Entry %u points to an already referenced data block, clearing\n",
3075 i);
3076 paBat[i] = UINT32_C(0xffffffff);
3077 fRepairBat = true;
3078 }
3079 }
3080 }
3081 }
3082
3083 /* Write repaired structures now. */
3084 if (!(fRepairBat || fRepairDynHeader || fRepairFooter))
3085 vdIfErrorMessage(pIfError, "VHD image is in a consistent state, no repair required\n");
3086 else if (!(fFlags & VD_REPAIR_DRY_RUN))
3087 {
3088 if (fRepairBat)
3089 {
3090 for (uint32_t i = 0; i < cBatEntries; i++)
3091 paBat[i] = RT_H2BE_U32(paBat[i]);
3092
3093 vdIfErrorMessage(pIfError, "Writing repaired block allocation table...\n");
3094
3095 rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, offBat, paBat,
3096 cBatEntries * sizeof(uint32_t));
3097 if (RT_FAILURE(rc))
3098 {
3099 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3100 "Could not write repaired block allocation table (at %llu), %Rrc",
3101 offBat, rc);
3102 break;
3103 }
3104 }
3105
3106 if (fRepairDynHeader)
3107 {
3108 Assert(fDynamic);
3109
3110 vdIfErrorMessage(pIfError, "Writing repaired dynamic disk header...\n");
3111 rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, offDynamicDiskHeader, &dynamicDiskHeader,
3112 sizeof(VHDDynamicDiskHeader));
3113 if (RT_FAILURE(rc))
3114 {
3115 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3116 "Could not write repaired dynamic disk header (at %llu), %Rrc",
3117 offDynamicDiskHeader, rc);
3118 break;
3119 }
3120 }
3121
3122 if (fRepairFooter)
3123 {
3124 vdIfErrorMessage(pIfError, "Writing repaired Footer...\n");
3125
3126 if (fDynamic)
3127 {
3128 /* Write backup at image beginning. */
3129 rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, 0, &vhdFooter,
3130 sizeof(VHDFooter));
3131 if (RT_FAILURE(rc))
3132 {
3133 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3134 "Could not write repaired backup footer (at %llu), %Rrc",
3135 0, rc);
3136 break;
3137 }
3138 }
3139
3140 rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, offFooter, &vhdFooter,
3141 sizeof(VHDFooter));
3142 if (RT_FAILURE(rc))
3143 {
3144 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3145 "Could not write repaired footer (at %llu), %Rrc",
3146 cbFile - sizeof(VHDFooter), rc);
3147 break;
3148 }
3149 }
3150
3151 vdIfErrorMessage(pIfError, "Corrupted VHD image repaired successfully\n");
3152 }
3153 } while(0);
3154
3155 if (paBat)
3156 RTMemFree(paBat);
3157
3158 if (pu32BlockBitmap)
3159 RTMemFree(pu32BlockBitmap);
3160
3161 if (pStorage)
3162 {
3163 int rc2 = vdIfIoIntFileClose(pIfIo, pStorage);
3164 if (RT_SUCCESS(rc))
3165 rc = rc2; /* Propagate status code only when repairing the image was successful. */
3166 }
3167
3168 LogFlowFunc(("returns %Rrc\n", rc));
3169 return rc;
3170}
3171
3172
3173VBOXHDDBACKEND g_VhdBackend =
3174{
3175 /* pszBackendName */
3176 "VHD",
3177 /* cbSize */
3178 sizeof(VBOXHDDBACKEND),
3179 /* uBackendCaps */
3180 VD_CAP_UUID | VD_CAP_DIFF | VD_CAP_FILE |
3181 VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC |
3182 VD_CAP_ASYNC | VD_CAP_VFS,
3183 /* paFileExtensions */
3184 s_aVhdFileExtensions,
3185 /* paConfigInfo */
3186 NULL,
3187 /* hPlugin */
3188 NIL_RTLDRMOD,
3189 /* pfnCheckIfValid */
3190 vhdCheckIfValid,
3191 /* pfnOpen */
3192 vhdOpen,
3193 /* pfnCreate */
3194 vhdCreate,
3195 /* pfnRename */
3196 vhdRename,
3197 /* pfnClose */
3198 vhdClose,
3199 /* pfnRead */
3200 vhdRead,
3201 /* pfnWrite */
3202 vhdWrite,
3203 /* pfnFlush */
3204 vhdFlush,
3205 /* pfnDiscard */
3206 NULL,
3207 /* pfnGetVersion */
3208 vhdGetVersion,
3209 /* pfnGetSize */
3210 vhdGetSize,
3211 /* pfnGetFileSize */
3212 vhdGetFileSize,
3213 /* pfnGetPCHSGeometry */
3214 vhdGetPCHSGeometry,
3215 /* pfnSetPCHSGeometry */
3216 vhdSetPCHSGeometry,
3217 /* pfnGetLCHSGeometry */
3218 vhdGetLCHSGeometry,
3219 /* pfnSetLCHSGeometry */
3220 vhdSetLCHSGeometry,
3221 /* pfnGetImageFlags */
3222 vhdGetImageFlags,
3223 /* pfnGetOpenFlags */
3224 vhdGetOpenFlags,
3225 /* pfnSetOpenFlags */
3226 vhdSetOpenFlags,
3227 /* pfnGetComment */
3228 vhdGetComment,
3229 /* pfnSetComment */
3230 vhdSetComment,
3231 /* pfnGetUuid */
3232 vhdGetUuid,
3233 /* pfnSetUuid */
3234 vhdSetUuid,
3235 /* pfnGetModificationUuid */
3236 vhdGetModificationUuid,
3237 /* pfnSetModificationUuid */
3238 vhdSetModificationUuid,
3239 /* pfnGetParentUuid */
3240 vhdGetParentUuid,
3241 /* pfnSetParentUuid */
3242 vhdSetParentUuid,
3243 /* pfnGetParentModificationUuid */
3244 vhdGetParentModificationUuid,
3245 /* pfnSetParentModificationUuid */
3246 vhdSetParentModificationUuid,
3247 /* pfnDump */
3248 vhdDump,
3249 /* pfnGetTimeStamp */
3250 vhdGetTimeStamp,
3251 /* pfnGetParentTimeStamp */
3252 vhdGetParentTimeStamp,
3253 /* pfnSetParentTimeStamp */
3254 vhdSetParentTimeStamp,
3255 /* pfnGetParentFilename */
3256 vhdGetParentFilename,
3257 /* pfnSetParentFilename */
3258 vhdSetParentFilename,
3259 /* pfnComposeLocation */
3260 genericFileComposeLocation,
3261 /* pfnComposeName */
3262 genericFileComposeName,
3263 /* pfnCompact */
3264 vhdCompact,
3265 /* pfnResize */
3266 vhdResize,
3267 /* pfnRepair */
3268 vhdRepair
3269};
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