2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/io/cleanup.c
6 * PROGRAMMER: David Welch (welch@mcmail.com)
11 /* INCLUDES ****************************************************************/
13 #include <ddk/ntddk.h>
14 #include <internal/io.h>
16 #include <internal/string.h>
17 #include <internal/ob.h>
20 #include <internal/debug.h>
22 /* FUNCTIONS ***************************************************************/
25 VOID
IoDeviceControlCompletion(PDEVICE_OBJECT DeviceObject
,
27 PIO_STACK_LOCATION IoStack
)
31 IoControlCode
= IoStack
->Parameters
.DeviceIoControl
.IoControlCode
;
33 switch (IO_METHOD_FROM_CTL_CODE(IoControlCode
))
36 DPRINT ("Using METHOD_BUFFERED!\n");
38 /* copy output buffer back and free it */
39 if (Irp
->AssociatedIrp
.SystemBuffer
)
41 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
)
43 RtlCopyMemory(Irp
->UserBuffer
,
44 Irp
->AssociatedIrp
.SystemBuffer
,
45 IoStack
->Parameters
.DeviceIoControl
.
48 ExFreePool (Irp
->AssociatedIrp
.SystemBuffer
);
52 case METHOD_IN_DIRECT
:
53 DPRINT ("Using METHOD_IN_DIRECT!\n");
55 /* free input buffer (control buffer) */
56 if (Irp
->AssociatedIrp
.SystemBuffer
)
57 ExFreePool (Irp
->AssociatedIrp
.SystemBuffer
);
59 /* free output buffer (data transfer buffer) */
61 IoFreeMdl (Irp
->MdlAddress
);
64 case METHOD_OUT_DIRECT
:
65 DPRINT ("Using METHOD_OUT_DIRECT!\n");
67 /* free input buffer (control buffer) */
68 if (Irp
->AssociatedIrp
.SystemBuffer
)
69 ExFreePool (Irp
->AssociatedIrp
.SystemBuffer
);
71 /* free output buffer (data transfer buffer) */
73 IoFreeMdl (Irp
->MdlAddress
);
77 DPRINT ("Using METHOD_NEITHER!\n");
83 VOID
IoReadWriteCompletion(PDEVICE_OBJECT DeviceObject
,
85 PIO_STACK_LOCATION IoStack
)
87 PFILE_OBJECT FileObject
;
89 FileObject
= IoStack
->FileObject
;
91 if (DeviceObject
->Flags
& DO_BUFFERED_IO
)
93 if (IoStack
->MajorFunction
== IRP_MJ_READ
)
95 DPRINT("Copying buffered io back to user\n");
96 memcpy(Irp
->UserBuffer
,Irp
->AssociatedIrp
.SystemBuffer
,
97 IoStack
->Parameters
.Read
.Length
);
99 ExFreePool(Irp
->AssociatedIrp
.SystemBuffer
);
101 if (DeviceObject
->Flags
& DO_DIRECT_IO
)
103 DPRINT("Tearing down MDL\n");
104 if (Irp
->MdlAddress
->MappedSystemVa
!=NULL
)
106 MmUnmapLockedPages(Irp
->MdlAddress
->MappedSystemVa
,
109 MmUnlockPages(Irp
->MdlAddress
);
110 ExFreePool(Irp
->MdlAddress
);
112 if (FileObject
!= NULL
)
114 FileObject
->CurrentByteOffset
.u
.LowPart
=
115 FileObject
->CurrentByteOffset
.u
.LowPart
+
116 Irp
->IoStatus
.Information
;
120 VOID
IoVolumeInformationCompletion(PDEVICE_OBJECT DeviceObject
,
122 PIO_STACK_LOCATION IoStack
)
126 VOID
IoSecondStageCompletion(PIRP Irp
, CCHAR PriorityBoost
)
128 * FUNCTION: Performs the second stage of irp completion for read/write irps
130 * Irp = Irp to completion
131 * FromDevice = True if the operation transfered data from the device
134 PIO_STACK_LOCATION IoStack
;
135 PDEVICE_OBJECT DeviceObject
;
136 PFILE_OBJECT FileObject
;
138 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
140 DeviceObject
= IoStack
->DeviceObject
;
142 switch (IoStack
->MajorFunction
)
145 case IRP_MJ_FLUSH_BUFFERS
:
151 IoReadWriteCompletion(DeviceObject
,Irp
,IoStack
);
154 case IRP_MJ_DEVICE_CONTROL
:
155 case IRP_MJ_INTERNAL_DEVICE_CONTROL
:
156 IoDeviceControlCompletion(DeviceObject
, Irp
, IoStack
);
159 case IRP_MJ_QUERY_VOLUME_INFORMATION
:
160 case IRP_MJ_SET_VOLUME_INFORMATION
:
161 IoVolumeInformationCompletion(DeviceObject
, Irp
, IoStack
);
167 if (Irp
->UserIosb
!=NULL
)
169 *Irp
->UserIosb
=Irp
->IoStatus
;
171 if (Irp
->UserEvent
!=NULL
)
173 KeSetEvent(Irp
->UserEvent
,PriorityBoost
,FALSE
);
176 FileObject
= IoStack
->FileObject
;
178 if (FileObject
!= NULL
&& IoStack
->MajorFunction
!= IRP_MJ_CLOSE
)
180 ObDereferenceObject(FileObject
);