win-pvdrivers

changeset 17:ce8bd97c4136

xenvbd: cleanups

clean up warnings
simplify XenVbd_HotPlugHandler flow, add comments
eliminate literals in backendstatehandler
fix XenBusInterface.Printf for context parameter
author Andy Grover <andy@groveronline.com>
date Thu Nov 29 12:29:08 2007 -0800 (2007-11-29)
parents 53fdfc9f5d56
children a54ad4e0e083
files xenvbd/xenvbd.c
line diff
     1.1 --- a/xenvbd/xenvbd.c	Thu Nov 29 10:59:16 2007 -0800
     1.2 +++ b/xenvbd/xenvbd.c	Thu Nov 29 12:29:08 2007 -0800
     1.3 @@ -64,6 +64,8 @@ XenVbd_HotPlugHandler(char *Path, PVOID 
     1.4  #pragma alloc_text (PAGE, XenVbd_AddDevice)
     1.5  #endif
     1.6  
     1.7 +#pragma warning(disable: 4127) // disable conditional expression is constant
     1.8 +
     1.9  LIST_ENTRY DeviceListHead;
    1.10  XEN_IFACE_EVTCHN EvtChnInterface;
    1.11  XEN_IFACE_XENBUS XenBusInterface;
    1.12 @@ -322,7 +324,7 @@ XenVbd_D0Entry(
    1.13    return status;
    1.14  }
    1.15  
    1.16 -static int EnumeratedDevices;
    1.17 +static LONG EnumeratedDevices;
    1.18  static KEVENT WaitDevicesEvent;
    1.19  
    1.20  static NTSTATUS
    1.21 @@ -406,8 +408,13 @@ XenVbd_D0Exit(
    1.22  }
    1.23  
    1.24  static NTSTATUS
    1.25 -XenVbd_DeviceUsageNotification(WDFDEVICE Device, WDF_SPECIAL_FILE_TYPE NotificationType, BOOLEAN IsInNotificationPath)
    1.26 +XenVbd_DeviceUsageNotification(
    1.27 +  WDFDEVICE Device,
    1.28 +  WDF_SPECIAL_FILE_TYPE NotificationType,
    1.29 +  BOOLEAN IsInNotificationPath)
    1.30  {
    1.31 +  UNREFERENCED_PARAMETER(Device);
    1.32 +
    1.33    KdPrint((__DRIVER_NAME " --> DeviceUsageNotification\n"));
    1.34  
    1.35    switch (NotificationType)
    1.36 @@ -655,7 +662,7 @@ XenVbd_Interrupt(PKINTERRUPT Interrupt, 
    1.37  static VOID
    1.38  XenVbd_BackEndStateHandler(char *Path, PVOID Data)
    1.39  {
    1.40 -  PXENVBD_CHILD_DEVICE_DATA DeviceData;
    1.41 +  PXENVBD_CHILD_DEVICE_DATA DeviceData = Data;
    1.42    char TmpPath[128];
    1.43    char *Value;
    1.44    int NewState;
    1.45 @@ -666,8 +673,6 @@ XenVbd_BackEndStateHandler(char *Path, P
    1.46    XENVBD_DEVICE_IDENTIFICATION_DESCRIPTION Description;
    1.47    NTSTATUS status;
    1.48  
    1.49 -  DeviceData = (PXENVBD_CHILD_DEVICE_DATA)Data;
    1.50 -
    1.51    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
    1.52      XBT_NIL, Path, &Value);
    1.53    NewState = atoi(Value);
    1.54 @@ -698,17 +703,20 @@ XenVbd_BackEndStateHandler(char *Path, P
    1.55      ref = GntTblInterface.GrantAccess(GntTblInterface.InterfaceHeader.Context,
    1.56        0, PFN, FALSE);
    1.57  
    1.58 -    RtlStringCbCopyA(TmpPath, 128, DeviceData->Path);
    1.59 -    RtlStringCbCatA(TmpPath, 128, "/ring-ref");
    1.60 -    XenBusInterface.Printf(XBT_NIL, TmpPath, "%d", ref);
    1.61 +    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
    1.62 +    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/ring-ref");
    1.63 +    XenBusInterface.Printf(XenBusInterface.InterfaceHeader.Context,
    1.64 +      XBT_NIL, TmpPath, "%d", ref);
    1.65  
    1.66 -    RtlStringCbCopyA(TmpPath, 128, DeviceData->Path);
    1.67 -    RtlStringCbCatA(TmpPath, 128, "/event-channel");
    1.68 -    XenBusInterface.Printf(XBT_NIL, TmpPath, "%d", DeviceData->EventChannel);
    1.69 +    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
    1.70 +    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/event-channel");
    1.71 +    XenBusInterface.Printf(XenBusInterface.InterfaceHeader.Context,
    1.72 +      XBT_NIL, TmpPath, "%d", DeviceData->EventChannel);
    1.73    
    1.74 -    RtlStringCbCopyA(TmpPath, 128, DeviceData->Path);
    1.75 -    RtlStringCbCatA(TmpPath, 128, "/state");
    1.76 -    XenBusInterface.Printf(XBT_NIL, TmpPath, "%d", XenbusStateInitialised);
    1.77 +    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
    1.78 +    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/state");
    1.79 +    XenBusInterface.Printf(XenBusInterface.InterfaceHeader.Context,
    1.80 +      XBT_NIL, TmpPath, "%d", XenbusStateInitialised);
    1.81  
    1.82      KdPrint((__DRIVER_NAME "     Set Frontend state to Initialised\n"));
    1.83      break;
    1.84 @@ -725,8 +733,8 @@ XenVbd_BackEndStateHandler(char *Path, P
    1.85  
    1.86      Description.DeviceData = DeviceData;
    1.87  
    1.88 -    RtlStringCbCopyA(TmpPath, 128, DeviceData->Path);
    1.89 -    RtlStringCbCatA(TmpPath, 128, "/device-type");
    1.90 +    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
    1.91 +    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/device-type");
    1.92      XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
    1.93        XBT_NIL, TmpPath, &Value);
    1.94      if (strcmp(Value, "disk") == 0)
    1.95 @@ -744,22 +752,22 @@ XenVbd_BackEndStateHandler(char *Path, P
    1.96        KdPrint((__DRIVER_NAME "     DeviceType = %s (This probably won't work!)\n", Value));
    1.97      }
    1.98  
    1.99 -    RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
   1.100 -    RtlStringCbCatA(TmpPath, 128, "/type"); // should probably check that this is 'phy' or 'file' or at least not ''
   1.101 +    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
   1.102 +    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/type"); // should probably check that this is 'phy' or 'file' or at least not ''
   1.103      XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
   1.104        XBT_NIL, TmpPath, &Value);
   1.105      KdPrint((__DRIVER_NAME "     Backend Type = %s\n", Value));
   1.106      ExFreePool(Value);
   1.107  
   1.108 -    RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
   1.109 -    RtlStringCbCatA(TmpPath, 128, "/mode"); // should store this...
   1.110 +    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
   1.111 +    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/mode"); // should store this...
   1.112      XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
   1.113        XBT_NIL, TmpPath, &Value);
   1.114      KdPrint((__DRIVER_NAME "     Backend Mode = %s\n", Value));
   1.115      ExFreePool(Value);
   1.116  
   1.117 -    RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
   1.118 -    RtlStringCbCatA(TmpPath, 128, "/sector-size");
   1.119 +    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
   1.120 +    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/sector-size");
   1.121      XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
   1.122        XBT_NIL, TmpPath, &Value);
   1.123      // should complain if Value == NULL
   1.124 @@ -768,8 +776,8 @@ XenVbd_BackEndStateHandler(char *Path, P
   1.125  
   1.126      KdPrint((__DRIVER_NAME "     BytesPerSector = %d\n", DeviceData->BytesPerSector));    
   1.127  
   1.128 -    RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
   1.129 -    RtlStringCbCatA(TmpPath, 128, "/sectors");
   1.130 +    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
   1.131 +    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/sectors");
   1.132      XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
   1.133        XBT_NIL, TmpPath, &Value);
   1.134      // should complain if Value == NULL
   1.135 @@ -794,9 +802,10 @@ XenVbd_BackEndStateHandler(char *Path, P
   1.136        KdPrint((__DRIVER_NAME "     WdfChildListAddOrUpdateChildDescriptionAsPresent failed %08x\n", status));
   1.137      } 
   1.138  
   1.139 -    RtlStringCbCopyA(TmpPath, 128, DeviceData->Path);
   1.140 -    RtlStringCbCatA(TmpPath, 128, "/state");
   1.141 -    XenBusInterface.Printf(XBT_NIL, TmpPath, "%d", XenbusStateConnected);
   1.142 +    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
   1.143 +    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/state");
   1.144 +    XenBusInterface.Printf(XenBusInterface.InterfaceHeader.Context,
   1.145 +      XBT_NIL, TmpPath, "%d", XenbusStateConnected);
   1.146  
   1.147      KdPrint((__DRIVER_NAME "     Set Frontend state to Connected\n"));
   1.148      InterlockedIncrement(&EnumeratedDevices);
   1.149 @@ -836,62 +845,59 @@ XenVbd_HotPlugHandler(char *Path, PVOID 
   1.150    //KdPrint((__DRIVER_NAME "     Path = %s\n", Path));
   1.151  
   1.152    Bits = SplitString(Path, '/', 4, &Count);
   1.153 -  switch (Count)
   1.154 +  if (Count != 4)
   1.155    {
   1.156 -  case 0:
   1.157 -  case 1:
   1.158 -  case 2:
   1.159 -    break; // should never happen
   1.160 -  case 3:
   1.161 -    break;
   1.162 -  case 4:
   1.163 -    
   1.164 -    if (strcmp(Bits[3], "state") != 0) // we only care when the state appears
   1.165 -      break;
   1.166 -    for (DeviceData = (PXENVBD_CHILD_DEVICE_DATA)DeviceListHead.Flink; DeviceData != (PXENVBD_CHILD_DEVICE_DATA)&DeviceListHead; DeviceData = (PXENVBD_CHILD_DEVICE_DATA)DeviceData->Entry.Flink)
   1.167 -    {
   1.168 -      if (strncmp(DeviceData->Path, Path, strlen(DeviceData->Path)) == 0 && Path[strlen(DeviceData->Path)] == '/')
   1.169 -      {
   1.170 -        break;
   1.171 -      }
   1.172 -    }
   1.173 -    if (DeviceData == (PXENVBD_CHILD_DEVICE_DATA)&DeviceListHead)
   1.174 -    {
   1.175 -      DeviceData = ExAllocatePoolWithTag(NonPagedPool, sizeof(XENVBD_CHILD_DEVICE_DATA), XENVBD_POOL_TAG);
   1.176 -      memset(DeviceData, 0, sizeof(XENVBD_CHILD_DEVICE_DATA));
   1.177 +    KdPrint((__FUNCTION__ ": Count = %d, not 4!\n", Count));
   1.178 +    goto cleanup;
   1.179 +  }
   1.180  
   1.181 -      //KdPrint((__DRIVER_NAME "     Allocated ChildDeviceData = %08x\n", DeviceData));
   1.182 -      
   1.183 -      InsertTailList(&DeviceListHead, &DeviceData->Entry);
   1.184 -      RtlStringCbCopyA(DeviceData->Path, 128, Bits[0]);
   1.185 -      RtlStringCbCatA(DeviceData->Path, 128, "/");
   1.186 -      RtlStringCbCatA(DeviceData->Path, 128, Bits[1]);
   1.187 -      RtlStringCbCatA(DeviceData->Path, 128, "/");
   1.188 -      RtlStringCbCatA(DeviceData->Path, 128, Bits[2]);
   1.189 -
   1.190 -      DeviceData->DeviceIndex = atoi(Bits[2]);
   1.191 +  if (strcmp(Bits[3], "state") != 0) // we only care when the state appears
   1.192 +    goto cleanup;
   1.193  
   1.194 -      RtlStringCbCopyA(TmpPath, 128, DeviceData->Path);
   1.195 -      RtlStringCbCatA(TmpPath, 128, "/backend");
   1.196 -      XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
   1.197 -        XBT_NIL, TmpPath, &Value);
   1.198 -      if (Value == NULL)
   1.199 -      {
   1.200 -        KdPrint((__DRIVER_NAME "     Read Failed\n"));
   1.201 -      }
   1.202 -      else
   1.203 -      {
   1.204 -        RtlStringCbCopyA(DeviceData->BackendPath, 128, Value);
   1.205 -      }
   1.206 -      ExFreePool(Value);
   1.207 -      RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
   1.208 -      RtlStringCbCatA(TmpPath, 128, "/state");
   1.209 -      XenBusInterface.AddWatch(XenBusInterface.InterfaceHeader.Context,
   1.210 -        XBT_NIL, TmpPath, XenVbd_BackEndStateHandler, DeviceData);
   1.211 +  /* ignore already known devices */
   1.212 +  for (DeviceData = (PXENVBD_CHILD_DEVICE_DATA)DeviceListHead.Flink; DeviceData != (PXENVBD_CHILD_DEVICE_DATA)&DeviceListHead; DeviceData = (PXENVBD_CHILD_DEVICE_DATA)DeviceData->Entry.Flink)
   1.213 +  {
   1.214 +    if (strncmp(DeviceData->Path, Path, strlen(DeviceData->Path)) == 0 && Path[strlen(DeviceData->Path)] == '/')
   1.215 +    {
   1.216 +      goto cleanup;
   1.217      }
   1.218 -    break;
   1.219    }
   1.220 +
   1.221 +  /* new device found, alloc and init dev extension */
   1.222 +  DeviceData = ExAllocatePoolWithTag(NonPagedPool,
   1.223 +    sizeof(XENVBD_CHILD_DEVICE_DATA), XENVBD_POOL_TAG);
   1.224 +  memset(DeviceData, 0, sizeof(XENVBD_CHILD_DEVICE_DATA));
   1.225 +
   1.226 +  //KdPrint((__DRIVER_NAME "     Allocated ChildDeviceData = %08x\n", DeviceData));
   1.227    
   1.228 +  InsertTailList(&DeviceListHead, &DeviceData->Entry);
   1.229 +  RtlStringCbPrintfA(DeviceData->Path, ARRAY_SIZE(DeviceData->Path),
   1.230 +    "%s/%s/%s", Bits[0], Bits[1], Bits[2]);
   1.231 +  DeviceData->DeviceIndex = atoi(Bits[2]);
   1.232 +
   1.233 +  /* Get backend path */
   1.234 +  RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
   1.235 +  RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/backend");
   1.236 +  XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
   1.237 +    XBT_NIL, TmpPath, &Value);
   1.238 +  if (!Value)
   1.239 +  {
   1.240 +    KdPrint((__DRIVER_NAME "     Read Failed\n"));
   1.241 +  }
   1.242 +  else
   1.243 +  {
   1.244 +    RtlStringCbCopyA(DeviceData->BackendPath,
   1.245 +      ARRAY_SIZE(DeviceData->BackendPath), Value);
   1.246 +  }
   1.247 +  ExFreePool(Value);
   1.248 +
   1.249 +  /* Add watch on backend state */
   1.250 +  RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
   1.251 +  RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/state");
   1.252 +  XenBusInterface.AddWatch(XenBusInterface.InterfaceHeader.Context,
   1.253 +    XBT_NIL, TmpPath, XenVbd_BackEndStateHandler, DeviceData);
   1.254 +
   1.255 +cleanup:
   1.256    FreeSplitString(Bits, Count);
   1.257  
   1.258    //KdPrint((__DRIVER_NAME " <-- HotPlugHandler\n"));  
   1.259 @@ -912,7 +918,7 @@ XenVbd_ChildListCreateDevice(WDFCHILDLIS
   1.260    WDF_DPC_CONFIG DpcConfig;
   1.261    WDF_OBJECT_ATTRIBUTES DpcObjectAttributes;
   1.262    WDF_DEVICE_STATE DeviceState;
   1.263 -  UCHAR ScsiMinors[1] = { IRP_MN_SCSI_CLASS };
   1.264 +  //UCHAR ScsiMinors[1] = { IRP_MN_SCSI_CLASS };
   1.265  
   1.266    UNREFERENCED_PARAMETER(ChildList);
   1.267  
   1.268 @@ -1064,9 +1070,9 @@ XenVbd_PutIrpOnRing(WDFDEVICE Device, PI
   1.269    PXENVBD_CHILD_DEVICE_DATA ChildDeviceData;
   1.270    blkif_request_t *req;
   1.271    int i;
   1.272 -  int j;
   1.273 +  ULONG j;
   1.274    int BlockCount;
   1.275 -  int sect_offset;
   1.276 +  UINT8 sect_offset;
   1.277  
   1.278    ChildDeviceData = *GetChildDeviceData(Device);
   1.279  
   1.280 @@ -1125,7 +1131,7 @@ XenVbd_PutIrpOnRing(WDFDEVICE Device, PI
   1.281  //    KdPrint((__DRIVER_NAME "     AddedToList = %d, RemovedFromList = %d, AddedToRing = %d, AddedToRingAtLastNotify = %d, AddedToRingAtLastInterrupt = %d, AddedToRingAtLastDpc = %d, RemovedFromRing = %d, IrpCompleted = %d\n", ChildDeviceData->IrpAddedToList, ChildDeviceData->IrpRemovedFromList, ChildDeviceData->IrpAddedToRing, ChildDeviceData->IrpAddedToRingAtLastNotify, ChildDeviceData->IrpAddedToRingAtLastInterrupt, ChildDeviceData->IrpAddedToRingAtLastDpc, ChildDeviceData->IrpRemovedFromRing, ChildDeviceData->IrpCompleted));
   1.282  //  }
   1.283  
   1.284 -  sect_offset = MmGetMdlByteOffset(ChildDeviceData->shadow[req->id].Mdl) >> 9;
   1.285 +  sect_offset = (UINT8)(MmGetMdlByteOffset(ChildDeviceData->shadow[req->id].Mdl) >> 9);
   1.286    for (i = 0, req->nr_segments = 0; i < BlockCount; req->nr_segments++)
   1.287    {
   1.288      req->seg[req->nr_segments].gref = GntTblInterface.GrantAccess(
   1.289 @@ -1173,10 +1179,10 @@ XenVBD_FillModePage(PXENVBD_CHILD_DEVICE
   1.290      ModeRigidGeometry->PageCode = PageCode;
   1.291      ModeRigidGeometry->PageSavable = 0;
   1.292      ModeRigidGeometry->PageLength = sizeof(MODE_RIGID_GEOMETRY_PAGE);
   1.293 -    ModeRigidGeometry->NumberOfCylinders[0] = (ChildDeviceData->Geometry.Cylinders.LowPart >> 16) & 0xFF;
   1.294 -    ModeRigidGeometry->NumberOfCylinders[1] = (ChildDeviceData->Geometry.Cylinders.LowPart >> 8) & 0xFF;
   1.295 -    ModeRigidGeometry->NumberOfCylinders[2] = (ChildDeviceData->Geometry.Cylinders.LowPart >> 0) & 0xFF;
   1.296 -    ModeRigidGeometry->NumberOfHeads = ChildDeviceData->Geometry.TracksPerCylinder;
   1.297 +    ModeRigidGeometry->NumberOfCylinders[0] = (UCHAR)((ChildDeviceData->Geometry.Cylinders.LowPart >> 16) & 0xFF);
   1.298 +    ModeRigidGeometry->NumberOfCylinders[1] = (UCHAR)((ChildDeviceData->Geometry.Cylinders.LowPart >> 8) & 0xFF);
   1.299 +    ModeRigidGeometry->NumberOfCylinders[2] = (UCHAR)((ChildDeviceData->Geometry.Cylinders.LowPart >> 0) & 0xFF);
   1.300 +    ModeRigidGeometry->NumberOfHeads = (UCHAR)ChildDeviceData->Geometry.TracksPerCylinder;
   1.301      //ModeRigidGeometry->LandZoneCyclinder = 0;
   1.302      ModeRigidGeometry->RoataionRate[0] = 0x05;
   1.303      ModeRigidGeometry->RoataionRate[0] = 0x39;
   1.304 @@ -1424,6 +1430,7 @@ static VOID
   1.305  XenVbd_Child_IoReadWrite(WDFQUEUE Queue, WDFREQUEST Request, size_t Length)
   1.306  {
   1.307    UNREFERENCED_PARAMETER(Queue);
   1.308 +  UNREFERENCED_PARAMETER(Length);
   1.309  
   1.310    KdPrint((__DRIVER_NAME " --> IoReadWrite\n"));
   1.311  
   1.312 @@ -1652,8 +1659,19 @@ XenVbd_Child_IoDeviceControl(WDFQUEUE Qu
   1.313  }
   1.314  
   1.315  static VOID 
   1.316 -XenVbd_Child_IoInternalDeviceControl(WDFQUEUE Queue, WDFREQUEST Request, size_t OutputBufferLength, size_t InputBufferLength, ULONG IoControlCode)
   1.317 +XenVbd_Child_IoInternalDeviceControl(
   1.318 +  WDFQUEUE Queue,
   1.319 +  WDFREQUEST Request,
   1.320 +  size_t OutputBufferLength,
   1.321 +  size_t InputBufferLength,
   1.322 +  ULONG IoControlCode)
   1.323  {
   1.324 +  UNREFERENCED_PARAMETER(Queue);
   1.325 +  UNREFERENCED_PARAMETER(Request);
   1.326 +  UNREFERENCED_PARAMETER(OutputBufferLength);
   1.327 +  UNREFERENCED_PARAMETER(InputBufferLength);
   1.328 +  UNREFERENCED_PARAMETER(IoControlCode);
   1.329 +
   1.330    KdPrint((__DRIVER_NAME " --> IoInternalDeviceControl\n"));
   1.331    KdPrint((__DRIVER_NAME " <-- IoInternalDeviceControl\n"));
   1.332  }