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