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.

119 lines
3.0 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. ntutils.c
  5. Abstract:
  6. This module contains various tools from NT land. Made a separate
  7. file because of the use of various nt headers.
  8. --*/
  9. #include <ntreppch.h>
  10. #pragma hdrstop
  11. #include <frs.h>
  12. #include <ntdddisk.h>
  13. BOOL
  14. FrsIsDiskWriteCacheEnabled(
  15. IN PWCHAR Path
  16. )
  17. /*++
  18. Description:
  19. Determines if the disk has enabled write caching.
  20. Arguments:
  21. Path - Fully qualified path of a file or directory
  22. Return value:
  23. TRUE if write cache is enabled, FALSE otherwise.
  24. --*/
  25. {
  26. #undef DEBSUB
  27. #define DEBSUB "FrsIsDiskWriteCacheEnabled:"
  28. DWORD WStatus;
  29. ULONG bytesTransferred;
  30. PWCHAR Volume = NULL;
  31. HANDLE driveHandle = INVALID_HANDLE_VALUE;
  32. DISK_CACHE_INFORMATION cacheInfo;
  33. cacheInfo.WriteCacheEnabled = FALSE;
  34. try {
  35. //
  36. // Extract the volume from Path
  37. //
  38. Volume = FrsWcsVolume(Path);
  39. if (!Volume) {
  40. goto CLEANUP;
  41. }
  42. //
  43. // Open handle to the PhysicalDrive
  44. //
  45. DPRINT1(4, ":S: Checking the write cache state on %ws\n", Volume);
  46. driveHandle = CreateFile(Volume,
  47. GENERIC_READ | GENERIC_WRITE,
  48. FILE_SHARE_READ | FILE_SHARE_WRITE,
  49. NULL,
  50. OPEN_EXISTING,
  51. 0,
  52. NULL);
  53. if (!HANDLE_IS_VALID(driveHandle)) {
  54. WStatus = GetLastError();
  55. DPRINT1_WS(4, ":S: WARN - Could not open drive %ws;", Volume, WStatus);
  56. goto CLEANUP;
  57. }
  58. //
  59. // Get cache info - IOCTL_DISK_GET_CACHE_INFORMATION
  60. //
  61. if (!DeviceIoControl(driveHandle,
  62. IOCTL_DISK_GET_CACHE_INFORMATION,
  63. NULL,
  64. 0,
  65. &cacheInfo,
  66. sizeof(DISK_CACHE_INFORMATION),
  67. &bytesTransferred,
  68. NULL)) {
  69. WStatus = GetLastError();
  70. DPRINT1_WS(4, ":S: WARN - DeviceIoControl(%ws);", Volume, WStatus);
  71. goto CLEANUP;
  72. }
  73. DPRINT2(4, ":S: NEW IOCTL: Write cache on %ws is %s\n",
  74. Volume, (cacheInfo.WriteCacheEnabled) ? "Enabled (WARNING)" : "Disabled");
  75. CLEANUP:;
  76. } except (EXCEPTION_EXECUTE_HANDLER) {
  77. GET_EXCEPTION_CODE(WStatus);
  78. DPRINT_WS(0, "ERROR - Exception.", WStatus);
  79. }
  80. //
  81. // Cleanup handles, memory, ...
  82. //
  83. try {
  84. FRS_CLOSE(driveHandle );
  85. FrsFree(Volume);
  86. } except (EXCEPTION_EXECUTE_HANDLER) {
  87. GET_EXCEPTION_CODE(WStatus);
  88. DPRINT_WS(0, "ERROR - Cleanup Exception.", WStatus);
  89. }
  90. return cacheInfo.WriteCacheEnabled;
  91. } // IsDiskWriteCacheEnabled