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