[40033] | 1 | /* $Id: VDI.cpp 103501 2024-02-21 15:43:55Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[1565] | 3 | * Virtual Disk Image (VDI), Core Code.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[1] | 26 | */
|
---|
| 27 |
|
---|
[57358] | 28 |
|
---|
| 29 | /*********************************************************************************************************************************
|
---|
| 30 | * Header Files *
|
---|
| 31 | *********************************************************************************************************************************/
|
---|
[7159] | 32 | #define LOG_GROUP LOG_GROUP_VD_VDI
|
---|
[33567] | 33 | #include <VBox/vd-plugin.h>
|
---|
[1565] | 34 | #include "VDICore.h"
|
---|
[1] | 35 | #include <VBox/err.h>
|
---|
| 36 |
|
---|
| 37 | #include <VBox/log.h>
|
---|
| 38 | #include <iprt/alloc.h>
|
---|
| 39 | #include <iprt/assert.h>
|
---|
| 40 | #include <iprt/uuid.h>
|
---|
[150] | 41 | #include <iprt/string.h>
|
---|
[745] | 42 | #include <iprt/asm.h>
|
---|
[1] | 43 |
|
---|
[50988] | 44 | #include "VDBackends.h"
|
---|
| 45 |
|
---|
[7159] | 46 | #define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
|
---|
[1] | 47 |
|
---|
[40936] | 48 | /** Macros for endianess conversion. */
|
---|
| 49 | #define SET_ENDIAN_U32(conv, u32) (conv == VDIECONV_H2F ? RT_H2LE_U32(u32) : RT_LE2H_U32(u32))
|
---|
| 50 | #define SET_ENDIAN_U64(conv, u64) (conv == VDIECONV_H2F ? RT_H2LE_U64(u64) : RT_LE2H_U64(u64))
|
---|
| 51 |
|
---|
[77606] | 52 | static const char *vdiAllocationBlockSize = "1048576";
|
---|
[11421] | 53 |
|
---|
[77622] | 54 | static const VDCONFIGINFO vdiConfigInfo[] =
|
---|
| 55 | {
|
---|
[79742] | 56 | { "AllocationBlockSize", vdiAllocationBlockSize, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_CREATEONLY },
|
---|
[77622] | 57 | { NULL, NULL, VDCFGVALUETYPE_INTEGER, 0 }
|
---|
[77606] | 58 | };
|
---|
| 59 |
|
---|
| 60 |
|
---|
[57358] | 61 | /*********************************************************************************************************************************
|
---|
| 62 | * Static Variables *
|
---|
| 63 | *********************************************************************************************************************************/
|
---|
| 64 |
|
---|
[11421] | 65 | /** NULL-terminated array of supported file extensions. */
|
---|
[33524] | 66 | static const VDFILEEXTENSION s_aVdiFileExtensions[] =
|
---|
[11421] | 67 | {
|
---|
[33524] | 68 | {"vdi", VDTYPE_HDD},
|
---|
| 69 | {NULL, VDTYPE_INVALID}
|
---|
[11421] | 70 | };
|
---|
| 71 |
|
---|
[57358] | 72 |
|
---|
| 73 | /*********************************************************************************************************************************
|
---|
| 74 | * Internal Functions *
|
---|
| 75 | *********************************************************************************************************************************/
|
---|
[1] | 76 | static unsigned getPowerOfTwo(unsigned uNumber);
|
---|
| 77 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
|
---|
[139] | 78 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
|
---|
| 79 | static int vdiValidateHeader(PVDIHEADER pHeader);
|
---|
[1] | 80 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
|
---|
[139] | 81 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
|
---|
| 82 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
[28065] | 83 | static int vdiUpdateHeaderAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx);
|
---|
[38876] | 84 | static int vdiUpdateBlockInfoAsync(PVDIIMAGEDESC pImage, unsigned uBlock, PVDIOCTX pIoCtx,
|
---|
| 85 | bool fUpdateHdr);
|
---|
[139] | 86 |
|
---|
[7159] | 87 | /**
|
---|
[40936] | 88 | * Internal: Convert the PreHeader fields to the appropriate endianess.
|
---|
| 89 | * @param enmConv Direction of the conversion.
|
---|
| 90 | * @param pPreHdrConv Where to store the converted pre header.
|
---|
| 91 | * @param pPreHdr PreHeader pointer.
|
---|
| 92 | */
|
---|
| 93 | static void vdiConvPreHeaderEndianess(VDIECONV enmConv, PVDIPREHEADER pPreHdrConv,
|
---|
| 94 | PVDIPREHEADER pPreHdr)
|
---|
| 95 | {
|
---|
[40953] | 96 | memcpy(pPreHdrConv->szFileInfo, pPreHdr->szFileInfo, sizeof(pPreHdr->szFileInfo));
|
---|
[40936] | 97 | pPreHdrConv->u32Signature = SET_ENDIAN_U32(enmConv, pPreHdr->u32Signature);
|
---|
| 98 | pPreHdrConv->u32Version = SET_ENDIAN_U32(enmConv, pPreHdr->u32Version);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Internal: Convert the VDIDISKGEOMETRY fields to the appropriate endianess.
|
---|
| 103 | * @param enmConv Direction of the conversion.
|
---|
| 104 | * @param pDiskGeoConv Where to store the converted geometry.
|
---|
| 105 | * @param pDiskGeo Pointer to the disk geometry to convert.
|
---|
| 106 | */
|
---|
| 107 | static void vdiConvGeometryEndianess(VDIECONV enmConv, PVDIDISKGEOMETRY pDiskGeoConv,
|
---|
| 108 | PVDIDISKGEOMETRY pDiskGeo)
|
---|
| 109 | {
|
---|
| 110 | pDiskGeoConv->cCylinders = SET_ENDIAN_U32(enmConv, pDiskGeo->cCylinders);
|
---|
| 111 | pDiskGeoConv->cHeads = SET_ENDIAN_U32(enmConv, pDiskGeo->cHeads);
|
---|
| 112 | pDiskGeoConv->cSectors = SET_ENDIAN_U32(enmConv, pDiskGeo->cSectors);
|
---|
| 113 | pDiskGeoConv->cbSector = SET_ENDIAN_U32(enmConv, pDiskGeo->cbSector);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * Internal: Convert the Header - version 0 fields to the appropriate endianess.
|
---|
| 118 | * @param enmConv Direction of the conversion.
|
---|
| 119 | * @param pHdrConv Where to store the converted header.
|
---|
| 120 | * @param pHdr Pointer to the version 0 header.
|
---|
| 121 | */
|
---|
| 122 | static void vdiConvHeaderEndianessV0(VDIECONV enmConv, PVDIHEADER0 pHdrConv,
|
---|
| 123 | PVDIHEADER0 pHdr)
|
---|
| 124 | {
|
---|
[46612] | 125 | memmove(pHdrConv->szComment, pHdr->szComment, sizeof(pHdr->szComment));
|
---|
[40936] | 126 | pHdrConv->u32Type = SET_ENDIAN_U32(enmConv, pHdr->u32Type);
|
---|
| 127 | pHdrConv->fFlags = SET_ENDIAN_U32(enmConv, pHdr->fFlags);
|
---|
| 128 | vdiConvGeometryEndianess(enmConv, &pHdrConv->LegacyGeometry, &pHdr->LegacyGeometry);
|
---|
| 129 | pHdrConv->cbDisk = SET_ENDIAN_U64(enmConv, pHdr->cbDisk);
|
---|
| 130 | pHdrConv->cbBlock = SET_ENDIAN_U32(enmConv, pHdr->cbBlock);
|
---|
| 131 | pHdrConv->cBlocks = SET_ENDIAN_U32(enmConv, pHdr->cBlocks);
|
---|
| 132 | pHdrConv->cBlocksAllocated = SET_ENDIAN_U32(enmConv, pHdr->cBlocksAllocated);
|
---|
| 133 | /* Don't convert the RTUUID fields. */
|
---|
| 134 | pHdrConv->uuidCreate = pHdr->uuidCreate;
|
---|
| 135 | pHdrConv->uuidModify = pHdr->uuidModify;
|
---|
| 136 | pHdrConv->uuidLinkage = pHdr->uuidLinkage;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * Internal: Set the Header - version 1 fields to the appropriate endianess.
|
---|
| 141 | * @param enmConv Direction of the conversion.
|
---|
| 142 | * @param pHdrConv Where to store the converted header.
|
---|
| 143 | * @param pHdr Version 1 Header pointer.
|
---|
| 144 | */
|
---|
| 145 | static void vdiConvHeaderEndianessV1(VDIECONV enmConv, PVDIHEADER1 pHdrConv,
|
---|
| 146 | PVDIHEADER1 pHdr)
|
---|
| 147 | {
|
---|
[46612] | 148 | memmove(pHdrConv->szComment, pHdr->szComment, sizeof(pHdr->szComment));
|
---|
[40936] | 149 | pHdrConv->cbHeader = SET_ENDIAN_U32(enmConv, pHdr->cbHeader);
|
---|
| 150 | pHdrConv->u32Type = SET_ENDIAN_U32(enmConv, pHdr->u32Type);
|
---|
| 151 | pHdrConv->fFlags = SET_ENDIAN_U32(enmConv, pHdr->fFlags);
|
---|
| 152 | pHdrConv->offBlocks = SET_ENDIAN_U32(enmConv, pHdr->offBlocks);
|
---|
| 153 | pHdrConv->offData = SET_ENDIAN_U32(enmConv, pHdr->offData);
|
---|
| 154 | vdiConvGeometryEndianess(enmConv, &pHdrConv->LegacyGeometry, &pHdr->LegacyGeometry);
|
---|
| 155 | pHdrConv->u32Dummy = SET_ENDIAN_U32(enmConv, pHdr->u32Dummy);
|
---|
| 156 | pHdrConv->cbDisk = SET_ENDIAN_U64(enmConv, pHdr->cbDisk);
|
---|
| 157 | pHdrConv->cbBlock = SET_ENDIAN_U32(enmConv, pHdr->cbBlock);
|
---|
| 158 | pHdrConv->cbBlockExtra = SET_ENDIAN_U32(enmConv, pHdr->cbBlockExtra);
|
---|
| 159 | pHdrConv->cBlocks = SET_ENDIAN_U32(enmConv, pHdr->cBlocks);
|
---|
| 160 | pHdrConv->cBlocksAllocated = SET_ENDIAN_U32(enmConv, pHdr->cBlocksAllocated);
|
---|
| 161 | /* Don't convert the RTUUID fields. */
|
---|
| 162 | pHdrConv->uuidCreate = pHdr->uuidCreate;
|
---|
| 163 | pHdrConv->uuidModify = pHdr->uuidModify;
|
---|
| 164 | pHdrConv->uuidLinkage = pHdr->uuidLinkage;
|
---|
| 165 | pHdrConv->uuidParentModify = pHdr->uuidParentModify;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * Internal: Set the Header - version 1plus fields to the appropriate endianess.
|
---|
| 170 | * @param enmConv Direction of the conversion.
|
---|
| 171 | * @param pHdrConv Where to store the converted header.
|
---|
| 172 | * @param pHdr Version 1+ Header pointer.
|
---|
| 173 | */
|
---|
| 174 | static void vdiConvHeaderEndianessV1p(VDIECONV enmConv, PVDIHEADER1PLUS pHdrConv,
|
---|
| 175 | PVDIHEADER1PLUS pHdr)
|
---|
| 176 | {
|
---|
[46612] | 177 | memmove(pHdrConv->szComment, pHdr->szComment, sizeof(pHdr->szComment));
|
---|
[40936] | 178 | pHdrConv->cbHeader = SET_ENDIAN_U32(enmConv, pHdr->cbHeader);
|
---|
| 179 | pHdrConv->u32Type = SET_ENDIAN_U32(enmConv, pHdr->u32Type);
|
---|
| 180 | pHdrConv->fFlags = SET_ENDIAN_U32(enmConv, pHdr->fFlags);
|
---|
| 181 | pHdrConv->offBlocks = SET_ENDIAN_U32(enmConv, pHdr->offBlocks);
|
---|
| 182 | pHdrConv->offData = SET_ENDIAN_U32(enmConv, pHdr->offData);
|
---|
| 183 | vdiConvGeometryEndianess(enmConv, &pHdrConv->LegacyGeometry, &pHdr->LegacyGeometry);
|
---|
| 184 | pHdrConv->u32Dummy = SET_ENDIAN_U32(enmConv, pHdr->u32Dummy);
|
---|
| 185 | pHdrConv->cbDisk = SET_ENDIAN_U64(enmConv, pHdr->cbDisk);
|
---|
| 186 | pHdrConv->cbBlock = SET_ENDIAN_U32(enmConv, pHdr->cbBlock);
|
---|
| 187 | pHdrConv->cbBlockExtra = SET_ENDIAN_U32(enmConv, pHdr->cbBlockExtra);
|
---|
| 188 | pHdrConv->cBlocks = SET_ENDIAN_U32(enmConv, pHdr->cBlocks);
|
---|
| 189 | pHdrConv->cBlocksAllocated = SET_ENDIAN_U32(enmConv, pHdr->cBlocksAllocated);
|
---|
| 190 | /* Don't convert the RTUUID fields. */
|
---|
| 191 | pHdrConv->uuidCreate = pHdr->uuidCreate;
|
---|
| 192 | pHdrConv->uuidModify = pHdr->uuidModify;
|
---|
| 193 | pHdrConv->uuidLinkage = pHdr->uuidLinkage;
|
---|
| 194 | pHdrConv->uuidParentModify = pHdr->uuidParentModify;
|
---|
| 195 | vdiConvGeometryEndianess(enmConv, &pHdrConv->LCHSGeometry, &pHdr->LCHSGeometry);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[77606] | 198 |
|
---|
[40936] | 199 | /**
|
---|
| 200 | * Internal: Set the appropriate endianess on all the Blocks pointed.
|
---|
| 201 | * @param enmConv Direction of the conversion.
|
---|
| 202 | * @param paBlocks Pointer to the block array.
|
---|
| 203 | * @param cEntries Number of entries in the block array.
|
---|
| 204 | *
|
---|
| 205 | * @note Unlike the other conversion functions this method does an in place conversion
|
---|
| 206 | * to avoid temporary memory allocations when writing the block array.
|
---|
| 207 | */
|
---|
| 208 | static void vdiConvBlocksEndianess(VDIECONV enmConv, PVDIIMAGEBLOCKPOINTER paBlocks,
|
---|
| 209 | unsigned cEntries)
|
---|
| 210 | {
|
---|
| 211 | for (unsigned i = 0; i < cEntries; i++)
|
---|
| 212 | paBlocks[i] = SET_ENDIAN_U32(enmConv, paBlocks[i]);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /**
|
---|
[32536] | 216 | * Internal: Flush the image file to disk.
|
---|
| 217 | */
|
---|
| 218 | static void vdiFlushImage(PVDIIMAGEDESC pImage)
|
---|
[28383] | 219 | {
|
---|
[32536] | 220 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
| 221 | {
|
---|
| 222 | /* Save header. */
|
---|
| 223 | int rc = vdiUpdateHeader(pImage);
|
---|
| 224 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
| 225 | pImage->pszFilename, rc));
|
---|
[38469] | 226 | vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
|
---|
[32536] | 227 | }
|
---|
[28383] | 228 | }
|
---|
[22966] | 229 |
|
---|
[32536] | 230 | /**
|
---|
| 231 | * Internal: Free all allocated space for representing an image, and optionally
|
---|
| 232 | * delete the image from disk.
|
---|
| 233 | */
|
---|
| 234 | static int vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
|
---|
| 235 | {
|
---|
| 236 | int rc = VINF_SUCCESS;
|
---|
[28383] | 237 |
|
---|
[32536] | 238 | /* Freeing a never allocated image (e.g. because the open failed) is
|
---|
| 239 | * not signalled as an error. After all nothing bad happens. */
|
---|
| 240 | if (pImage)
|
---|
| 241 | {
|
---|
| 242 | if (pImage->pStorage)
|
---|
| 243 | {
|
---|
| 244 | /* No point updating the file that is deleted anyway. */
|
---|
| 245 | if (!fDelete)
|
---|
| 246 | vdiFlushImage(pImage);
|
---|
| 247 |
|
---|
[46613] | 248 | rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
|
---|
[32536] | 249 | pImage->pStorage = NULL;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | if (pImage->paBlocks)
|
---|
| 253 | {
|
---|
| 254 | RTMemFree(pImage->paBlocks);
|
---|
| 255 | pImage->paBlocks = NULL;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[38621] | 258 | if (pImage->paBlocksRev)
|
---|
| 259 | {
|
---|
| 260 | RTMemFree(pImage->paBlocksRev);
|
---|
| 261 | pImage->paBlocksRev = NULL;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[32536] | 264 | if (fDelete && pImage->pszFilename)
|
---|
[68787] | 265 | {
|
---|
| 266 | int rc2 = vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
|
---|
| 267 | if (RT_SUCCESS(rc))
|
---|
| 268 | rc = rc2;
|
---|
| 269 | }
|
---|
[32536] | 270 | }
|
---|
| 271 |
|
---|
| 272 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
| 273 | return rc;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[1] | 276 | /**
|
---|
| 277 | * internal: return power of 2 or 0 if num error.
|
---|
| 278 | */
|
---|
| 279 | static unsigned getPowerOfTwo(unsigned uNumber)
|
---|
| 280 | {
|
---|
| 281 | if (uNumber == 0)
|
---|
| 282 | return 0;
|
---|
| 283 | unsigned uPower2 = 0;
|
---|
| 284 | while ((uNumber & 1) == 0)
|
---|
| 285 | {
|
---|
| 286 | uNumber >>= 1;
|
---|
| 287 | uPower2++;
|
---|
| 288 | }
|
---|
| 289 | return uNumber == 1 ? uPower2 : 0;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /**
|
---|
[7159] | 293 | * Internal: Init VDI preheader.
|
---|
[1] | 294 | */
|
---|
| 295 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
|
---|
| 296 | {
|
---|
| 297 | pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
|
---|
| 298 | pPreHdr->u32Version = VDI_IMAGE_VERSION;
|
---|
| 299 | memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
|
---|
[32878] | 300 | strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo)-1);
|
---|
[1] | 301 | }
|
---|
| 302 |
|
---|
| 303 | /**
|
---|
[7159] | 304 | * Internal: check VDI preheader.
|
---|
[1] | 305 | */
|
---|
| 306 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
|
---|
| 307 | {
|
---|
| 308 | if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
|
---|
[15673] | 309 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
[1] | 310 |
|
---|
[6363] | 311 | if ( VDI_GET_VERSION_MAJOR(pPreHdr->u32Version) != VDI_IMAGE_VERSION_MAJOR
|
---|
[1] | 312 | && pPreHdr->u32Version != 0x00000002) /* old version. */
|
---|
[15366] | 313 | return VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
[1] | 314 |
|
---|
| 315 | return VINF_SUCCESS;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | /**
|
---|
[17970] | 319 | * Internal: translate VD image flags to VDI image type enum.
|
---|
[13223] | 320 | */
|
---|
[17970] | 321 | static VDIIMAGETYPE vdiTranslateImageFlags2VDI(unsigned uImageFlags)
|
---|
[13223] | 322 | {
|
---|
[17970] | 323 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
| 324 | return VDI_IMAGE_TYPE_FIXED;
|
---|
| 325 | else if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
| 326 | return VDI_IMAGE_TYPE_DIFF;
|
---|
| 327 | else
|
---|
| 328 | return VDI_IMAGE_TYPE_NORMAL;
|
---|
[13223] | 329 | }
|
---|
| 330 |
|
---|
| 331 | /**
|
---|
| 332 | * Internal: translate VDI image type enum to VD image type enum.
|
---|
| 333 | */
|
---|
[17970] | 334 | static unsigned vdiTranslateVDI2ImageFlags(VDIIMAGETYPE enmType)
|
---|
[13223] | 335 | {
|
---|
| 336 | switch (enmType)
|
---|
| 337 | {
|
---|
| 338 | case VDI_IMAGE_TYPE_NORMAL:
|
---|
[17970] | 339 | return VD_IMAGE_FLAGS_NONE;
|
---|
[13223] | 340 | case VDI_IMAGE_TYPE_FIXED:
|
---|
[17970] | 341 | return VD_IMAGE_FLAGS_FIXED;
|
---|
[13223] | 342 | case VDI_IMAGE_TYPE_DIFF:
|
---|
[17970] | 343 | return VD_IMAGE_FLAGS_DIFF;
|
---|
[13223] | 344 | default:
|
---|
| 345 | AssertMsgFailed(("invalid VDIIMAGETYPE enmType=%d\n", (int)enmType));
|
---|
[17970] | 346 | return VD_IMAGE_FLAGS_NONE;
|
---|
[13223] | 347 | }
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | /**
|
---|
[7159] | 351 | * Internal: Init VDI header. Always use latest header version.
|
---|
[64272] | 352 | *
|
---|
| 353 | * @param pHeader Assumes it was initially initialized to all zeros.
|
---|
| 354 | * @param uImageFlags Flags for this image.
|
---|
| 355 | * @param pszComment Optional comment to set for the image.
|
---|
| 356 | * @param cbDisk Size of the disk in bytes.
|
---|
| 357 | * @param cbBlock Size of one block in the image.
|
---|
| 358 | * @param cbBlockExtra Extra data for one block private to the image.
|
---|
| 359 | * @param cbDataAlign The alignment for all data structures.
|
---|
[1] | 360 | */
|
---|
[17970] | 361 | static void vdiInitHeader(PVDIHEADER pHeader, uint32_t uImageFlags,
|
---|
| 362 | const char *pszComment, uint64_t cbDisk,
|
---|
[43602] | 363 | uint32_t cbBlock, uint32_t cbBlockExtra,
|
---|
| 364 | uint32_t cbDataAlign)
|
---|
[1] | 365 | {
|
---|
| 366 | pHeader->uVersion = VDI_IMAGE_VERSION;
|
---|
[33081] | 367 | pHeader->u.v1plus.cbHeader = sizeof(VDIHEADER1PLUS);
|
---|
| 368 | pHeader->u.v1plus.u32Type = (uint32_t)vdiTranslateImageFlags2VDI(uImageFlags);
|
---|
| 369 | pHeader->u.v1plus.fFlags = (uImageFlags & VD_VDI_IMAGE_FLAGS_ZERO_EXPAND) ? 1 : 0;
|
---|
[1] | 370 | #ifdef VBOX_STRICT
|
---|
| 371 | char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
|
---|
[33081] | 372 | Assert(!memcmp(pHeader->u.v1plus.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
|
---|
[1] | 373 | #endif
|
---|
[33081] | 374 | pHeader->u.v1plus.szComment[0] = '\0';
|
---|
[1] | 375 | if (pszComment)
|
---|
| 376 | {
|
---|
[33081] | 377 | AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1plus.szComment),
|
---|
[1] | 378 | ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
|
---|
[33081] | 379 | strncat(pHeader->u.v1plus.szComment, pszComment, sizeof(pHeader->u.v1plus.szComment)-1);
|
---|
[1] | 380 | }
|
---|
| 381 |
|
---|
[6363] | 382 | /* Mark the legacy geometry not-calculated. */
|
---|
[33081] | 383 | pHeader->u.v1plus.LegacyGeometry.cCylinders = 0;
|
---|
| 384 | pHeader->u.v1plus.LegacyGeometry.cHeads = 0;
|
---|
| 385 | pHeader->u.v1plus.LegacyGeometry.cSectors = 0;
|
---|
| 386 | pHeader->u.v1plus.LegacyGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
| 387 | pHeader->u.v1plus.u32Dummy = 0; /* used to be the translation value */
|
---|
[1] | 388 |
|
---|
[33081] | 389 | pHeader->u.v1plus.cbDisk = cbDisk;
|
---|
| 390 | pHeader->u.v1plus.cbBlock = cbBlock;
|
---|
| 391 | pHeader->u.v1plus.cBlocks = (uint32_t)(cbDisk / cbBlock);
|
---|
[1] | 392 | if (cbDisk % cbBlock)
|
---|
[33081] | 393 | pHeader->u.v1plus.cBlocks++;
|
---|
| 394 | pHeader->u.v1plus.cbBlockExtra = cbBlockExtra;
|
---|
| 395 | pHeader->u.v1plus.cBlocksAllocated = 0;
|
---|
[1] | 396 |
|
---|
| 397 | /* Init offsets. */
|
---|
[43602] | 398 | pHeader->u.v1plus.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1PLUS), cbDataAlign);
|
---|
| 399 | pHeader->u.v1plus.offData = RT_ALIGN_32(pHeader->u.v1plus.offBlocks + (pHeader->u.v1plus.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), cbDataAlign);
|
---|
[1] | 400 |
|
---|
| 401 | /* Init uuids. */
|
---|
[62740] | 402 | #ifdef _MSC_VER
|
---|
| 403 | # pragma warning(disable:4366) /* (harmless "misalignment") */
|
---|
| 404 | #endif
|
---|
[33081] | 405 | RTUuidCreate(&pHeader->u.v1plus.uuidCreate);
|
---|
| 406 | RTUuidClear(&pHeader->u.v1plus.uuidModify);
|
---|
| 407 | RTUuidClear(&pHeader->u.v1plus.uuidLinkage);
|
---|
| 408 | RTUuidClear(&pHeader->u.v1plus.uuidParentModify);
|
---|
[62740] | 409 | #ifdef _MSC_VER
|
---|
| 410 | # pragma warning(default:4366)
|
---|
| 411 | #endif
|
---|
[6363] | 412 |
|
---|
| 413 | /* Mark LCHS geometry not-calculated. */
|
---|
| 414 | pHeader->u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
| 415 | pHeader->u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
| 416 | pHeader->u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
| 417 | pHeader->u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
[1] | 418 | }
|
---|
| 419 |
|
---|
| 420 | /**
|
---|
[7159] | 421 | * Internal: Check VDI header.
|
---|
[1] | 422 | */
|
---|
| 423 | static int vdiValidateHeader(PVDIHEADER pHeader)
|
---|
| 424 | {
|
---|
[33540] | 425 | /* Check version-dependent header parameters. */
|
---|
[1] | 426 | switch (GET_MAJOR_HEADER_VERSION(pHeader))
|
---|
| 427 | {
|
---|
| 428 | case 0:
|
---|
| 429 | {
|
---|
| 430 | /* Old header version. */
|
---|
| 431 | break;
|
---|
| 432 | }
|
---|
| 433 | case 1:
|
---|
| 434 | {
|
---|
| 435 | /* Current header version. */
|
---|
| 436 |
|
---|
| 437 | if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
|
---|
[124] | 438 | {
|
---|
| 439 | LogRel(("VDI: v1 header size wrong (%d < %d)\n",
|
---|
| 440 | pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
|
---|
[15366] | 441 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
[124] | 442 | }
|
---|
[1] | 443 |
|
---|
| 444 | if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
|
---|
[124] | 445 | {
|
---|
| 446 | LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
|
---|
| 447 | getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
|
---|
[15366] | 448 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
[124] | 449 | }
|
---|
[1] | 450 |
|
---|
| 451 | if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
|
---|
[124] | 452 | {
|
---|
| 453 | LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
|
---|
| 454 | getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
|
---|
[15366] | 455 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
[124] | 456 | }
|
---|
[1] | 457 |
|
---|
| 458 | break;
|
---|
| 459 | }
|
---|
| 460 | default:
|
---|
| 461 | /* Unsupported. */
|
---|
[15366] | 462 | return VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
[1] | 463 | }
|
---|
| 464 |
|
---|
| 465 | /* Check common header parameters. */
|
---|
| 466 |
|
---|
[150] | 467 | bool fFailed = false;
|
---|
[139] | 468 |
|
---|
[1] | 469 | if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
|
---|
| 470 | || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
|
---|
[139] | 471 | {
|
---|
| 472 | LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
|
---|
[150] | 473 | fFailed = true;
|
---|
[139] | 474 | }
|
---|
[1] | 475 |
|
---|
[7159] | 476 | if (getImageFlags(pHeader) & ~VD_VDI_IMAGE_FLAGS_MASK)
|
---|
[139] | 477 | {
|
---|
| 478 | LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
|
---|
[150] | 479 | fFailed = true;
|
---|
[139] | 480 | }
|
---|
[1] | 481 |
|
---|
[103501] | 482 | PVDIDISKGEOMETRY pLCHSGeom = getImageLCHSGeometry(pHeader);
|
---|
| 483 | if ( pLCHSGeom
|
---|
| 484 | && pLCHSGeom->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
|
---|
[124] | 485 | {
|
---|
[103501] | 486 | LogRel(("VDI: wrong sector size (%d != %d)\n", pLCHSGeom->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
|
---|
[150] | 487 | fFailed = true;
|
---|
[124] | 488 | }
|
---|
[1] | 489 |
|
---|
| 490 | if ( getImageDiskSize(pHeader) == 0
|
---|
| 491 | || getImageBlockSize(pHeader) == 0
|
---|
| 492 | || getImageBlocks(pHeader) == 0
|
---|
[124] | 493 | || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
|
---|
| 494 | {
|
---|
| 495 | LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
|
---|
| 496 | getImageDiskSize(pHeader), getImageBlockSize(pHeader),
|
---|
| 497 | getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
|
---|
[150] | 498 | fFailed = true;
|
---|
[124] | 499 | }
|
---|
[1] | 500 |
|
---|
[124] | 501 | if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
|
---|
| 502 | {
|
---|
| 503 | LogRel(("VDI: too many blocks allocated (%d > %d)\n"
|
---|
| 504 | " blocksize=%d disksize=%lld\n",
|
---|
| 505 | getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
|
---|
| 506 | getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
|
---|
[150] | 507 | fFailed = true;
|
---|
[124] | 508 | }
|
---|
| 509 |
|
---|
[1] | 510 | if ( getImageExtraBlockSize(pHeader) != 0
|
---|
| 511 | && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
|
---|
[124] | 512 | {
|
---|
| 513 | LogRel(("VDI: wrong extra size (%d, %d)\n",
|
---|
| 514 | getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
|
---|
[150] | 515 | fFailed = true;
|
---|
[124] | 516 | }
|
---|
[1] | 517 |
|
---|
[139] | 518 | if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
|
---|
[124] | 519 | {
|
---|
| 520 | LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
|
---|
[139] | 521 | getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
|
---|
[150] | 522 | fFailed = true;
|
---|
[124] | 523 | }
|
---|
[1] | 524 |
|
---|
| 525 | if (RTUuidIsNull(getImageCreationUUID(pHeader)))
|
---|
[124] | 526 | {
|
---|
| 527 | LogRel(("VDI: uuid of creator is 0\n"));
|
---|
[150] | 528 | fFailed = true;
|
---|
[124] | 529 | }
|
---|
[139] | 530 |
|
---|
[1] | 531 | if (RTUuidIsNull(getImageModificationUUID(pHeader)))
|
---|
[124] | 532 | {
|
---|
[33595] | 533 | LogRel(("VDI: uuid of modifier is 0\n"));
|
---|
[150] | 534 | fFailed = true;
|
---|
[124] | 535 | }
|
---|
[1] | 536 |
|
---|
[15366] | 537 | return fFailed ? VERR_VD_VDI_INVALID_HEADER : VINF_SUCCESS;
|
---|
[1] | 538 | }
|
---|
| 539 |
|
---|
| 540 | /**
|
---|
[7159] | 541 | * Internal: Set up VDIIMAGEDESC structure by image header.
|
---|
[1] | 542 | */
|
---|
| 543 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
|
---|
| 544 | {
|
---|
[7159] | 545 | pImage->uImageFlags = getImageFlags(&pImage->Header);
|
---|
[17970] | 546 | pImage->uImageFlags |= vdiTranslateVDI2ImageFlags(getImageType(&pImage->Header));
|
---|
[139] | 547 | pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
|
---|
| 548 | pImage->offStartData = getImageDataOffset(&pImage->Header);
|
---|
| 549 | pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
|
---|
| 550 | pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
|
---|
| 551 | pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
|
---|
[79742] | 552 | pImage->cbAllocationBlock = getImageBlockSize(&pImage->Header);
|
---|
| 553 | pImage->cbTotalBlockData = pImage->offStartBlockData
|
---|
[7231] | 554 | + getImageBlockSize(&pImage->Header);
|
---|
[1] | 555 | }
|
---|
| 556 |
|
---|
| 557 | /**
|
---|
[63809] | 558 | * Sets up the complete image state from the given parameters.
|
---|
| 559 | *
|
---|
| 560 | * @returns VBox status code.
|
---|
[77607] | 561 | * @param pImage The VDI image descriptor.
|
---|
| 562 | * @param uImageFlags Image flags.
|
---|
| 563 | * @param pszComment The comment for the image (optional).
|
---|
| 564 | * @param cbSize Size of the resulting image in bytes.
|
---|
| 565 | * @param cbAllocationBlock Size of blocks allocated
|
---|
| 566 | * @param cbDataAlign Data alignment in bytes.
|
---|
| 567 | * @param pPCHSGeometry Physical CHS geometry for the image.
|
---|
| 568 | * @param pLCHSGeometry Logical CHS geometry for the image.
|
---|
[63809] | 569 | */
|
---|
| 570 | static int vdiSetupImageState(PVDIIMAGEDESC pImage, unsigned uImageFlags, const char *pszComment,
|
---|
[77606] | 571 | uint64_t cbSize, uint32_t cbAllocationBlock, uint32_t cbDataAlign, PCVDGEOMETRY pPCHSGeometry,
|
---|
[63809] | 572 | PCVDGEOMETRY pLCHSGeometry)
|
---|
| 573 | {
|
---|
| 574 | int rc = VINF_SUCCESS;
|
---|
| 575 |
|
---|
| 576 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
[77606] | 577 | vdiInitHeader(&pImage->Header, uImageFlags, pszComment, cbSize, cbAllocationBlock, 0,
|
---|
[63809] | 578 | cbDataAlign);
|
---|
| 579 | /* Save PCHS geometry. Not much work, and makes the flow of information
|
---|
| 580 | * quite a bit clearer - relying on the higher level isn't obvious. */
|
---|
| 581 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
| 582 | /* Set LCHS geometry (legacy geometry is ignored for the current 1.1+). */
|
---|
| 583 | pImage->Header.u.v1plus.LCHSGeometry.cCylinders = pLCHSGeometry->cCylinders;
|
---|
| 584 | pImage->Header.u.v1plus.LCHSGeometry.cHeads = pLCHSGeometry->cHeads;
|
---|
| 585 | pImage->Header.u.v1plus.LCHSGeometry.cSectors = pLCHSGeometry->cSectors;
|
---|
| 586 | pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
| 587 |
|
---|
| 588 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
| 589 | if (RT_LIKELY(pImage->paBlocks))
|
---|
| 590 | {
|
---|
| 591 | if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
| 592 | {
|
---|
| 593 | /* for growing images mark all blocks in paBlocks as free. */
|
---|
| 594 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
| 595 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 596 | }
|
---|
| 597 | else
|
---|
| 598 | {
|
---|
| 599 | /* for fixed images mark all blocks in paBlocks as allocated */
|
---|
| 600 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
| 601 | pImage->paBlocks[i] = i;
|
---|
| 602 | pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | /* Setup image parameters. */
|
---|
| 606 | vdiSetupImageDesc(pImage);
|
---|
| 607 | }
|
---|
| 608 | else
|
---|
| 609 | rc = VERR_NO_MEMORY;
|
---|
| 610 |
|
---|
| 611 | return rc;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | /**
|
---|
| 615 | * Creates the image file from the given descriptor.
|
---|
| 616 | *
|
---|
| 617 | * @returns VBox status code.
|
---|
| 618 | * @param pImage The VDI image descriptor.
|
---|
| 619 | * @param uOpenFlags Open flags.
|
---|
| 620 | * @param pIfProgress The progress interface.
|
---|
| 621 | * @param uPercentStart Progress starting point.
|
---|
| 622 | * @param uPercentSpan How many percent for this part of the operation is used.
|
---|
| 623 | */
|
---|
| 624 | static int vdiImageCreateFile(PVDIIMAGEDESC pImage, unsigned uOpenFlags,
|
---|
| 625 | PVDINTERFACEPROGRESS pIfProgress, unsigned uPercentStart,
|
---|
| 626 | unsigned uPercentSpan)
|
---|
| 627 | {
|
---|
| 628 | int rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
|
---|
| 629 | VDOpenFlagsToFileOpenFlags(uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
| 630 | true /* fCreate */),
|
---|
| 631 | &pImage->pStorage);
|
---|
| 632 | if (RT_SUCCESS(rc))
|
---|
| 633 | {
|
---|
| 634 | if (pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
| 635 | {
|
---|
| 636 | uint64_t cbTotal = pImage->offStartData
|
---|
| 637 | + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
|
---|
| 638 |
|
---|
[63887] | 639 | /* Check the free space on the disk and leave early if there is not
|
---|
| 640 | * sufficient space available. */
|
---|
| 641 | int64_t cbFree = 0;
|
---|
| 642 | rc = vdIfIoIntFileGetFreeSpace(pImage->pIfIo, pImage->pszFilename, &cbFree);
|
---|
| 643 | if (RT_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbTotal))
|
---|
| 644 | rc = vdIfError(pImage->pIfError, VERR_DISK_FULL, RT_SRC_POS,
|
---|
| 645 | N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
|
---|
| 646 | else
|
---|
| 647 | {
|
---|
| 648 | /*
|
---|
| 649 | * Allocate & commit whole file if fixed image, it must be more
|
---|
| 650 | * effective than expanding file by write operations.
|
---|
| 651 | */
|
---|
| 652 | rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pImage->pStorage, cbTotal, 0 /* fFlags */,
|
---|
| 653 | pIfProgress, uPercentStart, uPercentSpan);
|
---|
| 654 | pImage->cbImage = cbTotal;
|
---|
| 655 | }
|
---|
[63809] | 656 | }
|
---|
| 657 | else
|
---|
| 658 | {
|
---|
| 659 | /* Set file size to hold header and blocks array. */
|
---|
| 660 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->offStartData);
|
---|
| 661 | pImage->cbImage = pImage->offStartData;
|
---|
| 662 | }
|
---|
| 663 | if (RT_SUCCESS(rc))
|
---|
| 664 | {
|
---|
| 665 | /* Write pre-header. */
|
---|
| 666 | VDIPREHEADER PreHeader;
|
---|
| 667 | vdiConvPreHeaderEndianess(VDIECONV_H2F, &PreHeader, &pImage->PreHeader);
|
---|
| 668 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0,
|
---|
| 669 | &PreHeader, sizeof(PreHeader));
|
---|
| 670 | if (RT_SUCCESS(rc))
|
---|
| 671 | {
|
---|
| 672 | /* Write header. */
|
---|
| 673 | VDIHEADER1PLUS Hdr;
|
---|
| 674 | vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);
|
---|
| 675 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
|
---|
| 676 | &Hdr, sizeof(Hdr));
|
---|
| 677 | if (RT_SUCCESS(rc))
|
---|
| 678 | {
|
---|
| 679 | vdiConvBlocksEndianess(VDIECONV_H2F, pImage->paBlocks, getImageBlocks(&pImage->Header));
|
---|
| 680 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks, pImage->paBlocks,
|
---|
| 681 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
| 682 | vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, getImageBlocks(&pImage->Header));
|
---|
| 683 | if (RT_FAILURE(rc))
|
---|
| 684 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"),
|
---|
| 685 | pImage->pszFilename);
|
---|
| 686 | }
|
---|
| 687 | else
|
---|
| 688 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"),
|
---|
| 689 | pImage->pszFilename);
|
---|
| 690 | }
|
---|
| 691 | else
|
---|
| 692 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"),
|
---|
| 693 | pImage->pszFilename);
|
---|
| 694 | }
|
---|
| 695 | else
|
---|
| 696 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"),
|
---|
| 697 | pImage->pszFilename);
|
---|
| 698 | }
|
---|
| 699 | else
|
---|
| 700 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: cannot create image '%s'"),
|
---|
| 701 | pImage->pszFilename);
|
---|
| 702 |
|
---|
| 703 | return rc;
|
---|
| 704 | }
|
---|
| 705 |
|
---|
| 706 | /**
|
---|
[7159] | 707 | * Internal: Create VDI image file.
|
---|
[1] | 708 | */
|
---|
[17970] | 709 | static int vdiCreateImage(PVDIIMAGEDESC pImage, uint64_t cbSize,
|
---|
| 710 | unsigned uImageFlags, const char *pszComment,
|
---|
[63809] | 711 | PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
|
---|
| 712 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
| 713 | PVDINTERFACEPROGRESS pIfProgress, unsigned uPercentStart,
|
---|
[43602] | 714 | unsigned uPercentSpan, PVDINTERFACECONFIG pIfCfg)
|
---|
[1] | 715 | {
|
---|
[63809] | 716 | int rc = VINF_SUCCESS;
|
---|
[43602] | 717 | uint32_t cbDataAlign = VDI_DATA_ALIGN;
|
---|
[38469] | 718 | AssertPtr(pPCHSGeometry);
|
---|
| 719 | AssertPtr(pLCHSGeometry);
|
---|
| 720 |
|
---|
| 721 | pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
|
---|
| 722 | pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
|
---|
| 723 | AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
|
---|
| 724 |
|
---|
[1] | 725 | /* Special check for comment length. */
|
---|
[90802] | 726 | if ( RT_VALID_PTR(pszComment)
|
---|
[7159] | 727 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
[38469] | 728 | rc = vdIfError(pImage->pIfError, VERR_VD_VDI_COMMENT_TOO_LONG, RT_SRC_POS,
|
---|
| 729 | N_("VDI: comment is too long for '%s'"), pImage->pszFilename);
|
---|
[1] | 730 |
|
---|
[77606] | 731 | PVDINTERFACECONFIG pImgCfg = VDIfConfigGet(pImage->pVDIfsImage);
|
---|
[77622] | 732 | if (pImgCfg)
|
---|
| 733 | {
|
---|
[79742] | 734 | rc = VDCFGQueryU32Def(pImgCfg, "AllocationBlockSize",
|
---|
| 735 | &pImage->cbAllocationBlock, VDI_IMAGE_DEFAULT_BLOCK_SIZE);
|
---|
[77606] | 736 | if (RT_FAILURE(rc))
|
---|
| 737 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
|
---|
| 738 | N_("VDI: Getting AllocationBlockSize for '%s' failed (%Rrc)"), pImage->pszFilename, rc);
|
---|
[79967] | 739 | } else
|
---|
| 740 | pImage->cbAllocationBlock = VDI_IMAGE_DEFAULT_BLOCK_SIZE;
|
---|
[77606] | 741 |
|
---|
[43602] | 742 | if (pIfCfg)
|
---|
| 743 | {
|
---|
| 744 | rc = VDCFGQueryU32Def(pIfCfg, "DataAlignment", &cbDataAlign, VDI_DATA_ALIGN);
|
---|
| 745 | if (RT_FAILURE(rc))
|
---|
| 746 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
|
---|
[57007] | 747 | N_("VDI: Getting data alignment for '%s' failed (%Rrc)"), pImage->pszFilename, rc);
|
---|
[43602] | 748 | }
|
---|
| 749 |
|
---|
[63809] | 750 | if (RT_SUCCESS(rc))
|
---|
[1] | 751 | {
|
---|
[77606] | 752 |
|
---|
| 753 | rc = vdiSetupImageState(pImage, uImageFlags, pszComment, cbSize,
|
---|
[79742] | 754 | pImage->cbAllocationBlock, cbDataAlign, pPCHSGeometry, pLCHSGeometry);
|
---|
| 755 |
|
---|
[63809] | 756 | if (RT_SUCCESS(rc))
|
---|
| 757 | {
|
---|
| 758 | /* Use specified image uuid */
|
---|
| 759 | *getImageCreationUUID(&pImage->Header) = *pUuid;
|
---|
| 760 | /* Generate image last-modify uuid */
|
---|
| 761 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
| 762 |
|
---|
| 763 | rc = vdiImageCreateFile(pImage, uOpenFlags, pIfProgress,
|
---|
| 764 | uPercentStart, uPercentSpan);
|
---|
| 765 | }
|
---|
[1] | 766 | }
|
---|
| 767 |
|
---|
[63809] | 768 | if (RT_SUCCESS(rc))
|
---|
[66486] | 769 | {
|
---|
| 770 | PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
|
---|
| 771 | pImage->RegionList.fFlags = 0;
|
---|
| 772 | pImage->RegionList.cRegions = 1;
|
---|
| 773 |
|
---|
| 774 | pRegion->offRegion = 0; /* Disk start. */
|
---|
| 775 | pRegion->cbBlock = 512;
|
---|
| 776 | pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
|
---|
| 777 | pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
|
---|
| 778 | pRegion->cbData = 512;
|
---|
| 779 | pRegion->cbMetadata = 0;
|
---|
| 780 | pRegion->cRegionBlocksOrBytes = getImageDiskSize(&pImage->Header);
|
---|
| 781 |
|
---|
[63809] | 782 | vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
|
---|
[66486] | 783 | }
|
---|
[1] | 784 |
|
---|
[11266] | 785 | if (RT_FAILURE(rc))
|
---|
[63809] | 786 | vdiFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
|
---|
| 787 | return rc;
|
---|
| 788 | }
|
---|
[6481] | 789 |
|
---|
[63809] | 790 | /**
|
---|
| 791 | * Reads and validates the header for the given image descriptor.
|
---|
| 792 | *
|
---|
| 793 | * @returns VBox status code.
|
---|
| 794 | * @param pImage The VDI image descriptor.
|
---|
| 795 | */
|
---|
| 796 | static int vdiImageReadHeader(PVDIIMAGEDESC pImage)
|
---|
| 797 | {
|
---|
| 798 | /* Get file size. */
|
---|
| 799 | int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage,
|
---|
| 800 | &pImage->cbImage);
|
---|
| 801 | if (RT_SUCCESS(rc))
|
---|
[7159] | 802 | {
|
---|
[63809] | 803 | /* Read pre-header. */
|
---|
| 804 | VDIPREHEADER PreHeader;
|
---|
| 805 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0,
|
---|
| 806 | &PreHeader, sizeof(PreHeader));
|
---|
| 807 | if (RT_SUCCESS(rc))
|
---|
[1] | 808 | {
|
---|
[63809] | 809 | vdiConvPreHeaderEndianess(VDIECONV_F2H, &pImage->PreHeader, &PreHeader);
|
---|
| 810 | rc = vdiValidatePreHeader(&pImage->PreHeader);
|
---|
| 811 | if (RT_SUCCESS(rc))
|
---|
| 812 | {
|
---|
| 813 | /* Read header. */
|
---|
| 814 | pImage->Header.uVersion = pImage->PreHeader.u32Version;
|
---|
| 815 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
| 816 | {
|
---|
| 817 | case 0:
|
---|
| 818 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
|
---|
| 819 | &pImage->Header.u.v0, sizeof(pImage->Header.u.v0));
|
---|
| 820 | if (RT_SUCCESS(rc))
|
---|
| 821 | vdiConvHeaderEndianessV0(VDIECONV_F2H, &pImage->Header.u.v0, &pImage->Header.u.v0);
|
---|
| 822 | else
|
---|
| 823 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
|
---|
| 824 | break;
|
---|
| 825 | case 1:
|
---|
| 826 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
|
---|
| 827 | &pImage->Header.u.v1, sizeof(pImage->Header.u.v1));
|
---|
| 828 | if (RT_SUCCESS(rc))
|
---|
| 829 | {
|
---|
| 830 | vdiConvHeaderEndianessV1(VDIECONV_F2H, &pImage->Header.u.v1, &pImage->Header.u.v1);
|
---|
| 831 | /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write mode.
|
---|
| 832 | * Conversion is harmless, as any VirtualBox version supporting VDI
|
---|
| 833 | * 1.1 doesn't touch fields it doesn't know about. */
|
---|
| 834 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
| 835 | && GET_MINOR_HEADER_VERSION(&pImage->Header) == 1
|
---|
| 836 | && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
| 837 | {
|
---|
| 838 | pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
|
---|
| 839 | /* Mark LCHS geometry not-calculated. */
|
---|
| 840 | pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
| 841 | pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
| 842 | pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
| 843 | pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
| 844 | }
|
---|
| 845 | else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
|
---|
| 846 | {
|
---|
| 847 | /* Read the actual VDI 1.1+ header completely. */
|
---|
| 848 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
|
---|
| 849 | &pImage->Header.u.v1plus,
|
---|
| 850 | sizeof(pImage->Header.u.v1plus));
|
---|
| 851 | if (RT_SUCCESS(rc))
|
---|
| 852 | vdiConvHeaderEndianessV1p(VDIECONV_F2H, &pImage->Header.u.v1plus, &pImage->Header.u.v1plus);
|
---|
| 853 | else
|
---|
| 854 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
|
---|
| 855 | }
|
---|
| 856 | }
|
---|
| 857 | else
|
---|
| 858 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
|
---|
| 859 | break;
|
---|
| 860 | default:
|
---|
| 861 | rc = vdIfError(pImage->pIfError, VERR_VD_VDI_UNSUPPORTED_VERSION, RT_SRC_POS,
|
---|
| 862 | N_("VDI: unsupported major version %u in '%s'"), GET_MAJOR_HEADER_VERSION(&pImage->Header), pImage->pszFilename);
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | if (RT_SUCCESS(rc))
|
---|
| 866 | {
|
---|
| 867 | rc = vdiValidateHeader(&pImage->Header);
|
---|
| 868 | if (RT_SUCCESS(rc))
|
---|
| 869 | {
|
---|
| 870 | /* Setup image parameters by header. */
|
---|
| 871 | vdiSetupImageDesc(pImage);
|
---|
[64784] | 872 |
|
---|
| 873 | /*
|
---|
| 874 | * Until revision r111992 there was no check that the size was sector aligned
|
---|
| 875 | * when creating a new image and a bug in the VirtualBox GUI on OS X resulted
|
---|
| 876 | * in such images being created which caused issues when writing to the
|
---|
| 877 | * end of the image.
|
---|
| 878 | *
|
---|
| 879 | * Detect such images and repair the small damage by rounding down to the next
|
---|
| 880 | * aligned size. This is no problem as the guest would see a sector count
|
---|
| 881 | * only anyway from the device emulations so it already sees only the smaller
|
---|
| 882 | * size as result of the integer division of the size and sector size.
|
---|
| 883 | *
|
---|
| 884 | * This might not be written to the image if it is opened readonly
|
---|
| 885 | * which is not much of a problem because only writing to the last block
|
---|
| 886 | * causes trouble.
|
---|
| 887 | */
|
---|
| 888 | uint64_t cbDisk = getImageDiskSize(&pImage->Header);
|
---|
| 889 | if (cbDisk & 0x1ff)
|
---|
| 890 | setImageDiskSize(&pImage->Header, cbDisk & ~UINT64_C(0x1ff));
|
---|
[63809] | 891 | }
|
---|
| 892 | else
|
---|
| 893 | rc = vdIfError(pImage->pIfError, VERR_VD_VDI_INVALID_HEADER, RT_SRC_POS,
|
---|
| 894 | N_("VDI: invalid header in '%s'"), pImage->pszFilename);
|
---|
| 895 | }
|
---|
| 896 | }
|
---|
| 897 | else
|
---|
| 898 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
|
---|
[1] | 899 | }
|
---|
[63809] | 900 | else
|
---|
| 901 | {
|
---|
| 902 | vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
|
---|
| 903 | rc = VERR_VD_VDI_INVALID_HEADER;
|
---|
| 904 | }
|
---|
[7159] | 905 | }
|
---|
| 906 | else
|
---|
| 907 | {
|
---|
[63809] | 908 | vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error getting the image size in '%s'"), pImage->pszFilename);
|
---|
| 909 | rc = VERR_VD_VDI_INVALID_HEADER;
|
---|
[7159] | 910 | }
|
---|
[1] | 911 |
|
---|
[63809] | 912 | return rc;
|
---|
| 913 | }
|
---|
[11353] | 914 |
|
---|
[63809] | 915 | /**
|
---|
| 916 | * Creates the back resolving table for the image for the discard operation.
|
---|
| 917 | *
|
---|
| 918 | * @returns VBox status code.
|
---|
| 919 | * @param pImage The VDI image descriptor.
|
---|
| 920 | */
|
---|
| 921 | static int vdiImageBackResolvTblCreate(PVDIIMAGEDESC pImage)
|
---|
| 922 | {
|
---|
| 923 | int rc = VINF_SUCCESS;
|
---|
[1] | 924 |
|
---|
[63809] | 925 | /*
|
---|
| 926 | * Any error or inconsistency results in a fail because this might
|
---|
| 927 | * get us into trouble later on.
|
---|
| 928 | */
|
---|
| 929 | pImage->paBlocksRev = (unsigned *)RTMemAllocZ(sizeof(unsigned) * getImageBlocks(&pImage->Header));
|
---|
| 930 | if (pImage->paBlocksRev)
|
---|
[7159] | 931 | {
|
---|
[63809] | 932 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
| 933 | unsigned cBlocks = getImageBlocks(&pImage->Header);
|
---|
[1] | 934 |
|
---|
[63809] | 935 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
| 936 | pImage->paBlocksRev[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
[1] | 937 |
|
---|
[63809] | 938 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
| 939 | {
|
---|
| 940 | VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
|
---|
| 941 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
|
---|
| 942 | {
|
---|
| 943 | if (ptrBlock < cBlocksAllocated)
|
---|
| 944 | {
|
---|
| 945 | if (pImage->paBlocksRev[ptrBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
| 946 | pImage->paBlocksRev[ptrBlock] = i;
|
---|
| 947 | else
|
---|
| 948 | {
|
---|
| 949 | rc = VERR_VD_VDI_INVALID_HEADER;
|
---|
| 950 | break;
|
---|
| 951 | }
|
---|
| 952 | }
|
---|
| 953 | else
|
---|
| 954 | {
|
---|
| 955 | rc = VERR_VD_VDI_INVALID_HEADER;
|
---|
| 956 | break;
|
---|
| 957 | }
|
---|
| 958 | }
|
---|
| 959 | }
|
---|
[7159] | 960 | }
|
---|
[63809] | 961 | else
|
---|
| 962 | rc = VERR_NO_MEMORY;
|
---|
[1] | 963 |
|
---|
| 964 | return rc;
|
---|
| 965 | }
|
---|
| 966 |
|
---|
| 967 | /**
|
---|
[7159] | 968 | * Internal: Open a VDI image.
|
---|
[1] | 969 | */
|
---|
[7159] | 970 | static int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
|
---|
[1] | 971 | {
|
---|
[7159] | 972 | pImage->uOpenFlags = uOpenFlags;
|
---|
[1] | 973 |
|
---|
[38469] | 974 | pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
|
---|
| 975 | pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
|
---|
| 976 | AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
|
---|
[11435] | 977 |
|
---|
[1] | 978 | /*
|
---|
| 979 | * Open the image.
|
---|
| 980 | */
|
---|
[63809] | 981 | int rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
|
---|
| 982 | VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */),
|
---|
| 983 | &pImage->pStorage);
|
---|
| 984 | if (RT_SUCCESS(rc))
|
---|
[1] | 985 | {
|
---|
[63809] | 986 | rc = vdiImageReadHeader(pImage);
|
---|
| 987 | if (RT_SUCCESS(rc))
|
---|
| 988 | {
|
---|
| 989 | /* Allocate memory for blocks array. */
|
---|
| 990 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
| 991 | if (RT_LIKELY(pImage->paBlocks))
|
---|
[7159] | 992 | {
|
---|
[63809] | 993 | /* Read blocks array. */
|
---|
| 994 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks, pImage->paBlocks,
|
---|
| 995 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
| 996 | if (RT_SUCCESS(rc))
|
---|
[7159] | 997 | {
|
---|
[63809] | 998 | vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, getImageBlocks(&pImage->Header));
|
---|
[139] | 999 |
|
---|
[63809] | 1000 | if (uOpenFlags & VD_OPEN_FLAGS_DISCARD)
|
---|
| 1001 | rc = vdiImageBackResolvTblCreate(pImage);
|
---|
[38621] | 1002 | }
|
---|
[63809] | 1003 | else
|
---|
| 1004 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: Error reading the block table in '%s'"), pImage->pszFilename);
|
---|
[38621] | 1005 | }
|
---|
[63809] | 1006 | else
|
---|
| 1007 | rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
|
---|
| 1008 | N_("VDI: Error allocating memory for the block table in '%s'"), pImage->pszFilename);;
|
---|
[38621] | 1009 | }
|
---|
| 1010 | }
|
---|
[63809] | 1011 | /* else: Do NOT signal an appropriate error here, as the VD layer has the
|
---|
| 1012 | * choice of retrying the open if it failed. */
|
---|
[38621] | 1013 |
|
---|
[66486] | 1014 | if (RT_SUCCESS(rc))
|
---|
| 1015 | {
|
---|
| 1016 | PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
|
---|
| 1017 | pImage->RegionList.fFlags = 0;
|
---|
| 1018 | pImage->RegionList.cRegions = 1;
|
---|
| 1019 |
|
---|
| 1020 | pRegion->offRegion = 0; /* Disk start. */
|
---|
| 1021 | pRegion->cbBlock = 512;
|
---|
| 1022 | pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
|
---|
| 1023 | pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
|
---|
| 1024 | pRegion->cbData = 512;
|
---|
| 1025 | pRegion->cbMetadata = 0;
|
---|
| 1026 | pRegion->cRegionBlocksOrBytes = getImageDiskSize(&pImage->Header);
|
---|
[79742] | 1027 | if (uOpenFlags & VD_OPEN_FLAGS_INFO)
|
---|
| 1028 | {
|
---|
| 1029 | PVDINTERFACECONFIG pImgCfg = VDIfConfigGet(pImage->pVDIfsImage);
|
---|
| 1030 | if (pImgCfg)
|
---|
| 1031 | {
|
---|
| 1032 | rc = VDCFGUpdateU64(pImgCfg, true, "AllocationBlockSize", pImage->cbAllocationBlock);
|
---|
| 1033 | if (RT_FAILURE(rc))
|
---|
| 1034 | return rc;
|
---|
| 1035 | }
|
---|
| 1036 | }
|
---|
[66486] | 1037 | }
|
---|
| 1038 | else
|
---|
[7159] | 1039 | vdiFreeImage(pImage, false);
|
---|
[1] | 1040 | return rc;
|
---|
| 1041 | }
|
---|
| 1042 |
|
---|
| 1043 | /**
|
---|
[7159] | 1044 | * Internal: Save header to file.
|
---|
[1] | 1045 | */
|
---|
| 1046 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
|
---|
| 1047 | {
|
---|
[7159] | 1048 | int rc;
|
---|
| 1049 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
[1] | 1050 | {
|
---|
[7159] | 1051 | case 0:
|
---|
[40936] | 1052 | {
|
---|
| 1053 | VDIHEADER0 Hdr;
|
---|
| 1054 | vdiConvHeaderEndianessV0(VDIECONV_H2F, &Hdr, &pImage->Header.u.v0);
|
---|
[38469] | 1055 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER),
|
---|
[44233] | 1056 | &Hdr, sizeof(Hdr));
|
---|
[7159] | 1057 | break;
|
---|
[40936] | 1058 | }
|
---|
[7159] | 1059 | case 1:
|
---|
| 1060 | if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
[40936] | 1061 | {
|
---|
| 1062 | VDIHEADER1 Hdr;
|
---|
| 1063 | vdiConvHeaderEndianessV1(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1);
|
---|
[38469] | 1064 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER),
|
---|
[44233] | 1065 | &Hdr, sizeof(Hdr));
|
---|
[40936] | 1066 | }
|
---|
[7159] | 1067 | else
|
---|
[40936] | 1068 | {
|
---|
| 1069 | VDIHEADER1PLUS Hdr;
|
---|
| 1070 | vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);
|
---|
[38469] | 1071 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER),
|
---|
[44233] | 1072 | &Hdr, sizeof(Hdr));
|
---|
[40936] | 1073 | }
|
---|
[7159] | 1074 | break;
|
---|
| 1075 | default:
|
---|
[15366] | 1076 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
[7159] | 1077 | break;
|
---|
[1] | 1078 | }
|
---|
[11284] | 1079 | AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
|
---|
[1] | 1080 | return rc;
|
---|
| 1081 | }
|
---|
| 1082 |
|
---|
| 1083 | /**
|
---|
[28065] | 1084 | * Internal: Save header to file - async version.
|
---|
| 1085 | */
|
---|
| 1086 | static int vdiUpdateHeaderAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx)
|
---|
| 1087 | {
|
---|
| 1088 | int rc;
|
---|
| 1089 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
| 1090 | {
|
---|
| 1091 | case 0:
|
---|
[40936] | 1092 | {
|
---|
| 1093 | VDIHEADER0 Hdr;
|
---|
| 1094 | vdiConvHeaderEndianessV0(VDIECONV_H2F, &Hdr, &pImage->Header.u.v0);
|
---|
[44233] | 1095 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
| 1096 | sizeof(VDIPREHEADER), &Hdr, sizeof(Hdr),
|
---|
| 1097 | pIoCtx, NULL, NULL);
|
---|
[28065] | 1098 | break;
|
---|
[40936] | 1099 | }
|
---|
[28065] | 1100 | case 1:
|
---|
| 1101 | if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
[40936] | 1102 | {
|
---|
| 1103 | VDIHEADER1 Hdr;
|
---|
| 1104 | vdiConvHeaderEndianessV1(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1);
|
---|
[44233] | 1105 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
| 1106 | sizeof(VDIPREHEADER), &Hdr, sizeof(Hdr),
|
---|
| 1107 | pIoCtx, NULL, NULL);
|
---|
[40936] | 1108 | }
|
---|
[28065] | 1109 | else
|
---|
[40936] | 1110 | {
|
---|
| 1111 | VDIHEADER1PLUS Hdr;
|
---|
| 1112 | vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);
|
---|
[44233] | 1113 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
| 1114 | sizeof(VDIPREHEADER), &Hdr, sizeof(Hdr),
|
---|
| 1115 | pIoCtx, NULL, NULL);
|
---|
[40936] | 1116 | }
|
---|
[28065] | 1117 | break;
|
---|
| 1118 | default:
|
---|
| 1119 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
| 1120 | break;
|
---|
| 1121 | }
|
---|
[30555] | 1122 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS,
|
---|
| 1123 | ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
|
---|
[28065] | 1124 | return rc;
|
---|
| 1125 | }
|
---|
| 1126 |
|
---|
| 1127 | /**
|
---|
[7159] | 1128 | * Internal: Save block pointer to file, save header to file.
|
---|
[1] | 1129 | */
|
---|
| 1130 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
| 1131 | {
|
---|
| 1132 | /* Update image header. */
|
---|
| 1133 | int rc = vdiUpdateHeader(pImage);
|
---|
[11266] | 1134 | if (RT_SUCCESS(rc))
|
---|
[1] | 1135 | {
|
---|
| 1136 | /* write only one block pointer. */
|
---|
[40936] | 1137 | VDIIMAGEBLOCKPOINTER ptrBlock = RT_H2LE_U32(pImage->paBlocks[uBlock]);
|
---|
[38469] | 1138 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
| 1139 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
[44233] | 1140 | &ptrBlock, sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
[11284] | 1141 | AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n",
|
---|
[7159] | 1142 | uBlock, pImage->pszFilename, rc));
|
---|
[1] | 1143 | }
|
---|
| 1144 | return rc;
|
---|
| 1145 | }
|
---|
| 1146 |
|
---|
| 1147 | /**
|
---|
[28065] | 1148 | * Internal: Save block pointer to file, save header to file - async version.
|
---|
| 1149 | */
|
---|
| 1150 | static int vdiUpdateBlockInfoAsync(PVDIIMAGEDESC pImage, unsigned uBlock,
|
---|
[38876] | 1151 | PVDIOCTX pIoCtx, bool fUpdateHdr)
|
---|
[28065] | 1152 | {
|
---|
[38876] | 1153 | int rc = VINF_SUCCESS;
|
---|
| 1154 |
|
---|
[28065] | 1155 | /* Update image header. */
|
---|
[38876] | 1156 | if (fUpdateHdr)
|
---|
| 1157 | rc = vdiUpdateHeaderAsync(pImage, pIoCtx);
|
---|
| 1158 |
|
---|
[30555] | 1159 | if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
[28065] | 1160 | {
|
---|
| 1161 | /* write only one block pointer. */
|
---|
[40936] | 1162 | VDIIMAGEBLOCKPOINTER ptrBlock = RT_H2LE_U32(pImage->paBlocks[uBlock]);
|
---|
[44233] | 1163 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
| 1164 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
| 1165 | &ptrBlock, sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
| 1166 | pIoCtx, NULL, NULL);
|
---|
[30555] | 1167 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS,
|
---|
| 1168 | ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n",
|
---|
| 1169 | uBlock, pImage->pszFilename, rc));
|
---|
[28065] | 1170 | }
|
---|
| 1171 | return rc;
|
---|
| 1172 | }
|
---|
| 1173 |
|
---|
| 1174 | /**
|
---|
[28383] | 1175 | * Internal: Flush the image file to disk - async version.
|
---|
| 1176 | */
|
---|
[44252] | 1177 | static int vdiFlushImageIoCtx(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx)
|
---|
[28383] | 1178 | {
|
---|
[30555] | 1179 | int rc = VINF_SUCCESS;
|
---|
| 1180 |
|
---|
[28383] | 1181 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
| 1182 | {
|
---|
| 1183 | /* Save header. */
|
---|
[30555] | 1184 | rc = vdiUpdateHeaderAsync(pImage, pIoCtx);
|
---|
| 1185 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS,
|
---|
| 1186 | ("vdiUpdateHeaderAsync() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
| 1187 | pImage->pszFilename, rc));
|
---|
[44233] | 1188 | rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage, pIoCtx, NULL, NULL);
|
---|
[30555] | 1189 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS,
|
---|
| 1190 | ("Flushing data to disk failed rc=%Rrc\n", rc));
|
---|
[28383] | 1191 | }
|
---|
[30555] | 1192 |
|
---|
| 1193 | return rc;
|
---|
[28383] | 1194 | }
|
---|
| 1195 |
|
---|
[38657] | 1196 | /**
|
---|
[38876] | 1197 | * Completion callback for meta/userdata reads or writes.
|
---|
| 1198 | *
|
---|
| 1199 | * @return VBox status code.
|
---|
| 1200 | * VINF_SUCCESS if everything was successful and the transfer can continue.
|
---|
| 1201 | * VERR_VD_ASYNC_IO_IN_PROGRESS if there is another data transfer pending.
|
---|
| 1202 | * @param pBackendData The opaque backend data.
|
---|
| 1203 | * @param pIoCtx I/O context associated with this request.
|
---|
| 1204 | * @param pvUser Opaque user data passed during a read/write request.
|
---|
| 1205 | * @param rcReq Status code for the completed request.
|
---|
| 1206 | */
|
---|
| 1207 | static DECLCALLBACK(int) vdiDiscardBlockAsyncUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
|
---|
| 1208 | {
|
---|
[62742] | 1209 | RT_NOREF1(rcReq);
|
---|
[38876] | 1210 | int rc = VINF_SUCCESS;
|
---|
| 1211 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1212 | PVDIBLOCKDISCARDASYNC pDiscardAsync = (PVDIBLOCKDISCARDASYNC)pvUser;
|
---|
| 1213 |
|
---|
| 1214 | switch (pDiscardAsync->enmState)
|
---|
| 1215 | {
|
---|
| 1216 | case VDIBLOCKDISCARDSTATE_READ_BLOCK:
|
---|
| 1217 | {
|
---|
| 1218 | PVDMETAXFER pMetaXfer;
|
---|
| 1219 | uint64_t u64Offset = (uint64_t)pDiscardAsync->idxLastBlock * pImage->cbTotalBlockData + pImage->offStartData;
|
---|
[44233] | 1220 | rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
| 1221 | pDiscardAsync->pvBlock, pImage->cbTotalBlockData, pIoCtx,
|
---|
| 1222 | &pMetaXfer, vdiDiscardBlockAsyncUpdate, pDiscardAsync);
|
---|
[38876] | 1223 | if (RT_FAILURE(rc))
|
---|
| 1224 | break;
|
---|
| 1225 |
|
---|
| 1226 | /* Release immediately and go to next step. */
|
---|
| 1227 | vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
|
---|
| 1228 | pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_WRITE_BLOCK;
|
---|
| 1229 | }
|
---|
[69046] | 1230 | RT_FALL_THRU();
|
---|
[38876] | 1231 | case VDIBLOCKDISCARDSTATE_WRITE_BLOCK:
|
---|
| 1232 | {
|
---|
| 1233 | /* Block read complete. Write to the new location (discarded block). */
|
---|
| 1234 | uint64_t u64Offset = (uint64_t)pDiscardAsync->ptrBlockDiscard * pImage->cbTotalBlockData + pImage->offStartData;
|
---|
[44233] | 1235 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
| 1236 | pDiscardAsync->pvBlock, pImage->cbTotalBlockData, pIoCtx,
|
---|
| 1237 | vdiDiscardBlockAsyncUpdate, pDiscardAsync);
|
---|
[38876] | 1238 |
|
---|
| 1239 | pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_UPDATE_METADATA;
|
---|
| 1240 | if (RT_FAILURE(rc))
|
---|
| 1241 | break;
|
---|
| 1242 | }
|
---|
[69046] | 1243 | RT_FALL_THRU();
|
---|
[38876] | 1244 | case VDIBLOCKDISCARDSTATE_UPDATE_METADATA:
|
---|
| 1245 | {
|
---|
| 1246 | int rc2;
|
---|
| 1247 |
|
---|
| 1248 | /* Block write complete. Update metadata. */
|
---|
| 1249 | pImage->paBlocksRev[pDiscardAsync->idxLastBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 1250 | pImage->paBlocks[pDiscardAsync->uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
| 1251 |
|
---|
| 1252 | if (pDiscardAsync->idxLastBlock != pDiscardAsync->ptrBlockDiscard)
|
---|
| 1253 | {
|
---|
| 1254 | pImage->paBlocks[pDiscardAsync->uBlockLast] = pDiscardAsync->ptrBlockDiscard;
|
---|
| 1255 | pImage->paBlocksRev[pDiscardAsync->ptrBlockDiscard] = pDiscardAsync->uBlockLast;
|
---|
| 1256 |
|
---|
| 1257 | rc = vdiUpdateBlockInfoAsync(pImage, pDiscardAsync->uBlockLast, pIoCtx, false /* fUpdateHdr */);
|
---|
| 1258 | if ( RT_FAILURE(rc)
|
---|
| 1259 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
| 1260 | break;
|
---|
| 1261 | }
|
---|
| 1262 |
|
---|
| 1263 | setImageBlocksAllocated(&pImage->Header, pDiscardAsync->idxLastBlock);
|
---|
| 1264 | rc = vdiUpdateBlockInfoAsync(pImage, pDiscardAsync->uBlock, pIoCtx, true /* fUpdateHdr */);
|
---|
| 1265 | if ( RT_FAILURE(rc)
|
---|
| 1266 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
| 1267 | break;
|
---|
| 1268 |
|
---|
[40713] | 1269 | pImage->cbImage -= pImage->cbTotalBlockData;
|
---|
| 1270 | LogFlowFunc(("Set new size %llu\n", pImage->cbImage));
|
---|
| 1271 | rc2 = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->cbImage);
|
---|
[38876] | 1272 | if (RT_FAILURE(rc2))
|
---|
| 1273 | rc = rc2;
|
---|
| 1274 |
|
---|
| 1275 | /* Free discard state. */
|
---|
| 1276 | RTMemFree(pDiscardAsync->pvBlock);
|
---|
| 1277 | RTMemFree(pDiscardAsync);
|
---|
| 1278 | break;
|
---|
| 1279 | }
|
---|
| 1280 | default:
|
---|
| 1281 | AssertMsgFailed(("Invalid state %d\n", pDiscardAsync->enmState));
|
---|
| 1282 | }
|
---|
| 1283 |
|
---|
| 1284 | if (rc == VERR_VD_NOT_ENOUGH_METADATA)
|
---|
| 1285 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
| 1286 |
|
---|
| 1287 | return rc;
|
---|
| 1288 | }
|
---|
| 1289 |
|
---|
| 1290 | /**
|
---|
| 1291 | * Internal: Discard a whole block from the image filling the created hole with
|
---|
| 1292 | * data from another block - async I/O version.
|
---|
| 1293 | *
|
---|
| 1294 | * @returns VBox status code.
|
---|
| 1295 | * @param pImage VDI image instance data.
|
---|
| 1296 | * @param pIoCtx I/O context associated with this request.
|
---|
| 1297 | * @param uBlock The block to discard.
|
---|
| 1298 | * @param pvBlock Memory to use for the I/O.
|
---|
| 1299 | */
|
---|
| 1300 | static int vdiDiscardBlockAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx,
|
---|
| 1301 | unsigned uBlock, void *pvBlock)
|
---|
| 1302 | {
|
---|
| 1303 | int rc = VINF_SUCCESS;
|
---|
| 1304 | PVDIBLOCKDISCARDASYNC pDiscardAsync = NULL;
|
---|
| 1305 |
|
---|
| 1306 | LogFlowFunc(("pImage=%#p uBlock=%u pvBlock=%#p\n",
|
---|
| 1307 | pImage, uBlock, pvBlock));
|
---|
| 1308 |
|
---|
| 1309 | pDiscardAsync = (PVDIBLOCKDISCARDASYNC)RTMemAllocZ(sizeof(VDIBLOCKDISCARDASYNC));
|
---|
| 1310 | if (RT_UNLIKELY(!pDiscardAsync))
|
---|
| 1311 | return VERR_NO_MEMORY;
|
---|
| 1312 |
|
---|
| 1313 | /* Init block discard state. */
|
---|
| 1314 | pDiscardAsync->uBlock = uBlock;
|
---|
| 1315 | pDiscardAsync->pvBlock = pvBlock;
|
---|
| 1316 | pDiscardAsync->ptrBlockDiscard = pImage->paBlocks[uBlock];
|
---|
| 1317 | pDiscardAsync->idxLastBlock = getImageBlocksAllocated(&pImage->Header) - 1;
|
---|
| 1318 | pDiscardAsync->uBlockLast = pImage->paBlocksRev[pDiscardAsync->idxLastBlock];
|
---|
| 1319 |
|
---|
| 1320 | /*
|
---|
| 1321 | * The block is empty, remove it.
|
---|
| 1322 | * Read the last block of the image first.
|
---|
| 1323 | */
|
---|
| 1324 | if (pDiscardAsync->idxLastBlock != pDiscardAsync->ptrBlockDiscard)
|
---|
| 1325 | {
|
---|
| 1326 | LogFlowFunc(("Moving block [%u]=%u into [%u]=%u\n",
|
---|
| 1327 | pDiscardAsync->uBlockLast, pDiscardAsync->idxLastBlock,
|
---|
| 1328 | uBlock, pImage->paBlocks[uBlock]));
|
---|
| 1329 | pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_READ_BLOCK;
|
---|
| 1330 | }
|
---|
| 1331 | else
|
---|
| 1332 | {
|
---|
| 1333 | pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_UPDATE_METADATA; /* Start immediately to shrink the image. */
|
---|
| 1334 | LogFlowFunc(("Discard last block [%u]=%u\n", uBlock, pImage->paBlocks[uBlock]));
|
---|
| 1335 | }
|
---|
| 1336 |
|
---|
| 1337 | /* Call the update callback directly. */
|
---|
| 1338 | rc = vdiDiscardBlockAsyncUpdate(pImage, pIoCtx, pDiscardAsync, VINF_SUCCESS);
|
---|
| 1339 |
|
---|
| 1340 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
| 1341 | return rc;
|
---|
| 1342 | }
|
---|
| 1343 |
|
---|
| 1344 | /**
|
---|
[38657] | 1345 | * Internal: Creates a allocation bitmap from the given data.
|
---|
| 1346 | * Sectors which contain only 0 are marked as unallocated and sectors with
|
---|
| 1347 | * other data as allocated.
|
---|
| 1348 | *
|
---|
| 1349 | * @returns Pointer to the allocation bitmap or NULL on failure.
|
---|
| 1350 | * @param pvData The data to create the allocation bitmap for.
|
---|
| 1351 | * @param cbData Number of bytes in the buffer.
|
---|
| 1352 | */
|
---|
| 1353 | static void *vdiAllocationBitmapCreate(void *pvData, size_t cbData)
|
---|
| 1354 | {
|
---|
[48851] | 1355 | Assert(cbData <= UINT32_MAX / 8);
|
---|
| 1356 | uint32_t cSectors = (uint32_t)(cbData / 512);
|
---|
| 1357 | uint32_t uSectorCur = 0;
|
---|
[38657] | 1358 | void *pbmAllocationBitmap = NULL;
|
---|
[1] | 1359 |
|
---|
[38657] | 1360 | Assert(!(cbData % 512));
|
---|
| 1361 | Assert(!(cSectors % 8));
|
---|
| 1362 |
|
---|
| 1363 | pbmAllocationBitmap = RTMemAllocZ(cSectors / 8);
|
---|
[40713] | 1364 | if (!pbmAllocationBitmap)
|
---|
| 1365 | return NULL;
|
---|
[38657] | 1366 |
|
---|
| 1367 | while (uSectorCur < cSectors)
|
---|
| 1368 | {
|
---|
[48851] | 1369 | int idxSet = ASMBitFirstSet((uint8_t *)pvData + uSectorCur * 512, (uint32_t)cbData * 8);
|
---|
[38657] | 1370 |
|
---|
| 1371 | if (idxSet != -1)
|
---|
| 1372 | {
|
---|
| 1373 | unsigned idxSectorAlloc = idxSet / 8 / 512;
|
---|
| 1374 | ASMBitSet(pbmAllocationBitmap, uSectorCur + idxSectorAlloc);
|
---|
| 1375 |
|
---|
| 1376 | uSectorCur += idxSectorAlloc + 1;
|
---|
| 1377 | cbData -= (idxSectorAlloc + 1) * 512;
|
---|
| 1378 | }
|
---|
| 1379 | else
|
---|
| 1380 | break;
|
---|
| 1381 | }
|
---|
| 1382 |
|
---|
| 1383 | return pbmAllocationBitmap;
|
---|
| 1384 | }
|
---|
| 1385 |
|
---|
| 1386 |
|
---|
[40713] | 1387 | /**
|
---|
| 1388 | * Updates the state of the async cluster allocation.
|
---|
| 1389 | *
|
---|
| 1390 | * @returns VBox status code.
|
---|
| 1391 | * @param pBackendData The opaque backend data.
|
---|
| 1392 | * @param pIoCtx I/O context associated with this request.
|
---|
| 1393 | * @param pvUser Opaque user data passed during a read/write request.
|
---|
| 1394 | * @param rcReq Status code for the completed request.
|
---|
| 1395 | */
|
---|
[44252] | 1396 | static DECLCALLBACK(int) vdiBlockAllocUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
|
---|
[40713] | 1397 | {
|
---|
| 1398 | int rc = VINF_SUCCESS;
|
---|
| 1399 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1400 | PVDIASYNCBLOCKALLOC pBlockAlloc = (PVDIASYNCBLOCKALLOC)pvUser;
|
---|
| 1401 |
|
---|
| 1402 | if (RT_SUCCESS(rcReq))
|
---|
| 1403 | {
|
---|
| 1404 | pImage->cbImage += pImage->cbTotalBlockData;
|
---|
| 1405 | pImage->paBlocks[pBlockAlloc->uBlock] = pBlockAlloc->cBlocksAllocated;
|
---|
| 1406 |
|
---|
| 1407 | if (pImage->paBlocksRev)
|
---|
| 1408 | pImage->paBlocksRev[pBlockAlloc->cBlocksAllocated] = pBlockAlloc->uBlock;
|
---|
| 1409 |
|
---|
| 1410 | setImageBlocksAllocated(&pImage->Header, pBlockAlloc->cBlocksAllocated + 1);
|
---|
| 1411 | rc = vdiUpdateBlockInfoAsync(pImage, pBlockAlloc->uBlock, pIoCtx,
|
---|
| 1412 | true /* fUpdateHdr */);
|
---|
| 1413 | }
|
---|
| 1414 | /* else: I/O error don't update the block table. */
|
---|
| 1415 |
|
---|
| 1416 | RTMemFree(pBlockAlloc);
|
---|
| 1417 | return rc;
|
---|
| 1418 | }
|
---|
| 1419 |
|
---|
[63802] | 1420 | /** @copydoc VDIMAGEBACKEND::pfnProbe */
|
---|
| 1421 | static DECLCALLBACK(int) vdiProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
|
---|
[79965] | 1422 | PVDINTERFACE pVDIfsImage, VDTYPE enmDesiredType, VDTYPE *penmType)
|
---|
[7159] | 1423 | {
|
---|
[79965] | 1424 | RT_NOREF(enmDesiredType);
|
---|
[7159] | 1425 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
| 1426 | int rc = VINF_SUCCESS;
|
---|
[1] | 1427 |
|
---|
[90802] | 1428 | AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
|
---|
| 1429 | AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
|
---|
[1] | 1430 |
|
---|
[90802] | 1431 |
|
---|
[66505] | 1432 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)RTMemAllocZ(RT_UOFFSETOF(VDIIMAGEDESC, RegionList.aRegions[1]));
|
---|
[63809] | 1433 | if (RT_LIKELY(pImage))
|
---|
[1] | 1434 | {
|
---|
[63809] | 1435 | pImage->pszFilename = pszFilename;
|
---|
| 1436 | pImage->pStorage = NULL;
|
---|
| 1437 | pImage->paBlocks = NULL;
|
---|
| 1438 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
| 1439 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
[1] | 1440 |
|
---|
[63809] | 1441 | rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
|
---|
| 1442 | vdiFreeImage(pImage, false);
|
---|
| 1443 | RTMemFree(pImage);
|
---|
[1] | 1444 |
|
---|
[63809] | 1445 | if (RT_SUCCESS(rc))
|
---|
| 1446 | *penmType = VDTYPE_HDD;
|
---|
| 1447 | }
|
---|
| 1448 | else
|
---|
| 1449 | rc = VERR_NO_MEMORY;
|
---|
[33524] | 1450 |
|
---|
[11284] | 1451 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[1] | 1452 | return rc;
|
---|
| 1453 | }
|
---|
| 1454 |
|
---|
[63785] | 1455 | /** @copydoc VDIMAGEBACKEND::pfnOpen */
|
---|
[57388] | 1456 | static DECLCALLBACK(int) vdiOpen(const char *pszFilename, unsigned uOpenFlags,
|
---|
| 1457 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
| 1458 | VDTYPE enmType, void **ppBackendData)
|
---|
[1] | 1459 | {
|
---|
[63809] | 1460 | RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
|
---|
| 1461 |
|
---|
| 1462 | LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
|
---|
| 1463 | pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
|
---|
[1] | 1464 | int rc;
|
---|
| 1465 |
|
---|
[7159] | 1466 | /* Check open flags. All valid flags are supported. */
|
---|
[63809] | 1467 | AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
|
---|
[90802] | 1468 | AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
|
---|
| 1469 | AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
|
---|
[1] | 1470 |
|
---|
[90802] | 1471 |
|
---|
[66486] | 1472 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)RTMemAllocZ(RT_UOFFSETOF(VDIIMAGEDESC, RegionList.aRegions[1]));
|
---|
[63809] | 1473 | if (RT_LIKELY(pImage))
|
---|
[1] | 1474 | {
|
---|
[63809] | 1475 | pImage->pszFilename = pszFilename;
|
---|
| 1476 | pImage->pStorage = NULL;
|
---|
| 1477 | pImage->paBlocks = NULL;
|
---|
| 1478 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
| 1479 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
[1] | 1480 |
|
---|
[63809] | 1481 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
| 1482 | if (RT_SUCCESS(rc))
|
---|
| 1483 | *ppBackendData = pImage;
|
---|
| 1484 | else
|
---|
| 1485 | RTMemFree(pImage);
|
---|
[1] | 1486 | }
|
---|
[40739] | 1487 | else
|
---|
[63809] | 1488 | rc = VERR_NO_MEMORY;
|
---|
[1] | 1489 |
|
---|
[11284] | 1490 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
[1] | 1491 | return rc;
|
---|
| 1492 | }
|
---|
| 1493 |
|
---|
[63785] | 1494 | /** @copydoc VDIMAGEBACKEND::pfnCreate */
|
---|
[57388] | 1495 | static DECLCALLBACK(int) vdiCreate(const char *pszFilename, uint64_t cbSize,
|
---|
| 1496 | unsigned uImageFlags, const char *pszComment,
|
---|
| 1497 | PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
|
---|
| 1498 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
| 1499 | unsigned uPercentStart, unsigned uPercentSpan,
|
---|
| 1500 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
| 1501 | PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
|
---|
| 1502 | void **ppBackendData)
|
---|
[1] | 1503 | {
|
---|
[54430] | 1504 | 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\n",
|
---|
| 1505 | pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
|
---|
[710] | 1506 | int rc;
|
---|
[1] | 1507 |
|
---|
[63809] | 1508 | /* Check the VD container type and image flags. */
|
---|
| 1509 | if ( enmType != VDTYPE_HDD
|
---|
| 1510 | || (uImageFlags & ~VD_VDI_IMAGE_FLAGS_MASK) != 0)
|
---|
| 1511 | return VERR_VD_INVALID_TYPE;
|
---|
[37552] | 1512 |
|
---|
[63809] | 1513 | /* Check size. Maximum 4PB-3M. No tricks with adjusting the 1M block size
|
---|
| 1514 | * so far, which would extend the size. */
|
---|
[103501] | 1515 | if ( cbSize >= _1P * 4 - _1M * 3
|
---|
[64711] | 1516 | || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
|
---|
| 1517 | || (cbSize % 512))
|
---|
[63809] | 1518 | return VERR_VD_INVALID_SIZE;
|
---|
[43602] | 1519 |
|
---|
[7159] | 1520 | /* Check open flags. All valid flags are supported. */
|
---|
[63809] | 1521 | AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
|
---|
[90802] | 1522 | AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
|
---|
| 1523 | AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
|
---|
| 1524 | AssertPtrReturn(pPCHSGeometry, VERR_INVALID_POINTER);
|
---|
| 1525 | AssertPtrReturn(pLCHSGeometry, VERR_INVALID_POINTER);
|
---|
[1] | 1526 |
|
---|
[66486] | 1527 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)RTMemAllocZ(RT_UOFFSETOF(VDIIMAGEDESC, RegionList.aRegions[1]));
|
---|
[63809] | 1528 | if (RT_LIKELY(pImage))
|
---|
[18567] | 1529 | {
|
---|
[63809] | 1530 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
| 1531 | PVDINTERFACECONFIG pIfCfg = VDIfConfigGet(pVDIfsOperation);
|
---|
| 1532 | pImage->pszFilename = pszFilename;
|
---|
| 1533 | pImage->pStorage = NULL;
|
---|
| 1534 | pImage->paBlocks = NULL;
|
---|
| 1535 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
| 1536 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
[1] | 1537 |
|
---|
[63809] | 1538 | rc = vdiCreateImage(pImage, cbSize, uImageFlags, pszComment,
|
---|
| 1539 | pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags,
|
---|
| 1540 | pIfProgress, uPercentStart, uPercentSpan, pIfCfg);
|
---|
| 1541 | if (RT_SUCCESS(rc))
|
---|
[1] | 1542 | {
|
---|
[63809] | 1543 | /* So far the image is opened in read/write mode. Make sure the
|
---|
| 1544 | * image is opened in read-only mode if the caller requested that. */
|
---|
| 1545 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
[32536] | 1546 | {
|
---|
[63809] | 1547 | vdiFreeImage(pImage, false);
|
---|
| 1548 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
[32536] | 1549 | }
|
---|
[63809] | 1550 |
|
---|
| 1551 | if (RT_SUCCESS(rc))
|
---|
| 1552 | *ppBackendData = pImage;
|
---|
[1] | 1553 | }
|
---|
[63809] | 1554 |
|
---|
| 1555 | if (RT_FAILURE(rc))
|
---|
| 1556 | RTMemFree(pImage);
|
---|
[1] | 1557 | }
|
---|
[13427] | 1558 | else
|
---|
[63809] | 1559 | rc = VERR_NO_MEMORY;
|
---|
[1] | 1560 |
|
---|
[11284] | 1561 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
[1] | 1562 | return rc;
|
---|
| 1563 | }
|
---|
| 1564 |
|
---|
[63785] | 1565 | /** @copydoc VDIMAGEBACKEND::pfnRename */
|
---|
[57388] | 1566 | static DECLCALLBACK(int) vdiRename(void *pBackendData, const char *pszFilename)
|
---|
[1] | 1567 | {
|
---|
[7159] | 1568 | LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
|
---|
[7689] | 1569 | int rc = VINF_SUCCESS;
|
---|
| 1570 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1571 |
|
---|
| 1572 | /* Check arguments. */
|
---|
[63809] | 1573 | AssertReturn((pImage && pszFilename && *pszFilename), VERR_INVALID_PARAMETER);
|
---|
[7689] | 1574 |
|
---|
| 1575 | /* Close the image. */
|
---|
[46613] | 1576 | rc = vdiFreeImage(pImage, false);
|
---|
| 1577 | if (RT_SUCCESS(rc))
|
---|
[11266] | 1578 | {
|
---|
[46613] | 1579 | /* Rename the file. */
|
---|
| 1580 | rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
|
---|
[63809] | 1581 | if (RT_SUCCESS(rc))
|
---|
[46613] | 1582 | {
|
---|
[63809] | 1583 | /* Update pImage with the new information. */
|
---|
| 1584 | pImage->pszFilename = pszFilename;
|
---|
| 1585 |
|
---|
| 1586 | /* Open the new image. */
|
---|
| 1587 | rc = vdiOpenImage(pImage, pImage->uOpenFlags);
|
---|
| 1588 | }
|
---|
| 1589 | else
|
---|
| 1590 | {
|
---|
[46613] | 1591 | /* The move failed, try to reopen the original image. */
|
---|
| 1592 | int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
|
---|
| 1593 | if (RT_FAILURE(rc2))
|
---|
| 1594 | rc = rc2;
|
---|
| 1595 | }
|
---|
| 1596 | }
|
---|
[7689] | 1597 |
|
---|
[11284] | 1598 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[1] | 1599 | return rc;
|
---|
| 1600 | }
|
---|
| 1601 |
|
---|
[63785] | 1602 | /** @copydoc VDIMAGEBACKEND::pfnClose */
|
---|
[57388] | 1603 | static DECLCALLBACK(int) vdiClose(void *pBackendData, bool fDelete)
|
---|
[1] | 1604 | {
|
---|
[7159] | 1605 | LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
|
---|
| 1606 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 1607 |
|
---|
[63809] | 1608 | int rc = vdiFreeImage(pImage, fDelete);
|
---|
[32536] | 1609 | RTMemFree(pImage);
|
---|
[1] | 1610 |
|
---|
[11284] | 1611 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[1] | 1612 | return rc;
|
---|
| 1613 | }
|
---|
| 1614 |
|
---|
[57388] | 1615 | static DECLCALLBACK(int) vdiRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
|
---|
| 1616 | PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
|
---|
[1] | 1617 | {
|
---|
[44252] | 1618 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
|
---|
| 1619 | pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
|
---|
[7159] | 1620 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1621 | unsigned uBlock;
|
---|
| 1622 | unsigned offRead;
|
---|
[63809] | 1623 | int rc = VINF_SUCCESS;
|
---|
[1] | 1624 |
|
---|
[14269] | 1625 | AssertPtr(pImage);
|
---|
[7231] | 1626 | Assert(!(uOffset % 512));
|
---|
| 1627 | Assert(!(cbToRead % 512));
|
---|
[90802] | 1628 | AssertPtrReturn(pIoCtx, VERR_INVALID_POINTER);
|
---|
| 1629 | AssertReturn(cbToRead, VERR_INVALID_PARAMETER);
|
---|
[63809] | 1630 | AssertReturn(uOffset + cbToRead <= getImageDiskSize(&pImage->Header), VERR_INVALID_PARAMETER);
|
---|
[1] | 1631 |
|
---|
[7159] | 1632 | /* Calculate starting block number and offset inside it. */
|
---|
| 1633 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
| 1634 | offRead = (unsigned)uOffset & pImage->uBlockMask;
|
---|
[1] | 1635 |
|
---|
[7159] | 1636 | /* Clip read range to at most the rest of the block. */
|
---|
| 1637 | cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
|
---|
| 1638 | Assert(!(cbToRead % 512));
|
---|
[1] | 1639 |
|
---|
[7159] | 1640 | if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
[15366] | 1641 | rc = VERR_VD_BLOCK_FREE;
|
---|
[7159] | 1642 | else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
[1] | 1643 | {
|
---|
[44252] | 1644 | size_t cbSet;
|
---|
| 1645 |
|
---|
| 1646 | cbSet = vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
|
---|
| 1647 | Assert(cbSet == cbToRead);
|
---|
[1] | 1648 | }
|
---|
[7159] | 1649 | else
|
---|
[1] | 1650 | {
|
---|
[7231] | 1651 | /* Block present in image file, read relevant data. */
|
---|
| 1652 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
[7159] | 1653 | + (pImage->offStartData + pImage->offStartBlockData + offRead);
|
---|
[40713] | 1654 |
|
---|
| 1655 | if (u64Offset + cbToRead <= pImage->cbImage)
|
---|
[44252] | 1656 | rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
| 1657 | pIoCtx, cbToRead);
|
---|
[40713] | 1658 | else
|
---|
| 1659 | {
|
---|
| 1660 | LogRel(("VDI: Out of range access (%llu) in image %s, image size %llu\n",
|
---|
| 1661 | u64Offset, pImage->pszFilename, pImage->cbImage));
|
---|
[44252] | 1662 | vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
|
---|
[40713] | 1663 | rc = VERR_VD_READ_OUT_OF_RANGE;
|
---|
| 1664 | }
|
---|
[1] | 1665 | }
|
---|
| 1666 |
|
---|
[14269] | 1667 | if (pcbActuallyRead)
|
---|
[7159] | 1668 | *pcbActuallyRead = cbToRead;
|
---|
[1] | 1669 |
|
---|
[11284] | 1670 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[1] | 1671 | return rc;
|
---|
| 1672 | }
|
---|
| 1673 |
|
---|
[57388] | 1674 | static DECLCALLBACK(int) vdiWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
|
---|
| 1675 | PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
|
---|
| 1676 | size_t *pcbPostRead, unsigned fWrite)
|
---|
[1] | 1677 | {
|
---|
[44252] | 1678 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
|
---|
| 1679 | pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
|
---|
[7159] | 1680 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1681 | unsigned uBlock;
|
---|
| 1682 | unsigned offWrite;
|
---|
| 1683 | int rc = VINF_SUCCESS;
|
---|
[1] | 1684 |
|
---|
[14269] | 1685 | AssertPtr(pImage);
|
---|
[7231] | 1686 | Assert(!(uOffset % 512));
|
---|
| 1687 | Assert(!(cbToWrite % 512));
|
---|
[90802] | 1688 | AssertPtrReturn(pIoCtx, VERR_INVALID_POINTER);
|
---|
| 1689 | AssertReturn(cbToWrite, VERR_INVALID_PARAMETER);
|
---|
[1] | 1690 |
|
---|
[63809] | 1691 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
[1] | 1692 | {
|
---|
[63809] | 1693 | /* No size check here, will do that later. For dynamic images which are
|
---|
| 1694 | * not multiples of the block size in length, this would prevent writing to
|
---|
| 1695 | * the last block. */
|
---|
[1] | 1696 |
|
---|
[63809] | 1697 | /* Calculate starting block number and offset inside it. */
|
---|
| 1698 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
| 1699 | offWrite = (unsigned)uOffset & pImage->uBlockMask;
|
---|
[1] | 1700 |
|
---|
[63809] | 1701 | /* Clip write range to at most the rest of the block. */
|
---|
| 1702 | cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
|
---|
| 1703 | Assert(!(cbToWrite % 512));
|
---|
[1] | 1704 |
|
---|
[63809] | 1705 | do
|
---|
[1] | 1706 | {
|
---|
[63809] | 1707 | if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
[1] | 1708 | {
|
---|
[63809] | 1709 | /* Block is either free or zero. */
|
---|
| 1710 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
|
---|
| 1711 | && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
| 1712 | || cbToWrite == getImageBlockSize(&pImage->Header)))
|
---|
[15786] | 1713 | {
|
---|
[63809] | 1714 | /* If the destination block is unallocated at this point, it's
|
---|
| 1715 | * either a zero block or a block which hasn't been used so far
|
---|
| 1716 | * (which also means that it's a zero block. Don't need to write
|
---|
| 1717 | * anything to this block if the data consists of just zeroes. */
|
---|
| 1718 | if (vdIfIoIntIoCtxIsZero(pImage->pIfIo, pIoCtx, cbToWrite, true))
|
---|
| 1719 | {
|
---|
| 1720 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
| 1721 | *pcbPreRead = 0;
|
---|
| 1722 | *pcbPostRead = 0;
|
---|
| 1723 | break;
|
---|
| 1724 | }
|
---|
[15786] | 1725 | }
|
---|
[1] | 1726 |
|
---|
[63809] | 1727 | if ( cbToWrite == getImageBlockSize(&pImage->Header)
|
---|
| 1728 | && !(fWrite & VD_WRITE_NO_ALLOC))
|
---|
[44252] | 1729 | {
|
---|
[63809] | 1730 | /* Full block write to previously unallocated block.
|
---|
| 1731 | * Allocate block and write data. */
|
---|
| 1732 | Assert(!offWrite);
|
---|
| 1733 | PVDIASYNCBLOCKALLOC pBlockAlloc = (PVDIASYNCBLOCKALLOC)RTMemAllocZ(sizeof(VDIASYNCBLOCKALLOC));
|
---|
| 1734 | if (!pBlockAlloc)
|
---|
| 1735 | {
|
---|
| 1736 | rc = VERR_NO_MEMORY;
|
---|
| 1737 | break;
|
---|
| 1738 | }
|
---|
[44252] | 1739 |
|
---|
[63809] | 1740 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
| 1741 | uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
|
---|
| 1742 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
[38621] | 1743 |
|
---|
[63809] | 1744 | pBlockAlloc->cBlocksAllocated = cBlocksAllocated;
|
---|
| 1745 | pBlockAlloc->uBlock = uBlock;
|
---|
[38621] | 1746 |
|
---|
[63809] | 1747 | *pcbPreRead = 0;
|
---|
| 1748 | *pcbPostRead = 0;
|
---|
[1] | 1749 |
|
---|
[63809] | 1750 | rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
|
---|
| 1751 | u64Offset, pIoCtx, cbToWrite,
|
---|
| 1752 | vdiBlockAllocUpdate, pBlockAlloc);
|
---|
| 1753 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
| 1754 | break;
|
---|
| 1755 | else if (RT_FAILURE(rc))
|
---|
| 1756 | {
|
---|
| 1757 | RTMemFree(pBlockAlloc);
|
---|
| 1758 | break;
|
---|
| 1759 | }
|
---|
| 1760 |
|
---|
| 1761 | rc = vdiBlockAllocUpdate(pImage, pIoCtx, pBlockAlloc, rc);
|
---|
| 1762 | }
|
---|
| 1763 | else
|
---|
[44252] | 1764 | {
|
---|
[63809] | 1765 | /* Trying to do a partial write to an unallocated block. Don't do
|
---|
| 1766 | * anything except letting the upper layer know what to do. */
|
---|
| 1767 | *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
|
---|
| 1768 | *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
|
---|
| 1769 | rc = VERR_VD_BLOCK_FREE;
|
---|
[44252] | 1770 | }
|
---|
[15786] | 1771 | }
|
---|
| 1772 | else
|
---|
| 1773 | {
|
---|
[63809] | 1774 | /* Block present in image file, write relevant data. */
|
---|
| 1775 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
| 1776 | + (pImage->offStartData + pImage->offStartBlockData + offWrite);
|
---|
| 1777 | rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
|
---|
| 1778 | u64Offset, pIoCtx, cbToWrite, NULL, NULL);
|
---|
[15786] | 1779 | }
|
---|
[63809] | 1780 | } while (0);
|
---|
[15785] | 1781 |
|
---|
[63809] | 1782 | if (pcbWriteProcess)
|
---|
| 1783 | *pcbWriteProcess = cbToWrite;
|
---|
| 1784 | }
|
---|
| 1785 | else
|
---|
| 1786 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[1] | 1787 |
|
---|
[11284] | 1788 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[1] | 1789 | return rc;
|
---|
| 1790 | }
|
---|
| 1791 |
|
---|
[57388] | 1792 | static DECLCALLBACK(int) vdiFlush(void *pBackendData, PVDIOCTX pIoCtx)
|
---|
[1] | 1793 | {
|
---|
[7159] | 1794 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
| 1795 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1796 | int rc = VINF_SUCCESS;
|
---|
[1] | 1797 |
|
---|
[10539] | 1798 | Assert(pImage);
|
---|
| 1799 |
|
---|
[44252] | 1800 | rc = vdiFlushImageIoCtx(pImage, pIoCtx);
|
---|
[11284] | 1801 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[1] | 1802 | return rc;
|
---|
| 1803 | }
|
---|
| 1804 |
|
---|
[63785] | 1805 | /** @copydoc VDIMAGEBACKEND::pfnGetVersion */
|
---|
[57388] | 1806 | static DECLCALLBACK(unsigned) vdiGetVersion(void *pBackendData)
|
---|
[1] | 1807 | {
|
---|
[7159] | 1808 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
| 1809 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 1810 |
|
---|
[63809] | 1811 | AssertPtrReturn(pImage, 0);
|
---|
[1] | 1812 |
|
---|
[63809] | 1813 | LogFlowFunc(("returns %#x\n", pImage->PreHeader.u32Version));
|
---|
| 1814 | return pImage->PreHeader.u32Version;
|
---|
[1] | 1815 | }
|
---|
| 1816 |
|
---|
[63785] | 1817 | /** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
|
---|
[57388] | 1818 | static DECLCALLBACK(uint64_t) vdiGetFileSize(void *pBackendData)
|
---|
[1] | 1819 | {
|
---|
[7159] | 1820 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
| 1821 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1822 | uint64_t cb = 0;
|
---|
[1] | 1823 |
|
---|
[63809] | 1824 | AssertPtrReturn(pImage, 0);
|
---|
[1] | 1825 |
|
---|
[63809] | 1826 | if (pImage->pStorage)
|
---|
[1] | 1827 | {
|
---|
[7159] | 1828 | uint64_t cbFile;
|
---|
[63809] | 1829 | int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
|
---|
| 1830 | if (RT_SUCCESS(rc))
|
---|
| 1831 | cb += cbFile;
|
---|
[1] | 1832 | }
|
---|
| 1833 |
|
---|
[7159] | 1834 | LogFlowFunc(("returns %lld\n", cb));
|
---|
| 1835 | return cb;
|
---|
[1] | 1836 | }
|
---|
| 1837 |
|
---|
[63785] | 1838 | /** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
|
---|
[57388] | 1839 | static DECLCALLBACK(int) vdiGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
|
---|
[1] | 1840 | {
|
---|
[7159] | 1841 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
|
---|
| 1842 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[63809] | 1843 | int rc = VINF_SUCCESS;
|
---|
[1] | 1844 |
|
---|
[63809] | 1845 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 1846 |
|
---|
[63809] | 1847 | if (pImage->PCHSGeometry.cCylinders)
|
---|
| 1848 | *pPCHSGeometry = pImage->PCHSGeometry;
|
---|
[1] | 1849 | else
|
---|
[63809] | 1850 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
[1] | 1851 |
|
---|
[11284] | 1852 | LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
[7159] | 1853 | return rc;
|
---|
[1] | 1854 | }
|
---|
| 1855 |
|
---|
[63785] | 1856 | /** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
|
---|
[57388] | 1857 | static DECLCALLBACK(int) vdiSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
|
---|
[1] | 1858 | {
|
---|
[63809] | 1859 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
|
---|
| 1860 | pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
[7159] | 1861 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[63809] | 1862 | int rc = VINF_SUCCESS;
|
---|
[1] | 1863 |
|
---|
[63809] | 1864 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 1865 |
|
---|
[63809] | 1866 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
| 1867 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
| 1868 | else
|
---|
[7159] | 1869 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
[1] | 1870 |
|
---|
[11284] | 1871 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[7159] | 1872 | return rc;
|
---|
[1] | 1873 | }
|
---|
| 1874 |
|
---|
[63785] | 1875 | /** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
|
---|
[57388] | 1876 | static DECLCALLBACK(int) vdiGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
|
---|
[1] | 1877 | {
|
---|
[7159] | 1878 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
|
---|
| 1879 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 1880 |
|
---|
[63809] | 1881 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 1882 |
|
---|
[63809] | 1883 | int rc = VINF_SUCCESS;
|
---|
| 1884 | VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
|
---|
| 1885 | PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
| 1886 | if (!pGeometry)
|
---|
| 1887 | pGeometry = &DummyGeo;
|
---|
| 1888 |
|
---|
| 1889 | if ( pGeometry->cCylinders > 0
|
---|
| 1890 | && pGeometry->cHeads > 0
|
---|
| 1891 | && pGeometry->cSectors > 0)
|
---|
[1] | 1892 | {
|
---|
[63809] | 1893 | pLCHSGeometry->cCylinders = pGeometry->cCylinders;
|
---|
| 1894 | pLCHSGeometry->cHeads = pGeometry->cHeads;
|
---|
| 1895 | pLCHSGeometry->cSectors = pGeometry->cSectors;
|
---|
[1] | 1896 | }
|
---|
[7159] | 1897 | else
|
---|
[63809] | 1898 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
[1] | 1899 |
|
---|
[11284] | 1900 | LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
[7159] | 1901 | return rc;
|
---|
[1] | 1902 | }
|
---|
| 1903 |
|
---|
[63785] | 1904 | /** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
|
---|
[57388] | 1905 | static DECLCALLBACK(int) vdiSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
|
---|
[1] | 1906 | {
|
---|
[63809] | 1907 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
|
---|
| 1908 | pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
[7159] | 1909 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1910 | PVDIDISKGEOMETRY pGeometry;
|
---|
[63809] | 1911 | int rc = VINF_SUCCESS;
|
---|
[1] | 1912 |
|
---|
[63809] | 1913 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[7159] | 1914 |
|
---|
[63809] | 1915 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
[1] | 1916 | {
|
---|
[7159] | 1917 | pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
[6363] | 1918 | if (pGeometry)
|
---|
| 1919 | {
|
---|
| 1920 | pGeometry->cCylinders = pLCHSGeometry->cCylinders;
|
---|
| 1921 | pGeometry->cHeads = pLCHSGeometry->cHeads;
|
---|
| 1922 | pGeometry->cSectors = pLCHSGeometry->cSectors;
|
---|
| 1923 | pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
[1] | 1924 |
|
---|
[6363] | 1925 | /* Update header information in base image file. */
|
---|
[7159] | 1926 | vdiFlushImage(pImage);
|
---|
[6363] | 1927 | }
|
---|
[1] | 1928 | }
|
---|
[7159] | 1929 | else
|
---|
[63809] | 1930 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[1] | 1931 |
|
---|
[11284] | 1932 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[7159] | 1933 | return rc;
|
---|
[1] | 1934 | }
|
---|
| 1935 |
|
---|
[66486] | 1936 | /** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
|
---|
| 1937 | static DECLCALLBACK(int) vdiQueryRegions(void *pBackendData, PCVDREGIONLIST *ppRegionList)
|
---|
| 1938 | {
|
---|
| 1939 | LogFlowFunc(("pBackendData=%#p ppRegionList=%#p\n", pBackendData, ppRegionList));
|
---|
| 1940 | PVDIIMAGEDESC pThis = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1941 |
|
---|
| 1942 | AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
|
---|
| 1943 |
|
---|
| 1944 | *ppRegionList = &pThis->RegionList;
|
---|
| 1945 | LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
|
---|
| 1946 | return VINF_SUCCESS;
|
---|
| 1947 | }
|
---|
| 1948 |
|
---|
| 1949 | /** @copydoc VDIMAGEBACKEND::pfnRegionListRelease */
|
---|
| 1950 | static DECLCALLBACK(void) vdiRegionListRelease(void *pBackendData, PCVDREGIONLIST pRegionList)
|
---|
| 1951 | {
|
---|
| 1952 | RT_NOREF1(pRegionList);
|
---|
| 1953 | LogFlowFunc(("pBackendData=%#p pRegionList=%#p\n", pBackendData, pRegionList));
|
---|
| 1954 | PVDIIMAGEDESC pThis = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1955 | AssertPtr(pThis); RT_NOREF(pThis);
|
---|
| 1956 |
|
---|
| 1957 | /* Nothing to do here. */
|
---|
| 1958 | }
|
---|
| 1959 |
|
---|
[63785] | 1960 | /** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
|
---|
[57388] | 1961 | static DECLCALLBACK(unsigned) vdiGetImageFlags(void *pBackendData)
|
---|
[1] | 1962 | {
|
---|
[7159] | 1963 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
| 1964 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 1965 |
|
---|
[63809] | 1966 | AssertPtrReturn(pImage, 0);
|
---|
[1] | 1967 |
|
---|
[63809] | 1968 | LogFlowFunc(("returns %#x\n", pImage->uImageFlags));
|
---|
| 1969 | return pImage->uImageFlags;
|
---|
[1] | 1970 | }
|
---|
| 1971 |
|
---|
[63785] | 1972 | /** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
|
---|
[57388] | 1973 | static DECLCALLBACK(unsigned) vdiGetOpenFlags(void *pBackendData)
|
---|
[1] | 1974 | {
|
---|
[7159] | 1975 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
| 1976 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 1977 |
|
---|
[63809] | 1978 | AssertPtrReturn(pImage, 0);
|
---|
[1] | 1979 |
|
---|
[63809] | 1980 | LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
|
---|
| 1981 | return pImage->uOpenFlags;
|
---|
[1] | 1982 | }
|
---|
| 1983 |
|
---|
[63785] | 1984 | /** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
|
---|
[57388] | 1985 | static DECLCALLBACK(int) vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
|
---|
[1] | 1986 | {
|
---|
[13580] | 1987 | LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
|
---|
[7159] | 1988 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 1989 | int rc;
|
---|
| 1990 | const char *pszFilename;
|
---|
[1] | 1991 |
|
---|
[33182] | 1992 | /* Image must be opened and the new flags must be valid. */
|
---|
[44232] | 1993 | if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
|
---|
| 1994 | | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
|
---|
| 1995 | | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_DISCARD
|
---|
| 1996 | | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
|
---|
[63809] | 1997 | rc = VERR_INVALID_PARAMETER;
|
---|
| 1998 | else
|
---|
[1] | 1999 | {
|
---|
[63809] | 2000 | /* Implement this operation via reopening the image. */
|
---|
| 2001 | pszFilename = pImage->pszFilename;
|
---|
| 2002 | rc = vdiFreeImage(pImage, false);
|
---|
| 2003 | if (RT_SUCCESS(rc))
|
---|
| 2004 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
[1] | 2005 | }
|
---|
| 2006 |
|
---|
[11284] | 2007 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[7159] | 2008 | return rc;
|
---|
[1] | 2009 | }
|
---|
| 2010 |
|
---|
[63785] | 2011 | /** @copydoc VDIMAGEBACKEND::pfnGetComment */
|
---|
[57388] | 2012 | static DECLCALLBACK(int) vdiGetComment(void *pBackendData, char *pszComment,
|
---|
| 2013 | size_t cbComment)
|
---|
[1] | 2014 | {
|
---|
[7159] | 2015 | LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
|
---|
| 2016 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2017 |
|
---|
[63809] | 2018 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 2019 |
|
---|
[63809] | 2020 | int rc = VINF_SUCCESS;
|
---|
| 2021 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
| 2022 | /* Make this foolproof even if the image doesn't have the zero
|
---|
| 2023 | * termination. With some luck the repaired header will be saved. */
|
---|
| 2024 | size_t cb = RTStrNLen(pszTmp, VDI_IMAGE_COMMENT_SIZE);
|
---|
| 2025 | if (cb == VDI_IMAGE_COMMENT_SIZE)
|
---|
[1] | 2026 | {
|
---|
[63809] | 2027 | pszTmp[VDI_IMAGE_COMMENT_SIZE-1] = '\0';
|
---|
| 2028 | cb--;
|
---|
[1] | 2029 | }
|
---|
[63809] | 2030 | if (cb < cbComment)
|
---|
| 2031 | {
|
---|
| 2032 | /* memcpy is much better than strncpy. */
|
---|
| 2033 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
| 2034 | }
|
---|
[7159] | 2035 | else
|
---|
[63809] | 2036 | rc = VERR_BUFFER_OVERFLOW;
|
---|
[1] | 2037 |
|
---|
[11284] | 2038 | LogFlowFunc(("returns %Rrc comment=\"%s\"\n", rc, pszComment));
|
---|
[7159] | 2039 | return rc;
|
---|
[1] | 2040 | }
|
---|
| 2041 |
|
---|
[64272] | 2042 | /** @copydoc VDIMAGEBACKEND::pfnSetComment */
|
---|
[57388] | 2043 | static DECLCALLBACK(int) vdiSetComment(void *pBackendData, const char *pszComment)
|
---|
[1] | 2044 | {
|
---|
[7159] | 2045 | LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
|
---|
| 2046 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 2047 | int rc;
|
---|
[1] | 2048 |
|
---|
[63809] | 2049 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 2050 |
|
---|
[63809] | 2051 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
[1] | 2052 | {
|
---|
[63809] | 2053 | size_t cchComment = pszComment ? strlen(pszComment) : 0;
|
---|
| 2054 | if (cchComment < VDI_IMAGE_COMMENT_SIZE)
|
---|
[7159] | 2055 | {
|
---|
[33524] | 2056 | /* we don't support old style images */
|
---|
| 2057 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
| 2058 | {
|
---|
| 2059 | /*
|
---|
| 2060 | * Update the comment field, making sure to zero out all of the previous comment.
|
---|
| 2061 | */
|
---|
| 2062 | memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
|
---|
| 2063 | memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
|
---|
[7159] | 2064 |
|
---|
[33524] | 2065 | /* write out new the header */
|
---|
| 2066 | rc = vdiUpdateHeader(pImage);
|
---|
| 2067 | }
|
---|
| 2068 | else
|
---|
| 2069 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
[7159] | 2070 | }
|
---|
[63809] | 2071 | else
|
---|
| 2072 | {
|
---|
| 2073 | LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
|
---|
| 2074 | rc = VERR_VD_VDI_COMMENT_TOO_LONG;
|
---|
| 2075 | }
|
---|
[1] | 2076 | }
|
---|
[7159] | 2077 | else
|
---|
[63809] | 2078 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[1] | 2079 |
|
---|
[11284] | 2080 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[7159] | 2081 | return rc;
|
---|
[1] | 2082 | }
|
---|
| 2083 |
|
---|
[63785] | 2084 | /** @copydoc VDIMAGEBACKEND::pfnGetUuid */
|
---|
[57388] | 2085 | static DECLCALLBACK(int) vdiGetUuid(void *pBackendData, PRTUUID pUuid)
|
---|
[1] | 2086 | {
|
---|
[7159] | 2087 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
| 2088 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2089 |
|
---|
[63809] | 2090 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 2091 |
|
---|
[63809] | 2092 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
[1] | 2093 |
|
---|
[63809] | 2094 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
|
---|
| 2095 | return VINF_SUCCESS;
|
---|
[1] | 2096 | }
|
---|
| 2097 |
|
---|
[63785] | 2098 | /** @copydoc VDIMAGEBACKEND::pfnSetUuid */
|
---|
[57388] | 2099 | static DECLCALLBACK(int) vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
[1] | 2100 | {
|
---|
[11287] | 2101 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
[7159] | 2102 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2103 |
|
---|
[63809] | 2104 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 2105 |
|
---|
[63809] | 2106 | int rc = VINF_SUCCESS;
|
---|
| 2107 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
[1] | 2108 | {
|
---|
[63809] | 2109 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
| 2110 | pImage->Header.u.v1.uuidCreate = *pUuid;
|
---|
| 2111 | /* Make it possible to clone old VDIs. */
|
---|
| 2112 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
| 2113 | pImage->Header.u.v0.uuidCreate = *pUuid;
|
---|
| 2114 | else
|
---|
[7159] | 2115 | {
|
---|
[63809] | 2116 | LogFunc(("Version is not supported!\n"));
|
---|
| 2117 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
[7159] | 2118 | }
|
---|
[1] | 2119 | }
|
---|
[7159] | 2120 | else
|
---|
[63809] | 2121 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[1] | 2122 |
|
---|
[11284] | 2123 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[7159] | 2124 | return rc;
|
---|
[1] | 2125 | }
|
---|
| 2126 |
|
---|
[63785] | 2127 | /** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
|
---|
[57388] | 2128 | static DECLCALLBACK(int) vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
[1] | 2129 | {
|
---|
[7159] | 2130 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
| 2131 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2132 |
|
---|
[63809] | 2133 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 2134 |
|
---|
[63809] | 2135 | *pUuid = *getImageModificationUUID(&pImage->Header);
|
---|
[1] | 2136 |
|
---|
[63809] | 2137 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
|
---|
| 2138 | return VINF_SUCCESS;
|
---|
[1] | 2139 | }
|
---|
| 2140 |
|
---|
[63785] | 2141 | /** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
|
---|
[57388] | 2142 | static DECLCALLBACK(int) vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
[1] | 2143 | {
|
---|
[11287] | 2144 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
[7159] | 2145 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2146 |
|
---|
[63809] | 2147 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 2148 |
|
---|
[63809] | 2149 | int rc = VINF_SUCCESS;
|
---|
| 2150 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
[1] | 2151 | {
|
---|
[63809] | 2152 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
| 2153 | pImage->Header.u.v1.uuidModify = *pUuid;
|
---|
| 2154 | /* Make it possible to clone old VDIs. */
|
---|
| 2155 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
| 2156 | pImage->Header.u.v0.uuidModify = *pUuid;
|
---|
| 2157 | else
|
---|
[7159] | 2158 | {
|
---|
[63809] | 2159 | LogFunc(("Version is not supported!\n"));
|
---|
| 2160 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
[7159] | 2161 | }
|
---|
[1] | 2162 | }
|
---|
[7159] | 2163 | else
|
---|
[63809] | 2164 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[1] | 2165 |
|
---|
[11284] | 2166 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[1] | 2167 | return rc;
|
---|
| 2168 | }
|
---|
| 2169 |
|
---|
[63785] | 2170 | /** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
|
---|
[57388] | 2171 | static DECLCALLBACK(int) vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
|
---|
[1] | 2172 | {
|
---|
[7159] | 2173 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
| 2174 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2175 |
|
---|
[63809] | 2176 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[7159] | 2177 |
|
---|
[63809] | 2178 | *pUuid = *getImageParentUUID(&pImage->Header);
|
---|
[1] | 2179 |
|
---|
[63809] | 2180 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
|
---|
| 2181 | return VINF_SUCCESS;
|
---|
[1] | 2182 | }
|
---|
| 2183 |
|
---|
[63785] | 2184 | /** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
|
---|
[57388] | 2185 | static DECLCALLBACK(int) vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
[1] | 2186 | {
|
---|
[11287] | 2187 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
[7159] | 2188 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2189 |
|
---|
[63809] | 2190 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 2191 |
|
---|
[63809] | 2192 | int rc = VINF_SUCCESS;
|
---|
| 2193 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
[1] | 2194 | {
|
---|
[63809] | 2195 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
| 2196 | pImage->Header.u.v1.uuidLinkage = *pUuid;
|
---|
| 2197 | /* Make it possible to clone old VDIs. */
|
---|
| 2198 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
| 2199 | pImage->Header.u.v0.uuidLinkage = *pUuid;
|
---|
| 2200 | else
|
---|
[1] | 2201 | {
|
---|
[63809] | 2202 | LogFunc(("Version is not supported!\n"));
|
---|
| 2203 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
[1] | 2204 | }
|
---|
| 2205 | }
|
---|
[7159] | 2206 | else
|
---|
[63809] | 2207 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[1] | 2208 |
|
---|
[11284] | 2209 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[1] | 2210 | return rc;
|
---|
| 2211 | }
|
---|
| 2212 |
|
---|
[63785] | 2213 | /** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
|
---|
[57388] | 2214 | static DECLCALLBACK(int) vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
[1] | 2215 | {
|
---|
[7159] | 2216 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
| 2217 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2218 |
|
---|
[63809] | 2219 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[7159] | 2220 |
|
---|
[63809] | 2221 | *pUuid = *getImageParentModificationUUID(&pImage->Header);
|
---|
[1] | 2222 |
|
---|
[63809] | 2223 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", VINF_SUCCESS, pUuid));
|
---|
| 2224 | return VINF_SUCCESS;
|
---|
[1] | 2225 | }
|
---|
| 2226 |
|
---|
[63785] | 2227 | /** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
|
---|
[57388] | 2228 | static DECLCALLBACK(int) vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
[1] | 2229 | {
|
---|
[11287] | 2230 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
[7159] | 2231 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2232 |
|
---|
[63809] | 2233 | AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
|
---|
[1] | 2234 |
|
---|
[63809] | 2235 | int rc = VINF_SUCCESS;
|
---|
| 2236 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
[1] | 2237 | {
|
---|
[63809] | 2238 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
| 2239 | pImage->Header.u.v1.uuidParentModify = *pUuid;
|
---|
| 2240 | else
|
---|
[1] | 2241 | {
|
---|
[63809] | 2242 | LogFunc(("Version is not supported!\n"));
|
---|
| 2243 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
[1] | 2244 | }
|
---|
| 2245 | }
|
---|
[7159] | 2246 | else
|
---|
[63809] | 2247 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[1] | 2248 |
|
---|
[11284] | 2249 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
[1] | 2250 | return rc;
|
---|
| 2251 | }
|
---|
| 2252 |
|
---|
[63785] | 2253 | /** @copydoc VDIMAGEBACKEND::pfnDump */
|
---|
[57388] | 2254 | static DECLCALLBACK(void) vdiDump(void *pBackendData)
|
---|
[1] | 2255 | {
|
---|
[7159] | 2256 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
[1] | 2257 |
|
---|
[63809] | 2258 | AssertPtrReturnVoid(pImage);
|
---|
[38469] | 2259 | vdIfErrorMessage(pImage->pIfError, "Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%#p\n",
|
---|
| 2260 | pImage->pszFilename,
|
---|
| 2261 | (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
|
---|
| 2262 | pImage->uOpenFlags,
|
---|
| 2263 | pImage->pStorage);
|
---|
| 2264 | vdIfErrorMessage(pImage->pIfError, "Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
|
---|
| 2265 | pImage->PreHeader.u32Version,
|
---|
| 2266 | getImageType(&pImage->Header),
|
---|
| 2267 | getImageFlags(&pImage->Header),
|
---|
| 2268 | getImageDiskSize(&pImage->Header));
|
---|
| 2269 | vdIfErrorMessage(pImage->pIfError, "Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
|
---|
| 2270 | getImageBlockSize(&pImage->Header),
|
---|
| 2271 | getImageExtraBlockSize(&pImage->Header),
|
---|
| 2272 | getImageBlocks(&pImage->Header),
|
---|
| 2273 | getImageBlocksAllocated(&pImage->Header));
|
---|
| 2274 | vdIfErrorMessage(pImage->pIfError, "Header: offBlocks=%u offData=%u\n",
|
---|
| 2275 | getImageBlocksOffset(&pImage->Header),
|
---|
| 2276 | getImageDataOffset(&pImage->Header));
|
---|
[6291] | 2277 | PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
|
---|
[6363] | 2278 | if (pg)
|
---|
[38469] | 2279 | vdIfErrorMessage(pImage->pIfError, "Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
|
---|
| 2280 | pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
|
---|
| 2281 | vdIfErrorMessage(pImage->pIfError, "Header: uuidCreation={%RTuuid}\n", getImageCreationUUID(&pImage->Header));
|
---|
| 2282 | vdIfErrorMessage(pImage->pIfError, "Header: uuidModification={%RTuuid}\n", getImageModificationUUID(&pImage->Header));
|
---|
| 2283 | vdIfErrorMessage(pImage->pIfError, "Header: uuidParent={%RTuuid}\n", getImageParentUUID(&pImage->Header));
|
---|
[1] | 2284 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
|
---|
[38469] | 2285 | vdIfErrorMessage(pImage->pIfError, "Header: uuidParentModification={%RTuuid}\n", getImageParentModificationUUID(&pImage->Header));
|
---|
| 2286 | vdIfErrorMessage(pImage->pIfError, "Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
|
---|
| 2287 | pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
|
---|
| 2288 | vdIfErrorMessage(pImage->pIfError, "Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
|
---|
| 2289 | pImage->uBlockMask,
|
---|
| 2290 | pImage->cbTotalBlockData,
|
---|
| 2291 | pImage->uShiftOffset2Index,
|
---|
| 2292 | pImage->offStartBlockData);
|
---|
[139] | 2293 |
|
---|
| 2294 | unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
|
---|
| 2295 | for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
|
---|
| 2296 | {
|
---|
| 2297 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
| 2298 | {
|
---|
| 2299 | cBlocksNotFree++;
|
---|
| 2300 | if (pImage->paBlocks[uBlock] >= cBlocks)
|
---|
| 2301 | cBadBlocks++;
|
---|
| 2302 | }
|
---|
| 2303 | }
|
---|
| 2304 | if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
|
---|
| 2305 | {
|
---|
[38469] | 2306 | vdIfErrorMessage(pImage->pIfError, "!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
|
---|
| 2307 | cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
|
---|
[139] | 2308 | }
|
---|
| 2309 | if (cBadBlocks)
|
---|
| 2310 | {
|
---|
[38469] | 2311 | vdIfErrorMessage(pImage->pIfError, "!! WARNING: %u bad blocks found !!\n",
|
---|
| 2312 | cBadBlocks);
|
---|
[139] | 2313 | }
|
---|
[1] | 2314 | }
|
---|
| 2315 |
|
---|
[63785] | 2316 | /** @copydoc VDIMAGEBACKEND::pfnCompact */
|
---|
[57388] | 2317 | static DECLCALLBACK(int) vdiCompact(void *pBackendData, unsigned uPercentStart,
|
---|
| 2318 | unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
|
---|
| 2319 | PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation)
|
---|
[19176] | 2320 | {
|
---|
[62742] | 2321 | RT_NOREF2(pVDIfsDisk, pVDIfsImage);
|
---|
[19176] | 2322 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 2323 | int rc = VINF_SUCCESS;
|
---|
| 2324 | void *pvBuf = NULL, *pvTmp = NULL;
|
---|
| 2325 | unsigned *paBlocks2 = NULL;
|
---|
[10715] | 2326 |
|
---|
[85121] | 2327 | PFNVDPARENTREAD pfnParentRead = NULL;
|
---|
[19176] | 2328 | void *pvParent = NULL;
|
---|
[38469] | 2329 | PVDINTERFACEPARENTSTATE pIfParentState = VDIfParentStateGet(pVDIfsOperation);
|
---|
[19176] | 2330 | if (pIfParentState)
|
---|
| 2331 | {
|
---|
[38469] | 2332 | pfnParentRead = pIfParentState->pfnParentRead;
|
---|
| 2333 | pvParent = pIfParentState->Core.pvUser;
|
---|
[19176] | 2334 | }
|
---|
| 2335 |
|
---|
[38469] | 2336 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
[40031] | 2337 | PVDINTERFACEQUERYRANGEUSE pIfQueryRangeUse = VDIfQueryRangeUseGet(pVDIfsOperation);
|
---|
| 2338 |
|
---|
[63809] | 2339 | do
|
---|
| 2340 | {
|
---|
[19176] | 2341 | AssertBreakStmt(pImage, rc = VERR_INVALID_PARAMETER);
|
---|
| 2342 |
|
---|
| 2343 | AssertBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
| 2344 | rc = VERR_VD_IMAGE_READ_ONLY);
|
---|
| 2345 |
|
---|
| 2346 | unsigned cBlocks;
|
---|
| 2347 | unsigned cBlocksToMove = 0;
|
---|
| 2348 | size_t cbBlock;
|
---|
| 2349 | cBlocks = getImageBlocks(&pImage->Header);
|
---|
| 2350 | cbBlock = getImageBlockSize(&pImage->Header);
|
---|
| 2351 | if (pfnParentRead)
|
---|
| 2352 | {
|
---|
| 2353 | pvBuf = RTMemTmpAlloc(cbBlock);
|
---|
[54117] | 2354 | AssertBreakStmt(pvBuf, rc = VERR_NO_MEMORY);
|
---|
[19176] | 2355 | }
|
---|
| 2356 | pvTmp = RTMemTmpAlloc(cbBlock);
|
---|
[54117] | 2357 | AssertBreakStmt(pvTmp, rc = VERR_NO_MEMORY);
|
---|
[19176] | 2358 |
|
---|
| 2359 | uint64_t cbFile;
|
---|
[38469] | 2360 | rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
|
---|
[19176] | 2361 | AssertRCBreak(rc);
|
---|
| 2362 | unsigned cBlocksAllocated = (unsigned)((cbFile - pImage->offStartData - pImage->offStartBlockData) >> pImage->uShiftOffset2Index);
|
---|
[19202] | 2363 | if (cBlocksAllocated == 0)
|
---|
| 2364 | {
|
---|
| 2365 | /* No data blocks in this image, no need to compact. */
|
---|
| 2366 | rc = VINF_SUCCESS;
|
---|
| 2367 | break;
|
---|
| 2368 | }
|
---|
[19176] | 2369 |
|
---|
| 2370 | /* Allocate block array for back resolving. */
|
---|
| 2371 | paBlocks2 = (unsigned *)RTMemAlloc(sizeof(unsigned *) * cBlocksAllocated);
|
---|
[54117] | 2372 | AssertBreakStmt(paBlocks2, rc = VERR_NO_MEMORY);
|
---|
[19176] | 2373 | /* Fill out back resolving, check/fix allocation errors before
|
---|
| 2374 | * compacting the image, just to be on the safe side. Update the
|
---|
| 2375 | * image contents straight away, as this enables cancelling. */
|
---|
[19251] | 2376 | for (unsigned i = 0; i < cBlocksAllocated; i++)
|
---|
[19176] | 2377 | paBlocks2[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 2378 | rc = VINF_SUCCESS;
|
---|
| 2379 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
| 2380 | {
|
---|
| 2381 | VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
|
---|
| 2382 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
|
---|
| 2383 | {
|
---|
| 2384 | if (ptrBlock < cBlocksAllocated)
|
---|
| 2385 | {
|
---|
| 2386 | if (paBlocks2[ptrBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
| 2387 | paBlocks2[ptrBlock] = i;
|
---|
| 2388 | else
|
---|
| 2389 | {
|
---|
| 2390 | LogFunc(("Freed cross-linked block %u in file \"%s\"\n",
|
---|
| 2391 | i, pImage->pszFilename));
|
---|
| 2392 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 2393 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
| 2394 | if (RT_FAILURE(rc))
|
---|
| 2395 | break;
|
---|
| 2396 | }
|
---|
| 2397 | }
|
---|
| 2398 | else
|
---|
| 2399 | {
|
---|
| 2400 | LogFunc(("Freed out of bounds reference for block %u in file \"%s\"\n",
|
---|
| 2401 | i, pImage->pszFilename));
|
---|
[19251] | 2402 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
[19176] | 2403 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
| 2404 | if (RT_FAILURE(rc))
|
---|
| 2405 | break;
|
---|
| 2406 | }
|
---|
| 2407 | }
|
---|
| 2408 | }
|
---|
| 2409 | if (RT_FAILURE(rc))
|
---|
| 2410 | break;
|
---|
| 2411 |
|
---|
| 2412 | /* Find redundant information and update the block pointers
|
---|
| 2413 | * accordingly, creating bubbles. Keep disk up to date, as this
|
---|
| 2414 | * enables cancelling. */
|
---|
| 2415 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
| 2416 | {
|
---|
| 2417 | VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
|
---|
| 2418 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
|
---|
| 2419 | {
|
---|
| 2420 | /* Block present in image file, read relevant data. */
|
---|
| 2421 | uint64_t u64Offset = (uint64_t)ptrBlock * pImage->cbTotalBlockData
|
---|
| 2422 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
[44233] | 2423 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset, pvTmp, cbBlock);
|
---|
[19176] | 2424 | if (RT_FAILURE(rc))
|
---|
| 2425 | break;
|
---|
| 2426 |
|
---|
| 2427 | if (ASMBitFirstSet((volatile void *)pvTmp, (uint32_t)cbBlock * 8) == -1)
|
---|
| 2428 | {
|
---|
| 2429 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
|
---|
| 2430 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
| 2431 | if (RT_FAILURE(rc))
|
---|
| 2432 | break;
|
---|
| 2433 | paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 2434 | /* Adjust progress info, one block to be relocated. */
|
---|
| 2435 | cBlocksToMove++;
|
---|
| 2436 | }
|
---|
| 2437 | else if (pfnParentRead)
|
---|
| 2438 | {
|
---|
[40032] | 2439 | rc = pfnParentRead(pvParent, (uint64_t)i * cbBlock, pvBuf, cbBlock);
|
---|
[19176] | 2440 | if (RT_FAILURE(rc))
|
---|
| 2441 | break;
|
---|
[19202] | 2442 | if (!memcmp(pvTmp, pvBuf, cbBlock))
|
---|
[19176] | 2443 | {
|
---|
| 2444 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 2445 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
| 2446 | if (RT_FAILURE(rc))
|
---|
| 2447 | break;
|
---|
| 2448 | paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 2449 | /* Adjust progress info, one block to be relocated. */
|
---|
| 2450 | cBlocksToMove++;
|
---|
| 2451 | }
|
---|
| 2452 | }
|
---|
| 2453 | }
|
---|
| 2454 |
|
---|
[40031] | 2455 | /* Check if the range is in use if the block is still allocated. */
|
---|
| 2456 | ptrBlock = pImage->paBlocks[i];
|
---|
| 2457 | if ( IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock)
|
---|
| 2458 | && pIfQueryRangeUse)
|
---|
| 2459 | {
|
---|
| 2460 | bool fUsed = true;
|
---|
| 2461 |
|
---|
| 2462 | rc = vdIfQueryRangeUse(pIfQueryRangeUse, (uint64_t)i * cbBlock, cbBlock, &fUsed);
|
---|
| 2463 | if (RT_FAILURE(rc))
|
---|
| 2464 | break;
|
---|
| 2465 | if (!fUsed)
|
---|
| 2466 | {
|
---|
| 2467 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
|
---|
| 2468 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
| 2469 | if (RT_FAILURE(rc))
|
---|
| 2470 | break;
|
---|
| 2471 | paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 2472 | /* Adjust progress info, one block to be relocated. */
|
---|
| 2473 | cBlocksToMove++;
|
---|
| 2474 | }
|
---|
| 2475 | }
|
---|
| 2476 |
|
---|
[63809] | 2477 | vdIfProgress(pIfProgress, (uint64_t)i * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
|
---|
| 2478 | if (RT_FAILURE(rc))
|
---|
| 2479 | break;
|
---|
[19176] | 2480 | }
|
---|
| 2481 | if (RT_FAILURE(rc))
|
---|
| 2482 | break;
|
---|
| 2483 |
|
---|
| 2484 | /* Fill bubbles with other data (if available). */
|
---|
| 2485 | unsigned cBlocksMoved = 0;
|
---|
| 2486 | unsigned uBlockUsedPos = cBlocksAllocated;
|
---|
| 2487 | for (unsigned i = 0; i < cBlocksAllocated; i++)
|
---|
| 2488 | {
|
---|
| 2489 | unsigned uBlock = paBlocks2[i];
|
---|
| 2490 | if (uBlock == VDI_IMAGE_BLOCK_FREE)
|
---|
| 2491 | {
|
---|
[19202] | 2492 | unsigned uBlockData = VDI_IMAGE_BLOCK_FREE;
|
---|
| 2493 | while (uBlockUsedPos > i && uBlockData == VDI_IMAGE_BLOCK_FREE)
|
---|
| 2494 | {
|
---|
[19176] | 2495 | uBlockUsedPos--;
|
---|
| 2496 | uBlockData = paBlocks2[uBlockUsedPos];
|
---|
[19202] | 2497 | }
|
---|
[19176] | 2498 | /* Terminate early if there is no block which needs copying. */
|
---|
| 2499 | if (uBlockUsedPos == i)
|
---|
| 2500 | break;
|
---|
| 2501 | uint64_t u64Offset = (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
|
---|
| 2502 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
[38469] | 2503 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
[44233] | 2504 | pvTmp, cbBlock);
|
---|
[19176] | 2505 | u64Offset = (uint64_t)i * pImage->cbTotalBlockData
|
---|
| 2506 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
[38469] | 2507 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
[44233] | 2508 | pvTmp, cbBlock);
|
---|
[19176] | 2509 | pImage->paBlocks[uBlockData] = i;
|
---|
| 2510 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated - cBlocksMoved);
|
---|
| 2511 | rc = vdiUpdateBlockInfo(pImage, uBlockData);
|
---|
| 2512 | if (RT_FAILURE(rc))
|
---|
| 2513 | break;
|
---|
| 2514 | paBlocks2[i] = uBlockData;
|
---|
| 2515 | paBlocks2[uBlockUsedPos] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 2516 | cBlocksMoved++;
|
---|
| 2517 | }
|
---|
| 2518 |
|
---|
[63809] | 2519 | rc = vdIfProgress(pIfProgress, (uint64_t)(cBlocks + cBlocksMoved) * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
|
---|
| 2520 | if (RT_FAILURE(rc))
|
---|
| 2521 | break;
|
---|
[19176] | 2522 | }
|
---|
| 2523 | if (RT_FAILURE(rc))
|
---|
| 2524 | break;
|
---|
| 2525 |
|
---|
| 2526 | /* Update image header. */
|
---|
| 2527 | setImageBlocksAllocated(&pImage->Header, uBlockUsedPos);
|
---|
| 2528 | vdiUpdateHeader(pImage);
|
---|
| 2529 |
|
---|
| 2530 | /* Truncate the image to the proper size to finish compacting. */
|
---|
[38469] | 2531 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
|
---|
| 2532 | (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
|
---|
| 2533 | + pImage->offStartData + pImage->offStartBlockData);
|
---|
[19176] | 2534 | } while (0);
|
---|
| 2535 |
|
---|
| 2536 | if (paBlocks2)
|
---|
| 2537 | RTMemTmpFree(paBlocks2);
|
---|
| 2538 | if (pvTmp)
|
---|
| 2539 | RTMemTmpFree(pvTmp);
|
---|
| 2540 | if (pvBuf)
|
---|
| 2541 | RTMemTmpFree(pvBuf);
|
---|
| 2542 |
|
---|
[63809] | 2543 | if (RT_SUCCESS(rc))
|
---|
| 2544 | vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
|
---|
[19176] | 2545 |
|
---|
| 2546 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
| 2547 | return rc;
|
---|
| 2548 | }
|
---|
| 2549 |
|
---|
| 2550 |
|
---|
[63785] | 2551 | /** @copydoc VDIMAGEBACKEND::pfnResize */
|
---|
[57388] | 2552 | static DECLCALLBACK(int) vdiResize(void *pBackendData, uint64_t cbSize,
|
---|
| 2553 | PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
|
---|
| 2554 | unsigned uPercentStart, unsigned uPercentSpan,
|
---|
| 2555 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
| 2556 | PVDINTERFACE pVDIfsOperation)
|
---|
[31804] | 2557 | {
|
---|
[62742] | 2558 | RT_NOREF5(uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation);
|
---|
[31804] | 2559 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 2560 | int rc = VINF_SUCCESS;
|
---|
| 2561 |
|
---|
[64693] | 2562 | /* Check size. Maximum 4PB-3M. No tricks with adjusting the 1M block size
|
---|
| 2563 | * so far, which would extend the size. */
|
---|
[103501] | 2564 | if ( cbSize >= _1P * 4 - _1M * 3
|
---|
[64693] | 2565 | || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE)
|
---|
| 2566 | return VERR_VD_INVALID_SIZE;
|
---|
| 2567 |
|
---|
[31920] | 2568 | /*
|
---|
[32431] | 2569 | * Making the image smaller is not supported at the moment.
|
---|
[31920] | 2570 | * Resizing is also not supported for fixed size images and
|
---|
| 2571 | * very old images.
|
---|
| 2572 | */
|
---|
[32536] | 2573 | /** @todo implement making the image smaller, it is the responsibility of
|
---|
| 2574 | * the user to know what he's doing. */
|
---|
[76154] | 2575 | if (cbSize < getImageDiskSize(&pImage->Header))
|
---|
| 2576 | rc = VERR_VD_SHRINK_NOT_SUPPORTED;
|
---|
| 2577 | else if ( GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0
|
---|
| 2578 | || pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
[31804] | 2579 | rc = VERR_NOT_SUPPORTED;
|
---|
| 2580 | else if (cbSize > getImageDiskSize(&pImage->Header))
|
---|
| 2581 | {
|
---|
[31810] | 2582 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header); /** < Blocks currently allocated, doesn't change during resize */
|
---|
[84475] | 2583 | unsigned const cbBlock = RT_MAX(getImageBlockSize(&pImage->Header), 1);
|
---|
| 2584 | uint32_t cBlocksNew = cbSize / cbBlock; /** < New number of blocks in the image after the resize */
|
---|
| 2585 | if (cbSize % cbBlock)
|
---|
[31804] | 2586 | cBlocksNew++;
|
---|
| 2587 |
|
---|
[31810] | 2588 | uint32_t cBlocksOld = getImageBlocks(&pImage->Header); /** < Number of blocks before the resize. */
|
---|
| 2589 | uint64_t cbBlockspaceNew = cBlocksNew * sizeof(VDIIMAGEBLOCKPOINTER); /** < Required space for the block array after the resize. */
|
---|
[32536] | 2590 | uint64_t offStartDataNew = RT_ALIGN_32(pImage->offStartBlocks + cbBlockspaceNew, VDI_DATA_ALIGN); /** < New start offset for block data after the resize */
|
---|
[31804] | 2591 |
|
---|
[64504] | 2592 | if (pImage->offStartData < offStartDataNew)
|
---|
[31804] | 2593 | {
|
---|
[64504] | 2594 | if (cBlocksAllocated > 0)
|
---|
| 2595 | {
|
---|
| 2596 | /* Calculate how many sectors need to be relocated. */
|
---|
| 2597 | uint64_t cbOverlapping = offStartDataNew - pImage->offStartData;
|
---|
[84475] | 2598 | unsigned cBlocksReloc = cbOverlapping / cbBlock;
|
---|
| 2599 | if (cbOverlapping % cbBlock)
|
---|
[64504] | 2600 | cBlocksReloc++;
|
---|
[31804] | 2601 |
|
---|
[64504] | 2602 | /* Since only full blocks can be relocated the new data start is
|
---|
| 2603 | * determined by moving it block by block. */
|
---|
| 2604 | cBlocksReloc = RT_MIN(cBlocksReloc, cBlocksAllocated);
|
---|
| 2605 | offStartDataNew = pImage->offStartData;
|
---|
[31810] | 2606 |
|
---|
[64504] | 2607 | /* Do the relocation. */
|
---|
| 2608 | LogFlow(("Relocating %u blocks\n", cBlocksReloc));
|
---|
[31804] | 2609 |
|
---|
[64504] | 2610 | /*
|
---|
| 2611 | * Get the blocks we need to relocate first, they are appended to the end
|
---|
| 2612 | * of the image.
|
---|
| 2613 | */
|
---|
| 2614 | void *pvBuf = NULL, *pvZero = NULL;
|
---|
| 2615 | do
|
---|
[31804] | 2616 | {
|
---|
[64504] | 2617 | /* Allocate data buffer. */
|
---|
| 2618 | pvBuf = RTMemAllocZ(pImage->cbTotalBlockData);
|
---|
| 2619 | if (!pvBuf)
|
---|
| 2620 | {
|
---|
| 2621 | rc = VERR_NO_MEMORY;
|
---|
| 2622 | break;
|
---|
| 2623 | }
|
---|
[31804] | 2624 |
|
---|
[64504] | 2625 | /* Allocate buffer for overwriting with zeroes. */
|
---|
| 2626 | pvZero = RTMemAllocZ(pImage->cbTotalBlockData);
|
---|
| 2627 | if (!pvZero)
|
---|
| 2628 | {
|
---|
| 2629 | rc = VERR_NO_MEMORY;
|
---|
| 2630 | break;
|
---|
| 2631 | }
|
---|
[31804] | 2632 |
|
---|
[64504] | 2633 | for (unsigned i = 0; i < cBlocksReloc; i++)
|
---|
[31804] | 2634 | {
|
---|
[64504] | 2635 | /* Search the index in the block table. */
|
---|
| 2636 | for (unsigned idxBlock = 0; idxBlock < cBlocksOld; idxBlock++)
|
---|
[31804] | 2637 | {
|
---|
[64504] | 2638 | if (!pImage->paBlocks[idxBlock])
|
---|
| 2639 | {
|
---|
| 2640 | /* Read data and append to the end of the image. */
|
---|
| 2641 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
|
---|
| 2642 | offStartDataNew, pvBuf,
|
---|
| 2643 | pImage->cbTotalBlockData);
|
---|
| 2644 | if (RT_FAILURE(rc))
|
---|
| 2645 | break;
|
---|
[31804] | 2646 |
|
---|
[64504] | 2647 | uint64_t offBlockAppend;
|
---|
| 2648 | rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &offBlockAppend);
|
---|
| 2649 | if (RT_FAILURE(rc))
|
---|
| 2650 | break;
|
---|
[31804] | 2651 |
|
---|
[64504] | 2652 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
| 2653 | offBlockAppend, pvBuf,
|
---|
| 2654 | pImage->cbTotalBlockData);
|
---|
| 2655 | if (RT_FAILURE(rc))
|
---|
| 2656 | break;
|
---|
[31804] | 2657 |
|
---|
[64504] | 2658 | /* Zero out the old block area. */
|
---|
| 2659 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
| 2660 | offStartDataNew, pvZero,
|
---|
| 2661 | pImage->cbTotalBlockData);
|
---|
| 2662 | if (RT_FAILURE(rc))
|
---|
| 2663 | break;
|
---|
[31804] | 2664 |
|
---|
[64504] | 2665 | /* Update block counter. */
|
---|
| 2666 | pImage->paBlocks[idxBlock] = cBlocksAllocated - 1;
|
---|
[31804] | 2667 |
|
---|
[64504] | 2668 | /*
|
---|
| 2669 | * Decrease the block number of all other entries in the array.
|
---|
| 2670 | * They were moved one block to the front.
|
---|
| 2671 | * Doing it as a separate step iterating over the array again
|
---|
| 2672 | * because an error while relocating the block might end up
|
---|
| 2673 | * in a corrupted image otherwise.
|
---|
| 2674 | */
|
---|
| 2675 | for (unsigned idxBlock2 = 0; idxBlock2 < cBlocksOld; idxBlock2++)
|
---|
| 2676 | {
|
---|
| 2677 | if ( idxBlock2 != idxBlock
|
---|
| 2678 | && IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[idxBlock2]))
|
---|
| 2679 | pImage->paBlocks[idxBlock2]--;
|
---|
| 2680 | }
|
---|
| 2681 |
|
---|
| 2682 | /* Continue with the next block. */
|
---|
| 2683 | break;
|
---|
[31804] | 2684 | }
|
---|
[64504] | 2685 | }
|
---|
[31810] | 2686 |
|
---|
[64504] | 2687 | if (RT_FAILURE(rc))
|
---|
[31810] | 2688 | break;
|
---|
[64504] | 2689 |
|
---|
| 2690 | offStartDataNew += pImage->cbTotalBlockData;
|
---|
[31804] | 2691 | }
|
---|
[64504] | 2692 | } while (0);
|
---|
[31804] | 2693 |
|
---|
[64504] | 2694 | if (pvBuf)
|
---|
| 2695 | RTMemFree(pvBuf);
|
---|
| 2696 | if (pvZero)
|
---|
| 2697 | RTMemFree(pvZero);
|
---|
| 2698 | }
|
---|
[31804] | 2699 |
|
---|
[64504] | 2700 | /*
|
---|
| 2701 | * We need to update the new offsets for the image data in the out of memory
|
---|
| 2702 | * case too because we relocated the blocks already.
|
---|
| 2703 | */
|
---|
| 2704 | pImage->offStartData = offStartDataNew;
|
---|
| 2705 | setImageDataOffset(&pImage->Header, offStartDataNew);
|
---|
[31804] | 2706 | }
|
---|
| 2707 |
|
---|
| 2708 | /*
|
---|
| 2709 | * Relocation done, expand the block array and update the header with
|
---|
| 2710 | * the new data.
|
---|
| 2711 | */
|
---|
| 2712 | if (RT_SUCCESS(rc))
|
---|
| 2713 | {
|
---|
[31810] | 2714 | PVDIIMAGEBLOCKPOINTER paBlocksNew = (PVDIIMAGEBLOCKPOINTER)RTMemRealloc(pImage->paBlocks, cbBlockspaceNew);
|
---|
[31804] | 2715 | if (paBlocksNew)
|
---|
| 2716 | {
|
---|
[31810] | 2717 | pImage->paBlocks = paBlocksNew;
|
---|
| 2718 |
|
---|
[31804] | 2719 | /* Mark the new blocks as unallocated. */
|
---|
[31810] | 2720 | for (unsigned idxBlock = cBlocksOld; idxBlock < cBlocksNew; idxBlock++)
|
---|
[31804] | 2721 | pImage->paBlocks[idxBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
| 2722 | }
|
---|
| 2723 | else
|
---|
| 2724 | rc = VERR_NO_MEMORY;
|
---|
| 2725 |
|
---|
| 2726 | /* Write the block array before updating the rest. */
|
---|
[40936] | 2727 | vdiConvBlocksEndianess(VDIECONV_H2F, pImage->paBlocks, cBlocksNew);
|
---|
[38469] | 2728 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks,
|
---|
[44233] | 2729 | pImage->paBlocks, cbBlockspaceNew);
|
---|
[40936] | 2730 | vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, cBlocksNew);
|
---|
[31804] | 2731 |
|
---|
[31810] | 2732 | if (RT_SUCCESS(rc))
|
---|
| 2733 | {
|
---|
| 2734 | /* Update size and new block count. */
|
---|
| 2735 | setImageDiskSize(&pImage->Header, cbSize);
|
---|
| 2736 | setImageBlocks(&pImage->Header, cBlocksNew);
|
---|
| 2737 | /* Update geometry. */
|
---|
| 2738 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
[44225] | 2739 | pImage->cbImage = cbSize;
|
---|
[31804] | 2740 |
|
---|
[31810] | 2741 | PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
| 2742 | if (pGeometry)
|
---|
| 2743 | {
|
---|
| 2744 | pGeometry->cCylinders = pLCHSGeometry->cCylinders;
|
---|
| 2745 | pGeometry->cHeads = pLCHSGeometry->cHeads;
|
---|
| 2746 | pGeometry->cSectors = pLCHSGeometry->cSectors;
|
---|
| 2747 | pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
| 2748 | }
|
---|
[31804] | 2749 | }
|
---|
| 2750 | }
|
---|
| 2751 |
|
---|
| 2752 | /* Update header information in base image file. */
|
---|
| 2753 | vdiFlushImage(pImage);
|
---|
| 2754 | }
|
---|
| 2755 | /* Same size doesn't change the image at all. */
|
---|
| 2756 |
|
---|
| 2757 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
| 2758 | return rc;
|
---|
| 2759 | }
|
---|
| 2760 |
|
---|
[63785] | 2761 | /** @copydoc VDIMAGEBACKEND::pfnDiscard */
|
---|
[44252] | 2762 | static DECLCALLBACK(int) vdiDiscard(void *pBackendData, PVDIOCTX pIoCtx,
|
---|
[63809] | 2763 | uint64_t uOffset, size_t cbDiscard,
|
---|
| 2764 | size_t *pcbPreAllocated, size_t *pcbPostAllocated,
|
---|
| 2765 | size_t *pcbActuallyDiscarded, void **ppbmAllocationBitmap,
|
---|
| 2766 | unsigned fDiscard)
|
---|
[38876] | 2767 | {
|
---|
| 2768 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
| 2769 | unsigned uBlock;
|
---|
| 2770 | unsigned offDiscard;
|
---|
| 2771 | int rc = VINF_SUCCESS;
|
---|
| 2772 | void *pvBlock = NULL;
|
---|
| 2773 |
|
---|
| 2774 | LogFlowFunc(("pBackendData=%#p pIoCtx=%#p uOffset=%llu cbDiscard=%zu pcbPreAllocated=%#p pcbPostAllocated=%#p pcbActuallyDiscarded=%#p ppbmAllocationBitmap=%#p fDiscard=%#x\n",
|
---|
| 2775 | pBackendData, pIoCtx, uOffset, cbDiscard, pcbPreAllocated, pcbPostAllocated, pcbActuallyDiscarded, ppbmAllocationBitmap, fDiscard));
|
---|
| 2776 |
|
---|
| 2777 | AssertPtr(pImage);
|
---|
| 2778 | Assert(!(uOffset % 512));
|
---|
| 2779 | Assert(!(cbDiscard % 512));
|
---|
| 2780 |
|
---|
| 2781 | AssertMsgReturn(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
| 2782 | ("Image is readonly\n"), VERR_VD_IMAGE_READ_ONLY);
|
---|
| 2783 | AssertMsgReturn( uOffset + cbDiscard <= getImageDiskSize(&pImage->Header)
|
---|
| 2784 | && cbDiscard,
|
---|
| 2785 | ("Invalid parameters uOffset=%llu cbDiscard=%zu\n",
|
---|
| 2786 | uOffset, cbDiscard),
|
---|
| 2787 | VERR_INVALID_PARAMETER);
|
---|
| 2788 |
|
---|
| 2789 | do
|
---|
| 2790 | {
|
---|
| 2791 | AssertMsgBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
| 2792 | ("Image is opened readonly\n"),
|
---|
| 2793 | rc = VERR_VD_IMAGE_READ_ONLY);
|
---|
| 2794 |
|
---|
| 2795 | AssertMsgBreakStmt(cbDiscard,
|
---|
| 2796 | ("cbDiscard=%u\n", cbDiscard),
|
---|
| 2797 | rc = VERR_INVALID_PARAMETER);
|
---|
| 2798 |
|
---|
| 2799 | /* Calculate starting block number and offset inside it. */
|
---|
| 2800 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
| 2801 | offDiscard = (unsigned)uOffset & pImage->uBlockMask;
|
---|
| 2802 |
|
---|
| 2803 | /* Clip range to at most the rest of the block. */
|
---|
| 2804 | cbDiscard = RT_MIN(cbDiscard, getImageBlockSize(&pImage->Header) - offDiscard);
|
---|
| 2805 | Assert(!(cbDiscard % 512));
|
---|
| 2806 |
|
---|
| 2807 | if (pcbPreAllocated)
|
---|
| 2808 | *pcbPreAllocated = 0;
|
---|
| 2809 |
|
---|
| 2810 | if (pcbPostAllocated)
|
---|
| 2811 | *pcbPostAllocated = 0;
|
---|
| 2812 |
|
---|
| 2813 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
| 2814 | {
|
---|
[84475] | 2815 | unsigned const cbBlock = RT_MAX(getImageBlockSize(&pImage->Header), 1);
|
---|
| 2816 | size_t const cbPreAllocated = offDiscard % cbBlock;
|
---|
| 2817 | size_t const cbPostAllocated = getImageBlockSize(&pImage->Header) - cbDiscard - cbPreAllocated;
|
---|
[38876] | 2818 | uint8_t *pbBlockData;
|
---|
| 2819 |
|
---|
| 2820 | /* Read the block data. */
|
---|
| 2821 | pvBlock = RTMemAlloc(pImage->cbTotalBlockData);
|
---|
| 2822 | if (!pvBlock)
|
---|
| 2823 | {
|
---|
| 2824 | rc = VERR_NO_MEMORY;
|
---|
| 2825 | break;
|
---|
| 2826 | }
|
---|
| 2827 |
|
---|
| 2828 | if (!cbPreAllocated && !cbPostAllocated)
|
---|
| 2829 | {
|
---|
| 2830 | /*
|
---|
| 2831 | * Discarding a whole block, don't check for allocated sectors.
|
---|
| 2832 | * It is possible to just remove the whole block which avoids
|
---|
| 2833 | * one read and checking the whole block for data.
|
---|
| 2834 | */
|
---|
| 2835 | rc = vdiDiscardBlockAsync(pImage, pIoCtx, uBlock, pvBlock);
|
---|
| 2836 | }
|
---|
| 2837 | else if (fDiscard & VD_DISCARD_MARK_UNUSED)
|
---|
| 2838 | {
|
---|
| 2839 | /* Just zero out the given range. */
|
---|
| 2840 | memset(pvBlock, 0, cbDiscard);
|
---|
| 2841 |
|
---|
| 2842 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData + pImage->offStartData + offDiscard;
|
---|
[44233] | 2843 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
| 2844 | u64Offset, pvBlock, cbDiscard, pIoCtx,
|
---|
| 2845 | NULL, NULL);
|
---|
[38876] | 2846 | RTMemFree(pvBlock);
|
---|
| 2847 | }
|
---|
| 2848 | else
|
---|
| 2849 | {
|
---|
| 2850 | /*
|
---|
| 2851 | * Read complete block as metadata, the I/O context has no memory buffer
|
---|
| 2852 | * and we need to access the content directly anyway.
|
---|
| 2853 | */
|
---|
| 2854 | PVDMETAXFER pMetaXfer;
|
---|
| 2855 | pbBlockData = (uint8_t *)pvBlock + pImage->offStartBlockData;
|
---|
| 2856 |
|
---|
| 2857 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData + pImage->offStartData;
|
---|
[44233] | 2858 | rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
| 2859 | pbBlockData, pImage->cbTotalBlockData,
|
---|
| 2860 | pIoCtx, &pMetaXfer, NULL, NULL);
|
---|
[38876] | 2861 | if (RT_FAILURE(rc))
|
---|
[42914] | 2862 | {
|
---|
| 2863 | RTMemFree(pvBlock);
|
---|
[38876] | 2864 | break;
|
---|
[42914] | 2865 | }
|
---|
[38876] | 2866 |
|
---|
| 2867 | vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
|
---|
| 2868 |
|
---|
| 2869 | /* Clear data. */
|
---|
| 2870 | memset(pbBlockData + offDiscard , 0, cbDiscard);
|
---|
| 2871 |
|
---|
| 2872 | Assert(!(cbDiscard % 4));
|
---|
| 2873 | Assert(getImageBlockSize(&pImage->Header) * 8 <= UINT32_MAX);
|
---|
| 2874 | if (ASMBitFirstSet((volatile void *)pbBlockData, getImageBlockSize(&pImage->Header) * 8) == -1)
|
---|
| 2875 | rc = vdiDiscardBlockAsync(pImage, pIoCtx, uBlock, pvBlock);
|
---|
| 2876 | else
|
---|
| 2877 | {
|
---|
| 2878 | /* Block has data, create allocation bitmap. */
|
---|
| 2879 | *pcbPreAllocated = cbPreAllocated;
|
---|
| 2880 | *pcbPostAllocated = cbPostAllocated;
|
---|
| 2881 | *ppbmAllocationBitmap = vdiAllocationBitmapCreate(pbBlockData, getImageBlockSize(&pImage->Header));
|
---|
| 2882 | if (RT_UNLIKELY(!*ppbmAllocationBitmap))
|
---|
| 2883 | rc = VERR_NO_MEMORY;
|
---|
| 2884 | else
|
---|
| 2885 | rc = VERR_VD_DISCARD_ALIGNMENT_NOT_MET;
|
---|
| 2886 |
|
---|
| 2887 | RTMemFree(pvBlock);
|
---|
| 2888 | }
|
---|
| 2889 | } /* if: no complete block discarded */
|
---|
| 2890 | } /* if: Block is allocated. */
|
---|
| 2891 | /* else: nothing to do. */
|
---|
| 2892 | } while (0);
|
---|
| 2893 |
|
---|
| 2894 | if (pcbActuallyDiscarded)
|
---|
| 2895 | *pcbActuallyDiscarded = cbDiscard;
|
---|
| 2896 |
|
---|
| 2897 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
| 2898 | return rc;
|
---|
| 2899 | }
|
---|
| 2900 |
|
---|
[63785] | 2901 | /** @copydoc VDIMAGEBACKEND::pfnRepair */
|
---|
[40240] | 2902 | static DECLCALLBACK(int) vdiRepair(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
|
---|
| 2903 | PVDINTERFACE pVDIfsImage, uint32_t fFlags)
|
---|
| 2904 | {
|
---|
| 2905 | LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
|
---|
| 2906 | int rc;
|
---|
| 2907 | PVDINTERFACEERROR pIfError;
|
---|
| 2908 | PVDINTERFACEIOINT pIfIo;
|
---|
[84883] | 2909 | PVDIOSTORAGE pStorage = NULL;
|
---|
[40240] | 2910 | uint64_t cbFile;
|
---|
| 2911 | PVDIIMAGEBLOCKPOINTER paBlocks = NULL;
|
---|
| 2912 | uint32_t *pu32BlockBitmap = NULL;
|
---|
| 2913 | VDIPREHEADER PreHdr;
|
---|
| 2914 | VDIHEADER Hdr;
|
---|
| 2915 |
|
---|
| 2916 | pIfIo = VDIfIoIntGet(pVDIfsImage);
|
---|
| 2917 | AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
|
---|
| 2918 |
|
---|
| 2919 | pIfError = VDIfErrorGet(pVDIfsDisk);
|
---|
| 2920 |
|
---|
| 2921 | do
|
---|
| 2922 | {
|
---|
| 2923 | bool fRepairBlockArray = false;
|
---|
[64784] | 2924 | bool fRepairHdr = false;
|
---|
[40240] | 2925 |
|
---|
| 2926 | rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
|
---|
| 2927 | VDOpenFlagsToFileOpenFlags( fFlags & VD_REPAIR_DRY_RUN
|
---|
| 2928 | ? VD_OPEN_FLAGS_READONLY
|
---|
| 2929 | : 0,
|
---|
| 2930 | false /* fCreate */),
|
---|
| 2931 | &pStorage);
|
---|
| 2932 | if (RT_FAILURE(rc))
|
---|
| 2933 | {
|
---|
| 2934 | rc = vdIfError( |
---|