1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
/* $Id: HBDMgmt-darwin.cpp $ */
/** @file
* VBox storage devices: Host block device management API - darwin specifics.
*/
/*
* Copyright (C) 2015-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_VD
#include <VBox/cdefs.h>
#include <VBox/err.h>
#include <VBox/log.h>
#include <iprt/assert.h>
#include <iprt/list.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/once.h>
#include <iprt/semaphore.h>
#include <iprt/path.h>
#include <iprt/thread.h>
#include <DiskArbitration/DiskArbitration.h>
#include "HBDMgmt.h"
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* Claimed block device state.
*/
typedef struct HBDMGRDEV
{
/** List node. */
RTLISTNODE ListNode;
/** Handle to the DA Disk object. */
DADiskRef hDiskRef;
} HBDMGRDEV;
/** Pointer to a claimed block device. */
typedef HBDMGRDEV *PHBDMGRDEV;
/**
* Internal Host block device manager state.
*/
typedef struct HBDMGRINT
{
/** Session handle to the DiskArbitration daemon. */
DASessionRef hSessionRef;
/** Runloop reference of the worker thread. */
CFRunLoopRef hRunLoopRef;
/** Runloop source for waking up the worker thread. */
CFRunLoopSourceRef hRunLoopSrcWakeRef;
/** List of claimed block devices. */
RTLISTANCHOR ListClaimed;
/** Fast mutex protecting the list. */
RTSEMFASTMUTEX hMtxList;
/** Event sempahore to signal callback completion. */
RTSEMEVENT hEvtCallback;
/** Thread processing DA events. */
RTTHREAD hThrdDAEvts;
/** Flag whether the thread should keep running. */
volatile bool fRunning;
} HBDMGRINT;
/** Pointer to an interal block device manager state. */
typedef HBDMGRINT *PHBDMGRINT;
/**
* Helper structure containing the arguments
* for the claim/unmount callbacks.
*/
typedef struct HBDMGRDACLBKARGS
{
/** Pointer to the block device manager. */
PHBDMGRINT pThis;
/** The status code returned by the callback, after the operation completed. */
DAReturn rcDA;
/** A detailed error string in case of an error, can be NULL.
* Must be freed with RTStrFree(). */
char *pszErrDetail;
} HBDMGRDACLBKARGS;
typedef HBDMGRDACLBKARGS *PHBDMGRDACLBKARGS;
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
/**
* Unclaims the given block device and frees its state removing it from the list.
*
* @param pDev The block device to unclaim.
*/
static void hbdMgrDevUnclaim(PHBDMGRDEV pDev)
{
DADiskUnclaim(pDev->hDiskRef);
CFRelease(pDev->hDiskRef);
RTListNodeRemove(&pDev->ListNode);
RTMemFree(pDev);
}
/**
* Returns the block device given by the filename if claimed or NULL.
*
* @returns Pointer to the claimed block device or NULL if not claimed.
* @param pThis The block device manager.
* @param pszFilename The name to look for.
*/
static PHBDMGRDEV hbdMgrDevFindByName(PHBDMGRINT pThis, const char *pszFilename)
{
bool fFound = false;
const char *pszFilenameStripped = RTPathFilename(pszFilename);
AssertPtrReturn(pszFilenameStripped, NULL);
PHBDMGRDEV pIt;
RTListForEach(&pThis->ListClaimed, pIt, HBDMGRDEV, ListNode)
{
const char *pszBSDName = DADiskGetBSDName(pIt->hDiskRef);
if (!RTStrCmp(pszFilenameStripped, pszBSDName))
{
fFound = true;
break;
}
}
return fFound ? pIt : NULL;
}
/**
* Converts a given DA return code to a VBox status code.
*
* @returns VBox status code.
* @param hReturn The status code returned by a DA API call.
*/
static int hbdMgrDAReturn2VBoxStatus(DAReturn hReturn)
{
int rc = VERR_UNRESOLVED_ERROR;
switch (hReturn)
{
case kDAReturnBusy:
rc = VERR_RESOURCE_BUSY;
break;
case kDAReturnNotMounted:
case kDAReturnBadArgument:
rc = VERR_INVALID_PARAMETER;
break;
case kDAReturnNotPermitted:
case kDAReturnNotPrivileged:
case kDAReturnExclusiveAccess:
rc = VERR_ACCESS_DENIED;
break;
case kDAReturnNoResources:
rc = VERR_NO_MEMORY;
break;
case kDAReturnNotFound:
rc = VERR_NOT_FOUND;
break;
case kDAReturnNotReady:
rc = VERR_TRY_AGAIN;
break;
case kDAReturnNotWritable:
rc = VERR_WRITE_PROTECT;
break;
case kDAReturnUnsupported:
rc = VERR_NOT_SUPPORTED;
break;
case kDAReturnError:
default:
rc = VERR_UNRESOLVED_ERROR;
}
return rc;
}
/**
* Implements the OS X callback DADiskClaimCallback.
*
* This notifies us that the async DADiskClaim()/DADiskUnmount call has
* completed.
*
* @param hDiskRef The disk that was attempted claimed / unmounted.
* @param hDissenterRef NULL on success, contains details on failure.
* @param pvContext Pointer to the return code variable.
*/
static void hbdMgrDACallbackComplete(DADiskRef hDiskRef, DADissenterRef hDissenterRef, void *pvContext)
{
RT_NOREF(hDiskRef);
PHBDMGRDACLBKARGS pArgs = (PHBDMGRDACLBKARGS)pvContext;
pArgs->pszErrDetail = NULL;
if (!hDissenterRef)
pArgs->rcDA = kDAReturnSuccess;
else
{
CFStringRef hStrErr = DADissenterGetStatusString(hDissenterRef);
if (hStrErr)
{
const char *pszErrDetail = CFStringGetCStringPtr(hStrErr, kCFStringEncodingUTF8);
if (pszErrDetail)
pArgs->pszErrDetail = RTStrDup(pszErrDetail);
CFRelease(hStrErr);
}
pArgs->rcDA = DADissenterGetStatus(hDissenterRef);
}
RTSemEventSignal(pArgs->pThis->hEvtCallback);
}
/**
* Implements the OS X callback DADiskMountApprovalCallback.
*
* This notifies us about any attempt to mount a volume. If we claimed the
* volume or the complete disk containing the volume we will deny the attempt.
*
* @returns Reference to a DADissenter object which contains the result.
* @param hDiskRef The disk that is about to be mounted.
* @param pvContext Pointer to the block device manager.
*/
static DADissenterRef hbdMgrDAMountApprovalCallback(DADiskRef hDiskRef, void *pvContext)
{
PHBDMGRINT pThis = (PHBDMGRINT)pvContext;
DADiskRef hDiskParentRef = DADiskCopyWholeDisk(hDiskRef);
const char *pszBSDName = DADiskGetBSDName(hDiskRef);
const char *pszBSDNameParent = hDiskParentRef ? DADiskGetBSDName(hDiskParentRef) : NULL;
DADissenterRef hDissenterRef = NULL;
RTSemFastMutexRequest(pThis->hMtxList);
PHBDMGRDEV pIt;
RTListForEach(&pThis->ListClaimed, pIt, HBDMGRDEV, ListNode)
{
const char *pszBSDNameCur = DADiskGetBSDName(pIt->hDiskRef);
/*
* Prevent mounting any volume we have in use. This applies to the case
* where we have the whole disk occupied but a single volume is about to be
* mounted.
*/
if ( !RTStrCmp(pszBSDNameCur, pszBSDName)
|| ( pszBSDNameParent
&& !RTStrCmp(pszBSDNameParent, pszBSDNameCur)))
{
CFStringRef hStrReason = CFStringCreateWithCString(kCFAllocatorDefault, "The disk is currently in use by VirtualBox and cannot be mounted", kCFStringEncodingUTF8);
hDissenterRef = DADissenterCreate(kCFAllocatorDefault, kDAReturnExclusiveAccess, hStrReason);
break;
}
}
RTSemFastMutexRelease(pThis->hMtxList);
if (hDiskParentRef)
CFRelease(hDiskParentRef);
return hDissenterRef;
}
/**
* Implements OS X callback CFRunLoopSourceContext::perform.
*
* Dummy handler for the wakeup source to kick the worker thread.
*
* @param pInfo Opaque user data given during source creation, unused.
*/
static void hbdMgrDAPerformWakeup(void *pInfo)
{
RT_NOREF(pInfo);
}
/**
* Worker function of the thread processing messages from the Disk Arbitration daemon.
*
* @returns IPRT status code.
* @param hThreadSelf The thread handle.
* @param pvUser Opaque user data, the block device manager instance.
*/
static DECLCALLBACK(int) hbdMgrDAWorker(RTTHREAD hThreadSelf, void *pvUser)
{
PHBDMGRINT pThis = (PHBDMGRINT)pvUser;
/* Provide the runloop reference. */
pThis->hRunLoopRef = CFRunLoopGetCurrent();
RTThreadUserSignal(hThreadSelf);
/* Add the wake source to our runloop so we get notified about state changes. */
CFRunLoopAddSource(pThis->hRunLoopRef, pThis->hRunLoopSrcWakeRef, kCFRunLoopCommonModes);
/* Do what we are here for. */
while (ASMAtomicReadBool(&pThis->fRunning))
{
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, true);
}
/* Remove the wakeup source form our runloop. */
CFRunLoopRemoveSource(pThis->hRunLoopRef, pThis->hRunLoopSrcWakeRef, kCFRunLoopCommonModes);
return VINF_SUCCESS;
}
DECLHIDDEN(int) HBDMgrCreate(PHBDMGR phHbdMgr)
{
AssertPtrReturn(phHbdMgr, VERR_INVALID_POINTER);
PHBDMGRINT pThis = (PHBDMGRINT)RTMemAllocZ(sizeof(HBDMGRINT));
if (RT_UNLIKELY(!pThis))
return VERR_NO_MEMORY;
int rc = VINF_SUCCESS;
RTListInit(&pThis->ListClaimed);
pThis->fRunning = true;
pThis->hSessionRef = DASessionCreate(kCFAllocatorDefault);
if (pThis->hSessionRef)
{
rc = RTSemFastMutexCreate(&pThis->hMtxList);
if (RT_SUCCESS(rc))
{
rc = RTSemEventCreate(&pThis->hEvtCallback);
if (RT_SUCCESS(rc))
{
CFRunLoopSourceContext CtxRunLoopSource;
CtxRunLoopSource.version = 0;
CtxRunLoopSource.info = NULL;
CtxRunLoopSource.retain = NULL;
CtxRunLoopSource.release = NULL;
CtxRunLoopSource.copyDescription = NULL;
CtxRunLoopSource.equal = NULL;
CtxRunLoopSource.hash = NULL;
CtxRunLoopSource.schedule = NULL;
CtxRunLoopSource.cancel = NULL;
CtxRunLoopSource.perform = hbdMgrDAPerformWakeup;
pThis->hRunLoopSrcWakeRef = CFRunLoopSourceCreate(NULL, 0, &CtxRunLoopSource);
if (CFRunLoopSourceIsValid(pThis->hRunLoopSrcWakeRef))
{
rc = RTThreadCreate(&pThis->hThrdDAEvts, hbdMgrDAWorker, pThis, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "HbdDA-Wrk");
if (RT_SUCCESS(rc))
{
/* Wait for the thread to start up and provide the runloop reference. */
rc = RTThreadUserWait(pThis->hThrdDAEvts, RT_INDEFINITE_WAIT);
AssertRC(rc);
AssertPtr(pThis->hRunLoopRef);
DARegisterDiskMountApprovalCallback(pThis->hSessionRef, NULL, hbdMgrDAMountApprovalCallback, pThis);
DASessionScheduleWithRunLoop(pThis->hSessionRef, pThis->hRunLoopRef, kCFRunLoopDefaultMode);
*phHbdMgr = pThis;
return VINF_SUCCESS;
}
CFRelease(pThis->hRunLoopSrcWakeRef);
}
}
RTSemFastMutexDestroy(pThis->hMtxList);
}
CFRelease(pThis->hSessionRef);
}
else
rc = VERR_NO_MEMORY;
RTMemFree(pThis);
return rc;
}
DECLHIDDEN(void) HBDMgrDestroy(HBDMGR hHbdMgr)
{
PHBDMGRINT pThis = hHbdMgr;
AssertPtrReturnVoid(pThis);
/* Unregister the mount approval and DA session from the runloop. */
DASessionUnscheduleFromRunLoop(pThis->hSessionRef, pThis->hRunLoopRef, kCFRunLoopDefaultMode);
DAUnregisterApprovalCallback(pThis->hSessionRef, (void *)hbdMgrDAMountApprovalCallback, pThis);
/* Kick the worker thread to exit. */
ASMAtomicXchgBool(&pThis->fRunning, false);
CFRunLoopSourceSignal(pThis->hRunLoopSrcWakeRef);
CFRunLoopWakeUp(pThis->hRunLoopRef);
int rcThrd = VINF_SUCCESS;
int rc = RTThreadWait(pThis->hThrdDAEvts, RT_INDEFINITE_WAIT, &rcThrd);
AssertRC(rc); AssertRC(rcThrd);
CFRelease(pThis->hRunLoopSrcWakeRef);
/* Go through all claimed block devices and release them. */
RTSemFastMutexRequest(pThis->hMtxList);
PHBDMGRDEV pIt, pItNext;
RTListForEachSafe(&pThis->ListClaimed, pIt, pItNext, HBDMGRDEV, ListNode)
{
hbdMgrDevUnclaim(pIt);
}
RTSemFastMutexRelease(pThis->hMtxList);
CFRelease(pThis->hSessionRef);
RTSemFastMutexDestroy(pThis->hMtxList);
RTSemEventDestroy(pThis->hEvtCallback);
RTMemFree(pThis);
}
DECLHIDDEN(bool) HBDMgrIsBlockDevice(const char *pszFilename)
{
bool fIsBlockDevice = RTStrNCmp(pszFilename, "/dev/disk", sizeof("/dev/disk") - 1) == 0 ? true : false;
if (!fIsBlockDevice)
fIsBlockDevice = RTStrNCmp(pszFilename, "/dev/rdisk", sizeof("/dev/rdisk") - 1) == 0 ? true : false;
return fIsBlockDevice;
}
DECLHIDDEN(int) HBDMgrClaimBlockDevice(HBDMGR hHbdMgr, const char *pszFilename)
{
PHBDMGRINT pThis = hHbdMgr;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(HBDMgrIsBlockDevice(pszFilename), VERR_INVALID_PARAMETER);
int rc = VINF_SUCCESS;
PHBDMGRDEV pDev = hbdMgrDevFindByName(pThis, pszFilename);
if (!pDev)
{
DADiskRef hDiskRef = DADiskCreateFromBSDName(kCFAllocatorDefault, pThis->hSessionRef, pszFilename);
if (hDiskRef)
{
HBDMGRDACLBKARGS CalllbackArgs;
CalllbackArgs.pThis = pThis;
CalllbackArgs.rcDA = kDAReturnSuccess;
/* Claim the device. */
DADiskClaim(hDiskRef, kDADiskClaimOptionDefault, NULL, NULL, hbdMgrDACallbackComplete, &CalllbackArgs);
rc = RTSemEventWait(pThis->hEvtCallback, 120 * RT_MS_1SEC);
if ( RT_SUCCESS(rc)
&& CalllbackArgs.rcDA == kDAReturnSuccess)
{
/* Unmount anything which might be mounted. */
DADiskUnmount(hDiskRef, kDADiskUnmountOptionWhole, hbdMgrDACallbackComplete, &CalllbackArgs);
rc = RTSemEventWait(pThis->hEvtCallback, 120 * RT_MS_1SEC);
if ( RT_SUCCESS(rc)
&& ( CalllbackArgs.rcDA == kDAReturnSuccess
|| CalllbackArgs.rcDA == kDAReturnNotMounted))
{
pDev = (PHBDMGRDEV)RTMemAllocZ(sizeof(HBDMGRDEV));
if (RT_LIKELY(pDev))
{
pDev->hDiskRef = hDiskRef;
RTSemFastMutexRequest(pThis->hMtxList);
RTListAppend(&pThis->ListClaimed, &pDev->ListNode);
RTSemFastMutexRelease(pThis->hMtxList);
rc = VINF_SUCCESS;
}
else
rc = VERR_NO_MEMORY;
}
else if (RT_SUCCESS(rc))
{
rc = hbdMgrDAReturn2VBoxStatus(CalllbackArgs.rcDA);
LogRel(("HBDMgrClaimBlockDevice: DADiskUnmount(\"%s\") failed with %Rrc (%s)\n",
pszFilename, rc, CalllbackArgs.pszErrDetail ? CalllbackArgs.pszErrDetail : "<no detail>"));
if (CalllbackArgs.pszErrDetail)
RTStrFree(CalllbackArgs.pszErrDetail);
}
}
else if (RT_SUCCESS(rc))
{
rc = hbdMgrDAReturn2VBoxStatus(CalllbackArgs.rcDA);
LogRel(("HBDMgrClaimBlockDevice: DADiskClaim(\"%s\") failed with %Rrc (%s)\n",
pszFilename, rc, CalllbackArgs.pszErrDetail ? CalllbackArgs.pszErrDetail : "<no detail>"));
if (CalllbackArgs.pszErrDetail)
RTStrFree(CalllbackArgs.pszErrDetail);
}
if (RT_FAILURE(rc))
CFRelease(hDiskRef);
}
else
rc = VERR_NO_MEMORY;
}
else
rc = VERR_ALREADY_EXISTS;
return rc;
}
DECLHIDDEN(int) HBDMgrUnclaimBlockDevice(HBDMGR hHbdMgr, const char *pszFilename)
{
PHBDMGRINT pThis = hHbdMgr;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
RTSemFastMutexRequest(pThis->hMtxList);
int rc = VINF_SUCCESS;
PHBDMGRDEV pDev = hbdMgrDevFindByName(pThis, pszFilename);
if (pDev)
hbdMgrDevUnclaim(pDev);
else
rc = VERR_NOT_FOUND;
RTSemFastMutexRelease(pThis->hMtxList);
return rc;
}
DECLHIDDEN(bool) HBDMgrIsBlockDeviceClaimed(HBDMGR hHbdMgr, const char *pszFilename)
{
PHBDMGRINT pThis = hHbdMgr;
AssertPtrReturn(pThis, false);
RTSemFastMutexRequest(pThis->hMtxList);
PHBDMGRDEV pIt = hbdMgrDevFindByName(pThis, pszFilename);
RTSemFastMutexRelease(pThis->hMtxList);
return pIt ? true : false;
}
|