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 unsigned int ref
; /* ref count */
75 GUID guid
; /* volume uuid */
76 struct mount_point
*mount
; /* Volume{xxx} mount point */
81 struct list entry
; /* entry in drives list */
82 struct volume
*volume
; /* volume for this drive */
83 int drive
; /* drive letter (0 = A: etc.) */
84 struct mount_point
*mount
; /* DosDevices mount point */
87 static struct list drives_list
= LIST_INIT(drives_list
);
88 static struct list volumes_list
= LIST_INIT(volumes_list
);
90 static DRIVER_OBJECT
*harddisk_driver
;
92 static CRITICAL_SECTION device_section
;
93 static CRITICAL_SECTION_DEBUG critsect_debug
=
95 0, 0, &device_section
,
96 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
97 0, 0, { (DWORD_PTR
)(__FILE__
": device_section") }
99 static CRITICAL_SECTION device_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
101 static char *get_dosdevices_path( char **drive
)
103 const char *config_dir
= wine_get_config_dir();
104 size_t len
= strlen(config_dir
) + sizeof("/dosdevices/a::");
105 char *path
= HeapAlloc( GetProcessHeap(), 0, len
);
108 strcpy( path
, config_dir
);
109 strcat( path
, "/dosdevices/a::" );
110 *drive
= path
+ len
- 4;
115 static char *strdupA( const char *str
)
119 if (!str
) return NULL
;
120 if ((ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(str
) + 1 ))) strcpy( ret
, str
);
124 static const GUID
*get_default_uuid( int letter
)
128 guid
.Data4
[7] = 'A' + letter
;
132 /* read a Unix symlink; returned buffer must be freed by caller */
133 static char *read_symlink( const char *path
)
140 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
142 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
145 ret
= readlink( path
, buffer
, size
);
148 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
156 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
161 /* update a symlink if it changed; return TRUE if updated */
162 static void update_symlink( const char *path
, const char *dest
, const char *orig_dest
)
166 if (!orig_dest
|| strcmp( orig_dest
, dest
))
169 symlink( dest
, path
);
175 /* send notification about a change to a given drive */
176 static void send_notify( int drive
, int code
)
178 DEV_BROADCAST_VOLUME info
;
180 info
.dbcv_size
= sizeof(info
);
181 info
.dbcv_devicetype
= DBT_DEVTYP_VOLUME
;
182 info
.dbcv_reserved
= 0;
183 info
.dbcv_unitmask
= 1 << drive
;
184 info
.dbcv_flags
= DBTF_MEDIA
;
185 BroadcastSystemMessageW( BSF_FORCEIFHUNG
|BSF_QUERY
, NULL
,
186 WM_DEVICECHANGE
, code
, (LPARAM
)&info
);
189 /* create the disk device for a given volume */
190 static NTSTATUS
create_disk_device( enum device_type type
, struct disk_device
**device_ret
)
192 static const WCHAR harddiskvolW
[] = {'\\','D','e','v','i','c','e',
193 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
194 static const WCHAR harddiskW
[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
195 static const WCHAR cdromW
[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
196 static const WCHAR floppyW
[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
197 static const WCHAR ramdiskW
[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
198 static const WCHAR cdromlinkW
[] = {'\\','?','?','\\','C','d','R','o','m','%','u',0};
199 static const WCHAR physdriveW
[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
203 const WCHAR
*format
= NULL
;
204 const WCHAR
*link_format
= NULL
;
206 DEVICE_OBJECT
*dev_obj
;
207 struct disk_device
*device
;
212 case DEVICE_HARDDISK
:
213 case DEVICE_NETWORK
: /* FIXME */
215 link_format
= physdriveW
;
217 case DEVICE_HARDDISK_VOL
:
218 format
= harddiskvolW
;
219 first
= 1; /* harddisk volumes start counting from 1 */
227 link_format
= cdromlinkW
;
234 name
.MaximumLength
= (strlenW(format
) + 10) * sizeof(WCHAR
);
235 name
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, name
.MaximumLength
);
236 for (i
= first
; i
< 32; i
++)
238 sprintfW( name
.Buffer
, format
, i
);
239 name
.Length
= strlenW(name
.Buffer
) * sizeof(WCHAR
);
240 status
= IoCreateDevice( harddisk_driver
, sizeof(*device
), &name
, 0, 0, FALSE
, &dev_obj
);
241 if (status
!= STATUS_OBJECT_NAME_COLLISION
) break;
245 device
= dev_obj
->DeviceExtension
;
246 device
->dev_obj
= dev_obj
;
249 device
->unix_device
= NULL
;
250 device
->unix_mount
= NULL
;
251 device
->symlink
.Buffer
= NULL
;
255 UNICODE_STRING symlink
;
257 symlink
.MaximumLength
= (strlenW(link_format
) + 10) * sizeof(WCHAR
);
258 if ((symlink
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, symlink
.MaximumLength
)))
260 sprintfW( symlink
.Buffer
, link_format
, i
);
261 symlink
.Length
= strlenW(symlink
.Buffer
) * sizeof(WCHAR
);
262 if (!IoCreateSymbolicLink( &symlink
, &name
)) device
->symlink
= symlink
;
270 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
271 device
->devnum
.DeviceNumber
= i
;
272 device
->devnum
.PartitionNumber
= ~0u;
275 device
->devnum
.DeviceType
= FILE_DEVICE_CD_ROM
;
276 device
->devnum
.DeviceNumber
= i
;
277 device
->devnum
.PartitionNumber
= ~0u;
280 device
->devnum
.DeviceType
= FILE_DEVICE_DVD
;
281 device
->devnum
.DeviceNumber
= i
;
282 device
->devnum
.PartitionNumber
= ~0u;
285 case DEVICE_HARDDISK
:
286 case DEVICE_NETWORK
: /* FIXME */
287 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
288 device
->devnum
.DeviceNumber
= i
;
289 device
->devnum
.PartitionNumber
= 0;
291 case DEVICE_HARDDISK_VOL
:
292 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
293 device
->devnum
.DeviceNumber
= 0;
294 device
->devnum
.PartitionNumber
= i
;
297 *device_ret
= device
;
298 TRACE( "created device %s\n", debugstr_w(name
.Buffer
) );
302 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name
.Buffer
), status
);
303 RtlFreeUnicodeString( &name
);
308 /* delete the disk device for a given drive */
309 static void delete_disk_device( struct disk_device
*device
)
311 TRACE( "deleting device %s\n", debugstr_w(device
->name
.Buffer
) );
312 if (device
->symlink
.Buffer
)
314 IoDeleteSymbolicLink( &device
->symlink
);
315 RtlFreeUnicodeString( &device
->symlink
);
317 RtlFreeHeap( GetProcessHeap(), 0, device
->unix_device
);
318 RtlFreeHeap( GetProcessHeap(), 0, device
->unix_mount
);
319 RtlFreeUnicodeString( &device
->name
);
320 IoDeleteDevice( device
->dev_obj
);
323 /* grab another reference to a volume */
324 static struct volume
*grab_volume( struct volume
*volume
)
330 /* release a volume and delete the corresponding disk device when refcount is 0 */
331 static unsigned int release_volume( struct volume
*volume
)
333 unsigned int ret
= --volume
->ref
;
337 TRACE( "%s udi %s\n", debugstr_guid(&volume
->guid
), debugstr_a(volume
->udi
) );
338 assert( !volume
->udi
);
339 list_remove( &volume
->entry
);
340 if (volume
->mount
) delete_mount_point( volume
->mount
);
341 delete_disk_device( volume
->device
);
342 RtlFreeHeap( GetProcessHeap(), 0, volume
);
347 /* set the volume udi */
348 static void set_volume_udi( struct volume
*volume
, const char *udi
)
352 assert( !volume
->udi
);
353 /* having a udi means the HAL side holds an extra reference */
354 if ((volume
->udi
= strdupA( udi
))) grab_volume( volume
);
356 else if (volume
->udi
)
358 RtlFreeHeap( GetProcessHeap(), 0, volume
->udi
);
360 release_volume( volume
);
364 /* create a disk volume */
365 static NTSTATUS
create_volume( const char *udi
, enum device_type type
, struct volume
**volume_ret
)
367 struct volume
*volume
;
370 if (!(volume
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*volume
) )))
371 return STATUS_NO_MEMORY
;
373 if (!(status
= create_disk_device( type
, &volume
->device
)))
375 if (udi
) set_volume_udi( volume
, udi
);
376 list_add_tail( &volumes_list
, &volume
->entry
);
377 *volume_ret
= grab_volume( volume
);
379 else RtlFreeHeap( GetProcessHeap(), 0, volume
);
384 /* create the disk device for a given volume */
385 static NTSTATUS
create_dos_device( struct volume
*volume
, const char *udi
, int letter
,
386 enum device_type type
, struct dos_drive
**drive_ret
)
388 struct dos_drive
*drive
;
391 if (!(drive
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*drive
) ))) return STATUS_NO_MEMORY
;
392 drive
->drive
= letter
;
397 if (udi
) set_volume_udi( volume
, udi
);
398 drive
->volume
= grab_volume( volume
);
399 status
= STATUS_SUCCESS
;
401 else status
= create_volume( udi
, type
, &drive
->volume
);
403 if (status
== STATUS_SUCCESS
)
405 list_add_tail( &drives_list
, &drive
->entry
);
408 else RtlFreeHeap( GetProcessHeap(), 0, drive
);
413 /* delete the disk device for a given drive */
414 static void delete_dos_device( struct dos_drive
*drive
)
416 list_remove( &drive
->entry
);
417 if (drive
->mount
) delete_mount_point( drive
->mount
);
418 release_volume( drive
->volume
);
419 RtlFreeHeap( GetProcessHeap(), 0, drive
);
422 /* find a volume that matches the parameters */
423 static struct volume
*find_matching_volume( const char *udi
, const char *device
,
424 const char *mount_point
, enum device_type type
)
426 struct volume
*volume
;
427 struct disk_device
*disk_device
;
429 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
433 /* when we have a udi we only match drives added manually */
434 if (udi
&& volume
->udi
) continue;
435 /* and when we don't have a udi we only match dynamic drives */
436 if (!udi
&& !volume
->udi
) continue;
438 disk_device
= volume
->device
;
439 if (disk_device
->type
!= type
) continue;
440 if (device
&& disk_device
->unix_device
)
442 if (strcmp( device
, disk_device
->unix_device
)) continue;
445 if (mount_point
&& disk_device
->unix_mount
)
447 if (strcmp( mount_point
, disk_device
->unix_mount
)) continue;
450 if (!match
) continue;
451 TRACE( "found matching volume %s for device %s mount %s type %u\n",
452 debugstr_guid(&volume
->guid
), debugstr_a(device
), debugstr_a(mount_point
), type
);
453 return grab_volume( volume
);
458 /* change the information for an existing volume */
459 static NTSTATUS
set_volume_info( struct volume
*volume
, struct dos_drive
*drive
, const char *device
,
460 const char *mount_point
, enum device_type type
, const GUID
*guid
)
463 unsigned int id_len
= 0;
464 struct disk_device
*disk_device
= volume
->device
;
467 if (type
!= disk_device
->type
)
469 if ((status
= create_disk_device( type
, &disk_device
))) return status
;
472 delete_mount_point( volume
->mount
);
473 volume
->mount
= NULL
;
475 if (drive
&& drive
->mount
)
477 delete_mount_point( drive
->mount
);
480 delete_disk_device( volume
->device
);
481 volume
->device
= disk_device
;
485 RtlFreeHeap( GetProcessHeap(), 0, disk_device
->unix_device
);
486 RtlFreeHeap( GetProcessHeap(), 0, disk_device
->unix_mount
);
488 disk_device
->unix_device
= strdupA( device
);
489 disk_device
->unix_mount
= strdupA( mount_point
);
491 if (guid
&& memcmp( &volume
->guid
, guid
, sizeof(volume
->guid
) ))
493 volume
->guid
= *guid
;
496 delete_mount_point( volume
->mount
);
497 volume
->mount
= NULL
;
502 volume
->mount
= add_volume_mount_point( disk_device
->dev_obj
, &disk_device
->name
, &volume
->guid
);
503 if (drive
&& !drive
->mount
)
504 drive
->mount
= add_dosdev_mount_point( disk_device
->dev_obj
, &disk_device
->name
, drive
->drive
);
506 if (disk_device
->unix_mount
)
508 id
= disk_device
->unix_mount
;
509 id_len
= strlen( disk_device
->unix_mount
) + 1;
511 if (volume
->mount
) set_mount_point_id( volume
->mount
, id
, id_len
);
512 if (drive
&& drive
->mount
) set_mount_point_id( drive
->mount
, id
, id_len
);
514 return STATUS_SUCCESS
;
517 /* change the drive letter or volume for an existing drive */
518 static void set_drive_info( struct dos_drive
*drive
, int letter
, struct volume
*volume
)
520 if (drive
->drive
!= letter
)
522 if (drive
->mount
) delete_mount_point( drive
->mount
);
524 drive
->drive
= letter
;
526 if (drive
->volume
!= volume
)
528 if (drive
->mount
) delete_mount_point( drive
->mount
);
530 grab_volume( volume
);
531 release_volume( drive
->volume
);
532 drive
->volume
= volume
;
536 static inline int is_valid_device( struct stat
*st
)
538 #if defined(linux) || defined(__sun__)
539 return S_ISBLK( st
->st_mode
);
541 /* disks are char devices on *BSD */
542 return S_ISCHR( st
->st_mode
);
546 /* find or create a DOS drive for the corresponding device */
547 static int add_drive( const char *device
, enum device_type type
)
551 struct stat dev_st
, drive_st
;
552 int drive
, first
, last
, avail
= 0;
554 if (stat( device
, &dev_st
) == -1 || !is_valid_device( &dev_st
)) return -1;
556 if (!(path
= get_dosdevices_path( &p
))) return -1;
558 memset( in_use
, 0, sizeof(in_use
) );
580 for (drive
= first
; drive
< last
; drive
++)
582 if (in_use
[drive
]) continue; /* already checked */
584 if (stat( path
, &drive_st
) == -1)
586 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) /* this is a candidate */
591 /* if mount point symlink doesn't exist either, it's available */
592 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) avail
= drive
;
596 else in_use
[drive
] = 1;
601 if (!is_valid_device( &drive_st
)) continue;
602 if (dev_st
.st_rdev
== drive_st
.st_rdev
) goto done
;
607 /* try to use the one we found */
610 if (symlink( device
, path
) != -1) goto done
;
611 /* failed, retry the search */
617 HeapFree( GetProcessHeap(), 0, path
);
621 /* create devices for mapped drives */
622 static void create_drive_devices(void)
624 char *path
, *p
, *link
, *device
;
625 struct dos_drive
*drive
;
626 struct volume
*volume
;
629 enum device_type drive_type
;
630 WCHAR driveW
[] = {'a',':',0};
632 if (!(path
= get_dosdevices_path( &p
))) return;
633 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &drives_key
)) drives_key
= 0;
635 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++)
639 if (!(link
= read_symlink( path
))) continue;
641 device
= read_symlink( path
);
643 drive_type
= i
< 2 ? DEVICE_FLOPPY
: DEVICE_HARDDISK_VOL
;
647 DWORD j
, type
, size
= sizeof(buffer
);
650 if (!RegQueryValueExW( drives_key
, driveW
, NULL
, &type
, (BYTE
*)buffer
, &size
) &&
653 for (j
= 0; j
< sizeof(drive_types
)/sizeof(drive_types
[0]); j
++)
654 if (drive_types
[j
][0] && !strcmpiW( buffer
, drive_types
[j
] ))
659 if (drive_type
== DEVICE_FLOPPY
&& i
>= 2) drive_type
= DEVICE_HARDDISK
;
663 volume
= find_matching_volume( NULL
, device
, link
, drive_type
);
664 if (!create_dos_device( volume
, NULL
, i
, drive_type
, &drive
))
666 /* don't reset uuid if we used an existing volume */
667 const GUID
*guid
= volume
? NULL
: get_default_uuid(i
);
668 set_volume_info( drive
->volume
, drive
, device
, link
, drive_type
, guid
);
672 RtlFreeHeap( GetProcessHeap(), 0, link
);
673 RtlFreeHeap( GetProcessHeap(), 0, device
);
675 if (volume
) release_volume( volume
);
677 RegCloseKey( drives_key
);
678 RtlFreeHeap( GetProcessHeap(), 0, path
);
681 /* create a new disk volume */
682 NTSTATUS
add_volume( const char *udi
, const char *device
, const char *mount_point
,
683 enum device_type type
, const GUID
*guid
)
685 struct volume
*volume
;
686 NTSTATUS status
= STATUS_SUCCESS
;
688 TRACE( "adding %s device %s mount %s type %u uuid %s\n", debugstr_a(udi
),
689 debugstr_a(device
), debugstr_a(mount_point
), type
, debugstr_guid(guid
) );
691 EnterCriticalSection( &device_section
);
692 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
693 if (volume
->udi
&& !strcmp( udi
, volume
->udi
))
695 grab_volume( volume
);
699 /* udi not found, search for a non-dynamic volume */
700 if ((volume
= find_matching_volume( udi
, device
, mount_point
, type
))) set_volume_udi( volume
, udi
);
701 else status
= create_volume( udi
, type
, &volume
);
704 if (!status
) status
= set_volume_info( volume
, NULL
, device
, mount_point
, type
, guid
);
705 if (volume
) release_volume( volume
);
706 LeaveCriticalSection( &device_section
);
710 /* create a new disk volume */
711 NTSTATUS
remove_volume( const char *udi
)
713 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
714 struct volume
*volume
;
716 EnterCriticalSection( &device_section
);
717 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
719 if (!volume
->udi
|| strcmp( udi
, volume
->udi
)) continue;
720 set_volume_udi( volume
, NULL
);
721 status
= STATUS_SUCCESS
;
724 LeaveCriticalSection( &device_section
);
729 /* create a new dos drive */
730 NTSTATUS
add_dos_device( int letter
, const char *udi
, const char *device
,
731 const char *mount_point
, enum device_type type
, const GUID
*guid
)
735 NTSTATUS status
= STATUS_SUCCESS
;
736 struct dos_drive
*drive
, *next
;
737 struct volume
*volume
;
740 if (!(path
= get_dosdevices_path( &p
))) return STATUS_NO_MEMORY
;
742 EnterCriticalSection( &device_section
);
743 volume
= find_matching_volume( udi
, device
, mount_point
, type
);
745 if (letter
== -1) /* auto-assign a letter */
747 letter
= add_drive( device
, type
);
750 status
= STATUS_OBJECT_NAME_COLLISION
;
754 LIST_FOR_EACH_ENTRY_SAFE( drive
, next
, &drives_list
, struct dos_drive
, entry
)
756 if (drive
->volume
->udi
&& !strcmp( udi
, drive
->volume
->udi
)) goto found
;
757 if (drive
->drive
== letter
) delete_dos_device( drive
);
760 else /* simply reset the device symlink */
762 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
763 if (drive
->drive
== letter
) break;
766 if (&drive
->entry
== &drives_list
) update_symlink( path
, device
, NULL
);
769 update_symlink( path
, device
, drive
->volume
->device
->unix_device
);
770 delete_dos_device( drive
);
774 if ((status
= create_dos_device( volume
, udi
, letter
, type
, &drive
))) goto done
;
777 if (!guid
&& !volume
) guid
= get_default_uuid( letter
);
778 if (!volume
) volume
= grab_volume( drive
->volume
);
779 set_drive_info( drive
, letter
, volume
);
780 p
[0] = 'a' + drive
->drive
;
782 update_symlink( path
, mount_point
, volume
->device
->unix_mount
);
783 set_volume_info( volume
, drive
, device
, mount_point
, type
, guid
);
785 TRACE( "added device %c: udi %s for %s on %s type %u\n",
786 'a' + drive
->drive
, wine_dbgstr_a(udi
), wine_dbgstr_a(device
),
787 wine_dbgstr_a(mount_point
), type
);
789 /* hack: force the drive type in the registry */
790 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
792 const WCHAR
*type_name
= drive_types
[type
];
793 WCHAR name
[] = {'a',':',0};
795 name
[0] += drive
->drive
;
796 if (!type_name
[0] && type
== DEVICE_HARDDISK
) type_name
= drive_types
[DEVICE_FLOPPY
];
798 RegSetValueExW( hkey
, name
, 0, REG_SZ
, (const BYTE
*)type_name
,
799 (strlenW(type_name
) + 1) * sizeof(WCHAR
) );
801 RegDeleteValueW( hkey
, name
);
805 if (udi
) notify
= drive
->drive
;
808 if (volume
) release_volume( volume
);
809 LeaveCriticalSection( &device_section
);
810 RtlFreeHeap( GetProcessHeap(), 0, path
);
811 if (notify
!= -1) send_notify( notify
, DBT_DEVICEARRIVAL
);
815 /* remove an existing dos drive, by letter or udi */
816 NTSTATUS
remove_dos_device( int letter
, const char *udi
)
818 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
820 struct dos_drive
*drive
;
824 EnterCriticalSection( &device_section
);
825 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
829 if (!drive
->volume
->udi
) continue;
830 if (strcmp( udi
, drive
->volume
->udi
)) continue;
831 set_volume_udi( drive
->volume
, NULL
);
833 else if (drive
->drive
!= letter
) continue;
835 if ((path
= get_dosdevices_path( &p
)))
837 p
[0] = 'a' + drive
->drive
;
840 RtlFreeHeap( GetProcessHeap(), 0, path
);
843 /* clear the registry key too */
844 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
846 WCHAR name
[] = {'a',':',0};
847 name
[0] += drive
->drive
;
848 RegDeleteValueW( hkey
, name
);
852 if (udi
&& drive
->volume
->device
->unix_mount
) notify
= drive
->drive
;
854 delete_dos_device( drive
);
855 status
= STATUS_SUCCESS
;
858 LeaveCriticalSection( &device_section
);
859 if (notify
!= -1) send_notify( notify
, DBT_DEVICEREMOVECOMPLETE
);
863 /* query information about an existing dos drive, by letter or udi */
864 NTSTATUS
query_dos_device( int letter
, enum device_type
*type
, char **device
, char **mount_point
)
866 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
867 struct dos_drive
*drive
;
868 struct disk_device
*disk_device
;
870 EnterCriticalSection( &device_section
);
871 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
873 if (drive
->drive
!= letter
) continue;
874 disk_device
= drive
->volume
->device
;
875 if (type
) *type
= disk_device
->type
;
876 if (device
) *device
= strdupA( disk_device
->unix_device
);
877 if (mount_point
) *mount_point
= strdupA( disk_device
->unix_mount
);
878 status
= STATUS_SUCCESS
;
881 LeaveCriticalSection( &device_section
);
885 /* handler for ioctls on the harddisk device */
886 static NTSTATUS WINAPI
harddisk_ioctl( DEVICE_OBJECT
*device
, IRP
*irp
)
888 IO_STACK_LOCATION
*irpsp
= IoGetCurrentIrpStackLocation( irp
);
889 struct disk_device
*dev
= device
->DeviceExtension
;
891 TRACE( "ioctl %x insize %u outsize %u\n",
892 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
,
893 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
894 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
896 EnterCriticalSection( &device_section
);
898 switch(irpsp
->Parameters
.DeviceIoControl
.IoControlCode
)
900 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
903 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
905 info
.Cylinders
.QuadPart
= 10000;
906 info
.MediaType
= (dev
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
907 info
.TracksPerCylinder
= 255;
908 info
.SectorsPerTrack
= 63;
909 info
.BytesPerSector
= 512;
910 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &info
, len
);
911 irp
->IoStatus
.Information
= len
;
912 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
915 case IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
:
917 DISK_GEOMETRY_EX info
;
918 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
920 FIXME("The DISK_PARTITION_INFO and DISK_DETECTION_INFO structures will not be filled\n");
922 info
.Geometry
.Cylinders
.QuadPart
= 10000;
923 info
.Geometry
.MediaType
= (dev
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
924 info
.Geometry
.TracksPerCylinder
= 255;
925 info
.Geometry
.SectorsPerTrack
= 63;
926 info
.Geometry
.BytesPerSector
= 512;
927 info
.DiskSize
.QuadPart
= info
.Geometry
.Cylinders
.QuadPart
* info
.Geometry
.TracksPerCylinder
*
928 info
.Geometry
.SectorsPerTrack
* info
.Geometry
.BytesPerSector
;
930 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &info
, len
);
931 irp
->IoStatus
.Information
= len
;
932 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
935 case IOCTL_STORAGE_GET_DEVICE_NUMBER
:
937 DWORD len
= min( sizeof(dev
->devnum
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
939 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &dev
->devnum
, len
);
940 irp
->IoStatus
.Information
= len
;
941 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
944 case IOCTL_CDROM_READ_TOC
:
945 irp
->IoStatus
.u
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
947 case IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
:
949 DWORD len
= min( 32, irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
951 FIXME( "returning zero-filled buffer for IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS\n" );
952 memset( irp
->AssociatedIrp
.SystemBuffer
, 0, len
);
953 irp
->IoStatus
.Information
= len
;
954 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
959 ULONG code
= irpsp
->Parameters
.DeviceIoControl
.IoControlCode
;
960 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
961 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
962 irp
->IoStatus
.u
.Status
= STATUS_NOT_SUPPORTED
;
967 LeaveCriticalSection( &device_section
);
968 IoCompleteRequest( irp
, IO_NO_INCREMENT
);
969 return irp
->IoStatus
.u
.Status
;
972 /* driver entry point for the harddisk driver */
973 NTSTATUS WINAPI
harddisk_driver_entry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
975 struct disk_device
*device
;
977 harddisk_driver
= driver
;
978 driver
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = harddisk_ioctl
;
980 /* create a harddisk0 device that isn't assigned to any drive */
981 create_disk_device( DEVICE_HARDDISK
, &device
);
983 create_drive_devices();
985 return STATUS_SUCCESS
;