3934b9d6a823451d4bdbd2d3b58beed78b675e13
[reactos.git] / modules / rostests / kmtests / ntos_cc / CcSetFileSizes_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 CcSetFileSizes 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 #define IOCTL_START_TEST 1
14 #define IOCTL_FINISH_TEST 2
15
16 typedef struct _TEST_FCB
17 {
18 FSRTL_ADVANCED_FCB_HEADER Header;
19 SECTION_OBJECT_POINTERS SectionObjectPointers;
20 FAST_MUTEX HeaderMutex;
21 } TEST_FCB, *PTEST_FCB;
22
23 static ULONG TestTestId = -1;
24 static PFILE_OBJECT TestFileObject;
25 static PDEVICE_OBJECT TestDeviceObject;
26 static KMT_IRP_HANDLER TestIrpHandler;
27 static KMT_MESSAGE_HANDLER TestMessageHandler;
28
29 NTSTATUS
30 TestEntry(
31 _In_ PDRIVER_OBJECT DriverObject,
32 _In_ PCUNICODE_STRING RegistryPath,
33 _Out_ PCWSTR *DeviceName,
34 _Inout_ INT *Flags)
35 {
36 NTSTATUS Status = STATUS_SUCCESS;
37
38 PAGED_CODE();
39
40 UNREFERENCED_PARAMETER(RegistryPath);
41
42 *DeviceName = L"CcSetFileSizes";
43 *Flags = TESTENTRY_NO_EXCLUSIVE_DEVICE |
44 TESTENTRY_BUFFERED_IO_DEVICE |
45 TESTENTRY_NO_READONLY_DEVICE;
46
47 KmtRegisterIrpHandler(IRP_MJ_READ, NULL, TestIrpHandler);
48 KmtRegisterMessageHandler(0, NULL, TestMessageHandler);
49
50 return Status;
51 }
52
53 VOID
54 TestUnload(
55 _In_ PDRIVER_OBJECT DriverObject)
56 {
57 PAGED_CODE();
58 }
59
60 BOOLEAN
61 NTAPI
62 AcquireForLazyWrite(
63 _In_ PVOID Context,
64 _In_ BOOLEAN Wait)
65 {
66 return TRUE;
67 }
68
69 VOID
70 NTAPI
71 ReleaseFromLazyWrite(
72 _In_ PVOID Context)
73 {
74 return;
75 }
76
77 BOOLEAN
78 NTAPI
79 AcquireForReadAhead(
80 _In_ PVOID Context,
81 _In_ BOOLEAN Wait)
82 {
83 return TRUE;
84 }
85
86 VOID
87 NTAPI
88 ReleaseFromReadAhead(
89 _In_ PVOID Context)
90 {
91 return;
92 }
93
94 static CACHE_MANAGER_CALLBACKS Callbacks = {
95 AcquireForLazyWrite,
96 ReleaseFromLazyWrite,
97 AcquireForReadAhead,
98 ReleaseFromReadAhead,
99 };
100
101 static CC_FILE_SIZES NewFileSizes = {
102 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)VACB_MAPPING_GRANULARITY), // .AllocationSize
103 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)VACB_MAPPING_GRANULARITY), // .FileSize
104 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)VACB_MAPPING_GRANULARITY) // .ValidDataLength
105 };
106
107 static
108 PVOID
109 MapAndLockUserBuffer(
110 _In_ _Out_ PIRP Irp,
111 _In_ ULONG BufferLength)
112 {
113 PMDL Mdl;
114
115 if (Irp->MdlAddress == NULL)
116 {
117 Mdl = IoAllocateMdl(Irp->UserBuffer, BufferLength, FALSE, FALSE, Irp);
118 if (Mdl == NULL)
119 {
120 return NULL;
121 }
122
123 _SEH2_TRY
124 {
125 MmProbeAndLockPages(Mdl, Irp->RequestorMode, IoWriteAccess);
126 }
127 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
128 {
129 IoFreeMdl(Mdl);
130 Irp->MdlAddress = NULL;
131 _SEH2_YIELD(return NULL);
132 }
133 _SEH2_END;
134 }
135
136 return MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
137 }
138
139 static
140 VOID
141 PerformTest(
142 ULONG TestId,
143 PDEVICE_OBJECT DeviceObject)
144 {
145 PVOID Bcb;
146 BOOLEAN Ret;
147 PULONG Buffer;
148 PTEST_FCB Fcb;
149 LARGE_INTEGER Offset;
150 IO_STATUS_BLOCK IoStatus;
151
152 ok_eq_pointer(TestFileObject, NULL);
153 ok_eq_pointer(TestDeviceObject, NULL);
154 ok_eq_ulong(TestTestId, -1);
155
156 TestDeviceObject = DeviceObject;
157 TestTestId = TestId;
158 TestFileObject = IoCreateStreamFileObject(NULL, DeviceObject);
159 if (!skip(TestFileObject != NULL, "Failed to allocate FO\n"))
160 {
161 Fcb = ExAllocatePool(NonPagedPool, sizeof(TEST_FCB));
162 if (!skip(Fcb != NULL, "ExAllocatePool failed\n"))
163 {
164 RtlZeroMemory(Fcb, sizeof(TEST_FCB));
165 ExInitializeFastMutex(&Fcb->HeaderMutex);
166 FsRtlSetupAdvancedHeader(&Fcb->Header, &Fcb->HeaderMutex);
167
168 TestFileObject->FsContext = Fcb;
169 TestFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
170 Fcb->Header.AllocationSize.QuadPart = VACB_MAPPING_GRANULARITY;
171 Fcb->Header.FileSize.QuadPart = VACB_MAPPING_GRANULARITY - PAGE_SIZE;
172 Fcb->Header.ValidDataLength.QuadPart = VACB_MAPPING_GRANULARITY - PAGE_SIZE;
173
174 if (TestId > 1 && TestId < 4)
175 {
176 Fcb->Header.AllocationSize.QuadPart = VACB_MAPPING_GRANULARITY - PAGE_SIZE;
177 }
178
179 KmtStartSeh();
180 CcInitializeCacheMap(TestFileObject, (PCC_FILE_SIZES)&Fcb->Header.AllocationSize, TRUE, &Callbacks, NULL);
181 KmtEndSeh(STATUS_SUCCESS);
182
183 if (!skip(CcIsFileCached(TestFileObject) == TRUE, "CcInitializeCacheMap failed\n"))
184 {
185 if (TestId == 0 || TestId == 2)
186 {
187 Offset.QuadPart = 0;
188 KmtStartSeh();
189 Ret = CcMapData(TestFileObject, &Offset, VACB_MAPPING_GRANULARITY - PAGE_SIZE, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
190 KmtEndSeh(STATUS_SUCCESS);
191
192 if (!skip(Ret == TRUE, "CcMapData failed\n"))
193 {
194 ok_eq_ulong(Buffer[(VACB_MAPPING_GRANULARITY - PAGE_SIZE - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
195 CcUnpinData(Bcb);
196 }
197
198 KmtStartSeh();
199 CcSetFileSizes(TestFileObject, &NewFileSizes);
200 KmtEndSeh(STATUS_SUCCESS);
201
202 Fcb->Header.AllocationSize.QuadPart = VACB_MAPPING_GRANULARITY;
203 Fcb->Header.FileSize.QuadPart = VACB_MAPPING_GRANULARITY;
204
205 Offset.QuadPart = 0;
206 KmtStartSeh();
207 Ret = CcMapData(TestFileObject, &Offset, VACB_MAPPING_GRANULARITY, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
208 KmtEndSeh(STATUS_SUCCESS);
209
210 if (!skip(Ret == TRUE, "CcMapData failed\n"))
211 {
212 ok_eq_ulong(Buffer[(VACB_MAPPING_GRANULARITY - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
213
214 CcUnpinData(Bcb);
215 }
216 }
217 else if (TestId == 1 || TestId == 3)
218 {
219 Buffer = ExAllocatePool(NonPagedPool, PAGE_SIZE);
220 if (!skip(Buffer != NULL, "ExAllocatePool failed\n"))
221 {
222 Ret = FALSE;
223 Offset.QuadPart = VACB_MAPPING_GRANULARITY - 2 * PAGE_SIZE;
224
225 KmtStartSeh();
226 Ret = CcCopyRead(TestFileObject, &Offset, PAGE_SIZE, TRUE, Buffer, &IoStatus);
227 KmtEndSeh(STATUS_SUCCESS);
228
229 ok_eq_ulong(Buffer[(PAGE_SIZE - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
230
231 KmtStartSeh();
232 CcSetFileSizes(TestFileObject, &NewFileSizes);
233 KmtEndSeh(STATUS_SUCCESS);
234
235 Fcb->Header.AllocationSize.QuadPart = VACB_MAPPING_GRANULARITY;
236 Fcb->Header.FileSize.QuadPart = VACB_MAPPING_GRANULARITY;
237 RtlZeroMemory(Buffer, PAGE_SIZE);
238
239 Offset.QuadPart = VACB_MAPPING_GRANULARITY - PAGE_SIZE;
240
241 KmtStartSeh();
242 Ret = CcCopyRead(TestFileObject, &Offset, PAGE_SIZE, TRUE, Buffer, &IoStatus);
243 KmtEndSeh(STATUS_SUCCESS);
244
245 ok_eq_ulong(Buffer[(PAGE_SIZE - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
246
247 ExFreePool(Buffer);
248 }
249 }
250 }
251 }
252 }
253 }
254
255
256 static
257 VOID
258 CleanupTest(
259 ULONG TestId,
260 PDEVICE_OBJECT DeviceObject)
261 {
262 LARGE_INTEGER Zero = RTL_CONSTANT_LARGE_INTEGER(0LL);
263 CACHE_UNINITIALIZE_EVENT CacheUninitEvent;
264
265 ok_eq_pointer(TestDeviceObject, DeviceObject);
266 ok_eq_ulong(TestTestId, TestId);
267
268 if (!skip(TestFileObject != NULL, "No test FO\n"))
269 {
270 if (CcIsFileCached(TestFileObject))
271 {
272 KeInitializeEvent(&CacheUninitEvent.Event, NotificationEvent, FALSE);
273 CcUninitializeCacheMap(TestFileObject, &Zero, &CacheUninitEvent);
274 KeWaitForSingleObject(&CacheUninitEvent.Event, Executive, KernelMode, FALSE, NULL);
275 }
276
277 if (TestFileObject->FsContext != NULL)
278 {
279 ExFreePool(TestFileObject->FsContext);
280 TestFileObject->FsContext = NULL;
281 TestFileObject->SectionObjectPointer = NULL;
282 }
283
284 ObDereferenceObject(TestFileObject);
285 }
286
287 TestFileObject = NULL;
288 TestDeviceObject = NULL;
289 TestTestId = -1;
290 }
291
292
293 static
294 NTSTATUS
295 TestMessageHandler(
296 _In_ PDEVICE_OBJECT DeviceObject,
297 _In_ ULONG ControlCode,
298 _In_opt_ PVOID Buffer,
299 _In_ SIZE_T InLength,
300 _Inout_ PSIZE_T OutLength)
301 {
302 NTSTATUS Status = STATUS_SUCCESS;
303
304 FsRtlEnterFileSystem();
305
306 switch (ControlCode)
307 {
308 case IOCTL_START_TEST:
309 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
310 PerformTest(*(PULONG)Buffer, DeviceObject);
311 break;
312
313 case IOCTL_FINISH_TEST:
314 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
315 CleanupTest(*(PULONG)Buffer, DeviceObject);
316 break;
317
318 default:
319 Status = STATUS_NOT_IMPLEMENTED;
320 break;
321 }
322
323 FsRtlExitFileSystem();
324
325 return Status;
326 }
327
328 static
329 NTSTATUS
330 TestIrpHandler(
331 _In_ PDEVICE_OBJECT DeviceObject,
332 _In_ PIRP Irp,
333 _In_ PIO_STACK_LOCATION IoStack)
334 {
335 NTSTATUS Status;
336
337 PAGED_CODE();
338
339 DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
340 ASSERT(IoStack->MajorFunction == IRP_MJ_READ);
341
342 FsRtlEnterFileSystem();
343
344 Status = STATUS_NOT_SUPPORTED;
345 Irp->IoStatus.Information = 0;
346
347 if (IoStack->MajorFunction == IRP_MJ_READ)
348 {
349 PMDL Mdl;
350 ULONG Length;
351 PVOID Buffer;
352 PTEST_FCB Fcb;
353 LARGE_INTEGER Offset;
354
355 Offset = IoStack->Parameters.Read.ByteOffset;
356 Length = IoStack->Parameters.Read.Length;
357 Fcb = IoStack->FileObject->FsContext;
358
359 ok_eq_pointer(DeviceObject, TestDeviceObject);
360 ok_eq_pointer(IoStack->FileObject, TestFileObject);
361 ok(Fcb != NULL, "Null FCB\n");
362
363 ok(FlagOn(Irp->Flags, IRP_NOCACHE), "Not coming from Cc\n");
364
365 ok_irql(APC_LEVEL);
366 ok((Offset.QuadPart % PAGE_SIZE == 0 || Offset.QuadPart == 0), "Offset is not aligned: %I64i\n", Offset.QuadPart);
367 ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);
368
369 ok(Irp->AssociatedIrp.SystemBuffer == NULL, "A SystemBuffer was allocated!\n");
370 Buffer = MapAndLockUserBuffer(Irp, Length);
371 ok(Buffer != NULL, "Null pointer!\n");
372
373 if (Offset.QuadPart < Fcb->Header.FileSize.QuadPart)
374 {
375 RtlFillMemory(Buffer, min(Length, Fcb->Header.FileSize.QuadPart - Offset.QuadPart), 0xBA);
376 Buffer = (PVOID)((ULONG_PTR)Buffer + (ULONG_PTR)min(Length, Fcb->Header.FileSize.QuadPart - Offset.QuadPart));
377
378 if (Length > (Fcb->Header.FileSize.QuadPart - Offset.QuadPart))
379 {
380 RtlFillMemory(Buffer, Length - Fcb->Header.FileSize.QuadPart, 0xBD);
381 }
382 }
383 else
384 {
385 RtlFillMemory(Buffer, Length, 0xBD);
386 }
387
388 Status = STATUS_SUCCESS;
389
390 Mdl = Irp->MdlAddress;
391 ok(Mdl != NULL, "Null pointer for MDL!\n");
392 ok((Mdl->MdlFlags & MDL_PAGES_LOCKED) != 0, "MDL not locked\n");
393 ok((Mdl->MdlFlags & MDL_SOURCE_IS_NONPAGED_POOL) == 0, "MDL from non paged\n");
394 ok((Mdl->MdlFlags & MDL_IO_PAGE_READ) != 0, "Non paging IO\n");
395 ok((Irp->Flags & IRP_PAGING_IO) != 0, "Non paging IO\n");
396
397 Irp->IoStatus.Information = Length;
398 }
399
400 if (Status == STATUS_PENDING)
401 {
402 IoMarkIrpPending(Irp);
403 IoCompleteRequest(Irp, IO_NO_INCREMENT);
404 Status = STATUS_PENDING;
405 }
406 else
407 {
408 Irp->IoStatus.Status = Status;
409 IoCompleteRequest(Irp, IO_NO_INCREMENT);
410 }
411
412 FsRtlExitFileSystem();
413
414 return Status;
415 }