1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7     FxFileObject.hpp
8 
9 Abstract:
10 
11     This module implements a frameworks managed FileObject
12 
13 Author:
14 
15 
16 
17 Environment:
18 
19     Both kernel and user mode
20 
21 Revision History:
22 
23 
24 --*/
25 
26 #ifndef _FXFILEOBJECT_H_
27 #define _FXFILEOBJECT_H_
28 
29 class FxFileObject : public FxNonPagedObject, public IFxHasCallbacks {
30 
31 private:
32 
33     // Pointer to WDM FileObject
34     MxFileObject        m_FileObject;
35     PVOID               m_PkgContext;
36 
37 #if (FX_CORE_MODE == FX_CORE_USER_MODE)
38     //
39     // KMDF retrieves the name unicode string directly from wdm file object,
40     // and returns it to caller. UMDF doesn't provide a unicode string and
41     // therefore we allocate it in this object and return that to caller after
42     // populating it with name string retrieved from host.
43     //
44     UNICODE_STRING m_FileName;
45 
46     //
47     // Framework related file object
48     //
49     FxFileObject* m_RelatedFileObject;
50 
51 #endif
52 
53 public:
54 
55     // ListEntry for linking FileObjects off of the device
56     LIST_ENTRY         m_Link;
57 
58 private:
59 
60     VOID
61     SetFileObjectContext(
62         _In_ MdFileObject WdmFileObject,
63         _In_ WDF_FILEOBJECT_CLASS NormalizedFileClass,
64         _In_ MdIrp Irp,
65         _In_ FxDevice* Device
66         );
67 
68 public:
69     FxFileObject(
70         __in PFX_DRIVER_GLOBALS FxDriverGlobals,
71         __in MdFileObject       pWdmFileObject,
72         __in FxDevice*          Device
73         );
74 
75     virtual
76     ~FxFileObject(
77        );
78 
79     __inline
80     WDFFILEOBJECT
GetHandle(VOID)81     GetHandle(
82         VOID
83         )
84     {
85         return (WDFFILEOBJECT) GetObjectHandle();
86     }
87 
88     __inline
89     MdFileObject
GetWdmFileObject(VOID)90     GetWdmFileObject(
91         VOID
92         )
93     {
94         return m_FileObject.GetFileObject();
95     }
96 
97     __inline
98     CfxDevice*
GetDevice(VOID)99     GetDevice(
100         VOID
101         )
102     {
103         return m_Device;
104     }
105 
106     __inline
107     PUNICODE_STRING
GetFileName(VOID)108     GetFileName(
109         VOID
110         )
111     {
112     #if (FX_CORE_MODE == FX_CORE_KERNEL_MODE)
113         return m_FileObject.GetFileName(NULL);
114     #else
115         return m_FileObject.GetFileName(&m_FileName);
116     #endif
117     }
118 
119     __inline
120     PLARGE_INTEGER
GetCurrentByteOffset(VOID)121     GetCurrentByteOffset(
122         VOID
123         )
124     {
125         return m_FileObject.GetCurrentByteOffset();
126     }
127 
128     __inline
129     ULONG
GetFlags(VOID)130     GetFlags(
131         VOID
132         )
133     {
134         return m_FileObject.GetFlags();
135     }
136 
137     __inline
138     VOID
SetPkgCleanupCloseContext(PVOID Context)139     SetPkgCleanupCloseContext(
140         PVOID Context
141         )
142     {
143         m_PkgContext = Context;
144     }
145 
146     __inline
147     PVOID
GetPkgCleanupCloseContext(VOID)148     GetPkgCleanupCloseContext(
149         VOID
150         )
151     {
152         return m_PkgContext;
153     }
154 
155     //
156     // Create a WDFFILEOBJECT from the WDM PFILE_OBJECT
157     // and associate it with the WDM PFILE_OBJECT according
158     // to the FileObjectClass.
159     //
160     _Must_inspect_result_
161     static
162     NTSTATUS
163     _CreateFileObject(
164         __in FxDevice*                   pDevice,
165         __in MdIrp                        Irp,
166         __in WDF_FILEOBJECT_CLASS        FileObjectClass,
167         __in_opt PWDF_OBJECT_ATTRIBUTES  pObjectAttributes,
168         __in_opt MdFileObject            pWdmFileObject,
169         __deref_out_opt FxFileObject**   ppFxFileObject
170         );
171 
172     VOID
173     Initialize(
174         _In_ MdIrp CreateIrp
175         );
176 
177     //
178     // Destroy (dereference) the WDFFILEOBJECT related to the
179     // WDM PFILE_OBJECT according to its FileObjectClass.
180     //
181     static
182     VOID
183     _DestroyFileObject(
184         __in FxDevice*                   pDevice,
185         __in WDF_FILEOBJECT_CLASS        FileObjectClass,
186         __in_opt MdFileObject            pWdmFileObject
187         );
188 
189     //
190     // Return the FxFileObject* for the given WDM PFILE_OBJECT
191     // based on the FileObjectClass.
192     //
193     _Must_inspect_result_
194     static
195     NTSTATUS
196     _GetFileObjectFromWdm(
197         __in  FxDevice*                   pDevice,
198         __in  WDF_FILEOBJECT_CLASS        FileObjectClass,
199         __in_opt  MdFileObject            pWdmFileObject,
200         __deref_out_opt FxFileObject**    ppFxFileObject
201         );
202 
203     VOID
204     DeleteFileObjectFromFailedCreate(
205         VOID
206         );
207 
208     // begin FxObject overrides
209     _Must_inspect_result_
210     NTSTATUS
211     QueryInterface(
212         __in FxQueryInterfaceParams* Params
213         );
214     // end FxObject overrides
215 
216     // begin IFxHasCallbacks overrides
217     VOID
218     GetConstraints(
219         __in WDF_EXECUTION_LEVEL*       ExecutionLevel,
220         __in WDF_SYNCHRONIZATION_SCOPE* SynchronizationScope
221         ) ;
222 
223     _Must_inspect_result_
224     FxCallbackLock*
225     GetCallbackLockPtr(
226         __deref_out_opt FxObject** LockObject
227         );
228     // end IFxHasCallbacks overrides
229 
230     //
231     // Update the process keep-alive count
232     // TRUE: increment, FALSE: decrement
233     //
234     _Must_inspect_result_
235     NTSTATUS
236     UpdateProcessKeepAliveCount(
237         _In_ BOOLEAN Increment
238         );
239 
240 #if (FX_CORE_MODE == FX_CORE_USER_MODE)
241 
242     FxFileObject*
GetRelatedFileObject(VOID)243     GetRelatedFileObject(
244         VOID
245         )
246     {
247         return m_RelatedFileObject;
248     }
249 
250 #endif
251 
252 };
253 
254 #endif // _FXFILEOBJECT_H_
255 
256