Why my call to ZwWriteFile() failed?

Hi all,

I called ZwCreateFile() and ZwWriteFile() to write data to remote file in my
FS filter driver
But the return value of ZwWriteFile() was not NT_SUCCESS.
Why?
My code:
case IRP_MJ_WRITE:

if((RtlCompareMemory(fullPathName,MirrFolder,10)==10)&&(strlen(fullPathName)

10))
{
DbgPrint((“We Should write here !\n”));
RtlInitUnicodeString(&myObjectName,L"\Device\Mup\Ren-test\CopyFile
\test.txt");

InitializeObjectAttributes(&myObjectAttr,
&myObjectName,OBJ_KERNEL_HANDLE,NULL,NULL);

ntStatus=ZwCreateFile(&myCopyFile,FILE_ANY_ACCESS,&myObjectAttr,&myIoStatus,
NULL,FILE_ATTRIBUTE_NORMAL,FILE_SHARE_READ|FILE_SHARE_WRITE,
FILE_OPEN_IF,FILE_NO_INTERMEDIATE_BUFFERING,NULL,0);
hookCompletion=NT_SUCCESS(ntStatus);
if(!hookCompletion)

DbgPrint((“Cannot create the target file object.\n”));
}
else
{
//
// Initialize the event
//
KeInitializeEvent(&event, SynchronizationEvent, FALSE);
ntStatus=ZwWriteFile(myCopyFile,&event,NULL,NULL,&IoStatusBlock,
Irp->UserBuffer,currentIrpStack->Parameters.Write.Length,
&(currentIrpStack->Parameters.Write.ByteOffset),NULL);

hookCompletion=NT_SUCCESS(ntStatus);
if(!hookCompletion){
DbgPrint((“Filemirr: Could not Write to remote file\n”));
}else// we get the pointer of the file object then we create IRP
{
//
// Wait for the I/O
//
KeWaitForSingleObject(&event, Executive, KernelMode, TRUE, 0);
//to see if the write OK or not
hookCompletion=NT_SUCCESS( IoStatusBlock.Status );
if(hookCompletion)
{
DbgPrint((“Write to remote file OK!!\n”));
}
else
{
DbgPrint((“Write To Remote File Failed!!\n”));
}
ZwClose(myCopyFile);


Do You Yahoo!?

Get your free @yahoo.com address at http://mail.yahoo.com

Perhaps you could share the status code with us? It would make it easier in
trying to understand what the problem is in your code.

Regards,

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

Hope to see you at the next OSR file systems class October 7, 2002!

-----Original Message-----
From: gaoren [mailto:xxxxx@yahoo.ca]
Sent: Wednesday, May 22, 2002 9:32 AM
To: File Systems Developers
Subject: [ntfsd] Why my call to ZwWriteFile() failed?

Hi all,

I called ZwCreateFile() and ZwWriteFile() to write data to remote file in my
FS filter driver But the return value of ZwWriteFile() was not NT_SUCCESS.
Why? My code:
case IRP_MJ_WRITE:

if((RtlCompareMemory(fullPathName,MirrFolder,10)==10)&&(strlen(fullPathName)

10))
{
DbgPrint((“We Should write here !\n”));

RtlInitUnicodeString(&myObjectName,L"\Device\Mup\Ren-test\CopyFile
\test.txt");

InitializeObjectAttributes(&myObjectAttr,

&myObjectName,OBJ_KERNEL_HANDLE,NULL,NULL);

ntStatus=ZwCreateFile(&myCopyFile,FILE_ANY_ACCESS,&myObjectAttr,&myIoStatus,

NULL,FILE_ATTRIBUTE_NORMAL,FILE_SHARE_READ|FILE_SHARE_WRITE,

FILE_OPEN_IF,FILE_NO_INTERMEDIATE_BUFFERING,NULL,0);
hookCompletion=NT_SUCCESS(ntStatus);
if(!hookCompletion)

DbgPrint((“Cannot create the target file
object.\n”));
}
else
{
//
// Initialize the event
//
KeInitializeEvent(&event, SynchronizationEvent,
FALSE);

ntStatus=ZwWriteFile(myCopyFile,&event,NULL,NULL,&IoStatusBlock,

Irp->UserBuffer,currentIrpStack->Parameters.Write.Length,

&(currentIrpStack->Parameters.Write.ByteOffset),NULL);

hookCompletion=NT_SUCCESS(ntStatus);
if(!hookCompletion){
DbgPrint((“Filemirr: Could not Write to
remote file\n”));
}else// we get the pointer of the file object then
we create IRP
{
//
// Wait for the I/O
//
KeWaitForSingleObject(&event, Executive, KernelMode,
TRUE, 0);
//to see if the write OK or not
hookCompletion=NT_SUCCESS( IoStatusBlock.Status );
if(hookCompletion)
{
DbgPrint((“Write to remote file OK!!\n”));
}
else
{
DbgPrint((“Write To Remote File
Failed!!\n”));
}
ZwClose(myCopyFile);


Do You Yahoo!?

Get your free @yahoo.com address at http://mail.yahoo.com


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

Hi Tony,
I can not get the ntStatus code from this varaiable in windbg.
The IoStatusBlock info as follow:
IoStatusBlock.Status: -21234567184 – invalid
IoStatusBlock.Information: 0x16e7650 – invalid
IoStatusBlock.Pointer:0x816e7650 – value: 0x02

And I also can not get this variable’s info:
PIO_STACK_LOCATION currentIrpStack = IoGetCurrentIrpStackLocation(Irp);

I changed a parameter in ZwCreateFile()
from FILE_NO_INTERMEDIATE_BUFFERING to NULL,
because if NO_INTERMEDIATE_BUFFERING, the parameters in ZwWriteFile() has
some restriction . so I use NUL now. But the result is the same.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Tony Mason
Sent: Wednesday, May 22, 2002 9:47 AM
To: File Systems Developers
Subject: [ntfsd] RE: Why my call to ZwWriteFile() failed?

Perhaps you could share the status code with us? It would make it easier in
trying to understand what the problem is in your code.

Regards,

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

Hope to see you at the next OSR file systems class October 7, 2002!

-----Original Message-----
From: gaoren [mailto:xxxxx@yahoo.ca]
Sent: Wednesday, May 22, 2002 9:32 AM
To: File Systems Developers
Subject: [ntfsd] Why my call to ZwWriteFile() failed?

Hi all,

I called ZwCreateFile() and ZwWriteFile() to write data to remote file in my
FS filter driver But the return value of ZwWriteFile() was not NT_SUCCESS.
Why? My code:
case IRP_MJ_WRITE:

if((RtlCompareMemory(fullPathName,MirrFolder,10)==10)&&(strlen(fullPathName)

10))
{
DbgPrint((“We Should write here !\n”));

RtlInitUnicodeString(&myObjectName,L"\Device\Mup\Ren-test\CopyFile
\test.txt");

InitializeObjectAttributes(&myObjectAttr,

&myObjectName,OBJ_KERNEL_HANDLE,NULL,NULL);

ntStatus=ZwCreateFile(&myCopyFile,FILE_ANY_ACCESS,&myObjectAttr,&myIoStatus,

NULL,FILE_ATTRIBUTE_NORMAL,FILE_SHARE_READ|FILE_SHARE_WRITE,

FILE_OPEN_IF,FILE_NO_INTERMEDIATE_BUFFERING,NULL,0);
hookCompletion=NT_SUCCESS(ntStatus);
if(!hookCompletion)

DbgPrint((“Cannot create the target file
object.\n”));
}
else
{
//
// Initialize the event
//
KeInitializeEvent(&event, SynchronizationEvent,
FALSE);

ntStatus=ZwWriteFile(myCopyFile,&event,NULL,NULL,&IoStatusBlock,

Irp->UserBuffer,currentIrpStack->Parameters.Write.Length,

&(currentIrpStack->Parameters.Write.ByteOffset),NULL);

hookCompletion=NT_SUCCESS(ntStatus);
if(!hookCompletion){
DbgPrint((“Filemirr: Could not Write to
remote file\n”));
}else// we get the pointer of the file object then
we create IRP
{
//
// Wait for the I/O
//
KeWaitForSingleObject(&event, Executive, KernelMode,
TRUE, 0);
//to see if the write OK or not
hookCompletion=NT_SUCCESS( IoStatusBlock.Status );
if(hookCompletion)
{
DbgPrint((“Write to remote file OK!!\n”));
}
else
{
DbgPrint((“Write To Remote File
Failed!!\n”));
}
ZwClose(myCopyFile);


Do You Yahoo!?

Get your free @yahoo.com address at http://mail.yahoo.com


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


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


Do You Yahoo!?

Get your free @yahoo.com address at http://mail.yahoo.com

Hi,

KeInitializeEvent(&event, SynchronizationEvent,
FALSE);

ntStatus=ZwWriteFile(myCopyFile,&event,NULL,NULL,&IoStatusBlock,

The second parameter should be HANDLE of the event, not pointer to the
event object. You can get this handle using IoCreateSynchronizationEvent.
IoStatusBlock will be uninitialized if write is still pending.

Alexei.

Thank you Alexei.
Followed your advice, but it still did not work. :frowning:
I got the event handle but can not find the pointer info in WinDbg.
Laura

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Alexei Jelvis
Sent: Wednesday, May 22, 2002 11:44 AM
To: File Systems Developers
Subject: [ntfsd] RE: Why my call to ZwWriteFile() failed?

Hi,

KeInitializeEvent(&event, SynchronizationEvent,
FALSE);

ntStatus=ZwWriteFile(myCopyFile,&event,NULL,NULL,&IoStatusBlock,

The second parameter should be HANDLE of the event, not pointer to the
event object. You can get this handle using IoCreateSynchronizationEvent.
IoStatusBlock will be uninitialized if write is still pending.

Alexei.


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


Do You Yahoo!?

Get your free @yahoo.com address at http://mail.yahoo.com

Hi,

ntStatus=ZwWriteFile(myCopyFile,&event,NULL,NULL,&IoStatusBlock,

If ntStatus != STATUS_PENDING you can use it as the final status of the
operation. If it is you can use ZwWaitForSingleObject to wait on the event
handle and then check IoStatusBlock.

Another way to write to a file is build your own IRP using
IoBuildAsynchronousFsdRequest and send it to the device. This approach
will give more control to check what is going wrong.

Alexei.

Yes I tried to generate my IRP but when send it to Mup or LanmanRedirector
they all failed.
And I didnot have IFS or Rajeev’s book
so I thought that ZwWriteFile may be simpler. But it still didn’t work to
remote file.
I will try again.
Thanks a lot.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Alexei Jelvis
Sent: Wednesday, May 22, 2002 1:55 PM
To: File Systems Developers
Subject: [ntfsd] RE: Why my call to ZwWriteFile() failed?

Hi,

ntStatus=ZwWriteFile(myCopyFile,&event,NULL,NULL,&IoStatusBlock,

If ntStatus != STATUS_PENDING you can use it as the final status of the
operation. If it is you can use ZwWaitForSingleObject to wait on the event
handle and then check IoStatusBlock.

Another way to write to a file is build your own IRP using
IoBuildAsynchronousFsdRequest and send it to the device. This approach
will give more control to check what is going wrong.

Alexei.


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


Do You Yahoo!?

Get your free @yahoo.com address at http://mail.yahoo.com