Chinaunix首页 | 论坛 | 博客
  • 博客访问: 177663
  • 博文数量: 22
  • 博客积分: 1586
  • 博客等级: 上尉
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-06 10:32
文章分类

全部博文(22)

文章存档

2015年(2)

2014年(1)

2013年(2)

2012年(5)

2011年(1)

2010年(11)

我的朋友

分类:

2010-01-08 17:08:06

 
参考1:

void PurgeFileSystemBuffers(PFILE_OBJECT FileObject)
{
KeEnterCriticalRegion();
if(ExAcquireResourceExclusiveLite( ((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->Resource, FALSE))
{
    if(FileObject->SectionObjectPointer)
    {
        IoSetTopLevelIrp( (PIRP)FSRTL_FSP_TOP_LEVEL_IRP );
        CcFlushCache( FileObject->SectionObjectPointer, NULL, 0, NULL );
        if(FileObject->SectionObjectPointer->ImageSectionObject)
        {
            MmFlushImageSection(
                FileObject->SectionObjectPointer,
                MmFlushForWrite//MmFlushForDelete//

                ) ;
        }
        if(FileObject->SectionObjectPointer->DataSectionObject)
        {
            CcPurgeCacheSection( FileObject->SectionObjectPointer,
                NULL,
                0,
                TRUE ); //为TRUE,会强迫所有的FileObject重新初始化Cache

        }
        if(FileObject->PrivateCacheMap)
        {
            CcUninitializeCacheMap(FileObject, NULL, NULL);;
        }
        if(FileObject->SectionObjectPointer->DataSectionObject)
        {
            //Interval.QuadPart = DELAY_ONE_MILLISECOND * 500;//500ms

            //KeDelayExecutionThread(KernelMode, FALSE, &Interval);

            MmForceSectionClosed( FileObject->SectionObjectPointer,
                TRUE//改为TRUE,彻底刷新缓存!!!

                );
        }
        IoSetTopLevelIrp(NULL);
    }
    ExReleaseResourceLite( ((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->Resource );
}
KeLeaveCriticalRegion();
}

 

参考2:

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();

   }


阅读(4097) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~