b860829fc8e1c0a27806d27a4473764275459982
[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                 AFSRemoveFcb( &pObjectInfo->Fcb);
2229             }
2230
2231             *Fcb = NULL;
2232
2233             *Ccb = NULL;
2234         }
2235     }
2236
2237     return ntStatus;
2238 }
2239
2240 NTSTATUS
2241 AFSOpenTargetDirectory( IN PIRP Irp,
2242                         IN AFSVolumeCB *VolumeCB,
2243                         IN AFSDirectoryCB *ParentDirectoryCB,
2244                         IN AFSDirectoryCB *TargetDirectoryCB,
2245                         IN UNICODE_STRING *TargetName,
2246                         OUT AFSFcb **Fcb,
2247                         OUT AFSCcb **Ccb)
2248 {
2249
2250     NTSTATUS ntStatus = STATUS_SUCCESS;
2251     PFILE_OBJECT pFileObject = NULL;
2252     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2253     PACCESS_MASK pDesiredAccess = NULL;
2254     USHORT usShareAccess;
2255     BOOLEAN bAllocatedCcb = FALSE;
2256     BOOLEAN bReleaseFcb = FALSE, bAllocatedFcb = FALSE;
2257     AFSObjectInfoCB *pParentObject = NULL, *pTargetObject = NULL;
2258     UNICODE_STRING uniTargetName;
2259     LONG lCount;
2260
2261     __Enter
2262     {
2263
2264         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2265         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2266
2267         pFileObject = pIrpSp->FileObject;
2268
2269         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2270                       AFS_TRACE_LEVEL_VERBOSE,
2271                       "AFSOpenTargetDirectory (%08lX) Processing file %wZ\n",
2272                       Irp,
2273                       TargetName);
2274
2275         pParentObject = ParentDirectoryCB->ObjectInformation;
2276
2277         if( pParentObject->FileType != AFS_FILE_TYPE_DIRECTORY)
2278         {
2279
2280             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2281         }
2282
2283         //
2284         // Make sure we have an Fcb for the access
2285         //
2286
2287         if( pParentObject->Fcb != NULL)
2288         {
2289
2290             *Fcb = pParentObject->Fcb;
2291
2292             AFSAcquireExcl( &(*Fcb)->NPFcb->Resource,
2293                             TRUE);
2294         }
2295         else
2296         {
2297
2298             //
2299             // Allocate and initialize the Fcb for the file.
2300             //
2301
2302             ntStatus = AFSInitFcb( ParentDirectoryCB);
2303
2304             *Fcb = pParentObject->Fcb;
2305
2306             if( !NT_SUCCESS( ntStatus))
2307             {
2308
2309                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2310                               AFS_TRACE_LEVEL_ERROR,
2311                               "AFSOpenTargetDirectory (%08lX) Failed to initialize fcb %wZ Status %08lX\n",
2312                               Irp,
2313                               &ParentDirectoryCB->NameInformation.FileName,
2314                               ntStatus);
2315
2316                 try_return( ntStatus);
2317             }
2318
2319             if ( ntStatus != STATUS_REPARSE)
2320             {
2321
2322                 bAllocatedFcb = TRUE;
2323             }
2324
2325             ntStatus = STATUS_SUCCESS;
2326         }
2327
2328         //
2329         // Increment the open count on this Fcb
2330         //
2331
2332         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenReferenceCount);
2333
2334         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2335                       AFS_TRACE_LEVEL_VERBOSE,
2336                       "AFSOpenTargetDirectory Increment count on Fcb %08lX Cnt %d\n",
2337                       pParentObject->Fcb,
2338                       lCount);
2339
2340         bReleaseFcb = TRUE;
2341
2342         //
2343         // If there are current opens on the Fcb, check the access.
2344         //
2345
2346         if( pParentObject->Fcb->OpenHandleCount > 0)
2347         {
2348
2349             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2350                                            usShareAccess,
2351                                            pFileObject,
2352                                            &pParentObject->Fcb->ShareAccess,
2353                                            FALSE);
2354
2355             if( !NT_SUCCESS( ntStatus))
2356             {
2357
2358                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2359                               AFS_TRACE_LEVEL_ERROR,
2360                               "AFSOpenTargetDirectory (%08lX) Access check failure %wZ Status %08lX\n",
2361                               Irp,
2362                               &ParentDirectoryCB->NameInformation.FileName,
2363                               ntStatus);
2364
2365                 try_return( ntStatus);
2366             }
2367         }
2368
2369         //
2370         // Initialize the Ccb for the file.
2371         //
2372
2373         ntStatus = AFSInitCcb( Ccb);
2374
2375         if( !NT_SUCCESS( ntStatus))
2376         {
2377
2378             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2379                           AFS_TRACE_LEVEL_ERROR,
2380                           "AFSOpenTargetDirectory (%08lX) Failed to initialize ccb %wZ Status %08lX\n",
2381                           Irp,
2382                           &ParentDirectoryCB->NameInformation.FileName,
2383                           ntStatus);
2384
2385             try_return( ntStatus);
2386         }
2387
2388         bAllocatedCcb = TRUE;
2389
2390         //
2391         // Initialize the Ccb
2392         //
2393
2394         (*Ccb)->DirectoryCB = ParentDirectoryCB;
2395
2396         (*Ccb)->GrantedAccess = *pDesiredAccess;
2397
2398         if( TargetDirectoryCB != NULL &&
2399             FsRtlAreNamesEqual( &TargetDirectoryCB->NameInformation.FileName,
2400                                 TargetName,
2401                                 FALSE,
2402                                 NULL))
2403         {
2404
2405             Irp->IoStatus.Information = FILE_EXISTS;
2406
2407             uniTargetName = TargetDirectoryCB->NameInformation.FileName;
2408         }
2409         else
2410         {
2411
2412             Irp->IoStatus.Information = FILE_DOES_NOT_EXIST;
2413
2414             uniTargetName = *TargetName;
2415         }
2416
2417         //
2418         // Update the filename in the fileobject for rename processing
2419         //
2420
2421         RtlCopyMemory( pFileObject->FileName.Buffer,
2422                        uniTargetName.Buffer,
2423                        uniTargetName.Length);
2424
2425         pFileObject->FileName.Length = uniTargetName.Length;
2426
2427         //
2428         // OK, update the share access on the fileobject
2429         //
2430
2431         if( pParentObject->Fcb->OpenHandleCount > 0)
2432         {
2433
2434             IoUpdateShareAccess( pFileObject,
2435                                  &pParentObject->Fcb->ShareAccess);
2436         }
2437         else
2438         {
2439
2440             //
2441             // Set the access
2442             //
2443
2444             IoSetShareAccess( *pDesiredAccess,
2445                               usShareAccess,
2446                               pFileObject,
2447                               &pParentObject->Fcb->ShareAccess);
2448         }
2449
2450         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenHandleCount);
2451
2452         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2453                       AFS_TRACE_LEVEL_VERBOSE,
2454                       "AFSOpenTargetDirectory Increment handle count on Fcb %08lX Cnt %d\n",
2455                       pParentObject->Fcb,
2456                       lCount);
2457
2458         //
2459         // Increment the open reference and handle on the parent node
2460         //
2461
2462         if( pParentObject->ParentObjectInformation != NULL)
2463         {
2464
2465             lCount = InterlockedIncrement( &pParentObject->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
2466
2467             AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2468                           AFS_TRACE_LEVEL_VERBOSE,
2469                           "AFSOpenTargetDirectory Increment child open handle count on Parent object %08lX Cnt %d\n",
2470                           pParentObject->ParentObjectInformation,
2471                           lCount);
2472
2473             lCount = InterlockedIncrement( &pParentObject->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
2474
2475             AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2476                           AFS_TRACE_LEVEL_VERBOSE,
2477                           "AFSOpenTargetDirectory Increment child open ref count on Parent object %08lX Cnt %d\n",
2478                           pParentObject->ParentObjectInformation,
2479                           lCount);
2480         }
2481
2482 try_exit:
2483
2484         if( bReleaseFcb)
2485         {
2486
2487             if( !NT_SUCCESS( ntStatus))
2488             {
2489                 //
2490                 // Decrement the open count on this Fcb
2491                 //
2492
2493                 lCount = InterlockedDecrement( &pParentObject->Fcb->OpenReferenceCount);
2494
2495                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2496                               AFS_TRACE_LEVEL_VERBOSE,
2497                               "AFSOpenTargetDirectory Decrement count on Fcb %08lX Cnt %d\n",
2498                               pParentObject->Fcb,
2499                               lCount);
2500             }
2501
2502             AFSReleaseResource( &pParentObject->Fcb->NPFcb->Resource);
2503         }
2504
2505         if( !NT_SUCCESS( ntStatus))
2506         {
2507
2508             if( bAllocatedCcb)
2509             {
2510
2511                 AFSRemoveCcb( NULL,
2512                               *Ccb);
2513             }
2514
2515             *Ccb = NULL;
2516
2517             if( bAllocatedFcb)
2518             {
2519
2520                 AFSRemoveFcb( &pParentObject->Fcb);
2521             }
2522
2523             *Fcb = NULL;
2524         }
2525     }
2526
2527     return ntStatus;
2528 }
2529
2530 NTSTATUS
2531 AFSProcessOpen( IN PIRP Irp,
2532                 IN GUID *AuthGroup,
2533                 IN AFSVolumeCB *VolumeCB,
2534                 IN AFSDirectoryCB *ParentDirCB,
2535                 IN AFSDirectoryCB *DirectoryCB,
2536                 OUT AFSFcb **Fcb,
2537                 OUT AFSCcb **Ccb)
2538 {
2539
2540     NTSTATUS ntStatus = STATUS_SUCCESS;
2541     PFILE_OBJECT pFileObject = NULL;
2542     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2543     PACCESS_MASK pDesiredAccess = NULL;
2544     USHORT usShareAccess;
2545     BOOLEAN bAllocatedCcb = FALSE, bReleaseFcb = FALSE, bAllocatedFcb = FALSE;
2546     ULONG ulAdditionalFlags = 0, ulOptions = 0;
2547     AFSFileOpenCB   stOpenCB;
2548     AFSFileOpenResultCB stOpenResultCB;
2549     ULONG       ulResultLen = 0;
2550     AFSObjectInfoCB *pParentObjectInfo = NULL;
2551     AFSObjectInfoCB *pObjectInfo = NULL;
2552     ULONG       ulFileAccess = 0;
2553     AFSFileAccessReleaseCB stReleaseFileAccess;
2554     LONG lCount;
2555
2556     __Enter
2557     {
2558
2559         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2560         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2561
2562         pFileObject = pIrpSp->FileObject;
2563
2564         pParentObjectInfo = ParentDirCB->ObjectInformation;
2565
2566         pObjectInfo = DirectoryCB->ObjectInformation;
2567
2568         //
2569         // Check if the entry is pending a deletion
2570         //
2571
2572         if( BooleanFlagOn( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE))
2573         {
2574
2575             ntStatus = STATUS_DELETE_PENDING;
2576
2577             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2578                           AFS_TRACE_LEVEL_ERROR,
2579                           "AFSProcessOpen (%08lX) Entry pending delete %wZ Status %08lX\n",
2580                           Irp,
2581                           &DirectoryCB->NameInformation.FileName,
2582                           ntStatus);
2583
2584             try_return( ntStatus);
2585         }
2586
2587         //
2588         // Extract out the options
2589         //
2590
2591         ulOptions = pIrpSp->Parameters.Create.Options;
2592
2593         //
2594         // Check if we should go and retrieve updated information for the node
2595         //
2596
2597         ntStatus = AFSValidateEntry( DirectoryCB,
2598                                      AuthGroup,
2599                                      FALSE);
2600
2601         if( !NT_SUCCESS( ntStatus))
2602         {
2603
2604             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2605                           AFS_TRACE_LEVEL_ERROR,
2606                           "AFSProcessOpen (%08lX) Failed to validate entry %wZ Status %08lX\n",
2607                           Irp,
2608                           &DirectoryCB->NameInformation.FileName,
2609                           ntStatus);
2610
2611             try_return( ntStatus);
2612         }
2613
2614         //
2615         // If this is marked for delete on close then be sure we can delete the entry
2616         //
2617
2618         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2619         {
2620
2621             ntStatus = AFSNotifyDelete( DirectoryCB,
2622                                         AuthGroup,
2623                                         TRUE);
2624
2625             if( !NT_SUCCESS( ntStatus))
2626             {
2627
2628                 ntStatus = STATUS_CANNOT_DELETE;
2629
2630                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2631                               AFS_TRACE_LEVEL_ERROR,
2632                               "AFSProcessOpen (%08lX) Cannot delete entry %wZ marked for delete on close Status %08lX\n",
2633                               Irp,
2634                               &DirectoryCB->NameInformation.FileName,
2635                               ntStatus);
2636
2637                 try_return( ntStatus);
2638             }
2639         }
2640
2641         //
2642         // Be sure we have an Fcb for the current object
2643         //
2644
2645         ntStatus = AFSInitFcb( DirectoryCB);
2646
2647         if( !NT_SUCCESS( ntStatus))
2648         {
2649
2650             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2651                           AFS_TRACE_LEVEL_ERROR,
2652                           "AFSProcessOpen (%08lX) Failed to init fcb on %wZ Status %08lX\n",
2653                           Irp,
2654                           &DirectoryCB->NameInformation.FileName,
2655                           ntStatus);
2656
2657             try_return( ntStatus);
2658         }
2659
2660         if ( ntStatus != STATUS_REPARSE)
2661         {
2662
2663             bAllocatedFcb = TRUE;
2664         }
2665
2666         ntStatus = STATUS_SUCCESS;
2667
2668         //
2669         // AFSInitFcb returns the Fcb resource held
2670         //
2671
2672         bReleaseFcb = TRUE;
2673
2674         //
2675         // Increment the open count on this Fcb
2676         //
2677
2678         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
2679
2680         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2681                       AFS_TRACE_LEVEL_VERBOSE,
2682                       "AFSProcessOpen Increment2 count on Fcb %08lX Cnt %d\n",
2683                       pObjectInfo->Fcb,
2684                       lCount);
2685
2686         //
2687         // Check access on the entry
2688         //
2689
2690         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2691         {
2692
2693             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2694                                            usShareAccess,
2695                                            pFileObject,
2696                                            &pObjectInfo->Fcb->ShareAccess,
2697                                            FALSE);
2698
2699             if( !NT_SUCCESS( ntStatus))
2700             {
2701
2702                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2703                               AFS_TRACE_LEVEL_ERROR,
2704                               "AFSProcessOpen (%08lX) Failed to check share access on %wZ Status %08lX\n",
2705                               Irp,
2706                               &DirectoryCB->NameInformation.FileName,
2707                               ntStatus);
2708
2709                 try_return( ntStatus);
2710             }
2711         }
2712
2713         //
2714         // Additional checks
2715         //
2716
2717         if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_FILE_FCB)
2718         {
2719
2720             //
2721             // If the caller is asking for write access then try to flush the image section
2722             //
2723
2724             if( FlagOn( *pDesiredAccess, FILE_WRITE_DATA) ||
2725                 BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE))
2726             {
2727
2728                 if( !MmFlushImageSection( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2729                                           MmFlushForWrite))
2730                 {
2731
2732                     ntStatus = BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE) ? STATUS_CANNOT_DELETE :
2733                                                                             STATUS_SHARING_VIOLATION;
2734
2735                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2736                                   AFS_TRACE_LEVEL_ERROR,
2737                                   "AFSProcessOpen (%08lX) Failed to flush image section %wZ Status %08lX\n",
2738                                   Irp,
2739                                   &DirectoryCB->NameInformation.FileName,
2740                                   ntStatus);
2741
2742                     try_return( ntStatus);
2743                 }
2744             }
2745
2746             if( BooleanFlagOn( ulOptions, FILE_DIRECTORY_FILE))
2747             {
2748
2749                 ntStatus = STATUS_NOT_A_DIRECTORY;
2750
2751                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2752                               AFS_TRACE_LEVEL_ERROR,
2753                               "AFSProcessOpen (%08lX) Attempt to open file as directory %wZ Status %08lX\n",
2754                               Irp,
2755                               &DirectoryCB->NameInformation.FileName,
2756                               ntStatus);
2757
2758                 try_return( ntStatus);
2759             }
2760
2761             pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
2762         }
2763         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB ||
2764                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2765         {
2766
2767             if( BooleanFlagOn( ulOptions, FILE_NON_DIRECTORY_FILE))
2768             {
2769
2770                 ntStatus = STATUS_FILE_IS_A_DIRECTORY;
2771
2772                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2773                               AFS_TRACE_LEVEL_ERROR,
2774                               "AFSProcessOpen (%08lX) Attempt to open directory as file %wZ Status %08lX\n",
2775                               Irp,
2776                               &DirectoryCB->NameInformation.FileName,
2777                               ntStatus);
2778
2779                 try_return( ntStatus);
2780             }
2781         }
2782         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
2783                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
2784                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
2785                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_INVALID_FCB)
2786         {
2787
2788         }
2789         else
2790         {
2791             ASSERT( FALSE);
2792             try_return( ntStatus = STATUS_UNSUCCESSFUL);
2793         }
2794
2795         //
2796         // Check with the service that we can open the file
2797         //
2798
2799         stOpenCB.ParentId = pParentObjectInfo->FileId;
2800
2801         stOpenCB.DesiredAccess = *pDesiredAccess;
2802
2803         stOpenCB.ShareAccess = usShareAccess;
2804
2805         stOpenCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
2806
2807         stOpenCB.Identifier = (ULONGLONG)pFileObject;
2808
2809         stOpenResultCB.GrantedAccess = 0;
2810
2811         ulResultLen = sizeof( AFSFileOpenResultCB);
2812
2813         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_OPEN_FILE,
2814                                       AFS_REQUEST_FLAG_SYNCHRONOUS | AFS_REQUEST_FLAG_HOLD_FID,
2815                                       AuthGroup,
2816                                       &DirectoryCB->NameInformation.FileName,
2817                                       &pObjectInfo->FileId,
2818                                       (void *)&stOpenCB,
2819                                       sizeof( AFSFileOpenCB),
2820                                       (void *)&stOpenResultCB,
2821                                       &ulResultLen);
2822
2823         if( !NT_SUCCESS( ntStatus))
2824         {
2825
2826             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2827                           AFS_TRACE_LEVEL_ERROR,
2828                           "AFSProcessOpen (%08lX) Failed open in service %wZ Status %08lX\n",
2829                           Irp,
2830                           &DirectoryCB->NameInformation.FileName,
2831                           ntStatus);
2832
2833             try_return( ntStatus);
2834         }
2835
2836         //
2837         // Save the granted access in case we need to release it below
2838         //
2839
2840         ulFileAccess = stOpenResultCB.FileAccess;
2841
2842         //
2843         // Check if there is a conflict
2844         //
2845
2846         if( !AFSCheckAccess( *pDesiredAccess,
2847                              stOpenResultCB.GrantedAccess,
2848                              BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_DIRECTORY)))
2849         {
2850
2851             ntStatus = STATUS_ACCESS_DENIED;
2852
2853             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2854                           AFS_TRACE_LEVEL_ERROR,
2855                           "AFSProcessOpen (%08lX) Failed to check access from service Desired %08lX Granted %08lX Entry %wZ Status %08lX\n",
2856                           Irp,
2857                           *pDesiredAccess,
2858                           stOpenResultCB.GrantedAccess,
2859                           &DirectoryCB->NameInformation.FileName,
2860                           ntStatus);
2861
2862             try_return( ntStatus);
2863         }
2864
2865         //
2866         // Initialize the Ccb for the file.
2867         //
2868
2869         ntStatus = AFSInitCcb( Ccb);
2870
2871         if( !NT_SUCCESS( ntStatus))
2872         {
2873
2874             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2875                           AFS_TRACE_LEVEL_ERROR,
2876                           "AFSProcessOpen (%08lX) Failed to initialize ccb %wZ Status %08lX\n",
2877                           Irp,
2878                           &DirectoryCB->NameInformation.FileName,
2879                           ntStatus);
2880
2881             try_return( ntStatus);
2882         }
2883
2884         bAllocatedCcb = TRUE;
2885
2886         (*Ccb)->DirectoryCB = DirectoryCB;
2887
2888         (*Ccb)->FileAccess = ulFileAccess;
2889
2890         (*Ccb)->GrantedAccess = *pDesiredAccess;
2891
2892         //
2893         // Perform the access check on the target if this is a mount point or symlink
2894         //
2895
2896         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2897         {
2898
2899             IoUpdateShareAccess( pFileObject,
2900                                  &pObjectInfo->Fcb->ShareAccess);
2901         }
2902         else
2903         {
2904
2905             //
2906             // Set the access
2907             //
2908
2909             IoSetShareAccess( *pDesiredAccess,
2910                               usShareAccess,
2911                               pFileObject,
2912                               &pObjectInfo->Fcb->ShareAccess);
2913         }
2914
2915         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenHandleCount);
2916
2917         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2918                       AFS_TRACE_LEVEL_VERBOSE,
2919                       "AFSProcessOpen Increment handle count on Fcb %08lX Cnt %d\n",
2920                       pObjectInfo->Fcb,
2921                       lCount);
2922
2923         //
2924         // Increment the open reference and handle on the parent node
2925         //
2926
2927         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
2928
2929         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2930                       AFS_TRACE_LEVEL_VERBOSE,
2931                       "AFSProcessOpen Increment child open handle count on Parent object %08lX Cnt %d\n",
2932                       pObjectInfo->ParentObjectInformation,
2933                       lCount);
2934
2935         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
2936
2937         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2938                       AFS_TRACE_LEVEL_VERBOSE,
2939                       "AFSProcessOpen Increment child open ref count on Parent object %08lX Cnt %d\n",
2940                       pObjectInfo->ParentObjectInformation,
2941                       lCount);
2942
2943         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2944         {
2945
2946             //
2947             // Mark it for delete on close
2948             //
2949
2950             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2951                           AFS_TRACE_LEVEL_VERBOSE,
2952                           "AFSProcessOpen (%08lX) Setting PENDING_DELETE flag in DirEntry %p Name %wZ\n",
2953                           Irp,
2954                           DirectoryCB,
2955                           &DirectoryCB->NameInformation.FileName);
2956
2957             SetFlag( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2958         }
2959
2960         //
2961         // Indicate the object is held
2962         //
2963
2964         SetFlag( pObjectInfo->Flags, AFS_OBJECT_HELD_IN_SERVICE);
2965
2966         //
2967         // Return the open result for this file
2968         //
2969
2970         Irp->IoStatus.Information = FILE_OPENED;
2971
2972         *Fcb = pObjectInfo->Fcb;
2973
2974 try_exit:
2975
2976         if( bReleaseFcb)
2977         {
2978
2979             if( !NT_SUCCESS( ntStatus))
2980             {
2981                 //
2982                 // Decrement the open count on this Fcb
2983                 //
2984
2985                 lCount = InterlockedDecrement( &pObjectInfo->Fcb->OpenReferenceCount);
2986
2987                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2988                               AFS_TRACE_LEVEL_VERBOSE,
2989                               "AFSProcessOpen Decrement2 count on Fcb %08lX Cnt %d\n",
2990                               pObjectInfo->Fcb,
2991                               lCount);
2992             }
2993
2994             AFSReleaseResource( pObjectInfo->Fcb->Header.Resource);
2995         }
2996
2997         if( !NT_SUCCESS( ntStatus))
2998         {
2999
3000             if ( ulFileAccess > 0)
3001             {
3002
3003                 stReleaseFileAccess.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
3004
3005                 stReleaseFileAccess.FileAccess = ulFileAccess;
3006
3007                 stReleaseFileAccess.Identifier = (ULONGLONG)pFileObject;
3008
3009                 AFSProcessRequest( AFS_REQUEST_TYPE_RELEASE_FILE_ACCESS,
3010                                    AFS_REQUEST_FLAG_SYNCHRONOUS,
3011                                    AuthGroup,
3012                                    &DirectoryCB->NameInformation.FileName,
3013                                    &pObjectInfo->FileId,
3014                                    (void *)&stReleaseFileAccess,
3015                                    sizeof( AFSFileAccessReleaseCB),
3016                                    NULL,
3017                                    NULL);
3018             }
3019
3020             if( bAllocatedCcb)
3021             {
3022
3023                 AFSRemoveCcb( NULL,
3024                               *Ccb);
3025             }
3026
3027             *Ccb = NULL;
3028
3029             if( bAllocatedFcb)
3030             {
3031
3032                 AFSRemoveFcb( &pObjectInfo->Fcb);
3033             }
3034
3035             *Fcb = NULL;
3036         }
3037     }
3038
3039     return ntStatus;
3040 }
3041
3042 NTSTATUS
3043 AFSProcessOverwriteSupersede( IN PDEVICE_OBJECT DeviceObject,
3044                               IN PIRP           Irp,
3045                               IN AFSVolumeCB   *VolumeCB,
3046                               IN GUID          *AuthGroup,
3047                               IN AFSDirectoryCB *ParentDirCB,
3048                               IN AFSDirectoryCB *DirectoryCB,
3049                               OUT AFSFcb       **Fcb,
3050                               OUT AFSCcb       **Ccb)
3051 {
3052
3053     NTSTATUS ntStatus = STATUS_SUCCESS;
3054     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
3055     PFILE_OBJECT pFileObject = NULL;
3056     LARGE_INTEGER liZero = {0,0};
3057     BOOLEAN bReleasePaging = FALSE, bReleaseFcb = FALSE;
3058     ULONG   ulAttributes = 0;
3059     LARGE_INTEGER liTime;
3060     ULONG ulCreateDisposition = 0;
3061     BOOLEAN bAllocatedCcb = FALSE, bAllocatedFcb = FALSE;
3062     PACCESS_MASK pDesiredAccess = NULL;
3063     USHORT usShareAccess;
3064     AFSObjectInfoCB *pParentObjectInfo = NULL;
3065     AFSObjectInfoCB *pObjectInfo = NULL;
3066     LONG lCount;
3067     LARGE_INTEGER liSaveSize;
3068     LARGE_INTEGER liSaveVDL;
3069     LARGE_INTEGER liSaveAlloc;
3070
3071     __Enter
3072     {
3073
3074         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
3075
3076         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
3077
3078         pFileObject = pIrpSp->FileObject;
3079
3080         ulAttributes = pIrpSp->Parameters.Create.FileAttributes;
3081
3082         ulCreateDisposition = (pIrpSp->Parameters.Create.Options >> 24) & 0x000000ff;
3083
3084         if( BooleanFlagOn( VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
3085         {
3086
3087             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3088                           AFS_TRACE_LEVEL_ERROR,
3089                           "AFSProcessOverwriteSupersede Request failed on %wZ due to read only volume\n",
3090                           Irp,
3091                           &DirectoryCB->NameInformation.FileName);
3092
3093             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
3094         }
3095
3096         pParentObjectInfo = ParentDirCB->ObjectInformation;
3097
3098         pObjectInfo = DirectoryCB->ObjectInformation;
3099
3100         //
3101         // Check if we should go and retrieve updated information for the node
3102         //
3103
3104         ntStatus = AFSValidateEntry( DirectoryCB,
3105                                      AuthGroup,
3106                                      FALSE);
3107
3108         if( !NT_SUCCESS( ntStatus))
3109         {
3110
3111             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3112                           AFS_TRACE_LEVEL_ERROR,
3113                           "AFSProcessOverwriteSupersede (%08lX) Failed to validate entry %wZ Status %08lX\n",
3114                           Irp,
3115                           &DirectoryCB->NameInformation.FileName,
3116                           ntStatus);
3117
3118             try_return( ntStatus);
3119         }
3120
3121         //
3122         // Be sure we have an Fcb for the object block
3123         //
3124
3125         if( pObjectInfo->Fcb == NULL)
3126         {
3127
3128             ntStatus = AFSInitFcb( DirectoryCB);
3129
3130             *Fcb = pObjectInfo->Fcb;
3131
3132             if( !NT_SUCCESS( ntStatus))
3133             {
3134
3135                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3136                               AFS_TRACE_LEVEL_ERROR,
3137                               "AFSProcessOverwriteSupersede (%08lX) Failed to initialize fcb %wZ Status %08lX\n",
3138                               Irp,
3139                               &DirectoryCB->NameInformation.FileName,
3140                               ntStatus);
3141
3142                 try_return( ntStatus);
3143             }
3144
3145             if ( ntStatus != STATUS_REPARSE)
3146             {
3147
3148                 bAllocatedFcb = TRUE;
3149             }
3150
3151             ntStatus = STATUS_SUCCESS;
3152         }
3153         else
3154         {
3155
3156             AFSAcquireExcl( pObjectInfo->Fcb->Header.Resource,
3157                             TRUE);
3158         }
3159
3160         //
3161         // Increment the open count on this Fcb.
3162         //
3163
3164         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
3165
3166         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3167                       AFS_TRACE_LEVEL_VERBOSE,
3168                       "AFSProcessOverwriteSupersede Increment2 count on Fcb %08lX Cnt %d\n",
3169                       pObjectInfo->Fcb,
3170                       lCount);
3171
3172         bReleaseFcb = TRUE;
3173
3174         //
3175         // Check access on the entry
3176         //
3177
3178         if( pObjectInfo->Fcb->OpenHandleCount > 0)
3179         {
3180
3181             ntStatus = IoCheckShareAccess( *pDesiredAccess,
3182                                            usShareAccess,
3183                                            pFileObject,
3184                                            &pObjectInfo->Fcb->ShareAccess,
3185                                            FALSE);
3186
3187             if( !NT_SUCCESS( ntStatus))
3188             {
3189
3190                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3191                               AFS_TRACE_LEVEL_ERROR,
3192                               "AFSProcessOverwriteSupersede (%08lX) Access check failure %wZ Status %08lX\n",
3193                               Irp,
3194                               &DirectoryCB->NameInformation.FileName,
3195                               ntStatus);
3196
3197                 try_return( ntStatus);
3198             }
3199         }
3200
3201         //
3202         //  Before we actually truncate, check to see if the purge
3203         //  is going to fail.
3204         //
3205
3206         if( !MmCanFileBeTruncated( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
3207                                    &liZero))
3208         {
3209
3210             ntStatus = STATUS_USER_MAPPED_FILE;
3211
3212             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3213                           AFS_TRACE_LEVEL_ERROR,
3214                           "AFSProcessOverwriteSupersede (%08lX) File user mapped %wZ Status %08lX\n",
3215                           Irp,
3216                           &DirectoryCB->NameInformation.FileName,
3217                           ntStatus);
3218
3219             try_return( ntStatus);
3220         }
3221
3222         //
3223         // Initialize the Ccb for the file.
3224         //
3225
3226         ntStatus = AFSInitCcb( Ccb);
3227
3228         if( !NT_SUCCESS( ntStatus))
3229         {
3230
3231             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3232                           AFS_TRACE_LEVEL_ERROR,
3233                           "AFSProcessOverwriteSupersede (%08lX) Failed to initialize ccb %wZ Status %08lX\n",
3234                           Irp,
3235                           &DirectoryCB->NameInformation.FileName,
3236                           ntStatus);
3237
3238             try_return( ntStatus);
3239         }
3240
3241         bAllocatedCcb = TRUE;
3242
3243         //
3244         // Initialize the Ccb
3245         //
3246
3247         (*Ccb)->DirectoryCB = DirectoryCB;
3248
3249         (*Ccb)->GrantedAccess = *pDesiredAccess;
3250
3251         //
3252         // Set the file length to zero
3253         //
3254
3255         AFSAcquireExcl( pObjectInfo->Fcb->Header.PagingIoResource,
3256                         TRUE);
3257
3258         bReleasePaging = TRUE;
3259
3260         liSaveSize = pObjectInfo->Fcb->Header.FileSize;
3261         liSaveAlloc = pObjectInfo->Fcb->Header.AllocationSize;
3262         liSaveVDL = pObjectInfo->Fcb->Header.ValidDataLength;
3263
3264         pObjectInfo->Fcb->Header.FileSize.QuadPart = 0;
3265         pObjectInfo->Fcb->Header.ValidDataLength.QuadPart = 0;
3266         pObjectInfo->Fcb->Header.AllocationSize.QuadPart = 0;
3267
3268         pObjectInfo->EndOfFile.QuadPart = 0;
3269         pObjectInfo->AllocationSize.QuadPart = 0;
3270
3271         //
3272         // Trim down the extents. We do this BEFORE telling the service
3273         // the file is truncated since there is a potential race between
3274         // a worker thread releasing extents and us trimming
3275         //
3276
3277         AFSTrimExtents( pObjectInfo->Fcb,
3278                         &pObjectInfo->Fcb->Header.FileSize);
3279
3280         KeQuerySystemTime( &pObjectInfo->ChangeTime);
3281
3282         KeQuerySystemTime( &pObjectInfo->LastAccessTime);
3283
3284         KeQuerySystemTime( &pObjectInfo->LastWriteTime);
3285
3286         //
3287         // Set the update flag accordingly
3288         //
3289
3290         SetFlag( pObjectInfo->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED |
3291                                           AFS_FCB_FLAG_UPDATE_CREATE_TIME |
3292                                           AFS_FCB_FLAG_UPDATE_CHANGE_TIME |
3293                                           AFS_FCB_FLAG_UPDATE_ACCESS_TIME |
3294                                           AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME);
3295
3296         ntStatus = AFSUpdateFileInformation( &pParentObjectInfo->FileId,
3297                                              pObjectInfo,
3298                                              AuthGroup);
3299
3300         if( !NT_SUCCESS( ntStatus))
3301         {
3302
3303             pObjectInfo->Fcb->Header.ValidDataLength = liSaveVDL;
3304             pObjectInfo->Fcb->Header.FileSize = liSaveSize;
3305             pObjectInfo->Fcb->Header.AllocationSize = liSaveAlloc;
3306             pObjectInfo->Fcb->ObjectInformation->EndOfFile = liSaveSize;
3307             pObjectInfo->Fcb->ObjectInformation->AllocationSize = liSaveAlloc;
3308
3309             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3310                           AFS_TRACE_LEVEL_ERROR,
3311                           "AFSProcessOverwriteSupersede (%08lX) Failed to update file information %wZ Status %08lX\n",
3312                           Irp,
3313                           &DirectoryCB->NameInformation.FileName,
3314                           ntStatus);
3315
3316             try_return( ntStatus);
3317         }
3318
3319         ulAttributes |= FILE_ATTRIBUTE_ARCHIVE;
3320
3321         if( ulCreateDisposition == FILE_SUPERSEDE)
3322         {
3323
3324             pObjectInfo->FileAttributes = ulAttributes;
3325
3326         }
3327         else
3328         {
3329
3330             pObjectInfo->FileAttributes |= ulAttributes;
3331         }
3332
3333         //
3334         // Save off the access for the open
3335         //
3336
3337         if( pObjectInfo->Fcb->OpenHandleCount > 0)
3338         {
3339
3340             IoUpdateShareAccess( pFileObject,
3341                                  &pObjectInfo->Fcb->ShareAccess);
3342         }
3343         else
3344         {
3345
3346             //
3347             // Set the access
3348             //
3349
3350             IoSetShareAccess( *pDesiredAccess,
3351                               usShareAccess,
3352                               pFileObject,
3353                               &pObjectInfo->Fcb->ShareAccess);
3354         }
3355
3356         //
3357         // Return the correct action
3358         //
3359
3360         if( ulCreateDisposition == FILE_SUPERSEDE)
3361         {
3362
3363             Irp->IoStatus.Information = FILE_SUPERSEDED;
3364         }
3365         else
3366         {
3367
3368             Irp->IoStatus.Information = FILE_OVERWRITTEN;
3369         }
3370
3371         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenHandleCount);
3372
3373         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3374                       AFS_TRACE_LEVEL_VERBOSE,
3375                       "AFSProcessOverwriteSupersede Increment handle count on Fcb %08lX Cnt %d\n",
3376                       pObjectInfo->Fcb,
3377                       lCount);
3378
3379         //
3380         // Increment the open reference and handle on the parent node
3381         //
3382
3383         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
3384
3385         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3386                       AFS_TRACE_LEVEL_VERBOSE,
3387                       "AFSProcessOverwriteSupersede Increment child open handle count on Parent object %08lX Cnt %d\n",
3388                       pObjectInfo->ParentObjectInformation,
3389                       lCount);
3390
3391         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
3392
3393         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3394                       AFS_TRACE_LEVEL_VERBOSE,
3395                       "AFSProcessOverwriteSupersede Increment child open ref count on Parent object %08lX Cnt %d\n",
3396                       pObjectInfo->ParentObjectInformation,
3397                       lCount);
3398
3399         AFSReleaseResource( pObjectInfo->Fcb->Header.Resource);
3400
3401         bReleaseFcb = FALSE;
3402
3403         *Fcb = pObjectInfo->Fcb;
3404
3405         //
3406         // Now that the Fcb->Resource has been dropped
3407         // we can call CcSetFileSizes.  We are still holding
3408         // the PagingIoResource
3409         //
3410
3411         pFileObject->SectionObjectPointer = &pObjectInfo->Fcb->NPFcb->SectionObjectPointers;
3412
3413         pFileObject->FsContext = (void *)pObjectInfo->Fcb;
3414
3415         pFileObject->FsContext2 = (void *)*Ccb;
3416
3417         CcSetFileSizes( pFileObject,
3418                         (PCC_FILE_SIZES)&pObjectInfo->Fcb->Header.AllocationSize);
3419
3420 try_exit:
3421
3422         if( bReleasePaging)
3423         {
3424
3425             AFSReleaseResource( pObjectInfo->Fcb->Header.PagingIoResource);
3426         }
3427
3428         if( bReleaseFcb)
3429         {
3430
3431             if( !NT_SUCCESS( ntStatus))
3432             {
3433                 //
3434                 // Decrement the open count on this Fcb.
3435                 //
3436
3437                 lCount = InterlockedDecrement( &pObjectInfo->Fcb->OpenReferenceCount);
3438
3439                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3440                               AFS_TRACE_LEVEL_VERBOSE,
3441                               "AFSProcessOverwriteSupersede Decrement2 count on Fcb %08lX Cnt %d\n",
3442                               pObjectInfo->Fcb,
3443                               lCount);
3444             }
3445
3446             AFSReleaseResource( pObjectInfo->Fcb->Header.Resource);
3447         }
3448
3449         if( !NT_SUCCESS( ntStatus))
3450         {
3451
3452             if( bAllocatedCcb)
3453             {
3454
3455                 AFSRemoveCcb( NULL,
3456                               *Ccb);
3457             }
3458
3459             *Ccb = NULL;
3460
3461             if( bAllocatedFcb)
3462             {
3463
3464                 AFSRemoveFcb( &pObjectInfo->Fcb);
3465             }
3466
3467             *Fcb = NULL;
3468         }
3469     }
3470
3471     return ntStatus;
3472 }
3473
3474 NTSTATUS
3475 AFSControlDeviceCreate( IN PIRP Irp)
3476 {
3477
3478     NTSTATUS ntStatus = STATUS_SUCCESS;
3479
3480     __Enter
3481     {
3482
3483         //
3484         // For now, jsut let the open happen
3485         //
3486
3487         Irp->IoStatus.Information = FILE_OPENED;
3488     }
3489
3490     return ntStatus;
3491 }
3492
3493 NTSTATUS
3494 AFSOpenIOCtlFcb( IN PIRP Irp,
3495                  IN GUID *AuthGroup,
3496                  IN AFSDirectoryCB *ParentDirCB,
3497                  OUT AFSFcb **Fcb,
3498                  OUT AFSCcb **Ccb)
3499 {
3500
3501     NTSTATUS ntStatus = STATUS_SUCCESS;
3502     PFILE_OBJECT pFileObject = NULL;
3503     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
3504     BOOLEAN bReleaseFcb = FALSE, bAllocatedCcb = FALSE, bAllocatedFcb = FALSE;
3505     UNICODE_STRING uniFullFileName;
3506     AFSPIOCtlOpenCloseRequestCB stPIOCtlOpen;
3507     AFSFileID stFileID;
3508     AFSObjectInfoCB *pParentObjectInfo = NULL;
3509     LONG lCount;
3510
3511     __Enter
3512     {
3513
3514         pFileObject = pIrpSp->FileObject;
3515
3516         pParentObjectInfo = ParentDirCB->ObjectInformation;
3517
3518         //
3519         // If we haven't initialized the PIOCtl DirectoryCB for this directory then do it now
3520         //
3521
3522         if( pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB == NULL)
3523         {
3524
3525             ntStatus = AFSInitPIOCtlDirectoryCB( pParentObjectInfo);
3526
3527             if( !NT_SUCCESS( ntStatus))
3528             {
3529
3530                 try_return( ntStatus);
3531             }
3532         }
3533
3534         if( pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb == NULL)
3535         {
3536
3537             //
3538             // Allocate and initialize the Fcb for the file.
3539             //
3540
3541             ntStatus = AFSInitFcb( pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB);
3542
3543             *Fcb = pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb;
3544
3545             if( !NT_SUCCESS( ntStatus))
3546             {
3547
3548                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3549                               AFS_TRACE_LEVEL_ERROR,
3550                               "AFSOpenIOCtlFcb (%08lX) Failed to initialize fcb Status %08lX\n",
3551                               Irp,
3552                               ntStatus);
3553
3554                 try_return( ntStatus);
3555             }
3556
3557             if ( ntStatus != STATUS_REPARSE)
3558             {
3559
3560                 bAllocatedFcb = TRUE;
3561             }
3562
3563             ntStatus = STATUS_SUCCESS;
3564         }
3565         else
3566         {
3567
3568             *Fcb = pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb;
3569
3570             AFSAcquireExcl( &(*Fcb)->NPFcb->Resource,
3571                             TRUE);
3572         }
3573
3574         //
3575         // Increment the open reference and handle on the node
3576         //
3577
3578         lCount = InterlockedIncrement( &(*Fcb)->OpenReferenceCount);
3579
3580         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3581                       AFS_TRACE_LEVEL_VERBOSE,
3582                       "AFSOpenIOCtlFcb Increment count on Fcb %08lX Cnt %d\n",
3583                       (*Fcb),
3584                       lCount);
3585
3586         bReleaseFcb = TRUE;
3587
3588         //
3589         // Initialize the Ccb for the file.
3590         //
3591
3592         ntStatus = AFSInitCcb( Ccb);
3593
3594         if( !NT_SUCCESS( ntStatus))
3595         {
3596
3597             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3598                           AFS_TRACE_LEVEL_ERROR,
3599                           "AFSOpenIOCtlFcb (%08lX) Failed to initialize ccb Status %08lX\n",
3600                           Irp,
3601                           ntStatus);
3602
3603             try_return( ntStatus);
3604         }
3605
3606         bAllocatedCcb = TRUE;
3607
3608         //
3609         // Setup the Ccb
3610         //
3611
3612         (*Ccb)->DirectoryCB = pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB;
3613
3614         //
3615         // Set the PIOCtl index
3616         //
3617
3618         (*Ccb)->RequestID = InterlockedIncrement( &pParentObjectInfo->Specific.Directory.OpenRequestIndex);
3619
3620         RtlZeroMemory( &stPIOCtlOpen,
3621                        sizeof( AFSPIOCtlOpenCloseRequestCB));
3622
3623         stPIOCtlOpen.RequestId = (*Ccb)->RequestID;
3624
3625         stPIOCtlOpen.RootId = pParentObjectInfo->VolumeCB->ObjectInformation.FileId;
3626
3627         RtlZeroMemory( &stFileID,
3628                        sizeof( AFSFileID));
3629
3630         //
3631         // The parent directory FID of the node
3632         //
3633
3634         stFileID = pParentObjectInfo->FileId;
3635
3636         //
3637         // Issue the open request to the service
3638         //
3639
3640         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_OPEN,
3641                                       AFS_REQUEST_FLAG_SYNCHRONOUS,
3642                                       AuthGroup,
3643                                       NULL,
3644                                       &stFileID,
3645                                       (void *)&stPIOCtlOpen,
3646                                       sizeof( AFSPIOCtlOpenCloseRequestCB),
3647                                       NULL,
3648                                       NULL);
3649
3650         if( !NT_SUCCESS( ntStatus))
3651         {
3652
3653             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3654                           AFS_TRACE_LEVEL_ERROR,
3655                           "AFSOpenIOCtlFcb (%08lX) Failed service open Status %08lX\n",
3656                           Irp,
3657                           ntStatus);
3658
3659             try_return( ntStatus);
3660         }
3661