Flushing cache in kernel!

Hi,
I would like to build an IRP to flush all the cache in C drive to files.
Currently I am able to use Win 32 API to flush the cache. However, I would
like to flush it in kernel mode, as soon as IRP_MJ_CLEANUP is completed.
This is to force IO Manager to issue IRP_MJ_CLOSE for that particular file.
I tried to build an IRP but I still don’t get the IRP_MJ_CLOSE request after
the IRP is sent down to the lower file system.

Please suggest how to solve this problem. Thanks!

Regards,
Sin Lam
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////
//This routine is called to flush the cache in C drive as soon as
IRP_MJ_CLEANUP
//for a specific file is completed.
NTSTATUS ForceFlushBuffer() {
PIRP Irp;
NTSTATUS Status = STATUS_SUCCESS;
KEVENT Event;
IO_STATUS_BLOCK Iosb;
HANDLE ntFileHandle;
OBJECT_ATTRIBUTES objectAttributes;
PFILE_OBJECT pFO;
PDEVICE_OBJECT pDO;
UNICODE_STRING fileNameUnicodeString;
WCHAR filename = L"\DosDevices\C:";
PIO_STACK_LOCATION ioStackLocation;

RtlInitUnicodeString(&fileNameUnicodeString, filename);
InitializeObjectAttributes( &objectAttributes, &fileNameUnicodeString,
OBJ_CASE_INSENSITIVE, NULL, NULL );
Status = ZwCreateFile( &ntFileHandle, SYNCHRONIZE,
&objectAttributes, &Iosb, NULL, 0,
FILE_SHARE_READ,
FILE_OPEN,

FILE_SYNCHRONOUS_IO_NONALERT|FILE_NO_INTERMEDIATE_BUFFERING,
NULL, 0 );
if ( !NT_SUCCESS( Status ) ) {
DbgPrint((“### ForceFlushBuffer: ZwCreateFile failed with status =
%x\n”, Status));
return STATUS_UNSUCCESSFUL;
}

Status = ObReferenceObjectByHandle( ntFileHandle, FILE_READ_DATA,
NULL, KernelMode, &pFO, NULL );
if ( !NT_SUCCESS( Status )) {
DbgPrint((“### ForceFlushBuffer: ObReferenceObjectByHandle failed
with status = %x\n”, Status));
ZwClose( ntFileHandle );
return STATUS_UNSUCCESSFUL;
}

pDO = IoGetRelatedDeviceObject( pFO );
if ( !pDO ) {
DbgPrint((“### ForceFlushBuffer: IoGetRelatedDeviceObject failed
with status = %x\n”, Status));
ObDereferenceObject( pFO );
ZwClose( ntFileHandle );
return STATUS_UNSUCCESSFUL;
}

KeInitializeEvent(&Event, NotificationEvent, FALSE);
Irp = IoBuildSynchronousFsdRequest( IRP_MJ_FLUSH_BUFFERS, //Major
function
pDO, //lower file system
NULL, //out buffer
0, //Length
0, //StartingOffset
&Event, // Event object
&Iosb); // status block to be set
if (!Irp) {
DbgPrint((“### IoBuildSynchronousFsdRequest failed!\n”));
ObDereferenceObject(pFO);
ZwClose( ntFileHandle );
return STATUS_UNSUCCESSFUL;
}
ioStackLocation = IoGetNextIrpStackLocation(Irp);
ioStackLocation->DeviceObject = pDO;
ioStackLocation->FileObject = pFO;

Status = IoCallDriver(pDO, Irp);

if (Status == STATUS_PENDING) {
KeWaitForSingleObject(&Event, Executive, KernelMode, TRUE, 0);
Status = Iosb.Status;
}

ObDereferenceObject(pFO);
ZwClose( ntFileHandle );
if (Iosb.Status != STATUS_SUCCESS) {
DbgPrint((“### ForceFlushBuffer failed!\n”));
return STATUS_ACCESS_DENIED;
}

return STATUS_SUCCESS;
}

Flush doesn’t clear the data out of the cache - all it does is cause dirty
pages to be written to disk. You need to use CcPurgeCacheSection() to remove
the pages from the file cache and MmFlushImageSection() to remove any
executable pages cached (to cause the final close).

From the kernel, you can use CcFlushCache() to write the dirty pages to disk
instead of building the Irp.

As a matter of interest, why would you want to defeat the point of the file
cache by doing this? It loses a lot of its use if you throw it away when the
file is closed…

/simgr

-----Original Message-----
From: Tan Sin Lam [mailto:xxxxx@krdl.org.sg]
Sent: Wednesday, September 06, 2000 2:00 AM
To: File Systems Developers
Subject: [ntfsd] Flushing cache in kernel!

Hi,
I would like to build an IRP to flush all the cache in C drive to files.
Currently I am able to use Win 32 API to flush the cache. However, I would
like to flush it in kernel mode, as soon as IRP_MJ_CLEANUP is completed.
This is to force IO Manager to issue IRP_MJ_CLOSE for that particular file.
I tried to build an IRP but I still don’t get the IRP_MJ_CLOSE request after
the IRP is sent down to the lower file system.

Please suggest how to solve this problem. Thanks!

Regards,
Sin Lam
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////
//This routine is called to flush the cache in C drive as soon as
IRP_MJ_CLEANUP
//for a specific file is completed.
NTSTATUS ForceFlushBuffer() {
PIRP Irp;
NTSTATUS Status = STATUS_SUCCESS;
KEVENT Event;
IO_STATUS_BLOCK Iosb;
HANDLE ntFileHandle;
OBJECT_ATTRIBUTES objectAttributes;
PFILE_OBJECT pFO;
PDEVICE_OBJECT pDO;
UNICODE_STRING fileNameUnicodeString;
WCHAR filename = L"\DosDevices\C:";
PIO_STACK_LOCATION ioStackLocation;

RtlInitUnicodeString(&fileNameUnicodeString, filename);
InitializeObjectAttributes( &objectAttributes, &fileNameUnicodeString,
OBJ_CASE_INSENSITIVE, NULL, NULL );
Status = ZwCreateFile( &ntFileHandle, SYNCHRONIZE,
&objectAttributes, &Iosb, NULL, 0,
FILE_SHARE_READ,
FILE_OPEN,

FILE_SYNCHRONOUS_IO_NONALERT|FILE_NO_INTERMEDIATE_BUFFERING,
NULL, 0 );
if ( !NT_SUCCESS( Status ) ) {
DbgPrint((“### ForceFlushBuffer: ZwCreateFile failed with status =
%x\n”, Status));
return STATUS_UNSUCCESSFUL;
}

Status = ObReferenceObjectByHandle( ntFileHandle, FILE_READ_DATA,
NULL, KernelMode, &pFO, NULL );
if ( !NT_SUCCESS( Status )) {
DbgPrint((“### ForceFlushBuffer: ObReferenceObjectByHandle failed
with status = %x\n”, Status));
ZwClose( ntFileHandle );
return STATUS_UNSUCCESSFUL;
}

pDO = IoGetRelatedDeviceObject( pFO );
if ( !pDO ) {
DbgPrint((“### ForceFlushBuffer: IoGetRelatedDeviceObject failed
with status = %x\n”, Status));
ObDereferenceObject( pFO );
ZwClose( ntFileHandle );
return STATUS_UNSUCCESSFUL;
}

KeInitializeEvent(&Event, NotificationEvent, FALSE);
Irp = IoBuildSynchronousFsdRequest( IRP_MJ_FLUSH_BUFFERS, //Major
function
pDO, //lower file system
NULL, //out buffer
0, //Length
0, //StartingOffset
&Event, // Event object
&Iosb); // status block to be set
if (!Irp) {
DbgPrint((“### IoBuildSynchronousFsdRequest failed!\n”));
ObDereferenceObject(pFO);
ZwClose( ntFileHandle );
return STATUS_UNSUCCESSFUL;
}
ioStackLocation = IoGetNextIrpStackLocation(Irp);
ioStackLocation->DeviceObject = pDO;
ioStackLocation->FileObject = pFO;

Status = IoCallDriver(pDO, Irp);

if (Status == STATUS_PENDING) {
KeWaitForSingleObject(&Event, Executive, KernelMode, TRUE, 0);
Status = Iosb.Status;
}

ObDereferenceObject(pFO);
ZwClose( ntFileHandle );
if (Iosb.Status != STATUS_SUCCESS) {
DbgPrint((“### ForceFlushBuffer failed!\n”));
return STATUS_ACCESS_DENIED;
}

return STATUS_SUCCESS;
}


You are currently subscribed to ntfsd as: xxxxx@stratus.com
To unsubscribe send a blank email to $subst(‘Email.Unsub’)

> Hi,

I would like to build an IRP to flush all the cache in C drive to files.
Currently I am able to use Win 32 API to flush the cache. However, I would
like to flush it in kernel mode, as soon as IRP_MJ_CLEANUP is completed.
This is to force IO Manager to issue IRP_MJ_CLOSE for that particular
file.
I tried to build an IRP but I still don’t get the IRP_MJ_CLOSE request
after
the IRP is sent down to the lower file system.

Please suggest how to solve this problem. Thanks!

Send IRP_MJ_FLUSH_CACHE to the directly opened volume (open ??\C:).

Max

Hi Max!
I’m curious where IRP_MJ_FLUSH_CACHE is defined?

Thanks

From: “Maxim S. Shatskih”
>Reply-To: “File Systems Developers”
>To: “File Systems Developers”
>Subject: [ntfsd] Re: Flushing cache in kernel!
>Date: Sun, 17 Sep 2000 17:30:01 +0400
>
> > Hi,
> > I would like to build an IRP to flush all the cache in C drive to
>files.
> > Currently I am able to use Win 32 API to flush the cache. However, I
>would
> > like to flush it in kernel mode, as soon as IRP_MJ_CLEANUP is completed.
> > This is to force IO Manager to issue IRP_MJ_CLOSE for that particular
>file.
> > I tried to build an IRP but I still don’t get the IRP_MJ_CLOSE request
>after
> > the IRP is sent down to the lower file system.
> >
> > Please suggest how to solve this problem. Thanks!
>
>Send IRP_MJ_FLUSH_CACHE to the directly opened volume (open ??\C:).
>
> Max
>
>
>—
>You are currently subscribed to ntfsd as: xxxxx@hotmail.com
>To unsubscribe send a blank email to $subst(‘Email.Unsub’)
>

_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

Share information about yourself, create your own public profile at
http://profiles.msn.com.

This should be IRP_MJ_FLUSH_BUFFERS major function.
It is defined in both the NTDDK.H and NTIFS.H files.
Paul

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Jack Brown
Sent: Thursday, September 21, 2000 12:23 PM
To: File Systems Developers
Subject: [ntfsd] Re: Flushing cache in kernel!

Hi Max!
I’m curious where IRP_MJ_FLUSH_CACHE is defined?

Thanks

From: “Maxim S. Shatskih”
>Reply-To: “File Systems Developers”
>To: “File Systems Developers”
>Subject: [ntfsd] Re: Flushing cache in kernel!
>Date: Sun, 17 Sep 2000 17:30:01 +0400
>
> > Hi,
> > I would like to build an IRP to flush all the cache in C drive to
>files.
> > Currently I am able to use Win 32 API to flush the cache. However, I

>would
> > like to flush it in kernel mode, as soon as IRP_MJ_CLEANUP is
completed.
> > This is to force IO Manager to issue IRP_MJ_CLOSE for that
particular
>file.
> > I tried to build an IRP but I still don’t get the IRP_MJ_CLOSE
request
>after
> > the IRP is sent down to the lower file system.
> >
> > Please suggest how to solve this problem. Thanks!
>
>Send IRP_MJ_FLUSH_CACHE to the directly opened volume (open ??\C:).
>
> Max
>
>
>—
>You are currently subscribed to ntfsd as: xxxxx@hotmail.com
>To unsubscribe send a blank email to $subst(‘Email.Unsub’)
>

________________________________________________________________________
_
Get Your Private, Free E-mail from MSN Hotmail at
http://www.hotmail.com.

Share information about yourself, create your own public profile at
http://profiles.msn.com.


You are currently subscribed to ntfsd as: xxxxx@compelson.com
To unsubscribe send a blank email to $subst(‘Email.Unsub’)

Sorry, I mean IRP_MJ_FLUSH_BUFFERS.
FASTFAT flushes all caches if this IRP is sent to the file object which
represents the volume itself.

Max

----- Original Message -----
From: “Jack Brown”
To: “File Systems Developers”
Sent: Thursday, September 21, 2000 3:22 PM
Subject: [ntfsd] Re: Flushing cache in kernel!

> Hi Max!
> I’m curious where IRP_MJ_FLUSH_CACHE is defined?
>
> Thanks
>
>
> >From: “Maxim S. Shatskih”
> >Reply-To: “File Systems Developers”
> >To: “File Systems Developers”
> >Subject: [ntfsd] Re: Flushing cache in kernel!
> >Date: Sun, 17 Sep 2000 17:30:01 +0400
> >
> > > Hi,
> > > I would like to build an IRP to flush all the cache in C drive to
> >files.
> > > Currently I am able to use Win 32 API to flush the cache. However, I
> >would
> > > like to flush it in kernel mode, as soon as IRP_MJ_CLEANUP is
completed.
> > > This is to force IO Manager to issue IRP_MJ_CLOSE for that particular
> >file.
> > > I tried to build an IRP but I still don’t get the IRP_MJ_CLOSE request
> >after
> > > the IRP is sent down to the lower file system.
> > >
> > > Please suggest how to solve this problem. Thanks!
> >
> >Send IRP_MJ_FLUSH_CACHE to the directly opened volume (open ??\C:).
> >
> > Max
> >
> >
> >—
> >You are currently subscribed to ntfsd as: xxxxx@hotmail.com
> >To unsubscribe send a blank email to $subst(‘Email.Unsub’)
> >
>
> _________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
>
> Share information about yourself, create your own public profile at
> http://profiles.msn.com.
>
>
> —
> You are currently subscribed to ntfsd as: xxxxx@storagecraft.com
> To unsubscribe send a blank email to $subst(‘Email.Unsub’)
>