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