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>
13 typedef struct _TEST_FCB
15 FSRTL_ADVANCED_FCB_HEADER Header
;
16 SECTION_OBJECT_POINTERS SectionObjectPointers
;
17 FAST_MUTEX HeaderMutex
;
18 } TEST_FCB
, *PTEST_FCB
;
20 static PFILE_OBJECT TestFileObject
;
21 static PDEVICE_OBJECT TestDeviceObject
;
22 static KMT_IRP_HANDLER TestIrpHandler
;
23 static FAST_IO_DISPATCH TestFastIoDispatch
;
29 _In_ PFILE_OBJECT FileObject
,
30 _In_ PLARGE_INTEGER FileOffset
,
35 _Out_ PIO_STATUS_BLOCK IoStatus
,
36 _In_ PDEVICE_OBJECT DeviceObject
)
38 IoStatus
->Status
= STATUS_NOT_SUPPORTED
;
44 _In_ PDRIVER_OBJECT DriverObject
,
45 _In_ PCUNICODE_STRING RegistryPath
,
46 _Out_ PCWSTR
*DeviceName
,
49 NTSTATUS Status
= STATUS_SUCCESS
;
53 UNREFERENCED_PARAMETER(RegistryPath
);
55 *DeviceName
= L
"CcCopyRead";
56 *Flags
= TESTENTRY_NO_EXCLUSIVE_DEVICE
|
57 TESTENTRY_BUFFERED_IO_DEVICE
|
58 TESTENTRY_NO_READONLY_DEVICE
;
60 KmtRegisterIrpHandler(IRP_MJ_CLEANUP
, NULL
, TestIrpHandler
);
61 KmtRegisterIrpHandler(IRP_MJ_CREATE
, NULL
, TestIrpHandler
);
62 KmtRegisterIrpHandler(IRP_MJ_READ
, NULL
, TestIrpHandler
);
64 TestFastIoDispatch
.FastIoRead
= FastIoRead
;
65 DriverObject
->FastIoDispatch
= &TestFastIoDispatch
;
73 _In_ PDRIVER_OBJECT DriverObject
)
106 ReleaseFromReadAhead(
112 static CACHE_MANAGER_CALLBACKS Callbacks
= {
114 ReleaseFromLazyWrite
,
116 ReleaseFromReadAhead
,
121 MapAndLockUserBuffer(
123 _In_ ULONG BufferLength
)
127 if (Irp
->MdlAddress
== NULL
)
129 Mdl
= IoAllocateMdl(Irp
->UserBuffer
, BufferLength
, FALSE
, FALSE
, Irp
);
137 MmProbeAndLockPages(Mdl
, Irp
->RequestorMode
, IoWriteAccess
);
139 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
142 Irp
->MdlAddress
= NULL
;
143 _SEH2_YIELD(return NULL
);
148 return MmGetSystemAddressForMdlSafe(Irp
->MdlAddress
, NormalPagePriority
);
155 _In_ PDEVICE_OBJECT DeviceObject
,
157 _In_ PIO_STACK_LOCATION IoStack
)
161 CACHE_UNINITIALIZE_EVENT CacheUninitEvent
;
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
);
170 Status
= STATUS_NOT_SUPPORTED
;
171 Irp
->IoStatus
.Information
= 0;
173 if (IoStack
->MajorFunction
== IRP_MJ_CREATE
)
175 if (IoStack
->FileObject
->FileName
.Length
>= 2 * sizeof(WCHAR
))
177 TestDeviceObject
= DeviceObject
;
178 TestFileObject
= IoStack
->FileObject
;
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')
187 Fcb
->Header
.AllocationSize
.QuadPart
= 1000000;
188 Fcb
->Header
.FileSize
.QuadPart
= 1000000;
189 Fcb
->Header
.ValidDataLength
.QuadPart
= 1000000;
191 else if (IoStack
->FileObject
->FileName
.Length
>= 2 * sizeof(WCHAR
) &&
192 IoStack
->FileObject
->FileName
.Buffer
[1] == 'S')
194 Fcb
->Header
.AllocationSize
.QuadPart
= 1004;
195 Fcb
->Header
.FileSize
.QuadPart
= 1004;
196 Fcb
->Header
.ValidDataLength
.QuadPart
= 1004;
198 else if (IoStack
->FileObject
->FileName
.Length
>= 2 * sizeof(WCHAR
) &&
199 IoStack
->FileObject
->FileName
.Buffer
[1] == 'R')
201 Fcb
->Header
.AllocationSize
.QuadPart
= 62;
202 Fcb
->Header
.FileSize
.QuadPart
= 62;
203 Fcb
->Header
.ValidDataLength
.QuadPart
= 62;
207 Fcb
->Header
.AllocationSize
.QuadPart
= 512;
208 Fcb
->Header
.FileSize
.QuadPart
= 512;
209 Fcb
->Header
.ValidDataLength
.QuadPart
= 512;
211 Fcb
->Header
.IsFastIoPossible
= FastIoIsNotPossible
;
212 IoStack
->FileObject
->FsContext
= Fcb
;
213 IoStack
->FileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
215 CcInitializeCacheMap(IoStack
->FileObject
,
216 (PCC_FILE_SIZES
)&Fcb
->Header
.AllocationSize
,
217 FALSE
, &Callbacks
, NULL
);
219 Irp
->IoStatus
.Information
= FILE_OPENED
;
220 Status
= STATUS_SUCCESS
;
222 else if (IoStack
->MajorFunction
== IRP_MJ_READ
)
227 LARGE_INTEGER Offset
;
229 Offset
= IoStack
->Parameters
.Read
.ByteOffset
;
230 Length
= IoStack
->Parameters
.Read
.Length
;
231 Fcb
= IoStack
->FileObject
->FsContext
;
233 ok_eq_pointer(DeviceObject
, TestDeviceObject
);
234 ok_eq_pointer(IoStack
->FileObject
, TestFileObject
);
236 if (!FlagOn(Irp
->Flags
, IRP_NOCACHE
))
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
);
241 Buffer
= Irp
->AssociatedIrp
.SystemBuffer
;
242 ok(Buffer
!= NULL
, "Null pointer!\n");
246 Ret
= CcCopyRead(IoStack
->FileObject
, &Offset
, Length
, TRUE
, Buffer
,
248 ok_bool_true(Ret
, "CcCopyRead");
250 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
252 Irp
->IoStatus
.Status
= _SEH2_GetExceptionCode();
256 Status
= Irp
->IoStatus
.Status
;
258 if (NT_SUCCESS(Status
))
260 if (Offset
.QuadPart
<= 1000LL && Offset
.QuadPart
+ Length
> 1000LL)
262 ok_eq_hex(*(PUSHORT
)((ULONG_PTR
)Buffer
+ (ULONG_PTR
)(1000LL - Offset
.QuadPart
)), 0xFFFF);
266 ok_eq_hex(*(PUSHORT
)Buffer
, 0xBABA);
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
);
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);
280 Status
= STATUS_SUCCESS
;
281 if (Offset
.QuadPart
<= 1000LL && Offset
.QuadPart
+ Length
> 1000LL)
283 *(PUSHORT
)((ULONG_PTR
)Buffer
+ (ULONG_PTR
)(1000LL - Offset
.QuadPart
)) = 0xFFFF;
287 if (NT_SUCCESS(Status
))
289 Irp
->IoStatus
.Information
= Length
;
290 IoStack
->FileObject
->CurrentByteOffset
.QuadPart
= Offset
.QuadPart
+ Length
;
293 else if (IoStack
->MajorFunction
== IRP_MJ_CLEANUP
)
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
;
304 if (Status
== STATUS_PENDING
)
306 IoMarkIrpPending(Irp
);
307 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
308 Status
= STATUS_PENDING
;
312 Irp
->IoStatus
.Status
= Status
;
313 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);