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