VirtualBox

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

Last change on this file since 67954 was 66486, checked in by vboxsync, 7 years ago

Storage/VD: Convert all backends to use the region list callbacks, remove the pfnGetSize and pfnGetSectorSize callbacks because they are covered by the region lists

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