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.

314 lines
7.0 KiB

  1. #include "stdafx.hxx"
  2. #include "vss.h"
  3. #include "vswriter.h"
  4. #include "vsbackup.h"
  5. #include <debug.h>
  6. #include <time.h>
  7. #include <vs_inc.hxx>
  8. #include <vsevent.h>
  9. #include <vscoordint.h>
  10. #include <vdslun.h>
  11. #include <vs_wmxml.hxx>
  12. #include <vs_cmxml.hxx>
  13. #include <vs_trace.hxx>
  14. void PrintVolumeInfo(LPWSTR wszVolume)
  15. {
  16. wprintf(L"\n\nInformation for volume %s\n\n", wszVolume);
  17. // get rid of last backslash
  18. wszVolume[wcslen(wszVolume)-1] = L'\0';
  19. HANDLE hVol = CreateFile
  20. (
  21. wszVolume,
  22. GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  23. NULL,
  24. OPEN_EXISTING,
  25. FILE_ATTRIBUTE_NORMAL,
  26. NULL
  27. );
  28. if (hVol == INVALID_HANDLE_VALUE)
  29. {
  30. DWORD dwErr = GetLastError();
  31. wprintf(L"CreateFile of volume failed with error %d.\n", dwErr);
  32. return;
  33. }
  34. WCHAR bufExtents[1024];
  35. DWORD size;
  36. BOOL b = DeviceIoControl
  37. (
  38. hVol,
  39. IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
  40. NULL,
  41. 0,
  42. (PVOID) bufExtents,
  43. sizeof(bufExtents),
  44. &size,
  45. NULL
  46. );
  47. if (!b)
  48. {
  49. DWORD dwErr = GetLastError();
  50. wprintf(L"IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed with error %d.\n", dwErr);
  51. CloseHandle(hVol);
  52. return;
  53. }
  54. VOLUME_DISK_EXTENTS *pDiskExtents = (VOLUME_DISK_EXTENTS *) bufExtents;
  55. ULONG PrevDiskNo = 0xffffffff;
  56. printf("# of extents = %d\n\n", pDiskExtents->NumberOfDiskExtents);
  57. for(UINT iExtent = 0; iExtent < pDiskExtents->NumberOfDiskExtents; iExtent++)
  58. {
  59. ULONG DiskNo = pDiskExtents->Extents[iExtent].DiskNumber;
  60. printf("Extent %d:\nDisk %d, Low=0x%I64lx, Length=0x%I64lx\n\n",
  61. iExtent,
  62. DiskNo,
  63. pDiskExtents->Extents[iExtent].StartingOffset,
  64. pDiskExtents->Extents[iExtent].ExtentLength
  65. );
  66. if (DiskNo != PrevDiskNo)
  67. {
  68. PrevDiskNo = DiskNo;
  69. WCHAR wszbuf[32];
  70. swprintf(wszbuf, L"\\\\.\\PHYSICALDRIVE%u", DiskNo);
  71. HANDLE hDisk = CreateFile
  72. (
  73. wszbuf,
  74. GENERIC_READ|GENERIC_WRITE,
  75. FILE_SHARE_READ,
  76. NULL,
  77. OPEN_EXISTING,
  78. 0,
  79. NULL
  80. );
  81. if (hDisk == INVALID_HANDLE_VALUE)
  82. {
  83. DWORD dwErr = GetLastError();
  84. wprintf(L"Cannot open disk %d due to error %d. Skipping\n", DiskNo, dwErr);
  85. continue;
  86. }
  87. STORAGE_PROPERTY_QUERY query;
  88. query.PropertyId = StorageDeviceProperty;
  89. query.QueryType = PropertyStandardQuery;
  90. BYTE buf[1024];
  91. DWORD dwSize;
  92. if (!DeviceIoControl
  93. (
  94. hDisk,
  95. IOCTL_STORAGE_QUERY_PROPERTY,
  96. &query,
  97. sizeof(query),
  98. buf,
  99. 1024,
  100. &dwSize,
  101. NULL
  102. ))
  103. {
  104. DWORD dwErr = GetLastError();
  105. if (dwErr != ERROR_NOT_SUPPORTED)
  106. wprintf(L"IOCTL_STORAGE_QUERY_PROPERTY failed due to error %d.\n", dwErr);
  107. CloseHandle(hDisk);
  108. continue;
  109. }
  110. STORAGE_DEVICE_DESCRIPTOR *pDesc = (STORAGE_DEVICE_DESCRIPTOR *) buf;
  111. printf("Information for disk %d.\n\nbus=", DiskNo);
  112. switch (pDesc->BusType)
  113. {
  114. default:
  115. printf("(other)\n");
  116. break;
  117. case BusTypeScsi:
  118. printf("(SCSI)\n");
  119. break;
  120. case BusTypeAtapi:
  121. printf("(ATAPI)\n");
  122. break;
  123. case BusTypeAta:
  124. printf("(ATA)\n");
  125. break;
  126. case BusType1394:
  127. printf("(1394)\n");
  128. break;
  129. case BusTypeSsa:
  130. printf("(SSA)\n");
  131. break;
  132. case BusTypeFibre:
  133. printf("(Fibre)\n");
  134. break;
  135. case BusTypeUsb:
  136. printf("(Usb)\n");
  137. break;
  138. case BusTypeRAID:
  139. printf("(RAID)\n");
  140. break;
  141. }
  142. if (pDesc->VendorIdOffset)
  143. {
  144. LPSTR szVendor = (LPSTR)((BYTE *) pDesc + pDesc->VendorIdOffset);
  145. printf("VendorId: %s\n", szVendor);
  146. }
  147. if (pDesc->ProductIdOffset)
  148. {
  149. LPSTR szProduct = (LPSTR)((BYTE *) pDesc + pDesc->ProductIdOffset);
  150. printf("ProductId: %s\n", szProduct);
  151. }
  152. if (pDesc->ProductRevisionOffset)
  153. {
  154. LPSTR szRevision = (LPSTR)((BYTE *) pDesc + pDesc->ProductRevisionOffset);
  155. printf("RevisionId: %s\n", szRevision);
  156. }
  157. if (pDesc->SerialNumberOffset)
  158. {
  159. LPSTR szSerialNo = (LPSTR)((BYTE *) pDesc + pDesc->SerialNumberOffset);
  160. printf("Serial#: %s\n", szSerialNo);
  161. }
  162. query.PropertyId = StorageDeviceIdProperty;
  163. query.QueryType = PropertyStandardQuery;
  164. if (!DeviceIoControl
  165. (
  166. hDisk,
  167. IOCTL_STORAGE_QUERY_PROPERTY,
  168. &query,
  169. sizeof(query),
  170. buf,
  171. 1024,
  172. &dwSize,
  173. NULL
  174. ))
  175. {
  176. DWORD dwErr = GetLastError();
  177. if (dwErr != ERROR_NOT_SUPPORTED)
  178. wprintf(L"IOCTL_STORAGE_QUERY_PROPERTY failed due to error %d.\n", dwErr);
  179. CloseHandle(hDisk);
  180. continue;
  181. }
  182. STORAGE_DEVICE_ID_DESCRIPTOR *pDevId = (STORAGE_DEVICE_ID_DESCRIPTOR *) buf;
  183. printf("# of identifiers = %d\n", pDevId->NumberOfIdentifiers);
  184. STORAGE_IDENTIFIER *pId = (STORAGE_IDENTIFIER *) pDevId->Identifiers;
  185. for(UINT i = 0; i < pDevId->NumberOfIdentifiers; i++)
  186. {
  187. switch(pId->Type)
  188. {
  189. default:
  190. printf("(other) ");
  191. break;
  192. case StorageIdTypeVendorSpecific:
  193. printf("(vendor specific) ");
  194. break;
  195. case StorageIdTypeVendorId:
  196. printf("(vendor id) ");
  197. break;
  198. case StorageIdTypeEUI64:
  199. printf("(EUI64) ");
  200. break;
  201. case StorageIdTypeFCPHName:
  202. printf("(FCPHName) ");
  203. break;
  204. }
  205. if (pId->CodeSet == StorageIdCodeSetAscii)
  206. printf("%s\n", pId->Identifier);
  207. else
  208. {
  209. for(UINT i = 0; i < pId->IdentifierSize; i++)
  210. {
  211. printf("%2x ", pId->Identifier[i]);
  212. if ((i % 16) == 0 && i > 0)
  213. printf("\n");
  214. }
  215. }
  216. pId = (STORAGE_IDENTIFIER *) ((BYTE *) pId + pId->NextOffset);
  217. }
  218. CloseHandle(hDisk);
  219. }
  220. }
  221. }
  222. void EnumVolumes()
  223. {
  224. CVssFunctionTracer ft(VSSDBG_XML, L"EnumVolumes");
  225. HANDLE h = INVALID_HANDLE_VALUE;
  226. try
  227. {
  228. WCHAR volName[1024];
  229. h = FindFirstVolume(volName, sizeof(volName)/sizeof(WCHAR));
  230. if (h == INVALID_HANDLE_VALUE)
  231. {
  232. DWORD dwErr = GetLastError();
  233. Error(E_UNEXPECTED, L"FindFirstVolume failed due to error %d.\n", dwErr);
  234. }
  235. while(TRUE)
  236. {
  237. PrintVolumeInfo(volName);
  238. if (!FindNextVolume(h, volName, sizeof(volName)/ sizeof(WCHAR)))
  239. {
  240. DWORD dwErr = GetLastError();
  241. if (dwErr == ERROR_NO_MORE_FILES)
  242. break;
  243. else
  244. Error(E_UNEXPECTED, L"Unexpected error %d from FindNextVolume.\n", dwErr);
  245. }
  246. }
  247. if (!FindVolumeClose(h))
  248. {
  249. DWORD dwErr = GetLastError();
  250. Error(E_UNEXPECTED, L"Cannot close volume handle due to error %d.\n", dwErr);
  251. }
  252. h = INVALID_HANDLE_VALUE;
  253. }
  254. VSS_STANDARD_CATCH(ft)
  255. if (h != INVALID_HANDLE_VALUE)
  256. FindClose(h);
  257. }