3 Copyright (c) 1989-2000 Microsoft Corporation
11 This module implements the Cdfs Resource acquisition routines
19 // The Bug check file id for this module
22 #define BugCheckFileId (CDFS_BUG_CHECK_RESRCSUP)
25 #pragma alloc_text(PAGE, CdAcquireForCache)
26 #pragma alloc_text(PAGE, CdFilterCallbackAcquireForCreateSection)
27 #pragma alloc_text(PAGE, CdAcquireResource)
28 #pragma alloc_text(PAGE, CdNoopAcquire)
29 #pragma alloc_text(PAGE, CdNoopRelease)
30 #pragma alloc_text(PAGE, CdReleaseForCreateSection)
31 #pragma alloc_text(PAGE, CdReleaseFromCache)
36 _Requires_lock_held_(_Global_critical_region_
)
37 _When_(Type
== AcquireExclusive
&& return != FALSE
, _Acquires_exclusive_lock_(*Resource
))
38 _When_(Type
== AcquireShared
&& return != FALSE
, _Acquires_shared_lock_(*Resource
))
39 _When_(Type
== AcquireSharedStarveExclusive
&& return != FALSE
, _Acquires_shared_lock_(*Resource
))
40 _When_(IgnoreWait
== FALSE
, _Post_satisfies_(return == TRUE
))
43 _In_ PIRP_CONTEXT IrpContext
,
44 _Inout_ PERESOURCE Resource
,
45 _In_ BOOLEAN IgnoreWait
,
46 _In_ TYPE_OF_ACQUIRE Type
53 This is the single routine used to acquire file system resources. It
54 looks at the IgnoreWait flag to determine whether to try to acquire the
55 resource without waiting. Returning TRUE/FALSE to indicate success or
56 failure. Otherwise it is driven by the WAIT flag in the IrpContext and
57 will raise CANT_WAIT on a failure.
61 Resource - This is the resource to try and acquire.
63 IgnoreWait - If TRUE then this routine will not wait to acquire the
64 resource and will return a boolean indicating whether the resource was
65 acquired. Otherwise we use the flag in the IrpContext and raise
66 if the resource is not acquired.
68 Type - Indicates how we should try to get the resource.
72 BOOLEAN - TRUE if the resource is acquired. FALSE if not acquired and
73 IgnoreWait is specified. Otherwise we raise CANT_WAIT.
83 // We look first at the IgnoreWait flag, next at the flag in the Irp
84 // Context to decide how to acquire this resource.
87 if (!IgnoreWait
&& FlagOn( IrpContext
->Flags
, IRP_CONTEXT_FLAG_WAIT
)) {
93 // Attempt to acquire the resource either shared or exclusively.
97 case AcquireExclusive
:
99 #pragma prefast( suppress:28137, "prefast believes Wait should be a constant, but this is ok for CDFS" )
100 Acquired
= ExAcquireResourceExclusiveLite( Resource
, Wait
);
105 Acquired
= ExAcquireResourceSharedLite( Resource
, Wait
);
108 case AcquireSharedStarveExclusive
:
110 Acquired
= ExAcquireSharedStarveExclusive( Resource
, Wait
);
119 // If not acquired and the user didn't specifiy IgnoreWait then
123 if (!Acquired
&& !IgnoreWait
) {
125 CdRaiseStatus( IrpContext
, STATUS_CANT_WAIT
);
133 _Requires_lock_held_(_Global_critical_region_
)
134 _When_(return!=0, _Acquires_shared_lock_(*Fcb
->Resource
))
145 The address of this routine is specified when creating a CacheMap for
146 a file. It is subsequently called by the Lazy Writer for synchronization.
150 Fcb - The pointer supplied as context to the cache initialization
153 Wait - TRUE if the caller is willing to block.
164 NT_ASSERT(IoGetTopLevelIrp() == NULL
);
165 IoSetTopLevelIrp((PIRP
)FSRTL_CACHE_TOP_LEVEL_IRP
);
167 return ExAcquireResourceSharedLite( Fcb
->Resource
, Wait
);
171 _Requires_lock_held_(_Global_critical_region_
)
172 _Releases_lock_(*Fcb
->Resource
)
182 The address of this routine is specified when creating a CacheMap for
183 a virtual file. It is subsequently called by the Lazy Writer to release
184 a resource acquired above.
188 Fcb - The pointer supplied as context to the cache initialization
200 NT_ASSERT(IoGetTopLevelIrp() == (PIRP
)FSRTL_CACHE_TOP_LEVEL_IRP
);
201 IoSetTopLevelIrp( NULL
);
203 ExReleaseResourceLite( Fcb
->Resource
);
217 This routine does nothing.
221 Fcb - The Fcb/Vcb which was specified as a context parameter for this
224 Wait - TRUE if the caller is willing to block.
235 UNREFERENCED_PARAMETER( Fcb
);
236 UNREFERENCED_PARAMETER( Wait
);
251 This routine does nothing.
255 Fcb - The Fcb/Vcb which was specified as a context parameter for this
267 UNREFERENCED_PARAMETER( Fcb
);
272 _Requires_lock_held_(_Global_critical_region_
)
274 CdFilterCallbackAcquireForCreateSection (
275 _In_ PFS_FILTER_CALLBACK_DATA CallbackData
,
276 _Unreferenced_parameter_ PVOID
*CompletionContext
283 This is the callback routine for MM to use to acquire the file exclusively.
287 FS_FILTER_CALLBACK_DATA - Filter based callback data that provides the file object we
290 CompletionContext - Ignored.
294 On success we return STATUS_FSFILTER_OP_COMPLETED_SUCCESSFULLY.
296 If SyncType is SyncTypeCreateSection, we return a status that indicates there are no
297 writers to this file.
302 PFILE_OBJECT FileObject
;
307 NT_ASSERT( CallbackData
->Operation
== FS_FILTER_ACQUIRE_FOR_SECTION_SYNCHRONIZATION
);
308 NT_ASSERT( CallbackData
->SizeOfFsFilterCallbackData
== sizeof(FS_FILTER_CALLBACK_DATA
) );
311 // Get the file object from the callback data.
314 FileObject
= CallbackData
->FileObject
;
317 // Get the Fcb resource exclusively.
320 ExAcquireResourceExclusiveLite( &((PFCB
) FileObject
->FsContext
)->FcbNonpaged
->FcbResource
,
324 // Take the File resource shared. We need this later on when MM calls
325 // QueryStandardInfo to get the file size.
327 // If we don't use StarveExclusive, then we can get wedged behind an
328 // exclusive waiter who is waiting on someone else holding it shared in the
329 // read->initializecachemap path (which calls createsection) who is in turn
330 // waiting on us to finish the create section.
333 ExAcquireSharedStarveExclusive( ((PFCB
) FileObject
->FsContext
)->Resource
,
337 // CDFS is a read-only file system, so we can always indicate no writers.
338 // We only do this for create section synchronization. For others we
339 // return the generic success STATUS_FSFILTER_OP_COMPLETED_SUCCESSFULLY.
342 if (CallbackData
->Parameters
.AcquireForSectionSynchronization
.SyncType
== SyncTypeCreateSection
) {
344 return STATUS_FILE_LOCKED_WITH_ONLY_READERS
;
348 return STATUS_FSFILTER_OP_COMPLETED_SUCCESSFULLY
;
351 UNREFERENCED_PARAMETER( CompletionContext
);
355 _Function_class_(FAST_IO_RELEASE_FILE
)
356 _Requires_lock_held_(_Global_critical_region_
)
358 CdReleaseForCreateSection (
359 _In_ PFILE_OBJECT FileObject
366 This is the callback routine for MM to use to release a file acquired with
367 the AcquireForCreateSection call above.
371 FileObject - File object for a Cdfs stream.
383 // Release the resources.
386 ExReleaseResourceLite( &((PFCB
) FileObject
->FsContext
)->FcbNonpaged
->FcbResource
);
387 ExReleaseResourceLite( ((PFCB
) FileObject
->FsContext
)->Resource
);