a0d8ce43fc920afb9ecfd3dd16b6a8e524815919
[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 _Requires_lock_held_(_Global_critical_region_)
26 NTSTATUS
27 CdCommonCleanup (
28 _Inout_ PIRP_CONTEXT IrpContext,
29 _Inout_ 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 = FALSE;
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 if (TypeOfOpen == UserVolumeOpen) {
153
154 //
155 // For a force dismount, physically disconnect this Vcb from the device so
156 // a new mount can occur. Vcb deletion cannot happen at this time since
157 // there is a reference on it associated with this very request, but we'll
158 // call check for dismount again later after we process this close.
159 //
160
161 if (FlagOn( Ccb->Flags, CCB_FLAG_DISMOUNT_ON_CLOSE )) {
162
163 CdAcquireCdData( IrpContext );
164
165 CdCheckForDismount( IrpContext, Vcb, TRUE );
166
167 CdReleaseCdData( IrpContext );
168
169 //
170 // If this handle actually wrote something, flush the device buffers,
171 // and then set the verify bit now just to be safe (in case there is no
172 // dismount).
173 //
174
175 } else if (FlagOn( FileObject->Flags, FO_FILE_MODIFIED )) {
176
177 CdHijackIrpAndFlushDevice( IrpContext, Irp, Vcb->TargetDeviceObject );
178
179 CdMarkDevForVerifyIfVcbMounted( Vcb );
180 }
181 }
182
183 //
184 // Acquire the current file.
185 //
186
187 CdAcquireFcbExclusive( IrpContext, Fcb, FALSE );
188
189 //
190 // Use a try-finally to facilitate cleanup.
191 //
192
193 try {
194
195 //
196 // Case on the type of open that we are trying to cleanup.
197 //
198
199 switch (TypeOfOpen) {
200
201 case UserDirectoryOpen:
202
203 //
204 // Check if we need to complete any dir notify Irps on this file object.
205 //
206
207 FsRtlNotifyCleanup( Vcb->NotifySync,
208 &Vcb->DirNotifyList,
209 Ccb );
210
211 break;
212
213 case UserFileOpen:
214
215 //
216 // Coordinate the cleanup operation with the oplock state.
217 // Oplock cleanup operations can always cleanup immediately so no
218 // need to check for STATUS_PENDING.
219 //
220
221 FsRtlCheckOplock( CdGetFcbOplock(Fcb),
222 Irp,
223 IrpContext,
224 NULL,
225 NULL );
226
227 //
228 // Unlock all outstanding file locks.
229 //
230
231 if (Fcb->FileLock != NULL) {
232
233 FsRtlFastUnlockAll( Fcb->FileLock,
234 FileObject,
235 IoGetRequestorProcess( Irp ),
236 NULL );
237 }
238
239 //
240 // Cleanup the cache map.
241 //
242
243 CcUninitializeCacheMap( FileObject, NULL, NULL );
244
245 //
246 // Check the fast io state.
247 //
248
249 CdLockFcb( IrpContext, Fcb );
250 Fcb->IsFastIoPossible = CdIsFastIoPossible( Fcb );
251 CdUnlockFcb( IrpContext, Fcb );
252
253 break;
254
255 case UserVolumeOpen:
256
257 break;
258
259 default :
260
261 #pragma prefast( suppress:__WARNING_USE_OTHER_FUNCTION, "argument bogus" )
262 CdBugCheck( TypeOfOpen, 0, 0 );
263 }
264
265 //
266 // Now lock the Vcb in order to modify the fields in the in-memory
267 // structures.
268 //
269
270 CdLockVcb( IrpContext, Vcb );
271
272 //
273 // Decrement the cleanup counts in the Vcb and Fcb.
274 //
275
276 CdDecrementCleanupCounts( IrpContext, Fcb );
277
278 //
279 // If the cleanup count hit zero and the volume is not mounted, we
280 // will want to try to spark teardown.
281 //
282
283 AttemptTeardown = (Vcb->VcbCleanup == 0 && Vcb->VcbCondition == VcbNotMounted);
284
285 //
286 // If this file object has locked the volume then perform the unlock operation.
287 // We do this regardless of explicit or implicit (no share DASD open) lock.
288 //
289
290 if (FileObject == Vcb->VolumeLockFileObject) {
291
292 NT_ASSERT( FlagOn( Vcb->VcbState, VCB_STATE_LOCKED));
293
294 IoAcquireVpbSpinLock( &SavedIrql );
295
296 ClearFlag( Vcb->Vpb->Flags, VPB_LOCKED);
297 ClearFlag( Vcb->VcbState, VCB_STATE_LOCKED );
298 Vcb->VolumeLockFileObject = NULL;
299 SendUnlockNotification = TRUE;
300
301 IoReleaseVpbSpinLock( SavedIrql );
302 }
303
304 CdUnlockVcb( IrpContext, Vcb );
305
306 //
307 // We must clean up the share access at this time, since we may not
308 // get a Close call for awhile if the file was mapped through this
309 // File Object.
310 //
311
312 IoRemoveShareAccess( FileObject, &Fcb->ShareAccess );
313
314 } finally {
315
316 CdReleaseFcb( IrpContext, Fcb );
317
318 if (SendUnlockNotification) {
319
320 FsRtlNotifyVolumeEvent( FileObject, FSRTL_VOLUME_UNLOCK );
321 }
322 }
323
324 //
325 // If appropriate, try to spark teardown by purging the volume. Should
326 // this very fileobject we were cleaning up be the last reason for the
327 // volume to remain, teardown will commence on completion of this Irp.
328 //
329
330 if (AttemptTeardown) {
331
332 //
333 // Preacquire CdData here, since the purges will generate closes which
334 // may acquire CdData if there is a possibility of tearing the volume
335 // down.
336 //
337
338 CdAcquireCdData( IrpContext);
339
340 try {
341
342 CdAcquireVcbExclusive( IrpContext, Vcb, FALSE );
343 VcbAcquired = TRUE;
344
345 CdPurgeVolume( IrpContext, Vcb, FALSE );
346
347 } finally {
348
349 if (VcbAcquired) { CdReleaseVcb( IrpContext, Vcb ); }
350
351 CdReleaseCdData( IrpContext);
352 }
353 }
354
355 //
356 // If this is a normal termination then complete the request
357 //
358
359 CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
360
361 return STATUS_SUCCESS;
362 }
363
364