2005-09-04 21:44:02 +00:00
|
|
|
/*
|
2007-04-18 01:25:28 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* FILE: drivers/fs/np/rw.c
|
|
|
|
* PURPOSE: Named pipe filesystem
|
|
|
|
* PROGRAMMER: David Welch <welch@cwcom.net>
|
2009-01-05 00:08:43 +00:00
|
|
|
* Michael Martin
|
2007-04-18 01:25:28 +00:00
|
|
|
*/
|
2000-03-26 22:00:10 +00:00
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
2007-04-18 01:51:27 +00:00
|
|
|
#include "npfs.h"
|
|
|
|
|
2004-05-10 19:58:10 +00:00
|
|
|
#define NDEBUG
|
2001-05-01 11:09:01 +00:00
|
|
|
#include <debug.h>
|
2000-03-26 22:00:10 +00:00
|
|
|
|
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2004-05-10 19:58:10 +00:00
|
|
|
VOID HexDump(PUCHAR Buffer, ULONG Length)
|
|
|
|
{
|
2009-02-08 09:04:42 +00:00
|
|
|
CHAR Line[65];
|
|
|
|
UCHAR ch;
|
|
|
|
const char Hex[] = "0123456789ABCDEF";
|
2011-07-05 13:55:39 +00:00
|
|
|
ULONG i, j;
|
2009-02-08 09:04:42 +00:00
|
|
|
|
|
|
|
DbgPrint("---------------\n");
|
|
|
|
|
|
|
|
for (i = 0; i < Length; i+= 16)
|
|
|
|
{
|
|
|
|
memset(Line, ' ', 64);
|
|
|
|
Line[64] = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < 16 && j + i < Length; j++)
|
|
|
|
{
|
|
|
|
ch = Buffer[i + j];
|
|
|
|
Line[3*j + 0] = Hex[ch >> 4];
|
|
|
|
Line[3*j + 1] = Hex[ch & 0x0f];
|
|
|
|
Line[48 + j] = isprint(ch) ? ch : '.';
|
|
|
|
}
|
|
|
|
DbgPrint("%s\n", Line);
|
|
|
|
}
|
|
|
|
DbgPrint("---------------\n");
|
2004-05-10 19:58:10 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 15:53:31 +00:00
|
|
|
static DRIVER_CANCEL NpfsReadWriteCancelRoutine;
|
2008-11-29 20:14:45 +00:00
|
|
|
static VOID NTAPI
|
2005-03-28 18:42:53 +00:00
|
|
|
NpfsReadWriteCancelRoutine(IN PDEVICE_OBJECT DeviceObject,
|
2009-02-08 09:04:42 +00:00
|
|
|
IN PIRP Irp)
|
2005-03-23 22:11:20 +00:00
|
|
|
{
|
2009-02-08 09:04:42 +00:00
|
|
|
PNPFS_CONTEXT Context;
|
|
|
|
PIO_STACK_LOCATION IoStack;
|
2010-11-19 22:42:53 +00:00
|
|
|
PNPFS_VCB Vcb;
|
2009-02-08 09:04:42 +00:00
|
|
|
PNPFS_CCB Ccb;
|
2010-05-30 22:18:50 +00:00
|
|
|
PLIST_ENTRY ListEntry;
|
|
|
|
PNPFS_THREAD_CONTEXT ThreadContext;
|
|
|
|
ULONG i;
|
2009-02-08 09:04:42 +00:00
|
|
|
|
|
|
|
DPRINT("NpfsReadWriteCancelRoutine(DeviceObject %p, Irp %p)\n", DeviceObject, Irp);
|
|
|
|
|
|
|
|
IoReleaseCancelSpinLock(Irp->CancelIrql);
|
|
|
|
|
|
|
|
Context = (PNPFS_CONTEXT)&Irp->Tail.Overlay.DriverContext;
|
2010-11-19 22:42:53 +00:00
|
|
|
Vcb = (PNPFS_VCB)DeviceObject->DeviceExtension;
|
2009-02-08 09:04:42 +00:00
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
Ccb = IoStack->FileObject->FsContext2;
|
|
|
|
|
2010-11-19 22:42:53 +00:00
|
|
|
KeLockMutex(&Vcb->PipeListLock);
|
2009-02-08 09:04:42 +00:00
|
|
|
ExAcquireFastMutex(&Ccb->DataListLock);
|
|
|
|
switch(IoStack->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_READ:
|
2010-11-19 22:42:53 +00:00
|
|
|
ListEntry = Vcb->ThreadListHead.Flink;
|
|
|
|
while (ListEntry != &Vcb->ThreadListHead)
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
2010-05-30 22:18:50 +00:00
|
|
|
ThreadContext = CONTAINING_RECORD(ListEntry, NPFS_THREAD_CONTEXT, ListEntry);
|
|
|
|
/* Real events start at index 1 */
|
|
|
|
for (i = 1; i < ThreadContext->Count; i++)
|
|
|
|
{
|
|
|
|
if (ThreadContext->WaitIrpArray[i] == Irp)
|
|
|
|
{
|
|
|
|
ASSERT(ThreadContext->WaitObjectArray[i] == Context->WaitEvent);
|
|
|
|
|
|
|
|
ThreadContext->WaitIrpArray[i] = NULL;
|
|
|
|
|
|
|
|
RemoveEntryList(&Context->ListEntry);
|
|
|
|
|
|
|
|
Irp->IoStatus.Status = STATUS_CANCELLED;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
|
|
|
|
|
|
|
KeSetEvent(&ThreadContext->Event, IO_NO_INCREMENT, FALSE);
|
|
|
|
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
2010-11-19 22:42:53 +00:00
|
|
|
KeUnlockMutex(&Vcb->PipeListLock);
|
2010-05-30 22:18:50 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ListEntry = ListEntry->Flink;
|
2009-02-08 09:04:42 +00:00
|
|
|
}
|
2010-05-30 22:18:50 +00:00
|
|
|
|
|
|
|
RemoveEntryList(&Context->ListEntry);
|
|
|
|
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
2010-11-19 22:42:53 +00:00
|
|
|
KeUnlockMutex(&Vcb->PipeListLock);
|
2010-05-30 22:18:50 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
Irp->IoStatus.Status = STATUS_CANCELLED;
|
|
|
|
Irp->IoStatus.Information = 0;
|
2010-05-30 22:18:50 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
2010-05-30 22:18:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(FALSE);
|
2009-02-08 09:04:42 +00:00
|
|
|
}
|
2005-03-23 22:11:20 +00:00
|
|
|
}
|
|
|
|
|
2008-11-29 20:14:45 +00:00
|
|
|
static VOID NTAPI
|
2005-03-28 18:42:53 +00:00
|
|
|
NpfsWaiterThread(PVOID InitContext)
|
2005-03-23 22:11:20 +00:00
|
|
|
{
|
2009-02-08 09:04:42 +00:00
|
|
|
PNPFS_THREAD_CONTEXT ThreadContext = (PNPFS_THREAD_CONTEXT) InitContext;
|
|
|
|
ULONG CurrentCount;
|
2010-05-30 22:18:50 +00:00
|
|
|
ULONG Count = 0, i;
|
2009-02-08 09:04:42 +00:00
|
|
|
PIRP Irp = NULL;
|
|
|
|
NTSTATUS Status;
|
|
|
|
PIO_STACK_LOCATION IoStack = NULL;
|
2010-06-01 02:44:15 +00:00
|
|
|
KIRQL OldIrql;
|
2009-02-08 09:04:42 +00:00
|
|
|
|
2010-11-19 22:42:53 +00:00
|
|
|
KeLockMutex(&ThreadContext->Vcb->PipeListLock);
|
2009-02-08 09:04:42 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
CurrentCount = ThreadContext->Count;
|
2010-11-19 22:42:53 +00:00
|
|
|
KeUnlockMutex(&ThreadContext->Vcb->PipeListLock);
|
2010-06-01 02:44:15 +00:00
|
|
|
IoAcquireCancelSpinLock(&OldIrql);
|
|
|
|
if (Irp && IoSetCancelRoutine(Irp, NULL) != NULL)
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
2010-06-01 02:44:15 +00:00
|
|
|
IoReleaseCancelSpinLock(OldIrql);
|
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
switch (IoStack->MajorFunction)
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
case IRP_MJ_READ:
|
|
|
|
NpfsRead(IoStack->DeviceObject, Irp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
}
|
2010-06-01 02:44:15 +00:00
|
|
|
else
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
2010-06-01 02:44:15 +00:00
|
|
|
IoReleaseCancelSpinLock(OldIrql);
|
2009-02-08 09:04:42 +00:00
|
|
|
}
|
|
|
|
Status = KeWaitForMultipleObjects(CurrentCount,
|
|
|
|
ThreadContext->WaitObjectArray,
|
|
|
|
WaitAny,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL,
|
|
|
|
ThreadContext->WaitBlockArray);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
2010-11-19 22:42:53 +00:00
|
|
|
KeLockMutex(&ThreadContext->Vcb->PipeListLock);
|
2010-06-01 10:57:26 +00:00
|
|
|
Count = Status - STATUS_WAIT_0;
|
2009-02-08 09:04:42 +00:00
|
|
|
ASSERT (Count < CurrentCount);
|
|
|
|
if (Count > 0)
|
|
|
|
{
|
|
|
|
Irp = ThreadContext->WaitIrpArray[Count];
|
|
|
|
ThreadContext->Count--;
|
2010-11-19 22:42:53 +00:00
|
|
|
ThreadContext->Vcb->EmptyWaiterCount++;
|
2009-02-08 09:04:42 +00:00
|
|
|
ThreadContext->WaitObjectArray[Count] = ThreadContext->WaitObjectArray[ThreadContext->Count];
|
|
|
|
ThreadContext->WaitIrpArray[Count] = ThreadContext->WaitIrpArray[ThreadContext->Count];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-30 22:18:50 +00:00
|
|
|
/* someone has add a new wait request or cancelled an old one */
|
2009-02-08 09:04:42 +00:00
|
|
|
Irp = NULL;
|
2010-05-30 22:18:50 +00:00
|
|
|
|
|
|
|
/* Look for cancelled requests */
|
|
|
|
for (i = 1; i < ThreadContext->Count; i++)
|
|
|
|
{
|
|
|
|
if (ThreadContext->WaitIrpArray[i] == NULL)
|
|
|
|
{
|
|
|
|
ThreadContext->Count--;
|
2010-11-19 22:42:53 +00:00
|
|
|
ThreadContext->Vcb->EmptyWaiterCount++;
|
2010-05-30 22:18:50 +00:00
|
|
|
ThreadContext->WaitObjectArray[i] = ThreadContext->WaitObjectArray[ThreadContext->Count];
|
|
|
|
ThreadContext->WaitIrpArray[i] = ThreadContext->WaitIrpArray[ThreadContext->Count];
|
|
|
|
}
|
|
|
|
}
|
2009-02-08 09:04:42 +00:00
|
|
|
}
|
2010-11-19 22:42:53 +00:00
|
|
|
if (ThreadContext->Count == 1 && ThreadContext->Vcb->EmptyWaiterCount >= MAXIMUM_WAIT_OBJECTS)
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
/* it exist an other thread with empty wait slots, we can remove our thread from the list */
|
|
|
|
RemoveEntryList(&ThreadContext->ListEntry);
|
2011-07-06 18:14:42 +00:00
|
|
|
ExFreePoolWithTag(ThreadContext, TAG_NPFS_THREAD_CONTEXT);
|
2010-11-19 22:42:53 +00:00
|
|
|
KeUnlockMutex(&ThreadContext->Vcb->PipeListLock);
|
2010-06-01 02:44:15 +00:00
|
|
|
break;
|
2009-02-08 09:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-23 22:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS
|
2005-03-28 18:42:53 +00:00
|
|
|
NpfsAddWaitingReadWriteRequest(IN PDEVICE_OBJECT DeviceObject,
|
2009-02-08 09:04:42 +00:00
|
|
|
IN PIRP Irp)
|
2005-03-23 22:11:20 +00:00
|
|
|
{
|
2009-02-08 09:04:42 +00:00
|
|
|
PLIST_ENTRY ListEntry;
|
|
|
|
PNPFS_THREAD_CONTEXT ThreadContext = NULL;
|
2010-11-19 22:42:53 +00:00
|
|
|
PNPFS_CONTEXT Context;
|
2009-02-08 09:04:42 +00:00
|
|
|
HANDLE hThread;
|
2010-11-19 22:42:53 +00:00
|
|
|
PNPFS_VCB Vcb;
|
2009-02-08 09:04:42 +00:00
|
|
|
KIRQL oldIrql;
|
2010-11-19 22:42:53 +00:00
|
|
|
NTSTATUS Status;
|
2009-02-08 09:04:42 +00:00
|
|
|
|
2010-11-19 22:42:53 +00:00
|
|
|
Context = (PNPFS_CONTEXT)&Irp->Tail.Overlay.DriverContext;
|
|
|
|
Vcb = (PNPFS_VCB)DeviceObject->DeviceExtension;
|
2009-02-08 09:04:42 +00:00
|
|
|
|
|
|
|
DPRINT("NpfsAddWaitingReadWriteRequest(DeviceObject %p, Irp %p)\n", DeviceObject, Irp);
|
|
|
|
|
2010-11-19 22:42:53 +00:00
|
|
|
KeLockMutex(&Vcb->PipeListLock);
|
2009-02-08 09:04:42 +00:00
|
|
|
|
2010-11-19 22:42:53 +00:00
|
|
|
ListEntry = Vcb->ThreadListHead.Flink;
|
|
|
|
while (ListEntry != &Vcb->ThreadListHead)
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
ThreadContext = CONTAINING_RECORD(ListEntry, NPFS_THREAD_CONTEXT, ListEntry);
|
|
|
|
if (ThreadContext->Count < MAXIMUM_WAIT_OBJECTS)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ListEntry = ListEntry->Flink;
|
|
|
|
}
|
2010-11-19 22:42:53 +00:00
|
|
|
|
|
|
|
if (ListEntry == &Vcb->ThreadListHead)
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
2011-07-06 18:14:42 +00:00
|
|
|
ThreadContext = ExAllocatePoolWithTag(NonPagedPool,
|
|
|
|
sizeof(NPFS_THREAD_CONTEXT),
|
|
|
|
TAG_NPFS_THREAD_CONTEXT);
|
2009-02-08 09:04:42 +00:00
|
|
|
if (ThreadContext == NULL)
|
|
|
|
{
|
2010-11-19 22:42:53 +00:00
|
|
|
KeUnlockMutex(&Vcb->PipeListLock);
|
2009-02-08 09:04:42 +00:00
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
2010-11-19 22:42:53 +00:00
|
|
|
|
|
|
|
ThreadContext->Vcb = Vcb;
|
2009-02-08 09:04:42 +00:00
|
|
|
KeInitializeEvent(&ThreadContext->Event, SynchronizationEvent, FALSE);
|
|
|
|
ThreadContext->Count = 1;
|
|
|
|
ThreadContext->WaitObjectArray[0] = &ThreadContext->Event;
|
|
|
|
|
|
|
|
DPRINT("Creating a new system thread for waiting read/write requests\n");
|
|
|
|
|
|
|
|
Status = PsCreateSystemThread(&hThread,
|
|
|
|
THREAD_ALL_ACCESS,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NpfsWaiterThread,
|
|
|
|
(PVOID)ThreadContext);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2011-07-06 18:14:42 +00:00
|
|
|
ExFreePoolWithTag(ThreadContext, TAG_NPFS_THREAD_CONTEXT);
|
2010-11-19 22:42:53 +00:00
|
|
|
KeUnlockMutex(&Vcb->PipeListLock);
|
2009-02-08 09:04:42 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2010-11-19 22:42:53 +00:00
|
|
|
|
|
|
|
InsertHeadList(&Vcb->ThreadListHead, &ThreadContext->ListEntry);
|
|
|
|
Vcb->EmptyWaiterCount += MAXIMUM_WAIT_OBJECTS - 1;
|
2009-02-08 09:04:42 +00:00
|
|
|
}
|
|
|
|
IoMarkIrpPending(Irp);
|
|
|
|
|
|
|
|
IoAcquireCancelSpinLock(&oldIrql);
|
|
|
|
if (Irp->Cancel)
|
|
|
|
{
|
|
|
|
IoReleaseCancelSpinLock(oldIrql);
|
|
|
|
Status = STATUS_CANCELLED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(void)IoSetCancelRoutine(Irp, NpfsReadWriteCancelRoutine);
|
|
|
|
IoReleaseCancelSpinLock(oldIrql);
|
|
|
|
ThreadContext->WaitObjectArray[ThreadContext->Count] = Context->WaitEvent;
|
|
|
|
ThreadContext->WaitIrpArray[ThreadContext->Count] = Irp;
|
|
|
|
ThreadContext->Count++;
|
2010-11-19 22:42:53 +00:00
|
|
|
Vcb->EmptyWaiterCount--;
|
2009-02-08 09:04:42 +00:00
|
|
|
KeSetEvent(&ThreadContext->Event, IO_NO_INCREMENT, FALSE);
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
}
|
2010-11-19 22:42:53 +00:00
|
|
|
KeUnlockMutex(&Vcb->PipeListLock);
|
2009-02-08 09:04:42 +00:00
|
|
|
return Status;
|
2005-03-23 22:11:20 +00:00
|
|
|
}
|
|
|
|
|
2008-11-29 20:14:45 +00:00
|
|
|
NTSTATUS NTAPI
|
2005-03-28 18:42:53 +00:00
|
|
|
NpfsRead(IN PDEVICE_OBJECT DeviceObject,
|
2009-02-08 09:04:42 +00:00
|
|
|
IN PIRP Irp)
|
2000-03-26 22:00:10 +00:00
|
|
|
{
|
2009-02-08 09:04:42 +00:00
|
|
|
PFILE_OBJECT FileObject;
|
|
|
|
NTSTATUS Status;
|
|
|
|
NTSTATUS OriginalStatus = STATUS_SUCCESS;
|
|
|
|
PNPFS_CCB Ccb;
|
|
|
|
PNPFS_CONTEXT Context;
|
|
|
|
KEVENT Event;
|
|
|
|
ULONG Length;
|
|
|
|
ULONG Information = 0;
|
|
|
|
ULONG CopyLength = 0;
|
|
|
|
ULONG TempLength;
|
|
|
|
BOOLEAN IsOriginalRequest = TRUE;
|
|
|
|
PVOID Buffer;
|
|
|
|
|
|
|
|
DPRINT("NpfsRead(DeviceObject %p, Irp %p)\n", DeviceObject, Irp);
|
|
|
|
|
2010-12-04 08:54:16 +00:00
|
|
|
FileObject = IoGetCurrentIrpStackLocation(Irp)->FileObject;
|
|
|
|
DPRINT("FileObject %p\n", FileObject);
|
|
|
|
DPRINT("Pipe name %wZ\n", &FileObject->FileName);
|
|
|
|
Ccb = FileObject->FsContext2;
|
|
|
|
|
|
|
|
/* Fail, if the CCB is not a pipe CCB */
|
|
|
|
if (Ccb->Type != CCB_PIPE)
|
|
|
|
{
|
|
|
|
DPRINT("Not a pipe!\n");
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
if (Irp->MdlAddress == NULL)
|
|
|
|
{
|
|
|
|
DPRINT("Irp->MdlAddress == NULL\n");
|
|
|
|
Status = STATUS_UNSUCCESSFUL;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context = (PNPFS_CONTEXT)&Irp->Tail.Overlay.DriverContext;
|
|
|
|
|
|
|
|
if ((Ccb->OtherSide) && (Ccb->OtherSide->PipeState == FILE_PIPE_DISCONNECTED_STATE) && (Ccb->PipeState == FILE_PIPE_DISCONNECTED_STATE))
|
|
|
|
{
|
|
|
|
DPRINT("Both Client and Server are disconnected!\n");
|
|
|
|
Status = STATUS_PIPE_DISCONNECTED;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((Ccb->OtherSide == NULL) && (Ccb->ReadDataAvailable == 0))
|
|
|
|
{
|
2010-05-27 23:52:32 +00:00
|
|
|
if (Ccb->PipeState == FILE_PIPE_CONNECTED_STATE)
|
2009-03-29 11:15:10 +00:00
|
|
|
Status = STATUS_PIPE_BROKEN;
|
|
|
|
else if (Ccb->PipeState == FILE_PIPE_LISTENING_STATE)
|
2009-02-08 09:04:42 +00:00
|
|
|
Status = STATUS_PIPE_LISTENING;
|
|
|
|
else if (Ccb->PipeState == FILE_PIPE_DISCONNECTED_STATE)
|
|
|
|
Status = STATUS_PIPE_DISCONNECTED;
|
|
|
|
else
|
|
|
|
Status = STATUS_UNSUCCESSFUL;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ccb->Data == NULL)
|
|
|
|
{
|
|
|
|
DPRINT("Pipe is NOT readable!\n");
|
|
|
|
Status = STATUS_UNSUCCESSFUL;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExAcquireFastMutex(&Ccb->DataListLock);
|
|
|
|
|
|
|
|
if (IoIsOperationSynchronous(Irp))
|
|
|
|
{
|
|
|
|
InsertTailList(&Ccb->ReadRequestListHead, &Context->ListEntry);
|
|
|
|
if (Ccb->ReadRequestListHead.Flink != &Context->ListEntry)
|
|
|
|
{
|
|
|
|
KeInitializeEvent(&Event, SynchronizationEvent, FALSE);
|
|
|
|
Context->WaitEvent = &Event;
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
2011-11-06 23:40:06 +00:00
|
|
|
KeWaitForSingleObject(&Event,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
2009-02-08 09:04:42 +00:00
|
|
|
NULL);
|
|
|
|
ExAcquireFastMutex(&Ccb->DataListLock);
|
|
|
|
}
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
KIRQL oldIrql;
|
|
|
|
if (IsListEmpty(&Ccb->ReadRequestListHead) ||
|
|
|
|
Ccb->ReadRequestListHead.Flink != &Context->ListEntry)
|
|
|
|
{
|
|
|
|
/* this is a new request */
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
Context->WaitEvent = &Ccb->ReadEvent;
|
|
|
|
InsertTailList(&Ccb->ReadRequestListHead, &Context->ListEntry);
|
|
|
|
if (Ccb->ReadRequestListHead.Flink != &Context->ListEntry)
|
|
|
|
{
|
|
|
|
/* there was already a request on the list */
|
|
|
|
IoAcquireCancelSpinLock(&oldIrql);
|
|
|
|
if (Irp->Cancel)
|
|
|
|
{
|
|
|
|
IoReleaseCancelSpinLock(oldIrql);
|
|
|
|
RemoveEntryList(&Context->ListEntry);
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
|
|
|
Status = STATUS_CANCELLED;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
(void)IoSetCancelRoutine(Irp, NpfsReadWriteCancelRoutine);
|
|
|
|
IoReleaseCancelSpinLock(oldIrql);
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
|
|
|
IoMarkIrpPending(Irp);
|
|
|
|
Status = STATUS_PENDING;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
|
|
|
|
Information = Irp->IoStatus.Information;
|
|
|
|
Length = IoGetCurrentIrpStackLocation(Irp)->Parameters.Read.Length;
|
|
|
|
ASSERT (Information <= Length);
|
|
|
|
Buffer = (PVOID)((ULONG_PTR)Buffer + Information);
|
|
|
|
Length -= Information;
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (Ccb->ReadDataAvailable == 0)
|
|
|
|
{
|
|
|
|
ULONG ConnectionSideReadMode;
|
|
|
|
|
|
|
|
if (Ccb->PipeEnd == FILE_PIPE_CLIENT_END) ConnectionSideReadMode=Ccb->Fcb->ClientReadMode;
|
|
|
|
else ConnectionSideReadMode = Ccb->Fcb->ServerReadMode;
|
|
|
|
|
2009-03-29 11:15:10 +00:00
|
|
|
if ((Ccb->PipeState == FILE_PIPE_CONNECTED_STATE) && (Ccb->OtherSide))
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
KeSetEvent(&Ccb->OtherSide->WriteEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
if (Information > 0 &&
|
|
|
|
(ConnectionSideReadMode != FILE_PIPE_BYTE_STREAM_MODE ||
|
|
|
|
Ccb->PipeState != FILE_PIPE_CONNECTED_STATE))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-05-27 23:52:32 +00:00
|
|
|
if (((Ccb->PipeState != FILE_PIPE_CONNECTED_STATE) || (!Ccb->OtherSide)) && (Ccb->ReadDataAvailable == 0))
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
DPRINT("PipeState: %x\n", Ccb->PipeState);
|
|
|
|
Status = STATUS_PIPE_BROKEN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
|
|
|
|
|
|
|
if (IoIsOperationSynchronous(Irp))
|
|
|
|
{
|
|
|
|
/* Wait for ReadEvent to become signaled */
|
|
|
|
|
|
|
|
DPRINT("Waiting for readable data (%wZ)\n", &Ccb->Fcb->PipeName);
|
|
|
|
Status = KeWaitForSingleObject(&Ccb->ReadEvent,
|
|
|
|
UserRequest,
|
|
|
|
Irp->RequestorMode,
|
2011-11-06 18:47:51 +00:00
|
|
|
(FileObject->Flags & FO_ALERTABLE_IO),
|
2009-02-08 09:04:42 +00:00
|
|
|
NULL);
|
|
|
|
DPRINT("Finished waiting (%wZ)! Status: %x\n", &Ccb->Fcb->PipeName, Status);
|
|
|
|
|
2011-11-16 04:39:49 +00:00
|
|
|
ExAcquireFastMutex(&Ccb->DataListLock);
|
|
|
|
|
2011-11-06 18:47:51 +00:00
|
|
|
if ((Status == STATUS_USER_APC) || (Status == STATUS_KERNEL_APC) || (Status == STATUS_ALERTED))
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
Status = STATUS_CANCELLED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Context = (PNPFS_CONTEXT)&Irp->Tail.Overlay.DriverContext;
|
|
|
|
|
|
|
|
Context->WaitEvent = &Ccb->ReadEvent;
|
|
|
|
Status = NpfsAddWaitingReadWriteRequest(DeviceObject, Irp);
|
|
|
|
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
Status = STATUS_PENDING;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
ExAcquireFastMutex(&Ccb->DataListLock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(IoGetCurrentIrpStackLocation(Irp)->FileObject != NULL);
|
|
|
|
|
|
|
|
/* If the pipe type and read mode are both byte stream */
|
|
|
|
if (Ccb->Fcb->PipeType == FILE_PIPE_BYTE_STREAM_TYPE)
|
|
|
|
{
|
|
|
|
DPRINT("Byte stream mode: Ccb->Data %x\n", Ccb->Data);
|
|
|
|
/* Byte stream mode */
|
|
|
|
while (Length > 0 && Ccb->ReadDataAvailable > 0)
|
|
|
|
{
|
|
|
|
CopyLength = min(Ccb->ReadDataAvailable, Length);
|
|
|
|
if ((ULONG_PTR)Ccb->ReadPtr + CopyLength <= (ULONG_PTR)Ccb->Data + Ccb->MaxDataLength)
|
|
|
|
{
|
|
|
|
memcpy(Buffer, Ccb->ReadPtr, CopyLength);
|
|
|
|
Ccb->ReadPtr = (PVOID)((ULONG_PTR)Ccb->ReadPtr + CopyLength);
|
|
|
|
if (Ccb->ReadPtr == (PVOID)((ULONG_PTR)Ccb->Data + Ccb->MaxDataLength))
|
|
|
|
{
|
|
|
|
Ccb->ReadPtr = Ccb->Data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TempLength = (ULONG)((ULONG_PTR)Ccb->Data + Ccb->MaxDataLength - (ULONG_PTR)Ccb->ReadPtr);
|
|
|
|
memcpy(Buffer, Ccb->ReadPtr, TempLength);
|
|
|
|
memcpy((PVOID)((ULONG_PTR)Buffer + TempLength), Ccb->Data, CopyLength - TempLength);
|
|
|
|
Ccb->ReadPtr = (PVOID)((ULONG_PTR)Ccb->Data + CopyLength - TempLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer = (PVOID)((ULONG_PTR)Buffer + CopyLength);
|
|
|
|
Length -= CopyLength;
|
|
|
|
Information += CopyLength;
|
|
|
|
|
|
|
|
Ccb->ReadDataAvailable -= CopyLength;
|
|
|
|
Ccb->WriteQuotaAvailable += CopyLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((Length == 0) || (Ccb->ReadDataAvailable == 0))
|
|
|
|
{
|
2009-03-29 11:15:10 +00:00
|
|
|
if ((Ccb->PipeState == FILE_PIPE_CONNECTED_STATE) && (Ccb->OtherSide))
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
KeSetEvent(&Ccb->OtherSide->WriteEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
KeResetEvent(&Ccb->ReadEvent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Ccb->Fcb->PipeType == FILE_PIPE_MESSAGE_TYPE)
|
|
|
|
{
|
|
|
|
DPRINT("Message mode: Ccb>Data %x\n", Ccb->Data);
|
|
|
|
|
|
|
|
/* Check if buffer is full and the read pointer is not at the start of the buffer */
|
|
|
|
if ((Ccb->WriteQuotaAvailable == 0) && (Ccb->ReadPtr > Ccb->Data))
|
|
|
|
{
|
|
|
|
Ccb->WriteQuotaAvailable += (ULONG_PTR)Ccb->ReadPtr - (ULONG_PTR)Ccb->Data;
|
|
|
|
memcpy(Ccb->Data, Ccb->ReadPtr, (ULONG_PTR)Ccb->WritePtr - (ULONG_PTR)Ccb->ReadPtr);
|
|
|
|
Ccb->WritePtr = (PVOID)((ULONG_PTR)Ccb->WritePtr - ((ULONG_PTR)Ccb->ReadPtr - (ULONG_PTR)Ccb->Data));
|
|
|
|
Ccb->ReadPtr = Ccb->Data;
|
2009-03-29 11:15:10 +00:00
|
|
|
ASSERT((ULONG_PTR)Ccb->WritePtr < ((ULONG_PTR)Ccb->Data + Ccb->MaxDataLength));
|
|
|
|
ASSERT(Ccb->WritePtr >= Ccb->Data);
|
2009-02-08 09:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For Message mode, the Message length is stored in the buffer preceeding the Message. */
|
|
|
|
if (Ccb->ReadDataAvailable)
|
|
|
|
{
|
|
|
|
ULONG NextMessageLength = 0;
|
|
|
|
|
|
|
|
/*First get the size of the message */
|
|
|
|
memcpy(&NextMessageLength, Ccb->ReadPtr, sizeof(NextMessageLength));
|
|
|
|
|
|
|
|
if ((NextMessageLength == 0) || (NextMessageLength > Ccb->ReadDataAvailable))
|
|
|
|
{
|
|
|
|
DPRINT1("Possible memory corruption.\n");
|
|
|
|
HexDump(Ccb->Data, (ULONG_PTR)Ccb->WritePtr - (ULONG_PTR)Ccb->Data);
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use the smaller value */
|
|
|
|
CopyLength = min(NextMessageLength, Length);
|
|
|
|
ASSERT(CopyLength > 0);
|
|
|
|
ASSERT(CopyLength <= Ccb->ReadDataAvailable);
|
|
|
|
/* retrieve the message from the buffer */
|
|
|
|
memcpy(Buffer, (PVOID)((ULONG_PTR)Ccb->ReadPtr + sizeof(NextMessageLength)), CopyLength);
|
|
|
|
|
|
|
|
if (Ccb->ReadDataAvailable > CopyLength)
|
|
|
|
{
|
|
|
|
if (CopyLength < NextMessageLength)
|
|
|
|
/* Client only requested part of the message */
|
|
|
|
{
|
|
|
|
/* Calculate the remaining message new size */
|
|
|
|
ULONG NewMessageSize = NextMessageLength-CopyLength;
|
|
|
|
|
|
|
|
/* Update ReadPtr to point to new Message size location */
|
|
|
|
Ccb->ReadPtr = (PVOID)((ULONG_PTR)Ccb->ReadPtr + CopyLength);
|
|
|
|
|
|
|
|
/* Write a new Message size to buffer for the part of the message still there */
|
|
|
|
memcpy(Ccb->ReadPtr, &NewMessageSize, sizeof(NewMessageSize));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Client wanted the entire message */
|
|
|
|
{
|
|
|
|
/* Update ReadPtr to point to next message size */
|
|
|
|
Ccb->ReadPtr = (PVOID)((ULONG_PTR)Ccb->ReadPtr + CopyLength + sizeof(CopyLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This was the last Message, so just zero start of buffer for safety sake */
|
|
|
|
memset(Ccb->Data, 0, NextMessageLength + sizeof(NextMessageLength));
|
|
|
|
|
|
|
|
/* Reset to MaxDataLength as partial message retrievals dont
|
|
|
|
give the length back to Quota */
|
|
|
|
Ccb->WriteQuotaAvailable = Ccb->MaxDataLength;
|
|
|
|
|
|
|
|
/* reset read and write pointer to beginning of buffer */
|
|
|
|
Ccb->WritePtr = Ccb->Data;
|
|
|
|
Ccb->ReadPtr = Ccb->Data;
|
|
|
|
}
|
2009-01-16 15:59:03 +00:00
|
|
|
#ifndef NDEBUG
|
2009-02-08 09:04:42 +00:00
|
|
|
DPRINT("Length %d Buffer %x\n",CopyLength,Buffer);
|
|
|
|
HexDump((PUCHAR)Buffer, CopyLength);
|
2009-01-16 15:59:03 +00:00
|
|
|
#endif
|
2009-01-16 14:33:45 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
Information += CopyLength;
|
|
|
|
|
|
|
|
Ccb->ReadDataAvailable -= CopyLength;
|
|
|
|
|
|
|
|
if ((ULONG)Ccb->WriteQuotaAvailable > (ULONG)Ccb->MaxDataLength) ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Information > 0)
|
|
|
|
{
|
|
|
|
ULONG ConnectionSideReadMode;
|
|
|
|
|
|
|
|
if (Ccb->PipeEnd == FILE_PIPE_CLIENT_END) ConnectionSideReadMode=Ccb->Fcb->ClientReadMode;
|
|
|
|
else ConnectionSideReadMode = Ccb->Fcb->ServerReadMode;
|
|
|
|
|
|
|
|
if ((ConnectionSideReadMode == FILE_PIPE_BYTE_STREAM_MODE) && (Ccb->ReadDataAvailable) && (Length > CopyLength))
|
|
|
|
{
|
|
|
|
Buffer = (PVOID)((ULONG_PTR)Buffer + CopyLength);
|
|
|
|
Length -= CopyLength;
|
|
|
|
}
|
2011-07-06 18:14:42 +00:00
|
|
|
else
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
KeResetEvent(&Ccb->ReadEvent);
|
|
|
|
|
2009-03-29 11:15:10 +00:00
|
|
|
if ((Ccb->PipeState == FILE_PIPE_CONNECTED_STATE) && (Ccb->WriteQuotaAvailable > 0) && (Ccb->OtherSide))
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
KeSetEvent(&Ccb->OtherSide->WriteEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINT1("Unhandled Pipe Mode!\n");
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Irp->IoStatus.Information = Information;
|
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
|
|
|
|
ASSERT(IoGetCurrentIrpStackLocation(Irp)->FileObject != NULL);
|
|
|
|
|
|
|
|
if (IoIsOperationSynchronous(Irp))
|
|
|
|
{
|
|
|
|
RemoveEntryList(&Context->ListEntry);
|
|
|
|
if (!IsListEmpty(&Ccb->ReadRequestListHead))
|
|
|
|
{
|
|
|
|
Context = CONTAINING_RECORD(Ccb->ReadRequestListHead.Flink, NPFS_CONTEXT, ListEntry);
|
|
|
|
KeSetEvent(Context->WaitEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
|
|
|
|
|
|
|
DPRINT("NpfsRead done (Status %lx)\n", Status);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
KIRQL oldIrql;
|
|
|
|
|
|
|
|
if (IsOriginalRequest)
|
|
|
|
{
|
|
|
|
IsOriginalRequest = FALSE;
|
|
|
|
OriginalStatus = Status;
|
|
|
|
}
|
|
|
|
if (Status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
|
|
|
DPRINT("NpfsRead done (Status %lx)\n", OriginalStatus);
|
|
|
|
return OriginalStatus;
|
|
|
|
}
|
|
|
|
RemoveEntryList(&Context->ListEntry);
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
|
|
|
if (IsListEmpty(&Ccb->ReadRequestListHead))
|
|
|
|
{
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
|
|
|
DPRINT("NpfsRead done (Status %lx)\n", OriginalStatus);
|
|
|
|
return OriginalStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
IoAcquireCancelSpinLock(&oldIrql);
|
|
|
|
Context = CONTAINING_RECORD(Ccb->ReadRequestListHead.Flink, NPFS_CONTEXT, ListEntry);
|
|
|
|
|
|
|
|
Irp = CONTAINING_RECORD(Context, IRP, Tail.Overlay.DriverContext);
|
|
|
|
/* Verify the Irp wasnt cancelled */
|
|
|
|
if (Irp->Cancel)
|
|
|
|
{
|
|
|
|
IoReleaseCancelSpinLock(oldIrql);
|
|
|
|
RemoveEntryList(&Context->ListEntry);
|
|
|
|
ExReleaseFastMutex(&Ccb->DataListLock);
|
|
|
|
Status = STATUS_CANCELLED;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* The Irp will now be handled, so remove the CancelRoutine */
|
|
|
|
(void)IoSetCancelRoutine(Irp, NULL);
|
|
|
|
IoReleaseCancelSpinLock(oldIrql);
|
|
|
|
}
|
|
|
|
}
|
2001-10-21 18:58:32 +00:00
|
|
|
|
2001-11-20 20:35:10 +00:00
|
|
|
done:
|
2009-02-08 09:04:42 +00:00
|
|
|
Irp->IoStatus.Status = Status;
|
2001-11-20 20:35:10 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
if (Status != STATUS_PENDING)
|
|
|
|
{
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
|
|
|
}
|
|
|
|
DPRINT("NpfsRead done (Status %lx)\n", Status);
|
2004-12-30 16:15:46 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
return Status;
|
2000-03-26 22:00:10 +00:00
|
|
|
}
|
|
|
|
|
2008-11-29 20:14:45 +00:00
|
|
|
NTSTATUS NTAPI
|
2001-05-01 11:09:01 +00:00
|
|
|
NpfsWrite(PDEVICE_OBJECT DeviceObject,
|
2009-02-08 09:04:42 +00:00
|
|
|
PIRP Irp)
|
2001-05-01 11:09:01 +00:00
|
|
|
{
|
2009-02-08 09:04:42 +00:00
|
|
|
PIO_STACK_LOCATION IoStack;
|
|
|
|
PFILE_OBJECT FileObject;
|
|
|
|
PNPFS_FCB Fcb = NULL;
|
|
|
|
PNPFS_CCB Ccb = NULL;
|
|
|
|
PNPFS_CCB ReaderCcb;
|
|
|
|
PUCHAR Buffer;
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
ULONG Length;
|
|
|
|
ULONG Offset;
|
2010-12-04 08:54:16 +00:00
|
|
|
ULONG Information = 0;
|
2009-02-08 09:04:42 +00:00
|
|
|
ULONG CopyLength;
|
|
|
|
ULONG TempLength;
|
|
|
|
|
|
|
|
DPRINT("NpfsWrite()\n");
|
|
|
|
|
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
FileObject = IoStack->FileObject;
|
|
|
|
DPRINT("FileObject %p\n", FileObject);
|
|
|
|
DPRINT("Pipe name %wZ\n", &FileObject->FileName);
|
|
|
|
|
|
|
|
Ccb = FileObject->FsContext2;
|
2010-12-04 08:54:16 +00:00
|
|
|
|
|
|
|
/* Fail, if the CCB is not a pipe CCB */
|
|
|
|
if (Ccb->Type != CCB_PIPE)
|
|
|
|
{
|
|
|
|
DPRINT("Not a pipe!\n");
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
Length = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
ReaderCcb = Ccb->OtherSide;
|
|
|
|
Fcb = Ccb->Fcb;
|
|
|
|
|
|
|
|
Length = IoStack->Parameters.Write.Length;
|
|
|
|
Offset = IoStack->Parameters.Write.ByteOffset.u.LowPart;
|
|
|
|
|
|
|
|
if (Irp->MdlAddress == NULL)
|
|
|
|
{
|
|
|
|
DPRINT("Irp->MdlAddress == NULL\n");
|
|
|
|
Status = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ReaderCcb == NULL) || (Ccb->PipeState != FILE_PIPE_CONNECTED_STATE))
|
|
|
|
{
|
|
|
|
DPRINT("Pipe is NOT connected!\n");
|
|
|
|
if (Ccb->PipeState == FILE_PIPE_LISTENING_STATE)
|
|
|
|
Status = STATUS_PIPE_LISTENING;
|
|
|
|
else if (Ccb->PipeState == FILE_PIPE_DISCONNECTED_STATE)
|
|
|
|
Status = STATUS_PIPE_DISCONNECTED;
|
|
|
|
else
|
|
|
|
Status = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReaderCcb->Data == NULL)
|
|
|
|
{
|
|
|
|
DPRINT("Pipe is NOT writable!\n");
|
|
|
|
Status = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = STATUS_SUCCESS;
|
2010-06-01 09:52:16 +00:00
|
|
|
Buffer = MmGetSystemAddressForMdlSafe (Irp->MdlAddress, NormalPagePriority);
|
|
|
|
|
|
|
|
if (!Buffer)
|
|
|
|
{
|
|
|
|
DPRINT("MmGetSystemAddressForMdlSafe failed\n");
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
Length = 0;
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
}
|
2009-02-08 09:04:42 +00:00
|
|
|
|
|
|
|
ExAcquireFastMutex(&ReaderCcb->DataListLock);
|
2009-01-16 19:30:55 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
DPRINT("Length %d Buffer %x Offset %x\n",Length,Buffer,Offset);
|
2011-09-11 15:39:19 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2009-02-08 09:04:42 +00:00
|
|
|
HexDump(Buffer, Length);
|
2007-04-18 01:25:28 +00:00
|
|
|
#endif
|
2003-06-21 19:55:55 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if ((ReaderCcb->WriteQuotaAvailable == 0))
|
|
|
|
{
|
2010-05-27 23:52:32 +00:00
|
|
|
if (Ccb->PipeState != FILE_PIPE_CONNECTED_STATE || !Ccb->OtherSide)
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
Status = STATUS_PIPE_BROKEN;
|
|
|
|
ExReleaseFastMutex(&ReaderCcb->DataListLock);
|
|
|
|
goto done;
|
|
|
|
}
|
2010-05-27 23:52:32 +00:00
|
|
|
KeSetEvent(&ReaderCcb->ReadEvent, IO_NO_INCREMENT, FALSE);
|
2009-02-08 09:04:42 +00:00
|
|
|
ExReleaseFastMutex(&ReaderCcb->DataListLock);
|
|
|
|
|
|
|
|
DPRINT("Write Waiting for buffer space (%S)\n", Fcb->PipeName.Buffer);
|
|
|
|
Status = KeWaitForSingleObject(&Ccb->WriteEvent,
|
|
|
|
UserRequest,
|
|
|
|
Irp->RequestorMode,
|
2011-11-06 18:47:51 +00:00
|
|
|
(FileObject->Flags & FO_ALERTABLE_IO),
|
2009-02-08 09:04:42 +00:00
|
|
|
NULL);
|
|
|
|
DPRINT("Write Finished waiting (%S)! Status: %x\n", Fcb->PipeName.Buffer, Status);
|
|
|
|
|
2011-11-06 18:47:51 +00:00
|
|
|
if ((Status == STATUS_USER_APC) || (Status == STATUS_KERNEL_APC) || (Status == STATUS_ALERTED))
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
Status = STATUS_CANCELLED;
|
2009-03-29 11:15:10 +00:00
|
|
|
goto done;
|
2009-02-08 09:04:42 +00:00
|
|
|
}
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* It's possible that the event was signaled because the
|
|
|
|
* other side of pipe was closed.
|
|
|
|
*/
|
2010-05-27 23:52:32 +00:00
|
|
|
if (Ccb->PipeState != FILE_PIPE_CONNECTED_STATE || !Ccb->OtherSide)
|
2009-02-08 09:04:42 +00:00
|
|
|
{
|
|
|
|
DPRINT("PipeState: %x\n", Ccb->PipeState);
|
|
|
|
Status = STATUS_PIPE_BROKEN;
|
|
|
|
goto done;
|
|
|
|
}
|
2009-04-09 21:10:04 +00:00
|
|
|
/* Check that the pipe has not been closed */
|
2010-05-27 23:52:32 +00:00
|
|
|
if (ReaderCcb->PipeState != FILE_PIPE_CONNECTED_STATE || !ReaderCcb->OtherSide)
|
2009-04-09 21:10:04 +00:00
|
|
|
{
|
|
|
|
Status = STATUS_PIPE_BROKEN;
|
|
|
|
goto done;
|
|
|
|
}
|
2009-02-08 09:04:42 +00:00
|
|
|
ExAcquireFastMutex(&ReaderCcb->DataListLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ccb->Fcb->PipeType == FILE_PIPE_BYTE_STREAM_TYPE)
|
|
|
|
{
|
|
|
|
DPRINT("Byte stream mode: Ccb->Data %x, Ccb->WritePtr %x\n", ReaderCcb->Data, ReaderCcb->WritePtr);
|
|
|
|
|
|
|
|
while (Length > 0 && ReaderCcb->WriteQuotaAvailable > 0)
|
|
|
|
{
|
|
|
|
CopyLength = min(Length, ReaderCcb->WriteQuotaAvailable);
|
|
|
|
|
|
|
|
if ((ULONG_PTR)ReaderCcb->WritePtr + CopyLength <= (ULONG_PTR)ReaderCcb->Data + ReaderCcb->MaxDataLength)
|
|
|
|
{
|
|
|
|
memcpy(ReaderCcb->WritePtr, Buffer, CopyLength);
|
|
|
|
ReaderCcb->WritePtr = (PVOID)((ULONG_PTR)ReaderCcb->WritePtr + CopyLength);
|
|
|
|
if ((ULONG_PTR)ReaderCcb->WritePtr == (ULONG_PTR)ReaderCcb->Data + ReaderCcb->MaxDataLength)
|
|
|
|
{
|
|
|
|
ReaderCcb->WritePtr = ReaderCcb->Data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
TempLength = (ULONG)((ULONG_PTR)ReaderCcb->Data + ReaderCcb->MaxDataLength -
|
|
|
|
(ULONG_PTR)ReaderCcb->WritePtr);
|
|
|
|
|
|
|
|
memcpy(ReaderCcb->WritePtr, Buffer, TempLength);
|
|
|
|
memcpy(ReaderCcb->Data, Buffer + TempLength, CopyLength - TempLength);
|
|
|
|
ReaderCcb->WritePtr = (PVOID)((ULONG_PTR)ReaderCcb->Data + CopyLength - TempLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer += CopyLength;
|
|
|
|
Length -= CopyLength;
|
|
|
|
Information += CopyLength;
|
|
|
|
|
|
|
|
ReaderCcb->ReadDataAvailable += CopyLength;
|
|
|
|
ReaderCcb->WriteQuotaAvailable -= CopyLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Length == 0)
|
|
|
|
{
|
|
|
|
KeSetEvent(&ReaderCcb->ReadEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
KeResetEvent(&Ccb->WriteEvent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Ccb->Fcb->PipeType == FILE_PIPE_MESSAGE_TYPE)
|
|
|
|
{
|
|
|
|
/* For Message Type Pipe, the Pipes memory will be used to store the size of each message */
|
|
|
|
DPRINT("Message mode: Ccb->Data %x, Ccb->WritePtr %x\n",ReaderCcb->Data, ReaderCcb->WritePtr);
|
|
|
|
if (Length > 0)
|
|
|
|
{
|
|
|
|
/* Verify the WritePtr is still inside the buffer */
|
|
|
|
if (((ULONG_PTR)ReaderCcb->WritePtr > ((ULONG_PTR)ReaderCcb->Data + (ULONG_PTR)ReaderCcb->MaxDataLength)) ||
|
|
|
|
((ULONG_PTR)ReaderCcb->WritePtr < (ULONG_PTR)ReaderCcb->Data))
|
|
|
|
{
|
|
|
|
DPRINT1("NPFS is writing out of its buffer. Report to developer!\n");
|
|
|
|
DPRINT1("ReaderCcb->WritePtr %x, ReaderCcb->Data %x, ReaderCcb->MaxDataLength %lu\n",
|
|
|
|
ReaderCcb->WritePtr, ReaderCcb->Data, ReaderCcb->MaxDataLength);
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyLength = min(Length, ReaderCcb->WriteQuotaAvailable - sizeof(ULONG));
|
|
|
|
if (CopyLength > ReaderCcb->WriteQuotaAvailable)
|
|
|
|
{
|
|
|
|
DPRINT1("Writing %lu byte to pipe would overflow as only %lu bytes are available\n",
|
|
|
|
CopyLength, ReaderCcb->WriteQuotaAvailable);
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First Copy the Length of the message into the pipes buffer */
|
|
|
|
memcpy(ReaderCcb->WritePtr, &CopyLength, sizeof(CopyLength));
|
|
|
|
|
|
|
|
/* Now the user buffer itself */
|
|
|
|
memcpy((PVOID)((ULONG_PTR)ReaderCcb->WritePtr + sizeof(CopyLength)), Buffer, CopyLength);
|
|
|
|
|
|
|
|
/* Update the write pointer */
|
|
|
|
ReaderCcb->WritePtr = (PVOID)((ULONG_PTR)ReaderCcb->WritePtr + sizeof(CopyLength) + CopyLength);
|
|
|
|
|
|
|
|
Information += CopyLength;
|
|
|
|
|
|
|
|
ReaderCcb->ReadDataAvailable += CopyLength;
|
|
|
|
|
|
|
|
ReaderCcb->WriteQuotaAvailable -= (CopyLength + sizeof(ULONG));
|
|
|
|
|
|
|
|
if ((ULONG_PTR)ReaderCcb->WriteQuotaAvailable > (ULONG)ReaderCcb->MaxDataLength)
|
|
|
|
{
|
|
|
|
DPRINT1("QuotaAvailable is greater than buffer size!\n");
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Information > 0)
|
|
|
|
{
|
|
|
|
KeSetEvent(&ReaderCcb->ReadEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
KeResetEvent(&Ccb->WriteEvent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINT1("Unhandled Pipe Type Mode and Read Write Mode!\n");
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseFastMutex(&ReaderCcb->DataListLock);
|
2001-07-29 16:41:25 +00:00
|
|
|
|
2001-11-20 20:35:10 +00:00
|
|
|
done:
|
2009-02-08 09:04:42 +00:00
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
Irp->IoStatus.Information = Information;
|
2004-12-30 16:15:46 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
2003-06-21 19:55:55 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
DPRINT("NpfsWrite done (Status %lx)\n", Status);
|
2004-12-30 16:15:46 +00:00
|
|
|
|
2009-02-08 09:04:42 +00:00
|
|
|
return Status;
|
2001-05-01 11:09:01 +00:00
|
|
|
}
|
|
|
|
|
2000-03-26 22:00:10 +00:00
|
|
|
/* EOF */
|