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