[USP10_WINETEST] Sync with Wine Staging 2.9. CORE-13362
[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 if (IoStack->FileObject->FileName.Length >= 2 * sizeof(WCHAR) &&
199 IoStack->FileObject->FileName.Buffer[1] == 'R')
200 {
201 Fcb->Header.AllocationSize.QuadPart = 62;
202 Fcb->Header.FileSize.QuadPart = 62;
203 Fcb->Header.ValidDataLength.QuadPart = 62;
204 }
205 else
206 {
207 Fcb->Header.AllocationSize.QuadPart = 512;
208 Fcb->Header.FileSize.QuadPart = 512;
209 Fcb->Header.ValidDataLength.QuadPart = 512;
210 }
211 Fcb->Header.IsFastIoPossible = FastIoIsNotPossible;
212 IoStack->FileObject->FsContext = Fcb;
213 IoStack->FileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
214
215 CcInitializeCacheMap(IoStack->FileObject,
216 (PCC_FILE_SIZES)&Fcb->Header.AllocationSize,
217 FALSE, &Callbacks, NULL);
218
219 Irp->IoStatus.Information = FILE_OPENED;
220 Status = STATUS_SUCCESS;
221 }
222 else if (IoStack->MajorFunction == IRP_MJ_READ)
223 {
224 BOOLEAN Ret;
225 ULONG Length;
226 PVOID Buffer;
227 LARGE_INTEGER Offset;
228
229 Offset = IoStack->Parameters.Read.ByteOffset;
230 Length = IoStack->Parameters.Read.Length;
231 Fcb = IoStack->FileObject->FsContext;
232
233 ok_eq_pointer(DeviceObject, TestDeviceObject);
234 ok_eq_pointer(IoStack->FileObject, TestFileObject);
235
236 if (!FlagOn(Irp->Flags, IRP_NOCACHE))
237 {
238 ok(Offset.QuadPart % PAGE_SIZE != 0, "Offset is aligned: %I64i\n", Offset.QuadPart);
239 ok(Length % PAGE_SIZE != 0, "Length is aligned: %I64i\n", Length);
240
241 Buffer = Irp->AssociatedIrp.SystemBuffer;
242 ok(Buffer != NULL, "Null pointer!\n");
243
244 _SEH2_TRY
245 {
246 Ret = CcCopyRead(IoStack->FileObject, &Offset, Length, TRUE, Buffer,
247 &Irp->IoStatus);
248 ok_bool_true(Ret, "CcCopyRead");
249 }
250 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
251 {
252 Irp->IoStatus.Status = _SEH2_GetExceptionCode();
253 }
254 _SEH2_END;
255
256 Status = Irp->IoStatus.Status;
257
258 if (NT_SUCCESS(Status))
259 {
260 if (Offset.QuadPart <= 1000LL && Offset.QuadPart + Length > 1000LL)
261 {
262 ok_eq_hex(*(PUSHORT)((ULONG_PTR)Buffer + (ULONG_PTR)(1000LL - Offset.QuadPart)), 0xFFFF);
263 }
264 else
265 {
266 ok_eq_hex(*(PUSHORT)Buffer, 0xBABA);
267 }
268 }
269 }
270 else
271 {
272 ok((Offset.QuadPart % PAGE_SIZE == 0 || Offset.QuadPart == 0), "Offset is not aligned: %I64i\n", Offset.QuadPart);
273 ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);
274
275 ok(Irp->AssociatedIrp.SystemBuffer == NULL, "A SystemBuffer was allocated!\n");
276 Buffer = MapAndLockUserBuffer(Irp, Length);
277 ok(Buffer != NULL, "Null pointer!\n");
278 RtlFillMemory(Buffer, Length, 0xBA);
279
280 Status = STATUS_SUCCESS;
281 if (Offset.QuadPart <= 1000LL && Offset.QuadPart + Length > 1000LL)
282 {
283 *(PUSHORT)((ULONG_PTR)Buffer + (ULONG_PTR)(1000LL - Offset.QuadPart)) = 0xFFFF;
284 }
285 }
286
287 if (NT_SUCCESS(Status))
288 {
289 Irp->IoStatus.Information = Length;
290 IoStack->FileObject->CurrentByteOffset.QuadPart = Offset.QuadPart + Length;
291 }
292 }
293 else if (IoStack->MajorFunction == IRP_MJ_CLEANUP)
294 {
295 KeInitializeEvent(&CacheUninitEvent.Event, NotificationEvent, FALSE);
296 CcUninitializeCacheMap(IoStack->FileObject, NULL, &CacheUninitEvent);
297 KeWaitForSingleObject(&CacheUninitEvent.Event, Executive, KernelMode, FALSE, NULL);
298 Fcb = IoStack->FileObject->FsContext;
299 ExFreePoolWithTag(Fcb, 'FwrI');
300 IoStack->FileObject->FsContext = NULL;
301 Status = STATUS_SUCCESS;
302 }
303
304 if (Status == STATUS_PENDING)
305 {
306 IoMarkIrpPending(Irp);
307 IoCompleteRequest(Irp, IO_NO_INCREMENT);
308 Status = STATUS_PENDING;
309 }
310 else
311 {
312 Irp->IoStatus.Status = Status;
313 IoCompleteRequest(Irp, IO_NO_INCREMENT);
314 }
315
316 return Status;
317 }