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 /* No offline volumes, nothing more to do */
296 if (IsListEmpty(&(DeviceExtension
->OfflineDeviceListHead
)))
298 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
299 return STATUS_SUCCESS
;
302 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
304 /* Reactivate all the offline volumes */
305 while (!IsListEmpty(&(DeviceExtension
->OfflineDeviceListHead
)))
307 NextEntry
= RemoveHeadList(&(DeviceExtension
->OfflineDeviceListHead
));
308 DeviceInformation
= CONTAINING_RECORD(NextEntry
, DEVICE_INFORMATION
, DeviceListEntry
);
310 ArrivalStatus
= MountMgrMountedDeviceArrival(DeviceExtension
,
311 &(DeviceInformation
->SymbolicName
),
312 DeviceInformation
->Volume
);
313 /* Then, remove them dead information */
314 MountMgrFreeDeadDeviceInfo(DeviceInformation
);
316 if (NT_SUCCESS(Status
))
318 Status
= ArrivalStatus
;
329 IsFtVolume(IN PUNICODE_STRING SymbolicName
)
334 PFILE_OBJECT FileObject
;
335 IO_STATUS_BLOCK IoStatusBlock
;
336 PARTITION_INFORMATION PartitionInfo
;
337 PDEVICE_OBJECT DeviceObject
, FileDeviceObject
;
339 /* Get device object */
340 Status
= IoGetDeviceObjectPointer(SymbolicName
,
341 FILE_READ_ATTRIBUTES
,
344 if (!NT_SUCCESS(Status
))
349 /* Get attached device */
350 FileDeviceObject
= FileObject
->DeviceObject
;
351 DeviceObject
= IoGetAttachedDeviceReference(FileDeviceObject
);
353 /* FT volume can't be removable */
354 if (FileDeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
)
356 ObDereferenceObject(DeviceObject
);
357 ObDereferenceObject(FileObject
);
361 ObDereferenceObject(FileObject
);
363 /* Get partition information */
364 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
365 Irp
= IoBuildDeviceIoControlRequest(IOCTL_DISK_GET_PARTITION_INFO
,
370 sizeof(PartitionInfo
),
376 ObDereferenceObject(DeviceObject
);
380 Status
= IoCallDriver(DeviceObject
, Irp
);
381 if (Status
== STATUS_PENDING
)
383 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
384 Status
= IoStatusBlock
.Status
;
387 ObDereferenceObject(DeviceObject
);
388 if (!NT_SUCCESS(Status
))
393 /* Check if this is a FT volume */
394 return IsRecognizedPartition(PartitionInfo
.PartitionType
);
401 ProcessSuggestedDriveLetters(IN PDEVICE_EXTENSION DeviceExtension
)
403 WCHAR NameBuffer
[DRIVE_LETTER_LENGTH
/ sizeof(WCHAR
)];
404 PLIST_ENTRY NextEntry
;
405 UNICODE_STRING SymbolicName
;
406 PDEVICE_INFORMATION DeviceInformation
;
408 /* No devices? Nothing to do! */
409 if (IsListEmpty(&(DeviceExtension
->DeviceListHead
)))
414 /* For all the devices */
415 for (NextEntry
= DeviceExtension
->DeviceListHead
.Flink
;
416 NextEntry
!= &(DeviceExtension
->DeviceListHead
);
417 NextEntry
= NextEntry
->Flink
)
419 DeviceInformation
= CONTAINING_RECORD(NextEntry
, DEVICE_INFORMATION
, DeviceListEntry
);
421 /* If no drive letter */
422 if (DeviceInformation
->SuggestedDriveLetter
== (UCHAR
)-1)
424 /* Ensure it has no entry yet */
425 if (!HasDriveLetter(DeviceInformation
) &&
426 !HasNoDriveLetterEntry(DeviceInformation
->UniqueId
))
429 CreateNoDriveLetterEntry(DeviceInformation
->UniqueId
);
432 DeviceInformation
->SuggestedDriveLetter
= 0;
434 /* Suggested letter & no entry */
435 else if (DeviceInformation
->SuggestedDriveLetter
&&
436 !HasNoDriveLetterEntry(DeviceInformation
->UniqueId
))
438 /* Just create a mount point */
439 SymbolicName
.Buffer
= NameBuffer
;
440 RtlCopyMemory(NameBuffer
, DosDevices
.Buffer
, DosDevices
.Length
);
441 NameBuffer
[LETTER_POSITION
] = DeviceInformation
->SuggestedDriveLetter
;
442 NameBuffer
[COLON_POSITION
] = L
':';
443 SymbolicName
.Length
=
444 SymbolicName
.MaximumLength
= DRIVE_LETTER_LENGTH
;
446 MountMgrCreatePointWorker(DeviceExtension
, &SymbolicName
, &(DeviceInformation
->DeviceName
));
455 MountMgrNextDriveLetterWorker(IN PDEVICE_EXTENSION DeviceExtension
,
456 IN PUNICODE_STRING DeviceName
,
457 OUT PMOUNTMGR_DRIVE_LETTER_INFORMATION DriveLetterInfo
)
461 PLIST_ENTRY NextEntry
;
462 PMOUNTDEV_UNIQUE_ID UniqueId
;
463 BOOLEAN Removable
, GptDriveLetter
;
464 PDEVICE_INFORMATION DeviceInformation
;
465 WCHAR NameBuffer
[DRIVE_LETTER_LENGTH
];
466 PSYMLINK_INFORMATION SymlinkInformation
;
467 UNICODE_STRING TargetDeviceName
, SymbolicName
;
469 /* First, process suggested letters */
470 if (!DeviceExtension
->ProcessedSuggestions
)
472 ProcessSuggestedDriveLetters(DeviceExtension
);
473 DeviceExtension
->ProcessedSuggestions
= TRUE
;
476 /* Then, get information about the device */
477 Status
= QueryDeviceInformation(DeviceName
, &TargetDeviceName
, NULL
, &Removable
, &GptDriveLetter
, NULL
, NULL
, NULL
);
478 if (!NT_SUCCESS(Status
))
483 /* Ensure we have such device */
484 NextEntry
= DeviceExtension
->DeviceListHead
.Flink
;
485 while (NextEntry
!= &(DeviceExtension
->DeviceListHead
))
487 DeviceInformation
= CONTAINING_RECORD(NextEntry
, DEVICE_INFORMATION
, DeviceListEntry
);
489 if (RtlCompareUnicodeString(&(DeviceInformation
->DeviceName
), &TargetDeviceName
, TRUE
) == 0)
494 NextEntry
= NextEntry
->Flink
;
497 if (NextEntry
== &(DeviceExtension
->DeviceListHead
))
499 FreePool(TargetDeviceName
.Buffer
);
500 return STATUS_OBJECT_NAME_NOT_FOUND
;
503 /* Now, mark we have assigned a letter (assumption) */
504 DeviceInformation
->LetterAssigned
=
505 DriveLetterInfo
->DriveLetterWasAssigned
= TRUE
;
507 /* Browse all the symlink to see if there's already a drive letter */
508 NextEntry
= DeviceInformation
->SymbolicLinksListHead
.Flink
;
509 while (NextEntry
!= &(DeviceInformation
->SymbolicLinksListHead
))
511 SymlinkInformation
= CONTAINING_RECORD(NextEntry
, SYMLINK_INFORMATION
, SymbolicLinksListEntry
);
513 /* This is a driver letter & online one, forget about new drive eltter */
514 if (IsDriveLetter(&(SymlinkInformation
->Name
)) && SymlinkInformation
->Online
)
516 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
517 DriveLetterInfo
->CurrentDriveLetter
= (CHAR
)SymlinkInformation
->Name
.Buffer
[LETTER_POSITION
];
521 NextEntry
= NextEntry
->Flink
;
524 /* If we didn't find a drive letter online
525 * ensure there's no GPT drive letter nor no drive entry
527 if (NextEntry
== &(DeviceInformation
->SymbolicLinksListHead
))
529 if (GptDriveLetter
|| HasNoDriveLetterEntry(DeviceInformation
->UniqueId
))
531 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
532 DriveLetterInfo
->CurrentDriveLetter
= 0;
538 /* No, ensure that the device is not automonted nor removable */
539 if (!DeviceExtension
->NoAutoMount
&& !Removable
)
541 if (DriveLetterInfo
->DriveLetterWasAssigned
)
543 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
544 DriveLetterInfo
->CurrentDriveLetter
= 0;
550 if (!DriveLetterInfo
->DriveLetterWasAssigned
)
555 /* Now everything is fine, start processing */
556 if (RtlPrefixUnicodeString(&DeviceFloppy
, &TargetDeviceName
, TRUE
))
562 DriveLetter
= 'C' + RtlPrefixUnicodeString(&DeviceCdRom
, &TargetDeviceName
, TRUE
);
565 /* We cannot set NO drive letter */
566 ASSERT(DeviceInformation
->SuggestedDriveLetter
!= (UCHAR
)-1);
568 /* If we don't have suggested letter but it's a FT volume, fail */
569 if (!DeviceInformation
->SuggestedDriveLetter
&& IsFtVolume(&(DeviceInformation
->DeviceName
)))
571 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
572 DriveLetterInfo
->CurrentDriveLetter
= 0;
578 RtlCopyMemory(NameBuffer
, DosDevices
.Buffer
, DosDevices
.Length
);
579 NameBuffer
[COLON_POSITION
] = L
':';
580 SymbolicName
.Buffer
= NameBuffer
;
581 SymbolicName
.Length
=
582 SymbolicName
.MaximumLength
= DRIVE_LETTER_LENGTH
;
584 /* It's all prepared, create mount point */
585 if (DeviceInformation
->SuggestedDriveLetter
)
587 DriveLetterInfo
->CurrentDriveLetter
= DeviceInformation
->SuggestedDriveLetter
;
588 NameBuffer
[LETTER_POSITION
] = DeviceInformation
->SuggestedDriveLetter
;
590 Status
= MountMgrCreatePointWorker(DeviceExtension
, &SymbolicName
, &TargetDeviceName
);
591 if (NT_SUCCESS(Status
))
597 /* It failed with this letter... Try another one! */
598 for (DriveLetterInfo
->CurrentDriveLetter
= DriveLetter
;
599 DriveLetterInfo
->CurrentDriveLetter
<= L
'Z';
600 DriveLetterInfo
->CurrentDriveLetter
++)
602 NameBuffer
[LETTER_POSITION
] = DeviceInformation
->SuggestedDriveLetter
;
604 Status
= MountMgrCreatePointWorker(DeviceExtension
, &SymbolicName
, &TargetDeviceName
);
605 if (NT_SUCCESS(Status
))
611 /* We failed setting a letter */
612 if (DriveLetterInfo
->CurrentDriveLetter
> L
'Z')
614 DriveLetterInfo
->DriveLetterWasAssigned
= FALSE
;
615 DriveLetterInfo
->CurrentDriveLetter
= 0;
617 /* Try at least to add a no drive letter entry */
618 Status
= QueryDeviceInformation(&TargetDeviceName
, NULL
, &UniqueId
, NULL
, NULL
, NULL
, NULL
, NULL
);
619 if (NT_SUCCESS(Status
))
621 CreateNoDriveLetterEntry(UniqueId
);
627 FreePool(TargetDeviceName
.Buffer
);
629 return STATUS_SUCCESS
;
637 MountMgrNextDriveLetter(IN PDEVICE_EXTENSION DeviceExtension
,
641 PIO_STACK_LOCATION Stack
;
642 UNICODE_STRING DeviceName
;
643 PMOUNTMGR_DRIVE_LETTER_TARGET DriveLetterTarget
;
644 MOUNTMGR_DRIVE_LETTER_INFORMATION DriveLetterInformation
;
646 Stack
= IoGetNextIrpStackLocation(Irp
);
649 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_DRIVE_LETTER_TARGET
) ||
650 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(MOUNTMGR_DRIVE_LETTER_INFORMATION
))
652 return STATUS_INVALID_PARAMETER
;
655 DriveLetterTarget
= (PMOUNTMGR_DRIVE_LETTER_TARGET
)Irp
->AssociatedIrp
.SystemBuffer
;
656 if (DriveLetterTarget
->DeviceNameLength
+ sizeof(USHORT
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
658 return STATUS_INVALID_PARAMETER
;
661 /* Call the worker */
662 DeviceName
.Buffer
= DriveLetterTarget
->DeviceName
;
664 DeviceName
.MaximumLength
= DriveLetterTarget
->DeviceNameLength
;
666 Status
= MountMgrNextDriveLetterWorker(DeviceExtension
, &DeviceName
,
667 &DriveLetterInformation
);
668 if (NT_SUCCESS(Status
))
670 *(PMOUNTMGR_DRIVE_LETTER_INFORMATION
)Irp
->AssociatedIrp
.SystemBuffer
=
671 DriveLetterInformation
;
672 Irp
->IoStatus
.Information
= sizeof(MOUNTMGR_DRIVE_LETTER_INFORMATION
);
683 MountMgrQuerySystemVolumeNameQueryRoutine(IN PWSTR ValueName
,
686 IN ULONG ValueLength
,
688 IN PVOID EntryContext
)
690 UNICODE_STRING ValueString
;
691 PUNICODE_STRING SystemVolumeName
;
693 if (ValueType
!= REG_SZ
)
695 return STATUS_SUCCESS
;
698 RtlInitUnicodeString(&ValueString
, ValueData
);
699 SystemVolumeName
= Context
;
701 /* Return a string containing system volume name */
702 SystemVolumeName
->Length
= ValueString
.Length
;
703 SystemVolumeName
->MaximumLength
= ValueString
.Length
+ sizeof(WCHAR
);
704 SystemVolumeName
->Buffer
= AllocatePool(SystemVolumeName
->MaximumLength
);
705 if (SystemVolumeName
->Buffer
)
707 RtlCopyMemory(SystemVolumeName
->Buffer
, ValueData
, ValueString
.Length
);
708 SystemVolumeName
->Buffer
[ValueString
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
711 return STATUS_SUCCESS
;
719 MountMgrQuerySystemVolumeName(OUT PUNICODE_STRING SystemVolumeName
)
721 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
723 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
724 QueryTable
[0].QueryRoutine
= MountMgrQuerySystemVolumeNameQueryRoutine
;
725 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_REQUIRED
;
726 QueryTable
[0].Name
= L
"SystemPartition";
728 SystemVolumeName
->Buffer
= NULL
;
730 RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
731 L
"\\Registry\\Machine\\System\\Setup",
736 if (SystemVolumeName
->Buffer
)
738 return STATUS_SUCCESS
;
741 return STATUS_UNSUCCESSFUL
;
748 MountMgrAssignDriveLetters(IN PDEVICE_EXTENSION DeviceExtension
)
751 PLIST_ENTRY NextEntry
;
752 UNICODE_STRING SystemVolumeName
;
753 PDEVICE_INFORMATION DeviceInformation
;
754 MOUNTMGR_DRIVE_LETTER_INFORMATION DriveLetterInformation
;
756 /* First, get system volume name */
757 Status
= MountMgrQuerySystemVolumeName(&SystemVolumeName
);
759 /* If there are no device, it's all done */
760 if (IsListEmpty(&(DeviceExtension
->DeviceListHead
)))
762 if (NT_SUCCESS(Status
))
764 FreePool(SystemVolumeName
.Buffer
);
770 /* Now, for all the devices... */
771 for (NextEntry
= DeviceExtension
->DeviceListHead
.Flink
;
772 NextEntry
!= &(DeviceExtension
->DeviceListHead
);
773 NextEntry
= NextEntry
->Flink
)
775 DeviceInformation
= CONTAINING_RECORD(NextEntry
, DEVICE_INFORMATION
, DeviceListEntry
);
777 /* If the device doesn't have a letter assigned, do it! */
778 if (!DeviceInformation
->LetterAssigned
)
780 MountMgrNextDriveLetterWorker(DeviceExtension
,
781 &(DeviceInformation
->DeviceName
),
782 &DriveLetterInformation
);
785 /* If it was the system volume */
786 if (NT_SUCCESS(Status
) && RtlEqualUnicodeString(&SystemVolumeName
, &(DeviceInformation
->DeviceName
), TRUE
))
788 /* Keep track of it */
789 DeviceExtension
->DriveLetterData
= AllocatePool(DeviceInformation
->UniqueId
->UniqueIdLength
+
790 sizeof(MOUNTDEV_UNIQUE_ID
));
791 if (DeviceExtension
->DriveLetterData
)
793 RtlCopyMemory(DeviceExtension
->DriveLetterData
,
794 DeviceInformation
->UniqueId
,
795 DeviceInformation
->UniqueId
->UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
798 /* If it was not automount, ensure it gets mounted */
799 if (!DeviceExtension
->NoAutoMount
)
801 DeviceExtension
->NoAutoMount
= TRUE
;
803 MountMgrNextDriveLetterWorker(DeviceExtension
,
804 &(DeviceInformation
->DeviceName
),
805 &DriveLetterInformation
);
807 DeviceExtension
->NoAutoMount
= FALSE
;
812 if (NT_SUCCESS(Status
))
814 FreePool(SystemVolumeName
.Buffer
);
819 MountMgrQueryDosVolumePath(IN PDEVICE_EXTENSION DeviceExtension
,
822 return STATUS_NOT_IMPLEMENTED
;
826 MountMgrQueryDosVolumePaths(IN PDEVICE_EXTENSION DeviceExtension
,
829 return STATUS_NOT_IMPLEMENTED
;
836 MountMgrKeepLinksWhenOffline(IN PDEVICE_EXTENSION DeviceExtension
,
840 PIO_STACK_LOCATION Stack
;
841 UNICODE_STRING SymbolicName
;
842 PMOUNTMGR_TARGET_NAME Target
;
843 PDEVICE_INFORMATION DeviceInformation
;
845 Stack
= IoGetNextIrpStackLocation(Irp
);
848 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_TARGET_NAME
))
850 return STATUS_INVALID_PARAMETER
;
853 Target
= (PMOUNTMGR_TARGET_NAME
)Irp
->AssociatedIrp
.SystemBuffer
;
854 if (Target
->DeviceNameLength
+ sizeof(USHORT
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
856 return STATUS_INVALID_PARAMETER
;
859 SymbolicName
.Length
=
860 SymbolicName
.MaximumLength
= Target
->DeviceNameLength
;
861 SymbolicName
.Buffer
= Target
->DeviceName
;
863 /* Find the associated device */
864 Status
= FindDeviceInfo(DeviceExtension
, &SymbolicName
, FALSE
, &DeviceInformation
);
865 if (!NT_SUCCESS(Status
))
870 /* Mark we want to keep links */
871 DeviceInformation
->KeepLinks
= TRUE
;
873 return STATUS_SUCCESS
;
880 MountMgrVolumeArrivalNotification(IN PDEVICE_EXTENSION DeviceExtension
,
885 PIO_STACK_LOCATION Stack
;
886 UNICODE_STRING SymbolicName
;
887 PMOUNTMGR_TARGET_NAME Target
;
889 Stack
= IoGetNextIrpStackLocation(Irp
);
892 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_TARGET_NAME
))
894 return STATUS_INVALID_PARAMETER
;
897 Target
= (PMOUNTMGR_TARGET_NAME
)Irp
->AssociatedIrp
.SystemBuffer
;
898 if (Target
->DeviceNameLength
+ sizeof(USHORT
) > Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
900 return STATUS_INVALID_PARAMETER
;
903 SymbolicName
.Length
=
904 SymbolicName
.MaximumLength
= Target
->DeviceNameLength
;
905 SymbolicName
.Buffer
= Target
->DeviceName
;
907 /* Disable hard errors */
908 OldState
= PsGetThreadHardErrorsAreDisabled(PsGetCurrentThread());
909 PsSetThreadHardErrorsAreDisabled(PsGetCurrentThread(), TRUE
);
911 /* Call real worker */
912 Status
= MountMgrMountedDeviceArrival(DeviceExtension
, &SymbolicName
, TRUE
);
914 PsSetThreadHardErrorsAreDisabled(PsGetCurrentThread(), OldState
);
923 MountMgrQueryPoints(IN PDEVICE_EXTENSION DeviceExtension
,
927 PIO_STACK_LOCATION Stack
;
928 PMOUNTDEV_UNIQUE_ID UniqueId
;
929 PMOUNTMGR_MOUNT_POINT MountPoint
;
930 UNICODE_STRING SymbolicName
, DeviceName
;
932 Stack
= IoGetNextIrpStackLocation(Irp
);
934 /* Validate input... */
935 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINT
))
937 return STATUS_INVALID_PARAMETER
;
940 MountPoint
= (PMOUNTMGR_MOUNT_POINT
)Irp
->AssociatedIrp
.SystemBuffer
;
941 if (!MountPoint
->SymbolicLinkNameLength
)
943 MountPoint
->SymbolicLinkNameOffset
= 0;
946 if (!MountPoint
->UniqueIdLength
)
948 MountPoint
->UniqueIdOffset
= 0;
951 if (!MountPoint
->DeviceNameLength
)
953 MountPoint
->DeviceNameOffset
= 0;
956 /* Addresses can't be odd */
957 if ((MountPoint
->SymbolicLinkNameOffset
& 1) ||
958 (MountPoint
->SymbolicLinkNameLength
& 1))
960 return STATUS_INVALID_PARAMETER
;
963 if ((MountPoint
->UniqueIdOffset
& 1) ||
964 (MountPoint
->UniqueIdLength
& 1))
966 return STATUS_INVALID_PARAMETER
;
969 if ((MountPoint
->DeviceNameOffset
& 1) ||
970 (MountPoint
->DeviceNameLength
& 1))
972 return STATUS_INVALID_PARAMETER
;
975 /* We can't go beyond */
976 if (((ULONG
)MountPoint
->SymbolicLinkNameLength
+ MountPoint
->UniqueIdLength
+
977 MountPoint
->DeviceNameLength
) < Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
979 return STATUS_INVALID_PARAMETER
;
982 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINTS
))
984 return STATUS_INVALID_PARAMETER
;
987 /* If caller provided a Symlink, use it */
988 if (MountPoint
->SymbolicLinkNameLength
!= 0)
990 if (MountPoint
->SymbolicLinkNameLength
> MAXSHORT
)
992 return STATUS_INVALID_PARAMETER
;
995 SymbolicName
.Length
= MountPoint
->SymbolicLinkNameLength
;
996 SymbolicName
.MaximumLength
= MountPoint
->SymbolicLinkNameLength
+ sizeof(WCHAR
);
997 SymbolicName
.Buffer
= AllocatePool(SymbolicName
.MaximumLength
);
998 if (!SymbolicName
.Buffer
)
1000 return STATUS_INSUFFICIENT_RESOURCES
;
1003 RtlCopyMemory(SymbolicName
.Buffer
,
1004 (PWSTR
)((ULONG_PTR
)MountPoint
+ MountPoint
->SymbolicLinkNameOffset
),
1005 SymbolicName
.Length
);
1006 SymbolicName
.Buffer
[SymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1008 /* Query links using it */
1009 Status
= QueryPointsFromSymbolicLinkName(DeviceExtension
, &SymbolicName
, Irp
);
1010 FreePool(SymbolicName
.Buffer
);
1012 /* If user provided an unique ID */
1013 else if (MountPoint
->UniqueIdLength
!= 0)
1015 UniqueId
= AllocatePool(MountPoint
->UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
1018 return STATUS_INSUFFICIENT_RESOURCES
;
1021 UniqueId
->UniqueIdLength
= MountPoint
->UniqueIdLength
;
1022 RtlCopyMemory(UniqueId
->UniqueId
,
1023 (PVOID
)((ULONG_PTR
)MountPoint
+ MountPoint
->UniqueIdOffset
),
1024 MountPoint
->UniqueIdLength
);
1026 /* Query links using it */
1027 Status
= QueryPointsFromMemory(DeviceExtension
, Irp
, UniqueId
, NULL
);
1030 /* If caller provided a device name */
1031 else if (MountPoint
->DeviceNameLength
!= 0)
1033 if (MountPoint
->DeviceNameLength
> MAXSHORT
)
1035 return STATUS_INVALID_PARAMETER
;
1038 DeviceName
.Length
= MountPoint
->DeviceNameLength
;
1039 DeviceName
.MaximumLength
= MountPoint
->DeviceNameLength
+ sizeof(WCHAR
);
1040 DeviceName
.Buffer
= AllocatePool(DeviceName
.MaximumLength
);
1041 if (!DeviceName
.Buffer
)
1043 return STATUS_INSUFFICIENT_RESOURCES
;
1046 RtlCopyMemory(DeviceName
.Buffer
,
1047 (PWSTR
)((ULONG_PTR
)MountPoint
+ MountPoint
->DeviceNameOffset
),
1049 DeviceName
.Buffer
[DeviceName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1051 /* Query links using it */
1052 Status
= QueryPointsFromMemory(DeviceExtension
, Irp
, NULL
, &DeviceName
);
1053 FreePool(DeviceName
.Buffer
);
1057 /* Otherwise, query all links */
1058 Status
= QueryPointsFromMemory(DeviceExtension
, Irp
, NULL
, NULL
);
1068 MountMgrDeletePoints(IN PDEVICE_EXTENSION DeviceExtension
,
1073 BOOLEAN CreateNoDrive
;
1074 PIO_STACK_LOCATION Stack
;
1075 PMOUNTDEV_UNIQUE_ID UniqueId
;
1076 PMOUNTMGR_MOUNT_POINT MountPoint
;
1077 PMOUNTMGR_MOUNT_POINTS MountPoints
;
1078 UNICODE_STRING SymbolicName
, DeviceName
;
1080 Stack
= IoGetNextIrpStackLocation(Irp
);
1082 /* Validate input */
1083 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINT
))
1085 return STATUS_INVALID_PARAMETER
;
1089 MountPoint
= (PMOUNTMGR_MOUNT_POINT
)Irp
->AssociatedIrp
.SystemBuffer
;
1090 CreateNoDrive
= (MountPoint
->SymbolicLinkNameOffset
&& MountPoint
->SymbolicLinkNameLength
);
1092 Status
= MountMgrQueryPoints(DeviceExtension
, Irp
);
1093 if (!NT_SUCCESS(Status
))
1098 /* For all the points matching the request */
1099 MountPoints
= (PMOUNTMGR_MOUNT_POINTS
)Irp
->AssociatedIrp
.SystemBuffer
;
1100 for (Link
= 0; Link
< MountPoints
->NumberOfMountPoints
; Link
++)
1102 SymbolicName
.Length
= MountPoints
->MountPoints
[Link
].SymbolicLinkNameLength
;
1103 SymbolicName
.MaximumLength
= SymbolicName
.Length
+ sizeof(WCHAR
);
1104 SymbolicName
.Buffer
= AllocatePool(SymbolicName
.MaximumLength
);
1105 if (!SymbolicName
.Buffer
)
1107 return STATUS_INSUFFICIENT_RESOURCES
;
1110 RtlCopyMemory(SymbolicName
.Buffer
,
1111 (PWSTR
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].SymbolicLinkNameOffset
),
1112 SymbolicName
.Length
);
1113 SymbolicName
.Buffer
[SymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1115 /* Create a no drive entry for the drive letters */
1116 if (CreateNoDrive
&& IsDriveLetter(&SymbolicName
))
1118 UniqueId
= AllocatePool(MountPoints
->MountPoints
[Link
].UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
1121 UniqueId
->UniqueIdLength
= MountPoints
->MountPoints
[Link
].UniqueIdLength
;
1122 RtlCopyMemory(UniqueId
->UniqueId
,
1123 (PMOUNTDEV_UNIQUE_ID
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].UniqueIdOffset
),
1124 MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1126 CreateNoDriveLetterEntry(UniqueId
);
1131 /* If there are no link any more, and no need to create a no drive entry */
1132 if (Link
== 0 && !CreateNoDrive
)
1134 /* Then, delete everything */
1135 UniqueId
= AllocatePool(MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1138 RtlCopyMemory(UniqueId
,
1139 (PMOUNTDEV_UNIQUE_ID
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].UniqueIdOffset
),
1140 MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1142 DeleteNoDriveLetterEntry(UniqueId
);
1147 /* Delete all the information about the mount point */
1148 GlobalDeleteSymbolicLink(&SymbolicName
);
1149 DeleteSymbolicLinkNameFromMemory(DeviceExtension
, &SymbolicName
, FALSE
);
1150 RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE
, DatabasePath
, SymbolicName
.Buffer
);
1151 FreePool(SymbolicName
.Buffer
);
1153 /* Notify the change */
1154 DeviceName
.Length
= DeviceName
.MaximumLength
=
1155 MountPoints
->MountPoints
[Link
].DeviceNameLength
;
1156 DeviceName
.Buffer
= (PWSTR
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].DeviceNameOffset
);
1157 MountMgrNotifyNameChange(DeviceExtension
, &DeviceName
, TRUE
);
1160 MountMgrNotify(DeviceExtension
);
1169 MountMgrDeletePointsDbOnly(IN PDEVICE_EXTENSION DeviceExtension
,
1174 UNICODE_STRING SymbolicName
;
1175 PMOUNTDEV_UNIQUE_ID UniqueId
;
1176 PMOUNTMGR_MOUNT_POINTS MountPoints
;
1179 Status
= MountMgrQueryPoints(DeviceExtension
, Irp
);
1180 if (!NT_SUCCESS(Status
))
1185 MountPoints
= (PMOUNTMGR_MOUNT_POINTS
)Irp
->AssociatedIrp
.SystemBuffer
;
1186 if (MountPoints
->NumberOfMountPoints
== 0)
1191 /* For all the mount points */
1192 for (Link
= 0; Link
< MountPoints
->NumberOfMountPoints
; Link
++)
1194 SymbolicName
.Length
= MountPoints
->MountPoints
[Link
].SymbolicLinkNameLength
;
1195 SymbolicName
.MaximumLength
= SymbolicName
.Length
+ sizeof(WCHAR
);
1196 SymbolicName
.Buffer
= AllocatePool(SymbolicName
.MaximumLength
);
1197 if (!SymbolicName
.Buffer
)
1199 return STATUS_INSUFFICIENT_RESOURCES
;
1202 RtlCopyMemory(SymbolicName
.Buffer
,
1203 (PWSTR
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].SymbolicLinkNameOffset
),
1204 SymbolicName
.Length
);
1205 SymbolicName
.Buffer
[SymbolicName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1207 /* If the only mount point is a drive letter, then create a no letter drive entry */
1208 if (MountPoints
->NumberOfMountPoints
== 1 && IsDriveLetter(&SymbolicName
))
1210 UniqueId
= AllocatePool(MountPoints
->MountPoints
[Link
].UniqueIdLength
+ sizeof(MOUNTDEV_UNIQUE_ID
));
1213 UniqueId
->UniqueIdLength
= MountPoints
->MountPoints
[Link
].UniqueIdLength
;
1214 RtlCopyMemory(UniqueId
->UniqueId
,
1215 (PMOUNTDEV_UNIQUE_ID
)((ULONG_PTR
)MountPoints
+ MountPoints
->MountPoints
[Link
].UniqueIdOffset
),
1216 MountPoints
->MountPoints
[Link
].UniqueIdLength
);
1218 CreateNoDriveLetterEntry(UniqueId
);
1223 /* Simply delete mount point from DB */
1224 DeleteSymbolicLinkNameFromMemory(DeviceExtension
, &SymbolicName
, TRUE
);
1225 RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE
, DatabasePath
, SymbolicName
.Buffer
);
1226 FreePool(SymbolicName
.Buffer
);
1236 MountMgrVolumeMountPointChanged(IN PDEVICE_EXTENSION DeviceExtension
,
1238 IN NTSTATUS LockStatus
,
1239 OUT PUNICODE_STRING SourceDeviceName
,
1240 OUT PUNICODE_STRING SourceSymbolicName
,
1241 OUT PUNICODE_STRING TargetVolumeName
)
1245 PFILE_OBJECT FileObject
;
1246 PIO_STACK_LOCATION Stack
;
1247 ULONG Length
, SavedLength
;
1248 BOOLEAN FOReferenced
= FALSE
;
1249 IO_STATUS_BLOCK IoStatusBlock
;
1250 OBJECT_ATTRIBUTES ObjectAttributes
;
1251 PDEVICE_INFORMATION DeviceInformation
;
1252 OBJECT_NAME_INFORMATION ObjectNameInfo
;
1253 FILE_FS_DEVICE_INFORMATION FsDeviceInfo
;
1254 PFILE_NAME_INFORMATION FileNameInfo
= NULL
;
1255 PMOUNTMGR_VOLUME_MOUNT_POINT VolumeMountPoint
;
1256 POBJECT_NAME_INFORMATION ObjectNameInfoPtr
= NULL
;
1257 UNICODE_STRING SourceVolumeName
, TargetDeviceName
;
1259 Stack
= IoGetNextIrpStackLocation(Irp
);
1261 /* Validate input */
1262 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_VOLUME_MOUNT_POINT
))
1264 return STATUS_INVALID_PARAMETER
;
1267 VolumeMountPoint
= (PMOUNTMGR_VOLUME_MOUNT_POINT
)Irp
->AssociatedIrp
.SystemBuffer
;
1269 if (((ULONG
)VolumeMountPoint
->SourceVolumeNameLength
+ VolumeMountPoint
->TargetVolumeNameLength
) <
1270 Stack
->Parameters
.DeviceIoControl
.InputBufferLength
)
1272 return STATUS_INVALID_PARAMETER
;
1275 /* Get source volume name */
1276 SourceVolumeName
.Length
=
1277 SourceVolumeName
.MaximumLength
= VolumeMountPoint
->SourceVolumeNameLength
;
1278 SourceVolumeName
.Buffer
= (PWSTR
)((ULONG_PTR
)VolumeMountPoint
+ VolumeMountPoint
->SourceVolumeNameOffset
);
1280 InitializeObjectAttributes(&ObjectAttributes
,
1282 OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
,
1287 Status
= ZwOpenFile(&Handle
,
1288 SYNCHRONIZE
| FILE_READ_ATTRIBUTES
,
1291 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1292 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_REPARSE_POINT
);
1293 if (!NT_SUCCESS(Status
))
1298 TargetDeviceName
.Buffer
= NULL
;
1300 /* Query its attributes */
1301 Status
= ZwQueryVolumeInformationFile(Handle
,
1304 sizeof(FsDeviceInfo
),
1305 FileFsDeviceInformation
);
1306 if (!NT_SUCCESS(Status
))
1311 if (FsDeviceInfo
.DeviceType
!= FILE_DEVICE_DISK
&& FsDeviceInfo
.DeviceType
!= FILE_DEVICE_VIRTUAL_DISK
)
1316 if (FsDeviceInfo
.Characteristics
!= (FILE_REMOTE_DEVICE
| FILE_REMOVABLE_MEDIA
))
1322 Status
= ObReferenceObjectByHandle(Handle
, 0, IoFileObjectType
, KernelMode
, (PVOID
*)&FileObject
, NULL
);
1323 if (!NT_SUCCESS(Status
))
1327 FOReferenced
= TRUE
;
1330 FileNameInfo
= AllocatePool(sizeof(FILE_NAME_INFORMATION
));
1333 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1337 Status
= ZwQueryInformationFile(Handle
, &IoStatusBlock
, FileNameInfo
,
1338 sizeof(FILE_NAME_INFORMATION
),
1339 FileNameInformation
);
1340 if (Status
== STATUS_BUFFER_OVERFLOW
)
1342 /* Now we have real length, use it */
1343 Length
= FileNameInfo
->FileNameLength
;
1344 FreePool(FileNameInfo
);
1346 FileNameInfo
= AllocatePool(sizeof(FILE_NAME_INFORMATION
) + Length
);
1349 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1353 /* Really query file name */
1354 Status
= ZwQueryInformationFile(Handle
, &IoStatusBlock
, FileNameInfo
,
1355 sizeof(FILE_NAME_INFORMATION
) + Length
,
1356 FileNameInformation
);
1359 if (!NT_SUCCESS(Status
))
1364 /* Get symbolic name */
1365 ObjectNameInfoPtr
= &ObjectNameInfo
;
1366 SavedLength
= sizeof(OBJECT_NAME_INFORMATION
);
1367 Status
= ObQueryNameString(FileObject
->DeviceObject
, ObjectNameInfoPtr
, sizeof(OBJECT_NAME_INFORMATION
), &Length
);
1368 if (Status
== STATUS_INFO_LENGTH_MISMATCH
)
1370 /* Once again, with proper size, it works better */
1371 ObjectNameInfoPtr
= AllocatePool(Length
);
1372 if (!ObjectNameInfoPtr
)
1374 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1378 SavedLength
= Length
;
1379 Status
= ObQueryNameString(FileObject
->DeviceObject
, ObjectNameInfoPtr
, SavedLength
, &Length
);
1382 if (!NT_SUCCESS(Status
))
1387 /* Now, query the device name */
1388 Status
= QueryDeviceInformation(&ObjectNameInfoPtr
->Name
, SourceDeviceName
,
1389 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1390 if (!NT_SUCCESS(Status
))
1395 /* For target volume name, use input */
1396 TargetVolumeName
->Length
=
1397 TargetVolumeName
->MaximumLength
= VolumeMountPoint
->TargetVolumeNameLength
;
1398 TargetVolumeName
->Buffer
= (PWSTR
)((ULONG_PTR
)VolumeMountPoint
+ VolumeMountPoint
->TargetVolumeNameOffset
);
1400 /* Query its device name */
1401 Status
= QueryDeviceInformation(TargetVolumeName
, &TargetDeviceName
,
1402 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1403 if (!NT_SUCCESS(Status
))
1408 /* Return symbolic name */
1409 SourceSymbolicName
->Length
=
1410 SourceSymbolicName
->MaximumLength
= (USHORT
)FileNameInfo
->FileNameLength
;
1411 SourceSymbolicName
->Buffer
= (PWSTR
)FileNameInfo
;
1412 /* memmove allows memory overlap */
1413 RtlMoveMemory(SourceSymbolicName
->Buffer
, FileNameInfo
->FileName
, SourceSymbolicName
->Length
);
1414 FileNameInfo
= NULL
;
1416 /* Notify the change */
1417 MountMgrNotify(DeviceExtension
);
1418 MountMgrNotifyNameChange(DeviceExtension
, &TargetDeviceName
, TRUE
);
1420 /* If we are locked, sync databases if possible */
1421 if (NT_SUCCESS(LockStatus
))
1423 Status
= FindDeviceInfo(DeviceExtension
, SourceDeviceName
, FALSE
, &DeviceInformation
);
1424 if (NT_SUCCESS(Status
))
1426 ReconcileThisDatabaseWithMaster(DeviceExtension
, DeviceInformation
);
1430 Status
= STATUS_PENDING
;
1435 if (TargetDeviceName
.Buffer
)
1437 FreePool(TargetDeviceName
.Buffer
);
1440 if (ObjectNameInfoPtr
&& ObjectNameInfoPtr
!= &ObjectNameInfo
)
1442 FreePool(ObjectNameInfoPtr
);
1447 FreePool(FileNameInfo
);
1452 ObDereferenceObject(FileObject
);
1459 MountMgrVolumeMountPointCreated(IN PDEVICE_EXTENSION DeviceExtension
,
1461 IN NTSTATUS LockStatus
)
1463 return STATUS_NOT_IMPLEMENTED
;
1467 MountMgrVolumeMountPointDeleted(IN PDEVICE_EXTENSION DeviceExtension
,
1469 IN NTSTATUS LockStatus
)
1471 return STATUS_NOT_IMPLEMENTED
;
1479 MountMgrDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
1482 PIO_STACK_LOCATION Stack
;
1483 NTSTATUS Status
, LockStatus
;
1484 PDEVICE_EXTENSION DeviceExtension
;
1486 Stack
= IoGetNextIrpStackLocation(Irp
);
1487 DeviceExtension
= DeviceObject
->DeviceExtension
;
1489 KeWaitForSingleObject(&(DeviceExtension
->DeviceLock
), Executive
, KernelMode
, FALSE
, NULL
);
1491 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
1493 case IOCTL_MOUNTMGR_CREATE_POINT
:
1494 Status
= MountMgrCreatePoint(DeviceExtension
, Irp
);
1497 case IOCTL_MOUNTMGR_DELETE_POINTS
:
1498 Status
= MountMgrDeletePoints(DeviceExtension
, Irp
);
1501 case IOCTL_MOUNTMGR_QUERY_POINTS
:
1502 Status
= MountMgrQueryPoints(DeviceExtension
, Irp
);
1505 case IOCTL_MOUNTMGR_DELETE_POINTS_DBONLY
:
1506 Status
= MountMgrDeletePointsDbOnly(DeviceExtension
, Irp
);
1509 case IOCTL_MOUNTMGR_NEXT_DRIVE_LETTER
:
1510 Status
= MountMgrNextDriveLetter(DeviceExtension
, Irp
);
1513 case IOCTL_MOUNTMGR_AUTO_DL_ASSIGNMENTS
:
1514 DeviceExtension
->AutomaticDriveLetter
= TRUE
;
1515 Status
= STATUS_SUCCESS
;
1517 MountMgrAssignDriveLetters(DeviceExtension
);
1518 ReconcileAllDatabasesWithMaster(DeviceExtension
);
1519 WaitForOnlinesToComplete(DeviceExtension
);
1522 case IOCTL_MOUNTMGR_VOLUME_MOUNT_POINT_CREATED
:
1523 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
1525 LockStatus
= WaitForRemoteDatabaseSemaphore(DeviceExtension
);
1526 KeWaitForSingleObject(&(DeviceExtension
->DeviceLock
), Executive
, KernelMode
, FALSE
, NULL
);
1527 Status
= MountMgrVolumeMountPointCreated(DeviceExtension
, Irp
, LockStatus
);
1528 if (NT_SUCCESS(LockStatus
))
1530 ReleaseRemoteDatabaseSemaphore(DeviceExtension
);
1535 case IOCTL_MOUNTMGR_VOLUME_MOUNT_POINT_DELETED
:
1536 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
1538 LockStatus
= WaitForRemoteDatabaseSemaphore(DeviceExtension
);
1539 KeWaitForSingleObject(&(DeviceExtension
->DeviceLock
), Executive
, KernelMode
, FALSE
, NULL
);
1540 Status
= MountMgrVolumeMountPointDeleted(DeviceExtension
, Irp
, LockStatus
);
1541 if (NT_SUCCESS(LockStatus
))
1543 ReleaseRemoteDatabaseSemaphore(DeviceExtension
);
1548 case IOCTL_MOUNTMGR_CHANGE_NOTIFY
:
1549 Status
= MountMgrChangeNotify(DeviceExtension
, Irp
);
1552 case IOCTL_MOUNTMGR_KEEP_LINKS_WHEN_OFFLINE
:
1553 Status
= MountMgrKeepLinksWhenOffline(DeviceExtension
, Irp
);
1556 case IOCTL_MOUNTMGR_CHECK_UNPROCESSED_VOLUMES
:
1557 Status
= MountMgrCheckUnprocessedVolumes(DeviceExtension
, Irp
);
1560 case IOCTL_MOUNTMGR_VOLUME_ARRIVAL_NOTIFICATION
:
1561 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
1562 Status
= MountMgrVolumeArrivalNotification(DeviceExtension
, Irp
);
1565 case IOCTL_MOUNTMGR_QUERY_DOS_VOLUME_PATH
:
1566 Status
= MountMgrQueryDosVolumePath(DeviceExtension
, Irp
);
1569 case IOCTL_MOUNTMGR_QUERY_DOS_VOLUME_PATHS
:
1570 Status
= MountMgrQueryDosVolumePaths(DeviceExtension
, Irp
);
1573 case IOCTL_MOUNTMGR_SCRUB_REGISTRY
:
1574 Status
= MountMgrScrubRegistry(DeviceExtension
);
1577 case IOCTL_MOUNTMGR_QUERY_AUTO_MOUNT
:
1578 Status
= MountMgrQueryAutoMount(DeviceExtension
, Irp
);
1581 case IOCTL_MOUNTMGR_SET_AUTO_MOUNT
:
1582 Status
= MountMgrSetAutoMount(DeviceExtension
, Irp
);
1586 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1589 KeReleaseSemaphore(&(DeviceExtension
->DeviceLock
), IO_NO_INCREMENT
, 1, FALSE
);
1591 if (Status
!= STATUS_PENDING
)
1599 Irp
->IoStatus
.Status
= Status
;
1600 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);