Windows NT 4.0 source code leak
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.

258 lines
7.0 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. io.c
  5. Abstract:
  6. This module contains IRP building routines.
  7. Author:
  8. Manny Weiser (mannyw) 13-Jan-1992
  9. Revision History:
  10. --*/
  11. #include "mup.h"
  12. #ifdef ALLOC_PRAGMA
  13. #pragma alloc_text( PAGE, MupBuildIoControlRequest )
  14. #endif
  15. PIRP
  16. MupBuildIoControlRequest (
  17. IN OUT PIRP Irp OPTIONAL,
  18. IN PFILE_OBJECT FileObject OPTIONAL,
  19. IN PVOID Context,
  20. IN UCHAR MajorFunction,
  21. IN ULONG IoControlCode,
  22. IN PVOID MainBuffer,
  23. IN ULONG InputBufferLength,
  24. IN PVOID AuxiliaryBuffer OPTIONAL,
  25. IN ULONG OutputBufferLength,
  26. IN PIO_COMPLETION_ROUTINE CompletionRoutine
  27. )
  28. /*++
  29. Routine Description:
  30. This function builds an I/O request packet for a device or
  31. file system I/O control request.
  32. Arguments:
  33. Irp - Supplies a pointer to an IRP. If NULL, this routine allocates
  34. an IRP and returns its address. Otherwise, it supplies the
  35. address of an IRP allocated by the caller.
  36. FileObject - Supplies a pointer the file object to which this
  37. request is directed. This pointer is copied into the IRP, so
  38. that the called driver can find its file-based context. NOTE
  39. THAT THIS IS NOT A REFERENCED POINTER. The caller must ensure
  40. that the file object is not deleted while the I/O operation is
  41. in progress. The server accomplishes this by incrementing a
  42. reference count in a local block to account for the I/O; the
  43. local block in turn references the file object.
  44. Context - Supplies a PVOID value that is passed to the completion
  45. routine.
  46. MajorFunction - The major function that we are calling. Currently
  47. this most be one of IRP_MJ_FILE_SYSTEM_CONTROL or
  48. IRP_MJ_DEVICE_IO_CONTROL.
  49. IoControlCode - Supplies the control code for the operation.
  50. MainBuffer - Supplies the address of the main buffer. This must
  51. be a system virtual address, and the buffer must be locked in
  52. memory. If ControlCode specifies a method 0 request, the actual
  53. length of the buffer must be the greater of InputBufferLength
  54. and OutputBufferLength.
  55. InputBufferLength - Supplies the length of the input buffer.
  56. AuxiliaryBuffer - Supplies the address of the auxiliary buffer. If the
  57. control code method is 0, this is a buffered I/O buffer, but the
  58. data returned by the called driver in the system buffer is not
  59. automatically copied into the auxiliary buffer. Instead, the
  60. auxiliary data ends up in MainBuffer. If the caller wishes the
  61. data to be in AuxiliaryBuffer, it must copy the data at some point
  62. after the completion routine runs.
  63. If the control code method is 1 or 2, this parameter is ignored;
  64. instead, the Mdl parameter is used to obtain the starting
  65. virtual address of the buffer.
  66. CompletionRoutine - The IO completion routine.
  67. Return Value:
  68. PIRP - Returns a pointer to the constructed IRP. If the Irp
  69. parameter was not NULL on input, the function return value will
  70. be the same value (so it is safe to discard the return value in
  71. this case). It is the responsibility of the calling program to
  72. deallocate the IRP after the I/O request is complete.
  73. --*/
  74. {
  75. CLONG method;
  76. PDEVICE_OBJECT deviceObject;
  77. PIO_STACK_LOCATION irpSp;
  78. ASSERT( MajorFunction == IRP_MJ_DEVICE_CONTROL );
  79. PAGED_CODE();
  80. //
  81. // Get the method with which the buffers are being passed.
  82. //
  83. method = IoControlCode & 3;
  84. //
  85. // Allocate an IRP. The stack size is one higher
  86. // than that of the target device, to allow for the caller's
  87. // completion routine.
  88. //
  89. deviceObject = IoGetRelatedDeviceObject( FileObject );
  90. //
  91. // Get the address of the target device object.
  92. //
  93. Irp = IoAllocateIrp( (CCHAR)(deviceObject->StackSize + 1), FALSE );
  94. if ( Irp == NULL ) {
  95. //
  96. // Unable to allocate an IRP. Inform the caller.
  97. //
  98. return NULL;
  99. }
  100. IoSetNextIrpStackLocation( Irp );
  101. Irp->Tail.Overlay.OriginalFileObject = FileObject;
  102. Irp->Tail.Overlay.Thread = PsGetCurrentThread();
  103. //
  104. // Get a pointer to the current stack location and fill in the
  105. // device object pointer.
  106. //
  107. irpSp = IoGetCurrentIrpStackLocation( Irp );
  108. irpSp->DeviceObject = deviceObject;
  109. //
  110. // Set up the completion routine.
  111. //
  112. IoSetCompletionRoutine(
  113. Irp,
  114. CompletionRoutine,
  115. Context,
  116. TRUE,
  117. TRUE,
  118. TRUE
  119. );
  120. //
  121. // Get a pointer to the next stack location. This one is used to
  122. // hold the parameters for the device I/O control request.
  123. //
  124. irpSp = IoGetNextIrpStackLocation( Irp );
  125. irpSp->MajorFunction = MajorFunction;
  126. irpSp->MinorFunction = 0;
  127. irpSp->FileObject = FileObject;
  128. irpSp->DeviceObject = deviceObject;
  129. //
  130. // Copy the caller's parameters to the service-specific portion of the
  131. // IRP for those parameters that are the same for all three methods.
  132. //
  133. irpSp->Parameters.DeviceIoControl.OutputBufferLength = OutputBufferLength;
  134. irpSp->Parameters.DeviceIoControl.InputBufferLength = InputBufferLength;
  135. irpSp->Parameters.DeviceIoControl.IoControlCode = IoControlCode;
  136. //
  137. // Based on the method by which the buffers are being passed,
  138. // describe a system buffer and optionally build an MDL.
  139. //
  140. switch ( method ) {
  141. case 0:
  142. //
  143. // For this case, InputBuffer must be large enough to contain
  144. // both the input and the output buffers.
  145. //
  146. Irp->MdlAddress = NULL;
  147. Irp->AssociatedIrp.SystemBuffer = MainBuffer;
  148. Irp->UserBuffer = AuxiliaryBuffer;
  149. Irp->Flags = (ULONG)IRP_BUFFERED_IO;
  150. if ( ARGUMENT_PRESENT(AuxiliaryBuffer) ) {
  151. Irp->Flags |= IRP_INPUT_OPERATION;
  152. }
  153. break;
  154. case 1:
  155. case 2:
  156. //
  157. // For these two cases, MainBuffer is the buffered I/O "system
  158. // buffer". Build an MDL for either read or write access,
  159. // depending on the method, for the output buffer.
  160. //
  161. IoAllocateMdl( MainBuffer, InputBufferLength, FALSE, FALSE, Irp );
  162. Irp->AssociatedIrp.SystemBuffer = MainBuffer;
  163. Irp->Flags = (ULONG)IRP_BUFFERED_IO;
  164. //
  165. // An MDL to describe the buffer has not been built. Build
  166. // it now.
  167. //
  168. MmProbeAndLockPages(
  169. Irp->MdlAddress,
  170. KernelMode,
  171. IoReadAccess
  172. );
  173. break;
  174. case 3:
  175. //
  176. // For this case, do nothing. Everything is up to the driver.
  177. // Simply give the driver a copy of the caller's parameters and
  178. // let the driver do everything itself.
  179. //
  180. Irp->MdlAddress = NULL;
  181. Irp->AssociatedIrp.SystemBuffer = NULL;
  182. Irp->UserBuffer = AuxiliaryBuffer;
  183. irpSp->Parameters.DeviceIoControl.Type3InputBuffer = MainBuffer;
  184. break;
  185. }
  186. return Irp;
  187. } // MupBuildIoControlRequest