Leaked source code of windows server 2003
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.

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