VirtualBox

Changeset 7231 in vbox


Ignore:
Timestamp:
Mar 3, 2008 11:08:55 AM (17 years ago)
Author:
vboxsync
Message:

Fixed a couple of long-standing thinkos (one can't just add shift values, fortunately never triggered as the per-block extra data was always 0 bytes). Cleaned up the code even more.

Location:
trunk/src/VBox/Devices/Storage
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Storage/VDICore.h

    r7152 r7231  
    539539    /** Block shift value for converting byte hdd offset into paBlock index. */
    540540    unsigned                uShiftOffset2Index;
     541#ifndef VBOX_VDICORE_VD
    541542    /** Block shift value for converting block index into offset in image. */
    542543    unsigned                uShiftIndex2Offset;
     544#endif /* !VBOX_VDICORE_VD */
    543545    /** Offset of data from the beginning of block. */
    544546    unsigned                offStartBlockData;
     547#ifndef VBOX_VDICORE_VD
    545548    /** Image is modified flags (VDI_IMAGE_MODIFIED*). */
    546549    unsigned                fModified;
    547 #ifndef VBOX_VDICORE_VD
    548550    /** Container filename. (UTF-8)
    549551     * @todo Make this variable length to save a bunch of bytes. (low prio) */
    550552    char                    szFilename[RTPATH_MAX];
    551 #else /* !VBOX_VDICORE_VD */
     553#else /* VBOX_VDICORE_VD */
     554    /** Total size of image block (including the extra data). */
     555    unsigned                cbTotalBlockData;
    552556    /** Container filename. (UTF-8) */
    553557    const char             *pszFilename;
     
    558562    /** Opaque data for error callback. */
    559563    void                   *pvErrorUser;
    560 #endif /* !VBOX_VDICORE_VD */
     564#endif /* VBOX_VDICORE_VD */
    561565} VDIIMAGEDESC, *PVDIIMAGEDESC;
    562566
  • trunk/src/VBox/Devices/Storage/VDIHDDCore.cpp

    r7159 r7231  
    4040static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
    4141static int  vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
    42 static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType, uint32_t uImageFlags,
    43                           const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
     42static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType,
     43                          uint32_t uImageFlags, const char *pszComment,
     44                          uint64_t cbDisk, uint32_t cbBlock,
    4445                          uint32_t cbBlockExtra);
    4546static int  vdiValidateHeader(PVDIHEADER pHeader);
     
    113114 * @param   pHeader     Assumes it was initially initialized to all zeros.
    114115 */
    115 static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType, uint32_t uImageFlags,
    116                           const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
     116static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType,
     117                          uint32_t uImageFlags, const char *pszComment,
     118                          uint64_t cbDisk, uint32_t cbBlock,
    117119                          uint32_t cbBlockExtra)
    118120{
     
    296298    pImage->offStartData       = getImageDataOffset(&pImage->Header);
    297299    pImage->uBlockMask         = getImageBlockSize(&pImage->Header) - 1;
    298     pImage->uShiftIndex2Offset =
    299300    pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
    300301    pImage->offStartBlockData  = getImageExtraBlockSize(&pImage->Header);
    301     if (pImage->offStartBlockData != 0)
    302         pImage->uShiftIndex2Offset += getPowerOfTwo(pImage->offStartBlockData);
     302    pImage->cbTotalBlockData   =   pImage->offStartBlockData
     303                                 + getImageBlockSize(&pImage->Header);
    303304}
    304305
     
    375376
    376377    cbTotal =   pImage->offStartData
    377               + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
     378              + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
    378379
    379380    if (enmType == VD_IMAGE_TYPE_FIXED)
     
    456457        }
    457458
    458         cbFill = (uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset;
     459        cbFill = (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
    459460        uOff = 0;
    460461        /* do loop to fill all image. */
     
    684685static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
    685686{
    686     Assert(pImage);
     687    Assert(VALID_PTR(pImage));
    687688
    688689    vdiFlushImage(pImage);
     
    879880    int rc;
    880881
    881     Assert(pImage);
    882     Assert(uOffset % 512 == 0);
    883     Assert(cbToRead % 512 == 0);
     882    Assert(VALID_PTR(pImage));
     883    Assert(!(uOffset % 512));
     884    Assert(!(cbToRead % 512));
    884885
    885886    if (   uOffset + cbToRead > getImageDiskSize(&pImage->Header)
    886         || cbToRead == 0)
     887        || !VALID_PTR(pvBuf)
     888        || !cbToRead)
    887889    {
    888890        rc = VERR_INVALID_PARAMETER;
     
    907909    else
    908910    {
    909         /* block present in image file */
    910         uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
     911        /* Block present in image file, read relevant data. */
     912        uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
    911913                           + (pImage->offStartData + pImage->offStartBlockData + offRead);
    912914        rc = RTFileReadAt(pImage->File, u64Offset, pvBuf, cbToRead, NULL);
     
    932934    int rc = VINF_SUCCESS;
    933935
    934     Assert(pImage);
    935     Assert(uOffset % 512 == 0);
    936     Assert(cbToWrite % 512 == 0);
     936    Assert(VALID_PTR(pImage));
     937    Assert(!(uOffset % 512));
     938    Assert(!(cbToWrite % 512));
    937939
    938940    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
     
    942944    }
    943945
    944     if (cbToWrite == 0)
     946    if (!VALID_PTR(pvBuf) || !cbToWrite)
    945947    {
    946948        rc = VERR_INVALID_PARAMETER;
     
    971973             * (which also means that it's a zero block. Don't need to write
    972974             * anything to this block  if the data consists of just zeroes. */
    973             Assert(cbToWrite % 4 == 0);
     975            Assert(!(cbToWrite % 4));
    974976            Assert(cbToWrite * 8 <= UINT32_MAX);
    975977            if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
     
    984986            /* Full block write to previously unallocated block.
    985987             * Allocate block and write data. */
     988            Assert(!offWrite);
    986989            unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
    987             rc = RTFileWriteAt(pImage->File,
    988                                  ((uint64_t)cBlocksAllocated << pImage->uShiftIndex2Offset)
    989                                + pImage->offStartData,
    990                                pvBuf, cbToWrite, NULL);
     990            uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
     991                               + (pImage->offStartData + pImage->offStartBlockData);
     992            rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
    991993            if (VBOX_FAILURE(rc))
    992994                goto out;
     
    10111013    }
    10121014    else
    1013         rc = RTFileWriteAt(pImage->File,
    1014                              ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
    1015                            + pImage->offStartData + pImage->offStartBlockData + offWrite,
    1016                            pvBuf, cbToWrite, NULL);
     1015    {
     1016        /* Block present in image file, write relevant data. */
     1017        uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
     1018                           + (pImage->offStartData + pImage->offStartBlockData + offWrite);
     1019        rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
     1020    }
    10171021
    10181022out:
     
    10401044    unsigned uVersion;
    10411045
    1042     Assert(pImage);
     1046    Assert(VALID_PTR(pImage));
    10431047
    10441048    if (pImage)
     
    10581062    int rc = VINF_SUCCESS;
    10591063
    1060     Assert(pImage);
    1061     Assert(penmImageType);
     1064    Assert(VALID_PTR(pImage));
     1065    Assert(VALID_PTR(penmImageType));
    10621066
    10631067    if (pImage)
     
    10791083    uint64_t cbSize;
    10801084
    1081     Assert(pImage);
     1085    Assert(VALID_PTR(pImage));
    10821086
    10831087    if (pImage)
     
    10971101    uint64_t cb = 0;
    10981102
    1099     Assert(pImage);
     1103    Assert(VALID_PTR(pImage));
    11001104
    11011105    if (pImage)
     
    11221126    int rc;
    11231127
    1124     Assert(pImage);
     1128    Assert(VALID_PTR(pImage));
    11251129
    11261130    if (pImage)
     
    11491153    int rc;
    11501154
    1151     Assert(pImage);
     1155    Assert(VALID_PTR(pImage));
    11521156
    11531157    if (pImage)
     
    11781182    int rc;
    11791183
    1180     Assert(pImage);
     1184    Assert(VALID_PTR(pImage));
    11811185
    11821186    if (pImage)
     
    12151219    int rc;
    12161220
    1217     Assert(pImage);
     1221    Assert(VALID_PTR(pImage));
    12181222
    12191223    if (pImage)
     
    12531257    unsigned uImageFlags;
    12541258
    1255     Assert(pImage);
     1259    Assert(VALID_PTR(pImage));
    12561260
    12571261    if (pImage)
     
    12711275    unsigned uOpenFlags;
    12721276
    1273     Assert(pImage);
     1277    Assert(VALID_PTR(pImage));
    12741278
    12751279    if (pImage)
     
    13161320    int rc = VINF_SUCCESS;
    13171321
    1318     Assert(pImage);
     1322    Assert(VALID_PTR(pImage));
    13191323
    13201324    if (pImage)
     
    13441348    int rc;
    13451349
    1346     Assert(pImage);
     1350    Assert(VALID_PTR(pImage));
    13471351
    13481352    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
     
    13921396    int rc;
    13931397
    1394     Assert(pImage);
     1398    Assert(VALID_PTR(pImage));
    13951399
    13961400    if (pImage)
     
    14131417    int rc = VINF_SUCCESS;
    14141418
    1415     Assert(pImage);
     1419    Assert(VALID_PTR(pImage));
    14161420
    14171421    if (pImage)
     
    14471451    int rc;
    14481452
    1449     Assert(pImage);
     1453    Assert(VALID_PTR(pImage));
    14501454
    14511455    if (pImage)
     
    14681472    int rc = VINF_SUCCESS;
    14691473
    1470     Assert(pImage);
     1474    Assert(VALID_PTR(pImage));
    14711475
    14721476    if (pImage)
     
    15021506    int rc;
    15031507
    1504     Assert(pImage);
     1508    Assert(VALID_PTR(pImage));
    15051509
    15061510    if (pImage)
     
    15231527    int rc = VINF_SUCCESS;
    15241528
    1525     Assert(pImage);
     1529    Assert(VALID_PTR(pImage));
    15261530
    15271531    if (pImage)
     
    15571561    int rc;
    15581562
    1559     Assert(pImage);
     1563    Assert(VALID_PTR(pImage));
    15601564
    15611565    if (pImage)
     
    15781582    int rc = VINF_SUCCESS;
    15791583
    1580     Assert(pImage);
     1584    Assert(VALID_PTR(pImage));
    15811585
    15821586    if (pImage)
     
    16361640    RTLogPrintf("Image:  fFlags=%08X offStartBlocks=%u offStartData=%u\n",
    16371641                pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
    1638     RTLogPrintf("Image:  uBlockMask=%08X uShiftIndex2Offset=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
     1642    RTLogPrintf("Image:  uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
    16391643                pImage->uBlockMask,
    1640                 pImage->uShiftIndex2Offset,
     1644                pImage->cbTotalBlockData,
    16411645                pImage->uShiftOffset2Index,
    16421646                pImage->offStartBlockData);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette