Preventing filter reentrancy on create and "phantom" dev issue.

Hi everyone.

I’m using a technique proposed by Tony Mason to prevent reentrancy in my
filter when it needs to open
same file that is targeted in IRP_MJ_CREATE. In particular, in my filter
device I create another named
device that is used in the following manner. Whenever filter needs to open a
file in the filtering location
but it doesn’t want to see create IRP for this open it prepares file path in
format \PHANTOMDEVNAME\PathToFile
and calls ZwCreate for this path. PHANTOMDEVNAME is name of my device. So,
after parsing, I/O Mgr finally
issues IRP_MJ_CREATE to my “phantom” device where I simply pass this call
down to the FSD to which my original
filter device is attached (neat and beautiful solution!). It works just fine
except for the checked build where
I get the following assertion:

*** Assertion failed: No correspondence btwn file and device in
irp((IrpSp->FileObject->Vpb == NULL) && ((IrpSp->FileObject->DeviceObject !=
NULL) && (IrpSp->FileObject->DeviceObject->Vpb != NULL) &&
(IrpSp->DeviceObject ==
IrpSp->FileObject->DeviceObject->Vpb->DeviceObject))) ||
((IrpSp->FileObject->Vpb != NULL) && (IrpSp->DeviceObject ==
IrpSp->FileObject->Vpb->DeviceObject)) || (!FlagOn( Vcb->VcbState,
VCB_STATE_VOLUME_MOUNTED ))
*** Source File: D:\nt\private\ntos\cntfs\strucsup.c, line 6670

It still works (it does what it supposed to do) but I would like to get rid
of this assertion. And I need to know what would be the right way. Bellow is
my CREATE handler in the phantom device (BTW, that’s all it takes to solve
reenrancy issues!):

//
// IRP_MJ_CREATE handler
//
NTSTATUS CAppsFantomDevice::Create( PIRP Irp )
{
PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp );
PFILE_OBJECT FileObject = pIrpSp -> FileObject;

//
// A couple of checks…
//

if( m_FileSystemDevice == NULL )
{
DBGREPORT(“\nNo FSD below us!”);

COMPLETEIRP( Irp, STATUS_INVALID_PARAMETER, 0);

return STATUS_INVALID_PARAMETER;
}

if( FileObject -> RelatedFileObject != NULL )
{
DBGREPORT(“\nUnexpected relative create!”);

COMPLETEIRP( Irp, STATUS_INVALID_PARAMETER, 0);

return STATUS_INVALID_PARAMETER;
}

//
// Change device objects
//

if( FileObject -> DeviceObject != NULL )
{
if( FileObject -> DeviceObject == m_pDeviceObject )
{
// Deref us
InterlockedDecrement(&FileObject -> DeviceObject ->
ReferenceCount);
}
FileObject -> DeviceObject = m_FileSystemDevice;
// Adref them
InterlockedIncrement(&FileObject -> DeviceObject ->
ReferenceCount);
}

pIrpSp -> DeviceObject = m_FileSystemDevice;

//
// Call underlying driver
//

IoSkipCurrentIrpStackLocation( Irp );

return IoCallDriver( m_FileSystemDevice, Irp);
}

So, what’s wrong in this code?