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