handling removal of volume device

Hi,
I have a Upper volume filter in which I need to deny the request to
remove a device(disk management=>delete the volume). I need to this only
when device is in a specific logical (my own!!) state.

So I tried two things
1.
a. When a device enters that logical state, I used
IoGetAttachedDeviceReference to draw a reference to the device on top of
stack.
b. when a device leaves this logical state (which could be much later),
I used ObDereferenceObject to decrease the ref count(of device that I
got from IoGetAttachedDeviceReference) so that it can be deleted.

This did not help. When I deleted the partition from disk management,
the device was deleted. I got surprise remove and then remove PNP
notifications even though the device was in that logical state.

I decided to go against the documentation and I returned
STATUS_DEVICE_BUSY from both surprise remove and remove PNP handlers.
This did not help either. Partition was still removed.

I have a similar check built into Query_remove handler where I fail the
Irp with STATUS_DEVICE_BUSY if the device is in that state.

How Can I prevent a volume device from being deleted?
I searched OSR but could not find anything specific about my need.

Thanks for the help
Harish

If art interprets our dreams, the computer executes them in the guise of
programs!