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