Windows: additional AFS_SUBSYSTEM_OBJECT_REF_COUNTING
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSWorker.cpp
index 2af9c67..32cab20 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
- * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013 Your File System, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * - Redistributions of source code must retain the above copyright notice,
  *   this list of conditions and the following disclaimer.
  * - Redistributions in binary form must reproduce the above copyright
- *   notice,
- *   this list of conditions and the following disclaimer in the
- *   documentation
- *   and/or other materials provided with the distribution.
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
  * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
  *   nor the names of their contributors may be used to endorse or promote
  *   products derived from this software without specific prior written
 
 #include "AFSCommon.h"
 
+static
+VOID
+AFSPostedDeferredWrite( IN PVOID Context1,
+                        IN PVOID Context2);
+
 //
 // Function: AFSInitializeWorkerPool
 //
@@ -67,18 +70,6 @@ AFSInitializeWorkerPool()
         // Initialize the worker threads.
         //
 
-        pDevExt->Specific.Library.WorkerCount = 0;
-
-        KeInitializeEvent( &pDevExt->Specific.Library.WorkerQueueHasItems,
-                           SynchronizationEvent,
-                           FALSE);
-
-        //
-        // Initialize the queue resource
-        //
-
-        ExInitializeResourceLite( &pDevExt->Specific.Library.QueueLock);
-
         while( pDevExt->Specific.Library.WorkerCount < AFS_WORKER_COUNT)
         {
 
@@ -89,9 +80,9 @@ AFSInitializeWorkerPool()
             if( pCurrentWorker == NULL)
             {
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+                AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                               AFS_TRACE_LEVEL_ERROR,
-                              "AFSInitializeWorkerPool Failed to allocate worker context\n");
+                              "AFSInitializeWorkerPool Failed to allocate worker context\n"));
 
                 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
 
@@ -107,9 +98,10 @@ AFSInitializeWorkerPool()
             if( !NT_SUCCESS( ntStatus))
             {
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+                AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                               AFS_TRACE_LEVEL_ERROR,
-                              "AFSInitializeWorkerPool Failed to initialize worker thread Status %08lX\n", ntStatus);
+                              "AFSInitializeWorkerPool Failed to initialize worker thread Status %08lX\n",
+                              ntStatus));
 
                 ExFreePool( pCurrentWorker);
 
@@ -149,18 +141,6 @@ AFSInitializeWorkerPool()
         // Now our IO Worker queue
         //
 
-        pDevExt->Specific.Library.IOWorkerCount = 0;
-
-        KeInitializeEvent( &pDevExt->Specific.Library.IOWorkerQueueHasItems,
-                           SynchronizationEvent,
-                           FALSE);
-
-        //
-        // Initialize the queue resource
-        //
-
-        ExInitializeResourceLite( &pDevExt->Specific.Library.IOQueueLock);
-
         while( pDevExt->Specific.Library.IOWorkerCount < AFS_IO_WORKER_COUNT)
         {
 
@@ -171,9 +151,9 @@ AFSInitializeWorkerPool()
             if( pCurrentWorker == NULL)
             {
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+                AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                               AFS_TRACE_LEVEL_ERROR,
-                              "AFSInitializeWorkerPool Failed to allocate IO worker context\n");
+                              "AFSInitializeWorkerPool Failed to allocate IO worker context\n"));
 
                 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
 
@@ -189,9 +169,10 @@ AFSInitializeWorkerPool()
             if( !NT_SUCCESS( ntStatus))
             {
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+                AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                               AFS_TRACE_LEVEL_ERROR,
-                              "AFSInitializeWorkerPool Failed to initialize IO worker thread Status %08lX\n", ntStatus);
+                              "AFSInitializeWorkerPool Failed to initialize IO worker thread Status %08lX\n",
+                              ntStatus));
 
                 ExFreePool( pCurrentWorker);
 
@@ -315,12 +296,10 @@ AFSRemoveWorkerPool()
 
     pDevExt->Specific.Library.PoolHead = NULL;
 
-    ExDeleteResourceLite( &pDevExt->Specific.Library.QueueLock);
-
     //
     // Loop through the IO workers shutting them down in two stages.
     // First, clear AFS_WORKER_PROCESS_REQUESTS so that workers
-    // stop processing requests.  Second, call AFSShutdownWorkerThread()
+    // stop processing requests.  Second, call AFSShutdownIOWorkerThread()
     // to wake the workers and wait for them to exit.
     //
 
@@ -370,8 +349,6 @@ AFSRemoveWorkerPool()
 
     pDevExt->Specific.Library.IOPoolHead = NULL;
 
-    ExDeleteResourceLite( &pDevExt->Specific.Library.IOQueueLock);
-
     return ntStatus;
 }
 
@@ -380,7 +357,7 @@ AFSInitVolumeWorker( IN AFSVolumeCB *VolumeCB)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSWorkQueueContext *pWorker = &VolumeCB->VolumeWorkerContext;
+    AFSWorkQueueContext *pWorker = &VolumeCB->NonPagedVcb->VolumeWorkerContext;
     HANDLE hThread;
     AFSDeviceExt *pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
     PKSTART_ROUTINE pStartRoutine = NULL;
@@ -389,16 +366,13 @@ AFSInitVolumeWorker( IN AFSVolumeCB *VolumeCB)
     __Enter
     {
 
-        if( VolumeCB == AFSGlobalRoot)
+        if ( VolumeCB != AFSGlobalRoot)
         {
 
-            pStartRoutine = AFSPrimaryVolumeWorkerThread;
+            return STATUS_INVALID_PARAMETER;
         }
-        else
-        {
 
-            pStartRoutine = AFSVolumeWorkerThread;
-        }
+        pStartRoutine = AFSPrimaryVolumeWorkerThread;
 
         //
         // Initialize the worker thread
@@ -530,23 +504,25 @@ AFSShutdownVolumeWorker( IN AFSVolumeCB *VolumeCB)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSWorkQueueContext *pWorker = &VolumeCB->VolumeWorkerContext;
+    AFSWorkQueueContext *pWorker = &VolumeCB->NonPagedVcb->VolumeWorkerContext;
 
-    if( pWorker->WorkerThreadObject != NULL &&
-        BooleanFlagOn( pWorker->State, AFS_WORKER_INITIALIZED))
-    {
+    //
+    // Clear the 'keep processing' flag
+    //
 
-        //
-        // Clear the 'keep processing' flag
-        //
+    ClearFlag( pWorker->State, AFS_WORKER_PROCESS_REQUESTS);
 
-        ClearFlag( pWorker->State, AFS_WORKER_PROCESS_REQUESTS);
+    if( pWorker->WorkerThreadObject != NULL)
+    {
+        while ( BooleanFlagOn( pWorker->State, AFS_WORKER_INITIALIZED) )
+        {
 
-        ntStatus = KeWaitForSingleObject( pWorker->WorkerThreadObject,
-                                          Executive,
-                                          KernelMode,
-                                          FALSE,
-                                          NULL);
+            ntStatus = KeWaitForSingleObject( pWorker->WorkerThreadObject,
+                                              Executive,
+                                              KernelMode,
+                                              FALSE,
+                                              NULL);
+        }
 
         ObDereferenceObject( pWorker->WorkerThreadObject);
 
@@ -573,25 +549,28 @@ AFSShutdownWorkerThread( IN AFSWorkQueueContext *PoolContext)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
+    AFSDeviceExt *pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
 
-    if( PoolContext->WorkerThreadObject != NULL &&
-        BooleanFlagOn( PoolContext->State, AFS_WORKER_INITIALIZED))
+    if( PoolContext->WorkerThreadObject != NULL)
     {
 
-        //
-        // Wake up the thread if it is a sleep
-        //
+        while ( BooleanFlagOn( PoolContext->State, AFS_WORKER_INITIALIZED) )
+        {
 
-        KeSetEvent( &pDeviceExt->Specific.Library.WorkerQueueHasItems,
-                    0,
-                    FALSE);
+            //
+            // Wake up the thread if it is a sleep
+            //
 
-        ntStatus = KeWaitForSingleObject( PoolContext->WorkerThreadObject,
-                                          Executive,
-                                          KernelMode,
-                                          FALSE,
-                                          NULL);
+            KeSetEvent( &pControlDeviceExt->Specific.Control.WorkerQueueHasItems,
+                        0,
+                        FALSE);
+
+            ntStatus = KeWaitForSingleObject( PoolContext->WorkerThreadObject,
+                                              Executive,
+                                              KernelMode,
+                                              FALSE,
+                                              NULL);
+        }
 
         ObDereferenceObject( PoolContext->WorkerThreadObject);
 
@@ -618,25 +597,28 @@ AFSShutdownIOWorkerThread( IN AFSWorkQueueContext *PoolContext)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
+    AFSDeviceExt *pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
 
-    if( PoolContext->WorkerThreadObject != NULL &&
-        BooleanFlagOn( PoolContext->State, AFS_WORKER_INITIALIZED))
+    if( PoolContext->WorkerThreadObject != NULL)
     {
 
-        //
-        // Wake up the thread if it is a sleep
-        //
+        while ( BooleanFlagOn( PoolContext->State, AFS_WORKER_INITIALIZED) )
+        {
 
-        KeSetEvent( &pDeviceExt->Specific.Library.IOWorkerQueueHasItems,
-                    0,
-                    FALSE);
+            //
+            // Wake up the thread if it is a sleep
+            //
 
-        ntStatus = KeWaitForSingleObject( PoolContext->WorkerThreadObject,
-                                          Executive,
-                                          KernelMode,
-                                          FALSE,
-                                          NULL);
+            KeSetEvent( &pControlDeviceExt->Specific.Control.IOWorkerQueueHasItems,
+                        0,
+                        FALSE);
+
+            ntStatus = KeWaitForSingleObject( PoolContext->WorkerThreadObject,
+                                              Executive,
+                                              KernelMode,
+                                              FALSE,
+                                              NULL);
+        }
 
         ObDereferenceObject( PoolContext->WorkerThreadObject);
 
@@ -666,10 +648,10 @@ AFSWorkerThread( IN PVOID Context)
     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)Context;
     AFSWorkItem *pWorkItem;
     BOOLEAN freeWorkItem = TRUE;
-    AFSDeviceExt *pLibraryDevExt = NULL;
+    AFSDeviceExt *pControlDevExt = NULL;
     LONG lCount;
 
-    pLibraryDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
+    pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
 
     //
     // Indicate that we are initialized and ready
@@ -685,7 +667,7 @@ AFSWorkerThread( IN PVOID Context)
 
     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
 
-    ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.WorkerQueueHasItems,
+    ntStatus = KeWaitForSingleObject( &pControlDevExt->Specific.Control.WorkerQueueHasItems,
                                       Executive,
                                       KernelMode,
                                       FALSE,
@@ -697,9 +679,10 @@ AFSWorkerThread( IN PVOID Context)
         if( !NT_SUCCESS( ntStatus))
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSWorkerThread Wait for queue items failed Status %08lX\n", ntStatus);
+                          "AFSWorkerThread Wait for queue items failed Status %08lX\n",
+                          ntStatus));
 
             ntStatus = STATUS_SUCCESS;
         }
@@ -711,7 +694,7 @@ AFSWorkerThread( IN PVOID Context)
             if( pWorkItem == NULL)
             {
 
-                ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.WorkerQueueHasItems,
+                ntStatus = KeWaitForSingleObject( &pControlDevExt->Specific.Control.WorkerQueueHasItems,
                                                   Executive,
                                                   KernelMode,
                                                   FALSE,
@@ -739,7 +722,8 @@ AFSWorkerThread( IN PVOID Context)
                         {
 
                             AFSReleaseExtentsWithFlush( pWorkItem->Specific.Fcb.Fcb,
-                                                        &pWorkItem->AuthGroup);
+                                                        &pWorkItem->AuthGroup,
+                                                        FALSE);
                         }
 
                         ASSERT( pWorkItem->Specific.Fcb.Fcb->OpenReferenceCount != 0);
@@ -778,9 +762,10 @@ AFSWorkerThread( IN PVOID Context)
 
                     default:
 
-                        AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+                        AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                                       AFS_TRACE_LEVEL_ERROR,
-                                      "AFSWorkerThread Unknown request type %d\n", pWorkItem->RequestType);
+                                      "AFSWorkerThread Unknown request type %d\n",
+                                      pWorkItem->RequestType));
 
                         break;
                 }
@@ -800,7 +785,7 @@ AFSWorkerThread( IN PVOID Context)
 
     // Wake up another worker so they too can exit
 
-    KeSetEvent( &pLibraryDevExt->Specific.Library.WorkerQueueHasItems,
+    KeSetEvent( &pControlDevExt->Specific.Control.WorkerQueueHasItems,
                 0,
                 FALSE);
 
@@ -817,9 +802,9 @@ AFSIOWorkerThread( IN PVOID Context)
     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)Context;
     AFSWorkItem *pWorkItem;
     BOOLEAN freeWorkItem = TRUE;
-    AFSDeviceExt *pLibraryDevExt = NULL, *pRdrDevExt = NULL;
+    AFSDeviceExt *pControlDevExt = NULL, *pRdrDevExt = NULL;
 
-    pLibraryDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
+    pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
 
     //
     // Indicate that we are initialized and ready
@@ -836,7 +821,7 @@ AFSIOWorkerThread( IN PVOID Context)
 
     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
 
-    ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.IOWorkerQueueHasItems,
+    ntStatus = KeWaitForSingleObject( &pControlDevExt->Specific.Control.IOWorkerQueueHasItems,
                                       Executive,
                                       KernelMode,
                                       FALSE,
@@ -848,9 +833,10 @@ AFSIOWorkerThread( IN PVOID Context)
         if( !NT_SUCCESS( ntStatus))
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSIOWorkerThread Wait for queue items failed Status %08lX\n", ntStatus);
+                          "AFSIOWorkerThread Wait for queue items failed Status %08lX\n",
+                          ntStatus));
 
             ntStatus = STATUS_SUCCESS;
         }
@@ -862,7 +848,7 @@ AFSIOWorkerThread( IN PVOID Context)
             if( pWorkItem == NULL)
             {
 
-                ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.IOWorkerQueueHasItems,
+                ntStatus = KeWaitForSingleObject( &pControlDevExt->Specific.Control.IOWorkerQueueHasItems,
                                                   Executive,
                                                   KernelMode,
                                                   FALSE,
@@ -910,11 +896,25 @@ AFSIOWorkerThread( IN PVOID Context)
                         break;
                     }
 
+                    case AFS_WORK_DEFERRED_WRITE:
+                    {
+
+                        ntStatus = AFSCommonWrite( pWorkItem->Specific.AsynchIo.Device,
+                                                   pWorkItem->Specific.AsynchIo.Irp,
+                                                   pWorkItem->Specific.AsynchIo.CallingProcess,
+                                                   TRUE);
+
+                        freeWorkItem = TRUE;
+
+                        break;
+                    }
+
                     default:
 
-                        AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+                        AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                                       AFS_TRACE_LEVEL_ERROR,
-                                      "AFSWorkerThread Unknown request type %d\n", pWorkItem->RequestType);
+                                      "AFSIOWorkerThread Unknown request type %d\n",
+                                      pWorkItem->RequestType));
 
                         break;
                 }
@@ -934,7 +934,7 @@ AFSIOWorkerThread( IN PVOID Context)
 
     // Wake up another IOWorker so they too can exit
 
-    KeSetEvent( &pLibraryDevExt->Specific.Library.IOWorkerQueueHasItems,
+    KeSetEvent( &pControlDevExt->Specific.Control.IOWorkerQueueHasItems,
                 0,
                 FALSE);
 
@@ -943,686 +943,831 @@ AFSIOWorkerThread( IN PVOID Context)
     return;
 }
 
-void
-AFSPrimaryVolumeWorkerThread( IN PVOID Context)
+static BOOLEAN
+AFSExamineDirectory( IN AFSObjectInfoCB * pCurrentObject,
+                     IN AFSDirectoryCB  * pCurrentDirEntry)
 {
-
-    NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)&AFSGlobalRoot->VolumeWorkerContext;
-    AFSDeviceExt *pControlDeviceExt = NULL;
-    AFSDeviceExt *pRDRDeviceExt = NULL;
-    LARGE_INTEGER DueTime;
-    LONG TimeOut;
-    KTIMER Timer;
-    BOOLEAN bFoundOpenEntry = FALSE;
-    AFSObjectInfoCB *pCurrentObject = NULL, *pNextObject = NULL, *pCurrentChildObject = NULL;
-    AFSDirectoryCB *pCurrentDirEntry = NULL, *pNextDirEntry = NULL;
-    BOOLEAN bReleaseVolumeLock = FALSE;
-    AFSVolumeCB *pVolumeCB = NULL, *pNextVolume = NULL;
+    NTSTATUS ntStatus;
     AFSFcb *pFcb = NULL;
-    LONG lFileType;
-    LARGE_INTEGER liCurrentTime;
-    BOOLEAN bVolumeObject = FALSE;
+    AFSObjectInfoCB *pCurrentChildObject = NULL;
+    AFSVolumeCB * pVolumeCB = pCurrentObject->VolumeCB;
+    BOOLEAN bFcbBusy = FALSE;
     LONG lCount;
 
-    pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
-
-    pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
+    pCurrentChildObject = pCurrentDirEntry->ObjectInformation;
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
+    AFSDbgTrace(( AFS_SUBSYSTEM_CLEANUP_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
                   AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSPrimaryVolumeWorkerThread Initialized\n");
+                  "AFSExamineDirectory Deleting DE %wZ Object %p\n",
+                  &pCurrentDirEntry->NameInformation.FileName,
+                  pCurrentChildObject));
 
-    //
-    // Initialize the timer for the worker thread
-    //
+    AFSDeleteDirEntry( pCurrentObject,
+                       pCurrentDirEntry);
 
-    DueTime.QuadPart = -(5000);
+    if ( pCurrentChildObject != NULL)
+    {
 
-    TimeOut = 5000;
+        //
+        // Acquire ObjectInfoLock shared here so as not to deadlock
+        // with an invalidation call from the service during AFSCleanupFcb
+        //
 
-    KeInitializeTimerEx( &Timer,
-                         SynchronizationTimer);
+        lCount = AFSObjectInfoIncrement( pCurrentChildObject,
+                                         AFS_OBJECT_REFERENCE_WORKER);
 
-    KeSetTimerEx( &Timer,
-                  DueTime,
-                  TimeOut,
-                  NULL);
+        AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
+                      AFS_TRACE_LEVEL_VERBOSE,
+                      "AFSExamineDirectory Increment count on object %p Cnt %d\n",
+                      pCurrentChildObject,
+                      lCount));
 
-    //
-    // Indicate that we are initialized and ready
-    //
+        if( lCount == 1 &&
+            pCurrentChildObject->Fcb != NULL &&
+            pCurrentChildObject->FileType == AFS_FILE_TYPE_FILE)
+        {
 
-    KeSetEvent( &pPoolContext->WorkerThreadReady,
-                0,
-                FALSE);
+            //
+            // We must not hold pVolumeCB->ObjectInfoTree.TreeLock exclusive
+            // across an AFSCleanupFcb call since it can deadlock with an
+            // invalidation call from the service.
+            //
 
-    //
-    // Indicate we are initialized
-    //
+            AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
 
-    SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
+            //
+            // Cannot hold a TreeLock across an AFSCleanupFcb call
+            // as it can deadlock with an invalidation ioctl initiated
+            // from the service.
+            //
+            // Dropping the TreeLock permits the
+            // pCurrentObject->ObjectReferenceCount to change
+            //
 
-    while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
-    {
+            ntStatus = AFSCleanupFcb( pCurrentChildObject->Fcb,
+                                      TRUE);
 
-        KeWaitForSingleObject( &Timer,
-                               Executive,
-                               KernelMode,
-                               FALSE,
-                               NULL);
+            if ( ntStatus == STATUS_RETRY)
+            {
 
-        //
-        // This is the primary volume worker so it will traverse the volume list
-        // looking for cleanup or volumes requiring private workers
-        //
+                bFcbBusy = TRUE;
+            }
 
-        AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
-                          TRUE);
+            AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
+                            TRUE);
+        }
 
-        pVolumeCB = pRDRDeviceExt->Specific.RDR.VolumeListHead;
+        AFSAcquireExcl( &pCurrentChildObject->NonPagedInfo->ObjectInfoLock,
+                        TRUE);
 
-        while( pVolumeCB != NULL)
-        {
+        lCount = AFSObjectInfoDecrement( pCurrentChildObject,
+                                         AFS_OBJECT_REFERENCE_WORKER);
 
-            if( pVolumeCB == AFSGlobalRoot ||
-                !AFSAcquireExcl( pVolumeCB->VolumeLock,
-                                 FALSE))
-            {
+        AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
+                      AFS_TRACE_LEVEL_VERBOSE,
+                      "AFSExamineDirectory Decrement1 count on object %p Cnt %d\n",
+                      pCurrentChildObject,
+                      lCount));
 
-                pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
+        if( lCount == 0 &&
+            pCurrentChildObject->Fcb != NULL &&
+            pCurrentChildObject->Fcb->OpenReferenceCount == 0)
+        {
 
-                continue;
-            }
+            AFSRemoveFcb( &pCurrentChildObject->Fcb);
 
-            if( pVolumeCB->ObjectInfoListHead == NULL)
+            if( pCurrentChildObject->FileType == AFS_FILE_TYPE_DIRECTORY &&
+                pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
             {
 
-                AFSReleaseResource( pVolumeCB->VolumeLock);
-
-                AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
-
-                AFSAcquireExcl( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock,
-                                TRUE);
-
-                AFSAcquireExcl( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
+                AFSAcquireExcl( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock,
                                 TRUE);
 
-                if( !AFSAcquireExcl( pVolumeCB->VolumeLock,
-                                     FALSE))
-                {
+                AFSRemoveFcb( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
 
-                    AFSConvertToShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
+                AFSReleaseResource( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock);
 
-                    AFSReleaseResource( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock);
+                AFSDeleteObjectInfo( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
 
-                    pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
+                ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
 
-                    continue;
-                }
+                AFSExFreePoolWithTag( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged, AFS_DIR_ENTRY_NP_TAG);
 
-                KeQueryTickCount( &liCurrentTime);
+                AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_ALLOCATION,
+                              AFS_TRACE_LEVEL_VERBOSE,
+                              "AFSExamineDirectory (pioctl) AFS_DIR_ENTRY_TAG deallocating %p\n",
+                              pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB));
 
-                pNextVolume = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
+                AFSExFreePoolWithTag( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB, AFS_DIR_ENTRY_TAG);
+            }
 
-                if( pVolumeCB->ObjectInfoListHead == NULL &&
-                    pVolumeCB->DirectoryCB->OpenReferenceCount == 0 &&
-                    pVolumeCB->VolumeReferenceCount == 1 &&
-                    ( pVolumeCB->RootFcb == NULL ||
-                      pVolumeCB->RootFcb->OpenReferenceCount == 0) &&
-                    pVolumeCB->ObjectInformation.ObjectReferenceCount <= 0)
-                {
+            AFSReleaseResource( &pCurrentChildObject->NonPagedInfo->ObjectInfoLock);
 
-                    if( pVolumeCB->RootFcb != NULL)
-                    {
+            AFSDbgTrace(( AFS_SUBSYSTEM_CLEANUP_PROCESSING | AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSExamineDirectory Deleting object %p\n",
+                          pCurrentChildObject));
 
-                        AFSRemoveRootFcb( pVolumeCB->RootFcb);
-                    }
+            AFSDeleteObjectInfo( &pCurrentChildObject);
+        }
+        else
+        {
 
-                    AFSRemoveVolume( pVolumeCB);
-                }
-                else
-                {
+            AFSReleaseResource( &pCurrentChildObject->NonPagedInfo->ObjectInfoLock);
+        }
+    }
 
-                    AFSReleaseResource( pVolumeCB->VolumeLock);
-                }
+    return bFcbBusy;
+}
 
-                AFSConvertToShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
+//
+// Called with VolumeCB->ObjectInfoTree.TreeLock held shared.
+// The TreeLock will be released unless *pbReleaseVolumeLock is set to FALSE.
+//
 
-                AFSReleaseResource( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock);
+static BOOLEAN
+AFSExamineObjectInfo( IN AFSObjectInfoCB * pCurrentObject,
+                      IN BOOLEAN           bVolumeObject,
+                      IN OUT BOOLEAN     * pbReleaseVolumeLock)
+{
+    NTSTATUS ntStatus = STATUS_SUCCESS;
+    AFSDeviceExt *pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
+    AFSDeviceExt *pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
+    AFSDirectoryCB *pCurrentDirEntry = NULL, *pNextDirEntry = NULL;
+    AFSObjectInfoCB *pCurrentChildObject = NULL;
+    AFSVolumeCB * pVolumeCB = pCurrentObject->VolumeCB;
+    LARGE_INTEGER liCurrentTime;
+    BOOLEAN bFcbBusy = FALSE;
+    LONG lCount;
+    BOOLEAN bTemp;
 
-                pVolumeCB = pNextVolume;
+    switch ( pCurrentObject->FileType) {
 
-                continue;
-            }
+    case AFS_FILE_TYPE_DIRECTORY:
+        {
 
-            //
-            // Don't need this lock anymore now that we have a volume cb to work with
-            //
+            if ( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
+            {
 
-            AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
+                return FALSE;
+            }
 
             //
-            // For now we only need the volume lock shared
+            // If this object is deleted then remove it from the parent, if we can
             //
 
-            AFSConvertToShared( pVolumeCB->VolumeLock);
-
-            if( AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
-                                  FALSE))
+            if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DELETED) &&
+                pCurrentObject->ObjectReferenceCount <= 0 &&
+                ( pCurrentObject->Fcb == NULL ||
+                  pCurrentObject->Fcb->OpenReferenceCount == 0) &&
+                pCurrentObject->Specific.Directory.DirectoryNodeListHead == NULL &&
+                pCurrentObject->Specific.Directory.ChildOpenReferenceCount == 0)
             {
 
-                pCurrentObject = pVolumeCB->ObjectInfoListHead;
-
-                pNextObject = NULL;
+                AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
 
-                bReleaseVolumeLock = TRUE;
+                //
+                // Dropping the TreeLock permits the
+                // pCurrentObject->ObjectReferenceCount to change
+                //
 
-                while( pCurrentObject != NULL)
+                if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
+                                    FALSE))
                 {
 
-                    if( pCurrentObject != &pVolumeCB->ObjectInformation)
+                    AFSAcquireExcl( &pCurrentObject->NonPagedInfo->ObjectInfoLock,
+                                    TRUE);
+
+                    if ( pCurrentObject->ObjectReferenceCount <= 0)
                     {
 
-                        pNextObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
+                        AFSRemoveFcb( &pCurrentObject->Fcb);
 
-                        if( pNextObject == NULL &&
-                            pVolumeCB != AFSGlobalRoot)  // Don't free up the root of the global
+                        if( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
                         {
 
-                            pNextObject = &pVolumeCB->ObjectInformation;
-                        }
+                            AFSAcquireExcl( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock,
+                                            TRUE);
 
-                        bVolumeObject = FALSE;
-                    }
-                    else
-                    {
+                            AFSRemoveFcb( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
 
-                        pNextObject = NULL;
+                            AFSReleaseResource( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock);
 
-                        bVolumeObject = TRUE;
-                    }
+                            AFSDeleteObjectInfo( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
 
-                    if( pCurrentObject->FileType == AFS_FILE_TYPE_DIRECTORY &&
-                        !BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))  // If we are in shutdown mode skip directories
-                    {
+                            ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
 
-                        //
-                        // If this object is deleted then remove it from the parent, if we can
-                        //
+                            AFSExFreePoolWithTag( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged, AFS_DIR_ENTRY_NP_TAG);
 
-                        if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DELETED) &&
-                            pCurrentObject->ObjectReferenceCount <= 0 &&
-                            ( pCurrentObject->Fcb == NULL ||
-                              pCurrentObject->Fcb->OpenReferenceCount == 0) &&
-                            pCurrentObject->Specific.Directory.DirectoryNodeListHead == NULL &&
-                            pCurrentObject->Specific.Directory.ChildOpenReferenceCount == 0)
-                        {
+                            AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_ALLOCATION,
+                                          AFS_TRACE_LEVEL_VERBOSE,
+                                          "AFSExamineObjectInfo (pioctl) AFS_DIR_ENTRY_TAG deallocating %p\n",
+                                          pCurrentObject->Specific.Directory.PIOCtlDirectoryCB));
 
-                            AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
+                            AFSExFreePoolWithTag( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB, AFS_DIR_ENTRY_TAG);
+                        }
 
-                            //
-                            // Dropping the TreeLock permits the
-                            // pCurrentObject->ObjectReferenceCount to change
-                            //
+                        AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
 
-                            if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
-                                                FALSE))
-                            {
+                        AFSDbgTrace(( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
+                                      AFS_TRACE_LEVEL_VERBOSE,
+                                      "AFSExamineObjectInfo Deleting deleted object %p\n",
+                                      pCurrentObject));
 
-                                if ( pCurrentObject->ObjectReferenceCount <= 0)
-                                {
+                        AFSDeleteObjectInfo( &pCurrentObject);
+                    }
+                    else
+                    {
 
-                                    if( pCurrentObject->Fcb != NULL)
-                                    {
+                        AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
+                    }
 
-                                        AFSRemoveFcb( &pCurrentObject->Fcb);
-                                    }
+                    AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+                }
+                else
+                {
 
-                                    if( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
-                                    {
+                    *pbReleaseVolumeLock = FALSE;
+                }
 
-                                        if( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb != NULL)
-                                        {
+                return bFcbBusy;
+            }
 
-                                            AFSRemoveFcb( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
-                                        }
+            if ( pCurrentObject->Fcb != NULL &&
+                 pCurrentObject->Fcb->CcbListHead != NULL)
+            {
 
-                                        AFSDeleteObjectInfo( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
+                AFSCcb *pCcb;
 
-                                        ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
+                for ( pCcb = pCurrentObject->Fcb->CcbListHead;
+                      pCcb;
+                      pCcb = (AFSCcb *)pCcb->ListEntry.fLink)
+                {
 
-                                        AFSExFreePool( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged);
+                    if ( pCcb->NameArray) {
 
-                                        AFSExFreePool( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB);
-                                    }
+                        AFSDbgTrace(( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
+                                      AFS_TRACE_LEVEL_VERBOSE,
+                                      "AFSExamineObjectInfo Found Object %p Fcb %p Ccb %p\n",
+                                      pCurrentObject,
+                                      pCurrentObject->Fcb,
+                                      pCcb));
+                    }
+                }
 
-                                    AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
-                                                  AFS_TRACE_LEVEL_VERBOSE,
-                                                  "AFSPrimaryVolumeWorkerThread Deleting deleted object %08lX\n",
-                                                  pCurrentObject);
+                return bFcbBusy;
+            }
 
-                                    AFSDeleteObjectInfo( pCurrentObject);
-                                }
+            if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0 ||
+                ( pCurrentObject->Fcb != NULL &&
+                  pCurrentObject->Fcb->OpenReferenceCount > 0))
+            {
 
-                                AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+                return bFcbBusy;
+            }
 
-                                pCurrentObject = pNextObject;
+            if ( pCurrentObject->FileType != AFS_FILE_TYPE_DIRECTORY ||
+                 pCurrentObject->Specific.Directory.DirectoryNodeListHead != NULL)
+            {
 
-                                continue;
-                            }
-                            else
-                            {
+                if( !AFSAcquireShared( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
+                                       FALSE))
+                {
 
-                                bReleaseVolumeLock = FALSE;
+                    return bFcbBusy;
+                }
 
-                                break;
-                            }
-                        }
+                pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
 
-                        if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0 ||
-                            ( pCurrentObject->Fcb != NULL &&
-                              pCurrentObject->Fcb->OpenReferenceCount > 0) ||
-                            pCurrentObject->Specific.Directory.DirectoryNodeListHead == NULL)
-                        {
+                //
+                // Directory Entry Processing
+                //
 
-                            pCurrentObject = pNextObject;
+                KeQueryTickCount( &liCurrentTime);
 
-                            continue;
-                        }
+                while( pCurrentDirEntry != NULL)
+                {
 
-                        if( !AFSAcquireShared( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
-                                               FALSE))
-                        {
+                    if( pCurrentDirEntry->DirOpenReferenceCount > 0)
+                    {
 
-                            pCurrentObject = pNextObject;
+                        break;
+                    }
+
+                    if ( pCurrentDirEntry->NameArrayReferenceCount > 0)
+                    {
 
-                            continue;
+                        break;
+                    }
+
+                    if ( pCurrentDirEntry->ObjectInformation != NULL)
+                    {
+
+                        if ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
+                             pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0)
+                        {
+
+                            break;
                         }
 
-                        KeQueryTickCount( &liCurrentTime);
+                        if ( liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
+                             liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
+                             pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart)
+                        {
 
-                        pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
+                            break;
+                        }
 
-                        while( pCurrentDirEntry != NULL)
+                        if ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY)
                         {
 
-                            if( pCurrentDirEntry->OpenReferenceCount > 0 ||
-                                ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
-                                  pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0) ||
-                                liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
-                                liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
-                                                                        pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart ||
-                                ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
-                                   ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL ||
-                                     pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)) ||
-                                ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE &&
-                                  pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
-                                  pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0))
+                            if ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL)
                             {
 
                                 break;
                             }
 
-                            pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
+                            if ( pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)
+                            {
+
+                                break;
+                            }
                         }
 
-                        if( pCurrentDirEntry != NULL)
+                        if ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE)
                         {
 
-                            AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
-
-                            pCurrentObject = pNextObject;
+                            if ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
+                                 pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0)
+                            {
 
-                            continue;
+                                break;
+                            }
                         }
+                    }
+
+                    pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
+                }
+
+                if( pCurrentDirEntry != NULL)
+                {
+
+                    AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
+
+                    return bFcbBusy;
+                }
+
+                AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
+
+                AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
+
+                //
+                // Now acquire the locks excl without deadlocking
+                //
+
+                if( AFSAcquireExcl( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
+                                    FALSE))
+                {
+
+                    if( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
+                                         FALSE))
+                    {
 
                         AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
 
-                        AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
+                        *pbReleaseVolumeLock = FALSE;
 
-                        //
-                        // Now acquire the locks excl
-                        //
+                        return bFcbBusy;
+                    }
+
+                    if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0)
+                    {
+
+                        AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
 
-                        if( AFSAcquireExcl( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
-                                            FALSE))
+                        AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+
+                        return bFcbBusy;
+                    }
+
+                    KeQueryTickCount( &liCurrentTime);
+
+                    pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
+
+                    while( pCurrentDirEntry != NULL)
+                    {
+
+                        if( pCurrentDirEntry->DirOpenReferenceCount > 0)
                         {
 
-                            if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
-                                                FALSE))
-                            {
+                            break;
+                        }
 
-                                if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0)
-                                {
+                        if ( pCurrentDirEntry->NameArrayReferenceCount > 0)
+                        {
 
-                                    AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
+                            break;
+                        }
 
-                                    AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+                        if ( pCurrentDirEntry->ObjectInformation != NULL)
+                        {
 
-                                    pCurrentObject = pNextObject;
+                            if ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
+                                 pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0)
+                            {
 
-                                    continue;
-                                }
+                                break;
+                            }
+
+                            if ( liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
+                                 liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
+                                 pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart)
+                            {
 
-                                KeQueryTickCount( &liCurrentTime);
+                                break;
+                            }
 
-                                pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
+                            if ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY)
+                            {
 
-                                while( pCurrentDirEntry != NULL)
+                                if ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL)
                                 {
 
-                                    if( pCurrentDirEntry->OpenReferenceCount > 0 ||
-                                        ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
-                                          pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0) ||
-                                        liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
-                                        liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
-                                                                                pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart ||
-                                        ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
-                                          ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL ||
-                                            pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)) ||
-                                        ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE &&
-                                          pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
-                                          pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0))
-                                    {
-
-                                        break;
-                                    }
-
-                                    pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
+                                    break;
                                 }
 
-                                if( pCurrentDirEntry != NULL)
+                                if ( pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)
                                 {
 
-                                    AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
+                                    break;
+                                }
+                            }
 
-                                    AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+                            if ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE)
+                            {
 
-                                    pCurrentObject = pNextObject;
+                                if ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
+                                     pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0)
+                                {
 
-                                    continue;
+                                    break;
                                 }
+                            }
+                        }
 
-                                pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
+                        pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
+                    }
 
-                                while( pCurrentDirEntry != NULL)
-                                {
+                    if( pCurrentDirEntry != NULL)
+                    {
+
+                        AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
 
-                                    pNextDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
+                        AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
 
-                                    pCurrentChildObject = pCurrentDirEntry->ObjectInformation;
+                        return bFcbBusy;
+                    }
 
-                                    pFcb = NULL;
+                    pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
 
-                                    AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
-                                                  AFS_TRACE_LEVEL_VERBOSE,
-                                                  "AFSPrimaryVolumeWorkerThread Deleting DE %wZ Object %08lX\n",
-                                                  &pCurrentDirEntry->NameInformation.FileName,
-                                                  pCurrentChildObject);
+                    while( pCurrentDirEntry != NULL)
+                    {
 
-                                    AFSDeleteDirEntry( pCurrentObject,
-                                                       pCurrentDirEntry);
+                        pNextDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
 
-                                    if( pCurrentChildObject->ObjectReferenceCount <= 0 &&
-                                        pCurrentChildObject->Fcb != NULL &&
-                                        pCurrentChildObject->FileType == AFS_FILE_TYPE_FILE)
-                                    {
+                        AFSExamineDirectory( pCurrentObject,
+                                             pCurrentDirEntry);
 
-                                        //
-                                        // We must not hold pVolumeCB->ObjectInfoTree.TreeLock exclusive
-                                        // across an AFSCleanupFcb call since it can deadlock with an
-                                        // invalidation call from the service.
-                                        //
+                        pCurrentDirEntry = pNextDirEntry;
+                    }
 
-                                        AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
+                    //
+                    // Clear our enumerated flag on this object so we retrieve info again on next access
+                    //
 
-                                        //
-                                        // Dropping the TreeLock permits the
-                                        // pCurrentObject->ObjectReferenceCount to change
-                                        //
+                    ClearFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
 
-                                        AFSCleanupFcb( pCurrentChildObject->Fcb,
-                                                       TRUE);
+                    AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
+
+                    AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+                }
+                else
+                {
 
-                                        AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
-                                                        TRUE);
-                                    }
+                    //
+                    // Try to grab the volume lock again ... no problem if we don't
+                    //
 
-                                    if( pCurrentChildObject->ObjectReferenceCount <= 0 &&
-                                        ( pCurrentChildObject->Fcb == NULL ||
-                                          pCurrentChildObject->Fcb->OpenReferenceCount == 0 &&
-                                          pCurrentChildObject->Fcb->Specific.File.ExtentCount == 0))
-                                    {
+                    if( !AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
+                                           FALSE))
+                    {
 
-                                        if( pCurrentChildObject->Fcb != NULL)
-                                        {
-                                        
-                                            AFSRemoveFcb( &pCurrentChildObject->Fcb);
-                                        }
+                        *pbReleaseVolumeLock = FALSE;
 
-                                        if( pCurrentChildObject->FileType == AFS_FILE_TYPE_DIRECTORY &&
-                                            pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
-                                        {
+                        return bFcbBusy;
+                    }
+                }
+            }
+            else if ( bVolumeObject == FALSE)
+            {
+                //
+                // No children
+                //
 
-                                            if( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb != NULL)
-                                            {
+                AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
 
-                                                AFSRemoveFcb( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
-                                            }
+                if ( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
+                                      FALSE))
+                {
 
-                                            AFSDeleteObjectInfo( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
+                    *pbReleaseVolumeLock = FALSE;
 
-                                            ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
+                    return bFcbBusy;
+                }
 
-                                            AFSExFreePool( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged);
+                AFSAcquireExcl( &pCurrentObject->NonPagedInfo->ObjectInfoLock,
+                                TRUE);
 
-                                            AFSExFreePool( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB);
-                                        }
+                KeQueryTickCount( &liCurrentTime);
 
-                                        AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
-                                                      AFS_TRACE_LEVEL_VERBOSE,
-                                                      "AFSPrimaryVolumeWorkerThread Deleting object %08lX\n",
-                                                      pCurrentChildObject);
+                if( pCurrentObject->ObjectReferenceCount <= 0 &&
+                    ( pCurrentObject->Fcb == NULL ||
+                      pCurrentObject->Fcb->OpenReferenceCount <= 0) &&
+                    liCurrentTime.QuadPart > pCurrentObject->LastAccessCount.QuadPart &&
+                    liCurrentTime.QuadPart - pCurrentObject->LastAccessCount.QuadPart >
+                    pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart)
+                {
 
-                                        AFSDeleteObjectInfo( pCurrentChildObject);
-                                    }
+                    AFSRemoveFcb( &pCurrentObject->Fcb);
 
-                                    pCurrentDirEntry = pNextDirEntry;
+                    AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
 
-                                }
+                    AFSDeleteObjectInfo( &pCurrentObject);
+                }
+                else
+                {
 
-                                pCurrentObject->Specific.Directory.DirectoryNodeListHead = NULL;
+                    AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
+                }
 
-                                pCurrentObject->Specific.Directory.DirectoryNodeListTail = NULL;
+                AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+            }
+        }
+        break;
 
-                                pCurrentObject->Specific.Directory.ShortNameTree = NULL;
+    case AFS_FILE_TYPE_FILE:
+        {
 
-                                pCurrentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead = NULL;
+            lCount = AFSObjectInfoIncrement( pCurrentObject,
+                                             AFS_OBJECT_REFERENCE_WORKER);
 
-                                pCurrentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead = NULL;
+            AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSExamineObjectInfo Increment3 count on object %p Cnt %d\n",
+                          pCurrentObject,
+                          lCount));
 
-                                pCurrentObject->Specific.Directory.DirectoryNodeCount = 0;
+            AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
 
-                                AFSDbgLogMsg( AFS_SUBSYSTEM_DIR_NODE_COUNT,
-                                              AFS_TRACE_LEVEL_VERBOSE,
-                                              "AFSPrimaryVolumeWorkerThread Reset count to 0 on parent FID %08lX-%08lX-%08lX-%08lX\n",
-                                              pCurrentObject->FileId.Cell,
-                                              pCurrentObject->FileId.Volume,
-                                              pCurrentObject->FileId.Vnode,
-                                              pCurrentObject->FileId.Unique);
+            if( pCurrentObject->Fcb != NULL)
+            {
 
-                                //
-                                // Clear our enumerated flag on this object so we retrieve info again on next access
-                                //
+                //
+                // Dropping the TreeLock permits the
+                // pCurrentObject->ObjectReferenceCount to change
+                //
 
-                                ClearFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
+                ntStatus = AFSCleanupFcb( pCurrentObject->Fcb,
+                                          FALSE);
 
-                                AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
+                if ( ntStatus == STATUS_RETRY)
+                {
 
-                                AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
-                            }
-                            else
-                            {
+                    bFcbBusy = TRUE;
+                }
+            }
 
-                                AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
+            bTemp = AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
+                                    FALSE);
 
-                                bReleaseVolumeLock = FALSE;
+            lCount = AFSObjectInfoDecrement( pCurrentObject,
+                                             AFS_OBJECT_REFERENCE_WORKER);
 
-                                break;
-                            }
-                        }
-                        else
-                        {
+            AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSExamineObjectInfo Decrement3 count on object %p Cnt %d\n",
+                          pCurrentObject,
+                          lCount));
 
-                            //
-                            // Try to grab the volume lock again ... no problem if we don't
-                            //
+            if ( bTemp == FALSE)
+            {
 
-                            if( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
-                                                 FALSE))
-                            {
+                *pbReleaseVolumeLock = FALSE;
 
-                                bReleaseVolumeLock = FALSE;
+                return bFcbBusy;
+            }
 
-                                break;
-                            }
-                        }
+            AFSAcquireExcl( &pCurrentObject->NonPagedInfo->ObjectInfoLock,
+                            TRUE);
 
-                        if( pCurrentObject != &pVolumeCB->ObjectInformation)
-                        {
+            KeQueryTickCount( &liCurrentTime);
 
-                            pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
+            if( pCurrentObject->ObjectReferenceCount <= 0 &&
+                ( pCurrentObject->Fcb == NULL ||
+                  ( pCurrentObject->Fcb->OpenReferenceCount <= 0 &&
+                    pCurrentObject->Fcb->Specific.File.ExtentsDirtyCount <= 0)) &&
+                liCurrentTime.QuadPart > pCurrentObject->LastAccessCount.QuadPart &&
+                liCurrentTime.QuadPart - pCurrentObject->LastAccessCount.QuadPart >
+                pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart)
+            {
 
-                            if( pCurrentObject == NULL &&
-                                pVolumeCB != AFSGlobalRoot)
-                            {
+                AFSRemoveFcb( &pCurrentObject->Fcb);
 
-                                pCurrentObject = &pVolumeCB->ObjectInformation;
-                            }
-                        }
-                        else
-                        {
+                AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
 
-                            pCurrentObject = NULL;
-                        }
+                AFSDeleteObjectInfo( &pCurrentObject);
+            }
+            else
+            {
 
-                        continue;
-                    }
-                    else if( pCurrentObject->FileType == AFS_FILE_TYPE_FILE)
-                    {
+                AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
+            }
 
-                        AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
+            AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+        }
+        break;
 
-                        if( pCurrentObject->Fcb != NULL)
-                        {
+    default:
+        {
 
-                            //
-                            // Dropping the TreeLock permits the
-                            // pCurrentObject->ObjectReferenceCount to change
-                            //
+            AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
 
-                            AFSCleanupFcb( pCurrentObject->Fcb,
-                                           TRUE);
-                        }
+            if ( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
+                                  FALSE))
+            {
 
-                        if( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
-                                             FALSE))
-                        {
+                *pbReleaseVolumeLock = FALSE;
 
-                            bReleaseVolumeLock = FALSE;
+                return bFcbBusy;
+            }
 
-                            break;
-                        }
+            AFSAcquireExcl( &pCurrentObject->NonPagedInfo->ObjectInfoLock,
+                            TRUE);
 
-                        if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DELETED) &&
-                            pCurrentObject->ObjectReferenceCount <= 0 &&
-                            ( pCurrentObject->Fcb == NULL ||
-                              pCurrentObject->Fcb->OpenReferenceCount == 0 &&
-                              pCurrentObject->Fcb->Specific.File.ExtentCount == 0))
-                        {
+            KeQueryTickCount( &liCurrentTime);
 
-                            if( pCurrentObject->Fcb != NULL)
-                            {
+            if( pCurrentObject->ObjectReferenceCount <= 0 &&
+                ( pCurrentObject->Fcb == NULL ||
+                  pCurrentObject->Fcb->OpenReferenceCount <= 0) &&
+                liCurrentTime.QuadPart > pCurrentObject->LastAccessCount.QuadPart &&
+                liCurrentTime.QuadPart - pCurrentObject->LastAccessCount.QuadPart >
+                pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart)
+            {
 
-                                AFSRemoveFcb( &pCurrentObject->Fcb);
-                            }
+                AFSRemoveFcb( &pCurrentObject->Fcb);
 
-                            AFSDeleteObjectInfo( pCurrentObject);
-                        }
+                AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
 
-                        AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+                AFSDeleteObjectInfo( &pCurrentObject);
+            }
+            else
+            {
 
-                        pCurrentObject = pNextObject;
+                AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
+            }
+
+            AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
+        }
+    }
+
+    return bFcbBusy;
+}
 
-                        continue;
-                    }
 
-                    pCurrentObject = pNextObject;
+static BOOLEAN
+AFSExamineVolume( IN AFSVolumeCB *pVolumeCB)
+{
+    NTSTATUS ntStatus = STATUS_SUCCESS;
+    AFSObjectInfoCB *pCurrentObject = NULL, *pNextObject = NULL;
+    BOOLEAN bReleaseVolumeLock = FALSE;
+    BOOLEAN bVolumeObject = FALSE;
+    BOOLEAN bFcbBusy = FALSE;
+    LONG lCount;
+
+    if( AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
+                          FALSE))
+    {
+
+        bReleaseVolumeLock = TRUE;
+
+        pCurrentObject = pVolumeCB->ObjectInfoListHead;
+
+        pNextObject = NULL;
+
+        while( pCurrentObject != NULL)
+        {
+
+            if( pCurrentObject != &pVolumeCB->ObjectInformation)
+            {
+
+                pNextObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
+
+                //
+                // If the end of the VolumeCB ObjectInfo List is reached, then
+                // the next ObjectInformationCB to examine is the one embedded within
+                // the VolumeCB itself except when the VolumeCB is the AFSGlobalRoot.
+                //
+                // bVolumeObject is used to indicate whether the embedded ObjectInfoCB
+                // is being examined.
+                //
+
+                if( pNextObject == NULL &&
+                    pVolumeCB != AFSGlobalRoot)  // Don't free up the root of the global
+                {
+
+                    pNextObject = &pVolumeCB->ObjectInformation;
                 }
 
-                if( bReleaseVolumeLock)
+                bVolumeObject = FALSE;
+
+                if ( pNextObject)
                 {
 
-                    AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
+                    lCount = AFSObjectInfoIncrement( pNextObject,
+                                                     AFS_OBJECT_REFERENCE_WORKER);
+
+                    AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
+                                  AFS_TRACE_LEVEL_VERBOSE,
+                                  "AFSExamineVolume Increment count on object %p Cnt %d\n",
+                                  pNextObject,
+                                  lCount));
                 }
             }
+            else
+            {
 
-            //
-            // Next volume cb
-            //
+                pNextObject = NULL;
 
-            AFSReleaseResource( pVolumeCB->VolumeLock);
+                bVolumeObject = TRUE;
+            }
 
-            AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
-                              TRUE);
+            bFcbBusy = AFSExamineObjectInfo( pCurrentObject, bVolumeObject, &bReleaseVolumeLock);
 
-            pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
-        }
+            if ( pNextObject)
+            {
 
-        AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
+                lCount = AFSObjectInfoDecrement( pNextObject,
+                                                 AFS_OBJECT_REFERENCE_WORKER);
 
-    } // worker thread loop
+                AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
+                              AFS_TRACE_LEVEL_VERBOSE,
+                              "AFSExamineVolume Decrement count on object %p Cnt %d\n",
+                              pNextObject,
+                              lCount));
+            }
 
-    KeCancelTimer( &Timer);
+            //
+            // If AFSExamineObjectInfo drops the VolumeLock before returning
+            // we must halt processing of the Volume's ObjectInfo list.
+            //
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
-                  AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSPrimaryVolumeWorkerThread Exiting\n");
+            if ( bReleaseVolumeLock == FALSE)
+            {
 
-    lCount = InterlockedDecrement( &pControlDeviceExt->Specific.Control.VolumeWorkerThreadCount);
+                break;
+            }
 
-    if( lCount == 0)
-    {
+            pCurrentObject = pNextObject;
+        }
 
-        KeSetEvent( &pControlDeviceExt->Specific.Control.VolumeWorkerCloseEvent,
-                    0,
-                    FALSE);
-    }
+        if( bReleaseVolumeLock)
+        {
 
-    PsTerminateSystemThread( 0);
+            AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
+        }
+    }
 
-    return;
+    return bFcbBusy;
 }
 
 void
-AFSVolumeWorkerThread( IN PVOID Context)
+AFSPrimaryVolumeWorkerThread( IN PVOID Context)
 {
 
-    NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSVolumeCB *pVolumeCB = (AFSVolumeCB * )Context;
-    AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)&pVolumeCB->VolumeWorkerContext;
-    AFSDeviceExt *pControlDeviceExt = NULL;
-    AFSDeviceExt *pRDRDeviceExt = NULL;
-    BOOLEAN exitThread = FALSE;
+    UNREFERENCED_PARAMETER(Context);
+    AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)&AFSGlobalRoot->NonPagedVcb->VolumeWorkerContext;
+    AFSDeviceExt *pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
+    AFSDeviceExt *pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
     LARGE_INTEGER DueTime;
     LONG TimeOut;
     KTIMER Timer;
+    AFSVolumeCB *pVolumeCB = NULL, *pNextVolume = NULL;
+    BOOLEAN bFcbBusy = FALSE;
     LONG lCount;
 
-    pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
-
-    pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
+    AFSDbgTrace(( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
+                  AFS_TRACE_LEVEL_VERBOSE,
+                  "AFSPrimaryVolumeWorkerThread Initialized\n"));
 
     //
     // Initialize the timer for the worker thread
@@ -1657,36 +1802,149 @@ AFSVolumeWorkerThread( IN PVOID Context)
     while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
     {
 
-        ntStatus = KeWaitForSingleObject( &Timer,
-                                          Executive,
-                                          KernelMode,
-                                          FALSE,
-                                          NULL);
-
-        if( !NT_SUCCESS( ntStatus))
+        if ( bFcbBusy == FALSE)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
-                          AFS_TRACE_LEVEL_ERROR,
-                          "AFSVolumeWorkerThread Wait for queue items failed Status %08lX\n", ntStatus);
+            KeWaitForSingleObject( &Timer,
+                                   Executive,
+                                   KernelMode,
+                                   FALSE,
+                                   NULL);
         }
         else
         {
 
-            //
-            // If we are in shutdown mode and the dirty flag is clear then get out now
-            //
+            bFcbBusy = FALSE;
+        }
+
+        //
+        // This is the primary volume worker so it will traverse the volume list
+        // looking for cleanup or volumes requiring private workers
+        //
 
-            if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
+        AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
+                          TRUE);
+
+        pVolumeCB = pRDRDeviceExt->Specific.RDR.VolumeListHead;
+
+        while( pVolumeCB != NULL)
+        {
+
+            if( pVolumeCB == AFSGlobalRoot ||
+                !AFSAcquireExcl( pVolumeCB->VolumeLock,
+                                 FALSE))
             {
 
-                break;
+                pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
+
+                continue;
             }
+
+            if( pVolumeCB->ObjectInfoListHead == NULL)
+            {
+
+                AFSReleaseResource( pVolumeCB->VolumeLock);
+
+                AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
+
+                AFSAcquireExcl( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock,
+                                TRUE);
+
+                AFSAcquireExcl( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
+                                TRUE);
+
+                if( !AFSAcquireExcl( pVolumeCB->VolumeLock,
+                                     FALSE))
+                {
+
+                    AFSConvertToShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
+
+                    AFSReleaseResource( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock);
+
+                    pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
+
+                    continue;
+                }
+
+                pNextVolume = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
+
+                AFSAcquireShared( &pVolumeCB->ObjectInformation.NonPagedInfo->ObjectInfoLock,
+                                  TRUE);
+
+                //
+                // If VolumeCB is idle, the Volume can be garbage collected
+                //
+
+                if( pVolumeCB->ObjectInfoListHead == NULL &&
+                    pVolumeCB->DirectoryCB->DirOpenReferenceCount <= 0 &&
+                    pVolumeCB->DirectoryCB->NameArrayReferenceCount <= 0 &&
+                    pVolumeCB->VolumeReferenceCount == 0 &&
+                    ( pVolumeCB->RootFcb == NULL ||
+                      pVolumeCB->RootFcb->OpenReferenceCount == 0) &&
+                    pVolumeCB->ObjectInformation.ObjectReferenceCount <= 0)
+                {
+
+                    AFSRemoveRootFcb( pVolumeCB);
+
+                    AFSReleaseResource( &pVolumeCB->ObjectInformation.NonPagedInfo->ObjectInfoLock);
+
+                    AFSRemoveVolume( pVolumeCB);
+                }
+                else
+                {
+
+                    AFSReleaseResource( &pVolumeCB->ObjectInformation.NonPagedInfo->ObjectInfoLock);
+
+                    AFSReleaseResource( pVolumeCB->VolumeLock);
+                }
+
+                AFSConvertToShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
+
+                AFSReleaseResource( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock);
+
+                pVolumeCB = pNextVolume;
+
+                continue;
+            }
+
+            //
+            // Don't need this lock anymore now that we have a volume cb to work with
+            //
+
+            AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
+
+            //
+            // For now we only need the volume lock shared
+            //
+
+            AFSConvertToShared( pVolumeCB->VolumeLock);
+
+            AFSExamineVolume( pVolumeCB);
+
+            //
+            // Next volume cb
+            //
+
+            AFSReleaseResource( pVolumeCB->VolumeLock);
+
+            AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
+                              TRUE);
+
+            pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
         }
+
+        AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
+
     } // worker thread loop
 
     KeCancelTimer( &Timer);
 
+    ClearFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
+
+    AFSDbgTrace(( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
+                  AFS_TRACE_LEVEL_VERBOSE,
+                  "AFSPrimaryVolumeWorkerThread Exiting\n"));
+
     lCount = InterlockedDecrement( &pControlDeviceExt->Specific.Control.VolumeWorkerThreadCount);
 
     if( lCount == 0)
@@ -1707,48 +1965,48 @@ AFSInsertWorkitem( IN AFSWorkItem *WorkItem)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSDeviceExt *pDevExt = NULL;
+    AFSDeviceExt *pControlDevExt = NULL;
     LONG lCount;
 
-    pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
+    pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+    AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
                   AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSInsertWorkitem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
-                  &pDevExt->Specific.Library.QueueLock,
-                  PsGetCurrentThread());
+                  "AFSInsertWorkitem Acquiring Control QueueLock lock %p EXCL %08lX\n",
+                  &pControlDevExt->Specific.Control.QueueLock,
+                  PsGetCurrentThread()));
 
-    AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
+    AFSAcquireExcl( &pControlDevExt->Specific.Control.QueueLock,
                     TRUE);
 
-    lCount = InterlockedIncrement( &pDevExt->Specific.Library.QueueItemCount);
+    lCount = InterlockedIncrement( &pControlDevExt->Specific.Control.QueueItemCount);
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+    AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                   AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSInsertWorkitem Inserting work item %08lX Count %08lX\n",
+                  "AFSInsertWorkitem Inserting work item %p Count %d\n",
                   WorkItem,
-                  lCount);
+                  lCount));
 
-    if( pDevExt->Specific.Library.QueueTail != NULL) // queue already has nodes
+    if( pControlDevExt->Specific.Control.QueueTail != NULL) // queue already has nodes
     {
 
-        pDevExt->Specific.Library.QueueTail->next = WorkItem;
+        pControlDevExt->Specific.Control.QueueTail->next = WorkItem;
     }
     else // first node
     {
 
-        pDevExt->Specific.Library.QueueHead = WorkItem;
+        pControlDevExt->Specific.Control.QueueHead = WorkItem;
     }
 
     WorkItem->next = NULL;
-    pDevExt->Specific.Library.QueueTail = WorkItem;
+    pControlDevExt->Specific.Control.QueueTail = WorkItem;
 
     // indicate that the queue has nodes
-    KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
+    KeSetEvent( &(pControlDevExt->Specific.Control.WorkerQueueHasItems),
                 0,
                 FALSE);
 
-    AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
+    AFSReleaseResource( &pControlDevExt->Specific.Control.QueueLock);
 
     return ntStatus;
 }
@@ -1758,48 +2016,48 @@ AFSInsertIOWorkitem( IN AFSWorkItem *WorkItem)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSDeviceExt *pDevExt = NULL;
+    AFSDeviceExt *pControlDevExt = NULL;
     LONG lCount;
 
-    pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
+    pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+    AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
                   AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSInsertIOWorkitem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
-                  &pDevExt->Specific.Library.IOQueueLock,
-                  PsGetCurrentThread());
+                  "AFSInsertIOWorkitem Acquiring Control QueueLock lock %p EXCL %08lX\n",
+                  &pControlDevExt->Specific.Control.IOQueueLock,
+                  PsGetCurrentThread()));
 
-    AFSAcquireExcl( &pDevExt->Specific.Library.IOQueueLock,
+    AFSAcquireExcl( &pControlDevExt->Specific.Control.IOQueueLock,
                     TRUE);
 
-    lCount = InterlockedIncrement( &pDevExt->Specific.Library.IOQueueItemCount);
+    lCount = InterlockedIncrement( &pControlDevExt->Specific.Control.IOQueueItemCount);
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+    AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                   AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSInsertWorkitem Inserting IO work item %08lX Count %08lX\n",
+                  "AFSInsertWorkitem Inserting IO work item %p Count %d\n",
                   WorkItem,
-                  lCount);
+                  lCount));
 
-    if( pDevExt->Specific.Library.IOQueueTail != NULL) // queue already has nodes
+    if( pControlDevExt->Specific.Control.IOQueueTail != NULL) // queue already has nodes
     {
 
-        pDevExt->Specific.Library.IOQueueTail->next = WorkItem;
+        pControlDevExt->Specific.Control.IOQueueTail->next = WorkItem;
     }
     else // first node
     {
 
-        pDevExt->Specific.Library.IOQueueHead = WorkItem;
+        pControlDevExt->Specific.Control.IOQueueHead = WorkItem;
     }
 
     WorkItem->next = NULL;
-    pDevExt->Specific.Library.IOQueueTail = WorkItem;
+    pControlDevExt->Specific.Control.IOQueueTail = WorkItem;
 
     // indicate that the queue has nodes
-    KeSetEvent( &(pDevExt->Specific.Library.IOWorkerQueueHasItems),
+    KeSetEvent( &(pControlDevExt->Specific.Control.IOWorkerQueueHasItems),
                 0,
                 FALSE);
 
-    AFSReleaseResource( &pDevExt->Specific.Library.IOQueueLock);
+    AFSReleaseResource( &pControlDevExt->Specific.Control.IOQueueLock);
 
     return ntStatus;
 }
@@ -1809,41 +2067,41 @@ AFSInsertWorkitemAtHead( IN AFSWorkItem *WorkItem)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSDeviceExt *pDevExt = NULL;
+    AFSDeviceExt *pControlDevExt = NULL;
     LONG lCount;
 
-    pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
+    pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+    AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
                   AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSInsertWorkitemAtHead Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
-                  &pDevExt->Specific.Library.QueueLock,
-                  PsGetCurrentThread());
+                  "AFSInsertWorkitemAtHead Acquiring Control QueueLock lock %p EXCL %08lX\n",
+                  &pControlDevExt->Specific.Control.QueueLock,
+                  PsGetCurrentThread()));
 
-    AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
+    AFSAcquireExcl( &pControlDevExt->Specific.Control.QueueLock,
                     TRUE);
 
-    WorkItem->next = pDevExt->Specific.Library.QueueHead;
+    WorkItem->next = pControlDevExt->Specific.Control.QueueHead;
 
-    pDevExt->Specific.Library.QueueHead = WorkItem;
+    pControlDevExt->Specific.Control.QueueHead = WorkItem;
 
-    lCount = InterlockedIncrement( &pDevExt->Specific.Library.QueueItemCount);
+    lCount = InterlockedIncrement( &pControlDevExt->Specific.Control.QueueItemCount);
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+    AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                   AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSInsertWorkitemAtHead Inserting work item %08lX Count %08lX\n",
+                  "AFSInsertWorkitemAtHead Inserting work item %p Count %d\n",
                   WorkItem,
-                  lCount);
+                  lCount));
 
     //
     // indicate that the queue has nodes
     //
 
-    KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
+    KeSetEvent( &(pControlDevExt->Specific.Control.WorkerQueueHasItems),
                 0,
                 FALSE);
 
-    AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
+    AFSReleaseResource( &pControlDevExt->Specific.Control.QueueLock);
 
     return ntStatus;
 }
@@ -1852,42 +2110,41 @@ AFSWorkItem *
 AFSRemoveWorkItem()
 {
 
-    NTSTATUS ntStatus = STATUS_SUCCESS;
     AFSWorkItem        *pWorkItem = NULL;
-    AFSDeviceExt *pDevExt = NULL;
+    AFSDeviceExt *pControlDevExt = NULL;
     LONG lCount;
 
-    pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
+    pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+    AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
                   AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSRemoveWorkItem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
-                  &pDevExt->Specific.Library.QueueLock,
-                  PsGetCurrentThread());
+                  "AFSRemoveWorkItem Acquiring Control QueueLock lock %p EXCL %08lX\n",
+                  &pControlDevExt->Specific.Control.QueueLock,
+                  PsGetCurrentThread()));
 
-    AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
+    AFSAcquireExcl( &pControlDevExt->Specific.Control.QueueLock,
                     TRUE);
 
-    if( pDevExt->Specific.Library.QueueHead != NULL) // queue has nodes
+    if( pControlDevExt->Specific.Control.QueueHead != NULL) // queue has nodes
     {
 
-        pWorkItem = pDevExt->Specific.Library.QueueHead;
+        pWorkItem = pControlDevExt->Specific.Control.QueueHead;
 
-        lCount = InterlockedDecrement( &pDevExt->Specific.Library.QueueItemCount);
+        lCount = InterlockedDecrement( &pControlDevExt->Specific.Control.QueueItemCount);
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSRemoveWorkItem Removing work item %08lX Count %08lX Thread %08lX\n",
+                      "AFSRemoveWorkItem Removing work item %p Count %d Thread %08lX\n",
                       pWorkItem,
                       lCount,
-                      PsGetCurrentThreadId());
+                      PsGetCurrentThreadId()));
 
-        pDevExt->Specific.Library.QueueHead = pDevExt->Specific.Library.QueueHead->next;
+        pControlDevExt->Specific.Control.QueueHead = pControlDevExt->Specific.Control.QueueHead->next;
 
-        if( pDevExt->Specific.Library.QueueHead == NULL) // if queue just became empty
+        if( pControlDevExt->Specific.Control.QueueHead == NULL) // if queue just became empty
         {
 
-            pDevExt->Specific.Library.QueueTail = NULL;
+            pControlDevExt->Specific.Control.QueueTail = NULL;
         }
         else
         {
@@ -1896,13 +2153,13 @@ AFSRemoveWorkItem()
             // Wake up another worker
             //
 
-            KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
+            KeSetEvent( &(pControlDevExt->Specific.Control.WorkerQueueHasItems),
                         0,
                         FALSE);
         }
     }
 
-    AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
+    AFSReleaseResource( &pControlDevExt->Specific.Control.QueueLock);
 
     return pWorkItem;
 }
@@ -1911,42 +2168,41 @@ AFSWorkItem *
 AFSRemoveIOWorkItem()
 {
 
-    NTSTATUS ntStatus = STATUS_SUCCESS;
     AFSWorkItem        *pWorkItem = NULL;
-    AFSDeviceExt *pDevExt = NULL;
+    AFSDeviceExt *pControlDevExt = NULL;
     LONG lCount;
 
-    pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
+    pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
 
-    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+    AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
                   AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSRemoveIOWorkItem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
-                  &pDevExt->Specific.Library.IOQueueLock,
-                  PsGetCurrentThread());
+                  "AFSRemoveIOWorkItem Acquiring Control QueueLock lock %p EXCL %08lX\n",
+                  &pControlDevExt->Specific.Control.IOQueueLock,
+                  PsGetCurrentThread()));
 
-    AFSAcquireExcl( &pDevExt->Specific.Library.IOQueueLock,
+    AFSAcquireExcl( &pControlDevExt->Specific.Control.IOQueueLock,
                     TRUE);
 
-    if( pDevExt->Specific.Library.IOQueueHead != NULL) // queue has nodes
+    if( pControlDevExt->Specific.Control.IOQueueHead != NULL) // queue has nodes
     {
 
-        pWorkItem = pDevExt->Specific.Library.IOQueueHead;
+        pWorkItem = pControlDevExt->Specific.Control.IOQueueHead;
 
-        lCount = InterlockedDecrement( &pDevExt->Specific.Library.IOQueueItemCount);
+        lCount = InterlockedDecrement( &pControlDevExt->Specific.Control.IOQueueItemCount);
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSRemoveWorkItem Removing work item %08lX Count %08lX Thread %08lX\n",
+                      "AFSRemoveWorkItem Removing work item %p Count %d Thread %08lX\n",
                       pWorkItem,
                       lCount,
-                      PsGetCurrentThreadId());
+                      PsGetCurrentThreadId()));
 
-        pDevExt->Specific.Library.IOQueueHead = pDevExt->Specific.Library.IOQueueHead->next;
+        pControlDevExt->Specific.Control.IOQueueHead = pControlDevExt->Specific.Control.IOQueueHead->next;
 
-        if( pDevExt->Specific.Library.IOQueueHead == NULL) // if queue just became empty
+        if( pControlDevExt->Specific.Control.IOQueueHead == NULL) // if queue just became empty
         {
 
-            pDevExt->Specific.Library.IOQueueTail = NULL;
+            pControlDevExt->Specific.Control.IOQueueTail = NULL;
         }
         else
         {
@@ -1955,13 +2211,13 @@ AFSRemoveIOWorkItem()
             // Wake up another worker
             //
 
-            KeSetEvent( &(pDevExt->Specific.Library.IOWorkerQueueHasItems),
+            KeSetEvent( &(pControlDevExt->Specific.Control.IOWorkerQueueHasItems),
                         0,
                         FALSE);
         }
     }
 
-    AFSReleaseResource( &pDevExt->Specific.Library.IOQueueLock);
+    AFSReleaseResource( &pControlDevExt->Specific.Control.IOQueueLock);
 
     return pWorkItem;
 }
@@ -1971,7 +2227,6 @@ AFSQueueWorkerRequest( IN AFSWorkItem *WorkItem)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSDeviceExt *pDevExt = NULL;
     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
 
     //
@@ -2002,7 +2257,6 @@ AFSQueueIOWorkerRequest( IN AFSWorkItem *WorkItem)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSDeviceExt *pDevExt = NULL;
     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
 
     //
@@ -2033,7 +2287,6 @@ AFSQueueWorkerRequestAtHead( IN AFSWorkItem *WorkItem)
 {
 
     NTSTATUS ntStatus = STATUS_SUCCESS;
-    AFSDeviceExt *pDevExt = NULL;
     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
 
     //
@@ -2072,13 +2325,13 @@ AFSQueueFlushExtents( IN AFSFcb *Fcb,
     __try
     {
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
                       "AFSQueueFlushExtents Queuing request for FID %08lX-%08lX-%08lX-%08lX\n",
                       Fcb->ObjectInformation->FileId.Cell,
                       Fcb->ObjectInformation->FileId.Volume,
                       Fcb->ObjectInformation->FileId.Vnode,
-                      Fcb->ObjectInformation->FileId.Unique);
+                      Fcb->ObjectInformation->FileId.Unique));
 
         //
         // Increment our flush count here just to keep the number of items in the
@@ -2090,13 +2343,13 @@ AFSQueueFlushExtents( IN AFSFcb *Fcb,
         if( lCount > 3)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                           AFS_TRACE_LEVEL_VERBOSE,
                           "AFSQueueFlushExtents Max queued items for FID %08lX-%08lX-%08lX-%08lX\n",
                           Fcb->ObjectInformation->FileId.Cell,
                           Fcb->ObjectInformation->FileId.Volume,
                           Fcb->ObjectInformation->FileId.Vnode,
-                          Fcb->ObjectInformation->FileId.Unique);
+                          Fcb->ObjectInformation->FileId.Unique));
 
             try_return( ntStatus);
         }
@@ -2104,9 +2357,9 @@ AFSQueueFlushExtents( IN AFSFcb *Fcb,
         if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSQueueFlushExtents Failing request, in shutdown\n");
+                          "AFSQueueFlushExtents Failing request, in shutdown\n"));
 
             try_return( ntStatus = STATUS_TOO_LATE);
         }
@@ -2115,16 +2368,16 @@ AFSQueueFlushExtents( IN AFSFcb *Fcb,
         // Allocate our request structure and send it to the worker
         //
 
-        pWorkItem = (AFSWorkItem *)AFSLibExAllocatePoolWithTag( NonPagedPool,
-                                                                sizeof( AFSWorkItem),
-                                                                AFS_WORK_ITEM_TAG);
+        pWorkItem = (AFSWorkItem *)AFSExAllocatePoolWithTag( NonPagedPool,
+                                                             sizeof( AFSWorkItem),
+                                                             AFS_WORK_ITEM_TAG);
 
         if( pWorkItem == NULL)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSQueueFlushExtents Failed to allocate work item\n");
+                          "AFSQueueFlushExtents Failed to allocate work item\n"));
 
             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
         }
@@ -2160,33 +2413,33 @@ AFSQueueFlushExtents( IN AFSFcb *Fcb,
 
         lCount = InterlockedIncrement( &Fcb->OpenReferenceCount);
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_FCB_REF_COUNTING,
                       AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSQueueFlushExtents Increment count on Fcb %08lX Cnt %d\n",
+                      "AFSQueueFlushExtents Increment count on Fcb %p Cnt %d\n",
                       Fcb,
-                      lCount);
+                      lCount));
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSQueueFlushExtents Workitem %08lX for FID %08lX-%08lX-%08lX-%08lX\n",
+                      "AFSQueueFlushExtents Workitem %p for FID %08lX-%08lX-%08lX-%08lX\n",
                       pWorkItem,
                       Fcb->ObjectInformation->FileId.Cell,
                       Fcb->ObjectInformation->FileId.Volume,
                       Fcb->ObjectInformation->FileId.Vnode,
-                      Fcb->ObjectInformation->FileId.Unique);
+                      Fcb->ObjectInformation->FileId.Unique));
 
         ntStatus = AFSQueueWorkerRequest( pWorkItem);
 
 try_exit:
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
                       "AFSQueueFlushExtents Request complete Status %08lX FID %08lX-%08lX-%08lX-%08lX\n",
                       Fcb->ObjectInformation->FileId.Cell,
                       Fcb->ObjectInformation->FileId.Volume,
                       Fcb->ObjectInformation->FileId.Vnode,
                       Fcb->ObjectInformation->FileId.Unique,
-                      ntStatus);
+                      ntStatus));
 
         //
         // Remove the count we added above
@@ -2194,6 +2447,8 @@ try_exit:
 
         lCount = InterlockedDecrement( &Fcb->Specific.File.QueuedFlushCount);
 
+        ASSERT( lCount >= 0);
+
         if( lCount == 0)
         {
 
@@ -2213,17 +2468,18 @@ try_exit:
                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
             }
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSQueueFlushExtents Failed to queue request Status %08lX\n", ntStatus);
+                          "AFSQueueFlushExtents Failed to queue request Status %08lX\n",
+                          ntStatus));
         }
     }
     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
     {
 
-        AFSDbgLogMsg( 0,
+        AFSDbgTrace(( 0,
                       0,
-                      "EXCEPTION - AFSQueueFlushExtents\n");
+                      "EXCEPTION - AFSQueueFlushExtents\n"));
 
         AFSDumpTraceFilesFnc();
     }
@@ -2241,15 +2497,15 @@ AFSQueueGlobalRootEnumeration()
     __try
     {
 
-        pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
-                                                                 sizeof(AFSWorkItem),
-                                                                 AFS_WORK_ITEM_TAG);
+        pWorkItem = (AFSWorkItem *) AFSExAllocatePoolWithTag( NonPagedPool,
+                                                              sizeof(AFSWorkItem),
+                                                              AFS_WORK_ITEM_TAG);
         if (NULL == pWorkItem)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSQueueGlobalRootEnumeration Failed to allocate work item\n");
+                          "AFSQueueGlobalRootEnumeration Failed to allocate work item\n"));
 
             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
         }
@@ -2261,19 +2517,19 @@ AFSQueueGlobalRootEnumeration()
 
         pWorkItem->RequestType = AFS_WORK_ENUMERATE_GLOBAL_ROOT;
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSQueueGlobalRootEnumeration Workitem %08lX\n",
-                      pWorkItem);
+                      "AFSQueueGlobalRootEnumeration Workitem %p\n",
+                      pWorkItem));
 
         ntStatus = AFSQueueWorkerRequest( pWorkItem);
 
 try_exit:
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
                       "AFSQueueGlobalRootEnumeration Request complete Status %08lX\n",
-                      ntStatus);
+                      ntStatus));
 
         if( !NT_SUCCESS( ntStatus))
         {
@@ -2284,18 +2540,18 @@ try_exit:
                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
             }
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
                           "AFSQueueGlobalRootEnumeration Failed to queue request Status %08lX\n",
-                          ntStatus);
+                          ntStatus));
         }
     }
     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
     {
 
-        AFSDbgLogMsg( 0,
+        AFSDbgTrace(( 0,
                       0,
-                      "EXCEPTION - AFSQueueGlobalRootEnumeration\n");
+                      "EXCEPTION - AFSQueueGlobalRootEnumeration\n"));
 
         AFSDumpTraceFilesFnc();
     }
@@ -2322,9 +2578,9 @@ AFSQueueStartIos( IN PFILE_OBJECT CacheFileObject,
         if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSQueueStartIos Failing request, in shutdown\n");
+                          "AFSQueueStartIos Failing request, in shutdown\n"));
 
             try_return( ntStatus = STATUS_TOO_LATE);
         }
@@ -2333,16 +2589,16 @@ AFSQueueStartIos( IN PFILE_OBJECT CacheFileObject,
         // Allocate our request structure and send it to the worker
         //
 
-        pWorkItem = (AFSWorkItem *)AFSLibExAllocatePoolWithTag( NonPagedPool,
-                                                                sizeof( AFSWorkItem),
-                                                                AFS_WORK_ITEM_TAG);
+        pWorkItem = (AFSWorkItem *)AFSExAllocatePoolWithTag( NonPagedPool,
+                                                             sizeof( AFSWorkItem),
+                                                             AFS_WORK_ITEM_TAG);
 
         if( pWorkItem == NULL)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSQueueStartIos Failed to allocate work item\n");
+                          "AFSQueueStartIos Failed to allocate work item\n"));
 
             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
         }
@@ -2372,19 +2628,19 @@ AFSQueueStartIos( IN PFILE_OBJECT CacheFileObject,
 
         pWorkItem->Specific.CacheAccess.GatherIo = GatherIo;
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSQueueStartIos Queuing IO Workitem %08lX\n",
-                      pWorkItem);
+                      "AFSQueueStartIos Queuing IO Workitem %p\n",
+                      pWorkItem));
 
         ntStatus = AFSQueueIOWorkerRequest( pWorkItem);
 
 try_exit:
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
                       "AFSQueueStartIos Request complete Status %08lX\n",
-                      ntStatus);
+                      ntStatus));
 
         if( !NT_SUCCESS( ntStatus))
         {
@@ -2399,9 +2655,9 @@ try_exit:
     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
     {
 
-        AFSDbgLogMsg( 0,
+        AFSDbgTrace(( 0,
                       0,
-                      "EXCEPTION - AFSQueueStartIos\n");
+                      "EXCEPTION - AFSQueueStartIos\n"));
 
         AFSDumpTraceFilesFnc();
     }
@@ -2420,15 +2676,15 @@ AFSQueueInvalidateObject( IN AFSObjectInfoCB *ObjectInfo,
     __try
     {
 
-        pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
-                                                                 sizeof(AFSWorkItem),
-                                                                 AFS_WORK_ITEM_TAG);
+        pWorkItem = (AFSWorkItem *) AFSExAllocatePoolWithTag( NonPagedPool,
+                                                              sizeof(AFSWorkItem),
+                                                              AFS_WORK_ITEM_TAG);
         if (NULL == pWorkItem)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSQueueInvalidateObject Failed to allocate work item\n");
+                          "AFSQueueInvalidateObject Failed to allocate work item\n"));
 
             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
         }
@@ -2444,19 +2700,19 @@ AFSQueueInvalidateObject( IN AFSObjectInfoCB *ObjectInfo,
 
         pWorkItem->Specific.Invalidate.InvalidateReason = InvalidateReason;
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSQueueInvalidateObject Workitem %08lX\n",
-                      pWorkItem);
+                      "AFSQueueInvalidateObject Workitem %p\n",
+                      pWorkItem));
 
         ntStatus = AFSQueueWorkerRequest( pWorkItem);
 
 try_exit:
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
                       "AFSQueueInvalidateObject Request complete Status %08lX\n",
-                      ntStatus);
+                      ntStatus));
 
         if( !NT_SUCCESS( ntStatus))
         {
@@ -2466,21 +2722,163 @@ try_exit:
                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
             }
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
                           "AFSQueueInvalidateObject Failed to queue request Status %08lX\n",
-                          ntStatus);
+                          ntStatus));
         }
     }
     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
     {
 
-        AFSDbgLogMsg( 0,
+        AFSDbgTrace(( 0,
                       0,
-                      "EXCEPTION - AFSQueueInvalidateObject\n");
+                      "EXCEPTION - AFSQueueInvalidateObject\n"));
 
         AFSDumpTraceFilesFnc();
     }
 
     return ntStatus;
 }
+
+NTSTATUS
+AFSDeferWrite( IN PDEVICE_OBJECT DeviceObject,
+               IN PFILE_OBJECT FileObject,
+               IN HANDLE CallingUser,
+               IN PIRP Irp,
+               IN ULONG BytesToWrite,
+               IN BOOLEAN bRetrying)
+{
+    NTSTATUS ntStatus = STATUS_SUCCESS;
+    AFSWorkItem *pWorkItem = NULL;
+
+    __try
+    {
+
+        //
+        // Pin the user buffer (first time round only - AFSLockSystemBuffer is
+        // idempotent)
+        //
+
+        if ( NULL == AFSLockSystemBuffer( Irp, BytesToWrite ))
+        {
+
+            AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
+                          AFS_TRACE_LEVEL_ERROR,
+                          "%s Could not pin user memory item\n",
+                          __FUNCTION__));
+
+            try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
+        }
+
+        pWorkItem = (AFSWorkItem *) AFSExAllocatePoolWithTag( NonPagedPool,
+                                                              sizeof(AFSWorkItem),
+                                                              AFS_WORK_ITEM_TAG);
+
+        if (NULL == pWorkItem)
+        {
+
+            AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
+                          AFS_TRACE_LEVEL_ERROR,
+                          "%s Failed to allocate work item\n",
+                          __FUNCTION__));
+
+            try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
+        }
+
+        RtlZeroMemory( pWorkItem,
+                       sizeof(AFSWorkItem));
+
+        pWorkItem->Size = sizeof( AFSWorkItem);
+
+        pWorkItem->RequestType = AFS_WORK_DEFERRED_WRITE;
+
+        pWorkItem->Specific.AsynchIo.CallingProcess = CallingUser;
+
+        pWorkItem->Specific.AsynchIo.Device = DeviceObject;
+
+        pWorkItem->Specific.AsynchIo.Irp = Irp;
+
+        AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING | AFS_SUBSYSTEM_WORKER_PROCESSING,
+                      AFS_TRACE_LEVEL_VERBOSE,
+                      "%s Workitem %p\n",
+                      __FUNCTION__,
+                      pWorkItem));
+
+        CcDeferWrite( FileObject, AFSPostedDeferredWrite, pWorkItem, NULL, BytesToWrite, bRetrying);
+
+        IoMarkIrpPending(Irp);
+
+        ntStatus = STATUS_PENDING;
+    }
+    __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
+    {
+
+        AFSDbgTrace(( 0,
+                      0,
+                      "EXCEPTION - %s \n",
+                      __FUNCTION__));
+
+        ntStatus = GetExceptionCode();
+    }
+
+try_exit:
+
+    AFSDbgTrace(( AFS_SUBSYSTEM_WORKER_PROCESSING,
+                  AFS_TRACE_LEVEL_VERBOSE,
+                  "%s complete Status %08lX\n",
+                  __FUNCTION__,
+                  ntStatus));
+
+    if( !NT_SUCCESS( ntStatus))
+    {
+
+        if( pWorkItem != NULL)
+        {
+
+            ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
+        }
+
+        AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
+                      AFS_TRACE_LEVEL_ERROR,
+                      "%s Failed to queue request Status %08lX\n",
+                      __FUNCTION__,
+                      ntStatus));
+    }
+
+    return ntStatus;
+}
+
+static
+VOID
+AFSPostedDeferredWrite( IN PVOID Context1,
+                        IN PVOID Context2)
+{
+    UNREFERENCED_PARAMETER( Context2);
+    NTSTATUS ntStatus;
+
+    AFSWorkItem *pWorkItem = (AFSWorkItem *) Context1;
+
+    AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING | AFS_SUBSYSTEM_WORKER_PROCESSING,
+                  AFS_TRACE_LEVEL_ERROR,
+                  "%s Workitem %p\n",
+                  __FUNCTION__,
+                  pWorkItem));
+
+    ntStatus = AFSQueueIOWorkerRequest( pWorkItem);
+
+    if (!NT_SUCCESS( ntStatus))
+    {
+
+        AFSCompleteRequest( pWorkItem->Specific.AsynchIo.Irp, ntStatus);
+
+        ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
+
+        AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING | AFS_SUBSYSTEM_WORKER_PROCESSING,
+                      AFS_TRACE_LEVEL_ERROR,
+                      "%s (%p) Failed to queue request Status %08lX\n",
+                      __FUNCTION__,
+                      pWorkItem->Specific.AsynchIo.Irp,
+                      ntStatus));
+    }
+}