update for HEAD-2003091401
[reactos.git] / ntoskrnl / io / device.c
index 494011c..69ea332 100644 (file)
@@ -11,7 +11,8 @@
 
 /* INCLUDES ****************************************************************/
 
-#include <ddk/ntddk.h>
+#define NTOS_MODE_KERNEL
+#include <ntos.h>
 #include <internal/io.h>
 #include <internal/po.h>
 #include <internal/ldr.h>
@@ -35,6 +36,9 @@
 
 /* FUNCTIONS ***************************************************************/
 
+/*
+ * @implemented
+ */
 NTSTATUS STDCALL
 IoAttachDeviceByPointer(IN PDEVICE_OBJECT SourceDevice,
                        IN PDEVICE_OBJECT TargetDevice)
@@ -54,6 +58,9 @@ IoAttachDeviceByPointer(IN PDEVICE_OBJECT SourceDevice,
 }
 
 
+/*
+ * @implemented
+ */
 VOID STDCALL
 IoDeleteDevice(PDEVICE_OBJECT DeviceObject)
 {
@@ -90,16 +97,36 @@ IoDeleteDevice(PDEVICE_OBJECT DeviceObject)
 }
 
 
+/*
+ * @implemented
+ */
 PDEVICE_OBJECT
 STDCALL
 IoGetRelatedDeviceObject (
        IN      PFILE_OBJECT    FileObject
        )
 {
-       return (FileObject->DeviceObject);
+   /*Win NT File System Internals, page 633-634*/
+
+   /*get logical volume mounted on a physical/virtual/logical device*/
+   if (FileObject->Vpb && FileObject->Vpb->DeviceObject)
+   {
+      return IoGetAttachedDevice(FileObject->Vpb->DeviceObject);
+   }
+
+   /*check if fileobject has an associated device object mounted by some other file system*/
+   if (FileObject->DeviceObject->Vpb && FileObject->DeviceObject->Vpb->DeviceObject)
+   {
+      return IoGetAttachedDevice(FileObject->DeviceObject->Vpb->DeviceObject);
+   }
+
+   return IoGetAttachedDevice(FileObject->DeviceObject);
 }
 
 
+/*
+ * @implemented
+ */
 NTSTATUS
 STDCALL
 IoGetDeviceObjectPointer (
@@ -152,6 +179,9 @@ IoGetDeviceObjectPointer (
 }
 
 
+/*
+ * @unimplemented
+ */
 VOID
 STDCALL
 IoDetachDevice(PDEVICE_OBJECT TargetDevice)
@@ -161,6 +191,9 @@ IoDetachDevice(PDEVICE_OBJECT TargetDevice)
 }
 
 
+/*
+ * @implemented
+ */
 PDEVICE_OBJECT
 STDCALL
 IoGetAttachedDevice(PDEVICE_OBJECT DeviceObject)
@@ -179,6 +212,9 @@ IoGetAttachedDevice(PDEVICE_OBJECT DeviceObject)
    return(Current);
 }
 
+/*
+ * @implemented
+ */
 PDEVICE_OBJECT
 STDCALL
 IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject)
@@ -194,6 +230,9 @@ IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject)
    return(Current);
 }
 
+/*
+ * @implemented
+ */
 PDEVICE_OBJECT STDCALL
 IoAttachDeviceToDeviceStack(PDEVICE_OBJECT SourceDevice,
                            PDEVICE_OBJECT TargetDevice)
@@ -211,6 +250,9 @@ IoAttachDeviceToDeviceStack(PDEVICE_OBJECT SourceDevice,
    return(AttachedDevice);
 }
 
+/*
+ * @unimplemented
+ */
 VOID STDCALL
 IoRegisterDriverReinitialization(PDRIVER_OBJECT DriverObject,
                                 PDRIVER_REINITIALIZE ReinitRoutine,
@@ -234,7 +276,9 @@ IopDefaultDispatchFunction(PDEVICE_OBJECT DeviceObject,
 NTSTATUS
 IopCreateDriverObject(PDRIVER_OBJECT *DriverObject,
                      PUNICODE_STRING ServiceName,
-                     BOOLEAN FileSystem)
+                     BOOLEAN FileSystem,
+                     PVOID DriverImageStart,
+                     ULONG DriverImageSize)
 {
   PDRIVER_OBJECT Object;
   HANDLE DriverHandle = 0;
@@ -244,12 +288,13 @@ IopCreateDriverObject(PDRIVER_OBJECT *DriverObject,
   OBJECT_ATTRIBUTES ObjectAttributes;
   NTSTATUS Status;
 
-  DPRINT("IopCreateDriverObject(%p '%wZ' %x)\n", DriverObject, ServiceName, FileSystem);
+  DPRINT("IopCreateDriverObject(%p '%wZ' %x %p %x)\n", DriverObject, ServiceName, FileSystem,
+        DriverImageStart, DriverImageSize);
 
   *DriverObject = NULL;
 
   /*  Create ModuleName string  */
-  if (ServiceName != NULL)
+  if ((ServiceName != NULL) && (ServiceName->Buffer != NULL))
     {
       if (FileSystem == TRUE)
        wcscpy(NameBuffer, FILESYSTEM_ROOT_NAME);
@@ -262,15 +307,15 @@ IopCreateDriverObject(PDRIVER_OBJECT *DriverObject,
       DPRINT("Driver name: '%wZ'\n", &DriverName);
     }
 
-  /*  Initialize ObjectAttributes for ModuleObject  */
+  /* Initialize ObjectAttributes for driver object */
   InitializeObjectAttributes(&ObjectAttributes,
-                            (ServiceName != NULL)? &DriverName : NULL,
+                            ((ServiceName != NULL) && (ServiceName->Buffer != NULL))? &DriverName : NULL,
                             OBJ_PERMANENT,
                             NULL,
                             NULL);
 
-  /*  Create module object  */
-  Status = ObCreateObject(&DriverHandle,
+  /* Create module object */
+  Status = ObRosCreateObject(&DriverHandle,
                           STANDARD_RIGHTS_REQUIRED,
                           &ObjectAttributes,
                           IoDriverObjectType,
@@ -297,6 +342,9 @@ IopCreateDriverObject(PDRIVER_OBJECT *DriverObject,
 
   Object->Type = InternalDriverType;
 
+  Object->DriverStart = DriverImageStart;
+  Object->DriverSize = DriverImageSize;
+
   for (i=0; i<=IRP_MJ_MAXIMUM_FUNCTION; i++)
     {
        Object->MajorFunction[i] = (PDRIVER_DISPATCH) IopDefaultDispatchFunction;
@@ -352,7 +400,7 @@ IopInitializeDevice(PDEVICE_NODE DeviceNode,
         {
           /* FIXME: What do we do? Unload the driver or just disable the device? */
           DbgPrint("An FDO was not attached\n");
-          KeBugCheck(0);
+          KEBUGCHECK(0);
         }
 
       /* FIXME: Put some resources in the IRP for the device */
@@ -423,7 +471,8 @@ IopInitializeService(
       return(Status);
     }
 
-    Status = IopInitializeDriver(ModuleObject->EntryPoint, DeviceNode, FALSE);
+    Status = IopInitializeDriver(ModuleObject->EntryPoint, DeviceNode, FALSE,
+                                ModuleObject->Base, ModuleObject->Length);
     if (!NT_SUCCESS(Status))
     {
       LdrUnloadModule(ModuleObject);
@@ -490,7 +539,9 @@ IopInitializeDeviceNodeService(PDEVICE_NODE DeviceNode)
 NTSTATUS
 IopInitializeDriver(PDRIVER_INITIALIZE DriverEntry,
                    PDEVICE_NODE DeviceNode,
-                   BOOLEAN FileSystemDriver)
+                   BOOLEAN FileSystemDriver,
+                   PVOID DriverImageStart,
+                   ULONG DriverImageSize)
 /*
  * FUNCTION: Called to initalize a loaded driver
  * ARGUMENTS:
@@ -508,7 +559,9 @@ IopInitializeDriver(PDRIVER_INITIALIZE DriverEntry,
 
   Status = IopCreateDriverObject(&DriverObject,
                                 &DeviceNode->ServiceName,
-                                FileSystemDriver);
+                                FileSystemDriver,
+                                DriverImageStart,
+                                DriverImageSize);
   if (!NT_SUCCESS(Status))
     {
       return(Status);
@@ -546,9 +599,12 @@ IopInitializeDriver(PDRIVER_INITIALIZE DriverEntry,
 }
 
 
+/*
+ * @implemented
+ */
 NTSTATUS STDCALL
 IoAttachDevice(PDEVICE_OBJECT SourceDevice,
-              PUNICODE_STRING TargetDevice,
+          PUNICODE_STRING TargetDeviceName,
               PDEVICE_OBJECT* AttachedDevice)
 /*
  * FUNCTION: Layers a device over the highest device in a device stack
@@ -558,7 +614,25 @@ IoAttachDevice(PDEVICE_OBJECT SourceDevice,
  *       AttachedDevice (OUT) = Caller storage for the device attached to
  */
 {
-  UNIMPLEMENTED;
+   NTSTATUS       Status;
+   PFILE_OBJECT   FileObject;
+   PDEVICE_OBJECT TargetDevice;
+     
+   Status = IoGetDeviceObjectPointer(TargetDeviceName,
+                                     FILE_READ_ATTRIBUTES,
+                                     &FileObject,
+                                     &TargetDevice);
+   
+   if (!NT_SUCCESS(Status))
+   {
+      return Status;
+   }
+
+   *AttachedDevice = IoAttachDeviceToDeviceStack(SourceDevice,
+                                                 TargetDevice);
+
+   ObDereferenceObject(FileObject);
+   return STATUS_SUCCESS;
 }
 
 
@@ -581,6 +655,9 @@ IopCreateDevice(PVOID ObjectBody,
 }
 
 
+/*
+ * @implemented
+ */
 NTSTATUS STDCALL
 IoCreateDevice(PDRIVER_OBJECT DriverObject,
               ULONG DeviceExtensionSize,
@@ -627,7 +704,7 @@ IoCreateDevice(PDRIVER_OBJECT DriverObject,
    if (DeviceName != NULL)
      {
        InitializeObjectAttributes(&ObjectAttributes,DeviceName,0,NULL,NULL);
-       Status = ObCreateObject(NULL,
+       Status = ObRosCreateObject(NULL,
                                0,
                                &ObjectAttributes,
                                IoDeviceObjectType,
@@ -635,7 +712,7 @@ IoCreateDevice(PDRIVER_OBJECT DriverObject,
      }
    else
      {
-       Status = ObCreateObject(NULL,
+       Status = ObRosCreateObject(NULL,
                                0,
                                NULL,
                                IoDeviceObjectType,
@@ -646,7 +723,7 @@ IoCreateDevice(PDRIVER_OBJECT DriverObject,
    
    if (!NT_SUCCESS(Status))
      {
-       DPRINT("IoCreateDevice() ObCreateObject failed, status: 0x%08X\n", Status);
+       DPRINT("IoCreateDevice() ObRosCreateObject failed, status: 0x%08X\n", Status);
        return(Status);
      }