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 enum device_type type; /* drive type */
61 DEVICE_OBJECT *dev_obj; /* disk device allocated for this volume */
62 UNICODE_STRING name; /* device name */
63 UNICODE_STRING symlink; /* device symlink if any */
64 STORAGE_DEVICE_NUMBER devnum; /* device number info */
65 char *unix_device; /* unix device path */
66 char *unix_mount; /* unix mount point path */
71 struct list entry; /* entry in volumes list */
72 struct disk_device *device; /* disk device */
73 char *udi; /* unique identifier for dynamic volumes */
74 GUID guid; /* volume uuid */
75 struct mount_point *mount; /* Volume{xxx} mount point */
80 struct list entry; /* entry in drives list */
81 struct volume *volume; /* volume for this drive */
82 int drive; /* drive letter (0 = A: etc.) */
83 struct mount_point *mount; /* DosDevices mount point */
86 static struct list drives_list = LIST_INIT(drives_list);
87 static struct list volumes_list = LIST_INIT(volumes_list);
89 static DRIVER_OBJECT *harddisk_driver;
91 static char *get_dosdevices_path( char **drive )
93 const char *config_dir = wine_get_config_dir();
94 size_t len = strlen(config_dir) + sizeof("/dosdevices/a::");
95 char *path = HeapAlloc( GetProcessHeap(), 0, len );
98 strcpy( path, config_dir );
99 strcat( path, "/dosdevices/a::" );
100 *drive = path + len - 4;
105 static char *strdupA( const char *str )
109 if (!str) return NULL;
110 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
114 static const GUID *get_default_uuid( int letter )
118 guid.Data4[7] = 'A' + letter;
122 /* read a Unix symlink; returned buffer must be freed by caller */
123 static char *read_symlink( const char *path )
130 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
132 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
135 ret = readlink( path, buffer, size );
138 RtlFreeHeap( GetProcessHeap(), 0, buffer );
146 RtlFreeHeap( GetProcessHeap(), 0, buffer );
151 /* update a symlink if it changed; return TRUE if updated */
152 static void update_symlink( const char *path, const char *dest, const char *orig_dest )
156 if (!orig_dest || strcmp( orig_dest, dest ))
159 symlink( dest, path );
165 /* send notification about a change to a given drive */
166 static void send_notify( int drive, int code )
169 DEV_BROADCAST_VOLUME info;
171 info.dbcv_size = sizeof(info);
172 info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
173 info.dbcv_reserved = 0;
174 info.dbcv_unitmask = 1 << drive;
175 info.dbcv_flags = DBTF_MEDIA;
176 result = BroadcastSystemMessageW( BSF_FORCEIFHUNG|BSF_QUERY, NULL,
177 WM_DEVICECHANGE, code, (LPARAM)&info );
180 /* create the disk device for a given volume */
181 static NTSTATUS create_disk_device( enum device_type type, struct disk_device **device_ret )
183 static const WCHAR harddiskvolW[] = {'\\','D','e','v','i','c','e',
184 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
185 static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
186 static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
187 static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
188 static const WCHAR ramdiskW[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
189 static const WCHAR physdriveW[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
193 const WCHAR *format = NULL;
195 DEVICE_OBJECT *dev_obj;
196 struct disk_device *device;
201 case DEVICE_HARDDISK:
202 case DEVICE_NETWORK: /* FIXME */
205 case DEVICE_HARDDISK_VOL:
206 format = harddiskvolW;
207 first = 1; /* harddisk volumes start counting from 1 */
220 name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
221 name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
222 for (i = first; i < 32; i++)
224 sprintfW( name.Buffer, format, i );
225 name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
226 status = IoCreateDevice( harddisk_driver, sizeof(*device), &name, 0, 0, FALSE, &dev_obj );
227 if (status != STATUS_OBJECT_NAME_COLLISION) break;
231 device = dev_obj->DeviceExtension;
232 device->dev_obj = dev_obj;
235 device->unix_device = NULL;
236 device->unix_mount = NULL;
237 device->symlink.Buffer = NULL;
243 device->devnum.DeviceType = FILE_DEVICE_DISK;
244 device->devnum.DeviceNumber = i;
245 device->devnum.PartitionNumber = ~0u;
248 device->devnum.DeviceType = FILE_DEVICE_CD_ROM;
249 device->devnum.DeviceNumber = i;
250 device->devnum.PartitionNumber = ~0u;
253 case DEVICE_HARDDISK:
254 case DEVICE_NETWORK: /* FIXME */
256 UNICODE_STRING symlink;
258 symlink.MaximumLength = sizeof(physdriveW) + 10 * sizeof(WCHAR);
259 if ((symlink.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, symlink.MaximumLength)))
261 sprintfW( symlink.Buffer, physdriveW, i );
262 symlink.Length = strlenW(symlink.Buffer) * sizeof(WCHAR);
263 if (!IoCreateSymbolicLink( &symlink, &name )) device->symlink = symlink;
265 device->devnum.DeviceType = FILE_DEVICE_DISK;
266 device->devnum.DeviceNumber = i;
267 device->devnum.PartitionNumber = 0;
270 case DEVICE_HARDDISK_VOL:
271 device->devnum.DeviceType = FILE_DEVICE_DISK;
272 device->devnum.DeviceNumber = 0;
273 device->devnum.PartitionNumber = i;
276 *device_ret = device;
277 TRACE( "created device %s\n", debugstr_w(name.Buffer) );
281 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
282 RtlFreeUnicodeString( &name );
287 /* delete the disk device for a given drive */
288 static void delete_disk_device( struct disk_device *device )
290 TRACE( "deleting device %s\n", debugstr_w(device->name.Buffer) );
291 if (device->symlink.Buffer)
293 IoDeleteSymbolicLink( &device->symlink );
294 RtlFreeUnicodeString( &device->symlink );
296 RtlFreeHeap( GetProcessHeap(), 0, device->unix_device );
297 RtlFreeHeap( GetProcessHeap(), 0, device->unix_mount );
298 RtlFreeUnicodeString( &device->name );
299 IoDeleteDevice( device->dev_obj );
302 /* create a disk volume */
303 static NTSTATUS create_volume( const char *udi, enum device_type type, struct volume **volume_ret )
305 struct volume *volume;
308 if (!(volume = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*volume) )))
309 return STATUS_NO_MEMORY;
311 if (udi && !(volume->udi = strdupA( udi )))
313 RtlFreeHeap( GetProcessHeap(), 0, volume );
314 return STATUS_NO_MEMORY;
316 if (!(status = create_disk_device( type, &volume->device )))
318 list_add_tail( &volumes_list, &volume->entry );
319 *volume_ret = volume;
323 RtlFreeHeap( GetProcessHeap(), 0, volume->udi );
324 RtlFreeHeap( GetProcessHeap(), 0, volume );
329 /* delete a volume and the corresponding disk device */
330 static void delete_volume( struct volume *volume )
332 list_remove( &volume->entry );
333 if (volume->mount) delete_mount_point( volume->mount );
334 delete_disk_device( volume->device );
335 RtlFreeHeap( GetProcessHeap(), 0, volume->udi );
336 RtlFreeHeap( GetProcessHeap(), 0, volume );
339 /* create the disk device for a given volume */
340 static NTSTATUS create_dos_device( const char *udi, int letter, enum device_type type,
341 struct dos_drive **drive_ret )
343 struct dos_drive *drive;
346 if (!(drive = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*drive) ))) return STATUS_NO_MEMORY;
347 drive->drive = letter;
350 if (!(status = create_volume( udi, type, &drive->volume )))
352 list_add_tail( &drives_list, &drive->entry );
355 else RtlFreeHeap( GetProcessHeap(), 0, drive );
360 /* delete the disk device for a given drive */
361 static void delete_dos_device( struct dos_drive *drive )
363 list_remove( &drive->entry );
364 if (drive->mount) delete_mount_point( drive->mount );
365 delete_volume( drive->volume );
366 RtlFreeHeap( GetProcessHeap(), 0, drive );
369 /* change the information for an existing volume */
370 static NTSTATUS set_volume_info( struct volume *volume, struct dos_drive *drive, const char *device,
371 const char *mount_point, enum device_type type, const GUID *guid )
374 unsigned int id_len = 0;
375 struct disk_device *disk_device = volume->device;
378 if (type != disk_device->type)
380 if ((status = create_disk_device( type, &disk_device ))) return status;
383 delete_mount_point( volume->mount );
384 volume->mount = NULL;
386 if (drive && drive->mount)
388 delete_mount_point( drive->mount );
391 delete_disk_device( volume->device );
392 volume->device = disk_device;
396 RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_device );
397 RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_mount );
399 disk_device->unix_device = strdupA( device );
400 disk_device->unix_mount = strdupA( mount_point );
402 if (memcmp( &volume->guid, guid, sizeof(volume->guid) ))
404 volume->guid = *guid;
407 delete_mount_point( volume->mount );
408 volume->mount = NULL;
413 volume->mount = add_volume_mount_point( disk_device->dev_obj, &disk_device->name, &volume->guid );
414 if (drive && !drive->mount)
415 drive->mount = add_dosdev_mount_point( disk_device->dev_obj, &disk_device->name, drive->drive );
417 if (disk_device->unix_mount)
419 id = disk_device->unix_mount;
420 id_len = strlen( disk_device->unix_mount ) + 1;
422 if (volume->mount) set_mount_point_id( volume->mount, id, id_len );
423 if (drive && drive->mount) set_mount_point_id( drive->mount, id, id_len );
425 return STATUS_SUCCESS;
428 /* set or change the drive letter for an existing drive */
429 static void set_drive_letter( struct dos_drive *drive, int letter )
431 struct volume *volume = drive->volume;
433 if (drive->drive == letter) return;
434 if (drive->mount) delete_mount_point( drive->mount );
435 if (volume->mount) delete_mount_point( volume->mount );
436 drive->drive = letter;
438 volume->mount = NULL;
441 static inline int is_valid_device( struct stat *st )
443 #if defined(linux) || defined(__sun__)
444 return S_ISBLK( st->st_mode );
446 /* disks are char devices on *BSD */
447 return S_ISCHR( st->st_mode );
451 /* find or create a DOS drive for the corresponding device */
452 static int add_drive( const char *device, enum device_type type )
456 struct stat dev_st, drive_st;
457 int drive, first, last, avail = 0;
459 if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
461 if (!(path = get_dosdevices_path( &p ))) return -1;
463 memset( in_use, 0, sizeof(in_use) );
484 for (drive = first; drive < last; drive++)
486 if (in_use[drive]) continue; /* already checked */
488 if (stat( path, &drive_st ) == -1)
490 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
495 /* if mount point symlink doesn't exist either, it's available */
496 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
500 else in_use[drive] = 1;
505 if (!is_valid_device( &drive_st )) continue;
506 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
511 /* try to use the one we found */
514 if (symlink( device, path ) != -1) goto done;
515 /* failed, retry the search */
521 HeapFree( GetProcessHeap(), 0, path );
525 /* create devices for mapped drives */
526 static void create_drive_devices(void)
528 char *path, *p, *link, *device;
529 struct dos_drive *drive;
532 enum device_type drive_type;
533 WCHAR driveW[] = {'a',':',0};
535 if (!(path = get_dosdevices_path( &p ))) return;
536 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &drives_key )) drives_key = 0;
538 for (i = 0; i < MAX_DOS_DRIVES; i++)
542 if (!(link = read_symlink( path ))) continue;
544 device = read_symlink( path );
546 drive_type = i < 2 ? DEVICE_FLOPPY : DEVICE_HARDDISK_VOL;
550 DWORD j, type, size = sizeof(buffer);
553 if (!RegQueryValueExW( drives_key, driveW, NULL, &type, (BYTE *)buffer, &size ) &&
556 for (j = 0; j < sizeof(drive_types)/sizeof(drive_types[0]); j++)
557 if (drive_types[j][0] && !strcmpiW( buffer, drive_types[j] ))
562 if (drive_type == DEVICE_FLOPPY && i >= 2) drive_type = DEVICE_HARDDISK;
566 if (!create_dos_device( NULL, i, drive_type, &drive ))
568 set_volume_info( drive->volume, drive, device, link, drive_type, get_default_uuid(i) );
572 RtlFreeHeap( GetProcessHeap(), 0, link );
573 RtlFreeHeap( GetProcessHeap(), 0, device );
576 RegCloseKey( drives_key );
577 RtlFreeHeap( GetProcessHeap(), 0, path );
580 /* create a new disk volume */
581 NTSTATUS add_volume( const char *udi, const char *device, const char *mount_point,
582 enum device_type type, const GUID *guid )
584 struct volume *volume;
587 TRACE( "adding %s device %s mount %s type %u uuid %s\n", debugstr_a(udi),
588 debugstr_a(device), debugstr_a(mount_point), type, debugstr_guid(guid) );
590 LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
591 if (volume->udi && !strcmp( udi, volume->udi )) goto found;
593 if ((status = create_volume( udi, type, &volume ))) return status;
596 return set_volume_info( volume, NULL, device, mount_point, type, guid );
599 /* create a new disk volume */
600 NTSTATUS remove_volume( const char *udi )
602 struct volume *volume;
604 LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
606 if (!volume->udi || strcmp( udi, volume->udi )) continue;
607 delete_volume( volume );
608 return STATUS_SUCCESS;
610 return STATUS_NO_SUCH_DEVICE;
614 /* create a new dos drive */
615 NTSTATUS add_dos_device( int letter, const char *udi, const char *device,
616 const char *mount_point, enum device_type type, const GUID *guid )
620 NTSTATUS status = STATUS_SUCCESS;
621 struct dos_drive *drive, *next;
623 if (!(path = get_dosdevices_path( &p ))) return STATUS_NO_MEMORY;
625 if (letter == -1) /* auto-assign a letter */
627 letter = add_drive( device, type );
630 status = STATUS_OBJECT_NAME_COLLISION;
634 LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
636 if (drive->volume->udi && !strcmp( udi, drive->volume->udi )) goto found;
637 if (drive->drive == letter) delete_dos_device( drive );
640 else /* simply reset the device symlink */
643 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
645 if (drive->drive != letter) continue;
646 update_symlink( path, device, drive->volume->device->unix_device );
649 update_symlink( path, device, NULL );
652 if ((status = create_dos_device( udi, letter, type, &drive ))) goto done;
655 if (!guid) guid = get_default_uuid( letter );
656 p[0] = 'a' + drive->drive;
658 update_symlink( path, mount_point, drive->volume->device->unix_mount );
659 set_drive_letter( drive, letter );
660 set_volume_info( drive->volume, drive, device, mount_point, type, guid );
662 TRACE( "added device %c: udi %s for %s on %s type %u\n",
663 'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
664 wine_dbgstr_a(mount_point), type );
666 /* hack: force the drive type in the registry */
667 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
669 const WCHAR *type_name = drive_types[type];
670 WCHAR name[3] = {'a',':',0};
672 name[0] += drive->drive;
673 if (!type_name[0] && type == DEVICE_HARDDISK) type_name = drive_types[DEVICE_FLOPPY];
675 RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
676 (strlenW(type_name) + 1) * sizeof(WCHAR) );
678 RegDeleteValueW( hkey, name );
682 if (udi) send_notify( drive->drive, DBT_DEVICEARRIVAL );
685 RtlFreeHeap( GetProcessHeap(), 0, path );
689 /* remove an existing dos drive, by letter or udi */
690 NTSTATUS remove_dos_device( int letter, const char *udi )
693 struct dos_drive *drive;
696 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
700 if (!drive->volume->udi) continue;
701 if (strcmp( udi, drive->volume->udi )) continue;
703 else if (drive->drive != letter) continue;
705 if ((path = get_dosdevices_path( &p )))
707 p[0] = 'a' + drive->drive;
710 RtlFreeHeap( GetProcessHeap(), 0, path );
713 /* clear the registry key too */
714 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
716 WCHAR name[3] = {'a',':',0};
717 name[0] += drive->drive;
718 RegDeleteValueW( hkey, name );
722 if (udi && drive->volume->device->unix_mount)
723 send_notify( drive->drive, DBT_DEVICEREMOVECOMPLETE );
725 delete_dos_device( drive );
726 return STATUS_SUCCESS;
728 return STATUS_NO_SUCH_DEVICE;
731 /* query information about an existing dos drive, by letter or udi */
732 NTSTATUS query_dos_device( int letter, enum device_type *type,
733 const char **device, const char **mount_point )
735 struct dos_drive *drive;
736 struct disk_device *disk_device;
738 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
740 if (drive->drive != letter) continue;
741 disk_device = drive->volume->device;
742 if (type) *type = disk_device->type;
743 if (device) *device = disk_device->unix_device;
744 if (mount_point) *mount_point = disk_device->unix_mount;
745 return STATUS_SUCCESS;
747 return STATUS_NO_SUCH_DEVICE;
750 /* handler for ioctls on the harddisk device */
751 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
753 IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
754 struct disk_device *dev = device->DeviceExtension;
756 TRACE( "ioctl %x insize %u outsize %u\n",
757 irpsp->Parameters.DeviceIoControl.IoControlCode,
758 irpsp->Parameters.DeviceIoControl.InputBufferLength,
759 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
761 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
763 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
766 DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
768 info.Cylinders.QuadPart = 10000;
769 info.MediaType = (dev->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
770 info.TracksPerCylinder = 255;
771 info.SectorsPerTrack = 63;
772 info.BytesPerSector = 512;
773 memcpy( irp->MdlAddress->StartVa, &info, len );
774 irp->IoStatus.Information = len;
775 irp->IoStatus.u.Status = STATUS_SUCCESS;
778 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
780 DWORD len = min( sizeof(dev->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
782 memcpy( irp->MdlAddress->StartVa, &dev->devnum, len );
783 irp->IoStatus.Information = len;
784 irp->IoStatus.u.Status = STATUS_SUCCESS;
787 case IOCTL_CDROM_READ_TOC:
788 irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
791 FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
792 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
795 return irp->IoStatus.u.Status;
798 /* driver entry point for the harddisk driver */
799 NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
801 struct disk_device *device;
803 harddisk_driver = driver;
804 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
806 /* create a harddisk0 device that isn't assigned to any drive */
807 create_disk_device( DEVICE_HARDDISK, &device );
809 create_drive_devices();
811 return STATUS_SUCCESS;