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.

850 lines
24 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. FspDisp.c
  5. Abstract:
  6. This module implements the main dispatch procedure/thread for the Ntfs
  7. Fsp
  8. Author:
  9. Gary Kimura [GaryKi] 21-May-1991
  10. Revision History:
  11. --*/
  12. #include "NtfsProc.h"
  13. #define BugCheckFileId (NTFS_BUG_CHECK_FSPDISP)
  14. #pragma alloc_text(PAGE, NtfsSpecialDispatch)
  15. #pragma alloc_text(PAGE, NtfsPostSpecial)
  16. //
  17. // Define our local debug trace level
  18. //
  19. #define Dbg (DEBUG_TRACE_FSP_DISPATCHER)
  20. extern PETHREAD NtfsDesignatedTimeoutThread;
  21. VOID
  22. NtfsFspDispatch (
  23. IN PVOID Context
  24. )
  25. /*++
  26. Routine Description:
  27. This is the main FSP thread routine that is executed to receive
  28. and dispatch IRP requests. Each FSP thread begins its execution here.
  29. There is one thread created at system initialization time and subsequent
  30. threads created as needed.
  31. Arguments:
  32. Context - Supplies the thread id.
  33. Return Value:
  34. None - This routine never exits
  35. --*/
  36. {
  37. TOP_LEVEL_CONTEXT TopLevelContext;
  38. PTOP_LEVEL_CONTEXT ThreadTopLevelContext;
  39. OPLOCK_CLEANUP OplockCleanup;
  40. PIRP Irp;
  41. PIRP_CONTEXT IrpContext;
  42. PIO_STACK_LOCATION IrpSp;
  43. ULONG LogFileFullCount = 0;
  44. PVOLUME_DEVICE_OBJECT VolDo;
  45. BOOLEAN Retry;
  46. NTSTATUS Status = STATUS_SUCCESS;
  47. IrpContext = (PIRP_CONTEXT)Context;
  48. Irp = IrpContext->OriginatingIrp;
  49. if (Irp != NULL) {
  50. IrpSp = IoGetCurrentIrpStackLocation( Irp );
  51. }
  52. //
  53. // Now because we are the Fsp we will force the IrpContext to
  54. // indicate true on Wait.
  55. //
  56. SetFlag( IrpContext->State, IRP_CONTEXT_STATE_WAIT );
  57. //
  58. // If this request has an associated volume device object, remember it.
  59. //
  60. if ((Irp != NULL) &&
  61. (IrpSp->FileObject != NULL)) {
  62. VolDo = CONTAINING_RECORD( IrpSp->DeviceObject,
  63. VOLUME_DEVICE_OBJECT,
  64. DeviceObject );
  65. } else {
  66. VolDo = NULL;
  67. }
  68. //
  69. // Now case on the function code. For each major function code,
  70. // either call the appropriate FSP routine or case on the minor
  71. // function and then call the FSP routine. The FSP routine that
  72. // we call is responsible for completing the IRP, and not us.
  73. // That way the routine can complete the IRP and then continue
  74. // post processing as required. For example, a read can be
  75. // satisfied right away and then read can be done.
  76. //
  77. // We'll do all of the work within an exception handler that
  78. // will be invoked if ever some underlying operation gets into
  79. // trouble (e.g., if NtfsReadSectorsSync has trouble).
  80. //
  81. while (TRUE) {
  82. FsRtlEnterFileSystem();
  83. ASSERT( IoGetTopLevelIrp() != (PIRP) &TopLevelContext );
  84. ThreadTopLevelContext = NtfsInitializeTopLevelIrp( &TopLevelContext, TRUE, TRUE );
  85. ASSERT( ThreadTopLevelContext == &TopLevelContext );
  86. NtfsPostRequests += 1;
  87. do {
  88. //
  89. // If this is the initial try with this Irp Context, update the
  90. // top level Irp fields.
  91. //
  92. NtfsUpdateIrpContextWithTopLevel( IrpContext, ThreadTopLevelContext );
  93. Retry = FALSE;
  94. try {
  95. //
  96. // Always clear the exception code in the IrpContext so we respond
  97. // correctly to errors encountered in the Fsp.
  98. //
  99. IrpContext->ExceptionStatus = 0;
  100. SetFlag( IrpContext->State, IRP_CONTEXT_STATE_IN_FSP );
  101. //
  102. // See if we were posted due to a log file full condition, and
  103. // if so, then do a clean volume checkpoint if we are the
  104. // first ones to get there. If we see a different Lsn and do
  105. // not do the checkpoint, the worst that can happen is that we
  106. // will get posted again if the log file is still full.
  107. //
  108. if (IrpContext->LastRestartArea.QuadPart != 0) {
  109. NtfsCheckpointForLogFileFull( IrpContext );
  110. if (++LogFileFullCount >= 2) {
  111. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_EXCESS_LOG_FULL );
  112. }
  113. }
  114. //
  115. // If we have an Irp then proceed with our normal processing.
  116. //
  117. if (Irp != NULL) {
  118. switch ( IrpContext->MajorFunction ) {
  119. //
  120. // For Create Operation,
  121. //
  122. case IRP_MJ_CREATE:
  123. ClearFlag( IrpContext->State, IRP_CONTEXT_STATE_EFS_CREATE );
  124. if (FlagOn( IrpContext->State, IRP_CONTEXT_STATE_DASD_OPEN )) {
  125. Status = NtfsCommonVolumeOpen( IrpContext, Irp );
  126. } else {
  127. RtlZeroMemory( &OplockCleanup, sizeof( OplockCleanup ) );
  128. Status = NtfsCommonCreate( IrpContext, Irp, &OplockCleanup, NULL );
  129. }
  130. break;
  131. //
  132. // For close operations
  133. //
  134. case IRP_MJ_CLOSE:
  135. //
  136. // We should never post closes to this workqueue.
  137. //
  138. NtfsBugCheck( 0, 0, 0 );
  139. break;
  140. //
  141. // For read operations
  142. //
  143. case IRP_MJ_READ:
  144. (VOID) NtfsCommonRead( IrpContext, Irp, TRUE );
  145. break;
  146. //
  147. // For write operations,
  148. //
  149. case IRP_MJ_WRITE:
  150. (VOID) NtfsCommonWrite( IrpContext, Irp );
  151. break;
  152. //
  153. // For Query Information operations,
  154. //
  155. case IRP_MJ_QUERY_INFORMATION:
  156. (VOID) NtfsCommonQueryInformation( IrpContext, Irp );
  157. break;
  158. //
  159. // For Set Information operations,
  160. //
  161. case IRP_MJ_SET_INFORMATION:
  162. (VOID) NtfsCommonSetInformation( IrpContext, Irp );
  163. break;
  164. //
  165. // For Query EA operations,
  166. //
  167. case IRP_MJ_QUERY_EA:
  168. (VOID) NtfsCommonQueryEa( IrpContext, Irp );
  169. break;
  170. //
  171. // For Set EA operations,
  172. //
  173. case IRP_MJ_SET_EA:
  174. (VOID) NtfsCommonSetEa( IrpContext, Irp );
  175. break;
  176. //
  177. // For Flush buffers operations,
  178. //
  179. case IRP_MJ_FLUSH_BUFFERS:
  180. (VOID) NtfsCommonFlushBuffers( IrpContext, Irp );
  181. break;
  182. //
  183. // For Query Volume Information operations,
  184. //
  185. case IRP_MJ_QUERY_VOLUME_INFORMATION:
  186. (VOID) NtfsCommonQueryVolumeInfo( IrpContext, Irp );
  187. break;
  188. //
  189. // For Set Volume Information operations,
  190. //
  191. case IRP_MJ_SET_VOLUME_INFORMATION:
  192. (VOID) NtfsCommonSetVolumeInfo( IrpContext, Irp );
  193. break;
  194. //
  195. // For File Cleanup operations,
  196. //
  197. case IRP_MJ_CLEANUP:
  198. (VOID) NtfsCommonCleanup( IrpContext, Irp );
  199. break;
  200. //
  201. // For Directory Control operations,
  202. //
  203. case IRP_MJ_DIRECTORY_CONTROL:
  204. (VOID) NtfsCommonDirectoryControl( IrpContext, Irp );
  205. break;
  206. //
  207. // For File System Control operations,
  208. //
  209. case IRP_MJ_FILE_SYSTEM_CONTROL:
  210. (VOID) NtfsCommonFileSystemControl( IrpContext, Irp );
  211. break;
  212. //
  213. // For Lock Control operations,
  214. //
  215. case IRP_MJ_LOCK_CONTROL:
  216. (VOID) NtfsCommonLockControl( IrpContext, Irp );
  217. break;
  218. //
  219. // For Device Control operations,
  220. //
  221. case IRP_MJ_DEVICE_CONTROL:
  222. (VOID) NtfsCommonDeviceControl( IrpContext, Irp );
  223. break;
  224. //
  225. // For Query Security Information operations,
  226. //
  227. case IRP_MJ_QUERY_SECURITY:
  228. (VOID) NtfsCommonQuerySecurityInfo( IrpContext, Irp );
  229. break;
  230. //
  231. // For Set Security Information operations,
  232. //
  233. case IRP_MJ_SET_SECURITY:
  234. (VOID) NtfsCommonSetSecurityInfo( IrpContext, Irp );
  235. break;
  236. //
  237. // For Query Quota operations,
  238. //
  239. case IRP_MJ_QUERY_QUOTA:
  240. (VOID) NtfsCommonQueryQuota( IrpContext, Irp );
  241. break;
  242. //
  243. // For Set Quota operations,
  244. //
  245. case IRP_MJ_SET_QUOTA:
  246. (VOID) NtfsCommonSetQuota( IrpContext, Irp );
  247. break;
  248. //
  249. // For any other major operations, return an invalid
  250. // request.
  251. //
  252. default:
  253. NtfsCompleteRequest( IrpContext, Irp, STATUS_INVALID_DEVICE_REQUEST );
  254. break;
  255. }
  256. //
  257. // Otherwise complete the request to clean up this Irp Context.
  258. //
  259. } else {
  260. NtfsCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  261. IrpContext = NULL;
  262. }
  263. ASSERT( IoGetTopLevelIrp() != (PIRP) &TopLevelContext );
  264. } except(NtfsExceptionFilter( IrpContext, GetExceptionInformation() )) {
  265. PIO_STACK_LOCATION IrpSp;
  266. //
  267. // We had some trouble trying to perform the requested
  268. // operation, so we'll abort the I/O request with
  269. // the error status that we get back from the
  270. // execption code
  271. //
  272. if (Irp != NULL) {
  273. IrpSp = IoGetCurrentIrpStackLocation( Irp );
  274. Status = GetExceptionCode();
  275. if ((Status == STATUS_FILE_DELETED) &&
  276. ((IrpContext->MajorFunction == IRP_MJ_READ) ||
  277. (IrpContext->MajorFunction == IRP_MJ_WRITE) ||
  278. ((IrpContext->MajorFunction == IRP_MJ_SET_INFORMATION) &&
  279. (IrpSp->Parameters.SetFile.FileInformationClass == FileEndOfFileInformation)))) {
  280. IrpContext->ExceptionStatus = Status = STATUS_SUCCESS;
  281. }
  282. }
  283. //
  284. // If we failed to upgrade the volume's version during mount, we may
  285. // not have put the right exception code into the irp context yet.
  286. //
  287. if ((IrpContext != NULL) &&
  288. (FlagOn( IrpContext->State, IRP_CONTEXT_STATE_VOL_UPGR_FAILED )) &&
  289. (IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL) &&
  290. (IrpContext->MinorFunction == IRP_MN_MOUNT_VOLUME)) {
  291. IrpContext->ExceptionStatus = Status;
  292. }
  293. //
  294. // This is the return status code that we want the Irp Completion routine to receive.
  295. //
  296. Status = NtfsProcessException( IrpContext, Irp, Status );
  297. if ((Status == STATUS_CANT_WAIT) || (Status == STATUS_LOG_FILE_FULL)) {
  298. Retry = TRUE;
  299. }
  300. }
  301. } while (Retry);
  302. FsRtlExitFileSystem();
  303. //
  304. // If there are any entries on this volume's overflow queue, service
  305. // them.
  306. //
  307. if (VolDo != NULL) {
  308. KIRQL SavedIrql;
  309. PLIST_ENTRY Entry = NULL;
  310. //
  311. // We have a volume device object so see if there is any work
  312. // left to do in its overflow queue.
  313. //
  314. KeAcquireSpinLock( &VolDo->OverflowQueueSpinLock, &SavedIrql );
  315. while (VolDo->OverflowQueueCount > 0) {
  316. //
  317. // There is overflow work to do in this volume so we'll
  318. // decrement the Overflow count, dequeue the IRP, and release
  319. // the Event
  320. //
  321. Entry = VolDo->OverflowQueue.Flink;
  322. IrpContext = CONTAINING_RECORD( Entry,
  323. IRP_CONTEXT,
  324. WorkQueueItem.List );
  325. Irp = IrpContext->OriginatingIrp;
  326. //
  327. // If the cancel routine thinks it owns the irp ignore it
  328. //
  329. if (NtfsSetCancelRoutine( Irp, NULL, 0, FALSE )) {
  330. VolDo->OverflowQueueCount -= 1;
  331. RemoveEntryList( (PLIST_ENTRY)Entry );
  332. break;
  333. } else {
  334. //
  335. // Release the spinlock to let the cancel routine gain it and finish
  336. // its action
  337. //
  338. KeReleaseSpinLock( &VolDo->OverflowQueueSpinLock, SavedIrql );
  339. KeAcquireSpinLock( &VolDo->OverflowQueueSpinLock, &SavedIrql );
  340. Entry = NULL;
  341. }
  342. } // endwhile
  343. KeReleaseSpinLock( &VolDo->OverflowQueueSpinLock, SavedIrql );
  344. //
  345. // There wasn't an entry, break out of the loop and return to
  346. // the Ex Worker thread.
  347. //
  348. if ( Entry == NULL ) {
  349. break;
  350. }
  351. if (VolDo->OverflowQueueCount < OVERFLOW_QUEUE_LIMIT) {
  352. KeSetEvent( &VolDo->OverflowQueueEvent, IO_NO_INCREMENT, FALSE );
  353. }
  354. //
  355. // set wait to TRUE, and loop.
  356. //
  357. LogFileFullCount = 0;
  358. SetFlag( IrpContext->State, IRP_CONTEXT_STATE_WAIT );
  359. continue;
  360. } else {
  361. break;
  362. }
  363. }
  364. //
  365. // Decrement the PostedRequestCount.
  366. //
  367. if (VolDo) {
  368. ExInterlockedAddUlong( &VolDo->PostedRequestCount,
  369. 0xffffffff,
  370. &VolDo->OverflowQueueSpinLock );
  371. }
  372. return;
  373. }
  374. VOID
  375. NtfsPostSpecial (
  376. IN PIRP_CONTEXT IrpContext,
  377. IN PVCB Vcb,
  378. IN POST_SPECIAL_CALLOUT PostSpecialCallout,
  379. IN PVOID Context
  380. )
  381. /*++
  382. Routine Description:
  383. This routine posts a special request to a worker thread. The function
  384. to be called is passed in. The Vcb is referenced to ensure it is not
  385. deleted while the posted request is excuting.
  386. Arguments:
  387. Vcb - Volume control block for volume to post to.
  388. PostSpecialCallout - Function to be called from the worker thread.
  389. Context - Context point to pass to the function.
  390. Return Value:
  391. None
  392. --*/
  393. {
  394. PIRP_CONTEXT NewIrpContext = NULL;
  395. UNREFERENCED_PARAMETER( IrpContext );
  396. PAGED_CODE();
  397. //
  398. // Create an IrpContext for use to post the request.
  399. //
  400. NtfsInitializeIrpContext( NULL, TRUE, &NewIrpContext );
  401. NewIrpContext->Vcb = Vcb;
  402. NewIrpContext->Union.PostSpecialCallout = PostSpecialCallout;
  403. NewIrpContext->OriginatingIrp = Context;
  404. //
  405. // Updating the CloseCount and SystemFileCloseCount allows the volume
  406. // to be locked or dismounted, but the Vcb will not be deleted. This
  407. // routine will only be called with non-zero close counts so it is ok
  408. // to increment theses counts.
  409. //
  410. ASSERT( Vcb->CloseCount > 0 );
  411. InterlockedIncrement( &Vcb->CloseCount );
  412. InterlockedIncrement( &Vcb->SystemFileCloseCount );
  413. ExInitializeWorkItem( &NewIrpContext->WorkQueueItem,
  414. NtfsSpecialDispatch,
  415. NewIrpContext );
  416. //
  417. // Determine if the scavenger is already running.
  418. //
  419. ExAcquireFastMutexUnsafe( &NtfsScavengerLock );
  420. if (NtfsScavengerRunning) {
  421. //
  422. // Add this item to the scavanger work list.
  423. //
  424. NewIrpContext->WorkQueueItem.List.Flink = NULL;
  425. if (NtfsScavengerWorkList == NULL) {
  426. NtfsScavengerWorkList = NewIrpContext;
  427. } else {
  428. PIRP_CONTEXT WorkIrpContext;
  429. WorkIrpContext = NtfsScavengerWorkList;
  430. while (WorkIrpContext->WorkQueueItem.List.Flink != NULL) {
  431. WorkIrpContext = (PIRP_CONTEXT)
  432. WorkIrpContext->WorkQueueItem.List.Flink;
  433. }
  434. WorkIrpContext->WorkQueueItem.List.Flink = (PLIST_ENTRY)
  435. NewIrpContext;
  436. }
  437. } else {
  438. //
  439. // Start a worker thread to do scavenger work.
  440. //
  441. ExQueueWorkItem( &NewIrpContext->WorkQueueItem, DelayedWorkQueue );
  442. NtfsScavengerRunning = TRUE;
  443. }
  444. ExReleaseFastMutexUnsafe( &NtfsScavengerLock);
  445. }
  446. VOID
  447. NtfsSpecialDispatch (
  448. PVOID Context
  449. )
  450. /*++
  451. Routine Description:
  452. This routine is called when a special operation needs to be posted.
  453. It is called indirectly by NtfsPostSpecial. It is assumes that the
  454. Vcb is protected from going away by incrementing the volemue close
  455. counts for a file. If this routine fails nothing is done except
  456. to clean up the Vcb. This routine also handles issues log file full
  457. and can't wait.
  458. The function to be called is stored in the PostSpecialCallout field
  459. of the Irp Context, and the context is stored int he OriginatingIrp.
  460. Both fields are zeroed before the the callout function is called.
  461. Arguments:
  462. Context - Supplies a pointer to an IrpContext.
  463. Return Value:
  464. --*/
  465. {
  466. PVCB Vcb;
  467. PIRP_CONTEXT IrpContext = Context;
  468. TOP_LEVEL_CONTEXT TopLevelContext;
  469. PTOP_LEVEL_CONTEXT ThreadTopLevelContext;
  470. POST_SPECIAL_CALLOUT PostSpecialCallout;
  471. PVOID SpecialContext;
  472. ULONG LogFileFullCount;
  473. BOOLEAN Retry;
  474. PAGED_CODE();
  475. FsRtlEnterFileSystem();
  476. do {
  477. Vcb = IrpContext->Vcb;
  478. LogFileFullCount = 0;
  479. //
  480. // Capture the funciton pointer and context before using the IrpContext.
  481. //
  482. PostSpecialCallout = IrpContext->Union.PostSpecialCallout;
  483. SpecialContext = IrpContext->OriginatingIrp;
  484. IrpContext->Union.PostSpecialCallout = NULL;
  485. IrpContext->OriginatingIrp = NULL;
  486. ThreadTopLevelContext = NtfsInitializeTopLevelIrp( &TopLevelContext, TRUE, TRUE );
  487. ASSERT( ThreadTopLevelContext == &TopLevelContext );
  488. ASSERT( !FlagOn( IrpContext->State, IRP_CONTEXT_STATE_OWNS_TOP_LEVEL ));
  489. ASSERT( FlagOn( IrpContext->State, IRP_CONTEXT_STATE_ALLOC_FROM_POOL ));
  490. //
  491. // Initialize the thread top level structure, if needed.
  492. //
  493. ASSERT( IoGetTopLevelIrp() != (PIRP) &TopLevelContext );
  494. NtfsUpdateIrpContextWithTopLevel( IrpContext, ThreadTopLevelContext );
  495. //
  496. // Don't let this IrpContext be deleted.
  497. //
  498. SetFlag( IrpContext->State, IRP_CONTEXT_STATE_PERSISTENT );
  499. do {
  500. Retry = FALSE;
  501. try {
  502. //
  503. // See if we failed due to a log file full condition, and
  504. // if so, then do a clean volume checkpoint if we are the
  505. // first ones to get there. If we see a different Lsn and do
  506. // not do the checkpoint, the worst that can happen is that we
  507. // will fail again if the log file is still full.
  508. //
  509. if (IrpContext->LastRestartArea.QuadPart != 0) {
  510. NtfsCheckpointForLogFileFull( IrpContext );
  511. if (++LogFileFullCount >= 2) {
  512. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_EXCESS_LOG_FULL );
  513. }
  514. }
  515. //
  516. // Call the requested function.
  517. //
  518. ASSERT( FlagOn( IrpContext->TopLevelIrpContext->State, IRP_CONTEXT_STATE_OWNS_TOP_LEVEL ));
  519. PostSpecialCallout( IrpContext, SpecialContext );
  520. NtfsCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  521. } except(NtfsExceptionFilter( IrpContext, GetExceptionInformation() )) {
  522. NTSTATUS ExceptionCode;
  523. ExceptionCode = GetExceptionCode();
  524. ExceptionCode = NtfsProcessException( IrpContext, NULL, ExceptionCode );
  525. if ((ExceptionCode == STATUS_CANT_WAIT) ||
  526. (ExceptionCode == STATUS_LOG_FILE_FULL)) {
  527. Retry = TRUE;
  528. }
  529. }
  530. } while (Retry);
  531. //
  532. // Ok to let this IrpContext be deleted.
  533. //
  534. ClearFlag( IrpContext->State, IRP_CONTEXT_STATE_PERSISTENT );
  535. //
  536. // At this point regardless of the status the volume needs to
  537. // be cleaned up and the IrpContext freed.
  538. // Dereference the Vcb and check to see if it needs to be deleted.
  539. // since this call might raise wrap it with a try/execpt.
  540. //
  541. try {
  542. //
  543. // Acquire the volume exclusive so the counts can be
  544. // updated.
  545. //
  546. ASSERT( FlagOn( IrpContext->State, IRP_CONTEXT_STATE_WAIT ));
  547. NtfsAcquireExclusiveVcb( IrpContext, Vcb, TRUE );
  548. InterlockedDecrement( &Vcb->SystemFileCloseCount );
  549. InterlockedDecrement( &Vcb->CloseCount );
  550. NtfsReleaseVcb( IrpContext, Vcb );
  551. } except( EXCEPTION_EXECUTE_HANDLER ) {
  552. ASSERT( FsRtlIsNtstatusExpected( GetExceptionCode() ) );
  553. }
  554. //
  555. // Free the irp context.
  556. //
  557. NtfsCleanupIrpContext( IrpContext, TRUE );
  558. //
  559. // See if there is more work on the scavenger list.
  560. //
  561. ExAcquireFastMutexUnsafe( &NtfsScavengerLock );
  562. ASSERT( NtfsScavengerRunning );
  563. IrpContext = NtfsScavengerWorkList;
  564. if (IrpContext != NULL) {
  565. //
  566. // Remove the entry from the list.
  567. //
  568. NtfsScavengerWorkList = (PIRP_CONTEXT) IrpContext->WorkQueueItem.List.Flink;
  569. IrpContext->WorkQueueItem.List.Flink = NULL;
  570. } else {
  571. NtfsScavengerRunning = FALSE;
  572. }
  573. ExReleaseFastMutexUnsafe( &NtfsScavengerLock );
  574. } while ( IrpContext != NULL );
  575. FsRtlExitFileSystem();
  576. }