ReactOS 0.4.16-dev-237-gd6eebaa
povolume.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for povolume.c:

Go to the source code of this file.

Classes

struct  _POP_FLUSH_VOLUME
 

Macros

#define NDEBUG
 

Typedefs

typedef struct _POP_FLUSH_VOLUME POP_FLUSH_VOLUME
 
typedef struct _POP_FLUSH_VOLUMEPPOP_FLUSH_VOLUME
 

Functions

PDEVICE_OBJECT_POWER_EXTENSION NTAPI PopGetDope (IN PDEVICE_OBJECT DeviceObject)
 
VOID NTAPI PoVolumeDevice (IN PDEVICE_OBJECT DeviceObject)
 
VOID NTAPI PoRemoveVolumeDevice (IN PDEVICE_OBJECT DeviceObject)
 
VOID NTAPI PopFlushVolumeWorker (IN PVOID Context)
 
VOID NTAPI PopFlushVolumes (IN BOOLEAN ShuttingDown)
 
VOID NTAPI PoInitializeDeviceObject (IN OUT PDEVOBJ_EXTENSION DeviceObjectExtension)
 

Variables

ULONG PopFlushPolicy = 0
 
KGUARDED_MUTEX PopVolumeLock
 
LIST_ENTRY PopVolumeDevices
 
KSPIN_LOCK PopDopeGlobalLock
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file povolume.c.

Typedef Documentation

◆ POP_FLUSH_VOLUME

◆ PPOP_FLUSH_VOLUME

Function Documentation

◆ PoInitializeDeviceObject()

VOID NTAPI PoInitializeDeviceObject ( IN OUT PDEVOBJ_EXTENSION  DeviceObjectExtension)

Definition at line 361 of file povolume.c.

362{
363 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension = (PVOID)DeviceObjectExtension;
364 PAGED_CODE();
365
366 /* Initialize the power flags */
367 DeviceExtension->PowerFlags = PowerSystemUnspecified & 0xF;
368 DeviceExtension->PowerFlags |= ((PowerDeviceUnspecified << 4) & 0xF0);
369
370 /* The device object is not on drugs yet */
371 DeviceExtension->Dope = NULL;
372}
#define PAGED_CODE()
#define NULL
Definition: types.h:112
@ PowerSystemUnspecified
Definition: ntpoapi.h:35
@ PowerDeviceUnspecified
Definition: ntpoapi.h:48
struct _DEVICE_OBJECT_POWER_EXTENSION * Dope
Definition: iotypes.h:1083
void * PVOID
Definition: typedefs.h:50

Referenced by IoCreateDevice().

◆ PopFlushVolumes()

VOID NTAPI PopFlushVolumes ( IN BOOLEAN  ShuttingDown)

Definition at line 227 of file povolume.c.

228{
229 POP_FLUSH_VOLUME FlushContext = {{0}};
230 ULONG FlushPolicy;
231 UNICODE_STRING RegistryName = RTL_CONSTANT_STRING(L"\\Registry");
233 HANDLE RegistryHandle;
234 PLIST_ENTRY NextEntry;
236 ULONG VolumeCount = 0;
238 HANDLE ThreadHandle;
239 ULONG ThreadCount;
240
241 /* Setup the flush context */
242 InitializeListHead(&FlushContext.List);
244
245 /* What to flush */
246 FlushPolicy = ShuttingDown ? 1 | 2 : PopFlushPolicy;
247 if ((FlushPolicy & 1))
248 {
249 /* Registry flush requested, so open it */
250 DPRINT("Opening registry\n");
252 &RegistryName,
254 NULL,
255 NULL);
256 Status = ZwOpenKey(&RegistryHandle, KEY_READ, &ObjectAttributes);
257 if (NT_SUCCESS(Status))
258 {
259 /* Flush the registry */
260 DPRINT("Flushing registry\n");
261 ZwFlushKey(RegistryHandle);
262 ZwClose(RegistryHandle);
263 }
264 }
265
266 /* Serialize with other flushes */
268
269 /* Scan the volume list */
270 NextEntry = PopVolumeDevices.Flink;
271 while (NextEntry != &PopVolumeDevices)
272 {
273 /* Get the dope from the link */
275
276 /* Grab the next entry now, since we'll be modifying the list */
277 NextEntry = NextEntry->Flink;
278
279 /* Make sure the object is mounted, writable, exists, and is not a floppy */
280 if (!(Dope->DeviceObject->Vpb->Flags & VPB_MOUNTED) ||
281 (Dope->DeviceObject->Characteristics & FILE_FLOPPY_DISKETTE) ||
282 (Dope->DeviceObject->Characteristics & FILE_READ_ONLY_DEVICE) ||
283 ((Dope->DeviceObject->Vpb->RealDevice) &&
284 (Dope->DeviceObject->Vpb->RealDevice->Characteristics & FILE_FLOPPY_DISKETTE)))
285 {
286 /* Not flushable */
287 continue;
288 }
289
290 /* Remove it from the dope and add it to the flush context list */
291 RemoveEntryList(&Dope->Volume);
292 InsertTailList(&FlushContext.List, &Dope->Volume);
293
294 /* Next */
295 VolumeCount++;
296 }
297
298 /* Check if we should skip non-removable devices */
299 if (!(FlushPolicy & 2))
300 {
301 /* ReactOS only implements this routine for shutdown, which requires it */
303 }
304
305 /* Check if there were no volumes at all */
306 if (!VolumeCount)
307 {
308 /* Nothing to do */
310 return;
311 }
312
313 /* Allocate up to 8 flusher threads */
314 ThreadCount = min(VolumeCount, 8);
316 NULL,
318 NULL,
319 NULL);
320
321 /* We will ourselves become a flusher thread */
322 FlushContext.Count = 1;
323 ThreadCount--;
324
325 /* Look for any extra ones we might need */
326 while (ThreadCount > 0)
327 {
328 /* Create a new one */
329 ThreadCount--;
330 DPRINT("Creating flush thread\n");
331 Status = PsCreateSystemThread(&ThreadHandle,
334 0L,
335 NULL,
337 &FlushContext);
338 if (NT_SUCCESS(Status))
339 {
340 /* One more created... */
341 FlushContext.Count++;
342 ZwClose(ThreadHandle);
343 }
344 }
345
346 /* Allow flushes to go through */
348
349 /* Enter the flush work */
350 DPRINT("Local flush\n");
351 PopFlushVolumeWorker(&FlushContext);
352
353 /* Wait for all flushes to be over */
354 DPRINT("Waiting for flushes\n");
356 DPRINT("Flushes have completed\n");
357}
LONG NTSTATUS
Definition: precomp.h:26
#define UNIMPLEMENTED
Definition: ntoskrnl.c:15
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
#define InsertTailList(ListHead, Entry)
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
Status
Definition: gdiplustypes.h:25
VOID FASTCALL KeReleaseGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:53
VOID FASTCALL KeAcquireGuardedMutex(IN PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:42
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
UNICODE_STRING Volume
Definition: fltkernel.h:1172
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define min(a, b)
Definition: monoChain.cc:55
#define KernelMode
Definition: asm.h:34
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1339
#define KEY_READ
Definition: nt_native.h:1023
#define FILE_READ_ONLY_DEVICE
Definition: nt_native.h:808
#define FILE_FLOPPY_DISKETTE
Definition: nt_native.h:809
@ NotificationEvent
NTSTATUS NTAPI PsCreateSystemThread(OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE ProcessHandle, IN PCLIENT_ID ClientId, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
Definition: thread.c:602
#define L(x)
Definition: ntvdm.h:50
LIST_ENTRY PopVolumeDevices
Definition: povolume.c:27
ULONG PopFlushPolicy
Definition: povolume.c:24
VOID NTAPI PopFlushVolumeWorker(IN PVOID Context)
Definition: povolume.c:148
KGUARDED_MUTEX PopVolumeLock
Definition: povolume.c:26
#define DPRINT
Definition: sndvol32.h:73
PDEVICE_OBJECT DeviceObject
Definition: po.h:262
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
LIST_ENTRY List
Definition: povolume.c:19
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
#define VPB_MOUNTED
Definition: iotypes.h:1807
@ Executive
Definition: ketypes.h:415

Referenced by NtSetSystemPowerState().

◆ PopFlushVolumeWorker()

VOID NTAPI PopFlushVolumeWorker ( IN PVOID  Context)

Definition at line 148 of file povolume.c.

149{
152 PLIST_ENTRY NextEntry;
154 UCHAR Buffer[sizeof(OBJECT_NAME_INFORMATION) + 512];
160
161 /* Acquire the flush lock since we're messing with the list */
163
164 /* Loop the flush list */
165 while (!IsListEmpty(&FlushContext->List))
166 {
167 /* Grab the next (ie: current) entry and remove it */
168 NextEntry = FlushContext->List.Flink;
169 RemoveEntryList(NextEntry);
170
171 /* Add it back on the volume list */
172 InsertTailList(&PopVolumeDevices, NextEntry);
173
174 /* Done touching the volume list */
176
177 /* Get the dope from the volume link */
179
180 /* Get the name */
182 NameInfo,
183 sizeof(Buffer),
184 &Length);
185 if ((NT_SUCCESS(Status)) && (NameInfo->Name.Buffer))
186 {
187 /* Open the volume */
188 DPRINT("Opening: %wZ\n", &NameInfo->Name);
190 &NameInfo->Name,
192 0,
193 0);
194 Status = ZwCreateFile(&VolumeHandle,
198 NULL,
201 FILE_OPEN,
202 0,
203 NULL,
204 0);
205 if (NT_SUCCESS(Status))
206 {
207 /* Flush it and close it */
208 DPRINT("Sending flush to: %p\n", VolumeHandle);
211 }
212 }
213
214 /* Acquire the flush lock again since we'll touch the list */
216 }
217
218 /* One more flush completed... if it was the last, signal the caller */
219 if (!--FlushContext->Count) KeSetEvent(&FlushContext->Wait, IO_NO_INCREMENT, FALSE);
220
221 /* Serialize with flushers */
223}
Definition: bufpool.h:45
#define GENERIC_READ
Definition: compat.h:135
#define FILE_SHARE_READ
Definition: compat.h:136
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
_Must_inspect_result_ _Out_ PHANDLE VolumeHandle
Definition: fltkernel.h:2283
#define FILE_OPEN
Definition: from_kernel.h:54
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define SYNCHRONIZE
Definition: nt_native.h:61
#define FILE_WRITE_DATA
Definition: nt_native.h:631
#define FILE_READ_DATA
Definition: nt_native.h:628
struct _OBJECT_NAME_INFORMATION OBJECT_NAME_INFORMATION
#define GENERIC_WRITE
Definition: nt_native.h:90
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
NTSTATUS NTAPI ObQueryNameString(IN PVOID Object, OUT POBJECT_NAME_INFORMATION ObjectNameInfo, IN ULONG Length, OUT PULONG ReturnLength)
Definition: obname.c:1207
struct _POP_FLUSH_VOLUME * PPOP_FLUSH_VOLUME
#define IO_NO_INCREMENT
Definition: iotypes.h:598
unsigned char UCHAR
Definition: xmlstorage.h:181
NTSYSAPI NTSTATUS NTAPI ZwFlushBuffersFile(_In_ HANDLE FileHandle, _Out_ PIO_STATUS_BLOCK IoStatusBlock)

Referenced by PopFlushVolumes().

◆ PopGetDope()

PDEVICE_OBJECT_POWER_EXTENSION NTAPI PopGetDope ( IN PDEVICE_OBJECT  DeviceObject)

Definition at line 34 of file povolume.c.

35{
36 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension;
39 PAGED_CODE();
40
41 /* If the device already has the dope, return it */
42 DeviceExtension = IoGetDevObjExtension(DeviceObject);
43 if (DeviceExtension->Dope) goto Return;
44
45 /* Allocate some dope for the device */
49 if (!Dope) goto Return;
50
51 /* Initialize the initial contents of the dope */
56
57 /* Make sure only one caller can assign dope to a device */
59
60 /* Make sure the device still has no dope */
61 if (!DeviceExtension->Dope)
62 {
63 /* Give the local dope to this device, and remember we won the race */
64 DeviceExtension->Dope = (PVOID)Dope;
65 Dope = NULL;
66 }
67
68 /* Allow other dope transactions now */
70
71 /* Check if someone other than us already assigned the dope, so free ours */
72 if (Dope) ExFreePoolWithTag(Dope, TAG_PO_DOPE);
73
74 /* Return the dope to the caller */
75Return:
76 return (PDEVICE_OBJECT_POWER_EXTENSION)DeviceExtension->Dope;
77}
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
#define NonPagedPool
Definition: env_spec_w32.h:307
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
#define IoGetDevObjExtension(DeviceObject)
Definition: io.h:129
KSPIN_LOCK PopDopeGlobalLock
Definition: povolume.c:28
DEVICE_POWER_STATE State
Definition: po.h:264
#define TAG_PO_DOPE
Definition: tag.h:129
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778

Referenced by PoVolumeDevice().

◆ PoRemoveVolumeDevice()

VOID NTAPI PoRemoveVolumeDevice ( IN PDEVICE_OBJECT  DeviceObject)

Definition at line 103 of file povolume.c.

104{
106 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension;
108 PAGED_CODE();
109
110 /* If the device already has the dope, return it */
111 DeviceExtension = IoGetDevObjExtension(DeviceObject);
112 if (!DeviceExtension->Dope)
113 {
114 /* no dope */
115 return;
116 }
117
118 /* Make sure we can flush safely */
120
121 /* Get dope from device */
122 Dope = (PDEVICE_OBJECT_POWER_EXTENSION)DeviceExtension->Dope;
123
124 if (Dope->Volume.Flink)
125 {
126 /* Remove from volume from list */
127 RemoveEntryList(&Dope->Volume);
128 }
129
130 /* Allow flushes to go through */
132
133 /* Now remove dope from device object */
135
136 /* remove from dev obj */
137 DeviceExtension->Dope = NULL;
138
139 /* Release lock */
141
142 /* Free dope */
144}
if(dx< 0)
Definition: linetemp.h:194
struct _DEVICE_OBJECT_POWER_EXTENSION * PDEVICE_OBJECT_POWER_EXTENSION

Referenced by IoDeleteDevice().

◆ PoVolumeDevice()

VOID NTAPI PoVolumeDevice ( IN PDEVICE_OBJECT  DeviceObject)

Definition at line 81 of file povolume.c.

82{
84 PAGED_CODE();
85
86 /* Get dope from the device (if the device has no dope, it will receive some) */
88 if (Dope)
89 {
90 /* Make sure we can flush safely */
92
93 /* Add this volume into the list of power-manager volumes */
95
96 /* Allow flushes to go through */
98 }
99}
PDEVICE_OBJECT_POWER_EXTENSION NTAPI PopGetDope(IN PDEVICE_OBJECT DeviceObject)
Definition: povolume.c:34

Referenced by IoCreateDevice(), and IoVerifyVolume().

Variable Documentation

◆ PopDopeGlobalLock

KSPIN_LOCK PopDopeGlobalLock

Definition at line 28 of file povolume.c.

Referenced by PoInitSystem(), PopGetDope(), and PoRemoveVolumeDevice().

◆ PopFlushPolicy

ULONG PopFlushPolicy = 0

Definition at line 24 of file povolume.c.

Referenced by PopFlushVolumes().

◆ PopVolumeDevices

LIST_ENTRY PopVolumeDevices

Definition at line 27 of file povolume.c.

Referenced by PoInitSystem(), PopFlushVolumes(), PopFlushVolumeWorker(), and PoVolumeDevice().

◆ PopVolumeLock