+IoFreeIrp()
[reactos.git] / ntoskrnl / io / irp.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS kernel
5  * FILE:            ntoskrnl/io/irp.c
6  * PURPOSE:         Handle IRPs
7  * PROGRAMMER:      David Welch (welch@mcmail.com)
8  * UPDATE HISTORY: 
9  *                  24/05/98: Created 
10  */
11
12 /* NOTES *******************************************************************
13  * 
14  * Layout of an IRP 
15  * 
16  *             ################
17  *             #    Headers   #
18  *             ################
19  *             #              #
20  *             #   Variable   #
21  *             # length list  #
22  *             # of io stack  #
23  *             #  locations   #
24  *             #              #
25  *             ################
26  * 
27  * 
28  * 
29  */
30
31 /* INCLUDES ****************************************************************/
32
33 #include <ddk/ntddk.h>
34 #include <internal/io.h>
35 #include <internal/ps.h>
36 #include <internal/pool.h>
37
38 #define NDEBUG
39 #include <internal/debug.h>
40
41 /* GLOBALS *******************************************************************/
42
43 #define TAG_IRP     TAG('I', 'R', 'P', ' ')
44
45
46 /* FUNCTIONS ****************************************************************/
47
48
49 VOID STDCALL
50 IoFreeIrp(PIRP Irp)
51 /*
52  * FUNCTION: Releases a caller allocated irp
53  * ARGUMENTS:
54  *      Irp = Irp to free
55  */
56 {
57   ExFreePool(Irp);
58 }
59
60 #ifndef LIBCAPTIVE
61
62 PIRP STDCALL
63 IoMakeAssociatedIrp(PIRP Irp,
64                     CCHAR StackSize)
65 /*
66  * FUNCTION: Allocates and initializes an irp to associated with a master irp
67  * ARGUMENTS:
68  *       Irp = Master irp
69  *       StackSize = Number of stack locations to be allocated in the irp
70  * RETURNS: The irp allocated
71  */
72 {
73   PIRP AssocIrp;
74   
75   AssocIrp = IoAllocateIrp(StackSize,FALSE);
76   UNIMPLEMENTED;
77 }
78
79 #endif /* LIBCAPTIVE */
80
81 VOID STDCALL
82 IoInitializeIrp(PIRP Irp,
83                 USHORT PacketSize,
84                 CCHAR StackSize)
85 /*
86  * FUNCTION: Initalizes an irp allocated by the caller
87  * ARGUMENTS:
88  *          Irp = IRP to initalize
89  *          PacketSize = Size in bytes of the IRP
90  *          StackSize = Number of stack locations in the IRP
91  */
92 {
93   assert(Irp != NULL);
94
95   memset(Irp, 0, PacketSize);
96   Irp->Size = PacketSize;
97   Irp->StackCount = StackSize;
98   Irp->CurrentLocation = StackSize;
99   Irp->Tail.Overlay.CurrentStackLocation = &Irp->Stack[(ULONG)StackSize];
100 }
101
102
103 NTSTATUS FASTCALL
104 IofCallDriver(PDEVICE_OBJECT DeviceObject,
105               PIRP Irp)
106 /*
107   * FUNCTION: Sends an IRP to the next lower driver
108  */
109 {
110   NTSTATUS Status;
111   PDRIVER_OBJECT DriverObject;
112   PIO_STACK_LOCATION Param;
113   
114   DPRINT("IofCallDriver(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
115   
116   assert(Irp);
117   assert(DeviceObject);
118
119   DriverObject = DeviceObject->DriverObject;
120
121   assert(DriverObject);
122
123   Param = IoGetNextIrpStackLocation(Irp);
124
125   DPRINT("IrpSp 0x%X\n", Param);
126   
127   Irp->Tail.Overlay.CurrentStackLocation--;
128   Irp->CurrentLocation--;
129   
130   DPRINT("MajorFunction %d\n", Param->MajorFunction);
131   DPRINT("DriverObject->MajorFunction[Param->MajorFunction] %x\n",
132          DriverObject->MajorFunction[Param->MajorFunction]);
133   Status = DriverObject->MajorFunction[Param->MajorFunction](DeviceObject,
134                                                              Irp);
135
136   return(Status);
137 }
138
139
140 NTSTATUS
141 STDCALL
142 IoCallDriver (PDEVICE_OBJECT DeviceObject, PIRP Irp)
143 {
144   return(IofCallDriver(DeviceObject,
145                        Irp));
146 }
147
148
149 PIRP STDCALL
150 IoAllocateIrp(CCHAR StackSize,
151               BOOLEAN ChargeQuota)
152 /*
153  * FUNCTION: Allocates an IRP
154  * ARGUMENTS:
155  *          StackSize = the size of the stack required for the irp
156  *          ChargeQuota = Charge allocation to current threads quota
157  * RETURNS: Irp allocated
158  */
159 {
160   PIRP Irp;
161
162 #if 0
163   DbgPrint("IoAllocateIrp(StackSize %d ChargeQuota %d)\n",
164            StackSize,
165            ChargeQuota);
166   KeDumpStackFrames(0,8);
167 #endif
168   
169   if (ChargeQuota)
170     {
171 //      Irp = ExAllocatePoolWithQuota(NonPagedPool,IoSizeOfIrp(StackSize));
172       Irp = ExAllocatePoolWithTag(NonPagedPool,
173                                   IoSizeOfIrp(StackSize),
174                                   TAG_IRP);
175     }
176   else
177     {
178       Irp = ExAllocatePoolWithTag(NonPagedPool,
179                                   IoSizeOfIrp(StackSize),
180                                   TAG_IRP);
181     }
182
183   if (Irp==NULL)
184     {
185       return(NULL);
186     }
187
188   IoInitializeIrp(Irp,
189                   IoSizeOfIrp(StackSize),
190                   StackSize);
191
192 //  DPRINT("Irp %x Irp->StackPtr %d\n", Irp, Irp->CurrentLocation);
193
194   return(Irp);
195 }
196
197 #ifndef LIBCAPTIVE
198
199 VOID STDCALL
200 IopCompleteRequest(struct _KAPC* Apc,
201                    PKNORMAL_ROUTINE* NormalRoutine,
202                    PVOID* NormalContext,
203                    PVOID* SystemArgument1,
204                    PVOID* SystemArgument2)
205 {
206   DPRINT("IopCompleteRequest(Apc %x, SystemArgument1 %x, (*SystemArgument1) %x\n",
207          Apc,
208          SystemArgument1,
209          *SystemArgument1);
210   IoSecondStageCompletion((PIRP)(*SystemArgument1),
211                           (KPRIORITY)(*SystemArgument2));
212 }
213
214
215 VOID FASTCALL
216 IofCompleteRequest(PIRP Irp,
217                    CCHAR PriorityBoost)
218 /*
219  * FUNCTION: Indicates the caller has finished all processing for a given
220  * I/O request and is returning the given IRP to the I/O manager
221  * ARGUMENTS:
222  *         Irp = Irp to be cancelled
223  *         PriorityBoost = Increment by which to boost the priority of the
224  *                         thread making the request
225  */
226 {
227    unsigned int i;
228    NTSTATUS Status;
229    
230    DPRINT("IoCompleteRequest(Irp %x, PriorityBoost %d) Event %x THread %x\n",
231            Irp,PriorityBoost, Irp->UserEvent, PsGetCurrentThread());
232
233    for (i=Irp->CurrentLocation;i<Irp->StackCount;i++)
234    {
235       if (Irp->Stack[i].CompletionRoutine != NULL)
236       {
237          Status = Irp->Stack[i].CompletionRoutine(
238                                              Irp->Stack[i].DeviceObject,
239                                              Irp,
240                                              Irp->Stack[i].CompletionContext);
241          if (Status == STATUS_MORE_PROCESSING_REQUIRED)
242          {
243             return;
244          }
245       }
246       if (Irp->Stack[i].Control & SL_PENDING_RETURNED)
247       {
248          Irp->PendingReturned = TRUE;
249       }
250       if (Irp->CurrentLocation < Irp->StackCount - 1)
251       {
252          IoSkipCurrentIrpStackLocation(Irp);
253       }
254    }
255    if (Irp->PendingReturned)
256      {
257         DPRINT("Dispatching APC\n");
258         KeInitializeApc(&Irp->Tail.Apc,
259                         &Irp->Tail.Overlay.Thread->Tcb,
260                         0,
261                         IopCompleteRequest,
262                         NULL,
263                         (PKNORMAL_ROUTINE)
264                         NULL,
265                         KernelMode,
266                         NULL);
267         KeInsertQueueApc(&Irp->Tail.Apc,
268                          (PVOID)Irp,
269                          (PVOID)(ULONG)PriorityBoost,
270                          KernelMode);
271         DPRINT("Finished dispatching APC\n");
272      }
273    else
274      {
275         DPRINT("Calling completion routine directly\n");
276         IoSecondStageCompletion(Irp,PriorityBoost);
277         DPRINT("Finished completition routine\n");
278      }
279 }
280
281
282 VOID STDCALL
283 IoCompleteRequest(PIRP Irp,
284                   CCHAR PriorityBoost)
285 {
286   IofCompleteRequest(Irp,
287                      PriorityBoost);
288 }
289
290
291 /**********************************************************************
292  * NAME                                                 EXPORTED
293  *      IoIsOperationSynchronous@4
294  *
295  * DESCRIPTION
296  *      Check if the I/O operation associated with the given IRP
297  *      is synchronous.
298  *
299  * ARGUMENTS
300  *      Irp     Packet to check.
301  *
302  * RETURN VALUE
303  *      TRUE if Irp's operation is synchronous; otherwise FALSE.
304  */
305 BOOLEAN STDCALL
306 IoIsOperationSynchronous(IN PIRP Irp)
307 {
308   PFILE_OBJECT FileObject = NULL;
309   ULONG Flags = 0;
310
311   /* Check the FILE_OBJECT's flags first. */
312   FileObject = IoGetCurrentIrpStackLocation(Irp)->FileObject;
313   if (!(FO_SYNCHRONOUS_IO & FileObject->Flags))
314     {
315       /* Check IRP's flags. */
316       Flags = Irp->Flags;
317       if (!((IRP_SYNCHRONOUS_API | IRP_SYNCHRONOUS_PAGING_IO) & Flags))
318         {
319           return(FALSE);
320         }
321     }
322
323   /* Check more IRP's flags. */
324   Flags = Irp->Flags;
325   if (!(IRP_PAGING_IO & Flags)
326       || (IRP_SYNCHRONOUS_PAGING_IO & Flags))
327     {
328       return(TRUE);
329     }
330
331   /* Otherwise, it is an asynchronous operation. */
332   return(FALSE);
333 }
334
335
336 VOID STDCALL
337 IoEnqueueIrp(IN PIRP Irp)
338 {
339   UNIMPLEMENTED;
340 }
341
342 #endif /* LIBCAPTIVE */
343
344 VOID STDCALL
345 IoSetTopLevelIrp(IN PIRP Irp)
346 {
347   PETHREAD Thread;
348
349   Thread = PsGetCurrentThread();
350   Thread->TopLevelIrp->TopLevelIrp = Irp;
351 }
352
353
354 PIRP STDCALL
355 IoGetTopLevelIrp(VOID)
356 {
357   return(PsGetCurrentThread()->TopLevelIrp->TopLevelIrp);
358 }
359
360 #ifndef LIBCAPTIVE
361
362 VOID STDCALL
363 IoQueueThreadIrp(IN PIRP Irp)
364 {
365   UNIMPLEMENTED;
366 }
367
368 /*
369 NTSTATUS
370 STDCALL
371 IoSetDeviceInterfaceState(IN PUNICODE_STRING SymbolicLinkName, IN BOOLEAN Enable)
372 {
373   UNIMPLEMENTED;
374         return 0;
375 }
376
377 NTSTATUS
378 STDCALL
379 IoGetDeviceProperty(
380   IN PDEVICE_OBJECT DeviceObject,
381   IN DEVICE_REGISTRY_PROPERTY DeviceProperty,
382   IN ULONG BufferLength,
383   OUT PVOID PropertyBuffer,
384   OUT PULONG ResultLength)
385 {
386   UNIMPLEMENTED;
387         return 0;
388 }
389
390 NTSTATUS
391 STDCALL
392 IoOpenDeviceRegistryKey(
393   IN PDEVICE_OBJECT DeviceObject,
394   IN ULONG DevInstKeyType,
395   IN ACCESS_MASK DesiredAccess,
396   OUT PHANDLE DevInstRegKey)
397 {
398   UNIMPLEMENTED;
399         return 0;
400 }
401  */
402
403 #endif /* LIBCAPTIVE */
404
405 /* EOF */