sys: FsContext now maintains reference to its DeviceObject

This commit is contained in:
Bill Zissimopoulos 2015-12-10 21:31:02 -08:00
parent 140e567fdc
commit de065afe8d
5 changed files with 68 additions and 60 deletions

View File

@ -50,56 +50,56 @@ static NTSTATUS FspFsvolCleanup(
NTSTATUS Result;
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
PFILE_OBJECT FileObject = IrpSp->FileObject;
FSP_FILE_CONTEXT *FsContext = FileObject->FsContext;
UINT64 UserContext = FsContext->UserContext;
UINT64 UserContext2 = (UINT_PTR)FileObject->FsContext2;
BOOLEAN DeletePending;
LONG OpenCount;
/* lock the FsContext */
ExAcquireResourceExclusiveLite(FsContext->Header.Resource, TRUE);
/* propagate the FsContext DeleteOnClose to DeletePending */
if (FsContext->DeleteOnClose)
FsContext->DeletePending = TRUE;
DeletePending = FsContext->DeletePending;
/* all handles on this FileObject are gone; decrement its FsContext->OpenCount */
OpenCount = FspFileContextClose(FsContext);
/* unlock the FsContext */
ExReleaseResourceLite(FsContext->Header.Resource);
/* is the FsContext going away as well? */
if (0 == OpenCount)
{
/*
* The following must be done under the file system volume device Resource,
* because we are manipulating its GenericTable.
*/
ExAcquireResourceExclusiveLite(&FsvolDeviceExtension->Base.Resource, TRUE);
try
{
/* remove the FsContext from the file system volume device generic table */
FspFsvolDeviceDeleteContext(DeviceObject, FsContext->UserContext, 0);
}
finally
{
ExReleaseResourceLite(&FsvolDeviceExtension->Base.Resource);
}
}
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
if (!FspDeviceRetain(FsvrtDeviceObject))
return STATUS_CANCELLED;
try
{
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension =
FspFsvrtDeviceExtension(FsvrtDeviceObject);
PFILE_OBJECT FileObject = IrpSp->FileObject;
FSP_FILE_CONTEXT *FsContext = FileObject->FsContext;
UINT64 UserContext = FsContext->UserContext;
UINT64 UserContext2 = (UINT_PTR)FileObject->FsContext2;
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension = FspFsvrtDeviceExtension(FsvrtDeviceObject);
BOOLEAN FileNameRequired = 0 != FsvrtDeviceExtension->VolumeParams.FileNameRequired;
BOOLEAN DeletePending;
LONG OpenCount;
FSP_FSCTL_TRANSACT_REQ *Request;
/* lock the FsContext */
ExAcquireResourceExclusiveLite(FsContext->Header.Resource, TRUE);
/* propagate the FsContext DeleteOnClose to DeletePending */
if (FsContext->DeleteOnClose)
FsContext->DeletePending = TRUE;
DeletePending = FsContext->DeletePending;
/* all handles on this FileObject are gone; decrement its FsContext->OpenCount */
OpenCount = FspFileContextClose(FsContext);
/* unlock the FsContext */
ExReleaseResourceLite(FsContext->Header.Resource);
/* is the FsContext going away as well? */
if (0 == OpenCount)
{
/*
* The following must be done under the file system volume device Resource,
* because we are manipulating its GenericTable.
*/
ExAcquireResourceExclusiveLite(&FsvolDeviceExtension->Base.Resource, TRUE);
try
{
/* remove the FsContext from the file system volume device generic table */
FspFsvolDeviceDeleteContext(DeviceObject, FsContext->UserContext, 0);
}
finally
{
ExReleaseResourceLite(&FsvolDeviceExtension->Base.Resource);
}
}
/* create the user-mode file system request */
Result = FspIopCreateRequest(Irp, FileNameRequired ? &FsContext->FileName : 0, 0, &Request);
if (!NT_SUCCESS(Result))

View File

@ -50,24 +50,24 @@ static NTSTATUS FspFsvolClose(
NTSTATUS Result;
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
PFILE_OBJECT FileObject = IrpSp->FileObject;
FSP_FILE_CONTEXT *FsContext = FileObject->FsContext;
UINT64 UserContext = FsContext->UserContext;
UINT64 UserContext2 = (UINT_PTR)FileObject->FsContext2;
/* dereference the FsContext (and delete if no more references) */
FspFileContextRelease(FsContext);
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
if (!FspDeviceRetain(FsvrtDeviceObject))
return STATUS_CANCELLED;
try
{
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension =
FspFsvrtDeviceExtension(FsvrtDeviceObject);
PFILE_OBJECT FileObject = IrpSp->FileObject;
FSP_FILE_CONTEXT *FsContext = FileObject->FsContext;
UINT64 UserContext = FsContext->UserContext;
UINT64 UserContext2 = (UINT_PTR)FileObject->FsContext2;
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension = FspFsvrtDeviceExtension(FsvrtDeviceObject);
BOOLEAN FileNameRequired = 0 != FsvrtDeviceExtension->VolumeParams.FileNameRequired;
FSP_FSCTL_TRANSACT_REQ *Request;
/* dereference the FsContext (and delete if no more references) */
FspFileContextRelease(FsContext);
/* create the user-mode file system request */
Result = FspIopCreateRequest(0, FileNameRequired ? &FsContext->FileName : 0, 0, &Request);
if (!NT_SUCCESS(Result))

View File

@ -63,14 +63,14 @@ static NTSTATUS FspFsvolCreate(
NTSTATUS Result;
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
if (!FspDeviceRetain(FsvrtDeviceObject))
return STATUS_CANCELLED;
try
{
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension =
FspFsvrtDeviceExtension(FsvrtDeviceObject);
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension = FspFsvrtDeviceExtension(FsvrtDeviceObject);
PFILE_OBJECT FileObject = IrpSp->FileObject;
PFILE_OBJECT RelatedFileObject = FileObject->RelatedFileObject;
UNICODE_STRING FileName = FileObject->FileName;
@ -223,7 +223,7 @@ static NTSTATUS FspFsvolCreate(
BOOLEAN AppendBackslash =
sizeof(WCHAR) * 2/* not empty or root */ <= RelatedFsContext->FileName.Length &&
sizeof(WCHAR) <= FileName.Length && L':' != FileName.Buffer[0];
Result = FspFileContextCreate(
Result = FspFileContextCreate(DeviceObject,
RelatedFsContext->FileName.Length + AppendBackslash * sizeof(WCHAR) + FileName.Length,
&FsContext);
if (!NT_SUCCESS(Result))
@ -254,7 +254,7 @@ static NTSTATUS FspFsvolCreate(
goto exit;
}
Result = FspFileContextCreate(
Result = FspFileContextCreate(DeviceObject,
FileName.Length,
&FsContext);
if (!NT_SUCCESS(Result))

View File

@ -398,11 +398,13 @@ typedef struct
BOOLEAN DeleteOnClose; /* FILE_DELETE_ON_CLOSE */
/* read-only after creation */
FSP_DEVICE_GENERIC_TABLE_ELEMENT ElementStorage;
PDEVICE_OBJECT FsvolDeviceObject;
UINT64 UserContext;
UNICODE_STRING FileName;
WCHAR FileNameBuf[];
} FSP_FILE_CONTEXT;
NTSTATUS FspFileContextCreate(ULONG ExtraSize, FSP_FILE_CONTEXT **PContext);
NTSTATUS FspFileContextCreate(PDEVICE_OBJECT DeviceObject,
ULONG ExtraSize, FSP_FILE_CONTEXT **PFsContext);
VOID FspFileContextDelete(FSP_FILE_CONTEXT *Context);
static inline
VOID FspFileContextOpen(FSP_FILE_CONTEXT *Context)

View File

@ -6,7 +6,8 @@
#include <sys/driver.h>
NTSTATUS FspFileContextCreate(ULONG ExtraSize, FSP_FILE_CONTEXT **PContext);
NTSTATUS FspFileContextCreate(PDEVICE_OBJECT DeviceObject,
ULONG ExtraSize, FSP_FILE_CONTEXT **PFsContext);
VOID FspFileContextDelete(FSP_FILE_CONTEXT *Context);
#ifdef ALLOC_PRAGMA
@ -14,7 +15,8 @@ VOID FspFileContextDelete(FSP_FILE_CONTEXT *Context);
#pragma alloc_text(PAGE, FspFileContextDelete)
#endif
NTSTATUS FspFileContextCreate(ULONG ExtraSize, FSP_FILE_CONTEXT **PFsContext)
NTSTATUS FspFileContextCreate(PDEVICE_OBJECT DeviceObject,
ULONG ExtraSize, FSP_FILE_CONTEXT **PFsContext)
{
PAGED_CODE();
@ -45,6 +47,8 @@ NTSTATUS FspFileContextCreate(ULONG ExtraSize, FSP_FILE_CONTEXT **PFsContext)
FsRtlSetupAdvancedHeader(&FsContext->Header, &NonPaged->HeaderFastMutex);
FsContext->NonPaged = NonPaged;
FsContext->RefCount = 1;
FsContext->FsvolDeviceObject = DeviceObject;
FspDeviceRetain(FsContext->FsvolDeviceObject);
RtlInitEmptyUnicodeString(&FsContext->FileName, FsContext->FileNameBuf, (USHORT)ExtraSize);
*PFsContext = FsContext;
@ -58,6 +62,8 @@ VOID FspFileContextDelete(FSP_FILE_CONTEXT *FsContext)
FsRtlTeardownPerStreamContexts(&FsContext->Header);
FspDeviceRelease(FsContext->FsvolDeviceObject);
ExDeleteResourceLite(&FsContext->NonPaged->PagingIoResource);
ExDeleteResourceLite(&FsContext->NonPaged->Resource);
FspFree(FsContext->NonPaged);