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.

724 lines
21 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. LogRcSup.c
  5. Abstract:
  6. This module implements support for dealing with log records, both
  7. writing and recovering them.
  8. Author:
  9. Brian Andrew [BrianAn] 20-June-1991
  10. Revision History:
  11. --*/
  12. #include "lfsprocs.h"
  13. //
  14. // The debug trace level
  15. //
  16. #define Dbg (DEBUG_TRACE_LOG_RECORD_SUP)
  17. VOID
  18. LfsPrepareLfcbForLogRecord (
  19. IN OUT PLFCB Lfcb,
  20. IN ULONG RemainingLogBytes
  21. );
  22. VOID
  23. LfsTransferLogBytes (
  24. IN PLBCB Lbcb,
  25. IN OUT PLFS_WRITE_ENTRY *ThisWriteEntry,
  26. IN OUT PCHAR *CurrentBuffer,
  27. IN OUT PULONG CurrentByteCount,
  28. IN OUT PULONG PadBytes,
  29. IN OUT PULONG RemainingPageBytes,
  30. IN OUT PULONG RemainingLogBytes
  31. );
  32. #ifdef ALLOC_PRAGMA
  33. #pragma alloc_text(PAGE, LfsPrepareLfcbForLogRecord)
  34. #pragma alloc_text(PAGE, LfsTransferLogBytes)
  35. #pragma alloc_text(PAGE, LfsWriteLogRecordIntoLogPage)
  36. #endif
  37. BOOLEAN
  38. LfsWriteLogRecordIntoLogPage (
  39. IN PLFCB Lfcb,
  40. IN PLCH Lch,
  41. IN ULONG NumberOfWriteEntries,
  42. IN PLFS_WRITE_ENTRY WriteEntries,
  43. IN LFS_RECORD_TYPE RecordType,
  44. IN TRANSACTION_ID *TransactionId OPTIONAL,
  45. IN LSN ClientUndoNextLsn OPTIONAL,
  46. IN LSN ClientPreviousLsn OPTIONAL,
  47. IN LONG UndoRequirement,
  48. IN BOOLEAN ForceToDisk,
  49. OUT PLSN Lsn
  50. )
  51. /*++
  52. Routine Description:
  53. This routine is called to write a log record into the log file
  54. using the cache manager. If there is room in the current log
  55. page it is added to that. Otherwise we allocate a new log page
  56. and write the log record header for this log page. We then
  57. write the log record into the remaining bytes of this page and
  58. into any subsequent pages if needed.
  59. Arguments:
  60. Lfcb - File control block for this log file.
  61. Lch - This is the client handle, we may update the undo space for this
  62. client.
  63. NumberOfWriteEntries - Number of components of the log record.
  64. WriteEntries - Pointer to an array of write entries.
  65. UndoRequirement - Signed value indicating the requirement to write
  66. an abort log record for this log record. A negative
  67. value indicates that this is the abort record.
  68. RecordType - The Lfs-defined type of this log record.
  69. TransactionId - Pointer to the transaction structure containing the
  70. Id for transaction containing this operation.
  71. ClientUndoNextLsn - This is the Lsn provided by the client for use
  72. in his restart. Will be the zero Lsn for
  73. a restart log record.
  74. ClientPreviousLsn - This is the Lsn provided by the client for use
  75. in his restart. Will the the zero Lsn for a
  76. restart log record.
  77. UndoRequirement - This is the data size for the undo record for
  78. this log record.
  79. ForceToDisk - Indicates if this log record will be flushed immediately
  80. to disk.
  81. Lsn - A pointer to store the Lsn for this log record.
  82. Return Value:
  83. BOOLEAN - Advisory, TRUE indicates that less than 1/4 of the log file is
  84. available.
  85. --*/
  86. {
  87. PLFS_WRITE_ENTRY ThisWriteEntry;
  88. ULONG RemainingLogBytes;
  89. ULONG OriginalLogBytes;
  90. ULONG RemainingPageBytes;
  91. ULONG HeaderAdjust;
  92. PLBCB ThisLbcb;
  93. LSN NextLsn;
  94. PLFS_RECORD_HEADER RecordHeader;
  95. PCHAR CurrentBuffer;
  96. ULONG CurrentByteCount;
  97. ULONG PadBytes;
  98. LFS_WAITER LfsWaiter;
  99. BOOLEAN LogFileFull = FALSE;
  100. PAGED_CODE();
  101. DebugTrace( +1, Dbg, "LfsWriteLogRecordIntoLogPage: Entered\n", 0 );
  102. DebugTrace( 0, Dbg, "Lfcb -> %08lx\n", Lfcb );
  103. DebugTrace( 0, Dbg, "Lch -> %08lx\n", Lch );
  104. DebugTrace( 0, Dbg, "Number of Write Entries -> %08lx\n", NumberOfWriteEntries );
  105. DebugTrace( 0, Dbg, "Write Entries -> %08lx\n", WriteEntries );
  106. DebugTrace( 0, Dbg, "Record Type -> %08lx\n", RecordType );
  107. DebugTrace( 0, Dbg, "Transaction Id -> %08lx\n", TransactionId );
  108. DebugTrace( 0, Dbg, "ClientUndoNextLsn (Low) -> %08lx\n", ClientUndoNextLsn.LowPart );
  109. DebugTrace( 0, Dbg, "ClientUndoNextLsn (High) -> %08lx\n", ClientUndoNextLsn.HighPart );
  110. DebugTrace( 0, Dbg, "ClientPreviousLsn (Low) -> %08lx\n", ClientPreviousLsn.LowPart );
  111. DebugTrace( 0, Dbg, "ClientPreviousLsn (High) -> %08lx\n", ClientPreviousLsn.HighPart );
  112. DebugTrace( 0, Dbg, "UndoRequirement -> %08lx\n", UndoRequirement );
  113. DebugTrace( 0, Dbg, "ForceToDisk -> %04x\n", ForceToDisk );
  114. //
  115. // We'd absolutely hate for this to happen on a read only volume.
  116. //
  117. ASSERT( !(FlagOn( Lfcb->Flags, LFCB_READ_ONLY )));
  118. //
  119. // We compute the size of this log record.
  120. //
  121. ThisWriteEntry = WriteEntries;
  122. RemainingLogBytes = 0;
  123. while (NumberOfWriteEntries--) {
  124. RemainingLogBytes += QuadAlign( ThisWriteEntry->ByteLength );
  125. ThisWriteEntry++;
  126. }
  127. OriginalLogBytes = RemainingLogBytes;
  128. ThisWriteEntry = WriteEntries;
  129. //
  130. // Loop until we have the Lbcb and we know it is not part of
  131. // a partial page transfer. We need to make sure we have
  132. // a Bcb for this page.
  133. //
  134. while (TRUE) {
  135. LogFileFull = LfsVerifyLogSpaceAvail( Lfcb,
  136. Lch,
  137. RemainingLogBytes,
  138. UndoRequirement,
  139. ForceToDisk );
  140. //
  141. // We update the Lfcb so that we can start putting the log record into
  142. // the top of the Lbcb active list.
  143. //
  144. LfsPrepareLfcbForLogRecord( Lfcb,
  145. RemainingLogBytes + Lfcb->RecordHeaderLength );
  146. ThisLbcb = CONTAINING_RECORD( Lfcb->LbcbActive.Flink,
  147. LBCB,
  148. ActiveLinks );
  149. #ifdef BENL_DBG
  150. ASSERT( ThisLbcb->BufferOffset < 0x1000 );
  151. #endif
  152. //
  153. // If there is a Bcb then we are golden.
  154. //
  155. if (ThisLbcb->LogPageBcb != NULL) { break; }
  156. //
  157. // Otherwise we want to drop the Lfcb and wait for the IO to complete.
  158. //
  159. Lfcb->Waiters += 1;
  160. KeInitializeEvent( &LfsWaiter.Event, SynchronizationEvent, FALSE );
  161. LfsWaiter.Lsn.QuadPart = 0;
  162. //
  163. // Setup a lfs waiter to be signalled if io is ongoing - since
  164. // the lfcb is owned exclusive we don't need to use the sync fast mutex
  165. //
  166. if (Lfcb->Sync->LfsIoState == LfsNoIoInProgress) {
  167. LfsWaiter.Waiters.Flink = NULL;
  168. } else {
  169. InsertHeadList( &Lfcb->WaiterList, &LfsWaiter.Waiters );
  170. }
  171. LfsReleaseLfcb( Lfcb );
  172. //
  173. // If we really found i/o ongoing then wait on the event
  174. //
  175. if (LfsWaiter.Waiters.Flink != NULL) {
  176. KeWaitForSingleObject( &LfsWaiter.Event,
  177. Executive,
  178. KernelMode,
  179. FALSE,
  180. NULL );
  181. }
  182. LfsAcquireLfcbExclusive( Lfcb );
  183. Lfcb->Waiters -= 1;
  184. }
  185. RemainingPageBytes = (ULONG)Lfcb->LogPageSize - (ULONG)ThisLbcb->BufferOffset;
  186. //
  187. // Compute the Lsn starting in the next log buffer.
  188. //
  189. NextLsn.QuadPart = LfsComputeLsnFromLbcb( Lfcb, ThisLbcb );
  190. //
  191. // We get a pointer to the log record header and the start of the
  192. // log record in the pinned buffer.
  193. //
  194. RecordHeader = Add2Ptr( ThisLbcb->PageHeader,
  195. (ULONG)ThisLbcb->BufferOffset,
  196. PLFS_RECORD_HEADER );
  197. //
  198. // We update the record header.
  199. //
  200. //
  201. // Zero out the structure initially.
  202. //
  203. RtlZeroMemory( RecordHeader, Lfcb->RecordHeaderLength );
  204. //
  205. // Update all the fields.
  206. //
  207. RecordHeader->ThisLsn = NextLsn;
  208. RecordHeader->ClientPreviousLsn = ClientPreviousLsn;
  209. RecordHeader->ClientUndoNextLsn = ClientUndoNextLsn;
  210. if (TransactionId != NULL) {
  211. RecordHeader->TransactionId = *TransactionId;
  212. }
  213. RecordHeader->ClientDataLength = RemainingLogBytes;
  214. RecordHeader->ClientId = Lch->ClientId;
  215. RecordHeader->RecordType = RecordType;
  216. //
  217. // Check if this is a multi-page record.
  218. //
  219. if (RemainingLogBytes + Lfcb->RecordHeaderLength > RemainingPageBytes) {
  220. SetFlag( RecordHeader->Flags, LOG_RECORD_MULTI_PAGE );
  221. }
  222. RemainingPageBytes -= Lfcb->RecordHeaderLength;
  223. //
  224. // Update the buffer position in the Lbcb
  225. //
  226. (ULONG)ThisLbcb->BufferOffset += Lfcb->RecordHeaderLength;
  227. HeaderAdjust = Lfcb->RecordHeaderLength;
  228. //
  229. // Remember the values in the current write entry.
  230. //
  231. CurrentBuffer = ThisWriteEntry->Buffer;
  232. CurrentByteCount = ThisWriteEntry->ByteLength;
  233. PadBytes = (8 - (CurrentByteCount & ~(0xfffffff8))) & ~(0xfffffff8);
  234. //
  235. // Continue to transfer bytes until all the client's data has
  236. // been transferred.
  237. //
  238. while (RemainingLogBytes != 0) {
  239. PLFS_RECORD_PAGE_HEADER PageHeader;
  240. PageHeader = (PLFS_RECORD_PAGE_HEADER) ThisLbcb->PageHeader;
  241. //
  242. // If the Lbcb is empty and we are about to store data into it we
  243. // subtract the data size of the page from the available space.
  244. // Update all the information we want to put in the header.
  245. //
  246. if (!FlagOn( ThisLbcb->LbcbFlags, LBCB_NOT_EMPTY )) {
  247. //
  248. // We subtract this page from the available pages only if
  249. // we are at the beginning of the page. Otherwise this
  250. // could be a reuse page. In that case it has already
  251. // been subtracted.
  252. //
  253. if ((ULONG)ThisLbcb->BufferOffset - HeaderAdjust == (ULONG)Lfcb->LogPageDataOffset) {
  254. Lfcb->CurrentAvailable = Lfcb->CurrentAvailable - Lfcb->ReservedLogPageSize; //**** xxSub( Lfcb->CurrentAvailable, Lfcb->ReservedLogPageSize );
  255. }
  256. InsertTailList( &Lfcb->LbcbWorkque, &ThisLbcb->WorkqueLinks );
  257. SetFlag( ThisLbcb->LbcbFlags, LBCB_NOT_EMPTY );
  258. }
  259. HeaderAdjust = 0;
  260. //
  261. // Compute the number of transfer bytes. Update the remaining
  262. // page bytes, remaining log bytes and position in the write
  263. // buffer array. This routine also copies the bytes into the buffer.
  264. //
  265. LfsTransferLogBytes( ThisLbcb,
  266. &ThisWriteEntry,
  267. &CurrentBuffer,
  268. &CurrentByteCount,
  269. &PadBytes,
  270. &RemainingPageBytes,
  271. &RemainingLogBytes );
  272. //
  273. // This log record ends on this page. Update the fields for the
  274. // ending Lsn.
  275. //
  276. if (RemainingLogBytes == 0) {
  277. SetFlag( ThisLbcb->Flags, LOG_PAGE_LOG_RECORD_END );
  278. ThisLbcb->LastEndLsn = NextLsn;
  279. if (FlagOn( Lfcb->Flags, LFCB_PACK_LOG )) {
  280. PageHeader->Header.Packed.LastEndLsn = NextLsn;
  281. PageHeader->Header.Packed.NextRecordOffset = (USHORT)ThisLbcb->BufferOffset;
  282. }
  283. }
  284. //
  285. // We are done with this page, update the fields in the page header.
  286. //
  287. if ((RemainingPageBytes == 0) ||
  288. (RemainingLogBytes == 0)) {
  289. //
  290. // We are done with this page. Update the Lbcb and page header.
  291. //
  292. ThisLbcb->LastLsn = NextLsn;
  293. PageHeader->Copy.LastLsn = NextLsn;
  294. PageHeader->Flags = ThisLbcb->Flags;
  295. //
  296. // We can't put any more log records on this page. Remove
  297. // it from the active queue.
  298. //
  299. if (RemainingPageBytes < Lfcb->RecordHeaderLength) {
  300. RemoveHeadList( &Lfcb->LbcbActive );
  301. ClearFlag( ThisLbcb->LbcbFlags, LBCB_ON_ACTIVE_QUEUE );
  302. //
  303. // If there are more log bytes then get the next Lbcb.
  304. //
  305. if (RemainingLogBytes != 0) {
  306. ThisLbcb = CONTAINING_RECORD( Lfcb->LbcbActive.Flink,
  307. LBCB,
  308. ActiveLinks );
  309. RemainingPageBytes = (ULONG)Lfcb->LogPageSize
  310. - (ULONG)ThisLbcb->BufferOffset;
  311. }
  312. }
  313. }
  314. }
  315. *Lsn = NextLsn;
  316. Lfcb->RestartArea->CurrentLsn = NextLsn;
  317. Lfcb->LfsRestartBias = 1;
  318. Lfcb->RestartArea->LastLsnDataLength = OriginalLogBytes;
  319. ClearFlag( Lfcb->Flags, LFCB_NO_LAST_LSN );
  320. DebugTrace( 0, Dbg, "Lsn (Low) -> %08lx\n", Lsn->LowPart );
  321. DebugTrace( 0, Dbg, "Lsn (High) -> %08lx\n", Lsn->HighPart );
  322. DebugTrace( -1, Dbg, "LfsWriteLogRecordIntoLogPage: Exit\n", 0 );
  323. return LogFileFull;
  324. }
  325. //
  326. // Local support routine.
  327. //
  328. VOID
  329. LfsPrepareLfcbForLogRecord (
  330. IN OUT PLFCB Lfcb,
  331. IN ULONG RemainingLogBytes
  332. )
  333. /*++
  334. Routine Description:
  335. This routine is called to insure that the Lfcb has a Lbcb in the
  336. active queue to perform the next log record transfer.
  337. This condition is met when there is a least one buffer block and
  338. the log record data will fit entirely on this page or this buffer
  339. block contains no other data in the unpacked case. For the packed
  340. case we just need to make sure that there are sufficient Lbcb's.
  341. Arguments:
  342. Lfcb - File control block for this log file.
  343. RemainingLogBytes - The number of bytes remaining for this log record.
  344. Return Value:
  345. None
  346. --*/
  347. {
  348. PLBCB ThisLbcb;
  349. ULONG RemainingPageBytes;
  350. PLIST_ENTRY LbcbLinks;
  351. PAGED_CODE();
  352. DebugTrace( +1, Dbg, "LfsPrepareLfcbForLogRecord: Entered\n", 0 );
  353. DebugTrace( 0, Dbg, "Lfcb -> %08lx\n", Lfcb );
  354. DebugTrace( 0, Dbg, "RemainingLogBytes -> %08lx\n", RemainingLogBytes );
  355. //
  356. // If there is no Lbcb in the active queue, we don't check it for size.
  357. //
  358. if (!IsListEmpty( &Lfcb->LbcbActive )) {
  359. //
  360. // If the log record won't fit in the remaining bytes of this page,
  361. // we queue this log buffer.
  362. //
  363. ThisLbcb = CONTAINING_RECORD( Lfcb->LbcbActive.Flink,
  364. LBCB,
  365. ActiveLinks );
  366. RemainingPageBytes = (ULONG)Lfcb->LogPageSize
  367. - (ULONG)ThisLbcb->BufferOffset;
  368. //
  369. // This log page won't do if the remaining bytes won't hold the data
  370. // unless this is the first log record in the page or we are packing
  371. // the log file.
  372. //
  373. if ((RemainingLogBytes > RemainingPageBytes) &&
  374. !FlagOn( Lfcb->Flags, LFCB_PACK_LOG ) &&
  375. ((ULONG)ThisLbcb->BufferOffset != (ULONG)Lfcb->LogPageDataOffset)) {
  376. RemoveHeadList( &Lfcb->LbcbActive );
  377. ClearFlag( ThisLbcb->LbcbFlags, LBCB_ON_ACTIVE_QUEUE );
  378. }
  379. }
  380. //
  381. // We now make sure we can allocate enough Lbcb's for all of the log pages
  382. // we will need. We now include the bytes for the log record reader.
  383. //
  384. LbcbLinks = Lfcb->LbcbActive.Flink;
  385. while (TRUE) {
  386. //
  387. // If the Lbcb link we have is the head of the list, we will need another
  388. // Lbcb.
  389. //
  390. if (LbcbLinks == &Lfcb->LbcbActive) {
  391. ThisLbcb = LfsGetLbcb( Lfcb );
  392. } else {
  393. ThisLbcb = CONTAINING_RECORD( LbcbLinks,
  394. LBCB,
  395. ActiveLinks );
  396. }
  397. //
  398. // Remember the bytes remaining on this page. This will always be quad
  399. // aligned.
  400. //
  401. RemainingPageBytes = (ULONG)Lfcb->LogPageSize - (ULONG)ThisLbcb->BufferOffset;
  402. if (RemainingPageBytes >= RemainingLogBytes) {
  403. break;
  404. }
  405. //
  406. // Move to the next log record.
  407. //
  408. RemainingLogBytes -= RemainingPageBytes;
  409. LbcbLinks = ThisLbcb->ActiveLinks.Flink;
  410. }
  411. DebugTrace( -1, Dbg, "LfsPrepareLfcbForLogRecord: Exit\n", 0 );
  412. return;
  413. }
  414. VOID
  415. LfsTransferLogBytes (
  416. IN PLBCB Lbcb,
  417. IN OUT PLFS_WRITE_ENTRY *ThisWriteEntry,
  418. IN OUT PCHAR *CurrentBuffer,
  419. IN OUT PULONG CurrentByteCount,
  420. IN OUT PULONG PadBytes,
  421. IN OUT PULONG RemainingPageBytes,
  422. IN OUT PULONG RemainingLogBytes
  423. )
  424. /*++
  425. Routine Description:
  426. This routine is called to transfer the next block of bytes into
  427. a log page. It is given a pointer to the current position in the
  428. current Lfs write entry and the number of bytes remaining on that
  429. log page. It will transfer as many of the client's bytes from the
  430. current buffer that will fit and update various pointers.
  431. Arguments:
  432. Lbcb - This is the buffer block for this log page.
  433. ThisWriteEntry - This is a pointer to a pointer to the current Lfs
  434. write entry.
  435. CurrentBuffer - This is a pointer to a pointer to the current position
  436. in the current write entry buffer. If this points to a NULL
  437. value it means to put zero bytes into the log.
  438. CurrentByteCount - This is a pointer to the number of bytes remaining
  439. in the current buffer.
  440. PadBytes - This is a pointer to the number of padding byes for
  441. this write entry.
  442. RemainingPageBytes - This is pointer to the number of bytes remaining
  443. in this page.
  444. RemainingLogBytes - This is the number of bytes remaining to transfer
  445. for this log record.
  446. Return Value:
  447. None
  448. --*/
  449. {
  450. PCHAR CurrentLogPagePosition;
  451. PCHAR CurrentClientPosition;
  452. ULONG TransferBytes;
  453. ULONG ThisPadBytes;
  454. PAGED_CODE();
  455. DebugTrace( +1, Dbg, "LfsTransferLogBytes: Entered\n", 0 );
  456. DebugTrace( 0, Dbg, "Lbcb -> %08lx\n", Lbcb );
  457. DebugTrace( 0, Dbg, "ThisWriteEntry -> %08lx\n", *ThisWriteEntry );
  458. DebugTrace( 0, Dbg, "CurrentBuffer -> %08lx\n", *CurrentBuffer );
  459. DebugTrace( 0, Dbg, "CurrentByteCount -> %08lx\n", *CurrentByteCount );
  460. DebugTrace( 0, Dbg, "RemainingPageBytes -> %08lx\n", *RemainingPageBytes );
  461. DebugTrace( 0, Dbg, "RemainingLogBytes -> %08lx\n", *RemainingLogBytes );
  462. //
  463. // Remember the current client buffer position and current position
  464. // in log page.
  465. //
  466. CurrentLogPagePosition = Add2Ptr( Lbcb->PageHeader, (ULONG)Lbcb->BufferOffset, PCHAR );
  467. CurrentClientPosition = *CurrentBuffer;
  468. //
  469. // The limiting factor is either the number of bytes remaining in a
  470. // write entry or the number remaining in the log page.
  471. //
  472. if (*CurrentByteCount <= *RemainingPageBytes) {
  473. TransferBytes = *CurrentByteCount;
  474. ThisPadBytes = *PadBytes;
  475. if (*RemainingLogBytes != (*CurrentByteCount + *PadBytes) ) {
  476. (*ThisWriteEntry)++;
  477. *CurrentBuffer = (*ThisWriteEntry)->Buffer;
  478. *CurrentByteCount = (*ThisWriteEntry)->ByteLength;
  479. *PadBytes = (8 - (*CurrentByteCount & ~(0xfffffff8))) & ~(0xfffffff8);
  480. }
  481. } else {
  482. TransferBytes = *RemainingPageBytes;
  483. ThisPadBytes = 0;
  484. *CurrentByteCount -= TransferBytes;
  485. if (*CurrentBuffer != NULL) {
  486. *CurrentBuffer += TransferBytes;
  487. }
  488. }
  489. //
  490. // Transfer the requested bytes.
  491. //
  492. if (CurrentClientPosition != NULL) {
  493. RtlCopyMemory( CurrentLogPagePosition, CurrentClientPosition, TransferBytes );
  494. } else {
  495. RtlZeroMemory( CurrentLogPagePosition, TransferBytes );
  496. }
  497. //
  498. // Reduce the remaining page and log bytes by the transfer amount and
  499. // move forward in the log page.
  500. //
  501. *RemainingLogBytes -= (TransferBytes + ThisPadBytes);
  502. *RemainingPageBytes -= (TransferBytes + ThisPadBytes);
  503. (ULONG)Lbcb->BufferOffset += (TransferBytes + ThisPadBytes);
  504. DebugTrace( -1, Dbg, "LfsTransferLogBytes: Exit\n", 0 );
  505. return;
  506. }