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)
26 /* INCLUDES *****************************************************************/
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
->Volume
);
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 automonted 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 */
558 if (RtlPrefixUnicodeString(&DeviceFloppy
, &TargetDeviceName
, TRUE
))
564 DriveLetter
= 'C' + RtlPrefixUnicodeString(&DeviceCdRom
, &TargetDeviceName
, TRUE
);
567 /* We cannot set NO drive letter */
568 ASSERT(DeviceInformation
->SuggestedDriveLetter
!= (UCHAR
)-1);
570 /* If we don't have suggested letter but it's a FT volume, fail */
571 if (!DeviceInformation
->SuggestedDriveLetter
&& IsFtVolume(&(DeviceInformation
->DeviceName
)))
573 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
574 DriveLetterInfo
->CurrentDriveLetter
= 0;
580 RtlCopyMemory(NameBuffer
, DosDevices
.Buffer
, DosDevices
.Length
);
581 NameBuffer
[COLON_POSITION
] = L
':';
582 SymbolicName
.Buffer
= NameBuffer
;
583 SymbolicName
.Length
=
584 SymbolicName
.MaximumLength
= DRIVE_LETTER_LENGTH
;
586 /* It's all prepared, create mount point */
587 if (DeviceInformation
->SuggestedDriveLetter
)
589 DriveLetterInfo
->CurrentDriveLetter
= DeviceInformation
->SuggestedDriveLetter
;
590 NameBuffer
[LETTER_POSITION
] = DeviceInformation
->SuggestedDriveLetter
;
592 Status
= MountMgrCreatePointWorker(DeviceExtension
, &SymbolicName
, &TargetDeviceName
);
593 if (NT_SUCCESS(Status
))
599 /* It failed with this letter... Try another one! */
600 for (DriveLetterInfo
->CurrentDriveLetter
= DriveLetter
;
601 DriveLetterInfo
->CurrentDriveLetter
<= L
'Z';
602 DriveLetterInfo
->CurrentDriveLetter
++)
604 NameBuffer
[LETTER_POSITION
] = DeviceInformation
->SuggestedDriveLetter
;
606 Status
= MountMgrCreatePointWorker(DeviceExtension
, &SymbolicName
, &TargetDeviceName
);
607 if (NT_SUCCESS(Status
))
613 /* We failed setting a letter */
614 if (DriveLetterInfo
->CurrentDriveLetter
> L
'Z')
616 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
617 DriveLetterInfo
->CurrentDriveLetter
= 0;
619 /* Try at least to add a no drive letter entry */
620 Status
= QueryDeviceInformation(&TargetDeviceName
, NULL
, &UniqueId
, NULL
, NULL
, NULL
, NULL
, NULL
);
621 if (NT_SUCCESS(Status
))
623 CreateNoDriveLetterEntry(UniqueId
);
629 FreePool(TargetDeviceName
.Buffer
);
631 return STATUS_SUCCESS
;
639 MountMgrNextDriveLetter(IN PDEVICE_EXTENSION DeviceExtension
,
643 PIO_STACK_LOCATION Stack
;
644 UNICODE_STRING DeviceName
;
645 PMOUNTMGR_DRIVE_LETTER_TARGET DriveLetterTarget
;
646 MOUNTMGR_DRIVE_LETTER_INFORMATION DriveLetterInformation
;
648 Stack
= IoGetNextIrpStackLocation(Irp
);
651 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_DRIVE_LETTER_TARGET
) ||
652 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(MOUNTMGR_DRIVE_LETTER_INFORMATION
))
654 return STATUS_INVALID_PARAMETER
;
657 DriveLetterTarget
= (PMOUNTMGR_DRIVE_LETTER_TARGET
)Irp
->AssociatedIrp
.SystemBuffer
;
658 if (DriveLetterTarget
->DeviceNameLength
+ sizeof(USHORT
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
660 return STATUS_INVALID_PARAMETER
;
663 /* Call the worker */
664 DeviceName
.Buffer
= DriveLetterTarget
->DeviceName
;
666 DeviceName
.MaximumLength
= DriveLetterTarget
->DeviceNameLength
;
668 Status
= MountMgrNextDriveLetterWorker(DeviceExtension
, &DeviceName
,
669 &DriveLetterInformation
);
670 if (NT_SUCCESS(Status
))
672 *(PMOUNTMGR_DRIVE_LETTER_INFORMATION
)Irp
->AssociatedIrp
.SystemBuffer
=
673 DriveLetterInformation
;
674 Irp
->IoStatus
.Information
= sizeof(MOUNTMGR_DRIVE_LETTER_INFORMATION
);
685 MountMgrQuerySystemVolumeNameQueryRoutine(IN PWSTR ValueName
,
688 IN ULONG ValueLength
,
690 IN PVOID EntryContext
)
692 UNICODE_STRING ValueString
;
693 PUNICODE_STRING SystemVolumeName
;
695 UNREFERENCED_PARAMETER(ValueName
);
696 UNREFERENCED_PARAMETER(ValueLength
);
697 UNREFERENCED_PARAMETER(EntryContext
);
699 if (ValueType
!= REG_SZ
)
701 return STATUS_SUCCESS
;
704 RtlInitUnicodeString(&ValueString
, ValueData
);
705 SystemVolumeName
= Context
;
707 /* Return a string containing system volume name */
708 SystemVolumeName
->Length
= ValueString
.Length
;
709 SystemVolumeName
->MaximumLength
= ValueString
.Length
+ sizeof(WCHAR
);
710 SystemVolumeName
->Buffer
= AllocatePool(SystemVolumeName
->MaximumLength
);
711 if (SystemVolumeName
->Buffer
)
713 RtlCopyMemory(SystemVolumeName
->Buffer
, ValueData
, ValueString
.Length
);
714 SystemVolumeName
->Buffer
[ValueString
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
717 return STATUS_SUCCESS
;
725 MountMgrQuerySystemVolumeName(OUT PUNICODE_STRING SystemVolumeName
)
727 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
729 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
730 QueryTable
[0].QueryRoutine
= MountMgrQuerySystemVolumeNameQueryRoutine
;
731 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_REQUIRED
;
732 QueryTable
[0].Name
= L
"SystemPartition";
734 SystemVolumeName
->Buffer
= NULL
;
736 RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
737 L
"\\Registry\\Machine\\System\\Setup",
742 if (SystemVolumeName
->Buffer
)
744 return STATUS_SUCCESS
;
747 return STATUS_UNSUCCESSFUL
;
754 MountMgrAssignDriveLetters(IN PDEVICE_EXTENSION DeviceExtension
)
757 PLIST_ENTRY NextEntry
;
758 UNICODE_STRING SystemVolumeName
;
759 PDEVICE_INFORMATION DeviceInformation
;
760 MOUNTMGR_DRIVE_LETTER_INFORMATION DriveLetterInformation
;
762 /* First, get system volume name */
763 Status
= MountMgrQuerySystemVolumeName(&SystemVolumeName
);
765 /* If there are no device, it's all done */
766 if (IsListEmpty(&(DeviceExtension
->DeviceListHead
)))
768 if (NT_SUCCESS(Status
))
770 FreePool(SystemVolumeName
.Buffer
);
776 /* Now, for all the devices... */
777 for (NextEntry
= DeviceExtension
->DeviceListHead
.Flink
;
778 NextEntry
!= &(DeviceExtension
->DeviceListHead
);
779 NextEntry
= NextEntry
->Flink
)
781 DeviceInformation
= CONTAINING_RECORD(NextEntry
, DEVICE_INFORMATION
, DeviceListEntry
);
783 /* If the device doesn't have a letter assigned, do it! */
784 if (!DeviceInformation
->LetterAssigned
)
786 MountMgrNextDriveLetterWorker(DeviceExtension
,
787 &(DeviceInformation
->DeviceName
),
788 &DriveLetterInformation
);
791 /* If it was the system volume */
792 if (NT_SUCCESS(Status
) && RtlEqualUnicodeString(&SystemVolumeName
, &(DeviceInformation
->DeviceName
), TRUE
))
794 /* Keep track of it */
795 DeviceExtension
->DriveLetterData
= AllocatePool(DeviceInformation
->UniqueId
->UniqueIdLength
+
796 sizeof(MOUNTDEV_UNIQUE_ID
));
797 if (DeviceExtension
->DriveLetterData
)
799 RtlCopyMemory(DeviceExtension
->DriveLetterData
,
800 DeviceInformation
->UniqueId
,
801 DeviceInformation
->UniqueId
->UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
804 /* If it was not automount, ensure it gets mounted */
805 if (!DeviceExtension
->NoAutoMount
)
807 DeviceExtension
->NoAutoMount
= TRUE
;
809 MountMgrNextDriveLetterWorker(DeviceExtension
,
810 &(DeviceInformation
->DeviceName
),
811 &DriveLetterInformation
);
813 DeviceExtension
->NoAutoMount
= FALSE
;
818 if (NT_SUCCESS(Status
))
820 FreePool(SystemVolumeName
.Buffer
);
825 MountMgrQueryDosVolumePath(IN PDEVICE_EXTENSION DeviceExtension
,
828 UNREFERENCED_PARAMETER(DeviceExtension
);
829 UNREFERENCED_PARAMETER(Irp
);
830 return STATUS_NOT_IMPLEMENTED
;
834 MountMgrQueryDosVolumePaths(IN PDEVICE_EXTENSION DeviceExtension
,
837 UNREFERENCED_PARAMETER(DeviceExtension
);
838 UNREFERENCED_PARAMETER(Irp
);
839 return STATUS_NOT_IMPLEMENTED
;
846 MountMgrKeepLinksWhenOffline(IN PDEVICE_EXTENSION DeviceExtension
,
850 PIO_STACK_LOCATION Stack
;
851 UNICODE_STRING SymbolicName
;
852 PMOUNTMGR_TARGET_NAME Target
;
853 PDEVICE_INFORMATION DeviceInformation
;
855 Stack
= IoGetNextIrpStackLocation(Irp
);
858 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_TARGET_NAME
))
860 return STATUS_INVALID_PARAMETER
;
863 Target
= (PMOUNTMGR_TARGET_NAME
)Irp
->AssociatedIrp
.SystemBuffer
;
864 if (Target
->DeviceNameLength
+ sizeof(USHORT
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
866 return STATUS_INVALID_PARAMETER
;
869 SymbolicName
.Length
=
870 SymbolicName
.MaximumLength
= Target
->DeviceNameLength
;
871 SymbolicName
.Buffer
= Target
->DeviceName
;
873 /* Find the associated device */
874 Status
= FindDeviceInfo(DeviceExtension
, &SymbolicName
, FALSE
, &DeviceInformation
);
875 if (!NT_SUCCESS(Status
))
880 /* Mark we want to keep links */
881 DeviceInformation
->KeepLinks
= TRUE
;
883 return STATUS_SUCCESS
;
890 MountMgrVolumeArrivalNotification(IN PDEVICE_EXTENSION DeviceExtension
,
895 PIO_STACK_LOCATION Stack
;
896 UNICODE_STRING SymbolicName
;
897 PMOUNTMGR_TARGET_NAME Target
;
899 Stack
= IoGetNextIrpStackLocation(Irp
);
902 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_TARGET_NAME
))
904 return STATUS_INVALID_PARAMETER
;
907 Target
= (PMOUNTMGR_TARGET_NAME
)Irp
->AssociatedIrp
.SystemBuffer
;
908 if (Target
->DeviceNameLength
+ sizeof(USHORT
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
910 return STATUS_INVALID_PARAMETER
;
913 SymbolicName
.Length
=
914 SymbolicName
.MaximumLength
= Target
->DeviceNameLength
;
915 SymbolicName
.Buffer
= Target
->DeviceName
;
917 /* Disable hard errors */
918 OldState
= PsGetThreadHardErrorsAreDisabled(PsGetCurrentThread());
919 PsSetThreadHardErrorsAreDisabled(PsGetCurrentThread(), TRUE
);
921 /* Call real worker */
922 Status
= MountMgrMountedDeviceArrival(DeviceExtension
, &SymbolicName
, TRUE
);
924 PsSetThreadHardErrorsAreDisabled(PsGetCurrentThread(), OldState
);
933 MountMgrQueryPoints(IN PDEVICE_EXTENSION DeviceExtension
,
937 PIO_STACK_LOCATION Stack
;
938 PMOUNTDEV_UNIQUE_ID UniqueId
;
939 PMOUNTMGR_MOUNT_POINT MountPoint
;
940 UNICODE_STRING SymbolicName
, DeviceName
;
942 Stack
= IoGetNextIrpStackLocation(Irp
);
944 /* Validate input... */
945 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINT
))
947 return STATUS_INVALID_PARAMETER
;
950 MountPoint
= (PMOUNTMGR_MOUNT_POINT
)Irp
->AssociatedIrp
.SystemBuffer
;
951 if (!MountPoint
->SymbolicLinkNameLength
)
953 MountPoint
->SymbolicLinkNameOffset
= 0;
956 if (!MountPoint
->UniqueIdLength
)
958 MountPoint
->UniqueIdOffset
= 0;
961 if (!MountPoint
->DeviceNameLength
)
963 MountPoint
->DeviceNameOffset
= 0;
966 /* Addresses can't be odd */
967 if ((MountPoint
->SymbolicLinkNameOffset
& 1) ||
968 (MountPoint
->SymbolicLinkNameLength
& 1))
970 return STATUS_INVALID_PARAMETER
;
973 if ((MountPoint
->UniqueIdOffset
& 1) ||
974 (MountPoint
->UniqueIdLength
& 1))
976 return STATUS_INVALID_PARAMETER
;
979 if ((MountPoint
->DeviceNameOffset
& 1) ||
980 (MountPoint
->DeviceNameLength
& 1))
982 return STATUS_INVALID_PARAMETER
;
985 /* We can't go beyond */
986 if (((ULONG
)MountPoint
->SymbolicLinkNameLength
+ MountPoint
->UniqueIdLength
+
987 MountPoint
->DeviceNameLength
) < Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
989 return STATUS_INVALID_PARAMETER
;
992 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINTS
))
994 return STATUS_INVALID_PARAMETER
;
997 /* If caller provided a Symlink, use it */
998 if (MountPoint
->SymbolicLinkNameLength
!= 0)
1000 if (MountPoint
->SymbolicLinkNameLength
> MAXSHORT
)
1002 return STATUS_INVALID_PARAMETER
;
1005 SymbolicName
.Length
= MountPoint
->SymbolicLinkNameLength
;
1006 SymbolicName
.MaximumLength
= MountPoint
->SymbolicLinkNameLength
+ sizeof(WCHAR
);
1007 SymbolicName
.Buffer
= AllocatePool(SymbolicName
.MaximumLength
);
1008 if (!SymbolicName
.Buffer
)
1010 return STATUS_INSUFFICIENT_RESOURCES
;
1013 RtlCopyMemory(SymbolicName
.Buffer
,
1014 (PWSTR
)((ULONG_PTR
)MountPoint
+ MountPoint
->SymbolicLinkNameOffset
),
1015 SymbolicName
.Length
);
1016 SymbolicName
.Buffer
[SymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1018 /* Query links using it */
1019 Status
= QueryPointsFromSymbolicLinkName(DeviceExtension
, &SymbolicName
, Irp
);
1020 FreePool(SymbolicName
.Buffer
);
1022 /* If user provided an unique ID */
1023 else if (MountPoint
->UniqueIdLength
!= 0)
1025 UniqueId
= AllocatePool(MountPoint
->UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
1028 return STATUS_INSUFFICIENT_RESOURCES
;
1031 UniqueId
->UniqueIdLength
= MountPoint
->UniqueIdLength
;
1032 RtlCopyMemory(UniqueId
->UniqueId
,
1033 (PVOID
)((ULONG_PTR
)MountPoint
+ MountPoint
->UniqueIdOffset
),
1034 MountPoint
->UniqueIdLength
);
1036 /* Query links using it */
1037 Status
= QueryPointsFromMemory(DeviceExtension
, Irp
, UniqueId
, NULL
);
1040 /* If caller provided a device name */
1041 else if (MountPoint
->DeviceNameLength
!= 0)
1043 if (MountPoint
->DeviceNameLength
> MAXSHORT
)
1045 return STATUS_INVALID_PARAMETER
;
1048 DeviceName
.Length
= MountPoint
->DeviceNameLength
;
1049 DeviceName
.MaximumLength
= MountPoint
->DeviceNameLength
+ sizeof(WCHAR
);
1050 DeviceName
.Buffer
= AllocatePool(DeviceName
.MaximumLength
);
1051 if (!DeviceName
.Buffer
)
1053 return STATUS_INSUFFICIENT_RESOURCES
;
1056 RtlCopyMemory(DeviceName
.Buffer
,
1057 (PWSTR
)((ULONG_PTR
)MountPoint
+ MountPoint
->DeviceNameOffset
),
1059 DeviceName
.Buffer
[DeviceName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1061 /* Query links using it */
1062 Status
= QueryPointsFromMemory(DeviceExtension
, Irp
, NULL
, &DeviceName
);
1063 FreePool(DeviceName
.Buffer
);
1067 /* Otherwise, query all links */
1068 Status
= QueryPointsFromMemory(DeviceExtension
, Irp
, NULL
, NULL
);
1078 MountMgrDeletePoints(IN PDEVICE_EXTENSION DeviceExtension
,
1083 BOOLEAN CreateNoDrive
;
1084 PIO_STACK_LOCATION Stack
;
1085 PMOUNTDEV_UNIQUE_ID UniqueId
;
1086 PMOUNTMGR_MOUNT_POINT MountPoint
;
1087 PMOUNTMGR_MOUNT_POINTS MountPoints
;
1088 UNICODE_STRING SymbolicName
, DeviceName
;
1090 Stack
= IoGetNextIrpStackLocation(Irp
);
1092 /* Validate input */
1093 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINT
))
1095 return STATUS_INVALID_PARAMETER
;
1099 MountPoint
= (PMOUNTMGR_MOUNT_POINT
)Irp
->AssociatedIrp
.SystemBuffer
;
1100 CreateNoDrive
= (MountPoint
->SymbolicLinkNameOffset
&& MountPoint
->SymbolicLinkNameLength
);
1102 Status
= MountMgrQueryPoints(DeviceExtension
, Irp
);
1103 if (!NT_SUCCESS(Status
))
1108 /* For all the points matching the request */
1109 MountPoints
= (PMOUNTMGR_MOUNT_POINTS
)Irp
->AssociatedIrp
.SystemBuffer
;
1110 for (Link
= 0; Link
< MountPoints
->NumberOfMountPoints
; Link
++)
1112 SymbolicName
.Length
= MountPoints
->MountPoints
[Link
].SymbolicLinkNameLength
;
1113 SymbolicName
.MaximumLength
= SymbolicName
.Length
+ sizeof(WCHAR
);
1114 SymbolicName
.Buffer
= AllocatePool(SymbolicName
.MaximumLength
);
1115 if (!SymbolicName
.Buffer
)
1117 return STATUS_INSUFFICIENT_RESOURCES
;
1120 RtlCopyMemory(SymbolicName
.Buffer
,
1121 (PWSTR
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].SymbolicLinkNameOffset
),
1122 SymbolicName
.Length
);
1123 SymbolicName
.Buffer
[SymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1125 /* Create a no drive entry for the drive letters */
1126 if (CreateNoDrive
&& IsDriveLetter(&SymbolicName
))
1128 UniqueId
= AllocatePool(MountPoints
->MountPoints
[Link
].UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
1131 UniqueId
->UniqueIdLength
= MountPoints
->MountPoints
[Link
].UniqueIdLength
;
1132 RtlCopyMemory(UniqueId
->UniqueId
,
1133 (PMOUNTDEV_UNIQUE_ID
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].UniqueIdOffset
),
1134 MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1136 CreateNoDriveLetterEntry(UniqueId
);
1141 /* If there are no link any more, and no need to create a no drive entry */
1142 if (Link
== 0 && !CreateNoDrive
)
1144 /* Then, delete everything */
1145 UniqueId
= AllocatePool(MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1148 RtlCopyMemory(UniqueId
,
1149 (PMOUNTDEV_UNIQUE_ID
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].UniqueIdOffset
),
1150 MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1152 DeleteNoDriveLetterEntry(UniqueId
);
1157 /* Delete all the information about the mount point */
1158 GlobalDeleteSymbolicLink(&SymbolicName
);
1159 DeleteSymbolicLinkNameFromMemory(DeviceExtension
, &SymbolicName
, FALSE
);
1160 RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE
, DatabasePath
, SymbolicName
.Buffer
);
1161 FreePool(SymbolicName
.Buffer
);
1163 /* Notify the change */
1164 DeviceName
.Length
= DeviceName
.MaximumLength
=
1165 MountPoints
->MountPoints
[Link
].DeviceNameLength
;
1166 DeviceName
.Buffer
= (PWSTR
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].DeviceNameOffset
);
1167 MountMgrNotifyNameChange(DeviceExtension
, &DeviceName
, TRUE
);
1170 MountMgrNotify(DeviceExtension
);
1179 MountMgrDeletePointsDbOnly(IN PDEVICE_EXTENSION DeviceExtension
,
1184 UNICODE_STRING SymbolicName
;
1185 PMOUNTDEV_UNIQUE_ID UniqueId
;
1186 PMOUNTMGR_MOUNT_POINTS MountPoints
;
1189 Status
= MountMgrQueryPoints(DeviceExtension
, Irp
);
1190 if (!NT_SUCCESS(Status
))
1195 MountPoints
= (PMOUNTMGR_MOUNT_POINTS
)Irp
->AssociatedIrp
.SystemBuffer
;
1196 if (MountPoints
->NumberOfMountPoints
== 0)
1201 /* For all the mount points */
1202 for (Link
= 0; Link
< MountPoints
->NumberOfMountPoints
; Link
++)
1204 SymbolicName
.Length
= MountPoints
->MountPoints
[Link
].SymbolicLinkNameLength
;
1205 SymbolicName
.MaximumLength
= SymbolicName
.Length
+ sizeof(WCHAR
);
1206 SymbolicName
.Buffer
= AllocatePool(SymbolicName
.MaximumLength
);
1207 if (!SymbolicName
.Buffer
)
1209 return STATUS_INSUFFICIENT_RESOURCES
;
1212 RtlCopyMemory(SymbolicName
.Buffer
,
1213 (PWSTR
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].SymbolicLinkNameOffset
),
1214 SymbolicName
.Length
);
1215 SymbolicName
.Buffer
[SymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1217 /* If the only mount point is a drive letter, then create a no letter drive entry */
1218 if (MountPoints
->NumberOfMountPoints
== 1 && IsDriveLetter(&SymbolicName
))
1220 UniqueId
= AllocatePool(MountPoints
->MountPoints
[Link
].UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
1223 UniqueId
->UniqueIdLength
= MountPoints
->MountPoints
[Link
].UniqueIdLength
;
1224 RtlCopyMemory(UniqueId
->UniqueId
,
1225 (PMOUNTDEV_UNIQUE_ID
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].UniqueIdOffset
),
1226 MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1228 CreateNoDriveLetterEntry(UniqueId
);
1233 /* Simply delete mount point from DB */
1234 DeleteSymbolicLinkNameFromMemory(DeviceExtension
, &SymbolicName
, TRUE
);
1235 RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE
, DatabasePath
, SymbolicName
.Buffer
);
1236 FreePool(SymbolicName
.Buffer
);
1246 MountMgrVolumeMountPointChanged(IN PDEVICE_EXTENSION DeviceExtension
,
1248 IN NTSTATUS LockStatus
,
1249 OUT PUNICODE_STRING SourceDeviceName
,
1250 OUT PUNICODE_STRING SourceSymbolicName
,
1251 OUT PUNICODE_STRING TargetVolumeName
)
1255 PFILE_OBJECT FileObject
;
1256 PIO_STACK_LOCATION Stack
;
1257 ULONG Length
, SavedLength
;
1258 BOOLEAN FOReferenced
= FALSE
;
1259 IO_STATUS_BLOCK IoStatusBlock
;
1260 OBJECT_ATTRIBUTES ObjectAttributes
;
1261 PDEVICE_INFORMATION DeviceInformation
;
1262 OBJECT_NAME_INFORMATION ObjectNameInfo
;
1263 FILE_FS_DEVICE_INFORMATION FsDeviceInfo
;
1264 PFILE_NAME_INFORMATION FileNameInfo
= NULL
;
1265 PMOUNTMGR_VOLUME_MOUNT_POINT VolumeMountPoint
;
1266 POBJECT_NAME_INFORMATION ObjectNameInfoPtr
= NULL
;
1267 UNICODE_STRING SourceVolumeName
, TargetDeviceName
;
1269 Stack
= IoGetNextIrpStackLocation(Irp
);
1271 /* Validate input */
1272 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_VOLUME_MOUNT_POINT
))
1274 return STATUS_INVALID_PARAMETER
;
1277 VolumeMountPoint
= (PMOUNTMGR_VOLUME_MOUNT_POINT
)Irp
->AssociatedIrp
.SystemBuffer
;
1279 if (((ULONG
)VolumeMountPoint
->SourceVolumeNameLength
+ VolumeMountPoint
->TargetVolumeNameLength
) <
1280 Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
1282 return STATUS_INVALID_PARAMETER
;
1285 /* Get source volume name */
1286 SourceVolumeName
.Length
=
1287 SourceVolumeName
.MaximumLength
= VolumeMountPoint
->SourceVolumeNameLength
;
1288 SourceVolumeName
.Buffer
= (PWSTR
)((ULONG_PTR
)VolumeMountPoint
+ VolumeMountPoint
->SourceVolumeNameOffset
);
1290 InitializeObjectAttributes(&ObjectAttributes
,
1292 OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
,
1297 Status
= ZwOpenFile(&Handle
,
1298 SYNCHRONIZE
| FILE_READ_ATTRIBUTES
,
1301 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1302 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_REPARSE_POINT
);
1303 if (!NT_SUCCESS(Status
))
1308 TargetDeviceName
.Buffer
= NULL
;
1310 /* Query its attributes */
1311 Status
= ZwQueryVolumeInformationFile(Handle
,
1314 sizeof(FsDeviceInfo
),
1315 FileFsDeviceInformation
);
1316 if (!NT_SUCCESS(Status
))
1321 if (FsDeviceInfo
.DeviceType
!= FILE_DEVICE_DISK
&& FsDeviceInfo
.DeviceType
!= FILE_DEVICE_VIRTUAL_DISK
)
1326 if (FsDeviceInfo
.Characteristics
!= (FILE_REMOTE_DEVICE
| FILE_REMOVABLE_MEDIA
))
1332 Status
= ObReferenceObjectByHandle(Handle
, 0, IoFileObjectType
, KernelMode
, (PVOID
*)&FileObject
, NULL
);
1333 if (!NT_SUCCESS(Status
))
1337 FOReferenced
= TRUE
;
1340 FileNameInfo
= AllocatePool(sizeof(FILE_NAME_INFORMATION
));
1343 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1347 Status
= ZwQueryInformationFile(Handle
, &IoStatusBlock
, FileNameInfo
,
1348 sizeof(FILE_NAME_INFORMATION
),
1349 FileNameInformation
);
1350 if (Status
== STATUS_BUFFER_OVERFLOW
)
1352 /* Now we have real length, use it */
1353 Length
= FileNameInfo
->FileNameLength
;
1354 FreePool(FileNameInfo
);
1356 FileNameInfo
= AllocatePool(sizeof(FILE_NAME_INFORMATION
) + Length
);
1359 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1363 /* Really query file name */
1364 Status
= ZwQueryInformationFile(Handle
, &IoStatusBlock
, FileNameInfo
,
1365 sizeof(FILE_NAME_INFORMATION
) + Length
,
1366 FileNameInformation
);
1369 if (!NT_SUCCESS(Status
))
1374 /* Get symbolic name */
1375 ObjectNameInfoPtr
= &ObjectNameInfo
;
1376 SavedLength
= sizeof(OBJECT_NAME_INFORMATION
);
1377 Status
= ObQueryNameString(FileObject
->DeviceObject
, ObjectNameInfoPtr
, sizeof(OBJECT_NAME_INFORMATION
), &Length
);
1378 if (Status
== STATUS_INFO_LENGTH_MISMATCH
)
1380 /* Once again, with proper size, it works better */
1381 ObjectNameInfoPtr
= AllocatePool(Length
);
1382 if (!ObjectNameInfoPtr
)
1384 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1388 SavedLength
= Length
;
1389 Status
= ObQueryNameString(FileObject
->DeviceObject
, ObjectNameInfoPtr
, SavedLength
, &Length
);
1392 if (!NT_SUCCESS(Status
))
1397 /* Now, query the device name */
1398 Status
= QueryDeviceInformation(&ObjectNameInfoPtr
->Name
, SourceDeviceName
,
1399 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1400 if (!NT_SUCCESS(Status
))
1405 /* For target volume name, use input */
1406 TargetVolumeName
->Length
=
1407 TargetVolumeName
->MaximumLength
= VolumeMountPoint
->TargetVolumeNameLength
;
1408 TargetVolumeName
->Buffer
= (PWSTR
)((ULONG_PTR
)VolumeMountPoint
+ VolumeMountPoint
->TargetVolumeNameOffset
);
1410 /* Query its device name */
1411 Status
= QueryDeviceInformation(TargetVolumeName
, &TargetDeviceName
,
1412 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1413 if (!NT_SUCCESS(Status
))
1418 /* Return symbolic name */
1419 SourceSymbolicName
->Length
=
1420 SourceSymbolicName
->MaximumLength
= (USHORT
)FileNameInfo
->FileNameLength
;
1421 SourceSymbolicName
->Buffer
= (PWSTR
)FileNameInfo
;
1422 /* memmove allows memory overlap */
1423 RtlMoveMemory(SourceSymbolicName
->Buffer
, FileNameInfo
->FileName
, SourceSymbolicName
->Length
);
1424 FileNameInfo
= NULL
;
1426 /* Notify the change */
1427 MountMgrNotify(DeviceExtension
);
1428 MountMgrNotifyNameChange(DeviceExtension
, &TargetDeviceName
, TRUE
);
1430 /* If we are locked, sync databases if possible */
1431 if (NT_SUCCESS(LockStatus
))
1433 Status
= FindDeviceInfo(DeviceExtension
, SourceDeviceName
, FALSE
, &DeviceInformation
);
1434 if (NT_SUCCESS(Status
))
1436 ReconcileThisDatabaseWithMaster(DeviceExtension
, DeviceInformation
);
1440 Status
= STATUS_PENDING
;
1445 if (TargetDeviceName
.Buffer
)
1447 FreePool(TargetDeviceName
.Buffer
);
1450 if (ObjectNameInfoPtr
&& ObjectNameInfoPtr
!= &ObjectNameInfo
)
1452 FreePool(ObjectNameInfoPtr
);
1457 FreePool(FileNameInfo
);
1462 ObDereferenceObject(FileObject
);
1469 MountMgrVolumeMountPointCreated(IN PDEVICE_EXTENSION DeviceExtension
,
1471 IN NTSTATUS LockStatus
)
1473 UNREFERENCED_PARAMETER(DeviceExtension
);
1474 UNREFERENCED_PARAMETER(Irp
);
1475 UNREFERENCED_PARAMETER(LockStatus
);
1476 return STATUS_NOT_IMPLEMENTED
;
1480 MountMgrVolumeMountPointDeleted(IN PDEVICE_EXTENSION DeviceExtension
,
1482 IN NTSTATUS LockStatus
)
1484 UNREFERENCED_PARAMETER(DeviceExtension
);
1485 UNREFERENCED_PARAMETER(Irp
);
1486 UNREFERENCED_PARAMETER(LockStatus
);
1487 return STATUS_NOT_IMPLEMENTED
;
1495 MountMgrDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
1498 PIO_STACK_LOCATION Stack
;
1499 NTSTATUS Status
, LockStatus
;
1500 PDEVICE_EXTENSION DeviceExtension
;
1502 Stack
= IoGetNextIrpStackLocation(Irp
);
1503 DeviceExtension
= DeviceObject
->DeviceExtension
;
1505 KeWaitForSingleObject(&(DeviceExtension
->DeviceLock
), Executive
, KernelMode
, FALSE
, NULL
);
1507 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
1509 case IOCTL_MOUNTMGR_CREATE_POINT
:
1510 Status
= MountMgrCreatePoint(DeviceExtension
, Irp
);
1513 case IOCTL_MOUNTMGR_DELETE_POINTS
:
1514 Status
= MountMgrDeletePoints(DeviceExtension
, Irp
);
1517 case IOCTL_MOUNTMGR_QUERY_POINTS
:
1518 Status
= MountMgrQueryPoints(DeviceExtension
, Irp
);
1521 case IOCTL_MOUNTMGR_DELETE_POINTS_DBONLY
:
1522 Status
= MountMgrDeletePointsDbOnly(DeviceExtension
, Irp
);
1525 case IOCTL_MOUNTMGR_NEXT_DRIVE_LETTER
:
1526 Status
= MountMgrNextDriveLetter(DeviceExtension
, Irp
);
1529 case IOCTL_MOUNTMGR_AUTO_DL_ASSIGNMENTS
:
1530 DeviceExtension
->AutomaticDriveLetter
= TRUE
;
1531 Status
= STATUS_SUCCESS
;
1533 MountMgrAssignDriveLetters(DeviceExtension
);
1534 ReconcileAllDatabasesWithMaster(DeviceExtension
);
1535 WaitForOnlinesToComplete(DeviceExtension
);
1538 case IOCTL_MOUNTMGR_VOLUME_MOUNT_POINT_CREATED
:
1539 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
1541 LockStatus
= WaitForRemoteDatabaseSemaphore(DeviceExtension
);
1542 KeWaitForSingleObject(&(DeviceExtension
->DeviceLock
), Executive
, KernelMode
, FALSE
, NULL
);
1543 Status
= MountMgrVolumeMountPointCreated(DeviceExtension
, Irp
, LockStatus
);
1544 if (NT_SUCCESS(LockStatus
))
1546 ReleaseRemoteDatabaseSemaphore(DeviceExtension
);
1551 case IOCTL_MOUNTMGR_VOLUME_MOUNT_POINT_DELETED
:
1552 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
1554 LockStatus
= WaitForRemoteDatabaseSemaphore(DeviceExtension
);
1555 KeWaitForSingleObject(&(DeviceExtension
->DeviceLock
), Executive
, KernelMode
, FALSE
, NULL
);
1556 Status
= MountMgrVolumeMountPointDeleted(DeviceExtension
, Irp
, LockStatus
);
1557 if (NT_SUCCESS(LockStatus
))
1559 ReleaseRemoteDatabaseSemaphore(DeviceExtension
);
1564 case IOCTL_MOUNTMGR_CHANGE_NOTIFY
:
1565 Status
= MountMgrChangeNotify(DeviceExtension
, Irp
);
1568 case IOCTL_MOUNTMGR_KEEP_LINKS_WHEN_OFFLINE
:
1569 Status
= MountMgrKeepLinksWhenOffline(DeviceExtension
, Irp
);
1572 case IOCTL_MOUNTMGR_CHECK_UNPROCESSED_VOLUMES
:
1573 Status
= MountMgrCheckUnprocessedVolumes(DeviceExtension
, Irp
);
1576 case IOCTL_MOUNTMGR_VOLUME_ARRIVAL_NOTIFICATION
:
1577 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
1578 Status
= MountMgrVolumeArrivalNotification(DeviceExtension
, Irp
);
1581 case IOCTL_MOUNTMGR_QUERY_DOS_VOLUME_PATH
:
1582 Status
= MountMgrQueryDosVolumePath(DeviceExtension
, Irp
);
1585 case IOCTL_MOUNTMGR_QUERY_DOS_VOLUME_PATHS
:
1586 Status
= MountMgrQueryDosVolumePaths(DeviceExtension
, Irp
);
1589 case IOCTL_MOUNTMGR_SCRUB_REGISTRY
:
1590 Status
= MountMgrScrubRegistry(DeviceExtension
);
1593 case IOCTL_MOUNTMGR_QUERY_AUTO_MOUNT
:
1594 Status
= MountMgrQueryAutoMount(DeviceExtension
, Irp
);
1597 case IOCTL_MOUNTMGR_SET_AUTO_MOUNT
:
1598 Status
= MountMgrSetAutoMount(DeviceExtension
, Irp
);
1602 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1605 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
1607 if (Status
!= STATUS_PENDING
)
1615 Irp
->IoStatus
.Status
= Status
;
1616 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);