push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / mountmgr.sys / device.c
blob0467079e86bda51ecbe668583e266092b2b1a4f7
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 dos_drive
60 struct list entry; /* entry in drives list */
61 char *udi; /* unique identifier for dynamic drives */
62 int drive; /* drive letter (0 = A: etc.) */
63 enum device_type type; /* drive type */
64 DEVICE_OBJECT *device; /* disk device allocated for this drive */
65 UNICODE_STRING name; /* device name */
66 UNICODE_STRING symlink; /* device symlink if any */
67 STORAGE_DEVICE_NUMBER devnum; /* device number info */
68 struct mount_point *dosdev; /* DosDevices mount point */
69 struct mount_point *volume; /* Volume{xxx} mount point */
70 char *unix_device; /* unix device path */
71 char *unix_mount; /* unix mount point path */
74 static struct list drives_list = LIST_INIT(drives_list);
76 static DRIVER_OBJECT *harddisk_driver;
78 static char *get_dosdevices_path( char **drive )
80 const char *config_dir = wine_get_config_dir();
81 size_t len = strlen(config_dir) + sizeof("/dosdevices/a::");
82 char *path = HeapAlloc( GetProcessHeap(), 0, len );
83 if (path)
85 strcpy( path, config_dir );
86 strcat( path, "/dosdevices/a::" );
87 *drive = path + len - 4;
89 return path;
92 static char *strdupA( const char *str )
94 char *ret;
96 if (!str) return NULL;
97 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
98 return ret;
101 /* read a Unix symlink; returned buffer must be freed by caller */
102 static char *read_symlink( const char *path )
104 char *buffer;
105 int ret, size = 128;
107 for (;;)
109 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
111 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
112 return 0;
114 ret = readlink( path, buffer, size );
115 if (ret == -1)
117 RtlFreeHeap( GetProcessHeap(), 0, buffer );
118 return 0;
120 if (ret != size)
122 buffer[ret] = 0;
123 return buffer;
125 RtlFreeHeap( GetProcessHeap(), 0, buffer );
126 size *= 2;
130 /* send notification about a change to a given drive */
131 static void send_notify( int drive, int code )
133 DWORD_PTR result;
134 DEV_BROADCAST_VOLUME info;
136 info.dbcv_size = sizeof(info);
137 info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
138 info.dbcv_reserved = 0;
139 info.dbcv_unitmask = 1 << drive;
140 info.dbcv_flags = DBTF_MEDIA;
141 result = BroadcastSystemMessageW( BSF_FORCEIFHUNG|BSF_QUERY, NULL,
142 WM_DEVICECHANGE, code, (LPARAM)&info );
145 /* create the disk device for a given drive */
146 static NTSTATUS create_disk_device( const char *udi, enum device_type type, struct dos_drive **drive_ret )
148 static const WCHAR harddiskvolW[] = {'\\','D','e','v','i','c','e',
149 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
150 static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
151 static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
152 static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
153 static const WCHAR ramdiskW[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
154 static const WCHAR physdriveW[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
156 UINT i, first = 0;
157 NTSTATUS status = 0;
158 const WCHAR *format = NULL;
159 UNICODE_STRING name;
160 DEVICE_OBJECT *dev_obj;
161 struct dos_drive *drive;
163 switch(type)
165 case DEVICE_UNKNOWN:
166 case DEVICE_HARDDISK:
167 case DEVICE_NETWORK: /* FIXME */
168 format = harddiskW;
169 break;
170 case DEVICE_HARDDISK_VOL:
171 format = harddiskvolW;
172 first = 1; /* harddisk volumes start counting from 1 */
173 break;
174 case DEVICE_FLOPPY:
175 format = floppyW;
176 break;
177 case DEVICE_CDROM:
178 format = cdromW;
179 break;
180 case DEVICE_RAMDISK:
181 format = ramdiskW;
182 break;
185 name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
186 name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
187 for (i = first; i < 32; i++)
189 sprintfW( name.Buffer, format, i );
190 name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
191 status = IoCreateDevice( harddisk_driver, sizeof(*drive), &name, 0, 0, FALSE, &dev_obj );
192 if (status != STATUS_OBJECT_NAME_COLLISION) break;
194 if (!status)
196 drive = dev_obj->DeviceExtension;
197 drive->drive = -1;
198 drive->device = dev_obj;
199 drive->name = name;
200 drive->type = type;
201 drive->dosdev = NULL;
202 drive->volume = NULL;
203 drive->unix_device = NULL;
204 drive->unix_mount = NULL;
205 drive->symlink.Buffer = NULL;
206 if (udi)
208 if (!(drive->udi = HeapAlloc( GetProcessHeap(), 0, strlen(udi)+1 )))
210 RtlFreeUnicodeString( &name );
211 IoDeleteDevice( drive->device );
212 return STATUS_NO_MEMORY;
214 strcpy( drive->udi, udi );
216 switch (type)
218 case DEVICE_FLOPPY:
219 case DEVICE_RAMDISK:
220 drive->devnum.DeviceType = FILE_DEVICE_DISK;
221 drive->devnum.DeviceNumber = i;
222 drive->devnum.PartitionNumber = ~0u;
223 break;
224 case DEVICE_CDROM:
225 drive->devnum.DeviceType = FILE_DEVICE_CD_ROM;
226 drive->devnum.DeviceNumber = i;
227 drive->devnum.PartitionNumber = ~0u;
228 break;
229 case DEVICE_UNKNOWN:
230 case DEVICE_HARDDISK:
231 case DEVICE_NETWORK: /* FIXME */
233 UNICODE_STRING symlink;
235 symlink.MaximumLength = sizeof(physdriveW) + 10 * sizeof(WCHAR);
236 if ((symlink.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, symlink.MaximumLength)))
238 sprintfW( symlink.Buffer, physdriveW, i );
239 symlink.Length = strlenW(symlink.Buffer) * sizeof(WCHAR);
240 if (!IoCreateSymbolicLink( &symlink, &name )) drive->symlink = symlink;
242 drive->devnum.DeviceType = FILE_DEVICE_DISK;
243 drive->devnum.DeviceNumber = i;
244 drive->devnum.PartitionNumber = 0;
246 break;
247 case DEVICE_HARDDISK_VOL:
248 drive->devnum.DeviceType = FILE_DEVICE_DISK;
249 drive->devnum.DeviceNumber = 0;
250 drive->devnum.PartitionNumber = i;
251 break;
253 list_add_tail( &drives_list, &drive->entry );
254 *drive_ret = drive;
255 TRACE( "created device %s\n", debugstr_w(name.Buffer) );
257 else
259 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
260 RtlFreeUnicodeString( &name );
262 return status;
265 /* delete the disk device for a given drive */
266 static void delete_disk_device( struct dos_drive *drive )
268 TRACE( "deleting device %s\n", debugstr_w(drive->name.Buffer) );
269 list_remove( &drive->entry );
270 if (drive->dosdev) delete_mount_point( drive->dosdev );
271 if (drive->volume) delete_mount_point( drive->volume );
272 if (drive->symlink.Buffer)
274 IoDeleteSymbolicLink( &drive->symlink );
275 RtlFreeUnicodeString( &drive->symlink );
277 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_device );
278 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
279 RtlFreeHeap( GetProcessHeap(), 0, drive->udi );
280 RtlFreeUnicodeString( &drive->name );
281 IoDeleteDevice( drive->device );
284 /* set or change the drive letter for an existing drive */
285 static void set_drive_letter( struct dos_drive *drive, int letter )
287 void *id = NULL;
288 unsigned int id_len = 0;
290 if (drive->drive == letter) return;
291 if (drive->dosdev) delete_mount_point( drive->dosdev );
292 if (drive->volume) delete_mount_point( drive->volume );
293 drive->drive = letter;
294 if (letter == -1) return;
295 if (drive->unix_mount)
297 id = drive->unix_mount;
298 id_len = strlen( drive->unix_mount ) + 1;
300 drive->dosdev = add_dosdev_mount_point( drive->device, &drive->name, letter, id, id_len );
301 drive->volume = add_volume_mount_point( drive->device, &drive->name, letter, id, id_len );
304 static inline int is_valid_device( struct stat *st )
306 #if defined(linux) || defined(__sun__)
307 return S_ISBLK( st->st_mode );
308 #else
309 /* disks are char devices on *BSD */
310 return S_ISCHR( st->st_mode );
311 #endif
314 /* find or create a DOS drive for the corresponding device */
315 static int add_drive( const char *device, enum device_type type )
317 char *path, *p;
318 char in_use[26];
319 struct stat dev_st, drive_st;
320 int drive, first, last, avail = 0;
322 if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
324 if (!(path = get_dosdevices_path( &p ))) return -1;
326 memset( in_use, 0, sizeof(in_use) );
328 switch (type)
330 case DEVICE_FLOPPY:
331 first = 0;
332 last = 2;
333 break;
334 case DEVICE_CDROM:
335 first = 3;
336 last = 26;
337 break;
338 default:
339 first = 2;
340 last = 26;
341 break;
344 while (avail != -1)
346 avail = -1;
347 for (drive = first; drive < last; drive++)
349 if (in_use[drive]) continue; /* already checked */
350 *p = 'a' + drive;
351 if (stat( path, &drive_st ) == -1)
353 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
355 if (avail == -1)
357 p[2] = 0;
358 /* if mount point symlink doesn't exist either, it's available */
359 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
360 p[2] = ':';
363 else in_use[drive] = 1;
365 else
367 in_use[drive] = 1;
368 if (!is_valid_device( &drive_st )) continue;
369 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
372 if (avail != -1)
374 /* try to use the one we found */
375 drive = avail;
376 *p = 'a' + drive;
377 if (symlink( device, path ) != -1) goto done;
378 /* failed, retry the search */
381 drive = -1;
383 done:
384 HeapFree( GetProcessHeap(), 0, path );
385 return drive;
388 static BOOL set_unix_mount_point( struct dos_drive *drive, const char *mount_point )
390 char *path, *p;
391 BOOL modified = FALSE;
393 if (!(path = get_dosdevices_path( &p ))) return FALSE;
394 p[0] = 'a' + drive->drive;
395 p[2] = 0;
397 if (mount_point && mount_point[0])
399 /* try to avoid unlinking if already set correctly */
400 if (!drive->unix_mount || strcmp( drive->unix_mount, mount_point ))
402 unlink( path );
403 symlink( mount_point, path );
404 modified = TRUE;
406 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
407 drive->unix_mount = strdupA( mount_point );
408 if (drive->dosdev) set_mount_point_id( drive->dosdev, mount_point, strlen(mount_point) + 1 );
409 if (drive->volume) set_mount_point_id( drive->volume, mount_point, strlen(mount_point) + 1 );
411 else
413 if (unlink( path ) != -1) modified = TRUE;
414 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
415 drive->unix_mount = NULL;
416 if (drive->dosdev) set_mount_point_id( drive->dosdev, NULL, 0 );
417 if (drive->volume) set_mount_point_id( drive->volume, NULL, 0 );
420 HeapFree( GetProcessHeap(), 0, path );
421 return modified;
424 /* create devices for mapped drives */
425 static void create_drive_devices(void)
427 char *path, *p, *link, *device;
428 struct dos_drive *drive;
429 unsigned int i;
430 HKEY drives_key;
431 enum device_type drive_type;
432 WCHAR driveW[] = {'a',':',0};
434 if (!(path = get_dosdevices_path( &p ))) return;
435 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &drives_key )) drives_key = 0;
437 for (i = 0; i < MAX_DOS_DRIVES; i++)
439 p[0] = 'a' + i;
440 p[2] = 0;
441 if (!(link = read_symlink( path ))) continue;
442 p[2] = ':';
443 device = read_symlink( path );
445 drive_type = i < 2 ? DEVICE_FLOPPY : DEVICE_HARDDISK_VOL;
446 if (drives_key)
448 WCHAR buffer[32];
449 DWORD j, type, size = sizeof(buffer);
451 driveW[0] = 'a' + i;
452 if (!RegQueryValueExW( drives_key, driveW, NULL, &type, (BYTE *)buffer, &size ) &&
453 type == REG_SZ)
455 for (j = 0; j < sizeof(drive_types)/sizeof(drive_types[0]); j++)
456 if (drive_types[j][0] && !strcmpiW( buffer, drive_types[j] ))
458 drive_type = j;
459 break;
461 if (drive_type == DEVICE_FLOPPY && i >= 2) drive_type = DEVICE_HARDDISK;
465 if (!create_disk_device( NULL, drive_type, &drive ))
467 drive->unix_mount = link;
468 drive->unix_device = device;
469 set_drive_letter( drive, i );
471 else
473 RtlFreeHeap( GetProcessHeap(), 0, link );
474 RtlFreeHeap( GetProcessHeap(), 0, device );
477 RegCloseKey( drives_key );
478 RtlFreeHeap( GetProcessHeap(), 0, path );
481 /* create a new dos drive */
482 NTSTATUS add_dos_device( int letter, const char *udi, const char *device,
483 const char *mount_point, enum device_type type )
485 struct dos_drive *drive, *next;
487 if (letter == -1) /* auto-assign a letter */
489 letter = add_drive( device, type );
490 if (letter == -1) return STATUS_OBJECT_NAME_COLLISION;
492 else /* simply reset the device symlink */
494 char *path, *p;
496 if (!(path = get_dosdevices_path( &p ))) return STATUS_NO_MEMORY;
497 *p = 'a' + letter;
498 unlink( path );
499 if (device) symlink( device, path );
502 LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
504 if (udi && drive->udi && !strcmp( udi, drive->udi ))
506 if (type == drive->type) goto found;
507 delete_disk_device( drive );
508 continue;
510 if (drive->drive == letter) delete_disk_device( drive );
513 if (create_disk_device( udi, type, &drive )) return STATUS_NO_MEMORY;
515 found:
516 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_device );
517 drive->unix_device = strdupA( device );
518 set_drive_letter( drive, letter );
519 set_unix_mount_point( drive, mount_point );
521 if (drive->drive != -1)
523 HKEY hkey;
525 TRACE( "added device %c: udi %s for %s on %s type %u\n",
526 'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
527 wine_dbgstr_a(mount_point), type );
529 /* hack: force the drive type in the registry */
530 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
532 const WCHAR *type_name = drive_types[type];
533 WCHAR name[3] = {'a',':',0};
535 name[0] += drive->drive;
536 if (!type_name[0] && type == DEVICE_HARDDISK) type_name = drive_types[DEVICE_FLOPPY];
537 if (type_name[0])
538 RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
539 (strlenW(type_name) + 1) * sizeof(WCHAR) );
540 else
541 RegDeleteValueW( hkey, name );
542 RegCloseKey( hkey );
545 if (udi) send_notify( drive->drive, DBT_DEVICEARRIVAL );
547 return STATUS_SUCCESS;
550 /* remove an existing dos drive, by letter or udi */
551 NTSTATUS remove_dos_device( int letter, const char *udi )
553 HKEY hkey;
554 struct dos_drive *drive;
556 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
558 if (letter != -1 && drive->drive != letter) continue;
559 if (udi)
561 if (!drive->udi) continue;
562 if (strcmp( udi, drive->udi )) continue;
565 if (drive->drive != -1)
567 BOOL modified = set_unix_mount_point( drive, NULL );
569 /* clear the registry key too */
570 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
572 WCHAR name[3] = {'a',':',0};
573 name[0] += drive->drive;
574 RegDeleteValueW( hkey, name );
575 RegCloseKey( hkey );
578 if (modified && udi) send_notify( drive->drive, DBT_DEVICEREMOVECOMPLETE );
580 delete_disk_device( drive );
581 return STATUS_SUCCESS;
583 return STATUS_NO_SUCH_DEVICE;
586 /* query information about an existing dos drive, by letter or udi */
587 NTSTATUS query_dos_device( int letter, enum device_type *type,
588 const char **device, const char **mount_point )
590 struct dos_drive *drive;
592 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
594 if (drive->drive != letter) continue;
595 if (type) *type = drive->type;
596 if (device) *device = drive->unix_device;
597 if (mount_point) *mount_point = drive->unix_mount;
598 return STATUS_SUCCESS;
600 return STATUS_NO_SUCH_DEVICE;
603 /* handler for ioctls on the harddisk device */
604 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
606 IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
607 struct dos_drive *drive = device->DeviceExtension;
609 TRACE( "ioctl %x insize %u outsize %u\n",
610 irpsp->Parameters.DeviceIoControl.IoControlCode,
611 irpsp->Parameters.DeviceIoControl.InputBufferLength,
612 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
614 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
616 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
618 DISK_GEOMETRY info;
619 DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
621 info.Cylinders.QuadPart = 10000;
622 info.MediaType = (drive->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
623 info.TracksPerCylinder = 255;
624 info.SectorsPerTrack = 63;
625 info.BytesPerSector = 512;
626 memcpy( irp->MdlAddress->StartVa, &info, len );
627 irp->IoStatus.Information = len;
628 irp->IoStatus.u.Status = STATUS_SUCCESS;
629 break;
631 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
633 DWORD len = min( sizeof(drive->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
635 memcpy( irp->MdlAddress->StartVa, &drive->devnum, len );
636 irp->IoStatus.Information = len;
637 irp->IoStatus.u.Status = STATUS_SUCCESS;
638 break;
640 case IOCTL_CDROM_READ_TOC:
641 irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
642 break;
643 default:
644 FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
645 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
646 break;
648 return irp->IoStatus.u.Status;
651 /* driver entry point for the harddisk driver */
652 NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
654 struct dos_drive *drive;
656 harddisk_driver = driver;
657 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
659 /* create a harddisk0 device that isn't assigned to any drive */
660 create_disk_device( "harddisk0 placeholder", DEVICE_HARDDISK, &drive );
662 create_drive_devices();
664 return STATUS_SUCCESS;