Windows: Test for PRSFS_INSERT in redirector interface
[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 Name not found\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 %p FsContext2 %p\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 %p FsContext2 %p\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 %p FsContext2 %p\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         lCount = InterlockedIncrement( &VolumeCB->RootFcb->OpenReferenceCount);
1472
1473         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1474                       AFS_TRACE_LEVEL_VERBOSE,
1475                       "AFSOpenRoot Increment count on Fcb %p Cnt %d\n",
1476                       VolumeCB->RootFcb,
1477                       lCount);
1478
1479         bReleaseFcb = TRUE;
1480
1481         //
1482         // If there are current opens on the Fcb, check the access.
1483         //
1484
1485         if( VolumeCB->RootFcb->OpenHandleCount > 0)
1486         {
1487
1488             ntStatus = IoCheckShareAccess( *pDesiredAccess,
1489                                            usShareAccess,
1490                                            pFileObject,
1491                                            &VolumeCB->RootFcb->ShareAccess,
1492                                            FALSE);
1493
1494             if( !NT_SUCCESS( ntStatus))
1495             {
1496
1497                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1498                               AFS_TRACE_LEVEL_ERROR,
1499                               "AFSOpenRoot (%p) Access check failure Status %08lX\n",
1500                               Irp,
1501                               ntStatus);
1502
1503                 try_return( ntStatus);
1504             }
1505         }
1506
1507         //
1508         // Initialize the Ccb for the file.
1509         //
1510
1511         ntStatus = AFSInitCcb( Ccb,
1512                                VolumeCB->DirectoryCB,
1513                                *pDesiredAccess,
1514                                0);
1515
1516         if( !NT_SUCCESS( ntStatus))
1517         {
1518
1519             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1520                           AFS_TRACE_LEVEL_ERROR,
1521                           "AFSOpenRoot (%p) Failed to allocate Ccb Status %08lX\n",
1522                           Irp,
1523                           ntStatus);
1524
1525             try_return( ntStatus);
1526         }
1527
1528         bAllocatedCcb = TRUE;
1529
1530         //
1531         // OK, update the share access on the fileobject
1532         //
1533
1534         if( VolumeCB->RootFcb->OpenHandleCount > 0)
1535         {
1536
1537             IoUpdateShareAccess( pFileObject,
1538                                  &VolumeCB->RootFcb->ShareAccess);
1539         }
1540         else
1541         {
1542
1543             //
1544             // Set the access
1545             //
1546
1547             IoSetShareAccess( *pDesiredAccess,
1548                               usShareAccess,
1549                               pFileObject,
1550                               &VolumeCB->RootFcb->ShareAccess);
1551         }
1552
1553         //
1554         // Increment the open count on this Fcb
1555         //
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             if ( !NT_SUCCESS( ntStatus))
1584             {
1585
1586                 lCount = InterlockedDecrement( &VolumeCB->RootFcb->OpenReferenceCount);
1587
1588                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1589                               AFS_TRACE_LEVEL_VERBOSE,
1590                               "AFSOpenRoot Decrement count on Fcb %p Cnt %d\n",
1591                               VolumeCB->RootFcb,
1592                               lCount);
1593             }
1594
1595             AFSReleaseResource( VolumeCB->RootFcb->Header.Resource);
1596         }
1597
1598         if( !NT_SUCCESS( ntStatus))
1599         {
1600
1601             if( bAllocatedCcb)
1602             {
1603
1604                 AFSRemoveCcb( NULL,
1605                               *Ccb);
1606
1607                 *Ccb = NULL;
1608             }
1609
1610             Irp->IoStatus.Information = 0;
1611         }
1612     }
1613
1614     return ntStatus;
1615 }
1616
1617 NTSTATUS
1618 AFSProcessCreate( IN PIRP               Irp,
1619                   IN GUID              *AuthGroup,
1620                   IN AFSVolumeCB       *VolumeCB,
1621                   IN AFSDirectoryCB    *ParentDirCB,
1622                   IN PUNICODE_STRING    FileName,
1623                   IN PUNICODE_STRING    ComponentName,
1624                   IN PUNICODE_STRING    FullFileName,
1625                   OUT AFSFcb          **Fcb,
1626                   OUT AFSCcb          **Ccb)
1627 {
1628
1629     NTSTATUS ntStatus = STATUS_SUCCESS;
1630     PFILE_OBJECT pFileObject = NULL;
1631     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1632     ULONG ulOptions = 0;
1633     ULONG ulAttributes = 0;
1634     BOOLEAN bFileCreated = FALSE, bReleaseFcb = FALSE, bAllocatedCcb = FALSE;
1635     PACCESS_MASK pDesiredAccess = NULL;
1636     USHORT usShareAccess;
1637     AFSDirectoryCB *pDirEntry = NULL;
1638     AFSObjectInfoCB *pParentObjectInfo = NULL;
1639     AFSObjectInfoCB *pObjectInfo = NULL;
1640     LONG lCount;
1641
1642     __Enter
1643     {
1644
1645         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
1646         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
1647
1648         pFileObject = pIrpSp->FileObject;
1649
1650         //
1651         // Extract out the options
1652         //
1653
1654         ulOptions = pIrpSp->Parameters.Create.Options;
1655
1656         //
1657         // We pass all attributes they want to apply to the file to the create
1658         //
1659
1660         ulAttributes = pIrpSp->Parameters.Create.FileAttributes;
1661
1662         //
1663         // If this is a directory create then set the attribute correctly
1664         //
1665
1666         if( ulOptions & FILE_DIRECTORY_FILE)
1667         {
1668
1669             ulAttributes |= FILE_ATTRIBUTE_DIRECTORY;
1670         }
1671
1672         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1673                       AFS_TRACE_LEVEL_VERBOSE,
1674                       "AFSProcessCreate (%p) Creating file %wZ Attributes %08lX\n",
1675                       Irp,
1676                       FullFileName,
1677                       ulAttributes);
1678
1679         if( BooleanFlagOn( VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
1680         {
1681
1682             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1683                           AFS_TRACE_LEVEL_ERROR,
1684                           "AFSProcessCreate Request failed due to read only volume %wZ\n",
1685                           FullFileName);
1686
1687             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
1688         }
1689
1690         pParentObjectInfo = ParentDirCB->ObjectInformation;
1691
1692         //
1693         // Allocate and insert the direntry into the parent node
1694         //
1695
1696         ntStatus = AFSCreateDirEntry( AuthGroup,
1697                                       pParentObjectInfo,
1698                                       ParentDirCB,
1699                                       FileName,
1700                                       ComponentName,
1701                                       ulAttributes,
1702                                       &pDirEntry);
1703
1704         if( !NT_SUCCESS( ntStatus))
1705         {
1706
1707             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1708                           AFS_TRACE_LEVEL_ERROR,
1709                           "AFSProcessCreate (%p) Failed to create directory entry %wZ Status %08lX\n",
1710                           Irp,
1711                           FullFileName,
1712                           ntStatus);
1713
1714             try_return( ntStatus);
1715         }
1716
1717         bFileCreated = TRUE;
1718
1719         pObjectInfo = pDirEntry->ObjectInformation;
1720
1721         if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED) ||
1722             pObjectInfo->FileType == AFS_FILE_TYPE_UNKNOWN)
1723         {
1724
1725             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1726                           AFS_TRACE_LEVEL_VERBOSE,
1727                           "AFSProcessCreate (%p) Evaluating object %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1728                           Irp,
1729                           &pDirEntry->NameInformation.FileName,
1730                           pObjectInfo->FileId.Cell,
1731                           pObjectInfo->FileId.Volume,
1732                           pObjectInfo->FileId.Vnode,
1733                           pObjectInfo->FileId.Unique);
1734
1735             ntStatus = AFSEvaluateNode( AuthGroup,
1736                                         pDirEntry);
1737
1738             if( !NT_SUCCESS( ntStatus))
1739             {
1740
1741                 if ( ntStatus == STATUS_NOT_A_DIRECTORY)
1742                 {
1743
1744                     if ( pParentObjectInfo == pObjectInfo->ParentObjectInformation)
1745                     {
1746
1747                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1748                                       AFS_TRACE_LEVEL_ERROR,
1749                                       "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1750                                       Irp,
1751                                       &pDirEntry->NameInformation.FileName,
1752                                       pObjectInfo->FileId.Cell,
1753                                       pObjectInfo->FileId.Volume,
1754                                       pObjectInfo->FileId.Vnode,
1755                                       pObjectInfo->FileId.Unique,
1756                                       pParentObjectInfo->FileId.Cell,
1757                                       pParentObjectInfo->FileId.Volume,
1758                                       pParentObjectInfo->FileId.Vnode,
1759                                       pParentObjectInfo->FileId.Unique,
1760                                       ntStatus);
1761                     }
1762                     else if ( pObjectInfo->ParentObjectInformation == NULL)
1763                     {
1764
1765                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1766                                       AFS_TRACE_LEVEL_ERROR,
1767                                       "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX != NULL Status %08lX\n",
1768                                       Irp,
1769                                       &pDirEntry->NameInformation.FileName,
1770                                       pObjectInfo->FileId.Cell,
1771                                       pObjectInfo->FileId.Volume,
1772                                       pObjectInfo->FileId.Vnode,
1773                                       pObjectInfo->FileId.Unique,
1774                                       pParentObjectInfo->FileId.Cell,
1775                                       pParentObjectInfo->FileId.Volume,
1776                                       pParentObjectInfo->FileId.Vnode,
1777                                       pParentObjectInfo->FileId.Unique,
1778                                       ntStatus);
1779                     }
1780                     else
1781                     {
1782
1783                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1784                                       AFS_TRACE_LEVEL_ERROR,
1785                                       "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX != %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1786                                       Irp,
1787                                       &pDirEntry->NameInformation.FileName,
1788                                       pObjectInfo->FileId.Cell,
1789                                       pObjectInfo->FileId.Volume,
1790                                       pObjectInfo->FileId.Vnode,
1791                                       pObjectInfo->FileId.Unique,
1792                                       pParentObjectInfo->FileId.Cell,
1793                                       pParentObjectInfo->FileId.Volume,
1794                                       pParentObjectInfo->FileId.Vnode,
1795                                       pParentObjectInfo->FileId.Unique,
1796                                       pObjectInfo->ParentObjectInformation->FileId.Cell,
1797                                       pObjectInfo->ParentObjectInformation->FileId.Volume,
1798                                       pObjectInfo->ParentObjectInformation->FileId.Vnode,
1799                                       pObjectInfo->ParentObjectInformation->FileId.Unique,
1800                                       ntStatus);
1801                     }
1802                 }
1803                 else
1804                 {
1805
1806                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1807                                   AFS_TRACE_LEVEL_ERROR,
1808                                   "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1809                                   Irp,
1810                                   &pDirEntry->NameInformation.FileName,
1811                                   pObjectInfo->FileId.Cell,
1812                                   pObjectInfo->FileId.Volume,
1813                                   pObjectInfo->FileId.Vnode,
1814                                   pObjectInfo->FileId.Unique,
1815                                   ntStatus);
1816                 }
1817
1818                 try_return( ntStatus);
1819             }
1820
1821             ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED);
1822         }
1823
1824         //
1825         // We may have raced and the Fcb is already created
1826         //
1827
1828         //
1829         // Allocate and initialize the Fcb for the file.
1830         //
1831
1832         ntStatus = AFSInitFcb( pDirEntry);
1833
1834         *Fcb = pObjectInfo->Fcb;
1835
1836         if( !NT_SUCCESS( ntStatus))
1837         {
1838
1839             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1840                           AFS_TRACE_LEVEL_ERROR,
1841                           "AFSProcessCreate (%p) Failed to initialize fcb %wZ Status %08lX\n",
1842                           Irp,
1843                           FullFileName,
1844                           ntStatus);
1845
1846             try_return( ntStatus);
1847         }
1848
1849         ntStatus = STATUS_SUCCESS;
1850
1851         //
1852         // Increment the open count on this Fcb
1853         //
1854
1855         lCount = InterlockedIncrement( &(*Fcb)->OpenReferenceCount);
1856
1857         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1858                       AFS_TRACE_LEVEL_VERBOSE,
1859                       "AFSProcessCreate Increment count on Fcb %p Cnt %d\n",
1860                       *Fcb,
1861                       lCount);
1862
1863         bReleaseFcb = TRUE;
1864
1865         //
1866         // Initialize the Ccb for the file.
1867         //
1868
1869         ntStatus = AFSInitCcb( Ccb,
1870                                pDirEntry,
1871                                *pDesiredAccess,
1872                                0);
1873
1874         if( !NT_SUCCESS( ntStatus))
1875         {
1876
1877             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1878                           AFS_TRACE_LEVEL_ERROR,
1879                           "AFSProcessCreate (%p) Failed to initialize ccb %wZ Status %08lX\n",
1880                           Irp,
1881                           FullFileName,
1882                           ntStatus);
1883
1884             try_return( ntStatus);
1885         }
1886
1887         bAllocatedCcb = TRUE;
1888
1889         //
1890         // If this is a file, update the headers filesizes.
1891         //
1892
1893         if( (*Fcb)->Header.NodeTypeCode == AFS_FILE_FCB)
1894         {
1895
1896             //
1897             // Update the sizes with the information passed in
1898             //
1899
1900             (*Fcb)->Header.AllocationSize.QuadPart  = pObjectInfo->AllocationSize.QuadPart;
1901             (*Fcb)->Header.FileSize.QuadPart        = pObjectInfo->EndOfFile.QuadPart;
1902             (*Fcb)->Header.ValidDataLength.QuadPart = pObjectInfo->EndOfFile.QuadPart;
1903
1904             //
1905             // Notify the system of the addition
1906             //
1907
1908             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1909                                             *Ccb,
1910                                             (ULONG)FILE_NOTIFY_CHANGE_FILE_NAME,
1911                                             (ULONG)FILE_ACTION_ADDED);
1912
1913             (*Fcb)->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
1914         }
1915         else if( (*Fcb)->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
1916         {
1917
1918             //
1919             // This is a new directory node so indicate it has been enumerated
1920             //
1921
1922             SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1923
1924             //
1925             // And the parent directory entry
1926             //
1927
1928             KeQuerySystemTime( &pParentObjectInfo->ChangeTime);
1929
1930             //
1931             // Notify the system of the addition
1932             //
1933
1934             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1935                                             *Ccb,
1936                                             (ULONG)FILE_NOTIFY_CHANGE_DIR_NAME,
1937                                             (ULONG)FILE_ACTION_ADDED);
1938         }
1939         else if( (*Fcb)->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
1940                  (*Fcb)->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
1941                  (*Fcb)->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
1942                  (*Fcb)->Header.NodeTypeCode == AFS_INVALID_FCB)
1943         {
1944
1945             //
1946             // And the parent directory entry
1947             //
1948
1949             KeQuerySystemTime( &pParentObjectInfo->ChangeTime);
1950
1951             //
1952             // Notify the system of the addition
1953             //
1954
1955             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1956                                             *Ccb,
1957                                             (ULONG)FILE_NOTIFY_CHANGE_DIR_NAME,
1958                                             (ULONG)FILE_ACTION_ADDED);
1959         }
1960
1961         //
1962         // Save off the access for the open
1963         //
1964
1965         IoSetShareAccess( *pDesiredAccess,
1966                           usShareAccess,
1967                           pFileObject,
1968                           &(*Fcb)->ShareAccess);
1969
1970         lCount = InterlockedIncrement( &(*Fcb)->OpenHandleCount);
1971
1972         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1973                       AFS_TRACE_LEVEL_VERBOSE,
1974                       "AFSProcessCreate Increment handle count on Fcb %p Cnt %d\n",
1975                       (*Fcb),
1976                       lCount);
1977
1978         //
1979         // Increment the open reference and handle on the parent node
1980         //
1981
1982         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
1983
1984         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1985                       AFS_TRACE_LEVEL_VERBOSE,
1986                       "AFSProcessCreate Increment child open handle count on Parent object %p Cnt %d\n",
1987                       pObjectInfo->ParentObjectInformation,
1988                       lCount);
1989
1990         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
1991
1992         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1993                       AFS_TRACE_LEVEL_VERBOSE,
1994                       "AFSProcessCreate Increment child open ref count on Parent object %p Cnt %d\n",
1995                       pObjectInfo->ParentObjectInformation,
1996                       lCount);
1997
1998         if( ulOptions & FILE_DELETE_ON_CLOSE)
1999         {
2000
2001             //
2002             // Mark it for delete on close
2003             //
2004
2005             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2006                           AFS_TRACE_LEVEL_VERBOSE,
2007                           "AFSProcessCreate (%p) Setting PENDING_DELETE flag in DirEntry %p Name %wZ\n",
2008                           Irp,
2009                           pDirEntry,
2010                           FullFileName);
2011
2012             SetFlag( pDirEntry->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2013         }
2014
2015         //
2016         // Indicate the object is locked in the service
2017         //
2018
2019         SetFlag( pObjectInfo->Flags, AFS_OBJECT_HELD_IN_SERVICE);
2020
2021         //
2022         // Return the open result for this file
2023         //
2024
2025         Irp->IoStatus.Information = FILE_CREATED;
2026
2027 try_exit:
2028
2029         //
2030         // If we created the Fcb we need to release the resources
2031         //
2032
2033         if( bReleaseFcb)
2034         {
2035
2036             if( !NT_SUCCESS( ntStatus))
2037             {
2038                 //
2039                 // Decrement the open count on this Fcb
2040                 //
2041
2042                 lCount = InterlockedDecrement( &(*Fcb)->OpenReferenceCount);
2043
2044                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2045                               AFS_TRACE_LEVEL_VERBOSE,
2046                               "AFSProcessCreate Decrement count on Fcb %p Cnt %d\n",
2047                               *Fcb,
2048                               lCount);
2049             }
2050
2051             AFSReleaseResource( &(*Fcb)->NPFcb->Resource);
2052         }
2053
2054         if ( bFileCreated)
2055         {
2056
2057             //
2058             // Decrement the reference added during initialization of the DE
2059             // AFSInitCcb allocates its own reference count.
2060             //
2061
2062             lCount = InterlockedDecrement( &pDirEntry->DirOpenReferenceCount);
2063
2064             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2065                           AFS_TRACE_LEVEL_VERBOSE,
2066                           "AFSProcessCreate Decrement count on %wZ DE %p Cnt %d\n",
2067                           &pDirEntry->NameInformation.FileName,
2068                           pDirEntry,
2069                           lCount);
2070
2071             ASSERT( lCount >= 0);
2072         }
2073
2074         if( !NT_SUCCESS( ntStatus))
2075         {
2076
2077             if( bFileCreated)
2078             {
2079
2080                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2081                               AFS_TRACE_LEVEL_VERBOSE,
2082                               "AFSProcessCreate Create failed, removing DE %p from parent object %p Status %08lX\n",
2083                               pDirEntry,
2084                               pParentObjectInfo,
2085                               ntStatus);
2086
2087                 //
2088                 // Remove the dir entry from the parent
2089                 //
2090
2091                 AFSAcquireExcl( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
2092                                 TRUE);
2093
2094                 SetFlag( pDirEntry->Flags, AFS_DIR_ENTRY_DELETED);
2095
2096                 AFSNotifyDelete( pDirEntry,
2097                                  AuthGroup,
2098                                  FALSE);
2099
2100                 //
2101                 // Pull the directory entry from the parent
2102                 //
2103
2104                 AFSRemoveDirNodeFromParent( pParentObjectInfo,
2105                                             pDirEntry,
2106                                             FALSE); // Leave it in the enum list so the worker cleans it up
2107
2108                 //
2109                 // Tag the parent as needing verification
2110                 //
2111
2112                 SetFlag( pParentObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2113
2114                 pParentObjectInfo->DataVersion.QuadPart = (ULONGLONG)-1;
2115
2116                 AFSReleaseResource( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
2117             }
2118
2119             if( bAllocatedCcb)
2120             {
2121
2122                 AFSRemoveCcb( NULL,
2123                               *Ccb);
2124             }
2125
2126             //
2127             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2128             //
2129
2130             *Fcb = NULL;
2131
2132             *Ccb = NULL;
2133         }
2134     }
2135
2136     return ntStatus;
2137 }
2138
2139 NTSTATUS
2140 AFSOpenTargetDirectory( IN PIRP Irp,
2141                         IN AFSVolumeCB *VolumeCB,
2142                         IN AFSDirectoryCB *ParentDirectoryCB,
2143                         IN AFSDirectoryCB *TargetDirectoryCB,
2144                         IN UNICODE_STRING *TargetName,
2145                         OUT AFSFcb **Fcb,
2146                         OUT AFSCcb **Ccb)
2147 {
2148     UNREFERENCED_PARAMETER(VolumeCB);
2149     NTSTATUS ntStatus = STATUS_SUCCESS;
2150     PFILE_OBJECT pFileObject = NULL;
2151     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2152     PACCESS_MASK pDesiredAccess = NULL;
2153     USHORT usShareAccess;
2154     BOOLEAN bAllocatedCcb = FALSE;
2155     BOOLEAN bReleaseFcb = FALSE;
2156     AFSObjectInfoCB *pParentObject = NULL;
2157     UNICODE_STRING uniTargetName;
2158     LONG lCount;
2159
2160     __Enter
2161     {
2162
2163         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2164         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2165
2166         pFileObject = pIrpSp->FileObject;
2167
2168         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2169                       AFS_TRACE_LEVEL_VERBOSE,
2170                       "AFSOpenTargetDirectory (%p) Processing file %wZ\n",
2171                       Irp,
2172                       TargetName);
2173
2174         pParentObject = ParentDirectoryCB->ObjectInformation;
2175
2176         if( pParentObject->FileType != AFS_FILE_TYPE_DIRECTORY)
2177         {
2178
2179             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2180         }
2181
2182         //
2183         // Make sure we have an Fcb for the access
2184
2185         //
2186         // Allocate and initialize the Fcb for the file.
2187         //
2188
2189         ntStatus = AFSInitFcb( ParentDirectoryCB);
2190
2191         *Fcb = pParentObject->Fcb;
2192
2193         if( !NT_SUCCESS( ntStatus))
2194         {
2195
2196             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2197                           AFS_TRACE_LEVEL_ERROR,
2198                           "AFSOpenTargetDirectory (%p) Failed to initialize fcb %wZ Status %08lX\n",
2199                           Irp,
2200                           &ParentDirectoryCB->NameInformation.FileName,
2201                           ntStatus);
2202
2203             try_return( ntStatus);
2204         }
2205
2206         ntStatus = STATUS_SUCCESS;
2207
2208         //
2209         // Increment the open count on this Fcb
2210         //
2211
2212         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenReferenceCount);
2213
2214         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2215                       AFS_TRACE_LEVEL_VERBOSE,
2216                       "AFSOpenTargetDirectory Increment count on Fcb %p Cnt %d\n",
2217                       pParentObject->Fcb,
2218                       lCount);
2219
2220         bReleaseFcb = TRUE;
2221
2222         //
2223         // If there are current opens on the Fcb, check the access.
2224         //
2225
2226         if( pParentObject->Fcb->OpenHandleCount > 0)
2227         {
2228
2229             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2230                                            usShareAccess,
2231                                            pFileObject,
2232                                            &pParentObject->Fcb->ShareAccess,
2233                                            FALSE);
2234
2235             if( !NT_SUCCESS( ntStatus))
2236             {
2237
2238                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2239                               AFS_TRACE_LEVEL_ERROR,
2240                               "AFSOpenTargetDirectory (%p) Access check failure %wZ Status %08lX\n",
2241                               Irp,
2242                               &ParentDirectoryCB->NameInformation.FileName,
2243                               ntStatus);
2244
2245                 try_return( ntStatus);
2246             }
2247         }
2248
2249         //
2250         // Initialize the Ccb for the file.
2251         //
2252
2253         ntStatus = AFSInitCcb( Ccb,
2254                                ParentDirectoryCB,
2255                                *pDesiredAccess,
2256                                0);
2257
2258         if( !NT_SUCCESS( ntStatus))
2259         {
2260
2261             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2262                           AFS_TRACE_LEVEL_ERROR,
2263                           "AFSOpenTargetDirectory (%p) Failed to initialize ccb %wZ Status %08lX\n",
2264                           Irp,
2265                           &ParentDirectoryCB->NameInformation.FileName,
2266                           ntStatus);
2267
2268             try_return( ntStatus);
2269         }
2270
2271         bAllocatedCcb = TRUE;
2272
2273         if( TargetDirectoryCB != NULL &&
2274             FsRtlAreNamesEqual( &TargetDirectoryCB->NameInformation.FileName,
2275                                 TargetName,
2276                                 FALSE,
2277                                 NULL))
2278         {
2279
2280             Irp->IoStatus.Information = FILE_EXISTS;
2281
2282             uniTargetName = TargetDirectoryCB->NameInformation.FileName;
2283         }
2284         else
2285         {
2286
2287             Irp->IoStatus.Information = FILE_DOES_NOT_EXIST;
2288
2289             uniTargetName = *TargetName;
2290         }
2291
2292         //
2293         // Update the filename in the fileobject for rename processing
2294         //
2295
2296         RtlCopyMemory( pFileObject->FileName.Buffer,
2297                        uniTargetName.Buffer,
2298                        uniTargetName.Length);
2299
2300         pFileObject->FileName.Length = uniTargetName.Length;
2301
2302         //
2303         // OK, update the share access on the fileobject
2304         //
2305
2306         if( pParentObject->Fcb->OpenHandleCount > 0)
2307         {
2308
2309             IoUpdateShareAccess( pFileObject,
2310                                  &pParentObject->Fcb->ShareAccess);
2311         }
2312         else
2313         {
2314
2315             //
2316             // Set the access
2317             //
2318
2319             IoSetShareAccess( *pDesiredAccess,
2320                               usShareAccess,
2321                               pFileObject,
2322                               &pParentObject->Fcb->ShareAccess);
2323         }
2324
2325         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenHandleCount);
2326
2327         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2328                       AFS_TRACE_LEVEL_VERBOSE,
2329                       "AFSOpenTargetDirectory Increment handle count on Fcb %p Cnt %d\n",
2330                       pParentObject->Fcb,
2331                       lCount);
2332
2333         //
2334         // Increment the open reference and handle on the parent node
2335         //
2336
2337         if( pParentObject->ParentObjectInformation != NULL)
2338         {
2339
2340             lCount = InterlockedIncrement( &pParentObject->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
2341
2342             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2343                           AFS_TRACE_LEVEL_VERBOSE,
2344                           "AFSOpenTargetDirectory Increment child open handle count on Parent object %p Cnt %d\n",
2345                           pParentObject->ParentObjectInformation,
2346                           lCount);
2347
2348             lCount = InterlockedIncrement( &pParentObject->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
2349
2350             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2351                           AFS_TRACE_LEVEL_VERBOSE,
2352                           "AFSOpenTargetDirectory Increment child open ref count on Parent object %p Cnt %d\n",
2353                           pParentObject->ParentObjectInformation,
2354                           lCount);
2355         }
2356
2357 try_exit:
2358
2359         if( bReleaseFcb)
2360         {
2361
2362             if( !NT_SUCCESS( ntStatus))
2363             {
2364                 //
2365                 // Decrement the open count on this Fcb
2366                 //
2367
2368                 lCount = InterlockedDecrement( &pParentObject->Fcb->OpenReferenceCount);
2369
2370                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2371                               AFS_TRACE_LEVEL_VERBOSE,
2372                               "AFSOpenTargetDirectory Decrement count on Fcb %p Cnt %d\n",
2373                               pParentObject->Fcb,
2374                               lCount);
2375             }
2376
2377             AFSReleaseResource( &pParentObject->Fcb->NPFcb->Resource);
2378         }
2379
2380         if( !NT_SUCCESS( ntStatus))
2381         {
2382
2383             if( bAllocatedCcb)
2384             {
2385
2386                 AFSRemoveCcb( NULL,
2387                               *Ccb);
2388             }
2389
2390             *Ccb = NULL;
2391
2392             //
2393             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2394             //
2395
2396             *Fcb = NULL;
2397         }
2398     }
2399
2400     return ntStatus;
2401 }
2402
2403 NTSTATUS
2404 AFSProcessOpen( IN PIRP Irp,
2405                 IN GUID *AuthGroup,
2406                 IN AFSVolumeCB *VolumeCB,
2407                 IN AFSDirectoryCB *ParentDirCB,
2408                 IN AFSDirectoryCB *DirectoryCB,
2409                 OUT AFSFcb **Fcb,
2410                 OUT AFSCcb **Ccb)
2411 {
2412     UNREFERENCED_PARAMETER(VolumeCB);
2413     NTSTATUS ntStatus = STATUS_SUCCESS;
2414     PFILE_OBJECT pFileObject = NULL;
2415     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2416     PACCESS_MASK pDesiredAccess = NULL;
2417     USHORT usShareAccess;
2418     BOOLEAN bAllocatedCcb = FALSE, bReleaseFcb = FALSE;
2419     ULONG ulOptions = 0;
2420     AFSFileOpenCB   stOpenCB;
2421     AFSFileOpenResultCB stOpenResultCB;
2422     ULONG       ulResultLen = 0;
2423     AFSObjectInfoCB *pParentObjectInfo = NULL;
2424     AFSObjectInfoCB *pObjectInfo = NULL;
2425     ULONG       ulFileAccess = 0;
2426     AFSFileAccessReleaseCB stReleaseFileAccess;
2427     LONG lCount;
2428
2429     __Enter
2430     {
2431
2432         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2433         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2434
2435         pFileObject = pIrpSp->FileObject;
2436
2437         pParentObjectInfo = ParentDirCB->ObjectInformation;
2438
2439         pObjectInfo = DirectoryCB->ObjectInformation;
2440
2441         //
2442         // Check if the entry is pending a deletion
2443         //
2444
2445         if( BooleanFlagOn( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE))
2446         {
2447
2448             ntStatus = STATUS_DELETE_PENDING;
2449
2450             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2451                           AFS_TRACE_LEVEL_ERROR,
2452                           "AFSProcessOpen (%p) Entry pending delete %wZ Status %08lX\n",
2453                           Irp,
2454                           &DirectoryCB->NameInformation.FileName,
2455                           ntStatus);
2456
2457             try_return( ntStatus);
2458         }
2459
2460         //
2461         // Extract out the options
2462         //
2463
2464         ulOptions = pIrpSp->Parameters.Create.Options;
2465
2466         //
2467         // Check if we should go and retrieve updated information for the node
2468         //
2469
2470         ntStatus = AFSValidateEntry( DirectoryCB,
2471                                      AuthGroup,
2472                                      FALSE,
2473                                      TRUE);
2474
2475         if( !NT_SUCCESS( ntStatus))
2476         {
2477
2478             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2479                           AFS_TRACE_LEVEL_ERROR,
2480                           "AFSProcessOpen (%p) Failed to validate entry %wZ Status %08lX\n",
2481                           Irp,
2482                           &DirectoryCB->NameInformation.FileName,
2483                           ntStatus);
2484
2485             try_return( ntStatus);
2486         }
2487
2488         //
2489         // If this is marked for delete on close then be sure we can delete the entry
2490         //
2491
2492         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2493         {
2494
2495             ntStatus = AFSNotifyDelete( DirectoryCB,
2496                                         AuthGroup,
2497                                         TRUE);
2498
2499             if( !NT_SUCCESS( ntStatus))
2500             {
2501
2502                 ntStatus = STATUS_CANNOT_DELETE;
2503
2504                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2505                               AFS_TRACE_LEVEL_ERROR,
2506                               "AFSProcessOpen (%p) Cannot delete entry %wZ marked for delete on close Status %08lX\n",
2507                               Irp,
2508                               &DirectoryCB->NameInformation.FileName,
2509                               ntStatus);
2510
2511                 try_return( ntStatus);
2512             }
2513         }
2514
2515         //
2516         // Be sure we have an Fcb for the current object
2517         //
2518
2519         ntStatus = AFSInitFcb( DirectoryCB);
2520
2521         if( !NT_SUCCESS( ntStatus))
2522         {
2523
2524             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2525                           AFS_TRACE_LEVEL_ERROR,
2526                           "AFSProcessOpen (%p) Failed to init fcb on %wZ Status %08lX\n",
2527                           Irp,
2528                           &DirectoryCB->NameInformation.FileName,
2529                           ntStatus);
2530
2531             try_return( ntStatus);
2532         }
2533
2534         ntStatus = STATUS_SUCCESS;
2535
2536         //
2537         // AFSInitFcb returns the Fcb resource held
2538         //
2539
2540         bReleaseFcb = TRUE;
2541
2542         //
2543         // Increment the open count on this Fcb
2544         //
2545
2546         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
2547
2548         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2549                       AFS_TRACE_LEVEL_VERBOSE,
2550                       "AFSProcessOpen Increment2 count on Fcb %p Cnt %d\n",
2551                       pObjectInfo->Fcb,
2552                       lCount);
2553
2554         //
2555         // Check access on the entry
2556         //
2557
2558         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2559         {
2560
2561             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2562                                            usShareAccess,
2563                                            pFileObject,
2564                                            &pObjectInfo->Fcb->ShareAccess,
2565                                            FALSE);
2566
2567             if( !NT_SUCCESS( ntStatus))
2568             {
2569
2570                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2571                               AFS_TRACE_LEVEL_ERROR,
2572                               "AFSProcessOpen (%p) Failed to check share access on %wZ Status %08lX\n",
2573                               Irp,
2574                               &DirectoryCB->NameInformation.FileName,
2575                               ntStatus);
2576
2577                 try_return( ntStatus);
2578             }
2579         }
2580
2581         //
2582         // Additional checks
2583         //
2584
2585         if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_FILE_FCB)
2586         {
2587
2588             //
2589             // If the caller is asking for write access then try to flush the image section
2590             //
2591
2592             if( FlagOn( *pDesiredAccess, FILE_WRITE_DATA) ||
2593                 BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE))
2594             {
2595
2596                 BOOLEAN bMmFlushed;
2597
2598                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2599                               AFS_TRACE_LEVEL_VERBOSE,
2600                               "AFSProcessOpen Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
2601                               &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2602                               PsGetCurrentThread());
2603
2604                 AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2605                                 TRUE);
2606
2607                 bMmFlushed = MmFlushImageSection( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2608                                                   MmFlushForWrite);
2609
2610                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2611                               AFS_TRACE_LEVEL_VERBOSE,
2612                               "AFSProcessOpen Releasing Fcb SectionObject lock %p EXCL %08lX\n",
2613                               &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2614                               PsGetCurrentThread());
2615
2616                 AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
2617
2618                 if ( !bMmFlushed)
2619                 {
2620
2621                     ntStatus = BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE) ? STATUS_CANNOT_DELETE :
2622                                                                             STATUS_SHARING_VIOLATION;
2623
2624                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2625                                   AFS_TRACE_LEVEL_ERROR,
2626                                   "AFSProcessOpen (%p) Failed to flush image section %wZ Status %08lX\n",
2627                                   Irp,
2628                                   &DirectoryCB->NameInformation.FileName,
2629                                   ntStatus);
2630
2631                     try_return( ntStatus);
2632                 }
2633             }
2634
2635             if( BooleanFlagOn( ulOptions, FILE_DIRECTORY_FILE))
2636             {
2637
2638                 ntStatus = STATUS_NOT_A_DIRECTORY;
2639
2640                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2641                               AFS_TRACE_LEVEL_ERROR,
2642                               "AFSProcessOpen (%p) Attempt to open file as directory %wZ Status %08lX\n",
2643                               Irp,
2644                               &DirectoryCB->NameInformation.FileName,
2645                               ntStatus);
2646
2647                 try_return( ntStatus);
2648             }
2649
2650             pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
2651         }
2652         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB ||
2653                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2654         {
2655
2656             if( BooleanFlagOn( ulOptions, FILE_NON_DIRECTORY_FILE))
2657             {
2658
2659                 ntStatus = STATUS_FILE_IS_A_DIRECTORY;
2660
2661                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2662                               AFS_TRACE_LEVEL_ERROR,
2663                               "AFSProcessOpen (%p) Attempt to open directory as file %wZ Status %08lX\n",
2664                               Irp,
2665                               &DirectoryCB->NameInformation.FileName,
2666                               ntStatus);
2667
2668                 try_return( ntStatus);
2669             }
2670         }
2671         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
2672                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
2673                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
2674                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_INVALID_FCB)
2675         {
2676
2677         }
2678         else
2679         {
2680             ASSERT( FALSE);
2681             try_return( ntStatus = STATUS_UNSUCCESSFUL);
2682         }
2683
2684         //
2685         // Check with the service that we can open the file
2686         //
2687
2688         stOpenCB.ParentId = pParentObjectInfo->FileId;
2689
2690         stOpenCB.DesiredAccess = *pDesiredAccess;
2691
2692         stOpenCB.ShareAccess = usShareAccess;
2693
2694         stOpenCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
2695
2696         stOpenCB.Identifier = (ULONGLONG)pFileObject;
2697
2698         stOpenResultCB.GrantedAccess = 0;
2699
2700         ulResultLen = sizeof( AFSFileOpenResultCB);
2701
2702         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_OPEN_FILE,
2703                                       AFS_REQUEST_FLAG_SYNCHRONOUS | AFS_REQUEST_FLAG_HOLD_FID,
2704                                       AuthGroup,
2705                                       &DirectoryCB->NameInformation.FileName,
2706                                       &pObjectInfo->FileId,
2707                                       (void *)&stOpenCB,
2708                                       sizeof( AFSFileOpenCB),
2709                                       (void *)&stOpenResultCB,
2710                                       &ulResultLen);
2711
2712         if( !NT_SUCCESS( ntStatus))
2713         {
2714
2715             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2716                           AFS_TRACE_LEVEL_ERROR,
2717                           "AFSProcessOpen (%p) Failed open in service %wZ Status %08lX\n",
2718                           Irp,
2719                           &DirectoryCB->NameInformation.FileName,
2720                           ntStatus);
2721
2722             try_return( ntStatus);
2723         }
2724
2725         //
2726         // Save the granted access in case we need to release it below
2727         //
2728
2729         ulFileAccess = stOpenResultCB.FileAccess;
2730
2731         //
2732         // Check if there is a conflict
2733         //
2734
2735         if( !AFSCheckAccess( *pDesiredAccess,
2736                              stOpenResultCB.GrantedAccess,
2737                              BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_DIRECTORY)))
2738         {
2739
2740             ntStatus = STATUS_ACCESS_DENIED;
2741
2742             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2743                           AFS_TRACE_LEVEL_ERROR,
2744                           "AFSProcessOpen (%p) Failed to check access from service Desired %08lX Granted %08lX Entry %wZ Status %08lX\n",
2745                           Irp,
2746                           *pDesiredAccess,
2747                           stOpenResultCB.GrantedAccess,
2748                           &DirectoryCB->NameInformation.FileName,
2749                           ntStatus);
2750
2751             try_return( ntStatus);
2752         }
2753
2754         //
2755         // Initialize the Ccb for the file.
2756         //
2757
2758         ntStatus = AFSInitCcb( Ccb,
2759                                DirectoryCB,
2760                                *pDesiredAccess,
2761                                ulFileAccess);
2762
2763         if( !NT_SUCCESS( ntStatus))
2764         {
2765
2766             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2767                           AFS_TRACE_LEVEL_ERROR,
2768                           "AFSProcessOpen (%p) Failed to initialize ccb %wZ Status %08lX\n",
2769                           Irp,
2770                           &DirectoryCB->NameInformation.FileName,
2771                           ntStatus);
2772
2773             try_return( ntStatus);
2774         }
2775
2776         bAllocatedCcb = TRUE;
2777
2778         //
2779         // Perform the access check on the target if this is a mount point or symlink
2780         //
2781
2782         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2783         {
2784
2785             IoUpdateShareAccess( pFileObject,
2786                                  &pObjectInfo->Fcb->ShareAccess);
2787         }
2788         else
2789         {
2790
2791             //
2792             // Set the access
2793             //
2794
2795             IoSetShareAccess( *pDesiredAccess,
2796                               usShareAccess,
2797                               pFileObject,
2798                               &pObjectInfo->Fcb->ShareAccess);
2799         }
2800
2801         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenHandleCount);
2802
2803         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2804                       AFS_TRACE_LEVEL_VERBOSE,
2805                       "AFSProcessOpen Increment handle count on Fcb %p Cnt %d\n",
2806                       pObjectInfo->Fcb,
2807                       lCount);
2808
2809         //
2810         // Increment the open reference and handle on the parent node
2811         //
2812
2813         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
2814
2815         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2816                       AFS_TRACE_LEVEL_VERBOSE,
2817                       "AFSProcessOpen Increment child open handle count on Parent object %p Cnt %d\n",
2818                       pObjectInfo->ParentObjectInformation,
2819                       lCount);
2820
2821         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
2822
2823         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2824                       AFS_TRACE_LEVEL_VERBOSE,
2825                       "AFSProcessOpen Increment child open ref count on Parent object %p Cnt %d\n",
2826                       pObjectInfo->ParentObjectInformation,
2827                       lCount);
2828
2829         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2830         {
2831
2832             //
2833             // Mark it for delete on close
2834             //
2835
2836             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2837                           AFS_TRACE_LEVEL_VERBOSE,
2838                           "AFSProcessOpen (%p) Setting PENDING_DELETE flag in DirEntry %p Name %wZ\n",
2839                           Irp,
2840                           DirectoryCB,
2841                           &DirectoryCB->NameInformation.FileName);
2842
2843             SetFlag( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2844         }
2845
2846         //
2847         // Indicate the object is held
2848         //
2849
2850         SetFlag( pObjectInfo->Flags, AFS_OBJECT_HELD_IN_SERVICE);
2851
2852         //
2853         // Return the open result for this file
2854         //
2855
2856         Irp->IoStatus.Information = FILE_OPENED;
2857
2858         *Fcb = pObjectInfo->Fcb;
2859
2860 try_exit:
2861
2862         if( bReleaseFcb)
2863         {
2864
2865             if( !NT_SUCCESS( ntStatus))
2866             {
2867                 //
2868                 // Decrement the open count on this Fcb
2869                 //
2870
2871                 lCount = InterlockedDecrement( &pObjectInfo->Fcb->OpenReferenceCount);
2872
2873                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2874                               AFS_TRACE_LEVEL_VERBOSE,
2875                               "AFSProcessOpen Decrement2 count on Fcb %p Cnt %d\n",
2876                               pObjectInfo->Fcb,
2877                               lCount);
2878             }
2879
2880             AFSReleaseResource( pObjectInfo->Fcb->Header.Resource);
2881         }
2882
2883         if( !NT_SUCCESS( ntStatus))
2884         {
2885
2886             if ( ulFileAccess > 0)
2887             {
2888
2889                 stReleaseFileAccess.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
2890
2891                 stReleaseFileAccess.FileAccess = ulFileAccess;
2892
2893                 stReleaseFileAccess.Identifier = (ULONGLONG)pFileObject;
2894
2895                 AFSProcessRequest( AFS_REQUEST_TYPE_RELEASE_FILE_ACCESS,
2896                                    AFS_REQUEST_FLAG_SYNCHRONOUS,
2897                                    AuthGroup,
2898                                    &DirectoryCB->NameInformation.FileName,
2899                                    &pObjectInfo->FileId,
2900                                    (void *)&stReleaseFileAccess,
2901                                    sizeof( AFSFileAccessReleaseCB),
2902                                    NULL,
2903                                    NULL);
2904             }
2905
2906             if( bAllocatedCcb)
2907             {
2908
2909                 AFSRemoveCcb( NULL,
2910                               *Ccb);
2911             }
2912
2913             *Ccb = NULL;
2914
2915             //
2916             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2917             //
2918
2919             *Fcb = NULL;
2920         }
2921     }
2922
2923     return ntStatus;
2924 }
2925
2926 NTSTATUS
2927 AFSProcessOverwriteSupersede( IN PDEVICE_OBJECT DeviceObject,
2928                               IN PIRP           Irp,
2929                               IN AFSVolumeCB   *VolumeCB,
2930                               IN GUID          *AuthGroup,
2931                               IN AFSDirectoryCB *ParentDirCB,
2932                               IN AFSDirectoryCB *DirectoryCB,
2933                               OUT AFSFcb       **Fcb,
2934                               OUT AFSCcb       **Ccb)
2935 {
2936     UNREFERENCED_PARAMETER(DeviceObject);
2937     NTSTATUS ntStatus = STATUS_SUCCESS;
2938     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2939     PFILE_OBJECT pFileObject = NULL;
2940     LARGE_INTEGER liZero = {0,0};
2941     BOOLEAN bReleasePaging = FALSE, bReleaseFcb = FALSE;
2942     ULONG   ulAttributes = 0;
2943     ULONG ulCreateDisposition = 0;
2944     BOOLEAN bAllocatedCcb = FALSE;
2945     BOOLEAN bUserMapped = FALSE;
2946     PACCESS_MASK pDesiredAccess = NULL;
2947     USHORT usShareAccess;
2948     AFSObjectInfoCB *pParentObjectInfo = NULL;
2949     AFSObjectInfoCB *pObjectInfo = NULL;
2950     LONG lCount;
2951     LARGE_INTEGER liSaveSize;
2952     LARGE_INTEGER liSaveVDL;
2953     LARGE_INTEGER liSaveAlloc;
2954
2955     __Enter
2956     {
2957
2958         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2959
2960         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2961
2962         pFileObject = pIrpSp->FileObject;
2963
2964         ulAttributes = pIrpSp->Parameters.Create.FileAttributes;
2965
2966         ulCreateDisposition = (pIrpSp->Parameters.Create.Options >> 24) & 0x000000ff;
2967
2968         if( BooleanFlagOn( VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
2969         {
2970
2971             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2972                           AFS_TRACE_LEVEL_ERROR,
2973                           "AFSProcessOverwriteSupersede Request failed on %wZ due to read only volume\n",
2974                           Irp,
2975                           &DirectoryCB->NameInformation.FileName);
2976
2977             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
2978         }
2979
2980         pParentObjectInfo = ParentDirCB->ObjectInformation;
2981
2982         pObjectInfo = DirectoryCB->ObjectInformation;
2983
2984         //
2985         // Check if we should go and retrieve updated information for the node
2986         //
2987
2988         ntStatus = AFSValidateEntry( DirectoryCB,
2989                                      AuthGroup,
2990                                      FALSE,
2991                                      TRUE);
2992
2993         if( !NT_SUCCESS( ntStatus))
2994         {
2995
2996             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2997                           AFS_TRACE_LEVEL_ERROR,
2998                           "AFSProcessOverwriteSupersede (%p) Failed to validate entry %wZ Status %08lX\n",
2999                           Irp,
3000                           &DirectoryCB->NameInformation.FileName,
3001                           ntStatus);
3002
3003             try_return( ntStatus);
3004         }
3005
3006         //
3007         // Be sure we have an Fcb for the object block
3008         //
3009
3010         ntStatus = AFSInitFcb( DirectoryCB);
3011
3012         *Fcb = pObjectInfo->Fcb;
3013
3014         if( !NT_SUCCESS( ntStatus))
3015         {
3016
3017             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3018                           AFS_TRACE_LEVEL_ERROR,
3019                           "AFSProcessOverwriteSupersede (%p) Failed to initialize fcb %wZ Status %08lX\n",
3020                           Irp,
3021                           &DirectoryCB->NameInformation.FileName,
3022                           ntStatus);
3023
3024             try_return( ntStatus);
3025         }
3026
3027         ntStatus = STATUS_SUCCESS;
3028
3029         //
3030         // Increment the open count on this Fcb.
3031         //
3032
3033         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
3034
3035         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3036                       AFS_TRACE_LEVEL_VERBOSE,
3037                       "AFSProcessOverwriteSupersede Increment2 count on Fcb %p Cnt %d\n",
3038                       pObjectInfo->Fcb,
3039                       lCount);
3040
3041         bReleaseFcb = TRUE;
3042
3043         //
3044         // Check access on the entry
3045         //
3046
3047         if( pObjectInfo->Fcb->OpenHandleCount > 0)
3048         {
3049
3050             ntStatus = IoCheckShareAccess( *pDesiredAccess,
3051                                            usShareAccess,
3052                                            pFileObject,
3053                                            &pObjectInfo->Fcb->ShareAccess,
3054                                            FALSE);
3055
3056             if( !NT_SUCCESS( ntStatus))
3057             {
3058
3059                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3060                               AFS_TRACE_LEVEL_ERROR,
3061                               "AFSProcessOverwriteSupersede (%p) Access check failure %wZ Status %08lX\n",
3062                               Irp,
3063                               &DirectoryCB->NameInformation.FileName,
3064                               ntStatus);
3065
3066                 try_return( ntStatus);
3067             }
3068         }
3069
3070         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3071                       AFS_TRACE_LEVEL_VERBOSE,
3072                       "AFSProcessOverwriteSupercede Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
3073                       &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3074                       PsGetCurrentThread());
3075
3076         AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3077                         TRUE);
3078
3079         //
3080         //  Before we actually truncate, check to see if the purge
3081         //  is going to fail.
3082         //
3083
3084         bUserMapped = !MmCanFileBeTruncated( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
3085                                              &liZero);
3086
3087         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3088                       AFS_TRACE_LEVEL_VERBOSE,
3089                       "AFSProcessOverwriteSupercede Releasing Fcb SectionObject lock %p EXCL %08lX\n",
3090                       &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3091                       PsGetCurrentThread());
3092
3093         AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
3094
3095         if( bUserMapped)
3096         {
3097
3098             ntStatus = STATUS_USER_MAPPED_FILE;
3099
3100             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3101                           AFS_TRACE_LEVEL_ERROR,
3102                           "AFSProcessOverwriteSupersede (%p) File user mapped %wZ Status %08lX\n",
3103                           Irp,
3104                           &DirectoryCB->NameInformation.FileName,
3105                           ntStatus);
3106
3107             try_return( ntStatus);
3108         }
3109
3110         //
3111         // Initialize the Ccb for the file.
3112         //
3113
3114         ntStatus = AFSInitCcb( Ccb,
3115                                DirectoryCB,
3116                                *pDesiredAccess,
3117                                0);
3118
3119         if( !NT_SUCCESS( ntStatus))
3120         {
3121
3122             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3123                           AFS_TRACE_LEVEL_ERROR,
3124                           "AFSProcessOverwriteSupersede (%p) Failed to initialize ccb %wZ Status %08lX\n",
3125                           Irp,
3126                           &DirectoryCB->NameInformation.FileName,
3127                           ntStatus);
3128
3129             try_return( ntStatus);
3130         }
3131
3132         bAllocatedCcb = TRUE;
3133
3134         //
3135         // Set the file length to zero
3136         //
3137
3138         AFSAcquireExcl( pObjectInfo->Fcb->Header.PagingIoResource,
3139                         TRUE);
3140
3141         bReleasePaging = TRUE;
3142
3143         liSaveSize = pObjectInfo->Fcb->Header.FileSize;
3144         liSaveAlloc = pObjectInfo->Fcb->Header.AllocationSize;
3145         liSaveVDL = pObjectInfo->Fcb->Header.ValidDataLength;
3146
3147         pObjectInfo->Fcb->Header.FileSize.QuadPart = 0;
3148         pObjectInfo->Fcb->Header.ValidDataLength.QuadPart = 0;
3149         pObjectInfo->Fcb->Header.AllocationSize.QuadPart = 0;
3150
3151         pObjectInfo->EndOfFile.QuadPart = 0;
3152         pObjectInfo->AllocationSize.QuadPart = 0;
3153