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"
30 #define NONAMELESSUNION
37 #include "wine/library.h"
38 #include "wine/list.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr
);
44 #define MAX_DOS_DRIVES 26
46 static const WCHAR drive_types
[][8] =
48 { 0 }, /* DEVICE_UNKNOWN */
49 { 0 }, /* DEVICE_HARDDISK */
50 {'h','d',0}, /* DEVICE_HARDDISK_VOL */
51 {'f','l','o','p','p','y',0}, /* DEVICE_FLOPPY */
52 {'c','d','r','o','m',0}, /* DEVICE_CDROM */
53 {'c','d','r','o','m',0}, /* DEVICE_DVD */
54 {'n','e','t','w','o','r','k',0}, /* DEVICE_NETWORK */
55 {'r','a','m','d','i','s','k',0} /* DEVICE_RAMDISK */
58 static const WCHAR drives_keyW
[] = {'S','o','f','t','w','a','r','e','\\',
59 'W','i','n','e','\\','D','r','i','v','e','s',0};
63 enum device_type type
; /* drive type */
64 DEVICE_OBJECT
*dev_obj
; /* disk device allocated for this volume */
65 UNICODE_STRING name
; /* device name */
66 UNICODE_STRING symlink
; /* device symlink if any */
67 STORAGE_DEVICE_NUMBER devnum
; /* device number info */
68 char *unix_device
; /* unix device path */
69 char *unix_mount
; /* unix mount point path */
74 struct list entry
; /* entry in volumes list */
75 struct disk_device
*device
; /* disk device */
76 char *udi
; /* unique identifier for dynamic volumes */
77 unsigned int ref
; /* ref count */
78 GUID guid
; /* volume uuid */
79 struct mount_point
*mount
; /* Volume{xxx} mount point */
84 struct list entry
; /* entry in drives list */
85 struct volume
*volume
; /* volume for this drive */
86 int drive
; /* drive letter (0 = A: etc.) */
87 struct mount_point
*mount
; /* DosDevices mount point */
90 static struct list drives_list
= LIST_INIT(drives_list
);
91 static struct list volumes_list
= LIST_INIT(volumes_list
);
93 static DRIVER_OBJECT
*harddisk_driver
;
95 static CRITICAL_SECTION device_section
;
96 static CRITICAL_SECTION_DEBUG critsect_debug
=
98 0, 0, &device_section
,
99 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
100 0, 0, { (DWORD_PTR
)(__FILE__
": device_section") }
102 static CRITICAL_SECTION device_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
104 static char *get_dosdevices_path( char **drive
)
106 const char *config_dir
= wine_get_config_dir();
107 size_t len
= strlen(config_dir
) + sizeof("/dosdevices/a::");
108 char *path
= HeapAlloc( GetProcessHeap(), 0, len
);
111 strcpy( path
, config_dir
);
112 strcat( path
, "/dosdevices/a::" );
113 *drive
= path
+ len
- 4;
118 static char *strdupA( const char *str
)
122 if (!str
) return NULL
;
123 if ((ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(str
) + 1 ))) strcpy( ret
, str
);
127 static const GUID
*get_default_uuid( int letter
)
131 guid
.Data4
[7] = 'A' + letter
;
135 /* read a Unix symlink; returned buffer must be freed by caller */
136 static char *read_symlink( const char *path
)
143 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
145 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
148 ret
= readlink( path
, buffer
, size
);
151 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
159 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
164 /* update a symlink if it changed; return TRUE if updated */
165 static void update_symlink( const char *path
, const char *dest
, const char *orig_dest
)
169 if (!orig_dest
|| strcmp( orig_dest
, dest
))
172 symlink( dest
, path
);
178 /* send notification about a change to a given drive */
179 static void send_notify( int drive
, int code
)
181 DEV_BROADCAST_VOLUME info
;
183 info
.dbcv_size
= sizeof(info
);
184 info
.dbcv_devicetype
= DBT_DEVTYP_VOLUME
;
185 info
.dbcv_reserved
= 0;
186 info
.dbcv_unitmask
= 1 << drive
;
187 info
.dbcv_flags
= DBTF_MEDIA
;
188 BroadcastSystemMessageW( BSF_FORCEIFHUNG
|BSF_QUERY
, NULL
,
189 WM_DEVICECHANGE
, code
, (LPARAM
)&info
);
192 /* create the disk device for a given volume */
193 static NTSTATUS
create_disk_device( enum device_type type
, struct disk_device
**device_ret
)
195 static const WCHAR harddiskvolW
[] = {'\\','D','e','v','i','c','e',
196 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
197 static const WCHAR harddiskW
[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
198 static const WCHAR cdromW
[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
199 static const WCHAR floppyW
[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
200 static const WCHAR ramdiskW
[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
201 static const WCHAR cdromlinkW
[] = {'\\','?','?','\\','C','d','R','o','m','%','u',0};
202 static const WCHAR physdriveW
[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
206 const WCHAR
*format
= NULL
;
207 const WCHAR
*link_format
= NULL
;
209 DEVICE_OBJECT
*dev_obj
;
210 struct disk_device
*device
;
215 case DEVICE_HARDDISK
:
216 case DEVICE_NETWORK
: /* FIXME */
218 link_format
= physdriveW
;
220 case DEVICE_HARDDISK_VOL
:
221 format
= harddiskvolW
;
222 first
= 1; /* harddisk volumes start counting from 1 */
230 link_format
= cdromlinkW
;
237 name
.MaximumLength
= (strlenW(format
) + 10) * sizeof(WCHAR
);
238 name
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, name
.MaximumLength
);
239 for (i
= first
; i
< 32; i
++)
241 sprintfW( name
.Buffer
, format
, i
);
242 name
.Length
= strlenW(name
.Buffer
) * sizeof(WCHAR
);
243 status
= IoCreateDevice( harddisk_driver
, sizeof(*device
), &name
, 0, 0, FALSE
, &dev_obj
);
244 if (status
!= STATUS_OBJECT_NAME_COLLISION
) break;
248 device
= dev_obj
->DeviceExtension
;
249 device
->dev_obj
= dev_obj
;
252 device
->unix_device
= NULL
;
253 device
->unix_mount
= NULL
;
254 device
->symlink
.Buffer
= NULL
;
258 UNICODE_STRING symlink
;
260 symlink
.MaximumLength
= (strlenW(link_format
) + 10) * sizeof(WCHAR
);
261 if ((symlink
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, symlink
.MaximumLength
)))
263 sprintfW( symlink
.Buffer
, link_format
, i
);
264 symlink
.Length
= strlenW(symlink
.Buffer
) * sizeof(WCHAR
);
265 if (!IoCreateSymbolicLink( &symlink
, &name
)) device
->symlink
= symlink
;
273 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
274 device
->devnum
.DeviceNumber
= i
;
275 device
->devnum
.PartitionNumber
= ~0u;
278 device
->devnum
.DeviceType
= FILE_DEVICE_CD_ROM
;
279 device
->devnum
.DeviceNumber
= i
;
280 device
->devnum
.PartitionNumber
= ~0u;
283 device
->devnum
.DeviceType
= FILE_DEVICE_DVD
;
284 device
->devnum
.DeviceNumber
= i
;
285 device
->devnum
.PartitionNumber
= ~0u;
288 case DEVICE_HARDDISK
:
289 case DEVICE_NETWORK
: /* FIXME */
290 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
291 device
->devnum
.DeviceNumber
= i
;
292 device
->devnum
.PartitionNumber
= 0;
294 case DEVICE_HARDDISK_VOL
:
295 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
296 device
->devnum
.DeviceNumber
= 0;
297 device
->devnum
.PartitionNumber
= i
;
300 *device_ret
= device
;
301 TRACE( "created device %s\n", debugstr_w(name
.Buffer
) );
305 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name
.Buffer
), status
);
306 RtlFreeUnicodeString( &name
);
311 /* delete the disk device for a given drive */
312 static void delete_disk_device( struct disk_device
*device
)
314 TRACE( "deleting device %s\n", debugstr_w(device
->name
.Buffer
) );
315 if (device
->symlink
.Buffer
)
317 IoDeleteSymbolicLink( &device
->symlink
);
318 RtlFreeUnicodeString( &device
->symlink
);
320 RtlFreeHeap( GetProcessHeap(), 0, device
->unix_device
);
321 RtlFreeHeap( GetProcessHeap(), 0, device
->unix_mount
);
322 RtlFreeUnicodeString( &device
->name
);
323 IoDeleteDevice( device
->dev_obj
);
326 /* grab another reference to a volume */
327 static struct volume
*grab_volume( struct volume
*volume
)
333 /* release a volume and delete the corresponding disk device when refcount is 0 */
334 static unsigned int release_volume( struct volume
*volume
)
336 unsigned int ret
= --volume
->ref
;
340 TRACE( "%s udi %s\n", debugstr_guid(&volume
->guid
), debugstr_a(volume
->udi
) );
341 assert( !volume
->udi
);
342 list_remove( &volume
->entry
);
343 if (volume
->mount
) delete_mount_point( volume
->mount
);
344 delete_disk_device( volume
->device
);
345 RtlFreeHeap( GetProcessHeap(), 0, volume
);
350 /* set the volume udi */
351 static void set_volume_udi( struct volume
*volume
, const char *udi
)
355 assert( !volume
->udi
);
356 /* having a udi means the HAL side holds an extra reference */
357 if ((volume
->udi
= strdupA( udi
))) grab_volume( volume
);
359 else if (volume
->udi
)
361 RtlFreeHeap( GetProcessHeap(), 0, volume
->udi
);
363 release_volume( volume
);
367 /* create a disk volume */
368 static NTSTATUS
create_volume( const char *udi
, enum device_type type
, struct volume
**volume_ret
)
370 struct volume
*volume
;
373 if (!(volume
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*volume
) )))
374 return STATUS_NO_MEMORY
;
376 if (!(status
= create_disk_device( type
, &volume
->device
)))
378 if (udi
) set_volume_udi( volume
, udi
);
379 list_add_tail( &volumes_list
, &volume
->entry
);
380 *volume_ret
= grab_volume( volume
);
382 else RtlFreeHeap( GetProcessHeap(), 0, volume
);
387 /* create the disk device for a given volume */
388 static NTSTATUS
create_dos_device( struct volume
*volume
, const char *udi
, int letter
,
389 enum device_type type
, struct dos_drive
**drive_ret
)
391 struct dos_drive
*drive
;
394 if (!(drive
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*drive
) ))) return STATUS_NO_MEMORY
;
395 drive
->drive
= letter
;
400 if (udi
) set_volume_udi( volume
, udi
);
401 drive
->volume
= grab_volume( volume
);
402 status
= STATUS_SUCCESS
;
404 else status
= create_volume( udi
, type
, &drive
->volume
);
406 if (status
== STATUS_SUCCESS
)
408 list_add_tail( &drives_list
, &drive
->entry
);
411 else RtlFreeHeap( GetProcessHeap(), 0, drive
);
416 /* delete the disk device for a given drive */
417 static void delete_dos_device( struct dos_drive
*drive
)
419 list_remove( &drive
->entry
);
420 if (drive
->mount
) delete_mount_point( drive
->mount
);
421 release_volume( drive
->volume
);
422 RtlFreeHeap( GetProcessHeap(), 0, drive
);
425 /* find a volume that matches the parameters */
426 static struct volume
*find_matching_volume( const char *udi
, const char *device
,
427 const char *mount_point
, enum device_type type
)
429 struct volume
*volume
;
430 struct disk_device
*disk_device
;
432 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
436 /* when we have a udi we only match drives added manually */
437 if (udi
&& volume
->udi
) continue;
438 /* and when we don't have a udi we only match dynamic drives */
439 if (!udi
&& !volume
->udi
) continue;
441 disk_device
= volume
->device
;
442 if (disk_device
->type
!= type
) continue;
443 if (device
&& disk_device
->unix_device
)
445 if (strcmp( device
, disk_device
->unix_device
)) continue;
448 if (mount_point
&& disk_device
->unix_mount
)
450 if (strcmp( mount_point
, disk_device
->unix_mount
)) continue;
453 if (!match
) continue;
454 TRACE( "found matching volume %s for device %s mount %s type %u\n",
455 debugstr_guid(&volume
->guid
), debugstr_a(device
), debugstr_a(mount_point
), type
);
456 return grab_volume( volume
);
461 /* change the information for an existing volume */
462 static NTSTATUS
set_volume_info( struct volume
*volume
, struct dos_drive
*drive
, const char *device
,
463 const char *mount_point
, enum device_type type
, const GUID
*guid
)
466 unsigned int id_len
= 0;
467 struct disk_device
*disk_device
= volume
->device
;
470 if (type
!= disk_device
->type
)
472 if ((status
= create_disk_device( type
, &disk_device
))) return status
;
475 delete_mount_point( volume
->mount
);
476 volume
->mount
= NULL
;
478 if (drive
&& drive
->mount
)
480 delete_mount_point( drive
->mount
);
483 delete_disk_device( volume
->device
);
484 volume
->device
= disk_device
;
488 RtlFreeHeap( GetProcessHeap(), 0, disk_device
->unix_device
);
489 RtlFreeHeap( GetProcessHeap(), 0, disk_device
->unix_mount
);
491 disk_device
->unix_device
= strdupA( device
);
492 disk_device
->unix_mount
= strdupA( mount_point
);
494 if (guid
&& memcmp( &volume
->guid
, guid
, sizeof(volume
->guid
) ))
496 volume
->guid
= *guid
;
499 delete_mount_point( volume
->mount
);
500 volume
->mount
= NULL
;
505 volume
->mount
= add_volume_mount_point( disk_device
->dev_obj
, &disk_device
->name
, &volume
->guid
);
506 if (drive
&& !drive
->mount
)
507 drive
->mount
= add_dosdev_mount_point( disk_device
->dev_obj
, &disk_device
->name
, drive
->drive
);
509 if (disk_device
->unix_mount
)
511 id
= disk_device
->unix_mount
;
512 id_len
= strlen( disk_device
->unix_mount
) + 1;
514 if (volume
->mount
) set_mount_point_id( volume
->mount
, id
, id_len
);
515 if (drive
&& drive
->mount
) set_mount_point_id( drive
->mount
, id
, id_len
);
517 return STATUS_SUCCESS
;
520 /* change the drive letter or volume for an existing drive */
521 static void set_drive_info( struct dos_drive
*drive
, int letter
, struct volume
*volume
)
523 if (drive
->drive
!= letter
)
525 if (drive
->mount
) delete_mount_point( drive
->mount
);
527 drive
->drive
= letter
;
529 if (drive
->volume
!= volume
)
531 if (drive
->mount
) delete_mount_point( drive
->mount
);
533 grab_volume( volume
);
534 release_volume( drive
->volume
);
535 drive
->volume
= volume
;
539 static inline BOOL
is_valid_device( struct stat
*st
)
541 #if defined(linux) || defined(__sun__)
542 return S_ISBLK( st
->st_mode
);
544 /* disks are char devices on *BSD */
545 return S_ISCHR( st
->st_mode
);
549 /* find or create a DOS drive for the corresponding device */
550 static int add_drive( const char *device
, enum device_type type
)
554 struct stat dev_st
, drive_st
;
555 int drive
, first
, last
, avail
= 0;
557 if (stat( device
, &dev_st
) == -1 || !is_valid_device( &dev_st
)) return -1;
559 if (!(path
= get_dosdevices_path( &p
))) return -1;
561 memset( in_use
, 0, sizeof(in_use
) );
583 for (drive
= first
; drive
< last
; drive
++)
585 if (in_use
[drive
]) continue; /* already checked */
587 if (stat( path
, &drive_st
) == -1)
589 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) /* this is a candidate */
594 /* if mount point symlink doesn't exist either, it's available */
595 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) avail
= drive
;
599 else in_use
[drive
] = 1;
604 if (!is_valid_device( &drive_st
)) continue;
605 if (dev_st
.st_rdev
== drive_st
.st_rdev
) goto done
;
610 /* try to use the one we found */
613 if (symlink( device
, path
) != -1) goto done
;
614 /* failed, retry the search */
620 HeapFree( GetProcessHeap(), 0, path
);
624 /* create devices for mapped drives */
625 static void create_drive_devices(void)
627 char *path
, *p
, *link
, *device
;
628 struct dos_drive
*drive
;
629 struct volume
*volume
;
632 enum device_type drive_type
;
633 WCHAR driveW
[] = {'a',':',0};
635 if (!(path
= get_dosdevices_path( &p
))) return;
636 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &drives_key
)) drives_key
= 0;
638 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++)
642 if (!(link
= read_symlink( path
))) continue;
644 device
= read_symlink( path
);
646 drive_type
= i
< 2 ? DEVICE_FLOPPY
: DEVICE_HARDDISK_VOL
;
650 DWORD j
, type
, size
= sizeof(buffer
);
653 if (!RegQueryValueExW( drives_key
, driveW
, NULL
, &type
, (BYTE
*)buffer
, &size
) &&
656 for (j
= 0; j
< sizeof(drive_types
)/sizeof(drive_types
[0]); j
++)
657 if (drive_types
[j
][0] && !strcmpiW( buffer
, drive_types
[j
] ))
662 if (drive_type
== DEVICE_FLOPPY
&& i
>= 2) drive_type
= DEVICE_HARDDISK
;
666 volume
= find_matching_volume( NULL
, device
, link
, drive_type
);
667 if (!create_dos_device( volume
, NULL
, i
, drive_type
, &drive
))
669 /* don't reset uuid if we used an existing volume */
670 const GUID
*guid
= volume
? NULL
: get_default_uuid(i
);
671 set_volume_info( drive
->volume
, drive
, device
, link
, drive_type
, guid
);
675 RtlFreeHeap( GetProcessHeap(), 0, link
);
676 RtlFreeHeap( GetProcessHeap(), 0, device
);
678 if (volume
) release_volume( volume
);
680 RegCloseKey( drives_key
);
681 RtlFreeHeap( GetProcessHeap(), 0, path
);
684 /* create a new disk volume */
685 NTSTATUS
add_volume( const char *udi
, const char *device
, const char *mount_point
,
686 enum device_type type
, const GUID
*guid
)
688 struct volume
*volume
;
689 NTSTATUS status
= STATUS_SUCCESS
;
691 TRACE( "adding %s device %s mount %s type %u uuid %s\n", debugstr_a(udi
),
692 debugstr_a(device
), debugstr_a(mount_point
), type
, debugstr_guid(guid
) );
694 EnterCriticalSection( &device_section
);
695 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
696 if (volume
->udi
&& !strcmp( udi
, volume
->udi
))
698 grab_volume( volume
);
702 /* udi not found, search for a non-dynamic volume */
703 if ((volume
= find_matching_volume( udi
, device
, mount_point
, type
))) set_volume_udi( volume
, udi
);
704 else status
= create_volume( udi
, type
, &volume
);
707 if (!status
) status
= set_volume_info( volume
, NULL
, device
, mount_point
, type
, guid
);
708 if (volume
) release_volume( volume
);
709 LeaveCriticalSection( &device_section
);
713 /* create a new disk volume */
714 NTSTATUS
remove_volume( const char *udi
)
716 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
717 struct volume
*volume
;
719 EnterCriticalSection( &device_section
);
720 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
722 if (!volume
->udi
|| strcmp( udi
, volume
->udi
)) continue;
723 set_volume_udi( volume
, NULL
);
724 status
= STATUS_SUCCESS
;
727 LeaveCriticalSection( &device_section
);
732 /* create a new dos drive */
733 NTSTATUS
add_dos_device( int letter
, const char *udi
, const char *device
,
734 const char *mount_point
, enum device_type type
, const GUID
*guid
)
738 NTSTATUS status
= STATUS_SUCCESS
;
739 struct dos_drive
*drive
, *next
;
740 struct volume
*volume
;
743 if (!(path
= get_dosdevices_path( &p
))) return STATUS_NO_MEMORY
;
745 EnterCriticalSection( &device_section
);
746 volume
= find_matching_volume( udi
, device
, mount_point
, type
);
748 if (letter
== -1) /* auto-assign a letter */
750 letter
= add_drive( device
, type
);
753 status
= STATUS_OBJECT_NAME_COLLISION
;
757 LIST_FOR_EACH_ENTRY_SAFE( drive
, next
, &drives_list
, struct dos_drive
, entry
)
759 if (drive
->volume
->udi
&& !strcmp( udi
, drive
->volume
->udi
)) goto found
;
760 if (drive
->drive
== letter
) delete_dos_device( drive
);
763 else /* simply reset the device symlink */
765 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
766 if (drive
->drive
== letter
) break;
769 if (&drive
->entry
== &drives_list
) update_symlink( path
, device
, NULL
);
772 update_symlink( path
, device
, drive
->volume
->device
->unix_device
);
773 delete_dos_device( drive
);
777 if ((status
= create_dos_device( volume
, udi
, letter
, type
, &drive
))) goto done
;
780 if (!guid
&& !volume
) guid
= get_default_uuid( letter
);
781 if (!volume
) volume
= grab_volume( drive
->volume
);
782 set_drive_info( drive
, letter
, volume
);
783 p
[0] = 'a' + drive
->drive
;
785 update_symlink( path
, mount_point
, volume
->device
->unix_mount
);
786 set_volume_info( volume
, drive
, device
, mount_point
, type
, guid
);
788 TRACE( "added device %c: udi %s for %s on %s type %u\n",
789 'a' + drive
->drive
, wine_dbgstr_a(udi
), wine_dbgstr_a(device
),
790 wine_dbgstr_a(mount_point
), type
);
792 /* hack: force the drive type in the registry */
793 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
795 const WCHAR
*type_name
= drive_types
[type
];
796 WCHAR name
[] = {'a',':',0};
798 name
[0] += drive
->drive
;
799 if (!type_name
[0] && type
== DEVICE_HARDDISK
) type_name
= drive_types
[DEVICE_FLOPPY
];
801 RegSetValueExW( hkey
, name
, 0, REG_SZ
, (const BYTE
*)type_name
,
802 (strlenW(type_name
) + 1) * sizeof(WCHAR
) );
804 RegDeleteValueW( hkey
, name
);
808 if (udi
) notify
= drive
->drive
;
811 if (volume
) release_volume( volume
);
812 LeaveCriticalSection( &device_section
);
813 RtlFreeHeap( GetProcessHeap(), 0, path
);
814 if (notify
!= -1) send_notify( notify
, DBT_DEVICEARRIVAL
);
818 /* remove an existing dos drive, by letter or udi */
819 NTSTATUS
remove_dos_device( int letter
, const char *udi
)
821 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
823 struct dos_drive
*drive
;
827 EnterCriticalSection( &device_section
);
828 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
832 if (!drive
->volume
->udi
) continue;
833 if (strcmp( udi
, drive
->volume
->udi
)) continue;
834 set_volume_udi( drive
->volume
, NULL
);
836 else if (drive
->drive
!= letter
) continue;
838 if ((path
= get_dosdevices_path( &p
)))
840 p
[0] = 'a' + drive
->drive
;
843 RtlFreeHeap( GetProcessHeap(), 0, path
);
846 /* clear the registry key too */
847 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
849 WCHAR name
[] = {'a',':',0};
850 name
[0] += drive
->drive
;
851 RegDeleteValueW( hkey
, name
);
855 if (udi
&& drive
->volume
->device
->unix_mount
) notify
= drive
->drive
;
857 delete_dos_device( drive
);
858 status
= STATUS_SUCCESS
;
861 LeaveCriticalSection( &device_section
);
862 if (notify
!= -1) send_notify( notify
, DBT_DEVICEREMOVECOMPLETE
);
866 /* query information about an existing dos drive, by letter or udi */
867 NTSTATUS
query_dos_device( int letter
, enum device_type
*type
, char **device
, char **mount_point
)
869 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
870 struct dos_drive
*drive
;
871 struct disk_device
*disk_device
;
873 EnterCriticalSection( &device_section
);
874 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
876 if (drive
->drive
!= letter
) continue;
877 disk_device
= drive
->volume
->device
;
878 if (type
) *type
= disk_device
->type
;
879 if (device
) *device
= strdupA( disk_device
->unix_device
);
880 if (mount_point
) *mount_point
= strdupA( disk_device
->unix_mount
);
881 status
= STATUS_SUCCESS
;
884 LeaveCriticalSection( &device_section
);
888 /* handler for ioctls on the harddisk device */
889 static NTSTATUS WINAPI
harddisk_ioctl( DEVICE_OBJECT
*device
, IRP
*irp
)
891 IO_STACK_LOCATION
*irpsp
= IoGetCurrentIrpStackLocation( irp
);
892 struct disk_device
*dev
= device
->DeviceExtension
;
894 TRACE( "ioctl %x insize %u outsize %u\n",
895 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
,
896 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
897 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
899 EnterCriticalSection( &device_section
);
901 switch(irpsp
->Parameters
.DeviceIoControl
.IoControlCode
)
903 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
906 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
908 info
.Cylinders
.QuadPart
= 10000;
909 info
.MediaType
= (dev
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
910 info
.TracksPerCylinder
= 255;
911 info
.SectorsPerTrack
= 63;
912 info
.BytesPerSector
= 512;
913 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &info
, len
);
914 irp
->IoStatus
.Information
= len
;
915 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
918 case IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
:
920 DISK_GEOMETRY_EX info
;
921 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
923 FIXME("The DISK_PARTITION_INFO and DISK_DETECTION_INFO structures will not be filled\n");
925 info
.Geometry
.Cylinders
.QuadPart
= 10000;
926 info
.Geometry
.MediaType
= (dev
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
927 info
.Geometry
.TracksPerCylinder
= 255;
928 info
.Geometry
.SectorsPerTrack
= 63;
929 info
.Geometry
.BytesPerSector
= 512;
930 info
.DiskSize
.QuadPart
= info
.Geometry
.Cylinders
.QuadPart
* info
.Geometry
.TracksPerCylinder
*
931 info
.Geometry
.SectorsPerTrack
* info
.Geometry
.BytesPerSector
;
933 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &info
, len
);
934 irp
->IoStatus
.Information
= len
;
935 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
938 case IOCTL_STORAGE_GET_DEVICE_NUMBER
:
940 DWORD len
= min( sizeof(dev
->devnum
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
942 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &dev
->devnum
, len
);
943 irp
->IoStatus
.Information
= len
;
944 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
947 case IOCTL_CDROM_READ_TOC
:
948 irp
->IoStatus
.u
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
950 case IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
:
952 DWORD len
= min( 32, irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
954 FIXME( "returning zero-filled buffer for IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS\n" );
955 memset( irp
->AssociatedIrp
.SystemBuffer
, 0, len
);
956 irp
->IoStatus
.Information
= len
;
957 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
962 ULONG code
= irpsp
->Parameters
.DeviceIoControl
.IoControlCode
;
963 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
964 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
965 irp
->IoStatus
.u
.Status
= STATUS_NOT_SUPPORTED
;
970 LeaveCriticalSection( &device_section
);
971 IoCompleteRequest( irp
, IO_NO_INCREMENT
);
972 return STATUS_SUCCESS
;
975 /* driver entry point for the harddisk driver */
976 NTSTATUS WINAPI
harddisk_driver_entry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
978 struct disk_device
*device
;
980 harddisk_driver
= driver
;
981 driver
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = harddisk_ioctl
;
983 /* create a harddisk0 device that isn't assigned to any drive */
984 create_disk_device( DEVICE_HARDDISK
, &device
);
986 create_drive_devices();
988 return STATUS_SUCCESS
;