IRP_MJ_FLUSH_BUFFERS Problem

Hi Guys,

Can someone just check the following piece of code over quickly, Im going mad here! :) It must be something really stupid. Basically I have a driver that excepts an IOCTL to send the IRP_MJ_FLUSH_BUFFERS command to a specified drive, but this function just refuses to do it. It returns success but if I monitor the actual activity of the drive (using filespy from ifs kit) I dont see anything.

Here is my function

VOID FlushVolume( WCHAR Volume )
{
NTSTATUS status;
WCHAR wDrive = L"\??\@:";
UNICODE_STRING us;
PFILE_OBJECT pFileObject;
PDEVICE_OBJECT pDeviceObject;
IO_STATUS_BLOCK iosb;

// Format drive into string
wDrive[4] = Volume;
RtlInitUnicodeString( &us, wDrive );

// Get Fileobject
// I have tried all sorts of access_mask in this call, all with no
success!
status = IoGetDeviceObjectPointer( &us, FILE_GENERIC_WRITE,
&pFileObject, &pDeviceObject );

if( !NT_SUCCESS( status ) )
{
DbgPrint( “Failed open disk : %x\n”, status );
}
else
{
KEVENT event;
PIRP irp;

KeInitializeEvent( &event, NotificationEvent, FALSE );

irp = IoBuildSynchronousFsdRequest( IRP_MJ_FLUSH_BUFFERS,

pDeviceObject,

NULL,

0,

NULL,

&event,

&iosb );

if( !irp )
{
DbgPrint( “Failed to allocate irp for flush : %x\n”,
status );
}
else
{
// Put the fileobject in the iostack location
PIO_STACK_LOCATION iostack =
IoGetNextIrpStackLocation( irp );

iostack->FileObject = pFileObject;

DbgPrint( “About to flush drive…\n”) ;

status = IoCallDriver( pDeviceObject, irp );

if( status == STATUS_PENDING )
{
// DbgPrint( “IoCallDriver returned pending so
wait…\n” ) ;

KeWaitForSingleObject( &event, Executive,
KernelMode, FALSE, NULL );

status = iosb.Status;
}

DbgPrint( “Status from flush buffers : %x\n”,
iosb.Status ) ;
}

ObDereferenceObject( pFileObject );
}
}

Funny thing is that if I use my little console app to do much the same thing
(using flushfilebuffers with a handle to the drive) then it works fine.

Am I missing a trick here

Ben Curley
DESlock+ Lead Programmer
Data Encryption Systems Ltd.

Tel: +44 (0)1823 352357 (Main)
Tel: +44 (0)1823 358320 (Direct Dial)

Web: http://www.deslock.com

Try IoAllocateIrp instead, this worked for me.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From:
To: “Windows File Systems Devs Interest List”
Sent: Wednesday, December 24, 2003 11:52 AM
Subject: [ntfsd] IRP_MJ_FLUSH_BUFFERS Problem

>
> Hi Guys,
>
> Can someone just check the following piece of code over quickly, Im going<br>&gt; mad here! :) It must be something really stupid. Basically I have a driver<br>&gt; that excepts an IOCTL to send the IRP_MJ_FLUSH_BUFFERS command to a<br>&gt; specified drive, but this function just refuses to do it. It returns<br>&gt; success but if I monitor the actual activity of the drive (using filespy<br>&gt; from ifs kit) I dont see anything.
>
> Here is my function
>
> VOID FlushVolume( WCHAR Volume )
> {
> NTSTATUS status;
> WCHAR wDrive = L"\??\@:";
> UNICODE_STRING us;
> PFILE_OBJECT pFileObject;
> PDEVICE_OBJECT pDeviceObject;
> IO_STATUS_BLOCK iosb;
>
> // Format drive into string
> wDrive[4] = Volume;
> RtlInitUnicodeString( &us, wDrive );
>
> // Get Fileobject
> // I have tried all sorts of access_mask in this call, all with no
> success!
> status = IoGetDeviceObjectPointer( &us, FILE_GENERIC_WRITE,
> &pFileObject, &pDeviceObject );
>
> if( !NT_SUCCESS( status ) )
> {
> DbgPrint( “Failed open disk : %x\n”, status );
> }
> else
> {
> KEVENT event;
> PIRP irp;
>
> KeInitializeEvent( &event, NotificationEvent, FALSE );
>
> irp = IoBuildSynchronousFsdRequest( IRP_MJ_FLUSH_BUFFERS,
>
> pDeviceObject,
>
> NULL,
>
> 0,
>
> NULL,
>
> &event,
>
> &iosb );
>
> if( !irp )
> {
> DbgPrint( “Failed to allocate irp for flush : %x\n”,
> status );
> }
> else
> {
> // Put the fileobject in the iostack location
> PIO_STACK_LOCATION iostack =
> IoGetNextIrpStackLocation( irp );
>
> iostack->FileObject = pFileObject;
>
> DbgPrint( “About to flush drive…\n”) ;
>
> status = IoCallDriver( pDeviceObject, irp );
>
> if( status == STATUS_PENDING )
> {
> // DbgPrint( “IoCallDriver returned pending so
> wait…\n” ) ;
>
> KeWaitForSingleObject( &event, Executive,
> KernelMode, FALSE, NULL );
>
> status = iosb.Status;
> }
>
> DbgPrint( “Status from flush buffers : %x\n”,
> iosb.Status ) ;
> }
>
> ObDereferenceObject( pFileObject );
> }
> }
>
> Funny thing is that if I use my little console app to do much the same thing
> (using flushfilebuffers with a handle to the drive) then it works fine.
>
> Am I missing a trick here
>
> Ben Curley
> DESlock+ Lead Programmer
> Data Encryption Systems Ltd.
>
> Tel: +44 (0)1823 352357 (Main)
> Tel: +44 (0)1823 358320 (Direct Dial)
>
> Web: http://www.deslock.com
>
> —
> Questions? First check the IFS FAQ at
https://www.osronline.com/article.cfm?id=17
>
> You are currently subscribed to ntfsd as: xxxxx@storagecraft.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com