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.

677 lines
19 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. FatInit.c
  5. Abstract:
  6. This module implements the DRIVER_INITIALIZATION routine for Fat
  7. // @@BEGIN_DDKSPLIT
  8. Author:
  9. Gary Kimura [GaryKi] 28-Dec-1989
  10. Revision History:
  11. // @@END_DDKSPLIT
  12. --*/
  13. #include "FatProcs.h"
  14. NTSTATUS
  15. DriverEntry(
  16. IN PDRIVER_OBJECT DriverObject,
  17. IN PUNICODE_STRING RegistryPath
  18. );
  19. VOID
  20. FatUnload(
  21. IN PDRIVER_OBJECT DriverObject
  22. );
  23. NTSTATUS
  24. FatGetCompatibilityModeValue(
  25. IN PUNICODE_STRING ValueName,
  26. IN OUT PULONG Value
  27. );
  28. BOOLEAN
  29. FatIsFujitsuFMR (
  30. );
  31. #ifdef ALLOC_PRAGMA
  32. #pragma alloc_text(INIT, DriverEntry)
  33. #pragma alloc_text(INIT, FatGetCompatibilityModeValue)
  34. #pragma alloc_text(INIT, FatIsFujitsuFMR)
  35. //#pragma alloc_text(PAGE, FatUnload)
  36. #endif
  37. #define COMPATIBILITY_MODE_KEY_NAME L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\FileSystem"
  38. #define COMPATIBILITY_MODE_VALUE_NAME L"Win31FileSystem"
  39. #define CODE_PAGE_INVARIANCE_VALUE_NAME L"FatDisableCodePageInvariance"
  40. #define KEY_WORK_AREA ((sizeof(KEY_VALUE_FULL_INFORMATION) + \
  41. sizeof(ULONG)) + 64)
  42. #define REGISTRY_HARDWARE_DESCRIPTION_W \
  43. L"\\Registry\\Machine\\Hardware\\DESCRIPTION\\System"
  44. #define REGISTRY_MACHINE_IDENTIFIER_W L"Identifier"
  45. #define FUJITSU_FMR_NAME_W L"FUJITSU FMR-"
  46. NTSTATUS
  47. DriverEntry(
  48. IN PDRIVER_OBJECT DriverObject,
  49. IN PUNICODE_STRING RegistryPath
  50. )
  51. /*++
  52. Routine Description:
  53. This is the initialization routine for the Fat file system
  54. device driver. This routine creates the device object for the FileSystem
  55. device and performs all other driver initialization.
  56. Arguments:
  57. DriverObject - Pointer to driver object created by the system.
  58. Return Value:
  59. NTSTATUS - The function value is the final status from the initialization
  60. operation.
  61. --*/
  62. {
  63. USHORT MaxDepth;
  64. NTSTATUS Status;
  65. UNICODE_STRING UnicodeString;
  66. UNICODE_STRING ValueName;
  67. ULONG Value;
  68. //
  69. // Create the device object for disks. To avoid problems with filters who
  70. // know this name, we must keep it.
  71. //
  72. RtlInitUnicodeString( &UnicodeString, L"\\Fat" );
  73. Status = IoCreateDevice( DriverObject,
  74. 0,
  75. &UnicodeString,
  76. FILE_DEVICE_DISK_FILE_SYSTEM,
  77. 0,
  78. FALSE,
  79. &FatDiskFileSystemDeviceObject );
  80. if (!NT_SUCCESS( Status )) {
  81. return Status;
  82. }
  83. //
  84. // Create the device object for "cdroms".
  85. //
  86. RtlInitUnicodeString( &UnicodeString, L"\\FatCdrom" );
  87. Status = IoCreateDevice( DriverObject,
  88. 0,
  89. &UnicodeString,
  90. FILE_DEVICE_CD_ROM_FILE_SYSTEM,
  91. 0,
  92. FALSE,
  93. &FatCdromFileSystemDeviceObject );
  94. if (!NT_SUCCESS( Status )) {
  95. IoDeleteDevice( FatDiskFileSystemDeviceObject);
  96. return Status;
  97. }
  98. DriverObject->DriverUnload = FatUnload;
  99. #ifdef _PNP_POWER_
  100. //
  101. // This driver doesn't talk directly to a device, and (at the moment)
  102. // isn't otherwise concerned about power management.
  103. //
  104. FatDiskFileSystemDeviceObject->DeviceObjectExtension->PowerControlNeeded = FALSE;
  105. FatCdromFileSystemDeviceObject->DeviceObjectExtension->PowerControlNeeded = FALSE;
  106. #endif
  107. //
  108. // Note that because of the way data caching is done, we set neither
  109. // the Direct I/O or Buffered I/O bit in DeviceObject->Flags. If
  110. // data is not in the cache, or the request is not buffered, we may,
  111. // set up for Direct I/O by hand.
  112. //
  113. //
  114. // Initialize the driver object with this driver's entry points.
  115. //
  116. DriverObject->MajorFunction[IRP_MJ_CREATE] = (PDRIVER_DISPATCH)FatFsdCreate;
  117. DriverObject->MajorFunction[IRP_MJ_CLOSE] = (PDRIVER_DISPATCH)FatFsdClose;
  118. DriverObject->MajorFunction[IRP_MJ_READ] = (PDRIVER_DISPATCH)FatFsdRead;
  119. DriverObject->MajorFunction[IRP_MJ_WRITE] = (PDRIVER_DISPATCH)FatFsdWrite;
  120. DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = (PDRIVER_DISPATCH)FatFsdQueryInformation;
  121. DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] = (PDRIVER_DISPATCH)FatFsdSetInformation;
  122. DriverObject->MajorFunction[IRP_MJ_QUERY_EA] = (PDRIVER_DISPATCH)FatFsdQueryEa;
  123. DriverObject->MajorFunction[IRP_MJ_SET_EA] = (PDRIVER_DISPATCH)FatFsdSetEa;
  124. DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS] = (PDRIVER_DISPATCH)FatFsdFlushBuffers;
  125. DriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] = (PDRIVER_DISPATCH)FatFsdQueryVolumeInformation;
  126. DriverObject->MajorFunction[IRP_MJ_SET_VOLUME_INFORMATION] = (PDRIVER_DISPATCH)FatFsdSetVolumeInformation;
  127. DriverObject->MajorFunction[IRP_MJ_CLEANUP] = (PDRIVER_DISPATCH)FatFsdCleanup;
  128. DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] = (PDRIVER_DISPATCH)FatFsdDirectoryControl;
  129. DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] = (PDRIVER_DISPATCH)FatFsdFileSystemControl;
  130. DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL] = (PDRIVER_DISPATCH)FatFsdLockControl;
  131. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = (PDRIVER_DISPATCH)FatFsdDeviceControl;
  132. DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = (PDRIVER_DISPATCH)FatFsdShutdown;
  133. DriverObject->MajorFunction[IRP_MJ_PNP] = (PDRIVER_DISPATCH)FatFsdPnp;
  134. DriverObject->FastIoDispatch = &FatFastIoDispatch;
  135. RtlZeroMemory(&FatFastIoDispatch, sizeof(FatFastIoDispatch));
  136. FatFastIoDispatch.SizeOfFastIoDispatch = sizeof(FAST_IO_DISPATCH);
  137. FatFastIoDispatch.FastIoCheckIfPossible = FatFastIoCheckIfPossible; // CheckForFastIo
  138. FatFastIoDispatch.FastIoRead = FsRtlCopyRead; // Read
  139. FatFastIoDispatch.FastIoWrite = FsRtlCopyWrite; // Write
  140. FatFastIoDispatch.FastIoQueryBasicInfo = FatFastQueryBasicInfo; // QueryBasicInfo
  141. FatFastIoDispatch.FastIoQueryStandardInfo = FatFastQueryStdInfo; // QueryStandardInfo
  142. FatFastIoDispatch.FastIoLock = FatFastLock; // Lock
  143. FatFastIoDispatch.FastIoUnlockSingle = FatFastUnlockSingle; // UnlockSingle
  144. FatFastIoDispatch.FastIoUnlockAll = FatFastUnlockAll; // UnlockAll
  145. FatFastIoDispatch.FastIoUnlockAllByKey = FatFastUnlockAllByKey; // UnlockAllByKey
  146. FatFastIoDispatch.FastIoQueryNetworkOpenInfo = FatFastQueryNetworkOpenInfo;
  147. FatFastIoDispatch.AcquireForCcFlush = FatAcquireForCcFlush;
  148. FatFastIoDispatch.ReleaseForCcFlush = FatReleaseForCcFlush;
  149. //
  150. // Initialize the global data structures
  151. //
  152. //
  153. // The FatData record
  154. //
  155. RtlZeroMemory( &FatData, sizeof(FAT_DATA));
  156. FatData.NodeTypeCode = FAT_NTC_DATA_HEADER;
  157. FatData.NodeByteSize = sizeof(FAT_DATA);
  158. InitializeListHead(&FatData.VcbQueue);
  159. FatData.DriverObject = DriverObject;
  160. FatData.DiskFileSystemDeviceObject = FatDiskFileSystemDeviceObject;
  161. FatData.CdromFileSystemDeviceObject = FatCdromFileSystemDeviceObject;
  162. //
  163. // This list head keeps track of closes yet to be done.
  164. //
  165. InitializeListHead( &FatData.AsyncCloseList );
  166. InitializeListHead( &FatData.DelayedCloseList );
  167. FatData.FatCloseItem = IoAllocateWorkItem( FatDiskFileSystemDeviceObject);
  168. if (FatData.FatCloseItem == NULL) {
  169. IoDeleteDevice (FatDiskFileSystemDeviceObject);
  170. IoDeleteDevice (FatCdromFileSystemDeviceObject);
  171. return STATUS_INSUFFICIENT_RESOURCES;
  172. }
  173. //
  174. // Now initialize our general purpose spinlock (gag) and figure out how
  175. // deep and wide we want our delayed lists (along with fooling ourselves
  176. // about the lookaside depths).
  177. //
  178. KeInitializeSpinLock( &FatData.GeneralSpinLock );
  179. switch ( MmQuerySystemSize() ) {
  180. case MmSmallSystem:
  181. MaxDepth = 4;
  182. FatMaxDelayedCloseCount = FAT_MAX_DELAYED_CLOSES;
  183. break;
  184. case MmMediumSystem:
  185. MaxDepth = 8;
  186. FatMaxDelayedCloseCount = 4 * FAT_MAX_DELAYED_CLOSES;
  187. break;
  188. case MmLargeSystem:
  189. MaxDepth = 16;
  190. FatMaxDelayedCloseCount = 16 * FAT_MAX_DELAYED_CLOSES;
  191. break;
  192. }
  193. //
  194. // Initialize the cache manager callback routines
  195. //
  196. FatData.CacheManagerCallbacks.AcquireForLazyWrite = &FatAcquireFcbForLazyWrite;
  197. FatData.CacheManagerCallbacks.ReleaseFromLazyWrite = &FatReleaseFcbFromLazyWrite;
  198. FatData.CacheManagerCallbacks.AcquireForReadAhead = &FatAcquireFcbForReadAhead;
  199. FatData.CacheManagerCallbacks.ReleaseFromReadAhead = &FatReleaseFcbFromReadAhead;
  200. FatData.CacheManagerNoOpCallbacks.AcquireForLazyWrite = &FatNoOpAcquire;
  201. FatData.CacheManagerNoOpCallbacks.ReleaseFromLazyWrite = &FatNoOpRelease;
  202. FatData.CacheManagerNoOpCallbacks.AcquireForReadAhead = &FatNoOpAcquire;
  203. FatData.CacheManagerNoOpCallbacks.ReleaseFromReadAhead = &FatNoOpRelease;
  204. //
  205. // Set up global pointer to our process.
  206. //
  207. FatData.OurProcess = PsGetCurrentProcess();
  208. //
  209. // Read the registry to determine if we are in ChicagoMode.
  210. //
  211. ValueName.Buffer = COMPATIBILITY_MODE_VALUE_NAME;
  212. ValueName.Length = sizeof(COMPATIBILITY_MODE_VALUE_NAME) - sizeof(WCHAR);
  213. ValueName.MaximumLength = sizeof(COMPATIBILITY_MODE_VALUE_NAME);
  214. Status = FatGetCompatibilityModeValue( &ValueName, &Value );
  215. if (NT_SUCCESS(Status) && FlagOn(Value, 1)) {
  216. FatData.ChicagoMode = FALSE;
  217. } else {
  218. FatData.ChicagoMode = TRUE;
  219. }
  220. //
  221. // Read the registry to determine if we are going to generate LFNs
  222. // for valid 8.3 names with extended characters.
  223. //
  224. ValueName.Buffer = CODE_PAGE_INVARIANCE_VALUE_NAME;
  225. ValueName.Length = sizeof(CODE_PAGE_INVARIANCE_VALUE_NAME) - sizeof(WCHAR);
  226. ValueName.MaximumLength = sizeof(CODE_PAGE_INVARIANCE_VALUE_NAME);
  227. Status = FatGetCompatibilityModeValue( &ValueName, &Value );
  228. if (NT_SUCCESS(Status) && FlagOn(Value, 1)) {
  229. FatData.CodePageInvariant = FALSE;
  230. } else {
  231. FatData.CodePageInvariant = TRUE;
  232. }
  233. //
  234. // Initialize our global resource and fire up the lookaside lists.
  235. //
  236. ExInitializeResourceLite( &FatData.Resource );
  237. ExInitializeNPagedLookasideList( &FatIrpContextLookasideList,
  238. NULL,
  239. NULL,
  240. POOL_RAISE_IF_ALLOCATION_FAILURE,
  241. sizeof(IRP_CONTEXT),
  242. TAG_IRP_CONTEXT,
  243. MaxDepth );
  244. ExInitializeNPagedLookasideList( &FatNonPagedFcbLookasideList,
  245. NULL,
  246. NULL,
  247. POOL_RAISE_IF_ALLOCATION_FAILURE,
  248. sizeof(NON_PAGED_FCB),
  249. TAG_FCB_NONPAGED,
  250. MaxDepth );
  251. ExInitializeNPagedLookasideList( &FatEResourceLookasideList,
  252. NULL,
  253. NULL,
  254. POOL_RAISE_IF_ALLOCATION_FAILURE,
  255. sizeof(ERESOURCE),
  256. TAG_ERESOURCE,
  257. MaxDepth );
  258. ExInitializeSListHead( &FatCloseContextSList );
  259. ExInitializeFastMutex( &FatCloseQueueMutex );
  260. KeInitializeEvent( &FatReserveEvent, SynchronizationEvent, TRUE );
  261. //
  262. // Register the file system with the I/O system
  263. //
  264. IoRegisterFileSystem(FatDiskFileSystemDeviceObject);
  265. ObReferenceObject (FatDiskFileSystemDeviceObject);
  266. IoRegisterFileSystem(FatCdromFileSystemDeviceObject);
  267. ObReferenceObject (FatCdromFileSystemDeviceObject);
  268. //
  269. // Find out if we are running an a FujitsuFMR machine.
  270. //
  271. FatData.FujitsuFMR = FatIsFujitsuFMR();
  272. //
  273. // And return to our caller
  274. //
  275. return( STATUS_SUCCESS );
  276. }
  277. VOID
  278. FatUnload(
  279. IN PDRIVER_OBJECT DriverObject
  280. )
  281. /*++
  282. Routine Description:
  283. This is the unload routine for the filesystem
  284. Arguments:
  285. DriverObject - Pointer to driver object created by the system.
  286. Return Value:
  287. None
  288. --*/
  289. {
  290. ExDeleteNPagedLookasideList (&FatEResourceLookasideList);
  291. ExDeleteNPagedLookasideList (&FatNonPagedFcbLookasideList);
  292. ExDeleteNPagedLookasideList (&FatIrpContextLookasideList);
  293. ExDeleteResourceLite( &FatData.Resource );
  294. IoFreeWorkItem (FatData.FatCloseItem);
  295. ObDereferenceObject( FatDiskFileSystemDeviceObject);
  296. ObDereferenceObject( FatCdromFileSystemDeviceObject);
  297. }
  298. //
  299. // Local Support routine
  300. //
  301. NTSTATUS
  302. FatGetCompatibilityModeValue (
  303. IN PUNICODE_STRING ValueName,
  304. IN OUT PULONG Value
  305. )
  306. /*++
  307. Routine Description:
  308. Given a unicode value name this routine will go into the registry
  309. location for the Chicago compatibilitymode information and get the
  310. value.
  311. Arguments:
  312. ValueName - the unicode name for the registry value located in the registry.
  313. Value - a pointer to the ULONG for the result.
  314. Return Value:
  315. NTSTATUS
  316. If STATUS_SUCCESSFUL is returned, the location *Value will be
  317. updated with the DWORD value from the registry. If any failing
  318. status is returned, this value is untouched.
  319. --*/
  320. {
  321. HANDLE Handle;
  322. NTSTATUS Status;
  323. ULONG RequestLength;
  324. ULONG ResultLength;
  325. UCHAR Buffer[KEY_WORK_AREA];
  326. UNICODE_STRING KeyName;
  327. OBJECT_ATTRIBUTES ObjectAttributes;
  328. PKEY_VALUE_FULL_INFORMATION KeyValueInformation;
  329. KeyName.Buffer = COMPATIBILITY_MODE_KEY_NAME;
  330. KeyName.Length = sizeof(COMPATIBILITY_MODE_KEY_NAME) - sizeof(WCHAR);
  331. KeyName.MaximumLength = sizeof(COMPATIBILITY_MODE_KEY_NAME);
  332. InitializeObjectAttributes(&ObjectAttributes,
  333. &KeyName,
  334. OBJ_CASE_INSENSITIVE,
  335. NULL,
  336. NULL);
  337. Status = ZwOpenKey(&Handle,
  338. KEY_READ,
  339. &ObjectAttributes);
  340. if (!NT_SUCCESS(Status)) {
  341. return Status;
  342. }
  343. RequestLength = KEY_WORK_AREA;
  344. KeyValueInformation = (PKEY_VALUE_FULL_INFORMATION)Buffer;
  345. while (1) {
  346. Status = ZwQueryValueKey(Handle,
  347. ValueName,
  348. KeyValueFullInformation,
  349. KeyValueInformation,
  350. RequestLength,
  351. &ResultLength);
  352. ASSERT( Status != STATUS_BUFFER_OVERFLOW );
  353. if (Status == STATUS_BUFFER_OVERFLOW) {
  354. //
  355. // Try to get a buffer big enough.
  356. //
  357. if (KeyValueInformation != (PKEY_VALUE_FULL_INFORMATION)Buffer) {
  358. ExFreePool(KeyValueInformation);
  359. }
  360. RequestLength += 256;
  361. KeyValueInformation = (PKEY_VALUE_FULL_INFORMATION)
  362. ExAllocatePoolWithTag(PagedPool,
  363. RequestLength,
  364. ' taF');
  365. if (!KeyValueInformation) {
  366. return STATUS_NO_MEMORY;
  367. }
  368. } else {
  369. break;
  370. }
  371. }
  372. ZwClose(Handle);
  373. if (NT_SUCCESS(Status)) {
  374. if (KeyValueInformation->DataLength != 0) {
  375. PULONG DataPtr;
  376. //
  377. // Return contents to the caller.
  378. //
  379. DataPtr = (PULONG)
  380. ((PUCHAR)KeyValueInformation + KeyValueInformation->DataOffset);
  381. *Value = *DataPtr;
  382. } else {
  383. //
  384. // Treat as if no value was found
  385. //
  386. Status = STATUS_OBJECT_NAME_NOT_FOUND;
  387. }
  388. }
  389. if (KeyValueInformation != (PKEY_VALUE_FULL_INFORMATION)Buffer) {
  390. ExFreePool(KeyValueInformation);
  391. }
  392. return Status;
  393. }
  394. //
  395. // Local Support routine
  396. //
  397. BOOLEAN
  398. FatIsFujitsuFMR (
  399. )
  400. /*++
  401. Routine Description:
  402. This routine tells if is we running on a FujitsuFMR machine.
  403. Arguments:
  404. Return Value:
  405. BOOLEAN - TRUE is we are and FALSE otherwise
  406. --*/
  407. {
  408. ULONG Value;
  409. BOOLEAN Result;
  410. HANDLE Handle;
  411. NTSTATUS Status;
  412. ULONG RequestLength;
  413. ULONG ResultLength;
  414. UCHAR Buffer[KEY_WORK_AREA];
  415. UNICODE_STRING KeyName;
  416. UNICODE_STRING ValueName;
  417. OBJECT_ATTRIBUTES ObjectAttributes;
  418. PKEY_VALUE_FULL_INFORMATION KeyValueInformation;
  419. //
  420. // Set default as PC/AT
  421. //
  422. KeyName.Buffer = REGISTRY_HARDWARE_DESCRIPTION_W;
  423. KeyName.Length = sizeof(REGISTRY_HARDWARE_DESCRIPTION_W) - sizeof(WCHAR);
  424. KeyName.MaximumLength = sizeof(REGISTRY_HARDWARE_DESCRIPTION_W);
  425. InitializeObjectAttributes(&ObjectAttributes,
  426. &KeyName,
  427. OBJ_CASE_INSENSITIVE,
  428. NULL,
  429. NULL);
  430. Status = ZwOpenKey(&Handle,
  431. KEY_READ,
  432. &ObjectAttributes);
  433. if (!NT_SUCCESS(Status)) {
  434. return FALSE;
  435. }
  436. ValueName.Buffer = REGISTRY_MACHINE_IDENTIFIER_W;
  437. ValueName.Length = sizeof(REGISTRY_MACHINE_IDENTIFIER_W) - sizeof(WCHAR);
  438. ValueName.MaximumLength = sizeof(REGISTRY_MACHINE_IDENTIFIER_W);
  439. RequestLength = KEY_WORK_AREA;
  440. KeyValueInformation = (PKEY_VALUE_FULL_INFORMATION)Buffer;
  441. while (1) {
  442. Status = ZwQueryValueKey(Handle,
  443. &ValueName,
  444. KeyValueFullInformation,
  445. KeyValueInformation,
  446. RequestLength,
  447. &ResultLength);
  448. // ASSERT( Status != STATUS_BUFFER_OVERFLOW );
  449. if (Status == STATUS_BUFFER_OVERFLOW) {
  450. //
  451. // Try to get a buffer big enough.
  452. //
  453. if (KeyValueInformation != (PKEY_VALUE_FULL_INFORMATION)Buffer) {
  454. ExFreePool(KeyValueInformation);
  455. }
  456. RequestLength += 256;
  457. KeyValueInformation = (PKEY_VALUE_FULL_INFORMATION)
  458. ExAllocatePool(PagedPool, RequestLength);
  459. if (!KeyValueInformation) {
  460. return FALSE;
  461. }
  462. } else {
  463. break;
  464. }
  465. }
  466. ZwClose(Handle);
  467. if (NT_SUCCESS(Status) &&
  468. (KeyValueInformation->DataLength >= sizeof(FUJITSU_FMR_NAME_W)) &&
  469. (RtlCompareMemory((PUCHAR)KeyValueInformation + KeyValueInformation->DataOffset,
  470. FUJITSU_FMR_NAME_W,
  471. sizeof(FUJITSU_FMR_NAME_W) - sizeof(WCHAR)) ==
  472. sizeof(FUJITSU_FMR_NAME_W) - sizeof(WCHAR))) {
  473. Result = TRUE;
  474. } else {
  475. Result = FALSE;
  476. }
  477. if (KeyValueInformation != (PKEY_VALUE_FULL_INFORMATION)Buffer) {
  478. ExFreePool(KeyValueInformation);
  479. }
  480. return Result;
  481. }