Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pnp.c « Ext3Fsd - github.com/matt-wu/Ext3Fsd.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5ff2afbc915a95b4abd652720b60b3f16b57d4e7 (plain)
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
391
392
393
394
395
396
397
/*
 * COPYRIGHT:        See COPYRIGHT.TXT
 * PROJECT:          Ext2 File System Driver for WinNT/2K/XP
 * FILE:             pnp.c
 * PROGRAMMER:       Matt Wu <mattwu@163.com>
 * HOMEPAGE:         http://www.ext2fsd.com
 * UPDATE HISTORY:
 */

#if (_WIN32_WINNT >= 0x0500)

/* INCLUDES *****************************************************************/

#include "ext2fs.h"

/* GLOBALS ***************************************************************/

extern PEXT2_GLOBAL Ext2Global;

/* DEFINITIONS *************************************************************/

NTSTATUS
Ext2PnpCompletionRoutine (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP           Irp,
    IN PVOID          Contxt     );


#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, Ext2Pnp)
#pragma alloc_text(PAGE, Ext2PnpQueryRemove)
#pragma alloc_text(PAGE, Ext2PnpRemove)
#pragma alloc_text(PAGE, Ext2PnpCancelRemove)
#pragma alloc_text(PAGE, Ext2PnpSurpriseRemove)
#endif


/* FUNCTIONS *************************************************************/


NTSTATUS
Ext2PnpCompletionRoutine (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN PVOID Contxt
)
{
    PKEVENT Event = (PKEVENT) Contxt;

    KeSetEvent( Event, 0, FALSE );

    return STATUS_MORE_PROCESSING_REQUIRED;

    UNREFERENCED_PARAMETER( DeviceObject );
    UNREFERENCED_PARAMETER( Contxt );
}


NTSTATUS
Ext2Pnp (IN PEXT2_IRP_CONTEXT IrpContext)
{
    NTSTATUS            Status = STATUS_INVALID_PARAMETER;

    PIRP                Irp;
    PIO_STACK_LOCATION  IrpSp;
    PEXT2_VCB           Vcb;
    PDEVICE_OBJECT      DeviceObject;

    __try {

        ASSERT(IrpContext);

        ASSERT((IrpContext->Identifier.Type == EXT2ICX) &&
               (IrpContext->Identifier.Size == sizeof(EXT2_IRP_CONTEXT)));

        DeviceObject = IrpContext->DeviceObject;

        Vcb = (PEXT2_VCB) DeviceObject->DeviceExtension;

        ASSERT(Vcb != NULL);

        if ( !((Vcb->Identifier.Type == EXT2VCB) &&
                (Vcb->Identifier.Size == sizeof(EXT2_VCB)))) {
            __leave; // Status = STATUS_INVALID_PARAMETER
        }

        Irp = IrpContext->Irp;
        IrpSp = IoGetCurrentIrpStackLocation(Irp);

        SetFlag(IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT);

        switch ( IrpSp->MinorFunction ) {

        case IRP_MN_QUERY_REMOVE_DEVICE:

            DEBUG(DL_PNP, ("Ext2Pnp: Ext2PnpQueryRemove...\n"));
            Status = Ext2PnpQueryRemove(IrpContext, Vcb);

            break;

        case IRP_MN_REMOVE_DEVICE:

            DEBUG(DL_PNP, ("Ext2Pnp: Ext2PnpRemove...\n"));
            Status = Ext2PnpRemove(IrpContext, Vcb);
            break;

        case IRP_MN_CANCEL_REMOVE_DEVICE:

            DEBUG(DL_PNP, ("Ext2Pnp: Ext2PnpCancelRemove...\n"));
            Status = Ext2PnpCancelRemove(IrpContext, Vcb);
            break;

        case IRP_MN_SURPRISE_REMOVAL:

            DEBUG(DL_PNP, ("Ext2Pnp: Ext2PnpSupriseRemove...\n"));
            Status = Ext2PnpSurpriseRemove(IrpContext, Vcb);
            break;

        default:
            break;
        }

    } __finally {

        if (!IrpContext->ExceptionInProgress) {
            Irp = IrpContext->Irp;

            if (Irp) {

                //
                // Here we need pass the IRP to the disk driver.
                //

                IoSkipCurrentIrpStackLocation( Irp );

                Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);

                IrpContext->Irp = NULL;
            }

            Ext2CompleteIrpContext(IrpContext, Status);
        }
    }

    return Status;
}


NTSTATUS
Ext2PnpQueryRemove (
    PEXT2_IRP_CONTEXT IrpContext,
    PEXT2_VCB         Vcb
)
{
    NTSTATUS Status = STATUS_SUCCESS;
    KEVENT   Event;
    BOOLEAN  bDeleted = FALSE;
    BOOLEAN  VcbAcquired = FALSE;

    __try {

        CcWaitForCurrentLazyWriterActivity();

        VcbAcquired = ExAcquireResourceExclusiveLite(
                          &Vcb->MainResource, TRUE );

        Ext2FlushFiles(IrpContext, Vcb, FALSE);
        Ext2FlushVolume(IrpContext, Vcb, FALSE);

        DEBUG(DL_PNP, ("Ext2PnpQueryRemove: Ext2LockVcb: Vcb=%xh FileObject=%xh ...\n",
                       Vcb, IrpContext->FileObject));
        Status = Ext2LockVcb(Vcb, IrpContext->FileObject);

        if (VcbAcquired) {
            ExReleaseResourceLite(&Vcb->MainResource);
            VcbAcquired = FALSE;
        }

        DEBUG(DL_PNP, ("Ext2PnpQueryRemove: Ext2PurgeVolume ...\n"));
        Ext2PurgeVolume(Vcb, TRUE);

        if (!NT_SUCCESS(Status)) {
            __leave;
        }

        IoCopyCurrentIrpStackLocationToNext(IrpContext->Irp);

        KeInitializeEvent( &Event, NotificationEvent, FALSE );
        IoSetCompletionRoutine( IrpContext->Irp,
                                Ext2PnpCompletionRoutine,
                                &Event,
                                TRUE,
                                TRUE,
                                TRUE );

        DEBUG(DL_PNP, ("Ext2PnpQueryRemove: Call lower level driver...\n"));
        Status = IoCallDriver( Vcb->TargetDeviceObject,
                               IrpContext->Irp);

        if (Status == STATUS_PENDING) {
            KeWaitForSingleObject( &Event,
                                   Executive,
                                   KernelMode,
                                   FALSE,
                                   NULL );
            Status = IrpContext->Irp->IoStatus.Status;
        }

        if (NT_SUCCESS(Status)) {
            ASSERT(!VcbAcquired);
            DEBUG(DL_PNP, ("Ext2PnpQueryRemove: Ext2CheckDismount ...\n"));
            bDeleted = Ext2CheckDismount(IrpContext, Vcb, TRUE);
            DEBUG(DL_PNP, ("Ext2PnpQueryRemove: Ext2FlushVolume bDelted=%xh ...\n", bDeleted));
        }

    } __finally {

        if (VcbAcquired) {
            ExReleaseResourceLite(&Vcb->MainResource);
        }

        IrpContext->Irp->IoStatus.Status = Status;
        Ext2CompleteRequest(
            IrpContext->Irp, FALSE, (CCHAR)(NT_SUCCESS(Status)?
                                            IO_DISK_INCREMENT : IO_NO_INCREMENT) );

        IrpContext->Irp = NULL;
    }

    return Status;
}

NTSTATUS
Ext2PnpRemove (
    PEXT2_IRP_CONTEXT IrpContext,
    PEXT2_VCB         Vcb      )
{
    NTSTATUS Status = STATUS_SUCCESS;
    KEVENT   Event;
    BOOLEAN  bDeleted;

    __try {

        DEBUG(DL_PNP, ("Ext2PnpRemove by Ext2Pnp ...\n"));

        CcWaitForCurrentLazyWriterActivity();
        ExAcquireResourceExclusiveLite(
            &Vcb->MainResource,  TRUE );
        Status = Ext2LockVcb(Vcb, IrpContext->FileObject);
        ExReleaseResourceLite(&Vcb->MainResource);

        //
        // Setup the Irp. We'll send it to the lower disk driver.
        //

        IoCopyCurrentIrpStackLocationToNext(IrpContext->Irp);

        KeInitializeEvent( &Event, NotificationEvent, FALSE );
        IoSetCompletionRoutine( IrpContext->Irp,
                                Ext2PnpCompletionRoutine,
                                &Event,
                                TRUE,
                                TRUE,
                                TRUE );

        Status = IoCallDriver( Vcb->TargetDeviceObject,
                               IrpContext->Irp);

        if (Status == STATUS_PENDING) {

            KeWaitForSingleObject( &Event,
                                   Executive,
                                   KernelMode,
                                   FALSE,
                                   NULL );

            Status = IrpContext->Irp->IoStatus.Status;
        }

        /* purge volume cache */
        Ext2PurgeVolume(Vcb, FALSE);

        /* dismount volume */
        bDeleted = Ext2CheckDismount(IrpContext, Vcb, TRUE);
        SetLongFlag(Vcb->Flags, VCB_DEVICE_REMOVED);

    } __finally {

        IrpContext->Irp->IoStatus.Status = Status;
        Ext2CompleteRequest(
            IrpContext->Irp, FALSE, (CCHAR)(NT_SUCCESS(Status)?
                                            IO_DISK_INCREMENT : IO_NO_INCREMENT) );

        IrpContext->Irp = NULL;
    }

    return Status;
}


NTSTATUS
Ext2PnpSurpriseRemove (
    PEXT2_IRP_CONTEXT IrpContext,
    PEXT2_VCB         Vcb      )
{
    NTSTATUS Status;
    KEVENT   Event;
    BOOLEAN  bDeleted;

    __try {

        DEBUG(DL_PNP, ("Ext2PnpSupriseRemove by Ext2Pnp ...\n"));

        CcWaitForCurrentLazyWriterActivity();
        ExAcquireResourceExclusiveLite(
            &Vcb->MainResource,  TRUE );

        Status = Ext2LockVcb(Vcb, IrpContext->FileObject);

        ExReleaseResourceLite(&Vcb->MainResource);

        //
        // Setup the Irp. We'll send it to the lower disk driver.
        //

        IoCopyCurrentIrpStackLocationToNext(IrpContext->Irp);

        KeInitializeEvent( &Event, NotificationEvent, FALSE );
        IoSetCompletionRoutine( IrpContext->Irp,
                                Ext2PnpCompletionRoutine,
                                &Event,
                                TRUE,
                                TRUE,
                                TRUE );

        Status = IoCallDriver( Vcb->TargetDeviceObject,
                               IrpContext->Irp);

        if (Status == STATUS_PENDING) {

            KeWaitForSingleObject( &Event,
                                   Executive,
                                   KernelMode,
                                   FALSE,
                                   NULL );

            Status = IrpContext->Irp->IoStatus.Status;
        }

        /* purge volume cache */
        Ext2PurgeVolume(Vcb, FALSE);

        /* dismount volume */
        bDeleted = Ext2CheckDismount(IrpContext, Vcb, TRUE);
        SetLongFlag(Vcb->Flags, VCB_DEVICE_REMOVED);

    } __finally {

        IrpContext->Irp->IoStatus.Status = Status;
        Ext2CompleteRequest(
            IrpContext->Irp, FALSE, (CCHAR)(NT_SUCCESS(Status)?
                                            IO_DISK_INCREMENT : IO_NO_INCREMENT) );

        IrpContext->Irp = NULL;
    }

    return Status;
}


NTSTATUS
Ext2PnpCancelRemove (
    PEXT2_IRP_CONTEXT IrpContext,
    PEXT2_VCB         Vcb
)
{
    NTSTATUS Status;

    DEBUG(DL_PNP, ("Ext2PnpCancelRemove by Ext2Pnp ...\n"));

    ExAcquireResourceExclusiveLite(
        &Vcb->MainResource,  TRUE );

    Status = Ext2UnlockVcb(Vcb, IrpContext->FileObject);

    ExReleaseResourceLite(&Vcb->MainResource);

    IoSkipCurrentIrpStackLocation(IrpContext->Irp);

    Status = IoCallDriver(Vcb->TargetDeviceObject, IrpContext->Irp);

    IrpContext->Irp = NULL;

    return Status;
}

#endif //(_WIN32_WINNT >= 0x0500)