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