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 {'c','d','r','o','m',0}, /* DEVICE_DVD */
52 {'n','e','t','w','o','r','k',0}, /* DEVICE_NETWORK */
53 {'r','a','m','d','i','s','k',0} /* DEVICE_RAMDISK */
56 static const WCHAR drives_keyW
[] = {'S','o','f','t','w','a','r','e','\\',
57 'W','i','n','e','\\','D','r','i','v','e','s',0};
61 enum device_type type
; /* drive type */
62 DEVICE_OBJECT
*dev_obj
; /* disk device allocated for this volume */
63 UNICODE_STRING name
; /* device name */
64 UNICODE_STRING symlink
; /* device symlink if any */
65 STORAGE_DEVICE_NUMBER devnum
; /* device number info */
66 char *unix_device
; /* unix device path */
67 char *unix_mount
; /* unix mount point path */
72 struct list entry
; /* entry in volumes list */
73 struct disk_device
*device
; /* disk device */
74 char *udi
; /* unique identifier for dynamic volumes */
75 unsigned int ref
; /* ref count */
76 GUID guid
; /* volume uuid */
77 struct mount_point
*mount
; /* Volume{xxx} mount point */
82 struct list entry
; /* entry in drives list */
83 struct volume
*volume
; /* volume for this drive */
84 int drive
; /* drive letter (0 = A: etc.) */
85 struct mount_point
*mount
; /* DosDevices mount point */
88 static struct list drives_list
= LIST_INIT(drives_list
);
89 static struct list volumes_list
= LIST_INIT(volumes_list
);
91 static DRIVER_OBJECT
*harddisk_driver
;
93 static CRITICAL_SECTION device_section
;
94 static CRITICAL_SECTION_DEBUG critsect_debug
=
96 0, 0, &device_section
,
97 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
98 0, 0, { (DWORD_PTR
)(__FILE__
": device_section") }
100 static CRITICAL_SECTION device_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
102 static char *get_dosdevices_path( char **drive
)
104 const char *config_dir
= wine_get_config_dir();
105 size_t len
= strlen(config_dir
) + sizeof("/dosdevices/a::");
106 char *path
= HeapAlloc( GetProcessHeap(), 0, len
);
109 strcpy( path
, config_dir
);
110 strcat( path
, "/dosdevices/a::" );
111 *drive
= path
+ len
- 4;
116 static char *strdupA( const char *str
)
120 if (!str
) return NULL
;
121 if ((ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(str
) + 1 ))) strcpy( ret
, str
);
125 static const GUID
*get_default_uuid( int letter
)
129 guid
.Data4
[7] = 'A' + letter
;
133 /* read a Unix symlink; returned buffer must be freed by caller */
134 static char *read_symlink( const char *path
)
141 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
143 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
146 ret
= readlink( path
, buffer
, size
);
149 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
157 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
162 /* update a symlink if it changed; return TRUE if updated */
163 static void update_symlink( const char *path
, const char *dest
, const char *orig_dest
)
167 if (!orig_dest
|| strcmp( orig_dest
, dest
))
170 symlink( dest
, path
);
176 /* send notification about a change to a given drive */
177 static void send_notify( int drive
, int code
)
179 DEV_BROADCAST_VOLUME info
;
181 info
.dbcv_size
= sizeof(info
);
182 info
.dbcv_devicetype
= DBT_DEVTYP_VOLUME
;
183 info
.dbcv_reserved
= 0;
184 info
.dbcv_unitmask
= 1 << drive
;
185 info
.dbcv_flags
= DBTF_MEDIA
;
186 BroadcastSystemMessageW( BSF_FORCEIFHUNG
|BSF_QUERY
, NULL
,
187 WM_DEVICECHANGE
, code
, (LPARAM
)&info
);
190 /* create the disk device for a given volume */
191 static NTSTATUS
create_disk_device( enum device_type type
, struct disk_device
**device_ret
)
193 static const WCHAR harddiskvolW
[] = {'\\','D','e','v','i','c','e',
194 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
195 static const WCHAR harddiskW
[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
196 static const WCHAR cdromW
[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
197 static const WCHAR floppyW
[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
198 static const WCHAR ramdiskW
[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
199 static const WCHAR cdromlinkW
[] = {'\\','?','?','\\','C','d','R','o','m','%','u',0};
200 static const WCHAR physdriveW
[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
204 const WCHAR
*format
= NULL
;
205 const WCHAR
*link_format
= NULL
;
207 DEVICE_OBJECT
*dev_obj
;
208 struct disk_device
*device
;
213 case DEVICE_HARDDISK
:
214 case DEVICE_NETWORK
: /* FIXME */
216 link_format
= physdriveW
;
218 case DEVICE_HARDDISK_VOL
:
219 format
= harddiskvolW
;
220 first
= 1; /* harddisk volumes start counting from 1 */
228 link_format
= cdromlinkW
;
235 name
.MaximumLength
= (strlenW(format
) + 10) * sizeof(WCHAR
);
236 name
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, name
.MaximumLength
);
237 for (i
= first
; i
< 32; i
++)
239 sprintfW( name
.Buffer
, format
, i
);
240 name
.Length
= strlenW(name
.Buffer
) * sizeof(WCHAR
);
241 status
= IoCreateDevice( harddisk_driver
, sizeof(*device
), &name
, 0, 0, FALSE
, &dev_obj
);
242 if (status
!= STATUS_OBJECT_NAME_COLLISION
) break;
246 device
= dev_obj
->DeviceExtension
;
247 device
->dev_obj
= dev_obj
;
250 device
->unix_device
= NULL
;
251 device
->unix_mount
= NULL
;
252 device
->symlink
.Buffer
= NULL
;
256 UNICODE_STRING symlink
;
258 symlink
.MaximumLength
= (strlenW(link_format
) + 10) * sizeof(WCHAR
);
259 if ((symlink
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, symlink
.MaximumLength
)))
261 sprintfW( symlink
.Buffer
, link_format
, i
);
262 symlink
.Length
= strlenW(symlink
.Buffer
) * sizeof(WCHAR
);
263 if (!IoCreateSymbolicLink( &symlink
, &name
)) device
->symlink
= symlink
;
271 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
272 device
->devnum
.DeviceNumber
= i
;
273 device
->devnum
.PartitionNumber
= ~0u;
276 device
->devnum
.DeviceType
= FILE_DEVICE_CD_ROM
;
277 device
->devnum
.DeviceNumber
= i
;
278 device
->devnum
.PartitionNumber
= ~0u;
281 device
->devnum
.DeviceType
= FILE_DEVICE_DVD
;
282 device
->devnum
.DeviceNumber
= i
;
283 device
->devnum
.PartitionNumber
= ~0u;
286 case DEVICE_HARDDISK
:
287 case DEVICE_NETWORK
: /* FIXME */
288 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
289 device
->devnum
.DeviceNumber
= i
;
290 device
->devnum
.PartitionNumber
= 0;
292 case DEVICE_HARDDISK_VOL
:
293 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
294 device
->devnum
.DeviceNumber
= 0;
295 device
->devnum
.PartitionNumber
= i
;
298 *device_ret
= device
;
299 TRACE( "created device %s\n", debugstr_w(name
.Buffer
) );
303 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name
.Buffer
), status
);
304 RtlFreeUnicodeString( &name
);
309 /* delete the disk device for a given drive */
310 static void delete_disk_device( struct disk_device
*device
)
312 TRACE( "deleting device %s\n", debugstr_w(device
->name
.Buffer
) );
313 if (device
->symlink
.Buffer
)
315 IoDeleteSymbolicLink( &device
->symlink
);
316 RtlFreeUnicodeString( &device
->symlink
);
318 RtlFreeHeap( GetProcessHeap(), 0, device
->unix_device
);
319 RtlFreeHeap( GetProcessHeap(), 0, device
->unix_mount
);
320 RtlFreeUnicodeString( &device
->name
);
321 IoDeleteDevice( device
->dev_obj
);
324 /* grab another reference to a volume */
325 static struct volume
*grab_volume( struct volume
*volume
)
331 /* release a volume and delete the corresponding disk device when refcount is 0 */
332 static unsigned int release_volume( struct volume
*volume
)
334 unsigned int ret
= --volume
->ref
;
338 TRACE( "%s udi %s\n", debugstr_guid(&volume
->guid
), debugstr_a(volume
->udi
) );
339 assert( !volume
->udi
);
340 list_remove( &volume
->entry
);
341 if (volume
->mount
) delete_mount_point( volume
->mount
);
342 delete_disk_device( volume
->device
);
343 RtlFreeHeap( GetProcessHeap(), 0, volume
);
348 /* set the volume udi */
349 static void set_volume_udi( struct volume
*volume
, const char *udi
)
353 assert( !volume
->udi
);
354 /* having a udi means the HAL side holds an extra reference */
355 if ((volume
->udi
= strdupA( udi
))) grab_volume( volume
);
357 else if (volume
->udi
)
359 RtlFreeHeap( GetProcessHeap(), 0, volume
->udi
);
361 release_volume( volume
);
365 /* create a disk volume */
366 static NTSTATUS
create_volume( const char *udi
, enum device_type type
, struct volume
**volume_ret
)
368 struct volume
*volume
;
371 if (!(volume
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*volume
) )))
372 return STATUS_NO_MEMORY
;
374 if (!(status
= create_disk_device( type
, &volume
->device
)))
376 if (udi
) set_volume_udi( volume
, udi
);
377 list_add_tail( &volumes_list
, &volume
->entry
);
378 *volume_ret
= grab_volume( volume
);
380 else RtlFreeHeap( GetProcessHeap(), 0, volume
);
385 /* create the disk device for a given volume */
386 static NTSTATUS
create_dos_device( struct volume
*volume
, const char *udi
, int letter
,
387 enum device_type type
, struct dos_drive
**drive_ret
)
389 struct dos_drive
*drive
;
392 if (!(drive
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*drive
) ))) return STATUS_NO_MEMORY
;
393 drive
->drive
= letter
;
398 if (udi
) set_volume_udi( volume
, udi
);
399 drive
->volume
= grab_volume( volume
);
400 status
= STATUS_SUCCESS
;
402 else status
= create_volume( udi
, type
, &drive
->volume
);
404 if (status
== STATUS_SUCCESS
)
406 list_add_tail( &drives_list
, &drive
->entry
);
409 else RtlFreeHeap( GetProcessHeap(), 0, drive
);
414 /* delete the disk device for a given drive */
415 static void delete_dos_device( struct dos_drive
*drive
)
417 list_remove( &drive
->entry
);
418 if (drive
->mount
) delete_mount_point( drive
->mount
);
419 release_volume( drive
->volume
);
420 RtlFreeHeap( GetProcessHeap(), 0, drive
);
423 /* find a volume that matches the parameters */
424 static struct volume
*find_matching_volume( const char *udi
, const char *device
,
425 const char *mount_point
, enum device_type type
)
427 struct volume
*volume
;
428 struct disk_device
*disk_device
;
430 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
434 /* when we have a udi we only match drives added manually */
435 if (udi
&& volume
->udi
) continue;
436 /* and when we don't have a udi we only match dynamic drives */
437 if (!udi
&& !volume
->udi
) continue;
439 disk_device
= volume
->device
;
440 if (disk_device
->type
!= type
) continue;
441 if (device
&& disk_device
->unix_device
)
443 if (strcmp( device
, disk_device
->unix_device
)) continue;
446 if (mount_point
&& disk_device
->unix_mount
)
448 if (strcmp( mount_point
, disk_device
->unix_mount
)) continue;
451 if (!match
) continue;
452 TRACE( "found matching volume %s for device %s mount %s type %u\n",
453 debugstr_guid(&volume
->guid
), debugstr_a(device
), debugstr_a(mount_point
), type
);
454 return grab_volume( volume
);
459 /* change the information for an existing volume */
460 static NTSTATUS
set_volume_info( struct volume
*volume
, struct dos_drive
*drive
, const char *device
,
461 const char *mount_point
, enum device_type type
, const GUID
*guid
)
464 unsigned int id_len
= 0;
465 struct disk_device
*disk_device
= volume
->device
;
468 if (type
!= disk_device
->type
)
470 if ((status
= create_disk_device( type
, &disk_device
))) return status
;
473 delete_mount_point( volume
->mount
);
474 volume
->mount
= NULL
;
476 if (drive
&& drive
->mount
)
478 delete_mount_point( drive
->mount
);
481 delete_disk_device( volume
->device
);
482 volume
->device
= disk_device
;
486 RtlFreeHeap( GetProcessHeap(), 0, disk_device
->unix_device
);
487 RtlFreeHeap( GetProcessHeap(), 0, disk_device
->unix_mount
);
489 disk_device
->unix_device
= strdupA( device
);
490 disk_device
->unix_mount
= strdupA( mount_point
);
492 if (guid
&& memcmp( &volume
->guid
, guid
, sizeof(volume
->guid
) ))
494 volume
->guid
= *guid
;
497 delete_mount_point( volume
->mount
);
498 volume
->mount
= NULL
;
503 volume
->mount
= add_volume_mount_point( disk_device
->dev_obj
, &disk_device
->name
, &volume
->guid
);
504 if (drive
&& !drive
->mount
)
505 drive
->mount
= add_dosdev_mount_point( disk_device
->dev_obj
, &disk_device
->name
, drive
->drive
);
507 if (disk_device
->unix_mount
)
509 id
= disk_device
->unix_mount
;
510 id_len
= strlen( disk_device
->unix_mount
) + 1;
512 if (volume
->mount
) set_mount_point_id( volume
->mount
, id
, id_len
);
513 if (drive
&& drive
->mount
) set_mount_point_id( drive
->mount
, id
, id_len
);
515 return STATUS_SUCCESS
;
518 /* change the drive letter or volume for an existing drive */
519 static void set_drive_info( struct dos_drive
*drive
, int letter
, struct volume
*volume
)
521 if (drive
->drive
!= letter
)
523 if (drive
->mount
) delete_mount_point( drive
->mount
);
525 drive
->drive
= letter
;
527 if (drive
->volume
!= volume
)
529 if (drive
->mount
) delete_mount_point( drive
->mount
);
531 grab_volume( volume
);
532 release_volume( drive
->volume
);
533 drive
->volume
= volume
;
537 static inline int is_valid_device( struct stat
*st
)
539 #if defined(linux) || defined(__sun__)
540 return S_ISBLK( st
->st_mode
);
542 /* disks are char devices on *BSD */
543 return S_ISCHR( st
->st_mode
);
547 /* find or create a DOS drive for the corresponding device */
548 static int add_drive( const char *device
, enum device_type type
)
552 struct stat dev_st
, drive_st
;
553 int drive
, first
, last
, avail
= 0;
555 if (stat( device
, &dev_st
) == -1 || !is_valid_device( &dev_st
)) return -1;
557 if (!(path
= get_dosdevices_path( &p
))) return -1;
559 memset( in_use
, 0, sizeof(in_use
) );
581 for (drive
= first
; drive
< last
; drive
++)
583 if (in_use
[drive
]) continue; /* already checked */
585 if (stat( path
, &drive_st
) == -1)
587 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) /* this is a candidate */
592 /* if mount point symlink doesn't exist either, it's available */
593 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) avail
= drive
;
597 else in_use
[drive
] = 1;
602 if (!is_valid_device( &drive_st
)) continue;
603 if (dev_st
.st_rdev
== drive_st
.st_rdev
) goto done
;
608 /* try to use the one we found */
611 if (symlink( device
, path
) != -1) goto done
;
612 /* failed, retry the search */
618 HeapFree( GetProcessHeap(), 0, path
);
622 /* create devices for mapped drives */
623 static void create_drive_devices(void)
625 char *path
, *p
, *link
, *device
;
626 struct dos_drive
*drive
;
627 struct volume
*volume
;
630 enum device_type drive_type
;
631 WCHAR driveW
[] = {'a',':',0};
633 if (!(path
= get_dosdevices_path( &p
))) return;
634 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &drives_key
)) drives_key
= 0;
636 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++)
640 if (!(link
= read_symlink( path
))) continue;
642 device
= read_symlink( path
);
644 drive_type
= i
< 2 ? DEVICE_FLOPPY
: DEVICE_HARDDISK_VOL
;
648 DWORD j
, type
, size
= sizeof(buffer
);
651 if (!RegQueryValueExW( drives_key
, driveW
, NULL
, &type
, (BYTE
*)buffer
, &size
) &&
654 for (j
= 0; j
< sizeof(drive_types
)/sizeof(drive_types
[0]); j
++)
655 if (drive_types
[j
][0] && !strcmpiW( buffer
, drive_types
[j
] ))
660 if (drive_type
== DEVICE_FLOPPY
&& i
>= 2) drive_type
= DEVICE_HARDDISK
;
664 volume
= find_matching_volume( NULL
, device
, link
, drive_type
);
665 if (!create_dos_device( volume
, NULL
, i
, drive_type
, &drive
))
667 /* don't reset uuid if we used an existing volume */
668 const GUID
*guid
= volume
? NULL
: get_default_uuid(i
);
669 set_volume_info( drive
->volume
, drive
, device
, link
, drive_type
, guid
);
673 RtlFreeHeap( GetProcessHeap(), 0, link
);
674 RtlFreeHeap( GetProcessHeap(), 0, device
);
676 if (volume
) release_volume( volume
);
678 RegCloseKey( drives_key
);
679 RtlFreeHeap( GetProcessHeap(), 0, path
);
682 /* create a new disk volume */
683 NTSTATUS
add_volume( const char *udi
, const char *device
, const char *mount_point
,
684 enum device_type type
, const GUID
*guid
)
686 struct volume
*volume
;
687 NTSTATUS status
= STATUS_SUCCESS
;
689 TRACE( "adding %s device %s mount %s type %u uuid %s\n", debugstr_a(udi
),
690 debugstr_a(device
), debugstr_a(mount_point
), type
, debugstr_guid(guid
) );
692 EnterCriticalSection( &device_section
);
693 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
694 if (volume
->udi
&& !strcmp( udi
, volume
->udi
))
696 grab_volume( volume
);
700 /* udi not found, search for a non-dynamic volume */
701 if ((volume
= find_matching_volume( udi
, device
, mount_point
, type
))) set_volume_udi( volume
, udi
);
702 else status
= create_volume( udi
, type
, &volume
);
705 if (!status
) status
= set_volume_info( volume
, NULL
, device
, mount_point
, type
, guid
);
706 if (volume
) release_volume( volume
);
707 LeaveCriticalSection( &device_section
);
711 /* create a new disk volume */
712 NTSTATUS
remove_volume( const char *udi
)
714 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
715 struct volume
*volume
;
717 EnterCriticalSection( &device_section
);
718 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
720 if (!volume
->udi
|| strcmp( udi
, volume
->udi
)) continue;
721 set_volume_udi( volume
, NULL
);
722 status
= STATUS_SUCCESS
;
725 LeaveCriticalSection( &device_section
);
730 /* create a new dos drive */
731 NTSTATUS
add_dos_device( int letter
, const char *udi
, const char *device
,
732 const char *mount_point
, enum device_type type
, const GUID
*guid
)
736 NTSTATUS status
= STATUS_SUCCESS
;
737 struct dos_drive
*drive
, *next
;
738 struct volume
*volume
;
741 if (!(path
= get_dosdevices_path( &p
))) return STATUS_NO_MEMORY
;
743 EnterCriticalSection( &device_section
);
744 volume
= find_matching_volume( udi
, device
, mount_point
, type
);
746 if (letter
== -1) /* auto-assign a letter */
748 letter
= add_drive( device
, type
);
751 status
= STATUS_OBJECT_NAME_COLLISION
;
755 LIST_FOR_EACH_ENTRY_SAFE( drive
, next
, &drives_list
, struct dos_drive
, entry
)
757 if (drive
->volume
->udi
&& !strcmp( udi
, drive
->volume
->udi
)) goto found
;
758 if (drive
->drive
== letter
) delete_dos_device( drive
);
761 else /* simply reset the device symlink */
763 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
764 if (drive
->drive
== letter
) break;
767 if (&drive
->entry
== &drives_list
) update_symlink( path
, device
, NULL
);
770 update_symlink( path
, device
, drive
->volume
->device
->unix_device
);
771 delete_dos_device( drive
);
775 if ((status
= create_dos_device( volume
, udi
, letter
, type
, &drive
))) goto done
;
778 if (!guid
&& !volume
) guid
= get_default_uuid( letter
);
779 if (!volume
) volume
= grab_volume( drive
->volume
);
780 set_drive_info( drive
, letter
, volume
);
781 p
[0] = 'a' + drive
->drive
;
783 update_symlink( path
, mount_point
, volume
->device
->unix_mount
);
784 set_volume_info( volume
, drive
, device
, mount_point
, type
, guid
);
786 TRACE( "added device %c: udi %s for %s on %s type %u\n",
787 'a' + drive
->drive
, wine_dbgstr_a(udi
), wine_dbgstr_a(device
),
788 wine_dbgstr_a(mount_point
), type
);
790 /* hack: force the drive type in the registry */
791 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
793 const WCHAR
*type_name
= drive_types
[type
];
794 WCHAR name
[] = {'a',':',0};
796 name
[0] += drive
->drive
;
797 if (!type_name
[0] && type
== DEVICE_HARDDISK
) type_name
= drive_types
[DEVICE_FLOPPY
];
799 RegSetValueExW( hkey
, name
, 0, REG_SZ
, (const BYTE
*)type_name
,
800 (strlenW(type_name
) + 1) * sizeof(WCHAR
) );
802 RegDeleteValueW( hkey
, name
);
806 if (udi
) notify
= drive
->drive
;
809 if (volume
) release_volume( volume
);
810 LeaveCriticalSection( &device_section
);
811 RtlFreeHeap( GetProcessHeap(), 0, path
);
812 if (notify
!= -1) send_notify( notify
, DBT_DEVICEARRIVAL
);
816 /* remove an existing dos drive, by letter or udi */
817 NTSTATUS
remove_dos_device( int letter
, const char *udi
)
819 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
821 struct dos_drive
*drive
;
825 EnterCriticalSection( &device_section
);
826 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
830 if (!drive
->volume
->udi
) continue;
831 if (strcmp( udi
, drive
->volume
->udi
)) continue;
832 set_volume_udi( drive
->volume
, NULL
);
834 else if (drive
->drive
!= letter
) continue;
836 if ((path
= get_dosdevices_path( &p
)))
838 p
[0] = 'a' + drive
->drive
;
841 RtlFreeHeap( GetProcessHeap(), 0, path
);
844 /* clear the registry key too */
845 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
847 WCHAR name
[] = {'a',':',0};
848 name
[0] += drive
->drive
;
849 RegDeleteValueW( hkey
, name
);
853 if (udi
&& drive
->volume
->device
->unix_mount
) notify
= drive
->drive
;
855 delete_dos_device( drive
);
856 status
= STATUS_SUCCESS
;
859 LeaveCriticalSection( &device_section
);
860 if (notify
!= -1) send_notify( notify
, DBT_DEVICEREMOVECOMPLETE
);
864 /* query information about an existing dos drive, by letter or udi */
865 NTSTATUS
query_dos_device( int letter
, enum device_type
*type
, char **device
, char **mount_point
)
867 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
868 struct dos_drive
*drive
;
869 struct disk_device
*disk_device
;
871 EnterCriticalSection( &device_section
);
872 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
874 if (drive
->drive
!= letter
) continue;
875 disk_device
= drive
->volume
->device
;
876 if (type
) *type
= disk_device
->type
;
877 if (device
) *device
= strdupA( disk_device
->unix_device
);
878 if (mount_point
) *mount_point
= strdupA( disk_device
->unix_mount
);
879 status
= STATUS_SUCCESS
;
882 LeaveCriticalSection( &device_section
);
886 /* handler for ioctls on the harddisk device */
887 static NTSTATUS WINAPI
harddisk_ioctl( DEVICE_OBJECT
*device
, IRP
*irp
)
889 IO_STACK_LOCATION
*irpsp
= IoGetCurrentIrpStackLocation( irp
);
890 struct disk_device
*dev
= device
->DeviceExtension
;
892 TRACE( "ioctl %x insize %u outsize %u\n",
893 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
,
894 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
895 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
897 EnterCriticalSection( &device_section
);
899 switch(irpsp
->Parameters
.DeviceIoControl
.IoControlCode
)
901 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
904 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
906 info
.Cylinders
.QuadPart
= 10000;
907 info
.MediaType
= (dev
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
908 info
.TracksPerCylinder
= 255;
909 info
.SectorsPerTrack
= 63;
910 info
.BytesPerSector
= 512;
911 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &info
, len
);
912 irp
->IoStatus
.Information
= len
;
913 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
916 case IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
:
918 DISK_GEOMETRY_EX info
;
919 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
921 FIXME("The DISK_PARTITION_INFO and DISK_DETECTION_INFO structures will not be filled\n");
923 info
.Geometry
.Cylinders
.QuadPart
= 10000;
924 info
.Geometry
.MediaType
= (dev
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
925 info
.Geometry
.TracksPerCylinder
= 255;
926 info
.Geometry
.SectorsPerTrack
= 63;
927 info
.Geometry
.BytesPerSector
= 512;
928 info
.DiskSize
.QuadPart
= info
.Geometry
.Cylinders
.QuadPart
* info
.Geometry
.TracksPerCylinder
*
929 info
.Geometry
.SectorsPerTrack
* info
.Geometry
.BytesPerSector
;
931 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &info
, len
);
932 irp
->IoStatus
.Information
= len
;
933 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
936 case IOCTL_STORAGE_GET_DEVICE_NUMBER
:
938 DWORD len
= min( sizeof(dev
->devnum
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
940 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &dev
->devnum
, len
);
941 irp
->IoStatus
.Information
= len
;
942 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
945 case IOCTL_CDROM_READ_TOC
:
946 irp
->IoStatus
.u
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
948 case IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
:
950 DWORD len
= min( 32, irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
952 FIXME( "returning zero-filled buffer for IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS\n" );
953 memset( irp
->AssociatedIrp
.SystemBuffer
, 0, len
);
954 irp
->IoStatus
.Information
= len
;
955 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
960 ULONG code
= irpsp
->Parameters
.DeviceIoControl
.IoControlCode
;
961 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
962 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
963 irp
->IoStatus
.u
.Status
= STATUS_NOT_SUPPORTED
;
968 LeaveCriticalSection( &device_section
);
969 IoCompleteRequest( irp
, IO_NO_INCREMENT
);
970 return irp
->IoStatus
.u
.Status
;
973 /* driver entry point for the harddisk driver */
974 NTSTATUS WINAPI
harddisk_driver_entry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
976 struct disk_device
*device
;
978 harddisk_driver
= driver
;
979 driver
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = harddisk_ioctl
;
981 /* create a harddisk0 device that isn't assigned to any drive */
982 create_disk_device( DEVICE_HARDDISK
, &device
);
984 create_drive_devices();
986 return STATUS_SUCCESS
;