xref: /reactos/drivers/storage/mountmgr/mntmgr.h (revision 81db5e1d)
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 CODE_SEG("INIT")
180 DRIVER_INITIALIZE DriverEntry;
181 
182 VOID
183 NTAPI
184 MountMgrCancel(
185     IN PDEVICE_OBJECT DeviceObject,
186     IN PIRP Irp
187 );
188 
189 NTSTATUS
190 MountMgrMountedDeviceArrival(
191     IN PDEVICE_EXTENSION Extension,
192     IN PUNICODE_STRING SymbolicName,
193     IN BOOLEAN FromVolume
194 );
195 
196 VOID
197 MountMgrMountedDeviceRemoval(
198     IN PDEVICE_EXTENSION Extension,
199     IN PUNICODE_STRING DeviceName
200 );
201 
202 NTSTATUS
203 FindDeviceInfo(
204     IN PDEVICE_EXTENSION DeviceExtension,
205     IN PUNICODE_STRING SymbolicName,
206     IN BOOLEAN DeviceNameGiven,
207     OUT PDEVICE_INFORMATION * DeviceInformation
208 );
209 
210 VOID
211 MountMgrFreeDeadDeviceInfo(
212     IN PDEVICE_INFORMATION DeviceInformation
213 );
214 
215 NTSTATUS
216 QueryDeviceInformation(
217     IN PUNICODE_STRING SymbolicName,
218     OUT PUNICODE_STRING DeviceName OPTIONAL,
219     OUT PMOUNTDEV_UNIQUE_ID * UniqueId OPTIONAL,
220     OUT PBOOLEAN Removable OPTIONAL,
221     OUT PBOOLEAN GptDriveLetter OPTIONAL,
222     OUT PBOOLEAN HasGuid OPTIONAL,
223     IN OUT LPGUID StableGuid OPTIONAL,
224     OUT PBOOLEAN Valid OPTIONAL
225 );
226 
227 BOOLEAN
228 HasDriveLetter(
229     IN PDEVICE_INFORMATION DeviceInformation
230 );
231 
232 CODE_SEG("INIT")
233 BOOLEAN
234 MountmgrReadNoAutoMount(
235     IN PUNICODE_STRING RegistryPath
236 );
237 
238 /* database.c */
239 
240 extern PWSTR DatabasePath;
241 extern PWSTR OfflinePath;
242 
243 VOID
244 ReconcileThisDatabaseWithMaster(
245     IN PDEVICE_EXTENSION DeviceExtension,
246     IN PDEVICE_INFORMATION DeviceInformation
247 );
248 
249 NTSTATUS
250 WaitForRemoteDatabaseSemaphore(
251     IN PDEVICE_EXTENSION DeviceExtension
252 );
253 
254 VOID
255 ReleaseRemoteDatabaseSemaphore(
256     IN PDEVICE_EXTENSION DeviceExtension
257 );
258 
259 VOID
260 ChangeRemoteDatabaseUniqueId(
261     IN PDEVICE_INFORMATION DeviceInformation,
262     IN PMOUNTDEV_UNIQUE_ID OldUniqueId,
263     IN PMOUNTDEV_UNIQUE_ID NewUniqueId
264 );
265 
266 VOID
267 ReconcileAllDatabasesWithMaster(
268     IN PDEVICE_EXTENSION DeviceExtension
269 );
270 
271 VOID
272 DeleteFromLocalDatabase(
273     IN PUNICODE_STRING SymbolicLink,
274     IN PMOUNTDEV_UNIQUE_ID UniqueId
275 );
276 
277 VOID
278 DeleteRegistryDriveLetter(
279     IN PMOUNTDEV_UNIQUE_ID UniqueId
280 );
281 
282 VOID
283 DeleteNoDriveLetterEntry(
284     IN PMOUNTDEV_UNIQUE_ID UniqueId
285 );
286 
287 NTSTATUS
288 QueryVolumeName(
289     IN HANDLE RootDirectory,
290     IN PFILE_REPARSE_POINT_INFORMATION ReparsePointInformation,
291     IN PUNICODE_STRING FileName OPTIONAL,
292     OUT PUNICODE_STRING SymbolicName,
293     OUT PUNICODE_STRING VolumeName
294 );
295 
296 HANDLE
297 OpenRemoteDatabase(
298     IN PDEVICE_INFORMATION DeviceInformation,
299     IN BOOLEAN MigrateDatabase
300 );
301 
302 PDATABASE_ENTRY
303 GetRemoteDatabaseEntry(
304     IN HANDLE Database,
305     IN LONG StartingOffset
306 );
307 
308 NTSTATUS
309 WriteRemoteDatabaseEntry(
310     IN HANDLE Database,
311     IN LONG Offset,
312     IN PDATABASE_ENTRY Entry
313 );
314 
315 NTSTATUS
316 CloseRemoteDatabase(
317     IN HANDLE Database
318 );
319 
320 NTSTATUS
321 AddRemoteDatabaseEntry(
322     IN HANDLE Database,
323     IN PDATABASE_ENTRY Entry
324 );
325 
326 NTSTATUS
327 DeleteRemoteDatabaseEntry(
328     IN HANDLE Database,
329     IN LONG StartingOffset
330 );
331 
332 VOID
333 NTAPI
334 ReconcileThisDatabaseWithMasterWorker(
335     IN PVOID Parameter
336 );
337 
338 /* device.c */
339 
340 DRIVER_DISPATCH MountMgrDeviceControl;
341 
342 /* notify.c */
343 VOID
344 IssueUniqueIdChangeNotifyWorker(
345     IN PUNIQUE_ID_WORK_ITEM WorkItem,
346     IN PMOUNTDEV_UNIQUE_ID UniqueId
347 );
348 
349 VOID
350 WaitForOnlinesToComplete(
351     IN PDEVICE_EXTENSION DeviceExtension
352 );
353 
354 VOID
355 RegisterForTargetDeviceNotification(
356     IN PDEVICE_EXTENSION DeviceExtension,
357     IN PDEVICE_INFORMATION DeviceInformation
358 );
359 
360 VOID
361 SendOnlineNotification(
362     IN PUNICODE_STRING SymbolicName
363 );
364 
365 VOID
366 IssueUniqueIdChangeNotify(
367     IN PDEVICE_EXTENSION DeviceExtension,
368     IN PUNICODE_STRING DeviceName,
369     IN PMOUNTDEV_UNIQUE_ID UniqueId
370 );
371 
372 VOID
373 PostOnlineNotification(
374     IN PDEVICE_EXTENSION DeviceExtension,
375     IN PUNICODE_STRING SymbolicName
376 );
377 
378 VOID
379 MountMgrNotify(
380     IN PDEVICE_EXTENSION DeviceExtension
381 );
382 
383 VOID
384 MountMgrNotifyNameChange(
385     IN PDEVICE_EXTENSION DeviceExtension,
386     IN PUNICODE_STRING DeviceName,
387     IN BOOLEAN ValidateVolume
388 );
389 
390 /* uniqueid.c */
391 VOID
392 MountMgrUniqueIdChangeRoutine(
393     IN PDEVICE_EXTENSION DeviceExtension,
394     IN PMOUNTDEV_UNIQUE_ID OldUniqueId,
395     IN PMOUNTDEV_UNIQUE_ID NewUniqueId
396 );
397 
398 VOID
399 CreateNoDriveLetterEntry(
400     IN PMOUNTDEV_UNIQUE_ID UniqueId
401 );
402 
403 BOOLEAN
404 HasNoDriveLetterEntry(
405     IN PMOUNTDEV_UNIQUE_ID UniqueId
406 );
407 
408 VOID
409 UpdateReplicatedUniqueIds(
410     IN PDEVICE_INFORMATION DeviceInformation,
411     IN PDATABASE_ENTRY DatabaseEntry
412 );
413 
414 BOOLEAN
415 IsUniqueIdPresent(
416     IN PDEVICE_EXTENSION DeviceExtension,
417     IN PDATABASE_ENTRY DatabaseEntry
418 );
419 
420 /* point.c */
421 NTSTATUS
422 MountMgrCreatePointWorker(
423     IN PDEVICE_EXTENSION DeviceExtension,
424     IN PUNICODE_STRING SymbolicLinkName,
425     IN PUNICODE_STRING DeviceName
426 );
427 
428 NTSTATUS
429 QueryPointsFromSymbolicLinkName(
430     IN PDEVICE_EXTENSION DeviceExtension,
431     IN PUNICODE_STRING SymbolicName,
432     IN PIRP Irp
433 );
434 
435 NTSTATUS
436 QueryPointsFromMemory(
437     IN PDEVICE_EXTENSION DeviceExtension,
438     IN PIRP Irp,
439     IN PMOUNTDEV_UNIQUE_ID UniqueId OPTIONAL,
440     IN PUNICODE_STRING SymbolicName OPTIONAL
441 );
442 
443 /* symlink.c */
444 NTSTATUS
445 GlobalCreateSymbolicLink(
446     IN PUNICODE_STRING DosName,
447     IN PUNICODE_STRING DeviceName
448 );
449 
450 NTSTATUS
451 GlobalDeleteSymbolicLink(
452     IN PUNICODE_STRING DosName
453 );
454 
455 NTSTATUS
456 QuerySuggestedLinkName(
457     IN PUNICODE_STRING SymbolicName,
458     OUT PUNICODE_STRING SuggestedLinkName,
459     OUT PBOOLEAN UseOnlyIfThereAreNoOtherLinks
460 );
461 
462 NTSTATUS
463 QuerySymbolicLinkNamesFromStorage(
464     IN PDEVICE_EXTENSION DeviceExtension,
465     IN PDEVICE_INFORMATION DeviceInformation,
466     IN PUNICODE_STRING SuggestedLinkName,
467     IN BOOLEAN UseOnlyIfThereAreNoOtherLinks,
468     OUT PUNICODE_STRING * SymLinks,
469     OUT PULONG SymLinkCount,
470     IN BOOLEAN HasGuid,
471     IN LPGUID Guid
472 );
473 
474 PSAVED_LINK_INFORMATION
475 RemoveSavedLinks(
476     IN PDEVICE_EXTENSION DeviceExtension,
477     IN PMOUNTDEV_UNIQUE_ID UniqueId
478 );
479 
480 BOOLEAN
481 RedirectSavedLink(
482     IN PSAVED_LINK_INFORMATION SavedLinkInformation,
483     IN PUNICODE_STRING DosName,
484     IN PUNICODE_STRING NewLink
485 );
486 
487 VOID
488 SendLinkCreated(
489     IN PUNICODE_STRING SymbolicName
490 );
491 
492 NTSTATUS
493 CreateNewVolumeName(
494     OUT PUNICODE_STRING VolumeName,
495     IN PGUID VolumeGuid OPTIONAL
496 );
497 
498 BOOLEAN
499 IsDriveLetter(
500     PUNICODE_STRING SymbolicName
501 );
502 
503 VOID
504 DeleteSymbolicLinkNameFromMemory(
505     IN PDEVICE_EXTENSION DeviceExtension,
506     IN PUNICODE_STRING SymbolicLink,
507     IN BOOLEAN MarkOffline
508 );
509 
510 NTSTATUS
511 MountMgrQuerySymbolicLink(
512     IN PUNICODE_STRING SymbolicName,
513     IN OUT PUNICODE_STRING LinkTarget
514 );
515 
516 #endif /* _MNTMGR_H_ */
517