ReactOS  0.4.14-dev-614-gbfd8a84
ob_x.h
Go to the documentation of this file.
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/include/internal/ob_x.h
5 * PURPOSE: Internal Inlined Functions for the Object Manager
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8 
9 #include "ex.h"
10 
11 #define OBP_LOCK_STATE_PRE_ACQUISITION_EXCLUSIVE 0xAAAA1234
12 #define OBP_LOCK_STATE_PRE_ACQUISITION_SHARED 0xBBBB1234
13 #define OBP_LOCK_STATE_POST_ACQUISITION_EXCLUSIVE 0xCCCC1234
14 #define OBP_LOCK_STATE_POST_ACQUISITION_SHARED 0xDDDD1234
15 #define OBP_LOCK_STATE_RELEASED 0xEEEE1234
16 #define OBP_LOCK_STATE_INITIALIZED 0xFFFF1234
17 
18 #define OBP_NAME_LOOKASIDE_MAX_SIZE 248
19 
21 ULONG
24 {
25  if (PreviousMode == KernelMode)
26  {
27  /* For kernel, allow any valid attributes */
29  }
30  else
31  {
32  /* For user, mask out kernel-only attributes */
33  return (Attributes & OBJ_VALID_ATTRIBUTES) &
35  }
36 }
37 
39 ULONG
41 {
42  /* We have 4 locks total, this will return a 0-index slot */
43  return (((ULONG_PTR)ObjectHeader) >> 8) & 3;
44 }
45 
47 VOID
49 {
50  ULONG Slot;
51  POBJECT_TYPE ObjectType = ObjectHeader->Type;
52 
53  /* Sanity check */
55 
56  /* Pick a slot */
57  Slot = ObpSelectObjectLockSlot(ObjectHeader);
58 
59  /* Enter a critical region and acquire the resource */
61  ExAcquireResourceExclusiveLite(&ObjectType->ObjectLocks[Slot], TRUE);
62 }
63 
65 VOID
67 {
68  ULONG Slot;
69  POBJECT_TYPE ObjectType = ObjectHeader->Type;
70 
71  /* Sanity check */
73 
74  /* Pick a slot */
75  Slot = ObpSelectObjectLockSlot(ObjectHeader);
76 
77  /* Enter a critical region and acquire the resource */
79  ExAcquireResourceSharedLite(&ObjectType->ObjectLocks[Slot], TRUE);
80 }
81 
83 VOID
85 {
86  ULONG Slot;
87  POBJECT_TYPE ObjectType = ObjectHeader->Type;
88 
89  /* Pick a slot */
90  Slot = ObpSelectObjectLockSlot(ObjectHeader);
91 
92  /* Release the resource and leave a critical region */
93  ExReleaseResourceLite(&ObjectType->ObjectLocks[Slot]);
95 
96  /* Sanity check */
98 }
99 
103 {
104  POBJECT_HEADER_NAME_INFO ObjectNameInfo;
105  ULONG NewValue, References;
106 
107  /* Make sure we have name information at all */
108  ObjectNameInfo = OBJECT_HEADER_TO_NAME_INFO(ObjectHeader);
109  if (!ObjectNameInfo) return NULL;
110 
111  /* Get the number of references */
112  References = ObjectNameInfo->QueryReferences;
113  for (;;)
114  {
115  /* Check if the count is 0 and fail if so */
116  if (!References) return NULL;
117 
118  /* Increment the number of references */
119  NewValue = InterlockedCompareExchange((PLONG)&ObjectNameInfo->
120  QueryReferences,
121  References + 1,
122  References);
123  if (NewValue == References) break;
124 
125  /* We failed, try again */
126  References = NewValue;
127  }
128 
129  /* Check for magic flag */
130  if (ObjectNameInfo->QueryReferences & 0x80000000)
131  {
132  /* FIXME: Unhandled*/
133  DbgPrint("OB: Unhandled path\n");
134  ASSERT(FALSE);
135  }
136 
137  /* Return the name information */
138  return ObjectNameInfo;
139 }
140 
142 VOID
144 {
146 
147  /* Bail out if there's no info at all */
148  if (!HeaderNameInfo) return;
149 
150  /* Remove a query reference and check if it was the last one */
151  if (!InterlockedDecrement((PLONG)&HeaderNameInfo->QueryReferences))
152  {
153  /* Check if we have a name */
154  if (HeaderNameInfo->Name.Buffer)
155  {
156  /* We can get rid of the object name now */
157  ExFreePoolWithTag(HeaderNameInfo->Name.Buffer, OB_NAME_TAG);
158  RtlInitEmptyUnicodeString(&HeaderNameInfo->Name, NULL, 0);
159  }
160 
161  /* Check if the object has a directory associated to it */
162  Directory = HeaderNameInfo->Directory;
163  if (Directory)
164  {
165  /* Delete the directory */
166  HeaderNameInfo->Directory = NULL;
168  }
169  }
170 }
171 
173 VOID
176 {
177  /* It's not, set lock flag */
178  Context->LockStateSignature = OBP_LOCK_STATE_PRE_ACQUISITION_SHARED;
179 
180  /* Lock it */
183 
184  /* Update lock flag */
185  Context->LockStateSignature = OBP_LOCK_STATE_POST_ACQUISITION_SHARED;
186 }
187 
189 VOID
192 {
193  /* Update lock flag */
195 
196  /* Acquire an exclusive directory lock */
199 
200  /* Set the directory */
201  Context->Directory = Directory;
202 
203  /* Update lock settings */
205  Context->DirectoryLocked = TRUE;
206 }
207 
209 VOID
212 {
213  /* Release the lock */
215  Context->LockStateSignature = OBP_LOCK_STATE_RELEASED;
217 }
218 
220 VOID
222 {
223  /* Initialize a null context */
224  Context->Object = NULL;
225  Context->Directory = NULL;
226  Context->DirectoryLocked = FALSE;
227  Context->LockStateSignature = OBP_LOCK_STATE_INITIALIZED;
228 }
229 
231 VOID
233 {
234  POBJECT_HEADER ObjectHeader;
235  POBJECT_HEADER_NAME_INFO HeaderNameInfo;
236 
237  /* Check if we had found an object */
238  if (Context->Object)
239  {
240  /* Get the object name information */
241  ObjectHeader = OBJECT_TO_OBJECT_HEADER(Context->Object);
242  HeaderNameInfo = OBJECT_HEADER_TO_NAME_INFO(ObjectHeader);
243 
244  /* release the name information */
245  ObpDereferenceNameInfo(HeaderNameInfo);
246 
247  /* Dereference the object */
248  ObDereferenceObject(Context->Object);
249  Context->Object = NULL;
250  }
251 }
252 
254 VOID
256 {
257  /* Check if we came back with the directory locked */
258  if (Context->DirectoryLocked)
259  {
260  /* Release the lock */
262  Context->Directory = NULL;
263  Context->DirectoryLocked = FALSE;
264  }
265 
266  /* Clear the context */
268 }
269 
271 VOID
273 {
274  /* Sanity check */
276 
277  /* Enter a critical region and acquire the resource */
280 }
281 
283 VOID
285 {
286  /* Enter a critical region and acquire the resource */
289 
290  /* Sanity check */
292 }
293 
295 VOID
297 {
298  /* Check if we have a security descriptor */
299  if (ObjectCreateInfo->SecurityDescriptor)
300  {
301  /* Release it */
302  SeReleaseSecurityDescriptor(ObjectCreateInfo->SecurityDescriptor,
303  ObjectCreateInfo->ProbeMode,
304  TRUE);
305  ObjectCreateInfo->SecurityDescriptor = NULL;
306  }
307 }
308 
310 PVOID
312 {
313  PVOID Buffer;
315  PKPRCB Prcb = KeGetCurrentPrcb();
316 
317  /* Get the P list first */
319 
320  /* Attempt allocation */
321  List->L.TotalAllocates++;
322  Buffer = (PVOID)InterlockedPopEntrySList(&List->L.ListHead);
323  if (!Buffer)
324  {
325  /* Let the balancer know that the P list failed */
326  List->L.AllocateMisses++;
327 
328  /* Try the L List */
330  List->L.TotalAllocates++;
331  Buffer = (PVOID)InterlockedPopEntrySList(&List->L.ListHead);
332  if (!Buffer)
333  {
334  /* Let the balancer know the L list failed too */
335  List->L.AllocateMisses++;
336 
337  /* Allocate it */
338  Buffer = List->L.Allocate(List->L.Type, List->L.Size, List->L.Tag);
339  }
340  }
341 
342  /* Return buffer */
343  return Buffer;
344 }
345 
347 VOID
350 {
352  PKPRCB Prcb = KeGetCurrentPrcb();
353 
354  /* Use the P List */
356  List->L.TotalFrees++;
357 
358  /* Check if the Free was within the Depth or not */
359  if (ExQueryDepthSList(&List->L.ListHead) >= List->L.Depth)
360  {
361  /* Let the balancer know */
362  List->L.FreeMisses++;
363 
364  /* Use the L List */
366  List->L.TotalFrees++;
367 
368  /* Check if the Free was within the Depth or not */
369  if (ExQueryDepthSList(&List->L.ListHead) >= List->L.Depth)
370  {
371  /* All lists failed, use the pool */
372  List->L.FreeMisses++;
373  List->L.Free(Buffer);
374  }
375  else
376  {
377  /* The free was within the Depth */
378  InterlockedPushEntrySList(&List->L.ListHead,
380  }
381  }
382  else
383  {
384  /* The free was within the Depth */
385  InterlockedPushEntrySList(&List->L.ListHead,
387  }
388 }
389 
391 VOID
393 {
394  /* First release the attributes, then free them from the lookaside list */
395  ObpReleaseObjectCreateInformation(ObjectCreateInfo);
397 }
398 
399 #if DBG
401 VOID
402 ObpCalloutStart(IN PKIRQL CalloutIrql)
403 {
404  /* Save the callout IRQL */
405  *CalloutIrql = KeGetCurrentIrql();
406 }
407 
409 VOID
410 ObpCalloutEnd(IN KIRQL CalloutIrql,
411  IN PCHAR Procedure,
413  IN PVOID Object)
414 {
415  /* Detect IRQL change */
416  if (CalloutIrql != KeGetCurrentIrql())
417  {
418  /* Print error */
419  DbgPrint("OB: ObjectType: %wZ Procedure: %s Object: %p\n",
420  &ObjectType->Name, Procedure, Object);
421  DbgPrint(" Returned at %x IRQL, but was called at %x IRQL\n",
422  KeGetCurrentIrql(), CalloutIrql);
423  DbgBreakPoint();
424  }
425 }
426 #else
428 VOID
430 {
431  /* No-op */
432  UNREFERENCED_PARAMETER(CalloutIrql);
433 }
434 
436 VOID
437 ObpCalloutEnd(IN KIRQL CalloutIrql,
438  IN PCHAR Procedure,
440  IN PVOID Object)
441 {
442  UNREFERENCED_PARAMETER(CalloutIrql);
443 }
444 #endif
#define KeGetCurrentIrql()
Definition: env_spec_w32.h:706
signed char * PCHAR
Definition: retypes.h:7
ObjectType
Definition: metafile.c:80
FORCEINLINE VOID ExAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock)
Definition: ex.h:1022
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
FORCEINLINE VOID ObpAcquireObjectLock(IN POBJECT_HEADER ObjectHeader)
Definition: ob_x.h:48
FORCEINLINE VOID ObpFreeCapturedAttributes(IN PVOID Buffer, IN PP_NPAGED_LOOKASIDE_NUMBER Type)
Definition: ob_x.h:348
Type
Definition: Type.h:6
#define DbgPrint
Definition: loader.c:25
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
FORCEINLINE VOID ObpReleaseObjectLock(IN POBJECT_HEADER ObjectHeader)
Definition: ob_x.h:84
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1062
FORCEINLINE VOID ObpReleaseObjectCreateInformation(IN POBJECT_CREATE_INFORMATION ObjectCreateInfo)
Definition: ob_x.h:296
#define InterlockedCompareExchange
Definition: interlocked.h:104
#define OBJECT_HEADER_TO_NAME_INFO(h)
Definition: obtypes.h:114
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
void DbgBreakPoint()
Definition: mach.c:553
PSLIST_ENTRY WINAPI InterlockedPopEntrySList(PSLIST_HEADER ListHead)
Definition: interlocked.c:55
FORCEINLINE VOID ObpReleaseDirectoryLock(IN POBJECT_DIRECTORY Directory, IN POBP_LOOKUP_CONTEXT Context)
Definition: ob_x.h:210
NTSTATUS NTAPI SeReleaseSecurityDescriptor(IN PSECURITY_DESCRIPTOR CapturedSecurityDescriptor, IN KPROCESSOR_MODE CurrentMode, IN BOOLEAN CaptureIfKernelMode)
Definition: sd.c:766
struct LOOKASIDE_ALIGN _NPAGED_LOOKASIDE_LIST * PNPAGED_LOOKASIDE_LIST
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define OBJECT_TO_OBJECT_HEADER(o)
Definition: obtypes.h:111
BOOLEAN NTAPI ExAcquireResourceExclusiveLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:770
UCHAR KIRQL
Definition: env_spec_w32.h:591
FORCEINLINE PVOID ObpAllocateObjectCreateInfoBuffer(IN PP_NPAGED_LOOKASIDE_NUMBER Type)
Definition: ob_x.h:311
PP_LOOKASIDE_LIST PPLookasideList[16]
Definition: ketypes.h:624
#define OBP_LOCK_STATE_RELEASED
Definition: ob_x.h:15
VOID NTAPI ObDereferenceObjectDeferDelete(IN PVOID Object)
Definition: obref.c:360
#define OBP_LOCK_STATE_INITIALIZED
Definition: ob_x.h:16
FORCEINLINE VOID ObpAcquireDirectoryLockShared(IN POBJECT_DIRECTORY Directory, IN POBP_LOOKUP_CONTEXT Context)
Definition: ob_x.h:174
FORCEINLINE VOID ObpCalloutStart(IN PKIRQL CalloutIrql)
Definition: ob_x.h:429
smooth NULL
Definition: ftsmooth.c:416
Directory()
Definition: entries.h:140
FORCEINLINE VOID ObpFreeObjectCreateInformation(IN POBJECT_CREATE_INFORMATION ObjectCreateInfo)
Definition: ob_x.h:392
#define FORCEINLINE
Definition: ntbasedef.h:221
Definition: bufpool.h:45
#define OBJ_VALID_KERNEL_ATTRIBUTES
Definition: obtypes.h:92
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define OB_NAME_TAG
Definition: tag.h:151
#define OBP_LOCK_STATE_POST_ACQUISITION_SHARED
Definition: ob_x.h:14
FORCEINLINE ULONG ObpSelectObjectLockSlot(IN POBJECT_HEADER ObjectHeader)
Definition: ob_x.h:40
LIST_ENTRY List
Definition: psmgr.c:57
if(!(yy_init))
Definition: macro.lex.yy.c:714
FORCEINLINE POBJECT_HEADER_NAME_INFO ObpReferenceNameInfo(IN POBJECT_HEADER ObjectHeader)
Definition: ob_x.h:102
FORCEINLINE VOID ObpAcquireDirectoryLockExclusive(IN POBJECT_DIRECTORY Directory, IN POBP_LOOKUP_CONTEXT Context)
Definition: ob_x.h:190
FORCEINLINE VOID ObpEnterObjectTypeMutex(IN POBJECT_TYPE ObjectType)
Definition: ob_x.h:272
FORCEINLINE VOID ObpAcquireObjectLockShared(IN POBJECT_HEADER ObjectHeader)
Definition: ob_x.h:66
#define PSLIST_ENTRY
Definition: rtltypes.h:130
FORCEINLINE USHORT ExQueryDepthSList(_In_ PSLIST_HEADER SListHead)
Definition: exfuncs.h:153
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1817
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
static IUnknown Object
Definition: main.c:512
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
struct _GENERAL_LOOKASIDE * L
Definition: ketypes.h:760
FORCEINLINE VOID ObpLeaveObjectTypeMutex(IN POBJECT_TYPE ObjectType)
Definition: ob_x.h:284
FORCEINLINE VOID ObpCalloutEnd(IN KIRQL CalloutIrql, IN PCHAR Procedure, IN POBJECT_TYPE ObjectType, IN PVOID Object)
Definition: ob_x.h:437
#define InterlockedDecrement
Definition: armddk.h:52
#define KeEnterCriticalRegion()
Definition: ke_x.h:83
KIRQL * PKIRQL
Definition: env_spec_w32.h:592
NTKERNELAPI PSLIST_ENTRY FASTCALL InterlockedPushEntrySList(IN PSLIST_HEADER ListHead, IN PSLIST_ENTRY ListEntry)
Definition: interlocked.c:82
FORCEINLINE ULONG ObpValidateAttributes(IN ULONG Attributes, IN KPROCESSOR_MODE PreviousMode)
Definition: ob_x.h:22
FORCEINLINE VOID ExReleasePushLock(PEX_PUSH_LOCK PushLock)
Definition: ex.h:1282
FORCEINLINE VOID ObpReleaseLookupContext(IN POBP_LOOKUP_CONTEXT Context)
Definition: ob_x.h:255
FORCEINLINE VOID ExAcquirePushLockShared(PEX_PUSH_LOCK PushLock)
Definition: ex.h:1091
#define OBP_LOCK_STATE_PRE_ACQUISITION_SHARED
Definition: ob_x.h:12
_Must_inspect_result_ _In_ USHORT _In_ PHIDP_PREPARSED_DATA _Out_writes_to_ LengthAttributes PHIDP_EXTENDED_ATTRIBUTES Attributes
Definition: hidpi.h:348
FORCEINLINE VOID ObpInitializeLookupContext(IN POBP_LOOKUP_CONTEXT Context)
Definition: ob_x.h:221
#define KeLeaveCriticalRegion()
Definition: ke_x.h:114
FORCEINLINE VOID ObpReleaseLookupContextObject(IN POBP_LOOKUP_CONTEXT Context)
Definition: ob_x.h:232
struct _GENERAL_LOOKASIDE * P
Definition: ketypes.h:759
#define OBP_LOCK_STATE_POST_ACQUISITION_EXCLUSIVE
Definition: ob_x.h:13
BOOLEAN NTAPI ExAcquireResourceSharedLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:885
#define OBJ_VALID_ATTRIBUTES
Definition: winternl.h:233
enum _PP_NPAGED_LOOKASIDE_NUMBER PP_NPAGED_LOOKASIDE_NUMBER
unsigned int ULONG
Definition: retypes.h:1
base for all directory entries
Definition: entries.h:138
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
_In_ BOOLEAN _In_ USHORT Directory
Definition: rtlfuncs.h:3734
signed int * PLONG
Definition: retypes.h:5
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
FORCEINLINE VOID ObpDereferenceNameInfo(IN POBJECT_HEADER_NAME_INFO HeaderNameInfo)
Definition: ob_x.h:143
#define APC_LEVEL
Definition: env_spec_w32.h:695
#define OBP_LOCK_STATE_PRE_ACQUISITION_EXCLUSIVE
Definition: ob_x.h:11