Windows: Increase AFS DeviceObject StackSize
[openafs.git] / src / WINNT / afsrdr / kernel / fs / AFSRDRSupport.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: AFSRDRSupport.cpp
37 //
38 #include "AFSCommon.h"
39
40 typedef NTSTATUS  (*FsRtlRegisterUncProviderEx_t)( PHANDLE  MupHandle, PUNICODE_STRING  RedirDevName, PDEVICE_OBJECT  DeviceObject, ULONG  Flags);
41
42 NTSTATUS
43 AFSInitRDRDevice()
44 {
45
46     NTSTATUS       ntStatus = STATUS_SUCCESS;
47     UNICODE_STRING uniDeviceName;
48     AFSDeviceExt  *pDeviceExt = NULL;
49     UNICODE_STRING uniFsRtlRegisterUncProviderEx;
50     FsRtlRegisterUncProviderEx_t pFsRtlRegisterUncProviderEx = NULL;
51
52     __Enter
53     {
54
55         RtlInitUnicodeString( &uniDeviceName,
56                               AFS_RDR_DEVICE_NAME);
57
58         RtlInitUnicodeString( &uniFsRtlRegisterUncProviderEx,
59                               L"FsRtlRegisterUncProviderEx");
60
61         pFsRtlRegisterUncProviderEx = (FsRtlRegisterUncProviderEx_t)MmGetSystemRoutineAddress(&uniFsRtlRegisterUncProviderEx);
62
63         ntStatus = IoCreateDevice( AFSDriverObject,
64                                    sizeof( AFSDeviceExt),
65                                    pFsRtlRegisterUncProviderEx ? NULL : &uniDeviceName,
66                                    FILE_DEVICE_NETWORK_FILE_SYSTEM,
67                                    FILE_REMOTE_DEVICE,
68                                    FALSE,
69                                    &AFSRDRDeviceObject);
70
71         if( !NT_SUCCESS( ntStatus))
72         {
73
74             AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
75                           AFS_TRACE_LEVEL_ERROR,
76                           "AFSInitRDRDevice IoCreateDevice failure %08lX\n",
77                           ntStatus);
78
79             try_return( ntStatus);
80         }
81
82         pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
83
84         RtlZeroMemory( pDeviceExt,
85                        sizeof( AFSDeviceExt));
86
87         //
88         // Initialize resources
89         //
90
91         pDeviceExt->Specific.RDR.VolumeTree.TreeLock = &pDeviceExt->Specific.RDR.VolumeTreeLock;
92
93         ExInitializeResourceLite( pDeviceExt->Specific.RDR.VolumeTree.TreeLock);
94
95         pDeviceExt->Specific.RDR.VolumeTree.TreeHead = NULL;
96
97         ExInitializeResourceLite( &pDeviceExt->Specific.RDR.VolumeListLock);
98
99         pDeviceExt->Specific.RDR.VolumeListHead = NULL;
100
101         pDeviceExt->Specific.RDR.VolumeListTail = NULL;
102
103         KeInitializeEvent( &pDeviceExt->Specific.RDR.QueuedReleaseExtentEvent,
104                            NotificationEvent,
105                            TRUE);
106
107         ExInitializeResourceLite( &pDeviceExt->Specific.RDR.RootCellTreeLock);
108
109         pDeviceExt->Specific.RDR.RootCellTree.TreeLock = &pDeviceExt->Specific.RDR.RootCellTreeLock;
110
111         pDeviceExt->Specific.RDR.RootCellTree.TreeHead = NULL;
112
113         ExInitializeResourceLite( &pDeviceExt->Specific.RDR.ProviderListLock);
114
115         ntStatus = AFSInitRdrFcb( &pDeviceExt->Fcb);
116
117         if ( !NT_SUCCESS(ntStatus))
118         {
119
120             AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
121                           AFS_TRACE_LEVEL_ERROR,
122                           "AFSInitRDRDevice AFSInitRdrFcb failure %08lX\n",
123                           ntStatus);
124
125             try_return( ntStatus);
126         }
127
128         //
129         // Clear the initializing bit
130         //
131
132         AFSRDRDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
133
134         //
135         // Increase the StackSize to support the extra stack frame required
136         // for use of IoCompletion routines.
137         //
138
139         AFSRDRDeviceObject->StackSize++;
140
141         //
142         // Register this device with MUP with FilterMgr if Vista or above
143         //
144
145         if( pFsRtlRegisterUncProviderEx)
146         {
147
148             ntStatus = pFsRtlRegisterUncProviderEx( &AFSMUPHandle,
149                                                     &uniDeviceName,
150                                                     AFSRDRDeviceObject,
151                                                     0);
152             if ( !NT_SUCCESS( ntStatus))
153             {
154                 AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
155                               AFS_TRACE_LEVEL_ERROR,
156                               "AFSInitRDRDevice FsRtlRegisterUncProvider failure %08lX\n",
157                               ntStatus);
158             }
159         }
160         else
161         {
162
163             ntStatus = FsRtlRegisterUncProvider( &AFSMUPHandle,
164                                                  &uniDeviceName,
165                                                  FALSE);
166
167             if ( NT_SUCCESS( ntStatus))
168             {
169
170                 IoRegisterFileSystem( AFSRDRDeviceObject);
171             }
172             else
173             {
174                 AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
175                               AFS_TRACE_LEVEL_ERROR,
176                               "AFSInitRDRDevice FsRtlRegisterUncProvider failure %08lX\n",
177                               ntStatus);
178             }
179         }
180
181         //
182         // Good to go, all registered and ready to start receiving requests
183         //
184
185 try_exit:
186
187         if( !NT_SUCCESS( ntStatus))
188         {
189
190             //
191             // Delete our device and bail
192             //
193
194             ExDeleteResourceLite( pDeviceExt->Specific.RDR.VolumeTree.TreeLock);
195
196             ExDeleteResourceLite( &pDeviceExt->Specific.RDR.VolumeListLock);
197
198             ExDeleteResourceLite( &pDeviceExt->Specific.RDR.RootCellTreeLock);
199
200             ExDeleteResourceLite( &pDeviceExt->Specific.RDR.ProviderListLock);
201
202             IoDeleteDevice( AFSRDRDeviceObject);
203
204             AFSRDRDeviceObject = NULL;
205
206             try_return( ntStatus);
207         }
208     }
209
210     return ntStatus;
211 }
212
213 NTSTATUS
214 AFSRDRDeviceControl( IN PDEVICE_OBJECT DeviceObject,
215                      IN PIRP Irp)
216 {
217     UNREFERENCED_PARAMETER(DeviceObject);
218
219     NTSTATUS           ntStatus = STATUS_SUCCESS;
220     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
221     BOOLEAN            bCompleteIrp = TRUE;
222
223     __Enter
224     {
225
226         switch( pIrpSp->Parameters.DeviceIoControl.IoControlCode)
227         {
228
229             case IOCTL_REDIR_QUERY_PATH:
230             {
231
232                 QUERY_PATH_REQUEST *pPathRequest = (QUERY_PATH_REQUEST *)pIrpSp->Parameters.DeviceIoControl.Type3InputBuffer;
233                 QUERY_PATH_RESPONSE *pPathResponse = (QUERY_PATH_RESPONSE *)Irp->UserBuffer;
234                 UNICODE_STRING uniPathName;
235
236                 ntStatus = STATUS_BAD_NETWORK_PATH;
237
238                 uniPathName.Length = (USHORT)pPathRequest->PathNameLength;
239                 uniPathName.MaximumLength = uniPathName.Length;
240
241                 uniPathName.Buffer = pPathRequest->FilePathName;
242
243                 if( uniPathName.Length >= AFSServerName.Length + sizeof( WCHAR))
244                 {
245
246                     USHORT usLength = uniPathName.Length;
247
248                     uniPathName.Length = AFSServerName.Length;
249
250                     //
251                     // Skip over the first slash in the name
252                     //
253
254                     uniPathName.Buffer = &uniPathName.Buffer[ 1];
255
256
257                     //
258                     // Check to see if the first (or only) component
259                     // of the path matches the server name
260                     //
261
262                     if( RtlCompareUnicodeString( &AFSServerName,
263                                                  &uniPathName,
264                                                  TRUE) == 0 &&
265                         ( usLength == AFSServerName.Length + sizeof( WCHAR) ||
266                           uniPathName.Buffer[ AFSServerName.Length / sizeof( WCHAR)] == '\\'))
267                     {
268
269                         ntStatus = STATUS_SUCCESS;
270
271                         pPathResponse->LengthAccepted = AFSServerName.Length + sizeof( WCHAR);
272                     }
273                 }
274
275                 break;
276             }
277
278             case IOCTL_REDIR_QUERY_PATH_EX:
279             {
280
281                 QUERY_PATH_REQUEST_EX *pPathRequest = (QUERY_PATH_REQUEST_EX *)pIrpSp->Parameters.DeviceIoControl.Type3InputBuffer;
282                 QUERY_PATH_RESPONSE *pPathResponse = (QUERY_PATH_RESPONSE *)Irp->UserBuffer;
283                 UNICODE_STRING uniPathName;
284
285                 ntStatus = STATUS_BAD_NETWORK_PATH;
286
287                 uniPathName.Length = pPathRequest->PathName.Length;
288                 uniPathName.MaximumLength = uniPathName.Length;
289
290                 uniPathName.Buffer = pPathRequest->PathName.Buffer;
291
292                 if( uniPathName.Length >= AFSServerName.Length + sizeof( WCHAR))
293                 {
294
295                     USHORT usLength = uniPathName.Length;
296
297                     uniPathName.Length = AFSServerName.Length;
298
299                     //
300                     // Skip over the first slash in the name
301                     //
302
303                     uniPathName.Buffer = &uniPathName.Buffer[ 1];
304
305
306                     //
307                     // Check to see if the first (or only) component
308                     // of the path matches the server name
309                     //
310
311                     if( RtlCompareUnicodeString( &AFSServerName,
312                                                  &uniPathName,
313                                                  TRUE) == 0 &&
314                         ( usLength == AFSServerName.Length + sizeof( WCHAR) ||
315                           uniPathName.Buffer[ AFSServerName.Length / sizeof( WCHAR)] == '\\'))
316                     {
317
318                         ntStatus = STATUS_SUCCESS;
319
320                         pPathResponse->LengthAccepted = AFSServerName.Length + sizeof( WCHAR);
321                     }
322                 }
323
324                 break;
325             }
326
327             default:
328
329                 ntStatus = STATUS_INVALID_DEVICE_REQUEST;
330
331                 break;
332         }
333
334         if (bCompleteIrp)
335         {
336             //
337             // Complete the request
338             //
339
340             AFSCompleteRequest( Irp,
341                                 ntStatus);
342         }
343     }
344
345     return ntStatus;
346 }
347
348 NTSTATUS
349 AFSInitializeRedirector( IN AFSRedirectorInitInfo *RedirInitInfo)
350 {
351
352     NTSTATUS ntStatus = STATUS_SUCCESS;
353     LARGE_INTEGER cacheSizeBytes;
354     AFSDeviceExt *pDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
355     OBJECT_ATTRIBUTES   stObjectAttribs;
356     IO_STATUS_BLOCK stIoStatus;
357     UNICODE_STRING uniServiceName;
358
359     __Enter
360     {
361
362         //
363         // First this is to load the library
364         //
365
366         RtlInitUnicodeString( &uniServiceName,
367                               AFS_REDIR_LIBRARY_SERVICE_ENTRY);
368
369         ntStatus = AFSLoadLibrary( 0,
370                                    &uniServiceName);
371
372         if( !NT_SUCCESS( ntStatus))
373         {
374
375             AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
376                           AFS_TRACE_LEVEL_ERROR,
377                           "AFSInitializeRedirector AFSLoadLibrary failure %08lX\n",
378                           ntStatus);
379
380             try_return( ntStatus);
381         }
382
383         //
384         // Save off the cache file information
385         //
386
387         pDevExt->Specific.RDR.CacheBlockSize = RedirInitInfo->CacheBlockSize;
388
389         pDevExt->Specific.RDR.CacheBlockCount = RedirInitInfo->ExtentCount;
390
391         pDevExt->Specific.RDR.MaximumRPCLength = RedirInitInfo->MaximumChunkLength;
392
393         cacheSizeBytes = RedirInitInfo->ExtentCount;
394         cacheSizeBytes.QuadPart *= RedirInitInfo->CacheBlockSize;
395
396         AFSDumpFileLocation.Length = 0;
397
398         AFSDumpFileLocation.MaximumLength = (USHORT)RedirInitInfo->DumpFileLocationLength + (4 * sizeof( WCHAR));
399
400         AFSDumpFileLocation.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
401                                                                         AFSDumpFileLocation.MaximumLength,
402                                                                         AFS_GENERIC_MEMORY_23_TAG);
403
404         if( AFSDumpFileLocation.Buffer == NULL)
405         {
406
407             AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
408                           AFS_TRACE_LEVEL_ERROR,
409                           "AFSInitializeRedirector AFS_GENERIC_MEMORY_23_TAG allocation error\n");
410
411             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
412         }
413
414         RtlCopyMemory( AFSDumpFileLocation.Buffer,
415                        L"\\??\\",
416                        4 * sizeof( WCHAR));
417
418         AFSDumpFileLocation.Length = 4 * sizeof( WCHAR);
419
420         RtlCopyMemory( &AFSDumpFileLocation.Buffer[ AFSDumpFileLocation.Length/sizeof( WCHAR)],
421                        (void *)((char *)RedirInitInfo + RedirInitInfo->DumpFileLocationOffset),
422                        RedirInitInfo->DumpFileLocationLength);
423
424         AFSDumpFileLocation.Length += (USHORT)RedirInitInfo->DumpFileLocationLength;
425
426         //
427         // Be sure the shutdown flag is not set
428         //
429
430         ClearFlag( pDevExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN);
431
432         //
433         // Set up the Throttles.
434         //
435         // Max IO is 10% of the cache, or the value in the registry,
436         // with a minimum of 5Mb (and a maximum of 50% cache size)
437         //
438         if( AFSMaxDirectIo)
439         {
440             //
441             // collect what the user
442             //
443             pDevExt->Specific.RDR.MaxIo.QuadPart = AFSMaxDirectIo;
444             pDevExt->Specific.RDR.MaxIo.QuadPart *= (1024 * 1024);
445
446         }
447         else
448         {
449
450             pDevExt->Specific.RDR.MaxIo.QuadPart = cacheSizeBytes.QuadPart / 2;
451         }
452
453         if (pDevExt->Specific.RDR.MaxIo.QuadPart < (5 * 1024 * 1204))
454         {
455
456             pDevExt->Specific.RDR.MaxIo.QuadPart = 5 * 1024 * 1204;
457
458         }
459
460         //
461         // For small cache configurations ...
462         //
463
464         if (pDevExt->Specific.RDR.MaxIo.QuadPart > cacheSizeBytes.QuadPart / 2)
465         {
466
467             pDevExt->Specific.RDR.MaxIo.QuadPart  = cacheSizeBytes.QuadPart / 2;
468         }
469
470         //
471         // Maximum Dirty is 50% of the cache, or the value in the
472         // registry.  No minimum, maximum of 90% of cache size.
473         //
474         if (AFSMaxDirtyFile)
475         {
476
477             pDevExt->Specific.RDR.MaxDirty.QuadPart = AFSMaxDirtyFile;
478             pDevExt->Specific.RDR.MaxDirty.QuadPart *= (1024 * 1024);
479
480         }
481         else
482         {
483
484             pDevExt->Specific.RDR.MaxDirty.QuadPart = cacheSizeBytes.QuadPart/2;
485
486         }
487
488         cacheSizeBytes.QuadPart *= 9;
489         cacheSizeBytes.QuadPart  = cacheSizeBytes.QuadPart / 10;
490
491         if (pDevExt->Specific.RDR.MaxDirty.QuadPart > cacheSizeBytes.QuadPart)
492         {
493             pDevExt->Specific.RDR.MaxDirty.QuadPart = cacheSizeBytes.QuadPart;
494         }
495
496         //
497         // Store off any flags for the file system
498         //
499
500         if( BooleanFlagOn( RedirInitInfo->Flags, AFS_REDIR_INIT_FLAG_HIDE_DOT_FILES))
501         {
502
503             //
504             // Hide files which begin with .
505             //
506
507             SetFlag( pDevExt->DeviceFlags, AFS_DEVICE_FLAG_HIDE_DOT_NAMES);
508         }
509
510         if( BooleanFlagOn( RedirInitInfo->Flags, AFS_REDIR_INIT_FLAG_DISABLE_SHORTNAMES))
511         {
512
513             //
514             // Hide files which begin with .
515             //
516
517             SetFlag( pDevExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES);
518         }
519
520         //
521         // Are we performing direct to service IO?
522         //
523
524         if( BooleanFlagOn( RedirInitInfo->Flags, AFS_REDIR_INIT_PERFORM_SERVICE_IO))
525         {
526
527             //
528             // Send IO requests directly to service
529             //
530
531             SetFlag( pDevExt->DeviceFlags, AFS_DEVICE_FLAG_DIRECT_SERVICE_IO);
532         }
533         else
534         {
535
536             if( RedirInitInfo->MemoryCacheOffset.QuadPart != 0 &&
537                 RedirInitInfo->MemoryCacheLength.QuadPart != 0)
538             {
539
540                 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
541
542 #ifdef AMD64
543                 pDevExt->Specific.RDR.CacheMdl = MmCreateMdl( NULL,
544                                                               (void *)RedirInitInfo->MemoryCacheOffset.QuadPart,
545                                                               RedirInitInfo->MemoryCacheLength.QuadPart);
546 #else
547                 pDevExt->Specific.RDR.CacheMdl = MmCreateMdl( NULL,
548                                                               (void *)RedirInitInfo->MemoryCacheOffset.LowPart,
549                                                               RedirInitInfo->MemoryCacheLength.LowPart);
550 #endif
551
552                 if( pDevExt->Specific.RDR.CacheMdl != NULL)
553                 {
554
555                     __try
556                     {
557
558                         MmProbeAndLockPages( pDevExt->Specific.RDR.CacheMdl,
559                                              KernelMode,
560                                              IoModifyAccess);
561
562                         pDevExt->Specific.RDR.CacheBaseAddress = MmGetSystemAddressForMdlSafe( pDevExt->Specific.RDR.CacheMdl,
563                                                                                                NormalPagePriority);
564                     }
565                     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
566                     {
567
568                         AFSDumpTraceFilesFnc();
569
570                         IoFreeMdl( pDevExt->Specific.RDR.CacheMdl);
571                         pDevExt->Specific.RDR.CacheMdl = NULL;
572                     }
573
574                     if( pDevExt->Specific.RDR.CacheMdl != NULL)
575                     {
576                         pDevExt->Specific.RDR.CacheLength = RedirInitInfo->MemoryCacheLength;
577                         ntStatus = STATUS_SUCCESS;
578                     }
579
580                 }
581             }
582
583             if( !NT_SUCCESS( ntStatus) &&
584                 RedirInitInfo->CacheFileNameLength == 0)
585             {
586
587                 AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
588                               AFS_TRACE_LEVEL_ERROR,
589                               "AFSInitializeRedirector Unable to initialize cache file %08lX\n",
590                               ntStatus);
591
592                 try_return( ntStatus);
593             }
594
595             if( pDevExt->Specific.RDR.CacheMdl == NULL)
596             {
597
598                 if( RedirInitInfo->CacheFileNameLength == 0)
599                 {
600
601                     AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
602                                   AFS_TRACE_LEVEL_ERROR,
603                                   "AFSInitializeRedirector CacheMdl == NULL\n");
604
605                     try_return( ntStatus = STATUS_INVALID_PARAMETER);
606                 }
607
608                 //
609                 // Go open the cache file
610                 //
611
612                 pDevExt->Specific.RDR.CacheFile.Length = 0;
613                 pDevExt->Specific.RDR.CacheFile.MaximumLength = (USHORT)RedirInitInfo->CacheFileNameLength + (4 * sizeof( WCHAR));
614
615                 pDevExt->Specific.RDR.CacheFile.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
616                                                                                             pDevExt->Specific.RDR.CacheFile.MaximumLength,
617                                                                                             AFS_GENERIC_MEMORY_24_TAG);
618
619                 if( pDevExt->Specific.RDR.CacheFile.Buffer == NULL)
620                 {
621
622                     AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
623                                   AFS_TRACE_LEVEL_ERROR,
624                                   "AFSInitializeRedirector AFS_GENERIC_MEMORY_24_TAG allocation failure\n");
625
626                     try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
627                 }
628
629                 RtlCopyMemory( pDevExt->Specific.RDR.CacheFile.Buffer,
630                                L"\\??\\",
631                                4 * sizeof( WCHAR));
632
633                 pDevExt->Specific.RDR.CacheFile.Length = 4 * sizeof( WCHAR);
634
635                 RtlCopyMemory( &pDevExt->Specific.RDR.CacheFile.Buffer[ pDevExt->Specific.RDR.CacheFile.Length/sizeof( WCHAR)],
636                                RedirInitInfo->CacheFileName,
637                                RedirInitInfo->CacheFileNameLength);
638
639                 pDevExt->Specific.RDR.CacheFile.Length += (USHORT)RedirInitInfo->CacheFileNameLength;
640
641                 InitializeObjectAttributes( &stObjectAttribs,
642                                             &pDevExt->Specific.RDR.CacheFile,
643                                             OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
644                                             NULL,
645                                             NULL);
646
647                 ntStatus = ZwOpenFile( &pDevExt->Specific.RDR.CacheFileHandle,
648                                        GENERIC_READ | GENERIC_WRITE,
649                                        &stObjectAttribs,
650                                        &stIoStatus,
651                                        FILE_SHARE_READ | FILE_SHARE_WRITE,
652                                        FILE_WRITE_THROUGH | FILE_RANDOM_ACCESS);
653
654                 if( !NT_SUCCESS( ntStatus))
655                 {
656
657                     AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
658                                   AFS_TRACE_LEVEL_ERROR,
659                                   "AFSInitializeRedirector ZwOpenFile failure %08lX\n",
660                                   ntStatus);
661
662                     try_return( ntStatus);
663                 }
664
665                 //
666                 // Map to the fileobject
667                 //
668
669                 ntStatus = ObReferenceObjectByHandle( pDevExt->Specific.RDR.CacheFileHandle,
670                                                       SYNCHRONIZE,
671                                                       NULL,
672                                                       KernelMode,
673                                                       (void **)&pDevExt->Specific.RDR.CacheFileObject,
674                                                       NULL);
675
676                 if( !NT_SUCCESS( ntStatus))
677                 {
678
679                     AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
680                                   AFS_TRACE_LEVEL_ERROR,
681                                   "AFSInitializeRedirector ObReferenceObjectByHandle failure %08lX\n",
682                                   ntStatus);
683
684                     try_return( ntStatus);
685                 }
686             }
687         }
688
689         pDevExt->Specific.RDR.MaxLinkCount = RedirInitInfo->MaxPathLinkCount;
690
691         pDevExt->Specific.RDR.NameArrayLength = RedirInitInfo->NameArrayLength;
692
693         //
694         // Intialize the library
695         //
696
697         ntStatus = AFSInitializeLibrary( &RedirInitInfo->GlobalFileId,
698                                          TRUE);
699
700         if ( !NT_SUCCESS( ntStatus))
701         {
702             AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
703                           AFS_TRACE_LEVEL_ERROR,
704                           "AFSInitializeRedirector AFSInitializeLibrary failure %08lX\n",
705                           ntStatus);
706         }
707
708 try_exit:
709
710         if( !NT_SUCCESS( ntStatus))
711         {
712
713             if( pDevExt->Specific.RDR.CacheMdl != NULL)
714             {
715
716                 MmUnmapLockedPages( pDevExt->Specific.RDR.CacheBaseAddress,
717                                     pDevExt->Specific.RDR.CacheMdl);
718
719                 MmUnlockPages( pDevExt->Specific.RDR.CacheMdl);
720
721                 ExFreePool( pDevExt->Specific.RDR.CacheMdl);
722
723                 pDevExt->Specific.RDR.CacheMdl = NULL;
724                 pDevExt->Specific.RDR.CacheBaseAddress = NULL;
725                 pDevExt->Specific.RDR.CacheLength.QuadPart = 0;
726             }
727
728             if( pDevExt->Specific.RDR.CacheFileHandle != NULL)
729             {
730
731                 ZwClose( pDevExt->Specific.RDR.CacheFileHandle);
732
733                 pDevExt->Specific.RDR.CacheFileHandle = NULL;
734             }
735
736             if( pDevExt->Specific.RDR.CacheFileObject != NULL)
737             {
738
739                 ObDereferenceObject( pDevExt->Specific.RDR.CacheFileObject);
740
741                 pDevExt->Specific.RDR.CacheFileObject = NULL;
742             }
743
744             if( pDevExt->Specific.RDR.CacheFile.Buffer != NULL)
745             {
746
747                 ExFreePool( pDevExt->Specific.RDR.CacheFile.Buffer);
748
749                 pDevExt->Specific.RDR.CacheFile.Buffer = NULL;
750             }
751
752             if( AFSDumpFileLocation.Buffer != NULL)
753             {
754                 ExFreePool( AFSDumpFileLocation.Buffer);
755
756                 AFSDumpFileLocation.Buffer = NULL;
757             }
758
759             if ( pDevExt->Fcb != NULL)
760             {
761
762                 AFSRemoveRdrFcb( &pDevExt->Fcb);
763
764                 pDevExt = NULL;
765             }
766
767             AFSUnloadLibrary( TRUE);
768         }
769     }
770
771     return ntStatus;
772 }
773
774 NTSTATUS
775 AFSCloseRedirector()
776 {
777
778     NTSTATUS ntStatus = STATUS_SUCCESS;
779     AFSDeviceExt *pDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
780
781     __Enter
782     {
783
784         //
785         // Unload the library first so we close off any accesses to the cache or underlying
786         // shared memory
787         //
788
789         AFSUnloadLibrary( TRUE);
790
791         //
792         // Close off the cache file or mapping
793         //
794
795         if( pDevExt->Specific.RDR.CacheMdl != NULL)
796         {
797
798             MmUnmapLockedPages( pDevExt->Specific.RDR.CacheBaseAddress,
799                                 pDevExt->Specific.RDR.CacheMdl);
800
801             MmUnlockPages( pDevExt->Specific.RDR.CacheMdl);
802
803             ExFreePool( pDevExt->Specific.RDR.CacheMdl);
804
805             pDevExt->Specific.RDR.CacheMdl = NULL;
806             pDevExt->Specific.RDR.CacheBaseAddress = NULL;
807             pDevExt->Specific.RDR.CacheLength.QuadPart = 0;
808         }
809
810         if( pDevExt->Specific.RDR.CacheFileHandle != NULL)
811         {
812
813             ZwClose( pDevExt->Specific.RDR.CacheFileHandle);
814
815             pDevExt->Specific.RDR.CacheFileHandle = NULL;
816         }
817
818         if( pDevExt->Specific.RDR.CacheFileObject != NULL)
819         {
820
821             ObDereferenceObject( pDevExt->Specific.RDR.CacheFileObject);
822
823             pDevExt->Specific.RDR.CacheFileObject = NULL;
824         }
825
826         if( pDevExt->Specific.RDR.CacheFile.Buffer != NULL)
827         {
828
829             ExFreePool( pDevExt->Specific.RDR.CacheFile.Buffer);
830
831             pDevExt->Specific.RDR.CacheFile.Buffer = NULL;
832         }
833
834         if( AFSDumpFileLocation.Buffer != NULL)
835         {
836             ExFreePool( AFSDumpFileLocation.Buffer);
837
838             AFSDumpFileLocation.Buffer = NULL;
839         }
840
841         if ( pDevExt->Fcb != NULL)
842         {
843
844             AFSRemoveRdrFcb( &pDevExt->Fcb);
845
846             pDevExt->Fcb = NULL;
847         }
848
849     }
850
851     return ntStatus;
852 }
853
854 //
855 // Function: AFSInitRdrFcb
856 //
857 // Description:
858 //
859 //      This function performs Redirector Fcb initialization
860 //
861 // Return:
862 //
863 //      A status is returned for the function
864 //
865
866 NTSTATUS
867 AFSInitRdrFcb( OUT AFSFcb **RdrFcb)
868 {
869
870     NTSTATUS ntStatus = STATUS_SUCCESS;
871     AFSFcb *pFcb = NULL;
872     AFSNonPagedFcb *pNPFcb = NULL;
873
874     __Enter
875     {
876
877         //
878         // Initialize the root fcb
879         //
880
881         pFcb = (AFSFcb *)AFSExAllocatePoolWithTag( PagedPool,
882                                                    sizeof( AFSFcb),
883                                                    AFS_FCB_ALLOCATION_TAG);
884
885         if( pFcb == NULL)
886         {
887
888             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
889                           AFS_TRACE_LEVEL_ERROR,
890                           "AFSInitRdrFcb Failed to allocate the root fcb\n");
891
892             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
893         }
894
895         RtlZeroMemory( pFcb,
896                        sizeof( AFSFcb));
897
898         pFcb->Header.NodeByteSize = sizeof( AFSFcb);
899         pFcb->Header.NodeTypeCode = AFS_REDIRECTOR_FCB;
900
901         pNPFcb = (AFSNonPagedFcb *)AFSExAllocatePoolWithTag( NonPagedPool,
902                                                              sizeof( AFSNonPagedFcb),
903                                                              AFS_FCB_NP_ALLOCATION_TAG);
904
905         if( pNPFcb == NULL)
906         {
907
908             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
909                           AFS_TRACE_LEVEL_ERROR,
910                           "AFSInitRdrFcb Failed to allocate the non-paged fcb\n");
911
912             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
913         }
914
915         RtlZeroMemory( pNPFcb,
916                        sizeof( AFSNonPagedFcb));
917
918         pNPFcb->Size = sizeof( AFSNonPagedFcb);
919
920         pNPFcb->Type = AFS_NON_PAGED_FCB;
921
922         //
923         // OK, initialize the entry
924         //
925
926         ExInitializeFastMutex( &pNPFcb->AdvancedHdrMutex);
927
928         FsRtlSetupAdvancedHeader( &pFcb->Header, &pNPFcb->AdvancedHdrMutex);
929
930         ExInitializeResourceLite( &pNPFcb->Resource);
931
932         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
933                       AFS_TRACE_LEVEL_VERBOSE,
934                       "AFSInitRootFcb Acquiring Fcb lock %p EXCL %08lX\n",
935                       &pNPFcb->Resource,
936                       PsGetCurrentThread());
937
938         ExInitializeResourceLite( &pNPFcb->PagingResource);
939
940         pFcb->Header.Resource = &pNPFcb->Resource;
941
942         pFcb->Header.PagingIoResource = &pNPFcb->PagingResource;
943
944         pFcb->NPFcb = pNPFcb;
945
946         if ( InterlockedCompareExchangePointer( (PVOID *)RdrFcb, pFcb, NULL) != NULL)
947         {
948
949             try_return( ntStatus = STATUS_REPARSE);
950         }
951
952 try_exit:
953
954         if( ntStatus != STATUS_SUCCESS)
955         {
956
957             if( pFcb != NULL)
958             {
959
960                 AFSRemoveRdrFcb( &pFcb);
961             }
962         }
963     }
964
965     return ntStatus;
966 }
967
968 //
969 // Function: AFSRemoveRdrFcb
970 //
971 // Description:
972 //
973 //      This function performs Redirector Fcb removal/deallocation
974 //
975 // Return:
976 //
977 //      void.
978 //
979
980 void
981 AFSRemoveRdrFcb( IN OUT AFSFcb **RdrFcb)
982 {
983     AFSFcb *pFcb = NULL;
984
985     pFcb = (AFSFcb *) InterlockedCompareExchangePointer( (PVOID *)RdrFcb, NULL, (PVOID)(*RdrFcb));
986
987     if ( pFcb == NULL)
988     {
989
990         return;
991     }
992
993     if( pFcb->NPFcb != NULL)
994     {
995
996         //
997         // Now the resource
998         //
999
1000         ExDeleteResourceLite( &pFcb->NPFcb->Resource);
1001
1002         ExDeleteResourceLite( &pFcb->NPFcb->PagingResource);
1003
1004         //
1005         // The non paged region
1006         //
1007
1008         AFSExFreePoolWithTag( pFcb->NPFcb, AFS_FCB_NP_ALLOCATION_TAG);
1009     }
1010
1011     //
1012     // And the Fcb itself
1013     //
1014
1015     AFSExFreePoolWithTag( pFcb, AFS_FCB_ALLOCATION_TAG);
1016
1017     return;
1018 }