Changeset 40935 in vbox for trunk/src/VBox/Storage
- Timestamp:
- Apr 16, 2012 8:55:15 AM (13 years ago)
- Location:
- trunk/src/VBox/Storage
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Storage/VDI.cpp
r40906 r40935 32 32 33 33 #define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M 34 35 /** Macros for endianess conversion. */36 #define SET_ENDIAN_U32(conv, u32) (conv == VDIECONV_H2F ? RT_H2LE_U32(u32) : RT_LE2H_U32(u32))37 #define SET_ENDIAN_U64(conv, u64) (conv == VDIECONV_H2F ? RT_H2LE_U64(u64) : RT_LE2H_U64(u64))38 34 39 35 /******************************************************************************* … … 66 62 67 63 /** 68 * Internal: Convert the PreHeader fields to the appropriate endianess.69 * @param enmConv Direction of the conversion.70 * @param pPreHdrConv Where to store the converted pre header.71 * @param pPreHdr PreHeader pointer.72 */73 static void vdiConvPreHeaderEndianess(VDIECONV enmConv, PVDIPREHEADER pPreHdrConv,74 PVDIPREHEADER pPreHdr)75 {76 pPreHdrConv->u32Signature = SET_ENDIAN_U32(enmConv, pPreHdr->u32Signature);77 pPreHdrConv->u32Version = SET_ENDIAN_U32(enmConv, pPreHdr->u32Version);78 }79 80 /**81 * Internal: Convert the VDIDISKGEOMETRY fields to the appropriate endianess.82 * @param enmConv Direction of the conversion.83 * @param pDiskGeoConv Where to store the converted geometry.84 * @param pDiskGeo Pointer to the disk geometry to convert.85 */86 static void vdiConvGeometryEndianess(VDIECONV enmConv, PVDIDISKGEOMETRY pDiskGeoConv,87 PVDIDISKGEOMETRY pDiskGeo)88 {89 pDiskGeoConv->cCylinders = SET_ENDIAN_U32(enmConv, pDiskGeo->cCylinders);90 pDiskGeoConv->cHeads = SET_ENDIAN_U32(enmConv, pDiskGeo->cHeads);91 pDiskGeoConv->cSectors = SET_ENDIAN_U32(enmConv, pDiskGeo->cSectors);92 pDiskGeoConv->cbSector = SET_ENDIAN_U32(enmConv, pDiskGeo->cbSector);93 }94 95 /**96 * Internal: Convert the Header - version 0 fields to the appropriate endianess.97 * @param enmConv Direction of the conversion.98 * @param pHdrConv Where to store the converted header.99 * @param pHdr Pointer to the version 0 header.100 */101 static void vdiConvHeaderEndianessV0(VDIECONV enmConv, PVDIHEADER0 pHdrConv,102 PVDIHEADER0 pHdr)103 {104 pHdrConv->u32Type = SET_ENDIAN_U32(enmConv, pHdr->u32Type);105 pHdrConv->fFlags = SET_ENDIAN_U32(enmConv, pHdr->fFlags);106 vdiConvGeometryEndianess(enmConv, &pHdrConv->LegacyGeometry, &pHdr->LegacyGeometry);107 pHdrConv->cbDisk = SET_ENDIAN_U64(enmConv, pHdr->cbDisk);108 pHdrConv->cbBlock = SET_ENDIAN_U32(enmConv, pHdr->cbBlock);109 pHdrConv->cBlocks = SET_ENDIAN_U32(enmConv, pHdr->cBlocks);110 pHdrConv->cBlocksAllocated = SET_ENDIAN_U32(enmConv, pHdr->cBlocksAllocated);111 /* Don't touch the RTUUID fields. */112 }113 114 /**115 * Internal: Set the Header - version 1 fields to the appropriate endianess.116 * @param enmConv Direction of the conversion.117 * @param pHdrConv Where to store the converted header.118 * @param pHdr Version 1 Header pointer.119 */120 static void vdiConvHeaderEndianessV1(VDIECONV enmConv, PVDIHEADER1 pHdrConv,121 PVDIHEADER1 pHdr)122 {123 pHdrConv->cbHeader = SET_ENDIAN_U32(enmConv, pHdr->cbHeader);124 pHdrConv->u32Type = SET_ENDIAN_U32(enmConv, pHdr->u32Type);125 pHdrConv->fFlags = SET_ENDIAN_U32(enmConv, pHdr->fFlags);126 pHdrConv->offBlocks = SET_ENDIAN_U32(enmConv, pHdr->offBlocks);127 pHdrConv->offData = SET_ENDIAN_U32(enmConv, pHdr->offData);128 vdiConvGeometryEndianess(enmConv, &pHdrConv->LegacyGeometry, &pHdr->LegacyGeometry);129 pHdrConv->u32Dummy = SET_ENDIAN_U32(enmConv, pHdr->u32Dummy);130 pHdrConv->cbDisk = SET_ENDIAN_U64(enmConv, pHdr->cbDisk);131 pHdrConv->cbBlock = SET_ENDIAN_U32(enmConv, pHdr->cbBlock);132 pHdrConv->cbBlockExtra = SET_ENDIAN_U32(enmConv, pHdr->cbBlockExtra);133 pHdrConv->cBlocks = SET_ENDIAN_U32(enmConv, pHdr->cBlocks);134 pHdrConv->cBlocksAllocated = SET_ENDIAN_U32(enmConv, pHdr->cBlocksAllocated);135 /* don't touch the RTUUID */136 }137 138 /**139 * Internal: Set the Header - version 1plus fields to the appropriate endianess.140 * @param enmConv Direction of the conversion.141 * @param pHdrConv Where to store the converted header.142 * @param pHdr Version 1+ Header pointer.143 */144 static void vdiConvHeaderEndianessV1p(VDIECONV enmConv, PVDIHEADER1PLUS pHdrConv,145 PVDIHEADER1PLUS pHdr)146 {147 pHdrConv->cbHeader = SET_ENDIAN_U32(enmConv, pHdr->cbHeader);148 pHdrConv->u32Type = SET_ENDIAN_U32(enmConv, pHdr->u32Type);149 pHdrConv->fFlags = SET_ENDIAN_U32(enmConv, pHdr->fFlags);150 pHdrConv->offBlocks = SET_ENDIAN_U32(enmConv, pHdr->offBlocks);151 pHdrConv->offData = SET_ENDIAN_U32(enmConv, pHdr->offData);152 vdiConvGeometryEndianess(enmConv, &pHdrConv->LegacyGeometry, &pHdr->LegacyGeometry);153 pHdrConv->u32Dummy = SET_ENDIAN_U32(enmConv, pHdr->u32Dummy);154 pHdrConv->cbDisk = SET_ENDIAN_U64(enmConv, pHdr->cbDisk);155 pHdrConv->cbBlock = SET_ENDIAN_U32(enmConv, pHdr->cbBlock);156 pHdrConv->cbBlockExtra = SET_ENDIAN_U32(enmConv, pHdr->cbBlockExtra);157 pHdrConv->cBlocks = SET_ENDIAN_U32(enmConv, pHdr->cBlocks);158 pHdrConv->cBlocksAllocated = SET_ENDIAN_U32(enmConv, pHdr->cBlocksAllocated);159 /* don't touch the RTUUID */160 vdiConvGeometryEndianess(enmConv, &pHdrConv->LCHSGeometry, &pHdr->LCHSGeometry);161 }162 163 /**164 * Internal: Set the appropriate endianess on all the Blocks pointed.165 * @param enmConv Direction of the conversion.166 * @param paBlocks Pointer to the block array.167 * @param cEntries Number of entries in the block array.168 *169 * @note Unlike the other conversion functions this method does an in place conversion170 * to avoid temporary memory allocations when writing the block array.171 */172 static void vdiConvBlocksEndianess(VDIECONV enmConv, PVDIIMAGEBLOCKPOINTER paBlocks,173 unsigned cEntries)174 {175 for (unsigned i = 0; i < cEntries; i++)176 paBlocks[i] = SET_ENDIAN_U32(enmConv, paBlocks[i]);177 }178 179 /**180 64 * Internal: Flush the image file to disk. 181 65 */ … … 625 509 626 510 /* Write pre-header. */ 627 VDIPREHEADER PreHeader;628 vdiConvPreHeaderEndianess(VDIECONV_H2F, &PreHeader, &pImage->PreHeader);629 511 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0, 630 & PreHeader, sizeof(PreHeader), NULL);512 &pImage->PreHeader, sizeof(pImage->PreHeader), NULL); 631 513 if (RT_FAILURE(rc)) 632 514 { … … 637 519 638 520 /* Write header. */ 639 VDIHEADER1PLUS Hdr;640 vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);641 521 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader), 642 & Hdr, sizeof(Hdr), NULL);522 &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL); 643 523 if (RT_FAILURE(rc)) 644 524 { … … 648 528 } 649 529 650 vdiConvBlocksEndianess(VDIECONV_H2F, pImage->paBlocks, getImageBlocks(&pImage->Header));651 530 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks, pImage->paBlocks, 652 531 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER), 653 532 NULL); 654 vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, getImageBlocks(&pImage->Header));655 533 if (RT_FAILURE(rc)) 656 534 { … … 753 631 754 632 /* Read pre-header. */ 755 VDIPREHEADER PreHeader;756 633 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0, 757 & PreHeader, sizeof(PreHeader), NULL);634 &pImage->PreHeader, sizeof(pImage->PreHeader), NULL); 758 635 if (RT_FAILURE(rc)) 759 636 { … … 762 639 goto out; 763 640 } 764 vdiConvPreHeaderEndianess(VDIECONV_F2H, &pImage->PreHeader, &PreHeader);765 641 rc = vdiValidatePreHeader(&pImage->PreHeader); 766 642 if (RT_FAILURE(rc)) … … 783 659 goto out; 784 660 } 785 vdiConvHeaderEndianessV0(VDIECONV_F2H, &pImage->Header.u.v0, &pImage->Header.u.v0);786 661 break; 787 662 case 1: … … 794 669 goto out; 795 670 } 796 vdiConvHeaderEndianessV1(VDIECONV_F2H, &pImage->Header.u.v1, &pImage->Header.u.v1);797 671 /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write mode. 798 672 * Conversion is harmless, as any VirtualBox version supporting VDI … … 820 694 goto out; 821 695 } 822 vdiConvHeaderEndianessV1p(VDIECONV_F2H, &pImage->Header.u.v1plus, &pImage->Header.u.v1plus);823 696 } 824 697 break; … … 850 723 getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER), 851 724 NULL); 852 if (RT_FAILURE(rc))853 {854 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: Error reading the block table in '%s'"), pImage->pszFilename);855 goto out;856 }857 vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, getImageBlocks(&pImage->Header));858 725 859 726 if (uOpenFlags & VD_OPEN_FLAGS_DISCARD) … … 915 782 { 916 783 case 0: 917 {918 VDIHEADER0 Hdr;919 vdiConvHeaderEndianessV0(VDIECONV_H2F, &Hdr, &pImage->Header.u.v0);920 784 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER), 921 &Hdr, sizeof(Hdr), NULL); 785 &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), 786 NULL); 922 787 break; 923 }924 788 case 1: 925 789 if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus)) 926 {927 VDIHEADER1 Hdr;928 vdiConvHeaderEndianessV1(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1);929 790 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER), 930 & Hdr, sizeof(Hdr), NULL);931 }791 &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), 792 NULL); 932 793 else 933 {934 VDIHEADER1PLUS Hdr;935 vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);936 794 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER), 937 & Hdr, sizeof(Hdr), NULL);938 }795 &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), 796 NULL); 939 797 break; 940 798 default: … … 955 813 { 956 814 case 0: 957 {958 VDIHEADER0 Hdr;959 vdiConvHeaderEndianessV0(VDIECONV_H2F, &Hdr, &pImage->Header.u.v0);960 815 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage, 961 sizeof(VDIPREHEADER), &Hdr, sizeof(Hdr), 816 sizeof(VDIPREHEADER), &pImage->Header.u.v0, 817 sizeof(pImage->Header.u.v0), 962 818 pIoCtx, NULL, NULL); 963 819 break; 964 }965 820 case 1: 966 821 if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus)) 967 {968 VDIHEADER1 Hdr;969 vdiConvHeaderEndianessV1(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1);970 822 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage, 971 sizeof(VDIPREHEADER), &Hdr, sizeof(Hdr), 823 sizeof(VDIPREHEADER), &pImage->Header.u.v1, 824 sizeof(pImage->Header.u.v1), 972 825 pIoCtx, NULL, NULL); 973 }974 826 else 975 {976 VDIHEADER1PLUS Hdr;977 vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);978 827 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage, 979 sizeof(VDIPREHEADER), &Hdr, sizeof(Hdr), 828 sizeof(VDIPREHEADER), &pImage->Header.u.v1plus, 829 sizeof(pImage->Header.u.v1plus), 980 830 pIoCtx, NULL, NULL); 981 }982 831 break; 983 832 default: … … 1000 849 { 1001 850 /* write only one block pointer. */ 1002 VDIIMAGEBLOCKPOINTER ptrBlock = RT_H2LE_U32(pImage->paBlocks[uBlock]);1003 851 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 1004 852 pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER), 1005 &p trBlock, sizeof(VDIIMAGEBLOCKPOINTER),853 &pImage->paBlocks[uBlock], sizeof(VDIIMAGEBLOCKPOINTER), 1006 854 NULL); 1007 855 AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n", … … 1026 874 { 1027 875 /* write only one block pointer. */ 1028 VDIIMAGEBLOCKPOINTER ptrBlock = RT_H2LE_U32(pImage->paBlocks[uBlock]);1029 876 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage, 1030 877 pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER), 1031 &ptrBlock, sizeof(VDIIMAGEBLOCKPOINTER), 878 &pImage->paBlocks[uBlock], 879 sizeof(VDIIMAGEBLOCKPOINTER), 1032 880 pIoCtx, NULL, NULL); 1033 881 AssertMsg(RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS, … … 3032 2880 3033 2881 /* Write the block array before updating the rest. */ 3034 vdiConvBlocksEndianess(VDIECONV_H2F, pImage->paBlocks, cBlocksNew);3035 2882 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks, 3036 2883 pImage->paBlocks, cbBlockspaceNew, NULL); 3037 vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, cBlocksNew);3038 2884 3039 2885 if (RT_SUCCESS(rc)) … … 3386 3232 break; 3387 3233 } 3388 vdiConvPreHeaderEndianess(VDIECONV_F2H, &PreHdr, &PreHdr);3389 3234 rc = vdiValidatePreHeader(&PreHdr); 3390 3235 if (RT_FAILURE(rc)) … … 3396 3241 3397 3242 /* Read header. */ 3398 Hdr.uVersion = PreHdr.u32Version;3243 Hdr.uVersion = RT_H2LE_U32(PreHdr.u32Version); 3399 3244 switch (GET_MAJOR_HEADER_VERSION(&Hdr)) 3400 3245 { … … 3406 3251 rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), 3407 3252 pszFilename); 3408 vdiConvHeaderEndianessV0(VDIECONV_F2H, &Hdr.u.v0, &Hdr.u.v0);3409 3253 break; 3410 3254 case 1: … … 3416 3260 pszFilename); 3417 3261 } 3418 vdiConvHeaderEndianessV1(VDIECONV_F2H, &Hdr.u.v1, &Hdr.u.v1);3419 3262 if (Hdr.u.v1.cbHeader >= sizeof(Hdr.u.v1plus)) 3420 3263 { … … 3426 3269 rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), 3427 3270 pszFilename); 3428 vdiConvHeaderEndianessV1p(VDIECONV_F2H, &Hdr.u.v1plus, &Hdr.u.v1plus);3429 3271 } 3430 3272 break; … … 3475 3317 break; 3476 3318 } 3477 vdiConvBlocksEndianess(VDIECONV_F2H, paBlocks, getImageBlocks(&Hdr)); 3319 3320 for (uint32_t i = 0; i < getImageBlocks(&Hdr); i++) 3321 paBlocks[i] = RT_LE2H_U32(paBlocks[i]); 3478 3322 3479 3323 pu32BlockBitmap = (uint32_t *)RTMemAllocZ(RT_ALIGN_Z(getImageBlocks(&Hdr) / 8, 4)); … … 3518 3362 else if (!(fFlags & VD_REPAIR_DRY_RUN)) 3519 3363 { 3364 for (uint32_t i = 0; i < getImageBlocks(&Hdr); i++) 3365 paBlocks[i] = RT_H2LE_U32(paBlocks[i]); 3366 3520 3367 vdIfErrorMessage(pIfError, "Writing repaired block allocation table...\n"); 3521 3368 3522 vdiConvBlocksEndianess(VDIECONV_H2F, paBlocks, getImageBlocks(&Hdr));3523 3369 rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, offStartBlocks, paBlocks, 3524 3370 getImageBlocks(&Hdr) * sizeof(VDIIMAGEBLOCKPOINTER), -
trunk/src/VBox/Storage/VDICore.h
r40906 r40935 605 605 } VDIASYNCBLOCKALLOC, *PVDIASYNCBLOCKALLOC; 606 606 607 /**608 * Endianess conversion direction.609 */610 typedef enum VDIECONV611 {612 /** Host to file endianess. */613 VDIECONV_H2F = 0,614 /** File to host endianess. */615 VDIECONV_F2H616 } VDIECONV;617 618 607 #endif 619 608
Note:
See TracChangeset
for help on using the changeset viewer.