5727b813ecd3123ab0f702aaa39b9eb21f1dfd1f
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSNameSupport.cpp
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3  * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  *   this list of conditions and the following disclaimer.
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice,
14  *   this list of conditions and the following disclaimer in the
15  *   documentation
16  *   and/or other materials provided with the distribution.
17  * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
18  *   nor the names of their contributors may be used to endorse or promote
19  *   products derived from this software without specific prior written
20  *   permission from Kernel Drivers, LLC and Your File System, Inc.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 //
36 // File: AFSNameSupport.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 NTSTATUS
42 AFSLocateNameEntry( IN GUID *AuthGroup,
43                     IN PFILE_OBJECT FileObject,
44                     IN UNICODE_STRING *RootPathName,
45                     IN UNICODE_STRING *ParsedPathName,
46                     IN AFSNameArrayHdr *NameArray,
47                     IN ULONG Flags,
48                     OUT AFSVolumeCB **VolumeCB,
49                     IN OUT AFSDirectoryCB **ParentDirectoryCB,
50                     OUT AFSDirectoryCB **DirectoryCB,
51                     OUT PUNICODE_STRING ComponentName)
52 {
53
54     NTSTATUS          ntStatus = STATUS_SUCCESS;
55     UNICODE_STRING    uniPathName, uniComponentName, uniRemainingPath, uniSearchName, uniFullPathName;
56     ULONG             ulCRC = 0;
57     AFSDirectoryCB   *pDirEntry = NULL, *pParentDirEntry = NULL;
58     AFSDeviceExt *pDevExt = (AFSDeviceExt *) AFSRDRDeviceObject->DeviceExtension;
59     UNICODE_STRING    uniSysName;
60     ULONG             ulSubstituteIndex = 0;
61     BOOLEAN           bSubstituteName = FALSE;
62     AFSNameArrayHdr  *pNameArray = NameArray;
63     BOOLEAN           bAllocatedSymLinkBuffer = FALSE;
64     UNICODE_STRING    uniRelativeName, uniNoOpName;
65     AFSObjectInfoCB  *pCurrentObject = NULL;
66     AFSObjectInfoCB  *pParentObjectInfo = NULL;
67     AFSVolumeCB      *pCurrentVolume = *VolumeCB;
68     BOOLEAN           bReleaseCurrentVolume = TRUE;
69     BOOLEAN           bSubstitutedName = FALSE;
70     LONG              lCount;
71
72     __Enter
73     {
74
75         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
76                       AFS_TRACE_LEVEL_VERBOSE_2,
77                       "AFSLocateNameEntry (FO: %08lX) Processing full name %wZ\n",
78                       FileObject,
79                       RootPathName);
80
81         RtlInitUnicodeString( &uniSysName,
82                               L"*@SYS");
83
84         RtlInitUnicodeString( &uniRelativeName,
85                               L"..");
86
87         RtlInitUnicodeString( &uniNoOpName,
88                               L".");
89
90         //
91         // Cleanup some parameters
92         //
93
94         if( ComponentName != NULL)
95         {
96
97             ComponentName->Length = 0;
98             ComponentName->MaximumLength = 0;
99             ComponentName->Buffer = NULL;
100         }
101
102         //
103         // We will parse through the filename, locating the directory nodes until we encounter a cache miss
104         // Starting at the root node
105         //
106
107         pParentDirEntry = NULL;
108
109         pDirEntry = *ParentDirectoryCB;
110
111         uniPathName = *ParsedPathName;
112
113         uniFullPathName = *RootPathName;
114
115         uniComponentName.Length = uniComponentName.MaximumLength = 0;
116         uniComponentName.Buffer = NULL;
117
118         uniRemainingPath.Length = uniRemainingPath.MaximumLength = 0;
119         uniRemainingPath.Buffer = NULL;
120
121         uniSearchName.Length = uniSearchName.MaximumLength = 0;
122         uniSearchName.Buffer = NULL;
123
124         ASSERT( pCurrentVolume->VolumeReferenceCount > 1);
125
126         while( TRUE)
127         {
128
129             //
130             // Check our total link count for this name array
131             //
132
133             if( pNameArray->LinkCount >= (LONG)pDevExt->Specific.RDR.MaxLinkCount)
134             {
135
136                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
137             }
138
139             pCurrentObject = pDirEntry->ObjectInformation;
140
141             KeQueryTickCount( &pCurrentObject->LastAccessCount);
142
143             //
144             // Check that the directory entry is not deleted or pending delete
145             //
146
147             if( BooleanFlagOn( pDirEntry->Flags, AFS_DIR_ENTRY_DELETED))
148             {
149
150                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
151                               AFS_TRACE_LEVEL_ERROR,
152                               "AFSLocateNameEntry (FO: %08lX) Deleted parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
153                               FileObject,
154                               &pDirEntry->NameInformation.FileName,
155                               pCurrentObject->FileId.Cell,
156                               pCurrentObject->FileId.Volume,
157                               pCurrentObject->FileId.Vnode,
158                               pCurrentObject->FileId.Unique,
159                               STATUS_FILE_DELETED);
160
161                 try_return( ntStatus = STATUS_FILE_DELETED);
162             }
163
164             if( BooleanFlagOn( pDirEntry->Flags, AFS_DIR_ENTRY_PENDING_DELETE))
165             {
166
167                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
168                               AFS_TRACE_LEVEL_ERROR,
169                               "AFSLocateNameEntry (FO: %08lX) Delete pending on %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
170                               FileObject,
171                               &pDirEntry->NameInformation.FileName,
172                               pCurrentObject->FileId.Cell,
173                               pCurrentObject->FileId.Volume,
174                               pCurrentObject->FileId.Vnode,
175                               pCurrentObject->FileId.Unique,
176                               STATUS_DELETE_PENDING);
177
178                 try_return( ntStatus = STATUS_DELETE_PENDING);
179             }
180
181             //
182             // Check if the directory requires verification
183             //
184
185             if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_VERIFY) &&
186                 ( pCurrentObject->FileType != AFS_FILE_TYPE_DIRECTORY ||
187                   !AFSIsEnumerationInProcess( pCurrentObject)))
188             {
189
190                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
191                               AFS_TRACE_LEVEL_VERBOSE,
192                               "AFSLocateNameEntry (FO: %08lX) Verifying parent %wZ FID %08lX-%08lX-%08lX-%08lX\n",
193                               FileObject,
194                               &pDirEntry->NameInformation.FileName,
195                               pCurrentObject->FileId.Cell,
196                               pCurrentObject->FileId.Volume,
197                               pCurrentObject->FileId.Vnode,
198                               pCurrentObject->FileId.Unique);
199
200                 //
201                 // Directory TreeLock should be exclusively held
202                 //
203
204                 AFSAcquireExcl( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
205                                 TRUE);
206
207                 ntStatus = AFSVerifyEntry( AuthGroup,
208                                            pDirEntry);
209
210                 AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
211
212                 if( !NT_SUCCESS( ntStatus))
213                 {
214
215                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
216                                   AFS_TRACE_LEVEL_ERROR,
217                                   "AFSLocateNameEntry (FO: %08lX) Failed to verify parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
218                                   FileObject,
219                                   &pDirEntry->NameInformation.FileName,
220                                   pCurrentObject->FileId.Cell,
221                                   pCurrentObject->FileId.Volume,
222                                   pCurrentObject->FileId.Vnode,
223                                   pCurrentObject->FileId.Unique,
224                                   ntStatus);
225
226                     try_return( ntStatus);
227                 }
228             }
229
230             //
231             // Ensure the parent node has been evaluated, if not then go do it now
232             //
233
234             if( BooleanFlagOn( pDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED) ||
235                 pCurrentObject->FileType == AFS_FILE_TYPE_UNKNOWN)
236             {
237
238                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
239                               AFS_TRACE_LEVEL_VERBOSE,
240                               "AFSLocateNameEntry (FO: %08lX) Evaluating parent %wZ FID %08lX-%08lX-%08lX-%08lX\n",
241                               FileObject,
242                               &pDirEntry->NameInformation.FileName,
243                               pCurrentObject->FileId.Cell,
244                               pCurrentObject->FileId.Volume,
245                               pCurrentObject->FileId.Vnode,
246                               pCurrentObject->FileId.Unique);
247
248                 ntStatus = AFSEvaluateNode( AuthGroup,
249                                             pDirEntry);
250
251                 if( !NT_SUCCESS( ntStatus))
252                 {
253
254                     if ( ntStatus == STATUS_NOT_A_DIRECTORY)
255                     {
256
257                         if ( pCurrentObject->ParentObjectInformation == NULL)
258                         {
259
260                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
261                                           AFS_TRACE_LEVEL_ERROR,
262                                           "AFSLocateNameEntry (FO: %08lX) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT NULL Status %08lX\n",
263                                           FileObject,
264                                           &pDirEntry->NameInformation.FileName,
265                                           pCurrentObject->FileId.Cell,
266                                           pCurrentObject->FileId.Volume,
267                                           pCurrentObject->FileId.Vnode,
268                                           pCurrentObject->FileId.Unique,
269                                           ntStatus);
270                         }
271                         else
272                         {
273
274                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
275                                           AFS_TRACE_LEVEL_ERROR,
276                                           "AFSLocateNameEntry (FO: %08lX) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX Status %08lX\n",
277                                           FileObject,
278                                           &pDirEntry->NameInformation.FileName,
279                                           pCurrentObject->FileId.Cell,
280                                           pCurrentObject->FileId.Volume,
281                                           pCurrentObject->FileId.Vnode,
282                                           pCurrentObject->FileId.Unique,
283                                           pCurrentObject->ParentObjectInformation->FileId.Cell,
284                                           pCurrentObject->ParentObjectInformation->FileId.Volume,
285                                           pCurrentObject->ParentObjectInformation->FileId.Vnode,
286                                           pCurrentObject->ParentObjectInformation->FileId.Unique,
287                                           ntStatus);
288                         }
289                     }
290                     else
291                     {
292                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
293                                       AFS_TRACE_LEVEL_ERROR,
294                                       "AFSLocateNameEntry (FO: %08lX) Failed to evaluate parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
295                                       FileObject,
296                                       &pDirEntry->NameInformation.FileName,
297                                       pCurrentObject->FileId.Cell,
298                                       pCurrentObject->FileId.Volume,
299                                       pCurrentObject->FileId.Vnode,
300                                       pCurrentObject->FileId.Unique,
301                                       ntStatus);
302                     }
303
304                     try_return( ntStatus);
305                 }
306
307                 ClearFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED);
308             }
309
310             //
311             // If this is a mount point or symlink then go get the real directory node
312             //
313
314             switch( pCurrentObject->FileType)
315             {
316
317                 case AFS_FILE_TYPE_SYMLINK:
318                 {
319
320                     UNICODE_STRING uniTempName;
321                     WCHAR *pTmpBuffer = NULL;
322                     LONG lLinkCount = 0;
323
324                     //
325                     // Check if the flag is set to NOT evaluate a symlink
326                     // and we are done with the parsing
327                     //
328
329                     if( BooleanFlagOn( Flags, AFS_LOCATE_FLAGS_NO_SL_TARGET_EVAL) &&
330                         uniRemainingPath.Length == 0)
331                     {
332
333                         //
334                         // Pass back the directory entries
335                         //
336
337                         *ParentDirectoryCB = pParentDirEntry;
338
339                         *DirectoryCB = pDirEntry;
340
341                         *VolumeCB = pCurrentVolume;
342
343                         *RootPathName = uniFullPathName;
344
345                         try_return( ntStatus);
346                     }
347
348                     AFSAcquireExcl( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
349                                     TRUE);
350
351                     AFSAcquireExcl( &pDirEntry->NonPaged->Lock,
352                                     TRUE);
353
354                     if( pDirEntry->NameInformation.TargetName.Length == 0)
355                     {
356
357                         //
358                         // We'll reset the DV to ensure we validate the metadata content
359                         //
360
361                         pCurrentObject->DataVersion.QuadPart = (ULONGLONG)-1;
362
363                         SetFlag( pCurrentObject->Flags, AFS_OBJECT_FLAGS_VERIFY);
364
365                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
366                                       AFS_TRACE_LEVEL_VERBOSE,
367                                       "AFSLocateNameEntry (FO: %08lX) Verifying symlink parent %wZ FID %08lX-%08lX-%08lX-%08lX\n",
368                                       FileObject,
369                                       &pDirEntry->NameInformation.FileName,
370                                       pCurrentObject->FileId.Cell,
371                                       pCurrentObject->FileId.Volume,
372                                       pCurrentObject->FileId.Vnode,
373                                       pCurrentObject->FileId.Unique);
374
375                         //
376                         // Directory TreeLock should be exclusively held
377                         //
378
379                         ntStatus = AFSVerifyEntry( AuthGroup,
380                                                    pDirEntry);
381
382                         AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
383
384                         if( !NT_SUCCESS( ntStatus))
385                         {
386
387                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
388                                           AFS_TRACE_LEVEL_ERROR,
389                                           "AFSLocateNameEntry (FO: %08lX) Failed to verify symlink parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
390                                           FileObject,
391                                           &pDirEntry->NameInformation.FileName,
392                                           pCurrentObject->FileId.Cell,
393                                           pCurrentObject->FileId.Volume,
394                                           pCurrentObject->FileId.Vnode,
395                                           pCurrentObject->FileId.Unique,
396                                           ntStatus);
397
398                             AFSReleaseResource( &pDirEntry->NonPaged->Lock);
399
400                             try_return( ntStatus);
401                         }
402
403                         //
404                         // If the type changed then reprocess this entry
405                         //
406
407                         if( pCurrentObject->FileType != AFS_FILE_TYPE_SYMLINK)
408                         {
409
410                             AFSReleaseResource( &pDirEntry->NonPaged->Lock);
411
412                             continue;
413                         }
414                     }
415                     else
416                     {
417
418                         AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
419                     }
420
421                     //
422                     // If we were given a zero length target name then deny access to the entry
423                     //
424
425                     if( pDirEntry->NameInformation.TargetName.Length == 0)
426                     {
427
428                         ntStatus = STATUS_REPARSE_POINT_NOT_RESOLVED;
429
430                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
431                                       AFS_TRACE_LEVEL_ERROR,
432                                       "AFSLocateNameEntry (FO: %08lX) Failed to retrieve target name for symlink %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
433                                       FileObject,
434                                       &pDirEntry->NameInformation.FileName,
435                                       pCurrentObject->FileId.Cell,
436                                       pCurrentObject->FileId.Volume,
437                                       pCurrentObject->FileId.Vnode,
438                                       pCurrentObject->FileId.Unique,
439                                       ntStatus);
440
441                         AFSReleaseResource( &pDirEntry->NonPaged->Lock);
442
443                         try_return( ntStatus);
444                     }
445
446                     if( AFSIsRelativeName( &pDirEntry->NameInformation.TargetName))
447                     {
448
449                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
450                                       AFS_TRACE_LEVEL_VERBOSE,
451                                       "AFSLocateNameEntry (FO: %08lX) Processing relative symlink target %wZ for %wZ FID %08lX-%08lX-%08lX-%08lX\n",
452                                       FileObject,
453                                       &pDirEntry->NameInformation.TargetName,
454                                       &pDirEntry->NameInformation.FileName,
455                                       pCurrentObject->FileId.Cell,
456                                       pCurrentObject->FileId.Volume,
457                                       pCurrentObject->FileId.Vnode,
458                                       pCurrentObject->FileId.Unique);
459
460                         //
461                         // We'll substitute this name into the current process name
462                         // starting at where we sit in the path
463                         //
464
465                         uniTempName.Length = 0;
466                         uniTempName.MaximumLength = (USHORT)((char *)uniComponentName.Buffer - (char *)uniFullPathName.Buffer) +
467                                                                     pDirEntry->NameInformation.TargetName.Length +
468                                                                     sizeof( WCHAR) +
469                                                                     uniRemainingPath.Length;
470
471                         uniTempName.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
472                                                                                 uniTempName.MaximumLength,
473                                                                                 AFS_NAME_BUFFER_ONE_TAG);
474
475                         if( uniTempName.Buffer == NULL)
476                         {
477
478                             AFSReleaseResource( &pDirEntry->NonPaged->Lock);
479
480                             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
481                         }
482
483                         //
484                         // We have so first copy in the portion up to the component
485                         // name
486                         //
487
488                         RtlCopyMemory( uniTempName.Buffer,
489                                        uniFullPathName.Buffer,
490                                        (ULONG)((char *)uniComponentName.Buffer - (char *)uniFullPathName.Buffer));
491
492                         uniTempName.Length = (USHORT)((char *)uniComponentName.Buffer - (char *)uniFullPathName.Buffer);
493
494                         if( bAllocatedSymLinkBuffer ||
495                             bSubstitutedName)
496                         {
497
498                             pTmpBuffer = uniFullPathName.Buffer;
499                         }
500
501                         bAllocatedSymLinkBuffer = TRUE;
502
503                         //
504                         // Have we parsed this name yet? Better have at least once ...
505                         //
506
507                         if( uniComponentName.Length == 0)
508                         {
509                             ASSERT( FALSE);
510                         }
511
512                         //
513                         // Copy in the target name ...
514                         //
515
516                         RtlCopyMemory( &uniTempName.Buffer[ uniTempName.Length/sizeof( WCHAR)],
517                                        pDirEntry->NameInformation.TargetName.Buffer,
518                                        pDirEntry->NameInformation.TargetName.Length);
519
520                         uniPathName.Buffer = &uniTempName.Buffer[ uniTempName.Length/sizeof( WCHAR)];
521
522                         uniPathName.Length += pDirEntry->NameInformation.TargetName.Length;
523                         uniPathName.MaximumLength = uniTempName.MaximumLength;
524
525                         uniTempName.Length += pDirEntry->NameInformation.TargetName.Length;
526
527                         //
528                         // And now any remaining portion of the name
529                         //
530
531                         if( uniRemainingPath.Length > 0)
532                         {
533
534                             if( uniRemainingPath.Buffer[ 0] != L'\\')
535                             {
536
537                                 uniRemainingPath.Buffer--;
538                                 uniRemainingPath.Length += sizeof( WCHAR);
539
540                                 uniPathName.Length += sizeof( WCHAR);
541                             }
542
543                             RtlCopyMemory( &uniTempName.Buffer[ uniTempName.Length/sizeof( WCHAR)],
544                                            uniRemainingPath.Buffer,
545                                            uniRemainingPath.Length);
546
547                             uniTempName.Length += uniRemainingPath.Length;
548                         }
549
550                         uniFullPathName = uniTempName;
551
552                         if( pTmpBuffer != NULL)
553                         {
554
555                             AFSExFreePoolWithTag( pTmpBuffer, 0);
556                         }
557
558                         AFSReleaseResource( &pDirEntry->NonPaged->Lock);
559
560                         //
561                         // Dereference the current entry ..
562                         //
563
564                         lCount = InterlockedDecrement( &pDirEntry->OpenReferenceCount);
565
566                         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
567                                       AFS_TRACE_LEVEL_VERBOSE,
568                                       "AFSLocateNameEntry Decrement1 count on %wZ DE %p Ccb %p Cnt %d\n",
569                                       &pDirEntry->NameInformation.FileName,
570                                       pDirEntry,
571                                       NULL,
572                                       lCount);
573
574                         //
575                         // OK, need to back up one entry for the correct parent since the current
576                         // entry we are on is the symlink itself
577                         //
578
579                         pDirEntry = AFSBackupEntry( pNameArray);
580
581                         //
582                         // Increment our reference on this dir entry
583                         //
584
585                         lCount = InterlockedIncrement( &pDirEntry->OpenReferenceCount);
586
587                         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
588                                       AFS_TRACE_LEVEL_VERBOSE,
589                                       "AFSLocateNameEntry Increment1 count on %wZ DE %p Ccb %p Cnt %d\n",
590                                       &pDirEntry->NameInformation.FileName,
591                                       pDirEntry,
592                                       NULL,
593                                       lCount);
594
595                         if( BooleanFlagOn( pDirEntry->ObjectInformation->Flags, AFS_OBJECT_ROOT_VOLUME))
596                         {
597
598                             pParentDirEntry = NULL;
599                         }
600                         else
601                         {
602
603                             pParentDirEntry = AFSGetParentEntry( pNameArray);
604
605                             ASSERT( pParentDirEntry != pDirEntry);
606                         }
607                     }
608                     else
609                     {
610
611                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
612                                       AFS_TRACE_LEVEL_VERBOSE,
613                                       "AFSLocateNameEntry (FO: %08lX) Processing absolute symlink target %wZ for %wZ FID %08lX-%08lX-%08lX-%08lX\n",
614                                       FileObject,
615                                       &pDirEntry->NameInformation.TargetName,
616                                       &pDirEntry->NameInformation.FileName,
617                                       pCurrentObject->FileId.Cell,
618                                       pCurrentObject->FileId.Volume,
619                                       pCurrentObject->FileId.Vnode,
620                                       pCurrentObject->FileId.Unique);
621
622                         //
623                         // We'll substitute this name into the current process name
624                         // starting at where we sit in the path
625                         //
626
627                         uniTempName.Length = 0;
628                         uniTempName.MaximumLength = pDirEntry->NameInformation.TargetName.Length +
629                                                                     sizeof( WCHAR) +
630                                                                     uniRemainingPath.Length;
631
632                         uniTempName.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
633                                                                                 uniTempName.MaximumLength,
634                                                                                 AFS_NAME_BUFFER_TWO_TAG);
635
636                         if( uniTempName.Buffer == NULL)
637                         {
638
639                             AFSReleaseResource( &pDirEntry->NonPaged->Lock);
640
641                             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
642                         }
643
644                         if( bAllocatedSymLinkBuffer ||
645                             bSubstitutedName)
646                         {
647
648                             pTmpBuffer = uniFullPathName.Buffer;
649                         }
650
651                         bAllocatedSymLinkBuffer = TRUE;
652
653                         //
654                         // Have we parsed this name yet? Better have at least once ...
655                         //
656
657                         if( uniComponentName.Length == 0)
658                         {
659                             ASSERT( FALSE);
660                         }
661
662                         //
663                         // Copy in the target name ...
664                         //
665
666                         RtlCopyMemory( uniTempName.Buffer,
667                                        pDirEntry->NameInformation.TargetName.Buffer,
668                                        pDirEntry->NameInformation.TargetName.Length);
669
670                         uniTempName.Length = pDirEntry->NameInformation.TargetName.Length;
671
672                         //
673                         // And now any remaining portion of the name
674                         //
675
676                         if( uniRemainingPath.Length > 0)
677                         {
678
679                             if( uniRemainingPath.Buffer[ 0] != L'\\')
680                             {
681
682                                 uniRemainingPath.Buffer--;
683                                 uniRemainingPath.Length += sizeof( WCHAR);
684                             }
685
686                             RtlCopyMemory( &uniTempName.Buffer[ uniTempName.Length/sizeof( WCHAR)],
687                                            uniRemainingPath.Buffer,
688                                            uniRemainingPath.Length);
689
690                             uniTempName.Length += uniRemainingPath.Length;
691                         }
692
693                         uniFullPathName = uniTempName;
694
695                         uniPathName = uniTempName;
696
697                         if( pTmpBuffer != NULL)
698                         {
699
700                             AFSExFreePoolWithTag( pTmpBuffer, 0);
701                         }
702
703                         AFSReleaseResource( &pDirEntry->NonPaged->Lock);
704
705                         //
706                         // If our current volume is not the global root then make it so ...
707                         //
708
709                         if( pCurrentVolume != AFSGlobalRoot)
710                         {
711
712                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
713                                           AFS_TRACE_LEVEL_VERBOSE,
714                                           "AFSLocateNameEntry (FO: %08lX) Current volume not global, resetting for %wZ FID %08lX-%08lX-%08lX-%08lX\n",
715                                           FileObject,
716                                           &pDirEntry->NameInformation.FileName,
717                                           pCurrentObject->FileId.Cell,
718                                           pCurrentObject->FileId.Volume,
719                                           pCurrentObject->FileId.Vnode,
720                                           pCurrentObject->FileId.Unique);
721
722                             lCount = InterlockedDecrement( &pCurrentVolume->VolumeReferenceCount);
723
724                             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
725                                           AFS_TRACE_LEVEL_VERBOSE,
726                                           "AFSLocateNameEntry Decrement count on volume %08lX Cnt %d\n",
727                                           pCurrentVolume,
728                                           lCount);
729
730                             pCurrentVolume = AFSGlobalRoot;
731
732                             lCount = InterlockedIncrement( &pCurrentVolume->VolumeReferenceCount);
733
734                             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
735                                           AFS_TRACE_LEVEL_VERBOSE,
736                                           "AFSLocateNameEntry Increment count on volume %08lX Cnt %d\n",
737                                           pCurrentVolume,
738                                           lCount);
739                         }
740
741                         //
742                         // Dereference our current dir entry
743                         //
744
745                         lCount = InterlockedDecrement( &pDirEntry->OpenReferenceCount);
746
747                         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
748                                       AFS_TRACE_LEVEL_VERBOSE,
749                                       "AFSLocateNameEntry Decrement2 count on %wZ DE %p Ccb %p Cnt %d\n",
750                                       &pDirEntry->NameInformation.FileName,
751                                       pDirEntry,
752                                       NULL,
753                                       lCount);
754
755                         pDirEntry = pCurrentVolume->DirectoryCB;
756
757                         //
758                         // Reference the new dir entry
759                         //
760
761                         lCount = InterlockedIncrement( &pDirEntry->OpenReferenceCount);
762
763                         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
764                                       AFS_TRACE_LEVEL_VERBOSE,
765                                       "AFSLocateNameEntry Increment2 count on %wZ DE %p Ccb %p Cnt %d\n",
766                                       &pDirEntry->NameInformation.FileName,
767                                       pDirEntry,
768                                       NULL,
769                                       lCount);
770
771                         //
772                         // Reset the name array
773                         // Persist the link count in the name array
774                         //
775
776                         lLinkCount = pNameArray->LinkCount;
777
778                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
779                                       AFS_TRACE_LEVEL_VERBOSE,
780                                       "AFSLocateNameEntry (FO: %08lX) Resetting name array for %wZ FID %08lX-%08lX-%08lX-%08lX\n",
781                                       FileObject,
782                                       &pDirEntry->NameInformation.FileName,
783                                       pCurrentObject->FileId.Cell,
784                                       pCurrentObject->FileId.Volume,
785                                       pCurrentObject->FileId.Vnode,
786                                       pCurrentObject->FileId.Unique);
787
788                         AFSResetNameArray( pNameArray,
789                                            pDirEntry);
790
791                         pNameArray->LinkCount = lLinkCount;
792
793                         //
794                         // Process over the \\<Global root> portion of the name
795                         //
796
797                         FsRtlDissectName( uniPathName,
798                                           &uniComponentName,
799                                           &uniRemainingPath);
800
801                         if( RtlCompareUnicodeString( &uniComponentName,
802                                                      &AFSServerName,
803                                                      TRUE) != 0)
804                         {
805
806                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
807                                           AFS_TRACE_LEVEL_ERROR,
808                                           "AFSLocateNameEntry Name %wZ contains invalid server name\n",
809                                           &uniPathName);
810
811                             //
812                             // The correct response would be STATUS_OBJECT_PATH_INVALID
813                             // but that prevents cmd.exe from performing a recursive
814                             // directory enumeration when opening a directory entry
815                             // that represents a symlink to an invalid path is discovered.
816                             //
817                             try_return( ntStatus = STATUS_OBJECT_PATH_NOT_FOUND);
818                         }
819
820                         uniPathName = uniRemainingPath;
821
822                         pParentDirEntry = NULL;
823                     }
824
825                     //
826                     // Increment our link count
827                     //
828
829                     lCount = InterlockedIncrement( &pNameArray->LinkCount);
830
831                     continue;
832                 }
833
834                 case AFS_FILE_TYPE_MOUNTPOINT:
835                 {
836
837                     //
838                     // Check if the flag is set to NOT evaluate a mount point
839                     // and we are done with the parsing
840                     //
841
842                     if( BooleanFlagOn( Flags, AFS_LOCATE_FLAGS_NO_MP_TARGET_EVAL) &&
843                         uniRemainingPath.Length == 0)
844                     {
845
846                         //
847                         // Pass back the directory entries
848                         //
849
850                         *ParentDirectoryCB = pParentDirEntry;
851
852                         *DirectoryCB = pDirEntry;
853
854                         *VolumeCB = pCurrentVolume;
855
856                         *RootPathName = uniFullPathName;
857
858                         try_return( ntStatus);
859                     }
860
861                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
862                                   AFS_TRACE_LEVEL_VERBOSE,
863                                   "AFSLocateNameEntry (FO: %08lX) Building MP target for parent %wZ FID %08lX-%08lX-%08lX-%08lX\n",
864                                   FileObject,
865                                   &pDirEntry->NameInformation.FileName,
866                                   pCurrentObject->FileId.Cell,
867                                   pCurrentObject->FileId.Volume,
868                                   pCurrentObject->FileId.Vnode,
869                                   pCurrentObject->FileId.Unique);
870
871                     //
872                     // Go retrieve the target entry for this node
873                     // Release the current volume cb entry since we would
874                     // have lock inversion in the following call
875                     // Also decrement the ref count on the volume
876                     //
877
878                     ASSERT( pCurrentVolume->VolumeReferenceCount > 1);
879
880                     lCount = InterlockedDecrement( &pCurrentVolume->VolumeReferenceCount);
881
882                     AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
883                                   AFS_TRACE_LEVEL_VERBOSE,
884                                   "AFSLocateNameEntry Decrement2 count on volume %08lX Cnt %d\n",
885                                   pCurrentVolume,
886                                   pCurrentVolume->VolumeReferenceCount);
887
888                     ntStatus = AFSBuildMountPointTarget( AuthGroup,
889                                                          pDirEntry,
890                                                          &pCurrentVolume);
891
892                     if( !NT_SUCCESS( ntStatus))
893                     {
894
895                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
896                                       AFS_TRACE_LEVEL_ERROR,
897                                       "AFSLocateNameEntry (FO: %08lX) Failed to build MP target for parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
898                                       FileObject,
899                                       &pDirEntry->NameInformation.FileName,
900                                       pCurrentObject->FileId.Cell,
901                                       pCurrentObject->FileId.Volume,
902                                       pCurrentObject->FileId.Vnode,
903                                       pCurrentObject->FileId.Unique,
904                                       ntStatus);
905
906                         //
907                         // We already decremented the current volume above
908                         //
909
910                         bReleaseCurrentVolume = FALSE;
911
912                         try_return( ntStatus);
913                     }
914
915                     ASSERT( pCurrentVolume->VolumeReferenceCount > 1);
916
917                     //
918                     // The name array stores both the mount point and the target.
919                     // Insert the target.
920                     //
921
922                     AFSInsertNextElement( pNameArray,
923                                           pCurrentVolume->DirectoryCB);
924
925                     //
926                     // We want to restart processing here on the new parent ...
927                     // Deref and ref count the entries
928                     //
929
930                     lCount = InterlockedDecrement( &pDirEntry->OpenReferenceCount);
931
932                     AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
933                                   AFS_TRACE_LEVEL_VERBOSE,
934                                   "AFSLocateNameEntry Decrement3 count on %wZ DE %p Ccb %p Cnt %d\n",
935                                   &pDirEntry->NameInformation.FileName,
936                                   pDirEntry,
937                                   NULL,
938                                   lCount);
939
940                     pDirEntry = pCurrentVolume->DirectoryCB;
941
942                     lCount = InterlockedIncrement( &pDirEntry->OpenReferenceCount);
943
944                     AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
945                                   AFS_TRACE_LEVEL_VERBOSE,
946                                   "AFSLocateNameEntry Increment3 count on %wZ DE %p Ccb %p Cnt %d\n",
947                                   &pDirEntry->NameInformation.FileName,
948                                   pDirEntry,
949                                   NULL,
950                                   lCount);
951
952                     pParentDirEntry = NULL;
953
954                     //
955                     // Increment our link count
956                     //
957
958                     lCount = InterlockedIncrement( &pNameArray->LinkCount);
959
960                     continue;
961                 }
962
963                 case AFS_FILE_TYPE_DFSLINK:
964                 {
965
966                     if( BooleanFlagOn( Flags, AFS_LOCATE_FLAGS_NO_DFS_LINK_EVAL))
967                     {
968
969                         //
970                         // Pass back the directory entries
971                         //
972
973                         *ParentDirectoryCB = pParentDirEntry;
974
975                         *DirectoryCB = pDirEntry;
976
977                         *VolumeCB = pCurrentVolume;
978
979                         *RootPathName = uniFullPathName;
980
981                         try_return( ntStatus);
982                     }
983
984                     //
985                     // This is a DFS link so we need to update the file name and return STATUS_REPARSE to the
986                     // system for it to reevaluate it
987                     //
988
989                     if( FileObject != NULL)
990                     {
991
992                         ntStatus = AFSProcessDFSLink( pDirEntry,
993                                                       FileObject,
994                                                       &uniRemainingPath,
995                                                       AuthGroup);
996                     }
997                     else
998                     {
999
1000                         //
1001                         // This is where we have been re-entered from an NP evaluation call via the BuildBranch()
1002                         // routine.
1003                         //
1004
1005                         ntStatus = STATUS_INVALID_PARAMETER;
1006                     }
1007
1008                     if( ntStatus != STATUS_SUCCESS &&
1009                         ntStatus != STATUS_REPARSE)
1010                     {
1011
1012                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1013                                       AFS_TRACE_LEVEL_ERROR,
1014                                       "AFSLocateNameEntry (FO: %08lX) Failed to process DFSLink parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1015                                       FileObject,
1016                                       &pDirEntry->NameInformation.FileName,
1017                                       pCurrentObject->FileId.Cell,
1018                                       pCurrentObject->FileId.Volume,
1019                                       pCurrentObject->FileId.Vnode,
1020                                       pCurrentObject->FileId.Unique,
1021                                       ntStatus);
1022                     }
1023
1024                     try_return( ntStatus);
1025                 }
1026
1027                 case AFS_FILE_TYPE_UNKNOWN:
1028                 case AFS_FILE_TYPE_INVALID:
1029                 {
1030
1031                     //
1032                     // Something was not processed ...
1033                     //
1034
1035                     try_return( ntStatus = STATUS_ACCESS_DENIED);
1036                 }
1037
1038             }   /* end of switch */
1039
1040             //
1041             // If the parent is not initialized then do it now
1042             //
1043
1044             if( pCurrentObject->FileType == AFS_FILE_TYPE_DIRECTORY &&
1045                 !BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
1046             {
1047
1048                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1049                               AFS_TRACE_LEVEL_VERBOSE,
1050                               "AFSLocateNameEntry (FO: %08lX) Enumerating parent %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1051                               FileObject,
1052                               &pDirEntry->NameInformation.FileName,
1053                               pCurrentObject->FileId.Cell,
1054                               pCurrentObject->FileId.Volume,
1055                               pCurrentObject->FileId.Vnode,
1056                               pCurrentObject->FileId.Unique);
1057
1058                 AFSAcquireExcl( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
1059                                 TRUE);
1060
1061                 if( !BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
1062                 {
1063
1064                     ntStatus = AFSEnumerateDirectory( AuthGroup,
1065                                                       pCurrentObject,
1066                                                       TRUE);
1067
1068                     if( !NT_SUCCESS( ntStatus))
1069                     {
1070
1071                         AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1072
1073                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1074                                       AFS_TRACE_LEVEL_ERROR,
1075                                       "AFSLocateNameEntry (FO: %08lX) Failed to enumerate parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1076                                       FileObject,
1077                                       &pDirEntry->NameInformation.FileName,
1078                                       pCurrentObject->FileId.Cell,
1079                                       pCurrentObject->FileId.Volume,
1080                                       pCurrentObject->FileId.Vnode,
1081                                       pCurrentObject->FileId.Unique,
1082                                       ntStatus);
1083
1084                         try_return( ntStatus);
1085                     }
1086
1087                     SetFlag( pDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1088                 }
1089
1090                 AFSReleaseResource( pCurrentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
1091             }
1092             else if( pCurrentObject->FileType == AFS_FILE_TYPE_FILE)
1093             {
1094
1095                 if( uniPathName.Length > 0)
1096                 {
1097
1098                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1099                                   AFS_TRACE_LEVEL_ERROR,
1100                                   "AFSLocateNameEntry (FO: %08lX) Encountered file node %wZ FID %08lX-%08lX-%08lX-%08lX in path processing\n",
1101                                   FileObject,
1102                                   &pDirEntry->NameInformation.FileName,
1103                                   pCurrentObject->FileId.Cell,
1104                                   pCurrentObject->FileId.Volume,
1105                                   pCurrentObject->FileId.Vnode,
1106                                   pCurrentObject->FileId.Unique);
1107
1108                     // The proper error code to return would be STATUS_OBJECT_PATH_INVALID because
1109                     // one of the components of the path is not a directory.  However, returning
1110                     // that error prevents IIS 7 and 7.5 from being able to serve data out of AFS.
1111                     // Instead IIS insists on treating the target file as if it is a directory containing
1112                     // a potential web.config file.  NTFS and LanMan return STATUS_OBJECT_PATH_NOT_FOUND.
1113                     // AFS will follow suit.
1114
1115                     ntStatus = STATUS_OBJECT_PATH_NOT_FOUND;
1116                 }
1117                 else
1118                 {
1119
1120                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1121                                   AFS_TRACE_LEVEL_VERBOSE,
1122                                   "AFSLocateNameEntry (FO: %08lX) Returning file %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1123                                   FileObject,
1124                                   &pDirEntry->NameInformation.FileName,
1125                                   pCurrentObject->FileId.Cell,
1126                                   pCurrentObject->FileId.Volume,
1127                                   pCurrentObject->FileId.Vnode,
1128                                   pCurrentObject->FileId.Unique);
1129
1130                     //
1131                     // Pass back the directory entries
1132                     //
1133
1134                     *ParentDirectoryCB = pParentDirEntry;
1135
1136                     *DirectoryCB = pDirEntry;
1137
1138                     *VolumeCB = pCurrentVolume;
1139
1140                     *RootPathName = uniFullPathName;
1141                 }
1142
1143                 try_return( ntStatus);
1144             }
1145
1146             //
1147             // If we are at the end of the processing, set our returned information and get out
1148             //
1149
1150             if( uniPathName.Length == 0)
1151             {
1152
1153                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1154                               AFS_TRACE_LEVEL_VERBOSE,
1155                               "AFSLocateNameEntry (FO: %08lX) Completed processing returning %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1156                               FileObject,
1157                               &pDirEntry->NameInformation.FileName,
1158                               pCurrentObject->FileId.Cell,
1159                               pCurrentObject->FileId.Volume,
1160                               pCurrentObject->FileId.Vnode,
1161                               pCurrentObject->FileId.Unique);
1162
1163                 //
1164                 // Pass back the directory entries
1165                 //
1166
1167                 *ParentDirectoryCB = pParentDirEntry;
1168
1169                 *DirectoryCB = pDirEntry;
1170
1171                 *VolumeCB = pCurrentVolume;
1172
1173                 *RootPathName = uniFullPathName;
1174
1175                 try_return( ntStatus);
1176             }
1177
1178             //
1179             // We may have returned to the top of the while( TRUE)
1180             //
1181             if( bSubstituteName &&
1182                 uniSearchName.Buffer != NULL)
1183             {
1184
1185                 AFSExFreePoolWithTag( uniSearchName.Buffer, 0);
1186
1187                 bSubstituteName = FALSE;
1188
1189                 uniSearchName.Length = uniSearchName.MaximumLength = 0;
1190                 uniSearchName.Buffer = NULL;
1191             }
1192
1193             ulSubstituteIndex = 1;
1194
1195             ntStatus = STATUS_SUCCESS;
1196
1197             //
1198             // Get the next component name
1199             //
1200
1201             FsRtlDissectName( uniPathName,
1202                               &uniComponentName,
1203                               &uniRemainingPath);
1204
1205             //
1206             // Check for the . and .. in the path
1207             //
1208
1209             if( RtlCompareUnicodeString( &uniComponentName,
1210                                          &uniNoOpName,
1211                                          TRUE) == 0)
1212             {
1213
1214                 uniPathName = uniRemainingPath;
1215
1216                 continue;
1217             }
1218
1219             if( RtlCompareUnicodeString( &uniComponentName,
1220                                          &uniRelativeName,
1221                                          TRUE) == 0)
1222             {
1223
1224                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1225                               AFS_TRACE_LEVEL_VERBOSE,
1226                               "AFSLocateNameEntry (FO: %08lX) Backing up entry from %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1227                               FileObject,
1228                               &pDirEntry->NameInformation.FileName,
1229                               pCurrentObject->FileId.Cell,
1230                               pCurrentObject->FileId.Volume,
1231                               pCurrentObject->FileId.Vnode,
1232                               pCurrentObject->FileId.Unique);
1233
1234                 //
1235                 // Need to back up one entry in the name array
1236                 //
1237                 lCount = InterlockedDecrement( &pDirEntry->OpenReferenceCount);
1238
1239                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1240                               AFS_TRACE_LEVEL_VERBOSE,
1241                               "AFSLocateNameEntry Decrement4 count on %wZ DE %p Ccb %p Cnt %d\n",
1242                               &pDirEntry->NameInformation.FileName,
1243                               pDirEntry,
1244                               NULL,
1245                               lCount);
1246
1247                 pDirEntry = AFSBackupEntry( NameArray);
1248
1249                 if( pDirEntry == NULL)
1250                 {
1251
1252                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1253                                   AFS_TRACE_LEVEL_ERROR,
1254                                   "AFSLocateNameEntry AFSBackupEntry failed\n");
1255
1256                     try_return(ntStatus = STATUS_OBJECT_PATH_INVALID);
1257                 }
1258
1259                 lCount = InterlockedIncrement( &pDirEntry->OpenReferenceCount);
1260
1261                 if( BooleanFlagOn( pDirEntry->ObjectInformation->Flags, AFS_OBJECT_ROOT_VOLUME))
1262                 {
1263
1264                     pParentDirEntry = NULL;
1265                 }
1266                 else
1267                 {
1268
1269                     pParentDirEntry = AFSGetParentEntry( pNameArray);
1270
1271                     ASSERT( pParentDirEntry != pDirEntry);
1272                 }
1273
1274                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1275                               AFS_TRACE_LEVEL_VERBOSE,
1276                               "AFSLocateNameEntry Increment4 count on %wZ DE %p Ccb %p Cnt %d\n",
1277                               &pDirEntry->NameInformation.FileName,
1278                               pDirEntry,
1279                               NULL,
1280                               pDirEntry->OpenReferenceCount);
1281
1282                 uniPathName = uniRemainingPath;
1283
1284                 continue;
1285             }
1286
1287             //
1288             // Update our pointers
1289             //
1290
1291             pParentDirEntry = pDirEntry;
1292
1293             pDirEntry = NULL;
1294
1295             uniSearchName = uniComponentName;
1296
1297             while( pDirEntry == NULL)
1298             {
1299
1300                 //
1301                 // If the SearchName contains @SYS then we perform the substitution.
1302                 // If there is no substitution we give up.
1303                 //
1304
1305                 if( !bSubstituteName &&
1306                     FsRtlIsNameInExpression( &uniSysName,
1307                                              &uniSearchName,
1308                                              TRUE,
1309                                              NULL))
1310                 {
1311
1312                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1313                                   AFS_TRACE_LEVEL_VERBOSE_2,
1314                                   "AFSLocateNameEntry (FO: %08lX) Processing @SYS substitution for %wZ Index %08lX\n",
1315                                   FileObject,
1316                                   &uniComponentName,
1317                                   ulSubstituteIndex);
1318
1319                     ntStatus = AFSSubstituteSysName( &uniComponentName,
1320                                                      &uniSearchName,
1321                                                      ulSubstituteIndex);
1322
1323                     if ( NT_SUCCESS( ntStatus))
1324                     {
1325
1326                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1327                                       AFS_TRACE_LEVEL_VERBOSE_2,
1328                                       "AFSLocateNameEntry (FO: %08lX) Located substitution %wZ for %wZ Index %08lX\n",
1329                                       FileObject,
1330                                       &uniSearchName,
1331                                       &uniComponentName,
1332                                       ulSubstituteIndex);
1333
1334                         //
1335                         // Go reparse the name again
1336                         //
1337
1338                         bSubstituteName = TRUE;
1339
1340                         ulSubstituteIndex++; // For the next entry, if needed
1341
1342                         continue;   // while( pDirEntry == NULL)
1343                     }
1344                     else
1345                     {
1346
1347                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1348                                       AFS_TRACE_LEVEL_ERROR,
1349                                       "AFSLocateNameEntry (FO: %08lX) Failed to locate substitute string for %wZ Index %08lX Status %08lX\n",
1350                                       FileObject,
1351                                       &uniComponentName,
1352                                       ulSubstituteIndex,
1353                                       ntStatus);
1354
1355                         if( ntStatus == STATUS_OBJECT_NAME_NOT_FOUND)
1356                         {
1357
1358                             //
1359                             // Pass back the directory entries
1360                             //
1361
1362                             *ParentDirectoryCB = pParentDirEntry;
1363
1364                             *DirectoryCB = NULL;
1365
1366                             *VolumeCB = pCurrentVolume;
1367
1368                             if( ComponentName != NULL)
1369                             {
1370
1371                                 *ComponentName = uniComponentName;
1372                             }
1373
1374                             *RootPathName = uniFullPathName;
1375                         }
1376
1377                         //
1378                         // We can't possibly have a pDirEntry since the lookup failed
1379                         //
1380                         try_return( ntStatus);
1381                     }
1382                 }
1383
1384                 //
1385                 // Generate the CRC on the node and perform a case sensitive lookup
1386                 //
1387
1388                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1389                               AFS_TRACE_LEVEL_VERBOSE_2,
1390                               "AFSLocateNameEntry (FO: %08lX) Searching for entry %wZ case sensitive\n",
1391                               FileObject,
1392                               &uniSearchName);
1393
1394                 ulCRC = AFSGenerateCRC( &uniSearchName,
1395                                         FALSE);
1396
1397                 AFSAcquireShared( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock,
1398                                   TRUE);
1399
1400                 AFSLocateCaseSensitiveDirEntry( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead,
1401                                                 ulCRC,
1402                                                 &pDirEntry);
1403
1404                 if( pDirEntry == NULL)
1405                 {
1406
1407                     //
1408                     // Missed so perform a case insensitive lookup
1409                     //
1410
1411                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1412                                   AFS_TRACE_LEVEL_VERBOSE_2,
1413                                   "AFSLocateNameEntry (FO: %08lX) Searching for entry %wZ case insensitive\n",
1414                                   FileObject,
1415                                   &uniSearchName);
1416
1417                     ulCRC = AFSGenerateCRC( &uniSearchName,
1418                                             TRUE);
1419
1420                     AFSLocateCaseInsensitiveDirEntry( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead,
1421                                                       ulCRC,
1422                                                       &pDirEntry);
1423
1424                     if( pDirEntry == NULL)
1425                     {
1426
1427                         //
1428                         // OK, if this component is a valid short name then try
1429                         // a lookup in the short name tree
1430                         //
1431
1432                         if( RtlIsNameLegalDOS8Dot3( &uniSearchName,
1433                                                     NULL,
1434                                                     NULL))
1435                         {
1436
1437                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1438                                           AFS_TRACE_LEVEL_VERBOSE_2,
1439                                           "AFSLocateNameEntry (FO: %08lX) Searching for entry %wZ short name\n",
1440                                           FileObject,
1441                                           &uniSearchName);
1442
1443                             AFSLocateShortNameDirEntry( pParentDirEntry->ObjectInformation->Specific.Directory.ShortNameTree,
1444                                                         ulCRC,
1445                                                         &pDirEntry);
1446                         }
1447
1448                         if( pDirEntry == NULL)
1449                         {
1450
1451                             //
1452                             // If we substituted a name then reset our search name and try again
1453                             //
1454
1455                             if( bSubstituteName)
1456                             {
1457
1458                                 AFSExFreePoolWithTag( uniSearchName.Buffer, 0);
1459
1460                                 uniSearchName = uniComponentName;
1461
1462                                 bSubstituteName = FALSE;
1463
1464                                 AFSReleaseResource( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
1465
1466                                 continue;       // while( pDirEntry == NULL)
1467                             }
1468
1469                             if( uniRemainingPath.Length > 0)
1470                             {
1471
1472                                 ntStatus = STATUS_OBJECT_PATH_NOT_FOUND;
1473                             }
1474                             else
1475                             {
1476
1477                                 ntStatus = STATUS_OBJECT_NAME_NOT_FOUND;
1478
1479                                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1480                                               AFS_TRACE_LEVEL_VERBOSE,
1481                                               "AFSLocateNameEntry (FO: %08lX) Returning name not found for %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1482                                               FileObject,
1483                                               &uniSearchName,
1484                                               pCurrentObject->FileId.Cell,
1485                                               pCurrentObject->FileId.Volume,
1486                                               pCurrentObject->FileId.Vnode,
1487                                               pCurrentObject->FileId.Unique);
1488
1489                                 //
1490                                 // Pass back the directory entries
1491                                 //
1492
1493                                 *ParentDirectoryCB = pParentDirEntry;
1494
1495                                 *DirectoryCB = NULL;
1496
1497                                 *VolumeCB = pCurrentVolume;
1498
1499                                 if( ComponentName != NULL)
1500                                 {
1501
1502                                     *ComponentName = uniComponentName;
1503                                 }
1504
1505                                 *RootPathName = uniFullPathName;
1506                             }
1507
1508                             AFSReleaseResource( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
1509
1510                             //
1511                             // Node name not found so get out
1512                             //
1513
1514                             try_return( ntStatus);  // while( pDirEntry == NULL)
1515                         }
1516                     }
1517                     else
1518                     {
1519
1520                         //
1521                         // Here we have a match on the case insensitive lookup for the name. If there
1522                         // Is more than one link entry for this node then fail the lookup request
1523                         //
1524
1525                         if( !BooleanFlagOn( pDirEntry->Flags, AFS_DIR_ENTRY_CASE_INSENSTIVE_LIST_HEAD) ||
1526                             pDirEntry->CaseInsensitiveList.fLink != NULL)
1527                         {
1528
1529                             AFSReleaseResource( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
1530
1531                             try_return(ntStatus = STATUS_OBJECT_NAME_COLLISION);
1532                         }
1533                     }
1534                 }
1535
1536                 if( pDirEntry != NULL)
1537                 {
1538
1539                     //
1540                     // If the verify flag is set on the parent and the current entry is deleted
1541                     // revalidate the parent and search again.
1542                     //
1543
1544                     if( BooleanFlagOn( pDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED) &&
1545                         BooleanFlagOn( pParentDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_VERIFY))
1546                     {
1547
1548                         AFSReleaseResource( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
1549
1550                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1551                                       AFS_TRACE_LEVEL_VERBOSE,
1552                                       "AFSLocateNameEntry (FO: %08lX) Verifying(2) parent %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1553                                       FileObject,
1554                                       &pParentDirEntry->NameInformation.FileName,
1555                                       pParentDirEntry->ObjectInformation->FileId.Cell,
1556                                       pParentDirEntry->ObjectInformation->FileId.Volume,
1557                                       pParentDirEntry->ObjectInformation->FileId.Vnode,
1558                                       pParentDirEntry->ObjectInformation->FileId.Unique);
1559
1560                         //
1561                         // Directory TreeLock should be exclusively held
1562                         //
1563
1564                         AFSAcquireExcl( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock,
1565                                         TRUE);
1566
1567                         ntStatus = AFSVerifyEntry( AuthGroup,
1568                                                    pParentDirEntry);
1569
1570                         AFSReleaseResource( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
1571
1572                         if( !NT_SUCCESS( ntStatus))
1573                         {
1574
1575                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1576                                           AFS_TRACE_LEVEL_ERROR,
1577                                           "AFSLocateNameEntry (FO: %08lX) Failed to verify(2) parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1578                                           FileObject,
1579                                           &pParentDirEntry->NameInformation.FileName,
1580                                           pParentDirEntry->ObjectInformation->FileId.Cell,
1581                                           pParentDirEntry->ObjectInformation->FileId.Volume,
1582                                           pParentDirEntry->ObjectInformation->FileId.Vnode,
1583                                           pParentDirEntry->ObjectInformation->FileId.Unique,
1584                                           ntStatus);
1585
1586                             try_return( ntStatus);
1587                         }
1588
1589                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1590                                       AFS_TRACE_LEVEL_VERBOSE,
1591                                       "AFSLocateNameEntry (FO: %08lX) Reprocessing component %wZ in parent %wZ\n",
1592                                       FileObject,
1593                                       &uniSearchName,
1594                                       &pParentDirEntry->NameInformation.FileName);
1595
1596
1597                         pDirEntry = NULL;
1598
1599                         continue;
1600                     }
1601
1602                     //
1603                     // Increment our dir entry ref count
1604                     //
1605
1606                     lCount = InterlockedIncrement( &pDirEntry->OpenReferenceCount);
1607
1608                     AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1609                                   AFS_TRACE_LEVEL_VERBOSE,
1610                                   "AFSLocateNameEntry Increment5 count on %wZ DE %p Ccb %p Cnt %d\n",
1611                                   &pDirEntry->NameInformation.FileName,
1612                                   pDirEntry,
1613                                   NULL,
1614                                   lCount);
1615                 }
1616
1617                 AFSReleaseResource( pParentDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
1618
1619             } // End while( pDirEntry == NULL)
1620
1621             //
1622             // If we have a dirEntry for this component, perform some basic validation on it
1623             //
1624
1625             if( pDirEntry != NULL &&
1626                 BooleanFlagOn( pDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED))
1627             {
1628
1629                 pCurrentObject = pDirEntry->ObjectInformation;
1630
1631                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1632                               AFS_TRACE_LEVEL_ERROR,
1633                               "AFSLocateNameEntry (FO: %08lX) Deleted entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1634                               FileObject,
1635                               &pDirEntry->NameInformation.FileName,
1636                               pCurrentObject->FileId.Cell,
1637                               pCurrentObject->FileId.Volume,
1638                               pCurrentObject->FileId.Vnode,
1639                               pCurrentObject->FileId.Unique);
1640
1641                 //
1642                 // This entry was deleted through the invalidation call back so perform cleanup
1643                 // on the entry
1644                 //
1645
1646                 pParentObjectInfo = pCurrentObject->ParentObjectInformation;
1647
1648                 ASSERT( pParentObjectInfo != NULL);
1649
1650                 AFSAcquireExcl( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
1651                                 TRUE);
1652
1653                 AFSAcquireExcl( pCurrentObject->VolumeCB->ObjectInfoTree.TreeLock,
1654                                 TRUE);
1655
1656                 lCount = InterlockedDecrement( &pDirEntry->OpenReferenceCount);
1657
1658                 if( lCount == 0)
1659                 {
1660
1661                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING|AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1662                                   AFS_TRACE_LEVEL_VERBOSE,
1663                                   "AFSLocateNameEntry Deleting dir entry %08lX (%08lX) for %wZ\n",
1664                                   pDirEntry,
1665                                   pCurrentObject,
1666                                   &pDirEntry->NameInformation.FileName);
1667
1668                     //
1669                     // Remove and delete the directory entry from the parent list
1670                     //
1671
1672                     AFSDeleteDirEntry( pParentObjectInfo,
1673                                        pDirEntry);
1674
1675                     if( pCurrentObject->ObjectReferenceCount <= 0)
1676                     {
1677
1678                         if( BooleanFlagOn( pCurrentObject->Flags, AFS_OBJECT_INSERTED_HASH_TREE))
1679                         {
1680
1681                             AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
1682                                           AFS_TRACE_LEVEL_VERBOSE,
1683                                           "AFSLocateNameEntry Removing object %08lX from volume tree\n",
1684                                           pCurrentObject);
1685
1686                             AFSRemoveHashEntry( &pCurrentObject->VolumeCB->ObjectInfoTree.TreeHead,
1687                                                 &pCurrentObject->TreeEntry);
1688
1689                             ClearFlag( pCurrentObject->Flags, AFS_OBJECT_INSERTED_HASH_TREE);
1690                         }
1691                     }
1692                 }
1693                 else
1694                 {
1695
1696                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1697                                   AFS_TRACE_LEVEL_VERBOSE,
1698                                   "AFSLocateNameEntry Setting DELETE flag in dir entry %p for %wZ\n",
1699                                   pDirEntry,
1700                                   &pDirEntry->NameInformation.FileName);
1701
1702                     SetFlag( pDirEntry->Flags, AFS_DIR_ENTRY_DELETED);
1703
1704                     AFSRemoveNameEntry( pParentObjectInfo,
1705                                         pDirEntry);
1706                 }
1707
1708                 AFSReleaseResource( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
1709
1710                 AFSReleaseResource( pCurrentObject->VolumeCB->ObjectInfoTree.TreeLock);
1711
1712                 //
1713                 // We deleted the dir entry so check if there is any remaining portion
1714                 // of the name to process.
1715                 //
1716
1717                 if( uniRemainingPath.Length > 0)
1718                 {
1719                     ntStatus = STATUS_OBJECT_PATH_NOT_FOUND;
1720                 }
1721                 else
1722                 {
1723
1724                     ntStatus = STATUS_OBJECT_NAME_NOT_FOUND;
1725
1726                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1727                                   AFS_TRACE_LEVEL_VERBOSE,
1728                                   "AFSLocateNameEntry (FO: %08lX) Returning name not found(2) for %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1729                                   FileObject,
1730                                   &uniComponentName,
1731                                   pCurrentObject->FileId.Cell,
1732                                   pCurrentObject->FileId.Volume,
1733                                   pCurrentObject->FileId.Vnode,
1734                                   pCurrentObject->FileId.Unique);
1735
1736                     //
1737                     // Pass back the directory entries
1738                     //
1739
1740                     *ParentDirectoryCB = pParentDirEntry;
1741
1742                     *DirectoryCB = NULL;
1743
1744                     *VolumeCB = pCurrentVolume;
1745
1746                     if( ComponentName != NULL)
1747                     {
1748
1749                         *ComponentName = uniComponentName;
1750                     }
1751
1752                     *RootPathName = uniFullPathName;
1753                 }
1754             }
1755
1756             if( ntStatus != STATUS_SUCCESS)
1757             {
1758
1759                 try_return( ntStatus);
1760             }
1761
1762             //
1763             // Decrement the previous parent
1764             //
1765
1766             lCount = InterlockedDecrement( &pParentDirEntry->OpenReferenceCount);
1767
1768             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1769                           AFS_TRACE_LEVEL_VERBOSE,
1770                           "AFSLocateNameEntry Decrement5 count on Parent %wZ DE %p Ccb %p Cnt %d\n",
1771                           &pParentDirEntry->NameInformation.FileName,
1772                           pParentDirEntry,
1773                           NULL,
1774                           lCount);
1775
1776             //
1777             // If we ended up substituting a name in the component then update
1778             // the full path and update the pointers
1779             //
1780
1781             if( bSubstituteName)
1782             {
1783
1784                 BOOLEAN bRelativeOpen = FALSE;
1785
1786                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1787                               AFS_TRACE_LEVEL_VERBOSE_2,
1788                               "AFSLocateNameEntry (FO: %08lX) Substituting %wZ into %wZ Index %08lX\n",
1789                               FileObject,
1790                               &uniSearchName,
1791                               &uniComponentName,
1792                               ulSubstituteIndex);
1793
1794                 if( FileObject != NULL &&
1795                     FileObject->RelatedFileObject != NULL)
1796                 {
1797
1798                     bRelativeOpen = TRUE;
1799                 }
1800
1801                 //
1802                 // AFSSubstituteNameInPath will replace the uniFullPathName.Buffer
1803                 // and free the prior Buffer contents but only if the fourth
1804                 // parameter is TRUE.
1805                 //
1806
1807                 ntStatus = AFSSubstituteNameInPath( &uniFullPathName,
1808                                                     &uniComponentName,
1809                                                     &uniSearchName,
1810                                                     &uniRemainingPath,
1811                                                     bRelativeOpen ||
1812                                                             bAllocatedSymLinkBuffer ||
1813                                                             bSubstitutedName);
1814
1815                 if( !NT_SUCCESS( ntStatus))
1816                 {
1817
1818                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1819                                   AFS_TRACE_LEVEL_ERROR,
1820                                   "AFSLocateNameEntry (FO: %08lX) Failure to substitute %wZ into %wZ Index %08lX Status %08lX\n",
1821                                   FileObject,
1822                                   &uniSearchName,
1823                                   &uniComponentName,
1824                                   ulSubstituteIndex,
1825                                   ntStatus);
1826
1827                     try_return( ntStatus);
1828                 }
1829
1830                 //
1831                 // We have substituted a name into the buffer so if we do this again for this
1832                 // path, we need to free up the buffer we allocated.
1833                 //
1834
1835                 bSubstitutedName = TRUE;
1836             }
1837
1838             //
1839             // Update the search parameters
1840             //
1841
1842             uniPathName = uniRemainingPath;
1843
1844             //
1845             // Check if the is a SymLink entry but has no Target FileID or Name. In this
1846             // case it might be a DFS Link so let's go and evaluate it to be sure
1847             //
1848
1849             if( pCurrentObject->FileType == AFS_FILE_TYPE_SYMLINK &&
1850                 pCurrentObject->TargetFileId.Vnode == 0 &&
1851                 pCurrentObject->TargetFileId.Unique == 0 &&
1852                 pDirEntry->NameInformation.TargetName.Length == 0)
1853             {
1854
1855                 ntStatus = AFSValidateSymLink( AuthGroup,
1856                                                pDirEntry);
1857
1858                 if( !NT_SUCCESS( ntStatus))
1859                 {
1860
1861                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1862                                   AFS_TRACE_LEVEL_ERROR,
1863                                   "AFSLocateNameEntry (FO: %08lX) Failed to evaluate possible DFS Link %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1864                                   FileObject,
1865                                   &pDirEntry->NameInformation.FileName,
1866                                   pCurrentObject->FileId.Cell,
1867                                   pCurrentObject->FileId.Volume,
1868                                   pCurrentObject->FileId.Vnode,
1869                                   pCurrentObject->FileId.Unique,
1870                                   ntStatus);
1871
1872                     try_return( ntStatus);
1873                 }
1874             }
1875
1876             //
1877             // Update the name array
1878             //
1879
1880             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1881                           AFS_TRACE_LEVEL_VERBOSE,
1882                           "AFSLocateNameEntry (FO: %08lX) Inserting name array entry %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1883                           FileObject,
1884                           &pDirEntry->NameInformation.FileName,
1885                           pCurrentObject->FileId.Cell,
1886                           pCurrentObject->FileId.Volume,
1887                           pCurrentObject->FileId.Vnode,
1888                           pCurrentObject->FileId.Unique);
1889
1890             ntStatus = AFSInsertNextElement( pNameArray,
1891                                              pDirEntry);
1892
1893             if( !NT_SUCCESS( ntStatus))
1894             {
1895
1896                 try_return( ntStatus);
1897             }
1898         }       // while (TRUE)
1899
1900 try_exit:
1901
1902         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1903                       AFS_TRACE_LEVEL_VERBOSE,
1904                       "AFSLocateNameEntry (FO: %08lX) Completed processing %wZ Status %08lX\n",
1905                       FileObject,
1906                       RootPathName,
1907                       ntStatus);
1908
1909         if( ( !NT_SUCCESS( ntStatus) &&
1910               ntStatus != STATUS_OBJECT_NAME_NOT_FOUND) ||
1911             ntStatus == STATUS_REPARSE)
1912         {
1913
1914             if( pDirEntry != NULL)
1915             {
1916
1917                 lCount = InterlockedDecrement( &pDirEntry->OpenReferenceCount);
1918
1919                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1920                               AFS_TRACE_LEVEL_VERBOSE,
1921                               "AFSLocateNameEntry Decrement6 count on %wZ DE %p Ccb %p Cnt %d\n",
1922                               &pDirEntry->NameInformation.FileName,
1923                               pDirEntry,
1924                               NULL,
1925                               lCount);
1926             }
1927             else if( pParentDirEntry != NULL)
1928             {
1929
1930                 lCount = InterlockedDecrement( &pParentDirEntry->OpenReferenceCount);
1931
1932                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1933                               AFS_TRACE_LEVEL_VERBOSE,
1934                               "AFSLocateNameEntry Decrement7 count on %wZ DE %p Ccb %p Cnt %d\n",
1935                               &pParentDirEntry->NameInformation.FileName,
1936                               pParentDirEntry,
1937                               NULL,
1938                               lCount);
1939             }
1940
1941             if( bReleaseCurrentVolume)
1942             {
1943
1944                 ASSERT( pCurrentVolume->VolumeReferenceCount > 1);
1945
1946                 lCount = InterlockedDecrement( &pCurrentVolume->VolumeReferenceCount);
1947
1948                 AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1949                               AFS_TRACE_LEVEL_VERBOSE,
1950                               "AFSLocateNameEntry Decrement3 count on volume %08lX Cnt %d\n",
1951                               pCurrentVolume,
1952                               lCount);
1953             }
1954
1955             if( RootPathName->Buffer != uniFullPathName.Buffer)
1956             {
1957
1958                 AFSExFreePoolWithTag( uniFullPathName.Buffer, 0);
1959             }
1960         }
1961         else
1962         {
1963
1964             if( *ParentDirectoryCB != NULL)
1965             {
1966
1967                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1968                               AFS_TRACE_LEVEL_VERBOSE,
1969                               "AFSLocateNameEntry Count on Parent %wZ DE %p Ccb %p Cnt %d\n",
1970                               &(*ParentDirectoryCB)->NameInformation.FileName,
1971                               *ParentDirectoryCB,
1972                               NULL,
1973                               (*ParentDirectoryCB)->OpenReferenceCount);
1974             }
1975
1976             if( *DirectoryCB != NULL)
1977             {
1978
1979                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1980                               AFS_TRACE_LEVEL_VERBOSE,
1981                               "AFSLocateNameEntry Count on %wZ DE %p Ccb %p Cnt %d\n",
1982                               &(*DirectoryCB)->NameInformation.FileName,
1983                               *DirectoryCB,
1984                               NULL,
1985                               (*DirectoryCB)->OpenReferenceCount);
1986             }
1987         }
1988
1989         if( bSubstituteName &&
1990             uniSearchName.Buffer != NULL)
1991         {
1992
1993             AFSExFreePoolWithTag( uniSearchName.Buffer, 0);
1994         }
1995     }
1996
1997     return ntStatus;
1998 }
1999
2000 NTSTATUS
2001 AFSCreateDirEntry( IN GUID            *AuthGroup,
2002                    IN AFSObjectInfoCB *ParentObjectInfo,
2003                    IN AFSDirectoryCB *ParentDirCB,
2004                    IN PUNICODE_STRING FileName,
2005                    IN PUNICODE_STRING ComponentName,
2006                    IN ULONG Attributes,
2007                    IN OUT AFSDirectoryCB **DirEntry)
2008 {
2009
2010     NTSTATUS ntStatus = STATUS_SUCCESS;
2011     AFSDirectoryCB *pDirNode = NULL, *pExistingDirNode = NULL;
2012     UNICODE_STRING uniShortName;
2013     LARGE_INTEGER liFileSize = {0,0};
2014     LONG lCount;
2015
2016     __Enter
2017     {
2018
2019         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2020                       AFS_TRACE_LEVEL_VERBOSE_2,
2021                       "AFSCreateDirEntry Creating dir entry in parent %wZ FID %08lX-%08lX-%08lX-%08lX Component %wZ Attribs %08lX\n",
2022                       &ParentDirCB->NameInformation.FileName,
2023                       ParentObjectInfo->FileId.Cell,
2024                       ParentObjectInfo->FileId.Volume,
2025                       ParentObjectInfo->FileId.Vnode,
2026                       ParentObjectInfo->FileId.Unique,
2027                       ComponentName,
2028                       Attributes);
2029
2030         //
2031         // OK, before inserting the node into the parent tree, issue
2032         // the request to the service for node creation
2033         // We will need to drop the lock on the parent node since the create
2034         // could cause a callback into the file system to invalidate it's cache
2035         //
2036
2037         ntStatus = AFSNotifyFileCreate( AuthGroup,
2038                                         ParentObjectInfo,
2039                                         &liFileSize,
2040                                         Attributes,
2041                                         ComponentName,
2042                                         &pDirNode);
2043
2044         //
2045         // If the returned status is STATUS_REPARSE then the entry exists
2046         // and we raced, get out.
2047
2048         if( ntStatus == STATUS_REPARSE)
2049         {
2050
2051             *DirEntry = pDirNode;
2052
2053             try_return( ntStatus = STATUS_SUCCESS);
2054         }
2055
2056         if( !NT_SUCCESS( ntStatus))
2057         {
2058
2059             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2060                           AFS_TRACE_LEVEL_ERROR,
2061                           "AFSCreateDirEntry Failed to create dir entry in parent %wZ FID %08lX-%08lX-%08lX-%08lX Component %wZ Attribs %08lX Status %08lX\n",
2062                           &ParentDirCB->NameInformation.FileName,
2063                           ParentObjectInfo->FileId.Cell,
2064                           ParentObjectInfo->FileId.Volume,
2065                           ParentObjectInfo->FileId.Vnode,
2066                           ParentObjectInfo->FileId.Unique,
2067                           ComponentName,
2068                           Attributes,
2069                           ntStatus);
2070
2071             try_return( ntStatus);
2072         }
2073
2074         AFSAcquireExcl( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
2075                         TRUE);
2076
2077         //
2078         // Before attempting to insert the new entry, check if we need to validate the parent
2079         //
2080
2081         if( BooleanFlagOn( ParentObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY))
2082         {
2083
2084             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2085                           AFS_TRACE_LEVEL_VERBOSE,
2086                           "AFSCreateDirEntry Verifying parent %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2087                           &ParentDirCB->NameInformation.FileName,
2088                           ParentObjectInfo->FileId.Cell,
2089                           ParentObjectInfo->FileId.Volume,
2090                           ParentObjectInfo->FileId.Vnode,
2091                           ParentObjectInfo->FileId.Unique);
2092
2093             ntStatus = AFSVerifyEntry( AuthGroup,
2094                                        ParentDirCB);
2095
2096             if( !NT_SUCCESS( ntStatus))
2097             {
2098
2099                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2100                               AFS_TRACE_LEVEL_ERROR,
2101                               "AFSCreateDirEntry Failed to verify parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
2102                               &ParentDirCB->NameInformation.FileName,
2103                               ParentObjectInfo->FileId.Cell,
2104                               ParentObjectInfo->FileId.Volume,
2105                               ParentObjectInfo->FileId.Vnode,
2106                               ParentObjectInfo->FileId.Unique,
2107                               ntStatus);
2108
2109                 AFSReleaseResource( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
2110
2111                 try_return( ntStatus);
2112             }
2113         }
2114
2115         //
2116         // Check for the entry in the event we raced with some other thread
2117         //
2118
2119         AFSLocateCaseSensitiveDirEntry( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead,
2120                                         (ULONG)pDirNode->CaseSensitiveTreeEntry.HashIndex,
2121                                         &pExistingDirNode);
2122
2123         if( pExistingDirNode != NULL)
2124         {
2125             if (AFSIsEqualFID( &pDirNode->ObjectInformation->FileId,
2126                                &pExistingDirNode->ObjectInformation->FileId))
2127             {
2128
2129                 AFSDeleteDirEntry( ParentObjectInfo,
2130                                    pDirNode);
2131
2132                 lCount = InterlockedIncrement( &pExistingDirNode->OpenReferenceCount);
2133
2134                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2135                               AFS_TRACE_LEVEL_VERBOSE,
2136                               "AFSCreateDirEntry Increment count on %wZ DE %p Cnt %d\n",
2137                               &pExistingDirNode->NameInformation.FileName,
2138                               pExistingDirNode,
2139                               lCount);
2140
2141                 *DirEntry = pExistingDirNode;
2142
2143                 AFSReleaseResource( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
2144
2145                 try_return( ntStatus = STATUS_SUCCESS);
2146             }
2147             else
2148             {
2149
2150                 //
2151                 // Need to tear down this entry and rebuild it below
2152                 //
2153
2154                 if( pExistingDirNode->OpenReferenceCount == 0)
2155                 {
2156
2157                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2158                                   AFS_TRACE_LEVEL_VERBOSE,
2159                                   "AFSCreateDirEntry Different FIDs - Deleting DE %p for %wZ Old FID %08lX-%08lX-%08lX-%08lX New FID %08lX-%08lX-%08lX-%08lX\n",
2160                                   pExistingDirNode,
2161                                   &pExistingDirNode->NameInformation.FileName,
2162                                   pExistingDirNode->ObjectInformation->FileId.Cell,
2163                                   pExistingDirNode->ObjectInformation->FileId.Volume,
2164                                   pExistingDirNode->ObjectInformation->FileId.Vnode,
2165                                   pExistingDirNode->ObjectInformation->FileId.Unique,
2166                                   pDirNode->ObjectInformation->FileId.Cell,
2167                                   pDirNode->ObjectInformation->FileId.Volume,
2168                                   pDirNode->ObjectInformation->FileId.Vnode,
2169                                   pDirNode->ObjectInformation->FileId.Unique);
2170
2171                     AFSDeleteDirEntry( ParentObjectInfo,
2172                                        pExistingDirNode);
2173                 }
2174                 else
2175                 {
2176
2177                     SetFlag( pExistingDirNode->Flags, AFS_DIR_ENTRY_DELETED);
2178
2179                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2180                                   AFS_TRACE_LEVEL_VERBOSE,
2181                                   "AFSCreateDirEntry Different FIDs - Removing DE %p for %wZ Old FID %08lX-%08lX-%08lX-%08lX New FID %08lX-%08lX-%08lX-%08lX\n",
2182                                   pExistingDirNode,
2183                                   &pExistingDirNode->NameInformation.FileName,
2184                                   pExistingDirNode->ObjectInformation->FileId.Cell,
2185                                   pExistingDirNode->ObjectInformation->FileId.Volume,
2186                                   pExistingDirNode->ObjectInformation->FileId.Vnode,
2187                                   pExistingDirNode->ObjectInformation->FileId.Unique,
2188                                   pDirNode->ObjectInformation->FileId.Cell,
2189                                   pDirNode->ObjectInformation->FileId.Volume,
2190                                   pDirNode->ObjectInformation->FileId.Vnode,
2191                                   pDirNode->ObjectInformation->FileId.Unique);
2192
2193                     AFSRemoveNameEntry( ParentObjectInfo,
2194                                         pExistingDirNode);
2195                 }
2196
2197             }
2198         }
2199
2200         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2201                       AFS_TRACE_LEVEL_VERBOSE_2,
2202                       "AFSCreateDirEntry Inserting dir entry in parent %wZ FID %08lX-%08lX-%08lX-%08lX Component %wZ\n",
2203                       &ParentDirCB->NameInformation.FileName,
2204                       ParentObjectInfo->FileId.Cell,
2205                       ParentObjectInfo->FileId.Volume,
2206                       ParentObjectInfo->FileId.Vnode,
2207                       ParentObjectInfo->FileId.Unique,
2208                       ComponentName);
2209
2210         //
2211         // Insert the directory node
2212         //
2213
2214         AFSInsertDirectoryNode( ParentObjectInfo,
2215                                 pDirNode,
2216                                 TRUE);
2217
2218         lCount = InterlockedIncrement( &pDirNode->OpenReferenceCount);
2219
2220         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2221                       AFS_TRACE_LEVEL_VERBOSE,
2222                       "AFSCreateDirEntry Increment2 count on %wZ DE %p Cnt %d\n",
2223                       &pDirNode->NameInformation.FileName,
2224                       pDirNode,
2225                       lCount);
2226
2227         //
2228         // Pass back the dir entry
2229         //
2230
2231         *DirEntry = pDirNode;
2232
2233         AFSReleaseResource( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
2234
2235 try_exit:
2236
2237         NOTHING;
2238     }
2239
2240     return ntStatus;
2241 }
2242
2243 void
2244 AFSInsertDirectoryNode( IN AFSObjectInfoCB *ParentObjectInfo,
2245                         IN AFSDirectoryCB *DirEntry,
2246                         IN BOOLEAN InsertInEnumList)
2247 {
2248
2249     LONG lCount;
2250
2251     __Enter
2252     {
2253
2254         ASSERT( ExIsResourceAcquiredExclusiveLite( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock));
2255
2256         //
2257         // Insert the node into the directory node tree
2258         //
2259
2260         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2261                       AFS_TRACE_LEVEL_VERBOSE,
2262                       "AFSInsertDirectoryNode Insert DE %p for %wZ Clearing NOT_IN flag\n",
2263                       DirEntry,
2264                       &DirEntry->NameInformation.FileName);
2265
2266         ClearFlag( DirEntry->Flags, AFS_DIR_ENTRY_NOT_IN_PARENT_TREE);
2267
2268         if( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead == NULL)
2269         {
2270
2271             ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead = DirEntry;
2272
2273             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2274                           AFS_TRACE_LEVEL_VERBOSE,
2275                           "AFSInsertDirectoryNode Insert DE %p to head of case sensitive tree for %wZ\n",
2276                           DirEntry,
2277                           &DirEntry->NameInformation.FileName);
2278         }
2279         else
2280         {
2281
2282             AFSInsertCaseSensitiveDirEntry( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead,
2283                                             DirEntry);
2284
2285             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2286                           AFS_TRACE_LEVEL_VERBOSE,
2287                           "AFSInsertDirectoryNode Insert DE %p to case sensitive tree for %wZ\n",
2288                           DirEntry,
2289                           &DirEntry->NameInformation.FileName);
2290         }
2291
2292         if( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead == NULL)
2293         {
2294
2295             ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead = DirEntry;
2296
2297             SetFlag( DirEntry->Flags, AFS_DIR_ENTRY_CASE_INSENSTIVE_LIST_HEAD);
2298
2299             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2300                           AFS_TRACE_LEVEL_VERBOSE,
2301                           "AFSInsertDirectoryNode Insert DE %p to head of case insensitive tree for %wZ\n",
2302                           DirEntry,
2303                           &DirEntry->NameInformation.FileName);
2304         }
2305         else
2306         {
2307
2308             AFSInsertCaseInsensitiveDirEntry( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead,
2309                                               DirEntry);
2310
2311             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2312                           AFS_TRACE_LEVEL_VERBOSE,
2313                           "AFSInsertDirectoryNode Insert DE %p to case insensitive tree for %wZ\n",
2314                           DirEntry,
2315                           &DirEntry->NameInformation.FileName);
2316         }
2317
2318         //
2319         // Into the shortname tree
2320         //
2321
2322         if( DirEntry->Type.Data.ShortNameTreeEntry.HashIndex != 0)
2323         {
2324
2325             if( ParentObjectInfo->Specific.Directory.ShortNameTree == NULL)
2326             {
2327
2328                 ParentObjectInfo->Specific.Directory.ShortNameTree = DirEntry;
2329
2330                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2331                               AFS_TRACE_LEVEL_VERBOSE,
2332                               "AFSInsertDirectoryNode Insert DE %p to head of shortname tree for %wZ\n",
2333                               DirEntry,
2334                               &DirEntry->NameInformation.FileName);
2335
2336                 SetFlag( DirEntry->Flags, AFS_DIR_ENTRY_INSERTED_SHORT_NAME);
2337             }
2338             else
2339             {
2340
2341                 if( !NT_SUCCESS( AFSInsertShortNameDirEntry( ParentObjectInfo->Specific.Directory.ShortNameTree,
2342                                                              DirEntry)))
2343                 {
2344                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2345                                   AFS_TRACE_LEVEL_VERBOSE,
2346                                   "AFSInsertDirectoryNode Failed to insert DE %p to shortname tree for %wZ\n",
2347                                   DirEntry,
2348                                   &DirEntry->NameInformation.FileName);
2349                 }
2350                 else
2351                 {
2352                     SetFlag( DirEntry->Flags, AFS_DIR_ENTRY_INSERTED_SHORT_NAME);
2353
2354                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2355                                   AFS_TRACE_LEVEL_VERBOSE,
2356                                   "AFSInsertDirectoryNode Insert DE %p to shortname tree for %wZ\n",
2357                                   DirEntry,
2358                                   &DirEntry->NameInformation.FileName);
2359                 }
2360             }
2361         }
2362
2363         if( InsertInEnumList)
2364         {
2365
2366             //
2367             // And insert the node into the directory list
2368             //
2369
2370             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2371                           AFS_TRACE_LEVEL_VERBOSE,
2372                           "AFSInsertDirectoryNode Inserting entry %08lX %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2373                           DirEntry,
2374                           &DirEntry->NameInformation.FileName,
2375                           DirEntry->ObjectInformation->FileId.Cell,
2376                           DirEntry->ObjectInformation->FileId.Volume,
2377                           DirEntry->ObjectInformation->FileId.Vnode,
2378                           DirEntry->ObjectInformation->FileId.Unique);
2379
2380             if( ParentObjectInfo->Specific.Directory.DirectoryNodeListHead == NULL)
2381             {
2382
2383                 ParentObjectInfo->Specific.Directory.DirectoryNodeListHead = DirEntry;
2384             }
2385             else
2386             {
2387
2388                 ParentObjectInfo->Specific.Directory.DirectoryNodeListTail->ListEntry.fLink = (void *)DirEntry;
2389
2390                 DirEntry->ListEntry.bLink = (void *)ParentObjectInfo->Specific.Directory.DirectoryNodeListTail;
2391             }
2392
2393             ParentObjectInfo->Specific.Directory.DirectoryNodeListTail = DirEntry;
2394
2395             SetFlag( DirEntry->Flags, AFS_DIR_ENTRY_INSERTED_ENUM_LIST);
2396
2397             lCount = InterlockedIncrement( &ParentObjectInfo->Specific.Directory.DirectoryNodeCount);
2398
2399             AFSDbgLogMsg( AFS_SUBSYSTEM_DIR_NODE_COUNT,
2400                           AFS_TRACE_LEVEL_VERBOSE,
2401                           "AFSInsertDirectoryNode Adding entry %wZ Inc Count %d to parent FID %08lX-%08lX-%08lX-%08lX\n",
2402                           &DirEntry->NameInformation.FileName,
2403                           lCount,
2404                           ParentObjectInfo->FileId.Cell,
2405                           ParentObjectInfo->FileId.Volume,
2406                           ParentObjectInfo->FileId.Vnode,
2407                           ParentObjectInfo->FileId.Unique);
2408         }
2409     }
2410
2411     return;
2412 }
2413
2414 NTSTATUS
2415 AFSDeleteDirEntry( IN AFSObjectInfoCB *ParentObjectInfo,
2416                    IN AFSDirectoryCB *DirEntry)
2417 {
2418
2419     NTSTATUS ntStatus = STATUS_SUCCESS;
2420     LONG lCount;
2421
2422     __Enter
2423     {
2424
2425         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2426                       AFS_TRACE_LEVEL_VERBOSE,
2427                       "AFSDeleteDirEntry Deleting dir entry in parent %08lX Entry %08lX %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2428                       ParentObjectInfo,
2429                       DirEntry,
2430                       &DirEntry->NameInformation.FileName,
2431                       DirEntry->ObjectInformation->FileId.Cell,
2432                       DirEntry->ObjectInformation->FileId.Volume,
2433                       DirEntry->ObjectInformation->FileId.Vnode,
2434                       DirEntry->ObjectInformation->FileId.Unique);
2435
2436         AFSRemoveDirNodeFromParent( ParentObjectInfo,
2437                                     DirEntry,
2438                                     TRUE);
2439
2440         //
2441         // Free up the name buffer if it was reallocated
2442         //
2443
2444         if( BooleanFlagOn( DirEntry->Flags, AFS_DIR_RELEASE_NAME_BUFFER))
2445         {
2446
2447             AFSExFreePoolWithTag( DirEntry->NameInformation.FileName.Buffer, 0);
2448         }
2449
2450         if( BooleanFlagOn( DirEntry->Flags, AFS_DIR_RELEASE_TARGET_NAME_BUFFER))
2451         {
2452
2453             AFSExFreePoolWithTag( DirEntry->NameInformation.TargetName.Buffer, 0);
2454         }
2455
2456         //
2457         // Dereference the object for this dir entry
2458         //
2459
2460         ASSERT( DirEntry->ObjectInformation->ObjectReferenceCount > 0);
2461
2462         lCount = InterlockedDecrement( &DirEntry->ObjectInformation->ObjectReferenceCount);
2463
2464         if( lCount <= 0)
2465         {
2466             SetFlag( DirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED);
2467         }
2468
2469         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2470                       AFS_TRACE_LEVEL_VERBOSE,
2471                       "AFSDeleteDirEntry Decrement count on object %08lX Cnt %d\n",
2472                       DirEntry->ObjectInformation,
2473                       DirEntry->ObjectInformation->ObjectReferenceCount);
2474
2475         ExDeleteResourceLite( &DirEntry->NonPaged->Lock);
2476
2477         AFSExFreePoolWithTag( DirEntry->NonPaged, AFS_DIR_ENTRY_NP_TAG);
2478
2479         //
2480         // Free up the dir entry
2481         //
2482
2483         AFSExFreePoolWithTag( DirEntry, AFS_DIR_ENTRY_TAG);
2484     }
2485
2486     return ntStatus;
2487 }
2488
2489 NTSTATUS
2490 AFSRemoveDirNodeFromParent( IN AFSObjectInfoCB *ParentObjectInfo,
2491                             IN AFSDirectoryCB *DirEntry,
2492                             IN BOOLEAN RemoveFromEnumList)
2493 {
2494
2495     NTSTATUS ntStatus = STATUS_SUCCESS;
2496     LONG lCount;
2497
2498     __Enter
2499     {
2500
2501
2502         ASSERT( ExIsResourceAcquiredExclusiveLite( ParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock));
2503
2504         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2505                       AFS_TRACE_LEVEL_VERBOSE,
2506                       "AFSRemoveDirNodeFromParent Removing DirEntry %08lX %wZ FID %08lX-%08lX-%08lX-%08lX from Parent %08lX\n",
2507                       DirEntry,
2508                       &DirEntry->NameInformation.FileName,
2509                       DirEntry->ObjectInformation->FileId.Cell,
2510                       DirEntry->ObjectInformation->FileId.Volume,
2511                       DirEntry->ObjectInformation->FileId.Vnode,
2512                       DirEntry->ObjectInformation->FileId.Unique,
2513                       ParentObjectInfo);
2514
2515         if( !BooleanFlagOn( DirEntry->Flags, AFS_DIR_ENTRY_NOT_IN_PARENT_TREE))
2516         {
2517
2518             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2519                           AFS_TRACE_LEVEL_VERBOSE,
2520                           "AFSRemoveDirNodeFromParent Removing DirEntry %08lX name %wZ\n",
2521                           DirEntry,
2522                           &DirEntry->NameInformation.FileName);
2523
2524             AFSRemoveNameEntry( ParentObjectInfo,
2525                                 DirEntry);
2526         }
2527         else
2528         {
2529
2530             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2531                           AFS_TRACE_LEVEL_VERBOSE,
2532                           "AFSRemoveDirNodeFromParent DE %p for %wZ NOT removing entry due to flag set\n",
2533                           DirEntry,
2534                           &DirEntry->NameInformation.FileName);
2535
2536         }
2537
2538         if( RemoveFromEnumList &&
2539             BooleanFlagOn( DirEntry->Flags, AFS_DIR_ENTRY_INSERTED_ENUM_LIST))
2540         {
2541
2542             //
2543             // And remove the entry from the enumeration list
2544             //
2545
2546             if( DirEntry->ListEntry.fLink == NULL)
2547             {
2548
2549                 ParentObjectInfo->Specific.Directory.DirectoryNodeListTail = (AFSDirectoryCB *)DirEntry->ListEntry.bLink;
2550             }
2551             else
2552             {
2553
2554                 ((AFSDirectoryCB *)DirEntry->ListEntry.fLink)->ListEntry.bLink = DirEntry->ListEntry.bLink;
2555             }
2556
2557             if( DirEntry->ListEntry.bLink == NULL)
2558             {
2559
2560                 ParentObjectInfo->Specific.Directory.DirectoryNodeListHead = (AFSDirectoryCB *)DirEntry->ListEntry.fLink;
2561             }
2562             else
2563             {
2564
2565                 ((AFSDirectoryCB *)DirEntry->ListEntry.bLink)->ListEntry.fLink = DirEntry->ListEntry.fLink;
2566             }
2567
2568             ASSERT( ParentObjectInfo->Specific.Directory.DirectoryNodeCount > 0);
2569
2570             lCount = InterlockedDecrement( &ParentObjectInfo->Specific.Directory.DirectoryNodeCount);
2571
2572             ClearFlag( DirEntry->Flags, AFS_DIR_ENTRY_INSERTED_ENUM_LIST);
2573
2574             AFSDbgLogMsg( AFS_SUBSYSTEM_DIR_NODE_COUNT,
2575                           AFS_TRACE_LEVEL_VERBOSE,
2576                           "AFSRemoveDirNodeFromParent Removing entry %wZ Dec Count %d to parent FID %08lX-%08lX-%08lX-%08lX\n",
2577                           &DirEntry->NameInformation.FileName,
2578                           lCount,
2579                           ParentObjectInfo->FileId.Cell,
2580                           ParentObjectInfo->FileId.Volume,
2581                           ParentObjectInfo->FileId.Vnode,
2582                           ParentObjectInfo->FileId.Unique);
2583
2584             DirEntry->ListEntry.fLink = NULL;
2585             DirEntry->ListEntry.bLink = NULL;
2586         }
2587     }
2588
2589     return ntStatus;
2590 }
2591
2592 NTSTATUS
2593 AFSFixupTargetName( IN OUT PUNICODE_STRING FileName,
2594                     IN OUT PUNICODE_STRING TargetFileName)
2595 {
2596
2597     NTSTATUS ntStatus = STATUS_SUCCESS;
2598     UNICODE_STRING uniFileName;
2599
2600     __Enter
2601     {
2602
2603         //
2604         // We will process backwards from the end of the name looking
2605         // for the first \ we encounter
2606         //
2607
2608         uniFileName.Length = FileName->Length;
2609         uniFileName.MaximumLength = FileName->MaximumLength;
2610
2611         uniFileName.Buffer = FileName->Buffer;
2612
2613         while( TRUE)
2614         {
2615
2616             if( uniFileName.Buffer[ (uniFileName.Length/sizeof( WCHAR)) - 1] == L'\\')
2617             {
2618
2619                 //
2620                 // Subtract one more character off of the filename if it is not the root
2621                 //
2622
2623                 if( uniFileName.Length > sizeof( WCHAR))
2624                 {
2625
2626                     uniFileName.Length -= sizeof( WCHAR);
2627                 }
2628
2629                 //
2630                 // Now build up the target name
2631                 //
2632
2633                 TargetFileName->Length = FileName->Length - uniFileName.Length;
2634
2635                 //
2636                 // If we are not on the root then fixup the name
2637                 //
2638
2639                 if( uniFileName.Length > sizeof( WCHAR))
2640                 {
2641
2642                     TargetFileName->Length -= sizeof( WCHAR);
2643
2644                     TargetFileName->Buffer = &uniFileName.Buffer[ (uniFileName.Length/sizeof( WCHAR)) + 1];
2645                 }
2646                 else
2647                 {
2648
2649                     TargetFileName->Buffer = &uniFileName.Buffer[ uniFileName.Length/sizeof( WCHAR)];
2650                 }
2651
2652                 //
2653                 // Fixup the passed back filename length
2654                 //
2655
2656                 FileName->Length = uniFileName.Length;
2657
2658                 TargetFileName->MaximumLength = TargetFileName->Length;
2659
2660                 break;
2661             }
2662
2663             uniFileName.Length -= sizeof( WCHAR);
2664         }
2665     }
2666
2667     return ntStatus;
2668 }
2669
2670 NTSTATUS
2671 AFSParseName( IN PIRP Irp,
2672               IN GUID *AuthGroup,
2673               OUT PUNICODE_STRING FileName,
2674               OUT PUNICODE_STRING ParsedFileName,
2675               OUT PUNICODE_STRING RootFileName,
2676               OUT ULONG *ParseFlags,
2677               OUT AFSVolumeCB   **VolumeCB,
2678               OUT AFSDirectoryCB **ParentDirectoryCB,
2679               OUT AFSNameArrayHdr **NameArray)
2680 {
2681
2682     NTSTATUS            ntStatus = STATUS_SUCCESS;
2683     PIO_STACK_LOCATION  pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2684     AFSDeviceExt       *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2685     UNICODE_STRING      uniFullName, uniComponentName, uniRemainingPath;
2686     ULONG               ulCRC = 0;
2687     AFSDirectoryCB     *pDirEntry = NULL, *pShareDirEntry = NULL, *pTargetDirEntry = NULL;
2688     USHORT              usIndex = 0, usDriveIndex = 0;
2689     AFSCcb             *pRelatedCcb = NULL;
2690     AFSNameArrayHdr    *pNameArray = NULL, *pRelatedNameArray = NULL;
2691     USHORT              usComponentIndex = 0;
2692     USHORT              usComponentLength = 0;
2693     AFSVolumeCB        *pVolumeCB = NULL;
2694     AFSFcb             *pRelatedFcb = NULL;
2695     BOOLEAN             bReleaseTreeLock = FALSE;
2696     BOOLEAN             bIsAllShare = FALSE;
2697     LONG                lCount;
2698
2699     __Enter
2700     {
2701
2702         //
2703         // Indicate we are opening a root ...
2704         //
2705
2706         *ParseFlags = AFS_PARSE_FLAG_ROOT_ACCESS;
2707
2708         if( pIrpSp->FileObject->RelatedFileObject != NULL)
2709         {
2710
2711             pRelatedFcb = (AFSFcb *)pIrpSp->FileObject->RelatedFileObject->FsContext;
2712
2713             pRelatedCcb = (AFSCcb *)pIrpSp->FileObject->RelatedFileObject->FsContext2;
2714
2715             pRelatedNameArray = pRelatedCcb->NameArray;
2716
2717             uniFullName = pIrpSp->FileObject->FileName;
2718
2719             ASSERT( pRelatedFcb != NULL);
2720
2721             //
2722             // No wild cards in the name
2723             //
2724
2725             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2726                           AFS_TRACE_LEVEL_VERBOSE_2,
2727                           "AFSParseName (%08lX) Relative open for %wZ FID %08lX-%08lX-%08lX-%08lX component %wZ\n",
2728                           Irp,
2729                           &pRelatedCcb->DirectoryCB->NameInformation.FileName,
2730                           pRelatedCcb->DirectoryCB->ObjectInformation->FileId.Cell,
2731                           pRelatedCcb->DirectoryCB->ObjectInformation->FileId.Volume,
2732                           pRelatedCcb->DirectoryCB->ObjectInformation->FileId.Vnode,
2733                           pRelatedCcb->DirectoryCB->ObjectInformation->FileId.Unique,
2734                           &uniFullName);
2735
2736             if( FsRtlDoesNameContainWildCards( &uniFullName))
2737             {
2738
2739                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2740                               AFS_TRACE_LEVEL_ERROR,
2741                               "AFSParseName (%08lX) Component %wZ contains wild cards\n",
2742                               Irp,
2743                               &uniFullName);
2744
2745                 try_return( ntStatus = STATUS_OBJECT_NAME_INVALID);
2746             }
2747
2748             pVolumeCB = pRelatedFcb->ObjectInformation->VolumeCB;
2749
2750             pDirEntry = pRelatedCcb->DirectoryCB;
2751
2752             *FileName = pIrpSp->FileObject->FileName;
2753
2754             //
2755             // Grab the root node while checking state
2756             //
2757
2758             AFSAcquireShared( pVolumeCB->VolumeLock,
2759                               TRUE);
2760
2761             if( BooleanFlagOn( pVolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID) ||
2762                 BooleanFlagOn( pVolumeCB->Flags, AFS_VOLUME_FLAGS_OFFLINE))
2763             {
2764
2765                 //
2766                 // The volume has been taken off line so fail the access
2767                 //
2768
2769                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2770                               AFS_TRACE_LEVEL_ERROR,
2771                               "AFSParseName (%08lX) Volume %08lX:%08lX OFFLINE/INVALID\n",
2772                               Irp,
2773                               pVolumeCB->ObjectInformation.FileId.Cell,
2774                               pVolumeCB->ObjectInformation.FileId.Volume);
2775
2776                 AFSReleaseResource( pVolumeCB->VolumeLock);
2777
2778                 try_return( ntStatus = STATUS_DEVICE_NOT_READY);
2779             }
2780
2781             if( BooleanFlagOn( pVolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_VERIFY))
2782             {
2783
2784                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2785                               AFS_TRACE_LEVEL_VERBOSE,
2786                               "AFSParseName (%08lX) Verifying root of volume %08lX:%08lX\n",
2787                               Irp,
2788                               pVolumeCB->ObjectInformation.FileId.Cell,
2789                               pVolumeCB->ObjectInformation.FileId.Volume);
2790
2791                 ntStatus = AFSVerifyVolume( (ULONGLONG)PsGetCurrentProcessId(),
2792                                             pVolumeCB);
2793
2794                 if( !NT_SUCCESS( ntStatus))
2795                 {
2796
2797                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2798                                   AFS_TRACE_LEVEL_ERROR,
2799                                   "AFSParseName (%08lX) Failed verification of root Status %08lX\n",
2800                                   Irp,
2801                                   ntStatus);
2802
2803                     AFSReleaseResource( pVolumeCB->VolumeLock);
2804
2805                     try_return( ntStatus);
2806                 }
2807             }
2808
2809             AFSReleaseResource( pVolumeCB->VolumeLock);
2810
2811             if( BooleanFlagOn( pDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_VERIFY))
2812             {
2813
2814                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2815                               AFS_TRACE_LEVEL_VERBOSE,
2816                               "AFSParseName (%08lX) Verifying parent %wZ FID %08lX-%08lX-%08lX-%08lX\n",
2817                               Irp,
2818                               &pDirEntry->NameInformation.FileName,
2819                               pDirEntry->ObjectInformation->FileId.Cell,
2820                               pDirEntry->ObjectInformation->FileId.Volume,
2821                               pDirEntry->ObjectInformation->FileId.Vnode,
2822                               pDirEntry->ObjectInformation->FileId.Unique);
2823
2824                 //
2825                 // Directory TreeLock should be exclusively held
2826                 //
2827
2828                 AFSAcquireExcl( pDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock,
2829                                 TRUE);
2830
2831                 ntStatus = AFSVerifyEntry( AuthGroup,
2832                                            pDirEntry);
2833
2834                 AFSReleaseResource( pDirEntry->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
2835
2836                 if( !NT_SUCCESS( ntStatus))
2837                 {
2838
2839                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2840                                   AFS_TRACE_LEVEL_VERBOSE,
2841                                   "AFSParseName (%08lX) Failed verification of parent %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
2842                                   Irp,
2843                                   &pDirEntry->NameInformation.FileName,
2844                                   pDirEntry->ObjectInformation->FileId.Cell,
2845                                   pDirEntry->ObjectInformation->FileId.Volume,
2846                                   pDirEntry->ObjectInformation->FileId.Vnode,
2847                                   pDirEntry->ObjectInformation->FileId.Unique,
2848                                   ntStatus);
2849
2850                     try_return( ntStatus);
2851                 }
2852             }
2853
2854             //
2855             // Create our full path name buffer
2856             //
2857
2858             uniFullName.MaximumLength = pRelatedCcb->FullFileName.Length +
2859                                                     sizeof( WCHAR) +
2860                                                     pIrpSp->FileObject->FileName.Length +
2861                                                     sizeof( WCHAR);
2862
2863             uniFullName.Length = 0;
2864
2865             uniFullName.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
2866                                                                     uniFullName.MaximumLength,
2867                                                                     AFS_NAME_BUFFER_THREE_TAG);
2868
2869             if( uniFullName.Buffer == NULL)
2870             {
2871
2872                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2873                               AFS_TRACE_LEVEL_ERROR,
2874                               "AFSParseName (%08lX) Failed to allocate full name buffer\n",
2875                               Irp);
2876
2877                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2878             }
2879
2880             RtlZeroMemory( uniFullName.Buffer,
2881                            uniFullName.MaximumLength);
2882
2883             RtlCopyMemory( uniFullName.Buffer,
2884                            pRelatedCcb->FullFileName.Buffer,
2885                            pRelatedCcb->FullFileName.Length);
2886
2887             uniFullName.Length = pRelatedCcb->FullFileName.Length;
2888
2889             usComponentIndex = (USHORT)(uniFullName.Length/sizeof( WCHAR));
2890
2891             usComponentLength = pIrpSp->FileObject->FileName.Length;
2892
2893             if( uniFullName.Buffer[ (uniFullName.Length/sizeof( WCHAR)) - 1] != L'\\' &&
2894                 pIrpSp->FileObject->FileName.Length > 0 &&
2895                 pIrpSp->FileObject->FileName.Buffer[ 0] != L'\\' &&
2896                 pIrpSp->FileObject->FileName.Buffer[ 0] != L':')
2897             {
2898
2899                 uniFullName.Buffer[ (uniFullName.Length/sizeof( WCHAR))] = L'\\';
2900
2901                 uniFullName.Length += sizeof( WCHAR);
2902
2903                 usComponentLength += sizeof( WCHAR);
2904             }
2905
2906             if( pIrpSp->FileObject->FileName.Length > 0)
2907             {
2908
2909                 RtlCopyMemory( &uniFullName.Buffer[ uniFullName.Length/sizeof( WCHAR)],
2910                                pIrpSp->FileObject->FileName.Buffer,
2911                                pIr\epSp->FileObject->FileName.Length);
2912
2913                 uniFullName.Length += pIrpSp->FileObject->FileName.Length;
2914             }
2915
2916             *RootFileName = uniFullName;
2917
2918             //
2919             // We populate up to the current parent
2920             //
2921
2922             if( pRelatedNameArray == NULL)
2923             {
2924
2925                 //
2926                 // Init and populate our name array
2927                 //
2928
2929                 pNameArray = AFSInitNameArray( NULL,
2930                                                0);
2931
2932                 if( pNameArray == NULL)
2933                 {
2934
2935                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2936                                   AFS_TRACE_LEVEL_VERBOSE,
2937                                   "AFSParseName (%08lX) Failed to initialize name array\n",
2938                                   Irp);
2939
2940                     AFSExFreePoolWithTag( uniFullName.Buffer, 0);
2941
2942                     try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2943                 }
2944
2945                 ntStatus = AFSPopulateNameArray( pNameArray,
2946                                                  NULL,
2947                                                  pRelatedCcb->DirectoryCB);
2948             }
2949             else
2950             {
2951
2952                 //
2953                 // Init and populate our name array
2954                 //
2955
2956                 pNameArray = AFSInitNameArray( NULL,
2957                                                pRelatedNameArray->MaxElementCount);
2958
2959                 if( pNameArray == NULL)
2960                 {
2961
2962                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2963                                   AFS_TRACE_LEVEL_VERBOSE,
2964                                   "AFSParseName (%08lX) Failed to initialize name array\n",
2965                                   Irp);
2966
2967                     AFSExFreePoolWithTag( uniFullName.Buffer, 0);
2968
2969                     try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2970                 }
2971
2972                 ntStatus = AFSPopulateNameArrayFromRelatedArray( pNameArray,
2973                                                                  pRelatedNameArray,
2974                                                                  pRelatedCcb->DirectoryCB);
2975             }
2976
2977             if( !NT_SUCCESS( ntStatus))
2978             {
2979
2980                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2981                               AFS_TRACE_LEVEL_VERBOSE,
2982                               "AFSParseName (%08lX) Failed to populate name array\n",
2983                               Irp);
2984
2985                 AFSExFreePoolWithTag( uniFullName.Buffer, 0);
2986
2987                 try_return( ntStatus);
2988             }
2989
2990             ParsedFileName->Length = usComponentLength;
2991             ParsedFileName->MaximumLength = uniFullName.MaximumLength;
2992
2993             ParsedFileName->Buffer = &uniFullName.Buffer[ usComponentIndex];
2994
2995             //
2996             // Indicate to caller that RootFileName must be freed
2997             //
2998
2999             SetFlag( *ParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER);
3000
3001             *NameArray = pNameArray;
3002
3003             *VolumeCB = pVolumeCB;
3004
3005             //
3006             // Increment our volume reference count
3007             //
3008
3009             lCount = InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
3010
3011             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
3012                           AFS_TRACE_LEVEL_VERBOSE,
3013                           "AFSParseName Increment count on volume %08lX Cnt %d\n",
3014                           pVolumeCB,
3015                           lCount);
3016
3017             *ParentDirectoryCB = pDirEntry;
3018
3019             lCount = InterlockedIncrement( &pDirEntry->OpenReferenceCount);
3020
3021             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
3022                           AFS_TRACE_LEVEL_VERBOSE,
3023                           "AFSParseName Increment1 count on %wZ DE %p Ccb %p Cnt %d\n",
3024                           &pDirEntry->NameInformation.FileName,
3025                           pDirEntry,
3026                           NULL,
3027                           lCount);
3028
3029             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3030                           AFS_TRACE_LEVEL_VERBOSE_2,
3031                           "AFSParseName (%08lX) Returning full name %wZ\n",
3032                           Irp,
3033                           &uniFullName);
3034
3035             try_return( ntStatus);
3036         }
3037
3038         //
3039         // No wild cards in the name
3040         //
3041
3042         uniFullName = pIrpSp->FileObject->FileName;
3043
3044         if( FsRtlDoesNameContainWildCards( &uniFullName) ||
3045             uniFullName.Length < AFSServerName.Length)
3046         {
3047
3048             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3049                           AFS_TRACE_LEVEL_ERROR,
3050                           "AFSParseName (%08lX) Name %wZ contains wild cards or too short\n",
3051                           Irp,
3052                           &uniFullName);
3053
3054             try_return( ntStatus = STATUS_OBJECT_NAME_INVALID);
3055         }
3056
3057         //
3058         // The name is a fully qualified name. Parse out the server/share names and
3059         // point to the root qualified name
3060         // First thing is to locate the server name
3061         //
3062
3063         FsRtlDissectName( uniFullName,
3064                           &uniComponentName,
3065                           &uniRemainingPath);
3066
3067         uniFullName = uniRemainingPath;
3068
3069         //
3070         // This component is the server name we are serving
3071         //
3072
3073         if( RtlCompareUnicodeString( &uniComponentName,
3074                                      &AFSServerName,
3075                                      TRUE) != 0)
3076         {
3077
3078             //
3079             // Drive letter based name?
3080             //
3081
3082             uniFullName = pIrpSp->FileObject->FileName;
3083
3084             while( usIndex < uniFullName.Length/sizeof( WCHAR))
3085             {
3086
3087                 if( uniFullName.Buffer[ usIndex] == L':')
3088                 {
3089
3090                     uniFullName.Buffer = &uniFullName.Buffer[ usIndex + 2];
3091
3092                     uniFullName.Length -= (usIndex + 2) * sizeof( WCHAR);
3093
3094                     usDriveIndex = usIndex - 1;
3095
3096                     break;
3097                 }
3098
3099                 usIndex++;
3100             }
3101
3102             //
3103             // Do we have the right server name now?
3104             //
3105
3106             FsRtlDissectName( uniFullName,
3107                               &uniComponentName,
3108                               &uniRemainingPath);
3109
3110             uniFullName = uniRemainingPath;
3111
3112             //
3113             // This component is the server name we are serving
3114             //
3115
3116             if( RtlCompareUnicodeString( &uniComponentName,
3117                                          &AFSServerName,
3118                                          TRUE) != 0)
3119             {
3120
3121                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3122                               AFS_TRACE_LEVEL_ERROR,
3123                               "AFSParseName (%08lX) Name %wZ does not have server name\n",
3124                               Irp,
3125                               &pIrpSp->FileObject->FileName);
3126
3127                 try_return( ntStatus = STATUS_BAD_NETWORK_NAME);
3128             }
3129
3130             //
3131             // Validate this drive letter is actively mapped
3132             //
3133
3134             if( usDriveIndex > 0 &&
3135                 !AFSIsDriveMapped( pIrpSp->FileObject->FileName.Buffer[ usDriveIndex]))
3136             {
3137
3138                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3139                               AFS_TRACE_LEVEL_ERROR,
3140                               "AFSParseName (%08lX) Name %wZ contains invalid drive mapping\n",
3141                               Irp,
3142                               &pIrpSp->FileObject->FileName);
3143
3144                 try_return( ntStatus = STATUS_BAD_NETWORK_NAME);
3145             }
3146         }
3147
3148         if( FsRtlDoesNameContainWildCards( &uniFullName))
3149         {
3150
3151             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3152                           AFS_TRACE_LEVEL_ERROR,
3153                           "AFSParseName (%08lX) Component %wZ contains wild cards\n",
3154                           Irp,
3155                           &uniFullName);
3156
3157             try_return( ntStatus = STATUS_OBJECT_NAME_INVALID);
3158         }
3159
3160         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3161                       AFS_TRACE_LEVEL_VERBOSE_2,
3162                       "AFSParseName (%08lX) Processing full name %wZ\n",
3163                       Irp,
3164                       &uniFullName);
3165
3166         if( uniFullName.Length > 0 &&
3167             uniFullName.Buffer[ (uniFullName.Length/sizeof( WCHAR)) - 1] == L'\\')
3168         {
3169
3170             uniFullName.Length -= sizeof( WCHAR);
3171         }
3172
3173         //
3174         // Be sure we are online and ready to go
3175         //
3176
3177         AFSAcquireShared( AFSGlobalRoot->VolumeLock,
3178                           TRUE);
3179
3180         if( BooleanFlagOn( AFSGlobalRoot->ObjectInformation.Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID) ||
3181             BooleanFlagOn( AFSGlobalRoot->Flags, AFS_VOLUME_FLAGS_OFFLINE))
3182         {
3183
3184             //
3185             // The volume has been taken off line so fail the access
3186             //
3187
3188             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3189                           AFS_TRACE_LEVEL_ERROR,
3190                           "AFSParseName (%08lX) Volume %08lX:%08lX OFFLINE/INVALID\n",
3191                           Irp,
3192                           AFSGlobalRoot->ObjectInformation.FileId.Cell,
3193                           AFSGlobalRoot->ObjectInformation.FileId.Volume);
3194
3195             AFSReleaseResource( AFSGlobalRoot->VolumeLock);
3196
3197             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
3198         }
3199
3200         if( BooleanFlagOn( AFSGlobalRoot->ObjectInformation.Flags, AFS_OBJECT_FLAGS_VERIFY))
3201         {
3202
3203             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3204                           AFS_TRACE_LEVEL_VERBOSE,
3205                           "AFSParseName (%08lX) Verifying root of volume %08lX:%08lX\n",
3206                           Irp,
3207                           AFSGlobalRoot->ObjectInformation.FileId.Cell,
3208                           AFSGlobalRoot->ObjectInformation.FileId.Volume);
3209
3210             ntStatus = AFSVerifyVolume( (ULONGLONG)PsGetCurrentProcessId(),
3211                                         AFSGlobalRoot);
3212
3213             if( !NT_SUCCESS( ntStatus))
3214             {
3215
3216                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3217                               AFS_TRACE_LEVEL_ERROR,
3218                               "AFSParseName (%08lX) Failed verification of root Status %08lX\n",
3219                               Irp,
3220                               ntStatus);
3221
3222                 AFSReleaseResource( AFSGlobalRoot->VolumeLock);
3223
3224                 try_return( ntStatus);
3225             }
3226         }
3227
3228         AFSReleaseResource( AFSGlobalRoot->VolumeLock);
3229
3230         if( !BooleanFlagOn( AFSGlobalRoot->ObjectInformation.Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
3231         {
3232
3233             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3234                           AFS_TRACE_LEVEL_VERBOSE,
3235                           "AFSParseName (%08lX) Enumerating global root of volume %08lX:%08lX\n",
3236                           Irp,
3237                           AFSGlobalRoot->ObjectInformation.FileId.Cell,
3238                           AFSGlobalRoot->ObjectInformation.FileId.Volume);
3239
3240             ntStatus = AFSEnumerateGlobalRoot( AuthGroup);
3241
3242             if( !NT_SUCCESS( ntStatus))
3243             {
3244
3245                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3246                               AFS_TRACE_LEVEL_ERROR,
3247                               "AFSParseName (%08lX) Failed enumeraiton of root Status %08lX\n",
3248                               Irp,
3249                               ntStatus);
3250
3251                 try_return( ntStatus);
3252             }
3253         }
3254
3255         //
3256         // Check for the \\Server access and return it as though it where \\Server\Globalroot
3257         //
3258
3259         if( uniRemainingPath.Buffer == NULL ||
3260             ( uniRemainingPath.Length == sizeof( WCHAR) &&
3261               uniRemainingPath.Buffer[ 0] == L'\\'))
3262         {
3263
3264             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3265                           AFS_TRACE_LEVEL_VERBOSE_2,