[29250] | 1 | /* $Id: VHD.cpp 66486 2017-04-10 07:23:59Z vboxsync $ */
|
---|
[12116] | 2 | /** @file
|
---|
| 3 | * VHD Disk image, Core Code.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[60608] | 7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
[12116] | 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 |
|
---|
[57358] | 18 |
|
---|
| 19 | /*********************************************************************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *********************************************************************************************************************************/
|
---|
[12116] | 22 | #define LOG_GROUP LOG_GROUP_VD_VHD
|
---|
[33567] | 23 | #include <VBox/vd-plugin.h>
|
---|
[12116] | 24 | #include <VBox/err.h>
|
---|
| 25 |
|
---|
| 26 | #include <VBox/log.h>
|
---|
| 27 | #include <VBox/version.h>
|
---|
[29250] | 28 | #include <iprt/asm.h>
|
---|
[12116] | 29 | #include <iprt/assert.h>
|
---|
[29250] | 30 | #include <iprt/mem.h>
|
---|
[12116] | 31 | #include <iprt/uuid.h>
|
---|
| 32 | #include <iprt/path.h>
|
---|
| 33 | #include <iprt/string.h>
|
---|
| 34 |
|
---|
[50988] | 35 | #include "VDBackends.h"
|
---|
| 36 |
|
---|
[12116] | 37 | #define VHD_RELATIVE_MAX_PATH 512
|
---|
| 38 | #define VHD_ABSOLUTE_MAX_PATH 512
|
---|
| 39 |
|
---|
| 40 | #define VHD_SECTOR_SIZE 512
|
---|
[19685] | 41 | #define VHD_BLOCK_SIZE (2 * _1M)
|
---|
[12116] | 42 |
|
---|
| 43 | /* This is common to all VHD disk types and is located at the end of the image */
|
---|
| 44 | #pragma pack(1)
|
---|
[19685] | 45 | typedef struct VHDFooter
|
---|
| 46 | {
|
---|
[12116] | 47 | char Cookie[8];
|
---|
| 48 | uint32_t Features;
|
---|
| 49 | uint32_t Version;
|
---|
| 50 | uint64_t DataOffset;
|
---|
[58132] | 51 | uint32_t Timestamp;
|
---|
[12116] | 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 |
|
---|
[33540] | 68 | /* this really is spelled with only one n */
|
---|
[12116] | 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)
|
---|
| 93 | typedef 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 |
|
---|
| 102 | typedef 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];
|
---|
[58132] | 112 | uint32_t ParentTimestamp;
|
---|
[12116] | 113 | uint32_t Reserved0;
|
---|
[25963] | 114 | uint16_t ParentUnicodeName[256];
|
---|
[12116] | 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 | */
|
---|
| 127 | typedef struct VHDIMAGE
|
---|
| 128 | {
|
---|
[32536] | 129 | /** Image file name. */
|
---|
[32553] | 130 | const char *pszFilename;
|
---|
[32536] | 131 | /** Opaque storage handle. */
|
---|
[32553] | 132 | PVDIOSTORAGE pStorage;
|
---|
[32536] | 133 |
|
---|
[12116] | 134 | /** Pointer to the per-disk VD interface list. */
|
---|
| 135 | PVDINTERFACE pVDIfsDisk;
|
---|
[28620] | 136 | /** Pointer to the per-image VD interface list. */
|
---|
| 137 | PVDINTERFACE pVDIfsImage;
|
---|
[12116] | 138 | /** Error interface. */
|
---|
[38469] | 139 | PVDINTERFACEERROR pIfError;
|
---|
| 140 | /** I/O interface. */
|
---|
| 141 | PVDINTERFACEIOINT pIfIo;
|
---|
[12116] | 142 |
|
---|
[32536] | 143 | /** Open flags passed by VBoxHDD layer. */
|
---|
[12116] | 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;
|
---|
[32536] | 149 |
|
---|
[12116] | 150 | /** Physical geometry of this image. */
|
---|
[32536] | 151 | VDGEOMETRY PCHSGeometry;
|
---|
[12116] | 152 | /** Logical geometry of this image. */
|
---|
[32536] | 153 | VDGEOMETRY LCHSGeometry;
|
---|
| 154 |
|
---|
[12116] | 155 | /** Image UUID. */
|
---|
| 156 | RTUUID ImageUuid;
|
---|
| 157 | /** Parent image UUID. */
|
---|
| 158 | RTUUID ParentUuid;
|
---|
[32536] | 159 |
|
---|
[12116] | 160 | /** Parent's time stamp at the time of image creation. */
|
---|
[58132] | 161 | uint32_t u32ParentTimestamp;
|
---|
[12116] | 162 | /** Relative path to the parent image. */
|
---|
| 163 | char *pszParentFilename;
|
---|
[32536] | 164 |
|
---|
[12116] | 165 | /** The Block Allocation Table. */
|
---|
| 166 | uint32_t *pBlockAllocationTable;
|
---|
| 167 | /** Number of entries in the table. */
|
---|
| 168 | uint32_t cBlockAllocationTableEntries;
|
---|
[32536] | 169 |
|
---|
[12116] | 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. */
|
---|
[12304] | 185 | uint8_t *pu8Bitmap;
|
---|
[12116] | 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;
|
---|
[66486] | 190 | /** The static region list. */
|
---|
| 191 | VDREGIONLIST RegionList;
|
---|
[12116] | 192 | } VHDIMAGE, *PVHDIMAGE;
|
---|
| 193 |
|
---|
[30555] | 194 | /**
|
---|
| 195 | * Structure tracking the expansion process of the image
|
---|
| 196 | * for async access.
|
---|
| 197 | */
|
---|
| 198 | typedef 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 |
|
---|
[12116] | 239 |
|
---|
[57358] | 240 | /*********************************************************************************************************************************
|
---|
| 241 | * Static Variables *
|
---|
| 242 | *********************************************************************************************************************************/
|
---|
| 243 |
|
---|
[12116] | 244 | /** NULL-terminated array of supported file extensions. */
|
---|
[33524] | 245 | static const VDFILEEXTENSION s_aVhdFileExtensions[] =
|
---|
[12116] | 246 | {
|
---|
[33524] | 247 | {"vhd", VDTYPE_HDD},
|
---|
| 248 | {NULL, VDTYPE_INVALID}
|
---|
[12116] | 249 | };
|
---|
| 250 |
|
---|
| 251 |
|
---|
[57358] | 252 | /*********************************************************************************************************************************
|
---|
| 253 | * Internal Functions *
|
---|
| 254 | *********************************************************************************************************************************/
|
---|
[22966] | 255 |
|
---|
[57358] | 256 |
|
---|
[32536] | 257 | /**
|
---|
[12116] | 258 | * Internal: Compute and update header checksum.
|
---|
| 259 | */
|
---|
| 260 | static uint32_t vhdChecksum(void *pHeader, uint32_t cbSize)
|
---|
| 261 | {
|
---|
[63794] | 262 | uint32_t u32ChkSum = 0;
|
---|
[12116] | 263 | for (uint32_t i = 0; i < cbSize; i++)
|
---|
[63794] | 264 | u32ChkSum += ((unsigned char *)pHeader)[i];
|
---|
| 265 | return ~u32ChkSum;
|
---|
[12116] | 266 | }
|
---|
| 267 |
|
---|
[32536] | 268 | /**
|
---|
| 269 | * Internal: Convert filename to UTF16 with appropriate endianness.
|
---|
| 270 | */
|
---|
| 271 | static int vhdFilenameToUtf16(const char *pszFilename, uint16_t *pu16Buf,
|
---|
| 272 | uint32_t cbBufSize, uint32_t *pcbActualSize,
|
---|
| 273 | bool fBigEndian)
|
---|
[12116] | 274 | {
|
---|
| 275 | int rc;
|
---|
[63794] | 276 | PRTUTF16 pTmp16 = NULL;
|
---|
[12116] | 277 | size_t cTmp16Len;
|
---|
| 278 |
|
---|
[63794] | 279 | rc = RTStrToUtf16(pszFilename, &pTmp16);
|
---|
| 280 | if (RT_SUCCESS(rc))
|
---|
[12116] | 281 | {
|
---|
[63794] | 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;
|
---|
[12116] | 295 | }
|
---|
[25963] | 296 |
|
---|
[63794] | 297 | if (pTmp16)
|
---|
| 298 | RTUtf16Free(pTmp16);
|
---|
[12116] | 299 | return rc;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | /**
|
---|
| 303 | * Internal: Update one locator entry.
|
---|
| 304 | */
|
---|
| 305 | static int vhdLocatorUpdate(PVHDIMAGE pImage, PVHDPLE pLocator, const char *pszFilename)
|
---|
| 306 | {
|
---|
[44673] | 307 | int rc = VINF_SUCCESS;
|
---|
[62794] | 308 | uint32_t cb = 0;
|
---|
| 309 | uint32_t cbMaxLen = RT_BE2H_U32(pLocator->u32DataSpace);
|
---|
[12116] | 310 | void *pvBuf = RTMemTmpAllocZ(cbMaxLen);
|
---|
| 311 | char *pszTmp;
|
---|
| 312 |
|
---|
| 313 | if (!pvBuf)
|
---|
[44671] | 314 | return VERR_NO_MEMORY;
|
---|
[12116] | 315 |
|
---|
| 316 | switch (RT_BE2H_U32(pLocator->u32Code))
|
---|
| 317 | {
|
---|
| 318 | case VHD_PLATFORM_CODE_WI2R:
|
---|
[44671] | 319 | {
|
---|
| 320 | if (RTPathStartsWithRoot(pszFilename))
|
---|
[12116] | 321 | {
|
---|
[44671] | 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 | }
|
---|
[12116] | 337 | }
|
---|
[44671] | 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 | }
|
---|
[52468] | 349 | if (RT_SUCCESS(rc))
|
---|
| 350 | pLocator->u32DataLength = RT_H2BE_U32(cb);
|
---|
[12116] | 351 | break;
|
---|
[44671] | 352 | }
|
---|
[12116] | 353 | case VHD_PLATFORM_CODE_WI2K:
|
---|
| 354 | /* Update plain absolute name. */
|
---|
| 355 | rc = RTPathAbs(pszFilename, (char *)pvBuf, cbMaxLen);
|
---|
[44671] | 356 | if (RT_SUCCESS(rc))
|
---|
[62794] | 357 | {
|
---|
| 358 | cb = (uint32_t)strlen((const char *)pvBuf);
|
---|
| 359 | pLocator->u32DataLength = RT_H2BE_U32(cb);
|
---|
| 360 | }
|
---|
[12116] | 361 | break;
|
---|
| 362 | case VHD_PLATFORM_CODE_W2RU:
|
---|
[44671] | 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);
|
---|
[12116] | 380 | break;
|
---|
| 381 | case VHD_PLATFORM_CODE_W2KU:
|
---|
| 382 | /* Update unicode absolute name. */
|
---|
| 383 | pszTmp = (char*)RTMemTmpAllocZ(cbMaxLen);
|
---|
[33495] | 384 | if (!pszTmp)
|
---|
[12116] | 385 | {
|
---|
| 386 | rc = VERR_NO_MEMORY;
|
---|
[44671] | 387 | break;
|
---|
[12116] | 388 | }
|
---|
| 389 | rc = RTPathAbs(pszFilename, pszTmp, cbMaxLen);
|
---|
| 390 | if (RT_FAILURE(rc))
|
---|
| 391 | {
|
---|
| 392 | RTMemTmpFree(pszTmp);
|
---|
[44671] | 393 | break;
|
---|
[12116] | 394 | }
|
---|
[25963] | 395 | rc = vhdFilenameToUtf16(pszTmp, (uint16_t *)pvBuf, cbMaxLen, &cb, false);
|
---|
[12116] | 396 | RTMemTmpFree(pszTmp);
|
---|
[44671] | 397 | if (RT_SUCCESS(rc))
|
---|
| 398 | pLocator->u32DataLength = RT_H2BE_U32(cb);
|
---|
[12116] | 399 | break;
|
---|
| 400 | default:
|
---|
| 401 | rc = VERR_NOT_IMPLEMENTED;
|
---|
[44671] | 402 | break;
|
---|
[12116] | 403 | }
|
---|
| 404 |
|
---|
[44671] | 405 | if (RT_SUCCESS(rc))
|
---|
[62794] | 406 | {
|
---|
| 407 | Assert(cb > 0);
|
---|
[44671] | 408 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
| 409 | RT_BE2H_U64(pLocator->u64DataOffset),
|
---|
[62794] | 410 | pvBuf, cb);
|
---|
| 411 | }
|
---|
[44671] | 412 |
|
---|
[12116] | 413 | if (pvBuf)
|
---|
| 414 | RTMemTmpFree(pvBuf);
|
---|
| 415 | return rc;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | /**
|
---|
| 419 | * Internal: Update dynamic disk header from VHDIMAGE.
|
---|
| 420 | */
|
---|
| 421 | static int vhdDynamicHeaderUpdate(PVHDIMAGE pImage)
|
---|
| 422 | {
|
---|
| 423 | VHDDynamicDiskHeader ddh;
|
---|
| 424 | int rc, i;
|
---|
| 425 |
|
---|
| 426 | if (!pImage)
|
---|
[15366] | 427 | return VERR_VD_NOT_OPENED;
|
---|
[12116] | 428 |
|
---|
[38469] | 429 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
|
---|
[44233] | 430 | pImage->u64DataOffset, &ddh, sizeof(ddh));
|
---|
[12116] | 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)
|
---|
[15366] | 434 | return VERR_VD_VHD_INVALID_HEADER;
|
---|
[19685] | 435 |
|
---|
[12116] | 436 | uint32_t u32Checksum = RT_BE2H_U32(ddh.Checksum);
|
---|
| 437 | ddh.Checksum = 0;
|
---|
| 438 | if (u32Checksum != vhdChecksum(&ddh, sizeof(ddh)))
|
---|
[15366] | 439 | return VERR_VD_VHD_INVALID_HEADER;
|
---|
[19685] | 440 |
|
---|
[12116] | 441 | /* Update parent's timestamp. */
|
---|
[58132] | 442 | ddh.ParentTimestamp = RT_H2BE_U32(pImage->u32ParentTimestamp);
|
---|
[12116] | 443 | /* Update parent's filename. */
|
---|
[20473] | 444 | if (pImage->pszParentFilename)
|
---|
| 445 | {
|
---|
| 446 | rc = vhdFilenameToUtf16(RTPathFilename(pImage->pszParentFilename),
|
---|
[25963] | 447 | ddh.ParentUnicodeName, sizeof(ddh.ParentUnicodeName) - 1, NULL, true);
|
---|
[20473] | 448 | if (RT_FAILURE(rc))
|
---|
| 449 | return rc;
|
---|
| 450 | }
|
---|
| 451 |
|
---|
[12116] | 452 | /* Update parent's locators. */
|
---|
| 453 | for (i = 0; i < VHD_MAX_LOCATOR_ENTRIES; i++)
|
---|
| 454 | {
|
---|
| 455 | /* Skip empty locators */
|
---|
[51091] | 456 | if ( ddh.ParentLocatorEntry[i].u32Code != RT_H2BE_U32(VHD_PLATFORM_CODE_NONE)
|
---|
| 457 | && pImage->pszParentFilename)
|
---|
[12116] | 458 | {
|
---|
[51091] | 459 | rc = vhdLocatorUpdate(pImage, &ddh.ParentLocatorEntry[i], pImage->pszParentFilename);
|
---|
| 460 | if (RT_FAILURE(rc))
|
---|
| 461 | return rc;
|
---|
[12116] | 462 | }
|
---|
| 463 | }
|
---|
| 464 | /* Update parent's UUID */
|
---|
| 465 | memcpy(ddh.ParentUuid, pImage->ParentUuid.au8, sizeof(ddh.ParentUuid));
|
---|
[31921] | 466 |
|
---|
| 467 | /* Update data offset and number of table entries. */
|
---|
| 468 | ddh.MaxTableEntries = RT_H2BE_U32(pImage->cBlockAllocationTableEntries);
|
---|
| 469 |
|
---|
[12116] | 470 | ddh.Checksum = 0;
|
---|
| 471 | ddh.Checksum = RT_H2BE_U32(vhdChecksum(&ddh, sizeof(ddh)));
|
---|
[38469] | 472 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
[44233] | 473 | pImage->u64DataOffset, &ddh, sizeof(ddh));
|
---|
[32536] | 474 | return rc;
|
---|
| 475 | }
|
---|
[12116] | 476 |
|
---|
[32536] | 477 | /**
|
---|
| 478 | * Internal: Update the VHD footer.
|
---|
| 479 | */
|
---|
| 480 | static int vhdUpdateFooter(PVHDIMAGE pImage)
|
---|
| 481 | {
|
---|
| 482 | int rc = VINF_SUCCESS;
|
---|
[19736] | 483 |
|
---|
[32536] | 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)
|
---|
[38469] | 494 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0,
|
---|
[44233] | 495 | &pImage->vhdFooterCopy, sizeof(VHDFooter));
|
---|
[32536] | 496 |
|
---|
| 497 | if (RT_SUCCESS(rc))
|
---|
[38469] | 498 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
| 499 | pImage->uCurrentEndOfFile, &pImage->vhdFooterCopy,
|
---|
[44233] | 500 | sizeof(VHDFooter));
|
---|
[32536] | 501 |
|
---|
[12116] | 502 | return rc;
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[30555] | 505 | /**
|
---|
[32536] | 506 | * Internal. Flush image data to disk.
|
---|
| 507 | */
|
---|
| 508 | static 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 | */
|
---|
[38469] | 535 | vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->uBlockAllocationTableOffset,
|
---|
[44233] | 536 | pBlockAllocationTableToWrite, cbBlockAllocationTableToWrite);
|
---|
[32536] | 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))
|
---|
[38469] | 546 | rc = vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
|
---|
[32536] | 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 | */
|
---|
| 555 | static 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 |
|
---|
[46613] | 569 | rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
|
---|
[32536] | 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)
|
---|
[46613] | 590 | vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
|
---|
[32536] | 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 |
|
---|
[58132] | 600 | static uint32_t vhdRtTime2VhdTime(PCRTTIMESPEC pRtTimestamp)
|
---|
[32536] | 601 | {
|
---|
[58132] | 602 | uint64_t u64Seconds = RTTimeSpecGetSeconds(pRtTimestamp);
|
---|
[32536] | 603 | return (uint32_t)(u64Seconds - VHD_TO_UNIX_EPOCH_SECONDS);
|
---|
| 604 | }
|
---|
| 605 |
|
---|
[58132] | 606 | static void vhdTime2RtTime(PRTTIMESPEC pRtTimestamp, uint32_t u32VhdTimestamp)
|
---|
[32536] | 607 | {
|
---|
[58132] | 608 | RTTimeSpecSetSeconds(pRtTimestamp, VHD_TO_UNIX_EPOCH_SECONDS + u32VhdTimestamp);
|
---|
[32536] | 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 | */
|
---|
| 615 | DECLINLINE(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 | /**
|
---|
[30555] | 625 | * Internal: called when the async expansion process completed (failure or success).
|
---|
| 626 | * Will do the necessary rollback if an error occurred.
|
---|
| 627 | */
|
---|
| 628 | static 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;
|
---|
[12116] | 633 |
|
---|
[30555] | 634 | /* Quick path, check if everything succeeded. */
|
---|
| 635 | if (fFlags == VHDIMAGEEXPAND_ALL_SUCCESS)
|
---|
| 636 | {
|
---|
[52358] | 637 | pImage->pBlockAllocationTable[pExpand->idxBatAllocated] = RT_BE2H_U32(pExpand->idxBlockBe);
|
---|
[30555] | 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. */
|
---|
[31379] | 649 | pImage->pBlockAllocationTable[pExpand->idxBatAllocated] = ~0U;
|
---|
[30555] | 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 | {
|
---|
[44233] | 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);
|
---|
[30555] | 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. */
|
---|
[38469] | 668 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
|
---|
| 669 | pExpand->cbEofOld + sizeof(VHDFooter));
|
---|
[30555] | 670 | AssertRC(rc);
|
---|
| 671 |
|
---|
| 672 | pImage->uCurrentEndOfFile = pExpand->cbEofOld;
|
---|
[44233] | 673 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
| 674 | pImage->uCurrentEndOfFile,
|
---|
| 675 | &pImage->vhdFooterCopy, sizeof(VHDFooter),
|
---|
| 676 | pIoCtx, NULL, NULL);
|
---|
[30555] | 677 | fIoInProgress |= rc == VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | return fIoInProgress ? VERR_VD_ASYNC_IO_IN_PROGRESS : rc;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
[32536] | 683 | static int vhdAsyncExpansionStepCompleted(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq, unsigned iStep)
|
---|
[30555] | 684 | {
|
---|
[32536] | 685 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[30555] | 686 | PVHDIMAGEEXPAND pExpand = (PVHDIMAGEEXPAND)pvUser;
|
---|
| 687 |
|
---|
[32536] | 688 | LogFlowFunc(("pBackendData=%#p pIoCtx=%#p pvUser=%#p rcReq=%Rrc iStep=%u\n",
|
---|
| 689 | pBackendData, pIoCtx, pvUser, rcReq, iStep));
|
---|
[30555] | 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 |
|
---|
[57388] | 702 | static DECLCALLBACK(int) vhdAsyncExpansionDataBlockBitmapComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
|
---|
[30555] | 703 | {
|
---|
[32536] | 704 | return vhdAsyncExpansionStepCompleted(pBackendData, pIoCtx, pvUser, rcReq, VHDIMAGEEXPAND_BLOCKBITMAP_STATUS_SHIFT);
|
---|
[30555] | 705 | }
|
---|
| 706 |
|
---|
[57388] | 707 | static DECLCALLBACK(int) vhdAsyncExpansionDataComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
|
---|
[30555] | 708 | {
|
---|
[32536] | 709 | return vhdAsyncExpansionStepCompleted(pBackendData, pIoCtx, pvUser, rcReq, VHDIMAGEEXPAND_USERBLOCK_STATUS_SHIFT);
|
---|
[30555] | 710 | }
|
---|
| 711 |
|
---|
[57388] | 712 | static DECLCALLBACK(int) vhdAsyncExpansionBatUpdateComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
|
---|
[30555] | 713 | {
|
---|
[32536] | 714 | return vhdAsyncExpansionStepCompleted(pBackendData, pIoCtx, pvUser, rcReq, VHDIMAGEEXPAND_BAT_STATUS_SHIFT);
|
---|
[30555] | 715 | }
|
---|
| 716 |
|
---|
[57388] | 717 | static DECLCALLBACK(int) vhdAsyncExpansionFooterUpdateComplete(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
|
---|
[30555] | 718 | {
|
---|
[32536] | 719 | return vhdAsyncExpansionStepCompleted(pBackendData, pIoCtx, pvUser, rcReq, VHDIMAGEEXPAND_FOOTER_STATUS_SHIFT);
|
---|
[30555] | 720 | }
|
---|
| 721 |
|
---|
[32536] | 722 | static 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 | */
|
---|
[38469] | 735 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, uDynamicDiskHeaderOffset,
|
---|
[44233] | 736 | &vhdDynamicDiskHeader, sizeof(VHDDynamicDiskHeader));
|
---|
[33131] | 737 | if (memcmp(vhdDynamicDiskHeader.Cookie, VHD_DYNAMIC_DISK_HEADER_COOKIE, VHD_DYNAMIC_DISK_HEADER_COOKIE_SIZE))
|
---|
[32536] | 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;
|
---|
[38469] | 775 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
|
---|
| 776 | uBlockAllocationTableOffset, pBlockAllocationTable,
|
---|
[44233] | 777 | pImage->cBlockAllocationTableEntries * sizeof(uint32_t));
|
---|
[32536] | 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)
|
---|
[33478] | 785 | {
|
---|
| 786 | RTMemFree(pBlockAllocationTable);
|
---|
[32536] | 787 | return VERR_NO_MEMORY;
|
---|
[33478] | 788 | }
|
---|
[32536] | 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 |
|
---|
[12116] | 801 | static int vhdOpenImage(PVHDIMAGE pImage, unsigned uOpenFlags)
|
---|
| 802 | {
|
---|
| 803 | uint64_t FileSize;
|
---|
| 804 | VHDFooter vhdFooter;
|
---|
| 805 |
|
---|
| 806 | pImage->uOpenFlags = uOpenFlags;
|
---|
| 807 |
|
---|
[38469] | 808 | pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
|
---|
| 809 | pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
|
---|
| 810 | AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
|
---|
[12116] | 811 |
|
---|
| 812 | /*
|
---|
| 813 | * Open the image.
|
---|
| 814 | */
|
---|
[38469] | 815 | int rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
|
---|
| 816 | VDOpenFlagsToFileOpenFlags(uOpenFlags,
|
---|
| 817 | false /* fCreate */),
|
---|
| 818 | &pImage->pStorage);
|
---|
[12116] | 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 |
|
---|
[38469] | 826 | rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &FileSize);
|
---|
[12116] | 827 | pImage->uCurrentEndOfFile = FileSize - sizeof(VHDFooter);
|
---|
| 828 |
|
---|
[38469] | 829 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, pImage->uCurrentEndOfFile,
|
---|
[44233] | 830 | &vhdFooter, sizeof(VHDFooter));
|
---|
[43787] | 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,
|
---|
[44233] | 840 | &vhdFooter, sizeof(VHDFooter));
|
---|
[43787] | 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 | }
|
---|
[12116] | 850 |
|
---|
[43787] | 851 | if (RT_FAILURE(rc))
|
---|
| 852 | return rc;
|
---|
| 853 |
|
---|
[12116] | 854 | switch (RT_BE2H_U32(vhdFooter.DiskType))
|
---|
| 855 | {
|
---|
| 856 | case VHD_FOOTER_DISK_TYPE_FIXED:
|
---|
[38469] | 857 | pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED;
|
---|
[12116] | 858 | break;
|
---|
| 859 | case VHD_FOOTER_DISK_TYPE_DYNAMIC:
|
---|
[38469] | 860 | pImage->uImageFlags &= ~VD_IMAGE_FLAGS_FIXED;
|
---|
[12116] | 861 | break;
|
---|
| 862 | case VHD_FOOTER_DISK_TYPE_DIFFERENCING:
|
---|
[38469] | 863 | pImage->uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
| 864 | pImage->uImageFlags &= ~VD_IMAGE_FLAGS_FIXED;
|
---|
[12116] | 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);
|
---|
[14321] | 891 | LogFlowFunc(("DataOffset=%llu\n", pImage->u64DataOffset));
|
---|
[12116] | 892 |
|
---|
[17970] | 893 | if (!(pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
[12116] | 894 | rc = vhdLoadDynamicDisk(pImage, pImage->u64DataOffset);
|
---|
| 895 |
|
---|
[66486] | 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
|
---|
[32536] | 911 | vhdFreeImage(pImage, false);
|
---|
[12116] | 912 | return rc;
|
---|
| 913 | }
|
---|
| 914 |
|
---|
[32536] | 915 | /**
|
---|
| 916 | * Internal: Checks if a sector in the block bitmap is set
|
---|
| 917 | */
|
---|
| 918 | DECLINLINE(bool) vhdBlockBitmapSectorContainsData(PVHDIMAGE pImage, uint32_t cBlockBitmapEntry)
|
---|
[12116] | 919 | {
|
---|
[32536] | 920 | uint32_t iBitmap = (cBlockBitmapEntry / 8); /* Byte in the block bitmap. */
|
---|
[12116] | 921 |
|
---|
[32536] | 922 | /*
|
---|
| 923 | * The index of the bit in the byte of the data block bitmap.
|
---|
[33540] | 924 | * The most significant bit stands for a lower sector number.
|
---|
[32536] | 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 |
|
---|
[52277] | 932 | return ((*puBitmap) & RT_BIT(iBitInByte)) != 0;
|
---|
[32536] | 933 | }
|
---|
| 934 |
|
---|
| 935 | /**
|
---|
| 936 | * Internal: Sets the given sector in the sector bitmap.
|
---|
| 937 | */
|
---|
| 938 | DECLINLINE(bool) vhdBlockBitmapSectorSet(PVHDIMAGE pImage, uint8_t *pu8Bitmap, uint32_t cBlockBitmapEntry)
|
---|
| 939 | {
|
---|
[62753] | 940 | RT_NOREF1(pImage);
|
---|
[32536] | 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 |
|
---|
[52277] | 953 | bool fClear = ((*puBitmap) & RT_BIT(iBitInByte)) == 0;
|
---|
| 954 | *puBitmap |= RT_BIT(iBitInByte);
|
---|
| 955 | return fClear;
|
---|
[32536] | 956 | }
|
---|
| 957 |
|
---|
| 958 | /**
|
---|
| 959 | * Internal: Derive drive geometry from its size.
|
---|
| 960 | */
|
---|
| 961 | static 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)
|
---|
[12116] | 967 | {
|
---|
[32536] | 968 | /* ATA disks limited to 127 GB. */
|
---|
| 969 | u64TotalSectors = 65535 * 16 * 255;
|
---|
[12116] | 970 | }
|
---|
| 971 |
|
---|
[32536] | 972 | if (u64TotalSectors >= 65535 * 16 * 63)
|
---|
[12116] | 973 | {
|
---|
[32536] | 974 | u32SectorsPerTrack = 255;
|
---|
| 975 | u32Heads = 16;
|
---|
| 976 | u32CylinderTimesHeads = u64TotalSectors / u32SectorsPerTrack;
|
---|
[12116] | 977 | }
|
---|
[32536] | 978 | else
|
---|
| 979 | {
|
---|
| 980 | u32SectorsPerTrack = 17;
|
---|
| 981 | u32CylinderTimesHeads = u64TotalSectors / u32SectorsPerTrack;
|
---|
[12116] | 982 |
|
---|
[32536] | 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;
|
---|
[12116] | 1008 | }
|
---|
| 1009 |
|
---|
[32536] | 1010 |
|
---|
| 1011 | static uint32_t vhdAllocateParentLocators(PVHDIMAGE pImage, VHDDynamicDiskHeader *pDDH, uint64_t u64Offset)
|
---|
[12116] | 1012 | {
|
---|
[62753] | 1013 | RT_NOREF1(pImage);
|
---|
[32536] | 1014 | PVHDPLE pLocator = pDDH->ParentLocatorEntry;
|
---|
[44671] | 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));
|
---|
[32536] | 1026 | pLocator->u64DataOffset = RT_H2BE_U64(u64Offset);
|
---|
| 1027 | pLocator++;
|
---|
[44671] | 1028 | u64Offset += VHD_ABSOLUTE_MAX_PATH * sizeof(RTUTF16);
|
---|
[32536] | 1029 | /* Unicode relative Windows path. */
|
---|
| 1030 | pLocator->u32Code = RT_H2BE_U32(VHD_PLATFORM_CODE_W2RU);
|
---|
[44671] | 1031 | pLocator->u32DataSpace = RT_H2BE_U32(VHD_RELATIVE_MAX_PATH * sizeof(RTUTF16));
|
---|
[32536] | 1032 | pLocator->u64DataOffset = RT_H2BE_U64(u64Offset);
|
---|
| 1033 | u64Offset += VHD_RELATIVE_MAX_PATH * sizeof(RTUTF16);
|
---|
[44671] | 1034 | return u64Offset;
|
---|
[32536] | 1035 | }
|
---|
[12116] | 1036 |
|
---|
[32536] | 1037 | /**
|
---|
| 1038 | * Internal: Additional code for dynamic VHD image creation.
|
---|
| 1039 | */
|
---|
| 1040 | static int vhdCreateDynamicImage(PVHDIMAGE pImage, uint64_t cbSize)
|
---|
| 1041 | {
|
---|
| 1042 | int rc;
|
---|
| 1043 | VHDDynamicDiskHeader DynamicDiskHeader;
|
---|
| 1044 | uint32_t u32BlockAllocationTableSectors;
|
---|
| 1045 | void *pvTmp = NULL;
|
---|
[12116] | 1046 |
|
---|
[32536] | 1047 | memset(&DynamicDiskHeader, 0, sizeof(DynamicDiskHeader));
|
---|
[12116] | 1048 |
|
---|
[32536] | 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;
|
---|
[19685] | 1053 | pImage->cDataBlockBitmapSectors = pImage->cbDataBlockBitmap / VHD_SECTOR_SIZE;
|
---|
[32536] | 1054 | /* Align to sector boundary */
|
---|
[26995] | 1055 | if (pImage->cbDataBlockBitmap % VHD_SECTOR_SIZE > 0)
|
---|
| 1056 | pImage->cDataBlockBitmapSectors++;
|
---|
[32536] | 1057 | pImage->pu8Bitmap = vhdBlockBitmapAllocate(pImage);
|
---|
[12304] | 1058 | if (!pImage->pu8Bitmap)
|
---|
[38469] | 1059 | return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("VHD: cannot allocate memory for bitmap storage"));
|
---|
[12116] | 1060 |
|
---|
[32536] | 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;
|
---|
[12116] | 1065 | pImage->pBlockAllocationTable = (uint32_t *)RTMemAllocZ(pImage->cBlockAllocationTableEntries * sizeof(uint32_t));
|
---|
| 1066 | if (!pImage->pBlockAllocationTable)
|
---|
[38469] | 1067 | return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("VHD: cannot allocate memory for BAT"));
|
---|
[12116] | 1068 |
|
---|
[32536] | 1069 | for (unsigned i = 0; i < pImage->cBlockAllocationTableEntries; i++)
|
---|
| 1070 | {
|
---|
| 1071 | pImage->pBlockAllocationTable[i] = 0xFFFFFFFF; /* It is actually big endian. */
|
---|
| 1072 | }
|
---|
[12116] | 1073 |
|
---|
[32536] | 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;
|
---|
[12116] | 1080 |
|
---|
[32536] | 1081 | /* Set dynamic image size. */
|
---|
| 1082 | pvTmp = RTMemTmpAllocZ(pImage->uCurrentEndOfFile + sizeof(VHDFooter));
|
---|
| 1083 | if (!pvTmp)
|
---|
[38469] | 1084 | return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("VHD: cannot set the file size for '%s'"), pImage->pszFilename);
|
---|
[12116] | 1085 |
|
---|
[38469] | 1086 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0, pvTmp,
|
---|
[44233] | 1087 | pImage->uCurrentEndOfFile + sizeof(VHDFooter));
|
---|
[32536] | 1088 | if (RT_FAILURE(rc))
|
---|
| 1089 | {
|
---|
| 1090 | RTMemTmpFree(pvTmp);
|
---|
[38469] | 1091 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot set the file size for '%s'"), pImage->pszFilename);
|
---|
[32536] | 1092 | }
|
---|
[12116] | 1093 |
|
---|
[32536] | 1094 | RTMemTmpFree(pvTmp);
|
---|
[12116] | 1095 |
|
---|
[32536] | 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 |
|
---|
[38469] | 1107 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VHDFooter),
|
---|
[44233] | 1108 | &DynamicDiskHeader, sizeof(DynamicDiskHeader));
|
---|
[12116] | 1109 | if (RT_FAILURE(rc))
|
---|
[38469] | 1110 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot write dynamic disk header to image '%s'"), pImage->pszFilename);
|
---|
[12116] | 1111 |
|
---|
[32536] | 1112 | /* Write BAT. */
|
---|
[38469] | 1113 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->uBlockAllocationTableOffset,
|
---|
| 1114 | pImage->pBlockAllocationTable,
|
---|
[44233] | 1115 | pImage->cBlockAllocationTableEntries * sizeof(uint32_t));
|
---|
[12116] | 1116 | if (RT_FAILURE(rc))
|
---|
[38469] | 1117 | return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot write BAT to image '%s'"), pImage->pszFilename);
|
---|
[12116] | 1118 |
|
---|
[12304] | 1119 | return rc;
|
---|
[12116] | 1120 | }
|
---|
| 1121 |
|
---|
[32536] | 1122 | /**
|
---|
| 1123 | * Internal: The actual code for VHD image creation, both fixed and dynamic.
|
---|
| 1124 | */
|
---|
| 1125 | static int vhdCreateImage(PVHDIMAGE pImage, uint64_t cbSize,
|
---|
| 1126 | unsigned uImageFlags, const char *pszComment,
|
---|
| 1127 | PCVDGEOMETRY pPCHSGeometry,
|
---|
| 1128 | PCVDGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
|
---|
| 1129 | unsigned uOpenFlags,
|
---|
[63794] | 1130 | PVDINTERFACEPROGRESS pIfProgress,
|
---|
[32536] | 1131 | unsigned uPercentStart, unsigned uPercentSpan)
|
---|
[12116] | 1132 | {
|
---|
[62753] | 1133 | RT_NOREF3(pszComment, pPCHSGeometry, pLCHSGeometry);
|
---|
[32536] | 1134 | VHDFooter Footer;
|
---|
| 1135 | RTTIMESPEC now;
|
---|
[12116] | 1136 |
|
---|
[32536] | 1137 | pImage->uOpenFlags = uOpenFlags;
|
---|
| 1138 | pImage->uImageFlags = uImageFlags;
|
---|
[38469] | 1139 | pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
|
---|
[12116] | 1140 |
|
---|
[63794] | 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);
|
---|
[12116] | 1151 |
|
---|
[63794] | 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);
|
---|
[32536] | 1160 | #ifdef RT_OS_DARWIN
|
---|
[63794] | 1161 | Footer.CreatorOS = RT_H2BE_U32(0x4D616320); /* "Mac " */
|
---|
[32536] | 1162 | #else /* Virtual PC supports only two platforms atm, so everything else will be Wi2k. */
|
---|
[63794] | 1163 | Footer.CreatorOS = RT_H2BE_U32(0x5769326B); /* "Wi2k" */
|
---|
[32536] | 1164 | #endif
|
---|
[63794] | 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;
|
---|
[32536] | 1172 |
|
---|
[63794] | 1173 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
[12116] | 1174 | {
|
---|
[63794] | 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),
|
---|
[63811] | 1184 | 0 /* fFlags */, pIfProgress,
|
---|
[63794] | 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);
|
---|
[12116] | 1188 | }
|
---|
[63794] | 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);
|
---|
[12116] | 1211 |
|
---|
[63794] | 1212 | rc = vhdCreateDynamicImage(pImage, cbSize);
|
---|
| 1213 | }
|
---|
[12116] | 1214 |
|
---|
[63794] | 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)));
|
---|
[12116] | 1221 |
|
---|
[63794] | 1222 | pImage->vhdFooterCopy = Footer;
|
---|
[12116] | 1223 |
|
---|
[63794] | 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);
|
---|
[12116] | 1240 | }
|
---|
| 1241 | }
|
---|
[63794] | 1242 | else
|
---|
| 1243 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VHD: cannot create image '%s'"), pImage->pszFilename);
|
---|
[12116] | 1244 |
|
---|
[63794] | 1245 | if (RT_SUCCESS(rc))
|
---|
| 1246 | vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
|
---|
[32536] | 1247 |
|
---|
[66486] | 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
|
---|
[32536] | 1263 | vhdFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
|
---|
[12116] | 1264 | return rc;
|
---|
| 1265 | }
|
---|
| 1266 |
|
---|
[32536] | 1267 |
|
---|
[63802] | 1268 | /** @interface_method_impl{VDIMAGEBACKEND,pfnProbe} */
|
---|
| 1269 | static DECLCALLBACK(int) vhdProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
|
---|
| 1270 | PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
|
---|
[12116] | 1271 | {
|
---|
[62753] | 1272 | RT_NOREF1(pVDIfsDisk);
|
---|
[32536] | 1273 | LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
|
---|
| 1274 | PVDIOSTORAGE pStorage;
|
---|
[38469] | 1275 | PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
|
---|
| 1276 | AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
|
---|
[12116] | 1277 |
|
---|
[63794] | 1278 | int rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
|
---|
| 1279 | VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
|
---|
| 1280 | false /* fCreate */),
|
---|
| 1281 | &pStorage);
|
---|
| 1282 | if (RT_SUCCESS(rc))
|
---|
[12116] | 1283 | {
|
---|
[63794] | 1284 | uint64_t cbFile;
|
---|
[32536] | 1285 |
|
---|
[63794] | 1286 | rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
|
---|
| 1287 | if ( RT_SUCCESS(rc)
|
---|
| 1288 | && cbFile >= sizeof(VHDFooter))
|
---|
[43787] | 1289 | {
|
---|
[63794] | 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;
|
---|
[43787] | 1313 | }
|
---|
[63794] | 1314 | else if (RT_SUCCESS(rc))
|
---|
| 1315 | rc = VERR_VD_VHD_INVALID_HEADER;
|
---|
[43787] | 1316 |
|
---|
[63794] | 1317 | vdIfIoIntFileClose(pIfIo, pStorage);
|
---|
[33524] | 1318 | }
|
---|
[12116] | 1319 |
|
---|
[32536] | 1320 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 1321 | return rc;
|
---|
| 1322 | }
|
---|
| 1323 |
|
---|
[63785] | 1324 | /** @interface_method_impl{VDIMAGEBACKEND,pfnOpen} */
|
---|
[57388] | 1325 | static DECLCALLBACK(int) vhdOpen(const char *pszFilename, unsigned uOpenFlags,
|
---|
| 1326 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
| 1327 | VDTYPE enmType, void **ppBackendData)
|
---|
[12116] | 1328 | {
|
---|
[63794] | 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));
|
---|
[32536] | 1333 | int rc = VINF_SUCCESS;
|
---|
[12116] | 1334 |
|
---|
[32536] | 1335 | /* Check open flags. All valid flags are supported. */
|
---|
[63794] | 1336 | AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
|
---|
| 1337 | AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
|
---|
[12116] | 1338 |
|
---|
[66486] | 1339 | PVHDIMAGE pImage = (PVHDIMAGE)RTMemAllocZ(RT_UOFFSETOF(VHDIMAGE, RegionList.aRegions[1]));
|
---|
[63794] | 1340 | if (RT_LIKELY(pImage))
|
---|
[12116] | 1341 | {
|
---|
[63794] | 1342 | pImage->pszFilename = pszFilename;
|
---|
| 1343 | pImage->pStorage = NULL;
|
---|
| 1344 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
| 1345 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
[12116] | 1346 |
|
---|
[63794] | 1347 | rc = vhdOpenImage(pImage, uOpenFlags);
|
---|
| 1348 | if (RT_SUCCESS(rc))
|
---|
| 1349 | *ppBackendData = pImage;
|
---|
| 1350 | else
|
---|
| 1351 | RTMemFree(pImage);
|
---|
[12116] | 1352 | }
|
---|
| 1353 | else
|
---|
[63794] | 1354 | rc = VERR_NO_MEMORY;
|
---|
[12116] | 1355 |
|
---|
[32536] | 1356 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
[12116] | 1357 | return rc;
|
---|
| 1358 | }
|
---|
| 1359 |
|
---|
[63785] | 1360 | /** @interface_method_impl{VDIMAGEBACKEND,pfnCreate} */
|
---|
[57388] | 1361 | static 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)
|
---|
[12116] | 1369 | {
|
---|
[54430] | 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));
|
---|
[63794] | 1372 | int rc;
|
---|
[38469] | 1373 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
[12116] | 1374 |
|
---|
[54430] | 1375 | /* Check the VD container type. */
|
---|
| 1376 | if (enmType != VDTYPE_HDD)
|
---|
[63794] | 1377 | return VERR_VD_INVALID_TYPE;
|
---|
[54430] | 1378 |
|
---|
[32536] | 1379 | /* Check open flags. All valid flags are supported. */
|
---|
[63794] | 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);
|
---|
[63567] | 1385 | /** @todo Check the values of other params */
|
---|
[12116] | 1386 |
|
---|
[66486] | 1387 | PVHDIMAGE pImage = (PVHDIMAGE)RTMemAllocZ(RT_UOFFSETOF(VHDIMAGE, RegionList.aRegions[1]));
|
---|
[63794] | 1388 | if (RT_LIKELY(pImage))
|
---|
[32536] | 1389 | {
|
---|
[63794] | 1390 | pImage->pszFilename = pszFilename;
|
---|
| 1391 | pImage->pStorage = NULL;
|
---|
| 1392 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
| 1393 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
[12116] | 1394 |
|
---|
[63794] | 1395 | /* Get I/O interface. */
|
---|
| 1396 | pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
|
---|
| 1397 | if (RT_LIKELY(VALID_PTR(pImage->pIfIo)))
|
---|
[32536] | 1398 | {
|
---|
[63794] | 1399 | rc = vhdCreateImage(pImage, cbSize, uImageFlags, pszComment,
|
---|
| 1400 | pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags,
|
---|
| 1401 | pIfProgress, uPercentStart, uPercentSpan);
|
---|
| 1402 | if (RT_SUCCESS(rc))
|
---|
[32536] | 1403 | {
|
---|
[63794] | 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;
|
---|
[32536] | 1414 | }
|
---|
| 1415 | }
|
---|
[63794] | 1416 | else
|
---|
| 1417 | rc = VERR_INVALID_PARAMETER;
|
---|
| 1418 |
|
---|
| 1419 | if (RT_FAILURE(rc))
|
---|
| 1420 | RTMemFree(pImage);
|
---|
[12116] | 1421 | }
|
---|
[32536] | 1422 | else
|
---|
[63794] | 1423 | rc = VERR_NO_MEMORY;
|
---|
[12116] | 1424 |
|
---|
[32536] | 1425 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 1426 | return rc;
|
---|
| 1427 | }
|
---|
| 1428 |
|
---|
[63785] | 1429 | /** @interface_method_impl{VDIMAGEBACKEND,pfnRename} */
|
---|
[57388] | 1430 | static DECLCALLBACK(int) vhdRename(void *pBackendData, const char *pszFilename)
|
---|
[12116] | 1431 | {
|
---|
[18964] | 1432 | LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
|
---|
| 1433 | int rc = VINF_SUCCESS;
|
---|
[18965] | 1434 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[18964] | 1435 |
|
---|
| 1436 | /* Check arguments. */
|
---|
[63794] | 1437 | AssertReturn((pImage && pszFilename && *pszFilename), VERR_INVALID_PARAMETER);
|
---|
[18964] | 1438 |
|
---|
[32536] | 1439 | /* Close the image. */
|
---|
| 1440 | rc = vhdFreeImage(pImage, false);
|
---|
[63794] | 1441 | if (RT_SUCCESS(rc))
|
---|
[18964] | 1442 | {
|
---|
[63794] | 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;
|
---|
[18964] | 1449 |
|
---|
[63794] | 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 | }
|
---|
[18964] | 1460 | }
|
---|
| 1461 |
|
---|
| 1462 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
| 1463 | return rc;
|
---|
[12116] | 1464 | }
|
---|
| 1465 |
|
---|
[63785] | 1466 | /** @interface_method_impl{VDIMAGEBACKEND,pfnClose} */
|
---|
[57388] | 1467 | static DECLCALLBACK(int) vhdClose(void *pBackendData, bool fDelete)
|
---|
[13340] | 1468 | {
|
---|
[32536] | 1469 | LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
|
---|
| 1470 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 1471 |
|
---|
[63794] | 1472 | int rc = vhdFreeImage(pImage, fDelete);
|
---|
[13340] | 1473 | RTMemFree(pImage);
|
---|
| 1474 |
|
---|
[32536] | 1475 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 1476 | return rc;
|
---|
| 1477 | }
|
---|
| 1478 |
|
---|
[63785] | 1479 | /** @interface_method_impl{VDIMAGEBACKEND,pfnRead} */
|
---|
[64272] | 1480 | static DECLCALLBACK(int) vhdRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
|
---|
[57388] | 1481 | PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
|
---|
[12116] | 1482 | {
|
---|
| 1483 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 1484 | int rc = VINF_SUCCESS;
|
---|
| 1485 |
|
---|
[64272] | 1486 | LogFlowFunc(("pBackendData=%p uOffset=%#llx pIoCtx=%#p cbToRead=%u pcbActuallyRead=%p\n",
|
---|
| 1487 | pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
|
---|
[12116] | 1488 |
|
---|
[63794] | 1489 | AssertPtr(pImage);
|
---|
| 1490 | Assert(uOffset % 512 == 0);
|
---|
[64272] | 1491 | Assert(cbToRead % 512 == 0);
|
---|
| 1492 | AssertReturn((VALID_PTR(pIoCtx) && cbToRead), VERR_INVALID_PARAMETER);
|
---|
| 1493 | AssertReturn(uOffset + cbToRead <= pImage->cbSize, VERR_INVALID_PARAMETER);
|
---|
[44252] | 1494 |
|
---|
[20797] | 1495 | /*
|
---|
[12116] | 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 | */
|
---|
[19685] | 1503 | uint32_t cBlockAllocationTableEntry = (uOffset / VHD_SECTOR_SIZE) / pImage->cSectorsPerDataBlock;
|
---|
| 1504 | uint32_t cBATEntryIndex = (uOffset / VHD_SECTOR_SIZE) % pImage->cSectorsPerDataBlock;
|
---|
[12116] | 1505 | uint64_t uVhdOffset;
|
---|
| 1506 |
|
---|
[14321] | 1507 | LogFlowFunc(("cBlockAllocationTableEntry=%u cBatEntryIndex=%u\n", cBlockAllocationTableEntry, cBATEntryIndex));
|
---|
| 1508 | LogFlowFunc(("BlockAllocationEntry=%u\n", pImage->pBlockAllocationTable[cBlockAllocationTableEntry]));
|
---|
[12116] | 1509 |
|
---|
| 1510 | /*
|
---|
[38518] | 1511 | * Clip read range to remain in this data block.
|
---|
| 1512 | */
|
---|
[64272] | 1513 | cbToRead = RT_MIN(cbToRead, (pImage->cbDataBlock - (cBATEntryIndex * VHD_SECTOR_SIZE)));
|
---|
[38518] | 1514 |
|
---|
| 1515 | /*
|
---|
[12116] | 1516 | * If the block is not allocated the content of the entry is ~0
|
---|
| 1517 | */
|
---|
| 1518 | if (pImage->pBlockAllocationTable[cBlockAllocationTableEntry] == ~0U)
|
---|
[32536] | 1519 | rc = VERR_VD_BLOCK_FREE;
|
---|
[38519] | 1520 | else
|
---|
[12116] | 1521 | {
|
---|
[38518] | 1522 | uVhdOffset = ((uint64_t)pImage->pBlockAllocationTable[cBlockAllocationTableEntry] + pImage->cDataBlockBitmapSectors + cBATEntryIndex) * VHD_SECTOR_SIZE;
|
---|
[64272] | 1523 | LogFlowFunc(("uVhdOffset=%llu cbToRead=%u\n", uVhdOffset, cbToRead));
|
---|
[12304] | 1524 |
|
---|
[38518] | 1525 | /* Read in the block's bitmap. */
|
---|
[44252] | 1526 | PVDMETAXFER pMetaXfer;
|
---|
| 1527 | rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage,
|
---|
[38518] | 1528 | ((uint64_t)pImage->pBlockAllocationTable[cBlockAllocationTableEntry]) * VHD_SECTOR_SIZE,
|
---|
[44252] | 1529 | pImage->pu8Bitmap, pImage->cbDataBlockBitmap,
|
---|
| 1530 | pIoCtx, &pMetaXfer, NULL, NULL);
|
---|
| 1531 |
|
---|
[38518] | 1532 | if (RT_SUCCESS(rc))
|
---|
[12116] | 1533 | {
|
---|
[38518] | 1534 | uint32_t cSectors = 0;
|
---|
[20695] | 1535 |
|
---|
[44252] | 1536 | vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
|
---|
[38518] | 1537 | if (vhdBlockBitmapSectorContainsData(pImage, cBATEntryIndex))
|
---|
[12304] | 1538 | {
|
---|
[20797] | 1539 | cBATEntryIndex++;
|
---|
[38518] | 1540 | cSectors = 1;
|
---|
[12304] | 1541 |
|
---|
[38518] | 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 | */
|
---|
[64272] | 1547 | while ( (cSectors < (cbToRead / VHD_SECTOR_SIZE))
|
---|
[38518] | 1548 | && vhdBlockBitmapSectorContainsData(pImage, cBATEntryIndex))
|
---|
| 1549 | {
|
---|
| 1550 | cBATEntryIndex++;
|
---|
| 1551 | cSectors++;
|
---|
| 1552 | }
|
---|
[12304] | 1553 |
|
---|
[64272] | 1554 | cbToRead = cSectors * VHD_SECTOR_SIZE;
|
---|
[20695] | 1555 |
|
---|
[64272] | 1556 | LogFlowFunc(("uVhdOffset=%llu cbToRead=%u\n", uVhdOffset, cbToRead));
|
---|
[44252] | 1557 | rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage,
|
---|
[64272] | 1558 | uVhdOffset, pIoCtx, cbToRead);
|
---|
[38518] | 1559 | }
|
---|
| 1560 | else
|
---|
[12304] | 1561 | {
|
---|
[38518] | 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 | */
|
---|
[20797] | 1570 | cBATEntryIndex++;
|
---|
[38518] | 1571 | cSectors = 1;
|
---|
| 1572 |
|
---|
[64272] | 1573 | while ( (cSectors < (cbToRead / VHD_SECTOR_SIZE))
|
---|
[38518] | 1574 | && !vhdBlockBitmapSectorContainsData(pImage, cBATEntryIndex))
|
---|
| 1575 | {
|
---|
| 1576 | cBATEntryIndex++;
|
---|
| 1577 | cSectors++;
|
---|
| 1578 | }
|
---|
| 1579 |
|
---|
[64272] | 1580 | cbToRead = cSectors * VHD_SECTOR_SIZE;
|
---|
| 1581 | LogFunc(("Sectors free: uVhdOffset=%llu cbToRead=%u\n", uVhdOffset, cbToRead));
|
---|
[38518] | 1582 | rc = VERR_VD_BLOCK_FREE;
|
---|
[20695] | 1583 | }
|
---|
[12116] | 1584 | }
|
---|
[38518] | 1585 | else
|
---|
[44252] | 1586 | AssertMsg(rc == VERR_VD_NOT_ENOUGH_METADATA, ("Reading block bitmap failed rc=%Rrc\n", rc));
|
---|
[12116] | 1587 | }
|
---|
| 1588 | }
|
---|
| 1589 | else
|
---|
[64272] | 1590 | rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, uOffset, pIoCtx, cbToRead);
|
---|
[12116] | 1591 |
|
---|
[44252] | 1592 | if (pcbActuallyRead)
|
---|
[64272] | 1593 | *pcbActuallyRead = cbToRead;
|
---|
[12116] | 1594 |
|
---|
[44252] | 1595 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
[12116] | 1596 | return rc;
|
---|
| 1597 | }
|
---|
| 1598 |
|
---|
[63785] | 1599 | /** @interface_method_impl{VDIMAGEBACKEND,pfnWrite} */
|
---|
[64272] | 1600 | static DECLCALLBACK(int) vhdWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
|
---|
[57388] | 1601 | PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
|
---|
| 1602 | size_t *pcbPostRead, unsigned fWrite)
|
---|
[12116] | 1603 | {
|
---|
| 1604 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 1605 | int rc = VINF_SUCCESS;
|
---|
| 1606 |
|
---|
[64272] | 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));
|
---|
[12304] | 1609 |
|
---|
[14321] | 1610 | AssertPtr(pImage);
|
---|
[63794] | 1611 | Assert(!(uOffset % VHD_SECTOR_SIZE));
|
---|
[64272] | 1612 | Assert(!(cbToWrite % VHD_SECTOR_SIZE));
|
---|
| 1613 | AssertReturn((VALID_PTR(pIoCtx) && cbToWrite), VERR_INVALID_PARAMETER);
|
---|
| 1614 | AssertReturn(uOffset + cbToWrite <= pImage->cbSize, VERR_INVALID_PARAMETER);
|
---|
[13571] | 1615 |
|
---|
[12116] | 1616 | if (pImage->pBlockAllocationTable)
|
---|
| 1617 | {
|
---|
| 1618 | /*
|
---|
| 1619 | * Get the data block first.
|
---|
| 1620 | */
|
---|
[19685] | 1621 | uint32_t cSector = uOffset / VHD_SECTOR_SIZE;
|
---|
[12116] | 1622 | uint32_t cBlockAllocationTableEntry = cSector / pImage->cSectorsPerDataBlock;
|
---|
[13571] | 1623 | uint32_t cBATEntryIndex = cSector % pImage->cSectorsPerDataBlock;
|
---|
[12116] | 1624 | uint64_t uVhdOffset;
|
---|
| 1625 |
|
---|
| 1626 | /*
|
---|
[19824] | 1627 | * Clip write range.
|
---|
| 1628 | */
|
---|
[64272] | 1629 | cbToWrite = RT_MIN(cbToWrite, (pImage->cbDataBlock - (cBATEntryIndex * VHD_SECTOR_SIZE)));
|
---|
[19824] | 1630 |
|
---|
| 1631 | /*
|
---|
[12116] | 1632 | * If the block is not allocated the content of the entry is ~0
|
---|
[14910] | 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
|
---|
[19824] | 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.
|
---|
[12116] | 1639 | */
|
---|
| 1640 | if (pImage->pBlockAllocationTable[cBlockAllocationTableEntry] == ~0U)
|
---|
| 1641 | {
|
---|
[19824] | 1642 | /* Check if the block allocation should be suppressed. */
|
---|
[49944] | 1643 | if ( (fWrite & VD_WRITE_NO_ALLOC)
|
---|
[64272] | 1644 | || (cbToWrite != pImage->cbDataBlock))
|
---|
[19824] | 1645 | {
|
---|
| 1646 | *pcbPreRead = cBATEntryIndex * VHD_SECTOR_SIZE;
|
---|
[64272] | 1647 | *pcbPostRead = pImage->cSectorsPerDataBlock * VHD_SECTOR_SIZE - cbToWrite - *pcbPreRead;
|
---|
[20416] | 1648 |
|
---|
| 1649 | if (pcbWriteProcess)
|
---|
[64272] | 1650 | *pcbWriteProcess = cbToWrite;
|
---|
[44252] | 1651 | return VERR_VD_BLOCK_FREE;
|
---|
[19824] | 1652 | }
|
---|
| 1653 |
|
---|
[44252] | 1654 | PVHDIMAGEEXPAND pExpand = (PVHDIMAGEEXPAND)RTMemAllocZ(RT_OFFSETOF(VHDIMAGEEXPAND, au8Bitmap[pImage->cDataBlockBitmapSectors * VHD_SECTOR_SIZE]));
|
---|
| 1655 | bool fIoInProgress = false;
|
---|
[12116] | 1656 |
|
---|
[44252] | 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. */
|
---|
[64272] | 1665 | for (uint32_t iSector = 0; iSector < (cbToWrite / VHD_SECTOR_SIZE); iSector++)
|
---|
[32536] | 1666 | {
|
---|
[44252] | 1667 | /* No need to check for a changed value because this is an initial write. */
|
---|
| 1668 | vhdBlockBitmapSectorSet(pImage, pExpand->au8Bitmap, cBATEntryIndex);
|
---|
| 1669 | cBATEntryIndex++;
|
---|
[32536] | 1670 | }
|
---|
[12116] | 1671 |
|
---|
[44252] | 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 | }
|
---|
[12116] | 1695 |
|
---|
[24122] | 1696 |
|
---|
[44252] | 1697 | /*
|
---|
| 1698 | * Write the new block at the current end of the file.
|
---|
| 1699 | */
|
---|
| 1700 | rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
|
---|
[52358] | 1701 | pImage->uCurrentEndOfFile + (pImage->cDataBlockBitmapSectors + (cSector % pImage->cSectorsPerDataBlock)) * VHD_SECTOR_SIZE,
|
---|
[64272] | 1702 | pIoCtx, cbToWrite,
|
---|
[44252] | 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 | }
|
---|
[12116] | 1716 |
|
---|
[44252] | 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 | }
|
---|
[12116] | 1735 |
|
---|
[44252] | 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;
|
---|
[12116] | 1740 |
|
---|
[44252] | 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
|
---|
[12116] | 1766 | {
|
---|
[44252] | 1767 | /*
|
---|
| 1768 | * Calculate the real offset in the file.
|
---|
| 1769 | */
|
---|
| 1770 | uVhdOffset = ((uint64_t)pImage->pBlockAllocationTable[cBlockAllocationTableEntry] + pImage->cDataBlockBitmapSectors + cBATEntryIndex) * VHD_SECTOR_SIZE;
|
---|
[30555] | 1771 |
|
---|
[44252] | 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))
|
---|
[12304] | 1780 | {
|
---|
[44252] | 1781 | vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
|
---|
[12304] | 1782 |
|
---|
[44252] | 1783 | /* Write data. */
|
---|
| 1784 | rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
|
---|
[64272] | 1785 | uVhdOffset, pIoCtx, cbToWrite,
|
---|
[44252] | 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. */
|
---|
[64272] | 1792 | for (uint32_t iSector = 0; iSector < (cbToWrite / VHD_SECTOR_SIZE); iSector++)
|
---|
[44252] | 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 | }
|
---|
[30555] | 1817 | }
|
---|
[12116] | 1818 | }
|
---|
| 1819 | }
|
---|
| 1820 | else
|
---|
[44252] | 1821 | rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
|
---|
[64272] | 1822 | uOffset, pIoCtx, cbToWrite, NULL, NULL);
|
---|
[12116] | 1823 |
|
---|
| 1824 | if (pcbWriteProcess)
|
---|
[64272] | 1825 | *pcbWriteProcess = cbToWrite;
|
---|
[12116] | 1826 |
|
---|
[14910] | 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 |
|
---|
[12116] | 1832 | return rc;
|
---|
| 1833 | }
|
---|
| 1834 |
|
---|
[63785] | 1835 | /** @interface_method_impl{VDIMAGEBACKEND,pfnFlush} */
|
---|
[57388] | 1836 | static DECLCALLBACK(int) vhdFlush(void *pBackendData, PVDIOCTX pIoCtx)
|
---|
[12116] | 1837 | {
|
---|
[63794] | 1838 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
[12116] | 1839 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 1840 |
|
---|
[44252] | 1841 | /* No need to write anything here. Data is always updated on a write. */
|
---|
[63794] | 1842 | int rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage, pIoCtx, NULL, NULL);
|
---|
| 1843 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
| 1844 | return rc;
|
---|
[32536] | 1845 | }
|
---|
[12116] | 1846 |
|
---|
[63785] | 1847 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetVersion} */
|
---|
[57388] | 1848 | static DECLCALLBACK(unsigned) vhdGetVersion(void *pBackendData)
|
---|
[32536] | 1849 | {
|
---|
| 1850 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
| 1851 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[12116] | 1852 |
|
---|
[63794] | 1853 | AssertPtrReturn(pImage, 0);
|
---|
[12116] | 1854 |
|
---|
[63794] | 1855 | unsigned uVersion = 1; /**< @todo use correct version */
|
---|
[12116] | 1856 |
|
---|
[63794] | 1857 | LogFlowFunc(("returns %u\n", uVersion));
|
---|
| 1858 | return uVersion;
|
---|
[32536] | 1859 | }
|
---|
[12116] | 1860 |
|
---|
[63785] | 1861 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetFileSize} */
|
---|
[57388] | 1862 | static DECLCALLBACK(uint64_t) vhdGetFileSize(void *pBackendData)
|
---|
[12116] | 1863 | {
|
---|
[32536] | 1864 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
[12116] | 1865 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[32536] | 1866 | uint64_t cb = 0;
|
---|
[12116] | 1867 |
|
---|
[63794] | 1868 | AssertPtrReturn(pImage, 0);
|
---|
[12116] | 1869 |
|
---|
[63794] | 1870 | if (pImage->pStorage)
|
---|
[32536] | 1871 | cb = pImage->uCurrentEndOfFile + sizeof(VHDFooter);
|
---|
| 1872 |
|
---|
| 1873 | LogFlowFunc(("returns %lld\n", cb));
|
---|
| 1874 | return cb;
|
---|
[12116] | 1875 | }
|
---|
| 1876 |
|
---|
[63785] | 1877 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetPCHSGeometry} */
|
---|
[57388] | 1878 | static DECLCALLBACK(int) vhdGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
|
---|
[12116] | 1879 | {
|
---|
[32536] | 1880 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
|
---|
[12116] | 1881 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[63794] | 1882 | int rc = VINF_SUCCESS;
|
---|
[12116] | 1883 |
|
---|
[63794] | 1884 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 1885 |
|
---|
[63794] | 1886 | if (pImage->PCHSGeometry.cCylinders)
|
---|
| 1887 | *pPCHSGeometry = pImage->PCHSGeometry;
|
---|
[12116] | 1888 | else
|
---|
[63794] | 1889 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
[32536] | 1890 |
|
---|
[63794] | 1891 | LogFlowFunc(("returns %Rrc (CHS=%u/%u/%u)\n", rc, pImage->PCHSGeometry.cCylinders,
|
---|
| 1892 | pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors));
|
---|
[32536] | 1893 | return rc;
|
---|
[12116] | 1894 | }
|
---|
| 1895 |
|
---|
[63785] | 1896 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetPCHSGeometry} */
|
---|
[57388] | 1897 | static DECLCALLBACK(int) vhdSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
|
---|
[12116] | 1898 | {
|
---|
[63794] | 1899 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
|
---|
| 1900 | pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
[12116] | 1901 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[63794] | 1902 | int rc = VINF_SUCCESS;
|
---|
[12116] | 1903 |
|
---|
[63794] | 1904 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 1905 |
|
---|
[63794] | 1906 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
| 1907 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
| 1908 | else
|
---|
[32536] | 1909 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
| 1910 |
|
---|
| 1911 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 1912 | return rc;
|
---|
| 1913 | }
|
---|
| 1914 |
|
---|
[63785] | 1915 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetLCHSGeometry} */
|
---|
[57388] | 1916 | static DECLCALLBACK(int) vhdGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
|
---|
[12116] | 1917 | {
|
---|
[63794] | 1918 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
|
---|
[12116] | 1919 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[63794] | 1920 | int rc = VINF_SUCCESS;
|
---|
[12116] | 1921 |
|
---|
[63794] | 1922 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 1923 |
|
---|
[63794] | 1924 | if (pImage->LCHSGeometry.cCylinders)
|
---|
| 1925 | *pLCHSGeometry = pImage->LCHSGeometry;
|
---|
[12116] | 1926 | else
|
---|
[63794] | 1927 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
[32536] | 1928 |
|
---|
[63794] | 1929 | LogFlowFunc(("returns %Rrc (CHS=%u/%u/%u)\n", rc, pImage->LCHSGeometry.cCylinders,
|
---|
| 1930 | pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors));
|
---|
[12116] | 1931 | return rc;
|
---|
| 1932 | }
|
---|
| 1933 |
|
---|
[63785] | 1934 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetLCHSGeometry} */
|
---|
[57388] | 1935 | static DECLCALLBACK(int) vhdSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
|
---|
[12116] | 1936 | {
|
---|
| 1937 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[63794] | 1938 | int rc = VINF_SUCCESS;
|
---|
[12116] | 1939 |
|
---|
[63794] | 1940 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 1941 |
|
---|
[63794] | 1942 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
| 1943 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
| 1944 | else
|
---|
[32536] | 1945 | pImage->LCHSGeometry = *pLCHSGeometry;
|
---|
[12116] | 1946 |
|
---|
[32536] | 1947 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 1948 | return rc;
|
---|
| 1949 | }
|
---|
| 1950 |
|
---|
[66486] | 1951 | /** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
|
---|
| 1952 | static 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 */
|
---|
| 1965 | static 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 |
|
---|
[63785] | 1975 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetImageFlags} */
|
---|
[57388] | 1976 | static DECLCALLBACK(unsigned) vhdGetImageFlags(void *pBackendData)
|
---|
[12116] | 1977 | {
|
---|
[32536] | 1978 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
[12116] | 1979 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 1980 |
|
---|
[63794] | 1981 | AssertPtrReturn(pImage, 0);
|
---|
[12116] | 1982 |
|
---|
[63794] | 1983 | LogFlowFunc(("returns %#x\n", pImage->uImageFlags));
|
---|
| 1984 | return pImage->uImageFlags;
|
---|
[32536] | 1985 | }
|
---|
| 1986 |
|
---|
[63785] | 1987 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetOpenFlags} */
|
---|
[57388] | 1988 | static DECLCALLBACK(unsigned) vhdGetOpenFlags(void *pBackendData)
|
---|
[32536] | 1989 | {
|
---|
| 1990 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
| 1991 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 1992 |
|
---|
[63794] | 1993 | AssertPtrReturn(pImage, 0);
|
---|
[32536] | 1994 |
|
---|
[63794] | 1995 | LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
|
---|
| 1996 | return pImage->uOpenFlags;
|
---|
[32536] | 1997 | }
|
---|
| 1998 |
|
---|
[63785] | 1999 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetOpenFlags} */
|
---|
[57388] | 2000 | static DECLCALLBACK(int) vhdSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
|
---|
[32536] | 2001 | {
|
---|
| 2002 | LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
|
---|
| 2003 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[63794] | 2004 | int rc = VINF_SUCCESS;
|
---|
[32536] | 2005 |
|
---|
[33182] | 2006 | /* Image must be opened and the new flags must be valid. */
|
---|
[44232] | 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)))
|
---|
[63794] | 2010 | rc = VERR_INVALID_PARAMETER;
|
---|
| 2011 | else
|
---|
[12116] | 2012 | {
|
---|
[63794] | 2013 | /* Implement this operation via reopening the image. */
|
---|
| 2014 | rc = vhdFreeImage(pImage, false);
|
---|
| 2015 | if (RT_SUCCESS(rc))
|
---|
| 2016 | rc = vhdOpenImage(pImage, uOpenFlags);
|
---|
[12116] | 2017 | }
|
---|
| 2018 |
|
---|
[32536] | 2019 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 2020 | return rc;
|
---|
| 2021 | }
|
---|
| 2022 |
|
---|
[63785] | 2023 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetComment} */
|
---|
[57388] | 2024 | static DECLCALLBACK(int) vhdGetComment(void *pBackendData, char *pszComment,
|
---|
| 2025 | size_t cbComment)
|
---|
[12116] | 2026 | {
|
---|
[62753] | 2027 | RT_NOREF2(pszComment, cbComment);
|
---|
[32536] | 2028 | LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
|
---|
[12116] | 2029 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 2030 |
|
---|
[63794] | 2031 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 2032 |
|
---|
[63794] | 2033 | LogFlowFunc(("returns %Rrc comment='%s'\n", VERR_NOT_SUPPORTED, pszComment));
|
---|
| 2034 | return VERR_NOT_SUPPORTED;
|
---|
[12116] | 2035 | }
|
---|
| 2036 |
|
---|
[63785] | 2037 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetComment} */
|
---|
[57388] | 2038 | static DECLCALLBACK(int) vhdSetComment(void *pBackendData, const char *pszComment)
|
---|
[12116] | 2039 | {
|
---|
[62753] | 2040 | RT_NOREF1(pszComment);
|
---|
[32536] | 2041 | LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
|
---|
[12116] | 2042 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 2043 |
|
---|
[63794] | 2044 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 2045 |
|
---|
[63794] | 2046 | int rc;
|
---|
| 2047 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
| 2048 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[12116] | 2049 | else
|
---|
[63794] | 2050 | rc = VERR_NOT_SUPPORTED;
|
---|
[32536] | 2051 |
|
---|
| 2052 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 2053 | return rc;
|
---|
| 2054 | }
|
---|
| 2055 |
|
---|
[63785] | 2056 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetUuid} */
|
---|
[57388] | 2057 | static DECLCALLBACK(int) vhdGetUuid(void *pBackendData, PRTUUID pUuid)
|
---|
[12116] | 2058 | {
|
---|
[32536] | 2059 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
[12116] | 2060 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 2061 |
|
---|
[63794] | 2062 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 2063 |
|
---|
[63794] | 2064 | *pUuid = pImage->ImageUuid;
|
---|
[32536] | 2065 |
|
---|
[63794] | 2066 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
|
---|
| 2067 | return VINF_SUCCESS;
|
---|
[12116] | 2068 | }
|
---|
| 2069 |
|
---|
[63785] | 2070 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetUuid} */
|
---|
[57388] | 2071 | static DECLCALLBACK(int) vhdSetUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
[12116] | 2072 | {
|
---|
[32536] | 2073 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
[12116] | 2074 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[63794] | 2075 | int rc = VINF_SUCCESS;
|
---|
[12116] | 2076 |
|
---|
[63794] | 2077 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 2078 |
|
---|
[63794] | 2079 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
[12116] | 2080 | {
|
---|
[63794] | 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)));
|
---|
[32536] | 2087 |
|
---|
[63794] | 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;
|
---|
[12116] | 2091 | }
|
---|
| 2092 | else
|
---|
[63794] | 2093 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[32536] | 2094 |
|
---|
| 2095 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 2096 | return rc;
|
---|
| 2097 | }
|
---|
| 2098 |
|
---|
[63785] | 2099 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetModificationUuid} */
|
---|
[57388] | 2100 | static DECLCALLBACK(int) vhdGetModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
[12116] | 2101 | {
|
---|
[62753] | 2102 | RT_NOREF1(pUuid);
|
---|
[32536] | 2103 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
[12116] | 2104 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 2105 |
|
---|
[63794] | 2106 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 2107 |
|
---|
[63794] | 2108 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
|
---|
| 2109 | return VERR_NOT_SUPPORTED;
|
---|
[12116] | 2110 | }
|
---|
| 2111 |
|
---|
[63785] | 2112 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetModificationUuid} */
|
---|
[57388] | 2113 | static DECLCALLBACK(int) vhdSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
[12116] | 2114 | {
|
---|
[62753] | 2115 | RT_NOREF1(pUuid);
|
---|
[32536] | 2116 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
[12116] | 2117 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 2118 |
|
---|
[63794] | 2119 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 2120 |
|
---|
[63794] | 2121 | int rc;
|
---|
| 2122 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
| 2123 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[12116] | 2124 | else
|
---|
[63794] | 2125 | rc = VERR_NOT_SUPPORTED;
|
---|
[32536] | 2126 |
|
---|
| 2127 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 2128 | return rc;
|
---|
| 2129 | }
|
---|
| 2130 |
|
---|
[63785] | 2131 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetParentUuid} */
|
---|
[57388] | 2132 | static DECLCALLBACK(int) vhdGetParentUuid(void *pBackendData, PRTUUID pUuid)
|
---|
[12116] | 2133 | {
|
---|
[32536] | 2134 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
| 2135 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[12116] | 2136 |
|
---|
[63794] | 2137 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 2138 |
|
---|
[63794] | 2139 | *pUuid = pImage->ParentUuid;
|
---|
[12116] | 2140 |
|
---|
[63794] | 2141 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
|
---|
| 2142 | return VINF_SUCCESS;
|
---|
[12116] | 2143 | }
|
---|
| 2144 |
|
---|
[63785] | 2145 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetParentUuid} */
|
---|
[57388] | 2146 | static DECLCALLBACK(int) vhdSetParentUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
[12116] | 2147 | {
|
---|
[32536] | 2148 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
| 2149 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 2150 | int rc = VINF_SUCCESS;
|
---|
[12116] | 2151 |
|
---|
[32536] | 2152 | if (pImage && pImage->pStorage)
|
---|
[12116] | 2153 | {
|
---|
[32536] | 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;
|
---|
[12116] | 2161 | }
|
---|
[24527] | 2162 | else
|
---|
[32536] | 2163 | rc = VERR_VD_NOT_OPENED;
|
---|
[12116] | 2164 |
|
---|
[32536] | 2165 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 2166 | return rc;
|
---|
| 2167 | }
|
---|
| 2168 |
|
---|
[63785] | 2169 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetParentModificationUuid} */
|
---|
[57388] | 2170 | static DECLCALLBACK(int) vhdGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
[12116] | 2171 | {
|
---|
[62753] | 2172 | RT_NOREF1(pUuid);
|
---|
[32536] | 2173 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
| 2174 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[12116] | 2175 |
|
---|
[63794] | 2176 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 2177 |
|
---|
[63794] | 2178 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
|
---|
| 2179 | return VERR_NOT_SUPPORTED;
|
---|
[12116] | 2180 | }
|
---|
| 2181 |
|
---|
[63785] | 2182 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetParentModificationUuid} */
|
---|
[57388] | 2183 | static DECLCALLBACK(int) vhdSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
[12116] | 2184 | {
|
---|
[62753] | 2185 | RT_NOREF1(pUuid);
|
---|
[32536] | 2186 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
| 2187 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[12116] | 2188 |
|
---|
[63794] | 2189 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[12116] | 2190 |
|
---|
[63794] | 2191 | int rc;
|
---|
| 2192 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
| 2193 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[32536] | 2194 | else
|
---|
[63794] | 2195 | rc = VERR_NOT_SUPPORTED;
|
---|
[12116] | 2196 |
|
---|
[32536] | 2197 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 2198 | return rc;
|
---|
| 2199 | }
|
---|
| 2200 |
|
---|
[63785] | 2201 | /** @interface_method_impl{VDIMAGEBACKEND,pfnDump} */
|
---|
[57388] | 2202 | static DECLCALLBACK(void) vhdDump(void *pBackendData)
|
---|
[12116] | 2203 | {
|
---|
| 2204 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 2205 |
|
---|
[63794] | 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);
|
---|
[12116] | 2213 | }
|
---|
| 2214 |
|
---|
[63785] | 2215 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetTimestamp} */
|
---|
[58132] | 2216 | static DECLCALLBACK(int) vhdGetTimestamp(void *pBackendData, PRTTIMESPEC pTimestamp)
|
---|
[12116] | 2217 | {
|
---|
[32536] | 2218 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[12116] | 2219 |
|
---|
[63794] | 2220 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[32536] | 2221 |
|
---|
[63794] | 2222 | int rc = vdIfIoIntFileGetModificationTime(pImage->pIfIo, pImage->pszFilename, pTimestamp);
|
---|
[32536] | 2223 |
|
---|
| 2224 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 2225 | return rc;
|
---|
| 2226 | }
|
---|
| 2227 |
|
---|
[63785] | 2228 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetParentTimestamp} */
|
---|
[58132] | 2229 | static DECLCALLBACK(int) vhdGetParentTimestamp(void *pBackendData, PRTTIMESPEC pTimestamp)
|
---|
[12116] | 2230 | {
|
---|
[32536] | 2231 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[12116] | 2232 |
|
---|
[63794] | 2233 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[32536] | 2234 |
|
---|
[63794] | 2235 | vhdTime2RtTime(pTimestamp, pImage->u32ParentTimestamp);
|
---|
| 2236 | LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
|
---|
| 2237 | return VINF_SUCCESS;
|
---|
[12116] | 2238 | }
|
---|
| 2239 |
|
---|
[63785] | 2240 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetParentTimestamp} */
|
---|
[58132] | 2241 | static DECLCALLBACK(int) vhdSetParentTimestamp(void *pBackendData, PCRTTIMESPEC pTimestamp)
|
---|
[12116] | 2242 | {
|
---|
| 2243 | int rc = VINF_SUCCESS;
|
---|
[32536] | 2244 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[12116] | 2245 |
|
---|
[63794] | 2246 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
| 2247 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
| 2248 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
| 2249 | else
|
---|
[12116] | 2250 | {
|
---|
[63794] | 2251 | pImage->u32ParentTimestamp = vhdRtTime2VhdTime(pTimestamp);
|
---|
| 2252 | pImage->fDynHdrNeedsUpdate = true;
|
---|
[12116] | 2253 | }
|
---|
[32536] | 2254 |
|
---|
| 2255 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 2256 | return rc;
|
---|
| 2257 | }
|
---|
| 2258 |
|
---|
[63785] | 2259 | /** @interface_method_impl{VDIMAGEBACKEND,pfnGetParentFilename} */
|
---|
[57388] | 2260 | static DECLCALLBACK(int) vhdGetParentFilename(void *pBackendData, char **ppszParentFilename)
|
---|
[12116] | 2261 | {
|
---|
[32536] | 2262 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[12116] | 2263 |
|
---|
[63794] | 2264 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
| 2265 | *ppszParentFilename = RTStrDup(pImage->pszParentFilename);
|
---|
[32536] | 2266 |
|
---|
[63794] | 2267 | LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
|
---|
| 2268 | return VINF_SUCCESS;
|
---|
[12116] | 2269 | }
|
---|
| 2270 |
|
---|
[63785] | 2271 | /** @interface_method_impl{VDIMAGEBACKEND,pfnSetParentFilename} */
|
---|
[57388] | 2272 | static DECLCALLBACK(int) vhdSetParentFilename(void *pBackendData, const char *pszParentFilename)
|
---|
[12116] | 2273 | {
|
---|
| 2274 | int rc = VINF_SUCCESS;
|
---|
[32536] | 2275 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[12116] | 2276 |
|
---|
[63794] | 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
|
---|
[12116] | 2282 | {
|
---|
[63794] | 2283 | if (pImage->pszParentFilename)
|
---|
| 2284 | RTStrFree(pImage->pszParentFilename);
|
---|
| 2285 | pImage->pszParentFilename = RTStrDup(pszParentFilename);
|
---|
| 2286 | if (!pImage->pszParentFilename)
|
---|
| 2287 | rc = VERR_NO_MEMORY;
|
---|
[12116] | 2288 | else
|
---|
[63794] | 2289 | pImage->fDynHdrNeedsUpdate = true;
|
---|
[12116] | 2290 | }
|
---|
[32536] | 2291 |
|
---|
| 2292 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[12116] | 2293 | return rc;
|
---|
| 2294 | }
|
---|
| 2295 |
|
---|
[63785] | 2296 | /** @interface_method_impl{VDIMAGEBACKEND,pfnCompact} */
|
---|
[57388] | 2297 | static DECLCALLBACK(int) vhdCompact(void *pBackendData, unsigned uPercentStart,
|
---|
| 2298 | unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
|
---|
| 2299 | PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation)
|
---|
[36632] | 2300 | {
|
---|
[62753] | 2301 | RT_NOREF2(pVDIfsDisk, pVDIfsImage);
|
---|
[36632] | 2302 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
| 2303 | int rc = VINF_SUCCESS;
|
---|
[62753] | 2304 | void *pvBuf = NULL;
|
---|
[36632] | 2305 | uint32_t *paBlocks = NULL;
|
---|
[63794] | 2306 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
[36632] | 2307 |
|
---|
[57399] | 2308 | DECLCALLBACKMEMBER(int, pfnParentRead)(void *, uint64_t, void *, size_t) = NULL;
|
---|
[36632] | 2309 | void *pvParent = NULL;
|
---|
[38469] | 2310 | PVDINTERFACEPARENTSTATE pIfParentState = VDIfParentStateGet(pVDIfsOperation);
|
---|
[36632] | 2311 | if (pIfParentState)
|
---|
| 2312 | {
|
---|
[38469] | 2313 | pfnParentRead = pIfParentState->pfnParentRead;
|
---|
| 2314 | pvParent = pIfParentState->Core.pvUser;
|
---|
[36632] | 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);
|
---|
[54117] | 2334 | AssertBreakStmt(pvParent, rc = VERR_NO_MEMORY);
|
---|
[36632] | 2335 | }
|
---|
| 2336 | pvBuf = RTMemTmpAlloc(pImage->cbDataBlock);
|
---|
[54117] | 2337 | AssertBreakStmt(pvBuf, rc = VERR_NO_MEMORY);
|
---|
[36632] | 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));
|
---|
[54117] | 2362 | AssertBreakStmt(paBlocks, rc = VERR_NO_MEMORY);
|
---|
[36632] | 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;
|
---|
[38469] | 2398 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
|
---|
[44233] | 2399 | u64Offset, pvBuf, pImage->cbDataBlock);
|
---|
[36632] | 2400 | if (RT_FAILURE(rc))
|
---|
| 2401 | break;
|
---|
| 2402 |
|
---|
| 2403 | if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)pImage->cbDataBlock * 8) == -1)
|
---|
| 2404 | {
|
---|
[62753] | 2405 | paBat[i] = UINT32_MAX;
|
---|
[36632] | 2406 | paBlocks[idxBlock] = ~0U;
|
---|
| 2407 | /* Adjust progress info, one block to be relocated. */
|
---|
| 2408 | cBlocksToMove++;
|
---|
| 2409 | }
|
---|
| 2410 | else if (pfnParentRead)
|
---|
| 2411 | {
|
---|
[40034] | 2412 | rc = pfnParentRead(pvParent, (uint64_t)i * pImage->cbDataBlock, pvParent, pImage->cbDataBlock);
|
---|
[36632] | 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 |
|
---|
[63794] | 2425 | vdIfProgress(pIfProgress, (uint64_t)i * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
|
---|
[36632] | 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);
|
---|
[54117] | 2438 | AssertBreakStmt(pvBuf, rc = VERR_NO_MEMORY);
|
---|
[36632] | 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);
|
---|
[38469] | 2456 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
|
---|
[44233] | 2457 | u64Offset, pvBuf, cbBlock);
|
---|
[36632] | 2458 | if (RT_FAILURE(rc))
|
---|
| 2459 | break;
|
---|
| 2460 |
|
---|
| 2461 | u64Offset = (uint64_t)i * cbBlock
|
---|
| 2462 | + (offBlocksStart * VHD_SECTOR_SIZE);
|
---|
[38469] | 2463 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
[44233] | 2464 | u64Offset, pvBuf, cbBlock);
|
---|
[36632] | 2465 | if (RT_FAILURE(rc))
|
---|
| 2466 | break;
|
---|
| 2467 |
|
---|
[36636] | 2468 | paBat[uBlockData] = i*(pImage->cSectorsPerDataBlock + pImage->cDataBlockBitmapSectors) + offBlocksStart;
|
---|
[36632] | 2469 |
|
---|
| 2470 | /* Truncate the file but leave enough room for the footer to avoid
|
---|
| 2471 | * races if other processes fill the whole harddisk. */
|
---|
[38469] | 2472 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
|
---|
| 2473 | pImage->uCurrentEndOfFile - cbBlock + VHD_SECTOR_SIZE);
|
---|
[36632] | 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 |
|
---|
[63794] | 2490 | rc = vdIfProgress(pIfProgress, (uint64_t)(cBlocks + cBlocksMoved) * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
|
---|
[36632] | 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 |
|
---|
[63794] | 2505 | if (RT_SUCCESS(rc))
|
---|
| 2506 | vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
|
---|
[36632] | 2507 |
|
---|
| 2508 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
| 2509 | return rc;
|
---|
| 2510 | }
|
---|
| 2511 |
|
---|
[63785] | 2512 | /** @interface_method_impl{VDIMAGEBACKEND,pfnResize} */
|
---|
[57388] | 2513 | static 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)
|
---|
[31921] | 2518 | {
|
---|
[63794] | 2519 | RT_NOREF5(uPercentSpan, uPercentStart, pVDIfsDisk, pVDIfsImage, pVDIfsOperation);
|
---|
[32536] | 2520 | PVHDIMAGE pImage = (PVHDIMAGE)pBackendData;
|
---|
[31921] | 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 |
|
---|
[33540] | 2540 | /* Go through the BAT and find the data start offset. */
|
---|
[31921] | 2541 | for (unsigned idxBlock = 0; idxBlock < pImage->cBlockAllocationTableEntries; idxBlock++)
|
---|
| 2542 | {
|
---|
| 2543 | if (pImage->pBlockAllocationTable[idxBlock] != ~0U)
|
---|
| 2544 | {
|
---|
[52695] | 2545 | uint64_t offStartBlock = (uint64_t)pImage->pBlockAllocationTable[idxBlock] * VHD_SECTOR_SIZE;
|
---|
[31921] | 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;
|
---|
[34147] | 2557 | unsigned cBlocksReloc = (unsigned)(cbOverlapping / cbBlock);
|
---|
[31921] | 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 |
|
---|
[33540] | 2582 | /* Allocate buffer for overwriting with zeroes. */
|
---|
[31921] | 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. */
|
---|
[38469] | 2600 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
|
---|
[44233] | 2601 | offStartDataNew, pvBuf, cbBlock);
|
---|
[31921] | 2602 | if (RT_FAILURE(rc))
|
---|
| 2603 | break;
|
---|
| 2604 |
|
---|
[38469] | 2605 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
[44233] | 2606 | pImage->uCurrentEndOfFile, pvBuf, cbBlock);
|
---|
[31921] | 2607 | if (RT_FAILURE(rc))
|
---|
| 2608 | break;
|
---|
| 2609 |
|
---|
| 2610 | /* Zero out the old block area. */
|
---|
[38469] | 2611 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
[44233] | 2612 | offStartDataNew, pvZero, cbBlock);
|
---|
[31921] | 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. */
|
---|
[38469] | 2660 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
| 2661 | pImage->uBlockAllocationTableOffset,
|
---|
| 2662 | pImage->pBlockAllocationTable,
|
---|
[44233] | 2663 | cBlocksNew * sizeof(uint32_t));
|
---|
[31921] | 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;
|
---|
[44252] | 2680 | vhdFlushImage(pImage);
|
---|
[31921] | 2681 | }
|
---|
| 2682 | /* Same size doesn't change the image at all. */
|
---|
| 2683 |
|
---|
| 2684 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
| 2685 | return rc;
|
---|
| 2686 | }
|
---|
| 2687 |
|
---|
[63785] | 2688 | /** @interface_method_impl{VDIMAGEBACKEND,pfnRepair} */
|
---|
[39577] | 2689 | static 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;
|
---|
[31921] | 2702 |
|
---|
[39577] | 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),
|
---|
[44233] | 2747 | &vhdFooter, sizeof(VHDFooter));
|
---|
[39577] | 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,
|
---|
[44233] | 2758 | &vhdFooter, sizeof(VHDFooter));
|
---|
[39577] | 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
|
---|
[40107] | 2776 | vdIfErrorMessage(pIfError, "Missing footer structure, using backup\n");
|
---|
[39577] | 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 | {
|
---|
[40107] | 2793 | vdIfErrorMessage(pIfError, "Checksum is invalid (should be %u got %u), repairing\n",
|
---|
[39577] | 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,
|
---|
[44233] | 2833 | &dynamicDiskHeader, sizeof(VHDDynamicDiskHeader));
|
---|
[39577] | 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 | {
|
---|
[40107] | 2851 | vdIfErrorMessage(pIfError, "Checksum of dynamic disk header is invalid (should be %u got %u), repairing\n",
|
---|
[39577] | 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,
|
---|
[44233] | 2880 | cBatEntries * sizeof(uint32_t));
|
---|
[39577] | 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 |
|
---|
[40034] | 2889 | pu32BlockBitmap = (uint32_t *)RTMemAllocZ(RT_ALIGN_Z(cBatEntries / 8, 4));
|
---|
[39577] | 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 |
|
---|
[40107] | 2905 | vdIfErrorMessage(pIfError, "First data block at sector %u\n", idxMinBlock);
|
---|
[39577] | 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 | {
|
---|
[40107] | 2919 | vdIfErrorMessage(pIfError, "Entry %u points to invalid offset %llu, clearing\n",
|
---|
[39577] | 2920 | i, offBlock);
|
---|
[40240] | 2921 | paBat[i] = UINT32_C(0xffffffff);
|
---|
[39577] | 2922 | fRepairBat = true;
|
---|
| 2923 | }
|
---|
| 2924 | else if (offBlock + cbBlock > offFooter)
|
---|
| 2925 | {
|
---|
[40107] | 2926 | vdIfErrorMessage(pIfError, "Entry %u intersects with footer, aligning footer\n",
|
---|
[39577] | 2927 | i);
|
---|
| 2928 | offFooter = offBlock + cbBlock;
|
---|
| 2929 | fRepairBat = true;
|
---|
| 2930 | }
|
---|
| 2931 |
|
---|
[40240] | 2932 | if ( paBat[i] != UINT32_C(0xffffffff)
|
---|
[48851] | 2933 | && ASMBitTestAndSet(pu32BlockBitmap, (uint32_t)((paBat[i] - idxMinBlock) / (cbBlock / VHD_SECTOR_SIZE))))
|
---|
[39577] | 2934 | {
|
---|
[40107] | 2935 | vdIfErrorMessage(pIfError, "Entry %u points to an already referenced data block, clearing\n",
|
---|
[39577] | 2936 | i);
|
---|
[40240] | 2937 | paBat[i] = UINT32_C(0xffffffff);
|
---|
[39577] | 2938 | fRepairBat = true;
|
---|
| 2939 | }
|
---|
| 2940 | }
|
---|
| 2941 | }
|
---|
| 2942 | }
|
---|
| 2943 |
|
---|
| 2944 | /* Write repaired structures now. */
|
---|
| 2945 | if (!(fRepairBat || fRepairDynHeader || fRepairFooter))
|
---|
[40107] | 2946 | vdIfErrorMessage(pIfError, "VHD image is in a consistent state, no repair required\n");
|
---|
[39577] | 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 |
|
---|
[40107] | 2954 | vdIfErrorMessage(pIfError, "Writing repaired block allocation table...\n");
|
---|
[39577] | 2955 |
|
---|
| 2956 | rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, offBat, paBat,
|
---|
[44233] | 2957 | cBatEntries * sizeof(uint32_t));
|
---|
[39577] | 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 |
|
---|
[40107] | 2971 | vdIfErrorMessage(pIfError, "Writing repaired dynamic disk header...\n");
|
---|
[39577] | 2972 | rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, offDynamicDiskHeader, &dynamicDiskHeader,
|
---|
[44233] | 2973 | sizeof(VHDDynamicDiskHeader));
|
---|
[39577] | 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 | {
|
---|
[40107] | 2985 | vdIfErrorMessage(pIfError, "Writing repaired Footer...\n");
|
---|
[39577] | 2986 |
|
---|
| 2987 | if (fDynamic)
|
---|
| 2988 | {
|
---|
| 2989 | /* Write backup at image beginning. */
|
---|
| 2990 | rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, 0, &vhdFooter,
|
---|
[44233] | 2991 | sizeof(VHDFooter));
|
---|
[39577] | 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,
|
---|
[44233] | 3002 | sizeof(VHDFooter));
|
---|
[39577] | 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 |
|
---|
[40107] | 3012 | vdIfErrorMessage(pIfError, "Corrupted VHD image repaired successfully\n");
|
---|
[39577] | 3013 | }
|
---|
| 3014 | } while(0);
|
---|
| 3015 |
|
---|
| 3016 | if (paBat)
|
---|
| 3017 | RTMemFree(paBat);
|
---|
| 3018 |
|
---|
| 3019 | if (pu32BlockBitmap)
|
---|
| 3020 | RTMemFree(pu32BlockBitmap);
|
---|
| 3021 |
|
---|
| 3022 | if (pStorage)
|
---|
[46613] | 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 | }
|
---|
[39577] | 3028 |
|
---|
| 3029 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
| 3030 | return rc;
|
---|
| 3031 | }
|
---|
| 3032 |
|
---|
| 3033 |
|
---|
[63781] | 3034 | const VDIMAGEBACKEND g_VhdBackend =
|
---|
[12116] | 3035 | {
|
---|
[63905] | 3036 | /* u32Version */
|
---|
| 3037 | VD_IMGBACKEND_VERSION,
|
---|
[12116] | 3038 | /* pszBackendName */
|
---|
| 3039 | "VHD",
|
---|
| 3040 | /* uBackendCaps */
|
---|
[18051] | 3041 | VD_CAP_UUID | VD_CAP_DIFF | VD_CAP_FILE |
|
---|
[30555] | 3042 | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC |
|
---|
[60608] | 3043 | VD_CAP_ASYNC | VD_CAP_VFS | VD_CAP_PREFERRED,
|
---|
[33524] | 3044 | /* paFileExtensions */
|
---|
| 3045 | s_aVhdFileExtensions,
|
---|
[12116] | 3046 | /* paConfigInfo */
|
---|
| 3047 | NULL,
|
---|
[63802] |
---|