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 }, /* DEVICE_UNKNOWN */
47 { 0 }, /* DEVICE_HARDDISK */
48 {'h','d',0}, /* DEVICE_HARDDISK_VOL */
49 {'f','l','o','p','p','y',0}, /* DEVICE_FLOPPY */
50 {'c','d','r','o','m',0}, /* DEVICE_CDROM */
51 {'n','e','t','w','o','r','k',0}, /* DEVICE_NETWORK */
52 {'r','a','m','d','i','s','k',0} /* DEVICE_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 enum device_type type; /* drive type */
64 DEVICE_OBJECT *device; /* disk device allocated for this drive */
65 UNICODE_STRING name; /* device name */
66 UNICODE_STRING symlink; /* device symlink if any */
67 STORAGE_DEVICE_NUMBER devnum; /* device number info */
68 struct mount_point *dosdev; /* DosDevices mount point */
69 struct mount_point *volume; /* Volume{xxx} mount point */
70 char *unix_device; /* unix device path */
71 char *unix_mount; /* unix mount point path */
74 static struct list drives_list = LIST_INIT(drives_list);
76 static DRIVER_OBJECT *harddisk_driver;
78 static char *get_dosdevices_path( char **drive )
80 const char *config_dir = wine_get_config_dir();
81 size_t len = strlen(config_dir) + sizeof("/dosdevices/a::");
82 char *path = HeapAlloc( GetProcessHeap(), 0, len );
85 strcpy( path, config_dir );
86 strcat( path, "/dosdevices/a::" );
87 *drive = path + len - 4;
92 static char *strdupA( const char *str )
96 if (!str) return NULL;
97 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
101 /* read a Unix symlink; returned buffer must be freed by caller */
102 static char *read_symlink( const char *path )
109 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
111 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
114 ret = readlink( path, buffer, size );
117 RtlFreeHeap( GetProcessHeap(), 0, buffer );
125 RtlFreeHeap( GetProcessHeap(), 0, buffer );
130 /* send notification about a change to a given drive */
131 static void send_notify( int drive, int code )
134 DEV_BROADCAST_VOLUME info;
136 info.dbcv_size = sizeof(info);
137 info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
138 info.dbcv_reserved = 0;
139 info.dbcv_unitmask = 1 << drive;
140 info.dbcv_flags = DBTF_MEDIA;
141 result = BroadcastSystemMessageW( BSF_FORCEIFHUNG|BSF_QUERY, NULL,
142 WM_DEVICECHANGE, code, (LPARAM)&info );
145 /* create the disk device for a given drive */
146 static NTSTATUS create_disk_device( const char *udi, enum device_type type, struct dos_drive **drive_ret )
148 static const WCHAR harddiskvolW[] = {'\\','D','e','v','i','c','e',
149 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
150 static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
151 static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
152 static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
153 static const WCHAR ramdiskW[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
154 static const WCHAR physdriveW[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
158 const WCHAR *format = NULL;
160 DEVICE_OBJECT *dev_obj;
161 struct dos_drive *drive;
166 case DEVICE_HARDDISK:
167 case DEVICE_NETWORK: /* FIXME */
170 case DEVICE_HARDDISK_VOL:
171 format = harddiskvolW;
172 first = 1; /* harddisk volumes start counting from 1 */
185 name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
186 name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
187 for (i = first; i < 32; i++)
189 sprintfW( name.Buffer, format, i );
190 name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
191 status = IoCreateDevice( harddisk_driver, sizeof(*drive), &name, 0, 0, FALSE, &dev_obj );
192 if (status != STATUS_OBJECT_NAME_COLLISION) break;
196 drive = dev_obj->DeviceExtension;
198 drive->device = dev_obj;
201 drive->dosdev = NULL;
202 drive->volume = NULL;
203 drive->unix_device = NULL;
204 drive->unix_mount = NULL;
205 drive->symlink.Buffer = NULL;
208 if (!(drive->udi = HeapAlloc( GetProcessHeap(), 0, strlen(udi)+1 )))
210 RtlFreeUnicodeString( &name );
211 IoDeleteDevice( drive->device );
212 return STATUS_NO_MEMORY;
214 strcpy( drive->udi, udi );
220 drive->devnum.DeviceType = FILE_DEVICE_DISK;
221 drive->devnum.DeviceNumber = i;
222 drive->devnum.PartitionNumber = ~0u;
225 drive->devnum.DeviceType = FILE_DEVICE_CD_ROM;
226 drive->devnum.DeviceNumber = i;
227 drive->devnum.PartitionNumber = ~0u;
230 case DEVICE_HARDDISK:
231 case DEVICE_NETWORK: /* FIXME */
233 UNICODE_STRING symlink;
235 symlink.MaximumLength = sizeof(physdriveW) + 10 * sizeof(WCHAR);
236 if ((symlink.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, symlink.MaximumLength)))
238 sprintfW( symlink.Buffer, physdriveW, i );
239 symlink.Length = strlenW(symlink.Buffer) * sizeof(WCHAR);
240 if (!IoCreateSymbolicLink( &symlink, &name )) drive->symlink = symlink;
242 drive->devnum.DeviceType = FILE_DEVICE_DISK;
243 drive->devnum.DeviceNumber = i;
244 drive->devnum.PartitionNumber = 0;
247 case DEVICE_HARDDISK_VOL:
248 drive->devnum.DeviceType = FILE_DEVICE_DISK;
249 drive->devnum.DeviceNumber = 0;
250 drive->devnum.PartitionNumber = i;
253 list_add_tail( &drives_list, &drive->entry );
255 TRACE( "created device %s\n", debugstr_w(name.Buffer) );
259 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
260 RtlFreeUnicodeString( &name );
265 /* delete the disk device for a given drive */
266 static void delete_disk_device( struct dos_drive *drive )
268 TRACE( "deleting device %s\n", debugstr_w(drive->name.Buffer) );
269 list_remove( &drive->entry );
270 if (drive->dosdev) delete_mount_point( drive->dosdev );
271 if (drive->volume) delete_mount_point( drive->volume );
272 if (drive->symlink.Buffer)
274 IoDeleteSymbolicLink( &drive->symlink );
275 RtlFreeUnicodeString( &drive->symlink );
277 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_device );
278 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
279 RtlFreeHeap( GetProcessHeap(), 0, drive->udi );
280 RtlFreeUnicodeString( &drive->name );
281 IoDeleteDevice( drive->device );
284 /* set or change the drive letter for an existing drive */
285 static void set_drive_letter( struct dos_drive *drive, int letter )
288 unsigned int id_len = 0;
290 if (drive->drive == letter) return;
291 if (drive->dosdev) delete_mount_point( drive->dosdev );
292 if (drive->volume) delete_mount_point( drive->volume );
293 drive->drive = letter;
294 if (letter == -1) return;
295 if (drive->unix_mount)
297 id = drive->unix_mount;
298 id_len = strlen( drive->unix_mount ) + 1;
300 drive->dosdev = add_dosdev_mount_point( drive->device, &drive->name, letter, id, id_len );
301 drive->volume = add_volume_mount_point( drive->device, &drive->name, letter, id, id_len );
304 static inline int is_valid_device( struct stat *st )
306 #if defined(linux) || defined(__sun__)
307 return S_ISBLK( st->st_mode );
309 /* disks are char devices on *BSD */
310 return S_ISCHR( st->st_mode );
314 /* find or create a DOS drive for the corresponding device */
315 static int add_drive( const char *device, enum device_type type )
319 struct stat dev_st, drive_st;
320 int drive, first, last, avail = 0;
322 if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
324 if (!(path = get_dosdevices_path( &p ))) return -1;
326 memset( in_use, 0, sizeof(in_use) );
347 for (drive = first; drive < last; drive++)
349 if (in_use[drive]) continue; /* already checked */
351 if (stat( path, &drive_st ) == -1)
353 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
358 /* if mount point symlink doesn't exist either, it's available */
359 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
363 else in_use[drive] = 1;
368 if (!is_valid_device( &drive_st )) continue;
369 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
374 /* try to use the one we found */
377 if (symlink( device, path ) != -1) goto done;
378 /* failed, retry the search */
384 HeapFree( GetProcessHeap(), 0, path );
388 static BOOL set_unix_mount_point( struct dos_drive *drive, const char *mount_point )
391 BOOL modified = FALSE;
393 if (!(path = get_dosdevices_path( &p ))) return FALSE;
394 p[0] = 'a' + drive->drive;
397 if (mount_point && mount_point[0])
399 /* try to avoid unlinking if already set correctly */
400 if (!drive->unix_mount || strcmp( drive->unix_mount, mount_point ))
403 symlink( mount_point, path );
406 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
407 drive->unix_mount = strdupA( mount_point );
408 if (drive->dosdev) set_mount_point_id( drive->dosdev, mount_point, strlen(mount_point) + 1 );
409 if (drive->volume) set_mount_point_id( drive->volume, mount_point, strlen(mount_point) + 1 );
413 if (unlink( path ) != -1) modified = TRUE;
414 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
415 drive->unix_mount = NULL;
416 if (drive->dosdev) set_mount_point_id( drive->dosdev, NULL, 0 );
417 if (drive->volume) set_mount_point_id( drive->volume, NULL, 0 );
420 HeapFree( GetProcessHeap(), 0, path );
424 /* create devices for mapped drives */
425 static void create_drive_devices(void)
427 char *path, *p, *link, *device;
428 struct dos_drive *drive;
431 enum device_type drive_type;
432 WCHAR driveW[] = {'a',':',0};
434 if (!(path = get_dosdevices_path( &p ))) return;
435 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &drives_key )) drives_key = 0;
437 for (i = 0; i < MAX_DOS_DRIVES; i++)
441 if (!(link = read_symlink( path ))) continue;
443 device = read_symlink( path );
445 drive_type = i < 2 ? DEVICE_FLOPPY : DEVICE_HARDDISK_VOL;
449 DWORD j, type, size = sizeof(buffer);
452 if (!RegQueryValueExW( drives_key, driveW, NULL, &type, (BYTE *)buffer, &size ) &&
455 for (j = 0; j < sizeof(drive_types)/sizeof(drive_types[0]); j++)
456 if (drive_types[j][0] && !strcmpiW( buffer, drive_types[j] ))
461 if (drive_type == DEVICE_FLOPPY && i >= 2) drive_type = DEVICE_HARDDISK;
465 if (!create_disk_device( NULL, drive_type, &drive ))
467 drive->unix_mount = link;
468 drive->unix_device = device;
469 set_drive_letter( drive, i );
473 RtlFreeHeap( GetProcessHeap(), 0, link );
474 RtlFreeHeap( GetProcessHeap(), 0, device );
477 RegCloseKey( drives_key );
478 RtlFreeHeap( GetProcessHeap(), 0, path );
481 /* create a new dos drive */
482 NTSTATUS add_dos_device( int letter, const char *udi, const char *device,
483 const char *mount_point, enum device_type type )
485 struct dos_drive *drive, *next;
487 if (letter == -1) /* auto-assign a letter */
489 letter = add_drive( device, type );
490 if (letter == -1) return STATUS_OBJECT_NAME_COLLISION;
492 else /* simply reset the device symlink */
496 if (!(path = get_dosdevices_path( &p ))) return STATUS_NO_MEMORY;
499 if (device) symlink( device, path );
502 LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
504 if (udi && drive->udi && !strcmp( udi, drive->udi ))
506 if (type == drive->type) goto found;
507 delete_disk_device( drive );
510 if (drive->drive == letter) delete_disk_device( drive );
513 if (create_disk_device( udi, type, &drive )) return STATUS_NO_MEMORY;
516 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_device );
517 drive->unix_device = strdupA( device );
518 set_drive_letter( drive, letter );
519 set_unix_mount_point( drive, mount_point );
521 if (drive->drive != -1)
525 TRACE( "added device %c: udi %s for %s on %s type %u\n",
526 'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
527 wine_dbgstr_a(mount_point), type );
529 /* hack: force the drive type in the registry */
530 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
532 const WCHAR *type_name = drive_types[type];
533 WCHAR name[3] = {'a',':',0};
535 name[0] += drive->drive;
536 if (!type_name[0] && type == DEVICE_HARDDISK) type_name = drive_types[DEVICE_FLOPPY];
538 RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
539 (strlenW(type_name) + 1) * sizeof(WCHAR) );
541 RegDeleteValueW( hkey, name );
545 if (udi) send_notify( drive->drive, DBT_DEVICEARRIVAL );
547 return STATUS_SUCCESS;
550 /* remove an existing dos drive, by letter or udi */
551 NTSTATUS remove_dos_device( int letter, const char *udi )
554 struct dos_drive *drive;
556 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
558 if (letter != -1 && drive->drive != letter) continue;
561 if (!drive->udi) continue;
562 if (strcmp( udi, drive->udi )) continue;
565 if (drive->drive != -1)
567 BOOL modified = set_unix_mount_point( drive, NULL );
569 /* clear the registry key too */
570 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
572 WCHAR name[3] = {'a',':',0};
573 name[0] += drive->drive;
574 RegDeleteValueW( hkey, name );
578 if (modified && udi) send_notify( drive->drive, DBT_DEVICEREMOVECOMPLETE );
580 delete_disk_device( drive );
581 return STATUS_SUCCESS;
583 return STATUS_NO_SUCH_DEVICE;
586 /* query information about an existing dos drive, by letter or udi */
587 NTSTATUS query_dos_device( int letter, enum device_type *type,
588 const char **device, const char **mount_point )
590 struct dos_drive *drive;
592 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
594 if (drive->drive != letter) continue;
595 if (type) *type = drive->type;
596 if (device) *device = drive->unix_device;
597 if (mount_point) *mount_point = drive->unix_mount;
598 return STATUS_SUCCESS;
600 return STATUS_NO_SUCH_DEVICE;
603 /* handler for ioctls on the harddisk device */
604 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
606 IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
607 struct dos_drive *drive = device->DeviceExtension;
609 TRACE( "ioctl %x insize %u outsize %u\n",
610 irpsp->Parameters.DeviceIoControl.IoControlCode,
611 irpsp->Parameters.DeviceIoControl.InputBufferLength,
612 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
614 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
616 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
619 DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
621 info.Cylinders.QuadPart = 10000;
622 info.MediaType = (drive->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
623 info.TracksPerCylinder = 255;
624 info.SectorsPerTrack = 63;
625 info.BytesPerSector = 512;
626 memcpy( irp->MdlAddress->StartVa, &info, len );
627 irp->IoStatus.Information = len;
628 irp->IoStatus.u.Status = STATUS_SUCCESS;
631 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
633 DWORD len = min( sizeof(drive->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
635 memcpy( irp->MdlAddress->StartVa, &drive->devnum, len );
636 irp->IoStatus.Information = len;
637 irp->IoStatus.u.Status = STATUS_SUCCESS;
640 case IOCTL_CDROM_READ_TOC:
641 irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
644 FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
645 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
648 return irp->IoStatus.u.Status;
651 /* driver entry point for the harddisk driver */
652 NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
654 struct dos_drive *drive;
656 harddisk_driver = driver;
657 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
659 /* create a harddisk0 device that isn't assigned to any drive */
660 create_disk_device( "harddisk0 placeholder", DEVICE_HARDDISK, &drive );
662 create_drive_devices();
664 return STATUS_SUCCESS;