Fix a previous bug and fix NtReadFile: Read FileObject Status, Set Read Operation, support IRP_NOCACHE (ReactOS doesn't like this because of some other bug i'm investigating), use right event reqeuested access, add SEH

svn path=/trunk/; revision=15158
This commit is contained in:
Alex Ionescu 2005-05-09 00:33:23 +00:00
parent dd19f592a2
commit 2ba13f06b1

View file

@ -2058,7 +2058,7 @@ NtQueryDirectoryFile(IN HANDLE FileHandle,
PIO_STACK_LOCATION StackPtr;
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
NTSTATUS Status = STATUS_SUCCESS;
BOOLEAN LocalEvent;
BOOLEAN LocalEvent = FALSE;
PKEVENT Event = NULL;
DPRINT("NtQueryDirectoryFile()\n");
@ -2438,31 +2438,51 @@ NtReadFile(IN HANDLE FileHandle,
NTSTATUS Status;
PFILE_OBJECT FileObject;
PIRP Irp = NULL;
PDEVICE_OBJECT DeviceObject;
PIO_STACK_LOCATION StackPtr;
KPROCESSOR_MODE PreviousMode;
BOOLEAN LocalEvent = FALSE;
PKEVENT EventObject = NULL;
DPRINT("NtReadFile(FileHandle %x Buffer %x Length %x ByteOffset %x, "
"IoStatusBlock %x)\n", FileHandle, Buffer, Length, ByteOffset,
IoStatusBlock);
PAGED_CODE();
if (IoStatusBlock == NULL)
return STATUS_ACCESS_VIOLATION;
#if 0
/* Validate User-Mode Buffers */
if(PreviousMode != KernelMode)
{
_SEH_TRY
{
ProbeForWrite(IoStatusBlock,
sizeof(IO_STATUS_BLOCK),
sizeof(ULONG));
ProbeForWrite(Buffer,
Length,
sizeof(ULONG));
}
_SEH_HANDLE
{
Status = _SEH_GetExceptionCode();
}
_SEH_END;
PreviousMode = ExGetPreviousMode();
if(!NT_SUCCESS(Status)) return Status;
}
#endif
/* Get File Object */
Status = ObReferenceObjectByHandle(FileHandle,
FILE_READ_DATA,
IoFileObjectType,
PreviousMode,
(PVOID*)&FileObject,
NULL);
if (!NT_SUCCESS(Status))
{
return Status;
}
if (!NT_SUCCESS(Status)) return Status;
if (ByteOffset == NULL ||
/* Check the Byte Offset */
if (!ByteOffset ||
(ByteOffset->u.LowPart == FILE_USE_FILE_POINTER_POSITION &&
ByteOffset->u.HighPart == 0xffffffff))
{
@ -2474,13 +2494,16 @@ NtReadFile(IN HANDLE FileHandle,
return STATUS_INVALID_PARAMETER;
}
/* Use the Current Byte OFfset */
ByteOffset = &FileObject->CurrentByteOffset;
}
if (Event != NULL)
/* Check for event */
if (Event)
{
/* Reference it */
Status = ObReferenceObjectByHandle(Event,
SYNCHRONIZE,
EVENT_MODIFY_STATE,
ExEventObjectType,
PreviousMode,
(PVOID*)&EventObject,
@ -2490,14 +2513,35 @@ NtReadFile(IN HANDLE FileHandle,
ObDereferenceObject(FileObject);
return Status;
}
KeClearEvent(EventObject);
}
/* Check if this is a direct open or not */
if (FileObject->Flags & FO_DIRECT_DEVICE_OPEN)
{
DeviceObject = IoGetAttachedDevice(FileObject->DeviceObject);
}
else
{
DeviceObject = IoGetRelatedDeviceObject(FileObject);
}
/* Check if we should use Sync IO or not */
if (FileObject->Flags & FO_SYNCHRONOUS_IO)
{
/* Use File Object event */
KeClearEvent(&FileObject->Event);
}
else
{
LocalEvent = TRUE;
}
/* Create the IRP */
_SEH_TRY
{
Irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ,
FileObject->DeviceObject,
DeviceObject,
Buffer,
Length,
ByteOffset,
@ -2510,12 +2554,10 @@ NtReadFile(IN HANDLE FileHandle,
}
_SEH_END;
if (!NT_SUCCESS(Status) || Irp == NULL)
/* Cleanup if IRP Allocation Failed */
if (!NT_SUCCESS(Status) || !Irp)
{
if (Event)
{
ObDereferenceObject(&EventObject);
}
if (Event) ObDereferenceObject(EventObject);
ObDereferenceObject(FileObject);
if (Irp)
{
@ -2524,37 +2566,39 @@ NtReadFile(IN HANDLE FileHandle,
return NT_SUCCESS(Status) ? STATUS_INSUFFICIENT_RESOURCES : Status;
}
KeClearEvent(&FileObject->Event);
/* Trigger FileObject/Event dereferencing */
/* Set up IRP Data */
Irp->Tail.Overlay.OriginalFileObject = FileObject;
Irp->RequestorMode = PreviousMode;
Irp->Overlay.AsynchronousParameters.UserApcRoutine = ApcRoutine;
Irp->Overlay.AsynchronousParameters.UserApcContext = ApcContext;
Irp->Flags |= IRP_READ_OPERATION;
/* FIXME: Somethign weird is going on when I enable this. */
//if (FileObject->Flags & FO_NO_INTERMEDIATE_BUFFERING) Irp->Flags |= IRP_NOCACHE;
/* Setup Stack Data */
StackPtr = IoGetNextIrpStackLocation(Irp);
StackPtr->FileObject = FileObject;
StackPtr->Parameters.Read.Key = Key ? *Key : 0;
StackPtr->Parameters.Read.Length = Length;
StackPtr->Parameters.Read.ByteOffset = *ByteOffset;
Status = IoCallDriver(FileObject->DeviceObject, Irp);
if (Status == STATUS_PENDING && (FileObject->Flags & FO_SYNCHRONOUS_IO))
/* Call the Driver */
Status = IoCallDriver(DeviceObject, Irp);
if (Status == STATUS_PENDING)
{
Status = KeWaitForSingleObject(&FileObject->Event,
if (!LocalEvent)
{
KeWaitForSingleObject(&FileObject->Event,
Executive,
PreviousMode,
FileObject->Flags & FO_ALERTABLE_IO,
NULL);
if (Status != STATUS_WAIT_0)
{
/* Wait failed. */
return Status;
}
Status = IoStatusBlock->Status;
Status = FileObject->FinalStatus;
}
}
/* Return the Status */
return Status;
}