Commit in reactos/drivers/net/afd on MAIN
makefile+2-21.13 -> 1.14
afd/lock.c+37-251.3 -> 1.4
   /main.c+3-21.7 -> 1.8
   /read.c+16-31.7 -> 1.8
   /write.c+18-11.9 -> 1.10
include/afd.h+5-21.20 -> 1.21
+81-35
6 modified files
Prevent some deadlock scenarios by unlocking the FCB while calling into 
tcpip.sys.

reactos/drivers/net/afd
makefile 1.13 -> 1.14
diff -u -r1.13 -r1.14
--- makefile	15 Aug 2004 22:51:57 -0000	1.13
+++ makefile	12 Nov 2004 07:34:55 -0000	1.14
@@ -1,4 +1,4 @@
-# $Id: makefile,v 1.13 2004/08/15 22:51:57 chorns Exp $
+# $Id: makefile,v 1.14 2004/11/12 07:34:55 arty Exp $
 
 PATH_TO_TOP = ../../..
 
@@ -11,7 +11,7 @@
 TARGET_DDKLIBS = \
 	$(PATH_TO_TOP)/dk/w32/lib/pseh.a
 
-TARGET_CFLAGS = -I./include -I$(PATH_TO_TOP)/w32api/include/ddk -I$(PATH_TO_TOP)/include/afd -DDBG -D__USE_W32API -Werror -Wall
+TARGET_CFLAGS = -I./include -I$(PATH_TO_TOP)/w32api/include/ddk -I$(PATH_TO_TOP)/include/afd -D__USE_W32API -Werror -Wall
 
 TARGET_OBJECTS = \
 	afd/bind.o \

reactos/drivers/net/afd/afd
lock.c 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- lock.c	23 Sep 2004 06:42:16 -0000	1.3
+++ lock.c	12 Nov 2004 07:34:56 -0000	1.4
@@ -1,4 +1,4 @@
-/* $Id: lock.c,v 1.3 2004/09/23 06:42:16 arty Exp $
+/* $Id: lock.c,v 1.4 2004/11/12 07:34:56 arty Exp $
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
  * FILE:             drivers/net/afd/afd/lock.c
@@ -100,7 +100,8 @@
 UINT SocketAcquireStateLock( PAFD_FCB FCB ) {
     NTSTATUS Status = STATUS_SUCCESS;
     PVOID CurrentThread = KeGetCurrentThread();
-    KIRQL CurrentIrql = KeGetCurrentIrql();
+
+    ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
 
     AFD_DbgPrint(MAX_TRACE,("Called on %x, attempting to lock\n", FCB));
 
@@ -122,37 +123,36 @@
     }
 
 
-    if( CurrentIrql == PASSIVE_LEVEL ) {
-	ExAcquireFastMutex( &FCB->Mutex );
-	while( FCB->Locked ) {
-	    AFD_DbgPrint
-		(MID_TRACE,("FCB %x is locked, waiting for notification\n",
-			    FCB));
-	    ExReleaseFastMutex( &FCB->Mutex );
-	    Status = KeWaitForSingleObject( &FCB->StateLockedEvent,
-					    UserRequest,
-					    KernelMode,
-					    FALSE,
-					    NULL );
-	    ExAcquireFastMutex( &FCB->Mutex );
-	    if( Status == STATUS_SUCCESS ) break;
-	}
-	FCB->Locked = TRUE;
-	FCB->CurrentThread = CurrentThread;
-	FCB->LockCount++;
+    ExAcquireFastMutex( &FCB->Mutex );
+
+    while( FCB->Locked ) {
+	AFD_DbgPrint
+	    (MID_TRACE,("FCB %x is locked, waiting for notification\n",
+			FCB));
 	ExReleaseFastMutex( &FCB->Mutex );
-    } else { /* Nothing since we're not at PASSIVE_LEVEL */
-	FCB->Locked = TRUE;
-	FCB->CurrentThread = CurrentThread;
-	FCB->LockCount++;
+	Status = KeWaitForSingleObject( &FCB->StateLockedEvent,
+					UserRequest,
+					KernelMode,
+					FALSE,
+					NULL );
+	ExAcquireFastMutex( &FCB->Mutex );
     }
+    FCB->Locked = TRUE;
+    FCB->CurrentThread = CurrentThread;
+    FCB->LockCount++;
+    ExReleaseFastMutex( &FCB->Mutex );
+
     AFD_DbgPrint(MAX_TRACE,("Got lock (%d).\n", FCB->LockCount));
 
     return TRUE;
 }
 
 VOID SocketStateUnlock( PAFD_FCB FCB ) {
+    PVOID CurrentThread = KeGetCurrentThread();
     ASSERT(FCB->LockCount > 0);
+    ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
+
+    ExAcquireFastMutex( &FCB->Mutex );
     FCB->LockCount--;
 
     if( !FCB->LockCount ) {
@@ -162,8 +162,10 @@
 	AFD_DbgPrint(MAX_TRACE,("Unlocked.\n"));
 	KePulseEvent( &FCB->StateLockedEvent, IO_NETWORK_INCREMENT, FALSE );
     } else {
-	AFD_DbgPrint(MID_TRACE,("Lock count %d\n", FCB->LockCount));
+	AFD_DbgPrint(MAX_TRACE,("New lock count: %d (Thr: %x)\n", 
+				FCB->LockCount, CurrentThread));
     }
+    ExReleaseFastMutex( &FCB->Mutex );
 }
 
 NTSTATUS DDKAPI UnlockAndMaybeComplete
@@ -204,3 +206,13 @@
     return UnlockAndMaybeComplete( FCB, STATUS_PENDING, Irp, 0, NULL, FALSE );
 }
 
+VOID SocketCalloutEnter( PAFD_FCB FCB ) {
+    ASSERT(FCB->Locked);
+    FCB->Critical = TRUE;
+    SocketStateUnlock( FCB );
+}
+
+VOID SocketCalloutLeave( PAFD_FCB FCB ) {
+    FCB->Critical = FALSE;
+    SocketAcquireStateLock( FCB );
+}

reactos/drivers/net/afd/afd
main.c 1.7 -> 1.8
diff -u -r1.7 -r1.8
--- main.c	23 Sep 2004 06:42:16 -0000	1.7
+++ main.c	12 Nov 2004 07:34:56 -0000	1.8
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.7 2004/09/23 06:42:16 arty Exp $
+/* $Id: main.c,v 1.8 2004/11/12 07:34:56 arty Exp $
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
  * FILE:             drivers/net/afd/afd/main.c
@@ -151,7 +151,8 @@
     InFlightRequest[2] = &FCB->SendIrp;
 
     /* Return early here because we might be called in the mean time. */
-    if( FCB->ListenIrp.InFlightRequest || 
+    if( FCB->Critical ||
+	FCB->ListenIrp.InFlightRequest || 
 	FCB->ReceiveIrp.InFlightRequest || 
 	FCB->SendIrp.InFlightRequest ) {
 	AFD_DbgPrint(MIN_TRACE,("Leaving socket alive (%x %x %x)\n",

reactos/drivers/net/afd/afd
read.c 1.7 -> 1.8
diff -u -r1.7 -r1.8
--- read.c	3 Oct 2004 20:36:45 -0000	1.7
+++ read.c	12 Nov 2004 07:34:56 -0000	1.8
@@ -1,4 +1,4 @@
-/* $Id: read.c,v 1.7 2004/10/03 20:36:45 arty Exp $
+/* $Id: read.c,v 1.8 2004/11/12 07:34:56 arty Exp $
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
  * FILE:             drivers/net/afd/afd/read.c
@@ -139,6 +139,8 @@
 	    AFD_DbgPrint(MID_TRACE,
 			 ("Exhausted our buffer.  Requesting new: %x\n", FCB));
 
+	    SocketCalloutEnter( FCB );
+	    
 	    Status = TdiReceive( &FCB->ReceiveIrp.InFlightRequest,
 				 IrpSp->FileObject,
 				 TDI_RECEIVE_NORMAL,
@@ -147,6 +149,8 @@
 				 &FCB->ReceiveIrp.Iosb,
 				 ReceiveComplete,
 				 FCB );
+
+	    SocketCalloutLeave( FCB );
 	}
     } else {
 	while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_RECV] ) ) {
@@ -204,6 +208,9 @@
 	FCB->Recv.Content = 0;
 	FCB->Recv.BytesUsed = 0;
 	AFD_DbgPrint(MID_TRACE,("Replenishing buffer\n"));
+
+	SocketCalloutEnter( FCB );
+
 	Status = TdiReceive( &FCB->ReceiveIrp.InFlightRequest,
 			     FCB->Connection.Object,
 			     TDI_RECEIVE_NORMAL,
@@ -212,6 +219,8 @@
 			     &FCB->ReceiveIrp.Iosb,
 			     ReceiveComplete,
 			     FCB );
+
+	SocketCalloutLeave( FCB );
     } else Status = STATUS_SUCCESS;
 
     if( NT_SUCCESS(Status) ) 
@@ -346,6 +355,8 @@
 
     if( NT_SUCCESS(Irp->IoStatus.Status) ) {
 	/* Now relaunch the datagram request */
+	SocketCalloutEnter( FCB );
+
 	Status = TdiReceiveDatagram
 	    ( &FCB->ReceiveIrp.InFlightRequest,
 	      FCB->AddressFile.Object,
@@ -356,6 +367,8 @@
 	      &FCB->ReceiveIrp.Iosb,
 	      PacketSocketRecvComplete,
 	      FCB );
+
+	SocketCalloutLeave( FCB );
     }
 
     SocketStateUnlock( FCB );
@@ -381,8 +394,8 @@
 	return UnlockAndMaybeComplete
 	    ( FCB, STATUS_UNSUCCESSFUL, Irp, 0, NULL, FALSE );
     if( !(RecvReq = LockRequest( Irp, IrpSp )) ) 
-	return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY, 
-				       Irp, 0, NULL, FALSE );
+	return UnlockAndMaybeComplete
+	    ( FCB, STATUS_NO_MEMORY, Irp, 0, NULL, FALSE );
 
     if( !IsListEmpty( &FCB->DatagramList ) ) {
 	ListEntry = RemoveHeadList( &FCB->DatagramList );

reactos/drivers/net/afd/afd
write.c 1.9 -> 1.10
diff -u -r1.9 -r1.10
--- write.c	3 Oct 2004 21:16:27 -0000	1.9
+++ write.c	12 Nov 2004 07:34:56 -0000	1.10
@@ -1,4 +1,4 @@
-/* $Id: write.c,v 1.9 2004/10/03 21:16:27 arty Exp $
+/* $Id: write.c,v 1.10 2004/11/12 07:34:56 arty Exp $
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
  * FILE:             drivers/net/afd/afd/write.c
@@ -109,6 +109,9 @@
     /* Some data is still waiting */
     if( FCB->Send.BytesUsed ) {
 	FCB->PollState &= ~AFD_EVENT_SEND;
+
+	SocketCalloutEnter( FCB );
+
 	Status = TdiSend( &FCB->SendIrp.InFlightRequest,
 			  IrpSp->FileObject,
 			  0,
@@ -117,6 +120,8 @@
 			  &FCB->SendIrp.Iosb,
 			  SendComplete,
 			  FCB );
+
+	SocketCalloutLeave( FCB );
     } else {
 	FCB->PollState |= AFD_EVENT_SEND;
 	PollReeval( FCB->DeviceExt, FCB->FileObject );
@@ -219,6 +224,10 @@
 	if( TotalBytesCopied > 0 ) {
 	    UnlockBuffers( SendReq->BufferArray, SendReq->BufferCount );
 
+	    FCB->SendIrp.InFlightRequest = (PVOID)1; /* Placeholder */
+
+	    SocketCalloutEnter( FCB );
+
 	    Status = TdiSend( &FCB->SendIrp.InFlightRequest,
 			      FCB->Connection.Object,
 			      0,
@@ -228,6 +237,8 @@
 			      SendComplete,
 			      FCB );
 	    
+	    SocketCalloutLeave( FCB );
+
 	    if( Status == STATUS_PENDING )
 		Status = STATUS_SUCCESS;
 	    
@@ -265,6 +276,8 @@
 	return STATUS_SUCCESS;
     }
 
+    SocketStateUnlock( FCB );
+
     return STATUS_SUCCESS;
 }
 
@@ -301,6 +314,8 @@
     /* Check the size of the Address given ... */
 
     if( TargetAddress ) {
+	SocketCalloutEnter( FCB );
+
 	Status = TdiSendDatagram
 	    ( &FCB->SendIrp.InFlightRequest,
 	      FCB->AddressFile.Object,
@@ -311,6 +326,8 @@
 	      PacketSocketSendComplete,
 	      FCB );
     
+	SocketCalloutLeave( FCB );
+
 	ExFreePool( TargetAddress );
     } else Status = STATUS_NO_MEMORY;
 

reactos/drivers/net/afd/include
afd.h 1.20 -> 1.21
diff -u -r1.20 -r1.21
--- afd.h	5 Sep 2004 04:26:30 -0000	1.20
+++ afd.h	12 Nov 2004 07:34:56 -0000	1.21
@@ -1,4 +1,4 @@
-/* $Id: afd.h,v 1.20 2004/09/05 04:26:30 arty Exp $
+/* $Id: afd.h,v 1.21 2004/11/12 07:34:56 arty Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -21,6 +21,7 @@
 #include <shared.h>
 
 #ifndef _MSC_VER
+#include <roscfg.h>
 #include <rosrtl/string.h>
 #include <winsock2.h>
 #include <ddk/tdi.h>
@@ -109,7 +110,7 @@
 } AFD_STORED_DATAGRAM, *PAFD_STORED_DATAGRAM;
 
 typedef struct _AFD_FCB {
-    BOOLEAN Locked;
+    BOOLEAN Locked, Critical;
     UINT State, Flags;
     KIRQL OldIrql;
     UINT LockCount;
@@ -182,6 +183,8 @@
 NTSTATUS LostSocket( PIRP Irp, BOOL ShouldUnlockIrp );
 PVOID LockRequest( PIRP Irp, PIO_STACK_LOCATION IrpSp );
 VOID UnlockRequest( PIRP Irp, PIO_STACK_LOCATION IrpSp );
+VOID SocketCalloutEnter( PAFD_FCB FCB );
+VOID SocketCalloutLeave( PAFD_FCB FCB );
 
 /* main.c */
 
CVSspam 0.2.8