VirtualBox

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

Last change on this file since 72756 was 72577, checked in by vboxsync, 6 years ago

Storage/VHD: Stay within possible limits of the VDH spec for the block allocation table and reject images violating the spec, missing status code check [build fix]

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