flush memory mapped file

Hi,
I’ve been trying to find how to flush a memory mapped file created in my file system mini-filter driver for a few hours now and it does not seem obvious. I create it with ZwCreateSection and ZwMapViewOfSection using a file opened with ZwCreateFile. At certain critical points I need to be able to manually force all dirty pages to be flushed to disk synchronously.
I see in use mode one can use FlushViewOfFile, but there seems to be no ZwFlushViewOfFile counter part. I also read about MmFlushImageSection and CcFlushCache but those require SECTION_OBJECT_POITNERS and seem to be for OS drivers.
Is it possible to manually force a flush of all dirty pages as I want to do in my filter driver?

Russel

There is ZwFlushVirtualMemory, but I haven’t ever used it… Though it seems
to do what you need…

Also, since you have a minifilter, you could use FltFlushBuffers.

Thanks,
Alex.

The kernel equivalent of FlushViewOfFile is ZwFlushVirtualMemory.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

xxxxx@swbell.net” wrote in
message news:xxxxx@ntfsd:

> Hi,
> I’ve been trying to find how to flush a memory mapped file created in my file system mini-filter driver for a few hours now and it does not seem obvious. I create it with ZwCreateSection and ZwMapViewOfSection using a file opened with ZwCreateFile. At certain critical points I need to be able to manually force all dirty pages to be flushed to disk synchronously.
> I see in use mode one can use FlushViewOfFile, but there seems to be no ZwFlushViewOfFile counter part. I also read about MmFlushImageSection and CcFlushCache but those require SECTION_OBJECT_POITNERS and seem to be for OS drivers.
> Is it possible to manually force a flush of all dirty pages as I want to do in my filter driver?
>
> Russel

ZwFlushVirtualMemory is not synchronous. User mode docs for FlushViewOfFile say that you have to call FlushFileBuffers (which translates to ZwFlushBuffersFile/FltFlushBuffers in the kernel) after flushing the view in order to get synchronous behavior.

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: Monday, December 06, 2010 10:25 AM
To: Windows File Systems Devs Interest List
Subject: Re:[ntfsd] flush memory mapped file

The kernel equivalent of FlushViewOfFile is ZwFlushVirtualMemory.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

xxxxx@swbell.net” wrote in message news:xxxxx@ntfsd:

> Hi,
> I’ve been trying to find how to flush a memory mapped file created in my file system mini-filter driver for a few hours now and it does not seem obvious. I create it with ZwCreateSection and ZwMapViewOfSection using a file opened with ZwCreateFile. At certain critical points I need to be able to manually force all dirty pages to be flushed to disk synchronously.
> I see in use mode one can use FlushViewOfFile, but there seems to be no ZwFlushViewOfFile counter part. I also read about MmFlushImageSection and CcFlushCache but those require SECTION_OBJECT_POITNERS and seem to be for OS drivers.
> Is it possible to manually force a flush of all dirty pages as I want to do in my filter driver?
>
> Russel


NTFSD is sponsored by OSR

For our schedule of debugging and file system seminars (including our new fs mini-filter seminar) visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

You guys are the best! Thanks so much. I’ll try the FlushFileBuffers as well for synchronous behavior. Will post back once I get it working in case anyone else needs this. I need the synchronous part since I’m filtering a SQL Server database and to maintain crash-scenario integrity I need our memory mapped file flushed to disk before completing a SQL Server file header page write (which has the log LSNs in it).
Thanks again.

Yep that worked. Thanks!
if (pFilterCtx->hBitMapFile) {
NTSTATUS rcFlush = ZwFlushVirtualMemory(ZwCurrentProcess(), &pFlushBaseAddress, &iFlushLen, &iostat);
DbgTrace((“ZwFlushVirtualMemory - rcFlush: %d iostat: %d\n”,rcFlush, iostat.Status));

if (pFilterCtx->pBitMapFileObject) {
NTSTATUS rcFlushBuffers = FltFlushBuffers(pCfltRelatedObjects->Instance, pFilterCtx->pBitMapFileObject);
DbgTrace((“FltFlushBuffers - rc: %d\n”,rcFlushBuffers));
}
}