3 * Copyright (C) 2011-2012 ReactOS Team
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.
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.
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.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/mountmgr/device.c
22 * PURPOSE: Mount Manager - Device Control
23 * PROGRAMMER: Pierre Schweitzer (pierre.schweitzer@reactos.org)
28 #define MAX_DEVICES 0x3E8 /* Matches 1000 devices */
37 MountMgrChangeNotify(IN PDEVICE_EXTENSION DeviceExtension
,
42 PIO_STACK_LOCATION Stack
;
43 PMOUNTMGR_CHANGE_NOTIFY_INFO ChangeNotify
;
45 /* Get the I/O buffer */
46 Stack
= IoGetCurrentIrpStackLocation(Irp
);
47 ChangeNotify
= (PMOUNTMGR_CHANGE_NOTIFY_INFO
)Irp
->AssociatedIrp
.SystemBuffer
;
50 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(MOUNTMGR_CHANGE_NOTIFY_INFO
) ||
51 Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_CHANGE_NOTIFY_INFO
))
53 return STATUS_INVALID_PARAMETER
;
56 /* If epic number doesn't match, just return now one */
57 if (DeviceExtension
->EpicNumber
!= ChangeNotify
->EpicNumber
)
59 ChangeNotify
->EpicNumber
= DeviceExtension
->EpicNumber
;
60 Irp
->IoStatus
.Information
= 0;
61 return STATUS_SUCCESS
;
64 /* If IRP is to be canceled, forget about that */
65 IoAcquireCancelSpinLock(&OldIrql
);
68 Status
= STATUS_CANCELLED
;
70 /* Otherwise queue the IRP to be notified with the next epic number change */
73 InsertTailList(&(DeviceExtension
->IrpListHead
), &(Irp
->Tail
.Overlay
.ListEntry
));
74 IoMarkIrpPending(Irp
);
75 IoSetCancelRoutine(Irp
, MountMgrCancel
);
76 Status
= STATUS_PENDING
;
78 IoReleaseCancelSpinLock(OldIrql
);
87 MountmgrWriteNoAutoMount(IN PDEVICE_EXTENSION DeviceExtension
)
89 ULONG Value
= DeviceExtension
->NoAutoMount
;
91 return RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE
,
92 DeviceExtension
->RegistryPath
.Buffer
,
104 MountMgrSetAutoMount(IN PDEVICE_EXTENSION DeviceExtension
,
107 PIO_STACK_LOCATION Stack
;
108 PMOUNTMGR_SET_AUTO_MOUNT SetState
;
110 Stack
= IoGetCurrentIrpStackLocation(Irp
);
112 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_SET_AUTO_MOUNT
))
114 Irp
->IoStatus
.Information
= 0;
115 return STATUS_INVALID_PARAMETER
;
118 /* Only change if there's a real difference */
119 SetState
= (PMOUNTMGR_SET_AUTO_MOUNT
)Irp
->AssociatedIrp
.SystemBuffer
;
120 if (SetState
->NewState
== !DeviceExtension
->NoAutoMount
)
122 Irp
->IoStatus
.Information
= 0;
123 return STATUS_SUCCESS
;
126 /* Set new state; ! on purpose */
127 DeviceExtension
->NoAutoMount
= !SetState
->NewState
;
128 Irp
->IoStatus
.Information
= 0;
129 return MountmgrWriteNoAutoMount(DeviceExtension
);
136 MountMgrQueryAutoMount(IN PDEVICE_EXTENSION DeviceExtension
,
139 PIO_STACK_LOCATION Stack
;
140 PMOUNTMGR_QUERY_AUTO_MOUNT QueryState
;
142 Stack
= IoGetCurrentIrpStackLocation(Irp
);
144 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(MOUNTMGR_QUERY_AUTO_MOUNT
))
146 Irp
->IoStatus
.Information
= 0;
147 return STATUS_INVALID_PARAMETER
;
150 QueryState
= (PMOUNTMGR_QUERY_AUTO_MOUNT
)Irp
->AssociatedIrp
.SystemBuffer
;
151 QueryState
->CurrentState
= !DeviceExtension
->NoAutoMount
;
152 Irp
->IoStatus
.Information
= sizeof(MOUNTMGR_QUERY_AUTO_MOUNT
);
154 return STATUS_SUCCESS
;
162 ScrubRegistryRoutine(IN PWSTR ValueName
,
165 IN ULONG ValueLength
,
167 IN PVOID EntryContext
)
170 PLIST_ENTRY NextEntry
;
171 PDEVICE_INFORMATION DeviceInfo
;
172 PBOOLEAN Continue
= EntryContext
;
173 PDEVICE_EXTENSION DeviceExtension
= Context
;
175 if (ValueType
!= REG_BINARY
)
177 return STATUS_SUCCESS
;
180 /* Delete values for devices that don't have the matching unique ID */
181 if (!IsListEmpty(&(DeviceExtension
->DeviceListHead
)))
183 for (NextEntry
= DeviceExtension
->DeviceListHead
.Flink
;
184 NextEntry
!= &(DeviceExtension
->DeviceListHead
);
185 NextEntry
= NextEntry
->Flink
)
187 DeviceInfo
= CONTAINING_RECORD(NextEntry
,
191 if (!DeviceInfo
->UniqueId
|| DeviceInfo
->UniqueId
->UniqueIdLength
!= ValueLength
)
196 if (RtlCompareMemory(DeviceInfo
->UniqueId
->UniqueId
, ValueData
, ValueLength
) == ValueLength
)
198 return STATUS_SUCCESS
;
203 /* Wrong unique ID, scrub it */
204 Status
= RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE
,
207 if (!NT_SUCCESS(Status
))
210 return STATUS_UNSUCCESSFUL
;
221 MountMgrScrubRegistry(IN PDEVICE_EXTENSION DeviceExtension
)
225 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
229 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
230 QueryTable
[0].QueryRoutine
= ScrubRegistryRoutine
;
231 QueryTable
[0].EntryContext
= &Continue
;
234 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
249 MountMgrCreatePoint(IN PDEVICE_EXTENSION DeviceExtension
,
253 PIO_STACK_LOCATION Stack
;
254 PMOUNTMGR_CREATE_POINT_INPUT Point
;
255 UNICODE_STRING DeviceName
, SymbolicName
;
257 Stack
= IoGetCurrentIrpStackLocation(Irp
);
259 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_CREATE_POINT_INPUT
))
261 return STATUS_INVALID_PARAMETER
;
264 Point
= (PMOUNTMGR_CREATE_POINT_INPUT
)Irp
->AssociatedIrp
.SystemBuffer
;
266 MaxLength
= MAX((Point
->DeviceNameOffset
+ Point
->DeviceNameLength
),
267 (Point
->SymbolicLinkNameLength
+ Point
->SymbolicLinkNameOffset
));
268 if (MaxLength
> Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
270 return STATUS_INVALID_PARAMETER
;
273 /* Get all the strings and call the worker */
274 SymbolicName
.Length
= Point
->SymbolicLinkNameLength
;
275 SymbolicName
.MaximumLength
= Point
->SymbolicLinkNameLength
;
276 DeviceName
.Length
= Point
->DeviceNameLength
;
277 DeviceName
.MaximumLength
= Point
->DeviceNameLength
;
278 SymbolicName
.Buffer
= (PVOID
)((ULONG_PTR
)Point
+ Point
->SymbolicLinkNameOffset
);
279 DeviceName
.Buffer
= (PVOID
)((ULONG_PTR
)Point
+ Point
->DeviceNameOffset
);
281 return MountMgrCreatePointWorker(DeviceExtension
, &SymbolicName
, &DeviceName
);
288 MountMgrCheckUnprocessedVolumes(IN PDEVICE_EXTENSION DeviceExtension
,
291 PLIST_ENTRY NextEntry
;
292 PDEVICE_INFORMATION DeviceInformation
;
293 NTSTATUS ArrivalStatus
, Status
= STATUS_SUCCESS
;
295 UNREFERENCED_PARAMETER(Irp
);
297 /* No offline volumes, nothing more to do */
298 if (IsListEmpty(&(DeviceExtension
->OfflineDeviceListHead
)))
300 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
301 return STATUS_SUCCESS
;
304 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
306 /* Reactivate all the offline volumes */
307 while (!IsListEmpty(&(DeviceExtension
->OfflineDeviceListHead
)))
309 NextEntry
= RemoveHeadList(&(DeviceExtension
->OfflineDeviceListHead
));
310 DeviceInformation
= CONTAINING_RECORD(NextEntry
, DEVICE_INFORMATION
, DeviceListEntry
);
312 ArrivalStatus
= MountMgrMountedDeviceArrival(DeviceExtension
,
313 &(DeviceInformation
->SymbolicName
),
314 DeviceInformation
->ManuallyRegistered
);
315 /* Then, remove them dead information */
316 MountMgrFreeDeadDeviceInfo(DeviceInformation
);
318 if (NT_SUCCESS(Status
))
320 Status
= ArrivalStatus
;
331 IsFtVolume(IN PUNICODE_STRING SymbolicName
)
336 PFILE_OBJECT FileObject
;
337 IO_STATUS_BLOCK IoStatusBlock
;
338 PARTITION_INFORMATION PartitionInfo
;
339 PDEVICE_OBJECT DeviceObject
, FileDeviceObject
;
341 /* Get device object */
342 Status
= IoGetDeviceObjectPointer(SymbolicName
,
343 FILE_READ_ATTRIBUTES
,
346 if (!NT_SUCCESS(Status
))
351 /* Get attached device */
352 FileDeviceObject
= FileObject
->DeviceObject
;
353 DeviceObject
= IoGetAttachedDeviceReference(FileDeviceObject
);
355 /* FT volume can't be removable */
356 if (FileDeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
)
358 ObDereferenceObject(DeviceObject
);
359 ObDereferenceObject(FileObject
);
363 ObDereferenceObject(FileObject
);
365 /* Get partition information */
366 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
367 Irp
= IoBuildDeviceIoControlRequest(IOCTL_DISK_GET_PARTITION_INFO
,
372 sizeof(PartitionInfo
),
378 ObDereferenceObject(DeviceObject
);
382 Status
= IoCallDriver(DeviceObject
, Irp
);
383 if (Status
== STATUS_PENDING
)
385 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
386 Status
= IoStatusBlock
.Status
;
389 ObDereferenceObject(DeviceObject
);
390 if (!NT_SUCCESS(Status
))
395 /* Check if this is a FT volume */
396 return IsRecognizedPartition(PartitionInfo
.PartitionType
);
403 ProcessSuggestedDriveLetters(IN PDEVICE_EXTENSION DeviceExtension
)
405 WCHAR NameBuffer
[DRIVE_LETTER_LENGTH
/ sizeof(WCHAR
)];
406 PLIST_ENTRY NextEntry
;
407 UNICODE_STRING SymbolicName
;
408 PDEVICE_INFORMATION DeviceInformation
;
410 /* No devices? Nothing to do! */
411 if (IsListEmpty(&(DeviceExtension
->DeviceListHead
)))
416 /* For all the devices */
417 for (NextEntry
= DeviceExtension
->DeviceListHead
.Flink
;
418 NextEntry
!= &(DeviceExtension
->DeviceListHead
);
419 NextEntry
= NextEntry
->Flink
)
421 DeviceInformation
= CONTAINING_RECORD(NextEntry
, DEVICE_INFORMATION
, DeviceListEntry
);
423 /* If no drive letter */
424 if (DeviceInformation
->SuggestedDriveLetter
== (UCHAR
)-1)
426 /* Ensure it has no entry yet */
427 if (!HasDriveLetter(DeviceInformation
) &&
428 !HasNoDriveLetterEntry(DeviceInformation
->UniqueId
))
431 CreateNoDriveLetterEntry(DeviceInformation
->UniqueId
);
434 DeviceInformation
->SuggestedDriveLetter
= 0;
436 /* Suggested letter & no entry */
437 else if (DeviceInformation
->SuggestedDriveLetter
&&
438 !HasNoDriveLetterEntry(DeviceInformation
->UniqueId
))
440 /* Just create a mount point */
441 SymbolicName
.Buffer
= NameBuffer
;
442 RtlCopyMemory(NameBuffer
, DosDevices
.Buffer
, DosDevices
.Length
);
443 NameBuffer
[LETTER_POSITION
] = DeviceInformation
->SuggestedDriveLetter
;
444 NameBuffer
[COLON_POSITION
] = L
':';
445 SymbolicName
.Length
=
446 SymbolicName
.MaximumLength
= DRIVE_LETTER_LENGTH
;
448 MountMgrCreatePointWorker(DeviceExtension
, &SymbolicName
, &(DeviceInformation
->DeviceName
));
457 MountMgrNextDriveLetterWorker(IN PDEVICE_EXTENSION DeviceExtension
,
458 IN PUNICODE_STRING DeviceName
,
459 OUT PMOUNTMGR_DRIVE_LETTER_INFORMATION DriveLetterInfo
)
463 PLIST_ENTRY NextEntry
;
464 PMOUNTDEV_UNIQUE_ID UniqueId
;
465 BOOLEAN Removable
, GptDriveLetter
;
466 PDEVICE_INFORMATION DeviceInformation
;
467 WCHAR NameBuffer
[DRIVE_LETTER_LENGTH
];
468 PSYMLINK_INFORMATION SymlinkInformation
;
469 UNICODE_STRING TargetDeviceName
, SymbolicName
;
471 /* First, process suggested letters */
472 if (!DeviceExtension
->ProcessedSuggestions
)
474 ProcessSuggestedDriveLetters(DeviceExtension
);
475 DeviceExtension
->ProcessedSuggestions
= TRUE
;
478 /* Then, get information about the device */
479 Status
= QueryDeviceInformation(DeviceName
, &TargetDeviceName
, NULL
, &Removable
, &GptDriveLetter
, NULL
, NULL
, NULL
);
480 if (!NT_SUCCESS(Status
))
485 /* Ensure we have such device */
486 NextEntry
= DeviceExtension
->DeviceListHead
.Flink
;
487 while (NextEntry
!= &(DeviceExtension
->DeviceListHead
))
489 DeviceInformation
= CONTAINING_RECORD(NextEntry
, DEVICE_INFORMATION
, DeviceListEntry
);
491 if (RtlCompareUnicodeString(&(DeviceInformation
->DeviceName
), &TargetDeviceName
, TRUE
) == 0)
496 NextEntry
= NextEntry
->Flink
;
499 if (NextEntry
== &(DeviceExtension
->DeviceListHead
))
501 FreePool(TargetDeviceName
.Buffer
);
502 return STATUS_OBJECT_NAME_NOT_FOUND
;
505 /* Now, mark we have assigned a letter (assumption) */
506 DeviceInformation
->LetterAssigned
=
507 DriveLetterInfo
->DriveLetterWasAssigned
= TRUE
;
509 /* Browse all the symlink to see if there's already a drive letter */
510 NextEntry
= DeviceInformation
->SymbolicLinksListHead
.Flink
;
511 while (NextEntry
!= &(DeviceInformation
->SymbolicLinksListHead
))
513 SymlinkInformation
= CONTAINING_RECORD(NextEntry
, SYMLINK_INFORMATION
, SymbolicLinksListEntry
);
515 /* This is a driver letter & online one, forget about new drive eltter */
516 if (IsDriveLetter(&(SymlinkInformation
->Name
)) && SymlinkInformation
->Online
)
518 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
519 DriveLetterInfo
->CurrentDriveLetter
= (CHAR
)SymlinkInformation
->Name
.Buffer
[LETTER_POSITION
];
523 NextEntry
= NextEntry
->Flink
;
526 /* If we didn't find a drive letter online
527 * ensure there's no GPT drive letter nor no drive entry
529 if (NextEntry
== &(DeviceInformation
->SymbolicLinksListHead
))
531 if (GptDriveLetter
|| HasNoDriveLetterEntry(DeviceInformation
->UniqueId
))
533 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
534 DriveLetterInfo
->CurrentDriveLetter
= 0;
540 /* No, ensure that the device is not automounted nor removable */
541 if (!DeviceExtension
->NoAutoMount
&& !Removable
)
543 if (DriveLetterInfo
->DriveLetterWasAssigned
)
545 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
546 DriveLetterInfo
->CurrentDriveLetter
= 0;
552 if (!DriveLetterInfo
->DriveLetterWasAssigned
)
557 /* Now everything is fine, start processing */
559 if (RtlPrefixUnicodeString(&DeviceFloppy
, &TargetDeviceName
, TRUE
))
561 /* If the device is a floppy, start with letter A */
564 else if (RtlPrefixUnicodeString(&DeviceCdRom
, &TargetDeviceName
, TRUE
))
566 /* If the device is a CD-ROM, start with letter D */
571 /* Finally, if it's a disk, use C */
575 /* We cannot set NO drive letter */
576 ASSERT(DeviceInformation
->SuggestedDriveLetter
!= (UCHAR
)-1);
578 /* If we don't have suggested letter but it's a FT volume, fail */
579 if (!DeviceInformation
->SuggestedDriveLetter
&& IsFtVolume(&(DeviceInformation
->DeviceName
)))
581 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
582 DriveLetterInfo
->CurrentDriveLetter
= 0;
588 RtlCopyMemory(NameBuffer
, DosDevices
.Buffer
, DosDevices
.Length
);
589 NameBuffer
[COLON_POSITION
] = L
':';
590 SymbolicName
.Buffer
= NameBuffer
;
591 SymbolicName
.Length
=
592 SymbolicName
.MaximumLength
= DRIVE_LETTER_LENGTH
;
594 /* It's all prepared, create mount point */
595 if (DeviceInformation
->SuggestedDriveLetter
)
597 DriveLetterInfo
->CurrentDriveLetter
= DeviceInformation
->SuggestedDriveLetter
;
598 NameBuffer
[LETTER_POSITION
] = DeviceInformation
->SuggestedDriveLetter
;
600 Status
= MountMgrCreatePointWorker(DeviceExtension
, &SymbolicName
, &TargetDeviceName
);
601 if (NT_SUCCESS(Status
))
607 /* It failed with this letter... Try another one! */
608 for (DriveLetterInfo
->CurrentDriveLetter
= DriveLetter
;
609 DriveLetterInfo
->CurrentDriveLetter
<= L
'Z';
610 DriveLetterInfo
->CurrentDriveLetter
++)
612 NameBuffer
[LETTER_POSITION
] = DeviceInformation
->SuggestedDriveLetter
;
614 Status
= MountMgrCreatePointWorker(DeviceExtension
, &SymbolicName
, &TargetDeviceName
);
615 if (NT_SUCCESS(Status
))
621 /* We failed setting a letter */
622 if (DriveLetterInfo
->CurrentDriveLetter
> L
'Z')
624 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
625 DriveLetterInfo
->CurrentDriveLetter
= 0;
627 /* Try at least to add a no drive letter entry */
628 Status
= QueryDeviceInformation(&TargetDeviceName
, NULL
, &UniqueId
, NULL
, NULL
, NULL
, NULL
, NULL
);
629 if (NT_SUCCESS(Status
))
631 CreateNoDriveLetterEntry(UniqueId
);
637 FreePool(TargetDeviceName
.Buffer
);
639 return STATUS_SUCCESS
;
647 MountMgrNextDriveLetter(IN PDEVICE_EXTENSION DeviceExtension
,
651 PIO_STACK_LOCATION Stack
;
652 UNICODE_STRING DeviceName
;
653 PMOUNTMGR_DRIVE_LETTER_TARGET DriveLetterTarget
;
654 MOUNTMGR_DRIVE_LETTER_INFORMATION DriveLetterInformation
;
656 Stack
= IoGetCurrentIrpStackLocation(Irp
);
659 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_DRIVE_LETTER_TARGET
) ||
660 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(MOUNTMGR_DRIVE_LETTER_INFORMATION
))
662 return STATUS_INVALID_PARAMETER
;
665 DriveLetterTarget
= (PMOUNTMGR_DRIVE_LETTER_TARGET
)Irp
->AssociatedIrp
.SystemBuffer
;
666 if (DriveLetterTarget
->DeviceNameLength
+ sizeof(USHORT
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
668 return STATUS_INVALID_PARAMETER
;
671 /* Call the worker */
672 DeviceName
.Buffer
= DriveLetterTarget
->DeviceName
;
674 DeviceName
.MaximumLength
= DriveLetterTarget
->DeviceNameLength
;
676 Status
= MountMgrNextDriveLetterWorker(DeviceExtension
, &DeviceName
,
677 &DriveLetterInformation
);
678 if (NT_SUCCESS(Status
))
680 *(PMOUNTMGR_DRIVE_LETTER_INFORMATION
)Irp
->AssociatedIrp
.SystemBuffer
=
681 DriveLetterInformation
;
682 Irp
->IoStatus
.Information
= sizeof(MOUNTMGR_DRIVE_LETTER_INFORMATION
);
693 MountMgrQuerySystemVolumeNameQueryRoutine(IN PWSTR ValueName
,
696 IN ULONG ValueLength
,
698 IN PVOID EntryContext
)
700 UNICODE_STRING ValueString
;
701 PUNICODE_STRING SystemVolumeName
;
703 UNREFERENCED_PARAMETER(ValueName
);
704 UNREFERENCED_PARAMETER(ValueLength
);
705 UNREFERENCED_PARAMETER(EntryContext
);
707 if (ValueType
!= REG_SZ
)
709 return STATUS_SUCCESS
;
712 RtlInitUnicodeString(&ValueString
, ValueData
);
713 SystemVolumeName
= Context
;
715 /* Return a string containing system volume name */
716 SystemVolumeName
->Length
= ValueString
.Length
;
717 SystemVolumeName
->MaximumLength
= ValueString
.Length
+ sizeof(WCHAR
);
718 SystemVolumeName
->Buffer
= AllocatePool(SystemVolumeName
->MaximumLength
);
719 if (SystemVolumeName
->Buffer
)
721 RtlCopyMemory(SystemVolumeName
->Buffer
, ValueData
, ValueString
.Length
);
722 SystemVolumeName
->Buffer
[ValueString
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
725 return STATUS_SUCCESS
;
733 MountMgrQuerySystemVolumeName(OUT PUNICODE_STRING SystemVolumeName
)
735 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
737 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
738 QueryTable
[0].QueryRoutine
= MountMgrQuerySystemVolumeNameQueryRoutine
;
739 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_REQUIRED
;
740 QueryTable
[0].Name
= L
"SystemPartition";
742 SystemVolumeName
->Buffer
= NULL
;
744 RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
745 L
"\\Registry\\Machine\\System\\Setup",
750 if (SystemVolumeName
->Buffer
)
752 return STATUS_SUCCESS
;
755 return STATUS_UNSUCCESSFUL
;
762 MountMgrAssignDriveLetters(IN PDEVICE_EXTENSION DeviceExtension
)
765 PLIST_ENTRY NextEntry
;
766 UNICODE_STRING SystemVolumeName
;
767 PDEVICE_INFORMATION DeviceInformation
;
768 MOUNTMGR_DRIVE_LETTER_INFORMATION DriveLetterInformation
;
770 /* First, get system volume name */
771 Status
= MountMgrQuerySystemVolumeName(&SystemVolumeName
);
773 /* If there are no device, it's all done */
774 if (IsListEmpty(&(DeviceExtension
->DeviceListHead
)))
776 if (NT_SUCCESS(Status
))
778 FreePool(SystemVolumeName
.Buffer
);
784 /* Now, for all the devices... */
785 for (NextEntry
= DeviceExtension
->DeviceListHead
.Flink
;
786 NextEntry
!= &(DeviceExtension
->DeviceListHead
);
787 NextEntry
= NextEntry
->Flink
)
789 DeviceInformation
= CONTAINING_RECORD(NextEntry
, DEVICE_INFORMATION
, DeviceListEntry
);
791 /* If the device doesn't have a letter assigned, do it! */
792 if (!DeviceInformation
->LetterAssigned
)
794 MountMgrNextDriveLetterWorker(DeviceExtension
,
795 &(DeviceInformation
->DeviceName
),
796 &DriveLetterInformation
);
799 /* If it was the system volume */
800 if (NT_SUCCESS(Status
) && RtlEqualUnicodeString(&SystemVolumeName
, &(DeviceInformation
->DeviceName
), TRUE
))
802 /* Keep track of it */
803 DeviceExtension
->DriveLetterData
= AllocatePool(DeviceInformation
->UniqueId
->UniqueIdLength
+
804 sizeof(MOUNTDEV_UNIQUE_ID
));
805 if (DeviceExtension
->DriveLetterData
)
807 RtlCopyMemory(DeviceExtension
->DriveLetterData
,
808 DeviceInformation
->UniqueId
,
809 DeviceInformation
->UniqueId
->UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
812 /* If it was not automount, ensure it gets mounted */
813 if (!DeviceExtension
->NoAutoMount
)
815 DeviceExtension
->NoAutoMount
= TRUE
;
817 MountMgrNextDriveLetterWorker(DeviceExtension
,
818 &(DeviceInformation
->DeviceName
),
819 &DriveLetterInformation
);
821 DeviceExtension
->NoAutoMount
= FALSE
;
826 if (NT_SUCCESS(Status
))
828 FreePool(SystemVolumeName
.Buffer
);
836 MountMgrQueryDosVolumePath(IN PDEVICE_EXTENSION DeviceExtension
,
841 PIO_STACK_LOCATION Stack
;
842 PLIST_ENTRY SymlinksEntry
;
843 UNICODE_STRING SymbolicName
;
844 PMOUNTMGR_TARGET_NAME Target
;
845 PWSTR DeviceString
, OldBuffer
;
846 USHORT DeviceLength
, OldLength
;
847 PDEVICE_INFORMATION DeviceInformation
;
848 PSYMLINK_INFORMATION SymlinkInformation
;
849 PASSOCIATED_DEVICE_ENTRY AssociatedDevice
;
851 Stack
= IoGetCurrentIrpStackLocation(Irp
);
853 /* Validate input size */
854 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_TARGET_NAME
))
856 return STATUS_INVALID_PARAMETER
;
859 /* Ensure we have received UNICODE_STRING */
860 Target
= (PMOUNTMGR_TARGET_NAME
)Irp
->AssociatedIrp
.SystemBuffer
;
861 if (Target
->DeviceNameLength
& 1)
863 return STATUS_INVALID_PARAMETER
;
866 /* Validate the entry structure size */
867 if (Target
->DeviceNameLength
+ sizeof(UNICODE_NULL
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
869 return STATUS_INVALID_PARAMETER
;
872 /* Ensure we can at least return needed size */
873 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(ULONG
))
875 return STATUS_INVALID_PARAMETER
;
878 /* Construct string for query */
879 SymbolicName
.Length
= Target
->DeviceNameLength
;
880 SymbolicName
.MaximumLength
= Target
->DeviceNameLength
+ sizeof(UNICODE_NULL
);
881 SymbolicName
.Buffer
= Target
->DeviceName
;
883 /* Find device with our info */
884 Status
= FindDeviceInfo(DeviceExtension
, &SymbolicName
, FALSE
, &DeviceInformation
);
885 if (!NT_SUCCESS(Status
))
894 /* Try to find associated device info */
897 for (SymlinksEntry
= DeviceInformation
->SymbolicLinksListHead
.Flink
;
898 SymlinksEntry
!= &(DeviceInformation
->SymbolicLinksListHead
);
899 SymlinksEntry
= SymlinksEntry
->Flink
)
901 SymlinkInformation
= CONTAINING_RECORD(SymlinksEntry
, SYMLINK_INFORMATION
, SymbolicLinksListEntry
);
903 /* Try to find with drive letter */
904 if (MOUNTMGR_IS_DRIVE_LETTER(&SymlinkInformation
->Name
) && SymlinkInformation
->Online
)
910 /* We didn't find, break */
911 if (SymlinksEntry
== &(DeviceInformation
->SymbolicLinksListHead
))
916 /* It doesn't have associated device, go to fallback method */
917 if (IsListEmpty(&DeviceInformation
->AssociatedDevicesHead
))
919 goto TryWithVolumeName
;
922 /* Create a string with the information about the device */
923 AssociatedDevice
= CONTAINING_RECORD(&(DeviceInformation
->SymbolicLinksListHead
), ASSOCIATED_DEVICE_ENTRY
, AssociatedDevicesEntry
);
924 OldLength
= DeviceLength
;
925 OldBuffer
= DeviceString
;
926 DeviceLength
+= AssociatedDevice
->String
.Length
;
927 DeviceString
= AllocatePool(DeviceLength
);
935 return STATUS_INSUFFICIENT_RESOURCES
;
938 /* Store our info and previous if any */
939 RtlCopyMemory(DeviceString
, AssociatedDevice
->String
.Buffer
, AssociatedDevice
->String
.Length
);
942 RtlCopyMemory(&DeviceString
[AssociatedDevice
->String
.Length
/ sizeof(WCHAR
)], OldBuffer
, OldLength
);
946 /* Count and continue looking */
948 DeviceInformation
= AssociatedDevice
->DeviceInformation
;
950 /* If too many devices, try another way */
951 if (DevicesFound
> MAX_DEVICES
) /* 1000 */
953 goto TryWithVolumeName
;
957 /* Reallocate our string, so that we can prepend disk letter */
958 OldBuffer
= DeviceString
;
959 OldLength
= DeviceLength
;
960 DeviceLength
+= 2 * sizeof(WCHAR
);
961 DeviceString
= AllocatePool(DeviceLength
);
969 return STATUS_INSUFFICIENT_RESOURCES
;
973 DeviceString
[0] = SymlinkInformation
->Name
.Buffer
[LETTER_POSITION
];
974 DeviceString
[1] = L
':';
976 /* And copy the rest */
979 RtlCopyMemory(&DeviceString
[2], OldBuffer
, OldLength
);
984 /* If we didn't find anything, try differently */
985 if (DeviceLength
< 2 * sizeof(WCHAR
) || DeviceString
[1] != L
':')
989 FreePool(DeviceString
);
993 /* Try to find a volume name matching */
994 for (SymlinksEntry
= DeviceInformation
->SymbolicLinksListHead
.Flink
;
995 SymlinksEntry
!= &(DeviceInformation
->SymbolicLinksListHead
);
996 SymlinksEntry
= SymlinksEntry
->Flink
)
998 SymlinkInformation
= CONTAINING_RECORD(SymlinksEntry
, SYMLINK_INFORMATION
, SymbolicLinksListEntry
);
1000 if (MOUNTMGR_IS_VOLUME_NAME(&SymlinkInformation
->Name
))
1007 if (SymlinksEntry
!= &(DeviceInformation
->SymbolicLinksListHead
))
1009 DeviceLength
= SymlinkInformation
->Name
.Length
;
1010 DeviceString
= AllocatePool(DeviceLength
);
1013 return STATUS_INSUFFICIENT_RESOURCES
;
1016 RtlCopyMemory(DeviceString
, SymlinkInformation
->Name
.Buffer
, DeviceLength
);
1017 /* Ensure we are in the right namespace; [1] can be ? */
1018 DeviceString
[1] = L
'\\';
1022 /* If we found something */
1025 /* At least, we will return our length */
1026 ((PMOUNTMGR_VOLUME_PATHS
)Irp
->AssociatedIrp
.SystemBuffer
)->MultiSzLength
= DeviceLength
;
1027 /* MOUNTMGR_VOLUME_PATHS is a string + a ULONG */
1028 Irp
->IoStatus
.Information
= DeviceLength
+ sizeof(ULONG
);
1030 /* If we have enough room for copying the string */
1031 if (sizeof(ULONG
) + DeviceLength
<= Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
)
1036 RtlCopyMemory(((PMOUNTMGR_VOLUME_PATHS
)Irp
->AssociatedIrp
.SystemBuffer
)->MultiSz
, DeviceString
, DeviceLength
);
1039 /* And double zero at its end - this is needed in case of multiple paths which are separated by a single 0 */
1040 FreePool(DeviceString
);
1041 ((PMOUNTMGR_VOLUME_PATHS
)Irp
->AssociatedIrp
.SystemBuffer
)->MultiSz
[DeviceLength
/ sizeof(WCHAR
)] = 0;
1042 ((PMOUNTMGR_VOLUME_PATHS
)Irp
->AssociatedIrp
.SystemBuffer
)->MultiSz
[DeviceLength
/ sizeof(WCHAR
) + 1] = 0;
1044 return STATUS_SUCCESS
;
1048 /* Just return appropriate size and leave */
1049 FreePool(DeviceString
);
1050 Irp
->IoStatus
.Information
= sizeof(ULONG
);
1051 return STATUS_BUFFER_OVERFLOW
;
1056 return STATUS_NOT_FOUND
;
1063 MountMgrValidateBackPointer(IN PASSOCIATED_DEVICE_ENTRY AssociatedDeviceEntry
,
1064 IN PDEVICE_INFORMATION DeviceInformation
,
1065 OUT PBOOLEAN Invalid
)
1069 PLIST_ENTRY SymlinksEntry
;
1070 IO_STATUS_BLOCK IoStatusBlock
;
1071 PREPARSE_DATA_BUFFER ReparseData
;
1072 OBJECT_ATTRIBUTES ObjectAttributes
;
1073 UNICODE_STRING FullName
, SubstituteName
;
1074 PSYMLINK_INFORMATION SymlinkInformation
;
1076 /* Initialize & allocate a string big enough to contain our complete mount point name */
1077 FullName
.Length
= AssociatedDeviceEntry
->String
.Length
+ AssociatedDeviceEntry
->DeviceInformation
->DeviceName
.Length
+ sizeof(WCHAR
);
1078 FullName
.MaximumLength
= FullName
.Length
+ sizeof(UNICODE_NULL
);
1079 FullName
.Buffer
= AllocatePool(FullName
.MaximumLength
);
1080 if (!FullName
.Buffer
)
1082 return STATUS_INSUFFICIENT_RESOURCES
;
1085 /* Create the path */
1086 RtlCopyMemory(FullName
.Buffer
, AssociatedDeviceEntry
->DeviceInformation
->DeviceName
.Buffer
, AssociatedDeviceEntry
->DeviceInformation
->DeviceName
.Length
);
1087 FullName
.Buffer
[AssociatedDeviceEntry
->DeviceInformation
->DeviceName
.Length
/ sizeof(WCHAR
)] = L
'\\';
1088 RtlCopyMemory(&FullName
.Buffer
[AssociatedDeviceEntry
->DeviceInformation
->DeviceName
.Length
/ sizeof(WCHAR
) + 1], AssociatedDeviceEntry
->String
.Buffer
, AssociatedDeviceEntry
->String
.Length
);
1089 FullName
.Buffer
[FullName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1091 /* Open it to query the reparse point */
1092 InitializeObjectAttributes(&ObjectAttributes
,
1094 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1097 Status
= ZwOpenFile(&Handle
,
1098 SYNCHRONIZE
| FILE_READ_ATTRIBUTES
,
1099 &ObjectAttributes
, &IoStatusBlock
,
1100 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1101 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_REPARSE_POINT
);
1102 FreePool(FullName
.Buffer
);
1104 if (!NT_SUCCESS(Status
))
1107 return STATUS_SUCCESS
;
1110 /* Allocate a buffer big enough to read reparse data */
1111 ReparseData
= AllocatePool(MAXIMUM_REPARSE_DATA_BUFFER_SIZE
);
1112 if (ReparseData
== NULL
)
1115 return STATUS_INSUFFICIENT_RESOURCES
;
1118 /* Query reparse data */
1119 Status
= ZwFsControlFile(Handle
,
1122 FSCTL_GET_REPARSE_POINT
,
1124 ReparseData
, MAXIMUM_REPARSE_DATA_BUFFER_SIZE
);
1127 if (!NT_SUCCESS(Status
))
1129 FreePool(ReparseData
);
1131 return STATUS_SUCCESS
;
1134 /* Create a string with the substitute name */
1135 SubstituteName
.Length
= ReparseData
->SymbolicLinkReparseBuffer
.SubstituteNameLength
;
1136 SubstituteName
.MaximumLength
= SubstituteName
.Length
;
1137 SubstituteName
.Buffer
= (PWSTR
)((ULONG_PTR
)ReparseData
->SymbolicLinkReparseBuffer
.PathBuffer
+ ReparseData
->SymbolicLinkReparseBuffer
.SubstituteNameOffset
);
1139 /* If that's a volume name that matches our associated device, that's a success! */
1140 if (MOUNTMGR_IS_VOLUME_NAME(&SubstituteName
))
1142 if (SubstituteName
.Length
== 98 && SubstituteName
.Buffer
[1] == L
'?')
1144 for (SymlinksEntry
= DeviceInformation
->SymbolicLinksListHead
.Flink
;
1145 SymlinksEntry
!= &(DeviceInformation
->SymbolicLinksListHead
);
1146 SymlinksEntry
= SymlinksEntry
->Flink
)
1148 SymlinkInformation
= CONTAINING_RECORD(SymlinksEntry
, SYMLINK_INFORMATION
, SymbolicLinksListEntry
);
1150 if (RtlEqualUnicodeString(&SubstituteName
, &SymlinkInformation
->Name
, TRUE
))
1152 FreePool(ReparseData
);
1153 return STATUS_SUCCESS
;
1159 FreePool(ReparseData
);
1161 return STATUS_SUCCESS
;
1168 MountMgrQueryVolumePaths(IN PDEVICE_EXTENSION DeviceExtension
,
1169 IN PDEVICE_INFORMATION DeviceInformation
,
1170 IN PLIST_ENTRY DeviceInfoList
,
1171 OUT PMOUNTMGR_VOLUME_PATHS
* VolumePaths
,
1172 OUT PDEVICE_INFORMATION
*FailedDevice
)
1177 PSYMLINK_INFORMATION SymlinkInformation
;
1178 PDEVICE_INFORMATION_ENTRY DeviceInfoEntry
;
1179 PASSOCIATED_DEVICE_ENTRY AssociatedDeviceEntry
;
1180 PMOUNTMGR_VOLUME_PATHS
* Paths
= NULL
, * CurrentPath
;
1181 ULONG OutputPathLength
, NumberOfPaths
, ReturnedPaths
;
1183 /* We return at least null char */
1184 OutputPathLength
= sizeof(UNICODE_NULL
);
1186 for (Entry
= DeviceInformation
->SymbolicLinksListHead
.Flink
;
1187 Entry
!= &(DeviceInformation
->SymbolicLinksListHead
);
1188 Entry
= Entry
->Flink
)
1190 SymlinkInformation
= CONTAINING_RECORD(Entry
, SYMLINK_INFORMATION
, SymbolicLinksListEntry
);
1192 /* Try to find the drive letter (ie, DOS device) */
1193 if (MOUNTMGR_IS_DRIVE_LETTER(&SymlinkInformation
->Name
) && SymlinkInformation
->Online
)
1195 /* We'll return the letter */
1196 OutputPathLength
= 4 * sizeof(WCHAR
);
1201 /* We didn't find any */
1202 if (Entry
== &(DeviceInformation
->SymbolicLinksListHead
))
1204 SymlinkInformation
= NULL
;
1207 /* Do we have any device info to return? */
1208 for (Entry
= DeviceInfoList
->Flink
; Entry
!= DeviceInfoList
; Entry
= Entry
->Flink
)
1210 DeviceInfoEntry
= CONTAINING_RECORD(Entry
, DEVICE_INFORMATION_ENTRY
, DeviceInformationEntry
);
1212 /* Matching current device */
1213 if (DeviceInfoEntry
->DeviceInformation
== DeviceInformation
)
1215 /* Allocate the output buffer */
1216 *VolumePaths
= AllocatePool(sizeof(ULONG
) + OutputPathLength
);
1217 if (*VolumePaths
== NULL
)
1219 return STATUS_INSUFFICIENT_RESOURCES
;
1223 (*VolumePaths
)->MultiSzLength
= OutputPathLength
;
1224 /* If we have a drive letter, return it */
1225 if (SymlinkInformation
!= NULL
)
1227 (*VolumePaths
)->MultiSz
[0] = SymlinkInformation
->Name
.Buffer
[LETTER_POSITION
];
1228 (*VolumePaths
)->MultiSz
[1] = L
':';
1229 (*VolumePaths
)->MultiSz
[2] = UNICODE_NULL
;
1230 (*VolumePaths
)->MultiSz
[3] = UNICODE_NULL
;
1234 (*VolumePaths
)->MultiSz
[0] = UNICODE_NULL
;
1237 return STATUS_SUCCESS
;
1241 /* Allocate a new device entry */
1242 DeviceInfoEntry
= AllocatePool(sizeof(DEVICE_INFORMATION_ENTRY
));
1243 if (DeviceInfoEntry
== NULL
)
1245 return STATUS_INSUFFICIENT_RESOURCES
;
1248 /* Add it to the list */
1249 DeviceInfoEntry
->DeviceInformation
= DeviceInformation
;
1250 InsertTailList(DeviceInfoList
, &DeviceInfoEntry
->DeviceInformationEntry
);
1253 /* Count the amount of devices we will have to handle */
1254 if (!IsListEmpty(&DeviceInformation
->AssociatedDevicesHead
))
1256 for (Entry
= DeviceInformation
->AssociatedDevicesHead
.Flink
;
1257 Entry
!= &DeviceInformation
->AssociatedDevicesHead
;
1258 Entry
= Entry
->Flink
)
1263 ASSERT(NumberOfPaths
!= 0);
1264 /* And allocate a big enough buffer */
1265 Paths
= AllocatePool(NumberOfPaths
* sizeof(PMOUNTMGR_VOLUME_PATHS
));
1268 RemoveEntryList(&DeviceInfoEntry
->DeviceInformationEntry
);
1269 FreePool(DeviceInfoEntry
);
1270 return STATUS_INSUFFICIENT_RESOURCES
;
1274 /* Start the hot loop to gather all the paths and be able to compute total output length! */
1276 CurrentPath
= Paths
;
1277 for (Entry
= DeviceInformation
->AssociatedDevicesHead
.Flink
;
1278 Entry
!= &DeviceInformation
->AssociatedDevicesHead
;
1279 Entry
= Entry
->Flink
)
1281 USHORT InnerStrings
;
1282 BOOLEAN Invalid
= FALSE
;
1284 AssociatedDeviceEntry
= CONTAINING_RECORD(Entry
, ASSOCIATED_DEVICE_ENTRY
, AssociatedDevicesEntry
);
1286 /* Validate the fact its a mount point by query reparse data */
1287 Status
= MountMgrValidateBackPointer(AssociatedDeviceEntry
, DeviceInformation
, &Invalid
);
1289 /* If we found an invalid device, that's a failure */
1292 *FailedDevice
= AssociatedDeviceEntry
->DeviceInformation
;
1293 Status
= STATUS_UNSUCCESSFUL
;
1296 /* Check whether we failed, if so, bail out */
1297 if (!NT_SUCCESS(Status
))
1301 for (i
= 0; i
< ReturnedPaths
; ++i
)
1310 RemoveEntryList(&DeviceInfoEntry
->DeviceInformationEntry
);
1311 FreePool(DeviceInfoEntry
);
1315 /* Query associated paths (hello ourselves :-)) */
1316 Status
= MountMgrQueryVolumePaths(DeviceExtension
,
1317 AssociatedDeviceEntry
->DeviceInformation
,
1321 if (!NT_SUCCESS(Status
))
1325 for (i
= 0; i
< ReturnedPaths
; ++i
)
1334 RemoveEntryList(&DeviceInfoEntry
->DeviceInformationEntry
);
1335 FreePool(DeviceInfoEntry
);
1339 /* Count the number of strings we have in the multi string buffer */
1341 if ((*CurrentPath
)->MultiSzLength
!= sizeof(UNICODE_NULL
))
1344 PWSTR MultiSz
= (*CurrentPath
)->MultiSz
;
1346 for (i
= 0; i
< (*CurrentPath
)->MultiSzLength
/ sizeof(WCHAR
); ++i
, ++MultiSz
)
1348 if (*MultiSz
== UNICODE_NULL
)
1355 /* We returned one more path (ie, one more allocated buffer) */
1357 /* Move the next pointer to use in the array */
1359 /* Multiply String.Length by the number of found paths, we always add it after a path */
1360 OutputPathLength
+= (*CurrentPath
)->MultiSzLength
+ InnerStrings
* AssociatedDeviceEntry
->String
.Length
- sizeof(UNICODE_NULL
);
1363 /* Allocate the output buffer */
1364 *VolumePaths
= AllocatePool(sizeof(ULONG
) + OutputPathLength
);
1365 if (*VolumePaths
== NULL
)
1369 for (i
= 0; i
< ReturnedPaths
; ++i
)
1378 RemoveEntryList(&DeviceInfoEntry
->DeviceInformationEntry
);
1379 FreePool(DeviceInfoEntry
);
1380 return STATUS_INSUFFICIENT_RESOURCES
;
1384 /* If we had found a DOS letter, that's the first thing we return */
1385 (*VolumePaths
)->MultiSzLength
= OutputPathLength
;
1386 if (SymlinkInformation
!= NULL
)
1388 (*VolumePaths
)->MultiSz
[0] = SymlinkInformation
->Name
.Buffer
[LETTER_POSITION
];
1389 (*VolumePaths
)->MultiSz
[1] = L
':';
1390 (*VolumePaths
)->MultiSz
[2] = UNICODE_NULL
;
1394 /* Now, browse again all our paths to return them */
1395 CurrentPath
= Paths
;
1396 for (Entry
= DeviceInformation
->AssociatedDevicesHead
.Flink
;
1397 Entry
!= &DeviceInformation
->AssociatedDevicesHead
;
1398 Entry
= Entry
->Flink
)
1400 AssociatedDeviceEntry
= CONTAINING_RECORD(Entry
, ASSOCIATED_DEVICE_ENTRY
, AssociatedDevicesEntry
);
1402 /* If we had a path... */
1403 if ((*CurrentPath
)->MultiSzLength
!= sizeof(UNICODE_NULL
))
1408 /* This offset is used to "jump" into MultiSz, so, start with the string begin (ie, skip MultiSzLength) */
1409 Offset
= sizeof(ULONG
);
1410 /* Browse every single letter, and skip last UNICODE_NULL */
1411 for (i
= 0; i
< (*CurrentPath
)->MultiSzLength
/ sizeof(WCHAR
) - 1; ++i
)
1413 /* Get the letter */
1414 MultiSz
= (PWSTR
)((ULONG_PTR
)(*CurrentPath
) + Offset
);
1415 /* If it was part of the path, just return it */
1416 if (*MultiSz
!= UNICODE_NULL
)
1418 (*VolumePaths
)->MultiSz
[Written
] = *MultiSz
;
1422 /* Otherwise, as planed, return our whole associated device name */
1423 RtlCopyMemory(&(*VolumePaths
)->MultiSz
[Written
],
1424 AssociatedDeviceEntry
->String
.Buffer
,
1425 AssociatedDeviceEntry
->String
.Length
);
1426 Written
+= AssociatedDeviceEntry
->String
.Length
/ sizeof(WCHAR
);
1427 /* And don't forget to nullify */
1428 (*VolumePaths
)->MultiSz
[Written
] = UNICODE_NULL
;
1431 /* We at least return a letter or a null char */
1433 /* Move to the next letter */
1434 Offset
+= sizeof(WCHAR
);
1438 FreePool(*CurrentPath
);
1442 /* MultiSz: don't forget last null char */
1443 (*VolumePaths
)->MultiSz
[Written
] = UNICODE_NULL
;
1444 /* Cleanup everything and return success! */
1449 RemoveEntryList(&DeviceInfoEntry
->DeviceInformationEntry
);
1450 FreePool(DeviceInfoEntry
);
1451 return STATUS_SUCCESS
;
1458 MountMgrQueryDosVolumePaths(IN PDEVICE_EXTENSION DeviceExtension
,
1464 BOOLEAN NeedNotification
;
1465 PIO_STACK_LOCATION Stack
;
1466 UNICODE_STRING SymbolicName
;
1467 ULONG Attempts
, OutputLength
;
1468 PMOUNTMGR_TARGET_NAME Target
;
1469 PMOUNTMGR_VOLUME_PATHS Paths
, Output
;
1470 RECONCILE_WORK_ITEM_CONTEXT ReconcileContext
;
1471 PDEVICE_INFORMATION DeviceInformation
, ListDeviceInfo
, FailedDevice
;
1473 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1475 /* Validate input size */
1476 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_TARGET_NAME
))
1478 return STATUS_INVALID_PARAMETER
;
1481 /* Ensure we have received UNICODE_STRING */
1482 Target
= (PMOUNTMGR_TARGET_NAME
)Irp
->AssociatedIrp
.SystemBuffer
;
1483 if (Target
->DeviceNameLength
& 1)
1485 return STATUS_INVALID_PARAMETER
;
1488 /* Validate the entry structure size */
1489 if (Target
->DeviceNameLength
+ FIELD_OFFSET(MOUNTMGR_TARGET_NAME
, DeviceName
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
1491 return STATUS_INVALID_PARAMETER
;
1494 /* Ensure we can at least return needed size */
1495 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(ULONG
))
1497 return STATUS_INVALID_PARAMETER
;
1500 /* Construct string for query */
1501 SymbolicName
.Length
= Target
->DeviceNameLength
;
1502 SymbolicName
.MaximumLength
= Target
->DeviceNameLength
+ sizeof(UNICODE_NULL
);
1503 SymbolicName
.Buffer
= Target
->DeviceName
;
1505 /* Find device with our info */
1506 Status
= FindDeviceInfo(DeviceExtension
, &SymbolicName
, FALSE
, &DeviceInformation
);
1507 if (!NT_SUCCESS(Status
))
1512 NeedNotification
= FALSE
;
1516 FailedDevice
= NULL
;
1517 InitializeListHead(&Devices
);
1520 Status
= MountMgrQueryVolumePaths(DeviceExtension
, DeviceInformation
, &Devices
, &Paths
, &FailedDevice
);
1521 if (NT_SUCCESS(Status
))
1526 /* If it failed for generic reason (memory, whatever), bail out (ie, FailedDevice not set) */
1527 if (FailedDevice
== NULL
)
1532 /* If PnP, let's notify in case of success */
1533 if (!DeviceInformation
->ManuallyRegistered
)
1535 NeedNotification
= TRUE
;
1538 /* Reconcile database */
1539 ReconcileContext
.DeviceExtension
= DeviceExtension
;
1540 ReconcileContext
.DeviceInformation
= FailedDevice
;
1541 KeReleaseSemaphore(&DeviceExtension
->DeviceLock
, IO_NO_INCREMENT
, 1, FALSE
);
1542 ReconcileThisDatabaseWithMasterWorker(&ReconcileContext
);
1543 KeWaitForSingleObject(&DeviceExtension
->DeviceLock
, Executive
, KernelMode
, FALSE
, NULL
);
1545 /* Look for our device, to check it's online */
1546 for (Entry
= DeviceExtension
->DeviceListHead
.Flink
;
1547 Entry
!= &DeviceExtension
->DeviceListHead
;
1548 Entry
= Entry
->Flink
)
1550 ListDeviceInfo
= CONTAINING_RECORD(Entry
, DEVICE_INFORMATION
, DeviceListEntry
);
1551 /* It's online, it's OK! */
1552 if (ListDeviceInfo
== DeviceInformation
)
1558 /* It's not online, it's not good */
1559 if (Entry
== &DeviceExtension
->DeviceListHead
)
1561 return STATUS_OBJECT_NAME_NOT_FOUND
;
1564 /* Increase attempts count */
1566 /* Don't look forever and fail if we get out of attempts */
1567 if (Attempts
>= 1000)
1573 /* We need to notify? Go ahead */
1574 if (NeedNotification
)
1576 MountMgrNotifyNameChange(DeviceExtension
, &SymbolicName
, FALSE
);
1579 /* Get output buffer */
1580 Output
= (PMOUNTMGR_VOLUME_PATHS
)Irp
->AssociatedIrp
.SystemBuffer
;
1582 /* Set required size */
1583 Output
->MultiSzLength
= Paths
->MultiSzLength
;
1585 /* Compute total length */
1586 OutputLength
= Output
->MultiSzLength
+ sizeof(ULONG
);
1588 /* If it cannot fit, just return need size and quit */
1589 if (OutputLength
> Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
)
1591 Irp
->IoStatus
.Information
= sizeof(ULONG
);
1593 return STATUS_BUFFER_OVERFLOW
;
1596 /* Copy data and quit */
1597 Irp
->IoStatus
.Information
= OutputLength
;
1598 RtlCopyMemory(Output
->MultiSz
, Paths
->MultiSz
, Output
->MultiSzLength
);
1600 return STATUS_SUCCESS
;
1607 MountMgrKeepLinksWhenOffline(IN PDEVICE_EXTENSION DeviceExtension
,
1611 PIO_STACK_LOCATION Stack
;
1612 UNICODE_STRING SymbolicName
;
1613 PMOUNTMGR_TARGET_NAME Target
;
1614 PDEVICE_INFORMATION DeviceInformation
;
1616 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1618 /* Validate input */
1619 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_TARGET_NAME
))
1621 return STATUS_INVALID_PARAMETER
;
1624 Target
= (PMOUNTMGR_TARGET_NAME
)Irp
->AssociatedIrp
.SystemBuffer
;
1625 if (Target
->DeviceNameLength
+ sizeof(USHORT
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
1627 return STATUS_INVALID_PARAMETER
;
1630 SymbolicName
.Length
=
1631 SymbolicName
.MaximumLength
= Target
->DeviceNameLength
;
1632 SymbolicName
.Buffer
= Target
->DeviceName
;
1634 /* Find the associated device */
1635 Status
= FindDeviceInfo(DeviceExtension
, &SymbolicName
, FALSE
, &DeviceInformation
);
1636 if (!NT_SUCCESS(Status
))
1641 /* Mark we want to keep links */
1642 DeviceInformation
->KeepLinks
= TRUE
;
1644 return STATUS_SUCCESS
;
1651 MountMgrVolumeArrivalNotification(IN PDEVICE_EXTENSION DeviceExtension
,
1656 PIO_STACK_LOCATION Stack
;
1657 UNICODE_STRING SymbolicName
;
1658 PMOUNTMGR_TARGET_NAME Target
;
1660 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1662 /* Validate input */
1663 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_TARGET_NAME
))
1665 return STATUS_INVALID_PARAMETER
;
1668 Target
= (PMOUNTMGR_TARGET_NAME
)Irp
->AssociatedIrp
.SystemBuffer
;
1669 if (Target
->DeviceNameLength
+ sizeof(USHORT
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
1671 return STATUS_INVALID_PARAMETER
;
1674 SymbolicName
.Length
=
1675 SymbolicName
.MaximumLength
= Target
->DeviceNameLength
;
1676 SymbolicName
.Buffer
= Target
->DeviceName
;
1678 /* Disable hard errors */
1679 OldState
= PsGetThreadHardErrorsAreDisabled(PsGetCurrentThread());
1680 PsSetThreadHardErrorsAreDisabled(PsGetCurrentThread(), TRUE
);
1682 /* Call real worker */
1683 Status
= MountMgrMountedDeviceArrival(DeviceExtension
, &SymbolicName
, TRUE
);
1685 PsSetThreadHardErrorsAreDisabled(PsGetCurrentThread(), OldState
);
1694 MountMgrQueryPoints(IN PDEVICE_EXTENSION DeviceExtension
,
1698 PIO_STACK_LOCATION Stack
;
1699 PMOUNTDEV_UNIQUE_ID UniqueId
;
1700 PMOUNTMGR_MOUNT_POINT MountPoint
;
1701 UNICODE_STRING SymbolicName
, DeviceName
;
1703 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1705 /* Validate input... */
1706 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINT
))
1708 return STATUS_INVALID_PARAMETER
;
1711 MountPoint
= (PMOUNTMGR_MOUNT_POINT
)Irp
->AssociatedIrp
.SystemBuffer
;
1712 if (!MountPoint
->SymbolicLinkNameLength
)
1714 MountPoint
->SymbolicLinkNameOffset
= 0;
1717 if (!MountPoint
->UniqueIdLength
)
1719 MountPoint
->UniqueIdOffset
= 0;
1722 if (!MountPoint
->DeviceNameLength
)
1724 MountPoint
->DeviceNameOffset
= 0;
1727 /* Addresses can't be odd */
1728 if ((MountPoint
->SymbolicLinkNameOffset
& 1) ||
1729 (MountPoint
->SymbolicLinkNameLength
& 1))
1731 return STATUS_INVALID_PARAMETER
;
1734 if ((MountPoint
->UniqueIdOffset
& 1) ||
1735 (MountPoint
->UniqueIdLength
& 1))
1737 return STATUS_INVALID_PARAMETER
;
1740 if ((MountPoint
->DeviceNameOffset
& 1) ||
1741 (MountPoint
->DeviceNameLength
& 1))
1743 return STATUS_INVALID_PARAMETER
;
1746 /* We can't go beyond */
1747 if (((ULONG
)MountPoint
->SymbolicLinkNameLength
+ MountPoint
->UniqueIdLength
+
1748 MountPoint
->DeviceNameLength
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
1750 return STATUS_INVALID_PARAMETER
;
1753 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINTS
))
1755 return STATUS_INVALID_PARAMETER
;
1758 /* If caller provided a Symlink, use it */
1759 if (MountPoint
->SymbolicLinkNameLength
!= 0)
1761 if (MountPoint
->SymbolicLinkNameLength
> MAXSHORT
)
1763 return STATUS_INVALID_PARAMETER
;
1766 SymbolicName
.Length
= MountPoint
->SymbolicLinkNameLength
;
1767 SymbolicName
.MaximumLength
= MountPoint
->SymbolicLinkNameLength
+ sizeof(WCHAR
);
1768 SymbolicName
.Buffer
= AllocatePool(SymbolicName
.MaximumLength
);
1769 if (!SymbolicName
.Buffer
)
1771 return STATUS_INSUFFICIENT_RESOURCES
;
1774 RtlCopyMemory(SymbolicName
.Buffer
,
1775 (PWSTR
)((ULONG_PTR
)MountPoint
+ MountPoint
->SymbolicLinkNameOffset
),
1776 SymbolicName
.Length
);
1777 SymbolicName
.Buffer
[SymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1779 /* Query links using it */
1780 Status
= QueryPointsFromSymbolicLinkName(DeviceExtension
, &SymbolicName
, Irp
);
1781 FreePool(SymbolicName
.Buffer
);
1783 /* If user provided an unique ID */
1784 else if (MountPoint
->UniqueIdLength
!= 0)
1786 UniqueId
= AllocatePool(MountPoint
->UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
1789 return STATUS_INSUFFICIENT_RESOURCES
;
1792 UniqueId
->UniqueIdLength
= MountPoint
->UniqueIdLength
;
1793 RtlCopyMemory(UniqueId
->UniqueId
,
1794 (PVOID
)((ULONG_PTR
)MountPoint
+ MountPoint
->UniqueIdOffset
),
1795 MountPoint
->UniqueIdLength
);
1797 /* Query links using it */
1798 Status
= QueryPointsFromMemory(DeviceExtension
, Irp
, UniqueId
, NULL
);
1801 /* If caller provided a device name */
1802 else if (MountPoint
->DeviceNameLength
!= 0)
1804 if (MountPoint
->DeviceNameLength
> MAXSHORT
)
1806 return STATUS_INVALID_PARAMETER
;
1809 DeviceName
.Length
= MountPoint
->DeviceNameLength
;
1810 DeviceName
.MaximumLength
= MountPoint
->DeviceNameLength
+ sizeof(WCHAR
);
1811 DeviceName
.Buffer
= AllocatePool(DeviceName
.MaximumLength
);
1812 if (!DeviceName
.Buffer
)
1814 return STATUS_INSUFFICIENT_RESOURCES
;
1817 RtlCopyMemory(DeviceName
.Buffer
,
1818 (PWSTR
)((ULONG_PTR
)MountPoint
+ MountPoint
->DeviceNameOffset
),
1820 DeviceName
.Buffer
[DeviceName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1822 /* Query links using it */
1823 Status
= QueryPointsFromMemory(DeviceExtension
, Irp
, NULL
, &DeviceName
);
1824 FreePool(DeviceName
.Buffer
);
1828 /* Otherwise, query all links */
1829 Status
= QueryPointsFromMemory(DeviceExtension
, Irp
, NULL
, NULL
);
1839 MountMgrDeletePoints(IN PDEVICE_EXTENSION DeviceExtension
,
1844 BOOLEAN CreateNoDrive
;
1845 PIO_STACK_LOCATION Stack
;
1846 PMOUNTDEV_UNIQUE_ID UniqueId
;
1847 PMOUNTMGR_MOUNT_POINT MountPoint
;
1848 PMOUNTMGR_MOUNT_POINTS MountPoints
;
1849 UNICODE_STRING SymbolicName
, DeviceName
;
1851 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1853 /* Validate input */
1854 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINT
))
1856 return STATUS_INVALID_PARAMETER
;
1860 MountPoint
= (PMOUNTMGR_MOUNT_POINT
)Irp
->AssociatedIrp
.SystemBuffer
;
1861 CreateNoDrive
= (MountPoint
->SymbolicLinkNameOffset
&& MountPoint
->SymbolicLinkNameLength
);
1863 Status
= MountMgrQueryPoints(DeviceExtension
, Irp
);
1864 if (!NT_SUCCESS(Status
))
1869 /* For all the points matching the request */
1870 MountPoints
= (PMOUNTMGR_MOUNT_POINTS
)Irp
->AssociatedIrp
.SystemBuffer
;
1871 for (Link
= 0; Link
< MountPoints
->NumberOfMountPoints
; Link
++)
1873 SymbolicName
.Length
= MountPoints
->MountPoints
[Link
].SymbolicLinkNameLength
;
1874 SymbolicName
.MaximumLength
= SymbolicName
.Length
+ sizeof(WCHAR
);
1875 SymbolicName
.Buffer
= AllocatePool(SymbolicName
.MaximumLength
);
1876 if (!SymbolicName
.Buffer
)
1878 return STATUS_INSUFFICIENT_RESOURCES
;
1881 RtlCopyMemory(SymbolicName
.Buffer
,
1882 (PWSTR
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].SymbolicLinkNameOffset
),
1883 SymbolicName
.Length
);
1884 SymbolicName
.Buffer
[SymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1886 /* Create a no drive entry for the drive letters */
1887 if (CreateNoDrive
&& IsDriveLetter(&SymbolicName
))
1889 UniqueId
= AllocatePool(MountPoints
->MountPoints
[Link
].UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
1892 UniqueId
->UniqueIdLength
= MountPoints
->MountPoints
[Link
].UniqueIdLength
;
1893 RtlCopyMemory(UniqueId
->UniqueId
,
1894 (PMOUNTDEV_UNIQUE_ID
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].UniqueIdOffset
),
1895 MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1897 CreateNoDriveLetterEntry(UniqueId
);
1902 /* If there are no link any more, and no need to create a no drive entry */
1903 if (Link
== 0 && !CreateNoDrive
)
1905 /* Then, delete everything */
1906 UniqueId
= AllocatePool(MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1909 RtlCopyMemory(UniqueId
,
1910 (PMOUNTDEV_UNIQUE_ID
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].UniqueIdOffset
),
1911 MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1913 DeleteNoDriveLetterEntry(UniqueId
);
1918 /* Delete all the information about the mount point */
1919 GlobalDeleteSymbolicLink(&SymbolicName
);
1920 DeleteSymbolicLinkNameFromMemory(DeviceExtension
, &SymbolicName
, FALSE
);
1921 RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE
, DatabasePath
, SymbolicName
.Buffer
);
1922 FreePool(SymbolicName
.Buffer
);
1924 /* Notify the change */
1925 DeviceName
.Length
= DeviceName
.MaximumLength
=
1926 MountPoints
->MountPoints
[Link
].DeviceNameLength
;
1927 DeviceName
.Buffer
= (PWSTR
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].DeviceNameOffset
);
1928 MountMgrNotifyNameChange(DeviceExtension
, &DeviceName
, TRUE
);
1931 MountMgrNotify(DeviceExtension
);
1940 MountMgrDeletePointsDbOnly(IN PDEVICE_EXTENSION DeviceExtension
,
1945 UNICODE_STRING SymbolicName
;
1946 PMOUNTDEV_UNIQUE_ID UniqueId
;
1947 PMOUNTMGR_MOUNT_POINTS MountPoints
;
1950 Status
= MountMgrQueryPoints(DeviceExtension
, Irp
);
1951 if (!NT_SUCCESS(Status
))
1956 MountPoints
= (PMOUNTMGR_MOUNT_POINTS
)Irp
->AssociatedIrp
.SystemBuffer
;
1957 if (MountPoints
->NumberOfMountPoints
== 0)
1962 /* For all the mount points */
1963 for (Link
= 0; Link
< MountPoints
->NumberOfMountPoints
; Link
++)
1965 SymbolicName
.Length
= MountPoints
->MountPoints
[Link
].SymbolicLinkNameLength
;
1966 SymbolicName
.MaximumLength
= SymbolicName
.Length
+ sizeof(WCHAR
);
1967 SymbolicName
.Buffer
= AllocatePool(SymbolicName
.MaximumLength
);
1968 if (!SymbolicName
.Buffer
)
1970 return STATUS_INSUFFICIENT_RESOURCES
;
1973 RtlCopyMemory(SymbolicName
.Buffer
,
1974 (PWSTR
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].SymbolicLinkNameOffset
),
1975 SymbolicName
.Length
);
1976 SymbolicName
.Buffer
[SymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1978 /* If the only mount point is a drive letter, then create a no letter drive entry */
1979 if (MountPoints
->NumberOfMountPoints
== 1 && IsDriveLetter(&SymbolicName
))
1981 UniqueId
= AllocatePool(MountPoints
->MountPoints
[Link
].UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
1984 UniqueId
->UniqueIdLength
= MountPoints
->MountPoints
[Link
].UniqueIdLength
;
1985 RtlCopyMemory(UniqueId
->UniqueId
,
1986 (PMOUNTDEV_UNIQUE_ID
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].UniqueIdOffset
),
1987 MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1989 CreateNoDriveLetterEntry(UniqueId
);
1994 /* Simply delete mount point from DB */
1995 DeleteSymbolicLinkNameFromMemory(DeviceExtension
, &SymbolicName
, TRUE
);
1996 RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE
, DatabasePath
, SymbolicName
.Buffer
);
1997 FreePool(SymbolicName
.Buffer
);
2007 MountMgrVolumeMountPointChanged(IN PDEVICE_EXTENSION DeviceExtension
,
2009 IN NTSTATUS LockStatus
,
2010 OUT PUNICODE_STRING SourceDeviceName
,
2011 OUT PUNICODE_STRING SourceSymbolicName
,
2012 OUT PUNICODE_STRING TargetVolumeName
)
2016 PFILE_OBJECT FileObject
;
2017 PIO_STACK_LOCATION Stack
;
2018 ULONG Length
, SavedLength
;
2019 BOOLEAN FOReferenced
= FALSE
;
2020 IO_STATUS_BLOCK IoStatusBlock
;
2021 OBJECT_ATTRIBUTES ObjectAttributes
;
2022 PDEVICE_INFORMATION DeviceInformation
;
2023 OBJECT_NAME_INFORMATION ObjectNameInfo
;
2024 FILE_FS_DEVICE_INFORMATION FsDeviceInfo
;
2025 PFILE_NAME_INFORMATION FileNameInfo
= NULL
;
2026 PMOUNTMGR_VOLUME_MOUNT_POINT VolumeMountPoint
;
2027 POBJECT_NAME_INFORMATION ObjectNameInfoPtr
= NULL
;
2028 UNICODE_STRING SourceVolumeName
, TargetDeviceName
;
2030 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2032 /* Validate input */
2033 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_VOLUME_MOUNT_POINT
))
2035 return STATUS_INVALID_PARAMETER
;
2038 VolumeMountPoint
= (PMOUNTMGR_VOLUME_MOUNT_POINT
)Irp
->AssociatedIrp
.SystemBuffer
;
2040 if (((ULONG
)VolumeMountPoint
->SourceVolumeNameLength
+ VolumeMountPoint
->TargetVolumeNameLength
) <
2041 Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
2043 return STATUS_INVALID_PARAMETER
;
2046 /* Get source volume name */
2047 SourceVolumeName
.Length
=
2048 SourceVolumeName
.MaximumLength
= VolumeMountPoint
->SourceVolumeNameLength
;
2049 SourceVolumeName
.Buffer
= (PWSTR
)((ULONG_PTR
)VolumeMountPoint
+ VolumeMountPoint
->SourceVolumeNameOffset
);
2051 InitializeObjectAttributes(&ObjectAttributes
,
2053 OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
,
2058 Status
= ZwOpenFile(&Handle
,
2059 SYNCHRONIZE
| FILE_READ_ATTRIBUTES
,
2062 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2063 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_REPARSE_POINT
);
2064 if (!NT_SUCCESS(Status
))
2069 TargetDeviceName
.Buffer
= NULL
;
2071 /* Query its attributes */
2072 Status
= ZwQueryVolumeInformationFile(Handle
,
2075 sizeof(FsDeviceInfo
),
2076 FileFsDeviceInformation
);
2077 if (!NT_SUCCESS(Status
))
2082 if (FsDeviceInfo
.DeviceType
!= FILE_DEVICE_DISK
&& FsDeviceInfo
.DeviceType
!= FILE_DEVICE_VIRTUAL_DISK
)
2087 if (FsDeviceInfo
.Characteristics
!= (FILE_REMOTE_DEVICE
| FILE_REMOVABLE_MEDIA
))
2093 Status
= ObReferenceObjectByHandle(Handle
, 0, *IoFileObjectType
, KernelMode
, (PVOID
*)&FileObject
, NULL
);
2094 if (!NT_SUCCESS(Status
))
2098 FOReferenced
= TRUE
;
2101 FileNameInfo
= AllocatePool(sizeof(FILE_NAME_INFORMATION
));
2104 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2108 Status
= ZwQueryInformationFile(Handle
, &IoStatusBlock
, FileNameInfo
,
2109 sizeof(FILE_NAME_INFORMATION
),
2110 FileNameInformation
);
2111 if (Status
== STATUS_BUFFER_OVERFLOW
)
2113 /* Now we have real length, use it */
2114 Length
= FileNameInfo
->FileNameLength
;
2115 FreePool(FileNameInfo
);
2117 FileNameInfo
= AllocatePool(sizeof(FILE_NAME_INFORMATION
) + Length
);
2120 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2124 /* Really query file name */
2125 Status
= ZwQueryInformationFile(Handle
, &IoStatusBlock
, FileNameInfo
,
2126 sizeof(FILE_NAME_INFORMATION
) + Length
,
2127 FileNameInformation
);
2130 if (!NT_SUCCESS(Status
))
2135 /* Get symbolic name */
2136 ObjectNameInfoPtr
= &ObjectNameInfo
;
2137 SavedLength
= sizeof(OBJECT_NAME_INFORMATION
);
2138 Status
= ObQueryNameString(FileObject
->DeviceObject
, ObjectNameInfoPtr
, sizeof(OBJECT_NAME_INFORMATION
), &Length
);
2139 if (Status
== STATUS_INFO_LENGTH_MISMATCH
)
2141 /* Once again, with proper size, it works better */
2142 ObjectNameInfoPtr
= AllocatePool(Length
);
2143 if (!ObjectNameInfoPtr
)
2145 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2149 SavedLength
= Length
;
2150 Status
= ObQueryNameString(FileObject
->DeviceObject
, ObjectNameInfoPtr
, SavedLength
, &Length
);
2153 if (!NT_SUCCESS(Status
))
2158 /* Now, query the device name */
2159 Status
= QueryDeviceInformation(&ObjectNameInfoPtr
->Name
, SourceDeviceName
,
2160 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
2161 if (!NT_SUCCESS(Status
))
2166 /* For target volume name, use input */
2167 TargetVolumeName
->Length
=
2168 TargetVolumeName
->MaximumLength
= VolumeMountPoint
->TargetVolumeNameLength
;
2169 TargetVolumeName
->Buffer
= (PWSTR
)((ULONG_PTR
)VolumeMountPoint
+ VolumeMountPoint
->TargetVolumeNameOffset
);
2171 /* Query its device name */
2172 Status
= QueryDeviceInformation(TargetVolumeName
, &TargetDeviceName
,
2173 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
2174 if (!NT_SUCCESS(Status
))
2179 /* Return symbolic name */
2180 SourceSymbolicName
->Length
=
2181 SourceSymbolicName
->MaximumLength
= (USHORT
)FileNameInfo
->FileNameLength
;
2182 SourceSymbolicName
->Buffer
= (PWSTR
)FileNameInfo
;
2183 /* memmove allows memory overlap */
2184 RtlMoveMemory(SourceSymbolicName
->Buffer
, FileNameInfo
->FileName
, SourceSymbolicName
->Length
);
2185 FileNameInfo
= NULL
;
2187 /* Notify the change */
2188 MountMgrNotify(DeviceExtension
);
2189 MountMgrNotifyNameChange(DeviceExtension
, &TargetDeviceName
, TRUE
);
2191 /* If we are locked, sync databases if possible */
2192 if (NT_SUCCESS(LockStatus
))
2194 Status
= FindDeviceInfo(DeviceExtension
, SourceDeviceName
, FALSE
, &DeviceInformation
);
2195 if (NT_SUCCESS(Status
))
2197 ReconcileThisDatabaseWithMaster(DeviceExtension
, DeviceInformation
);
2201 Status
= STATUS_PENDING
;
2206 if (TargetDeviceName
.Buffer
)
2208 FreePool(TargetDeviceName
.Buffer
);
2211 if (ObjectNameInfoPtr
&& ObjectNameInfoPtr
!= &ObjectNameInfo
)
2213 FreePool(ObjectNameInfoPtr
);
2218 FreePool(FileNameInfo
);
2223 ObDereferenceObject(FileObject
);
2233 MountMgrVolumeMountPointCreated(IN PDEVICE_EXTENSION DeviceExtension
,
2235 IN NTSTATUS LockStatus
)
2240 HANDLE RemoteDatabase
;
2241 PMOUNTDEV_UNIQUE_ID UniqueId
;
2242 PDATABASE_ENTRY DatabaseEntry
;
2243 PASSOCIATED_DEVICE_ENTRY AssociatedEntry
;
2244 PDEVICE_INFORMATION DeviceInformation
, TargetDeviceInformation
;
2245 UNICODE_STRING LinkTarget
, SourceDeviceName
, SourceSymbolicName
, TargetVolumeName
, VolumeName
, DbName
;
2247 /* Initialize string */
2248 LinkTarget
.Length
= 0;
2249 LinkTarget
.MaximumLength
= 0xC8;
2250 LinkTarget
.Buffer
= AllocatePool(LinkTarget
.MaximumLength
);
2251 if (LinkTarget
.Buffer
== NULL
)
2253 return STATUS_INSUFFICIENT_RESOURCES
;
2256 /* If the mount point was created, then, it changed!
2257 * Also use it to query some information
2259 Status
= MountMgrVolumeMountPointChanged(DeviceExtension
, Irp
, LockStatus
, &SourceDeviceName
, &SourceSymbolicName
, &TargetVolumeName
);
2260 /* Pending means DB are under synchronization, bail out */
2261 if (Status
== STATUS_PENDING
)
2263 FreePool(LinkTarget
.Buffer
);
2264 FreePool(SourceDeviceName
.Buffer
);
2265 FreePool(SourceSymbolicName
.Buffer
);
2266 return STATUS_SUCCESS
;
2268 else if (!NT_SUCCESS(Status
))
2270 FreePool(LinkTarget
.Buffer
);
2274 /* Query the device information */
2275 Status
= FindDeviceInfo(DeviceExtension
, &SourceDeviceName
, FALSE
, &DeviceInformation
);
2276 if (!NT_SUCCESS(Status
))
2278 /* If it failed, first try to get volume name */
2279 Status
= QueryVolumeName(0, NULL
, &SourceDeviceName
, &LinkTarget
, &VolumeName
);
2280 if (!NT_SUCCESS(Status
))
2282 /* Then, try to read the symlink */
2283 Status
= MountMgrQuerySymbolicLink(&SourceDeviceName
, &LinkTarget
);
2284 if (!NT_SUCCESS(Status
))
2286 FreePool(LinkTarget
.Buffer
);
2287 FreePool(SourceDeviceName
.Buffer
);
2288 FreePool(SourceSymbolicName
.Buffer
);
2294 FreePool(VolumeName
.Buffer
);
2297 FreePool(SourceDeviceName
.Buffer
);
2299 SourceDeviceName
.Length
= LinkTarget
.Length
;
2300 SourceDeviceName
.MaximumLength
= LinkTarget
.MaximumLength
;
2301 SourceDeviceName
.Buffer
= LinkTarget
.Buffer
;
2303 /* Now that we have the correct source, reattempt to query information */
2304 Status
= FindDeviceInfo(DeviceExtension
, &SourceDeviceName
, FALSE
, &DeviceInformation
);
2305 if (!NT_SUCCESS(Status
))
2307 FreePool(SourceDeviceName
.Buffer
);
2308 FreePool(SourceSymbolicName
.Buffer
);
2313 FreePool(SourceDeviceName
.Buffer
);
2315 /* Get information about target device */
2316 Status
= FindDeviceInfo(DeviceExtension
, &TargetVolumeName
, FALSE
, &TargetDeviceInformation
);
2317 if (!NT_SUCCESS(Status
))
2319 FreePool(SourceSymbolicName
.Buffer
);
2323 /* Notify if not disabled */
2324 if (!TargetDeviceInformation
->SkipNotifications
)
2326 PostOnlineNotification(DeviceExtension
, &TargetDeviceInformation
->SymbolicName
);
2329 /* Open the remote database */
2330 RemoteDatabase
= OpenRemoteDatabase(DeviceInformation
, TRUE
);
2331 if (RemoteDatabase
== 0)
2333 FreePool(SourceSymbolicName
.Buffer
);
2334 return STATUS_INSUFFICIENT_RESOURCES
;
2337 /* Browse all the entries */
2342 DatabaseEntry
= GetRemoteDatabaseEntry(RemoteDatabase
, Offset
);
2343 if (DatabaseEntry
== NULL
)
2348 /* Try to find ourselves */
2349 DbName
.MaximumLength
= DatabaseEntry
->SymbolicNameLength
;
2350 DbName
.Length
= DbName
.MaximumLength
;
2351 DbName
.Buffer
= (PWSTR
)((ULONG_PTR
)DatabaseEntry
+ DatabaseEntry
->SymbolicNameOffset
);
2352 if (RtlEqualUnicodeString(&TargetVolumeName
, &DbName
, TRUE
))
2354 /* Reference ourselves and update the entry */
2355 ++DatabaseEntry
->EntryReferences
;
2356 Status
= WriteRemoteDatabaseEntry(RemoteDatabase
, Offset
, DatabaseEntry
);
2357 FreePool(DatabaseEntry
);
2362 Offset
+= DatabaseEntry
->EntrySize
;
2363 FreePool(DatabaseEntry
);
2366 /* We couldn't find ourselves, we'll have to add ourselves */
2370 PUNIQUE_ID_REPLICATE UniqueIdReplicate
;
2372 /* Query the device unique ID */
2373 Status
= QueryDeviceInformation(&TargetVolumeName
, NULL
, &UniqueId
, NULL
, NULL
, NULL
, NULL
, NULL
);
2374 if (!NT_SUCCESS(Status
))
2376 FreePool(SourceSymbolicName
.Buffer
);
2377 CloseRemoteDatabase(RemoteDatabase
);
2381 /* Allocate a database entry */
2382 EntrySize
= UniqueId
->UniqueIdLength
+ TargetVolumeName
.Length
+ sizeof(DATABASE_ENTRY
);
2383 DatabaseEntry
= AllocatePool(EntrySize
);
2384 if (DatabaseEntry
== NULL
)
2387 FreePool(SourceSymbolicName
.Buffer
);
2388 CloseRemoteDatabase(RemoteDatabase
);
2389 return STATUS_INSUFFICIENT_RESOURCES
;
2393 DatabaseEntry
->EntrySize
= EntrySize
;
2394 DatabaseEntry
->EntryReferences
= 1;
2395 DatabaseEntry
->SymbolicNameOffset
= sizeof(DATABASE_ENTRY
);
2396 DatabaseEntry
->SymbolicNameLength
= TargetVolumeName
.Length
;
2397 DatabaseEntry
->UniqueIdOffset
= TargetVolumeName
.Length
+ sizeof(DATABASE_ENTRY
);
2398 DatabaseEntry
->UniqueIdLength
= UniqueId
->UniqueIdLength
;
2399 RtlCopyMemory((PVOID
)((ULONG_PTR
)DatabaseEntry
+ sizeof(DATABASE_ENTRY
)), TargetVolumeName
.Buffer
, DatabaseEntry
->SymbolicNameLength
);
2400 RtlCopyMemory((PVOID
)((ULONG_PTR
)DatabaseEntry
+ DatabaseEntry
->UniqueIdOffset
), UniqueId
->UniqueId
, UniqueId
->UniqueIdLength
);
2402 /* And write it down */
2403 Status
= AddRemoteDatabaseEntry(RemoteDatabase
, DatabaseEntry
);
2404 FreePool(DatabaseEntry
);
2405 if (!NT_SUCCESS(Status
))
2408 FreePool(SourceSymbolicName
.Buffer
);
2409 CloseRemoteDatabase(RemoteDatabase
);
2413 /* And now, allocate an Unique ID item */
2414 UniqueIdReplicate
= AllocatePool(sizeof(UNIQUE_ID_REPLICATE
));
2415 if (UniqueIdReplicate
== NULL
)
2418 FreePool(SourceSymbolicName
.Buffer
);
2419 CloseRemoteDatabase(RemoteDatabase
);
2423 /* To associate it with the device */
2424 UniqueIdReplicate
->UniqueId
= UniqueId
;
2425 InsertTailList(&DeviceInformation
->ReplicatedUniqueIdsListHead
, &UniqueIdReplicate
->ReplicatedUniqueIdsListEntry
);
2428 /* We're done with the remote database */
2429 CloseRemoteDatabase(RemoteDatabase
);
2431 /* Check we were find writing the entry */
2432 if (!NT_SUCCESS(Status
))
2434 FreePool(SourceSymbolicName
.Buffer
);
2438 /* This is the end, allocate an associated entry */
2439 AssociatedEntry
= AllocatePool(sizeof(ASSOCIATED_DEVICE_ENTRY
));
2440 if (AssociatedEntry
== NULL
)
2442 FreePool(SourceSymbolicName
.Buffer
);
2443 return STATUS_INSUFFICIENT_RESOURCES
;
2446 /* Initialize its source name string */
2447 AssociatedEntry
->String
.Length
= SourceSymbolicName
.Length
;
2448 AssociatedEntry
->String
.MaximumLength
= AssociatedEntry
->String
.Length
+ sizeof(UNICODE_NULL
);
2449 AssociatedEntry
->String
.Buffer
= AllocatePool(AssociatedEntry
->String
.MaximumLength
);
2450 if (AssociatedEntry
->String
.Buffer
== NULL
)
2452 FreePool(AssociatedEntry
);
2453 FreePool(SourceSymbolicName
.Buffer
);
2454 return STATUS_INSUFFICIENT_RESOURCES
;
2457 /* Copy data & insert in list */
2458 RtlCopyMemory(AssociatedEntry
->String
.Buffer
, SourceSymbolicName
.Buffer
, SourceSymbolicName
.Length
);
2459 AssociatedEntry
->String
.Buffer
[SourceSymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2460 AssociatedEntry
->DeviceInformation
= DeviceInformation
;
2461 InsertTailList(&TargetDeviceInformation
->AssociatedDevicesHead
, &AssociatedEntry
->AssociatedDevicesEntry
);
2464 FreePool(SourceSymbolicName
.Buffer
);
2465 return STATUS_SUCCESS
;
2472 MountMgrVolumeMountPointDeleted(IN PDEVICE_EXTENSION DeviceExtension
,
2474 IN NTSTATUS LockStatus
)
2479 HANDLE RemoteDatabase
;
2480 PDATABASE_ENTRY DatabaseEntry
;
2481 PUNIQUE_ID_REPLICATE UniqueIdReplicate
;
2482 PASSOCIATED_DEVICE_ENTRY AssociatedEntry
;
2483 PDEVICE_INFORMATION DeviceInformation
, TargetDeviceInformation
;
2484 UNICODE_STRING LinkTarget
, SourceDeviceName
, SourceSymbolicName
, TargetVolumeName
, VolumeName
, DbName
;
2486 /* Initialize string */
2487 LinkTarget
.Length
= 0;
2488 LinkTarget
.MaximumLength
= 0xC8;
2489 LinkTarget
.Buffer
= AllocatePool(LinkTarget
.MaximumLength
);
2490 if (LinkTarget
.Buffer
== NULL
)
2492 return STATUS_INSUFFICIENT_RESOURCES
;
2495 /* If the mount point was deleted, then, it changed!
2496 * Also use it to query some information
2498 Status
= MountMgrVolumeMountPointChanged(DeviceExtension
, Irp
, LockStatus
, &SourceDeviceName
, &SourceSymbolicName
, &TargetVolumeName
);
2499 /* Pending means DB are under synchronization, bail out */
2500 if (Status
== STATUS_PENDING
)
2502 FreePool(LinkTarget
.Buffer
);
2503 FreePool(SourceDeviceName
.Buffer
);
2504 FreePool(SourceSymbolicName
.Buffer
);
2505 return STATUS_SUCCESS
;
2507 else if (!NT_SUCCESS(Status
))
2509 FreePool(LinkTarget
.Buffer
);
2513 /* Query the device information */
2514 Status
= FindDeviceInfo(DeviceExtension
, &SourceDeviceName
, FALSE
, &DeviceInformation
);
2515 if (!NT_SUCCESS(Status
))
2517 /* If it failed, first try to get volume name */
2518 Status
= QueryVolumeName(0, NULL
, &SourceDeviceName
, &LinkTarget
, &VolumeName
);
2519 if (!NT_SUCCESS(Status
))
2521 /* Then, try to read the symlink */
2522 Status
= MountMgrQuerySymbolicLink(&SourceDeviceName
, &LinkTarget
);
2523 if (!NT_SUCCESS(Status
))
2525 FreePool(LinkTarget
.Buffer
);
2526 FreePool(SourceDeviceName
.Buffer
);
2527 FreePool(SourceSymbolicName
.Buffer
);
2533 FreePool(VolumeName
.Buffer
);
2536 FreePool(SourceDeviceName
.Buffer
);
2538 SourceDeviceName
.Length
= LinkTarget
.Length
;
2539 SourceDeviceName
.MaximumLength
= LinkTarget
.MaximumLength
;
2540 SourceDeviceName
.Buffer
= LinkTarget
.Buffer
;
2542 /* Now that we have the correct source, reattempt to query information */
2543 Status
= FindDeviceInfo(DeviceExtension
, &SourceDeviceName
, FALSE
, &DeviceInformation
);
2544 if (!NT_SUCCESS(Status
))
2546 FreePool(SourceDeviceName
.Buffer
);
2547 FreePool(SourceSymbolicName
.Buffer
);
2552 FreePool(SourceDeviceName
.Buffer
);
2554 /* Get information about target device */
2555 Status
= FindDeviceInfo(DeviceExtension
, &TargetVolumeName
, FALSE
, &TargetDeviceInformation
);
2556 if (!NT_SUCCESS(Status
))
2558 FreePool(SourceSymbolicName
.Buffer
);
2562 /* Open the remote database */
2563 RemoteDatabase
= OpenRemoteDatabase(DeviceInformation
, TRUE
);
2564 if (RemoteDatabase
== 0)
2566 FreePool(SourceSymbolicName
.Buffer
);
2567 return STATUS_INSUFFICIENT_RESOURCES
;
2570 /* Browse all the entries */
2574 DatabaseEntry
= GetRemoteDatabaseEntry(RemoteDatabase
, Offset
);
2575 if (DatabaseEntry
== NULL
)
2577 /* We didn't find ourselves, that's infortunate! */
2578 FreePool(SourceSymbolicName
.Buffer
);
2579 CloseRemoteDatabase(RemoteDatabase
);
2580 return STATUS_INVALID_PARAMETER
;
2583 /* Try to find ourselves */
2584 DbName
.MaximumLength
= DatabaseEntry
->SymbolicNameLength
;
2585 DbName
.Length
= DbName
.MaximumLength
;
2586 DbName
.Buffer
= (PWSTR
)((ULONG_PTR
)DatabaseEntry
+ DatabaseEntry
->SymbolicNameOffset
);
2587 if (RtlEqualUnicodeString(&TargetVolumeName
, &DbName
, TRUE
))
2592 Offset
+= DatabaseEntry
->EntrySize
;
2593 FreePool(DatabaseEntry
);
2596 /* Dereference ourselves */
2597 DatabaseEntry
->EntryReferences
--;
2598 if (DatabaseEntry
->EntryReferences
== 0)
2600 /* If we're still referenced, just update the entry */
2601 Status
= WriteRemoteDatabaseEntry(RemoteDatabase
, Offset
, DatabaseEntry
);
2605 /* Otherwise, delete the entry */
2606 Status
= DeleteRemoteDatabaseEntry(RemoteDatabase
, Offset
);
2607 if (!NT_SUCCESS(Status
))
2609 FreePool(DatabaseEntry
);
2610 FreePool(SourceSymbolicName
.Buffer
);
2611 CloseRemoteDatabase(RemoteDatabase
);
2615 /* Also, delete our unique ID replicated record */
2616 for (Entry
= DeviceInformation
->ReplicatedUniqueIdsListHead
.Flink
;
2617 Entry
!= &DeviceInformation
->ReplicatedUniqueIdsListHead
;
2618 Entry
= Entry
->Flink
)
2620 UniqueIdReplicate
= CONTAINING_RECORD(Entry
, UNIQUE_ID_REPLICATE
, ReplicatedUniqueIdsListEntry
);
2622 if (UniqueIdReplicate
->UniqueId
->UniqueIdLength
== DatabaseEntry
->UniqueIdLength
&&
2623 RtlCompareMemory(UniqueIdReplicate
->UniqueId
->UniqueId
,
2624 (PVOID
)((ULONG_PTR
)DatabaseEntry
+ DatabaseEntry
->UniqueIdOffset
),
2625 DatabaseEntry
->UniqueIdLength
) == DatabaseEntry
->UniqueIdLength
)
2631 /* It has to exist! */
2632 if (Entry
== &DeviceInformation
->ReplicatedUniqueIdsListHead
)
2634 FreePool(DatabaseEntry
);
2635 FreePool(SourceSymbolicName
.Buffer
);
2636 CloseRemoteDatabase(RemoteDatabase
);
2637 return STATUS_UNSUCCESSFUL
;
2640 /* Remove it and free it */
2641 RemoveEntryList(&UniqueIdReplicate
->ReplicatedUniqueIdsListEntry
);
2642 FreePool(UniqueIdReplicate
->UniqueId
);
2643 FreePool(UniqueIdReplicate
);
2646 /* We're done with the remote database */
2647 FreePool(DatabaseEntry
);
2648 CloseRemoteDatabase(RemoteDatabase
);
2650 /* Check write operation succeed */
2651 if (!NT_SUCCESS(Status
))
2653 FreePool(SourceSymbolicName
.Buffer
);
2657 /* Try to find our associated device entry */
2658 for (Entry
= TargetDeviceInformation
->AssociatedDevicesHead
.Flink
;
2659 Entry
!= &TargetDeviceInformation
->AssociatedDevicesHead
;
2660 Entry
= Entry
->Flink
)
2662 AssociatedEntry
= CONTAINING_RECORD(Entry
, ASSOCIATED_DEVICE_ENTRY
, AssociatedDevicesEntry
);
2664 /* If found, delete it */
2665 if (AssociatedEntry
->DeviceInformation
== DeviceInformation
&&
2666 RtlEqualUnicodeString(&AssociatedEntry
->String
, &SourceSymbolicName
, TRUE
))
2668 RemoveEntryList(&AssociatedEntry
->AssociatedDevicesEntry
);
2669 FreePool(AssociatedEntry
->String
.Buffer
);
2670 FreePool(AssociatedEntry
);
2676 FreePool(SourceSymbolicName
.Buffer
);
2677 return STATUS_SUCCESS
;
2685 MountMgrDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
2688 PIO_STACK_LOCATION Stack
;
2689 NTSTATUS Status
, LockStatus
;
2690 PDEVICE_EXTENSION DeviceExtension
;
2692 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2693 DeviceExtension
= DeviceObject
->DeviceExtension
;
2695 KeWaitForSingleObject(&(DeviceExtension
->DeviceLock
), Executive
, KernelMode
, FALSE
, NULL
);
2697 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
2699 case IOCTL_MOUNTMGR_CREATE_POINT
:
2700 Status
= MountMgrCreatePoint(DeviceExtension
, Irp
);
2703 case IOCTL_MOUNTMGR_DELETE_POINTS
:
2704 Status
= MountMgrDeletePoints(DeviceExtension
, Irp
);
2707 case IOCTL_MOUNTMGR_QUERY_POINTS
:
2708 Status
= MountMgrQueryPoints(DeviceExtension
, Irp
);
2711 case IOCTL_MOUNTMGR_DELETE_POINTS_DBONLY
:
2712 Status
= MountMgrDeletePointsDbOnly(DeviceExtension
, Irp
);
2715 case IOCTL_MOUNTMGR_NEXT_DRIVE_LETTER
:
2716 Status
= MountMgrNextDriveLetter(DeviceExtension
, Irp
);
2719 case IOCTL_MOUNTMGR_AUTO_DL_ASSIGNMENTS
:
2720 DeviceExtension
->AutomaticDriveLetter
= TRUE
;
2721 Status
= STATUS_SUCCESS
;
2723 MountMgrAssignDriveLetters(DeviceExtension
);
2724 ReconcileAllDatabasesWithMaster(DeviceExtension
);
2725 WaitForOnlinesToComplete(DeviceExtension
);
2728 case IOCTL_MOUNTMGR_VOLUME_MOUNT_POINT_CREATED
:
2729 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
2731 LockStatus
= WaitForRemoteDatabaseSemaphore(DeviceExtension
);
2732 KeWaitForSingleObject(&(DeviceExtension
->DeviceLock
), Executive
, KernelMode
, FALSE
, NULL
);
2733 Status
= MountMgrVolumeMountPointCreated(DeviceExtension
, Irp
, LockStatus
);
2734 if (NT_SUCCESS(LockStatus
))
2736 ReleaseRemoteDatabaseSemaphore(DeviceExtension
);
2741 case IOCTL_MOUNTMGR_VOLUME_MOUNT_POINT_DELETED
:
2742 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
2744 LockStatus
= WaitForRemoteDatabaseSemaphore(DeviceExtension
);
2745 KeWaitForSingleObject(&(DeviceExtension
->DeviceLock
), Executive
, KernelMode
, FALSE
, NULL
);
2746 Status
= MountMgrVolumeMountPointDeleted(DeviceExtension
, Irp
, LockStatus
);
2747 if (NT_SUCCESS(LockStatus
))
2749 ReleaseRemoteDatabaseSemaphore(DeviceExtension
);
2754 case IOCTL_MOUNTMGR_CHANGE_NOTIFY
:
2755 Status
= MountMgrChangeNotify(DeviceExtension
, Irp
);
2758 case IOCTL_MOUNTMGR_KEEP_LINKS_WHEN_OFFLINE
:
2759 Status
= MountMgrKeepLinksWhenOffline(DeviceExtension
, Irp
);
2762 case IOCTL_MOUNTMGR_CHECK_UNPROCESSED_VOLUMES
:
2763 Status
= MountMgrCheckUnprocessedVolumes(DeviceExtension
, Irp
);
2766 case IOCTL_MOUNTMGR_VOLUME_ARRIVAL_NOTIFICATION
:
2767 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
2768 Status
= MountMgrVolumeArrivalNotification(DeviceExtension
, Irp
);
2771 case IOCTL_MOUNTMGR_QUERY_DOS_VOLUME_PATH
:
2772 Status
= MountMgrQueryDosVolumePath(DeviceExtension
, Irp
);
2775 case IOCTL_MOUNTMGR_QUERY_DOS_VOLUME_PATHS
:
2776 Status
= MountMgrQueryDosVolumePaths(DeviceExtension
, Irp
);
2779 case IOCTL_MOUNTMGR_SCRUB_REGISTRY
:
2780 Status
= MountMgrScrubRegistry(DeviceExtension
);
2783 case IOCTL_MOUNTMGR_QUERY_AUTO_MOUNT
:
2784 Status
= MountMgrQueryAutoMount(DeviceExtension
, Irp
);
2787 case IOCTL_MOUNTMGR_SET_AUTO_MOUNT
:
2788 Status
= MountMgrSetAutoMount(DeviceExtension
, Irp
);
2791 case IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE
:
2792 case IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE
:
2793 DPRINT1("Winism! Rewrite the caller!\n");
2795 Status
= STATUS_INVALID_DEVICE_REQUEST
;
2798 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
2800 if (Status
!= STATUS_PENDING
)
2808 Irp
->IoStatus
.Status
= Status
;
2809 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);