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.

316 lines
8.3 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. LsnSup.c
  5. Abstract:
  6. This module implements support for manipulating Lsn's.
  7. Author:
  8. Brian Andrew [BrianAn] 20-June-1991
  9. Revision History:
  10. --*/
  11. #include "lfsprocs.h"
  12. //
  13. // The debug trace level
  14. //
  15. #define Dbg (DEBUG_TRACE_LSN_SUP)
  16. #ifdef ALLOC_PRAGMA
  17. #pragma alloc_text(PAGE, LfsFindNextLsn)
  18. #pragma alloc_text(PAGE, LfsLsnFinalOffset)
  19. #endif
  20. VOID
  21. LfsLsnFinalOffset (
  22. IN PLFCB Lfcb,
  23. IN LSN Lsn,
  24. IN ULONG DataLength,
  25. OUT PLONGLONG FinalOffset
  26. )
  27. /*++
  28. Routine Description:
  29. This routine will compute the final offset of the last byte of the log
  30. record. It does this by computing how many bytes are on the current
  31. page and then computing how many more pages will be needed.
  32. Arguments:
  33. Lfcb - This is the file control block for the log file.
  34. Lsn - This is the log record being considered.
  35. DataLength - This is the length of the data for this log record. We will add the
  36. header length here.
  37. FinalOffset - Address to store the result.
  38. Return Value:
  39. None.
  40. --*/
  41. {
  42. ULONG RemainingPageBytes;
  43. ULONG PageOffset;
  44. PAGED_CODE();
  45. DebugTrace( +1, Dbg, "LfsLsnFinalOffset: Entered\n", 0 );
  46. DebugTrace( 0, Dbg, "Lfcb -> %08lx\n", Lfcb );
  47. DebugTrace( 0, Dbg, "Lsn (Low) -> %08lx\n", Lsn.LowPart );
  48. DebugTrace( 0, Dbg, "Lsn (High) -> %08lx\n", Lsn.HighPart );
  49. DebugTrace( 0, Dbg, "DataLength -> %08lx\n", DataLength );
  50. //
  51. // We compute the starting log page file offset, the number of bytes
  52. // remaining in the current log page and the position on this page
  53. // before any data bytes.
  54. //
  55. LfsTruncateLsnToLogPage( Lfcb, Lsn, FinalOffset );
  56. PageOffset = LfsLsnToPageOffset( Lfcb, Lsn );
  57. RemainingPageBytes = (ULONG)Lfcb->LogPageSize - PageOffset;
  58. PageOffset -= 1;
  59. //
  60. // Add the length of the header.
  61. //
  62. DataLength += Lfcb->RecordHeaderLength;
  63. //
  64. // If this Lsn is contained in this log page we are done.
  65. // Otherwise we need to walk through several log pages.
  66. //
  67. if (DataLength > RemainingPageBytes) {
  68. DataLength -= RemainingPageBytes;
  69. RemainingPageBytes = (ULONG)Lfcb->LogPageDataSize;
  70. PageOffset = (ULONG)Lfcb->LogPageDataOffset - 1;
  71. while (TRUE) {
  72. BOOLEAN Wrapped;
  73. LfsNextLogPageOffset( Lfcb, *FinalOffset, FinalOffset, &Wrapped );
  74. //
  75. // We are done if the remaining bytes fit on this page.
  76. //
  77. if (DataLength <= RemainingPageBytes) {
  78. break;
  79. }
  80. DataLength -= RemainingPageBytes;
  81. }
  82. }
  83. //
  84. // We add the remaining bytes to our starting position on this page
  85. // and then add that value to the file offset of this log page.
  86. //
  87. *(PULONG)FinalOffset += (DataLength + PageOffset);
  88. DebugTrace( 0, Dbg, "FinalOffset (Low) -> %08lx\n", LogPageFileOffset.LowPart );
  89. DebugTrace( 0, Dbg, "FinalOffset (High) -> %08lx\n", LogPageFileOffset.HighPart );
  90. DebugTrace( -1, Dbg, "LfsLsnFinalOffset: Exit\n", 0 );
  91. return;
  92. }
  93. BOOLEAN
  94. LfsFindNextLsn (
  95. IN PLFCB Lfcb,
  96. IN PLFS_RECORD_HEADER RecordHeader,
  97. OUT PLSN Lsn
  98. )
  99. /*++
  100. Routine Description:
  101. This routine takes as a starting point the log record header of an
  102. Lsn in the log file. It searches for the next Lsn in the file and
  103. returns that value in the 'Lsn' argument. The boolean return value
  104. indicates whether there is another Lsn in the file.
  105. Arguments:
  106. Lfcb - This is the file control block for the log file.
  107. RecordHeader - This is the log record for the Lsn starting point.
  108. Lsn - This supplies the address to store the next Lsn, if found.
  109. Return Value:
  110. BOOLEAN - Indicates whether the next Lsn was found.
  111. --*/
  112. {
  113. BOOLEAN FoundNextLsn;
  114. LONGLONG LsnOffset;
  115. LONGLONG EndOfLogRecord;
  116. LONGLONG LogHeaderOffset;
  117. LONGLONG SequenceNumber;
  118. PLFS_RECORD_PAGE_HEADER LogRecordPage;
  119. PBCB LogRecordPageBcb;
  120. BOOLEAN UsaError;
  121. PAGED_CODE();
  122. DebugTrace( +1, Dbg, "LfsFindNextLsn: Entered\n", 0 );
  123. DebugTrace( 0, Dbg, "Lfcb -> %08lx\n", Lfcb );
  124. DebugTrace( 0, Dbg, "Record Header -> %08lx\n", RecordHeader );
  125. LogRecordPageBcb = NULL;
  126. FoundNextLsn = FALSE;
  127. //
  128. // Use a try-finally to facilitate cleanup.
  129. //
  130. try {
  131. //
  132. // Find the file offset of the log page which contains the end
  133. // of the log record for this Lsn.
  134. //
  135. LsnOffset = LfsLsnToFileOffset( Lfcb, RecordHeader->ThisLsn );
  136. LfsLsnFinalOffset( Lfcb,
  137. RecordHeader->ThisLsn,
  138. RecordHeader->ClientDataLength,
  139. &EndOfLogRecord );
  140. LfsTruncateOffsetToLogPage( Lfcb, EndOfLogRecord, &LogHeaderOffset );
  141. //
  142. // Remember the sequence number for this page.
  143. //
  144. SequenceNumber = LfsLsnToSeqNumber( Lfcb, RecordHeader->ThisLsn );
  145. //
  146. // Remember if we wrapped.
  147. //
  148. if ( EndOfLogRecord <= LsnOffset ) { //**** xxLeq( EndOfLogRecord, LsnOffset )
  149. SequenceNumber = SequenceNumber + 1; //**** xxAdd( SequenceNumber, LfsLi1 );
  150. }
  151. //
  152. // Pin the log page header for this page.
  153. //
  154. LfsPinOrMapData( Lfcb,
  155. LogHeaderOffset,
  156. (ULONG)Lfcb->LogPageSize,
  157. FALSE,
  158. FALSE,
  159. FALSE,
  160. &UsaError,
  161. (PVOID *)&LogRecordPage,
  162. &LogRecordPageBcb );
  163. //
  164. // If the Lsn we were given was not the last Lsn on this page, then
  165. // the starting offset for the next Lsn is on a quad word boundary
  166. // following the last file offset for the current Lsn. Otherwise
  167. // the file offset is the start of the data on the next page.
  168. //
  169. if ( RecordHeader->ThisLsn.QuadPart == LogRecordPage->Copy.LastLsn.QuadPart ) { //**** xxEql( RecordHeader->ThisLsn, LogRecordPage->Copy.LastLsn )
  170. BOOLEAN Wrapped;
  171. LfsNextLogPageOffset( Lfcb,
  172. LogHeaderOffset,
  173. &LogHeaderOffset,
  174. &Wrapped );
  175. LsnOffset = LogHeaderOffset + Lfcb->LogPageDataOffset; //**** xxAdd( LogHeaderOffset, Lfcb->LogPageDataOffset );
  176. //
  177. // If we wrapped, we need to increment the sequence number.
  178. //
  179. if (Wrapped) {
  180. SequenceNumber = SequenceNumber + 1; //**** xxAdd( SequenceNumber, LfsLi1 );
  181. }
  182. } else {
  183. LiQuadAlign( EndOfLogRecord, &LsnOffset );
  184. }
  185. //
  186. // Compute the Lsn based on the file offset and the sequence count.
  187. //
  188. Lsn->QuadPart = LfsFileOffsetToLsn( Lfcb, LsnOffset, SequenceNumber );
  189. //
  190. // If this Lsn is within the legal range for the file, we return TRUE.
  191. // Otherwise FALSE indicates that there are no more Lsn's.
  192. //
  193. if (LfsIsLsnInFile( Lfcb, *Lsn )) {
  194. FoundNextLsn = TRUE;
  195. }
  196. } finally {
  197. DebugUnwind( LfsFindNextLsn );
  198. //
  199. // Unpin the log page header if held.
  200. //
  201. if (LogRecordPageBcb != NULL) {
  202. CcUnpinData( LogRecordPageBcb );
  203. }
  204. DebugTrace( 0, Dbg, "Lsn (Low) -> %08lx\n", Lsn->LowPart );
  205. DebugTrace( 0, Dbg, "Lsn (High) -> %08lx\n", Lsn->HighPart );
  206. DebugTrace( -1, Dbg, "LfsFindNextLsn: Exit -> %08x\n", FoundNextLsn );
  207. }
  208. return FoundNextLsn;
  209. }