Windows: VolumeCB->ObjectInfoTree.TreeLock Deadlock
[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                             if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1188                                                 FALSE))
1189                             {
1190
1191                                 if( pCurrentObject->Fcb != NULL)
1192                                 {
1193
1194                                     AFSRemoveFcb( &pCurrentObject->Fcb);
1195                                 }
1196
1197                                 if( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
1198                                 {
1199
1200                                     if( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb != NULL)
1201                                     {
1202
1203                                         AFSRemoveFcb( &pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
1204                                     }
1205
1206                                     AFSDeleteObjectInfo( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
1207
1208                                     ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
1209
1210                                     AFSExFreePool( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged);
1211
1212                                     AFSExFreePool( pCurrentObject->Specific.Directory.PIOCtlDirectoryCB);
1213                                 }
1214
1215                                 AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1216                                               AFS_TRACE_LEVEL_VERBOSE,
1217                                               "AFSPrimaryVolumeWorkerThread Deleting deleted object %08lX\n",
1218                                               pCurrentObject);
1219
1220                                 AFSDeleteObjectInfo( pCurrentObject);
1221
1222                                 AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1223
1224                                 pCurrentObject = pNextObject;
1225
1226                                 continue;
1227                             }
1228                             else
1229                             {
1230
1231                                 bReleaseVolumeLock = FALSE;
1232
1233                                 break;
1234                             }
1235                         }
1236
1237                         if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0 ||
1238                             ( pCurrentObject->Fcb != NULL &&
1239                               pCurrentObject->Fcb->OpenReferenceCount > 0) ||
1240                             pCurrentObject->Specific.Directory.DirectoryNodeListHead == NULL)
1241                         {
1242
1243                             pCurrentObject = pNextObject;
1244
1245                             continue;
1246                         }
1247
1248                         if( !AFSAcquireShared( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
1249                                                FALSE))
1250                         {
1251
1252                             pCurrentObject = pNextObject;
1253
1254                             continue;
1255                         }
1256
1257                         KeQueryTickCount( &liCurrentTime);
1258
1259                         pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
1260
1261                         while( pCurrentDirEntry != NULL)
1262                         {
1263
1264                             if( pCurrentDirEntry->OpenReferenceCount > 0 ||
1265                                 ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1266                                   pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0) ||
1267                                 liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
1268                                 liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
1269                                                                         pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart ||
1270                                 ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
1271                                    ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL ||
1272                                      pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)) ||
1273                                 ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE &&
1274                                   pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1275                                   pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0))
1276                             {
1277
1278                                 break;
1279                             }
1280
1281                             pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
1282                         }
1283
1284                         if( pCurrentDirEntry != NULL)
1285                         {
1286
1287                             AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1288
1289                             pCurrentObject = pNextObject;
1290
1291                             continue;
1292                         }
1293
1294                         AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1295
1296                         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1297
1298                         //
1299                         // Now acquire the locks excl
1300                         //
1301
1302                         if( AFSAcquireExcl( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
1303                                             FALSE))
1304                         {
1305
1306                             if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1307                                                 FALSE))
1308                             {
1309
1310                                 if( pCurrentObject->Specific.Directory.ChildOpenReferenceCount > 0)
1311                                 {
1312
1313                                     AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1314
1315                                     AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1316
1317                                     pCurrentObject = pNextObject;
1318
1319                                     continue;
1320                                 }
1321
1322                                 KeQueryTickCount( &liCurrentTime);
1323
1324                                 pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
1325
1326                                 while( pCurrentDirEntry != NULL)
1327                                 {
1328
1329                                     if( pCurrentDirEntry->OpenReferenceCount > 0 ||
1330                                         ( pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1331                                           pCurrentDirEntry->ObjectInformation->Fcb->OpenReferenceCount > 0) ||
1332                                         liCurrentTime.QuadPart <= pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart ||
1333                                         liCurrentTime.QuadPart - pCurrentDirEntry->ObjectInformation->LastAccessCount.QuadPart <
1334                                                                                 pControlDeviceExt->Specific.Control.ObjectLifeTimeCount.QuadPart ||
1335                                         ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
1336                                           ( pCurrentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeListHead != NULL ||
1337                                             pCurrentDirEntry->ObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)) ||
1338                                         ( pCurrentDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE &&
1339                                           pCurrentDirEntry->ObjectInformation->Fcb != NULL &&
1340                                           pCurrentDirEntry->ObjectInformation->Fcb->Specific.File.ExtentsDirtyCount > 0))
1341                                     {
1342
1343                                         break;
1344                                     }
1345
1346                                     pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
1347                                 }
1348
1349                                 if( pCurrentDirEntry != NULL)
1350                                 {
1351
1352                                     AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1353
1354                                     AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1355
1356                                     pCurrentObject = pNextObject;
1357
1358                                     continue;
1359                                 }
1360
1361                                 pCurrentDirEntry = pCurrentObject->Specific.Directory.DirectoryNodeListHead;
1362
1363                                 while( pCurrentDirEntry != NULL)
1364                                 {
1365
1366                                     pNextDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
1367
1368                                     pCurrentChildObject = pCurrentDirEntry->ObjectInformation;
1369
1370                                     pFcb = NULL;
1371
1372                                     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1373                                                   AFS_TRACE_LEVEL_VERBOSE,
1374                                                   "AFSPrimaryVolumeWorkerThread Deleting DE %wZ Object %08lX\n",
1375                                                   &pCurrentDirEntry->NameInformation.FileName,
1376                                                   pCurrentChildObject);
1377
1378                                     AFSDeleteDirEntry( pCurrentObject,
1379                                                        pCurrentDirEntry);
1380
1381                                     if( pCurrentChildObject->ObjectReferenceCount == 0)
1382                                     {
1383
1384                                         if( pCurrentChildObject->Fcb != NULL)
1385                                         {
1386
1387                                             pFcb = (AFSFcb *) InterlockedCompareExchangePointer( (PVOID *)&pCurrentChildObject->Fcb, NULL, (PVOID)pCurrentChildObject->Fcb);
1388
1389                                             lFileType = pCurrentChildObject->FileType;
1390                                         }
1391
1392                                         if( pCurrentChildObject->FileType == AFS_FILE_TYPE_DIRECTORY &&
1393                                             pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB != NULL)
1394                                         {
1395
1396                                             if( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb != NULL)
1397                                             {
1398
1399                                                 AFSRemoveFcb( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
1400                                             }
1401
1402                                             AFSDeleteObjectInfo( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
1403
1404                                             ExDeleteResourceLite( &pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
1405
1406                                             AFSExFreePool( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB->NonPaged);
1407
1408                                             AFSExFreePool( pCurrentChildObject->Specific.Directory.PIOCtlDirectoryCB);
1409                                         }
1410
1411                                         AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1412                                                       AFS_TRACE_LEVEL_VERBOSE,
1413                                                       "AFSPrimaryVolumeWorkerThread Deleting object %08lX\n",
1414                                                       pCurrentChildObject);
1415
1416                                         AFSDeleteObjectInfo( pCurrentChildObject);
1417                                     }
1418
1419                                     pCurrentDirEntry = pNextDirEntry;
1420
1421                                     if ( pFcb != NULL)
1422                                     {
1423
1424                                         if( lFileType == AFS_FILE_TYPE_FILE)
1425                                         {
1426                                             //
1427                                             // We must not hold pVolumeCB->ObjectInfoTree.TreeLock exclusive
1428                                             // across an AFSCleanupFcb call since it can deadlock with an
1429                                             // invalidation call from the service.
1430                                             //
1431
1432                                             AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1433
1434                                             AFSCleanupFcb( pFcb,
1435                                                            TRUE);
1436
1437                                             AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1438                                                             TRUE);
1439                                         }
1440
1441                                         AFSRemoveFcb( &pFcb);
1442                                     }
1443                                 }
1444
1445                                 pCurrentObject->Specific.Directory.DirectoryNodeListHead = NULL;
1446
1447                                 pCurrentObject->Specific.Directory.DirectoryNodeListTail = NULL;
1448
1449                                 pCurrentObject->Specific.Directory.ShortNameTree = NULL;
1450
1451                                 pCurrentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead = NULL;
1452
1453                                 pCurrentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead = NULL;
1454
1455                                 pCurrentObject->Specific.Directory.DirectoryNodeCount = 0;
1456
1457                                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIR_NODE_COUNT,
1458                                               AFS_TRACE_LEVEL_VERBOSE,
1459                                               "AFSPrimaryVolumeWorkerThread Reset count to 0 on parent FID %08lX-%08lX-%08lX-%08lX\n",
1460                                               pCurrentObject->FileId.Cell,
1461                                               pCurrentObject->FileId.Volume,
1462                                               pCurrentObject->FileId.Vnode,
1463                                               pCurrentObject->FileId.Unique);
1464
1465
1466                                 //
1467                                 // Clear our enumerated flag on this object so we retrieve info again on next access
1468                                 //
1469
1470                                 ClearFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1471
1472                                 AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1473                             }
1474                             else
1475                             {
1476
1477                                 AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1478
1479                                 bReleaseVolumeLock = FALSE;
1480
1481                                 break;
1482                             }
1483
1484                             AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1485                         }
1486                         else
1487                         {
1488
1489                             //
1490                             // Try to grab the volume lock again ... no problem if we don't
1491                             //
1492
1493                             if( !AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1494                                                  FALSE))
1495                             {
1496
1497                                 bReleaseVolumeLock = FALSE;
1498
1499                                 break;
1500                             }
1501                         }
1502
1503                         if( pCurrentObject != &pVolumeCB->ObjectInformation)
1504                         {
1505
1506                             pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
1507
1508                             if( pCurrentObject == NULL &&
1509                                 pVolumeCB != AFSGlobalRoot)
1510                             {
1511
1512                                 pCurrentObject = &pVolumeCB->ObjectInformation;
1513                             }
1514                         }
1515                         else
1516                         {
1517
1518                             pCurrentObject = NULL;
1519                         }
1520
1521                         continue;
1522                     }
1523                     else if( pCurrentObject->FileType == AFS_FILE_TYPE_FILE)
1524                     {
1525
1526                         if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DELETED) &&
1527                             pCurrentObject->ObjectReferenceCount == 0 &&
1528                             ( pCurrentObject->Fcb == NULL ||
1529                               pCurrentObject->Fcb->OpenReferenceCount == 0))
1530                         {
1531
1532                             pFcb = (AFSFcb *) InterlockedCompareExchangePointer( (PVOID *)&pCurrentObject->Fcb, NULL, (PVOID)pCurrentObject->Fcb);
1533
1534                             if( pFcb != NULL)
1535                             {
1536
1537                                 AFSCleanupFcb( pFcb,
1538                                                TRUE);
1539
1540                                 AFSRemoveFcb( &pFcb);
1541                             }
1542
1543                             AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1544
1545                             if( AFSAcquireExcl( pVolumeCB->ObjectInfoTree.TreeLock,
1546                                                 FALSE))
1547                             {
1548
1549                                 AFSDeleteObjectInfo( pCurrentObject);
1550
1551                                 AFSConvertToShared( pVolumeCB->ObjectInfoTree.TreeLock);
1552
1553                                 pCurrentObject = pNextObject;
1554
1555                                 continue;
1556                             }
1557                             else
1558                             {
1559
1560                                 bReleaseVolumeLock = FALSE;
1561
1562                                 break;
1563                             }
1564                         }
1565                         else if( pCurrentObject->Fcb != NULL)
1566                         {
1567
1568                             AFSCleanupFcb( pCurrentObject->Fcb,
1569                                            FALSE);
1570                         }
1571                     }
1572
1573                     pCurrentObject = pNextObject;
1574                 }
1575
1576                 if( bReleaseVolumeLock)
1577                 {
1578
1579                     AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1580                 }
1581             }
1582
1583             //
1584             // Next volume cb
1585             //
1586
1587             AFSReleaseResource( pVolumeCB->VolumeLock);
1588
1589             AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
1590                               TRUE);
1591
1592             pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
1593         }
1594
1595         AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1596
1597     } // worker thread loop
1598
1599     KeCancelTimer( &Timer);
1600
1601     AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1602                   AFS_TRACE_LEVEL_VERBOSE,
1603                   "AFSPrimaryVolumeWorkerThread Exiting\n");
1604
1605     lCount = InterlockedDecrement( &pControlDeviceExt->Specific.Control.VolumeWorkerThreadCount);
1606
1607     if( lCount == 0)
1608     {
1609
1610         KeSetEvent( &pControlDeviceExt->Specific.Control.VolumeWorkerCloseEvent,
1611                     0,
1612                     FALSE);
1613     }
1614
1615     PsTerminateSystemThread( 0);
1616
1617     return;
1618 }
1619
1620 void
1621 AFSVolumeWorkerThread( IN PVOID Context)
1622 {
1623
1624     NTSTATUS ntStatus = STATUS_SUCCESS;
1625     AFSVolumeCB *pVolumeCB = (AFSVolumeCB * )Context;
1626     AFSWorkQueueContext *pPoolContext = (AFSWorkQueueContext *)&pVolumeCB->VolumeWorkerContext;
1627     AFSDeviceExt *pControlDeviceExt = NULL;
1628     AFSDeviceExt *pRDRDeviceExt = NULL;
1629     BOOLEAN exitThread = FALSE;
1630     LARGE_INTEGER DueTime;
1631     LONG TimeOut;
1632     KTIMER Timer;
1633     LONG lCount;
1634
1635     pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
1636
1637     pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1638
1639     //
1640     // Initialize the timer for the worker thread
1641     //
1642
1643     DueTime.QuadPart = -(5000);
1644
1645     TimeOut = 5000;
1646
1647     KeInitializeTimerEx( &Timer,
1648                          SynchronizationTimer);
1649
1650     KeSetTimerEx( &Timer,
1651                   DueTime,
1652                   TimeOut,
1653                   NULL);
1654
1655     //
1656     // Indicate that we are initialized and ready
1657     //
1658
1659     KeSetEvent( &pPoolContext->WorkerThreadReady,
1660                 0,
1661                 FALSE);
1662
1663     //
1664     // Indicate we are initialized
1665     //
1666
1667     SetFlag( pPoolContext->State, AFS_WORKER_INITIALIZED);
1668
1669     while( BooleanFlagOn( pPoolContext->State, AFS_WORKER_PROCESS_REQUESTS))
1670     {
1671
1672         ntStatus = KeWaitForSingleObject( &Timer,
1673                                           Executive,
1674                                           KernelMode,
1675                                           FALSE,
1676                                           NULL);
1677
1678         if( !NT_SUCCESS( ntStatus))
1679         {
1680
1681             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1682                           AFS_TRACE_LEVEL_ERROR,
1683                           "AFSVolumeWorkerThread Wait for queue items failed Status %08lX\n", ntStatus);
1684         }
1685         else
1686         {
1687
1688             //
1689             // If we are in shutdown mode and the dirty flag is clear then get out now
1690             //
1691
1692             if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
1693             {
1694
1695                 break;
1696             }
1697         }
1698     } // worker thread loop
1699
1700     KeCancelTimer( &Timer);
1701
1702     lCount = InterlockedDecrement( &pControlDeviceExt->Specific.Control.VolumeWorkerThreadCount);
1703
1704     if( lCount == 0)
1705     {
1706
1707         KeSetEvent( &pControlDeviceExt->Specific.Control.VolumeWorkerCloseEvent,
1708                     0,
1709                     FALSE);
1710     }
1711
1712     PsTerminateSystemThread( 0);
1713
1714     return;
1715 }
1716
1717 NTSTATUS
1718 AFSInsertWorkitem( IN AFSWorkItem *WorkItem)
1719 {
1720
1721     NTSTATUS ntStatus = STATUS_SUCCESS;
1722     AFSDeviceExt *pDevExt = NULL;
1723     LONG lCount;
1724
1725     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1726
1727     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1728                   AFS_TRACE_LEVEL_VERBOSE,
1729                   "AFSInsertWorkitem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1730                   &pDevExt->Specific.Library.QueueLock,
1731                   PsGetCurrentThread());
1732
1733     AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
1734                     TRUE);
1735
1736     lCount = InterlockedIncrement( &pDevExt->Specific.Library.QueueItemCount);
1737
1738     AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1739                   AFS_TRACE_LEVEL_VERBOSE,
1740                   "AFSInsertWorkitem Inserting work item %08lX Count %08lX\n",
1741                   WorkItem,
1742                   lCount);
1743
1744     if( pDevExt->Specific.Library.QueueTail != NULL) // queue already has nodes
1745     {
1746
1747         pDevExt->Specific.Library.QueueTail->next = WorkItem;
1748     }
1749     else // first node
1750     {
1751
1752         pDevExt->Specific.Library.QueueHead = WorkItem;
1753     }
1754
1755     WorkItem->next = NULL;
1756     pDevExt->Specific.Library.QueueTail = WorkItem;
1757
1758     // indicate that the queue has nodes
1759     KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
1760                 0,
1761                 FALSE);
1762
1763     AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
1764
1765     return ntStatus;
1766 }
1767
1768 NTSTATUS
1769 AFSInsertIOWorkitem( IN AFSWorkItem *WorkItem)
1770 {
1771
1772     NTSTATUS ntStatus = STATUS_SUCCESS;
1773     AFSDeviceExt *pDevExt = NULL;
1774     LONG lCount;
1775
1776     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1777
1778     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1779                   AFS_TRACE_LEVEL_VERBOSE,
1780                   "AFSInsertIOWorkitem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1781                   &pDevExt->Specific.Library.IOQueueLock,
1782                   PsGetCurrentThread());
1783
1784     AFSAcquireExcl( &pDevExt->Specific.Library.IOQueueLock,
1785                     TRUE);
1786
1787     lCount = InterlockedIncrement( &pDevExt->Specific.Library.IOQueueItemCount);
1788
1789     AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1790                   AFS_TRACE_LEVEL_VERBOSE,
1791                   "AFSInsertWorkitem Inserting IO work item %08lX Count %08lX\n",
1792                   WorkItem,
1793                   lCount);
1794
1795     if( pDevExt->Specific.Library.IOQueueTail != NULL) // queue already has nodes
1796     {
1797
1798         pDevExt->Specific.Library.IOQueueTail->next = WorkItem;
1799     }
1800     else // first node
1801     {
1802
1803         pDevExt->Specific.Library.IOQueueHead = WorkItem;
1804     }
1805
1806     WorkItem->next = NULL;
1807     pDevExt->Specific.Library.IOQueueTail = WorkItem;
1808
1809     // indicate that the queue has nodes
1810     KeSetEvent( &(pDevExt->Specific.Library.IOWorkerQueueHasItems),
1811                 0,
1812                 FALSE);
1813
1814     AFSReleaseResource( &pDevExt->Specific.Library.IOQueueLock);
1815
1816     return ntStatus;
1817 }
1818
1819 NTSTATUS
1820 AFSInsertWorkitemAtHead( IN AFSWorkItem *WorkItem)
1821 {
1822
1823     NTSTATUS ntStatus = STATUS_SUCCESS;
1824     AFSDeviceExt *pDevExt = NULL;
1825     LONG lCount;
1826
1827     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1828
1829     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1830                   AFS_TRACE_LEVEL_VERBOSE,
1831                   "AFSInsertWorkitemAtHead Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1832                   &pDevExt->Specific.Library.QueueLock,
1833                   PsGetCurrentThread());
1834
1835     AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
1836                     TRUE);
1837
1838     WorkItem->next = pDevExt->Specific.Library.QueueHead;
1839
1840     pDevExt->Specific.Library.QueueHead = WorkItem;
1841
1842     lCount = InterlockedIncrement( &pDevExt->Specific.Library.QueueItemCount);
1843
1844     AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1845                   AFS_TRACE_LEVEL_VERBOSE,
1846                   "AFSInsertWorkitemAtHead Inserting work item %08lX Count %08lX\n",
1847                   WorkItem,
1848                   lCount);
1849
1850     //
1851     // indicate that the queue has nodes
1852     //
1853
1854     KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
1855                 0,
1856                 FALSE);
1857
1858     AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
1859
1860     return ntStatus;
1861 }
1862
1863 AFSWorkItem *
1864 AFSRemoveWorkItem()
1865 {
1866
1867     NTSTATUS ntStatus = STATUS_SUCCESS;
1868     AFSWorkItem        *pWorkItem = NULL;
1869     AFSDeviceExt *pDevExt = NULL;
1870     LONG lCount;
1871
1872     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1873
1874     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1875                   AFS_TRACE_LEVEL_VERBOSE,
1876                   "AFSRemoveWorkItem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1877                   &pDevExt->Specific.Library.QueueLock,
1878                   PsGetCurrentThread());
1879
1880     AFSAcquireExcl( &pDevExt->Specific.Library.QueueLock,
1881                     TRUE);
1882
1883     if( pDevExt->Specific.Library.QueueHead != NULL) // queue has nodes
1884     {
1885
1886         pWorkItem = pDevExt->Specific.Library.QueueHead;
1887
1888         lCount = InterlockedDecrement( &pDevExt->Specific.Library.QueueItemCount);
1889
1890         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1891                       AFS_TRACE_LEVEL_VERBOSE,
1892                       "AFSRemoveWorkItem Removing work item %08lX Count %08lX Thread %08lX\n",
1893                       pWorkItem,
1894                       lCount,
1895                       PsGetCurrentThreadId());
1896
1897         pDevExt->Specific.Library.QueueHead = pDevExt->Specific.Library.QueueHead->next;
1898
1899         if( pDevExt->Specific.Library.QueueHead == NULL) // if queue just became empty
1900         {
1901
1902             pDevExt->Specific.Library.QueueTail = NULL;
1903         }
1904         else
1905         {
1906
1907             //
1908             // Wake up another worker
1909             //
1910
1911             KeSetEvent( &(pDevExt->Specific.Library.WorkerQueueHasItems),
1912                         0,
1913                         FALSE);
1914         }
1915     }
1916
1917     AFSReleaseResource( &pDevExt->Specific.Library.QueueLock);
1918
1919     return pWorkItem;
1920 }
1921
1922 AFSWorkItem *
1923 AFSRemoveIOWorkItem()
1924 {
1925
1926     NTSTATUS ntStatus = STATUS_SUCCESS;
1927     AFSWorkItem        *pWorkItem = NULL;
1928     AFSDeviceExt *pDevExt = NULL;
1929     LONG lCount;
1930
1931     pDevExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
1932
1933     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1934                   AFS_TRACE_LEVEL_VERBOSE,
1935                   "AFSRemoveIOWorkItem Acquiring Control QueueLock lock %08lX EXCL %08lX\n",
1936                   &pDevExt->Specific.Library.IOQueueLock,
1937                   PsGetCurrentThread());
1938
1939     AFSAcquireExcl( &pDevExt->Specific.Library.IOQueueLock,
1940                     TRUE);
1941
1942     if( pDevExt->Specific.Library.IOQueueHead != NULL) // queue has nodes
1943     {
1944
1945         pWorkItem = pDevExt->Specific.Library.IOQueueHead;
1946
1947         lCount = InterlockedDecrement( &pDevExt->Specific.Library.IOQueueItemCount);
1948
1949         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
1950                       AFS_TRACE_LEVEL_VERBOSE,
1951                       "AFSRemoveWorkItem Removing work item %08lX Count %08lX Thread %08lX\n",
1952                       pWorkItem,
1953                       lCount,
1954                       PsGetCurrentThreadId());
1955
1956         pDevExt->Specific.Library.IOQueueHead = pDevExt->Specific.Library.IOQueueHead->next;
1957
1958         if( pDevExt->Specific.Library.IOQueueHead == NULL) // if queue just became empty
1959         {
1960
1961             pDevExt->Specific.Library.IOQueueTail = NULL;
1962         }
1963         else
1964         {
1965
1966             //
1967             // Wake up another worker
1968             //
1969
1970             KeSetEvent( &(pDevExt->Specific.Library.IOWorkerQueueHasItems),
1971                         0,
1972                         FALSE);
1973         }
1974     }
1975
1976     AFSReleaseResource( &pDevExt->Specific.Library.IOQueueLock);
1977
1978     return pWorkItem;
1979 }
1980
1981 NTSTATUS
1982 AFSQueueWorkerRequest( IN AFSWorkItem *WorkItem)
1983 {
1984
1985     NTSTATUS ntStatus = STATUS_SUCCESS;
1986     AFSDeviceExt *pDevExt = NULL;
1987     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
1988
1989     //
1990     // Submit the work item to the worker
1991     //
1992
1993     ntStatus = AFSInsertWorkitem( WorkItem);
1994
1995     if( bWait)
1996     {
1997
1998         //
1999         // Sync request so block on the work item event
2000         //
2001
2002         ntStatus = KeWaitForSingleObject( &WorkItem->Event,
2003                                           Executive,
2004                                           KernelMode,
2005                                           FALSE,
2006                                           NULL);
2007     }
2008
2009     return ntStatus;
2010 }
2011
2012 NTSTATUS
2013 AFSQueueIOWorkerRequest( IN AFSWorkItem *WorkItem)
2014 {
2015
2016     NTSTATUS ntStatus = STATUS_SUCCESS;
2017     AFSDeviceExt *pDevExt = NULL;
2018     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
2019
2020     //
2021     // Submit the work item to the worker
2022     //
2023
2024     ntStatus = AFSInsertIOWorkitem( WorkItem);
2025
2026     if( bWait)
2027     {
2028
2029         //
2030         // Sync request so block on the work item event
2031         //
2032
2033         ntStatus = KeWaitForSingleObject( &WorkItem->Event,
2034                                           Executive,
2035                                           KernelMode,
2036                                           FALSE,
2037                                           NULL);
2038     }
2039
2040     return ntStatus;
2041 }
2042
2043 NTSTATUS
2044 AFSQueueWorkerRequestAtHead( IN AFSWorkItem *WorkItem)
2045 {
2046
2047     NTSTATUS ntStatus = STATUS_SUCCESS;
2048     AFSDeviceExt *pDevExt = NULL;
2049     BOOLEAN bWait = BooleanFlagOn( WorkItem->RequestFlags, AFS_SYNCHRONOUS_REQUEST);
2050
2051     //
2052     // Submit the work item to the worker
2053     //
2054
2055     ntStatus = AFSInsertWorkitemAtHead( WorkItem);
2056
2057     if( bWait)
2058     {
2059
2060         //
2061         // Sync request so block on the work item event
2062         //
2063
2064         ntStatus = KeWaitForSingleObject( &WorkItem->Event,
2065                                           Executive,
2066                                           KernelMode,
2067                                           FALSE,
2068                                           NULL);
2069     }
2070
2071     return ntStatus;
2072 }
2073
2074 NTSTATUS
2075 AFSQueueFlushExtents( IN AFSFcb *Fcb,
2076                       IN GUID *AuthGroup)
2077 {
2078
2079     NTSTATUS ntStatus = STATUS_SUCCESS;
2080     AFSDeviceExt *pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2081     AFSWorkItem *pWorkItem = NULL;
2082     LONG lCount;
2083
2084     __try
2085     {
2086
2087         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2088                       AFS_TRACE_LEVEL_VERBOSE,
2089                       "AFSQueueFlushExtents Queuing request for FID %08lX-%08lX-%08lX-%08lX\n",
2090                       Fcb->ObjectInformation->FileId.Cell,
2091                       Fcb->ObjectInformation->FileId.Volume,
2092                       Fcb->ObjectInformation->FileId.Vnode,
2093                       Fcb->ObjectInformation->FileId.Unique);
2094
2095         //
2096         // Increment our flush count here just to keep the number of items in the
2097         // queue down. We'll decrement it just below.
2098         //
2099
2100         lCount = InterlockedIncrement( &Fcb->Specific.File.QueuedFlushCount);
2101
2102         if( lCount > 3)
2103         {
2104
2105             AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2106                           AFS_TRACE_LEVEL_VERBOSE,
2107                           "AFSQueueFlushExtents Max queued items for FID %08lX-%08lX-%08lX-%08lX\n",
2108                           Fcb->ObjectInformation->FileId.Cell,
2109                           Fcb->ObjectInformation->FileId.Volume,
2110                           Fcb->ObjectInformation->FileId.Vnode,
2111                           Fcb->ObjectInformation->FileId.Unique);
2112
2113             try_return( ntStatus);
2114         }
2115
2116         if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
2117         {
2118
2119             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2120                           AFS_TRACE_LEVEL_ERROR,
2121                           "AFSQueueFlushExtents Failing request, in shutdown\n");
2122
2123             try_return( ntStatus = STATUS_TOO_LATE);
2124         }
2125
2126         //
2127         // Allocate our request structure and send it to the worker
2128         //
2129
2130         pWorkItem = (AFSWorkItem *)AFSLibExAllocatePoolWithTag( NonPagedPool,
2131                                                                 sizeof( AFSWorkItem),
2132                                                                 AFS_WORK_ITEM_TAG);
2133
2134         if( pWorkItem == NULL)
2135         {
2136
2137             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2138                           AFS_TRACE_LEVEL_ERROR,
2139                           "AFSQueueFlushExtents Failed to allocate work item\n");
2140
2141             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2142         }
2143
2144         RtlZeroMemory( pWorkItem,
2145                        sizeof( AFSWorkItem));
2146
2147         pWorkItem->Size = sizeof( AFSWorkItem);
2148
2149         pWorkItem->ProcessID = (ULONGLONG)PsGetCurrentProcessId();
2150
2151         pWorkItem->RequestType = AFS_WORK_FLUSH_FCB;
2152
2153         RtlCopyMemory( &pWorkItem->AuthGroup,
2154                        AuthGroup,
2155                        sizeof( GUID));
2156
2157         pWorkItem->Specific.Fcb.Fcb = Fcb;
2158
2159         lCount = InterlockedIncrement( &Fcb->OpenReferenceCount);
2160
2161         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2162                       AFS_TRACE_LEVEL_VERBOSE,
2163                       "AFSQueueFlushExtents Increment count on Fcb %08lX Cnt %d\n",
2164                       Fcb,
2165                       lCount);
2166
2167         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2168                       AFS_TRACE_LEVEL_VERBOSE,
2169                       "AFSQueueFlushExtents Workitem %08lX for FID %08lX-%08lX-%08lX-%08lX\n",
2170                       pWorkItem,
2171                       Fcb->ObjectInformation->FileId.Cell,
2172                       Fcb->ObjectInformation->FileId.Volume,
2173                       Fcb->ObjectInformation->FileId.Vnode,
2174                       Fcb->ObjectInformation->FileId.Unique);
2175
2176         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2177
2178 try_exit:
2179
2180         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2181                       AFS_TRACE_LEVEL_VERBOSE,
2182                       "AFSQueueFlushExtents Request complete Status %08lX FID %08lX-%08lX-%08lX-%08lX\n",
2183                       Fcb->ObjectInformation->FileId.Cell,
2184                       Fcb->ObjectInformation->FileId.Volume,
2185                       Fcb->ObjectInformation->FileId.Vnode,
2186                       Fcb->ObjectInformation->FileId.Unique,
2187                       ntStatus);
2188
2189         //
2190         // Remove the count we added above
2191         //
2192
2193         lCount = InterlockedDecrement( &Fcb->Specific.File.QueuedFlushCount);
2194
2195         if( lCount == 0)
2196         {
2197
2198             KeSetEvent( &Fcb->NPFcb->Specific.File.QueuedFlushEvent,
2199                         0,
2200                         FALSE);
2201         }
2202
2203         if( !NT_SUCCESS( ntStatus))
2204         {
2205
2206             if( pWorkItem != NULL)
2207             {
2208
2209                 lCount = InterlockedDecrement( &Fcb->OpenReferenceCount);
2210
2211                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2212             }
2213
2214             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2215                           AFS_TRACE_LEVEL_ERROR,
2216                           "AFSQueueFlushExtents Failed to queue request Status %08lX\n", ntStatus);
2217         }
2218     }
2219     __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
2220     {
2221
2222         AFSDbgLogMsg( 0,
2223                       0,
2224                       "EXCEPTION - AFSQueueFlushExtents\n");
2225     }
2226
2227     return ntStatus;
2228 }
2229
2230 NTSTATUS
2231 AFSQueueAsyncRead( IN PDEVICE_OBJECT DeviceObject,
2232                    IN PIRP Irp,
2233                    IN HANDLE CallerProcess)
2234 {
2235
2236     NTSTATUS ntStatus = STATUS_SUCCESS;
2237     AFSWorkItem *pWorkItem = NULL;
2238
2239     __try
2240     {
2241
2242         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2243                       AFS_TRACE_LEVEL_VERBOSE,
2244                       "AFSQueueAsyncRead Queuing request for Irp %08lX\n",
2245                       Irp);
2246
2247         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2248                                                                  sizeof(AFSWorkItem),
2249                                                                  AFS_WORK_ITEM_TAG);
2250         if (NULL == pWorkItem)
2251         {
2252
2253             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2254                           AFS_TRACE_LEVEL_ERROR,
2255                           "AFSQueueAsyncRead Failed to allocate work item\n");
2256
2257             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2258         }
2259
2260         RtlZeroMemory( pWorkItem,
2261                        sizeof(AFSWorkItem));
2262
2263         pWorkItem->Size = sizeof( AFSWorkItem);
2264
2265         pWorkItem->RequestType = AFS_WORK_ASYNCH_READ;
2266
2267         pWorkItem->Specific.AsynchIo.Device = DeviceObject;
2268
2269         pWorkItem->Specific.AsynchIo.Irp = Irp;
2270
2271         pWorkItem->Specific.AsynchIo.CallingProcess = CallerProcess;
2272
2273         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2274                       AFS_TRACE_LEVEL_VERBOSE,
2275                       "AFSQueueAsyncRead Workitem %08lX for Irp %08lX\n",
2276                       pWorkItem,
2277                       Irp);
2278
2279         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2280
2281 try_exit:
2282
2283         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2284                       AFS_TRACE_LEVEL_VERBOSE,
2285                       "AFSQueueAsyncRead Request for Irp %08lX complete Status %08lX\n",
2286                       Irp,
2287                       ntStatus);
2288
2289         if( !NT_SUCCESS( ntStatus))
2290         {
2291
2292             if( pWorkItem != NULL)
2293             {
2294
2295                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2296             }
2297
2298             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2299                           AFS_TRACE_LEVEL_ERROR,
2300                           "AFSQueueAsyncRead Failed to queue request Status %08lX\n", ntStatus);
2301         }
2302     }
2303     __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
2304     {
2305
2306         AFSDbgLogMsg( 0,
2307                       0,
2308                       "EXCEPTION - AFSQueueAsyncRead\n");
2309     }
2310
2311     return ntStatus;
2312 }
2313
2314 NTSTATUS
2315 AFSQueueAsyncWrite( IN PDEVICE_OBJECT DeviceObject,
2316                     IN PIRP Irp,
2317                     IN HANDLE CallerProcess)
2318 {
2319
2320     NTSTATUS ntStatus = STATUS_SUCCESS;
2321     AFSWorkItem *pWorkItem = NULL;
2322
2323     __try
2324     {
2325
2326         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2327                       AFS_TRACE_LEVEL_VERBOSE,
2328                       "AFSQueueAsyncWrite Queuing request for Irp %08lX\n",
2329                       Irp);
2330
2331         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2332                                                                  sizeof(AFSWorkItem),
2333                                                                  AFS_WORK_ITEM_TAG);
2334         if (NULL == pWorkItem)
2335         {
2336
2337             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2338                           AFS_TRACE_LEVEL_ERROR,
2339                           "AFSQueueAsyncWrite Failed to allocate work item\n");
2340
2341             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2342         }
2343
2344         RtlZeroMemory( pWorkItem,
2345                        sizeof(AFSWorkItem));
2346
2347         pWorkItem->Size = sizeof( AFSWorkItem);
2348
2349         pWorkItem->RequestType = AFS_WORK_ASYNCH_WRITE;
2350
2351         pWorkItem->Specific.AsynchIo.Device = DeviceObject;
2352
2353         pWorkItem->Specific.AsynchIo.Irp = Irp;
2354
2355         pWorkItem->Specific.AsynchIo.CallingProcess = CallerProcess;
2356
2357         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2358                       AFS_TRACE_LEVEL_VERBOSE,
2359                       "AFSQueueAsyncWrite Workitem %08lX for Irp %08lX\n",
2360                       pWorkItem,
2361                       Irp);
2362
2363         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2364
2365 try_exit:
2366
2367         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2368                       AFS_TRACE_LEVEL_VERBOSE,
2369                       "AFSQueueAsyncWrite Request for Irp %08lX complete Status %08lX\n",
2370                       Irp,
2371                       ntStatus);
2372
2373         if( !NT_SUCCESS( ntStatus))
2374         {
2375
2376             if( pWorkItem != NULL)
2377             {
2378
2379                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2380             }
2381
2382             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2383                           AFS_TRACE_LEVEL_ERROR,
2384                           "AFSQueueAsyncWrite Failed to queue request Status %08lX\n", ntStatus);
2385         }
2386     }
2387     __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
2388     {
2389
2390         AFSDbgLogMsg( 0,
2391                       0,
2392                       "EXCEPTION - AFSQueueAsyncWrite\n");
2393     }
2394
2395     return ntStatus;
2396 }
2397
2398 NTSTATUS
2399 AFSQueueGlobalRootEnumeration()
2400 {
2401
2402     NTSTATUS ntStatus = STATUS_SUCCESS;
2403     AFSWorkItem *pWorkItem = NULL;
2404
2405     __try
2406     {
2407
2408         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2409                                                                  sizeof(AFSWorkItem),
2410                                                                  AFS_WORK_ITEM_TAG);
2411         if (NULL == pWorkItem)
2412         {
2413
2414             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2415                           AFS_TRACE_LEVEL_ERROR,
2416                           "AFSQueueGlobalRootEnumeration Failed to allocate work item\n");
2417
2418             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2419         }
2420
2421         RtlZeroMemory( pWorkItem,
2422                        sizeof(AFSWorkItem));
2423
2424         pWorkItem->Size = sizeof( AFSWorkItem);
2425
2426         pWorkItem->RequestType = AFS_WORK_ENUMERATE_GLOBAL_ROOT;
2427
2428         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2429                       AFS_TRACE_LEVEL_VERBOSE,
2430                       "AFSQueueGlobalRootEnumeration Workitem %08lX\n",
2431                       pWorkItem);
2432
2433         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2434
2435 try_exit:
2436
2437         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2438                       AFS_TRACE_LEVEL_VERBOSE,
2439                       "AFSQueueGlobalRootEnumeration Request complete Status %08lX\n",
2440                       ntStatus);
2441
2442         if( !NT_SUCCESS( ntStatus))
2443         {
2444
2445             if( pWorkItem != NULL)
2446             {
2447
2448                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2449             }
2450
2451             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2452                           AFS_TRACE_LEVEL_ERROR,
2453                           "AFSQueueGlobalRootEnumeration Failed to queue request Status %08lX\n",
2454                           ntStatus);
2455         }
2456     }
2457     __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
2458     {
2459
2460         AFSDbgLogMsg( 0,
2461                       0,
2462                       "EXCEPTION - AFSQueueGlobalRootEnumeration\n");
2463     }
2464
2465     return ntStatus;
2466 }
2467
2468 NTSTATUS
2469 AFSQueueStartIos( IN PFILE_OBJECT CacheFileObject,
2470                   IN UCHAR FunctionCode,
2471                   IN ULONG RequestFlags,
2472                   IN AFSIoRun *IoRuns,
2473                   IN ULONG RunCount,
2474                   IN AFSGatherIo *GatherIo)
2475 {
2476
2477     NTSTATUS ntStatus = STATUS_SUCCESS;
2478     AFSDeviceExt *pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2479     AFSWorkItem *pWorkItem = NULL;
2480
2481     __try
2482     {
2483
2484         if( BooleanFlagOn( pRDRDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
2485         {
2486
2487             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2488                           AFS_TRACE_LEVEL_ERROR,
2489                           "AFSQueueStartIos Failing request, in shutdown\n");
2490
2491             try_return( ntStatus = STATUS_TOO_LATE);
2492         }
2493
2494         //
2495         // Allocate our request structure and send it to the worker
2496         //
2497
2498         pWorkItem = (AFSWorkItem *)AFSLibExAllocatePoolWithTag( NonPagedPool,
2499                                                                 sizeof( AFSWorkItem),
2500                                                                 AFS_WORK_ITEM_TAG);
2501
2502         if( pWorkItem == NULL)
2503         {
2504
2505             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2506                           AFS_TRACE_LEVEL_ERROR,
2507                           "AFSQueueStartIos Failed to allocate work item\n");
2508
2509             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2510         }
2511
2512         RtlZeroMemory( pWorkItem,
2513                        sizeof( AFSWorkItem));
2514
2515         KeInitializeEvent( &pWorkItem->Event,
2516                            NotificationEvent,
2517                            FALSE);
2518
2519         pWorkItem->Size = sizeof( AFSWorkItem);
2520
2521         pWorkItem->ProcessID = (ULONGLONG)PsGetCurrentProcessId();
2522
2523         pWorkItem->RequestType = AFS_WORK_START_IOS;
2524
2525         pWorkItem->Specific.CacheAccess.CacheFileObject = CacheFileObject;
2526
2527         pWorkItem->Specific.CacheAccess.FunctionCode = FunctionCode;
2528
2529         pWorkItem->Specific.CacheAccess.RequestFlags = RequestFlags;
2530
2531         pWorkItem->Specific.CacheAccess.IoRuns = IoRuns;
2532
2533         pWorkItem->Specific.CacheAccess.RunCount = RunCount;
2534
2535         pWorkItem->Specific.CacheAccess.GatherIo = GatherIo;
2536
2537         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2538                       AFS_TRACE_LEVEL_VERBOSE,
2539                       "AFSQueueStartIos Queuing IO Workitem %08lX\n",
2540                       pWorkItem);
2541
2542         ntStatus = AFSQueueIOWorkerRequest( pWorkItem);
2543
2544 try_exit:
2545
2546         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2547                       AFS_TRACE_LEVEL_VERBOSE,
2548                       "AFSQueueStartIos Request complete Status %08lX\n",
2549                       ntStatus);
2550
2551         if( !NT_SUCCESS( ntStatus))
2552         {
2553
2554             if( pWorkItem != NULL)
2555             {
2556
2557                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2558             }
2559         }
2560     }
2561     __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
2562     {
2563
2564         AFSDbgLogMsg( 0,
2565                       0,
2566                       "EXCEPTION - AFSQueueStartIos\n");
2567     }
2568
2569     return ntStatus;
2570 }
2571
2572 NTSTATUS
2573 AFSQueueInvalidateObject( IN AFSObjectInfoCB *ObjectInfo,
2574                           IN ULONG InvalidateReason)
2575 {
2576
2577     NTSTATUS ntStatus = STATUS_SUCCESS;
2578     AFSWorkItem *pWorkItem = NULL;
2579
2580     __try
2581     {
2582
2583         pWorkItem = (AFSWorkItem *) AFSLibExAllocatePoolWithTag( NonPagedPool,
2584                                                                  sizeof(AFSWorkItem),
2585                                                                  AFS_WORK_ITEM_TAG);
2586         if (NULL == pWorkItem)
2587         {
2588
2589             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2590                           AFS_TRACE_LEVEL_ERROR,
2591                           "AFSQueueInvalidateObject Failed to allocate work item\n");
2592
2593             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2594         }
2595
2596         RtlZeroMemory( pWorkItem,
2597                        sizeof(AFSWorkItem));
2598
2599         pWorkItem->Size = sizeof( AFSWorkItem);
2600
2601         pWorkItem->RequestType = AFS_WORK_INVALIDATE_OBJECT;
2602
2603         pWorkItem->Specific.Invalidate.ObjectInfo = ObjectInfo;
2604
2605         pWorkItem->Specific.Invalidate.InvalidateReason = InvalidateReason;
2606
2607         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2608                       AFS_TRACE_LEVEL_VERBOSE,
2609                       "AFSQueueInvalidateObject Workitem %08lX\n",
2610                       pWorkItem);
2611
2612         ntStatus = AFSQueueWorkerRequest( pWorkItem);
2613
2614 try_exit:
2615
2616         AFSDbgLogMsg( AFS_SUBSYSTEM_WORKER_PROCESSING,
2617                       AFS_TRACE_LEVEL_VERBOSE,
2618                       "AFSQueueInvalidateObject Request complete Status %08lX\n",
2619                       ntStatus);
2620
2621         if( !NT_SUCCESS( ntStatus))
2622         {
2623
2624             if( pWorkItem != NULL)
2625             {
2626                 ExFreePoolWithTag( pWorkItem, AFS_WORK_ITEM_TAG);
2627             }
2628
2629             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2630                           AFS_TRACE_LEVEL_ERROR,
2631                           "AFSQueueInvalidateObject Failed to queue request Status %08lX\n",
2632                           ntStatus);
2633         }
2634     }
2635     __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
2636     {
2637
2638         AFSDbgLogMsg( 0,
2639                       0,
2640                       "EXCEPTION - AFSQueueInvalidateObject\n");
2641     }
2642
2643     return ntStatus;
2644 }