f0ec3eb8d4466e748355f72d2d287aaf22afc7fa
[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 AFSShutdownWorkerThread()
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             pStartRoutine = AFSPrimaryVolumeWorkerThread;
396         }
397         else
398         {
399
400             pStartRoutine = AFSVolumeWorkerThread;
401         }
402
403         //
404         // Initialize the worker thread
405         //
406
407         KeInitializeEvent( &pWorker->WorkerThreadReady,
408                            NotificationEvent,
409                            FALSE);
410
411         //
412         // Set the worker to process requests
413         //
414
415         pWorker->State = AFS_WORKER_PROCESS_REQUESTS;
416
417         //
418         // Launch the thread
419         //
420
421         ntStatus =  PsCreateSystemThread( &hThread,
422                                           0,
423                                           NULL,
424                                           NULL,
425                                           NULL,
426                                           pStartRoutine,
427                                           (void *)VolumeCB);
428
429         if( NT_SUCCESS( ntStatus))
430         {
431
432             ObReferenceObjectByHandle( hThread,
433                                        GENERIC_READ | GENERIC_WRITE,
434                                        NULL,
435                                        KernelMode,
436                                        (PVOID *)&pWorker->WorkerThreadObject,
437                                        NULL);
438
439             ntStatus = KeWaitForSingleObject( &pWorker->WorkerThreadReady,
440                                               Executive,
441                                               KernelMode,
442                                               FALSE,
443                                               NULL);
444
445             lCount = InterlockedIncrement( &pControlDeviceExt->Specific.Control.VolumeWorkerThreadCount);
446
447             if( lCount > 0)
448             {
449
450                 KeClearEvent( &pControlDeviceExt->Specific.Control.VolumeWorkerCloseEvent);
451             }
452
453             ZwClose( hThread);
454         }
455     }
456
457     return ntStatus;
458 }
459
460 //
461 // Function: AFSInitWorkerThread
462 //
463 // Description:
464 //
465 //      This function initializes a worker thread in the pool
466 //
467 // Return:
468 //
469 //      A status is returned for the function
470 //
471
472 NTSTATUS
473 AFSInitWorkerThread( IN AFSWorkQueueContext *PoolContext,
474                      IN PKSTART_ROUTINE WorkerRoutine)
475 {
476
477     NTSTATUS ntStatus = STATUS_SUCCESS;
478     HANDLE Handle;
479
480     //
481     // INitialize the worker signal thread
482     //
483
484     KeInitializeEvent( &PoolContext->WorkerThreadReady,
485                        NotificationEvent,
486                        FALSE);
487
488     //
489     // Set the worker to process requests
490     //
491
492     PoolContext->State = AFS_WORKER_PROCESS_REQUESTS;
493
494     //
495     // Launch the thread
496     //
497
498     ntStatus =  PsCreateSystemThread( &Handle,
499                                       0,
500                                       NULL,
501                                       NULL,
502                                       NULL,
503                                       WorkerRoutine,
504                                       (void *)PoolContext);
505
506     if( NT_SUCCESS( ntStatus))
507     {
508
509         ObReferenceObjectByHandle( Handle,
510                                    GENERIC_READ | GENERIC_WRITE,
511                                    NULL,
512                                    KernelMode,
513                                    (PVOID *)&PoolContext->WorkerThreadObject,
514                                    NULL);
515
516         ntStatus = KeWaitForSingleObject( &PoolContext->WorkerThreadReady,
517                                           Executive,
518                                           KernelMode,
519                                           FALSE,
520                                           NULL);
521
522         ZwClose( Handle);
523     }
524
525     return ntStatus;
526 }
527
528 NTSTATUS
529 AFSShutdownVolumeWorker( IN AFSVolumeCB *VolumeCB)
530 {
531
532     NTSTATUS ntStatus = STATUS_SUCCESS;
533     AFSWorkQueueContext *pWorker = &VolumeCB->VolumeWorkerContext;
534
535     if( pWorker->WorkerThreadObject != NULL &&
536         BooleanFlagOn( pWorker->State, AFS_WORKER_INITIALIZED))
537     {
538
539         //
540         // Clear the 'keep processing' flag
541         //
542
543         ClearFlag( pWorker->State, AFS_WORKER_PROCESS_REQUESTS);
544
545         ntStatus = KeWaitForSingleObject( pWorker->WorkerThreadObject,
546                                           Executive,
547                                           KernelMode,
548                                           FALSE,
549                                           NULL);
550
551         ObDereferenceObject( pWorker->WorkerThreadObject);
552
553         pWorker->WorkerThreadObject = NULL;
554     }
555
556     return ntStatus;
557 }
558
559 //
560 // Function: AFSShutdownWorkerThread
561 //
562 // Description:
563 //
564 //      This function shutsdown a worker thread in the pool
565 //
566 // Return:
567 //
568 //      A status is returned for the function
569 //
570
571 NTSTATUS
572 AFSShutdownWorkerThread( IN AFSWorkQueueContext *PoolContext)
573 {
574
575     NTSTATUS ntStatus = STATUS_SUCCESS;
576     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
577
578     if( PoolContext->WorkerThreadObject != NULL &&
579         BooleanFlagOn( PoolContext->State, AFS_WORKER_INITIALIZED))
580     {
581
582         //
583         // Wake up the thread if it is a sleep
584         //
585
586         KeSetEvent( &pDeviceExt->Specific.Library.WorkerQueueHasItems,
587                     0,
588                     FALSE);
589
590         ntStatus = KeWaitForSingleObject( PoolContext->WorkerThreadObject,
591                                           Executive,
592                                           KernelMode,
593                                           FALSE,
594                                           NULL);
595
596         ObDereferenceObject( PoolContext->WorkerThreadObject);
597
598         PoolContext->WorkerThreadObject = NULL;
599     }
600
601     return ntStatus;
602 }
603
604 //
605 // Function: AFSShutdownIOWorkerThread
606 //
607 // Description:
608 //
609 //      This function shutsdown an IO worker thread in the pool
610 //
611 // Return:
612 //
613 //      A status is returned for the function
614 //
615
616 NTSTATUS
617 AFSShutdownIOWorkerThread( IN AFSWorkQueueContext *PoolContext)
618 {
619
620     NTSTATUS ntStatus = STATUS_SUCCESS;
621     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
622
623     if( PoolContext->WorkerThreadObject != NULL &&
624         BooleanFlagOn( PoolContext->State, AFS_WORKER_INITIALIZED))
625     {
626
627         //
628         // Wake up the thread if it is a sleep
629         //
630
631         KeSetEvent( &pDeviceExt->Specific.Library.IOWorkerQueueHasItems,
632                     0,
633                     FALSE);
634
635         ntStatus = KeWaitForSingleObject( PoolContext->WorkerThreadObject,
636                                           Executive,
637                                           KernelMode,
638                                           FALSE,
639                                           NULL);
640
641         ObDereferenceObject( PoolContext->WorkerThreadObject);
642
643         PoolContext->WorkerThreadObject = NULL;
644     }
645
646     return ntStatus;
647 }
648
649 //
650 // Function: AFSWorkerThread
651 //
652 // Description:
653 //
654 //      This is the worker thread entry point.
655 //
656 // Return:
657 //
658 //      A status is returned for the function
659 //
660
661 void
662 AFSWorkerThread( IN PVOID Context)
663 {
664
665     NTSTATUS ntStatus = STATUS_SUCCESS;
666     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)Context;
667     AFSWorkItem *pWorkItem;
668     BOOLEAN freeWorkItem = TRUE;
669     AFSDeviceExt *pLibraryDevExt = NULL;
670     LONG lCount;
671
672     pLibraryDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
673
674     //
675     // Indicate that we are initialized and ready
676     //
677
678     KeSetEvent( &pPoolContext->WorkerThreadReady,
679                 0,
680                 FALSE);
681
682     //
683     // Indicate we are initialized
684     //
685
686     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
687
688     ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.WorkerQueueHasItems,
689                                       Executive,
690                                       KernelMode,
691                                       FALSE,
692                                       NULL);
693
694     while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
695     {
696
697         if( !NT_SUCCESS( ntStatus))
698         {
699
700             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
701                           AFS_TRACE_LEVEL_ERROR,
702                           "AFSWorkerThread Wait for queue items failed Status %08lX\n", ntStatus);
703
704             ntStatus = STATUS_SUCCESS;
705         }
706         else
707         {
708
709             pWorkItem = AFSRemoveWorkItem();
710
711             if( pWorkItem == NULL)
712             {
713
714                 ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.WorkerQueueHasItems,
715                                                   Executive,
716                                                   KernelMode,
717                                                   FALSE,
718                                                   NULL);
719             }
720             else
721             {
722
723                 freeWorkItem = TRUE;
724
725                 //
726                 // Switch on the type of work item to process
727                 //
728
729                 switch( pWorkItem->RequestType)
730                 {
731
732                     case AFS_WORK_FLUSH_FCB:
733                     {
734
735                         ntStatus = AFSFlushExtents( pWorkItem->Specific.Fcb.Fcb,
736                                                     &pWorkItem->AuthGroup);
737
738                         if( !NT_SUCCESS( ntStatus))
739                         {
740
741                             AFSReleaseExtentsWithFlush( pWorkItem->Specific.Fcb.Fcb,
742                                                         &pWorkItem->AuthGroup);
743                         }
744
745                         ASSERT( pWorkItem->Specific.Fcb.Fcb->OpenReferenceCount != 0);
746
747                         lCount = InterlockedDecrement( &pWorkItem->Specific.Fcb.Fcb->OpenReferenceCount);
748
749                         break;
750                     }
751
752                     case AFS_WORK_ASYNCH_READ:
753                     {
754
755                         ASSERT( pWorkItem->Specific.AsynchIo.CallingProcess != NULL);
756
757                         (VOID) AFSCommonRead( pWorkItem->Specific.AsynchIo.Device,
758                                               pWorkItem->Specific.AsynchIo.Irp,
759                                               pWorkItem->Specific.AsynchIo.CallingProcess);
760
761                         break;
762                     }
763
764                     case AFS_WORK_ASYNCH_WRITE:
765                     {
766
767                         ASSERT( pWorkItem->Specific.AsynchIo.CallingProcess != NULL);
768
769                         (VOID) AFSCommonWrite( pWorkItem->Specific.AsynchIo.Device,
770                                                pWorkItem->Specific.AsynchIo.Irp,
771                                                pWorkItem->Specific.AsynchIo.CallingProcess);
772                         break;
773                     }
774
775                     case AFS_WORK_ENUMERATE_GLOBAL_ROOT:
776                     {
777
778                         AFSEnumerateGlobalRoot( NULL);
779
780                         break;
781                     }
782
783                     case AFS_WORK_INVALIDATE_OBJECT:
784                     {
785
786                         AFSPerformObjectInvalidate( pWorkItem->Specific.Invalidate.ObjectInfo,
787                                                     pWorkItem->Specific.Invalidate.InvalidateReason);
788
789                         freeWorkItem = TRUE;
790
791                         break;
792                     }
793
794                     case AFS_WORK_START_IOS:
795                     {
796
797                         freeWorkItem = TRUE;
798
799                         break;
800                     }
801
802                     default:
803
804                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
805                                       AFS_TRACE_LEVEL_ERROR,
806                                       "AFSWorkerThread Unknown request type %d\n", pWorkItem->RequestType);
807
808                         break;
809                 }
810
811                 if( freeWorkItem)
812                 {
813
814                     ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
815                 }
816
817                 ntStatus = STATUS_SUCCESS;
818             }
819         }
820     } // worker thread loop
821
822     ClearFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
823
824     // Wake up another worker so they too can exit
825
826     KeSetEvent( &pLibraryDevExt->Specific.Library.WorkerQueueHasItems,
827                 0,
828                 FALSE);
829
830     PsTerminateSystemThread( 0);
831
832     return;
833 }
834
835 void
836 AFSIOWorkerThread( IN PVOID Context)
837 {
838
839     NTSTATUS ntStatus = STATUS_SUCCESS;
840     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)Context;
841     AFSWorkItem *pWorkItem;
842     BOOLEAN freeWorkItem = TRUE;
843     AFSDeviceExt *pLibraryDevExt = NULL, *pRdrDevExt = NULL;
844
845     pLibraryDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
846
847     //
848     // Indicate that we are initialized and ready
849     //
850
851     KeSetEvent( &pPoolContext->WorkerThreadReady,
852                 0,
853                 FALSE);
854
855
856     //
857     // Indicate we are initialized
858     //
859
860     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
861
862     ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.IOWorkerQueueHasItems,
863                                       Executive,
864                                       KernelMode,
865                                       FALSE,
866                                       NULL);
867
868     while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
869     {
870
871         if( !NT_SUCCESS( ntStatus))
872         {
873
874             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
875                           AFS_TRACE_LEVEL_ERROR,
876                           "AFSIOWorkerThread Wait for queue items failed Status %08lX\n", ntStatus);
877
878             ntStatus = STATUS_SUCCESS;
879         }
880         else
881         {
882
883             pWorkItem = AFSRemoveIOWorkItem();
884
885             if( pWorkItem == NULL)
886             {
887
888                 ntStatus = KeWaitForSingleObject( &pLibraryDevExt->Specific.Library.IOWorkerQueueHasItems,
889                                                   Executive,
890                                                   KernelMode,
891                                                   FALSE,
892                                                   NULL);
893             }
894             else
895             {
896
897                 freeWorkItem = TRUE;
898
899                 //
900                 // Switch on the type of work item to process
901                 //
902
903                 switch( pWorkItem->RequestType)
904                 {
905
906                     case AFS_WORK_START_IOS:
907                     {
908
909                         pRdrDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
910
911                         //
912                         // The final status is in the gather io
913                         //
914
915                         ntStatus = AFSStartIos( pWorkItem->Specific.CacheAccess.CacheFileObject,
916                                                 pWorkItem->Specific.CacheAccess.FunctionCode,
917                                                 pWorkItem->Specific.CacheAccess.RequestFlags,
918                                                 pWorkItem->Specific.CacheAccess.IoRuns,
919                                                 pWorkItem->Specific.CacheAccess.RunCount,
920                                                 pWorkItem->Specific.CacheAccess.GatherIo);
921
922                         //
923                         // Regardless of the status we we do the complete - there may
924                         // be IOs in flight
925                         // Decrement the count - setting the event if we were told
926                         // to. This may trigger completion.
927                         //
928
929                         AFSCompleteIo( pWorkItem->Specific.CacheAccess.GatherIo, ntStatus );
930
931                         freeWorkItem = TRUE;
932
933                         break;
934                     }
935
936                     default:
937
938                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
939                                       AFS_TRACE_LEVEL_ERROR,
940                                       "AFSWorkerThread Unknown request type %d\n", pWorkItem->RequestType);
941
942                         break;
943                 }
944
945                 if( freeWorkItem)
946                 {
947
948                     ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
949                 }
950
951                 ntStatus = STATUS_SUCCESS;
952             }
953         }
954     } // worker thread loop
955
956     ClearFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
957
958     // Wake up another IOWorker so they too can exit
959
960     KeSetEvent( &pLibraryDevExt->Specific.Library.IOWorkerQueueHasItems,
961                 0,
962                 FALSE);
963
964     PsTerminateSystemThread( 0);
965
966     return;
967 }
968
969 void
970 AFSPrimaryVolumeWorkerThread( IN PVOID Context)
971 {
972
973     NTSTATUS ntStatus = STATUS_SUCCESS;
974     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)&AFSGlobalRoot->VolumeWorkerContext;
975     AFSDeviceExt *pControlDeviceExt = NULL;
976     AFSDeviceExt *pRDRDeviceExt = NULL;
977     LARGE_INTEGER DueTime;
978     LONG TimeOut;
979     KTIMER Timer;
980     BOOLEAN bFoundOpenEntry = FALSE;
981     AFSObjectInfoCB *pCurrentObject = NULL, *pNextObject = NULL, *pCurrentChildObject = NULL;
982     AFSDirectoryCB *pCurrentDirEntry = NULL, *pNextDirEntry = NULL;
983     BOOLEAN bReleaseVolumeLock = FALSE;
984     AFSVolumeCB *pVolumeCB = NULL, *pNextVolume = NULL;
985     AFSFcb *pFcb = NULL;
986     LONG lFileType;
987     LARGE_INTEGER liCurrentTime;
988     BOOLEAN bVolumeObject = FALSE;
989     LONG lCount;
990
991     pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
992
993     pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
994
995     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
996                   AFS_TRACE_LEVEL_VERBOSE,
997                   "AFSPrimaryVolumeWorkerThread Initialized\n");
998
999     //
1000     // Initialize the timer for the worker thread
1001     //
1002
1003     DueTime.QuadPart = -(5000);
1004
1005     TimeOut = 5000;
1006
1007     KeInitializeTimerEx( &Timer,
1008                          SynchronizationTimer);
1009
1010     KeSetTimerEx( &Timer,
1011                   DueTime,
1012                   TimeOut,
1013                   NULL);
1014
1015     //
1016     // Indicate that we are initialized and ready
1017     //
1018
1019     KeSetEvent( &pPoolContext->WorkerThreadReady,
1020                 0,
1021                 FALSE);
1022
1023     //
1024     // Indicate we are initialized
1025     //
1026
1027     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
1028
1029     while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
1030     {
1031
1032         KeWaitForSingleObject( &Timer,
1033                                Executive,
1034                                KernelMode,
1035                                FALSE,
1036                                NULL);
1037
1038         //
1039         // This is the primary volume worker so it will traverse the volume list
1040         // looking for cleanup or volumes requiring private workers
1041         //
1042
1043         AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
1044                           TRUE);
1045
1046         pVolumeCB = pRDRDeviceExt->Specific.RDR.VolumeListHead;
1047
1048         while( pVolumeCB != NULL)
1049         {
1050
1051             if( pVolumeCB == AFSGlobalRoot ||
1052                 !AFSAcquireExcl( pVolumeCB->VolumeLock,
1053                                  FALSE))
1054             {
1055
1056                 pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
1057
1058                 continue;
1059             }
1060
1061             if( pVolumeCB->ObjectInfoListHead == NULL)
1062             {
1063
1064                 AFSReleaseResource( pVolumeCB->VolumeLock);
1065
1066                 AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1067
1068                 AFSAcquireExcl( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock,
1069                                 TRUE);
1070
1071                 AFSAcquireExcl( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
1072                                 TRUE);
1073
1074                 if( !AFSAcquireExcl( pVolumeCB->VolumeLock,
1075                                      FALSE))
1076                 {
1077
1078                     AFSConvertToShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1079
1080                     AFSReleaseResource( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock);
1081
1082                     pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
1083
1084                     continue;
1085                 }
1086
1087                 KeQueryTickCount( &liCurrentTime);
1088
1089                 pNextVolume = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
1090
1091                 if( pVolumeCB->ObjectInfoListHead == NULL &&
1092                     pVolumeCB->DirectoryCB->OpenReferenceCount == 0 &&
1093                     pVolumeCB->VolumeReferenceCount == 1 &&
1094                     ( pVolumeCB->RootFcb == NULL ||
1095                       pVolumeCB->RootFcb->OpenReferenceCount == 0) &&
1096                     pVolumeCB->ObjectInformation.ObjectReferenceCount <= 0)
1097                 {
1098
1099                     if( pVolumeCB->RootFcb != NULL)
1100                     {
1101
1102                         AFSRemoveRootFcb( pVolumeCB->RootFcb);
1103                     }
1104
1105                     AFSRemoveVolume( pVolumeCB);
1106                 }
1107                 else
1108                 {
1109
1110                     AFSReleaseResource( pVolumeCB->VolumeLock);
1111                 }
1112
1113                 AFSConvertToShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1114
1115                 AFSReleaseResource( pRDRDeviceExt->Specific.RDR.VolumeTree.TreeLock);
1116
1117                 pVolumeCB = pNextVolume;
1118
1119                 continue;
1120             }
1121
1122             //
1123             // Don't need this lock anymore now that we have a volume cb to work with
1124             //
1125
1126             AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1127
1128             //
1129             // For now we only need the volume lock shared
1130             //
1131
1132             AFSConvertToShared( pVolumeCB->VolumeLock);
1133
1134             if( AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
1135                                   FALSE))
1136             {
1137
1138                 pCurrentObject = pVolumeCB->ObjectInfoListHead;
1139
1140                 pNextObject = NULL;
1141
1142                 bReleaseVolumeLock = TRUE;
1143
1144                 while( pCurrentObject != NULL)
1145                 {
1146
1147                     if( pCurrentObject != &pVolumeCB->ObjectInformation)
1148                     {
1149
1150                         pNextObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
1151
1152                         if( pNextObject == NULL &&
1153                             pVolumeCB != AFSGlobalRoot)  // Don't free up the root of the global
1154                         {
1155
1156                             pNextObject = &pVolumeCB->ObjectInformation;
1157                         }
1158
1159                         bVolumeObject = FALSE;
1160                     }
1161                     else
1162                     {
1163
1164                         pNextObject = NULL;
1165
1166                         bVolumeObject = TRUE;
1167                     }
1168
1169                     if( pCurrentObject->FileType == AFS_FILE_TYPE_DIRECTORY &&
1170                         !BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))  // If we are in shutdown mode skip directories
1171                     {
1172
1173                         //
1174                         // If this object is deleted then remove it from the parent, if we can
1175                         //
1176
1177                         if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DELETED) &&
1178                             pCurrentObject->ObjectReferenceCount <= 0 &&
1179                             ( pCurrentObject->Fcb == NULL ||
1180                               pCurrentObject->Fcb->OpenReferenceCount == 0) &&
1181                             pCurrentObject->Specific.Directory.DirectoryNodeListHead == NULL &&
1182                             pCurrentObject->Specific.Directory.ChildOpenReferenceCount == 0)
1183                         {
1184
1185                             AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1186
1187                             //
1188                             // Dropping the TreeLock permits the
1189                             // pCurrentObject->ObjectReferenceCount to change
1190                             //
1191
1192                             if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1193                                                 FALSE))
1194                             {
1195
1196                                 if ( pCurrentObject->ObjectReferenceCount <= 0)
1197                                 {
1198
1199                                     if( pCurrentObject->Fcb != NULL)
1200                                     {
1201
1202                                         AFSRemoveFcb( &pCurrentObject->Fcb);
1203                                     }
1204
1205                                     if( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
1206                                     {
1207
1208                                         if( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb != NULL)
1209                                         {
1210
1211                                             AFSRemoveFcb( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
1212                                         }
1213
1214                                         AFSDeleteObjectInfo( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
1215
1216                                         ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
1217
1218                                         AFSExFreePool( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged);
1219
1220                                         AFSExFreePool( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB);
1221                                     }
1222
1223                                     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1224                                                   AFS_TRACE_LEVEL_VERBOSE,
1225                                                   "AFSPrimaryVolumeWorkerThread Deleting deleted object %08lX\n",
1226                                                   pCurrentObject);
1227
1228                                     AFSDeleteObjectInfo( pCurrentObject);
1229                                 }
1230
1231                                 AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1232
1233                                 pCurrentObject = pNextObject;
1234
1235                                 continue;
1236                             }
1237                             else
1238                             {
1239
1240                                 bReleaseVolumeLock = FALSE;
1241
1242                                 break;
1243                             }
1244                         }
1245
1246                         if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0 ||
1247                             ( pCurrentObject->Fcb != NULL &&
1248                               pCurrentObject->Fcb->OpenReferenceCount > 0) ||
1249                             pCurrentObject->Specific.Directory.DirectoryNodeListHead == NULL)
1250                         {
1251
1252                             pCurrentObject = pNextObject;
1253
1254                             continue;
1255                         }
1256
1257                         if( !AFSAcquireShared( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
1258                                                FALSE))
1259                         {
1260
1261                             pCurrentObject = pNextObject;
1262
1263                             continue;
1264                         }
1265
1266                         KeQueryTickCount( &liCurrentTime);
1267
1268                         pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
1269
1270                         while( pCurrentDirEntry != NULL)
1271                         {
1272
1273                             if( pCurrentDirEntry->OpenReferenceCount > 0 ||
1274                                 ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1275                                   pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0) ||
1276                                 liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
1277                                 liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
1278                                                                         pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart ||
1279                                 ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
1280                                    ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL ||
1281                                      pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)) ||
1282                                 ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE &&
1283                                   pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1284                                   pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0))
1285                             {
1286
1287                                 break;
1288                             }
1289
1290                             pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
1291                         }
1292
1293                         if( pCurrentDirEntry != NULL)
1294                         {
1295
1296                             AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1297
1298                             pCurrentObject = pNextObject;
1299
1300                             continue;
1301                         }
1302
1303                         AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1304
1305                         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1306
1307                         //
1308                         // Now acquire the locks excl
1309                         //
1310
1311                         if( AFSAcquireExcl( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
1312                                             FALSE))
1313                         {
1314
1315                             if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1316                                                 FALSE))
1317                             {
1318
1319                                 if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0)
1320                                 {
1321
1322                                     AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1323
1324                                     AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1325
1326                                     pCurrentObject = pNextObject;
1327
1328                                     continue;
1329                                 }
1330
1331                                 KeQueryTickCount( &liCurrentTime);
1332
1333                                 pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
1334
1335                                 while( pCurrentDirEntry != NULL)
1336                                 {
1337
1338                                     if( pCurrentDirEntry->OpenReferenceCount > 0 ||
1339                                         ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1340                                           pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0) ||
1341                                         liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
1342                                         liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
1343                                                                                 pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart ||
1344                                         ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
1345                                           ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL ||
1346                                             pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)) ||
1347                                         ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE &&
1348                                           pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1349                                           pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0))
1350                                     {
1351
1352                                         break;
1353                                     }
1354
1355                                     pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
1356                                 }
1357
1358                                 if( pCurrentDirEntry != NULL)
1359                                 {
1360
1361                                     AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1362
1363                                     AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1364
1365                                     pCurrentObject = pNextObject;
1366
1367                                     continue;
1368                                 }
1369
1370                                 pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
1371
1372                                 while( pCurrentDirEntry != NULL)
1373                                 {
1374
1375                                     pNextDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
1376
1377                                     pCurrentChildObject = pCurrentDirEntry->ObjectInformation;
1378
1379                                     pFcb = NULL;
1380
1381                                     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1382                                                   AFS_TRACE_LEVEL_VERBOSE,
1383                                                   "AFSPrimaryVolumeWorkerThread Deleting DE %wZ Object %08lX\n",
1384                                                   &pCurrentDirEntry->NameInformation.FileName,
1385                                                   pCurrentChildObject);
1386
1387                                     AFSDeleteDirEntry( pCurrentObject,
1388                                                        pCurrentDirEntry);
1389
1390                                     if( pCurrentChildObject->ObjectReferenceCount <= 0 &&
1391                                         pCurrentChildObject->Fcb != NULL &&
1392                                         pCurrentChildObject->FileType == AFS_FILE_TYPE_FILE)
1393                                     {
1394
1395                                         //
1396                                         // We must not hold pVolumeCB->ObjectInfoTree.TreeLock exclusive
1397                                         // across an AFSCleanupFcb call since it can deadlock with an
1398                                         // invalidation call from the service.
1399                                         //
1400
1401                                         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1402
1403                                         //
1404                                         // Dropping the TreeLock permits the
1405                                         // pCurrentObject->ObjectReferenceCount to change
1406                                         //
1407
1408                                         AFSCleanupFcb( pCurrentChildObject->Fcb,
1409                                                        TRUE);
1410
1411                                         AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1412                                                         TRUE);
1413                                     }
1414
1415                                     if( pCurrentChildObject->ObjectReferenceCount <= 0)
1416                                     {
1417
1418                                         if( pCurrentChildObject->Fcb != NULL)
1419                                         {
1420                                         
1421                                             AFSRemoveFcb( &pCurrentChildObject->Fcb);
1422                                         }
1423
1424                                         if( pCurrentChildObject->FileType == AFS_FILE_TYPE_DIRECTORY &&
1425                                             pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
1426                                         {
1427
1428                                             if( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb != NULL)
1429                                             {
1430
1431                                                 AFSRemoveFcb( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
1432                                             }
1433
1434                                             AFSDeleteObjectInfo( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
1435
1436                                             ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
1437
1438                                             AFSExFreePool( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged);
1439
1440                                             AFSExFreePool( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB);
1441                                         }
1442
1443                                         AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1444                                                       AFS_TRACE_LEVEL_VERBOSE,
1445                                                       "AFSPrimaryVolumeWorkerThread Deleting object %08lX\n",
1446                                                       pCurrentChildObject);
1447
1448                                         AFSDeleteObjectInfo( pCurrentChildObject);
1449                                     }
1450
1451                                     pCurrentDirEntry = pNextDirEntry;
1452
1453                                 }
1454
1455                                 pCurrentObject->Specific.Directory.DirectoryNodeListHead = NULL;
1456
1457                                 pCurrentObject->Specific.Directory.DirectoryNodeListTail = NULL;
1458
1459                                 pCurrentObject->Specific.Directory.ShortNameTree = NULL;
1460
1461                                 pCurrentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead = NULL;
1462
1463                                 pCurrentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead = NULL;
1464
1465                                 pCurrentObject->Specific.Directory.DirectoryNodeCount = 0;
1466
1467                                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIR_NODE_COUNT,
1468                                               AFS_TRACE_LEVEL_VERBOSE,
1469                                               "AFSPrimaryVolumeWorkerThread Reset count to 0 on parent FID %08lX-%08lX-%08lX-%08lX\n",
1470                                               pCurrentObject->FileId.Cell,
1471                                               pCurrentObject->FileId.Volume,
1472                                               pCurrentObject->FileId.Vnode,
1473                                               pCurrentObject->FileId.Unique);
1474
1475                                 //
1476                                 // Clear our enumerated flag on this object so we retrieve info again on next access
1477                                 //
1478
1479                                 ClearFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1480
1481                                 AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1482
1483                                 AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1484                             }
1485                             else
1486                             {
1487
1488                                 AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1489
1490                                 bReleaseVolumeLock = FALSE;
1491
1492                                 break;
1493                             }
1494                         }
1495                         else
1496                         {
1497
1498                             //
1499                             // Try to grab the volume lock again ... no problem if we don't
1500                             //
1501
1502                             if( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1503                                                  FALSE))
1504                             {
1505
1506                                 bReleaseVolumeLock = FALSE;
1507
1508                                 break;
1509                             }
1510                         }
1511
1512                         if( pCurrentObject != &pVolumeCB->ObjectInformation)
1513                         {
1514
1515                             pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
1516
1517                             if( pCurrentObject == NULL &&
1518                                 pVolumeCB != AFSGlobalRoot)
1519                             {
1520
1521                                 pCurrentObject = &pVolumeCB->ObjectInformation;
1522                             }
1523                         }
1524                         else
1525                         {
1526
1527                             pCurrentObject = NULL;
1528                         }
1529
1530                         continue;
1531                     }
1532                     else if( pCurrentObject->FileType == AFS_FILE_TYPE_FILE)
1533                     {
1534
1535                         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1536
1537                         if( pCurrentObject->Fcb != NULL)
1538                         {
1539
1540                             //
1541                             // Dropping the TreeLock permits the
1542                             // pCurrentObject->ObjectReferenceCount to change
1543                             //
1544
1545                             AFSCleanupFcb( pCurrentObject->Fcb,
1546                                            TRUE);
1547                         }
1548
1549                         if( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1550                                              FALSE))
1551                         {
1552
1553                             bReleaseVolumeLock = FALSE;
1554
1555                             break;
1556                         }
1557
1558                         if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DELETED) &&
1559                             pCurrentObject->ObjectReferenceCount <= 0 &&
1560                             ( pCurrentObject->Fcb == NULL ||
1561                               pCurrentObject->Fcb->OpenReferenceCount == 0))
1562                         {
1563
1564                             if( pCurrentObject->Fcb != NULL)
1565                             {
1566
1567                                 AFSRemoveFcb( &pCurrentObject->Fcb);
1568                             }
1569
1570                             AFSDeleteObjectInfo( pCurrentObject);
1571                         }
1572
1573                         AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1574
1575                         pCurrentObject = pNextObject;
1576
1577                         continue;
1578                     }
1579
1580                     pCurrentObject = pNextObject;
1581                 }
1582
1583                 if( bReleaseVolumeLock)
1584                 {
1585
1586                     AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1587                 }
1588             }
1589
1590             //
1591             // Next volume cb
1592             //
1593
1594             AFSReleaseResource( pVolumeCB->VolumeLock);
1595
1596             AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
1597                               TRUE);
1598
1599             pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
1600         }
1601
1602         AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1603
1604     } // worker thread loop
1605
1606     KeCancelTimer( &Timer);
1607
1608     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1609                   AFS_TRACE_LEVEL_VERBOSE,
1610                   "AFSPrimaryVolumeWorkerThread Exiting\n");
1611
1612     lCount = InterlockedDecrement( &pControlDeviceExt->Specific.Control.VolumeWorkerThreadCount);
1613
1614     if( lCount == 0)
1615     {
1616
1617         KeSetEvent( &pControlDeviceExt->Specific.Control.VolumeWorkerCloseEvent,
1618                     0,
1619                     FALSE);
1620     }
1621
1622     PsTerminateSystemThread( 0);
1623
1624     return;
1625 }
1626
1627 void
1628 AFSVolumeWorkerThread( IN PVOID Context)
1629 {
1630
1631     NTSTATUS ntStatus = STATUS_SUCCESS;
1632     AFSVolumeCB *pVolumeCB = (AFSVolumeCB * )Context;
1633     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)&pVolumeCB->VolumeWorkerContext;
1634     AFSDeviceExt *pControlDeviceExt = NULL;
1635     AFSDeviceExt *pRDRDeviceExt = NULL;
1636     BOOLEAN exitThread = FALSE;
1637     LARGE_INTEGER DueTime;
1638     LONG TimeOut;
1639     KTIMER Timer;
1640     LONG lCount;
1641
1642     pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
1643
1644     pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1645
1646     //
1647     // Initialize the timer for the worker thread
1648     //
1649
1650     DueTime.QuadPart = -(5000);
1651
1652     TimeOut = 5000;
1653
1654     KeInitializeTimerEx( &Timer,
1655                          SynchronizationTimer);
1656
1657     KeSetTimerEx( &Timer,
1658                   DueTime,
1659                   TimeOut,
1660                   NULL);
1661
1662     //
1663     // Indicate that we are initialized and ready
1664     //
1665
1666     KeSetEvent( &pPoolContext->WorkerThreadReady,
1667                 0,
1668                 FALSE);
1669
1670     //
1671     // Indicate we are initialized
1672     //
1673
1674     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
1675
1676     while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
1677     {
1678
1679         ntStatus = KeWaitForSingleObject( &Timer,
1680                                           Executive,
1681                                           KernelMode,
1682                                           FALSE,
1683                                           NULL);
1684
1685         if( !NT_SUCCESS( ntStatus))
1686         {
1687
1688             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1689                           AFS_TRACE_LEVEL_ERROR,
1690                           "AFSVolumeWorkerThread Wait for queue items failed Status %08lX\n", ntStatus);
1691         }
1692         else
1693         {
1694
1695             //
1696             // If we are in shutdown mode and the dirty flag is clear then get out now
1697             //
1698
1699             if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
1700             {
1701
1702                 break;
1703             }
1704         }
1705     } // worker thread loop
1706
1707     KeCancelTimer( &Timer);
1708
1709     lCount = InterlockedDecrement( &pControlDeviceExt->Specific.Control.VolumeWorkerThreadCount);
1710
1711     if( lCount == 0)
1712     {
1713
1714         KeSetEvent( &pControlDeviceExt->Specific.Control.VolumeWorkerCloseEvent,
1715                     0,
1716                     FALSE);
1717     }
1718
1719     PsTerminateSystemThread( 0);
1720
1721     return;
1722 }
1723
1724 NTSTATUS
1725 AFSInsertWorkitem( IN AFSWorkItem *WorkItem)
1726 {
1727
1728     NTSTATUS ntStatus = STATUS_SUCCESS;
1729     AFSDeviceExt *pDevExt = NULL;
1730     LONG lCount;
1731
1732     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1733
1734     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1735                   AFS_TRACE_LEVEL_VERBOSE,
1736                   "AFSInsertWorkitem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1737                   &pDevExt->Specific.Library.QueueLock,
1738                   PsGetCurrentThread());
1739
1740     AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
1741                     TRUE);
1742
1743     lCount = InterlockedIncrement( &pDevExt->Specific.Library.QueueItemCount);
1744
1745     AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1746                   AFS_TRACE_LEVEL_VERBOSE,
1747                   "AFSInsertWorkitem Inserting work item %08lX Count %08lX\n",
1748                   WorkItem,
1749                   lCount);
1750
1751     if( pDevExt->Specific.Library.QueueTail != NULL) // queue already has nodes
1752     {
1753
1754         pDevExt->Specific.Library.QueueTail->next = WorkItem;
1755     }
1756     else // first node
1757     {
1758
1759         pDevExt->Specific.Library.QueueHead = WorkItem;
1760     }
1761
1762     WorkItem->next = NULL;
1763     pDevExt->Specific.Library.QueueTail = WorkItem;
1764
1765     // indicate that the queue has nodes
1766     KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
1767                 0,
1768                 FALSE);
1769
1770     AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
1771
1772     return ntStatus;
1773 }
1774
1775 NTSTATUS
1776 AFSInsertIOWorkitem( IN AFSWorkItem *WorkItem)
1777 {
1778
1779     NTSTATUS ntStatus = STATUS_SUCCESS;
1780     AFSDeviceExt *pDevExt = NULL;
1781     LONG lCount;
1782
1783     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1784
1785     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1786                   AFS_TRACE_LEVEL_VERBOSE,
1787                   "AFSInsertIOWorkitem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1788                   &pDevExt->Specific.Library.IOQueueLock,
1789                   PsGetCurrentThread());
1790
1791     AFSAcquireExcl( &pDevExt->Specific.Library.IOQueueLock,
1792                     TRUE);
1793
1794     lCount = InterlockedIncrement( &pDevExt->Specific.Library.IOQueueItemCount);
1795
1796     AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1797                   AFS_TRACE_LEVEL_VERBOSE,
1798                   "AFSInsertWorkitem Inserting IO work item %08lX Count %08lX\n",
1799                   WorkItem,
1800                   lCount);
1801
1802     if( pDevExt->Specific.Library.IOQueueTail != NULL) // queue already has nodes
1803     {
1804
1805         pDevExt->Specific.Library.IOQueueTail->next = WorkItem;
1806     }
1807     else // first node
1808     {
1809
1810         pDevExt->Specific.Library.IOQueueHead = WorkItem;
1811     }
1812
1813     WorkItem->next = NULL;
1814     pDevExt->Specific.Library.IOQueueTail = WorkItem;
1815
1816     // indicate that the queue has nodes
1817     KeSetEvent( &(pDevExt->Specific.Library.IOWorkerQueueHasItems),
1818                 0,
1819                 FALSE);
1820
1821     AFSReleaseResource( &pDevExt->Specific.Library.IOQueueLock);
1822
1823     return ntStatus;
1824 }
1825
1826 NTSTATUS
1827 AFSInsertWorkitemAtHead( IN AFSWorkItem *WorkItem)
1828 {
1829
1830     NTSTATUS ntStatus = STATUS_SUCCESS;
1831     AFSDeviceExt *pDevExt = NULL;
1832     LONG lCount;
1833
1834     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1835
1836     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1837                   AFS_TRACE_LEVEL_VERBOSE,
1838                   "AFSInsertWorkitemAtHead Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1839                   &pDevExt->Specific.Library.QueueLock,
1840                   PsGetCurrentThread());
1841
1842     AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
1843                     TRUE);
1844
1845     WorkItem->next = pDevExt->Specific.Library.QueueHead;
1846
1847     pDevExt->Specific.Library.QueueHead = WorkItem;
1848
1849     lCount = InterlockedIncrement( &pDevExt->Specific.Library.QueueItemCount);
1850
1851     AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1852                   AFS_TRACE_LEVEL_VERBOSE,
1853                   "AFSInsertWorkitemAtHead Inserting work item %08lX Count %08lX\n",
1854                   WorkItem,
1855                   lCount);
1856
1857     //
1858     // indicate that the queue has nodes
1859     //
1860
1861     KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
1862                 0,
1863                 FALSE);
1864
1865     AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
1866
1867     return ntStatus;
1868 }
1869
1870 AFSWorkItem *
1871 AFSRemoveWorkItem()
1872 {
1873
1874     NTSTATUS ntStatus = STATUS_SUCCESS;
1875     AFSWorkItem        *pWorkItem = NULL;
1876     AFSDeviceExt *pDevExt = NULL;
1877     LONG lCount;
1878
1879     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1880
1881     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1882                   AFS_TRACE_LEVEL_VERBOSE,
1883                   "AFSRemoveWorkItem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1884                   &pDevExt->Specific.Library.QueueLock,
1885                   PsGetCurrentThread());
1886
1887     AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
1888                     TRUE);
1889
1890     if( pDevExt->Specific.Library.QueueHead != NULL) // queue has nodes
1891     {
1892
1893         pWorkItem = pDevExt->Specific.Library.QueueHead;
1894
1895         lCount = InterlockedDecrement( &pDevExt->Specific.Library.QueueItemCount);
1896
1897         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1898                       AFS_TRACE_LEVEL_VERBOSE,
1899                       "AFSRemoveWorkItem Removing work item %08lX Count %08lX Thread %08lX\n",
1900                       pWorkItem,
1901                       lCount,
1902                       PsGetCurrentThreadId());
1903
1904         pDevExt->Specific.Library.QueueHead = pDevExt->Specific.Library.QueueHead->next;
1905
1906         if( pDevExt->Specific.Library.QueueHead == NULL) // if queue just became empty
1907         {
1908
1909             pDevExt->Specific.Library.QueueTail = NULL;
1910         }
1911         else
1912         {
1913
1914             //
1915             // Wake up another worker
1916             //
1917
1918             KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
1919                         0,
1920                         FALSE);
1921         }
1922     }
1923
1924     AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
1925
1926     return pWorkItem;
1927 }
1928
1929 AFSWorkItem *
1930 AFSRemoveIOWorkItem()
1931 {
1932
1933     NTSTATUS ntStatus = STATUS_SUCCESS;
1934     AFSWorkItem        *pWorkItem = NULL;
1935     AFSDeviceExt *pDevExt = NULL;
1936     LONG lCount;
1937
1938     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1939
1940     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1941                   AFS_TRACE_LEVEL_VERBOSE,
1942                   "AFSRemoveIOWorkItem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1943                   &pDevExt->Specific.Library.IOQueueLock,
1944                   PsGetCurrentThread());
1945
1946     AFSAcquireExcl( &pDevExt->Specific.Library.IOQueueLock,
1947                     TRUE);
1948
1949     if( pDevExt->Specific.Library.IOQueueHead != NULL) // queue has nodes
1950     {
1951
1952         pWorkItem = pDevExt->Specific.Library.IOQueueHead;
1953
1954         lCount = InterlockedDecrement( &pDevExt->Specific.Library.IOQueueItemCount);
1955
1956         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1957                       AFS_TRACE_LEVEL_VERBOSE,
1958                       "AFSRemoveWorkItem Removing work item %08lX Count %08lX Thread %08lX\n",
1959                       pWorkItem,
1960                       lCount,
1961                       PsGetCurrentThreadId());
1962
1963         pDevExt->Specific.Library.IOQueueHead = pDevExt->Specific.Library.IOQueueHead->next;
1964
1965         if( pDevExt->Specific.Library.IOQueueHead == NULL) // if queue just became empty
1966         {
1967
1968             pDevExt->Specific.Library.IOQueueTail = NULL;
1969         }
1970         else
1971         {
1972
1973             //
1974             // Wake up another worker
1975             //
1976
1977             KeSetEvent( &(pDevExt->Specific.Library.IOWorkerQueueHasItems),
1978                         0,
1979                         FALSE);
1980         }
1981     }
1982
1983     AFSReleaseResource( &pDevExt->Specific.Library.IOQueueLock);
1984
1985     return pWorkItem;
1986 }
1987
1988 NTSTATUS
1989 AFSQueueWorkerRequest( IN AFSWorkItem *WorkItem)
1990 {
1991
1992     NTSTATUS ntStatus = STATUS_SUCCESS;
1993     AFSDeviceExt *pDevExt = NULL;
1994     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
1995
1996     //
1997     // Submit the work item to the worker
1998     //
1999
2000     ntStatus = AFSInsertWorkitem( WorkItem);
2001
2002     if( bWait)
2003     {
2004
2005         //
2006         // Sync request so block on the work item event
2007         //
2008
2009         ntStatus = KeWaitForSingleObject( &WorkItem->Event,
2010                                           Executive,
2011                                           KernelMode,
2012                                           FALSE,
2013                                           NULL);
2014     }
2015
2016     return ntStatus;
2017 }
2018
2019 NTSTATUS
2020 AFSQueueIOWorkerRequest( IN AFSWorkItem *WorkItem)
2021 {
2022
2023     NTSTATUS ntStatus = STATUS_SUCCESS;
2024     AFSDeviceExt *pDevExt = NULL;
2025     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
2026
2027     //
2028     // Submit the work item to the worker
2029     //
2030
2031     ntStatus = AFSInsertIOWorkitem( WorkItem);
2032
2033     if( bWait)
2034     {
2035
2036         //
2037         // Sync request so block on the work item event
2038         //
2039
2040         ntStatus = KeWaitForSingleObject( &WorkItem->Event,
2041                                           Executive,
2042                                           KernelMode,
2043                                           FALSE,
2044                                           NULL);
2045     }
2046
2047     return ntStatus;
2048 }
2049
2050 NTSTATUS
2051 AFSQueueWorkerRequestAtHead( IN AFSWorkItem *WorkItem)
2052 {
2053
2054     NTSTATUS ntStatus = STATUS_SUCCESS;
2055     AFSDeviceExt *pDevExt = NULL;
2056     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
2057
2058     //
2059     // Submit the work item to the worker
2060     //
2061
2062     ntStatus = AFSInsertWorkitemAtHead( WorkItem);
2063
2064     if( bWait)
2065     {
2066
2067         //
2068         // Sync request so block on the work item event
2069         //
2070
2071         ntStatus = KeWaitForSingleObject( &WorkItem->Event,
2072                                           Executive,
2073                                           KernelMode,
2074                                           FALSE,
2075                                           NULL);
2076     }
2077
2078     return ntStatus;
2079 }
2080
2081 NTSTATUS
2082 AFSQueueFlushExtents( IN AFSFcb *Fcb,
2083                       IN GUID *AuthGroup)
2084 {
2085
2086     NTSTATUS ntStatus = STATUS_SUCCESS;
2087     AFSDeviceExt *pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2088     AFSWorkItem *pWorkItem = NULL;
2089     LONG lCount;
2090
2091     __try
2092     {
2093
2094         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2095                       AFS_TRACE_LEVEL_VERBOSE,
2096                       "AFSQueueFlushExtents Queuing request for FID %08lX-%08lX-%08lX-%08lX\n",
2097                       Fcb->ObjectInformation->FileId.Cell,
2098                       Fcb->ObjectInformation->FileId.Volume,
2099                       Fcb->ObjectInformation->FileId.Vnode,
2100                       Fcb->ObjectInformation->FileId.Unique);
2101
2102         //
2103         // Increment our flush count here just to keep the number of items in the
2104         // queue down. We'll decrement it just below.
2105         //
2106
2107         lCount = InterlockedIncrement( &Fcb->Specific.File.QueuedFlushCount);
2108
2109         if( lCount > 3)
2110         {
2111
2112             AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2113                           AFS_TRACE_LEVEL_VERBOSE,
2114                           "AFSQueueFlushExtents Max queued items for FID %08lX-%08lX-%08lX-%08lX\n",
2115                           Fcb->ObjectInformation->FileId.Cell,
2116                           Fcb->ObjectInformation->FileId.Volume,
2117                           Fcb->ObjectInformation->FileId.Vnode,
2118                           Fcb->ObjectInformation->FileId.Unique);
2119
2120             try_return( ntStatus);
2121         }
2122
2123         if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
2124         {
2125
2126             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2127                           AFS_TRACE_LEVEL_ERROR,
2128                           "AFSQueueFlushExtents Failing request, in shutdown\n");
2129
2130             try_return( ntStatus = STATUS_TOO_LATE);
2131         }
2132
2133         //
2134         // Allocate our request structure and send it to the worker
2135         //
2136
2137         pWorkItem = (AFSWorkItem *)AFSLibExAllocatePoolWithTag( NonPagedPool,
2138                                                                 sizeof( AFSWorkItem),
2139                                                                 AFS_WORK_ITEM_TAG);
2140
2141         if( pWorkItem == NULL)
2142         {
2143
2144             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2145                           AFS_TRACE_LEVEL_ERROR,
2146                           "AFSQueueFlushExtents Failed to allocate work item\n");
2147
2148             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2149         }
2150
2151         RtlZeroMemory( pWorkItem,
2152                        sizeof( AFSWorkItem));
2153
2154         pWorkItem->Size = sizeof( AFSWorkItem);
2155
2156         pWorkItem->ProcessID = (ULONGLONG)PsGetCurrentProcessId();
2157
2158         pWorkItem->RequestType = AFS_WORK_FLUSH_FCB;
2159
2160         if ( AuthGroup == NULL)
2161         {
2162
2163             RtlZeroMemory( &pWorkItem->AuthGroup,
2164                            sizeof( GUID));
2165
2166             ntStatus = AFSRetrieveValidAuthGroup( Fcb,
2167                                                   NULL,
2168                                                   TRUE,
2169                                                   &pWorkItem->AuthGroup);
2170         }
2171         else
2172         {
2173             RtlCopyMemory( &pWorkItem->AuthGroup,
2174                            AuthGroup,
2175                            sizeof( GUID));
2176         }
2177
2178         pWorkItem->Specific.Fcb.Fcb = Fcb;
2179
2180         lCount = InterlockedIncrement( &Fcb->OpenReferenceCount);
2181
2182         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2183                       AFS_TRACE_LEVEL_VERBOSE,
2184                       "AFSQueueFlushExtents Increment count on Fcb %08lX Cnt %d\n",
2185                       Fcb,
2186                       lCount);
2187
2188         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2189                       AFS_TRACE_LEVEL_VERBOSE,
2190                       "AFSQueueFlushExtents Workitem %08lX for FID %08lX-%08lX-%08lX-%08lX\n",
2191                       pWorkItem,
2192                       Fcb->ObjectInformation->FileId.Cell,
2193                       Fcb->ObjectInformation->FileId.Volume,
2194                       Fcb->ObjectInformation->FileId.Vnode,
2195                       Fcb->ObjectInformation->FileId.Unique);
2196
2197         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2198
2199 try_exit:
2200
2201         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2202                       AFS_TRACE_LEVEL_VERBOSE,
2203                       "AFSQueueFlushExtents Request complete Status %08lX FID %08lX-%08lX-%08lX-%08lX\n",
2204                       Fcb->ObjectInformation->FileId.Cell,
2205                       Fcb->ObjectInformation->FileId.Volume,
2206                       Fcb->ObjectInformation->FileId.Vnode,
2207                       Fcb->ObjectInformation->FileId.Unique,
2208                       ntStatus);
2209
2210         //
2211         // Remove the count we added above
2212         //
2213
2214         lCount = InterlockedDecrement( &Fcb->Specific.File.QueuedFlushCount);
2215
2216         if( lCount == 0)
2217         {
2218
2219             KeSetEvent( &Fcb->NPFcb->Specific.File.QueuedFlushEvent,
2220                         0,
2221                         FALSE);
2222         }
2223
2224         if( !NT_SUCCESS( ntStatus))
2225         {
2226
2227             if( pWorkItem != NULL)
2228             {
2229
2230                 lCount = InterlockedDecrement( &Fcb->OpenReferenceCount);
2231
2232                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2233             }
2234
2235             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2236                           AFS_TRACE_LEVEL_ERROR,
2237                           "AFSQueueFlushExtents Failed to queue request Status %08lX\n", ntStatus);
2238         }
2239     }
2240     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2241     {
2242
2243         AFSDbgLogMsg( 0,
2244                       0,
2245                       "EXCEPTION - AFSQueueFlushExtents\n");
2246
2247         AFSDumpTraceFilesFnc();
2248     }
2249
2250     return ntStatus;
2251 }
2252
2253 NTSTATUS
2254 AFSQueueAsyncRead( IN PDEVICE_OBJECT DeviceObject,
2255                    IN PIRP Irp,
2256                    IN HANDLE CallerProcess)
2257 {
2258
2259     NTSTATUS ntStatus = STATUS_SUCCESS;
2260     AFSWorkItem *pWorkItem = NULL;
2261
2262     __try
2263     {
2264
2265         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2266                       AFS_TRACE_LEVEL_VERBOSE,
2267                       "AFSQueueAsyncRead Queuing request for Irp %08lX\n",
2268                       Irp);
2269
2270         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2271                                                                  sizeof(AFSWorkItem),
2272                                                                  AFS_WORK_ITEM_TAG);
2273         if (NULL == pWorkItem)
2274         {
2275
2276             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2277                           AFS_TRACE_LEVEL_ERROR,
2278                           "AFSQueueAsyncRead Failed to allocate work item\n");
2279
2280             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2281         }
2282
2283         RtlZeroMemory( pWorkItem,
2284                        sizeof(AFSWorkItem));
2285
2286         pWorkItem->Size = sizeof( AFSWorkItem);
2287
2288         pWorkItem->RequestType = AFS_WORK_ASYNCH_READ;
2289
2290         pWorkItem->Specific.AsynchIo.Device = DeviceObject;
2291
2292         pWorkItem->Specific.AsynchIo.Irp = Irp;
2293
2294         pWorkItem->Specific.AsynchIo.CallingProcess = CallerProcess;
2295
2296         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2297                       AFS_TRACE_LEVEL_VERBOSE,
2298                       "AFSQueueAsyncRead Workitem %08lX for Irp %08lX\n",
2299                       pWorkItem,
2300                       Irp);
2301
2302         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2303
2304 try_exit:
2305
2306         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2307                       AFS_TRACE_LEVEL_VERBOSE,
2308                       "AFSQueueAsyncRead Request for Irp %08lX complete Status %08lX\n",
2309                       Irp,
2310                       ntStatus);
2311
2312         if( !NT_SUCCESS( ntStatus))
2313         {
2314
2315             if( pWorkItem != NULL)
2316             {
2317
2318                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2319             }
2320
2321             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2322                           AFS_TRACE_LEVEL_ERROR,
2323                           "AFSQueueAsyncRead Failed to queue request Status %08lX\n", ntStatus);
2324         }
2325     }
2326     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2327     {
2328
2329         AFSDbgLogMsg( 0,
2330                       0,
2331                       "EXCEPTION - AFSQueueAsyncRead\n");
2332
2333         AFSDumpTraceFilesFnc();
2334     }
2335
2336     return ntStatus;
2337 }
2338
2339 NTSTATUS
2340 AFSQueueAsyncWrite( IN PDEVICE_OBJECT DeviceObject,
2341                     IN PIRP Irp,
2342                     IN HANDLE CallerProcess)
2343 {
2344
2345     NTSTATUS ntStatus = STATUS_SUCCESS;
2346     AFSWorkItem *pWorkItem = NULL;
2347
2348     __try
2349     {
2350
2351         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2352                       AFS_TRACE_LEVEL_VERBOSE,
2353                       "AFSQueueAsyncWrite Queuing request for Irp %08lX\n",
2354                       Irp);
2355
2356         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2357                                                                  sizeof(AFSWorkItem),
2358                                                                  AFS_WORK_ITEM_TAG);
2359         if (NULL == pWorkItem)
2360         {
2361
2362             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2363                           AFS_TRACE_LEVEL_ERROR,
2364                           "AFSQueueAsyncWrite Failed to allocate work item\n");
2365
2366             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2367         }
2368
2369         RtlZeroMemory( pWorkItem,
2370                        sizeof(AFSWorkItem));
2371
2372         pWorkItem->Size = sizeof( AFSWorkItem);
2373
2374         pWorkItem->RequestType = AFS_WORK_ASYNCH_WRITE;
2375
2376         pWorkItem->Specific.AsynchIo.Device = DeviceObject;
2377
2378         pWorkItem->Specific.AsynchIo.Irp = Irp;
2379
2380         pWorkItem->Specific.AsynchIo.CallingProcess = CallerProcess;
2381
2382         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2383                       AFS_TRACE_LEVEL_VERBOSE,
2384                       "AFSQueueAsyncWrite Workitem %08lX for Irp %08lX\n",
2385                       pWorkItem,
2386                       Irp);
2387
2388         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2389
2390 try_exit:
2391
2392         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2393                       AFS_TRACE_LEVEL_VERBOSE,
2394                       "AFSQueueAsyncWrite Request for Irp %08lX complete Status %08lX\n",
2395                       Irp,
2396                       ntStatus);
2397
2398         if( !NT_SUCCESS( ntStatus))
2399         {
2400
2401             if( pWorkItem != NULL)
2402             {
2403
2404                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2405             }
2406
2407             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2408                           AFS_TRACE_LEVEL_ERROR,
2409                           "AFSQueueAsyncWrite Failed to queue request Status %08lX\n", ntStatus);
2410         }
2411     }
2412     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2413     {
2414
2415         AFSDbgLogMsg( 0,
2416                       0,
2417                       "EXCEPTION - AFSQueueAsyncWrite\n");
2418
2419         AFSDumpTraceFilesFnc();
2420     }
2421
2422     return ntStatus;
2423 }
2424
2425 NTSTATUS
2426 AFSQueueGlobalRootEnumeration()
2427 {
2428
2429     NTSTATUS ntStatus = STATUS_SUCCESS;
2430     AFSWorkItem *pWorkItem = NULL;
2431
2432     __try
2433     {
2434
2435         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2436                                                                  sizeof(AFSWorkItem),
2437                                                                  AFS_WORK_ITEM_TAG);
2438         if (NULL == pWorkItem)
2439         {
2440
2441             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2442                           AFS_TRACE_LEVEL_ERROR,
2443                           "AFSQueueGlobalRootEnumeration Failed to allocate work item\n");
2444
2445             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2446         }
2447
2448         RtlZeroMemory( pWorkItem,
2449                        sizeof(AFSWorkItem));
2450
2451         pWorkItem->Size = sizeof( AFSWorkItem);
2452
2453         pWorkItem->RequestType = AFS_WORK_ENUMERATE_GLOBAL_ROOT;
2454
2455         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2456                       AFS_TRACE_LEVEL_VERBOSE,
2457                       "AFSQueueGlobalRootEnumeration Workitem %08lX\n",
2458                       pWorkItem);
2459
2460         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2461
2462 try_exit:
2463
2464         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2465                       AFS_TRACE_LEVEL_VERBOSE,
2466                       "AFSQueueGlobalRootEnumeration Request complete Status %08lX\n",
2467                       ntStatus);
2468
2469         if( !NT_SUCCESS( ntStatus))
2470         {
2471
2472             if( pWorkItem != NULL)
2473             {
2474
2475                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2476             }
2477
2478             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2479                           AFS_TRACE_LEVEL_ERROR,
2480                           "AFSQueueGlobalRootEnumeration Failed to queue request Status %08lX\n",
2481                           ntStatus);
2482         }
2483     }
2484     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2485     {
2486
2487         AFSDbgLogMsg( 0,
2488                       0,
2489                       "EXCEPTION - AFSQueueGlobalRootEnumeration\n");
2490
2491         AFSDumpTraceFilesFnc();
2492     }
2493
2494     return ntStatus;
2495 }
2496
2497 NTSTATUS
2498 AFSQueueStartIos( IN PFILE_OBJECT CacheFileObject,
2499                   IN UCHAR FunctionCode,
2500                   IN ULONG RequestFlags,
2501                   IN AFSIoRun *IoRuns,
2502                   IN ULONG RunCount,
2503                   IN AFSGatherIo *GatherIo)
2504 {
2505
2506     NTSTATUS ntStatus = STATUS_SUCCESS;
2507     AFSDeviceExt *pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2508     AFSWorkItem *pWorkItem = NULL;
2509
2510     __try
2511     {
2512
2513         if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
2514         {
2515
2516             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2517                           AFS_TRACE_LEVEL_ERROR,
2518                           "AFSQueueStartIos Failing request, in shutdown\n");
2519
2520             try_return( ntStatus = STATUS_TOO_LATE);
2521         }
2522
2523         //
2524         // Allocate our request structure and send it to the worker
2525         //
2526
2527         pWorkItem = (AFSWorkItem *)AFSLibExAllocatePoolWithTag( NonPagedPool,
2528                                                                 sizeof( AFSWorkItem),
2529                                                                 AFS_WORK_ITEM_TAG);
2530
2531         if( pWorkItem == NULL)
2532         {
2533
2534             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2535                           AFS_TRACE_LEVEL_ERROR,
2536                           "AFSQueueStartIos Failed to allocate work item\n");
2537
2538             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2539         }
2540
2541         RtlZeroMemory( pWorkItem,
2542                        sizeof( AFSWorkItem));
2543
2544         KeInitializeEvent( &pWorkItem->Event,
2545                            NotificationEvent,
2546                            FALSE);
2547
2548         pWorkItem->Size = sizeof( AFSWorkItem);
2549
2550         pWorkItem->ProcessID = (ULONGLONG)PsGetCurrentProcessId();
2551
2552         pWorkItem->RequestType = AFS_WORK_START_IOS;
2553
2554         pWorkItem->Specific.CacheAccess.CacheFileObject = CacheFileObject;
2555
2556         pWorkItem->Specific.CacheAccess.FunctionCode = FunctionCode;
2557
2558         pWorkItem->Specific.CacheAccess.RequestFlags = RequestFlags;
2559
2560         pWorkItem->Specific.CacheAccess.IoRuns = IoRuns;
2561
2562         pWorkItem->Specific.CacheAccess.RunCount = RunCount;
2563
2564         pWorkItem->Specific.CacheAccess.GatherIo = GatherIo;
2565
2566         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2567                       AFS_TRACE_LEVEL_VERBOSE,
2568                       "AFSQueueStartIos Queuing IO Workitem %08lX\n",
2569                       pWorkItem);
2570
2571         ntStatus = AFSQueueIOWorkerRequest( pWorkItem);
2572
2573 try_exit:
2574
2575         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2576                       AFS_TRACE_LEVEL_VERBOSE,
2577                       "AFSQueueStartIos Request complete Status %08lX\n",
2578                       ntStatus);
2579
2580         if( !NT_SUCCESS( ntStatus))
2581         {
2582
2583             if( pWorkItem != NULL)
2584             {
2585
2586                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2587             }
2588         }
2589     }
2590     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2591     {
2592
2593         AFSDbgLogMsg( 0,
2594                       0,
2595                       "EXCEPTION - AFSQueueStartIos\n");
2596
2597         AFSDumpTraceFilesFnc();
2598     }
2599
2600     return ntStatus;
2601 }
2602
2603 NTSTATUS
2604 AFSQueueInvalidateObject( IN AFSObjectInfoCB *ObjectInfo,
2605                           IN ULONG InvalidateReason)
2606 {
2607
2608     NTSTATUS ntStatus = STATUS_SUCCESS;
2609     AFSWorkItem *pWorkItem = NULL;
2610
2611     __try
2612     {
2613
2614         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2615                                                                  sizeof(AFSWorkItem),
2616                                                                  AFS_WORK_ITEM_TAG);
2617         if (NULL == pWorkItem)
2618         {
2619
2620             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2621                           AFS_TRACE_LEVEL_ERROR,
2622                           "AFSQueueInvalidateObject Failed to allocate work item\n");
2623
2624             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2625         }
2626
2627         RtlZeroMemory( pWorkItem,
2628                        sizeof(AFSWorkItem));
2629
2630         pWorkItem->Size = sizeof( AFSWorkItem);
2631
2632         pWorkItem->RequestType = AFS_WORK_INVALIDATE_OBJECT;
2633
2634         pWorkItem->Specific.Invalidate.ObjectInfo = ObjectInfo;
2635
2636         pWorkItem->Specific.Invalidate.InvalidateReason = InvalidateReason;
2637
2638         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2639                       AFS_TRACE_LEVEL_VERBOSE,
2640                       "AFSQueueInvalidateObject Workitem %08lX\n",
2641                       pWorkItem);
2642
2643         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2644
2645 try_exit:
2646
2647         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2648                       AFS_TRACE_LEVEL_VERBOSE,
2649                       "AFSQueueInvalidateObject Request complete Status %08lX\n",
2650                       ntStatus);
2651
2652         if( !NT_SUCCESS( ntStatus))
2653         {
2654
2655             if( pWorkItem != NULL)
2656             {
2657                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2658             }
2659
2660             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2661                           AFS_TRACE_LEVEL_ERROR,
2662                           "AFSQueueInvalidateObject Failed to queue request Status %08lX\n",
2663                           ntStatus);
2664         }
2665     }
2666     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
2667     {
2668
2669         AFSDbgLogMsg( 0,
2670                       0,
2671                       "EXCEPTION - AFSQueueInvalidateObject\n");
2672
2673         AFSDumpTraceFilesFnc();
2674     }
2675
2676     return ntStatus;
2677 }