summaryrefslogtreecommitdiffstats
path: root/src/VBox/Additions/darwin/VBoxSF/VBoxSF-VfsOps.cpp
blob: cb61d7a42850067339f5f8e806660e22662096b9 (plain)
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/* $Id: VBoxSF-VfsOps.cpp $ */
/** @file
 * VBoxFS - Darwin Shared Folders, Virtual File System Operations.
 */

/*
 * Copyright (C) 2013-2019 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_SHARED_FOLDERS
#include "VBoxSFInternal.h"

#include <iprt/assert.h>
#include <iprt/asm.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <VBox/log.h>



/**
 * vfsops::vfs_getattr implementation.
 *
 * @returns 0 on success or errno.h value on failure.
 * @param   pMount      The mount data structure.
 * @param   pFsAttr     Input & output structure.
 * @param   pContext    Unused kAuth parameter.
 */
static int vboxSfDwnVfsGetAttr(mount_t pMount, struct vfs_attr *pFsAttr, vfs_context_t pContext)
{
    PVBOXSFMNTDATA pThis = (PVBOXSFMNTDATA)vfs_fsprivate(pMount);
    AssertReturn(pThis, EBADMSG);
    LogFlow(("vboxSfDwnVfsGetAttr: %s\n", pThis->MntInfo.szFolder));
    RT_NOREF(pContext);

    /*
     * Get the file system stats from the host.
     */
    int rc;
    struct MyEmbReq
    {
        VBGLIOCIDCHGCMFASTCALL  Hdr;
        VMMDevHGCMCall          Call;
        VBoxSFParmInformation   Parms;
        SHFLVOLINFO             VolInfo;
    } *pReq = (struct MyEmbReq *)VbglR0PhysHeapAlloc(sizeof(*pReq));
    if (pReq)
    {
        RT_ZERO(pReq->VolInfo);

        VBGLIOCIDCHGCMFASTCALL_INIT(&pReq->Hdr, VbglR0PhysHeapGetPhysAddr(pReq), &pReq->Call, g_SfClientDarwin.idClient,
                                    SHFL_FN_INFORMATION, SHFL_CPARMS_INFORMATION, sizeof(*pReq));
        pReq->Parms.id32Root.type               = VMMDevHGCMParmType_32bit;
        pReq->Parms.id32Root.u.value32          = pThis->hHostFolder.root;
        pReq->Parms.u64Handle.type              = VMMDevHGCMParmType_64bit;
        pReq->Parms.u64Handle.u.value64         = 0;
        pReq->Parms.f32Flags.type               = VMMDevHGCMParmType_32bit;
        pReq->Parms.f32Flags.u.value32          = SHFL_INFO_VOLUME | SHFL_INFO_GET;
        pReq->Parms.cb32.type                   = VMMDevHGCMParmType_32bit;
        pReq->Parms.cb32.u.value32              = sizeof(pReq->VolInfo);
        pReq->Parms.pInfo.type                  = VMMDevHGCMParmType_Embedded;
        pReq->Parms.pInfo.u.Embedded.cbData     = sizeof(pReq->VolInfo);
        pReq->Parms.pInfo.u.Embedded.offData    = RT_UOFFSETOF(struct MyEmbReq, VolInfo) - sizeof(VBGLIOCIDCHGCMFASTCALL);
        pReq->Parms.pInfo.u.Embedded.fFlags     = VBOX_HGCM_F_PARM_DIRECTION_FROM_HOST;

        int vrc = VbglR0HGCMFastCall(g_SfClientDarwin.handle, &pReq->Hdr, sizeof(*pReq));
        if (RT_SUCCESS(vrc))
            vrc = pReq->Call.header.result;
        if (RT_SUCCESS(vrc))
        {
            /*
             * Fill in stuff.
             */
            /* Copy over the results we got from the host. */
            uint32_t cbUnit = pReq->VolInfo.ulBytesPerSector * pReq->VolInfo.ulBytesPerAllocationUnit;
            VFSATTR_RETURN(pFsAttr, f_bsize, cbUnit);
            VFSATTR_RETURN(pFsAttr, f_iosize, _64K); /** @todo what's a good block size...  */
            VFSATTR_RETURN(pFsAttr, f_blocks, (uint64_t)pReq->VolInfo.ullTotalAllocationBytes / cbUnit);
            VFSATTR_RETURN(pFsAttr, f_bavail, (uint64_t)pReq->VolInfo.ullAvailableAllocationBytes / cbUnit);
            VFSATTR_RETURN(pFsAttr, f_bfree,  (uint64_t)pReq->VolInfo.ullAvailableAllocationBytes / cbUnit);
            VFSATTR_RETURN(pFsAttr, f_bused,
                           ((uint64_t)pReq->VolInfo.ullTotalAllocationBytes - (uint64_t)pReq->VolInfo.ullAvailableAllocationBytes) / cbUnit);
            fsid_t const fsid = { { vfs_statfs(pMount)->f_fsid.val[0], vfs_typenum(pMount) } };
            VFSATTR_RETURN(pFsAttr, f_fsid, fsid);

            /* f_owner is handled by caller. */
            /* f_signature is handled by caller. */

            struct timespec TmpTv = { 1084190406, 0 };
            VFSATTR_RETURN(pFsAttr, f_create_time, TmpTv);

            /*
             * Unsupported bits.
             */
            /* Dummies for some values we don't support. */
            VFSATTR_RETURN(pFsAttr, f_objcount, 0);
            VFSATTR_RETURN(pFsAttr, f_filecount, 0);
            VFSATTR_RETURN(pFsAttr, f_dircount, 0);
            VFSATTR_RETURN(pFsAttr, f_maxobjcount, UINT32_MAX);
            VFSATTR_RETURN(pFsAttr, f_files, UINT32_MAX);
            VFSATTR_RETURN(pFsAttr, f_ffree, UINT32_MAX);
            VFSATTR_RETURN(pFsAttr, f_fssubtype, 0);
            VFSATTR_RETURN(pFsAttr, f_carbon_fsid, 0);

            /* Totally not supported: */
            VFSATTR_CLEAR_ACTIVE(pFsAttr, f_modify_time);
            VFSATTR_CLEAR_ACTIVE(pFsAttr, f_access_time);
            VFSATTR_CLEAR_ACTIVE(pFsAttr, f_backup_time);

            /*
             * Annoying capability stuff.
             * The 'valid' bits are only supposed to be set when we know for sure.
             */
            if (VFSATTR_IS_ACTIVE(pFsAttr, f_capabilities))
            {
                vol_capabilities_attr_t *pCaps = &pFsAttr->f_capabilities;

                pCaps->valid[VOL_CAPABILITIES_FORMAT]            = VOL_CAP_FMT_PERSISTENTOBJECTIDS
                                                                 | VOL_CAP_FMT_SYMBOLICLINKS
                                                                 | VOL_CAP_FMT_HARDLINKS
                                                                 | VOL_CAP_FMT_JOURNAL
                                                                 | VOL_CAP_FMT_JOURNAL_ACTIVE
                                                                 | VOL_CAP_FMT_NO_ROOT_TIMES
                                                                 | VOL_CAP_FMT_SPARSE_FILES
                                                                 | VOL_CAP_FMT_ZERO_RUNS
                                                                 | VOL_CAP_FMT_CASE_SENSITIVE
                                                                 | VOL_CAP_FMT_CASE_PRESERVING
                                                                 | VOL_CAP_FMT_FAST_STATFS
                                                                 | VOL_CAP_FMT_2TB_FILESIZE
                                                                 | VOL_CAP_FMT_OPENDENYMODES
                                                                 | VOL_CAP_FMT_HIDDEN_FILES
                                                                 | VOL_CAP_FMT_PATH_FROM_ID
                                                                 | VOL_CAP_FMT_NO_VOLUME_SIZES
                                                                 | VOL_CAP_FMT_DECMPFS_COMPRESSION
                                                                 | VOL_CAP_FMT_64BIT_OBJECT_IDS;
                pCaps->capabilities[VOL_CAPABILITIES_FORMAT]     = VOL_CAP_FMT_2TB_FILESIZE
                                                                 /// @todo | VOL_CAP_FMT_SYMBOLICLINKS - later
                                                                 /// @todo | VOL_CAP_FMT_SPARSE_FILES - probably, needs testing.
                                                                 /*| VOL_CAP_FMT_CASE_SENSITIVE - case-insensitive */
                                                                 | VOL_CAP_FMT_CASE_PRESERVING
                                                                 /// @todo | VOL_CAP_FMT_HIDDEN_FILES - if windows host.
                                                                 /// @todo | VOL_CAP_FMT_OPENDENYMODES - if windows host.
                                                                 ;
                pCaps->valid[VOL_CAPABILITIES_INTERFACES]        = VOL_CAP_INT_SEARCHFS
                                                                 | VOL_CAP_INT_ATTRLIST
                                                                 | VOL_CAP_INT_NFSEXPORT
                                                                 | VOL_CAP_INT_READDIRATTR
                                                                 | VOL_CAP_INT_EXCHANGEDATA
                                                                 | VOL_CAP_INT_COPYFILE
                                                                 | VOL_CAP_INT_ALLOCATE
                                                                 | VOL_CAP_INT_VOL_RENAME
                                                                 | VOL_CAP_INT_ADVLOCK
                                                                 | VOL_CAP_INT_FLOCK
                                                                 | VOL_CAP_INT_EXTENDED_SECURITY
                                                                 | VOL_CAP_INT_USERACCESS
                                                                 | VOL_CAP_INT_MANLOCK
                                                                 | VOL_CAP_INT_NAMEDSTREAMS
                                                                 | VOL_CAP_INT_EXTENDED_ATTR;
                pCaps->capabilities[VOL_CAPABILITIES_INTERFACES] = 0
                                                                 /// @todo | VOL_CAP_INT_SEARCHFS
                                                                 /// @todo | VOL_CAP_INT_COPYFILE
                                                                 /// @todo | VOL_CAP_INT_READDIRATTR
                                                                 ;

                pCaps->valid[VOL_CAPABILITIES_RESERVED1]         = 0;
                pCaps->capabilities[VOL_CAPABILITIES_RESERVED1]  = 0;

                pCaps->valid[VOL_CAPABILITIES_RESERVED2]         = 0;
                pCaps->capabilities[VOL_CAPABILITIES_RESERVED2]  = 0;

                VFSATTR_SET_SUPPORTED(pFsAttr, f_capabilities);
            }


            /*
             * Annoying attribute stuff.
             * The 'valid' bits are only supposed to be set when we know for sure.
             */
            if (VFSATTR_IS_ACTIVE(pFsAttr, f_attributes))
            {
                vol_attributes_attr_t *pAt = &pFsAttr->f_attributes;

                pAt->validattr.commonattr  = ATTR_CMN_NAME
                                           | ATTR_CMN_DEVID
                                           | ATTR_CMN_FSID
                                           | ATTR_CMN_OBJTYPE
                                           | ATTR_CMN_OBJTAG
                                           | ATTR_CMN_OBJID
                                           | ATTR_CMN_OBJPERMANENTID
                                           | ATTR_CMN_PAROBJID
                                           | ATTR_CMN_SCRIPT
                                           | ATTR_CMN_CRTIME
                                           | ATTR_CMN_MODTIME
                                           | ATTR_CMN_CHGTIME
                                           | ATTR_CMN_ACCTIME
                                           | ATTR_CMN_BKUPTIME
                                           | ATTR_CMN_FNDRINFO
                                           | ATTR_CMN_OWNERID
                                           | ATTR_CMN_GRPID
                                           | ATTR_CMN_ACCESSMASK
                                           | ATTR_CMN_FLAGS
                                           | ATTR_CMN_USERACCESS
                                           | ATTR_CMN_EXTENDED_SECURITY
                                           | ATTR_CMN_UUID
                                           | ATTR_CMN_GRPUUID
                                           | ATTR_CMN_FILEID
                                           | ATTR_CMN_PARENTID
                                           | ATTR_CMN_FULLPATH
                                           | ATTR_CMN_ADDEDTIME;
                pAt->nativeattr.commonattr = ATTR_CMN_NAME
                                           | ATTR_CMN_DEVID
                                           | ATTR_CMN_FSID
                                           | ATTR_CMN_OBJTYPE
                                           | ATTR_CMN_OBJTAG
                                           | ATTR_CMN_OBJID
                                           //| ATTR_CMN_OBJPERMANENTID
                                           | ATTR_CMN_PAROBJID
                                           //| ATTR_CMN_SCRIPT
                                           | ATTR_CMN_CRTIME
                                           | ATTR_CMN_MODTIME
                                           | ATTR_CMN_CHGTIME
                                           | ATTR_CMN_ACCTIME
                                           //| ATTR_CMN_BKUPTIME
                                           //| ATTR_CMN_FNDRINFO
                                           //| ATTR_CMN_OWNERID
                                           //| ATTR_CMN_GRPID
                                           | ATTR_CMN_ACCESSMASK
                                           //| ATTR_CMN_FLAGS
                                           //| ATTR_CMN_USERACCESS
                                           //| ATTR_CMN_EXTENDED_SECURITY
                                           //| ATTR_CMN_UUID
                                           //| ATTR_CMN_GRPUUID
                                           | ATTR_CMN_FILEID
                                           | ATTR_CMN_PARENTID
                                           | ATTR_CMN_FULLPATH
                                           //| ATTR_CMN_ADDEDTIME
                                           ;
                pAt->validattr.volattr     = ATTR_VOL_FSTYPE
                                           | ATTR_VOL_SIGNATURE
                                           | ATTR_VOL_SIZE
                                           | ATTR_VOL_SPACEFREE
                                           | ATTR_VOL_SPACEAVAIL
                                           | ATTR_VOL_MINALLOCATION
                                           | ATTR_VOL_ALLOCATIONCLUMP
                                           | ATTR_VOL_IOBLOCKSIZE
                                           | ATTR_VOL_OBJCOUNT
                                           | ATTR_VOL_FILECOUNT
                                           | ATTR_VOL_DIRCOUNT
                                           | ATTR_VOL_MAXOBJCOUNT
                                           | ATTR_VOL_MOUNTPOINT
                                           | ATTR_VOL_NAME
                                           | ATTR_VOL_MOUNTFLAGS
                                           | ATTR_VOL_MOUNTEDDEVICE
                                           | ATTR_VOL_ENCODINGSUSED
                                           | ATTR_VOL_CAPABILITIES
                                           | ATTR_VOL_UUID
                                           | ATTR_VOL_ATTRIBUTES
                                           | ATTR_VOL_INFO;
                pAt->nativeattr.volattr     = ATTR_VOL_FSTYPE
                                            //| ATTR_VOL_SIGNATURE
                                            | ATTR_VOL_SIZE
                                            | ATTR_VOL_SPACEFREE
                                            | ATTR_VOL_SPACEAVAIL
                                            | ATTR_VOL_MINALLOCATION
                                            | ATTR_VOL_ALLOCATIONCLUMP
                                            | ATTR_VOL_IOBLOCKSIZE
                                            //| ATTR_VOL_OBJCOUNT
                                            //| ATTR_VOL_FILECOUNT
                                            //| ATTR_VOL_DIRCOUNT
                                            //| ATTR_VOL_MAXOBJCOUNT
                                            //| ATTR_VOL_MOUNTPOINT - ??
                                            | ATTR_VOL_NAME
                                            | ATTR_VOL_MOUNTFLAGS
                                            | ATTR_VOL_MOUNTEDDEVICE
                                            //| ATTR_VOL_ENCODINGSUSED
                                            | ATTR_VOL_CAPABILITIES
                                            //| ATTR_VOL_UUID
                                            | ATTR_VOL_ATTRIBUTES
                                            //| ATTR_VOL_INFO
                                            ;
                pAt->validattr.dirattr      = ATTR_DIR_LINKCOUNT
                                            | ATTR_DIR_ENTRYCOUNT
                                            | ATTR_DIR_MOUNTSTATUS;
                pAt->nativeattr.dirattr     = 0 //ATTR_DIR_LINKCOUNT
                                            | ATTR_DIR_ENTRYCOUNT
                                            | ATTR_DIR_MOUNTSTATUS
                                            ;
                pAt->validattr.fileattr     = ATTR_FILE_LINKCOUNT
                                            | ATTR_FILE_TOTALSIZE
                                            | ATTR_FILE_ALLOCSIZE
                                            | ATTR_FILE_IOBLOCKSIZE
                                            | ATTR_FILE_DEVTYPE
                                            | ATTR_FILE_FORKCOUNT
                                            | ATTR_FILE_FORKLIST
                                            | ATTR_FILE_DATALENGTH
                                            | ATTR_FILE_DATAALLOCSIZE
                                            | ATTR_FILE_RSRCLENGTH
                                            | ATTR_FILE_RSRCALLOCSIZE;
                pAt->nativeattr.fileattr    = 0
                                            //|ATTR_FILE_LINKCOUNT
                                            | ATTR_FILE_TOTALSIZE
                                            | ATTR_FILE_ALLOCSIZE
                                            //| ATTR_FILE_IOBLOCKSIZE
                                            | ATTR_FILE_DEVTYPE
                                            //| ATTR_FILE_FORKCOUNT
                                            //| ATTR_FILE_FORKLIST
                                            | ATTR_FILE_DATALENGTH
                                            | ATTR_FILE_DATAALLOCSIZE
                                            | ATTR_FILE_RSRCLENGTH
                                            | ATTR_FILE_RSRCALLOCSIZE
                                            ;
                pAt->validattr.forkattr     = ATTR_FORK_TOTALSIZE
                                            | ATTR_FORK_ALLOCSIZE;
                pAt->nativeattr.forkattr    = 0
                                            //| ATTR_FORK_TOTALSIZE
                                            //| ATTR_FORK_ALLOCSIZE
                                            ;
                VFSATTR_SET_SUPPORTED(pFsAttr, f_attributes);
            }

            if (VFSATTR_IS_ACTIVE(pFsAttr, f_vol_name))
            {
                RTStrCopy(pFsAttr->f_vol_name, MAXPATHLEN, pThis->MntInfo.szFolder);
                VFSATTR_SET_SUPPORTED(pFsAttr, f_vol_name);
            }

            rc = 0;
        }
        else
        {
            Log(("vboxSfOs2QueryFileInfo: VbglR0SfFsInfo failed: %Rrc\n", vrc));
            rc = RTErrConvertToErrno(vrc);
        }

        VbglR0PhysHeapFree(pReq);
    }
    else
        rc = ENOMEM;
    return rc;
}


/**
 * vfsops::vfs_root implementation.
 *
 * @returns 0 on success or errno.h value on failure.
 * @param   pMount      The mount data structure.
 * @param   ppVnode     Where to return the referenced root node on success.
 * @param   pContext    Unused kAuth parameter.
 */
static int vboxSfDwnVfsRoot(mount_t pMount, vnode_t *ppVnode, vfs_context_t pContext)
{
    PVBOXSFMNTDATA pThis = (PVBOXSFMNTDATA)vfs_fsprivate(pMount);
    AssertReturn(pThis, EBADMSG);
    LogFlow(("vboxSfDwnVfsRoot: pThis=%p:{%s}\n", pThis, pThis->MntInfo.szFolder));
    RT_NOREF(pContext);

    /*
     * We shouldn't be callable during unmount, should we?
     */
    AssertReturn(vfs_isunmount(pMount), EBUSY);

    /*
     * There should always be a root node around.
     */
    if (pThis->pVnRoot)
    {
        int rc = vnode_get(pThis->pVnRoot);
        if (rc == 0)
        {
            *ppVnode = pThis->pVnRoot;
            LogFlow(("vboxSfDwnVfsRoot: return %p\n", *ppVnode));
            return 0;
        }
        Log(("vboxSfDwnVfsRoot: vnode_get failed! %d\n", rc));
        return rc;
    }

    LogRel(("vboxSfDwnVfsRoot: pVnRoot is NULL!\n"));
    return EILSEQ;
}


/**
 * vfsops::vfs_umount implementation.
 *
 * @returns 0 on success or errno.h value on failure.
 * @param   pMount      The mount data.
 * @param   fFlags      Unmount flags.
 * @param   pContext    kAuth context which we don't care much about.
 *
 */
static int vboxSfDwnVfsUnmount(mount_t pMount, int fFlags, vfs_context_t pContext)
{
    PVBOXSFMNTDATA pThis = (PVBOXSFMNTDATA)vfs_fsprivate(pMount);
    AssertReturn(pThis, 0);
    LogFlowFunc(("pThis=%p:{%s} fFlags=%#x\n", pThis, pThis->MntInfo.szFolder, fFlags));
    RT_NOREF(pContext);

    /*
     * Flush vnodes.
     */
    int rc = vflush(pMount, pThis->pVnRoot, fFlags & MNT_FORCE ? FORCECLOSE : 0);
    if (rc == 0)
    {
        /*
         * Is the file system still busy?
         *
         * Until we find a way of killing any active host calls, we cannot properly
         * respect the MNT_FORCE flag here. So, MNT_FORCE is ignored here.
         */
        if (   !pThis->pVnRoot
            || !vnode_isinuse(pThis->pVnRoot, 1))
        {
            /*
             * Release our root vnode reference and do another flush.
             */
            if (pThis->pVnRoot)
            {
                vnode_put(pThis->pVnRoot);
                pThis->pVnRoot = NULL;
            }
            vflush(pMount, NULLVP, FORCECLOSE);

            /*
             * Unmap the shared folder and destroy our mount info structure.
             */
            vfs_setfsprivate(pMount, NULL);

            rc = VbglR0SfUnmapFolder(&g_SfClientDarwin, &pThis->hHostFolder);
            AssertRC(rc);

            RT_ZERO(*pThis);
            RTMemFree(pThis);

            vfs_clearflags(pMount, MNT_LOCAL); /* ?? */
            rc = 0;

            g_cVBoxSfMounts--;
        }
        else
        {
            Log(("VBoxSF: umount failed: file system busy! (%s)\n", pThis->MntInfo.szFolder));
            rc = EBUSY;
        }
    }
    return rc;
}


/**
 * vfsops::vfs_start implementation.
 */
static int vboxSfDwnVfsStart(mount_t pMount, int fFlags, vfs_context_t pContext)
{
    RT_NOREF(pMount, fFlags, pContext);
    return 0;
}


/**
 * vfsops::vfs_mount implementation.
 *
 * @returns 0 on success or errno.h value on failure.
 * @param   pMount      The mount data structure.
 * @param   pDevVp      The device to mount.  Not used by us.
 * @param   pUserData   User space address of parameters supplied to mount().
 *                      We expect a VBOXSFDRWNMOUNTINFO structure.
 * @param   pContext    kAuth context needed in order to authentificate mount
 *                      operation.
 */
static int vboxSfDwnVfsMount(mount_t pMount, vnode_t pDevVp, user_addr_t pUserData, vfs_context_t pContext)
{
    RT_NOREF(pDevVp, pContext);

    /*
     * We don't support mount updating.
     */
    if (vfs_isupdate(pMount))
    {
        LogRel(("VBoxSF: mount: MNT_UPDATE is not supported.\n"));
        return ENOTSUP;
    }
    if (pUserData == USER_ADDR_NULL)
    {
        LogRel(("VBoxSF: mount: pUserData is NULL.\n"));
        return EINVAL;
    }
    struct vfsstatfs *pFsStats = vfs_statfs(pMount);
    AssertReturn(pFsStats, EINVAL);

    /*
     * Get the mount information from userland.
     */
    PVBOXSFMNTDATA pThis = (PVBOXSFMNTDATA)RTMemAllocZ(sizeof(*pThis));
    if (!pThis)
        return ENOMEM;
    pThis->uidMounter = pFsStats->f_owner;

    int rc = RTR0MemUserCopyFrom(&pThis->MntInfo, (RTR3PTR)pUserData, sizeof(pThis->MntInfo));
    if (RT_FAILURE(rc))
    {
        LogRel(("VBoxSF: mount: Failed to copy in mount user data: %Rrc\n", rc));
        rc = EFAULT;
    }
    else if (pThis->MntInfo.u32Magic != VBOXSFDRWNMOUNTINFO_MAGIC)
    {
        LogRel(("VBoxSF: mount: Invalid user data magic (%#x)\n", pThis->MntInfo.u32Magic));
        rc = EINVAL;
    }
    else if (   (rc = RTStrValidateEncodingEx(pThis->MntInfo.szFolder, sizeof(pThis->MntInfo.szFolder),
                                              RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED)) != VINF_SUCCESS
             || pThis->MntInfo.szFolder[0] == '\0')
    {
        LogRel(("VBoxSF: mount: Invalid or empty share name!\n"));
        rc = EINVAL;
    }
    else
    {
        /*
         * Try map the shared folder.
         */
        if (vboxSfDwnConnect())
        {
            PSHFLSTRING pName = ShflStringDupUtf8(pThis->MntInfo.szFolder);
            if (pName)
            {
                rc = VbglR0SfMapFolder(&g_SfClientDarwin, pName, &pThis->hHostFolder);
                RTMemFree(pName);
                if (RT_SUCCESS(rc))
                {

                    /*
                     * Create a root node, that avoid races later.
                     */
                    pThis->pVnRoot = vboxSfDwnVnAlloc(pMount, VDIR, NULL /*pParent*/, 0);
                    if (pThis->pVnRoot)
                    {
                        /*
                         * Fill file system stats with dummy data.
                         */
                        pFsStats->f_bsize  = 512;
                        pFsStats->f_iosize = _64K;
                        pFsStats->f_blocks = _1M;
                        pFsStats->f_bavail = _1M / 4 * 3;
                        pFsStats->f_bused  = _1M / 4;
                        pFsStats->f_files  = 1024;
                        pFsStats->f_ffree  = _64K;
                        vfs_getnewfsid(pMount); /* f_fsid */
                        /* pFsStats->f_fowner - don't touch */
                        /* pFsStats->f_fstypename - don't touch */
                        /* pFsStats->f_mntonname - don't touch */
                        RTStrCopy(pFsStats->f_mntfromname, sizeof(pFsStats->f_mntfromname), pThis->MntInfo.szFolder);
                        /* pFsStats->f_fssubtype - don't touch? */
                        /* pFsStats->f_reserved[0] - don't touch? */
                        /* pFsStats->f_reserved[1] - don't touch? */

                        /*
                         * We're good. Set private data and flags.
                         */
                        vfs_setfsprivate(pMount, pThis);
                        vfs_setflags(pMount, MNT_SYNCHRONOUS | MNT_NOSUID | MNT_NODEV);
                        /** @todo Consider flags like MNT_NOEXEC ? */

                        /// @todo vfs_setauthopaque(pMount)?
                        /// @todo vfs_clearauthopaqueaccess(pMount)?
                        /// @todo vfs_clearextendedsecurity(pMount)?

                        LogRel(("VBoxSF: mount: Successfully mounted '%s' (uidMounter=%u).\n",
                                pThis->MntInfo.szFolder, pThis->uidMounter));
                        return 0;
                    }

                    LogRel(("VBoxSF: mount: Failed to allocate root node!\n"));
                    rc = ENOMEM;
                }
                else
                {
                    LogRel(("VBoxSF: mount: VbglR0SfMapFolder failed on '%s': %Rrc\n", pThis->MntInfo.szFolder, rc));
                    rc = ENOENT;
                }
            }
            else
                rc = ENOMEM;
        }
        else
        {
            LogRel(("VBoxSF: mount: Not connected to shared folders service!\n"));
            rc = ENOTCONN;
        }
    }
    RTMemFree(pThis);
    return rc;
}


/**
 * VFS operations
 */
struct vfsops g_VBoxSfVfsOps =
{
    vboxSfDwnVfsMount,
    vboxSfDwnVfsStart,
    vboxSfDwnVfsUnmount,
    vboxSfDwnVfsRoot,
    NULL,               /* Skipped: vfs_quotactl */
    vboxSfDwnVfsGetAttr,
    NULL,               /* Skipped: vfs_sync */
    NULL,               /* Skipped: vfs_vget */
    NULL,               /* Skipped: vfs_fhtovp */
    NULL,               /* Skipped: vfs_vptofh */
    NULL,               /* Skipped: vfs_init */
    NULL,               /* Skipped: vfs_sysctl */
    NULL,               /* Skipped: vfs_setattr */
    /* Reserved */
    { NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
};