⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 volume.c

📁 WinCE5.0部分核心源码
💻 C
📖 第 1 页 / 共 5 页
字号:
                        clusEntry = UNKNOWN_CLUSTER;

                    if ((pstm->s_flags & STF_DIRTY) ||
                        pde->de_attr == pstm->s_attr &&
                        pde->de_size == pstm->s_size && clusEntry == pstm->s_clusFirst)
                    {
                        pstm->s_flags &= ~STF_UNMOUNTED;
                    }
                }
                UnholdBuffer(pbuf);
            }
        }

        // If the stream is now (re)mounted, make sure all its handles are (re)mounted also.

        if (!(pstm->s_flags & STF_UNMOUNTED)) {

            PFHANDLE pfh, pfhEnd;

            pfh = pstm->s_dlOpenHandles.pfhNext;
            pfhEnd = (PFHANDLE)&pstm->s_dlOpenHandles;

            while (pfh != pfhEnd) {
                pfh->fh_flags &= ~FHF_UNMOUNTED;
                pfh = pfh->fh_dlOpenHandles.pfhNext;
            }
        }

        CloseStream(pstm);
        EnterCriticalSection(&pvol->v_csStms);
        goto restart;
    }

    LeaveCriticalSection(&pvol->v_csStms);

    // Make sure the buffer pool is clean too, now that we've finished
    // resurrecting and committing all resurrectable streams.

    if (CommitVolumeBuffers(pvol) == ERROR_SUCCESS) {

        DEBUGMSG(ZONE_INIT && (pvol->v_flags & VOLF_DIRTY),(DBGTEXT("FATFS!RefreshVolume: dirty data successfully committed\r\n")));
        pvol->v_flags &= ~VOLF_DIRTY;

    }
    else
        DEBUGMSG(ZONE_INIT,(DBGTEXT("FATFS!RefreshVolume: unable to commit dirty data to volume 0x%08x\r\n"), pvol));
}




DWORD LockVolume(PVOLUME pvol, DWORD dwFlags)
{
    DWORD dwError = ERROR_SUCCESS;
//    pvol->v_flags |= dwFlags;
//   return dwError;
    
    EnterCriticalSection(&pvol->v_cs);

    // Since VOLF_LOCKED is the concatenation of all lock flags, LockVolume
    // is disallowed if any lock of any kind is already in effect.  This
    // simplification should be fine for our purposes.

    if (!(pvol->v_flags & VOLF_LOCKED)) {

        pvol->v_flags |= dwFlags;

        if (CheckStreamHandles(pvol, NULL)) {
            pvol->v_flags &= ~dwFlags;
            dwError = ERROR_ACCESS_DENIED;
        }
    }
    else
        dwError = ERROR_DRIVE_LOCKED;

    LeaveCriticalSection(&pvol->v_cs);

    // If successful, wait until all threads are done before returning.
    // Easiest way to do this is simulate a quick power-off/power-on sequence. 
    // Since the volume's LOCKED bit is already set, we don't need to worry about
    // any new calls doing anything until the volume is unlocked.

    if (!dwError && cFATThreads > 1) {
        FAT_Notify(pvol, FSNOTIFY_POWER_OFF);
        FAT_Notify(pvol, FSNOTIFY_DEVICES_ON);
    }
    return dwError;
}


/*  UnlockVolume - Unlock a previously locked VOLUME
 *
 *  ENTRY
 *      pvol -> VOLUME structure
 *
 *  EXIT
 *      None
 */

void UnlockVolume(PVOLUME pvol)
{
//    pvol->v_flags &= ~VOLF_LOCKED;
//    return;
    // We need to clear the volume's lock flag first, because if
    // MountDisk decides to try to format it, it will need to be able
    // to (re)lock the volume.

    ASSERT(pvol->v_flags & VOLF_LOCKED);
    pvol->v_flags &= ~VOLF_LOCKED;

    if (pvol->v_flags & VOLF_MODIFIED) {

        DWORD dwOldFlags;
        HANDLE hdsk = pvol->v_pdsk->d_hdsk;

        // Reinvigorate our data structures since it appears that the disk
        // could have been significantly modified.

        EnterCriticalSection(&csFATFS);

        // Since the unmount/remount can cause the REMOUNTED or RECYCLED bits
        // to be set, and since this could be called *within* an earlier mount,
        // we don't want to lose the original bits for that earlier mount.

        dwOldFlags = pvol->v_flags;

        // We set the RETAIN bit, so that we don't have to worry about
        // UnmountVolume freeing the current VOLUME structure.  It will be
        // reset by UnmountVolume as soon as CloseVolume has completed.
        // (I used to set the FROZEN bit instead, but that doesn't get reset
        // until much later -- when the volume is reopened by OpenVolume -- and
        // it prevents legitimate ReadVolume/WriteVolume calls from working -JTP)

        pvol->v_flags |= VOLF_RETAIN;
        UnmountVolume(pvol, FALSE);

        // When UnmountVolume called CloseVolume, CloseVolume froze the volume
        // and set v_bMediaDesc to MAX_VOLUMES.  We set it to zero now to insure
        // that we will re-use the volume without delay.
        pvol->v_bMediaDesc = 0;

        if (pvol->v_flags & VOLF_FROZEN) {
            pvol->v_pdsk->pVol = pvol;
            pvol->v_flags &= ~VOLF_FROZEN;
        }    

#ifndef DEBUG
        MountDisk(hdsk, NULL, pvol->v_flags);
#else
        VERIFYTRUE(MountDisk(hdsk, NULL, pvol->v_flags));
#endif        

        // IMPORTANT NOTE: If the VOLF_FROZEN bit is *not* clear, then it's
        // possible that MountDisk (which calls MountVolume) may have recycled
        // the *wrong* volume (or allocated a completely new one), which means
        // that we may have just leaked a VOLUME;  at the very least, it probably
        // means our current pvol pointer is stale.

// TODO: Yadhug
//        ASSERT(!(pvol->v_flags & VOLF_FROZEN));

        // Restore the REMOUNTED and RECYCLED bits to their former glory

        pvol->v_flags &= ~(VOLF_REMOUNTED | VOLF_RECYCLED);
        pvol->v_flags |= dwOldFlags & (VOLF_REMOUNTED | VOLF_RECYCLED);

        LeaveCriticalSection(&csFATFS);

#if 0 
        if (pvol->v_volID > INVALID_AFS) {
            DEREGISTERAFS(pvol);
            pvol->v_volID = REGISTERAFS(pvol, pvol->v_volID);
        }
#endif        
    }
}


/*  OpenVolume - Allocate a VOLUME structure and validate volume
 *
 *  ENTRY
 *      pdsk -> DSK structure
 *      ppi -> partition info, if any
 *      ppbgbs - pointer to address of PBR (partition boot record) for volume
 *      pstmParent - pointer to parent stream (only if MiniFAT volume)
 *
 *  EXIT
 *      pointer to new VOLUME structure, or NULL if we couldn't make one
 */

PVOLUME OpenVolume(PDSK pdsk, PBIGFATBOOTSEC *ppbgbs, PDSTREAM pstmParent)
{
    extern CONST WCHAR awcFlags[]; // From apis.c
    extern CONST WCHAR awcUpdateAccess[]; // From apis.c
    extern CONST WCHAR awcCodePage[]; // From apis.c
    PVOLUME pvol;

    ASSERT(pdsk->d_hdsk != INVALID_HANDLE_VALUE);

    EnterCriticalSection(&csFATFS);

    // Find a reusable VOLUME or allocate a new one.

    pvol = FindVolume(pdsk, *ppbgbs);
    if (!pvol)
        goto exit;

    EnterCriticalSection(&pvol->v_cs);

    pvol->v_flags &= ~(VOLF_FROZEN | VOLF_UNCERTAIN | VOLF_DIRTY_WARN);
    
    pvol->v_flFATFS = FATFS_REGISTRY_FLAGS;    
    FSDMGR_GetRegistryValue((HDSK)pdsk->d_hdsk, awcFlags, &pvol->v_flFATFS);  

    FSDMGR_GetRegistryFlag((HDSK)pdsk->d_hdsk, awcUpdateAccess, &pvol->v_flFATFS, FATFS_UPDATE_ACCESS);

    DEBUGMSGW(ZONE_INIT,(DBGTEXTW("FATFS!OpenVolume: access time updates %s\r\n"), pvol->v_flFATFS & FATFS_UPDATE_ACCESS? TEXTW("enabled") : TEXTW("disabled")));
    DEBUGMSGW(ZONE_INIT,(DBGTEXTW("FATFS!OpenVolume: event logging %s\r\n"), pvol->v_flFATFS & FATFS_DISABLE_LOG? TEXTW("disabled") : TEXTW("enabled")));
    DEBUGMSGW(ZONE_INIT,(DBGTEXTW("FATFS!OpenVolume: automatic scanning %s\r\n"), pvol->v_flFATFS & FATFS_DISABLE_AUTOSCAN? TEXTW("disabled") : TEXTW("enabled")));
    DEBUGMSGW(ZONE_INIT,(DBGTEXTW("FATFS!OpenVolume: write verify %s\r\n"), pvol->v_flFATFS & FATFS_VERIFY_WRITES? TEXTW("enabled") : (pvol->v_flFATFS & FATFS_DISABLE_AUTOSCAN? TEXTW("disabled") : TEXTW("enabled on first 3 writes"))));
    DEBUGMSGW(ZONE_INIT,(DBGTEXTW("FATFS!OpenVolume: extra FAT on format %s\r\n"), pvol->v_flFATFS & FATFS_ENABLE_BACKUP_FAT? TEXTW("enabled") : TEXTW("disabled")));
    DEBUGMSGW(ZONE_INIT,(DBGTEXTW("FATFS!OpenVolume: force write through %s\r\n"), pvol->v_flFATFS & FATFS_FORCE_WRITETHROUGH? TEXTW("enabled") : TEXTW("disabled")));

    if (!FSDMGR_GetRegistryValue((HDSK)pdsk->d_hdsk, awcCodePage, &pvol->v_nCodePage)) {
        pvol->v_nCodePage = CP_OEMCP;
    }
    DEBUGMSG( ZONE_INIT,(DBGTEXTW("FATFS!OpenVolume: Codepage = %ld\r\n"), pvol->v_nCodePage));

    // Get the number of path cache entries from the registry
    if (!FSDMGR_GetRegistryValue((HDSK)pdsk->d_hdsk, awcPathCacheEntries, &pvol->v_cMaxCaches)) {
        pvol->v_cMaxCaches = MAX_CACHE_PER_VOLUME;
    }
    DEBUGMSG( ZONE_INIT,(DBGTEXTW("FATFS!OpenVolume: Number of path cache entries = %ld\r\n"), pvol->v_cMaxCaches));

    // Initialize the rest of the VOLUME structure now.  This doesn't perform
    // any I/O, and will return an error only if there is a problem allocating
    // memory for either the FAT or root directory streams.

    if (!InitVolume(pvol, *ppbgbs)) {
        DEBUGMSG(ZONE_INIT || ZONE_ERRORS,(DBGTEXT("FATFS!OpenVolume: InitVolume failed, volume not opened!\r\n")));
        CloseVolume(pvol, NULL);
        pvol = NULL;
        goto exit;
    }

    // Now make sure the volume is valid.  This will set the INVALID bit if
    // it isn't, but we will still register/mount the volume so that it can be
    // made valid (ie, formatted) later.

    TestVolume(pvol, ppbgbs);

  exit:
    LeaveCriticalSection(&csFATFS);
    return pvol;
}


/*  CloseVolume - Free a VOLUME structure (if no open handles)
 *
 *  ENTRY
 *      pvol - pointer to VOLUME
 *      pwsVolName - pointer to volume name buffer (assumed to be MAX_PATH)
 *
 *  EXIT
 *      TRUE if freed, FALSE if not.  If not freed, then at a minimum,
 *      the disk handle is invalidated and the volume is marked FROZEN.
 *
 *  NOTES
 *      The volume's critical section should be held by the caller,
 *      and is the caller's responsibility to unhold if this returns FALSE
 *      (ie, volume not freed).
 */

BOOL CloseVolume(PVOLUME pvol, PWSTR pwsVolName)
{
    BOOL fSuccess = TRUE;
    PDSTREAM pstm, pstmEnd;
    PFHANDLE pfh, pfhEnd;

    DEBUGMSG(ZONE_APIS,(DBGTEXT("FATFS!CloseVolume(0x%x)\r\n"), pvol));

    ASSERT(pvol);

    ASSERT(OWNCRITICALSECTION(&csFATFS));
    ASSERT(OWNCRITICALSECTION(&pvol->v_cs));

#ifdef PATH_CACHING
    // We destroy the path cache first, releasing any streams
    // that it may have been holding onto, so that our assertion
    // checks below don't give us false fits.

    PathCacheDestroyAll(pvol);
#endif

    // Walk the open stream list, and for each open stream, walk
    // the open handle list, and for each open handle, mark it as
    // unmounted.  Streams with no open handles are immediately
    // closed;  the only streams that should fall into that category
    // are the special streams for the FAT and root directory.

    EnterCriticalSection(&pvol->v_csStms);

    pstm = pvol->v_dlOpenStreams.pstmNext;
    pstmEnd = (PDSTREAM)&pvol->v_dlOpenStreams;

    while (pstm != pstmEnd) {
        pstm->s_flags &= ~STF_VISITED;
        pstm = pstm->s_dlOpenStreams.pstmNext;
    }

  restart:
    pstm = pvol->v_dlOpenStreams.pstmNext;
    while (pstm != pstmEnd) {

        if (pstm->s_flags & STF_VISITED) {
            pstm = pstm->s_dlOpenStreams.pstmNext;
            continue;
        }

        pstm->s_flags |= STF_VISITED;

        // Add a ref to insure that the stream can't go away once we
        // let go of the volume's critical section.

        pstm->s_refs++;

        LeaveCriticalSection(&pvol->v_csStms);
        EnterCriticalSection(&pstm->s_cs);

        // Try to commit the stream before we unmount it, because we may
        // never be able to remount this stream again, and CommitStream
        // will refuse to commit a stream that's unmounted (and rightly so).

        CommitStream(pstm, TRUE);

        pfh = pstm->s_dlOpenHandles.pfhNext;
        pfhEnd = (PFHANDLE)&pstm->s_dlOpenHandles;

        if (pfh == pfhEnd) {

            if (pstm->s_refs == 2) {
                if (pstm == pvol->v_pstmFAT) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -