Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

420 lines
11 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1998
  3. Module Name:
  4. pmwmireg.c
  5. Abstract:
  6. This file contains routines to register for and handle WMI queries.
  7. Author:
  8. Bruce Worthington 26-Oct-1998
  9. Environment:
  10. kernel mode only
  11. Notes:
  12. Revision History:
  13. --*/
  14. #define RTL_USE_AVL_TABLES 0
  15. #include <ntosp.h>
  16. #include <stdio.h>
  17. #include <ntddvol.h>
  18. #include <ntdddisk.h>
  19. #include <wdmguid.h>
  20. #include <volmgr.h>
  21. #include <wmistr.h>
  22. #include <wmikm.h>
  23. #include <wmilib.h>
  24. #include <partmgr.h>
  25. #include <pmwmicnt.h>
  26. #include <initguid.h>
  27. #include <wmiguid.h>
  28. #include <zwapi.h>
  29. NTSTATUS PmRegisterDevice(
  30. IN PDEVICE_OBJECT DeviceObject
  31. );
  32. NTSTATUS
  33. PmQueryWmiRegInfo(
  34. IN PDEVICE_OBJECT DeviceObject,
  35. OUT ULONG *RegFlags,
  36. OUT PUNICODE_STRING InstanceName,
  37. OUT PUNICODE_STRING *RegistryPath,
  38. OUT PUNICODE_STRING MofResourceName,
  39. OUT PDEVICE_OBJECT *Pdo
  40. );
  41. NTSTATUS
  42. PmQueryWmiDataBlock(
  43. IN PDEVICE_OBJECT DeviceObject,
  44. IN PIRP Irp,
  45. IN ULONG GuidIndex,
  46. IN ULONG InstanceIndex,
  47. IN ULONG InstanceCount,
  48. IN OUT PULONG InstanceLengthArray,
  49. IN ULONG BufferAvail,
  50. OUT PUCHAR Buffer
  51. );
  52. BOOLEAN
  53. PmQueryEnableAlways(
  54. IN PDEVICE_OBJECT DeviceObject
  55. );
  56. WMIGUIDREGINFO DiskperfGuidList[] =
  57. {
  58. { &DiskPerfGuid,
  59. 1,
  60. 0
  61. }
  62. };
  63. ULONG DiskperfGuidCount = (sizeof(DiskperfGuidList) / sizeof(WMIGUIDREGINFO));
  64. #ifdef ALLOC_PRAGMA
  65. #pragma alloc_text (PAGE, PmRegisterDevice)
  66. #pragma alloc_text (PAGE, PmQueryWmiRegInfo)
  67. #pragma alloc_text (PAGE, PmQueryWmiDataBlock)
  68. #pragma alloc_text (PAGE, PmQueryEnableAlways)
  69. #endif
  70. NTSTATUS
  71. PmRegisterDevice(
  72. IN PDEVICE_OBJECT DeviceObject
  73. )
  74. /*++
  75. Routine Description:
  76. Routine to initialize a proper name for the device object, and
  77. register it with WMI
  78. Arguments:
  79. DeviceObject - pointer to a device object to be initialized.
  80. Return Value:
  81. Status of the initialization. NOTE: If the registration fails,
  82. the device name in the DeviceExtension will be left as empty.
  83. --*/
  84. {
  85. NTSTATUS status;
  86. IO_STATUS_BLOCK ioStatus;
  87. KEVENT event;
  88. PDEVICE_EXTENSION deviceExtension;
  89. PIRP irp;
  90. STORAGE_DEVICE_NUMBER number;
  91. ULONG registrationFlag = 0;
  92. PAGED_CODE();
  93. deviceExtension = DeviceObject->DeviceExtension;
  94. KeInitializeEvent(&event, NotificationEvent, FALSE);
  95. //
  96. // Request for the device number
  97. //
  98. irp = IoBuildDeviceIoControlRequest(
  99. IOCTL_STORAGE_GET_DEVICE_NUMBER,
  100. deviceExtension->TargetObject,
  101. NULL,
  102. 0,
  103. &number,
  104. sizeof(number),
  105. FALSE,
  106. &event,
  107. &ioStatus);
  108. if (!irp) {
  109. return STATUS_INSUFFICIENT_RESOURCES;
  110. }
  111. status = IoCallDriver(deviceExtension->TargetObject, irp);
  112. if (status == STATUS_PENDING) {
  113. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  114. status = ioStatus.Status;
  115. }
  116. if (!NT_SUCCESS(status)) {
  117. return status;
  118. }
  119. //
  120. // Remember the disk number for use as parameter in
  121. // PhysicalDiskIoNotifyRoutine
  122. //
  123. deviceExtension->DiskNumber = number.DeviceNumber;
  124. deviceExtension->PhysicalDeviceName.Length = 0;
  125. deviceExtension->PhysicalDeviceName.MaximumLength = DEVICENAME_MAXSTR;
  126. // Create device name for each partition
  127. swprintf(deviceExtension->PhysicalDeviceNameBuffer,
  128. L"\\Device\\Harddisk%d\\Partition%d",
  129. number.DeviceNumber, number.PartitionNumber);
  130. RtlInitUnicodeString(
  131. &deviceExtension->PhysicalDeviceName,
  132. &deviceExtension->PhysicalDeviceNameBuffer[0]);
  133. if (number.PartitionNumber == 0) {
  134. registrationFlag = WMIREG_FLAG_TRACE_PROVIDER | WMIREG_NOTIFY_DISK_IO;
  135. }
  136. status = IoWMIRegistrationControl(DeviceObject,
  137. WMIREG_ACTION_REGISTER | registrationFlag );
  138. if (NT_SUCCESS(status)) {
  139. PmWmiCounterEnable(&deviceExtension->PmWmiCounterContext);
  140. PmWmiCounterDisable(&deviceExtension->PmWmiCounterContext,
  141. FALSE, FALSE);
  142. }
  143. return status;
  144. }
  145. NTSTATUS
  146. PmQueryWmiRegInfo(
  147. IN PDEVICE_OBJECT DeviceObject,
  148. OUT ULONG *RegFlags,
  149. OUT PUNICODE_STRING InstanceName,
  150. OUT PUNICODE_STRING *RegistryPath,
  151. OUT PUNICODE_STRING MofResourceName,
  152. OUT PDEVICE_OBJECT *Pdo
  153. )
  154. /*++
  155. Routine Description:
  156. This routine is a callback into the driver to retrieve information about
  157. the guids being registered.
  158. Implementations of this routine may be in paged memory
  159. Arguments:
  160. DeviceObject is the device whose registration information is needed
  161. *RegFlags returns with a set of flags that describe all of the guids being
  162. registered for this device. If the device wants enable and disable
  163. collection callbacks before receiving queries for the registered
  164. guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
  165. returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
  166. the instance name is determined from the PDO associated with the
  167. device object. Note that the PDO must have an associated devnode. If
  168. WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
  169. name for the device. These flags are ORed into the flags specified
  170. by the GUIDREGINFO for each guid.
  171. InstanceName returns with the instance name for the guids if
  172. WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
  173. caller will call ExFreePool with the buffer returned.
  174. *RegistryPath returns with the registry path of the driver. This is
  175. required
  176. MofResourceName returns with the name of the MOF resource attached to
  177. the binary file. If the driver does not have a mof resource attached
  178. then this can be returned unmodified. If a value is returned then
  179. it is NOT freed.
  180. The MOF file is assumed to be already included in wmicore.mof
  181. *Pdo returns with the device object for the PDO associated with this
  182. device if the WMIREG_FLAG_INSTANCE_PDO flag is retured in
  183. *RegFlags.
  184. Return Value:
  185. status
  186. --*/
  187. {
  188. NTSTATUS status;
  189. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  190. PAGED_CODE();
  191. InstanceName->Buffer = (PUSHORT) NULL;
  192. *RegistryPath = &deviceExtension->DriverExtension->DiskPerfRegistryPath;
  193. *RegFlags = WMIREG_FLAG_INSTANCE_PDO | WMIREG_FLAG_EXPENSIVE;
  194. *Pdo = deviceExtension->Pdo;
  195. status = STATUS_SUCCESS;
  196. return(status);
  197. }
  198. NTSTATUS
  199. PmQueryWmiDataBlock(
  200. IN PDEVICE_OBJECT DeviceObject,
  201. IN PIRP Irp,
  202. IN ULONG GuidIndex,
  203. IN ULONG InstanceIndex,
  204. IN ULONG InstanceCount,
  205. IN OUT PULONG InstanceLengthArray,
  206. IN ULONG BufferAvail,
  207. OUT PUCHAR Buffer
  208. )
  209. /*++
  210. Routine Description:
  211. This routine is a callback into the driver to query for the contents of
  212. all instances of a data block. When the driver has finished filling the
  213. data block it must call WmiCompleteRequest to complete the irp. The
  214. driver can return STATUS_PENDING if the irp cannot be completed
  215. immediately.
  216. Arguments:
  217. DeviceObject is the device whose data block is being queried
  218. Irp is the Irp that makes this request
  219. GuidIndex is the index into the list of guids provided when the
  220. device registered
  221. InstanceCount is the number of instnaces expected to be returned for
  222. the data block.
  223. InstanceLengthArray is a pointer to an array of ULONG that returns the
  224. lengths of each instance of the data block. If this is NULL then
  225. there was not enough space in the output buffer to fufill the request
  226. so the irp should be completed with the buffer needed.
  227. BufferAvail on entry has the maximum size available to write the data
  228. blocks.
  229. Buffer on return is filled with the returned data blocks. Note that each
  230. instance of the data block must be aligned on a 8 byte boundry.
  231. Return Value:
  232. status
  233. --*/
  234. {
  235. NTSTATUS status;
  236. PDEVICE_EXTENSION deviceExtension;
  237. ULONG sizeNeeded = 0;
  238. KIRQL currentIrql;
  239. PWCHAR diskNamePtr;
  240. PAGED_CODE();
  241. deviceExtension = DeviceObject->DeviceExtension;
  242. if (GuidIndex == 0)
  243. {
  244. if (!(deviceExtension->CountersEnabled)) {
  245. status = STATUS_UNSUCCESSFUL;
  246. } else {
  247. sizeNeeded = ((sizeof(DISK_PERFORMANCE) + 1) & ~1)
  248. + deviceExtension->PhysicalDeviceName.Length
  249. + sizeof(UNICODE_NULL);
  250. if (BufferAvail >= sizeNeeded) {
  251. PmWmiCounterQuery(deviceExtension->PmWmiCounterContext,
  252. (PDISK_PERFORMANCE) Buffer, L"Partmgr ",
  253. deviceExtension->DiskNumber);
  254. diskNamePtr = (PWCHAR)(Buffer +
  255. ((sizeof(DISK_PERFORMANCE) + 1) & ~1));
  256. *diskNamePtr++ = deviceExtension->PhysicalDeviceName.Length;
  257. RtlCopyMemory(diskNamePtr,
  258. deviceExtension->PhysicalDeviceName.Buffer,
  259. deviceExtension->PhysicalDeviceName.Length);
  260. *InstanceLengthArray = sizeNeeded;
  261. status = STATUS_SUCCESS;
  262. } else {
  263. status = STATUS_BUFFER_TOO_SMALL;
  264. }
  265. }
  266. } else {
  267. status = STATUS_WMI_GUID_NOT_FOUND;
  268. }
  269. status = WmiCompleteRequest( DeviceObject, Irp, status, sizeNeeded,
  270. IO_NO_INCREMENT);
  271. return status;
  272. }
  273. BOOLEAN
  274. PmQueryEnableAlways(
  275. IN PDEVICE_OBJECT DeviceObject
  276. )
  277. {
  278. NTSTATUS status;
  279. UNICODE_STRING uString;
  280. OBJECT_ATTRIBUTES objAttributes;
  281. PKEY_VALUE_PARTIAL_INFORMATION keyValue;
  282. ULONG Buffer[4]; // sizeof keyValue + ULONG
  283. ULONG enableAlways = 0;
  284. PDEVICE_EXTENSION extension = DeviceObject->DeviceExtension;
  285. HANDLE keyHandle;
  286. ULONG returnLength;
  287. PAGED_CODE();
  288. RtlInitUnicodeString(&uString, L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\Partmgr");
  289. InitializeObjectAttributes(
  290. &objAttributes,
  291. &uString,
  292. OBJ_CASE_INSENSITIVE,
  293. NULL,
  294. NULL
  295. );
  296. status = ZwOpenKey(&keyHandle, KEY_READ, &objAttributes);
  297. if (NT_SUCCESS(status)) {
  298. RtlInitUnicodeString(&uString, L"EnableCounterForIoctl");
  299. status = ZwQueryValueKey(keyHandle, &uString,
  300. KeyValuePartialInformation,
  301. Buffer,
  302. sizeof(Buffer),
  303. &returnLength);
  304. keyValue = (PKEY_VALUE_PARTIAL_INFORMATION) &Buffer[0];
  305. if (NT_SUCCESS(status) && (keyValue->DataLength == sizeof(ULONG))) {
  306. enableAlways = *((PULONG) keyValue->Data);
  307. }
  308. ZwClose(keyHandle);
  309. }
  310. if (enableAlways == 1) {
  311. if (InterlockedCompareExchange(&extension->EnableAlways, 1, 0) == 0) {
  312. status = PmWmiCounterEnable(&extension->PmWmiCounterContext);
  313. if (NT_SUCCESS(status)) {
  314. extension->CountersEnabled = TRUE;
  315. return TRUE;
  316. }
  317. else {
  318. InterlockedExchange(&extension->EnableAlways, 0);
  319. }
  320. }
  321. }
  322. return FALSE;
  323. }