include: Don't accidentally redefine a member in struct _IRP (LLVM/Clang).
[wine/multimedia.git] / dlls / mountmgr.sys / device.c
blobbf2bceec77b054a0a692e8621f8d92434891051e
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <sys/time.h>
30 #include "mountmgr.h"
31 #include "winreg.h"
32 #include "winuser.h"
33 #include "dbt.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};
58 struct disk_device
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 */
69 struct volume
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 */
79 struct dos_drive
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 );
106 if (path)
108 strcpy( path, config_dir );
109 strcat( path, "/dosdevices/a::" );
110 *drive = path + len - 4;
112 return path;
115 static char *strdupA( const char *str )
117 char *ret;
119 if (!str) return NULL;
120 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
121 return ret;
124 static const GUID *get_default_uuid( int letter )
126 static GUID guid;
128 guid.Data4[7] = 'A' + letter;
129 return &guid;
132 /* read a Unix symlink; returned buffer must be freed by caller */
133 static char *read_symlink( const char *path )
135 char *buffer;
136 int ret, size = 128;
138 for (;;)
140 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
142 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
143 return 0;
145 ret = readlink( path, buffer, size );
146 if (ret == -1)
148 RtlFreeHeap( GetProcessHeap(), 0, buffer );
149 return 0;
151 if (ret != size)
153 buffer[ret] = 0;
154 return buffer;
156 RtlFreeHeap( GetProcessHeap(), 0, buffer );
157 size *= 2;
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 )
164 if (dest && dest[0])
166 if (!orig_dest || strcmp( orig_dest, dest ))
168 unlink( path );
169 symlink( dest, path );
172 else unlink( path );
175 /* send notification about a change to a given drive */
176 static void send_notify( int drive, int code )
178 DWORD_PTR result;
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 result = 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 physdriveW[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
201 UINT i, first = 0;
202 NTSTATUS status = 0;
203 const WCHAR *format = NULL;
204 UNICODE_STRING name;
205 DEVICE_OBJECT *dev_obj;
206 struct disk_device *device;
208 switch(type)
210 case DEVICE_UNKNOWN:
211 case DEVICE_HARDDISK:
212 case DEVICE_NETWORK: /* FIXME */
213 format = harddiskW;
214 break;
215 case DEVICE_HARDDISK_VOL:
216 format = harddiskvolW;
217 first = 1; /* harddisk volumes start counting from 1 */
218 break;
219 case DEVICE_FLOPPY:
220 format = floppyW;
221 break;
222 case DEVICE_CDROM:
223 format = cdromW;
224 break;
225 case DEVICE_RAMDISK:
226 format = ramdiskW;
227 break;
230 name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
231 name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
232 for (i = first; i < 32; i++)
234 sprintfW( name.Buffer, format, i );
235 name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
236 status = IoCreateDevice( harddisk_driver, sizeof(*device), &name, 0, 0, FALSE, &dev_obj );
237 if (status != STATUS_OBJECT_NAME_COLLISION) break;
239 if (!status)
241 device = dev_obj->DeviceExtension;
242 device->dev_obj = dev_obj;
243 device->name = name;
244 device->type = type;
245 device->unix_device = NULL;
246 device->unix_mount = NULL;
247 device->symlink.Buffer = NULL;
249 switch (type)
251 case DEVICE_FLOPPY:
252 case DEVICE_RAMDISK:
253 device->devnum.DeviceType = FILE_DEVICE_DISK;
254 device->devnum.DeviceNumber = i;
255 device->devnum.PartitionNumber = ~0u;
256 break;
257 case DEVICE_CDROM:
258 device->devnum.DeviceType = FILE_DEVICE_CD_ROM;
259 device->devnum.DeviceNumber = i;
260 device->devnum.PartitionNumber = ~0u;
261 break;
262 case DEVICE_UNKNOWN:
263 case DEVICE_HARDDISK:
264 case DEVICE_NETWORK: /* FIXME */
266 UNICODE_STRING symlink;
268 symlink.MaximumLength = sizeof(physdriveW) + 10 * sizeof(WCHAR);
269 if ((symlink.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, symlink.MaximumLength)))
271 sprintfW( symlink.Buffer, physdriveW, i );
272 symlink.Length = strlenW(symlink.Buffer) * sizeof(WCHAR);
273 if (!IoCreateSymbolicLink( &symlink, &name )) device->symlink = symlink;
275 device->devnum.DeviceType = FILE_DEVICE_DISK;
276 device->devnum.DeviceNumber = i;
277 device->devnum.PartitionNumber = 0;
279 break;
280 case DEVICE_HARDDISK_VOL:
281 device->devnum.DeviceType = FILE_DEVICE_DISK;
282 device->devnum.DeviceNumber = 0;
283 device->devnum.PartitionNumber = i;
284 break;
286 *device_ret = device;
287 TRACE( "created device %s\n", debugstr_w(name.Buffer) );
289 else
291 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
292 RtlFreeUnicodeString( &name );
294 return status;
297 /* delete the disk device for a given drive */
298 static void delete_disk_device( struct disk_device *device )
300 TRACE( "deleting device %s\n", debugstr_w(device->name.Buffer) );
301 if (device->symlink.Buffer)
303 IoDeleteSymbolicLink( &device->symlink );
304 RtlFreeUnicodeString( &device->symlink );
306 RtlFreeHeap( GetProcessHeap(), 0, device->unix_device );
307 RtlFreeHeap( GetProcessHeap(), 0, device->unix_mount );
308 RtlFreeUnicodeString( &device->name );
309 IoDeleteDevice( device->dev_obj );
312 /* grab another reference to a volume */
313 static struct volume *grab_volume( struct volume *volume )
315 volume->ref++;
316 return volume;
319 /* release a volume and delete the corresponding disk device when refcount is 0 */
320 static unsigned int release_volume( struct volume *volume )
322 unsigned int ret = --volume->ref;
324 if (!ret)
326 TRACE( "%s udi %s\n", debugstr_guid(&volume->guid), debugstr_a(volume->udi) );
327 assert( !volume->udi );
328 list_remove( &volume->entry );
329 if (volume->mount) delete_mount_point( volume->mount );
330 delete_disk_device( volume->device );
331 RtlFreeHeap( GetProcessHeap(), 0, volume );
333 return ret;
336 /* set the volume udi */
337 static void set_volume_udi( struct volume *volume, const char *udi )
339 if (udi)
341 assert( !volume->udi );
342 /* having a udi means the HAL side holds an extra reference */
343 if ((volume->udi = strdupA( udi ))) grab_volume( volume );
345 else if (volume->udi)
347 RtlFreeHeap( GetProcessHeap(), 0, volume->udi );
348 volume->udi = NULL;
349 release_volume( volume );
353 /* create a disk volume */
354 static NTSTATUS create_volume( const char *udi, enum device_type type, struct volume **volume_ret )
356 struct volume *volume;
357 NTSTATUS status;
359 if (!(volume = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*volume) )))
360 return STATUS_NO_MEMORY;
362 if (!(status = create_disk_device( type, &volume->device )))
364 if (udi) set_volume_udi( volume, udi );
365 list_add_tail( &volumes_list, &volume->entry );
366 *volume_ret = grab_volume( volume );
368 else RtlFreeHeap( GetProcessHeap(), 0, volume );
370 return status;
373 /* create the disk device for a given volume */
374 static NTSTATUS create_dos_device( struct volume *volume, const char *udi, int letter,
375 enum device_type type, struct dos_drive **drive_ret )
377 struct dos_drive *drive;
378 NTSTATUS status;
380 if (!(drive = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*drive) ))) return STATUS_NO_MEMORY;
381 drive->drive = letter;
382 drive->mount = NULL;
384 if (volume)
386 if (udi) set_volume_udi( volume, udi );
387 drive->volume = grab_volume( volume );
388 status = STATUS_SUCCESS;
390 else status = create_volume( udi, type, &drive->volume );
392 if (status == STATUS_SUCCESS)
394 list_add_tail( &drives_list, &drive->entry );
395 *drive_ret = drive;
397 else RtlFreeHeap( GetProcessHeap(), 0, drive );
399 return status;
402 /* delete the disk device for a given drive */
403 static void delete_dos_device( struct dos_drive *drive )
405 list_remove( &drive->entry );
406 if (drive->mount) delete_mount_point( drive->mount );
407 release_volume( drive->volume );
408 RtlFreeHeap( GetProcessHeap(), 0, drive );
411 /* find a volume that matches the parameters */
412 static struct volume *find_matching_volume( const char *udi, const char *device,
413 const char *mount_point, enum device_type type )
415 struct volume *volume;
416 struct disk_device *disk_device;
418 LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
420 /* when we have a udi we only match drives added manually */
421 if (udi && volume->udi) continue;
422 /* and when we don't have a udi we only match dynamic drives */
423 if (!udi && !volume->udi) continue;
425 disk_device = volume->device;
426 if (disk_device->type != type) continue;
427 if (device && disk_device->unix_device && strcmp( device, disk_device->unix_device )) continue;
428 if (mount_point && disk_device->unix_mount && strcmp( mount_point, disk_device->unix_mount )) continue;
429 TRACE( "found matching volume %s for device %s mount %s type %u\n",
430 debugstr_guid(&volume->guid), debugstr_a(device), debugstr_a(mount_point), type );
431 return grab_volume( volume );
433 return NULL;
436 /* change the information for an existing volume */
437 static NTSTATUS set_volume_info( struct volume *volume, struct dos_drive *drive, const char *device,
438 const char *mount_point, enum device_type type, const GUID *guid )
440 void *id = NULL;
441 unsigned int id_len = 0;
442 struct disk_device *disk_device = volume->device;
443 NTSTATUS status;
445 if (type != disk_device->type)
447 if ((status = create_disk_device( type, &disk_device ))) return status;
448 if (volume->mount)
450 delete_mount_point( volume->mount );
451 volume->mount = NULL;
453 if (drive && drive->mount)
455 delete_mount_point( drive->mount );
456 drive->mount = NULL;
458 delete_disk_device( volume->device );
459 volume->device = disk_device;
461 else
463 RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_device );
464 RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_mount );
466 disk_device->unix_device = strdupA( device );
467 disk_device->unix_mount = strdupA( mount_point );
469 if (guid && memcmp( &volume->guid, guid, sizeof(volume->guid) ))
471 volume->guid = *guid;
472 if (volume->mount)
474 delete_mount_point( volume->mount );
475 volume->mount = NULL;
479 if (!volume->mount)
480 volume->mount = add_volume_mount_point( disk_device->dev_obj, &disk_device->name, &volume->guid );
481 if (drive && !drive->mount)
482 drive->mount = add_dosdev_mount_point( disk_device->dev_obj, &disk_device->name, drive->drive );
484 if (disk_device->unix_mount)
486 id = disk_device->unix_mount;
487 id_len = strlen( disk_device->unix_mount ) + 1;
489 if (volume->mount) set_mount_point_id( volume->mount, id, id_len );
490 if (drive && drive->mount) set_mount_point_id( drive->mount, id, id_len );
492 return STATUS_SUCCESS;
495 /* change the drive letter or volume for an existing drive */
496 static void set_drive_info( struct dos_drive *drive, int letter, struct volume *volume )
498 if (drive->drive != letter)
500 if (drive->mount) delete_mount_point( drive->mount );
501 drive->mount = NULL;
502 drive->drive = letter;
504 if (drive->volume != volume)
506 if (drive->mount) delete_mount_point( drive->mount );
507 drive->mount = NULL;
508 grab_volume( volume );
509 release_volume( drive->volume );
510 drive->volume = volume;
514 static inline int is_valid_device( struct stat *st )
516 #if defined(linux) || defined(__sun__)
517 return S_ISBLK( st->st_mode );
518 #else
519 /* disks are char devices on *BSD */
520 return S_ISCHR( st->st_mode );
521 #endif
524 /* find or create a DOS drive for the corresponding device */
525 static int add_drive( const char *device, enum device_type type )
527 char *path, *p;
528 char in_use[26];
529 struct stat dev_st, drive_st;
530 int drive, first, last, avail = 0;
532 if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
534 if (!(path = get_dosdevices_path( &p ))) return -1;
536 memset( in_use, 0, sizeof(in_use) );
538 switch (type)
540 case DEVICE_FLOPPY:
541 first = 0;
542 last = 2;
543 break;
544 case DEVICE_CDROM:
545 first = 3;
546 last = 26;
547 break;
548 default:
549 first = 2;
550 last = 26;
551 break;
554 while (avail != -1)
556 avail = -1;
557 for (drive = first; drive < last; drive++)
559 if (in_use[drive]) continue; /* already checked */
560 *p = 'a' + drive;
561 if (stat( path, &drive_st ) == -1)
563 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
565 if (avail == -1)
567 p[2] = 0;
568 /* if mount point symlink doesn't exist either, it's available */
569 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
570 p[2] = ':';
573 else in_use[drive] = 1;
575 else
577 in_use[drive] = 1;
578 if (!is_valid_device( &drive_st )) continue;
579 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
582 if (avail != -1)
584 /* try to use the one we found */
585 drive = avail;
586 *p = 'a' + drive;
587 if (symlink( device, path ) != -1) goto done;
588 /* failed, retry the search */
591 drive = -1;
593 done:
594 HeapFree( GetProcessHeap(), 0, path );
595 return drive;
598 /* create devices for mapped drives */
599 static void create_drive_devices(void)
601 char *path, *p, *link, *device;
602 struct dos_drive *drive;
603 struct volume *volume;
604 unsigned int i;
605 HKEY drives_key;
606 enum device_type drive_type;
607 WCHAR driveW[] = {'a',':',0};
609 if (!(path = get_dosdevices_path( &p ))) return;
610 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &drives_key )) drives_key = 0;
612 for (i = 0; i < MAX_DOS_DRIVES; i++)
614 p[0] = 'a' + i;
615 p[2] = 0;
616 if (!(link = read_symlink( path ))) continue;
617 p[2] = ':';
618 device = read_symlink( path );
620 drive_type = i < 2 ? DEVICE_FLOPPY : DEVICE_HARDDISK_VOL;
621 if (drives_key)
623 WCHAR buffer[32];
624 DWORD j, type, size = sizeof(buffer);
626 driveW[0] = 'a' + i;
627 if (!RegQueryValueExW( drives_key, driveW, NULL, &type, (BYTE *)buffer, &size ) &&
628 type == REG_SZ)
630 for (j = 0; j < sizeof(drive_types)/sizeof(drive_types[0]); j++)
631 if (drive_types[j][0] && !strcmpiW( buffer, drive_types[j] ))
633 drive_type = j;
634 break;
636 if (drive_type == DEVICE_FLOPPY && i >= 2) drive_type = DEVICE_HARDDISK;
640 volume = find_matching_volume( NULL, device, link, drive_type );
641 if (!create_dos_device( volume, NULL, i, drive_type, &drive ))
643 /* don't reset uuid if we used an existing volume */
644 const GUID *guid = volume ? NULL : get_default_uuid(i);
645 set_volume_info( drive->volume, drive, device, link, drive_type, guid );
647 else
649 RtlFreeHeap( GetProcessHeap(), 0, link );
650 RtlFreeHeap( GetProcessHeap(), 0, device );
652 if (volume) release_volume( volume );
654 RegCloseKey( drives_key );
655 RtlFreeHeap( GetProcessHeap(), 0, path );
658 /* create a new disk volume */
659 NTSTATUS add_volume( const char *udi, const char *device, const char *mount_point,
660 enum device_type type, const GUID *guid )
662 struct volume *volume;
663 NTSTATUS status = STATUS_SUCCESS;
665 TRACE( "adding %s device %s mount %s type %u uuid %s\n", debugstr_a(udi),
666 debugstr_a(device), debugstr_a(mount_point), type, debugstr_guid(guid) );
668 EnterCriticalSection( &device_section );
669 LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
670 if (volume->udi && !strcmp( udi, volume->udi )) goto found;
672 /* udi not found, search for a non-dynamic volume */
673 if ((volume = find_matching_volume( udi, device, mount_point, type ))) set_volume_udi( volume, udi );
674 else status = create_volume( udi, type, &volume );
676 found:
677 if (!status) status = set_volume_info( volume, NULL, device, mount_point, type, guid );
678 if (volume) release_volume( volume );
679 LeaveCriticalSection( &device_section );
680 return status;
683 /* create a new disk volume */
684 NTSTATUS remove_volume( const char *udi )
686 NTSTATUS status = STATUS_NO_SUCH_DEVICE;
687 struct volume *volume;
689 EnterCriticalSection( &device_section );
690 LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
692 if (!volume->udi || strcmp( udi, volume->udi )) continue;
693 set_volume_udi( volume, NULL );
694 status = STATUS_SUCCESS;
695 break;
697 LeaveCriticalSection( &device_section );
698 return status;
702 /* create a new dos drive */
703 NTSTATUS add_dos_device( int letter, const char *udi, const char *device,
704 const char *mount_point, enum device_type type, const GUID *guid )
706 char *path, *p;
707 HKEY hkey;
708 NTSTATUS status = STATUS_SUCCESS;
709 struct dos_drive *drive, *next;
710 struct volume *volume;
711 int notify = -1;
713 if (!(path = get_dosdevices_path( &p ))) return STATUS_NO_MEMORY;
715 EnterCriticalSection( &device_section );
716 volume = find_matching_volume( udi, device, mount_point, type );
718 if (letter == -1) /* auto-assign a letter */
720 letter = add_drive( device, type );
721 if (letter == -1)
723 status = STATUS_OBJECT_NAME_COLLISION;
724 goto done;
727 LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
729 if (drive->volume->udi && !strcmp( udi, drive->volume->udi )) goto found;
730 if (drive->drive == letter) delete_dos_device( drive );
733 else /* simply reset the device symlink */
735 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
736 if (drive->drive == letter) break;
738 *p = 'a' + letter;
739 if (&drive->entry == &drives_list) update_symlink( path, device, NULL );
740 else
742 update_symlink( path, device, drive->volume->device->unix_device );
743 delete_dos_device( drive );
747 if ((status = create_dos_device( volume, udi, letter, type, &drive ))) goto done;
749 found:
750 if (!guid && !volume) guid = get_default_uuid( letter );
751 if (!volume) volume = grab_volume( drive->volume );
752 set_drive_info( drive, letter, volume );
753 p[0] = 'a' + drive->drive;
754 p[2] = 0;
755 update_symlink( path, mount_point, volume->device->unix_mount );
756 set_volume_info( volume, drive, device, mount_point, type, guid );
758 TRACE( "added device %c: udi %s for %s on %s type %u\n",
759 'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
760 wine_dbgstr_a(mount_point), type );
762 /* hack: force the drive type in the registry */
763 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
765 const WCHAR *type_name = drive_types[type];
766 WCHAR name[3] = {'a',':',0};
768 name[0] += drive->drive;
769 if (!type_name[0] && type == DEVICE_HARDDISK) type_name = drive_types[DEVICE_FLOPPY];
770 if (type_name[0])
771 RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
772 (strlenW(type_name) + 1) * sizeof(WCHAR) );
773 else
774 RegDeleteValueW( hkey, name );
775 RegCloseKey( hkey );
778 if (udi) notify = drive->drive;
780 done:
781 if (volume) release_volume( volume );
782 LeaveCriticalSection( &device_section );
783 RtlFreeHeap( GetProcessHeap(), 0, path );
784 if (notify != -1) send_notify( notify, DBT_DEVICEARRIVAL );
785 return status;
788 /* remove an existing dos drive, by letter or udi */
789 NTSTATUS remove_dos_device( int letter, const char *udi )
791 NTSTATUS status = STATUS_NO_SUCH_DEVICE;
792 HKEY hkey;
793 struct dos_drive *drive;
794 char *path, *p;
795 int notify = -1;
797 EnterCriticalSection( &device_section );
798 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
800 if (udi)
802 if (!drive->volume->udi) continue;
803 if (strcmp( udi, drive->volume->udi )) continue;
804 set_volume_udi( drive->volume, NULL );
806 else if (drive->drive != letter) continue;
808 if ((path = get_dosdevices_path( &p )))
810 p[0] = 'a' + drive->drive;
811 p[2] = 0;
812 unlink( path );
813 RtlFreeHeap( GetProcessHeap(), 0, path );
816 /* clear the registry key too */
817 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
819 WCHAR name[3] = {'a',':',0};
820 name[0] += drive->drive;
821 RegDeleteValueW( hkey, name );
822 RegCloseKey( hkey );
825 if (udi && drive->volume->device->unix_mount) notify = drive->drive;
827 delete_dos_device( drive );
828 status = STATUS_SUCCESS;
829 break;
831 LeaveCriticalSection( &device_section );
832 if (notify != -1) send_notify( notify, DBT_DEVICEREMOVECOMPLETE );
833 return status;
836 /* query information about an existing dos drive, by letter or udi */
837 NTSTATUS query_dos_device( int letter, enum device_type *type, char **device, char **mount_point )
839 NTSTATUS status = STATUS_NO_SUCH_DEVICE;
840 struct dos_drive *drive;
841 struct disk_device *disk_device;
843 EnterCriticalSection( &device_section );
844 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
846 if (drive->drive != letter) continue;
847 disk_device = drive->volume->device;
848 if (type) *type = disk_device->type;
849 if (device) *device = strdupA( disk_device->unix_device );
850 if (mount_point) *mount_point = strdupA( disk_device->unix_mount );
851 status = STATUS_SUCCESS;
852 break;
854 LeaveCriticalSection( &device_section );
855 return status;
858 /* handler for ioctls on the harddisk device */
859 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
861 IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u2.CurrentStackLocation;
862 struct disk_device *dev = device->DeviceExtension;
864 TRACE( "ioctl %x insize %u outsize %u\n",
865 irpsp->Parameters.DeviceIoControl.IoControlCode,
866 irpsp->Parameters.DeviceIoControl.InputBufferLength,
867 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
869 EnterCriticalSection( &device_section );
871 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
873 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
875 DISK_GEOMETRY info;
876 DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
878 info.Cylinders.QuadPart = 10000;
879 info.MediaType = (dev->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
880 info.TracksPerCylinder = 255;
881 info.SectorsPerTrack = 63;
882 info.BytesPerSector = 512;
883 memcpy( irp->MdlAddress->StartVa, &info, len );
884 irp->IoStatus.Information = len;
885 irp->IoStatus.u.Status = STATUS_SUCCESS;
886 break;
888 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
890 DWORD len = min( sizeof(dev->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
892 memcpy( irp->MdlAddress->StartVa, &dev->devnum, len );
893 irp->IoStatus.Information = len;
894 irp->IoStatus.u.Status = STATUS_SUCCESS;
895 break;
897 case IOCTL_CDROM_READ_TOC:
898 irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
899 break;
900 default:
901 FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
902 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
903 break;
906 LeaveCriticalSection( &device_section );
907 return irp->IoStatus.u.Status;
910 /* driver entry point for the harddisk driver */
911 NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
913 struct disk_device *device;
915 harddisk_driver = driver;
916 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
918 /* create a harddisk0 device that isn't assigned to any drive */
919 create_disk_device( DEVICE_HARDDISK, &device );
921 create_drive_devices();
923 return STATUS_SUCCESS;