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