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.

703 lines
19 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. logsup.c
  5. Abstract:
  6. This module implements the special cache manager support for logging
  7. file systems.
  8. Author:
  9. Tom Miller [TomM] 30-Jul-1991
  10. Revision History:
  11. --*/
  12. #include "cc.h"
  13. //
  14. // Define our debug constant
  15. //
  16. #define me 0x0000040
  17. #ifdef ALLOC_PRAGMA
  18. #pragma alloc_text(PAGE,CcSetLogHandleForFile)
  19. #endif
  20. VOID
  21. CcSetAdditionalCacheAttributes (
  22. IN PFILE_OBJECT FileObject,
  23. IN BOOLEAN DisableReadAhead,
  24. IN BOOLEAN DisableWriteBehind
  25. )
  26. /*++
  27. Routine Description:
  28. This routine supports the setting of disable read ahead or disable write
  29. behind flags to control Cache Manager operation. This routine may be
  30. called any time after calling CcInitializeCacheMap. Initially both
  31. read ahead and write behind are enabled. Note that the state of both
  32. of these flags must be specified on each call to this routine.
  33. Arguments:
  34. FileObject - File object for which the respective flags are to be set.
  35. DisableReadAhead - FALSE to enable read ahead, TRUE to disable it.
  36. DisableWriteBehind - FALSE to enable write behind, TRUE to disable it.
  37. Return Value:
  38. None.
  39. --*/
  40. {
  41. PSHARED_CACHE_MAP SharedCacheMap;
  42. KIRQL OldIrql;
  43. //
  44. // Get pointer to SharedCacheMap.
  45. //
  46. SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
  47. //
  48. // Now set the flags and return.
  49. //
  50. CcAcquireMasterLock( &OldIrql );
  51. if (DisableReadAhead) {
  52. SetFlag(SharedCacheMap->Flags, DISABLE_READ_AHEAD);
  53. } else {
  54. ClearFlag(SharedCacheMap->Flags, DISABLE_READ_AHEAD);
  55. }
  56. if (DisableWriteBehind) {
  57. SetFlag(SharedCacheMap->Flags, DISABLE_WRITE_BEHIND | MODIFIED_WRITE_DISABLED);
  58. } else {
  59. ClearFlag(SharedCacheMap->Flags, DISABLE_WRITE_BEHIND);
  60. }
  61. CcReleaseMasterLock( OldIrql );
  62. }
  63. NTKERNELAPI
  64. BOOLEAN
  65. CcSetPrivateWriteFile(
  66. PFILE_OBJECT FileObject
  67. )
  68. /*++
  69. Routine Description:
  70. This routine will instruct the cache manager to treat the file as
  71. a private-write stream, so that a caller can implement a private
  72. logging mechanism for it. We will turn on both Mm's modify-no-write
  73. and our disable-write-behind, and disallow non-aware flush/purge for
  74. the file.
  75. Caching must already be initiated on the file.
  76. This routine is only exported to the kernel.
  77. Arguments:
  78. FileObject - File to make private-write.
  79. Return Value:
  80. None.
  81. --*/
  82. {
  83. PSHARED_CACHE_MAP SharedCacheMap;
  84. BOOLEAN Disabled;
  85. KIRQL OldIrql;
  86. PVACB Vacb;
  87. ULONG ActivePage;
  88. ULONG PageIsDirty;
  89. //
  90. // Pick up the file exclusive to synchronize against readahead and
  91. // other purge/map activity.
  92. //
  93. FsRtlAcquireFileExclusive( FileObject );
  94. //
  95. // Get pointer to SharedCacheMap.
  96. //
  97. if( FileObject->SectionObjectPointer == NULL ) {
  98. return FALSE;
  99. }
  100. SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
  101. if( !SharedCacheMap ) {
  102. return FALSE;
  103. }
  104. //
  105. // Unmap all the views in preparation for making the disable mw call.
  106. //
  107. //
  108. // We still need to wait for any dangling cache read or writes.
  109. //
  110. // In fact we have to loop and wait because the lazy writer can
  111. // sneak in and do an CcGetVirtualAddressIfMapped, and we are not
  112. // synchronized.
  113. //
  114. // This is the same bit of code that our purge will do. We assume
  115. // that a private writer has succesfully blocked out other activity.
  116. //
  117. //
  118. // If there is an active Vacb, then nuke it now (before waiting!).
  119. //
  120. CcAcquireMasterLock( &OldIrql );
  121. GetActiveVacbAtDpcLevel( SharedCacheMap, Vacb, ActivePage, PageIsDirty );
  122. CcReleaseMasterLock( OldIrql );
  123. if (Vacb != NULL) {
  124. CcFreeActiveVacb( SharedCacheMap, Vacb, ActivePage, PageIsDirty );
  125. }
  126. while ((SharedCacheMap->Vacbs != NULL) &&
  127. !CcUnmapVacbArray( SharedCacheMap, NULL, 0, FALSE )) {
  128. CcWaitOnActiveCount( SharedCacheMap );
  129. }
  130. //
  131. // Knock the file down.
  132. //
  133. CcFlushCache( FileObject->SectionObjectPointer, NULL, 0, NULL );
  134. //
  135. // Now the file is clean and unmapped. We can still have a racing
  136. // lazy writer, though.
  137. //
  138. // We just wait for the lazy writer queue to drain before disabling
  139. // modified write. There may be a better way to do this by having
  140. // an event for the WRITE_QUEUED flag. ? This would also let us
  141. // dispense with the pagingio pick/drop in the FS cache coherency
  142. // paths, but there could be reasons why CcFlushCache shouldn't
  143. // always do such a block. Investigate this.
  144. //
  145. // This wait takes on the order of ~.5s avg. case.
  146. //
  147. CcAcquireMasterLock( &OldIrql );
  148. if (FlagOn( SharedCacheMap->Flags, WRITE_QUEUED )) {
  149. CcReleaseMasterLock( OldIrql );
  150. FsRtlReleaseFile( FileObject );
  151. CcWaitForCurrentLazyWriterActivity();
  152. FsRtlAcquireFileExclusive( FileObject );
  153. } else {
  154. CcReleaseMasterLock( OldIrql );
  155. }
  156. //
  157. // Now set the flags and return. We do not set our MODIFIED_WRITE_DISABLED
  158. // since we don't want to fully promote this cache map. Future?
  159. //
  160. Disabled = MmDisableModifiedWriteOfSection( FileObject->SectionObjectPointer );
  161. if (Disabled) {
  162. CcAcquireMasterLock( &OldIrql );
  163. SetFlag(SharedCacheMap->Flags, DISABLE_WRITE_BEHIND | PRIVATE_WRITE);
  164. CcReleaseMasterLock( OldIrql );
  165. }
  166. //
  167. // Now release the file for regular operation.
  168. //
  169. FsRtlReleaseFile( FileObject );
  170. return Disabled;
  171. }
  172. VOID
  173. CcSetLogHandleForFile (
  174. IN PFILE_OBJECT FileObject,
  175. IN PVOID LogHandle,
  176. IN PFLUSH_TO_LSN FlushToLsnRoutine
  177. )
  178. /*++
  179. Routine Description:
  180. This routine may be called to instruct the Cache Manager to store the
  181. specified log handle with the shared cache map for a file, to support
  182. subsequent calls to the other routines in this module which effectively
  183. perform an associative search for files by log handle.
  184. Arguments:
  185. FileObject - File for which the log handle should be stored.
  186. LogHandle - Log Handle to store.
  187. FlushToLsnRoutine - A routine to call before flushing buffers for this
  188. file, to insure a log file is flushed to the most
  189. recent Lsn for any Bcb being flushed.
  190. Return Value:
  191. None.
  192. --*/
  193. {
  194. PSHARED_CACHE_MAP SharedCacheMap;
  195. //
  196. // Get pointer to SharedCacheMap.
  197. //
  198. SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
  199. //
  200. // Now set the log file handle and flush routine
  201. //
  202. SharedCacheMap->LogHandle = LogHandle;
  203. SharedCacheMap->FlushToLsnRoutine = FlushToLsnRoutine;
  204. }
  205. LARGE_INTEGER
  206. CcGetDirtyPages (
  207. IN PVOID LogHandle,
  208. IN PDIRTY_PAGE_ROUTINE DirtyPageRoutine,
  209. IN PVOID Context1,
  210. IN PVOID Context2
  211. )
  212. /*++
  213. Routine Description:
  214. This routine may be called to return all of the dirty pages in all files
  215. for a given log handle. Each page is returned by an individual call to
  216. the Dirty Page Routine. The Dirty Page Routine is defined by a prototype
  217. in ntos\inc\cache.h.
  218. Arguments:
  219. LogHandle - Log Handle which must match the log handle previously stored
  220. for all files which are to be returned.
  221. DirtyPageRoutine -- The routine to call as each dirty page for this log
  222. handle is found.
  223. Context1 - First context parameter to be passed to the Dirty Page Routine.
  224. Context2 - First context parameter to be passed to the Dirty Page Routine.
  225. Return Value:
  226. LARGE_INTEGER - Oldest Lsn found of all the dirty pages, or 0 if no dirty pages
  227. --*/
  228. {
  229. PSHARED_CACHE_MAP SharedCacheMap;
  230. PBCB Bcb, BcbToUnpin = NULL;
  231. KLOCK_QUEUE_HANDLE LockHandle;
  232. LARGE_INTEGER SavedFileOffset, SavedOldestLsn, SavedNewestLsn;
  233. ULONG SavedByteLength;
  234. LARGE_INTEGER OldestLsn = {0,0};
  235. //
  236. // Synchronize with changes to the SharedCacheMap list.
  237. //
  238. CcAcquireMasterLock( &LockHandle.OldIrql );
  239. SharedCacheMap = CONTAINING_RECORD( CcDirtySharedCacheMapList.SharedCacheMapLinks.Flink,
  240. SHARED_CACHE_MAP,
  241. SharedCacheMapLinks );
  242. //
  243. // Use try/finally for cleanup. The only spot where we can raise is out of the
  244. // filesystem callback, but we have the exception handler out here so we aren't
  245. // constantly setting/unsetting it.
  246. //
  247. try {
  248. while (&SharedCacheMap->SharedCacheMapLinks != &CcDirtySharedCacheMapList.SharedCacheMapLinks) {
  249. //
  250. // Skip over cursors, SharedCacheMaps for other LogHandles, and ones with
  251. // no dirty pages
  252. //
  253. if (!FlagOn(SharedCacheMap->Flags, IS_CURSOR) && (SharedCacheMap->LogHandle == LogHandle) &&
  254. (SharedCacheMap->DirtyPages != 0)) {
  255. //
  256. // This SharedCacheMap should stick around for a while in the dirty list.
  257. //
  258. CcIncrementOpenCount( SharedCacheMap, 'pdGS' );
  259. SharedCacheMap->DirtyPages += 1;
  260. CcReleaseMasterLock( LockHandle.OldIrql );
  261. //
  262. // Set our initial resume point and point to first Bcb in List.
  263. //
  264. KeAcquireInStackQueuedSpinLock( &SharedCacheMap->BcbSpinLock, &LockHandle );
  265. Bcb = CONTAINING_RECORD( SharedCacheMap->BcbList.Flink, BCB, BcbLinks );
  266. //
  267. // Scan to the end of the Bcb list.
  268. //
  269. while (&Bcb->BcbLinks != &SharedCacheMap->BcbList) {
  270. //
  271. // If the Bcb is dirty, then capture the inputs for the
  272. // callback routine so we can call without holding a spinlock.
  273. //
  274. if ((Bcb->NodeTypeCode == CACHE_NTC_BCB) && Bcb->Dirty) {
  275. SavedFileOffset = Bcb->FileOffset;
  276. SavedByteLength = Bcb->ByteLength;
  277. SavedOldestLsn = Bcb->OldestLsn;
  278. SavedNewestLsn = Bcb->NewestLsn;
  279. //
  280. // Increment PinCount so the Bcb sticks around
  281. //
  282. Bcb->PinCount += 1;
  283. KeReleaseInStackQueuedSpinLock( &LockHandle );
  284. //
  285. // Any Bcb to unref from a previous loop?
  286. //
  287. if (BcbToUnpin != NULL) {
  288. CcUnpinFileData( BcbToUnpin, TRUE, UNREF );
  289. BcbToUnpin = NULL;
  290. }
  291. //
  292. // Call the file system. This callback may raise status.
  293. //
  294. (*DirtyPageRoutine)( SharedCacheMap->FileObject,
  295. &SavedFileOffset,
  296. SavedByteLength,
  297. &SavedOldestLsn,
  298. &SavedNewestLsn,
  299. Context1,
  300. Context2 );
  301. //
  302. // Possibly update OldestLsn
  303. //
  304. if ((SavedOldestLsn.QuadPart != 0) &&
  305. ((OldestLsn.QuadPart == 0) || (SavedOldestLsn.QuadPart < OldestLsn.QuadPart ))) {
  306. OldestLsn = SavedOldestLsn;
  307. }
  308. //
  309. // Now reacquire the spinlock and scan from the resume point
  310. // point to the next Bcb to return in the descending list.
  311. //
  312. KeAcquireInStackQueuedSpinLock( &SharedCacheMap->BcbSpinLock, &LockHandle );
  313. //
  314. // Normally the Bcb can stay around a while, but if not,
  315. // we will just remember it for the next time we do not
  316. // have the spin lock. We cannot unpin it now, because
  317. // we would lose our place in the list.
  318. //
  319. // This is cheating, but it works and is sane since we're
  320. // already traversing the bcb list - dropping the bcb count
  321. // is OK, as long as we don't hit zero. Zero requires a
  322. // slight bit more attention that shouldn't be replicated.
  323. // (unmapping the view)
  324. //
  325. if (Bcb->PinCount > 1) {
  326. Bcb->PinCount -= 1;
  327. } else {
  328. BcbToUnpin = Bcb;
  329. }
  330. }
  331. Bcb = CONTAINING_RECORD( Bcb->BcbLinks.Flink, BCB, BcbLinks );
  332. }
  333. KeReleaseInStackQueuedSpinLock( &LockHandle );
  334. //
  335. // We need to unref any Bcb we are holding before moving on to
  336. // the next SharedCacheMap, or else CcDeleteSharedCacheMap will
  337. // also delete this Bcb.
  338. //
  339. if (BcbToUnpin != NULL) {
  340. CcUnpinFileData( BcbToUnpin, TRUE, UNREF );
  341. BcbToUnpin = NULL;
  342. }
  343. CcAcquireMasterLock( &LockHandle.OldIrql );
  344. //
  345. // Now release the SharedCacheMap, leaving it in the dirty list.
  346. //
  347. CcDecrementOpenCount( SharedCacheMap, 'pdGF' );
  348. SharedCacheMap->DirtyPages -= 1;
  349. }
  350. //
  351. // Now loop back for the next cache map.
  352. //
  353. SharedCacheMap =
  354. CONTAINING_RECORD( SharedCacheMap->SharedCacheMapLinks.Flink,
  355. SHARED_CACHE_MAP,
  356. SharedCacheMapLinks );
  357. }
  358. CcReleaseMasterLock( LockHandle.OldIrql );
  359. } finally {
  360. //
  361. // Drop the Bcb if we are being ejected. We are guaranteed that the
  362. // only raise is from the callback, at which point we have an incremented
  363. // pincount.
  364. //
  365. if (AbnormalTermination()) {
  366. CcUnpinFileData( Bcb, TRUE, UNPIN );
  367. }
  368. }
  369. return OldestLsn;
  370. }
  371. BOOLEAN
  372. CcIsThereDirtyData (
  373. IN PVPB Vpb
  374. )
  375. /*++
  376. Routine Description:
  377. This routine returns TRUE if the specified Vcb has any unwritten dirty
  378. data in the cache.
  379. Arguments:
  380. Vpb - specifies Vpb to check for
  381. Return Value:
  382. FALSE - if the Vpb has no dirty data
  383. TRUE - if the Vpb has dirty data
  384. --*/
  385. {
  386. PSHARED_CACHE_MAP SharedCacheMap;
  387. KIRQL OldIrql;
  388. ULONG LoopsWithLockHeld = 0;
  389. //
  390. // Synchronize with changes to the SharedCacheMap list.
  391. //
  392. CcAcquireMasterLock( &OldIrql );
  393. SharedCacheMap = CONTAINING_RECORD( CcDirtySharedCacheMapList.SharedCacheMapLinks.Flink,
  394. SHARED_CACHE_MAP,
  395. SharedCacheMapLinks );
  396. while (&SharedCacheMap->SharedCacheMapLinks != &CcDirtySharedCacheMapList.SharedCacheMapLinks) {
  397. //
  398. // Look at this one if the Vpb matches and if there is dirty data.
  399. // For what it's worth, don't worry about dirty data in temporary files,
  400. // as that should not concern the caller if it wants to dismount.
  401. //
  402. if (!FlagOn(SharedCacheMap->Flags, IS_CURSOR) &&
  403. (SharedCacheMap->FileObject->Vpb == Vpb) &&
  404. (SharedCacheMap->DirtyPages != 0) &&
  405. !FlagOn(SharedCacheMap->FileObject->Flags, FO_TEMPORARY_FILE)) {
  406. CcReleaseMasterLock( OldIrql );
  407. return TRUE;
  408. }
  409. //
  410. // Make sure we occasionally drop the lock. Set WRITE_QUEUED
  411. // to keep the guy from going away, and increment DirtyPages to
  412. // keep it in this list.
  413. //
  414. if ((++LoopsWithLockHeld >= 20) &&
  415. !FlagOn(SharedCacheMap->Flags, WRITE_QUEUED | IS_CURSOR)) {
  416. SetFlag( *((ULONG volatile *)&SharedCacheMap->Flags), WRITE_QUEUED);
  417. *((ULONG volatile *)&SharedCacheMap->DirtyPages) += 1;
  418. CcReleaseMasterLock( OldIrql );
  419. LoopsWithLockHeld = 0;
  420. CcAcquireMasterLock( &OldIrql );
  421. ClearFlag( *((ULONG volatile *)&SharedCacheMap->Flags), WRITE_QUEUED);
  422. *((ULONG volatile *)&SharedCacheMap->DirtyPages) -= 1;
  423. }
  424. //
  425. // Now loop back for the next cache map.
  426. //
  427. SharedCacheMap =
  428. CONTAINING_RECORD( SharedCacheMap->SharedCacheMapLinks.Flink,
  429. SHARED_CACHE_MAP,
  430. SharedCacheMapLinks );
  431. }
  432. CcReleaseMasterLock( OldIrql );
  433. return FALSE;
  434. }
  435. LARGE_INTEGER
  436. CcGetLsnForFileObject(
  437. IN PFILE_OBJECT FileObject,
  438. OUT PLARGE_INTEGER OldestLsn OPTIONAL
  439. )
  440. /*++
  441. Routine Description:
  442. This routine returns the oldest and newest LSNs for a file object.
  443. Arguments:
  444. FileObject - File for which the log handle should be stored.
  445. OldestLsn - pointer to location to store oldest LSN for file object.
  446. Return Value:
  447. The newest LSN for the file object.
  448. --*/
  449. {
  450. PBCB Bcb;
  451. KLOCK_QUEUE_HANDLE LockHandle;
  452. LARGE_INTEGER Oldest, Newest;
  453. PSHARED_CACHE_MAP SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
  454. //
  455. // initialize lsn variables
  456. //
  457. Oldest.LowPart = 0;
  458. Oldest.HighPart = 0;
  459. Newest.LowPart = 0;
  460. Newest.HighPart = 0;
  461. if(SharedCacheMap == NULL) {
  462. return Oldest;
  463. }
  464. KeAcquireInStackQueuedSpinLock(&SharedCacheMap->BcbSpinLock, &LockHandle);
  465. //
  466. // Now point to first Bcb in List, and loop through it.
  467. //
  468. Bcb = CONTAINING_RECORD( SharedCacheMap->BcbList.Flink, BCB, BcbLinks );
  469. while (&Bcb->BcbLinks != &SharedCacheMap->BcbList) {
  470. //
  471. // If the Bcb is dirty then capture the oldest and newest lsn
  472. //
  473. if ((Bcb->NodeTypeCode == CACHE_NTC_BCB) && Bcb->Dirty) {
  474. LARGE_INTEGER BcbLsn, BcbNewest;
  475. BcbLsn = Bcb->OldestLsn;
  476. BcbNewest = Bcb->NewestLsn;
  477. if ((BcbLsn.QuadPart != 0) &&
  478. ((Oldest.QuadPart == 0) ||
  479. (BcbLsn.QuadPart < Oldest.QuadPart))) {
  480. Oldest = BcbLsn;
  481. }
  482. if ((BcbLsn.QuadPart != 0) && (BcbNewest.QuadPart > Newest.QuadPart)) {
  483. Newest = BcbNewest;
  484. }
  485. }
  486. Bcb = CONTAINING_RECORD( Bcb->BcbLinks.Flink, BCB, BcbLinks );
  487. }
  488. //
  489. // Now release the spin lock for this Bcb list and generate a callback
  490. // if we got something.
  491. //
  492. KeReleaseInStackQueuedSpinLock( &LockHandle );
  493. if (ARGUMENT_PRESENT(OldestLsn)) {
  494. *OldestLsn = Oldest;
  495. }
  496. return Newest;
  497. }