Shadow Device Dispatch hangs

Hi,

I have created the shadow device object as below with its dispatch routine.

//---------------------------------CREATING SHADOW DEVICE
OBJECT----------------------//

status =
IoCreateDevice(gFileSpyDriverObject, sizeof(MY_DEVICE_EXTENSION),
&nameString, FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
ClearFlag(fdo->Flags, DO_DEVICE_INITIALIZING);

//-----------------------------------------------------------------------------------------------------------//
After this i initialize the stack size of the device object and Device
Extension.

//-------------------------------------DISPATCH
ROUTINE-----------------------------------------//
PDEVICE_OBJECT attachedDevice =
((MY_DEVICE_EXTENSION)DeviceObject->DeviceExtension->RealDeviceObject->DeviceExtension)->AttachedToDeviceObject;
PFILE_OBJECT fileObject;

DbgPrint(“SHADOW DEVICE OBJECT…\n”);
pIoStackLocation = IoGetCurrentIrpStackLocation(Irp);
fileObject->DeviceObject = attachedDevice;
IoSkipCurrentIrpStackLocation(Irp);
status = IoCallDriver( attachedDevice, Irp );

//--------------------------------------------------------------------------------------------------------//

When the call comes to me i call ZwCreateFile during normal disptach but i
redirect this call to the shadow device and then call ZwClose.

The Problem is that the call hangs in my Dispatch routine at IoCallDriver
when i try to open the file from network. First time the call passes but the
second time it tries to call it again it fails. (while opening a file in
wordpad the file can be opened multiple times.)

Please let me know if there is some problem here

Thanks
Suhail


Get fast, reliable Internet access with MSN 9 Dial-up – now 3 months FREE!
http://join.msn.click-url.com/go/onm00200361ave/direct/01/

You are not handling the FILE_COMPLETE_IF_OPLOCKED bit properly. Note
that if this is set in the original request, you will need to set it in
your new request, otherwise it will hang waiting for the oplock break to
be processed.

Since APCs are disabled in that thread, the APC needed to handle the
oplock break cannot run. By setting this bit, you tell the file system
NOT to wait for the oplock break to complete. That eliminates the hang.

Regards,

Tony

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

Looking forward to seeing you at the Next OSR File Systems Class October
18, 2004 in Silicon Valley!

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Suhail Ansari
Sent: Monday, June 28, 2004 12:06 PM
To: ntfsd redirect
Subject: [ntfsd] Shadow Device Dispatch hangs

Hi,

I have created the shadow device object as below with its dispatch
routine.

//---------------------------------CREATING SHADOW DEVICE
OBJECT----------------------//

status =
IoCreateDevice(gFileSpyDriverObject,
sizeof(MY_DEVICE_EXTENSION),
&nameString,
FILE_DEVICE_UNKNOWN, 0,
FALSE, &fdo);
ClearFlag(fdo->Flags, DO_DEVICE_INITIALIZING);

//----------------------------------------------------------------------
-------------------------------------//
After this i initialize the stack size of the device object and Device
Extension.

//-------------------------------------DISPATCH
ROUTINE-----------------------------------------//
PDEVICE_OBJECT attachedDevice =
((MY_DEVICE_EXTENSION)DeviceObject->DeviceExtension->RealDeviceObject->D
eviceExtension)->AttachedToDeviceObject;
PFILE_OBJECT fileObject;

DbgPrint(“SHADOW DEVICE OBJECT…\n”);
pIoStackLocation = IoGetCurrentIrpStackLocation(Irp);
fileObject->DeviceObject = attachedDevice;
IoSkipCurrentIrpStackLocation(Irp);
status = IoCallDriver( attachedDevice, Irp );

//----------------------------------------------------------------------
----------------------------------//

When the call comes to me i call ZwCreateFile during normal disptach but
i
redirect this call to the shadow device and then call ZwClose.

The Problem is that the call hangs in my Dispatch routine at
IoCallDriver
when i try to open the file from network. First time the call passes but
the
second time it tries to call it again it fails. (while opening a file in

wordpad the file can be opened multiple times.)

Please let me know if there is some problem here

Thanks
Suhail


Get fast, reliable Internet access with MSN 9 Dial-up - now 3 months
FREE!
http://join.msn.click-url.com/go/onm00200361ave/direct/01/


Questions? First check the IFS FAQ at
https://www.osronline.com/article.cfm?id=17

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

Hi,

Thanks Tony that solved one of the the problem but there is another problem
that i have.

PROBLEM:
I call this ZwCreateFile and then call ZwQueryXXX routine to get the
filesize and then ZwReadFile. The problem is that ZwReadFile Hangs. I create
a new thread and call this functions there. Below is the code

if( (Options & FILE_COMPLETE_IF_OPLOCKED) == 0)
{
StatusRet = ZwCreateFile(&FileHandle,
FILE_READ_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
0,
FILE_SHARE_VALID_FLAGS,
FILE_OPEN,
FILE_NO_INTERMEDIATE_BUFFERING | FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0
);
}
else
{
StatusRet = ZwCreateFile(&FileHandle,
FILE_READ_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
0,
FILE_SHARE_VALID_FLAGS,
FILE_OPEN,
FILE_NO_INTERMEDIATE_BUFFERING | FILE_COMPLETE_IF_OPLOCKED |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0
);
}
if(!NT_SUCCESS(StatusRet))
{
goto EXIT_CREATE;
}

// Query
ZwQuery(…);

// Calculate the file offset from where to read in the file
ReadOffset.QuadPart = FileInformation.EndOfFile.QuadPart -
HEADER_SIZE_WITH_LEN;

// Get the header
Header = ExAllocatePool(NonPagedPool, HEADER_SIZE_WITH_LEN);
if(Header == NULL)
{
StatusRet = STATUS_INSUFFICIENT_RESOURCES;
goto EXIT_CREATE;
}

StatusRet = ZwReadFile(FileHandle, NULL, NULL, NULL, &IoStatusBlock, Header,
HEADER_SIZE_WITH_LEN, &ReadOffset, NULL);

if(!NT_SUCCESS(StatusRet))
{
goto EXIT_CREATE;
}

DbgPrint(“ZwReadFile output is …%x\n”, StatusRet);
DbgPrint(“IoStatusBlock.Status is …%x\n”, IoStatusBlock.Status);
DbgPrint(“IoStatusBlock.Information is …%d\n”,
IoStatusBlock.Information);

Thanks
Suhail


Get fast, reliable Internet access with MSN 9 Dial-up – now 3 months FREE!
http://join.msn.click-url.com/go/onm00200361ave/direct/01/

I don’t see how this code fixes your original problem. Did you look at
STATUS_OPLOCK_BREAK_IN_PROGRESS? The numeric value is 0x107 and that is
a success code. But if the caller has blocked APCs (you might wish to
check this) the read will likely never “complete” because it waits for
the oplock to finish breaking.

You are not even implementing the oplock protocol correctly. Try going
back and reading the Platform SDK documentation on oplocks, as well as
looking over some of the articles we’ve written about oplocks - your
implementation is just not going to work as written.

Of course, the fundamental point here is that you need to learn how to
debug thread hangs. They are a common form of problem in file systems
space, so this is an essential skill. The basic question to ask
yourself is “why is this thread not making forward progress? What is it
waiting for? Why is the thing for which it waits not becoming
available/signaled/completed?”

Regards,

Tony

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

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Suhail Ansari
Sent: Wednesday, June 30, 2004 6:39 PM
To: ntfsd redirect
Subject: RE: [ntfsd] Shadow Device Dispatch hangs

Hi,

Thanks Tony that solved one of the the problem but there is another
problem that i have.

PROBLEM:
I call this ZwCreateFile and then call ZwQueryXXX routine to get the
filesize and then ZwReadFile. The problem is that ZwReadFile Hangs. I
create a new thread and call this functions there. Below is the code

if( (Options & FILE_COMPLETE_IF_OPLOCKED) == 0) {
StatusRet = ZwCreateFile(&FileHandle,
FILE_READ_DATA | FILE_READ_ATTRIBUTES |
SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
0,
FILE_SHARE_VALID_FLAGS,
FILE_OPEN,
FILE_NO_INTERMEDIATE_BUFFERING |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0
);
}
else
{
StatusRet = ZwCreateFile(&FileHandle,
FILE_READ_DATA | FILE_READ_ATTRIBUTES |
SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
0,
FILE_SHARE_VALID_FLAGS,
FILE_OPEN,
FILE_NO_INTERMEDIATE_BUFFERING |
FILE_COMPLETE_IF_OPLOCKED | FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0
);
}
if(!NT_SUCCESS(StatusRet))
{
goto EXIT_CREATE;
}

// Query
ZwQuery(…);

// Calculate the file offset from where to read in the file
ReadOffset.QuadPart = FileInformation.EndOfFile.QuadPart -
HEADER_SIZE_WITH_LEN;

// Get the header
Header = ExAllocatePool(NonPagedPool, HEADER_SIZE_WITH_LEN); if(Header
== NULL) {
StatusRet = STATUS_INSUFFICIENT_RESOURCES;
goto EXIT_CREATE;
}

StatusRet = ZwReadFile(FileHandle, NULL, NULL, NULL, &IoStatusBlock,
Header, HEADER_SIZE_WITH_LEN, &ReadOffset, NULL);

if(!NT_SUCCESS(StatusRet))
{
goto EXIT_CREATE;
}

DbgPrint(“ZwReadFile output is …%x\n”, StatusRet);
DbgPrint(“IoStatusBlock.Status is …%x\n”, IoStatusBlock.Status);
DbgPrint(“IoStatusBlock.Information is …%d\n”,
IoStatusBlock.Information);

Thanks
Suhail


Get fast, reliable Internet access with MSN 9 Dial-up - now 3 months
FREE!
http://join.msn.click-url.com/go/onm00200361ave/direct/01/


Questions? First check the IFS FAQ at
https://www.osronline.com/article.cfm?id=17

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

> Of course, the fundamental point here is that you need to learn how to

debug thread hangs. They are a common form of problem in file systems
space, so this is an essential skill. The basic question to ask
yourself is "why is this thread not making forward progress? What is it

!locks WinDbg command helps a lot.

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