Windows: RDR DeviceObject Characteristics
[openafs.git] / src / WINNT / afsrdr / kernel / fs / AFSRDRSupport.cpp
index 31d1e34..295a60b 100644 (file)
@@ -45,9 +45,7 @@ AFSInitRDRDevice()
 
     NTSTATUS       ntStatus = STATUS_SUCCESS;
     UNICODE_STRING uniDeviceName;
-    ULONG          ulIndex = 0;
     AFSDeviceExt  *pDeviceExt = NULL;
-    AFSFileID      stRootFid;
     UNICODE_STRING uniFsRtlRegisterUncProviderEx;
     FsRtlRegisterUncProviderEx_t pFsRtlRegisterUncProviderEx = NULL;
 
@@ -66,17 +64,17 @@ AFSInitRDRDevice()
                                    sizeof( AFSDeviceExt),
                                    pFsRtlRegisterUncProviderEx ? NULL : &uniDeviceName,
                                    FILE_DEVICE_NETWORK_FILE_SYSTEM,
-                                   FILE_REMOTE_DEVICE,
+                                   FILE_DEVICE_SECURE_OPEN | FILE_REMOTE_DEVICE,
                                    FALSE,
                                    &AFSRDRDeviceObject);
 
         if( !NT_SUCCESS( ntStatus))
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
                           "AFSInitRDRDevice IoCreateDevice failure %08lX\n",
-                          ntStatus);
+                          ntStatus));
 
             try_return( ntStatus);
         }
@@ -119,10 +117,10 @@ AFSInitRDRDevice()
         if ( !NT_SUCCESS(ntStatus))
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
                           "AFSInitRDRDevice AFSInitRdrFcb failure %08lX\n",
-                          ntStatus);
+                          ntStatus));
 
             try_return( ntStatus);
         }
@@ -134,6 +132,13 @@ AFSInitRDRDevice()
         AFSRDRDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
 
         //
+        // Increase the StackSize to support the extra stack frame required
+        // for use of IoCompletion routines.
+        //
+
+        AFSRDRDeviceObject->StackSize++;
+
+        //
         // Register this device with MUP with FilterMgr if Vista or above
         //
 
@@ -146,10 +151,10 @@ AFSInitRDRDevice()
                                                     0);
             if ( !NT_SUCCESS( ntStatus))
             {
-                AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
+                AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
                               AFS_TRACE_LEVEL_ERROR,
                               "AFSInitRDRDevice FsRtlRegisterUncProvider failure %08lX\n",
-                              ntStatus);
+                              ntStatus));
             }
         }
         else
@@ -166,10 +171,10 @@ AFSInitRDRDevice()
             }
             else
             {
-                AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
+                AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
                               AFS_TRACE_LEVEL_ERROR,
                               "AFSInitRDRDevice FsRtlRegisterUncProvider failure %08lX\n",
-                              ntStatus);
+                              ntStatus));
             }
         }
 
@@ -209,10 +214,10 @@ NTSTATUS
 AFSRDRDeviceControl( IN PDEVICE_OBJECT DeviceObject,
                      IN PIRP Irp)
 {
+    UNREFERENCED_PARAMETER(DeviceObject);
 
     NTSTATUS           ntStatus = STATUS_SUCCESS;
     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
-    PFILE_OBJECT       pFileObject = pIrpSp->FileObject;
     BOOLEAN            bCompleteIrp = TRUE;
 
     __Enter
@@ -347,7 +352,6 @@ AFSInitializeRedirector( IN AFSRedirectorInitInfo *RedirInitInfo)
     NTSTATUS ntStatus = STATUS_SUCCESS;
     LARGE_INTEGER cacheSizeBytes;
     AFSDeviceExt *pDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
-    AFSDeviceExt *pControlDevExt = (AFSDeviceExt *)AFSDeviceObject->DeviceExtension;
     OBJECT_ATTRIBUTES   stObjectAttribs;
     IO_STATUS_BLOCK stIoStatus;
     UNICODE_STRING uniServiceName;
@@ -368,10 +372,10 @@ AFSInitializeRedirector( IN AFSRedirectorInitInfo *RedirInitInfo)
         if( !NT_SUCCESS( ntStatus))
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
                           "AFSInitializeRedirector AFSLoadLibrary failure %08lX\n",
-                          ntStatus);
+                          ntStatus));
 
             try_return( ntStatus);
         }
@@ -400,9 +404,9 @@ AFSInitializeRedirector( IN AFSRedirectorInitInfo *RedirInitInfo)
         if( AFSDumpFileLocation.Buffer == NULL)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSInitializeRedirector AFS_GENERIC_MEMORY_23_TAG allocation error\n");
+                          "AFSInitializeRedirector AFS_GENERIC_MEMORY_23_TAG allocation error\n"));
 
             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
         }
@@ -503,152 +507,182 @@ AFSInitializeRedirector( IN AFSRedirectorInitInfo *RedirInitInfo)
             SetFlag( pDevExt->DeviceFlags, AFS_DEVICE_FLAG_HIDE_DOT_NAMES);
         }
 
-        if( RedirInitInfo->MemoryCacheOffset.QuadPart != 0 &&
-            RedirInitInfo->MemoryCacheLength.QuadPart != 0)
+        if( BooleanFlagOn( RedirInitInfo->Flags, AFS_REDIR_INIT_FLAG_DISABLE_SHORTNAMES))
         {
 
-            ntStatus = STATUS_INSUFFICIENT_RESOURCES;
+            //
+            // Hide files which begin with .
+            //
 
-#ifdef AMD64
-            pDevExt->Specific.RDR.CacheMdl = MmCreateMdl( NULL,
-                                                          (void *)RedirInitInfo->MemoryCacheOffset.QuadPart,
-                                                          RedirInitInfo->MemoryCacheLength.QuadPart);
-#else
-            pDevExt->Specific.RDR.CacheMdl = MmCreateMdl( NULL,
-                                                          (void *)RedirInitInfo->MemoryCacheOffset.LowPart,
-                                                          RedirInitInfo->MemoryCacheLength.LowPart);
-#endif
+            SetFlag( pDevExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES);
+        }
 
-            if( pDevExt->Specific.RDR.CacheMdl != NULL)
-            {
+        //
+        // Are we performing direct to service IO?
+        //
 
-                __try
-                {
+        if( BooleanFlagOn( RedirInitInfo->Flags, AFS_REDIR_INIT_PERFORM_SERVICE_IO))
+        {
 
-                    MmProbeAndLockPages( pDevExt->Specific.RDR.CacheMdl,
-                                         KernelMode,
-                                         IoModifyAccess);
+            //
+            // Send IO requests directly to service
+            //
 
-                    pDevExt->Specific.RDR.CacheBaseAddress = MmGetSystemAddressForMdlSafe( pDevExt->Specific.RDR.CacheMdl,
-                                                                                           NormalPagePriority);
-                }
-                __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
-                {
-                    IoFreeMdl( pDevExt->Specific.RDR.CacheMdl);
-                    pDevExt->Specific.RDR.CacheMdl = NULL;
-                }
+            SetFlag( pDevExt->DeviceFlags, AFS_DEVICE_FLAG_DIRECT_SERVICE_IO);
+        }
+        else
+        {
+
+            if( RedirInitInfo->MemoryCacheOffset.QuadPart != 0 &&
+                RedirInitInfo->MemoryCacheLength.QuadPart != 0)
+            {
+
+                ntStatus = STATUS_INSUFFICIENT_RESOURCES;
+
+#ifdef AMD64
+                pDevExt->Specific.RDR.CacheMdl = MmCreateMdl( NULL,
+                                                              (void *)RedirInitInfo->MemoryCacheOffset.QuadPart,
+                                                              RedirInitInfo->MemoryCacheLength.QuadPart);
+#else
+                pDevExt->Specific.RDR.CacheMdl = MmCreateMdl( NULL,
+                                                              (void *)RedirInitInfo->MemoryCacheOffset.LowPart,
+                                                              RedirInitInfo->MemoryCacheLength.LowPart);
+#endif
 
                 if( pDevExt->Specific.RDR.CacheMdl != NULL)
                 {
-                    pDevExt->Specific.RDR.CacheLength = RedirInitInfo->MemoryCacheLength;
-                    ntStatus = STATUS_SUCCESS;
-                }
 
-            }
-        }
+                    __try
+                    {
 
-        if( !NT_SUCCESS( ntStatus) &&
-            RedirInitInfo->CacheFileNameLength == 0)
-        {
+                        MmProbeAndLockPages( pDevExt->Specific.RDR.CacheMdl,
+                                             KernelMode,
+                                             IoModifyAccess);
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
-                          AFS_TRACE_LEVEL_ERROR,
-                          "AFSInitializeRedirector Unable to initialize cache file %08lX\n",
-                          ntStatus);
+                        pDevExt->Specific.RDR.CacheBaseAddress = MmGetSystemAddressForMdlSafe( pDevExt->Specific.RDR.CacheMdl,
+                                                                                               NormalPagePriority);
+                    }
+                    __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
+                    {
 
-            try_return( ntStatus);
-        }
+                        AFSDumpTraceFilesFnc();
 
-        if( pDevExt->Specific.RDR.CacheMdl == NULL)
-        {
+                        IoFreeMdl( pDevExt->Specific.RDR.CacheMdl);
+                        pDevExt->Specific.RDR.CacheMdl = NULL;
+                    }
+
+                    if( pDevExt->Specific.RDR.CacheMdl != NULL)
+                    {
+                        pDevExt->Specific.RDR.CacheLength = RedirInitInfo->MemoryCacheLength;
+                        ntStatus = STATUS_SUCCESS;
+                    }
+
+                }
+            }
 
-            if( RedirInitInfo->CacheFileNameLength == 0)
+            if( !NT_SUCCESS( ntStatus) &&
+                RedirInitInfo->CacheFileNameLength == 0)
             {
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
+                AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
                               AFS_TRACE_LEVEL_ERROR,
-                              "AFSInitializeRedirector CacheMdl == NULL\n");
+                              "AFSInitializeRedirector Unable to initialize cache file %08lX\n",
+                              ntStatus));
 
-                try_return( ntStatus = STATUS_INVALID_PARAMETER);
+                try_return( ntStatus);
             }
 
-            //
-            // Go open the cache file
-            //
+            if( pDevExt->Specific.RDR.CacheMdl == NULL)
+            {
 
-            pDevExt->Specific.RDR.CacheFile.Length = 0;
-            pDevExt->Specific.RDR.CacheFile.MaximumLength = (USHORT)RedirInitInfo->CacheFileNameLength + (4 * sizeof( WCHAR));
+                if( RedirInitInfo->CacheFileNameLength == 0)
+                {
 
-            pDevExt->Specific.RDR.CacheFile.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
-                                                                                        pDevExt->Specific.RDR.CacheFile.MaximumLength,
-                                                                                        AFS_GENERIC_MEMORY_24_TAG);
+                    AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
+                                  AFS_TRACE_LEVEL_ERROR,
+                                  "AFSInitializeRedirector CacheMdl == NULL\n"));
 
-            if( pDevExt->Specific.RDR.CacheFile.Buffer == NULL)
-            {
+                    try_return( ntStatus = STATUS_INVALID_PARAMETER);
+                }
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
-                              AFS_TRACE_LEVEL_ERROR,
-                              "AFSInitializeRedirector AFS_GENERIC_MEMORY_24_TAG allocation failure\n");
+                //
+                // Go open the cache file
+                //
 
-                try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
-            }
+                pDevExt->Specific.RDR.CacheFile.Length = 0;
+                pDevExt->Specific.RDR.CacheFile.MaximumLength = (USHORT)RedirInitInfo->CacheFileNameLength + (4 * sizeof( WCHAR));
 
-            RtlCopyMemory( pDevExt->Specific.RDR.CacheFile.Buffer,
-                           L"\\??\\",
-                           4 * sizeof( WCHAR));
+                pDevExt->Specific.RDR.CacheFile.Buffer = (WCHAR *)AFSExAllocatePoolWithTag( PagedPool,
+                                                                                            pDevExt->Specific.RDR.CacheFile.MaximumLength,
+                                                                                            AFS_GENERIC_MEMORY_24_TAG);
 
-            pDevExt->Specific.RDR.CacheFile.Length = 4 * sizeof( WCHAR);
+                if( pDevExt->Specific.RDR.CacheFile.Buffer == NULL)
+                {
 
-            RtlCopyMemory( &pDevExt->Specific.RDR.CacheFile.Buffer[ pDevExt->Specific.RDR.CacheFile.Length/sizeof( WCHAR)],
-                           RedirInitInfo->CacheFileName,
-                           RedirInitInfo->CacheFileNameLength);
+                    AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
+                                  AFS_TRACE_LEVEL_ERROR,
+                                  "AFSInitializeRedirector AFS_GENERIC_MEMORY_24_TAG allocation failure\n"));
 
-            pDevExt->Specific.RDR.CacheFile.Length += (USHORT)RedirInitInfo->CacheFileNameLength;
+                    try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
+                }
 
-            InitializeObjectAttributes( &stObjectAttribs,
-                                        &pDevExt->Specific.RDR.CacheFile,
-                                        OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
-                                        NULL,
-                                        NULL);
+                RtlCopyMemory( pDevExt->Specific.RDR.CacheFile.Buffer,
+                               L"\\??\\",
+                               4 * sizeof( WCHAR));
 
-            ntStatus = ZwOpenFile( &pDevExt->Specific.RDR.CacheFileHandle,
-                                   GENERIC_READ | GENERIC_WRITE,
-                                   &stObjectAttribs,
-                                   &stIoStatus,
-                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
-                                   FILE_WRITE_THROUGH | FILE_RANDOM_ACCESS);
+                pDevExt->Specific.RDR.CacheFile.Length = 4 * sizeof( WCHAR);
 
-            if( !NT_SUCCESS( ntStatus))
-            {
+                RtlCopyMemory( &pDevExt->Specific.RDR.CacheFile.Buffer[ pDevExt->Specific.RDR.CacheFile.Length/sizeof( WCHAR)],
+                               RedirInitInfo->CacheFileName,
+                               RedirInitInfo->CacheFileNameLength);
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
-                              AFS_TRACE_LEVEL_ERROR,
-                              "AFSInitializeRedirector ZwOpenFile failure %08lX\n",
-                              ntStatus);
+                pDevExt->Specific.RDR.CacheFile.Length += (USHORT)RedirInitInfo->CacheFileNameLength;
 
-                try_return( ntStatus);
-            }
+                InitializeObjectAttributes( &stObjectAttribs,
+                                            &pDevExt->Specific.RDR.CacheFile,
+                                            OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
+                                            NULL,
+                                            NULL);
 
-            //
-            // Map to the fileobject
-            //
+                ntStatus = ZwOpenFile( &pDevExt->Specific.RDR.CacheFileHandle,
+                                       GENERIC_READ | GENERIC_WRITE,
+                                       &stObjectAttribs,
+                                       &stIoStatus,
+                                       FILE_SHARE_READ | FILE_SHARE_WRITE,
+                                       FILE_WRITE_THROUGH | FILE_RANDOM_ACCESS);
 
-            ntStatus = ObReferenceObjectByHandle( pDevExt->Specific.RDR.CacheFileHandle,
-                                                  SYNCHRONIZE,
-                                                  NULL,
-                                                  KernelMode,
-                                                  (void **)&pDevExt->Specific.RDR.CacheFileObject,
-                                                  NULL);
+                if( !NT_SUCCESS( ntStatus))
+                {
 
-            if( !NT_SUCCESS( ntStatus))
-            {
+                    AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
+                                  AFS_TRACE_LEVEL_ERROR,
+                                  "AFSInitializeRedirector ZwOpenFile failure %08lX\n",
+                                  ntStatus));
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
-                              AFS_TRACE_LEVEL_ERROR,
-                              "AFSInitializeRedirector ObReferenceObjectByHandle failure %08lX\n",
-                              ntStatus);
+                    try_return( ntStatus);
+                }
 
-                try_return( ntStatus);
+                //
+                // Map to the fileobject
+                //
+
+                ntStatus = ObReferenceObjectByHandle( pDevExt->Specific.RDR.CacheFileHandle,
+                                                      SYNCHRONIZE,
+                                                      NULL,
+                                                      KernelMode,
+                                                      (void **)&pDevExt->Specific.RDR.CacheFileObject,
+                                                      NULL);
+
+                if( !NT_SUCCESS( ntStatus))
+                {
+
+                    AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
+                                  AFS_TRACE_LEVEL_ERROR,
+                                  "AFSInitializeRedirector ObReferenceObjectByHandle failure %08lX\n",
+                                  ntStatus));
+
+                    try_return( ntStatus);
+                }
             }
         }
 
@@ -665,10 +699,10 @@ AFSInitializeRedirector( IN AFSRedirectorInitInfo *RedirInitInfo)
 
         if ( !NT_SUCCESS( ntStatus))
         {
-            AFSDbgLogMsg( AFS_SUBSYSTEM_INIT_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_INIT_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
                           "AFSInitializeRedirector AFSInitializeLibrary failure %08lX\n",
-                          ntStatus);
+                          ntStatus));
         }
 
 try_exit:
@@ -836,8 +870,6 @@ AFSInitRdrFcb( OUT AFSFcb **RdrFcb)
     NTSTATUS ntStatus = STATUS_SUCCESS;
     AFSFcb *pFcb = NULL;
     AFSNonPagedFcb *pNPFcb = NULL;
-    IO_STATUS_BLOCK stIoStatus = {0,0};
-    AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
 
     __Enter
     {
@@ -853,9 +885,9 @@ AFSInitRdrFcb( OUT AFSFcb **RdrFcb)
         if( pFcb == NULL)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSInitRdrFcb Failed to allocate the root fcb\n");
+                          "AFSInitRdrFcb Failed to allocate the root fcb\n"));
 
             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
         }
@@ -873,9 +905,9 @@ AFSInitRdrFcb( OUT AFSFcb **RdrFcb)
         if( pNPFcb == NULL)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
+            AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
                           AFS_TRACE_LEVEL_ERROR,
-                          "AFSInitRdrFcb Failed to allocate the non-paged fcb\n");
+                          "AFSInitRdrFcb Failed to allocate the non-paged fcb\n"));
 
             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
         }
@@ -897,11 +929,11 @@ AFSInitRdrFcb( OUT AFSFcb **RdrFcb)
 
         ExInitializeResourceLite( &pNPFcb->Resource);
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+        AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSInitRootFcb Acquiring Fcb lock %08lX EXCL %08lX\n",
+                      "AFSInitRootFcb Acquiring Fcb lock %p EXCL %08lX\n",
                       &pNPFcb->Resource,
-                      PsGetCurrentThread());
+                      PsGetCurrentThread()));
 
         ExInitializeResourceLite( &pNPFcb->PagingResource);
 
@@ -973,14 +1005,14 @@ AFSRemoveRdrFcb( IN OUT AFSFcb **RdrFcb)
         // The non paged region
         //
 
-        AFSExFreePool( pFcb->NPFcb);
+        AFSExFreePoolWithTag( pFcb->NPFcb, AFS_FCB_NP_ALLOCATION_TAG);
     }
 
     //
     // And the Fcb itself
     //
 
-    AFSExFreePool( pFcb);
+    AFSExFreePoolWithTag( pFcb, AFS_FCB_ALLOCATION_TAG);
 
     return;
 }