[disk,device] Move disk__type::DiskNumber to device__type::dev_num
authorShao Miller <Shao.Miller@yrdsb.edu.on.ca>
Sun, 12 Dec 2010 03:18:06 +0000 (22:18 -0500)
committerShao Miller <Shao.Miller@yrdsb.edu.on.ca>
Sun, 12 Dec 2010 23:34:02 +0000 (18:34 -0500)
We can number all devices on a bus, rather than just disks.

src/aoe/driver.c
src/include/device.h
src/include/disk.h
src/winvblock/bus/bus.c
src/winvblock/bus/dev_ctl.c
src/winvblock/disk/dev_ctl.c
src/winvblock/disk/disk.c

index dba0ef4..e62c2cb 100644 (file)
@@ -2200,7 +2200,7 @@ static NTSTATUS STDCALL show(
         disk__type_ptr disk = disk__get_ptr(dev_walker);
         struct aoe__disk_type_ * aoe_disk = aoe__get_(dev_walker);
 
-        disks->Disk[count].Disk = disk->DiskNumber;
+        disks->Disk[count].Disk = dev_walker->dev_num;
         RtlCopyMemory(
             &disks->Disk[count].ClientMac,
             &aoe_disk->ClientMac,
index 57a8e76..1d01073 100644 (file)
@@ -124,6 +124,8 @@ struct device__type {
     PDEVICE_OBJECT Self;
     /* Points to the parent bus' DEVICE_OBJECT */
     PDEVICE_OBJECT Parent;
+    /* The device's child ID relative to the parent bus. */
+    winvblock__uint32 dev_num;
     /* Points to the driver. */
     PDRIVER_OBJECT DriverObject;
     /* Current state of the device. */
index 9fbde5a..4099830 100644 (file)
@@ -152,7 +152,6 @@ struct _disk__type
   KSPIN_LOCK SpinLock;
   winvblock__bool BootDrive;
   winvblock__bool Unmount;
-  winvblock__uint32 DiskNumber;
   disk__media media;
   disk__ops disk_ops;
   ULONGLONG LBADiskSize;
index 7ed1d6e..497aa4e 100644 (file)
@@ -57,6 +57,7 @@ winvblock__lib_func winvblock__bool STDCALL bus__add_child(
     PDEVICE_OBJECT dev_obj_ptr;
     /* Walks the child nodes. */
     struct device__type * walker;
+    winvblock__uint32 dev_num;
 
     DBG("Entry\n");
     if ((bus_ptr == NULL) || (dev_ptr == NULL)) {
@@ -79,14 +80,37 @@ winvblock__lib_func winvblock__bool STDCALL bus__add_child(
     dev_ptr->ops.init(dev_ptr);
     dev_obj_ptr->Flags &= ~DO_DEVICE_INITIALIZING;
     /* Add the new device's extension to the bus' list of children. */
+    dev_num = 0;
     if (bus_ptr->first_child == NULL) {
         bus_ptr->first_child = dev_ptr;
       } else {
         walker = bus_ptr->first_child;
-        while (walker->next_sibling_ptr != NULL)
-          walker = walker->next_sibling_ptr;
-        walker->next_sibling_ptr = dev_ptr;
+        /* If the first child device number isn't 0... */
+        if (walker->dev_num) {
+            /* We insert before. */
+            dev_ptr->next_sibling_ptr = walker;
+            bus_ptr->first_child = dev_ptr;
+          } else {
+            while (walker->next_sibling_ptr != NULL) {
+                /* If there's a gap in the device numbers for the bus... */
+                if (walker->dev_num < walker->next_sibling_ptr->dev_num - 1) {
+                    /* Insert here, instead of at the end. */
+                    dev_num = walker->dev_num + 1;
+                    dev_ptr->next_sibling_ptr = walker->next_sibling_ptr;
+                    walker->next_sibling_ptr = dev_ptr;
+                    break;
+                  }
+                walker = walker->next_sibling_ptr;
+                dev_num = walker->dev_num + 1;
+              }
+            /* If we haven't already inserted the device... */
+            if (!dev_ptr->next_sibling_ptr) {
+                walker->next_sibling_ptr = dev_ptr;
+                dev_num = walker->dev_num + 1;
+              }
+          }
       }
+    dev_ptr->dev_num = dev_num;
     bus_ptr->Children++;
     if (bus_ptr->PhysicalDeviceObject != NULL) {
         IoInvalidateDeviceRelations(
index 78ec7d7..45b3c2e 100644 (file)
@@ -57,7 +57,7 @@ static NTSTATUS STDCALL disk_detach(
     if (dev_walker != NULL)
       disk_walker = disk__get_ptr(dev_walker);
     prev_disk_walker = disk_walker;
-    while ((disk_walker != NULL) && (disk_walker->DiskNumber != disk_num)) {
+    while ((disk_walker != NULL) && (dev_walker->dev_num != disk_num)) {
         prev_disk_walker = disk_walker;
         dev_walker = dev_walker->next_sibling_ptr;
         if (dev_walker != NULL)
@@ -70,7 +70,7 @@ static NTSTATUS STDCALL disk_detach(
             *completion = TRUE;
             return STATUS_INVALID_DEVICE_REQUEST;
           }
-        DBG("Deleting disk %d\n", disk_walker->DiskNumber);
+        DBG("Deleting disk %d\n", dev_walker->dev_num);
         if (disk_walker == disk__get_ptr(bus->first_child))
           bus->first_child = dev_walker->next_sibling_ptr;
           else {
index a900c89..01fbb9f 100644 (file)
@@ -158,7 +158,6 @@ static NTSTATUS STDCALL scsi_get_address(
 {
   winvblock__uint32 copy_size;
   SCSI_ADDRESS scsi_address;
-  disk__type_ptr disk_ptr;
 
   copy_size =
     ( Stack->Parameters.DeviceIoControl.OutputBufferLength <
@@ -167,8 +166,7 @@ static NTSTATUS STDCALL scsi_get_address(
   scsi_address.Length = sizeof ( SCSI_ADDRESS );
   scsi_address.PortNumber = 0;
   scsi_address.PathId = 0;
-  disk_ptr = disk__get_ptr ( dev_ptr );
-  scsi_address.TargetId = ( winvblock__uint8 ) disk_ptr->DiskNumber;
+  scsi_address.TargetId = (winvblock__uint8) dev_ptr->dev_num;
   scsi_address.Lun = 0;
   RtlCopyMemory ( Irp->AssociatedIrp.SystemBuffer, &scsi_address, copy_size );
   Irp->IoStatus.Information = ( ULONG_PTR ) copy_size;
index c24b8c6..fdac4b6 100644 (file)
@@ -51,7 +51,6 @@ __divdi3 (
 #endif
 
 /* Globals. */
-static winvblock__uint32 next_disk = 0;
 static LIST_ENTRY disk_list;
 static KSPIN_LOCK disk_list_lock;
 winvblock__bool disk__removable[disk__media_count] = { TRUE, FALSE, TRUE };
@@ -172,7 +171,6 @@ static PDEVICE_OBJECT STDCALL create_pdo(IN struct device__type * dev_ptr) {
     dev_ptr->Self = dev_obj_ptr;
     KeInitializeEvent ( &disk_ptr->SearchEvent, SynchronizationEvent, FALSE );
     KeInitializeSpinLock ( &disk_ptr->SpinLock );
-    disk_ptr->DiskNumber = InterlockedIncrement ( &next_disk ) - 1;
     /*
      * Some device parameters
      */