d6bf731be1f67db08b006fa0d0c359bb7949fd76
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSFileInfo.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: AFSFileInfo.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 //
42 // Function: AFSQueryFileInfo
43 //
44 // Description:
45 //
46 //      This function is the dispatch handler for the IRP_MJ_QUERY_FILE_INFORMATION request
47 //
48 // Return:
49 //
50 //      A status is returned for the function
51 //
52
53 NTSTATUS
54 AFSQueryFileInfo( IN PDEVICE_OBJECT LibDeviceObject,
55                   IN PIRP Irp)
56 {
57
58     UNREFERENCED_PARAMETER(LibDeviceObject);
59     NTSTATUS ntStatus = STATUS_SUCCESS;
60     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
61     AFSFcb *pFcb = NULL;
62     AFSCcb *pCcb = NULL;
63     BOOLEAN bReleaseMain = FALSE;
64     LONG lLength = 0;
65     FILE_INFORMATION_CLASS stFileInformationClass;
66     GUID stAuthGroup;
67     PVOID pBuffer;
68
69     __try
70     {
71
72         //
73         // Determine the type of request this request is
74         //
75
76         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
77
78         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
79
80         if( pFcb == NULL)
81         {
82
83             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
84                           AFS_TRACE_LEVEL_ERROR,
85                           "AFSQueryFileInfo Attempted access (%p) when pFcb == NULL\n",
86                           Irp));
87
88             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
89         }
90
91         lLength = (LONG)pIrpSp->Parameters.QueryFile.Length;
92         stFileInformationClass = pIrpSp->Parameters.QueryFile.FileInformationClass;
93         pBuffer = Irp->AssociatedIrp.SystemBuffer;
94
95         if ( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_VERIFY))
96         {
97
98             RtlZeroMemory( &stAuthGroup,
99                            sizeof( GUID));
100
101             AFSRetrieveAuthGroupFnc( (ULONGLONG)PsGetCurrentProcessId(),
102                                      (ULONGLONG)PsGetCurrentThreadId(),
103                                      &stAuthGroup);
104
105             ntStatus = AFSVerifyEntry( &stAuthGroup,
106                                        pCcb->DirectoryCB);
107
108             if ( NT_SUCCESS( ntStatus))
109             {
110
111                 ClearFlag( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_VERIFY);
112             }
113             else
114             {
115
116                 ntStatus = STATUS_SUCCESS;
117             }
118         }
119
120         //
121         // Grab the main shared right off the bat
122         //
123
124         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
125                       AFS_TRACE_LEVEL_VERBOSE,
126                       "AFSQueryFileInfo Acquiring Fcb lock %p SHARED %08lX\n",
127                       &pFcb->NPFcb->Resource,
128                       PsGetCurrentThread()));
129
130         AFSAcquireShared( &pFcb->NPFcb->Resource,
131                           TRUE);
132
133         bReleaseMain = TRUE;
134
135         //
136         // Don't allow requests against IOCtl nodes
137         //
138
139         if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
140         {
141
142             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
143                           AFS_TRACE_LEVEL_VERBOSE,
144                           "AFSQueryFileInfo Processing request against SpecialShare Fcb\n"));
145
146             ntStatus = AFSProcessShareQueryInfo( Irp,
147                                                  pFcb,
148                                                  pCcb);
149
150             try_return( ntStatus);
151         }
152         else if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
153         {
154             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
155                           AFS_TRACE_LEVEL_VERBOSE,
156                           "AFSQueryFileInfo request against PIOCtl Fcb\n"));
157
158             ntStatus = AFSProcessPIOCtlQueryInfo( Irp,
159                                                   pFcb,
160                                                   pCcb,
161                                                   &lLength);
162
163             try_return( ntStatus);
164         }
165
166         else if( pFcb->Header.NodeTypeCode == AFS_INVALID_FCB)
167         {
168             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
169                           AFS_TRACE_LEVEL_VERBOSE,
170                           "AFSQueryFileInfo request against Invalid Fcb\n"));
171
172             try_return( ntStatus = STATUS_ACCESS_DENIED);
173         }
174
175         //
176         // Process the request
177         //
178
179         switch( stFileInformationClass)
180         {
181
182             case FileAllInformation:
183             {
184
185                 PFILE_ALL_INFORMATION pAllInfo;
186
187                 //
188                 //  For the all information class we'll typecast a local
189                 //  pointer to the output buffer and then call the
190                 //  individual routines to fill in the buffer.
191                 //
192
193                 pAllInfo = (PFILE_ALL_INFORMATION)pBuffer;
194
195                 ntStatus = AFSQueryBasicInfo( Irp,
196                                               pCcb->DirectoryCB,
197                                               &pAllInfo->BasicInformation,
198                                               &lLength);
199
200                 if( !NT_SUCCESS( ntStatus))
201                 {
202
203                     try_return( ntStatus);
204                 }
205
206                 ntStatus = AFSQueryStandardInfo( Irp,
207                                                  pCcb->DirectoryCB,
208                                                  &pAllInfo->StandardInformation,
209                                                  &lLength);
210
211                 if( !NT_SUCCESS( ntStatus))
212                 {
213
214                     try_return( ntStatus);
215                 }
216
217                 ntStatus = AFSQueryInternalInfo( Irp,
218                                                  pFcb,
219                                                  &pAllInfo->InternalInformation,
220                                                  &lLength);
221
222                 if( !NT_SUCCESS( ntStatus))
223                 {
224
225                     try_return( ntStatus);
226                 }
227
228                 ntStatus = AFSQueryEaInfo( Irp,
229                                            pCcb->DirectoryCB,
230                                            &pAllInfo->EaInformation,
231                                            &lLength);
232
233                 if( !NT_SUCCESS( ntStatus))
234                 {
235
236                     try_return( ntStatus);
237                 }
238
239                 ntStatus = AFSQueryAccess( Irp,
240                                            pFcb,
241                                            &pAllInfo->AccessInformation,
242                                            &lLength);
243
244                 if( !NT_SUCCESS( ntStatus))
245                 {
246
247                     try_return( ntStatus);
248                 }
249
250                 ntStatus = AFSQueryPositionInfo( Irp,
251                                                  pFcb,
252                                                  &pAllInfo->PositionInformation,
253                                                  &lLength);
254
255                 if( !NT_SUCCESS( ntStatus))
256                 {
257
258                     try_return( ntStatus);
259                 }
260
261                 ntStatus = AFSQueryMode( Irp,
262                                          pFcb,
263                                          &pAllInfo->ModeInformation,
264                                          &lLength);
265
266                 if( !NT_SUCCESS( ntStatus))
267                 {
268
269                     try_return( ntStatus);
270                 }
271
272                 ntStatus = AFSQueryAlignment( Irp,
273                                               pFcb,
274                                               &pAllInfo->AlignmentInformation,
275                                               &lLength);
276
277                 if( !NT_SUCCESS( ntStatus))
278                 {
279
280                     try_return( ntStatus);
281                 }
282
283                 ntStatus = AFSQueryNameInfo( Irp,
284                                              pCcb->DirectoryCB,
285                                              &pAllInfo->NameInformation,
286                                              &lLength);
287
288                 if( !NT_SUCCESS( ntStatus))
289                 {
290
291                     try_return( ntStatus);
292                 }
293
294                 break;
295             }
296
297             case FileBasicInformation:
298             {
299
300                 ntStatus = AFSQueryBasicInfo( Irp,
301                                               pCcb->DirectoryCB,
302                                               (PFILE_BASIC_INFORMATION)pBuffer,
303                                               &lLength);
304
305                 break;
306             }
307
308             case FileStandardInformation:
309             {
310
311                 ntStatus = AFSQueryStandardInfo( Irp,
312                                                  pCcb->DirectoryCB,
313                                                  (PFILE_STANDARD_INFORMATION)pBuffer,
314                                                  &lLength);
315
316                 break;
317             }
318
319             case FileInternalInformation:
320             {
321
322                 ntStatus = AFSQueryInternalInfo( Irp,
323                                                  pFcb,
324                                                  (PFILE_INTERNAL_INFORMATION)pBuffer,
325                                                  &lLength);
326
327                 break;
328             }
329
330             case FileEaInformation:
331             {
332
333                 ntStatus = AFSQueryEaInfo( Irp,
334                                            pCcb->DirectoryCB,
335                                            (PFILE_EA_INFORMATION)pBuffer,
336                                            &lLength);
337
338                 break;
339             }
340
341             case FilePositionInformation:
342             {
343
344                 ntStatus = AFSQueryPositionInfo( Irp,
345                                       pFcb,
346                                       (PFILE_POSITION_INFORMATION)pBuffer,
347                                       &lLength);
348
349                 break;
350             }
351
352             case FileNormalizedNameInformation:
353             case FileNameInformation:
354             {
355
356                 ntStatus = AFSQueryNameInfo( Irp,
357                                   pCcb->DirectoryCB,
358                                   (PFILE_NAME_INFORMATION)pBuffer,
359                                   &lLength);
360
361                 break;
362             }
363
364             case FileAlternateNameInformation:
365             {
366
367                 ntStatus = AFSQueryShortNameInfo( Irp,
368                                        pCcb->DirectoryCB,
369                                        (PFILE_NAME_INFORMATION)pBuffer,
370                                        &lLength);
371
372                 break;
373             }
374
375             case FileNetworkOpenInformation:
376             {
377
378                 ntStatus = AFSQueryNetworkInfo( Irp,
379                                      pCcb->DirectoryCB,
380                                      (PFILE_NETWORK_OPEN_INFORMATION)pBuffer,
381                                      &lLength);
382
383                 break;
384             }
385
386             case FileStreamInformation:
387             {
388
389                 ntStatus = AFSQueryStreamInfo( Irp,
390                                                pCcb->DirectoryCB,
391                                                (FILE_STREAM_INFORMATION *)pBuffer,
392                                                &lLength);
393
394                 break;
395             }
396
397
398             case FileAttributeTagInformation:
399             {
400
401                 ntStatus = AFSQueryAttribTagInfo( Irp,
402                                                   pCcb->DirectoryCB,
403                                                   (FILE_ATTRIBUTE_TAG_INFORMATION *)pBuffer,
404                                                   &lLength);
405
406                 break;
407             }
408
409             case FileRemoteProtocolInformation:
410             {
411
412                     ntStatus = AFSQueryRemoteProtocolInfo( Irp,
413                                                            pCcb->DirectoryCB,
414                                                            (FILE_REMOTE_PROTOCOL_INFORMATION *)pBuffer,
415                                                            &lLength);
416
417                 break;
418             }
419
420             case FileNetworkPhysicalNameInformation:
421             {
422
423                 ntStatus = AFSQueryPhysicalNameInfo( Irp,
424                                                      pCcb->DirectoryCB,
425                                                      (FILE_NETWORK_PHYSICAL_NAME_INFORMATION *)pBuffer,
426                                                      &lLength);
427
428                 break;
429             }
430
431             default:
432             {
433                 ntStatus = STATUS_INVALID_PARAMETER;
434                 break;
435             }
436         }
437
438 try_exit:
439
440         Irp->IoStatus.Information = pIrpSp->Parameters.QueryFile.Length - lLength;
441
442         if( bReleaseMain)
443         {
444
445             AFSReleaseResource( &pFcb->NPFcb->Resource);
446         }
447
448         if( !NT_SUCCESS( ntStatus) &&
449             ntStatus != STATUS_INVALID_PARAMETER &&
450             ntStatus != STATUS_BUFFER_OVERFLOW)
451         {
452
453             if( pCcb != NULL &&
454                 pCcb->DirectoryCB != NULL)
455             {
456
457                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
458                               AFS_TRACE_LEVEL_ERROR,
459                               "AFSQueryFileInfo Failed to process request for %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
460                               &pCcb->DirectoryCB->NameInformation.FileName,
461                               pFcb->ObjectInformation->FileId.Cell,
462                               pFcb->ObjectInformation->FileId.Volume,
463                               pFcb->ObjectInformation->FileId.Vnode,
464                               pFcb->ObjectInformation->FileId.Unique,
465                               ntStatus));
466             }
467         }
468     }
469     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
470     {
471
472         AFSDbgTrace(( 0,
473                       0,
474                       "EXCEPTION - AFSQueryFileInfo\n"));
475
476         AFSDumpTraceFilesFnc();
477
478         ntStatus = STATUS_UNSUCCESSFUL;
479
480         if( bReleaseMain)
481         {
482
483             AFSReleaseResource( &pFcb->NPFcb->Resource);
484         }
485     }
486
487     AFSCompleteRequest( Irp,
488                         ntStatus);
489
490     return ntStatus;
491 }
492
493 //
494 // Function: AFSSetFileInfo
495 //
496 // Description:
497 //
498 //      This function is the dispatch handler for the IRP_MJ_SET_FILE_INFORMATION request
499 //
500 // Return:
501 //
502 //      A status is returned for the function
503 //
504
505 NTSTATUS
506 AFSSetFileInfo( IN PDEVICE_OBJECT LibDeviceObject,
507                 IN PIRP Irp)
508 {
509
510     UNREFERENCED_PARAMETER(LibDeviceObject);
511     NTSTATUS ntStatus = STATUS_SUCCESS;
512     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
513     AFSFcb *pFcb = NULL;
514     AFSCcb *pCcb = NULL;
515     FILE_INFORMATION_CLASS FileInformationClass;
516     BOOLEAN bCanQueueRequest = FALSE;
517     PFILE_OBJECT pFileObject = NULL;
518     BOOLEAN bReleaseMain = FALSE;
519     BOOLEAN bUpdateFileInfo = FALSE;
520     AFSFileID stParentFileId;
521
522     __try
523     {
524
525         pFileObject = pIrpSp->FileObject;
526
527         pFcb = (AFSFcb *)pFileObject->FsContext;
528         pCcb = (AFSCcb *)pFileObject->FsContext2;
529
530         if( pFcb == NULL)
531         {
532
533             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
534                           AFS_TRACE_LEVEL_ERROR,
535                           "AFSSetFileInfo Attempted access (%p) when pFcb == NULL\n",
536                           Irp));
537
538             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
539         }
540
541         bCanQueueRequest = !(IoIsOperationSynchronous( Irp) | (KeGetCurrentIrql() != PASSIVE_LEVEL));
542         FileInformationClass = pIrpSp->Parameters.SetFile.FileInformationClass;
543
544         //
545         // Grab the Fcb EXCL
546         //
547
548         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
549                       AFS_TRACE_LEVEL_VERBOSE,
550                       "AFSSetFileInfo Acquiring Fcb lock %p EXCL %08lX\n",
551                       &pFcb->NPFcb->Resource,
552                       PsGetCurrentThread()));
553
554         AFSAcquireExcl( &pFcb->NPFcb->Resource,
555                         TRUE);
556
557         bReleaseMain = TRUE;
558
559         //
560         // Don't allow requests against IOCtl nodes
561         //
562
563         if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
564         {
565
566             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
567                           AFS_TRACE_LEVEL_ERROR,
568                           "AFSSetFileInfo Failing request against PIOCtl Fcb\n"));
569
570             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
571         }
572         else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
573         {
574
575             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
576                           AFS_TRACE_LEVEL_VERBOSE,
577                           "AFSSetFileInfo Processing request against SpecialShare Fcb\n"));
578
579             ntStatus = AFSProcessShareSetInfo( Irp,
580                                                pFcb,
581                                                pCcb);
582
583             try_return( ntStatus);
584         }
585
586         if( BooleanFlagOn( pFcb->ObjectInformation->VolumeCB->VolumeInformation.FileSystemAttributes, FILE_READ_ONLY_VOLUME))
587         {
588
589             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
590                           AFS_TRACE_LEVEL_ERROR,
591                           "AFSSetFileInfo Request failed due to read only volume\n",
592                           Irp));
593
594             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
595         }
596
597         if( pFcb->Header.NodeTypeCode == AFS_INVALID_FCB &&
598             FileInformationClass != FileDispositionInformation)
599         {
600             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
601                           AFS_TRACE_LEVEL_VERBOSE,
602                           "AFSSetFileInfo request against Invalid Fcb\n"));
603
604             try_return( ntStatus = STATUS_ACCESS_DENIED);
605         }
606
607         //
608         // Ensure rename operations are synchronous
609         //
610
611         if( FileInformationClass == FileRenameInformation)
612         {
613
614             bCanQueueRequest = FALSE;
615         }
616
617         //
618         // Store away the parent fid
619         //
620
621         RtlZeroMemory( &stParentFileId,
622                        sizeof( AFSFileID));
623
624         if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
625         {
626
627             stParentFileId = pFcb->ObjectInformation->ParentFileId;
628         }
629
630         //
631         // Process the request
632         //
633
634         switch( FileInformationClass)
635         {
636
637             case FileBasicInformation:
638             {
639
640                 bUpdateFileInfo = TRUE;
641
642                 ntStatus = AFSSetBasicInfo( Irp,
643                                             pCcb->DirectoryCB);
644
645                 break;
646             }
647
648             case FileDispositionInformation:
649             {
650
651                 ntStatus = AFSSetDispositionInfo( Irp,
652                                                   pCcb->DirectoryCB);
653
654                 break;
655             }
656
657             case FileRenameInformation:
658             {
659
660                 ntStatus = AFSSetRenameInfo( Irp);
661
662                 break;
663             }
664
665             case FilePositionInformation:
666             {
667
668                 ntStatus = AFSSetPositionInfo( Irp,
669                                                pCcb->DirectoryCB);
670
671                 break;
672             }
673
674             case FileLinkInformation:
675             {
676
677                 ntStatus = AFSSetFileLinkInfo( Irp);
678
679                 break;
680             }
681
682             case FileAllocationInformation:
683             {
684
685                 ntStatus = AFSSetAllocationInfo( Irp,
686                                                  pCcb->DirectoryCB);
687
688                 break;
689             }
690
691             case FileEndOfFileInformation:
692             {
693
694                 ntStatus = AFSSetEndOfFileInfo( Irp,
695                                                 pCcb->DirectoryCB);
696
697                 break;
698             }
699
700             default:
701
702                 ntStatus = STATUS_INVALID_PARAMETER;
703
704                 break;
705         }
706
707 try_exit:
708
709         if( bReleaseMain)
710         {
711
712             AFSReleaseResource( &pFcb->NPFcb->Resource);
713         }
714
715         if( NT_SUCCESS( ntStatus) &&
716             bUpdateFileInfo)
717         {
718
719             ntStatus = AFSUpdateFileInformation( &stParentFileId,
720                                                  pFcb->ObjectInformation,
721                                                  &pCcb->AuthGroup);
722
723             if( !NT_SUCCESS( ntStatus))
724             {
725
726                 AFSAcquireExcl( &pFcb->NPFcb->Resource,
727                                 TRUE);
728
729                 //
730                 // Unwind the update and fail the request
731                 //
732
733                 AFSUnwindFileInfo( pFcb,
734                                    pCcb);
735
736                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
737                               AFS_TRACE_LEVEL_ERROR,
738                               "AFSSetFileInfo Failed to send file info update to service request for %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
739                               &pCcb->DirectoryCB->NameInformation.FileName,
740                               pFcb->ObjectInformation->FileId.Cell,
741                               pFcb->ObjectInformation->FileId.Volume,
742                               pFcb->ObjectInformation->FileId.Vnode,
743                               pFcb->ObjectInformation->FileId.Unique,
744                               ntStatus));
745
746                 AFSReleaseResource( &pFcb->NPFcb->Resource);
747             }
748         }
749
750         if( !NT_SUCCESS( ntStatus))
751         {
752
753             if( pCcb != NULL &&
754                 pCcb->DirectoryCB != NULL)
755             {
756
757                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
758                               AFS_TRACE_LEVEL_ERROR,
759                               "AFSSetFileInfo Failed to process request for %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
760                               &pCcb->DirectoryCB->NameInformation.FileName,
761                               pFcb->ObjectInformation->FileId.Cell,
762                               pFcb->ObjectInformation->FileId.Volume,
763                               pFcb->ObjectInformation->FileId.Vnode,
764                               pFcb->ObjectInformation->FileId.Unique,
765                               ntStatus));
766             }
767         }
768     }
769     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
770     {
771
772         AFSDbgTrace(( 0,
773                       0,
774                       "EXCEPTION - AFSSetFileInfo\n"));
775
776         AFSDumpTraceFilesFnc();
777
778         ntStatus = STATUS_UNSUCCESSFUL;
779
780         if( bReleaseMain)
781         {
782
783             AFSReleaseResource( &pFcb->NPFcb->Resource);
784         }
785     }
786
787     AFSCompleteRequest( Irp,
788                         ntStatus);
789
790     return ntStatus;
791 }
792
793 //
794 // Function: AFSQueryBasicInfo
795 //
796 // Description:
797 //
798 //      This function is the handler for the query basic information request
799 //
800 // Return:
801 //
802 //      A status is returned for the function
803 //
804
805 NTSTATUS
806 AFSQueryBasicInfo( IN PIRP Irp,
807                    IN AFSDirectoryCB *DirectoryCB,
808                    IN OUT PFILE_BASIC_INFORMATION Buffer,
809                    IN OUT PLONG Length)
810 {
811     NTSTATUS ntStatus = STATUS_SUCCESS;
812     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
813     ULONG ulFileAttribs = 0;
814     AFSFcb *pFcb = NULL;
815     AFSCcb *pCcb = NULL;
816     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
817     AFSFileInfoCB stFileInfo;
818     AFSDirectoryCB *pParentDirectoryCB = NULL;
819     UNICODE_STRING uniParentPath;
820
821     if( *Length >= sizeof( FILE_BASIC_INFORMATION))
822     {
823
824         RtlZeroMemory( Buffer,
825                        *Length);
826
827         ulFileAttribs = DirectoryCB->ObjectInformation->FileAttributes;
828
829         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
830         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
831
832         if( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SYMLINK)
833         {
834
835             pParentDirectoryCB = AFSGetParentEntry( pCcb->NameArray);
836
837             AFSRetrieveParentPath( &pCcb->FullFileName,
838                                    &uniParentPath);
839
840             RtlZeroMemory( &stFileInfo,
841                            sizeof( AFSFileInfoCB));
842
843             //
844             // Can't hold the Fcb while evaluating the path, leads to lock inversion
845             //
846
847             AFSReleaseResource( &pFcb->NPFcb->Resource);
848
849             //
850             // Its a reparse point regardless of whether the file attributes
851             // can be retrieved for the target.
852             //
853
854             if ( ulFileAttribs == FILE_ATTRIBUTE_NORMAL)
855             {
856
857                 ulFileAttribs = FILE_ATTRIBUTE_REPARSE_POINT;
858             }
859             else
860             {
861
862                 ulFileAttribs |= FILE_ATTRIBUTE_REPARSE_POINT;
863             }
864
865             if( NT_SUCCESS( AFSRetrieveFileAttributes( pParentDirectoryCB,
866                                                        DirectoryCB,
867                                                        &uniParentPath,
868                                                        pCcb->NameArray,
869                                                        &pCcb->AuthGroup,
870                                                        &stFileInfo)))
871             {
872
873                 if ( stFileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
874                 {
875
876                     ulFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
877                 }
878             }
879
880             AFSAcquireShared( &pFcb->NPFcb->Resource,
881                               TRUE);
882         }
883
884
885         AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
886                       AFS_TRACE_LEVEL_VERBOSE_2,
887                       "AFSQueryBasicInfo %wZ Type 0x%x Attrib 0x%x -> 0x%x\n",
888                       &pCcb->DirectoryCB->NameInformation.FileName,
889                       pFcb->ObjectInformation->FileType,
890                       pFcb->ObjectInformation->FileAttributes,
891                       ulFileAttribs));
892
893         Buffer->CreationTime = DirectoryCB->ObjectInformation->CreationTime;
894         Buffer->LastAccessTime = DirectoryCB->ObjectInformation->LastAccessTime;
895         Buffer->LastWriteTime = DirectoryCB->ObjectInformation->LastWriteTime;
896         Buffer->ChangeTime = DirectoryCB->ObjectInformation->ChangeTime;
897         Buffer->FileAttributes = ulFileAttribs;
898
899         if( DirectoryCB->NameInformation.FileName.Buffer[ 0] == L'.' &&
900             BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_HIDE_DOT_NAMES))
901         {
902
903             if ( Buffer->FileAttributes != FILE_ATTRIBUTE_NORMAL)
904             {
905                 Buffer->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
906             }
907             else
908             {
909                 Buffer->FileAttributes = FILE_ATTRIBUTE_HIDDEN;
910             }
911         }
912
913         *Length -= sizeof( FILE_BASIC_INFORMATION);
914     }
915     else
916     {
917
918         ntStatus = STATUS_BUFFER_TOO_SMALL;
919     }
920
921     return ntStatus;
922 }
923
924 NTSTATUS
925 AFSQueryStandardInfo( IN PIRP Irp,
926                       IN AFSDirectoryCB *DirectoryCB,
927                       IN OUT PFILE_STANDARD_INFORMATION Buffer,
928                       IN OUT PLONG Length)
929 {
930
931     NTSTATUS ntStatus = STATUS_SUCCESS;
932     AFSFcb *pFcb = NULL;
933     AFSCcb *pCcb = NULL;
934     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
935     AFSFileInfoCB stFileInfo;
936     AFSDirectoryCB *pParentDirectoryCB = NULL;
937     UNICODE_STRING uniParentPath;
938     ULONG ulFileAttribs = 0;
939
940     if( *Length >= sizeof( FILE_STANDARD_INFORMATION))
941     {
942
943         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
944         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
945
946         RtlZeroMemory( Buffer,
947                        *Length);
948
949         Buffer->NumberOfLinks = 1;
950         Buffer->DeletePending = BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
951
952         Buffer->AllocationSize.QuadPart = (ULONGLONG)((DirectoryCB->ObjectInformation->AllocationSize.QuadPart/PAGE_SIZE) + 1) * PAGE_SIZE;
953
954         Buffer->EndOfFile = DirectoryCB->ObjectInformation->EndOfFile;
955
956         ulFileAttribs = DirectoryCB->ObjectInformation->FileAttributes;
957
958         if( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SYMLINK)
959         {
960
961             pParentDirectoryCB = AFSGetParentEntry( pCcb->NameArray);
962
963             AFSRetrieveParentPath( &pCcb->FullFileName,
964                                    &uniParentPath);
965
966             RtlZeroMemory( &stFileInfo,
967                            sizeof( AFSFileInfoCB));
968
969             //
970             // Can't hold the Fcb while evaluating the path, leads to lock inversion
971             //
972
973             AFSReleaseResource( &pFcb->NPFcb->Resource);
974
975             //
976             // Its a reparse point regardless of whether or not the
977             // file attributes can be retrieved.
978             //
979
980             if ( ulFileAttribs == FILE_ATTRIBUTE_NORMAL)
981             {
982
983                 ulFileAttribs = FILE_ATTRIBUTE_REPARSE_POINT;
984             }
985             else
986             {
987
988                 ulFileAttribs |= FILE_ATTRIBUTE_REPARSE_POINT;
989             }
990
991             if( NT_SUCCESS( AFSRetrieveFileAttributes( pParentDirectoryCB,
992                                                        DirectoryCB,
993                                                        &uniParentPath,
994                                                        pCcb->NameArray,
995                                                        &pCcb->AuthGroup,
996                                                        &stFileInfo)))
997             {
998
999                 if ( stFileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1000                 {
1001
1002                     ulFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
1003                 }
1004             }
1005
1006             AFSAcquireShared( &pFcb->NPFcb->Resource,
1007                               TRUE);
1008         }
1009
1010         AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1011                       AFS_TRACE_LEVEL_VERBOSE_2,
1012                       "AFSQueryStandardInfo %wZ Type 0x%x Attrib 0x%x -> 0x%x\n",
1013                       &pCcb->DirectoryCB->NameInformation.FileName,
1014                       pFcb->ObjectInformation->FileType,
1015                       pFcb->ObjectInformation->FileAttributes,
1016                       ulFileAttribs));
1017
1018         Buffer->Directory = BooleanFlagOn( ulFileAttribs, FILE_ATTRIBUTE_DIRECTORY);
1019
1020         *Length -= sizeof( FILE_STANDARD_INFORMATION);
1021     }
1022     else
1023     {
1024
1025         ntStatus = STATUS_BUFFER_TOO_SMALL;
1026     }
1027
1028     return ntStatus;
1029 }
1030
1031 NTSTATUS
1032 AFSQueryInternalInfo( IN PIRP Irp,
1033                       IN AFSFcb *Fcb,
1034                       IN OUT PFILE_INTERNAL_INFORMATION Buffer,
1035                       IN OUT PLONG Length)
1036 {
1037
1038     UNREFERENCED_PARAMETER(Irp);
1039     NTSTATUS ntStatus = STATUS_SUCCESS;
1040
1041     if( *Length >= sizeof( FILE_INTERNAL_INFORMATION))
1042     {
1043
1044         Buffer->IndexNumber.HighPart = Fcb->ObjectInformation->FileId.Vnode;
1045
1046         Buffer->IndexNumber.LowPart = Fcb->ObjectInformation->FileId.Unique;
1047
1048         *Length -= sizeof( FILE_INTERNAL_INFORMATION);
1049     }
1050     else
1051     {
1052
1053         ntStatus = STATUS_BUFFER_TOO_SMALL;
1054     }
1055
1056     return ntStatus;
1057 }
1058
1059 NTSTATUS
1060 AFSQueryEaInfo( IN PIRP Irp,
1061                 IN AFSDirectoryCB *DirectoryCB,
1062                 IN OUT PFILE_EA_INFORMATION Buffer,
1063                 IN OUT PLONG Length)
1064 {
1065
1066     UNREFERENCED_PARAMETER(Irp);
1067     UNREFERENCED_PARAMETER(DirectoryCB);
1068     NTSTATUS ntStatus = STATUS_SUCCESS;
1069
1070     RtlZeroMemory( Buffer,
1071                    *Length);
1072
1073     if( *Length >= sizeof( FILE_EA_INFORMATION))
1074     {
1075
1076         Buffer->EaSize = 0;
1077
1078         *Length -= sizeof( FILE_EA_INFORMATION);
1079     }
1080     else
1081     {
1082
1083         ntStatus = STATUS_BUFFER_TOO_SMALL;
1084     }
1085
1086     return ntStatus;
1087 }
1088
1089 NTSTATUS
1090 AFSQueryPositionInfo( IN PIRP Irp,
1091                       IN AFSFcb *Fcb,
1092                       IN OUT PFILE_POSITION_INFORMATION Buffer,
1093                       IN OUT PLONG Length)
1094 {
1095
1096     UNREFERENCED_PARAMETER(Fcb);
1097     NTSTATUS ntStatus = STATUS_SUCCESS;
1098     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1099
1100     if( *Length >= sizeof( FILE_POSITION_INFORMATION))
1101     {
1102
1103         RtlZeroMemory( Buffer,
1104                        *Length);
1105
1106         Buffer->CurrentByteOffset.QuadPart = pIrpSp->FileObject->CurrentByteOffset.QuadPart;
1107
1108         *Length -= sizeof( FILE_POSITION_INFORMATION);
1109     }
1110     else
1111     {
1112
1113         ntStatus = STATUS_BUFFER_TOO_SMALL;
1114     }
1115
1116     return ntStatus;
1117 }
1118
1119 NTSTATUS
1120 AFSQueryAccess( IN PIRP Irp,
1121                 IN AFSFcb *Fcb,
1122                 IN OUT PFILE_ACCESS_INFORMATION Buffer,
1123                 IN OUT PLONG Length)
1124 {
1125
1126     UNREFERENCED_PARAMETER(Irp);
1127     UNREFERENCED_PARAMETER(Fcb);
1128     NTSTATUS ntStatus = STATUS_SUCCESS;
1129
1130     if( *Length >= sizeof( FILE_ACCESS_INFORMATION))
1131     {
1132
1133         RtlZeroMemory( Buffer,
1134                        *Length);
1135
1136         Buffer->AccessFlags = 0;
1137
1138         *Length -= sizeof( FILE_ACCESS_INFORMATION);
1139     }
1140     else
1141     {
1142
1143         ntStatus = STATUS_BUFFER_TOO_SMALL;
1144     }
1145
1146     return ntStatus;
1147 }
1148
1149 NTSTATUS
1150 AFSQueryMode( IN PIRP Irp,
1151               IN AFSFcb *Fcb,
1152               IN OUT PFILE_MODE_INFORMATION Buffer,
1153               IN OUT PLONG Length)
1154 {
1155
1156     UNREFERENCED_PARAMETER(Irp);
1157     UNREFERENCED_PARAMETER(Fcb);
1158     NTSTATUS ntStatus = STATUS_SUCCESS;
1159
1160     if( *Length >= sizeof( FILE_MODE_INFORMATION))
1161     {
1162
1163         RtlZeroMemory( Buffer,
1164                        *Length);
1165
1166         Buffer->Mode = 0;
1167
1168         *Length -= sizeof( FILE_MODE_INFORMATION);
1169     }
1170     else
1171     {
1172
1173         ntStatus = STATUS_BUFFER_TOO_SMALL;
1174     }
1175
1176     return ntStatus;
1177 }
1178
1179 NTSTATUS
1180 AFSQueryAlignment( IN PIRP Irp,
1181                    IN AFSFcb *Fcb,
1182                    IN OUT PFILE_ALIGNMENT_INFORMATION Buffer,
1183                    IN OUT PLONG Length)
1184 {
1185
1186     UNREFERENCED_PARAMETER(Irp);
1187     UNREFERENCED_PARAMETER(Fcb);
1188     NTSTATUS ntStatus = STATUS_SUCCESS;
1189
1190     if( *Length >= sizeof( FILE_ALIGNMENT_INFORMATION))
1191     {
1192
1193         RtlZeroMemory( Buffer,
1194                        *Length);
1195
1196         Buffer->AlignmentRequirement = 1;
1197
1198         *Length -= sizeof( FILE_ALIGNMENT_INFORMATION);
1199     }
1200     else
1201     {
1202
1203         ntStatus = STATUS_BUFFER_TOO_SMALL;
1204     }
1205
1206     return ntStatus;
1207 }
1208
1209 NTSTATUS
1210 AFSQueryNameInfo( IN PIRP Irp,
1211                   IN AFSDirectoryCB *DirectoryCB,
1212                   IN OUT PFILE_NAME_INFORMATION Buffer,
1213                   IN OUT PLONG Length)
1214 {
1215
1216     UNREFERENCED_PARAMETER(DirectoryCB);
1217     NTSTATUS ntStatus = STATUS_SUCCESS;
1218     ULONG ulCopyLength = 0;
1219     ULONG cchCopied = 0;
1220     AFSFcb *pFcb = NULL;
1221     AFSCcb *pCcb = NULL;
1222     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1223     BOOLEAN bAddLeadingSlash = FALSE;
1224     BOOLEAN bAddTrailingSlash = FALSE;
1225     USHORT usFullNameLength = 0;
1226
1227     pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1228
1229     pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1230
1231     if( *Length >= FIELD_OFFSET( FILE_NAME_INFORMATION, FileName))
1232     {
1233
1234         RtlZeroMemory( Buffer,
1235                        *Length);
1236
1237         if( pCcb->FullFileName.Length == 0 ||
1238             pCcb->FullFileName.Buffer[ 0] != L'\\')
1239         {
1240             bAddLeadingSlash = TRUE;
1241         }
1242
1243         if( pFcb->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
1244             pCcb->FullFileName.Length > 0 &&
1245             pCcb->FullFileName.Buffer[ (pCcb->FullFileName.Length/sizeof( WCHAR)) - 1] != L'\\')
1246         {
1247             bAddTrailingSlash = TRUE;
1248         }
1249
1250         usFullNameLength = sizeof( WCHAR) +
1251                                     AFSServerName.Length +
1252                                     pCcb->FullFileName.Length;
1253
1254         if( bAddLeadingSlash)
1255         {
1256             usFullNameLength += sizeof( WCHAR);
1257         }
1258
1259         if( bAddTrailingSlash)
1260         {
1261             usFullNameLength += sizeof( WCHAR);
1262         }
1263
1264         if( *Length >= (LONG)(FIELD_OFFSET( FILE_NAME_INFORMATION, FileName) + (LONG)usFullNameLength))
1265         {
1266
1267             ulCopyLength = (LONG)usFullNameLength;
1268         }
1269         else
1270         {
1271
1272             ulCopyLength = *Length - FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1273
1274             ntStatus = STATUS_BUFFER_OVERFLOW;
1275         }
1276
1277         Buffer->FileNameLength = (ULONG)usFullNameLength;
1278
1279         *Length -= FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1280
1281         if( ulCopyLength > 0)
1282         {
1283
1284             Buffer->FileName[ 0] = L'\\';
1285             ulCopyLength -= sizeof( WCHAR);
1286
1287             *Length -= sizeof( WCHAR);
1288             cchCopied += 1;
1289
1290             if( ulCopyLength >= AFSServerName.Length)
1291             {
1292
1293                 RtlCopyMemory( &Buffer->FileName[ 1],
1294                                AFSServerName.Buffer,
1295                                AFSServerName.Length);
1296
1297                 ulCopyLength -= AFSServerName.Length;
1298                 *Length -= AFSServerName.Length;
1299                 cchCopied += AFSServerName.Length/sizeof( WCHAR);
1300
1301                 if ( ulCopyLength > 0 &&
1302                      bAddLeadingSlash)
1303                 {
1304
1305                     Buffer->FileName[ cchCopied] = L'\\';
1306
1307                     ulCopyLength -= sizeof( WCHAR);
1308                     *Length -= sizeof( WCHAR);
1309                     cchCopied++;
1310                 }
1311
1312                 if( ulCopyLength >= pCcb->FullFileName.Length)
1313                 {
1314
1315                     RtlCopyMemory( &Buffer->FileName[ cchCopied],
1316                                    pCcb->FullFileName.Buffer,
1317                                    pCcb->FullFileName.Length);
1318
1319                     ulCopyLength -= pCcb->FullFileName.Length;
1320                     *Length -= pCcb->FullFileName.Length;
1321                     cchCopied += pCcb->FullFileName.Length/sizeof( WCHAR);
1322
1323                     if( ulCopyLength > 0 &&
1324                         bAddTrailingSlash)
1325                     {
1326                         Buffer->FileName[ cchCopied] = L'\\';
1327
1328                         *Length -= sizeof( WCHAR);
1329                     }
1330                 }
1331                 else
1332                 {
1333
1334                     RtlCopyMemory( &Buffer->FileName[ cchCopied],
1335                                    pCcb->FullFileName.Buffer,
1336                                    ulCopyLength);
1337
1338                     *Length -= ulCopyLength;
1339                 }
1340             }
1341         }
1342     }
1343     else
1344     {
1345
1346         ntStatus = STATUS_BUFFER_TOO_SMALL;
1347     }
1348
1349     return ntStatus;
1350 }
1351
1352 NTSTATUS
1353 AFSQueryShortNameInfo( IN PIRP Irp,
1354                        IN AFSDirectoryCB *DirectoryCB,
1355                        IN OUT PFILE_NAME_INFORMATION Buffer,
1356                        IN OUT PLONG Length)
1357 {
1358
1359     UNREFERENCED_PARAMETER(Irp);
1360     NTSTATUS ntStatus = STATUS_BUFFER_TOO_SMALL;
1361     ULONG ulCopyLength = 0;
1362
1363     RtlZeroMemory( Buffer,
1364                    *Length);
1365
1366     if( DirectoryCB->NameInformation.ShortNameLength == 0)
1367     {
1368
1369         //
1370         // The short name IS the long name
1371         //
1372
1373         if( *Length >= (LONG)FIELD_OFFSET( FILE_NAME_INFORMATION, FileName))
1374         {
1375
1376             if( *Length >= (LONG)(FIELD_OFFSET( FILE_NAME_INFORMATION, FileName) + (LONG)DirectoryCB->NameInformation.FileName.Length))
1377             {
1378
1379                 ulCopyLength = (LONG)DirectoryCB->NameInformation.FileName.Length;
1380
1381                 ntStatus = STATUS_SUCCESS;
1382             }
1383             else
1384             {
1385
1386                 ulCopyLength = *Length - FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1387
1388                 ntStatus = STATUS_BUFFER_OVERFLOW;
1389             }
1390
1391             Buffer->FileNameLength = DirectoryCB->NameInformation.FileName.Length;
1392
1393             *Length -= FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1394
1395             if( ulCopyLength > 0)
1396             {
1397
1398                 RtlCopyMemory( Buffer->FileName,
1399                                DirectoryCB->NameInformation.FileName.Buffer,
1400                                ulCopyLength);
1401
1402                 *Length -= ulCopyLength;
1403             }
1404         }
1405     }
1406     else
1407     {
1408
1409         if( *Length >= (LONG)FIELD_OFFSET( FILE_NAME_INFORMATION, FileName))
1410         {
1411
1412             if( *Length >= (LONG)(FIELD_OFFSET( FILE_NAME_INFORMATION, FileName) + (LONG)DirectoryCB->NameInformation.FileName.Length))
1413             {
1414
1415                 ulCopyLength = (LONG)DirectoryCB->NameInformation.ShortNameLength;
1416
1417                 ntStatus = STATUS_SUCCESS;
1418             }
1419             else
1420             {
1421
1422                 ulCopyLength = *Length - FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1423
1424                 ntStatus = STATUS_BUFFER_OVERFLOW;
1425             }
1426
1427             Buffer->FileNameLength = DirectoryCB->NameInformation.ShortNameLength;
1428
1429             *Length -= FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1430
1431             if( ulCopyLength > 0)
1432             {
1433
1434                 RtlCopyMemory( Buffer->FileName,
1435                                DirectoryCB->NameInformation.ShortName,
1436                                Buffer->FileNameLength);
1437
1438                 *Length -= ulCopyLength;
1439             }
1440         }
1441     }
1442
1443     return ntStatus;
1444 }
1445
1446 NTSTATUS
1447 AFSQueryNetworkInfo( IN PIRP Irp,
1448                      IN AFSDirectoryCB *DirectoryCB,
1449                      IN OUT PFILE_NETWORK_OPEN_INFORMATION Buffer,
1450                      IN OUT PLONG Length)
1451 {
1452
1453     NTSTATUS ntStatus = STATUS_SUCCESS;
1454     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1455     AFSFcb *pFcb = NULL;
1456     AFSCcb *pCcb = NULL;
1457     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1458     AFSFileInfoCB stFileInfo;
1459     AFSDirectoryCB *pParentDirectoryCB = NULL;
1460     UNICODE_STRING uniParentPath;
1461     ULONG ulFileAttribs = 0;
1462
1463     RtlZeroMemory( Buffer,
1464                    *Length);
1465
1466     if( *Length >= sizeof( FILE_NETWORK_OPEN_INFORMATION))
1467     {
1468
1469         ulFileAttribs = DirectoryCB->ObjectInformation->FileAttributes;
1470
1471         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1472         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1473
1474         if( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SYMLINK)
1475         {
1476
1477             pParentDirectoryCB = AFSGetParentEntry( pCcb->NameArray);
1478
1479             AFSRetrieveParentPath( &pCcb->FullFileName,
1480                                    &uniParentPath);
1481
1482             RtlZeroMemory( &stFileInfo,
1483                            sizeof( AFSFileInfoCB));
1484
1485             //
1486             // Can't hold the Fcb while evaluating the path, leads to lock inversion
1487             //
1488
1489             AFSReleaseResource( &pFcb->NPFcb->Resource);
1490
1491             //
1492             // Its a reparse point regardless of whether the file attributes
1493             // can be retrieved for the target.
1494             //
1495
1496             if ( ulFileAttribs == FILE_ATTRIBUTE_NORMAL)
1497             {
1498
1499                 ulFileAttribs = FILE_ATTRIBUTE_REPARSE_POINT;
1500             }
1501             else
1502             {
1503
1504                 ulFileAttribs |= FILE_ATTRIBUTE_REPARSE_POINT;
1505             }
1506
1507             if( NT_SUCCESS( AFSRetrieveFileAttributes( pParentDirectoryCB,
1508                                                        DirectoryCB,
1509                                                        &uniParentPath,
1510                                                        pCcb->NameArray,
1511                                                        &pCcb->AuthGroup,
1512                                                        &stFileInfo)))
1513             {
1514
1515                 if ( stFileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1516                 {
1517
1518                     ulFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
1519                 }
1520             }
1521
1522             AFSAcquireShared( &pFcb->NPFcb->Resource,
1523                               TRUE);
1524         }
1525
1526         AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1527                       AFS_TRACE_LEVEL_VERBOSE_2,
1528                       "AFSQueryNetworkInfo %wZ Type 0x%x Attrib 0x%x -> 0x%x\n",
1529                       &pCcb->DirectoryCB->NameInformation.FileName,
1530                       pFcb->ObjectInformation->FileType,
1531                       pFcb->ObjectInformation->FileAttributes,
1532                       ulFileAttribs));
1533
1534         Buffer->CreationTime.QuadPart = DirectoryCB->ObjectInformation->CreationTime.QuadPart;
1535         Buffer->LastAccessTime.QuadPart = DirectoryCB->ObjectInformation->LastAccessTime.QuadPart;
1536         Buffer->LastWriteTime.QuadPart = DirectoryCB->ObjectInformation->LastWriteTime.QuadPart;
1537         Buffer->ChangeTime.QuadPart = DirectoryCB->ObjectInformation->ChangeTime.QuadPart;
1538
1539         Buffer->AllocationSize.QuadPart = DirectoryCB->ObjectInformation->AllocationSize.QuadPart;
1540         Buffer->EndOfFile.QuadPart = DirectoryCB->ObjectInformation->EndOfFile.QuadPart;
1541
1542         Buffer->FileAttributes = ulFileAttribs;
1543
1544         if( DirectoryCB->NameInformation.FileName.Buffer[ 0] == L'.' &&
1545             BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_HIDE_DOT_NAMES))
1546         {
1547
1548             if ( Buffer->FileAttributes != FILE_ATTRIBUTE_NORMAL)
1549             {
1550
1551                 Buffer->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1552             }
1553             else
1554             {
1555
1556                 Buffer->FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1557             }
1558         }
1559
1560         *Length -= sizeof( FILE_NETWORK_OPEN_INFORMATION);
1561     }
1562     else
1563     {
1564
1565         ntStatus = STATUS_BUFFER_TOO_SMALL;
1566     }
1567
1568     return ntStatus;
1569 }
1570
1571 NTSTATUS
1572 AFSQueryStreamInfo( IN PIRP Irp,
1573                     IN AFSDirectoryCB *DirectoryCB,
1574                     IN OUT FILE_STREAM_INFORMATION *Buffer,
1575                     IN OUT PLONG Length)
1576 {
1577
1578     UNREFERENCED_PARAMETER(Irp);
1579     NTSTATUS ntStatus = STATUS_BUFFER_TOO_SMALL;
1580     ULONG ulCopyLength = 0;
1581
1582     if( *Length >= FIELD_OFFSET( FILE_STREAM_INFORMATION, StreamName))
1583     {
1584
1585         RtlZeroMemory( Buffer,
1586                        *Length);
1587
1588         Buffer->NextEntryOffset = 0;
1589
1590
1591         if( !BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_DIRECTORY))
1592         {
1593
1594             if( *Length >= (LONG)(FIELD_OFFSET( FILE_STREAM_INFORMATION, StreamName) + 14))  // ::$DATA
1595             {
1596
1597                 ulCopyLength = 14;
1598
1599                 ntStatus = STATUS_SUCCESS;
1600             }
1601             else
1602             {
1603
1604                 ulCopyLength = *Length - FIELD_OFFSET( FILE_STREAM_INFORMATION, StreamName);
1605
1606                 ntStatus = STATUS_BUFFER_OVERFLOW;
1607             }
1608
1609             Buffer->StreamNameLength = 14; // ::$DATA
1610
1611             Buffer->StreamSize.QuadPart = DirectoryCB->ObjectInformation->EndOfFile.QuadPart;
1612
1613             Buffer->StreamAllocationSize.QuadPart = DirectoryCB->ObjectInformation->AllocationSize.QuadPart;
1614
1615             *Length -= FIELD_OFFSET( FILE_STREAM_INFORMATION, StreamName);
1616
1617             if( ulCopyLength > 0)
1618             {
1619
1620                 RtlCopyMemory( Buffer->StreamName,
1621                                L"::$DATA",
1622                                ulCopyLength);
1623
1624                 *Length -= ulCopyLength;
1625             }
1626         }
1627         else
1628         {
1629
1630             Buffer->StreamNameLength = 0;       // No stream for a directory
1631
1632             // The response size is zero
1633
1634             ntStatus = STATUS_SUCCESS;
1635         }
1636     }
1637
1638     return ntStatus;
1639 }
1640
1641 NTSTATUS
1642 AFSQueryAttribTagInfo( IN PIRP Irp,
1643                        IN AFSDirectoryCB *DirectoryCB,
1644                        IN OUT FILE_ATTRIBUTE_TAG_INFORMATION *Buffer,
1645                        IN OUT PLONG Length)
1646 {
1647
1648     NTSTATUS ntStatus = STATUS_BUFFER_TOO_SMALL;
1649     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1650     AFSFcb *pFcb = NULL;
1651     AFSCcb *pCcb = NULL;
1652     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1653     AFSFileInfoCB stFileInfo;
1654     AFSDirectoryCB *pParentDirectoryCB = NULL;
1655     UNICODE_STRING uniParentPath;
1656     ULONG ulFileAttribs = 0;
1657
1658     if( *Length >= sizeof( FILE_ATTRIBUTE_TAG_INFORMATION))
1659     {
1660
1661         RtlZeroMemory( Buffer,
1662                        *Length);
1663
1664         ulFileAttribs = DirectoryCB->ObjectInformation->FileAttributes;
1665
1666         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1667         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1668
1669         if( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SYMLINK)
1670         {
1671
1672             pParentDirectoryCB = AFSGetParentEntry( pCcb->NameArray);
1673
1674             AFSRetrieveParentPath( &pCcb->FullFileName,
1675                                    &uniParentPath);
1676
1677             RtlZeroMemory( &stFileInfo,
1678                            sizeof( AFSFileInfoCB));
1679
1680             //
1681             // Can't hold the Fcb while evaluating the path, leads to lock inversion
1682             //
1683
1684             AFSReleaseResource( &pFcb->NPFcb->Resource);
1685
1686             //
1687             // Its a reparse point regardless of whether the file attributes
1688             // can be retrieved for the target.
1689             //
1690
1691             if ( ulFileAttribs == FILE_ATTRIBUTE_NORMAL)
1692             {
1693
1694                 ulFileAttribs = FILE_ATTRIBUTE_REPARSE_POINT;
1695             }
1696             else
1697             {
1698
1699                 ulFileAttribs |= FILE_ATTRIBUTE_REPARSE_POINT;
1700             }
1701
1702             if( NT_SUCCESS( AFSRetrieveFileAttributes( pParentDirectoryCB,
1703                                                        DirectoryCB,
1704                                                        &uniParentPath,
1705                                                        pCcb->NameArray,
1706                                                        &pCcb->AuthGroup,
1707                                                        &stFileInfo)))
1708             {
1709
1710                 if ( stFileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1711                 {
1712
1713                     ulFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
1714                 }
1715             }
1716
1717             AFSAcquireShared( &pFcb->NPFcb->Resource,
1718                               TRUE);
1719         }
1720
1721         AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1722                       AFS_TRACE_LEVEL_VERBOSE_2,
1723                       "AFSAttribTagInfo %wZ Type 0x%x Attrib 0x%x -> 0x%x\n",
1724                       &pCcb->DirectoryCB->NameInformation.FileName,
1725                       pFcb->ObjectInformation->FileType,
1726                       pFcb->ObjectInformation->FileAttributes,
1727                       ulFileAttribs));
1728
1729         Buffer->FileAttributes = ulFileAttribs;
1730
1731         if( DirectoryCB->NameInformation.FileName.Buffer[ 0] == L'.' &&
1732             BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_HIDE_DOT_NAMES))
1733         {
1734
1735             if ( Buffer->FileAttributes != FILE_ATTRIBUTE_NORMAL)
1736             {
1737
1738                 Buffer->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1739             }
1740             else
1741             {
1742
1743                 Buffer->FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1744             }
1745         }
1746
1747         if ( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_MOUNTPOINT)
1748         {
1749
1750             Buffer->ReparseTag = IO_REPARSE_TAG_SURROGATE|IO_REPARSE_TAG_OPENAFS_DFS;
1751         }
1752         else if( BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_REPARSE_POINT))
1753         {
1754
1755             Buffer->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1756         }
1757
1758         *Length -= sizeof( FILE_ATTRIBUTE_TAG_INFORMATION);
1759
1760         ntStatus = STATUS_SUCCESS;
1761     }
1762
1763     return ntStatus;
1764 }
1765
1766 NTSTATUS
1767 AFSQueryRemoteProtocolInfo( IN PIRP Irp,
1768                             IN AFSDirectoryCB *DirectoryCB,
1769                             IN OUT FILE_REMOTE_PROTOCOL_INFORMATION *Buffer,
1770                             IN OUT PLONG Length)
1771 {
1772
1773     UNREFERENCED_PARAMETER(Irp);
1774     UNREFERENCED_PARAMETER(DirectoryCB);
1775     NTSTATUS ntStatus = STATUS_BUFFER_TOO_SMALL;
1776
1777     if( *Length >= sizeof( FILE_REMOTE_PROTOCOL_INFORMATION))
1778     {
1779
1780         RtlZeroMemory( Buffer,
1781                        *Length);
1782
1783         Buffer->StructureVersion = 1;
1784
1785         Buffer->StructureSize = sizeof(FILE_REMOTE_PROTOCOL_INFORMATION);
1786
1787         Buffer->Protocol = WNNC_NET_OPENAFS;
1788
1789         Buffer->ProtocolMajorVersion = 3;
1790
1791         Buffer->ProtocolMinorVersion = 0;
1792
1793         Buffer->ProtocolRevision = 0;
1794
1795         *Length -= sizeof( FILE_REMOTE_PROTOCOL_INFORMATION);
1796
1797         ntStatus = STATUS_SUCCESS;
1798     }
1799
1800     return ntStatus;
1801 }
1802
1803 NTSTATUS
1804 AFSQueryPhysicalNameInfo( IN PIRP Irp,
1805                           IN AFSDirectoryCB *DirectoryCB,
1806                           IN OUT PFILE_NETWORK_PHYSICAL_NAME_INFORMATION Buffer,
1807                           IN OUT PLONG Length)
1808 {
1809
1810     UNREFERENCED_PARAMETER(DirectoryCB);
1811     NTSTATUS ntStatus = STATUS_SUCCESS;
1812     ULONG ulCopyLength = 0;
1813     ULONG cchCopied = 0;
1814     AFSFcb *pFcb = NULL;
1815     AFSCcb *pCcb = NULL;
1816     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1817     BOOLEAN bAddLeadingSlash = FALSE;
1818     USHORT usFullNameLength = 0;
1819
1820     pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1821
1822     pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1823
1824     if( *Length >= FIELD_OFFSET( FILE_NETWORK_PHYSICAL_NAME_INFORMATION, FileName))
1825     {
1826
1827         RtlZeroMemory( Buffer,
1828                        *Length);
1829
1830         if( pCcb->FullFileName.Length == 0 ||
1831             pCcb->FullFileName.Buffer[ 0] != L'\\')
1832         {
1833             bAddLeadingSlash = TRUE;
1834         }
1835
1836         usFullNameLength = pCcb->FullFileName.Length;
1837
1838         if( bAddLeadingSlash)
1839         {
1840             usFullNameLength += sizeof( WCHAR);
1841         }
1842
1843         if( *Length >= (LONG)(FIELD_OFFSET( FILE_NETWORK_PHYSICAL_NAME_INFORMATION, FileName) + (LONG)usFullNameLength))
1844         {
1845             ulCopyLength = (LONG)usFullNameLength;
1846         }
1847         else
1848         {
1849
1850             ulCopyLength = *Length - FIELD_OFFSET( FILE_NETWORK_PHYSICAL_NAME_INFORMATION, FileName);
1851
1852             ntStatus = STATUS_BUFFER_OVERFLOW;
1853         }
1854
1855         Buffer->FileNameLength = (ULONG)usFullNameLength;
1856
1857         *Length -= FIELD_OFFSET( FILE_NETWORK_PHYSICAL_NAME_INFORMATION, FileName);
1858
1859         if( ulCopyLength > 0)
1860         {
1861
1862             if( bAddLeadingSlash)
1863             {
1864
1865                 Buffer->FileName[ cchCopied] = L'\\';
1866
1867                 ulCopyLength -= sizeof( WCHAR);
1868                 *Length -= sizeof( WCHAR);
1869                 cchCopied++;
1870             }
1871
1872             if( ulCopyLength >= pCcb->FullFileName.Length)
1873             {
1874
1875                 RtlCopyMemory( &Buffer->FileName[ cchCopied],
1876                                pCcb->FullFileName.Buffer,
1877                                pCcb->FullFileName.Length);
1878
1879                 ulCopyLength -= pCcb->FullFileName.Length;
1880                 *Length -= pCcb->FullFileName.Length;
1881                 cchCopied += pCcb->FullFileName.Length/sizeof( WCHAR);
1882             }
1883             else
1884             {
1885
1886                 RtlCopyMemory( &Buffer->FileName[ cchCopied],
1887                                pCcb->FullFileName.Buffer,
1888                                ulCopyLength);
1889
1890                 *Length -= ulCopyLength;
1891             }
1892         }
1893     }
1894     else
1895     {
1896
1897         ntStatus = STATUS_BUFFER_TOO_SMALL;
1898     }
1899
1900     return ntStatus;
1901 }
1902
1903 NTSTATUS
1904 AFSSetBasicInfo( IN PIRP Irp,
1905                  IN AFSDirectoryCB *DirectoryCB)
1906 {
1907     NTSTATUS ntStatus = STATUS_SUCCESS;
1908     PFILE_BASIC_INFORMATION pBuffer;
1909     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1910     ULONG ulNotifyFilter = 0;
1911     AFSCcb *pCcb = NULL;
1912
1913     __Enter
1914     {
1915
1916         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1917
1918         pBuffer = (PFILE_BASIC_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
1919
1920         pCcb->FileUnwindInfo.FileAttributes = (ULONG)-1;
1921
1922         if( pBuffer->FileAttributes != (ULONGLONG)0)
1923         {
1924
1925             if( DirectoryCB->ObjectInformation->Fcb->Header.NodeTypeCode == AFS_FILE_FCB &&
1926                 BooleanFlagOn( pBuffer->FileAttributes, FILE_ATTRIBUTE_DIRECTORY))
1927             {
1928
1929                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
1930             }
1931
1932             if( DirectoryCB->ObjectInformation->Fcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
1933             {
1934
1935                 pBuffer->FileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
1936             }
1937
1938             pCcb->FileUnwindInfo.FileAttributes = DirectoryCB->ObjectInformation->FileAttributes;
1939
1940             DirectoryCB->ObjectInformation->FileAttributes = pBuffer->FileAttributes;
1941
1942             ulNotifyFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
1943
1944             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED);
1945         }
1946
1947         pCcb->FileUnwindInfo.CreationTime.QuadPart = (ULONGLONG)-1;
1948
1949         if( pBuffer->CreationTime.QuadPart != (ULONGLONG)-1 &&
1950             pBuffer->CreationTime.QuadPart != (ULONGLONG)0)
1951         {
1952
1953             pCcb->FileUnwindInfo.CreationTime.QuadPart = DirectoryCB->ObjectInformation->CreationTime.QuadPart;
1954
1955             DirectoryCB->ObjectInformation->CreationTime.QuadPart = pBuffer->CreationTime.QuadPart;
1956
1957             ulNotifyFilter |= FILE_NOTIFY_CHANGE_CREATION;
1958
1959             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CREATE_TIME);
1960         }
1961
1962         pCcb->FileUnwindInfo.LastAccessTime.QuadPart = (ULONGLONG)-1;
1963
1964         if( pBuffer->LastAccessTime.QuadPart != (ULONGLONG)-1 &&
1965             pBuffer->LastAccessTime.QuadPart != (ULONGLONG)0)
1966         {
1967
1968             pCcb->FileUnwindInfo.LastAccessTime.QuadPart = DirectoryCB->ObjectInformation->LastAccessTime.QuadPart;
1969
1970             DirectoryCB->ObjectInformation->LastAccessTime.QuadPart = pBuffer->LastAccessTime.QuadPart;
1971
1972             ulNotifyFilter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
1973
1974             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_ACCESS_TIME);
1975         }
1976
1977         pCcb->FileUnwindInfo.LastWriteTime.QuadPart = (ULONGLONG)-1;
1978
1979         if( pBuffer->LastWriteTime.QuadPart != (ULONGLONG)-1 &&
1980             pBuffer->LastWriteTime.QuadPart != (ULONGLONG)0)
1981         {
1982
1983             pCcb->FileUnwindInfo.LastWriteTime.QuadPart = DirectoryCB->ObjectInformation->LastWriteTime.QuadPart;
1984
1985             DirectoryCB->ObjectInformation->LastWriteTime.QuadPart = pBuffer->LastWriteTime.QuadPart;
1986
1987             ulNotifyFilter |= FILE_NOTIFY_CHANGE_LAST_WRITE;
1988
1989             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME);
1990         }
1991
1992         pCcb->FileUnwindInfo.ChangeTime.QuadPart = (ULONGLONG)-1;
1993
1994         if( pBuffer->ChangeTime.QuadPart != (ULONGLONG)-1 &&
1995             pBuffer->ChangeTime.QuadPart != (ULONGLONG)0)
1996         {
1997
1998             pCcb->FileUnwindInfo.ChangeTime.QuadPart = DirectoryCB->ObjectInformation->ChangeTime.QuadPart;
1999
2000             DirectoryCB->ObjectInformation->ChangeTime.QuadPart = pBuffer->ChangeTime.QuadPart;
2001
2002             ulNotifyFilter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
2003
2004             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
2005         }
2006
2007         if( ulNotifyFilter > 0)
2008         {
2009
2010             if( BooleanFlagOn( DirectoryCB->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
2011             {
2012
2013                 AFSObjectInfoCB * pParentObjectInfo = AFSFindObjectInfo( DirectoryCB->ObjectInformation->VolumeCB,
2014                                                                          &DirectoryCB->ObjectInformation->ParentFileId);
2015
2016                 if ( pParentObjectInfo != NULL)
2017                 {
2018                     AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
2019                                                     pCcb,
2020                                                     (ULONG)ulNotifyFilter,
2021                                                     (ULONG)FILE_ACTION_MODIFIED);
2022
2023                     AFSReleaseObjectInfo( &pParentObjectInfo);
2024                 }
2025             }
2026         }
2027
2028 try_exit:
2029
2030         NOTHING;
2031     }
2032
2033     return ntStatus;
2034 }
2035
2036 NTSTATUS
2037 AFSSetDispositionInfo( IN PIRP Irp,
2038                        IN AFSDirectoryCB *DirectoryCB)
2039 {
2040     NTSTATUS ntStatus = STATUS_SUCCESS;
2041     PFILE_DISPOSITION_INFORMATION pBuffer;
2042     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2043     AFSFcb *pFcb = NULL;
2044     AFSCcb *pCcb = NULL;
2045
2046     __Enter
2047     {
2048
2049         pBuffer = (PFILE_DISPOSITION_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
2050
2051         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
2052
2053         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
2054
2055         //
2056         // Can't delete the root
2057         //
2058
2059         if( pFcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2060         {
2061
2062             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2063                           AFS_TRACE_LEVEL_ERROR,
2064                           "AFSSetDispositionInfo Attempt to delete root entry\n"));
2065
2066             try_return( ntStatus = STATUS_CANNOT_DELETE);
2067         }
2068
2069         //
2070         // If the file is read only then do not allow the delete
2071         //
2072
2073         if( BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_READONLY))
2074         {
2075
2076             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2077                           AFS_TRACE_LEVEL_ERROR,
2078                           "AFSSetDispositionInfo Attempt to delete read only entry %wZ\n",
2079                           &DirectoryCB->NameInformation.FileName));
2080
2081             try_return( ntStatus = STATUS_CANNOT_DELETE);
2082         }
2083
2084         if( pBuffer->DeleteFile)
2085         {
2086
2087             //
2088             // Check if the caller can delete the file
2089             //
2090
2091             ntStatus = AFSNotifyDelete( DirectoryCB,
2092                                         &pCcb->AuthGroup,
2093                                         TRUE);
2094
2095             if( !NT_SUCCESS( ntStatus))
2096             {
2097
2098                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2099                               AFS_TRACE_LEVEL_ERROR,
2100                               "AFSSetDispositionInfo Cannot delete entry %wZ Status %08lX\n",
2101                               &DirectoryCB->NameInformation.FileName,
2102                               ntStatus));
2103
2104                 try_return( ntStatus);
2105             }
2106
2107             if( pFcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
2108             {
2109
2110                 //
2111                 // Reduce the Link count in the object information block
2112                 // to correspond with the deletion of the directory entry.
2113                 //
2114
2115                 pFcb->ObjectInformation->Links--;
2116
2117                 //
2118                 // Check if this is a directory that there are not currently other opens
2119                 //
2120
2121                 if( pFcb->ObjectInformation->Specific.Directory.ChildOpenHandleCount > 0)
2122                 {
2123
2124                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2125                                   AFS_TRACE_LEVEL_ERROR,
2126                                   "AFSSetDispositionInfo Attempt to delete directory %wZ with open %u handles\n",
2127                                   &DirectoryCB->NameInformation.FileName,
2128                                   pFcb->ObjectInformation->Specific.Directory.ChildOpenHandleCount));
2129
2130                     try_return( ntStatus = STATUS_DIRECTORY_NOT_EMPTY);
2131                 }
2132
2133                 if( !AFSIsDirectoryEmptyForDelete( pFcb))
2134                 {
2135
2136                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2137                                   AFS_TRACE_LEVEL_ERROR,
2138                                   "AFSSetDispositionInfo Attempt to delete non-empty directory %wZ\n",
2139                                   &DirectoryCB->NameInformation.FileName));
2140
2141                     try_return( ntStatus = STATUS_DIRECTORY_NOT_EMPTY);
2142                 }
2143
2144                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2145                               AFS_TRACE_LEVEL_VERBOSE,
2146                               "AFSSetDispositionInfo Setting PENDING_DELETE on DirEntry  %p Name %wZ\n",
2147                               DirectoryCB,
2148                               &DirectoryCB->NameInformation.FileName));
2149
2150                 SetFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2151             }
2152             else if( pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
2153             {
2154                 BOOLEAN bMmFlushed;
2155
2156                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
2157                               AFS_TRACE_LEVEL_VERBOSE,
2158                               "AFSSetDispositionInfo Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
2159                               &pFcb->NPFcb->SectionObjectResource,
2160                               PsGetCurrentThread()));
2161
2162                 AFSAcquireExcl( &pFcb->NPFcb->SectionObjectResource,
2163                                 TRUE);
2164
2165                 //
2166                 // Attempt to flush any outstanding data
2167                 //
2168
2169                 bMmFlushed = MmFlushImageSection( &pFcb->NPFcb->SectionObjectPointers,
2170                                                   MmFlushForDelete);
2171
2172                 if ( bMmFlushed)
2173                 {
2174
2175                     //
2176                     // Set PENDING_DELETE before CcPurgeCacheSection to avoid a
2177                     // deadlock with Trend Micro's Enterprise anti-virus product
2178                     // which attempts to open the file which is being deleted.
2179                     //
2180
2181                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2182                                   AFS_TRACE_LEVEL_VERBOSE,
2183                                   "AFSSetDispositionInfo Setting PENDING_DELETE on DirEntry %p Name %wZ\n",
2184                                   DirectoryCB,
2185                                   &DirectoryCB->NameInformation.FileName));
2186
2187                     SetFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2188
2189                     //
2190                     // Purge the cache as well
2191                     //
2192
2193                     if( pFcb->NPFcb->SectionObjectPointers.DataSectionObject != NULL)
2194                     {
2195
2196                         if ( !CcPurgeCacheSection( &pFcb->NPFcb->SectionObjectPointers,
2197                                                    NULL,
2198                                                    0,
2199                                                    TRUE))
2200                         {
2201
2202                             SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
2203                         }
2204                     }
2205                 }
2206
2207                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
2208                               AFS_TRACE_LEVEL_VERBOSE,
2209                               "AFSSetDispositionInfo Releasing Fcb SectionObject lock %p EXCL %08lX\n",
2210                               &pFcb->NPFcb->SectionObjectResource,
2211                               PsGetCurrentThread()));
2212
2213                 AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
2214
2215                 if ( !bMmFlushed)
2216                 {
2217
2218                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2219                                   AFS_TRACE_LEVEL_ERROR,
2220                                   "AFSSetDispositionInfo Failed to flush image section for delete Entry %wZ\n",
2221                                   &DirectoryCB->NameInformation.FileName));
2222
2223                     try_return( ntStatus = STATUS_CANNOT_DELETE);
2224                 }
2225             }
2226             else if( pFcb->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
2227                      pFcb->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
2228                      pFcb->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
2229                      pFcb->Header.NodeTypeCode == AFS_INVALID_FCB)
2230             {
2231
2232                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2233                               AFS_TRACE_LEVEL_VERBOSE,
2234                               "AFSSetDispositionInfo Setting PENDING_DELETE on DirEntry %p Name %wZ\n",
2235                               DirectoryCB,
2236                               &DirectoryCB->NameInformation.FileName));
2237
2238                 SetFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2239             }
2240         }
2241         else
2242         {
2243
2244             ClearFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2245         }
2246
2247         //
2248         // OK, should be good to go, set the flag in the file object
2249         //
2250
2251         pIrpSp->FileObject->DeletePending = pBuffer->DeleteFile;
2252
2253 try_exit:
2254
2255         NOTHING;
2256     }
2257
2258     return ntStatus;
2259 }
2260
2261 NTSTATUS
2262 AFSSetFileLinkInfo( IN PIRP Irp)
2263 {
2264
2265     NTSTATUS ntStatus = STATUS_SUCCESS;
2266     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2267     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2268     PFILE_LINK_INFORMATION pFileLinkInfo = NULL;
2269     PFILE_OBJECT pSrcFileObj = NULL;
2270     PFILE_OBJECT pTargetFileObj = pIrpSp->Parameters.SetFile.FileObject;
2271     AFSFcb *pSrcFcb = NULL, *pTargetDcb = NULL;
2272     AFSCcb *pSrcCcb = NULL, *pTargetDirCcb = NULL;
2273     AFSObjectInfoCB *pSrcObject = NULL;
2274     AFSObjectInfoCB *pSrcParentObject = NULL, *pTargetParentObject = NULL;
2275     UNICODE_STRING uniSourceName, uniTargetName;
2276     UNICODE_STRING uniFullTargetName, uniTargetParentName;
2277     BOOLEAN bCommonParent = FALSE;
2278     AFSDirectoryCB *pTargetDirEntry = NULL;
2279     AFSDirectoryCB *pNewTargetDirEntry = NULL;
2280     ULONG ulTargetCRC;
2281     BOOLEAN bTargetEntryExists = FALSE;
2282     LONG lCount;
2283     BOOLEAN bReleaseTargetDirLock = FALSE;
2284     ULONG ulNotificationAction = 0, ulNotifyFilter = 0;
2285
2286     __Enter
2287     {
2288
2289         pSrcFileObj = pIrpSp->FileObject;
2290
2291         pSrcFcb = (AFSFcb *)pSrcFileObj->FsContext;
2292         pSrcCcb = (AFSCcb *)pSrcFileObj->FsContext2;
2293
2294         pSrcObject = pSrcFcb->ObjectInformation;
2295
2296         if ( BooleanFlagOn( pSrcFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
2297         {
2298
2299             pSrcParentObject = AFSFindObjectInfo( pSrcFcb->ObjectInformation->VolumeCB,
2300                                                   &pSrcFcb->ObjectInformation->ParentFileId);
2301         }
2302
2303         if( pSrcParentObject == NULL)
2304         {
2305
2306             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2307                           AFS_TRACE_LEVEL_ERROR,
2308                           "AFSSetFileLinkInfo Unable to resolve SrcParentObject (INVALID_PARAMETER)\n"));
2309
2310             ASSERT( FALSE);
2311
2312             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2313         }
2314
2315         pFileLinkInfo = (PFILE_LINK_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
2316
2317         //
2318         // Perform some basic checks to ensure FS integrity
2319         //
2320
2321         if( pSrcFcb->Header.NodeTypeCode != AFS_FILE_FCB)
2322         {
2323
2324             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2325                           AFS_TRACE_LEVEL_ERROR,
2326                           "AFSSetFileLinkInfo Attempt to non-file (INVALID_PARAMETER)\n"));
2327
2328             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2329         }
2330
2331         if( pTargetFileObj == NULL)
2332         {
2333
2334             if ( pFileLinkInfo->RootDirectory)
2335             {
2336
2337                 //
2338                 // The target directory is provided by HANDLE
2339                 // RootDirectory is only set when the target directory is not the same
2340                 // as the source directory.
2341                 //
2342                 // AFS only supports hard links within a single directory.
2343                 //
2344                 // The IOManager should translate any Handle to a FileObject for us.
2345                 // However, the failure to receive a FileObject is treated as a fatal
2346                 // error.
2347                 //
2348
2349                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2350                               AFS_TRACE_LEVEL_ERROR,
2351                               "AFSSetFileLinkInfo Attempt to link %wZ to alternate directory by handle INVALID_PARAMETER\n",
2352                               &pSrcCcb->DirectoryCB->NameInformation.FileName));
2353
2354                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
2355             }
2356             else
2357             {
2358
2359                 uniFullTargetName.Length = (USHORT)pFileLinkInfo->FileNameLength;
2360
2361                 uniFullTargetName.Buffer = (PWSTR)&pFileLinkInfo->FileName;
2362
2363                 AFSRetrieveFinalComponent( &uniFullTargetName,
2364                                            &uniTargetName);
2365
2366                 AFSRetrieveParentPath( &uniFullTargetName,
2367                                        &uniTargetParentName);
2368
2369                 if ( uniTargetParentName.Length == 0)
2370                 {
2371
2372                     //
2373                     // This is a simple rename. Here the target directory is the same as the source parent directory
2374                     // and the name is retrieved from the system buffer information
2375                     //
2376
2377                     pTargetParentObject = pSrcParentObject;
2378                 }
2379                 else
2380                 {
2381                     //
2382                     // uniTargetParentName contains the directory the renamed object
2383                     // will be moved to.  Must obtain the TargetParentObject.
2384                     //
2385
2386                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2387                                   AFS_TRACE_LEVEL_ERROR,
2388                                   "AFSSetFileLinkInfo Attempt to link  %wZ to alternate directory %wZ (NOT_SAME_DEVICE)\n",
2389                                   &pSrcCcb->DirectoryCB->NameInformation.FileName,
2390                                   &uniFullTargetName));
2391
2392                     try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2393                 }
2394             }
2395
2396             pTargetDcb = pTargetParentObject->Fcb;
2397         }
2398         else
2399         {
2400
2401             //
2402             // So here we have the target directory taken from the targetfile object
2403             //
2404
2405             pTargetDcb = (AFSFcb *)pTargetFileObj->FsContext;
2406
2407             pTargetDirCcb = (AFSCcb *)pTargetFileObj->FsContext2;
2408
2409             pTargetParentObject = (AFSObjectInfoCB *)pTargetDcb->ObjectInformation;
2410
2411             //
2412             // Grab the target name which we setup in the IRP_MJ_CREATE handler. By how we set this up
2413             // it is only the target component of the rename operation
2414             //
2415
2416             uniTargetName = *((PUNICODE_STRING)&pTargetFileObj->FileName);
2417         }
2418
2419         //
2420         // The quick check to see if they are self linking.
2421         // Do the names match? Only do this where the parent directories are
2422         // the same
2423         //
2424
2425         if( pTargetParentObject == pSrcParentObject)
2426         {
2427
2428             if( FsRtlAreNamesEqual( &uniTargetName,
2429                                     &uniSourceName,
2430                                     FALSE,
2431                                     NULL))
2432             {
2433                 try_return( ntStatus = STATUS_SUCCESS);
2434             }
2435
2436             bCommonParent = TRUE;
2437         }
2438         else
2439         {
2440
2441             //
2442             // We do not allow cross-volume hard links
2443             //
2444
2445             if( pTargetParentObject->VolumeCB != pSrcObject->VolumeCB)
2446             {
2447
2448                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2449                               AFS_TRACE_LEVEL_ERROR,
2450                               "AFSSetFileLinkInfo Attempt to link to different volume %wZ\n",
2451                               &pSrcCcb->DirectoryCB->NameInformation.FileName));
2452
2453                 try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2454             }
2455         }
2456
2457         ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2458                                       FALSE);
2459
2460         AFSAcquireExcl( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
2461                         TRUE);
2462
2463         bReleaseTargetDirLock = TRUE;
2464
2465         AFSLocateCaseSensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead,
2466                                         ulTargetCRC,
2467                                         &pTargetDirEntry);
2468
2469         if( pTargetDirEntry == NULL)
2470         {
2471
2472             //
2473             // Missed so perform a case insensitive lookup
2474             //
2475
2476             ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2477                                           TRUE);
2478
2479             AFSLocateCaseInsensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead,
2480                                               ulTargetCRC,
2481                                               &pTargetDirEntry);
2482         }
2483
2484         if ( !BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES) &&
2485              pTargetDirEntry == NULL && RtlIsNameLegalDOS8Dot3( &uniTargetName,
2486                                                                 NULL,
2487                                                                 NULL))
2488         {
2489             //
2490             // Try the short name
2491             //
2492             AFSLocateShortNameDirEntry( pTargetParentObject->Specific.Directory.ShortNameTree,
2493                                         ulTargetCRC,
2494                                         &pTargetDirEntry);
2495         }
2496
2497         //
2498         // Increment our ref count on the dir entry
2499         //
2500
2501         if( pTargetDirEntry != NULL)
2502         {
2503
2504             ASSERT( BooleanFlagOn( pTargetDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
2505                     AFSIsEqualFID( &pTargetParentObject->FileId, &pTargetDirEntry->ObjectInformation->ParentFileId));
2506
2507             lCount = InterlockedIncrement( &pTargetDirEntry->DirOpenReferenceCount);
2508
2509             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2510                           AFS_TRACE_LEVEL_VERBOSE,
2511                           "AFSSetFileLinkInfo Increment count on %wZ DE %p Ccb %p Cnt %d\n",
2512                           &pTargetDirEntry->NameInformation.FileName,
2513                           pTargetDirEntry,
2514                           pSrcCcb,
2515                           lCount));
2516
2517             ASSERT( lCount >= 0);
2518
2519             if( !pFileLinkInfo->ReplaceIfExists)
2520             {
2521
2522                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2523                               AFS_TRACE_LEVEL_ERROR,
2524                               "AFSSetFileLinkInfo Attempt to link with target collision %wZ Target %wZ\n",
2525                               &pSrcCcb->DirectoryCB->NameInformation.FileName,
2526                               &pTargetDirEntry->NameInformation.FileName));
2527
2528                 try_return( ntStatus = STATUS_OBJECT_NAME_COLLISION);
2529             }
2530
2531             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2532                           AFS_TRACE_LEVEL_ERROR,
2533                           "AFSSetFileLinkInfo Target %wZ exists DE %p Count %d, performing delete of target\n",
2534                           &pTargetDirEntry->NameInformation.FileName,
2535                           pTargetDirEntry,
2536                           lCount));
2537
2538             //
2539             // Pull the directory entry from the parent
2540             //
2541
2542             AFSRemoveDirNodeFromParent( pTargetParentObject,
2543                                         pTargetDirEntry,
2544                                         FALSE);
2545
2546             bTargetEntryExists = TRUE;
2547         }
2548         else
2549         {
2550             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2551                           AFS_TRACE_LEVEL_VERBOSE,
2552                           "AFSSetFileLinkInfo Target does NOT exist, normal linking\n"));
2553         }
2554
2555         //
2556         // OK, this is a simple rename. Issue the rename
2557         // request to the service.
2558         //
2559
2560         ntStatus = AFSNotifyHardLink( pSrcFcb->ObjectInformation,
2561                                       &pSrcCcb->AuthGroup,
2562                                       pSrcParentObject,
2563                                       pTargetDcb->ObjectInformation,
2564                                       pSrcCcb->DirectoryCB,
2565                                       &uniTargetName,
2566                                       pFileLinkInfo->ReplaceIfExists,
2567                                       &pNewTargetDirEntry);
2568
2569         if( ntStatus != STATUS_REPARSE &&
2570             !NT_SUCCESS( ntStatus))
2571         {
2572
2573             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2574                           AFS_TRACE_LEVEL_ERROR,
2575                           "AFSSetFileLinkInfo Failed link of %wZ to target %wZ Status %08lX\n",
2576                           &pSrcCcb->DirectoryCB->NameInformation.FileName,
2577                           &uniTargetName,
2578                           ntStatus));
2579
2580             try_return( ntStatus);
2581         }
2582
2583         if ( ntStatus != STATUS_REPARSE)
2584         {
2585
2586             AFSInsertDirectoryNode( pTargetDcb->ObjectInformation,
2587                                     pNewTargetDirEntry,
2588                                     TRUE);
2589         }
2590
2591         //
2592         // Send notification for the target link file
2593         //
2594
2595         if( bTargetEntryExists || pNewTargetDirEntry)
2596         {
2597
2598             ulNotificationAction = FILE_ACTION_MODIFIED;
2599         }
2600         else
2601         {
2602
2603             ulNotificationAction = FILE_ACTION_ADDED;
2604         }
2605
2606         AFSFsRtlNotifyFullReportChange( pTargetParentObject,
2607                                         pSrcCcb,
2608                                         (ULONG)ulNotifyFilter,
2609                                         (ULONG)ulNotificationAction);
2610
2611       try_exit:
2612
2613         if( !NT_SUCCESS( ntStatus))
2614         {
2615
2616             if( bTargetEntryExists)
2617             {
2618
2619                 AFSInsertDirectoryNode( pTargetParentObject,
2620                                         pTargetDirEntry,
2621                                         FALSE);
2622             }
2623         }
2624
2625         if( pTargetDirEntry != NULL)
2626         {
2627
2628             //
2629             // Release DirOpenReferenceCount obtained above
2630             //
2631
2632             lCount = InterlockedDecrement( &pTargetDirEntry->DirOpenReferenceCount);
2633
2634             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2635                           AFS_TRACE_LEVEL_VERBOSE,
2636                           "AFSSetFileLinkInfo Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
2637                           &pTargetDirEntry->NameInformation.FileName,
2638                           pTargetDirEntry,
2639                           pSrcCcb,
2640                           lCount));
2641
2642             ASSERT( lCount >= 0);
2643         }
2644
2645         if( pNewTargetDirEntry != NULL)
2646         {
2647
2648             //
2649             // Release DirOpenReferenceCount obtained from AFSNotifyHardLink
2650             //
2651
2652             lCount = InterlockedDecrement( &pNewTargetDirEntry->DirOpenReferenceCount);
2653
2654             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2655                           AFS_TRACE_LEVEL_VERBOSE,
2656                           "AFSSetFileLinkInfo Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
2657                           &pNewTargetDirEntry->NameInformation.FileName,
2658                           pNewTargetDirEntry,
2659                           pSrcCcb,
2660                           lCount));
2661
2662             ASSERT( lCount >= 0);
2663         }
2664
2665         if( bReleaseTargetDirLock)
2666         {
2667
2668             AFSReleaseResource( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
2669         }
2670
2671         if ( pSrcParentObject != NULL)
2672         {
2673
2674             AFSReleaseObjectInfo( &pSrcParentObject);
2675         }
2676
2677         //
2678         // No need to release pTargetParentObject as it is either a copy of pSrcParentObject
2679         // or (AFSFcb *)pTargetFileObj->FsContext->ObjectInformation
2680         //
2681
2682         pTargetParentObject = NULL;
2683     }
2684
2685     return ntStatus;
2686 }
2687
2688 NTSTATUS
2689 AFSSetRenameInfo( IN PIRP Irp)
2690 {
2691
2692     NTSTATUS ntStatus = STATUS_SUCCESS;
2693     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2694     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2695     AFSFcb *pSrcFcb = NULL, *pTargetDcb = NULL, *pTargetFcb = NULL;
2696     AFSCcb *pSrcCcb = NULL, *pTargetDirCcb = NULL;
2697     PFILE_OBJECT pSrcFileObj = pIrpSp->FileObject;
2698     PFILE_OBJECT pTargetFileObj = pIrpSp->Parameters.SetFile.FileObject;
2699     PFILE_OBJECT pTargetParentFileObj = NULL;
2700     PFILE_RENAME_INFORMATION pRenameInfo = NULL;
2701     UNICODE_STRING uniTargetName, uniSourceName, uniTargetParentName;
2702     BOOLEAN bReplaceIfExists = FALSE;
2703     UNICODE_STRING uniShortName;
2704     AFSDirectoryCB *pTargetDirEntry = NULL;
2705     ULONG ulTargetCRC = 0;
2706     BOOLEAN bTargetEntryExists = FALSE;
2707     AFSObjectInfoCB *pSrcObject = NULL;
2708     AFSObjectInfoCB *pSrcParentObject = NULL, *pTargetParentObject = NULL;
2709     AFSFileID stNewFid;
2710     ULONG ulNotificationAction = 0, ulNotifyFilter = 0;
2711     UNICODE_STRING uniFullTargetName;
2712     BOOLEAN bCommonParent = FALSE;
2713     BOOLEAN bReleaseTargetDirLock = FALSE;
2714     BOOLEAN bReleaseSourceDirLock = FALSE;
2715     BOOLEAN bDereferenceTargetParentObject = FALSE;
2716     PERESOURCE  pSourceDirLock = NULL;
2717     LONG lCount;
2718
2719     __Enter
2720     {
2721
2722         bReplaceIfExists = pIrpSp->Parameters.SetFile.ReplaceIfExists;
2723
2724         pRenameInfo = (PFILE_RENAME_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
2725
2726         pSrcFcb = (AFSFcb *)pSrcFileObj->FsContext;
2727         pSrcCcb = (AFSCcb *)pSrcFileObj->FsContext2;
2728
2729         pSrcObject = pSrcFcb->ObjectInformation;
2730
2731         if ( BooleanFlagOn( pSrcFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
2732         {
2733
2734             pSrcParentObject = AFSFindObjectInfo( pSrcFcb->ObjectInformation->VolumeCB,
2735                                                   &pSrcFcb->ObjectInformation->ParentFileId);
2736         }
2737
2738         if( pSrcParentObject == NULL)
2739         {
2740
2741             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2742                           AFS_TRACE_LEVEL_ERROR,
2743                           "AFSSetRenameInfo Unable to resolve SrcParentObject (INVALID_PARAMETER)\n"));
2744
2745             ASSERT( FALSE);
2746
2747             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2748         }
2749
2750         //
2751         // Perform some basic checks to ensure FS integrity
2752         //
2753
2754         if( pSrcFcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2755         {
2756
2757             //
2758             // Can't rename the root directory
2759             //
2760
2761             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2762                           AFS_TRACE_LEVEL_ERROR,
2763                           "AFSSetRenameInfo Attempt to rename root entry\n"));
2764
2765             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2766         }
2767
2768         if( pSrcFcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
2769         {
2770
2771             //
2772             // If there are any open children then fail the rename
2773             //
2774
2775             if( pSrcFcb->ObjectInformation->Specific.Directory.ChildOpenHandleCount > 0)
2776             {
2777
2778                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2779                               AFS_TRACE_LEVEL_ERROR,
2780                               "AFSSetRenameInfo Attempt to rename directory with open children %wZ\n",
2781                               &pSrcCcb->DirectoryCB->NameInformation.FileName));
2782
2783                 try_return( ntStatus = STATUS_ACCESS_DENIED);
2784             }
2785         }
2786
2787
2788         //
2789         // Extract off the final component name from the Fcb
2790         //
2791
2792         uniSourceName.Length = (USHORT)pSrcCcb->DirectoryCB->NameInformation.FileName.Length;
2793         uniSourceName.MaximumLength = uniSourceName.Length;
2794
2795         uniSourceName.Buffer = pSrcCcb->DirectoryCB->NameInformation.FileName.Buffer;
2796
2797         //
2798         // Resolve the target fileobject
2799         //
2800
2801         if( pTargetFileObj == NULL)
2802         {
2803
2804             if ( pRenameInfo->RootDirectory)
2805             {
2806
2807                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2808                               AFS_TRACE_LEVEL_ERROR,
2809                               "AFSSetRenameInfo Handle provided but no FileObject ntStatus INVALID_PARAMETER\n"));
2810
2811                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
2812             }
2813             else
2814             {
2815
2816                 uniFullTargetName.Length = (USHORT)pRenameInfo->FileNameLength;
2817
2818                 uniFullTargetName.Buffer = (PWSTR)&pRenameInfo->FileName;
2819
2820                 AFSRetrieveFinalComponent( &uniFullTargetName,
2821                                            &uniTargetName);
2822
2823                 AFSRetrieveParentPath( &uniFullTargetName,
2824                                        &uniTargetParentName);
2825
2826                 if ( uniTargetParentName.Length == 0)
2827                 {
2828
2829                     //
2830                     // This is a simple rename. Here the target directory is the same as the source parent directory
2831                     // and the name is retrieved from the system buffer information
2832                     //
2833
2834                     pTargetParentObject = pSrcParentObject;
2835                 }
2836                 else
2837                 {
2838                     //
2839                     // uniTargetParentName contains the directory the renamed object
2840                     // will be moved to.  Must obtain the TargetParentObject.
2841                     //
2842
2843                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2844                                   AFS_TRACE_LEVEL_ERROR,
2845                                   "AFSSetRenameInfo Attempt to move %wZ to %wZ -- not yet supported (NOT_SAME_DEVICE)\n",
2846                                   &pSrcCcb->DirectoryCB->NameInformation.FileName,
2847                                   &uniFullTargetName));
2848
2849                     try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2850                 }
2851             }
2852
2853             pTargetDcb = pTargetParentObject->Fcb;
2854         }
2855         else
2856         {
2857
2858             //
2859             // So here we have the target directory taken from the targetfile object
2860             //
2861
2862             pTargetDcb = (AFSFcb *)pTargetFileObj->FsContext;
2863
2864             pTargetDirCcb = (AFSCcb *)pTargetFileObj->FsContext2;
2865
2866             pTargetParentObject = (AFSObjectInfoCB *)pTargetDcb->ObjectInformation;
2867
2868             //
2869             // Grab the target name which we setup in the IRP_MJ_CREATE handler. By how we set this up
2870             // it is only the target component of the rename operation
2871             //
2872
2873             uniTargetName = *((PUNICODE_STRING)&pTargetFileObj->FileName);
2874         }
2875
2876         //
2877         // The quick check to see if they are not really performing a rename
2878         // Do the names match? Only do this where the parent directories are
2879         // the same
2880         //
2881
2882         if( pTargetParentObject == pSrcParentObject)
2883         {
2884
2885             if( FsRtlAreNamesEqual( &uniTargetName,
2886                                     &uniSourceName,
2887                                     FALSE,
2888                                     NULL))
2889             {
2890                 try_return( ntStatus = STATUS_SUCCESS);
2891             }
2892
2893             bCommonParent = TRUE;
2894         }
2895         else
2896         {
2897
2898             bCommonParent = FALSE;
2899         }
2900
2901         //
2902         // We do not allow cross-volume renames to occur
2903         //
2904
2905         if( pTargetParentObject->VolumeCB != pSrcObject->VolumeCB)
2906         {
2907
2908             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2909                           AFS_TRACE_LEVEL_ERROR,
2910                           "AFSSetRenameInfo Attempt to rename directory to different volume %wZ\n",
2911                           &pSrcCcb->DirectoryCB->NameInformation.FileName));
2912
2913             try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2914         }
2915
2916         ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2917                                       FALSE);
2918
2919         AFSAcquireExcl( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
2920                         TRUE);
2921
2922         bReleaseTargetDirLock = TRUE;
2923
2924         if( pTargetParentObject != pSrcParentObject)
2925         {
2926             AFSAcquireExcl( pSrcParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
2927                             TRUE);
2928
2929             bReleaseSourceDirLock = TRUE;
2930
2931             pSourceDirLock = pSrcParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock;
2932         }
2933
2934         AFSLocateCaseSensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead,
2935                                         ulTargetCRC,
2936                                         &pTargetDirEntry);
2937
2938         if( pTargetDirEntry == NULL)
2939         {
2940
2941             //
2942             // Missed so perform a case insensitive lookup
2943             //
2944
2945             ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2946                                           TRUE);
2947
2948             AFSLocateCaseInsensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead,
2949                                               ulTargetCRC,
2950                                               &pTargetDirEntry);
2951         }
2952
2953         if ( !BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES) &&
2954              pTargetDirEntry == NULL && RtlIsNameLegalDOS8Dot3( &uniTargetName,
2955                                                                NULL,
2956                                                                NULL))
2957         {
2958             //
2959             // Try the short name
2960             //
2961             AFSLocateShortNameDirEntry( pTargetParentObject->Specific.Directory.ShortNameTree,
2962                                         ulTargetCRC,
2963                                         &pTargetDirEntry);
2964         }
2965
2966         //
2967         // Increment our ref count on the dir entry
2968         //
2969
2970         if( pTargetDirEntry != NULL)
2971         {
2972
2973             ASSERT( BooleanFlagOn( pTargetDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
2974                     AFSIsEqualFID( &pTargetParentObject->FileId, &pTargetDirEntry->ObjectInformation->ParentFileId));
2975
2976             lCount = InterlockedIncrement( &pTargetDirEntry->DirOpenReferenceCount);
2977
2978             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2979                           AFS_TRACE_LEVEL_VERBOSE,
2980                           "AFSSetRenameInfo Increment count on %wZ DE %p Ccb %p Cnt %d\n",
2981                           &pTargetDirEntry->NameInformation.FileName,
2982                           pTargetDirEntry,
2983                           pSrcCcb,
2984                           lCount));
2985
2986             ASSERT( lCount >= 0);
2987
2988             if( !bReplaceIfExists)
2989             {
2990
2991                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2992                               AFS_TRACE_LEVEL_ERROR,
2993                               "AFSSetRenameInfo Attempt to rename directory with target collision %wZ Target %wZ\n",
2994                               &pSrcCcb->DirectoryCB->NameInformation.FileName,
2995                               &pTargetDirEntry->NameInformation.FileName));
2996
2997                 try_return( ntStatus = STATUS_OBJECT_NAME_COLLISION);
2998             }
2999
3000             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
3001                           AFS_TRACE_LEVEL_ERROR,
3002                           "AFSSetRenameInfo Target %wZ exists DE %p Count %d, performing delete of target\n",
3003                           &pTargetDirEntry->NameInformation.FileName,
3004                           pTargetDirEntry,
3005                           lCount));
3006
3007             //
3008             // Pull the directory entry from the parent
3009             //
3010
3011             AFSRemoveDirNodeFromParent( pTargetParentObject,
3012                                         pTargetDirEntry,
3013                                         FALSE);
3014
3015             bTargetEntryExists = TRUE;
3016         }
3017         else
3018         {
3019             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3020                           AFS_TRACE_LEVEL_VERBOSE,
3021                           "AFSSetRenameInfo Target does NOT exist, normal rename\n"));
3022         }
3023
3024         //
3025         // We need to remove the DirEntry from the parent node, update the index
3026         // and reinsert it into the parent tree. Note that for entries with the
3027         // same parent we do not pull the node from the enumeration list
3028         //
3029
3030         AFSRemoveDirNodeFromParent( pSrcParentObject,
3031                                     pSrcCcb->DirectoryCB,
3032                                     !bCommonParent);
3033
3034         //
3035         // OK, this is a simple rename. Issue the rename
3036         // request to the service.
3037         //
3038
3039         ntStatus = AFSNotifyRename( pSrcFcb->ObjectInformation,
3040                                     &pSrcCcb->AuthGroup,
3041                                     pSrcParentObject,
3042                                     pTargetDcb->ObjectInformation,
3043                                     pSrcCcb->DirectoryCB,
3044                                     &uniTargetName,
3045                                     &stNewFid);
3046
3047         if( !NT_SUCCESS( ntStatus))
3048         {
3049
3050             //
3051             // Attempt to re-insert the directory entry
3052             //
3053
3054             AFSInsertDirectoryNode( pSrcParentObject,
3055                                     pSrcCcb->DirectoryCB,
3056                                     !bCommonParent);
3057
3058             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3059                           AFS_TRACE_LEVEL_ERROR,
3060                           "AFSSetRenameInfo Failed rename of %wZ to target %wZ Status %08lX\n",
3061                           &pSrcCcb->DirectoryCB->NameInformation.FileName,
3062                           &uniTargetName,
3063                           ntStatus));
3064
3065             try_return( ntStatus);
3066         }
3067
3068         //
3069         // Set the notification up for the source file
3070         //
3071
3072         if( pSrcParentObject == pTargetParentObject &&
3073             !bTargetEntryExists)
3074         {
3075
3076             ulNotificationAction = FILE_ACTION_RENAMED_OLD_NAME;
3077         }
3078         else
3079         {
3080
3081             ulNotificationAction = FILE_ACTION_REMOVED;
3082         }
3083
3084         if( pSrcFcb->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY)
3085         {
3086
3087             ulNotifyFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
3088         }
3089         else
3090         {
3091
3092             ulNotifyFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
3093         }
3094
3095         AFSFsRtlNotifyFullReportChange( pSrcParentObject,
3096                                         pSrcCcb,
3097                                         (ULONG)ulNotifyFilter,
3098                                         (ULONG)ulNotificationAction);
3099
3100         //
3101         // Update the name in the dir entry.
3102         //
3103
3104         ntStatus = AFSUpdateDirEntryName( pSrcCcb->DirectoryCB,
3105                                           &uniTargetName);
3106
3107         if( !NT_SUCCESS( ntStatus))
3108         {
3109
3110             //
3111             // Attempt to re-insert the directory entry
3112             //
3113
3114             AFSInsertDirectoryNode( pSrcParentObject,
3115                                     pSrcCcb->DirectoryCB,
3116                                     !bCommonParent);
3117
3118             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3119                           AFS_TRACE_LEVEL_ERROR,
3120                           "AFSSetRenameInfo Failed update of dir entry %wZ to target %wZ Status %08lX\n",
3121                           &pSrcCcb->DirectoryCB->NameInformation.FileName,
3122                           &uniTargetName,
3123                           ntStatus));
3124
3125             try_return( ntStatus);
3126         }
3127
3128         //
3129         // Update the object information block, if needed
3130         //
3131
3132         if( !AFSIsEqualFID( &pSrcObject->FileId,
3133                             &stNewFid))
3134         {
3135
3136             AFSAcquireExcl( pSrcObject->VolumeCB->ObjectInfoTree.TreeLock,
3137                             TRUE);
3138
3139             //
3140             // Remove the old information entry
3141             //
3142
3143             AFSRemoveHashEntry( &pSrcObject->VolumeCB->ObjectInfoTree.TreeHead,
3144                                 &pSrcObject->TreeEntry);
3145
3146             RtlCopyMemory( &pSrcObject->FileId,
3147                            &stNewFid,
3148                            sizeof( AFSFileID));
3149
3150             //
3151             // Insert the entry into the new object table.
3152             //
3153
3154             pSrcObject->TreeEntry.HashIndex = AFSCreateLowIndex( &pSrcObject->FileId);
3155
3156             if( pSrcObject->VolumeCB->ObjectInfoTree.TreeHead == NULL)
3157             {
3158
3159                 pSrcObject->VolumeCB->ObjectInfoTree.TreeHead = &pSrcObject->TreeEntry;
3160             }
3161             else
3162             {
3163
3164                 if ( !NT_SUCCESS( AFSInsertHashEntry( pSrcObject->VolumeCB->ObjectInfoTree.TreeHead,
3165                                                      &pSrcObject->TreeEntry)))
3166                 {
3167
3168                     //
3169                     // Lost a race, an ObjectInfo object already exists for this FID.
3170                     // Let this copy be garbage collected.
3171                     //
3172
3173                     ClearFlag( pSrcObject->Flags, AFS_OBJECT_INSERTED_HASH_TREE);
3174                 }
3175             }
3176
3177             AFSReleaseResource( pSrcObject->VolumeCB->ObjectInfoTree.TreeLock);
3178         }
3179
3180         //
3181         // Update the hash values for the name trees.
3182         //
3183
3184         pSrcCcb->DirectoryCB->CaseSensitiveTreeEntry.HashIndex = AFSGenerateCRC( &pSrcCcb->DirectoryCB->NameInformation.FileName,
3185                                                                                  FALSE);
3186
3187         pSrcCcb->DirectoryCB->CaseInsensitiveTreeEntry.HashIndex = AFSGenerateCRC( &pSrcCcb->DirectoryCB->NameInformation.FileName,
3188                                                                                    TRUE);
3189
3190         if( !BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES) &&
3191             pSrcCcb->DirectoryCB->NameInformation.ShortNameLength > 0 &&
3192             !RtlIsNameLegalDOS8Dot3( &pSrcCcb->DirectoryCB->NameInformation.FileName,
3193                                      NULL,
3194                                      NULL))
3195         {
3196
3197             uniShortName.Length = pSrcCcb->DirectoryCB->NameInformation.ShortNameLength;
3198             uniShortName.MaximumLength = uniShortName.Length;
3199             uniShortName.Buffer = pSrcCcb->DirectoryCB->NameInformation.ShortName;
3200
3201             pSrcCcb->DirectoryCB->Type.Data.ShortNameTreeEntry.HashIndex = AFSGenerateCRC( &uniShortName,
3202                                                                                            TRUE);
3203
3204             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3205                           AFS_TRACE_LEVEL_VERBOSE,
3206                           "AFSSetRenameInfo Initialized short name hash for %wZ longname %wZ\n",
3207                           &uniShortName,
3208                           &pSrcCcb->DirectoryCB->NameInformation.FileName));
3209         }
3210         else
3211         {
3212
3213             pSrcCcb->DirectoryCB->Type.Data.ShortNameTreeEntry.HashIndex = 0;
3214         }
3215
3216         if( !bCommonParent)
3217         {
3218
3219             //
3220             // Update the file index for the object in the new parent
3221             //
3222
3223             pSrcCcb->DirectoryCB->FileIndex = (ULONG)InterlockedIncrement( &pTargetParentObject->Specific.Directory.DirectoryNodeHdr.ContentIndex);
3224         }
3225
3226         //
3227         // Re-insert the directory entry
3228         //
3229
3230         AFSInsertDirectoryNode( pTargetParentObject,
3231                                 pSrcCcb->DirectoryCB,
3232                                 !bCommonParent);
3233
3234         //
3235         // Update the parent pointer in the source object if they are different
3236         //
3237
3238         if( pSrcParentObject != pTargetParentObject)
3239         {
3240
3241             lCount = InterlockedDecrement( &pSrcParentObject->Specific.Directory.ChildOpenHandleCount);
3242
3243             lCount = InterlockedDecrement( &pSrcParentObject->Specific.Directory.ChildOpenReferenceCount);
3244
3245             lCount = InterlockedIncrement( &pTargetParentObject->Specific.Directory.ChildOpenHandleCount);
3246
3247             lCount = InterlockedIncrement( &pTargetParentObject->Specific.Directory.ChildOpenReferenceCount);
3248
3249             lCount = AFSObjectInfoIncrement( pTargetParentObject,
3250                                              AFS_OBJECT_REFERENCE_CHILD);
3251
3252             AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
3253                           AFS_TRACE_LEVEL_VERBOSE,
3254                           "AFSSetRenameInfo Increment count on parent object %p Cnt %d\n",
3255                           pTargetParentObject,
3256                           lCount));
3257
3258             lCount = AFSObjectInfoDecrement( pSrcParentObject,
3259                                              AFS_OBJECT_REFERENCE_CHILD);
3260
3261             AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
3262                           AFS_TRACE_LEVEL_VERBOSE,
3263                           "AFSSetRenameInfo Decrement count on parent object %p Cnt %d\n",
3264                           pSrcParentObject,
3265                           lCount));
3266
3267             pSrcFcb->ObjectInformation->ParentFileId = pTargetParentObject->FileId;
3268
3269             SetFlag( pSrcFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID);
3270
3271             ulNotificationAction = FILE_ACTION_ADDED;
3272         }
3273         else
3274         {
3275
3276             ulNotificationAction = FILE_ACTION_RENAMED_NEW_NAME;
3277         }
3278
3279         //
3280         // Now update the notification for the target file
3281         //
3282
3283         AFSFsRtlNotifyFullReportChange( pTargetParentObject,
3284                                         pSrcCcb,
3285                                         (ULONG)ulNotifyFilter,
3286                                         (ULONG)ulNotificationAction);
3287
3288         //
3289         // If we performed the rename of the target because it existed, we now need to
3290         // delete the tmp target we created above
3291         //
3292
3293         if( bTargetEntryExists)
3294         {
3295
3296             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3297                           AFS_TRACE_LEVEL_VERBOSE,
3298                           "AFSSetRenameInfo Setting DELETE flag in dir entry %p name %wZ\n",
3299                           pTargetDirEntry,
3300                           &pTargetDirEntry->NameInformation.FileName));
3301
3302             SetFlag( pTargetDirEntry->Flags, AFS_DIR_ENTRY_DELETED);
3303
3304             //
3305             // Try and purge the cache map if this is a file
3306             //
3307
3308             if( pTargetDirEntry->ObjectInformation->FileType == AFS_FILE_TYPE_FILE &&
3309                 pTargetDirEntry->ObjectInformation->Fcb != NULL &&
3310                 pTargetDirEntry->DirOpenReferenceCount > 1)
3311             {
3312
3313                 pTargetFcb = pTargetDirEntry->ObjectInformation->Fcb;
3314             }
3315
3316             ASSERT( pTargetDirEntry->DirOpenReferenceCount > 0);
3317
3318             lCount = InterlockedDecrement( &pTargetDirEntry->DirOpenReferenceCount); // The count we added above
3319
3320             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
3321                           AFS_TRACE_LEVEL_VERBOSE,
3322                           "AFSSetRenameInfo Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
3323                           &pTargetDirEntry->NameInformation.FileName,
3324                           pTargetDirEntry,
3325                           pSrcCcb,
3326                           lCount));
3327
3328             ASSERT( lCount >= 0);
3329
3330             if( lCount == 0 &&
3331                 pTargetDirEntry->NameArrayReferenceCount <= 0)
3332             {
3333
3334                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3335                               AFS_TRACE_LEVEL_VERBOSE,
3336                               "AFSSetRenameInfo Deleting dir entry %p name %wZ\n",
3337                               pTargetDirEntry,
3338                               &pTargetDirEntry->NameInformation.FileName));
3339
3340                 AFSDeleteDirEntry( pTargetParentObject,
3341                                    &pTargetDirEntry);
3342             }
3343
3344             pTargetDirEntry = NULL;
3345
3346             if ( pTargetFcb != NULL)
3347             {
3348
3349                 //
3350                 // Do not hold TreeLocks across the MmForceSectionClosed() call as
3351                 // it can deadlock with Trend Micro's TmPreFlt!TmpQueryFullName
3352                 //
3353
3354                 if( bReleaseTargetDirLock)
3355                 {
3356                     AFSReleaseResource( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
3357
3358                     bReleaseTargetDirLock = FALSE;
3359                 }
3360
3361                 if( bReleaseSourceDirLock)
3362                 {
3363
3364                     AFSReleaseResource( pSourceDirLock);
3365
3366                     bReleaseSourceDirLock = FALSE;
3367                 }
3368
3369                 //
3370                 // MmForceSectionClosed() can eventually call back into AFSCleanup
3371                 // which will need to acquire Fcb->Resource exclusively.  Failure
3372                 // to obtain it here before holding the SectionObjectResource will
3373                 // permit the locks to be obtained out of order risking a deadlock.
3374                 //
3375
3376                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
3377                               AFS_TRACE_LEVEL_VERBOSE,
3378                               "AFSSetRenameInfo Acquiring Fcb lock %p EXCL %08lX\n",
3379                               &pTargetFcb->NPFcb->Resource,
3380                               PsGetCurrentThread()));
3381
3382                 AFSAcquireExcl( &pTargetFcb->NPFcb->Resource,
3383                                 TRUE);
3384
3385                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
3386                               AFS_TRACE_LEVEL_VERBOSE,
3387                               "AFSSetRenameInfo Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
3388                               &pTargetFcb->NPFcb->SectionObjectResource,
3389                               PsGetCurrentThread()));
3390
3391                 AFSAcquireExcl( &pTargetFcb->NPFcb->SectionObjectResource,
3392                                 TRUE);
3393
3394                 //
3395                 // Close the section in the event it was mapped
3396                 //
3397
3398                 if( !MmForceSectionClosed( &pTargetFcb->NPFcb->SectionObjectPointers,
3399                                            TRUE))
3400                 {
3401
3402                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3403                                   AFS_TRACE_LEVEL_ERROR,
3404                                   "AFSSetRenameInfo Failed to delete section for target file %wZ\n",
3405                                   &uniTargetName));
3406                 }
3407
3408                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
3409                               AFS_TRACE_LEVEL_VERBOSE,
3410                               "AFSSetRenameInfo Releasing Fcb SectionObject lock %p EXCL %08lX\n",
3411                               &pTargetFcb->NPFcb->SectionObjectResource,
3412                               PsGetCurrentThread()));
3413
3414                 AFSReleaseResource( &pTargetFcb->NPFcb->SectionObjectResource);
3415
3416                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
3417                               AFS_TRACE_LEVEL_VERBOSE,
3418                               "AFSSetRenameInfo Releasing Fcb lock %p EXCL %08lX\n",
3419                               &pTargetFcb->NPFcb->Resource,
3420                               PsGetCurrentThread()));
3421
3422                 AFSReleaseResource( &pTargetFcb->NPFcb->Resource);
3423             }
3424         }
3425
3426 try_exit:
3427
3428         if( !NT_SUCCESS( ntStatus))
3429         {
3430
3431             if( bTargetEntryExists)
3432             {
3433
3434                 ASSERT( pTargetParentObject != NULL);
3435
3436                 AFSInsertDirectoryNode( pTargetParentObject,
3437                                         pTargetDirEntry,
3438                                         FALSE);
3439             }
3440         }
3441
3442         if( pTargetDirEntry != NULL)
3443         {
3444
3445             lCount = InterlockedDecrement( &pTargetDirEntry->DirOpenReferenceCount);
3446
3447             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
3448                           AFS_TRACE_LEVEL_VERBOSE,
3449                           "AFSSetRenameInfo Decrement2 count on %wZ DE %p Ccb %p Cnt %d\n",
3450                           &pTargetDirEntry->NameInformation.FileName,
3451                           pTargetDirEntry,
3452                           pSrcCcb,
3453                           lCount));
3454
3455             ASSERT( lCount >= 0);
3456         }
3457
3458         if( bReleaseTargetDirLock)
3459         {
3460
3461             AFSReleaseResource( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
3462         }
3463
3464         if( bReleaseSourceDirLock)
3465         {
3466
3467             AFSReleaseResource( pSourceDirLock);
3468         }
3469
3470         if ( bDereferenceTargetParentObject)
3471         {
3472
3473             ObDereferenceObject( pTargetParentFileObj);
3474         }
3475
3476         if ( pSrcParentObject != NULL)
3477         {
3478
3479             AFSReleaseObjectInfo( &pSrcParentObject);
3480         }
3481
3482         //
3483         // No need to release pTargetParentObject as it is either a copy of pSrcParentObject
3484         // or (AFSFcb *)pTargetFileObj->FsContext->ObjectInformation
3485         //
3486
3487         pTargetParentObject = NULL;
3488     }
3489
3490     return ntStatus;
3491 }
3492
3493 NTSTATUS
3494 AFSSetPositionInfo( IN PIRP Irp,
3495                     IN AFSDirectoryCB *DirectoryCB)
3496 {
3497     UNREFERENCED_PARAMETER(DirectoryCB);
3498     NTSTATUS ntStatus = STATUS_SUCCESS;
3499     PFILE_POSITION_INFORMATION pBuffer;
3500     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
3501
3502     pBuffer = (PFILE_POSITION_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
3503
3504     pIrpSp->FileObject->CurrentByteOffset.QuadPart = pBuffer->CurrentByteOffset.QuadPart;
3505
3506     return ntStatus;
3507 }
3508
3509 NTSTATUS
3510 AFSSetAllocationInfo( IN PIRP Irp,
3511                       IN AFSDirectoryCB *DirectoryCB)
3512 {
3513     UNREFERENCED_PARAMETER(DirectoryCB);
3514     NTSTATUS ntStatus = STATUS_SUCCESS;
3515     PFILE_ALLOCATION_INFORMATION pBuffer;
3516     BOOLEAN bReleasePaging = FALSE;
3517     BOOLEAN bTellCc = FALSE;
3518     BOOLEAN bTellService = FALSE;
3519     BOOLEAN bUserMapped = FALSE;
3520     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
3521     PFILE_OBJECT pFileObject = pIrpSp->FileObject;
3522     AFSFcb *pFcb = NULL;
3523     AFSCcb *pCcb = NULL;
3524     LARGE_INTEGER liSaveAlloc;
3525     LARGE_INTEGER liSaveFileSize;
3526     LARGE_INTEGER liSaveVDL;
3527
3528     pBuffer = (PFILE_ALLOCATION_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
3529
3530     pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
3531
3532     pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
3533
3534     //
3535     // save values to put back
3536     //
3537     liSaveAlloc = pFcb->Header.AllocationSize;
3538     liSaveFileSize = pFcb->Header.FileSize;
3539     liSaveVDL = pFcb->Header.ValidDataLength;
3540
3541     if( pFcb->Header.AllocationSize.QuadPart == pBuffer->AllocationSize.QuadPart ||
3542         pIrpSp->Parameters.SetFile.AdvanceOnly)
3543     {
3544         return STATUS_SUCCESS ;
3545     }
3546
3547     if( pFcb->Header.AllocationSize.QuadPart > pBuffer->AllocationSize.QuadPart)
3548     {
3549
3550         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
3551                       AFS_TRACE_LEVEL_VERBOSE,
3552                       "AFSSetAllocationInfo Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
3553                       &pFcb->NPFcb->SectionObjectResource,
3554                       PsGetCurrentThread()));
3555
3556         AFSAcquireExcl( &pFcb->NPFcb->SectionObjectResource,
3557                         TRUE);
3558
3559         bUserMapped = !MmCanFileBeTruncated( pFileObject->SectionObjectPointer,
3560                                              &pBuffer->AllocationSize);
3561
3562         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
3563                       AFS_TRACE_LEVEL_VERBOSE,
3564                       "AFSSetAllocationInfo Releasing Fcb SectionObject lock %p EXCL %08lX\n",
3565                       &pFcb->NPFcb->SectionObjectResource,
3566                       PsGetCurrentThread()));
3567
3568         AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
3569
3570         //
3571         // Truncating the file
3572         //
3573         if ( bUserMapped)
3574         {
3575
3576             ntStatus = STATUS_USER_MAPPED_FILE ;
3577         }
3578         else
3579         {
3580
3581             //
3582             // If this is a truncation we need to grab the paging IO resource.
3583             //
3584
3585             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
3586                           AFS_TRACE_LEVEL_VERBOSE,
3587                           "AFSSetAllocationInfo Acquiring Fcb PagingIo lock %p EXCL %08lX\n",
3588                           &pFcb->NPFcb->PagingResource,
3589                           PsGetCurrentThread()));
3590
3591             AFSAcquireExcl( &pFcb->NPFcb->PagingResource,
3592                             TRUE);
3593
3594             bReleasePaging = TRUE;
3595
3596             //
3597             // Must drop the Fcb Resource.  When changing the file size
3598             // a deadlock can occur with Trend Micro's filter if the file
3599             // size is set to zero.
3600             //
3601
3602             AFSReleaseResource( &pFcb->NPFcb->Resource);
3603
3604             pFcb->Header.AllocationSize = pBuffer->AllocationSize;
3605
3606             pFcb->ObjectInformation->AllocationSize = pBuffer->AllocationSize;
3607
3608             //
3609             // Tell Cc that Allocation is moved.
3610             //
3611             bTellCc = TRUE;
3612
3613             if( pFcb->Header.FileSize.QuadPart > pBuffer->AllocationSize.QuadPart)
3614             {
3615                 //
3616                 // We are pulling the EOF back as well so we need to tell
3617                 // the service.
3618                 //
3619                 bTellService = TRUE;
3620
3621                 pFcb->Header.FileSize = pBuffer->AllocationSize;
3622
3623                 pFcb->ObjectInformation->EndOfFile = pBuffer->AllocationSize;
3624             }
3625
3626         }
3627     }
3628     else
3629     {
3630         //
3631         // Tell Cc if allocation is increased.
3632         //
3633
3634         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
3635                       AFS_TRACE_LEVEL_VERBOSE,
3636                       "AFSSetAllocationInfo Acquiring Fcb PagingIo lock %p EXCL %08lX\n",
3637                       &pFcb->NPFcb->PagingResource,
3638                       PsGetCurrentThread()));
3639
3640         AFSAcquireExcl( &pFcb->NPFcb->PagingResource,
3641                         TRUE);
3642
3643         bReleasePaging = TRUE;
3644
3645         //
3646         // Must drop the Fcb Resource.  When changing the file size
3647         // a deadlock can occur with Trend Micro's filter if the file
3648         // size is set to zero.
3649         //
3650
3651         AFSReleaseResource( &pFcb->NPFcb->Resource);
3652
3653         bTellCc = pBuffer->AllocationSize.QuadPart > pFcb->Header.AllocationSize.QuadPart;
3654
3655         pFcb->Header.AllocationSize = pBuffer->AllocationSize;
3656
3657         pFcb->ObjectInformation->AllocationSize = pBuffer->AllocationSize;
3658     }
3659
3660     //
3661     // Now Tell the server if we have to
3662     //
3663     if (bTellService)
3664     {
3665
3666         ASSERT( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID));
3667
3668         ntStatus = AFSUpdateFileInformation( &pFcb->ObjectInformation->ParentFileId,
3669                                              pFcb->ObjectInformation,
3670                                              &pCcb->AuthGroup);
3671     }
3672
3673     if (NT_SUCCESS(ntStatus))
3674     {
3675         //
3676         // Trim extents if we told the service - the update has done an implicit
3677         // trim at the service.
3678         //
3679         if (bTellService)
3680         {
3681             AFSTrimExtents( pFcb,
3682                             &pFcb->Header.FileSize);
3683         }
3684
3685         KeQuerySystemTime( &pFcb->ObjectInformation->ChangeTime);
3686
3687         SetFlag( pFcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
3688
3689         if (bTellCc &&
3690             CcIsFileCached( pFileObject))
3691         {
3692             CcSetFileSizes( pFileObject,
3693                             (PCC_FILE_SIZES)&pFcb->Header.AllocationSize);
3694         }
3695     }
3696     else
3697     {
3698         //
3699         // Put the saved values back
3700         //
3701         pFcb->Header.ValidDataLength = liSaveVDL;
3702         pFcb->Header.FileSize = liSaveFileSize;
3703         pFcb->Header.AllocationSize = liSaveAlloc;
3704         pFcb->ObjectInformation->EndOfFile = liSaveFileSize;
3705         pFcb->ObjectInformation->AllocationSize = liSaveAlloc;
3706     }
3707
3708     if( bReleasePaging)
3709     {
3710
3711         AFSReleaseResource( &pFcb->NPFcb->PagingResource);
3712
3713         AFSAcquireExcl( &pFcb->NPFcb->Resource,
3714                         TRUE);
3715     }
3716
3717     return ntStatus;
3718 }
3719
3720 NTSTATUS
3721 AFSSetEndOfFileInfo( IN PIRP Irp,
3722                      IN AFSDirectoryCB *DirectoryCB)
3723 {
3724     UNREFERENCED_PARAMETER(DirectoryCB);
3725     NTSTATUS ntStatus = STATUS_SUCCESS;
3726     PFILE_END_OF_FILE_INFORMATION pBuffer;
3727     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
3728     PFILE_OBJECT pFileObject = pIrpSp->FileObject;
3729     LARGE_INTEGER liSaveSize;
3730     LARGE_INTEGER liSaveVDL;
3731     LARGE_INTEGER liSaveAlloc;
3732     BOOLEAN bModified = FALSE;
3733     BOOLEAN bReleasePaging = FALSE;
3734     BOOLEAN bTruncated = FALSE;
3735     BOOLEAN bUserMapped = FALSE;
3736     AFSFcb *pFcb = NULL;
3737     AFSCcb *pCcb = NULL;
3738
3739     pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
3740
3741     pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
3742
3743     pBuffer = (PFILE_END_OF_FILE_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
3744
3745     liSaveSize = pFcb->Header.FileSize;
3746     liSaveAlloc = pFcb->Header.AllocationSize;
3747     liSaveVDL = pFcb->Header.ValidDataLength;