- Implemented InterlockedBitTestAndReset, InterlockedBitTestAndSet, InterlockedExchan...
[reactos.git] / reactos / ntoskrnl / fs / util.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/fs/util.c
5 * PURPOSE: Misc Utility Functions for File System Drivers
6 *
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ntoskrnl.h>
13 #include <internal/debug.h>
14
15 #define FSRTL_MAX_RESOURCES 16
16
17 #define FTTYPE ((ULONG)'f')
18 #define FT_BALANCED_READ_MODE \
19 CTL_CODE(FTTYPE, 6, METHOD_NEITHER, FILE_ANY_ACCESS)
20
21 /* GLOBALS *******************************************************************/
22
23 BOOLEAN STDCALL MmIsFileAPagingFile(PFILE_OBJECT FileObject);
24 VOID STDCALL INIT_FUNCTION RtlpInitializeResources(VOID);
25 static ULONG FsRtlpAllocatedResources = 0;
26 static PERESOURCE FsRtlpResources;
27
28 #if defined (ALLOC_PRAGMA)
29 #pragma alloc_text(INIT, RtlpInitializeResources)
30 #endif
31
32 /* PRIVATE FUNCTIONS**********************************************************/
33
34 VOID
35 STDCALL
36 INIT_FUNCTION
37 RtlpInitializeResources(VOID)
38 {
39 ULONG i;
40
41 /* Allocate the Resource Buffer */
42 FsRtlpResources = FsRtlAllocatePool(NonPagedPool,
43 FSRTL_MAX_RESOURCES*sizeof(ERESOURCE));
44
45 /* Initialize the Resources */
46 for (i = 0; i < FSRTL_MAX_RESOURCES; i++)
47 {
48 ExInitializeResource(&FsRtlpResources[i]);
49 }
50 }
51
52 /* FUNCTIONS *****************************************************************/
53
54 /*++
55 * @name FsRtlIsTotalDeviceFailure
56 * @implemented NT 4.0
57 *
58 * The FsRtlIsTotalDeviceFailure routine checks if an NTSTATUS error code
59 * represents a disk hardware failure.
60 *
61 * @param NtStatus
62 * The NTSTATUS Code to Test
63 *
64 * @return TRUE in case of Hardware Failure, FALSE otherwise.
65 *
66 * @remarks None.
67 *
68 *--*/
69 BOOLEAN
70 STDCALL
71 FsRtlIsTotalDeviceFailure(IN NTSTATUS NtStatus)
72 {
73 return((NT_SUCCESS(NtStatus)) ||
74 (STATUS_CRC_ERROR == NtStatus) ||
75 (STATUS_DEVICE_DATA_ERROR == NtStatus) ? FALSE : TRUE);
76 }
77
78 /*++
79 * @name FsRtlIsNtstatusExpected
80 * @implemented NT 4.0
81 *
82 * The FsRtlIsNtstatusExpected routine checks if an NTSTATUS error code
83 * is expected by the File System Support Library.
84 *
85 * @param NtStatus
86 * The NTSTATUS Code to Test
87 *
88 * @return TRUE if the Value is Expected, FALSE otherwise.
89 *
90 * @remarks None.
91 *
92 *--*/
93 BOOLEAN
94 STDCALL
95 FsRtlIsNtstatusExpected(IN NTSTATUS NtStatus)
96 {
97 return((STATUS_DATATYPE_MISALIGNMENT == NtStatus) ||
98 (STATUS_ACCESS_VIOLATION == NtStatus) ||
99 (STATUS_ILLEGAL_INSTRUCTION == NtStatus) ||
100 (STATUS_INSTRUCTION_MISALIGNMENT == NtStatus)) ? FALSE : TRUE;
101 }
102
103 /*++
104 * @name FsRtlIsPagingFile
105 * @implemented NT 4.0
106 *
107 * The FsRtlIsPagingFile routine checks if the FileObject is a Paging File.
108 *
109 * @param FileObject
110 * A pointer to the File Object to be tested.
111 *
112 * @return TRUE if the File is a Paging File, FALSE otherwise.
113 *
114 * @remarks None.
115 *
116 *--*/
117 BOOLEAN
118 STDCALL
119 FsRtlIsPagingFile(IN PFILE_OBJECT FileObject)
120 {
121 return MmIsFileAPagingFile(FileObject);
122 }
123
124 /*++
125 * @name FsRtlNormalizeNtstatus
126 * @implemented NT 4.0
127 *
128 * The FsRtlNormalizeNtstatus routine normalizes an NTSTATUS error code.
129 *
130 * @param NtStatusToNormalize
131 * The NTSTATUS error code to Normalize.
132 *
133 * @param NormalizedNtStatus
134 * The NTSTATUS error code to return if the NtStatusToNormalize is not
135 * a proper expected error code by the File System Library.
136 *
137 * @return NtStatusToNormalize if it is an expected value, otherwise
138 * NormalizedNtStatus.
139 *
140 * @remarks None.
141 *
142 *--*/
143 NTSTATUS
144 STDCALL
145 FsRtlNormalizeNtstatus(IN NTSTATUS NtStatusToNormalize,
146 IN NTSTATUS NormalizedNtStatus)
147 {
148 return(TRUE == FsRtlIsNtstatusExpected(NtStatusToNormalize)) ?
149 NtStatusToNormalize : NormalizedNtStatus;
150 }
151
152 /*++
153 * @name FsRtlAllocateResource
154 * @implemented NT 4.0
155 *
156 * The FsRtlAllocateResource routine returns a pre-initialized ERESOURCE
157 * for use by a File System Driver.
158 *
159 * @return A Pointer to a pre-initialized ERESOURCE.
160 *
161 * @remarks The File System Library only provides up to 16 Resources.
162 *
163 *--*/
164 PERESOURCE
165 STDCALL
166 FsRtlAllocateResource(VOID)
167 {
168 /* Return a pre-allocated ERESOURCE */
169 return &FsRtlpResources[FsRtlpAllocatedResources++ & FSRTL_MAX_RESOURCES];
170 }
171
172 /*++
173 * @name FsRtlBalanceReads
174 * @implemented NT 4.0
175 *
176 * The FsRtlBalanceReads routine sends an IRP to an FTDISK Driver
177 * requesting the driver to balance read requests across a mirror set.
178 *
179 * @param TargetDevice
180 * A pointer to an FTDISK Device Object.
181 *
182 * @return The NTSTATUS error code returned by the FTDISK Driver.
183 *
184 * @remarks FTDISK is a Software RAID Implementation.
185 *
186 *--*/
187 NTSTATUS
188 STDCALL
189 FsRtlBalanceReads(PDEVICE_OBJECT TargetDevice)
190 {
191 PIRP Irp;
192 KEVENT Event;
193 IO_STATUS_BLOCK IoStatusBlock;
194 NTSTATUS Status;
195
196 /* Initialize the Local Event */
197 KeInitializeEvent(&Event, NotificationEvent, FALSE);
198
199 /* Build the special IOCTL */
200 Irp = IoBuildDeviceIoControlRequest(FT_BALANCED_READ_MODE,
201 TargetDevice,
202 NULL,
203 0,
204 NULL,
205 0,
206 FALSE,
207 &Event,
208 &IoStatusBlock);
209
210 /* Send it */
211 Status = IoCallDriver(TargetDevice, Irp);
212
213 /* Wait if needed */
214 if (Status == STATUS_PENDING)
215 {
216 Status = KeWaitForSingleObject(&Event,
217 Executive,
218 KernelMode,
219 FALSE,
220 NULL);
221 /* Return Status */
222 Status = IoStatusBlock.Status;
223 }
224
225 return Status;
226 }
227
228 /*++
229 * @name FsRtlPostPagingFileStackOverflow
230 * @unimplemented NT 4.0
231 *
232 * The FsRtlPostPagingFileStackOverflow routine
233 *
234 * @param Context
235 *
236 * @param Event
237 *
238 * @param StackOverflowRoutine
239 *
240 * @return
241 *
242 * @remarks None.
243 *
244 *--*/
245 VOID
246 STDCALL
247 FsRtlPostPagingFileStackOverflow(IN PVOID Context,
248 IN PKEVENT Event,
249 IN PFSRTL_STACK_OVERFLOW_ROUTINE StackOverflowRoutine)
250 {
251 UNIMPLEMENTED;
252 }
253
254 /*++
255 * @name FsRtlPostStackOverflow
256 * @unimplemented NT 4.0
257 *
258 * The FsRtlPostStackOverflow routine
259 *
260 * @param Context
261 *
262 * @param Event
263 *
264 * @param StackOverflowRoutine
265 *
266 * @return
267 *
268 * @remarks None.
269 *
270 *--*/
271 VOID
272 STDCALL
273 FsRtlPostStackOverflow(IN PVOID Context,
274 IN PKEVENT Event,
275 IN PFSRTL_STACK_OVERFLOW_ROUTINE StackOverflowRoutine)
276 {
277 UNIMPLEMENTED;
278 }
279
280 /*++
281 * @name FsRtlSyncVolumes
282 * @implemented NT 4.0
283 *
284 * The FsRtlSyncVolumes routine is deprecated.
285 *
286 * @return Always returns STATUS_SUCCESS.
287 *
288 * @remarks Deprecated.
289 *
290 *--*/
291 NTSTATUS
292 STDCALL
293 FsRtlSyncVolumes(DWORD Unknown0,
294 DWORD Unknown1,
295 DWORD Unknown2)
296 {
297 return STATUS_SUCCESS;
298 }
299
300 /* EOF */