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