2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/io/irp.c
6 * PROGRAMMER: David Welch (welch@mcmail.com)
11 /* NOTES *******************************************************************
30 /* INCLUDES ****************************************************************/
32 #include <ddk/ntddk.h>
34 #include <internal/string.h>
35 #include <internal/io.h>
38 #include <internal/debug.h>
40 /* FUNCTIONS ****************************************************************/
42 PDEVICE_OBJECT
IoGetDeviceToVerify(PETHREAD Thread
)
44 * FUNCTION: Returns a pointer to the device, representing a removable-media
45 * device, that is the target of the given thread's I/O request
51 VOID
IoFreeIrp(PIRP Irp
)
53 * FUNCTION: Releases a caller allocated irp
61 PIRP
IoMakeAssociatedIrp(PIRP Irp
, CCHAR StackSize
)
63 * FUNCTION: Allocates and initializes an irp to associated with a master irp
66 * StackSize = Number of stack locations to be allocated in the irp
67 * RETURNS: The irp allocated
72 AssocIrp
= IoAllocateIrp(StackSize
,FALSE
);
76 VOID
IoMarkIrpPending(PIRP Irp
)
78 * FUNCTION: Marks the specified irp, indicating further processing will
79 * be required by other driver routines
84 DPRINT("IoGetCurrentIrpStackLocation(Irp) %x\n",
85 IoGetCurrentIrpStackLocation(Irp
));
86 IoGetCurrentIrpStackLocation(Irp
)->Control
|= SL_PENDING_RETURNED
;
87 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
88 DPRINT("IoGetCurrentIrpStackLocation(Irp)->Control %x\n",
89 IoGetCurrentIrpStackLocation(Irp
)->Control
);
90 DPRINT("SL_PENDING_RETURNED %x\n",SL_PENDING_RETURNED
);
93 USHORT
IoSizeOfIrp(CCHAR StackSize
)
95 * FUNCTION: Determines the size of an IRP
97 * StackSize = number of stack locations in the IRP
98 * RETURNS: The size of the IRP in bytes
101 return(sizeof(IRP
)+((StackSize
-1)*sizeof(IO_STACK_LOCATION
)));
104 VOID
IoInitializeIrp(PIRP Irp
, USHORT PacketSize
, CCHAR StackSize
)
106 * FUNCTION: Initalizes an irp allocated by the caller
108 * Irp = IRP to initalize
109 * PacketSize = Size in bytes of the IRP
110 * StackSize = Number of stack locations in the IRP
115 memset(Irp
,0,PacketSize
);
116 Irp
->StackCount
=StackSize
;
117 Irp
->CurrentLocation
=StackSize
;
118 Irp
->Tail
.Overlay
.CurrentStackLocation
=IoGetCurrentIrpStackLocation(Irp
);
121 PIO_STACK_LOCATION
IoGetCurrentIrpStackLocation(PIRP Irp
)
123 * FUNCTION: Gets a pointer to the callers location in the I/O stack in
126 * Irp = Points to the IRP
127 * RETURNS: A pointer to the stack location
130 DPRINT("IoGetCurrentIrpStackLocation: Irp %08lx CurLoc %d StkCnt %d\n",
132 Irp
->CurrentLocation
,
135 return &Irp
->Stack
[(ULONG
)Irp
->CurrentLocation
];
139 VOID
IoSetNextIrpStackLocation(PIRP Irp
)
141 Irp
->CurrentLocation
--;
142 Irp
->Tail
.Overlay
.CurrentStackLocation
--;
145 PIO_STACK_LOCATION
IoGetNextIrpStackLocation(PIRP Irp
)
147 * FUNCTION: Gives a higher level driver access to the next lower driver's
150 * Irp = points to the irp
151 * RETURNS: A pointer to the stack location
154 DPRINT("IoGetNextIrpStackLocation(Irp %x)\n",Irp
);
157 DPRINT("Irp %x Irp->StackPtr %x\n",Irp
,Irp
->CurrentLocation
);
158 return(&Irp
->Stack
[Irp
->CurrentLocation
-1]);
161 NTSTATUS
IoCallDriver(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
163 * FUNCTION: Sends an IRP to the next lower driver
167 PDRIVER_OBJECT DriverObject
;
168 PIO_STACK_LOCATION param
;
170 DPRINT("IoCallDriver(DeviceObject %x, Irp %x)\n",DeviceObject
,Irp
);
172 DriverObject
= DeviceObject
->DriverObject
;
173 param
= IoGetNextIrpStackLocation(Irp
);
175 Irp
->Tail
.Overlay
.CurrentStackLocation
--;
176 Irp
->CurrentLocation
--;
178 DPRINT("MajorFunction %d\n",MajorFunction
);
179 DPRINT("DriverObject->MajorFunction[param->MajorFunction] %x\n",
180 DriverObject
->MajorFunction
[param
->MajorFunction
]);
181 Status
= DriverObject
->MajorFunction
[param
->MajorFunction
](DeviceObject
,
186 PIRP
IoAllocateIrp(CCHAR StackSize
, BOOLEAN ChargeQuota
)
188 * FUNCTION: Allocates an IRP
190 * StackSize = the size of the stack required for the irp
191 * ChargeQuota = Charge allocation to current threads quota
192 * RETURNS: Irp allocated
198 DbgPrint("IoAllocateIrp(StackSize %d ChargeQuota %d)\n",
201 KeDumpStackFrames(0,8);
206 // Irp = ExAllocatePoolWithQuota(NonPagedPool,IoSizeOfIrp(StackSize));
207 Irp
= ExAllocatePool(NonPagedPool
,IoSizeOfIrp(StackSize
));
211 Irp
= ExAllocatePool(NonPagedPool
,IoSizeOfIrp(StackSize
));
219 IoInitializeIrp(Irp
, IoSizeOfIrp(StackSize
), StackSize
);
221 DPRINT("Irp %x Irp->StackPtr %d\n", Irp
, Irp
->CurrentLocation
);
226 VOID
IoSetCompletionRoutine(PIRP Irp
,
227 PIO_COMPLETION_ROUTINE CompletionRoutine
,
229 BOOLEAN InvokeOnSuccess
,
230 BOOLEAN InvokeOnError
,
231 BOOLEAN InvokeOnCancel
)
233 IO_STACK_LOCATION
* param
= IoGetNextIrpStackLocation(Irp
);
235 param
->CompletionRoutine
=CompletionRoutine
;
236 param
->CompletionContext
=Context
;
239 param
->Control
= param
->Control
| SL_INVOKE_ON_SUCCESS
;
243 param
->Control
= param
->Control
| SL_INVOKE_ON_ERROR
;
247 param
->Control
= param
->Control
| SL_INVOKE_ON_CANCEL
;
251 VOID
IopCompleteRequest(struct _KAPC
* Apc
,
252 PKNORMAL_ROUTINE
* NormalRoutine
,
253 PVOID
* NormalContext
,
254 PVOID
* SystemArgument1
,
255 PVOID
* SystemArgument2
)
257 IoSecondStageCompletion((PIRP
)(*NormalContext
),
261 VOID
IoCompleteRequest(PIRP Irp
, CCHAR PriorityBoost
)
263 * FUNCTION: Indicates the caller has finished all processing for a given
264 * I/O request and is returning the given IRP to the I/O manager
266 * Irp = Irp to be cancelled
267 * PriorityBoost = Increment by which to boost the priority of the
268 * thread making the request
274 DPRINT("IoCompleteRequest(Irp %x, PriorityBoost %d)\n",
277 for (i
=0;i
<Irp
->StackCount
;i
++)
279 if (Irp
->Stack
[i
].CompletionRoutine
!= NULL
)
281 Status
= Irp
->Stack
[i
].CompletionRoutine(
282 Irp
->Stack
[i
].DeviceObject
,
284 Irp
->Stack
[i
].CompletionContext
);
285 if (Status
== STATUS_MORE_PROCESSING_REQUIRED
)
290 if (Irp
->Stack
[i
].Control
& SL_PENDING_RETURNED
)
292 Irp
->PendingReturned
= TRUE
;
296 if (Irp
->PendingReturned
)
298 KeInitializeApc(&Irp
->Tail
.Apc
,
299 &Irp
->Tail
.Overlay
.Thread
->Tcb
,
306 KeInsertQueueApc(&Irp
->Tail
.Apc
,NULL
,NULL
,0);
310 IoSecondStageCompletion(Irp
,PriorityBoost
);