deafda2b51f88485a090772da359a7dab363641e
[reactos.git] / reactos / drivers / filesystems / ext2 / src / cmcb.c
1 /*
2 * COPYRIGHT: See COPYRIGHT.TXT
3 * PROJECT: Ext2 File System Driver for WinNT/2K/XP
4 * FILE: cmcb.c
5 * PROGRAMMER: Matt Wu <mattwu@163.com>
6 * HOMEPAGE: http://www.ext2fsd.com
7 * UPDATE HISTORY:
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include "ext2fs.h"
13
14 /* GLOBALS ***************************************************************/
15
16 extern PEXT2_GLOBAL Ext2Global;
17
18 /* DEFINITIONS *************************************************************/
19
20 #define CMCB_DEBUG_LEVEL DL_NVR
21
22 BOOLEAN NTAPI
23 Ext2AcquireForLazyWrite (
24 IN PVOID Context,
25 IN BOOLEAN Wait)
26 {
27 //
28 // On a readonly filesystem this function still has to exist but it
29 // doesn't need to do anything.
30
31 PEXT2_FCB Fcb;
32
33 Fcb = (PEXT2_FCB) Context;
34 ASSERT(Fcb != NULL);
35 ASSERT((Fcb->Identifier.Type == EXT2FCB) &&
36 (Fcb->Identifier.Size == sizeof(EXT2_FCB)));
37 #if EXT2_DEBUG
38 DEBUG(CMCB_DEBUG_LEVEL, ("Ext2AcquireForLazyWrite: %s %s Fcb=%p\n",
39 Ext2GetCurrentProcessName(), "ACQUIRE_FOR_LAZY_WRITE", Fcb));
40 #endif
41 if (!ExAcquireResourceSharedLite(
42 &Fcb->PagingIoResource, Wait)) {
43 return FALSE;
44 }
45
46 ASSERT(Fcb->LazyWriterThread == NULL);
47 Fcb->LazyWriterThread = PsGetCurrentThread();
48
49 ASSERT(IoGetTopLevelIrp() == NULL);
50
51 IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
52
53 return TRUE;
54 }
55
56 VOID NTAPI
57 Ext2ReleaseFromLazyWrite (IN PVOID Context)
58 {
59 //
60 // On a readonly filesystem this function still has to exist but it
61 // doesn't need to do anything.
62 PEXT2_FCB Fcb;
63
64 Fcb = (PEXT2_FCB) Context;
65
66 ASSERT(Fcb != NULL);
67
68 ASSERT((Fcb->Identifier.Type == EXT2FCB) &&
69 (Fcb->Identifier.Size == sizeof(EXT2_FCB)));
70 #if EXT2_DEBUG
71 DEBUG(CMCB_DEBUG_LEVEL, ( "Ext2ReleaseFromLazyWrite: %s %s Fcb=%p\n",
72 Ext2GetCurrentProcessName(), "RELEASE_FROM_LAZY_WRITE", Fcb));
73 #endif
74 ASSERT(Fcb->LazyWriterThread == PsGetCurrentThread());
75 Fcb->LazyWriterThread = NULL;
76
77 ExReleaseResourceLite(&Fcb->PagingIoResource);
78
79 ASSERT(IoGetTopLevelIrp() == (PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
80 IoSetTopLevelIrp( NULL );
81 }
82
83 BOOLEAN NTAPI
84 Ext2AcquireForReadAhead (IN PVOID Context,
85 IN BOOLEAN Wait)
86 {
87 PEXT2_FCB Fcb;
88
89 Fcb = (PEXT2_FCB) Context;
90
91 ASSERT(Fcb != NULL);
92
93 ASSERT((Fcb->Identifier.Type == EXT2FCB) &&
94 (Fcb->Identifier.Size == sizeof(EXT2_FCB)));
95
96 DEBUG(CMCB_DEBUG_LEVEL, ("Ext2AcquireForReadAhead: i=%xh Fcb=%p\n",
97 Fcb->Mcb->Inode.i_ino, Fcb));
98
99 if (!ExAcquireResourceSharedLite(
100 &Fcb->MainResource, Wait ))
101 return FALSE;
102
103 ASSERT(IoGetTopLevelIrp() == NULL);
104
105 IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
106
107 return TRUE;
108 }
109
110 VOID NTAPI
111 Ext2ReleaseFromReadAhead (IN PVOID Context)
112 {
113 PEXT2_FCB Fcb;
114
115 Fcb = (PEXT2_FCB) Context;
116
117 ASSERT(Fcb != NULL);
118
119 ASSERT((Fcb->Identifier.Type == EXT2FCB) &&
120 (Fcb->Identifier.Size == sizeof(EXT2_FCB)));
121
122 DEBUG(CMCB_DEBUG_LEVEL, ("Ext2ReleaseFromReadAhead: i=%xh Fcb=%p\n",
123 Fcb->Mcb->Inode.i_ino, Fcb));
124
125 IoSetTopLevelIrp( NULL );
126
127 ExReleaseResourceLite(&Fcb->MainResource);
128 }
129
130 BOOLEAN NTAPI
131 Ext2NoOpAcquire (
132 IN PVOID Fcb,
133 IN BOOLEAN Wait
134 )
135 {
136 ASSERT(IoGetTopLevelIrp() == NULL);
137 IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
138 return TRUE;
139 }
140
141 VOID NTAPI
142 Ext2NoOpRelease (
143 IN PVOID Fcb
144 )
145 {
146 ASSERT(IoGetTopLevelIrp() == (PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
147 IoSetTopLevelIrp( NULL );
148
149 return;
150 }
151
152
153 VOID NTAPI
154 Ext2AcquireForCreateSection (
155 IN PFILE_OBJECT FileObject
156 )
157
158 {
159 PEXT2_FCB Fcb = FileObject->FsContext;
160
161 if (Fcb->Header.Resource != NULL) {
162 ExAcquireResourceExclusiveLite(Fcb->Header.Resource, TRUE);
163 }
164
165 DEBUG(CMCB_DEBUG_LEVEL, ("Ext2AcquireForCreateSection: Fcb=%p\n", Fcb));
166 }
167
168 VOID NTAPI
169 Ext2ReleaseForCreateSection (
170 IN PFILE_OBJECT FileObject
171 )
172
173 {
174 PEXT2_FCB Fcb = FileObject->FsContext;
175
176 DEBUG(CMCB_DEBUG_LEVEL, ("Ext2ReleaseForCreateSection: Fcb=%p\n", Fcb));
177
178 if (Fcb->Header.Resource != NULL) {
179 ExReleaseResourceLite(Fcb->Header.Resource);
180 }
181 }
182
183
184 NTSTATUS NTAPI
185 Ext2AcquireFileForModWrite (
186 IN PFILE_OBJECT FileObject,
187 IN PLARGE_INTEGER EndingOffset,
188 OUT PERESOURCE *ResourceToRelease,
189 IN PDEVICE_OBJECT DeviceObject
190 )
191
192 {
193 BOOLEAN ResourceAcquired = FALSE;
194
195 PEXT2_FCB Fcb = FileObject->FsContext;
196
197 if (Fcb->Header.PagingIoResource != NULL) {
198 *ResourceToRelease = Fcb->Header.PagingIoResource;
199 } else {
200 *ResourceToRelease = Fcb->Header.Resource;
201 }
202
203 ResourceAcquired = ExAcquireResourceSharedLite(*ResourceToRelease, FALSE);
204 if (!ResourceAcquired) {
205 *ResourceToRelease = NULL;
206 }
207
208 DEBUG(CMCB_DEBUG_LEVEL, ("Ext2AcquireFileForModWrite: Fcb=%p Acquired=%d\n",
209 Fcb, ResourceAcquired));
210
211 return (ResourceAcquired ? STATUS_SUCCESS : STATUS_CANT_WAIT);
212 }
213
214 NTSTATUS NTAPI
215 Ext2ReleaseFileForModWrite (
216 IN PFILE_OBJECT FileObject,
217 IN PERESOURCE ResourceToRelease,
218 IN PDEVICE_OBJECT DeviceObject
219 )
220 {
221 PEXT2_FCB Fcb = FileObject->FsContext;
222
223 DEBUG(CMCB_DEBUG_LEVEL, ("Ext2ReleaseFileForModWrite: Fcb=%p\n", Fcb));
224
225 if (ResourceToRelease != NULL) {
226 ASSERT(ResourceToRelease == Fcb->Header.PagingIoResource ||
227 ResourceToRelease == Fcb->Header.Resource);
228 ExReleaseResourceLite(ResourceToRelease);
229 } else {
230 DbgBreak();
231 }
232
233 return STATUS_SUCCESS;
234 }
235
236 NTSTATUS NTAPI
237 Ext2AcquireFileForCcFlush (
238 IN PFILE_OBJECT FileObject,
239 IN PDEVICE_OBJECT DeviceObject
240 )
241 {
242 PEXT2_FCB Fcb = FileObject->FsContext;
243
244 if (Fcb->Header.PagingIoResource != NULL) {
245 ExAcquireResourceSharedLite(Fcb->Header.PagingIoResource, TRUE);
246 }
247
248 DEBUG(CMCB_DEBUG_LEVEL, ("Ext2AcquireFileForCcFlush: Fcb=%p\n", Fcb));
249
250 return STATUS_SUCCESS;
251 }
252
253 NTSTATUS NTAPI
254 Ext2ReleaseFileForCcFlush (
255 IN PFILE_OBJECT FileObject,
256 IN PDEVICE_OBJECT DeviceObject
257 )
258 {
259 PEXT2_FCB Fcb = FileObject->FsContext;
260
261 DEBUG(CMCB_DEBUG_LEVEL, ("Ext2ReleaseFileForCcFlush: Fcb=%p\n", Fcb));
262
263 if (Fcb->Header.PagingIoResource != NULL) {
264 ExReleaseResourceLite(Fcb->Header.PagingIoResource);
265 }
266
267 return STATUS_SUCCESS;
268 }