Windows: Categorize ObjectInformationCB RefCnts
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSWorker.cpp
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3  * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  *   this list of conditions and the following disclaimer.
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice,
14  *   this list of conditions and the following disclaimer in the
15  *   documentation
16  *   and/or other materials provided with the distribution.
17  * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
18  *   nor the names of their contributors may be used to endorse or promote
19  *   products derived from this software without specific prior written
20  *   permission from Kernel Drivers, LLC and Your File System, Inc.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 //
36 // File: AFSWorker.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 //
42 // Function: AFSInitializeWorkerPool
43 //
44 // Description:
45 //
46 //      This function initializes the worker thread pool
47 //
48 // Return:
49 //
50 //      A status is returned for the function
51 //
52
53 NTSTATUS
54 AFSInitializeWorkerPool()
55 {
56
57     NTSTATUS ntStatus = STATUS_SUCCESS;
58     AFSWorkQueueContext        *pCurrentWorker = NULL, *pLastWorker = NULL;
59     AFSDeviceExt *pDevExt = NULL;
60
61     __Enter
62     {
63
64         pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
65
66         //
67         // Initialize the worker threads.
68         //
69
70         pDevExt->Specific.Library.WorkerCount = 0;
71
72         KeInitializeEvent( &pDevExt->Specific.Library.WorkerQueueHasItems,
73                            SynchronizationEvent,
74                            FALSE);
75
76         //
77         // Initialize the queue resource
78         //
79
80         ExInitializeResourceLite( &pDevExt->Specific.Library.QueueLock);
81
82         while( pDevExt->Specific.Library.WorkerCount < AFS_WORKER_COUNT)
83         {
84
85             pCurrentWorker = (AFSWorkQueueContext *)AFSLibExAllocatePoolWithTag( NonPagedPool,
86                                                                                  sizeof( AFSWorkQueueContext),
87                                                                                  AFS_WORKER_CB_TAG);
88
89             if( pCurrentWorker == NULL)
90             {
91
92                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
93                               AFS_TRACE_LEVEL_ERROR,
94                               "AFSInitializeWorkerPool Failed to allocate worker context\n");
95
96                 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
97
98                 break;
99             }
100
101             RtlZeroMemory( pCurrentWorker,
102                            sizeof( AFSWorkQueueContext));
103
104             ntStatus = AFSInitWorkerThread( pCurrentWorker,
105                                             (PKSTART_ROUTINE)AFSWorkerThread);
106
107             if( !NT_SUCCESS( ntStatus))
108             {
109
110                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
111                               AFS_TRACE_LEVEL_ERROR,
112                               "AFSInitializeWorkerPool Failed to initialize worker thread Status %08lX\n", ntStatus);
113
114                 ExFreePool( pCurrentWorker);
115
116                 break;
117             }
118
119             if( pDevExt->Specific.Library.PoolHead == NULL)
120             {
121
122                 pDevExt->Specific.Library.PoolHead = pCurrentWorker;
123             }
124             else
125             {
126
127                 pLastWorker->fLink = pCurrentWorker;
128             }
129
130             pLastWorker = pCurrentWorker;
131
132             pDevExt->Specific.Library.WorkerCount++;
133         }
134
135         //
136         // If there was a failure but there is at least one worker, then go with it.
137         //
138
139         if( !NT_SUCCESS( ntStatus) &&
140             pDevExt->Specific.Library.WorkerCount == 0)
141         {
142
143             try_return( ntStatus);
144         }
145
146         ntStatus = STATUS_SUCCESS;
147
148         //
149         // Now our IO Worker queue
150         //
151
152         pDevExt->Specific.Library.IOWorkerCount = 0;
153
154         KeInitializeEvent( &pDevExt->Specific.Library.IOWorkerQueueHasItems,
155                            SynchronizationEvent,
156                            FALSE);
157
158         //
159         // Initialize the queue resource
160         //
161
162         ExInitializeResourceLite( &pDevExt->Specific.Library.IOQueueLock);
163
164         while( pDevExt->Specific.Library.IOWorkerCount < AFS_IO_WORKER_COUNT)
165         {
166
167             pCurrentWorker = (AFSWorkQueueContext *)AFSLibExAllocatePoolWithTag( NonPagedPool,
168                                                                                  sizeof( AFSWorkQueueContext),
169                                                                                  AFS_WORKER_CB_TAG);
170
171             if( pCurrentWorker == NULL)
172             {
173
174                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
175                               AFS_TRACE_LEVEL_ERROR,
176                               "AFSInitializeWorkerPool Failed to allocate IO worker context\n");
177
178                 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
179
180                 break;
181             }
182
183             RtlZeroMemory( pCurrentWorker,
184                            sizeof( AFSWorkQueueContext));
185
186             ntStatus = AFSInitWorkerThread( pCurrentWorker,
187                                             (PKSTART_ROUTINE)AFSIOWorkerThread);
188
189             if( !NT_SUCCESS( ntStatus))
190             {
191
192                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
193                               AFS_TRACE_LEVEL_ERROR,
194                               "AFSInitializeWorkerPool Failed to initialize IO worker thread Status %08lX\n", ntStatus);
195
196                 ExFreePool( pCurrentWorker);
197
198                 break;
199             }
200
201             if( pDevExt->Specific.Library.IOPoolHead == NULL)
202             {
203
204                 pDevExt->Specific.Library.IOPoolHead = pCurrentWorker;
205             }
206             else
207             {
208
209                 pLastWorker->fLink = pCurrentWorker;
210             }
211
212             pLastWorker = pCurrentWorker;
213
214             pDevExt->Specific.Library.IOWorkerCount++;
215         }
216
217         //
218         // If there was a failure but there is at least one worker, then go with it.
219         //
220
221         if( !NT_SUCCESS( ntStatus) &&
222             pDevExt->Specific.Library.IOWorkerCount == 0)
223         {
224
225             try_return( ntStatus);
226         }
227
228 try_exit:
229
230         if( !NT_SUCCESS( ntStatus))
231         {
232
233             //
234             // Failed to initialize the pool so tear it down
235             //
236
237             AFSRemoveWorkerPool();
238         }
239     }
240
241     return ntStatus;
242 }
243
244 //
245 // Function: AFSRemoveWorkerPool
246 //
247 // Description:
248 //
249 //      This function tears down the worker thread pool
250 //
251 // Return:
252 //
253 //      A status is returned for the function
254 //
255
256 NTSTATUS
257 AFSRemoveWorkerPool()
258 {
259
260     NTSTATUS ntStatus = STATUS_SUCCESS;
261     ULONG index = 0;
262     AFSWorkQueueContext        *pCurrentWorker = NULL, *pNextWorker = NULL;
263     AFSDeviceExt *pDevExt = NULL;
264
265     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
266
267     //
268     // Loop through the workers shutting them down in two stages.
269     // First, clear AFS_WORKER_PROCESS_REQUESTS so that workers
270     // stop processing requests.  Second, call AFSShutdownWorkerThread()
271     // to wake the workers and wait for them to exit.
272     //
273
274     pCurrentWorker = pDevExt->Specific.Library.PoolHead;
275
276     while( index < pDevExt->Specific.Library.WorkerCount)
277     {
278
279         ClearFlag( pCurrentWorker->State, AFS_WORKER_PROCESS_REQUESTS);
280
281         pCurrentWorker = pCurrentWorker->fLink;
282
283         if ( pCurrentWorker == NULL)
284         {
285
286             break;
287         }
288
289         index++;
290     }
291
292     pCurrentWorker = pDevExt->Specific.Library.PoolHead;
293
294     index = 0;
295
296     while( index < pDevExt->Specific.Library.WorkerCount)
297     {
298
299         ntStatus = AFSShutdownWorkerThread( pCurrentWorker);
300
301         pNextWorker = pCurrentWorker->fLink;
302
303         ExFreePool( pCurrentWorker);
304
305         pCurrentWorker = pNextWorker;
306
307         if( pCurrentWorker == NULL)
308         {
309
310             break;
311         }
312
313         index++;
314     }
315
316     pDevExt->Specific.Library.PoolHead = NULL;
317
318     ExDeleteResourceLite( &pDevExt->Specific.Library.QueueLock);
319
320     //
321     // Loop through the IO workers shutting them down in two stages.
322     // First, clear AFS_WORKER_PROCESS_REQUESTS so that workers
323     // stop processing requests.  Second, call AFSShutdownIOWorkerThread()
324     // to wake the workers and wait for them to exit.
325     //
326
327     pCurrentWorker = pDevExt->Specific.Library.IOPoolHead;
328
329     index = 0;
330
331     while( index < pDevExt->Specific.Library.IOWorkerCount)
332     {
333
334         ClearFlag( pCurrentWorker->State, AFS_WORKER_PROCESS_REQUESTS);
335
336         pCurrentWorker = pCurrentWorker->fLink;
337
338         if ( pCurrentWorker == NULL)
339         {
340
341             break;
342         }
343
344         index++;
345     }
346
347     pCurrentWorker = pDevExt->Specific.Library.IOPoolHead;
348
349     index = 0;
350
351     while( index < pDevExt->Specific.Library.IOWorkerCount)
352     {
353
354         ntStatus = AFSShutdownIOWorkerThread( pCurrentWorker);
355
356         pNextWorker = pCurrentWorker->fLink;
357
358         ExFreePool( pCurrentWorker);
359
360         pCurrentWorker = pNextWorker;
361
362         if( pCurrentWorker == NULL)
363         {
364
365             break;
366         }
367
368         index++;
369     }
370
371     pDevExt->Specific.Library.IOPoolHead = NULL;
372
373     ExDeleteResourceLite( &pDevExt->Specific.Library.IOQueueLock);
374
375     return ntStatus;
376 }
377
378 NTSTATUS
379 AFSInitVolumeWorker( IN AFSVolumeCB *VolumeCB)
380 {
381
382     NTSTATUS ntStatus = STATUS_SUCCESS;
383     AFSWorkQueueContext *pWorker = &VolumeCB->VolumeWorkerContext;
384     HANDLE hThread;
385     AFSDeviceExt *pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
386     PKSTART_ROUTINE pStartRoutine = NULL;
387     LONG lCount;
388
389     __Enter
390     {
391
392         if ( VolumeCB != AFSGlobalRoot)
393         {
394
395             return STATUS_INVALID_PARAMETER;
396         }
397
398         pStartRoutine = AFSPrimaryVolumeWorkerThread;
399
400         //
401         // Initialize the worker thread
402         //
403
404         KeInitializeEvent( &pWorker->WorkerThreadReady,
405                            NotificationEvent,
406                            FALSE);
407
408         //
409         // Set the worker to process requests
410         //
411
412         pWorker->State = AFS_WORKER_PROCESS_REQUESTS;
413
414         //
415         // Launch the thread
416         //
417
418         ntStatus =  PsCreateSystemThread( &hThread,
419                                           0,
420                                           NULL,
421                                           NULL,
422                                           NULL,
423                                           pStartRoutine,
424                                           (void *)VolumeCB);
425
426         if( NT_SUCCESS( ntStatus))
427         {
428
429             ObReferenceObjectByHandle( hThread,
430                                        GENERIC_READ | GENERIC_WRITE,
431                                        NULL,
432                                        KernelMode,
433                                        (PVOID *)&pWorker->WorkerThreadObject,
434                                        NULL);
435
436             ntStatus = KeWaitForSingleObject( &pWorker->WorkerThreadReady,
437                                               Executive,
438                                               KernelMode,
439                                               FALSE,
440                                               NULL);
441
442             lCount = InterlockedIncrement( &pControlDeviceExt->Specific.Control.VolumeWorkerThreadCount);
443
444             if( lCount > 0)
445             {
446
447                 KeClearEvent( &pControlDeviceExt->Specific.Control.VolumeWorkerCloseEvent);
448             }
449
450             ZwClose( hThread);
451         }
452     }
453
454     return ntStatus;
455 }
456
457 //
458 // Function: AFSInitWorkerThread
459 //
460 // Description:
461 //
462 //      This function initializes a worker thread in the pool
463 //
464 // Return:
465 //
466 //      A status is returned for the function
467 //
468
469 NTSTATUS
470 AFSInitWorkerThread( IN AFSWorkQueueContext *PoolContext,
471                      IN PKSTART_ROUTINE WorkerRoutine)
472 {
473
474     NTSTATUS ntStatus = STATUS_SUCCESS;
475     HANDLE Handle;
476
477     //
478     // INitialize the worker signal thread
479     //
480
481     KeInitializeEvent( &PoolContext->WorkerThreadReady,
482                        NotificationEvent,
483                        FALSE);
484
485     //
486     // Set the worker to process requests
487     //
488
489     PoolContext->State = AFS_WORKER_PROCESS_REQUESTS;
490
491     //
492     // Launch the thread
493     //
494
495     ntStatus =  PsCreateSystemThread( &Handle,
496                                       0,
497                                       NULL,
498                                       NULL,
499                                       NULL,
500                                       WorkerRoutine,
501                                       (void *)PoolContext);
502
503     if( NT_SUCCESS( ntStatus))
504     {
505
506         ObReferenceObjectByHandle( Handle,
507                                    GENERIC_READ | GENERIC_WRITE,
508                                    NULL,
509                                    KernelMode,
510                                    (PVOID *)&PoolContext->WorkerThreadObject,
511                                    NULL);
512
513         ntStatus = KeWaitForSingleObject( &PoolContext->WorkerThreadReady,
514                                           Executive,
515                                           KernelMode,
516                                           FALSE,
517                                           NULL);
518
519         ZwClose( Handle);
520     }
521
522     return ntStatus;
523 }
524
525 NTSTATUS
526 AFSShutdownVolumeWorker( IN AFSVolumeCB *VolumeCB)
527 {
528
529     NTSTATUS ntStatus = STATUS_SUCCESS;
530     AFSWorkQueueContext *pWorker = &VolumeCB->VolumeWorkerContext;
531
532     //
533     // Clear the 'keep processing' flag
534     //
535
536     ClearFlag( pWorker->State, AFS_WORKER_PROCESS_REQUESTS);
537
538     if( pWorker->WorkerThreadObject != NULL)
539     {
540         while ( BooleanFlagOn( pWorker->State, AFS_WORKER_INITIALIZED) )
541         {
542
543             ntStatus = KeWaitForSingleObject( pWorker->WorkerThreadObject,
544                                               Executive,
545                                               KernelMode,
546                                               FALSE,
547                                               NULL);
548         }
549
550         ObDereferenceObject( pWorker->WorkerThreadObject);
551
552         pWorker->WorkerThreadObject = NULL;
553     }
554
555     return ntStatus;
556 }
557
558 //
559 // Function: AFSShutdownWorkerThread
560 //
561 // Description:
562 //
563 //      This function shutsdown a worker thread in the pool
564 //
565 // Return:
566 //
567 //      A status is returned for the function
568 //
569
570 NTSTATUS
571 AFSShutdownWorkerThread( IN AFSWorkQueueContext *PoolContext)
572 {
573
574     NTSTATUS ntStatus = STATUS_SUCCESS;
575     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
576
577     if( PoolContext->WorkerThreadObject != NULL)
578     {
579
580         while ( BooleanFlagOn( PoolContext->State, AFS_WORKER_INITIALIZED) )
581         {
582
583             //
584             // Wake up the thread if it is a sleep
585             //
586
587             KeSetEvent( &pDeviceExt->Specific.Library.WorkerQueueHasItems,
588                         0,
589                         FALSE);
590
591             ntStatus = KeWaitForSingleObject( PoolContext->WorkerThreadObject,
592                                               Executive,
593                                               KernelMode,
594                                               FALSE,
595                                               NULL);
596         }
597
598         ObDereferenceObject( PoolContext->WorkerThreadObject);
599
600         PoolContext->WorkerThreadObject = NULL;
601     }
602
603     return ntStatus;
604 }
605
606 //
607 // Function: AFSShutdownIOWorkerThread
608 //
609 // Description:
610 //
611 //      This function shutsdown an IO worker thread in the pool
612 //
613 // Return:
614 //
615 //      A status is returned for the function
616 //
617
618 NTSTATUS
619 AFSShutdownIOWorkerThread( IN AFSWorkQueueContext *PoolContext)
620 {
621
622     NTSTATUS ntStatus = STATUS_SUCCESS;
623     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
624
625     if( PoolContext->WorkerThreadObject != NULL)
626     {
627
628         while ( BooleanFlagOn( PoolContext->State, AFS_WORKER_INITIALIZED) )
629         {
630
631             //
632             // Wake up the thread if it is a sleep
633             //
634
635             KeSetEvent( &pDeviceExt->Specific.Library.IOWorkerQueueHasItems,
636                         0,
637                         FALSE);
638
639             ntStatus = KeWaitForSingleObject( PoolContext->WorkerThreadObject,
640                                               Executive,
641                                               KernelMode,
642                                               FALSE,
643                                               NULL);
644         }
645
646         ObDereferenceObject( PoolContext->WorkerThreadObject);
647
648         PoolContext->WorkerThreadObject = NULL;
649     }
650
651     return ntStatus;
652 }
653
654 //
655 // Function: AFSWorkerThread
656 //
657 // Description:
658 //
659 //      This is the worker thread entry point.
660 //
661 // Return:
662 //
663 //      A status is returned for the function
664 //
665
666 void
667 AFSWorkerThread( IN PVOID Context)
668 {
669
670     NTSTATUS ntStatus = STATUS_SUCCESS;
671     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)Context;
672     AFSWorkItem *pWorkItem;
673     BOOLEAN freeWorkItem = TRUE;
674     AFSDeviceExt *pLibraryDevExt = NULL;
675     LONG lCount;
676
677     pLibraryDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
678
679     //
680     // Indicate that we are initialized and ready
681     //
682
683     KeSetEvent( &pPoolContext->WorkerThreadReady,
684                 0,
685                 FALSE);
686
687     //
688     // Indicate we are initialized
689     //
690
691     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
692
693     ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.WorkerQueueHasItems,
694                                       Executive,
695                                       KernelMode,
696                                       FALSE,
697                                       NULL);
698
699     while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
700     {
701
702         if( !NT_SUCCESS( ntStatus))
703         {
704
705             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
706                           AFS_TRACE_LEVEL_ERROR,
707                           "AFSWorkerThread Wait for queue items failed Status %08lX\n", ntStatus);
708
709             ntStatus = STATUS_SUCCESS;
710         }
711         else
712         {
713
714             pWorkItem = AFSRemoveWorkItem();
715
716             if( pWorkItem == NULL)
717             {
718
719                 ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.WorkerQueueHasItems,
720                                                   Executive,
721                                                   KernelMode,
722                                                   FALSE,
723                                                   NULL);
724             }
725             else
726             {
727
728                 freeWorkItem = TRUE;
729
730                 //
731                 // Switch on the type of work item to process
732                 //
733
734                 switch( pWorkItem->RequestType)
735                 {
736
737                     case AFS_WORK_FLUSH_FCB:
738                     {
739
740                         ntStatus = AFSFlushExtents( pWorkItem->Specific.Fcb.Fcb,
741                                                     &pWorkItem->AuthGroup);
742
743                         if( !NT_SUCCESS( ntStatus))
744                         {
745
746                             AFSReleaseExtentsWithFlush( pWorkItem->Specific.Fcb.Fcb,
747                                                         &pWorkItem->AuthGroup,
748                                                         FALSE);
749                         }
750
751                         ASSERT( pWorkItem->Specific.Fcb.Fcb->OpenReferenceCount != 0);
752
753                         lCount = InterlockedDecrement( &pWorkItem->Specific.Fcb.Fcb->OpenReferenceCount);
754
755                         break;
756                     }
757
758                     case AFS_WORK_ENUMERATE_GLOBAL_ROOT:
759                     {
760
761                         AFSEnumerateGlobalRoot( NULL);
762
763                         break;
764                     }
765
766                     case AFS_WORK_INVALIDATE_OBJECT:
767                     {
768
769                         AFSPerformObjectInvalidate( pWorkItem->Specific.Invalidate.ObjectInfo,
770                                                     pWorkItem->Specific.Invalidate.InvalidateReason);
771
772                         freeWorkItem = TRUE;
773
774                         break;
775                     }
776
777                     case AFS_WORK_START_IOS:
778                     {
779
780                         freeWorkItem = TRUE;
781
782                         break;
783                     }
784
785                     default:
786
787                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
788                                       AFS_TRACE_LEVEL_ERROR,
789                                       "AFSWorkerThread Unknown request type %d\n", pWorkItem->RequestType);
790
791                         break;
792                 }
793
794                 if( freeWorkItem)
795                 {
796
797                     ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
798                 }
799
800                 ntStatus = STATUS_SUCCESS;
801             }
802         }
803     } // worker thread loop
804
805     ClearFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
806
807     // Wake up another worker so they too can exit
808
809     KeSetEvent( &pLibraryDevExt->Specific.Library.WorkerQueueHasItems,
810                 0,
811                 FALSE);
812
813     PsTerminateSystemThread( 0);
814
815     return;
816 }
817
818 void
819 AFSIOWorkerThread( IN PVOID Context)
820 {
821
822     NTSTATUS ntStatus = STATUS_SUCCESS;
823     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)Context;
824     AFSWorkItem *pWorkItem;
825     BOOLEAN freeWorkItem = TRUE;
826     AFSDeviceExt *pLibraryDevExt = NULL, *pRdrDevExt = NULL;
827
828     pLibraryDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
829
830     //
831     // Indicate that we are initialized and ready
832     //
833
834     KeSetEvent( &pPoolContext->WorkerThreadReady,
835                 0,
836                 FALSE);
837
838
839     //
840     // Indicate we are initialized
841     //
842
843     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
844
845     ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.IOWorkerQueueHasItems,
846                                       Executive,
847                                       KernelMode,
848                                       FALSE,
849                                       NULL);
850
851     while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
852     {
853
854         if( !NT_SUCCESS( ntStatus))
855         {
856
857             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
858                           AFS_TRACE_LEVEL_ERROR,
859                           "AFSIOWorkerThread Wait for queue items failed Status %08lX\n", ntStatus);
860
861             ntStatus = STATUS_SUCCESS;
862         }
863         else
864         {
865
866             pWorkItem = AFSRemoveIOWorkItem();
867
868             if( pWorkItem == NULL)
869             {
870
871                 ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.IOWorkerQueueHasItems,
872                                                   Executive,
873                                                   KernelMode,
874                                                   FALSE,
875                                                   NULL);
876             }
877             else
878             {
879
880                 freeWorkItem = TRUE;
881
882                 //
883                 // Switch on the type of work item to process
884                 //
885
886                 switch( pWorkItem->RequestType)
887                 {
888
889                     case AFS_WORK_START_IOS:
890                     {
891
892                         pRdrDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
893
894                         //
895                         // The final status is in the gather io
896                         //
897
898                         ntStatus = AFSStartIos( pWorkItem->Specific.CacheAccess.CacheFileObject,
899                                                 pWorkItem->Specific.CacheAccess.FunctionCode,
900                                                 pWorkItem->Specific.CacheAccess.RequestFlags,
901                                                 pWorkItem->Specific.CacheAccess.IoRuns,
902                                                 pWorkItem->Specific.CacheAccess.RunCount,
903                                                 pWorkItem->Specific.CacheAccess.GatherIo);
904
905                         //
906                         // Regardless of the status we we do the complete - there may
907                         // be IOs in flight
908                         // Decrement the count - setting the event if we were told
909                         // to. This may trigger completion.
910                         //
911
912                         AFSCompleteIo( pWorkItem->Specific.CacheAccess.GatherIo, ntStatus );
913
914                         freeWorkItem = TRUE;
915
916                         break;
917                     }
918
919                     default:
920
921                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
922                                       AFS_TRACE_LEVEL_ERROR,
923                                       "AFSWorkerThread Unknown request type %d\n", pWorkItem->RequestType);
924
925                         break;
926                 }
927
928                 if( freeWorkItem)
929                 {
930
931                     ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
932                 }
933
934                 ntStatus = STATUS_SUCCESS;
935             }
936         }
937     } // worker thread loop
938
939     ClearFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
940
941     // Wake up another IOWorker so they too can exit
942
943     KeSetEvent( &pLibraryDevExt->Specific.Library.IOWorkerQueueHasItems,
944                 0,
945                 FALSE);
946
947     PsTerminateSystemThread( 0);
948
949     return;
950 }
951
952 void
953 AFSPrimaryVolumeWorkerThread( IN PVOID Context)
954 {
955
956     UNREFERENCED_PARAMETER(Context);
957     NTSTATUS ntStatus = STATUS_SUCCESS;
958     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)&AFSGlobalRoot->VolumeWorkerContext;
959     AFSDeviceExt *pControlDeviceExt = NULL;
960     AFSDeviceExt *pRDRDeviceExt = NULL;
961     LARGE_INTEGER DueTime;
962     LONG TimeOut;
963     KTIMER Timer;
964     AFSObjectInfoCB *pCurrentObject = NULL, *pNextObject = NULL, *pCurrentChildObject = NULL;
965     AFSDirectoryCB *pCurrentDirEntry = NULL, *pNextDirEntry = NULL;
966     BOOLEAN bReleaseVolumeLock = FALSE;
967     AFSVolumeCB *pVolumeCB = NULL, *pNextVolume = NULL;
968     AFSFcb *pFcb = NULL;
969     LARGE_INTEGER liCurrentTime;
970     BOOLEAN bVolumeObject = FALSE;
971     BOOLEAN bFcbBusy = FALSE;
972     LONG lCount;
973
974     pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
975
976     pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
977
978     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
979                   AFS_TRACE_LEVEL_VERBOSE,
980                   "AFSPrimaryVolumeWorkerThread Initialized\n");
981
982     //
983     // Initialize the timer for the worker thread
984     //
985
986     DueTime.QuadPart = -(5000);
987
988     TimeOut = 5000;
989
990     KeInitializeTimerEx( &Timer,
991                          SynchronizationTimer);
992
993     KeSetTimerEx( &Timer,
994                   DueTime,
995                   TimeOut,
996                   NULL);
997
998     //
999     // Indicate that we are initialized and ready
1000     //
1001
1002     KeSetEvent( &pPoolContext->WorkerThreadReady,
1003                 0,
1004                 FALSE);
1005
1006     //
1007     // Indicate we are initialized
1008     //
1009
1010     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
1011
1012     while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
1013     {
1014
1015         if ( bFcbBusy == FALSE)
1016         {
1017
1018             KeWaitForSingleObject( &Timer,
1019                                    Executive,
1020                                    KernelMode,
1021                                    FALSE,
1022                                    NULL);
1023         }
1024         else
1025         {
1026
1027             bFcbBusy = FALSE;
1028         }
1029
1030         //
1031         // This is the primary volume worker so it will traverse the volume list
1032         // looking for cleanup or volumes requiring private workers
1033         //
1034
1035         AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
1036                           TRUE);
1037
1038         pVolumeCB = pRDRDeviceExt->Specific.RDR.VolumeListHead;
1039
1040         while( pVolumeCB != NULL)
1041         {
1042
1043             if( pVolumeCB == AFSGlobalRoot ||
1044                 !AFSAcquireExcl( pVolumeCB->VolumeLock,
1045                                  FALSE))
1046             {
1047
1048                 pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
1049
1050                 continue;
1051             }
1052
1053             if( pVolumeCB->ObjectInfoListHead == NULL)
1054             {
1055
1056                 AFSReleaseResource( pVolumeCB->VolumeLock);
1057
1058                 AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1059
1060                 AFSAcquireExcl( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock,
1061                                 TRUE);
1062
1063                 AFSAcquireExcl( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
1064                                 TRUE);
1065
1066                 if( !AFSAcquireExcl( pVolumeCB->VolumeLock,
1067                                      FALSE))
1068                 {
1069
1070                     AFSConvertToShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1071
1072                     AFSReleaseResource( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock);
1073
1074                     pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
1075
1076                     continue;
1077                 }
1078
1079                 KeQueryTickCount( &liCurrentTime);
1080
1081                 pNextVolume = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
1082
1083                 AFSAcquireShared( &pVolumeCB->ObjectInformation.NonPagedInfo->ObjectInfoLock,
1084                                   TRUE);
1085
1086                 if( pVolumeCB->ObjectInfoListHead == NULL &&
1087                     pVolumeCB->DirectoryCB->DirOpenReferenceCount <= 0 &&
1088                     pVolumeCB->VolumeReferenceCount == 1 &&
1089                     ( pVolumeCB->RootFcb == NULL ||
1090                       pVolumeCB->RootFcb->OpenReferenceCount == 0) &&
1091                     pVolumeCB->ObjectInformation.ObjectReferenceCount <= 0)
1092                 {
1093
1094                     if( pVolumeCB->RootFcb != NULL)
1095                     {
1096
1097                         AFSRemoveRootFcb( pVolumeCB->RootFcb);
1098                     }
1099
1100                     AFSReleaseResource( &pVolumeCB->ObjectInformation.NonPagedInfo->ObjectInfoLock);
1101
1102                     AFSRemoveVolume( pVolumeCB);
1103                 }
1104                 else
1105                 {
1106
1107                     AFSReleaseResource( &pVolumeCB->ObjectInformation.NonPagedInfo->ObjectInfoLock);
1108
1109                     AFSReleaseResource( pVolumeCB->VolumeLock);
1110                 }
1111
1112                 AFSConvertToShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1113
1114                 AFSReleaseResource( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock);
1115
1116                 pVolumeCB = pNextVolume;
1117
1118                 continue;
1119             }
1120
1121             //
1122             // Don't need this lock anymore now that we have a volume cb to work with
1123             //
1124
1125             AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1126
1127             //
1128             // For now we only need the volume lock shared
1129             //
1130
1131             AFSConvertToShared( pVolumeCB->VolumeLock);
1132
1133             if( AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
1134                                   FALSE))
1135             {
1136
1137                 pCurrentObject = pVolumeCB->ObjectInfoListHead;
1138
1139                 pNextObject = NULL;
1140
1141                 bReleaseVolumeLock = TRUE;
1142
1143                 while( pCurrentObject != NULL)
1144                 {
1145
1146                     if( pCurrentObject != &pVolumeCB->ObjectInformation)
1147                     {
1148
1149                         pNextObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
1150
1151                         if( pNextObject == NULL &&
1152                             pVolumeCB != AFSGlobalRoot)  // Don't free up the root of the global
1153                         {
1154
1155                             pNextObject = &pVolumeCB->ObjectInformation;
1156                         }
1157
1158                         bVolumeObject = FALSE;
1159                     }
1160                     else
1161                     {
1162
1163                         pNextObject = NULL;
1164
1165                         bVolumeObject = TRUE;
1166                     }
1167
1168                     if( pCurrentObject->FileType == AFS_FILE_TYPE_DIRECTORY &&
1169                         !BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))  // If we are in shutdown mode skip directories
1170                     {
1171
1172                         //
1173                         // If this object is deleted then remove it from the parent, if we can
1174                         //
1175
1176                         if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DELETED) &&
1177                             pCurrentObject->ObjectReferenceCount <= 0 &&
1178                             ( pCurrentObject->Fcb == NULL ||
1179                               pCurrentObject->Fcb->OpenReferenceCount == 0) &&
1180                             pCurrentObject->Specific.Directory.DirectoryNodeListHead == NULL &&
1181                             pCurrentObject->Specific.Directory.ChildOpenReferenceCount == 0)
1182                         {
1183
1184                             AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1185
1186                             //
1187                             // Dropping the TreeLock permits the
1188                             // pCurrentObject->ObjectReferenceCount to change
1189                             //
1190
1191                             if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1192                                                 FALSE))
1193                             {
1194
1195                                 AFSAcquireExcl( &pCurrentObject->NonPagedInfo->ObjectInfoLock,
1196                                                 TRUE);
1197
1198                                 if ( pCurrentObject->ObjectReferenceCount <= 0)
1199                                 {
1200
1201                                     AFSRemoveFcb( &pCurrentObject->Fcb);
1202
1203                                     if( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
1204                                     {
1205
1206                                         AFSAcquireExcl( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock,
1207                                                         TRUE);
1208
1209                                         AFSRemoveFcb( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
1210
1211                                         AFSReleaseResource( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock);
1212
1213                                         AFSDeleteObjectInfo( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
1214
1215                                         ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
1216
1217                                         AFSExFreePoolWithTag( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged, AFS_DIR_ENTRY_NP_TAG);
1218
1219                                         AFSExFreePoolWithTag( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB, AFS_DIR_ENTRY_TAG);
1220                                     }
1221
1222                                     AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
1223
1224                                     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1225                                                   AFS_TRACE_LEVEL_VERBOSE,
1226                                                   "AFSPrimaryVolumeWorkerThread Deleting deleted object %p\n",
1227                                                   pCurrentObject);
1228
1229                                     AFSDeleteObjectInfo( pCurrentObject);
1230                                 }
1231                                 else
1232                                 {
1233
1234                                     AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
1235                                 }
1236
1237                                 AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1238
1239                                 pCurrentObject = pNextObject;
1240
1241                                 continue;
1242                             }
1243                             else
1244                             {
1245
1246                                 bReleaseVolumeLock = FALSE;
1247
1248                                 break;
1249                             }
1250                         }
1251
1252                         if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0 ||
1253                             ( pCurrentObject->Fcb != NULL &&
1254                               pCurrentObject->Fcb->OpenReferenceCount > 0) ||
1255                             pCurrentObject->Specific.Directory.DirectoryNodeListHead == NULL)
1256                         {
1257
1258                             pCurrentObject = pNextObject;
1259
1260                             continue;
1261                         }
1262
1263                         if( !AFSAcquireShared( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
1264                                                FALSE))
1265                         {
1266
1267                             pCurrentObject = pNextObject;
1268
1269                             continue;
1270                         }
1271
1272                         KeQueryTickCount( &liCurrentTime);
1273
1274                         pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
1275
1276                         while( pCurrentDirEntry != NULL)
1277                         {
1278
1279                             if( pCurrentDirEntry->DirOpenReferenceCount > 0 ||
1280                                 ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1281                                   pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0) ||
1282                                 liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
1283                                 liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
1284                                                                         pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart ||
1285                                 ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
1286                                    ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL ||
1287                                      pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)) ||
1288                                 ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE &&
1289                                   pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1290                                   pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0))
1291                             {
1292
1293                                 break;
1294                             }
1295
1296                             pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
1297                         }
1298
1299                         if( pCurrentDirEntry != NULL)
1300                         {
1301
1302                             AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1303
1304                             pCurrentObject = pNextObject;
1305
1306                             continue;
1307                         }
1308
1309                         AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1310
1311                         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1312
1313                         //
1314                         // Now acquire the locks excl
1315                         //
1316
1317                         if( AFSAcquireExcl( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
1318                                             FALSE))
1319                         {
1320
1321                             if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1322                                                 FALSE))
1323                             {
1324
1325                                 if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0)
1326                                 {
1327
1328                                     AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1329
1330                                     AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1331
1332                                     pCurrentObject = pNextObject;
1333
1334                                     continue;
1335                                 }
1336
1337                                 KeQueryTickCount( &liCurrentTime);
1338
1339                                 pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
1340
1341                                 while( pCurrentDirEntry != NULL)
1342                                 {
1343
1344                                     if( pCurrentDirEntry->DirOpenReferenceCount > 0 ||
1345                                         ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1346                                           pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0) ||
1347                                         liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
1348                                         liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
1349                                                                                 pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart ||
1350                                         ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
1351                                           ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL ||
1352                                             pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)) ||
1353                                         ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE &&
1354                                           pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1355                                           pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0))
1356                                     {
1357
1358                                         break;
1359                                     }
1360
1361                                     pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
1362                                 }
1363
1364                                 if( pCurrentDirEntry != NULL)
1365                                 {
1366
1367                                     AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1368
1369                                     AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1370
1371                                     pCurrentObject = pNextObject;
1372
1373                                     continue;
1374                                 }
1375
1376                                 pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
1377
1378                                 while( pCurrentDirEntry != NULL)
1379                                 {
1380
1381                                     pNextDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
1382
1383                                     pCurrentChildObject = pCurrentDirEntry->ObjectInformation;
1384
1385                                     pFcb = NULL;
1386
1387                                     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1388                                                   AFS_TRACE_LEVEL_VERBOSE,
1389                                                   "AFSPrimaryVolumeWorkerThread Deleting DE %wZ Object %p\n",
1390                                                   &pCurrentDirEntry->NameInformation.FileName,
1391                                                   pCurrentChildObject);
1392
1393                                     AFSDeleteDirEntry( pCurrentObject,
1394                                                        pCurrentDirEntry);
1395
1396
1397                                     //
1398                                     // Acquire ObjectInfoLock shared here so as not to deadlock
1399                                     // with an invalidation call from the service during AFSCleanupFcb
1400                                     //
1401
1402                                     lCount = AFSObjectInfoIncrement( pCurrentChildObject,
1403                                                                      AFS_OBJECT_REFERENCE_WORKER);
1404
1405                                     AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1406                                                   AFS_TRACE_LEVEL_VERBOSE,
1407                                                   "AFSPrimaryVolumeWorkerThread Increment count on object %p Cnt %d\n",
1408                                                   pCurrentChildObject,
1409                                                   lCount);
1410
1411                                     if( lCount == 1 &&
1412                                         pCurrentChildObject->Fcb != NULL &&
1413                                         pCurrentChildObject->FileType == AFS_FILE_TYPE_FILE)
1414                                     {
1415
1416                                         //
1417                                         // We must not hold pVolumeCB->ObjectInfoTree.TreeLock exclusive
1418                                         // across an AFSCleanupFcb call since it can deadlock with an
1419                                         // invalidation call from the service.
1420                                         //
1421
1422                                         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1423
1424                                         //
1425                                         // Cannot hold a TreeLock across an AFSCleanupFcb call
1426                                         // as it can deadlock with an invalidation ioctl initiated
1427                                         // from the service.
1428                                         //
1429                                         // Dropping the TreeLock permits the
1430                                         // pCurrentObject->ObjectReferenceCount to change
1431                                         //
1432
1433                                         ntStatus = AFSCleanupFcb( pCurrentChildObject->Fcb,
1434                                                                   TRUE);
1435
1436                                         if ( ntStatus == STATUS_RETRY)
1437                                         {
1438
1439                                             bFcbBusy = TRUE;
1440                                         }
1441
1442                                         AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1443                                                         TRUE);
1444                                     }
1445
1446                                     lCount = AFSObjectInfoDecrement( pCurrentChildObject,
1447                                                                      AFS_OBJECT_REFERENCE_WORKER);
1448
1449                                     AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1450                                                   AFS_TRACE_LEVEL_VERBOSE,
1451                                                   "AFSPrimaryVolumeWorkerThread Decrement1 count on object %p Cnt %d\n",
1452                                                   pCurrentChildObject,
1453                                                   lCount);
1454
1455                                     AFSAcquireExcl( &pCurrentChildObject->NonPagedInfo->ObjectInfoLock,
1456                                                     TRUE);
1457
1458                                     if( pCurrentChildObject->ObjectReferenceCount <= 0)
1459                                     {
1460
1461                                         AFSRemoveFcb( &pCurrentChildObject->Fcb);
1462
1463                                         if( pCurrentChildObject->FileType == AFS_FILE_TYPE_DIRECTORY &&
1464                                             pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
1465                                         {
1466
1467                                             AFSAcquireExcl( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock,
1468                                                             TRUE);
1469
1470                                             AFSRemoveFcb( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
1471
1472                                             AFSReleaseResource( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock);
1473
1474                                             AFSDeleteObjectInfo( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
1475
1476                                             ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
1477
1478                                             AFSExFreePoolWithTag( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged, AFS_DIR_ENTRY_NP_TAG);
1479
1480                                             AFSExFreePoolWithTag( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB, AFS_DIR_ENTRY_TAG);
1481                                         }
1482
1483                                         AFSReleaseResource( &pCurrentChildObject->NonPagedInfo->ObjectInfoLock);
1484
1485                                         AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1486                                                       AFS_TRACE_LEVEL_VERBOSE,
1487                                                       "AFSPrimaryVolumeWorkerThread Deleting object %p\n",
1488                                                       pCurrentChildObject);
1489
1490                                         AFSDeleteObjectInfo( pCurrentChildObject);
1491                                     }
1492                                     else
1493                                     {
1494
1495                                         AFSReleaseResource( &pCurrentChildObject->NonPagedInfo->ObjectInfoLock);
1496                                     }
1497
1498                                     pCurrentDirEntry = pNextDirEntry;
1499
1500                                 }
1501
1502                                 pCurrentObject->Specific.Directory.DirectoryNodeListHead = NULL;
1503
1504                                 pCurrentObject->Specific.Directory.DirectoryNodeListTail = NULL;
1505
1506                                 pCurrentObject->Specific.Directory.ShortNameTree = NULL;
1507
1508                                 pCurrentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead = NULL;
1509
1510                                 pCurrentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead = NULL;
1511
1512                                 pCurrentObject->Specific.Directory.DirectoryNodeCount = 0;
1513
1514                                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIR_NODE_COUNT,
1515                                               AFS_TRACE_LEVEL_VERBOSE,
1516                                               "AFSPrimaryVolumeWorkerThread Reset count to 0 on parent FID %08lX-%08lX-%08lX-%08lX\n",
1517                                               pCurrentObject->FileId.Cell,
1518                                               pCurrentObject->FileId.Volume,
1519                                               pCurrentObject->FileId.Vnode,
1520                                               pCurrentObject->FileId.Unique);
1521
1522                                 //
1523                                 // Clear our enumerated flag on this object so we retrieve info again on next access
1524                                 //
1525
1526                                 ClearFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1527
1528                                 AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1529
1530                                 AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1531                             }
1532                             else
1533                             {
1534
1535                                 AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1536
1537                                 bReleaseVolumeLock = FALSE;
1538
1539                                 break;
1540                             }
1541                         }
1542                         else
1543                         {
1544
1545                             //
1546                             // Try to grab the volume lock again ... no problem if we don't
1547                             //
1548
1549                             if( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1550                                                  FALSE))
1551                             {
1552
1553                                 bReleaseVolumeLock = FALSE;
1554
1555                                 break;
1556                             }
1557                         }
1558
1559                         if( pCurrentObject != &pVolumeCB->ObjectInformation)
1560                         {
1561
1562                             pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
1563
1564                             if( pCurrentObject == NULL &&
1565                                 pVolumeCB != AFSGlobalRoot)
1566                             {
1567
1568                                 pCurrentObject = &pVolumeCB->ObjectInformation;
1569                             }
1570                         }
1571                         else
1572                         {
1573
1574                             pCurrentObject = NULL;
1575                         }
1576
1577                         continue;
1578                     }
1579                     else if( pCurrentObject->FileType == AFS_FILE_TYPE_FILE)
1580                     {
1581
1582                         lCount = AFSObjectInfoIncrement( pCurrentObject,
1583                                                          AFS_OBJECT_REFERENCE_WORKER);
1584
1585                         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1586                                       AFS_TRACE_LEVEL_VERBOSE,
1587                                       "AFSPrimaryVolumeWorkerThread Increment2 count on object %p Cnt %d\n",
1588                                       pCurrentObject,
1589                                       lCount);
1590
1591                         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1592
1593                         if( pCurrentObject->Fcb != NULL)
1594                         {
1595
1596                             //
1597                             // Dropping the TreeLock permits the
1598                             // pCurrentObject->ObjectReferenceCount to change
1599                             //
1600
1601                             ntStatus = AFSCleanupFcb( pCurrentObject->Fcb,
1602                                                       FALSE);
1603
1604                             if ( ntStatus == STATUS_RETRY)
1605                             {
1606
1607                                 bFcbBusy = TRUE;
1608                             }
1609                         }
1610
1611                         lCount = AFSObjectInfoDecrement( pCurrentObject,
1612                                                          AFS_OBJECT_REFERENCE_WORKER);
1613
1614                         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1615                                       AFS_TRACE_LEVEL_VERBOSE,
1616                                       "AFSPrimaryVolumeWorkerThread Decrement2 count on object %p Cnt %d\n",
1617                                       pCurrentObject,
1618                                       lCount);
1619
1620                         if( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1621                                              FALSE))
1622                         {
1623
1624                             bReleaseVolumeLock = FALSE;
1625
1626                             break;
1627                         }
1628
1629                         AFSAcquireExcl( &pCurrentObject->NonPagedInfo->ObjectInfoLock,
1630                                         TRUE);
1631
1632                         if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DELETED) &&
1633                             pCurrentObject->ObjectReferenceCount <= 0)
1634                         {
1635
1636                             AFSRemoveFcb( &pCurrentObject->Fcb);
1637
1638                             AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
1639
1640                             AFSDeleteObjectInfo( pCurrentObject);
1641                         }
1642                         else
1643                         {
1644
1645                             AFSReleaseResource( &pCurrentObject->NonPagedInfo->ObjectInfoLock);
1646                         }
1647
1648                         AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1649
1650                         pCurrentObject = pNextObject;
1651
1652                         continue;
1653                     }
1654
1655                     pCurrentObject = pNextObject;
1656                 }
1657
1658                 if( bReleaseVolumeLock)
1659                 {
1660
1661                     AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1662                 }
1663             }
1664
1665             //
1666             // Next volume cb
1667             //
1668
1669             AFSReleaseResource( pVolumeCB->VolumeLock);
1670
1671             AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
1672                               TRUE);
1673
1674             pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
1675         }
1676
1677         AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1678
1679     } // worker thread loop
1680
1681     KeCancelTimer( &Timer);
1682
1683     ClearFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
1684
1685     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1686                   AFS_TRACE_LEVEL_VERBOSE,
1687                   "AFSPrimaryVolumeWorkerThread Exiting\n");
1688
1689     lCount = InterlockedDecrement( &pControlDeviceExt->Specific.Control.VolumeWorkerThreadCount);
1690
1691     if( lCount == 0)
1692     {
1693
1694         KeSetEvent( &pControlDeviceExt->Specific.Control.VolumeWorkerCloseEvent,
1695                     0,
1696                     FALSE);
1697     }
1698
1699     PsTerminateSystemThread( 0);
1700
1701     return;
1702 }
1703
1704 NTSTATUS
1705 AFSInsertWorkitem( IN AFSWorkItem *WorkItem)
1706 {
1707
1708     NTSTATUS ntStatus = STATUS_SUCCESS;
1709     AFSDeviceExt *pDevExt = NULL;
1710     LONG lCount;
1711
1712     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1713
1714     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1715                   AFS_TRACE_LEVEL_VERBOSE,
1716                   "AFSInsertWorkitem Acquiring Control QueueLock lock %p EXCL %08lX\n",
1717                   &pDevExt->Specific.Library.QueueLock,
1718                   PsGetCurrentThread());
1719
1720     AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
1721                     TRUE);
1722
1723     lCount = InterlockedIncrement( &pDevExt->Specific.Library.QueueItemCount);
1724
1725     AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1726                   AFS_TRACE_LEVEL_VERBOSE,
1727                   "AFSInsertWorkitem Inserting work item %p Count %d\n",
1728                   WorkItem,
1729                   lCount);
1730
1731     if( pDevExt->Specific.Library.QueueTail != NULL) // queue already has nodes
1732     {
1733
1734         pDevExt->Specific.Library.QueueTail->next = WorkItem;
1735     }
1736     else // first node
1737     {
1738
1739         pDevExt->Specific.Library.QueueHead = WorkItem;
1740     }
1741
1742     WorkItem->next = NULL;
1743     pDevExt->Specific.Library.QueueTail = WorkItem;
1744
1745     // indicate that the queue has nodes
1746     KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
1747                 0,
1748                 FALSE);
1749
1750     AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
1751
1752     return ntStatus;
1753 }
1754
1755 NTSTATUS
1756 AFSInsertIOWorkitem( IN AFSWorkItem *WorkItem)
1757 {
1758
1759     NTSTATUS ntStatus = STATUS_SUCCESS;
1760     AFSDeviceExt *pDevExt = NULL;
1761     LONG lCount;
1762
1763     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1764
1765     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1766                   AFS_TRACE_LEVEL_VERBOSE,
1767                   "AFSInsertIOWorkitem Acquiring Control QueueLock lock %p EXCL %08lX\n",
1768                   &pDevExt->Specific.Library.IOQueueLock,
1769                   PsGetCurrentThread());
1770
1771     AFSAcquireExcl( &pDevExt->Specific.Library.IOQueueLock,
1772                     TRUE);
1773
1774     lCount = InterlockedIncrement( &pDevExt->Specific.Library.IOQueueItemCount);
1775
1776     AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1777                   AFS_TRACE_LEVEL_VERBOSE,
1778                   "AFSInsertWorkitem Inserting IO work item %p Count %d\n",
1779                   WorkItem,
1780                   lCount);
1781
1782     if( pDevExt->Specific.Library.IOQueueTail != NULL) // queue already has nodes
1783     {
1784
1785         pDevExt->Specific.Library.IOQueueTail->next = WorkItem;
1786     }
1787     else // first node
1788     {
1789
1790         pDevExt->Specific.Library.IOQueueHead = WorkItem;
1791     }
1792
1793     WorkItem->next = NULL;
1794     pDevExt->Specific.Library.IOQueueTail = WorkItem;
1795
1796     // indicate that the queue has nodes
1797     KeSetEvent( &(pDevExt->Specific.Library.IOWorkerQueueHasItems),
1798                 0,
1799                 FALSE);
1800
1801     AFSReleaseResource( &pDevExt->Specific.Library.IOQueueLock);
1802
1803     return ntStatus;
1804 }
1805
1806 NTSTATUS
1807 AFSInsertWorkitemAtHead( IN AFSWorkItem *WorkItem)
1808 {
1809
1810     NTSTATUS ntStatus = STATUS_SUCCESS;
1811     AFSDeviceExt *pDevExt = NULL;
1812     LONG lCount;
1813
1814     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1815
1816     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1817                   AFS_TRACE_LEVEL_VERBOSE,
1818                   "AFSInsertWorkitemAtHead Acquiring Control QueueLock lock %p EXCL %08lX\n",
1819                   &pDevExt->Specific.Library.QueueLock,
1820                   PsGetCurrentThread());
1821
1822     AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
1823                     TRUE);
1824
1825     WorkItem->next = pDevExt->Specific.Library.QueueHead;
1826
1827     pDevExt->Specific.Library.QueueHead = WorkItem;
1828
1829     lCount = InterlockedIncrement( &pDevExt->Specific.Library.QueueItemCount);
1830
1831     AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1832                   AFS_TRACE_LEVEL_VERBOSE,
1833                   "AFSInsertWorkitemAtHead Inserting work item %p Count %d\n",
1834                   WorkItem,
1835                   lCount);
1836
1837     //
1838     // indicate that the queue has nodes
1839     //
1840
1841     KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
1842                 0,
1843                 FALSE);
1844
1845     AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
1846
1847     return ntStatus;
1848 }
1849
1850 AFSWorkItem *
1851 AFSRemoveWorkItem()
1852 {
1853
1854     AFSWorkItem        *pWorkItem = NULL;
1855     AFSDeviceExt *pDevExt = NULL;
1856     LONG lCount;
1857
1858     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1859
1860     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1861                   AFS_TRACE_LEVEL_VERBOSE,
1862                   "AFSRemoveWorkItem Acquiring Control QueueLock lock %p EXCL %08lX\n",
1863                   &pDevExt->Specific.Library.QueueLock,
1864                   PsGetCurrentThread());
1865
1866     AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
1867                     TRUE);
1868
1869     if( pDevExt->Specific.Library.QueueHead != NULL) // queue has nodes
1870     {
1871
1872         pWorkItem = pDevExt->Specific.Library.QueueHead;
1873
1874         lCount = InterlockedDecrement( &pDevExt->Specific.Library.QueueItemCount);
1875
1876         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1877                       AFS_TRACE_LEVEL_VERBOSE,
1878                       "AFSRemoveWorkItem Removing work item %p Count %d Thread %08lX\n",
1879                       pWorkItem,
1880                       lCount,
1881                       PsGetCurrentThreadId());
1882
1883         pDevExt->Specific.Library.QueueHead = pDevExt->Specific.Library.QueueHead->next;
1884
1885         if( pDevExt->Specific.Library.QueueHead == NULL) // if queue just became empty
1886         {
1887
1888             pDevExt->Specific.Library.QueueTail = NULL;
1889         }
1890         else
1891         {
1892
1893             //
1894             // Wake up another worker
1895             //
1896
1897             KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
1898                         0,
1899                         FALSE);
1900         }
1901     }
1902
1903     AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
1904
1905     return pWorkItem;
1906 }
1907
1908 AFSWorkItem *
1909 AFSRemoveIOWorkItem()
1910 {
1911
1912     AFSWorkItem        *pWorkItem = NULL;
1913     AFSDeviceExt *pDevExt = NULL;
1914     LONG lCount;
1915
1916     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1917
1918     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1919                   AFS_TRACE_LEVEL_VERBOSE,
1920                   "AFSRemoveIOWorkItem Acquiring Control QueueLock lock %p EXCL %08lX\n",
1921                   &pDevExt->Specific.Library.IOQueueLock,
1922                   PsGetCurrentThread());
1923
1924     AFSAcquireExcl( &pDevExt->Specific.Library.IOQueueLock,
1925                     TRUE);
1926
1927     if( pDevExt->Specific.Library.IOQueueHead != NULL) // queue has nodes
1928     {
1929
1930         pWorkItem = pDevExt->Specific.Library.IOQueueHead;
1931
1932         lCount = InterlockedDecrement( &pDevExt->Specific.Library.IOQueueItemCount);
1933
1934         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1935                       AFS_TRACE_LEVEL_VERBOSE,
1936                       "AFSRemoveWorkItem Removing work item %p Count %d Thread %08lX\n",
1937                       pWorkItem,
1938                       lCount,
1939                       PsGetCurrentThreadId());
1940
1941         pDevExt->Specific.Library.IOQueueHead = pDevExt->Specific.Library.IOQueueHead->next;
1942
1943         if( pDevExt->Specific.Library.IOQueueHead == NULL) // if queue just became empty
1944         {
1945
1946             pDevExt->Specific.Library.IOQueueTail = NULL;
1947         }
1948         else
1949         {
1950
1951             //
1952             // Wake up another worker
1953             //
1954
1955             KeSetEvent( &(pDevExt->Specific.Library.IOWorkerQueueHasItems),
1956                         0,
1957                         FALSE);
1958         }
1959     }
1960
1961     AFSReleaseResource( &pDevExt->Specific.Library.IOQueueLock);
1962
1963     return pWorkItem;
1964 }
1965
1966 NTSTATUS
1967 AFSQueueWorkerRequest( IN AFSWorkItem *WorkItem)
1968 {
1969
1970     NTSTATUS ntStatus = STATUS_SUCCESS;
1971     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
1972
1973     //
1974     // Submit the work item to the worker
1975     //
1976
1977     ntStatus = AFSInsertWorkitem( WorkItem);
1978
1979     if( bWait)
1980     {
1981
1982         //
1983         // Sync request so block on the work item event
1984         //
1985
1986         ntStatus = KeWaitForSingleObject( &WorkItem->Event,
1987                                           Executive,
1988                                           KernelMode,
1989                                           FALSE,
1990                                           NULL);
1991     }
1992
1993     return ntStatus;
1994 }
1995
1996 NTSTATUS
1997 AFSQueueIOWorkerRequest( IN AFSWorkItem *WorkItem)
1998 {
1999
2000     NTSTATUS ntStatus = STATUS_SUCCESS;
2001     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
2002
2003     //
2004     // Submit the work item to the worker
2005     //
2006
2007     ntStatus = AFSInsertIOWorkitem( WorkItem);
2008
2009     if( bWait)
2010     {
2011
2012         //
2013         // Sync request so block on the work item event
2014         //
2015
2016         ntStatus = KeWaitForSingleObject( &WorkItem->Event,
2017                                           Executive,
2018                                           KernelMode,
2019                                           FALSE,
2020                                           NULL);
2021     }
2022
2023     return ntStatus;
2024 }
2025
2026 NTSTATUS
2027 AFSQueueWorkerRequestAtHead( IN AFSWorkItem *WorkItem)
2028 {
2029
2030     NTSTATUS ntStatus = STATUS_SUCCESS;
2031     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
2032
2033     //
2034     // Submit the work item to the worker
2035     //
2036
2037     ntStatus = AFSInsertWorkitemAtHead( WorkItem);
2038
2039     if( bWait)
2040     {
2041
2042         //
2043         // Sync request so block on the work item event
2044         //
2045
2046         ntStatus = KeWaitForSingleObject( &WorkItem->Event,
2047                                           Executive,
2048                                           KernelMode,
2049                                           FALSE,
2050                                           NULL);
2051     }
2052
2053     return ntStatus;
2054 }
2055
2056 NTSTATUS
2057 AFSQueueFlushExtents( IN AFSFcb *Fcb,
2058                       IN GUID *AuthGroup)
2059 {
2060
2061     NTSTATUS ntStatus = STATUS_SUCCESS;
2062     AFSDeviceExt *pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2063     AFSWorkItem *pWorkItem = NULL;
2064     LONG lCount;
2065
2066     __try
2067     {
2068
2069         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2070                       AFS_TRACE_LEVEL_VERBOSE,
2071                       "AFSQueueFlushExtents Queuing request for FID %08lX-%08lX-%08lX-%08lX\n",
2072                       Fcb->ObjectInformation->FileId.Cell,
2073                       Fcb->ObjectInformation->FileId.Volume,
2074                       Fcb->ObjectInformation->FileId.Vnode,
2075                       Fcb->ObjectInformation->FileId.Unique);
2076
2077         //
2078         // Increment our flush count here just to keep the number of items in the
2079         // queue down. We'll decrement it just below.
2080         //
2081
2082         lCount = InterlockedIncrement( &Fcb->Specific.File.QueuedFlushCount);
2083
2084         if( lCount > 3)
2085         {
2086
2087             AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2088                           AFS_TRACE_LEVEL_VERBOSE,
2089                           "AFSQueueFlushExtents Max queued items for FID %08lX-%08lX-%08lX-%08lX\n",
2090                           Fcb->ObjectInformation->FileId.Cell,
2091                           Fcb->ObjectInformation->FileId.Volume,
2092                           Fcb->ObjectInformation->FileId.Vnode,
2093                           Fcb->ObjectInformation->FileId.Unique);
2094
2095             try_return( ntStatus);
2096         }
2097
2098         if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
2099         {
2100
2101             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2102                           AFS_TRACE_LEVEL_ERROR,
2103                           "AFSQueueFlushExtents Failing request, in shutdown\n");
2104
2105             try_return( ntStatus = STATUS_TOO_LATE);
2106         }
2107
2108         //
2109         // Allocate our request structure and send it to the worker
2110         //
2111
2112         pWorkItem = (AFSWorkItem *)AFSLibExAllocatePoolWithTag( NonPagedPool,
2113                                                                 sizeof( AFSWorkItem),
2114                                                                 AFS_WORK_ITEM_TAG);
2115
2116         if( pWorkItem == NULL)
2117         {
2118
2119             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2120                           AFS_TRACE_LEVEL_ERROR,
2121                           "AFSQueueFlushExtents Failed to allocate work item\n");
2122
2123             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2124         }
2125
2126         RtlZeroMemory( pWorkItem,
2127                        sizeof( AFSWorkItem));
2128
2129         pWorkItem->Size = sizeof( AFSWorkItem);
2130
2131         pWorkItem->ProcessID = (ULONGLONG)PsGetCurrentProcessId();
2132
2133         pWorkItem->RequestType = AFS_WORK_FLUSH_FCB;
2134
2135         if ( AuthGroup == NULL)
2136         {
2137
2138             RtlZeroMemory( &pWorkItem->AuthGroup,
2139                            sizeof( GUID));
2140
2141             ntStatus = AFSRetrieveValidAuthGroup( Fcb,
2142                                                   NULL,
2143                                                   TRUE,
2144                                                   &pWorkItem->AuthGroup);
2145         }
2146         else
2147         {
2148             RtlCopyMemory( &pWorkItem->AuthGroup,
2149                            AuthGroup,
2150                            sizeof( GUID));
2151         }
2152
2153         pWorkItem->Specific.Fcb.Fcb = Fcb;
2154
2155         lCount = InterlockedIncrement( &Fcb->OpenReferenceCount);
2156
2157         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2158                       AFS_TRACE_LEVEL_VERBOSE,
2159                       "AFSQueueFlushExtents Increment count on Fcb %p Cnt %d\n",
2160                       Fcb,
2161                       lCount);
2162
2163         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2164                       AFS_TRACE_LEVEL_VERBOSE,
2165                       "AFSQueueFlushExtents Workitem %p for FID %08lX-%08lX-%08lX-%08lX\n",
2166                       pWorkItem,
2167                       Fcb->ObjectInformation->FileId.Cell,
2168                       Fcb->ObjectInformation->FileId.Volume,
2169                       Fcb->ObjectInformation->FileId.Vnode,
2170                       Fcb->ObjectInformation->FileId.Unique);
2171
2172         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2173
2174 try_exit:
2175
2176         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2177                       AFS_TRACE_LEVEL_VERBOSE,
2178                       "AFSQueueFlushExtents Request complete Status %08lX FID %08lX-%08lX-%08lX-%08lX\n",
2179                       Fcb->ObjectInformation->FileId.Cell,
2180                       Fcb->ObjectInformation->FileId.Volume,
2181                       Fcb->ObjectInformation->FileId.Vnode,
2182                       Fcb->ObjectInformation->FileId.Unique,
2183                       ntStatus);
2184
2185         //
2186         // Remove the count we added above
2187         //
2188
2189         lCount = InterlockedDecrement( &Fcb->Specific.File.QueuedFlushCount);
2190
2191         ASSERT( lCount >= 0);
2192
2193         if( lCount == 0)
2194         {
2195
2196             KeSetEvent( &Fcb->NPFcb->Specific.File.QueuedFlushEvent,
2197                         0,
2198                         FALSE);
2199         }
2200
2201         if( !NT_SUCCESS( ntStatus))
2202         {
2203
2204             if( pWorkItem != NULL)
2205             {
2206
2207                 lCount = InterlockedDecrement( &Fcb->OpenReferenceCount);
2208
2209                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2210             }
2211
2212             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2213                           AFS_TRACE_LEVEL_ERROR,
2214                           "AFSQueueFlushExtents Failed to queue request Status %08lX\n", ntStatus);
2215         }
2216     }
2217     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2218     {
2219
2220         AFSDbgLogMsg( 0,
2221                       0,
2222                       "EXCEPTION - AFSQueueFlushExtents\n");
2223
2224         AFSDumpTraceFilesFnc();
2225     }
2226
2227     return ntStatus;
2228 }
2229
2230 NTSTATUS
2231 AFSQueueGlobalRootEnumeration()
2232 {
2233
2234     NTSTATUS ntStatus = STATUS_SUCCESS;
2235     AFSWorkItem *pWorkItem = NULL;
2236
2237     __try
2238     {
2239
2240         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2241                                                                  sizeof(AFSWorkItem),
2242                                                                  AFS_WORK_ITEM_TAG);
2243         if (NULL == pWorkItem)
2244         {
2245
2246             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2247                           AFS_TRACE_LEVEL_ERROR,
2248                           "AFSQueueGlobalRootEnumeration Failed to allocate work item\n");
2249
2250             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2251         }
2252
2253         RtlZeroMemory( pWorkItem,
2254                        sizeof(AFSWorkItem));
2255
2256         pWorkItem->Size = sizeof( AFSWorkItem);
2257
2258         pWorkItem->RequestType = AFS_WORK_ENUMERATE_GLOBAL_ROOT;
2259
2260         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2261                       AFS_TRACE_LEVEL_VERBOSE,
2262                       "AFSQueueGlobalRootEnumeration Workitem %p\n",
2263                       pWorkItem);
2264
2265         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2266
2267 try_exit:
2268
2269         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2270                       AFS_TRACE_LEVEL_VERBOSE,
2271                       "AFSQueueGlobalRootEnumeration Request complete Status %08lX\n",
2272                       ntStatus);
2273
2274         if( !NT_SUCCESS( ntStatus))
2275         {
2276
2277             if( pWorkItem != NULL)
2278             {
2279
2280                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2281             }
2282
2283             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2284                           AFS_TRACE_LEVEL_ERROR,
2285                           "AFSQueueGlobalRootEnumeration Failed to queue request Status %08lX\n",
2286                           ntStatus);
2287         }
2288     }
2289     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2290     {
2291
2292         AFSDbgLogMsg( 0,
2293                       0,
2294                       "EXCEPTION - AFSQueueGlobalRootEnumeration\n");
2295
2296         AFSDumpTraceFilesFnc();
2297     }
2298
2299     return ntStatus;
2300 }
2301
2302 NTSTATUS
2303 AFSQueueStartIos( IN PFILE_OBJECT CacheFileObject,
2304                   IN UCHAR FunctionCode,
2305                   IN ULONG RequestFlags,
2306                   IN AFSIoRun *IoRuns,
2307                   IN ULONG RunCount,
2308                   IN AFSGatherIo *GatherIo)
2309 {
2310
2311     NTSTATUS ntStatus = STATUS_SUCCESS;
2312     AFSDeviceExt *pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2313     AFSWorkItem *pWorkItem = NULL;
2314
2315     __try
2316     {
2317
2318         if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
2319         {
2320
2321             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2322                           AFS_TRACE_LEVEL_ERROR,
2323                           "AFSQueueStartIos Failing request, in shutdown\n");
2324
2325             try_return( ntStatus = STATUS_TOO_LATE);
2326         }
2327
2328         //
2329         // Allocate our request structure and send it to the worker
2330         //
2331
2332         pWorkItem = (AFSWorkItem *)AFSLibExAllocatePoolWithTag( NonPagedPool,
2333                                                                 sizeof( AFSWorkItem),
2334                                                                 AFS_WORK_ITEM_TAG);
2335
2336         if( pWorkItem == NULL)
2337         {
2338
2339             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2340                           AFS_TRACE_LEVEL_ERROR,
2341                           "AFSQueueStartIos Failed to allocate work item\n");
2342
2343             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2344         }
2345
2346         RtlZeroMemory( pWorkItem,
2347                        sizeof( AFSWorkItem));
2348
2349         KeInitializeEvent( &pWorkItem->Event,
2350                            NotificationEvent,
2351                            FALSE);
2352
2353         pWorkItem->Size = sizeof( AFSWorkItem);
2354
2355         pWorkItem->ProcessID = (ULONGLONG)PsGetCurrentProcessId();
2356
2357         pWorkItem->RequestType = AFS_WORK_START_IOS;
2358
2359         pWorkItem->Specific.CacheAccess.CacheFileObject = CacheFileObject;
2360
2361         pWorkItem->Specific.CacheAccess.FunctionCode = FunctionCode;
2362
2363         pWorkItem->Specific.CacheAccess.RequestFlags = RequestFlags;
2364
2365         pWorkItem->Specific.CacheAccess.IoRuns = IoRuns;
2366
2367         pWorkItem->Specific.CacheAccess.RunCount = RunCount;
2368
2369         pWorkItem->Specific.CacheAccess.GatherIo = GatherIo;
2370
2371         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2372                       AFS_TRACE_LEVEL_VERBOSE,
2373                       "AFSQueueStartIos Queuing IO Workitem %p\n",
2374                       pWorkItem);
2375
2376         ntStatus = AFSQueueIOWorkerRequest( pWorkItem);
2377
2378 try_exit:
2379
2380         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2381                       AFS_TRACE_LEVEL_VERBOSE,
2382                       "AFSQueueStartIos Request complete Status %08lX\n",
2383                       ntStatus);
2384
2385         if( !NT_SUCCESS( ntStatus))
2386         {
2387
2388             if( pWorkItem != NULL)
2389             {
2390
2391                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2392             }
2393         }
2394     }
2395     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2396     {
2397
2398         AFSDbgLogMsg( 0,
2399                       0,
2400                       "EXCEPTION - AFSQueueStartIos\n");
2401
2402         AFSDumpTraceFilesFnc();
2403     }
2404
2405     return ntStatus;
2406 }
2407
2408 NTSTATUS
2409 AFSQueueInvalidateObject( IN AFSObjectInfoCB *ObjectInfo,
2410                           IN ULONG InvalidateReason)
2411 {
2412
2413     NTSTATUS ntStatus = STATUS_SUCCESS;
2414     AFSWorkItem *pWorkItem = NULL;
2415
2416     __try
2417     {
2418
2419         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2420                                                                  sizeof(AFSWorkItem),
2421                                                                  AFS_WORK_ITEM_TAG);
2422         if (NULL == pWorkItem)
2423         {
2424
2425             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2426                           AFS_TRACE_LEVEL_ERROR,
2427                           "AFSQueueInvalidateObject Failed to allocate work item\n");
2428
2429             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2430         }
2431
2432         RtlZeroMemory( pWorkItem,
2433                        sizeof(AFSWorkItem));
2434
2435         pWorkItem->Size = sizeof( AFSWorkItem);
2436
2437         pWorkItem->RequestType = AFS_WORK_INVALIDATE_OBJECT;
2438
2439         pWorkItem->Specific.Invalidate.ObjectInfo = ObjectInfo;
2440
2441         pWorkItem->Specific.Invalidate.InvalidateReason = InvalidateReason;
2442
2443         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2444                       AFS_TRACE_LEVEL_VERBOSE,
2445                       "AFSQueueInvalidateObject Workitem %p\n",
2446                       pWorkItem);
2447
2448         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2449
2450 try_exit:
2451
2452         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2453                       AFS_TRACE_LEVEL_VERBOSE,
2454                       "AFSQueueInvalidateObject Request complete Status %08lX\n",
2455                       ntStatus);
2456
2457         if( !NT_SUCCESS( ntStatus))
2458         {
2459
2460             if( pWorkItem != NULL)
2461             {
2462                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2463             }
2464
2465             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2466                           AFS_TRACE_LEVEL_ERROR,
2467                           "AFSQueueInvalidateObject Failed to queue request Status %08lX\n",
2468                           ntStatus);
2469         }
2470     }
2471     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2472     {
2473
2474         AFSDbgLogMsg( 0,
2475                       0,
2476                       "EXCEPTION - AFSQueueInvalidateObject\n");
2477
2478         AFSDumpTraceFilesFnc();
2479     }
2480
2481     return ntStatus;
2482 }