Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers...
[reactos.git] / drivers / filesystems / cdfs_new / cleanup.c
1 /*++
2
3 Copyright (c) 1989-2000 Microsoft Corporation
4
5 Module Name:
6
7 Cleanup.c
8
9 Abstract:
10
11 This module implements the File Cleanup routine for Cdfs called by the
12 dispatch driver.
13
14
15 --*/
16
17 #include "CdProcs.h"
18
19 //
20 // The Bug check file id for this module
21 //
22
23 #define BugCheckFileId (CDFS_BUG_CHECK_CLEANUP)
24
25
26 NTSTATUS
27 CdCommonCleanup (
28 IN PIRP_CONTEXT IrpContext,
29 IN PIRP Irp
30 )
31
32 /*++
33
34 Routine Description:
35
36 This is the common routine for cleanup of a file/directory called by both
37 the fsd and fsp threads.
38
39 Cleanup is invoked whenever the last handle to a file object is closed.
40 This is different than the Close operation which is invoked when the last
41 reference to a file object is deleted.
42
43 The function of cleanup is to essentially "cleanup" the file/directory
44 after a user is done with it. The Fcb/Dcb remains around (because MM
45 still has the file object referenced) but is now available for another
46 user to open (i.e., as far as the user is concerned the is now closed).
47
48 See close for a more complete description of what close does.
49
50 We do no synchronization in this routine until we get to the point
51 where we modify the counts, share access and volume lock field.
52
53 We need to update the Fcb and Vcb to show that a user handle has been closed.
54 The following structures and fields are affected.
55
56 Vcb:
57
58 VolumeLockFileObject - Did the user lock the volume with this file object.
59 VcbState - Check if we are unlocking the volume here.
60 VcbCleanup - Count of outstanding handles on the volume.
61 DirNotifyQueue - If this file object has pending DirNotify Irps.
62
63 Fcb:
64
65 ShareAccess - If this is a user handle.
66 FcbCleanup - Count of outstanding handles on this Fcb.
67 Oplock - Any outstanding oplocks on this file object.
68 FileLock - Any outstanding filelocks on this file object.
69
70 Arguments:
71
72 Irp - Supplies the Irp to process
73
74 Return Value:
75
76 NTSTATUS - The return status for the operation.
77
78 --*/
79
80 {
81 PFILE_OBJECT FileObject;
82 TYPE_OF_OPEN TypeOfOpen;
83
84 BOOLEAN SendUnlockNotification = FALSE;
85 BOOLEAN AttemptTeardown;
86 BOOLEAN VcbAcquired = FALSE;
87
88 PVCB Vcb;
89 PFCB Fcb;
90 PCCB Ccb;
91
92 KIRQL SavedIrql;
93
94 ASSERT_IRP_CONTEXT( IrpContext );
95 ASSERT_IRP( Irp );
96
97 //
98 // If we were called with our file system device object instead of a
99 // volume device object, just complete this request with STATUS_SUCCESS.
100 //
101
102 if (IrpContext->Vcb == NULL) {
103
104 CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
105 return STATUS_SUCCESS;
106 }
107
108 //
109 // Get the file object out of the Irp and decode the type of open.
110 //
111
112 FileObject = IoGetCurrentIrpStackLocation( Irp )->FileObject;
113
114 TypeOfOpen = CdDecodeFileObject( IrpContext,
115 FileObject,
116 &Fcb,
117 &Ccb );
118
119 //
120 // No work here for either an UnopenedFile object or a StreamFileObject.
121 //
122
123 if (TypeOfOpen <= StreamFileOpen) {
124
125 CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
126
127 return STATUS_SUCCESS;
128 }
129
130 //
131 // Keep a local pointer to the Vcb.
132 //
133
134 Vcb = Fcb->Vcb;
135
136 //
137 // Synchronise with reads while we set the cleanup complete
138 // flag on this fileobject. Once this flag is set, any further
139 // reads will be rejected (CdVerifyFcbOperation)
140 //
141
142 CdAcquireFileExclusive( IrpContext, Fcb);
143
144 //
145 // Set the flag in the FileObject to indicate that cleanup is complete.
146 //
147
148 SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );
149
150 CdReleaseFile( IrpContext, Fcb);
151
152 //
153 // Acquire the current file.
154 //
155
156 CdAcquireFcbExclusive( IrpContext, Fcb, FALSE );
157
158 //
159 // Use a try-finally to facilitate cleanup.
160 //
161
162 //try { /* ReactOS Change: Manual removal of SEH since macros to hack around it don't allow multiple SEH usage within one function */
163
164 //
165 // Case on the type of open that we are trying to cleanup.
166 //
167
168 switch (TypeOfOpen) {
169
170 case UserDirectoryOpen:
171
172 //
173 // Check if we need to complete any dir notify Irps on this file object.
174 //
175
176 FsRtlNotifyCleanup( Vcb->NotifySync,
177 &Vcb->DirNotifyList,
178 Ccb );
179
180 break;
181
182 case UserFileOpen:
183
184 //
185 // Coordinate the cleanup operation with the oplock state.
186 // Oplock cleanup operations can always cleanup immediately so no
187 // need to check for STATUS_PENDING.
188 //
189
190 FsRtlCheckOplock( &Fcb->Oplock,
191 Irp,
192 IrpContext,
193 NULL,
194 NULL );
195
196 //
197 // Unlock all outstanding file locks.
198 //
199
200 if (Fcb->FileLock != NULL) {
201
202 FsRtlFastUnlockAll( Fcb->FileLock,
203 FileObject,
204 IoGetRequestorProcess( Irp ),
205 NULL );
206 }
207
208 //
209 // Cleanup the cache map.
210 //
211
212 CcUninitializeCacheMap( FileObject, NULL, NULL );
213
214 //
215 // Check the fast io state.
216 //
217
218 CdLockFcb( IrpContext, Fcb );
219 Fcb->IsFastIoPossible = CdIsFastIoPossible( Fcb );
220 CdUnlockFcb( IrpContext, Fcb );
221
222 break;
223
224 case UserVolumeOpen :
225
226 break;
227
228 default :
229
230 CdBugCheck( TypeOfOpen, 0, 0 );
231 }
232
233 //
234 // Now lock the Vcb in order to modify the fields in the in-memory
235 // structures.
236 //
237
238 CdLockVcb( IrpContext, Vcb );
239
240 //
241 // Decrement the cleanup counts in the Vcb and Fcb.
242 //
243
244 CdDecrementCleanupCounts( IrpContext, Fcb );
245
246 //
247 // If the cleanup count hit zero and the volume is not mounted, we
248 // will want to try to spark teardown.
249 //
250
251 AttemptTeardown = (Vcb->VcbCleanup == 0 && Vcb->VcbCondition == VcbNotMounted);
252
253 //
254 // If this file object has locked the volume then perform the unlock operation.
255 // We do this regardless of explicit or implicit (no share DASD open) lock.
256 //
257
258 if (FileObject == Vcb->VolumeLockFileObject) {
259
260 ASSERT( FlagOn( Vcb->VcbState, VCB_STATE_LOCKED));
261
262 IoAcquireVpbSpinLock( &SavedIrql );
263
264 ClearFlag( Vcb->Vpb->Flags, VPB_LOCKED);
265 ClearFlag( Vcb->VcbState, VCB_STATE_LOCKED );
266 Vcb->VolumeLockFileObject = NULL;
267 SendUnlockNotification = TRUE;
268
269 IoReleaseVpbSpinLock( SavedIrql );
270 }
271
272 CdUnlockVcb( IrpContext, Vcb );
273
274 //
275 // We must clean up the share access at this time, since we may not
276 // get a Close call for awhile if the file was mapped through this
277 // File Object.
278 //
279
280 IoRemoveShareAccess( FileObject, &Fcb->ShareAccess );
281
282 //} finally { /* ReactOS Change: Manual removal of SEH since macros to hack around it don't allow multiple SEH usage within one function */
283
284 CdReleaseFcb( IrpContext, Fcb );
285
286 if (SendUnlockNotification) {
287
288 FsRtlNotifyVolumeEvent( FileObject, FSRTL_VOLUME_UNLOCK );
289 }
290 //} /* ReactOS Change: Manual removal of SEH since macros to hack around it don't allow multiple SEH usage within one function */
291
292 //
293 // If appropriate, try to spark teardown by purging the volume. Should
294 // this very fileobject we were cleaning up be the last reason for the
295 // volume to remain, teardown will commence on completion of this Irp.
296 //
297
298 if (AttemptTeardown) {
299
300 //
301 // Preacquire CdData here, since the purges will generate closes which
302 // may acquire CdData if there is a possibility of tearing the volume
303 // down.
304 //
305
306 CdAcquireCdData( IrpContext);
307
308 try {
309
310 CdAcquireVcbExclusive( IrpContext, Vcb, FALSE );
311 VcbAcquired = TRUE;
312
313 CdPurgeVolume( IrpContext, Vcb, FALSE );
314
315 } finally {
316
317 if (VcbAcquired) { CdReleaseVcb( IrpContext, Vcb ); }
318
319 CdReleaseCdData( IrpContext);
320 }
321 }
322
323 //
324 // If this is a normal termination then complete the request
325 //
326
327 CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
328
329 return STATUS_SUCCESS;
330 }
331
332