2 * Dynamic devices support
4 * Copyright 2006 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
22 #include "wine/port.h"
35 #include "wine/library.h"
36 #include "wine/list.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr
);
42 #define MAX_DOS_DRIVES 26
44 static const WCHAR drive_types
[][8] =
46 { 0 }, /* DRIVE_UNKNOWN */
47 { 0 }, /* DRIVE_NO_ROOT_DIR */
48 {'f','l','o','p','p','y',0}, /* DRIVE_REMOVABLE */
49 {'h','d',0}, /* DRIVE_FIXED */
50 {'n','e','t','w','o','r','k',0}, /* DRIVE_REMOTE */
51 {'c','d','r','o','m',0}, /* DRIVE_CDROM */
52 {'r','a','m','d','i','s','k',0} /* DRIVE_RAMDISK */
55 static const WCHAR drives_keyW
[] = {'S','o','f','t','w','a','r','e','\\',
56 'W','i','n','e','\\','D','r','i','v','e','s',0};
60 struct list entry
; /* entry in drives list */
61 char *udi
; /* unique identifier for dynamic drives */
62 int drive
; /* drive letter (0 = A: etc.) */
63 DWORD type
; /* drive type */
64 DEVICE_OBJECT
*device
; /* disk device allocated for this drive */
65 UNICODE_STRING name
; /* device name */
66 STORAGE_DEVICE_NUMBER devnum
; /* device number info */
67 struct mount_point
*dosdev
; /* DosDevices mount point */
68 struct mount_point
*volume
; /* Volume{xxx} mount point */
69 char *unix_mount
; /* unix mount point path */
72 static struct list drives_list
= LIST_INIT(drives_list
);
74 static DRIVER_OBJECT
*harddisk_driver
;
76 static char *get_dosdevices_path( char **drive
)
78 const char *config_dir
= wine_get_config_dir();
79 size_t len
= strlen(config_dir
) + sizeof("/dosdevices/a::");
80 char *path
= HeapAlloc( GetProcessHeap(), 0, len
);
83 strcpy( path
, config_dir
);
84 strcat( path
, "/dosdevices/a::" );
85 *drive
= path
+ len
- 4;
90 /* read a Unix symlink; returned buffer must be freed by caller */
91 static char *read_symlink( const char *path
)
98 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
100 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
103 ret
= readlink( path
, buffer
, size
);
106 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
114 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
119 /* send notification about a change to a given drive */
120 static void send_notify( int drive
, int code
)
123 DEV_BROADCAST_VOLUME info
;
125 info
.dbcv_size
= sizeof(info
);
126 info
.dbcv_devicetype
= DBT_DEVTYP_VOLUME
;
127 info
.dbcv_reserved
= 0;
128 info
.dbcv_unitmask
= 1 << drive
;
129 info
.dbcv_flags
= DBTF_MEDIA
;
130 result
= BroadcastSystemMessageW( BSF_FORCEIFHUNG
|BSF_QUERY
, NULL
,
131 WM_DEVICECHANGE
, code
, (LPARAM
)&info
);
134 /* create the disk device for a given drive */
135 static NTSTATUS
create_disk_device( const char *udi
, DWORD type
, struct dos_drive
**drive_ret
)
137 static const WCHAR harddiskvolW
[] = {'\\','D','e','v','i','c','e',
138 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
139 static const WCHAR harddiskW
[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
140 static const WCHAR cdromW
[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
141 static const WCHAR floppyW
[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
147 DEVICE_OBJECT
*dev_obj
;
148 struct dos_drive
*drive
;
152 case DRIVE_REMOVABLE
:
160 if (udi
) format
= harddiskW
;
163 format
= harddiskvolW
;
164 first
= 1; /* harddisk volumes start counting from 1 */
169 name
.MaximumLength
= (strlenW(format
) + 10) * sizeof(WCHAR
);
170 name
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, name
.MaximumLength
);
171 for (i
= first
; i
< 32; i
++)
173 sprintfW( name
.Buffer
, format
, i
);
174 name
.Length
= strlenW(name
.Buffer
) * sizeof(WCHAR
);
175 status
= IoCreateDevice( harddisk_driver
, sizeof(*drive
), &name
, 0, 0, FALSE
, &dev_obj
);
176 if (status
!= STATUS_OBJECT_NAME_COLLISION
) break;
180 drive
= dev_obj
->DeviceExtension
;
182 drive
->device
= dev_obj
;
185 drive
->dosdev
= NULL
;
186 drive
->volume
= NULL
;
187 drive
->unix_mount
= NULL
;
190 if (!(drive
->udi
= HeapAlloc( GetProcessHeap(), 0, strlen(udi
)+1 )))
192 RtlFreeUnicodeString( &name
);
193 IoDeleteDevice( drive
->device
);
194 return STATUS_NO_MEMORY
;
196 strcpy( drive
->udi
, udi
);
200 case DRIVE_REMOVABLE
:
201 drive
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
202 drive
->devnum
.DeviceNumber
= i
;
203 drive
->devnum
.PartitionNumber
= ~0u;
206 drive
->devnum
.DeviceType
= FILE_DEVICE_CD_ROM
;
207 drive
->devnum
.DeviceNumber
= i
;
208 drive
->devnum
.PartitionNumber
= ~0u;
212 drive
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
215 drive
->devnum
.DeviceNumber
= i
;
216 drive
->devnum
.PartitionNumber
= 0;
220 drive
->devnum
.DeviceNumber
= 0;
221 drive
->devnum
.PartitionNumber
= i
;
225 list_add_tail( &drives_list
, &drive
->entry
);
227 TRACE( "created device %s\n", debugstr_w(name
.Buffer
) );
231 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name
.Buffer
), status
);
232 RtlFreeUnicodeString( &name
);
237 /* delete the disk device for a given drive */
238 static void delete_disk_device( struct dos_drive
*drive
)
240 TRACE( "deleting device %s\n", debugstr_w(drive
->name
.Buffer
) );
241 list_remove( &drive
->entry
);
242 if (drive
->dosdev
) delete_mount_point( drive
->dosdev
);
243 if (drive
->volume
) delete_mount_point( drive
->volume
);
244 RtlFreeHeap( GetProcessHeap(), 0, drive
->unix_mount
);
245 RtlFreeHeap( GetProcessHeap(), 0, drive
->udi
);
246 RtlFreeUnicodeString( &drive
->name
);
247 IoDeleteDevice( drive
->device
);
250 /* set or change the drive letter for an existing drive */
251 static void set_drive_letter( struct dos_drive
*drive
, int letter
)
254 unsigned int id_len
= 0;
256 if (drive
->drive
== letter
) return;
257 if (drive
->dosdev
) delete_mount_point( drive
->dosdev
);
258 if (drive
->volume
) delete_mount_point( drive
->volume
);
259 drive
->drive
= letter
;
260 if (letter
== -1) return;
261 if (drive
->unix_mount
)
263 id
= drive
->unix_mount
;
264 id_len
= strlen( drive
->unix_mount
) + 1;
266 drive
->dosdev
= add_dosdev_mount_point( drive
->device
, &drive
->name
, letter
, id
, id_len
);
267 drive
->volume
= add_volume_mount_point( drive
->device
, &drive
->name
, letter
, id
, id_len
);
270 static inline int is_valid_device( struct stat
*st
)
272 #if defined(linux) || defined(__sun__)
273 return S_ISBLK( st
->st_mode
);
275 /* disks are char devices on *BSD */
276 return S_ISCHR( st
->st_mode
);
280 /* find or create a DOS drive for the corresponding device */
281 static int add_drive( const char *device
, DWORD type
)
285 struct stat dev_st
, drive_st
;
286 int drive
, first
, last
, avail
= 0;
288 if (stat( device
, &dev_st
) == -1 || !is_valid_device( &dev_st
)) return -1;
290 if (!(path
= get_dosdevices_path( &p
))) return -1;
292 memset( in_use
, 0, sizeof(in_use
) );
296 if (type
== DRIVE_REMOVABLE
)
305 for (drive
= first
; drive
< last
; drive
++)
307 if (in_use
[drive
]) continue; /* already checked */
309 if (stat( path
, &drive_st
) == -1)
311 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) /* this is a candidate */
316 /* if mount point symlink doesn't exist either, it's available */
317 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) avail
= drive
;
321 else in_use
[drive
] = 1;
326 if (!is_valid_device( &drive_st
)) continue;
327 if (dev_st
.st_rdev
== drive_st
.st_rdev
) goto done
;
332 /* try to use the one we found */
335 if (symlink( device
, path
) != -1) goto done
;
336 /* failed, retry the search */
342 HeapFree( GetProcessHeap(), 0, path
);
346 static BOOL
set_unix_mount_point( struct dos_drive
*drive
, const char *mount_point
)
349 BOOL modified
= FALSE
;
351 if (!(path
= get_dosdevices_path( &p
))) return FALSE
;
352 p
[0] = 'a' + drive
->drive
;
355 if (mount_point
&& mount_point
[0])
357 /* try to avoid unlinking if already set correctly */
358 if (!drive
->unix_mount
|| strcmp( drive
->unix_mount
, mount_point
))
361 symlink( mount_point
, path
);
364 RtlFreeHeap( GetProcessHeap(), 0, drive
->unix_mount
);
365 if ((drive
->unix_mount
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
) + 1 )))
366 strcpy( drive
->unix_mount
, mount_point
);
367 if (drive
->dosdev
) set_mount_point_id( drive
->dosdev
, mount_point
, strlen(mount_point
) + 1 );
368 if (drive
->volume
) set_mount_point_id( drive
->volume
, mount_point
, strlen(mount_point
) + 1 );
372 if (unlink( path
) != -1) modified
= TRUE
;
373 RtlFreeHeap( GetProcessHeap(), 0, drive
->unix_mount
);
374 drive
->unix_mount
= NULL
;
375 if (drive
->dosdev
) set_mount_point_id( drive
->dosdev
, NULL
, 0 );
376 if (drive
->volume
) set_mount_point_id( drive
->volume
, NULL
, 0 );
379 HeapFree( GetProcessHeap(), 0, path
);
383 /* create devices for mapped drives */
384 static void create_drive_devices(void)
386 char *path
, *p
, *link
;
387 struct dos_drive
*drive
;
391 WCHAR driveW
[] = {'a',':',0};
393 if (!(path
= get_dosdevices_path( &p
))) return;
394 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &drives_key
)) drives_key
= 0;
396 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++)
400 if (!(link
= read_symlink( path
))) continue;
402 drive_type
= i
< 2 ? DRIVE_REMOVABLE
: DRIVE_FIXED
;
406 DWORD j
, type
, size
= sizeof(buffer
);
409 if (!RegQueryValueExW( drives_key
, driveW
, NULL
, &type
, (BYTE
*)buffer
, &size
) &&
412 for (j
= 0; j
< sizeof(drive_types
)/sizeof(drive_types
[0]); j
++)
413 if (drive_types
[j
][0] && !strcmpiW( buffer
, drive_types
[j
] ))
421 if (!create_disk_device( NULL
, drive_type
, &drive
))
423 drive
->unix_mount
= link
;
424 set_drive_letter( drive
, i
);
426 else RtlFreeHeap( GetProcessHeap(), 0, link
);
428 RegCloseKey( drives_key
);
429 RtlFreeHeap( GetProcessHeap(), 0, path
);
432 BOOL
add_dos_device( const char *udi
, const char *device
, const char *mount_point
, DWORD type
)
434 struct dos_drive
*drive
, *next
;
435 int letter
= add_drive( device
, type
);
437 if (letter
== -1) return FALSE
;
439 LIST_FOR_EACH_ENTRY_SAFE( drive
, next
, &drives_list
, struct dos_drive
, entry
)
441 if (drive
->udi
&& !strcmp( udi
, drive
->udi
))
443 if (type
== drive
->type
) goto found
;
444 delete_disk_device( drive
);
447 if (drive
->drive
== letter
) delete_disk_device( drive
);
450 if (create_disk_device( udi
, type
, &drive
)) return FALSE
;
453 set_drive_letter( drive
, letter
);
454 set_unix_mount_point( drive
, mount_point
);
456 if (drive
->drive
!= -1)
460 TRACE( "added device %c: udi %s for %s on %s type %u\n",
461 'a' + drive
->drive
, wine_dbgstr_a(udi
), wine_dbgstr_a(device
),
462 wine_dbgstr_a(mount_point
), type
);
464 /* hack: force the drive type in the registry */
465 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
467 const WCHAR
*type_name
= drive_types
[type
];
468 WCHAR name
[3] = {'a',':',0};
470 name
[0] += drive
->drive
;
472 RegSetValueExW( hkey
, name
, 0, REG_SZ
, (const BYTE
*)type_name
,
473 (strlenW(type_name
) + 1) * sizeof(WCHAR
) );
475 RegDeleteValueW( hkey
, name
);
479 send_notify( drive
->drive
, DBT_DEVICEARRIVAL
);
484 BOOL
remove_dos_device( const char *udi
)
487 struct dos_drive
*drive
;
489 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
491 if (!drive
->udi
|| strcmp( udi
, drive
->udi
)) continue;
493 if (drive
->drive
!= -1)
495 BOOL modified
= set_unix_mount_point( drive
, NULL
);
497 /* clear the registry key too */
498 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
500 WCHAR name
[3] = {'a',':',0};
501 name
[0] += drive
->drive
;
502 RegDeleteValueW( hkey
, name
);
506 if (modified
) send_notify( drive
->drive
, DBT_DEVICEREMOVECOMPLETE
);
508 delete_disk_device( drive
);
514 /* handler for ioctls on the harddisk device */
515 static NTSTATUS WINAPI
harddisk_ioctl( DEVICE_OBJECT
*device
, IRP
*irp
)
517 IO_STACK_LOCATION
*irpsp
= irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
;
518 struct dos_drive
*drive
= device
->DeviceExtension
;
520 TRACE( "ioctl %x insize %u outsize %u\n",
521 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
,
522 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
523 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
525 switch(irpsp
->Parameters
.DeviceIoControl
.IoControlCode
)
527 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
530 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
532 info
.Cylinders
.QuadPart
= 10000;
533 info
.MediaType
= (drive
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
534 info
.TracksPerCylinder
= 255;
535 info
.SectorsPerTrack
= 63;
536 info
.BytesPerSector
= 512;
537 memcpy( irp
->MdlAddress
->StartVa
, &info
, len
);
538 irp
->IoStatus
.Information
= len
;
539 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
542 case IOCTL_STORAGE_GET_DEVICE_NUMBER
:
544 DWORD len
= min( sizeof(drive
->devnum
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
546 memcpy( irp
->MdlAddress
->StartVa
, &drive
->devnum
, len
);
547 irp
->IoStatus
.Information
= len
;
548 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
551 case IOCTL_CDROM_READ_TOC
:
552 irp
->IoStatus
.u
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
555 FIXME( "unsupported ioctl %x\n", irpsp
->Parameters
.DeviceIoControl
.IoControlCode
);
556 irp
->IoStatus
.u
.Status
= STATUS_NOT_SUPPORTED
;
559 return irp
->IoStatus
.u
.Status
;
562 /* driver entry point for the harddisk driver */
563 NTSTATUS WINAPI
harddisk_driver_entry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
565 static const WCHAR harddisk0W
[] = {'\\','D','e','v','i','c','e',
566 '\\','H','a','r','d','d','i','s','k','0',0};
567 static const WCHAR physdrive0W
[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','0',0};
569 UNICODE_STRING nameW
, linkW
;
570 DEVICE_OBJECT
*device
;
572 struct dos_drive
*drive
;
574 harddisk_driver
= driver
;
575 driver
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = harddisk_ioctl
;
577 RtlInitUnicodeString( &nameW
, harddisk0W
);
578 RtlInitUnicodeString( &linkW
, physdrive0W
);
579 if (!(status
= IoCreateDevice( driver
, sizeof(*drive
), &nameW
, 0, 0, FALSE
, &device
)))
580 status
= IoCreateSymbolicLink( &linkW
, &nameW
);
583 FIXME( "failed to create device error %x\n", status
);
586 drive
= device
->DeviceExtension
;
589 drive
->type
= DRIVE_FIXED
;
591 drive
->device
= device
;
592 drive
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
593 drive
->devnum
.DeviceNumber
= 0;
594 drive
->devnum
.PartitionNumber
= 0;
596 create_drive_devices();