mountmgr: Better reuse of existing devices.
[wine] / dlls / mountmgr.sys / device.c
1 /*
2  * Dynamic devices support
3  *
4  * Copyright 2006 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <sys/time.h>
29
30 #include "mountmgr.h"
31 #include "winreg.h"
32 #include "winuser.h"
33 #include "dbt.h"
34
35 #include "wine/library.h"
36 #include "wine/list.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
41
42 #define MAX_DOS_DRIVES 26
43
44 static const WCHAR drive_types[][8] =
45 {
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 */
53 };
54
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};
57
58 struct disk_device
59 {
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 */
67 };
68
69 struct volume
70 {
71     struct list           entry;       /* entry in volumes list */
72     struct disk_device   *device;      /* disk device */
73     char                 *udi;         /* unique identifier for dynamic volumes */
74     GUID                  guid;        /* volume uuid */
75     struct mount_point   *mount;       /* Volume{xxx} mount point */
76 };
77
78 struct dos_drive
79 {
80     struct list           entry;       /* entry in drives list */
81     struct volume        *volume;      /* volume for this drive */
82     int                   drive;       /* drive letter (0 = A: etc.) */
83     struct mount_point   *mount;       /* DosDevices mount point */
84 };
85
86 static struct list drives_list = LIST_INIT(drives_list);
87 static struct list volumes_list = LIST_INIT(volumes_list);
88
89 static DRIVER_OBJECT *harddisk_driver;
90
91 static char *get_dosdevices_path( char **drive )
92 {
93     const char *config_dir = wine_get_config_dir();
94     size_t len = strlen(config_dir) + sizeof("/dosdevices/a::");
95     char *path = HeapAlloc( GetProcessHeap(), 0, len );
96     if (path)
97     {
98         strcpy( path, config_dir );
99         strcat( path, "/dosdevices/a::" );
100         *drive = path + len - 4;
101     }
102     return path;
103 }
104
105 static char *strdupA( const char *str )
106 {
107     char *ret;
108
109     if (!str) return NULL;
110     if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
111     return ret;
112 }
113
114 static const GUID *get_default_uuid( int letter )
115 {
116     static GUID guid;
117
118     guid.Data4[7] = 'A' + letter;
119     return &guid;
120 }
121
122 /* read a Unix symlink; returned buffer must be freed by caller */
123 static char *read_symlink( const char *path )
124 {
125     char *buffer;
126     int ret, size = 128;
127
128     for (;;)
129     {
130         if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
131         {
132             SetLastError( ERROR_NOT_ENOUGH_MEMORY );
133             return 0;
134         }
135         ret = readlink( path, buffer, size );
136         if (ret == -1)
137         {
138             RtlFreeHeap( GetProcessHeap(), 0, buffer );
139             return 0;
140         }
141         if (ret != size)
142         {
143             buffer[ret] = 0;
144             return buffer;
145         }
146         RtlFreeHeap( GetProcessHeap(), 0, buffer );
147         size *= 2;
148     }
149 }
150
151 /* update a symlink if it changed; return TRUE if updated */
152 static void update_symlink( const char *path, const char *dest, const char *orig_dest )
153 {
154     if (dest && dest[0])
155     {
156         if (!orig_dest || strcmp( orig_dest, dest ))
157         {
158             unlink( path );
159             symlink( dest, path );
160         }
161     }
162     else unlink( path );
163 }
164
165 /* send notification about a change to a given drive */
166 static void send_notify( int drive, int code )
167 {
168     DWORD_PTR result;
169     DEV_BROADCAST_VOLUME info;
170
171     info.dbcv_size       = sizeof(info);
172     info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
173     info.dbcv_reserved   = 0;
174     info.dbcv_unitmask   = 1 << drive;
175     info.dbcv_flags      = DBTF_MEDIA;
176     result = BroadcastSystemMessageW( BSF_FORCEIFHUNG|BSF_QUERY, NULL,
177                                       WM_DEVICECHANGE, code, (LPARAM)&info );
178 }
179
180 /* create the disk device for a given volume */
181 static NTSTATUS create_disk_device( enum device_type type, struct disk_device **device_ret )
182 {
183     static const WCHAR harddiskvolW[] = {'\\','D','e','v','i','c','e',
184                                          '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
185     static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
186     static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
187     static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
188     static const WCHAR ramdiskW[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
189     static const WCHAR physdriveW[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
190
191     UINT i, first = 0;
192     NTSTATUS status = 0;
193     const WCHAR *format = NULL;
194     UNICODE_STRING name;
195     DEVICE_OBJECT *dev_obj;
196     struct disk_device *device;
197
198     switch(type)
199     {
200     case DEVICE_UNKNOWN:
201     case DEVICE_HARDDISK:
202     case DEVICE_NETWORK:  /* FIXME */
203         format = harddiskW;
204         break;
205     case DEVICE_HARDDISK_VOL:
206         format = harddiskvolW;
207         first = 1;  /* harddisk volumes start counting from 1 */
208         break;
209     case DEVICE_FLOPPY:
210         format = floppyW;
211         break;
212     case DEVICE_CDROM:
213         format = cdromW;
214         break;
215     case DEVICE_RAMDISK:
216         format = ramdiskW;
217         break;
218     }
219
220     name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
221     name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
222     for (i = first; i < 32; i++)
223     {
224         sprintfW( name.Buffer, format, i );
225         name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
226         status = IoCreateDevice( harddisk_driver, sizeof(*device), &name, 0, 0, FALSE, &dev_obj );
227         if (status != STATUS_OBJECT_NAME_COLLISION) break;
228     }
229     if (!status)
230     {
231         device = dev_obj->DeviceExtension;
232         device->dev_obj        = dev_obj;
233         device->name           = name;
234         device->type           = type;
235         device->unix_device    = NULL;
236         device->unix_mount     = NULL;
237         device->symlink.Buffer = NULL;
238
239         switch (type)
240         {
241         case DEVICE_FLOPPY:
242         case DEVICE_RAMDISK:
243             device->devnum.DeviceType = FILE_DEVICE_DISK;
244             device->devnum.DeviceNumber = i;
245             device->devnum.PartitionNumber = ~0u;
246             break;
247         case DEVICE_CDROM:
248             device->devnum.DeviceType = FILE_DEVICE_CD_ROM;
249             device->devnum.DeviceNumber = i;
250             device->devnum.PartitionNumber = ~0u;
251             break;
252         case DEVICE_UNKNOWN:
253         case DEVICE_HARDDISK:
254         case DEVICE_NETWORK:  /* FIXME */
255             {
256                 UNICODE_STRING symlink;
257
258                 symlink.MaximumLength = sizeof(physdriveW) + 10 * sizeof(WCHAR);
259                 if ((symlink.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, symlink.MaximumLength)))
260                 {
261                     sprintfW( symlink.Buffer, physdriveW, i );
262                     symlink.Length = strlenW(symlink.Buffer) * sizeof(WCHAR);
263                     if (!IoCreateSymbolicLink( &symlink, &name )) device->symlink = symlink;
264                 }
265                 device->devnum.DeviceType = FILE_DEVICE_DISK;
266                 device->devnum.DeviceNumber = i;
267                 device->devnum.PartitionNumber = 0;
268             }
269             break;
270         case DEVICE_HARDDISK_VOL:
271             device->devnum.DeviceType = FILE_DEVICE_DISK;
272             device->devnum.DeviceNumber = 0;
273             device->devnum.PartitionNumber = i;
274             break;
275         }
276         *device_ret = device;
277         TRACE( "created device %s\n", debugstr_w(name.Buffer) );
278     }
279     else
280     {
281         FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
282         RtlFreeUnicodeString( &name );
283     }
284     return status;
285 }
286
287 /* delete the disk device for a given drive */
288 static void delete_disk_device( struct disk_device *device )
289 {
290     TRACE( "deleting device %s\n", debugstr_w(device->name.Buffer) );
291     if (device->symlink.Buffer)
292     {
293         IoDeleteSymbolicLink( &device->symlink );
294         RtlFreeUnicodeString( &device->symlink );
295     }
296     RtlFreeHeap( GetProcessHeap(), 0, device->unix_device );
297     RtlFreeHeap( GetProcessHeap(), 0, device->unix_mount );
298     RtlFreeUnicodeString( &device->name );
299     IoDeleteDevice( device->dev_obj );
300 }
301
302 /* create a disk volume */
303 static NTSTATUS create_volume( const char *udi, enum device_type type, struct volume **volume_ret )
304 {
305     struct volume *volume;
306     NTSTATUS status;
307
308     if (!(volume = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*volume) )))
309         return STATUS_NO_MEMORY;
310
311     if (udi && !(volume->udi = strdupA( udi )))
312     {
313         RtlFreeHeap( GetProcessHeap(), 0, volume );
314         return STATUS_NO_MEMORY;
315     }
316     if (!(status = create_disk_device( type, &volume->device )))
317     {
318         list_add_tail( &volumes_list, &volume->entry );
319         *volume_ret = volume;
320     }
321     else
322     {
323         RtlFreeHeap( GetProcessHeap(), 0, volume->udi );
324         RtlFreeHeap( GetProcessHeap(), 0, volume );
325     }
326     return status;
327 }
328
329 /* delete a volume and the corresponding disk device */
330 static void delete_volume( struct volume *volume )
331 {
332     list_remove( &volume->entry );
333     if (volume->mount) delete_mount_point( volume->mount );
334     delete_disk_device( volume->device );
335     RtlFreeHeap( GetProcessHeap(), 0, volume->udi );
336     RtlFreeHeap( GetProcessHeap(), 0, volume );
337 }
338
339 /* create the disk device for a given volume */
340 static NTSTATUS create_dos_device( const char *udi, int letter, enum device_type type,
341                                    struct dos_drive **drive_ret )
342 {
343     struct dos_drive *drive;
344     NTSTATUS status;
345
346     if (!(drive = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*drive) ))) return STATUS_NO_MEMORY;
347     drive->drive = letter;
348     drive->mount = NULL;
349
350     if (!(status = create_volume( udi, type, &drive->volume )))
351     {
352         list_add_tail( &drives_list, &drive->entry );
353         *drive_ret = drive;
354     }
355     else RtlFreeHeap( GetProcessHeap(), 0, drive );
356
357     return status;
358 }
359
360 /* delete the disk device for a given drive */
361 static void delete_dos_device( struct dos_drive *drive )
362 {
363     list_remove( &drive->entry );
364     if (drive->mount) delete_mount_point( drive->mount );
365     delete_volume( drive->volume );
366     RtlFreeHeap( GetProcessHeap(), 0, drive );
367 }
368
369 /* change the information for an existing volume */
370 static NTSTATUS set_volume_info( struct volume *volume, struct dos_drive *drive, const char *device,
371                                  const char *mount_point, enum device_type type, const GUID *guid )
372 {
373     void *id = NULL;
374     unsigned int id_len = 0;
375     struct disk_device *disk_device = volume->device;
376     NTSTATUS status;
377
378     if (type != disk_device->type)
379     {
380         if ((status = create_disk_device( type, &disk_device ))) return status;
381         if (volume->mount)
382         {
383             delete_mount_point( volume->mount );
384             volume->mount = NULL;
385         }
386         if (drive && drive->mount)
387         {
388             delete_mount_point( drive->mount );
389             drive->mount = NULL;
390         }
391         delete_disk_device( volume->device );
392         volume->device = disk_device;
393     }
394     else
395     {
396         RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_device );
397         RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_mount );
398     }
399     disk_device->unix_device = strdupA( device );
400     disk_device->unix_mount = strdupA( mount_point );
401
402     if (memcmp( &volume->guid, guid, sizeof(volume->guid) ))
403     {
404         volume->guid = *guid;
405         if (volume->mount)
406         {
407             delete_mount_point( volume->mount );
408             volume->mount = NULL;
409         }
410     }
411
412     if (!volume->mount)
413         volume->mount = add_volume_mount_point( disk_device->dev_obj, &disk_device->name, &volume->guid );
414     if (drive && !drive->mount)
415         drive->mount = add_dosdev_mount_point( disk_device->dev_obj, &disk_device->name, drive->drive );
416
417     if (disk_device->unix_mount)
418     {
419         id = disk_device->unix_mount;
420         id_len = strlen( disk_device->unix_mount ) + 1;
421     }
422     if (volume->mount) set_mount_point_id( volume->mount, id, id_len );
423     if (drive && drive->mount) set_mount_point_id( drive->mount, id, id_len );
424
425     return STATUS_SUCCESS;
426 }
427
428 /* set or change the drive letter for an existing drive */
429 static void set_drive_letter( struct dos_drive *drive, int letter )
430 {
431     struct volume *volume = drive->volume;
432
433     if (drive->drive == letter) return;
434     if (drive->mount) delete_mount_point( drive->mount );
435     if (volume->mount) delete_mount_point( volume->mount );
436     drive->drive = letter;
437     drive->mount = NULL;
438     volume->mount = NULL;
439 }
440
441 static inline int is_valid_device( struct stat *st )
442 {
443 #if defined(linux) || defined(__sun__)
444     return S_ISBLK( st->st_mode );
445 #else
446     /* disks are char devices on *BSD */
447     return S_ISCHR( st->st_mode );
448 #endif
449 }
450
451 /* find or create a DOS drive for the corresponding device */
452 static int add_drive( const char *device, enum device_type type )
453 {
454     char *path, *p;
455     char in_use[26];
456     struct stat dev_st, drive_st;
457     int drive, first, last, avail = 0;
458
459     if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
460
461     if (!(path = get_dosdevices_path( &p ))) return -1;
462
463     memset( in_use, 0, sizeof(in_use) );
464
465     switch (type)
466     {
467     case DEVICE_FLOPPY:
468         first = 0;
469         last = 2;
470         break;
471     case DEVICE_CDROM:
472         first = 3;
473         last = 26;
474         break;
475     default:
476         first = 2;
477         last = 26;
478         break;
479     }
480
481     while (avail != -1)
482     {
483         avail = -1;
484         for (drive = first; drive < last; drive++)
485         {
486             if (in_use[drive]) continue;  /* already checked */
487             *p = 'a' + drive;
488             if (stat( path, &drive_st ) == -1)
489             {
490                 if (lstat( path, &drive_st ) == -1 && errno == ENOENT)  /* this is a candidate */
491                 {
492                     if (avail == -1)
493                     {
494                         p[2] = 0;
495                         /* if mount point symlink doesn't exist either, it's available */
496                         if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
497                         p[2] = ':';
498                     }
499                 }
500                 else in_use[drive] = 1;
501             }
502             else
503             {
504                 in_use[drive] = 1;
505                 if (!is_valid_device( &drive_st )) continue;
506                 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
507             }
508         }
509         if (avail != -1)
510         {
511             /* try to use the one we found */
512             drive = avail;
513             *p = 'a' + drive;
514             if (symlink( device, path ) != -1) goto done;
515             /* failed, retry the search */
516         }
517     }
518     drive = -1;
519
520 done:
521     HeapFree( GetProcessHeap(), 0, path );
522     return drive;
523 }
524
525 /* create devices for mapped drives */
526 static void create_drive_devices(void)
527 {
528     char *path, *p, *link, *device;
529     struct dos_drive *drive;
530     unsigned int i;
531     HKEY drives_key;
532     enum device_type drive_type;
533     WCHAR driveW[] = {'a',':',0};
534
535     if (!(path = get_dosdevices_path( &p ))) return;
536     if (RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &drives_key )) drives_key = 0;
537
538     for (i = 0; i < MAX_DOS_DRIVES; i++)
539     {
540         p[0] = 'a' + i;
541         p[2] = 0;
542         if (!(link = read_symlink( path ))) continue;
543         p[2] = ':';
544         device = read_symlink( path );
545
546         drive_type = i < 2 ? DEVICE_FLOPPY : DEVICE_HARDDISK_VOL;
547         if (drives_key)
548         {
549             WCHAR buffer[32];
550             DWORD j, type, size = sizeof(buffer);
551
552             driveW[0] = 'a' + i;
553             if (!RegQueryValueExW( drives_key, driveW, NULL, &type, (BYTE *)buffer, &size ) &&
554                 type == REG_SZ)
555             {
556                 for (j = 0; j < sizeof(drive_types)/sizeof(drive_types[0]); j++)
557                     if (drive_types[j][0] && !strcmpiW( buffer, drive_types[j] ))
558                     {
559                         drive_type = j;
560                         break;
561                     }
562                 if (drive_type == DEVICE_FLOPPY && i >= 2) drive_type = DEVICE_HARDDISK;
563             }
564         }
565
566         if (!create_dos_device( NULL, i, drive_type, &drive ))
567         {
568             set_volume_info( drive->volume, drive, device, link, drive_type, get_default_uuid(i) );
569         }
570         else
571         {
572             RtlFreeHeap( GetProcessHeap(), 0, link );
573             RtlFreeHeap( GetProcessHeap(), 0, device );
574         }
575     }
576     RegCloseKey( drives_key );
577     RtlFreeHeap( GetProcessHeap(), 0, path );
578 }
579
580 /* create a new disk volume */
581 NTSTATUS add_volume( const char *udi, const char *device, const char *mount_point,
582                      enum device_type type, const GUID *guid )
583 {
584     struct volume *volume;
585     NTSTATUS status;
586
587     TRACE( "adding %s device %s mount %s type %u uuid %s\n", debugstr_a(udi),
588            debugstr_a(device), debugstr_a(mount_point), type, debugstr_guid(guid) );
589
590     LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
591         if (volume->udi && !strcmp( udi, volume->udi )) goto found;
592
593     if ((status = create_volume( udi, type, &volume ))) return status;
594
595 found:
596     return set_volume_info( volume, NULL, device, mount_point, type, guid );
597 }
598
599 /* create a new disk volume */
600 NTSTATUS remove_volume( const char *udi )
601 {
602     struct volume *volume;
603
604     LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
605     {
606         if (!volume->udi || strcmp( udi, volume->udi )) continue;
607         delete_volume( volume );
608         return STATUS_SUCCESS;
609     }
610     return STATUS_NO_SUCH_DEVICE;
611 }
612
613
614 /* create a new dos drive */
615 NTSTATUS add_dos_device( int letter, const char *udi, const char *device,
616                          const char *mount_point, enum device_type type, const GUID *guid )
617 {
618     char *path, *p;
619     HKEY hkey;
620     NTSTATUS status = STATUS_SUCCESS;
621     struct dos_drive *drive, *next;
622
623     if (!(path = get_dosdevices_path( &p ))) return STATUS_NO_MEMORY;
624
625     if (letter == -1)  /* auto-assign a letter */
626     {
627         letter = add_drive( device, type );
628         if (letter == -1)
629         {
630             status = STATUS_OBJECT_NAME_COLLISION;
631             goto done;
632         }
633
634         LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
635         {
636             if (drive->volume->udi && !strcmp( udi, drive->volume->udi )) goto found;
637             if (drive->drive == letter) delete_dos_device( drive );
638         }
639     }
640     else  /* simply reset the device symlink */
641     {
642         *p = 'a' + letter;
643         LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
644         {
645             if (drive->drive != letter) continue;
646             update_symlink( path, device, drive->volume->device->unix_device );
647             goto found;
648         }
649         update_symlink( path, device, NULL );
650     }
651
652     if ((status = create_dos_device( udi, letter, type, &drive ))) goto done;
653
654 found:
655     if (!guid) guid = get_default_uuid( letter );
656     p[0] = 'a' + drive->drive;
657     p[2] = 0;
658     update_symlink( path, mount_point, drive->volume->device->unix_mount );
659     set_drive_letter( drive, letter );
660     set_volume_info( drive->volume, drive, device, mount_point, type, guid );
661
662     TRACE( "added device %c: udi %s for %s on %s type %u\n",
663            'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
664            wine_dbgstr_a(mount_point), type );
665
666     /* hack: force the drive type in the registry */
667     if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
668     {
669         const WCHAR *type_name = drive_types[type];
670         WCHAR name[3] = {'a',':',0};
671
672         name[0] += drive->drive;
673         if (!type_name[0] && type == DEVICE_HARDDISK) type_name = drive_types[DEVICE_FLOPPY];
674         if (type_name[0])
675             RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
676                             (strlenW(type_name) + 1) * sizeof(WCHAR) );
677         else
678             RegDeleteValueW( hkey, name );
679         RegCloseKey( hkey );
680     }
681
682     if (udi) send_notify( drive->drive, DBT_DEVICEARRIVAL );
683
684 done:
685     RtlFreeHeap( GetProcessHeap(), 0, path );
686     return status;
687 }
688
689 /* remove an existing dos drive, by letter or udi */
690 NTSTATUS remove_dos_device( int letter, const char *udi )
691 {
692     HKEY hkey;
693     struct dos_drive *drive;
694     char *path, *p;
695
696     LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
697     {
698         if (udi)
699         {
700             if (!drive->volume->udi) continue;
701             if (strcmp( udi, drive->volume->udi )) continue;
702         }
703         else if (drive->drive != letter) continue;
704
705         if ((path = get_dosdevices_path( &p )))
706         {
707             p[0] = 'a' + drive->drive;
708             p[2] = 0;
709             unlink( path );
710             RtlFreeHeap( GetProcessHeap(), 0, path );
711         }
712
713         /* clear the registry key too */
714         if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
715         {
716             WCHAR name[3] = {'a',':',0};
717             name[0] += drive->drive;
718             RegDeleteValueW( hkey, name );
719             RegCloseKey( hkey );
720         }
721
722         if (udi && drive->volume->device->unix_mount)
723             send_notify( drive->drive, DBT_DEVICEREMOVECOMPLETE );
724
725         delete_dos_device( drive );
726         return STATUS_SUCCESS;
727     }
728     return STATUS_NO_SUCH_DEVICE;
729 }
730
731 /* query information about an existing dos drive, by letter or udi */
732 NTSTATUS query_dos_device( int letter, enum device_type *type,
733                            const char **device, const char **mount_point )
734 {
735     struct dos_drive *drive;
736     struct disk_device *disk_device;
737
738     LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
739     {
740         if (drive->drive != letter) continue;
741         disk_device = drive->volume->device;
742         if (type) *type = disk_device->type;
743         if (device) *device = disk_device->unix_device;
744         if (mount_point) *mount_point = disk_device->unix_mount;
745         return STATUS_SUCCESS;
746     }
747     return STATUS_NO_SUCH_DEVICE;
748 }
749
750 /* handler for ioctls on the harddisk device */
751 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
752 {
753     IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
754     struct disk_device *dev = device->DeviceExtension;
755
756     TRACE( "ioctl %x insize %u outsize %u\n",
757            irpsp->Parameters.DeviceIoControl.IoControlCode,
758            irpsp->Parameters.DeviceIoControl.InputBufferLength,
759            irpsp->Parameters.DeviceIoControl.OutputBufferLength );
760
761     switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
762     {
763     case IOCTL_DISK_GET_DRIVE_GEOMETRY:
764     {
765         DISK_GEOMETRY info;
766         DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
767
768         info.Cylinders.QuadPart = 10000;
769         info.MediaType = (dev->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
770         info.TracksPerCylinder = 255;
771         info.SectorsPerTrack = 63;
772         info.BytesPerSector = 512;
773         memcpy( irp->MdlAddress->StartVa, &info, len );
774         irp->IoStatus.Information = len;
775         irp->IoStatus.u.Status = STATUS_SUCCESS;
776         break;
777     }
778     case IOCTL_STORAGE_GET_DEVICE_NUMBER:
779     {
780         DWORD len = min( sizeof(dev->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
781
782         memcpy( irp->MdlAddress->StartVa, &dev->devnum, len );
783         irp->IoStatus.Information = len;
784         irp->IoStatus.u.Status = STATUS_SUCCESS;
785         break;
786     }
787     case IOCTL_CDROM_READ_TOC:
788         irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
789         break;
790     default:
791         FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
792         irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
793         break;
794     }
795     return irp->IoStatus.u.Status;
796 }
797
798 /* driver entry point for the harddisk driver */
799 NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
800 {
801     struct disk_device *device;
802
803     harddisk_driver = driver;
804     driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
805
806     /* create a harddisk0 device that isn't assigned to any drive */
807     create_disk_device( DEVICE_HARDDISK, &device );
808
809     create_drive_devices();
810
811     return STATUS_SUCCESS;
812 }