[FLTMGR]
[reactos.git] / reactos / drivers / filters / fltmgr / Object.c
1 /*
2 * PROJECT: Filesystem Filter Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/fs_minifilter/fltmgr/Object.c
5 * PURPOSE: Miscellaneous library functions
6 * PROGRAMMERS: Ged Murphy (gedmurphy@reactos.org)
7 */
8
9 // NOTE: Split this file into filter object and device object functions
10 // when the code base grows sufficiently
11
12 /* INCLUDES ******************************************************************/
13
14 #include "fltmgr.h"
15
16 #define NDEBUG
17 #include <debug.h>
18
19
20 /* DATA *********************************************************************/
21
22
23
24 /* EXPORTED FUNCTIONS ******************************************************/
25
26
27 NTSTATUS
28 FLTAPI
29 FltObjectReference(_Inout_ PVOID Object)
30 {
31 if (!FltpExAcquireRundownProtection(&((PFLT_OBJECT)Object)->RundownRef))
32 {
33 return STATUS_FLT_DELETING_OBJECT;
34 }
35
36 return STATUS_SUCCESS;
37 }
38
39 VOID
40 FLTAPI
41 FltObjectDereference(_Inout_ PVOID Object)
42 {
43 FltpExReleaseRundownProtection(&((PFLT_OBJECT)Object)->RundownRef);
44 }
45
46
47
48 /* INTERNAL FUNCTIONS ******************************************************/
49
50 VOID
51 FltpExInitializeRundownProtection(_Out_ PEX_RUNDOWN_REF RundownRef)
52 {
53 ExInitializeRundownProtection(RundownRef);
54 }
55
56 BOOLEAN
57 FltpExAcquireRundownProtection(_Inout_ PEX_RUNDOWN_REF RundownRef)
58 {
59 return ExAcquireRundownProtection(RundownRef);
60 }
61
62 BOOLEAN
63 FltpExReleaseRundownProtection(_Inout_ PEX_RUNDOWN_REF RundownRef)
64 {
65 ExReleaseRundownProtection(RundownRef);
66 return TRUE;
67 }
68
69 BOOLEAN
70 FltpExRundownCompleted(_Inout_ PEX_RUNDOWN_REF RundownRef)
71 {
72 return _InterlockedExchange((PLONG)RundownRef, 1);
73 }
74
75 NTSTATUS
76 NTAPI
77 FltpObjectRundownWait(_Inout_ PEX_RUNDOWN_REF RundownRef)
78 {
79 //return FltpExWaitForRundownProtectionRelease(RundownRef);
80 return 0;
81 }
82
83 NTSTATUS
84 FltpGetBaseDeviceObjectName(_In_ PDEVICE_OBJECT DeviceObject,
85 _Inout_ PUNICODE_STRING ObjectName)
86 {
87 PDEVICE_OBJECT BaseDeviceObject;
88 NTSTATUS Status;
89
90 /*
91 * Get the lowest device object on the stack, which may be the
92 * object we were passed, and lookup the name for that object
93 */
94 BaseDeviceObject = IoGetDeviceAttachmentBaseRef(DeviceObject);
95 Status = FltpGetObjectName(BaseDeviceObject, ObjectName);
96 ObDereferenceObject(BaseDeviceObject);
97
98 return Status;
99 }
100
101 NTSTATUS
102 FltpGetObjectName(_In_ PVOID Object,
103 _Inout_ PUNICODE_STRING ObjectName)
104 {
105 POBJECT_NAME_INFORMATION ObjectNameInfo = NULL;
106 OBJECT_NAME_INFORMATION LocalNameInfo;
107 ULONG ReturnLength;
108 NTSTATUS Status;
109
110 if (ObjectName == NULL)
111 return STATUS_INVALID_PARAMETER;
112
113 /* Get the size of the buffer required to hold the nameinfo */
114 Status = ObQueryNameString(Object,
115 &LocalNameInfo,
116 sizeof(LocalNameInfo),
117 &ReturnLength);
118 if (Status == STATUS_INFO_LENGTH_MISMATCH)
119 {
120 ObjectNameInfo = ExAllocatePoolWithTag(PagedPool,
121 ReturnLength,
122 FM_TAG_UNICODE_STRING);
123 if (ObjectNameInfo == NULL) return STATUS_INSUFFICIENT_RESOURCES;
124
125 /* Get the actual name info now we have the buffer to hold it */
126 Status = ObQueryNameString(Object,
127 ObjectNameInfo,
128 ReturnLength,
129 &ReturnLength);
130 }
131
132
133 if (NT_SUCCESS(Status))
134 {
135 /* Make sure the buffer we were passed is large enough to hold the string */
136 if (ObjectName->MaximumLength < ObjectNameInfo->Name.Length)
137 {
138 /* It wasn't, let's enlarge the buffer */
139 Status = FltpReallocateUnicodeString(ObjectName,
140 ObjectNameInfo->Name.Length,
141 FALSE);
142
143 }
144
145 if (NT_SUCCESS(Status))
146 {
147 /* Copy the object name into the callers buffer */
148 RtlCopyUnicodeString(ObjectName, &ObjectNameInfo->Name);
149 }
150 }
151
152 if (ObjectNameInfo)
153 {
154 ExFreePoolWithTag(ObjectNameInfo, FM_TAG_UNICODE_STRING);
155 }
156
157 return Status;
158 }