2 * Mount manager service implementation
4 * Copyright 2008 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
28 #define WIN32_NO_STATUS
37 #include "ddk/mountmgr.h"
38 #include "wine/library.h"
39 #include "wine/unicode.h"
40 #include "wine/list.h"
41 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr
);
47 #define MAX_DOS_DRIVES 26
49 /* extra info for disk devices, stored in DeviceExtension */
50 struct disk_device_info
52 STORAGE_DEVICE_NUMBER devnum
; /* device number info */
57 struct list entry
; /* entry in mount points list */
58 DEVICE_OBJECT
*device
; /* disk device */
59 UNICODE_STRING name
; /* device name */
60 UNICODE_STRING link
; /* DOS device symlink */
61 void *id
; /* device unique id */
65 static struct list mount_points_list
= LIST_INIT(mount_points_list
);
66 static HKEY mount_key
;
68 /* read a Unix symlink; returned buffer must be freed by caller */
69 static char *read_symlink( const char *path
)
76 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
78 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
81 ret
= readlink( path
, buffer
, size
);
84 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
92 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
97 static NTSTATUS
create_disk_device( DRIVER_OBJECT
*driver
, DWORD type
, DEVICE_OBJECT
**dev_obj
,
98 UNICODE_STRING
*device_name
)
100 static const WCHAR harddiskW
[] = {'\\','D','e','v','i','c','e',
101 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
102 static const WCHAR cdromW
[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
103 static const WCHAR floppyW
[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
109 struct disk_device_info
*info
;
113 case DRIVE_REMOVABLE
:
122 first
= 1; /* harddisk volumes start counting from 1 */
126 name
.MaximumLength
= (strlenW(format
) + 10) * sizeof(WCHAR
);
127 name
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, name
.MaximumLength
);
128 for (i
= first
; i
< 32; i
++)
130 sprintfW( name
.Buffer
, format
, i
);
131 name
.Length
= strlenW(name
.Buffer
) * sizeof(WCHAR
);
132 status
= IoCreateDevice( driver
, sizeof(*info
), &name
, 0, 0, FALSE
, dev_obj
);
133 if (status
!= STATUS_OBJECT_NAME_COLLISION
) break;
137 info
= (*dev_obj
)->DeviceExtension
;
141 case DRIVE_REMOVABLE
:
142 info
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
143 info
->devnum
.DeviceNumber
= i
;
144 info
->devnum
.PartitionNumber
= ~0u;
147 info
->devnum
.DeviceType
= FILE_DEVICE_CD_ROM
;
148 info
->devnum
.DeviceNumber
= i
;
149 info
->devnum
.PartitionNumber
= ~0u;
153 info
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
154 info
->devnum
.DeviceNumber
= 0;
155 info
->devnum
.PartitionNumber
= i
;
161 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name
.Buffer
), status
);
162 RtlFreeUnicodeString( &name
);
168 static void set_mount_point_id( struct mount_point
*mount
, const void *id
, unsigned int id_len
)
170 RtlFreeHeap( GetProcessHeap(), 0, mount
->id
);
171 mount
->id_len
= max( MIN_ID_LEN
, id_len
);
172 if ((mount
->id
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, mount
->id_len
)))
174 memcpy( mount
->id
, id
, id_len
);
175 RegSetValueExW( mount_key
, mount
->link
.Buffer
, 0, REG_BINARY
, mount
->id
, mount
->id_len
);
177 else mount
->id_len
= 0;
180 static struct mount_point
*add_mount_point( DEVICE_OBJECT
*device
, UNICODE_STRING
*device_name
,
181 const WCHAR
*link
, const void *id
, unsigned int id_len
)
183 struct mount_point
*mount
;
185 UINT len
= (strlenW(link
) + 1) * sizeof(WCHAR
) + device_name
->Length
+ sizeof(WCHAR
);
187 if (!(mount
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*mount
) + len
))) return NULL
;
189 str
= (WCHAR
*)(mount
+ 1);
190 strcpyW( str
, link
);
191 RtlInitUnicodeString( &mount
->link
, str
);
192 str
+= strlenW(str
) + 1;
193 memcpy( str
, device_name
->Buffer
, device_name
->Length
);
194 str
[device_name
->Length
/ sizeof(WCHAR
)] = 0;
195 mount
->name
.Buffer
= str
;
196 mount
->name
.Length
= device_name
->Length
;
197 mount
->name
.MaximumLength
= device_name
->Length
+ sizeof(WCHAR
);
198 mount
->device
= device
;
200 list_add_tail( &mount_points_list
, &mount
->entry
);
202 IoCreateSymbolicLink( &mount
->link
, device_name
);
203 set_mount_point_id( mount
, id
, id_len
);
205 TRACE( "created %s id %s for %s\n", debugstr_w(mount
->link
.Buffer
),
206 debugstr_a(mount
->id
), debugstr_w(mount
->name
.Buffer
) );
210 /* create the DosDevices mount point symlink for a new device */
211 static struct mount_point
*add_dosdev_mount_point( DEVICE_OBJECT
*device
, UNICODE_STRING
*device_name
,
212 int drive
, const void *id
, unsigned int id_len
)
214 static const WCHAR driveW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
215 WCHAR link
[sizeof(driveW
)];
217 sprintfW( link
, driveW
, 'A' + drive
);
218 return add_mount_point( device
, device_name
, link
, id
, id_len
);
221 /* create the Volume mount point symlink for a new device */
222 static struct mount_point
*add_volume_mount_point( DEVICE_OBJECT
*device
, UNICODE_STRING
*device_name
,
223 int drive
, const void *id
, unsigned int id_len
)
225 static const WCHAR volumeW
[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
226 '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
227 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
228 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
229 WCHAR link
[sizeof(volumeW
)];
232 memset( &guid
, 0, sizeof(guid
) ); /* FIXME */
233 guid
.Data4
[7] = 'A' + drive
;
234 sprintfW( link
, volumeW
, guid
.Data1
, guid
.Data2
, guid
.Data3
,
235 guid
.Data4
[0], guid
.Data4
[1], guid
.Data4
[2], guid
.Data4
[3],
236 guid
.Data4
[4], guid
.Data4
[5], guid
.Data4
[6], guid
.Data4
[7]);
237 return add_mount_point( device
, device_name
, link
, id
, id_len
);
240 /* check if a given mount point matches the requested specs */
241 static BOOL
matching_mount_point( const struct mount_point
*mount
, const MOUNTMGR_MOUNT_POINT
*spec
)
243 if (spec
->SymbolicLinkNameOffset
)
245 const WCHAR
*name
= (const WCHAR
*)((const char *)spec
+ spec
->SymbolicLinkNameOffset
);
246 if (spec
->SymbolicLinkNameLength
!= mount
->link
.Length
) return FALSE
;
247 if (memicmpW( name
, mount
->link
.Buffer
, mount
->link
.Length
/sizeof(WCHAR
)))
250 if (spec
->DeviceNameOffset
)
252 const WCHAR
*name
= (const WCHAR
*)((const char *)spec
+ spec
->DeviceNameOffset
);
253 if (spec
->DeviceNameLength
!= mount
->name
.Length
) return FALSE
;
254 if (memicmpW( name
, mount
->name
.Buffer
, mount
->name
.Length
/sizeof(WCHAR
)))
257 if (spec
->UniqueIdOffset
)
259 const void *id
= ((const char *)spec
+ spec
->UniqueIdOffset
);
260 if (spec
->UniqueIdLength
!= mount
->id_len
) return FALSE
;
261 if (memcmp( id
, mount
->id
, mount
->id_len
)) return FALSE
;
266 /* implementation of IOCTL_MOUNTMGR_QUERY_POINTS */
267 static NTSTATUS
query_mount_points( const void *in_buff
, SIZE_T insize
,
268 void *out_buff
, SIZE_T outsize
, IO_STATUS_BLOCK
*iosb
)
270 UINT count
, pos
, size
;
271 const MOUNTMGR_MOUNT_POINT
*input
= in_buff
;
272 MOUNTMGR_MOUNT_POINTS
*info
= out_buff
;
273 struct mount_point
*mount
;
276 if (input
->SymbolicLinkNameOffset
+ input
->SymbolicLinkNameLength
> insize
||
277 input
->UniqueIdOffset
+ input
->UniqueIdLength
> insize
||
278 input
->DeviceNameOffset
+ input
->DeviceNameLength
> insize
||
279 input
->SymbolicLinkNameOffset
+ input
->SymbolicLinkNameLength
< input
->SymbolicLinkNameOffset
||
280 input
->UniqueIdOffset
+ input
->UniqueIdLength
< input
->UniqueIdOffset
||
281 input
->DeviceNameOffset
+ input
->DeviceNameLength
< input
->DeviceNameOffset
)
282 return STATUS_INVALID_PARAMETER
;
285 LIST_FOR_EACH_ENTRY( mount
, &mount_points_list
, struct mount_point
, entry
)
287 if (!matching_mount_point( mount
, input
)) continue;
288 size
+= mount
->name
.Length
;
289 size
+= mount
->link
.Length
;
290 size
+= mount
->id_len
;
291 size
= (size
+ sizeof(WCHAR
) - 1) & ~(sizeof(WCHAR
) - 1);
294 pos
= FIELD_OFFSET( MOUNTMGR_MOUNT_POINTS
, MountPoints
[count
] );
299 if (size
>= sizeof(info
->Size
)) info
->Size
= size
;
300 iosb
->Information
= sizeof(info
->Size
);
301 return STATUS_MORE_ENTRIES
;
304 info
->NumberOfMountPoints
= count
;
306 LIST_FOR_EACH_ENTRY( mount
, &mount_points_list
, struct mount_point
, entry
)
308 if (!matching_mount_point( mount
, input
)) continue;
310 info
->MountPoints
[count
].DeviceNameOffset
= pos
;
311 info
->MountPoints
[count
].DeviceNameLength
= mount
->name
.Length
;
312 memcpy( (char *)out_buff
+ pos
, mount
->name
.Buffer
, mount
->name
.Length
);
313 pos
+= mount
->name
.Length
;
315 info
->MountPoints
[count
].SymbolicLinkNameOffset
= pos
;
316 info
->MountPoints
[count
].SymbolicLinkNameLength
= mount
->link
.Length
;
317 memcpy( (char *)out_buff
+ pos
, mount
->link
.Buffer
, mount
->link
.Length
);
318 pos
+= mount
->link
.Length
;
320 info
->MountPoints
[count
].UniqueIdOffset
= pos
;
321 info
->MountPoints
[count
].UniqueIdLength
= mount
->id_len
;
322 memcpy( (char *)out_buff
+ pos
, mount
->id
, mount
->id_len
);
323 pos
+= mount
->id_len
;
324 pos
= (pos
+ sizeof(WCHAR
) - 1) & ~(sizeof(WCHAR
) - 1);
328 iosb
->Information
= pos
;
329 return STATUS_SUCCESS
;
332 /* handler for ioctls on the mount manager device */
333 static NTSTATUS WINAPI
mountmgr_ioctl( DEVICE_OBJECT
*device
, IRP
*irp
)
335 IO_STACK_LOCATION
*irpsp
= irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
;
337 TRACE( "ioctl %x insize %u outsize %u\n",
338 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
,
339 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
340 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
342 switch(irpsp
->Parameters
.DeviceIoControl
.IoControlCode
)
344 case IOCTL_MOUNTMGR_QUERY_POINTS
:
345 if (irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(MOUNTMGR_MOUNT_POINT
))
346 return STATUS_INVALID_PARAMETER
;
347 irp
->IoStatus
.u
.Status
= query_mount_points( irpsp
->Parameters
.DeviceIoControl
.Type3InputBuffer
,
348 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
349 irp
->MdlAddress
->StartVa
,
350 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
,
354 FIXME( "ioctl %x not supported\n", irpsp
->Parameters
.DeviceIoControl
.IoControlCode
);
355 irp
->IoStatus
.u
.Status
= STATUS_NOT_SUPPORTED
;
358 return irp
->IoStatus
.u
.Status
;
361 /* handler for ioctls on the harddisk device */
362 static NTSTATUS WINAPI
harddisk_ioctl( DEVICE_OBJECT
*device
, IRP
*irp
)
364 IO_STACK_LOCATION
*irpsp
= irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
;
365 struct disk_device_info
*disk_info
= device
->DeviceExtension
;
367 TRACE( "ioctl %x insize %u outsize %u\n",
368 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
,
369 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
370 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
372 switch(irpsp
->Parameters
.DeviceIoControl
.IoControlCode
)
374 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
377 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
379 info
.Cylinders
.QuadPart
= 10000;
380 info
.MediaType
= (disk_info
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
381 info
.TracksPerCylinder
= 255;
382 info
.SectorsPerTrack
= 63;
383 info
.BytesPerSector
= 512;
384 memcpy( irp
->MdlAddress
->StartVa
, &info
, len
);
385 irp
->IoStatus
.Information
= len
;
386 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
389 case IOCTL_STORAGE_GET_DEVICE_NUMBER
:
391 DWORD len
= min( sizeof(disk_info
->devnum
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
393 memcpy( irp
->MdlAddress
->StartVa
, &disk_info
->devnum
, len
);
394 irp
->IoStatus
.Information
= len
;
395 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
398 case IOCTL_CDROM_READ_TOC
:
399 irp
->IoStatus
.u
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
402 FIXME( "unsupported ioctl %x\n", irpsp
->Parameters
.DeviceIoControl
.IoControlCode
);
403 irp
->IoStatus
.u
.Status
= STATUS_NOT_SUPPORTED
;
406 return irp
->IoStatus
.u
.Status
;
409 /* create mount points for mapped drives */
410 static void create_drive_mount_points( DRIVER_OBJECT
*driver
)
412 const char *config_dir
= wine_get_config_dir();
413 char *buffer
, *p
, *link
;
415 DEVICE_OBJECT
*device
;
416 UNICODE_STRING device_name
;
418 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
419 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
421 strcpy( buffer
, config_dir
);
422 strcat( buffer
, "/dosdevices/a:" );
423 p
= buffer
+ strlen(buffer
) - 2;
425 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++)
428 if (!(link
= read_symlink( buffer
))) continue;
429 if (!create_disk_device( driver
, DRIVE_FIXED
, &device
, &device_name
))
431 add_dosdev_mount_point( device
, &device_name
, i
, link
, strlen(link
) + 1 );
432 add_volume_mount_point( device
, &device_name
, i
, link
, strlen(link
) + 1 );
433 RtlFreeUnicodeString( &device_name
);
435 RtlFreeHeap( GetProcessHeap(), 0, link
);
437 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
441 /* driver entry point for the harddisk driver */
442 static NTSTATUS WINAPI
harddisk_driver_entry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
444 static const WCHAR mounted_devicesW
[] = {'S','y','s','t','e','m','\\',
445 'M','o','u','n','t','e','d','D','e','v','i','c','e','s',0};
446 static const WCHAR harddisk0W
[] = {'\\','D','e','v','i','c','e',
447 '\\','H','a','r','d','d','i','s','k','0',0};
448 static const WCHAR physdrive0W
[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','0',0};
450 UNICODE_STRING nameW
, linkW
;
451 DEVICE_OBJECT
*device
;
453 struct disk_device_info
*info
;
455 driver
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = harddisk_ioctl
;
457 RegCreateKeyExW( HKEY_LOCAL_MACHINE
, mounted_devicesW
, 0, NULL
,
458 REG_OPTION_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &mount_key
, NULL
);
460 RtlInitUnicodeString( &nameW
, harddisk0W
);
461 RtlInitUnicodeString( &linkW
, physdrive0W
);
462 if (!(status
= IoCreateDevice( driver
, sizeof(*info
), &nameW
, 0, 0, FALSE
, &device
)))
463 status
= IoCreateSymbolicLink( &linkW
, &nameW
);
466 FIXME( "failed to create device error %x\n", status
);
469 info
= device
->DeviceExtension
;
470 info
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
471 info
->devnum
.DeviceNumber
= 0;
472 info
->devnum
.PartitionNumber
= 0;
474 create_drive_mount_points( driver
);
479 /* main entry point for the mount point manager driver */
480 NTSTATUS WINAPI
DriverEntry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
482 static const WCHAR device_mountmgrW
[] = {'\\','D','e','v','i','c','e','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
483 static const WCHAR link_mountmgrW
[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
484 static const WCHAR harddiskW
[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
486 UNICODE_STRING nameW
, linkW
;
487 DEVICE_OBJECT
*device
;
490 TRACE( "%s\n", debugstr_w(path
->Buffer
) );
492 driver
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = mountmgr_ioctl
;
494 RtlInitUnicodeString( &nameW
, device_mountmgrW
);
495 RtlInitUnicodeString( &linkW
, link_mountmgrW
);
496 if (!(status
= IoCreateDevice( driver
, 0, &nameW
, 0, 0, FALSE
, &device
)))
497 status
= IoCreateSymbolicLink( &linkW
, &nameW
);
500 FIXME( "failed to create device error %x\n", status
);
505 initialize_diskarbitration();
507 RtlInitUnicodeString( &nameW
, harddiskW
);
508 status
= IoCreateDriver( &nameW
, harddisk_driver_entry
);