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
35 #include "ddk/mountmgr.h"
36 #include "wine/library.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr
);
42 #define MAX_DOS_DRIVES 26
43 #define MAX_MOUNT_POINTS (2 * MAX_DOS_DRIVES)
45 /* extra info for disk devices, stored in DeviceExtension */
46 struct disk_device_info
48 UNICODE_STRING name
; /* device name */
53 DEVICE_OBJECT
*device
;
54 UNICODE_STRING link
; /* DOS device symlink */
55 void *id
; /* device unique id */
59 static struct mount_point mount_points
[MAX_MOUNT_POINTS
];
60 static HKEY mount_key
;
62 static inline UNICODE_STRING
*get_device_name( DEVICE_OBJECT
*dev
)
64 return &((struct disk_device_info
*)dev
->DeviceExtension
)->name
;
67 /* read a Unix symlink; returned buffer must be freed by caller */
68 static char *read_symlink( const char *path
)
75 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
77 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
80 ret
= readlink( path
, buffer
, size
);
83 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
91 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
96 static NTSTATUS
create_disk_device( DRIVER_OBJECT
*driver
, DWORD type
, DEVICE_OBJECT
**dev_obj
)
98 static const WCHAR harddiskW
[] = {'\\','D','e','v','i','c','e',
99 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
100 static const WCHAR cdromW
[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
101 static const WCHAR floppyW
[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
107 struct disk_device_info
*info
;
111 case DRIVE_REMOVABLE
:
120 first
= 1; /* harddisk volumes start counting from 1 */
124 name
.MaximumLength
= (strlenW(format
) + 10) * sizeof(WCHAR
);
125 name
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, name
.MaximumLength
);
126 for (i
= first
; i
< 32; i
++)
128 sprintfW( name
.Buffer
, format
, i
);
129 name
.Length
= strlenW(name
.Buffer
) * sizeof(WCHAR
);
130 status
= IoCreateDevice( driver
, sizeof(*info
), &name
, 0, 0, FALSE
, dev_obj
);
131 if (status
!= STATUS_OBJECT_NAME_COLLISION
) break;
135 info
= (*dev_obj
)->DeviceExtension
;
140 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name
.Buffer
), status
);
141 RtlFreeUnicodeString( &name
);
147 static NTSTATUS
add_mount_point( DRIVER_OBJECT
*driver
, DWORD type
, int drive
,
148 const void *id
, unsigned int id_len
)
150 static const WCHAR driveW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
151 static const WCHAR volumeW
[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
152 '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
153 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
154 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
155 WCHAR
*drive_link
, *volume_link
;
159 struct mount_point
*mount_drive
= NULL
, *mount_volume
= NULL
;
161 /* find two free mount points */
163 for (i
= 0; i
< MAX_MOUNT_POINTS
; i
++)
165 if (mount_points
[i
].device
) continue;
168 mount_drive
= &mount_points
[i
];
171 mount_volume
= &mount_points
[i
];
174 if (!mount_volume
) return STATUS_NO_MEMORY
;
176 /* create the volume */
178 memset( &guid
, 0, sizeof(guid
) ); /* FIXME */
179 guid
.Data4
[7] = 'A' + drive
;
181 drive_link
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(driveW
) );
182 volume_link
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(volumeW
) );
183 sprintfW( drive_link
, driveW
, 'A' + drive
);
184 sprintfW( volume_link
, volumeW
, guid
.Data1
, guid
.Data2
, guid
.Data3
,
185 guid
.Data4
[0], guid
.Data4
[1], guid
.Data4
[2], guid
.Data4
[3],
186 guid
.Data4
[4], guid
.Data4
[5], guid
.Data4
[6], guid
.Data4
[7]);
188 RtlInitUnicodeString( &mount_drive
->link
, drive_link
);
189 RtlInitUnicodeString( &mount_volume
->link
, volume_link
);
190 status
= create_disk_device( driver
, type
, &mount_drive
->device
);
193 RtlFreeUnicodeString( &mount_drive
->link
);
194 RtlFreeUnicodeString( &mount_volume
->link
);
198 mount_volume
->device
= mount_drive
->device
; /* FIXME: incr ref count */
199 mount_drive
->id
= RtlAllocateHeap( GetProcessHeap(), 0, id_len
);
200 mount_drive
->id_len
= id_len
;
201 memcpy( mount_drive
->id
, id
, id_len
);
202 mount_volume
->id
= RtlAllocateHeap( GetProcessHeap(), 0, id_len
);
203 mount_volume
->id_len
= id_len
;
204 memcpy( mount_volume
->id
, id
, id_len
);
206 IoCreateSymbolicLink( &mount_drive
->link
, get_device_name(mount_drive
->device
) );
207 IoCreateSymbolicLink( &mount_volume
->link
, get_device_name(mount_volume
->device
) );
209 TRACE( "created device %s symlinks %s %s\n", debugstr_w(get_device_name(mount_drive
->device
)->Buffer
),
210 debugstr_w(mount_drive
->link
.Buffer
), debugstr_w(mount_volume
->link
.Buffer
) );
212 RegSetValueExW( mount_key
, mount_drive
->link
.Buffer
, 0, REG_BINARY
,
213 mount_drive
->id
, mount_drive
->id_len
);
214 RegSetValueExW( mount_key
, mount_volume
->link
.Buffer
, 0, REG_BINARY
,
215 mount_volume
->id
, mount_volume
->id_len
);
217 return STATUS_SUCCESS
;
220 /* handler for ioctls on the mount manager device */
221 static NTSTATUS WINAPI
mountmgr_ioctl( DEVICE_OBJECT
*device
, IRP
*irp
)
223 IO_STACK_LOCATION
*irpsp
= irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
;
225 TRACE( "ioctl %x insize %u outsize %u\n",
226 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
,
227 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
228 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
230 switch(irpsp
->Parameters
.DeviceIoControl
.IoControlCode
)
233 FIXME( "ioctl %x not supported\n", irpsp
->Parameters
.DeviceIoControl
.IoControlCode
);
234 irp
->IoStatus
.u
.Status
= STATUS_NOT_SUPPORTED
;
237 return irp
->IoStatus
.u
.Status
;
240 /* handler for ioctls on the harddisk device */
241 static NTSTATUS WINAPI
harddisk_ioctl( DEVICE_OBJECT
*device
, IRP
*irp
)
243 IO_STACK_LOCATION
*irpsp
= irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
;
245 TRACE( "ioctl %x insize %u outsize %u\n",
246 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
,
247 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
248 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
250 switch(irpsp
->Parameters
.DeviceIoControl
.IoControlCode
)
253 FIXME( "unsupported ioctl %x\n", irpsp
->Parameters
.DeviceIoControl
.IoControlCode
);
254 irp
->IoStatus
.u
.Status
= STATUS_NOT_SUPPORTED
;
257 return irp
->IoStatus
.u
.Status
;
260 /* create mount points for mapped drives */
261 static void create_drive_mount_points( DRIVER_OBJECT
*driver
)
263 const char *config_dir
= wine_get_config_dir();
264 char *buffer
, *p
, *link
;
267 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
268 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
270 strcpy( buffer
, config_dir
);
271 strcat( buffer
, "/dosdevices/a:" );
272 p
= buffer
+ strlen(buffer
) - 2;
274 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++)
277 if (!(link
= read_symlink( buffer
))) continue;
278 add_mount_point( driver
, DRIVE_FIXED
, i
, link
, strlen(link
) );
279 RtlFreeHeap( GetProcessHeap(), 0, link
);
281 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
285 /* driver entry point for the harddisk driver */
286 static NTSTATUS WINAPI
harddisk_driver_entry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
288 static const WCHAR mounted_devicesW
[] = {'S','y','s','t','e','m','\\',
289 'M','o','u','n','t','e','d','D','e','v','i','c','e','s',0};
290 static const WCHAR harddisk0W
[] = {'\\','D','e','v','i','c','e',
291 '\\','H','a','r','d','d','i','s','k','0',0};
292 static const WCHAR physdrive0W
[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','0',0};
294 UNICODE_STRING nameW
, linkW
;
295 DEVICE_OBJECT
*device
;
298 driver
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = harddisk_ioctl
;
300 RegCreateKeyExW( HKEY_LOCAL_MACHINE
, mounted_devicesW
, 0, NULL
,
301 REG_OPTION_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &mount_key
, NULL
);
303 RtlInitUnicodeString( &nameW
, harddisk0W
);
304 RtlInitUnicodeString( &linkW
, physdrive0W
);
305 if (!(status
= IoCreateDevice( driver
, 0, &nameW
, 0, 0, FALSE
, &device
)))
306 status
= IoCreateSymbolicLink( &linkW
, &nameW
);
309 FIXME( "failed to create device error %x\n", status
);
313 create_drive_mount_points( driver
);
318 /* main entry point for the mount point manager driver */
319 NTSTATUS WINAPI
DriverEntry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
321 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};
322 static const WCHAR link_mountmgrW
[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
323 static const WCHAR harddiskW
[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
325 UNICODE_STRING nameW
, linkW
;
326 DEVICE_OBJECT
*device
;
329 TRACE( "%s\n", debugstr_w(path
->Buffer
) );
331 driver
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = mountmgr_ioctl
;
333 RtlInitUnicodeString( &nameW
, device_mountmgrW
);
334 RtlInitUnicodeString( &linkW
, link_mountmgrW
);
335 if (!(status
= IoCreateDevice( driver
, 0, &nameW
, 0, 0, FALSE
, &device
)))
336 status
= IoCreateSymbolicLink( &linkW
, &nameW
);
339 FIXME( "failed to create device error %x\n", status
);
343 RtlInitUnicodeString( &nameW
, harddiskW
);
344 status
= IoCreateDriver( &nameW
, harddisk_driver_entry
);