ZwWriteFile fails with status code 0xC0000054

Hi,
I have a disk filter driver. From this driver I want to flush some data to
a file when the user requests the driver to flush the data.

I am using ZwCreateFile and ZwWriteFile to achieve it. The same code also
flushes the data to the disk when the system is shutting down.

I am writing data in multiples of 512 bytes. The write succeeds for 1st
512 bytes of data, but the next 512 or more bytes fails with status code
0xC0000054. If the 1st write is writing more then 512 bytes then that also
fails with the same status code.

Does anybody have any idea why it might be happening like that. Any help
would be apreciated.

Regards
Avi

Here is the code which does that part.

//////////////////////////////////////////////////////////////////////////
NTSTATUS
SaveDataToDisk(
PDEVICE_EXTENSION deviceExtension
)
{
IO_STATUS_BLOCK IoStatus;
OBJECT_ATTRIBUTES objectAttributes;
NTSTATUS status;
UNICODE_STRING fileName;
HANDLE fileHandle=0;
UCHAR buffer[SECTOR_SIZE];

RtlInitUnicodeString(&fileName, deviceExtension->DataFileName);
InitializeObjectAttributes( &objectAttributes,
(PUNICODE_STRING)&fileName,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);

status=ZwCreateFile( &fileHandle,
GENERIC_WRITE|SYNCHRONIZE,
&objectAttributes,
&IoStatus,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OVERWRITE_IF,
FILE_SYNCHRONOUS_IO_NONALERT |
FILE_NO_INTERMEDIATE_BUFFERING,
NULL,
0);

if(!NT_SUCCESS(status))
{
DebugPrint((1, " SaveDataToDisk: Cannot create data file, status =
<%X>\n", status));
goto exit_point;
}
RtlZeroMemory(buffer, sizeof(buffer));
buffer[0]=0xCE;
RtlCopyMemory(&buffer[1], &deviceExtension->FileSize1,
sizeof(deviceExtension-> FileSize1));
RtlCopyMemory(&buffer[5], &deviceExtension->FileSize2,
sizeof(deviceExtension-> FileSize2));
RtlCopyMemory(&buffer[9], &deviceExtension->FileSize3,
sizeof(deviceExtension-> FileSize3));
status=ZwWriteFile(fileHandle, NULL, NULL, NULL, &IoStatus, buffer,
512, NULL, NULL);
if(!NT_SUCCESS(status))
goto exit_point;

status=ZwWriteFile(fileHandle, NULL, NULL, NULL, &IoStatus,
deviceExtension->File1Data, deviceExtension-> FileSize1, NULL , NULL);
if(!NT_SUCCESS(status))
goto exit_point;

status=ZwWriteFile(fileHandle, NULL, NULL, NULL, &IoStatus,
deviceExtension-> File2Data, deviceExtension-> FileSize2, NULL, NULL);
if(!NT_SUCCESS(status))
goto exit_point;

status=ZwWriteFile(fileHandle, NULL, NULL, NULL, &IoStatus,
deviceExtension-> File3Data, deviceExtension-> FileSize3, NULL, NULL);
if(!NT_SUCCESS(status))
goto exit_point;

exit_point:
if(!NT_SUCCESS(status))
DebugPrint((3, “SaveDataToDisk: failed status = <%X>\n”, status));
if(fileHandle)
ZwClose(fileHandle);
return status;
}

//////////////////////////////////////////////////////////////////////////

You must specify NO_INTERMEDIATE_BIFFERING and WRITE_THROUGH; you are
seeing a cache deadlock.

Jamey

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Avinash Kumar
Sent: Monday, December 30, 2002 11:05 AM
To: NT Developers Interest List
Subject: [ntdev] ZwWriteFile fails with status code 0xC0000054

Hi,
I have a disk filter driver. From this driver I want to flush some data
to
a file when the user requests the driver to flush the data.

I am using ZwCreateFile and ZwWriteFile to achieve it. The same code
also
flushes the data to the disk when the system is shutting down.

I am writing data in multiples of 512 bytes. The write succeeds for 1st
512 bytes of data, but the next 512 or more bytes fails with status code
0xC0000054. If the 1st write is writing more then 512 bytes then that
also
fails with the same status code.

Does anybody have any idea why it might be happening like that. Any help
would be apreciated.

Regards
Avi

Here is the code which does that part.

////////////////////////////////////////////////////////////////////////
//
NTSTATUS
SaveDataToDisk(
PDEVICE_EXTENSION deviceExtension
)
{
IO_STATUS_BLOCK IoStatus;
OBJECT_ATTRIBUTES objectAttributes;
NTSTATUS status;
UNICODE_STRING fileName;
HANDLE fileHandle=0;
UCHAR buffer[SECTOR_SIZE];

RtlInitUnicodeString(&fileName, deviceExtension->DataFileName);
InitializeObjectAttributes( &objectAttributes,
(PUNICODE_STRING)&fileName,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);

status=ZwCreateFile( &fileHandle,
GENERIC_WRITE|SYNCHRONIZE,
&objectAttributes,
&IoStatus,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OVERWRITE_IF,
FILE_SYNCHRONOUS_IO_NONALERT |
FILE_NO_INTERMEDIATE_BUFFERING,
NULL,
0);

if(!NT_SUCCESS(status))
{
DebugPrint((1, " SaveDataToDisk: Cannot create data file, status

<%X>\n", status));
goto exit_point;
}
RtlZeroMemory(buffer, sizeof(buffer));
buffer[0]=0xCE;
RtlCopyMemory(&buffer[1], &deviceExtension->FileSize1,
sizeof(deviceExtension-> FileSize1));
RtlCopyMemory(&buffer[5], &deviceExtension->FileSize2,
sizeof(deviceExtension-> FileSize2));
RtlCopyMemory(&buffer[9], &deviceExtension->FileSize3,
sizeof(deviceExtension-> FileSize3));
status=ZwWriteFile(fileHandle, NULL, NULL, NULL, &IoStatus, buffer,
512, NULL, NULL);
if(!NT_SUCCESS(status))
goto exit_point;

status=ZwWriteFile(fileHandle, NULL, NULL, NULL, &IoStatus,
deviceExtension->File1Data, deviceExtension-> FileSize1, NULL , NULL);
if(!NT_SUCCESS(status))
goto exit_point;

status=ZwWriteFile(fileHandle, NULL, NULL, NULL, &IoStatus,
deviceExtension-> File2Data, deviceExtension-> FileSize2, NULL, NULL);
if(!NT_SUCCESS(status))
goto exit_point;

status=ZwWriteFile(fileHandle, NULL, NULL, NULL, &IoStatus,
deviceExtension-> File3Data, deviceExtension-> FileSize3, NULL, NULL);
if(!NT_SUCCESS(status))
goto exit_point;

exit_point:
if(!NT_SUCCESS(status))
DebugPrint((3, “SaveDataToDisk: failed status = <%X>\n”, status));
if(fileHandle)
ZwClose(fileHandle);
return status;
}

////////////////////////////////////////////////////////////////////////
//


You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

Thanks for the reply. I tried that option, but i am getting the same error
again :frowning: The DDK docs say that “FILE_WRITE_THROUGH flag is automatically
set if the CreateOptions flag FILE_NO_INTERMEDIATE_BUFFERING is set.”

Avi

You must specify NO_INTERMEDIATE_BIFFERING and WRITE_THROUGH; you are
seeing a cache deadlock.

Jamey

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Avinash Kumar
Sent: Monday, December 30, 2002 11:05 AM
To: NT Developers Interest List
Subject: [ntdev] ZwWriteFile fails with status code 0xC0000054

Hi,
I have a disk filter driver. From this driver I want to flush some data
to
a file when the user requests the driver to flush the data.

I am using ZwCreateFile and ZwWriteFile to achieve it. The same code
also
flushes the data to the disk when the system is shutting down.

I am writing data in multiples of 512 bytes. The write succeeds for 1st
512 bytes of data, but the next 512 or more bytes fails with status code
0xC0000054. If the 1st write is writing more then 512 bytes then that
also
fails with the same status code.

Does anybody have any idea why it might be happening like that. Any help
would be apreciated.

Regards
Avi