Lines Matching refs:pImage

62 static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
63 static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
64 static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
65 static int vdiUpdateHeaderAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx);
66 static int vdiUpdateBlockInfoAsync(PVDIIMAGEDESC pImage, unsigned uBlock, PVDIOCTX pIoCtx,
199 static void vdiFlushImage(PVDIIMAGEDESC pImage)
201 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
204 int rc = vdiUpdateHeader(pImage);
206 pImage->pszFilename, rc));
207 vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
215 static int vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
221 if (pImage)
223 if (pImage->pStorage)
227 vdiFlushImage(pImage);
229 rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
230 pImage->pStorage = NULL;
233 if (pImage->paBlocks)
235 RTMemFree(pImage->paBlocks);
236 pImage->paBlocks = NULL;
239 if (pImage->paBlocksRev)
241 RTMemFree(pImage->paBlocksRev);
242 pImage->paBlocksRev = NULL;
245 if (fDelete && pImage->pszFilename)
246 vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
507 static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
509 pImage->uImageFlags = getImageFlags(&pImage->Header);
510 pImage->uImageFlags |= vdiTranslateVDI2ImageFlags(getImageType(&pImage->Header));
511 pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
512 pImage->offStartData = getImageDataOffset(&pImage->Header);
513 pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
514 pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
515 pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
516 pImage->cbTotalBlockData = pImage->offStartBlockData
517 + getImageBlockSize(&pImage->Header);
523 static int vdiCreateImage(PVDIIMAGEDESC pImage, uint64_t cbSize,
540 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
541 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
542 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
548 rc = vdIfError(pImage->pIfError, VERR_VD_VDI_COMMENT_TOO_LONG, RT_SRC_POS,
549 N_("VDI: comment is too long for '%s'"), pImage->pszFilename);
558 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
559 N_("VDI: Getting data alignment for '%s' failed (%Rrc)"), pImage->pszFilename);
564 vdiInitPreHeader(&pImage->PreHeader);
565 vdiInitHeader(&pImage->Header, uImageFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0,
569 pImage->PCHSGeometry = *pPCHSGeometry;
571 pImage->Header.u.v1plus.LCHSGeometry.cCylinders = pLCHSGeometry->cCylinders;
572 pImage->Header.u.v1plus.LCHSGeometry.cHeads = pLCHSGeometry->cHeads;
573 pImage->Header.u.v1plus.LCHSGeometry.cSectors = pLCHSGeometry->cSectors;
574 pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
576 pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
577 if (!pImage->paBlocks)
586 for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
587 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
592 for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
593 pImage->paBlocks[i] = i;
594 pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
598 vdiSetupImageDesc(pImage);
601 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
604 &pImage->pStorage);
607 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: cannot create image '%s'"),
608 pImage->pszFilename);
612 cbTotal = pImage->offStartData
613 + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
620 rc = vdIfIoIntFileGetFreeSpace(pImage->pIfIo, pImage->pszFilename, &cbFree);
623 rc = vdIfError(pImage->pIfError, VERR_DISK_FULL, RT_SRC_POS,
624 N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
635 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, cbTotal);
636 pImage->cbImage = cbTotal;
641 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->offStartData);
642 pImage->cbImage = pImage->offStartData;
646 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"),
647 pImage->pszFilename);
652 *getImageCreationUUID(&pImage->Header) = *pUuid;
655 RTUuidCreate(getImageModificationUUID(&pImage->Header));
659 vdiConvPreHeaderEndianess(VDIECONV_H2F, &PreHeader, &pImage->PreHeader);
660 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0,
664 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"),
665 pImage->pszFilename);
671 vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);
672 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
676 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"),
677 pImage->pszFilename);
681 vdiConvBlocksEndianess(VDIECONV_H2F, pImage->paBlocks, getImageBlocks(&pImage->Header));
682 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks, pImage->paBlocks,
683 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER));
684 vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, getImageBlocks(&pImage->Header));
687 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"),
688 pImage->pszFilename);
710 cbFill = (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
717 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartData + uOff,
721 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing block failed for '%s'"), pImage->pszFilename);
744 vdiFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
751 static int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
755 pImage->uOpenFlags = uOpenFlags;
757 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
758 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
759 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
764 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
766 &pImage->pStorage);
775 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage,
776 &pImage->cbImage);
779 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error getting the image size in '%s'"), pImage->pszFilename);
786 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0,
790 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
794 vdiConvPreHeaderEndianess(VDIECONV_F2H, &pImage->PreHeader, &PreHeader);
795 rc = vdiValidatePreHeader(&pImage->PreHeader);
798 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
803 pImage->Header.uVersion = pImage->PreHeader.u32Version;
804 switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
807 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
808 &pImage->Header.u.v0, sizeof(pImage->Header.u.v0));
811 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
814 vdiConvHeaderEndianessV0(VDIECONV_F2H, &pImage->Header.u.v0, &pImage->Header.u.v0);
817 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
818 &pImage->Header.u.v1, sizeof(pImage->Header.u.v1));
821 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
824 vdiConvHeaderEndianessV1(VDIECONV_F2H, &pImage->Header.u.v1, &pImage->Header.u.v1);
828 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
829 && GET_MINOR_HEADER_VERSION(&pImage->Header) == 1
830 && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
832 pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
834 pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
835 pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
836 pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
837 pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
839 else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
842 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
843 &pImage->Header.u.v1plus,
844 sizeof(pImage->Header.u.v1plus));
847 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
850 vdiConvHeaderEndianessV1p(VDIECONV_F2H, &pImage->Header.u.v1plus, &pImage->Header.u.v1plus);
854 rc = vdIfError(pImage->pIfError, VERR_VD_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: unsupported major version %u in '%s'"), GET_MAJOR_HEADER_VERSION(&pImage->Header), pImage->pszFilename);
858 rc = vdiValidateHeader(&pImage->Header);
861 rc = vdIfError(pImage->pIfError, VERR_VD_VDI_INVALID_HEADER, RT_SRC_POS, N_("VDI: invalid header in '%s'"), pImage->pszFilename);
866 vdiSetupImageDesc(pImage);
869 pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
870 if (!pImage->paBlocks)
877 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks, pImage->paBlocks,
878 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER));
881 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: Error reading the block table in '%s'"), pImage->pszFilename);
884 vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, getImageBlocks(&pImage->Header));
893 pImage->paBlocksRev = (unsigned *)RTMemAllocZ(sizeof(unsigned) * getImageBlocks(&pImage->Header));
894 if (pImage->paBlocksRev)
896 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
897 unsigned cBlocks = getImageBlocks(&pImage->Header);
900 pImage->paBlocksRev[i] = VDI_IMAGE_BLOCK_FREE;
904 VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
909 if (pImage->paBlocksRev[ptrBlock] == VDI_IMAGE_BLOCK_FREE)
910 pImage->paBlocksRev[ptrBlock] = i;
931 vdiFreeImage(pImage, false);
938 static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
941 switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
946 vdiConvHeaderEndianessV0(VDIECONV_H2F, &Hdr, &pImage->Header.u.v0);
947 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER),
952 if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
955 vdiConvHeaderEndianessV1(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1);
956 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER),
962 vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);
963 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER),
971 AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
978 static int vdiUpdateHeaderAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx)
981 switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
986 vdiConvHeaderEndianessV0(VDIECONV_H2F, &Hdr, &pImage->Header.u.v0);
987 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
993 if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
996 vdiConvHeaderEndianessV1(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1);
997 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1004 vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);
1005 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1015 ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
1022 static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
1025 int rc = vdiUpdateHeader(pImage);
1029 VDIIMAGEBLOCKPOINTER ptrBlock = RT_H2LE_U32(pImage->paBlocks[uBlock]);
1030 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
1031 pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
1034 uBlock, pImage->pszFilename, rc));
1042 static int vdiUpdateBlockInfoAsync(PVDIIMAGEDESC pImage, unsigned uBlock,
1049 rc = vdiUpdateHeaderAsync(pImage, pIoCtx);
1054 VDIIMAGEBLOCKPOINTER ptrBlock = RT_H2LE_U32(pImage->paBlocks[uBlock]);
1055 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1056 pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
1061 uBlock, pImage->pszFilename, rc));
1069 static int vdiFlushImageIoCtx(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx)
1073 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1076 rc = vdiUpdateHeaderAsync(pImage, pIoCtx);
1079 pImage->pszFilename, rc));
1080 rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage, pIoCtx, NULL, NULL);
1102 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1110 uint64_t u64Offset = (uint64_t)pDiscardAsync->idxLastBlock * pImage->cbTotalBlockData + pImage->offStartData;
1111 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
1112 pDiscardAsync->pvBlock, pImage->cbTotalBlockData, pIoCtx,
1118 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
1124 uint64_t u64Offset = (uint64_t)pDiscardAsync->ptrBlockDiscard * pImage->cbTotalBlockData + pImage->offStartData;
1125 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
1126 pDiscardAsync->pvBlock, pImage->cbTotalBlockData, pIoCtx,
1138 pImage->paBlocksRev[pDiscardAsync->idxLastBlock] = VDI_IMAGE_BLOCK_FREE;
1139 pImage->paBlocks[pDiscardAsync->uBlock] = VDI_IMAGE_BLOCK_ZERO;
1143 pImage->paBlocks[pDiscardAsync->uBlockLast] = pDiscardAsync->ptrBlockDiscard;
1144 pImage->paBlocksRev[pDiscardAsync->ptrBlockDiscard] = pDiscardAsync->uBlockLast;
1146 rc = vdiUpdateBlockInfoAsync(pImage, pDiscardAsync->uBlockLast, pIoCtx, false /* fUpdateHdr */);
1152 setImageBlocksAllocated(&pImage->Header, pDiscardAsync->idxLastBlock);
1153 rc = vdiUpdateBlockInfoAsync(pImage, pDiscardAsync->uBlock, pIoCtx, true /* fUpdateHdr */);
1158 pImage->cbImage -= pImage->cbTotalBlockData;
1159 LogFlowFunc(("Set new size %llu\n", pImage->cbImage));
1160 rc2 = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->cbImage);
1184 * @param pImage VDI image instance data.
1189 static int vdiDiscardBlockAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx,
1195 LogFlowFunc(("pImage=%#p uBlock=%u pvBlock=%#p\n",
1196 pImage, uBlock, pvBlock));
1205 pDiscardAsync->ptrBlockDiscard = pImage->paBlocks[uBlock];
1206 pDiscardAsync->idxLastBlock = getImageBlocksAllocated(&pImage->Header) - 1;
1207 pDiscardAsync->uBlockLast = pImage->paBlocksRev[pDiscardAsync->idxLastBlock];
1217 uBlock, pImage->paBlocks[uBlock]));
1223 LogFlowFunc(("Discard last block [%u]=%u\n", uBlock, pImage->paBlocks[uBlock]));
1227 rc = vdiDiscardBlockAsyncUpdate(pImage, pIoCtx, pDiscardAsync, VINF_SUCCESS);
1288 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1293 pImage->cbImage += pImage->cbTotalBlockData;
1294 pImage->paBlocks[pBlockAlloc->uBlock] = pBlockAlloc->cBlocksAllocated;
1296 if (pImage->paBlocksRev)
1297 pImage->paBlocksRev[pBlockAlloc->cBlocksAllocated] = pBlockAlloc->uBlock;
1299 setImageBlocksAllocated(&pImage->Header, pBlockAlloc->cBlocksAllocated + 1);
1300 rc = vdiUpdateBlockInfoAsync(pImage, pBlockAlloc->uBlock, pIoCtx,
1315 PVDIIMAGEDESC pImage;
1324 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
1325 if (!pImage)
1330 pImage->pszFilename = pszFilename;
1331 pImage->pStorage = NULL;
1332 pImage->paBlocks = NULL;
1333 pImage->pVDIfsDisk = pVDIfsDisk;
1334 pImage->pVDIfsImage = pVDIfsImage;
1336 rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
1337 vdiFreeImage(pImage, false);
1338 RTMemFree(pImage);
1355 PVDIIMAGEDESC pImage;
1374 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
1375 if (!pImage)
1380 pImage->pszFilename = pszFilename;
1381 pImage->pStorage = NULL;
1382 pImage->paBlocks = NULL;
1383 pImage->pVDIfsDisk = pVDIfsDisk;
1384 pImage->pVDIfsImage = pVDIfsImage;
1386 rc = vdiOpenImage(pImage, uOpenFlags);
1388 *ppBackendData = pImage;
1390 RTMemFree(pImage);
1410 PVDIIMAGEDESC pImage;
1464 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
1465 if (!pImage)
1470 pImage->pszFilename = pszFilename;
1471 pImage->pStorage = NULL;
1472 pImage->paBlocks = NULL;
1473 pImage->pVDIfsDisk = pVDIfsDisk;
1474 pImage->pVDIfsImage = pVDIfsImage;
1476 rc = vdiCreateImage(pImage, cbSize, uImageFlags, pszComment,
1486 vdiFreeImage(pImage, false);
1487 rc = vdiOpenImage(pImage, uOpenFlags);
1490 RTMemFree(pImage);
1494 *ppBackendData = pImage;
1497 RTMemFree(pImage);
1510 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1513 if ( !pImage
1522 rc = vdiFreeImage(pImage, false);
1526 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1530 int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
1537 /* Update pImage with the new information. */
1538 pImage->pszFilename = pszFilename;
1541 rc = vdiOpenImage(pImage, pImage->uOpenFlags);
1555 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1558 rc = vdiFreeImage(pImage, fDelete);
1559 RTMemFree(pImage);
1570 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1575 AssertPtr(pImage);
1579 if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
1588 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
1589 offRead = (unsigned)uOffset & pImage->uBlockMask;
1592 cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
1595 if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
1597 else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
1601 cbSet = vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
1609 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
1610 + (pImage->offStartData + pImage->offStartBlockData + offRead);
1612 if (u64Offset + cbToRead <= pImage->cbImage)
1613 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, u64Offset,
1618 u64Offset, pImage->pszFilename, pImage->cbImage));
1619 vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
1638 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1643 AssertPtr(pImage);
1647 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1664 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
1665 offWrite = (unsigned)uOffset & pImage->uBlockMask;
1668 cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
1673 if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
1676 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
1677 && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
1678 || cbToWrite == getImageBlockSize(&pImage->Header)))
1684 if (vdIfIoIntIoCtxIsZero(pImage->pIfIo, pIoCtx, cbToWrite, true))
1686 pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
1693 if ( cbToWrite == getImageBlockSize(&pImage->Header)
1706 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
1707 uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
1708 + (pImage->offStartData + pImage->offStartBlockData);
1716 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1727 rc = vdiBlockAllocUpdate(pImage, pIoCtx, pBlockAlloc, rc);
1733 *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
1734 *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
1741 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
1742 + (pImage->offStartData + pImage->offStartBlockData + offWrite);
1743 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1759 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1762 Assert(pImage);
1764 rc = vdiFlushImageIoCtx(pImage, pIoCtx);
1773 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1776 AssertPtr(pImage);
1778 if (pImage)
1779 uVersion = pImage->PreHeader.u32Version;
1791 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1794 AssertPtr(pImage);
1796 if (pImage && pImage->pStorage)
1807 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1810 AssertPtr(pImage);
1812 if (pImage)
1813 cbSize = getImageDiskSize(&pImage->Header);
1825 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1828 AssertPtr(pImage);
1830 if (pImage)
1833 if (pImage->pStorage)
1835 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1849 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1852 AssertPtr(pImage);
1854 if (pImage)
1856 if (pImage->PCHSGeometry.cCylinders)
1858 *pPCHSGeometry = pImage->PCHSGeometry;
1875 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1878 AssertPtr(pImage);
1880 if (pImage)
1882 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1888 pImage->PCHSGeometry = *pPCHSGeometry;
1903 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1906 AssertPtr(pImage);
1908 if (pImage)
1911 PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
1938 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1942 AssertPtr(pImage);
1944 if (pImage)
1946 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1952 pGeometry = getImageLCHSGeometry(&pImage->Header);
1961 vdiFlushImage(pImage);
1977 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1980 AssertPtr(pImage);
1982 if (pImage)
1983 uImageFlags = pImage->uImageFlags;
1995 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1998 AssertPtr(pImage);
2000 if (pImage)
2001 uOpenFlags = pImage->uOpenFlags;
2013 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2018 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2028 pszFilename = pImage->pszFilename;
2029 rc = vdiFreeImage(pImage, false);
2032 rc = vdiOpenImage(pImage, uOpenFlags);
2044 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2047 AssertPtr(pImage);
2049 if (pImage)
2051 char *pszTmp = getImageComment(&pImage->Header);
2079 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2082 AssertPtr(pImage);
2084 if (pImage)
2086 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2099 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2104 memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
2105 memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
2108 rc = vdiUpdateHeader(pImage);
2126 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2129 AssertPtr(pImage);
2131 if (pImage)
2133 *pUuid = *getImageCreationUUID(&pImage->Header);
2147 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2150 AssertPtr(pImage);
2152 if (pImage)
2154 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2156 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2157 pImage->Header.u.v1.uuidCreate = *pUuid;
2159 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
2160 pImage->Header.u.v0.uuidCreate = *pUuid;
2181 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2184 AssertPtr(pImage);
2186 if (pImage)
2188 *pUuid = *getImageModificationUUID(&pImage->Header);
2202 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2205 AssertPtr(pImage);
2207 if (pImage)
2209 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2211 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2212 pImage->Header.u.v1.uuidModify = *pUuid;
2214 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
2215 pImage->Header.u.v0.uuidModify = *pUuid;
2236 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2239 AssertPtr(pImage);
2241 if (pImage)
2243 *pUuid = *getImageParentUUID(&pImage->Header);
2257 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2260 AssertPtr(pImage);
2262 if (pImage)
2264 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2266 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2267 pImage->Header.u.v1.uuidLinkage = *pUuid;
2269 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
2270 pImage->Header.u.v0.uuidLinkage = *pUuid;
2291 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2294 AssertPtr(pImage);
2296 if (pImage)
2298 *pUuid = *getImageParentModificationUUID(&pImage->Header);
2312 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2315 AssertPtr(pImage);
2317 if (pImage)
2319 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2321 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2322 pImage->Header.u.v1.uuidParentModify = *pUuid;
2342 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2344 vdIfErrorMessage(pImage->pIfError, "Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%#p\n",
2345 pImage->pszFilename,
2346 (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
2347 pImage->uOpenFlags,
2348 pImage->pStorage);
2349 vdIfErrorMessage(pImage->pIfError, "Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
2350 pImage->PreHeader.u32Version,
2351 getImageType(&pImage->Header),
2352 getImageFlags(&pImage->Header),
2353 getImageDiskSize(&pImage->Header));
2354 vdIfErrorMessage(pImage->pIfError, "Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
2355 getImageBlockSize(&pImage->Header),
2356 getImageExtraBlockSize(&pImage->Header),
2357 getImageBlocks(&pImage->Header),
2358 getImageBlocksAllocated(&pImage->Header));
2359 vdIfErrorMessage(pImage->pIfError, "Header: offBlocks=%u offData=%u\n",
2360 getImageBlocksOffset(&pImage->Header),
2361 getImageDataOffset(&pImage->Header));
2362 PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
2364 vdIfErrorMessage(pImage->pIfError, "Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
2366 vdIfErrorMessage(pImage->pIfError, "Header: uuidCreation={%RTuuid}\n", getImageCreationUUID(&pImage->Header));
2367 vdIfErrorMessage(pImage->pIfError, "Header: uuidModification={%RTuuid}\n", getImageModificationUUID(&pImage->Header));
2368 vdIfErrorMessage(pImage->pIfError, "Header: uuidParent={%RTuuid}\n", getImageParentUUID(&pImage->Header));
2369 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
2370 vdIfErrorMessage(pImage->pIfError, "Header: uuidParentModification={%RTuuid}\n", getImageParentModificationUUID(&pImage->Header));
2371 vdIfErrorMessage(pImage->pIfError, "Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
2372 pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
2373 vdIfErrorMessage(pImage->pIfError, "Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
2374 pImage->uBlockMask,
2375 pImage->cbTotalBlockData,
2376 pImage->uShiftOffset2Index,
2377 pImage->offStartBlockData);
2379 unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
2382 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
2385 if (pImage->paBlocks[uBlock] >= cBlocks)
2389 if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
2391 vdIfErrorMessage(pImage->pIfError, "!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
2392 cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
2396 vdIfErrorMessage(pImage->pIfError, "!! WARNING: %u bad blocks found !!\n",
2406 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2427 AssertBreakStmt(pImage, rc = VERR_INVALID_PARAMETER);
2429 AssertBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
2435 cBlocks = getImageBlocks(&pImage->Header);
2436 cbBlock = getImageBlockSize(&pImage->Header);
2446 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2448 unsigned cBlocksAllocated = (unsigned)((cbFile - pImage->offStartData - pImage->offStartBlockData) >> pImage->uShiftOffset2Index);
2467 VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
2477 i, pImage->pszFilename));
2478 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
2479 rc = vdiUpdateBlockInfo(pImage, i);
2487 i, pImage->pszFilename));
2488 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
2489 rc = vdiUpdateBlockInfo(pImage, i);
2503 VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
2507 uint64_t u64Offset = (uint64_t)ptrBlock * pImage->cbTotalBlockData
2508 + (pImage->offStartData + pImage->offStartBlockData);
2509 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset, pvTmp, cbBlock);
2515 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
2516 rc = vdiUpdateBlockInfo(pImage, i);
2530 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
2531 rc = vdiUpdateBlockInfo(pImage, i);
2542 ptrBlock = pImage->paBlocks[i];
2553 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
2554 rc = vdiUpdateBlockInfo(pImage, i);
2591 uint64_t u64Offset = (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
2592 + (pImage->offStartData + pImage->offStartBlockData);
2593 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset,
2595 u64Offset = (uint64_t)i * pImage->cbTotalBlockData
2596 + (pImage->offStartData + pImage->offStartBlockData);
2597 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, u64Offset,
2599 pImage->paBlocks[uBlockData] = i;
2600 setImageBlocksAllocated(&pImage->Header, cBlocksAllocated - cBlocksMoved);
2601 rc = vdiUpdateBlockInfo(pImage, uBlockData);
2622 setImageBlocksAllocated(&pImage->Header, uBlockUsedPos);
2623 vdiUpdateHeader(pImage);
2626 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
2627 (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
2628 + pImage->offStartData + pImage->offStartBlockData);
2656 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2670 if ( cbSize < getImageDiskSize(&pImage->Header)
2671 || GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0
2672 || pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
2674 else if (cbSize > getImageDiskSize(&pImage->Header))
2676 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header); /** < Blocks currently allocated, doesn't change during resize */
2677 uint32_t cBlocksNew = cbSize / getImageBlockSize(&pImage->Header); /** < New number of blocks in the image after the resize */
2678 if (cbSize % getImageBlockSize(&pImage->Header))
2681 uint32_t cBlocksOld = getImageBlocks(&pImage->Header); /** < Number of blocks before the resize. */
2683 uint64_t offStartDataNew = RT_ALIGN_32(pImage->offStartBlocks + cbBlockspaceNew, VDI_DATA_ALIGN); /** < New start offset for block data after the resize */
2685 if ( pImage->offStartData != offStartDataNew
2689 uint64_t cbOverlapping = offStartDataNew - pImage->offStartData;
2690 unsigned cBlocksReloc = cbOverlapping / getImageBlockSize(&pImage->Header);
2691 if (cbOverlapping % getImageBlockSize(&pImage->Header))
2697 offStartDataNew = pImage->offStartData;
2710 pvBuf = RTMemAllocZ(pImage->cbTotalBlockData);
2718 pvZero = RTMemAllocZ(pImage->cbTotalBlockData);
2730 if (!pImage->paBlocks[idxBlock])
2733 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
2735 pImage->cbTotalBlockData);
2740 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &offBlockAppend);
2744 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2746 pImage->cbTotalBlockData);
2751 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2753 pImage->cbTotalBlockData);
2758 pImage->paBlocks[idxBlock] = cBlocksAllocated - 1;
2770 && IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[idxBlock2]))
2771 pImage->paBlocks[idxBlock2]--;
2782 offStartDataNew += pImage->cbTotalBlockData;
2796 pImage->offStartData = offStartDataNew;
2797 setImageDataOffset(&pImage->Header, offStartDataNew);
2805 PVDIIMAGEBLOCKPOINTER paBlocksNew = (PVDIIMAGEBLOCKPOINTER)RTMemRealloc(pImage->paBlocks, cbBlockspaceNew);
2808 pImage->paBlocks = paBlocksNew;
2812 pImage->paBlocks[idxBlock] = VDI_IMAGE_BLOCK_FREE;
2818 vdiConvBlocksEndianess(VDIECONV_H2F, pImage->paBlocks, cBlocksNew);
2819 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks,
2820 pImage->paBlocks, cbBlockspaceNew);
2821 vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, cBlocksNew);
2826 setImageDiskSize(&pImage->Header, cbSize);
2827 setImageBlocks(&pImage->Header, cBlocksNew);
2829 pImage->PCHSGeometry = *pPCHSGeometry;
2830 pImage->cbImage = cbSize;
2832 PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
2844 vdiFlushImage(pImage);
2861 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2870 AssertPtr(pImage);
2874 AssertMsgReturn(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
2876 AssertMsgReturn( uOffset + cbDiscard <= getImageDiskSize(&pImage->Header)
2884 AssertMsgBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
2893 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
2894 offDiscard = (unsigned)uOffset & pImage->uBlockMask;
2897 cbDiscard = RT_MIN(cbDiscard, getImageBlockSize(&pImage->Header) - offDiscard);
2906 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
2911 cbPreAllocated = offDiscard % getImageBlockSize(&pImage->Header);
2912 cbPostAllocated = getImageBlockSize(&pImage->Header) - cbDiscard - cbPreAllocated;
2915 pvBlock = RTMemAlloc(pImage->cbTotalBlockData);
2929 rc = vdiDiscardBlockAsync(pImage, pIoCtx, uBlock, pvBlock);
2936 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData + pImage->offStartData + offDiscard;
2937 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
2949 pbBlockData = (uint8_t *)pvBlock + pImage->offStartBlockData;
2951 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData + pImage->offStartData;
2952 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
2953 pbBlockData, pImage->cbTotalBlockData,
2961 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
2967 Assert(getImageBlockSize(&pImage->Header) * 8 <= UINT32_MAX);
2968 if (ASMBitFirstSet((volatile void *)pbBlockData, getImageBlockSize(&pImage->Header) * 8) == -1)
2969 rc = vdiDiscardBlockAsync(pImage, pIoCtx, uBlock, pvBlock);
2975 *ppbmAllocationBitmap = vdiAllocationBitmapCreate(pbBlockData, getImageBlockSize(&pImage->Header));