mountmgr: Create a new harddisk device for dynamic harddisk drives.
[wine/wine-kai.git] / dlls / mountmgr.sys / device.c
blob21e0b2ce6b659729548fa2c7d6fc7c74a94c6277
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 }, /* DRIVE_UNKNOWN */
47 { 0 }, /* DRIVE_NO_ROOT_DIR */
48 {'f','l','o','p','p','y',0}, /* DRIVE_REMOVABLE */
49 {'h','d',0}, /* DRIVE_FIXED */
50 {'n','e','t','w','o','r','k',0}, /* DRIVE_REMOTE */
51 {'c','d','r','o','m',0}, /* DRIVE_CDROM */
52 {'r','a','m','d','i','s','k',0} /* DRIVE_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 DWORD type; /* drive type */
64 DEVICE_OBJECT *device; /* disk device allocated for this drive */
65 UNICODE_STRING name; /* device name */
66 STORAGE_DEVICE_NUMBER devnum; /* device number info */
67 struct mount_point *dosdev; /* DosDevices mount point */
68 struct mount_point *volume; /* Volume{xxx} mount point */
69 char *unix_mount; /* unix mount point path */
72 static struct list drives_list = LIST_INIT(drives_list);
74 static DRIVER_OBJECT *harddisk_driver;
76 static char *get_dosdevices_path( char **drive )
78 const char *config_dir = wine_get_config_dir();
79 size_t len = strlen(config_dir) + sizeof("/dosdevices/a::");
80 char *path = HeapAlloc( GetProcessHeap(), 0, len );
81 if (path)
83 strcpy( path, config_dir );
84 strcat( path, "/dosdevices/a::" );
85 *drive = path + len - 4;
87 return path;
90 /* read a Unix symlink; returned buffer must be freed by caller */
91 static char *read_symlink( const char *path )
93 char *buffer;
94 int ret, size = 128;
96 for (;;)
98 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
100 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
101 return 0;
103 ret = readlink( path, buffer, size );
104 if (ret == -1)
106 RtlFreeHeap( GetProcessHeap(), 0, buffer );
107 return 0;
109 if (ret != size)
111 buffer[ret] = 0;
112 return buffer;
114 RtlFreeHeap( GetProcessHeap(), 0, buffer );
115 size *= 2;
119 /* send notification about a change to a given drive */
120 static void send_notify( int drive, int code )
122 DWORD_PTR result;
123 DEV_BROADCAST_VOLUME info;
125 info.dbcv_size = sizeof(info);
126 info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
127 info.dbcv_reserved = 0;
128 info.dbcv_unitmask = 1 << drive;
129 info.dbcv_flags = DBTF_MEDIA;
130 result = BroadcastSystemMessageW( BSF_FORCEIFHUNG|BSF_QUERY, NULL,
131 WM_DEVICECHANGE, code, (LPARAM)&info );
134 /* create the disk device for a given drive */
135 static NTSTATUS create_disk_device( const char *udi, DWORD type, struct dos_drive **drive_ret )
137 static const WCHAR harddiskvolW[] = {'\\','D','e','v','i','c','e',
138 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
139 static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
140 static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
141 static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
143 UINT i, first = 0;
144 NTSTATUS status = 0;
145 const WCHAR *format;
146 UNICODE_STRING name;
147 DEVICE_OBJECT *dev_obj;
148 struct dos_drive *drive;
150 switch(type)
152 case DRIVE_REMOVABLE:
153 format = floppyW;
154 break;
155 case DRIVE_CDROM:
156 format = cdromW;
157 break;
158 case DRIVE_FIXED:
159 default: /* FIXME */
160 if (udi) format = harddiskW;
161 else
163 format = harddiskvolW;
164 first = 1; /* harddisk volumes start counting from 1 */
166 break;
169 name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
170 name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
171 for (i = first; i < 32; i++)
173 sprintfW( name.Buffer, format, i );
174 name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
175 status = IoCreateDevice( harddisk_driver, sizeof(*drive), &name, 0, 0, FALSE, &dev_obj );
176 if (status != STATUS_OBJECT_NAME_COLLISION) break;
178 if (!status)
180 drive = dev_obj->DeviceExtension;
181 drive->drive = -1;
182 drive->device = dev_obj;
183 drive->name = name;
184 drive->type = type;
185 drive->dosdev = NULL;
186 drive->volume = NULL;
187 drive->unix_mount = NULL;
188 if (udi)
190 if (!(drive->udi = HeapAlloc( GetProcessHeap(), 0, strlen(udi)+1 )))
192 RtlFreeUnicodeString( &name );
193 IoDeleteDevice( drive->device );
194 return STATUS_NO_MEMORY;
196 strcpy( drive->udi, udi );
198 switch (type)
200 case DRIVE_REMOVABLE:
201 drive->devnum.DeviceType = FILE_DEVICE_DISK;
202 drive->devnum.DeviceNumber = i;
203 drive->devnum.PartitionNumber = ~0u;
204 break;
205 case DRIVE_CDROM:
206 drive->devnum.DeviceType = FILE_DEVICE_CD_ROM;
207 drive->devnum.DeviceNumber = i;
208 drive->devnum.PartitionNumber = ~0u;
209 break;
210 case DRIVE_FIXED:
211 default: /* FIXME */
212 drive->devnum.DeviceType = FILE_DEVICE_DISK;
213 if (udi)
215 drive->devnum.DeviceNumber = i;
216 drive->devnum.PartitionNumber = 0;
218 else
220 drive->devnum.DeviceNumber = 0;
221 drive->devnum.PartitionNumber = i;
223 break;
225 list_add_tail( &drives_list, &drive->entry );
226 *drive_ret = drive;
227 TRACE( "created device %s\n", debugstr_w(name.Buffer) );
229 else
231 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
232 RtlFreeUnicodeString( &name );
234 return status;
237 /* delete the disk device for a given drive */
238 static void delete_disk_device( struct dos_drive *drive )
240 TRACE( "deleting device %s\n", debugstr_w(drive->name.Buffer) );
241 list_remove( &drive->entry );
242 if (drive->dosdev) delete_mount_point( drive->dosdev );
243 if (drive->volume) delete_mount_point( drive->volume );
244 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
245 RtlFreeHeap( GetProcessHeap(), 0, drive->udi );
246 RtlFreeUnicodeString( &drive->name );
247 IoDeleteDevice( drive->device );
250 /* set or change the drive letter for an existing drive */
251 static void set_drive_letter( struct dos_drive *drive, int letter )
253 void *id = NULL;
254 unsigned int id_len = 0;
256 if (drive->drive == letter) return;
257 if (drive->dosdev) delete_mount_point( drive->dosdev );
258 if (drive->volume) delete_mount_point( drive->volume );
259 drive->drive = letter;
260 if (letter == -1) return;
261 if (drive->unix_mount)
263 id = drive->unix_mount;
264 id_len = strlen( drive->unix_mount ) + 1;
266 drive->dosdev = add_dosdev_mount_point( drive->device, &drive->name, letter, id, id_len );
267 drive->volume = add_volume_mount_point( drive->device, &drive->name, letter, id, id_len );
270 static inline int is_valid_device( struct stat *st )
272 #if defined(linux) || defined(__sun__)
273 return S_ISBLK( st->st_mode );
274 #else
275 /* disks are char devices on *BSD */
276 return S_ISCHR( st->st_mode );
277 #endif
280 /* find or create a DOS drive for the corresponding device */
281 static int add_drive( const char *device, DWORD type )
283 char *path, *p;
284 char in_use[26];
285 struct stat dev_st, drive_st;
286 int drive, first, last, avail = 0;
288 if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
290 if (!(path = get_dosdevices_path( &p ))) return -1;
292 memset( in_use, 0, sizeof(in_use) );
294 first = 2;
295 last = 26;
296 if (type == DRIVE_REMOVABLE)
298 first = 0;
299 last = 2;
302 while (avail != -1)
304 avail = -1;
305 for (drive = first; drive < last; drive++)
307 if (in_use[drive]) continue; /* already checked */
308 *p = 'a' + drive;
309 if (stat( path, &drive_st ) == -1)
311 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
313 if (avail == -1)
315 p[2] = 0;
316 /* if mount point symlink doesn't exist either, it's available */
317 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
318 p[2] = ':';
321 else in_use[drive] = 1;
323 else
325 in_use[drive] = 1;
326 if (!is_valid_device( &drive_st )) continue;
327 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
330 if (avail != -1)
332 /* try to use the one we found */
333 drive = avail;
334 *p = 'a' + drive;
335 if (symlink( device, path ) != -1) goto done;
336 /* failed, retry the search */
339 drive = -1;
341 done:
342 HeapFree( GetProcessHeap(), 0, path );
343 return drive;
346 static BOOL set_unix_mount_point( struct dos_drive *drive, const char *mount_point )
348 char *path, *p;
349 BOOL modified = FALSE;
351 if (!(path = get_dosdevices_path( &p ))) return FALSE;
352 p[0] = 'a' + drive->drive;
353 p[2] = 0;
355 if (mount_point && mount_point[0])
357 /* try to avoid unlinking if already set correctly */
358 if (!drive->unix_mount || strcmp( drive->unix_mount, mount_point ))
360 unlink( path );
361 symlink( mount_point, path );
362 modified = TRUE;
364 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
365 if ((drive->unix_mount = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point) + 1 )))
366 strcpy( drive->unix_mount, mount_point );
367 if (drive->dosdev) set_mount_point_id( drive->dosdev, mount_point, strlen(mount_point) + 1 );
368 if (drive->volume) set_mount_point_id( drive->volume, mount_point, strlen(mount_point) + 1 );
370 else
372 if (unlink( path ) != -1) modified = TRUE;
373 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
374 drive->unix_mount = NULL;
375 if (drive->dosdev) set_mount_point_id( drive->dosdev, NULL, 0 );
376 if (drive->volume) set_mount_point_id( drive->volume, NULL, 0 );
379 HeapFree( GetProcessHeap(), 0, path );
380 return modified;
383 /* create devices for mapped drives */
384 static void create_drive_devices(void)
386 char *path, *p, *link;
387 struct dos_drive *drive;
388 unsigned int i;
389 HKEY drives_key;
390 DWORD drive_type;
391 WCHAR driveW[] = {'a',':',0};
393 if (!(path = get_dosdevices_path( &p ))) return;
394 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &drives_key )) drives_key = 0;
396 for (i = 0; i < MAX_DOS_DRIVES; i++)
398 p[0] = 'a' + i;
399 p[2] = 0;
400 if (!(link = read_symlink( path ))) continue;
402 drive_type = i < 2 ? DRIVE_REMOVABLE : DRIVE_FIXED;
403 if (drives_key)
405 WCHAR buffer[32];
406 DWORD j, type, size = sizeof(buffer);
408 driveW[0] = 'a' + i;
409 if (!RegQueryValueExW( drives_key, driveW, NULL, &type, (BYTE *)buffer, &size ) &&
410 type == REG_SZ)
412 for (j = 0; j < sizeof(drive_types)/sizeof(drive_types[0]); j++)
413 if (drive_types[j][0] && !strcmpiW( buffer, drive_types[j] ))
415 drive_type = j;
416 break;
421 if (!create_disk_device( NULL, drive_type, &drive ))
423 drive->unix_mount = link;
424 set_drive_letter( drive, i );
426 else RtlFreeHeap( GetProcessHeap(), 0, link );
428 RegCloseKey( drives_key );
429 RtlFreeHeap( GetProcessHeap(), 0, path );
432 BOOL add_dos_device( const char *udi, const char *device, const char *mount_point, DWORD type )
434 struct dos_drive *drive, *next;
435 int letter = add_drive( device, type );
437 if (letter == -1) return FALSE;
439 LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
441 if (drive->udi && !strcmp( udi, drive->udi ))
443 if (type == drive->type) goto found;
444 delete_disk_device( drive );
445 continue;
447 if (drive->drive == letter) delete_disk_device( drive );
450 if (create_disk_device( udi, type, &drive )) return FALSE;
452 found:
453 set_drive_letter( drive, letter );
454 set_unix_mount_point( drive, mount_point );
456 if (drive->drive != -1)
458 HKEY hkey;
460 TRACE( "added device %c: udi %s for %s on %s type %u\n",
461 'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
462 wine_dbgstr_a(mount_point), type );
464 /* hack: force the drive type in the registry */
465 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
467 const WCHAR *type_name = drive_types[type];
468 WCHAR name[3] = {'a',':',0};
470 name[0] += drive->drive;
471 if (type_name[0])
472 RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
473 (strlenW(type_name) + 1) * sizeof(WCHAR) );
474 else
475 RegDeleteValueW( hkey, name );
476 RegCloseKey( hkey );
479 send_notify( drive->drive, DBT_DEVICEARRIVAL );
481 return TRUE;
484 BOOL remove_dos_device( const char *udi )
486 HKEY hkey;
487 struct dos_drive *drive;
489 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
491 if (!drive->udi || strcmp( udi, drive->udi )) continue;
493 if (drive->drive != -1)
495 BOOL modified = set_unix_mount_point( drive, NULL );
497 /* clear the registry key too */
498 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
500 WCHAR name[3] = {'a',':',0};
501 name[0] += drive->drive;
502 RegDeleteValueW( hkey, name );
503 RegCloseKey( hkey );
506 if (modified) send_notify( drive->drive, DBT_DEVICEREMOVECOMPLETE );
508 delete_disk_device( drive );
509 return TRUE;
511 return FALSE;
514 /* handler for ioctls on the harddisk device */
515 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
517 IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
518 struct dos_drive *drive = device->DeviceExtension;
520 TRACE( "ioctl %x insize %u outsize %u\n",
521 irpsp->Parameters.DeviceIoControl.IoControlCode,
522 irpsp->Parameters.DeviceIoControl.InputBufferLength,
523 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
525 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
527 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
529 DISK_GEOMETRY info;
530 DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
532 info.Cylinders.QuadPart = 10000;
533 info.MediaType = (drive->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
534 info.TracksPerCylinder = 255;
535 info.SectorsPerTrack = 63;
536 info.BytesPerSector = 512;
537 memcpy( irp->MdlAddress->StartVa, &info, len );
538 irp->IoStatus.Information = len;
539 irp->IoStatus.u.Status = STATUS_SUCCESS;
540 break;
542 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
544 DWORD len = min( sizeof(drive->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
546 memcpy( irp->MdlAddress->StartVa, &drive->devnum, len );
547 irp->IoStatus.Information = len;
548 irp->IoStatus.u.Status = STATUS_SUCCESS;
549 break;
551 case IOCTL_CDROM_READ_TOC:
552 irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
553 break;
554 default:
555 FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
556 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
557 break;
559 return irp->IoStatus.u.Status;
562 /* driver entry point for the harddisk driver */
563 NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
565 static const WCHAR harddisk0W[] = {'\\','D','e','v','i','c','e',
566 '\\','H','a','r','d','d','i','s','k','0',0};
567 static const WCHAR physdrive0W[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','0',0};
569 UNICODE_STRING nameW, linkW;
570 DEVICE_OBJECT *device;
571 NTSTATUS status;
572 struct dos_drive *drive;
574 harddisk_driver = driver;
575 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
577 RtlInitUnicodeString( &nameW, harddisk0W );
578 RtlInitUnicodeString( &linkW, physdrive0W );
579 if (!(status = IoCreateDevice( driver, sizeof(*drive), &nameW, 0, 0, FALSE, &device )))
580 status = IoCreateSymbolicLink( &linkW, &nameW );
581 if (status)
583 FIXME( "failed to create device error %x\n", status );
584 return status;
586 drive = device->DeviceExtension;
587 drive->drive = -1;
588 drive->udi = NULL;
589 drive->type = DRIVE_FIXED;
590 drive->name = nameW;
591 drive->device = device;
592 drive->devnum.DeviceType = FILE_DEVICE_DISK;
593 drive->devnum.DeviceNumber = 0;
594 drive->devnum.PartitionNumber = 0;
596 create_drive_devices();
598 return status;