summaryrefslogtreecommitdiffstats
path: root/private/ntos/mailslot/datasup.c
blob: ecf7d6f64db8e497eabf79f53ab8272197d3bbd1 (plain) (blame)
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    DataSup.c

Abstract:

    This module implements the mailslot data queue support functions.

Author:

    Manny Weiser (mannyw)    9-Jan-1991

Revision History:

--*/

#include "mailslot.h"

//
//  The debug trace level
//

#define Dbg                              (DEBUG_TRACE_DATASUP)

//
// Local declarations
//

VOID
MsCancelDataQueueIrp (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    );

VOID
MsResetCancelRoutine(
    IN PIRP Irp
    );

VOID
MsSetCancelRoutine(
    IN PIRP Irp
    );

//
//  The following macro is used to dump a data queue
//

#define DumpDataQueue(S,P) {                                   \
    ULONG MsDumpDataQueue(IN ULONG Level, IN PDATA_QUEUE Ptr); \
    DebugTrace(0,Dbg,S,0);                                     \
    DebugTrace(0,Dbg,"", MsDumpDataQueue(Dbg,P));              \
}


#ifdef ALLOC_PRAGMA
#pragma alloc_text( PAGE, MsAddDataQueueEntry )
#pragma alloc_text( PAGE, MsInitializeDataQueue )
#pragma alloc_text( PAGE, MsRemoveDataQueueEntry )
#pragma alloc_text( PAGE, MsUninitializeDataQueue )
#endif

#if 0
NOT PAGEABLE -- MsCancelDataQueueIrp
NOT PAGEABLE -- MsResetCancelRoutine
NOT PAGEABLE -- MsSetCancelRoutine
#endif


VOID
MsInitializeDataQueue (
    IN PDATA_QUEUE DataQueue,
    IN PEPROCESS Process,
    IN ULONG Quota,
    IN ULONG MaximumMessageSize
    )

/*++

Routine Description:

    This routine initializes a new data queue.  The indicated quota is taken
    from the process and not returned until the data queue is uninitialized.

Arguments:

    DataQueue - Supplies the data queue to be initialized.

    Process - Supplies a pointer to the process creating the mailslot.

    Quota - Supplies the quota to assign to the data queue.

    MaximumMessageSize - The size of the largest message that can be
        written to the data queue.

Return Value:

    None.

--*/

{
    PAGED_CODE();
    DebugTrace(+1, Dbg, "MsInitializeDataQueue, DataQueue = %08lx\n", (ULONG)DataQueue);

    //
    // Get the process's quota, if we can't get it then this call will
    // raise status.
    //

    ObReferenceObject( Process );
    PsChargePoolQuota( Process, PagedPool, Quota );

    //
    // Initialize the data queue structure.
    //

    DataQueue->BytesInQueue       = 0;
    DataQueue->EntriesInQueue     = 0;
    DataQueue->QueueState         = Empty;
    DataQueue->MaximumMessageSize = MaximumMessageSize;
    DataQueue->Quota              = Quota;
    DataQueue->QuotaUsed          = 0;
    InitializeListHead( &DataQueue->DataEntryList );

    //
    // Return to the caller.
    //

    DebugTrace(-1, Dbg, "MsInitializeDataQueue -> VOID\n", 0);
    return;
}


VOID
MsUninitializeDataQueue (
    IN PDATA_QUEUE DataQueue,
    IN PEPROCESS Process
    )

/*++

Routine Description:

    This routine uninitializes a data queue.  The previously debited quota
    is returned to the process.

Arguments:

    DataQueue - Supplies the data queue being uninitialized

    Process - Supplies a pointer to the process who created the mailslot

Return Value:

    None.

--*/

{
    PAGED_CODE();
    DebugTrace(+1, Dbg, "MsUninitializeDataQueue, DataQueue = %08lx\n", (ULONG)DataQueue);

    //
    //  Assert that the queue is empty
    //

    ASSERT( IsListEmpty(&DataQueue->DataEntryList) );

    //
    //  Return all of our quota back to the process
    //

    PsReturnPoolQuota( Process, PagedPool, DataQueue->Quota );
    ObDereferenceObject( Process );

    //
    // For safety's sake, zero out the data queue structure.
    //

    RtlZeroMemory( DataQueue, sizeof(DATA_QUEUE ) );

    //
    // Return to the caller.
    //

    DebugTrace(-1, Dbg, "MsUnininializeDataQueue -> VOID\n", 0);
    return;
}


PDATA_ENTRY
MsAddDataQueueEntry (
    IN PDATA_QUEUE DataQueue,
    IN QUEUE_STATE Who,
    IN ULONG DataSize,
    IN PIRP Irp
    )

/*++

Routine Description:

    This function adds a new data entry to the of the data queue.
    Entries are always appended to the queue.  If necessary this
    function will allocate a data entry buffer, or use space in
    the IRP, and possibly complete the indicated IRP.

    The different actions we are perform are based on the type and who
    parameters and quota requirements.

    *** This function must be called from within a try statement.

    Who == ReadEntries

        +----------+                      - Allocate Data Entry from IRP
        |Irp       |     +-----------+
        |BufferedIo|<----|Buffered   |    - Allocate New System buffer
        |DeallBu...|     |EitherQuota|
        +----------+     +-----------+    - Reference and modify Irp to
          |      |         |                do Buffered I/O, Deallocate
          v      |         v                buffer, and have I/O completion
        +------+ +------>+------+           copy the buffer (Input operation)
        |User  |         |System|
        |Buffer|         |Buffer|
        +------+         +------+

    Who == WriteEntries && Quota Available

        +----------+                      - Allocate Data Entry from Quota
        |Irp       |     +-----------+
        |          |     |Buffered   |    - Allocate New System buffer
        |          |     |Quota      |
        +----------+     +-----------+    - Copy data from User buffer to
          |                |                system buffer
          v                v
        +------+         +------+         - Complete IRP
        |User  |..copy..>|System|
        |Buffer|         |Buffer|
        +------+         +------+

    Who == WriteEntries && Quota Not Available

        +----------+                     - Allocate Data Entry from Irp
        |Irp       |     +-----------+
        |BufferedIo|<----|Buffered   |   - Allocate New System buffer
        |DeallBuff |     |UserQuota  |
        +----------+     +-----------+   - Reference and modify Irp to use
          |      |         |               the new system buffer, do Buffered
          v      |         v               I/O, and Deallocate buffer
        +------+ +------>+------+
        |User  |         |System|        - Copy data from User buffer to
        |Buffer|..copy..>|Buffer|          system buffer
        +------+         +------+


Arguments:

    DataQueue - Supplies the Data queue being modified.

    Who - Indicates if this is the reader or writer that is adding to the
        mailslot.

    DataSize - Indicates the size of the data buffer needed to represent
        this entry.

    Irp - Supplies a pointer to the IRP responsible for this entry.

Return Value:

    PDATA_ENTRY - Returns a pointer to the newly added data entry.

--*/

{
    PDATA_ENTRY dataEntry;
    PLIST_ENTRY previousEntry;
    PFCB fcb;

    PVOID rewind1 = NULL;
    PVOID rewind2 = NULL;
    BOOLEAN irpCompleted = FALSE;

    PAGED_CODE( );

    DebugTrace(+1, Dbg, "MsAddDataQueueEntry, DataQueue = %08lx\n", (ULONG)DataQueue);

    ASSERT( DataQueue->QueueState != -1 );

    try {

        if (Who == ReadEntries) {

            //
            // Allocate a data entry from the IRP, and allocate a new
            // system buffer.
            //

            dataEntry = (PDATA_ENTRY)IoGetNextIrpStackLocation( Irp );

            dataEntry->DataPointer = NULL;
            dataEntry->Irp = Irp;
            dataEntry->DataSize = DataSize;
            dataEntry->TimeoutWorkContext = NULL;

            //
            // Check to see if the mailslot has enough quota left to
            // allocate the system buffer.
            //

            if ((DataQueue->Quota - DataQueue->QuotaUsed) >= DataSize) {

                //
                // Use the mailslot quota to allocate pool for the request.
                //

                rewind1 = dataEntry->DataPointer =
                    FsRtlAllocatePool(
                        PagedPool,
                        DataSize == 0 ? 1 : DataSize
                        );

                DataQueue->QuotaUsed += DataSize;

                dataEntry->From = MailslotQuota;

            } else {

                //
                // Use the caller's quota to allocate pool for the request.
                //

                rewind1 = dataEntry->DataPointer =
                    FsRtlAllocatePoolWithQuota(
                        PagedPool,
                        DataSize == 0 ? 1 : DataSize
                        );

                dataEntry->From = UserQuota;
            }

            //
            // Modify the IRP to be buffered I/O, deallocate the buffer, copy
            // the buffer on completion, and to reference the new system
            // buffer.
            //

            Irp->Flags |= IRP_BUFFERED_IO | IRP_DEALLOCATE_BUFFER | IRP_INPUT_OPERATION;
            Irp->AssociatedIrp.SystemBuffer = dataEntry->DataPointer;

            //
            // Setup to add this entry to the end of the data queue.
            //

            previousEntry = &DataQueue->DataEntryList;
            Irp->IoStatus.Status = (ULONG)DataQueue;

        } else {

            //
            // This is a writer entry.
            //

            //
            // If there is enough quota left in the mailslot then we will
            // allocate the data entry and data buffer from the mailslot
            // quota.
            //

            if ((DataQueue->Quota - DataQueue->QuotaUsed) >= sizeof(DATA_ENTRY) + DataSize) {

                //
                // Allocate the data buffer using the mailslot quota.
                //

                rewind2 = dataEntry =
                    FsRtlAllocatePool(
                        PagedPool,
                        sizeof(DATA_ENTRY)
                        );

                rewind1 = dataEntry->DataPointer =
                    FsRtlAllocatePool(
                        PagedPool,
                        DataSize == 0 ? 1 : DataSize
                        );

                DataQueue->QuotaUsed += sizeof(DATA_ENTRY) + DataSize;

                dataEntry->From = MailslotQuota;
                dataEntry->Irp = NULL;
                dataEntry->DataSize = DataSize;
                dataEntry->TimeoutWorkContext = NULL;

            } else {

                //
                // There isn't enough quota in the mailslot.  Use the
                // caller's quota.
                //
                // Allocate a data entry from the Irp, and allocate a new
                // system buffer.
                //

                rewind2 = dataEntry =
                    (PDATA_ENTRY)FsRtlAllocatePoolWithQuota(
                                     PagedPool,
                                     sizeof( DATA_ENTRY )
                                     );

                dataEntry->From = UserQuota;
                dataEntry->Irp = NULL;
                dataEntry->DataSize = DataSize;
                dataEntry->TimeoutWorkContext = NULL;

                rewind1 = dataEntry->DataPointer =
                    FsRtlAllocatePoolWithQuota(
                        PagedPool,
                        DataSize == 0 ? 1 : DataSize
                        );

            }

            //
            // Copy the user buffer to the new system buffer, update the FCB
            // timestamps and complete the IRP.
            //

            try {
                RtlMoveMemory( dataEntry->DataPointer, Irp->UserBuffer, DataSize );
            } except( EXCEPTION_EXECUTE_HANDLER ) {
                ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
            }

            fcb = CONTAINING_RECORD( DataQueue, FCB, DataQueue );
            KeQuerySystemTime( &fcb->Specific.Fcb.LastModificationTime );

            MsCompleteRequest( Irp, STATUS_SUCCESS );
            irpCompleted = TRUE;

            //
            // Find the place in the queue to insert this data entry.
            //

            previousEntry = &DataQueue->DataEntryList;

        } // else (writer entry)

        //
        // Now data entry points to a new data entry to add to the data queue
        // Check if the queue is empty otherwise we will add this entry to
        // the end of the queue.
        //

        if ( IsListEmpty( &DataQueue->DataEntryList ) ) {

            ASSERT( DataQueue->QueueState == Empty );
            DataQueue->QueueState     = Who;
            DataQueue->BytesInQueue   = dataEntry->DataSize;
            DataQueue->EntriesInQueue = 1;

        } else {

            ASSERT( DataQueue->QueueState == Who );
            DataQueue->BytesInQueue     += dataEntry->DataSize;
            DataQueue->EntriesInQueue   += 1;

        }

        //
        // Insert the new entry at the appropriate place in the data queue.
        //

        InsertTailList( previousEntry, &dataEntry->ListEntry );

    } finally {

        if ( AbnormalTermination() ) {
            if ( rewind1 != NULL ) {
                ExFreePool( rewind1 );
            }
            if ( rewind2 != NULL ) {
                ExFreePool( rewind2 );
            }
        }

        //
        // If we have not already completed the IRP, see if the IRP has
        // been cancelled.
        //

        if ( !irpCompleted ) {

            MsSetCancelRoutine( Irp );
        }
    }

    //
    // Return to the caller.
    //

    DumpDataQueue( "After AddDataQueueEntry\n", DataQueue );
    DebugTrace(-1, Dbg, "MsAddDataQueueEntry -> %08lx\n", (ULONG)dataEntry);

    return dataEntry;
}


PIRP
MsRemoveDataQueueEntry (
    IN PDATA_QUEUE DataQueue,
    IN PDATA_ENTRY DataEntry
    )

/*++

Routine Description:

    This routine removes the specified data entry from the indicated
    data queue, and possibly returns the IRP associated with the entry if
    it wasn't already completed.

    If the data entry we are removing indicates buffered I/O then we also
    need to deallocate the data buffer besides the data entry but only
    if the IRP is null.  Note that the data entry might be stored in an IRP.
    If it is then we are going to return the IRP it is stored in.

Arguments:

    DataEntry - Supplies a pointer to the data entry to remove.

Return Value:

    PIRP - Possibly returns a pointer to an IRP.

--*/

{
    FROM from;
    PIRP irp;
    ULONG dataSize;
    PVOID dataPointer;

    PAGED_CODE( );

    DebugTrace(+1, Dbg, "MsRemoveDataQueueEntry, DataEntry = %08lx\n", (ULONG)DataEntry);
    DebugTrace( 0, Dbg, "DataQueue = %08lx\n", (ULONG)DataQueue);

    //
    // Remove the data entry from the queue and update the count of
    // data entries in the queue.
    //

    --DataQueue->EntriesInQueue;
    RemoveEntryList( &DataEntry->ListEntry );

    //
    // If the queue is now empty, update the state.
    //

    if (DataQueue->EntriesInQueue == 0) {
        DataQueue->QueueState = Empty;
    }

    //
    // Capture some of the fields from the data entry to make our
    // other references a little easier.
    //

    from = DataEntry->From;
    irp = DataEntry->Irp;
    dataSize = DataEntry->DataSize;
    dataPointer = DataEntry->DataPointer;

    //
    // Check if we need to deallocate the data buffer, we only need
    // to deallocate if the IRP is null.
    //

    if ( irp == NULL ) {
        ExFreePool( dataPointer );
    }

    //
    // The preceding call returned the user's quota or it
    // simply deallocated the buffer.  If it only deallocated
    // the buffer then we need to credit our quota.
    //

    if (from == MailslotQuota) {
        DataQueue->QuotaUsed -= dataSize;
    }

    //
    // Check if we still have an IRP to return.  If we do then
    // we know that this data entry is located in the current IRP
    // stack location and we need to zero out the data entry (skipping
    // over the spare field), otherwise we got allocated from either
    // the mailslot or user quota, and we need to deallocate it ourselves.
    //

    if (irp != NULL) {

        DataEntry->From = 0;
        DataEntry->Irp = 0;
        DataEntry->DataSize = 0;
        DataEntry->DataPointer = 0;

        //
        // Null out the cancel routine
        //

        MsResetCancelRoutine( irp );

    } else {

        ExFreePool( DataEntry );

        //
        // The preceding call returned the user's quota or it
        // simply deallocated the buffer.  If it only deallocated
        // the buffer then we need to credit the mailslot quota.
        //

        if (from == MailslotQuota) {
            DataQueue->QuotaUsed -= sizeof(DATA_ENTRY);
        }
    }

    //
    // Return to the caller.
    //

    DumpDataQueue( "After RemoveDataQueueEntry\n", DataQueue );
    DebugTrace(-1, Dbg, "MsRemoveDataQueueEntry -> %08lx\n", (ULONG)irp);

    return irp;
}


VOID
MsCancelDataQueueIrp (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine implements the cancel function for an IRP saved in a
    data queue

Arguments:

    DeviceObject - ignored

    Irp - Supplies the Irp being cancelled.  A pointer to the data queue
        structure is stored in the information field of the Irp Iosb
        field.

Return Value:

    None.

--*/

{
    PFCB fcb;
    PDATA_QUEUE dataQueue;

    PDATA_ENTRY dataEntry;
    PLIST_ENTRY listEntry, nextListEntry;

    PWORK_CONTEXT workContext;
    PKTIMER timer;

    UNREFERENCED_PARAMETER( DeviceObject );

    //
    // The status field is used to store a pointer to the data queue
    // containing this IRP.
    //

    dataQueue = (PDATA_QUEUE)Irp->IoStatus.Status;

    //
    // We now need to void the cancel routine and release the io cancel
    // spin-lock.
    //

    IoSetCancelRoutine( Irp, NULL );
    IoReleaseCancelSpinLock( Irp->CancelIrql );

    //
    //  Get exclusive access to the mailslot FCB so we can now do our work.
    //

    fcb = CONTAINING_RECORD( dataQueue, FCB, DataQueue );
    MsAcquireExclusiveFcb( fcb );

    try {

        //
        // Scan the data queue looking for entries that have IRPs that
        // have been cancelled.
        //

        listEntry = dataQueue->DataEntryList.Flink;

        while ( listEntry != &dataQueue->DataEntryList ) {

            nextListEntry = listEntry->Flink;

            dataEntry = (PDATA_ENTRY)CONTAINING_RECORD(
                                         listEntry,
                                         DATA_ENTRY,
                                         ListEntry);
            //
            //  If the data entry contains an Irp and the irp is cancelled then
            //  we have some work to do.
            //

            if ( (dataEntry->Irp != NULL) && (dataEntry->Irp->Cancel)) {

                //
                // Cancel the timer, if there is one for this IRP.
                //

                workContext = dataEntry->TimeoutWorkContext;
                if (workContext != NULL) {

                    DebugTrace( 0, Dbg, "Cancelling a timer\n", 0);

                    //
                    // There was a timer on this read operation.  Attempt
                    // to cancel the operation.  If the cancel operation
                    // is successful, then we must cleanup after the operation.
                    // If it was unsuccessful the timer DPC will run, and
                    // will eventually cleanup.
                    //

                    timer = &workContext->Timer;

                    if (KeCancelTimer( timer ) ) {

                        //
                        // Release the reference to the FCB.
                        //

                        MsDereferenceFcb( workContext->Fcb );

                        //
                        // Free the memory from the work context, the timer,
                        // and the DPC.
                        //

                        ExFreePool( workContext );

                    }

                }

                //
                // Remove this data entry from the queue.
                //

                RemoveEntryList( listEntry );
                --dataQueue->EntriesInQueue;

                //
                // If the queue is now empty then we need to fix the queue
                // state.
                //

                if (IsListEmpty( &dataQueue->DataEntryList ) ) {
                    dataQueue->QueueState = Empty;
                }

                //
                // Check if we need to return mailslot quota.
                //

                if ( dataEntry->From == MailslotQuota ) {
                    dataQueue->QuotaUsed -= dataEntry->DataSize;
                }

                //
                // Complete the request saying that it has been cancelled.
                //

                MsCompleteRequest( dataEntry->Irp, STATUS_CANCELLED );

            }

            listEntry = nextListEntry;

        }

    } finally {
        MsReleaseFcb( fcb );
    }

    //
    //  And return to our caller
    //

    return;

} // MsCancelDataQueueIrp

VOID
MsResetCancelRoutine(
    IN PIRP Irp
    )

/*++

Routine Description:

    Stub to null out the cancel routine.

Arguments:

    Irp - Supplies the Irp whose cancel routine is to be nulled out.

Return Value:

    None.

--*/
{
    IoAcquireCancelSpinLock( &Irp->CancelIrql );
    IoSetCancelRoutine( Irp, NULL );
    IoReleaseCancelSpinLock( Irp->CancelIrql );

    return;

} // MsResetCancelRoutine

VOID
MsSetCancelRoutine(
    IN PIRP Irp
    )

/*++

Routine Description:

    Stub to set the cancel routine.  If the irp has already been cancelled,
    the cancel routine is called.

Arguments:

    Irp - Supplies the Irp whose cancel routine is to be set.

Return Value:

    None.

--*/
{
    IoAcquireCancelSpinLock( &Irp->CancelIrql );

    if ( Irp->Cancel ) {

        //
        // The cancel spinlock will be released by this call.
        //

        MsCancelDataQueueIrp( NULL, Irp );

    } else {
        IoSetCancelRoutine( Irp, MsCancelDataQueueIrp );
        IoReleaseCancelSpinLock( Irp->CancelIrql );
    }

    return;

} // MsSetCancelRoutine