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.

954 lines
26 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. lazyrite.c
  5. Abstract:
  6. This module implements the lazy writer for the Cache subsystem.
  7. Author:
  8. Tom Miller [TomM] 22-July-1990
  9. Revision History:
  10. --*/
  11. #include "cc.h"
  12. //
  13. // The Bug check file id for this module
  14. //
  15. #define BugCheckFileId (CACHE_BUG_CHECK_LAZYRITE)
  16. //
  17. // Define our debug constant
  18. //
  19. #define me 0x00000020
  20. //
  21. // Local support routines
  22. //
  23. PWORK_QUEUE_ENTRY
  24. CcReadWorkQueue (
  25. );
  26. VOID
  27. CcLazyWriteScan (
  28. );
  29. VOID
  30. CcScheduleLazyWriteScan (
  31. IN BOOLEAN FastScan
  32. )
  33. /*++
  34. Routine Description:
  35. This routine may be called to schedule the next lazy writer scan,
  36. during which lazy write and lazy close activity is posted to other
  37. worker threads. Callers should acquire the lazy writer spin lock
  38. to see if the scan is currently active, and then call this routine
  39. still holding the spin lock if not. One special call is used at
  40. the end of the lazy write scan to propagate lazy write active once
  41. we go active. This call is "the" scan thread, and it can therefore
  42. safely schedule the next scan without taking out the spin lock.
  43. Arguments:
  44. FastScan - if set, make the scan happen immediately
  45. Return Value:
  46. None.
  47. --*/
  48. {
  49. //
  50. // It is important to set the active flag TRUE first for the propagate
  51. // case, because it is conceivable that once the timer is set, another
  52. // thread could actually run and make the scan go idle before we then
  53. // jam the flag TRUE.
  54. //
  55. // When going from idle to active, we delay a little longer to let the
  56. // app finish saving its file.
  57. //
  58. if (FastScan) {
  59. LazyWriter.ScanActive = TRUE;
  60. KeSetTimer( &LazyWriter.ScanTimer, CcNoDelay, &LazyWriter.ScanDpc );
  61. } else if (LazyWriter.ScanActive) {
  62. KeSetTimer( &LazyWriter.ScanTimer, CcIdleDelay, &LazyWriter.ScanDpc );
  63. } else {
  64. LazyWriter.ScanActive = TRUE;
  65. KeSetTimer( &LazyWriter.ScanTimer, CcFirstDelay, &LazyWriter.ScanDpc );
  66. }
  67. }
  68. VOID
  69. CcScanDpc (
  70. IN PKDPC Dpc,
  71. IN PVOID DeferredContext,
  72. IN PVOID SystemArgument1,
  73. IN PVOID SystemArgument2
  74. )
  75. /*++
  76. Routine Description:
  77. This is the Dpc routine which runs when the scan timer goes off. It
  78. simply posts an element for an Ex Worker thread to do the scan.
  79. Arguments:
  80. (All are ignored)
  81. Return Value:
  82. None.
  83. --*/
  84. {
  85. PWORK_QUEUE_ENTRY WorkQueueEntry;
  86. UNREFERENCED_PARAMETER(Dpc);
  87. UNREFERENCED_PARAMETER(DeferredContext);
  88. UNREFERENCED_PARAMETER(SystemArgument1);
  89. UNREFERENCED_PARAMETER(SystemArgument2);
  90. WorkQueueEntry = CcAllocateWorkQueueEntry();
  91. //
  92. // If we failed to allocate a WorkQueueEntry, things must
  93. // be in pretty bad shape. However, all we have to do is
  94. // say we are not active, and wait for another event to
  95. // wake things up again.
  96. //
  97. if (WorkQueueEntry == NULL) {
  98. LazyWriter.ScanActive = FALSE;
  99. } else {
  100. //
  101. // Otherwise post a work queue entry to do the scan.
  102. //
  103. WorkQueueEntry->Function = (UCHAR)LazyWriteScan;
  104. CcPostWorkQueue( WorkQueueEntry, &CcRegularWorkQueue );
  105. }
  106. }
  107. NTSTATUS
  108. CcWaitForCurrentLazyWriterActivity (
  109. )
  110. /*++
  111. Routine Description:
  112. This routine allows a thread to receive notification when the current tick
  113. of lazy writer work has completed. It must not be called within a lazy
  114. writer workitem! The caller must not be holding synchronization that could
  115. block a Cc workitem!
  116. In particular, this lets a caller insure that all available lazy closes at
  117. the time of the call have completed.
  118. Arguments:
  119. None.
  120. Return Value:
  121. Final result of the wait.
  122. --*/
  123. {
  124. KIRQL OldIrql;
  125. KEVENT Event;
  126. PWORK_QUEUE_ENTRY WorkQueueEntry;
  127. WorkQueueEntry = CcAllocateWorkQueueEntry();
  128. if (WorkQueueEntry == NULL) {
  129. return STATUS_INSUFFICIENT_RESOURCES;
  130. }
  131. WorkQueueEntry->Function = (UCHAR)EventSet;
  132. KeInitializeEvent( &Event, NotificationEvent, FALSE );
  133. WorkQueueEntry->Parameters.Event.Event = &Event;
  134. //
  135. // Add this to the post-tick work queue and wake the lazy writer for it.
  136. // The lazy writer will add this to the end of the next batch of work
  137. // he issues.
  138. //
  139. CcAcquireMasterLock( &OldIrql );
  140. InsertTailList( &CcPostTickWorkQueue, &WorkQueueEntry->WorkQueueLinks );
  141. LazyWriter.OtherWork = TRUE;
  142. if (!LazyWriter.ScanActive) {
  143. CcScheduleLazyWriteScan( TRUE );
  144. }
  145. CcReleaseMasterLock( OldIrql );
  146. return KeWaitForSingleObject( &Event, Executive, KernelMode, FALSE, NULL );
  147. }
  148. VOID
  149. CcLazyWriteScan (
  150. )
  151. /*++
  152. Routine Description:
  153. This routine implements the Lazy Writer scan for dirty data to flush
  154. or any other work to do (lazy close). This routine is scheduled by
  155. calling CcScheduleLazyWriteScan.
  156. Arguments:
  157. None.
  158. Return Value:
  159. None.
  160. --*/
  161. {
  162. ULONG PagesToWrite, ForegroundRate, EstimatedDirtyNextInterval;
  163. PSHARED_CACHE_MAP SharedCacheMap, FirstVisited;
  164. KIRQL OldIrql;
  165. ULONG LoopsWithLockHeld = 0;
  166. BOOLEAN AlreadyMoved = FALSE;
  167. LIST_ENTRY PostTickWorkQueue;
  168. //
  169. // Top of Lazy Writer scan.
  170. //
  171. try {
  172. //
  173. // If there is no work to do, then we will go inactive, and return.
  174. //
  175. CcAcquireMasterLock( &OldIrql );
  176. if ((CcTotalDirtyPages == 0) && !LazyWriter.OtherWork) {
  177. //
  178. // Sleep if there are no deferred writes. It is important to check
  179. // proactively because writes may be blocked for reasons external
  180. // to the cache manager. The lazy writer must keep poking since it
  181. // may have no bytes to write itself.
  182. //
  183. if (IsListEmpty(&CcDeferredWrites)) {
  184. LazyWriter.ScanActive = FALSE;
  185. CcReleaseMasterLock( OldIrql );
  186. } else {
  187. CcReleaseMasterLock( OldIrql );
  188. //
  189. // Check for writes and schedule the next scan.
  190. //
  191. CcPostDeferredWrites();
  192. CcScheduleLazyWriteScan( FALSE );
  193. }
  194. return;
  195. }
  196. //
  197. // Pull out the post tick workitems for this pass. It is important that
  198. // we are doing this at the top since more could be queued as we rummage
  199. // for work to do. Post tick workitems are guaranteed to occur after all
  200. // work generated in a complete scan.
  201. //
  202. InitializeListHead( &PostTickWorkQueue );
  203. while (!IsListEmpty( &CcPostTickWorkQueue )) {
  204. PLIST_ENTRY Entry = RemoveHeadList( &CcPostTickWorkQueue );
  205. InsertTailList( &PostTickWorkQueue, Entry );
  206. }
  207. //
  208. // Calculate the next sweep time stamp, then update all relevant fields for
  209. // the next time around. Also we can clear the OtherWork flag.
  210. //
  211. LazyWriter.OtherWork = FALSE;
  212. //
  213. // Assume we will write our usual fraction of dirty pages. Do not do the
  214. // divide if there is not enough dirty pages, or else we will never write
  215. // the last few pages.
  216. //
  217. PagesToWrite = CcTotalDirtyPages;
  218. if (PagesToWrite > LAZY_WRITER_MAX_AGE_TARGET) {
  219. PagesToWrite /= LAZY_WRITER_MAX_AGE_TARGET;
  220. }
  221. //
  222. // Estimate the rate of dirty pages being produced in the foreground.
  223. // This is the total number of dirty pages now plus the number of dirty
  224. // pages we scheduled to write last time, minus the number of dirty
  225. // pages we have now. Throw out any cases which would not produce a
  226. // positive rate.
  227. //
  228. ForegroundRate = 0;
  229. if ((CcTotalDirtyPages + CcPagesWrittenLastTime) > CcDirtyPagesLastScan) {
  230. ForegroundRate = (CcTotalDirtyPages + CcPagesWrittenLastTime) -
  231. CcDirtyPagesLastScan;
  232. }
  233. //
  234. // If we estimate that we will exceed our dirty page target by the end
  235. // of this interval, then we must write more. Try to arrive on target.
  236. //
  237. EstimatedDirtyNextInterval = CcTotalDirtyPages - PagesToWrite + ForegroundRate;
  238. if (EstimatedDirtyNextInterval > CcDirtyPageTarget) {
  239. PagesToWrite += EstimatedDirtyNextInterval - CcDirtyPageTarget;
  240. }
  241. //
  242. // Now save away the number of dirty pages and the number of pages we
  243. // just calculated to write.
  244. //
  245. CcDirtyPagesLastScan = CcTotalDirtyPages;
  246. CcPagesYetToWrite = CcPagesWrittenLastTime = PagesToWrite;
  247. //
  248. // Loop to flush enough Shared Cache Maps to write the number of pages
  249. // we just calculated.
  250. //
  251. SharedCacheMap = CONTAINING_RECORD( CcLazyWriterCursor.SharedCacheMapLinks.Flink,
  252. SHARED_CACHE_MAP,
  253. SharedCacheMapLinks );
  254. DebugTrace( 0, me, "Start of Lazy Writer Scan\n", 0 );
  255. //
  256. // Normally we would just like to visit every Cache Map once on each scan,
  257. // so the scan will terminate normally when we return to FirstVisited. But
  258. // in the off chance that FirstVisited gets deleted, we are guaranteed to stop
  259. // when we get back to our own listhead.
  260. //
  261. FirstVisited = NULL;
  262. while ((SharedCacheMap != FirstVisited) &&
  263. (&SharedCacheMap->SharedCacheMapLinks != &CcLazyWriterCursor.SharedCacheMapLinks)) {
  264. if (FirstVisited == NULL) {
  265. FirstVisited = SharedCacheMap;
  266. }
  267. //
  268. // Skip the SharedCacheMap if a write behind request is
  269. // already queued, write behind has been disabled, or
  270. // if there is no work to do (either dirty data to be written
  271. // or a delete is required).
  272. //
  273. // Note that for streams where modified writing is disabled, we
  274. // need to take out Bcbs exclusive, which serializes with foreground
  275. // activity. Therefore we use a special counter in the SharedCacheMap
  276. // to only service these once every n intervals.
  277. //
  278. // Skip temporary files unless we currently could not write as many
  279. // bytes as we might charge some hapless thread for throttling, unless
  280. // it has been closed. We assume that the "tick" of the lazy writer,
  281. // delayed temporarily by the passcount check, will permit the common
  282. // open/write/close/delete action on temporary files to sneak in and
  283. // truncate the file before we really write the data, if the file was
  284. // not opened delete-on-close to begin with.
  285. //
  286. // Since we will write closed files with dirty pages as part of the
  287. // regular pass (even temporary ones), only do lazy close on files
  288. // with no dirty pages.
  289. //
  290. if (!FlagOn(SharedCacheMap->Flags, WRITE_QUEUED | IS_CURSOR)
  291. &&
  292. (((PagesToWrite != 0) && (SharedCacheMap->DirtyPages != 0) &&
  293. (((++SharedCacheMap->LazyWritePassCount & 0xF) == 0) ||
  294. !FlagOn(SharedCacheMap->Flags, MODIFIED_WRITE_DISABLED) ||
  295. (CcCapturedSystemSize == MmSmallSystem) ||
  296. (SharedCacheMap->DirtyPages >= (4 * (MAX_WRITE_BEHIND / PAGE_SIZE)))) &&
  297. (!FlagOn(SharedCacheMap->FileObject->Flags, FO_TEMPORARY_FILE) ||
  298. (SharedCacheMap->OpenCount == 0) ||
  299. !CcCanIWrite(SharedCacheMap->FileObject, WRITE_CHARGE_THRESHOLD, FALSE, MAXUCHAR)))
  300. ||
  301. ((SharedCacheMap->OpenCount == 0) &&
  302. (SharedCacheMap->DirtyPages == 0) ||
  303. (SharedCacheMap->FileSize.QuadPart == 0)))) {
  304. PWORK_QUEUE_ENTRY WorkQueueEntry;
  305. //
  306. // If this is a metadata stream with at least 4 times
  307. // the maximum write behind I/O size, then let's tell
  308. // this guy to write 1/8 of his dirty data on this pass
  309. // so it doesn't build up.
  310. //
  311. // Else assume we can write everything (PagesToWrite only affects
  312. // metadata streams - otherwise writing is controlled by the Mbcb -
  313. // this throttle is engaged in CcWriteBehind).
  314. //
  315. SharedCacheMap->PagesToWrite = SharedCacheMap->DirtyPages;
  316. if (FlagOn(SharedCacheMap->Flags, MODIFIED_WRITE_DISABLED) &&
  317. (SharedCacheMap->PagesToWrite >= (4 * (MAX_WRITE_BEHIND / PAGE_SIZE))) &&
  318. (CcCapturedSystemSize != MmSmallSystem)) {
  319. SharedCacheMap->PagesToWrite /= 8;
  320. }
  321. //
  322. // If still searching for pages to write, adjust our targets.
  323. //
  324. if (!AlreadyMoved) {
  325. //
  326. // See if he exhausts the number of pages to write. (We
  327. // keep going in case there are any closes to do.)
  328. //
  329. if (SharedCacheMap->PagesToWrite >= PagesToWrite) {
  330. //
  331. // Here is where we should move the cursor to. Figure
  332. // out if we should resume on this stream or the next one.
  333. //
  334. RemoveEntryList( &CcLazyWriterCursor.SharedCacheMapLinks );
  335. //
  336. // For Metadata streams, set up to resume on the next stream on the
  337. // next scan. Also force a push forward every n intervals if all of
  338. // the pages came from this stream, so we don't get preoccupied with
  339. // one stream at the expense of others (which may be waiting for a
  340. // lazy close). Normally we would like to avoid seek overhead and
  341. // take the common case of a large sequential series of writes.
  342. //
  343. // This is similar to hotspot detection.
  344. //
  345. if (FlagOn(SharedCacheMap->Flags, MODIFIED_WRITE_DISABLED) ||
  346. ((FirstVisited == SharedCacheMap) &&
  347. ((SharedCacheMap->LazyWritePassCount & 0xF) == 0))) {
  348. InsertHeadList( &SharedCacheMap->SharedCacheMapLinks, &CcLazyWriterCursor.SharedCacheMapLinks );
  349. //
  350. // For other streams, set up to resume on the same stream on the
  351. // next scan.
  352. //
  353. } else {
  354. InsertTailList( &SharedCacheMap->SharedCacheMapLinks, &CcLazyWriterCursor.SharedCacheMapLinks );
  355. }
  356. PagesToWrite = 0;
  357. AlreadyMoved = TRUE;
  358. } else {
  359. PagesToWrite -= SharedCacheMap->PagesToWrite;
  360. }
  361. }
  362. //
  363. // Otherwise show we are actively writing, and keep it in the dirty
  364. // list.
  365. //
  366. SetFlag(SharedCacheMap->Flags, WRITE_QUEUED);
  367. SharedCacheMap->DirtyPages += 1;
  368. CcReleaseMasterLock( OldIrql );
  369. //
  370. // Queue the request to do the work to a worker thread.
  371. //
  372. WorkQueueEntry = CcAllocateWorkQueueEntry();
  373. //
  374. // If we failed to allocate a WorkQueueEntry, things must
  375. // be in pretty bad shape. However, all we have to do is
  376. // break out of our current loop, and try to go back and
  377. // delay a while. Even if the current guy should have gone
  378. // away when we clear WRITE_QUEUED, we will find him again
  379. // in the LW scan.
  380. //
  381. if (WorkQueueEntry == NULL) {
  382. CcAcquireMasterLock( &OldIrql );
  383. ClearFlag(SharedCacheMap->Flags, WRITE_QUEUED);
  384. SharedCacheMap->DirtyPages -= 1;
  385. break;
  386. }
  387. WorkQueueEntry->Function = (UCHAR)WriteBehind;
  388. WorkQueueEntry->Parameters.Write.SharedCacheMap = SharedCacheMap;
  389. //
  390. // Post it to the regular work queue.
  391. //
  392. CcAcquireMasterLock( &OldIrql );
  393. SharedCacheMap->DirtyPages -= 1;
  394. CcPostWorkQueue( WorkQueueEntry, &CcRegularWorkQueue );
  395. LoopsWithLockHeld = 0;
  396. //
  397. // Make sure we occasionally drop the lock. Set WRITE_QUEUED
  398. // to keep the guy from going away.
  399. //
  400. } else if ((++LoopsWithLockHeld >= 20) &&
  401. !FlagOn(SharedCacheMap->Flags, WRITE_QUEUED | IS_CURSOR)) {
  402. SetFlag(SharedCacheMap->Flags, WRITE_QUEUED);
  403. SharedCacheMap->DirtyPages += 1;
  404. CcReleaseMasterLock( OldIrql );
  405. LoopsWithLockHeld = 0;
  406. CcAcquireMasterLock( &OldIrql );
  407. ClearFlag(SharedCacheMap->Flags, WRITE_QUEUED);
  408. SharedCacheMap->DirtyPages -= 1;
  409. }
  410. //
  411. // Now loop back.
  412. //
  413. SharedCacheMap =
  414. CONTAINING_RECORD( SharedCacheMap->SharedCacheMapLinks.Flink,
  415. SHARED_CACHE_MAP,
  416. SharedCacheMapLinks );
  417. }
  418. DebugTrace( 0, me, "End of Lazy Writer Scan\n", 0 );
  419. //
  420. // Queue up our post tick workitems for this pass.
  421. //
  422. while (!IsListEmpty( &PostTickWorkQueue )) {
  423. PLIST_ENTRY Entry = RemoveHeadList( &PostTickWorkQueue );
  424. CcPostWorkQueue( CONTAINING_RECORD( Entry, WORK_QUEUE_ENTRY, WorkQueueLinks ),
  425. &CcRegularWorkQueue );
  426. }
  427. //
  428. // Now we can release the global list and loop back, per chance to sleep.
  429. //
  430. CcReleaseMasterLock( OldIrql );
  431. //
  432. // Once again we need to give the deferred writes a poke. We can have all dirty
  433. // pages on disable_write_behind files but also have an external condition that
  434. // caused the cached IO to be deferred. If so, this serves as our only chance to
  435. // issue it when the condition clears.
  436. //
  437. // Case hit on ForrestF's 5gb Alpha, 1/12/99.
  438. //
  439. if (!IsListEmpty(&CcDeferredWrites)) {
  440. CcPostDeferredWrites();
  441. }
  442. //
  443. // Now go ahead and schedule the next scan.
  444. //
  445. CcScheduleLazyWriteScan( FALSE );
  446. //
  447. // Basically, the Lazy Writer thread should never get an exception,
  448. // so we put a try-except around it that bug checks one way or the other.
  449. // Better we bug check here than worry about what happens if we let one
  450. // get by.
  451. //
  452. } except( CcExceptionFilter( GetExceptionCode() )) {
  453. CcBugCheck( GetExceptionCode(), 0, 0 );
  454. }
  455. }
  456. //
  457. // Internal support routine
  458. //
  459. LONG
  460. CcExceptionFilter (
  461. IN NTSTATUS ExceptionCode
  462. )
  463. /*++
  464. Routine Description:
  465. This is the standard exception filter for worker threads which simply
  466. calls an FsRtl routine to see if an expected status is being raised.
  467. If so, the exception is handled, else we bug check.
  468. Arguments:
  469. ExceptionCode - the exception code which was raised.
  470. Return Value:
  471. EXCEPTION_EXECUTE_HANDLER if expected, else a Bug Check occurs.
  472. --*/
  473. {
  474. DebugTrace(0, 0, "CcExceptionFilter %08lx\n", ExceptionCode);
  475. if (FsRtlIsNtstatusExpected( ExceptionCode )) {
  476. return EXCEPTION_EXECUTE_HANDLER;
  477. } else {
  478. return EXCEPTION_CONTINUE_SEARCH;
  479. }
  480. }
  481. //
  482. // Internal support routine
  483. //
  484. VOID
  485. FASTCALL
  486. CcPostWorkQueue (
  487. IN PWORK_QUEUE_ENTRY WorkQueueEntry,
  488. IN PLIST_ENTRY WorkQueue
  489. )
  490. /*++
  491. Routine Description:
  492. This routine queues a WorkQueueEntry, which has been allocated and
  493. initialized by the caller, to the WorkQueue for FIFO processing by
  494. the work threads.
  495. Arguments:
  496. WorkQueueEntry - supplies a pointer to the entry to queue
  497. Return Value:
  498. None
  499. --*/
  500. {
  501. KIRQL OldIrql;
  502. PLIST_ENTRY WorkerThreadEntry = NULL;
  503. ASSERT(FIELD_OFFSET(WORK_QUEUE_ITEM, List) == 0);
  504. DebugTrace(+1, me, "CcPostWorkQueue:\n", 0 );
  505. DebugTrace( 0, me, " WorkQueueEntry = %08lx\n", WorkQueueEntry );
  506. //
  507. // Queue the entry to the respective work queue.
  508. //
  509. CcAcquireWorkQueueLock( &OldIrql );
  510. InsertTailList( WorkQueue, &WorkQueueEntry->WorkQueueLinks );
  511. //
  512. // Now, if we aren't throttled and have any more idle threads we can
  513. // use, activate one.
  514. //
  515. if (!CcQueueThrottle && !IsListEmpty(&CcIdleWorkerThreadList)) {
  516. WorkerThreadEntry = RemoveHeadList( &CcIdleWorkerThreadList );
  517. CcNumberActiveWorkerThreads += 1;
  518. }
  519. CcReleaseWorkQueueLock( OldIrql );
  520. if (WorkerThreadEntry != NULL) {
  521. //
  522. // I had to peak in the sources to verify that this routine
  523. // is a noop if the Flink is not NULL. Sheeeeit!
  524. //
  525. ((PWORK_QUEUE_ITEM)WorkerThreadEntry)->List.Flink = NULL;
  526. ExQueueWorkItem( (PWORK_QUEUE_ITEM)WorkerThreadEntry, CriticalWorkQueue );
  527. }
  528. //
  529. // And return to our caller
  530. //
  531. DebugTrace(-1, me, "CcPostWorkQueue -> VOID\n", 0 );
  532. return;
  533. }
  534. //
  535. // Internal support routine
  536. //
  537. VOID
  538. CcWorkerThread (
  539. PVOID ExWorkQueueItem
  540. )
  541. /*++
  542. Routine Description:
  543. This is worker thread routine for processing cache manager work queue
  544. entries.
  545. Arguments:
  546. ExWorkQueueItem - The work item used for this thread
  547. Return Value:
  548. None
  549. --*/
  550. {
  551. KIRQL OldIrql;
  552. PLIST_ENTRY WorkQueue;
  553. PWORK_QUEUE_ENTRY WorkQueueEntry;
  554. BOOLEAN RescanOk = FALSE;
  555. BOOLEAN DropThrottle = FALSE;
  556. IO_STATUS_BLOCK IoStatus;
  557. IoStatus.Status = STATUS_SUCCESS;
  558. IoStatus.Information = 0;
  559. ASSERT(FIELD_OFFSET(WORK_QUEUE_ENTRY, WorkQueueLinks) == 0);
  560. while (TRUE) {
  561. CcAcquireWorkQueueLock( &OldIrql );
  562. //
  563. // If we just processed a throttled operation, drop the flag.
  564. //
  565. if (DropThrottle) {
  566. DropThrottle = CcQueueThrottle = FALSE;
  567. }
  568. //
  569. // On requeue, push at end of the source queue and clear hint.
  570. //
  571. if (IoStatus.Information == CC_REQUEUE) {
  572. InsertTailList( WorkQueue, &WorkQueueEntry->WorkQueueLinks );
  573. IoStatus.Information = 0;
  574. }
  575. //
  576. // First see if there is something in the express queue.
  577. //
  578. if (!IsListEmpty(&CcExpressWorkQueue)) {
  579. WorkQueue = &CcExpressWorkQueue;
  580. //
  581. // If there was nothing there, then try the regular queue.
  582. //
  583. } else if (!IsListEmpty(&CcRegularWorkQueue)) {
  584. WorkQueue = &CcRegularWorkQueue;
  585. //
  586. // Else we can break and go idle.
  587. //
  588. } else {
  589. break;
  590. }
  591. WorkQueueEntry = CONTAINING_RECORD( WorkQueue->Flink, WORK_QUEUE_ENTRY, WorkQueueLinks );
  592. //
  593. // If this is an EventSet, throttle down to a single thread to be sure
  594. // that this event fires after all preceeding workitems have completed.
  595. //
  596. if (WorkQueueEntry->Function == EventSet && CcNumberActiveWorkerThreads > 1) {
  597. CcQueueThrottle = TRUE;
  598. break;
  599. }
  600. //
  601. // Pop the workitem off: we will execute it now.
  602. //
  603. RemoveHeadList( WorkQueue );
  604. CcReleaseWorkQueueLock( OldIrql );
  605. //
  606. // Process the entry within a try-except clause, so that any errors
  607. // will cause us to continue after the called routine has unwound.
  608. //
  609. try {
  610. switch (WorkQueueEntry->Function) {
  611. //
  612. // Perform read ahead
  613. //
  614. case ReadAhead:
  615. DebugTrace( 0, me, "CcWorkerThread Read Ahead FileObject = %08lx\n",
  616. WorkQueueEntry->Parameters.Read.FileObject );
  617. CcPerformReadAhead( WorkQueueEntry->Parameters.Read.FileObject );
  618. break;
  619. //
  620. // Perform write behind
  621. //
  622. case WriteBehind:
  623. DebugTrace( 0, me, "CcWorkerThread WriteBehind SharedCacheMap = %08lx\n",
  624. WorkQueueEntry->Parameters.Write.SharedCacheMap );
  625. CcWriteBehind( WorkQueueEntry->Parameters.Write.SharedCacheMap, &IoStatus );
  626. RescanOk = (BOOLEAN)NT_SUCCESS(IoStatus.Status);
  627. break;
  628. //
  629. // Perform set event
  630. //
  631. case EventSet:
  632. DebugTrace( 0, me, "CcWorkerThread SetEvent Event = %08lx\n",
  633. WorkQueueEntry->Parameters.Event.Event );
  634. KeSetEvent( WorkQueueEntry->Parameters.Event.Event, 0, FALSE );
  635. DropThrottle = TRUE;
  636. break;
  637. //
  638. // Perform Lazy Write Scan
  639. //
  640. case LazyWriteScan:
  641. DebugTrace( 0, me, "CcWorkerThread Lazy Write Scan\n", 0 );
  642. CcLazyWriteScan();
  643. break;
  644. }
  645. }
  646. except( CcExceptionFilter( GetExceptionCode() )) {
  647. NOTHING;
  648. }
  649. //
  650. // If not a requeue request, free the workitem.
  651. //
  652. if (IoStatus.Information != CC_REQUEUE) {
  653. CcFreeWorkQueueEntry( WorkQueueEntry );
  654. }
  655. }
  656. //
  657. // No more work. Requeue our worker thread entry and get out.
  658. //
  659. InsertTailList( &CcIdleWorkerThreadList,
  660. &((PWORK_QUEUE_ITEM)ExWorkQueueItem)->List );
  661. CcNumberActiveWorkerThreads -= 1;
  662. CcReleaseWorkQueueLock( OldIrql );
  663. if (!IsListEmpty(&CcDeferredWrites) && (CcTotalDirtyPages >= 20) && RescanOk) {
  664. CcLazyWriteScan();
  665. }
  666. return;
  667. }