Synchronize up to trunk's revision r57756.
[reactos.git] / drivers / filters / mountmgr / notify.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2011 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/mountmgr/notify.c
22 * PURPOSE: Mount Manager - Notifications handlers
23 * PROGRAMMER: Pierre Schweitzer (pierre.schweitzer@reactos.org)
24 * Alex Ionescu (alex.ionescu@reactos.org)
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "mntmgr.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 /*
35 * @implemented
36 */
37 VOID
38 SendOnlineNotification(IN PUNICODE_STRING SymbolicName)
39 {
40 PIRP Irp;
41 KEVENT Event;
42 NTSTATUS Status;
43 PFILE_OBJECT FileObject;
44 PIO_STACK_LOCATION Stack;
45 PDEVICE_OBJECT DeviceObject;
46 IO_STATUS_BLOCK IoStatusBlock;
47
48 /* Get device object */
49 Status = IoGetDeviceObjectPointer(SymbolicName,
50 FILE_READ_ATTRIBUTES,
51 &FileObject,
52 &DeviceObject);
53 if (!NT_SUCCESS(Status))
54 {
55 return;
56 }
57
58 /* And attached device object */
59 DeviceObject = IoGetAttachedDeviceReference(FileObject->DeviceObject);
60
61 /* And send VOLUME_ONLINE */
62 KeInitializeEvent(&Event, NotificationEvent, FALSE);
63 Irp = IoBuildDeviceIoControlRequest(IOCTL_VOLUME_ONLINE,
64 DeviceObject,
65 NULL, 0,
66 NULL, 0,
67 FALSE,
68 &Event,
69 &IoStatusBlock);
70 if (!Irp)
71 {
72 goto Cleanup;
73 }
74
75 Stack = IoGetNextIrpStackLocation(Irp);
76 Stack->FileObject = FileObject;
77
78 Status = IoCallDriver(DeviceObject, Irp);
79 if (Status == STATUS_PENDING)
80 {
81 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
82 }
83
84 Cleanup:
85 ObDereferenceObject(DeviceObject);
86 ObDereferenceObject(FileObject);
87
88 return;
89 }
90
91 /*
92 * @implemented
93 */
94 VOID
95 NTAPI
96 SendOnlineNotificationWorker(IN PVOID Parameter)
97 {
98 KIRQL OldIrql;
99 PLIST_ENTRY Head;
100 PDEVICE_EXTENSION DeviceExtension;
101 PONLINE_NOTIFICATION_WORK_ITEM WorkItem;
102 PONLINE_NOTIFICATION_WORK_ITEM NewWorkItem;
103
104 WorkItem = (PONLINE_NOTIFICATION_WORK_ITEM)Parameter;
105 DeviceExtension = WorkItem->DeviceExtension;
106
107 /* First, send the notification */
108 SendOnlineNotification(&(WorkItem->SymbolicName));
109
110 KeAcquireSpinLock(&(DeviceExtension->WorkerLock), &OldIrql);
111 /* If there are no notifications running any longer, reset event */
112 if (--DeviceExtension->OnlineNotificationCount == 0)
113 {
114 KeSetEvent(&(DeviceExtension->OnlineNotificationEvent), 0, FALSE);
115 }
116
117 /* If there are still notifications in queue */
118 if (!IsListEmpty(&(DeviceExtension->OnlineNotificationListHead)))
119 {
120 /* Queue a new one for execution */
121 Head = RemoveHeadList(&(DeviceExtension->OnlineNotificationListHead));
122 NewWorkItem = CONTAINING_RECORD(Head, ONLINE_NOTIFICATION_WORK_ITEM, List);
123 KeReleaseSpinLock(&(DeviceExtension->WorkerLock), OldIrql);
124 NewWorkItem->List.Blink = NULL;
125 NewWorkItem->List.Flink = NULL;
126 ExQueueWorkItem((PWORK_QUEUE_ITEM)NewWorkItem, DelayedWorkQueue);
127 }
128 else
129 {
130 /* Mark it's over */
131 DeviceExtension->OnlineNotificationWorkerActive = 0;
132 KeReleaseSpinLock(&(DeviceExtension->WorkerLock), OldIrql);
133 }
134
135 FreePool(WorkItem->SymbolicName.Buffer);
136 FreePool(WorkItem);
137
138 return;
139 }
140
141 /*
142 * @implemented
143 */
144 VOID
145 PostOnlineNotification(IN PDEVICE_EXTENSION DeviceExtension,
146 IN PUNICODE_STRING SymbolicName)
147 {
148 KIRQL OldIrql;
149 PONLINE_NOTIFICATION_WORK_ITEM WorkItem;
150
151 /* Allocate a notification work item */
152 WorkItem = AllocatePool(sizeof(ONLINE_NOTIFICATION_WORK_ITEM));
153 if (!WorkItem)
154 {
155 return;
156 }
157
158 WorkItem->List.Flink = NULL;
159 WorkItem->DeviceExtension = DeviceExtension;
160 WorkItem->WorkerRoutine = SendOnlineNotificationWorker;
161 WorkItem->Parameter = WorkItem;
162 WorkItem->SymbolicName.Length = SymbolicName->Length;
163 WorkItem->SymbolicName.MaximumLength = SymbolicName->Length + sizeof(WCHAR);
164 WorkItem->SymbolicName.Buffer = AllocatePool(WorkItem->SymbolicName.MaximumLength);
165 if (!WorkItem->SymbolicName.Buffer)
166 {
167 FreePool(WorkItem);
168 return;
169 }
170
171 RtlCopyMemory(WorkItem->SymbolicName.Buffer, SymbolicName->Buffer, SymbolicName->Length);
172 WorkItem->SymbolicName.Buffer[SymbolicName->Length / sizeof(WCHAR)] = UNICODE_NULL;
173
174 KeAcquireSpinLock(&(DeviceExtension->WorkerLock), &OldIrql);
175 DeviceExtension->OnlineNotificationCount++;
176
177 /* If no worker are active */
178 if (DeviceExtension->OnlineNotificationWorkerActive == 0)
179 {
180 /* Queue that one for execution */
181 DeviceExtension->OnlineNotificationWorkerActive = 1;
182 ExQueueWorkItem((PWORK_QUEUE_ITEM)WorkItem, DelayedWorkQueue);
183 }
184 else
185 {
186 /* Otherwise, just put it in the queue list */
187 InsertTailList(&(DeviceExtension->OnlineNotificationListHead), &(WorkItem->List));
188 }
189
190 KeReleaseSpinLock(&(DeviceExtension->WorkerLock), OldIrql);
191
192 return;
193 }
194
195 /*
196 * @implemented
197 */
198 VOID
199 WaitForOnlinesToComplete(IN PDEVICE_EXTENSION DeviceExtension)
200 {
201 KIRQL OldIrql;
202
203 KeInitializeEvent(&(DeviceExtension->OnlineNotificationEvent), NotificationEvent, FALSE);
204
205 KeAcquireSpinLock(&(DeviceExtension->WorkerLock), &OldIrql);
206
207 /* Just wait all the worker are done */
208 if (DeviceExtension->OnlineNotificationCount != 1)
209 {
210 DeviceExtension->OnlineNotificationCount--;
211 KeReleaseSpinLock(&(DeviceExtension->WorkerLock), OldIrql);
212
213 KeWaitForSingleObject(&(DeviceExtension->OnlineNotificationEvent),
214 Executive,
215 KernelMode,
216 FALSE,
217 NULL);
218
219 KeAcquireSpinLock(&(DeviceExtension->WorkerLock), &OldIrql);
220 DeviceExtension->OnlineNotificationCount++;
221 }
222
223 KeReleaseSpinLock(&(DeviceExtension->WorkerLock), OldIrql);
224 }
225
226 /*
227 * @implemented
228 */
229 NTSTATUS
230 NTAPI
231 MountMgrTargetDeviceNotification(IN PVOID NotificationStructure,
232 IN PVOID Context)
233 {
234 PDEVICE_EXTENSION DeviceExtension;
235 PDEVICE_INFORMATION DeviceInformation;
236 PDEVICE_INTERFACE_CHANGE_NOTIFICATION Notification;
237
238 DeviceInformation = Context;
239 DeviceExtension = DeviceInformation->DeviceExtension;
240 Notification = NotificationStructure;
241
242 /* If it's to signal that removal is complete, then, execute the function */
243 if (IsEqualGUID(&(Notification->Event), &GUID_TARGET_DEVICE_REMOVE_COMPLETE))
244 {
245 MountMgrMountedDeviceRemoval(DeviceExtension, Notification->SymbolicLinkName);
246 }
247 /* It it's to signal that a volume has been mounted
248 * Verify if a database sync is required and execute it
249 */
250 else if (IsEqualGUID(&(Notification->Event), &GUID_IO_VOLUME_MOUNT))
251 {
252 if (InterlockedCompareExchange(&(DeviceInformation->MountState),
253 FALSE,
254 TRUE) == TRUE)
255 {
256 InterlockedDecrement(&(DeviceInformation->MountState));
257 }
258 else
259 {
260 if (DeviceInformation->NeedsReconcile)
261 {
262 DeviceInformation->NeedsReconcile = FALSE;
263 ReconcileThisDatabaseWithMaster(DeviceExtension, DeviceInformation);
264 }
265 }
266 }
267
268 return STATUS_SUCCESS;
269 }
270
271 /*
272 * @implemented
273 */
274 VOID
275 RegisterForTargetDeviceNotification(IN PDEVICE_EXTENSION DeviceExtension,
276 IN PDEVICE_INFORMATION DeviceInformation)
277 {
278 NTSTATUS Status;
279 PFILE_OBJECT FileObject;
280 PDEVICE_OBJECT DeviceObject;
281
282 /* Get device object */
283 Status = IoGetDeviceObjectPointer(&(DeviceInformation->DeviceName),
284 FILE_READ_ATTRIBUTES,
285 &FileObject,
286 &DeviceObject);
287 if (!NT_SUCCESS(Status))
288 {
289 return;
290 }
291
292 /* And simply register for notifications */
293 Status = IoRegisterPlugPlayNotification(EventCategoryTargetDeviceChange,
294 0, FileObject,
295 DeviceExtension->DriverObject,
296 MountMgrTargetDeviceNotification,
297 DeviceInformation,
298 &(DeviceInformation->TargetDeviceNotificationEntry));
299 if (!NT_SUCCESS(Status))
300 {
301 DeviceInformation->TargetDeviceNotificationEntry = NULL;
302 }
303
304 ObDereferenceObject(FileObject);
305
306 return;
307 }
308
309 /*
310 * @implemented
311 */
312 VOID
313 MountMgrNotify(IN PDEVICE_EXTENSION DeviceExtension)
314 {
315 PIRP Irp;
316 KIRQL OldIrql;
317 LIST_ENTRY CopyList;
318 PLIST_ENTRY NextEntry;
319
320 /* Increase the epic number */
321 DeviceExtension->EpicNumber++;
322
323 InitializeListHead(&CopyList);
324
325 /* Copy all the pending IRPs for notification */
326 IoAcquireCancelSpinLock(&OldIrql);
327 while (!IsListEmpty(&(DeviceExtension->IrpListHead)))
328 {
329 NextEntry = RemoveHeadList(&(DeviceExtension->IrpListHead));
330 Irp = CONTAINING_RECORD(NextEntry, IRP, Tail.Overlay.ListEntry);
331 InsertTailList(&CopyList, &(Irp->Tail.Overlay.ListEntry));
332 }
333 IoReleaseCancelSpinLock(OldIrql);
334
335 /* Then, notifiy them one by one */
336 while (!IsListEmpty(&CopyList))
337 {
338 NextEntry = RemoveHeadList(&CopyList);
339 Irp = CONTAINING_RECORD(NextEntry, IRP, Tail.Overlay.ListEntry);
340
341 *((PULONG)Irp->AssociatedIrp.SystemBuffer) = DeviceExtension->EpicNumber;
342 Irp->IoStatus.Information = sizeof(DeviceExtension->EpicNumber);
343
344 IoCompleteRequest(Irp, IO_NO_INCREMENT);
345 }
346 }
347
348 /*
349 * @implemented
350 */
351 VOID
352 MountMgrNotifyNameChange(IN PDEVICE_EXTENSION DeviceExtension,
353 IN PUNICODE_STRING DeviceName,
354 IN BOOLEAN ValidateVolume)
355 {
356 PIRP Irp;
357 KEVENT Event;
358 NTSTATUS Status;
359 PLIST_ENTRY NextEntry;
360 PFILE_OBJECT FileObject;
361 PIO_STACK_LOCATION Stack;
362 PDEVICE_OBJECT DeviceObject;
363 IO_STATUS_BLOCK IoStatusBlock;
364 PDEVICE_RELATIONS DeviceRelations;
365 PDEVICE_INFORMATION DeviceInformation;
366 TARGET_DEVICE_CUSTOM_NOTIFICATION DeviceNotification;
367
368 /* If we have to validate volume */
369 if (ValidateVolume)
370 {
371 /* Then, ensure we can find the device */
372 NextEntry = DeviceExtension->DeviceListHead.Flink;
373 while (NextEntry != &(DeviceExtension->DeviceListHead))
374 {
375 DeviceInformation = CONTAINING_RECORD(NextEntry, DEVICE_INFORMATION, DeviceListEntry);
376 if (RtlCompareUnicodeString(DeviceName, &(DeviceInformation->DeviceName), TRUE) == 0)
377 {
378 break;
379 }
380 }
381
382 if (NextEntry == &(DeviceExtension->DeviceListHead) ||
383 !DeviceInformation->Volume)
384 {
385 return;
386 }
387 }
388
389 /* Then, get device object */
390 Status = IoGetDeviceObjectPointer(DeviceName,
391 FILE_READ_ATTRIBUTES,
392 &FileObject,
393 &DeviceObject);
394 if (!NT_SUCCESS(Status))
395 {
396 return;
397 }
398
399 DeviceObject = IoGetAttachedDeviceReference(FileObject->DeviceObject);
400
401 KeInitializeEvent(&Event, NotificationEvent, FALSE);
402
403 /* Set up empty IRP (yes, yes!) */
404 Irp = IoBuildDeviceIoControlRequest(0,
405 DeviceObject,
406 NULL,
407 0,
408 NULL,
409 0,
410 FALSE,
411 &Event,
412 &IoStatusBlock);
413 if (!Irp)
414 {
415 ObDereferenceObject(DeviceObject);
416 ObDereferenceObject(FileObject);
417 }
418
419 Stack = IoGetNextIrpStackLocation(Irp);
420
421 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
422 Irp->IoStatus.Information = 0;
423
424 /* Properly set it, we want to query device relations */
425 Stack->MajorFunction = IRP_MJ_PNP;
426 Stack->MinorFunction = IRP_MN_QUERY_DEVICE_RELATIONS;
427 Stack->Parameters.QueryDeviceRelations.Type = TargetDeviceRelation;
428 Stack->FileObject = FileObject;
429
430 /* And call driver */
431 Status = IoCallDriver(DeviceObject, Irp);
432 if (Status == STATUS_PENDING)
433 {
434 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
435 Status = IoStatusBlock.Status;
436 }
437
438 ObDereferenceObject(DeviceObject);
439 ObDereferenceObject(FileObject);
440
441 if (!NT_SUCCESS(Status))
442 {
443 return;
444 }
445
446 /* Validate device return */
447 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
448 if (DeviceRelations->Count < 1)
449 {
450 ExFreePool(DeviceRelations);
451 return;
452 }
453
454 DeviceObject = DeviceRelations->Objects[0];
455 ExFreePool(DeviceRelations);
456
457 /* Set up real notification */
458 DeviceNotification.Version = 1;
459 DeviceNotification.Size = sizeof(TARGET_DEVICE_CUSTOM_NOTIFICATION);
460 DeviceNotification.Event = GUID_IO_VOLUME_NAME_CHANGE;
461 DeviceNotification.FileObject = NULL;
462 DeviceNotification.NameBufferOffset = -1;
463
464 /* And report */
465 IoReportTargetDeviceChangeAsynchronous(DeviceObject,
466 &DeviceNotification,
467 NULL, NULL);
468
469 ObDereferenceObject(DeviceObject);
470
471 return;
472 }
473
474 /*
475 * @implemented
476 */
477 VOID
478 RemoveWorkItem(IN PUNIQUE_ID_WORK_ITEM WorkItem)
479 {
480 PDEVICE_EXTENSION DeviceExtension = WorkItem->DeviceExtension;
481
482 KeWaitForSingleObject(&(DeviceExtension->DeviceLock), Executive, KernelMode, FALSE, NULL);
483
484 /* If even if being worked, it's too late */
485 if (WorkItem->Event)
486 {
487 KeReleaseSemaphore(&(DeviceExtension->DeviceLock), IO_NO_INCREMENT, 1, FALSE);
488 KeSetEvent(WorkItem->Event, 0, FALSE);
489 }
490 else
491 {
492 /* Otherwise, remove it from the list, and delete it */
493 RemoveEntryList(&(WorkItem->UniqueIdWorkerItemListEntry));
494 KeReleaseSemaphore(&(DeviceExtension->DeviceLock), IO_NO_INCREMENT, 1, FALSE);
495 IoFreeIrp(WorkItem->Irp);
496 FreePool(WorkItem->DeviceName.Buffer);
497 FreePool(WorkItem->IrpBuffer);
498 FreePool(WorkItem);
499 }
500 }
501
502 /*
503 * @implemented
504 */
505 VOID
506 NTAPI
507 UniqueIdChangeNotifyWorker(IN PDEVICE_OBJECT DeviceObject,
508 IN PVOID Context)
509 {
510 PUNIQUE_ID_WORK_ITEM WorkItem = Context;
511 PMOUNTDEV_UNIQUE_ID OldUniqueId, NewUniqueId;
512 PMOUNTDEV_UNIQUE_ID_CHANGE_NOTIFY_OUTPUT UniqueIdChange;
513
514 /* Validate worker */
515 if (!NT_SUCCESS(WorkItem->Irp->IoStatus.Status))
516 {
517 RemoveWorkItem(WorkItem);
518 return;
519 }
520
521 UniqueIdChange = WorkItem->Irp->AssociatedIrp.SystemBuffer;
522 /* Get the old unique ID */
523 OldUniqueId = AllocatePool(UniqueIdChange->OldUniqueIdLength + sizeof(MOUNTDEV_UNIQUE_ID));
524 if (!OldUniqueId)
525 {
526 RemoveWorkItem(WorkItem);
527 return;
528 }
529
530 OldUniqueId->UniqueIdLength = UniqueIdChange->OldUniqueIdLength;
531 RtlCopyMemory(OldUniqueId->UniqueId,
532 (PVOID)((ULONG_PTR)UniqueIdChange + UniqueIdChange->OldUniqueIdOffset),
533 UniqueIdChange->OldUniqueIdLength);
534
535 /* Get the new unique ID */
536 NewUniqueId = AllocatePool(UniqueIdChange->NewUniqueIdLength + sizeof(MOUNTDEV_UNIQUE_ID));
537 if (!NewUniqueId)
538 {
539 FreePool(OldUniqueId);
540 RemoveWorkItem(WorkItem);
541 return;
542 }
543
544 NewUniqueId->UniqueIdLength = UniqueIdChange->NewUniqueIdLength;
545 RtlCopyMemory(NewUniqueId->UniqueId,
546 (PVOID)((ULONG_PTR)UniqueIdChange + UniqueIdChange->NewUniqueIdOffset),
547 UniqueIdChange->NewUniqueIdLength);
548
549 /* Call the real worker */
550 MountMgrUniqueIdChangeRoutine(WorkItem->DeviceExtension, OldUniqueId, NewUniqueId);
551 IssueUniqueIdChangeNotifyWorker(WorkItem, NewUniqueId);
552
553 FreePool(NewUniqueId);
554 FreePool(OldUniqueId);
555
556 return;
557 }
558
559 /*
560 * @implemented
561 */
562 NTSTATUS
563 NTAPI
564 UniqueIdChangeNotifyCompletion(IN PDEVICE_OBJECT DeviceObject,
565 IN PIRP Irp,
566 IN PVOID Context)
567 {
568 PUNIQUE_ID_WORK_ITEM WorkItem = Context;
569
570 /* Simply queue the work item */
571 IoQueueWorkItem(WorkItem->WorkItem,
572 UniqueIdChangeNotifyWorker,
573 DelayedWorkQueue,
574 WorkItem);
575
576 return STATUS_MORE_PROCESSING_REQUIRED;
577 }
578
579 /*
580 * @implemented
581 */
582 VOID
583 IssueUniqueIdChangeNotifyWorker(IN PUNIQUE_ID_WORK_ITEM WorkItem,
584 IN PMOUNTDEV_UNIQUE_ID UniqueId)
585 {
586 PIRP Irp;
587 NTSTATUS Status;
588 PFILE_OBJECT FileObject;
589 PIO_STACK_LOCATION Stack;
590 PDEVICE_OBJECT DeviceObject;
591
592 /* Get the device object */
593 Status = IoGetDeviceObjectPointer(&(WorkItem->DeviceName),
594 FILE_READ_ATTRIBUTES,
595 &FileObject,
596 &DeviceObject);
597 if (!NT_SUCCESS(Status))
598 {
599 RemoveWorkItem(WorkItem);
600 return;
601 }
602
603 /* And then, the attached device */
604 DeviceObject = IoGetAttachedDeviceReference(FileObject->DeviceObject);
605
606 /* Initialize the IRP */
607 Irp = WorkItem->Irp;
608 IoInitializeIrp(Irp, IoSizeOfIrp(WorkItem->StackSize), (CCHAR)WorkItem->StackSize);
609
610 if (InterlockedExchange((PLONG)&(WorkItem->Event), 0) != 0)
611 {
612 ObDereferenceObject(FileObject);
613 ObDereferenceObject(DeviceObject);
614 RemoveWorkItem(WorkItem);
615 return;
616 }
617
618 Irp->AssociatedIrp.SystemBuffer = WorkItem->IrpBuffer;
619 Irp->Tail.Overlay.Thread = PsGetCurrentThread();
620 RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, UniqueId, UniqueId->UniqueIdLength + sizeof(USHORT));
621
622 Stack = IoGetNextIrpStackLocation(Irp);
623
624 Stack->Parameters.DeviceIoControl.InputBufferLength = UniqueId->UniqueIdLength + sizeof(USHORT);
625 Stack->Parameters.DeviceIoControl.OutputBufferLength = WorkItem->IrpBufferLength;
626 Stack->Parameters.DeviceIoControl.Type3InputBuffer = 0;
627 Stack->Parameters.DeviceIoControl.IoControlCode = IOCTL_MOUNTDEV_UNIQUE_ID_CHANGE_NOTIFY;
628 Stack->MajorFunction = IRP_MJ_DEVICE_CONTROL;
629
630 Status = IoSetCompletionRoutineEx(WorkItem->DeviceExtension->DeviceObject,
631 Irp,
632 UniqueIdChangeNotifyCompletion,
633 WorkItem,
634 TRUE, TRUE, TRUE);
635 if (!NT_SUCCESS(Status))
636 {
637 ObDereferenceObject(FileObject);
638 ObDereferenceObject(DeviceObject);
639 RemoveWorkItem(WorkItem);
640 return;
641 }
642
643 /* Call the driver */
644 IoCallDriver(DeviceObject, Irp);
645 ObDereferenceObject(FileObject);
646 ObDereferenceObject(DeviceObject);
647 }
648
649 /*
650 * @implemented
651 */
652 VOID
653 IssueUniqueIdChangeNotify(IN PDEVICE_EXTENSION DeviceExtension,
654 IN PUNICODE_STRING DeviceName,
655 IN PMOUNTDEV_UNIQUE_ID UniqueId)
656 {
657 NTSTATUS Status;
658 PVOID IrpBuffer = NULL;
659 PFILE_OBJECT FileObject;
660 PDEVICE_OBJECT DeviceObject;
661 PUNIQUE_ID_WORK_ITEM WorkItem = NULL;
662
663 /* Get the associated device object */
664 Status = IoGetDeviceObjectPointer(DeviceName,
665 FILE_READ_ATTRIBUTES,
666 &FileObject,
667 &DeviceObject);
668 if (!NT_SUCCESS(Status))
669 {
670 return;
671 }
672
673 /* And then, get attached device */
674 DeviceObject = IoGetAttachedDeviceReference(FileObject->DeviceObject);
675
676 ObDereferenceObject(FileObject);
677
678 /* Allocate a work item */
679 WorkItem = AllocatePool(sizeof(UNIQUE_ID_WORK_ITEM));
680 if (!WorkItem)
681 {
682 ObDereferenceObject(DeviceObject);
683 return;
684 }
685
686 WorkItem->Event = NULL;
687 WorkItem->WorkItem = IoAllocateWorkItem(DeviceExtension->DeviceObject);
688 if (!WorkItem->WorkItem)
689 {
690 ObDereferenceObject(DeviceObject);
691 goto Cleanup;
692 }
693
694 WorkItem->DeviceExtension = DeviceExtension;
695 WorkItem->StackSize = DeviceObject->StackSize;
696 /* Already provide the IRP */
697 WorkItem->Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
698
699 ObDereferenceObject(DeviceObject);
700
701 if (!WorkItem->Irp)
702 {
703 goto Cleanup;
704 }
705
706 /* Ensure it has enough space */
707 IrpBuffer = AllocatePool(sizeof(MOUNTDEV_UNIQUE_ID_CHANGE_NOTIFY_OUTPUT) + 1024);
708 if (!IrpBuffer)
709 {
710 goto Cleanup;
711 }
712
713 WorkItem->DeviceName.Length = DeviceName->Length;
714 WorkItem->DeviceName.MaximumLength = DeviceName->Length + sizeof(WCHAR);
715 WorkItem->DeviceName.Buffer = AllocatePool(WorkItem->DeviceName.MaximumLength);
716 if (!WorkItem->DeviceName.Buffer)
717 {
718 goto Cleanup;
719 }
720
721 RtlCopyMemory(WorkItem->DeviceName.Buffer, DeviceName->Buffer, DeviceName->Length);
722 WorkItem->DeviceName.Buffer[DeviceName->Length / sizeof(WCHAR)] = UNICODE_NULL;
723
724 WorkItem->IrpBuffer = IrpBuffer;
725 WorkItem->IrpBufferLength = sizeof(MOUNTDEV_UNIQUE_ID_CHANGE_NOTIFY_OUTPUT) + 1024;
726
727 /* Add the worker in the list */
728 KeWaitForSingleObject(&(DeviceExtension->DeviceLock), Executive, KernelMode, FALSE, NULL);
729 InsertHeadList(&(DeviceExtension->UniqueIdWorkerItemListHead), &(WorkItem->UniqueIdWorkerItemListEntry));
730 KeReleaseSemaphore(&(DeviceExtension->DeviceLock), IO_NO_INCREMENT, 1, FALSE);
731
732 /* And call the worker */
733 IssueUniqueIdChangeNotifyWorker(WorkItem, UniqueId);
734
735 return;
736
737 Cleanup:
738 if (IrpBuffer)
739 {
740 FreePool(IrpBuffer);
741 }
742
743 if (WorkItem->Irp)
744 {
745 IoFreeIrp(WorkItem->Irp);
746 }
747
748 if (WorkItem->WorkItem)
749 {
750 IoFreeWorkItem(WorkItem->WorkItem);
751 }
752
753 if (WorkItem)
754 {
755 FreePool(WorkItem);
756 }
757 }