4aef0c06924a391bc3dd8917e6a01228d8a04b02
[reactos.git] / reactos / ntoskrnl / io / rw.c
1 /* $Id: rw.c,v 1.33 2000/10/22 16:36:50 ekohl Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/io/rw.c
6 * PURPOSE: Implements read/write APIs
7 * PROGRAMMER: David Welch (welch@cwcom.net)
8 * UPDATE HISTORY:
9 * 30/05/98: Created
10 */
11
12 /* INCLUDES ****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/io.h>
16 #include <internal/ob.h>
17
18 #define NDEBUG
19 #include <internal/debug.h>
20
21 /* DATA ********************************************************************/
22
23
24
25 /* FUNCTIONS ***************************************************************/
26
27
28 /**********************************************************************
29 * NAME EXPORTED
30 * NtReadFile
31 *
32 * DESCRIPTION
33 *
34 * ARGUMENTS
35 *
36 * RETURN VALUE
37 *
38 * REVISIONS
39 *
40 */
41 NTSTATUS STDCALL NtReadFile(HANDLE FileHandle,
42 HANDLE EventHandle,
43 PIO_APC_ROUTINE ApcRoutine,
44 PVOID ApcContext,
45 PIO_STATUS_BLOCK IoStatusBlock,
46 PVOID Buffer,
47 ULONG Length,
48 PLARGE_INTEGER ByteOffset,
49 PULONG Key)
50 {
51 NTSTATUS Status;
52 PFILE_OBJECT FileObject;
53 PIRP Irp;
54 PIO_STACK_LOCATION StackPtr;
55 PKEVENT ptrEvent = NULL;
56 KEVENT Event;
57
58 DPRINT("NtReadFile(FileHandle %x Buffer %x Length %x ByteOffset %x, "
59 "IoStatusBlock %x)\n", FileHandle, Buffer, Length, ByteOffset,
60 IoStatusBlock);
61
62 Status = ObReferenceObjectByHandle(FileHandle,
63 FILE_READ_DATA,
64 IoFileObjectType,
65 UserMode,
66 (PVOID*)&FileObject,
67 NULL);
68 if (!NT_SUCCESS(Status))
69 {
70 DPRINT("NtReadFile() = %x\n",Status);
71 return Status;
72 }
73
74 if (ByteOffset == NULL)
75 {
76 ByteOffset = &FileObject->CurrentByteOffset;
77 }
78
79 if (EventHandle != NULL)
80 {
81 Status = ObReferenceObjectByHandle(EventHandle,
82 SYNCHRONIZE,
83 ExEventObjectType,
84 UserMode,
85 (PVOID*)&ptrEvent,
86 NULL);
87 if (!NT_SUCCESS(Status))
88 {
89 ObDereferenceObject(FileObject);
90 return Status;
91 }
92 }
93 else if (FileObject->Flags & FO_SYNCHRONOUS_IO)
94 {
95 KeInitializeEvent(&Event,
96 NotificationEvent,
97 FALSE);
98 ptrEvent = &Event;
99 }
100 else
101 {
102 ptrEvent = NULL;
103 }
104
105 Irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ,
106 FileObject->DeviceObject,
107 Buffer,
108 Length,
109 ByteOffset,
110 ptrEvent,
111 IoStatusBlock);
112
113 Irp->Overlay.AsynchronousParameters.UserApcRoutine = ApcRoutine;
114 Irp->Overlay.AsynchronousParameters.UserApcContext = ApcContext;
115
116 StackPtr = IoGetNextIrpStackLocation(Irp);
117 StackPtr->FileObject = FileObject;
118 if (Key != NULL)
119 {
120 StackPtr->Parameters.Read.Key = *Key;
121 }
122 else
123 {
124 StackPtr->Parameters.Read.Key = 0;
125 }
126
127 Status = IoCallDriver(FileObject->DeviceObject,
128 Irp);
129 if ((Status == STATUS_PENDING) &&
130 (FileObject->Flags & FO_SYNCHRONOUS_IO))
131 {
132 BOOLEAN Alertable;
133
134 if (FileObject->Flags & FO_ALERTABLE_IO)
135 {
136 Alertable = TRUE;
137 }
138 else
139 {
140 Alertable = FALSE;
141 }
142
143 KeWaitForSingleObject(&Event,
144 Executive,
145 KernelMode,
146 Alertable,
147 NULL);
148 Status = IoStatusBlock->Status;
149 }
150
151 return (Status);
152 }
153
154
155 /**********************************************************************
156 * NAME EXPORTED
157 * NtWriteFile
158 *
159 * DESCRIPTION
160 *
161 * ARGUMENTS
162 *
163 * RETURN VALUE
164 *
165 * REVISIONS
166 *
167 */
168 NTSTATUS STDCALL NtWriteFile(HANDLE FileHandle,
169 HANDLE EventHandle,
170 PIO_APC_ROUTINE ApcRoutine,
171 PVOID ApcContext,
172 PIO_STATUS_BLOCK IoStatusBlock,
173 PVOID Buffer,
174 ULONG Length,
175 PLARGE_INTEGER ByteOffset,
176 PULONG Key)
177 {
178 NTSTATUS Status;
179 PFILE_OBJECT FileObject;
180 PIRP Irp;
181 PIO_STACK_LOCATION StackPtr;
182 KEVENT Event;
183 PKEVENT ptrEvent;
184
185 DPRINT("NtWriteFile(FileHandle %x, Buffer %x, Length %d)\n",
186 FileHandle, Buffer, Length);
187
188 Status = ObReferenceObjectByHandle(FileHandle,
189 FILE_WRITE_DATA,
190 IoFileObjectType,
191 UserMode,
192 (PVOID*)&FileObject,
193 NULL);
194 if (!NT_SUCCESS(Status))
195 {
196 return(Status);
197 }
198
199 if (ByteOffset == NULL)
200 {
201 ByteOffset = &FileObject->CurrentByteOffset;
202 }
203
204 if (EventHandle != NULL)
205 {
206 Status = ObReferenceObjectByHandle(EventHandle,
207 SYNCHRONIZE,
208 ExEventObjectType,
209 UserMode,
210 (PVOID*)&ptrEvent,
211 NULL);
212 if (!NT_SUCCESS(Status))
213 {
214 ObDereferenceObject(FileObject);
215 return(Status);
216 }
217 }
218 else if (FileObject->Flags & FO_SYNCHRONOUS_IO)
219 {
220 KeInitializeEvent(&Event,
221 NotificationEvent,
222 FALSE);
223 ptrEvent = &Event;
224 }
225 else
226 {
227 ptrEvent = NULL;
228 }
229
230 KeInitializeEvent(&Event,
231 NotificationEvent,
232 FALSE);
233 Irp = IoBuildSynchronousFsdRequest(IRP_MJ_WRITE,
234 FileObject->DeviceObject,
235 Buffer,
236 Length,
237 ByteOffset,
238 ptrEvent,
239 IoStatusBlock);
240
241 Irp->Overlay.AsynchronousParameters.UserApcRoutine = ApcRoutine;
242 Irp->Overlay.AsynchronousParameters.UserApcContext = ApcContext;
243
244 DPRINT("FileObject->DeviceObject %x\n",FileObject->DeviceObject);
245
246 StackPtr = IoGetNextIrpStackLocation(Irp);
247 StackPtr->FileObject = FileObject;
248 if (Key != NULL)
249 {
250 StackPtr->Parameters.Write.Key = *Key;
251 }
252 else
253 {
254 StackPtr->Parameters.Write.Key = 0;
255 }
256 Status = IoCallDriver(FileObject->DeviceObject, Irp);
257 if ((Status == STATUS_PENDING) &&
258 (FileObject->Flags & FO_SYNCHRONOUS_IO))
259 {
260 KeWaitForSingleObject(&Event,
261 Executive,
262 KernelMode,
263 FALSE,
264 NULL);
265 Status = Irp->IoStatus.Status;
266 }
267 return(Status);
268 }
269
270
271 /**********************************************************************
272 * NAME EXPORTED
273 * NtReadFileScatter
274 *
275 * DESCRIPTION
276 *
277 * ARGUMENTS
278 *
279 * RETURN VALUE
280 *
281 * REVISIONS
282 *
283 */
284 NTSTATUS
285 STDCALL
286 NtReadFileScatter (
287 IN HANDLE FileHandle,
288 IN HANDLE Event OPTIONAL,
289 IN PIO_APC_ROUTINE UserApcRoutine OPTIONAL,
290 IN PVOID UserApcContext OPTIONAL,
291 OUT PIO_STATUS_BLOCK UserIoStatusBlock,
292 IN FILE_SEGMENT_ELEMENT BufferDescription [],
293 IN ULONG BufferLength,
294 IN PLARGE_INTEGER ByteOffset,
295 IN PULONG Key OPTIONAL
296 )
297 {
298 UNIMPLEMENTED;
299 }
300
301
302 /**********************************************************************
303 * NAME EXPORTED
304 * NtWriteFileGather
305 *
306 * DESCRIPTION
307 *
308 * ARGUMENTS
309 *
310 * RETURN VALUE
311 *
312 * REVISIONS
313 *
314 */
315 NTSTATUS
316 STDCALL
317 NtWriteFileGather (
318 IN HANDLE FileHandle,
319 IN HANDLE Event OPTIONAL,
320 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
321 IN PVOID ApcContext OPTIONAL,
322 OUT PIO_STATUS_BLOCK IoStatusBlock,
323 IN FILE_SEGMENT_ELEMENT BufferDescription [],
324 IN ULONG BufferLength,
325 IN PLARGE_INTEGER ByteOffset,
326 IN PULONG Key OPTIONAL
327 )
328 {
329 UNIMPLEMENTED;
330 }
331
332
333 /* EOF */