VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/SharedFolders/driver/vbsfhlp.c@ 42955

Last change on this file since 42955 was 40986, checked in by vboxsync, 13 years ago

header fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1/** @file
2 *
3 * VirtualBox Windows Guest Shared Folders
4 *
5 * File System Driver system helpers
6 */
7
8/*
9 * Copyright (C) 2012 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#include <ntifs.h>
21#include <ntdddisk.h>
22
23#include "vbsfhlp.h"
24
25#ifdef DEBUG
26static int s_iAllocRefCount = 0;
27#endif
28
29void vbsfHlpSleep (ULONG ulMillies)
30{
31 KEVENT event;
32 LARGE_INTEGER dueTime;
33
34 KeInitializeEvent(&event, NotificationEvent, FALSE);
35
36 dueTime.QuadPart = -10000 * (int)ulMillies;
37
38 KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, &dueTime);
39}
40
41/**
42 * Convert VBox IRT file attributes to NT file attributes
43 *
44 * @returns NT file attributes
45 * @param fMode IRT file attributes
46 *
47 */
48uint32_t VBoxToNTFileAttributes (uint32_t fMode)
49{
50 uint32_t FileAttributes = 0;
51
52 if (fMode & RTFS_DOS_READONLY)
53 FileAttributes |= FILE_ATTRIBUTE_READONLY;
54 if (fMode & RTFS_DOS_HIDDEN)
55 FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
56 if (fMode & RTFS_DOS_SYSTEM)
57 FileAttributes |= FILE_ATTRIBUTE_SYSTEM;
58 if (fMode & RTFS_DOS_DIRECTORY)
59 FileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
60 if (fMode & RTFS_DOS_ARCHIVED)
61 FileAttributes |= FILE_ATTRIBUTE_ARCHIVE;
62 if (fMode & RTFS_DOS_NT_TEMPORARY)
63 FileAttributes |= FILE_ATTRIBUTE_TEMPORARY;
64 if (fMode & RTFS_DOS_NT_SPARSE_FILE)
65 FileAttributes |= FILE_ATTRIBUTE_SPARSE_FILE;
66 if (fMode & RTFS_DOS_NT_REPARSE_POINT)
67 FileAttributes |= FILE_ATTRIBUTE_REPARSE_POINT;
68 if (fMode & RTFS_DOS_NT_COMPRESSED)
69 FileAttributes |= FILE_ATTRIBUTE_COMPRESSED;
70 /* if (fMode & RTFS_DOS_NT_OFFLINE) */
71 if (fMode & RTFS_DOS_NT_NOT_CONTENT_INDEXED)
72 FileAttributes |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
73 if (fMode & RTFS_DOS_NT_ENCRYPTED)
74 FileAttributes |= FILE_ATTRIBUTE_ENCRYPTED;
75 if (fMode & RTFS_DOS_NT_NORMAL)
76 {
77 FileAttributes |= FILE_ATTRIBUTE_NORMAL;
78 }
79 return FileAttributes;
80}
81
82/**
83 * Convert VBox IRT file attributes to NT file attributes
84 *
85 * @returns NT file attributes
86 * @param fMode IRT file attributes
87 *
88 */
89uint32_t NTToVBoxFileAttributes (uint32_t fMode)
90{
91 uint32_t FileAttributes = 0;
92
93 if (fMode & FILE_ATTRIBUTE_READONLY)
94 FileAttributes |= RTFS_DOS_READONLY;
95 if (fMode & FILE_ATTRIBUTE_HIDDEN)
96 FileAttributes |= RTFS_DOS_HIDDEN;
97 if (fMode & FILE_ATTRIBUTE_SYSTEM)
98 FileAttributes |= RTFS_DOS_SYSTEM;
99 if (fMode & FILE_ATTRIBUTE_DIRECTORY)
100 FileAttributes |= RTFS_DOS_DIRECTORY;
101 if (fMode & FILE_ATTRIBUTE_ARCHIVE)
102 FileAttributes |= RTFS_DOS_ARCHIVED;
103 if (fMode & FILE_ATTRIBUTE_TEMPORARY)
104 FileAttributes |= RTFS_DOS_NT_TEMPORARY;
105 if (fMode & FILE_ATTRIBUTE_SPARSE_FILE)
106 FileAttributes |= RTFS_DOS_NT_SPARSE_FILE;
107 if (fMode & FILE_ATTRIBUTE_REPARSE_POINT)
108 FileAttributes |= RTFS_DOS_NT_REPARSE_POINT;
109 if (fMode & FILE_ATTRIBUTE_COMPRESSED)
110 FileAttributes |= RTFS_DOS_NT_COMPRESSED;
111 if (fMode & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
112 FileAttributes |= RTFS_DOS_NT_NOT_CONTENT_INDEXED;
113 if (fMode & FILE_ATTRIBUTE_ENCRYPTED)
114 FileAttributes |= RTFS_DOS_NT_ENCRYPTED;
115 if (fMode & FILE_ATTRIBUTE_NORMAL)
116 {
117 FileAttributes |= RTFS_DOS_NT_NORMAL;
118 }
119 return FileAttributes;
120}
121
122NTSTATUS vbsfHlpCreateDriveLetter (WCHAR Letter, UNICODE_STRING *pDeviceName)
123{
124 UNICODE_STRING driveName;
125 RtlInitUnicodeString(&driveName,L"\\??\\_:" );
126
127 /* Replace '_' with actual drive letter */
128 driveName.Buffer[driveName.Length/sizeof(WCHAR) - 2] = Letter;
129
130 return IoCreateSymbolicLink (&driveName, pDeviceName);
131}
132
133NTSTATUS vbsfHlpDeleteDriveLetter (WCHAR Letter)
134{
135 UNICODE_STRING driveName;
136 RtlInitUnicodeString(&driveName,L"\\??\\_:" );
137
138 /* Replace '_' with actual drive letter */
139 driveName.Buffer[driveName.Length/sizeof(WCHAR) - 2] = Letter;
140
141 return IoDeleteSymbolicLink (&driveName);
142}
143
144 /**
145 * Convert VBox error code to NT status code
146 *
147 * @returns NT status code
148 * @param vboxRC VBox error code
149 *
150 */
151NTSTATUS VBoxErrorToNTStatus (int vboxRC)
152{
153 NTSTATUS Status;
154
155 switch (vboxRC)
156 {
157 case VINF_SUCCESS:
158 Status = STATUS_SUCCESS;
159 break;
160
161 case VERR_ACCESS_DENIED:
162 Status = STATUS_ACCESS_DENIED;
163 break;
164
165 case VERR_NO_MORE_FILES:
166 Status = STATUS_NO_MORE_FILES;
167 break;
168
169 case VERR_PATH_NOT_FOUND:
170 Status = STATUS_OBJECT_PATH_NOT_FOUND;
171 break;
172
173 case VERR_FILE_NOT_FOUND:
174 Status = STATUS_OBJECT_NAME_NOT_FOUND;
175 break;
176
177 case VERR_DIR_NOT_EMPTY:
178 Status = STATUS_DIRECTORY_NOT_EMPTY;
179 break;
180
181 case VERR_SHARING_VIOLATION:
182 Status = STATUS_SHARING_VIOLATION;
183 break;
184
185 case VERR_FILE_LOCK_VIOLATION:
186 Status = STATUS_FILE_LOCK_CONFLICT;
187 break;
188
189 case VERR_FILE_LOCK_FAILED:
190 Status = STATUS_LOCK_NOT_GRANTED;
191 break;
192
193 case VINF_BUFFER_OVERFLOW:
194 Status = STATUS_BUFFER_OVERFLOW;
195 break;
196
197 case VERR_EOF:
198 case VINF_EOF:
199 Status = STATUS_END_OF_FILE;
200 break;
201
202 case VERR_READ_ERROR:
203 case VERR_WRITE_ERROR:
204 case VERR_FILE_IO_ERROR:
205 Status = STATUS_UNEXPECTED_IO_ERROR;
206 break;
207
208 case VERR_WRITE_PROTECT:
209 Status = STATUS_MEDIA_WRITE_PROTECTED;
210 break;
211
212 case VERR_ALREADY_EXISTS:
213 Status = STATUS_OBJECT_NAME_COLLISION;
214 break;
215
216 case VERR_NOT_A_DIRECTORY:
217 Status = STATUS_NOT_A_DIRECTORY;
218 break;
219
220 case VERR_SEEK:
221 Status = STATUS_INVALID_PARAMETER;
222 break;
223
224 case VERR_INVALID_PARAMETER:
225 Status = STATUS_INVALID_PARAMETER;
226 break;
227
228 case VERR_NOT_SUPPORTED:
229 Status = STATUS_NOT_SUPPORTED;
230 break;
231
232 default:
233 /* @todo error handling */
234 Status = STATUS_INVALID_PARAMETER;
235 Log(("Unexpected vbox error %Rrc\n",
236 vboxRC));
237 break;
238 }
239 return Status;
240}
241
242PVOID vbsfAllocNonPagedMem (ULONG ulSize)
243{
244 PVOID pMemory = NULL;
245
246#ifdef DEBUG
247 s_iAllocRefCount = s_iAllocRefCount + 1;
248 Log(("vbsfAllocNonPagedMem: RefCnt after incrementing: %d\n", s_iAllocRefCount));
249#endif
250
251 /* Tag is reversed (a.k.a "SHFL") to display correctly in debuggers, so search for "SHFL" */
252 pMemory = ExAllocatePoolWithTag(NonPagedPool, ulSize, 'LFHS');
253
254 if (NULL != pMemory)
255 {
256 RtlZeroMemory(pMemory, ulSize);
257#ifdef DEBUG
258 Log(("vbsfAllocNonPagedMem: Allocated %d bytes of memory at %p.\n", ulSize, pMemory));
259#endif
260 }
261 else
262 {
263#ifdef DEBUG
264 Log(("vbsfAllocNonPagedMem: ERROR: Could not allocate %d bytes of memory!\n", ulSize));
265#endif
266 }
267
268 return pMemory;
269}
270
271void vbsfFreeNonPagedMem (PVOID lpMem)
272{
273#ifdef DEBUG
274 s_iAllocRefCount = s_iAllocRefCount - 1;
275 Log(("vbsfFreeNonPagedMem: RefCnt after decrementing: %d\n", s_iAllocRefCount));
276#endif
277
278 Assert(lpMem);
279
280 /* MSDN: The ExFreePoolWithTag routine issues a bug check if the specified value for Tag does not match the tag value passed
281 to the routine that originally allocated the memory block. Otherwise, the behavior of this routine is identical to ExFreePool. */
282 ExFreePoolWithTag(lpMem, 'LFHS');
283 lpMem = NULL;
284}
285
286#if 0 //def DEBUG
287/**
288 * Callback for RTLogFormatV which writes to the backdoor.
289 * See PFNLOGOUTPUT() for details.
290 */
291static DECLCALLBACK(size_t) rtLogBackdoorOutput(void *pv, const char *pachChars, size_t cbChars)
292{
293 RTLogWriteUser(pachChars, cbChars);
294 return cbChars;
295}
296
297int RTLogBackdoorPrintf1(const char *pszFormat, ...)
298{
299 va_list args;
300
301 LARGE_INTEGER time;
302
303 KeQueryTickCount(&time);
304
305 RTLogBackdoorPrintf("T=%RX64 ", time.QuadPart);
306 va_start(args, pszFormat);
307 RTLogFormatV(rtLogBackdoorOutput, NULL, pszFormat, args);
308 va_end(args);
309
310 return 0;
311}
312#endif
313
314#if defined(DEBUG) || defined (LOG_ENABLED)
315static PCHAR PnPMinorFunctionString(LONG MinorFunction)
316{
317 switch (MinorFunction)
318 {
319 case IRP_MN_START_DEVICE:
320 return "IRP_MJ_PNP - IRP_MN_START_DEVICE";
321 case IRP_MN_QUERY_REMOVE_DEVICE:
322 return "IRP_MJ_PNP - IRP_MN_QUERY_REMOVE_DEVICE";
323 case IRP_MN_REMOVE_DEVICE:
324 return "IRP_MJ_PNP - IRP_MN_REMOVE_DEVICE";
325 case IRP_MN_CANCEL_REMOVE_DEVICE:
326 return "IRP_MJ_PNP - IRP_MN_CANCEL_REMOVE_DEVICE";
327 case IRP_MN_STOP_DEVICE:
328 return "IRP_MJ_PNP - IRP_MN_STOP_DEVICE";
329 case IRP_MN_QUERY_STOP_DEVICE:
330 return "IRP_MJ_PNP - IRP_MN_QUERY_STOP_DEVICE";
331 case IRP_MN_CANCEL_STOP_DEVICE:
332 return "IRP_MJ_PNP - IRP_MN_CANCEL_STOP_DEVICE";
333 case IRP_MN_QUERY_DEVICE_RELATIONS:
334 return "IRP_MJ_PNP - IRP_MN_QUERY_DEVICE_RELATIONS";
335 case IRP_MN_QUERY_INTERFACE:
336 return "IRP_MJ_PNP - IRP_MN_QUERY_INTERFACE";
337 case IRP_MN_QUERY_CAPABILITIES:
338 return "IRP_MJ_PNP - IRP_MN_QUERY_CAPABILITIES";
339 case IRP_MN_QUERY_RESOURCES:
340 return "IRP_MJ_PNP - IRP_MN_QUERY_RESOURCES";
341 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
342 return "IRP_MJ_PNP - IRP_MN_QUERY_RESOURCE_REQUIREMENTS";
343 case IRP_MN_QUERY_DEVICE_TEXT:
344 return "IRP_MJ_PNP - IRP_MN_QUERY_DEVICE_TEXT";
345 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
346 return "IRP_MJ_PNP - IRP_MN_FILTER_RESOURCE_REQUIREMENTS";
347 case IRP_MN_READ_CONFIG:
348 return "IRP_MJ_PNP - IRP_MN_READ_CONFIG";
349 case IRP_MN_WRITE_CONFIG:
350 return "IRP_MJ_PNP - IRP_MN_WRITE_CONFIG";
351 case IRP_MN_EJECT:
352 return "IRP_MJ_PNP - IRP_MN_EJECT";
353 case IRP_MN_SET_LOCK:
354 return "IRP_MJ_PNP - IRP_MN_SET_LOCK";
355 case IRP_MN_QUERY_ID:
356 return "IRP_MJ_PNP - IRP_MN_QUERY_ID";
357 case IRP_MN_QUERY_PNP_DEVICE_STATE:
358 return "IRP_MJ_PNP - IRP_MN_QUERY_PNP_DEVICE_STATE";
359 case IRP_MN_QUERY_BUS_INFORMATION:
360 return "IRP_MJ_PNP - IRP_MN_QUERY_BUS_INFORMATION";
361 case IRP_MN_DEVICE_USAGE_NOTIFICATION:
362 return "IRP_MJ_PNP - IRP_MN_DEVICE_USAGE_NOTIFICATION";
363 case IRP_MN_SURPRISE_REMOVAL:
364 return "IRP_MJ_PNP - IRP_MN_SURPRISE_REMOVAL";
365 default:
366 return "IRP_MJ_PNP - unknown_pnp_irp";
367 }
368}
369
370PCHAR MajorFunctionString(UCHAR MajorFunction, LONG MinorFunction)
371{
372 switch (MinorFunction)
373 {
374 case IRP_MJ_CREATE:
375 return "IRP_MJ_CREATE";
376 case IRP_MJ_CREATE_NAMED_PIPE:
377 return "IRP_MJ_CREATE_NAMED_PIPE";
378 case IRP_MJ_CLOSE:
379 return "IRP_MJ_CLOSE";
380 case IRP_MJ_READ:
381 return "IRP_MJ_READ";
382 case IRP_MJ_WRITE:
383 return "IRP_MJ_WRITE";
384 case IRP_MJ_QUERY_INFORMATION:
385 return "IRP_MJ_QUERY_INFORMATION";
386 case IRP_MJ_SET_INFORMATION:
387 return "IRP_MJ_SET_INFORMATION";
388 case IRP_MJ_QUERY_EA:
389 return "IRP_MJ_QUERY_EA";
390 case IRP_MJ_SET_EA:
391 return "IRP_MJ_SET_EA";
392 case IRP_MJ_FLUSH_BUFFERS:
393 return "IRP_MJ_FLUSH_BUFFERS";
394 case IRP_MJ_QUERY_VOLUME_INFORMATION:
395 return "IRP_MJ_QUERY_VOLUME_INFORMATION";
396 case IRP_MJ_SET_VOLUME_INFORMATION:
397 return "IRP_MJ_SET_VOLUME_INFORMATION";
398 case IRP_MJ_DIRECTORY_CONTROL:
399 return "IRP_MJ_DIRECTORY_CONTROL";
400 case IRP_MJ_FILE_SYSTEM_CONTROL:
401 return "IRP_MJ_FILE_SYSTEM_CONTROL";
402 case IRP_MJ_DEVICE_CONTROL:
403 return "IRP_MJ_DEVICE_CONTROL";
404 case IRP_MJ_INTERNAL_DEVICE_CONTROL:
405 return "IRP_MJ_INTERNAL_DEVICE_CONTROL";
406 case IRP_MJ_SHUTDOWN:
407 return "IRP_MJ_SHUTDOWN";
408 case IRP_MJ_LOCK_CONTROL:
409 return "IRP_MJ_LOCK_CONTROL";
410 case IRP_MJ_CLEANUP:
411 return "IRP_MJ_CLEANUP";
412 case IRP_MJ_CREATE_MAILSLOT:
413 return "IRP_MJ_CREATE_MAILSLOT";
414 case IRP_MJ_QUERY_SECURITY:
415 return "IRP_MJ_QUERY_SECURITY";
416 case IRP_MJ_SET_SECURITY:
417 return "IRP_MJ_SET_SECURITY";
418 case IRP_MJ_POWER:
419 return "IRP_MJ_POWER";
420 case IRP_MJ_SYSTEM_CONTROL:
421 return "IRP_MJ_SYSTEM_CONTROL";
422 case IRP_MJ_DEVICE_CHANGE:
423 return "IRP_MJ_DEVICE_CHANGE";
424 case IRP_MJ_QUERY_QUOTA:
425 return "IRP_MJ_QUERY_QUOTA";
426 case IRP_MJ_SET_QUOTA:
427 return "IRP_MJ_SET_QUOTA";
428 case IRP_MJ_PNP:
429 return PnPMinorFunctionString(MinorFunction);
430
431 default:
432 return "unknown_pnp_irp";
433 }
434}
435#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette