How to use IoBuildSynchronousFsdRequest to read a disk file?

hi,

I want to use IoBuildSynchronousFsdRequest to read/write “c:\1.txt” at
“HardDisk\Volume1”, coz ZwOpenFile sometimes failes with a error
STATUS_SHARE_VIOLATION.

How to set its parameters? I have read the DDK docs, but can’t figure out
how to fill the 3rd parameter named “buffer”.

Thx for help!

Here is example of a file logging function that I use. The 3rd parameter is a pointer to a character buffer
that contains the logging text. The 4th parameter contains the size of the character buffer. It can be just that, a simple buffer.

NTSTATUS Status;
IO_STATUS_BLOCK ioStatusBlock;
PIRP newIRP;
KEVENT event;
PIO_STACK_LOCATION IrpSp;

if( m_FileHandle == NULL )
{
return;
}
if( m_WriteCount == 0 )
{
return;
}

KeInitializeEvent( &event,
NotificationEvent,
FALSE );

newIRP = IoBuildSynchronousFsdRequest( IRP_MJ_WRITE,
m_DeviceObject,
m_WriteBuffer,
m_WriteCount,
&m_FilePosition,
&event,
&ioStatusBlock );

// can’t flush, can’t create an IRP
if( !newIRP )
{
return;
}

IrpSp = IoGetNextIrpStackLocation( newIRP );
IrpSp->FileObject = m_FileObject;
Status = IoCallDriver( m_DeviceObject, newIRP );

if( Status == STATUS_PENDING )
{
KeWaitForSingleObject( &event,
Executive,
KernelMode,
FALSE,
NULL );
}
Status = ioStatusBlock.Status;
if( NT_SUCCESS(Status) )
{
m_FilePosition.LowPart += ioStatusBlock.Information;
}

-----Original Message-----
From: zhangbo [mailto:xxxxx@gmx.net]
Sent: Thursday, October 10, 2002 5:28 AM
To: File Systems Developers
Subject: [ntfsd] How to use IoBuildSynchronousFsdRequest to read a disk
file?

hi,

I want to use IoBuildSynchronousFsdRequest to read/write “c:\1.txt” at
“HardDisk\Volume1”, coz ZwOpenFile sometimes failes with a error
STATUS_SHARE_VIOLATION.

How to set its parameters? I have read the DDK docs, but can’t figure out
how to fill the 3rd parameter named “buffer”.

Thx for help!


You are currently subscribed to ntfsd as: xxxxx@inflectionsystems.com
To unsubscribe send a blank email to %%email.unsub%%

thx for the sample code.

But how to associate the “c:\1.txt” with m_FileObject? Shall I use
ZwOpenFile and ObReferenceObjectByHandle to do this? ZwOpenFile sometimes
returns STATUS_SHARE_VIOLATION with this file. thx again.

are u trying to read the file in an irp request for the file? if so, the
file object in the irp is the one. if u are trying to open and read/write
the file in an irp not for the file, and zwopenfile fails with sharing
violation, i don’t think u will be able to access it even if u roll your own
create irp for it.

Ho Mun Chuen
@@ “Not everything that counts can be counted;
<” )~ and not everything that can be counted counts"
//\ … Albert Einstein
----- Original Message -----
From: “zhangbo”
To: “File Systems Developers”
Sent: Friday, October 11, 2002 9:28 AM
Subject: [ntfsd] RE: How to use IoBuildSynchronousFsdRequest to read a disk
file?

thx for the sample code.

But how to associate the “c:\1.txt” with m_FileObject? Shall I use
ZwOpenFile and ObReferenceObjectByHandle to do this? ZwOpenFile sometimes
returns STATUS_SHARE_VIOLATION with this file. thx again.


You are currently subscribed to ntfsd as: xxxxx@pmail.ntu.edu.sg
To unsubscribe send a blank email to %%email.unsub%%

yes. I want to do some pre-processing and post-processing with some files in the filter driver.

  1. When the filter driver receives IRP_MJ_CREATE, it tries to ZwOpen/ZwRead/write/close the file specified in the IRP, then passes the IRP to lower driver.

  2. When the filter driver receives IRP_MJ_CLOSE, it saves the file name specified in the IRP, and passes the IRP to lower driver, then tries to read/write that file in the completion routine.

Is the above design scheme correct? Thx. I encountered some BSOD when running the filter driver.
“Ho Mun Chuen”
are u trying to read the file in an irp request for the file? if so, the
file object in the irp is the one. if u are trying to open and read/write
the file in an irp not for the file, and zwopenfile fails with sharing
violation, i don’t think u will be able to access it even if u roll your own
create irp for it.

some points i can see is

  1. if your zwopen fails with sharing violation prior sending to lower driver, try changing some of the share access settings. if it still fails, i think it because the file has already been opened by some other thread/process and it did not opened it for sharing. most probably the create request itself will also fail if u pass it down.

  2. by using zwxxx calls, u will have to handle re-entrancy issues of requests generated due to these calls coming back to your filter driver. have u handled those?

  3. file names are only definitely valid in create, and not in others. so u may not be always able to get the file name. in that case, u may get bsod trying to access a null file name.

  4. if the file is set for delete on close, it will not be there on close completion for your driver to open.

Ho Mun Chuen
@@ “Not everything that counts can be counted;
<” )~ and not everything that can be counted counts"
//\ … Albert Einstein
----- Original Message -----
From: zhangbo
Newsgroups: ntfsd
To: File Systems Developers
Sent: Friday, October 11, 2002 10:32 AM
Subject: [ntfsd] Re: How to use IoBuildSynchronousFsdRequest to read a disk file?

yes. I want to do some pre-processing and post-processing with some files in the filter driver.

  1. When the filter driver receives IRP_MJ_CREATE, it tries to ZwOpen/ZwRead/write/close the file specified in the IRP, then passes the IRP to lower driver.

  2. When the filter driver receives IRP_MJ_CLOSE, it saves the file name specified in the IRP, and passes the IRP to lower driver, then tries to read/write that file in the completion routine.

Is the above design scheme correct? Thx. I encountered some BSOD when running the filter driver.
“Ho Mun Chuen”
are u trying to read the file in an irp request for the file? if so, the
file object in the irp is the one. if u are trying to open and read/write
the file in an irp not for the file, and zwopenfile fails with sharing
violation, i don’t think u will be able to access it even if u roll your own
create irp for it.

b???.???????&?v?'??jƦ?ȩ???۞v? N???r??zǧu??jy???^j???xBX???,??&

yeah I have avoided re-entrancy. I will try changing the share access permissions.
thx again.
“Ho Mun Chuen” ???:xxxxx@ntfsd…
some points i can see is

1. if your zwopen fails with sharing violation prior sending to lower driver, try changing some of the share access settings. if it still fails, i think it because the file has already been opened by some other thread/process and it did not opened it for sharing. most probably the create request itself will also fail if u pass it down.

2. by using zwxxx calls, u will have to handle re-entrancy issues of requests generated due to these calls coming back to your filter driver. have u handled those?

3. file names are only definitely valid in create, and not in others. so u may not be always able to get the file name. in that case, u may get bsod trying to access a null file name.

4. if the file is set for delete on close, it will not be there on close completion for your driver to open.

Ho Mun Chuen
@@ “Not everything that counts can be counted;
<” )~ and not everything that can be counted counts"
//\ … Albert Einstein

You can open the file for only SYNCHRONIZE to avoid open sharing violations. However, a checked build will compain when you start issuing the read and write IRPs to the file object that isn’t opened for read or write access. The I/O will succeed, but you will get assertions from the checked build of Windows 2000 (and probably other versions also).

-----Original Message-----
From: zhangbo [mailto:xxxxx@gmx.net]
Sent: Friday, October 11, 2002 9:21 AM
To: File Systems Developers
Subject: [ntfsd] Re: How to use IoBuildSynchronousFsdRequest to read a disk file?

yeah I have avoided re-entrancy. I will try changing the share access permissions.
thx again.

“Ho Mun Chuen” < xxxxx@pmail.ntu.edu.sg> ???:xxxxx@ntfsd…
some points i can see is

  1. if your zwopen fails with sharing violation prior sending to lower driver, try changing some of the share access settings. if it still fails, i think it because the file has already been opened by some other thread/process and it did not opened it for sharing. most probably the create request itself will also fail if u pass it down.

  2. by using zwxxx calls, u will have to handle re-entrancy issues of requests generated due to these calls coming back to your filter driver. have u handled those?

  3. file names are only definitely valid in create, and not in others. so u may not be always able to get the file name. in that case, u may get bsod trying to access a null file name.

  4. if the file is set for delete on close, it will not be there on close completion for your driver to open.

Ho Mun Chuen
@@ “Not everything that counts can be counted;
<” )~ and not everything that can be counted counts"
//\ … Albert Einstein

b???v?jj???ʚ?:.?˛???m??֛???zf???%y?ޞ??wݵ?Ib??(??(