ReactOS  0.4.15-dev-1384-g878186b
io.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2005 ReactOS Team (and the authors from the programmers section)
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  *
19  * PROJECT: ReactOS kernel
20  * FILE: ntoskrnl/cache/section/io.c
21  * PURPOSE: Implements section objects
22  *
23  * PROGRAMMERS: Rex Jolliff
24  * David Welch
25  * Eric Kohl
26  * Emanuele Aliberti
27  * Eugene Ingerman
28  * Casper Hornstrup
29  * KJK::Hyperion
30  * Guido de Jong
31  * Ge van Geldorp
32  * Royce Mitchell III
33  * Filip Navara
34  * Aleksey Bragin
35  * Jason Filby
36  * Thomas Weidenmueller
37  * Gunnar Andre' Dalsnes
38  * Mike Nordell
39  * Alex Ionescu
40  * Gregor Anich
41  * Steven Edwards
42  * Herve Poussineau
43  */
44 
45 /* INCLUDES *****************************************************************/
46 
47 #include <ntoskrnl.h>
48 #include "newmm.h"
49 #define NDEBUG
50 #include <debug.h>
51 #include <reactos/exeformat.h>
52 
54 
56 NTAPI
58 {
60 }
61 
62 /*
63 
64 Note:
65 This completion function is really required. Paging io completion does almost
66 nothing, including freeing the mdls.
67 
68 */
69 _Function_class_(IO_COMPLETION_ROUTINE)
71 NTAPI
72 MiSimpleReadComplete(PDEVICE_OBJECT DeviceObject,
73  PIRP Irp,
74  PVOID Context)
75 {
76  PMDL Mdl = Irp->MdlAddress;
77 
78  /* Unlock MDL Pages, page 167. */
79  DPRINT("MiSimpleReadComplete %p\n", Irp);
80  while (Mdl)
81  {
82  DPRINT("MDL Unlock %p\n", Mdl);
84  Mdl = Mdl->Next;
85  }
86 
87  /* Check if there's an MDL */
88  while ((Mdl = Irp->MdlAddress))
89  {
90  /* Clear all of them */
91  Irp->MdlAddress = Mdl->Next;
92  IoFreeMdl(Mdl);
93  }
94 
95  return STATUS_SUCCESS;
96 }
97 
98 /*
99 
100 MiSimpleRead is a convenience function that provides either paging or non
101 paging reads. The caching and mm systems use this in paging mode, where
102 a completion function is required as above. The Paging BOOLEAN determines
103 whether the read is issued as a paging read or as an ordinary buffered read.
104 
105 */
106 
107 NTSTATUS
108 NTAPI
111  PVOID Buffer,
112  ULONG Length,
113  BOOLEAN Paging,
115 {
117  PIRP Irp = NULL;
118  KEVENT ReadWait;
121 
124  ASSERT(Buffer);
126 
129  ReadStatus->Information = 0;
130 
132 
133  DPRINT("PAGING READ: FileObject %p <%wZ> Offset %08x%08x Length %ul\n",
134  FileObject,
135  &FileObject->FileName,
136  FileOffset->HighPart,
137  FileOffset->LowPart,
138  Length);
139 
141 
143  DeviceObject,
144  Buffer,
145  Length,
146  FileOffset,
147  ReadStatus);
148 
149  if (!Irp)
150  {
151  return STATUS_NO_MEMORY;
152  }
153 
155 
156  Irp->UserEvent = &ReadWait;
157  Irp->Tail.Overlay.OriginalFileObject = FileObject;
158  Irp->Tail.Overlay.Thread = PsGetCurrentThread();
162  IrpSp->CompletionRoutine = MiSimpleReadComplete;
163 
164  /* Non paging case, the FileObject will be dereferenced at completion */
165  if (!Paging)
167 
169  if (Status == STATUS_PENDING)
170  {
171  DPRINT("KeWaitForSingleObject(&ReadWait)\n");
172  if (!NT_SUCCESS(KeWaitForSingleObject(&ReadWait,
173  Suspended,
174  KernelMode,
175  FALSE,
176  NULL)))
177  {
178  DPRINT1("Warning: Failed to wait for synchronous IRP\n");
179  ASSERT(FALSE);
180  return Status;
181  }
182  }
183 
184  DPRINT("Paging IO Done: %08x\n", ReadStatus->Status);
185  /* When "ReadStatus->Information > 0" is false and "ReadStatus->Status == STATUS_END_OF_FILE" is true
186  * it means that read pointer is out of file, so we must fail */
187  Status = ReadStatus->Status == STATUS_END_OF_FILE && ReadStatus->Information > 0 ? STATUS_SUCCESS : ReadStatus->Status;
188  return Status;
189 }
190 
191 /*
192 
193 Convenience function for writing from kernel space. This issues a paging
194 write in all cases.
195 
196 */
197 
198 NTSTATUS
199 NTAPI
202  PVOID Buffer,
203  ULONG Length,
205  const char *File,
206  int Line)
207 {
209  PIRP Irp = NULL;
210  KEVENT ReadWait;
213 
216  ASSERT(Buffer);
218 
221 
222  DPRINT("PAGING WRITE: FileObject %p <%wZ> Offset 0x%I64x Length %lu (%s:%d)\n",
223  FileObject,
224  &FileObject->FileName,
225  FileOffset->QuadPart,
226  Length,
227  File,
228  Line);
229 
231 
233  DeviceObject,
234  Buffer,
235  Length,
236  FileOffset,
237  ReadStatus);
238 
239  if (!Irp)
240  {
241  return STATUS_NO_MEMORY;
242  }
243 
245 
246  Irp->UserEvent = &ReadWait;
247  Irp->Tail.Overlay.OriginalFileObject = FileObject;
248  Irp->Tail.Overlay.Thread = PsGetCurrentThread();
252  IrpSp->CompletionRoutine = MiSimpleReadComplete;
253 
254  DPRINT("Call Driver\n");
256  DPRINT("Status %x\n", Status);
257 
258  if (Status == STATUS_PENDING)
259  {
260  DPRINT("KeWaitForSingleObject(&ReadWait)\n");
261  if (!NT_SUCCESS(KeWaitForSingleObject(&ReadWait,
262  Suspended,
263  KernelMode,
264  FALSE,
265  NULL)))
266  {
267  DPRINT1("Warning: Failed to wait for synchronous IRP\n");
268  ASSERT(FALSE);
269  return Status;
270  }
271  }
272 
273  DPRINT("Paging IO Done: %08x\n", ReadStatus->Status);
274  return ReadStatus->Status;
275 }
276 
277 extern KEVENT MpwThreadEvent;
279 
280 /*
281 
282 Function which uses MiSimpleWrite to write back a single page to a file.
283 The page in question does not need to be mapped. This function could be
284 made a bit more efficient by avoiding the copy and making a system space
285 mdl.
286 
287 */
288 
289 NTSTATUS
290 NTAPI
293  ULONG Length,
294  PFN_NUMBER Page,
295  const char *File,
296  int Line)
297 {
299  PVOID Hyperspace;
301  KIRQL OldIrql;
303 
304  if (!PageBuffer) return STATUS_NO_MEMORY;
305 
306  Hyperspace = MiMapPageInHyperSpace(PsGetCurrentProcess(), Page, &OldIrql);
307  if (!Hyperspace)
308  {
309  ExFreePool(PageBuffer);
310  return STATUS_NO_MEMORY;
311  }
312  RtlCopyMemory(PageBuffer, Hyperspace, PAGE_SIZE);
314 
315  DPRINT("MiWriteBackPage(%wZ,%08x%08x,%s:%d)\n",
316  &FileObject->FileName,
317  FileOffset->u.HighPart,
318  FileOffset->u.LowPart,
319  File,
320  Line);
321 
323  FileOffset,
324  PageBuffer,
325  Length,
326  &Iosb);
327 
328  ExFreePool(PageBuffer);
329 
330  if (!NT_SUCCESS(Status))
331  {
332  DPRINT1("MiSimpleWrite failed (%x)\n", Status);
333  }
334 
335  return Status;
336 }
#define IN
Definition: typedefs.h:39
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
LONG NTSTATUS
Definition: precomp.h:26
#define IRP_NOCACHE
VOID NTAPI MmUnlockPages(IN PMDL Mdl)
Definition: mdlsup.c:1439
PDEVICE_OBJECT NTAPI IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject)
Definition: device.c:1539
KEVENT CcpLazyWriteEvent
Definition: io.c:53
NTSTATUS NTAPI _MiSimpleWrite(PFILE_OBJECT FileObject, PLARGE_INTEGER FileOffset, PVOID Buffer, ULONG Length, PIO_STATUS_BLOCK ReadStatus, const char *File, int Line)
Definition: io.c:200
NTSTATUS NTAPI _MiWriteBackPage(PFILE_OBJECT FileObject, PLARGE_INTEGER FileOffset, ULONG Length, PFN_NUMBER Page, const char *File, int Line)
Definition: io.c:291
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define STATUS_END_OF_FILE
Definition: shellext.h:67
#define STATUS_INTERNAL_ERROR
Definition: ntstatus.h:465
UCHAR KIRQL
Definition: env_spec_w32.h:591
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
ULONG PFN_NUMBER
Definition: ke.h:9
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
_In_ PIRP Irp
Definition: csq.h:116
#define PsGetCurrentProcess
Definition: psfuncs.h:17
PVOID NTAPI MiMapPageInHyperSpace(IN PEPROCESS Process, IN PFN_NUMBER Page, IN PKIRQL OldIrql)
Definition: hypermap.c:30
unsigned char BOOLEAN
void DPRINT(...)
Definition: polytest.cpp:61
Definition: bufpool.h:45
PDEVICE_OBJECT NTAPI MmGetDeviceObjectForFile(IN PFILE_OBJECT FileObject)
Definition: io.c:57
PIO_COMPLETION_ROUTINE CompletionRoutine
Definition: iotypes.h:3293
_In_ WDFREQUEST _In_ WDFFILEOBJECT FileObject
Definition: wdfdevice.h:547
Status
Definition: gdiplustypes.h:24
FAST_MUTEX
Definition: extypes.h:17
#define ASSERT(a)
Definition: mode.c:45
return Iosb
Definition: create.c:4402
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
static USHORT ReadStatus(IN PUCHAR ReadDataPort)
Definition: hardware.c:128
PIRP NTAPI IoBuildAsynchronousFsdRequest(IN ULONG MajorFunction, IN PDEVICE_OBJECT DeviceObject, IN PVOID Buffer, IN ULONG Length, IN PLARGE_INTEGER StartingOffset, IN PIO_STATUS_BLOCK IoStatusBlock)
Definition: irp.c:750
* PFILE_OBJECT
Definition: iotypes.h:1978
VOID NTAPI IoFreeMdl(PMDL Mdl)
Definition: iomdl.c:146
#define SL_INVOKE_ON_ERROR
Definition: iotypes.h:3308
Definition: ncftp.h:79
KEVENT MpwThreadEvent
_Function_class_(IO_COMPLETION_ROUTINE)
Definition: io.c:69
VOID NTAPI MiUnmapPageInHyperSpace(IN PEPROCESS Process, IN PVOID Address, IN KIRQL OldIrql)
Definition: hypermap.c:93
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:790
#define MiSimpleWrite(F, O, B, L, R)
Definition: newmm.h:198
#define ExAllocatePool(type, size)
Definition: fbtusb.h:44
#define PAGE_SIZE
Definition: env_spec_w32.h:49
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2691
FAST_MUTEX MiWriteMutex
Definition: io.c:278
PFILE_OBJECT FileObject
Definition: iotypes.h:3148
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define SL_INVOKE_ON_SUCCESS
Definition: iotypes.h:3307
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:159
_In_ PIO_STACK_LOCATION IrpSp
Definition: create.c:4137
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define NULL
Definition: types.h:112
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
_In_ WDFDEVICE _In_ PVOID _In_opt_ PMDL Mdl
#define IRP_PAGING_IO
#define DPRINT1
Definition: precomp.h:8
#define IRP_MJ_READ
Definition: rdpdr.c:46
#define ObReferenceObject
Definition: obfuncs.h:204
unsigned int ULONG
Definition: retypes.h:1
#define IRP_SYNCHRONOUS_API
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define IRP_MJ_WRITE
Definition: rdpdr.c:47
Definition: File.h:15
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define IRP_SYNCHRONOUS_PAGING_IO
NTSTATUS NTAPI MiSimpleRead(PFILE_OBJECT FileObject, PLARGE_INTEGER FileOffset, PVOID Buffer, ULONG Length, BOOLEAN Paging, PIO_STATUS_BLOCK ReadStatus)
Definition: io.c:109