1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7     FxRequestContext.cpp
8 
9 Abstract:
10 
11     This module implements FxRequest object
12 
13 Author:
14 
15 
16 
17 Environment:
18 
19     Both kernel and user mode
20 
21 Revision History:
22 
23 
24 
25 --*/
26 
27 #include "coreprivshared.hpp"
28 
29 // Tracing support
30 extern "C" {
31 // #include "FxRequestContext.tmh"
32 }
33 
34 FxRequestContext::FxRequestContext(
35     __in FX_REQUEST_CONTEXT_TYPE Type
36     ) :
37     m_RequestType(Type),
38     m_RequestMemory(NULL)
39 
40 /*++
41 
42 Routine Description:
43     Constructs an FxRequestContext and initialized the m_RequestType field
44 
45 Arguments:
46     Type - The type of this request.
47 
48 
49 
50 
51 
52 Return Value:
53     None.
54 
55   --*/
56 {
57     InitCompletionParams();
58 }
59 
60 FxRequestContext::~FxRequestContext()
61 /*++
62 
63 Routine Description:
64     Destruct for an FxRequestContext.  Releases all outstanding references.
65 
66 Arguments:
67     None
68 
69 Return Value:
70     None
71 
72   --*/
73 {
74     ASSERT(m_RequestMemory == NULL);
75 }
76 
77 VOID
78 FxRequestContext::StoreAndReferenceMemory(
79     __in FxRequestBuffer* Buffer
80     )
81 {
82     _StoreAndReferenceMemoryWorker(this, &m_RequestMemory, Buffer);
83 }
84 
85 VOID
86 FxRequestContext::ReleaseAndRestore(
87     __in FxRequestBase* Request
88     )
89 /*++
90 
91 Routine Description:
92     This routine releases any outstanding references taken on the previous
93     format call and restores any fields in the PIRP that were overwritten
94     when the formatting occurred.
95 
96 Arguments:
97     Irp
98 
99 Return Value:
100 
101 
102   --*/
103 
104 {
105 #if (FX_CORE_MODE == FX_CORE_KERNEL_MODE)
106     Request->FreeMdls();
107 #else
108     UNREFERENCED_PARAMETER(Request);
109 #endif
110 
111     if (m_RequestMemory != NULL) {
112         m_RequestMemory->RELEASE(this);
113         m_RequestMemory = NULL;
114     }
115 
116     InitCompletionParams();
117 }
118 
119 VOID
120 FxRequestContext::_StoreAndReferenceMemoryWorker(
121     __in PVOID Tag,
122     __deref_out_opt IFxMemory** PPMemory,
123     __in FxRequestBuffer* Buffer
124     )
125 {
126     ASSERT(*PPMemory == NULL);
127 
128     switch (Buffer->DataType) {
129     case FxRequestBufferMemory:
130         Buffer->u.Memory.Memory->ADDREF(Tag);
131         *PPMemory = Buffer->u.Memory.Memory;
132         break;
133 
134     case FxRequestBufferReferencedMdl:
135         Buffer->u.RefMdl.Memory->ADDREF(Tag);
136         *PPMemory = Buffer->u.RefMdl.Memory;
137         break;
138 
139     default:
140         *PPMemory = NULL;
141     }
142 }
143 
144 VOID
145 FxRequestContext::FormatWriteParams(
146     __in_opt IFxMemory* WriteMemory,
147     __in_opt PWDFMEMORY_OFFSET WriteOffsets
148     )
149 {
150     m_CompletionParams.Type = WdfRequestTypeWrite;
151 
152     if (WriteMemory != NULL) {
153         m_CompletionParams.Parameters.Write.Buffer = WriteMemory->GetHandle();
154     }
155 
156     if (WriteOffsets != NULL) {
157         m_CompletionParams.Parameters.Write.Offset =
158             WriteOffsets->BufferOffset;
159     }
160     else {
161         m_CompletionParams.Parameters.Write.Offset = 0;
162     }
163 }
164 
165 VOID
166 FxRequestContext::FormatReadParams(
167     __in_opt IFxMemory* ReadMemory,
168     __in_opt PWDFMEMORY_OFFSET ReadOffsets
169     )
170 {
171     m_CompletionParams.Type = WdfRequestTypeRead;
172 
173     if (ReadMemory != NULL) {
174         m_CompletionParams.Parameters.Read.Buffer = ReadMemory->GetHandle();
175     }
176 
177     if (ReadOffsets != NULL) {
178         m_CompletionParams.Parameters.Read.Offset =
179             ReadOffsets->BufferOffset;
180     }
181     else {
182         m_CompletionParams.Parameters.Read.Offset = 0;
183     }
184 }
185 
186 VOID
187 FxRequestContext::FormatOtherParams(
188     __in FxInternalIoctlParams *InternalIoctlParams
189     )
190 {
191     m_CompletionParams.Type = WdfRequestTypeOther;
192     m_CompletionParams.Parameters.Others.Argument1.Ptr = InternalIoctlParams->Argument1;
193     m_CompletionParams.Parameters.Others.Argument2.Ptr = InternalIoctlParams->Argument2;
194     m_CompletionParams.Parameters.Others.Argument4.Ptr = InternalIoctlParams->Argument4;
195 }
196