summaryrefslogtreecommitdiffstats
path: root/private/ntos/npfs/readsup.c
blob: 0e1367c82435f7eba50879a26d1c69e4a2a5f560 (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
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    ReadSup.c

Abstract:

    This module implements the Read support routine.  This is a common
    read function that is called to do read, unbuffered read, peek, and
    transceive.

Author:

    Gary Kimura     [GaryKi]    20-Sep-1990

Revision History:

--*/

#include "NpProcs.h"

//
//  The debug trace level
//

#define Dbg                              (DEBUG_TRACE_READSUP)

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, NpReadDataQueue)
#endif


IO_STATUS_BLOCK
NpReadDataQueue (
    IN PDATA_QUEUE ReadQueue,
    IN BOOLEAN PeekOperation,
    IN BOOLEAN ReadOverflowOperation,
    IN PUCHAR ReadBuffer,
    IN ULONG ReadLength,
    IN READ_MODE ReadMode,
    IN PCCB Ccb
    )

/*++

Routine Description:

    This procedure reads data from the read queue and fills up the
    read buffer.  It will also dequeue the queue or leave it alone based
    on an input parameter.

Arguments:

    ReadQueue - Provides the read queue to examine.  Its state must
        already be set to WriteEntries.

    PeekOperation - Indicates if the operation is to dequeue information
        off of the queue as it is being read or leave the queue alone.
        TRUE means to leave the queue alone.

    ReadOverflowOperation - Indicates if this is a read overflow operation.
        With read overflow we will not alter the named pipe if the data
        will overflow the read buffer.

    ReadBuffer - Supplies a buffer to receive the data

    ReadLength - Supplies the length, in bytes, of ReadBuffer.

    ReadMode - Indicates if the read operation is message mode or
        byte stream mode.

    NamedPipeEnd - Supplies the end of the named pipe doing the read

    Ccb - Supplies the ccb for the pipe

Return Value:

    IO_STATUS_BLOCK - Indicates the result of the operation.

--*/

{
    IO_STATUS_BLOCK Iosb;

    PDATA_ENTRY DataEntry;

    ULONG ReadRemaining;
    ULONG AmountRead;

    PUCHAR WriteBuffer;
    ULONG WriteLength;
    ULONG WriteRemaining;

    ULONG AmountToCopy;

    PAGED_CODE();

    DebugTrace(+1, Dbg, "NpReadDataQueue\n", 0);
    DebugTrace( 0, Dbg, "ReadQueue             = %08lx\n", ReadQueue);
    DebugTrace( 0, Dbg, "PeekOperation         = %08lx\n", PeekOperation);
    DebugTrace( 0, Dbg, "ReadOverflowOperation = %08lx\n", ReadOverflowOperation);
    DebugTrace( 0, Dbg, "ReadBuffer            = %08lx\n", ReadBuffer);
    DebugTrace( 0, Dbg, "ReadLength            = %08lx\n", ReadLength);
    DebugTrace( 0, Dbg, "ReadMode              = %08lx\n", ReadMode);
    DebugTrace( 0, Dbg, "Ccb                   = %08lx\n", Ccb);

    //
    //  If this is an overflow operation then we will force us to do peeks.
    //  Later when are determine that the opreation succeeded we will complete
    //  the write irp.
    //

    if (ReadOverflowOperation) {

        PeekOperation = TRUE;
    }

    //
    //  Now for every real data entry we loop until either we run out
    //  of data entries or until the read buffer is full
    //

    ReadRemaining = ReadLength;
    Iosb.Status = STATUS_SUCCESS;
    AmountRead = 0;

    for (DataEntry = (PeekOperation ? NpGetNextDataQueueEntry( ReadQueue, NULL )
                                    : NpGetNextRealDataQueueEntry( ReadQueue ));

         (DataEntry != NULL) && (ReadRemaining > 0);

         DataEntry = (PeekOperation ? NpGetNextDataQueueEntry( ReadQueue, DataEntry )
                                    : NpGetNextRealDataQueueEntry( ReadQueue ))) {

        DebugTrace(0, Dbg, "Top of Loop\n", 0);
        DebugTrace(0, Dbg, "ReadRemaining  = %08lx\n", ReadRemaining);

        //
        //  If this is a peek operation then make sure we got a real
        //  data entry and not a close or flush
        //

        if (!PeekOperation ||
            (DataEntry->DataEntryType == Buffered) ||
            (DataEntry->DataEntryType == Unbuffered)) {

            //
            //  Calculate how much data is in this entry.  The write
            //  remaining is based on whether this is the first entry
            //  in the queue or a later data entry
            //

            WriteBuffer = DataEntry->DataPointer;
            WriteLength = DataEntry->DataSize;
            WriteRemaining = WriteLength;

            if (DataEntry == NpGetNextDataQueueEntry( ReadQueue, NULL )) {

                WriteRemaining -= ReadQueue->NextByteOffset;
            }

            DebugTrace(0, Dbg, "WriteBuffer    = %08lx\n", WriteBuffer);
            DebugTrace(0, Dbg, "WriteLength    = %08lx\n", WriteLength);
            DebugTrace(0, Dbg, "WriteRemaining = %08lx\n", WriteRemaining);

            //
            //  copy data from the write buffer at write offset to the
            //  read buffer at read offset by the mininum of write
            //  remaining or read remaining
            //

            AmountToCopy = (WriteRemaining < ReadRemaining ? WriteRemaining
                                                           : ReadRemaining);

            try {

                RtlCopyMemory( &ReadBuffer[ ReadLength - ReadRemaining ],
                               &WriteBuffer[ WriteLength - WriteRemaining ],
                               AmountToCopy );

            } except(EXCEPTION_EXECUTE_HANDLER) {

                ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
            }

            //
            //  Update the Read and Write remaining counts, the total
            //  amount we've read and the next byte offset field in the
            //  read queue
            //

            ReadRemaining  -= AmountToCopy;
            WriteRemaining -= AmountToCopy;
            AmountRead += AmountToCopy;

            if (!PeekOperation) {

                ReadQueue->NextByteOffset = WriteLength - WriteRemaining;
            }

            //
            //  Now update the security fields in the ccb
            //

            NpCopyClientContext( Ccb, DataEntry );

            //
            //  If the remaining write length is greater than zero
            //  then we've filled up the read buffer so we need to
            //  figure out if its an overflow error
            //

            if (WriteRemaining > 0 ||
                (ReadOverflowOperation && (AmountRead == 0))) {

                DebugTrace(0, Dbg, "Write remaining is > 0\n", 0);

                if (ReadMode == FILE_PIPE_MESSAGE_MODE) {

                    DebugTrace(0, Dbg, "Overflow message mode read\n", 0);

                    //
                    //  Set the status field and break out of the for-loop.
                    //

                    Iosb.Status = STATUS_BUFFER_OVERFLOW;
                    break;
                }

            } else {

                DebugTrace(0, Dbg, "Remaining Write is zero\n", 0);

                //
                //  The write entry is done so remove it from the read
                //  queue, if this is not a peek operation.  This might
                //  also have an Irp that needs to be completed
                //

                if (!PeekOperation || ReadOverflowOperation) {

                    PIRP WriteIrp;

                    //
                    //  For a read overflow operation we need to get the read data
                    //  queue entry and remove it.
                    //

                    if (ReadOverflowOperation) {
                        PDATA_ENTRY TempDataEntry;
                        TempDataEntry = NpGetNextRealDataQueueEntry( ReadQueue );
                        ASSERT(TempDataEntry == DataEntry);
                    }

                    if ((WriteIrp = NpRemoveDataQueueEntry( ReadQueue )) != NULL) {

                        NpCompleteRequest( WriteIrp, STATUS_SUCCESS );
                    }
                }

                //
                //  And if we are doing message mode reads then we'll
                //  work on completing this irp without going back
                //  to the top of the loop
                //

                if (ReadMode == FILE_PIPE_MESSAGE_MODE) {

                    DebugTrace(0, Dbg, "Successful message mode read\n", 0);

                    //
                    //  Set the status field and break out of the for-loop.
                    //

                    Iosb.Status = STATUS_SUCCESS;
                    break;
                }

                ASSERTMSG("Srv cannot use read overflow on a byte stream pipe ", !ReadOverflowOperation);
            }
        }
    }

    DebugTrace(0, Dbg, "End of loop, AmountRead = %08lx\n", AmountRead);

    Iosb.Information = AmountRead;

    DebugTrace(-1, Dbg, "NpReadDataQueue -> Iosb.Status = %08lx\n", Iosb.Status);
    return Iosb;
}