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.

311 lines
9.3 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. ReadSup.c
  5. Abstract:
  6. This module implements the Read support routine. This is a common
  7. read function that is called to do read, unbuffered read, peek, and
  8. transceive.
  9. Author:
  10. Gary Kimura [GaryKi] 20-Sep-1990
  11. Revision History:
  12. --*/
  13. #include "NpProcs.h"
  14. //
  15. // The debug trace level
  16. //
  17. #define Dbg (DEBUG_TRACE_READSUP)
  18. #ifdef ALLOC_PRAGMA
  19. #pragma alloc_text(PAGE, NpReadDataQueue)
  20. #endif
  21. IO_STATUS_BLOCK
  22. NpReadDataQueue (
  23. IN PDATA_QUEUE ReadQueue,
  24. IN BOOLEAN PeekOperation,
  25. IN BOOLEAN ReadOverflowOperation,
  26. IN PUCHAR ReadBuffer,
  27. IN ULONG ReadLength,
  28. IN READ_MODE ReadMode,
  29. IN PCCB Ccb,
  30. IN PLIST_ENTRY DeferredList
  31. )
  32. /*++
  33. Routine Description:
  34. This procedure reads data from the read queue and fills up the
  35. read buffer. It will also dequeue the queue or leave it alone based
  36. on an input parameter.
  37. Arguments:
  38. ReadQueue - Provides the read queue to examine. Its state must
  39. already be set to WriteEntries.
  40. PeekOperation - Indicates if the operation is to dequeue information
  41. off of the queue as it is being read or leave the queue alone.
  42. TRUE means to leave the queue alone.
  43. ReadOverflowOperation - Indicates if this is a read overflow operation.
  44. With read overflow we will not alter the named pipe if the data
  45. will overflow the read buffer.
  46. ReadBuffer - Supplies a buffer to receive the data
  47. ReadLength - Supplies the length, in bytes, of ReadBuffer.
  48. ReadMode - Indicates if the read operation is message mode or
  49. byte stream mode.
  50. NamedPipeEnd - Supplies the end of the named pipe doing the read
  51. Ccb - Supplies the ccb for the pipe
  52. DeferredList - List of IRP's to complete later after we drop locks
  53. Return Value:
  54. IO_STATUS_BLOCK - Indicates the result of the operation.
  55. --*/
  56. {
  57. IO_STATUS_BLOCK Iosb= {0};
  58. PDATA_ENTRY DataEntry;
  59. ULONG ReadRemaining;
  60. ULONG AmountRead;
  61. PUCHAR WriteBuffer;
  62. ULONG WriteLength;
  63. ULONG WriteRemaining;
  64. BOOLEAN StartStalled = FALSE;
  65. ULONG AmountToCopy;
  66. PAGED_CODE();
  67. DebugTrace(+1, Dbg, "NpReadDataQueue\n", 0);
  68. DebugTrace( 0, Dbg, "ReadQueue = %08lx\n", ReadQueue);
  69. DebugTrace( 0, Dbg, "PeekOperation = %08lx\n", PeekOperation);
  70. DebugTrace( 0, Dbg, "ReadOverflowOperation = %08lx\n", ReadOverflowOperation);
  71. DebugTrace( 0, Dbg, "ReadBuffer = %08lx\n", ReadBuffer);
  72. DebugTrace( 0, Dbg, "ReadLength = %08lx\n", ReadLength);
  73. DebugTrace( 0, Dbg, "ReadMode = %08lx\n", ReadMode);
  74. DebugTrace( 0, Dbg, "Ccb = %08lx\n", Ccb);
  75. //
  76. // If this is an overflow operation then we will force us to do peeks.
  77. // Later when are determine that the opreation succeeded we will complete
  78. // the write irp.
  79. //
  80. if (ReadOverflowOperation) {
  81. PeekOperation = TRUE;
  82. }
  83. //
  84. // Now for every real data entry we loop until either we run out
  85. // of data entries or until the read buffer is full
  86. //
  87. ReadRemaining = ReadLength;
  88. Iosb.Status = STATUS_SUCCESS;
  89. Iosb.Information = 0;
  90. AmountRead = 0;
  91. for (DataEntry = (PeekOperation ? NpGetNextDataQueueEntry( ReadQueue, NULL )
  92. : NpGetNextRealDataQueueEntry( ReadQueue, DeferredList ));
  93. (DataEntry != (PDATA_ENTRY) &ReadQueue->Queue) && (ReadRemaining > 0);
  94. DataEntry = (PeekOperation ? NpGetNextDataQueueEntry( ReadQueue, DataEntry )
  95. : NpGetNextRealDataQueueEntry( ReadQueue, DeferredList ))) {
  96. DebugTrace(0, Dbg, "Top of Loop\n", 0);
  97. DebugTrace(0, Dbg, "ReadRemaining = %08lx\n", ReadRemaining);
  98. //
  99. // If this is a peek operation then make sure we got a real
  100. // data entry and not a close or flush
  101. //
  102. if (!PeekOperation ||
  103. (DataEntry->DataEntryType == Buffered) ||
  104. (DataEntry->DataEntryType == Unbuffered)) {
  105. //
  106. // Calculate how much data is in this entry. The write
  107. // remaining is based on whether this is the first entry
  108. // in the queue or a later data entry
  109. //
  110. if (DataEntry->DataEntryType == Unbuffered) {
  111. WriteBuffer = DataEntry->Irp->AssociatedIrp.SystemBuffer;
  112. } else {
  113. WriteBuffer = DataEntry->DataBuffer;
  114. }
  115. WriteLength = DataEntry->DataSize;
  116. WriteRemaining = WriteLength;
  117. if (DataEntry == NpGetNextDataQueueEntry( ReadQueue, NULL )) {
  118. WriteRemaining -= ReadQueue->NextByteOffset;
  119. }
  120. DebugTrace(0, Dbg, "WriteBuffer = %08lx\n", WriteBuffer);
  121. DebugTrace(0, Dbg, "WriteLength = %08lx\n", WriteLength);
  122. DebugTrace(0, Dbg, "WriteRemaining = %08lx\n", WriteRemaining);
  123. //
  124. // copy data from the write buffer at write offset to the
  125. // read buffer at read offset by the mininum of write
  126. // remaining or read remaining
  127. //
  128. AmountToCopy = (WriteRemaining < ReadRemaining ? WriteRemaining
  129. : ReadRemaining);
  130. try {
  131. RtlCopyMemory( &ReadBuffer[ ReadLength - ReadRemaining ],
  132. &WriteBuffer[ WriteLength - WriteRemaining ],
  133. AmountToCopy );
  134. } except(EXCEPTION_EXECUTE_HANDLER) {
  135. Iosb.Status = GetExceptionCode ();
  136. goto exit_1;
  137. }
  138. //
  139. // Update the Read and Write remaining counts, the total
  140. // amount we've read and the next byte offset field in the
  141. // read queue
  142. //
  143. ReadRemaining -= AmountToCopy;
  144. WriteRemaining -= AmountToCopy;
  145. AmountRead += AmountToCopy;
  146. if (!PeekOperation) {
  147. DataEntry->QuotaCharged -= AmountToCopy;
  148. ReadQueue->QuotaUsed -= AmountToCopy;
  149. ReadQueue->NextByteOffset += AmountToCopy;
  150. StartStalled = TRUE;
  151. }
  152. //
  153. // Now update the security fields in the ccb
  154. //
  155. NpCopyClientContext( Ccb, DataEntry );
  156. //
  157. // If the remaining write length is greater than zero
  158. // then we've filled up the read buffer so we need to
  159. // figure out if its an overflow error
  160. //
  161. if (WriteRemaining > 0 ||
  162. (ReadOverflowOperation && (AmountRead == 0))) {
  163. DebugTrace(0, Dbg, "Write remaining is > 0\n", 0);
  164. if (ReadMode == FILE_PIPE_MESSAGE_MODE) {
  165. DebugTrace(0, Dbg, "Overflow message mode read\n", 0);
  166. //
  167. // Set the status field and break out of the for-loop.
  168. //
  169. Iosb.Status = STATUS_BUFFER_OVERFLOW;
  170. break;
  171. }
  172. } else {
  173. DebugTrace(0, Dbg, "Remaining Write is zero\n", 0);
  174. //
  175. // The write entry is done so remove it from the read
  176. // queue, if this is not a peek operation. This might
  177. // also have an Irp that needs to be completed
  178. //
  179. if (!PeekOperation || ReadOverflowOperation) {
  180. PIRP WriteIrp;
  181. //
  182. // For a read overflow operation we need to get the read data
  183. // queue entry and remove it.
  184. //
  185. if (ReadOverflowOperation) {
  186. PDATA_ENTRY TempDataEntry;
  187. TempDataEntry = NpGetNextRealDataQueueEntry( ReadQueue, DeferredList );
  188. ASSERT(TempDataEntry == DataEntry);
  189. }
  190. if ((WriteIrp = NpRemoveDataQueueEntry( ReadQueue, TRUE, DeferredList)) != NULL) {
  191. WriteIrp->IoStatus.Information = WriteLength;
  192. NpDeferredCompleteRequest( WriteIrp, STATUS_SUCCESS, DeferredList );
  193. }
  194. }
  195. //
  196. // And if we are doing message mode reads then we'll
  197. // work on completing this irp without going back
  198. // to the top of the loop
  199. //
  200. if (ReadMode == FILE_PIPE_MESSAGE_MODE) {
  201. DebugTrace(0, Dbg, "Successful message mode read\n", 0);
  202. //
  203. // Set the status field and break out of the for-loop.
  204. //
  205. Iosb.Status = STATUS_SUCCESS;
  206. break;
  207. }
  208. ASSERTMSG("Srv cannot use read overflow on a byte stream pipe ", !ReadOverflowOperation);
  209. }
  210. }
  211. }
  212. DebugTrace(0, Dbg, "End of loop, AmountRead = %08lx\n", AmountRead);
  213. Iosb.Information = AmountRead;
  214. exit_1:
  215. if (StartStalled) {
  216. NpCompleteStalledWrites (ReadQueue, DeferredList);
  217. }
  218. DebugTrace(-1, Dbg, "NpReadDataQueue -> Iosb.Status = %08lx\n", Iosb.Status);
  219. return Iosb;
  220. }