Windows: Trace DirectoryCB allocations
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSGeneric.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: AFSGeneric.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 //
42 // Function: AFSExceptionFilter
43 //
44 // Description:
45 //
46 //      This function is the exception handler
47 //
48 // Return:
49 //
50 //      A status is returned for the function
51 //
52
53 ULONG
54 AFSExceptionFilter( IN CHAR *FunctionString,
55                     IN ULONG Code,
56                     IN PEXCEPTION_POINTERS ExceptPtrs)
57 {
58
59     UNREFERENCED_PARAMETER(Code);
60     PEXCEPTION_RECORD ExceptRec;
61     PCONTEXT Context;
62
63     __try
64     {
65
66         ExceptRec = ExceptPtrs->ExceptionRecord;
67
68         Context = ExceptPtrs->ContextRecord;
69
70         AFSDbgLogMsg( 0,
71                       0,
72                       "AFSExceptionFilter (Library) - EXR %p CXR %p Function %s Code %08lX Address %p Routine %p\n",
73                       ExceptRec,
74                       Context,
75                       FunctionString,
76                       ExceptRec->ExceptionCode,
77                       ExceptRec->ExceptionAddress,
78                       (void *)AFSExceptionFilter);
79
80         DbgPrint("**** Exception Caught in AFS Redirector Library ****\n");
81
82         DbgPrint("\n\nPerform the following WnDbg Cmds:\n");
83         DbgPrint("\n\t.exr %p ;  .cxr %p\n\n", ExceptRec, Context);
84
85         DbgPrint("**** Exception Complete from AFS Redirector Library ****\n");
86
87         if( BooleanFlagOn( AFSDebugFlags, AFS_DBG_BUGCHECK_EXCEPTION))
88         {
89
90             KeBugCheck( (ULONG)-2);
91         }
92         else
93         {
94
95             AFSBreakPoint();
96         }
97     }
98     __except( EXCEPTION_EXECUTE_HANDLER)
99     {
100
101         NOTHING;
102     }
103
104     return EXCEPTION_EXECUTE_HANDLER;
105 }
106
107 //
108 // Function: AFSLibExAllocatePoolWithTag()
109 //
110 // Purpose: Allocate Pool Memory.  If BugCheck Exception flag
111 //          is configured on, then bugcheck the system if
112 //          a memory allocation fails.  The routine should be
113 //          used for all memory allocations that are to be freed
114 //          when the library is unloaded.  Memory allocations that
115 //          are to survive library unload and reload should be
116 //          performed using AFSExAllocatePoolWithTag() which is
117 //          provided by the AFS Framework.
118 //
119 // Parameters:
120 //                POOL_TYPE PoolType - Paged or NonPaged
121 //                SIZE_T NumberOfBytes - requested allocation size
122 //                ULONG  Tag - Pool Allocation Tag to be applied for tracking
123 //
124 // Return:
125 //                void * - the memory allocation
126 //
127
128 void *
129 AFSLibExAllocatePoolWithTag( IN POOL_TYPE  PoolType,
130                              IN SIZE_T  NumberOfBytes,
131                              IN ULONG  Tag)
132 {
133
134     void *pBuffer = NULL;
135
136     pBuffer = ExAllocatePoolWithTag( PoolType,
137                                      NumberOfBytes,
138                                      Tag);
139
140     if( pBuffer == NULL)
141     {
142
143         if( BooleanFlagOn( AFSDebugFlags, AFS_DBG_BUGCHECK_EXCEPTION))
144         {
145
146             KeBugCheck( (ULONG)-2);
147         }
148         else
149         {
150
151             AFSDbgLogMsg( 0,
152                           0,
153                           "AFSLibExAllocatePoolWithTag failure Type %08lX Size %08lX Tag %08lX %08lX\n",
154                           PoolType,
155                           NumberOfBytes,
156                           Tag,
157                           PsGetCurrentThread());
158
159             AFSBreakPoint();
160         }
161     }
162
163     return pBuffer;
164 }
165
166 //
167 // Function: AFSAcquireExcl()
168 //
169 // Purpose: Called to acquire a resource exclusive with optional wait
170 //
171 // Parameters:
172 //                PERESOURCE Resource - Resource to acquire
173 //                BOOLEAN Wait - Whether to block
174 //
175 // Return:
176 //                BOOLEAN - Whether the mask was acquired
177 //
178
179 BOOLEAN
180 AFSAcquireExcl( IN PERESOURCE Resource,
181                 IN BOOLEAN wait)
182 {
183
184     BOOLEAN bStatus = FALSE;
185
186     //
187     // Normal kernel APCs must be disabled before calling
188     // ExAcquireResourceExclusiveLite. Otherwise a bugcheck occurs.
189     //
190
191     KeEnterCriticalRegion();
192
193     bStatus = ExAcquireResourceExclusiveLite( Resource,
194                                               wait);
195
196     if( !bStatus)
197     {
198
199         KeLeaveCriticalRegion();
200     }
201
202     return bStatus;
203 }
204
205 BOOLEAN
206 AFSAcquireSharedStarveExclusive( IN PERESOURCE Resource,
207                                  IN BOOLEAN Wait)
208 {
209
210     BOOLEAN bStatus = FALSE;
211
212     KeEnterCriticalRegion();
213
214     bStatus = ExAcquireSharedStarveExclusive( Resource,
215                                               Wait);
216
217     if( !bStatus)
218     {
219
220         KeLeaveCriticalRegion();
221     }
222
223     return bStatus;
224 }
225
226 //
227 // Function: AFSAcquireShared()
228 //
229 // Purpose: Called to acquire a resource shared with optional wait
230 //
231 // Parameters:
232 //                PERESOURCE Resource - Resource to acquire
233 //                BOOLEAN Wait - Whether to block
234 //
235 // Return:
236 //                BOOLEAN - Whether the mask was acquired
237 //
238
239 BOOLEAN
240 AFSAcquireShared( IN PERESOURCE Resource,
241                   IN BOOLEAN wait)
242 {
243
244     BOOLEAN bStatus = FALSE;
245
246     KeEnterCriticalRegion();
247
248     bStatus = ExAcquireResourceSharedLite( Resource,
249                                            wait);
250
251     if( !bStatus)
252     {
253
254         KeLeaveCriticalRegion();
255     }
256
257     return bStatus;
258 }
259
260 //
261 // Function: AFSReleaseResource()
262 //
263 // Purpose: Called to release a resource
264 //
265 // Parameters:
266 //                PERESOURCE Resource - Resource to release
267 //
268 // Return:
269 //                None
270 //
271
272 void
273 AFSReleaseResource( IN PERESOURCE Resource)
274 {
275
276     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
277                   AFS_TRACE_LEVEL_VERBOSE,
278                   "AFSReleaseResource Releasing lock %p Thread %08lX\n",
279                   Resource,
280                   PsGetCurrentThread());
281
282     ExReleaseResourceLite( Resource);
283
284     KeLeaveCriticalRegion();
285
286     return;
287 }
288
289 void
290 AFSConvertToShared( IN PERESOURCE Resource)
291 {
292
293     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
294                   AFS_TRACE_LEVEL_VERBOSE,
295                   "AFSConvertToShared Converting lock %p Thread %08lX\n",
296                   Resource,
297                   PsGetCurrentThread());
298
299     ExConvertExclusiveToSharedLite( Resource);
300
301     return;
302 }
303
304 //
305 // Function: AFSCompleteRequest
306 //
307 // Description:
308 //
309 //      This function completes irps
310 //
311 // Return:
312 //
313 //      A status is returned for the function
314 //
315
316 void
317 AFSCompleteRequest( IN PIRP Irp,
318                     IN ULONG Status)
319 {
320
321     Irp->IoStatus.Status = Status;
322
323     IoCompleteRequest( Irp,
324                        IO_NO_INCREMENT);
325
326     return;
327 }
328
329 //
330 // Function: AFSGenerateCRC
331 //
332 // Description:
333 //
334 //      Given a device and filename this function generates a CRC
335 //
336 // Return:
337 //
338 //      A status is returned for the function
339 //
340
341 ULONG
342 AFSGenerateCRC( IN PUNICODE_STRING FileName,
343                 IN BOOLEAN UpperCaseName)
344 {
345
346     ULONG ulCRC = 0;
347     NTSTATUS ntStatus = STATUS_SUCCESS;
348
349     ntStatus = RtlHashUnicodeString( FileName,
350                                      UpperCaseName,
351                                      HASH_STRING_ALGORITHM_DEFAULT,
352                                      &ulCRC);
353
354     if( !NT_SUCCESS( ntStatus))
355     {
356         ulCRC = 0;
357     }
358
359     return ulCRC;
360 }
361
362 void *
363 AFSLockSystemBuffer( IN PIRP Irp,
364                      IN ULONG Length)
365 {
366
367     void *pAddress = NULL;
368
369     if( Irp->MdlAddress != NULL)
370     {
371
372         pAddress = MmGetSystemAddressForMdlSafe( Irp->MdlAddress,
373                                                  NormalPagePriority);
374     }
375     else if( Irp->AssociatedIrp.SystemBuffer != NULL)
376     {
377
378         pAddress = Irp->AssociatedIrp.SystemBuffer;
379     }
380     else if( Irp->UserBuffer != NULL)
381     {
382
383         Irp->MdlAddress = IoAllocateMdl( Irp->UserBuffer,
384                                          Length,
385                                          FALSE,
386                                          FALSE,
387                                          Irp);
388
389         if( Irp->MdlAddress != NULL)
390         {
391
392             //
393             //  Lock the new Mdl in memory.
394             //
395
396             __try
397             {
398                 PIO_STACK_LOCATION pIoStack;
399                 pIoStack = IoGetCurrentIrpStackLocation( Irp);
400
401
402                 MmProbeAndLockPages( Irp->MdlAddress, KernelMode,
403                                      (pIoStack->MajorFunction == IRP_MJ_READ) ? IoWriteAccess : IoReadAccess);
404
405                 pAddress = MmGetSystemAddressForMdlSafe( Irp->MdlAddress, NormalPagePriority );
406
407             }
408             __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
409             {
410
411                 AFSDumpTraceFilesFnc();
412
413                 IoFreeMdl( Irp->MdlAddress );
414                 Irp->MdlAddress = NULL;
415                 pAddress = NULL;
416             }
417         }
418     }
419
420     return pAddress;
421 }
422
423 void *
424 AFSLockUserBuffer( IN void *UserBuffer,
425                    IN ULONG BufferLength,
426                    OUT MDL ** Mdl)
427 {
428
429     NTSTATUS ntStatus = STATUS_SUCCESS;
430     void *pAddress = NULL;
431     MDL *pMdl = NULL;
432
433     __Enter
434     {
435
436         pMdl = IoAllocateMdl( UserBuffer,
437                               BufferLength,
438                               FALSE,
439                               FALSE,
440                               NULL);
441
442             if( pMdl == NULL)
443             {
444
445                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
446             }
447
448         //
449         //  Lock the new Mdl in memory.
450         //
451
452         __try
453         {
454
455             MmProbeAndLockPages( pMdl,
456                                  KernelMode,
457                                  IoWriteAccess);
458
459             pAddress = MmGetSystemAddressForMdlSafe( pMdl,
460                                                      NormalPagePriority);
461         }
462         __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
463         {
464
465             AFSDumpTraceFilesFnc();
466
467             IoFreeMdl( pMdl);
468             pMdl = NULL;
469             pAddress = NULL;
470         }
471
472         if( pMdl != NULL)
473         {
474
475             *Mdl = pMdl;
476         }
477
478 try_exit:
479
480         NOTHING;
481     }
482
483     return pAddress;
484 }
485
486 void *
487 AFSMapToService( IN PIRP Irp,
488                  IN ULONG ByteCount)
489 {
490
491     NTSTATUS ntStatus = STATUS_SUCCESS;
492     void *pMappedBuffer = NULL;
493     AFSDeviceExt *pDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
494     KAPC stApcState;
495
496     __Enter
497     {
498
499         if( pDevExt->Specific.Control.ServiceProcess == NULL)
500         {
501
502             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
503         }
504
505         if( Irp->MdlAddress == NULL)
506         {
507
508             if( AFSLockSystemBuffer( Irp,
509                                      ByteCount) == NULL)
510             {
511
512                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
513             }
514         }
515
516         //
517         // Attach to the service process for mapping
518         //
519
520         KeStackAttachProcess( pDevExt->Specific.Control.ServiceProcess,
521                               (PRKAPC_STATE)&stApcState);
522
523         pMappedBuffer = MmMapLockedPagesSpecifyCache( Irp->MdlAddress,
524                                                       UserMode,
525                                                       MmCached,
526                                                       NULL,
527                                                       FALSE,
528                                                       NormalPagePriority);
529
530         KeUnstackDetachProcess( (PRKAPC_STATE)&stApcState);
531
532 try_exit:
533
534         NOTHING;
535     }
536
537     return pMappedBuffer;
538 }
539
540 NTSTATUS
541 AFSUnmapServiceMappedBuffer( IN void *MappedBuffer,
542                              IN PMDL Mdl)
543 {
544
545     NTSTATUS ntStatus = STATUS_SUCCESS;
546     AFSDeviceExt *pDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
547     KAPC stApcState;
548
549     __Enter
550     {
551
552         if( pDevExt->Specific.Control.ServiceProcess == NULL)
553         {
554
555             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
556         }
557
558         if( Mdl != NULL)
559         {
560
561             //
562             // Attach to the service process for mapping
563             //
564
565             KeStackAttachProcess( pDevExt->Specific.Control.ServiceProcess,
566                                   (PRKAPC_STATE)&stApcState);
567
568             MmUnmapLockedPages( MappedBuffer,
569                                 Mdl);
570
571             KeUnstackDetachProcess( (PRKAPC_STATE)&stApcState);
572         }
573
574 try_exit:
575
576         NOTHING;
577     }
578
579     return ntStatus;
580 }
581
582 NTSTATUS
583 AFSInitializeLibraryDevice()
584 {
585
586     NTSTATUS ntStatus = STATUS_SUCCESS;
587     AFSDeviceExt *pDeviceExt = NULL;
588
589     __Enter
590     {
591
592         pDeviceExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
593
594         //
595         // The PIOCtl file name
596         //
597
598         RtlInitUnicodeString( &AFSPIOCtlName,
599                               AFS_PIOCTL_FILE_INTERFACE_NAME);
600
601         //
602         // And the global root share name
603         //
604
605         RtlInitUnicodeString( &AFSGlobalRootName,
606                               AFS_GLOBAL_ROOT_SHARE_NAME);
607
608     }
609
610     return ntStatus;
611 }
612
613 NTSTATUS
614 AFSRemoveLibraryDevice()
615 {
616
617     NTSTATUS ntStatus = STATUS_SUCCESS;
618
619     __Enter
620     {
621
622     }
623
624     return ntStatus;
625 }
626
627 NTSTATUS
628 AFSDefaultDispatch( IN PDEVICE_OBJECT DeviceObject,
629                     IN PIRP Irp)
630 {
631
632     UNREFERENCED_PARAMETER(DeviceObject);
633     NTSTATUS            ntStatus = STATUS_INVALID_DEVICE_REQUEST;
634
635     AFSCompleteRequest( Irp,
636                         ntStatus);
637
638     return ntStatus;
639 }
640
641 NTSTATUS
642 AFSInitializeGlobalDirectoryEntries()
643 {
644
645     NTSTATUS ntStatus = STATUS_SUCCESS;
646     AFSDirectoryCB *pDirNode = NULL;
647     ULONG ulEntryLength = 0;
648     AFSObjectInfoCB *pObjectInfoCB = NULL;
649     AFSNonPagedDirectoryCB *pNonPagedDirEntry = NULL;
650     LONG lCount;
651
652     __Enter
653     {
654
655         //
656         // Initialize the global . entry
657         //
658
659         pObjectInfoCB = AFSAllocateObjectInfo( &AFSGlobalRoot->ObjectInformation,
660                                                0);
661
662         if( pObjectInfoCB == NULL)
663         {
664
665             AFSDbgLogMsg( AFS_SUBSYSTEM_LOAD_LIBRARY | AFS_SUBSYSTEM_INIT_PROCESSING,
666                           AFS_TRACE_LEVEL_ERROR,
667                           "AFSInitializeGlobalDirectory AFSAllocateObjectInfo failure %08lX\n",
668                           ntStatus);
669
670             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
671         }
672
673         lCount = AFSObjectInfoIncrement( pObjectInfoCB,
674                                          AFS_OBJECT_REFERENCE_GLOBAL);
675
676         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
677                       AFS_TRACE_LEVEL_VERBOSE,
678                       "AFSInitializeGlobalDirectoryEntries Increment count on object %p Cnt %d\n",
679                       pObjectInfoCB,
680                       lCount);
681
682         ntStatus = STATUS_SUCCESS;
683
684         ulEntryLength = sizeof( AFSDirectoryCB) +
685                                      sizeof( WCHAR);
686
687         pDirNode = (AFSDirectoryCB *)AFSLibExAllocatePoolWithTag( PagedPool,
688                                                                   ulEntryLength,
689                                                                   AFS_DIR_ENTRY_TAG);
690
691         if( pDirNode == NULL)
692         {
693
694             AFSDeleteObjectInfo( pObjectInfoCB);
695
696             AFSDbgLogMsg( AFS_SUBSYSTEM_LOAD_LIBRARY | AFS_SUBSYSTEM_INIT_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_ALLOCATION,
697                           AFS_TRACE_LEVEL_ERROR,
698                           "AFSInitializeGlobalDirectory AFS_DIR_ENTRY_TAG allocation failure\n");
699
700             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
701         }
702
703         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_ALLOCATION,
704                       AFS_TRACE_LEVEL_VERBOSE,
705                       "AFSInitializeGlobalDirectory AFS_DIR_ENTRY_TAG allocated %p\n",
706                       pDirNode);
707
708         pNonPagedDirEntry = (AFSNonPagedDirectoryCB *)AFSLibExAllocatePoolWithTag( NonPagedPool,
709                                                                                    sizeof( AFSNonPagedDirectoryCB),
710                                                                                    AFS_DIR_ENTRY_NP_TAG);
711
712         if( pNonPagedDirEntry == NULL)
713         {
714
715             ExFreePool( pDirNode);
716
717             AFSDeleteObjectInfo( pObjectInfoCB);
718
719             AFSDbgLogMsg( AFS_SUBSYSTEM_LOAD_LIBRARY | AFS_SUBSYSTEM_INIT_PROCESSING,
720                           AFS_TRACE_LEVEL_ERROR,
721                           "AFSInitializeGlobalDirectory AFS_DIR_ENTRY_NP_TAG allocation failure\n");
722
723             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
724         }
725
726         RtlZeroMemory( pDirNode,
727                        ulEntryLength);
728
729         RtlZeroMemory( pNonPagedDirEntry,
730                        sizeof( AFSNonPagedDirectoryCB));
731
732         ExInitializeResourceLite( &pNonPagedDirEntry->Lock);
733
734         pDirNode->NonPaged = pNonPagedDirEntry;
735
736         pDirNode->ObjectInformation = pObjectInfoCB;
737
738         //
739         // Set valid entry
740         //
741
742         SetFlag( pDirNode->Flags, AFS_DIR_ENTRY_NOT_IN_PARENT_TREE | AFS_DIR_ENTRY_FAKE | AFS_DIR_ENTRY_VALID);
743
744         pDirNode->FileIndex = (ULONG)AFS_DIR_ENTRY_DOT_INDEX;
745
746         //
747         // Setup the names in the entry
748         //
749
750         pDirNode->NameInformation.FileName.Length = sizeof( WCHAR);
751
752         pDirNode->NameInformation.FileName.MaximumLength = sizeof( WCHAR);
753
754         pDirNode->NameInformation.FileName.Buffer = (WCHAR *)((char *)pDirNode + sizeof( AFSDirectoryCB));
755
756         pDirNode->NameInformation.FileName.Buffer[ 0] = L'.';
757
758         //
759         // Populate the rest of the data
760         //
761
762         pObjectInfoCB->FileType = AFS_FILE_TYPE_DIRECTORY;
763
764         pObjectInfoCB->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
765
766         AFSGlobalDotDirEntry = pDirNode;
767
768         //
769         // Now the .. entry
770         //
771
772         pObjectInfoCB = AFSAllocateObjectInfo( &AFSGlobalRoot->ObjectInformation,
773                                                0);
774
775         if( pObjectInfoCB == NULL)
776         {
777
778             AFSDbgLogMsg( AFS_SUBSYSTEM_LOAD_LIBRARY | AFS_SUBSYSTEM_INIT_PROCESSING,
779                           AFS_TRACE_LEVEL_ERROR,
780                           "AFSInitializeGlobalDirectory AFSAllocateObjectInfo (2) failure %08lX\n",
781                           ntStatus);
782
783             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
784         }
785
786         lCount = AFSObjectInfoIncrement( pObjectInfoCB,
787                                          AFS_OBJECT_REFERENCE_GLOBAL);
788
789         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
790                       AFS_TRACE_LEVEL_VERBOSE,
791                       "AFSInitializeGlobalDirectoryEntries Increment count on object %p Cnt %d\n",
792                       pObjectInfoCB,
793                       lCount);
794
795         ntStatus = STATUS_SUCCESS;
796
797         ulEntryLength = sizeof( AFSDirectoryCB) +
798                                      ( 2 * sizeof( WCHAR));
799
800         pDirNode = (AFSDirectoryCB *)AFSLibExAllocatePoolWithTag( PagedPool,
801                                                                   ulEntryLength,
802                                                                   AFS_DIR_ENTRY_TAG);
803
804         if( pDirNode == NULL)
805         {
806
807             AFSDbgLogMsg( AFS_SUBSYSTEM_LOAD_LIBRARY | AFS_SUBSYSTEM_INIT_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_ALLOCATION,
808                           AFS_TRACE_LEVEL_ERROR,
809                           "AFSInitializeGlobalDirectoryEntries AFS_DIR_ENTRY_TAG allocation failure\n");
810
811             AFSDeleteObjectInfo( pObjectInfoCB);
812
813             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
814         }
815
816         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_ALLOCATION,
817                       AFS_TRACE_LEVEL_VERBOSE,
818                       "AFSInitializeGlobalDirectoryEntries AFS_DIR_ENTRY_TAG allocated %p\n",
819                       pDirNode);
820
821         pNonPagedDirEntry = (AFSNonPagedDirectoryCB *)AFSLibExAllocatePoolWithTag( NonPagedPool,
822                                                                                    sizeof( AFSNonPagedDirectoryCB),
823                                                                                    AFS_DIR_ENTRY_NP_TAG);
824
825         if( pNonPagedDirEntry == NULL)
826         {
827
828             ExFreePool( pDirNode);
829
830             AFSDeleteObjectInfo( pObjectInfoCB);
831
832             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
833         }
834
835         RtlZeroMemory( pDirNode,
836                        ulEntryLength);
837
838         RtlZeroMemory( pNonPagedDirEntry,
839                        sizeof( AFSNonPagedDirectoryCB));
840
841         ExInitializeResourceLite( &pNonPagedDirEntry->Lock);
842
843         pDirNode->NonPaged = pNonPagedDirEntry;
844
845         pDirNode->ObjectInformation = pObjectInfoCB;
846
847         //
848         // Set valid entry
849         //
850
851         SetFlag( pDirNode->Flags, AFS_DIR_ENTRY_NOT_IN_PARENT_TREE | AFS_DIR_ENTRY_FAKE | AFS_DIR_ENTRY_VALID);
852
853         pDirNode->FileIndex = (ULONG)AFS_DIR_ENTRY_DOT_DOT_INDEX;
854
855         //
856         // Setup the names in the entry
857         //
858
859         pDirNode->NameInformation.FileName.Length = 2 * sizeof( WCHAR);
860
861         pDirNode->NameInformation.FileName.MaximumLength = 2 * sizeof( WCHAR);
862
863         pDirNode->NameInformation.FileName.Buffer = (WCHAR *)((char *)pDirNode + sizeof( AFSDirectoryCB));
864
865         pDirNode->NameInformation.FileName.Buffer[ 0] = L'.';
866
867         pDirNode->NameInformation.FileName.Buffer[ 1] = L'.';
868
869         //
870         // Populate the rest of the data
871         //
872
873         pObjectInfoCB->FileType = AFS_FILE_TYPE_DIRECTORY;
874
875         pObjectInfoCB->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
876
877         AFSGlobalDotDotDirEntry = pDirNode;
878
879 try_exit:
880
881         if( !NT_SUCCESS( ntStatus))
882         {
883
884             if( AFSGlobalDotDirEntry != NULL)
885             {
886
887                 AFSDeleteObjectInfo( AFSGlobalDotDirEntry->ObjectInformation);
888
889                 ExDeleteResourceLite( &AFSGlobalDotDirEntry->NonPaged->Lock);
890
891                 ExFreePool( AFSGlobalDotDirEntry->NonPaged);
892
893                 ExFreePool( AFSGlobalDotDirEntry);
894
895                 AFSGlobalDotDirEntry = NULL;
896             }
897
898             if( AFSGlobalDotDotDirEntry != NULL)
899             {
900
901                 AFSDeleteObjectInfo( AFSGlobalDotDotDirEntry->ObjectInformation);
902
903                 ExDeleteResourceLite( &AFSGlobalDotDotDirEntry->NonPaged->Lock);
904
905                 ExFreePool( AFSGlobalDotDotDirEntry->NonPaged);
906
907                 ExFreePool( AFSGlobalDotDotDirEntry);
908
909                 AFSGlobalDotDotDirEntry = NULL;
910             }
911         }
912     }
913
914     return ntStatus;
915 }
916
917 AFSDirectoryCB *
918 AFSInitDirEntry( IN AFSObjectInfoCB *ParentObjectInfo,
919                  IN PUNICODE_STRING FileName,
920                  IN PUNICODE_STRING TargetName,
921                  IN AFSDirEnumEntry *DirEnumEntry,
922                  IN ULONG FileIndex)
923 {
924
925     AFSDirectoryCB *pDirNode = NULL;
926     NTSTATUS ntStatus = STATUS_SUCCESS;
927     ULONG ulEntryLength = 0;
928     AFSObjectInfoCB *pObjectInfoCB = NULL;
929     BOOLEAN bAllocatedObjectCB = FALSE;
930     ULONGLONG ullIndex = 0;
931     AFSNonPagedDirectoryCB *pNonPagedDirEntry = NULL;
932     LONG lCount;
933
934     __Enter
935     {
936
937         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
938                       AFS_TRACE_LEVEL_VERBOSE,
939                       "AFSInitDirEntry Initializing entry %wZ parent FID %08lX-%08lX-%08lX-%08lX\n",
940                       FileName,
941                       ParentObjectInfo->FileId.Cell,
942                       ParentObjectInfo->FileId.Volume,
943                       ParentObjectInfo->FileId.Vnode,
944                       ParentObjectInfo->FileId.Unique);
945
946         //
947         // First thing is to locate/create our object information block
948         // for this entry
949         //
950
951         AFSAcquireExcl( ParentObjectInfo->VolumeCB->ObjectInfoTree.TreeLock,
952                         TRUE);
953
954         ullIndex = AFSCreateLowIndex( &DirEnumEntry->FileId);
955
956         ntStatus = AFSLocateHashEntry( ParentObjectInfo->VolumeCB->ObjectInfoTree.TreeHead,
957                                        ullIndex,
958                                        (AFSBTreeEntry **)&pObjectInfoCB);
959
960         if( !NT_SUCCESS( ntStatus) ||
961             pObjectInfoCB == NULL)
962         {
963
964             //
965             // Allocate our object info cb
966             //
967
968             pObjectInfoCB = AFSAllocateObjectInfo( ParentObjectInfo,
969                                                    ullIndex);
970
971             if( pObjectInfoCB == NULL)
972             {
973
974                 AFSReleaseResource( ParentObjectInfo->VolumeCB->ObjectInfoTree.TreeLock);
975
976                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
977             }
978
979             bAllocatedObjectCB = TRUE;
980
981             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
982                           AFS_TRACE_LEVEL_VERBOSE,
983                           "AFSInitDirEntry initialized object %p Parent Object %p for %wZ\n",
984                           pObjectInfoCB,
985                           ParentObjectInfo,
986                           FileName);
987         }
988
989         lCount = AFSObjectInfoIncrement( pObjectInfoCB,
990                                          AFS_OBJECT_REFERENCE_DIRENTRY);
991
992         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
993                       AFS_TRACE_LEVEL_VERBOSE,
994                       "AFSInitDirEntry Increment count on object %p Cnt %d\n",
995                       pObjectInfoCB,
996                       lCount);
997
998         AFSReleaseResource( ParentObjectInfo->VolumeCB->ObjectInfoTree.TreeLock);
999
1000         ntStatus = STATUS_SUCCESS;
1001
1002         ulEntryLength = sizeof( AFSDirectoryCB) +
1003                                      FileName->Length;
1004
1005         if( TargetName != NULL)
1006         {
1007
1008             ulEntryLength += TargetName->Length;
1009         }
1010
1011         pDirNode = (AFSDirectoryCB *)AFSExAllocatePoolWithTag( PagedPool,
1012                                                                ulEntryLength,
1013                                                                AFS_DIR_ENTRY_TAG);
1014
1015         if( pDirNode == NULL)
1016         {
1017
1018             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1019         }
1020
1021         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_ALLOCATION,
1022                       AFS_TRACE_LEVEL_VERBOSE,
1023                       "AFSInitDirEntry AFS_DIR_ENTRY_TAG allocated %p\n",
1024                       pDirNode);
1025
1026         pNonPagedDirEntry = (AFSNonPagedDirectoryCB *)AFSExAllocatePoolWithTag( NonPagedPool,
1027                                                                                 sizeof( AFSNonPagedDirectoryCB),
1028                                                                                 AFS_DIR_ENTRY_NP_TAG);
1029
1030         if( pNonPagedDirEntry == NULL)
1031         {
1032
1033             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1034         }
1035
1036         RtlZeroMemory( pDirNode,
1037                        ulEntryLength);
1038
1039         RtlZeroMemory( pNonPagedDirEntry,
1040                        sizeof( AFSNonPagedDirectoryCB));
1041
1042         ExInitializeResourceLite( &pNonPagedDirEntry->Lock);
1043
1044         pDirNode->NonPaged = pNonPagedDirEntry;
1045
1046         pDirNode->ObjectInformation = pObjectInfoCB;
1047
1048         //
1049         // Set valid entry and NOT_IN_PARENT flag
1050         //
1051
1052         SetFlag( pDirNode->Flags, AFS_DIR_ENTRY_VALID | AFS_DIR_ENTRY_NOT_IN_PARENT_TREE);
1053
1054         pDirNode->FileIndex = FileIndex;
1055
1056         //
1057         // Setup the names in the entry
1058         //
1059
1060         if( FileName->Length > 0)
1061         {
1062
1063             pDirNode->NameInformation.FileName.Length = FileName->Length;
1064
1065             pDirNode->NameInformation.FileName.MaximumLength = FileName->Length;
1066
1067             pDirNode->NameInformation.FileName.Buffer = (WCHAR *)((char *)pDirNode + sizeof( AFSDirectoryCB));
1068
1069             RtlCopyMemory( pDirNode->NameInformation.FileName.Buffer,
1070                            FileName->Buffer,
1071                            pDirNode->NameInformation.FileName.Length);
1072
1073             //
1074             // Create a CRC for the file
1075             //
1076
1077             pDirNode->CaseSensitiveTreeEntry.HashIndex = AFSGenerateCRC( &pDirNode->NameInformation.FileName,
1078                                                                          FALSE);
1079
1080             pDirNode->CaseInsensitiveTreeEntry.HashIndex = AFSGenerateCRC( &pDirNode->NameInformation.FileName,
1081                                                                            TRUE);
1082         }
1083
1084         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1085                       AFS_TRACE_LEVEL_VERBOSE,
1086                       "AFSInitDirEntry Initialized DE %p for %wZ in parent FID %08lX-%08lX-%08lX-%08lX\n",
1087                       pDirNode,
1088                       FileName,
1089                       ParentObjectInfo->FileId.Cell,
1090                       ParentObjectInfo->FileId.Volume,
1091                       ParentObjectInfo->FileId.Vnode,
1092                       ParentObjectInfo->FileId.Unique);
1093
1094         if( TargetName != NULL &&
1095             TargetName->Length > 0)
1096         {
1097
1098             pDirNode->NameInformation.TargetName.Length = TargetName->Length;
1099
1100             pDirNode->NameInformation.TargetName.MaximumLength = pDirNode->NameInformation.TargetName.Length;
1101
1102             pDirNode->NameInformation.TargetName.Buffer = (WCHAR *)((char *)pDirNode +
1103                                                                             sizeof( AFSDirectoryCB) +
1104                                                                             pDirNode->NameInformation.FileName.Length);
1105
1106             RtlCopyMemory( pDirNode->NameInformation.TargetName.Buffer,
1107                            TargetName->Buffer,
1108                            pDirNode->NameInformation.TargetName.Length);
1109         }
1110
1111         //
1112         // If we allocated the object information cb then update the information
1113         //
1114
1115         if( bAllocatedObjectCB)
1116         {
1117
1118             //
1119             // Populate the rest of the data
1120             //
1121
1122             pObjectInfoCB->FileId = DirEnumEntry->FileId;
1123
1124             pObjectInfoCB->TargetFileId = DirEnumEntry->TargetFileId;
1125
1126             pObjectInfoCB->FileType = DirEnumEntry->FileType;
1127
1128             pObjectInfoCB->CreationTime = DirEnumEntry->CreationTime;
1129
1130             pObjectInfoCB->LastAccessTime = DirEnumEntry->LastAccessTime;
1131
1132             pObjectInfoCB->LastWriteTime = DirEnumEntry->LastWriteTime;
1133
1134             pObjectInfoCB->ChangeTime = DirEnumEntry->ChangeTime;
1135
1136             pObjectInfoCB->EndOfFile = DirEnumEntry->EndOfFile;
1137
1138             pObjectInfoCB->AllocationSize = DirEnumEntry->AllocationSize;
1139
1140             pObjectInfoCB->FileAttributes = DirEnumEntry->FileAttributes;
1141
1142             if( pObjectInfoCB->FileType == AFS_FILE_TYPE_MOUNTPOINT)
1143             {
1144
1145                 pObjectInfoCB->FileAttributes = (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT);
1146             }
1147
1148             if (pObjectInfoCB->FileType == AFS_FILE_TYPE_SYMLINK ||
1149                 pObjectInfoCB->FileType == AFS_FILE_TYPE_DFSLINK)
1150             {
1151
1152                 pObjectInfoCB->FileAttributes = FILE_ATTRIBUTE_REPARSE_POINT;
1153             }
1154
1155             pObjectInfoCB->EaSize = DirEnumEntry->EaSize;
1156
1157             //
1158             // Check for the case where we have a filetype of SymLink but both the TargetFid and the
1159             // TargetName are empty. In this case set the filetype to zero so we evaluate it later in
1160             // the code
1161             //
1162
1163             if( pObjectInfoCB->FileType == AFS_FILE_TYPE_SYMLINK &&
1164                 pObjectInfoCB->TargetFileId.Vnode == 0 &&
1165                 pObjectInfoCB->TargetFileId.Unique == 0 &&
1166                 pDirNode->NameInformation.TargetName.Length == 0)
1167             {
1168
1169                 //
1170                 // This will ensure we perform a validation on the node
1171                 //
1172
1173                 pObjectInfoCB->FileType = AFS_FILE_TYPE_UNKNOWN;
1174             }
1175
1176             if( pObjectInfoCB->FileType == AFS_FILE_TYPE_UNKNOWN)
1177             {
1178
1179                 SetFlag( pObjectInfoCB->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED);
1180             }
1181         }
1182
1183         //
1184         // Object specific information
1185         //
1186
1187         pObjectInfoCB->Links = DirEnumEntry->Links;
1188
1189         pObjectInfoCB->Expiration = DirEnumEntry->Expiration;
1190
1191         pObjectInfoCB->DataVersion = DirEnumEntry->DataVersion;
1192
1193 try_exit:
1194
1195         if( !NT_SUCCESS( ntStatus))
1196         {
1197
1198             if( pNonPagedDirEntry != NULL)
1199             {
1200
1201                 ExDeleteResourceLite( &pNonPagedDirEntry->Lock);
1202
1203                 AFSExFreePoolWithTag( pNonPagedDirEntry, AFS_DIR_ENTRY_NP_TAG);
1204             }
1205
1206             if( pDirNode != NULL)
1207             {
1208
1209                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_ALLOCATION,
1210                               AFS_TRACE_LEVEL_VERBOSE,
1211                               "AFSInitDirEntry AFS_DIR_ENTRY_TAG deallocating %p\n",
1212                               pDirNode);
1213
1214                 AFSExFreePoolWithTag( pDirNode, AFS_DIR_ENTRY_TAG);
1215
1216                 pDirNode = NULL;
1217             }
1218
1219             //
1220             // Dereference our object info block if we have one
1221             //
1222
1223             if( pObjectInfoCB != NULL)
1224             {
1225
1226                 lCount = AFSObjectInfoDecrement( pObjectInfoCB,
1227                                                  AFS_OBJECT_REFERENCE_DIRENTRY);
1228
1229                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1230                               AFS_TRACE_LEVEL_VERBOSE,
1231                               "AFSInitDirEntry Decrement count on object %p Cnt %d\n",
1232                               pObjectInfoCB,
1233                               lCount);
1234
1235                 if( bAllocatedObjectCB)
1236                 {
1237
1238                     ASSERT( pObjectInfoCB->ObjectReferenceCount == 0);
1239
1240                     AFSDeleteObjectInfo( pObjectInfoCB);
1241                 }
1242             }
1243         }
1244     }
1245
1246     return pDirNode;
1247 }
1248
1249 BOOLEAN
1250 AFSCheckForReadOnlyAccess( IN ACCESS_MASK DesiredAccess,
1251                            IN BOOLEAN DirectoryEntry)
1252 {
1253
1254     BOOLEAN bReturn = TRUE;
1255     ACCESS_MASK stAccessMask = 0;
1256
1257     //
1258     // Get rid of anything we don't know about
1259     //
1260
1261     DesiredAccess = (DesiredAccess   &
1262                           ( DELETE |
1263                             READ_CONTROL |
1264                             WRITE_OWNER |
1265                             WRITE_DAC |
1266                             SYNCHRONIZE |
1267                             ACCESS_SYSTEM_SECURITY |
1268                             FILE_WRITE_DATA |
1269                             FILE_READ_EA |
1270                             FILE_WRITE_EA |
1271                             FILE_READ_ATTRIBUTES |
1272                             FILE_WRITE_ATTRIBUTES |
1273                             FILE_LIST_DIRECTORY |
1274                             FILE_TRAVERSE |
1275                             FILE_DELETE_CHILD |
1276                             FILE_APPEND_DATA));
1277
1278     //
1279     // Our 'read only' access mask. These are the accesses we will
1280     // allow for a read only file
1281     //
1282
1283     stAccessMask = DELETE |
1284                         READ_CONTROL |
1285                         WRITE_OWNER |
1286                         WRITE_DAC |
1287                         SYNCHRONIZE |
1288                         ACCESS_SYSTEM_SECURITY |
1289                         FILE_READ_DATA |
1290                         FILE_READ_EA |
1291                         FILE_WRITE_EA |
1292                         FILE_READ_ATTRIBUTES |
1293                         FILE_WRITE_ATTRIBUTES |
1294                         FILE_EXECUTE |
1295                         FILE_LIST_DIRECTORY |
1296                         FILE_TRAVERSE;
1297
1298     //
1299     // For a directory, add in the directory specific accesses
1300     //
1301
1302     if( DirectoryEntry)
1303     {
1304
1305         stAccessMask |= FILE_ADD_SUBDIRECTORY |
1306                                 FILE_ADD_FILE |
1307                                 FILE_DELETE_CHILD;
1308     }
1309
1310     if( FlagOn( DesiredAccess, ~stAccessMask))
1311     {
1312
1313         //
1314         // A write access is set ...
1315         //
1316
1317         bReturn = FALSE;
1318     }
1319
1320     return bReturn;
1321 }
1322
1323 NTSTATUS
1324 AFSEvaluateNode( IN GUID *AuthGroup,
1325                  IN AFSDirectoryCB *DirEntry)
1326 {
1327
1328     NTSTATUS ntStatus = STATUS_SUCCESS;
1329     AFSDirEnumEntry *pDirEntry = NULL;
1330     UNICODE_STRING uniTargetName;
1331
1332     __Enter
1333     {
1334
1335         ntStatus = AFSEvaluateTargetByID( DirEntry->ObjectInformation,
1336                                           AuthGroup,
1337                                           FALSE,
1338                                           &pDirEntry);
1339
1340         if( !NT_SUCCESS( ntStatus))
1341         {
1342
1343             try_return( ntStatus);
1344         }
1345
1346         DirEntry->ObjectInformation->TargetFileId = pDirEntry->TargetFileId;
1347
1348         DirEntry->ObjectInformation->Expiration = pDirEntry->Expiration;
1349
1350         DirEntry->ObjectInformation->DataVersion = pDirEntry->DataVersion;
1351
1352         DirEntry->ObjectInformation->FileType = pDirEntry->FileType;
1353
1354         DirEntry->ObjectInformation->CreationTime = pDirEntry->CreationTime;
1355
1356         DirEntry->ObjectInformation->LastAccessTime = pDirEntry->LastAccessTime;
1357
1358         DirEntry->ObjectInformation->LastWriteTime = pDirEntry->LastWriteTime;
1359
1360         DirEntry->ObjectInformation->ChangeTime = pDirEntry->ChangeTime;
1361
1362         DirEntry->ObjectInformation->EndOfFile = pDirEntry->EndOfFile;
1363
1364         DirEntry->ObjectInformation->AllocationSize = pDirEntry->AllocationSize;
1365
1366         DirEntry->ObjectInformation->FileAttributes = pDirEntry->FileAttributes;
1367
1368         if( pDirEntry->FileType == AFS_FILE_TYPE_MOUNTPOINT)
1369         {
1370
1371             DirEntry->ObjectInformation->FileAttributes = (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT);
1372         }
1373
1374         if( pDirEntry->FileType == AFS_FILE_TYPE_SYMLINK ||
1375             pDirEntry->FileType == AFS_FILE_TYPE_DFSLINK)
1376         {
1377
1378             DirEntry->ObjectInformation->FileAttributes = FILE_ATTRIBUTE_REPARSE_POINT;
1379         }
1380
1381         DirEntry->ObjectInformation->EaSize = pDirEntry->EaSize;
1382
1383         DirEntry->ObjectInformation->Links = pDirEntry->Links;
1384
1385         //
1386         // If we have a target name then see if it needs updating ...
1387         //
1388
1389         if( pDirEntry->TargetNameLength > 0)
1390         {
1391
1392             //
1393             // Update the target name information if needed
1394             //
1395
1396             uniTargetName.Length = (USHORT)pDirEntry->TargetNameLength;
1397
1398             uniTargetName.MaximumLength = uniTargetName.Length;
1399
1400             uniTargetName.Buffer = (WCHAR *)((char *)pDirEntry + pDirEntry->TargetNameOffset);
1401
1402             AFSAcquireExcl( &DirEntry->NonPaged->Lock,
1403                             TRUE);
1404
1405             if( DirEntry->NameInformation.TargetName.Length == 0 ||
1406                 RtlCompareUnicodeString( &uniTargetName,
1407                                          &DirEntry->NameInformation.TargetName,
1408                                          TRUE) != 0)
1409             {
1410
1411                 //
1412                 // Update the target name
1413                 //
1414
1415                 ntStatus = AFSUpdateTargetName( &DirEntry->NameInformation.TargetName,
1416                                                 &DirEntry->Flags,
1417                                                 uniTargetName.Buffer,
1418                                                 uniTargetName.Length);
1419
1420                 if( !NT_SUCCESS( ntStatus))
1421                 {
1422
1423                     AFSReleaseResource( &DirEntry->NonPaged->Lock);
1424
1425                     try_return( ntStatus);
1426                 }
1427             }
1428
1429             AFSReleaseResource( &DirEntry->NonPaged->Lock);
1430         }
1431
1432 try_exit:
1433
1434         if( pDirEntry != NULL)
1435         {
1436
1437             AFSExFreePoolWithTag( pDirEntry, AFS_GENERIC_MEMORY_2_TAG);
1438         }
1439     }
1440
1441     return ntStatus;
1442 }
1443
1444 NTSTATUS
1445 AFSValidateSymLink( IN GUID *AuthGroup,
1446                     IN AFSDirectoryCB *DirEntry)
1447 {
1448
1449     NTSTATUS ntStatus = STATUS_SUCCESS;
1450     AFSDirEnumEntry *pDirEntry = NULL;
1451     UNICODE_STRING uniTargetName;
1452
1453     __Enter
1454     {
1455
1456         ntStatus = AFSEvaluateTargetByID( DirEntry->ObjectInformation,
1457                                           AuthGroup,
1458                                           FALSE,
1459                                           &pDirEntry);
1460
1461         if( !NT_SUCCESS( ntStatus))
1462         {
1463
1464             try_return( ntStatus);
1465         }
1466
1467         if( pDirEntry->FileType == AFS_FILE_TYPE_UNKNOWN ||
1468             pDirEntry->FileType == AFS_FILE_TYPE_INVALID)
1469         {
1470
1471             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1472                           AFS_TRACE_LEVEL_VERBOSE_2,
1473                           "AFSValidateSymLink Invalid type Status %08lX\n",
1474                           STATUS_OBJECT_NAME_NOT_FOUND);
1475
1476             try_return( ntStatus = STATUS_OBJECT_NAME_NOT_FOUND);
1477         }
1478
1479         DirEntry->ObjectInformation->TargetFileId = pDirEntry->TargetFileId;
1480
1481         DirEntry->ObjectInformation->Expiration = pDirEntry->Expiration;
1482
1483         DirEntry->ObjectInformation->DataVersion = pDirEntry->DataVersion;
1484
1485         //
1486         // Update the target name information if needed
1487         //
1488
1489         uniTargetName.Length = (USHORT)pDirEntry->TargetNameLength;
1490
1491         uniTargetName.MaximumLength = uniTargetName.Length;
1492
1493         uniTargetName.Buffer = (WCHAR *)((char *)pDirEntry + pDirEntry->TargetNameOffset);
1494
1495         if( uniTargetName.Length > 0)
1496         {
1497
1498             AFSAcquireExcl( &DirEntry->NonPaged->Lock,
1499                             TRUE);
1500
1501             if( DirEntry->NameInformation.TargetName.Length == 0 ||
1502                 RtlCompareUnicodeString( &uniTargetName,
1503                                          &DirEntry->NameInformation.TargetName,
1504                                          TRUE) != 0)
1505             {
1506
1507                 //
1508                 // Update the target name
1509                 //
1510
1511                 ntStatus = AFSUpdateTargetName( &DirEntry->NameInformation.TargetName,
1512                                                 &DirEntry->Flags,
1513                                                 uniTargetName.Buffer,
1514                                                 uniTargetName.Length);
1515
1516                 if( !NT_SUCCESS( ntStatus))
1517                 {
1518
1519                     AFSReleaseResource( &DirEntry->NonPaged->Lock);
1520
1521                     try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1522                 }
1523             }
1524
1525             AFSReleaseResource( &DirEntry->NonPaged->Lock);
1526         }
1527
1528         //
1529         // If the FileType is the same then nothing to do since it IS
1530         // a SymLink
1531         //
1532
1533         if( pDirEntry->FileType == DirEntry->ObjectInformation->FileType)
1534         {
1535
1536             ASSERT( pDirEntry->FileType == AFS_FILE_TYPE_SYMLINK);
1537
1538             try_return( ntStatus = STATUS_SUCCESS);
1539         }
1540
1541         DirEntry->ObjectInformation->FileType = pDirEntry->FileType;
1542
1543         DirEntry->ObjectInformation->CreationTime = pDirEntry->CreationTime;
1544
1545         DirEntry->ObjectInformation->LastAccessTime = pDirEntry->LastAccessTime;
1546
1547         DirEntry->ObjectInformation->LastWriteTime = pDirEntry->LastWriteTime;
1548
1549         DirEntry->ObjectInformation->ChangeTime = pDirEntry->ChangeTime;
1550
1551         DirEntry->ObjectInformation->EndOfFile = pDirEntry->EndOfFile;
1552
1553         DirEntry->ObjectInformation->AllocationSize = pDirEntry->AllocationSize;
1554
1555         DirEntry->ObjectInformation->FileAttributes = pDirEntry->FileAttributes;
1556
1557         if( pDirEntry->FileType == AFS_FILE_TYPE_MOUNTPOINT)
1558         {
1559
1560             DirEntry->ObjectInformation->FileAttributes = (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT);
1561         }
1562
1563         if( pDirEntry->FileType == AFS_FILE_TYPE_SYMLINK ||
1564             pDirEntry->FileType == AFS_FILE_TYPE_DFSLINK)
1565         {
1566
1567             DirEntry->ObjectInformation->FileAttributes = FILE_ATTRIBUTE_REPARSE_POINT;
1568         }
1569
1570         DirEntry->ObjectInformation->EaSize = pDirEntry->EaSize;
1571
1572         DirEntry->ObjectInformation->Links = pDirEntry->Links;
1573
1574 try_exit:
1575
1576         if( pDirEntry != NULL)
1577         {
1578
1579             AFSExFreePoolWithTag( pDirEntry, AFS_GENERIC_MEMORY_2_TAG);
1580         }
1581     }
1582
1583     return ntStatus;
1584 }
1585
1586 NTSTATUS
1587 AFSInvalidateObject( IN OUT AFSObjectInfoCB **ppObjectInfo,
1588                      IN     ULONG Reason)
1589 {
1590
1591     NTSTATUS ntStatus = STATUS_SUCCESS;
1592     IO_STATUS_BLOCK stIoStatus;
1593     ULONG ulFilter = 0;
1594
1595     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1596                   AFS_TRACE_LEVEL_VERBOSE,
1597                   "AFSInvalidateObject Invalidation on node type %d for fid %08lX-%08lX-%08lX-%08lX Reason %d\n",
1598                   (*ppObjectInfo)->FileType,
1599                   (*ppObjectInfo)->FileId.Cell,
1600                   (*ppObjectInfo)->FileId.Volume,
1601                   (*ppObjectInfo)->FileId.Vnode,
1602                   (*ppObjectInfo)->FileId.Unique,
1603                   Reason);
1604
1605     if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_SYMLINK ||
1606         (*ppObjectInfo)->FileType == AFS_FILE_TYPE_DFSLINK ||
1607         (*ppObjectInfo)->FileType == AFS_FILE_TYPE_MOUNTPOINT)
1608     {
1609         //
1610         // We only act on the mount point itself, not the target. If the
1611         // node has been deleted then mark it as such otherwise indicate
1612         // it requires verification
1613         //
1614
1615         if( Reason == AFS_INVALIDATE_DELETED)
1616         {
1617             SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID);
1618         }
1619         else
1620         {
1621
1622             if( Reason == AFS_INVALIDATE_FLUSHED)
1623             {
1624
1625                 (*ppObjectInfo)->DataVersion.QuadPart = (ULONGLONG)-1;
1626
1627                 SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA);
1628             }
1629
1630             (*ppObjectInfo)->Expiration.QuadPart = 0;
1631
1632             (*ppObjectInfo)->TargetFileId.Vnode = 0;
1633
1634             (*ppObjectInfo)->TargetFileId.Unique = 0;
1635
1636             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1637                           AFS_TRACE_LEVEL_VERBOSE,
1638                           "AFSInvalidateObject Setting VERIFY flag on fid %08lX-%08lX-%08lX-%08lX\n",
1639                           (*ppObjectInfo)->FileId.Cell,
1640                           (*ppObjectInfo)->FileId.Volume,
1641                           (*ppObjectInfo)->FileId.Vnode,
1642                           (*ppObjectInfo)->FileId.Unique);
1643
1644             SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_VERIFY);
1645         }
1646
1647         ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
1648
1649         if( Reason == AFS_INVALIDATE_CREDS)
1650         {
1651             ulFilter |= FILE_NOTIFY_CHANGE_SECURITY;
1652         }
1653
1654         if( Reason == AFS_INVALIDATE_DATA_VERSION ||
1655             Reason == AFS_INVALIDATE_FLUSHED)
1656         {
1657             ulFilter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
1658         }
1659         else
1660         {
1661             ulFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
1662         }
1663
1664         AFSFsRtlNotifyFullReportChange( (*ppObjectInfo)->ParentObjectInformation,
1665                                         NULL,
1666                                         ulFilter,
1667                                         FILE_ACTION_MODIFIED);
1668
1669         try_return( ntStatus);
1670     }
1671
1672     //
1673     // Depending on the reason for invalidation then perform work on the node
1674     //
1675
1676     switch( Reason)
1677     {
1678
1679     case AFS_INVALIDATE_DELETED:
1680         {
1681
1682             //
1683             // Mark this node as invalid
1684             //
1685
1686             (*ppObjectInfo)->Links = 0;
1687
1688             SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_DELETED);
1689
1690             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1691                           AFS_TRACE_LEVEL_VERBOSE,
1692                           "AFSInvalidateObject Set DELETE flag on fid %08lX-%08lX-%08lX-%08lX\n",
1693                           (*ppObjectInfo)->FileId.Cell,
1694                           (*ppObjectInfo)->FileId.Volume,
1695                           (*ppObjectInfo)->FileId.Vnode,
1696                           (*ppObjectInfo)->FileId.Unique);
1697
1698             if( (*ppObjectInfo)->ParentObjectInformation != NULL)
1699             {
1700
1701                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1702                               AFS_TRACE_LEVEL_VERBOSE,
1703                               "AFSInvalidateObject Set VERIFY flag on parent fid %08lX-%08lX-%08lX-%08lX\n",
1704                               (*ppObjectInfo)->ParentObjectInformation->FileId.Cell,
1705                               (*ppObjectInfo)->ParentObjectInformation->FileId.Volume,
1706                               (*ppObjectInfo)->ParentObjectInformation->FileId.Vnode,
1707                               (*ppObjectInfo)->ParentObjectInformation->FileId.Unique);
1708
1709                 SetFlag( (*ppObjectInfo)->ParentObjectInformation->Flags, AFS_OBJECT_FLAGS_VERIFY);
1710
1711                 (*ppObjectInfo)->ParentObjectInformation->DataVersion.QuadPart = (ULONGLONG)-1;
1712
1713                 (*ppObjectInfo)->ParentObjectInformation->Expiration.QuadPart = 0;
1714             }
1715
1716             if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_DIRECTORY)
1717             {
1718                 ulFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
1719             }
1720             else
1721             {
1722                 ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
1723             }
1724
1725             AFSFsRtlNotifyFullReportChange( (*ppObjectInfo)->ParentObjectInformation,
1726                                             NULL,
1727                                             ulFilter,
1728                                             FILE_ACTION_REMOVED);
1729
1730             if( NT_SUCCESS( AFSQueueInvalidateObject( (*ppObjectInfo),
1731                                                       Reason)))
1732             {
1733                 (*ppObjectInfo) = NULL; // We'll dec the count in the worker item
1734             }
1735
1736             break;
1737         }
1738
1739     case AFS_INVALIDATE_FLUSHED:
1740         {
1741
1742             if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_FILE &&
1743                 (*ppObjectInfo)->Fcb != NULL)
1744             {
1745
1746                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1747                               AFS_TRACE_LEVEL_VERBOSE,
1748                               "AFSInvalidateObject Flush/purge file fid %08lX-%08lX-%08lX-%08lX\n",
1749                               (*ppObjectInfo)->FileId.Cell,
1750                               (*ppObjectInfo)->FileId.Volume,
1751                               (*ppObjectInfo)->FileId.Vnode,
1752                               (*ppObjectInfo)->FileId.Unique);
1753
1754                 AFSAcquireExcl( &(*ppObjectInfo)->Fcb->NPFcb->SectionObjectResource,
1755                                 TRUE);
1756
1757                 __try
1758                 {
1759
1760                     CcFlushCache( &(*ppObjectInfo)->Fcb->NPFcb->SectionObjectPointers,
1761                                   NULL,
1762                                   0,
1763                                   &stIoStatus);
1764
1765                     if( !NT_SUCCESS( stIoStatus.Status))
1766                     {
1767
1768                         AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1769                                       AFS_TRACE_LEVEL_ERROR,
1770                                       "AFSInvalidateObject CcFlushCache failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1771                                       (*ppObjectInfo)->FileId.Cell,
1772                                       (*ppObjectInfo)->FileId.Volume,
1773                                       (*ppObjectInfo)->FileId.Vnode,
1774                                       (*ppObjectInfo)->FileId.Unique,
1775                                       stIoStatus.Status,
1776                                       stIoStatus.Information);
1777
1778                         ntStatus = stIoStatus.Status;
1779                     }
1780
1781
1782                     if ( (*ppObjectInfo)->Fcb->NPFcb->SectionObjectPointers.DataSectionObject != NULL)
1783                     {
1784
1785                         if ( !CcPurgeCacheSection( &(*ppObjectInfo)->Fcb->NPFcb->SectionObjectPointers,
1786                                                    NULL,
1787                                                    0,
1788                                                    FALSE))
1789                         {
1790
1791                             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1792                                           AFS_TRACE_LEVEL_WARNING,
1793                                           "AFSInvalidateObject CcPurgeCacheSection failure FID %08lX-%08lX-%08lX-%08lX\n",
1794                                           (*ppObjectInfo)->FileId.Cell,
1795                                           (*ppObjectInfo)->FileId.Volume,
1796                                           (*ppObjectInfo)->FileId.Vnode,
1797                                           (*ppObjectInfo)->FileId.Unique);
1798
1799                             SetFlag( (*ppObjectInfo)->Fcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
1800                         }
1801                     }
1802                 }
1803                 __except( EXCEPTION_EXECUTE_HANDLER)
1804                 {
1805
1806                     ntStatus = GetExceptionCode();
1807
1808                     AFSDbgLogMsg( 0,
1809                                   0,
1810                                   "EXCEPTION - AFSInvalidateObject Cc FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
1811                                   (*ppObjectInfo)->FileId.Cell,
1812                                   (*ppObjectInfo)->FileId.Volume,
1813                                   (*ppObjectInfo)->FileId.Vnode,
1814                                   (*ppObjectInfo)->FileId.Unique,
1815                                   ntStatus);
1816
1817                     SetFlag( (*ppObjectInfo)->Fcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
1818                 }
1819
1820                 AFSReleaseResource( &(*ppObjectInfo)->Fcb->NPFcb->SectionObjectResource);
1821
1822                 //
1823                 // Clear out the extents
1824                 // Get rid of them (note this involves waiting
1825                 // for any writes or reads to the cache to complete)
1826                 //
1827
1828                 AFSTearDownFcbExtents( (*ppObjectInfo)->Fcb,
1829                                        NULL);
1830             }
1831
1832             (*ppObjectInfo)->DataVersion.QuadPart = (ULONGLONG)-1;
1833
1834
1835             if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_FILE)
1836             {
1837
1838                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1839                               AFS_TRACE_LEVEL_VERBOSE,
1840                               "AFSInvalidateObject Setting VERIFY_DATA flag on fid %08lX-%08lX-%08lX-%08lX\n",
1841                               (*ppObjectInfo)->FileId.Cell,
1842                               (*ppObjectInfo)->FileId.Volume,
1843                               (*ppObjectInfo)->FileId.Vnode,
1844                               (*ppObjectInfo)->FileId.Unique);
1845
1846                 SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA);
1847             }
1848
1849             // Fall through to the default processing
1850         }
1851
1852     default:
1853         {
1854
1855             if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_DIRECTORY)
1856             {
1857                 ulFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
1858             }
1859             else
1860             {
1861                 ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
1862             }
1863
1864             if( Reason == AFS_INVALIDATE_CREDS)
1865             {
1866                 ulFilter |= FILE_NOTIFY_CHANGE_SECURITY;
1867             }
1868
1869             if( Reason == AFS_INVALIDATE_DATA_VERSION)
1870             {
1871                 ulFilter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
1872             }
1873             else
1874             {
1875                 ulFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
1876             }
1877
1878             if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_DIRECTORY)
1879             {
1880
1881                 AFSFsRtlNotifyFullReportChange( (*ppObjectInfo),
1882                                                 NULL,
1883                                                 ulFilter,
1884                                                 FILE_ACTION_MODIFIED);
1885             }
1886             else
1887             {
1888
1889                 AFSFsRtlNotifyFullReportChange( (*ppObjectInfo)->ParentObjectInformation,
1890                                                 NULL,
1891                                                 ulFilter,
1892                                                 FILE_ACTION_MODIFIED);
1893             }
1894
1895             //
1896             // Indicate this node requires re-evaluation for the remaining reasons
1897             //
1898
1899             (*ppObjectInfo)->Expiration.QuadPart = 0;
1900
1901             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1902                           AFS_TRACE_LEVEL_VERBOSE,
1903                           "AFSInvalidateObject Setting VERIFY flag on fid %08lX-%08lX-%08lX-%08lX\n",
1904                           (*ppObjectInfo)->FileId.Cell,
1905                           (*ppObjectInfo)->FileId.Volume,
1906                           (*ppObjectInfo)->FileId.Vnode,
1907                           (*ppObjectInfo)->FileId.Unique);
1908
1909             SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_VERIFY);
1910
1911             if( Reason == AFS_INVALIDATE_DATA_VERSION ||
1912                 (*ppObjectInfo)->FileType == AFS_FILE_TYPE_FILE &&
1913                 ( Reason == AFS_INVALIDATE_CALLBACK ||
1914                   Reason == AFS_INVALIDATE_EXPIRED))
1915             {
1916                 if ( NT_SUCCESS( AFSQueueInvalidateObject( (*ppObjectInfo),
1917                                                            AFS_INVALIDATE_DATA_VERSION)))
1918                 {
1919
1920                     (*ppObjectInfo) = NULL; // We'll dec the count in the worker item
1921                 }
1922             }
1923
1924             break;
1925         }
1926     }
1927
1928   try_exit:
1929
1930     return ntStatus;
1931 }
1932
1933 NTSTATUS
1934 AFSInvalidateCache( IN AFSInvalidateCacheCB *InvalidateCB)
1935 {
1936
1937     NTSTATUS ntStatus = STATUS_SUCCESS;
1938     AFSVolumeCB *pVolumeCB = NULL;
1939     AFSDeviceExt *pDevExt = (AFSDeviceExt *) AFSRDRDeviceObject->DeviceExtension;
1940     ULONGLONG   ullIndex = 0;
1941     AFSObjectInfoCB *pObjectInfo = NULL;
1942     LONG lCount;
1943
1944     __Enter
1945     {
1946
1947         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1948                       AFS_TRACE_LEVEL_VERBOSE,
1949                       "AFSInvalidateCache Invalidation FID %08lX-%08lX-%08lX-%08lX Type %d WholeVolume %d Reason %d\n",
1950                       InvalidateCB->FileID.Cell,
1951                       InvalidateCB->FileID.Volume,
1952                       InvalidateCB->FileID.Vnode,
1953                       InvalidateCB->FileID.Unique,
1954                       InvalidateCB->FileType,
1955                       InvalidateCB->WholeVolume,
1956                       InvalidateCB->Reason);
1957
1958         //
1959         // Need to locate the Fcb for the directory to purge
1960         //
1961
1962         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1963                       AFS_TRACE_LEVEL_VERBOSE,
1964                       "AFSInvalidateCache Acquiring RDR VolumeTreeLock lock %p SHARED %08lX\n",
1965                       &pDevExt->Specific.RDR.VolumeTreeLock,
1966                       PsGetCurrentThread());
1967
1968         //
1969         // Starve any exclusive waiters on this paticular call
1970         //
1971
1972         AFSAcquireSharedStarveExclusive( &pDevExt->Specific.RDR.VolumeTreeLock, TRUE);
1973
1974         //
1975         // Locate the volume node
1976         //
1977
1978         ullIndex = AFSCreateHighIndex( &InvalidateCB->FileID);
1979
1980         ntStatus = AFSLocateHashEntry( pDevExt->Specific.RDR.VolumeTree.TreeHead,
1981                                        ullIndex,
1982                                        (AFSBTreeEntry **)&pVolumeCB);
1983
1984         if( pVolumeCB != NULL)
1985         {
1986
1987             lCount = InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
1988
1989             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1990                           AFS_TRACE_LEVEL_VERBOSE,
1991                           "AFSInvalidateCache Increment count on volume %p Cnt %d\n",
1992                           pVolumeCB,
1993                           lCount);
1994         }
1995
1996         AFSReleaseResource( &pDevExt->Specific.RDR.VolumeTreeLock);
1997
1998         if( !NT_SUCCESS( ntStatus) ||
1999             pVolumeCB == NULL)
2000         {
2001
2002             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2003                           AFS_TRACE_LEVEL_WARNING,
2004                           "AFSInvalidateCache Invalidation FAILURE Unable to locate volume node FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
2005                           InvalidateCB->FileID.Cell,
2006                           InvalidateCB->FileID.Volume,
2007                           InvalidateCB->FileID.Vnode,
2008                           InvalidateCB->FileID.Unique,
2009                           ntStatus);
2010
2011             try_return( ntStatus = STATUS_SUCCESS);
2012         }
2013
2014         //
2015         // If this is a whole volume invalidation then go do it now
2016         //
2017
2018         if( InvalidateCB->WholeVolume)
2019         {
2020
2021             ntStatus = AFSInvalidateVolume( pVolumeCB,
2022                                             InvalidateCB->Reason);
2023
2024             try_return( ntStatus);
2025         }
2026
2027         AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
2028                           TRUE);
2029
2030         if ( AFSIsVolumeFID( &InvalidateCB->FileID))
2031         {
2032
2033             pObjectInfo = &pVolumeCB->ObjectInformation;
2034         }
2035         else
2036         {
2037
2038             ullIndex = AFSCreateLowIndex( &InvalidateCB->FileID);
2039
2040             ntStatus = AFSLocateHashEntry( pVolumeCB->ObjectInfoTree.TreeHead,
2041                                            ullIndex,
2042                                            (AFSBTreeEntry **)&pObjectInfo);
2043         }
2044
2045         if( pObjectInfo != NULL)
2046         {
2047
2048             //
2049             // Reference the node so it won't be torn down
2050             //
2051
2052             lCount = AFSObjectInfoIncrement( pObjectInfo,
2053                                              AFS_OBJECT_REFERENCE_INVALIDATION);
2054
2055             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2056                           AFS_TRACE_LEVEL_VERBOSE,
2057                           "AFSInvalidateCache Increment count on object %p Cnt %d\n",
2058                           pObjectInfo,
2059                           lCount);
2060         }
2061
2062         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
2063
2064         if( !NT_SUCCESS( ntStatus) ||
2065             pObjectInfo == NULL)
2066         {
2067
2068             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2069                           AFS_TRACE_LEVEL_VERBOSE,
2070                           "AFSInvalidateCache Invalidation FAILURE Unable to locate object FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
2071                           InvalidateCB->FileID.Cell,
2072                           InvalidateCB->FileID.Volume,
2073                           InvalidateCB->FileID.Vnode,
2074                           InvalidateCB->FileID.Unique,
2075                           ntStatus);
2076
2077             try_return( ntStatus = STATUS_SUCCESS);
2078         }
2079
2080         AFSInvalidateObject( &pObjectInfo,
2081                              InvalidateCB->Reason);
2082
2083 try_exit:
2084
2085         if( pObjectInfo != NULL)
2086         {
2087
2088             lCount = AFSObjectInfoDecrement( pObjectInfo,
2089                                              AFS_OBJECT_REFERENCE_INVALIDATION);
2090
2091             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2092                           AFS_TRACE_LEVEL_VERBOSE,
2093                           "AFSInvalidateCache Decrement count on object %p Cnt %d\n",
2094                           pObjectInfo,
2095                           lCount);
2096         }
2097
2098         if ( pVolumeCB != NULL)
2099         {
2100
2101             lCount = InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
2102
2103             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2104                           AFS_TRACE_LEVEL_VERBOSE,
2105                           "AFSInvalidateCache Decrement count on volume %p Cnt %d\n",
2106                           pVolumeCB,
2107                           lCount);
2108         }
2109     }
2110
2111     return ntStatus;
2112 }
2113
2114 BOOLEAN
2115 AFSIsChildOfParent( IN AFSFcb *Dcb,
2116                     IN AFSFcb *Fcb)
2117 {
2118
2119     BOOLEAN bIsChild = FALSE;
2120     AFSFcb *pCurrentFcb = Fcb;
2121
2122     while( pCurrentFcb != NULL)
2123     {
2124
2125         if( pCurrentFcb->ObjectInformation->ParentObjectInformation == Dcb->ObjectInformation)
2126         {
2127
2128             bIsChild = TRUE;
2129
2130             break;
2131         }
2132
2133         pCurrentFcb = pCurrentFcb->ObjectInformation->ParentObjectInformation->Fcb;
2134     }
2135
2136     return bIsChild;
2137 }
2138
2139 inline
2140 ULONGLONG
2141 AFSCreateHighIndex( IN AFSFileID *FileID)
2142 {
2143
2144     ULONGLONG ullIndex = 0;
2145
2146     ullIndex = (((ULONGLONG)FileID->Cell << 32) | FileID->Volume);
2147
2148     return ullIndex;
2149 }
2150
2151 inline
2152 ULONGLONG
2153 AFSCreateLowIndex( IN AFSFileID *FileID)
2154 {
2155
2156     ULONGLONG ullIndex = 0;
2157
2158     ullIndex = (((ULONGLONG)FileID->Vnode << 32) | FileID->Unique);
2159
2160     return ullIndex;
2161 }
2162
2163 BOOLEAN
2164 AFSCheckAccess( IN ACCESS_MASK DesiredAccess,
2165                 IN ACCESS_MASK GrantedAccess,
2166                 IN BOOLEAN DirectoryEntry)
2167 {
2168
2169     BOOLEAN bAccessGranted = TRUE;
2170
2171     //
2172     // Check if we are asking for read/write and granted only read only
2173     // NOTE: There will be more checks here
2174     //
2175
2176     if( !AFSCheckForReadOnlyAccess( DesiredAccess,
2177                                     DirectoryEntry) &&
2178         AFSCheckForReadOnlyAccess( GrantedAccess,
2179                                    DirectoryEntry))
2180     {
2181
2182         bAccessGranted = FALSE;
2183     }
2184
2185     return bAccessGranted;
2186 }
2187
2188 NTSTATUS
2189 AFSGetDriverStatus( IN AFSDriverStatusRespCB *DriverStatus)
2190 {
2191
2192     NTSTATUS         ntStatus = STATUS_SUCCESS;
2193     AFSDeviceExt    *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
2194
2195     //
2196     // Start with read
2197     //
2198
2199     DriverStatus->Status = AFS_DRIVER_STATUS_READY;
2200
2201     if( AFSGlobalRoot == NULL)
2202     {
2203
2204         //
2205         // We are not ready
2206         //
2207
2208         DriverStatus->Status = AFS_DRIVER_STATUS_NOT_READY;
2209     }
2210
2211     if( pControlDevExt->Specific.Control.CommServiceCB.IrpPoolControlFlag != POOL_ACTIVE)
2212     {
2213
2214         //
2215         // No service yet
2216         //
2217
2218         DriverStatus->Status = AFS_DRIVER_STATUS_NO_SERVICE;
2219     }
2220
2221     return ntStatus;
2222 }
2223
2224 NTSTATUS
2225 AFSSubstituteSysName( IN UNICODE_STRING *ComponentName,
2226                       IN UNICODE_STRING *SubstituteName,
2227                       IN ULONG StringIndex)
2228 {
2229
2230     NTSTATUS ntStatus = STATUS_SUCCESS;
2231     AFSDeviceExt    *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
2232     AFSSysNameCB    *pSysName = NULL;
2233     ERESOURCE       *pSysNameLock = NULL;
2234     ULONG            ulIndex = 1;
2235     USHORT           usIndex = 0;
2236     UNICODE_STRING   uniSysName;
2237
2238     __Enter
2239     {
2240
2241 #if defined(_WIN64)
2242
2243         if( IoIs32bitProcess( NULL))
2244         {
2245
2246             pSysNameLock = &pControlDevExt->Specific.Control.SysName32ListLock;
2247
2248             pSysName = pControlDevExt->Specific.Control.SysName32ListHead;
2249         }
2250         else
2251         {
2252
2253             pSysNameLock = &pControlDevExt->Specific.Control.SysName64ListLock;
2254
2255             pSysName = pControlDevExt->Specific.Control.SysName64ListHead;
2256         }
2257 #else
2258
2259         pSysNameLock = &pControlDevExt->Specific.Control.SysName32ListLock;
2260
2261         pSysName = pControlDevExt->Specific.Control.SysName32ListHead;
2262
2263 #endif
2264
2265         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2266                       AFS_TRACE_LEVEL_VERBOSE,
2267                       "AFSSubstituteSysName Acquiring SysName lock %p SHARED %08lX\n",
2268                       pSysNameLock,
2269                       PsGetCurrentThread());
2270
2271         AFSAcquireShared( pSysNameLock,
2272                           TRUE);
2273
2274         //
2275         // Find where we are in the list
2276         //
2277
2278         while( pSysName != NULL &&
2279             ulIndex < StringIndex)
2280         {
2281
2282             pSysName = pSysName->fLink;
2283
2284             ulIndex++;
2285         }
2286
2287         if( pSysName == NULL)
2288         {
2289
2290             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2291                           AFS_TRACE_LEVEL_VERBOSE_2,
2292                           "AFSSubstituteSysName No sysname %wZ Status %08lX\n",
2293                           &ComponentName,
2294                           STATUS_OBJECT_NAME_NOT_FOUND);
2295
2296             try_return( ntStatus = STATUS_OBJECT_NAME_NOT_FOUND);
2297         }
2298
2299         RtlInitUnicodeString( &uniSysName,
2300                               L"@SYS");
2301         //
2302         // If it is a full component of @SYS then just substitue the
2303         // name in
2304         //
2305
2306         if( RtlCompareUnicodeString( &uniSysName,
2307                                      ComponentName,
2308                                      TRUE) == 0)
2309         {
2310
2311             SubstituteName->Length = pSysName->SysName.Length;
2312             SubstituteName->MaximumLength = SubstituteName->Length;
2313
2314             SubstituteName->Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2315                                                                         SubstituteName->Length,
2316                                                                         AFS_SUBST_BUFFER_TAG);
2317
2318             if( SubstituteName->Buffer == NULL)
2319             {
2320
2321                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2322             }
2323
2324             RtlCopyMemory( SubstituteName->Buffer,
2325                            pSysName->SysName.Buffer,
2326                            pSysName->SysName.Length);
2327         }
2328         else
2329         {
2330
2331             usIndex = 0;
2332
2333             while( ComponentName->Buffer[ usIndex] != L'@')
2334             {
2335
2336                 usIndex++;
2337             }
2338
2339             SubstituteName->Length = (usIndex * sizeof( WCHAR)) + pSysName->SysName.Length;
2340             SubstituteName->MaximumLength = SubstituteName->Length;
2341
2342             SubstituteName->Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2343                                                                         SubstituteName->Length,
2344                                                                         AFS_SUBST_BUFFER_TAG);
2345
2346             if( SubstituteName->Buffer == NULL)
2347             {
2348
2349                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2350             }
2351
2352             RtlCopyMemory( SubstituteName->Buffer,
2353                            ComponentName->Buffer,
2354                            usIndex * sizeof( WCHAR));
2355
2356             RtlCopyMemory( &SubstituteName->Buffer[ usIndex],
2357                            pSysName->SysName.Buffer,
2358                            pSysName->SysName.Length);
2359         }
2360
2361 try_exit:
2362
2363         AFSReleaseResource( pSysNameLock);
2364     }
2365
2366     return ntStatus;
2367 }
2368
2369 NTSTATUS
2370 AFSSubstituteNameInPath( IN OUT UNICODE_STRING *FullPathName,
2371                          IN OUT UNICODE_STRING *ComponentName,
2372                          IN UNICODE_STRING *SubstituteName,
2373                          IN OUT UNICODE_STRING *RemainingPath,
2374                          IN BOOLEAN FreePathName)
2375 {
2376
2377     NTSTATUS ntStatus = STATUS_SUCCESS;
2378     UNICODE_STRING uniPathName;
2379     USHORT usPrefixNameLen = 0;
2380     SHORT  sNameLenDelta = 0;
2381
2382     __Enter
2383     {
2384
2385         //
2386         // If the passed in name can handle the additional length
2387         // then just moves things around
2388         //
2389
2390         sNameLenDelta = SubstituteName->Length - ComponentName->Length;
2391
2392         usPrefixNameLen = (USHORT)(ComponentName->Buffer - FullPathName->Buffer);
2393
2394         if( FullPathName->MaximumLength > FullPathName->Length + sNameLenDelta)
2395         {
2396
2397             if( FullPathName->Length > usPrefixNameLen + ComponentName->Length)
2398             {
2399
2400                 RtlMoveMemory( &FullPathName->Buffer[ ((usPrefixNameLen*sizeof( WCHAR) + SubstituteName->Length)/sizeof( WCHAR))],
2401                                &FullPathName->Buffer[ ((usPrefixNameLen*sizeof( WCHAR) + ComponentName->Length)/sizeof( WCHAR))],
2402                                FullPathName->Length - usPrefixNameLen*sizeof( WCHAR) - ComponentName->Length);
2403             }
2404
2405             RtlCopyMemory( &FullPathName->Buffer[ usPrefixNameLen],
2406                            SubstituteName->Buffer,
2407                            SubstituteName->Length);
2408
2409             FullPathName->Length += sNameLenDelta;
2410
2411             ComponentName->Length += sNameLenDelta;
2412
2413             ComponentName->MaximumLength = ComponentName->Length;
2414
2415             if ( RemainingPath->Buffer)
2416             {
2417
2418                 RemainingPath->Buffer += sNameLenDelta/sizeof( WCHAR);
2419             }
2420
2421             try_return( ntStatus);
2422         }
2423
2424         //
2425         // Need to re-allocate the buffer
2426         //
2427
2428         uniPathName.Length = FullPathName->Length -
2429                                          ComponentName->Length +
2430                                          SubstituteName->Length;
2431
2432         uniPathName.MaximumLength = FullPathName->MaximumLength + PAGE_SIZE;
2433
2434         uniPathName.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2435                                                                 uniPathName.MaximumLength,
2436                                                                 AFS_NAME_BUFFER_FOUR_TAG);
2437
2438         if( uniPathName.Buffer == NULL)
2439         {
2440
2441             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2442         }
2443
2444         usPrefixNameLen = (USHORT)(ComponentName->Buffer - FullPathName->Buffer);
2445
2446         usPrefixNameLen *= sizeof( WCHAR);
2447
2448         RtlZeroMemory( uniPathName.Buffer,
2449                        uniPathName.MaximumLength);
2450
2451         RtlCopyMemory( uniPathName.Buffer,
2452                        FullPathName->Buffer,
2453                        usPrefixNameLen);
2454
2455         RtlCopyMemory( &uniPathName.Buffer[ (usPrefixNameLen/sizeof( WCHAR))],
2456                        SubstituteName->Buffer,
2457                        SubstituteName->Length);
2458
2459         if( FullPathName->Length > usPrefixNameLen + ComponentName->Length)
2460         {
2461
2462             RtlCopyMemory( &uniPathName.Buffer[ (usPrefixNameLen + SubstituteName->Length)/sizeof( WCHAR)],
2463                            &FullPathName->Buffer[ (usPrefixNameLen + ComponentName->Length)/sizeof( WCHAR)],
2464                            FullPathName->Length - usPrefixNameLen - ComponentName->Length);
2465         }
2466
2467         ComponentName->Buffer = uniPathName.Buffer + (ComponentName->Buffer - FullPathName->Buffer);
2468
2469         ComponentName->Length += sNameLenDelta;
2470
2471         ComponentName->MaximumLength = ComponentName->Length;
2472
2473         if ( RemainingPath->Buffer)
2474         {
2475
2476             RemainingPath->Buffer = uniPathName.Buffer
2477                 + (RemainingPath->Buffer - FullPathName->Buffer)
2478                 + sNameLenDelta/sizeof( WCHAR);
2479         }
2480
2481         if( FreePathName)
2482         {
2483             AFSExFreePoolWithTag( FullPathName->Buffer, 0);
2484         }
2485
2486         *FullPathName = uniPathName;
2487
2488 try_exit:
2489
2490         NOTHING;
2491     }
2492
2493     return ntStatus;
2494 }
2495
2496 NTSTATUS
2497 AFSInvalidateVolume( IN AFSVolumeCB *VolumeCB,
2498                      IN ULONG Reason)
2499 {
2500
2501     NTSTATUS ntStatus = STATUS_SUCCESS;
2502     AFSObjectInfoCB *pCurrentObject = NULL;
2503     AFSObjectInfoCB *pNextObject = NULL;
2504     LONG lCount;
2505
2506     __Enter
2507     {
2508
2509         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2510                       AFS_TRACE_LEVEL_VERBOSE,
2511                       "AFSInvalidateVolume Invalidate volume fid %08lX-%08lX-%08lX-%08lX Reason %08lX\n",
2512                       VolumeCB->ObjectInformation.FileId.Cell,
2513                       VolumeCB->ObjectInformation.FileId.Volume,
2514                       VolumeCB->ObjectInformation.FileId.Vnode,
2515                       VolumeCB->ObjectInformation.FileId.Unique,
2516                       Reason);
2517
2518         //
2519         // Depending on the reason for invalidation then perform work on the node
2520         //
2521
2522         switch( Reason)
2523         {
2524
2525             case AFS_INVALIDATE_DELETED:
2526             {
2527
2528                 //
2529                 // Mark this volume as invalid
2530                 //
2531
2532                 SetFlag( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID);
2533
2534                 SetFlag( VolumeCB->Flags, AFS_VOLUME_FLAGS_OFFLINE);
2535
2536                 break;
2537             }
2538         }
2539
2540         //
2541         // Invalidate the volume root directory
2542         //
2543
2544         pCurrentObject = &VolumeCB->ObjectInformation;
2545
2546         if ( pCurrentObject )
2547         {
2548
2549             lCount = AFSObjectInfoIncrement( pCurrentObject,
2550                                              AFS_OBJECT_REFERENCE_INVALIDATION);
2551
2552             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2553                           AFS_TRACE_LEVEL_VERBOSE,
2554                           "AFSInvalidateVolumeObjects Increment count on object %p Cnt %d\n",
2555                           pCurrentObject,
2556                           lCount);
2557
2558             AFSInvalidateObject( &pCurrentObject,
2559                                  Reason);
2560
2561             if ( pCurrentObject)
2562             {
2563
2564                 lCount = AFSObjectInfoDecrement( pCurrentObject,
2565                                                  AFS_OBJECT_REFERENCE_INVALIDATION);
2566
2567                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2568                               AFS_TRACE_LEVEL_VERBOSE,
2569                               "AFSInvalidateVolumeObjects Decrement count on object %p Cnt %d\n",
2570                               pCurrentObject,
2571                               lCount);
2572             }
2573         }
2574
2575         //
2576         // Apply invalidation to all other volume objects
2577         //
2578
2579         AFSAcquireShared( VolumeCB->ObjectInfoTree.TreeLock,
2580                           TRUE);
2581
2582         pCurrentObject = VolumeCB->ObjectInfoListHead;
2583
2584         if ( pCurrentObject)
2585         {
2586
2587             //
2588             // Reference the node so it won't be torn down
2589             //
2590
2591             lCount = AFSObjectInfoIncrement( pCurrentObject,
2592                                              AFS_OBJECT_REFERENCE_INVALIDATION);
2593
2594             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2595                           AFS_TRACE_LEVEL_VERBOSE,
2596                           "AFSInvalidateVolumeObjects Increment count on object %p Cnt %d\n",
2597                           pCurrentObject,
2598                           lCount);
2599         }
2600
2601         while( pCurrentObject != NULL)
2602         {
2603
2604             pNextObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
2605
2606             if ( pNextObject)
2607             {
2608
2609                 //
2610                 // Reference the node so it won't be torn down
2611                 //
2612
2613                 lCount = AFSObjectInfoIncrement( pNextObject,
2614                                                  AFS_OBJECT_REFERENCE_INVALIDATION);
2615
2616                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2617                               AFS_TRACE_LEVEL_VERBOSE,
2618                               "AFSInvalidateVolumeObjects Increment count on object %p Cnt %d\n",
2619                               pNextObject,
2620                               lCount);
2621             }
2622
2623             AFSReleaseResource( VolumeCB->ObjectInfoTree.TreeLock);
2624
2625             AFSInvalidateObject( &pCurrentObject,
2626                                  Reason);
2627
2628             if ( pCurrentObject )
2629             {
2630
2631                 lCount = AFSObjectInfoDecrement( pCurrentObject,
2632                                                  AFS_OBJECT_REFERENCE_INVALIDATION);
2633
2634                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2635                               AFS_TRACE_LEVEL_VERBOSE,
2636                               "AFSInvalidateVolumeObjects Decrement count on object %p Cnt %d\n",
2637                               pCurrentObject,
2638                               lCount);
2639             }
2640
2641             AFSAcquireShared( VolumeCB->ObjectInfoTree.TreeLock,
2642                               TRUE);
2643
2644             pCurrentObject = pNextObject;
2645         }
2646
2647         AFSReleaseResource( VolumeCB->ObjectInfoTree.TreeLock);
2648     }
2649
2650     return ntStatus;
2651 }
2652
2653 VOID
2654 AFSInvalidateAllVolumes( VOID)
2655 {
2656     AFSVolumeCB *pVolumeCB = NULL;
2657     AFSVolumeCB *pNextVolumeCB = NULL;
2658     AFSDeviceExt *pRDRDeviceExt = NULL;
2659     LONG lCount;
2660
2661     pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2662
2663     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2664                   AFS_TRACE_LEVEL_VERBOSE,
2665                   "AFSInvalidateAllVolumes Acquiring RDR VolumeListLock lock %p SHARED %08lX\n",
2666                   &pRDRDeviceExt->Specific.RDR.VolumeListLock,
2667                   PsGetCurrentThread());
2668
2669     AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
2670                       TRUE);
2671
2672     pVolumeCB = pRDRDeviceExt->Specific.RDR.VolumeListHead;
2673
2674     if ( pVolumeCB)
2675     {
2676
2677         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2678                       AFS_TRACE_LEVEL_VERBOSE,
2679                       "AFSInvalidateAllVolumes Acquiring VolumeRoot ObjectInfoTree lock %p SHARED %08lX\n",
2680                       pVolumeCB->ObjectInfoTree.TreeLock,
2681                       PsGetCurrentThread());
2682
2683         lCount = InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
2684
2685         AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2686                       AFS_TRACE_LEVEL_VERBOSE,
2687                       "AFSInvalidateAllVolumes Increment count on volume %p Cnt %d\n",
2688                       pVolumeCB,
2689                       lCount);
2690     }
2691
2692     while( pVolumeCB != NULL)
2693     {
2694
2695         pNextVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
2696
2697         if ( pNextVolumeCB)
2698         {
2699
2700             lCount = InterlockedIncrement( &pNextVolumeCB->VolumeReferenceCount);
2701
2702             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2703                           AFS_TRACE_LEVEL_VERBOSE,
2704                           "AFSInvalidateAllVolumes Increment count on volume %p Cnt %d\n",
2705                           pVolumeCB,
2706                           lCount);
2707         }
2708
2709         AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
2710
2711         // do I need to hold the volume lock here?
2712
2713         AFSInvalidateVolume( pVolumeCB, AFS_INVALIDATE_EXPIRED);
2714
2715         AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
2716                           TRUE);
2717
2718         lCount = InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
2719
2720         AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2721                       AFS_TRACE_LEVEL_VERBOSE,
2722                       "AFSInvalidateAllVolumes Decrement count on volume %p Cnt %d\n",
2723                       pVolumeCB,
2724                       lCount);
2725
2726         pVolumeCB = pNextVolumeCB;
2727     }
2728
2729     AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
2730 }
2731
2732 NTSTATUS
2733 AFSVerifyEntry( IN GUID *AuthGroup,
2734                 IN AFSDirectoryCB *DirEntry)
2735 {
2736
2737     NTSTATUS ntStatus = STATUS_SUCCESS;
2738     AFSDirEnumEntry *pDirEnumEntry = NULL;
2739     AFSObjectInfoCB *pObjectInfo = DirEntry->ObjectInformation;
2740     IO_STATUS_BLOCK stIoStatus;
2741
2742     __Enter
2743     {
2744
2745         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2746                       AFS_TRACE_LEVEL_VERBOSE_2,
2747                       "AFSVerifyEntry Verifying entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2748                       &DirEntry->NameInformation.FileName,
2749                       pObjectInfo->FileId.Cell,
2750                       pObjectInfo->FileId.Volume,
2751                       pObjectInfo->FileId.Vnode,
2752                       pObjectInfo->FileId.Unique);
2753
2754         ntStatus = AFSEvaluateTargetByID( pObjectInfo,
2755                                           AuthGroup,
2756                                           FALSE,
2757                                           &pDirEnumEntry);
2758
2759         if( !NT_SUCCESS( ntStatus))
2760         {
2761
2762             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2763                           AFS_TRACE_LEVEL_ERROR,
2764                           "AFSVerifyEntry Evaluate Target failed %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
2765                           &DirEntry->NameInformation.FileName,
2766                           pObjectInfo->FileId.Cell,
2767                           pObjectInfo->FileId.Volume,
2768                           pObjectInfo->FileId.Vnode,
2769                           pObjectInfo->FileId.Unique,
2770                           ntStatus);
2771
2772             try_return( ntStatus);
2773         }
2774
2775         //
2776         // Check the data version of the file
2777         //
2778
2779         if( pObjectInfo->DataVersion.QuadPart == pDirEnumEntry->DataVersion.QuadPart)
2780         {
2781             if ( !BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA))
2782             {
2783
2784                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2785                               AFS_TRACE_LEVEL_VERBOSE,
2786                               "AFSVerifyEntry No DV change %I64X for Fcb %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2787                               pObjectInfo->DataVersion.QuadPart,
2788                               &DirEntry->NameInformation.FileName,
2789                               pObjectInfo->FileId.Cell,
2790                               pObjectInfo->FileId.Volume,
2791                               pObjectInfo->FileId.Vnode,
2792                               pObjectInfo->FileId.Unique);
2793
2794                 //
2795                 // We are ok, just get out
2796                 //
2797
2798                 ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2799
2800                 try_return( ntStatus = STATUS_SUCCESS);
2801             }
2802         }
2803
2804         //
2805         // New data version so we will need to process the node based on the type
2806         //
2807
2808         switch( pDirEnumEntry->FileType)
2809         {
2810
2811             case AFS_FILE_TYPE_MOUNTPOINT:
2812             {
2813
2814                 //
2815                 // For a mount point we need to ensure the target is the same
2816                 //
2817
2818                 if( !AFSIsEqualFID( &pObjectInfo->TargetFileId,
2819                                     &pDirEnumEntry->TargetFileId))
2820                 {
2821
2822                 }
2823
2824                 //
2825                 // Update the metadata for the entry
2826                 //
2827
2828                 ntStatus = AFSUpdateMetaData( DirEntry,
2829                                               pDirEnumEntry);
2830
2831                 if( NT_SUCCESS( ntStatus))
2832                 {
2833
2834                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2835                 }
2836
2837                 break;
2838             }
2839
2840             case AFS_FILE_TYPE_SYMLINK:
2841             {
2842
2843                 //
2844                 // Update the metadata for the entry
2845                 //
2846
2847                 ntStatus = AFSUpdateMetaData( DirEntry,
2848                                               pDirEnumEntry);
2849
2850                 if( NT_SUCCESS( ntStatus))
2851                 {
2852
2853                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2854                 }
2855
2856                 break;
2857             }
2858
2859             case AFS_FILE_TYPE_FILE:
2860             {
2861                 FILE_OBJECT * pCCFileObject = NULL;
2862                 BOOLEAN bPurgeExtents = FALSE;
2863
2864                 if ( pObjectInfo->DataVersion.QuadPart != pDirEnumEntry->DataVersion.QuadPart)
2865                 {
2866
2867                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2868                                   AFS_TRACE_LEVEL_VERBOSE,
2869                                   "AFSVerifyEntry DV Change %wZ FID %08lX-%08lX-%08lX-%08lX (%08lX != %08lX)\n",
2870                                   &DirEntry->NameInformation.FileName,
2871                                   pObjectInfo->FileId.Cell,
2872                                   pObjectInfo->FileId.Volume,
2873                                   pObjectInfo->FileId.Vnode,
2874                                   pObjectInfo->FileId.Unique,
2875                                   pObjectInfo->DataVersion.LowPart,
2876                                   pDirEnumEntry->DataVersion.LowPart
2877                                   );
2878
2879                     bPurgeExtents = TRUE;
2880                 }
2881
2882                 if ( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA))
2883                 {
2884
2885                     bPurgeExtents = TRUE;
2886
2887                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2888                                   AFS_TRACE_LEVEL_VERBOSE,
2889                                   "AFSVerifyEntry Clearing VERIFY_DATA flag %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2890                                   &DirEntry->NameInformation.FileName,
2891                                   pObjectInfo->FileId.Cell,
2892                                   pObjectInfo->FileId.Volume,
2893                                   pObjectInfo->FileId.Vnode,
2894                                   pObjectInfo->FileId.Unique);
2895
2896                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA);
2897                 }
2898
2899                 if( pObjectInfo->Fcb != NULL)
2900                 {
2901
2902                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2903                                   AFS_TRACE_LEVEL_VERBOSE,
2904                                   "AFSVerifyEntry Flush/purge entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2905                                   &DirEntry->NameInformation.FileName,
2906                                   pObjectInfo->FileId.Cell,
2907                                   pObjectInfo->FileId.Volume,
2908                                   pObjectInfo->FileId.Vnode,
2909                                   pObjectInfo->FileId.Unique);
2910
2911                     AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2912                                     TRUE);
2913
2914                     __try
2915                     {
2916
2917                         CcFlushCache( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2918                                       NULL,
2919                                       0,
2920                                       &stIoStatus);
2921
2922                         if( !NT_SUCCESS( stIoStatus.Status))
2923                         {
2924
2925                             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2926                                           AFS_TRACE_LEVEL_ERROR,
2927                                           "AFSVerifyEntry CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
2928                                           &DirEntry->NameInformation.FileName,
2929                                           pObjectInfo->FileId.Cell,
2930                                           pObjectInfo->FileId.Volume,
2931                                           pObjectInfo->FileId.Vnode,
2932                                           pObjectInfo->FileId.Unique,
2933                                           stIoStatus.Status,
2934                                           stIoStatus.Information);
2935
2936                             ntStatus = stIoStatus.Status;
2937                         }
2938
2939                         if ( bPurgeExtents &&
2940                              pObjectInfo->Fcb->NPFcb->SectionObjectPointers.DataSectionObject != NULL)
2941                         {
2942
2943                             if ( !CcPurgeCacheSection( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2944                                                        NULL,
2945                                                        0,
2946                                                        FALSE))
2947                             {
2948
2949                                 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2950                                               AFS_TRACE_LEVEL_WARNING,
2951                                               "AFSVerifyEntry CcPurgeCacheSection failure %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2952                                               &DirEntry->NameInformation.FileName,
2953                                               pObjectInfo->FileId.Cell,
2954                                               pObjectInfo->FileId.Volume,
2955                                               pObjectInfo->FileId.Vnode,
2956                                               pObjectInfo->FileId.Unique);
2957
2958                                 SetFlag( pObjectInfo->Fcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
2959                             }
2960                         }
2961                     }
2962                     __except( EXCEPTION_EXECUTE_HANDLER)
2963                     {
2964                         ntStatus = GetExceptionCode();
2965
2966                         AFSDbgLogMsg( 0,
2967                                       0,
2968                                       "EXCEPTION - AFSVerifyEntry CcFlushCache or CcPurgeCacheSection %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
2969                                       &DirEntry->NameInformation.FileName,
2970                                       pObjectInfo->FileId.Cell,
2971                                       pObjectInfo->FileId.Volume,
2972                                       pObjectInfo->FileId.Vnode,
2973                                       pObjectInfo->FileId.Unique,
2974                                       ntStatus);
2975
2976                         SetFlag( pObjectInfo->Fcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
2977                     }
2978
2979                     AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
2980
2981                     if ( bPurgeExtents)
2982                     {
2983                         AFSFlushExtents( pObjectInfo->Fcb,
2984                                          AuthGroup);
2985                     }
2986
2987                     //
2988                     // Reacquire the Fcb to purge the cache
2989                     //
2990
2991                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2992                                   AFS_TRACE_LEVEL_VERBOSE,
2993                                   "AFSVerifyEntry Acquiring Fcb lock %p EXCL %08lX\n",
2994                                   &pObjectInfo->Fcb->NPFcb->Resource,
2995                                   PsGetCurrentThread());
2996
2997                     AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Resource,
2998                                     TRUE);
2999
3000                     //
3001                     // Update the metadata for the entry
3002                     //
3003
3004                     ntStatus = AFSUpdateMetaData( DirEntry,
3005                                                   pDirEnumEntry);
3006
3007                     if( !NT_SUCCESS( ntStatus))
3008                     {
3009
3010                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3011                                       AFS_TRACE_LEVEL_ERROR,
3012                                       "AFSVerifyEntry Meta Data Update failed %wZ FID %08lX-%08lX-%08lX-%08lX ntStatus %08lX\n",
3013                                       &DirEntry->NameInformation.FileName,
3014                                       pObjectInfo->FileId.Cell,
3015                                       pObjectInfo->FileId.Volume,
3016                                       pObjectInfo->FileId.Vnode,
3017                                       pObjectInfo->FileId.Unique,
3018                                       ntStatus);
3019
3020                         break;
3021                     }
3022
3023                     //
3024                     // Update file sizes
3025                     //
3026
3027                     pObjectInfo->Fcb->Header.AllocationSize.QuadPart  = pObjectInfo->AllocationSize.QuadPart;
3028                     pObjectInfo->Fcb->Header.FileSize.QuadPart        = pObjectInfo->EndOfFile.QuadPart;
3029                     pObjectInfo->Fcb->Header.ValidDataLength.QuadPart = pObjectInfo->EndOfFile.QuadPart;
3030
3031                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3032                                   AFS_TRACE_LEVEL_VERBOSE,
3033                                   "AFSVerifyEntry Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
3034                                   &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3035                                   PsGetCurrentThread());
3036
3037                     AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3038                                     TRUE);
3039
3040                     pCCFileObject = CcGetFileObjectFromSectionPtrs( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers);
3041
3042                     if ( pCCFileObject != NULL)
3043                     {
3044                         CcSetFileSizes( pCCFileObject,
3045                                         (PCC_FILE_SIZES)&pObjectInfo->Fcb->Header.AllocationSize);
3046                     }
3047
3048                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3049                                   AFS_TRACE_LEVEL_VERBOSE,
3050                                   "AFSVerifyEntry Releasing Fcb SectionObject lock %p EXCL %08lX\n",
3051                                   &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3052                                   PsGetCurrentThread());
3053
3054                     AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
3055
3056                     AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->Resource);
3057                 }
3058                 else
3059                 {
3060
3061                     //
3062                     // Update the metadata for the entry
3063                     //
3064
3065                     ntStatus = AFSUpdateMetaData( DirEntry,
3066                                                   pDirEnumEntry);
3067
3068                     if( !NT_SUCCESS( ntStatus))
3069                     {
3070
3071                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3072                                       AFS_TRACE_LEVEL_ERROR,
3073                                       "AFSVerifyEntry Meta Data Update failed %wZ FID %08lX-%08lX-%08lX-%08lX ntStatus %08lX\n",
3074                                       &DirEntry->NameInformation.FileName,
3075                                       pObjectInfo->FileId.Cell,
3076                                       pObjectInfo->FileId.Volume,
3077                                       pObjectInfo->FileId.Vnode,
3078                                       pObjectInfo->FileId.Unique,
3079                                       ntStatus);
3080
3081                         break;
3082                     }
3083
3084                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3085                                   AFS_TRACE_LEVEL_WARNING,
3086                                   "AFSVerifyEntry Fcb NULL %wZ FID %08lX-%08lX-%08lX-%08lX\n",
3087                                   &DirEntry->NameInformation.FileName,
3088                                   pObjectInfo->FileId.Cell,
3089                                   pObjectInfo->FileId.Volume,
3090                                   pObjectInfo->FileId.Vnode,
3091                                   pObjectInfo->FileId.Unique);
3092                 }
3093
3094                 ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
3095
3096                 break;
3097             }
3098
3099             case AFS_FILE_TYPE_DIRECTORY:
3100             {
3101
3102                 //
3103                 // For a directory or root entry flush the content of
3104                 // the directory enumeration.
3105                 //
3106
3107                 if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
3108                 {
3109
3110                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3111                                   AFS_TRACE_LEVEL_VERBOSE_2,
3112                                   "AFSVerifyEntry Validating directory content for entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
3113                                   &DirEntry->NameInformation.FileName,
3114                                   pObjectInfo->FileId.Cell,
3115                                   pObjectInfo->FileId.Volume,
3116                                   pObjectInfo->FileId.Vnode,
3117                                   pObjectInfo->FileId.Unique);
3118
3119                     AFSAcquireExcl( pObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
3120                                     TRUE);
3121
3122                     ntStatus = AFSValidateDirectoryCache( pObjectInfo,
3123                                                           AuthGroup);
3124
3125                     AFSReleaseResource( pObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
3126
3127                     if ( !NT_SUCCESS( ntStatus))
3128                     {
3129
3130                         try_return( ntStatus);
3131                     }
3132                 }
3133
3134                 //
3135                 // Update the metadata for the entry
3136                 //
3137
3138                 ntStatus = AFSUpdateMetaData( DirEntry,
3139                                               pDirEnumEntry);
3140
3141                 if( NT_SUCCESS( ntStatus))
3142                 {
3143
3144                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
3145                 }
3146
3147                 break;
3148             }
3149
3150             case AFS_FILE_TYPE_DFSLINK:
3151             {
3152
3153                 UNICODE_STRING uniTargetName;
3154
3155                 //
3156                 // For a DFS link need to check the target name has not changed
3157                 //
3158
3159                 uniTargetName.Length = (USHORT)pDirEnumEntry->TargetNameLength;
3160
3161                 uniTargetName.MaximumLength = uniTargetName.Length;
3162
3163                 uniTargetName.Buffer = (WCHAR *)((char *)pDirEnumEntry + pDirEnumEntry->TargetNameOffset);
3164
3165                 AFSAcquireExcl( &DirEntry->NonPaged->Lock,
3166                                 TRUE);
3167
3168                 if( DirEntry->NameInformation.TargetName.Length == 0 ||
3169                     RtlCompareUnicodeString( &uniTargetName,
3170                                              &DirEntry->NameInformation.TargetName,
3171                                              TRUE) != 0)
3172                 {
3173
3174                     //
3175                     // Update the target name
3176                     //
3177
3178                     ntStatus = AFSUpdateTargetName( &DirEntry->NameInformation.TargetName,
3179                                                     &DirEntry->Flags,
3180                                                     uniTargetName.Buffer,
3181                                                     uniTargetName.Length);
3182
3183                     if( !NT_SUCCESS( ntStatus))
3184                     {
3185
3186                         AFSReleaseResource( &DirEntry->NonPaged->Lock);
3187
3188                         break;
3189                     }
3190                 }
3191
3192                 AFSReleaseResource( &DirEntry->NonPaged->Lock);
3193
3194                 //
3195                 // Update the metadata for the entry
3196                 //
3197
3198                 ntStatus = AFSUpdateMetaData( DirEntry,
3199                                               pDirEnumEntry);
3200
3201                 if( NT_SUCCESS( ntStatus))
3202                 {
3203
3204                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
3205                 }
3206
3207                 break;
3208             }
3209
3210             default:
3211
3212                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3213                               AFS_TRACE_LEVEL_WARNING,
3214                               "AFSVerifyEntry Attempt to verify node of type %d %wZ FID %08lX-%08lX-%08lX-%08lX\n",
3215                               pObjectInfo->FileType,
3216                               &DirEntry->NameInformation.FileName,
3217                               pObjectInfo->FileId.Cell,
3218                               pObjectInfo->FileId.Volume,
3219                               pObjectInfo->FileId.Vnode,
3220                               pObjectInfo->FileId.Unique);
3221
3222                 break;
3223         }
3224
3225  try_exit:
3226
3227         if( pDirEnumEntry != NULL)
3228         {
3229
3230             AFSExFreePoolWithTag( pDirEnumEntry, AFS_GENERIC_MEMORY_2_TAG);
3231         }
3232     }
3233
3234     return ntStatus;
3235 }
3236
3237 NTSTATUS
3238 AFSSetVolumeState( IN AFSVolumeStatusCB *VolumeStatus)
3239 {