阅读:3437回复:5
一段清除缓存的代码,大家进来看看
大家帮我看看这段清除缓存的代码为什么在FAT32下正常在NTFS分区下有时候会蓝屏?
还有就是如何判断文件缓冲是否清除了呢?我的WDK里面怎么没有CCFileIsCache这个宏的定义呢? // 清理缓冲 void cfFileCacheClear(PFILE_OBJECT pFileObject) { PFSRTL_COMMON_FCB_HEADER pFcb; LARGE_INTEGER liInterval; BOOLEAN bNeedReleaseResource = FALSE; BOOLEAN bNeedReleasePagingIoResource = FALSE; KIRQL irql; pFcb = (PFSRTL_COMMON_FCB_HEADER)pFileObject->FsContext; if(pFcb == NULL) return; irql = KeGetCurrentIrql(); if (irql >= DISPATCH_LEVEL) { return; } liInterval.QuadPart = -1 * (LONGLONG)50; while (TRUE) { BOOLEAN bBreak = TRUE; BOOLEAN bLockedResource = FALSE; BOOLEAN bLockedPagingIoResource = FALSE; bNeedReleaseResource = FALSE; bNeedReleasePagingIoResource = FALSE; // 到fcb中去拿锁。 if (pFcb->PagingIoResource) bLockedPagingIoResource = ExIsResourceAcquiredExclusiveLite(pFcb->PagingIoResource); // 总之一定要拿到这个锁。 if (pFcb->Resource) { bLockedResource = TRUE; if (ExIsResourceAcquiredExclusiveLite(pFcb->Resource) == FALSE) { bNeedReleaseResource = TRUE; if (bLockedPagingIoResource) { if (ExAcquireResourceExclusiveLite(pFcb->Resource, FALSE) == FALSE) { bBreak = FALSE; bNeedReleaseResource = FALSE; bLockedResource = FALSE; } } else ExAcquireResourceExclusiveLite(pFcb->Resource, TRUE); } } if (bLockedPagingIoResource == FALSE) { if (pFcb->PagingIoResource) { bLockedPagingIoResource = TRUE; bNeedReleasePagingIoResource = TRUE; if (bLockedResource) { if (ExAcquireResourceExclusiveLite(pFcb->PagingIoResource, FALSE) == FALSE) { bBreak = FALSE; bLockedPagingIoResource = FALSE; bNeedReleasePagingIoResource = FALSE; } } else { ExAcquireResourceExclusiveLite(pFcb->PagingIoResource, TRUE); } } } if (bBreak) { break; } if (bNeedReleasePagingIoResource) { ExReleaseResourceLite(pFcb->PagingIoResource); } if (bNeedReleaseResource) { ExReleaseResourceLite(pFcb->Resource); } if (irql == PASSIVE_LEVEL) { KeDelayExecutionThread(KernelMode, FALSE, &liInterval); } else { KEVENT waitEvent; KeInitializeEvent(&waitEvent, NotificationEvent, FALSE); KeWaitForSingleObject(&waitEvent, Executive, KernelMode, FALSE, &liInterval); } } if (pFileObject->SectionObjectPointer) { IO_STATUS_BLOCK ioStatus; CcFlushCache(pFileObject->SectionObjectPointer, NULL, 0, &ioStatus); //added by ljy if(NT_SUCCESS(ioStatus.Status)) { KdPrint(("CcFlushCache OK\n")); } else { KdPrint(("CcFlushCache Failed\n")); } if (pFileObject->SectionObjectPointer->ImageSectionObject) { //MmFlushImageSection(pFileObject->SectionObjectPointer,MmFlushForWrite); // MmFlushForDelete //added by ljy if(MmFlushImageSection(pFileObject->SectionObjectPointer,MmFlushForWrite)==TRUE) { KdPrint(("MmFlushImageSection OK\n")); } else { KdPrint(("MmFlushImageSection Failed\n")); } } //CcPurgeCacheSection(pFileObject->SectionObjectPointer, NULL, 0, FALSE); //added by ljy if(CcPurgeCacheSection(pFileObject->SectionObjectPointer, NULL, 0, TRUE)==TRUE) { KdPrint(("CcPurgeCacheSection OK\n")); } else { KdPrint(("CcPurgeCacheSection Failed\n")); } //问题就出在这里,如果我不加CcUninitializeCacheMap的话就不会蓝屏 //但不加的话在NTFS下有时候清除不了缓存 added by ljy { KEVENT waitEvent1; LARGE_INTEGER liInterval0; liInterval0.QuadPart=0; KeInitializeEvent(&waitEvent1, NotificationEvent, FALSE); CcUninitializeCacheMap(pFileObject,&liInterval0,&waitEvent1); KeWaitForSingleObject(&waitEvent1, Executive, KernelMode, FALSE, &liInterval0); } //CcSetFileSizes(pFileObject,0); } if (bNeedReleasePagingIoResource) { ExReleaseResourceLite(pFcb->PagingIoResource); } if (bNeedReleaseResource) { ExReleaseResourceLite(pFcb->Resource); } } |
|
沙发#
发布于:2009-10-13 16:38
自己顶一下。
|
|
板凳#
发布于:2009-11-05 15:23
回 1楼(skymelai) 的帖子
你好 你的这个问题解决了吗? |
|
地板#
发布于:2009-11-07 10:02
.....似乎是我看的那本书上的代码 看223页代码上面那段
说的是参考fastfat代码中缓存清除部分写的 那么不能在ntfs的文件卷上使用也是正常的 非要不蓝屏的话 只能在过滤的时候判断一下当前文件卷的文件类型吧 |
|
地下室#
发布于:2010-03-23 11:44
顶一下
|
|
5楼#
发布于:2010-03-24 10:52
这个是参考fastfat源代码的当然只能支持fat32不能支持ntfs了啊
|
|