xref: /reactos/drivers/storage/mountmgr/mntmgr.h (revision 8a92b556)
1 #ifndef _MNTMGR_H_
2 #define _MNTMGR_H_
3 
4 #include <ntifs.h>
5 #include <mountdev.h>
6 #include <ntddvol.h>
7 #include <ntdddisk.h>
8 #include <wdmguid.h>
9 #include <ndk/psfuncs.h>
10 #include <ntdddisk.h>
11 #include <section_attribs.h>
12 
13 typedef struct _DEVICE_EXTENSION
14 {
15     PDEVICE_OBJECT DeviceObject;           // 0x0
16     PDRIVER_OBJECT DriverObject;           // 0x4
17     LIST_ENTRY DeviceListHead;             // 0x8
18     LIST_ENTRY OfflineDeviceListHead;      // 0x10
19     PVOID NotificationEntry;               // 0x18
20     KSEMAPHORE DeviceLock;                 // 0x1C
21     KSEMAPHORE RemoteDatabaseLock;         // 0x30
22     ULONG AutomaticDriveLetter;            // 0x44
23     LIST_ENTRY IrpListHead;                // 0x48
24     ULONG EpicNumber;                      // 0x50
25     LIST_ENTRY SavedLinksListHead;         // 0x54
26     BOOLEAN ProcessedSuggestions;          // 0x5C
27     BOOLEAN NoAutoMount;                   // 0x5D
28     LIST_ENTRY WorkerQueueListHead;        // 0x60
29     KSEMAPHORE WorkerSemaphore;            // 0x68
30     LONG WorkerReferences;                 // 0x7C
31     KSPIN_LOCK WorkerLock;                 // 0x80
32     LIST_ENTRY UniqueIdWorkerItemListHead; // 0x84
33     PMOUNTDEV_UNIQUE_ID DriveLetterData;   // 0x8C
34     UNICODE_STRING RegistryPath;           // 0x90
35     LONG WorkerThreadStatus;               // 0x98
36     LIST_ENTRY OnlineNotificationListHead; // 0x9C
37     ULONG OnlineNotificationWorkerActive;  // 0xA4
38     ULONG OnlineNotificationCount;         // 0xA8
39     KEVENT OnlineNotificationEvent;        // 0xAC
40 } DEVICE_EXTENSION, *PDEVICE_EXTENSION;    // 0xBC
41 
42 typedef struct _DEVICE_INFORMATION
43 {
44     LIST_ENTRY DeviceListEntry;             // 0x00
45     LIST_ENTRY SymbolicLinksListHead;       // 0x08
46     LIST_ENTRY ReplicatedUniqueIdsListHead; // 0x10
47     LIST_ENTRY AssociatedDevicesHead;       // 0x18
48     UNICODE_STRING SymbolicName;            // 0x20
49     PMOUNTDEV_UNIQUE_ID UniqueId;           // 0x28
50     UNICODE_STRING DeviceName;              // 0x2C
51     BOOLEAN KeepLinks;                      // 0x34
52     UCHAR SuggestedDriveLetter;             // 0x35
53     BOOLEAN ManuallyRegistered;             // 0x36
54     BOOLEAN Removable;                      // 0x37
55     BOOLEAN LetterAssigned;                 // 0x38
56     BOOLEAN NeedsReconcile;                 // 0x39
57     BOOLEAN NoDatabase;                     // 0x3A
58     BOOLEAN SkipNotifications;              // 0x3B
59     ULONG Migrated;                         // 0x3C
60     LONG MountState;                        // 0x40
61     PVOID TargetDeviceNotificationEntry;    // 0x44
62     PDEVICE_EXTENSION DeviceExtension;      // 0x48
63 } DEVICE_INFORMATION, *PDEVICE_INFORMATION; // 0x4C
64 
65 typedef struct _SYMLINK_INFORMATION
66 {
67     LIST_ENTRY SymbolicLinksListEntry;        // 0x00
68     UNICODE_STRING Name;                      // 0x08
69     BOOLEAN Online;                           // 0x10
70 } SYMLINK_INFORMATION, *PSYMLINK_INFORMATION; // 0x14
71 
72 typedef struct _SAVED_LINK_INFORMATION
73 {
74     LIST_ENTRY SavedLinksListEntry;                 // 0x0
75     LIST_ENTRY SymbolicLinksListHead;               // 0x8
76     PMOUNTDEV_UNIQUE_ID UniqueId;                   // 0x10
77 } SAVED_LINK_INFORMATION, *PSAVED_LINK_INFORMATION; // 0x14
78 
79 typedef struct _UNIQUE_ID_REPLICATE
80 {
81     LIST_ENTRY ReplicatedUniqueIdsListEntry;  // 0x0
82     PMOUNTDEV_UNIQUE_ID UniqueId;             // 0x8
83 } UNIQUE_ID_REPLICATE, *PUNIQUE_ID_REPLICATE; // 0xC
84 
85 typedef struct _DATABASE_ENTRY
86 {
87     ULONG EntrySize;                // 0x00
88     ULONG EntryReferences;          // 0x04
89     USHORT SymbolicNameOffset;      // 0x08
90     USHORT SymbolicNameLength;      // 0x0A
91     USHORT UniqueIdOffset;          // 0x0C
92     USHORT UniqueIdLength;          // 0x0E
93 } DATABASE_ENTRY, *PDATABASE_ENTRY; // 0x10
94 
95 typedef struct _ASSOCIATED_DEVICE_ENTRY
96 {
97     LIST_ENTRY AssociatedDevicesEntry;                // 0x00
98     PDEVICE_INFORMATION DeviceInformation;            // 0x08
99     UNICODE_STRING String;                            // 0x0C
100 } ASSOCIATED_DEVICE_ENTRY, *PASSOCIATED_DEVICE_ENTRY; // 0x14
101 
102 typedef struct _DEVICE_INFORMATION_ENTRY
103 {
104     LIST_ENTRY DeviceInformationEntry;                  // 0x00
105     PDEVICE_INFORMATION DeviceInformation;              // 0x08
106 } DEVICE_INFORMATION_ENTRY, *PDEVICE_INFORMATION_ENTRY; // 0x0C
107 
108 typedef struct _ONLINE_NOTIFICATION_WORK_ITEM
109 {
110     WORK_QUEUE_ITEM WorkItem;                                     // 0x00
111     PDEVICE_EXTENSION DeviceExtension;                            // 0x10
112     UNICODE_STRING SymbolicName;                                  // 0x14
113 } ONLINE_NOTIFICATION_WORK_ITEM, *PONLINE_NOTIFICATION_WORK_ITEM; // 0x1C
114 
115 typedef struct _RECONCILE_WORK_ITEM_CONTEXT
116 {
117     PDEVICE_EXTENSION DeviceExtension;
118     PDEVICE_INFORMATION DeviceInformation;
119 } RECONCILE_WORK_ITEM_CONTEXT, *PRECONCILE_WORK_ITEM_CONTEXT;
120 
121 typedef struct _RECONCILE_WORK_ITEM
122 {
123     LIST_ENTRY WorkerQueueListEntry;            // 0x00
124     PIO_WORKITEM WorkItem;                      // 0x08
125     PWORKER_THREAD_ROUTINE WorkerRoutine;       // 0x0C
126     PVOID Context;                              // 0x10
127     RECONCILE_WORK_ITEM_CONTEXT;                // 0x14
128 } RECONCILE_WORK_ITEM, *PRECONCILE_WORK_ITEM;   // 0x1C
129 
130 typedef struct _MIGRATE_WORK_ITEM
131 {
132     PIO_WORKITEM WorkItem;                 // 0x0
133     PDEVICE_INFORMATION DeviceInformation; // 0x4
134     PKEVENT Event;                         // 0x8
135     NTSTATUS Status;                       // 0x0C
136     HANDLE Database;                       // 0x10
137 } MIGRATE_WORK_ITEM, *PMIGRATE_WORK_ITEM;  // 0x14
138 
139 typedef struct _UNIQUE_ID_WORK_ITEM
140 {
141     LIST_ENTRY UniqueIdWorkerItemListEntry;   // 0x0
142     PIO_WORKITEM WorkItem;                    // 0x8
143     PDEVICE_EXTENSION DeviceExtension;        // 0xC
144     PIRP Irp;                                 // 0x10
145     PVOID IrpBuffer;                          // 0x14
146     PKEVENT Event;                            // 0x1C
147     UNICODE_STRING DeviceName;                // 0x20
148     ULONG IrpBufferLength;                    // 0x28
149     ULONG StackSize;                          // 0x2C
150 } UNIQUE_ID_WORK_ITEM, *PUNIQUE_ID_WORK_ITEM; // 0x30
151 
152 /* Memory allocation helpers */
153 #define AllocatePool(Size) ExAllocatePoolWithTag(PagedPool, Size, 'AtnM')
154 #define FreePool(P)        ExFreePoolWithTag(P, 'AtnM')
155 
156 /* Misc macros */
157 #define MAX(a, b)          ((a > b) ? a : b)
158 
159 #define LETTER_POSITION     0xC
160 #define COLON_POSITION      0xD
161 #define DRIVE_LETTER_LENGTH 0x1C
162 
163 /* mountmgr.c */
164 
165 extern UNICODE_STRING DosDevicesMount;
166 extern PDEVICE_OBJECT gdeviceObject;
167 extern UNICODE_STRING ReparseIndex;
168 extern UNICODE_STRING DeviceFloppy;
169 extern UNICODE_STRING DeviceMount;
170 extern UNICODE_STRING DeviceCdRom;
171 extern UNICODE_STRING SafeVolumes;
172 extern UNICODE_STRING DosDevices;
173 extern UNICODE_STRING DosGlobal;
174 extern UNICODE_STRING Global;
175 extern UNICODE_STRING Volume;
176 extern KEVENT UnloadEvent;
177 extern LONG Unloading;
178 
179 DRIVER_INITIALIZE DriverEntry;
180 
181 VOID
182 NTAPI
183 MountMgrCancel(
184     IN PDEVICE_OBJECT DeviceObject,
185     IN PIRP Irp
186 );
187 
188 NTSTATUS
189 MountMgrMountedDeviceArrival(
190     IN PDEVICE_EXTENSION Extension,
191     IN PUNICODE_STRING SymbolicName,
192     IN BOOLEAN FromVolume
193 );
194 
195 VOID
196 MountMgrMountedDeviceRemoval(
197     IN PDEVICE_EXTENSION Extension,
198     IN PUNICODE_STRING DeviceName
199 );
200 
201 NTSTATUS
202 FindDeviceInfo(
203     IN PDEVICE_EXTENSION DeviceExtension,
204     IN PUNICODE_STRING SymbolicName,
205     IN BOOLEAN DeviceNameGiven,
206     OUT PDEVICE_INFORMATION * DeviceInformation
207 );
208 
209 VOID
210 MountMgrFreeDeadDeviceInfo(
211     IN PDEVICE_INFORMATION DeviceInformation
212 );
213 
214 NTSTATUS
215 QueryDeviceInformation(
216     IN PUNICODE_STRING SymbolicName,
217     OUT PUNICODE_STRING DeviceName OPTIONAL,
218     OUT PMOUNTDEV_UNIQUE_ID * UniqueId OPTIONAL,
219     OUT PBOOLEAN Removable OPTIONAL,
220     OUT PBOOLEAN GptDriveLetter OPTIONAL,
221     OUT PBOOLEAN HasGuid OPTIONAL,
222     IN OUT LPGUID StableGuid OPTIONAL,
223     OUT PBOOLEAN Valid OPTIONAL
224 );
225 
226 BOOLEAN
227 HasDriveLetter(
228     IN PDEVICE_INFORMATION DeviceInformation
229 );
230 
231 BOOLEAN
232 MountmgrReadNoAutoMount(
233     IN PUNICODE_STRING RegistryPath
234 );
235 
236 /* database.c */
237 
238 extern PWSTR DatabasePath;
239 extern PWSTR OfflinePath;
240 
241 VOID
242 ReconcileThisDatabaseWithMaster(
243     IN PDEVICE_EXTENSION DeviceExtension,
244     IN PDEVICE_INFORMATION DeviceInformation
245 );
246 
247 NTSTATUS
248 WaitForRemoteDatabaseSemaphore(
249     IN PDEVICE_EXTENSION DeviceExtension
250 );
251 
252 VOID
253 ReleaseRemoteDatabaseSemaphore(
254     IN PDEVICE_EXTENSION DeviceExtension
255 );
256 
257 VOID
258 ChangeRemoteDatabaseUniqueId(
259     IN PDEVICE_INFORMATION DeviceInformation,
260     IN PMOUNTDEV_UNIQUE_ID OldUniqueId,
261     IN PMOUNTDEV_UNIQUE_ID NewUniqueId
262 );
263 
264 VOID
265 ReconcileAllDatabasesWithMaster(
266     IN PDEVICE_EXTENSION DeviceExtension
267 );
268 
269 VOID
270 DeleteFromLocalDatabase(
271     IN PUNICODE_STRING SymbolicLink,
272     IN PMOUNTDEV_UNIQUE_ID UniqueId
273 );
274 
275 VOID
276 DeleteRegistryDriveLetter(
277     IN PMOUNTDEV_UNIQUE_ID UniqueId
278 );
279 
280 VOID
281 DeleteNoDriveLetterEntry(
282     IN PMOUNTDEV_UNIQUE_ID UniqueId
283 );
284 
285 NTSTATUS
286 QueryVolumeName(
287     IN HANDLE RootDirectory,
288     IN PFILE_REPARSE_POINT_INFORMATION ReparsePointInformation,
289     IN PUNICODE_STRING FileName OPTIONAL,
290     OUT PUNICODE_STRING SymbolicName,
291     OUT PUNICODE_STRING VolumeName
292 );
293 
294 HANDLE
295 OpenRemoteDatabase(
296     IN PDEVICE_INFORMATION DeviceInformation,
297     IN BOOLEAN MigrateDatabase
298 );
299 
300 PDATABASE_ENTRY
301 GetRemoteDatabaseEntry(
302     IN HANDLE Database,
303     IN LONG StartingOffset
304 );
305 
306 NTSTATUS
307 WriteRemoteDatabaseEntry(
308     IN HANDLE Database,
309     IN LONG Offset,
310     IN PDATABASE_ENTRY Entry
311 );
312 
313 NTSTATUS
314 CloseRemoteDatabase(
315     IN HANDLE Database
316 );
317 
318 NTSTATUS
319 AddRemoteDatabaseEntry(
320     IN HANDLE Database,
321     IN PDATABASE_ENTRY Entry
322 );
323 
324 NTSTATUS
325 DeleteRemoteDatabaseEntry(
326     IN HANDLE Database,
327     IN LONG StartingOffset
328 );
329 
330 VOID
331 NTAPI
332 ReconcileThisDatabaseWithMasterWorker(
333     IN PVOID Parameter
334 );
335 
336 /* device.c */
337 
338 DRIVER_DISPATCH MountMgrDeviceControl;
339 
340 /* notify.c */
341 VOID
342 IssueUniqueIdChangeNotifyWorker(
343     IN PUNIQUE_ID_WORK_ITEM WorkItem,
344     IN PMOUNTDEV_UNIQUE_ID UniqueId
345 );
346 
347 VOID
348 WaitForOnlinesToComplete(
349     IN PDEVICE_EXTENSION DeviceExtension
350 );
351 
352 VOID
353 RegisterForTargetDeviceNotification(
354     IN PDEVICE_EXTENSION DeviceExtension,
355     IN PDEVICE_INFORMATION DeviceInformation
356 );
357 
358 VOID
359 SendOnlineNotification(
360     IN PUNICODE_STRING SymbolicName
361 );
362 
363 VOID
364 IssueUniqueIdChangeNotify(
365     IN PDEVICE_EXTENSION DeviceExtension,
366     IN PUNICODE_STRING DeviceName,
367     IN PMOUNTDEV_UNIQUE_ID UniqueId
368 );
369 
370 VOID
371 PostOnlineNotification(
372     IN PDEVICE_EXTENSION DeviceExtension,
373     IN PUNICODE_STRING SymbolicName
374 );
375 
376 VOID
377 MountMgrNotify(
378     IN PDEVICE_EXTENSION DeviceExtension
379 );
380 
381 VOID
382 MountMgrNotifyNameChange(
383     IN PDEVICE_EXTENSION DeviceExtension,
384     IN PUNICODE_STRING DeviceName,
385     IN BOOLEAN ValidateVolume
386 );
387 
388 /* uniqueid.c */
389 VOID
390 MountMgrUniqueIdChangeRoutine(
391     IN PDEVICE_EXTENSION DeviceExtension,
392     IN PMOUNTDEV_UNIQUE_ID OldUniqueId,
393     IN PMOUNTDEV_UNIQUE_ID NewUniqueId
394 );
395 
396 VOID
397 CreateNoDriveLetterEntry(
398     IN PMOUNTDEV_UNIQUE_ID UniqueId
399 );
400 
401 BOOLEAN
402 HasNoDriveLetterEntry(
403     IN PMOUNTDEV_UNIQUE_ID UniqueId
404 );
405 
406 VOID
407 UpdateReplicatedUniqueIds(
408     IN PDEVICE_INFORMATION DeviceInformation,
409     IN PDATABASE_ENTRY DatabaseEntry
410 );
411 
412 BOOLEAN
413 IsUniqueIdPresent(
414     IN PDEVICE_EXTENSION DeviceExtension,
415     IN PDATABASE_ENTRY DatabaseEntry
416 );
417 
418 /* point.c */
419 NTSTATUS
420 MountMgrCreatePointWorker(
421     IN PDEVICE_EXTENSION DeviceExtension,
422     IN PUNICODE_STRING SymbolicLinkName,
423     IN PUNICODE_STRING DeviceName
424 );
425 
426 NTSTATUS
427 QueryPointsFromSymbolicLinkName(
428     IN PDEVICE_EXTENSION DeviceExtension,
429     IN PUNICODE_STRING SymbolicName,
430     IN PIRP Irp
431 );
432 
433 NTSTATUS
434 QueryPointsFromMemory(
435     IN PDEVICE_EXTENSION DeviceExtension,
436     IN PIRP Irp,
437     IN PMOUNTDEV_UNIQUE_ID UniqueId OPTIONAL,
438     IN PUNICODE_STRING SymbolicName OPTIONAL
439 );
440 
441 /* symlink.c */
442 NTSTATUS
443 GlobalCreateSymbolicLink(
444     IN PUNICODE_STRING DosName,
445     IN PUNICODE_STRING DeviceName
446 );
447 
448 NTSTATUS
449 GlobalDeleteSymbolicLink(
450     IN PUNICODE_STRING DosName
451 );
452 
453 NTSTATUS
454 QuerySuggestedLinkName(
455     IN PUNICODE_STRING SymbolicName,
456     OUT PUNICODE_STRING SuggestedLinkName,
457     OUT PBOOLEAN UseOnlyIfThereAreNoOtherLinks
458 );
459 
460 NTSTATUS
461 QuerySymbolicLinkNamesFromStorage(
462     IN PDEVICE_EXTENSION DeviceExtension,
463     IN PDEVICE_INFORMATION DeviceInformation,
464     IN PUNICODE_STRING SuggestedLinkName,
465     IN BOOLEAN UseOnlyIfThereAreNoOtherLinks,
466     OUT PUNICODE_STRING * SymLinks,
467     OUT PULONG SymLinkCount,
468     IN BOOLEAN HasGuid,
469     IN LPGUID Guid
470 );
471 
472 PSAVED_LINK_INFORMATION
473 RemoveSavedLinks(
474     IN PDEVICE_EXTENSION DeviceExtension,
475     IN PMOUNTDEV_UNIQUE_ID UniqueId
476 );
477 
478 BOOLEAN
479 RedirectSavedLink(
480     IN PSAVED_LINK_INFORMATION SavedLinkInformation,
481     IN PUNICODE_STRING DosName,
482     IN PUNICODE_STRING NewLink
483 );
484 
485 VOID
486 SendLinkCreated(
487     IN PUNICODE_STRING SymbolicName
488 );
489 
490 NTSTATUS
491 CreateNewVolumeName(
492     OUT PUNICODE_STRING VolumeName,
493     IN PGUID VolumeGuid OPTIONAL
494 );
495 
496 BOOLEAN
497 IsDriveLetter(
498     PUNICODE_STRING SymbolicName
499 );
500 
501 VOID
502 DeleteSymbolicLinkNameFromMemory(
503     IN PDEVICE_EXTENSION DeviceExtension,
504     IN PUNICODE_STRING SymbolicLink,
505     IN BOOLEAN MarkOffline
506 );
507 
508 NTSTATUS
509 MountMgrQuerySymbolicLink(
510     IN PUNICODE_STRING SymbolicName,
511     IN OUT PUNICODE_STRING LinkTarget
512 );
513 
514 #endif /* _MNTMGR_H_ */
515