Sample Code

windows driver samples/ cdfs file system driver/ C++/ resrcsup.c/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*++
 
Copyright (c) 1989-2000 Microsoft Corporation
 
Module Name:
 
    ResrcSup.c
 
Abstract:
 
    This module implements the Cdfs Resource acquisition routines
 
 
--*/
 
#include "CdProcs.h"
 
//
//  The Bug check file id for this module
//
 
#define BugCheckFileId                   (CDFS_BUG_CHECK_RESRCSUP)
 
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, CdAcquireForCache)
#pragma alloc_text(PAGE, CdFilterCallbackAcquireForCreateSection)
#pragma alloc_text(PAGE, CdAcquireResource)
#pragma alloc_text(PAGE, CdNoopAcquire)
#pragma alloc_text(PAGE, CdNoopRelease)
#pragma alloc_text(PAGE, CdReleaseForCreateSection)
#pragma alloc_text(PAGE, CdReleaseFromCache)
#endif
 
 
_Requires_lock_held_(_Global_critical_region_)
_When_(Type == AcquireExclusive && return != FALSE, _Acquires_exclusive_lock_(*Resource))
_When_(Type == AcquireShared && return != FALSE, _Acquires_shared_lock_(*Resource))
_When_(Type == AcquireSharedStarveExclusive && return != FALSE, _Acquires_shared_lock_(*Resource))
_When_(IgnoreWait == FALSE, _Post_satisfies_(return == TRUE))
BOOLEAN
CdAcquireResource (
    _In_ PIRP_CONTEXT IrpContext,
    _Inout_ PERESOURCE Resource,
    _In_ BOOLEAN IgnoreWait,
    _In_ TYPE_OF_ACQUIRE Type
    )
 
/*++
 
Routine Description:
 
    This is the single routine used to acquire file system resources.  It
    looks at the IgnoreWait flag to determine whether to try to acquire the
    resource without waiting.  Returning TRUE/FALSE to indicate success or
    failure.  Otherwise it is driven by the WAIT flag in the IrpContext and
    will raise CANT_WAIT on a failure.
 
Arguments:
 
    Resource - This is the resource to try and acquire.
 
    IgnoreWait - If TRUE then this routine will not wait to acquire the
        resource and will return a boolean indicating whether the resource was
        acquired.  Otherwise we use the flag in the IrpContext and raise
        if the resource is not acquired.
 
    Type - Indicates how we should try to get the resource.
 
Return Value:
 
    BOOLEAN - TRUE if the resource is acquired.  FALSE if not acquired and
        IgnoreWait is specified.  Otherwise we raise CANT_WAIT.
 
--*/
 
{
    BOOLEAN Wait = FALSE;
    BOOLEAN Acquired;
    PAGED_CODE();
 
    //
    //  We look first at the IgnoreWait flag, next at the flag in the Irp
    //  Context to decide how to acquire this resource.
    //
 
    if (!IgnoreWait && FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT )) {
 
        Wait = TRUE;
    }
 
    //
    //  Attempt to acquire the resource either shared or exclusively.
    //
 
    switch (Type) {
        case AcquireExclusive:
 
#pragma prefast( suppress:28137, "prefast believes Wait should be a constant, but this is ok for CDFS" )
            Acquired = ExAcquireResourceExclusiveLite( Resource, Wait );
            break;
 
        case AcquireShared:
 
#pragma prefast( suppress:28137, "prefast believes Wait should be a constant, but this is ok for CDFS" )
            Acquired = ExAcquireResourceSharedLite( Resource, Wait );
            break;
 
        case AcquireSharedStarveExclusive:
 
#pragma prefast( suppress:28137, "prefast believes Wait should be a constant, but this is ok for CDFS" )
            Acquired = ExAcquireSharedStarveExclusive( Resource, Wait );
            break;
 
        default:
            Acquired = FALSE;
            NT_ASSERT( FALSE );
    }
 
    //
    //  If not acquired and the user didn't specifiy IgnoreWait then
    //  raise CANT_WAIT.
    //
 
    if (!Acquired && !IgnoreWait) {
 
        CdRaiseStatus( IrpContext, STATUS_CANT_WAIT );
    }
 
    return Acquired;
}
 
 
_Requires_lock_held_(_Global_critical_region_)
_When_(return!=0, _Acquires_shared_lock_(*Fcb->Resource))
BOOLEAN
CdAcquireForCache (
    _Inout_ PFCB Fcb,
    _In_ BOOLEAN Wait
    )
 
/*++
 
Routine Description:
 
    The address of this routine is specified when creating a CacheMap for
    a file.  It is subsequently called by the Lazy Writer for synchronization.
 
Arguments:
 
    Fcb -  The pointer supplied as context to the cache initialization
           routine.
 
    Wait - TRUE if the caller is willing to block.
 
Return Value:
 
    None
 
--*/
 
{
    PAGED_CODE();
 
    NT_ASSERT(IoGetTopLevelIrp() == NULL);
    IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
 
    return ExAcquireResourceSharedLite( Fcb->Resource, Wait );
}
 
_Requires_lock_held_(_Global_critical_region_)
_Releases_lock_(*Fcb->Resource)
VOID
CdReleaseFromCache (
    _Inout_ PFCB Fcb
    )
 
/*++
 
Routine Description:
 
    The address of this routine is specified when creating a CacheMap for
    a virtual file.  It is subsequently called by the Lazy Writer to release
    a resource acquired above.
 
Arguments:
 
    Fcb -  The pointer supplied as context to the cache initialization
           routine.
 
Return Value:
 
    None
 
--*/
 
{
    PAGED_CODE();
 
    NT_ASSERT(IoGetTopLevelIrp() == (PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
    IoSetTopLevelIrp( NULL );
     
    ExReleaseResourceLite( Fcb->Resource );
}
 
BOOLEAN
CdNoopAcquire (
    _In_ PVOID Fcb,
    _In_ BOOLEAN Wait
    )
 
/*++
 
Routine Description:
 
    This routine does nothing.
 
Arguments:
 
    Fcb - The Fcb/Vcb which was specified as a context parameter for this
          routine.
 
    Wait - TRUE if the caller is willing to block.
 
Return Value:
 
    TRUE
 
--*/
 
{
    PAGED_CODE();
 
    UNREFERENCED_PARAMETER( Fcb );
    UNREFERENCED_PARAMETER( Wait );
     
    return TRUE;
}
 
VOID
CdNoopRelease (
    _In_ PVOID Fcb
    )
 
/*++
 
Routine Description:
 
    This routine does nothing.
 
Arguments:
 
    Fcb - The Fcb/Vcb which was specified as a context parameter for this
          routine.
 
Return Value:
 
    None
 
--*/
 
{
    PAGED_CODE();
 
    UNREFERENCED_PARAMETER( Fcb );
}
 
 
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
CdFilterCallbackAcquireForCreateSection (
    _In_ PFS_FILTER_CALLBACK_DATA CallbackData,
    _Unreferenced_parameter_ PVOID *CompletionContext
    )
 
/*++
 
Routine Description:
 
    This is the callback routine for MM to use to acquire the file exclusively.
 
Arguments:
 
    FS_FILTER_CALLBACK_DATA - Filter based callback data that provides the file object we
                              want to acquire.
 
    CompletionContext - Ignored.
 
Return Value:
 
    On success we return STATUS_FSFILTER_OP_COMPLETED_SUCCESSFULLY.
 
    If SyncType is SyncTypeCreateSection, we return a status that indicates there are no
    writers to this file.
 
--*/
 
{
    PFILE_OBJECT FileObject;
 
 
    PAGED_CODE();
     
    NT_ASSERT( CallbackData->Operation == FS_FILTER_ACQUIRE_FOR_SECTION_SYNCHRONIZATION );
    NT_ASSERT( CallbackData->SizeOfFsFilterCallbackData == sizeof(FS_FILTER_CALLBACK_DATA) );
 
    //
    //  Get the file object from the callback data.
    //
 
    FileObject = CallbackData->FileObject;
 
    //
    //  Get the Fcb resource exclusively.
    //
 
    ExAcquireResourceExclusiveLite( &((PFCB) FileObject->FsContext)->FcbNonpaged->FcbResource,
                                    TRUE );
                                 
    //
    //  Take the File resource shared.  We need this later on when MM calls
    //  QueryStandardInfo to get the file size. 
    //
    //  If we don't use StarveExclusive,  then we can get wedged behind an
    //  exclusive waiter who is waiting on someone else holding it shared in the
    //  read->initializecachemap path (which calls createsection) who is in turn
    //  waiting on us to finish the create section.
    //
 
    ExAcquireSharedStarveExclusive( ((PFCB) FileObject->FsContext)->Resource,
                                    TRUE );
 
    //
    //  CDFS is a read-only file system, so we can always indicate no writers.
    //  We only do this for create section synchronization.  For others we
    //  return the generic success STATUS_FSFILTER_OP_COMPLETED_SUCCESSFULLY.
    //
 
    if (CallbackData->Parameters.AcquireForSectionSynchronization.SyncType == SyncTypeCreateSection) {
 
        return STATUS_FILE_LOCKED_WITH_ONLY_READERS;
 
    } else {
 
        return STATUS_FSFILTER_OP_COMPLETED_SUCCESSFULLY;
    }
 
    UNREFERENCED_PARAMETER( CompletionContext );
}
 
_Function_class_(FAST_IO_RELEASE_FILE)
_Requires_lock_held_(_Global_critical_region_)
VOID
CdReleaseForCreateSection (
    _In_ PFILE_OBJECT FileObject
    )
 
/*++
 
Routine Description:
 
    This is the callback routine for MM to use to release a file acquired with
    the AcquireForCreateSection call above.
 
Arguments:
 
    FileObject - File object for a Cdfs stream.
 
Return Value:
 
    None
 
--*/
 
{
    PAGED_CODE();
 
    //
    //  Release the resources.
    //
 
    ExReleaseResourceLite( &((PFCB) FileObject->FsContext)->FcbNonpaged->FcbResource );
    ExReleaseResourceLite( ((PFCB) FileObject->FsContext)->Resource);
}

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS