Fixed LARGE_INTEGER handling
[reactos.git] / reactos / ntoskrnl / io / rw.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/io/rw.c
5 * PURPOSE: Implements read/write APIs
6 * PROGRAMMER: David Welch (welch@cwcom.net)
7 * UPDATE HISTORY:
8 * 30/05/98: Created
9 */
10
11 /* INCLUDES ****************************************************************/
12
13 #include <ddk/ntddk.h>
14 #include <internal/io.h>
15 #include <string.h>
16 #include <internal/string.h>
17 #include <internal/ob.h>
18
19 #define NDEBUG
20 #include <internal/debug.h>
21
22 /* FUNCTIONS ***************************************************************/
23
24 NTSTATUS NtReadFile(HANDLE FileHandle,
25 HANDLE EventHandle,
26 PIO_APC_ROUTINE ApcRoutine,
27 PVOID ApcContext,
28 PIO_STATUS_BLOCK IoStatusBlock,
29 PVOID Buffer,
30 ULONG Length,
31 PLARGE_INTEGER ByteOffset,
32 PULONG Key)
33 {
34 return(ZwReadFile(FileHandle,
35 EventHandle,
36 ApcRoutine,
37 ApcContext,
38 IoStatusBlock,
39 Buffer,
40 Length,
41 ByteOffset,
42 Key));
43 }
44
45 NTSTATUS ZwReadFile(HANDLE FileHandle,
46 HANDLE EventHandle,
47 PIO_APC_ROUTINE ApcRoutine,
48 PVOID ApcContext,
49 PIO_STATUS_BLOCK IoStatusBlock,
50 PVOID Buffer,
51 ULONG Length,
52 PLARGE_INTEGER ByteOffset,
53 PULONG Key)
54 {
55 NTSTATUS Status;
56 PFILE_OBJECT FileObject;
57 PIRP Irp;
58 PIO_STACK_LOCATION StackPtr;
59 PKEVENT ptrEvent = NULL;
60 KEVENT Event;
61
62 assert(KeGetCurrentIrql()==PASSIVE_LEVEL);
63
64 DPRINT("ZwReadFile(FileHandle %x Buffer %x Length %x ByteOffset %x, "
65 "IoStatusBlock %x)\n",
66 FileHandle,Buffer,Length,ByteOffset,IoStatusBlock);
67
68 Status = ObReferenceObjectByHandle(FileHandle,
69 FILE_READ_DATA,
70 IoFileType,
71 UserMode,
72 (PVOID *) &FileObject,
73 NULL);
74 if (!NT_SUCCESS(STATUS_SUCCESS))
75 {
76 DPRINT("ZwReadFile() = %x\n",Status);
77 return(Status);
78 }
79
80 DPRINT("ByteOffset %x FileObject->CurrentByteOffset %d\n",
81 ByteOffset,
82 FileObject->CurrentByteOffset.LowPart);
83 if (ByteOffset==NULL)
84 {
85 ByteOffset = &(FileObject->CurrentByteOffset);
86 }
87
88 if (EventHandle != NULL)
89 {
90 Status = ObReferenceObjectByHandle(EventHandle,
91 SYNCHRONIZE,
92 ExEventType,
93 UserMode,
94 (PVOID *)ptrEvent,
95 NULL);
96 if (!NT_SUCCESS(Status))
97 {
98 return(Status);
99 }
100 }
101 else
102 {
103 KeInitializeEvent(&Event,NotificationEvent,FALSE);
104 ptrEvent = &Event;
105 }
106
107 DPRINT("FileObject %x\n",FileObject);
108 Irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ,
109 FileObject->DeviceObject,
110 Buffer,
111 Length,
112 ByteOffset,
113 ptrEvent,
114 IoStatusBlock);
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,Irp);
128 if (Status == STATUS_PENDING && (FileObject->Flags & FO_SYNCHRONOUS_IO))
129 {
130 KeWaitForSingleObject(&Event,Executive,KernelMode,FALSE,NULL);
131 Status = IoStatusBlock->Status;
132 }
133 DPRINT("ZwReadFile() = %x\n",Status);
134 return(Status);
135 }
136
137 NTSTATUS NtWriteFile(HANDLE FileHandle,
138 HANDLE EventHandle,
139 PIO_APC_ROUTINE ApcRoutine,
140 PVOID ApcContext,
141 PIO_STATUS_BLOCK IoStatusBlock,
142 PVOID Buffer,
143 ULONG Length,
144 PLARGE_INTEGER ByteOffset,
145 PULONG Key)
146 {
147 return(ZwWriteFile(FileHandle,
148 EventHandle,
149 ApcRoutine,
150 ApcContext,
151 IoStatusBlock,
152 Buffer,
153 Length,
154 ByteOffset,
155 Key));
156 }
157
158 NTSTATUS ZwWriteFile(HANDLE FileHandle,
159 HANDLE EventHandle,
160 PIO_APC_ROUTINE ApcRoutine,
161 PVOID ApcContext,
162 PIO_STATUS_BLOCK IoStatusBlock,
163 PVOID Buffer,
164 ULONG Length,
165 PLARGE_INTEGER ByteOffset,
166 PULONG Key)
167 {
168 NTSTATUS Status;
169 PFILE_OBJECT FileObject;
170 PIRP Irp;
171 PIO_STACK_LOCATION StackPtr;
172 KEVENT Event;
173
174 DPRINT("ZwWriteFile(FileHandle %x, Buffer %x, Length %d)\n",
175 FileHandle,Buffer,Length);
176
177 Status = ObReferenceObjectByHandle(FileHandle,
178 FILE_WRITE_DATA,
179 IoFileType,
180 UserMode,
181 (PVOID *) &FileObject,
182 NULL);
183 if (Status != STATUS_SUCCESS)
184 {
185 return(Status);
186 }
187 if (ByteOffset==NULL)
188 {
189 ByteOffset = &(FileObject->CurrentByteOffset);
190 }
191
192 KeInitializeEvent(&Event,NotificationEvent,FALSE);
193 Irp = IoBuildSynchronousFsdRequest(IRP_MJ_WRITE,
194 FileObject->DeviceObject,
195 Buffer,
196 Length,
197 ByteOffset,
198 &Event,
199 IoStatusBlock);
200 DPRINT("FileObject->DeviceObject %x\n",FileObject->DeviceObject);
201 StackPtr = IoGetNextIrpStackLocation(Irp);
202 StackPtr->FileObject = FileObject;
203 if (Key!=NULL)
204 {
205 StackPtr->Parameters.Write.Key = *Key;
206 }
207 else
208 {
209 StackPtr->Parameters.Write.Key = 0;
210 }
211 Status = IoCallDriver(FileObject->DeviceObject,Irp);
212 if (Status == STATUS_PENDING && (FileObject->Flags & FO_SYNCHRONOUS_IO))
213 {
214 KeWaitForSingleObject(&Event,Executive,KernelMode,FALSE,NULL);
215 Status = Irp->IoStatus.Status;
216 }
217 return(Status);
218 }
219
220 NTSTATUS STDCALL NtReadFileScatter(IN HANDLE FileHandle,
221 IN HANDLE Event OPTIONAL,
222 IN PIO_APC_ROUTINE UserApcRoutine OPTIONAL,
223 IN PVOID UserApcContext OPTIONAL,
224 OUT PIO_STATUS_BLOCK UserIoStatusBlock,
225 IN FILE_SEGMENT_ELEMENT BufferDescription[],
226 IN ULONG BufferLength,
227 IN PLARGE_INTEGER ByteOffset,
228 IN PULONG Key OPTIONAL)
229 {
230 return(ZwReadFileScatter(FileHandle,
231 Event,
232 UserApcRoutine,
233 UserApcContext,
234 UserIoStatusBlock,
235 BufferDescription,
236 BufferLength,
237 ByteOffset,
238 Key));
239 }
240
241 NTSTATUS STDCALL ZwReadFileScatter(IN HANDLE FileHandle,
242 IN HANDLE Event OPTIONAL,
243 IN PIO_APC_ROUTINE UserApcRoutine OPTIONAL,
244 IN PVOID UserApcContext OPTIONAL,
245 OUT PIO_STATUS_BLOCK UserIoStatusBlock,
246 IN FILE_SEGMENT_ELEMENT BufferDescription[],
247 IN ULONG BufferLength,
248 IN PLARGE_INTEGER ByteOffset,
249 IN PULONG Key OPTIONAL)
250 {
251 UNIMPLEMENTED;
252 }
253
254
255 NTSTATUS STDCALL NtWriteFileGather(IN HANDLE FileHandle,
256 IN HANDLE Event OPTIONAL,
257 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
258 IN PVOID ApcContext OPTIONAL,
259 OUT PIO_STATUS_BLOCK IoStatusBlock,
260 IN FILE_SEGMENT_ELEMENT BufferDescription[],
261 IN ULONG BufferLength,
262 IN PLARGE_INTEGER ByteOffset,
263 IN PULONG Key OPTIONAL)
264 {
265 return(ZwWriteFileGather(FileHandle,
266 Event,
267 ApcRoutine,
268 ApcContext,
269 IoStatusBlock,
270 BufferDescription,
271 BufferLength,
272 ByteOffset,
273 Key));
274 }
275
276 NTSTATUS STDCALL ZwWriteFileGather(IN HANDLE FileHandle,
277 IN HANDLE Event OPTIONAL,
278 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
279 IN PVOID ApcContext OPTIONAL,
280 OUT PIO_STATUS_BLOCK IoStatusBlock,
281 IN FILE_SEGMENT_ELEMENT BufferDescription[],
282 IN ULONG BufferLength,
283 IN PLARGE_INTEGER ByteOffset,
284 IN PULONG Key OPTIONAL)
285 {
286 UNIMPLEMENTED;
287 }