Windows: DirOpenReferenceCount reorganizing completed
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSCreate.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: AFSCreate.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 //
42 // Function: AFSCreate
43 //
44 // Description:
45 //
46 //      This function is the dispatch handler for the IRP_MJ_CREATE requests. It makes the determination to
47 //      which interface this request is destined.
48 //
49 // Return:
50 //
51 //      A status is returned for the function. The Irp completion processing is handled in the specific
52 //      interface handler.
53 //
54
55 NTSTATUS
56 AFSCreate( IN PDEVICE_OBJECT LibDeviceObject,
57            IN PIRP Irp)
58 {
59     UNREFERENCED_PARAMETER(LibDeviceObject);
60     NTSTATUS ntStatus = STATUS_SUCCESS;
61     IO_STACK_LOCATION  *pIrpSp;
62     FILE_OBJECT        *pFileObject = NULL;
63
64     __try
65     {
66
67         pIrpSp = IoGetCurrentIrpStackLocation( Irp);
68         pFileObject = pIrpSp->FileObject;
69
70         if( pFileObject == NULL ||
71             pFileObject->FileName.Buffer == NULL)
72         {
73
74             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
75                           AFS_TRACE_LEVEL_VERBOSE,
76                           "AFSCreate (%p) Processing control device open request\n",
77                           Irp);
78
79             ntStatus = AFSControlDeviceCreate( Irp);
80
81             try_return( ntStatus);
82         }
83
84         if( AFSRDRDeviceObject == NULL)
85         {
86
87             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
88                           AFS_TRACE_LEVEL_VERBOSE,
89                           "AFSCreate (%p) Invalid request to open before library is initialized\n",
90                           Irp);
91
92             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
93         }
94
95         ntStatus = AFSCommonCreate( AFSRDRDeviceObject,
96                                     Irp);
97
98 try_exit:
99
100         NOTHING;
101     }
102     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
103     {
104
105         AFSDbgLogMsg( 0,
106                       0,
107                       "EXCEPTION - AFSCreate\n");
108
109         ntStatus = STATUS_ACCESS_DENIED;
110
111         AFSDumpTraceFilesFnc();
112     }
113
114     //
115     // Complete the request
116     //
117
118     AFSCompleteRequest( Irp,
119                           ntStatus);
120
121     return ntStatus;
122 }
123
124 NTSTATUS
125 AFSCommonCreate( IN PDEVICE_OBJECT DeviceObject,
126                  IN PIRP Irp)
127 {
128
129     NTSTATUS            ntStatus = STATUS_SUCCESS;
130     UNICODE_STRING      uniFileName;
131     ULONG               ulCreateDisposition = 0;
132     ULONG               ulOptions = 0;
133     BOOLEAN             bNoIntermediateBuffering = FALSE;
134     FILE_OBJECT        *pFileObject = NULL;
135     IO_STACK_LOCATION  *pIrpSp;
136     AFSFcb             *pFcb = NULL;
137     AFSCcb             *pCcb = NULL;
138     AFSDeviceExt       *pDeviceExt = NULL;
139     BOOLEAN             bOpenTargetDirectory = FALSE, bReleaseVolume = FALSE;
140     PACCESS_MASK        pDesiredAccess = NULL;
141     UNICODE_STRING      uniComponentName, uniRootFileName, uniParsedFileName;
142     UNICODE_STRING      uniSubstitutedPathName;
143     UNICODE_STRING      uniRelativeName;
144     AFSNameArrayHdr    *pNameArray = NULL;
145     AFSVolumeCB        *pVolumeCB = NULL;
146     AFSDirectoryCB     *pParentDirectoryCB = NULL, *pDirectoryCB = NULL;
147     BOOLEAN             bReleaseParentDir = FALSE, bReleaseDir = FALSE;
148     ULONG               ulParseFlags = 0;
149     GUID                stAuthGroup = {0};
150     ULONG               ulNameProcessingFlags = 0;
151     BOOLEAN             bOpenedReparsePoint = FALSE;
152     LONG                lCount;
153
154     __Enter
155     {
156
157         pIrpSp = IoGetCurrentIrpStackLocation( Irp);
158         pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
159         ulCreateDisposition = (pIrpSp->Parameters.Create.Options >> 24) & 0x000000ff;
160         ulOptions = pIrpSp->Parameters.Create.Options;
161         bNoIntermediateBuffering = BooleanFlagOn( ulOptions, FILE_NO_INTERMEDIATE_BUFFERING);
162         bOpenTargetDirectory = BooleanFlagOn( pIrpSp->Flags, SL_OPEN_TARGET_DIRECTORY);
163         pFileObject = pIrpSp->FileObject;
164         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
165
166         uniFileName.Length = uniFileName.MaximumLength = 0;
167         uniFileName.Buffer = NULL;
168
169         uniRootFileName.Length = uniRootFileName.MaximumLength = 0;
170         uniRootFileName.Buffer = NULL;
171
172         uniParsedFileName.Length = uniParsedFileName.MaximumLength = 0;
173         uniParsedFileName.Buffer = NULL;
174
175         uniSubstitutedPathName.Buffer = NULL;
176         uniSubstitutedPathName.Length = 0;
177
178         uniRelativeName.Buffer = NULL;
179         uniRelativeName.Length = 0;
180
181         if( AFSGlobalRoot == NULL)
182         {
183             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
184         }
185
186         RtlZeroMemory( &stAuthGroup,
187                        sizeof( GUID));
188
189         AFSRetrieveAuthGroupFnc( (ULONGLONG)PsGetCurrentProcessId(),
190                                  (ULONGLONG)PsGetCurrentThreadId(),
191                                   &stAuthGroup);
192
193         //
194         // If we are in shutdown mode then fail the request
195         //
196
197         if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
198         {
199
200             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
201                           AFS_TRACE_LEVEL_WARNING,
202                           "AFSCommonCreate (%p) Open request after shutdown\n",
203                           Irp);
204
205             try_return( ntStatus = STATUS_TOO_LATE);
206         }
207
208         if( !BooleanFlagOn( AFSGlobalRoot->ObjectInformation.Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
209         {
210
211             ntStatus = AFSEnumerateGlobalRoot( &stAuthGroup);
212
213             if( !NT_SUCCESS( ntStatus))
214             {
215
216                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
217                               AFS_TRACE_LEVEL_ERROR,
218                               "AFSCommonCreate Failed to enumerate global root Status %08lX\n",
219                               ntStatus);
220
221                 try_return( ntStatus);
222             }
223         }
224
225         //
226         // Go and parse the name for processing.
227         // If ulParseFlags is returned with AFS_PARSE_FLAG_FREE_FILE_BUFFER set,
228         // then we are responsible for releasing the uniRootFileName.Buffer.
229         //
230
231         ntStatus = AFSParseName( Irp,
232                                  &stAuthGroup,
233                                  &uniFileName,
234                                  &uniParsedFileName,
235                                  &uniRootFileName,
236                                  &ulParseFlags,
237                                  &pVolumeCB,
238                                  &pParentDirectoryCB,
239                                  &pNameArray);
240
241         if( !NT_SUCCESS( ntStatus))
242         {
243
244             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
245                           uniFileName.Length > 0 ? AFS_TRACE_LEVEL_ERROR : AFS_TRACE_LEVEL_VERBOSE,
246                           "AFSCommonCreate (%p) Failed to parse name \"%wZ\" Status %08lX\n",
247                           Irp,
248                           &uniFileName,
249                           ntStatus);
250
251             try_return( ntStatus);
252         }
253
254         //
255         // Check for STATUS_REPARSE
256         //
257
258         if( ntStatus == STATUS_REPARSE)
259         {
260
261             //
262             // Update the information and return
263             //
264
265             Irp->IoStatus.Information = IO_REPARSE;
266
267             try_return( ntStatus);
268         }
269
270         if ( pParentDirectoryCB != NULL)
271         {
272
273             bReleaseParentDir = TRUE;
274         }
275
276         //
277         // If the returned volume cb is NULL then we are dealing with the \\Server\GlobalRoot
278         // name
279         //
280
281         if( pVolumeCB == NULL)
282         {
283
284             //
285             // Remove any leading or trailing slashes
286             //
287
288             if( uniFileName.Length >= sizeof( WCHAR) &&
289                 uniFileName.Buffer[ (uniFileName.Length/sizeof( WCHAR)) - 1] == L'\\')
290             {
291
292                 uniFileName.Length -= sizeof( WCHAR);
293             }
294
295             if( uniFileName.Length >= sizeof( WCHAR) &&
296                 uniFileName.Buffer[ 0] == L'\\')
297             {
298
299                 uniFileName.Buffer = &uniFileName.Buffer[ 1];
300
301                 uniFileName.Length -= sizeof( WCHAR);
302             }
303
304             //
305             // If there is a remaining portion returned for this request then
306             // check if it is for the PIOCtl interface
307             //
308
309             if( uniFileName.Length > 0)
310             {
311
312                 //
313                 // We don't accept any other opens off of the AFS Root
314                 //
315
316                 ntStatus = STATUS_OBJECT_NAME_NOT_FOUND;
317
318                 //
319                 // If this is an open on "_._AFS_IOCTL_._" then perform handling on it accordingly
320                 //
321
322                 if( RtlCompareUnicodeString( &AFSPIOCtlName,
323                                              &uniFileName,
324                                              TRUE) == 0)
325                 {
326
327                     //
328                     // AFSOpenIOCtlFcb does not free a DirOpenReferenceCount for
329                     // AFSGlobalRoot->DirectoryCB.
330                     //
331
332                     ntStatus = AFSOpenIOCtlFcb( Irp,
333                                                 &stAuthGroup,
334                                                 AFSGlobalRoot->DirectoryCB,
335                                                 &pFcb,
336                                                 &pCcb);
337
338                     if( !NT_SUCCESS( ntStatus))
339                     {
340
341                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
342                                       AFS_TRACE_LEVEL_ERROR,
343                                       "AFSCommonCreate Failed to open root IOCtl Fcb Status %08lX\n",
344                                       ntStatus);
345                     }
346                 }
347                 else if( pParentDirectoryCB != NULL)
348                 {
349
350                     if( pParentDirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SPECIAL_SHARE_NAME)
351                     {
352
353                         ntStatus = AFSOpenSpecialShareFcb( Irp,
354                                                            &stAuthGroup,
355                                                            pParentDirectoryCB,
356                                                            &pFcb,
357                                                            &pCcb);
358
359                         if( !NT_SUCCESS( ntStatus))
360                         {
361
362                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
363                                           AFS_TRACE_LEVEL_ERROR,
364                                           "AFSCommonCreate Failed to open special share Fcb Status %08lX\n",
365                                           ntStatus);
366                         }
367                     }
368                 }
369
370                 try_return( ntStatus);
371             }
372
373             ntStatus = AFSOpenAFSRoot( Irp,
374                                        &pFcb,
375                                        &pCcb);
376
377             if( !NT_SUCCESS( ntStatus))
378             {
379
380                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
381                               AFS_TRACE_LEVEL_ERROR,
382                               "AFSCommonCreate Failed to open root Status %08lX\n",
383                               ntStatus);
384             }
385
386             try_return( ntStatus);
387         }
388
389         //
390         // We have a reference on the root volume
391         //
392
393         bReleaseVolume = TRUE;
394
395         //
396         // Attempt to locate the node in the name tree if this is not a target
397         // open and the target is not the root
398         //
399
400         uniComponentName.Length = 0;
401         uniComponentName.Buffer = NULL;
402
403         if( uniFileName.Length > sizeof( WCHAR) ||
404             uniFileName.Buffer[ 0] != L'\\')
405         {
406
407             if( !AFSValidNameFormat( &uniFileName))
408             {
409
410                 ntStatus = STATUS_OBJECT_NAME_NOT_FOUND;
411
412                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
413                               AFS_TRACE_LEVEL_VERBOSE,
414                               "AFSCommonCreate (%p) Invalid name %wZ Status %08lX\n",
415                               Irp,
416                               &uniFileName,
417                               ntStatus);
418
419                 try_return( ntStatus);
420             }
421
422             //
423             // Opening a reparse point directly?
424             //
425
426             ulNameProcessingFlags = AFS_LOCATE_FLAGS_SUBSTITUTE_NAME;
427
428             if( BooleanFlagOn( ulOptions, FILE_OPEN_REPARSE_POINT))
429             {
430                 ulNameProcessingFlags |= (AFS_LOCATE_FLAGS_NO_MP_TARGET_EVAL |
431                                           AFS_LOCATE_FLAGS_NO_SL_TARGET_EVAL |
432                                           AFS_LOCATE_FLAGS_NO_DFS_LINK_EVAL);
433             }
434
435             uniSubstitutedPathName = uniRootFileName;
436
437             ntStatus = AFSLocateNameEntry( &stAuthGroup,
438                                            pFileObject,
439                                            &uniRootFileName,
440                                            &uniParsedFileName,
441                                            pNameArray,
442                                            ulNameProcessingFlags,
443                                            &pVolumeCB,
444                                            &pParentDirectoryCB,
445                                            &pDirectoryCB,
446                                            &uniComponentName);
447
448             if( !NT_SUCCESS( ntStatus) &&
449                 ntStatus != STATUS_OBJECT_NAME_NOT_FOUND)
450             {
451
452                 if ( uniSubstitutedPathName.Buffer == uniRootFileName.Buffer)
453                 {
454                     uniSubstitutedPathName.Buffer = NULL;
455                 }
456
457                 //
458                 // AFSLocateNameEntry released the Parent while walking the
459                 // branch
460                 //
461
462                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
463                               AFS_TRACE_LEVEL_VERBOSE,
464                               "AFSCommonCreate (%p) Failed to locate name entry for %wZ Status %08lX\n",
465                               Irp,
466                               &uniFileName,
467                               ntStatus);
468
469                 //
470                 // We released any root volume locks in the above on failure
471                 //
472
473                 bReleaseVolume = FALSE;
474
475                 bReleaseParentDir = FALSE;
476
477                 try_return( ntStatus);
478             }
479
480             //
481             // Check for STATUS_REPARSE
482             //
483
484             if( ntStatus == STATUS_REPARSE)
485             {
486
487                 uniSubstitutedPathName.Buffer = NULL;
488
489                 //
490                 // Update the information and return
491                 //
492
493                 Irp->IoStatus.Information = IO_REPARSE;
494
495                 //
496                 // We released the volume lock above
497                 //
498
499                 bReleaseVolume = FALSE;
500
501                 bReleaseParentDir = FALSE;
502
503                 try_return( ntStatus);
504             }
505
506             //
507             // If we re-allocated the name, then update our substitute name
508             //
509
510             if( uniSubstitutedPathName.Buffer != uniRootFileName.Buffer)
511             {
512
513                 uniSubstitutedPathName = uniRootFileName;
514             }
515             else
516             {
517
518                 uniSubstitutedPathName.Buffer = NULL;
519             }
520
521             //
522             // Check for a symlink access
523             //
524
525             if( ntStatus == STATUS_OBJECT_NAME_NOT_FOUND &&
526                 pParentDirectoryCB != NULL)
527             {
528
529                 //
530                 // pParentDirectoryCB DirOpenReferenceCount is still held
531                 //
532
533                 UNICODE_STRING uniFinalComponent;
534
535                 uniFinalComponent.Length = 0;
536                 uniFinalComponent.MaximumLength = 0;
537                 uniFinalComponent.Buffer = NULL;
538
539                 AFSRetrieveFinalComponent( &uniFileName,
540                                            &uniFinalComponent);
541
542                 ntStatus = AFSCheckSymlinkAccess( pParentDirectoryCB,
543                                                   &uniFinalComponent);
544
545                 if( !NT_SUCCESS( ntStatus) &&
546                     ntStatus != STATUS_OBJECT_NAME_NOT_FOUND)
547                 {
548
549                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
550                                   AFS_TRACE_LEVEL_VERBOSE,
551                                   "AFSCommonCreate (%p) Failing access to symlink %wZ Status %08lX\n",
552                                   Irp,
553                                   &uniFileName,
554                                   ntStatus);
555
556                     try_return( ntStatus);
557                 }
558             }
559             else
560             {
561
562                 //
563                 // AFSLocateNameEntry succeeded.  The parent directory reference
564                 // has been released and if there is a directory returned, it is
565                 // referenced.
566                 //
567
568                 bReleaseParentDir = FALSE;
569
570                 if ( pDirectoryCB)
571                 {
572
573                     bReleaseDir = TRUE;
574                 }
575             }
576         }
577
578         //
579         // If we have no parent then this is a root open, be sure there is a directory entry
580         // for the root
581         //
582
583         else if( pParentDirectoryCB == NULL &&
584                  pDirectoryCB == NULL)
585         {
586
587             pDirectoryCB = pVolumeCB->DirectoryCB;
588
589             lCount = InterlockedIncrement( &pDirectoryCB->DirOpenReferenceCount);
590
591             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
592                           AFS_TRACE_LEVEL_VERBOSE,
593                           "AFSCommonCreate Increment0 count on %wZ DE %p Ccb %p Cnt %d\n",
594                           &pDirectoryCB->NameInformation.FileName,
595                           pDirectoryCB,
596                           pCcb,
597                           lCount);
598
599             bReleaseDir = TRUE;
600         }
601
602         if( bOpenTargetDirectory)
603         {
604
605             //
606             // If we have a directory cb for the entry then dereference it and reference the parent
607             //
608
609             if( pDirectoryCB != NULL)
610             {
611
612                 if ( !bReleaseParentDir)
613                 {
614
615                     //
616                     // Perform in this order to prevent thrashing
617                     //
618
619                     lCount = InterlockedIncrement( &pParentDirectoryCB->DirOpenReferenceCount);
620
621                     AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
622                                   AFS_TRACE_LEVEL_VERBOSE,
623                                   "AFSCommonCreate Increment1 count on %wZ DE %p Ccb %p Cnt %d\n",
624                                   &pParentDirectoryCB->NameInformation.FileName,
625                                   pParentDirectoryCB,
626                                   pCcb,
627                                   lCount);
628
629                     bReleaseParentDir = TRUE;
630                 }
631
632                 //
633                 // Do NOT decrement the reference count on the pDirectoryCB yet.
634                 // The BackupEntry below might drop the count to zero leaving
635                 // the entry subject to being deleted and we need some of the
636                 // contents during later processing
637                 //
638
639                 AFSBackupEntry( pNameArray);
640             }
641
642             //
643             // OK, open the target directory
644             //
645
646             if( uniComponentName.Length == 0)
647             {
648                 AFSRetrieveFinalComponent( &uniFileName,
649                                            &uniComponentName);
650             }
651
652             ntStatus = AFSOpenTargetDirectory( Irp,
653                                                pVolumeCB,
654                                                pParentDirectoryCB,
655                                                pDirectoryCB,
656                                                &uniComponentName,
657                                                &pFcb,
658                                                &pCcb);
659
660             if( !NT_SUCCESS( ntStatus))
661             {
662
663                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
664                               AFS_TRACE_LEVEL_ERROR,
665                               "AFSCommonCreate Failed to open target directory %wZ Status %08lX\n",
666                               &pParentDirectoryCB->NameInformation.FileName,
667                               ntStatus);
668             }
669
670             try_return( ntStatus);
671         }
672
673         if ( BooleanFlagOn( ulOptions, FILE_OPEN_REPARSE_POINT))
674         {
675
676             if( pDirectoryCB == NULL ||
677                 !BooleanFlagOn( pDirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_REPARSE_POINT))
678             {
679                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
680                               AFS_TRACE_LEVEL_VERBOSE,
681                               "AFSCommonCreate (%p) Reparse open request but attribute not set for %wZ DirCB %p Type %08lX\n",
682                               Irp,
683                               &uniFileName,
684                               pDirectoryCB,
685                               pDirectoryCB ? pDirectoryCB->ObjectInformation->FileType : 0);
686             }
687             else
688             {
689                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
690                               AFS_TRACE_LEVEL_VERBOSE,
691                               "AFSCommonCreate (%p) Opening as reparse point %wZ Type %08lX\n",
692                               Irp,
693                               &uniFileName,
694                               pDirectoryCB->ObjectInformation->FileType);
695
696                 bOpenedReparsePoint = TRUE;
697             }
698         }
699
700         //
701         // Based on the options passed in, process the file accordingly.
702         //
703
704         if( ulCreateDisposition == FILE_CREATE ||
705             ( ( ulCreateDisposition == FILE_OPEN_IF ||
706                 ulCreateDisposition == FILE_OVERWRITE_IF) &&
707               pDirectoryCB == NULL))
708         {
709
710             if( uniComponentName.Length == 0 ||
711                 pDirectoryCB != NULL)
712             {
713
714                 //
715                 // We traversed the entire path so we found each entry,
716                 // fail with collision
717                 //
718
719                 if( pDirectoryCB != NULL)
720                 {
721
722                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
723                                   AFS_TRACE_LEVEL_VERBOSE,
724                                   "AFSCommonCreate Object name collision on create of %wZ Status %08lX\n",
725                                   &pDirectoryCB->NameInformation.FileName,
726                                   ntStatus);
727                 }
728                 else
729                 {
730
731                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
732                                   AFS_TRACE_LEVEL_VERBOSE,
733                                   "AFSCommonCreate Object name collision on create Status %08lX\n",
734                                   ntStatus);
735                 }
736
737                 try_return( ntStatus = STATUS_OBJECT_NAME_COLLISION);
738             }
739
740             //
741             // OK, go and create the node
742             //
743
744             ntStatus = AFSProcessCreate( Irp,
745                                          &stAuthGroup,
746                                          pVolumeCB,
747                                          pParentDirectoryCB,
748                                          &uniFileName,
749                                          &uniComponentName,
750                                          &uniRootFileName,
751                                          &pFcb,
752                                          &pCcb);
753
754             if( !NT_SUCCESS( ntStatus))
755             {
756
757                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
758                               AFS_TRACE_LEVEL_ERROR,
759                               "AFSCommonCreate Failed to create of %wZ in directory %wZ Status %08lX\n",
760                               &uniComponentName,
761                               &pParentDirectoryCB->NameInformation.FileName,
762                               ntStatus);
763             }
764
765             try_return( ntStatus);
766         }
767
768         //
769         // We should not have an extra component except for PIOCtl opens
770         //
771
772         if( uniComponentName.Length > 0)
773         {
774
775             //
776             // If this is an open on "_._AFS_IOCTL_._" then perform handling on it accordingly
777             //
778
779             if( RtlCompareUnicodeString( &AFSPIOCtlName,
780                                          &uniComponentName,
781                                          TRUE) == 0)
782             {
783
784                 //
785                 // AFSOpenIOCtlFcb does not free a DirOpenReferenceCount for
786                 // pParentDirectoryCB.
787                 //
788
789                 ntStatus = AFSOpenIOCtlFcb( Irp,
790                                             &stAuthGroup,
791                                             pParentDirectoryCB,
792                                             &pFcb,
793                                             &pCcb);
794
795                 if( !NT_SUCCESS( ntStatus))
796                 {
797
798                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
799                                   AFS_TRACE_LEVEL_ERROR,
800                                   "AFSCommonCreate Failed to IOCtl open on %wZ Status %08lX\n",
801                                   &uniComponentName,
802                                   ntStatus);
803                 }
804             }
805             else
806             {
807
808                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
809                               AFS_TRACE_LEVEL_VERBOSE,
810                               "AFSCommonCreate (%p) File %wZ name not found\n",
811                               Irp,
812                               &uniFileName);
813
814                 ntStatus = STATUS_OBJECT_NAME_NOT_FOUND;
815             }
816
817             try_return( ntStatus);
818         }
819
820         //
821         // For root opens the parent will be NULL
822         //
823
824         if( pParentDirectoryCB == NULL)
825         {
826
827             //
828             // Check for the delete on close flag for the root
829             //
830
831             if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE ))
832             {
833
834                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
835                               AFS_TRACE_LEVEL_ERROR,
836                               "AFSCommonCreate (%p) Attempt to open root as delete on close\n",
837                               Irp);
838
839                 try_return( ntStatus = STATUS_CANNOT_DELETE);
840             }
841
842             //
843             // If this is the target directory, then bail
844             //
845
846             if( bOpenTargetDirectory)
847             {
848
849                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
850                               AFS_TRACE_LEVEL_ERROR,
851                               "AFSCommonCreate (%p) Attempt to open root as target directory\n",
852                               Irp);
853
854                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
855             }
856
857             //
858             // Go and open the root of the volume
859             //
860
861             ntStatus = AFSOpenRoot( Irp,
862                                     pVolumeCB,
863                                     &stAuthGroup,
864                                     &pFcb,
865                                     &pCcb);
866
867             if( !NT_SUCCESS( ntStatus))
868             {
869
870                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
871                               AFS_TRACE_LEVEL_ERROR,
872                               "AFSCommonCreate Failed to open volume root %08lX-%08lX Status %08lX\n",
873                               pVolumeCB->ObjectInformation.FileId.Cell,
874                               pVolumeCB->ObjectInformation.FileId.Volume,
875                               ntStatus);
876             }
877
878             try_return( ntStatus);
879         }
880
881         //
882         // At this point if we have no pDirectoryCB it was not found.
883         //
884
885         if( pDirectoryCB == NULL)
886         {
887
888             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
889                           AFS_TRACE_LEVEL_ERROR,
890                           "AFSCommonCreate Failing access to %wZ\n",
891                           &uniFileName);
892
893             try_return( ntStatus = STATUS_OBJECT_NAME_NOT_FOUND);
894         }
895
896         if( ulCreateDisposition == FILE_OVERWRITE ||
897             ulCreateDisposition == FILE_SUPERSEDE ||
898             ulCreateDisposition == FILE_OVERWRITE_IF)
899         {
900
901             //
902             // Go process a file for overwrite or supersede.
903             //
904
905             ntStatus = AFSProcessOverwriteSupersede( DeviceObject,
906                                                      Irp,
907                                                      pVolumeCB,
908                                                      &stAuthGroup,
909                                                      pParentDirectoryCB,
910                                                      pDirectoryCB,
911                                                      &pFcb,
912                                                      &pCcb);
913
914             if( !NT_SUCCESS( ntStatus))
915             {
916
917                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
918                               AFS_TRACE_LEVEL_ERROR,
919                               "AFSCommonCreate Failed overwrite/supersede on %wZ Status %08lX\n",
920                               &pDirectoryCB->NameInformation.FileName,
921                               ntStatus);
922             }
923
924             try_return( ntStatus);
925         }
926
927         //
928         // Trying to open the file
929         //
930
931         ntStatus = AFSProcessOpen( Irp,
932                                    &stAuthGroup,
933                                    pVolumeCB,
934                                    pParentDirectoryCB,
935                                    pDirectoryCB,
936                                    &pFcb,
937                                    &pCcb);
938
939         if( !NT_SUCCESS( ntStatus))
940         {
941
942             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
943                           AFS_TRACE_LEVEL_ERROR,
944                           "AFSCommonCreate Failed open on %wZ Status %08lX\n",
945                           &pDirectoryCB->NameInformation.FileName,
946                           ntStatus);
947         }
948
949 try_exit:
950
951         if( NT_SUCCESS( ntStatus) &&
952             ntStatus != STATUS_REPARSE)
953         {
954
955             if( pCcb != NULL)
956             {
957
958                 AFSAcquireExcl( &pCcb->NPCcb->CcbLock,
959                                 TRUE);
960
961                 RtlCopyMemory( &pCcb->AuthGroup,
962                                &stAuthGroup,
963                                sizeof( GUID));
964
965                 //
966                 // If we have a substitute name, then use it
967                 //
968
969                 if( uniSubstitutedPathName.Buffer != NULL)
970                 {
971
972                     pCcb->FullFileName = uniSubstitutedPathName;
973
974                     SetFlag( pCcb->Flags, CCB_FLAG_FREE_FULL_PATHNAME);
975
976                     ClearFlag( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER);
977                 }
978                 else
979                 {
980
981                     pCcb->FullFileName = uniRootFileName;
982
983                     if( BooleanFlagOn( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER))
984                     {
985
986                         SetFlag( pCcb->Flags, CCB_FLAG_FREE_FULL_PATHNAME);
987
988                         ClearFlag( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER);
989                     }
990                 }
991
992                 if( bOpenedReparsePoint)
993                 {
994                     SetFlag( pCcb->Flags, CCB_FLAG_MASK_OPENED_REPARSE_POINT);
995                 }
996
997                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
998                               AFS_TRACE_LEVEL_VERBOSE,
999                               "AFSCommonCreate Count on %wZ DE %p Ccb %p Cnt %d\n",
1000                               &pCcb->DirectoryCB->NameInformation.FileName,
1001                               pCcb->DirectoryCB,
1002                               pCcb,
1003                               lCount = pCcb->DirectoryCB->DirOpenReferenceCount);
1004
1005                 ASSERT( lCount >= 0);
1006
1007                 pCcb->CurrentDirIndex = 0;
1008
1009                 if( !BooleanFlagOn( ulParseFlags, AFS_PARSE_FLAG_ROOT_ACCESS))
1010                 {
1011
1012                     SetFlag( pCcb->Flags, CCB_FLAG_RETURN_RELATIVE_ENTRIES);
1013                 }
1014
1015                 //
1016                 // Save off the name array for this instance
1017                 //
1018
1019                 pCcb->NameArray = pNameArray;
1020
1021                 pNameArray = NULL;
1022
1023                 AFSReleaseResource( &pCcb->NPCcb->CcbLock);
1024             }
1025
1026             //
1027             // If we make it here then init the FO for the request.
1028             //
1029
1030             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1031                           AFS_TRACE_LEVEL_VERBOSE_2,
1032                           "AFSCommonCreate (%p) FileObject %p FsContext %08lX FsContext2 %08lX\n",
1033                           Irp,
1034                           pFileObject,
1035                           pFcb,
1036                           pCcb);
1037
1038             pFileObject->FsContext = (void *)pFcb;
1039
1040             pFileObject->FsContext2 = (void *)pCcb;
1041
1042             if( pFcb != NULL)
1043             {
1044
1045                 ASSERT( pFcb->OpenHandleCount > 0);
1046
1047                 ClearFlag( pFcb->Flags, AFS_FCB_FILE_CLOSED);
1048
1049                 //
1050                 // For files perform additional processing
1051                 //
1052
1053                 switch( pFcb->Header.NodeTypeCode)
1054                 {
1055
1056                     case AFS_FILE_FCB:
1057                     case AFS_IOCTL_FCB:
1058                     {
1059
1060                         pFileObject->SectionObjectPointer = &pFcb->NPFcb->SectionObjectPointers;
1061                     }
1062                 }
1063
1064                 //
1065                 // If the user did not request nobuffering then mark the FO as cacheable
1066                 //
1067
1068                 if( bNoIntermediateBuffering)
1069                 {
1070
1071                     pFileObject->Flags |= FO_NO_INTERMEDIATE_BUFFERING;
1072                 }
1073                 else
1074                 {
1075
1076                     pFileObject->Flags |= FO_CACHE_SUPPORTED;
1077                 }
1078
1079                 //
1080                 // If the file was opened for execution then we need to set the bit in the FO
1081                 //
1082
1083                 if( BooleanFlagOn( *pDesiredAccess,
1084                                    FILE_EXECUTE))
1085                 {
1086
1087                     SetFlag( pFileObject->Flags, FO_FILE_FAST_IO_READ);
1088                 }
1089
1090                 //
1091                 // Update the last access time
1092                 //
1093
1094                 KeQuerySystemTime( &pFcb->ObjectInformation->LastAccessTime);
1095
1096                 if( pCcb != NULL)
1097                 {
1098                     AFSInsertCcb( pFcb,
1099                                   pCcb);
1100                 }
1101             }
1102             else
1103             {
1104
1105                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1106                               AFS_TRACE_LEVEL_ERROR,
1107                               "AFSCommonCreate (%p) Returning with NULL Fcb FileObject %p FsContext %08lX FsContext2 %08lX\n",
1108                               Irp,
1109                               pFileObject,
1110                               pFcb,
1111                               pCcb);
1112             }
1113         }
1114         else
1115         {
1116             if( NT_SUCCESS( ntStatus) &&
1117                 ntStatus == STATUS_REPARSE)
1118             {
1119
1120                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1121                               AFS_TRACE_LEVEL_ERROR,
1122                               "AFSCommonCreate (%p) STATUS_REPARSE FileObject %p FsContext %08lX FsContext2 %08lX\n",
1123                               Irp,
1124                               pFileObject,
1125                               pFcb,
1126                               pCcb);
1127             }
1128
1129             //
1130             // Free up the sub name if we have one
1131             //
1132
1133             if( uniSubstitutedPathName.Buffer != NULL)
1134             {
1135
1136                 AFSExFreePoolWithTag( uniSubstitutedPathName.Buffer, 0);
1137
1138                 ClearFlag( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER);
1139             }
1140         }
1141
1142         //
1143         // Free up the name array ...
1144         //
1145
1146         if( pNameArray != NULL)
1147         {
1148
1149             AFSFreeNameArray( pNameArray);
1150         }
1151
1152         if( BooleanFlagOn( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER))
1153         {
1154
1155             AFSExFreePoolWithTag( uniRootFileName.Buffer, 0);
1156         }
1157
1158         if( bReleaseVolume)
1159         {
1160
1161             lCount = InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
1162
1163             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1164                           AFS_TRACE_LEVEL_VERBOSE,
1165                           "AFSCommonCreate Decrement count on Volume %08lX Cnt %d\n",
1166                           pVolumeCB,
1167                           lCount);
1168         }
1169
1170         if ( bReleaseDir)
1171         {
1172
1173             //
1174             // Release the reference from AFSLocateNameEntry
1175             //
1176
1177             lCount = InterlockedDecrement( &pDirectoryCB->DirOpenReferenceCount);
1178
1179             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1180                           AFS_TRACE_LEVEL_VERBOSE,
1181                           "AFSCommonCreate Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
1182                           &pDirectoryCB->NameInformation.FileName,
1183                           pDirectoryCB,
1184                           pCcb,
1185                           lCount);
1186
1187             ASSERT( lCount >= 0);
1188         }
1189
1190         if ( bReleaseParentDir)
1191         {
1192
1193             //
1194             // Release the reference from AFSLocateNameEntry
1195             //
1196
1197             lCount = InterlockedDecrement( &pParentDirectoryCB->DirOpenReferenceCount);
1198
1199             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1200                           AFS_TRACE_LEVEL_VERBOSE,
1201                           "AFSCommonCreate Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
1202                           &pParentDirectoryCB->NameInformation.FileName,
1203                           pParentDirectoryCB,
1204                           pCcb,
1205                           lCount);
1206
1207             ASSERT( lCount >= 0);
1208         }
1209
1210         //
1211         // Setup the Irp for completion, the Information has been set previously
1212         //
1213
1214         Irp->IoStatus.Status = ntStatus;
1215     }
1216
1217     return ntStatus;
1218 }
1219
1220 NTSTATUS
1221 AFSOpenAFSRoot( IN PIRP Irp,
1222                 IN AFSFcb **Fcb,
1223                 IN AFSCcb **Ccb)
1224 {
1225
1226     NTSTATUS ntStatus = STATUS_SUCCESS;
1227     LONG lCount;
1228
1229     __Enter
1230     {
1231
1232         //
1233         // Initialize the Ccb for the file.
1234         //
1235
1236         ntStatus = AFSInitCcb( Ccb,
1237                                AFSGlobalRoot->DirectoryCB,
1238                                0,
1239                                0);
1240
1241         if( !NT_SUCCESS( ntStatus))
1242         {
1243
1244             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1245                           AFS_TRACE_LEVEL_ERROR,
1246                           "AFSOpenAFSRoot (%p) Failed to allocate Ccb\n",
1247                           Irp);
1248
1249             try_return( ntStatus);
1250         }
1251
1252         //
1253         // Increment the open count on this Fcb
1254         //
1255
1256         lCount = InterlockedIncrement( &AFSGlobalRoot->RootFcb->OpenReferenceCount);
1257
1258         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1259                       AFS_TRACE_LEVEL_VERBOSE,
1260                       "AFSOpenAFSRoot Increment count on Fcb %p Cnt %d\n",
1261                       AFSGlobalRoot->RootFcb,
1262                       lCount);
1263
1264         lCount = InterlockedIncrement( &AFSGlobalRoot->RootFcb->OpenHandleCount);
1265
1266         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1267                       AFS_TRACE_LEVEL_VERBOSE,
1268                       "AFSOpenAFSRoot Increment handle count on Fcb %p Cnt %d\n",
1269                       AFSGlobalRoot->RootFcb,
1270                       lCount);
1271
1272         *Fcb = AFSGlobalRoot->RootFcb;
1273
1274         //
1275         // Return the open result for this file
1276         //
1277
1278         Irp->IoStatus.Information = FILE_OPENED;
1279
1280 try_exit:
1281
1282         NOTHING;
1283     }
1284
1285     return ntStatus;
1286 }
1287
1288 NTSTATUS
1289 AFSOpenRoot( IN PIRP Irp,
1290              IN AFSVolumeCB *VolumeCB,
1291              IN GUID *AuthGroup,
1292              OUT AFSFcb **RootFcb,
1293              OUT AFSCcb **Ccb)
1294 {
1295
1296     NTSTATUS ntStatus = STATUS_SUCCESS;
1297     PFILE_OBJECT pFileObject = NULL;
1298     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1299     PACCESS_MASK pDesiredAccess = NULL;
1300     USHORT usShareAccess;
1301     ULONG ulOptions;
1302     BOOLEAN bAllocatedCcb = FALSE;
1303     BOOLEAN bReleaseFcb = FALSE;
1304     AFSFileOpenCB   stOpenCB;
1305     AFSFileOpenResultCB stOpenResultCB;
1306     ULONG       ulResultLen = 0;
1307     LONG        lCount;
1308
1309     __Enter
1310     {
1311
1312         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
1313         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
1314         ulOptions = pIrpSp->Parameters.Create.Options;
1315
1316         pFileObject = pIrpSp->FileObject;
1317
1318         if( BooleanFlagOn( ulOptions, FILE_NON_DIRECTORY_FILE))
1319         {
1320
1321             ntStatus = STATUS_FILE_IS_A_DIRECTORY;
1322
1323             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1324                           AFS_TRACE_LEVEL_ERROR,
1325                           "AFSOpenRoot (%p) Attempt to open root as file Status %08lX\n",
1326                           Irp,
1327                           ntStatus);
1328
1329             try_return( ntStatus);
1330         }
1331
1332         //
1333         // Check if we should go and retrieve updated information for the node
1334         //
1335
1336         ntStatus = AFSValidateEntry( VolumeCB->DirectoryCB,
1337                                      AuthGroup,
1338                                      FALSE,
1339                                      TRUE);
1340
1341         if( !NT_SUCCESS( ntStatus))
1342         {
1343
1344             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1345                           AFS_TRACE_LEVEL_ERROR,
1346                           "AFSOpenRoot (%p) Failed to validate root entry Status %08lX\n",
1347                           Irp,
1348                           ntStatus);
1349
1350             try_return( ntStatus);
1351         }
1352
1353         //
1354         // Check with the service that we can open the file
1355         //
1356
1357         RtlZeroMemory( &stOpenCB,
1358                        sizeof( AFSFileOpenCB));
1359
1360         stOpenCB.DesiredAccess = *pDesiredAccess;
1361
1362         stOpenCB.ShareAccess = usShareAccess;
1363
1364         stOpenResultCB.GrantedAccess = 0;
1365
1366         ulResultLen = sizeof( AFSFileOpenResultCB);
1367
1368         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_OPEN_FILE,
1369                                       AFS_REQUEST_FLAG_SYNCHRONOUS | AFS_REQUEST_FLAG_HOLD_FID,
1370                                       AuthGroup,
1371                                       NULL,
1372                                       &VolumeCB->ObjectInformation.FileId,
1373                                       (void *)&stOpenCB,
1374                                       sizeof( AFSFileOpenCB),
1375                                       (void *)&stOpenResultCB,
1376                                       &ulResultLen);
1377
1378         if( !NT_SUCCESS( ntStatus))
1379         {
1380
1381             UNICODE_STRING uniGUID;
1382
1383             uniGUID.Length = 0;
1384             uniGUID.MaximumLength = 0;
1385             uniGUID.Buffer = NULL;
1386
1387             if( AuthGroup != NULL)
1388             {
1389                 RtlStringFromGUID( *AuthGroup,
1390                                    &uniGUID);
1391             }
1392
1393             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1394                           AFS_TRACE_LEVEL_ERROR,
1395                           "AFSOpenRoot (%p) Failed open in service volume %08lX-%08lX AuthGroup %wZ Status %08lX\n",
1396                           Irp,
1397                           VolumeCB->ObjectInformation.FileId.Cell,
1398                           VolumeCB->ObjectInformation.FileId.Volume,
1399                           &uniGUID,
1400                           ntStatus);
1401
1402             if( AuthGroup != NULL)
1403             {
1404                 RtlFreeUnicodeString( &uniGUID);
1405             }
1406
1407             try_return( ntStatus);
1408         }
1409
1410         //
1411         // If the entry is not initialized then do it now
1412         //
1413
1414         if( !BooleanFlagOn( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
1415         {
1416
1417             AFSAcquireExcl( VolumeCB->ObjectInformation.Specific.Directory.DirectoryNodeHdr.TreeLock,
1418                             TRUE);
1419
1420             if( !BooleanFlagOn( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
1421             {
1422
1423                 ntStatus = AFSEnumerateDirectory( AuthGroup,
1424                                                   &VolumeCB->ObjectInformation,
1425                                                   TRUE);
1426
1427                 if( !NT_SUCCESS( ntStatus))
1428                 {
1429
1430                     AFSReleaseResource( VolumeCB->ObjectInformation.Specific.Directory.DirectoryNodeHdr.TreeLock);
1431
1432                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1433                                   AFS_TRACE_LEVEL_ERROR,
1434                                   "AFSOpenRoot (%p) Failed to enumerate directory Status %08lX\n",
1435                                   Irp,
1436                                   ntStatus);
1437
1438                     try_return( ntStatus);
1439                 }
1440
1441                 SetFlag( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1442             }
1443
1444             AFSReleaseResource( VolumeCB->ObjectInformation.Specific.Directory.DirectoryNodeHdr.TreeLock);
1445         }
1446
1447         //
1448         // If the root fcb has been initialized then check access otherwise
1449         // init the volume fcb
1450         //
1451
1452         if( VolumeCB->RootFcb == NULL)
1453         {
1454
1455             ntStatus = AFSInitRootFcb( (ULONGLONG)PsGetCurrentProcessId(),
1456                                        VolumeCB);
1457
1458             if( !NT_SUCCESS( ntStatus))
1459             {
1460
1461                 try_return( ntStatus);
1462             }
1463         }
1464         else
1465         {
1466
1467             AFSAcquireExcl( VolumeCB->RootFcb->Header.Resource,
1468                             TRUE);
1469         }
1470
1471         bReleaseFcb = TRUE;
1472
1473         //
1474         // If there are current opens on the Fcb, check the access.
1475         //
1476
1477         if( VolumeCB->RootFcb->OpenHandleCount > 0)
1478         {
1479
1480             ntStatus = IoCheckShareAccess( *pDesiredAccess,
1481                                            usShareAccess,
1482                                            pFileObject,
1483                                            &VolumeCB->RootFcb->ShareAccess,
1484                                            FALSE);
1485
1486             if( !NT_SUCCESS( ntStatus))
1487             {
1488
1489                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1490                               AFS_TRACE_LEVEL_ERROR,
1491                               "AFSOpenRoot (%p) Access check failure Status %08lX\n",
1492                               Irp,
1493                               ntStatus);
1494
1495                 try_return( ntStatus);
1496             }
1497         }
1498
1499         //
1500         // Initialize the Ccb for the file.
1501         //
1502
1503         ntStatus = AFSInitCcb( Ccb,
1504                                VolumeCB->DirectoryCB,
1505                                *pDesiredAccess,
1506                                0);
1507
1508         if( !NT_SUCCESS( ntStatus))
1509         {
1510
1511             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1512                           AFS_TRACE_LEVEL_ERROR,
1513                           "AFSOpenRoot (%p) Failed to allocate Ccb Status %08lX\n",
1514                           Irp,
1515                           ntStatus);
1516
1517             try_return( ntStatus);
1518         }
1519
1520         bAllocatedCcb = TRUE;
1521
1522         //
1523         // OK, update the share access on the fileobject
1524         //
1525
1526         if( VolumeCB->RootFcb->OpenHandleCount > 0)
1527         {
1528
1529             IoUpdateShareAccess( pFileObject,
1530                                  &VolumeCB->RootFcb->ShareAccess);
1531         }
1532         else
1533         {
1534
1535             //
1536             // Set the access
1537             //
1538
1539             IoSetShareAccess( *pDesiredAccess,
1540                               usShareAccess,
1541                               pFileObject,
1542                               &VolumeCB->RootFcb->ShareAccess);
1543         }
1544
1545         //
1546         // Increment the open count on this Fcb
1547         //
1548
1549         lCount = InterlockedIncrement( &VolumeCB->RootFcb->OpenReferenceCount);
1550
1551         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1552                       AFS_TRACE_LEVEL_VERBOSE,
1553                       "AFSOpenRoot Increment count on Fcb %p Cnt %d\n",
1554                       VolumeCB->RootFcb,
1555                       lCount);
1556
1557         lCount = InterlockedIncrement( &VolumeCB->RootFcb->OpenHandleCount);
1558
1559         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1560                       AFS_TRACE_LEVEL_VERBOSE,
1561                       "AFSOpenRoot Increment handle count on Fcb %p Cnt %d\n",
1562                       VolumeCB->RootFcb,
1563                       lCount);
1564
1565         //
1566         // Indicate the object is held
1567         //
1568
1569         SetFlag( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_HELD_IN_SERVICE);
1570
1571         //
1572         // Return the open result for this file
1573         //
1574
1575         Irp->IoStatus.Information = FILE_OPENED;
1576
1577         *RootFcb = VolumeCB->RootFcb;
1578
1579 try_exit:
1580
1581         if( bReleaseFcb)
1582         {
1583
1584             AFSReleaseResource( VolumeCB->RootFcb->Header.Resource);
1585         }
1586
1587         if( !NT_SUCCESS( ntStatus))
1588         {
1589
1590             if( bAllocatedCcb)
1591             {
1592
1593                 AFSRemoveCcb( NULL,
1594                               *Ccb);
1595
1596                 *Ccb = NULL;
1597             }
1598
1599             Irp->IoStatus.Information = 0;
1600         }
1601     }
1602
1603     return ntStatus;
1604 }
1605
1606 NTSTATUS
1607 AFSProcessCreate( IN PIRP               Irp,
1608                   IN GUID              *AuthGroup,
1609                   IN AFSVolumeCB       *VolumeCB,
1610                   IN AFSDirectoryCB    *ParentDirCB,
1611                   IN PUNICODE_STRING    FileName,
1612                   IN PUNICODE_STRING    ComponentName,
1613                   IN PUNICODE_STRING    FullFileName,
1614                   OUT AFSFcb          **Fcb,
1615                   OUT AFSCcb          **Ccb)
1616 {
1617
1618     NTSTATUS ntStatus = STATUS_SUCCESS;
1619     PFILE_OBJECT pFileObject = NULL;
1620     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1621     ULONG ulOptions = 0;
1622     ULONG ulAttributes = 0;
1623     BOOLEAN bFileCreated = FALSE, bReleaseFcb = FALSE, bAllocatedCcb = FALSE;
1624     PACCESS_MASK pDesiredAccess = NULL;
1625     USHORT usShareAccess;
1626     AFSDirectoryCB *pDirEntry = NULL;
1627     AFSObjectInfoCB *pParentObjectInfo = NULL;
1628     AFSObjectInfoCB *pObjectInfo = NULL;
1629     LONG lCount;
1630
1631     __Enter
1632     {
1633
1634         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
1635         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
1636
1637         pFileObject = pIrpSp->FileObject;
1638
1639         //
1640         // Extract out the options
1641         //
1642
1643         ulOptions = pIrpSp->Parameters.Create.Options;
1644
1645         //
1646         // We pass all attributes they want to apply to the file to the create
1647         //
1648
1649         ulAttributes = pIrpSp->Parameters.Create.FileAttributes;
1650
1651         //
1652         // If this is a directory create then set the attribute correctly
1653         //
1654
1655         if( ulOptions & FILE_DIRECTORY_FILE)
1656         {
1657
1658             ulAttributes |= FILE_ATTRIBUTE_DIRECTORY;
1659         }
1660
1661         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1662                       AFS_TRACE_LEVEL_VERBOSE,
1663                       "AFSProcessCreate (%p) Creating file %wZ Attributes %08lX\n",
1664                       Irp,
1665                       FullFileName,
1666                       ulAttributes);
1667
1668         if( BooleanFlagOn( VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
1669         {
1670
1671             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1672                           AFS_TRACE_LEVEL_ERROR,
1673                           "AFSProcessCreate Request failed due to read only volume %wZ\n",
1674                           FullFileName);
1675
1676             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
1677         }
1678
1679         pParentObjectInfo = ParentDirCB->ObjectInformation;
1680
1681         //
1682         // Allocate and insert the direntry into the parent node
1683         //
1684
1685         ntStatus = AFSCreateDirEntry( AuthGroup,
1686                                       pParentObjectInfo,
1687                                       ParentDirCB,
1688                                       FileName,
1689                                       ComponentName,
1690                                       ulAttributes,
1691                                       &pDirEntry);
1692
1693         if( !NT_SUCCESS( ntStatus))
1694         {
1695
1696             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1697                           AFS_TRACE_LEVEL_ERROR,
1698                           "AFSProcessCreate (%p) Failed to create directory entry %wZ Status %08lX\n",
1699                           Irp,
1700                           FullFileName,
1701                           ntStatus);
1702
1703             try_return( ntStatus);
1704         }
1705
1706         bFileCreated = TRUE;
1707
1708         pObjectInfo = pDirEntry->ObjectInformation;
1709
1710         if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED) ||
1711             pObjectInfo->FileType == AFS_FILE_TYPE_UNKNOWN)
1712         {
1713
1714             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1715                           AFS_TRACE_LEVEL_VERBOSE,
1716                           "AFSProcessCreate (%p) Evaluating object %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1717                           Irp,
1718                           &pDirEntry->NameInformation.FileName,
1719                           pObjectInfo->FileId.Cell,
1720                           pObjectInfo->FileId.Volume,
1721                           pObjectInfo->FileId.Vnode,
1722                           pObjectInfo->FileId.Unique);
1723
1724             ntStatus = AFSEvaluateNode( AuthGroup,
1725                                         pDirEntry);
1726
1727             if( !NT_SUCCESS( ntStatus))
1728             {
1729
1730                 if ( ntStatus == STATUS_NOT_A_DIRECTORY)
1731                 {
1732
1733                     if ( pParentObjectInfo == pObjectInfo->ParentObjectInformation)
1734                     {
1735
1736                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1737                                       AFS_TRACE_LEVEL_ERROR,
1738                                       "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1739                                       Irp,
1740                                       &pDirEntry->NameInformation.FileName,
1741                                       pObjectInfo->FileId.Cell,
1742                                       pObjectInfo->FileId.Volume,
1743                                       pObjectInfo->FileId.Vnode,
1744                                       pObjectInfo->FileId.Unique,
1745                                       pParentObjectInfo->FileId.Cell,
1746                                       pParentObjectInfo->FileId.Volume,
1747                                       pParentObjectInfo->FileId.Vnode,
1748                                       pParentObjectInfo->FileId.Unique,
1749                                       ntStatus);
1750                     }
1751                     else if ( pObjectInfo->ParentObjectInformation == NULL)
1752                     {
1753
1754                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1755                                       AFS_TRACE_LEVEL_ERROR,
1756                                       "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX != NULL Status %08lX\n",
1757                                       Irp,
1758                                       &pDirEntry->NameInformation.FileName,
1759                                       pObjectInfo->FileId.Cell,
1760                                       pObjectInfo->FileId.Volume,
1761                                       pObjectInfo->FileId.Vnode,
1762                                       pObjectInfo->FileId.Unique,
1763                                       pParentObjectInfo->FileId.Cell,
1764                                       pParentObjectInfo->FileId.Volume,
1765                                       pParentObjectInfo->FileId.Vnode,
1766                                       pParentObjectInfo->FileId.Unique,
1767                                       ntStatus);
1768                     }
1769                     else
1770                     {
1771
1772                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1773                                       AFS_TRACE_LEVEL_ERROR,
1774                                       "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX != %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1775                                       Irp,
1776                                       &pDirEntry->NameInformation.FileName,
1777                                       pObjectInfo->FileId.Cell,
1778                                       pObjectInfo->FileId.Volume,
1779                                       pObjectInfo->FileId.Vnode,
1780                                       pObjectInfo->FileId.Unique,
1781                                       pParentObjectInfo->FileId.Cell,
1782                                       pParentObjectInfo->FileId.Volume,
1783                                       pParentObjectInfo->FileId.Vnode,
1784                                       pParentObjectInfo->FileId.Unique,
1785                                       pObjectInfo->ParentObjectInformation->FileId.Cell,
1786                                       pObjectInfo->ParentObjectInformation->FileId.Volume,
1787                                       pObjectInfo->ParentObjectInformation->FileId.Vnode,
1788                                       pObjectInfo->ParentObjectInformation->FileId.Unique,
1789                                       ntStatus);
1790                     }
1791                 }
1792                 else
1793                 {
1794
1795                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1796                                   AFS_TRACE_LEVEL_ERROR,
1797                                   "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1798                                   Irp,
1799                                   &pDirEntry->NameInformation.FileName,
1800                                   pObjectInfo->FileId.Cell,
1801                                   pObjectInfo->FileId.Volume,
1802                                   pObjectInfo->FileId.Vnode,
1803                                   pObjectInfo->FileId.Unique,
1804                                   ntStatus);
1805                 }
1806
1807                 try_return( ntStatus);
1808             }
1809
1810             ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED);
1811         }
1812
1813         //
1814         // We may have raced and the Fcb is already created
1815         //
1816
1817         //
1818         // Allocate and initialize the Fcb for the file.
1819         //
1820
1821         ntStatus = AFSInitFcb( pDirEntry);
1822
1823         *Fcb = pObjectInfo->Fcb;
1824
1825         if( !NT_SUCCESS( ntStatus))
1826         {
1827
1828             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1829                           AFS_TRACE_LEVEL_ERROR,
1830                           "AFSProcessCreate (%p) Failed to initialize fcb %wZ Status %08lX\n",
1831                           Irp,
1832                           FullFileName,
1833                           ntStatus);
1834
1835             try_return( ntStatus);
1836         }
1837
1838         ntStatus = STATUS_SUCCESS;
1839
1840         //
1841         // Increment the open count on this Fcb
1842         //
1843
1844         lCount = InterlockedIncrement( &(*Fcb)->OpenReferenceCount);
1845
1846         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1847                       AFS_TRACE_LEVEL_VERBOSE,
1848                       "AFSProcessCreate Increment count on Fcb %p Cnt %d\n",
1849                       *Fcb,
1850                       lCount);
1851
1852         bReleaseFcb = TRUE;
1853
1854         //
1855         // Initialize the Ccb for the file.
1856         //
1857
1858         ntStatus = AFSInitCcb( Ccb,
1859                                pDirEntry,
1860                                *pDesiredAccess,
1861                                0);
1862
1863         if( !NT_SUCCESS( ntStatus))
1864         {
1865
1866             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1867                           AFS_TRACE_LEVEL_ERROR,
1868                           "AFSProcessCreate (%p) Failed to initialize ccb %wZ Status %08lX\n",
1869                           Irp,
1870                           FullFileName,
1871                           ntStatus);
1872
1873             try_return( ntStatus);
1874         }
1875
1876         bAllocatedCcb = TRUE;
1877
1878         //
1879         // If this is a file, update the headers filesizes.
1880         //
1881
1882         if( (*Fcb)->Header.NodeTypeCode == AFS_FILE_FCB)
1883         {
1884
1885             //
1886             // Update the sizes with the information passed in
1887             //
1888
1889             (*Fcb)->Header.AllocationSize.QuadPart  = pObjectInfo->AllocationSize.QuadPart;
1890             (*Fcb)->Header.FileSize.QuadPart        = pObjectInfo->EndOfFile.QuadPart;
1891             (*Fcb)->Header.ValidDataLength.QuadPart = pObjectInfo->EndOfFile.QuadPart;
1892
1893             //
1894             // Notify the system of the addition
1895             //
1896
1897             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1898                                             *Ccb,
1899                                             (ULONG)FILE_NOTIFY_CHANGE_FILE_NAME,
1900                                             (ULONG)FILE_ACTION_ADDED);
1901
1902             (*Fcb)->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
1903         }
1904         else if( (*Fcb)->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
1905         {
1906
1907             //
1908             // This is a new directory node so indicate it has been enumerated
1909             //
1910
1911             SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1912
1913             //
1914             // And the parent directory entry
1915             //
1916
1917             KeQuerySystemTime( &pParentObjectInfo->ChangeTime);
1918
1919             //
1920             // Notify the system of the addition
1921             //
1922
1923             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1924                                             *Ccb,
1925                                             (ULONG)FILE_NOTIFY_CHANGE_DIR_NAME,
1926                                             (ULONG)FILE_ACTION_ADDED);
1927         }
1928         else if( (*Fcb)->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
1929                  (*Fcb)->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
1930                  (*Fcb)->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
1931                  (*Fcb)->Header.NodeTypeCode == AFS_INVALID_FCB)
1932         {
1933
1934             //
1935             // And the parent directory entry
1936             //
1937
1938             KeQuerySystemTime( &pParentObjectInfo->ChangeTime);
1939
1940             //
1941             // Notify the system of the addition
1942             //
1943
1944             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1945                                             *Ccb,
1946                                             (ULONG)FILE_NOTIFY_CHANGE_DIR_NAME,
1947                                             (ULONG)FILE_ACTION_ADDED);
1948         }
1949
1950         //
1951         // Save off the access for the open
1952         //
1953
1954         IoSetShareAccess( *pDesiredAccess,
1955                           usShareAccess,
1956                           pFileObject,
1957                           &(*Fcb)->ShareAccess);
1958
1959         lCount = InterlockedIncrement( &(*Fcb)->OpenHandleCount);
1960
1961         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1962                       AFS_TRACE_LEVEL_VERBOSE,
1963                       "AFSProcessCreate Increment handle count on Fcb %p Cnt %d\n",
1964                       (*Fcb),
1965                       lCount);
1966
1967         //
1968         // Increment the open reference and handle on the parent node
1969         //
1970
1971         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
1972
1973         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1974                       AFS_TRACE_LEVEL_VERBOSE,
1975                       "AFSProcessCreate Increment child open handle count on Parent object %p Cnt %d\n",
1976                       pObjectInfo->ParentObjectInformation,
1977                       lCount);
1978
1979         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
1980
1981         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1982                       AFS_TRACE_LEVEL_VERBOSE,
1983                       "AFSProcessCreate Increment child open ref count on Parent object %p Cnt %d\n",
1984                       pObjectInfo->ParentObjectInformation,
1985                       lCount);
1986
1987         if( ulOptions & FILE_DELETE_ON_CLOSE)
1988         {
1989
1990             //
1991             // Mark it for delete on close
1992             //
1993
1994             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1995                           AFS_TRACE_LEVEL_VERBOSE,
1996                           "AFSProcessCreate (%p) Setting PENDING_DELETE flag in DirEntry %p Name %wZ\n",
1997                           Irp,
1998                           pDirEntry,
1999                           FullFileName);
2000
2001             SetFlag( pDirEntry->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2002         }
2003
2004         //
2005         // Indicate the object is locked in the service
2006         //
2007
2008         SetFlag( pObjectInfo->Flags, AFS_OBJECT_HELD_IN_SERVICE);
2009
2010         //
2011         // Return the open result for this file
2012         //
2013
2014         Irp->IoStatus.Information = FILE_CREATED;
2015
2016 try_exit:
2017
2018         //
2019         // If we created the Fcb we need to release the resources
2020         //
2021
2022         if( bReleaseFcb)
2023         {
2024
2025             if( !NT_SUCCESS( ntStatus))
2026             {
2027                 //
2028                 // Decrement the open count on this Fcb
2029                 //
2030
2031                 lCount = InterlockedDecrement( &(*Fcb)->OpenReferenceCount);
2032
2033                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2034                               AFS_TRACE_LEVEL_VERBOSE,
2035                               "AFSProcessCreate Decrement count on Fcb %p Cnt %d\n",
2036                               *Fcb,
2037                               lCount);
2038             }
2039
2040             AFSReleaseResource( &(*Fcb)->NPFcb->Resource);
2041         }
2042
2043         if( !NT_SUCCESS( ntStatus))
2044         {
2045
2046             if( bFileCreated)
2047             {
2048
2049                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2050                               AFS_TRACE_LEVEL_VERBOSE,
2051                               "AFSProcessCreate Create failed, removing DE %p from parent object %p Status %08lX\n",
2052                               pDirEntry,
2053                               pParentObjectInfo,
2054                               ntStatus);
2055
2056                 //
2057                 // Remove the dir entry from the parent
2058                 //
2059
2060                 AFSAcquireExcl( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
2061                                 TRUE);
2062
2063                 SetFlag( pDirEntry->Flags, AFS_DIR_ENTRY_DELETED);
2064
2065                 AFSNotifyDelete( pDirEntry,
2066                                  AuthGroup,
2067                                  FALSE);
2068
2069                 //
2070                 // Decrement the reference added during initialization of the DE
2071                 //
2072
2073                 lCount = InterlockedDecrement( &pDirEntry->DirOpenReferenceCount);
2074
2075                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2076                               AFS_TRACE_LEVEL_VERBOSE,
2077                               "AFSProcessCreate Decrement count on %wZ DE %p Cnt %d\n",
2078                               &pDirEntry->NameInformation.FileName,
2079                               pDirEntry,
2080                               lCount);
2081
2082                 ASSERT( lCount >= 0);
2083
2084                 //
2085                 // Pull the directory entry from the parent
2086                 //
2087
2088                 AFSRemoveDirNodeFromParent( pParentObjectInfo,
2089                                             pDirEntry,
2090                                             FALSE); // Leave it in the enum list so the worker cleans it up
2091
2092                 //
2093                 // Tag the parent as needing verification
2094                 //
2095
2096                 SetFlag( pParentObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2097
2098                 pParentObjectInfo->DataVersion.QuadPart = (ULONGLONG)-1;
2099
2100                 AFSReleaseResource( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
2101             }
2102
2103             if( bAllocatedCcb)
2104             {
2105
2106                 AFSRemoveCcb( NULL,
2107                               *Ccb);
2108             }
2109
2110             //
2111             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2112             //
2113
2114             *Fcb = NULL;
2115
2116             *Ccb = NULL;
2117         }
2118     }
2119
2120     return ntStatus;
2121 }
2122
2123 NTSTATUS
2124 AFSOpenTargetDirectory( IN PIRP Irp,
2125                         IN AFSVolumeCB *VolumeCB,
2126                         IN AFSDirectoryCB *ParentDirectoryCB,
2127                         IN AFSDirectoryCB *TargetDirectoryCB,
2128                         IN UNICODE_STRING *TargetName,
2129                         OUT AFSFcb **Fcb,
2130                         OUT AFSCcb **Ccb)
2131 {
2132     UNREFERENCED_PARAMETER(VolumeCB);
2133     NTSTATUS ntStatus = STATUS_SUCCESS;
2134     PFILE_OBJECT pFileObject = NULL;
2135     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2136     PACCESS_MASK pDesiredAccess = NULL;
2137     USHORT usShareAccess;
2138     BOOLEAN bAllocatedCcb = FALSE;
2139     BOOLEAN bReleaseFcb = FALSE;
2140     AFSObjectInfoCB *pParentObject = NULL;
2141     UNICODE_STRING uniTargetName;
2142     LONG lCount;
2143
2144     __Enter
2145     {
2146
2147         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2148         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2149
2150         pFileObject = pIrpSp->FileObject;
2151
2152         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2153                       AFS_TRACE_LEVEL_VERBOSE,
2154                       "AFSOpenTargetDirectory (%p) Processing file %wZ\n",
2155                       Irp,
2156                       TargetName);
2157
2158         pParentObject = ParentDirectoryCB->ObjectInformation;
2159
2160         if( pParentObject->FileType != AFS_FILE_TYPE_DIRECTORY)
2161         {
2162
2163             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2164         }
2165
2166         //
2167         // Make sure we have an Fcb for the access
2168
2169         //
2170         // Allocate and initialize the Fcb for the file.
2171         //
2172
2173         ntStatus = AFSInitFcb( ParentDirectoryCB);
2174
2175         *Fcb = pParentObject->Fcb;
2176
2177         if( !NT_SUCCESS( ntStatus))
2178         {
2179
2180             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2181                           AFS_TRACE_LEVEL_ERROR,
2182                           "AFSOpenTargetDirectory (%p) Failed to initialize fcb %wZ Status %08lX\n",
2183                           Irp,
2184                           &ParentDirectoryCB->NameInformation.FileName,
2185                           ntStatus);
2186
2187             try_return( ntStatus);
2188         }
2189
2190         ntStatus = STATUS_SUCCESS;
2191
2192         //
2193         // Increment the open count on this Fcb
2194         //
2195
2196         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenReferenceCount);
2197
2198         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2199                       AFS_TRACE_LEVEL_VERBOSE,
2200                       "AFSOpenTargetDirectory Increment count on Fcb %p Cnt %d\n",
2201                       pParentObject->Fcb,
2202                       lCount);
2203
2204         bReleaseFcb = TRUE;
2205
2206         //
2207         // If there are current opens on the Fcb, check the access.
2208         //
2209
2210         if( pParentObject->Fcb->OpenHandleCount > 0)
2211         {
2212
2213             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2214                                            usShareAccess,
2215                                            pFileObject,
2216                                            &pParentObject->Fcb->ShareAccess,
2217                                            FALSE);
2218
2219             if( !NT_SUCCESS( ntStatus))
2220             {
2221
2222                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2223                               AFS_TRACE_LEVEL_ERROR,
2224                               "AFSOpenTargetDirectory (%p) Access check failure %wZ Status %08lX\n",
2225                               Irp,
2226                               &ParentDirectoryCB->NameInformation.FileName,
2227                               ntStatus);
2228
2229                 try_return( ntStatus);
2230             }
2231         }
2232
2233         //
2234         // Initialize the Ccb for the file.
2235         //
2236
2237         ntStatus = AFSInitCcb( Ccb,
2238                                ParentDirectoryCB,
2239                                *pDesiredAccess,
2240                                0);
2241
2242         if( !NT_SUCCESS( ntStatus))
2243         {
2244
2245             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2246                           AFS_TRACE_LEVEL_ERROR,
2247                           "AFSOpenTargetDirectory (%p) Failed to initialize ccb %wZ Status %08lX\n",
2248                           Irp,
2249                           &ParentDirectoryCB->NameInformation.FileName,
2250                           ntStatus);
2251
2252             try_return( ntStatus);
2253         }
2254
2255         bAllocatedCcb = TRUE;
2256
2257         if( TargetDirectoryCB != NULL &&
2258             FsRtlAreNamesEqual( &TargetDirectoryCB->NameInformation.FileName,
2259                                 TargetName,
2260                                 FALSE,
2261                                 NULL))
2262         {
2263
2264             Irp->IoStatus.Information = FILE_EXISTS;
2265
2266             uniTargetName = TargetDirectoryCB->NameInformation.FileName;
2267         }
2268         else
2269         {
2270
2271             Irp->IoStatus.Information = FILE_DOES_NOT_EXIST;
2272
2273             uniTargetName = *TargetName;
2274         }
2275
2276         //
2277         // Update the filename in the fileobject for rename processing
2278         //
2279
2280         RtlCopyMemory( pFileObject->FileName.Buffer,
2281                        uniTargetName.Buffer,
2282                        uniTargetName.Length);
2283
2284         pFileObject->FileName.Length = uniTargetName.Length;
2285
2286         //
2287         // OK, update the share access on the fileobject
2288         //
2289
2290         if( pParentObject->Fcb->OpenHandleCount > 0)
2291         {
2292
2293             IoUpdateShareAccess( pFileObject,
2294                                  &pParentObject->Fcb->ShareAccess);
2295         }
2296         else
2297         {
2298
2299             //
2300             // Set the access
2301             //
2302
2303             IoSetShareAccess( *pDesiredAccess,
2304                               usShareAccess,
2305                               pFileObject,
2306                               &pParentObject->Fcb->ShareAccess);
2307         }
2308
2309         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenHandleCount);
2310
2311         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2312                       AFS_TRACE_LEVEL_VERBOSE,
2313                       "AFSOpenTargetDirectory Increment handle count on Fcb %p Cnt %d\n",
2314                       pParentObject->Fcb,
2315                       lCount);
2316
2317         //
2318         // Increment the open reference and handle on the parent node
2319         //
2320
2321         if( pParentObject->ParentObjectInformation != NULL)
2322         {
2323
2324             lCount = InterlockedIncrement( &pParentObject->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
2325
2326             AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2327                           AFS_TRACE_LEVEL_VERBOSE,
2328                           "AFSOpenTargetDirectory Increment child open handle count on Parent object %p Cnt %d\n",
2329                           pParentObject->ParentObjectInformation,
2330                           lCount);
2331
2332             lCount = InterlockedIncrement( &pParentObject->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
2333
2334             AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2335                           AFS_TRACE_LEVEL_VERBOSE,
2336                           "AFSOpenTargetDirectory Increment child open ref count on Parent object %p Cnt %d\n",
2337                           pParentObject->ParentObjectInformation,
2338                           lCount);
2339         }
2340
2341 try_exit:
2342
2343         if( bReleaseFcb)
2344         {
2345
2346             if( !NT_SUCCESS( ntStatus))
2347             {
2348                 //
2349                 // Decrement the open count on this Fcb
2350                 //
2351
2352                 lCount = InterlockedDecrement( &pParentObject->Fcb->OpenReferenceCount);
2353
2354                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2355                               AFS_TRACE_LEVEL_VERBOSE,
2356                               "AFSOpenTargetDirectory Decrement count on Fcb %p Cnt %d\n",
2357                               pParentObject->Fcb,
2358                               lCount);
2359             }
2360
2361             AFSReleaseResource( &pParentObject->Fcb->NPFcb->Resource);
2362         }
2363
2364         if( !NT_SUCCESS( ntStatus))
2365         {
2366
2367             if( bAllocatedCcb)
2368             {
2369
2370                 AFSRemoveCcb( NULL,
2371                               *Ccb);
2372             }
2373
2374             *Ccb = NULL;
2375
2376             //
2377             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2378             //
2379
2380             *Fcb = NULL;
2381         }
2382     }
2383
2384     return ntStatus;
2385 }
2386
2387 NTSTATUS
2388 AFSProcessOpen( IN PIRP Irp,
2389                 IN GUID *AuthGroup,
2390                 IN AFSVolumeCB *VolumeCB,
2391                 IN AFSDirectoryCB *ParentDirCB,
2392                 IN AFSDirectoryCB *DirectoryCB,
2393                 OUT AFSFcb **Fcb,
2394                 OUT AFSCcb **Ccb)
2395 {
2396     UNREFERENCED_PARAMETER(VolumeCB);
2397     NTSTATUS ntStatus = STATUS_SUCCESS;
2398     PFILE_OBJECT pFileObject = NULL;
2399     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2400     PACCESS_MASK pDesiredAccess = NULL;
2401     USHORT usShareAccess;
2402     BOOLEAN bAllocatedCcb = FALSE, bReleaseFcb = FALSE;
2403     ULONG ulOptions = 0;
2404     AFSFileOpenCB   stOpenCB;
2405     AFSFileOpenResultCB stOpenResultCB;
2406     ULONG       ulResultLen = 0;
2407     AFSObjectInfoCB *pParentObjectInfo = NULL;
2408     AFSObjectInfoCB *pObjectInfo = NULL;
2409     ULONG       ulFileAccess = 0;
2410     AFSFileAccessReleaseCB stReleaseFileAccess;
2411     LONG lCount;
2412
2413     __Enter
2414     {
2415
2416         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2417         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2418
2419         pFileObject = pIrpSp->FileObject;
2420
2421         pParentObjectInfo = ParentDirCB->ObjectInformation;
2422
2423         pObjectInfo = DirectoryCB->ObjectInformation;
2424
2425         //
2426         // Check if the entry is pending a deletion
2427         //
2428
2429         if( BooleanFlagOn( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE))
2430         {
2431
2432             ntStatus = STATUS_DELETE_PENDING;
2433
2434             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2435                           AFS_TRACE_LEVEL_ERROR,
2436                           "AFSProcessOpen (%p) Entry pending delete %wZ Status %08lX\n",
2437                           Irp,
2438                           &DirectoryCB->NameInformation.FileName,
2439                           ntStatus);
2440
2441             try_return( ntStatus);
2442         }
2443
2444         //
2445         // Extract out the options
2446         //
2447
2448         ulOptions = pIrpSp->Parameters.Create.Options;
2449
2450         //
2451         // Check if we should go and retrieve updated information for the node
2452         //
2453
2454         ntStatus = AFSValidateEntry( DirectoryCB,
2455                                      AuthGroup,
2456                                      FALSE,
2457                                      TRUE);
2458
2459         if( !NT_SUCCESS( ntStatus))
2460         {
2461
2462             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2463                           AFS_TRACE_LEVEL_ERROR,
2464                           "AFSProcessOpen (%p) Failed to validate entry %wZ Status %08lX\n",
2465                           Irp,
2466                           &DirectoryCB->NameInformation.FileName,
2467                           ntStatus);
2468
2469             try_return( ntStatus);
2470         }
2471
2472         //
2473         // If this is marked for delete on close then be sure we can delete the entry
2474         //
2475
2476         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2477         {
2478
2479             ntStatus = AFSNotifyDelete( DirectoryCB,
2480                                         AuthGroup,
2481                                         TRUE);
2482
2483             if( !NT_SUCCESS( ntStatus))
2484             {
2485
2486                 ntStatus = STATUS_CANNOT_DELETE;
2487
2488                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2489                               AFS_TRACE_LEVEL_ERROR,
2490                               "AFSProcessOpen (%p) Cannot delete entry %wZ marked for delete on close Status %08lX\n",
2491                               Irp,
2492                               &DirectoryCB->NameInformation.FileName,
2493                               ntStatus);
2494
2495                 try_return( ntStatus);
2496             }
2497         }
2498
2499         //
2500         // Be sure we have an Fcb for the current object
2501         //
2502
2503         ntStatus = AFSInitFcb( DirectoryCB);
2504
2505         if( !NT_SUCCESS( ntStatus))
2506         {
2507
2508             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2509                           AFS_TRACE_LEVEL_ERROR,
2510                           "AFSProcessOpen (%p) Failed to init fcb on %wZ Status %08lX\n",
2511                           Irp,
2512                           &DirectoryCB->NameInformation.FileName,
2513                           ntStatus);
2514
2515             try_return( ntStatus);
2516         }
2517
2518         ntStatus = STATUS_SUCCESS;
2519
2520         //
2521         // AFSInitFcb returns the Fcb resource held
2522         //
2523
2524         bReleaseFcb = TRUE;
2525
2526         //
2527         // Increment the open count on this Fcb
2528         //
2529
2530         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
2531
2532         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2533                       AFS_TRACE_LEVEL_VERBOSE,
2534                       "AFSProcessOpen Increment2 count on Fcb %p Cnt %d\n",
2535                       pObjectInfo->Fcb,
2536                       lCount);
2537
2538         //
2539         // Check access on the entry
2540         //
2541
2542         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2543         {
2544
2545             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2546                                            usShareAccess,
2547                                            pFileObject,
2548                                            &pObjectInfo->Fcb->ShareAccess,
2549                                            FALSE);
2550
2551             if( !NT_SUCCESS( ntStatus))
2552             {
2553
2554                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2555                               AFS_TRACE_LEVEL_ERROR,
2556                               "AFSProcessOpen (%p) Failed to check share access on %wZ Status %08lX\n",
2557                               Irp,
2558                               &DirectoryCB->NameInformation.FileName,
2559                               ntStatus);
2560
2561                 try_return( ntStatus);
2562             }
2563         }
2564
2565         //
2566         // Additional checks
2567         //
2568
2569         if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_FILE_FCB)
2570         {
2571
2572             //
2573             // If the caller is asking for write access then try to flush the image section
2574             //
2575
2576             if( FlagOn( *pDesiredAccess, FILE_WRITE_DATA) ||
2577                 BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE))
2578             {
2579
2580                 BOOLEAN bMmFlushed;
2581
2582                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2583                               AFS_TRACE_LEVEL_VERBOSE,
2584                               "AFSProcessOpen Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
2585                               &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2586                               PsGetCurrentThread());
2587
2588                 AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2589                                 TRUE);
2590
2591                 bMmFlushed = MmFlushImageSection( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2592                                                   MmFlushForWrite);
2593
2594                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2595                               AFS_TRACE_LEVEL_VERBOSE,
2596                               "AFSProcessOpen Releasing Fcb SectionObject lock %p EXCL %08lX\n",
2597                               &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2598                               PsGetCurrentThread());
2599
2600                 AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
2601
2602                 if ( !bMmFlushed)
2603                 {
2604
2605                     ntStatus = BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE) ? STATUS_CANNOT_DELETE :
2606                                                                             STATUS_SHARING_VIOLATION;
2607
2608                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2609                                   AFS_TRACE_LEVEL_ERROR,
2610                                   "AFSProcessOpen (%p) Failed to flush image section %wZ Status %08lX\n",
2611                                   Irp,
2612                                   &DirectoryCB->NameInformation.FileName,
2613                                   ntStatus);
2614
2615                     try_return( ntStatus);
2616                 }
2617             }
2618
2619             if( BooleanFlagOn( ulOptions, FILE_DIRECTORY_FILE))
2620             {
2621
2622                 ntStatus = STATUS_NOT_A_DIRECTORY;
2623
2624                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2625                               AFS_TRACE_LEVEL_ERROR,
2626                               "AFSProcessOpen (%p) Attempt to open file as directory %wZ Status %08lX\n",
2627                               Irp,
2628                               &DirectoryCB->NameInformation.FileName,
2629                               ntStatus);
2630
2631                 try_return( ntStatus);
2632             }
2633
2634             pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
2635         }
2636         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB ||
2637                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2638         {
2639
2640             if( BooleanFlagOn( ulOptions, FILE_NON_DIRECTORY_FILE))
2641             {
2642
2643                 ntStatus = STATUS_FILE_IS_A_DIRECTORY;
2644
2645                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2646                               AFS_TRACE_LEVEL_ERROR,
2647                               "AFSProcessOpen (%p) Attempt to open directory as file %wZ Status %08lX\n",
2648                               Irp,
2649                               &DirectoryCB->NameInformation.FileName,
2650                               ntStatus);
2651
2652                 try_return( ntStatus);
2653             }
2654         }
2655         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
2656                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
2657                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
2658                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_INVALID_FCB)
2659         {
2660
2661         }
2662         else
2663         {
2664             ASSERT( FALSE);
2665             try_return( ntStatus = STATUS_UNSUCCESSFUL);
2666         }
2667
2668         //
2669         // Check with the service that we can open the file
2670         //
2671
2672         stOpenCB.ParentId = pParentObjectInfo->FileId;
2673
2674         stOpenCB.DesiredAccess = *pDesiredAccess;
2675
2676         stOpenCB.ShareAccess = usShareAccess;
2677
2678         stOpenCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
2679
2680         stOpenCB.Identifier = (ULONGLONG)pFileObject;
2681
2682         stOpenResultCB.GrantedAccess = 0;
2683
2684         ulResultLen = sizeof( AFSFileOpenResultCB);
2685
2686         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_OPEN_FILE,
2687                                       AFS_REQUEST_FLAG_SYNCHRONOUS | AFS_REQUEST_FLAG_HOLD_FID,
2688                                       AuthGroup,
2689                                       &DirectoryCB->NameInformation.FileName,
2690                                       &pObjectInfo->FileId,
2691                                       (void *)&stOpenCB,
2692                                       sizeof( AFSFileOpenCB),
2693                                       (void *)&stOpenResultCB,
2694                                       &ulResultLen);
2695
2696         if( !NT_SUCCESS( ntStatus))
2697         {
2698
2699             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2700                           AFS_TRACE_LEVEL_ERROR,
2701                           "AFSProcessOpen (%p) Failed open in service %wZ Status %08lX\n",
2702                           Irp,
2703                           &DirectoryCB->NameInformation.FileName,
2704                           ntStatus);
2705
2706             try_return( ntStatus);
2707         }
2708
2709         //
2710         // Save the granted access in case we need to release it below
2711         //
2712
2713         ulFileAccess = stOpenResultCB.FileAccess;
2714
2715         //
2716         // Check if there is a conflict
2717         //
2718
2719         if( !AFSCheckAccess( *pDesiredAccess,
2720                              stOpenResultCB.GrantedAccess,
2721                              BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_DIRECTORY)))
2722         {
2723
2724             ntStatus = STATUS_ACCESS_DENIED;
2725
2726             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2727                           AFS_TRACE_LEVEL_ERROR,
2728                           "AFSProcessOpen (%p) Failed to check access from service Desired %08lX Granted %08lX Entry %wZ Status %08lX\n",
2729                           Irp,
2730                           *pDesiredAccess,
2731                           stOpenResultCB.GrantedAccess,
2732                           &DirectoryCB->NameInformation.FileName,
2733                           ntStatus);
2734
2735             try_return( ntStatus);
2736         }
2737
2738         //
2739         // Initialize the Ccb for the file.
2740         //
2741
2742         ntStatus = AFSInitCcb( Ccb,
2743                                DirectoryCB,
2744                                *pDesiredAccess,
2745                                ulFileAccess);
2746
2747         if( !NT_SUCCESS( ntStatus))
2748         {
2749
2750             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2751                           AFS_TRACE_LEVEL_ERROR,
2752                           "AFSProcessOpen (%p) Failed to initialize ccb %wZ Status %08lX\n",
2753                           Irp,
2754                           &DirectoryCB->NameInformation.FileName,
2755                           ntStatus);
2756
2757             try_return( ntStatus);
2758         }
2759
2760         bAllocatedCcb = TRUE;
2761
2762         //
2763         // Perform the access check on the target if this is a mount point or symlink
2764         //
2765
2766         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2767         {
2768
2769             IoUpdateShareAccess( pFileObject,
2770                                  &pObjectInfo->Fcb->ShareAccess);
2771         }
2772         else
2773         {
2774
2775             //
2776             // Set the access
2777             //
2778
2779             IoSetShareAccess( *pDesiredAccess,
2780                               usShareAccess,
2781                               pFileObject,
2782                               &pObjectInfo->Fcb->ShareAccess);
2783         }
2784
2785         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenHandleCount);
2786
2787         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2788                       AFS_TRACE_LEVEL_VERBOSE,
2789                       "AFSProcessOpen Increment handle count on Fcb %p Cnt %d\n",
2790                       pObjectInfo->Fcb,
2791                       lCount);
2792
2793         //
2794         // Increment the open reference and handle on the parent node
2795         //
2796
2797         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
2798
2799         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2800                       AFS_TRACE_LEVEL_VERBOSE,
2801                       "AFSProcessOpen Increment child open handle count on Parent object %p Cnt %d\n",
2802                       pObjectInfo->ParentObjectInformation,
2803                       lCount);
2804
2805         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
2806
2807         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2808                       AFS_TRACE_LEVEL_VERBOSE,
2809                       "AFSProcessOpen Increment child open ref count on Parent object %p Cnt %d\n",
2810                       pObjectInfo->ParentObjectInformation,
2811                       lCount);
2812
2813         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2814         {
2815
2816             //
2817             // Mark it for delete on close
2818             //
2819
2820             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2821                           AFS_TRACE_LEVEL_VERBOSE,
2822                           "AFSProcessOpen (%p) Setting PENDING_DELETE flag in DirEntry %p Name %wZ\n",
2823                           Irp,
2824                           DirectoryCB,
2825                           &DirectoryCB->NameInformation.FileName);
2826
2827             SetFlag( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2828         }
2829
2830         //
2831         // Indicate the object is held
2832         //
2833
2834         SetFlag( pObjectInfo->Flags, AFS_OBJECT_HELD_IN_SERVICE);
2835
2836         //
2837         // Return the open result for this file
2838         //
2839
2840         Irp->IoStatus.Information = FILE_OPENED;
2841
2842         *Fcb = pObjectInfo->Fcb;
2843
2844 try_exit:
2845
2846         if( bReleaseFcb)
2847         {
2848
2849             if( !NT_SUCCESS( ntStatus))
2850             {
2851                 //
2852                 // Decrement the open count on this Fcb
2853                 //
2854
2855                 lCount = InterlockedDecrement( &pObjectInfo->Fcb->OpenReferenceCount);
2856
2857                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2858                               AFS_TRACE_LEVEL_VERBOSE,
2859                               "AFSProcessOpen Decrement2 count on Fcb %p Cnt %d\n",
2860                               pObjectInfo->Fcb,
2861                               lCount);
2862             }
2863
2864             AFSReleaseResource( pObjectInfo->Fcb->Header.Resource);
2865         }
2866
2867         if( !NT_SUCCESS( ntStatus))
2868         {
2869
2870             if ( ulFileAccess > 0)
2871             {
2872
2873                 stReleaseFileAccess.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
2874
2875                 stReleaseFileAccess.FileAccess = ulFileAccess;
2876
2877                 stReleaseFileAccess.Identifier = (ULONGLONG)pFileObject;
2878
2879                 AFSProcessRequest( AFS_REQUEST_TYPE_RELEASE_FILE_ACCESS,
2880                                    AFS_REQUEST_FLAG_SYNCHRONOUS,
2881                                    AuthGroup,
2882                                    &DirectoryCB->NameInformation.FileName,
2883                                    &pObjectInfo->FileId,
2884                                    (void *)&stReleaseFileAccess,
2885                                    sizeof( AFSFileAccessReleaseCB),
2886                                    NULL,
2887                                    NULL);
2888             }
2889
2890             if( bAllocatedCcb)
2891             {
2892
2893                 AFSRemoveCcb( NULL,
2894                               *Ccb);
2895             }
2896
2897             *Ccb = NULL;
2898
2899             //
2900             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2901             //
2902
2903             *Fcb = NULL;
2904         }
2905     }
2906
2907     return ntStatus;
2908 }
2909
2910 NTSTATUS
2911 AFSProcessOverwriteSupersede( IN PDEVICE_OBJECT DeviceObject,
2912                               IN PIRP           Irp,
2913                               IN AFSVolumeCB   *VolumeCB,
2914                               IN GUID          *AuthGroup,
2915                               IN AFSDirectoryCB *ParentDirCB,
2916                               IN AFSDirectoryCB *DirectoryCB,
2917                               OUT AFSFcb       **Fcb,
2918                               OUT AFSCcb       **Ccb)
2919 {
2920     UNREFERENCED_PARAMETER(DeviceObject);
2921     NTSTATUS ntStatus = STATUS_SUCCESS;
2922     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2923     PFILE_OBJECT pFileObject = NULL;
2924     LARGE_INTEGER liZero = {0,0};
2925     BOOLEAN bReleasePaging = FALSE, bReleaseFcb = FALSE;
2926     ULONG   ulAttributes = 0;
2927     ULONG ulCreateDisposition = 0;
2928     BOOLEAN bAllocatedCcb = FALSE;
2929     BOOLEAN bUserMapped = FALSE;
2930     PACCESS_MASK pDesiredAccess = NULL;
2931     USHORT usShareAccess;
2932     AFSObjectInfoCB *pParentObjectInfo = NULL;
2933     AFSObjectInfoCB *pObjectInfo = NULL;
2934     LONG lCount;
2935     LARGE_INTEGER liSaveSize;
2936     LARGE_INTEGER liSaveVDL;
2937     LARGE_INTEGER liSaveAlloc;
2938
2939     __Enter
2940     {
2941
2942         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2943
2944         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2945
2946         pFileObject = pIrpSp->FileObject;
2947
2948         ulAttributes = pIrpSp->Parameters.Create.FileAttributes;
2949
2950         ulCreateDisposition = (pIrpSp->Parameters.Create.Options >> 24) & 0x000000ff;
2951
2952         if( BooleanFlagOn( VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
2953         {
2954
2955             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2956                           AFS_TRACE_LEVEL_ERROR,
2957                           "AFSProcessOverwriteSupersede Request failed on %wZ due to read only volume\n",
2958                           Irp,
2959                           &DirectoryCB->NameInformation.FileName);
2960
2961             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
2962         }
2963
2964         pParentObjectInfo = ParentDirCB->ObjectInformation;
2965
2966         pObjectInfo = DirectoryCB->ObjectInformation;
2967
2968         //
2969         // Check if we should go and retrieve updated information for the node
2970         //
2971
2972         ntStatus = AFSValidateEntry( DirectoryCB,
2973                                      AuthGroup,
2974                                      FALSE,
2975                                      TRUE);
2976
2977         if( !NT_SUCCESS( ntStatus))
2978         {
2979
2980             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2981                           AFS_TRACE_LEVEL_ERROR,
2982                           "AFSProcessOverwriteSupersede (%p) Failed to validate entry %wZ Status %08lX\n",
2983                           Irp,
2984                           &DirectoryCB->NameInformation.FileName,
2985                           ntStatus);
2986
2987             try_return( ntStatus);
2988         }
2989
2990         //
2991         // Be sure we have an Fcb for the object block
2992         //
2993
2994         ntStatus = AFSInitFcb( DirectoryCB);
2995
2996         *Fcb = pObjectInfo->Fcb;
2997
2998         if( !NT_SUCCESS( ntStatus))
2999         {
3000
3001             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3002                           AFS_TRACE_LEVEL_ERROR,
3003                           "AFSProcessOverwriteSupersede (%p) Failed to initialize fcb %wZ Status %08lX\n",
3004                           Irp,
3005                           &DirectoryCB->NameInformation.FileName,
3006                           ntStatus);
3007
3008             try_return( ntStatus);
3009         }
3010
3011         ntStatus = STATUS_SUCCESS;
3012
3013         //
3014         // Increment the open count on this Fcb.
3015         //
3016
3017         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
3018
3019         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3020                       AFS_TRACE_LEVEL_VERBOSE,
3021                       "AFSProcessOverwriteSupersede Increment2 count on Fcb %p Cnt %d\n",
3022                       pObjectInfo->Fcb,
3023                       lCount);
3024
3025         bReleaseFcb = TRUE;
3026
3027         //
3028         // Check access on the entry
3029         //
3030
3031         if( pObjectInfo->Fcb->OpenHandleCount > 0)
3032         {
3033
3034             ntStatus = IoCheckShareAccess( *pDesiredAccess,
3035                                            usShareAccess,
3036                                            pFileObject,
3037                                            &pObjectInfo->Fcb->ShareAccess,
3038                                            FALSE);
3039
3040             if( !NT_SUCCESS( ntStatus))
3041             {
3042
3043                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3044                               AFS_TRACE_LEVEL_ERROR,
3045                               "AFSProcessOverwriteSupersede (%p) Access check failure %wZ Status %08lX\n",
3046                               Irp,
3047                               &DirectoryCB->NameInformation.FileName,
3048                               ntStatus);
3049
3050                 try_return( ntStatus);
3051             }
3052         }
3053
3054         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3055                       AFS_TRACE_LEVEL_VERBOSE,
3056                       "AFSProcessOverwriteSupercede Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
3057                       &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3058                       PsGetCurrentThread());
3059
3060         AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3061                         TRUE);
3062
3063         //
3064         //  Before we actually truncate, check to see if the purge
3065         //  is going to fail.
3066         //
3067
3068         bUserMapped = !MmCanFileBeTruncated( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
3069                                              &liZero);
3070
3071         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3072                       AFS_TRACE_LEVEL_VERBOSE,
3073                       "AFSProcessOverwriteSupercede Releasing Fcb SectionObject lock %p EXCL %08lX\n",
3074                       &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3075                       PsGetCurrentThread());
3076
3077         AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
3078
3079         if( bUserMapped)
3080         {
3081
3082             ntStatus = STATUS_USER_MAPPED_FILE;
3083
3084             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3085                           AFS_TRACE_LEVEL_ERROR,
3086                           "AFSProcessOverwriteSupersede (%p) File user mapped %wZ Status %08lX\n",
3087                           Irp,
3088                           &DirectoryCB->NameInformation.FileName,
3089                           ntStatus);
3090
3091             try_return( ntStatus);
3092         }
3093
3094         //
3095         // Initialize the Ccb for the file.
3096         //
3097
3098         ntStatus = AFSInitCcb( Ccb,
3099                                DirectoryCB,
3100                                *pDesiredAccess,
3101                                0);
3102
3103         if( !NT_SUCCESS( ntStatus))
3104         {
3105
3106             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3107                           AFS_TRACE_LEVEL_ERROR,
3108                           "AFSProcessOverwriteSupersede (%p) Failed to initialize ccb %wZ Status %08lX\n",
3109                           Irp,
3110                           &DirectoryCB->NameInformation.FileName,
3111                           ntStatus);
3112
3113             try_return( ntStatus);
3114         }
3115
3116         bAllocatedCcb = TRUE;
3117
3118         //
3119         // Set the file length to zero
3120         //
3121
3122         AFSAcquireExcl( pObjectInfo->Fcb->Header.PagingIoResource,
3123                         TRUE);
3124
3125         bReleasePaging = TRUE;
3126
3127         liSaveSize = pObjectInfo->Fcb->Header.FileSize;
3128         liSaveAlloc = pObjectInfo->Fcb->Header.AllocationSize;
3129         liSaveVDL = pObjectInfo->Fcb->Header.ValidDataLength;
3130
3131         pObjectInfo->Fcb->Header.FileSize.QuadPart = 0;
3132         pObjectInfo->Fcb->Header.ValidDataLength.QuadPart = 0;
3133         pObjectInfo->Fcb->Header.AllocationSize.QuadPart = 0;
3134
3135         pObjectInfo->EndOfFile.QuadPart = 0;
3136         pObjectInfo->AllocationSize.QuadPart = 0;
3137
3138         //
3139         // Trim down the extents. We do this BEFORE telling the service
3140         // the file is truncated since there is a potential race between
3141         // a worker thread releasing extents and us trimming
3142         //
3143
3144         AFSTrimExtents( pObjectInfo->Fcb,
3145                         &pObjectInfo->Fcb->Header.FileSize);
3146
3147         KeQuerySystemTime( &pObjectInfo->ChangeTime);
3148
3149         KeQuerySystemTime( &pObjectInfo->LastAccessTime);
3150
3151         KeQuerySystemTime( &pObjectInfo->LastWriteTime);
3152
3153         //
3154         // Set the update flag accordingly