Sample Code

windows driver samples/ cdfs file system driver/ C++/ fspdisp.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
/*++
 
Copyright (c) 1989-2000 Microsoft Corporation
 
Module Name:
 
    FspDisp.c
 
Abstract:
 
    This module implements the main dispatch procedure/thread for the Cdfs
    Fsp
 
 
--*/
 
#include "CdProcs.h"
 
//
//  The Bug check file id for this module
//
 
#define BugCheckFileId                   (CDFS_BUG_CHECK_FSPDISP)
 
VOID
CdFspDispatch (
    _In_ PVOID Context
    )
 
/*++
 
Routine Description:
 
    This is the main FSP thread routine that is executed to receive
    and dispatch IRP requests.  Each FSP thread begins its execution here.
    There is one thread created at system initialization time and subsequent
    threads created as needed.
 
Arguments:
 
    IrpContext - IrpContext for a request to process.
 
Return Value:
 
    None
 
--*/
 
{
    THREAD_CONTEXT ThreadContext = {0};
    PIRP_CONTEXT IrpContext = Context;
    NTSTATUS Status;
 
    PIRP Irp = IrpContext->Irp;
    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
 
    PVOLUME_DEVICE_OBJECT VolDo = NULL;
 
    //
    //  If this request has an associated volume device object, remember it.
    //
 
    if (IrpSp->FileObject != NULL) {
 
        VolDo = CONTAINING_RECORD( IrpSp->DeviceObject,
                                   VOLUME_DEVICE_OBJECT,
                                   DeviceObject );
    }
 
    //
    //  Now case on the function code.  For each major function code,
    //  either call the appropriate worker routine.  This routine that
    //  we call is responsible for completing the IRP, and not us.
    //  That way the routine can complete the IRP and then continue
    //  post processing as required.  For example, a read can be
    //  satisfied right away and then read can be done.
    //
    //  We'll do all of the work within an exception handler that
    //  will be invoked if ever some underlying operation gets into
    //  trouble.
    //
 
    while ( TRUE ) {
 
        //
        //  Set all the flags indicating we are in the Fsp.
        //
 
        SetFlag( IrpContext->Flags, IRP_CONTEXT_FSP_FLAGS );
 
        FsRtlEnterFileSystem();
 
        CdSetThreadContext( IrpContext, &ThreadContext );
 
        while (TRUE) {
 
            try {
 
                //
                //  Reinitialize for the next try at completing this
                //  request.
                //
 
                Status =
                IrpContext->ExceptionStatus = STATUS_SUCCESS;
 
                //
                //  Initialize the Io status field in the Irp.
                //
 
                Irp->IoStatus.Status = STATUS_SUCCESS;
                Irp->IoStatus.Information = 0;
 
                //
                //  Case on the major irp code.
                //
 
                switch (IrpContext->MajorFunction) {
 
                case IRP_MJ_CREATE :
 
                    CdCommonCreate( IrpContext, Irp );
                    break;
 
                case IRP_MJ_CLOSE :
 
                    NT_ASSERT( FALSE );
                    break;
 
                case IRP_MJ_READ :
 
                    CdCommonRead( IrpContext, Irp );
                    break;
 
                case IRP_MJ_QUERY_INFORMATION :
 
                    CdCommonQueryInfo( IrpContext, Irp );
                    break;
 
                case IRP_MJ_SET_INFORMATION :
 
                    CdCommonSetInfo( IrpContext, Irp );
                    break;
 
                case IRP_MJ_QUERY_VOLUME_INFORMATION :
 
                    CdCommonQueryVolInfo( IrpContext, Irp );
                    break;
 
                case IRP_MJ_DIRECTORY_CONTROL :
 
                    CdCommonDirControl( IrpContext, Irp );
                    break;
 
                case IRP_MJ_FILE_SYSTEM_CONTROL :
 
                    CdCommonFsControl( IrpContext, Irp );
                    break;
 
                case IRP_MJ_DEVICE_CONTROL :
 
                    CdCommonDevControl( IrpContext, Irp );
                    break;
 
                case IRP_MJ_LOCK_CONTROL :
 
                    CdCommonLockControl( IrpContext, Irp );
                    break;
 
                case IRP_MJ_CLEANUP :
 
                    CdCommonCleanup( IrpContext, Irp );
                    break;
 
                case IRP_MJ_PNP :
 
                    NT_ASSERT( FALSE );
                    CdCommonPnp( IrpContext, Irp );
                    break;
 
                default :
 
                    Status = STATUS_INVALID_DEVICE_REQUEST;
                    CdCompleteRequest( IrpContext, Irp, Status );
                }
 
            } except( CdExceptionFilter( IrpContext, GetExceptionInformation() )) {
 
                Status = CdProcessException( IrpContext, Irp, GetExceptionCode() );
            }
 
            //
            //  Break out of the loop if we didn't get CANT_WAIT.
            //
 
            if (Status != STATUS_CANT_WAIT) { break; }
 
            //
            //  We are retrying this request.  Cleanup the IrpContext for the retry.
            //
 
            SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_MORE_PROCESSING );
            CdCleanupIrpContext( IrpContext, FALSE );
        }
 
        FsRtlExitFileSystem();
 
        //
        //  If there are any entries on this volume's overflow queue, service
        //  them.
        //
 
        if (VolDo != NULL) {
 
            KIRQL SavedIrql;
            PVOID Entry = NULL;
 
            //
            //  We have a volume device object so see if there is any work
            //  left to do in its overflow queue.
            //
 
            KeAcquireSpinLock( &VolDo->OverflowQueueSpinLock, &SavedIrql );
 
            if (VolDo->OverflowQueueCount > 0) {
 
                //
                //  There is overflow work to do in this volume so we'll
                //  decrement the Overflow count, dequeue the IRP, and release
                //  the Event
                //
 
                VolDo->OverflowQueueCount -= 1;
 
                Entry = RemoveHeadList( &VolDo->OverflowQueue );
 
            } else {
 
                VolDo->PostedRequestCount -= 1;           
 
                Entry = NULL;
            }
 
            KeReleaseSpinLock( &VolDo->OverflowQueueSpinLock, SavedIrql );
 
            //
            //  There wasn't an entry, break out of the loop and return to
            //  the Ex Worker thread.
            //
 
            if (Entry == NULL) {
 
                break;
            }
 
            //
            //  Extract the IrpContext , Irp, set wait to TRUE, and loop.
            //
 
            IrpContext = CONTAINING_RECORD( Entry,
                                            IRP_CONTEXT,
                                            WorkQueueItem.List );
 
            Irp = IrpContext->Irp;
            IrpSp = IoGetCurrentIrpStackLocation( Irp );
            __analysis_assert( IrpSp != 0 );
 
            continue;
        }
 
        break;
    }
 
    return;
}

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