6a69bfe8def0ea4c501859fdf241d774009182e0
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSInit.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: AFSInit.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 //
42 // DriverEntry
43 //
44 // This is the initial entry point for the driver.
45 //
46 // Inputs:
47 //  DriverObject        Pointer to Driver Object created by I/O manager
48 //  RegistryPath        Pointer to registry path representing this Driver
49 //
50 // Returns:
51 //  Success             To indicate Driver's inituaialization processing
52 //                      was successful
53 //  NT ERROR STATUS     Otherwise -- Driver does not remain loaded
54 //
55
56 NTSTATUS
57 DriverEntry( PDRIVER_OBJECT DriverObject,
58              PUNICODE_STRING RegistryPath)
59 {
60
61     NTSTATUS ntStatus = STATUS_SUCCESS;
62     AFSDeviceExt    *pDeviceExt;
63     UNICODE_STRING uniDeviceName;
64     ULONG ulIndex = 0;
65     UNICODE_STRING uniRoutine;
66     RTL_OSVERSIONINFOW sysVersion;
67
68     BOOLEAN bExit = FALSE;
69
70     __try
71     {
72
73         AFSPrint("AFSLibrary DriverEntry Initialization build %s:%s\n", __DATE__, __TIME__);
74
75         //
76         // Our backdoor to not let the driver load
77         //
78
79         if( bExit)
80         {
81
82             //
83             // Return a failure so we can update the binary and manually start it without
84             // having to do a reboot
85             //
86
87             try_return( ntStatus = STATUS_UNSUCCESSFUL);
88         }
89
90         //
91         // Perform some initialization
92         //
93
94         AFSLibraryDriverObject = DriverObject;
95
96         //
97         // Setup the registry string
98         //
99
100         AFSRegistryPath.MaximumLength = RegistryPath->MaximumLength;
101         AFSRegistryPath.Length        = RegistryPath->Length;
102
103         AFSRegistryPath.Buffer = (PWSTR)AFSLibExAllocatePoolWithTag( PagedPool,
104                                                                      AFSRegistryPath.Length,
105                                                                      AFS_GENERIC_MEMORY_13_TAG);
106
107         if( AFSRegistryPath.Buffer == NULL)
108         {
109
110             AFSPrint("AFS DriverEntry Failed to allocate registry path buffer\n");
111
112             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
113         }
114
115         RtlCopyMemory( AFSRegistryPath.Buffer,
116                        RegistryPath->Buffer,
117                        RegistryPath->Length);
118
119         RtlZeroMemory( &sysVersion,
120                        sizeof( RTL_OSVERSIONINFOW));
121
122         sysVersion.dwOSVersionInfoSize = sizeof( RTL_OSVERSIONINFOW);
123
124         RtlGetVersion( &sysVersion);
125
126 #if 0
127         //
128         // By not fetching the RtlSetSaclSecurityDescriptor function
129         // pointer it disables the additional of a mandatory label
130         // to the default acl which is returned by AFSRedir for all
131         // security information queries.   The addition of the
132         // mandatory label appears to have a negative consequence
133         // for roaming profiles and redirected folders.  All links
134         // become untrusted and IE9 is unable to open a new instance
135         // to a non-default home page.
136         //
137         //
138         // Only retrieve this function for Vista and above since
139         // Mandatory Labels only exist on those operating systems.
140         //
141
142         if( sysVersion.dwMajorVersion >= 6)
143         {
144             RtlInitUnicodeString( &uniRoutine,
145                                   L"RtlSetSaclSecurityDescriptor");
146
147             AFSRtlSetSaclSecurityDescriptor = (PAFSRtlSetSaclSecurityDescriptor)MmGetSystemRoutineAddress( &uniRoutine);
148         }
149
150 #endif
151
152         RtlInitUnicodeString( &uniRoutine,
153                               L"RtlSetGroupSecurityDescriptor");
154
155         AFSRtlSetGroupSecurityDescriptor = (PAFSRtlSetGroupSecurityDescriptor)MmGetSystemRoutineAddress( &uniRoutine);
156
157         ntStatus = AFSCreateDefaultSecurityDescriptor();
158
159         if( !NT_SUCCESS( ntStatus))
160         {
161
162             AFSPrint("AFS DriverEntry  AFSCreateDefaultSecurityDescriptor failed Status %08lX\n", ntStatus);
163
164             ntStatus = STATUS_SUCCESS;
165         }
166
167         //
168         // Initilize the control device
169         //
170
171         RtlInitUnicodeString( &uniDeviceName,
172                               AFS_LIBRARY_CONTROL_DEVICE_NAME);
173
174         ntStatus = IoCreateDevice( DriverObject,
175                                    sizeof( AFSDeviceExt),
176                                    &uniDeviceName,
177                                    FILE_DEVICE_DISK_FILE_SYSTEM,
178                                    0,
179                                    FALSE,
180                                    &AFSLibraryDeviceObject);
181
182         if( !NT_SUCCESS( ntStatus))
183         {
184
185             AFSPrint("AFS DriverEntry - Failed to allocate device control object Status %08lX\n", ntStatus);
186
187             try_return( ntStatus);
188         }
189
190         //
191         // Setup the device extension
192         //
193
194         pDeviceExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
195
196         //
197         // Now initialize the control device
198         //
199
200         ntStatus = AFSInitializeLibraryDevice();
201
202         if( !NT_SUCCESS( ntStatus))
203         {
204
205             try_return( ntStatus);
206         }
207
208         //
209         // Initialize the worker thread pool
210         //
211
212         ntStatus = AFSInitializeWorkerPool();
213
214         if( !NT_SUCCESS( ntStatus))
215         {
216
217             AFSPrint("AFS DriverEntry Failed to initialize worker pool Status %08lX\n", ntStatus);
218
219             try_return( ntStatus);
220         }
221
222         //
223         // Fill in the dispatch table
224         //
225
226         for( ulIndex = 0; ulIndex <= IRP_MJ_MAXIMUM_FUNCTION; ulIndex++)
227         {
228
229             DriverObject->MajorFunction[ ulIndex] = AFSDefaultDispatch;
230         }
231
232         DriverObject->MajorFunction[IRP_MJ_CREATE] =                    AFSCreate;
233         DriverObject->MajorFunction[IRP_MJ_CLOSE] =                     AFSClose;
234         DriverObject->MajorFunction[IRP_MJ_READ] =                      AFSRead;
235         DriverObject->MajorFunction[IRP_MJ_WRITE] =                     AFSWrite;
236         DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] =         AFSQueryFileInfo;
237         DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] =           AFSSetFileInfo;
238         DriverObject->MajorFunction[IRP_MJ_QUERY_EA] =                  AFSQueryEA;
239         DriverObject->MajorFunction[IRP_MJ_SET_EA] =                    AFSSetEA;
240         DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS] =             AFSFlushBuffers;
241         DriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] =  AFSQueryVolumeInfo;
242         DriverObject->MajorFunction[IRP_MJ_SET_VOLUME_INFORMATION] =    AFSSetVolumeInfo;
243         DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] =         AFSDirControl;
244         DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =       AFSFSControl;
245         DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =            AFSDevControl;
246         DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] =   AFSInternalDevControl;
247         DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] =                  AFSShutdown;
248         DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL] =              AFSLockControl;
249         DriverObject->MajorFunction[IRP_MJ_CLEANUP] =                   AFSCleanup;
250         DriverObject->MajorFunction[IRP_MJ_QUERY_SECURITY] =            AFSQuerySecurity;
251         DriverObject->MajorFunction[IRP_MJ_SET_SECURITY] =              AFSSetSecurity;
252         DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] =            AFSSystemControl;
253         //DriverObject->MajorFunction[IRP_MJ_QUERY_QUOTA] =               AFSQueryQuota;
254         //DriverObject->MajorFunction[IRP_MJ_SET_QUOTA] =                 AFSSetQuota;
255
256         DriverObject->DriverUnload = AFSUnload;
257
258         AFSSysProcess = PsGetCurrentProcessId();
259
260 try_exit:
261
262         if( !NT_SUCCESS( ntStatus))
263         {
264
265             AFSPrint("AFSLibrary DriverEntry failed to initialize %08lX\n", ntStatus);
266
267             if( AFSLibraryDeviceObject != NULL)
268             {
269
270                 AFSRemoveWorkerPool();
271             }
272
273             if( AFSRegistryPath.Buffer != NULL)
274             {
275
276                 ExFreePool( AFSRegistryPath.Buffer);
277             }
278
279             if( AFSLibraryDeviceObject != NULL)
280             {
281
282                 AFSRemoveLibraryDevice();
283
284                 IoDeleteDevice( AFSLibraryDeviceObject);
285             }
286         }
287     }
288     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
289     {
290
291         AFSPrint( "EXCEPTION - AFS DriverEntry\n");
292
293         AFSDumpTraceFilesFnc();
294     }
295
296     return ntStatus;
297 }
298
299 void
300 AFSUnload( IN PDRIVER_OBJECT DriverObject)
301 {
302
303     UNREFERENCED_PARAMETER(DriverObject);
304     if( AFSGlobalRoot != NULL)
305     {
306
307         AFSInvalidateVolume( AFSGlobalRoot,
308                              AFS_INVALIDATE_CALLBACK);
309
310         ClearFlag( AFSGlobalRoot->Flags, AFS_VOLUME_ACTIVE_GLOBAL_ROOT);
311
312         AFSShutdownVolumeWorker( AFSGlobalRoot);
313     }
314
315     if( AFSLibraryDeviceObject != NULL)
316     {
317
318         AFSRemoveWorkerPool();
319     }
320
321     if( AFSRegistryPath.Buffer != NULL)
322     {
323
324         ExFreePool( AFSRegistryPath.Buffer);
325     }
326
327     AFSCloseLibrary();
328
329     if( AFSDefaultSD != NULL)
330     {
331         ExFreePool( AFSDefaultSD);
332     }
333
334     if( AFSLibraryDeviceObject != NULL)
335     {
336
337         AFSRemoveLibraryDevice();
338
339         IoDeleteDevice( AFSLibraryDeviceObject);
340     }
341
342     return;
343 }