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
,
124 if (Irp
->MdlAddress
== NULL
)
126 return Irp
->UserBuffer
;
130 return MmGetSystemAddressForMdlSafe(Irp
->MdlAddress
, NormalPagePriority
);
138 _In_ PDEVICE_OBJECT DeviceObject
,
140 _In_ PIO_STACK_LOCATION IoStack
)
144 CACHE_UNINITIALIZE_EVENT CacheUninitEvent
;
148 DPRINT("IRP %x/%x\n", IoStack
->MajorFunction
, IoStack
->MinorFunction
);
149 ASSERT(IoStack
->MajorFunction
== IRP_MJ_CLEANUP
||
150 IoStack
->MajorFunction
== IRP_MJ_CREATE
||
151 IoStack
->MajorFunction
== IRP_MJ_READ
);
153 Status
= STATUS_NOT_SUPPORTED
;
154 Irp
->IoStatus
.Information
= 0;
156 if (IoStack
->MajorFunction
== IRP_MJ_CREATE
)
158 if (IoStack
->FileObject
->FileName
.Length
>= 2 * sizeof(WCHAR
))
160 TestDeviceObject
= DeviceObject
;
161 TestFileObject
= IoStack
->FileObject
;
163 Fcb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(*Fcb
), 'FwrI');
164 RtlZeroMemory(Fcb
, sizeof(*Fcb
));
165 ExInitializeFastMutex(&Fcb
->HeaderMutex
);
166 FsRtlSetupAdvancedHeader(&Fcb
->Header
, &Fcb
->HeaderMutex
);
167 if (IoStack
->FileObject
->FileName
.Length
>= 2 * sizeof(WCHAR
) &&
168 IoStack
->FileObject
->FileName
.Buffer
[1] == 'B')
170 Fcb
->Header
.AllocationSize
.QuadPart
= 1000000;
171 Fcb
->Header
.FileSize
.QuadPart
= 1000000;
172 Fcb
->Header
.ValidDataLength
.QuadPart
= 1000000;
174 else if (IoStack
->FileObject
->FileName
.Length
>= 2 * sizeof(WCHAR
) &&
175 IoStack
->FileObject
->FileName
.Buffer
[1] == 'S')
177 Fcb
->Header
.AllocationSize
.QuadPart
= 3000;
178 Fcb
->Header
.FileSize
.QuadPart
= 3000;
179 Fcb
->Header
.ValidDataLength
.QuadPart
= 3000;
183 Fcb
->Header
.AllocationSize
.QuadPart
= 512;
184 Fcb
->Header
.FileSize
.QuadPart
= 512;
185 Fcb
->Header
.ValidDataLength
.QuadPart
= 512;
187 Fcb
->Header
.IsFastIoPossible
= FALSE
;
188 IoStack
->FileObject
->FsContext
= Fcb
;
189 IoStack
->FileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
191 CcInitializeCacheMap(IoStack
->FileObject
,
192 (PCC_FILE_SIZES
)&Fcb
->Header
.AllocationSize
,
193 FALSE
, &Callbacks
, NULL
);
195 Irp
->IoStatus
.Information
= FILE_OPENED
;
196 Status
= STATUS_SUCCESS
;
198 else if (IoStack
->MajorFunction
== IRP_MJ_READ
)
203 LARGE_INTEGER Offset
;
205 Offset
= IoStack
->Parameters
.Read
.ByteOffset
;
206 Length
= IoStack
->Parameters
.Read
.Length
;
207 Fcb
= IoStack
->FileObject
->FsContext
;
209 ok_eq_pointer(DeviceObject
, TestDeviceObject
);
210 ok_eq_pointer(IoStack
->FileObject
, TestFileObject
);
212 if (!FlagOn(Irp
->Flags
, IRP_NOCACHE
))
214 ok(Offset
.QuadPart
% 512 != 0, "Offset is aligned: %I64i\n", Offset
.QuadPart
);
215 ok(Length
% 512 != 0, "Length is aligned: %I64i\n", Length
);
217 Buffer
= MapUserBuffer(Irp
);
218 ok(Buffer
!= NULL
, "Null pointer!\n");
222 Ret
= CcCopyRead(IoStack
->FileObject
, &Offset
, Length
, TRUE
, Buffer
,
224 ok_bool_true(Ret
, "CcCopyRead");
226 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
228 Irp
->IoStatus
.Status
= _SEH2_GetExceptionCode();
232 Status
= Irp
->IoStatus
.Status
;
236 ok(Offset
.QuadPart
% 512 == 0, "Offset is not aligned: %I64i\n", Offset
.QuadPart
);
237 ok(Length
% 512 == 0, "Length is not aligned: %I64i\n", Length
);
239 Status
= STATUS_SUCCESS
;
242 if (NT_SUCCESS(Status
))
244 Irp
->IoStatus
.Information
= Length
;
245 IoStack
->FileObject
->CurrentByteOffset
.QuadPart
= Offset
.QuadPart
+ Length
;
248 else if (IoStack
->MajorFunction
== IRP_MJ_CLEANUP
)
250 KeInitializeEvent(&CacheUninitEvent
.Event
, NotificationEvent
, FALSE
);
251 CcUninitializeCacheMap(IoStack
->FileObject
, NULL
, &CacheUninitEvent
);
252 KeWaitForSingleObject(&CacheUninitEvent
.Event
, Executive
, KernelMode
, FALSE
, NULL
);
253 Fcb
= IoStack
->FileObject
->FsContext
;
254 ExFreePoolWithTag(Fcb
, 'FwrI');
255 IoStack
->FileObject
->FsContext
= NULL
;
256 Status
= STATUS_SUCCESS
;
259 if (Status
== STATUS_PENDING
)
261 IoMarkIrpPending(Irp
);
262 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
263 Status
= STATUS_PENDING
;
267 Irp
->IoStatus
.Status
= Status
;
268 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);