Windows: New Trace for DirEntry tracking problem
[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_FILE_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         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1132                       AFS_TRACE_LEVEL_VERBOSE,
1133                       "AFSInitDirEntry Initialized DE %p for %wZ in parent FID %08lX-%08lX-%08lX-%08lX\n",
1134                       pDirNode,
1135                       FileName,
1136                       ParentObjectInfo->FileId.Cell,
1137                       ParentObjectInfo->FileId.Volume,
1138                       ParentObjectInfo->FileId.Vnode,
1139                       ParentObjectInfo->FileId.Unique);
1140
1141         if( TargetName != NULL &&
1142             TargetName->Length > 0)
1143         {
1144
1145             pDirNode->NameInformation.TargetName.Length = TargetName->Length;
1146
1147             pDirNode->NameInformation.TargetName.MaximumLength = pDirNode->NameInformation.TargetName.Length;
1148
1149             pDirNode->NameInformation.TargetName.Buffer = (WCHAR *)((char *)pDirNode +
1150                                                                             sizeof( AFSDirectoryCB) +
1151                                                                             pDirNode->NameInformation.FileName.Length);
1152
1153             RtlCopyMemory( pDirNode->NameInformation.TargetName.Buffer,
1154                            TargetName->Buffer,
1155                            pDirNode->NameInformation.TargetName.Length);
1156         }
1157
1158         //
1159         // If we allocated the object information cb then update the information
1160         //
1161
1162         if( bAllocatedObjectCB)
1163         {
1164
1165             //
1166             // Populate the rest of the data
1167             //
1168
1169             pObjectInfoCB->FileId = DirEnumEntry->FileId;
1170
1171             pObjectInfoCB->TargetFileId = DirEnumEntry->TargetFileId;
1172
1173             pObjectInfoCB->FileType = DirEnumEntry->FileType;
1174
1175             pObjectInfoCB->CreationTime = DirEnumEntry->CreationTime;
1176
1177             pObjectInfoCB->LastAccessTime = DirEnumEntry->LastAccessTime;
1178
1179             pObjectInfoCB->LastWriteTime = DirEnumEntry->LastWriteTime;
1180
1181             pObjectInfoCB->ChangeTime = DirEnumEntry->ChangeTime;
1182
1183             pObjectInfoCB->EndOfFile = DirEnumEntry->EndOfFile;
1184
1185             pObjectInfoCB->AllocationSize = DirEnumEntry->AllocationSize;
1186
1187             pObjectInfoCB->FileAttributes = DirEnumEntry->FileAttributes;
1188
1189             if( pObjectInfoCB->FileType == AFS_FILE_TYPE_MOUNTPOINT ||
1190                 pObjectInfoCB->FileType == AFS_FILE_TYPE_SYMLINK ||
1191                 pObjectInfoCB->FileType == AFS_FILE_TYPE_DFSLINK)
1192             {
1193
1194                 pObjectInfoCB->FileAttributes |= FILE_ATTRIBUTE_REPARSE_POINT;
1195             }
1196
1197             pObjectInfoCB->EaSize = DirEnumEntry->EaSize;
1198
1199             //
1200             // Object specific information
1201             //
1202
1203             pObjectInfoCB->Links = DirEnumEntry->Links;
1204
1205             pObjectInfoCB->Expiration = DirEnumEntry->Expiration;
1206
1207             pObjectInfoCB->DataVersion = DirEnumEntry->DataVersion;
1208
1209             //
1210             // Check for the case where we have a filetype of SymLink but both the TargetFid and the
1211             // TargetName are empty. In this case set the filetype to zero so we evaluate it later in
1212             // the code
1213             //
1214
1215             if( pObjectInfoCB->FileType == AFS_FILE_TYPE_SYMLINK &&
1216                 pObjectInfoCB->TargetFileId.Vnode == 0 &&
1217                 pObjectInfoCB->TargetFileId.Unique == 0 &&
1218                 pDirNode->NameInformation.TargetName.Length == 0)
1219             {
1220
1221                 //
1222                 // This will ensure we perform a validation on the node
1223                 //
1224
1225                 pObjectInfoCB->FileType = AFS_FILE_TYPE_UNKNOWN;
1226             }
1227
1228             if( pObjectInfoCB->FileType == AFS_FILE_TYPE_UNKNOWN)
1229             {
1230
1231                 SetFlag( pObjectInfoCB->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED);
1232             }
1233         }
1234
1235 try_exit:
1236
1237         if( !NT_SUCCESS( ntStatus))
1238         {
1239
1240             if( pNonPagedDirEntry != NULL)
1241             {
1242
1243                 ExDeleteResourceLite( &pNonPagedDirEntry->Lock);
1244
1245                 AFSExFreePool( pNonPagedDirEntry);
1246             }
1247
1248             if( pDirNode != NULL)
1249             {
1250
1251                 AFSExFreePool( pDirNode);
1252
1253                 pDirNode = NULL;
1254             }
1255
1256             //
1257             // Dereference our object info block if we have one
1258             //
1259
1260             if( pObjectInfoCB != NULL)
1261             {
1262
1263                 InterlockedDecrement( &pObjectInfoCB->ObjectReferenceCount);
1264
1265                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1266                               AFS_TRACE_LEVEL_VERBOSE,
1267                               "AFSInitDirEntry Decrement count on object %08lX Cnt %d\n",
1268                               pObjectInfoCB,
1269                               pObjectInfoCB->ObjectReferenceCount);
1270
1271                 if( bAllocatedObjectCB)
1272                 {
1273
1274                     ASSERT( pObjectInfoCB->ObjectReferenceCount == 0);
1275
1276                     AFSDeleteObjectInfo( pObjectInfoCB);
1277                 }
1278             }
1279         }
1280     }
1281
1282     return pDirNode;
1283 }
1284
1285 BOOLEAN
1286 AFSCheckForReadOnlyAccess( IN ACCESS_MASK DesiredAccess,
1287                            IN BOOLEAN DirectoryEntry)
1288 {
1289
1290     BOOLEAN bReturn = TRUE;
1291     ACCESS_MASK stAccessMask = 0;
1292
1293     //
1294     // Get rid of anything we don't know about
1295     //
1296
1297     DesiredAccess = (DesiredAccess   &
1298                           ( DELETE |
1299                             READ_CONTROL |
1300                             WRITE_OWNER |
1301                             WRITE_DAC |
1302                             SYNCHRONIZE |
1303                             ACCESS_SYSTEM_SECURITY |
1304                             FILE_WRITE_DATA |
1305                             FILE_READ_EA |
1306                             FILE_WRITE_EA |
1307                             FILE_READ_ATTRIBUTES |
1308                             FILE_WRITE_ATTRIBUTES |
1309                             FILE_LIST_DIRECTORY |
1310                             FILE_TRAVERSE |
1311                             FILE_DELETE_CHILD |
1312                             FILE_APPEND_DATA));
1313
1314     //
1315     // Our 'read only' access mask. These are the accesses we will
1316     // allow for a read only file
1317     //
1318
1319     stAccessMask = DELETE |
1320                         READ_CONTROL |
1321                         WRITE_OWNER |
1322                         WRITE_DAC |
1323                         SYNCHRONIZE |
1324                         ACCESS_SYSTEM_SECURITY |
1325                         FILE_READ_DATA |
1326                         FILE_READ_EA |
1327                         FILE_WRITE_EA |
1328                         FILE_READ_ATTRIBUTES |
1329                         FILE_WRITE_ATTRIBUTES |
1330                         FILE_EXECUTE |
1331                         FILE_LIST_DIRECTORY |
1332                         FILE_TRAVERSE;
1333
1334     //
1335     // For a directory, add in the directory specific accesses
1336     //
1337
1338     if( DirectoryEntry)
1339     {
1340
1341         stAccessMask |= FILE_ADD_SUBDIRECTORY |
1342                                 FILE_ADD_FILE |
1343                                 FILE_DELETE_CHILD;
1344     }
1345
1346     if( FlagOn( DesiredAccess, ~stAccessMask))
1347     {
1348
1349         //
1350         // A write access is set ...
1351         //
1352
1353         bReturn = FALSE;
1354     }
1355
1356     return bReturn;
1357 }
1358
1359 NTSTATUS
1360 AFSEvaluateNode( IN GUID *AuthGroup,
1361                  IN AFSDirectoryCB *DirEntry)
1362 {
1363
1364     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1365     NTSTATUS ntStatus = STATUS_SUCCESS;
1366     AFSDirEnumEntry *pDirEntry = NULL;
1367     UNICODE_STRING uniTargetName;
1368
1369     __Enter
1370     {
1371
1372         ntStatus = AFSEvaluateTargetByID( DirEntry->ObjectInformation,
1373                                           AuthGroup,
1374                                           FALSE,
1375                                           &pDirEntry);
1376
1377         if( !NT_SUCCESS( ntStatus))
1378         {
1379
1380             try_return( ntStatus);
1381         }
1382
1383         DirEntry->ObjectInformation->TargetFileId = pDirEntry->TargetFileId;
1384
1385         DirEntry->ObjectInformation->Expiration = pDirEntry->Expiration;
1386
1387         DirEntry->ObjectInformation->DataVersion = pDirEntry->DataVersion;
1388
1389         DirEntry->ObjectInformation->FileType = pDirEntry->FileType;
1390
1391         DirEntry->ObjectInformation->CreationTime = pDirEntry->CreationTime;
1392
1393         DirEntry->ObjectInformation->LastAccessTime = pDirEntry->LastAccessTime;
1394
1395         DirEntry->ObjectInformation->LastWriteTime = pDirEntry->LastWriteTime;
1396
1397         DirEntry->ObjectInformation->ChangeTime = pDirEntry->ChangeTime;
1398
1399         DirEntry->ObjectInformation->EndOfFile = pDirEntry->EndOfFile;
1400
1401         DirEntry->ObjectInformation->AllocationSize = pDirEntry->AllocationSize;
1402
1403         DirEntry->ObjectInformation->FileAttributes = pDirEntry->FileAttributes;
1404
1405         if( pDirEntry->FileType == AFS_FILE_TYPE_MOUNTPOINT ||
1406             pDirEntry->FileType == AFS_FILE_TYPE_SYMLINK ||
1407             pDirEntry->FileType == AFS_FILE_TYPE_DFSLINK)
1408         {
1409
1410             DirEntry->ObjectInformation->FileAttributes |= FILE_ATTRIBUTE_REPARSE_POINT;
1411         }
1412
1413         DirEntry->ObjectInformation->EaSize = pDirEntry->EaSize;
1414
1415         DirEntry->ObjectInformation->Links = pDirEntry->Links;
1416
1417         //
1418         // If we have a target name then see if it needs updating ...
1419         //
1420
1421         if( pDirEntry->TargetNameLength > 0)
1422         {
1423
1424             //
1425             // Update the target name information if needed
1426             //
1427
1428             uniTargetName.Length = (USHORT)pDirEntry->TargetNameLength;
1429
1430             uniTargetName.MaximumLength = uniTargetName.Length;
1431
1432             uniTargetName.Buffer = (WCHAR *)((char *)pDirEntry + pDirEntry->TargetNameOffset);
1433
1434             AFSAcquireExcl( &DirEntry->NonPaged->Lock,
1435                             TRUE);
1436
1437             if( DirEntry->NameInformation.TargetName.Length == 0 ||
1438                 RtlCompareUnicodeString( &uniTargetName,
1439                                          &DirEntry->NameInformation.TargetName,
1440                                          TRUE) != 0)
1441             {
1442
1443                 //
1444                 // Update the target name
1445                 //
1446
1447                 ntStatus = AFSUpdateTargetName( &DirEntry->NameInformation.TargetName,
1448                                                 &DirEntry->Flags,
1449                                                 uniTargetName.Buffer,
1450                                                 uniTargetName.Length);
1451
1452                 if( !NT_SUCCESS( ntStatus))
1453                 {
1454
1455                     AFSReleaseResource( &DirEntry->NonPaged->Lock);
1456
1457                     try_return( ntStatus);
1458                 }
1459             }
1460
1461             AFSReleaseResource( &DirEntry->NonPaged->Lock);
1462         }
1463
1464 try_exit:
1465
1466         if( pDirEntry != NULL)
1467         {
1468
1469             AFSExFreePool( pDirEntry);
1470         }
1471     }
1472
1473     return ntStatus;
1474 }
1475
1476 NTSTATUS
1477 AFSValidateSymLink( IN GUID *AuthGroup,
1478                     IN AFSDirectoryCB *DirEntry)
1479 {
1480
1481     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1482     NTSTATUS ntStatus = STATUS_SUCCESS;
1483     AFSDirEnumEntry *pDirEntry = NULL;
1484     UNICODE_STRING uniTargetName;
1485
1486     __Enter
1487     {
1488
1489         ntStatus = AFSEvaluateTargetByID( DirEntry->ObjectInformation,
1490                                           AuthGroup,
1491                                           FALSE,
1492                                           &pDirEntry);
1493
1494         if( !NT_SUCCESS( ntStatus))
1495         {
1496
1497             try_return( ntStatus);
1498         }
1499
1500         if( pDirEntry->FileType == AFS_FILE_TYPE_UNKNOWN ||
1501             pDirEntry->FileType == AFS_FILE_TYPE_INVALID)
1502         {
1503
1504             try_return( ntStatus = STATUS_OBJECT_NAME_NOT_FOUND);
1505         }
1506
1507         DirEntry->ObjectInformation->TargetFileId = pDirEntry->TargetFileId;
1508
1509         DirEntry->ObjectInformation->Expiration = pDirEntry->Expiration;
1510
1511         DirEntry->ObjectInformation->DataVersion = pDirEntry->DataVersion;
1512
1513         //
1514         // Update the target name information if needed
1515         //
1516
1517         uniTargetName.Length = (USHORT)pDirEntry->TargetNameLength;
1518
1519         uniTargetName.MaximumLength = uniTargetName.Length;
1520
1521         uniTargetName.Buffer = (WCHAR *)((char *)pDirEntry + pDirEntry->TargetNameOffset);
1522
1523         if( uniTargetName.Length > 0)
1524         {
1525
1526             AFSAcquireExcl( &DirEntry->NonPaged->Lock,
1527                             TRUE);
1528
1529             if( DirEntry->NameInformation.TargetName.Length == 0 ||
1530                 RtlCompareUnicodeString( &uniTargetName,
1531                                          &DirEntry->NameInformation.TargetName,
1532                                          TRUE) != 0)
1533             {
1534
1535                 //
1536                 // Update the target name
1537                 //
1538
1539                 ntStatus = AFSUpdateTargetName( &DirEntry->NameInformation.TargetName,
1540                                                 &DirEntry->Flags,
1541                                                 uniTargetName.Buffer,
1542                                                 uniTargetName.Length);
1543
1544                 if( !NT_SUCCESS( ntStatus))
1545                 {
1546
1547                     AFSReleaseResource( &DirEntry->NonPaged->Lock);
1548
1549                     try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1550                 }
1551             }
1552
1553             AFSReleaseResource( &DirEntry->NonPaged->Lock);
1554         }
1555
1556         //
1557         // If the FileType is the same then nothing to do since it IS
1558         // a SymLink
1559         //
1560
1561         if( pDirEntry->FileType == DirEntry->ObjectInformation->FileType)
1562         {
1563
1564             ASSERT( pDirEntry->FileType == AFS_FILE_TYPE_SYMLINK);
1565
1566             try_return( ntStatus = STATUS_SUCCESS);
1567         }
1568
1569         DirEntry->ObjectInformation->FileType = pDirEntry->FileType;
1570
1571         DirEntry->ObjectInformation->CreationTime = pDirEntry->CreationTime;
1572
1573         DirEntry->ObjectInformation->LastAccessTime = pDirEntry->LastAccessTime;
1574
1575         DirEntry->ObjectInformation->LastWriteTime = pDirEntry->LastWriteTime;
1576
1577         DirEntry->ObjectInformation->ChangeTime = pDirEntry->ChangeTime;
1578
1579         DirEntry->ObjectInformation->EndOfFile = pDirEntry->EndOfFile;
1580
1581         DirEntry->ObjectInformation->AllocationSize = pDirEntry->AllocationSize;
1582
1583         DirEntry->ObjectInformation->FileAttributes = pDirEntry->FileAttributes;
1584
1585         if( pDirEntry->FileType == AFS_FILE_TYPE_MOUNTPOINT ||
1586             pDirEntry->FileType == AFS_FILE_TYPE_SYMLINK ||
1587             pDirEntry->FileType == AFS_FILE_TYPE_DFSLINK)
1588         {
1589
1590             DirEntry->ObjectInformation->FileAttributes |= FILE_ATTRIBUTE_REPARSE_POINT;
1591         }
1592
1593         DirEntry->ObjectInformation->EaSize = pDirEntry->EaSize;
1594
1595         DirEntry->ObjectInformation->Links = pDirEntry->Links;
1596
1597 try_exit:
1598
1599         if( pDirEntry != NULL)
1600         {
1601
1602             AFSExFreePool( pDirEntry);
1603         }
1604     }
1605
1606     return ntStatus;
1607 }
1608
1609 NTSTATUS
1610 AFSInvalidateCache( IN AFSInvalidateCacheCB *InvalidateCB)
1611 {
1612
1613     NTSTATUS ntStatus = STATUS_SUCCESS;
1614     AFSFcb      *pDcb = NULL, *pFcb = NULL, *pNextFcb = NULL;
1615     AFSVolumeCB *pVolumeCB = NULL;
1616     AFSFcb      *pTargetDcb = NULL;
1617     AFSDeviceExt *pDevExt = (AFSDeviceExt *) AFSRDRDeviceObject->DeviceExtension;
1618     AFSDirectoryCB *pCurrentDirEntry = NULL;
1619     BOOLEAN     bIsChild = FALSE;
1620     ULONGLONG   ullIndex = 0;
1621     AFSObjectInfoCB *pObjectInfo = NULL;
1622     IO_STATUS_BLOCK stIoStatus;
1623     ULONG ulFilter = 0;
1624
1625     __Enter
1626     {
1627
1628         //
1629         // Need to locate the Fcb for the directory to purge
1630         //
1631
1632         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1633                       AFS_TRACE_LEVEL_VERBOSE,
1634                       "AFSInvalidateCache Acquiring RDR VolumeTreeLock lock %08lX SHARED %08lX\n",
1635                       &pDevExt->Specific.RDR.VolumeTreeLock,
1636                       PsGetCurrentThread());
1637
1638         //
1639         // Starve any exclusive waiters on this paticular call
1640         //
1641
1642         AFSAcquireSharedStarveExclusive( &pDevExt->Specific.RDR.VolumeTreeLock, TRUE);
1643
1644         //
1645         // Locate the volume node
1646         //
1647
1648         ullIndex = AFSCreateHighIndex( &InvalidateCB->FileID);
1649
1650         ntStatus = AFSLocateHashEntry( pDevExt->Specific.RDR.VolumeTree.TreeHead,
1651                                        ullIndex,
1652                                        (AFSBTreeEntry **)&pVolumeCB);
1653
1654         if( pVolumeCB != NULL)
1655         {
1656
1657             InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
1658
1659             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1660                           AFS_TRACE_LEVEL_VERBOSE,
1661                           "AFSInvalidateCache Increment count on volume %08lX Cnt %d\n",
1662                           pVolumeCB,
1663                           pVolumeCB->VolumeReferenceCount);
1664         }
1665
1666         AFSReleaseResource( &pDevExt->Specific.RDR.VolumeTreeLock);
1667
1668         if( !NT_SUCCESS( ntStatus) ||
1669             pVolumeCB == NULL)
1670         {
1671             try_return( ntStatus = STATUS_SUCCESS);
1672         }
1673
1674         //
1675         // If this is a whole volume invalidation then go do it now
1676         //
1677
1678         if( InvalidateCB->WholeVolume ||
1679             AFSIsVolumeFID( &InvalidateCB->FileID))
1680         {
1681
1682             ntStatus = AFSInvalidateVolume( pVolumeCB,
1683                                             InvalidateCB->Reason);
1684
1685             AFSFsRtlNotifyFullReportChange( &pVolumeCB->ObjectInformation,
1686                                             NULL,
1687                                             FILE_NOTIFY_CHANGE_FILE_NAME |
1688                                                 FILE_NOTIFY_CHANGE_DIR_NAME |
1689                                                 FILE_NOTIFY_CHANGE_NAME |
1690                                                 FILE_NOTIFY_CHANGE_ATTRIBUTES |
1691                                                 FILE_NOTIFY_CHANGE_SIZE,
1692                                             FILE_ACTION_MODIFIED);
1693
1694             InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
1695
1696             try_return( ntStatus);
1697         }
1698
1699         AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
1700                           TRUE);
1701
1702         InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
1703
1704         AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1705                       AFS_TRACE_LEVEL_VERBOSE,
1706                       "AFSInvalidateCache Decrement count on volume %08lX Cnt %d\n",
1707                       pVolumeCB,
1708                       pVolumeCB->VolumeReferenceCount);
1709
1710         ullIndex = AFSCreateLowIndex( &InvalidateCB->FileID);
1711
1712         ntStatus = AFSLocateHashEntry( pVolumeCB->ObjectInfoTree.TreeHead,
1713                                        ullIndex,
1714                                        (AFSBTreeEntry **)&pObjectInfo);
1715
1716         if( pObjectInfo != NULL)
1717         {
1718
1719             //
1720             // Reference the node so it won't be torn down
1721             //
1722
1723             InterlockedIncrement( &pObjectInfo->ObjectReferenceCount);
1724
1725             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1726                           AFS_TRACE_LEVEL_VERBOSE,
1727                           "AFSInvalidateCache Increment count on object %08lX Cnt %d\n",
1728                           pObjectInfo,
1729                           pObjectInfo->ObjectReferenceCount);
1730         }
1731
1732         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1733
1734         if( !NT_SUCCESS( ntStatus) ||
1735             pObjectInfo == NULL)
1736         {
1737             try_return( ntStatus = STATUS_SUCCESS);
1738         }
1739
1740         if( pObjectInfo->FileType == AFS_FILE_TYPE_SYMLINK ||
1741             pObjectInfo->FileType == AFS_FILE_TYPE_DFSLINK ||
1742             pObjectInfo->FileType == AFS_FILE_TYPE_MOUNTPOINT)
1743         {
1744
1745             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1746                           AFS_TRACE_LEVEL_VERBOSE,
1747                           "AFSInvalidateCache Invalidation on node type %d for fid %08lX-%08lX-%08lX-%08lX Reason %d\n",
1748                           pObjectInfo->FileType,
1749                           pObjectInfo->FileId.Cell,
1750                           pObjectInfo->FileId.Volume,
1751                           pObjectInfo->FileId.Vnode,
1752                           pObjectInfo->FileId.Unique,
1753                           InvalidateCB->Reason);
1754
1755             //
1756             // We only act on the mount point itself, not the target. If the
1757             // node has been deleted then mark it as such otherwise indicate
1758             // it requires verification
1759             //
1760
1761             if( InvalidateCB->Reason == AFS_INVALIDATE_DELETED)
1762             {
1763                 SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID);
1764             }
1765             else
1766             {
1767
1768                 if( InvalidateCB->Reason == AFS_INVALIDATE_FLUSHED ||
1769                     InvalidateCB->Reason == AFS_INVALIDATE_DATA_VERSION)
1770                 {
1771                     pObjectInfo->DataVersion.QuadPart = (ULONGLONG)-1;
1772                 }
1773
1774                 pObjectInfo->Expiration.QuadPart = 0;
1775
1776                 pObjectInfo->TargetFileId.Vnode = 0;
1777
1778                 pObjectInfo->TargetFileId.Unique = 0;
1779
1780                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1781                               AFS_TRACE_LEVEL_VERBOSE,
1782                               "AFSInvalidateCache Setting VERIFY flag on fid %08lX-%08lX-%08lX-%08lX\n",
1783                               pObjectInfo->FileId.Cell,
1784                               pObjectInfo->FileId.Volume,
1785                               pObjectInfo->FileId.Vnode,
1786                               pObjectInfo->FileId.Unique);
1787
1788                 SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
1789             }
1790
1791             ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
1792
1793             if( InvalidateCB->Reason == AFS_INVALIDATE_CREDS)
1794             {
1795                 ulFilter |= FILE_NOTIFY_CHANGE_SECURITY;
1796             }
1797
1798             if( InvalidateCB->Reason == AFS_INVALIDATE_DATA_VERSION)
1799             {
1800                 ulFilter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
1801             }
1802             else
1803             {
1804                 ulFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
1805             }
1806
1807             AFSFsRtlNotifyFullReportChange( pObjectInfo->ParentObjectInformation,
1808                                             NULL,
1809                                             FILE_NOTIFY_CHANGE_FILE_NAME |
1810                                             FILE_NOTIFY_CHANGE_ATTRIBUTES,
1811                                             FILE_ACTION_MODIFIED);
1812
1813             try_return( ntStatus);
1814         }
1815
1816         //
1817         // Depending on the reason for invalidation then perform work on the node
1818         //
1819
1820         switch( InvalidateCB->Reason)
1821         {
1822
1823             case AFS_INVALIDATE_DELETED:
1824             {
1825
1826                 //
1827                 // Mark this node as invalid
1828                 //
1829
1830                 SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_DELETED);
1831
1832                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1833                               AFS_TRACE_LEVEL_VERBOSE,
1834                               "AFSInvalidateCache Set DELETE flag on fid %08lX-%08lX-%08lX-%08lX\n",
1835                               pObjectInfo->FileId.Cell,
1836                               pObjectInfo->FileId.Volume,
1837                               pObjectInfo->FileId.Vnode,
1838                               pObjectInfo->FileId.Unique);
1839
1840                 if( pObjectInfo->ParentObjectInformation != NULL)
1841                 {
1842
1843                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1844                                   AFS_TRACE_LEVEL_VERBOSE,
1845                                   "AFSInvalidateCache Set VERIFY flag on parent fid %08lX-%08lX-%08lX-%08lX\n",
1846                                   pObjectInfo->ParentObjectInformation->FileId.Cell,
1847                                   pObjectInfo->ParentObjectInformation->FileId.Volume,
1848                                   pObjectInfo->ParentObjectInformation->FileId.Vnode,
1849                                   pObjectInfo->ParentObjectInformation->FileId.Unique);
1850
1851                     SetFlag( pObjectInfo->ParentObjectInformation->Flags, AFS_OBJECT_FLAGS_VERIFY);
1852                     pObjectInfo->ParentObjectInformation->DataVersion.QuadPart = (ULONGLONG)-1;
1853                     pObjectInfo->ParentObjectInformation->Expiration.QuadPart = 0;
1854                 }
1855
1856                 if( pObjectInfo->FileType == AFS_FILE_TYPE_DIRECTORY)
1857                 {
1858                     ulFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
1859                 }
1860                 else
1861                 {
1862                     ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
1863                 }
1864
1865                 AFSFsRtlNotifyFullReportChange( pObjectInfo->ParentObjectInformation,
1866                                                 NULL,
1867                                                 ulFilter,
1868                                                 FILE_ACTION_REMOVED);
1869
1870                 break;
1871             }
1872
1873             case AFS_INVALIDATE_FLUSHED:
1874             {
1875
1876                 if( pObjectInfo->FileType == AFS_FILE_TYPE_FILE &&
1877                     pObjectInfo->Fcb != NULL)
1878                 {
1879
1880                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1881                                   AFS_TRACE_LEVEL_VERBOSE,
1882                                   "AFSInvalidateCache Flush/purge file fid %08lX-%08lX-%08lX-%08lX\n",
1883                                   pObjectInfo->FileId.Cell,
1884                                   pObjectInfo->FileId.Volume,
1885                                   pObjectInfo->FileId.Vnode,
1886                                   pObjectInfo->FileId.Unique);
1887
1888                     AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Resource,
1889                                     TRUE);
1890
1891                     __try
1892                     {
1893
1894                         CcFlushCache( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
1895                                       NULL,
1896                                       0,
1897                                       &stIoStatus);
1898
1899                         if( !NT_SUCCESS( stIoStatus.Status))
1900                         {
1901
1902                             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1903                                           AFS_TRACE_LEVEL_ERROR,
1904                                           "AFSInvalidateCache CcFlushCache failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1905                                           pObjectInfo->FileId.Cell,
1906                                           pObjectInfo->FileId.Volume,
1907                                           pObjectInfo->FileId.Vnode,
1908                                           pObjectInfo->FileId.Unique,
1909                                           stIoStatus.Status,
1910                                           stIoStatus.Information);
1911
1912                             ntStatus = stIoStatus.Status;
1913                         }
1914
1915                         CcPurgeCacheSection( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
1916                                              NULL,
1917                                              0,
1918                                              FALSE);
1919                     }
1920                     __except( EXCEPTION_EXECUTE_HANDLER)
1921                     {
1922
1923                         ntStatus = GetExceptionCode();
1924                     }
1925
1926                     AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->Resource);
1927
1928                     //
1929                     // Clear out the extents
1930                     // Get rid of them (note this involves waiting
1931                     // for any writes or reads to the cache to complete)
1932                     //
1933
1934                     (VOID) AFSTearDownFcbExtents( pObjectInfo->Fcb);
1935                 }
1936
1937                 // Fall through to the default processing
1938             }
1939
1940             default:
1941             {
1942
1943                 if( pObjectInfo->FileType == AFS_FILE_TYPE_DIRECTORY)
1944                 {
1945                     ulFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
1946                 }
1947                 else
1948                 {
1949                     ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
1950                 }
1951
1952                 if( InvalidateCB->Reason == AFS_INVALIDATE_CREDS)
1953                 {
1954                     ulFilter |= FILE_NOTIFY_CHANGE_SECURITY;
1955                 }
1956
1957                 if( InvalidateCB->Reason == AFS_INVALIDATE_DATA_VERSION)
1958                 {
1959                     ulFilter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
1960                 }
1961                 else
1962                 {
1963                     ulFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
1964                 }
1965
1966                 AFSFsRtlNotifyFullReportChange( pObjectInfo->ParentObjectInformation,
1967                                                 NULL,
1968                                                 ulFilter,
1969                                                 FILE_ACTION_MODIFIED);
1970
1971                 //
1972                 // Indicate this node requires re-evaluation for the remaining reasons
1973                 //
1974
1975                 pObjectInfo->Expiration.QuadPart = 0;
1976
1977                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1978                               AFS_TRACE_LEVEL_VERBOSE,
1979                               "AFSInvalidateCache Setting VERIFY flag on fid %08lX-%08lX-%08lX-%08lX\n",
1980                               pObjectInfo->FileId.Cell,
1981                               pObjectInfo->FileId.Volume,
1982                               pObjectInfo->FileId.Vnode,
1983                               pObjectInfo->FileId.Unique);
1984
1985                 SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
1986
1987                 if( InvalidateCB->Reason == AFS_INVALIDATE_FLUSHED ||
1988                     InvalidateCB->Reason == AFS_INVALIDATE_DATA_VERSION)
1989                 {
1990                     pObjectInfo->DataVersion.QuadPart = (ULONGLONG)-1;
1991
1992                     if( pObjectInfo->FileType == AFS_FILE_TYPE_FILE)
1993                     {
1994
1995                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1996                                       AFS_TRACE_LEVEL_VERBOSE,
1997                                       "AFSInvalidateCache Setting VERIFY_DATA flag on fid %08lX-%08lX-%08lX-%08lX\n",
1998                                       pObjectInfo->FileId.Cell,
1999                                       pObjectInfo->FileId.Volume,
2000                                       pObjectInfo->FileId.Vnode,
2001                                       pObjectInfo->FileId.Unique);
2002
2003                         SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA);
2004                     }
2005                 }
2006
2007                 break;
2008             }
2009         }
2010
2011 try_exit:
2012
2013         if( pObjectInfo != NULL)
2014         {
2015
2016             InterlockedDecrement( &pObjectInfo->ObjectReferenceCount);
2017
2018             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2019                           AFS_TRACE_LEVEL_VERBOSE,
2020                           "AFSInvalidateCache Decrement count on object %08lX Cnt %d\n",
2021                           pObjectInfo,
2022                           pObjectInfo->ObjectReferenceCount);
2023         }
2024     }
2025
2026     return ntStatus;
2027 }
2028
2029 BOOLEAN
2030 AFSIsChildOfParent( IN AFSFcb *Dcb,
2031                     IN AFSFcb *Fcb)
2032 {
2033
2034     BOOLEAN bIsChild = FALSE;
2035     AFSFcb *pCurrentFcb = Fcb;
2036
2037     while( pCurrentFcb != NULL)
2038     {
2039
2040         if( pCurrentFcb->ObjectInformation->ParentObjectInformation == Dcb->ObjectInformation)
2041         {
2042
2043             bIsChild = TRUE;
2044
2045             break;
2046         }
2047
2048         pCurrentFcb = pCurrentFcb->ObjectInformation->ParentObjectInformation->Fcb;
2049     }
2050
2051     return bIsChild;
2052 }
2053
2054 inline
2055 ULONGLONG
2056 AFSCreateHighIndex( IN AFSFileID *FileID)
2057 {
2058
2059     ULONGLONG ullIndex = 0;
2060
2061     ullIndex = (((ULONGLONG)FileID->Cell << 32) | FileID->Volume);
2062
2063     return ullIndex;
2064 }
2065
2066 inline
2067 ULONGLONG
2068 AFSCreateLowIndex( IN AFSFileID *FileID)
2069 {
2070
2071     ULONGLONG ullIndex = 0;
2072
2073     ullIndex = (((ULONGLONG)FileID->Vnode << 32) | FileID->Unique);
2074
2075     return ullIndex;
2076 }
2077
2078 BOOLEAN
2079 AFSCheckAccess( IN ACCESS_MASK DesiredAccess,
2080                 IN ACCESS_MASK GrantedAccess,
2081                 IN BOOLEAN DirectoryEntry)
2082 {
2083
2084     BOOLEAN bAccessGranted = TRUE;
2085
2086     //
2087     // Check if we are asking for read/write and granted only read only
2088     // NOTE: There will be more checks here
2089     //
2090
2091     if( !AFSCheckForReadOnlyAccess( DesiredAccess,
2092                                     DirectoryEntry) &&
2093         AFSCheckForReadOnlyAccess( GrantedAccess,
2094                                    DirectoryEntry))
2095     {
2096
2097         bAccessGranted = FALSE;
2098     }
2099
2100     return bAccessGranted;
2101 }
2102
2103 NTSTATUS
2104 AFSGetDriverStatus( IN AFSDriverStatusRespCB *DriverStatus)
2105 {
2106
2107     NTSTATUS         ntStatus = STATUS_SUCCESS;
2108     AFSDeviceExt    *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
2109
2110     //
2111     // Start with read
2112     //
2113
2114     DriverStatus->Status = AFS_DRIVER_STATUS_READY;
2115
2116     if( AFSGlobalRoot == NULL)
2117     {
2118
2119         //
2120         // We are not ready
2121         //
2122
2123         DriverStatus->Status = AFS_DRIVER_STATUS_NOT_READY;
2124     }
2125
2126     if( pControlDevExt->Specific.Control.CommServiceCB.IrpPoolControlFlag != POOL_ACTIVE)
2127     {
2128
2129         //
2130         // No service yet
2131         //
2132
2133         DriverStatus->Status = AFS_DRIVER_STATUS_NO_SERVICE;
2134     }
2135
2136     return ntStatus;
2137 }
2138
2139 NTSTATUS
2140 AFSSubstituteSysName( IN UNICODE_STRING *ComponentName,
2141                       IN UNICODE_STRING *SubstituteName,
2142                       IN ULONG StringIndex)
2143 {
2144
2145     NTSTATUS ntStatus = STATUS_SUCCESS;
2146     AFSDeviceExt    *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
2147     AFSSysNameCB    *pSysName = NULL;
2148     ERESOURCE       *pSysNameLock = NULL;
2149     ULONG            ulIndex = 1;
2150     USHORT           usIndex = 0;
2151     UNICODE_STRING   uniSysName;
2152
2153     __Enter
2154     {
2155
2156 #if defined(_WIN64)
2157
2158         if( IoIs32bitProcess( NULL))
2159         {
2160
2161             pSysNameLock = &pControlDevExt->Specific.Control.SysName32ListLock;
2162
2163             pSysName = pControlDevExt->Specific.Control.SysName32ListHead;
2164         }
2165         else
2166         {
2167
2168             pSysNameLock = &pControlDevExt->Specific.Control.SysName64ListLock;
2169
2170             pSysName = pControlDevExt->Specific.Control.SysName64ListHead;
2171         }
2172 #else
2173
2174         pSysNameLock = &pControlDevExt->Specific.Control.SysName32ListLock;
2175
2176         pSysName = pControlDevExt->Specific.Control.SysName32ListHead;
2177
2178 #endif
2179
2180         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2181                       AFS_TRACE_LEVEL_VERBOSE,
2182                       "AFSSubstituteSysName Acquiring SysName lock %08lX SHARED %08lX\n",
2183                       pSysNameLock,
2184                       PsGetCurrentThread());
2185
2186         AFSAcquireShared( pSysNameLock,
2187                           TRUE);
2188
2189         //
2190         // Find where we are in the list
2191         //
2192
2193         while( pSysName != NULL &&
2194             ulIndex < StringIndex)
2195         {
2196
2197             pSysName = pSysName->fLink;
2198
2199             ulIndex++;
2200         }
2201
2202         if( pSysName == NULL)
2203         {
2204
2205             try_return( ntStatus = STATUS_OBJECT_NAME_NOT_FOUND);
2206         }
2207
2208         RtlInitUnicodeString( &uniSysName,
2209                               L"@SYS");
2210         //
2211         // If it is a full component of @SYS then just substitue the
2212         // name in
2213         //
2214
2215         if( RtlCompareUnicodeString( &uniSysName,
2216                                      ComponentName,
2217                                      TRUE) == 0)
2218         {
2219
2220             SubstituteName->Length = pSysName->SysName.Length;
2221             SubstituteName->MaximumLength = SubstituteName->Length;
2222
2223             SubstituteName->Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2224                                                                         SubstituteName->Length,
2225                                                                         AFS_SUBST_BUFFER_TAG);
2226
2227             if( SubstituteName->Buffer == NULL)
2228             {
2229
2230                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2231             }
2232
2233             RtlCopyMemory( SubstituteName->Buffer,
2234                            pSysName->SysName.Buffer,
2235                            pSysName->SysName.Length);
2236         }
2237         else
2238         {
2239
2240             usIndex = 0;
2241
2242             while( ComponentName->Buffer[ usIndex] != L'@')
2243             {
2244
2245                 usIndex++;
2246             }
2247
2248             SubstituteName->Length = (usIndex * sizeof( WCHAR)) + pSysName->SysName.Length;
2249             SubstituteName->MaximumLength = SubstituteName->Length;
2250
2251             SubstituteName->Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2252                                                                         SubstituteName->Length,
2253                                                                         AFS_SUBST_BUFFER_TAG);
2254
2255             if( SubstituteName->Buffer == NULL)
2256             {
2257
2258                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2259             }
2260
2261             RtlCopyMemory( SubstituteName->Buffer,
2262                            ComponentName->Buffer,
2263                            usIndex * sizeof( WCHAR));
2264
2265             RtlCopyMemory( &SubstituteName->Buffer[ usIndex],
2266                            pSysName->SysName.Buffer,
2267                            pSysName->SysName.Length);
2268         }
2269
2270 try_exit:
2271
2272         AFSReleaseResource( pSysNameLock);
2273     }
2274
2275     return ntStatus;
2276 }
2277
2278 NTSTATUS
2279 AFSSubstituteNameInPath( IN OUT UNICODE_STRING *FullPathName,
2280                          IN OUT UNICODE_STRING *ComponentName,
2281                          IN UNICODE_STRING *SubstituteName,
2282                          IN OUT UNICODE_STRING *RemainingPath,
2283                          IN BOOLEAN FreePathName)
2284 {
2285
2286     NTSTATUS ntStatus = STATUS_SUCCESS;
2287     UNICODE_STRING uniPathName;
2288     USHORT usPrefixNameLen = 0;
2289     SHORT  sNameLenDelta = 0;
2290
2291     __Enter
2292     {
2293
2294         //
2295         // If the passed in name can handle the additional length
2296         // then just moves things around
2297         //
2298
2299         sNameLenDelta = SubstituteName->Length - ComponentName->Length;
2300
2301         usPrefixNameLen = (USHORT)(ComponentName->Buffer - FullPathName->Buffer);
2302
2303         if( FullPathName->MaximumLength > FullPathName->Length + sNameLenDelta)
2304         {
2305
2306             if( FullPathName->Length > usPrefixNameLen + ComponentName->Length)
2307             {
2308
2309                 RtlMoveMemory( &FullPathName->Buffer[ ((usPrefixNameLen*sizeof( WCHAR) + SubstituteName->Length)/sizeof( WCHAR))],
2310                                &FullPathName->Buffer[ ((usPrefixNameLen*sizeof( WCHAR) + ComponentName->Length)/sizeof( WCHAR))],
2311                                FullPathName->Length - usPrefixNameLen*sizeof( WCHAR) - ComponentName->Length);
2312             }
2313
2314             RtlCopyMemory( &FullPathName->Buffer[ usPrefixNameLen],
2315                            SubstituteName->Buffer,
2316                            SubstituteName->Length);
2317
2318             FullPathName->Length += sNameLenDelta;
2319
2320             ComponentName->Length += sNameLenDelta;
2321
2322             ComponentName->MaximumLength = ComponentName->Length;
2323
2324             if ( RemainingPath->Buffer)
2325             {
2326
2327                 RemainingPath->Buffer += sNameLenDelta/sizeof( WCHAR);
2328             }
2329
2330             try_return( ntStatus);
2331         }
2332
2333         //
2334         // Need to re-allocate the buffer
2335         //
2336
2337         uniPathName.Length = FullPathName->Length -
2338                                          ComponentName->Length +
2339                                          SubstituteName->Length;
2340
2341         uniPathName.MaximumLength = FullPathName->MaximumLength + PAGE_SIZE;
2342
2343         uniPathName.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2344                                                                 uniPathName.MaximumLength,
2345                                                                 AFS_NAME_BUFFER_FOUR_TAG);
2346
2347         if( uniPathName.Buffer == NULL)
2348         {
2349
2350             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2351         }
2352
2353         usPrefixNameLen = (USHORT)(ComponentName->Buffer - FullPathName->Buffer);
2354
2355         usPrefixNameLen *= sizeof( WCHAR);
2356
2357         RtlZeroMemory( uniPathName.Buffer,
2358                        uniPathName.MaximumLength);
2359
2360         RtlCopyMemory( uniPathName.Buffer,
2361                        FullPathName->Buffer,
2362                        usPrefixNameLen);
2363
2364         RtlCopyMemory( &uniPathName.Buffer[ (usPrefixNameLen/sizeof( WCHAR))],
2365                        SubstituteName->Buffer,
2366                        SubstituteName->Length);
2367
2368         if( FullPathName->Length > usPrefixNameLen + ComponentName->Length)
2369         {
2370
2371             RtlCopyMemory( &uniPathName.Buffer[ (usPrefixNameLen + SubstituteName->Length)/sizeof( WCHAR)],
2372                            &FullPathName->Buffer[ (usPrefixNameLen + ComponentName->Length)/sizeof( WCHAR)],
2373                            FullPathName->Length - usPrefixNameLen - ComponentName->Length);
2374         }
2375
2376         ComponentName->Buffer = uniPathName.Buffer + (ComponentName->Buffer - FullPathName->Buffer);
2377
2378         ComponentName->Length += sNameLenDelta;
2379
2380         ComponentName->MaximumLength = ComponentName->Length;
2381
2382         if ( RemainingPath->Buffer)
2383         {
2384
2385             RemainingPath->Buffer = uniPathName.Buffer
2386                 + (RemainingPath->Buffer - FullPathName->Buffer)
2387                 + sNameLenDelta/sizeof( WCHAR);
2388         }
2389
2390         if( FreePathName)
2391         {
2392             AFSExFreePool( FullPathName->Buffer);
2393         }
2394
2395         *FullPathName = uniPathName;
2396
2397 try_exit:
2398
2399         NOTHING;
2400     }
2401
2402     return ntStatus;
2403 }
2404
2405 NTSTATUS
2406 AFSInvalidateVolume( IN AFSVolumeCB *VolumeCB,
2407                      IN ULONG Reason)
2408 {
2409
2410     NTSTATUS ntStatus = STATUS_SUCCESS;
2411     AFSFcb *pFcb = NULL;
2412     AFSObjectInfoCB *pCurrentObject = NULL;
2413     ULONG ulFilter = 0;
2414
2415     __Enter
2416     {
2417
2418         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2419                       AFS_TRACE_LEVEL_VERBOSE,
2420                       "AFSInvalidateVolume Invalidate volume fid %08lX-%08lX-%08lX-%08lX Reason %08lX\n",
2421                       VolumeCB->ObjectInformation.FileId.Cell,
2422                       VolumeCB->ObjectInformation.FileId.Volume,
2423                       VolumeCB->ObjectInformation.FileId.Vnode,
2424                       VolumeCB->ObjectInformation.FileId.Unique,
2425                       Reason);
2426
2427         //
2428         // Depending on the reason for invalidation then perform work on the node
2429         //
2430
2431         switch( Reason)
2432         {
2433
2434             case AFS_INVALIDATE_DELETED:
2435             {
2436
2437                 //
2438                 // Mark this volume as invalid
2439                 //
2440
2441                 VolumeCB->ObjectInformation.Expiration.QuadPart = 0;
2442
2443                 SetFlag( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID);
2444
2445                 SetFlag( VolumeCB->Flags, AFS_VOLUME_FLAGS_OFFLINE);
2446
2447                 AFSFsRtlNotifyFullReportChange( &VolumeCB->ObjectInformation,
2448                                                 NULL,
2449                                                 FILE_NOTIFY_CHANGE_DIR_NAME,
2450                                                 FILE_ACTION_REMOVED);
2451
2452                 AFSAcquireShared( VolumeCB->ObjectInfoTree.TreeLock,
2453                                   TRUE);
2454
2455                 pCurrentObject = VolumeCB->ObjectInfoListHead;
2456
2457                 while( pCurrentObject != NULL)
2458                 {
2459
2460                     if( pCurrentObject->FileType == AFS_FILE_TYPE_DIRECTORY)
2461                     {
2462                         ulFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
2463                     }
2464                     else
2465                     {
2466                         ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
2467                     }
2468
2469                     AFSFsRtlNotifyFullReportChange( pCurrentObject,
2470                                                     NULL,
2471                                                     ulFilter,
2472                                                     FILE_ACTION_REMOVED);
2473
2474                     SetFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID);
2475
2476                     pFcb = pCurrentObject->Fcb;
2477
2478                     if( pFcb != NULL &&
2479                         pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
2480                     {
2481
2482
2483                         //
2484                         // Clear out the extents
2485                         // And get rid of them (note this involves waiting
2486                         // for any writes or reads to the cache to complete)
2487                         //
2488
2489                         (VOID) AFSTearDownFcbExtents( pFcb);
2490                     }
2491
2492                     pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
2493                 }
2494
2495                 AFSReleaseResource( VolumeCB->ObjectInfoTree.TreeLock);
2496
2497                 break;
2498             }
2499
2500             default:
2501             {
2502
2503                 //
2504                 // Indicate this node requires re-evaluation for the remaining reasons
2505                 //
2506
2507                 VolumeCB->ObjectInformation.Expiration.QuadPart = 0;
2508
2509                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2510                               AFS_TRACE_LEVEL_VERBOSE,
2511                               "AFSInvalidateVolume Setting VERIFY flag on fid %08lX-%08lX-%08lX-%08lX\n",
2512                               VolumeCB->ObjectInformation.FileId.Cell,
2513                               VolumeCB->ObjectInformation.FileId.Volume,
2514                               VolumeCB->ObjectInformation.FileId.Vnode,
2515                               VolumeCB->ObjectInformation.FileId.Unique);
2516
2517                 SetFlag( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_VERIFY);
2518
2519                 if( Reason == AFS_INVALIDATE_FLUSHED ||
2520                     Reason == AFS_INVALIDATE_DATA_VERSION)
2521                 {
2522
2523                     VolumeCB->ObjectInformation.DataVersion.QuadPart = (ULONGLONG)-1;
2524                 }
2525
2526                 //
2527                 // Notify anyone that cares
2528                 //
2529
2530                 ulFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
2531
2532                 if( Reason == AFS_INVALIDATE_CREDS)
2533                 {
2534                     ulFilter |= FILE_NOTIFY_CHANGE_SECURITY;
2535                 }
2536
2537                 if( Reason == AFS_INVALIDATE_DATA_VERSION)
2538                 {
2539                     ulFilter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
2540                 }
2541                 else
2542                 {
2543                     ulFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
2544                 }
2545
2546                 AFSFsRtlNotifyFullReportChange( &VolumeCB->ObjectInformation,
2547                                                 NULL,
2548                                                 ulFilter,
2549                                                 FILE_ACTION_MODIFIED);
2550
2551                 //
2552                 // Volume invalidations require all objects in the volume be re-verified
2553                 //
2554
2555                 AFSAcquireShared( VolumeCB->ObjectInfoTree.TreeLock,
2556                                   TRUE);
2557
2558                 pCurrentObject = VolumeCB->ObjectInfoListHead;
2559
2560                 while( pCurrentObject != NULL)
2561                 {
2562
2563                     pCurrentObject->Expiration.QuadPart = 0;
2564
2565                     pCurrentObject->TargetFileId.Vnode = 0;
2566
2567                     pCurrentObject->TargetFileId.Unique = 0;
2568
2569                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2570                                   AFS_TRACE_LEVEL_VERBOSE,
2571                                   "AFSInvalidateVolume Setting VERIFY flag on fid %08lX-%08lX-%08lX-%08lX\n",
2572                                   pCurrentObject->FileId.Cell,
2573                                   pCurrentObject->FileId.Volume,
2574                                   pCurrentObject->FileId.Vnode,
2575                                   pCurrentObject->FileId.Unique);
2576
2577                     SetFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_VERIFY);
2578
2579                     if( Reason == AFS_INVALIDATE_FLUSHED ||
2580                         Reason == AFS_INVALIDATE_DATA_VERSION)
2581                     {
2582
2583                         pCurrentObject->DataVersion.QuadPart = (ULONGLONG)-1;
2584
2585                         if( pCurrentObject->FileType == AFS_FILE_TYPE_FILE)
2586                         {
2587
2588                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2589                                           AFS_TRACE_LEVEL_VERBOSE,
2590                                           "AFSInvalidateVolume Setting VERIFY_DATA flag on fid %08lX-%08lX-%08lX-%08lX\n",
2591                                           pCurrentObject->FileId.Cell,
2592                                           pCurrentObject->FileId.Volume,
2593                                           pCurrentObject->FileId.Vnode,
2594                                           pCurrentObject->FileId.Unique);
2595
2596                             SetFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA);
2597                         }
2598                     }
2599
2600                     if( pCurrentObject->FileType == AFS_FILE_TYPE_DIRECTORY)
2601                     {
2602                         ulFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
2603                     }
2604                     else
2605                     {
2606                         ulFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
2607                     }
2608
2609                     if( Reason == AFS_INVALIDATE_CREDS)
2610                     {
2611                         ulFilter |= FILE_NOTIFY_CHANGE_SECURITY;
2612                     }
2613
2614                     if( Reason == AFS_INVALIDATE_DATA_VERSION)
2615                     {
2616                         ulFilter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
2617                     }
2618                     else
2619                     {
2620                         ulFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
2621                     }
2622
2623                     AFSFsRtlNotifyFullReportChange( pCurrentObject,
2624                                                     NULL,
2625                                                     ulFilter,
2626                                                     FILE_ACTION_MODIFIED);
2627
2628                     pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
2629                 }
2630
2631                 AFSReleaseResource( VolumeCB->ObjectInfoTree.TreeLock);
2632
2633                 break;
2634             }
2635         }
2636     }
2637
2638     return ntStatus;
2639 }
2640
2641 NTSTATUS
2642 AFSVerifyEntry( IN GUID *AuthGroup,
2643                 IN AFSDirectoryCB *DirEntry)
2644 {
2645
2646     NTSTATUS ntStatus = STATUS_SUCCESS;
2647     AFSDirEnumEntry *pDirEnumEntry = NULL;
2648     AFSObjectInfoCB *pObjectInfo = DirEntry->ObjectInformation;
2649     IO_STATUS_BLOCK stIoStatus;
2650
2651     __Enter
2652     {
2653
2654         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2655                       AFS_TRACE_LEVEL_VERBOSE_2,
2656                       "AFSVerifyEntry Verifying entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2657                       &DirEntry->NameInformation.FileName,
2658                       pObjectInfo->FileId.Cell,
2659                       pObjectInfo->FileId.Volume,
2660                       pObjectInfo->FileId.Vnode,
2661                       pObjectInfo->FileId.Unique);
2662
2663         ntStatus = AFSEvaluateTargetByID( pObjectInfo,
2664                                           AuthGroup,
2665                                           FALSE,
2666                                           &pDirEnumEntry);
2667
2668         if( !NT_SUCCESS( ntStatus))
2669         {
2670
2671             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2672                           AFS_TRACE_LEVEL_ERROR,
2673                           "AFSValidateEntry Evaluate Target failed %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
2674                           &DirEntry->NameInformation.FileName,
2675                           pObjectInfo->FileId.Cell,
2676                           pObjectInfo->FileId.Volume,
2677                           pObjectInfo->FileId.Vnode,
2678                           pObjectInfo->FileId.Unique,
2679                           ntStatus);
2680
2681             try_return( ntStatus);
2682         }
2683
2684         //
2685         // Check the data version of the file
2686         //
2687
2688         if( pObjectInfo->DataVersion.QuadPart == pDirEnumEntry->DataVersion.QuadPart &&
2689             !BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA))
2690         {
2691
2692             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2693                           AFS_TRACE_LEVEL_VERBOSE,
2694                           "AFSVerifyEntry No DV change %I64X for Fcb %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2695                           pObjectInfo->DataVersion.QuadPart,
2696                           &DirEntry->NameInformation.FileName,
2697                           pObjectInfo->FileId.Cell,
2698                           pObjectInfo->FileId.Volume,
2699                           pObjectInfo->FileId.Vnode,
2700                           pObjectInfo->FileId.Unique);
2701
2702             //
2703             // We are ok, just get out
2704             //
2705
2706             ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2707
2708             try_return( ntStatus = STATUS_SUCCESS);
2709         }
2710
2711         //
2712         // New data version so we will need to process the node based on the type
2713         //
2714
2715         switch( pDirEnumEntry->FileType)
2716         {
2717
2718             case AFS_FILE_TYPE_MOUNTPOINT:
2719             {
2720
2721                 //
2722                 // For a mount point we need to ensure the target is the same
2723                 //
2724
2725                 if( !AFSIsEqualFID( &pObjectInfo->TargetFileId,
2726                                     &pDirEnumEntry->TargetFileId))
2727                 {
2728
2729                 }
2730
2731                 //
2732                 // Update the metadata for the entry
2733                 //
2734
2735                 ntStatus = AFSUpdateMetaData( DirEntry,
2736                                               pDirEnumEntry);
2737
2738                 if( NT_SUCCESS( ntStatus))
2739                 {
2740
2741                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2742                 }
2743
2744                 break;
2745             }
2746
2747             case AFS_FILE_TYPE_SYMLINK:
2748             {
2749
2750                 ASSERT( pDirEnumEntry->TargetNameLength > 0);
2751
2752                 //
2753                 // Update the metadata for the entry
2754                 //
2755
2756                 ntStatus = AFSUpdateMetaData( DirEntry,
2757                                               pDirEnumEntry);
2758
2759                 if( NT_SUCCESS( ntStatus))
2760                 {
2761
2762                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2763                 }
2764
2765                 break;
2766             }
2767
2768             case AFS_FILE_TYPE_FILE:
2769             {
2770                 FILE_OBJECT * pCCFileObject = NULL;
2771                 BOOLEAN bPurgeExtents = FALSE;
2772
2773                 if ( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA))
2774                 {
2775                     bPurgeExtents = TRUE;
2776
2777                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2778                                   AFS_TRACE_LEVEL_VERBOSE,
2779                                   "AFSVerifyEntry Clearing VERIFY_DATA flag %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2780                                   &DirEntry->NameInformation.FileName,
2781                                   pObjectInfo->FileId.Cell,
2782                                   pObjectInfo->FileId.Volume,
2783                                   pObjectInfo->FileId.Vnode,
2784                                   pObjectInfo->FileId.Unique);
2785
2786                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA);
2787                 }
2788
2789                 //
2790                 // Update the metadata for the entry
2791                 //
2792
2793                 ntStatus = AFSUpdateMetaData( DirEntry,
2794                                               pDirEnumEntry);
2795
2796                 if( !NT_SUCCESS( ntStatus))
2797                 {
2798
2799                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2800                                   AFS_TRACE_LEVEL_ERROR,
2801                                   "AFSInvalidateCache Meta Data Update failed %wZ FID %08lX-%08lX-%08lX-%08lX ntStatus %08lX\n",
2802                                   &DirEntry->NameInformation.FileName,
2803                                   pObjectInfo->FileId.Cell,
2804                                   pObjectInfo->FileId.Volume,
2805                                   pObjectInfo->FileId.Vnode,
2806                                   pObjectInfo->FileId.Unique,
2807                                   ntStatus);
2808
2809                     break;
2810                 }
2811
2812                 if( pObjectInfo->Fcb != NULL)
2813                 {
2814
2815                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2816                                   AFS_TRACE_LEVEL_VERBOSE,
2817                                   "AFSVerifyEntry Flush/purge entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2818                                   &DirEntry->NameInformation.FileName,
2819                                   pObjectInfo->FileId.Cell,
2820                                   pObjectInfo->FileId.Volume,
2821                                   pObjectInfo->FileId.Vnode,
2822                                   pObjectInfo->FileId.Unique);
2823
2824                     AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Resource,
2825                                     TRUE);
2826
2827                     __try
2828                     {
2829
2830                         CcFlushCache( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2831                                       NULL,
2832                                       0,
2833                                       &stIoStatus);
2834
2835                         if( !NT_SUCCESS( stIoStatus.Status))
2836                         {
2837
2838                             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2839                                           AFS_TRACE_LEVEL_ERROR,
2840                                           "AFSVerifyEntry CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
2841                                           &DirEntry->NameInformation.FileName,
2842                                           pObjectInfo->FileId.Cell,
2843                                           pObjectInfo->FileId.Volume,
2844                                           pObjectInfo->FileId.Vnode,
2845                                           pObjectInfo->FileId.Unique,
2846                                           stIoStatus.Status,
2847                                           stIoStatus.Information);
2848
2849                             ntStatus = stIoStatus.Status;
2850                         }
2851
2852                         if ( bPurgeExtents)
2853                         {
2854
2855                             CcPurgeCacheSection( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2856                                                  NULL,
2857                                                  0,
2858                                                  FALSE);
2859                         }
2860                     }
2861                     __except( EXCEPTION_EXECUTE_HANDLER)
2862                     {
2863                         ntStatus = GetExceptionCode();
2864
2865                         AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2866                                       AFS_TRACE_LEVEL_ERROR,
2867                                       "AFSVerifyEntry CcFlushCache or CcPurgeCacheSection Exception %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
2868                                       &DirEntry->NameInformation.FileName,
2869                                       pObjectInfo->FileId.Cell,
2870                                       pObjectInfo->FileId.Volume,
2871                                       pObjectInfo->FileId.Vnode,
2872                                       pObjectInfo->FileId.Unique,
2873                                       ntStatus);
2874                     }
2875
2876                     AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->Resource);
2877
2878                     if ( bPurgeExtents)
2879                     {
2880                         AFSFlushExtents( pObjectInfo->Fcb);
2881                     }
2882
2883                     //
2884                     // Reacquire the Fcb to purge the cache
2885                     //
2886
2887                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2888                                   AFS_TRACE_LEVEL_VERBOSE,
2889                                   "AFSVerifyEntry Acquiring Fcb lock %08lX EXCL %08lX\n",
2890                                   &pObjectInfo->Fcb->NPFcb->Resource,
2891                                   PsGetCurrentThread());
2892
2893                     AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Resource,
2894                                     TRUE);
2895
2896                     //
2897                     // Update file sizes
2898                     //
2899
2900                     pObjectInfo->Fcb->Header.AllocationSize.QuadPart  = pObjectInfo->AllocationSize.QuadPart;
2901                     pObjectInfo->Fcb->Header.FileSize.QuadPart        = pObjectInfo->EndOfFile.QuadPart;
2902                     pObjectInfo->Fcb->Header.ValidDataLength.QuadPart = pObjectInfo->EndOfFile.QuadPart;
2903
2904                     pCCFileObject = CcGetFileObjectFromSectionPtrs( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers);
2905
2906                     if ( pCCFileObject != NULL)
2907                     {
2908                         CcSetFileSizes( pCCFileObject,
2909                                         (PCC_FILE_SIZES)&pObjectInfo->Fcb->Header.AllocationSize);
2910                     }
2911
2912                     AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->Resource);
2913                 }
2914                 else
2915                 {
2916                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2917                                   AFS_TRACE_LEVEL_WARNING,
2918                                   "AFSValidateEntry Fcb NULL %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2919                                   &DirEntry->NameInformation.FileName,
2920                                   pObjectInfo->FileId.Cell,
2921                                   pObjectInfo->FileId.Volume,
2922                                   pObjectInfo->FileId.Vnode,
2923                                   pObjectInfo->FileId.Unique);
2924                 }
2925
2926                 ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2927
2928                 break;
2929             }
2930
2931             case AFS_FILE_TYPE_DIRECTORY:
2932             {
2933
2934                 AFSFcb *pCurrentFcb = NULL;
2935                 AFSDirectoryCB *pCurrentDirEntry = NULL;
2936
2937                 //
2938                 // For a directory or root entry flush the content of
2939                 // the directory enumeration.
2940                 //
2941
2942                 if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
2943                 {
2944
2945                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2946                                   AFS_TRACE_LEVEL_VERBOSE_2,
2947                                   "AFSVerifyEntry Validating directory content for entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2948                                   &DirEntry->NameInformation.FileName,
2949                                   pObjectInfo->FileId.Cell,
2950                                   pObjectInfo->FileId.Volume,
2951                                   pObjectInfo->FileId.Vnode,
2952                                   pObjectInfo->FileId.Unique);
2953
2954                     AFSAcquireExcl( pObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
2955                                     TRUE);
2956
2957                     AFSValidateDirectoryCache( pObjectInfo,
2958                                                AuthGroup);
2959
2960                     AFSReleaseResource( pObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
2961                 }
2962
2963                 //
2964                 // Update the metadata for the entry
2965                 //
2966
2967                 ntStatus = AFSUpdateMetaData( DirEntry,
2968                                               pDirEnumEntry);
2969
2970                 if( NT_SUCCESS( ntStatus))
2971                 {
2972
2973                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2974                 }
2975
2976                 break;
2977             }
2978
2979             case AFS_FILE_TYPE_DFSLINK:
2980             {
2981
2982                 UNICODE_STRING uniTargetName;
2983
2984                 //
2985                 // For a DFS link need to check the target name has not changed
2986                 //
2987
2988                 uniTargetName.Length = (USHORT)pDirEnumEntry->TargetNameLength;
2989
2990                 uniTargetName.MaximumLength = uniTargetName.Length;
2991
2992                 uniTargetName.Buffer = (WCHAR *)((char *)pDirEnumEntry + pDirEnumEntry->TargetNameOffset);
2993
2994                 AFSAcquireExcl( &DirEntry->NonPaged->Lock,
2995                                 TRUE);
2996
2997                 if( DirEntry->NameInformation.TargetName.Length == 0 ||
2998                     RtlCompareUnicodeString( &uniTargetName,
2999                                              &DirEntry->NameInformation.TargetName,
3000                                              TRUE) != 0)
3001                 {
3002
3003                     //
3004                     // Update the target name
3005                     //
3006
3007                     ntStatus = AFSUpdateTargetName( &DirEntry->NameInformation.TargetName,
3008                                                     &DirEntry->Flags,
3009                                                     uniTargetName.Buffer,
3010                                                     uniTargetName.Length);
3011
3012                     if( !NT_SUCCESS( ntStatus))
3013                     {
3014
3015                         AFSReleaseResource( &DirEntry->NonPaged->Lock);
3016
3017                         break;
3018                     }
3019                 }
3020
3021                 AFSReleaseResource( &DirEntry->NonPaged->Lock);
3022
3023                 //
3024                 // Update the metadata for the entry
3025                 //
3026
3027                 ntStatus = AFSUpdateMetaData( DirEntry,
3028                                               pDirEnumEntry);
3029
3030                 if( NT_SUCCESS( ntStatus))
3031                 {
3032
3033                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
3034                 }
3035
3036                 break;
3037             }
3038
3039             default:
3040
3041                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3042                               AFS_TRACE_LEVEL_WARNING,
3043                               "AFSVerifyEntry Attempt to verify node of type %d\n",
3044                               pObjectInfo->FileType);
3045
3046                 break;
3047         }
3048
3049  try_exit:
3050
3051         if( pDirEnumEntry != NULL)
3052         {
3053
3054             AFSExFreePool( pDirEnumEntry);
3055         }
3056     }
3057
3058     return ntStatus;
3059 }
3060
3061 NTSTATUS
3062 AFSSetVolumeState( IN AFSVolumeStatusCB *VolumeStatus)
3063 {
3064
3065     NTSTATUS ntStatus = STATUS_SUCCESS;
3066     AFSDeviceExt *pDevExt = (AFSDeviceExt *) AFSRDRDeviceObject->DeviceExtension;
3067     ULONGLONG   ullIndex = 0;
3068     AFSVolumeCB *pVolumeCB = NULL;
3069     AFSFcb *pFcb = NULL;
3070     AFSObjectInfoCB *pCurrentObject = NULL;
3071
3072     __Enter
3073     {
3074
3075         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3076                       AFS_TRACE_LEVEL_VERBOSE,
3077                       "AFSSetVolumeState Marking volume state %d Volume Cell %08lX Volume %08lX\n",
3078                       VolumeStatus->Online,
3079                       VolumeStatus->FileID.Cell,
3080                       VolumeStatus->FileID.Volume);
3081
3082         //
3083         // Need to locate the Fcb for the directory to purge
3084         //
3085
3086         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3087                       AFS_TRACE_LEVEL_VERBOSE,
3088                       "AFSSetVolumeState Acquiring RDR VolumeTreeLock lock %08lX SHARED %08lX\n",
3089                       &pDevExt->Specific.RDR.VolumeTreeLock,
3090                       PsGetCurrentThread());
3091
3092         AFSAcquireShared( &pDevExt->Specific.RDR.VolumeTreeLock, TRUE);
3093
3094         //
3095         // Locate the volume node
3096         //
3097
3098         ullIndex = AFSCreateHighIndex( &VolumeStatus->FileID);
3099
3100         ntStatus = AFSLocateHashEntry( pDevExt->Specific.RDR.VolumeTree.TreeHead,
3101                                        ullIndex,
3102                                        (AFSBTreeEntry **)&pVolumeCB);
3103
3104         if( pVolumeCB != NULL)
3105         {
3106
3107             InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
3108
3109             AFSReleaseResource( &pDevExt->Specific.RDR.VolumeTreeLock);
3110
3111             //
3112             // Set the volume state accordingly
3113             //
3114
3115             if( VolumeStatus->Online)
3116             {
3117
3118                 InterlockedAnd( (LONG *)&(pVolumeCB->Flags), ~AFS_VOLUME_FLAGS_OFFLINE);
3119             }
3120             else
3121             {
3122
3123                 InterlockedOr( (LONG *)&(pVolumeCB->Flags), AFS_VOLUME_FLAGS_OFFLINE);
3124             }
3125
3126             AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
3127                               TRUE);
3128
3129             pCurrentObject = pVolumeCB->ObjectInfoListHead;;
3130
3131             while( pCurrentObject != NULL)
3132             {
3133
3134                 if( VolumeStatus->Online)
3135                 {
3136
3137                     ClearFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID);
3138
3139                     SetFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_VERIFY);
3140
3141                     pCurrentObject->DataVersion.QuadPart = (ULONGLONG)-1;
3142                 }
3143                 else
3144                 {
3145
3146                     SetFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID);
3147                 }
3148
3149                 pFcb = pCurrentObject->Fcb;
3150
3151                 if( pFcb != NULL &&
3152                     !(VolumeStatus->Online) &&
3153                     pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
3154                 {
3155
3156                     AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
3157                                   AFS_TRACE_LEVEL_ERROR,
3158                                   "AFSSetVolumeState Marking volume offline and canceling extents Volume Cell %08lX Volume %08lX\n",
3159                                   VolumeStatus->FileID.Cell,
3160                                   VolumeStatus->FileID.Volume);
3161
3162                     //
3163                     // Clear out the extents
3164                     //
3165
3166                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3167                                   AFS_TRACE_LEVEL_VERBOSE,
3168                                   "AFSSetVolumeState Acquiring Fcb extents lock %08lX EXCL %08lX\n",
3169                                   &pFcb->NPFcb->Specific.File.ExtentsResource,
3170                                   PsGetCurrentThread());
3171
3172                     AFSAcquireExcl( &pFcb->NPFcb->Specific.File.ExtentsResource,
3173                                     TRUE);
3174
3175                     pFcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_CANCELLED;
3176
3177                     KeSetEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete,
3178                                 0,
3179                                 FALSE);
3180
3181                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3182                                   AFS_TRACE_LEVEL_VERBOSE,
3183                                   "AFSSetVolumeState Releasing Fcb extents lock %08lX EXCL %08lX\n",
3184                                   &pFcb->NPFcb->Specific.File.ExtentsResource,
3185                                   PsGetCurrentThread());
3186
3187                     AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource);
3188
3189                     //
3190                     // And get rid of them (note this involves waiting
3191                     // for any writes or reads to the cache to complete)
3192                     //
3193
3194                     (VOID) AFSTearDownFcbExtents( pFcb);
3195                 }
3196
3197                 pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
3198             }
3199
3200             AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
3201
3202             InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
3203         }
3204         else
3205         {
3206
3207             AFSReleaseResource( &pDevExt->Specific.RDR.VolumeTreeLock);
3208         }
3209     }
3210
3211     return ntStatus;
3212 }
3213
3214 NTSTATUS
3215 AFSSetNetworkState( IN AFSNetworkStatusCB *NetworkStatus)
3216 {
3217
3218     NTSTATUS ntStatus = STATUS_SUCCESS;
3219
3220     __Enter
3221     {
3222
3223         if( AFSGlobalRoot == NULL)
3224         {
3225
3226             try_return( ntStatus);
3227         }
3228
3229         AFSAcquireExcl( AFSGlobalRoot->VolumeLock,
3230                         TRUE);
3231
3232         //
3233         // Set the network state according to the information
3234         //
3235
3236         if( NetworkStatus->Online)
3237         {
3238
3239             ClearFlag( AFSGlobalRoot->Flags, AFS_VOLUME_FLAGS_OFFLINE);
3240         }
3241         else
3242         {
3243
3244             SetFlag( AFSGlobalRoot->Flags, AFS_VOLUME_FLAGS_OFFLINE);
3245         }
3246
3247         AFSReleaseResource( AFSGlobalRoot->VolumeLock);
3248
3249 try_exit:
3250
3251         NOTHING;
3252     }
3253
3254     return ntStatus;
3255 }
3256
3257 NTSTATUS
3258 AFSValidateDirectoryCache( IN AFSObjectInfoCB *ObjectInfo,
3259                            IN GUID *AuthGroup)
3260 {
3261
3262     NTSTATUS ntStatus = STATUS_SUCCESS;
3263     BOOLEAN  bAcquiredLock = FALSE;
3264     AFSDirectoryCB *pCurrentDirEntry = NULL, *pNextDirEntry = NULL;
3265     AFSFcb *pFcb = NULL;
3266
3267     __Enter
3268     {
3269
3270         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3271                       AFS_TRACE_LEVEL_VERBOSE,
3272                       "AFSValidateDirectoryCache Validating content for FID %08lX-%08lX-%08lX-%08lX\n",
3273                       ObjectInfo->FileId.Cell,
3274                       ObjectInfo->FileId.Volume,
3275                       ObjectInfo->FileId.Vnode,
3276                       ObjectInfo->FileId.Unique);
3277
3278         if( !ExIsResourceAcquiredLite( ObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock))
3279         {
3280
3281             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3282                           AFS_TRACE_LEVEL_VERBOSE,
3283                           "AFSValidateDirectoryCache Acquiring DirectoryNodeHdr.TreeLock lock %08lX EXCL %08lX\n",
3284                           ObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
3285                           PsGetCurrentThread());
3286
3287             AFSAcquireExcl( ObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
3288                             TRUE);
3289
3290             bAcquiredLock = TRUE;
3291         }
3292
3293         //
3294         // Check for inconsistency between DirectoryNodeList and DirectoryNodeCount
3295         //
3296
3297         if ( ObjectInfo->Specific.Directory.DirectoryNodeListHead == NULL &&
3298              ObjectInfo->Specific.Directory.DirectoryNodeCount > 0)
3299         {
3300
3301             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3302                           AFS_TRACE_LEVEL_ERROR,
3303                           "AFSValidateDirectoryCache Empty Node List but Non-Zero Node Count %08lX for dir FID %08lX-%08lX-%08lX-%08lX\n",
3304                           ObjectInfo->Specific.Directory.DirectoryNodeCount,
3305                           ObjectInfo->FileId.Cell,
3306                           ObjectInfo->FileId.Volume,
3307                           ObjectInfo->FileId.Vnode,
3308                           ObjectInfo->FileId.Unique);
3309         }
3310
3311         //
3312         // Reset the directory list information by clearing all valid entries
3313         //
3314
3315         pCurrentDirEntry = ObjectInfo->Specific.Directory.DirectoryNodeListHead;
3316
3317         while( pCurrentDirEntry != NULL)
3318         {
3319
3320             if( !BooleanFlagOn( pCurrentDirEntry->Flags, AFS_DIR_ENTRY_FAKE))
3321             {
3322
3323                 ClearFlag( pCurrentDirEntry->Flags, AFS_DIR_ENTRY_VALID);
3324             }
3325
3326             pCurrentDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
3327         }
3328
3329         //
3330         // Reget the directory contents
3331         //
3332
3333         AFSVerifyDirectoryContent( ObjectInfo,
3334                                    AuthGroup);
3335
3336         //
3337         // Now start again and tear down any entries not valid
3338         //
3339
3340         pCurrentDirEntry = ObjectInfo->Specific.Directory.DirectoryNodeListHead;
3341
3342         while( pCurrentDirEntry != NULL)
3343         {
3344
3345             pNextDirEntry = (AFSDirectoryCB *)pCurrentDirEntry->ListEntry.fLink;
3346
3347             if( BooleanFlagOn( pCurrentDirEntry->Flags, AFS_DIR_ENTRY_VALID))
3348             {
3349
3350                 pCurrentDirEntry = pNextDirEntry;
3351
3352                 continue;
3353             }
3354
3355             if( pCurrentDirEntry->OpenReferenceCount == 0)
3356             {
3357
3358                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3359                               AFS_TRACE_LEVEL_VERBOSE,
3360                               "AFSValidateDirectoryCache Deleting dir entry %wZ from parent FID %08lX-%08lX-%08lX-%08lX\n",
3361                               &pCurrentDirEntry->NameInformation.FileName,
3362                               ObjectInfo->FileId.Cell,
3363                               ObjectInfo->FileId.Volume,
3364                               ObjectInfo->FileId.Vnode,
3365                               ObjectInfo->FileId.Unique);
3366
3367                 AFSDeleteDirEntry( ObjectInfo,
3368                                    pCurrentDirEntry);
3369             }
3370             else
3371             {
3372
3373                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3374                               AFS_TRACE_LEVEL_VERBOSE,
3375                               "AFSValidateDirectoryCache Setting dir entry %p Name %wZ DELETED in parent FID %08lX-%08lX-%08lX-%08lX\n",
3376                               pCurrentDirEntry,
3377                               &pCurrentDirEntry->NameInformation.FileName,
3378                               ObjectInfo->FileId.Cell,
3379                               ObjectInfo->FileId.Volume,
3380                               ObjectInfo->FileId.Vnode,
3381                               ObjectInfo->FileId.Unique);
3382
3383                 SetFlag( pCurrentDirEntry->Flags, AFS_DIR_ENTRY_DELETED);
3384
3385                 AFSRemoveNameEntry( ObjectInfo,
3386                                     pCurrentDirEntry);
3387             }
3388
3389             pCurrentDirEntry = pNextDirEntry;
3390         }
3391
3392 #if DBG
3393         if( !AFSValidateDirList( ObjectInfo))
3394         {
3395
3396             AFSPrint("AFSValidateDirectoryCache Invalid count ...\n");
3397         }
3398 #endif
3399
3400         if( bAcquiredLock)
3401         {
3402
3403             AFSReleaseResource( ObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
3404         }
3405     }
3406
3407     return ntStatus;
3408 }
3409
3410 BOOLEAN
3411 AFSIsVolumeFID( IN AFSFileID *FileID)
3412 {
3413
3414     BOOLEAN bIsVolume = FALSE;
3415
3416     if( FileID->Vnode == 1 &&
3417         FileID->Unique == 1)
3418     {
3419
3420         bIsVolume = TRUE;
3421     }
3422
3423     return bIsVolume;
3424 }
3425
3426 BOOLEAN
3427 AFSIsFinalNode( IN AFSFcb *Fcb)
3428 {
3429
3430     BOOLEAN bIsFinalNode = FALSE;
3431
3432     if( Fcb->Header.NodeTypeCode == AFS_ROOT_FCB ||
3433         Fcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB ||
3434         Fcb->Header.NodeTypeCode == AFS_FILE_FCB ||
3435         Fcb->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
3436         Fcb->Header.NodeTypeCode == AFS_INVALID_FCB )
3437     {
3438
3439         bIsFinalNode = TRUE;
3440     }
3441     else
3442     {
3443
3444         ASSERT( Fcb->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
3445                 Fcb->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB);
3446     }
3447
3448     return bIsFinalNode;
3449 }
3450
3451 NTSTATUS
3452 AFSUpdateMetaData( IN AFSDirectoryCB *DirEntry,
3453                    IN AFSDirEnumEntry *DirEnumEntry)
3454 {
3455
3456     NTSTATUS ntStatus = STATUS_SUCCESS;
3457     UNICODE_STRING uniTargetName;
3458     AFSObjectInfoCB *pObjectInfo = DirEntry->ObjectInformation;
3459
3460     __Enter
3461     {
3462
3463         pObjectInfo->TargetFileId = DirEnumEntry->TargetFileId;
3464
3465         pObjectInfo->Expiration = DirEnumEntry->Expiration;
3466
3467         pObjectInfo->DataVersion = DirEnumEntry->DataVersion;
3468
3469         pObjectInfo->FileType = DirEnumEntry->FileType;
3470
3471         pObjectInfo->CreationTime = DirEnumEntry->CreationTime;
3472
3473         pObjectInfo->LastAccessTime = DirEnumEntry->LastAccessTime;
3474
3475         pObjectInfo->LastWriteTime = DirEnumEntry->LastWriteTime;
3476
3477         pObjectInfo->ChangeTime = DirEnumEntry->ChangeTime;
3478
3479         pObjectInfo->EndOfFile = DirEnumEntry->EndOfFile;
3480
3481         pObjectInfo->AllocationSize = DirEnumEntry->AllocationSize;
3482
3483         pObjectInfo->FileAttributes = DirEnumEntry->FileAttributes;
3484
3485         if( pObjectInfo->FileType == AFS_FILE_TYPE_MOUNTPOINT ||
3486             pObjectInfo->FileType == AFS_FILE_TYPE_SYMLINK ||
3487             pObjectInfo->FileType == AFS_FILE_TYPE_DFSLINK)
3488         {
3489
3490             pObjectInfo->FileAttributes |= FILE_ATTRIBUTE_REPARSE_POINT;
3491         }
3492
3493         pObjectInfo->EaSize = DirEnumEntry->EaSize;
3494
3495         pObjectInfo->Links = DirEnumEntry->Links;
3496
3497         if( DirEnumEntry->TargetNameLength > 0)
3498         {
3499
3500             //
3501             // Update the target name information if needed
3502             //
3503
3504             uniTargetName.Length = (USHORT)DirEnumEntry->TargetNameLength;
3505
3506             uniTargetName.MaximumLength = uniTargetName.Length;
3507
3508             uniTargetName.Buffer = (WCHAR *)((char *)DirEnumEntry + DirEnumEntry->TargetNameOffset);
3509
3510             AFSAcquireExcl( &DirEntry->NonPaged->Lock,
3511                             TRUE);
3512
3513             if( DirEntry->NameInformation.TargetName.Length == 0 ||
3514                 RtlCompareUnicodeString( &uniTargetName,
3515                                          &DirEntry->NameInformation.TargetName,
3516                                          TRUE) != 0)
3517             {
3518
3519                 //
3520                 // Update the target name
3521                 //
3522
3523                 ntStatus = AFSUpdateTargetName( &DirEntry->NameInformation.TargetName,
3524                                                 &DirEntry->Flags,
3525                                                 uniTargetName.Buffer,
3526                                                 uniTargetName.Length);
3527
3528                 if( !NT_SUCCESS( ntStatus))
3529                 {
3530
3531                     AFSReleaseResource( &DirEntry->NonPaged->Lock);
3532
3533                     try_return( ntStatus);
3534                 }
3535             }
3536
3537             AFSReleaseResource( &DirEntry->NonPaged->Lock);
3538         }
3539         else if( DirEntry->NameInformation.TargetName.Length > 0)
3540         {
3541
3542             AFSAcquireExcl( &DirEntry->NonPaged->Lock,
3543                             TRUE);
3544
3545             if( BooleanFlagOn( DirEntry->Flags, AFS_DIR_RELEASE_TARGET_NAME_BUFFER) &&
3546                 DirEntry->NameInformation.TargetName.Buffer != NULL)
3547             {
3548                 AFSExFreePool( DirEntry->NameInformation.TargetName.Buffer);
3549             }
3550
3551             ClearFlag( DirEntry->Flags, AFS_DIR_RELEASE_TARGET_NAME_BUFFER);
3552
3553             DirEntry->NameInformation.TargetName.Length = 0;
3554             DirEntry->NameInformation.TargetName.MaximumLength = 0;
3555             DirEntry->NameInformation.TargetName.Buffer = NULL;
3556
3557             AFSReleaseResource( &DirEntry->NonPaged->Lock);
3558         }
3559
3560 try_exit:
3561
3562         NOTHING;
3563     }
3564
3565     return ntStatus;
3566 }
3567
3568 NTSTATUS
3569 AFSValidateEntry( IN AFSDirectoryCB *DirEntry,
3570                   IN GUID *AuthGroup,
3571                   IN BOOLEAN PurgeContent,
3572                   IN BOOLEAN FastCall)
3573 {
3574
3575     NTSTATUS ntStatus = STATUS_SUCCESS;
3576     LARGE_INTEGER liSystemTime;
3577     AFSDirEnumEntry *pDirEnumEntry = NULL;
3578     AFSFcb *pCurrentFcb = NULL;
3579     BOOLEAN bReleaseFcb = FALSE;
3580     AFSObjectInfoCB *pObjectInfo = DirEntry->ObjectInformation;
3581
3582     __Enter
3583     {
3584
3585         //
3586         // If we have an Fcb hanging off the directory entry then be sure to acquire the locks in the
3587         // correct order
3588         //
3589
3590         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3591                       AFS_TRACE_LEVEL_VERBOSE_2,
3592                       "AFSValidateEntry Validating entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
3593                       &DirEntry->NameInformation.FileName,
3594                       pObjectInfo->FileId.Cell,
3595                       pObjectInfo->FileId.Volume,
3596                       pObjectInfo->FileId.Vnode,
3597                       pObjectInfo->FileId.Unique);
3598
3599         //
3600         // If this is a fake node then bail since the service knows nothing about it
3601         //
3602
3603         if( BooleanFlagOn( DirEntry->Flags, AFS_DIR_ENTRY_FAKE))
3604         {
3605
3606             try_return( ntStatus);
3607         }
3608
3609         if( PurgeContent &&
3610             pObjectInfo->Fcb != NULL)
3611         {
3612
3613             pCurrentFcb = pObjectInfo->Fcb;
3614
3615             if( !ExIsResourceAcquiredLite( &pCurrentFcb->NPFcb->Resource))
3616             {
3617
3618                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3619                               AFS_TRACE_LEVEL_VERBOSE,
3620                               "AFSValidateEntry Acquiring Fcb lock %08lX EXCL %08lX\n",
3621                               &pCurrentFcb->NPFcb->Resource,
3622                               PsGetCurrentThread());
3623
3624                 AFSAcquireExcl( &pCurrentFcb->NPFcb->Resource,
3625                                 TRUE);
3626
3627                 bReleaseFcb = TRUE;
3628             }
3629         }
3630
3631         //
3632         // This routine ensures that the current entry is valid by:
3633         //
3634         //      1) Checking that the expiration time is non-zero and after where we
3635         //         currently are
3636         //
3637
3638         KeQuerySystemTime( &liSystemTime);
3639
3640         if( !BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED) &&
3641             !BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY) &&
3642             !BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA) &&
3643             pObjectInfo->Expiration.QuadPart >= liSystemTime.QuadPart)
3644         {
3645
3646             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3647                           AFS_TRACE_LEVEL_VERBOSE_2,
3648                           "AFSValidateEntry Directory entry %wZ FID %08lX-%08lX-%08lX-%08lX VALID\n",
3649                           &DirEntry->NameInformation.FileName,
3650                           pObjectInfo->FileId.Cell,
3651                           pObjectInfo->FileId.Volume,
3652                           pObjectInfo->FileId.Vnode,
3653                           pObjectInfo->FileId.Unique);
3654
3655             try_return( ntStatus);
3656         }
3657
3658         //
3659         // This node requires updating
3660         //
3661
3662         ntStatus = AFSEvaluateTargetByID( pObjectInfo,
3663                                           AuthGroup,
3664                                           FastCall,
3665                                           &pDirEnumEntry);
3666
3667         if( !NT_SUCCESS( ntStatus))
3668         {
3669
3670             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3671                           AFS_TRACE_LEVEL_ERROR,
3672                           "AFSValidateEntry Failed to evaluate entry %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
3673                           &DirEntry->NameInformation.FileName,
3674                           pObjectInfo->FileId.Cell,
3675                           pObjectInfo->FileId.Volume,
3676                           pObjectInfo->FileId.Vnode,
3677                           pObjectInfo->FileId.Unique,
3678                           ntStatus);
3679
3680             //
3681             // Failed validation of node so return access-denied
3682             //
3683
3684             try_return( ntStatus);
3685         }
3686
3687         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3688                       AFS_TRACE_LEVEL_VERBOSE,
3689                       "AFSValidateEntry Validating entry %wZ FID %08lX-%08lX-%08lX-%08lX DV %I64X returned DV %I64X FT %d\n",
3690                       &DirEntry->NameInformation.FileName,
3691                       pObjectInfo->FileId.Cell,
3692                       pObjectInfo->FileId.Volume,
3693                       pObjectInfo->FileId.Vnode,
3694                       pObjectInfo->FileId.Unique,
3695                       pObjectInfo->DataVersion.QuadPart,
3696                       pDirEnumEntry->DataVersion.QuadPart,
3697                       pDirEnumEntry->FileType);
3698
3699
3700         //
3701         // Based on the file type, process the node
3702         //
3703
3704         switch( pDirEnumEntry->FileType)
3705         {
3706
3707             case AFS_FILE_TYPE_MOUNTPOINT:
3708             {
3709
3710                 //
3711                 // Update the metadata for the entry
3712                 //
3713
3714                 ntStatus = AFSUpdateMetaData( DirEntry,
3715                                               pDirEnumEntry);
3716
3717                 if( NT_SUCCESS( ntStatus))
3718                 {
3719
3720                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY | AFS_OBJECT_FLAGS_NOT_EVALUATED);
3721                 }
3722
3723                 break;
3724             }
3725
3726             case AFS_FILE_TYPE_SYMLINK:
3727             case AFS_FILE_TYPE_DFSLINK:
3728             {
3729
3730                 //
3731                 // Update the metadata for the entry
3732                 //
3733
3734                 ntStatus = AFSUpdateMetaData( DirEntry,
3735                                               pDirEnumEntry);
3736
3737                 if( NT_SUCCESS( ntStatus))
3738                 {
3739
3740                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY | AFS_OBJECT_FLAGS_NOT_EVALUATED);
3741                 }
3742
3743                 break;
3744             }
3745
3746             case AFS_FILE_TYPE_FILE:
3747             {
3748
3749                 //
3750                 // For a file where the data version has become invalid we need to
3751                 // fail any current extent requests and purge the cache for the file
3752                 // Can't hold the Fcb resource while doing this
3753                 //
3754
3755                 if( pCurrentFcb != NULL &&
3756                     (pObjectInfo->DataVersion.QuadPart != pDirEnumEntry->DataVersion.QuadPart ||
3757                     BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY_DATA)))
3758