void ManageFileCache(PFILE_OBJECT pFileObject) { PFSRTL_COMMON_FCB_HEADER pFcb; LARGE_INTEGER liInterval; //BOOLEAN bNeedReleaseResource = FALSE;
//BOOLEAN bNeedReleasePagingIoResource = FALSE;
KIRQL irql; BOOLEAN bLockedResource=FALSE; BOOLEAN bLockedPagingIoResource=FALSE; BOOLEAN bReleaseResource=FALSE; BOOLEAN bReleasePagingIoResource=FALSE;
pFcb = (PFSRTL_COMMON_FCB_HEADER)pFileObject->FsContext; if(pFcb == NULL) { return; } irql = KeGetCurrentIrql(); if (irql >= DISPATCH_LEVEL) { return; } liInterval.QuadPart = -1 * (LONGLONG)50;
//KeEnterCriticalRegion();
bLockedPagingIoResource = ExIsResourceAcquiredExclusiveLite(pFcb->PagingIoResource); bLockedResource=ExIsResourceAcquiredExclusiveLite(pFcb->Resource); if(!bLockedPagingIoResource) bReleasePagingIoResource=TRUE; if(!bLockedResource) bReleaseResource=TRUE;
while(!bLockedPagingIoResource || !bLockedResource) { if(!bLockedResource) { if(ExAcquireResourceExclusiveLite(pFcb->Resource, FALSE)==TRUE) bLockedResource=TRUE; else goto clear; } if(!bLockedPagingIoResource) { if(ExAcquireResourceExclusiveLite(pFcb->PagingIoResource, FALSE)==TRUE) bLockedPagingIoResource=TRUE; else goto clear; } if(bLockedPagingIoResource && bLockedResource) break; clear: if (bLockedPagingIoResource) { ExReleaseResourceLite(pFcb->PagingIoResource); bLockedPagingIoResource = FALSE; } if (bLockedResource) { ExReleaseResourceLite(pFcb->Resource); bLockedResource=FALSE; } KeLeaveCriticalRegion(); if (irql == PASSIVE_LEVEL) { KeDelayExecutionThread(KernelMode, FALSE, &liInterval); } else { KEVENT waitEvent; KeInitializeEvent(&waitEvent, NotificationEvent, FALSE); KeWaitForSingleObject(&waitEvent, Executive, KernelMode, FALSE, &liInterval); } KeEnterCriticalRegion(); }
//加锁完毕
if (pFileObject->SectionObjectPointer != NULL) { IO_STATUS_BLOCK ioStatus; CcFlushCache(pFileObject->SectionObjectPointer, NULL, 0, &ioStatus); if (pFileObject->SectionObjectPointer->ImageSectionObject != NULL) MmFlushImageSection(pFileObject->SectionObjectPointer, MmFlushForWrite);//MmFlushForDelete
if (pFileObject->SectionObjectPointer->DataSectionObject != NULL) CcPurgeCacheSection(pFileObject->SectionObjectPointer , NULL, 0, FALSE); } if (bReleasePagingIoResource) { ExReleaseResourceLite(pFcb->PagingIoResource); bLockedPagingIoResource = FALSE; } if (bReleaseResource) { ExReleaseResourceLite(pFcb->Resource); bLockedResource=FALSE; }
KeLeaveCriticalRegion();
}
|