[KMTESTS:CC]
[reactos.git] / rostests / kmtests / ntos_cc / CcCopyRead_drv.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test driver for CcCopyRead function
5 * PROGRAMMER: Pierre Schweitzer <pierre@reactos.org>
6 */
7
8 #include <kmt_test.h>
9
10 #define NDEBUG
11 #include <debug.h>
12
13 typedef struct _TEST_FCB
14 {
15 FSRTL_ADVANCED_FCB_HEADER Header;
16 SECTION_OBJECT_POINTERS SectionObjectPointers;
17 FAST_MUTEX HeaderMutex;
18 } TEST_FCB, *PTEST_FCB;
19
20 static PFILE_OBJECT TestFileObject;
21 static PDEVICE_OBJECT TestDeviceObject;
22 static KMT_IRP_HANDLER TestIrpHandler;
23 static FAST_IO_DISPATCH TestFastIoDispatch;
24
25 static
26 BOOLEAN
27 NTAPI
28 FastIoRead(
29 _In_ PFILE_OBJECT FileObject,
30 _In_ PLARGE_INTEGER FileOffset,
31 _In_ ULONG Length,
32 _In_ BOOLEAN Wait,
33 _In_ ULONG LockKey,
34 _Out_ PVOID Buffer,
35 _Out_ PIO_STATUS_BLOCK IoStatus,
36 _In_ PDEVICE_OBJECT DeviceObject)
37 {
38 IoStatus->Status = STATUS_NOT_SUPPORTED;
39 return FALSE;
40 }
41
42 NTSTATUS
43 TestEntry(
44 _In_ PDRIVER_OBJECT DriverObject,
45 _In_ PCUNICODE_STRING RegistryPath,
46 _Out_ PCWSTR *DeviceName,
47 _Inout_ INT *Flags)
48 {
49 NTSTATUS Status = STATUS_SUCCESS;
50
51 PAGED_CODE();
52
53 UNREFERENCED_PARAMETER(RegistryPath);
54
55 *DeviceName = L"CcCopyRead";
56 *Flags = TESTENTRY_NO_EXCLUSIVE_DEVICE |
57 TESTENTRY_BUFFERED_IO_DEVICE |
58 TESTENTRY_NO_READONLY_DEVICE;
59
60 KmtRegisterIrpHandler(IRP_MJ_CLEANUP, NULL, TestIrpHandler);
61 KmtRegisterIrpHandler(IRP_MJ_CREATE, NULL, TestIrpHandler);
62 KmtRegisterIrpHandler(IRP_MJ_READ, NULL, TestIrpHandler);
63
64 TestFastIoDispatch.FastIoRead = FastIoRead;
65 DriverObject->FastIoDispatch = &TestFastIoDispatch;
66
67
68 return Status;
69 }
70
71 VOID
72 TestUnload(
73 _In_ PDRIVER_OBJECT DriverObject)
74 {
75 PAGED_CODE();
76 }
77
78 BOOLEAN
79 NTAPI
80 AcquireForLazyWrite(
81 _In_ PVOID Context,
82 _In_ BOOLEAN Wait)
83 {
84 return TRUE;
85 }
86
87 VOID
88 NTAPI
89 ReleaseFromLazyWrite(
90 _In_ PVOID Context)
91 {
92 return;
93 }
94
95 BOOLEAN
96 NTAPI
97 AcquireForReadAhead(
98 _In_ PVOID Context,
99 _In_ BOOLEAN Wait)
100 {
101 return TRUE;
102 }
103
104 VOID
105 NTAPI
106 ReleaseFromReadAhead(
107 _In_ PVOID Context)
108 {
109 return;
110 }
111
112 static CACHE_MANAGER_CALLBACKS Callbacks = {
113 AcquireForLazyWrite,
114 ReleaseFromLazyWrite,
115 AcquireForReadAhead,
116 ReleaseFromReadAhead,
117 };
118
119 static
120 PVOID
121 MapAndLockUserBuffer(
122 _In_ _Out_ PIRP Irp,
123 _In_ ULONG BufferLength)
124 {
125 PMDL Mdl;
126
127 if (Irp->MdlAddress == NULL)
128 {
129 Mdl = IoAllocateMdl(Irp->UserBuffer, BufferLength, FALSE, FALSE, Irp);
130 if (Mdl == NULL)
131 {
132 return NULL;
133 }
134
135 _SEH2_TRY
136 {
137 MmProbeAndLockPages(Mdl, Irp->RequestorMode, IoWriteAccess);
138 }
139 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
140 {
141 IoFreeMdl(Mdl);
142 Irp->MdlAddress = NULL;
143 _SEH2_YIELD(return NULL);
144 }
145 _SEH2_END;
146 }
147
148 return MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
149 }
150
151
152 static
153 NTSTATUS
154 TestIrpHandler(
155 _In_ PDEVICE_OBJECT DeviceObject,
156 _In_ PIRP Irp,
157 _In_ PIO_STACK_LOCATION IoStack)
158 {
159 NTSTATUS Status;
160 PTEST_FCB Fcb;
161 CACHE_UNINITIALIZE_EVENT CacheUninitEvent;
162
163 PAGED_CODE();
164
165 DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
166 ASSERT(IoStack->MajorFunction == IRP_MJ_CLEANUP ||
167 IoStack->MajorFunction == IRP_MJ_CREATE ||
168 IoStack->MajorFunction == IRP_MJ_READ);
169
170 Status = STATUS_NOT_SUPPORTED;
171 Irp->IoStatus.Information = 0;
172
173 if (IoStack->MajorFunction == IRP_MJ_CREATE)
174 {
175 if (IoStack->FileObject->FileName.Length >= 2 * sizeof(WCHAR))
176 {
177 TestDeviceObject = DeviceObject;
178 TestFileObject = IoStack->FileObject;
179 }
180 Fcb = ExAllocatePoolWithTag(NonPagedPool, sizeof(*Fcb), 'FwrI');
181 RtlZeroMemory(Fcb, sizeof(*Fcb));
182 ExInitializeFastMutex(&Fcb->HeaderMutex);
183 FsRtlSetupAdvancedHeader(&Fcb->Header, &Fcb->HeaderMutex);
184 if (IoStack->FileObject->FileName.Length >= 2 * sizeof(WCHAR) &&
185 IoStack->FileObject->FileName.Buffer[1] == 'B')
186 {
187 Fcb->Header.AllocationSize.QuadPart = 1000000;
188 Fcb->Header.FileSize.QuadPart = 1000000;
189 Fcb->Header.ValidDataLength.QuadPart = 1000000;
190 }
191 else if (IoStack->FileObject->FileName.Length >= 2 * sizeof(WCHAR) &&
192 IoStack->FileObject->FileName.Buffer[1] == 'S')
193 {
194 Fcb->Header.AllocationSize.QuadPart = 1004;
195 Fcb->Header.FileSize.QuadPart = 1004;
196 Fcb->Header.ValidDataLength.QuadPart = 1004;
197 }
198 else
199 {
200 Fcb->Header.AllocationSize.QuadPart = 512;
201 Fcb->Header.FileSize.QuadPart = 512;
202 Fcb->Header.ValidDataLength.QuadPart = 512;
203 }
204 Fcb->Header.IsFastIoPossible = FastIoIsNotPossible;
205 IoStack->FileObject->FsContext = Fcb;
206 IoStack->FileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
207
208 CcInitializeCacheMap(IoStack->FileObject,
209 (PCC_FILE_SIZES)&Fcb->Header.AllocationSize,
210 FALSE, &Callbacks, NULL);
211
212 Irp->IoStatus.Information = FILE_OPENED;
213 Status = STATUS_SUCCESS;
214 }
215 else if (IoStack->MajorFunction == IRP_MJ_READ)
216 {
217 BOOLEAN Ret;
218 ULONG Length;
219 PVOID Buffer;
220 LARGE_INTEGER Offset;
221
222 Offset = IoStack->Parameters.Read.ByteOffset;
223 Length = IoStack->Parameters.Read.Length;
224 Fcb = IoStack->FileObject->FsContext;
225
226 ok_eq_pointer(DeviceObject, TestDeviceObject);
227 ok_eq_pointer(IoStack->FileObject, TestFileObject);
228
229 if (!FlagOn(Irp->Flags, IRP_NOCACHE))
230 {
231 ok(Offset.QuadPart % PAGE_SIZE != 0, "Offset is aligned: %I64i\n", Offset.QuadPart);
232 ok(Length % PAGE_SIZE != 0, "Length is aligned: %I64i\n", Length);
233
234 Buffer = Irp->AssociatedIrp.SystemBuffer;
235 ok(Buffer != NULL, "Null pointer!\n");
236
237 _SEH2_TRY
238 {
239 Ret = CcCopyRead(IoStack->FileObject, &Offset, Length, TRUE, Buffer,
240 &Irp->IoStatus);
241 ok_bool_true(Ret, "CcCopyRead");
242 }
243 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
244 {
245 Irp->IoStatus.Status = _SEH2_GetExceptionCode();
246 }
247 _SEH2_END;
248
249 Status = Irp->IoStatus.Status;
250
251 if (NT_SUCCESS(Status))
252 {
253 if (Offset.QuadPart <= 1000LL && Offset.QuadPart + Length > 1000LL)
254 {
255 ok_eq_hex(*(PUSHORT)((ULONG_PTR)Buffer + (ULONG_PTR)(1000LL - Offset.QuadPart)), 0xFFFF);
256 }
257 else
258 {
259 ok_eq_hex(*(PUSHORT)Buffer, 0xBABA);
260 }
261 }
262 }
263 else
264 {
265 ok(Offset.QuadPart % PAGE_SIZE == 0, "Offset is not aligned: %I64i\n", Offset.QuadPart);
266 ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);
267
268 ok(Irp->AssociatedIrp.SystemBuffer == NULL, "A SystemBuffer was allocated!\n");
269 Buffer = MapAndLockUserBuffer(Irp, Length);
270 ok(Buffer != NULL, "Null pointer!\n");
271 RtlFillMemory(Buffer, Length, 0xBA);
272
273 Status = STATUS_SUCCESS;
274 if (Offset.QuadPart <= 1000LL && Offset.QuadPart + Length > 1000LL)
275 {
276 *(PUSHORT)((ULONG_PTR)Buffer + (ULONG_PTR)(1000LL - Offset.QuadPart)) = 0xFFFF;
277 }
278 }
279
280 if (NT_SUCCESS(Status))
281 {
282 Irp->IoStatus.Information = Length;
283 IoStack->FileObject->CurrentByteOffset.QuadPart = Offset.QuadPart + Length;
284 }
285 }
286 else if (IoStack->MajorFunction == IRP_MJ_CLEANUP)
287 {
288 KeInitializeEvent(&CacheUninitEvent.Event, NotificationEvent, FALSE);
289 CcUninitializeCacheMap(IoStack->FileObject, NULL, &CacheUninitEvent);
290 KeWaitForSingleObject(&CacheUninitEvent.Event, Executive, KernelMode, FALSE, NULL);
291 Fcb = IoStack->FileObject->FsContext;
292 ExFreePoolWithTag(Fcb, 'FwrI');
293 IoStack->FileObject->FsContext = NULL;
294 Status = STATUS_SUCCESS;
295 }
296
297 if (Status == STATUS_PENDING)
298 {
299 IoMarkIrpPending(Irp);
300 IoCompleteRequest(Irp, IO_NO_INCREMENT);
301 Status = STATUS_PENDING;
302 }
303 else
304 {
305 Irp->IoStatus.Status = Status;
306 IoCompleteRequest(Irp, IO_NO_INCREMENT);
307 }
308
309 return Status;
310 }