user32: Further implementation of MNS_NOTIFYBYPOS.
[wine] / dlls / mountmgr.sys / mountmgr.c
1 /*
2  * Mount manager service implementation
3  *
4  * Copyright 2008 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 <stdarg.h>
22 #include <unistd.h>
23
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winternl.h"
32 #include "winioctl.h"
33 #include "winreg.h"
34 #include "ntddstor.h"
35 #include "ntddcdrm.h"
36 #include "ddk/wdm.h"
37 #include "ddk/mountmgr.h"
38 #include "wine/library.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41 #include "mountmgr.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
44
45 #define MAX_DOS_DRIVES 26
46 #define MAX_MOUNT_POINTS (2 * MAX_DOS_DRIVES)
47
48 /* extra info for disk devices, stored in DeviceExtension */
49 struct disk_device_info
50 {
51     UNICODE_STRING        name;     /* device name */
52     STORAGE_DEVICE_NUMBER devnum;   /* device number info */
53 };
54
55 struct mount_point
56 {
57     DEVICE_OBJECT *device;
58     UNICODE_STRING link;    /* DOS device symlink */
59     void          *id;      /* device unique id */
60     unsigned int   id_len;
61 };
62
63 static struct mount_point mount_points[MAX_MOUNT_POINTS];
64 static HKEY mount_key;
65
66 static inline UNICODE_STRING *get_device_name( DEVICE_OBJECT *dev )
67 {
68     return &((struct disk_device_info *)dev->DeviceExtension)->name;
69 }
70
71 /* read a Unix symlink; returned buffer must be freed by caller */
72 static char *read_symlink( const char *path )
73 {
74     char *buffer;
75     int ret, size = 128;
76
77     for (;;)
78     {
79         if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
80         {
81             SetLastError( ERROR_NOT_ENOUGH_MEMORY );
82             return 0;
83         }
84         ret = readlink( path, buffer, size );
85         if (ret == -1)
86         {
87             RtlFreeHeap( GetProcessHeap(), 0, buffer );
88             return 0;
89         }
90         if (ret != size)
91         {
92             buffer[ret] = 0;
93             return buffer;
94         }
95         RtlFreeHeap( GetProcessHeap(), 0, buffer );
96         size *= 2;
97     }
98 }
99
100 static NTSTATUS create_disk_device( DRIVER_OBJECT *driver, DWORD type, DEVICE_OBJECT **dev_obj )
101 {
102     static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e',
103                                       '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
104     static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
105     static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
106
107     UINT i, first = 0;
108     NTSTATUS status = 0;
109     const WCHAR *format;
110     UNICODE_STRING name;
111     struct disk_device_info *info;
112
113     switch(type)
114     {
115     case DRIVE_REMOVABLE:
116         format = floppyW;
117         break;
118     case DRIVE_CDROM:
119         format = cdromW;
120         break;
121     case DRIVE_FIXED:
122     default:  /* FIXME */
123         format = harddiskW;
124         first = 1;  /* harddisk volumes start counting from 1 */
125         break;
126     }
127
128     name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
129     name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
130     for (i = first; i < 32; i++)
131     {
132         sprintfW( name.Buffer, format, i );
133         name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
134         status = IoCreateDevice( driver, sizeof(*info), &name, 0, 0, FALSE, dev_obj );
135         if (status != STATUS_OBJECT_NAME_COLLISION) break;
136     }
137     if (!status)
138     {
139         info = (*dev_obj)->DeviceExtension;
140         info->name = name;
141         switch(type)
142         {
143         case DRIVE_REMOVABLE:
144             info->devnum.DeviceType = FILE_DEVICE_DISK;
145             info->devnum.DeviceNumber = i;
146             info->devnum.PartitionNumber = ~0u;
147             break;
148         case DRIVE_CDROM:
149             info->devnum.DeviceType = FILE_DEVICE_CD_ROM;
150             info->devnum.DeviceNumber = i;
151             info->devnum.PartitionNumber = ~0u;
152             break;
153         case DRIVE_FIXED:
154         default:  /* FIXME */
155             info->devnum.DeviceType = FILE_DEVICE_DISK;
156             info->devnum.DeviceNumber = 0;
157             info->devnum.PartitionNumber = i;
158             break;
159         }
160     }
161     else
162     {
163         FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
164         RtlFreeUnicodeString( &name );
165     }
166     return status;
167 }
168
169
170 static NTSTATUS add_mount_point( DRIVER_OBJECT *driver, DWORD type, int drive,
171                                  const void *id, unsigned int id_len )
172 {
173     static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
174     static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
175                                     '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
176                                     '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
177                                     '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
178     WCHAR *drive_link, *volume_link;
179     NTSTATUS status;
180     GUID guid;
181     UINT i;
182     struct mount_point *mount_drive = NULL, *mount_volume = NULL;
183
184     /* find two free mount points */
185
186     for (i = 0; i < MAX_MOUNT_POINTS; i++)
187     {
188         if (mount_points[i].device) continue;
189         if (!mount_drive)
190         {
191             mount_drive = &mount_points[i];
192             continue;
193         }
194         mount_volume = &mount_points[i];
195         break;
196     }
197     if (!mount_volume) return STATUS_NO_MEMORY;
198
199     /* create the volume */
200
201     memset( &guid, 0, sizeof(guid) );  /* FIXME */
202     guid.Data4[7] = 'A' + drive;
203
204     drive_link = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(driveW) );
205     volume_link = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(volumeW) );
206     sprintfW( drive_link, driveW, 'A' + drive );
207     sprintfW( volume_link, volumeW, guid.Data1, guid.Data2, guid.Data3,
208               guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
209               guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
210
211     RtlInitUnicodeString( &mount_drive->link, drive_link );
212     RtlInitUnicodeString( &mount_volume->link, volume_link );
213     status = create_disk_device( driver, type, &mount_drive->device );
214     if (status)
215     {
216         RtlFreeUnicodeString( &mount_drive->link );
217         RtlFreeUnicodeString( &mount_volume->link );
218         return status;
219     }
220
221     mount_volume->device = mount_drive->device;  /* FIXME: incr ref count */
222     mount_drive->id = RtlAllocateHeap( GetProcessHeap(), 0, id_len );
223     mount_drive->id_len = id_len;
224     memcpy( mount_drive->id, id, id_len );
225     mount_volume->id = RtlAllocateHeap( GetProcessHeap(), 0, id_len );
226     mount_volume->id_len = id_len;
227     memcpy( mount_volume->id, id, id_len );
228
229     IoCreateSymbolicLink( &mount_drive->link, get_device_name(mount_drive->device) );
230     IoCreateSymbolicLink( &mount_volume->link, get_device_name(mount_volume->device) );
231
232     TRACE( "created device %s symlinks %s %s\n", debugstr_w(get_device_name(mount_drive->device)->Buffer),
233            debugstr_w(mount_drive->link.Buffer), debugstr_w(mount_volume->link.Buffer) );
234
235     RegSetValueExW( mount_key, mount_drive->link.Buffer, 0, REG_BINARY,
236                     mount_drive->id, mount_drive->id_len );
237     RegSetValueExW( mount_key, mount_volume->link.Buffer, 0, REG_BINARY,
238                     mount_volume->id, mount_volume->id_len );
239
240     return STATUS_SUCCESS;
241 }
242
243 /* check if a given mount point matches the requested specs */
244 static BOOL matching_mount_point( const struct mount_point *mount, const MOUNTMGR_MOUNT_POINT *spec )
245 {
246     if (spec->SymbolicLinkNameOffset)
247     {
248         const WCHAR *name = (const WCHAR *)((const char *)spec + spec->SymbolicLinkNameOffset);
249         if (spec->SymbolicLinkNameLength != mount->link.Length) return FALSE;
250         if (memicmpW( name, mount->link.Buffer, mount->link.Length/sizeof(WCHAR)))
251             return FALSE;
252     }
253     if (spec->DeviceNameOffset)
254     {
255         const WCHAR *name = (const WCHAR *)((const char *)spec + spec->DeviceNameOffset);
256         const UNICODE_STRING *dev_name = get_device_name( mount->device );
257         if (spec->DeviceNameLength != dev_name->Length) return FALSE;
258         if (memicmpW( name, dev_name->Buffer, dev_name->Length/sizeof(WCHAR)))
259             return FALSE;
260     }
261     if (spec->UniqueIdOffset)
262     {
263         const void *id = ((const char *)spec + spec->UniqueIdOffset);
264         if (spec->UniqueIdLength != mount->id_len) return FALSE;
265         if (memcmp( id, mount->id, mount->id_len )) return FALSE;
266     }
267     return TRUE;
268 }
269
270 /* implementation of IOCTL_MOUNTMGR_QUERY_POINTS */
271 static NTSTATUS query_mount_points( const void *in_buff, SIZE_T insize,
272                                     void *out_buff, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
273 {
274     UINT i, j, pos, size;
275     const MOUNTMGR_MOUNT_POINT *input = in_buff;
276     MOUNTMGR_MOUNT_POINTS *info = out_buff;
277     UNICODE_STRING *dev_name;
278
279     /* sanity checks */
280     if (input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength > insize ||
281         input->UniqueIdOffset + input->UniqueIdLength > insize ||
282         input->DeviceNameOffset + input->DeviceNameLength > insize ||
283         input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength < input->SymbolicLinkNameOffset ||
284         input->UniqueIdOffset + input->UniqueIdLength < input->UniqueIdOffset ||
285         input->DeviceNameOffset + input->DeviceNameLength < input->DeviceNameOffset)
286         return STATUS_INVALID_PARAMETER;
287
288     for (i = j = size = 0; i < MAX_MOUNT_POINTS; i++)
289     {
290         if (!mount_points[i].device) continue;
291         if (!matching_mount_point( &mount_points[i], input )) continue;
292         size += get_device_name(mount_points[i].device)->Length;
293         size += mount_points[i].link.Length;
294         j++;
295     }
296     pos = FIELD_OFFSET( MOUNTMGR_MOUNT_POINTS, MountPoints[j] );
297     size += pos;
298
299     if (size > outsize)
300     {
301         if (size >= sizeof(info->Size)) info->Size = size;
302         iosb->Information = sizeof(info->Size);
303         return STATUS_MORE_ENTRIES;
304     }
305
306     info->NumberOfMountPoints = j;
307     for (i = j = 0; i < MAX_MOUNT_POINTS; i++)
308     {
309         if (!mount_points[i].device) continue;
310         if (!matching_mount_point( &mount_points[i], input )) continue;
311         info->MountPoints[j].UniqueIdOffset = 0;  /* FIXME */
312         info->MountPoints[j].UniqueIdLength = 0;
313
314         dev_name = get_device_name( mount_points[i].device );
315         info->MountPoints[j].DeviceNameOffset = pos;
316         info->MountPoints[j].DeviceNameLength = dev_name->Length;
317         memcpy( (char *)out_buff + pos, dev_name->Buffer, dev_name->Length );
318         pos += dev_name->Length;
319
320         info->MountPoints[j].SymbolicLinkNameOffset = pos;
321         info->MountPoints[j].SymbolicLinkNameLength = mount_points[i].link.Length;
322         memcpy( (char *)out_buff + pos, mount_points[i].link.Buffer, mount_points[i].link.Length );
323         pos += mount_points[i].link.Length;
324         j++;
325     }
326     info->Size = pos;
327     iosb->Information = pos;
328     return STATUS_SUCCESS;
329 }
330
331 /* handler for ioctls on the mount manager device */
332 static NTSTATUS WINAPI mountmgr_ioctl( DEVICE_OBJECT *device, IRP *irp )
333 {
334     IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
335
336     TRACE( "ioctl %x insize %u outsize %u\n",
337            irpsp->Parameters.DeviceIoControl.IoControlCode,
338            irpsp->Parameters.DeviceIoControl.InputBufferLength,
339            irpsp->Parameters.DeviceIoControl.OutputBufferLength );
340
341     switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
342     {
343     case IOCTL_MOUNTMGR_QUERY_POINTS:
344         if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(MOUNTMGR_MOUNT_POINT))
345             return STATUS_INVALID_PARAMETER;
346         irp->IoStatus.u.Status = query_mount_points( irpsp->Parameters.DeviceIoControl.Type3InputBuffer,
347                                                      irpsp->Parameters.DeviceIoControl.InputBufferLength,
348                                                      irp->MdlAddress->StartVa,
349                                                      irpsp->Parameters.DeviceIoControl.OutputBufferLength,
350                                                      &irp->IoStatus );
351         break;
352     default:
353         FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
354         irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
355         break;
356     }
357     return irp->IoStatus.u.Status;
358 }
359
360 /* handler for ioctls on the harddisk device */
361 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
362 {
363     IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
364     struct disk_device_info *disk_info = device->DeviceExtension;
365
366     TRACE( "ioctl %x insize %u outsize %u\n",
367            irpsp->Parameters.DeviceIoControl.IoControlCode,
368            irpsp->Parameters.DeviceIoControl.InputBufferLength,
369            irpsp->Parameters.DeviceIoControl.OutputBufferLength );
370
371     switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
372     {
373     case IOCTL_DISK_GET_DRIVE_GEOMETRY:
374     {
375         DISK_GEOMETRY info;
376         DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
377
378         info.Cylinders.QuadPart = 10000;
379         info.MediaType = (disk_info->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
380         info.TracksPerCylinder = 255;
381         info.SectorsPerTrack = 63;
382         info.BytesPerSector = 512;
383         memcpy( irp->MdlAddress->StartVa, &info, len );
384         irp->IoStatus.Information = len;
385         irp->IoStatus.u.Status = STATUS_SUCCESS;
386         break;
387     }
388     case IOCTL_STORAGE_GET_DEVICE_NUMBER:
389     {
390         DWORD len = min( sizeof(disk_info->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
391
392         memcpy( irp->MdlAddress->StartVa, &disk_info->devnum, len );
393         irp->IoStatus.Information = len;
394         irp->IoStatus.u.Status = STATUS_SUCCESS;
395         break;
396     }
397     case IOCTL_CDROM_READ_TOC:
398         irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
399         break;
400     default:
401         FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
402         irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
403         break;
404     }
405     return irp->IoStatus.u.Status;
406 }
407
408 /* create mount points for mapped drives */
409 static void create_drive_mount_points( DRIVER_OBJECT *driver )
410 {
411     const char *config_dir = wine_get_config_dir();
412     char *buffer, *p, *link;
413     unsigned int i;
414
415     if ((buffer = RtlAllocateHeap( GetProcessHeap(), 0,
416                                    strlen(config_dir) + sizeof("/dosdevices/a:") )))
417     {
418         strcpy( buffer, config_dir );
419         strcat( buffer, "/dosdevices/a:" );
420         p = buffer + strlen(buffer) - 2;
421
422         for (i = 0; i < MAX_DOS_DRIVES; i++)
423         {
424             *p = 'a' + i;
425             if (!(link = read_symlink( buffer ))) continue;
426             add_mount_point( driver, DRIVE_FIXED, i, link, strlen(link) );
427             RtlFreeHeap( GetProcessHeap(), 0, link );
428         }
429         RtlFreeHeap( GetProcessHeap(), 0, buffer );
430     }
431 }
432
433 /* driver entry point for the harddisk driver */
434 static NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
435 {
436     static const WCHAR mounted_devicesW[] = {'S','y','s','t','e','m','\\',
437                                              'M','o','u','n','t','e','d','D','e','v','i','c','e','s',0};
438     static const WCHAR harddisk0W[] = {'\\','D','e','v','i','c','e',
439                                        '\\','H','a','r','d','d','i','s','k','0',0};
440     static const WCHAR physdrive0W[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','0',0};
441
442     UNICODE_STRING nameW, linkW;
443     DEVICE_OBJECT *device;
444     NTSTATUS status;
445     struct disk_device_info *info;
446
447     driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
448
449     RegCreateKeyExW( HKEY_LOCAL_MACHINE, mounted_devicesW, 0, NULL,
450                      REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &mount_key, NULL );
451
452     RtlInitUnicodeString( &nameW, harddisk0W );
453     RtlInitUnicodeString( &linkW, physdrive0W );
454     if (!(status = IoCreateDevice( driver, sizeof(*info), &nameW, 0, 0, FALSE, &device )))
455         status = IoCreateSymbolicLink( &linkW, &nameW );
456     if (status)
457     {
458         FIXME( "failed to create device error %x\n", status );
459         return status;
460     }
461     info = device->DeviceExtension;
462     info->name = nameW;
463     info->devnum.DeviceType = FILE_DEVICE_DISK;
464     info->devnum.DeviceNumber = 0;
465     info->devnum.PartitionNumber = 0;
466
467     create_drive_mount_points( driver );
468
469     return status;
470 }
471
472 /* main entry point for the mount point manager driver */
473 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
474 {
475     static const WCHAR device_mountmgrW[] = {'\\','D','e','v','i','c','e','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
476     static const WCHAR link_mountmgrW[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
477     static const WCHAR harddiskW[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
478
479     UNICODE_STRING nameW, linkW;
480     DEVICE_OBJECT *device;
481     NTSTATUS status;
482
483     TRACE( "%s\n", debugstr_w(path->Buffer) );
484
485     driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = mountmgr_ioctl;
486
487     RtlInitUnicodeString( &nameW, device_mountmgrW );
488     RtlInitUnicodeString( &linkW, link_mountmgrW );
489     if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
490         status = IoCreateSymbolicLink( &linkW, &nameW );
491     if (status)
492     {
493         FIXME( "failed to create device error %x\n", status );
494         return status;
495     }
496
497     initialize_hal();
498     initialize_diskarbitration();
499
500     RtlInitUnicodeString( &nameW, harddiskW );
501     status = IoCreateDriver( &nameW, harddisk_driver_entry );
502
503     return status;
504 }