0620b6a4cbb83fc60fca7ad0611bdbe05aec3e15
[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     AFSObjectInfoCB * pParentObjectInfo = NULL;
1595
1596     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1597                   AFS_TRACE_LEVEL_VERBOSE,
1598                   "AFSInvalidateObject Invalidation on node type %d for fid %08lX-%08lX-%08lX-%08lX Reason %d\n",
1599                   (*ppObjectInfo)->FileType,
1600                   (*ppObjectInfo)->FileId.Cell,
1601                   (*ppObjectInfo)->FileId.Volume,
1602                   (*ppObjectInfo)->FileId.Vnode,
1603                   (*ppObjectInfo)->FileId.Unique,
1604                   Reason);
1605
1606     if ( BooleanFlagOn( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
1607     {
1608
1609         pParentObjectInfo = AFSFindObjectInfo( (*ppObjectInfo)->VolumeCB,
1610                                                &(*ppObjectInfo)->ParentFileId);
1611     }
1612
1613     if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_SYMLINK ||
1614         (*ppObjectInfo)->FileType == AFS_FILE_TYPE_DFSLINK ||
1615         (*ppObjectInfo)->FileType == AFS_FILE_TYPE_MOUNTPOINT)
1616     {
1617         //
1618         // We only act on the mount point itself, not the target. If the
1619         // node has been deleted then mark it as such otherwise indicate
1620         // it requires verification
1621         //
1622
1623         if( Reason == AFS_INVALIDATE_DELETED)
1624         {
1625             SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID);
1626         }
1627         else
1628         {
1629
1630             if( Reason == AFS_INVALIDATE_FLUSHED)
1631             {
1632
1633                 (*ppObjectInfo)->DataVersion.QuadPart = (ULONGLONG)-1;
1634
1635                 SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA);
1636             }
1637
1638             (*ppObjectInfo)->Expiration.QuadPart = 0;
1639
1640             (*ppObjectInfo)->TargetFileId.Vnode = 0;
1641
1642             (*ppObjectInfo)->TargetFileId.Unique = 0;
1643
1644             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1645                           AFS_TRACE_LEVEL_VERBOSE,
1646                           "AFSInvalidateObject Setting VERIFY flag on fid %08lX-%08lX-%08lX-%08lX\n",
1647                           (*ppObjectInfo)->FileId.Cell,
1648                           (*ppObjectInfo)->FileId.Volume,
1649                           (*ppObjectInfo)->FileId.Vnode,
1650                           (*ppObjectInfo)->FileId.Unique);
1651
1652             SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_VERIFY);
1653         }
1654
1655         if ( pParentObjectInfo != NULL)
1656         {
1657
1658             ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
1659
1660             if( Reason == AFS_INVALIDATE_CREDS)
1661             {
1662                 ulFilter |= FILE_NOTIFY_CHANGE_SECURITY;
1663             }
1664
1665             if( Reason == AFS_INVALIDATE_DATA_VERSION ||
1666                 Reason == AFS_INVALIDATE_FLUSHED)
1667             {
1668                 ulFilter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
1669             }
1670             else
1671             {
1672                 ulFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
1673             }
1674
1675             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1676                                             NULL,
1677                                             ulFilter,
1678                                             FILE_ACTION_MODIFIED);
1679         }
1680
1681         try_return( ntStatus);
1682     }
1683
1684     //
1685     // Depending on the reason for invalidation then perform work on the node
1686     //
1687
1688     switch( Reason)
1689     {
1690
1691     case AFS_INVALIDATE_DELETED:
1692         {
1693
1694             //
1695             // Mark this node as invalid
1696             //
1697
1698             (*ppObjectInfo)->Links = 0;
1699
1700             SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_DELETED);
1701
1702             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1703                           AFS_TRACE_LEVEL_VERBOSE,
1704                           "AFSInvalidateObject Set DELETE flag on fid %08lX-%08lX-%08lX-%08lX\n",
1705                           (*ppObjectInfo)->FileId.Cell,
1706                           (*ppObjectInfo)->FileId.Volume,
1707                           (*ppObjectInfo)->FileId.Vnode,
1708                           (*ppObjectInfo)->FileId.Unique);
1709
1710             if( pParentObjectInfo != NULL)
1711             {
1712
1713                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1714                               AFS_TRACE_LEVEL_VERBOSE,
1715                               "AFSInvalidateObject Set VERIFY flag on parent fid %08lX-%08lX-%08lX-%08lX\n",
1716                               pParentObjectInfo->FileId.Cell,
1717                               pParentObjectInfo->FileId.Volume,
1718                               pParentObjectInfo->FileId.Vnode,
1719                               pParentObjectInfo->FileId.Unique);
1720
1721                 SetFlag( pParentObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
1722
1723                 pParentObjectInfo->DataVersion.QuadPart = (ULONGLONG)-1;
1724
1725                 pParentObjectInfo->Expiration.QuadPart = 0;
1726
1727                 if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_DIRECTORY)
1728                 {
1729                     ulFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
1730                 }
1731                 else
1732                 {
1733                     ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
1734                 }
1735
1736                 AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1737                                                 NULL,
1738                                                 ulFilter,
1739                                                 FILE_ACTION_REMOVED);
1740             }
1741
1742             if( NT_SUCCESS( AFSQueueInvalidateObject( (*ppObjectInfo),
1743                                                       Reason)))
1744             {
1745                 (*ppObjectInfo) = NULL; // We'll dec the count in the worker item
1746             }
1747
1748             break;
1749         }
1750
1751     case AFS_INVALIDATE_FLUSHED:
1752         {
1753
1754             if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_FILE &&
1755                 (*ppObjectInfo)->Fcb != NULL)
1756             {
1757
1758                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1759                               AFS_TRACE_LEVEL_VERBOSE,
1760                               "AFSInvalidateObject Flush/purge file fid %08lX-%08lX-%08lX-%08lX\n",
1761                               (*ppObjectInfo)->FileId.Cell,
1762                               (*ppObjectInfo)->FileId.Volume,
1763                               (*ppObjectInfo)->FileId.Vnode,
1764                               (*ppObjectInfo)->FileId.Unique);
1765
1766                 AFSAcquireExcl( &(*ppObjectInfo)->Fcb->NPFcb->SectionObjectResource,
1767                                 TRUE);
1768
1769                 __try
1770                 {
1771
1772                     CcFlushCache( &(*ppObjectInfo)->Fcb->NPFcb->SectionObjectPointers,
1773                                   NULL,
1774                                   0,
1775                                   &stIoStatus);
1776
1777                     if( !NT_SUCCESS( stIoStatus.Status))
1778                     {
1779
1780                         AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1781                                       AFS_TRACE_LEVEL_ERROR,
1782                                       "AFSInvalidateObject CcFlushCache failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1783                                       (*ppObjectInfo)->FileId.Cell,
1784                                       (*ppObjectInfo)->FileId.Volume,
1785                                       (*ppObjectInfo)->FileId.Vnode,
1786                                       (*ppObjectInfo)->FileId.Unique,
1787                                       stIoStatus.Status,
1788                                       stIoStatus.Information);
1789
1790                         ntStatus = stIoStatus.Status;
1791                     }
1792
1793
1794                     if ( (*ppObjectInfo)->Fcb->NPFcb->SectionObjectPointers.DataSectionObject != NULL)
1795                     {
1796
1797                         if ( !CcPurgeCacheSection( &(*ppObjectInfo)->Fcb->NPFcb->SectionObjectPointers,
1798                                                    NULL,
1799                                                    0,
1800                                                    FALSE))
1801                         {
1802
1803                             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1804                                           AFS_TRACE_LEVEL_WARNING,
1805                                           "AFSInvalidateObject CcPurgeCacheSection failure FID %08lX-%08lX-%08lX-%08lX\n",
1806                                           (*ppObjectInfo)->FileId.Cell,
1807                                           (*ppObjectInfo)->FileId.Volume,
1808                                           (*ppObjectInfo)->FileId.Vnode,
1809                                           (*ppObjectInfo)->FileId.Unique);
1810
1811                             SetFlag( (*ppObjectInfo)->Fcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
1812                         }
1813                     }
1814                 }
1815                 __except( EXCEPTION_EXECUTE_HANDLER)
1816                 {
1817
1818                     ntStatus = GetExceptionCode();
1819
1820                     AFSDbgLogMsg( 0,
1821                                   0,
1822                                   "EXCEPTION - AFSInvalidateObject Cc FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
1823                                   (*ppObjectInfo)->FileId.Cell,
1824                                   (*ppObjectInfo)->FileId.Volume,
1825                                   (*ppObjectInfo)->FileId.Vnode,
1826                                   (*ppObjectInfo)->FileId.Unique,
1827                                   ntStatus);
1828
1829                     SetFlag( (*ppObjectInfo)->Fcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
1830                 }
1831
1832                 AFSReleaseResource( &(*ppObjectInfo)->Fcb->NPFcb->SectionObjectResource);
1833
1834                 //
1835                 // Clear out the extents
1836                 // Get rid of them (note this involves waiting
1837                 // for any writes or reads to the cache to complete)
1838                 //
1839
1840                 AFSTearDownFcbExtents( (*ppObjectInfo)->Fcb,
1841                                        NULL);
1842             }
1843
1844             (*ppObjectInfo)->DataVersion.QuadPart = (ULONGLONG)-1;
1845
1846
1847             if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_FILE)
1848             {
1849
1850                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1851                               AFS_TRACE_LEVEL_VERBOSE,
1852                               "AFSInvalidateObject Setting VERIFY_DATA flag on fid %08lX-%08lX-%08lX-%08lX\n",
1853                               (*ppObjectInfo)->FileId.Cell,
1854                               (*ppObjectInfo)->FileId.Volume,
1855                               (*ppObjectInfo)->FileId.Vnode,
1856                               (*ppObjectInfo)->FileId.Unique);
1857
1858                 SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA);
1859             }
1860
1861             // Fall through to the default processing
1862         }
1863
1864     default:
1865         {
1866
1867             if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_DIRECTORY)
1868             {
1869                 ulFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
1870             }
1871             else
1872             {
1873                 ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
1874             }
1875
1876             if( Reason == AFS_INVALIDATE_CREDS)
1877             {
1878                 ulFilter |= FILE_NOTIFY_CHANGE_SECURITY;
1879             }
1880
1881             if( Reason == AFS_INVALIDATE_DATA_VERSION)
1882             {
1883                 ulFilter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
1884             }
1885             else
1886             {
1887                 ulFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
1888             }
1889
1890             if( (*ppObjectInfo)->FileType == AFS_FILE_TYPE_DIRECTORY)
1891             {
1892
1893                 AFSFsRtlNotifyFullReportChange( (*ppObjectInfo),
1894                                                 NULL,
1895                                                 ulFilter,
1896                                                 FILE_ACTION_MODIFIED);
1897             }
1898             else if ( pParentObjectInfo != NULL)
1899             {
1900
1901                 AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1902                                                 NULL,
1903                                                 ulFilter,
1904                                                 FILE_ACTION_MODIFIED);
1905             }
1906
1907             //
1908             // Indicate this node requires re-evaluation for the remaining reasons
1909             //
1910
1911             (*ppObjectInfo)->Expiration.QuadPart = 0;
1912
1913             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1914                           AFS_TRACE_LEVEL_VERBOSE,
1915                           "AFSInvalidateObject Setting VERIFY flag on fid %08lX-%08lX-%08lX-%08lX\n",
1916                           (*ppObjectInfo)->FileId.Cell,
1917                           (*ppObjectInfo)->FileId.Volume,
1918                           (*ppObjectInfo)->FileId.Vnode,
1919                           (*ppObjectInfo)->FileId.Unique);
1920
1921             SetFlag( (*ppObjectInfo)->Flags, AFS_OBJECT_FLAGS_VERIFY);
1922
1923             if( Reason == AFS_INVALIDATE_DATA_VERSION ||
1924                 (*ppObjectInfo)->FileType == AFS_FILE_TYPE_FILE &&
1925                 ( Reason == AFS_INVALIDATE_CALLBACK ||
1926                   Reason == AFS_INVALIDATE_EXPIRED))
1927             {
1928                 if ( NT_SUCCESS( AFSQueueInvalidateObject( (*ppObjectInfo),
1929                                                            AFS_INVALIDATE_DATA_VERSION)))
1930                 {
1931
1932                     (*ppObjectInfo) = NULL; // We'll dec the count in the worker item
1933                 }
1934             }
1935
1936             break;
1937         }
1938     }
1939
1940   try_exit:
1941
1942     if ( pParentObjectInfo != NULL)
1943     {
1944
1945         AFSReleaseObjectInfo( &pParentObjectInfo);
1946     }
1947
1948     return ntStatus;
1949 }
1950
1951 NTSTATUS
1952 AFSInvalidateCache( IN AFSInvalidateCacheCB *InvalidateCB)
1953 {
1954
1955     NTSTATUS ntStatus = STATUS_SUCCESS;
1956     AFSVolumeCB *pVolumeCB = NULL;
1957     AFSDeviceExt *pDevExt = (AFSDeviceExt *) AFSRDRDeviceObject->DeviceExtension;
1958     ULONGLONG   ullIndex = 0;
1959     AFSObjectInfoCB *pObjectInfo = NULL;
1960     LONG lCount;
1961
1962     __Enter
1963     {
1964
1965         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1966                       AFS_TRACE_LEVEL_VERBOSE,
1967                       "AFSInvalidateCache Invalidation FID %08lX-%08lX-%08lX-%08lX Type %d WholeVolume %d Reason %d\n",
1968                       InvalidateCB->FileID.Cell,
1969                       InvalidateCB->FileID.Volume,
1970                       InvalidateCB->FileID.Vnode,
1971                       InvalidateCB->FileID.Unique,
1972                       InvalidateCB->FileType,
1973                       InvalidateCB->WholeVolume,
1974                       InvalidateCB->Reason);
1975
1976         //
1977         // Need to locate the Fcb for the directory to purge
1978         //
1979
1980         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1981                       AFS_TRACE_LEVEL_VERBOSE,
1982                       "AFSInvalidateCache Acquiring RDR VolumeTreeLock lock %p SHARED %08lX\n",
1983                       &pDevExt->Specific.RDR.VolumeTreeLock,
1984                       PsGetCurrentThread());
1985
1986         //
1987         // Starve any exclusive waiters on this paticular call
1988         //
1989
1990         AFSAcquireSharedStarveExclusive( &pDevExt->Specific.RDR.VolumeTreeLock, TRUE);
1991
1992         //
1993         // Locate the volume node
1994         //
1995
1996         ullIndex = AFSCreateHighIndex( &InvalidateCB->FileID);
1997
1998         ntStatus = AFSLocateHashEntry( pDevExt->Specific.RDR.VolumeTree.TreeHead,
1999                                        ullIndex,
2000                                        (AFSBTreeEntry **)&pVolumeCB);
2001
2002         if( pVolumeCB != NULL)
2003         {
2004
2005             lCount = AFSVolumeIncrement( pVolumeCB,
2006                                          AFS_VOLUME_REFERENCE_INVALIDATE);
2007
2008             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2009                           AFS_TRACE_LEVEL_VERBOSE,
2010                           "AFSInvalidateCache Increment count on volume %p Cnt %d\n",
2011                           pVolumeCB,
2012                           lCount);
2013         }
2014
2015         AFSReleaseResource( &pDevExt->Specific.RDR.VolumeTreeLock);
2016
2017         if( !NT_SUCCESS( ntStatus) ||
2018             pVolumeCB == NULL)
2019         {
2020
2021             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2022                           AFS_TRACE_LEVEL_WARNING,
2023                           "AFSInvalidateCache Invalidation FAILURE Unable to locate volume node FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
2024                           InvalidateCB->FileID.Cell,
2025                           InvalidateCB->FileID.Volume,
2026                           InvalidateCB->FileID.Vnode,
2027                           InvalidateCB->FileID.Unique,
2028                           ntStatus);
2029
2030             try_return( ntStatus = STATUS_SUCCESS);
2031         }
2032
2033         //
2034         // If this is a whole volume invalidation then go do it now
2035         //
2036
2037         if( InvalidateCB->WholeVolume)
2038         {
2039
2040             ntStatus = AFSInvalidateVolume( pVolumeCB,
2041                                             InvalidateCB->Reason);
2042
2043             try_return( ntStatus);
2044         }
2045
2046         AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
2047                           TRUE);
2048
2049         if ( AFSIsVolumeFID( &InvalidateCB->FileID))
2050         {
2051
2052             pObjectInfo = &pVolumeCB->ObjectInformation;
2053         }
2054         else
2055         {
2056
2057             ullIndex = AFSCreateLowIndex( &InvalidateCB->FileID);
2058
2059             ntStatus = AFSLocateHashEntry( pVolumeCB->ObjectInfoTree.TreeHead,
2060                                            ullIndex,
2061                                            (AFSBTreeEntry **)&pObjectInfo);
2062         }
2063
2064         if( pObjectInfo != NULL)
2065         {
2066
2067             //
2068             // Reference the node so it won't be torn down
2069             //
2070
2071             lCount = AFSObjectInfoIncrement( pObjectInfo,
2072                                              AFS_OBJECT_REFERENCE_INVALIDATION);
2073
2074             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2075                           AFS_TRACE_LEVEL_VERBOSE,
2076                           "AFSInvalidateCache Increment count on object %p Cnt %d\n",
2077                           pObjectInfo,
2078                           lCount);
2079         }
2080
2081         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
2082
2083         if( !NT_SUCCESS( ntStatus) ||
2084             pObjectInfo == NULL)
2085         {
2086
2087             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2088                           AFS_TRACE_LEVEL_VERBOSE,
2089                           "AFSInvalidateCache Invalidation FAILURE Unable to locate object FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
2090                           InvalidateCB->FileID.Cell,
2091                           InvalidateCB->FileID.Volume,
2092                           InvalidateCB->FileID.Vnode,
2093                           InvalidateCB->FileID.Unique,
2094                           ntStatus);
2095
2096             try_return( ntStatus = STATUS_SUCCESS);
2097         }
2098
2099         AFSInvalidateObject( &pObjectInfo,
2100                              InvalidateCB->Reason);
2101
2102 try_exit:
2103
2104         if( pObjectInfo != NULL)
2105         {
2106
2107             lCount = AFSObjectInfoDecrement( pObjectInfo,
2108                                              AFS_OBJECT_REFERENCE_INVALIDATION);
2109
2110             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2111                           AFS_TRACE_LEVEL_VERBOSE,
2112                           "AFSInvalidateCache Decrement count on object %p Cnt %d\n",
2113                           pObjectInfo,
2114                           lCount);
2115         }
2116
2117         if ( pVolumeCB != NULL)
2118         {
2119
2120             lCount = AFSVolumeDecrement( pVolumeCB,
2121                                          AFS_VOLUME_REFERENCE_INVALIDATE);
2122
2123             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2124                           AFS_TRACE_LEVEL_VERBOSE,
2125                           "AFSInvalidateCache Decrement count on volume %p Cnt %d\n",
2126                           pVolumeCB,
2127                           lCount);
2128         }
2129     }
2130
2131     return ntStatus;
2132 }
2133
2134 BOOLEAN
2135 AFSIsChildOfParent( IN AFSFcb *Dcb,
2136                     IN AFSFcb *Fcb)
2137 {
2138
2139     BOOLEAN bIsChild = FALSE;
2140     AFSFcb *pCurrentFcb = Fcb;
2141     AFSObjectInfoCB * pParentObjectInfo = NULL;
2142
2143     while( pCurrentFcb != NULL)
2144     {
2145
2146         if( BooleanFlagOn( pCurrentFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
2147             AFSIsEqualFID( &pCurrentFcb->ObjectInformation->ParentFileId, &Dcb->ObjectInformation->FileId))
2148         {
2149
2150             bIsChild = TRUE;
2151
2152             break;
2153         }
2154
2155         pParentObjectInfo = AFSFindObjectInfo( pCurrentFcb->ObjectInformation->VolumeCB,
2156                                                &pCurrentFcb->ObjectInformation->ParentFileId);
2157
2158         if ( pParentObjectInfo != NULL)
2159         {
2160
2161             pCurrentFcb = pParentObjectInfo->Fcb;
2162
2163             AFSReleaseObjectInfo( &pParentObjectInfo);
2164         }
2165         else
2166         {
2167
2168             pCurrentFcb = NULL;
2169         }
2170     }
2171
2172     return bIsChild;
2173 }
2174
2175 inline
2176 ULONGLONG
2177 AFSCreateHighIndex( IN AFSFileID *FileID)
2178 {
2179
2180     ULONGLONG ullIndex = 0;
2181
2182     ullIndex = (((ULONGLONG)FileID->Cell << 32) | FileID->Volume);
2183
2184     return ullIndex;
2185 }
2186
2187 inline
2188 ULONGLONG
2189 AFSCreateLowIndex( IN AFSFileID *FileID)
2190 {
2191
2192     ULONGLONG ullIndex = 0;
2193
2194     ullIndex = (((ULONGLONG)FileID->Vnode << 32) | FileID->Unique);
2195
2196     return ullIndex;
2197 }
2198
2199 BOOLEAN
2200 AFSCheckAccess( IN ACCESS_MASK DesiredAccess,
2201                 IN ACCESS_MASK GrantedAccess,
2202                 IN BOOLEAN DirectoryEntry)
2203 {
2204
2205     BOOLEAN bAccessGranted = TRUE;
2206
2207     //
2208     // Check if we are asking for read/write and granted only read only
2209     // NOTE: There will be more checks here
2210     //
2211
2212     if( !AFSCheckForReadOnlyAccess( DesiredAccess,
2213                                     DirectoryEntry) &&
2214         AFSCheckForReadOnlyAccess( GrantedAccess,
2215                                    DirectoryEntry))
2216     {
2217
2218         bAccessGranted = FALSE;
2219     }
2220
2221     return bAccessGranted;
2222 }
2223
2224 NTSTATUS
2225 AFSGetDriverStatus( IN AFSDriverStatusRespCB *DriverStatus)
2226 {
2227
2228     NTSTATUS         ntStatus = STATUS_SUCCESS;
2229     AFSDeviceExt    *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
2230
2231     //
2232     // Start with read
2233     //
2234
2235     DriverStatus->Status = AFS_DRIVER_STATUS_READY;
2236
2237     if( AFSGlobalRoot == NULL)
2238     {
2239
2240         //
2241         // We are not ready
2242         //
2243
2244         DriverStatus->Status = AFS_DRIVER_STATUS_NOT_READY;
2245     }
2246
2247     if( pControlDevExt->Specific.Control.CommServiceCB.IrpPoolControlFlag != POOL_ACTIVE)
2248     {
2249
2250         //
2251         // No service yet
2252         //
2253
2254         DriverStatus->Status = AFS_DRIVER_STATUS_NO_SERVICE;
2255     }
2256
2257     return ntStatus;
2258 }
2259
2260 NTSTATUS
2261 AFSSubstituteSysName( IN UNICODE_STRING *ComponentName,
2262                       IN UNICODE_STRING *SubstituteName,
2263                       IN ULONG StringIndex)
2264 {
2265
2266     NTSTATUS ntStatus = STATUS_SUCCESS;
2267     AFSDeviceExt    *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
2268     AFSSysNameCB    *pSysName = NULL;
2269     ERESOURCE       *pSysNameLock = NULL;
2270     ULONG            ulIndex = 1;
2271     USHORT           usIndex = 0;
2272     UNICODE_STRING   uniSysName;
2273
2274     __Enter
2275     {
2276
2277 #if defined(_WIN64)
2278
2279         if( IoIs32bitProcess( NULL))
2280         {
2281
2282             pSysNameLock = &pControlDevExt->Specific.Control.SysName32ListLock;
2283
2284             pSysName = pControlDevExt->Specific.Control.SysName32ListHead;
2285         }
2286         else
2287         {
2288
2289             pSysNameLock = &pControlDevExt->Specific.Control.SysName64ListLock;
2290
2291             pSysName = pControlDevExt->Specific.Control.SysName64ListHead;
2292         }
2293 #else
2294
2295         pSysNameLock = &pControlDevExt->Specific.Control.SysName32ListLock;
2296
2297         pSysName = pControlDevExt->Specific.Control.SysName32ListHead;
2298
2299 #endif
2300
2301         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2302                       AFS_TRACE_LEVEL_VERBOSE,
2303                       "AFSSubstituteSysName Acquiring SysName lock %p SHARED %08lX\n",
2304                       pSysNameLock,
2305                       PsGetCurrentThread());
2306
2307         AFSAcquireShared( pSysNameLock,
2308                           TRUE);
2309
2310         //
2311         // Find where we are in the list
2312         //
2313
2314         while( pSysName != NULL &&
2315             ulIndex < StringIndex)
2316         {
2317
2318             pSysName = pSysName->fLink;
2319
2320             ulIndex++;
2321         }
2322
2323         if( pSysName == NULL)
2324         {
2325
2326             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2327                           AFS_TRACE_LEVEL_VERBOSE_2,
2328                           "AFSSubstituteSysName No sysname %wZ Status %08lX\n",
2329                           &ComponentName,
2330                           STATUS_OBJECT_NAME_NOT_FOUND);
2331
2332             try_return( ntStatus = STATUS_OBJECT_NAME_NOT_FOUND);
2333         }
2334
2335         RtlInitUnicodeString( &uniSysName,
2336                               L"@SYS");
2337         //
2338         // If it is a full component of @SYS then just substitue the
2339         // name in
2340         //
2341
2342         if( RtlCompareUnicodeString( &uniSysName,
2343                                      ComponentName,
2344                                      TRUE) == 0)
2345         {
2346
2347             SubstituteName->Length = pSysName->SysName.Length;
2348             SubstituteName->MaximumLength = SubstituteName->Length;
2349
2350             SubstituteName->Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2351                                                                         SubstituteName->Length,
2352                                                                         AFS_SUBST_BUFFER_TAG);
2353
2354             if( SubstituteName->Buffer == NULL)
2355             {
2356
2357                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2358             }
2359
2360             RtlCopyMemory( SubstituteName->Buffer,
2361                            pSysName->SysName.Buffer,
2362                            pSysName->SysName.Length);
2363         }
2364         else
2365         {
2366
2367             usIndex = 0;
2368
2369             while( ComponentName->Buffer[ usIndex] != L'@')
2370             {
2371
2372                 usIndex++;
2373             }
2374
2375             SubstituteName->Length = (usIndex * sizeof( WCHAR)) + pSysName->SysName.Length;
2376             SubstituteName->MaximumLength = SubstituteName->Length;
2377
2378             SubstituteName->Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2379                                                                         SubstituteName->Length,
2380                                                                         AFS_SUBST_BUFFER_TAG);
2381
2382             if( SubstituteName->Buffer == NULL)
2383             {
2384
2385                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2386             }
2387
2388             RtlCopyMemory( SubstituteName->Buffer,
2389                            ComponentName->Buffer,
2390                            usIndex * sizeof( WCHAR));
2391
2392             RtlCopyMemory( &SubstituteName->Buffer[ usIndex],
2393                            pSysName->SysName.Buffer,
2394                            pSysName->SysName.Length);
2395         }
2396
2397 try_exit:
2398
2399         AFSReleaseResource( pSysNameLock);
2400     }
2401
2402     return ntStatus;
2403 }
2404
2405 NTSTATUS
2406 AFSSubstituteNameInPath( IN OUT UNICODE_STRING *FullPathName,
2407                          IN OUT UNICODE_STRING *ComponentName,
2408                          IN UNICODE_STRING *SubstituteName,
2409                          IN OUT UNICODE_STRING *RemainingPath,
2410                          IN BOOLEAN FreePathName)
2411 {
2412
2413     NTSTATUS ntStatus = STATUS_SUCCESS;
2414     UNICODE_STRING uniPathName;
2415     USHORT usPrefixNameLen = 0;
2416     SHORT  sNameLenDelta = 0;
2417
2418     __Enter
2419     {
2420
2421         //
2422         // If the passed in name can handle the additional length
2423         // then just moves things around
2424         //
2425
2426         sNameLenDelta = SubstituteName->Length - ComponentName->Length;
2427
2428         usPrefixNameLen = (USHORT)(ComponentName->Buffer - FullPathName->Buffer);
2429
2430         if( FullPathName->MaximumLength > FullPathName->Length + sNameLenDelta)
2431         {
2432
2433             if( FullPathName->Length > usPrefixNameLen + ComponentName->Length)
2434             {
2435
2436                 RtlMoveMemory( &FullPathName->Buffer[ ((usPrefixNameLen*sizeof( WCHAR) + SubstituteName->Length)/sizeof( WCHAR))],
2437                                &FullPathName->Buffer[ ((usPrefixNameLen*sizeof( WCHAR) + ComponentName->Length)/sizeof( WCHAR))],
2438                                FullPathName->Length - usPrefixNameLen*sizeof( WCHAR) - ComponentName->Length);
2439             }
2440
2441             RtlCopyMemory( &FullPathName->Buffer[ usPrefixNameLen],
2442                            SubstituteName->Buffer,
2443                            SubstituteName->Length);
2444
2445             FullPathName->Length += sNameLenDelta;
2446
2447             ComponentName->Length += sNameLenDelta;
2448
2449             ComponentName->MaximumLength = ComponentName->Length;
2450
2451             if ( RemainingPath->Buffer)
2452             {
2453
2454                 RemainingPath->Buffer += sNameLenDelta/sizeof( WCHAR);
2455             }
2456
2457             try_return( ntStatus);
2458         }
2459
2460         //
2461         // Need to re-allocate the buffer
2462         //
2463
2464         uniPathName.Length = FullPathName->Length -
2465                                          ComponentName->Length +
2466                                          SubstituteName->Length;
2467
2468         uniPathName.MaximumLength = FullPathName->MaximumLength + PAGE_SIZE;
2469
2470         uniPathName.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2471                                                                 uniPathName.MaximumLength,
2472                                                                 AFS_NAME_BUFFER_FOUR_TAG);
2473
2474         if( uniPathName.Buffer == NULL)
2475         {
2476
2477             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2478         }
2479
2480         usPrefixNameLen = (USHORT)(ComponentName->Buffer - FullPathName->Buffer);
2481
2482         usPrefixNameLen *= sizeof( WCHAR);
2483
2484         RtlZeroMemory( uniPathName.Buffer,
2485                        uniPathName.MaximumLength);
2486
2487         RtlCopyMemory( uniPathName.Buffer,
2488                        FullPathName->Buffer,
2489                        usPrefixNameLen);
2490
2491         RtlCopyMemory( &uniPathName.Buffer[ (usPrefixNameLen/sizeof( WCHAR))],
2492                        SubstituteName->Buffer,
2493                        SubstituteName->Length);
2494
2495         if( FullPathName->Length > usPrefixNameLen + ComponentName->Length)
2496         {
2497
2498             RtlCopyMemory( &uniPathName.Buffer[ (usPrefixNameLen + SubstituteName->Length)/sizeof( WCHAR)],
2499                            &FullPathName->Buffer[ (usPrefixNameLen + ComponentName->Length)/sizeof( WCHAR)],
2500                            FullPathName->Length - usPrefixNameLen - ComponentName->Length);
2501         }
2502
2503         ComponentName->Buffer = uniPathName.Buffer + (ComponentName->Buffer - FullPathName->Buffer);
2504
2505         ComponentName->Length += sNameLenDelta;
2506
2507         ComponentName->MaximumLength = ComponentName->Length;
2508
2509         if ( RemainingPath->Buffer)
2510         {
2511
2512             RemainingPath->Buffer = uniPathName.Buffer
2513                 + (RemainingPath->Buffer - FullPathName->Buffer)
2514                 + sNameLenDelta/sizeof( WCHAR);
2515         }
2516
2517         if( FreePathName)
2518         {
2519             AFSExFreePoolWithTag( FullPathName->Buffer, 0);
2520         }
2521
2522         *FullPathName = uniPathName;
2523
2524 try_exit:
2525
2526         NOTHING;
2527     }
2528
2529     return ntStatus;
2530 }
2531
2532 NTSTATUS
2533 AFSInvalidateVolume( IN AFSVolumeCB *VolumeCB,
2534                      IN ULONG Reason)
2535 {
2536
2537     NTSTATUS ntStatus = STATUS_SUCCESS;
2538     AFSObjectInfoCB *pCurrentObject = NULL;
2539     AFSObjectInfoCB *pNextObject = NULL;
2540     LONG lCount;
2541
2542     __Enter
2543     {
2544
2545         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2546                       AFS_TRACE_LEVEL_VERBOSE,
2547                       "AFSInvalidateVolume Invalidate volume fid %08lX-%08lX-%08lX-%08lX Reason %08lX\n",
2548                       VolumeCB->ObjectInformation.FileId.Cell,
2549                       VolumeCB->ObjectInformation.FileId.Volume,
2550                       VolumeCB->ObjectInformation.FileId.Vnode,
2551                       VolumeCB->ObjectInformation.FileId.Unique,
2552                       Reason);
2553
2554         //
2555         // Depending on the reason for invalidation then perform work on the node
2556         //
2557
2558         switch( Reason)
2559         {
2560
2561             case AFS_INVALIDATE_DELETED:
2562             {
2563
2564                 //
2565                 // Mark this volume as invalid
2566                 //
2567
2568                 SetFlag( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID);
2569
2570                 SetFlag( VolumeCB->Flags, AFS_VOLUME_FLAGS_OFFLINE);
2571
2572                 break;
2573             }
2574         }
2575
2576         //
2577         // Invalidate the volume root directory
2578         //
2579
2580         pCurrentObject = &VolumeCB->ObjectInformation;
2581
2582         if ( pCurrentObject )
2583         {
2584
2585             lCount = AFSObjectInfoIncrement( pCurrentObject,
2586                                              AFS_OBJECT_REFERENCE_INVALIDATION);
2587
2588             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2589                           AFS_TRACE_LEVEL_VERBOSE,
2590                           "AFSInvalidateVolumeObjects Increment count on object %p Cnt %d\n",
2591                           pCurrentObject,
2592                           lCount);
2593
2594             AFSInvalidateObject( &pCurrentObject,
2595                                  Reason);
2596
2597             if ( pCurrentObject)
2598             {
2599
2600                 lCount = AFSObjectInfoDecrement( pCurrentObject,
2601                                                  AFS_OBJECT_REFERENCE_INVALIDATION);
2602
2603                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2604                               AFS_TRACE_LEVEL_VERBOSE,
2605                               "AFSInvalidateVolumeObjects Decrement count on object %p Cnt %d\n",
2606                               pCurrentObject,
2607                               lCount);
2608             }
2609         }
2610
2611         //
2612         // Apply invalidation to all other volume objects
2613         //
2614
2615         AFSAcquireShared( VolumeCB->ObjectInfoTree.TreeLock,
2616                           TRUE);
2617
2618         pCurrentObject = VolumeCB->ObjectInfoListHead;
2619
2620         if ( pCurrentObject)
2621         {
2622
2623             //
2624             // Reference the node so it won't be torn down
2625             //
2626
2627             lCount = AFSObjectInfoIncrement( pCurrentObject,
2628                                              AFS_OBJECT_REFERENCE_INVALIDATION);
2629
2630             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2631                           AFS_TRACE_LEVEL_VERBOSE,
2632                           "AFSInvalidateVolumeObjects Increment count on object %p Cnt %d\n",
2633                           pCurrentObject,
2634                           lCount);
2635         }
2636
2637         while( pCurrentObject != NULL)
2638         {
2639
2640             pNextObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
2641
2642             if ( pNextObject)
2643             {
2644
2645                 //
2646                 // Reference the node so it won't be torn down
2647                 //
2648
2649                 lCount = AFSObjectInfoIncrement( pNextObject,
2650                                                  AFS_OBJECT_REFERENCE_INVALIDATION);
2651
2652                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2653                               AFS_TRACE_LEVEL_VERBOSE,
2654                               "AFSInvalidateVolumeObjects Increment count on object %p Cnt %d\n",
2655                               pNextObject,
2656                               lCount);
2657             }
2658
2659             AFSReleaseResource( VolumeCB->ObjectInfoTree.TreeLock);
2660
2661             AFSInvalidateObject( &pCurrentObject,
2662                                  Reason);
2663
2664             if ( pCurrentObject )
2665             {
2666
2667                 lCount = AFSObjectInfoDecrement( pCurrentObject,
2668                                                  AFS_OBJECT_REFERENCE_INVALIDATION);
2669
2670                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2671                               AFS_TRACE_LEVEL_VERBOSE,
2672                               "AFSInvalidateVolumeObjects Decrement count on object %p Cnt %d\n",
2673                               pCurrentObject,
2674                               lCount);
2675             }
2676
2677             AFSAcquireShared( VolumeCB->ObjectInfoTree.TreeLock,
2678                               TRUE);
2679
2680             pCurrentObject = pNextObject;
2681         }
2682
2683         AFSReleaseResource( VolumeCB->ObjectInfoTree.TreeLock);
2684     }
2685
2686     return ntStatus;
2687 }
2688
2689 VOID
2690 AFSInvalidateAllVolumes( VOID)
2691 {
2692     AFSVolumeCB *pVolumeCB = NULL;
2693     AFSVolumeCB *pNextVolumeCB = NULL;
2694     AFSDeviceExt *pRDRDeviceExt = NULL;
2695     LONG lCount;
2696
2697     pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2698
2699     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2700                   AFS_TRACE_LEVEL_VERBOSE,
2701                   "AFSInvalidateAllVolumes Acquiring RDR VolumeListLock lock %p SHARED %08lX\n",
2702                   &pRDRDeviceExt->Specific.RDR.VolumeListLock,
2703                   PsGetCurrentThread());
2704
2705     AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
2706                       TRUE);
2707
2708     pVolumeCB = pRDRDeviceExt->Specific.RDR.VolumeListHead;
2709
2710     if ( pVolumeCB)
2711     {
2712
2713         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2714                       AFS_TRACE_LEVEL_VERBOSE,
2715                       "AFSInvalidateAllVolumes Acquiring VolumeRoot ObjectInfoTree lock %p SHARED %08lX\n",
2716                       pVolumeCB->ObjectInfoTree.TreeLock,
2717                       PsGetCurrentThread());
2718
2719         lCount = AFSVolumeIncrement( pVolumeCB,
2720                                      AFS_VOLUME_REFERENCE_INVALIDATE);
2721
2722         AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2723                       AFS_TRACE_LEVEL_VERBOSE,
2724                       "AFSInvalidateAllVolumes Increment count on volume %p Cnt %d\n",
2725                       pVolumeCB,
2726                       lCount);
2727     }
2728
2729     while( pVolumeCB != NULL)
2730     {
2731
2732         pNextVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
2733
2734         if ( pNextVolumeCB)
2735         {
2736
2737             lCount = AFSVolumeIncrement( pNextVolumeCB,
2738                                          AFS_VOLUME_REFERENCE_INVALIDATE);
2739
2740             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2741                           AFS_TRACE_LEVEL_VERBOSE,
2742                           "AFSInvalidateAllVolumes Increment count on volume %p Cnt %d\n",
2743                           pVolumeCB,
2744                           lCount);
2745         }
2746
2747         AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
2748
2749         // do I need to hold the volume lock here?
2750
2751         AFSInvalidateVolume( pVolumeCB, AFS_INVALIDATE_EXPIRED);
2752
2753         AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
2754                           TRUE);
2755
2756         lCount = AFSVolumeDecrement( pVolumeCB,
2757                                      AFS_VOLUME_REFERENCE_INVALIDATE);
2758
2759         AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2760                       AFS_TRACE_LEVEL_VERBOSE,
2761                       "AFSInvalidateAllVolumes Decrement count on volume %p Cnt %d\n",
2762                       pVolumeCB,
2763                       lCount);
2764
2765         pVolumeCB = pNextVolumeCB;
2766     }
2767
2768     AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
2769 }
2770
2771 NTSTATUS
2772 AFSVerifyEntry( IN GUID *AuthGroup,
2773                 IN AFSDirectoryCB *DirEntry)
2774 {
2775
2776     NTSTATUS ntStatus = STATUS_SUCCESS;
2777     AFSDirEnumEntry *pDirEnumEntry = NULL;
2778     AFSObjectInfoCB *pObjectInfo = DirEntry->ObjectInformation;
2779     IO_STATUS_BLOCK stIoStatus;
2780
2781     __Enter
2782     {
2783
2784         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2785                       AFS_TRACE_LEVEL_VERBOSE_2,
2786                       "AFSVerifyEntry Verifying entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2787                       &DirEntry->NameInformation.FileName,
2788                       pObjectInfo->FileId.Cell,
2789                       pObjectInfo->FileId.Volume,
2790                       pObjectInfo->FileId.Vnode,
2791                       pObjectInfo->FileId.Unique);
2792
2793         ntStatus = AFSEvaluateTargetByID( pObjectInfo,
2794                                           AuthGroup,
2795                                           FALSE,
2796                                           &pDirEnumEntry);
2797
2798         if( !NT_SUCCESS( ntStatus))
2799         {
2800
2801             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2802                           AFS_TRACE_LEVEL_ERROR,
2803                           "AFSVerifyEntry Evaluate Target failed %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
2804                           &DirEntry->NameInformation.FileName,
2805                           pObjectInfo->FileId.Cell,
2806                           pObjectInfo->FileId.Volume,
2807                           pObjectInfo->FileId.Vnode,
2808                           pObjectInfo->FileId.Unique,
2809                           ntStatus);
2810
2811             try_return( ntStatus);
2812         }
2813
2814         //
2815         // Check the data version of the file
2816         //
2817
2818         if( pObjectInfo->DataVersion.QuadPart == pDirEnumEntry->DataVersion.QuadPart)
2819         {
2820             if ( !BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA))
2821             {
2822
2823                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2824                               AFS_TRACE_LEVEL_VERBOSE,
2825                               "AFSVerifyEntry No DV change %I64X for Fcb %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2826                               pObjectInfo->DataVersion.QuadPart,
2827                               &DirEntry->NameInformation.FileName,
2828                               pObjectInfo->FileId.Cell,
2829                               pObjectInfo->FileId.Volume,
2830                               pObjectInfo->FileId.Vnode,
2831                               pObjectInfo->FileId.Unique);
2832
2833                 //
2834                 // We are ok, just get out
2835                 //
2836
2837                 ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2838
2839                 try_return( ntStatus = STATUS_SUCCESS);
2840             }
2841         }
2842
2843         //
2844         // New data version so we will need to process the node based on the type
2845         //
2846
2847         switch( pDirEnumEntry->FileType)
2848         {
2849
2850             case AFS_FILE_TYPE_MOUNTPOINT:
2851             {
2852
2853                 //
2854                 // For a mount point we need to ensure the target is the same
2855                 //
2856
2857                 if( !AFSIsEqualFID( &pObjectInfo->TargetFileId,
2858                                     &pDirEnumEntry->TargetFileId))
2859                 {
2860
2861                 }
2862
2863                 //
2864                 // Update the metadata for the entry
2865                 //
2866
2867                 ntStatus = AFSUpdateMetaData( DirEntry,
2868                                               pDirEnumEntry);
2869
2870                 if( NT_SUCCESS( ntStatus))
2871                 {
2872
2873                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2874                 }
2875
2876                 break;
2877             }
2878
2879             case AFS_FILE_TYPE_SYMLINK:
2880             {
2881
2882                 //
2883                 // Update the metadata for the entry
2884                 //
2885
2886                 ntStatus = AFSUpdateMetaData( DirEntry,
2887                                               pDirEnumEntry);
2888
2889                 if( NT_SUCCESS( ntStatus))
2890                 {
2891
2892                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2893                 }
2894
2895                 break;
2896             }
2897
2898             case AFS_FILE_TYPE_FILE:
2899             {
2900                 FILE_OBJECT * pCCFileObject = NULL;
2901                 BOOLEAN bPurgeExtents = FALSE;
2902
2903                 if ( pObjectInfo->DataVersion.QuadPart != pDirEnumEntry->DataVersion.QuadPart)
2904                 {
2905
2906                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2907                                   AFS_TRACE_LEVEL_VERBOSE,
2908                                   "AFSVerifyEntry DV Change %wZ FID %08lX-%08lX-%08lX-%08lX (%08lX != %08lX)\n",
2909                                   &DirEntry->NameInformation.FileName,
2910                                   pObjectInfo->FileId.Cell,
2911                                   pObjectInfo->FileId.Volume,
2912                                   pObjectInfo->FileId.Vnode,
2913                                   pObjectInfo->FileId.Unique,
2914                                   pObjectInfo->DataVersion.LowPart,
2915                                   pDirEnumEntry->DataVersion.LowPart
2916                                   );
2917
2918                     bPurgeExtents = TRUE;
2919                 }
2920
2921                 if ( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA))
2922                 {
2923
2924                     bPurgeExtents = TRUE;
2925
2926                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2927                                   AFS_TRACE_LEVEL_VERBOSE,
2928                                   "AFSVerifyEntry Clearing VERIFY_DATA flag %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2929                                   &DirEntry->NameInformation.FileName,
2930                                   pObjectInfo->FileId.Cell,
2931                                   pObjectInfo->FileId.Volume,
2932                                   pObjectInfo->FileId.Vnode,
2933                                   pObjectInfo->FileId.Unique);
2934
2935                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA);
2936                 }
2937
2938                 if( pObjectInfo->Fcb != NULL)
2939                 {
2940
2941                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2942                                   AFS_TRACE_LEVEL_VERBOSE,
2943                                   "AFSVerifyEntry Flush/purge entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2944                                   &DirEntry->NameInformation.FileName,
2945                                   pObjectInfo->FileId.Cell,
2946                                   pObjectInfo->FileId.Volume,
2947                                   pObjectInfo->FileId.Vnode,
2948                                   pObjectInfo->FileId.Unique);
2949
2950                     AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2951                                     TRUE);
2952
2953                     __try
2954                     {
2955
2956                         CcFlushCache( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2957                                       NULL,
2958                                       0,
2959                                       &stIoStatus);
2960
2961                         if( !NT_SUCCESS( stIoStatus.Status))
2962                         {
2963
2964                             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2965                                           AFS_TRACE_LEVEL_ERROR,
2966                                           "AFSVerifyEntry CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
2967                                           &DirEntry->NameInformation.FileName,
2968                                           pObjectInfo->FileId.Cell,
2969                                           pObjectInfo->FileId.Volume,
2970                                           pObjectInfo->FileId.Vnode,
2971                                           pObjectInfo->FileId.Unique,
2972                                           stIoStatus.Status,
2973                                           stIoStatus.Information);
2974
2975                             ntStatus = stIoStatus.Status;
2976                         }
2977
2978                         if ( bPurgeExtents &&
2979                              pObjectInfo->Fcb->NPFcb->SectionObjectPointers.DataSectionObject != NULL)
2980                         {
2981
2982                             if ( !CcPurgeCacheSection( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2983                                                        NULL,
2984                                                        0,
2985                                                        FALSE))
2986                             {
2987
2988                                 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2989                                               AFS_TRACE_LEVEL_WARNING,
2990                                               "AFSVerifyEntry CcPurgeCacheSection failure %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2991                                               &DirEntry->NameInformation.FileName,
2992                                               pObjectInfo->FileId.Cell,
2993                                               pObjectInfo->FileId.Volume,
2994                                               pObjectInfo->FileId.Vnode,
2995                                               pObjectInfo->FileId.Unique);
2996
2997                                 SetFlag( pObjectInfo->Fcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
2998                             }
2999                         }
3000                     }
3001                     __except( EXCEPTION_EXECUTE_HANDLER)
3002                     {
3003                         ntStatus = GetExceptionCode();
3004
3005                         AFSDbgLogMsg( 0,
3006                                       0,
3007                                       "EXCEPTION - AFSVerifyEntry CcFlushCache or CcPurgeCacheSection %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
3008                                       &DirEntry->NameInformation.FileName,
3009                                       pObjectInfo->FileId.Cell,
3010                                       pObjectInfo->FileId.Volume,
3011                                       pObjectInfo->FileId.Vnode,
3012                                       pObjectInfo->FileId.Unique,
3013                                       ntStatus);
3014
3015                         SetFlag( pObjectInfo->Fcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
3016                     }
3017
3018                     AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
3019
3020                     if ( bPurgeExtents)
3021                     {
3022                         AFSFlushExtents( pObjectInfo->Fcb,
3023                                          AuthGroup);
3024                     }
3025
3026                     //
3027                     // Reacquire the Fcb to purge the cache
3028                     //
3029
3030                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3031                                   AFS_TRACE_LEVEL_VERBOSE,
3032                                   "AFSVerifyEntry Acquiring Fcb lock %p EXCL %08lX\n",
3033                                   &pObjectInfo->Fcb->NPFcb->Resource,
3034                                   PsGetCurrentThread());
3035
3036                     AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Resource,
3037                                     TRUE);
3038
3039                     //
3040                     // Update the metadata for the entry
3041                     //
3042
3043                     ntStatus = AFSUpdateMetaData( DirEntry,
3044                                                   pDirEnumEntry);
3045
3046                     if( !NT_SUCCESS( ntStatus))
3047                     {
3048
3049                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3050                                       AFS_TRACE_LEVEL_ERROR,
3051                                       "AFSVerifyEntry Meta Data Update failed %wZ FID %08lX-%08lX-%08lX-%08lX ntStatus %08lX\n",
3052                                       &DirEntry->NameInformation.FileName,
3053                                       pObjectInfo->FileId.Cell,
3054                                       pObjectInfo->FileId.Volume,
3055                                       pObjectInfo->FileId.Vnode,
3056                                       pObjectInfo->FileId.Unique,
3057                                       ntStatus);
3058
3059                         break;
3060                     }
3061
3062                     //
3063                     // Update file sizes
3064                     //
3065
3066                     pObjectInfo->Fcb->Header.AllocationSize.QuadPart  = pObjectInfo->AllocationSize.QuadPart;
3067                     pObjectInfo->Fcb->Header.FileSize.QuadPart        = pObjectInfo->EndOfFile.QuadPart;
3068                     pObjectInfo->Fcb->Header.ValidDataLength.QuadPart = pObjectInfo->EndOfFile.QuadPart;
3069
3070                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3071                                   AFS_TRACE_LEVEL_VERBOSE,
3072                                   "AFSVerifyEntry Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
3073                                   &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3074                                   PsGetCurrentThread());
3075
3076                     AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3077                                     TRUE);
3078
3079                     pCCFileObject = CcGetFileObjectFromSectionPtrs( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers);
3080
3081                     if ( pCCFileObject != NULL)
3082                     {
3083                         CcSetFileSizes( pCCFileObject,
3084                                         (PCC_FILE_SIZES)&pObjectInfo->Fcb->Header.AllocationSize);
3085                     }
3086
3087                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3088                                   AFS_TRACE_LEVEL_VERBOSE,
3089                                   "AFSVerifyEntry Releasing Fcb SectionObject lock %p EXCL %08lX\n",
3090                                   &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3091                                   PsGetCurrentThread());
3092
3093                     AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
3094
3095                     AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->Resource);
3096                 }
3097                 else
3098                 {
3099
3100                     //
3101                     // Update the metadata for the entry
3102                     //
3103
3104                     ntStatus = AFSUpdateMetaData( DirEntry,
3105                                                   pDirEnumEntry);
3106
3107                     if( !NT_SUCCESS( ntStatus))
3108                     {
3109
3110                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3111                                       AFS_TRACE_LEVEL_ERROR,
3112                                       "AFSVerifyEntry Meta Data Update failed %wZ FID %08lX-%08lX-%08lX-%08lX ntStatus %08lX\n",
3113                                       &DirEntry->NameInformation.FileName,
3114                                       pObjectInfo->FileId.Cell,
3115                                       pObjectInfo->FileId.Volume,
3116                                       pObjectInfo->FileId.Vnode,
3117                                       pObjectInfo->FileId.Unique,
3118                                       ntStatus);
3119
3120                         break;
3121                     }
3122
3123                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3124                                   AFS_TRACE_LEVEL_WARNING,
3125                                   "AFSVerifyEntry Fcb NULL %wZ FID %08lX-%08lX-%08lX-%08lX\n",
3126                                   &DirEntry->NameInformation.FileName,
3127                                   pObjectInfo->FileId.Cell,
3128                                   pObjectInfo->FileId.Volume,
3129                                   pObjectInfo->FileId.Vnode,
3130                                   pObjectInfo->FileId.Unique);
3131                 }
3132
3133                 ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
3134
3135                 break;
3136             }
3137
3138             case AFS_FILE_TYPE_DIRECTORY:
3139             {
3140
3141                 //
3142                 // For a directory or root entry flush the content of
3143                 // the directory enumeration.
3144                 //
3145
3146                 if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
3147                 {
3148
3149                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3150                                   AFS_TRACE_LEVEL_VERBOSE_2,
3151                                   "AFSVerifyEntry Validating directory content for entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
3152                                   &DirEntry->NameInformation.FileName,
3153                                   pObjectInfo->FileId.Cell,
3154                                   pObjectInfo->FileId.Volume,
3155                                   pObjectInfo->FileId.Vnode,
3156                                   pObjectInfo->FileId.Unique);
3157
3158                     AFSAcquireExcl( pObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
3159                                     TRUE);
3160
3161                     ntStatus = AFSValidateDirectoryCache( pObjectInfo,
3162                                                           AuthGroup);
3163
3164                     AFSReleaseResource( pObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
3165
3166                     if ( !NT_SUCCESS( ntStatus))
3167                     {
3168
3169                         try_return( ntStatus);
3170                     }
3171                 }
3172
3173                 //
3174                 // Update the metadata for the entry
3175                 //
3176
3177                 ntStatus = AFSUpdateMetaData( DirEntry,
3178                                               pDirEnumEntry);
3179
3180                 if( NT_SUCCESS( ntStatus))
3181                 {
3182
3183                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
3184                 }
3185
3186                 break;
3187             }
3188
3189             case AFS_FILE_TYPE_DFSLINK:
3190             {
3191
3192                 UNICODE_STRING uniTargetName;
3193
3194                 //
3195                 // For a DFS link need to check the target name has not changed
3196                 //
3197
3198                 uniTargetName.Length = (USHORT)pDirEnumEntry->TargetNameLength;
3199
3200                 uniTargetName.MaximumLength = uniTargetName.Length;
3201
3202                 uniTargetName.Buffer = (WCHAR *)((char *)pDirEnumEntry + pDirEnumEntry->TargetNameOffset);
3203
3204                 AFSAcquireExcl( &DirEntry->NonPaged->Lock,
3205                                 TRUE);
3206
3207                 if( DirEntry->NameInformation.TargetName.Length == 0 ||
3208                     RtlCompareUnicodeString( &uniTargetName,
3209                                              &DirEntry->NameInformation.TargetName,
3210                                              TRUE) != 0)
3211                 {
3212
3213                     //
3214                     // Update the target name
3215                     //
3216
3217                     ntStatus = AFSUpdateTargetName( &DirEntry->NameInformation.TargetName,
3218                                                     &DirEntry->Flags,
3219                                                     uniTargetName.Buffer,
3220                                                     uniTargetName.Length);
3221
3222                     if( !NT_SUCCESS( ntStatus))
3223                     {
3224
3225                         AFSReleaseResource( &DirEntry->NonPaged->Lock);
3226
3227                         break;
3228                     }
3229                 }
3230
3231                 AFSReleaseResource( &DirEntry->NonPaged->Lock);
3232
3233                 //
3234                 // Update the metadata for the entry
3235                 //
3236
3237                 ntStatus = AFSUpdateMetaData( DirEntry,
3238                                               pDirEnumEntry);
3239
3240