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