[NPFS/KERNEL32]
[reactos.git] / reactos / drivers / filesystems / npfs / fsctrl.c
index 5e723b4..ce68432 100644 (file)
@@ -1,7 +1,7 @@
 /*
 * COPYRIGHT:  See COPYING in the top level directory
 * PROJECT:    ReactOS kernel
-* FILE:       drivers/fs/np/fsctrl.c
+* FILE:       drivers/filesystems/npfs/fsctrl.c
 * PURPOSE:    Named pipe filesystem
 * PROGRAMMER: David Welch <welch@cwcom.net>
 *             Eric Kohl
@@ -26,6 +26,8 @@ NpfsListeningCancelRoutine(IN PDEVICE_OBJECT DeviceObject,
 {
     PNPFS_WAITER_ENTRY Waiter;
 
+    UNREFERENCED_PARAMETER(DeviceObject);
+
     Waiter = (PNPFS_WAITER_ENTRY)&Irp->Tail.Overlay.DriverContext;
 
     DPRINT("NpfsListeningCancelRoutine() called for <%wZ>\n",
@@ -57,12 +59,12 @@ NpfsAddListeningServerInstance(PIRP Irp,
 
     KeLockMutex(&Ccb->Fcb->CcbListLock);
 
-    IoMarkIrpPending(Irp);
-    InsertTailList(&Ccb->Fcb->WaiterListHead, &Entry->Entry);
-
     IoAcquireCancelSpinLock(&oldIrql);
     if (!Irp->Cancel)
     {
+        Ccb->PipeState = FILE_PIPE_LISTENING_STATE;
+        IoMarkIrpPending(Irp);
+        InsertTailList(&Ccb->Fcb->WaiterListHead, &Entry->Entry);
         (void)IoSetCancelRoutine(Irp, NpfsListeningCancelRoutine);
         IoReleaseCancelSpinLock(oldIrql);
         KeUnlockMutex(&Ccb->Fcb->CcbListLock);
@@ -92,6 +94,7 @@ NpfsConnectPipe(PIRP Irp,
     PNPFS_FCB Fcb;
     PNPFS_CCB ClientCcb;
     NTSTATUS Status;
+    KPROCESSOR_MODE WaitMode;
 
     DPRINT("NpfsConnectPipe()\n");
 
@@ -124,6 +127,7 @@ NpfsConnectPipe(PIRP Irp,
     IoStack = IoGetCurrentIrpStackLocation(Irp);
     FileObject = IoStack->FileObject;
     Flags = FileObject->Flags;
+    WaitMode = Irp->RequestorMode;
 
     /* search for a listening client fcb */
     KeLockMutex(&Fcb->CcbListLock);
@@ -175,18 +179,16 @@ NpfsConnectPipe(PIRP Irp,
     /* no listening client fcb found */
     DPRINT("No listening client fcb found -- waiting for client\n");
 
-    Ccb->PipeState = FILE_PIPE_LISTENING_STATE;
-
     Status = NpfsAddListeningServerInstance(Irp, Ccb);
 
     KeUnlockMutex(&Fcb->CcbListLock);
 
-    if (Flags & FO_SYNCHRONOUS_IO)
+    if ((Status == STATUS_PENDING) && (Flags & FO_SYNCHRONOUS_IO))
     {
         KeWaitForSingleObject(&Ccb->ConnectEvent,
             UserRequest,
-            Irp->RequestorMode,
-            FALSE,
+            WaitMode,
+            (Flags & FO_ALERTABLE_IO) != 0,
             NULL);
     }
 
@@ -287,7 +289,7 @@ NpfsDisconnectPipe(PNPFS_CCB Ccb)
             {
                 RemoveEntryList(Entry);
                 Irp = CONTAINING_RECORD(Entry, IRP, Tail.Overlay.DriverContext);
-                Complete = (NULL == IoSetCancelRoutine(Irp, NULL));
+                Complete = (NULL != IoSetCancelRoutine(Irp, NULL));
                 break;
             }
             Entry = Entry->Flink;
@@ -359,7 +361,7 @@ NpfsWaitPipe(PIRP Irp,
     /* Fail if not pipe was found */
     if (Fcb == NULL)
     {
-        DPRINT("No pipe found!\n", Fcb);
+        DPRINT("No pipe found!\n");
         return STATUS_OBJECT_NAME_NOT_FOUND;
     }
 
@@ -375,7 +377,7 @@ NpfsWaitPipe(PIRP Irp,
         {
             /* found a listening server CCB */
             DPRINT("Listening server CCB found -- connecting\n");
-
+            NpfsDereferenceFcb(Fcb);
             return STATUS_SUCCESS;
         }
 
@@ -402,12 +404,15 @@ NpfsWaitPipe(PIRP Irp,
         /* Wait forever */
         TimeOut = NULL;
     }
+    NpfsDereferenceFcb(Fcb);
 
-     Status = KeWaitForSingleObject(&Ccb->ConnectEvent,
-                                    UserRequest,
-                                    KernelMode,
-                                    TRUE,
-                                    TimeOut);
+    Status = KeWaitForSingleObject(&Ccb->ConnectEvent,
+                                   UserRequest,
+                                   Irp->RequestorMode,
+                                   (Ccb->FileObject->Flags & FO_ALERTABLE_IO) != 0,
+                                   TimeOut);
+    if ((Status == STATUS_USER_APC) || (Status == STATUS_KERNEL_APC) || (Status == STATUS_ALERTED))
+        Status = STATUS_CANCELLED;
 
     DPRINT("KeWaitForSingleObject() returned (Status %lx)\n", Status);
 
@@ -478,7 +483,7 @@ NpfsWaitPipe2(PIRP Irp,
     /* Fail if not pipe was found */
     if (Fcb == NULL)
     {
-        DPRINT("No pipe found!\n", Fcb);
+        DPRINT("No pipe found!\n");
         return STATUS_OBJECT_NAME_NOT_FOUND;
     }
 
@@ -505,7 +510,9 @@ NpfsWaitPipe2(PIRP Irp,
         {
             /* found a listening server CCB */
             DPRINT("Listening server CCB found -- connecting\n");
-
+#ifdef USING_PROPER_NPFS_WAIT_SEMANTICS
+            NpfsDereferenceFcb(Fcb);
+#endif
             return STATUS_SUCCESS;
         }
 
@@ -519,13 +526,18 @@ NpfsWaitPipe2(PIRP Irp,
         TimeOut = WaitPipe->Timeout;
     else
         TimeOut = Fcb->TimeOut;
+#ifdef USING_PROPER_NPFS_WAIT_SEMANTICS
+    NpfsDereferenceFcb(Fcb);
+#endif
 
     /* Wait for one */
     Status = KeWaitForSingleObject(&Ccb->ConnectEvent,
         UserRequest,
-        KernelMode,
-        FALSE,
+        Irp->RequestorMode,
+        (Ccb->FileObject->Flags & FO_ALERTABLE_IO) != 0,
         &TimeOut);
+    if ((Status == STATUS_USER_APC) || (Status == STATUS_KERNEL_APC) || (Status == STATUS_ALERTED))
+        Status = STATUS_CANCELLED;
 
     DPRINT("KeWaitForSingleObject() returned (Status %lx)\n", Status);
 
@@ -557,7 +569,7 @@ NpfsPeekPipe(PIRP Irp,
     ULONG OutputBufferLength;
     ULONG ReturnLength = 0;
     PFILE_PIPE_PEEK_BUFFER Reply;
-    PNPFS_FCB Fcb;
+    //PNPFS_FCB Fcb;
     PNPFS_CCB Ccb;
     NTSTATUS Status;
     ULONG MessageCount = 0;
@@ -571,15 +583,15 @@ NpfsPeekPipe(PIRP Irp,
     DPRINT("OutputBufferLength: %lu\n", OutputBufferLength);
 
     /* Validate parameters */
-    if (OutputBufferLength < sizeof(FILE_PIPE_PEEK_BUFFER))
+    if (OutputBufferLength < (ULONG)FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]))
     {
         DPRINT1("Buffer too small\n");
         return STATUS_INVALID_PARAMETER;
     }
 
     Ccb = IoStack->FileObject->FsContext2;
-    Reply = (PFILE_PIPE_PEEK_BUFFER)Irp->AssociatedIrp.SystemBuffer;
-    Fcb = Ccb->Fcb;
+    Reply = Irp->AssociatedIrp.SystemBuffer;
+    //Fcb = Ccb->Fcb;
 
 
     Reply->NamedPipeState = Ccb->PipeState;
@@ -589,52 +601,55 @@ NpfsPeekPipe(PIRP Irp,
 
     ExAcquireFastMutex(&Ccb->DataListLock);
     BufferPtr = Ccb->ReadPtr;
-    DPRINT("BufferPtr = %x\n", BufferPtr);
+    DPRINT("BufferPtr = %p\n", BufferPtr);
     if (Ccb->Fcb->PipeType == FILE_PIPE_BYTE_STREAM_TYPE)
     {
         DPRINT("Byte Stream Mode\n");
         Reply->MessageLength = Ccb->ReadDataAvailable;
-        DPRINT("Reply->MessageLength  %lu\n",Reply->MessageLength );
+        DPRINT("Reply->MessageLength  %lu\n", Reply->MessageLength);
         MessageCount = 1;
 
-        if (Reply->Data[0] && (OutputBufferLength >= Ccb->ReadDataAvailable + FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0])))
+        if (OutputBufferLength >= (ULONG)FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[Ccb->ReadDataAvailable]))
         {
+            RtlCopyMemory(Reply->Data, BufferPtr, Ccb->ReadDataAvailable);
             ReturnLength = Ccb->ReadDataAvailable;
-            memcpy(&Reply->Data[0], (PVOID)BufferPtr, Ccb->ReadDataAvailable);
         }
     }
     else
     {
         DPRINT("Message Mode\n");
-        ReadDataAvailable=Ccb->ReadDataAvailable;
+        ReadDataAvailable = Ccb->ReadDataAvailable;
 
         if (ReadDataAvailable > 0)
         {
-            memcpy(&Reply->MessageLength, BufferPtr, sizeof(ULONG));
+            RtlCopyMemory(&Reply->MessageLength,
+                          BufferPtr,
+                          sizeof(Reply->MessageLength));
 
             while ((ReadDataAvailable > 0) && (BufferPtr < Ccb->WritePtr))
             {
-                memcpy(&MessageLength, BufferPtr, sizeof(MessageLength));
+                RtlCopyMemory(&MessageLength, BufferPtr, sizeof(MessageLength));
 
                 ASSERT(MessageLength > 0);
 
-                DPRINT("MessageLength = %lu\n",MessageLength);
+                DPRINT("MessageLength = %lu\n", MessageLength);
                 ReadDataAvailable -= MessageLength;
                 MessageCount++;
 
                 /* If its the first message, copy the Message if the size of buffer is large enough */
-                if (MessageCount==1)
+                if (MessageCount == 1)
                 {
-                    if ((Reply->Data[0])
-                        && (OutputBufferLength >= (MessageLength + FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]))))
+                    if (OutputBufferLength >= (ULONG)FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[MessageLength]))
                     {
-                        memcpy(&Reply->Data[0], (PVOID)((ULONG_PTR)BufferPtr + sizeof(MessageLength)), MessageLength);
+                        RtlCopyMemory(Reply->Data,
+                                      (PVOID)((ULONG_PTR)BufferPtr + sizeof(MessageLength)),
+                                      MessageLength);
                         ReturnLength = MessageLength;
                     }
                 }
 
-                BufferPtr =(PVOID)((ULONG_PTR)BufferPtr + MessageLength + sizeof(MessageLength));
-                DPRINT("BufferPtr = %x\n", BufferPtr);
+                BufferPtr = (PVOID)((ULONG_PTR)BufferPtr + sizeof(MessageLength) + MessageLength);
+                DPRINT("BufferPtr = %p\n", BufferPtr);
                 DPRINT("ReadDataAvailable: %lu\n", ReadDataAvailable);
             }
 
@@ -649,7 +664,7 @@ NpfsPeekPipe(PIRP Irp,
 
     Reply->NumberOfMessages = MessageCount;
 
-    Irp->IoStatus.Information = ReturnLength + FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]);
+    Irp->IoStatus.Information = FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[ReturnLength]);
     Irp->IoStatus.Status = STATUS_SUCCESS;
 
     Status = STATUS_SUCCESS;
@@ -667,13 +682,13 @@ NpfsFileSystemControl(PDEVICE_OBJECT DeviceObject,
     PIO_STACK_LOCATION IoStack;
     PFILE_OBJECT FileObject;
     NTSTATUS Status;
-    PNPFS_VCB Vcb;
+    //PNPFS_VCB Vcb;
     PNPFS_FCB Fcb;
     PNPFS_CCB Ccb;
 
     DPRINT("NpfsFileSystemContol(DeviceObject %p Irp %p)\n", DeviceObject, Irp);
 
-    Vcb = (PNPFS_VCB)DeviceObject->DeviceExtension;
+    //Vcb = (PNPFS_VCB)DeviceObject->DeviceExtension;
     IoStack = IoGetCurrentIrpStackLocation(Irp);
     DPRINT("IoStack: %p\n", IoStack);
     FileObject = IoStack->FileObject;
@@ -782,6 +797,7 @@ NpfsFlushBuffers(PDEVICE_OBJECT DeviceObject,
                  PIRP Irp)
 {
     /* FIXME: Implement */
+    UNREFERENCED_PARAMETER(DeviceObject);
 
     Irp->IoStatus.Status = STATUS_SUCCESS;
     Irp->IoStatus.Information = 0;